:root {
  --bg: #f6f5f1;
  --surface: #ffffff;
  --surface-2: #efede7;
  --text: #1d1d1b;
  --text-2: #5f5e5a;
  --text-3: #8b8a84;
  --border: rgba(0, 0, 0, 0.1);
  --border-strong: rgba(0, 0, 0, 0.2);
  --accent: #2563c9;
  --accent-bg: #e6eefb;
  --on-accent: #ffffff;
  --danger: #b42318;
  --danger-bg: #fbeaea;
  --danger-fill: #d9433b;
  --warning: #8a5300;
  --warning-bg: #fbf0d9;
  --warning-fill: #e3a32b;
  --success: #2f6b12;
  --success-bg: #e8f3dc;
  --success-fill: #5a9a2a;
  --income: #4f8fd9;
  --radius: 10px;
  --radius-lg: 14px;
  --nav-h: 64px;
  --font: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif;
  color-scheme: light;
}
@media (prefers-color-scheme: dark) {
  :root {
    --bg: #1b1b19;
    --surface: #252523;
    --surface-2: #2f2f2c;
    --text: #f1efe8;
    --text-2: #b4b2a9;
    --text-3: #888780;
    --border: rgba(255, 255, 255, 0.1);
    --border-strong: rgba(255, 255, 255, 0.22);
    --accent: #6ea2ec;
    --accent-bg: #1d3352;
    --on-accent: #0b1a2e;
    --danger: #f09595;
    --danger-bg: #4a1b1b;
    --danger-fill: #e2605a;
    --warning: #f2c46d;
    --warning-bg: #45320f;
    --warning-fill: #e3a32b;
    --success: #a8d67c;
    --success-bg: #22380f;
    --success-fill: #78b440;
    --income: #6ea2ec;
    color-scheme: dark;
  }
}

* { box-sizing: border-box; }
html, body { margin: 0; }
body {
  background: var(--bg);
  color: var(--text);
  font-family: var(--font);
  font-size: 15px;
  line-height: 1.5;
  -webkit-text-size-adjust: 100%;
  -webkit-tap-highlight-color: transparent;
}
button, input, select, textarea { font: inherit; color: inherit; }
h1, h2, h3 { margin: 0; font-weight: 600; line-height: 1.25; }
h1 { font-size: 22px; }
h2 { font-size: 17px; }
h3 { font-size: 15px; }
p { margin: 0; }
.muted { color: var(--text-2); }
.small { font-size: 13px; }
.tiny { font-size: 12px; color: var(--text-3); }
.num { font-variant-numeric: tabular-nums; }
.neg { color: var(--danger); }
.pos { color: var(--success); }
.sr-only { position: absolute; width: 1px; height: 1px; overflow: hidden; clip: rect(0 0 0 0); white-space: nowrap; }

/* Layout */
.app { min-height: 100vh; }
.sidebar { display: none; }
.main {
  max-width: 720px;
  margin: 0 auto;
  padding: calc(16px + env(safe-area-inset-top)) 16px calc(var(--nav-h) + 88px + env(safe-area-inset-bottom));
  outline: none;
}
.page { display: flex; flex-direction: column; gap: 16px; }
.page-head { display: flex; align-items: center; justify-content: space-between; gap: 8px; }
.page-head .title { display: flex; align-items: center; gap: 4px; }
.grid-2 { display: grid; grid-template-columns: minmax(0, 1fr) minmax(0, 1fr); gap: 10px; }
.grid-3 { display: grid; grid-template-columns: repeat(3, minmax(0, 1fr)); gap: 8px; }
.stack { display: flex; flex-direction: column; gap: 10px; }
.row { display: flex; align-items: center; justify-content: space-between; gap: 10px; }
.grow { flex: 1; min-width: 0; }
.ellipsis { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }

/* Navigation */
.bottom-nav {
  position: fixed; left: 0; right: 0; bottom: 0; z-index: 20;
  display: flex; justify-content: space-around;
  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(--border);
}
.nav-item {
  flex: 1; display: flex; flex-direction: column; align-items: center; justify-content: center; gap: 2px;
  color: var(--text-3); text-decoration: none; font-size: 11px;
}
.nav-item svg { width: 22px; height: 22px; }
.nav-item[aria-current="page"] { color: var(--accent); }
.fab {
  position: fixed; right: 18px; z-index: 21;
  bottom: calc(var(--nav-h) + 16px + env(safe-area-inset-bottom));
  width: 58px; height: 58px; border-radius: 50%; border: 0;
  background: var(--accent); color: var(--on-accent);
  font-size: 30px; line-height: 1; cursor: pointer;
  box-shadow: 0 4px 14px rgba(0, 0, 0, 0.22);
}
.app.setup .bottom-nav, .app.setup .fab, .app.setup .sidebar { display: none !important; }

