/* TVS-PMS design system (Phase 5 task 5.1, master plan §5.1; tokens from
   CLAUDE.md §5.1 and docs/brand-guidelines.md, verbatim).

   **Every template references a token (`var(--x)`), never a literal hex
   value** — enforced by `tests/test_brand_css.py`'s token-audit test
   (brand guidelines §7's shipping checklist, first item). This file is the
   one and only place a hex literal is allowed to appear.

   **Contrast pairings below are not arbitrary — every one is WCAG AA
   verified** (`tests/test_brand_css.py`'s contrast-check test recomputes
   every pair listed here in Python and asserts >= 4.5:1 for normal text,
   >= 3.0:1 for the two pairings marked large-text-only). Two real findings
   from that check, both encoded directly into the component rules below,
   not left to a template author to discover the hard way:
     - **`--orange` needs `--ink` text, not white** (`.btn--accent` below):
       white-on-orange is 2.71:1, well under AA. `--ink`-on-orange is
       5.77:1.
     - **`--lavender` is a swatch colour only, never a text background at
       body-text size**: both white-on-lavender (3.84:1) and
       ink-on-lavender (4.07:1) clear only the 3.0:1 large-text threshold,
       not the 4.5:1 normal-text one a small badge label needs. `.tag-dot`
       below carries the colour; the label stays `--ink` on `--bg-card`
       beside it, sidestepping the problem rather than shipping a badge
       that fails accessibility at the one size it would actually render.
*/

/* ---------- Reset ---------- */

*, *::before, *::after {
  box-sizing: border-box;
}

html, body, h1, h2, h3, h4, h5, h6, p, figure, blockquote, dl, dd, ul, ol {
  margin: 0;
}

ul, ol {
  padding: 0;
  list-style: none;
}

img, svg {
  display: block;
  max-width: 100%;
}

button, input, select, textarea {
  font: inherit;
  color: inherit;
}

/* ---------- Tokens ---------- */

:root {
  /* Colour — CLAUDE.md §5.1, verbatim */
  --blue: #144f92;
  --blue-dk: #0e3a6e;
  --blue-lt: #e8f0fe;
  --orange: #f47c22;
  --creative-blue: #76a2db;
  --lavender: #8874d1;
  --light-green: #95de9a;   /* BACKGROUND ONLY — always pair with --ink */
  --yellow-shandy: #f9e168; /* BACKGROUND ONLY — always pair with --ink */

  --ink: #1a2433;
  --muted: #5a6b80;
  --border: #dce7f4;
  --bg-page: #f4f7fb;
  --bg-card: #ffffff;

  --green: #1e7e34;
  --red: #b3261e;
  --green-text: #1e7e34;
  --amber-text: #8a5a00;

  --white: #ffffff;

  /* Typography — brand guidelines §3 */
  --font-display: "STIX Two Text", Georgia, "Times New Roman", serif;
  --font-body: "Source Sans Pro", -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, sans-serif;
  --font-mono: "JetBrains Mono", ui-monospace, "Cascadia Mono", Menlo, Consolas, monospace;

  /* 8-point spacing scale */
  --space-1: 4px;   /* half-step, use sparingly (icon gaps) */
  --space-2: 8px;
  --space-3: 16px;
  --space-4: 24px;
  --space-5: 32px;
  --space-6: 40px;
  --space-7: 48px;
  --space-8: 64px;

  /* Radii, borders, shadows */
  --radius-sm: 4px;
  --radius-md: 8px;
  --radius-pill: 999px;
  --shadow-card: 0 1px 2px rgba(20, 79, 146, 0.08), 0 1px 1px rgba(20, 79, 146, 0.04);
  --shadow-modal: 0 8px 24px rgba(20, 79, 146, 0.20);

  --container-max: 1600px;
}

/* ---------- Base ---------- */

body {
  font-family: var(--font-body);
  color: var(--ink);
  background: var(--bg-page);
  line-height: 1.5;
  -webkit-font-smoothing: antialiased;
}

h1, h2, h3, h4 {
  font-family: var(--font-display);
  color: var(--ink);
  line-height: 1.2;
}

