/* ============================================================================
   FinTracker — stylesheet
   Sections: tokens · reset/base · typography · header · layout · bottom nav
   · tabs · buttons · inputs/filters · stat cards · tables · record cards
   · tags/chips/badges · duplicates · trend chart · modals/sheets · toast
   · auth · spinner/overlay · empty states · @media 768 · @media 480
   · @media reduced-motion
   ========================================================================= */

/* ── TOKENS ───────────────────────────────────────────────────────────────── */
/* Neutral zinc ramp with a near-black primary: chrome stays monochrome and
   colour is reserved for data (charts, debit/credit, status). */
:root {
  --bg: #ffffff;
  --surface: #fafafa;    /* zinc-50  — muted surfaces */
  --surface2: #f4f4f5;   /* zinc-100 — hover / secondary fills */
  --border: #e4e4e7;     /* zinc-200 */
  --border2: #d4d4d8;    /* zinc-300 */

  /* "accent" is the primary action colour, deliberately near-black. */
  --accent: #18181b;         /* zinc-900 */
  --accent-hover: #27272a;   /* zinc-800 */
  --accent-fg: #fafafa;      /* zinc-50 — text on primary */
  --accent-light: #f4f4f5;   /* subtle tinted background */
  --accent-subtle: #e4e4e7;

  --text: #09090b;       /* zinc-950 */
  --text2: #27272a;      /* zinc-800 */
  --muted: #71717a;      /* zinc-500 */
  --muted2: #a1a1aa;     /* zinc-400 */

  --ring: #18181b;

  --danger: #dc2626;
  --danger-hover: #b91c1c;
  --danger-light: #fef2f2;
  --danger-border: #fecaca;

  --success: #059669;
  --success-light: #ecfdf5;
  --success-border: #a7f3d0;
  --success-deep: #065f46;

  --warning: #d97706;
  --warning-light: #fffbeb;
  --warning-border: #fde68a;
  --warning-deep: #92400e;

  --overlay: rgba(9, 9, 11, 0.5);

  /* Categorical series colours. TREND_PALETTE in app.js reads these. */
  --chart-1: #2563eb;
  --chart-2: #7c3aed;
  --chart-3: #db2777;
  --chart-4: #d97706;
  --chart-5: #059669;
  --chart-6: #0891b2;
  --chart-7: #ea580c;
  --chart-8: #65a30d;

  /* Duplicate-reason chips */
  --chip-amber-bg: #fef3c7;  --chip-amber-fg: #92400e;
  --chip-blue-bg:  #dbeafe;  --chip-blue-fg:  #1e40af;
  --chip-pink-bg:  #fce7f3;  --chip-pink-fg:  #9d174d;

  --radius-sm: 6px;
  --radius: 8px;
  --radius-lg: 12px;
  --radius-full: 999px;

  /* Barely-there elevation; a border does most of the separating. */
  --shadow-sm: 0 1px 2px 0 rgba(9, 9, 11, 0.05);
  --shadow: 0 1px 3px 0 rgba(9, 9, 11, 0.07), 0 1px 2px -1px rgba(9, 9, 11, 0.07);
  --shadow-md: 0 4px 6px -1px rgba(9, 9, 11, 0.07), 0 2px 4px -2px rgba(9, 9, 11, 0.06);
  --shadow-lg: 0 10px 24px -4px rgba(9, 9, 11, 0.12), 0 4px 8px -4px rgba(9, 9, 11, 0.08);

  --font-sans: 'Inter', -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, sans-serif;
  --font-mono: ui-monospace, 'SF Mono', 'Fira Code', Menlo, monospace;

  --header-h: 56px;
  --nav-h: 60px;
  --safe-b: env(safe-area-inset-bottom, 0px);
}

/* ── RESET / BASE ─────────────────────────────────────────────────────────── */
* { margin: 0; padding: 0; box-sizing: border-box; }

body {
  background: var(--bg);
  color: var(--text);
  font-family: var(--font-sans);
  min-height: 100vh;
  font-size: 15px;
  line-height: 1.5;
  -webkit-font-smoothing: antialiased;
  -webkit-tap-highlight-color: transparent;
}

/* One consistent focus ring for everything interactive. */
:focus-visible {
  outline: 2px solid var(--ring);
  outline-offset: 2px;
  border-radius: var(--radius-sm);
}
button:focus:not(:focus-visible) { outline: none; }

::selection { background: var(--accent-subtle); color: var(--text); }

code {
  font-family: var(--font-mono);
  font-size: 0.9em;
  background: var(--surface2);
  padding: 1px 5px;
  border-radius: 5px;
}

.mono { font-family: var(--font-mono); }
.num  { font-variant-numeric: tabular-nums; }

/* ── TYPOGRAPHY ───────────────────────────────────────────────────────────── */
/* Sentence case, not uppercase micro-labels — closer to shadcn section headings. */
.section-label {
  font-size: 14px;
  font-weight: 600;
  letter-spacing: -0.2px;
  color: var(--text);
  margin-bottom: 12px;
}
.sub { font-size: 13.5px; color: var(--muted); }

