:root {
  color-scheme: light;
  --bg: #fbfcfd;
  --raised: #ffffff;
  --ink: #0f1d33;
  --ink-2: #47536a;
  --muted: #6f7a8c;
  --hair: #e4e8ef;
  --axis: #c6cdd8;
  --accent: #0b3a82;
  --accent-wash: #e8eef8;
  --ok: #2a78d6;
  --rev: #eb6834;
  --loss: #e34948;
  --oth: #a3acba;
  --good-text: #006300;
  --bad-text: #b42323;
  --warn: #fab219;
  --crit: #d03b3b;
  --good: #0ca30c;
  --shadow: 0 -8px 40px rgba(15, 29, 51, .18);
  --font: system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
  --safe-b: env(safe-area-inset-bottom, 0px);
}
@media (prefers-color-scheme: dark) {
  :root {
    color-scheme: dark;
    --bg: #121924;
    --raised: #1a2331;
    --ink: #edf1f7;
    --ink-2: #b8c1cf;
    --muted: #8b95a6;
    --hair: #243042;
    --axis: #344257;
    --accent: #9bbcff;
    --accent-wash: #1f2c42;
    --ok: #3987e5;
    --rev: #d95926;
    --loss: #e66767;
    --oth: #5f6b7d;
    --good-text: #2fbf4f;
    --bad-text: #f08080;
    --shadow: 0 -8px 40px rgba(0, 0, 0, .5);
  }
}

* { box-sizing: border-box; }
html { -webkit-text-size-adjust: 100%; }
body {
  margin: 0; background: var(--bg); color: var(--ink);
  font: 400 15px/1.45 var(--font);
  padding-bottom: calc(72px + var(--safe-b));
  -webkit-tap-highlight-color: transparent;
}
button { font: inherit; color: inherit; background: none; border: 0; padding: 0; cursor: pointer; }
:focus-visible { outline: 2px solid var(--accent); outline-offset: 2px; border-radius: 6px; }
h1, h2, p { margin: 0; }
b { font-weight: 600; }
[hidden] { display: none !important; }

/* barra superiore */
.topbar {
  position: sticky; top: 0; z-index: 5;
  display: flex; align-items: center; justify-content: space-between;
  padding: calc(env(safe-area-inset-top, 0px) + 12px) 18px 10px;
  background: color-mix(in srgb, var(--bg) 88%, transparent);
  backdrop-filter: saturate(1.4) blur(14px); -webkit-backdrop-filter: saturate(1.4) blur(14px);
  border-bottom: 1px solid transparent;
}
.topbar.scrolled { border-bottom-color: var(--hair); }
.topbar h1 { font-size: 22px; font-weight: 650; letter-spacing: -.01em; }
.sync { display: flex; align-items: center; gap: 7px; font-size: 13px; color: var(--muted); padding: 6px 2px 6px 8px; }
.sync .dot { width: 8px; height: 8px; border-radius: 50%; background: var(--oth); }
.sync.live .dot { background: var(--good); }
.sync.warn .dot { background: var(--warn); }
.sync.err .dot { background: var(--crit); }
.sync.busy .dot { animation: blink 1s ease-in-out infinite; }
@keyframes blink { 50% { opacity: .25; } }
@media (prefers-reduced-motion: reduce) { .sync.busy .dot { animation: none; } }

main { max-width: 720px; margin: 0 auto; padding: 4px 18px 24px; overflow-x: clip; }
.view { display: flex; flex-direction: column; gap: 26px; }

/* controllo periodo */
.seg { display: grid; grid-template-columns: repeat(4, 1fr); background: var(--accent-wash); border-radius: 10px; padding: 3px; margin-top: 6px; }
.seg button { font-size: 13.5px; padding: 7px 0; border-radius: 8px; color: var(--ink-2); }
.seg button[aria-selected="true"] { background: var(--raised); color: var(--ink); font-weight: 600; box-shadow: 0 1px 2px rgba(15,29,51,.12); }

/* numero principale */
.hero { padding-top: 2px; }
.hero-label { font-size: 14px; color: var(--ink-2); }
.hero-value { font-size: 54px; line-height: 1.05; font-weight: 650; letter-spacing: -.03em; margin: 4px 0 8px; }
.hero-value.pos { color: var(--good-text); }
.hero-value.neg { color: var(--bad-text); }
.hero-sub { display: flex; gap: 18px; color: var(--muted); font-size: 14px; }
.hero-sub b { color: var(--ink-2); font-variant-numeric: tabular-nums; }

