/* ---------- Design tokens ---------- */
:root {
  --bg: #f6f7f9;
  --surface: #ffffff;
  --surface-2: #f1f3f6;
  --tab-active: #ffffff;
  --border: #e3e6eb;
  --text: #111827;
  --text-2: #4b5563;
  --text-muted: #6b7280;
  --primary: #0d9488;
  --primary-hover: #0f766e;
  --primary-soft: #ccfbf1;
  --on-primary: #ffffff;
  --success: #15803d;
  --success-soft: #dcfce7;
  --warning: #b45309;
  --warning-soft: #fef3c7;
  --danger: #dc2626;
  --danger-soft: #fee2e2;
  --focus: #0d948866;
  --shadow: 0 1px 2px rgb(16 24 40 / 0.05), 0 1px 3px rgb(16 24 40 / 0.06);
  --shadow-lg: 0 20px 40px -12px rgb(16 24 40 / 0.25);

  --radius-sm: 8px;
  --radius: 12px;
  --radius-lg: 16px;
  --s1: 4px; --s2: 8px; --s3: 12px; --s4: 16px; --s5: 24px; --s6: 32px;

  --fs-xs: 0.75rem;
  --fs-sm: 0.875rem;
  --fs-md: 1rem;
  --fs-lg: 1.125rem;
  --fs-xl: 1.5rem;
  --fs-2xl: 1.75rem;

  color-scheme: light;
}

@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) {
    --bg: #0b0f14;
    --surface: #131921;
    --surface-2: #1b232d;
    --tab-active: #2c3746;
    --border: #263040;
    --text: #f3f4f6;
    --text-2: #c2c8d1;
    --text-muted: #8b95a3;
    --primary: #2dd4bf;
    --primary-hover: #5eead4;
    --primary-soft: #134e4a;
    --on-primary: #042f2e;
    --success: #4ade80;
    --success-soft: #14532d;
    --warning: #fbbf24;
    --warning-soft: #451a03;
    --danger: #f87171;
    --danger-soft: #450a0a;
    --focus: #2dd4bf66;
    --shadow: 0 1px 2px rgb(0 0 0 / 0.4);
    --shadow-lg: 0 20px 40px -12px rgb(0 0 0 / 0.7);
    color-scheme: dark;
  }
}
:root[data-theme="dark"] {
  --bg: #0b0f14;
  --surface: #131921;
  --surface-2: #1b232d;
  --tab-active: #2c3746;
  --border: #263040;
  --text: #f3f4f6;
  --text-2: #c2c8d1;
  --text-muted: #8b95a3;
  --primary: #2dd4bf;
  --primary-hover: #5eead4;
  --primary-soft: #134e4a;
  --on-primary: #042f2e;
  --success: #4ade80;
  --success-soft: #14532d;
  --warning: #fbbf24;
  --warning-soft: #451a03;
  --danger: #f87171;
  --danger-soft: #450a0a;
  --focus: #2dd4bf66;
  --shadow: 0 1px 2px rgb(0 0 0 / 0.4);
  --shadow-lg: 0 20px 40px -12px rgb(0 0 0 / 0.7);
  color-scheme: dark;
}

/* ---------- Base ---------- */
*, *::before, *::after { box-sizing: border-box; }
html { -webkit-text-size-adjust: 100%; }
body {
  margin: 0;
  background: var(--bg);
  color: var(--text);
  font: 400 var(--fs-md)/1.5 Inter, system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
  -webkit-font-smoothing: antialiased;
  font-feature-settings: "tnum" 1, "cv11" 1;
}
h1, h2, p { margin: 0; }
h2 { font-size: var(--fs-md); font-weight: 600; }
button, input, select { font: inherit; color: inherit; }
code { font-size: 0.85em; background: var(--surface-2); padding: 1px 5px; border-radius: 4px; }
hr { border: 0; border-top: 1px solid var(--border); margin: var(--s4) 0; }
[hidden] { display: none !important; }
.sr-only { position: absolute; width: 1px; height: 1px; overflow: hidden; clip: rect(0 0 0 0); white-space: nowrap; }
.muted { color: var(--text-muted); }
.small { font-size: var(--fs-xs); }
:focus-visible { outline: 3px solid var(--focus); outline-offset: 2px; border-radius: 6px; }