/* ── HEADER ───────────────────────────────────────────────────────────────── */
header {
  padding: 0 24px;
  height: var(--header-h);
  border-bottom: 1px solid var(--border);
  display: flex;
  align-items: center;
  gap: 24px;
  background: rgba(255, 255, 255, 0.85);
  -webkit-backdrop-filter: saturate(180%) blur(12px);
  backdrop-filter: saturate(180%) blur(12px);
  position: sticky;
  top: 0;
  z-index: 50;
}

.logo {
  font-size: 18px;
  font-weight: 700;
  letter-spacing: -0.4px;
  color: var(--text);
  flex-shrink: 0;
}
/* accent is near-black now, so the wordmark splits tonally instead of by hue. */
.logo span { color: var(--muted2); }
/* The mark sits inline with the wordmark, so the text nodes stay in one flow. */
.logo-mark { width: 20px; height: 20px; margin-right: 8px; vertical-align: -4px; }

.status-dot {
  width: 7px; height: 7px;
  border-radius: 50%;
  background: var(--muted2);
  flex-shrink: 0;
  transition: background 0.3s;
}
.status-dot.connected { background: var(--success); }

/* ── LAYOUT ───────────────────────────────────────────────────────────────── */
.container { height: calc(100vh - var(--header-h)); height: calc(100dvh - var(--header-h)); }

.main {
  height: 100%;
  overflow-y: auto;
  padding: 24px 32px 48px;
  background: var(--bg);
}

/* ── BOTTOM NAV (mobile only) ─────────────────────────────────────────────── */
.bottom-nav {
  display: none;
  position: fixed;
  left: 0; right: 0; bottom: 0;
  z-index: 90;
  background: rgba(255, 255, 255, 0.92);
  -webkit-backdrop-filter: saturate(180%) blur(14px);
  backdrop-filter: saturate(180%) blur(14px);
  border-top: 1px solid var(--border);
  padding-bottom: var(--safe-b);
}
.nav-item {
  flex: 1;
  min-width: 0;
  min-height: var(--nav-h);
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 3px;
  padding: 8px 2px;
  background: none;
  border: none;
  font-family: inherit;
  font-size: 10px;
  font-weight: 600;
  letter-spacing: 0.1px;
  color: var(--muted);
  cursor: pointer;
  position: relative;
  transition: color 0.15s;
}
/* Hidden on the auth/loading screens — set by showAuth()/showApp(). */
body.logged-out .bottom-nav { display: none !important; }

.nav-item svg { width: 21px; height: 21px; stroke-width: 1.9; }
.nav-item.active { color: var(--accent); }
.nav-label {
  max-width: 100%;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}
.nav-badge {
  position: absolute;
  top: 4px;
  left: 50%;
  margin-left: 4px;
  min-width: 17px;
  padding: 0 5px;
  font-size: 10px;
  font-weight: 600;
  line-height: 16px;
  text-align: center;
  background: var(--warning-light);
  border: 1px solid var(--warning-border);
  color: var(--warning-deep);
  border-radius: var(--radius-full);
}

/* ── TABS (desktop) — segmented control, hosted in the header ──────────────── */
.tabs {
  display: flex;
  gap: 2px;
  padding: 3px;
  background: var(--surface2);
  border-radius: var(--radius);
  /* Hug the tabs, but scroll rather than overflow a narrow desktop window. */
  width: fit-content;
  max-width: 100%;
  min-width: 0;
  overflow-x: auto;
  scrollbar-width: none;
}
.tabs::-webkit-scrollbar { display: none; }
/* No app chrome on the auth/loading screens. */
body.logged-out .tabs { display: none; }
.tab {
  display: inline-flex;
  align-items: center;
  gap: 6px;
  padding: 7px 13px;
  background: none;
  border: none;
  color: var(--muted);
  font-family: inherit;
  font-size: 13.5px;
  font-weight: 500;
  cursor: pointer;
  white-space: nowrap;
  border-radius: var(--radius-sm);
  transition: color 0.15s, background 0.15s, box-shadow 0.15s;
}
.tab.active {
  color: var(--text);
  background: var(--bg);
  box-shadow: var(--shadow-sm);
}
.tab:hover:not(.active) { color: var(--text2); }
.tab-badge {
  display: inline-block;
  min-width: 18px;
  padding: 0 6px;
  font-size: 11px;
  font-weight: 600;
  font-variant-numeric: tabular-nums;
  text-align: center;
  line-height: 18px;
  background: var(--warning-light);
  border: 1px solid var(--warning-border);
  color: var(--warning-deep);
  border-radius: var(--radius-full);
}

