:root {
  --bg: #f4f5f7;
  --surface: #ffffff;
  --ink: #1f2a37;
  --muted: #5b6676;
  --line: #dde1e7;
  --brand: #1f2a37;
  --accent: #f5a524;
  --accent-ink: #3d2800;
  --ok: #17803d;
  --ok-bg: #e5f5ea;
  --ko: #c4291c;
  --ko-bg: #fdeceb;
  --warn: #a15c00;
  --warn-bg: #fff3dc;
  --na: #6b7280;
  --na-bg: #eef0f3;
  --radius: 12px;
  --shadow: 0 1px 2px rgba(16, 24, 40, .06), 0 1px 3px rgba(16, 24, 40, .08);
  --nav-h: 64px;
  color-scheme: light;
}

* { box-sizing: border-box; }
html { -webkit-text-size-adjust: 100%; }
body {
  margin: 0;
  background: var(--bg);
  color: var(--ink);
  font: 16px/1.45 system-ui, -apple-system, "Segoe UI", Roboto, Arial, sans-serif;
  padding-bottom: calc(var(--nav-h) + env(safe-area-inset-bottom) + 16px);
}
a { color: inherit; }
h1 { font-size: 1.45rem; margin: 0; }
h2 { font-size: 1.1rem; margin: 0 0 .75rem; }
h3 { font-size: 1rem; margin: 0; }
p { margin: .25rem 0; }
.muted { color: var(--muted); }
.small { font-size: .875rem; }

/* ---------- struttura ---------- */
.topbar {
  position: sticky; top: 0; z-index: 20;
  display: flex; align-items: center; justify-content: space-between;
  padding: 10px 16px; background: var(--brand); color: #fff;
}
.brand { display: flex; align-items: center; gap: 10px; text-decoration: none; font-weight: 700; font-size: 1.1rem; }
.saved { font-size: .85rem; opacity: 0; transition: opacity .3s; }
.saved.show { opacity: .85; }

.container { max-width: 1000px; margin: 0 auto; padding: 16px; }

.nav {
  position: fixed; left: 0; right: 0; bottom: 0; z-index: 20;
  display: flex; justify-content: space-around;
  height: calc(var(--nav-h) + env(safe-area-inset-bottom));
  padding-bottom: env(safe-area-inset-bottom);
  background: var(--surface); border-top: 1px solid var(--line);
}
.nav a {
  flex: 1; display: flex; flex-direction: column; align-items: center; justify-content: center;
  gap: 2px; font-size: .72rem; text-decoration: none; color: var(--muted);
}
.nav a .ico { font-size: 1.3rem; line-height: 1; }
.nav a.active { color: var(--ink); font-weight: 700; box-shadow: inset 0 3px 0 var(--accent); }

.page-head { display: flex; align-items: center; justify-content: space-between; gap: 12px; flex-wrap: wrap; margin-bottom: 16px; }
.page-head .sub { margin-top: 2px; }
.crumb { display: inline-block; margin-bottom: 6px; color: var(--muted); text-decoration: none; font-size: .9rem; }

.card {
  background: var(--surface); border: 1px solid var(--line); border-radius: var(--radius);
  box-shadow: var(--shadow); padding: 16px; margin-bottom: 16px;
}
.card.error { border-color: var(--ko); color: var(--ko); }
.card-head { display: flex; align-items: center; justify-content: space-between; gap: 8px; margin-bottom: 12px; flex-wrap: wrap; }
.card-head h2 { margin: 0; }

.stats { display: grid; grid-template-columns: repeat(2, minmax(0, 1fr)); gap: 12px; margin-bottom: 16px; }
@media (min-width: 720px) { .stats { grid-template-columns: repeat(4, minmax(0, 1fr)); } }
.stat { background: var(--surface); border: 1px solid var(--line); border-radius: var(--radius); padding: 14px; text-decoration: none; box-shadow: var(--shadow); }
.stat b { display: block; font-size: 1.8rem; line-height: 1.1; font-variant-numeric: tabular-nums; }
.stat span { color: var(--muted); font-size: .9rem; }
.stat.alert b { color: var(--ko); }

