:root {
  --bg: #f4f5f7;
  --surface: #ffffff;
  --surface-2: #f0f2f5;
  --text: #15181e;
  --muted: #667085;
  --line: #e3e6eb;
  --primary: #2563eb;
  --primary-ink: #ffffff;
  --ok: #16a34a;
  --warn: #d97706;
  --bad: #dc2626;
  --shadow: 0 1px 2px rgba(16, 24, 40, .06), 0 1px 3px rgba(16, 24, 40, .08);
  --radius: 14px;
  --nav-h: 64px;
  color-scheme: light;
}
@media (prefers-color-scheme: dark) {
  :root {
    --bg: #0f1115;
    --surface: #181b21;
    --surface-2: #20242c;
    --text: #eef0f3;
    --muted: #98a2b3;
    --line: #2a2f38;
    --primary: #4f8cff;
    --ok: #22c55e;
    --warn: #f59e0b;
    --bad: #f87171;
    --shadow: none;
    color-scheme: dark;
  }
}

* { box-sizing: border-box; }
html, body { margin: 0; }
body {
  background: var(--bg);
  color: var(--text);
  font: 15px/1.45 system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
  -webkit-tap-highlight-color: transparent;
  min-height: 100dvh;
}
button, input, select { font: inherit; color: inherit; }
button { cursor: pointer; }
.num { font-variant-numeric: tabular-nums; white-space: nowrap; }
.muted { color: var(--muted); }
.small { font-size: 13px; }
.hidden { display: none !important; }

/* ---------- Layout ---------- */
.screen {
  max-width: 640px;
  margin: 0 auto;
  padding: calc(env(safe-area-inset-top) + 12px) 16px calc(var(--nav-h) + env(safe-area-inset-bottom) + 88px);
}
.topbar { display: flex; align-items: center; justify-content: space-between; gap: 8px; margin-bottom: 12px; }
.topbar h1 { font-size: 20px; margin: 0; }
.hh-name { font-size: 13px; color: var(--muted); }

.month-nav { display: flex; align-items: center; justify-content: space-between; background: var(--surface); border-radius: var(--radius); box-shadow: var(--shadow); padding: 4px; margin-bottom: 12px; }
.month-nav .label { font-weight: 600; text-transform: capitalize; }
.icon-btn { background: none; border: 0; width: 44px; height: 44px; border-radius: 10px; font-size: 22px; line-height: 1; }
.icon-btn:active { background: var(--surface-2); }

.card { background: var(--surface); border-radius: var(--radius); box-shadow: var(--shadow); padding: 14px; margin-bottom: 12px; }
.card h2 { font-size: 15px; margin: 0 0 10px; display: flex; align-items: center; gap: 8px; }
.card h3 { font-size: 13px; margin: 14px 0 6px; color: var(--muted); text-transform: uppercase; letter-spacing: .04em; }
.dot { width: 10px; height: 10px; border-radius: 50%; display: inline-block; flex: none; }

.kpis { display: grid; grid-template-columns: repeat(3, 1fr); gap: 8px; }
.kpi { background: var(--surface-2); border-radius: 10px; padding: 8px 10px; min-width: 0; }
.kpi .k { font-size: 12px; color: var(--muted); }
.kpi .v { font-weight: 700; font-size: 16px; overflow: hidden; text-overflow: ellipsis; }

.bar { height: 8px; background: var(--surface-2); border-radius: 99px; overflow: hidden; margin: 8px 0 4px; }
.bar > span { display: block; height: 100%; border-radius: 99px; background: var(--ok); }
.bar.warn > span { background: var(--warn); }
.bar.bad > span { background: var(--bad); }
.bar.thin { height: 5px; margin: 4px 0 0; }

.acc-head { display: flex; justify-content: space-between; align-items: baseline; gap: 8px; width: 100%; background: none; border: 0; padding: 0; text-align: left; }
.acc-head .t { font-weight: 600; display: flex; align-items: center; gap: 8px; }
.acc-sub { display: flex; justify-content: space-between; font-size: 13px; color: var(--muted); }
.pos { color: var(--ok); }
.neg { color: var(--bad); }

.lines { margin-top: 10px; border-top: 1px solid var(--line); }
.line { padding: 8px 0; border-bottom: 1px solid var(--line); }
.line:last-child { border-bottom: 0; }
.line .row { display: flex; justify-content: space-between; gap: 8px; }
.line .name { min-width: 0; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }

table.recap { width: 100%; border-collapse: collapse; font-size: 14px; }
table.recap th, table.recap td { padding: 6px 4px; text-align: right; border-bottom: 1px solid var(--line); }
table.recap th:first-child, table.recap td:first-child { text-align: left; }
table.recap thead th { font-size: 12px; color: var(--muted); font-weight: 600; }

/* ---------- Expenses list ---------- */
.chips { display: flex; gap: 6px; overflow-x: auto; padding-bottom: 4px; margin-bottom: 10px; scrollbar-width: none; }
.chip { flex: none; border: 1px solid var(--line); background: var(--surface); border-radius: 99px; padding: 6px 12px; font-size: 14px; display: inline-flex; align-items: center; gap: 6px; }
.chip.on { background: var(--text); color: var(--bg); border-color: var(--text); }
.day { font-size: 12px; font-weight: 600; color: var(--muted); text-transform: capitalize; margin: 14px 4px 6px; }
.exp { display: flex; align-items: center; gap: 10px; width: 100%; background: var(--surface); border: 0; border-radius: 12px; padding: 10px 12px; margin-bottom: 6px; box-shadow: var(--shadow); text-align: left; }
.exp .main { flex: 1; min-width: 0; }
.exp .title { font-weight: 600; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.exp .meta { font-size: 12px; color: var(--muted); overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.exp .amt { font-weight: 700; }
.badge { font-size: 11px; background: var(--surface-2); color: var(--muted); border-radius: 6px; padding: 1px 6px; margin-left: 4px; }

.banner { display: flex; align-items: center; gap: 10px; background: color-mix(in srgb, var(--primary) 12%, var(--surface)); border: 1px solid color-mix(in srgb, var(--primary) 30%, transparent); border-radius: var(--radius); padding: 12px; margin-bottom: 12px; }
.banner .txt { flex: 1; font-size: 14px; }

.empty { text-align: center; color: var(--muted); padding: 40px 16px; }

/* ---------- Forms ---------- */
label.f { display: block; margin-bottom: 12px; }
label.f > span { display: block; font-size: 13px; color: var(--muted); margin-bottom: 4px; }
.input, select.input {
  width: 100%; height: 46px; padding: 0 12px; border-radius: 10px;
  border: 1px solid var(--line); background: var(--surface); outline: none;
}
.input:focus { border-color: var(--primary); box-shadow: 0 0 0 3px color-mix(in srgb, var(--primary) 20%, transparent); }
.input.big { height: 64px; font-size: 30px; font-weight: 700; text-align: center; }
.btn { height: 46px; padding: 0 16px; border-radius: 10px; border: 1px solid var(--line); background: var(--surface); font-weight: 600; }
.btn.primary { background: var(--primary); color: var(--primary-ink); border-color: var(--primary); }
.btn.danger { color: var(--bad); }
.btn.block { width: 100%; }
.btn.sm { height: 36px; padding: 0 12px; font-size: 14px; }
.btn:disabled { opacity: .5; }
.row-btns { display: flex; gap: 8px; }
.row-btns > * { flex: 1; }
.seg { display: flex; gap: 6px; flex-wrap: wrap; }
.seg button { flex: 1 1 30%; min-height: 42px; border-radius: 10px; border: 1px solid var(--line); background: var(--surface); font-size: 14px; display: inline-flex; align-items: center; justify-content: center; gap: 6px; padding: 4px 8px; }
.seg button.on { border-color: var(--primary); background: color-mix(in srgb, var(--primary) 12%, var(--surface)); font-weight: 600; }
.err { color: var(--bad); font-size: 14px; margin: 8px 0; min-height: 1em; }

/* ---------- Budget editor ---------- */
.bud-row { display: grid; grid-template-columns: 1fr 104px 40px 36px; gap: 6px; align-items: center; margin-bottom: 6px; }
.bud-row .input { height: 40px; padding: 0 10px; }
.bud-row .amount { text-align: right; }
.toggle { width: 40px; height: 40px; border-radius: 10px; border: 1px solid var(--line); background: var(--surface); font-size: 16px; opacity: .45; }
.toggle.on { opacity: 1; border-color: var(--primary); background: color-mix(in srgb, var(--primary) 12%, var(--surface)); }
.del { width: 36px; height: 40px; border: 0; background: none; color: var(--muted); font-size: 20px; }
.bud-total { display: flex; justify-content: space-between; font-weight: 700; padding-top: 6px; border-top: 1px solid var(--line); margin-top: 6px; }
.add-link { background: none; border: 0; color: var(--primary); font-weight: 600; padding: 8px 0; }
.param-row { display: grid; grid-template-columns: 1fr 110px 80px; gap: 6px; align-items: center; margin-bottom: 6px; }
.param-row .input { height: 40px; text-align: right; }
.savebar {
  position: fixed; left: 0; right: 0; bottom: calc(var(--nav-h) + env(safe-area-inset-bottom));
  background: var(--surface); border-top: 1px solid var(--line); padding: 10px 16px; z-index: 20;
  box-shadow: 0 -4px 12px rgba(0,0,0,.08);
}
.savebar .inner { max-width: 640px; margin: 0 auto; display: flex; gap: 8px; align-items: center; flex-wrap: wrap; }
.savebar select { flex: 1; min-width: 150px; height: 40px; }
.savebar .inner > .btn { flex: 1; }

/* ---------- Nav / FAB ---------- */
nav.tabs {
  position: fixed; left: 0; right: 0; bottom: 0; z-index: 30;
  height: calc(var(--nav-h) + env(safe-area-inset-bottom)); padding-bottom: env(safe-area-inset-bottom);
  background: var(--surface); border-top: 1px solid var(--line); display: flex;
}
nav.tabs button { flex: 1; background: none; border: 0; display: flex; flex-direction: column; align-items: center; justify-content: center; gap: 2px; font-size: 11px; color: var(--muted); }
nav.tabs button .ico { font-size: 20px; line-height: 1; }
nav.tabs button.on { color: var(--primary); font-weight: 600; }
.fab {
  position: fixed; right: max(16px, calc(50% - 304px)); bottom: calc(var(--nav-h) + env(safe-area-inset-bottom) + 16px); z-index: 25;
  width: 58px; height: 58px; border-radius: 50%; border: 0; background: var(--primary); color: #fff; font-size: 30px; box-shadow: 0 6px 16px rgba(37, 99, 235, .4);
}

/* ---------- Sheet / dialog ---------- */
.overlay { position: fixed; inset: 0; background: rgba(0,0,0,.45); z-index: 50; display: flex; align-items: flex-end; justify-content: center; }
.sheet { background: var(--bg); width: 100%; max-width: 640px; max-height: 92dvh; overflow: auto; border-radius: 18px 18px 0 0; padding: 16px 16px calc(16px + env(safe-area-inset-bottom)); }
.sheet .grab { width: 40px; height: 4px; border-radius: 4px; background: var(--line); margin: 0 auto 12px; }
.sheet h2 { margin: 0 0 12px; font-size: 18px; }
.overlay.center { align-items: center; padding: 16px; }
.dialog { background: var(--surface); border-radius: 16px; padding: 18px; max-width: 400px; width: 100%; }
.dialog p { margin: 0 0 16px; }

#toast { position: fixed; left: 50%; transform: translateX(-50%); bottom: calc(var(--nav-h) + env(safe-area-inset-bottom) + 90px); z-index: 60; background: var(--text); color: var(--bg); padding: 10px 16px; border-radius: 10px; font-size: 14px; opacity: 0; pointer-events: none; transition: opacity .2s; max-width: calc(100% - 32px); }
#toast.show { opacity: 1; }
#toast.bad { background: var(--bad); color: #fff; }

/* ---------- Auth ---------- */
.auth { max-width: 400px; margin: 0 auto; padding: calc(env(safe-area-inset-top) + 48px) 16px 32px; }
.auth .logo { width: 64px; height: 64px; border-radius: 16px; display: block; margin: 0 auto 12px; }
.auth h1 { text-align: center; margin: 0 0 4px; font-size: 24px; }
.auth .sub { text-align: center; color: var(--muted); margin: 0 0 24px; }
.linkbtn { background: none; border: 0; color: var(--primary); padding: 8px 0; font-size: 14px; }

.splash { min-height: 100dvh; display: grid; place-items: center; }
.spinner { width: 32px; height: 32px; border: 3px solid var(--line); border-top-color: var(--primary); border-radius: 50%; animation: spin .8s linear infinite; }
@keyframes spin { to { transform: rotate(360deg); } }

.code { font: 700 24px/1 ui-monospace, SFMono-Regular, Menlo, monospace; letter-spacing: .15em; background: var(--surface-2); border-radius: 10px; padding: 12px; text-align: center; margin-bottom: 8px; user-select: all; }
.set-row { display: flex; gap: 8px; align-items: center; margin-bottom: 8px; }
.set-row .input { flex: 1; height: 40px; }
input[type=color] { width: 40px; height: 40px; padding: 2px; border: 1px solid var(--line); border-radius: 10px; background: var(--surface); }