/* ── BUTTONS ──────────────────────────────────────────────────────────────── */
.btn {
  height: 36px;
  padding: 0 16px;
  border-radius: var(--radius-sm);
  border: 1px solid transparent;
  font-family: inherit;
  font-size: 13.5px;
  font-weight: 500;
  cursor: pointer;
  transition: background 0.15s, border-color 0.15s, color 0.15s, opacity 0.15s;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 6px;
  line-height: 1;
  white-space: nowrap;
}
.btn:disabled { opacity: 0.5; cursor: not-allowed; }
.btn-primary { background: var(--accent); color: var(--accent-fg); }
.btn-primary:hover:not(:disabled) { background: var(--accent-hover); }
.btn-secondary { background: var(--bg); color: var(--text); border-color: var(--border); }
.btn-secondary:hover:not(:disabled) { background: var(--surface2); }
.btn-danger { background: var(--danger); color: #fff; }
.btn-danger:hover:not(:disabled) { background: var(--danger-hover); }
.btn-ghost { background: none; color: var(--muted); border-color: transparent; padding: 0 10px; }
.btn-ghost:hover { color: var(--text); background: var(--surface2); }
.btn-full { width: 100%; }
.btn-sm { height: 32px; padding: 0 12px; font-size: 13px; }

.icon-btn {
  width: 32px;
  height: 32px;
  padding: 0;
  border: 1px solid var(--border);
  background: var(--bg);
  color: var(--text2);
  border-radius: var(--radius-sm);
  cursor: pointer;
  font-size: 14px;
  line-height: 1;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  flex-shrink: 0;
  transition: background 0.12s, border-color 0.12s, color 0.12s;
}
.icon-btn:hover { background: var(--accent-light); border-color: var(--accent); }
.icon-btn.keep:hover   { background: var(--success-light); border-color: var(--success); color: var(--success); }
.icon-btn.delete:hover { background: var(--danger-light);  border-color: var(--danger);  color: var(--danger); }
.icon-btn.swap:hover   { background: var(--accent-light);  border-color: var(--accent);  color: var(--accent); }

/* ── INPUTS / FILTERS ─────────────────────────────────────────────────────── */
.input-group { display: flex; flex-direction: column; gap: 6px; margin-bottom: 14px; }

label { font-size: 13px; font-weight: 500; color: var(--text2); }

input:not([type="checkbox"]), select, textarea {
  background: var(--bg);
  border: 1px solid var(--border);
  color: var(--text);
  font-family: inherit;
  font-size: 13.5px;
  padding: 8px 11px;
  min-height: 36px;
  border-radius: var(--radius-sm);
  outline: none;
  transition: border-color 0.15s, box-shadow 0.15s;
  width: 100%;
}
/* shadcn-style ring: 2px in the ring colour with a background-coloured offset. */
input:not([type="checkbox"]):focus, select:focus, textarea:focus {
  border-color: var(--ring);
  box-shadow: 0 0 0 1px var(--ring);
}
input:not([type="checkbox"]):focus-visible,
select:focus-visible,
textarea:focus-visible { outline: none; }
input::placeholder, textarea::placeholder { color: var(--muted2); }
input[type="file"] { display: none; }
input[type="checkbox"] { accent-color: var(--accent); width: 17px; height: 17px; }

.filters { display: flex; gap: 10px; align-items: center; margin-bottom: 20px; flex-wrap: wrap; }
.filters select { width: auto; }

/* ── MONTH FLIPPER ────────────────────────────────────────────────────────── */
/* "‹ Aug 26 ›" stepper standing in for the old month dropdown. */
.month-flip {
  display: inline-flex;
  align-items: stretch;
  flex-shrink: 0;
  height: 36px;
  background: var(--bg);
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  overflow: hidden;
}
.month-arrow {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  width: 32px;
  padding: 0;
  background: none;
  border: none;
  color: var(--muted);
  cursor: pointer;
  transition: background 0.12s, color 0.12s;
}
.month-arrow svg { width: 16px; height: 16px; }
.month-arrow:hover:not(:disabled) { background: var(--surface2); color: var(--text); }
.month-arrow:disabled { color: var(--border2); cursor: default; }
.month-label {
  min-width: 78px;
  padding: 0 6px;
  background: none;
  border: none;
  border-left: 1px solid var(--border);
  border-right: 1px solid var(--border);
  font-family: inherit;
  font-size: 13.5px;
  font-weight: 500;
  font-variant-numeric: tabular-nums;
  color: var(--text);
  cursor: pointer;
  white-space: nowrap;
  transition: background 0.12s;
}
.month-label:hover { background: var(--surface2); }
/* All-time reads as a muted state so a single month looks like the norm. */
.month-flip.is-all .month-label { color: var(--muted); min-width: 88px; }

/* ── STAT CARDS ───────────────────────────────────────────────────────────── */
.cards {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(180px, 1fr));
  gap: 14px;
  margin-bottom: 28px;
}
.card {
  background: var(--bg);
  border: 1px solid var(--border);
  border-radius: var(--radius-lg);
  padding: 18px 20px;
  box-shadow: var(--shadow-sm);
}
.card-label {
  font-size: 13px;
  font-weight: 500;
  color: var(--muted);
  margin-bottom: 8px;
}
.card-value {
  font-size: 26px;
  font-weight: 600;
  letter-spacing: -0.7px;
  color: var(--text);
  font-variant-numeric: tabular-nums;
}
.card-value.accent  { color: var(--accent); }
.card-value.danger  { color: var(--danger); }
.card-value.success { color: var(--success); }
.card-sub { font-size: 12px; color: var(--muted); margin-top: 4px; }

/* ── SETTINGS ─────────────────────────────────────────────────────────────── */
.settings-card {
  background: var(--bg);
  border: 1px solid var(--border);
  border-radius: var(--radius-lg);
  box-shadow: var(--shadow-sm);
  max-width: 680px;
}
.settings-row {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 20px;
  padding: 16px 18px;
}
.settings-text { min-width: 0; }
.settings-title { font-size: 14px; font-weight: 500; color: var(--text); }
.settings-sub {
  font-size: 13px;
  color: var(--muted);
  margin-top: 3px;
  word-break: break-word;
}
.conn-pill {
  display: inline-flex;
  align-items: center;
  gap: 7px;
  flex-shrink: 0;
  background: var(--surface);
  border: 1px solid var(--border);
  padding: 5px 11px;
  border-radius: var(--radius-full);
  font-size: 12px;
  font-weight: 500;
  color: var(--muted);
}

