:root {
  --bg: #f6f3ee;
  --surface: #ffffff;
  --surface-2: #efeae2;
  --line: #e2dcd2;
  --text: #1f1c18;
  --muted: #6f675d;
  --faint: #a39a8e;
  --accent: #1f1c18;
  --on-accent: #ffffff;
  --comune: #0f766e;
  --comune-soft: #d7efec;
  --a: #c2410c;
  --a-soft: #fbe3d6;
  --b: #6d28d9;
  --b-soft: #ebe1fb;
  --entrata: #15803d;
  --entrata-soft: #dcf2e3;
  --danger: #b91c1c;
  --radius: 14px;
  --shadow: 0 1px 2px rgba(31, 28, 24, .06), 0 4px 16px rgba(31, 28, 24, .05);
  --nav-h: 64px;
  --safe-b: env(safe-area-inset-bottom, 0px);
  --safe-t: env(safe-area-inset-top, 0px);
  color-scheme: light;
}
@media (prefers-color-scheme: dark) {
  :root {
    --bg: #171513;
    --surface: #211e1b;
    --surface-2: #2b2723;
    --line: #36312c;
    --text: #f1ece5;
    --muted: #b1a89c;
    --faint: #7a7166;
    --accent: #f1ece5;
    --on-accent: #171513;
    --comune: #2dd4bf;
    --comune-soft: #12312e;
    --a: #fb923c;
    --a-soft: #3a2316;
    --b: #a78bfa;
    --b-soft: #2a2142;
    --entrata: #4ade80;
    --entrata-soft: #15301f;
    --danger: #f87171;
    --shadow: none;
    color-scheme: dark;
  }
}

* { box-sizing: border-box; }
html { -webkit-text-size-adjust: 100%; }
body {
  margin: 0;
  background: var(--bg);
  color: var(--text);
  font: 16px/1.45 -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif;
  -webkit-font-smoothing: antialiased;
  -webkit-tap-highlight-color: transparent;
}
button, input, select, textarea { font: inherit; color: inherit; }
button { cursor: pointer; }
.num { font-variant-numeric: tabular-nums; }
[hidden] { display: none !important; }

.splash { min-height: 100vh; display: grid; place-items: center; color: var(--faint); font-weight: 600; letter-spacing: .02em; }

/* ---------- layout */
.top {
  position: sticky; top: 0; z-index: 10;
  padding: calc(var(--safe-t) + 10px) 16px 10px;
  background: color-mix(in srgb, var(--bg) 88%, transparent);
  backdrop-filter: blur(12px); -webkit-backdrop-filter: blur(12px);
  display: flex; align-items: center; gap: 12px;
}
.top h1 { font-size: 19px; margin: 0; flex: 1; letter-spacing: -.01em; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; min-width: 0; }
.saldo-pill {
  border: 1px solid var(--line); background: var(--surface); border-radius: 999px;
  padding: 6px 12px; font-size: 13px; color: var(--muted); max-width: 55%; flex: 0 1 auto;
  white-space: nowrap; overflow: hidden; text-overflow: ellipsis;
}
.saldo-pill b { color: var(--text); font-weight: 600; }
main { max-width: 640px; margin: 0 auto; padding: 4px 16px calc(var(--nav-h) + var(--safe-b) + 96px); }

nav.tabs {
  position: fixed; left: 0; right: 0; bottom: 0; z-index: 20;
  height: calc(var(--nav-h) + var(--safe-b)); padding-bottom: var(--safe-b);
  background: var(--surface); border-top: 1px solid var(--line);
  display: grid; grid-template-columns: repeat(4, 1fr);
}
nav.tabs button {
  background: none; border: 0; display: flex; flex-direction: column; align-items: center; justify-content: center; gap: 3px;
  font-size: 11px; color: var(--faint); font-weight: 500;
}
nav.tabs button svg { width: 24px; height: 24px; }
nav.tabs button[aria-current="page"] { color: var(--text); }

