/* ============================================================
   ALLOYSCAPE INSTRUMENT PANEL — design tokens
   House identity: authentic dark brown + ember orange.
   One accent. Semantic colour only where it encodes state.
   ============================================================ */
:root {
  /* surfaces — warm brown, not grey */
  --bg:            #16110D;
  --surface:       #1E1811;
  --surface-alt:   #261F16;
  --surface-hi:    #2E2519;
  --border:        #3A2F22;
  --border-hi:     #4C3D2B;

  /* type */
  --text:          #EDE6DC;
  --text-muted:    #C0AC93;   /* 6.86:1 worst case (on --surface-hi) */
  --text-faint:    #A4917A;   /* 4.95:1 worst case — #6E5F4E was 2.55:1, an AA failure */

  /* the one accent — ember */
  --ember:         #FF7A1A;
  --ember-hi:      #FF9147;
  --ember-dim:     #B85410;
  --ember-wash:    rgba(255,122,26,.10);
  --ember-line:    rgba(255,122,26,.34);

  /* state — always paired with a glyph or word, never colour alone */
  --ok:            #7FB86A;
  --ok-wash:       rgba(127,184,106,.12);
  --warn:          #E8A33D;
  --warn-wash:     rgba(232,163,61,.12);
  --bad:           #E86F5C;   /* 4.92:1 worst case — #E4614E was 4.39:1 */
  --bad-wash:      rgba(232,111,92,.12);
  --dark-state:    #9E8D76;   /* 4.68:1 worst case — #7A6A57 was 2.89:1, an AA failure */

  /* provenance chips */
  --measured:      #7FB86A;
  --demo:          #8AA0C8;
  --static-c:      #C58AB8;

  --r:   10px;
  --r-s: 6px;
  --mono: "JetBrains Mono", ui-monospace, SFMono-Regular, Menlo, monospace;
  --sans: "General Sans", -apple-system, BlinkMacSystemFont, "Segoe UI", sans-serif;

  --sp-1: 4px;  --sp-2: 8px;  --sp-3: 12px; --sp-4: 16px;
  --sp-5: 24px; --sp-6: 32px; --sp-7: 48px;
}

* { box-sizing: border-box; }

html, body { height: 100%; margin: 0; }
body {
  background: var(--bg);
  color: var(--text);
  font-family: var(--sans);
  font-size: 15px;
  line-height: 1.55;
  /* General Sans ships a narrow 0.20em space (system sans is 0.28em).
     Measured, then corrected — otherwise short words visually fuse. */
  word-spacing: .05em;
  -webkit-font-smoothing: antialiased;
  overflow: hidden;
}
@media (max-width: 900px) { body { overflow: auto; } }

.num, td.num, .kpi-val, time, .mono {
  font-family: var(--mono);
  word-spacing: normal;
  font-variant-numeric: tabular-nums lining-nums;
  font-feature-settings: "tnum" 1, "lnum" 1;
}

/* ---------- shell ---------- */
.shell {
  display: grid;
  grid-template-columns: 232px 1fr;
  grid-template-rows: auto 1fr;
  height: 100dvh;
}
@media (max-width: 900px) {
  .shell { grid-template-columns: 1fr; grid-template-rows: auto auto auto; height: auto; }
}

/* ---------- sidebar ---------- */
.side {
  grid-row: 1 / -1;
  background: var(--surface);
  border-right: 1px solid var(--border);
  overflow-y: auto;
  overscroll-behavior: contain;
  padding: var(--sp-4);
  display: flex; flex-direction: column; gap: var(--sp-5);
}
@media (max-width: 900px) { .side { grid-row: auto; border-right: 0; border-bottom: 1px solid var(--border); overflow: visible; } }

.brand { display: flex; align-items: center; gap: 10px; }
.brand svg { flex: 0 0 auto; color: var(--ember); }
.brand-txt { display: flex; flex-direction: column; line-height: 1.15; }
.brand-txt b { font-size: 15px; font-weight: 600; letter-spacing: -.01em; }
.brand-txt span { font-size: 12px; color: var(--text-faint); letter-spacing: .06em; text-transform: uppercase; }

