:root {
  --page-plane: #f4f5f7;
  --surface-1: #ffffff;
  --surface-2: #f8f9fb;
  --text-primary: #14161a;
  --text-secondary: #52545c;
  --text-muted: #8a8d96;
  --gridline: #e7e8ec;
  --baseline: #cfd1d8;
  --border: rgba(11, 11, 11, 0.08);
  --good: #0ca30c;
  --good-text: #027a02;
  --critical: #d03b3b;
  --sequential: #2a78d6;
  --cat-1: #2a78d6;
  --cat-2: #1baf7a;
  --accent: #2a5fd6;
  --accent-2: #6d3ad6;
  --accent-rgb: 42, 95, 214;
  --cat-1-rgb: 42, 120, 214;
  --cat-2-rgb: 27, 175, 122;
  --shadow-sm: 0 1px 2px rgba(20, 22, 26, 0.04), 0 1px 1px rgba(20, 22, 26, 0.03);
  --shadow-md: 0 8px 24px rgba(20, 22, 26, 0.08), 0 2px 6px rgba(20, 22, 26, 0.04);
}

@media (prefers-color-scheme: dark) {
  :root {
    --page-plane: #0b0c0f;
    --surface-1: #16181d;
    --surface-2: #1c1f26;
    --text-primary: #f4f5f7;
    --text-secondary: #b7b9c2;
    --text-muted: #7a7d87;
    --gridline: #262931;
    --baseline: #383b45;
    --border: rgba(255, 255, 255, 0.08);
    --good: #17c964;
    --good-text: #2fe07d;
    --critical: #f16565;
    --sequential: #4d90eb;
    --cat-1: #4d90eb;
    --cat-2: #2ed195;
    --accent: #5b8bf0;
    --accent-2: #9a6bf0;
    --accent-rgb: 91, 139, 240;
    --cat-1-rgb: 77, 144, 235;
    --cat-2-rgb: 46, 209, 149;
    --shadow-sm: 0 1px 2px rgba(0, 0, 0, 0.3);
    --shadow-md: 0 12px 32px rgba(0, 0, 0, 0.45), 0 2px 8px rgba(0, 0, 0, 0.3);
  }
}

* { box-sizing: border-box; }

body.tracker-body {
  margin: 0;
  font-family: system-ui, -apple-system, "Segoe UI", sans-serif;
  background:
    radial-gradient(1200px 600px at 12% -10%, rgba(42, 95, 214, 0.10), transparent 60%),
    radial-gradient(1000px 500px at 100% 0%, rgba(109, 58, 214, 0.07), transparent 55%),
    var(--page-plane);
  color: var(--text-primary);
  min-height: 100vh;
  -webkit-font-smoothing: antialiased;
}

/* Password gate */
#gate {
  min-height: 100vh;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 20px;
}

.gate-card {
  background: var(--surface-1);
  border: 1px solid var(--border);
  border-radius: 16px;
  padding: 40px 34px;
  width: 100%;
  max-width: 340px;
  text-align: center;
  box-shadow: var(--shadow-md);
}

.gate-card h1 {
  font-size: 1.35em;
  margin: 0 0 6px;
  letter-spacing: -0.01em;
}

.gate-card p {
  color: var(--text-secondary);
  font-size: 0.9em;
  margin: 0 0 22px;
}

.gate-card input {
  width: 100%;
  padding: 11px 13px;
  border-radius: 10px;
  border: 1px solid var(--border);
  background: var(--surface-2);
  color: var(--text-primary);
  font-size: 1em;
  margin-bottom: 14px;
}

.gate-card button {
  width: 100%;
  padding: 11px 12px;
  border-radius: 10px;
  border: none;
  background: linear-gradient(135deg, var(--accent), var(--accent-2));
  color: white;
  font-size: 1em;
  font-weight: 600;
  cursor: pointer;
  box-shadow: 0 4px 14px rgba(42, 95, 214, 0.35);
}

.gate-error {
  color: var(--critical);
  font-size: 0.85em;
  min-height: 1.2em;
  margin-top: 10px;
}

/* Dashboard */
#dashboard { display: none; }
#dashboard.unlocked { display: block; }

/* Top nav */
.topnav {
  position: sticky;
  top: 0;
  z-index: 10;
  background: var(--surface-1);
  border-bottom: 1px solid var(--border);
}

.topnav-inner {
  max-width: 1100px;
  margin: 0 auto;
  padding: 14px 20px;
  display: flex;
  align-items: center;
  gap: 24px;
}

.brand {
  font-weight: 700;
  font-size: 1.05em;
  letter-spacing: -0.01em;
  white-space: nowrap;
  color: var(--accent);
}

.nav-links {
  display: flex;
  gap: 4px;
  flex: 1;
}

