/* UXD-001..UXD-017 — one design system, responsive to phone, theme-aware. */
:root {
  --bg: #f6f7f9;
  --surface: #ffffff;
  --surface-2: #fbfbfd;
  --border: #e2e5ea;
  --border-strong: #cfd4dc;
  --text: #16181d;
  --text-muted: #5f6672;
  /* Corrected by the accessibility sweep (QTG-009/UXD-015). The ratios are
     recorded because the next person to adjust a colour needs to know what
     the old one failed, not just that it changed.
       --text-faint  was #8b919c, 3.17:1 on white  → 5.22:1
       --warn        was #9a6700, 4.48:1 on its own soft background → 5.78:1
     --primary-strong exists because --primary is legible on white (4.63:1)
     but not on --primary-soft (4.04:1), which is where pills and the active
     navigation item put it. */
  --text-faint: #666d78;
  --primary: #1f6feb;
  --primary-strong: #175cc4;
  --primary-ink: #ffffff;
  --primary-soft: #e8f0fe;
  --success: #157347;
  --success-soft: #e6f4ec;
  --warn: #845700;
  --warn-soft: #fdf5e2;
  --danger: #b42318;
  --danger-soft: #fdeceb;
  --info-soft: #eef2f7;
  --radius: 10px;
  --radius-sm: 6px;
  --shadow: 0 1px 2px rgba(16,24,40,.06), 0 1px 3px rgba(16,24,40,.08);
  --shadow-lg: 0 8px 24px rgba(16,24,40,.12);
  --mono: ui-monospace, SFMono-Regular, "SF Mono", Menlo, Consolas, monospace;
  --sans: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif;
}
@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) {
    --bg: #0e1116; --surface: #161b22; --surface-2: #1b2028;
    --border: #262c36; --border-strong: #364150;
    --text: #e6e9ef; --text-muted: #9aa4b2; --text-faint: #8b95a3;
    --primary: #4f8cf7; --primary-strong: #7fb0ff; --primary-soft: #17263f;
    --success: #3fb950; --success-soft: #12261a;
    --warn: #d29922; --warn-soft: #2a2113;
    --danger: #f85149; --danger-soft: #2d1618;
    --info-soft: #1b2028;
    --shadow: 0 1px 2px rgba(0,0,0,.4);
    --shadow-lg: 0 8px 24px rgba(0,0,0,.5);
  }
}

* { box-sizing: border-box; }
html, body { height: 100%; }
body {
  margin: 0; font-family: var(--sans); font-size: 14px; line-height: 1.5;
  background: var(--bg); color: var(--text); -webkit-font-smoothing: antialiased;
}
a { color: var(--primary); text-decoration: none; }
a:hover { text-decoration: underline; }
h1, h2, h3, h4 { margin: 0; font-weight: 620; letter-spacing: -0.01em; }
h1 { font-size: 20px; } h2 { font-size: 17px; } h3 { font-size: 15px; }

/* ---------------------------------------------------------------- shell */
#app { min-height: 100%; }
.shell { display: grid; grid-template-columns: 232px 1fr; min-height: 100vh; }
.sidebar {
  background: var(--surface); border-right: 1px solid var(--border);
  display: flex; flex-direction: column; position: sticky; top: 0; height: 100vh; overflow-y: auto;
}
.brand { padding: 16px 16px 12px; border-bottom: 1px solid var(--border); }
.brand-name { font-weight: 680; font-size: 15px; }
.brand-sub { color: var(--text-faint); font-size: 12px; margin-top: 2px; }
.nav { padding: 8px; flex: 1; }
.nav-group { margin-bottom: 4px; }
.nav-label {
  font-size: 11px; text-transform: uppercase; letter-spacing: .06em;
  color: var(--text-faint); padding: 10px 10px 4px; font-weight: 600;
}
.nav a {
  display: flex; align-items: center; gap: 9px; padding: 7px 10px; border-radius: var(--radius-sm);
  color: var(--text-muted); font-size: 13.5px; text-decoration: none;
}
.nav a:hover { background: var(--info-soft); color: var(--text); }
.nav a.active { background: var(--primary-soft); color: var(--primary-strong); font-weight: 600; }
.nav .ico { width: 16px; text-align: center; opacity: .85; font-size: 13px; }
.nav .badge { margin-left: auto; }

