/* ==========================================================================
   GSE Shepherd design system: shepherd.css
   Industrial brutalist x Nothing. Binding brief: docs/DESIGN_SPEC.md.
   Class reference and examples: docs/DESIGN_SYSTEM.md.

   Rules this file keeps (and pages must keep too):
   - Raw hex appears ONLY in the :root token block below.
   - border-radius is 0 everywhere except LED dots and .sh-led.
   - No gradients except the spec textures (LED dots, hatch, hazard band),
     no shadows except the focus ring halo, no glow, blur or translucency.
   - --hazard is for state only (see the spec's list).
   - System fonts only; no external resources (grain is a data: URI).
   ========================================================================== */

/* --------------------------------------------------------------------------
   1. Tokens
   -------------------------------------------------------------------------- */
:root {
  /* Colour (spec section 2) */
  --void: #000000;
  --plate: #121212;
  --plate-hi: #1A1A1A;
  --rule: #262626;
  --edge: #6B6B6B;
  --dim: #8A8A8A;
  --ash: #9A9A9A;
  --text: #D4D4D4;
  --signal: #EDEDED;
  --focus: #FFFFFF;
  --hazard: #FF2A2A;

  /* Fixed values the spec names inline, tokenised so no hex leaves this block */
  --signal-hi: #FFFFFF;  /* primary button hover fill */
  --dashed: #404040;     /* dashed "not available now" lines (disabled, locked) */
  --dot-off: #1C1C1C;    /* unlit LED dots (the matrix field) */
  --dot-dim: #3A3A3A;    /* lit dots of an inactive / dim numeral */

  /* Type */
  --f-grot: "Helvetica Neue", Helvetica, "Nimbus Sans", Arial, sans-serif;
  --f-mono: ui-monospace, "SF Mono", Menlo, "Cascadia Mono", Consolas, "Liberation Mono", "DejaVu Sans Mono", monospace;

  /* Space (4px base) */
  --s1: 4px;
  --s2: 8px;
  --s3: 12px;
  --s4: 16px;
  --s5: 24px;
  --s6: 32px;
  --s7: 48px;
  --s8: 64px;
  --s9: 96px;

  /* Motion. Use as: transition: color var(--t-tick); animation: name var(--t-print) both; */
  --t-tick: 80ms linear;
  --t-press: 60ms;
  --t-shift: 260ms cubic-bezier(.65, 0, .15, 1);
  --t-print: 360ms steps(12, jump-end);
  --t-open: 160ms cubic-bezier(.16, 1, .3, 1);
  --t-blink: 1s steps(2) infinite;

  /* Textures */
  --hatch: repeating-linear-gradient(135deg, var(--plate-hi) 0 1px, transparent 1px 7px);
  --hazard-stripes: repeating-linear-gradient(135deg, var(--hazard) 0 8px, var(--void) 8px 16px);
  /* feTurbulence grain, white at alpha <= 0.06 (alpha = 0.06 x noise) */
  --grain: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' width='160' height='160'%3E%3Cfilter id='g'%3E%3CfeTurbulence type='fractalNoise' baseFrequency='.85' numOctaves='2' stitchTiles='stitch'/%3E%3CfeColorMatrix values='0 0 0 0 1 0 0 0 0 1 0 0 0 0 1 0.06 0 0 0 0'/%3E%3C/filter%3E%3Crect width='100%25' height='100%25' filter='url(%23g)'/%3E%3C/svg%3E");

  /* Layout constants */
  --header-h: 64px;
  --gutter: 24px;

  color-scheme: dark;
}

@media (max-width: 639px) {
  :root { --gutter: 16px; }
}

/* --------------------------------------------------------------------------
   2. Base
   -------------------------------------------------------------------------- */
*, *::before, *::after { box-sizing: border-box; }

[hidden] { display: none !important; }

html {
  background: var(--void);
  color: var(--text);
  -webkit-text-size-adjust: 100%;
  text-size-adjust: 100%;
}

/* Pages with a sticky header keep in-page targets clear of it. */
html:has(.sh-header--sticky) { scroll-padding-top: calc(var(--header-h) + 8px); }

/* NB: pages set an inline body{visibility:hidden} for FOUC; never set visibility here. */
body {
  margin: 0;
  min-height: 100vh;
  background: var(--void);
  color: var(--text);
  font-family: var(--f-grot);
  font-size: 1rem;
  line-height: 1.6;
  font-weight: 400;
  -webkit-font-smoothing: antialiased;
  -moz-osx-font-smoothing: grayscale;
}

h1, h2, h3, h4, h5, h6 {
  margin: 0;
  color: var(--signal);
  font-weight: 700;
}
p, ul, ol, dl, dd, figure, blockquote, pre { margin: 0; }

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

button, input, textarea, select {
  font: inherit;
  color: inherit;
  letter-spacing: inherit;
}
button { cursor: pointer; }

code, kbd, samp, pre, .sh-mono {
  font-family: var(--f-mono);
  font-variant-numeric: tabular-nums;
}

strong, b { color: var(--signal); font-weight: 700; }

::selection { background: var(--signal); color: var(--void); }

textarea { caret-color: var(--hazard); }

::placeholder { color: var(--dim); opacity: 1; }

/* Focus ring, everywhere. Never red, never removed without a replacement. */
:focus { outline: none; }
:focus-visible {
  outline: 2px solid var(--focus);
  outline-offset: 2px;
  box-shadow: 0 0 0 2px var(--void);
}
/* Inside clipped / overflow containers, pull the ring inside. */
.sh-focus-inset:focus-visible,
.sh-focus-inset :focus-visible {
  outline-offset: -4px;
  box-shadow: inset 0 0 0 2px var(--void);
}

/* Accessibility utilities */
.sh-visually-hidden,
.sh-skip:not(:focus) {
  position: absolute !important;
  width: 1px !important;
  height: 1px !important;
  padding: 0 !important;
  margin: -1px !important;
  overflow: hidden !important;
  clip: rect(0 0 0 0) !important;
  clip-path: inset(50%) !important;
  white-space: nowrap !important;
  border: 0 !important;
}

