:root {
  --bg: #0f1420;
  --bg-elev: #171e2e;
  --bg-elev-2: #1e2740;
  --line: #29344d;
  --text: #e8edf7;
  --text-dim: #9aa7c2;
  --muted: #6f7d9c;
  --accent: #4f8cff;
  --income: #22c1a4;
  --expense: #eb5757;
  --invest: #9b6dff;
  --save: #4f8cff;
  --radius: 12px;
  --shadow: 0 4px 20px rgba(0,0,0,.35);
  color-scheme: dark;
}
:root[data-theme="light"] {
  --bg: #f4f6fb;
  --bg-elev: #ffffff;
  --bg-elev-2: #eef1f8;
  --line: #dde3ee;
  --text: #1a2233;
  --text-dim: #566078;
  --muted: #8a94a8;
  --shadow: 0 4px 18px rgba(20,30,60,.08);
  color-scheme: light;
}

* { box-sizing: border-box; }
html, body { margin: 0; height: 100%; }
body {
  background: var(--bg);
  color: var(--text);
  font: 14px/1.5 -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Helvetica, Arial, sans-serif;
  -webkit-font-smoothing: antialiased;
}
.num { text-align: right; font-variant-numeric: tabular-nums; }
.nowrap { white-space: nowrap; }
.muted { color: var(--muted); }
.small { font-size: 12px; }
h3 { margin: 0 0 12px; font-size: 15px; font-weight: 650; letter-spacing: .01em; }

