/* ==========================================================================
   Vault — premium fintech UI
   Minimalism / Swiss style: grid-based, high contrast, restrained colour.
   Dark by default, light on request. Colour is used sparingly and always
   semantically: income, expense, reserve, distribution, payment, outstanding.

   Typeface: IBM Plex Sans (self-hosted — no external request, works offline,
   and keeps the strict `font-src 'self'` CSP intact). IBM Plex Mono carries
   references and identifiers.

   Every text/background pair below was checked to at least 4.5:1.
   ========================================================================== */

@font-face {
  font-family: "IBM Plex Sans"; font-style: normal; font-weight: 400; font-display: swap;
  src: url("/fonts/ibm-plex-sans-latin-400-normal.woff2") format("woff2");
}
@font-face {
  font-family: "IBM Plex Sans"; font-style: normal; font-weight: 500; font-display: swap;
  src: url("/fonts/ibm-plex-sans-latin-500-normal.woff2") format("woff2");
}
@font-face {
  font-family: "IBM Plex Sans"; font-style: normal; font-weight: 600; font-display: swap;
  src: url("/fonts/ibm-plex-sans-latin-600-normal.woff2") format("woff2");
}
@font-face {
  font-family: "IBM Plex Sans"; font-style: normal; font-weight: 700; font-display: swap;
  src: url("/fonts/ibm-plex-sans-latin-700-normal.woff2") format("woff2");
}
@font-face {
  font-family: "IBM Plex Mono"; font-style: normal; font-weight: 400; font-display: swap;
  src: url("/fonts/ibm-plex-mono-latin-400-normal.woff2") format("woff2");
}
@font-face {
  font-family: "IBM Plex Mono"; font-style: normal; font-weight: 500; font-display: swap;
  src: url("/fonts/ibm-plex-mono-latin-500-normal.woff2") format("woff2");
}

:root {
  --bg: #0b1120;
  --bg-elev: #0f172a;
  --surface: #182030;
  --surface-2: #1e2738;
  --surface-3: #262f42;
  --border: #2b3448;
  --border-strong: #3a4459;
  --text: #f8fafc;
  --text-muted: #a8b3c4;
  --text-dim: #8995a8;

  --income: #34d399;
  --income-soft: rgba(52, 211, 153, .12);
  --expense: #fb923c;
  --expense-soft: rgba(251, 146, 60, .12);
  --reserve: #60a5fa;
  --reserve-soft: rgba(96, 165, 250, .12);
  --distribution: #a78bfa;
  --distribution-soft: rgba(167, 139, 250, .12);
  --payment: #22d3ee;
  --payment-soft: rgba(34, 211, 238, .12);
  --outstanding: #fbbf24;
  --outstanding-soft: rgba(251, 191, 36, .12);
  --danger: #f87171;
  --danger-soft: rgba(248, 113, 113, .12);

  --accent: #10b981;
  --accent-ink: #04140f;
  --focus: #5eead4;

  /* Dense dashboard spacing scale (8 -> 32px). */
  --space-1: 4px;
  --space-2: 8px;
  --space-3: 12px;
  --space-4: 16px;
  --space-5: 24px;
  --space-6: 32px;

  --radius: 10px;
  --radius-sm: 7px;
  --radius-lg: 16px;
  --shadow: 0 1px 2px rgba(0, 0, 0, .4), 0 8px 28px rgba(0, 0, 0, .34);
  --shadow-sm: 0 1px 2px rgba(0, 0, 0, .35);
  --sidebar-w: 252px;
  --tap: 44px;

  --font: "IBM Plex Sans", -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, system-ui, sans-serif;
  --mono: "IBM Plex Mono", ui-monospace, "SF Mono", "Cascadia Mono", Consolas, monospace;
}

[data-theme="light"] {
  --bg: #f6f7f9;
  --bg-elev: #ffffff;
  --surface: #ffffff;
  --surface-2: #f3f4f6;
  --surface-3: #e9ebef;
  --border: #e2e5ea;
  --border-strong: #cfd4dc;
  --text: #11151b;
  --text-muted: #5b6472;
  --text-dim: #6b7485;

  --income: #047857;
  --income-soft: rgba(4, 120, 87, .10);
  --expense: #c2410c;
  --expense-soft: rgba(194, 65, 12, .10);
  --reserve: #1d4ed8;
  --reserve-soft: rgba(29, 78, 216, .10);
  --distribution: #6d28d9;
  --distribution-soft: rgba(109, 40, 217, .10);
  --payment: #0e7490;
  --payment-soft: rgba(14, 116, 144, .10);
  --outstanding: #b45309;
  --outstanding-soft: rgba(180, 83, 9, .10);
  --danger: #b91c1c;
  --danger-soft: rgba(185, 28, 28, .10);

  --accent: #047857;
  --accent-ink: #ffffff;
  --focus: #0f766e;
  --shadow: 0 1px 2px rgba(16, 24, 40, .05), 0 10px 24px rgba(16, 24, 40, .08);
  --shadow-sm: 0 1px 2px rgba(16, 24, 40, .06);
}

* { box-sizing: border-box; }

html, body { height: 100%; }

body {
  margin: 0;
  background: var(--bg);
  color: var(--text);
  font-family: var(--font);
  font-size: 14px;
  line-height: 1.5;
  -webkit-font-smoothing: antialiased;
  text-rendering: optimizeLegibility;
}

[hidden] { display: none !important; }

h1, h2, h3, h4 { margin: 0; font-weight: 600; letter-spacing: -0.015em; }
p { margin: 0; }
a { color: var(--accent); text-decoration: none; }
a:hover { text-decoration: underline; }