nav { display: flex; flex-direction: column; gap: 2px; }
nav a {
  display: flex; align-items: center; justify-content: space-between; gap: var(--sp-2);
  padding: 7px 10px; border-radius: var(--r-s);
  color: var(--text-muted); text-decoration: none; font-size: 13.5px;
  border: 1px solid transparent;
}
nav a:hover { background: var(--surface-alt); color: var(--text); }
nav a[aria-current="true"] { background: var(--ember-wash); color: var(--ember-hi); border-color: var(--ember-line); }
nav a .tag { font-family: var(--mono); font-size: 12px; padding: 1px 5px; border-radius: 4px; background: var(--surface-hi); color: var(--text-faint); }
nav a[aria-current="true"] .tag { background: rgba(255,122,26,.18); color: var(--ember-hi); }

.side-law { border-top: 1px solid var(--border); padding-top: var(--sp-4); margin-top: auto; }
.side-law p { font-size: 12.5px; color: var(--text-faint); margin: 0 0 6px; }
.side-law b { color: var(--text-muted); font-weight: 500; }

/* ---------- header ---------- */
header {
  grid-column: 2; position: sticky; top: 0; z-index: 20;
  background: rgba(22,17,13,.86);
  backdrop-filter: blur(10px);
  border-bottom: 1px solid var(--border);
  padding: var(--sp-3) var(--sp-5);
  display: flex; align-items: center; gap: var(--sp-4); flex-wrap: wrap;
}
@media (max-width: 900px) { header { grid-column: 1; position: static; } }
header h1 { font-size: 16px; font-weight: 600; margin: 0; letter-spacing: -.01em; }
header .spacer { flex: 1 1 auto; }

.pill {
  display: inline-flex; align-items: center; gap: 6px;
  font-size: 12.5px; padding: 3px 9px; border-radius: 999px;
  border: 1px solid var(--border-hi); background: var(--surface-alt); color: var(--text-muted);
  white-space: nowrap;
}
.pill .dot { width: 7px; height: 7px; border-radius: 50%; background: var(--dark-state); flex: 0 0 auto; }
.pill.ok    { color: var(--ok);   border-color: rgba(127,184,106,.4); background: var(--ok-wash); }
.pill.ok .dot { background: var(--ok); }
.pill.warn  { color: var(--warn); border-color: rgba(232,163,61,.42); background: var(--warn-wash); }
.pill.warn .dot { background: var(--warn); }
.pill.bad   { color: var(--bad);  border-color: rgba(232,111,92,.44);  background: var(--bad-wash); }
.pill.bad .dot { background: var(--bad); }

.btn {
  font-family: var(--sans); font-size: 12.5px; cursor: pointer;
  padding: 6px 12px; border-radius: var(--r-s);
  background: var(--surface-alt); color: var(--text); border: 1px solid var(--border-hi);
}
.btn:hover { background: var(--surface-hi); border-color: var(--ember-line); }
.btn[aria-pressed="true"] { background: var(--ember-wash); color: var(--ember-hi); border-color: var(--ember-line); }
.btn:focus-visible, a:focus-visible, [tabindex]:focus-visible, th button:focus-visible {
  outline: 2px solid var(--ember-hi); outline-offset: 2px;
}

/* ---------- main ---------- */
main {
  grid-column: 2; overflow-y: auto; overscroll-behavior: contain;
  padding: var(--sp-5); display: flex; flex-direction: column; gap: var(--sp-6);
}
@media (max-width: 900px) { main { grid-column: 1; overflow: visible; padding: var(--sp-4); } }

section > .sec-head { display: flex; align-items: baseline; gap: var(--sp-3); flex-wrap: wrap; margin-bottom: var(--sp-3); }
section h2 { font-size: 13px; font-weight: 600; text-transform: uppercase; letter-spacing: .09em; color: var(--text-muted); margin: 0; }
section .sec-note { font-size: 12.5px; color: var(--text-faint); margin: 0; }

