:root {
  --bg: #f4f5fb;
  --surface: #ffffff;
  --surface-2: #eef0f8;
  --text: #14152b;
  --muted: #6b6f8a;
  --line: #e3e5f0;
  --primary: #4f46e5;
  --primary-2: #9333ea;
  --primary-soft: #eceafe;
  --pos: #16a34a;
  --neg: #dc2626;
  --warn: #d97706;
  --radius: 18px;
  --shadow: 0 6px 24px rgba(30, 27, 75, .08);
  --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: #0d0e1a;
    --surface: #171829;
    --surface-2: #212338;
    --text: #eceefe;
    --muted: #9195b3;
    --line: #2a2c45;
    --primary-soft: #26245a;
    --shadow: 0 6px 24px rgba(0, 0, 0, .35);
    color-scheme: dark;
  }
}

* { box-sizing: border-box; -webkit-tap-highlight-color: transparent; }
html, body { margin: 0; padding: 0; }
body {
  font: 16px/1.4 -apple-system, BlinkMacSystemFont, "SF Pro Text", "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif;
  background: var(--bg);
  color: var(--text);
  min-height: 100dvh;
  overscroll-behavior-y: none;
}
a { color: var(--primary); text-decoration: none; }
button, input, select, textarea { font: inherit; color: inherit; }
h1, h2, h3 { margin: 0; }

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

/* ---------- layout ---------- */
.screen {
  max-width: 520px; margin: 0 auto;
  padding: calc(12px + var(--safe-t)) 16px calc(96px + var(--safe-b));
  animation: fade .18s ease-out;
}
.screen.no-tabs { padding-bottom: calc(24px + var(--safe-b)); }
@keyframes fade { from { opacity: 0; transform: translateY(4px); } }

.topbar { display: flex; align-items: center; gap: 12px; min-height: 48px; margin-bottom: 12px; }
.topbar h1 { font-size: 22px; font-weight: 700; flex: 1; }
.topbar .back {
  width: 40px; height: 40px; border-radius: 12px; border: 0; background: var(--surface);
  display: grid; place-items: center; cursor: pointer; box-shadow: var(--shadow);
}
.topbar .back svg { width: 22px; height: 22px; }

.greet { display: flex; align-items: center; gap: 12px; flex: 1; color: var(--text); }
.greet small { display: block; color: var(--muted); font-size: 13px; }
.greet b { font-size: 18px; }

.avatar {
  width: 44px; height: 44px; border-radius: 50%; flex: none;
  display: grid; place-items: center; color: #fff; font-weight: 700; font-size: 16px;
}
.avatar.lg { width: 72px; height: 72px; font-size: 26px; }

/* ---------- tab bar ---------- */
.tabbar {
  position: fixed; left: 0; right: 0; bottom: 0; z-index: 10;
  background: color-mix(in srgb, var(--surface) 92%, transparent);
  backdrop-filter: blur(16px); -webkit-backdrop-filter: blur(16px);
  border-top: 1px solid var(--line);
  padding: 6px 4px calc(6px + var(--safe-b));
}
.tabbar nav { max-width: 520px; margin: 0 auto; display: grid; grid-template-columns: repeat(5, 1fr); }
.tabbar a {
  display: flex; flex-direction: column; align-items: center; gap: 2px;
  font-size: 11px; color: var(--muted); padding: 4px 0; font-weight: 500;
}
.tabbar a svg { width: 24px; height: 24px; }
.tabbar a.active { color: var(--primary); }

/* ---------- cards & lists ---------- */
.card { background: var(--surface); border-radius: var(--radius); box-shadow: var(--shadow); padding: 16px; margin-bottom: 14px; }
.section-title { display: flex; justify-content: space-between; align-items: baseline; margin: 22px 4px 10px; }
.section-title h2 { font-size: 18px; }
.section-title a { font-size: 14px; }

