:root {
  --bg: #f4f6f8;
  --surface: #ffffff;
  --surface-2: #f1f4f7;
  --border: #e3e8ee;
  --text: #0f1b2a;
  --text-2: #4a5a6d;
  --muted: #7b8a9c;
  --accent: #1f6feb;
  --accent-ink: #ffffff;
  --accent-soft: #e7f0ff;
  --debit: #c2410c;
  --credit: #047857;
  --warn-bg: #fff7e6;
  --warn-ink: #8a5300;
  --danger: #dc2626;
  --bar: #3b82f6;
  --bar-track: #eaeff5;
  --shadow: 0 1px 2px rgba(15, 27, 42, .06), 0 4px 16px rgba(15, 27, 42, .06);
  --radius: 16px;
  --nav-h: 64px;
  --safe-b: env(safe-area-inset-bottom, 0px);
  color-scheme: light;
}
@media (prefers-color-scheme: dark) {
  :root {
    --bg: #0c1117;
    --surface: #151c25;
    --surface-2: #1c2531;
    --border: #283241;
    --text: #e8eef5;
    --text-2: #aab6c4;
    --muted: #7f8c9c;
    --accent: #4c8dff;
    --accent-ink: #06111f;
    --accent-soft: #16263f;
    --debit: #fb923c;
    --credit: #34d399;
    --warn-bg: #2b2210;
    --warn-ink: #f5c26b;
    --bar: #60a5fa;
    --bar-track: #232e3c;
    --shadow: 0 1px 2px rgba(0, 0, 0, .3);
    color-scheme: dark;
  }
}