.main { display: flex; flex-direction: column; min-width: 0; }
.topbar {
  display: flex; align-items: center; gap: 12px; padding: 10px 20px;
  background: var(--surface); border-bottom: 1px solid var(--border); position: sticky; top: 0; z-index: 20;
}
.topbar h1 { flex: 1; min-width: 0; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.content { padding: 20px; flex: 1; max-width: 1500px; width: 100%; }
.menu-btn { display: none; }

/* ---------------------------------------------------------------- cards */
.card {
  background: var(--surface); border: 1px solid var(--border);
  border-radius: var(--radius); box-shadow: var(--shadow); margin-bottom: 16px; overflow: hidden;
}
.card-head {
  display: flex; align-items: center; gap: 10px; flex-wrap: wrap;
  padding: 12px 16px; border-bottom: 1px solid var(--border); background: var(--surface-2);
}
.card-head h2, .card-head h3 { flex: 1; min-width: 140px; }
.card-body { padding: 16px; }
.card-body.tight { padding: 0; }
.muted { color: var(--text-muted); }
.faint { color: var(--text-faint); font-size: 12.5px; }

.grid { display: grid; gap: 16px; }
.grid.c2 { grid-template-columns: repeat(2, minmax(0,1fr)); }
.grid.c3 { grid-template-columns: repeat(3, minmax(0,1fr)); }
.grid.c4 { grid-template-columns: repeat(4, minmax(0,1fr)); }

/* ------------------------------------------------------------- stat tiles */
.stat { background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius);
  padding: 14px 16px; box-shadow: var(--shadow); }
.stat .label { font-size: 12px; color: var(--text-muted); font-weight: 550; }
.stat .value { font-size: 22px; font-weight: 660; margin-top: 4px; letter-spacing: -0.02em;
  font-variant-numeric: tabular-nums; }
.stat .delta { font-size: 12px; margin-top: 3px; }
.up { color: var(--success); } .down { color: var(--danger); }

/* ----------------------------------------------------------------- table */
.table-wrap { overflow-x: auto; -webkit-overflow-scrolling: touch; }
table { width: 100%; border-collapse: collapse; font-size: 13.5px; }
th, td { padding: 9px 12px; text-align: left; border-bottom: 1px solid var(--border); vertical-align: top; }
th { font-size: 11.5px; text-transform: uppercase; letter-spacing: .04em; color: var(--text-muted);
  font-weight: 620; background: var(--surface-2); position: sticky; top: 0; white-space: nowrap; }
tbody tr:hover { background: var(--surface-2); }
td.num, th.num { text-align: right; font-variant-numeric: tabular-nums; white-space: nowrap; }
td.mono { font-family: var(--mono); font-size: 12.5px; }
tr.clickable { cursor: pointer; }
tfoot td { font-weight: 650; background: var(--surface-2); border-top: 2px solid var(--border-strong); }
.empty { padding: 32px 16px; text-align: center; color: var(--text-faint); }

/* ---------------------------------------------------------------- pills */
.pill { display: inline-block; padding: 2px 8px; border-radius: 999px; font-size: 11.5px;
  font-weight: 600; white-space: nowrap; background: var(--info-soft); color: var(--text-muted); }
.pill.ok { background: var(--success-soft); color: var(--success); }
.pill.warn { background: var(--warn-soft); color: var(--warn); }
.pill.bad { background: var(--danger-soft); color: var(--danger); }
.pill.info { background: var(--primary-soft); color: var(--primary-strong); }
.badge { background: var(--danger); color: #fff; border-radius: 999px; font-size: 11px;
  padding: 1px 7px; font-weight: 650; }