/* provenance chip — every panel declares where its number came from */
.prov {
  font-family: var(--mono); font-size: 12px; letter-spacing: .07em; text-transform: uppercase;
  padding: 2px 6px; border-radius: 4px; border: 1px solid;
}
.prov.measured { color: var(--measured); border-color: rgba(127,184,106,.4); background: var(--ok-wash); }
.prov.demo     { color: var(--demo);     border-color: rgba(138,160,200,.4); background: rgba(138,160,200,.1); }
.prov.static   { color: var(--static-c); border-color: rgba(197,138,184,.4); background: rgba(197,138,184,.1); }
.prov.inferred { color: var(--warn);     border-color: rgba(232,163,61,.42); background: var(--warn-wash); }

/* ---------- cards ---------- */
.grid { display: grid; gap: var(--sp-3); }
.kpis { grid-template-columns: repeat(auto-fit, minmax(178px, 1fr)); }
.card-grid > * { content-visibility: auto; contain-intrinsic-size: 0 200px; }

.card {
  background: var(--surface); border: 1px solid var(--border); border-radius: var(--r);
  padding: var(--sp-4);
}
.card.flag-warn { border-color: rgba(232,163,61,.45); box-shadow: inset 3px 0 0 var(--warn); }
.card.flag-bad  { border-color: rgba(228,97,78,.45);  box-shadow: inset 3px 0 0 var(--bad); }
.card.stale     { opacity: .55; }

.kpi-top { display: flex; align-items: center; justify-content: space-between; gap: var(--sp-2); margin-bottom: 6px; }
.kpi-lab { font-size: 12.5px; color: var(--text-muted); text-transform: uppercase; letter-spacing: .07em; }
.kpi-val { font-size: 30px; font-weight: 500; line-height: 1.1; letter-spacing: -.02em; }
.kpi-val small { font-size: 15px; color: var(--text-faint); font-weight: 400; }
.kpi-sub { font-size: 12px; color: var(--text-faint); margin-top: 4px; }
.kpi-sub.warn { color: var(--warn); }
.kpi-sub.bad  { color: var(--bad); }
.kpi-sub.ok   { color: var(--ok); }

/* ---------- the ladder ---------- */
.ladder { display: flex; flex-direction: column; gap: 6px; }
.rung {
  display: grid; grid-template-columns: 22px 1fr auto; align-items: center; gap: var(--sp-3);
  padding: 10px var(--sp-3); border-radius: var(--r-s);
  background: var(--surface-alt); border: 1px solid var(--border);
}
.rung .n { font-family: var(--mono); font-size: 12px; color: var(--text-faint); }
.rung .who { display: flex; flex-direction: column; gap: 2px; min-width: 0; }
.rung .who b { font-size: 13.5px; font-weight: 500; }
.rung .who span { font-size: 12.5px; color: var(--text-faint); }
.rung .bar { grid-column: 1 / -1; height: 6px; border-radius: 3px; background: var(--surface-hi); overflow: hidden; }
.rung .bar i { display: block; height: 100%; background: var(--ember); border-radius: 3px; transition: width .7s cubic-bezier(.4,0,.2,1); }
.rung.answering { border-color: var(--ember-line); background: var(--ember-wash); }
.rung.silent .who b { color: var(--text-faint); }
.rung.silent .bar i { background: var(--dark-state); }
.rung .pct { font-family: var(--mono); font-size: 14px; }

/* ---------- tables ---------- */
.tbl-wrap { overflow-x: auto; overscroll-behavior: contain; border: 1px solid var(--border); border-radius: var(--r); background: var(--surface); }
table { width: 100%; border-collapse: collapse; font-size: 13px; min-width: 620px; }
.cell-reads { font-size: 12.5px; }
.cell-why { color: var(--text-faint); font-size: 12.5px; }
thead th {
  position: sticky; top: 0; z-index: 1;
  background: var(--surface-alt); color: var(--text-muted);
  text-align: left; font-weight: 500; font-size: 12px; letter-spacing: .07em; text-transform: uppercase;
  padding: 9px var(--sp-3); border-bottom: 1px solid var(--border); white-space: nowrap;
}
thead th button { all: unset; cursor: pointer; display: inline-flex; gap: 4px; align-items: center; }
thead th button:hover { color: var(--text); }
tbody td { padding: 9px var(--sp-3); border-bottom: 1px solid rgba(58,47,34,.55); vertical-align: middle; }
tbody tr:last-child td { border-bottom: 0; }
tbody tr:hover { background: var(--surface-alt); }
td.num { text-align: right; }
.seat { display: inline-flex; align-items: center; gap: 7px; font-weight: 500; }
.mismatch { color: var(--warn); }
.mismatch b { font-weight: 600; }