/* Cards and metrics */
.card {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius-lg);
  padding: 14px 16px;
}
.card.flush { padding: 0; overflow: hidden; }
.card-head { display: flex; align-items: baseline; justify-content: space-between; gap: 8px; margin-bottom: 10px; }
.metric { background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius); padding: 10px 12px; min-width: 0; }
.metric .label { font-size: 12px; color: var(--text-2); }
.metric .value { font-size: 19px; font-weight: 600; font-variant-numeric: tabular-nums; white-space: nowrap; }
.metric .sub { font-size: 12px; color: var(--text-3); }

.banner { border-radius: var(--radius); padding: 10px 12px; font-size: 14px; display: flex; gap: 10px; align-items: flex-start; }
.banner.warn { background: var(--warning-bg); color: var(--warning); }
.banner.danger { background: var(--danger-bg); color: var(--danger); }
.banner.info { background: var(--accent-bg); color: var(--accent); }
.banner.ok { background: var(--success-bg); color: var(--success); }
.banner .grow { flex: 1; }
.banner button { color: inherit; }

.badge { display: inline-block; font-size: 11px; font-weight: 600; padding: 2px 7px; border-radius: 6px; white-space: nowrap; }
.badge.danger { background: var(--danger-bg); color: var(--danger); }
.badge.warn { background: var(--warning-bg); color: var(--warning); }
.badge.ok { background: var(--success-bg); color: var(--success); }
.badge.info { background: var(--accent-bg); color: var(--accent); }
.badge.neutral { background: var(--surface-2); color: var(--text-2); }

.bar { height: 7px; border-radius: 4px; background: var(--surface-2); overflow: hidden; }
.bar > span { display: block; height: 100%; border-radius: 4px; background: var(--success-fill); }
.bar > span.warn { background: var(--warning-fill); }
.bar > span.over { background: var(--danger-fill); }
.bar > span.none { background: var(--text-3); }

/* Lists */
.list { list-style: none; margin: 0; padding: 0; }
.list > li { border-top: 1px solid var(--border); }
.list > li:first-child { border-top: 0; }
.list-btn {
  width: 100%; display: flex; align-items: center; gap: 10px;
  padding: 11px 16px; background: none; border: 0; text-align: left; cursor: pointer;
  min-width: 0;
}
.tx-item { display: flex; align-items: center; padding-right: 6px; }
.tx-item .list-btn { flex: 1; padding-right: 8px; }
.list-btn:hover { background: var(--surface-2); }
.cat-row { display: flex; flex-direction: column; gap: 6px; width: 100%; min-width: 0; }
.date-head { padding: 8px 16px 4px; font-size: 12px; font-weight: 600; color: var(--text-2); background: var(--surface-2); }
.tx-amount { font-weight: 600; font-variant-numeric: tabular-nums; white-space: nowrap; }

/* Buttons and forms */
.btn {
  display: inline-flex; align-items: center; justify-content: center; gap: 6px;
  min-height: 40px; padding: 8px 14px; border-radius: var(--radius);
  border: 1px solid var(--border-strong); background: var(--surface); cursor: pointer;
  font-weight: 500; text-decoration: none;
}
.btn:hover { background: var(--surface-2); }
.btn svg { width: 16px; height: 16px; flex: none; }
.page-head h1 { white-space: nowrap; }
@media (max-width: 420px) {
  .page-head h1 { font-size: 19px; }
  .hide-sm { display: none; }
}
.btn.primary { background: var(--accent); border-color: var(--accent); color: var(--on-accent); }
.btn.primary:hover { filter: brightness(1.06); }
.btn.danger { color: var(--danger); border-color: var(--danger); }
.btn.small { min-height: 32px; padding: 4px 10px; font-size: 13px; }
.btn.block { width: 100%; }
.btn[disabled] { opacity: 0.55; cursor: progress; }
.icon-btn { width: 40px; height: 40px; border-radius: 50%; border: 0; background: none; cursor: pointer; display: inline-flex; align-items: center; justify-content: center; color: var(--text-2); }
.icon-btn:hover { background: var(--surface-2); }
.icon-btn svg { width: 20px; height: 20px; }
.link { background: none; border: 0; padding: 0; color: var(--accent); cursor: pointer; font-size: inherit; text-decoration: underline; text-underline-offset: 2px; }

.field { display: flex; flex-direction: column; gap: 4px; }
.field > span { font-size: 13px; color: var(--text-2); }
.input, select.input, textarea.input {
  width: 100%; min-height: 44px; padding: 9px 12px;
  border: 1px solid var(--border-strong); border-radius: var(--radius);
  background: var(--surface); font-size: 16px;
}
.input:focus { outline: 2px solid var(--accent); outline-offset: 0; border-color: transparent; }
.amount-input { font-size: 30px; font-weight: 600; text-align: center; border: 0; background: transparent; min-height: 56px; font-variant-numeric: tabular-nums; }
.amount-input:focus { outline: none; }
.error { color: var(--danger); font-size: 13px; }

