:root {
  color-scheme: light;
  --surface: #fcfcfb;
  --card: #ffffff;
  --surface-2: #f2f1ee;
  --border: #e3e2dd;
  --text: #0b0b0b;
  --text-muted: #52514e;
  --on-accent: #ffffff;
  --accent: #2a78d6;
  --accent-2: #eb6834;
  --danger: #e34948;
  --good: #1baf7a;
  --warn: #eda100;
  --flash-bg: #fff7e6;
  --flash-error-bg: #fdecec;
  --flash-success-bg: #eafaf3;
  --radius: 10px;
}

/* System dark mode applies unless the user explicitly picked light via the toggle. */
@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) {
    color-scheme: dark;
    --surface: #1a1a19;
    --card: #242322;
    --surface-2: #2c2b29;
    --border: #3a3937;
    --text: #ffffff;
    --text-muted: #c3c2b7;
    --accent: #3987e5;
    --accent-2: #d95926;
    --danger: #e66767;
    --good: #199e70;
    --warn: #c98500;
    --flash-bg: #3a3220;
    --flash-error-bg: #3a2323;
    --flash-success-bg: #1e3329;
  }
}

/* Explicit toggle (localStorage-backed) wins over the OS setting in both directions. */
:root[data-theme="dark"] {
  color-scheme: dark;
  --surface: #1a1a19;
  --card: #242322;
  --surface-2: #2c2b29;
  --border: #3a3937;
  --text: #ffffff;
  --text-muted: #c3c2b7;
  --accent: #3987e5;
  --accent-2: #d95926;
  --danger: #e66767;
  --good: #199e70;
  --warn: #c98500;
  --flash-bg: #3a3220;
  --flash-error-bg: #3a2323;
  --flash-success-bg: #1e3329;
}

* { box-sizing: border-box; }

body {
  margin: 0;
  padding: 0 0 76px 0;
  background: var(--surface);
  color: var(--text);
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Arial, sans-serif;
  font-size: 16px;
  line-height: 1.4;
}

.topbar {
  display: flex;
  justify-content: space-between;
  align-items: center;
  gap: 12px;
  padding: 14px 16px;
  border-bottom: 1px solid var(--border);
  background: var(--surface);
  position: sticky;
  top: 0;
  z-index: 10;
}
.topbar-title { font-weight: 700; font-size: 17px; margin-right: auto; }
.topbar-logout { color: var(--text-muted); font-size: 14px; text-decoration: none; }

.theme-toggle {
  background: none;
  border: 1px solid var(--border);
  border-radius: 999px;
  width: 32px;
  height: 32px;
  font-size: 15px;
  line-height: 1;
  cursor: pointer;
  color: var(--text);
  display: flex;
  align-items: center;
  justify-content: center;
}

.content {
  max-width: 640px;
  margin: 0 auto;
  padding: 16px;
}

h1 { font-size: 20px; margin: 0 0 16px; }
h2 { font-size: 16px; margin: 24px 0 8px; color: var(--text-muted); }

.card {
  background: var(--card);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 16px;
  margin-bottom: 12px;
}

.row {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 8px 0;
  border-bottom: 1px solid var(--border);
}
.row:last-child { border-bottom: none; }
.row-title { font-weight: 500; }
.row-sub { color: var(--text-muted); font-size: 13px; }
.row-value { font-weight: 600; white-space: nowrap; }
.row-value.expense { color: var(--danger); }
.row-value.income { color: var(--good); }

a.row-link { color: inherit; text-decoration: none; display: block; }
a.row-link:hover .row-title { text-decoration: underline; }

.currency-total {
  font-style: italic;
  color: var(--text-muted);
  font-size: 14px;
}

form { display: flex; flex-direction: column; gap: 12px; }

label { font-size: 14px; color: var(--text-muted); display: block; margin-bottom: 4px; }

input[type="text"], input[type="password"], input[type="number"], select, textarea {
  width: 100%;
  padding: 10px 12px;
  border: 1px solid var(--border);
  border-radius: var(--radius);
  font-size: 16px;
  background: var(--card);
  color: var(--text);
}
textarea { resize: vertical; min-height: 60px; }

.field { margin-bottom: 4px; }

.btn {
  display: inline-block;
  padding: 10px 18px;
  border-radius: var(--radius);
  border: none;
  background: var(--accent);
  color: var(--on-accent);
  font-size: 15px;
  font-weight: 600;
  cursor: pointer;
  text-decoration: none;
  text-align: center;
}
.btn:hover { opacity: 0.9; }
.btn-secondary { background: var(--surface-2); color: var(--text); }
.btn-danger { background: var(--danger); }
.btn-block { display: block; width: 100%; }
.btn-row { display: flex; gap: 8px; flex-wrap: wrap; }
.btn-sm { padding: 6px 12px; font-size: 13px; }

.grid-2 { display: grid; grid-template-columns: 1fr 1fr; gap: 8px; }
.grid-3 { display: grid; grid-template-columns: repeat(3, 1fr); gap: 8px; }

.pill-group { display: flex; gap: 8px; flex-wrap: wrap; margin-bottom: 8px; }
.pill {
  padding: 8px 14px;
  border-radius: 999px;
  border: 1px solid var(--border);
  background: var(--card);
  color: var(--text);
  text-decoration: none;
  font-size: 14px;
  cursor: pointer;
}
.pill.active { background: var(--accent); color: var(--on-accent); border-color: var(--accent); }

.flash {
  background: var(--flash-bg);
  border: 1px solid var(--warn);
  border-radius: var(--radius);
  padding: 10px 14px;
  margin-bottom: 12px;
  font-size: 14px;
  color: var(--text);
}
.flash.error { background: var(--flash-error-bg); border-color: var(--danger); }
.flash.success { background: var(--flash-success-bg); border-color: var(--good); }

.error-text { color: var(--danger); font-size: 14px; margin: 0; }

.muted { color: var(--text-muted); }
.center { text-align: center; }

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

.chart-img { width: 100%; height: auto; border-radius: var(--radius); border: 1px solid var(--border); }

.tabbar {
  position: fixed;
  bottom: 0;
  left: 0;
  right: 0;
  display: flex;
  background: var(--card);
  border-top: 1px solid var(--border);
  padding: 6px 0 max(6px, env(safe-area-inset-bottom));
  z-index: 10;
}
.tabbar a {
  flex: 1;
  text-align: center;
  text-decoration: none;
  color: var(--text-muted);
  font-size: 20px;
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 2px;
  padding: 4px 0;
}
.tabbar a span { font-size: 11px; }
.tabbar a.active { color: var(--accent); }

.auth-wrap {
  max-width: 380px;
  margin: 40px auto;
  padding: 0 16px;
}
.auth-wrap h1 { text-align: center; margin-bottom: 24px; }

.kv { display: flex; justify-content: space-between; gap: 12px; padding: 4px 0; }
.kv .k { color: var(--text-muted); }

.badge {
  display: inline-block;
  padding: 2px 8px;
  border-radius: 999px;
  font-size: 12px;
  font-weight: 600;
}
.badge-green { background: var(--flash-success-bg); color: var(--good); }
.badge-yellow { background: var(--flash-bg); color: var(--warn); }
.badge-red { background: var(--flash-error-bg); color: var(--danger); }

.quick-add {
  display: flex;
  gap: 8px;
}
.quick-add input { flex: 1; }

@media (max-width: 400px) {
  .content { padding: 12px; }
}