.balance-card {
  position: relative; overflow: hidden; color: #fff; border-radius: 24px; padding: 20px;
  background: linear-gradient(135deg, var(--primary), var(--primary-2));
  box-shadow: 0 14px 36px rgba(79, 70, 229, .35);
}
.balance-card::after {
  content: ""; position: absolute; width: 220px; height: 220px; border-radius: 50%;
  right: -70px; top: -90px; background: rgba(255, 255, 255, .12);
}
.balance-card .label { opacity: .85; font-size: 14px; }
.balance-card .amount { font-size: 36px; font-weight: 800; letter-spacing: -.5px; margin: 6px 0 14px; word-break: break-all; }
.balance-card .meta { display: flex; justify-content: space-between; font-size: 13px; opacity: .9; gap: 8px; flex-wrap: wrap; }

.actions { display: grid; grid-template-columns: repeat(3, 1fr); gap: 10px; margin-top: 16px; }
.action {
  display: flex; flex-direction: column; align-items: center; gap: 8px; padding: 14px 4px;
  background: var(--surface); border-radius: var(--radius); box-shadow: var(--shadow);
  color: var(--text); font-size: 13px; font-weight: 600; border: 0; cursor: pointer;
}
.action .ic {
  width: 46px; height: 46px; border-radius: 14px; display: grid; place-items: center;
  background: var(--primary-soft); color: var(--primary);
}
.action .ic svg { width: 24px; height: 24px; }