/* blocchi */
.block { display: flex; flex-direction: column; gap: 8px; }
.block-h { display: flex; align-items: baseline; justify-content: space-between; gap: 12px; flex-wrap: wrap; }
.block-h h2 { font-size: 17px; font-weight: 620; letter-spacing: -.005em; }
.sub { font-size: 13px; color: var(--muted); margin-top: -4px; }
.note { font-size: 13px; color: var(--ink-2); }
.legend { display: flex; gap: 12px; font-size: 12.5px; color: var(--ink-2); }
.legend span { display: inline-flex; align-items: center; gap: 5px; }
.sw { display: inline-block; width: 10px; height: 10px; border-radius: 3px; flex: none; }
.sw.ok { background: var(--ok); } .sw.rev { background: var(--rev); } .sw.oth { background: var(--oth); } .sw.loss { background: var(--loss); }

/* grafici */
.chart-box { width: 100%; min-height: 60px; }
.chart { display: block; width: 100%; height: auto; overflow: visible; touch-action: pan-y; }
.chart .hair { stroke: var(--hair); stroke-width: 1; }
.chart .base { stroke: var(--axis); stroke-width: 1; }
.chart .tick { fill: var(--muted); font-size: 11px; font-variant-numeric: tabular-nums; }
.chart .empty { fill: var(--muted); font-size: 13px; }
.chart .hit.on { fill: var(--ink); fill-opacity: .05; }
.chart .cross { stroke: var(--axis); stroke-width: 1; }
.chart .ring { stroke: var(--bg); stroke-width: 2; }

.tooltip {
  position: fixed; left: 0; top: 0; z-index: 50; pointer-events: none;
  background: var(--raised); color: var(--ink); border: 1px solid var(--hair);
  box-shadow: 0 6px 24px rgba(15,29,51,.16); border-radius: 10px; padding: 8px 10px;
  font-size: 13px; min-width: 150px; max-width: 260px;
}
.tooltip .tt-h { font-weight: 600; margin-bottom: 4px; }
.tooltip .row { display: flex; align-items: center; gap: 6px; }
.tooltip .row span { flex: 1; color: var(--ink-2); }
.tooltip .row b { font-variant-numeric: tabular-nums; }
.tooltip .tt-f { color: var(--muted); margin-top: 4px; font-size: 12px; }

details.data summary { font-size: 13px; color: var(--accent); cursor: pointer; list-style: none; padding: 4px 0; }
details.data summary::-webkit-details-marker { display: none; }
details.data[open] summary { margin-bottom: 6px; }
table { width: 100%; border-collapse: collapse; font-size: 13px; font-variant-numeric: tabular-nums; }
th, td { text-align: right; padding: 6px 4px; border-bottom: 1px solid var(--hair); }
th:first-child, td:first-child { text-align: left; }
td:not(:first-child) { white-space: nowrap; }
th { font-weight: 600; color: var(--ink-2); font-size: 12px; }