/* ── TABLES ───────────────────────────────────────────────────────────────── */
.table-wrap {
  overflow-x: auto;
  border: 1px solid var(--border);
  border-radius: var(--radius-lg);
  background: var(--bg);
}
table { width: 100%; border-collapse: collapse; }
/* shadcn tables have no filled header band — just muted sentence-case labels
   over a single rule. */
th {
  text-align: left;
  font-size: 12.5px;
  font-weight: 500;
  color: var(--muted);
  height: 40px;
  padding: 0 16px;
  border-bottom: 1px solid var(--border);
  background: var(--bg);
  white-space: nowrap;
}
td {
  padding: 11px 16px;
  border-bottom: 1px solid var(--border);
  font-size: 13.5px;
  vertical-align: middle;
}
tr:last-child td { border-bottom: none; }
tr:hover td { background: var(--surface); }

.amount-debit  { color: var(--danger);  font-weight: 600; font-variant-numeric: tabular-nums; white-space: nowrap; }
.amount-credit { color: var(--success); font-weight: 600; font-variant-numeric: tabular-nums; white-space: nowrap; }

/* Description cell: truncate by default, click to expand. */
.desc-cell {
  max-width: 280px;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
  cursor: pointer;
}
.desc-cell.expanded {
  white-space: normal;
  overflow: visible;
  max-width: 460px;
  word-break: break-word;
}

/* Note icon + hover tooltip showing the full note. */
.note-btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  width: 30px; height: 30px;
  border-radius: var(--radius-sm);
  cursor: pointer;
  position: relative;
  flex-shrink: 0;
  transition: background 0.15s, opacity 0.15s, color 0.15s;
}
.note-btn.empty { color: var(--muted); opacity: 0.35; }
.note-btn.empty:hover { opacity: 1; background: var(--surface2); color: var(--accent); }
.note-btn.has-note { color: var(--accent); }
.note-btn.has-note:hover { background: var(--surface2); }
.note-btn[data-tip]:hover::after {
  content: attr(data-tip);
  position: absolute;
  bottom: calc(100% + 8px);
  right: 0;
  background: var(--text);
  color: #fff;
  padding: 8px 11px;
  border-radius: var(--radius-sm);
  font-size: 12px;
  line-height: 1.45;
  font-weight: 400;
  white-space: pre-wrap;
  text-align: left;
  width: max-content;
  max-width: 300px;
  box-shadow: var(--shadow-md);
  z-index: 60;
  pointer-events: none;
}

/* ── RECORD CARDS (mobile replacement for wide tables) ────────────────────── */
.card-list { display: none; }

.rec-card {
  background: var(--bg);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 12px 14px;
  box-shadow: var(--shadow-sm);
}
.rec-main {
  display: flex;
  align-items: flex-start;
  justify-content: space-between;
  gap: 12px;
}
.rec-title {
  font-size: 13.5px;
  font-weight: 500;
  color: var(--text);
  line-height: 1.35;
  overflow: hidden;
  display: -webkit-box;
  -webkit-line-clamp: 2;
  line-clamp: 2;
  -webkit-box-orient: vertical;
  word-break: break-word;
}
.rec-amt { font-size: 15px; flex-shrink: 0; }
.rec-meta {
  margin-top: 5px;
  font-size: 12px;
  color: var(--muted);
  font-variant-numeric: tabular-nums;
}
.rec-body { margin-top: 8px; font-size: 14px; color: var(--text2); word-break: break-word; }
.rec-actions {
  margin-top: 10px;
  padding-top: 10px;
  border-top: 1px solid var(--border);
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 8px;
  flex-wrap: wrap;
}
.rec-actions .spacer { flex: 1; }
.rec-card.is-used { opacity: 0.65; }

/* Spending breakdown, mobile form */
.spend-card { padding: 12px 14px; }
.spend-head {
  display: flex;
  align-items: baseline;
  justify-content: space-between;
  gap: 12px;
  font-size: 14px;
}
.spend-head .amt { font-weight: 700; font-variant-numeric: tabular-nums; white-space: nowrap; }
.spend-bar {
  height: 6px;
  background: var(--surface2);
  border-radius: var(--radius-full);
  overflow: hidden;
  margin: 8px 0 6px;
}
.spend-fill { height: 100%; background: var(--chart-1); border-radius: var(--radius-full); }
.spend-meta { font-size: 12px; color: var(--muted); font-variant-numeric: tabular-nums; }
.rec-card.is-total { border-color: var(--accent-subtle); background: var(--accent-light); }