:focus-visible {
  outline: 2px solid var(--focus);
  outline-offset: 2px;
  border-radius: 4px;
}

.muted { color: var(--text-muted); }
.dim { color: var(--text-dim); }
.num { font-variant-numeric: tabular-nums; font-feature-settings: "tnum"; }
.right { text-align: right; }
.nowrap { white-space: nowrap; }
.mono { font-family: var(--mono); font-size: .9em; letter-spacing: -.01em; }

/* Icons are decorative SVG that inherit colour and sit on the text baseline. */
.icon { flex: none; vertical-align: -0.125em; }

/* Every interactive element gets a pointer cursor and a visible hover change. */
button:not(:disabled), [role="button"], summary, a { cursor: pointer; }
button:disabled { cursor: not-allowed; }

/* ------------------------------------------------------------------ boot -- */

.boot { position: fixed; inset: 0; display: grid; place-items: center; background: var(--bg); z-index: 90; }

.spinner {
  width: 26px; height: 26px; border-radius: 50%;
  border: 2px solid var(--border-strong); border-top-color: var(--accent);
  animation: spin .7s linear infinite;
}
@keyframes spin { to { transform: rotate(360deg); } }

/* ----------------------------------------------------------------- login -- */

.login-shell {
  min-height: 100%;
  display: grid; place-items: center; padding: 24px;
  background:
    radial-gradient(900px 500px at 20% -10%, rgba(16, 185, 129, .10), transparent 60%),
    radial-gradient(700px 420px at 90% 10%, rgba(96, 165, 250, .08), transparent 60%),
    var(--bg);
}

.login-card {
  width: min(400px, 100%);
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius-lg);
  padding: 28px;
  box-shadow: var(--shadow);
  display: grid; gap: 14px;
}
.login-brand { margin-bottom: 4px; }
.login-card h2 { font-size: 20px; }

/* --------------------------------------------------------------- shell ---- */

.app-shell {
  display: grid; grid-template-columns: var(--sidebar-w) 1fr;
  min-height: 100vh; min-height: 100dvh;
}

.sidebar {
  background: var(--bg-elev);
  border-right: 1px solid var(--border);
  display: flex; flex-direction: column;
  position: sticky; top: 0; height: 100vh; height: 100dvh;
  padding: 18px 12px 14px;
  z-index: 40;
}

.brand { display: flex; align-items: center; gap: 10px; padding: 0 8px 18px; }
.brand-mark {
  width: 32px; height: 32px; flex: none;
  display: grid; place-items: center;
  background: linear-gradient(145deg, var(--accent), #0ea5e9);
  color: var(--accent-ink);
  border-radius: 9px; font-weight: 800; font-size: 15px;
}
.brand-text { display: grid; line-height: 1.2; }
.brand-text strong { font-size: 15px; letter-spacing: -.01em; }
.brand-text small { color: var(--text-dim); font-size: 11px; }

.nav { display: grid; gap: 2px; overflow-y: auto; flex: 1; }
.nav-group { margin-top: 14px; padding: 0 10px 6px; font-size: 10.5px; letter-spacing: .09em;
  text-transform: uppercase; color: var(--text-dim); font-weight: 600; }
.nav a {
  display: flex; align-items: center; gap: 10px;
  padding: 8px 10px; border-radius: var(--radius-sm);
  color: var(--text-muted); font-weight: 500; font-size: 13.5px;
  transition: background .14s ease, color .14s ease;
}
.nav a:hover { background: var(--surface-2); color: var(--text); text-decoration: none; }
.nav a.active { background: var(--surface-3); color: var(--text); }
.nav a.active .icon, .nav a:hover .icon { color: var(--accent); }
.nav .icon { color: var(--text-dim); transition: color .2s ease; }

.sidebar-foot { display: grid; gap: 8px; padding-top: 12px; border-top: 1px solid var(--border); }
.theme-toggle {
  display: flex; align-items: center; gap: 10px; width: 100%;
  background: none; border: 0; color: var(--text-muted); cursor: pointer;
  padding: 8px 10px; border-radius: var(--radius-sm); font: inherit; font-size: 13.5px;
}
.theme-toggle:hover { background: var(--surface-2); color: var(--text); }
.theme-toggle .icon { color: var(--text-dim); }

.user-chip {
  display: grid; gap: 2px; padding: 9px 10px;
  background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius-sm);
}
.user-chip strong { font-size: 13px; }
.user-chip small { color: var(--text-dim); font-size: 11px; }
.user-chip button {
  margin-top: 6px; background: none; border: 0; padding: 0;
  color: var(--text-muted); font: inherit; font-size: 12px; cursor: pointer; text-align: left;
}
.user-chip button:hover { color: var(--danger); }

.main { display: flex; flex-direction: column; min-width: 0; }

.topbar {
  position: sticky; top: 0; z-index: 30;
  display: flex; align-items: center; gap: 14px;
  padding: 16px 24px;
  background: color-mix(in srgb, var(--bg) 86%, transparent);
  backdrop-filter: blur(10px);
  border-bottom: 1px solid var(--border);
}
.topbar-title { flex: 1; min-width: 0; }
.topbar-title h1 { font-size: 18px; }
.topbar-title p { color: var(--text-dim); font-size: 12.5px; margin-top: 2px; }
.topbar-actions { display: flex; gap: 8px; flex-wrap: wrap; justify-content: flex-end; }
/* Raised specificity: .icon-btn is declared later and would otherwise win
   on source order, leaving the hamburger visible on desktop. */
.topbar .menu-btn { display: none; }

