/* ── Design tokens ────────────────────────────────────────────────────
   Two rules for anything added below:

   1. Use a token, never a raw value. Colours had no dark counterpart and
      spacing had grown to seventeen distinct pixel values (1,2,3,4,5,6,7,8,
      9,10,12,14,16,18,20,24,32), which is what made the UI read as
      approximately-aligned rather than deliberate. Snapping to a scale is
      most of the difference between "a web page with numbers on it" and
      something that looks designed.

   2. Colours must stay in step with `ios-app/Budget/DesignSystem/Theme.swift`.
      That file's own comment says its palette was "ported from
      frontend/style.css, with a dark counterpart for each token"; the dark
      values below are those counterparts ported back, so the phone and the
      browser are one product rather than two that resemble each other. If you
      change a colour here, change it there. */
:root {
  /* Surfaces */
  --bg: #f5f6f8;
  --card: #ffffff;
  --border: #e2e5ea;
  /* A second surface for table headers and inset areas. Previously these
     borrowed --bg, which works in light mode and collapses in dark, where the
     page background is darker than the card sitting on it. */
  --surface-2: #f5f6f8;

  /* Text */
  --text: #1c2128;
  --muted: #6b7280;

  /* Semantic */
  --accent: #0f766e;
  --accent-dark: #0b544e;
  --accent-contrast: #ffffff;
  --danger: #b91c1c;
  --good: #15803d;
  --warning: #d97706;

  /* Spacing scale. Everything is a multiple of 4, so unrelated components
     land on the same rhythm without anyone having to think about it. */
  --space-1: 4px;
  --space-2: 8px;
  --space-3: 12px;
  --space-4: 16px;
  --space-5: 24px;
  --space-6: 32px;

  /* Type scale. Five steps replacing eight, three of which (0.85/0.9/0.95em)
     were indistinguishable in practice while guaranteeing inconsistency. */
  --text-xs: 0.75rem;
  --text-sm: 0.85rem;
  --text-base: 0.95rem;
  --text-lg: 1.25rem;
  --text-xl: 1.75rem;

  /* Shape */
  --radius: 10px;
  --radius-sm: 6px;

  /* Elevation. Every surface previously had an identical 1px border, so a
     hero figure and a settings panel carried the same visual weight. These
     are deliberately subtle: a dashboard should have a hierarchy, not a
     drop-shadow competition. */
  --shadow-sm: 0 1px 2px rgba(16, 24, 40, 0.04);
  --shadow-md: 0 1px 3px rgba(16, 24, 40, 0.08), 0 1px 2px rgba(16, 24, 40, 0.04);

  /* Motion. One duration and one curve, so nothing feels out of step. */
  --ease: 140ms cubic-bezier(0.4, 0, 0.2, 1);
}

/* Dark counterparts, matching Theme.swift's dark column exactly. Follows the
   system rather than forcing a scheme: as that file notes, a budgeting app
   gets opened at a checkout in daylight as often as on the sofa at night. */
@media (prefers-color-scheme: dark) {
  :root {
    --bg: #0f1115;
    --card: #171a21;
    --border: #2a2f3a;
    --surface-2: #1e222b;

    --text: #e6e8ec;
    --muted: #9aa3b2;

    --accent: #2dd4bf;
    --accent-dark: #5eead4;
    /* Dark-mode accent is a light teal, so text on top of it has to invert or
       it disappears. */
    --accent-contrast: #0f1115;
    --danger: #f87171;
    --good: #4ade80;
    --warning: #fbbf24;

    /* Shadows do almost nothing on dark surfaces; depth comes from the border
       and the lighter card instead, so these stay minimal rather than turning
       into muddy halos. */
    --shadow-sm: none;
    --shadow-md: 0 1px 2px rgba(0, 0, 0, 0.4);
  }
}

* { box-sizing: border-box; }

