/* One stylesheet, implementing docs/DESIGN.md ("Fils").
 *
 * The application is dense, internal, and read on a desktop by people who use
 * it all day, so it is laid out for scanning tables rather than for a first
 * impression. Every rule below traces to a numbered section of that document;
 * the section is named where the reason is not obvious from the code.
 *
 * Two mechanical rules govern this file and are enforced by
 * tests/test_design_tokens.py:
 *
 *   - No colour is defined only under dark. The full light set lives on
 *     :root and dark redefines the same names (DESIGN.md 2.4).
 *   - No physical left/right property. Logical properties only, so turning
 *     Arabic on is a direction flip and not a rewrite (13).
 */

/* ── faces ────────────────────────────────────────────────────────────────
 * Self-hosted, committed, never fetched. The host may have no egress and
 * runs a strict CSP, which is the same reason jQuery is vendored (15).
 *
 * Only the Latin1 subset of each face ships. Arabic (13) is a third file
 * added when the locale is switched on, not a redesign.
 */

/* Inter is the UI face (3). The WOFF2 files are not committed yet, so the
   stack below falls through to the platform's own neutral grotesque -- SF on
   macOS, Segoe on Windows -- which is far closer to Inter than IBM Plex Sans
   was. Drop Inter-{Regular,Medium,SemiBold}-Latin1.woff2 into static/fonts/,
   uncomment the three blocks below, and add the preload to base.html.

@font-face {
  font-family: "Inter";
  src: url("../fonts/Inter-Regular-Latin1.woff2") format("woff2");
  font-weight: 400;
  font-style: normal;
  font-display: swap;
}

@font-face {
  font-family: "Inter";
  src: url("../fonts/Inter-Medium-Latin1.woff2") format("woff2");
  font-weight: 500;
  font-style: normal;
  font-display: swap;
}

@font-face {
  font-family: "Inter";
  src: url("../fonts/Inter-SemiBold-Latin1.woff2") format("woff2");
  font-weight: 600;
  font-style: normal;
  font-display: swap;
}

*/

@font-face {
  font-family: "IBM Plex Mono";
  src: url("../fonts/IBMPlexMono-Regular-Latin1.woff2") format("woff2");
  font-weight: 400;
  font-style: normal;
  font-display: swap;
}

@font-face {
  font-family: "IBM Plex Mono";
  src: url("../fonts/IBMPlexMono-Medium-Latin1.woff2") format("woff2");
  font-weight: 500;
  font-style: normal;
  font-display: swap;
}

/* ── tokens ───────────────────────────────────────────────────────────── */

:root {
  color-scheme: light;

  /* Foundation (2.1). ReUI is pure white on a neutral grey scale; the warm
     paper of the previous revision fought those greys. */
  --paper: #FFFFFF;
  --surface: #FFFFFF;
  --surface-sunken: #FAFAFA;
  --surface-muted: #F5F5F5;
  --ink: #0A0A0A;
  --ink-muted: #737373;
  --ink-faint: #A1A1A1;          /* decorative only. Never carries information. */
  --rule: #E5E5E5;
  --rule-strong: #D4D4D4;

  /* Primary (2.2). Near-black, not a brand colour. That is what frees the
     palette for signals, and it is why Principle 6 can be as strict as it is.
     This is the token ABK's brand sheet replaces; nothing else moves. */
  --primary: #171717;
  --primary-hover: #262626;
  --primary-ink: #FAFAFA;

  /* Links, focus and selection only -- never a button (2.2). */
  --accent: #1447E6;
  --accent-hover: #1035B5;
  --accent-subtle: #EEF2FF;

  /* Signal (2.3) -- the only semantic colours. Fill and ink are separate
     tokens because emerald-500 on white is about 2:1: fine for a 6px dot and
     illegible as a word (14). Only the -ink half may carry text. */
  --signal-good: #00BC7D;
  --signal-warn: #F0B100;
  --signal-bad: #E40014;
  --signal-none: #A1A1A1;

  --signal-good-ink: #004F3B;
  --signal-warn-ink: #733E0A;
  --signal-bad-ink: #9F0712;
  --signal-none-ink: #737373;

  --signal-good-wash: #E8F8F1;
  --signal-warn-wash: #FDF3D6;
  --signal-bad-wash: #FDEAEA;

  /* Typography (3). Inter for the UI, Plex Mono for every numeral compared
     column-to-column: a KWD figure in a proportional face is a defect. */
  --font-ui: "Inter", system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
  --font-mono: "IBM Plex Mono", ui-monospace, "SF Mono", Menlo, monospace;

  /* Space (4). 4px base. These eight steps and nothing else. */
  --s1: 4px;
  --s2: 8px;
  --s3: 12px;
  --s4: 16px;
  --s6: 24px;
  --s8: 32px;
  --s12: 48px;
  --s16: 64px;

  /* Density (4). One setting for the whole application, chosen in the header
     and remembered locally -- an operator comparing two screens should not
     have to re-set it. */
  --row-h: 40px;
  --cell-pad-block: 8px;
  --cell-pad-inline: 12px;

  /* Radius (4), from ReUI's --radius: .625rem. */
  --radius-sm: 6px;
  --radius: 10px;
  --radius-lg: 14px;

  /* Motion (12). Tailwind's ease-out, which is what ReUI transitions on.
     Everything not listed there changes state instantly. */
  --ease: cubic-bezier(0, 0, 0.2, 1);
  --motion-fast: 100ms;
  --motion: 150ms;

  /* Elevation (2.5). Exactly two, and no third: a hairline ring draws the
     edge of a container, a lift belongs to things that genuinely float. */
  --ring-hairline: 0 0 0 1px rgba(10, 10, 10, 0.10);
  --lift: 0 8px 24px rgba(10, 10, 10, 0.14);

  --sidebar-w: 256px;
  --sidebar-w-rail: 56px;
  --chrome-h: 48px;
  --context-h: 48px;
  --container-app: 80rem;

  --z-sticky: 20;
  --z-drawer: 40;
  --z-scrim: 100;
  --z-dialog: 110;
}

/* Dark (2.4) redefines the same token names and nothing else. Signals shift
   up in lightness to hold 4.5:1 on #101215. */