.demo-banner {
  display: flex; gap: 8px; flex-wrap: wrap;
  margin: 16px 24px -4px; padding: 9px 14px;
  background: var(--outstanding-soft); border: 1px solid color-mix(in srgb, var(--outstanding) 35%, transparent);
  color: var(--outstanding); border-radius: var(--radius-sm); font-size: 12.5px;
}
.demo-banner span { color: var(--text-muted); }

.content { padding: 24px; display: grid; gap: 18px; align-content: start; min-height: 0; }

/* ---------------------------------------------------------------- cards -- */

.card {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  box-shadow: var(--shadow-sm);
}
.card-head {
  display: flex; align-items: center; gap: 12px; flex-wrap: wrap;
  padding: 14px 16px; border-bottom: 1px solid var(--border);
}
.card-head h2 { font-size: 14px; }
.card-head .sub { color: var(--text-dim); font-size: 12px; }
.card-head .spacer { flex: 1; }
.card-body { padding: 16px; }
.card-body.tight { padding: 0; }

.stat-grid {
  display: grid; gap: 12px;
  grid-template-columns: repeat(auto-fit, minmax(188px, 1fr));
}
.stat {
  background: var(--surface); border: 1px solid var(--border);
  border-radius: var(--radius); padding: 14px 15px;
  display: grid; gap: 6px; position: relative; overflow: hidden;
}
.stat::before {
  content: ""; position: absolute; inset: 0 auto 0 0; width: 3px;
  background: var(--tint, var(--border-strong));
}
.stat .label {
  display: flex; align-items: center; gap: 7px;
  font-size: 11px; text-transform: uppercase; letter-spacing: .07em;
  color: var(--text-dim); font-weight: 600;
}
.stat .label .ico { color: var(--tint, var(--text-dim)); }
.stat .value { font-size: 23px; font-weight: 650; letter-spacing: -.02em; }
.stat .value.pos { color: var(--income); }
.stat .value.neg { color: var(--danger); }
.stat .foot { font-size: 11.5px; color: var(--text-dim); }

.stat.income      { --tint: var(--income); }
.stat.expense     { --tint: var(--expense); }
.stat.reserve     { --tint: var(--reserve); }
.stat.distribution{ --tint: var(--distribution); }
.stat.payment     { --tint: var(--payment); }
.stat.outstanding { --tint: var(--outstanding); }

/* --------------------------------------------------------------- tables -- */

.table-wrap { overflow-x: auto; }

table.data { width: 100%; border-collapse: collapse; font-variant-numeric: tabular-nums; }
table.data th, table.data td { padding: 10px 14px; text-align: left; white-space: nowrap; }
table.data thead th {
  position: sticky; top: 0; z-index: 1;
  background: var(--surface-2);
  font-size: 10.5px; letter-spacing: .07em; text-transform: uppercase;
  color: var(--text-dim); font-weight: 600;
  border-bottom: 1px solid var(--border);
}
table.data thead th.sortable { padding: 0; }
table.data .th-sort {
  display: flex; align-items: center; gap: 5px;
  width: 100%; min-height: 34px; padding: 8px 14px;
  background: none; border: 0; color: inherit; font: inherit;
  letter-spacing: inherit; text-transform: inherit; cursor: pointer;
  transition: color .2s ease, background .2s ease;
}
table.data thead th.right .th-sort { justify-content: flex-end; }
table.data .th-sort:hover { color: var(--text); background: var(--surface-3); }
table.data .th-sort .arrow { opacity: .55; }
table.data thead th[aria-sort] .th-sort { color: var(--text); }
table.data thead th[aria-sort] .th-sort .arrow { opacity: 1; color: var(--accent); }
table.data tbody tr { border-bottom: 1px solid var(--border); }
table.data tbody tr:last-child { border-bottom: 0; }
table.data tbody tr:hover { background: var(--surface-2); }
table.data td.wrap { white-space: normal; max-width: 320px; }
table.data tfoot td {
  border-top: 1px solid var(--border-strong);
  background: var(--surface-2); font-weight: 600;
}
table.data .row-actions { display: flex; gap: 6px; justify-content: flex-end; }

.money { font-variant-numeric: tabular-nums; }
.money.pos { color: var(--income); }
.money.neg { color: var(--danger); }

.cell-stack { display: grid; gap: 1px; }
.cell-stack small { color: var(--text-dim); font-size: 11.5px; }

/* --------------------------------------------------------------- pills ---- */

.pill {
  display: inline-flex; align-items: center; gap: 5px;
  padding: 2px 8px; border-radius: 999px;
  font-size: 11px; font-weight: 600; letter-spacing: .02em;
  border: 1px solid transparent; white-space: nowrap;
}
.pill.active, .pill.approved, .pill.paid, .pill.verified
  { background: var(--income-soft); color: var(--income); border-color: color-mix(in srgb, var(--income) 30%, transparent); }
.pill.draft, .pill.calculated, .pill.unverified
  { background: var(--surface-3); color: var(--text-muted); border-color: var(--border-strong); }
.pill.void, .pill.reversed, .pill.disputed
  { background: var(--danger-soft); color: var(--danger); border-color: color-mix(in srgb, var(--danger) 30%, transparent); }
.pill.partially_paid, .pill.pending_approval, .pill.adjusted, .pill.open
  { background: var(--outstanding-soft); color: var(--outstanding); border-color: color-mix(in srgb, var(--outstanding) 30%, transparent); }
