:root {
  --bg: #f6f5f1;
  --surface: #fcfcfb;
  --surface-2: #f0efea;
  --line: #dddbd3;
  --line-strong: #c4c1b6;
  --text: #17160f;
  --text-2: #52514e;
  --text-3: #75736b;
  --accent: #7a5c12;
  --accent-strong: #5e460c;
  --accent-soft: #f4ead1;
  --series: #2a78d6;
  --good: #0b7a0b;
  --good-soft: #e3f3e1;
  --warn: #8a5a00;
  --warn-soft: #fdf0d2;
  --bad: #b3261e;
  --bad-soft: #fbe3e1;
  --info: #1c5cab;
  --info-soft: #e1ecfb;
  --radius: 10px;
  --shadow: 0 1px 2px rgba(20, 18, 10, .06), 0 2px 8px rgba(20, 18, 10, .04);
  --mono: ui-monospace, SFMono-Regular, Menlo, Consolas, monospace;
  color-scheme: light;
}
@media (prefers-color-scheme: dark) {
  :root {
    --bg: #141413;
    --surface: #1c1c1a;
    --surface-2: #262623;
    --line: #34332f;
    --line-strong: #4a4943;
    --text: #f0efec;
    --text-2: #c3c2b7;
    --text-3: #9b998f;
    --accent: #e0b85a;
    --accent-strong: #f0cc77;
    --accent-soft: #3a2f14;
    --series: #3987e5;
    --good: #5fcf5f;
    --good-soft: #17301a;
    --warn: #f0b84a;
    --warn-soft: #3a2c10;
    --bad: #ff8a80;
    --bad-soft: #3d1715;
    --info: #86b6ef;
    --info-soft: #172a44;
    color-scheme: dark;
  }
}

* { box-sizing: border-box; }
html { -webkit-text-size-adjust: 100%; }
body {
  margin: 0; background: var(--bg); color: var(--text);
  font: 15px/1.45 -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif;
  font-variant-numeric: tabular-nums;
}
h1, h2, h3, h4 { line-height: 1.2; margin: 0 0 .6em; }
h1 { font-size: 1.25rem; }
h2 { font-size: 1.2rem; }
h3 { font-size: 1.02rem; }
p { margin: 0 0 .7em; }
a { color: var(--accent); }
small, .muted { color: var(--text-2); }
.tiny { font-size: .82rem; color: var(--text-3); }
code, .mono { font-family: var(--mono); font-size: .9em; }
[hidden] { display: none !important; }

/* ---------------------------------------------------------------- layout */
.topbar {
  position: sticky; top: 0; z-index: 20; background: var(--surface); border-bottom: 1px solid var(--line);
  padding: .55rem max(1rem, env(safe-area-inset-left)) .4rem;
}
.topbar-row { display: flex; align-items: center; gap: .75rem; max-width: 1400px; margin: 0 auto; }
.brand { display: flex; align-items: baseline; gap: .5rem; flex-wrap: wrap; }
.brand h1 { margin: 0; }
.version { font-size: .78rem; color: var(--text-3); }
.spacer { flex: 1; }
.user-chip { font-size: .85rem; color: var(--text-2); text-align: right; }
.sync { font-size: .78rem; }
.sync.ok { color: var(--good); }
.sync.err { color: var(--bad); }
nav.tabs { display: flex; gap: .25rem; overflow-x: auto; max-width: 1400px; margin: .45rem auto 0; scrollbar-width: none; }
nav.tabs::-webkit-scrollbar { display: none; }
nav.tabs button {
  border: 0; background: none; color: var(--text-2); padding: .5rem .8rem; border-radius: 8px 8px 0 0;
  font: inherit; font-weight: 600; white-space: nowrap; cursor: pointer; border-bottom: 3px solid transparent;
}
nav.tabs button[aria-selected="true"] { color: var(--accent); border-bottom-color: var(--accent); }
main { max-width: 1400px; margin: 0 auto; padding: 1rem max(1rem, env(safe-area-inset-left)) 4rem; }
.banner {
  background: var(--warn-soft); color: var(--text); border: 1px solid color-mix(in srgb, var(--warn) 35%, transparent);
  border-radius: var(--radius); padding: .6rem .85rem; margin-bottom: 1rem; font-size: .9rem;
}
.card { background: var(--surface); border: 1px solid var(--line); border-radius: var(--radius); box-shadow: var(--shadow); padding: 1rem 1.1rem; margin-bottom: 1rem; }
.card > h2:first-child, .card > h3:first-child { margin-top: 0; }
.grid { display: grid; gap: .8rem; }
.g2 { grid-template-columns: repeat(2, minmax(0, 1fr)); }
.g3 { grid-template-columns: repeat(3, minmax(0, 1fr)); }
.g5 { grid-template-columns: repeat(5, minmax(0, 1fr)); }
.cols { display: grid; grid-template-columns: minmax(0, 1.15fr) minmax(0, 1fr); gap: 1rem; align-items: start; }
@media (max-width: 900px) { .cols { grid-template-columns: 1fr; } .g5 { grid-template-columns: repeat(3, minmax(0, 1fr)); } }
@media (max-width: 560px) { .g2, .g3 { grid-template-columns: 1fr; } .g5 { grid-template-columns: repeat(2, minmax(0, 1fr)); } }
.row { display: flex; gap: .5rem; flex-wrap: wrap; align-items: center; }
.row.end { justify-content: flex-end; }
.stack > * + * { margin-top: .6rem; }

