:root {
  --bg: #0f1720;
  --bg-grad: radial-gradient(1200px 600px at 50% -10%, #16232f 0%, #0f1720 60%);
  --surface: #182430;
  --surface-2: #1f2d3b;
  --line: #26374a;
  --text: #e8eef4;
  --muted: #8ba0b3;
  --faint: #5f7488;
  --accent: #34d399;
  --accent-press: #2bb98a;
  --danger: #f87171;
  --danger-bg: #3a1f24;
  --warn: #fbbf24;
  --warn-bg: #362f18;
  --income: #34d399;
  --expense: #fb7185;
  --shadow: 0 8px 24px rgba(0, 0, 0, 0.35);
  --nav-h: 62px;
  --top-safe: env(safe-area-inset-top, 0px);
  --bot-safe: env(safe-area-inset-bottom, 0px);
  color-scheme: dark;
}

* { box-sizing: border-box; -webkit-tap-highlight-color: transparent; }

html, body {
  margin: 0;
  padding: 0;
  background: var(--bg);
  color: var(--text);
  font-family: -apple-system, BlinkMacSystemFont, "SF Pro Text", "Segoe UI", Roboto, system-ui, sans-serif;
  -webkit-font-smoothing: antialiased;
  overscroll-behavior-y: none;
}

body { background: var(--bg-grad); background-attachment: fixed; }

button { font-family: inherit; cursor: pointer; border: none; background: none; color: inherit; }
input, select, textarea { font-family: inherit; font-size: 16px; }

/* ---------- App shell ---------- */
#app { min-height: 100vh; }

.app-header {
  position: sticky;
  top: 0;
  z-index: 20;
  padding: calc(var(--top-safe) + 14px) 18px 12px;
  background: linear-gradient(180deg, rgba(15, 23, 32, 0.96), rgba(15, 23, 32, 0.72) 70%, transparent);
  backdrop-filter: saturate(140%) blur(10px);
}
.app-header h1 { margin: 0; font-size: 26px; font-weight: 700; letter-spacing: -0.02em; }
.app-header .sub { color: var(--muted); font-size: 13px; margin-top: 2px; }

.view {
  padding: 4px 16px calc(var(--nav-h) + var(--bot-safe) + 28px);
  max-width: 720px;
  margin: 0 auto;
  animation: fade 0.22s ease;
}
@keyframes fade { from { opacity: 0; transform: translateY(4px); } to { opacity: 1; transform: none; } }

/* ---------- Cards ---------- */
.card {
  background: var(--surface);
  border: 1px solid var(--line);
  border-radius: 18px;
  padding: 16px;
  margin: 12px 0;
  box-shadow: var(--shadow);
}
.card.tight { padding: 12px 14px; }
.section-title { font-size: 13px; text-transform: uppercase; letter-spacing: 0.08em; color: var(--faint); margin: 22px 4px 6px; font-weight: 600; }

/* ---------- Balance hero ---------- */
.balance {
  background: linear-gradient(135deg, #123, #0e5c46 140%);
  background: linear-gradient(135deg, #12303a, #0f3d33);
  border: 1px solid #1c4a3f;
  text-align: center;
  padding: 22px 16px;
}
.balance .label { color: var(--muted); font-size: 13px; text-transform: uppercase; letter-spacing: 0.08em; }
.balance .amount { font-size: 42px; font-weight: 800; letter-spacing: -0.03em; margin: 6px 0 2px; }
.balance .amount.neg { color: var(--danger); }
.balance .meta { color: var(--muted); font-size: 13px; }

.stat-row { display: flex; gap: 12px; margin: 12px 0; }
.stat {
  flex: 1; background: var(--surface); border: 1px solid var(--line); border-radius: 16px; padding: 14px;
}
.stat .k { font-size: 12px; color: var(--muted); text-transform: uppercase; letter-spacing: 0.06em; }
.stat .v { font-size: 20px; font-weight: 700; margin-top: 4px; }
.v.income { color: var(--income); }
.v.expense { color: var(--expense); }

/* ---------- Alert banners ---------- */
.alert { border-radius: 16px; padding: 14px 16px; margin: 12px 0; display: flex; align-items: center; gap: 12px; }
.alert.overdue { background: var(--danger-bg); border: 1px solid #5b2b31; }
.alert.overdue .icon { font-size: 22px; }
.alert .grow { flex: 1; }
.alert .title { font-weight: 700; }
.alert .desc { font-size: 13px; color: var(--muted); }
.chip-link { color: var(--accent); font-weight: 600; font-size: 14px; }

/* ---------- Bill / txn rows ---------- */
.list { display: flex; flex-direction: column; }
.row {
  display: flex; align-items: center; gap: 12px;
  background: var(--surface); border: 1px solid var(--line);
  border-radius: 14px; padding: 12px 14px; margin: 8px 0;
  width: 100%; text-align: left;
}
.row:active { background: var(--surface-2); }
.row .dot { width: 38px; height: 38px; border-radius: 11px; display: grid; place-items: center; font-size: 18px; flex: none; background: var(--surface-2); }
.row .main { flex: 1; min-width: 0; }
.row .name { font-weight: 600; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.row .sub { font-size: 12.5px; color: var(--muted); margin-top: 2px; }
.row .amt { font-weight: 700; font-variant-numeric: tabular-nums; white-space: nowrap; }
.row .amt.income { color: var(--income); }
.row .amt.expense { color: var(--expense); }

.badge { font-size: 11px; font-weight: 700; padding: 3px 8px; border-radius: 999px; display: inline-block; }
.badge.late { background: var(--danger-bg); color: var(--danger); }
.badge.soon { background: var(--warn-bg); color: var(--warn); }
.badge.ok { background: #16302a; color: var(--accent); }
.badge.paid { background: #1c2a38; color: var(--muted); }

.empty { text-align: center; color: var(--faint); padding: 26px 10px; font-size: 14px; }

/* ---------- Buttons ---------- */
.btn {
  display: inline-flex; align-items: center; justify-content: center; gap: 8px;
  background: var(--accent); color: #06231a; font-weight: 700; font-size: 16px;
  border-radius: 14px; padding: 13px 16px; width: 100%;
}
.btn:active { background: var(--accent-press); }
.btn.secondary { background: var(--surface-2); color: var(--text); border: 1px solid var(--line); }
.btn.danger { background: var(--danger-bg); color: var(--danger); border: 1px solid #5b2b31; }
.btn.small { width: auto; padding: 9px 14px; font-size: 14px; border-radius: 11px; }
.btn-group { display: flex; gap: 10px; margin-top: 12px; }

/* ---------- Bottom nav ---------- */
.nav {
  position: fixed; left: 0; right: 0; bottom: 0; z-index: 30;
  height: calc(var(--nav-h) + var(--bot-safe));
  padding-bottom: var(--bot-safe);
  display: flex; align-items: stretch;
  background: rgba(18, 27, 36, 0.92);
  backdrop-filter: saturate(160%) blur(16px);
  border-top: 1px solid var(--line);
}
.nav button {
  flex: 1; display: flex; flex-direction: column; align-items: center; justify-content: center; gap: 3px;
  color: var(--faint); font-size: 10.5px; font-weight: 600;
}
.nav button .ic { font-size: 21px; line-height: 1; }
.nav button.active { color: var(--accent); }

/* ---------- FAB ---------- */
.fab {
  position: fixed; right: 18px; bottom: calc(var(--nav-h) + var(--bot-safe) + 16px);
  width: 58px; height: 58px; border-radius: 50%; z-index: 31;
  background: var(--accent); color: #06231a; font-size: 30px; font-weight: 300;
  box-shadow: 0 10px 26px rgba(52, 211, 153, 0.4); display: grid; place-items: center;
}
.fab:active { transform: scale(0.94); }

/* ---------- Bottom sheet / modal ---------- */
.scrim {
  position: fixed; inset: 0; z-index: 40; background: rgba(0, 0, 0, 0.55);
  display: flex; align-items: flex-end; justify-content: center;
  animation: fade 0.18s ease;
}
.sheet {
  width: 100%; max-width: 620px;
  background: var(--surface); border: 1px solid var(--line); border-bottom: none;
  border-radius: 22px 22px 0 0;
  padding: 8px 18px calc(var(--bot-safe) + 20px);
  max-height: 92vh; overflow-y: auto;
  animation: slideup 0.24s cubic-bezier(0.2, 0.8, 0.2, 1);
}
@keyframes slideup { from { transform: translateY(100%); } to { transform: none; } }
.sheet .grabber { width: 40px; height: 4px; border-radius: 2px; background: var(--line); margin: 8px auto 12px; }
.sheet h2 { margin: 4px 2px 14px; font-size: 20px; }

.field { margin: 12px 0; }
.field label { display: block; font-size: 13px; color: var(--muted); margin-bottom: 6px; }
.field input, .field select, .field textarea {
  width: 100%; background: var(--surface-2); color: var(--text);
  border: 1px solid var(--line); border-radius: 12px; padding: 12px 13px; outline: none;
}
.field input:focus, .field select:focus, .field textarea:focus { border-color: var(--accent); }
.field.row2 { display: flex; gap: 10px; }
.field.row2 > div { flex: 1; }

.seg { display: flex; background: var(--surface-2); border: 1px solid var(--line); border-radius: 12px; padding: 4px; gap: 4px; }
.seg button { flex: 1; padding: 10px; border-radius: 9px; font-weight: 600; color: var(--muted); }
.seg button.active { background: var(--accent); color: #06231a; }
.seg.expense button.active { background: var(--expense); color: #2a0d13; }

.pill-row { display: flex; flex-wrap: wrap; gap: 8px; margin-top: 4px; }
.pill { background: var(--surface-2); border: 1px solid var(--line); border-radius: 999px; padding: 6px 12px; font-size: 13px; display: inline-flex; align-items: center; gap: 6px; }
.pill button { color: var(--danger); font-weight: 700; }

/* ---------- Login ---------- */
.login-wrap { min-height: 100vh; display: flex; flex-direction: column; align-items: center; justify-content: center; padding: 24px; text-align: center; }
.login-wrap .logo { width: 76px; height: 76px; border-radius: 20px; background: linear-gradient(135deg, #12303a, #0f3d33); border: 1px solid #1c4a3f; display: grid; place-items: center; font-size: 40px; margin-bottom: 18px; }
.login-wrap h1 { margin: 0 0 4px; font-size: 26px; }
.login-wrap p { color: var(--muted); margin: 0 0 22px; }
.login-form { width: 100%; max-width: 340px; }
.error-msg { color: var(--danger); font-size: 14px; min-height: 18px; margin-top: 8px; }

/* ---------- Charts ---------- */
.donut-wrap { display: flex; align-items: center; gap: 16px; }
.donut-wrap svg { flex: none; }
.legend { flex: 1; display: flex; flex-direction: column; gap: 7px; min-width: 0; }
.legend .li { display: flex; align-items: center; gap: 8px; font-size: 13px; }
.legend .sw { width: 11px; height: 11px; border-radius: 3px; flex: none; }
.legend .lname { flex: 1; color: var(--muted); white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.legend .lval { font-variant-numeric: tabular-nums; font-weight: 600; }

.bars { display: flex; align-items: flex-end; gap: 10px; height: 150px; padding-top: 8px; }
.bars .col { flex: 1; display: flex; flex-direction: column; align-items: center; gap: 6px; height: 100%; justify-content: flex-end; }
.bars .pair { display: flex; align-items: flex-end; gap: 3px; height: 100%; width: 100%; justify-content: center; }
.bars .b { width: 12px; border-radius: 4px 4px 0 0; min-height: 2px; }
.bars .b.income { background: var(--income); }
.bars .b.expense { background: var(--expense); }
.bars .lab { font-size: 11px; color: var(--muted); }

.toast {
  position: fixed; left: 50%; transform: translateX(-50%);
  bottom: calc(var(--nav-h) + var(--bot-safe) + 84px); z-index: 60;
  background: #eafff6; color: #06231a; font-weight: 600; font-size: 14px;
  padding: 11px 18px; border-radius: 999px; box-shadow: var(--shadow);
  animation: fade 0.2s ease;
}
.toast.err { background: #ffe9e9; color: #4a1013; }

.muted { color: var(--muted); }
.center { text-align: center; }
.row-actions { display: flex; gap: 8px; margin-top: 6px; }
hr.div { border: none; border-top: 1px solid var(--line); margin: 18px 0; }
a { color: var(--accent); }