/* avvisi */
.alerts { list-style: none; margin: 0; padding: 0; display: flex; flex-direction: column; gap: 8px; }
.alerts:empty { display: none; }
.alert { display: flex; gap: 10px; align-items: flex-start; padding: 11px 12px; border-radius: 12px; background: var(--accent-wash); font-size: 14px; }
.alert svg { width: 18px; height: 18px; flex: none; margin-top: 1px; }
.alert.warning svg { color: #c98a00; } .alert.critical svg { color: var(--crit); } .alert.info svg { color: var(--accent); }
.alert b { display: block; }
.alert p { color: var(--ink-2); font-size: 13.5px; }
.alert button { color: var(--accent); font-size: 13.5px; font-weight: 600; margin-top: 4px; }

/* statistiche: griglia con linee sottili, non card */
.stats { display: grid; grid-template-columns: 1fr 1fr; border-top: 1px solid var(--hair); }
.stat { padding: 14px 4px 14px 0; border-bottom: 1px solid var(--hair); }
.stat:nth-child(odd) { padding-right: 14px; border-right: 1px solid var(--hair); }
.stat:nth-child(even) { padding-left: 14px; }
.stat .l { font-size: 13px; color: var(--ink-2); }
.stat .v { font-size: 24px; font-weight: 620; letter-spacing: -.015em; margin: 2px 0; }
.stat .d { font-size: 12.5px; color: var(--muted); }

/* liste transazioni */
.chips { display: flex; gap: 8px; overflow-x: auto; margin-top: 6px; scrollbar-width: none; }
.chips button { flex: none; font-size: 14px; padding: 7px 14px; border-radius: 999px; border: 1px solid var(--hair); color: var(--ink-2); }
.chips button[aria-selected="true"] { background: var(--ink); border-color: var(--ink); color: var(--bg); font-weight: 600; }
.count { font-size: 13px; color: var(--muted); margin-bottom: -16px; }
.txlist { list-style: none; margin: 0; padding: 0; }
.txlist li { border-bottom: 1px solid var(--hair); }
.txlist li > button { display: grid; grid-template-columns: 22px minmax(0, 1fr) auto; gap: 10px; align-items: center; width: 100%; text-align: left; padding: 11px 0; }
.txlist .ic { width: 22px; height: 22px; border-radius: 50%; display: grid; place-items: center; color: #fff; font-size: 12px; font-weight: 700; }
.txlist .ic.ok { background: var(--ok); } .txlist .ic.rev { background: var(--rev); } .txlist .ic.oth { background: var(--oth); }
.txlist .t1 { font-weight: 560; font-size: 15px; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.txlist .t2 { font-size: 12.5px; color: var(--muted); white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.txlist .amt { text-align: right; font-variant-numeric: tabular-nums; }
.txlist .amt .n { font-weight: 600; font-size: 15px; }
.txlist .amt .g { font-size: 12px; color: var(--muted); }
.pos { color: var(--good-text); } .neg { color: var(--bad-text); } .zero { color: var(--muted); }
.ranked { counter-reset: r; }
.ranked li > button { grid-template-columns: 22px minmax(0, 1fr) auto; }
.txlist .mid { display: block; min-width: 0; }
.txlist .t1, .txlist .t2 { display: block; }
.ranked .ic.rank { background: none; color: var(--ink-2); font-size: 13px; font-weight: 600; font-variant-numeric: tabular-nums; }
.more { align-self: center; color: var(--accent); font-weight: 600; padding: 10px 18px; }
.emptymsg { color: var(--muted); font-size: 14px; padding: 14px 0; }

/* righe saldi */
.rows { list-style: none; margin: 0; padding: 0; }
.rows li { display: grid; grid-template-columns: minmax(0, 1fr) auto; gap: 2px 12px; padding: 11px 0; border-bottom: 1px solid var(--hair); }
.rows .a { font-weight: 560; }
.rows .b { text-align: right; font-weight: 600; font-variant-numeric: tabular-nums; }
.rows .c { font-size: 12.5px; color: var(--muted); }
.rows .e { font-size: 12.5px; color: var(--muted); text-align: right; font-variant-numeric: tabular-nums; }
.share { grid-column: 1 / -1; height: 4px; border-radius: 4px; background: var(--accent-wash); overflow: hidden; margin-top: 6px; }
.share i { display: block; height: 100%; background: var(--ok); border-radius: 4px; }
.tag { display: inline-block; font-size: 11.5px; font-weight: 600; padding: 1px 7px; border-radius: 6px; margin-left: 6px; vertical-align: 1px; background: var(--accent-wash); color: var(--accent); }
.tag.warn { background: color-mix(in srgb, var(--warn) 22%, transparent); color: var(--ink); }
.mono { font-family: ui-monospace, "SF Mono", Menlo, monospace; font-size: 13px; }

.tank .meter { height: 8px; border-radius: 8px; background: var(--accent-wash); overflow: hidden; margin: 10px 0 6px; }
.tank .meter i { display: block; height: 100%; border-radius: 8px; }
.tank .big { font-size: 24px; font-weight: 620; }
.tank p { font-size: 13.5px; color: var(--ink-2); }
.foot { font-size: 12.5px; color: var(--muted); text-align: center; }

/* barra di navigazione */
.tabbar {
  position: fixed; left: 0; right: 0; bottom: 0; z-index: 10;
  display: grid; grid-template-columns: repeat(4, 1fr);
  padding: 6px 6px calc(6px + var(--safe-b));
  background: color-mix(in srgb, var(--bg) 92%, transparent);
  backdrop-filter: saturate(1.4) blur(14px); -webkit-backdrop-filter: saturate(1.4) blur(14px);
  border-top: 1px solid var(--hair);
}
.tabbar button { display: flex; flex-direction: column; align-items: center; gap: 2px; font-size: 11px; color: var(--muted); padding: 4px 0; }
.tabbar svg { width: 24px; height: 24px; fill: none; stroke: currentColor; stroke-width: 1.8; stroke-linecap: round; stroke-linejoin: round; }
.tabbar button[aria-current="page"] { color: var(--accent); font-weight: 600; }

/* foglio dettaglio */
.scrim { position: fixed; inset: 0; background: rgba(8, 14, 24, .38); z-index: 20; }
.sheet {
  position: fixed; left: 0; right: 0; bottom: 0; z-index: 21; max-height: 88vh; overflow-y: auto;
  background: var(--raised); border-radius: 18px 18px 0 0; box-shadow: var(--shadow);
  padding: 8px 20px calc(24px + var(--safe-b)); max-width: 720px; margin: 0 auto;
  animation: up .22s ease-out;
}
@keyframes up { from { transform: translateY(40px); opacity: 0; } }
@media (prefers-reduced-motion: reduce) { .sheet { animation: none; } }
.sheet .close { position: absolute; top: 12px; right: 16px; color: var(--accent); font-weight: 600; font-size: 15px; padding: 4px 6px; }
.grab { width: 38px; height: 5px; border-radius: 3px; background: var(--hair); margin: 2px auto 14px; }
.sheet h2 { font-size: 20px; font-weight: 650; padding-right: 64px; }
.sheet .big { font-size: 36px; font-weight: 650; letter-spacing: -.02em; margin: 6px 0 2px; }
.sheet dl { display: grid; grid-template-columns: auto 1fr; gap: 8px 14px; margin: 16px 0; font-size: 14px; }
.sheet dt { color: var(--muted); }
.sheet dd { margin: 0; text-align: right; font-variant-numeric: tabular-nums; overflow-wrap: anywhere; }
.sheet h3 { font-size: 15px; font-weight: 620; margin: 18px 0 6px; }
.flow { list-style: none; padding: 0; margin: 0; font-size: 13.5px; }
.flow li { display: grid; grid-template-columns: 34px 1fr auto; gap: 8px; padding: 7px 0; border-bottom: 1px solid var(--hair); align-items: baseline; }
.flow .dir { font-size: 12px; font-weight: 600; color: var(--muted); }
.flow .who { color: var(--muted); font-size: 12px; }
.sheet .open { display: block; text-align: center; margin-top: 18px; padding: 12px; border-radius: 12px; background: var(--accent-wash); color: var(--accent); font-weight: 600; text-decoration: none; }

/* login */
dialog { border: 0; border-radius: 16px; padding: 22px; background: var(--raised); color: var(--ink); width: min(360px, 92vw); }
dialog::backdrop { background: rgba(8, 14, 24, .5); }
dialog h2 { font-size: 18px; margin-bottom: 6px; }
dialog p { font-size: 14px; color: var(--ink-2); margin-bottom: 12px; }
dialog input { width: 100%; font: inherit; padding: 10px 12px; border-radius: 10px; border: 1px solid var(--axis); background: var(--bg); color: var(--ink); }
dialog .err { color: var(--bad-text); margin: 8px 0 0; }
dialog button { margin-top: 14px; width: 100%; padding: 11px; border-radius: 10px; background: var(--accent); color: var(--bg); font-weight: 600; }

/* avanzamento primo avvio */
.boot { padding: 30px 0; text-align: center; color: var(--ink-2); }
.boot .bar { height: 6px; border-radius: 6px; background: var(--accent-wash); overflow: hidden; margin: 14px auto; max-width: 280px; }
.boot .bar i { display: block; height: 100%; background: var(--ok); width: 0; transition: width .4s; }
.vh { position: absolute; width: 1px; height: 1px; overflow: hidden; clip: rect(0 0 0 0); white-space: nowrap; }
.mono { color: inherit; }
a.mono { color: var(--accent); text-decoration: none; }
body.booting #view-home > :not(#boot) { display: none; }
.preview-note { font-size: 13px; color: var(--ink-2); background: var(--accent-wash); border-radius: 10px; padding: 9px 12px; margin-top: 6px; }