/* ---------------------------------------------------------------- form */
label.f { display: flex; flex-direction: column; gap: .25rem; font-size: .85rem; font-weight: 600; color: var(--text-2); }
label.f .hint { font-weight: 400; color: var(--text-3); font-size: .78rem; }
input, select, textarea {
  font: inherit; color: var(--text); background: var(--surface); border: 1px solid var(--line-strong);
  border-radius: 8px; padding: .55rem .65rem; min-height: 42px; width: 100%;
}
input[type="checkbox"], input[type="radio"] { width: auto; min-height: 0; accent-color: var(--accent); transform: scale(1.15); }
input:focus-visible, select:focus-visible, textarea:focus-visible, button:focus-visible { outline: 2px solid var(--accent); outline-offset: 1px; }
input.num { text-align: right; font-variant-numeric: tabular-nums; }
input[aria-invalid="true"] { border-color: var(--bad); background: var(--bad-soft); }
.check { display: inline-flex; gap: .45rem; align-items: center; font-size: .9rem; cursor: pointer; }

button.btn, a.btn {
  font: inherit; font-weight: 600; border-radius: 8px; padding: .55rem .95rem; min-height: 42px; cursor: pointer;
  border: 1px solid var(--line-strong); background: var(--surface); color: var(--text); text-decoration: none; display: inline-flex; align-items: center; gap: .35rem;
}
button.btn:hover { background: var(--surface-2); }
button.primary { background: var(--accent); border-color: var(--accent); color: #fff; }
@media (prefers-color-scheme: dark) { button.primary { color: #1a1408; } }
button.primary:hover { background: var(--accent-strong); }
button.danger { color: var(--bad); border-color: color-mix(in srgb, var(--bad) 50%, transparent); }
button.small { min-height: 32px; padding: .25rem .6rem; font-size: .85rem; }
button:disabled { opacity: .5; cursor: not-allowed; }
button.link { background: none; border: 0; padding: 0; color: var(--accent); font: inherit; cursor: pointer; text-decoration: underline; }

/* ---------------------------------------------------------------- badge & stati */
.badge { display: inline-flex; align-items: center; gap: .25rem; font-size: .75rem; font-weight: 700; padding: .1rem .45rem; border-radius: 999px; white-space: nowrap; border: 1px solid transparent; }
.b-auto { background: var(--good-soft); color: var(--good); }
.b-stor { background: var(--info-soft); color: var(--info); }
.b-doc { background: var(--surface-2); color: var(--text-2); border-color: var(--line); }
.b-man { background: var(--accent-soft); color: var(--accent); }
.b-prov { background: var(--warn-soft); color: var(--warn); }
.b-miss { background: var(--bad-soft); color: var(--bad); }
.b-wait { background: var(--surface-2); color: var(--text-2); border-color: var(--line); }
.b-new { background: var(--good-soft); color: var(--good); }
.b-upd { background: var(--info-soft); color: var(--info); }
.b-same { background: var(--surface-2); color: var(--text-2); }
.b-excl { background: var(--surface-2); color: var(--text-3); text-decoration: line-through; }
.b-err { background: var(--bad-soft); color: var(--bad); }
.msg { border-radius: 8px; padding: .55rem .75rem; font-size: .9rem; }
.msg.ok { background: var(--good-soft); color: var(--good); }
.msg.err { background: var(--bad-soft); color: var(--bad); }
.msg.warn { background: var(--warn-soft); color: var(--warn); }
.msg.info { background: var(--info-soft); color: var(--info); }
ul.problemi { margin: 0; padding-left: 1.1rem; font-size: .8rem; }
ul.problemi li.errore { color: var(--bad); }
ul.problemi li.avviso { color: var(--warn); }

/* ---------------------------------------------------------------- KPI */
.kpis { display: grid; grid-template-columns: repeat(auto-fit, minmax(150px, 1fr)); gap: .6rem; margin-bottom: 1rem; }
.kpi { background: var(--surface); border: 1px solid var(--line); border-radius: var(--radius); padding: .7rem .8rem; }
.kpi .k { font-size: .78rem; color: var(--text-2); font-weight: 600; }
.kpi .v { font-size: 1.35rem; font-weight: 700; margin-top: .15rem; }
.kpi .s { font-size: .76rem; color: var(--text-3); margin-top: .1rem; }

.calc-preview dl { display: grid; grid-template-columns: auto auto; gap: .3rem 1rem; margin: 0; font-size: .92rem; }
.calc-preview dt { color: var(--text-2); }
.calc-preview dd { margin: 0; text-align: right; font-weight: 600; }
.calc-preview dd.big { font-size: 1.15rem; }

/* ---------------------------------------------------------------- tabelle */
.table-wrap { overflow-x: auto; border: 1px solid var(--line); border-radius: var(--radius); background: var(--surface); }
table { border-collapse: collapse; width: 100%; font-size: .86rem; }
th, td { padding: .45rem .55rem; border-bottom: 1px solid var(--line); text-align: left; vertical-align: top; }
th { background: var(--surface-2); font-weight: 700; color: var(--text-2); position: sticky; top: 0; z-index: 1; white-space: nowrap; font-size: .8rem; }
td.n, th.n { text-align: right; white-space: nowrap; }
tr.mese td { background: var(--accent-soft); font-weight: 700; }
tr.tot td { background: var(--surface-2); font-weight: 700; }
tr.clic { cursor: pointer; }
tr.clic:hover td { background: color-mix(in srgb, var(--accent-soft) 45%, transparent); }
tr.desel td { opacity: .6; }
.nowrap { white-space: nowrap; }
.filtri { display: flex; gap: .5rem; flex-wrap: wrap; align-items: flex-end; margin-bottom: .8rem; }
.filtri label.f { min-width: 130px; flex: 1 1 130px; max-width: 220px; }
.chips { display: flex; gap: .35rem; flex-wrap: wrap; margin: .5rem 0; }
.chips button { border: 1px solid var(--line-strong); background: var(--surface); border-radius: 999px; padding: .25rem .7rem; font: inherit; font-size: .84rem; cursor: pointer; color: var(--text); }
.chips button[aria-pressed="true"] { background: var(--accent); color: #fff; border-color: var(--accent); }

/* schede atti su mobile */
@media (max-width: 720px) {
  table.responsive thead { display: none; }
  table.responsive, table.responsive tbody, table.responsive tr, table.responsive td { display: block; width: 100%; }
  table.responsive tr { border-bottom: 1px solid var(--line-strong); padding: .4rem .2rem; }
  table.responsive td { border: 0; padding: .15rem .4rem; display: flex; justify-content: space-between; gap: 1rem; text-align: right; }
  table.responsive td::before { content: attr(data-l); color: var(--text-2); font-weight: 600; text-align: left; }
  table.responsive td.vuota { display: none; }
  table.responsive td.full { display: block; text-align: left; }
  table.responsive td.full::before { display: none; }
  table.responsive tr.mese td, table.responsive tr.tot td { display: block; text-align: left; }
  table.responsive tr.mese td::before, table.responsive tr.tot td::before { display: none; }
}

/* ---------------------------------------------------------------- grafico */
.chart { width: 100%; height: auto; display: block; max-width: 1100px; }
@media (max-width: 720px) { .chart text { font-size: 22px; } }
.chart .bar { fill: var(--series); }
.chart .bar:hover { opacity: .8; }
.chart .grid-line { stroke: var(--line); stroke-width: 1; }
.chart text { fill: var(--text-2); font-size: 11px; }
.chart .val { fill: var(--text); font-weight: 600; }
.tooltip { position: fixed; pointer-events: none; z-index: 60; background: var(--text); color: var(--bg); padding: .35rem .55rem; border-radius: 6px; font-size: .8rem; max-width: 260px; }

/* ---------------------------------------------------------------- dialog & toast */
dialog { border: 1px solid var(--line); border-radius: 14px; padding: 0; max-width: min(720px, 94vw); width: 100%; background: var(--surface); color: var(--text); box-shadow: 0 20px 60px rgba(0, 0, 0, .3); }
dialog::backdrop { background: rgba(10, 10, 5, .45); }
dialog .d-body { padding: 1.1rem 1.2rem; max-height: 75vh; overflow: auto; }
dialog .d-foot { padding: .75rem 1.2rem; border-top: 1px solid var(--line); display: flex; gap: .5rem; justify-content: flex-end; flex-wrap: wrap; }
#toasts { position: fixed; bottom: max(1rem, env(safe-area-inset-bottom)); left: 50%; transform: translateX(-50%); z-index: 100; display: flex; flex-direction: column; gap: .4rem; width: min(560px, 92vw); }
.toast { padding: .65rem .9rem; border-radius: 10px; box-shadow: var(--shadow); font-size: .92rem; font-weight: 600; }
.toast.ok { background: var(--good); color: #fff; }
.toast.err { background: var(--bad); color: #fff; }
.toast.info { background: var(--text); color: var(--bg); }

/* ---------------------------------------------------------------- accesso */
.auth { max-width: 440px; margin: 6vh auto; }
.auth .card { padding: 1.4rem; }
.steps { display: flex; gap: .4rem; margin-bottom: .8rem; flex-wrap: wrap; }
.steps span { font-size: .8rem; font-weight: 700; padding: .25rem .6rem; border-radius: 999px; background: var(--surface-2); color: var(--text-3); }
.steps span.on { background: var(--accent); color: #fff; }
details summary { cursor: pointer; font-weight: 600; }
.loading { padding: 3rem; text-align: center; color: var(--text-2); }
.progress { height: 6px; background: var(--surface-2); border-radius: 3px; overflow: hidden; }
.progress > div { height: 100%; background: var(--accent); width: 0; transition: width .2s; }

@media (max-width: 560px) {
  .brand h1 { font-size: 1.05rem; }
  .version { display: none; }
  .user-chip { font-size: .75rem; max-width: 45vw; }
  .sync { font-size: .7rem; }
  .banner { font-size: .8rem; padding: .45rem .6rem; }
  .toast { font-size: .85rem; padding: .5rem .7rem; }
}

.avviso-globale { margin-bottom: .6rem; display: flex; gap: .6rem; align-items: center; flex-wrap: wrap; justify-content: space-between; }
#suggerisci-atto { text-align: left; font-weight: 600; margin-top: .15rem; }
@media (max-width: 720px) {
  .azioni-form { position: sticky; bottom: 0; background: var(--surface); padding: .6rem 0 max(.6rem, env(safe-area-inset-bottom)); margin-bottom: -1rem; border-top: 1px solid var(--line); z-index: 5; }
  .azioni-form .btn { flex: 1; justify-content: center; }
}

/* finestrelle mese → negozio → atti */
details.gruppo { border: 1px solid var(--line); border-radius: 12px; margin: .5rem 0; background: var(--surface); }
details.gruppo > summary { list-style: none; display: flex; flex-wrap: wrap; align-items: baseline; gap: .3rem 1rem; padding: .75rem 1rem; min-height: 44px; }
details.gruppo > summary::-webkit-details-marker { display: none; }
details.gruppo > summary::before { content: '▸'; color: var(--text-3); width: .8em; }
details.gruppo[open] > summary::before { content: '▾'; }
details.gruppo .g-tit { font-weight: 700; }
details.gruppo .g-dati { font-weight: 400; color: var(--text-2); font-size: .92rem; }
details.gruppo.mese > .g-corpo { padding: 0 .6rem .6rem; }
details.gruppo.negozio { background: var(--surface-2); }
details.gruppo.negozio > .table-wrap { margin: 0 .4rem .5rem; }
.g-totale { display: flex; flex-wrap: wrap; gap: .3rem 1rem; align-items: baseline; padding: .6rem 1rem; }
.g-totale .g-tit { font-weight: 700; }
.g-totale .g-dati { color: var(--text-2); font-size: .92rem; }
.g-generale { border: 1px dashed var(--line); border-radius: 12px; }
details.gruppo-card > summary { list-style: none; }
details.gruppo-card > summary::-webkit-details-marker { display: none; }
details.gruppo-card > summary::before { content: '▸ '; }
details.gruppo-card[open] > summary::before { content: '▾ '; }