/* Skip link: hidden until focused, then a primary button at top-left. */
.sh-skip {
  position: fixed;
  top: var(--s2);
  left: var(--s2);
  z-index: 1000;
  display: inline-flex;
  align-items: center;
  min-height: 44px;
  padding: 0 20px;
  background: var(--signal);
  color: var(--void);
  border: 1px solid var(--signal);
  font-family: var(--f-grot);
  font-size: .875rem;
  line-height: 1;
  font-weight: 700;
  letter-spacing: .01em;
  text-decoration: none;
}

/* --------------------------------------------------------------------------
   3. Layout primitives
   -------------------------------------------------------------------------- */
.sh-container {
  width: 100%;
  max-width: 1280px;
  margin-inline: auto;
  padding-inline: var(--gutter);
}

/* Site header bar: 64px, 1px rule below. Put .sh-container inside. */
.sh-header {
  background: var(--void);
  border-bottom: 1px solid var(--rule);
}
.sh-header--sticky {
  position: sticky;
  top: 0;
  z-index: 50;
}
.sh-header__inner {
  display: flex;
  align-items: center;
  gap: var(--s4);
  min-height: calc(var(--header-h) - 1px);
}
.sh-header__spacer { flex: 1 1 auto; }
.sh-header__actions {
  display: flex;
  align-items: center;
  gap: var(--s3);
  flex-wrap: wrap;
  min-width: 0;
}
.sh-wordmark {
  font-family: var(--f-grot);
  font-size: .9375rem;
  line-height: 1;
  font-weight: 700;
  letter-spacing: .02em;
  text-transform: uppercase;
  color: var(--signal);
  text-decoration: none;
  white-space: nowrap;
}
a.sh-wordmark:hover { text-decoration: underline; text-underline-offset: 3px; }

/* 12-column flush-left grid. Spans apply from 900px; below that every child is full width. */
.sh-grid-12 {
  display: grid;
  grid-template-columns: repeat(12, minmax(0, 1fr));
  column-gap: var(--s5);
  row-gap: var(--s5);
}
.sh-grid-12 > * { grid-column: 1 / -1; min-width: 0; }
@media (min-width: 900px) {
  /* auto / span N, so a .sh-start-N class can set the start line */
  .sh-span-1 { grid-column: auto / span 1; }
  .sh-span-2 { grid-column: auto / span 2; }
  .sh-span-3 { grid-column: auto / span 3; }
  .sh-span-4 { grid-column: auto / span 4; }
  .sh-span-5 { grid-column: auto / span 5; }
  .sh-span-6 { grid-column: auto / span 6; }
  .sh-span-7 { grid-column: auto / span 7; }
  .sh-span-8 { grid-column: auto / span 8; }
  .sh-span-9 { grid-column: auto / span 9; }
  .sh-span-10 { grid-column: auto / span 10; }
  .sh-span-11 { grid-column: auto / span 11; }
  .sh-span-12 { grid-column: auto / span 12; }
  .sh-start-1 { grid-column-start: 1; }
  .sh-start-2 { grid-column-start: 2; }
  .sh-start-3 { grid-column-start: 3; }
  .sh-start-4 { grid-column-start: 4; }
  .sh-start-5 { grid-column-start: 5; }
  .sh-start-6 { grid-column-start: 6; }
  .sh-start-7 { grid-column-start: 7; }
  .sh-start-8 { grid-column-start: 8; }
  .sh-start-9 { grid-column-start: 9; }
  .sh-start-10 { grid-column-start: 10; }
  .sh-start-11 { grid-column-start: 11; }
  .sh-start-12 { grid-column-start: 12; }
}

/* Seam grid: 1px --rule lines between and around cells. Cells sit on --void
   by default; add .sh-plate to a cell for a raised cell.
   Columns: --cols (desktop) and --cols-sm (under 640px, default 1). */
.sh-seams {
  --cols: 1;
  --cols-sm: 1;
  display: grid;
  grid-template-columns: repeat(var(--cols), minmax(0, 1fr));
  gap: 1px;
  background: var(--rule);
  border: 1px solid var(--rule);
}
.sh-seams > * { background-color: var(--void); min-width: 0; }
.sh-seams > .sh-plate { background-color: var(--plate); }
.sh-seams--2 { --cols: 2; }
.sh-seams--3 { --cols: 3; }
.sh-seams--4 { --cols: 4; }
.sh-seams--sm-2 { --cols-sm: 2; }
@media (max-width: 639px) {
  .sh-seams { grid-template-columns: repeat(var(--cols-sm), minmax(0, 1fr)); }
}
.sh-cell { padding: var(--s4); }

/* Raised plate with grain. The only surface that carries grain. */
.sh-plate {
  background-color: var(--plate);
  background-image: var(--grain);
  background-repeat: repeat;
}

.sh-section { margin-block-start: var(--s9); }
.sh-section--first { margin-block-start: var(--s8); }
.sh-section__head { margin-block-end: var(--s5); }
.sh-rule { height: 0; margin: 0; border: 0; border-top: 1px solid var(--rule); }
@media (max-width: 639px) {
  .sh-section { margin-block-start: var(--s8); }
  .sh-section--first { margin-block-start: var(--s7); }
}

/* Stack helpers for data clusters (4-8px) and blocks (16-24px) */
.sh-stack-2 > * + * { margin-block-start: var(--s2); }
.sh-stack-4 > * + * { margin-block-start: var(--s4); }
.sh-stack-5 > * + * { margin-block-start: var(--s5); }

/* --------------------------------------------------------------------------
   4. Type roles
   -------------------------------------------------------------------------- */
