:root {
  --bg: #f5f6f8;
  --panel: #ffffff;
  --text: #1d2430;
  --muted: #637083;
  --line: #dde1e7;
  --accent: #1f4e79;
  --accent-soft: #e6eef7;
  --add: #1e7a46;
  --add-bg: #e3f4ea;
  --del: #b3261e;
  --del-bg: #fbe7e5;
  --mod: #9a6300;
  --mod-bg: #fff3d6;
  --same: #6b7480;
  --same-bg: #eef0f3;
  --radius: 8px;
  color-scheme: light dark;
}
@media (prefers-color-scheme: dark) {
  :root {
    --bg: #14181e;
    --panel: #1c222a;
    --text: #e4e8ee;
    --muted: #98a3b3;
    --line: #2f3742;
    --accent: #7fb2e5;
    --accent-soft: #22303f;
    --add: #6fd39b;
    --add-bg: #173326;
    --del: #ff8a80;
    --del-bg: #3a1c1a;
    --mod: #f2c063;
    --mod-bg: #3a2e14;
    --same: #9aa3ae;
    --same-bg: #242b34;
  }
}

* { box-sizing: border-box; }
html { -webkit-text-size-adjust: 100%; }
body {
  margin: 0;
  background: var(--bg);
  color: var(--text);
  font: 14px/1.45 system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
}
.wrap { max-width: 1400px; margin: 0 auto; padding: 0 16px; }
button, input { font: inherit; color: inherit; }

.top { background: var(--panel); border-bottom: 1px solid var(--line); padding: 18px 0 14px; margin-bottom: 20px; }
.top h1 { margin: 0; font-size: 20px; color: var(--accent); }
.sub { margin: 4px 0 0; color: var(--muted); }

