:root {
  color-scheme: light;
  --bg: #f4f3ef;
  --surface: #fcfcfb;
  --surface-2: #efeee9;
  --line: #e2e0d9;
  --text: #121211;
  --text-2: #52514e;
  --muted: #8a8984;
  --accent: #2a78d6;
  --accent-ink: #ffffff;
  --good: #0f7b45;
  --bad: #c9362f;
  --s1: #2a78d6;   /* entrate */
  --s2: #eb6834;   /* uscite */
  --pos: #2a78d6;
  --neg: #e34948;
  --shadow: 0 1px 2px rgba(0,0,0,.05), 0 4px 16px rgba(0,0,0,.06);
  --radius: 14px;
  --safe-b: env(safe-area-inset-bottom, 0px);
  --safe-t: env(safe-area-inset-top, 0px);
}
@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) {
    color-scheme: dark;
    --bg: #111110;
    --surface: #1a1a19;
    --surface-2: #242422;
    --line: #2f2f2c;
    --text: #ffffff;
    --text-2: #c3c2b7;
    --muted: #8f8e86;
    --accent: #3987e5;
    --good: #3fbf7f;
    --bad: #ef6b63;
    --s1: #3987e5;
    --s2: #d95926;
    --pos: #3987e5;
    --neg: #e66767;
    --shadow: 0 1px 2px rgba(0,0,0,.4);
  }
}
:root[data-theme="dark"] {
  color-scheme: dark;
  --bg: #111110; --surface: #1a1a19; --surface-2: #242422; --line: #2f2f2c;
  --text: #ffffff; --text-2: #c3c2b7; --muted: #8f8e86; --accent: #3987e5;
  --good: #3fbf7f; --bad: #ef6b63; --s1: #3987e5; --s2: #d95926; --pos: #3987e5; --neg: #e66767;
  --shadow: 0 1px 2px rgba(0,0,0,.4);
}

* { box-sizing: border-box; -webkit-tap-highlight-color: transparent; }
html, body { margin: 0; }
body {
  background: var(--bg); color: var(--text);
  font: 16px/1.4 -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, system-ui, sans-serif;
  -webkit-font-smoothing: antialiased;
  min-height: 100vh;
}
button, input, select { font: inherit; color: inherit; }
button { cursor: pointer; }
.num { font-variant-numeric: tabular-nums; }
.hidden { display: none !important; }

/* ---------- struttura */
#app { max-width: 640px; margin: 0 auto; padding: calc(var(--safe-t) + 8px) 16px calc(96px + var(--safe-b)); }
.topbar { display: flex; align-items: center; justify-content: space-between; gap: 8px; padding: 6px 0 12px; position: sticky; top: 0; z-index: 5; background: var(--bg); }
.topbar h1 { font-size: 20px; margin: 0; font-weight: 650; letter-spacing: -.01em; text-transform: capitalize; text-align: center; flex: 1; }
.iconbtn { width: 40px; height: 40px; border-radius: 50%; border: 0; background: var(--surface); box-shadow: var(--shadow); display: grid; place-items: center; font-size: 20px; color: var(--text-2); }
.iconbtn:disabled { opacity: .35; }

.tabbar { position: fixed; left: 0; right: 0; bottom: 0; z-index: 20; display: flex; justify-content: center; background: var(--surface); border-top: 1px solid var(--line); padding-bottom: var(--safe-b); }
.tabbar .inner { display: flex; width: 100%; max-width: 640px; }
.tabbar button { flex: 1; border: 0; background: none; padding: 10px 0 12px; font-size: 12px; color: var(--muted); display: flex; flex-direction: column; align-items: center; gap: 2px; }
.tabbar button svg { width: 24px; height: 24px; }
.tabbar button.on { color: var(--accent); font-weight: 600; }

.fab { position: fixed; right: max(16px, calc(50% - 304px)); bottom: calc(78px + var(--safe-b)); z-index: 21; width: 60px; height: 60px; border-radius: 50%; border: 0; background: var(--accent); color: var(--accent-ink); font-size: 32px; line-height: 1; box-shadow: 0 6px 20px rgba(42,120,214,.4); }