.sh-display {
  font-family: var(--f-grot);
  font-size: clamp(2.5rem, 6vw, 5.5rem);
  line-height: .9;
  font-weight: 700;
  letter-spacing: -.04em;
  text-transform: uppercase;
  color: var(--signal);
}
.sh-title {
  font-family: var(--f-grot);
  font-size: clamp(2rem, 4.2vw, 3.75rem);
  line-height: .92;
  font-weight: 700;
  letter-spacing: -.03em;
  text-transform: uppercase;
  color: var(--signal);
  overflow-wrap: break-word;
}
.sh-card-title {
  font-family: var(--f-grot);
  font-size: 1.375rem;
  line-height: 1.05;
  font-weight: 700;
  letter-spacing: -.015em;
  text-transform: uppercase;
  color: var(--signal);
}
.sh-h2 {
  font-family: var(--f-grot);
  font-size: 1.375rem;
  line-height: 1.15;
  font-weight: 700;
  letter-spacing: -.01em;
  color: var(--signal);
}
.sh-h3 {
  font-family: var(--f-grot);
  font-size: 1.125rem;
  line-height: 1.3;
  font-weight: 700;
  color: var(--signal);
}
.sh-panel-title,
.sh-h4 {
  font-family: var(--f-mono);
  font-size: .75rem;
  line-height: 1.3;
  font-weight: 600;
  letter-spacing: .08em;
  text-transform: uppercase;
  font-variant-numeric: tabular-nums;
}
.sh-panel-title { color: var(--signal); }
.sh-h4 { color: var(--ash); }
.sh-lead {
  font-size: 1.125rem;
  line-height: 1.5;
  font-weight: 400;
  color: var(--text);
  max-width: 56ch;
}
.sh-body {
  font-size: 1rem;
  line-height: 1.6;
  font-weight: 400;
  color: var(--text);
  max-width: 68ch;
}
.sh-small { font-size: .875rem; line-height: 1.5; }
.sh-meta {
  font-family: var(--f-mono);
  font-size: .75rem;
  line-height: 1.3;
  font-weight: 500;
  letter-spacing: .08em;
  text-transform: uppercase;
  font-variant-numeric: tabular-nums;
  color: var(--ash);
}
/* Text colour helpers */
.sh-strong { color: var(--signal); font-weight: 700; }   /* replaces cs-text-strong */
.sh-muted { color: var(--ash); }                          /* replaces cs-text-secondary */
.sh-dim { color: var(--dim); }
.sh-signal { color: var(--signal); }

kbd, .sh-kbd {
  display: inline-block;
  font-family: var(--f-mono);
  font-size: .75rem;
  line-height: 1.3;
  font-weight: 500;
  letter-spacing: 0;
  padding: 0 4px;
  border: 1px solid var(--edge);
  color: inherit;
  font-variant-numeric: tabular-nums;
  white-space: nowrap;
}

/* --------------------------------------------------------------------------
   5. LED dot-matrix numeral
   "Shepherd Matrix" is the installed bold grotesque (local() only, nothing is
   downloaded) with its vertical metrics overridden so the line box is exactly
   the digit height (.75em above the baseline, .03em below). With line-height
   .78 the box then wraps the glyphs in every fallback font, so background-clip
   text never crops the tops of the digits.
   -------------------------------------------------------------------------- */
@font-face {
  font-family: "Shepherd Matrix";
  src: local("Helvetica Neue Bold"), local("HelveticaNeue-Bold"),
       local("Helvetica Bold"), local("Helvetica-Bold"),
       local("Nimbus Sans Bold"), local("NimbusSans-Bold"),
       local("Nimbus Sans L Bold"), local("NimbusSanL-Bold"),
       local("Arial Bold"), local("Arial-BoldMT"),
       local("Liberation Sans Bold"), local("LiberationSans-Bold");
  font-weight: 700;
  font-style: normal;
  ascent-override: 75%;
  descent-override: 3%;
  line-gap-override: 0%;
}

/* --------------------------------------------------------------------------
   LED dot-matrix numeral, usage
   <span class="sh-matrix-field sh-matrix-field--l" aria-hidden="true">
     <span class="sh-matrix">01</span>
   </span>
   The field is padded by whole pitches so the lit dots land on the unlit grid.
   -------------------------------------------------------------------------- */
/* Registered so a --lit change (e.g. hovering an inactive card) can
   transition at --t-tick. The field supplies the default; the numeral inherits. */
@property --lit {
  syntax: "<color>";
  inherits: true;
  initial-value: transparent;
}
.sh-matrix-field {
  --pitch: 8px;
  --lit: var(--signal);
  display: inline-flex;
  padding: var(--pitch);
  background: radial-gradient(circle closest-side, var(--dot-off) 72%, transparent 78%) 0 0 / var(--pitch) var(--pitch);
  line-height: 0;
  user-select: none;
}
.sh-matrix {
  display: block;
  font-family: "Shepherd Matrix", var(--f-grot);
  font-weight: 700;
  line-height: .78;
  /* Spec 2 collision rule: -.06em makes 0 touch 4 under Nimbus Sans and
     Arial, so the numeral is loosened to -.03em. */
  letter-spacing: -.03em;
  white-space: nowrap;
  font-variant-numeric: tabular-nums;
  /* Tracking eats the right side bearing of the last glyph; give it back. */
  padding-inline-end: .03em;
  background: radial-gradient(circle closest-side, var(--lit, var(--signal)) 72%, transparent 78%) 0 0 / var(--pitch, 8px) var(--pitch, 8px);
  -webkit-background-clip: text;
  background-clip: text;
  color: transparent;
  user-select: none;
  transition: --lit var(--t-tick);
}
.sh-matrix--dim, .sh-matrix-field--dim { --lit: var(--dot-dim); }

/* Each numeral box is snapped to a whole number of dot rows (--rows x
   --pitch, the nearest whole pitch to the .78em line box). That keeps the
   field a whole number of pitches tall, drops the partial bottom row of dots,
   and lets the print step exactly one row at a time. Only the descent
   overshoot (under .03em) falls outside the box, and background-clip:text
   never paints there anyway. */