/* ---- caricamento ---- */
.upload {
  display: grid;
  grid-template-columns: 1fr auto 1fr;
  gap: 12px;
  align-items: stretch;
  margin-bottom: 24px;
}
.drop {
  display: flex; flex-direction: column; gap: 4px;
  padding: 18px; min-height: 110px;
  background: var(--panel);
  border: 2px dashed var(--line);
  border-radius: var(--radius);
  cursor: pointer;
  position: relative;
  transition: border-color .15s, background .15s;
}
.drop:hover, .drop.over { border-color: var(--accent); background: var(--accent-soft); }
.drop.filled { border-style: solid; }
.drop-title { font-weight: 600; font-size: 15px; }
.drop-file { color: var(--muted); word-break: break-all; }
.drop.filled .drop-file { color: var(--text); }
.tag {
  position: absolute; top: 12px; right: 12px;
  width: 26px; height: 26px; border-radius: 50%;
  display: grid; place-items: center;
  font-weight: 700; font-size: 13px; color: #fff;
}
.tag-a { background: #7a8597; }
.tag-b { background: var(--accent); }
@media (prefers-color-scheme: dark) { .tag-b { color: #14181e; } }
.swap {
  align-self: center;
  width: 40px; height: 40px; border-radius: 50%;
  border: 1px solid var(--line); background: var(--panel); cursor: pointer; font-size: 18px;
}
.swap:hover { border-color: var(--accent); }
.actions { grid-column: 1 / -1; display: flex; align-items: center; gap: 14px; flex-wrap: wrap; }
.primary {
  background: var(--accent); color: #fff; border: 0; border-radius: var(--radius);
  padding: 10px 26px; font-weight: 600; cursor: pointer;
}
@media (prefers-color-scheme: dark) { .primary { color: #14181e; } }
.primary:disabled { opacity: .45; cursor: default; }
.status { color: var(--muted); }
.status.error { color: var(--del); font-weight: 600; }

/* ---- intestazione risultati ---- */
.docs { display: grid; grid-template-columns: 1fr 1fr; gap: 12px; margin-bottom: 12px; }
.doc { background: var(--panel); border: 1px solid var(--line); border-radius: var(--radius); padding: 12px 14px; }
.doc h3 { margin: 0 0 4px; font-size: 14px; display: flex; gap: 8px; align-items: center; }
.doc h3 .tag { position: static; width: 22px; height: 22px; font-size: 12px; flex: none; }
.doc p { margin: 0; color: var(--muted); font-size: 13px; }
.doc .ogg { color: var(--text); }

.warn {
  background: var(--mod-bg); color: var(--text); border-left: 4px solid var(--mod);
  padding: 8px 12px; border-radius: 4px; margin-bottom: 12px; font-size: 13px;
}
.warn summary { cursor: pointer; font-weight: 600; }
.warn ul { margin: 6px 0 0; padding-left: 18px; }

.cards { display: grid; grid-template-columns: repeat(auto-fit, minmax(150px, 1fr)); gap: 10px; margin-bottom: 14px; }
.card {
  background: var(--panel); border: 1px solid var(--line); border-radius: var(--radius);
  padding: 10px 14px; text-align: left;
}
.card .lbl { color: var(--muted); font-size: 12px; text-transform: uppercase; letter-spacing: .03em; }
.card .val { font-size: 20px; font-weight: 650; font-variant-numeric: tabular-nums; margin-top: 2px; }
.card .val small { font-size: 13px; font-weight: 500; white-space: nowrap; }
button.card { cursor: pointer; font: inherit; color: inherit; }
button.card:hover { border-color: var(--accent); }
button.card.on { outline: 2px solid var(--accent); outline-offset: -1px; }
.card.k-mod .val { color: var(--mod); }
.card.k-add .val { color: var(--add); }
.card.k-del .val { color: var(--del); }
.card.k-same .val { color: var(--same); }
.pos { color: var(--del); }   /* il computo costa di più */
.neg { color: var(--add); }   /* il computo costa di meno */

.cats { background: var(--panel); border: 1px solid var(--line); border-radius: var(--radius); padding: 8px 14px; margin-bottom: 14px; }
.cats summary { cursor: pointer; font-weight: 600; }
.cats table { width: 100%; border-collapse: collapse; margin-top: 8px; }
.cats th, .cats td { padding: 5px 8px; border-top: 1px solid var(--line); text-align: right; font-variant-numeric: tabular-nums; }
.cats th:first-child, .cats td:first-child { text-align: left; }
.cats th { color: var(--muted); font-weight: 600; font-size: 12px; }

/* ---- barra filtri ---- */
.toolbar { display: flex; gap: 8px; flex-wrap: wrap; align-items: center; margin-bottom: 10px; }
.chips { display: flex; gap: 6px; flex-wrap: wrap; }
.chip, .toolbar > button {
  border: 1px solid var(--line); background: var(--panel); border-radius: 999px;
  padding: 5px 12px; cursor: pointer; font-size: 13px;
}
.chip.on { background: var(--accent); border-color: var(--accent); color: #fff; }
@media (prefers-color-scheme: dark) { .chip.on { color: #14181e; } }
.chip .n { opacity: .7; margin-left: 4px; }
/* ricerca: barra larga sopra i filtri */
.search { position: relative; margin-bottom: 10px; }
.search svg {
  position: absolute; left: 14px; top: 50%; transform: translateY(-50%);
  width: 18px; height: 18px; fill: none; stroke: var(--muted); stroke-width: 2; stroke-linecap: round;
  pointer-events: none;
}
.search input {
  width: 100%; padding: 10px 14px 10px 42px;
  border: 1px solid var(--line); border-radius: var(--radius); background: var(--panel);
  font-size: 15px;
}
.search input:focus { outline: 2px solid var(--accent); outline-offset: -1px; border-color: var(--accent); }
.count { margin-left: auto; color: var(--muted); font-size: 13px; white-space: nowrap; }

/* filtri rapidi (pieni quando attivi) | stati combinabili (con casella di spunta) */
.chip-sep { width: 1px; align-self: stretch; background: var(--line); margin: 2px 4px; }
.chip.toggle { display: inline-flex; align-items: center; gap: 6px; }
.chip.toggle.on { background: var(--panel); color: var(--text); }
.chip .check {
  width: 14px; height: 14px; border-radius: 3px; border: 1.5px solid var(--muted);
  display: inline-grid; place-items: center; flex: none;
}
.chip.toggle.on .check::after { content: "✓"; font-size: 11px; line-height: 1; color: #fff; font-weight: 700; }
.chip.c-modificata.on { border-color: var(--mod); } .chip.c-modificata.on .check { background: var(--mod); border-color: var(--mod); }
.chip.c-aggiunta.on { border-color: var(--add); }   .chip.c-aggiunta.on .check { background: var(--add); border-color: var(--add); }
.chip.c-rimossa.on { border-color: var(--del); }    .chip.c-rimossa.on .check { background: var(--del); border-color: var(--del); }
.chip.c-non_iniziata.on { border-color: var(--del); } .chip.c-non_iniziata.on .check { background: var(--del); border-color: var(--del); }
.chip.c-parziale.on { border-color: var(--mod); }      .chip.c-parziale.on .check { background: var(--mod); border-color: var(--mod); }
.chip.c-completata.on { border-color: var(--add); }    .chip.c-completata.on .check { background: var(--add); border-color: var(--add); }
.chip.c-oltre.on { border-color: #7b3fb5; }            .chip.c-oltre.on .check { background: #7b3fb5; border-color: #7b3fb5; }
.chip.c-invariata.on { border-color: var(--same); } .chip.c-invariata.on .check { background: var(--same); border-color: var(--same); }
@media (prefers-color-scheme: dark) { .chip.toggle.on .check::after { color: #14181e; } }

/* ---- tabella ---- */
/* niente overflow sul contenitore: altrimenti l'intestazione resterebbe ferma dentro di lui
   e non in cima alla finestra */
.table-wrap { background: var(--panel); border: 1px solid var(--line); border-radius: var(--radius); margin-bottom: 40px; }
table.diff { width: 100%; border-collapse: collapse; font-size: 13px; }
.diff th {
  position: sticky; top: 0; background: var(--panel); z-index: 2;
  text-align: left; padding: 8px;
  box-shadow: inset 0 -2px var(--line);   /* il bordo normale sparisce con sticky + border-collapse */
  color: var(--muted); font-size: 12px; font-weight: 600; white-space: nowrap;
}
.diff td { padding: 7px 8px; border-bottom: 1px solid var(--line); vertical-align: top; }
.diff .num { text-align: right; font-variant-numeric: tabular-nums; white-space: nowrap; }
.diff tr.item { cursor: pointer; }
.diff tr.item:hover td { background: var(--accent-soft); }
.diff tr.s-aggiunta td:first-child { box-shadow: inset 4px 0 var(--add); }
.diff tr.s-rimossa td:first-child { box-shadow: inset 4px 0 var(--del); }
.diff tr.s-modificata td:first-child { box-shadow: inset 4px 0 var(--mod); }
.diff tr.s-rimossa .txt { text-decoration: line-through; text-decoration-color: var(--del); }
.code { font-family: ui-monospace, "SF Mono", Menlo, Consolas, monospace; font-size: 12px; white-space: nowrap; }
.desc { max-width: 520px; }
.desc .short { display: -webkit-box; -webkit-line-clamp: 2; -webkit-box-orient: vertical; overflow: hidden; }

.badge { display: inline-block; padding: 1px 8px; border-radius: 999px; font-size: 11.5px; font-weight: 600; white-space: nowrap; }
.b-aggiunta { background: var(--add-bg); color: var(--add); }
.b-rimossa { background: var(--del-bg); color: var(--del); }
.b-modificata { background: var(--mod-bg); color: var(--mod); }
.b-invariata { background: var(--same-bg); color: var(--same); }

/* valore cambiato: vecchio barrato sopra, nuovo sotto */
.chg .old { display: block; color: var(--del); text-decoration: line-through; font-size: 12px; }
.chg .new { display: block; color: var(--add); font-weight: 600; }

/* dettaglio */
tr.detail > td { background: var(--bg); padding: 12px 16px 16px; }
.det h4 { margin: 10px 0 6px; font-size: 12px; text-transform: uppercase; letter-spacing: .04em; color: var(--muted); }
.det h4:first-child { margin-top: 0; }
.det .fulldesc { line-height: 1.6; max-width: 1000px; }
ins { background: var(--add-bg); color: var(--add); text-decoration: none; border-radius: 3px; padding: 0 2px; }
del { background: var(--del-bg); color: var(--del); border-radius: 3px; padding: 0 2px; }
.changed-list { display: flex; gap: 6px; flex-wrap: wrap; }
.changed-list span { background: var(--mod-bg); color: var(--mod); border-radius: 4px; padding: 1px 7px; font-size: 12px; }
table.mis { border-collapse: collapse; font-size: 12.5px; min-width: 640px; background: var(--panel); border: 1px solid var(--line); }
table.mis th, table.mis td { padding: 4px 8px; border-bottom: 1px solid var(--line); }
table.mis th { color: var(--muted); font-weight: 600; text-align: right; font-size: 11.5px; }
table.mis th:nth-child(2), table.mis td:nth-child(2) { text-align: left; }
table.mis td { text-align: right; font-variant-numeric: tabular-nums; }
table.mis tr.m-ins td { background: var(--add-bg); }
table.mis tr.m-del td { background: var(--del-bg); text-decoration: line-through; }
table.mis tr.m-mod td { background: var(--mod-bg); }
table.mis tr.m-eq td { color: var(--muted); }
table.mis td.op { text-align: center; font-weight: 700; width: 24px; }
table.mis tr.m-ins td.op { color: var(--add); }
table.mis tr.m-del td.op { color: var(--del); }
table.mis tr.m-mod td.op { color: var(--mod); }
table.mis tr.group td { font-weight: 600; color: var(--text); }
table.mis.cost { min-width: 560px; }
table.mis.cost th:first-child { text-align: left; color: var(--text); }
table.mis.cost td:nth-child(2) { text-align: right; }
table.mis.cost tbody th { font-size: 12.5px; }
table.mis.cost tr.c-chg td, table.mis.cost tr.c-chg th { background: var(--mod-bg); font-weight: 600; }
table.mis.cost td.pos { color: var(--del); }
table.mis.cost td.neg { color: var(--add); }
.cost-split { display: flex; gap: 18px; flex-wrap: wrap; margin-top: 6px; font-size: 12.5px; color: var(--muted); }
.cost-split b { color: var(--text); }
.cost-split b.pos { color: var(--del); }
.cost-split b.neg { color: var(--add); }
.empty { padding: 20px; text-align: center; color: var(--muted); }

/* schermi stretti: la tabella deve scorrere in orizzontale, quindi scorre in un riquadro
   alto quanto la finestra e l'intestazione resta ferma in cima al riquadro */
@media (max-width: 1100px) {
  .table-wrap { overflow: auto; max-height: calc(100vh - 24px); }
}

/* ---- navigazione tra le pagine ---- */
.nav { display: flex; gap: 4px; margin-bottom: 10px; flex-wrap: wrap; }
.nav a {
  padding: 4px 12px; border-radius: 999px; text-decoration: none; color: var(--muted);
  border: 1px solid transparent; font-size: 13px;
}
.nav a:hover { color: var(--text); border-color: var(--line); }
.nav a.on { background: var(--accent-soft); color: var(--accent); font-weight: 600; }

/* ---- pagina SAL ---- */
.steps { display: grid; grid-template-columns: 1fr 1fr; gap: 14px; margin-bottom: 10px; align-items: start; }
.step { background: var(--panel); border: 1px solid var(--line); border-radius: var(--radius); padding: 14px; display: flex; flex-direction: column; gap: 10px; }
.step h2 { margin: 0; font-size: 15px; display: flex; align-items: center; gap: 8px; }
.step h2 .n {
  width: 22px; height: 22px; border-radius: 50%; background: var(--accent); color: #fff;
  display: grid; place-items: center; font-size: 12px;
}
@media (prefers-color-scheme: dark) { .step h2 .n { color: #14181e; } }
.step .doc { background: var(--bg); }
.drop.small { min-height: 0; padding: 14px; }
.drop.compact { padding: 8px 14px; }
.drop.compact .drop-title { font-size: 13px; font-weight: 500; }
.drop.compact .drop-file { display: none; }
.sal-list { list-style: none; margin: 0; padding: 0; display: flex; flex-direction: column; gap: 6px; }
.sal-list:empty { display: none; }
.sal-item {
  display: grid; grid-template-columns: 110px 1fr auto; gap: 10px; align-items: center;
  background: var(--bg); border: 1px solid var(--line); border-radius: 6px; padding: 6px 8px;
}
.sal-name { width: 100%; padding: 4px 6px; border: 1px solid var(--line); border-radius: 4px; background: var(--panel); font-weight: 600; }
.sal-meta { display: flex; flex-direction: column; min-width: 0; font-size: 12.5px; color: var(--muted); }
.sal-file { color: var(--text); overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.sal-btns { display: flex; gap: 4px; }
.sal-btns button {
  width: 28px; height: 28px; border: 1px solid var(--line); border-radius: 4px; background: var(--panel); cursor: pointer;
}
.sal-btns button:disabled { opacity: .35; cursor: default; }
.sal-btns button:not(:disabled):hover { border-color: var(--accent); }
.warn-text { color: var(--mod); font-weight: 600; }
.check-row { display: flex; gap: 8px; align-items: flex-start; font-size: 13px; color: var(--muted); cursor: pointer; }
.check-row b { color: var(--text); }
.check-row input { margin-top: 3px; }
.link-btn { margin-left: auto; background: none; border: 0; color: var(--muted); text-decoration: underline; cursor: pointer; font-size: 13px; }
.link-btn:hover { color: var(--del); }
#result { margin-top: 14px; }

.card.k-over .val { color: #7b3fb5; }
.progress { margin: -4px 0 16px; }
.bar { height: 10px; background: var(--same-bg); border-radius: 999px; overflow: hidden; }
.bar span { display: block; height: 100%; background: var(--add); border-radius: 999px; }
.bar-lbl { font-size: 12.5px; color: var(--muted); margin-top: 4px; }

.tabs { display: flex; gap: 2px; border-bottom: 2px solid var(--line); margin-bottom: 12px; flex-wrap: wrap; }
.tab {
  border: 0; background: none; padding: 8px 14px; cursor: pointer; font-weight: 600; color: var(--muted);
  border-bottom: 2px solid transparent; margin-bottom: -2px;
}
.tab:hover { color: var(--text); }
.tab.on { color: var(--accent); border-bottom-color: var(--accent); }
.tab .n { margin-left: 6px; font-weight: 500; background: var(--same-bg); border-radius: 999px; padding: 0 7px; font-size: 12px; }

table.sal td.muted { color: var(--muted); }
table.sal td.strong { font-weight: 650; }
table.sal tr.s-non_iniziata td:first-child { box-shadow: inset 4px 0 var(--del); }
table.sal tr.s-parziale td:first-child { box-shadow: inset 4px 0 var(--mod); }
table.sal tr.s-completata td:first-child { box-shadow: inset 4px 0 var(--add); }
table.sal tr.s-oltre td:first-child { box-shadow: inset 4px 0 #7b3fb5; }
table.sal tfoot td { font-weight: 700; border-top: 2px solid var(--line); border-bottom: 0; padding-top: 10px; }
.note { display: block; margin-top: 3px; font-size: 12px; color: var(--mod); }
.sb-non_iniziata { background: var(--del-bg); color: var(--del); }
.sb-parziale { background: var(--mod-bg); color: var(--mod); }
.sb-completata { background: var(--add-bg); color: var(--add); }
.sb-oltre { background: #efe4fa; color: #7b3fb5; }
@media (prefers-color-scheme: dark) { .sb-oltre { background: #2e2140; color: #c9a3f0; } .card.k-over .val { color: #c9a3f0; } }
.mini { display: inline-block; width: 36px; height: 6px; background: var(--same-bg); border-radius: 3px; margin-right: 6px; vertical-align: middle; overflow: hidden; }
.mini span { display: block; height: 100%; background: var(--add); }
.print-head { display: none; }
.print-legend { display: flex; gap: 6px; flex-wrap: wrap; margin-top: 6px; }

@media (max-width: 900px) {
  .steps { grid-template-columns: 1fr; }
}

@media (max-width: 720px) {
  .sal-item { grid-template-columns: 1fr auto; }
  .sal-meta { grid-column: 1 / -1; grid-row: 2; }
  .upload { grid-template-columns: 1fr; }
  .swap { justify-self: center; transform: rotate(90deg); }
  .docs { grid-template-columns: 1fr; }
  .desc { min-width: 220px; }
}

@media print {
  .upload, .toolbar, .search, .top, .steps, .actions, .tabs, .cards, .progress { display: none !important; }
  .print-head { display: block; margin-bottom: 12px; }
  .print-head h2 { margin: 0 0 6px; font-size: 16px; }
  .print-head p { margin: 2px 0; }
  @page { size: A4 landscape; margin: 10mm; }
  .desc { min-width: 300px; width: 40%; }
  .desc .short { -webkit-line-clamp: 3; }
  .diff th, .diff td { padding: 4px 6px; }
  tfoot { display: table-row-group; }   /* il totale una volta sola, in fondo */
  /* stampa e PDF: sempre i colori chiari, anche se il sistema è in tema scuro */
  :root {
    --bg: #ffffff; --panel: #ffffff; --text: #1d2430; --muted: #5a6575; --line: #cfd5dd;
    --accent: #1f4e79; --accent-soft: #e6eef7;
    --add: #1e7a46; --add-bg: #dcf1e4; --del: #b3261e; --del-bg: #f9e0dd;
    --mod: #9a6300; --mod-bg: #fdeec8; --same: #6b7480; --same-bg: #eef0f3;
  }
  .sb-oltre { background: #efe4fa !important; color: #7b3fb5 !important; }
  /* senza questo il browser toglie sfondi e barre colorate (opzione "grafica di sfondo") */
  *, *::before, *::after { -webkit-print-color-adjust: exact; print-color-adjust: exact; }
  body { background: #fff; color: #000; font-size: 11px; }
  tr { break-inside: avoid; }
  .table-wrap { overflow: visible; border: 0; }
  .diff th { position: static; }
  .cats { display: block; }
  tr.item, tr.detail { break-inside: avoid; }
}

/* pagina confronto: tabella a tutta larghezza */
.wrap.wide { max-width: none; padding: 0 20px; }
.wide .desc { max-width: none; min-width: 360px; }

/* SAL: dettaglio voce */
table.sal-mis tr.mis-head td { background: var(--accent-soft); color: var(--text); font-weight: 650; border-top: 2px solid var(--line); }
table.sal-mis tr.mis-head td small { font-weight: 400; color: var(--muted); margin-left: 6px; }
table.sal-mis tr.mis-head.h-none td { background: var(--bg); color: var(--muted); }
table.sal-mis td:first-child, table.sal-mis th:first-child { text-align: left; }
table.sal-cost td:nth-child(2) { text-align: right; }
table.sal-cost tr.muted-row th, table.sal-cost tr.muted-row td { color: var(--muted); font-weight: 400; }
table.sal-cost tr.tot-row th, table.sal-cost tr.tot-row td { border-top: 2px solid var(--line); font-weight: 650; }
table.sal-cost tr.r-open td, table.sal-cost tr.r-open th { background: var(--del-bg); color: var(--del); }
table.sal-cost tr.r-done td, table.sal-cost tr.r-done th { background: var(--add-bg); color: var(--add); }
table.sal-cost tr.r-over td, table.sal-cost tr.r-over th { background: rgba(123, 63, 181, .13); color: #7b3fb5; }
table.sal-cost th small { font-weight: 400; color: var(--muted); }
/* le tabelline del dettaglio non ereditano l'intestazione fissa della tabella principale */
.diff table.mis th { position: static; box-shadow: none; background: transparent; white-space: nowrap; }
.diff table.mis thead th { background: var(--panel); }

/* barra di avanzamento divisa per SAL */
.bar.segs { display: flex; }
.bar.segs > span { display: block; height: 100%; border-radius: 0; background: none; }
.bar.segs > span + span { box-shadow: inset 2px 0 var(--panel); }
.bar-legend { display: flex; flex-wrap: wrap; gap: 4px 16px; margin-top: 6px; }
.bar-legend > span { white-space: nowrap; }
.bar-legend i { display: inline-block; width: 10px; height: 10px; border-radius: 3px; margin-right: 6px; vertical-align: -1px; }
.bar-legend small { color: var(--muted); }
.sal-c0 { background: #2563eb !important; } .sal-c1 { background: #0891b2 !important; }
.sal-c2 { background: #4f46e5 !important; } .sal-c3 { background: #db2777 !important; }
.sal-c4 { background: #475569 !important; } .sal-c5 { background: #38bdf8 !important; }
.sal-c6 { background: #92400e !important; } .sal-c7 { background: #0f766e !important; }
.bar.segs { height: 14px; }