:root[data-theme="dark"] {
  color-scheme: dark;

  --paper: #0A0A0A;
  --surface: #171717;
  --surface-sunken: #171717;
  --surface-muted: #262626;
  --ink: #FAFAFA;
  --ink-muted: #A1A1A1;
  --ink-faint: #737373;
  --rule: rgba(255, 255, 255, 0.10);
  --rule-strong: rgba(255, 255, 255, 0.15);

  --primary: #E5E5E5;
  --primary-hover: #FAFAFA;
  --primary-ink: #171717;

  --accent: #7CA0FF;
  --accent-hover: #A5BEFF;
  --accent-subtle: #16203A;

  /* On a dark ground the 500 carries text, so fill and ink converge. */
  --signal-good: #00BC7D;
  --signal-warn: #F0B100;
  --signal-bad: #FF6568;
  --signal-none: #525252;

  --signal-good-ink: #00BC7D;
  --signal-warn-ink: #F0B100;
  --signal-bad-ink: #FF6568;
  --signal-none-ink: #A1A1A1;

  --signal-good-wash: #06251C;
  --signal-warn-wash: #2A1F06;
  --signal-bad-wash: #2B0F12;

  --ring-hairline: 0 0 0 1px rgba(255, 255, 255, 0.10);
  --lift: 0 8px 24px rgba(0, 0, 0, 0.5);
}

@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) {
    color-scheme: dark;

    --paper: #0A0A0A;
    --surface: #171717;
    --surface-sunken: #171717;
    --surface-muted: #262626;
    --ink: #FAFAFA;
    --ink-muted: #A1A1A1;
    --ink-faint: #737373;
    --rule: rgba(255, 255, 255, 0.10);
    --rule-strong: rgba(255, 255, 255, 0.15);

    --primary: #E5E5E5;
    --primary-hover: #FAFAFA;
    --primary-ink: #171717;

    --accent: #7CA0FF;
    --accent-hover: #A5BEFF;
    --accent-subtle: #16203A;

    /* On a dark ground the 500 carries text, so fill and ink converge. */
    --signal-good: #00BC7D;
    --signal-warn: #F0B100;
    --signal-bad: #FF6568;
    --signal-none: #525252;

    --signal-good-ink: #00BC7D;
    --signal-warn-ink: #F0B100;
    --signal-bad-ink: #FF6568;
    --signal-none-ink: #A1A1A1;

    --signal-good-wash: #06251C;
    --signal-warn-wash: #2A1F06;
    --signal-bad-wash: #2B0F12;

    --ring-hairline: 0 0 0 1px rgba(255, 255, 255, 0.10);
    --lift: 0 8px 24px rgba(0, 0, 0, 0.5);
  }
}

:root[data-density="compact"] {
  --row-h: 32px;
  --cell-pad-block: 4px;
}

/* ── base ─────────────────────────────────────────────────────────────── */

* { box-sizing: border-box; }

html, body { height: 100%; }

body {
  margin: 0;
  background: var(--paper);
  color: var(--ink);
  font: 400 14px/20px var(--font-ui);
  -webkit-font-smoothing: antialiased;
}

h1 { font: 600 20px/28px var(--font-ui); margin: 0; }
h2 { font: 600 16px/24px var(--font-ui); margin: 0; }
h3 { font: 600 14px/20px var(--font-ui); margin: 0; }

a { color: var(--accent); }

/* Visible on every interactive element (14). Never removed without a
   replacement. */
:focus-visible {
  outline: 2px solid var(--accent);
  outline-offset: 2px;
  border-radius: var(--radius-sm);
}

.visually-hidden {
  position: absolute;
  width: 1px;
  height: 1px;
  margin: -1px;
  padding: 0;
  overflow: hidden;
  clip-path: inset(50%);
  white-space: nowrap;
}

.icon {
  width: 16px;
  height: 16px;
  flex: none;
  fill: none;
  stroke: currentColor;
  stroke-width: 1.75;
  stroke-linecap: round;
  stroke-linejoin: round;
}

/* ── shell ────────────────────────────────────────────────────────────── */

.app {
  display: grid;
  grid-template-columns: var(--sidebar-w) minmax(0, 1fr);
  min-height: 100%;
  transition: grid-template-columns var(--motion) var(--ease);
}

:root[data-sidebar="rail"] .app { grid-template-columns: var(--sidebar-w-rail) minmax(0, 1fr); }

/* ── sidebar ──────────────────────────────────────────────────────────── */

.sidebar {
  position: sticky;
  inset-block-start: 0;
  align-self: start;
  height: 100vh;
  height: 100dvh;
  display: flex;
  flex-direction: column;
  gap: var(--s2);
  padding: var(--s2);
  background: var(--surface-sunken);
  border-inline-end: 1px solid var(--rule);
  overflow: hidden;
  z-index: var(--z-drawer);
}

.sidebar-brand {
  display: flex;
  align-items: center;
  gap: var(--s2);
  height: var(--chrome-h);
  padding-inline: var(--s2);
  color: var(--ink);
  text-decoration: none;
  font-weight: 600;
  white-space: nowrap;
}

.sidebar-brand .mark {
  display: grid;
  place-items: center;
  width: 24px;
  height: 24px;
  flex: none;
  border-radius: var(--radius-sm);
  background: var(--primary);
  color: var(--primary-ink);
  font: 600 11px/1 var(--font-mono);
  letter-spacing: 0.02em;
}

.nav { display: flex; flex-direction: column; gap: var(--s3); overflow-y: auto; flex: 1; }
.nav-group { display: flex; flex-direction: column; gap: 1px; }

.nav-label {
  padding: var(--s2) var(--s2) var(--s1);
  font: 500 12px/16px var(--font-ui);
  color: var(--ink-faint);
  white-space: nowrap;
}

.nav a {
  display: flex;
  align-items: center;
  gap: var(--s3);
  min-height: 32px;
  padding: var(--s2);
  border-radius: var(--radius);
  color: var(--ink-muted);
  text-decoration: none;
  white-space: nowrap;
  transition: background var(--motion-fast) var(--ease), color var(--motion-fast) var(--ease);
}

.nav a:hover { background: var(--surface-muted); color: var(--ink); }

