:root {
  --bg: #F6F5F1; --card: #FFFFFF; --line: #E6E3DC; --line2: #EFEDE7; --chip: #EBE8E1; --soft: #F6F5F1;
  --ink: #16181D; --ink2: #3F4450; --muted: #5B616E; --faint: #A8A399;
  --accent: #0F766E; --accent-text: #0F766E; --on-accent: #FFFFFF;
  --in: #15803D; --out: #C2410C; --bar-in: #0F766E; --bar-out: #F59E0B;
  --ok-bg: #DCFCE7; --ok-ink: #14532D; --warn-bg: #FFEDD5; --warn-ink: #9A3412;
  --track: #CFCAC0; --shadow: 0 1px 3px rgba(0,0,0,.12);
  --r: 20px; --font: -apple-system, BlinkMacSystemFont, "SF Pro Text", "Segoe UI", Roboto, "Helvetica Neue", sans-serif;
  color-scheme: light;
}
@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) {
    --bg: #0F1012; --card: #1A1C20; --line: #2A2D33; --line2: #23262B; --chip: #23262B; --soft: #141518;
    --ink: #F2F2F0; --ink2: #C9CCD2; --muted: #9AA0AA; --faint: #6B7079;
    --accent: #0F766E; --accent-text: #2DD4BF; --in: #4ADE80; --out: #FB923C; --bar-in: #14B8A6; --bar-out: #F59E0B;
    --ok-bg: #12301E; --ok-ink: #86EFAC; --warn-bg: #3B2412; --warn-ink: #FDBA74; --track: #3A3E45; --shadow: 0 1px 3px rgba(0,0,0,.5);
    color-scheme: dark;
  }
}
:root[data-theme="dark"] {
  --bg: #0F1012; --card: #1A1C20; --line: #2A2D33; --line2: #23262B; --chip: #23262B; --soft: #141518;
  --ink: #F2F2F0; --ink2: #C9CCD2; --muted: #9AA0AA; --faint: #6B7079;
  --accent: #0F766E; --accent-text: #2DD4BF; --in: #4ADE80; --out: #FB923C; --bar-in: #14B8A6; --bar-out: #F59E0B;
  --ok-bg: #12301E; --ok-ink: #86EFAC; --warn-bg: #3B2412; --warn-ink: #FDBA74; --track: #3A3E45; --shadow: 0 1px 3px rgba(0,0,0,.5);
  color-scheme: dark;
}
* { box-sizing: border-box; -webkit-tap-highlight-color: transparent; }
html, body { touch-action: manipulation; -webkit-text-size-adjust: 100%; text-size-adjust: 100%; margin: 0; background: var(--bg); color: var(--ink); font-family: var(--font); font-size: 16px; font-variant-numeric: tabular-nums; }
html, body { height: 100%; overflow: hidden; }
body { overscroll-behavior: none; }
button, input, select, textarea { font: inherit; color: inherit; }
a { color: var(--accent-text); text-decoration: none; }
h1, h2, h3 { margin: 0; }
svg { display: block; }
#app { max-width: 560px; margin: 0 auto; height: 100vh; height: 100dvh; position: relative; display: flex; flex-direction: column; }
/* lo scorrimento avviene dentro l'app, non nella pagina: la barra del browser resta ferma e non ci sono ridisegni */
#view { flex: 1 1 auto; min-height: 0; overflow-y: auto; overflow-x: hidden; -webkit-overflow-scrolling: touch; overscroll-behavior: contain; }
#view { box-sizing: border-box; padding: calc(env(safe-area-inset-top) + 20px) 18px calc(env(safe-area-inset-bottom) + 110px); display: flex; flex-direction: column; gap: 18px; }
#view.no-tab { padding-bottom: calc(env(safe-area-inset-bottom) + 110px); }