/* ── TAGS / CHIPS / BADGES ────────────────────────────────────────────────── */
/* Badge: bordered pill, the shadcn <Badge> shape. */
.tag {
  display: inline-flex;
  align-items: center;
  padding: 3px 10px;
  border: 1px solid transparent;
  border-radius: var(--radius-full);
  font-size: 12px;
  font-weight: 500;
  cursor: pointer;
  white-space: nowrap;
  transition: background 0.15s, color 0.15s, border-color 0.15s;
}
.tag.uncategorized {
  background: var(--warning-light);
  border-color: var(--warning-border);
  color: var(--warning-deep);
}
.tag.categorized {
  background: var(--surface2);
  border-color: var(--border);
  color: var(--text2);
}
.tag.ai {
  background: var(--bg);
  border-color: var(--border);
  color: var(--muted);
}
.tag.uncategorized:hover { background: var(--warning-border); }
.tag.categorized:hover   { background: var(--border); }

.tag-static { cursor: default; }
.tag-pending { background: var(--warning-light); border-color: var(--warning-border); color: var(--warning-deep); }
.tag-used    { background: var(--success-light); border-color: var(--success-border); color: var(--success-deep); }
/* Queued is neutral, not a warning: nothing is wrong, the write just has not
   left the device yet. "not synced" is the one that needs attention. */
.tag-queued  { background: var(--surface2); border-color: var(--border2); color: var(--text2); }
.tag-failed  { background: var(--danger-light); border-color: var(--danger-border); color: var(--danger); }

/* ── OFFLINE BANNER ───────────────────────────────────────────────────────────*/
/* Sticky inside .main rather than under the header: the header and .container
   are a fixed 100dvh split, so anything inserted between them pushes the app
   past the viewport and introduces a second scrollbar. */
#offline-banner {
  display: none;
  align-items: center;
  gap: 8px;
  padding: 9px 14px;
  margin-bottom: 16px;
  background: var(--warning-light);
  border: 1px solid var(--warning-border);
  border-radius: var(--radius-sm);
  color: var(--warning-deep);
  font-size: 13px;
  position: sticky;
  top: 0;
  z-index: 20;
}
#offline-banner svg { width: 15px; height: 15px; flex-shrink: 0; }

.freq-tag {
  display: inline-block;
  padding: 2px 9px;
  border: 1px solid var(--border);
  border-radius: var(--radius-full);
  font-size: 11px;
  font-weight: 500;
  background: var(--surface2);
  color: var(--text2);
}

/* ── DUPLICATES ───────────────────────────────────────────────────────────── */
.dup-bulk-bar { display: flex; flex-wrap: wrap; gap: 8px; margin-bottom: 16px; }