/* ---------- Top bar ---------- */
.topbar {
  position: sticky; top: 0; z-index: 10;
  display: flex; align-items: center; gap: var(--s4);
  padding: var(--s3) var(--s5);
  background: color-mix(in srgb, var(--surface) 85%, transparent);
  backdrop-filter: saturate(1.4) blur(10px);
  border-bottom: 1px solid var(--border);
}
.brand { display: flex; align-items: center; gap: var(--s2); font-weight: 700; }
.brand-mark { color: var(--primary); display: inline-flex; }
.tabs { display: flex; gap: var(--s1); margin-inline: auto; background: var(--surface-2); padding: 4px; border-radius: 999px; }
.tab {
  border: 0; background: transparent; padding: 6px 14px; border-radius: 999px;
  font-size: var(--fs-sm); font-weight: 500; color: var(--text-2); cursor: pointer;
  transition: background .15s, color .15s;
}
.tab:hover { color: var(--text); }
.tab[aria-selected="true"] { background: var(--tab-active); color: var(--text); box-shadow: var(--shadow); }
.topbar-actions { display: flex; gap: var(--s2); align-items: center; }

/* ---------- Buttons ---------- */
.btn {
  display: inline-flex; align-items: center; justify-content: center; gap: 6px;
  min-height: 40px; padding: 0 16px; border-radius: var(--radius-sm);
  border: 1px solid transparent; font-size: var(--fs-sm); font-weight: 600;
  cursor: pointer; transition: background .15s, border-color .15s, color .15s, transform .05s;
  white-space: nowrap;
}
.btn:active { transform: translateY(1px); }
.btn-sm { min-height: 32px; padding: 0 12px; }
.btn-primary { background: var(--primary); color: var(--on-primary); }
.btn-primary:hover { background: var(--primary-hover); }
.btn-secondary { background: var(--surface); border-color: var(--border); color: var(--text); }
.btn-secondary:hover { background: var(--surface-2); }
.btn-ghost { background: transparent; color: var(--text-2); }
.btn-ghost:hover { background: var(--surface-2); color: var(--text); }
.btn-danger { background: var(--danger); color: #fff; }
.btn-danger:hover { filter: brightness(1.08); }
.btn-danger-ghost { background: transparent; color: var(--danger); }
.btn-danger-ghost:hover { background: var(--danger-soft); }
.icon-btn {
  display: inline-grid; place-items: center; width: 40px; height: 40px;
  border: 0; background: transparent; border-radius: var(--radius-sm); color: var(--text-2); cursor: pointer;
}
.icon-btn:hover { background: var(--surface-2); color: var(--text); }
.link-btn { border: 0; background: none; color: var(--primary); font-weight: 600; font-size: var(--fs-sm); cursor: pointer; padding: 4px; }
.link-btn:hover { text-decoration: underline; }
.btn-row { display: flex; flex-wrap: wrap; gap: var(--s2); margin: var(--s3) 0; }
.file-btn { position: relative; }

.i-moon { display: none; }
:root[data-theme="dark"] .i-sun { display: none; }
:root[data-theme="dark"] .i-moon { display: block; }
@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) .i-sun { display: none; }
  :root:not([data-theme="light"]) .i-moon { display: block; }
}

/* ---------- Layout ---------- */
.container { max-width: 1120px; margin: 0 auto; padding: var(--s5) var(--s5) 96px; }
.month-bar { display: flex; align-items: center; gap: var(--s2); margin-bottom: var(--s5); }
.month-label { font-size: var(--fs-2xl); font-weight: 700; letter-spacing: -0.02em; min-width: 11ch; text-align: center; }
.month-bar .btn { margin-left: auto; }
.grid-2 { display: grid; grid-template-columns: repeat(2, minmax(0, 1fr)); gap: var(--s4); margin-bottom: var(--s4); }
.stack { display: grid; gap: var(--s4); align-content: start; }