.sh-matrix-field--xl { --pitch: 8px; }
.sh-matrix-field--xl .sh-matrix {
  font-size: clamp(180px, 38vw, 440px);
  height: round(nearest, .78em, var(--pitch)); /* ignored where round() is unsupported */
}
.sh-matrix-field--l { --pitch: 8px; --rows: 16; }           /* 168px x .78 = 131px -> 16 x 8 = 128px */
.sh-matrix-field--l .sh-matrix { font-size: 168px; }
.sh-matrix-field--m { --pitch: 8px; --rows: 12; }           /* 120px x .78 = 94px -> 12 x 8 = 96px; 8px reads as round LEDs at 1x DPR */
.sh-matrix-field--m .sh-matrix { font-size: 120px; }
.sh-matrix-field--l .sh-matrix,
.sh-matrix-field--m .sh-matrix { height: calc(var(--pitch) * var(--rows)); }
@media (max-width: 639px) {
  .sh-matrix-field--xl, .sh-matrix-field--l { --pitch: 6px; }
  .sh-matrix-field--l { --rows: 17; }                       /* 128px x .78 = 100px -> 17 x 6 = 102px */
  .sh-matrix-field--l .sh-matrix { font-size: 128px; }
  .sh-matrix-field--m { --pitch: 6px; --rows: 11; }         /* 88px x .78 = 69px -> 11 x 6 = 66px */
  .sh-matrix-field--m .sh-matrix { font-size: 88px; }
}

/* Print: the numeral is revealed row by row. Add .is-printing (remove and
   re-add, with a reflow in between, to replay). */
@keyframes sh-print {
  from { clip-path: inset(0 0 100% 0); }
  to { clip-path: inset(0 0 0 0); }
}
/* --t-print supplies the 360ms duration; the step count is overridden to
   one step per dot row (--rows, set per size) so the leading edge never cuts
   through a row. Sizes without --rows fall back to the token's 12 steps. */
.sh-matrix.is-printing {
  animation: sh-print var(--t-print) both;
  animation-timing-function: steps(var(--rows, 12), jump-end);
}

@supports not ((background-clip: text) or (-webkit-background-clip: text)) {
  .sh-matrix { background: none; color: var(--signal); }
  .sh-matrix--dim, .sh-matrix-field--dim .sh-matrix { color: var(--edge); }
}

/* --------------------------------------------------------------------------
   6. Buttons
   -------------------------------------------------------------------------- */
.sh-btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: var(--s3);
  min-height: 44px;
  padding: 0 20px;
  margin: 0;
  border: 1px solid transparent;
  border-radius: 0;
  background: transparent;
  color: var(--signal);
  font-family: var(--f-grot);
  font-size: .875rem;
  line-height: 1;
  font-weight: 700;
  letter-spacing: .01em;
  text-transform: none;
  text-decoration: none;
  white-space: nowrap;
  cursor: pointer;
  user-select: none;
  transition: background-color var(--t-tick), border-color var(--t-tick), color var(--t-tick), transform var(--t-press);
}
.sh-btn:active:not(:disabled):not([aria-disabled="true"]) { transform: translateY(1px); }

.sh-btn--primary {
  background: var(--signal);
  border-color: var(--signal);
  color: var(--void);
}
.sh-btn--primary:hover:not(:disabled):not([aria-disabled="true"]) {
  background: var(--signal-hi);
  border-color: var(--signal-hi);
}

.sh-btn--secondary {
  background: transparent;
  border-color: var(--signal);
  color: var(--signal);
}
.sh-btn--secondary:hover:not(:disabled):not([aria-disabled="true"]) {
  background: var(--signal);
  color: var(--void);
}

/* Disabled: a dashed line means "not available now". */
.sh-btn:disabled,
.sh-btn[aria-disabled="true"] {
  background: transparent;
  border: 1px dashed var(--dashed);
  color: var(--dim);
  cursor: not-allowed;
}

/* Busy (e.g. Send while "Sending..."): NOT disabled styling, solid border.
   Wins over :disabled because JS disables the button while sending. */
.sh-btn[aria-busy="true"],
.sh-btn.is-busy {
  background: transparent;
  border: 1px solid var(--signal);
  color: var(--signal);
  cursor: progress;
}

.sh-btn--compact {
  min-height: 32px;
  padding: 0 12px;
  font-size: .8125rem;
}
@media (pointer: coarse) {
  .sh-btn--compact { min-height: 44px; }
}
.sh-btn--block { display: flex; width: 100%; justify-content: space-between; }
.sh-btn--icon {
  width: 48px;
  min-height: 48px;
  height: 48px;
  padding: 0;
}
.sh-btn--icon svg { width: 20px; height: 20px; }
@media (max-width: 639px) {
  .sh-btn--icon { width: 44px; height: 44px; min-height: 44px; }
}
.sh-btn kbd { border-color: currentColor; }

/* Row of actions, right-aligned on wide screens */
.sh-actions {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  gap: var(--s2) var(--s3);
}
.sh-actions__push { margin-inline-start: auto; }

/* --------------------------------------------------------------------------
   7. Links (replaces cs-link)
   -------------------------------------------------------------------------- */
.sh-link,
.rich-text a {
  color: var(--signal);
  text-decoration-line: underline;
  text-decoration-thickness: 1px;
  text-underline-offset: 3px;
  transition: text-decoration-thickness var(--t-tick);
}
.sh-link:hover, .sh-link:focus-visible,
.rich-text a:hover, .rich-text a:focus-visible {
  text-decoration-thickness: 2px;
}
.sh-link--back { white-space: nowrap; display: inline-flex; align-items: center; gap: var(--s2); min-height: 44px; }
.sh-link--back svg { width: 14px; height: 14px; }

/* --------------------------------------------------------------------------
   8. Status LED and tags
   -------------------------------------------------------------------------- */
.sh-led {
  display: inline-block;
  flex: none;
  width: 6px;
  height: 6px;
  border-radius: 50%;
  border: 1px solid var(--edge);
  background: transparent;
  vertical-align: middle;
}
.sh-led--on { background: var(--signal); border-color: var(--signal); }
.sh-led--busy {
  background: var(--hazard);
  border-color: var(--hazard);
  animation: sh-blink var(--t-blink);
}
@keyframes sh-blink {
  0%, 50% { opacity: 1; }
  50.01%, 100% { opacity: 0; }
}