.nav a[aria-current="page"] {
  background: var(--accent-subtle);
  color: var(--accent);
  font-weight: 500;
}

.sidebar-foot {
  border-block-start: 1px solid var(--rule);
  padding-block-start: var(--s2);
  display: flex;
  align-items: center;
  gap: var(--s2);
  min-height: 44px;
}

.user-chip {
  display: flex;
  align-items: center;
  gap: var(--s2);
  min-width: 0;
  flex: 1;
  padding-inline: var(--s2);
}

.user-chip .mark {
  display: grid;
  place-items: center;
  width: 26px;
  height: 26px;
  flex: none;
  border-radius: 50%;
  background: var(--surface-muted);
  border: 1px solid var(--rule);
  color: var(--ink-muted);
  font: 600 11px/1 var(--font-ui);
  text-transform: uppercase;
}

.user-chip .who {
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
  color: var(--ink);
}

/* The rail keeps the icons and drops the words. Section 14 requires an
   accessible name on an icon-only control, so the label is hidden visually
   and stays in the accessibility tree. */
:root[data-sidebar="rail"] .sidebar { padding-inline: var(--s1); }
:root[data-sidebar="rail"] .nav a { justify-content: center; padding-inline: 0; }
:root[data-sidebar="rail"] .nav a span,
:root[data-sidebar="rail"] .nav-label,
:root[data-sidebar="rail"] .sidebar-brand span,
:root[data-sidebar="rail"] .user-chip .who,
:root[data-sidebar="rail"] .signout {
  position: absolute;
  width: 1px;
  height: 1px;
  overflow: hidden;
  clip-path: inset(50%);
  white-space: nowrap;
}
:root[data-sidebar="rail"] .sidebar-brand { justify-content: center; padding-inline: 0; }
:root[data-sidebar="rail"] .user-chip { justify-content: center; padding-inline: 0; }

/* ── chrome strip ─────────────────────────────────────────────────────── */

.chrome {
  position: sticky;
  inset-block-start: 0;
  z-index: var(--z-sticky);
  display: flex;
  align-items: center;
  gap: var(--s2);
  height: var(--chrome-h);
  padding-inline: var(--s4);
  background: var(--paper);
  border-block-end: 1px solid var(--rule);
}

.crumbs {
  display: flex;
  align-items: center;
  gap: var(--s2);
  flex: 1;
  min-width: 0;
  color: var(--ink-muted);
}

.crumbs .sep { color: var(--ink-faint); }
.crumbs strong { color: var(--ink); font-weight: 600; }

/* ── context bar ──────────────────────────────────────────────────────── */

/* Period and company are global state (4). A user must never have to guess
   which month a number belongs to, so the selectors live here rather than
   being repeated on every screen that reads them. */
.context {
  position: sticky;
  inset-block-start: var(--chrome-h);
  z-index: var(--z-sticky);
  display: flex;
  align-items: center;
  gap: var(--s4);
  flex-wrap: wrap;
  min-height: var(--context-h);
  padding: var(--s2) var(--s4);
  background: var(--surface);
  border-block-end: 1px solid var(--rule);
}

.context .field { flex-direction: row; align-items: center; gap: var(--s2); }

/* ── page ─────────────────────────────────────────────────────────────── */

.page {
  padding: var(--s6) var(--s6) var(--s16);
  max-width: var(--container-app);
  margin-inline: auto;
}

.page-head {
  display: flex;
  align-items: start;
  gap: var(--s4);
  flex-wrap: wrap;
  margin-block-end: var(--s6);
}

.page-head .titles { flex: 1; min-width: 260px; }
.page-head .actions { display: flex; gap: var(--s2); flex-wrap: wrap; }

/* Prose is capped; tables are not -- they earn the full viewport (4). */
.lede { color: var(--ink-muted); margin: var(--s1) 0 0; max-width: 72ch; }

/* ── stat strip ───────────────────────────────────────────────────────── */

/* Counts here are derived from the rows on screen, never a server total:
   cursor pagination has no total and a count is a claim (Principle 2). */
.stats {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(180px, 1fr));
  gap: var(--s3);
  margin-block-end: var(--s4);
}

.stat {
  background: var(--surface);
  border-radius: var(--radius-lg);
  box-shadow: var(--ring-hairline);
  padding: var(--s4);
}

.stat .label {
  display: flex;
  align-items: center;
  gap: var(--s2);
  font: 500 14px/20px var(--font-ui);
  color: var(--ink-muted);
}

.stat .value {
  margin-block-start: var(--s1);
  font: 500 24px/32px var(--font-mono);
  font-variant-numeric: tabular-nums;
}

.stat .value.good { color: var(--signal-good-ink); }
.stat .value.warn { color: var(--signal-warn-ink); }
.stat .value.bad { color: var(--signal-bad-ink); }
.stat .value.none { color: var(--signal-none-ink); }

/* ── card ─────────────────────────────────────────────────────────────── */

.card {
  background: var(--surface);
  border-radius: var(--radius-lg);
  box-shadow: var(--ring-hairline);
  margin-block-end: var(--s4);
  overflow: hidden;
}

.card-head {
  display: flex;
  align-items: center;
  gap: var(--s3);
  flex-wrap: wrap;
  padding: var(--s3) var(--s4);
  border-block-end: 1px solid var(--rule);
}

.card-head .titles { flex: 1; min-width: 200px; }
.card-head .lede { font-size: 14px; }
.card-body { padding: var(--s4); }

.toolbar {
  display: flex;
  align-items: end;
  gap: var(--s2);
  flex-wrap: wrap;
  padding: var(--s3) var(--s4);
  border-block-end: 1px solid var(--rule);
}

/* A toolbar of controls that is not sitting above a table wants the grouping
   without the sunken band. */
.toolbar.bare { border: 0; padding: 0; }

/* Tables scroll inside their own container; the page body never scrolls
   horizontally (4). */
.scroll { overflow-x: auto; }

/* ── tables ───────────────────────────────────────────────────────────── */

/* The table is the primary component of this product (6). */
table { width: 100%; border-collapse: separate; border-spacing: 0; }

caption {
  text-align: start;
  padding: var(--s3) var(--s4);
  color: var(--ink-muted);
}