.pill.finalized { background: var(--reserve-soft); color: var(--reserve); border-color: color-mix(in srgb, var(--reserve) 30%, transparent); }
.pill.reopened { background: var(--distribution-soft); color: var(--distribution); border-color: color-mix(in srgb, var(--distribution) 30%, transparent); }
.pill.demo { background: var(--outstanding-soft); color: var(--outstanding); }

.basis-tag {
  display: inline-flex; align-items: center; gap: 6px;
  padding: 3px 9px; border-radius: 999px;
  background: var(--reserve-soft); color: var(--reserve);
  border: 1px solid color-mix(in srgb, var(--reserve) 28%, transparent);
  font-size: 11px; font-weight: 600;
}

/* -------------------------------------------------------------- buttons --- */

.btn {
  display: inline-flex; align-items: center; justify-content: center; gap: 7px;
  padding: 8px 14px; border-radius: var(--radius-sm);
  border: 1px solid var(--border-strong); background: var(--surface-2); color: var(--text);
  font: inherit; font-size: 13px; font-weight: 550; cursor: pointer;
  transition: background .14s ease, border-color .14s ease, transform .1s ease;
  white-space: nowrap;
}
.btn:hover { background: var(--surface-3); border-color: var(--border-strong); }
.btn:active { transform: translateY(1px); }
.btn:disabled { opacity: .5; cursor: not-allowed; }
.btn-primary { background: var(--accent); border-color: var(--accent); color: var(--accent-ink); }
.btn-primary:hover { background: color-mix(in srgb, var(--accent) 88%, #fff); border-color: transparent; }
.btn-danger { background: var(--danger-soft); border-color: color-mix(in srgb, var(--danger) 40%, transparent); color: var(--danger); }
.btn-danger:hover { background: color-mix(in srgb, var(--danger) 22%, transparent); }
.btn-ghost { background: transparent; border-color: transparent; color: var(--text-muted); }
.btn-ghost:hover { background: var(--surface-2); color: var(--text); }
.btn-sm { padding: 5px 10px; font-size: 12px; }
.btn-block { width: 100%; }
.icon-btn {
  display: inline-grid; place-items: center; width: 32px; height: 32px;
  background: var(--surface-2); border: 1px solid var(--border); border-radius: var(--radius-sm);
  color: var(--text-muted); cursor: pointer; font-size: 15px;
}
.icon-btn:hover { color: var(--text); background: var(--surface-3); }

/* --------------------------------------------------------------- forms ---- */

.field { display: grid; gap: 6px; min-width: 0; }
.field > span, .field-label {
  font-size: 12px; font-weight: 600; color: var(--text-muted);
  display: flex; align-items: center; gap: 6px;
}
.field .req { color: var(--danger); font-weight: 700; }
.field .hint { font-size: 11.5px; color: var(--text-dim); font-weight: 400; }

input, select, textarea {
  width: 100%; padding: 8px 11px;
  background: var(--bg-elev); color: var(--text);
  border: 1px solid var(--border-strong); border-radius: var(--radius-sm);
  font: inherit; font-size: 13.5px;
  transition: border-color .14s ease, box-shadow .14s ease;
}
input:hover, select:hover, textarea:hover { border-color: var(--text-dim); }
input:focus, select:focus, textarea:focus {
  outline: none; border-color: var(--accent);
  box-shadow: 0 0 0 3px color-mix(in srgb, var(--accent) 18%, transparent);
}
input[aria-invalid="true"], select[aria-invalid="true"] {
  border-color: var(--danger); box-shadow: 0 0 0 3px var(--danger-soft);
}
/* A control the current choice has ruled out: visibly out of play, and still
   readable, so it is clear which half of a two-way filter is answering. */
input:disabled, select:disabled, textarea:disabled {
  background: var(--surface-2); color: var(--text-dim);
  border-color: var(--border); cursor: not-allowed;
}
input:disabled:hover, select:disabled:hover { border-color: var(--border); }
.field.is-inactive > span { opacity: .55; }
textarea { resize: vertical; min-height: 68px; }
input[type="month"], input[type="date"] { color-scheme: dark; }
[data-theme="light"] input[type="month"], [data-theme="light"] input[type="date"] { color-scheme: light; }
input::placeholder, textarea::placeholder { color: var(--text-dim); }

.field-error { color: var(--danger); font-size: 11.5px; }

/*
 * Focusable error summary. Complements the inline per-field messages: focus
 * lands here after a failed submit and each item links to its invalid field.
 */
.error-summary {
  background: var(--danger-soft);
  border: 1px solid color-mix(in srgb, var(--danger) 42%, transparent);
  border-left: 3px solid var(--danger);
  border-radius: var(--radius-sm);
  padding: 12px 14px;
}
.error-summary:focus-visible { outline: 2px solid var(--danger); outline-offset: 2px; }
.error-summary h3 { font-size: 13px; color: var(--danger); margin-bottom: 6px; }
.error-summary ul { margin: 0; padding-left: 18px; display: grid; gap: 3px; }
.error-summary li { font-size: 12.5px; color: var(--text); }
.error-summary a { color: var(--danger); text-decoration: underline; text-underline-offset: 2px; }

.form-error {
  background: var(--danger-soft); color: var(--danger);
  border: 1px solid color-mix(in srgb, var(--danger) 30%, transparent);
  border-radius: var(--radius-sm); padding: 8px 11px; font-size: 12.5px;
}
.form-warning {
  background: var(--outstanding-soft); color: var(--outstanding);
  border: 1px solid color-mix(in srgb, var(--outstanding) 30%, transparent);
  border-radius: var(--radius-sm); padding: 8px 11px; font-size: 12.5px;
}
.form-note {
  background: var(--reserve-soft); color: var(--reserve);
  border: 1px solid color-mix(in srgb, var(--reserve) 26%, transparent);
  border-radius: var(--radius-sm); padding: 8px 11px; font-size: 12.5px;
}

.grid-2 { display: grid; gap: 12px; grid-template-columns: repeat(auto-fit, minmax(190px, 1fr)); }
.grid-3 { display: grid; gap: 12px; grid-template-columns: repeat(auto-fit, minmax(150px, 1fr)); }
.form-grid { display: grid; gap: 14px; }
.form-section {
  border: 1px solid var(--border); border-radius: var(--radius);
  padding: 14px; display: grid; gap: 12px; background: var(--surface-2);
}
.form-section > h3 {
  font-size: 11px; text-transform: uppercase; letter-spacing: .07em; color: var(--text-dim);
}
.form-actions { display: flex; gap: 8px; justify-content: flex-end; flex-wrap: wrap; }

/* -------------------------------------------------------------- filters --- */

.filters { display: grid; gap: 12px; }
.filter-row { display: flex; gap: 10px; flex-wrap: wrap; align-items: flex-end; }
.filter-row .field { flex: 1 1 150px; }
.presets { display: flex; gap: 6px; flex-wrap: wrap; }
.preset {
  padding: 5px 11px; border-radius: 999px; font-size: 12px; cursor: pointer;
  background: var(--surface-2); border: 1px solid var(--border); color: var(--text-muted);
}
.preset:hover { color: var(--text); border-color: var(--border-strong); }
.preset.active { background: var(--accent); border-color: var(--accent); color: var(--accent-ink); font-weight: 600; }

.active-filters { display: flex; gap: 6px; flex-wrap: wrap; }
.chip {
  display: inline-flex; align-items: center; gap: 6px;
  padding: 3px 6px 3px 10px; border-radius: 999px; font-size: 11.5px;
  background: var(--surface-3); border: 1px solid var(--border-strong); color: var(--text-muted);
}
.chip b { color: var(--text); font-weight: 600; }
.chip button {
  background: none; border: 0; color: var(--text-dim); cursor: pointer;
  font-size: 13px; line-height: 1; padding: 0 2px;
}
.chip button:hover { color: var(--danger); }

/* A checkbox row: box and label on one line, the whole row clickable. */
.field-check { gap: 6px; }
.check-row {
  display: flex; align-items: center; gap: 10px; cursor: pointer;
  font-size: 13px; color: var(--text); min-height: var(--tap);
}
.check-row input[type='checkbox'] {
  width: 18px; height: 18px; min-height: 0; flex: 0 0 auto;
  accent-color: var(--accent); cursor: pointer;
}

/*
 * Filter disclosure.
 *
 * The dashboard opens on its numbers, not on its controls, so the panel stays
 * collapsed until it is asked for. The trigger carries the active count, which
 * is the one thing that must never be hidden: a filtered dashboard would
 * otherwise be indistinguishable from the whole picture.
 */
.filter-toggle[aria-expanded='true'] {
  background: var(--surface-3);
  border-color: color-mix(in srgb, var(--accent) 55%, var(--border-strong));
  color: var(--text);
}
.filter-toggle .count {
  display: inline-grid; place-items: center;
  min-width: 18px; height: 18px; padding: 0 5px; margin-left: 1px;
  border-radius: 999px; background: var(--accent); color: var(--accent-ink);
  font-size: 11px; font-weight: 700; font-variant-numeric: tabular-nums;
}

/* Opacity and transform only: animating height would reflow every card below
   it on each frame. The entrance is animated and the exit is immediate. */
.filter-panel {
  opacity: 0; transform: translateY(-6px);
  transition: opacity .16s ease, transform .16s ease;
}
.filter-panel.open { opacity: 1; transform: none; }

/* For status text that only a screen reader needs. */
.sr-only {
  position: absolute; width: 1px; height: 1px; padding: 0; margin: -1px;
  overflow: hidden; clip-path: inset(50%); white-space: nowrap; border: 0;
}

/* --------------------------------------------------------------- charts -- */

.chart-grid { display: grid; gap: 14px; grid-template-columns: repeat(auto-fit, minmax(330px, 1fr)); }
.chart-box { position: relative; height: 260px; }
.chart-box canvas { max-height: 100%; }

/* ------------------------------------------------------- misc components -- */

.empty {
  display: grid; gap: 8px; justify-items: center; text-align: center;
  padding: 44px 20px; color: var(--text-muted);
}
.empty .ico { color: var(--text-dim); opacity: .8; }
.empty strong { color: var(--text); font-size: 14px; }
.empty p { font-size: 13px; max-width: 42ch; }

.skeleton {
  background: linear-gradient(90deg, var(--surface-2) 25%, var(--surface-3) 50%, var(--surface-2) 75%);
  background-size: 200% 100%; animation: shimmer 1.3s infinite;
  border-radius: var(--radius-sm); height: 14px;
}
@keyframes shimmer { to { background-position: -200% 0; } }

.pagination {
  display: flex; align-items: center; gap: 10px; flex-wrap: wrap;
  padding: 11px 16px; border-top: 1px solid var(--border); font-size: 12.5px; color: var(--text-muted);
}
.pagination .spacer { flex: 1; }

.toasts {
  position: fixed; right: 18px; bottom: 18px; z-index: 80;
  display: grid; gap: 8px; max-width: min(380px, calc(100vw - 36px));
}
.toast {
  display: flex; gap: 10px; align-items: flex-start;
  background: var(--surface); border: 1px solid var(--border-strong);
  border-left: 3px solid var(--tint, var(--accent));
  border-radius: var(--radius-sm); padding: 11px 13px; box-shadow: var(--shadow);
  animation: toast-in .2s ease;
}
.toast.success { --tint: var(--income); }
.toast.error { --tint: var(--danger); }
.toast.warning { --tint: var(--outstanding); }
.toast.info { --tint: var(--reserve); }
.toast .toast-ico { color: var(--tint); margin-top: 2px; }
.toast .body { flex: 1; font-size: 13px; }
.toast .body strong { display: block; margin-bottom: 1px; }
.toast .body span { color: var(--text-muted); font-size: 12.5px; }
.toast .toast-close {
  display: grid; place-items: center; width: 24px; height: 24px; margin: -2px -4px 0 0;
  background: none; border: 0; border-radius: 5px; color: var(--text-dim); cursor: pointer;
}
.toast .toast-close:hover { background: var(--surface-3); color: var(--text); }
@keyframes toast-in { from { opacity: 0; transform: translateY(8px); } }

.modal-backdrop {
  position: fixed; inset: 0; z-index: 70;
  background: rgba(3, 5, 8, .62); backdrop-filter: blur(3px);
  display: grid; place-items: center; padding: 20px;
  animation: fade .15s ease;
}
@keyframes fade { from { opacity: 0; } }
.modal {
  width: min(680px, 100%);
  max-height: min(88vh, 900px); max-height: min(88dvh, 900px);
  background: var(--surface); border: 1px solid var(--border-strong);
  border-radius: var(--radius-lg); box-shadow: var(--shadow);
  display: flex; flex-direction: column; overflow: hidden;
  animation: modal-in .18s cubic-bezier(.2, .9, .3, 1);
}
.modal.wide { width: min(940px, 100%); }
@keyframes modal-in { from { opacity: 0; transform: translateY(10px) scale(.99); } }
.modal-head {
  display: flex; align-items: center; gap: 12px;
  padding: 16px 18px; border-bottom: 1px solid var(--border);
}
.modal-head h2 { font-size: 15px; flex: 1; }
.modal-body { padding: 18px; overflow-y: auto; display: grid; gap: 14px; }
.modal-foot {
  display: flex; gap: 8px; justify-content: flex-end; flex-wrap: wrap;
  padding: 14px 18px; border-top: 1px solid var(--border); background: var(--surface-2);
}

.detail-list { display: grid; gap: 0; }
.detail-list > div {
  display: grid; grid-template-columns: minmax(130px, 34%) 1fr; gap: 12px;
  padding: 8px 0; border-bottom: 1px solid var(--border); font-size: 13px;
}
.detail-list > div:last-child { border-bottom: 0; }
.detail-list dt, .detail-list .k { color: var(--text-dim); font-size: 12.5px; }

.timeline { display: grid; gap: 10px; }
.timeline-item {
  display: grid; gap: 3px; padding-left: 16px; position: relative; font-size: 12.5px;
}
.timeline-item::before {
  content: ""; position: absolute; left: 0; top: 6px;
  width: 7px; height: 7px; border-radius: 50%; background: var(--border-strong);
}
.timeline-item strong { font-size: 13px; }
.timeline-item .when { color: var(--text-dim); font-size: 11.5px; }
.timeline-item .changes { color: var(--text-muted); font-family: var(--mono); font-size: 11.5px; }

.split { display: grid; gap: 14px; grid-template-columns: 1.6fr 1fr; align-items: start; }
.stack { display: grid; gap: 14px; }
.row { display: flex; gap: 10px; align-items: center; flex-wrap: wrap; }

.bar-track { height: 6px; background: var(--surface-3); border-radius: 999px; overflow: hidden; }
.bar-fill { height: 100%; background: var(--payment); border-radius: 999px; transition: width .4s ease; }

.alloc-row {
  display: grid; gap: 8px; grid-template-columns: 1.1fr 1.4fr .8fr auto;
  align-items: end; padding: 10px; background: var(--bg-elev);
  border: 1px solid var(--border); border-radius: var(--radius-sm);
}

/* ---------------------------------------------------------- responsive --- */

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

@media (max-width: 860px) {
  .app-shell { grid-template-columns: 1fr; }
  .sidebar {
    position: fixed; inset: 0 auto 0 0; width: var(--sidebar-w);
    transform: translateX(-100%); transition: transform .22s ease;
  }
  .sidebar.open { transform: none; box-shadow: var(--shadow); }
  .topbar .menu-btn { display: inline-grid; }
  .content { padding: 16px; }
  .topbar { padding: 13px 16px; }
  .demo-banner { margin: 12px 16px -4px; }

  /* Tables become stacked cards so nothing is lost on a phone. */
  table.data thead { display: none; }
  table.data, table.data tbody, table.data tr, table.data td { display: block; width: 100%; }
  table.data tbody tr {
    border: 1px solid var(--border); border-radius: var(--radius);
    margin: 10px; padding: 6px 0; background: var(--surface);
  }
  table.data td {
    display: grid; grid-template-columns: 42% 1fr; gap: 10px;
    white-space: normal; padding: 7px 13px; align-items: baseline;
  }
  table.data td::before {
    content: attr(data-label);
    font-size: 11px; text-transform: uppercase; letter-spacing: .06em;
    color: var(--text-dim); font-weight: 600;
  }
  table.data td.actions-cell { grid-template-columns: 1fr; }
  table.data td.actions-cell::before { display: none; }
  table.data .row-actions { justify-content: flex-start; }
  table.data tfoot { display: none; }
}

/*
 * Touch targets. On a pointer-coarse device every control gets at least 44x44
 * with real spacing between them, which is the difference between a usable
 * finance app on a phone and a frustrating one.
 */
/*
 * Phones. The breakpoint above this one turns tables into cards and reveals the
 * sidebar button; this one deals with what is left: multi-column form rows that
 * have no room to be multi-column, and the notch.
 */
@media (max-width: 560px) {
  .content { padding: 12px; }
  .topbar { padding: 11px 12px; }
  .demo-banner { margin: 10px 12px -4px; }

  /* A toolbar of three or four buttons gets its own line rather than
     squeezing the page title into nothing. */
  .topbar { flex-wrap: wrap; }
  .topbar-title { flex: 1 1 100%; }
  .topbar-actions { flex: 1 1 100%; justify-content: flex-start; }

  /* One field per line: a 150px minimum column still collides at 360px. */
  .grid-2, .grid-3 { grid-template-columns: 1fr; }
  .filter-row { flex-direction: column; align-items: stretch; }
  .filter-row .field { flex: 1 1 auto; }

  /* The allocation editor is four columns on a desktop. Stacked, each line
     keeps its labels and its Remove button stays reachable. */
  .alloc-row { grid-template-columns: 1fr; }
  .alloc-row .btn-danger { justify-self: start; }

  /* Label above value, so a long company name is not squeezed into 66%. */
  .detail-list > div { grid-template-columns: 1fr; gap: 2px; }

  /* Columns a list is not scanned by. Every one of them would be another line
     in every card, and the full record is one tap away. */
  .phone-hide { display: none !important; }

  .card-body, .card-head { padding: 13px; }
  .modal-backdrop { padding: 8px; }
  .modal, .modal.wide { width: 100%; max-height: 94dvh; }
  .modal-body { padding: 14px; }
  .modal-head, .modal-foot { padding: 12px 14px; }
  /* Full-width actions: a thumb finds them without aiming. */
  .modal-foot { flex-direction: column-reverse; }
  .modal-foot .btn { width: 100%; }
  .form-actions .btn { width: 100%; }
}

/*
 * Notches and home indicators. viewport-fit=cover lets the page use the whole
 * screen, which then needs the insets honoured by hand.
 */
@supports (padding: env(safe-area-inset-left)) {
  .topbar { padding-right: calc(16px + env(safe-area-inset-right)); }
  .content {
    padding-left: calc(16px + env(safe-area-inset-left));
    padding-right: calc(16px + env(safe-area-inset-right));
    padding-bottom: calc(16px + env(safe-area-inset-bottom));
  }
  .sidebar { padding-left: env(safe-area-inset-left); }
  .modal-foot { padding-bottom: calc(14px + env(safe-area-inset-bottom)); }
  .toasts { padding-bottom: env(safe-area-inset-bottom); }
}

@media (pointer: coarse) {
  .btn, .icon-btn, .preset, .theme-toggle, .nav a, table.data .th-sort {
    min-height: var(--tap);
  }
  .btn-sm { min-height: 38px; padding: 8px 12px; }
  .icon-btn { width: var(--tap); height: var(--tap); }
  input, select, textarea { min-height: var(--tap); font-size: 16px; } /* 16px avoids iOS zoom */
  .row-actions { gap: var(--space-2); }
  .chip { min-height: 32px; }
  .chip button { min-width: 28px; min-height: 28px; display: grid; place-items: center; }
  .toast .toast-close { width: 32px; height: 32px; }
}

@media (prefers-reduced-motion: reduce) {
  *, *::before, *::after {
    animation-duration: .01ms !important; animation-iteration-count: 1 !important;
    transition-duration: .01ms !important;
  }
}

@media print {
  .sidebar, .topbar-actions, .filters, .filter-panel, .pagination, .menu-btn { display: none !important; }
  body { background: #fff; color: #000; }
  .app-shell { grid-template-columns: 1fr; }
}

/* ===========================================================================
   Company structure and the SSN waterfall.
   =========================================================================== */

/* ------------------------------------------------------------- org tree -- */

.org-tree { display: grid; gap: 2px; }

.org-row {
  display: grid;
  grid-template-columns: 22px minmax(140px, 1.6fr) minmax(120px, 1fr) 90px auto;
  align-items: center;
  gap: var(--space-3);
  /* The depth custom property is the only thing that says "under": one rule
     indents every level, so adding a level needs no new CSS. */
  margin-left: calc(var(--depth, 0) * 26px);
  padding: 10px var(--space-3);
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
}
.org-row.kind-main { background: var(--surface-2); border-color: var(--border-strong); }
.org-row.inactive { opacity: .6; }

.org-mark { display: grid; place-items: center; color: var(--text-dim); }
.org-row.kind-main .org-mark { color: var(--accent); }
.org-row.kind-ssn .org-mark { color: var(--reserve); }

.org-name { display: grid; gap: 1px; min-width: 0; }
.org-name strong { font-size: 14px; }
.org-name small, .org-share small, .org-tax small {
  color: var(--text-dim); font-size: 11.5px;
}
.org-share, .org-tax { display: grid; gap: 1px; }
.org-share .num { font-weight: 700; }
.org-share.muted { color: var(--text-dim); font-size: 12px; }
.org-actions { display: flex; gap: 6px; justify-content: flex-end; flex-wrap: wrap; }
.org-actions .danger { color: var(--danger); }

.flow-note { display: grid; gap: var(--space-2); }
.flow-note p { margin: 0; font-size: 13px; line-height: 1.55; }
.flow-note .muted { color: var(--text-muted); }

.choice-grid { display: grid; gap: var(--space-2); }
.choice {
  display: grid; gap: 2px; text-align: left; cursor: pointer;
  padding: var(--space-3); border-radius: var(--radius-sm);
  background: var(--surface-2); border: 1px solid var(--border); color: var(--text);
}
.choice:hover { border-color: var(--accent); }
.choice small { color: var(--text-dim); }

/* ------------------------------------------------------------ waterfall -- */

.waterfall { display: grid; gap: 1px; }

.wf-row {
  display: flex; align-items: baseline; justify-content: space-between;
  gap: var(--space-3); padding: 9px var(--space-3);
  border-bottom: 1px solid var(--border);
}
.wf-row:last-of-type { border-bottom: 0; }
.wf-label { display: grid; gap: 1px; }
.wf-label small { color: var(--text-dim); font-size: 11.5px; }
.wf-value { font-weight: 700; font-variant-numeric: tabular-nums; }

.wf-row.plus .wf-value { color: var(--income); }
.wf-row.minus .wf-value { color: var(--expense); }
.wf-row.subtotal {
  background: var(--surface-2); border-radius: var(--radius-sm);
  border-bottom: 0; margin: 3px 0;
}
.wf-row.subtotal.strong .wf-value { font-size: 17px; font-weight: 800; }

.wf-split {
  display: grid; gap: var(--space-2); margin-top: var(--space-3);
  grid-template-columns: repeat(auto-fit, minmax(200px, 1fr));
}
.wf-half {
  display: grid; gap: 3px; padding: var(--space-3);
  border-radius: var(--radius-sm); border: 1px solid var(--border);
}
.wf-half.keep { background: var(--reserve-soft); border-color: var(--reserve); }
.wf-half.up { background: var(--income-soft); border-color: var(--accent); }
.wf-half-label {
  display: flex; align-items: center; gap: 6px;
  font-size: 12px; color: var(--text-muted);
}
.wf-half-value { font-size: 19px; font-weight: 800; }
.wf-half small { color: var(--text-dim); font-size: 11.5px; }

.wf-check {
  display: flex; align-items: center; gap: 6px; margin: var(--space-3) 0 0;
  font-size: 12px; font-variant-numeric: tabular-nums;
}
.wf-check.ok { color: var(--text-dim); }
.wf-check.bad { color: var(--danger); font-weight: 700; }
.form-note.warn { color: var(--outstanding); }

/* ------------------------------------------------------- small helpers -- */

.inline-field { display: flex; gap: var(--space-2); align-items: flex-end; }
.inline-field .field { margin: 0; min-width: 150px; }
.inline-field .field span { font-size: 11px; }

.btn-group { display: inline-flex; gap: 6px; }
.row-actions { display: flex; gap: 4px; justify-content: flex-end; }

.tabs { display: flex; gap: 4px; margin-bottom: var(--space-3); flex-wrap: wrap; }
.tab {
  padding: 8px var(--space-3); border-radius: var(--radius-sm);
  background: transparent; border: 1px solid transparent;
  color: var(--text-muted); cursor: pointer; font-size: 13px; font-weight: 600;
}
.tab:hover { background: var(--surface-2); color: var(--text); }
.tab.active { background: var(--surface); border-color: var(--border-strong); color: var(--text); }

.link-btn {
  background: none; border: 0; padding: 0; cursor: pointer;
  color: var(--accent); font: inherit; font-weight: 600; text-align: left;
}
.link-btn:hover { text-decoration: underline; }

@media (max-width: 720px) {
  .org-row {
    grid-template-columns: 22px 1fr;
    grid-template-areas: 'mark name' '. share' '. tax' '. actions';
    row-gap: 6px;
  }
  .org-mark { grid-area: mark; }
  .org-name { grid-area: name; }
  .org-share { grid-area: share; }
  .org-tax { grid-area: tax; }
  .org-actions { grid-area: actions; justify-content: flex-start; }
  .org-row { margin-left: calc(var(--depth, 0) * 14px); }
}

/* ---------------------------------------------- dashboard range and presets -- */

/* What the page is actually showing, stated above the figures. A filtered
   dashboard must never be mistakable for the whole picture. */
.range-bar {
  display: flex; align-items: center; gap: var(--space-2);
  flex-wrap: wrap; margin-bottom: var(--space-3);
}
.range-main { display: flex; align-items: center; gap: 6px; color: var(--text-muted); }
.range-main strong { color: var(--text); font-size: 14px; }
.range-main small { color: var(--text-dim); }

.preset-row {
  display: flex; align-items: center; gap: 6px;
  flex-wrap: wrap; margin-top: var(--space-3);
  padding-top: var(--space-3); border-top: 1px solid var(--border);
}
.preset-label { font-size: 11.5px; color: var(--text-dim); margin-right: 2px; }

/* ------------------------------------------- clickable stat cards + details -- */

/* A card that opens a breakdown looks like it will: it lifts on hover, and the
   open one is marked so which panel is showing is never ambiguous. */
.stat[role="button"] { transition: border-color .12s, transform .12s; }
.stat[role="button"]:hover { border-color: var(--border-strong); transform: translateY(-1px); }
.stat[role="button"]:focus-visible { outline: 2px solid var(--focus); outline-offset: 2px; }

.stat.open {
  border-color: var(--accent);
  box-shadow: inset 0 -2px 0 var(--accent);
}

.detail-stack { display: grid; gap: var(--space-3); }
.detail-head {
  font-size: 12px; text-transform: uppercase; letter-spacing: .06em;
  color: var(--text-dim); margin: var(--space-3) 0 var(--space-2);
}