body {
  margin: 0;
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Helvetica, Arial, sans-serif;
  background: var(--bg);
  color: var(--text);
  font-size: 16px;
  line-height: 1.5;
  -webkit-font-smoothing: antialiased;
}

.hidden { display: none !important; }
.muted { color: var(--muted); }
.error-text { color: var(--danger); min-height: 1.2em; font-size: var(--text-sm); }

/* Money and dates line up column-wise only with tabular figures. This applied
   to table cells alone before, so the large dashboard values, the ones anyone
   actually reads, shifted horizontally as the digits changed. For a finance
   app that wobble is the single most visible tell. */
.stat-card .value,
.month-nav-label,
.amount-pos,
.amount-neg,
.closed-badge,
.compare-row-label,
td.num, th.num {
  font-variant-numeric: tabular-nums;
  font-feature-settings: "tnum";
}

/* One visible focus ring for everything, rather than the browser default on
   some controls and nothing on the rest. :focus-visible so it appears for
   keyboard users without ringing every mouse click. */
:focus-visible {
  outline: 2px solid var(--accent);
  outline-offset: 2px;
  border-radius: var(--radius-sm);
}

/* ── Login ─────────────────────────────────────────────── */
.login-screen {
  min-height: 100vh;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: var(--space-4);
}
.login-card {
  background: var(--card);
  padding: var(--space-6);
  border-radius: var(--radius);
  border: 1px solid var(--border);
  box-shadow: var(--shadow-md);
  width: 340px;
  max-width: 100%;
  display: flex;
  flex-direction: column;
  gap: var(--space-3);
}
.login-card h1 { margin: 0; font-size: var(--text-lg); letter-spacing: -0.01em; }
.login-card label { display: flex; flex-direction: column; gap: var(--space-1); font-size: var(--text-sm); color: var(--muted); }
.login-card input {
  padding: var(--space-2) var(--space-3);
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  font-size: var(--text-base);
  background: var(--card);
  color: var(--text);
  transition: border-color var(--ease);
}
.login-card input:focus { border-color: var(--accent); }
.login-card button {
  margin-top: var(--space-2);
  padding: var(--space-3);
  background: var(--accent);
  color: var(--accent-contrast);
  border: none;
  border-radius: var(--radius-sm);
  cursor: pointer;
  font-size: var(--text-base);
  font-weight: 600;
  transition: background var(--ease);
}
.login-card button:hover { background: var(--accent-dark); }

/* ── Layout ────────────────────────────────────────────── */
.topbar {
  display: flex;
  align-items: center;
  gap: var(--space-5);
  padding: var(--space-3) var(--space-5);
  background: var(--card);
  border-bottom: 1px solid var(--border);
  position: sticky;
  top: 0;
  z-index: 10;
}
.brand { font-weight: 600; letter-spacing: -0.01em; }
.tabs { display: flex; gap: var(--space-1); flex: 1; }
.tab-btn {
  background: none;
  border: none;
  padding: var(--space-2) var(--space-3);
  border-radius: var(--radius-sm);
  cursor: pointer;
  font-size: var(--text-base);
  color: var(--muted);
  transition: background var(--ease), color var(--ease);
}
.tab-btn:hover { background: var(--surface-2); color: var(--text); }
.tab-btn.active { background: var(--accent); color: var(--accent-contrast); font-weight: 500; }
.tab-btn.active:hover { background: var(--accent-dark); color: var(--accent-contrast); }
.tab-badge {
  display: inline-block;
  margin-left: var(--space-1);
  padding: 1px 7px;
  border-radius: 999px;
  background: var(--danger);
  color: #fff;
  font-size: var(--text-xs);
  font-weight: 600;
}
.topbar-right { display: flex; align-items: center; gap: var(--space-3); }
.link-btn {
  background: none;
  border: none;
  color: var(--accent);
  cursor: pointer;
  text-decoration: underline;
  font-size: var(--text-sm);
  padding: var(--space-1);
}