/* Mono status tag (e.g. "Seen", "Loading", alert tags). Uppercased by CSS. */
.sh-tag {
  display: inline-flex;
  align-items: center;
  flex: none;
  min-height: 20px;
  padding: 0 6px;
  border: 1px solid var(--edge);
  font-family: var(--f-mono);
  font-size: .75rem;
  line-height: 1.3;
  font-weight: 600;
  letter-spacing: .08em;
  text-transform: uppercase;
  color: var(--ash);
  white-space: nowrap;
}

/* --------------------------------------------------------------------------
   9. Alerts (replaces cs-alert / cs-flashbar)
   -------------------------------------------------------------------------- */
.sh-alert {
  display: flex;
  align-items: flex-start;
  flex-wrap: wrap;
  gap: var(--s2) var(--s4);
  padding: var(--s3) var(--s4);
  border: 1px solid var(--edge);
  background: var(--void);
  color: var(--text);
  font-size: 1rem;
  line-height: 1.5;
}
/* Inner row, for alerts whose display JS sets to "block" (#config-error). */
.sh-alert__row {
  display: flex;
  align-items: flex-start;
  flex-wrap: wrap;
  gap: var(--s2) var(--s4);
}
.sh-alert__tag {
  flex: none;
  display: inline-flex;
  align-items: center;
  min-height: 24px;
  font-family: var(--f-mono);
  font-size: .75rem;
  line-height: 1.3;
  font-weight: 600;
  letter-spacing: .08em;
  text-transform: uppercase;
  color: var(--ash);
}
.sh-alert__body { flex: 1 1 30ch; min-width: 0; max-width: 68ch; }
.sh-alert__body > * + * { margin-top: var(--s2); }
.sh-alert__action { flex: none; margin-inline-start: auto; }

.sh-alert--notice { border-color: var(--edge); }

.sh-alert--error {
  border: 1px solid var(--hazard);
  border-inline-start: 4px solid var(--hazard);
}
.sh-alert--error .sh-alert__tag { color: var(--hazard); }

/* #config-error: full-width sticky top bar, red fill, black text */
.sh-alert--config {
  position: sticky;
  top: 0;
  z-index: 100;
  width: 100%;
  padding: var(--s3) 0;
  border: 0;
  background: var(--hazard);
  color: var(--void);
}
.sh-alert--config .sh-alert__tag {
  color: var(--void);
  padding: 0 6px;
  border: 1px solid var(--void);
}
.sh-alert--config a,
.sh-alert--config .sh-link,
.sh-alert--config strong,
.sh-alert--config code { color: var(--void); }
.sh-alert--config code { background: transparent; border-color: var(--void); }
.sh-alert--config ::selection { background: var(--void); color: var(--hazard); }

/* --------------------------------------------------------------------------
   10. Form field (replaces cs-form-field / cs-textarea)
   -------------------------------------------------------------------------- */
.sh-field { display: grid; gap: var(--s2); align-content: start; min-width: 0; }
.sh-field__label {
  font-family: var(--f-mono);
  font-size: .75rem;
  line-height: 1.3;
  font-weight: 500;
  letter-spacing: .08em;
  text-transform: uppercase;
  color: var(--ash);
}
.sh-textarea {
  display: block;
  width: 100%;
  min-height: calc(4 * 1.55em + 24px);
  margin: 0;
  padding: 12px 16px;
  border: 1px solid var(--edge);
  border-radius: 0;
  background: var(--plate);
  color: var(--text);
  font-family: var(--f-mono);
  font-size: .9375rem;
  line-height: 1.55;
  font-variant-numeric: tabular-nums;
  caret-color: var(--hazard);
  resize: vertical;
  transition: border-color var(--t-tick);
  -webkit-appearance: none;
  appearance: none;
}
/* Focus: a 2px --signal border is the replacement for the ring; padding
   shrinks by 1px so nothing shifts. */
.sh-textarea:focus,
.sh-textarea:focus-visible {
  outline: none;
  box-shadow: none;
  border: 2px solid var(--signal);
  padding: 11px 15px;
}
/* Over the byte limit (set aria-invalid="true" or .is-over) */
.sh-textarea[aria-invalid="true"],
.sh-textarea.is-over {
  border: 2px solid var(--hazard);
  padding: 11px 15px;
}
.sh-textarea:disabled {
  border-style: dashed;
  border-color: var(--dashed);
  color: var(--dim);
  cursor: not-allowed;
}

/* Byte counter: --ash; data-level="near" (above 9000) --signal; "over" --hazard */
.sh-counter {
  font-family: var(--f-mono);
  font-size: .75rem;
  line-height: 1.3;
  font-weight: 500;
  letter-spacing: .08em;
  text-transform: uppercase;
  font-variant-numeric: tabular-nums;
  color: var(--ash);
  white-space: nowrap;
}
.sh-counter[data-level="near"] { color: var(--signal); }
.sh-counter[data-level="over"] { color: var(--hazard); }

/* Field messages (#challenge-limit-message, #challenge-status).
   data-tone="error" or "info" (JS). Sentence case, never uppercase. */
.sh-field__msg {
  margin: 0;
  padding: var(--s2) var(--s3);
  border: 1px solid var(--edge);
  color: var(--text);
  font-size: .875rem;
  line-height: 1.5;
  max-width: 68ch;
}
.sh-field__msg[data-tone="error"] {
  border-color: var(--hazard);
  border-inline-start: 4px solid var(--hazard);
}
.sh-field__msg[data-tone="info"] { border-color: var(--edge); }

/* --------------------------------------------------------------------------
   11. Segmented bar
   -------------------------------------------------------------------------- */
.sh-segments {
  display: flex;
  align-items: center;
  gap: 2px;
  min-width: 0;
}
.sh-segments__cell {
  flex: 1 1 0;
  height: 6px;
  background: var(--edge);
}

/* Carousel position index: each cell is a <button> with a >= 24x44 hit area
   and a 6px visual bar drawn by ::before. Keep .carousel-dot on each. */
