/* ============================================================
   Bespoke Logistics Finance Portal — application stylesheet
   ============================================================
   Mobile-first: every layout rule starts at the phone width and
   widens at min-width breakpoints. Nothing relies on hover
   alone, and every interactive target is at least 44px tall on
   touch.

   Colour: one set of custom properties, redeclared for dark mode
   under BOTH prefers-color-scheme and [data-theme], so the OS
   setting and the in-app toggle each win where they should.
   The --series-* values are the validated chart palette; see
   assets/js/charts.js.
   ============================================================ */

/* ── Tokens ─────────────────────────────────────────────────── */
:root {
  color-scheme: light;

  /* surfaces & ink */
  --bg:            #f4f4f2;
  --surface:       #ffffff;
  --surface-2:     #fafaf9;
  --surface-sunk:  #f0efec;
  --border:        #e3e2dc;
  --border-strong: #cfcec7;
  --text:          #14140f;
  --text-2:        #56554f;
  --text-3:        #86847d;

  /* brand */
  --brand:         #1c5cab;
  --brand-hover:   #184f95;
  --brand-soft:    #e8f0fb;
  --brand-ink:     #ffffff;
  --focus:         #2a78d6;

  /* status (fixed — never themed, always paired with icon + label) */
  --good:          #0ca30c;
  --good-soft:     #e7f7e7;
  --warning:       #fab219;
  --warning-soft:  #fdf3dd;
  --serious:       #ec835a;
  --serious-soft:  #fdeee7;
  --critical:      #d03b3b;
  --critical-soft: #fbe9e9;
  --good-text:     #006300;
  --critical-text: #a82a2a;

  /* money direction — money in reads cool, money out reads warm */
  --in:            #2a78d6;
  --in-soft:       #e8f0fb;
  --out:           #eb6834;
  --out-soft:      #fdefe8;

  /* chart series (validated: light surface #ffffff) */
  --series-1: #2a78d6;
  --series-2: #eb6834;
  --series-3: #1baf7a;
  --series-4: #eda100;
  --series-5: #e87ba4;
  --series-6: #008300;
  --series-7: #4a3aa7;
  --series-8: #e34948;

  --grid:      #e1e0d9;
  --axis:      #c3c2b7;
  --axis-text: #86847d;

  /* geometry */
  --radius:    10px;
  --radius-sm: 7px;
  --radius-lg: 14px;
  --shadow:    0 1px 2px rgba(20,20,15,.05), 0 1px 3px rgba(20,20,15,.04);
  --shadow-md: 0 4px 12px rgba(20,20,15,.08), 0 2px 4px rgba(20,20,15,.04);
  --shadow-lg: 0 12px 32px rgba(20,20,15,.14);

  --sidebar-w: 248px;
  --header-h:  56px;

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

/* Dark mode: its own steps against the dark surface, not an inversion.
   Declared under both scopes so the toggle beats the OS either way. */
@media (prefers-color-scheme: dark) {
  :root:where(:not([data-theme="light"])) {
    color-scheme: dark;
    --bg:            #0d0d0d;
    --surface:       #16181d;
    --surface-2:     #1c1f25;
    --surface-sunk:  #101216;
    --border:        #2c2e34;
    --border-strong: #3d4046;
    --text:          #f4f4f2;
    --text-2:        #b6b5ad;
    --text-3:        #8a8983;

    --brand:         #3987e5;
    --brand-hover:   #5598e7;
    --brand-soft:    #14243a;
    --brand-ink:     #08131f;
    --focus:         #5598e7;

    --good-soft:     #0f2410;
    --warning-soft:  #2a2109;
    --serious-soft:  #2b1a12;
    --critical-soft: #2c1213;
    --good-text:     #0ca30c;
    --critical-text: #e88080;

    --in:            #3987e5;
    --in-soft:       #14243a;
    --out:           #d95926;
    --out-soft:      #2b1a12;

    --series-1: #3987e5;
    --series-2: #d95926;
    --series-3: #199e70;
    --series-4: #c98500;
    --series-5: #d55181;
    --series-6: #008300;
    --series-7: #9085e9;
    --series-8: #e66767;

    --grid:      #2c2c2a;
    --axis:      #383835;
    --axis-text: #8a8983;

    --shadow:    0 1px 2px rgba(0,0,0,.4);
    --shadow-md: 0 4px 12px rgba(0,0,0,.45);
    --shadow-lg: 0 12px 32px rgba(0,0,0,.6);
  }
}

:root[data-theme="dark"] {
  color-scheme: dark;
  --bg:            #0d0d0d;
  --surface:       #16181d;
  --surface-2:     #1c1f25;
  --surface-sunk:  #101216;
  --border:        #2c2e34;
  --border-strong: #3d4046;
  --text:          #f4f4f2;
  --text-2:        #b6b5ad;
  --text-3:        #8a8983;

  --brand:         #3987e5;
  --brand-hover:   #5598e7;
  --brand-soft:    #14243a;
  --brand-ink:     #08131f;
  --focus:         #5598e7;

  --good-soft:     #0f2410;
  --warning-soft:  #2a2109;
  --serious-soft:  #2b1a12;
  --critical-soft: #2c1213;
  --good-text:     #0ca30c;
  --critical-text: #e88080;

  --in:            #3987e5;
  --in-soft:       #14243a;
  --out:           #d95926;
  --out-soft:      #2b1a12;

  --series-1: #3987e5;
  --series-2: #d95926;
  --series-3: #199e70;
  --series-4: #c98500;
  --series-5: #d55181;
  --series-6: #008300;
  --series-7: #9085e9;
  --series-8: #e66767;

  --grid:      #2c2c2a;
  --axis:      #383835;
  --axis-text: #8a8983;

  --shadow:    0 1px 2px rgba(0,0,0,.4);
  --shadow-md: 0 4px 12px rgba(0,0,0,.45);
  --shadow-lg: 0 12px 32px rgba(0,0,0,.6);
}

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

html { -webkit-text-size-adjust: 100%; }

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

h1, h2, h3, h4 { margin: 0; font-weight: 650; line-height: 1.25; }
p  { margin: 0 0 .75rem; }
a  { color: var(--brand); text-decoration: none; }
a:hover { text-decoration: underline; }

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

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

/* A visible focus ring everywhere — keyboard users need it and it costs
   mouse users nothing. */
:focus-visible {
  outline: 2px solid var(--focus);
  outline-offset: 2px;
  border-radius: 4px;
}

::selection { background: var(--brand-soft); color: var(--text); }

/* ── Screen-reader-only ─────────────────────────────────────── */
.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 — visible only when focused. */
.skip-link {
  position: absolute; left: -999px; top: 0; z-index: 200;
  padding: .6rem 1rem; background: var(--brand); color: var(--brand-ink);
  border-radius: 0 0 var(--radius-sm) 0; font-weight: 600;
}
.skip-link:focus { left: 0; }

/* ============================================================
   App shell
   ============================================================ */
.app {
  min-height: 100vh;
  min-height: 100dvh;   /* dvh keeps mobile browser chrome out of the maths */
  display: flex;
  flex-direction: column;
}

/* ── Top bar ─────────────────────────────────────────────────── */
.topbar {
  position: sticky; top: 0; z-index: 60;
  height: var(--header-h);
  display: flex; align-items: center; gap: .5rem;
  padding: 0 .75rem;
  background: var(--surface);
  border-bottom: 1px solid var(--border);
}

.topbar-brand {
  display: flex; align-items: center; gap: .55rem;
  font-weight: 700; font-size: .95rem; color: var(--text);
  min-width: 0;
}
.topbar-brand:hover { text-decoration: none; }
.topbar-logo {
  width: 30px; height: 30px; flex: none;
  display: grid; place-items: center;
  background: var(--brand); color: var(--brand-ink);
  border-radius: 8px; font-size: .72rem; font-weight: 800; letter-spacing: -.02em;
}
.topbar-name  { white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.topbar-name small {
  display: block; font-size: .66rem; font-weight: 500;
  color: var(--text-3); letter-spacing: .02em;
}
.topbar-spacer { flex: 1; }

.icon-btn {
  width: 40px; height: 40px; flex: none;
  display: grid; place-items: center;
  background: transparent; border: 1px solid transparent;
  border-radius: var(--radius-sm); color: var(--text-2);
  cursor: pointer;
}
.icon-btn:hover { background: var(--surface-sunk); color: var(--text); }
.icon-btn[aria-expanded="true"] { background: var(--surface-sunk); color: var(--text); }

/* ── Sidebar ─────────────────────────────────────────────────── */
/* Off-canvas on mobile, permanent from 1024px up. */
.sidebar {
  position: fixed; inset: var(--header-h) auto 0 0; z-index: 70;
  width: min(84vw, var(--sidebar-w));
  display: flex; flex-direction: column;
  background: var(--surface);
  border-right: 1px solid var(--border);
  transform: translateX(-100%);
  transition: transform .22s ease;
  overflow-y: auto;
  overscroll-behavior: contain;
}
.sidebar.is-open { transform: translateX(0); box-shadow: var(--shadow-lg); }

.sidebar-scrim {
  position: fixed; inset: 0; z-index: 65;
  background: rgba(10,10,8,.45);
  opacity: 0; pointer-events: none;
  transition: opacity .22s ease;
}
.sidebar-scrim.is-open { opacity: 1; pointer-events: auto; }

.nav-section {
  padding: .85rem .75rem .25rem;
  font-size: .64rem; font-weight: 700;
  text-transform: uppercase; letter-spacing: .09em;
  color: var(--text-3);
}

.nav-link {
  display: flex; align-items: center; gap: .65rem;
  min-height: 44px; padding: .5rem .75rem;
  margin: 1px .5rem;
  border-radius: var(--radius-sm);
  color: var(--text-2); font-size: .875rem; font-weight: 500;
}
.nav-link:hover { background: var(--surface-sunk); color: var(--text); text-decoration: none; }
.nav-link svg { flex: none; width: 17px; height: 17px; }

.nav-link.is-active {
  background: var(--brand-soft); color: var(--brand); font-weight: 650;
}
/* Identity is not carried by colour alone — the active item also gets a
   rail on its leading edge. */
.nav-link.is-active::before {
  content: ""; position: absolute; left: 0;
  width: 3px; height: 22px; border-radius: 0 3px 3px 0;
  background: var(--brand);
}
.nav-link { position: relative; }

.nav-badge {
  margin-left: auto; min-width: 20px; padding: 1px 6px;
  border-radius: 99px; background: var(--critical); color: #fff;
  font-size: .68rem; font-weight: 700; text-align: center;
}
.nav-badge.is-muted { background: var(--surface-sunk); color: var(--text-2); }

.sidebar-foot {
  margin-top: auto; padding: .75rem;
  border-top: 1px solid var(--border);
  font-size: .7rem; color: var(--text-3);
}

/* ── Main ────────────────────────────────────────────────────── */
.main { flex: 1; min-width: 0; padding: .875rem; }

.page-head {
  display: flex; flex-wrap: wrap; align-items: flex-start;
  gap: .75rem; margin-bottom: 1rem;
}
.page-head-text { min-width: 0; flex: 1 1 260px; }
.page-title { font-size: 1.15rem; letter-spacing: -.01em; }
.page-sub   { margin: 2px 0 0; font-size: .8rem; color: var(--text-2); }
.page-actions { display: flex; flex-wrap: wrap; gap: .5rem; }

@media (min-width: 640px) {
  .topbar, .main { padding-left: 1.25rem; padding-right: 1.25rem; }
  .page-title { font-size: 1.35rem; }
}

@media (min-width: 1024px) {
  .app { flex-direction: row; }
  .topbar {
    position: fixed; top: 0; left: 0; right: 0;
    padding-left: 1rem;
  }
  .sidebar {
    position: fixed; transform: none; box-shadow: none;
    inset: var(--header-h) auto 0 0;
  }
  .sidebar-scrim, .nav-toggle { display: none; }
  .main {
    margin-left: var(--sidebar-w);
    margin-top: var(--header-h);
    padding: 1.5rem;
    width: calc(100% - var(--sidebar-w));
  }
}

@media (min-width: 1440px) {
  .main { padding: 1.75rem 2rem; }
}

/* ============================================================
   Components
   ============================================================ */

/* ── Buttons ─────────────────────────────────────────────────── */
.btn {
  display: inline-flex; align-items: center; justify-content: center; gap: .45rem;
  min-height: 42px; padding: 0 1rem;
  border: 1px solid var(--border-strong);
  border-radius: var(--radius-sm);
  background: var(--surface); color: var(--text);
  font-size: .875rem; font-weight: 600;
  cursor: pointer; white-space: nowrap;
  transition: background .12s, border-color .12s, opacity .12s;
}
.btn:hover { background: var(--surface-sunk); text-decoration: none; }
.btn:active { transform: translateY(1px); }
.btn:disabled, .btn[aria-disabled="true"] { opacity: .5; cursor: not-allowed; }
.btn svg { width: 16px; height: 16px; flex: none; }

.btn-primary {
  background: var(--brand); border-color: var(--brand); color: var(--brand-ink);
}
.btn-primary:hover { background: var(--brand-hover); border-color: var(--brand-hover); }

.btn-danger {
  background: var(--critical); border-color: var(--critical); color: #fff;
}
.btn-danger:hover { filter: brightness(.93); background: var(--critical); }

.btn-ghost { background: transparent; border-color: transparent; color: var(--text-2); }
.btn-ghost:hover { background: var(--surface-sunk); color: var(--text); }

.btn-sm { min-height: 34px; padding: 0 .7rem; font-size: .8rem; }
.btn-sm svg { width: 14px; height: 14px; }
.btn-block { width: 100%; }

.btn-group { display: inline-flex; gap: 0; }
.btn-group .btn { border-radius: 0; margin-left: -1px; }
.btn-group .btn:first-child { border-radius: var(--radius-sm) 0 0 var(--radius-sm); margin-left: 0; }
.btn-group .btn:last-child  { border-radius: 0 var(--radius-sm) var(--radius-sm) 0; }
.btn-group .btn.is-active { background: var(--brand-soft); color: var(--brand); border-color: var(--brand); z-index: 1; }

/* ── Cards ───────────────────────────────────────────────────── */
.card {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  box-shadow: var(--shadow);
  /* min-width:0 so a card never inherits a wide child's min-content size
     and push the page into a horizontal scroll. */
  min-width: 0;
}
.card-head {
  display: flex; align-items: center; gap: .6rem;
  /* Wraps on narrow screens — a head with a title plus a select and a
     button is wider than a phone, and must not force the card open. */
  flex-wrap: wrap;
  padding: .8rem 1rem; border-bottom: 1px solid var(--border);
  min-width: 0;
}
.card-body, .card-foot { min-width: 0; }
.card-title { font-size: .9rem; font-weight: 650; }
.card-sub   { font-size: .74rem; color: var(--text-3); margin-top: 1px; }
.card-head-actions { margin-left: auto; display: flex; gap: .4rem; align-items: center; }
.card-body { padding: 1rem; }
.card-body.is-flush { padding: 0; }
.card-foot {
  padding: .7rem 1rem; border-top: 1px solid var(--border);
  background: var(--surface-2); font-size: .78rem; color: var(--text-2);
  border-radius: 0 0 var(--radius) var(--radius);
}

/* Every track is minmax(0, 1fr), never a bare 1fr: a bare fr track keeps an
   automatic min-content floor, so one wide child (the A4 invoice paper, a
   640px item table) stretches the track and the whole page scrolls
   sideways. minmax(0, …) lets the track shrink and the child's own
   overflow-x container do its job. */
.grid { display: grid; gap: .875rem; }
.grid > * { min-width: 0; }
.grid-2 { grid-template-columns: minmax(0, 1fr); }
.grid-3 { grid-template-columns: minmax(0, 1fr); }
.grid-4 { grid-template-columns: repeat(2, minmax(0, 1fr)); }

@media (min-width: 700px) {
  .grid-2 { grid-template-columns: repeat(2, minmax(0, 1fr)); }
  .grid-3 { grid-template-columns: repeat(2, minmax(0, 1fr)); }
}
@media (min-width: 1100px) {
  .grid-3 { grid-template-columns: repeat(3, minmax(0, 1fr)); }
  .grid-4 { grid-template-columns: repeat(4, minmax(0, 1fr)); }
}

/* ── Stat tiles ──────────────────────────────────────────────── */
.stat {
  padding: .9rem 1rem;
  background: var(--surface); border: 1px solid var(--border);
  border-radius: var(--radius); box-shadow: var(--shadow);
  display: flex; flex-direction: column; gap: .3rem;
  min-width: 0;
}
.stat-label {
  display: flex; align-items: center; gap: .4rem;
  font-size: .68rem; font-weight: 700;
  text-transform: uppercase; letter-spacing: .07em; color: var(--text-3);
}
.stat-dot { width: 8px; height: 8px; border-radius: 2px; flex: none; }
.stat-dot.in  { background: var(--in); }
.stat-dot.out { background: var(--out); }

/* Proportional figures for the hero number; tabular only where columns
   must align. */
.stat-value {
  font-size: 1.45rem; font-weight: 700; letter-spacing: -.02em;
  line-height: 1.15; word-break: break-word;
}
.stat-value.is-in  { color: var(--in); }
.stat-value.is-out { color: var(--out); }
.stat-meta { font-size: .74rem; color: var(--text-2); }
.stat-meta strong { color: var(--text); font-weight: 650; }

.stat-delta { display: inline-flex; align-items: center; gap: .2rem; font-weight: 650; }
.stat-delta.up   { color: var(--good-text); }
.stat-delta.down { color: var(--critical-text); }

@media (min-width: 640px) { .stat-value { font-size: 1.6rem; } }

/* ── Pills & badges ──────────────────────────────────────────── */
.pill {
  display: inline-flex; align-items: center; gap: .3rem;
  padding: 2px 8px; border-radius: 99px;
  font-size: .68rem; font-weight: 700;
  letter-spacing: .03em; white-space: nowrap;
  background: var(--surface-sunk); color: var(--text-2);
  border: 1px solid transparent;
}
.pill svg { width: 11px; height: 11px; }

.pill-good     { background: var(--good-soft);     color: var(--good-text); }
.pill-warning  { background: var(--warning-soft);  color: #7a5200; }
.pill-serious  { background: var(--serious-soft);  color: #9c4a22; }
.pill-critical { background: var(--critical-soft); color: var(--critical-text); }
.pill-in       { background: var(--in-soft);       color: var(--in); }
.pill-out      { background: var(--out-soft);      color: var(--out); }
.pill-brand    { background: var(--brand-soft);    color: var(--brand); }
.pill-outline  { background: transparent; border-color: var(--border-strong); color: var(--text-2); }

:root[data-theme="dark"] .pill-warning { color: #f0c260; }
:root[data-theme="dark"] .pill-serious { color: #f0a883; }
@media (prefers-color-scheme: dark) {
  :root:where(:not([data-theme="light"])) .pill-warning { color: #f0c260; }
  :root:where(:not([data-theme="light"])) .pill-serious { color: #f0a883; }
}

.code {
  font-family: var(--mono); font-size: .78rem;
  padding: 1px 5px; border-radius: 4px;
  background: var(--surface-sunk); color: var(--text-2);
  word-break: break-all;
}

/* ── Forms ───────────────────────────────────────────────────── */
.field { margin-bottom: .875rem; min-width: 0; }
.field-row { display: grid; gap: .875rem; grid-template-columns: minmax(0, 1fr); }
.field-row > * { min-width: 0; }
@media (min-width: 640px) {
  .field-row.cols-2 { grid-template-columns: repeat(2, minmax(0, 1fr)); }
  .field-row.cols-3 { grid-template-columns: repeat(3, minmax(0, 1fr)); }
}

.label {
  display: block; margin-bottom: .3rem;
  font-size: .72rem; font-weight: 700;
  text-transform: uppercase; letter-spacing: .06em; color: var(--text-2);
}
.label .req { color: var(--critical); margin-left: 2px; }
.label-hint { display: block; margin-top: 2px; font-size: .7rem; font-weight: 500;
  text-transform: none; letter-spacing: 0; color: var(--text-3); }

.input, .select, .textarea {
  width: 100%; min-height: 44px;
  padding: .55rem .7rem;
  background: var(--surface); color: var(--text);
  border: 1px solid var(--border-strong);
  border-radius: var(--radius-sm);
  font-size: .9rem;          /* ≥16px on iOS avoids the zoom-on-focus jump */
  transition: border-color .12s, box-shadow .12s;
}
@media (max-width: 767px) {
  .input, .select, .textarea { font-size: 16px; }
}
.input:focus, .select:focus, .textarea:focus {
  outline: none;
  border-color: var(--focus);
  box-shadow: 0 0 0 3px color-mix(in srgb, var(--focus) 18%, transparent);
}
.input:disabled, .select:disabled, .textarea:disabled {
  background: var(--surface-sunk); color: var(--text-3); cursor: not-allowed;
}
.input[readonly] { background: var(--surface-sunk); color: var(--text-2); }
.textarea { min-height: 84px; resize: vertical; line-height: 1.5; }
.input.is-invalid, .select.is-invalid { border-color: var(--critical); }
.input.is-mono { font-family: var(--mono); }

.field-error {
  display: none; margin-top: .3rem;
  font-size: .76rem; color: var(--critical-text); font-weight: 500;
}
.field-error.is-shown { display: block; }

/* Password field with the mask toggle inside it. */
.input-affix { position: relative; }
.input-affix .input { padding-right: 46px; }
.affix-btn {
  position: absolute; right: 4px; top: 50%; transform: translateY(-50%);
  width: 38px; height: 38px;
  display: grid; place-items: center;
  background: transparent; border: none; border-radius: var(--radius-sm);
  color: var(--text-3); cursor: pointer;
}
.affix-btn:hover { background: var(--surface-sunk); color: var(--text); }
.affix-btn svg { width: 18px; height: 18px; }

/* Checkbox / radio rows with a 44px hit area. */
.check {
  display: flex; align-items: center; gap: .55rem;
  min-height: 40px; cursor: pointer; user-select: none;
  font-size: .875rem; color: var(--text);
}
.check input[type="checkbox"], .check input[type="radio"] {
  width: 18px; height: 18px; flex: none; accent-color: var(--brand); cursor: pointer;
}
.check-hint { font-size: .74rem; color: var(--text-3); }

.switch-row {
  display: flex; align-items: center; gap: .6rem; flex-wrap: wrap;
  padding: .6rem .75rem; border: 1px solid var(--border);
  border-radius: var(--radius-sm); background: var(--surface-2);
}
.switch-row .pct { width: 92px; min-height: 38px; }

/* Permission checkboxes: one per row on a phone (full 40px touch target),
   two columns from tablet up. Each line is short, and stacking all 35 made
   the editor thousands of pixels tall. */
.perm-grid { display: grid; grid-template-columns: minmax(0, 1fr); gap: 0 1rem; }
@media (min-width: 640px) {
  .perm-grid { grid-template-columns: repeat(2, minmax(0, 1fr)); }
}
.perm-grid .check { align-items: flex-start; padding: .15rem 0; }
.perm-grid .check > span { line-height: 1.35; padding-top: 1px; }
.perm-grid .check .code { display: inline-block; margin-top: 1px; }

/* ── Toolbar / filters ───────────────────────────────────────── */
.toolbar {
  display: flex; flex-wrap: wrap; gap: .5rem; align-items: center;
  padding: .7rem; margin-bottom: .875rem;
  background: var(--surface); border: 1px solid var(--border);
  border-radius: var(--radius);
}
.toolbar .input, .toolbar .select { min-height: 40px; }
.toolbar-search { flex: 1 1 180px; min-width: 0; }
.toolbar-field { flex: 0 1 auto; min-width: 0; }
.toolbar-field .select { min-width: 130px; }
.toolbar-spacer { flex: 1 1 auto; }

/* ── Tables ──────────────────────────────────────────────────── */
/* Wide tables scroll inside their own container; the page never scrolls
   sideways. */
.table-wrap {
  overflow-x: auto;
  -webkit-overflow-scrolling: touch;
  max-width: 100%;
  border-radius: 0 0 var(--radius) var(--radius);
}
.table { width: 100%; border-collapse: collapse; font-size: .85rem; }
.table thead th {
  position: sticky; top: 0; z-index: 2;
  padding: .6rem .8rem; text-align: left; white-space: nowrap;
  font-size: .66rem; font-weight: 700;
  text-transform: uppercase; letter-spacing: .06em; color: var(--text-3);
  background: var(--surface-2);
  border-bottom: 1px solid var(--border);
}
.table th.num, .table td.num { text-align: right; }
.table tbody td {
  padding: .65rem .8rem;
  border-bottom: 1px solid var(--border);
  vertical-align: middle;
}
.table tbody tr:last-child td { border-bottom: none; }
.table tbody tr.is-clickable { cursor: pointer; }
.table tbody tr.is-clickable:hover { background: var(--surface-2); }
.table tbody tr.is-clickable:focus-visible { background: var(--brand-soft); outline-offset: -2px; }
.table td.num, .table th.num { font-variant-numeric: tabular-nums; }
.table td.money { font-variant-numeric: tabular-nums; font-weight: 600; white-space: nowrap; }
.table .cell-strong { font-weight: 650; color: var(--text); }
.table .cell-muted  { color: var(--text-3); font-size: .8rem; }
.table .cell-actions { white-space: nowrap; text-align: right; }
.table tfoot td {
  padding: .65rem .8rem; background: var(--surface-2);
  font-weight: 700; border-top: 1px solid var(--border-strong);
  font-variant-numeric: tabular-nums;
}
.table-compact tbody td { padding: .45rem .7rem; }

/* Row-level state: a left rail, not a background wash, so the text
   contrast never degrades. */
.row-flag { box-shadow: inset 3px 0 0 var(--critical); }
.row-flag-warn { box-shadow: inset 3px 0 0 var(--warning); }
.row-void { opacity: .55; }
.row-void .cell-strong { text-decoration: line-through; }

.empty {
  padding: 2.5rem 1rem; text-align: center; color: var(--text-3);
}
.empty-icon { color: var(--border-strong); margin-bottom: .5rem; }
.empty-icon svg { width: 34px; height: 34px; }
.empty-title { font-weight: 650; color: var(--text-2); margin-bottom: .2rem; }
.empty-text { font-size: .82rem; max-width: 34ch; margin: 0 auto; }

/* Mobile card view: below 720px the invoice/party tables re-flow into
   stacked cards, driven by data-label attributes in the markup. */
@media (max-width: 719px) {
  .table-cards thead { display: none; }
  .table-cards tbody tr {
    display: block; padding: .7rem .8rem;
    border-bottom: 1px solid var(--border);
  }
  .table-cards tbody td {
    display: flex; justify-content: space-between; gap: 1rem;
    padding: .2rem 0; border: none; text-align: right;
  }
  .table-cards tbody td::before {
    content: attr(data-label);
    font-size: .68rem; font-weight: 700; text-transform: uppercase;
    letter-spacing: .05em; color: var(--text-3);
    text-align: left; flex: none;
  }
  .table-cards tbody td.cell-full {
    display: block; text-align: left; padding-bottom: .35rem;
  }
  .table-cards tbody td.cell-full::before { display: none; }
  .table-cards tbody td.cell-actions { justify-content: flex-end; padding-top: .5rem; }
}

/* ── Pagination ──────────────────────────────────────────────── */
.pager {
  display: flex; flex-wrap: wrap; align-items: center; gap: .4rem;
  padding: .7rem 1rem; border-top: 1px solid var(--border);
  font-size: .8rem; color: var(--text-2);
}
.pager-spacer { flex: 1; }

/* ── Alerts ──────────────────────────────────────────────────── */
.alert {
  display: flex; gap: .6rem; align-items: flex-start;
  padding: .7rem .85rem; margin-bottom: .875rem;
  border: 1px solid var(--border); border-radius: var(--radius-sm);
  background: var(--surface-2); font-size: .84rem;
}
.alert svg { width: 17px; height: 17px; flex: none; margin-top: 1px; }
.alert-title { font-weight: 650; }
.alert-good     { background: var(--good-soft);     border-color: color-mix(in srgb, var(--good) 35%, transparent);     color: var(--good-text); }
.alert-warning  { background: var(--warning-soft);  border-color: color-mix(in srgb, var(--warning) 40%, transparent);  color: var(--text); }
.alert-critical { background: var(--critical-soft); border-color: color-mix(in srgb, var(--critical) 35%, transparent); color: var(--critical-text); }
.alert-info     { background: var(--brand-soft);    border-color: color-mix(in srgb, var(--brand) 30%, transparent);    color: var(--text); }
.alert-dismiss { margin-left: auto; }

/* ── Toasts ──────────────────────────────────────────────────── */
.toast-stack {
  position: fixed; z-index: 200;
  left: .75rem; right: .75rem; bottom: .75rem;
  display: flex; flex-direction: column-reverse; gap: .5rem;
  pointer-events: none;
}
@media (min-width: 640px) {
  .toast-stack { left: auto; right: 1.25rem; bottom: 1.25rem; width: 360px; }
}
.toast {
  pointer-events: auto;
  display: flex; gap: .6rem; align-items: flex-start;
  padding: .75rem .85rem;
  background: var(--surface); color: var(--text);
  border: 1px solid var(--border); border-left: 3px solid var(--brand);
  border-radius: var(--radius-sm); box-shadow: var(--shadow-lg);
  font-size: .85rem;
  animation: toast-in .2s ease;
}
.toast.is-good     { border-left-color: var(--good); }
.toast.is-critical { border-left-color: var(--critical); }
.toast.is-warning  { border-left-color: var(--warning); }
.toast svg { width: 17px; height: 17px; flex: none; margin-top: 1px; }
.toast-body { flex: 1; min-width: 0; }
.toast-close {
  background: none; border: none; color: var(--text-3);
  cursor: pointer; padding: 0 .2rem; font-size: 1.1rem; line-height: 1;
}
@keyframes toast-in { from { opacity: 0; transform: translateY(8px); } }

/* ── Modal ───────────────────────────────────────────────────── */
.modal {
  position: fixed; inset: 0; z-index: 150;
  display: none; align-items: flex-end; justify-content: center;
  padding: 0;
  background: rgba(10,10,8,.5);
}
.modal.is-open { display: flex; }
.modal-panel {
  width: 100%; max-height: 92dvh;
  display: flex; flex-direction: column;
  background: var(--surface);
  border-radius: var(--radius-lg) var(--radius-lg) 0 0;
  box-shadow: var(--shadow-lg);
  /* Keeps the scrolling body inside the rounded corners. */
  overflow: hidden;
  animation: sheet-up .22s ease;
}
@keyframes sheet-up { from { transform: translateY(14px); opacity: .6; } }

@media (min-width: 640px) {
  .modal { align-items: center; padding: 1.25rem; }
  .modal-panel {
    max-width: 560px; max-height: 88dvh;
    border-radius: var(--radius-lg);
    animation: modal-in .18s ease;
  }
  .modal-panel.is-wide { max-width: 860px; }
  @keyframes modal-in { from { transform: scale(.97); opacity: .6; } }
}

.modal-head {
  display: flex; align-items: center; gap: .6rem;
  padding: .9rem 1rem; border-bottom: 1px solid var(--border);
}
.modal-title { font-size: .95rem; font-weight: 650; }
/* Most modals wrap the body AND the footer in a <form>, which makes the
   form — not the body — the panel's flex child. So the form has to become a
   flex column itself and be allowed to shrink; otherwise the body below
   can never scroll no matter what it declares. */
.modal-panel > form {
  display: flex;
  flex-direction: column;
  flex: 1 1 auto;
  min-height: 0;
}

/* min-height:0 is load-bearing, not defensive. A flex item defaults to
   min-height:auto, which refuses to shrink below its content — so
   overflow-y:auto never engages, the body grows to its full height (2600px+
   for the permission editor), and the footer with the Save button is pushed
   off the bottom of the screen. */
.modal-body {
  padding: 1rem;
  overflow-y: auto;
  -webkit-overflow-scrolling: touch;
  flex: 1 1 auto;
  min-height: 0;
}

/* The head and foot must never shrink or scroll away — the foot holds the
   primary action. */
.modal-head, .modal-foot { flex: none; }

.modal-foot {
  display: flex; gap: .5rem; justify-content: flex-end;
  padding: .8rem 1rem; border-top: 1px solid var(--border);
  background: var(--surface-2);
}
.modal-foot .btn { flex: 1; }
@media (min-width: 640px) { .modal-foot .btn { flex: none; } }

/* Drag handle affordance on the mobile sheet. */
.modal-grip {
  width: 36px; height: 4px; border-radius: 99px;
  background: var(--border-strong); margin: .5rem auto -.25rem;
}
@media (min-width: 640px) { .modal-grip { display: none; } }

/* ── Tabs ────────────────────────────────────────────────────── */
.tabs {
  display: flex; gap: .15rem; overflow-x: auto;
  border-bottom: 1px solid var(--border); margin-bottom: 1rem;
  -webkit-overflow-scrolling: touch;
}
.tab {
  padding: .6rem .85rem; min-height: 42px;
  background: none; border: none; border-bottom: 2px solid transparent;
  color: var(--text-2); font-size: .85rem; font-weight: 600;
  white-space: nowrap; cursor: pointer;
}
.tab:hover { color: var(--text); }
.tab.is-active { color: var(--brand); border-bottom-color: var(--brand); }

/* ── Definition list (profile / detail views) ────────────────── */
.dl { display: grid; gap: .7rem; grid-template-columns: minmax(0, 1fr); }
.dl > * { min-width: 0; }
@media (min-width: 560px) { .dl.cols-2 { grid-template-columns: repeat(2, minmax(0, 1fr)); } }
@media (min-width: 900px) { .dl.cols-3 { grid-template-columns: repeat(3, minmax(0, 1fr)); } }
.dl-item { min-width: 0; }
.dl-label {
  font-size: .66rem; font-weight: 700; text-transform: uppercase;
  letter-spacing: .06em; color: var(--text-3); margin-bottom: 2px;
}
.dl-value { font-size: .875rem; word-break: break-word; }
.dl-value.is-mono { font-family: var(--mono); font-size: .8rem; }

/* ── Timeline (history / activity) ───────────────────────────── */
.timeline { list-style: none; margin: 0; padding: 0; }
.timeline li {
  position: relative; padding: 0 0 .9rem 1.4rem;
  border-left: 1px solid var(--border); margin-left: 4px;
}
.timeline li:last-child { border-left-color: transparent; padding-bottom: 0; }
.timeline li::before {
  content: ""; position: absolute; left: -4.5px; top: 5px;
  width: 8px; height: 8px; border-radius: 99px;
  background: var(--border-strong);
  box-shadow: 0 0 0 2px var(--surface);
}
.timeline li.is-good::before     { background: var(--good); }
.timeline li.is-critical::before { background: var(--critical); }
.timeline li.is-warning::before  { background: var(--warning); }
.timeline-title { font-size: .84rem; font-weight: 600; }
.timeline-meta  { font-size: .72rem; color: var(--text-3); margin-top: 1px; }

/* ── Charts ──────────────────────────────────────────────────── */
.chart-wrap { position: relative; width: 100%; }
.chart-wrap svg { display: block; width: 100%; height: auto; overflow: visible; }

.chart-legend {
  display: flex; flex-wrap: wrap; gap: .5rem 1rem;
  padding: .6rem 1rem 0; font-size: .76rem; color: var(--text-2);
}
.chart-legend-item { display: inline-flex; align-items: center; gap: .35rem; }
.chart-legend-swatch { width: 9px; height: 9px; border-radius: 2px; flex: none; }

/* Tooltip: positioned by JS, never wider than the viewport. */
.chart-tip {
  position: absolute; z-index: 20; pointer-events: none;
  display: none; min-width: 130px; max-width: min(260px, 80vw);
  padding: .5rem .6rem;
  background: var(--surface); color: var(--text);
  border: 1px solid var(--border-strong); border-radius: var(--radius-sm);
  box-shadow: var(--shadow-md);
  font-size: .76rem;
}
.chart-tip.is-shown { display: block; }
.chart-tip-title {
  font-weight: 700; margin-bottom: .3rem;
  padding-bottom: .3rem; border-bottom: 1px solid var(--border);
}
.chart-tip-row { display: flex; align-items: center; gap: .4rem; padding: 1px 0; }
.chart-tip-row .sw { width: 8px; height: 8px; border-radius: 2px; flex: none; }
.chart-tip-row .lbl { color: var(--text-2); }
.chart-tip-row .val { margin-left: auto; font-weight: 650; font-variant-numeric: tabular-nums; }

/* The table view every chart ships alongside it — this is the relief for
   the sub-3:1 light-mode series colours, and the a11y fallback. */
.chart-table-toggle {
  background: none; border: none; padding: 0;
  color: var(--brand); font-size: .74rem; font-weight: 600; cursor: pointer;
}
.chart-data-table { display: none; margin-top: .5rem; }
.chart-data-table.is-shown { display: block; }

.chart-axis-label { font-size: 10px; fill: var(--axis-text); }
.chart-grid-line  { stroke: var(--grid); stroke-width: 1; }
.chart-baseline   { stroke: var(--axis); stroke-width: 1; }

/* Aging bars — a horizontal bar list, not an SVG chart. */
.bars { display: flex; flex-direction: column; gap: .55rem; }
.bar-row { display: grid; grid-template-columns: 62px 1fr auto; gap: .6rem; align-items: center; }
.bar-label { font-size: .74rem; color: var(--text-2); font-weight: 600; }
.bar-track { height: 20px; background: var(--surface-sunk); border-radius: 4px; overflow: hidden; }
.bar-fill  { height: 100%; border-radius: 4px 2px 2px 4px; min-width: 2px; }
.bar-value { font-size: .78rem; font-weight: 650; font-variant-numeric: tabular-nums; white-space: nowrap; }

/* ── Invoice item builder ────────────────────────────────────── */
/* max-width is load-bearing: without it the 640px min-width table grows
   the container instead of scrolling inside it, and the whole page ends up
   scrolling sideways on a phone. */
.items-scroll {
  overflow-x: auto;
  -webkit-overflow-scrolling: touch;
  max-width: 100%;
}
.items-table { width: 100%; min-width: 640px; border-collapse: collapse; font-size: .85rem; }
.items-table th {
  position: relative;
  padding: .45rem .5rem; text-align: left; white-space: nowrap;
  font-size: .64rem; font-weight: 700; text-transform: uppercase;
  letter-spacing: .05em; color: var(--text-3);
  background: var(--surface-2); border-bottom: 1px solid var(--border);
}
.items-table th.num { text-align: right; }
.items-table td { padding: .25rem .3rem; border-bottom: 1px solid var(--border); }

/* The "auto" marker on a computed heading. The th is uppercased, so this
   has to opt out or it reads as part of the label ("ITEM NO. AUTO"). */
.items-table th .auto-flag {
  text-transform: lowercase;
  letter-spacing: 0;
  font-weight: 600;
  font-size: .6rem;
  color: var(--text-3);
  font-style: italic;
}

/* Editable cells carry a resting border: with a transparent one the row
   looks like an empty strip and nobody can tell where to type. */
.items-table input {
  width: 100%; min-height: 38px; padding: .35rem .5rem;
  background: var(--surface); color: var(--text);
  border: 1px solid var(--border); border-radius: 5px; font-size: .85rem;
}
.items-table input:hover { border-color: var(--border-strong); }
.items-table input::placeholder { color: var(--text-3); }
.items-table input:focus {
  outline: none; border-color: var(--focus);
  box-shadow: 0 0 0 2px color-mix(in srgb, var(--focus) 20%, transparent);
}
/* Computed cells are visibly not-for-typing: sunk, dashed, no hover. */
.items-table input[readonly] {
  background: var(--surface-sunk); color: var(--text-2);
  border-style: dashed; font-weight: 650; cursor: default;
}
.items-table input[readonly]:hover { border-color: var(--border); }
.items-table input.num { text-align: right; font-variant-numeric: tabular-nums; }
.items-table tfoot td { background: var(--surface-2); font-weight: 700; padding: .5rem; }

.col-menu-btn {
  position: absolute; top: 2px; right: 2px;
  width: 22px; height: 22px; display: grid; place-items: center;
  background: transparent; border: none; border-radius: 4px;
  color: var(--text-3); cursor: pointer; opacity: 0;
}
.items-table th:hover .col-menu-btn,
.col-menu-btn:focus-visible { opacity: 1; }
.col-menu-btn:hover { background: var(--surface-sunk); color: var(--text); }

.row-drag {
  cursor: grab; color: var(--text-3); background: none; border: none;
  padding: .3rem; border-radius: 4px;
}
.row-drag:hover { background: var(--surface-sunk); color: var(--text); }
.items-table tr.is-dragging { opacity: .4; }
.items-table tr.is-drop-target td { border-top: 2px solid var(--brand); }

.row-del {
  width: 32px; height: 32px; display: grid; place-items: center;
  background: transparent; border: 1px solid transparent;
  border-radius: 5px; color: var(--text-3); cursor: pointer;
}
.row-del:hover { background: var(--critical-soft); border-color: var(--critical); color: var(--critical-text); }

/* Totals panel beside the item table. */
.totals { display: flex; flex-direction: column; gap: .1rem; }
.totals-row {
  display: flex; justify-content: space-between; gap: 1rem;
  padding: .3rem 0; font-size: .85rem;
}
.totals-row .lbl { color: var(--text-2); }
.totals-row .val { font-variant-numeric: tabular-nums; font-weight: 600; }
.totals-row.is-grand {
  margin-top: .35rem; padding-top: .55rem;
  border-top: 2px solid var(--border-strong);
  font-size: 1.02rem; font-weight: 700;
}
.totals-row.is-grand .val { color: var(--brand); }
.totals-row.is-deduction .val { color: var(--critical-text); }

/* ── Contact / requirement repeaters ────────────────────────── */
.repeat-row {
  display: grid; gap: .5rem; grid-template-columns: 1fr;
  padding: .7rem; margin-bottom: .5rem;
  background: var(--surface-2); border: 1px solid var(--border);
  border-radius: var(--radius-sm);
}
@media (min-width: 760px) {
  .repeat-row { grid-template-columns: 140px 1fr 1fr 150px auto; align-items: end; }
}
.repeat-row .repeat-del { align-self: center; }

.doc-row {
  display: flex; flex-wrap: wrap; gap: .6rem; align-items: center;
  padding: .7rem .8rem; border-bottom: 1px solid var(--border);
}
.doc-row:last-child { border-bottom: none; }
.doc-main { flex: 1 1 220px; min-width: 0; }
.doc-name { font-size: .875rem; font-weight: 600; }
.doc-meta { font-size: .74rem; color: var(--text-3); margin-top: 1px; }
.doc-actions { display: flex; gap: .35rem; flex-wrap: wrap; }

/* ── Utilities ───────────────────────────────────────────────── */
.stack   { display: flex; flex-direction: column; gap: .875rem; }
.row     { display: flex; flex-wrap: wrap; gap: .5rem; align-items: center; }
.row-tight { display: flex; flex-wrap: wrap; gap: .3rem; align-items: center; }
.spacer  { flex: 1; }
.mono    { font-family: var(--mono); }
.nowrap  { white-space: nowrap; }
.tabular { font-variant-numeric: tabular-nums; }
.text-2  { color: var(--text-2); }
.text-3  { color: var(--text-3); }
.text-sm { font-size: .8rem; }
.text-xs { font-size: .72rem; }
.text-right { text-align: right; }
.fw-6    { font-weight: 650; }
.mt-0 { margin-top: 0; }   .mt-1 { margin-top: .5rem; }   .mt-2 { margin-top: 1rem; }
.mb-0 { margin-bottom: 0; } .mb-1 { margin-bottom: .5rem; } .mb-2 { margin-bottom: 1rem; }
.hidden { display: none !important; }

.spin { animation: spin 1s linear infinite; }
@keyframes spin { to { transform: rotate(360deg); } }

.skeleton {
  background: linear-gradient(90deg, var(--surface-sunk) 25%, var(--surface-2) 50%, var(--surface-sunk) 75%);
  background-size: 200% 100%;
  animation: shimmer 1.4s infinite; border-radius: 4px; color: transparent;
}
@keyframes shimmer { to { background-position: -200% 0; } }

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

/* Forced-colors: let the OS palette through rather than fighting it. */
@media (forced-colors: active) {
  .card, .stat, .input, .btn { border: 1px solid CanvasText; }
  .nav-link.is-active { forced-color-adjust: none; }
}

/* ── Print ───────────────────────────────────────────────────── */
@media print {
  .topbar, .sidebar, .sidebar-scrim, .toolbar, .page-actions,
  .toast-stack, .no-print, .pager { display: none !important; }
  body { background: #fff; }
  .main { margin: 0; padding: 0; width: 100%; }
  .card { border: none; box-shadow: none; }
}