* { box-sizing: border-box; -webkit-tap-highlight-color: transparent; }
html, body { margin: 0; }
body {
  font: 15px/1.45 -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif;
  background: var(--bg);
  color: var(--text);
  min-height: 100dvh;
  overscroll-behavior-y: none;
}
button, input, select, textarea { font: inherit; color: inherit; }
a { color: var(--accent); text-decoration: none; }
h1, h2, h3 { margin: 0; line-height: 1.2; }
.hidden { display: none !important; }
.muted { color: var(--muted); }
.small { font-size: 13px; }
.tiny { font-size: 12px; }
.num { font-variant-numeric: tabular-nums; }
.debit { color: var(--debit); }
.credit { color: var(--credit); }
.row { display: flex; align-items: center; gap: 10px; }
.grow { flex: 1; min-width: 0; }
.ellipsis { white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.stack > * + * { margin-top: 12px; }

/* ------------------------------------------------ app shell */
.topbar {
  position: sticky; top: 0; z-index: 20;
  display: flex; align-items: center; gap: 8px;
  padding: calc(env(safe-area-inset-top, 0px) + 10px) 16px 10px;
  background: color-mix(in srgb, var(--bg) 88%, transparent);
  backdrop-filter: blur(12px); -webkit-backdrop-filter: blur(12px);
}
.topbar h1 { font-size: 20px; font-weight: 700; flex: 1; min-width: 0; }
.avatar {
  width: 36px; height: 36px; border-radius: 50%; border: 0;
  display: grid; place-items: center; font-weight: 700; color: #fff; cursor: pointer; flex: none;
}
main { padding: 4px 16px calc(var(--nav-h) + var(--safe-b) + 90px); max-width: 720px; margin: 0 auto; }

.bottomnav {
  position: fixed; left: 0; right: 0; bottom: 0; z-index: 30;
  display: grid; grid-template-columns: repeat(5, 1fr);
  height: calc(var(--nav-h) + var(--safe-b)); padding-bottom: var(--safe-b);
  background: var(--surface); border-top: 1px solid var(--border);
}
.bottomnav a {
  display: flex; flex-direction: column; align-items: center; justify-content: center; gap: 2px;
  color: var(--muted); font-size: 11px; font-weight: 600; position: relative;
}
.bottomnav a .ic { font-size: 21px; line-height: 1; filter: grayscale(1); opacity: .75; }
.bottomnav a.active { color: var(--accent); }
.bottomnav a.active .ic { filter: none; opacity: 1; }
.badge {
  position: absolute; top: 7px; left: calc(50% + 6px);
  min-width: 18px; height: 18px; padding: 0 5px; border-radius: 9px;
  background: var(--danger); color: #fff; font-size: 11px; line-height: 18px; text-align: center;
}
.fab {
  position: fixed; right: 18px; bottom: calc(var(--nav-h) + var(--safe-b) + 16px); z-index: 25;
  width: 56px; height: 56px; border-radius: 18px; border: 0;
  background: var(--accent); color: var(--accent-ink); font-size: 30px; line-height: 1;
  box-shadow: 0 6px 20px rgba(31, 111, 235, .35); cursor: pointer;
}

/* ------------------------------------------------ cards & lists */
.card { background: var(--surface); border-radius: var(--radius); box-shadow: var(--shadow); padding: 16px; }
.card + .card, .card + .section, .section + .card { margin-top: 14px; }
.section { margin-top: 22px; }
.section-title { display: flex; align-items: baseline; justify-content: space-between; margin: 0 4px 8px; }
.section-title h2 { font-size: 15px; font-weight: 700; color: var(--text-2); }

.summary { display: grid; grid-template-columns: 1fr 1fr; gap: 10px; }
.summary .big { grid-column: 1 / -1; }
.stat .label { font-size: 12px; font-weight: 600; color: var(--muted); text-transform: uppercase; letter-spacing: .04em; }
.stat .value { font-size: 20px; font-weight: 700; font-variant-numeric: tabular-nums; margin-top: 2px; }
.stat.big .value { font-size: 32px; }

.monthnav { display: flex; align-items: center; justify-content: center; gap: 4px; margin: 2px 0 12px; }
.monthnav button { border: 0; background: var(--surface); box-shadow: var(--shadow); width: 38px; height: 38px; border-radius: 12px; font-size: 18px; cursor: pointer; }
.monthnav .m { min-width: 150px; text-align: center; font-weight: 700; font-size: 16px; }

.alert {
  display: flex; align-items: center; gap: 12px; padding: 14px 16px; border-radius: var(--radius);
  background: var(--warn-bg); color: var(--warn-ink); font-weight: 600; margin-top: 10px; cursor: pointer;
}
.alert .ic { font-size: 22px; }
.alert .go { margin-left: auto; font-size: 20px; opacity: .6; }

.list { background: var(--surface); border-radius: var(--radius); box-shadow: var(--shadow); overflow: hidden; }
.item {
  display: flex; align-items: center; gap: 12px; padding: 12px 14px; cursor: pointer;
  border-bottom: 1px solid var(--border); background: none; border-left: 0; border-right: 0; border-top: 0; width: 100%; text-align: left;
}
.item:last-child { border-bottom: 0; }
.item:active { background: var(--surface-2); }
.item .ic {
  width: 40px; height: 40px; border-radius: 12px; flex: none;
  display: grid; place-items: center; font-size: 20px; background: var(--surface-2);
}
.item .title { font-weight: 600; }
.item .sub { font-size: 12.5px; color: var(--muted); margin-top: 1px; }
.item .amt { font-weight: 700; font-variant-numeric: tabular-nums; text-align: right; white-space: nowrap; }
.date-head { padding: 14px 6px 6px; font-size: 12.5px; font-weight: 700; color: var(--muted); text-transform: uppercase; letter-spacing: .04em; }

.pill {
  display: inline-flex; align-items: center; gap: 4px; padding: 1px 8px; border-radius: 999px;
  font-size: 11.5px; font-weight: 600; background: var(--surface-2); color: var(--text-2); white-space: nowrap;
}
.pill.warn { background: var(--warn-bg); color: var(--warn-ink); }
.pill.ok { background: color-mix(in srgb, var(--credit) 14%, transparent); color: var(--credit); }
.pill.accent { background: var(--accent-soft); color: var(--accent); }
.pill.danger { background: color-mix(in srgb, var(--danger) 14%, transparent); color: var(--danger); }
.pills { display: flex; flex-wrap: wrap; gap: 4px; margin-top: 4px; }

.empty { text-align: center; padding: 36px 16px; color: var(--muted); }
.empty .ic { font-size: 40px; margin-bottom: 6px; }

/* bars (single-series magnitude: one hue, labels carry identity) */
.bars .b { padding: 8px 2px; cursor: pointer; }
.bars .b .top { display: flex; justify-content: space-between; gap: 8px; font-size: 14px; }
.bars .b .track { height: 8px; border-radius: 4px; background: var(--bar-track); margin-top: 6px; overflow: hidden; }
.bars .b .fill { height: 100%; border-radius: 4px; background: var(--bar); }
.trend { display: flex; align-items: flex-end; gap: 8px; height: 120px; padding-top: 8px; }
.trend .col { flex: 1; display: flex; flex-direction: column; align-items: center; justify-content: flex-end; height: 100%; gap: 4px; cursor: pointer; }
.trend .bar { width: 100%; max-width: 38px; background: var(--bar); border-radius: 4px 4px 0 0; min-height: 2px; opacity: .55; }
.trend .col.cur .bar { opacity: 1; }
.trend .lbl { font-size: 11px; color: var(--muted); }
.trend-tip { text-align: center; font-size: 13px; color: var(--text-2); min-height: 20px; margin-top: 6px; }

.bill { display: flex; align-items: center; gap: 12px; padding: 11px 2px; border-bottom: 1px solid var(--border); }
.bill:last-child { border-bottom: 0; }
.bill .check { width: 26px; height: 26px; border-radius: 50%; display: grid; place-items: center; font-size: 14px; flex: none; border: 2px solid var(--border); }
.bill.paid .check { background: var(--credit); border-color: var(--credit); color: #fff; }

/* ------------------------------------------------ forms */
label.f { display: block; }
label.f > span { display: block; font-size: 13px; font-weight: 600; color: var(--text-2); margin: 0 2px 5px; }
.input, select.input, textarea.input {
  width: 100%; padding: 12px 14px; border-radius: 12px; border: 1px solid var(--border);
  background: var(--surface); outline: none; font-size: 16px; /* 16px avoids iOS zoom */
}
.input:focus { border-color: var(--accent); box-shadow: 0 0 0 3px var(--accent-soft); }
textarea.input { min-height: 80px; resize: vertical; }
.grid2 { display: grid; grid-template-columns: 1fr 1fr; gap: 10px; }

.btn {
  display: inline-flex; align-items: center; justify-content: center; gap: 6px;
  padding: 12px 18px; border-radius: 12px; border: 1px solid var(--border);
  background: var(--surface); font-weight: 600; cursor: pointer; min-height: 46px;
}
.btn.primary { background: var(--accent); border-color: var(--accent); color: var(--accent-ink); }
.btn.danger { color: var(--danger); }
.btn.block { width: 100%; }
.btn.sm { padding: 6px 12px; min-height: 34px; font-size: 13.5px; border-radius: 10px; }
.btn:disabled { opacity: .55; cursor: default; }
.btn-row { display: flex; gap: 10px; }
.btn-row .btn { flex: 1; }

.seg { display: grid; grid-auto-flow: column; grid-auto-columns: 1fr; background: var(--surface-2); padding: 4px; border-radius: 12px; gap: 4px; }
.seg button { border: 0; background: none; padding: 9px 6px; border-radius: 9px; font-weight: 600; color: var(--text-2); cursor: pointer; }
.seg button.on { background: var(--surface); color: var(--text); box-shadow: var(--shadow); }

.chips { display: flex; gap: 6px; overflow-x: auto; padding: 2px 0 10px; scrollbar-width: none; }
.chips::-webkit-scrollbar { display: none; }
.chip {
  flex: none; padding: 7px 13px; border-radius: 999px; border: 1px solid var(--border);
  background: var(--surface); font-size: 13.5px; font-weight: 600; cursor: pointer; white-space: nowrap;
}
.chip.on { background: var(--accent); color: var(--accent-ink); border-color: var(--accent); }

.catgrid { display: grid; grid-template-columns: repeat(auto-fill, minmax(96px, 1fr)); gap: 8px; }
.cat {
  display: flex; flex-direction: column; align-items: center; justify-content: center; gap: 4px;
  padding: 10px 6px; min-height: 76px; border-radius: 14px; border: 2px solid var(--border);
  background: var(--surface); cursor: pointer; font-size: 12px; font-weight: 600; text-align: center; line-height: 1.2;
}
.cat .e { font-size: 22px; }
.cat.on { border-color: var(--accent); background: var(--accent-soft); }

.switch { display: flex; align-items: center; gap: 12px; padding: 4px 0; cursor: pointer; }
.switch input { width: 22px; height: 22px; accent-color: var(--accent); flex: none; }

.box { background: var(--surface-2); border-radius: 14px; padding: 14px; }
.alloc { display: grid; grid-template-columns: 1fr 110px 36px; gap: 6px; align-items: center; margin-bottom: 6px; }
.alloc .input { padding: 10px; font-size: 15px; }
.alloc .note { grid-column: 1 / 3; }
.iconbtn { border: 0; background: none; width: 36px; height: 36px; border-radius: 10px; cursor: pointer; font-size: 18px; color: var(--muted); }

.drop {
  display: block; border: 2px dashed var(--border); border-radius: var(--radius); padding: 28px 16px;
  text-align: center; background: var(--surface); cursor: pointer;
}
.drop .ic { font-size: 36px; }
.drop.has { border-color: var(--accent); background: var(--accent-soft); }

/* ------------------------------------------------ sheet */
.sheet-bg {
  position: fixed; inset: 0; z-index: 50; background: rgba(8, 14, 22, .45);
  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; -webkit-overflow-scrolling: touch;
  background: var(--bg); border-radius: 22px 22px 0 0;
  padding: 8px 16px calc(20px + var(--safe-b));
  animation: up .22s cubic-bezier(.2, .8, .2, 1);
}
.sheet .grab { width: 40px; height: 5px; border-radius: 3px; background: var(--border); margin: 4px auto 10px; }
.sheet-head { display: flex; align-items: center; gap: 8px; margin-bottom: 12px; }
.sheet-head h2 { font-size: 18px; flex: 1; }
.sheet .footer {
  position: sticky; bottom: calc(-20px - var(--safe-b)); margin: 16px -16px calc(-20px - var(--safe-b));
  padding: 12px 16px calc(12px + var(--safe-b)); background: var(--bg); border-top: 1px solid var(--border);
}
@media (min-width: 700px) {
  .sheet-bg { align-items: center; }
  .sheet { border-radius: 22px; max-height: 88dvh; }
}
@keyframes up { from { transform: translateY(40px); } to { transform: none; } }
@keyframes fade { from { opacity: 0; } }

.hero-amt { font-size: 34px; font-weight: 800; font-variant-numeric: tabular-nums; }

.toast {
  position: fixed; left: 50%; bottom: calc(var(--nav-h) + var(--safe-b) + 20px); transform: translateX(-50%);
  z-index: 80; background: var(--text); color: var(--bg); padding: 11px 18px; border-radius: 12px;
  font-weight: 600; font-size: 14px; max-width: calc(100% - 32px); box-shadow: var(--shadow); animation: up .2s;
}
.toast.err { background: var(--danger); color: #fff; }

/* ------------------------------------------------ auth screens */
.auth { min-height: 100dvh; display: flex; flex-direction: column; justify-content: center; padding: 24px 20px; max-width: 420px; margin: 0 auto; }
.auth .logo { font-size: 48px; text-align: center; }
.auth h1 { text-align: center; font-size: 26px; margin: 6px 0 4px; }
.auth p.lead { text-align: center; color: var(--muted); margin: 0 0 24px; }
.who { display: grid; grid-template-columns: repeat(auto-fit, minmax(120px, 1fr)); gap: 10px; }
.who button {
  border: 2px solid var(--border); background: var(--surface); border-radius: 16px; padding: 16px 8px;
  cursor: pointer; font-weight: 700; display: flex; flex-direction: column; align-items: center; gap: 8px;
}
.who button.on { border-color: var(--accent); background: var(--accent-soft); }
.who .avatar { width: 44px; height: 44px; font-size: 18px; }

.spinner { width: 28px; height: 28px; border: 3px solid var(--border); border-top-color: var(--accent); border-radius: 50%; animation: spin .8s linear infinite; margin: 40px auto; }
@keyframes spin { to { transform: rotate(360deg); } }

/* Offline / install */
#installitem:empty { display: none; }
#installcard .alert { cursor: default; background: var(--accent-soft); color: var(--text); }
#installcard .alert > span:nth-child(2) { flex: 1; }
#installcard .iconbtn { margin: -6px -8px -6px -6px; }
.topbar #offline { flex-shrink: 0; }