.sh-segments--nav > * {
  position: relative;
  flex: 1 1 0;
  min-width: 24px;
  height: 44px;
  margin: 0;
  padding: 0;
  border: 0;
  border-radius: 0;
  background: transparent;
  cursor: pointer;
}
.sh-segments--nav > *::before {
  content: "";
  position: absolute;
  left: 0;
  right: 0;
  top: 19px;
  height: 6px;
  background: var(--edge);
  transition: background-color var(--t-tick);
}
.sh-segments--nav > .is-progress::before,
.sh-segments--nav > [data-progress="true"]::before { background: var(--ash); }
.sh-segments--nav > :hover::before { background: var(--signal); }
.sh-segments--nav > [aria-current="true"]::before { background: var(--hazard); }

/* Turn meter: used = --signal, unused = 1px --edge outline on void,
   at the limit (.is-limit on the bar) every cell is --hazard. */
.sh-segments--meter { width: 7.5rem; flex: none; }
.sh-segments--meter > * {
  flex: 1 1 0;
  height: 6px;
  border: 1px solid var(--edge);
  background: var(--void);
}
.sh-segments--meter > .is-used {
  border-color: var(--signal);
  background: var(--signal);
}
.sh-segments--meter.is-limit > * {
  border-color: var(--hazard);
  background: var(--hazard);
}

/* --------------------------------------------------------------------------
   12. Panels and panel header bar
   -------------------------------------------------------------------------- */
.sh-panel {
  border: 1px solid var(--rule);
  background-color: var(--void);
  min-width: 0;
}
.sh-panel.sh-plate { background-color: var(--plate); }
.sh-panel-head {
  display: flex;
  align-items: center;
  flex-wrap: wrap;
  gap: var(--s2) var(--s3);
  min-height: 44px;
  padding: var(--s1) var(--s4);
  border-bottom: 1px solid var(--rule);
}
.sh-panel-head__readouts {
  display: flex;
  align-items: center;
  flex-wrap: wrap;
  gap: var(--s2) var(--s3);
  margin-inline-start: auto;
}
.sh-panel__body { padding: var(--s4); }

/* --------------------------------------------------------------------------
   13. Rich text (content fragments rendered by buildSafe)
   -------------------------------------------------------------------------- */
.rich-text {
  color: var(--text);
  font-size: 1rem;
  line-height: 1.6;
  overflow-wrap: break-word;
}
.rich-text > :first-child { margin-top: 0; }
.rich-text > :last-child { margin-bottom: 0; }
.rich-text p {
  margin: 0 0 var(--s3);
  max-width: 68ch;
}
.rich-text strong { color: var(--signal); font-weight: 700; }
.rich-text em { font-style: italic; }
.rich-text h3 {
  margin: var(--s5) 0 var(--s2);
  font-size: 1.125rem;
  line-height: 1.3;
  font-weight: 700;
  color: var(--signal);
}
.rich-text h4 {
  margin: var(--s5) 0 var(--s2);
  font-family: var(--f-mono);
  font-size: .75rem;
  line-height: 1.3;
  font-weight: 600;
  letter-spacing: .08em;
  text-transform: uppercase;
  color: var(--ash);
}
.rich-text ul,
.rich-text ol {
  margin: 0 0 var(--s3);
  padding-inline-start: 20px;
  max-width: 68ch;
}
.rich-text ol { padding-inline-start: 32px; }
.rich-text ul { list-style-type: square; }
.rich-text ol { list-style-type: decimal-leading-zero; }
.rich-text li { margin: 0 0 var(--s1); padding-inline-start: var(--s1); }
.rich-text li > ul, .rich-text li > ol { margin: var(--s1) 0 0; }
.rich-text li::marker { color: var(--ash); }
.rich-text ol > li::marker {
  font-family: var(--f-mono);
  font-size: .8125rem;
  font-variant-numeric: tabular-nums;
}
.rich-text code {
  font-family: var(--f-mono);
  font-size: .9em;
  padding: 0 4px;
  background: var(--plate-hi);
  border: 1px solid var(--rule);
  color: var(--signal);
  overflow-wrap: anywhere;
}
.rich-text pre {
  margin: 0 0 var(--s3);
  padding: var(--s4);
  background: var(--void);
  border: 1px solid var(--rule);
  color: var(--text);
  font-family: var(--f-mono);
  font-size: .8125rem;
  line-height: 1.6;
  white-space: pre-wrap;
  overflow-wrap: anywhere;
  tab-size: 2;
}
.rich-text pre code {
  padding: 0;
  background: none;
  border: 0;
  font-size: inherit;
  color: inherit;
}
.rich-text blockquote {
  margin: 0 0 var(--s3);
  padding: 0 var(--s4);
  border-inline-start: 2px solid var(--signal);
  color: var(--ash);
  max-width: 68ch;
}
.rich-text blockquote p { color: inherit; }

/* --------------------------------------------------------------------------
   14. Payload strip (JS-built .payload around each pre, with .copy-btn)
   Works whatever the DOM order of label, pre and button: the grid places
   the 32px strip (label left, Copy right) above the pre.
   -------------------------------------------------------------------------- */
.rich-text .payload,
.payload {
  display: grid;
  grid-template-columns: minmax(0, 1fr) auto;
  grid-template-rows: minmax(32px, auto) auto;
  margin: 0 0 var(--s3);
  border: 1px solid var(--rule);
  background: var(--void);
}
.payload::before {
  content: "";
  grid-row: 1;
  grid-column: 1 / -1;
  background: var(--plate);
  border-bottom: 1px solid var(--rule);
}
.payload__label {
  grid-row: 1;
  grid-column: 1;
  align-self: center;
  padding: 0 var(--s3);
  font-family: var(--f-mono);
  font-size: .75rem;
  line-height: 1.3;
  font-weight: 500;
  letter-spacing: .08em;
  text-transform: uppercase;
  color: var(--ash);
}
.payload > .copy-btn {
  grid-row: 1;
  grid-column: 2;
  align-self: stretch;
  margin: 0;
}
.rich-text .payload > pre,
.payload > pre {
  grid-row: 2;
  grid-column: 1 / -1;
  margin: 0;
  border: 0;
}
@media (pointer: coarse) {
  .rich-text .payload, .payload { grid-template-rows: minmax(44px, auto) auto; }
}