/* --------------------------------------------------------------- buttons */
button, .btn {
  font: inherit; font-size: 13.5px; font-weight: 560; cursor: pointer;
  padding: 7px 13px; border-radius: var(--radius-sm); border: 1px solid var(--border-strong);
  background: var(--surface); color: var(--text); min-height: 36px;
  display: inline-flex; align-items: center; gap: 6px; justify-content: center;
}
button:hover:not(:disabled), .btn:hover { background: var(--surface-2); text-decoration: none; }
button:disabled { opacity: .5; cursor: not-allowed; }
button.primary { background: var(--primary); border-color: var(--primary); color: var(--primary-ink); }
button.primary:hover:not(:disabled) { filter: brightness(1.07); }
button.danger { background: var(--danger); border-color: var(--danger); color: #fff; }
button.ghost { border-color: transparent; background: transparent; color: var(--text-muted); }
button.ghost:hover { background: var(--info-soft); }
button.sm { min-height: 30px; padding: 4px 10px; font-size: 12.5px; }
.btn-row { display: flex; gap: 8px; flex-wrap: wrap; }

/* ---------------------------------------------------------------- forms */
.field { margin-bottom: 13px; }
label { display: block; font-size: 12.5px; font-weight: 580; margin-bottom: 4px; color: var(--text-muted); }
input, select, textarea {
  font: inherit; font-size: 14px; width: 100%; padding: 8px 10px; min-height: 38px;
  border: 1px solid var(--border-strong); border-radius: var(--radius-sm);
  background: var(--surface); color: var(--text);
}
input:focus, select:focus, textarea:focus {
  outline: 2px solid var(--primary); outline-offset: -1px; border-color: var(--primary);
}

/* WCAG 2.4.7. Form controls had a focus style; links and buttons did not, so a
   keyboard user moving through the navigation could not see where they were.
   :focus-visible rather than :focus, so a mouse click does not draw a ring. */
a:focus-visible, button:focus-visible, [tabindex]:focus-visible,
summary:focus-visible, .row-open:focus-visible {
  outline: 2px solid var(--primary);
  outline-offset: 2px;
  border-radius: var(--radius-sm);
}
.nav a:focus-visible { outline-offset: -2px; }
/* The content region takes focus after a route change (see announceScreen).
   It is not a control, so it gets no ring — only the screen behind it moves. */
#main:focus { outline: none; }

/* Available to assistive technology, absent from the layout. Used for the
   labels and status text that a sighted user reads from context. */
.sr-only {
  position: absolute; width: 1px; height: 1px; padding: 0; margin: -1px;
  overflow: hidden; clip: rect(0 0 0 0); clip-path: inset(50%); white-space: nowrap; border: 0;
}

/* 2.4.1 Bypass blocks. Visually hidden until it takes focus, at which point it
   is the first thing on the page. */
.skip-link {
  position: absolute; left: 8px; top: -60px; z-index: 300;
  background: var(--surface); color: var(--text);
  border: 1px solid var(--border-strong); border-radius: var(--radius-sm);
  padding: 10px 14px; font-weight: 600; box-shadow: var(--shadow-lg);
}
/* No transition: a focus indicator that fades in over a tenth of a second is a
   focus indicator that is not there when the key is pressed. */
.skip-link:focus { top: 8px; text-decoration: none; }

/* 2.1.1 Keyboard. A row that opens a record now carries a real link (or a
   button, where the row opens a dialog rather than a page) in its first cell.
   It fills the cell so the whole of it stays clickable, and it inherits the
   cell's typography so the table does not suddenly look like a list of links. */
.row-open {
  display: block; width: 100%; text-align: inherit;
  font: inherit; color: inherit; background: none; border: 0;
  padding: 0; margin: 0; min-height: 0; cursor: pointer;
}
.row-open:hover { text-decoration: underline; }
td.num .row-open { text-align: right; }
/* 2.5.8: the control fills the height of its cell rather than just its line,
   so on a phone the target is the row, which is what it looks like. */
.row-open { padding: 7px 0; margin: -7px 0; }
input[readonly] { background: var(--surface-2); color: var(--text-muted); }
.help { font-size: 12px; color: var(--text-faint); margin-top: 3px; }
.err { font-size: 12.5px; color: var(--danger); margin-top: 3px; }
.form-grid { display: grid; grid-template-columns: repeat(2, minmax(0,1fr)); gap: 0 14px; }
.form-grid .full { grid-column: 1 / -1; }

/* --------------------------------------------------------------- banners */
.banner { padding: 11px 14px; border-radius: var(--radius-sm); margin-bottom: 14px; font-size: 13.5px;
  border: 1px solid transparent; }
.banner.error { background: var(--danger-soft); color: var(--danger); border-color: var(--danger); }
.banner.ok { background: var(--success-soft); color: var(--success); border-color: var(--success); }
.banner.info { background: var(--primary-soft); color: var(--primary-strong); border-color: var(--primary); }
.banner.warn { background: var(--warn-soft); color: var(--warn); border-color: var(--warn); }
.banner ul { margin: 6px 0 0; padding-left: 18px; }

/* ---------------------------------------------------------------- modal */
.modal-back { position: fixed; inset: 0; background: rgba(16,24,40,.5); display: flex;
  align-items: center; justify-content: center; padding: 16px; z-index: 100; }
.modal { background: var(--surface); border-radius: var(--radius); box-shadow: var(--shadow-lg);
  width: 100%; max-width: 720px; max-height: 90vh; display: flex; flex-direction: column; }
.modal-head { padding: 14px 18px; border-bottom: 1px solid var(--border); display: flex;
  align-items: center; gap: 10px; }
.modal-head h2 { flex: 1; }
.modal-body { padding: 18px; overflow-y: auto; }
.modal-foot { padding: 13px 18px; border-top: 1px solid var(--border); display: flex;
  gap: 8px; justify-content: flex-end; background: var(--surface-2); }

/* ------------------------------------------------------------- approvals */
.step { display: flex; gap: 12px; padding: 11px 0; border-bottom: 1px solid var(--border); }
.step:last-child { border-bottom: 0; }
.step-dot { width: 26px; height: 26px; border-radius: 999px; flex: none; display: grid;
  place-items: center; font-size: 12px; font-weight: 700; background: var(--info-soft);
  color: var(--text-muted); }
.step-dot.done { background: var(--success-soft); color: var(--success); }
.step-dot.rej { background: var(--danger-soft); color: var(--danger); }
.step-dot.now { background: var(--primary-soft); color: var(--primary);
  box-shadow: 0 0 0 3px var(--primary-soft); }
.step-main { flex: 1; min-width: 0; }
.step-name { font-weight: 600; }

/* ---------------------------------------------------------------- login */
.login-wrap { min-height: 100vh; display: grid; place-items: center; padding: 20px; }
.login-card { width: 100%; max-width: 380px; }
.login-card .card-body { padding: 24px; }
.login-title { font-size: 22px; font-weight: 680; margin-bottom: 2px; }
.demo-users { margin-top: 14px; border-top: 1px solid var(--border); padding-top: 12px; }
.demo-users button { width: 100%; justify-content: flex-start; margin-bottom: 5px; font-size: 12.5px; }

/* ------------------------------------------------------------------ misc */
.seg { display: inline-flex; gap: 0; }
.seg button { border-radius: 0; margin-left: -1px; }
.seg button:first-child { border-radius: var(--radius-sm) 0 0 var(--radius-sm); margin-left: 0; }
.seg button:last-child { border-radius: 0 var(--radius-sm) var(--radius-sm) 0; }
.toolbar { display: flex; gap: 8px; flex-wrap: wrap; align-items: center; margin-bottom: 14px; }
.toolbar input, .toolbar select { width: auto; min-width: 140px; }
.spacer { flex: 1; }
.kv { display: grid; grid-template-columns: minmax(120px, 180px) 1fr; gap: 6px 14px; font-size: 13.5px; }
.kv dt { color: var(--text-muted); }
.kv dd { margin: 0; font-weight: 550; }
.spin { display: inline-block; width: 15px; height: 15px; border: 2px solid var(--border-strong);
  border-top-color: var(--primary); border-radius: 50%; animation: spin .7s linear infinite; }
@keyframes spin { to { transform: rotate(360deg); } }
.loading { padding: 40px; text-align: center; color: var(--text-faint); }
.line-table input, .line-table select { min-height: 32px; padding: 5px 7px; font-size: 13px; }
.chart { display: flex; align-items: flex-end; gap: 6px; height: 120px; padding-top: 8px; }
.chart .bar { flex: 1; background: var(--primary); border-radius: 3px 3px 0 0; min-height: 2px;
  position: relative; opacity: .85; }
.chart .bar:hover { opacity: 1; }
.chart-labels { display: flex; gap: 6px; margin-top: 6px; }
.chart-labels span { flex: 1; text-align: center; font-size: 10.5px; color: var(--text-faint); }

/* Explanatory copy on screens that have something to say rather than to list. */
.prose { max-width: 68ch; color: var(--text-muted); font-size: 13.5px; line-height: 1.6; }
.prose p { margin: 0 0 8px; }
/* A card whose body holds a table loses its padding; prose above it must not. */
.card-body.tight > .prose,
.card-body.tight > .chip-row,
.card-body.tight > ul.tick,
.card-body.tight > .faint { padding: 14px 16px; }
.card-body.tight > .prose + * { margin-top: 0; }
ul.tick { list-style: none; margin: 0; padding: 0; }
ul.tick-none { margin: 6px 0 0; padding-left: 18px; }
ul.tick-none li { margin: 2px 0; }
ul.tick li { padding: 4px 0 4px 22px; position: relative; font-size: 13.5px; }
ul.tick li::before { content: '✓'; position: absolute; left: 2px; color: var(--success); font-weight: 600; }
.chip-row { display: flex; flex-wrap: wrap; gap: 6px; }
code { font-family: var(--mono, ui-monospace, monospace); font-size: 12.5px;
  background: var(--surface-alt, rgba(127,127,127,.12)); padding: 1px 5px; border-radius: 4px; }

/* Branding screen */
.logo-frame { display: flex; align-items: center; justify-content: center;
  min-height: 120px; padding: 16px; border: 1px dashed var(--border);
  border-radius: var(--radius-sm); background: var(--surface); }
.logo-frame img { max-height: 88px; max-width: 100%; object-fit: contain; }
.colour-row { display: flex; gap: 8px; align-items: center; }
.colour-row input[type="color"] { width: 44px; height: 34px; padding: 2px; flex: none;
  border: 1px solid var(--border); border-radius: var(--radius-sm); background: none; }
.colour-row input[type="text"] { flex: 1; min-width: 0; }
.swatch { display: inline-block; width: 14px; height: 14px; border-radius: 3px;
  border: 1px solid var(--border); vertical-align: -2px; margin-right: 6px; }
.brand-preview { margin-top: 8px; }
.bp-surface { background: var(--bp-surface, #fff); border: 1px solid var(--border);
  border-radius: var(--radius-sm); padding: 18px; display: flex; gap: 16px;
  align-items: center; justify-content: space-between; flex-wrap: wrap; }
.bp-text { color: var(--bp-text, #111827); font-size: 14px; }
.bp-button { background: var(--bp-primary, #2563eb); color: var(--bp-on-primary, #fff);
  border: 0; padding: 9px 16px; border-radius: var(--radius-sm); font-weight: 560; }

/* ------------------------------------------------------ responsive (UXD-010..013) */
@media (max-width: 1100px) { .grid.c4 { grid-template-columns: repeat(2, minmax(0,1fr)); } }
@media (max-width: 900px) {
  .shell { grid-template-columns: 1fr; }
  .sidebar {
    position: fixed; left: 0; top: 0; bottom: 0; width: 250px; z-index: 60; height: 100vh;
    transform: translateX(-100%); transition: transform .18s ease; box-shadow: var(--shadow-lg);
  }
  .sidebar.open { transform: translateX(0); }
  .menu-btn { display: inline-flex; }
  .scrim { position: fixed; inset: 0; background: rgba(16,24,40,.45); z-index: 50; }
  .grid.c3, .grid.c2 { grid-template-columns: 1fr; }
  .content { padding: 14px; }
  .form-grid { grid-template-columns: 1fr; }
}
@media (max-width: 620px) {
  .grid.c4 { grid-template-columns: 1fr 1fr; }
  .topbar { padding: 10px 12px; }
  .stat .value { font-size: 19px; }
  .kv { grid-template-columns: 1fr; gap: 2px 0; }
  .kv dd { margin-bottom: 8px; }

  /* UXD-012: on a phone, a record view is a stack of sections and only the
     first needs to be open. The card head becomes the control; the marker
     turns; and the whole thing works without JavaScript, so a section cannot
     get stuck shut by a script that failed to load. */
  .card.collapsible > .card-head { cursor: pointer; user-select: none; }
  .card.collapsible > .card-head::after {
    content: '⌄'; margin-left: auto; font-size: 16px; line-height: 1;
    color: var(--text-faint); transition: transform .15s ease;
  }
  .card.collapsible[data-open="false"] > .card-head::after { transform: rotate(-90deg); }
  .card.collapsible[data-open="false"] > .card-body { display: none; }

  /* UXD-011/013: record lists become cards; no field is lost or overlapped. */
  table.responsive thead { display: none; }
  table.responsive, table.responsive tbody, table.responsive tr, table.responsive td { display: block; width: 100%; }
  table.responsive tr {
    border: 1px solid var(--border); border-radius: var(--radius-sm);
    margin-bottom: 10px; padding: 4px 0; background: var(--surface);
  }
  table.responsive td { border: 0; padding: 5px 12px; display: flex; gap: 12px; }
  table.responsive td::before {
    content: attr(data-label); flex: none; width: 42%; color: var(--text-muted);
    font-size: 12px; font-weight: 580;
  }
  table.responsive td.num { text-align: left; justify-content: flex-start; }
  table.responsive td:empty { display: none; }
}
