:root {
  --bg: #f2f2f7;
  --card: #ffffff;
  --card-2: #f7f7fa;
  --text: #111114;
  --muted: #6e6e76;
  --line: rgba(60, 60, 67, 0.14);
  --brand: #0f9d58;
  --brand-ink: #ffffff;
  --brand-soft: rgba(15, 157, 88, 0.12);
  --ok: #18a058;
  --warn: #e08a00;
  --bad: #e5484d;
  --bad-soft: rgba(229, 72, 77, 0.12);
  --warn-soft: rgba(224, 138, 0, 0.14);
  --income: #0f9d58;
  --shadow: 0 1px 2px rgba(0, 0, 0, 0.04), 0 6px 24px rgba(0, 0, 0, 0.06);
  --radius: 18px;
  --tabbar-h: 64px;
  color-scheme: light;
}

@media (prefers-color-scheme: dark) {
  :root {
    --bg: #000000;
    --card: #1c1c1e;
    --card-2: #2c2c2e;
    --text: #f5f5f7;
    --muted: #9a9aa2;
    --line: rgba(255, 255, 255, 0.1);
    --brand: #2fc777;
    --brand-ink: #04210f;
    --brand-soft: rgba(47, 199, 119, 0.16);
    --ok: #2fc777;
    --warn: #ffb224;
    --bad: #ff6369;
    --bad-soft: rgba(255, 99, 105, 0.16);
    --warn-soft: rgba(255, 178, 36, 0.16);
    --income: #2fc777;
    --shadow: none;
    color-scheme: dark;
  }
  .seg > button.on { background: #48484a; }
}

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

html, body {
  margin: 0;
  background: var(--bg);
  color: var(--text);
  font-family: -apple-system, BlinkMacSystemFont, 'SF Pro Text', 'Segoe UI', Roboto, sans-serif;
  font-size: 16px;
  line-height: 1.35;
  -webkit-font-smoothing: antialiased;
  -webkit-text-size-adjust: 100%;
}

body { min-height: 100vh; overscroll-behavior-y: none; }

button, input, select, textarea { font: inherit; color: inherit; }

#app {
  max-width: 560px;
  margin: 0 auto;
  padding: calc(env(safe-area-inset-top) + 12px) 16px calc(var(--tabbar-h) + env(safe-area-inset-bottom) + 28px);
}

.screen-title { display: flex; align-items: center; justify-content: space-between; gap: 12px; margin: 6px 2px 14px; }
.screen-title h1 { font-size: 30px; line-height: 1.1; margin: 0; letter-spacing: -0.02em; }
.screen-title .sub { color: var(--muted); font-size: 14px; margin-top: 2px; }

.back { background: none; border: 0; color: var(--brand); font-size: 17px; padding: 4px 0; display: inline-flex; align-items: center; gap: 4px; cursor: pointer; }

.card { background: var(--card); border-radius: var(--radius); padding: 16px; margin-bottom: 14px; box-shadow: var(--shadow); }
.card.flush { padding: 0; overflow: hidden; }
.card h2 { font-size: 13px; text-transform: uppercase; letter-spacing: 0.04em; color: var(--muted); margin: 0 0 10px; font-weight: 600; }
.card h3 { font-size: 17px; margin: 0; }