.nav-link {
  padding: 7px 14px;
  border-radius: 8px;
  font-size: 0.88em;
  font-weight: 500;
  color: var(--text-secondary);
  text-decoration: none;
}

.nav-link:hover { color: var(--text-primary); background: var(--surface-2); }

.nav-link.active {
  color: var(--accent);
  background: rgba(var(--accent-rgb), 0.12);
}

.nav-actions {
  display: flex;
  align-items: center;
  gap: 10px;
}

.currency-toggle {
  display: inline-flex;
  background: var(--surface-2);
  border: 1px solid var(--border);
  border-radius: 999px;
  padding: 3px;
  gap: 2px;
}

.currency-toggle button {
  border: none;
  background: none;
  padding: 5px 12px;
  border-radius: 999px;
  font-size: 0.8em;
  font-weight: 600;
  color: var(--text-secondary);
  cursor: pointer;
}

.currency-toggle button.active {
  background: var(--surface-1);
  color: var(--text-primary);
  box-shadow: var(--shadow-sm);
}

.lock-btn {
  background: none;
  border: 1px solid var(--border);
  color: var(--text-secondary);
  padding: 7px 14px;
  border-radius: 999px;
  cursor: pointer;
  font-size: 0.82em;
}

.lock-btn:hover { color: var(--text-primary); }

.tracker-main {
  max-width: 1100px;
  margin: 0 auto;
  padding: 24px 20px 60px;
}

.page-title {
  font-size: 1.4em;
  font-weight: 700;
  letter-spacing: -0.01em;
  margin: 4px 0 20px;
}

/* Stat tiles */
.error-banner {
  background: rgba(208, 59, 59, 0.12);
  border: 1px solid var(--critical);
  color: var(--critical);
  border-radius: 10px;
  padding: 12px 16px;
  font-size: 0.85em;
  margin-bottom: 20px;
}

.stat-row {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(180px, 1fr));
  gap: 14px;
  margin: 16px 0 28px;
}

.stat-tile {
  background: var(--surface-1);
  border: 1px solid var(--border);
  border-radius: 14px;
  padding: 18px 20px;
  box-shadow: var(--shadow-sm);
}

.stat-tile .label {
  font-size: 0.78em;
  color: var(--text-secondary);
  font-weight: 500;
}

.stat-tile .value {
  font-size: 1.7em;
  font-weight: 700;
  margin-top: 6px;
  letter-spacing: -0.01em;
}

.stat-tile .sub {
  font-size: 0.78em;
  color: var(--text-muted);
  margin-top: 2px;
}

.value.pos { color: var(--good-text); }
.value.neg { color: var(--critical); }

/* Chart cards */
.chart-row {
  display: grid;
  grid-template-columns: 1.3fr 1fr;
  gap: 16px;
  margin-bottom: 28px;
}

@media (max-width: 720px) {
  .chart-row { grid-template-columns: 1fr; }
  .topnav-inner { flex-wrap: wrap; gap: 10px; }
  .nav-links { order: 3; width: 100%; }
}

.chart-card {
  background: var(--surface-1);
  border: 1px solid var(--border);
  border-radius: 14px;
  padding: 20px 20px 12px;
  position: relative;
  box-shadow: var(--shadow-sm);
}

.chart-card h2 {
  font-size: 0.95em;
  margin: 0 0 12px;
  color: var(--text-secondary);
  font-weight: 600;
}

.chart-card svg { display: block; width: 100%; height: auto; overflow: visible; }

.chart-empty {
  color: var(--text-muted);
  font-size: 0.85em;
  padding: 30px 0;
  text-align: center;
}

.chart-tooltip {
  position: absolute;
  pointer-events: none;
  background: var(--surface-1);
  border: 1px solid var(--border);
  border-radius: 10px;
  padding: 7px 11px;
  font-size: 0.78em;
  box-shadow: var(--shadow-md);
  opacity: 0;
  transition: opacity 0.1s;
  white-space: nowrap;
  z-index: 5;
}

.chart-tooltip .t-label { color: var(--text-secondary); }
.chart-tooltip .t-value { color: var(--text-primary); font-weight: 600; margin-left: 6px; }

.chart-legend {
  display: flex;
  gap: 16px;
  margin-bottom: 10px;
  font-size: 0.8em;
  color: var(--text-secondary);
}

.legend-item { display: inline-flex; align-items: center; gap: 6px; }

.legend-swatch {
  width: 10px;
  height: 10px;
  border-radius: 3px;
  display: inline-block;
}

/* Section headers */
.section-title {
  font-size: 1.05em;
  font-weight: 700;
  margin: 32px 0 14px;
  display: flex;
  align-items: center;
  gap: 10px;
  letter-spacing: -0.01em;
}