/* ---------- card e liste */
.card { background: var(--surface); border-radius: var(--radius); box-shadow: var(--shadow); margin-bottom: 14px; overflow: hidden; }
.card-h { display: flex; align-items: baseline; justify-content: space-between; padding: 14px 16px 8px; }
.card-h h2 { font-size: 15px; margin: 0; font-weight: 650; }
.card-h .tot { font-weight: 650; color: var(--text-2); }
.card-h .sub { font-size: 13px; color: var(--muted); }
.row { display: flex; align-items: center; gap: 12px; padding: 11px 16px; border-top: 1px solid var(--line); width: 100%; background: none; border-left: 0; border-right: 0; border-bottom: 0; text-align: left; }
.row:active { background: var(--surface-2); }
.row .main { flex: 1; min-width: 0; }
.row .t { font-weight: 550; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.row .d { font-size: 13px; color: var(--muted); overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.row .amt { font-weight: 600; white-space: nowrap; }
.row .amt.inc { color: var(--good); }
.tag { display: inline-block; font-size: 11px; padding: 1px 6px; border-radius: 6px; background: var(--surface-2); color: var(--text-2); margin-left: 6px; vertical-align: 1px; font-weight: 500; }
.linkrow { color: var(--accent); font-weight: 550; justify-content: center; }
.empty { padding: 14px 16px 18px; color: var(--muted); font-size: 14px; border-top: 1px solid var(--line); }

.hero { padding: 18px 16px 16px; }
.hero .lbl { font-size: 13px; color: var(--muted); }
.hero .big { font-size: 38px; font-weight: 700; letter-spacing: -.02em; margin: 2px 0 12px; }
.hero .big.pos { color: var(--good); } .hero .big.neg { color: var(--bad); }
.stats { display: grid; grid-template-columns: 1fr 1fr; gap: 10px; }
.stat { background: var(--surface-2); border-radius: 10px; padding: 10px 12px; }
.stat .lbl { font-size: 12px; }
.stat .v { font-size: 18px; font-weight: 650; }
.stat .x { font-size: 12px; color: var(--muted); }

.filters { display: flex; gap: 6px; padding: 0 16px 10px; overflow-x: auto; scrollbar-width: none; }
.chip { border: 1px solid var(--line); background: var(--surface); border-radius: 999px; padding: 6px 12px; font-size: 14px; white-space: nowrap; color: var(--text-2); }
.chip.on { background: var(--accent); border-color: var(--accent); color: var(--accent-ink); }

/* ---------- foglio (bottom sheet) */
.scrim { position: fixed; inset: 0; background: rgba(0,0,0,.4); z-index: 40; opacity: 0; transition: opacity .2s; }
.scrim.show { opacity: 1; }
.sheet { position: fixed; left: 0; right: 0; bottom: 0; z-index: 41; margin: 0 auto; max-width: 640px; background: var(--surface); border-radius: 18px 18px 0 0; padding: 8px 16px calc(16px + var(--safe-b)); max-height: 92vh; overflow-y: auto; transform: translateY(100%); transition: transform .22s ease-out; }
.sheet.show { transform: none; }
.sheet .grab { width: 40px; height: 5px; border-radius: 3px; background: var(--line); margin: 0 auto 10px; }
.sheet h3 { margin: 0 0 12px; font-size: 18px; }
.field { margin-bottom: 12px; }
.field label { display: block; font-size: 13px; color: var(--text-2); margin-bottom: 4px; }
.field input, .field select { width: 100%; padding: 12px; border: 1px solid var(--line); border-radius: 10px; background: var(--bg); font-size: 16px; }
.field input:focus { outline: 2px solid var(--accent); outline-offset: -1px; }
.field input.amount { font-size: 30px; font-weight: 650; text-align: center; padding: 10px; }
.chips { display: flex; flex-wrap: wrap; gap: 6px; margin-top: 8px; }
.seg { display: flex; background: var(--surface-2); border-radius: 10px; padding: 3px; margin-bottom: 14px; }
.seg button { flex: 1; border: 0; background: none; padding: 8px; border-radius: 8px; font-weight: 550; color: var(--text-2); }
.seg button.on { background: var(--surface); color: var(--text); box-shadow: var(--shadow); }
.radio { display: flex; gap: 10px; align-items: flex-start; padding: 10px 12px; border: 1px solid var(--line); border-radius: 10px; margin-bottom: 8px; }
.radio input { margin-top: 3px; width: 18px; height: 18px; accent-color: var(--accent); }
.radio small { display: block; color: var(--muted); }
.btns { display: flex; gap: 10px; margin-top: 16px; }
.btn { flex: 1; border: 0; border-radius: 12px; padding: 14px; font-weight: 650; background: var(--surface-2); color: var(--text); }
.btn.primary { background: var(--accent); color: var(--accent-ink); }
.btn.danger { background: none; color: var(--bad); flex: 0 0 auto; }
.hint { font-size: 13px; color: var(--muted); margin-top: 6px; }
.subedit { display: flex; gap: 8px; margin-bottom: 6px; }
.subedit input { flex: 1; padding: 10px 12px; border: 1px solid var(--line); border-radius: 10px; background: var(--bg); font-size: 16px; }
.subedit .iconbtn { box-shadow: none; background: var(--surface-2); flex: 0 0 40px; }
.err { color: var(--bad); font-size: 14px; min-height: 18px; margin-top: 6px; }

/* ---------- login */
.login { max-width: 360px; margin: 12vh auto 0; text-align: center; }
.login img { width: 72px; height: 72px; border-radius: 18px; }
.login h1 { margin: 12px 0 4px; font-size: 24px; }
.login p { color: var(--muted); margin: 0 0 22px; }
.login .seg { margin-bottom: 12px; }

/* ---------- grafici */
.chart { position: relative; padding: 0 8px 8px; }
.chart svg { display: block; width: 100%; height: auto; overflow: visible; }
.chart .grid { stroke: var(--line); stroke-width: 1; }
.chart .axis { fill: var(--muted); font-size: 11px; }
.legend { display: flex; gap: 14px; padding: 0 16px 6px; font-size: 13px; color: var(--text-2); }
.legend i { display: inline-block; width: 10px; height: 10px; border-radius: 3px; margin-right: 6px; vertical-align: -1px; }
.tip { position: absolute; pointer-events: none; background: var(--text); color: var(--bg); font-size: 12px; padding: 6px 8px; border-radius: 8px; white-space: nowrap; transform: translate(-50%, -100%); z-index: 3; line-height: 1.35; }
.bar-row { padding: 8px 16px; border-top: 1px solid var(--line); }
.bar-row .top { display: flex; justify-content: space-between; gap: 10px; font-size: 14px; margin-bottom: 5px; }
.bar-row .top span:first-child { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.bar-row .track { height: 8px; border-radius: 4px; background: var(--surface-2); }
.bar-row .subs { display: flex; flex-wrap: wrap; gap: 4px 14px; margin-top: 6px; font-size: 12px; color: var(--muted); }
.bar-row .subs b { color: var(--text-2); font-weight: 600; }
.bar-row .fill { height: 8px; border-radius: 4px; background: var(--s2); }
table.data { width: 100%; border-collapse: collapse; font-size: 13px; }
table.data th, table.data td { padding: 7px 10px; text-align: right; border-top: 1px solid var(--line); }
table.data th:first-child, table.data td:first-child { text-align: left; }
table.data th { color: var(--muted); font-weight: 550; }
details.tbl summary { padding: 10px 16px; color: var(--accent); font-size: 14px; cursor: pointer; border-top: 1px solid var(--line); }

.toast { position: fixed; left: 50%; bottom: calc(150px + var(--safe-b)); transform: translateX(-50%); background: var(--text); color: var(--bg); padding: 10px 16px; border-radius: 10px; font-size: 14px; z-index: 60; opacity: 0; transition: opacity .2s; pointer-events: none; }
.toast.show { opacity: 1; }

/* ---------- calendario mesi */
.mtitle { border: 0; background: none; font: inherit; font-weight: inherit; color: inherit; text-transform: capitalize; padding: 6px 10px; border-radius: 10px; }
.mtitle span { font-size: 13px; color: var(--muted); }
.mtitle:active { background: var(--surface-2); }
.yearnav { display: flex; align-items: center; justify-content: space-between; margin-bottom: 14px; }
.yearnav h3 { margin: 0; font-size: 20px; }
.yearnav .iconbtn { box-shadow: none; background: var(--surface-2); }
.mgrid { display: grid; grid-template-columns: repeat(3, 1fr); gap: 8px; }
.mgrid button { position: relative; border: 1px solid var(--line); background: var(--bg); border-radius: 12px; padding: 16px 0; font-size: 16px; font-weight: 550; text-transform: capitalize; }
.mgrid button.now { border-color: var(--accent); color: var(--accent); }
.mgrid button.sel { background: var(--accent); border-color: var(--accent); color: var(--accent-ink); }
.mgrid button i { position: absolute; left: 50%; bottom: 6px; width: 5px; height: 5px; margin-left: -2.5px; border-radius: 50%; background: currentColor; opacity: .6; }

/* ---------- griglie (su telefono una colonna, come prima) */
.month-grid { display: grid; grid-template-columns: minmax(0, 1fr); grid-template-areas: "hero" "pockets" "spese" "entrate" "clear"; }
.a-hero { grid-area: hero; } .a-pockets { grid-area: pockets; } .a-spese { grid-area: spese; }
.a-entrate { grid-area: entrate; } .a-clear { grid-area: clear; }
.brand, .tabbar .sideadd { display: none; }
.tabletools { display: flex; gap: 8px; padding: 0 16px 12px; }
.tabletools input, .tabletools select { padding: 9px 12px; border: 1px solid var(--line); border-radius: 10px; background: var(--bg); font-size: 14px; }
.tabletools input { flex: 1; min-width: 0; }
table.exp { width: 100%; border-collapse: collapse; font-size: 14px; }
table.exp th { text-align: left; font-weight: 550; font-size: 12px; color: var(--muted); padding: 8px 12px; border-top: 1px solid var(--line); user-select: none; white-space: nowrap; }
table.exp th.sortable { cursor: pointer; } table.exp th.sortable:hover, table.exp th.on { color: var(--text); }
table.exp td { padding: 10px 12px; border-top: 1px solid var(--line); vertical-align: top; }
table.exp td:first-child, table.exp th:first-child { padding-left: 16px; width: 70px; }
table.exp td:last-child, table.exp th:last-child { padding-right: 16px; }
table.exp .r { text-align: right; white-space: nowrap; }
table.exp .muted { color: var(--text-2); }
table.exp tbody tr { cursor: pointer; }
table.exp tbody tr:hover { background: var(--surface-2); }
table.exp tbody tr:focus-visible { outline: 2px solid var(--accent); outline-offset: -2px; }

/* ---------- PC (finestra larga) */
@media (min-width: 960px) {
  body { padding-left: 232px; }
  #app { max-width: 1240px; padding: 20px 32px 48px; }
  .topbar { padding: 4px 0 18px; }
  .topbar h1 { font-size: 24px; }

  .tabbar { top: 0; right: auto; width: 232px; border-top: 0; border-right: 1px solid var(--line); padding: 20px 14px; align-items: flex-start; }
  .tabbar .inner { flex-direction: column; gap: 4px; }
  .tabbar button { flex: 0 0 auto; flex-direction: row; gap: 12px; padding: 10px 12px; font-size: 15px; border-radius: 10px; color: var(--text-2); }
  .tabbar button:hover { background: var(--surface-2); }
  .tabbar button.on { background: var(--surface-2); color: var(--accent); }
  .tabbar button svg { width: 20px; height: 20px; }
  .brand { display: flex; align-items: center; gap: 10px; font-weight: 700; font-size: 17px; padding: 0 8px 18px; }
  .brand img { width: 32px; height: 32px; border-radius: 8px; }
  .tabbar .sideadd { display: block; background: var(--accent); color: var(--accent-ink); font-weight: 650; justify-content: center; margin-bottom: 14px; padding: 12px; }
  .tabbar .sideadd:hover { background: var(--accent); filter: brightness(1.08); }
  .fab { display: none; }

  .month-grid { grid-template-columns: 360px minmax(0, 1fr); column-gap: 20px; align-items: start;
    grid-template-areas: "hero spese" "pockets spese" "entrate spese" "clear spese"; grid-template-rows: auto auto auto 1fr; }
  .a-spese { min-height: 300px; }
  .hero .big { font-size: 34px; }
  .row { padding: 10px 16px; }
  .row:hover { background: var(--surface-2); }

  .two { display: grid; grid-template-columns: minmax(0, 1fr) minmax(0, 1fr); gap: 20px; align-items: start; }
  .settings-grid { display: grid; grid-template-columns: minmax(0, 1fr) minmax(0, 1fr); gap: 0 20px; align-items: start; }

  /* moduli: finestra al centro invece che dal basso */
  .sheet { left: 50%; right: auto; bottom: auto; top: 50%; width: 520px; max-height: 86vh; border-radius: 18px; padding: 22px 24px 20px;
    transform: translate(-50%, -46%); opacity: 0; transition: transform .18s ease-out, opacity .18s; box-shadow: 0 20px 60px rgba(0,0,0,.3); }
  .sheet.show { transform: translate(-50%, -50%); opacity: 1; }
  .sheet .grab { display: none; }
  .toast { bottom: 32px; left: calc(50% + 116px); }
}

/* ---------- saldo pocket */
.pocketrow .amt { text-align: right; }
.pocketrow .amt small { display: block; font-size: 11px; font-weight: 500; color: var(--muted); }
.amt.neg, b.neg { color: var(--bad); }
.ptrack { height: 5px; border-radius: 3px; background: var(--surface-2); margin-top: 6px; max-width: 220px; }
.ptrack div { height: 5px; border-radius: 3px; background: var(--accent); }
.ptag { color: var(--accent); }
.pbal { background: var(--surface-2); border-radius: 12px; padding: 6px 14px; margin-bottom: 12px; }
.pbal div { display: flex; justify-content: space-between; padding: 6px 0; font-size: 15px; }
.pbal div span { color: var(--text-2); }
.pbal .tot { border-top: 1px solid var(--line); margin-top: 4px; padding-top: 10px; font-size: 17px; }
.pbal .tot span { color: var(--text); font-weight: 600; }