.dup-group {
  border: 1px solid var(--border);
  border-radius: var(--radius-lg);
  overflow: hidden;
  margin-bottom: 12px;
  background: var(--bg);
  box-shadow: var(--shadow-sm);
}
.dup-group-header {
  padding: 9px 14px;
  background: var(--surface);
  color: var(--muted);
  font-size: 12px;
  font-weight: 500;
  border-bottom: 1px solid var(--border);
  display: flex;
  justify-content: space-between;
  align-items: center;
  gap: 8px;
}
.dup-row {
  display: grid;
  grid-template-columns: 28px 110px 1fr 110px 110px 130px 124px;
  gap: 12px;
  align-items: center;
  padding: 10px 14px;
  border-top: 1px solid var(--border);
  font-size: 13px;
}
.dup-row:first-of-type { border-top: none; }
.dup-row .col-uploaded { color: var(--muted); font-size: 11px; margin-top: 2px; font-style: italic; }
.dup-row .col-mono { font-family: var(--font-mono); color: var(--muted); font-size: 12px; white-space: nowrap; }
.dup-row .col-desc { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.dup-row .col-bank { color: var(--muted); font-size: 12px; }
.dup-row .col-amount { font-variant-numeric: tabular-nums; }
.dup-row.survivor {
  background: var(--success-light);
  border-left: 3px solid var(--success);
  padding-left: 11px;
}
.dup-row.flagged {
  background: var(--warning-light);
  border-left: 3px solid var(--warning);
  padding-left: 11px;
}
.dup-marker { font-weight: 700; text-align: center; line-height: 1; }
.dup-marker.keep { color: var(--success); }
.dup-marker.flag { color: var(--warning); }
.dup-actions { display: flex; gap: 6px; justify-content: flex-end; }

.dup-reason-chip {
  display: inline-block;
  padding: 3px 9px;
  border-radius: var(--radius-full);
  font-size: 11px;
  font-weight: 600;
  background: var(--border);
  color: var(--muted);
}
.dup-reason-chip.hsbc_date_drift            { background: var(--chip-amber-bg); color: var(--chip-amber-fg); }
.dup-reason-chip.whitespace_or_punct_drift  { background: var(--chip-blue-bg);  color: var(--chip-blue-fg); }
.dup-reason-chip.parser_text_drift          { background: var(--chip-pink-bg);  color: var(--chip-pink-fg); }
.dup-reason-chip.manual_rejected            { background: var(--border);        color: var(--muted); }

/* ── TREND CHART ──────────────────────────────────────────────────────────── */
.trend-card { margin-top: 28px; }
.trend-chips { display: flex; flex-wrap: wrap; gap: 6px; margin: 10px 0 14px; }
/* Legend chips behave like shadcn toggle buttons: neutral surface, selection
   shown by an elevated white pill, hue carried only by the swatch dot. */
.trend-chip {
  display: inline-flex; align-items: center; gap: 7px;
  padding: 5px 11px; border-radius: var(--radius-full);
  font-size: 12px; font-weight: 500; cursor: pointer;
  background: transparent; color: var(--muted);
  border: 1px solid transparent; user-select: none;
  transition: background 0.15s, color 0.15s, border-color 0.15s, box-shadow 0.15s;
}
.trend-chip:hover { background: var(--surface2); color: var(--text2); }
.trend-chip.active {
  background: var(--bg);
  border-color: var(--border);
  color: var(--text);
  box-shadow: var(--shadow-sm);
}
.trend-chip.disabled { opacity: 0.45; cursor: not-allowed; }
.trend-chip .swatch { width: 8px; height: 8px; border-radius: 50%; display: inline-block; flex-shrink: 0; }
.trend-chart-wrap {
  width: 100%;
  overflow-x: auto;
  background: var(--bg);
  border: 1px solid var(--border);
  border-radius: var(--radius-lg);
  padding: 12px 12px 4px;
}
.trend-chart-wrap svg { display: block; width: 100%; height: 300px; }
.trend-grid { stroke: var(--border); stroke-width: 1; }
.trend-axis-label { fill: var(--muted); font-size: 11px; font-family: inherit; }
.trend-line { fill: none; stroke-width: 2; stroke-linecap: round; stroke-linejoin: round; }
.trend-dot { stroke: var(--bg); stroke-width: 1.5; }
.trend-empty { color: var(--muted); padding: 24px; text-align: center; }

/* ── SPENDING TABLE ───────────────────────────────────────────────────────── */
.cat-link {
  color: var(--text);
  text-decoration: underline;
  text-decoration-color: var(--border2);
  text-underline-offset: 3px;
  cursor: pointer;
  font-weight: 500;
}
.cat-link:hover { text-decoration-color: var(--text); }
.spending-table th { cursor: pointer; user-select: none; }
.spending-table th:hover { color: var(--text); }
.spending-table td { font-variant-numeric: tabular-nums; }
.spending-table .pct-bar {
  display: inline-block;
  height: 5px;
  border-radius: var(--radius-full);
  background: var(--chart-1);
  vertical-align: middle;
  margin-left: 8px;
}
.spending-table .total-row td { border-top: 2px solid var(--border); font-weight: 700; color: var(--accent); }

/* ── RECURRING ────────────────────────────────────────────────────────────── */
.recurring-section { margin-bottom: 28px; }
.recurring-section table td { font-size: 13px; }

/* ── MODALS / SHEETS ──────────────────────────────────────────────────────── */
.modal-bg {
  display: none;
  position: fixed; inset: 0;
  background: var(--overlay);
  -webkit-backdrop-filter: blur(4px);
  backdrop-filter: blur(4px);
  z-index: 100;
  align-items: center;
  justify-content: center;
  padding: 20px;
}
.modal-bg.open { display: flex; }
.modal {
  position: relative;
  background: var(--bg);
  border: 1px solid var(--border);
  border-radius: var(--radius-lg);
  padding: 26px 28px;
  width: 460px;
  max-width: 100%;
  max-height: calc(100dvh - 40px);
  overflow-y: auto;
  box-shadow: var(--shadow-lg);
}
.modal-wide   { width: 480px; }
.modal-narrow { width: 380px; }
.modal h3 { font-size: 17px; font-weight: 600; margin-bottom: 4px; padding-right: 32px; letter-spacing: -0.3px; }
.modal .sub { font-size: 14px; color: var(--muted); margin-bottom: 20px; }
.modal-actions { display: flex; gap: 8px; margin-top: 24px; justify-content: flex-end; }
.modal-close {
  position: absolute;
  top: 14px; right: 14px;
  width: 32px; height: 32px;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  border: none;
  background: none;
  color: var(--muted);
  border-radius: var(--radius-sm);
  font-size: 20px;
  line-height: 1;
  cursor: pointer;
  transition: background 0.15s, color 0.15s;
}
.modal-close:hover { background: var(--surface2); color: var(--text); }

/* Change-password modal helpers */
.pw-reveal {
  display: flex;
  align-items: center;
  gap: 8px;
  cursor: pointer;
  font-size: 13px;
  color: var(--text2);
  user-select: none;
}
.pw-checks {
  list-style: none;
  margin: 14px 0 0;
  display: flex;
  flex-direction: column;
  gap: 7px;
}
.pw-checks li {
  display: flex;
  align-items: center;
  gap: 9px;
  font-size: 13px;
  color: var(--muted);
  transition: color 0.15s;
}
.pw-checks .dot {
  width: 15px; height: 15px;
  flex-shrink: 0;
  border-radius: 50%;
  border: 1.5px solid var(--border2);
  position: relative;
  transition: background 0.15s, border-color 0.15s;
}
.pw-checks li.met { color: var(--success-deep); }
.pw-checks li.met .dot { background: var(--success); border-color: var(--success); }
.pw-checks li.met .dot::after {
  content: '';
  position: absolute;
  left: 4px; top: 1px;
  width: 4px; height: 8px;
  border: solid #fff;
  border-width: 0 2px 2px 0;
  transform: rotate(45deg);
}

/* "More" sheet — mobile nav overflow */
.sheet-bg {
  display: none;
  position: fixed; inset: 0;
  background: var(--overlay);
  z-index: 95;
}
.sheet-bg.open { display: block; }
.more-sheet {
  position: absolute;
  left: 0; right: 0; bottom: 0;
  background: var(--bg);
  border-radius: var(--radius-lg) var(--radius-lg) 0 0;
  padding: 8px 12px calc(12px + var(--safe-b));
  box-shadow: var(--shadow-lg);
}
.sheet-handle {
  width: 38px; height: 4px;
  border-radius: var(--radius-full);
  background: var(--border2);
  margin: 6px auto 10px;
}
.sheet-item {
  display: flex;
  align-items: center;
  gap: 12px;
  width: 100%;
  min-height: 52px;
  padding: 12px 14px;
  background: none;
  border: none;
  border-radius: var(--radius-sm);
  font-family: inherit;
  font-size: 14.5px;
  font-weight: 500;
  color: var(--text);
  text-align: left;
  cursor: pointer;
}
.sheet-item:hover, .sheet-item.active { background: var(--surface2); }
.sheet-item.active { color: var(--accent); }
.sheet-item svg { width: 20px; height: 20px; flex-shrink: 0; }

/* ── TOAST ────────────────────────────────────────────────────────────────── */
.toast {
  position: fixed; bottom: 24px; right: 24px;
  background: var(--bg);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 14px 20px;
  font-size: 14px;
  z-index: 200;
  transform: translateY(80px);
  opacity: 0;
  transition: transform 0.3s, opacity 0.3s;
  max-width: 360px;
  box-shadow: var(--shadow-md);
  pointer-events: none;
}
.toast.show { transform: translateY(0); opacity: 1; }
.toast.success { border-left: 3px solid var(--success); color: var(--text2); }
.toast.error   { border-left: 3px solid var(--danger);  color: var(--text2); }

/* ── AUTH ─────────────────────────────────────────────────────────────────── */
.auth-screen {
  display: flex;
  align-items: center;
  justify-content: center;
  min-height: calc(100vh - var(--header-h));
  min-height: calc(100dvh - var(--header-h));
  padding: 20px;
  background: var(--surface);
}
.auth-box {
  background: var(--bg);
  border: 1px solid var(--border);
  border-radius: var(--radius-lg);
  padding: 36px;
  width: 420px;
  max-width: 100%;
  box-shadow: var(--shadow-md);
}
.auth-mark { width: 44px; height: 44px; display: block; margin-bottom: 18px; }
.auth-box h2 { font-size: 22px; font-weight: 600; margin-bottom: 4px; letter-spacing: -0.5px; }
.auth-box .subtitle { color: var(--muted); font-size: 14px; margin-bottom: 28px; }
.auth-error {
  background: var(--danger-light);
  border: 1px solid var(--danger-border);
  color: var(--danger);
  padding: 10px 14px;
  border-radius: var(--radius-sm);
  font-size: 13px;
  margin-bottom: 14px;
  display: none;
}
.auth-error.show { display: block; }

/* ── SPINNER / API OVERLAY ────────────────────────────────────────────────── */
.spinner {
  width: 32px;
  height: 32px;
  border: 3px solid var(--border);
  border-top-color: var(--accent);
  border-radius: 50%;
  animation: spin 0.6s linear infinite;
  margin: 0 auto;
}
@keyframes spin { to { transform: rotate(360deg); } }

#api-overlay {
  position: fixed;
  inset: 0;
  z-index: 9999;
  display: none;
  align-items: center;
  justify-content: center;
  background: rgba(9, 9, 11, 0.2);
  -webkit-backdrop-filter: blur(1.5px);
  backdrop-filter: blur(1.5px);
}
#api-overlay.show { display: flex; }