main { max-width: 1080px; margin: 0 auto; padding: var(--space-5); }
.tab-panel { display: none; }
.tab-panel.active { display: block; }

h2 { font-size: var(--text-lg); letter-spacing: -0.01em; margin: 0 0 var(--space-3); }

.row-between { display: flex; justify-content: space-between; align-items: center; margin-bottom: var(--space-3); }
.row-between h2 { margin-bottom: 0; }

/* ── Cards ─────────────────────────────────────────────── */
.card-grid {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(200px, 1fr));
  gap: var(--space-4);
  margin-bottom: var(--space-5);
}
.card {
  background: var(--card);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: var(--space-4) var(--space-5);
  margin-bottom: var(--space-5);
  box-shadow: var(--shadow-sm);
}
/* Stat tiles are the top of the hierarchy: they carry the figures the page
   exists to show, so they sit a step above the cards below them. */
.stat-card {
  background: var(--card);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: var(--space-4) var(--space-5);
  box-shadow: var(--shadow-md);
}
.stat-card .label {
  font-size: var(--text-xs);
  color: var(--muted);
  text-transform: uppercase;
  letter-spacing: 0.05em;
  font-weight: 600;
}
.stat-card .value {
  font-size: var(--text-xl);
  font-weight: 650;
  margin-top: var(--space-1);
  letter-spacing: -0.02em;
  line-height: 1.2;
}
.stat-card .sub { font-size: var(--text-sm); color: var(--muted); margin-top: var(--space-1); }
.progress-bar {
  height: 6px;
  background: var(--border);
  border-radius: 999px;
  margin-top: var(--space-3);
  overflow: hidden;
}
.progress-bar-fill {
  height: 100%;
  background: var(--accent);
  border-radius: 999px;
  transition: width var(--ease);
}

/* ── Forms ─────────────────────────────────────────────── */
.inline-form {
  display: flex;
  gap: var(--space-2);
  flex-wrap: wrap;
  margin-bottom: var(--space-4);
  background: var(--card);
  padding: var(--space-3);
  border-radius: var(--radius);
  border: 1px solid var(--border);
  box-shadow: var(--shadow-sm);
}
.inline-form.wrap { flex-wrap: wrap; }
.inline-form input, .inline-form select {
  padding: var(--space-2) var(--space-3);
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  font-size: var(--text-sm);
  background: var(--card);
  color: var(--text);
  transition: border-color var(--ease);
}
.inline-form input:focus, .inline-form select:focus { border-color: var(--accent); }
.inline-form input[type="number"] { width: 120px; }
.inline-form input[type="text"] { width: 150px; }

/* One primary-button appearance, shared, rather than four near-identical
   blocks that drifted apart as panels were added. */
.inline-form button,
.settings-form button,
.month-close-actions button {
  padding: var(--space-2) var(--space-4);
  background: var(--accent);
  color: var(--accent-contrast);
  border: none;
  border-radius: var(--radius-sm);
  cursor: pointer;
  font-size: var(--text-sm);
  font-weight: 600;
  transition: background var(--ease), opacity var(--ease);
}
.inline-form button:hover,
.settings-form button:hover,
.month-close-actions button:hover { background: var(--accent-dark); }

.settings-form { display: grid; grid-template-columns: repeat(auto-fit, minmax(220px, 1fr)); gap: var(--space-3); margin-top: var(--space-3); }
.settings-form label { display: flex; flex-direction: column; gap: var(--space-1); font-size: var(--text-sm); color: var(--muted); }
/* select is here alongside input because the income settings panel uses
   dropdowns for pay frequency, without it they render unstyled next to the
   text fields they sit beside. */
