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

  --cat-food: #2a78d6;
  --cat-transport: #eb6834;
  --cat-cafe: #1baf7a;
  --cat-home: #eda100;
  --cat-health: #e87ba4;
  --cat-fun: #008300;
  --cat-connection: #4a3aa7;
  --cat-other: #e34948;

  --accent: #2a78d6;
  --accent-soft: #cde2fb;
}

@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;
    --grid: #2c2c2a;
    --baseline: #383835;
    --border: rgba(255, 255, 255, 0.10);
    --success: #0ca30c;

    --cat-food: #3987e5;
    --cat-transport: #d95926;
    --cat-cafe: #199e70;
    --cat-home: #c98500;
    --cat-health: #d55181;
    --cat-fun: #008300;
    --cat-connection: #9085e9;
    --cat-other: #e66767;

    --accent: #3987e5;
    --accent-soft: #18314d;
  }
}
:root[data-theme="dark"] {
  color-scheme: dark;
  --surface-1: #1a1a19;
  --page: #0d0d0d;
  --text-primary: #ffffff;
  --text-secondary: #c3c2b7;
  --text-muted: #898781;
  --grid: #2c2c2a;
  --baseline: #383835;
  --border: rgba(255, 255, 255, 0.10);
  --success: #0ca30c;

  --cat-food: #3987e5;
  --cat-transport: #d95926;
  --cat-cafe: #199e70;
  --cat-home: #c98500;
  --cat-health: #d55181;
  --cat-fun: #008300;
  --cat-connection: #9085e9;
  --cat-other: #e66767;

  --accent: #3987e5;
  --accent-soft: #18314d;
}

* { box-sizing: border-box; }
html, body { margin: 0; padding: 0; }
body {
  font-family: system-ui, -apple-system, "Segoe UI", sans-serif;
  background: var(--page);
  color: var(--text-primary);
  min-height: 100vh;
}

.app { max-width: 1180px; margin: 0 auto; padding: 24px 20px 64px; }

.topbar {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  justify-content: space-between;
  gap: 16px;
  margin-bottom: 24px;
}
.brand { font-size: 20px; font-weight: 600; }
.filters { display: flex; align-items: center; gap: 12px; flex-wrap: wrap; }

