:root {
  color-scheme: light;
  --surface-1: #fcfcfb;
  --page: #f9f9f7;
  --text-primary: #0b0b0b;
  --text-secondary: #52514e;
  --text-muted: #898781;
  --gridline: #e1e0d9;
  --baseline: #c3c2b7;
  --border: rgba(11, 11, 11, 0.10);
  --success: #006300;
  --danger: #d03b3b;

  --series-1: #2a78d6; /* blue */
  --series-2: #eb6834; /* orange */
  --series-3: #1baf7a; /* aqua */
  --series-4: #eda100; /* yellow */
  --series-5: #e87ba4; /* magenta */
  --series-6: #008300; /* green */
  --series-7: #4a3aa7; /* violet */
  --series-8: #e34948; /* red */
  --series-other: #898781;

  --seq-100: #cde2fb;
  --seq-400: #3987e5;
}

@media (prefers-color-scheme: dark) {
  :root:where(:not([data-theme="light"])) {
    color-scheme: dark;
    --surface-1: #1a1a19;
    --page: #0d0d0d;
    --text-primary: #ffffff;
    --text-secondary: #c3c2b7;
    --text-muted: #898781;
    --gridline: #2c2c2a;
    --baseline: #383835;
    --border: rgba(255, 255, 255, 0.10);
    --success: #0ca30c;
    --danger: #e66767;

    --series-1: #3987e5;
    --series-2: #d95926;
    --series-3: #199e70;
    --series-4: #c98500;
    --series-5: #d55181;
    --series-6: #008300;
    --series-7: #9085e9;
    --series-8: #e66767;
  }
}
:root[data-theme="dark"] {
  color-scheme: dark;
  --surface-1: #1a1a19;
  --page: #0d0d0d;
  --text-primary: #ffffff;
  --text-secondary: #c3c2b7;
  --text-muted: #898781;
  --gridline: #2c2c2a;
  --baseline: #383835;
  --border: rgba(255, 255, 255, 0.10);
  --success: #0ca30c;
  --danger: #e66767;

  --series-1: #3987e5;
  --series-2: #d95926;
  --series-3: #199e70;
  --series-4: #c98500;
  --series-5: #d55181;
  --series-6: #008300;
  --series-7: #9085e9;
  --series-8: #e66767;
}

* { box-sizing: border-box; }

body {
  margin: 0;
  font-family: system-ui, -apple-system, "Segoe UI", sans-serif;
  background: var(--page);
  color: var(--text-primary);
}

.muted { color: var(--text-muted); font-size: 0.9em; }
.error-text { color: var(--danger); }

.view[hidden] { display: none; }

/* ---- Login ---- */
/* :not([hidden]) keeps this from outranking `.view[hidden] { display: none }` above —
   a plain `#view-login { display: flex }` is an ID selector and would always win the
   cascade regardless of the hidden attribute, showing the login card over the app. */
#view-login:not([hidden]) {
  min-height: 100vh;
  display: flex;
  align-items: center;
  justify-content: center;
}
.login-card {
  background: var(--surface-1);
  border: 1px solid var(--border);
  border-radius: 12px;
  padding: 32px;
  width: min(360px, 90vw);
  display: flex;
  flex-direction: column;
  gap: 14px;
}
.login-card h1 { margin: 0 0 8px; font-size: 1.4rem; }
.login-card label { display: flex; flex-direction: column; gap: 4px; font-size: 0.9rem; color: var(--text-secondary); }

input, select, button {
  font: inherit;
  color: inherit;
}
input[type="text"], input[type="password"], input[type="date"], input[type="number"], select {
  background: var(--surface-1);
  border: 1px solid var(--border);
  border-radius: 8px;
  padding: 8px 10px;
  color: var(--text-primary);
}
button {
  background: var(--series-1);
  color: #fff;
  border: none;
  border-radius: 8px;
  padding: 8px 14px;
  cursor: pointer;
}
button.ghost {
  background: transparent;
  color: var(--text-secondary);
  border: 1px solid var(--border);
}
button:disabled { opacity: 0.5; cursor: default; }

/* ---- App shell ---- */
.topbar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 14px 20px;
  border-bottom: 1px solid var(--border);
}
.topbar h1 { font-size: 1.1rem; margin: 0; }
.topbar-actions { display: flex; align-items: center; gap: 10px; }

.tabs {
  display: flex;
  gap: 4px;
  padding: 8px 20px 0;
  border-bottom: 1px solid var(--border);
  overflow-x: auto;
}
.tab-btn {
  background: none;
  border: none;
  color: var(--text-secondary);
  padding: 8px 12px;
  border-radius: 8px 8px 0 0;
  cursor: pointer;
  white-space: nowrap;
}
.tab-btn.is-active { color: var(--text-primary); background: var(--surface-1); font-weight: 600; }