/* ── EMPTY STATES ─────────────────────────────────────────────────────────── */
.empty-state { text-align: center; padding: 56px 20px; color: var(--muted); }
.empty-state .big { font-size: 34px; margin-bottom: 12px; opacity: 0.45; }
.empty-state p { font-size: 14px; line-height: 1.8; }

/* ═══════════════════════════════════════════════════════════════════════════
   MOBILE — ≤768px
   ═══════════════════════════════════════════════════════════════════════════ */
@media (max-width: 900px) {
  /* iOS zooms the page when a focused field is under 16px. */
  input:not([type="checkbox"]), select, textarea { font-size: 16px; }

  header { padding: 0 14px; }

  /* Actions stack instead of crowding a narrow row. */
  .settings-row { flex-direction: column; align-items: stretch; gap: 12px; }
  .settings-row .btn { width: 100%; }
  .conn-pill { align-self: flex-start; }

  /* Let the document scroll natively instead of an inner 100vh box —
     this is what makes the iOS toolbar and momentum scrolling behave. */
  .container { height: auto; }
  .main {
    height: auto;
    overflow: visible;
    padding: 16px 14px calc(var(--nav-h) + var(--safe-b) + 24px);
  }

  .tabs { display: none; }
  .bottom-nav { display: flex; }

  /* Stat cards: 2×2, never a single column. */
  .cards { grid-template-columns: 1fr 1fr; gap: 10px; margin-bottom: 22px; }
  .card { padding: 14px; border-radius: var(--radius); }
  .card-value { font-size: 21px; letter-spacing: -0.4px; }
  .card-label { font-size: 10px; margin-bottom: 6px; }

  /* Filters: search on its own row, selects two-up. */
  .filters {
    display: grid;
    grid-template-columns: 1fr 1fr;
    gap: 8px;
    align-items: stretch;
  }
  .filters select,
  .filters input[type="text"] { width: 100% !important; min-height: 44px; }
  .filters input[type="text"] { grid-column: 1 / -1; }
  .filters > :only-child { grid-column: 1 / -1; }
  /* An odd trailing select would sit alone in a half-width cell — span it. */
  .filters select:last-of-type:nth-of-type(odd) { grid-column: 1 / -1; }

  /* The month is the primary filter: full width, first row, thumb-sized. */
  .filters .month-flip { grid-column: 1 / -1; width: 100%; height: 44px; }
  .month-arrow { width: 52px; }
  .month-label { flex: 1; min-width: 0; font-size: 14.5px; }

  /* Wide tables give way to record cards. */
  .table-wrap.mobile-cards { display: none; }
  .card-list { display: flex; flex-direction: column; gap: 10px; }

  /* Tables that stay: tighten and drop low-value columns. */
  th { padding: 10px 12px; }
  td { padding: 10px 12px; font-size: 13px; }
  .col-from { display: none; }
  #categories-table-wrap { max-width: none !important; }

  /* Broad graphs are hidden on mobile by design. */
  .trend-card { display: none; }

  /* Duplicates: the 7-column grid becomes a stacked card. */
  .dup-row {
    grid-template-columns: 20px 1fr auto;
    gap: 4px 10px;
    padding: 12px 14px;
    align-items: start;
  }
  .dup-marker { grid-row: 1; }
  .dup-row .col-mono { grid-column: 2; font-size: 11px; }
  .dup-row .col-amount { grid-column: 3; grid-row: 1; text-align: right; font-weight: 600; }
  .dup-row .col-desc {
    grid-column: 2 / -1;
    white-space: normal;
    overflow: hidden;
    display: -webkit-box;
    -webkit-line-clamp: 2;
    line-clamp: 2;
    -webkit-box-orient: vertical;
    font-size: 13px;
    word-break: break-word;
  }
  .dup-row .col-bank { grid-column: 2 / -1; }
  /* Category chip and the keep/delete buttons share one row. */
  .dup-row > div:nth-last-child(2) { grid-column: 2; margin-top: 8px; align-self: center; }
  .dup-actions { grid-column: 3; justify-content: flex-end; margin-top: 8px; }

  /* Modals become bottom sheets. */
  .modal-bg { align-items: flex-end; padding: 0; }
  .modal {
    width: 100%;
    max-width: 100%;
    border-radius: var(--radius-lg) var(--radius-lg) 0 0;
    border-bottom: none;
    padding: 22px 18px calc(18px + var(--safe-b));
    max-height: 88dvh;
  }
  .modal-actions { flex-direction: column-reverse; gap: 8px; }
  .modal-actions .btn { width: 100%; min-height: 46px; }

  .toast {
    left: 12px;
    right: 12px;
    bottom: calc(var(--nav-h) + var(--safe-b) + 12px);
    max-width: none;
  }

  .empty-state { padding: 40px 16px; }
  .auth-box { padding: 28px 22px; }
}