th, td {
  padding: var(--cell-pad-block) var(--cell-pad-inline);
  text-align: start;
  border-block-end: 1px solid var(--rule);
  height: var(--row-h);
}

thead th {
  position: sticky;
  inset-block-start: 0;
  z-index: 2;
  background: var(--surface);
  color: var(--ink);
  font: 500 14px/20px var(--font-ui);
  white-space: nowrap;
}

tbody td { font-size: 14px; line-height: 20px; }

/* No zebra striping. It fights the density toggle and adds noise to columns
   that are already aligned (6). */
tbody tr:hover td { background: var(--surface-muted); }
tbody tr:last-child td { border-block-end: 0; }

tbody tr[aria-selected="true"] td { background: var(--accent-subtle); }
tbody tr[aria-selected="true"] td:first-child { box-shadow: inset 2px 0 0 var(--accent); }

/* The first column is the identity and stays put when the table scrolls
   sideways (6). */
.sticky-id td:first-child,
.sticky-id th:first-child {
  position: sticky;
  inset-inline-start: 0;
  z-index: 1;
  background: var(--surface);
  border-inline-end: 1px solid var(--rule);
}

.sticky-id thead th:first-child { z-index: 3; background: var(--surface); }
.sticky-id tbody tr:hover td:first-child { background: var(--surface-muted); }

/* Money and rates are compared down the column, so they are end-aligned and
   tabular: proportional digits make 1,111 look narrower than 8,888 (5.1). */
td.num, th.num, .mono {
  text-align: end;
  font-family: var(--font-mono);
  font-variant-numeric: tabular-nums;
  font-feature-settings: "tnum";
}

th.num { text-align: end; }
tfoot td, tfoot th { font-weight: 500; background: var(--surface-muted); }

/* Identifiers: loan and invoice numbers, GL codes, hashes, UUIDs. Isolated
   from the bidi algorithm, because a period label reordered by it is a wrong
   period label (5.5, 13). */
.ident {
  font-family: var(--font-mono);
  font-size: 13px;
  direction: ltr;
  unicode-bidi: isolate;
  white-space: nowrap;
}

button.ident {
  border: 0;
  background: none;
  padding: 0;
  color: var(--accent);
  cursor: pointer;
  text-decoration: underline dotted;
  text-underline-offset: 3px;
}

/* Absence is not zero (Principle 1). A null money cell is a dash in the
   absence colour, never 0.000, never blank. */
.absent { color: var(--signal-none-ink); font-family: var(--font-mono); }

/* The only cells where the number itself carries signal colour (5.2). Inside
   the band the variance is explained, not tolerated, so it stays ink. */
.variance.out { color: var(--signal-bad-ink); }
.variance.in { color: var(--ink); }

td.empty { padding: var(--s12) var(--s4); border-block-end: 0; }

/* Empty state names why it is empty and what to do -- never a bare
   "No data" (6).

   It sticks to the scroll origin and aligns to the start rather than centring
   across the table. A wide table centres its empty row somewhere off-screen,
   which puts the explanation behind a horizontal scroll on exactly the
   screens that most need it. */
.empty-state {
  position: sticky;
  inset-inline-start: 0;
  display: grid;
  justify-items: start;
  gap: var(--s2);
  text-align: start;
  max-width: 60ch;
}
.empty-state .icon { width: 24px; height: 24px; color: var(--ink-faint); }
.empty-state .reason { color: var(--ink); font-weight: 600; }
.empty-state .next { color: var(--ink-muted); }

/* Loading is skeleton rows at the current density -- neutral bars, and never
   over a money cell, because a fake number on a bank screen is worse than a
   blank one (5.1, 6). */
.skeleton {
  display: block;
  height: 10px;
  border-radius: var(--radius-sm);
  background: var(--surface-muted);
}

/* ── pagination ───────────────────────────────────────────────────────── */

/* Forward via the API's next cursor, back via a client-held stack. The
   control reads Previous / Next and nothing else: no totals, no page
   numbers, no "of N" (6.1). */
.pager {
  display: flex;
  align-items: center;
  justify-content: center;
  gap: var(--s2);
  padding: var(--s3);
  border-block-start: 1px solid var(--rule);
}

/* ── status ───────────────────────────────────────────────────────────── */

/* The default badge is a dot plus a word (7). The word carries the meaning
   and the dot only reinforces it, so "never colour alone" holds (14). */
.tag {
  display: inline-flex;
  align-items: center;
  gap: var(--s2);
  color: var(--ink);
  white-space: nowrap;
}

.tag::before {
  content: "";
  width: 6px;
  height: 6px;
  flex: none;
  border-radius: 50%;
  background: var(--signal-none);
}

/* ABK.badge still builds a leading icon for every status. In the dot form the
   dot is the marker, so the icon is suppressed rather than shown alongside it
   -- two markers on one three-word label is noise. The filled form keeps it,
   because that is the one that has to be findable at a glance (7). */
.tag .icon { display: none; }

.tag.good::before { background: var(--signal-good); }
.tag.warn::before { background: var(--signal-warn); }
.tag.accent::before { background: var(--accent); }

/* Absence renders as a hollow ring: structurally different from the other
   three, and it must never read as good or bad (2.3, Principle 1). */
.tag.none::before {
  background: none;
  box-shadow: inset 0 0 0 1px var(--signal-none);
}

/* The filled form, for the states that must survive out of context: FAILED,
   REJECTED, MOVED, and a DIFF outside its band. A user scanning a job list
   must not have to read carefully to find the failure (7). */
.tag.bad {
  padding: 2px var(--s2);
  border-radius: var(--radius);
  background: var(--signal-bad-wash);
  color: var(--signal-bad-ink);
  font-size: 12px;
  line-height: 16px;
}

.tag.bad::before { background: var(--signal-bad); }
.tag.bad .icon { display: inline-block; width: 12px; height: 12px; color: currentColor; }

/* is_poci, is_non_accrual, is_derived. A derived collection is not an
   observed one, and the difference must survive a glance (7). */