a {
  color: var(--blue);
}
a:hover {
  color: var(--blue-dk);
}

/* Financial figures — tabular-nums so columns of digits stay aligned;
   never plain body font for a number a trader has to compare down a
   column (brand guidelines §3.2). */
.num {
  font-family: var(--font-mono);
  font-variant-numeric: tabular-nums;
}

/* ---------- Layout primitives ---------- */

.container {
  max-width: var(--container-max);
  margin-inline: auto;
  padding-inline: var(--space-4);
}

.stack {
  display: flex;
  flex-direction: column;
  gap: var(--space-3);
}

.cluster {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  gap: var(--space-2);
}

.grid {
  display: grid;
  gap: var(--space-3);
  grid-template-columns: repeat(var(--grid-cols, 4), minmax(0, 1fr));
}

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

/* ---------- Shell (layouts/base.html) ---------- */

.app-shell {
  display: grid;
  grid-template-columns: 220px 1fr;
  min-height: 100vh;
}

@media (max-width: 960px) {
  .app-shell {
    grid-template-columns: 64px 1fr;
  }
}

.app-nav {
  background: var(--blue-dk);
  color: var(--white);
  display: flex;
  align-items: center;
  gap: var(--space-3);
  padding-inline: var(--space-4);
  height: 64px;
}

.app-sidebar {
  background: var(--bg-card);
  border-right: 1px solid var(--border);
  padding: var(--space-3);
}

.app-sidebar a {
  display: block;
  padding: var(--space-2) var(--space-3);
  border-radius: var(--radius-sm);
  color: var(--ink);
  text-decoration: none;
}

.app-sidebar a:hover,
.app-sidebar a[aria-current="page"] {
  background: var(--blue-lt);
  color: var(--blue-dk);
}

.app-main {
  padding: var(--space-4);
}

/* Session / stale-valuation banners (0.2, 0.3 of the wireframes) */
.banner {
  display: flex;
  align-items: center;
  gap: var(--space-2);
  padding: var(--space-3);
  border-radius: var(--radius-md);
  margin-bottom: var(--space-4);
}
.banner--info {
  background: var(--blue-lt);
  color: var(--blue-dk);
}
.banner--stale {
  background: var(--yellow-shandy);
  color: var(--ink); /* CLAUDE.md §5.1: --yellow-shandy is background-only */
  font-weight: 700;
}

/* ---------- Card ---------- */

.card {
  background: var(--bg-card);
  border: 1px solid var(--border);
  border-radius: var(--radius-md);
  box-shadow: var(--shadow-card);
  padding: var(--space-4);
}

/* ---------- KPI card ---------- */

.kpi-card {
  background: var(--bg-card);
  border: 1px solid var(--border);
  border-radius: var(--radius-md);
  padding: var(--space-4);
}
.kpi-card__label {
  color: var(--muted);
  font-size: 0.875rem;
}
.kpi-card__value {
  font-family: var(--font-mono);
  font-variant-numeric: tabular-nums;
  font-size: 2rem;
  color: var(--ink);
}
/* Zero-pre-EOD state (wireframes 0.4/0.5): never a bare "0.0 bn" for a
   figure the EOD batch alone populates — an em dash with a captioned
   reason instead. */
.kpi-card__value--pending {
  color: var(--muted);
}
.kpi-card__caption {
  color: var(--muted);
  font-size: 0.8125rem;
}

/* ---------- Table ---------- */

.table {
  width: 100%;
  border-collapse: collapse;
  background: var(--bg-card);
}
.table th,
.table td {
  padding: var(--space-2) var(--space-3);
  border-bottom: 1px solid var(--border);
  text-align: left;
}
.table th {
  color: var(--muted);
  font-size: 0.8125rem;
  font-weight: 700;
  text-transform: uppercase;
  letter-spacing: 0.03em;
}
.table td.num,
.table th.num {
  text-align: right;
}
.table tbody tr:hover {
  background: var(--bg-page);
}

/* ---------- Badge ---------- */
/* One modifier per distinct enum family this system renders as a status
   pill (TxnStatus, AssetStatus, RepoStatus, PledgeStatus, RiskLimitStatus/
   ViolationStatus, FacilityStatus, LoanStatus) — mapped once in
   format.js (D5), never hand-picked per template. */