.card {
  background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius-lg);
  padding: var(--s5); box-shadow: var(--shadow); min-width: 0;
}
.card-head { display: flex; align-items: flex-start; justify-content: space-between; gap: var(--s3); margin-bottom: var(--s4); }
.card-head p { margin-top: 4px; font-size: var(--fs-sm); max-width: 60ch; }

/* ---------- KPIs ---------- */
.kpis { display: grid; grid-template-columns: repeat(4, minmax(0, 1fr)); gap: var(--s4); margin-bottom: var(--s4); }
.kpi { padding: var(--s4) var(--s5); }
.kpi-label { font-size: var(--fs-sm); color: var(--text-muted); font-weight: 500; }
.kpi-value { font-size: var(--fs-xl); font-weight: 700; letter-spacing: -0.02em; margin: 4px 0 2px; }
.kpi-sub { font-size: var(--fs-xs); color: var(--text-muted); }
.pos { color: var(--success); }
.neg { color: var(--danger); }
.warn { color: var(--warning); }

.bar { height: 6px; background: var(--surface-2); border-radius: 999px; overflow: hidden; margin: 6px 0; }
.bar > span { display: block; height: 100%; width: 0; background: var(--primary); border-radius: inherit; transition: width .4s ease; }
.bar.is-warn > span { background: var(--warning); }
.bar.is-over > span { background: var(--danger); }

/* ---------- Charts ---------- */
.chart-box { position: relative; }
.trend-box { height: 260px; }
.donut-wrap { display: grid; grid-template-columns: 200px 1fr; gap: var(--s5); align-items: center; }
.donut-box { height: 200px; }
.legend { list-style: none; margin: 0; padding: 0; display: grid; gap: var(--s2); font-size: var(--fs-sm); }
.legend li { display: grid; grid-template-columns: 10px 1fr auto auto; gap: var(--s2); align-items: center; }
.legend .amt { font-weight: 600; }
.legend .pct { color: var(--text-muted); min-width: 3.5ch; text-align: right; }
.dot { display: inline-block; width: 10px; height: 10px; border-radius: 3px; }
.dot-income { background: var(--success); }
.dot-expense { background: var(--primary); }
.mini-legend { display: flex; gap: var(--s3); font-size: var(--fs-xs); color: var(--text-muted); }
.mini-legend span { display: inline-flex; align-items: center; gap: 6px; }