.chip {
  display: inline-flex;
  align-items: center;
  gap: var(--s1);
  padding: 1px var(--s2);
  border: 1px solid var(--rule-strong);
  border-radius: var(--radius-sm);
  color: var(--ink-muted);
  font: 500 12px/16px var(--font-ui);
  white-space: nowrap;
}

.chip.stage-1 { color: var(--signal-good-ink); border-color: currentColor; }
.chip.stage-2 { color: var(--signal-warn-ink); border-color: currentColor; }
.chip.stage-3 { color: var(--signal-bad-ink); border-color: currentColor; }

/* Batch progression is a pipeline and should look like one, not six separate
   badges (7). */
/* One line, always. The table scrolls inside its own container (4); a
   pipeline broken across five rows stops reading as a pipeline. */
.stepper { display: flex; align-items: center; gap: var(--s1); flex-wrap: nowrap; }

/* A loose group of chips in a cell, which may wrap. */
.chips { display: flex; align-items: center; gap: var(--s1); flex-wrap: wrap; }
.stepper .step {
  display: inline-flex;
  align-items: center;
  gap: var(--s1);
  padding: 2px var(--s2);
  border-radius: var(--radius);
  border: 1px solid var(--rule-strong);
  color: var(--ink-faint);
  font: 400 12px/16px var(--font-ui);
  white-space: nowrap;
}
.stepper .step[data-state="done"] { color: var(--signal-good-ink); border-color: currentColor; }
.stepper .step[data-state="current"] { color: var(--ink); border-color: var(--ink); font-weight: 500; }
.stepper .step[data-state="failed"] { color: var(--signal-bad-ink); border-color: currentColor; }
.stepper .arrow { color: var(--ink-faint); }

/* Two or three controls sharing a table cell. Distinct from .stepper, which
   is a pipeline and is meant to wrap. */
.row-actions { display: flex; gap: var(--s2); align-items: center; }

/* ── controls ─────────────────────────────────────────────────────────── */

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

.field { display: inline-flex; flex-direction: column; gap: var(--s1); min-width: 0; }

.field > .label,
label > .label {
  font-size: 12px;
  line-height: 16px;
  color: var(--ink-muted);
}

.req { color: var(--signal-bad-ink); }

input, select, textarea {
  min-height: 32px;
  padding: var(--s1) var(--s3);
  border: 1px solid var(--rule-strong);
  border-radius: var(--radius);
  background: var(--surface);
  color: var(--ink);
}

input::placeholder, textarea::placeholder { color: var(--ink-faint); }

input[aria-invalid="true"], select[aria-invalid="true"], textarea[aria-invalid="true"] {
  border-color: var(--signal-bad);
}

.hint { font-size: 12px; line-height: 16px; color: var(--ink-muted); }
.hint.bad { color: var(--signal-bad-ink); }

/* Minimum target 24x24, including compact-density row controls (14). */
button {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: var(--s2);
  min-height: 32px;
  min-width: 32px;
  padding: var(--s1) var(--s3);
  border: 1px solid var(--primary);
  border-radius: var(--radius);
  background: var(--primary);
  color: var(--primary-ink);
  font-weight: 500;
  cursor: pointer;
  transition: background var(--motion-fast) var(--ease), border-color var(--motion-fast) var(--ease);
}

button:hover { background: var(--primary-hover); border-color: var(--primary-hover); }

button[disabled] {
  opacity: 0.5;
  cursor: not-allowed;
  background: var(--primary);
  border-color: var(--primary);
}

button.secondary { background: var(--surface); color: var(--ink); border-color: var(--rule); }
button.secondary:hover { background: var(--surface-muted); border-color: var(--rule-strong); }
button.secondary[disabled] { background: var(--surface); color: var(--ink-muted); }

/* Destructive actions use the danger signal and sit apart from the primary
   one (9). */