/* ═══════════════════════════════════════════════════════════════════════════
   TOUCH TARGETS — keyed off pointer type, not width, so tablets get them too
   ═══════════════════════════════════════════════════════════════════════════ */
@media (hover: none) and (pointer: coarse) {
  .btn-sm { min-height: 40px; padding: 9px 14px; }
  .icon-btn { width: 40px; height: 40px; font-size: 16px; }
  .note-btn { width: 40px; height: 40px; }
  .note-btn[data-tip]:hover::after { display: none; }
  /* The category chip is the primary action on these screens — size it for a thumb. */
  .tag { min-height: 40px; padding: 8px 14px; font-size: 13px; }
  .tag-static { min-height: 32px; }
  .filters select, .filters input[type="text"] { min-height: 44px; }
  #new-cat-name, #txn-search { min-height: 44px; }
}

/* ═══════════════════════════════════════════════════════════════════════════
   SMALL PHONES — ≤380px
   ═══════════════════════════════════════════════════════════════════════════ */
@media (max-width: 380px) {
  .main { padding: 14px 10px calc(var(--nav-h) + var(--safe-b) + 24px); }
  .card { padding: 12px; }
  .card-value { font-size: 19px; }
  .nav-item { font-size: 9px; }
  .rec-card { padding: 11px 12px; }
  /* Let long headers like "Transactions" wrap instead of forcing the table wide. */
  th { white-space: normal; }
  td, th { padding: 9px 8px; }
  /* Rule counts are the least load-bearing column — drop them at this width. */
  .col-rules { display: none; }
}

/* ═══════════════════════════════════════════════════════════════════════════
   REDUCED MOTION
   ═══════════════════════════════════════════════════════════════════════════ */
@media (prefers-reduced-motion: reduce) {
  *, *::before, *::after {
    animation-duration: 0.01ms !important;
    animation-iteration-count: 1 !important;
    transition-duration: 0.01ms !important;
    scroll-behavior: auto !important;
  }
  .spinner { animation: spin 1.2s linear infinite !important; }
}