.badge {
  display: inline-flex;
  align-items: center;
  gap: var(--space-1);
  padding: var(--space-1) var(--space-2);
  border-radius: var(--radius-pill);
  font-size: 0.8125rem;
  font-weight: 700;
  line-height: 1.4;
}
.badge--neutral {
  background: var(--blue-lt);
  color: var(--blue-dk);
}
.badge--success {
  background: var(--green);
  color: var(--white);
}
.badge--danger {
  background: var(--red);
  color: var(--white);
}
.badge--warning {
  background: var(--yellow-shandy);
  color: var(--ink); /* background-only token — never any other text colour */
}
.badge--pending {
  background: var(--light-green);
  color: var(--ink); /* background-only token — never any other text colour */
}
.badge--info {
  background: var(--creative-blue);
  color: var(--ink); /* white-on-creative-blue fails AA (2.64:1); ink passes (5.92:1) */
}

/* Fund-category tag — a swatch dot, never a text-bearing pill (see the
   module-level note on --lavender). */
.tag-dot {
  display: inline-block;
  width: 10px;
  height: 10px;
  border-radius: 50%;
  background: var(--tag-color, var(--lavender));
}
.tag {
  display: inline-flex;
  align-items: center;
  gap: var(--space-1);
  color: var(--ink);
  font-size: 0.8125rem;
}

/* ---------- Button ---------- */

.btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: var(--space-1);
  padding: var(--space-2) var(--space-4);
  border-radius: var(--radius-sm);
  border: 1px solid transparent;
  font-weight: 700;
  cursor: pointer;
}
.btn--primary {
  background: var(--blue);
  color: var(--white);
}
.btn--primary:hover {
  background: var(--blue-dk);
}
.btn--accent {
  /* CTAs & warnings (CLAUDE.md §5.1) — ink text, not white: see the
     module-level note on --orange. */
  background: var(--orange);
  color: var(--ink);
}
.btn--ghost {
  background: transparent;
  color: var(--blue);
  border-color: var(--border);
}
.btn--ghost:hover {
  background: var(--blue-lt);
}
.btn--danger {
  background: var(--red);
  color: var(--white);
}
.btn:disabled {
  opacity: 0.5;
  cursor: not-allowed;
}

/* ---------- Form field ---------- */

.field {
  display: flex;
  flex-direction: column;
  gap: var(--space-1);
}
.field__label {
  font-size: 0.875rem;
  font-weight: 700;
  color: var(--ink);
}
.field__input {
  padding: var(--space-2) var(--space-3);
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  background: var(--bg-card);
  color: var(--ink);
}
.field__input:focus {
  outline: 2px solid var(--blue);
  outline-offset: 1px;
}
.field__input[aria-invalid="true"] {
  border-color: var(--red);
}
.field__error {
  color: var(--red);
  font-size: 0.8125rem;
}
.field__caption {
  color: var(--muted);
  font-size: 0.8125rem;
}
.field__input:disabled {
  background: var(--bg-page);
  color: var(--muted);
}

/* ---------- Modal ---------- */

.modal-backdrop {
  position: fixed;
  inset: 0;
  background: rgba(26, 36, 51, 0.5);
  display: flex;
  align-items: center;
  justify-content: center;
  padding: var(--space-4);
}
.modal {
  background: var(--bg-card);
  border-radius: var(--radius-md);
  box-shadow: var(--shadow-modal);
  max-width: 640px;
  width: 100%;
  max-height: 90vh;
  overflow-y: auto;
  padding: var(--space-4);
}
.modal__header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: var(--space-3);
}
.modal__footer {
  display: flex;
  justify-content: flex-end;
  gap: var(--space-2);
  margin-top: var(--space-4);
}

/* ---------- Toast ---------- */

.toast-region {
  position: fixed;
  bottom: var(--space-4);
  right: var(--space-4);
  display: flex;
  flex-direction: column;
  gap: var(--space-2);
  z-index: 1000;
}
.toast {
  background: var(--ink);
  color: var(--white);
  padding: var(--space-3) var(--space-4);
  border-radius: var(--radius-sm);
  box-shadow: var(--shadow-modal);
}
.toast--error {
  background: var(--red);
}
.toast--success {
  background: var(--green);
}

