:root {
  --primary: #1E293B;
  --primary-hover: #334155;
  --bg: #F8FAFC;
  --surface: #F1F5F9;
  --card: #FFFFFF;
  --border: #E2E8F0;
  --text: #0F172A;
  --muted: #64748B;
  --pos: #15803D;
  --pos-bg: #F0FDF4;
  --neg: #B91C1C;
  --neg-bg: #FEF2F2;
  --radius: 14px;
  --safe-b: env(safe-area-inset-bottom, 0px);
  --safe-t: env(safe-area-inset-top, 0px);
  --tabbar-h: 60px;
  --actions-h: 72px;
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif;
  -webkit-text-size-adjust: 100%;
}

* { box-sizing: border-box; }
html, body { margin: 0; background: var(--bg); color: var(--text); }
body { min-height: 100dvh; -webkit-tap-highlight-color: transparent; font-size: 15px; line-height: 1.4; }
button, input, select { font: inherit; color: inherit; }
button { cursor: pointer; }
h1, h2, h3 { margin: 0; font-weight: 650; letter-spacing: -0.01em; }
.muted { color: var(--muted); }
.small { font-size: 13px; }
.num { font-variant-numeric: tabular-nums; white-space: nowrap; }
.pos { color: var(--pos); }
.neg { color: var(--neg); }
.hidden { display: none !important; }

.boot { min-height: 100dvh; display: grid; place-items: center; }
.boot img { border-radius: 14px; opacity: .9; }

/* ---------- struttura ---------- */
.shell { max-width: 640px; margin: 0 auto; min-height: 100dvh; }
.topbar {
  position: sticky; top: 0; z-index: 5;
  display: flex; align-items: center; gap: 10px;
  padding: calc(var(--safe-t) + 10px) 16px 10px;
  background: rgba(248, 250, 252, .92);
  backdrop-filter: saturate(1.4) blur(10px); -webkit-backdrop-filter: saturate(1.4) blur(10px);
  border-bottom: 1px solid var(--border);
}
.topbar img { width: 26px; height: 26px; border-radius: 7px; }
.topbar h1 { font-size: 17px; flex: 1; }
.main { padding: 14px 14px calc(var(--tabbar-h) + var(--actions-h) + var(--safe-b) + 18px); display: grid; gap: 12px; }
.main.no-actions { padding-bottom: calc(var(--tabbar-h) + var(--safe-b) + 18px); }

.card { background: var(--card); border: 1px solid var(--border); border-radius: var(--radius); padding: 14px; }
.card-head { display: flex; align-items: center; justify-content: space-between; gap: 10px; margin-bottom: 8px; }
.card h3 { font-size: 15px; }
.mb { margin-bottom: 6px; }
.hint { font-size: 12.5px; color: var(--muted); margin: 8px 0 0; }

/* hero totali */
.hero { padding: 16px; }
.hero .label { font-size: 13px; color: var(--muted); }
.hero .big { font-size: 32px; font-weight: 700; letter-spacing: -0.02em; margin: 2px 0 6px; }
.stats { display: grid; grid-template-columns: repeat(3, 1fr); gap: 8px; margin-top: 10px; }
.stat { background: var(--surface); border-radius: 10px; padding: 8px 10px; }
.stat b { display: block; font-size: 15px; }
.stat span { font-size: 12px; color: var(--muted); }

/* chi deve a chi */
.debt { display: flex; align-items: center; gap: 10px; padding: 10px 0; border-top: 1px solid var(--border); }
.debt:first-of-type { border-top: 0; }
.debt .who { flex: 1; min-width: 0; }
.debt .who b { font-weight: 600; }
.debt .arrow { color: var(--muted); margin: 0 4px; }
.debt .amt { font-weight: 650; }
.allgood { display: flex; align-items: center; gap: 8px; padding: 10px 12px; border-radius: 10px; background: var(--pos-bg); color: var(--pos); font-weight: 550; }