button.danger { background: var(--signal-bad); border-color: var(--signal-bad); color: #fff; }
button.danger:hover { filter: brightness(1.1); background: var(--signal-bad); }
button.danger[disabled] { background: var(--signal-bad); border-color: var(--signal-bad); }

button.link {
  background: none;
  border: 0;
  min-height: 24px;
  min-width: 0;
  padding: 0;
  color: var(--accent);
  font-weight: 400;
  text-decoration: underline;
}
button.link:hover { background: none; color: var(--accent-hover); }

button.icon-button {
  background: none;
  border: 1px solid transparent;
  border-radius: var(--radius);
  color: var(--ink-muted);
  padding: var(--s1);
}
button.icon-button:hover { background: var(--surface-muted); border-color: var(--rule); color: var(--ink); }
button.icon-button .icon { width: 18px; height: 18px; }

.signout { display: inline-flex; margin: 0; }

/* ── tabs ─────────────────────────────────────────────────────────────── */

.tabs {
  display: flex;
  gap: var(--s1);
  padding-inline: var(--s4);
  border-block-end: 1px solid var(--rule);
  overflow-x: auto;
}

.tabs button {
  background: none;
  border: 0;
  border-block-end: 2px solid transparent;
  border-radius: 0;
  color: var(--ink-muted);
  padding: var(--s3) var(--s2);
  white-space: nowrap;
}

.tabs button:hover { background: none; color: var(--ink); }
.tabs button[aria-selected="true"] { color: var(--ink); border-block-end-color: var(--ink); font-weight: 500; }

/* ── alerts ───────────────────────────────────────────────────────────── */

/* Errors the user must act on render inline at form level, never in a toast:
   a disappearing instruction is not an error message (8). */
.alert {
  display: none;
  gap: var(--s3);
  padding: var(--s3);
  margin-block-end: var(--s4);
  border: 1px solid var(--signal-bad);
  border-inline-start-width: 3px;
  border-radius: var(--radius);
  background: var(--signal-bad-wash);
  color: var(--ink);
}

.alert[data-open] { display: flex; }
.alert .icon { color: var(--signal-bad); margin-block-start: 2px; }
.alert .body { flex: 1; min-width: 0; }
.alert .detail { font-weight: 600; }

/* code, and the X-Request-ID behind it, is what makes a support call
   resolvable against the audit log (8). */
.alert .meta {
  margin-block-start: var(--s1);
  font-family: var(--font-mono);
  font-size: 12px;
  color: var(--ink-muted);
  overflow-wrap: anywhere;
}

/* 429 is a passive banner and never an error dialog (8). */
.alert.passive { border-color: var(--signal-warn); background: var(--signal-warn-wash); }
.alert.passive .icon { color: var(--signal-warn); }

.notice { color: var(--ink-muted); margin: var(--s2) 0; }

/* ── jobs ─────────────────────────────────────────────────────────────── */

/* A determinate bar wherever the API supplies progress. Never a spinner where
   a progress value exists (11, 17). */
.progress {
  height: 6px;
  border-radius: 3px;
  background: var(--surface-muted);
  overflow: hidden;
}

.progress > span {
  display: block;
  height: 100%;
  background: var(--accent);
  transition: width var(--motion) var(--ease);
}

.log {
  margin: 0;
  padding: var(--s3);
  max-height: 240px;
  overflow: auto;
  background: var(--surface-muted);
  border-radius: var(--radius);
  font: 400 12px/18px var(--font-mono);
  white-space: pre-wrap;
  overflow-wrap: anywhere;
}

/* ── dialog ───────────────────────────────────────────────────────────── */

/* The scrim is the positioning container, so the card centres by grid rather
   than by a percentage offset plus a transform. The offset form reads
   inset-inline-start, which flips under RTL while the transform does not, and
   the card lands off-screen (13). */
.scrim {
  position: fixed;
  inset: 0;
  z-index: var(--z-scrim);
  display: grid;
  place-items: center;
  padding: var(--s4);
  background: rgba(10, 10, 10, 0.5);
}

/* The consequence card of section 9. A dialog with a required deliberate
   act, never a one-button "Are you sure?". */
.dialog {
  z-index: var(--z-dialog);
  width: min(560px, 100%);
  max-height: calc(100dvh - var(--s8));
  overflow: auto;
  padding: var(--s6);
  background: var(--surface);
  border-radius: var(--radius-lg);
  box-shadow: var(--lift), var(--ring-hairline);
  animation: rise var(--motion) var(--ease);
}

/* Dialogs fade with a 4px rise (12). */
@keyframes rise {
  from { opacity: 0; transform: translateY(4px); }
  to { opacity: 1; transform: none; }
}

.dialog h2 { margin-block-end: var(--s3); }
.dialog .consequence { color: var(--ink); margin: 0 0 var(--s4); }
.dialog .scope { margin: 0 0 var(--s4); padding: 0; list-style: none; display: grid; gap: var(--s1); }
.dialog .scope li { display: flex; gap: var(--s3); }
.dialog .scope .k { color: var(--ink-muted); min-width: 110px; }
.dialog .blockers { margin: 0 0 var(--s4); padding-inline-start: var(--s4); color: var(--signal-bad-ink); }
.dialog .act { display: grid; gap: var(--s1); margin-block-end: var(--s6); }
.dialog .act input, .dialog .act textarea { width: 100%; }
.dialog .buttons { display: flex; justify-content: flex-end; gap: var(--s2); }

/* ── definition grid ──────────────────────────────────────────────────── */

.defs {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(200px, 1fr));
  gap: var(--s4);
  margin: 0;
}

.defs > div { display: grid; gap: var(--s1); min-width: 0; }
.defs dt { font-size: 12px; color: var(--ink-muted); }
.defs dd { margin: 0; }

/* ── sign in ──────────────────────────────────────────────────────────── */

.signin-page { display: grid; place-items: center; min-height: 100dvh; padding: var(--s4); }

.signin {
  width: min(360px, 100%);
  display: flex;
  flex-direction: column;
  gap: var(--s2);
  padding: var(--s8);
  background: var(--surface);
  border-radius: var(--radius-lg);
  box-shadow: var(--ring-hairline);
}

.signin .brand-lockup {
  display: flex;
  align-items: center;
  gap: var(--s3);
  margin-block-end: var(--s6);
}

.signin .brand-lockup .mark {
  display: grid;
  place-items: center;
  width: 32px;
  height: 32px;
  border-radius: var(--radius-sm);
  background: var(--primary);
  color: var(--primary-ink);
  font: 600 13px/1 var(--font-mono);
}

.signin h1 { font-size: 20px; line-height: 28px; }
.signin label { display: grid; gap: var(--s1); margin-block-start: var(--s3); font-size: 12px; color: var(--ink-muted); }
.signin button { margin-block-start: var(--s6); }
.signin .alert { display: flex; margin-block-end: 0; }

/* ── responsive ───────────────────────────────────────────────────────── */

/* Below 1024 the sidebar becomes an off-canvas drawer. It moves with a
   logical margin rather than a physical transform, so the direction flips
   with the document (13). */
@media (max-width: 1023px) {
  .app { grid-template-columns: minmax(0, 1fr); }
  :root[data-sidebar="rail"] .app { grid-template-columns: minmax(0, 1fr); }

  .sidebar {
    position: fixed;
    inset-block: 0;
    inset-inline-start: 0;
    width: var(--sidebar-w);
    margin-inline-start: calc(-1 * var(--sidebar-w));
    transition: margin-inline-start var(--motion) var(--ease);
  }

  :root[data-drawer="open"] .sidebar { margin-inline-start: 0; }
  :root[data-sidebar="rail"] .sidebar { padding-inline: var(--s2); }
  :root[data-sidebar="rail"] .nav a { justify-content: start; padding-inline: var(--s2); }
  :root[data-sidebar="rail"] .nav a span,
  :root[data-sidebar="rail"] .nav-label,
  :root[data-sidebar="rail"] .sidebar-brand span,
  :root[data-sidebar="rail"] .user-chip .who,
  :root[data-sidebar="rail"] .signout {
    position: static;
    width: auto;
    height: auto;
    clip-path: none;
  }
  :root[data-sidebar="rail"] .sidebar-brand,
  :root[data-sidebar="rail"] .user-chip { justify-content: start; padding-inline: var(--s2); }

  .page { padding-inline: var(--s4); }
  .chrome, .context { padding-inline: var(--s4); }
}

@media (min-width: 1024px) {
  .drawer-toggle { display: none; }
}

/* ── motion ───────────────────────────────────────────────────────────── */

/* Not optional (12). */
@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;
  }
}