/* ---------- Pagination ---------- */

.pagination {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: var(--space-3);
  padding-top: var(--space-3);
  color: var(--muted);
  font-size: 0.875rem;
}
.pagination__pages {
  display: flex;
  gap: var(--space-1);
}
.pagination__pages button[aria-current="page"] {
  background: var(--blue);
  color: var(--white);
}

/* ---------- Audit trail ---------- */

.audit-trail {
  border-top: 1px solid var(--border);
  margin-top: var(--space-4);
  padding-top: var(--space-3);
}
.audit-trail__entry {
  display: grid;
  grid-template-columns: 160px 1fr;
  gap: var(--space-3);
  padding: var(--space-2) 0;
  border-bottom: 1px solid var(--border);
  font-size: 0.875rem;
}
.audit-trail__entry time {
  color: var(--muted);
  font-family: var(--font-mono);
}
/* Redacted fields (CLAUDE.md §6.5) never reach the template at all — this
   class exists only so a diff view can render the server's own
   "[REDACTED]" placeholder string with a visual cue, never to hide a
   real value client-side. */
.audit-trail__redacted {
  color: var(--muted);
  font-style: italic;
}

/* ---------- Tabs (task 5.4 addition — registry/facilities/risk_limits/
   admin all tab between record-type or sub-screen views; wireframes 0.4
   never named "tabs" as one of the seven components, so this extends
   the design system discovered against real templates rather than
   silently improvising an inline style) ---------- */

.tabs {
  display: flex;
  gap: var(--space-1);
  border-bottom: 1px solid var(--border);
  margin-bottom: var(--space-4);
}
.tabs [role="tab"] {
  background: none;
  border: none;
  border-bottom: 2px solid transparent;
  padding: var(--space-2) var(--space-3);
  font-weight: 700;
  color: var(--muted);
  cursor: pointer;
}
.tabs [role="tab"][aria-selected="true"] {
  color: var(--blue-dk);
  border-bottom-color: var(--blue);
}
.tabs [role="tab"]:hover {
  color: var(--blue-dk);
}

/* ---------- Meter (utilisation bars — facilities.html/risk_limits.html;
   task 5.4 addition, same reasoning as .tabs above) ---------- */

.meter {
  display: flex;
  align-items: center;
  gap: var(--space-2);
}
.meter__track {
  flex: 1;
  height: 8px;
  border-radius: var(--radius-pill);
  background: var(--bg-page);
  border: 1px solid var(--border);
  overflow: hidden;
}
.meter__fill {
  height: 100%;
  background: var(--green);
}
/* Utilisation bands — risk_limits.html: green < 85%, amber 85-100%, red
   >= 100% (BreachSeverity); facilities.html reuses the same convention
   for facility drawdown utilisation (UI-only, wireframes §9's own
   note: "a UI-only convention, not a data-model claim"). */
.meter__fill--warn {
  background: var(--amber-text);
}
.meter__fill--danger {
  background: var(--red);
}
.meter__label {
  font-family: var(--font-mono);
  font-variant-numeric: tabular-nums;
  font-size: 0.8125rem;
  color: var(--muted);
  min-width: 3.5em;
  text-align: right;
}

/* ---------- Accessibility helpers (task 5.4 addition) ---------- */

.sr-only {
  position: absolute;
  width: 1px;
  height: 1px;
  padding: 0;
  margin: -1px;
  overflow: hidden;
  clip: rect(0, 0, 0, 0);
  white-space: nowrap;
  border: 0;
}
.skip-link {
  position: absolute;
  left: var(--space-2);
  top: -48px;
  background: var(--blue-dk);
  color: var(--white);
  padding: var(--space-2) var(--space-3);
  border-radius: var(--radius-sm);
  z-index: 2000;
  transition: top 0.1s ease-in;
}
.skip-link:focus {
  top: var(--space-2);
}

/* ---------- Top nav additions (brand, portfolio selector, user menu —
   task 5.4 addition, layouts/base.html) ---------- */