/* testata */
.hdr { display: flex; align-items: center; justify-content: space-between; gap: 10px; min-height: 44px; }
.hdr-title { font-size: 17px; font-weight: 700; text-align: center; flex: 1; }
.h1 { font-size: 30px; font-weight: 800; letter-spacing: -.02em; }
.hello { display: flex; flex-direction: column; gap: 2px; }
.hello small { font-size: 13px; color: var(--muted); font-weight: 500; }
.hello b { font-size: 22px; font-weight: 800; letter-spacing: -.02em; }
.iconbtn { width: 44px; height: 44px; flex: 0 0 auto; border-radius: 14px; background: var(--card); border: 1px solid var(--line); display: inline-flex; align-items: center; justify-content: center; color: var(--ink); cursor: pointer; padding: 0; }
.iconbtn.primary { background: var(--accent); border-color: var(--accent); color: var(--on-accent); }
.iconbtn.ghost { visibility: hidden; }
.row-actions { display: flex; gap: 8px; }

/* testi */
.muted { color: var(--muted); }
.small { font-size: 12px; }
.cap { font-size: 11px; font-weight: 700; color: var(--muted); letter-spacing: .06em; text-transform: uppercase; }
.big { font-size: 30px; font-weight: 800; letter-spacing: -.02em; line-height: 1.1; }
.mid { font-size: 20px; font-weight: 800; }
.pos { color: var(--in); } .neg { color: var(--out); }
.sec-h { display: flex; justify-content: space-between; align-items: baseline; gap: 8px; }
.sec-h h2 { font-size: 17px; font-weight: 700; }
.sec-h a, .link { font-size: 13px; font-weight: 600; cursor: pointer; background: none; border: 0; color: var(--accent-text); padding: 0; }
.stack { display: flex; flex-direction: column; gap: 10px; }
.note { font-size: 12px; color: var(--muted); line-height: 1.45; padding: 0 4px; }

/* card */
.card { background: var(--card); border: 1px solid var(--line); border-radius: var(--r); padding: 16px; display: flex; flex-direction: column; gap: 12px; }
.card.flush { padding: 4px 14px; gap: 0; }
.card.np { padding: 0; overflow: hidden; gap: 0; }
.card-h { display: flex; flex-direction: column; gap: 3px; padding-bottom: 12px; border-bottom: 1px solid var(--line2); }
.card-h h2 { font-size: 19px; font-weight: 800; }
.card-h span { font-size: 13px; color: var(--muted); }
.hero { background: var(--accent); color: var(--on-accent); border-radius: 26px; padding: 22px; display: flex; flex-direction: column; gap: 18px; }
.hero .lbl { font-size: 13px; font-weight: 600; opacity: .85; }
.hero .num { font-size: 38px; font-weight: 800; letter-spacing: -.03em; line-height: 1; }
.hero .sub { font-size: 13px; font-weight: 600; opacity: .9; }
.hero .mini { background: rgba(255,255,255,.14); border-radius: 16px; padding: 12px 14px; display: flex; flex-direction: column; gap: 4px; }
.hero .mini span { font-size: 12px; font-weight: 600; opacity: .9; }
.hero .mini b { font-size: 17px; }
.grid2 { display: grid; grid-template-columns: repeat(2, minmax(0, 1fr)); gap: 10px; }
.grid3 { display: grid; grid-template-columns: repeat(3, minmax(0, 1fr)); gap: 8px; }
.grid4 { display: grid; grid-template-columns: repeat(4, minmax(0, 1fr)); gap: 8px; }
.stat { background: var(--card); border: 1px solid var(--line); border-radius: 16px; padding: 12px; display: flex; flex-direction: column; gap: 3px; min-width: 0; }
.stat span { font-size: 12px; font-weight: 600; color: var(--muted); }
.stat b { font-size: 15px; font-weight: 800; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }

/* liste */
.item { display: flex; align-items: center; gap: 12px; padding: 10px 0; border-bottom: 1px solid var(--line2); min-height: 56px; width: 100%; background: none; border-left: 0; border-right: 0; border-top: 0; text-align: left; cursor: pointer; color: inherit; }
.item:last-child { border-bottom: 0; }
.item .main { flex: 1; min-width: 0; display: flex; flex-direction: column; gap: 2px; }
.item .t { font-size: 15px; font-weight: 600; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.item .s { font-size: 12px; color: var(--muted); overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.item .amt { font-size: 15px; font-weight: 700; white-space: nowrap; }
.item .amt small { display: block; font-size: 11px; color: var(--muted); font-weight: 500; text-align: right; }
.ico { width: 42px; height: 42px; border-radius: 14px; display: flex; align-items: center; justify-content: center; flex: 0 0 auto; }
.ico.sm { width: 36px; height: 36px; border-radius: 12px; }
.dot { width: 10px; height: 10px; border-radius: 50%; flex: 0 0 auto; }
.day-h { display: flex; justify-content: space-between; font-size: 13px; font-weight: 700; color: var(--muted); padding: 0 4px; }
.datebox { width: 44px; flex: 0 0 auto; display: flex; flex-direction: column; align-items: center; font-size: 11px; font-weight: 700; color: var(--muted); }
.datebox b { font-size: 17px; color: var(--ink); }

/* conti orizzontali */
.hscroll { display: flex; gap: 10px; overflow-x: auto; margin: 0 -18px; padding: 0 18px 4px; scrollbar-width: none; }
.hscroll::-webkit-scrollbar { display: none; }
.acc { flex: 0 0 auto; width: 142px; background: var(--card); border: 1px solid var(--line); border-radius: 18px; padding: 14px; display: flex; flex-direction: column; gap: 8px; color: inherit; }
.acc.excl { background: var(--soft); border: 1.5px dashed var(--track); }
.acc .sw { width: 10px; height: 10px; border-radius: 4px; }
.acc .n { font-size: 12px; color: var(--muted); font-weight: 600; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.acc .b { font-size: 16px; font-weight: 700; }

/* barre */
.bar { position: relative; height: 10px; border-radius: 10px; background: var(--line2); }
.bar > i { position: absolute; left: 0; top: 0; bottom: 0; border-radius: 10px; }
.bar.thick { height: 14px; border-radius: 7px; }
.bar.thin { height: 6px; }
.bar .mark { position: absolute; top: -4px; width: 3px; height: 18px; border-radius: 2px; background: var(--ink); }
.pill { display: inline-block; align-self: flex-start; font-size: 12px; font-weight: 700; border-radius: 10px; padding: 6px 10px; }
.pill.warn { background: var(--warn-bg); color: var(--warn-ink); }
.pill.ok { background: var(--ok-bg); color: var(--ok-ink); }
.pill.neutral { background: var(--chip); color: var(--ink2); }
.badge { font-size: 11px; font-weight: 700; color: var(--ink2); background: var(--chip); border-radius: 8px; padding: 3px 7px; white-space: nowrap; }
.delta { min-width: 58px; text-align: center; font-size: 12px; font-weight: 700; border-radius: 7px; padding: 4px 0; background: var(--soft); }

/* controlli */
.seg { display: grid; grid-auto-columns: minmax(0, 1fr); grid-auto-flow: column; gap: 4px; padding: 4px; background: var(--chip); border-radius: 14px; }
.seg button { height: 38px; border: 0; border-radius: 10px; background: transparent; font-size: 13px; font-weight: 700; color: var(--ink2); cursor: pointer; padding: 0 4px; }
.seg button[aria-pressed="true"] { background: var(--card); color: var(--ink); box-shadow: var(--shadow); }
.seg.accent button[aria-pressed="true"] { background: var(--accent); color: var(--on-accent); }
.chips { display: flex; gap: 8px; overflow-x: auto; margin: 0 -18px; padding: 0 18px; scrollbar-width: none; }
.chips.wrap { flex-wrap: wrap; margin: 0; padding: 0; overflow: visible; }
.chip { flex: 0 0 auto; height: 38px; padding: 0 14px; border-radius: 12px; font-size: 14px; font-weight: 600; display: inline-flex; align-items: center; background: var(--card); color: var(--ink); border: 1px solid var(--line); cursor: pointer; }
.chip[aria-pressed="true"] { background: var(--ink); color: var(--bg); border-color: var(--ink); }
.chip.acc-on[aria-pressed="true"] { background: var(--accent); color: var(--on-accent); border-color: var(--accent); }
.chip.dashed { border: 1.5px dashed var(--track); color: var(--ink2); background: transparent; }
.btn { min-height: 52px; border-radius: 16px; border: 0; font-size: 16px; font-weight: 700; cursor: pointer; display: flex; align-items: center; justify-content: center; gap: 8px; padding: 0 18px; width: 100%; }
.btn.primary { background: var(--accent); color: var(--on-accent); }
.btn.soft { background: var(--chip); color: var(--ink); }
.btn.danger { background: transparent; color: var(--out); border: 1px solid var(--line); }
.btn.dashed { background: transparent; border: 1.5px dashed var(--track); color: var(--ink2); font-size: 14px; font-weight: 600; }
.btn.small { min-height: 40px; font-size: 13px; border-radius: 12px; }
.btn:disabled { opacity: .5; }
.toggle { width: 50px; height: 30px; padding: 3px; border-radius: 30px; border: 0; background: var(--track); display: flex; justify-content: flex-start; flex: 0 0 auto; cursor: pointer; }
.toggle::after { content: ""; width: 24px; height: 24px; border-radius: 50%; background: #FFFFFF; box-shadow: 0 1px 3px rgba(0,0,0,.25); }
.toggle[aria-pressed="true"] { background: var(--accent); justify-content: flex-end; }

/* form */
.form { display: flex; flex-direction: column; gap: 16px; }
.fieldset { background: var(--card); border: 1px solid var(--line); border-radius: 20px; padding: 0 16px; }
.field { display: flex; justify-content: space-between; align-items: center; gap: 12px; min-height: 52px; border-bottom: 1px solid var(--line2); font-size: 15px; }
.field:last-child { border-bottom: 0; }
.field > span { color: var(--muted); font-weight: 500; flex: 0 0 auto; }
.field input, .field select { border: 0; background: transparent; text-align: right; font-weight: 600; min-width: 0; flex: 1; padding: 8px 0; outline: none; appearance: none; -webkit-appearance: none; }
.field select { direction: rtl; }
.field input[type="date"] { text-align: right; display: flex; justify-content: flex-end; align-items: center; min-height: 36px; -webkit-appearance: none; appearance: none; }
.field input[type="date"]::-webkit-date-and-time-value { text-align: right; margin: 0; }
.field input[type="date"]::-webkit-datetime-edit { padding: 0; }
input, select, textarea { font-size: 16px; } /* evita lo zoom automatico di iOS sui campi */
.field.col { flex-direction: column; align-items: stretch; gap: 8px; padding: 12px 0; }
.field.col input { text-align: left; }
.box-input { width: 100%; height: 48px; border-radius: 14px; border: 1px solid var(--line); background: var(--card); padding: 0 14px; font-size: 16px; outline: none; }
.box-input:focus, .field input:focus { outline: none; }
.amount { display: flex; flex-direction: column; align-items: center; gap: 6px; padding: 8px 0; }
.amount label { font-size: 13px; font-weight: 600; color: var(--muted); }
.amount input { width: 100%; text-align: center; border: 0; background: transparent; font-size: 48px; font-weight: 800; letter-spacing: -.03em; outline: none; padding: 0; }
.catgrid { display: grid; grid-template-columns: repeat(4, minmax(0, 1fr)); gap: 8px; }
.cat { display: flex; flex-direction: column; align-items: center; gap: 6px; padding: 10px 2px; border-radius: 16px; background: var(--card); border: 1px solid var(--line); cursor: pointer; }
.cat[aria-pressed="true"] { border: 2px solid var(--accent); }
.cat span:last-child { font-size: 11px; font-weight: 600; text-align: center; line-height: 1.2; }
.radio { display: flex; gap: 12px; align-items: flex-start; padding: 14px; border-radius: 16px; background: var(--card); border: 1px solid var(--line); cursor: pointer; text-align: left; width: 100%; }
.radio[aria-pressed="true"] { border: 2px solid var(--accent); }
.radio .ring { width: 20px; height: 20px; border-radius: 50%; border: 2px solid var(--faint); flex: 0 0 auto; margin-top: 1px; }
.radio[aria-pressed="true"] .ring { border: 6px solid var(--accent); }
.radio b { display: block; font-size: 14px; }
.radio small { display: block; font-size: 12px; color: var(--muted); line-height: 1.45; margin-top: 3px; }
.swatches { display: flex; flex-wrap: wrap; gap: 10px; }
.swatch { width: 36px; height: 36px; border-radius: 12px; border: 3px solid transparent; cursor: pointer; }
.swatch[aria-pressed="true"] { border-color: var(--ink); }
.check { display: flex; align-items: center; gap: 12px; min-height: 48px; font-size: 15px; font-weight: 600; cursor: pointer; }
.check input { width: 22px; height: 22px; accent-color: var(--accent); }
.sticky-save { position: fixed; left: 0; right: 0; bottom: 0; padding: 12px 18px calc(env(safe-area-inset-bottom) + 14px); background: var(--bg); border-top: 1px solid var(--line); z-index: 20; }
.sticky-save > div { max-width: 524px; margin: 0 auto; display: flex; gap: 10px; }

/* tabbar */
.tabbar { position: fixed; left: 0; right: 0; bottom: 0; z-index: 30; background: var(--card); border-top: 1px solid var(--line); display: flex; justify-content: center; padding: 8px 0 calc(env(safe-area-inset-bottom) + 6px); }
.tabbar.hidden { display: none; }
.tabbar > div { width: 100%; max-width: 560px; display: flex; justify-content: space-around; align-items: flex-start; }
.tab { display: flex; flex-direction: column; align-items: center; gap: 3px; min-width: 56px; min-height: 44px; font-size: 11px; font-weight: 600; color: var(--muted); }
.tab[aria-current="page"] { color: var(--accent-text); font-weight: 700; }
.fab { width: 56px; height: 56px; margin-top: -26px; border-radius: 20px; background: var(--accent); color: var(--on-accent); display: flex; align-items: center; justify-content: center; box-shadow: 0 8px 20px rgba(15,118,110,.35); }

/* navigatore periodo (analisi) */
.periodnav { position: fixed; left: 0; right: 0; bottom: calc(env(safe-area-inset-bottom) + 64px); z-index: 25; padding: 10px 16px 8px; background: color-mix(in srgb, var(--bg) 94%, transparent); border-top: 1px solid var(--line); }
.periodnav > div { max-width: 528px; margin: 0 auto; display: flex; flex-direction: column; align-items: center; gap: 8px; }
.pn { width: 100%; display: flex; align-items: center; gap: 6px; padding: 4px; background: var(--accent); border-radius: 16px; }
.pn button { border: 0; background: transparent; color: var(--on-accent); width: 44px; height: 44px; display: flex; align-items: center; justify-content: center; cursor: pointer; }
.pn button:disabled { opacity: .35; }
.pn .lbl { flex: 1; width: auto; border-radius: 12px; background: var(--card); color: var(--ink); font-size: 16px; font-weight: 800; gap: 6px; }
.dots { display: flex; gap: 6px; }
.dots i { width: 7px; height: 7px; border-radius: 50%; background: var(--track); }
.dots i.on { background: var(--accent); }
.has-pn #view { padding-bottom: calc(env(safe-area-inset-bottom) + 200px); }

/* tabella rapporti */
.rep-h { display: flex; align-items: center; gap: 8px; padding: 12px 16px; background: var(--chip); font-size: 12px; font-weight: 800; letter-spacing: .06em; }
.rep-h span:first-child { flex: 1; }
.rep-h b { font-size: 14px; letter-spacing: 0; }
.rep-row { display: flex; align-items: center; gap: 8px; padding: 0 16px; min-height: 52px; border: 0; border-bottom: 1px solid var(--line2); background: none; width: 100%; text-align: left; cursor: pointer; }
.rep-row .n { flex: 1; font-size: 15px; font-weight: 600; min-width: 0; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.rep-row .v { font-size: 14px; font-weight: 700; white-space: nowrap; }
.rep-row.sub { padding-left: 36px; min-height: 44px; background: var(--soft); }
.rep-row.sub .n { font-size: 14px; font-weight: 500; }
.chev { transition: transform .15s; color: var(--faint); }
.chev.open { transform: rotate(90deg); }

/* grafici */
.chart { width: 100%; height: auto; overflow: visible; }
.chart text { font-family: var(--font); }
.cols { display: flex; gap: 6px; align-items: flex-end; }
.cols > div { flex: 1; min-width: 0; display: flex; flex-direction: column; align-items: center; gap: 4px; }
.cols .lbl { font-size: 10px; font-weight: 700; color: var(--muted); }
.legend { display: flex; flex-wrap: wrap; gap: 6px 14px; font-size: 12px; font-weight: 600; justify-content: center; }
.legend span { display: flex; align-items: center; gap: 5px; }
.legend i { width: 10px; height: 10px; border-radius: 3px; }

/* foglio inferiore, toast */
.sheet-bg { position: fixed; inset: 0; background: rgba(22,24,29,.45); z-index: 50; display: flex; align-items: flex-end; justify-content: center; animation: fade .15s; }
.sheet { width: 100%; max-width: 560px; max-height: 88vh; overflow-y: auto; background: var(--card); border-radius: 26px 26px 0 0; padding: 10px 18px calc(env(safe-area-inset-bottom) + 22px); display: flex; flex-direction: column; gap: 14px; animation: up .2s ease-out; }
.sheet .grip { align-self: center; width: 40px; height: 5px; border-radius: 3px; background: var(--track); }
.sheet h3 { font-size: 18px; font-weight: 800; }
.sheet .fieldset { background: var(--soft); }
.menu-item { display: flex; justify-content: space-between; align-items: center; min-height: 52px; padding: 0 12px; border-radius: 14px; border: 0; background: transparent; font-size: 16px; font-weight: 700; cursor: pointer; width: 100%; text-align: left; }
.menu-item[aria-pressed="true"] { background: var(--chip); }
.menu-item small { font-size: 12px; font-weight: 600; color: var(--muted); }
@keyframes up { from { transform: translateY(40px); opacity: .4; } }
@keyframes fade { from { opacity: 0; } }
#toast { position: fixed; left: 50%; bottom: calc(env(safe-area-inset-bottom) + 100px); transform: translateX(-50%) translateY(20px); background: var(--ink); color: var(--bg); padding: 12px 18px; border-radius: 14px; font-size: 14px; font-weight: 600; opacity: 0; pointer-events: none; transition: all .2s; z-index: 60; max-width: 90vw; text-align: center; }
#toast.show { opacity: 1; transform: translateX(-50%) translateY(0); }

/* accesso */
.auth { min-height: 80vh; display: flex; flex-direction: column; justify-content: center; gap: 20px; }
.logo { width: 64px; height: 64px; border-radius: 20px; background: var(--accent); display: flex; align-items: center; justify-content: center; color: #fff; }
.empty { text-align: center; color: var(--muted); font-size: 14px; padding: 18px 8px; line-height: 1.5; }
.skeleton { height: 120px; border-radius: var(--r); background: var(--line2); animation: pulse 1.2s infinite; }
@keyframes pulse { 50% { opacity: .5; } }
.sr { position: absolute; width: 1px; height: 1px; overflow: hidden; clip: rect(0 0 0 0); white-space: nowrap; }
.field input[type="checkbox"], .check input[type="checkbox"] { flex: 0 0 auto; width: 22px; height: 22px; padding: 0; -webkit-appearance: checkbox; appearance: auto; accent-color: var(--accent); }
.check.field { justify-content: flex-start; }
/* nel riquadro che scorre gli elementi non devono restringersi (filtri e liste orizzontali sparivano) */
#view > * { flex-shrink: 0; }

/* saldo totale colorato in base alle soglie */
.hero.lvl-red { background: #B42318; color: #FFFFFF; }
.hero.lvl-yellow { background: #F5B301; color: #1F1500; }
.hero.lvl-yellow .mini { background: rgba(0,0,0,.08); }
.hero.lvl-green { background: #15803D; color: #FFFFFF; }
.hero .lvl-tag { align-self: flex-start; font-size: 11px; font-weight: 800; letter-spacing: .04em; text-transform: uppercase; padding: 4px 8px; border-radius: 8px; background: rgba(255,255,255,.18); }
.hero.lvl-yellow .lvl-tag { background: rgba(0,0,0,.1); }
.tip { display: flex; gap: 12px; align-items: flex-start; }
.tip p { margin: 0; font-size: 13px; line-height: 1.5; color: var(--ink2); }