.range-presets { display: flex; gap: 4px; background: var(--surface-1); border: 1px solid var(--border); border-radius: 10px; padding: 3px; }
.range-presets button {
  border: none; background: transparent; color: var(--text-secondary);
  font: inherit; font-size: 13px; padding: 6px 12px; border-radius: 8px; cursor: pointer;
}
.range-presets button:hover { background: var(--accent-soft); }
.range-presets button.active { background: var(--accent); color: #fff; }

select, input[type="text"], input[type="number"], input[type="password"] {
  font: inherit; font-size: 13px; color: var(--text-primary);
  background: var(--surface-1); border: 1px solid var(--border);
  border-radius: 8px; padding: 7px 10px;
}

.stats-row {
  display: grid;
  gap: 12px;
  margin-bottom: 12px;
}
.stats-row-primary { grid-template-columns: repeat(3, 1fr); margin-bottom: 16px; }
.stat-tile {
  background: var(--surface-1); border: 1px solid var(--border); border-radius: 14px;
  padding: 16px 18px;
}
.stat-label { font-size: 12px; color: var(--text-muted); margin-bottom: 6px; }
.stat-value { font-size: 22px; font-weight: 600; font-variant-numeric: proportional-nums; }
.stat-tile.hero .stat-value { font-size: 28px; }
.stat-tile.hero.income .stat-value { color: var(--success); }
.stat-tile.hero.expense .stat-value { color: var(--text-primary); }
.stat-tile.hero.balance.positive .stat-value { color: var(--success); }
.stat-tile.hero.balance.negative .stat-value { color: var(--critical); }

.card {
  background: var(--surface-1); border: 1px solid var(--border); border-radius: 14px;
  padding: 18px 20px;
  margin-bottom: 12px;
}
.card h2 { font-size: 14px; font-weight: 600; color: var(--text-secondary); margin: 0 0 14px; }

.categories-body { display: flex; align-items: center; gap: 32px; flex-wrap: wrap; }

.donut-wrap { position: relative; width: 200px; height: 200px; flex: none; margin: 0 auto; }
.donut-wrap svg { width: 100%; height: 100%; transform: rotate(-90deg); }
.donut-center {
  position: absolute; inset: 0; display: flex; flex-direction: column;
  align-items: center; justify-content: center; text-align: center; pointer-events: none;
}
#donutCenterValue { font-size: 20px; font-weight: 600; }
.donut-center .muted { font-size: 11px; color: var(--text-muted); }

.category-list {
  list-style: none; margin: 0; padding: 0; flex: 1; min-width: 240px;
  display: grid; grid-template-columns: repeat(auto-fill, minmax(220px, 1fr)); gap: 8px 24px;
}
.category-list li { display: flex; align-items: center; justify-content: space-between; gap: 12px; font-size: 13px; }
.category-list .name { color: var(--text-secondary); }
.category-list .val { color: var(--text-primary); font-variant-numeric: tabular-nums; font-weight: 500; white-space: nowrap; }
.category-list .muted-row { color: var(--text-muted); }

.table-card { overflow-x: auto; }
.table-head { display: flex; align-items: center; justify-content: space-between; margin-bottom: 10px; }
table { width: 100%; border-collapse: collapse; font-size: 13px; }
thead th {
  text-align: left; font-weight: 500; color: var(--text-muted); font-size: 12px;
  padding: 8px 10px; border-bottom: 1px solid var(--grid);
}
tbody tr { position: relative; background: var(--surface-1); }
tbody td { padding: 10px; border-bottom: 1px solid var(--grid); vertical-align: middle; }
tbody tr:last-child td { border-bottom: none; }
td.amount { text-align: right; font-variant-numeric: tabular-nums; font-weight: 500; white-space: nowrap; }
td.amount.income { color: var(--success); }
td.cat .cat-name { font-weight: 500; }
td.cat .cat-date { color: var(--text-secondary); font-size: 12px; white-space: nowrap; margin-left: 8px; }
td.icons { white-space: nowrap; }
.icon-btn {
  border: none; background: transparent; cursor: pointer; font-size: 14px;
  color: var(--text-muted); padding: 4px 6px; border-radius: 6px;
}
.icon-btn:hover { background: var(--accent-soft); color: var(--text-primary); }
.desc { color: var(--text-secondary); }
.receipt-link { color: var(--accent); text-decoration: none; font-size: 12px; }

tbody tr.swiping { transition: none; }
tbody tr.swipe-armed { background: color-mix(in srgb, var(--critical) 15%, var(--surface-1)); }

tr.day-header { background: transparent; }
tr.day-header td {
  padding: 14px 10px 6px; border-bottom: none; color: var(--text-muted);
  font-size: 12px; font-weight: 600; text-transform: uppercase; letter-spacing: .03em;
}
tbody tr.day-header:first-child td { padding-top: 0; }

.empty-state { text-align: center; color: var(--text-muted); padding: 32px 0; font-size: 14px; }

.edit-row td { padding: 10px; }
.edit-row input, .edit-row select { width: 100%; }
.edit-row td.cat .cat-date { display: block; margin-left: 0; margin-top: 4px; }
.edit-actions { display: flex; gap: 6px; justify-content: flex-end; }
.edit-actions button {
  font-size: 12px; padding: 6px 10px; border-radius: 6px; border: 1px solid var(--border);
  background: var(--surface-1); color: var(--text-primary); cursor: pointer;
}
.edit-actions button.primary { background: var(--accent); color: #fff; border-color: var(--accent); }

.login-wrap { min-height: 100vh; display: flex; align-items: center; justify-content: center; }
.login-card {
  background: var(--surface-1); border: 1px solid var(--border); border-radius: 16px;
  padding: 32px 28px; width: 300px; text-align: center;
}
.login-card h1 { font-size: 18px; margin: 0 0 4px; }
.login-card p { color: var(--text-muted); font-size: 13px; margin: 0 0 20px; }
.login-card input { width: 100%; margin-bottom: 12px; text-align: center; }
.login-card button {
  width: 100%; padding: 9px; border-radius: 8px; border: none;
  background: var(--accent); color: #fff; font: inherit; font-size: 14px; cursor: pointer;
}
.login-error { color: #e34948; font-size: 12px; min-height: 16px; margin-top: 10px; }

@media (max-width: 820px) {
  .stats-row-primary { grid-template-columns: repeat(3, 1fr); }

  #expensesTable thead { display: none; }
  #expensesTable, #expensesTable tbody { display: block; width: 100%; }
  #expensesTable tr:not(.day-header) {
    display: grid;
    grid-template-columns: 32px 1fr 84px;
    grid-template-areas:
      "icons cat  amount"
      "icons desc amount";
    column-gap: 8px;
    row-gap: 1px;
    padding: 8px 0;
    border-bottom: 1px solid var(--grid);
    touch-action: pan-y;
    user-select: none;
  }
  #expensesTable tr.edit-row {
    grid-template-areas:
      "icons cat     amount"
      "icons desc    desc"
      "icons actions actions";
    user-select: auto;
  }
  #expensesTable tr:last-child { border-bottom: none; }
  #expensesTable tr.day-header { display: block; }
  #expensesTable tr.day-header td { padding: 14px 0 6px; }
  #expensesTable tr.day-header:first-child td { padding-top: 0; }
  #expensesTable td { display: block; padding: 0; border-bottom: none; }
  #expensesTable td.icons { grid-area: icons; display: flex; flex-direction: column; align-items: center; justify-content: center; gap: 0; }
  #expensesTable td.cat { grid-area: cat; display: flex; flex-wrap: wrap; align-items: baseline; column-gap: 6px; font-size: 13px; }
  #expensesTable td.cat .cat-date { font-size: 11px; margin-left: 0; }
  #expensesTable td.desc { grid-area: desc; font-size: 12px; }
  #expensesTable td.amount { grid-area: amount; align-self: center; text-align: right; font-size: 14px; }
  #expensesTable td.actions { grid-area: actions; margin-top: 4px; }
  #expensesTable .edit-actions { justify-content: flex-end; }
  #expensesTable .icon-btn { padding: 2px 4px; font-size: 13px; }
}
@media (max-width: 560px) {
  .stats-row-primary { grid-template-columns: 1fr; }
}