/* Layout */
.layout { display: grid; grid-template-columns: 216px 1fr; min-height: 100vh; }
.sidebar {
  background: var(--bg-elev); border-right: 1px solid var(--line);
  padding: 18px 12px; display: flex; flex-direction: column; gap: 4px;
}
.brand { display: flex; align-items: center; gap: 10px; font-weight: 700; font-size: 17px; padding: 6px 10px 18px; }
.brand-mark {
  display: grid; place-items: center; width: 28px; height: 28px; border-radius: 8px;
  background: linear-gradient(135deg, var(--accent), var(--income)); color: #fff; font-weight: 800;
}
.nav-btn {
  text-align: left; background: none; border: 0; color: var(--text-dim);
  padding: 9px 12px; border-radius: 9px; cursor: pointer; font-size: 14px; font-weight: 550;
}
.nav-btn:hover { background: var(--bg-elev-2); color: var(--text); }
.nav-btn.active { background: var(--accent); color: #fff; }

.content { display: flex; flex-direction: column; min-width: 0; }
.topbar {
  display: flex; justify-content: space-between; align-items: center; gap: 12px;
  padding: 14px 24px; border-bottom: 1px solid var(--line); background: var(--bg-elev);
  position: sticky; top: 0; z-index: 5;
}
.topbar-left, .topbar-right { display: flex; align-items: center; gap: 12px; }
.view { padding: 24px; display: flex; flex-direction: column; gap: 18px; max-width: 1240px; }

/* Controls */
.filter { display: inline-flex; align-items: center; gap: 8px; color: var(--text-dim); font-size: 13px; }
.filter.grow { flex: 1; }
select, input, .btn {
  font: inherit; color: var(--text); background: var(--bg-elev-2);
  border: 1px solid var(--line); border-radius: 8px; padding: 7px 10px;
}
input[type=search] { width: 100%; }
.btn { cursor: pointer; font-weight: 550; }
.btn:hover { border-color: var(--accent); }
.btn.icon { padding: 7px 10px; }
.btn-ok { background: var(--income); border-color: var(--income); color: #04231d; font-weight: 650; }
.btn-primary { background: var(--accent); border-color: var(--accent); color: #fff; }
.btn-primary:hover { filter: brightness(1.08); }
.icon-btn { background: none; border: 0; color: var(--muted); cursor: pointer; font-size: 13px; }
.icon-btn:hover { color: var(--expense); }
.year-select { font-weight: 650; }
.toolbar { display: flex; flex-wrap: wrap; gap: 14px; align-items: center; }

/* Cards */
.cards { display: grid; grid-template-columns: repeat(auto-fit, minmax(160px, 1fr)); gap: 14px; }
.card { background: var(--bg-elev); border: 1px solid var(--line); border-radius: var(--radius); padding: 16px; box-shadow: var(--shadow); }
.card-label { color: var(--text-dim); font-size: 12px; text-transform: uppercase; letter-spacing: .05em; }
.card-value { font-size: 24px; font-weight: 700; margin-top: 6px; font-variant-numeric: tabular-nums; }
.card-income .card-value { color: var(--income); }
.card-expense .card-value { color: var(--expense); }
.card-invest .card-value { color: var(--invest); }
.card-save .card-value { color: var(--save); }

/* Panels */
.panel { background: var(--bg-elev); border: 1px solid var(--line); border-radius: var(--radius); padding: 18px; box-shadow: var(--shadow); }
.panel-intro h3 { margin-bottom: 4px; }
.panel-intro p { margin: 0; max-width: 70ch; }
.scroll-x { overflow-x: auto; }

/* Tables */
table.grid { width: 100%; border-collapse: collapse; font-size: 13px; }
table.grid th { text-align: left; color: var(--text-dim); font-weight: 600; padding: 8px 10px; border-bottom: 1px solid var(--line); position: sticky; top: 0; background: var(--bg-elev); white-space: nowrap; }
table.grid th.num { text-align: right; }
table.grid td { padding: 7px 10px; border-bottom: 1px solid var(--line); }
table.grid tbody tr:hover { background: var(--bg-elev-2); }
.rowhead { font-weight: 600; white-space: nowrap; }
.avg { color: var(--text-dim); }
.aggregate td.num { min-width: 62px; }
tr.summary td { border-top: 1px solid var(--line); font-weight: 650; background: var(--bg-elev-2); }
tr.tone-income .rowhead, tr.tone-income .num { color: var(--income); }
tr.tone-expense .rowhead, tr.tone-expense .num { color: var(--expense); }
tr.tone-invest .rowhead, tr.tone-invest .num { color: var(--invest); }
tr.tone-save .rowhead, tr.tone-save .num { color: var(--save); }
.table-meta { display: flex; justify-content: space-between; align-items: center; gap: 12px; margin-bottom: 10px; color: var(--text-dim); }
.desc { max-width: 340px; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
tr.is-income td.num { color: var(--income); }
tr.excluded { opacity: .4; }
.cat-dot { display: inline-block; width: 9px; height: 9px; border-radius: 50%; margin-right: 7px; vertical-align: middle; }
.cat-select, .split-input, .kw-input { padding: 4px 6px; font-size: 13px; }
.split-input { width: 62px; }
.kw-input { width: 100%; }
.add-row { display: flex; gap: 10px; margin-top: 14px; align-items: center; }

/* Bar list */
.barlist { display: flex; flex-direction: column; gap: 8px; }
.barlist-row { display: grid; grid-template-columns: 140px 1fr 96px; align-items: center; gap: 12px; }
.barlist-label { color: var(--text-dim); font-size: 13px; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.barlist-track { background: var(--bg-elev-2); border-radius: 6px; height: 12px; overflow: hidden; }
.barlist-fill { display: block; height: 100%; border-radius: 6px; }
.barlist-val { text-align: right; font-variant-numeric: tabular-nums; font-size: 13px; }

/* Charts */
.linechart { width: 100%; height: auto; }
.linechart .grid { stroke: var(--line); stroke-width: 1; }
.linechart .axis { fill: var(--muted); font-size: 10px; }
.legend { display: flex; gap: 18px; margin-top: 10px; }
.legend-item { display: inline-flex; align-items: center; gap: 6px; color: var(--text-dim); font-size: 12px; }
.legend-dot { width: 10px; height: 10px; border-radius: 3px; display: inline-block; }

.import-status { min-height: 18px; }

.topbar-title { font-weight: 650; font-size: 15px; }
.overview-head { display: flex; align-items: center; gap: 14px; }
.overview-head h2 { margin: 0; font-size: 20px; }
.overview-head .spacer { flex: 1; }
.segmented { display: inline-flex; background: var(--bg-elev-2); border: 1px solid var(--line); border-radius: 9px; padding: 3px; gap: 3px; }
.seg { border: 0; background: none; color: var(--text-dim); padding: 6px 14px; border-radius: 7px; cursor: pointer; font-size: 13px; font-weight: 550; }
.seg:hover { color: var(--text); }
.seg.active { background: var(--accent); color: #fff; }
.intro-head { display: flex; align-items: center; justify-content: space-between; gap: 16px; }

/* Modal */
.modal-overlay {
  position: fixed; inset: 0; background: rgba(0,0,0,.55); backdrop-filter: blur(2px);
  display: grid; place-items: center; z-index: 100; padding: 24px;
}
.modal {
  background: var(--bg-elev); border: 1px solid var(--line); border-radius: 14px;
  box-shadow: var(--shadow); width: min(560px, 100%); max-height: 82vh; display: flex;
  flex-direction: column; padding: 18px;
}
.modal-head { display: flex; justify-content: space-between; align-items: center; }
.modal-head h3 { margin: 0; }
.modal-foot { display: flex; justify-content: flex-end; margin-top: 14px; }
.scan-list { overflow-y: auto; margin-top: 12px; display: flex; flex-direction: column; gap: 4px; }
.scan-row {
  display: grid; grid-template-columns: 1fr auto 160px auto; align-items: center; gap: 12px;
  padding: 7px 8px; border-radius: 8px;
}
.scan-row:hover { background: var(--bg-elev-2); }
.scan-row.done { opacity: .5; }
.scan-word { font-weight: 550; }
.scan-count { justify-self: end; }
.scan-tick { color: var(--income); white-space: nowrap; min-width: 70px; }

.restore-opts { display: flex; flex-direction: column; gap: 8px; margin: 12px 0; }
.restore-opt { text-align: left; background: var(--bg-elev-2); border: 1px solid var(--line); border-radius: 10px; padding: 12px 14px; cursor: pointer; }
.restore-opt:hover { border-color: var(--accent); }
.restore-opt-label { font-weight: 600; margin-bottom: 2px; }
.restore-status { min-height: 16px; }

/* Flash toast */
.flash {
  position: fixed; bottom: 24px; left: 50%; transform: translate(-50%, 20px);
  background: var(--bg-elev-2); border: 1px solid var(--line); color: var(--text);
  padding: 12px 18px; border-radius: 10px; box-shadow: var(--shadow); opacity: 0;
  transition: all .3s ease; z-index: 50;
}
.flash.show { opacity: 1; transform: translate(-50%, 0); }

@media (max-width: 760px) {
  .layout { grid-template-columns: 1fr; }
  .sidebar { flex-direction: row; overflow-x: auto; }
}