.settings-form input, .settings-form select {
  padding: var(--space-2) var(--space-3);
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  background: var(--card);
  color: var(--text);
  font: inherit;
  font-size: var(--text-sm);
  transition: border-color var(--ease);
}
.settings-form input:focus, .settings-form select:focus { border-color: var(--accent); }
.settings-form label.checkbox-label { flex-direction: row; align-items: center; gap: var(--space-2); font-size: var(--text-base); color: var(--text); }
.settings-form label.checkbox-label input { width: auto; padding: 0; }
.settings-form button { grid-column: 1 / -1; padding: var(--space-3) var(--space-5); width: fit-content; }

/* Shared button feedback: a save that succeeds silently still needs to look
   like it did something, and a disabled button has to read as unavailable
   rather than just unstyled. */
button:disabled { opacity: 0.5; cursor: not-allowed; }
button.btn-done,
.settings-form button.btn-done,
.month-close-actions button.btn-done,
.inline-form button.btn-done { background: var(--good); color: #fff; }
button.btn-done:hover { background: var(--good); }

/* ── Tables ────────────────────────────────────────────── */
.data-table {
  width: 100%;
  border-collapse: collapse;
  background: var(--card);
  border-radius: var(--radius);
  overflow: hidden;
  border: 1px solid var(--border);
  box-shadow: var(--shadow-sm);
}
.data-table th, .data-table td {
  padding: var(--space-2) var(--space-3);
  text-align: left;
  border-bottom: 1px solid var(--border);
  font-size: var(--text-sm);
}
.data-table th {
  background: var(--surface-2);
  font-weight: 600;
  color: var(--muted);
  font-size: var(--text-xs);
  text-transform: uppercase;
  letter-spacing: 0.04em;
}
.data-table td.num, .data-table th.num { text-align: right; }
.data-table tbody tr { transition: background var(--ease); }
.data-table tbody tr:hover { background: var(--surface-2); }
.data-table tbody tr:last-child td { border-bottom: none; }
.data-table .del-btn { background: none; border: none; color: var(--danger); cursor: pointer; font-size: var(--text-sm); padding: var(--space-1); }
.data-table input.inline-edit, .data-table select.inline-edit {
  width: 100%;
  min-width: 90px;
  padding: var(--space-1) var(--space-2);
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  font-size: var(--text-sm);
  background: var(--card);
  color: var(--text);
}
.amount-neg { color: var(--danger); }
.amount-pos { color: var(--good); }

details.card summary, .card details summary {
  cursor: pointer;
  font-weight: 600;
  padding: var(--space-1) 0;
}
.card details { margin-top: var(--space-4); }

/* ── Benchmark comparison card ────────────────────────────── */
.compare-row { margin-top: var(--space-4); }
.compare-row:first-of-type { margin-top: var(--space-1); }
.compare-row-label { display: flex; justify-content: space-between; align-items: baseline; font-size: var(--text-sm); font-weight: 600; }
.compare-row-sub { font-size: var(--text-xs); margin-top: var(--space-1); }
.compare-heading { font-size: var(--text-base); margin: var(--space-5) 0 var(--space-1); }

/* ── Month navigation ──────────────────────────────────────── */
.month-nav { display: flex; align-items: center; gap: var(--space-4); }
.month-nav-label { font-weight: 600; min-width: 120px; text-align: center; }

/* ── Charts ────────────────────────────────────────────────── */
canvas { max-width: 100%; }

/* Transactions search. Sits between the month picker and the table, so it
   reads as "narrow what's below" rather than as part of the add form. */
.tx-search-row { margin-bottom: var(--space-3); gap: var(--space-3); }
.search-input {
  flex: 1;
  padding: var(--space-2) var(--space-3);
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  font-size: var(--text-sm);
  background: var(--card);
  color: var(--text);
  transition: border-color var(--ease);
}
.search-input:focus { border-color: var(--accent); }

.close-part { border-top: 1px solid var(--border); margin-top: var(--space-4); padding-top: var(--space-4); }
.close-part:first-of-type { border-top: none; margin-top: var(--space-2); }
.close-part .row-between { margin-bottom: var(--space-1); }
.close-part p.muted { margin: var(--space-1) 0 0; font-size: var(--text-sm); }

.inline-check { display: block; margin-top: var(--space-3); font-size: var(--text-sm); }
.inline-check input { margin-right: var(--space-2); }

/* ── Month close ────────────────────────────────────────────────────── */
.month-close-actions { display: flex; flex-wrap: wrap; gap: var(--space-2); align-items: center; margin-top: var(--space-3); }
.month-close-actions input {
  padding: var(--space-2) var(--space-3);
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  font: inherit;
  font-size: var(--text-sm);
  background: var(--card);
  color: var(--text);
  transition: border-color var(--ease);
}
.month-close-actions input:focus { border-color: var(--accent); }
.month-close-actions input[type="number"] { width: 220px; }
.month-close-actions input[type="text"] { flex: 1; min-width: 200px; }
/* The reopen button is destructive-ish, so it reads as an outline rather than
   a second primary action sitting next to Close. */
.month-close-actions button.del-btn { background: none; border: 1px solid var(--danger); color: var(--danger); }
.month-close-actions button.del-btn:hover { background: var(--danger); color: #fff; }
.closed-badge { color: var(--good); font-size: var(--text-sm); font-weight: 500; }

/* ── Mobile ────────────────────────────────────────────────────────────
   Everything above is written desktop-first; this section adapts the
   same markup for phone-width screens rather than shipping a separate
   layout. If you build a native iOS client later, the API underneath
   (JWT bearer auth over plain HTTP endpoints, no cookies/sessions) is
   already client-agnostic. This section is purely about this web UI. */
@media (max-width: 640px) {
  main { padding: var(--space-3); }

  .topbar {
    padding: var(--space-3);
    gap: var(--space-3);
    flex-wrap: nowrap;
  }
  .brand { font-size: var(--text-sm); white-space: nowrap; }
  .tabs {
    overflow-x: auto;
    -webkit-overflow-scrolling: touch;
    flex-wrap: nowrap;
    scrollbar-width: none;
  }
  .tabs::-webkit-scrollbar { display: none; }
  .tab-btn { white-space: nowrap; flex-shrink: 0; }
  .topbar-right { flex-shrink: 0; }
  .topbar-right .muted { display: none; } /* username, reclaim space, Log out stays */

  .row-between {
    flex-direction: column;
    align-items: flex-start;
    gap: var(--space-2);
  }

  .card-grid { grid-template-columns: 1fr 1fr; gap: var(--space-2); }
  .card, .stat-card { padding: var(--space-3) var(--space-4); }
  .stat-card .value { font-size: var(--text-lg); }

  .inline-form { flex-direction: column; align-items: stretch; }
  .inline-form input, .inline-form select, .inline-form button {
    width: 100%;
  }

  .settings-form { grid-template-columns: 1fr; }
  .settings-form button { width: 100%; }

  /* Scrollable tables instead of squeezing every column illegibly, the
     table keeps its natural width and the .table-scroll wrapper scrolls. */
  .table-scroll {
    overflow-x: auto;
    -webkit-overflow-scrolling: touch;
    margin-bottom: var(--space-5);
  }
  .table-scroll .data-table { min-width: 480px; margin-bottom: 0; }

  .month-nav { width: 100%; justify-content: space-between; }
  .month-nav-label { min-width: 0; }

  .month-close-actions { flex-direction: column; align-items: stretch; }
  .month-close-actions input[type="number"] { width: 100%; }
  .month-close-actions button { width: 100%; }
}

@media (max-width: 400px) {
  .card-grid { grid-template-columns: 1fr; }
}

/* Respects a system-level request for less animation. Everything here is
   decorative, so it can all be turned off without losing meaning. */
@media (prefers-reduced-motion: reduce) {
  * { transition: none !important; animation: none !important; }
}