@media print {
  .sidebar, .chrome, .context, .toolbar, .pager { display: none; }
  .app { grid-template-columns: minmax(0, 1fr); }
}

/* ── RTL / Arabic support ─────────────────────────────────────────────── */

/* Arabic font loaded when dir="rtl" is active */
@font-face {
  font-family: "Noto Naskh Arabic";
  src: local("Noto Naskh Arabic");
  font-weight: 400 700;
  font-display: swap;
}

[dir="rtl"] {
  --font-ui: "Noto Naskh Arabic", "Segoe UI", Tahoma, Arial, sans-serif;
  text-align: start;
}

/* Sidebar slides from the end (right in LTR, left in RTL) */
[dir="rtl"] .sidebar { border-inline-end: none; border-inline-start: 1px solid var(--rule); }
[dir="rtl"] .app { direction: rtl; }

/* ApexCharts text mirrors fine with logical props; just ensure tooltip mirrors */
[dir="rtl"] .apexcharts-tooltip { direction: rtl; text-align: start; }

/* ── Language toggle button ─────────────────────────────────────────────── */

.btn-lang {
  min-width: 40px;
  font: 600 12px/1 var(--font-mono);
  padding-inline: var(--s2);
  height: 28px;
  border: 1px solid var(--rule-strong);
  border-radius: var(--radius-sm);
  cursor: pointer;
  user-select: none;
}
.btn-lang:hover { background: var(--surface-muted); }

/* ── Dashboard filter bar ──────────────────────────────────────────────── */

/* ── Language segmented switch ─────────────────────────────────────────── */

.lang-switch-wrap {
  display: inline-flex;
  align-items: center;
  background: var(--surface-sunken);
  border: 1px solid var(--rule);
  border-radius: var(--radius-sm);
  padding: 2px;
  gap: 2px;
}

.lang-btn {
  border: none;
  background: transparent;
  color: var(--ink-muted);
  font: 600 11px/1 var(--font-ui);
  padding-block: 4px;
  padding-inline: 8px;
  border-radius: calc(var(--radius-sm) - 2px);
  cursor: pointer;
  transition: background var(--motion) var(--ease), color var(--motion) var(--ease);
}

.lang-btn:hover {
  color: var(--ink);
}

.lang-btn.active {
  background: var(--surface);
  color: var(--ink);
  box-shadow: 0 1px 2px rgba(0, 0, 0, 0.08);
  font-weight: 700;
}

/* ── Dashboard Filter Bar ──────────────────────────────────────────────── */

.dash-filter-bar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  flex-wrap: wrap;
  gap: var(--s3);
  margin-block-end: var(--s5);
  padding: var(--s3) var(--s4);
  background: var(--surface);
  border-radius: var(--radius);
  border: 1px solid var(--rule);
}

.filter-inputs-wrap {
  display: flex;
  align-items: center;
  flex-wrap: wrap;
  gap: var(--s3);
}

.filter-presets-wrap {
  display: flex;
  align-items: center;
  gap: var(--s2);
}

.filter-date-group {
  display: inline-flex;
  align-items: center;
  gap: var(--s2);
  background: var(--surface-sunken);
  border: 1px solid var(--rule);
  border-radius: var(--radius-sm);
  padding-block: 4px;
  padding-inline: 10px;
}

.filter-date-group .icon {
  width: 16px;
  height: 16px;
  color: var(--ink-muted);
  flex-shrink: 0;
}

.filter-date-input {
  border: none;
  background: transparent;
  color: var(--ink);
  font: 500 13px/1.4 var(--font-mono);
  outline: none;
  width: 110px;
}

.filter-preset-btn {
  border: 1px solid var(--rule);
  background: var(--surface-sunken);
  color: var(--ink-muted);
  font: 500 12px/1 var(--font-ui);
  padding-block: 6px;
  padding-inline: 12px;
  border-radius: var(--radius-sm);
  cursor: pointer;
  transition: all var(--motion) var(--ease);
}

.filter-preset-btn:hover {
  background: var(--surface-muted);
  color: var(--ink);
}

.filter-preset-btn.active {
  background: var(--primary);
  border-color: var(--primary);
  color: var(--primary-ink);
  font-weight: 600;
}

/* ── KPI cards (col 3 / 4 across) ───────────────────────────────────────── */

.kpi-grid {
  display: grid;
  grid-template-columns: repeat(4, 1fr);
  gap: var(--s4);
  margin-block-end: var(--s6);
}

@media (max-width: 1100px) {
  .kpi-grid { grid-template-columns: repeat(2, 1fr); }
}

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

.kpi-card {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: var(--s4) var(--s5);
  background: var(--surface);
  border: 1px solid var(--rule);
  border-radius: var(--radius);
  min-height: 86px;
  transition: border-color var(--motion) var(--ease), box-shadow var(--motion) var(--ease);
}

.kpi-card:hover {
  border-color: var(--rule-strong);
}

.kpi-info {
  display: flex;
  flex-direction: column;
  gap: 2px;
  min-width: 0;
}

.kpi-label {
  font: 600 11px/14px var(--font-ui);
  color: var(--ink-muted);
  text-transform: uppercase;
  letter-spacing: 0.4px;
}

.kpi-val {
  font: 700 26px/1.1 var(--font-mono);
  color: var(--ink);
}

.kpi-sub {
  font: 500 11px/14px var(--font-ui);
  color: var(--ink-muted);
  display: flex;
  align-items: center;
  gap: 6px;
  margin-block-start: 2px;
}

.kpi-icon-badge {
  width: 44px;
  height: 44px;
  border-radius: var(--radius);
  display: flex;
  align-items: center;
  justify-content: center;
  flex-shrink: 0;
}

.kpi-icon {
  width: 22px !important;
  height: 22px !important;
  max-width: 22px !important;
  max-height: 22px !important;
  display: block !important;
  flex-shrink: 0 !important;
}

/* ── Charts ────────────────────────────────────────────────────────────── */

.chart-grid {
  display: grid;
  grid-template-columns: repeat(2, 1fr);
  gap: var(--s4);
  margin-block-end: var(--s6);
}

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