.badge {
  display: inline-flex; align-items: center; gap: 5px;
  font-size: 12px; font-family: var(--mono); padding: 2px 7px; border-radius: 4px;
  border: 1px solid var(--border-hi); background: var(--surface-alt); color: var(--text-muted);
  white-space: nowrap;
}
.badge.ok   { color: var(--ok);   border-color: rgba(127,184,106,.4); background: var(--ok-wash); }
.badge.warn { color: var(--warn); border-color: rgba(232,163,61,.42); background: var(--warn-wash); }
.badge.bad  { color: var(--bad);  border-color: rgba(232,111,92,.44);  background: var(--bad-wash); }
.badge.dark { color: var(--dark-state); }

/* ---------- seat radar: three honest states ---------- */
.radar { display: grid; grid-template-columns: repeat(auto-fill, minmax(132px, 1fr)); gap: var(--sp-2); }
.radar-legend { margin: var(--sp-3) 0 0; }
.seat-chip {
  display: flex; align-items: center; gap: var(--sp-2);
  padding: 8px 10px; border-radius: var(--r-s);
  background: var(--surface-alt); border: 1px solid var(--border); font-size: 12.5px;
}
.seat-chip .glyph { font-family: var(--mono); font-size: 12px; width: 14px; text-align: center; flex: 0 0 auto; }
.seat-chip.attested   { border-color: rgba(127,184,106,.35); }
.seat-chip.attested .glyph   { color: var(--ok); }
.seat-chip.unattested { border-color: rgba(232,163,61,.4); }
.seat-chip.unattested .glyph { color: var(--warn); }
.seat-chip.dark       { opacity: .6; }
.seat-chip.dark .glyph { color: var(--dark-state); }
.seat-chip .nm { flex: 1 1 auto; min-width: 0; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; font-family: var(--mono); font-size: 12px; }
.seat-chip.unattested { background: var(--warn-wash); }
.seat-chip.attested   { background: var(--ok-wash); }

/* ---------- ghost feed ---------- */
.feed { display: flex; flex-direction: column; gap: var(--sp-2); max-height: 340px; overflow-y: auto; overscroll-behavior: contain; }
.msg { border: 1px solid var(--border); border-left-width: 3px; border-radius: var(--r-s); padding: 10px var(--sp-3); background: var(--surface-alt); }
.msg.clean    { border-left-color: var(--ok); }
.msg.degraded { border-left-color: var(--warn); }
.msg.ghost    { border-left-color: var(--bad); background: var(--bad-wash); }
.msg-head { display: flex; align-items: center; gap: var(--sp-2); flex-wrap: wrap; margin-bottom: 5px; }
.msg-head time { font-size: 12px; color: var(--text-faint); margin-left: auto; }
.msg-body { font-size: 13px; color: var(--text-muted); }
.msg-attest { font-family: var(--mono); font-size: 12px; color: var(--text-faint); margin-top: 6px; word-break: break-word; }
.msg.ghost .msg-body { color: var(--bad); font-style: italic; }

/* ---------- empty / skeleton ---------- */
.empty { text-align: center; padding: var(--sp-6) var(--sp-4); color: var(--text-faint); font-size: 13px; }
.empty b { display: block; color: var(--text-muted); font-weight: 500; margin-bottom: 4px; font-size: 14px; }
.sk { background: linear-gradient(90deg, var(--surface-alt) 25%, var(--surface-hi) 50%, var(--surface-alt) 75%); background-size: 200% 100%; animation: sh 1.4s infinite; border-radius: 4px; }
@keyframes sh { 0% { background-position: 200% 0; } 100% { background-position: -200% 0; } }