/* Copy button feedback (JS sets data-state for 2s, alongside the text) */
.copy-btn[data-state="copied"],
.sh-btn.copy-btn[data-state="copied"]:hover:not(:disabled):not([aria-disabled="true"]) {
  background: var(--signal);
  border-color: var(--signal);
  color: var(--void);
}
.sh-btn.copy-btn[data-state="failed"],
.sh-btn.copy-btn[data-state="failed"]:hover:not(:disabled):not([aria-disabled="true"]) {
  background: transparent;
  border: 1px solid var(--hazard);
  color: var(--hazard);
}

/* --------------------------------------------------------------------------
   15. Reveal rows (hints, example solution, how to defend)
   .reveal-item[data-state="locked|available|open|seen"]
   -------------------------------------------------------------------------- */
.reveal-box { min-width: 0; }
.reveal-list { display: grid; gap: var(--s2); }
.reveal-item {
  border: 1px solid var(--edge);
  background: var(--void);
  min-width: 0;
}
.reveal-item + .reveal-item { margin-top: var(--s2); }
.reveal-list > .reveal-item + .reveal-item { margin-top: 0; }
.reveal-header {
  display: flex;
  align-items: center;
  flex-wrap: wrap;
  column-gap: var(--s3);
  row-gap: var(--s1);
  min-height: 54px; /* 56px row including the 1px borders */
  padding: var(--s1) 5px var(--s1) var(--s4);
}
.reveal-label {   /* replaces the cs-text-strong label span */
  font-family: var(--f-grot);
  font-size: .9375rem;
  line-height: 1.3;
  font-weight: 600;
  color: var(--signal);
}
.reveal-state {
  display: inline-flex;
  align-items: center;
  gap: var(--s2);
  font-size: .875rem;
  line-height: 1.4;
  color: var(--ash);
}
.reveal-lock { width: 12px; height: 12px; color: var(--dim); }
.reveal-header > button,
.reveal-header > .sh-btn { margin-inline-start: auto; }
.reveal-seen { display: none; }

.reveal-item[data-state="locked"] {
  border: 1px dashed var(--dashed);
  background-color: var(--void);
  background-image: var(--hatch);
}
.reveal-item[data-state="locked"] .reveal-label,
.reveal-item[data-state="locked"] .reveal-state { color: var(--dim); }

.reveal-item[data-state="available"] { border: 1px solid var(--edge); }

.reveal-item[data-state="open"] {
  border: 1px solid var(--edge);
  border-inline-start: 2px solid var(--signal);
}
.reveal-item[data-state="open"] .reveal-header { padding-inline-start: 15px; }

.reveal-item[data-state="seen"] .reveal-seen { display: inline-flex; }

.reveal-content {
  padding: var(--s4);
  border-top: 1px solid var(--rule);
  background-color: var(--plate);
  animation: sh-open var(--t-open) both;
}
@keyframes sh-open {
  from { opacity: 0; transform: translateY(-4px); }
  to { opacity: 1; transform: translateY(0); }
}
.reveal-note { margin: 0 0 var(--s3); font-size: .875rem; line-height: 1.5; color: var(--ash); }

/* --------------------------------------------------------------------------
   16. Chat transcript (#chat-log li.you / li.bot / li.pending, span.meta)
   -------------------------------------------------------------------------- */
#chat-log,
.sh-chat {
  list-style: none;
  margin: 0;
  padding: 0;
}
#chat-log li,
.sh-chat > li {
  display: grid;
  grid-template-columns: 96px minmax(0, 1fr);
  column-gap: var(--s4);
  row-gap: var(--s1);
  align-items: baseline;
  margin: 0;
  padding: var(--s3) var(--s4);
  border-top: 1px solid var(--rule);
  font-size: .9375rem;
  line-height: 1.55;
  white-space: pre-wrap;
  overflow-wrap: anywhere;
}
#chat-log li:first-child,
.sh-chat > li:first-child { border-top: 0; }
#chat-log .meta,
.sh-chat .meta {
  grid-column: 1;
  font-family: var(--f-mono);
  font-size: .75rem;
  line-height: 1.3;
  font-weight: 500;
  letter-spacing: .08em;
  text-transform: uppercase;
  font-variant-numeric: tabular-nums;
  color: var(--ash);
  white-space: normal;
  overflow-wrap: normal;
}
#chat-log .meta .meta__turn,
.sh-chat .meta .meta__turn { display: block; color: var(--dim); }
#chat-log li.you,
.sh-chat > li.you {
  background: var(--void);
  border-inline-start: 2px solid var(--signal);
  padding-inline-start: 14px;
  font-family: var(--f-mono);
  font-variant-numeric: tabular-nums;
  color: var(--signal);
}
#chat-log li.bot,
.sh-chat > li.bot {
  background-color: var(--plate);
  font-family: var(--f-grot);
  color: var(--text);
}
#chat-log li.pending,
.sh-chat > li.pending { color: var(--ash); }

/* Static "Assistant is responding" line with an 8x14 block cursor */
.sh-responding {
  font-family: var(--f-mono);
  font-size: .75rem;
  line-height: 1.3;
  font-weight: 500;
  letter-spacing: .08em;
  text-transform: uppercase;
  color: var(--ash);
  white-space: normal;
}
#chat-log li.sh-responding,
.sh-chat > li.sh-responding {
  display: flex;
  align-items: center;
  gap: var(--s2);
  font-size: .75rem;
  line-height: 1.3;
  white-space: normal;
  background-color: var(--plate);
}
.sh-cursor {
  display: inline-block;
  flex: none;
  width: 8px;
  height: 14px;
  vertical-align: middle;
  background: var(--signal);
  animation: sh-blink var(--t-blink);
}

.sh-empty {
  margin: 0;
  padding: var(--s5) var(--s4);
  font-size: .875rem;
  line-height: 1.5;
  color: var(--ash);
}