.chart-card {
  background: var(--surface);
  border: 1px solid var(--rule);
  border-radius: var(--radius);
  padding: var(--s4) var(--s5);
}

.chart-head {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding-block-end: var(--s3);
  border-block-end: 1px solid var(--rule);
  margin-block-end: var(--s4);
}

.chart-title {
  font: 600 14px/20px var(--font-ui);
  color: var(--ink);
  margin: 0;
}

.chart-area {
  min-height: 300px;
}

.chart-empty {
  color: var(--ink-muted);
  font-size: 13px;
  text-align: center;
  padding-block: var(--s8);
}

/* ── Kanban board ──────────────────────────────────────────────────────── */

.kanban-board {
  display: grid;
  grid-template-columns: repeat(4, minmax(240px, 1fr));
  gap: var(--s4);
  align-items: start;
  overflow-x: auto;
  padding-block-end: var(--s8);
}

@media (max-width: 900px) {
  .kanban-board { grid-template-columns: repeat(2, 1fr); }
}
@media (max-width: 560px) {
  .kanban-board { grid-template-columns: 1fr; }
}

.kanban-col {
  background: var(--surface-sunken);
  border-radius: var(--radius);
  border: 1px solid var(--rule);
  min-height: 400px;
  display: flex;
  flex-direction: column;
  gap: 0;
}

.kanban-col-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: var(--s3) var(--s4);
  border-block-end: 1px solid var(--rule);
}

.kanban-col-title { font: 600 13px/20px var(--font-ui); }

.kanban-cards {
  display: flex;
  flex-direction: column;
  gap: var(--s2);
  padding: var(--s3);
  flex: 1;
  min-height: 80px;
  transition: background var(--motion-fast) var(--ease);
}

.kanban-cards.drag-over { background: var(--accent-subtle); }

.kanban-empty { color: var(--ink-faint); font-size: 12px; text-align: center; padding-block: var(--s4); }

.kanban-card {
  background: var(--surface);
  border: 1px solid var(--rule);
  border-radius: var(--radius-sm);
  padding: var(--s3) var(--s3);
  cursor: pointer;
  transition: box-shadow var(--motion-fast) var(--ease), border-color var(--motion-fast) var(--ease);
  display: flex;
  flex-direction: column;
  gap: var(--s2);
}

.kanban-card:hover { box-shadow: var(--lift); border-color: var(--accent); }
.kanban-card.dragging { opacity: 0.4; }
.draggable-card { cursor: grab; }

.kanban-card-top { display: flex; align-items: center; gap: var(--s2); flex-wrap: wrap; }
.kanban-card-title { font: 500 13px/18px var(--font-ui); }
.kanban-card-meta { display: flex; flex-direction: column; gap: 2px; }
.kanban-card-foot { display: flex; align-items: center; justify-content: space-between; }

/* ── Modal / Dialog ────────────────────────────────────────────────────── */

.modal {
  background: var(--surface);
  border: 1px solid var(--rule);
  border-radius: var(--radius-lg);
  box-shadow: var(--lift);
  padding: 0;
  max-width: 560px;
  width: 100%;
  color: var(--ink);
}
.modal::backdrop { background: rgba(10,10,10,0.5); }

.modal-side { position: fixed; inset-block: 0; inset-inline-end: 0; border-radius: 0; max-width: 420px; margin: 0; height: 100%; }

.modal-inner { display: flex; flex-direction: column; max-height: 90vh; overflow: hidden; }
.modal-side .modal-inner { max-height: 100vh; }

.modal-head {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: var(--s4) var(--s6);
  border-block-end: 1px solid var(--rule);
  flex-shrink: 0;
}

.modal-foot {
  display: flex;
  justify-content: flex-end;
  gap: var(--s3);
  padding: var(--s4) var(--s6);
  border-block-start: 1px solid var(--rule);
  flex-shrink: 0;
}

.form-stack { display: flex; flex-direction: column; gap: var(--s4); padding: var(--s6); overflow-y: auto; }
.form-row { display: grid; grid-template-columns: 1fr 1fr; gap: var(--s4); }
.field { display: flex; flex-direction: column; gap: var(--s1); }
.field label { font: 500 12px/16px var(--font-ui); color: var(--ink-muted); }

/* ── Sidebar collapse button (at foot of sidebar) ──────────────────────── */

.sidebar-collapse-btn {
  align-self: flex-start;
  margin-inline-start: var(--s1);
  margin-block-end: var(--s2);
  color: var(--ink-muted);
}
.sidebar-collapse-btn:hover { color: var(--ink); }

/* ── Salesman dashboard ─────────────────────────────────────────────────── */

.todo-item {
  display: flex;
  align-items: center;
  gap: var(--s3);
  padding-block: var(--s2);
  border-block-end: 1px solid var(--rule);
}
.todo-item:last-child { border-block-end: none; }
.todo-title { flex: 1; font: 400 13px/18px var(--font-ui); }

/* ── Detail list (task drawer) ─────────────────────────────────────────── */

.detail-list { display: grid; grid-template-columns: 120px 1fr; gap: var(--s2) var(--s4); margin: 0; }
.detail-list dt { font: 600 12px/16px var(--font-ui); color: var(--ink-muted); align-self: start; padding-block-start: 2px; }
.detail-list dd { margin: 0; font: 400 13px/18px var(--font-ui); word-break: break-word; }

/* ── Priority badge variants ────────────────────────────────────────────── */

.badge-priority-urgent { background: var(--signal-bad); color: #fff; }
.badge-priority-high   { background: var(--signal-warn); color: var(--signal-warn-ink); }
.badge-priority-medium { background: var(--accent); color: #fff; }
.badge-priority-low    { background: var(--signal-none); color: #fff; }

.badge-outline {
  border: 1px solid var(--rule-strong);
  background: transparent;
  color: var(--ink-muted);
}

/* ── Chip ───────────────────────────────────────────────────────────────── */

.chip {
  display: inline-grid;
  place-items: center;
  min-width: 24px;
  height: 24px;
  border-radius: 50%;
  background: var(--primary);
  color: var(--primary-ink);
  font: 600 10px/1 var(--font-mono);
  padding-inline: 4px;
}
.chip-sm { min-width: 20px; height: 20px; font-size: 9px; }