main { padding: 16px 20px 40px; max-width: 900px; margin: 0 auto; }

.tab-panel { display: none; }
.tab-panel.is-active { display: block; }

.filters-row {
  display: flex;
  gap: 8px;
  flex-wrap: wrap;
  margin-bottom: 16px;
}

/* ---- Transactions ---- */
.tx-list { display: flex; flex-direction: column; gap: 2px; }
.tx-row {
  display: grid;
  grid-template-columns: 90px 1fr auto auto;
  align-items: center;
  gap: 10px;
  padding: 10px 8px;
  border-radius: 8px;
}
.tx-row:hover { background: var(--surface-1); }
.tx-date { color: var(--text-muted); font-size: 0.85rem; font-variant-numeric: tabular-nums; }
.tx-desc { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.tx-amount { font-variant-numeric: tabular-nums; font-weight: 600; text-align: right; }
.tx-amount.negative { color: var(--text-primary); }
.tx-amount.positive { color: var(--success); }
.tx-category-select { max-width: 140px; }

.pager { display: flex; align-items: center; justify-content: center; gap: 12px; margin-top: 12px; }

/* ---- Accounts ---- */
.accounts-list { display: flex; flex-direction: column; gap: 10px; }
.account-card {
  background: var(--surface-1);
  border: 1px solid var(--border);
  border-radius: 10px;
  padding: 12px 16px;
  display: flex;
  justify-content: space-between;
  align-items: center;
}
.account-card .account-balance { font-weight: 600; font-variant-numeric: tabular-nums; }
.account-card.is-archived { opacity: 0.5; }

/* ---- Categories ---- */
.category-form { display: flex; gap: 8px; margin-bottom: 16px; }
.category-form input[type="text"] { flex: 1; }
.categories-list { display: flex; flex-direction: column; gap: 6px; }
.category-row {
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 8px 10px;
  border-radius: 8px;
  border: 1px solid var(--border);
}
.category-swatch { width: 14px; height: 14px; border-radius: 4px; flex-shrink: 0; }
.category-row .category-name { flex: 1; }

/* ---- Charts (dataviz) ---- */
.chart-card {
  background: var(--surface-1);
  border: 1px solid var(--border);
  border-radius: 10px;
  padding: 16px;
  margin-bottom: 24px;
}
.bar-row {
  display: grid;
  grid-template-columns: 120px 1fr 90px;
  align-items: center;
  gap: 10px;
  padding: 6px 0;
  position: relative;
}
.bar-row-label {
  display: flex;
  align-items: center;
  gap: 6px;
  font-size: 0.9rem;
  color: var(--text-secondary);
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}
.bar-swatch { width: 10px; height: 10px; border-radius: 3px; flex-shrink: 0; }
.bar-track {
  background: var(--gridline);
  border-radius: 4px;
  height: 20px;
  position: relative;
  overflow: hidden;
}
.bar-fill {
  height: 100%;
  border-radius: 4px 0 0 4px;
  min-width: 2px;
}
.bar-value {
  font-variant-numeric: tabular-nums;
  font-size: 0.85rem;
  text-align: right;
  color: var(--text-primary);
}
.bar-row[data-tooltip] { cursor: default; }
.bar-row .tooltip {
  position: absolute;
  top: -6px;
  left: 130px;
  transform: translateY(-100%);
  background: var(--text-primary);
  color: var(--surface-1);
  padding: 4px 8px;
  border-radius: 6px;
  font-size: 0.8rem;
  white-space: nowrap;
  pointer-events: none;
  opacity: 0;
  transition: opacity 0.1s;
  z-index: 10;
}
.bar-row:hover .tooltip, .bar-row:focus-within .tooltip { opacity: 1; }

.month-bars {
  display: flex;
  align-items: flex-end;
  gap: 6px;
  height: 160px;
  padding-top: 20px;
}
.month-bar-col {
  flex: 1;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: flex-end;
  height: 100%;
  position: relative;
}
.month-bar {
  width: 100%;
  max-width: 24px;
  background: var(--series-1);
  border-radius: 4px 4px 0 0;
}
.month-bar-label { font-size: 0.7rem; color: var(--text-muted); margin-top: 4px; }
.month-bar-col .tooltip {
  position: absolute;
  bottom: 100%;
  margin-bottom: 6px;
  background: var(--text-primary);
  color: var(--surface-1);
  padding: 3px 7px;
  border-radius: 6px;
  font-size: 0.75rem;
  white-space: nowrap;
  opacity: 0;
  pointer-events: none;
  transition: opacity 0.1s;
}
.month-bar-col:hover .tooltip, .month-bar-col:focus-within .tooltip { opacity: 1; }

.empty-state { color: var(--text-muted); padding: 20px 0; text-align: center; }
