/* ---------- Token ---------- */
:root {
  color-scheme: light;
  --page: #f4f3ef;
  --surface: #fcfcfb;
  --surface-2: #efeee9;
  --ink: #141413;
  --ink-2: #45443f;
  --muted: #66645e;          /* ≥ 4.5:1 su surface */
  --grid: #e4e2da;
  --axis: #bdbcb1;
  --border: rgba(11, 11, 11, 0.10);
  --accent: #2a78d6;
  --accent-ink: #1c5cab;
  --accent-soft: #e3eefb;
  --s1: #2a78d6; --s2: #eb6834; --s3: #1baf7a; --s4: #eda100; --s5: #e87ba4; --s6: #008300; --s7: #4a3aa7;
  --good: #0ca30c; --good-ink: #006300;
  --warn: #fab219;
  --bad: #d03b3b; --bad-ink: #b42b2b;
  --radius: 16px;
  --shadow: 0 1px 2px rgba(0,0,0,.04), 0 6px 20px rgba(0,0,0,.04);
  --gutter: clamp(16px, 3vw, 48px);
  --gap: clamp(12px, 1.2vw, 20px);

  /* Scala tipografica (rem = base fluida 16 → 17,5 px) */
  --fs-xs: 0.8125rem;   /* 13 */
  --fs-sm: 0.875rem;    /* 14 */
  --fs-md: 1rem;        /* 16 */
  --fs-lg: 1.125rem;    /* 18 */
  --fs-xl: 1.375rem;    /* 22 */
  --fs-2xl: 1.875rem;   /* 30 */
  --fs-hero: clamp(2.5rem, 4vw, 3.5rem);
  font-family: system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
}
@media (prefers-color-scheme: dark) {
  :root:where(:not([data-theme="light"])) {
    color-scheme: dark;
    --page: #0f0f0e; --surface: #1a1a19; --surface-2: #252523;
    --ink: #f5f4ef; --ink-2: #d2d1c8; --muted: #a3a199;
    --grid: #2c2c2a; --axis: #45453f; --border: rgba(255,255,255,.10);
    --accent: #3987e5; --accent-ink: #86b6ef; --accent-soft: #1b2a3d;
    --s1: #3987e5; --s2: #d95926; --s3: #199e70; --s4: #c98500; --s5: #d55181; --s6: #008300; --s7: #9085e9;
    --good-ink: #3ccf3c; --bad-ink: #f47a7a;
    --shadow: none;
  }
}
:root[data-theme="dark"] {
  color-scheme: dark;
  --page: #0f0f0e; --surface: #1a1a19; --surface-2: #252523;
  --ink: #f5f4ef; --ink-2: #d2d1c8; --muted: #a3a199;
  --grid: #2c2c2a; --axis: #45453f; --border: rgba(255,255,255,.10);
  --accent: #3987e5; --accent-ink: #86b6ef; --accent-soft: #1b2a3d;
  --s1: #3987e5; --s2: #d95926; --s3: #199e70; --s4: #c98500; --s5: #d55181; --s6: #008300; --s7: #9085e9;
  --good-ink: #3ccf3c; --bad-ink: #f47a7a;
  --shadow: none;
}

/* ---------- Base ---------- */
* { box-sizing: border-box; }
html { font-size: clamp(16px, 0.35vw + 12.5px, 17.5px); -webkit-text-size-adjust: 100%; }
html, body { margin: 0; }
body { background: var(--page); color: var(--ink); font-size: var(--fs-md); line-height: 1.55; -webkit-font-smoothing: antialiased; }
a { color: var(--accent-ink); }
h1, h2, h3 { margin: 0; font-weight: 650; letter-spacing: -0.01em; line-height: 1.25; }
h2 { font-size: var(--fs-lg); }
h3 { font-size: var(--fs-md); }
p { margin: 0 0 .6em; }
small, .muted { color: var(--muted); }
small { font-size: var(--fs-xs); }
.ink2 { color: var(--ink-2); }
.num, td.n, th.n { font-variant-numeric: tabular-nums; text-align: right; white-space: nowrap; }
code { font-size: .92em; background: var(--surface-2); padding: 0 .3em; border-radius: 4px; }