.list { background: var(--surface); border-radius: var(--radius); box-shadow: var(--shadow); overflow: hidden; }
.row { display: flex; align-items: center; gap: 12px; padding: 12px 14px; color: var(--text); }
.row + .row { border-top: 1px solid var(--line); }
.row .ic {
  width: 42px; height: 42px; border-radius: 50%; flex: none; display: grid; place-items: center;
  background: var(--surface-2); color: var(--muted); font-weight: 700;
}
.row .ic svg { width: 22px; height: 22px; }
.row .body { flex: 1; min-width: 0; }
.row .title { font-weight: 600; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.row .sub { color: var(--muted); font-size: 13px; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.row .val { font-weight: 700; white-space: nowrap; text-align: right; }
.row .val small { display: block; font-weight: 400; color: var(--muted); font-size: 12px; }
.row.link { cursor: pointer; }
.row.link:active { background: var(--surface-2); }
.pos { color: var(--pos); }
.neg { color: var(--text); }
.day { font-size: 13px; color: var(--muted); margin: 16px 6px 8px; font-weight: 600; }
.empty { color: var(--muted); text-align: center; padding: 28px 16px; }

.badge { display: inline-block; font-size: 11px; font-weight: 700; padding: 2px 8px; border-radius: 99px; background: var(--surface-2); color: var(--muted); }
.badge.ok { background: #dcfce7; color: #166534; }
.badge.warn { background: #fef3c7; color: #92400e; }
.badge.bad { background: #fee2e2; color: #991b1b; }

.progress { height: 6px; border-radius: 99px; background: var(--surface-2); overflow: hidden; margin-top: 8px; }
.progress div { height: 100%; background: linear-gradient(90deg, var(--primary), var(--primary-2)); }

/* ---------- bank card ---------- */
.bank-card {
  aspect-ratio: 1.586; border-radius: 22px; padding: 22px; color: #fff; position: relative; overflow: hidden;
  background: radial-gradient(circle at 85% 0%, rgba(255,255,255,.25), transparent 45%), linear-gradient(135deg, #1e1b4b, #4f46e5 55%, #9333ea);
  box-shadow: 0 18px 40px rgba(30, 27, 75, .4);
  display: flex; flex-direction: column; justify-content: space-between;
}
.bank-card .top { display: flex; justify-content: space-between; align-items: center; font-weight: 700; }
.bank-card .chip { width: 44px; height: 32px; border-radius: 7px; background: linear-gradient(135deg, #fde68a, #d97706); }
.bank-card .num { font: 600 clamp(17px, 5.4vw, 24px)/1 ui-monospace, "SF Mono", Menlo, monospace; letter-spacing: 1.5px; }
.bank-card .bottom { display: flex; justify-content: space-between; font-size: 13px; text-transform: uppercase; letter-spacing: .5px; }
.bank-card .bottom small { display: block; opacity: .7; font-size: 10px; }

/* ---------- forms ---------- */
.field { display: block; margin-bottom: 14px; }
.field > span { display: block; font-size: 13px; color: var(--muted); margin: 0 4px 6px; font-weight: 500; }
.input {
  width: 100%; border: 1.5px solid var(--line); background: var(--surface); border-radius: 14px;
  padding: 13px 14px; outline: none; transition: border-color .15s; font-size: 16px;
}
.input:focus { border-color: var(--primary); }
.input.big { font-size: 28px; font-weight: 700; }
.hint { font-size: 13px; color: var(--muted); margin: 6px 4px 0; min-height: 18px; }
.hint.ok { color: var(--pos); }
.hint.err { color: var(--neg); }
.check { display: flex; align-items: center; gap: 10px; margin-bottom: 14px; font-size: 15px; }
.check input { width: 20px; height: 20px; accent-color: var(--primary); }
.grid2 { display: grid; grid-template-columns: 1fr 1fr; gap: 10px; }

.btn {
  display: inline-flex; align-items: center; justify-content: center; gap: 8px;
  border: 0; border-radius: 14px; padding: 14px 18px; font-weight: 700; cursor: pointer;
  background: var(--primary); color: #fff; width: 100%; font-size: 16px;
  transition: transform .08s, opacity .15s;
}
.btn:active { transform: scale(.98); }
.btn:disabled { opacity: .5; cursor: default; }
.btn.secondary { background: var(--primary-soft); color: var(--primary); }
.btn.ghost { background: transparent; color: var(--primary); }
.btn.danger { background: #fee2e2; color: #b91c1c; }
.btn.sm { padding: 9px 12px; font-size: 14px; border-radius: 11px; width: auto; }
.btn-row { display: flex; gap: 10px; }
.btn-row > * { flex: 1; }

.chips { display: flex; gap: 8px; flex-wrap: wrap; margin: -4px 0 14px; }
.chip-btn { border: 0; background: var(--surface-2); padding: 7px 12px; border-radius: 99px; font-size: 14px; cursor: pointer; font-weight: 600; }

.segmented { display: grid; grid-auto-flow: column; grid-auto-columns: 1fr; background: var(--surface-2); padding: 4px; border-radius: 14px; margin-bottom: 16px; }
.segmented button { border: 0; background: transparent; padding: 9px; border-radius: 11px; font-weight: 600; cursor: pointer; color: var(--muted); }
.segmented button.on { background: var(--surface); color: var(--text); box-shadow: 0 2px 8px rgba(0,0,0,.08); }

/* ---------- login ---------- */
.login { min-height: 100dvh; display: flex; flex-direction: column; justify-content: center; padding: 24px 16px calc(24px + var(--safe-b)); max-width: 420px; margin: 0 auto; }
.login .logo { width: 84px; height: 84px; border-radius: 24px; margin: 0 auto 16px; display: block; box-shadow: 0 14px 36px rgba(79, 70, 229, .35); }
.login h1 { text-align: center; font-size: 28px; margin-bottom: 4px; }
.login p { text-align: center; color: var(--muted); margin: 0 0 28px; }

/* ---------- qr ---------- */
.qr-box { background: #fff; border-radius: 20px; padding: 16px; width: min(280px, 80vw); margin: 4px auto 14px; box-shadow: var(--shadow); }
.qr-box img, .qr-box svg { width: 100%; height: auto; display: block; }
.scanner { position: relative; border-radius: 24px; overflow: hidden; background: #000; aspect-ratio: 3/4; max-height: 62dvh; margin: 0 auto 16px; }
.scanner video { width: 100%; height: 100%; object-fit: cover; display: block; }
.scanner .frame {
  position: absolute; inset: 0; margin: auto; width: 64%; aspect-ratio: 1; border-radius: 22px;
  box-shadow: 0 0 0 9999px rgba(0, 0, 0, .45); border: 3px solid rgba(255, 255, 255, .9);
}
.scanner .msg { position: absolute; left: 0; right: 0; bottom: 14px; text-align: center; color: #fff; font-size: 14px; padding: 0 16px; }

.center { text-align: center; }
.big-amount { font-size: 34px; font-weight: 800; text-align: center; margin: 4px 0; }
.success-ic { width: 84px; height: 84px; border-radius: 50%; margin: 24px auto 16px; display: grid; place-items: center; background: #dcfce7; color: var(--pos); }
.success-ic svg { width: 44px; height: 44px; }
.muted { color: var(--muted); }
.mono { font-family: ui-monospace, "SF Mono", Menlo, monospace; }
.kv { display: flex; justify-content: space-between; gap: 12px; padding: 10px 0; }
.kv + .kv { border-top: 1px solid var(--line); }
.kv span:first-child { color: var(--muted); }
.kv span:last-child { font-weight: 600; text-align: right; word-break: break-all; }
.copy { border: 0; background: none; color: var(--primary); cursor: pointer; padding: 0 0 0 6px; font-weight: 600; }

/* ---------- modal & toast ---------- */
.modal-back {
  position: fixed; inset: 0; background: rgba(10, 10, 30, .5); z-index: 50;
  display: flex; align-items: flex-end; justify-content: center; animation: fadeIn .15s;
}
@keyframes fadeIn { from { opacity: 0; } }
.sheet {
  background: var(--bg); width: 100%; max-width: 520px; border-radius: 24px 24px 0 0;
  padding: 10px 16px calc(20px + var(--safe-b)); max-height: 92dvh; overflow: auto;
  animation: up .2s ease-out;
}
.sheet::before { content: ""; display: block; width: 40px; height: 5px; border-radius: 9px; background: var(--line); margin: 0 auto 14px; }
.sheet h2 { font-size: 20px; margin-bottom: 14px; }
@keyframes up { from { transform: translateY(40px); opacity: .5; } }
@media (min-width: 700px) {
  .modal-back { align-items: center; }
  .sheet { border-radius: 24px; }
}

#toast {
  position: fixed; left: 50%; top: calc(12px + var(--safe-t)); transform: translate(-50%, -150%);
  background: #14152b; color: #fff; padding: 12px 18px; border-radius: 14px; z-index: 100;
  font-weight: 600; font-size: 15px; transition: transform .25s; max-width: calc(100vw - 32px);
  box-shadow: 0 10px 30px rgba(0, 0, 0, .3);
}
#toast.show { transform: translate(-50%, 0); }
#toast.err { background: #b91c1c; }
#toast.ok { background: #15803d; }

/* ---------- admin ---------- */
.admin { max-width: 980px; margin: 0 auto; padding: calc(12px + var(--safe-t)) 16px calc(32px + var(--safe-b)); }
.admin-head { display: flex; align-items: center; gap: 12px; flex-wrap: wrap; margin-bottom: 16px; }
.admin-head h1 { font-size: 22px; flex: 1; min-width: 160px; }
.admin-nav { display: flex; gap: 6px; flex-wrap: wrap; }
.admin-nav a { padding: 8px 12px; border-radius: 10px; color: var(--muted); font-weight: 600; font-size: 14px; }
.admin-nav a.active { background: var(--primary-soft); color: var(--primary); }
.stats { display: grid; grid-template-columns: repeat(auto-fit, minmax(150px, 1fr)); gap: 10px; margin-bottom: 14px; }
.stat { background: var(--surface); border-radius: var(--radius); padding: 14px 16px; box-shadow: var(--shadow); }
.stat small { color: var(--muted); font-size: 13px; }
.stat b { display: block; font-size: 22px; margin-top: 4px; word-break: break-all; }
.toolbar { display: flex; gap: 10px; margin-bottom: 14px; flex-wrap: wrap; }
.toolbar .input { flex: 1; min-width: 180px; }
.toolbar .btn { width: auto; }
.cols { display: grid; gap: 14px; }
@media (min-width: 860px) { .cols { grid-template-columns: 1fr 1fr; align-items: start; } }
.card h3 { font-size: 16px; margin-bottom: 12px; }
.creds { background: var(--surface-2); border-radius: 14px; padding: 14px; margin-bottom: 14px; }

/* ---------- multi-currency & privacy ---------- */
.eye {
  width: 40px; height: 40px; border-radius: 12px; border: 0; flex: none; cursor: pointer;
  background: var(--surface); color: var(--muted); display: grid; place-items: center; box-shadow: var(--shadow);
}
.eye svg { width: 22px; height: 22px; }
.acc-scroll {
  display: flex; gap: 12px; overflow-x: auto; scroll-snap-type: x mandatory;
  margin: 0 -16px; padding: 0 16px 20px; scrollbar-width: none;
}
.acc-scroll::-webkit-scrollbar { display: none; }
.acc-scroll .balance-card { flex: 0 0 calc(100% - 28px); scroll-snap-align: center; display: block; color: #fff; }
.acc-scroll .balance-card:only-child { flex-basis: 100%; }
.total-line { display: flex; justify-content: space-between; gap: 8px; color: var(--muted); font-size: 14px; margin: -8px 4px 0; flex-wrap: wrap; }
.cur-ic { font-weight: 800; font-size: 17px; color: var(--primary) !important; background: var(--primary-soft) !important; }
.grid3 { display: grid; grid-template-columns: 1fr 1fr 1fr; gap: 10px; }
select.input {
  appearance: none; -webkit-appearance: none; padding-right: 36px; cursor: pointer;
  background-image: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 24 24' fill='none' stroke='%236b6f8a' stroke-width='2'%3E%3Cpath d='M6 9l6 6 6-6'/%3E%3C/svg%3E");
  background-repeat: no-repeat; background-position: right 12px center; background-size: 18px;
}
.ex-result { background: var(--surface); border-radius: var(--radius); padding: 14px 16px; margin-bottom: 14px; box-shadow: var(--shadow); }
.ex-result b { display: block; font-size: 24px; margin-top: 2px; }
.stat b.multi { font-size: 16px; line-height: 1.5; }
.val small.alt { color: var(--muted); }

/* ---------- spending chart ---------- */
/* Categorical series: validated palette (light vs white cards, dark vs #171829). Fixed slot per category. */
:root {
  --s1: #2a78d6; --s2: #eb6834; --s3: #1baf7a; --s4: #eda100;
  --s5: #e87ba4; --s6: #008300; --s7: #4a3aa7; --s8: #e34948; --s-rest: #9a98a6;
}
@media (prefers-color-scheme: dark) {
  :root {
    --s1: #3987e5; --s2: #d95926; --s3: #199e70; --s4: #c98500;
    --s5: #d55181; --s6: #008300; --s7: #9085e9; --s8: #e66767; --s-rest: #6b6d80;
  }
}
.spend-sum { display: flex; justify-content: space-between; gap: 8px; margin-bottom: 6px; font-size: 14px; color: var(--muted); }
.spend-sum b { display: block; font-size: 17px; color: var(--text); }
.donut-wrap { position: relative; width: min(220px, 70vw); margin: 6px auto 12px; }
.donut { display: block; width: 100%; height: auto; }
.donut .slice { cursor: pointer; transition: opacity .15s; }
.donut .slice.dim { opacity: .25; }
.donut-center { position: absolute; inset: 0; display: grid; place-content: center; text-align: center; pointer-events: none; padding: 0 22%; }
.donut-center small { color: var(--muted); font-size: 12px; }
.donut-center b { font-size: 19px; line-height: 1.2; }
.legend { display: grid; gap: 2px; }
.leg-row {
  display: grid; grid-template-columns: 12px 1fr auto auto; align-items: center; gap: 10px;
  border: 0; background: none; padding: 8px 6px; border-radius: 10px; text-align: left; cursor: pointer; width: 100%;
}
.leg-row.on { background: var(--surface-2); }
.leg-row.dim { opacity: .45; }
.leg-row .sw { width: 12px; height: 12px; border-radius: 50%; }
.leg-row .pct { color: var(--muted); font-size: 13px; font-variant-numeric: tabular-nums; }
.leg-row .amt { font-weight: 600; font-variant-numeric: tabular-nums; }

/* ---------- history generator ---------- */
.sheet.wide { max-width: 720px; }
.gen-head, .gen-row { display: grid; grid-template-columns: 1.5fr 1fr .8fr 36px; gap: 8px; }
.gen-head { font-size: 12px; color: var(--muted); margin: 0 4px 6px; }
.gen-row { padding-bottom: 10px; margin-bottom: 10px; border-bottom: 1px solid var(--line); }
.gen-row .input { padding: 10px; font-size: 15px; }
.gen-row .wide { grid-column: 1 / -1; }
.gen-del { border: 0; border-radius: 10px; background: var(--surface-2); color: var(--muted); font-size: 20px; cursor: pointer; }
.gen-sum { background: var(--surface-2); border-radius: 14px; padding: 12px 14px; margin: 4px 0 14px; font-size: 14px; line-height: 1.6; }
.gen-sum b { font-variant-numeric: tabular-nums; }