.hero { padding: 20px 18px; }
.hero .label { font-size: 15px; color: var(--muted); }
.hero .big { font-size: 40px; font-weight: 700; letter-spacing: -0.03em; margin: 2px 0 6px; font-variant-numeric: tabular-nums; }
.hero.ok { background: linear-gradient(135deg, var(--brand), #0b7d45); color: #fff; }
.hero.ok .label, .hero.ok .meta { color: rgba(255, 255, 255, 0.85); }
.hero.todo .big { color: var(--brand); }
.hero.over .big { color: var(--bad); }
.hero .meta { display: flex; flex-wrap: wrap; gap: 6px 14px; color: var(--muted); font-size: 13px; margin-top: 6px; }

.month-switch { display: inline-flex; align-items: center; gap: 4px; background: var(--card); border-radius: 999px; padding: 4px; box-shadow: var(--shadow); }
.month-switch button { border: 0; background: none; width: 32px; height: 32px; border-radius: 50%; font-size: 18px; color: var(--brand); cursor: pointer; }
.month-switch span { font-size: 14px; font-weight: 600; min-width: 112px; text-align: center; }

.row { display: flex; align-items: center; gap: 12px; padding: 12px 16px; border-bottom: 1px solid var(--line); background: var(--card); }
.row:last-child { border-bottom: 0; }
.row.tap { cursor: pointer; }
.row.tap:active { background: var(--card-2); }
.row .icon { width: 36px; height: 36px; border-radius: 10px; background: var(--card-2); display: grid; place-items: center; font-size: 19px; flex: none; }
.row .main { flex: 1; min-width: 0; }
.row .title { font-weight: 600; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.row .meta { color: var(--muted); font-size: 13px; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.row .end { text-align: right; font-variant-numeric: tabular-nums; flex: none; }
.row .end .meta { font-size: 12px; }
.chev::after { content: '›'; color: var(--muted); font-size: 22px; margin-left: 4px; }

.amount-neg { color: var(--text); font-weight: 600; }
.amount-pos { color: var(--income); font-weight: 600; }

.bar { height: 6px; border-radius: 99px; background: var(--card-2); overflow: hidden; margin-top: 6px; }
.bar > i { display: block; height: 100%; border-radius: 99px; background: var(--ok); transition: width 0.3s ease; }
.bar.warn > i { background: var(--warn); }
.bar.bad > i { background: var(--bad); }
.bar.big { height: 10px; }

.line-item { padding: 12px 16px; border-bottom: 1px solid var(--line); cursor: pointer; }
.line-item:last-child { border-bottom: 0; }
.line-item:active { background: var(--card-2); }
.line-item .top { display: flex; align-items: center; gap: 10px; }
.line-item .name { flex: 1; font-weight: 500; min-width: 0; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.line-item .left { font-variant-numeric: tabular-nums; font-weight: 600; }
.line-item .left.bad { color: var(--bad); }
.line-item .sub { color: var(--muted); font-size: 12px; margin-top: 4px; display: flex; justify-content: space-between; }

.group-head { display: flex; justify-content: space-between; align-items: baseline; padding: 14px 16px 6px; }
.group-head h2 { margin: 0; }
.group-head span { color: var(--muted); font-size: 13px; font-variant-numeric: tabular-nums; }

.btn { appearance: none; border: 0; border-radius: 14px; padding: 14px 18px; font-weight: 600; font-size: 17px; cursor: pointer; background: var(--brand); color: var(--brand-ink); width: 100%; display: inline-flex; align-items: center; justify-content: center; gap: 8px; }
.btn:active { opacity: 0.85; }
.btn:disabled { opacity: 0.5; }
.btn.secondary { background: var(--brand-soft); color: var(--brand); }
.btn.ghost { background: var(--card-2); color: var(--text); }
.btn.danger { background: var(--bad-soft); color: var(--bad); }
.btn.small { width: auto; padding: 8px 14px; font-size: 15px; border-radius: 12px; }
.btn-row { display: flex; gap: 10px; }
.btn-row > * { flex: 1; }
.hero.ok .btn.secondary { background: rgba(255, 255, 255, 0.2); color: #fff; }

.link { background: none; border: 0; color: var(--brand); font-weight: 600; cursor: pointer; padding: 0; font-size: 15px; }

.pill { display: inline-flex; align-items: center; gap: 4px; padding: 3px 9px; border-radius: 99px; font-size: 12px; font-weight: 600; background: var(--card-2); color: var(--muted); }
.pill.ok { background: var(--brand-soft); color: var(--brand); }
.pill.warn { background: var(--warn-soft); color: var(--warn); }
.pill.bad { background: var(--bad-soft); color: var(--bad); }

.empty { text-align: center; color: var(--muted); padding: 28px 16px; }
.empty .emoji { font-size: 40px; display: block; margin-bottom: 8px; }

.hint { color: var(--muted); font-size: 13px; margin: 8px 2px 0; }
.tip { background: var(--brand-soft); color: var(--text); border-radius: 14px; padding: 12px 14px; font-size: 14px; margin-bottom: 14px; }
.tip b { color: var(--brand); }

/* Tab bar */
.tabbar { position: fixed; left: 0; right: 0; bottom: 0; z-index: 20; background: color-mix(in srgb, var(--card) 86%, transparent); backdrop-filter: saturate(180%) blur(20px); -webkit-backdrop-filter: saturate(180%) blur(20px); border-top: 1px solid var(--line); padding-bottom: env(safe-area-inset-bottom); }
.tabbar nav { max-width: 560px; margin: 0 auto; height: var(--tabbar-h); display: grid; grid-template-columns: repeat(5, 1fr); align-items: center; }
.tabbar a { display: flex; flex-direction: column; align-items: center; gap: 2px; text-decoration: none; color: var(--muted); font-size: 11px; font-weight: 500; }
.tabbar a .ti { font-size: 22px; line-height: 1; filter: grayscale(1); opacity: 0.7; }
.tabbar a.active { color: var(--brand); }
.tabbar a.active .ti { filter: none; opacity: 1; }
.tabbar .plus { justify-self: center; width: 54px; height: 54px; border-radius: 50%; background: var(--brand); color: var(--brand-ink); border: 0; font-size: 30px; font-weight: 400; box-shadow: 0 6px 18px rgba(15, 157, 88, 0.35); cursor: pointer; display: grid; place-items: center; margin-top: -18px; }

/* 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, select.input, textarea.input { width: 100%; border: 1px solid var(--line); background: var(--card-2); border-radius: 12px; padding: 12px 14px; font-size: 17px; outline: none; -webkit-appearance: none; appearance: none; }
.input:focus { border-color: var(--brand); background: var(--card); }
.input.money { font-variant-numeric: tabular-nums; }
.amount-input { font-size: 44px; font-weight: 700; text-align: center; border: 0; background: none; width: 100%; outline: none; letter-spacing: -0.02em; font-variant-numeric: tabular-nums; padding: 6px 0; }
.amount-input::placeholder { color: var(--muted); opacity: 0.35; }
.amount-wrap { text-align: center; margin: 4px 0 14px; }
.amount-wrap .cur { color: var(--muted); font-size: 15px; }
.field-row { display: flex; gap: 10px; }
.field-row > .field { flex: 1; min-width: 0; }

.seg { display: flex; background: var(--card-2); border-radius: 12px; padding: 3px; gap: 3px; margin-bottom: 14px; }
.seg button { flex: 1; border: 0; background: none; padding: 9px 6px; border-radius: 10px; font-weight: 600; font-size: 15px; color: var(--muted); cursor: pointer; }
.seg button.on { background: var(--card); color: var(--text); box-shadow: 0 1px 4px rgba(0, 0, 0, 0.12); }

.chips { display: flex; flex-wrap: wrap; gap: 8px; margin-bottom: 14px; }
.chip { border: 1px solid var(--line); background: var(--card); border-radius: 99px; padding: 8px 12px; font-size: 14px; font-weight: 500; cursor: pointer; display: inline-flex; gap: 6px; align-items: center; }
.chip.on { background: var(--brand); color: var(--brand-ink); border-color: var(--brand); }

.cat-grid { display: grid; grid-template-columns: repeat(4, 1fr); gap: 8px; margin-bottom: 14px; }
.cat { border: 1px solid var(--line); background: var(--card); border-radius: 14px; padding: 10px 4px 8px; text-align: center; cursor: pointer; font-size: 11px; font-weight: 500; line-height: 1.15; }
.cat .ce { font-size: 22px; display: block; margin-bottom: 4px; }
.cat.on { border-color: var(--brand); background: var(--brand-soft); color: var(--brand); }

/* Sheet */
.sheet-backdrop { position: fixed; inset: 0; background: rgba(0, 0, 0, 0.35); z-index: 40; opacity: 0; transition: opacity 0.2s ease; }
.sheet-backdrop.open { opacity: 1; }
.sheet { position: fixed; left: 0; right: 0; bottom: 0; z-index: 41; max-width: 560px; margin: 0 auto; background: var(--bg); border-radius: 22px 22px 0 0; max-height: 92vh; display: flex; flex-direction: column; transform: translateY(100%); transition: transform 0.28s cubic-bezier(0.2, 0.9, 0.3, 1); }
.sheet.open { transform: translateY(0); }
.sheet header { display: flex; align-items: center; justify-content: space-between; padding: 14px 16px 8px; }
.sheet header h2 { font-size: 18px; margin: 0; }
.sheet header button { background: var(--card-2); border: 0; width: 30px; height: 30px; border-radius: 50%; font-size: 16px; color: var(--muted); cursor: pointer; }
.sheet .grabber { width: 38px; height: 5px; border-radius: 99px; background: var(--line); margin: 8px auto 0; }
.sheet .body { overflow-y: auto; padding: 6px 16px calc(env(safe-area-inset-bottom) + 18px); -webkit-overflow-scrolling: touch; }

/* Toast */
.toast { position: fixed; left: 50%; top: calc(env(safe-area-inset-top) + 12px); transform: translate(-50%, -150%); z-index: 60; background: var(--text); color: var(--bg); padding: 12px 18px; border-radius: 14px; font-size: 15px; font-weight: 500; max-width: calc(100% - 32px); box-shadow: 0 8px 30px rgba(0, 0, 0, 0.25); transition: transform 0.3s ease; text-align: center; }
.toast.show { transform: translate(-50%, 0); }

/* Login */
.login { min-height: calc(100vh - 80px); display: flex; flex-direction: column; justify-content: center; }
.login .logo { width: 76px; height: 76px; border-radius: 20px; margin: 0 auto 14px; display: block; box-shadow: 0 10px 30px rgba(15, 157, 88, 0.3); }
.login h1 { text-align: center; margin: 0 0 4px; font-size: 28px; }
.login p { text-align: center; color: var(--muted); margin: 0 0 24px; }
.code-input { letter-spacing: 0.5em; text-align: center; font-size: 28px; font-weight: 700; }

/* Debts */
.compare { display: grid; grid-template-columns: 1fr 1fr; gap: 10px; margin-bottom: 14px; }
.compare .opt { background: var(--card); border-radius: 16px; padding: 14px; border: 2px solid transparent; box-shadow: var(--shadow); cursor: pointer; }
.compare .opt.on { border-color: var(--brand); }
.compare .opt .t { font-weight: 700; margin-bottom: 4px; }
.compare .opt .v { font-size: 22px; font-weight: 700; letter-spacing: -0.02em; }
.compare .opt .m { color: var(--muted); font-size: 12px; }
.range { width: 100%; accent-color: var(--brand); margin: 10px 0 4px; }

.step-dots { display: flex; gap: 6px; margin: 10px 0 2px; }
.step-dots i { flex: 1; height: 6px; border-radius: 99px; background: var(--card-2); }
.step-dots i.done { background: var(--brand); }
.step-dots i.now { background: linear-gradient(90deg, var(--brand) var(--p, 0%), var(--card-2) var(--p, 0%)); }

.stat-grid { display: grid; grid-template-columns: 1fr 1fr; gap: 12px; }
.stat .k { color: var(--muted); font-size: 13px; }
.stat .v { font-size: 20px; font-weight: 700; letter-spacing: -0.02em; font-variant-numeric: tabular-nums; }

.sticky-top { position: sticky; top: 0; z-index: 10; background: var(--bg); padding: calc(env(safe-area-inset-top) + 6px) 0 10px; margin: calc(-1 * env(safe-area-inset-top) - 12px) 0 6px; }
.sticky-bottom { position: sticky; bottom: calc(var(--tabbar-h) + env(safe-area-inset-bottom) + 10px); z-index: 10; }

.plan-line { display: flex; align-items: center; gap: 10px; padding: 10px 16px; border-bottom: 1px solid var(--line); }
.plan-line:last-child { border-bottom: 0; }
.plan-line .name { flex: 1; min-width: 0; }
.plan-line .name .meta { color: var(--muted); font-size: 12px; }
.plan-line input { width: 118px; text-align: right; padding: 9px 10px; font-size: 16px; }

.day-head { font-size: 13px; color: var(--muted); font-weight: 600; margin: 16px 4px 6px; text-transform: capitalize; }
.badge-wa { font-size: 10px; font-weight: 700; color: #25d366; margin-left: 4px; }

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

.prose p { margin: 0 0 10px; }
.prose ol, .prose ul { padding-left: 20px; margin: 0 0 10px; }
.prose li { margin-bottom: 6px; }
.prose code { background: var(--card-2); padding: 1px 6px; border-radius: 6px; font-size: 14px; }