.wrap { max-width: 1920px; margin: 0 auto; padding: 0 var(--gutter); }

/* ---------- Header ---------- */
header.top { background: var(--surface); border-bottom: 1px solid var(--border); position: sticky; top: 0; z-index: 20; }
/* app installata (iPhone con notch / isola dinamica) */
header.top { padding-top: env(safe-area-inset-top); }
.wrap { padding-left: max(var(--gutter), env(safe-area-inset-left)); padding-right: max(var(--gutter), env(safe-area-inset-right)); }
footer { padding-bottom: max(44px, env(safe-area-inset-bottom)); }
@media (display-mode: standalone) { body { overscroll-behavior-y: none; } }
.top-row { display: flex; align-items: center; gap: 12px; padding: 14px 0 8px; }
.brand { display: flex; flex-direction: column; min-width: 0; flex: 1 1 auto; }
.brand h1 { font-size: var(--fs-xl); white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.brand span { font-size: var(--fs-sm); color: var(--muted); white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.top-actions { margin-left: auto; display: flex; align-items: center; gap: 8px; flex: none; }
.pill { display: inline-flex; align-items: center; gap: 8px; font-size: var(--fs-sm); padding: 6px 12px; border-radius: 999px; background: var(--surface-2); color: var(--ink-2); white-space: nowrap; }
.pill .dot { width: 9px; height: 9px; border-radius: 50%; background: var(--muted); flex: none; }
.pill.ok .dot { background: var(--good); }
.pill.busy .dot { background: var(--warn); }
.pill.err .dot { background: var(--bad); }
.icon-btn { appearance: none; border: 1px solid var(--border); background: var(--surface-2); color: var(--ink-2); width: 40px; height: 40px; border-radius: 12px; display: inline-grid; place-items: center; cursor: pointer; }
.icon-btn:hover { color: var(--ink); background: var(--grid); }
.icon-btn:focus-visible { outline: 2px solid var(--accent); outline-offset: 2px; }
.icon-btn svg { width: 22px; height: 22px; }
.icon-btn[aria-pressed="true"] { background: var(--accent-soft); color: var(--accent-ink); border-color: transparent; }
nav.tabs { display: flex; gap: 4px; overflow-x: auto; scrollbar-width: none; }
nav.tabs::-webkit-scrollbar { display: none; }
nav.tabs button { appearance: none; border: 0; background: none; color: var(--ink-2); font: inherit; font-size: var(--fs-md); padding: 12px 14px; border-bottom: 3px solid transparent; cursor: pointer; white-space: nowrap; }
nav.tabs button:hover { color: var(--ink); }
nav.tabs button[aria-selected="true"] { color: var(--ink); border-bottom-color: var(--accent); font-weight: 650; }
nav.tabs button:focus-visible { outline: 2px solid var(--accent); outline-offset: -2px; border-radius: 6px; }

main { padding: var(--gap) 0 64px; }
section.view { display: none; }
section.view.active { display: block; }

/* ---------- Griglie & card ---------- */
.grid { display: grid; gap: var(--gap); }
.g2 { grid-template-columns: repeat(2, minmax(0, 1fr)); }
.g3 { grid-template-columns: repeat(3, minmax(0, 1fr)); }
.g4 { grid-template-columns: repeat(4, minmax(0, 1fr)); }
.kpis { grid-template-columns: repeat(auto-fit, minmax(min(100%, 15rem), 1fr)); }
@media (min-width: 1500px) { .kpis.eight { grid-template-columns: repeat(4, minmax(0, 1fr)); } }
@media (max-width: 1100px) { .g4 { grid-template-columns: repeat(2, minmax(0, 1fr)); } .g3 { grid-template-columns: repeat(2, minmax(0, 1fr)); } }
@media (max-width: 760px) { .g2, .g3 { grid-template-columns: minmax(0, 1fr); } }
@media (max-width: 480px) { .g4, .kpis { grid-template-columns: minmax(0, 1fr); } }
.mt { margin-top: var(--gap); }
.card { background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius); padding: clamp(16px, 1.4vw, 24px); box-shadow: var(--shadow); min-width: 0; }
.card-head { display: flex; align-items: baseline; gap: 10px; flex-wrap: wrap; margin-bottom: 14px; }
.card-head .sub { font-size: var(--fs-sm); color: var(--muted); }
.card-head .right { margin-left: auto; display: flex; gap: 8px; flex-wrap: wrap; }
.stack > * + * { margin-top: 16px; }
.section-title { display: flex; align-items: baseline; gap: 12px; flex-wrap: wrap; margin: 32px 0 12px; }
.section-title:first-child { margin-top: 0; }
.section-title .sub { color: var(--muted); font-size: var(--fs-sm); }
.fine { font-size: var(--fs-xs); color: var(--muted); margin-top: 10px; }

/* ---------- Hero ---------- */
.hero { display: grid; grid-template-columns: 1.2fr 1fr; gap: clamp(20px, 3vw, 48px); align-items: center; }
@media (max-width: 860px) { .hero { grid-template-columns: 1fr; } }
.hero .label { font-size: var(--fs-md); color: var(--ink-2); }
.hero .value { font-size: var(--fs-hero); font-weight: 650; letter-spacing: -0.02em; line-height: 1.1; margin: 6px 0; }
.hero .meta { font-size: var(--fs-sm); color: var(--muted); }
.meter-row { display: flex; justify-content: space-between; font-size: var(--fs-md); color: var(--ink-2); margin-bottom: 6px; gap: 10px; }
.meter-row small { font-size: var(--fs-sm); }
.meter-track { height: 10px; background: var(--accent-soft); border-radius: 999px; overflow: hidden; }
.meter-fill { height: 100%; background: var(--accent); border-radius: 999px; transition: width .4s ease; min-width: 3px; }
.meter-fill.neg { background: var(--bad); }
.meter + .meter { margin-top: 18px; }

/* ---------- Stat tile ---------- */
.tile .t-label { font-size: var(--fs-md); color: var(--ink-2); display: flex; gap: 8px; align-items: flex-start; justify-content: space-between; }
.tile .t-value { font-size: var(--fs-2xl); font-weight: 650; margin: 6px 0 4px; letter-spacing: -0.015em; line-height: 1.15; }
.tile .t-sub { font-size: var(--fs-sm); color: var(--muted); }
.pos { color: var(--good-ink); }
.neg { color: var(--bad-ink); }
.info { appearance: none; display: inline-grid; place-items: center; width: 24px; height: 24px; padding: 0; border-radius: 50%; border: 1px solid var(--axis); background: transparent; color: var(--ink-2); font: inherit; font-size: var(--fs-xs); font-weight: 650; line-height: 1; cursor: pointer; flex: none; vertical-align: middle; position: relative; }
.info::after { content: ""; position: absolute; inset: -10px; } /* area di tocco ≥ 44px */
.info:hover, .info[aria-expanded="true"] { background: var(--accent-soft); color: var(--accent-ink); border-color: var(--accent); }
.info:focus-visible { outline: 2px solid var(--accent); outline-offset: 2px; }
.card-head .info, .label .info, .meter-row .info { margin-left: 6px; }
#info-pop { position: fixed; z-index: 70; width: min(27rem, calc(100vw - 24px)); background: var(--surface); color: var(--ink); border: 1px solid var(--axis); border-radius: 14px; padding: 16px 18px; box-shadow: 0 10px 40px rgba(0,0,0,.22); font-size: var(--fs-sm); line-height: 1.55; max-height: calc(100vh - 24px); overflow: auto; }
#info-pop.sheet { left: 12px; right: 12px; bottom: 12px; top: auto; width: auto; max-height: 70vh; }
#info-pop p { margin: 0 0 10px; }
#info-pop .ip-head { display: flex; align-items: flex-start; gap: 10px; margin-bottom: 8px; }
#info-pop h4 { margin: 0; font-size: var(--fs-md); font-weight: 650; flex: 1; }
#info-pop .ip-close { appearance: none; border: 0; background: var(--surface-2); color: var(--ink-2); width: 32px; height: 32px; border-radius: 8px; font-size: 1.25rem; line-height: 1; cursor: pointer; flex: none; }
#info-pop .ip-close:focus-visible { outline: 2px solid var(--accent); }
#info-pop .ip-sec { border-top: 1px solid var(--grid); padding-top: 10px; margin-top: 4px; }
#info-pop .ip-sec span { display: block; font-size: var(--fs-xs); text-transform: uppercase; letter-spacing: .04em; color: var(--muted); margin-bottom: 4px; font-weight: 600; }
#info-pop .ip-note { background: var(--surface-2); border-left: 3px solid var(--warn); border-radius: 8px; padding: 8px 10px; color: var(--ink-2); margin: 6px 0 0; }

/* ---------- Controlli ---------- */
.seg { display: inline-flex; background: var(--surface-2); border-radius: 12px; padding: 4px; gap: 2px; flex-wrap: wrap; }
.seg button { border: 0; background: none; font: inherit; font-size: var(--fs-sm); padding: 7px 14px; border-radius: 9px; color: var(--ink-2); cursor: pointer; }
.seg button:hover { color: var(--ink); }
.seg button[aria-pressed="true"] { background: var(--surface); color: var(--ink); box-shadow: 0 1px 3px rgba(0,0,0,.12); font-weight: 650; }
.seg button:focus-visible { outline: 2px solid var(--accent); outline-offset: 1px; }
.chip { display: inline-flex; align-items: center; gap: 8px; border: 1px solid var(--border); background: var(--surface); border-radius: 999px; padding: 6px 14px; font-size: var(--fs-sm); cursor: pointer; color: var(--ink-2); user-select: none; position: relative; }
.chip input { position: absolute; opacity: 0; pointer-events: none; }
.chip .sw { width: 16px; height: 3px; border-radius: 2px; background: currentColor; }
.chip.off { opacity: .6; }
.chip.off .sw { background: var(--axis) !important; }
.chip:focus-within { outline: 2px solid var(--accent); outline-offset: 1px; }
.toolbar { display: flex; flex-wrap: wrap; gap: 10px 12px; align-items: center; margin-bottom: 16px; }
.toolbar .lbl { font-size: var(--fs-sm); color: var(--muted); }

/* ---------- Grafici ---------- */
.chart { position: relative; height: clamp(260px, 30vh, 380px); }
.chart.tall { height: clamp(300px, 38vh, 480px); }
.legend { display: flex; flex-wrap: wrap; gap: 16px; font-size: var(--fs-sm); color: var(--ink-2); margin-top: 12px; }
.legend i { display: inline-block; width: 12px; height: 12px; border-radius: 3px; margin-right: 6px; vertical-align: -1px; }

/* ---------- Tabelle ---------- */
.table-wrap { overflow-x: auto; -webkit-overflow-scrolling: touch; margin: 0 calc(-1 * clamp(16px, 1.4vw, 24px)); padding: 0 clamp(16px, 1.4vw, 24px); }
table { width: 100%; border-collapse: collapse; font-size: var(--fs-sm); }
th, td { padding: 10px 10px; border-bottom: 1px solid var(--grid); text-align: left; vertical-align: top; }
td:first-child { white-space: nowrap; }
th { font-weight: 650; color: var(--ink-2); font-size: var(--fs-sm); position: sticky; top: 0; background: var(--surface); }
tr.total td { font-weight: 650; border-top: 1px solid var(--axis); border-bottom: 0; }
td .est { color: var(--muted); font-size: var(--fs-xs); margin-left: 2px; }
tbody tr:hover td { background: var(--surface-2); }
.kv { display: grid; grid-template-columns: 1fr auto; gap: 10px 18px; font-size: var(--fs-md); margin: 0; }
.kv dt { color: var(--ink-2); }
.kv dd { margin: 0; text-align: right; font-variant-numeric: tabular-nums; }
.kv .tot { font-weight: 650; border-top: 1px solid var(--grid); padding-top: 10px; }
.kv .fine-row { grid-column: 1 / -1; font-size: var(--fs-xs); color: var(--muted); }

/* ---------- Form ---------- */
form .row { display: grid; gap: 14px; grid-template-columns: repeat(auto-fill, minmax(12rem, 1fr)); }
label.f { display: flex; flex-direction: column; gap: 6px; font-size: var(--fs-sm); color: var(--ink-2); }
label.f.wide { grid-column: 1 / -1; }
input, select, textarea { font: inherit; font-size: var(--fs-md); color: var(--ink); background: var(--surface); border: 1px solid var(--axis); border-radius: 10px; padding: 10px 12px; min-width: 0; width: 100%; min-height: 44px; }
input:focus, select:focus, textarea:focus { outline: 2px solid var(--accent); outline-offset: 0; border-color: transparent; }
input[type="range"] { padding: 0; border: 0; accent-color: var(--accent); min-height: 28px; }
input[type="checkbox"] { width: 20px; height: 20px; min-height: 0; accent-color: var(--accent); }
.btn { appearance: none; border: 1px solid var(--axis); background: var(--surface); color: var(--ink); font: inherit; font-size: var(--fs-md); padding: 10px 18px; border-radius: 10px; cursor: pointer; display: inline-flex; align-items: center; gap: 8px; text-decoration: none; min-height: 44px; }
.btn:hover { background: var(--surface-2); }
.btn.primary { background: var(--accent); color: #fff; border-color: transparent; font-weight: 600; }
.btn.primary:hover { filter: brightness(1.06); }
.btn.ghost { border-color: transparent; background: transparent; color: var(--ink-2); }
.btn.small { font-size: var(--fs-sm); padding: 6px 12px; border-radius: 8px; min-height: 36px; }
.btn.danger { color: var(--bad-ink); }
.btn:focus-visible { outline: 2px solid var(--accent); outline-offset: 2px; }
.actions { display: flex; gap: 10px; flex-wrap: wrap; margin-top: 16px; align-items: center; }
.drop { display: block; border: 2px dashed var(--axis); border-radius: 14px; padding: 24px; text-align: center; color: var(--ink-2); font-size: var(--fs-md); cursor: pointer; transition: background .15s; }
.drop.over, .drop:hover { background: var(--accent-soft); border-color: var(--accent); }
.drop input { display: none; }
.note { font-size: var(--fs-sm); color: var(--ink-2); background: var(--surface-2); border-radius: 12px; padding: 12px 14px; }
.note.warn { border-left: 4px solid var(--warn); }
.note.bad { border-left: 4px solid var(--bad); }
.badge { display: inline-block; font-size: var(--fs-xs); padding: 2px 9px; border-radius: 999px; background: var(--surface-2); color: var(--ink-2); white-space: nowrap; font-weight: 500; }
.badge.ok { background: rgba(12,163,12,.14); color: var(--good-ink); }
.badge.bad { background: rgba(208,59,59,.16); color: var(--bad-ink); }
.badge.warn { background: rgba(250,178,25,.2); color: var(--ink-2); }
details.box { border: 1px solid var(--border); border-radius: 12px; padding: 12px 16px; background: var(--surface); }
details.box summary { cursor: pointer; font-weight: 650; font-size: var(--fs-md); }
details.box[open] summary { margin-bottom: 12px; }
ol.steps { font-size: var(--fs-md); padding-left: 1.3em; margin: 0; color: var(--ink-2); }
ol.steps li + li { margin-top: 8px; }
.prose { font-size: var(--fs-md); color: var(--ink-2); }

/* Slider */
.sl { display: grid; grid-template-columns: 1fr 6.5rem; gap: 6px 12px; align-items: center; }
.sl .l { grid-column: 1 / -1; font-size: var(--fs-sm); color: var(--ink-2); display: flex; justify-content: space-between; gap: 8px; }
.sl input[type="number"] { padding: 6px 10px; text-align: right; min-height: 40px; }

/* Toast */
#toast { position: fixed; left: 50%; bottom: 24px; transform: translateX(-50%) translateY(20px); background: var(--ink); color: var(--page); padding: 12px 18px; border-radius: 12px; font-size: var(--fs-md); opacity: 0; pointer-events: none; transition: all .25s ease; z-index: 50; max-width: calc(100% - 32px); }
#toast.show { opacity: 1; transform: translateX(-50%) translateY(0); }
#toast.err { background: var(--bad); color: #fff; }

/* ---------- Modalità privacy ---------- */
.priv-badge { display: none; }
body.privacy .priv-badge { display: inline-flex; }
@media (max-width: 1100px) { body.privacy .priv-badge { display: none; } }
body.privacy .priv-blur { filter: blur(7px); transition: filter .15s; }
body.privacy .priv-blur:focus, body.privacy .priv-blur:focus-within { filter: none; }
.masked { letter-spacing: 0.08em; color: var(--muted); }

.sr-only { position: absolute; width: 1px; height: 1px; overflow: hidden; clip: rect(0 0 0 0); white-space: nowrap; }
.empty { padding: 28px; text-align: center; color: var(--muted); font-size: var(--fs-md); }
footer { color: var(--muted); font-size: var(--fs-sm); padding: 24px 0 44px; }

@media (max-width: 560px) {
  #src-label { display: none; }
  .top-actions { gap: 6px; }
  .icon-btn { width: 38px; height: 38px; }
  .pill { padding: 8px; }
  .top-row { padding-top: 10px; }
  nav.tabs { display: grid; grid-template-columns: repeat(3, 1fr); gap: 0; }
  nav.tabs button { padding: 10px 4px; font-size: var(--fs-sm); }
}
.onb { border: 2px solid var(--accent); margin-bottom: 16px; }
.onb-steps { margin: 0; padding-left: 1.4em; display: grid; gap: 12px; }
.onb-steps li { line-height: 1.45; }
.onb-steps li.done { color: var(--muted); }
.onb-steps li.done strong::before { content: '✓ '; color: var(--accent); }
.onb-steps .btn { margin-left: 6px; vertical-align: middle; }

.rep { margin-top: 16px; border-top: 1px solid var(--grid); padding-top: 16px; }
.rep-head { display: flex; align-items: center; gap: 10px; flex-wrap: wrap; margin-bottom: 12px; }
.rep-head select { width: auto; min-height: 40px; }
.rep-sum { display: grid; grid-template-columns: repeat(auto-fit, minmax(9rem, 1fr)); gap: 10px; margin-bottom: 12px; }
.rep-sum > div { background: var(--surface-2); border-radius: 12px; padding: 10px 12px; }
.rep-sum span { display: block; font-size: var(--fs-xs); color: var(--muted); }
.rep-sum strong { font-size: var(--fs-lg); font-variant-numeric: tabular-nums; }
.checks { list-style: none; padding: 0; margin: 0 0 12px; }
.checks li { display: flex; gap: 10px; padding: 9px 0; border-bottom: 1px solid var(--grid); font-size: var(--fs-sm); color: var(--ink-2); align-items: flex-start; }
.checks li:last-child { border-bottom: 0; }
.checks .ic { flex: none; width: 1.4em; text-align: center; }
.checks li.lv-warn { color: var(--ink); font-weight: 500; }
.map-grid { display: grid; gap: 12px; grid-template-columns: repeat(auto-fill, minmax(14rem, 1fr)); margin: 12px 0; }

/* ---------- Modali (nuova prenotazione / nuova spesa) ---------- */
dialog.modal { border: 1px solid var(--axis); border-radius: 16px; padding: 0; background: var(--surface); color: var(--ink); width: min(40rem, calc(100vw - 24px)); max-width: none; max-height: calc(100dvh - 24px); overflow: visible; box-shadow: 0 20px 60px rgba(0,0,0,.35); }
dialog.modal::backdrop { background: rgba(0,0,0,.5); }
.modal-body { padding: 18px clamp(16px, 2vw, 24px) 20px; overflow: auto; max-height: calc(100dvh - 24px); border-radius: inherit; }
.modal-head { display: flex; align-items: center; gap: 10px; margin-bottom: 14px; }
.modal-head h3 { margin: 0; font-size: var(--fs-lg); flex: none; }
.modal-x { appearance: none; border: 0; background: var(--surface-2); color: var(--ink-2); width: 36px; height: 36px; border-radius: 10px; font-size: 1.4rem; line-height: 1; cursor: pointer; margin-left: auto; flex: none; }
.modal-x:hover { background: var(--grid); }
.modal-x:focus-visible { outline: 2px solid var(--accent); }
@media (max-width: 640px) {
  dialog.modal { width: 100vw; margin: auto 0 0; max-height: 92dvh; border-radius: 16px 16px 0 0; border-bottom: 0; }
  .modal-body { max-height: 92dvh; }
  .modal .actions .btn { flex: 1 1 100%; justify-content: center; }
}