/* persone */
.person { display: flex; align-items: center; gap: 12px; padding: 11px 0; border-top: 1px solid var(--border); }
.person:first-of-type { border-top: 0; }
.avatar { width: 36px; height: 36px; flex: none; border-radius: 50%; display: grid; place-items: center; background: var(--surface); color: var(--primary); font-weight: 650; font-size: 14px; border: 1px solid var(--border); }
.person .info { flex: 1; min-width: 0; }
.person .name { font-weight: 600; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.person .detail { font-size: 12.5px; color: var(--muted); }
.bal { text-align: right; font-weight: 700; }
.bal small { display: block; font-weight: 500; font-size: 11.5px; color: var(--muted); }

/* movimenti */
.filters { display: flex; gap: 6px; overflow-x: auto; }
.day { font-size: 12.5px; color: var(--muted); font-weight: 600; text-transform: capitalize; margin: 6px 4px -4px; }
.list { background: var(--card); border: 1px solid var(--border); border-radius: var(--radius); overflow: hidden; }
.mov { width: 100%; display: flex; align-items: center; gap: 12px; padding: 12px 14px; border: 0; border-top: 1px solid var(--border); background: none; text-align: left; }
.mov:first-child { border-top: 0; }
.mov:active { background: var(--surface); }
.mov-icon { width: 36px; height: 36px; flex: none; border-radius: 10px; display: grid; place-items: center; font-weight: 700; font-size: 15px; }
.mov-icon.exp { background: var(--surface); color: var(--primary); }
.mov-icon.tr { background: var(--pos-bg); color: var(--pos); }
.mov-main { flex: 1; min-width: 0; }
.mov-title { font-weight: 600; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.mov-sub { font-size: 12.5px; color: var(--muted); overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.mov-amt { font-weight: 650; }
.empty { text-align: center; padding: 28px 16px; color: var(--muted); }

/* ---------- controlli ---------- */
.btn {
  display: inline-flex; align-items: center; justify-content: center; gap: 8px;
  min-height: 48px; padding: 0 18px; border-radius: 12px; border: 1px solid transparent;
  font-weight: 600; font-size: 15.5px; text-decoration: none; user-select: none;
}
.btn.primary { background: var(--primary); color: #fff; }
.btn.primary:active, .btn.primary:hover { background: var(--primary-hover); }
.btn.outline { background: var(--card); border-color: var(--border); color: var(--primary); }
.btn.ghost { background: transparent; color: var(--primary); }
.btn.danger { background: var(--card); border-color: #FECACA; color: var(--neg); }
.btn.sm { min-height: 36px; padding: 0 12px; font-size: 13.5px; border-radius: 10px; }
.btn.block { width: 100%; }
.btn:disabled { opacity: .5; cursor: default; }
.icon-btn { width: 40px; height: 40px; border-radius: 10px; border: 1px solid var(--border); background: var(--card); display: grid; place-items: center; color: var(--muted); font-size: 16px; flex: none; }

.chips { display: flex; flex-wrap: wrap; gap: 8px; }
.chip {
  min-height: 40px; padding: 0 14px; border-radius: 999px; border: 1px solid var(--border);
  background: var(--card); font-weight: 550; font-size: 14.5px; color: var(--text);
}
.chip.on { background: var(--primary); border-color: var(--primary); color: #fff; }
.chip:disabled { opacity: .35; }
.chip.tiny { min-height: 32px; padding: 0 12px; font-size: 13px; }

.seg { display: inline-flex; background: var(--surface); border-radius: 10px; padding: 3px; gap: 2px; }
.seg button { border: 0; background: none; padding: 7px 12px; border-radius: 8px; font-size: 13.5px; font-weight: 550; color: var(--muted); min-height: 34px; }
.seg button.on { background: var(--card); color: var(--text); box-shadow: 0 1px 2px rgba(15, 23, 42, .08); }
.seg.full { display: flex; }
.seg.full button { flex: 1; }

.field { display: grid; gap: 6px; }
.field > span, .label-line { font-size: 13px; font-weight: 600; color: var(--muted); }
input[type=text], input[type=password], input[type=date], input:not([type]), .input {
  width: 100%; min-height: 48px; padding: 0 14px; border-radius: 12px; border: 1px solid var(--border);
  background: var(--card); font-size: 16px; outline: none; -webkit-appearance: none; appearance: none;
}
input[type=date] { display: block; line-height: 46px; }
input:focus { border-color: var(--primary); box-shadow: 0 0 0 3px rgba(30, 41, 59, .12); }
.amount-input { position: relative; }
.amount-input input { font-size: 22px; font-weight: 650; padding-right: 36px; font-variant-numeric: tabular-nums; }
.amount-input i { position: absolute; right: 14px; top: 50%; transform: translateY(-50%); font-style: normal; color: var(--muted); font-weight: 600; }
.row2 { display: grid; grid-template-columns: 1fr 1fr; gap: 10px; }
.form { display: grid; gap: 14px; }
.error-text { color: var(--neg); font-size: 13.5px; min-height: 1em; }

/* ---------- barra azioni + tab ---------- */
.actionbar {
  position: fixed; left: 0; right: 0; bottom: calc(var(--tabbar-h) + var(--safe-b)); z-index: 6;
  display: flex; justify-content: center; padding: 10px 14px; pointer-events: none;
}
.actionbar .inner { pointer-events: auto; display: grid; grid-template-columns: 1.3fr 1fr; gap: 10px; width: 100%; max-width: 612px; }
.actionbar .btn { min-height: 52px; box-shadow: 0 6px 18px rgba(15, 23, 42, .14); }
.tabbar {
  position: fixed; left: 0; right: 0; bottom: 0; z-index: 7;
  background: rgba(255, 255, 255, .96); border-top: 1px solid var(--border);
  padding-bottom: var(--safe-b);
  backdrop-filter: blur(10px); -webkit-backdrop-filter: blur(10px);
}
.tabbar .inner { max-width: 640px; margin: 0 auto; display: grid; grid-template-columns: repeat(3, 1fr); height: var(--tabbar-h); }
.tab { border: 0; background: none; display: grid; place-items: center; align-content: center; gap: 2px; color: var(--muted); font-size: 11.5px; font-weight: 600; }
.tab svg { width: 22px; height: 22px; }
.tab.on { color: var(--primary); }

/* ---------- foglio modale ---------- */
.sheet-backdrop { position: fixed; inset: 0; z-index: 20; background: rgba(15, 23, 42, .38); display: flex; align-items: flex-end; justify-content: center; animation: fade .15s ease-out; }
.sheet {
  width: 100%; max-width: 640px; max-height: 94dvh; overflow-y: auto; overscroll-behavior: contain;
  background: var(--bg); border-radius: 18px 18px 0 0; padding: 8px 16px calc(16px + var(--safe-b));
  animation: up .2s ease-out;
}
.sheet .grab { width: 38px; height: 4px; border-radius: 2px; background: #CBD5E1; margin: 0 auto 10px; }
.sheet-head { display: flex; align-items: center; justify-content: space-between; margin-bottom: 14px; }
.sheet-head h2 { font-size: 18px; }
.sheet-actions { display: flex; gap: 10px; margin-top: 4px; position: sticky; bottom: calc(-16px - var(--safe-b)); background: var(--bg); padding: 10px 0 calc(6px + var(--safe-b)); }
.sheet-actions .btn.primary { flex: 1; }
.preview { font-size: 13.5px; color: var(--muted); background: var(--surface); border-radius: 10px; padding: 9px 12px; }
.preview b { color: var(--text); }
.custom-row { display: grid; grid-template-columns: 1fr 130px; gap: 10px; align-items: center; }
.custom-row .input { min-height: 44px; text-align: right; font-variant-numeric: tabular-nums; }
.suggest { display: flex; align-items: center; justify-content: space-between; gap: 8px; }
@media (min-width: 700px) {
  .sheet-backdrop { align-items: center; }
  .sheet { border-radius: 18px; max-height: 88vh; }
}
@keyframes up { from { transform: translateY(24px); opacity: .6; } to { transform: none; opacity: 1; } }
@keyframes fade { from { opacity: 0; } to { opacity: 1; } }

/* ---------- impostazioni ---------- */
.person-edit { display: flex; gap: 8px; align-items: center; padding: 6px 0; }
.person-edit input { flex: 1; }
.settings-list { display: grid; gap: 10px; }
.archived { display: flex; align-items: center; justify-content: space-between; padding: 8px 0; border-top: 1px solid var(--border); }

/* ---------- login ---------- */
.login { min-height: 100dvh; display: grid; place-items: center; padding: 24px; }
.login .box { width: 100%; max-width: 360px; display: grid; gap: 16px; text-align: center; }
.login img { width: 64px; height: 64px; border-radius: 16px; margin: 0 auto; }
.login h1 { font-size: 22px; }

/* ---------- toast ---------- */
#toast {
  position: fixed; left: 50%; bottom: calc(var(--tabbar-h) + var(--actions-h) + var(--safe-b) + 16px); transform: translate(-50%, 20px);
  background: var(--primary); color: #fff; padding: 10px 16px; border-radius: 12px; font-size: 14px; font-weight: 550;
  opacity: 0; pointer-events: none; transition: all .2s ease; z-index: 30; max-width: 90vw; text-align: center;
}
#toast.show { opacity: 1; transform: translate(-50%, 0); }
#toast.err { background: var(--neg); }