/* ---------- common */
.card { background: var(--surface); border-radius: var(--radius); box-shadow: var(--shadow); padding: 16px; margin: 12px 0; border: 1px solid var(--line); }
.card h2 { font-size: 15px; margin: 0 0 12px; font-weight: 650; }
.card h2 small { color: var(--muted); font-weight: 400; }
.muted { color: var(--muted); }
.faint { color: var(--faint); }
.small { font-size: 13px; }
.row { display: flex; align-items: center; gap: 10px; }
.row.between { justify-content: space-between; }
.grow { flex: 1; min-width: 0; }
.ellipsis { white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.stack > * + * { margin-top: 14px; }

.seg { display: flex; background: var(--surface-2); border-radius: 12px; padding: 3px; gap: 3px; }
.seg button { flex: 1; border: 0; background: none; padding: 9px 6px; border-radius: 9px; font-size: 14px; font-weight: 550; color: var(--muted); }
.seg button[aria-pressed="true"] { background: var(--surface); color: var(--text); box-shadow: 0 1px 3px rgba(0,0,0,.12); }
.seg.sm button { padding: 6px 4px; font-size: 13px; }

.field { display: block; }
.field > span { display: block; font-size: 13px; color: var(--muted); margin-bottom: 6px; font-weight: 500; }
.input, select.input, textarea.input {
  width: 100%; border: 1px solid var(--line); background: var(--surface); border-radius: 12px; padding: 12px 14px;
  outline: none; appearance: none; -webkit-appearance: none; min-height: 48px;
}
.input:focus { border-color: var(--text); }
textarea.input { resize: vertical; min-height: 70px; }

.amount-wrap { display: flex; align-items: baseline; justify-content: center; gap: 6px; padding: 8px 0 2px; }
.amount {
  border: 0; background: none; outline: none; text-align: center; font-size: 46px; font-weight: 650; letter-spacing: -.02em;
  width: 5ch; min-width: 4ch; max-width: 100%; font-variant-numeric: tabular-nums; padding: 0;
}
.amount::placeholder { color: var(--faint); }
.amount-wrap .cur { font-size: 28px; color: var(--muted); font-weight: 500; }

.pick {
  width: 100%; text-align: left; border: 1px solid var(--line); background: var(--surface); border-radius: 12px; padding: 12px 14px;
  display: flex; align-items: center; gap: 12px; min-height: 56px;
}
.pick .ph { color: var(--faint); }
.pick .chev { color: var(--faint); margin-left: auto; }

.chips { display: flex; flex-wrap: wrap; gap: 8px; }
.chip { border: 1px solid var(--line); background: var(--surface); border-radius: 999px; padding: 7px 12px; font-size: 13px; line-height: 1.2; text-align: left; }
.chip[aria-pressed="true"] { background: var(--accent); color: var(--on-accent); border-color: var(--accent); }
.chips.scroll { flex-wrap: nowrap; overflow-x: auto; scrollbar-width: none; margin: 0 -16px; padding: 0 16px; }
.chips.scroll::-webkit-scrollbar { display: none; }
.chips.scroll .chip { flex: 0 0 auto; }

.dot { width: 10px; height: 10px; border-radius: 50%; flex: 0 0 auto; display: inline-block; }
.dot.comune { background: var(--comune); } .dot.a { background: var(--a); } .dot.b { background: var(--b); } .dot.entrata { background: var(--entrata); } .dot.conguaglio { background: var(--faint); }
.tag { display: inline-flex; align-items: center; gap: 5px; font-size: 12px; padding: 2px 8px; border-radius: 999px; font-weight: 550; white-space: nowrap; }
.tag.comune { background: var(--comune-soft); color: var(--comune); }
.tag.a { background: var(--a-soft); color: var(--a); }
.tag.b { background: var(--b-soft); color: var(--b); }
.tag.entrata { background: var(--entrata-soft); color: var(--entrata); }
.tag.conguaglio { background: var(--surface-2); color: var(--muted); }

.btn { border: 0; border-radius: 12px; padding: 14px 18px; font-weight: 600; background: var(--surface-2); color: var(--text); min-height: 48px; }
.btn.primary { background: var(--accent); color: var(--on-accent); }
.btn.danger { background: none; color: var(--danger); border: 1px solid var(--line); }
.btn.ghost { background: none; border: 1px solid var(--line); }
.btn.block { width: 100%; }
.btn.sm { padding: 8px 12px; min-height: 36px; font-size: 14px; border-radius: 10px; }
.btn:disabled { opacity: .45; }
.linkbtn { border: 0; background: none; padding: 4px; color: var(--muted); text-decoration: underline; text-underline-offset: 3px; font-size: 14px; }
.iconbtn { border: 0; background: var(--surface-2); width: 40px; height: 40px; border-radius: 12px; display: grid; place-items: center; flex: 0 0 auto; }
.iconbtn svg { width: 20px; height: 20px; }

.hint { font-size: 13px; color: var(--muted); background: var(--surface-2); border-radius: 10px; padding: 10px 12px; }
.save-bar { position: fixed; left: 0; right: 0; bottom: calc(var(--nav-h) + var(--safe-b)); padding: 10px 16px; background: linear-gradient(transparent, var(--bg) 30%); z-index: 15; }
.save-bar .inner { max-width: 640px; margin: 0 auto; display: flex; gap: 10px; }
.save-bar .btn.primary { flex: 1; font-size: 17px; }

/* ---------- lista movimenti */
.month-nav { display: flex; align-items: center; justify-content: space-between; gap: 8px; margin: 6px 0 10px; }
.month-nav strong { font-size: 17px; text-transform: capitalize; }
.day-h { font-size: 12px; text-transform: uppercase; letter-spacing: .06em; color: var(--faint); margin: 18px 4px 6px; font-weight: 600; }
.list { background: var(--surface); border: 1px solid var(--line); border-radius: var(--radius); overflow: hidden; }
.item { display: flex; align-items: center; gap: 12px; padding: 12px 14px; width: 100%; border: 0; background: none; text-align: left; }
.item + .item { border-top: 1px solid var(--line); }
.item .t { font-weight: 550; }
.item .s { font-size: 12.5px; color: var(--muted); }
.item .amt { font-weight: 650; white-space: nowrap; }
.item .amt.in { color: var(--entrata); }
.empty { text-align: center; color: var(--muted); padding: 40px 16px; }

.sum-grid { display: grid; grid-template-columns: repeat(3, 1fr); gap: 8px; }
.sum { background: var(--surface); border: 1px solid var(--line); border-radius: 12px; padding: 10px 12px; }
.sum .l { font-size: 12px; color: var(--muted); display: flex; align-items: center; gap: 6px; }
.sum .v { font-weight: 650; font-size: 16px; margin-top: 2px; }

/* ---------- report */
.kpi-big { font-size: 34px; font-weight: 700; letter-spacing: -.02em; }
.kpis { display: grid; grid-template-columns: 1fr 1fr; gap: 10px; }
.kpi { border-radius: 12px; padding: 12px; background: var(--surface-2); }
.kpi .l { font-size: 12.5px; color: var(--muted); display: flex; align-items: center; gap: 6px; }
.kpi .v { font-size: 20px; font-weight: 650; margin-top: 2px; }
.kpi .d { font-size: 12px; color: var(--muted); margin-top: 2px; }

.bars { display: flex; align-items: flex-end; gap: 5px; height: 150px; padding-top: 8px; }
.bars .col { flex: 1; display: flex; flex-direction: column; justify-content: flex-end; height: 100%; min-width: 0; position: relative; border: 0; background: none; padding: 0; }
.bars .col .seg-b { width: 100%; }
.bars .col .seg-b:first-child { border-radius: 4px 4px 0 0; }
.bars .col.sel::after { content: ""; position: absolute; inset: -4px -2px -4px; border-radius: 6px; outline: 2px solid var(--text); outline-offset: 0; opacity: .25; }
.bars-x { display: flex; gap: 5px; margin-top: 6px; }
.bars-x span { flex: 1; text-align: center; font-size: 10.5px; color: var(--faint); text-transform: capitalize; min-width: 0; overflow: hidden; }
.legend { display: flex; flex-wrap: wrap; gap: 14px; font-size: 12.5px; color: var(--muted); margin-top: 10px; }
.legend span { display: inline-flex; align-items: center; gap: 6px; }

.hbar { height: 8px; border-radius: 999px; background: var(--surface-2); overflow: hidden; margin-top: 6px; }
.hbar > i { display: block; height: 100%; border-radius: 999px; }
.macro { border: 0; background: none; width: 100%; text-align: left; padding: 10px 0; }
.macro + .macro, .macro-wrap + .macro-wrap { border-top: 1px solid var(--line); }
.voci { padding: 0 0 10px 12px; }
.voci .v { display: flex; justify-content: space-between; gap: 10px; font-size: 14px; padding: 5px 0; color: var(--muted); }
.voci .v b { color: var(--text); font-weight: 550; }

.split { display: flex; height: 26px; border-radius: 8px; overflow: hidden; background: var(--surface-2); font-size: 11.5px; font-weight: 600; }
.split i { display: flex; align-items: center; padding: 0 8px; font-style: normal; white-space: nowrap; overflow: hidden; }
.split .ess { background: var(--text); color: var(--bg); }
.split .dis { background: var(--faint); color: var(--bg); }

.signal { display: flex; gap: 10px; padding: 9px 0; font-size: 14px; }
.signal + .signal { border-top: 1px solid var(--line); }
.signal .ic { width: 24px; height: 24px; border-radius: 8px; display: grid; place-items: center; flex: 0 0 auto; font-weight: 700; font-size: 13px; }
.signal .ic.su { background: var(--a-soft); color: var(--a); }
.signal .ic.giu { background: var(--entrata-soft); color: var(--entrata); }
.signal .ic.nuova { background: var(--b-soft); color: var(--b); }
.signal .ic.info { background: var(--surface-2); color: var(--muted); }

/* ---------- sheet (picker / dialog) */
.sheet { position: fixed; inset: 0; z-index: 50; display: flex; flex-direction: column; justify-content: flex-end; }
.sheet .backdrop { position: absolute; inset: 0; background: rgba(0,0,0,.35); animation: fade .18s ease-out; }
.sheet .panel {
  position: relative; background: var(--bg); border-radius: 20px 20px 0 0; max-height: 92vh; display: flex; flex-direction: column;
  padding-bottom: var(--safe-b); animation: up .22s cubic-bezier(.2,.8,.2,1); width: 100%; max-width: 640px; margin: 0 auto;
}
.sheet .panel.full { height: 92vh; }
.sheet .head { padding: 14px 16px 10px; display: flex; align-items: center; gap: 10px; border-bottom: 1px solid var(--line); }
.sheet .head h3 { margin: 0; font-size: 17px; flex: 1; }
.sheet .body { overflow-y: auto; padding: 12px 16px 24px; -webkit-overflow-scrolling: touch; }
.sheet .grp { font-size: 12px; text-transform: uppercase; letter-spacing: .06em; color: var(--faint); margin: 16px 2px 6px; font-weight: 600; }
.sheet .opt { width: 100%; text-align: left; border: 0; background: var(--surface); padding: 13px 14px; display: flex; gap: 10px; align-items: center; }
.sheet .opts { border: 1px solid var(--line); border-radius: 12px; overflow: hidden; }
.sheet .opt + .opt { border-top: 1px solid var(--line); }
.sheet .opt[aria-selected="true"] { font-weight: 650; }
.sheet .opt .mac { font-size: 12px; color: var(--muted); }
@keyframes up { from { transform: translateY(40px); opacity: .4; } }
@keyframes fade { from { opacity: 0; } }

.toast {
  position: fixed; left: 50%; bottom: calc(var(--nav-h) + var(--safe-b) + 84px); transform: translateX(-50%) translateY(20px);
  background: var(--text); color: var(--bg); padding: 11px 16px; border-radius: 12px; font-size: 14px; font-weight: 550;
  opacity: 0; pointer-events: none; transition: .22s; z-index: 60; max-width: calc(100% - 32px); text-align: center;
}
.toast.show { opacity: 1; transform: translateX(-50%) translateY(0); }
.toast.err { background: var(--danger); color: #fff; }

/* ---------- login */
.login { min-height: 100vh; display: grid; place-items: center; padding: 24px 16px; }
.login form { width: 100%; max-width: 360px; }
.login h1 { font-size: 28px; margin: 0 0 4px; letter-spacing: -.02em; }
.check { display: flex; align-items: center; gap: 10px; font-size: 14px; color: var(--muted); }
.check input { width: 20px; height: 20px; accent-color: var(--text); }

.set-item { display: flex; align-items: center; gap: 12px; padding: 12px 0; }
.set-item + .set-item { border-top: 1px solid var(--line); }
.cat-row { display: flex; align-items: center; gap: 10px; padding: 10px 0; }
.cat-row + .cat-row { border-top: 1px solid var(--line); }
.cat-row.off .t { color: var(--faint); text-decoration: line-through; }

@media (min-width: 700px) {
  .sheet { justify-content: center; }
  .sheet .panel { border-radius: 20px; max-height: 86vh; }
  .sheet .panel.full { height: 86vh; }
}
@media (prefers-reduced-motion: reduce) {
  * { animation: none !important; transition: none !important; }
}