.seg { display: inline-flex; background: var(--surface-2); border-radius: var(--radius); padding: 3px; gap: 2px; }
.seg button { border: 0; background: none; padding: 6px 14px; border-radius: 8px; cursor: pointer; font-weight: 500; color: var(--text-2); }
.seg button[aria-pressed="true"] { background: var(--surface); color: var(--text); box-shadow: 0 1px 2px rgba(0, 0, 0, 0.12); }
.seg.out button[aria-pressed="true"] { color: var(--danger); }
.seg.in button[aria-pressed="true"] { color: var(--success); }

.chips { display: flex; flex-wrap: wrap; gap: 6px; }
.chip {
  border: 1px solid var(--border-strong); background: var(--surface); border-radius: 999px;
  padding: 6px 12px; font-size: 14px; cursor: pointer;
}
.chip[aria-pressed="true"] { background: var(--accent-bg); border-color: var(--accent); color: var(--accent); font-weight: 600; }
.toolbar { display: flex; gap: 8px; flex-wrap: wrap; }
.toolbar .input { flex: 1 1 140px; width: auto; min-width: 0; }
.toolbar .input[type="search"] { flex-basis: 100%; }
@media (min-width: 900px) { .toolbar .input[type="search"] { flex-basis: 260px; } }

/* Sheet (dialog) */
.sheet {
  border: 0; padding: 0; margin: auto auto 0;
  width: 100%; max-width: 560px; max-height: 92vh;
  border-radius: 18px 18px 0 0;
  background: var(--surface); color: var(--text);
}
.sheet::backdrop { background: rgba(0, 0, 0, 0.45); }
.sheet-inner { display: flex; flex-direction: column; gap: 14px; padding: 16px 16px calc(16px + env(safe-area-inset-bottom)); }
.sheet-head { display: flex; align-items: center; justify-content: space-between; }
.sheet-actions { display: flex; gap: 8px; }
.sheet-actions .btn { flex: 1; }

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

/* Chart */
.chart svg { width: 100%; height: auto; display: block; }
.chart text { fill: var(--text-3); font-size: 11px; }
.chart .b-out { fill: var(--danger-fill); }
.chart .b-in { fill: var(--income); }
.chart .grid { stroke: var(--border); }
.legend { display: flex; gap: 14px; font-size: 12px; color: var(--text-2); }
.legend i { display: inline-block; width: 10px; height: 10px; border-radius: 3px; margin-right: 5px; vertical-align: -1px; }

.empty { text-align: center; padding: 28px 16px; color: var(--text-2); }
.spinner { width: 28px; height: 28px; border-radius: 50%; border: 3px solid var(--surface-2); border-top-color: var(--accent); animation: spin 0.8s linear infinite; margin: 0 auto; }
@keyframes spin { to { transform: rotate(360deg); } }

/* Setup */
.setup-wrap { max-width: 440px; margin: 8vh auto 0; display: flex; flex-direction: column; gap: 16px; }
.steps { margin: 0; padding-left: 20px; display: flex; flex-direction: column; gap: 6px; color: var(--text-2); font-size: 14px; }

/* Desktop */
@media (min-width: 900px) {
  .app { display: grid; grid-template-columns: 232px minmax(0, 1fr); }
  .sidebar {
    display: flex; flex-direction: column; gap: 18px;
    position: sticky; top: 0; height: 100vh; padding: 20px 14px;
    background: var(--surface); border-right: 1px solid var(--border);
  }
  .brand { display: flex; align-items: center; gap: 10px; font-weight: 700; font-size: 18px; padding: 0 8px; }
  .nav { display: flex; flex-direction: column; gap: 2px; }
  .nav .nav-item { flex-direction: row; justify-content: flex-start; gap: 12px; font-size: 15px; padding: 10px 12px; border-radius: var(--radius); color: var(--text-2); }
  .nav .nav-item:hover { background: var(--surface-2); }
  .nav .nav-item[aria-current="page"] { background: var(--accent-bg); color: var(--accent); font-weight: 600; }
  .bottom-nav, .fab { display: none; }
  .main { max-width: 1080px; width: 100%; padding: 28px 32px 48px; }
  .toast { bottom: 32px; }
  .desk-2 { display: grid; grid-template-columns: minmax(0, 1.4fr) minmax(0, 1fr); gap: 16px; align-items: start; }
  .metric .value { font-size: 22px; }
  .sheet { margin: auto; border-radius: 18px; }
}