.app-nav__brand {
  display: flex;
  align-items: center;
  gap: var(--space-2);
  color: var(--white);
  text-decoration: none;
  font-family: var(--font-display);
  font-weight: 700;
}
.app-nav__brand img {
  height: 28px;
  width: auto;
}
.app-nav__spacer {
  flex: 1;
}
.app-nav select.field__input,
.app-nav .field__input {
  background: var(--white);
}
.app-nav__user {
  position: relative;
}
.app-nav__user-toggle {
  color: var(--white);
}
.app-nav__user-roles {
  padding: var(--space-1) var(--space-3);
  color: var(--muted);
  font-size: 0.8125rem;
}
.app-nav__user-menu {
  position: absolute;
  right: 0;
  top: calc(100% + var(--space-1));
  background: var(--bg-card);
  color: var(--ink);
  border: 1px solid var(--border);
  border-radius: var(--radius-md);
  box-shadow: var(--shadow-modal);
  min-width: 220px;
  padding: var(--space-2);
  z-index: 900;
}
.app-nav__user-menu a,
.app-nav__user-menu button {
  display: block;
  width: 100%;
  text-align: left;
  padding: var(--space-2) var(--space-3);
  border-radius: var(--radius-sm);
  background: none;
  border: none;
  font: inherit;
  color: var(--ink);
  cursor: pointer;
}
.app-nav__user-menu a:hover,
.app-nav__user-menu button:hover {
  background: var(--blue-lt);
}

.app-sidebar--collapsed-label {
  display: none;
}
@media (max-width: 960px) {
  .app-sidebar span {
    display: none;
  }
}

.empty-state {
  color: var(--muted);
  text-align: center;
  padding: var(--space-5);
}

.section-title {
  font-family: var(--font-display);
  margin-bottom: var(--space-3);
}

/* Utility classes — task 5.4 addition. CLAUDE.md §2.1's CSP has no
   `unsafe-inline` on `style-src` either, so a template can never write
   `style="..."` (only `var(--token)` inside a *stylesheet* is exempt) —
   these exist so a template that needs "4 columns here, 2 there" or "a
   bit more top margin" reaches for a class, never an inline style. */
.grid-cols-2 { --grid-cols: 2; }
.grid-cols-3 { --grid-cols: 3; }
.grid-cols-4 { --grid-cols: 4; }
.mt-3 { margin-top: var(--space-3); }
.mt-4 { margin-top: var(--space-4); }
.cluster--between { justify-content: space-between; }
.w-full { width: 100%; }
.text-muted { color: var(--muted); }
.table-scroll { overflow-x: auto; }

.form-grid {
  display: grid;
  grid-template-columns: repeat(2, minmax(0, 1fr));
  gap: var(--space-3);
}
@media (max-width: 720px) {
  .form-grid {
    grid-template-columns: 1fr;
  }
}

/* ---------- Auth shell (login.html — task 5.4 addition; standalone,
   never `layouts/base.html`, since no sidebar/portfolio selector applies
   pre-session) ---------- */

.auth-body {
  min-height: 100vh;
  display: flex;
  align-items: center;
  justify-content: center;
  background: var(--blue-dk);
}
.auth-card {
  width: 100%;
  max-width: 420px;
  display: flex;
  flex-direction: column;
  gap: var(--space-3);
}
.auth-logo {
  height: 36px;
  width: auto;
  margin-bottom: var(--space-2);
}
.auth-recovery-codes {
  font-family: var(--font-mono);
  font-variant-numeric: tabular-nums;
  background: var(--bg-page);
  border-radius: var(--radius-sm);
  padding: var(--space-3);
}
.auth-qr :is(svg) {
  width: 200px;
  height: 200px;
}

/* ---------- Print (NAV/holdings statements — brand guidelines §6) ---------- */

@media print {
  .app-nav, .app-sidebar, .banner, .pagination, .btn, .toast-region {
    display: none !important;
  }
  .app-shell {
    display: block;
  }
  body {
    background: var(--white);
  }
  .card, .table {
    box-shadow: none;
    border: 1px solid var(--border);
  }
  a {
    color: var(--ink);
    text-decoration: none;
  }
}