@media (max-width: 559px) {
  #chat-log li,
  .sh-chat > li { grid-template-columns: minmax(0, 1fr); }
  #chat-log .meta .meta__turn,
  .sh-chat .meta .meta__turn { display: inline; margin-inline-start: .5em; }
}

/* --------------------------------------------------------------------------
   17. Pager (previous / next challenge)
   -------------------------------------------------------------------------- */
.sh-pager {
  display: grid;
  grid-template-columns: repeat(2, minmax(0, 1fr));
  gap: 1px;
  background: var(--rule);
  border: 1px solid var(--rule);
}
.sh-pager__cell {
  position: relative;
  display: flex;
  flex-direction: column;
  justify-content: center;
  gap: var(--s2);
  min-height: 96px;
  padding: 20px;
  background: var(--void);
  color: var(--signal);
  text-decoration: none;
  min-width: 0;
}
.sh-pager__cell.next,
.sh-pager__cell--next {
  grid-column: 2;
  align-items: flex-end;
  text-align: right;
}
.sh-pager__cell.prev,
.sh-pager__cell--prev { grid-column: 1; }
.sh-pager__cell::after {
  content: "";
  position: absolute;
  inset: 0;
  border: 2px solid transparent;
  pointer-events: none;
  transition: border-color var(--t-tick);
}
.sh-pager__cell:hover::after,
.sh-pager__cell:focus-visible::after { border-color: var(--signal); }
.sh-pager__dir {
  font-family: var(--f-mono);
  font-size: .75rem;
  line-height: 1.3;
  font-weight: 500;
  letter-spacing: .08em;
  text-transform: uppercase;
  font-variant-numeric: tabular-nums;
  color: var(--ash);
}
.sh-pager__title {
  font-family: var(--f-grot);
  font-size: 1.25rem;
  line-height: 1.2;
  font-weight: 700;
  color: var(--signal);
  text-decoration-line: underline;
  text-decoration-thickness: 1px;
  text-underline-offset: 3px;
  overflow-wrap: break-word;
}
.sh-pager__cell:hover .sh-pager__title,
.sh-pager__cell:focus-visible .sh-pager__title { text-decoration-thickness: 2px; }
/* One side hidden: the other spans both columns. */
.sh-pager:has(> [hidden]) > :not([hidden]),
.sh-pager--single > * { grid-column: 1 / -1; }
@media (max-width: 559px) {
  .sh-pager { grid-template-columns: minmax(0, 1fr); }
  .sh-pager > .sh-pager__cell.sh-pager__cell { grid-column: 1 / -1; }
}

/* --------------------------------------------------------------------------
   18. Loading skeleton (static, never animated)
   -------------------------------------------------------------------------- */
.sh-skeleton { display: grid; gap: var(--s2); padding-block: var(--s2); }
.sh-skeleton > :not(.sh-skeleton__bar) { justify-self: start; }
.sh-skeleton__bar {
  display: block;
  height: 12px;
  background: var(--plate-hi);
}
.sh-skeleton__bar:nth-child(3n + 1) { width: 92%; }
.sh-skeleton__bar:nth-child(3n + 2) { width: 78%; }
.sh-skeleton__bar:nth-child(3n + 3) { width: 60%; }

/* --------------------------------------------------------------------------
   19. Hazard band (ground rules panel only)
   -------------------------------------------------------------------------- */
.sh-hazard-band {
  display: block;
  height: 6px;
  background: var(--hazard-stripes);
}

/* --------------------------------------------------------------------------
   20. Preference and mode overrides
   -------------------------------------------------------------------------- */
@media (prefers-reduced-motion: reduce) {
  :root {
    --t-tick: 0.01ms linear;
    --t-press: 0.01ms;
    --t-shift: 0.01ms linear;
    --t-print: 0.01ms linear;
    --t-open: 0.01ms linear;
    --t-blink: 0.01ms linear 1;
  }
  html:focus-within { scroll-behavior: auto; }
  *, *::before, *::after {
    animation-duration: 0.01ms !important;
    animation-iteration-count: 1 !important;
    transition-duration: 0.01ms !important;
    scroll-behavior: auto !important;
  }
  .sh-matrix.is-printing,
  .sh-led--busy,
  .sh-cursor,
  .reveal-content { animation: none !important; }
}

@media (prefers-contrast: more) {
  :root {
    --rule: var(--edge);
    --dim: var(--text);
    --ash: var(--text);
  }
  .sh-plate { background-image: none; }
}

@media (forced-colors: active) {
  .sh-plate { background-image: none; }
  .sh-matrix-field { background: none; }
  .sh-matrix {
    background: none;
    color: CanvasText;
    forced-color-adjust: none;
  }
  .sh-btn:disabled, .sh-btn[aria-disabled="true"] { color: GrayText; border-color: GrayText; }
  .sh-led, .sh-cursor, .sh-segments__cell,
  .sh-segments--nav > *::before, .sh-segments--meter > *,
  .sh-skeleton__bar { forced-color-adjust: none; }
  .sh-led { border-color: CanvasText; background: Canvas; }
  .sh-led--on { background: CanvasText; }
  .sh-led--busy { background: Highlight; border-color: Highlight; }
  .sh-cursor { background: CanvasText; }
  .sh-segments__cell, .sh-segments--nav > *::before { background: GrayText; }
  .sh-segments--nav > :hover::before { background: CanvasText; }
  .sh-segments--nav > [aria-current="true"]::before { background: Highlight; }
  .sh-segments--meter > * { background: Canvas; border-color: GrayText; }
  .sh-segments--meter > .is-used { background: CanvasText; border-color: CanvasText; }
  .sh-segments--meter.is-limit > * { background: Highlight; border-color: Highlight; }
  .sh-skeleton__bar { background: GrayText; }
  .sh-hazard-band { forced-color-adjust: none; background: CanvasText; height: 2px; }
  .reveal-item[data-state="locked"] { background-image: none; }
  .sh-pager__cell:hover::after, .sh-pager__cell:focus-visible::after { border-color: Highlight; }
  .copy-btn[data-state="failed"] { border-color: Mark; }
}