.grid2 { display: grid; gap: 12px; }
@media (min-width: 720px) { .grid2 { grid-template-columns: 1fr 1fr; } }

/* ---------- elenchi ---------- */
.list { display: flex; flex-direction: column; }
.row {
  display: flex; align-items: center; gap: 12px; padding: 12px 4px;
  border-bottom: 1px solid var(--line); text-decoration: none; cursor: pointer;
  background: none; border-left: 0; border-right: 0; border-top: 0; width: 100%; text-align: left; font: inherit; color: inherit;
}
.row:last-child { border-bottom: 0; }
.row:hover { background: #fafbfc; }
.row .grow { flex: 1; min-width: 0; }
.row .title { font-weight: 600; overflow-wrap: anywhere; }
.row .meta { color: var(--muted); font-size: .85rem; }
.empty { color: var(--muted); text-align: center; padding: 20px 8px; }

.badge {
  display: inline-block; padding: 2px 8px; border-radius: 999px; font-size: .75rem; font-weight: 700;
  white-space: nowrap; background: var(--na-bg); color: var(--na);
}
.badge.alta, .badge.ko { background: var(--ko-bg); color: var(--ko); }
.badge.media, .badge.warn { background: var(--warn-bg); color: var(--warn); }
.badge.bassa { background: #e8f0fe; color: #1a56b8; }
.badge.ok { background: var(--ok-bg); color: var(--ok); }
.late { color: var(--ko); font-weight: 600; }

.progress { height: 8px; background: var(--na-bg); border-radius: 999px; overflow: hidden; }
.progress > i { display: block; height: 100%; background: var(--accent); }

/* ---------- timeline ---------- */
.timeline { list-style: none; margin: 0; padding: 0; }
.timeline li { position: relative; padding: 0 0 16px 26px; }
.timeline li::before { content: ""; position: absolute; left: 7px; top: 6px; bottom: -6px; width: 2px; background: var(--line); }
.timeline li:last-child::before { display: none; }
.timeline li::after { content: ""; position: absolute; left: 1px; top: 4px; width: 14px; height: 14px; border-radius: 50%; background: var(--surface); border: 3px solid var(--accent); }
.timeline .actions { display: flex; gap: 8px; margin-top: 6px; flex-wrap: wrap; }

/* ---------- form ---------- */
label.field { display: block; margin-bottom: 12px; }
label.field > span { display: block; font-size: .85rem; font-weight: 600; color: var(--muted); margin-bottom: 4px; }
input[type=text], input[type=date], input[type=datetime-local], input[type=number], input[type=search], select, textarea {
  width: 100%; font: inherit; color: inherit; padding: 10px 12px; min-height: 44px;
  border: 1px solid var(--line); border-radius: 10px; background: #fff;
}
textarea { min-height: 80px; resize: vertical; }
input:focus, select:focus, textarea:focus { outline: 2px solid var(--accent); outline-offset: 0; border-color: var(--accent); }
input[type=range] { width: 100%; accent-color: var(--accent); }
fieldset { border: 0; padding: 0; margin: 0; min-width: 0; }
fieldset:disabled .hide-locked { display: none !important; }

.btn {
  display: inline-flex; align-items: center; justify-content: center; gap: 6px;
  min-height: 44px; padding: 8px 16px; border-radius: 10px; border: 1px solid var(--line);
  background: var(--surface); color: var(--ink); font: inherit; font-weight: 600; text-decoration: none; cursor: pointer;
}
.btn:hover { background: #f7f8fa; }
.btn.primary { background: var(--accent); border-color: var(--accent); color: var(--accent-ink); }
.btn.primary:hover { filter: brightness(.96); }
.btn.dark { background: var(--brand); border-color: var(--brand); color: #fff; }
.btn.danger { color: var(--ko); border-color: #f1c4c0; }
.btn.sm { min-height: 34px; padding: 4px 10px; font-size: .85rem; }
.btn:disabled { opacity: .5; cursor: default; }
.btns { display: flex; gap: 8px; flex-wrap: wrap; }

/* ---------- checklist ---------- */
details.cl { border: 1px solid var(--line); border-radius: 10px; margin-bottom: 10px; background: #fff; }
details.cl > summary { list-style: none; cursor: pointer; padding: 12px; display: flex; align-items: center; gap: 10px; }
details.cl > summary::-webkit-details-marker { display: none; }
details.cl > summary::before { content: "▸"; color: var(--muted); transition: transform .15s; }
details.cl[open] > summary::before { transform: rotate(90deg); }
details.cl > summary h3 { flex: 1; }
.cl-tools { display: flex; gap: 8px; padding: 0 12px 8px; flex-wrap: wrap; }
.voce { padding: 10px 12px; border-top: 1px solid var(--line); }
.voce[data-esito=NC] { background: var(--ko-bg); }
.voce-top { display: flex; gap: 10px; align-items: flex-start; flex-wrap: wrap; }
.voce-top .testo { flex: 1 1 220px; }
.seg { display: inline-flex; border: 1px solid var(--line); border-radius: 10px; overflow: hidden; flex: none; }
.seg button { min-width: 52px; min-height: 40px; border: 0; background: #fff; font: inherit; font-weight: 700; cursor: pointer; color: var(--muted); }
.seg button + button { border-left: 1px solid var(--line); }
.seg button.on[data-v=C] { background: var(--ok); color: #fff; }
.seg button.on[data-v=NC] { background: var(--ko); color: #fff; }
.seg button.on[data-v=NA] { background: var(--na); color: #fff; }
.voce-extra { display: flex; gap: 8px; margin-top: 8px; }
.voce-extra input { min-height: 38px; padding: 6px 10px; font-size: .9rem; }
.voce .crit-btn { display: none; }
.voce[data-esito=NC] .crit-btn { display: inline-flex; }

/* ---------- foto e firma ---------- */
.thumbs { display: flex; flex-wrap: wrap; gap: 8px; margin-top: 8px; }
.thumb { position: relative; }
.thumb img { width: 88px; height: 88px; object-fit: cover; border-radius: 8px; border: 1px solid var(--line); display: block; }
.thumb button { position: absolute; top: -6px; right: -6px; width: 26px; height: 26px; border-radius: 50%; border: 0; background: var(--ko); color: #fff; cursor: pointer; font-weight: 700; }
.sign { width: 100%; height: 180px; border: 1px dashed var(--muted); border-radius: 10px; background: #fff; touch-action: none; display: block; }

/* ---------- modale ---------- */
dialog.modal { border: 0; border-radius: 16px; padding: 0; width: min(640px, calc(100vw - 24px)); max-height: calc(100vh - 24px); box-shadow: 0 20px 50px rgba(0,0,0,.25); }
dialog.modal::backdrop { background: rgba(15, 23, 42, .5); }
.modal-body { padding: 20px; overflow-y: auto; }
.modal-foot { display: flex; justify-content: space-between; gap: 8px; padding: 12px 20px; border-top: 1px solid var(--line); background: #fafbfc; flex-wrap: wrap; }

.toast {
  position: fixed; left: 50%; bottom: calc(var(--nav-h) + 20px); transform: translateX(-50%) translateY(20px);
  background: var(--brand); color: #fff; padding: 10px 18px; border-radius: 999px; opacity: 0; pointer-events: none;
  transition: all .25s; z-index: 50; max-width: calc(100vw - 32px);
}
.toast.show { opacity: 1; transform: translateX(-50%) translateY(0); }

.banner { padding: 12px 16px; border-radius: 10px; margin-bottom: 16px; background: var(--warn-bg); color: var(--warn); display: flex; justify-content: space-between; align-items: center; gap: 12px; flex-wrap: wrap; }
.actionbar { display: flex; gap: 8px; flex-wrap: wrap; justify-content: flex-end; }

/* ---------- report stampabile ---------- */
.toolbar { display: flex; gap: 8px; justify-content: space-between; flex-wrap: wrap; margin-bottom: 16px; }
.report { background: #fff; padding: 32px; border: 1px solid var(--line); border-radius: 4px; font-size: 14px; }
.report header { display: flex; justify-content: space-between; gap: 16px; border-bottom: 3px solid var(--ink); padding-bottom: 12px; margin-bottom: 16px; flex-wrap: wrap; }
.report h1 { font-size: 20px; }
.report h2 { font-size: 15px; margin: 22px 0 8px; padding-bottom: 4px; border-bottom: 1px solid var(--line); text-transform: uppercase; letter-spacing: .03em; }
.report h3 { font-size: 14px; margin: 14px 0 6px; }
.report table { width: 100%; border-collapse: collapse; margin-bottom: 8px; }
.report th, .report td { border: 1px solid var(--line); padding: 6px 8px; text-align: left; vertical-align: top; }
.report th { background: #f4f5f7; font-weight: 600; }
.report table.kv th { width: 34%; }
.report .esito { font-weight: 700; white-space: nowrap; }
.report .esito.C { color: var(--ok); } .report .esito.NC { color: var(--ko); } .report .esito.NA { color: var(--na); }
.report .summary { display: grid; grid-template-columns: repeat(auto-fit, minmax(120px, 1fr)); gap: 8px; }
.report .summary div { border: 1px solid var(--line); border-radius: 6px; padding: 8px; }
.report .summary b { display: block; font-size: 20px; }
.report .photos { display: grid; grid-template-columns: repeat(auto-fill, minmax(220px, 1fr)); gap: 12px; margin-top: 6px; }
.report .photos figure { margin: 0; break-inside: avoid; }
.report .photos img { width: 100%; aspect-ratio: 4 / 3; object-fit: cover; border: 1px solid var(--line); display: block; }
.report .photos figcaption { font-size: 12px; margin-top: 4px; }
.report .firme { display: grid; grid-template-columns: 1fr 1fr; gap: 24px; margin-top: 28px; }
.report .firme div { border-top: 1px solid var(--ink); padding-top: 6px; min-height: 90px; }
.report .firme img { max-height: 80px; display: block; }
.report .wrap { white-space: pre-wrap; }
.report .table-scroll { overflow-x: auto; }
@media (max-width: 600px) { .report { padding: 16px; } .report .firme { grid-template-columns: 1fr; } }

@media print {
  @page { size: A4; margin: 14mm; }
  body { background: #fff; padding: 0; }
  .no-print, .topbar, .nav, .toolbar, .toast { display: none !important; }
  .container { max-width: none; padding: 0; }
  .report { border: 0; padding: 0; font-size: 11pt; }
  .report h2, .report tr, .report .photos figure, .report .firme { break-inside: avoid; }
  .report th { background: #eee !important; -webkit-print-color-adjust: exact; print-color-adjust: exact; }
}

/* Dettatura vocale */
.btn.mic { margin-top: 6px; font-size: .85rem; padding: 6px 12px; }
.btn.mic.rec { background: #c62828; color: #fff; border-color: #c62828; }
@media print { .btn.mic { display: none; } }

/* Logo e carta intestata */
.brand img.logo { object-fit: contain; background: #fff; border-radius: 4px; }
.logo-prev { max-height: 80px; max-width: 240px; display: block; margin-top: 8px; }
.report .intest { display: flex; align-items: center; gap: 12px; flex: 1 1 260px; }
.report .intest .logo { max-height: 64px; max-width: 160px; object-fit: contain; }
.report table.sheet, .report .sheet > * > tr > td { border: 0; padding: 0; margin: 0; }
.report .piede { margin-top: 24px; padding-top: 6px; border-top: 1px solid var(--line); font-size: 11px; color: var(--muted); text-align: center; }
.report .piede-spazio { display: none; }
@media print {
  .report .sheet > tbody > tr { break-inside: auto; }
  .report .piede-spazio { display: block; height: 22mm; }
  .report .piede { position: fixed; bottom: 0; left: 0; right: 0; margin: 0; font-size: 8pt; background: #fff; }
}