.section-title:first-of-type { margin-top: 0; }

.section-subnote {
  font-size: 0.7em;
  font-weight: 500;
  color: var(--text-muted);
  text-transform: none;
  letter-spacing: normal;
}

.toggle-zero-btn {
  background: var(--surface-2);
  border: 1px solid var(--border);
  color: var(--text-secondary);
  padding: 5px 12px;
  border-radius: 999px;
  cursor: pointer;
  font-size: 0.72em;
  font-weight: 500;
}

.toggle-zero-btn:hover { color: var(--text-primary); }

/* Form */
.entry-form {
  background: var(--surface-1);
  border: 1px solid var(--border);
  border-radius: 14px;
  padding: 20px;
  margin-bottom: 20px;
  display: flex;
  flex-direction: column;
  gap: 14px;
  box-shadow: var(--shadow-sm);
}

.form-row {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(160px, 1fr));
  gap: 12px;
}

.entry-form label {
  display: flex;
  flex-direction: column;
  font-size: 0.78em;
  color: var(--text-secondary);
  font-weight: 500;
  gap: 5px;
}

.entry-form label.note-field { grid-column: 1 / -1; }

.entry-form input, .entry-form select {
  padding: 9px 11px;
  border-radius: 8px;
  border: 1px solid var(--border);
  background: var(--surface-2);
  color: var(--text-primary);
  font-size: 0.9em;
}

.entry-form input:focus, .entry-form select:focus {
  outline: 2px solid rgba(var(--accent-rgb), 0.4);
  outline-offset: 1px;
}

.form-actions {
  display: flex;
  gap: 8px;
}

.entry-form button[type="submit"] {
  background: linear-gradient(135deg, var(--accent), var(--accent-2));
  color: white;
  border: none;
  padding: 10px 18px;
  border-radius: 8px;
  cursor: pointer;
  font-size: 0.9em;
  font-weight: 600;
  height: fit-content;
  box-shadow: 0 4px 14px rgba(42, 95, 214, 0.3);
}

.entry-form .cancel-edit {
  background: none;
  border: 1px solid var(--border);
  color: var(--text-secondary);
  padding: 10px 14px;
  border-radius: 8px;
  cursor: pointer;
  font-size: 0.85em;
  display: none;
  margin-left: 8px;
}

/* Table */
.table-wrap {
  background: var(--surface-1);
  border: 1px solid var(--border);
  border-radius: 14px;
  overflow-x: auto;
  box-shadow: var(--shadow-sm);
}

table.entries-table {
  width: 100%;
  border-collapse: collapse;
  font-size: 0.85em;
}

.entries-table th, .entries-table td {
  text-align: left;
  padding: 11px 16px;
  border-bottom: 1px solid var(--gridline);
  white-space: nowrap;
}

.entries-table th {
  color: var(--text-muted);
  font-weight: 600;
  font-size: 0.82em;
  text-transform: uppercase;
  letter-spacing: 0.03em;
  background: var(--surface-2);
}

.entries-table tbody tr:last-child td { border-bottom: none; }
.entries-table tbody tr:hover td { background: var(--surface-2); }
.entries-table tbody tr.zero-row td { opacity: 0.6; }

.entries-table td.num { font-variant-numeric: tabular-nums; text-align: right; }

.entries-table td.pl-cell { font-weight: 700; }
.entries-table td.pl-cell.pos { color: var(--good-text); }
.entries-table td.pl-cell.neg { color: var(--critical); }

.entries-table td.note { white-space: normal; color: var(--text-secondary); max-width: 220px; }

.entries-table .empty-row { text-align: center; color: var(--text-muted); white-space: normal; }

.entries-table button {
  background: none;
  border: none;
  color: var(--text-muted);
  cursor: pointer;
  font-size: 0.85em;
  padding: 2px 6px;
}

.entries-table button:hover { color: var(--sequential); }
.entries-table button.del:hover { color: var(--critical); }

.badge {
  display: inline-block;
  padding: 3px 10px;
  border-radius: 999px;
  font-size: 0.78em;
  font-weight: 600;
}

.badge-cat1 { background: rgba(var(--cat-1-rgb), 0.15); color: var(--cat-1); }
.badge-cat2 { background: rgba(var(--cat-2-rgb), 0.15); color: var(--cat-2); }

.backup-row {
  display: flex;
  gap: 10px;
  justify-content: flex-end;
  margin-top: 14px;
}

.backup-row button {
  background: none;
  border: 1px solid var(--border);
  color: var(--text-secondary);
  padding: 8px 15px;
  border-radius: 999px;
  cursor: pointer;
  font-size: 0.8em;
}

.backup-row button:hover { color: var(--text-primary); }

.backup-row input[type="file"] { display: none; }