/* ---------- Lists ---------- */
.tx-list, .budget-list, .budget-edit, .cat-list { list-style: none; margin: 0; padding: 0; }
.tx {
  display: grid; grid-template-columns: 36px 1fr auto; gap: var(--s3); align-items: center;
  padding: 10px var(--s2); margin: 0 calc(var(--s2) * -1); border-radius: var(--radius-sm); cursor: pointer;
}
.tx:hover { background: var(--surface-2); }
.tx-icon {
  width: 36px; height: 36px; border-radius: 10px; display: grid; place-items: center;
  font-weight: 700; font-size: var(--fs-sm); color: #fff;
}
.tx-main { min-width: 0; }
.tx-title { font-weight: 500; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.tx-meta { font-size: var(--fs-xs); color: var(--text-muted); }
.tx-amt { font-weight: 600; white-space: nowrap; }
.tx-amt.income { color: var(--success); }

.tx-group + .tx-group { margin-top: var(--s4); }
.tx-group-head {
  display: flex; justify-content: space-between; font-size: var(--fs-xs); font-weight: 600;
  text-transform: uppercase; letter-spacing: .04em; color: var(--text-muted);
  padding: var(--s2) 0; border-bottom: 1px solid var(--border); margin-bottom: var(--s1);
}

.budget-list li + li { margin-top: var(--s4); }
.budget-row-top { display: flex; justify-content: space-between; gap: var(--s2); font-size: var(--fs-sm); }
.budget-row-top .name { font-weight: 500; display: inline-flex; align-items: center; gap: 8px; }
.budget-row-top .nums { color: var(--text-muted); }
.budget-row-top .nums b { color: var(--text); font-weight: 600; }

.budget-edit li {
  display: grid; grid-template-columns: minmax(0, 1fr) 140px; gap: var(--s4); align-items: center;
  padding: var(--s3) 0; border-top: 1px solid var(--border);
}
.budget-edit li:first-child { border-top: 0; }
.budget-edit .meta { font-size: var(--fs-xs); color: var(--text-muted); margin-top: 2px; }

.cat-list li {
  display: flex; align-items: center; gap: var(--s3); padding: 8px 0; border-top: 1px solid var(--border);
}
.cat-list li:first-child { border-top: 0; }
.cat-list .cat-name { flex: 1; min-width: 0; }
.cat-list input[type="color"] { width: 28px; height: 28px; padding: 0; border: 1px solid var(--border); border-radius: 8px; background: none; cursor: pointer; }
.cat-list input[type="color"]::-webkit-color-swatch-wrapper { padding: 2px; }
.cat-list input[type="color"]::-webkit-color-swatch { border: 0; border-radius: 6px; }
.badge { font-size: var(--fs-xs); padding: 2px 8px; border-radius: 999px; background: var(--surface-2); color: var(--text-2); }
.badge.income { background: var(--success-soft); color: var(--success); }

/* ---------- Forms ---------- */
.field { display: grid; gap: 6px; min-width: 0; }
.field-grow { flex: 1; }
.field-label { font-size: var(--fs-sm); font-weight: 500; color: var(--text-2); }
.field-error { font-size: var(--fs-xs); color: var(--danger); }
input:not([type="radio"]):not([type="color"]):not([type="file"]), select {
  width: 100%; min-height: 40px; padding: 8px 12px; background: var(--surface);
  border: 1px solid var(--border); border-radius: var(--radius-sm); outline: none;
  transition: border-color .15s, box-shadow .15s;
}
input:focus, select:focus { border-color: var(--primary); box-shadow: 0 0 0 3px var(--focus); }
input[aria-invalid="true"] { border-color: var(--danger); }
.filters { display: flex; flex-wrap: wrap; gap: var(--s2); }
.filter-summary { font-size: var(--fs-sm); color: var(--text-muted); margin: var(--s3) 0 var(--s2); }
.inline-form { display: flex; gap: var(--s2); margin-bottom: var(--s3); flex-wrap: wrap; }
.row-2 { display: grid; grid-template-columns: 1fr 1fr; gap: var(--s3); }
.budget-input { position: relative; }
.budget-input span { position: absolute; left: 12px; top: 50%; transform: translateY(-50%); color: var(--text-muted); font-size: var(--fs-sm); pointer-events: none; }
.budget-input input { padding-left: 28px !important; text-align: right; }

/* ---------- Empty states ---------- */
.empty { text-align: center; padding: var(--s6) var(--s4); display: grid; gap: 6px; justify-items: center; }
.empty-title { font-weight: 600; }
.empty-sub { color: var(--text-muted); font-size: var(--fs-sm); max-width: 36ch; }
.empty .btn { margin-top: var(--s2); }

/* ---------- Danger zone ---------- */
.danger-zone { display: flex; align-items: center; justify-content: space-between; gap: var(--s4); }
.danger-title { font-weight: 600; color: var(--danger); }

/* ---------- Dialog ---------- */
dialog {
  width: min(460px, calc(100vw - 32px)); padding: 0; border: 1px solid var(--border);
  border-radius: var(--radius-lg); background: var(--surface); color: var(--text); box-shadow: var(--shadow-lg);
}
dialog::backdrop { background: rgb(9 12 18 / 0.5); backdrop-filter: blur(2px); }
dialog[open] { animation: pop .18s ease-out; }
@keyframes pop { from { opacity: 0; transform: translateY(8px) scale(.98); } }
#txForm { display: grid; gap: var(--s4); padding: var(--s5); }
.dialog-head { display: flex; align-items: center; justify-content: space-between; margin: -8px -8px 0 0; }
.dialog-head h2 { font-size: var(--fs-lg); }
.dialog-foot { display: flex; gap: var(--s2); align-items: center; margin-top: var(--s2); }
.spacer { flex: 1; }

.segmented { display: grid; grid-template-columns: 1fr 1fr; background: var(--surface-2); padding: 4px; border-radius: var(--radius-sm); }
.segmented label { position: relative; }
.segmented input { position: absolute; opacity: 0; inset: 0; }
.segmented span {
  display: block; text-align: center; padding: 8px; border-radius: 6px; font-weight: 600;
  font-size: var(--fs-sm); color: var(--text-2); cursor: pointer;
}
.segmented input:checked + span { background: var(--surface); color: var(--text); box-shadow: var(--shadow); }
.segmented input:focus-visible + span { outline: 3px solid var(--focus); }

.amount-input { display: flex; align-items: center; border: 1px solid var(--border); border-radius: var(--radius-sm); padding-left: 14px; background: var(--surface); }
.amount-input:focus-within { border-color: var(--primary); box-shadow: 0 0 0 3px var(--focus); }
.amount-input:has([aria-invalid="true"]) { border-color: var(--danger); }
.amount-input span { color: var(--text-muted); font-size: var(--fs-xl); font-weight: 600; }
.amount-input input { border: 0 !important; box-shadow: none !important; font-size: var(--fs-xl) !important; font-weight: 700; min-height: 56px !important; }

/* ---------- Toast ---------- */
.toast {
  position: fixed; left: 50%; bottom: 24px; transform: translateX(-50%); z-index: 50;
  display: flex; align-items: center; gap: var(--s4);
  background: var(--text); color: var(--bg); padding: 10px 16px; border-radius: var(--radius);
  box-shadow: var(--shadow-lg); font-size: var(--fs-sm); max-width: calc(100vw - 32px);
}
.toast .link-btn { color: var(--primary-soft); }
:root[data-theme="dark"] .toast .link-btn { color: #0f766e; }

/* ---------- Responsive ---------- */
@media (max-width: 900px) {
  .kpis { grid-template-columns: repeat(2, minmax(0, 1fr)); }
  .grid-2 { grid-template-columns: minmax(0, 1fr); }
}
@media (max-width: 720px) {
  .topbar { flex-wrap: wrap; padding: var(--s3) var(--s4); gap: var(--s2); }
  .topbar-actions { margin-left: auto; }
  .tabs { order: 3; width: 100%; margin: 0; overflow-x: auto; scrollbar-width: none; }
  .tabs::-webkit-scrollbar { display: none; }
  .tab { flex: 1; padding: 6px 4px; font-size: 0.8125rem; }
  .container { padding: var(--s4) var(--s4) 96px; }
  .month-label { font-size: var(--fs-xl); min-width: 0; }
  .card { padding: var(--s4); }
  .kpi { padding: var(--s3) var(--s4); }
  .kpi-value { font-size: var(--fs-lg); }
  .donut-wrap { grid-template-columns: minmax(0, 1fr); }
  .donut-box { height: 180px; }
  .trend-box { height: 220px; }
  .filters .field:not(.field-grow) { flex: 1 1 40%; }
  .filters .field-grow { flex-basis: 100%; }
  .budget-edit li { grid-template-columns: minmax(0, 1fr) 120px; }
  .danger-zone { flex-direction: column; align-items: stretch; }
  .row-2 { grid-template-columns: 1fr; }
  #addBtn span { display: none; }
  #addBtn { padding: 0 12px; }
}

@media (prefers-reduced-motion: reduce) {
  *, *::before, *::after { animation: none !important; transition: none !important; }
}