/* ---------- audit list ---------- */
.audit-row { display: grid; grid-template-columns: 1fr auto; gap: var(--sp-3); align-items: center; padding: 10px 0; border-bottom: 1px solid rgba(58,47,34,.55); }
.audit-row:last-child { border-bottom: 0; }
.audit-row .f { font-family: var(--mono); font-size: 12.5px; }
.audit-row .d { font-size: 12.5px; color: var(--text-faint); margin-top: 2px; }
.defects { display: flex; gap: 5px; flex-wrap: wrap; justify-content: flex-end; }

footer { color: var(--text-faint); font-size: 12.5px; padding: var(--sp-5) 0 var(--sp-6); border-top: 1px solid var(--border); }
footer a { color: var(--ember-hi); }

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

/* =========================================================
   PHONE — Rosa reads this on a phone, so the phone is not
   an afterthought. Tables stop being tables and become
   cards; every control clears a 44px thumb.
   ========================================================= */
@media (max-width: 760px) {
  /* no sideways scrolling of the whole page, ever */
  html, body { overflow-x: hidden; }
  /* widest label is COMPONENT / LAST SEEN / CALLS 24H — measured, not guessed */
  :root { --label-col: 100px; }

  .shell { grid-template-columns: 1fr; }
  aside { position: static; height: auto; border-right: 0; border-bottom: 1px solid var(--border); }
  /* the nav must not cost a full screen before the first number */
  aside nav { display: flex; flex-direction: row; flex-wrap: wrap; gap: 6px; }
  aside nav a {
    flex: 0 0 auto; min-height: 44px; display: flex; align-items: center;
    gap: 6px; padding: 0 12px; font-size: 13.5px;
    border: 1px solid var(--border); border-radius: 999px;
  }
  aside nav a .tag { margin-left: 0; }
  .side-law { display: none; }

  header { flex-wrap: wrap; gap: var(--sp-2); padding: var(--sp-3) var(--sp-4); }
  header h1 { font-size: 17px; flex: 1 1 100%; }
  header .spacer { display: none; }
  header .btn { flex: 1 1 40%; min-height: 44px; font-size: 13px; justify-content: center; }

  main { padding: var(--sp-4); }
  .kpis { grid-template-columns: 1fr 1fr; }
  .radar { grid-template-columns: 1fr 1fr; }
  .kpi-head { flex-wrap: wrap; }

  /* tables → stacked cards, each cell labelled by its column */
  table { min-width: 0; display: block; }
  table thead { position: absolute; width: 1px; height: 1px; overflow: hidden; clip-path: inset(50%); }
  table tbody, table tr, table td { display: block; width: auto; }
  table tr {
    border: 1px solid var(--border); border-radius: var(--r-s);
    background: var(--surface-alt); padding: var(--sp-3);
    margin-bottom: var(--sp-2);
  }
  table td {
    border: 0; padding: 3px 0; white-space: normal;
    display: grid; grid-template-columns: var(--label-col) 1fr; gap: var(--sp-3); align-items: baseline;
  }
  table td::before {
    content: attr(data-label); white-space: nowrap;
    font-size: 12px; letter-spacing: .06em; text-transform: uppercase;
    color: var(--text-faint); font-family: var(--sans);
  }
  table td.cell-key { font-weight: 600; color: var(--text); }
  /* a badge is a chip, not a banner — keep it the width of its word */
  table td > .badge, table td > .prov { justify-self: start; width: max-content; }
  table td.num { text-align: left; }

  .audit-row { grid-template-columns: 1fr; }
  .defects { justify-content: flex-start; }
  .feed { max-height: none; }
  .msg-head { flex-wrap: wrap; }
  .msg-head time { margin-left: 0; }
}

/* Narrow phones: one KPI per row. Two columns clipped the labels
   at 390px, and a clipped number is a lie by omission. */
@media (max-width: 520px) {
  .kpis  { grid-template-columns: 1fr; }
  .radar { grid-template-columns: 1fr 1fr; }
  header .btn { flex: 1 1 100%; }

}

/* thumb-sized controls on any touch device, not only narrow ones */
@media (pointer: coarse) {
  nav a, header .btn { min-height: 44px; }
}
