/* ============================================
   crt.css — NES.css-inspired widget library
   reskinned for CRT green phosphor aesthetic.

   Uses pseudo-element borders (no border-image)
   so it renders consistently in Safari.

   IMPORTANT: widgets must not be placed inside a
   stacking context (no z-index, transform, opacity,
   filter on ancestors) or the z-index: -1 borders
   will disappear behind the ancestor's background.
   ============================================ */

/* Self-contained font loading so any page using crt.css gets the
   correct fonts without needing style.css too. */
@font-face {
  font-family: 'PrintChar21';
  src: url('PrintChar21.ttf') format('truetype');
  font-display: swap;
}
@font-face {
  font-family: 'PRNumber3';
  src: url('PRNumber3.ttf') format('truetype');
  font-display: swap;
}
@font-face {
  font-family: 'Polymath';
  src: url('Polymath.otf') format('opentype');
  font-weight: normal;
  font-display: swap;
}
@font-face {
  font-family: 'Polymath';
  src: url('Polymath-Bold.otf') format('opentype');
  font-weight: bold;
  font-display: swap;
}

:root {
  --crt-bg:        #0a0a0a;
  --crt-fg:        #33ff33;
  --crt-fg-dim:    #1a8c1a;
  --crt-fg-bright: #66ff66;
  --crt-amber:     #ffaa00;
  --crt-amber-dim: #aa6b00;
  --crt-red:       #ff4444;
  --crt-red-dim:   #aa2828;
  --crt-cyan:      #66ffff;
  --crt-cyan-dim:  #2a8888;

  --crt-glow:        0 0 4px rgba(51, 255, 51, 0.45);
  --crt-glow-bright: 0 0 8px rgba(51, 255, 51, 0.7);
  --crt-glow-amber:  0 0 4px rgba(255, 170, 0, 0.45);
  --crt-glow-red:    0 0 4px rgba(255, 68, 68, 0.45);

  --crt-border:      4px;
}

/* Base font/color helpers */
.crt {
  font-family: 'PrintChar21', 'Courier New', monospace;
  color: var(--crt-fg);
  text-shadow: var(--crt-glow);
  -webkit-font-smoothing: none;
  font-smooth: never;
}

.crt-bright { color: var(--crt-fg-bright); text-shadow: var(--crt-glow-bright); }
.crt-dim    { color: var(--crt-fg-dim);    text-shadow: none; }
.crt-amber  { color: var(--crt-amber);     text-shadow: var(--crt-glow-amber); }
.crt-red    { color: var(--crt-red);       text-shadow: var(--crt-glow-red); }

/* ============================================
   CONTAINER
   Chunky pixel border with cut corners.
   ============================================ */
.crt-container {
  position: relative;
  margin: var(--crt-border);
  padding: 1.5rem 2rem;
  background: var(--crt-bg);
  color: var(--crt-fg);
  font-family: 'PrintChar21', 'Courier New', monospace;
  text-shadow: var(--crt-glow);
}

/* Top + bottom border bars (full width, extending above/below) */
.crt-container::before {
  content: '';
  position: absolute;
  z-index: -1;
  top: calc(var(--crt-border) * -1);
  right: 0;
  bottom: calc(var(--crt-border) * -1);
  left: 0;
  background: var(--crt-fg-dim);
}

/* Left + right border bars (full height, extending left/right) */
.crt-container::after {
  content: '';
  position: absolute;
  z-index: -1;
  top: 0;
  right: calc(var(--crt-border) * -1);
  bottom: 0;
  left: calc(var(--crt-border) * -1);
  background: var(--crt-fg-dim);
}

/* Container variants */
.crt-container.is-bright::before,
.crt-container.is-bright::after {
  background: var(--crt-fg);
  box-shadow: 0 0 12px rgba(51, 255, 51, 0.4);
}

.crt-container.is-amber::before,
.crt-container.is-amber::after {
  background: var(--crt-amber-dim);
}

.crt-container.is-red::before,
.crt-container.is-red::after {
  background: var(--crt-red-dim);
}

/* Container with title — title sits in the top border */
.crt-container.with-title {
  margin-top: 2.5rem;
}

.crt-container.with-title > .crt-title {
  display: table;
  position: relative;
  top: -2.5rem;
  margin: 0 -1rem -2rem 0.5rem;
  padding: 0 0.75rem;
  background: var(--crt-bg);
  color: var(--crt-fg);
  font-size: 1rem;
  text-shadow: var(--crt-glow);
}

.crt-container.is-bright.with-title > .crt-title {
  color: var(--crt-fg-bright);
  text-shadow: var(--crt-glow-bright);
}

/* ============================================
   BUTTON
   Same border technique, with hover/active states.
   ============================================ */
.crt-btn {
  position: relative;
  display: inline-block;
  margin: var(--crt-border);
  padding: 0.5rem 1.5rem;
  background: var(--crt-bg);
  color: var(--crt-fg);
  border: none;
  font-family: 'PrintChar21', 'Courier New', monospace;
  font-size: 1rem;
  cursor: pointer;
  text-decoration: none;
  text-align: center;
  text-shadow: var(--crt-glow);
  -webkit-font-smoothing: none;
  font-smooth: never;
}

.crt-btn::before {
  content: '';
  position: absolute;
  z-index: -1;
  top: calc(var(--crt-border) * -1);
  right: 0;
  bottom: calc(var(--crt-border) * -1);
  left: 0;
  background: var(--crt-fg-dim);
  transition: background 0.1s, box-shadow 0.1s;
}

.crt-btn::after {
  content: '';
  position: absolute;
  z-index: -1;
  top: 0;
  right: calc(var(--crt-border) * -1);
  bottom: 0;
  left: calc(var(--crt-border) * -1);
  background: var(--crt-fg-dim);
  transition: background 0.1s, box-shadow 0.1s;
}

.crt-btn:hover,
.crt-btn:focus {
  color: var(--crt-fg-bright);
  text-shadow: var(--crt-glow-bright);
  outline: none;
}

.crt-btn:hover::before,
.crt-btn:focus::before,
.crt-btn:hover::after,
.crt-btn:focus::after {
  background: var(--crt-fg);
  box-shadow: 0 0 10px rgba(51, 255, 51, 0.4);
}

.crt-btn:active {
  transform: translate(2px, 2px);
}

/* Button variants */
.crt-btn.is-primary       { color: var(--crt-fg-bright); text-shadow: var(--crt-glow-bright); }
.crt-btn.is-primary::before,
.crt-btn.is-primary::after { background: var(--crt-fg); box-shadow: 0 0 8px rgba(51, 255, 51, 0.3); }

.crt-btn.is-warning       { color: var(--crt-amber); text-shadow: var(--crt-glow-amber); }
.crt-btn.is-warning::before,
.crt-btn.is-warning::after { background: var(--crt-amber-dim); }
.crt-btn.is-warning:hover::before,
.crt-btn.is-warning:hover::after { background: var(--crt-amber); box-shadow: 0 0 10px rgba(255, 170, 0, 0.4); }

.crt-btn.is-error         { color: var(--crt-red); text-shadow: var(--crt-glow-red); }
.crt-btn.is-error::before,
.crt-btn.is-error::after   { background: var(--crt-red-dim); }
.crt-btn.is-error:hover::before,
.crt-btn.is-error:hover::after   { background: var(--crt-red); box-shadow: 0 0 10px rgba(255, 68, 68, 0.4); }

/* Passive — amber frame around normal green text. Used when a
   choice unlocks because of a passive ability check. The amber
   frame signals "this is here for a special reason." */
.crt-btn.is-passive::before,
.crt-btn.is-passive::after { background: var(--crt-amber-dim); }
.crt-btn.is-passive:hover::before,
.crt-btn.is-passive:focus::before,
.crt-btn.is-passive:hover::after,
.crt-btn.is-passive:focus::after {
  background: var(--crt-amber);
  box-shadow: 0 0 10px rgba(255, 170, 0, 0.4);
}

.crt-btn.is-disabled,
.crt-btn[disabled] {
  opacity: 0.35;
  cursor: not-allowed;
  pointer-events: none;
}

/* ============================================
   SINGLE-SELECTION BUTTON GROUP
   When any button in a group is hovered or focused,
   .is-primary siblings drop back to default state so
   only one button looks "selected" at a time.
   Uses :has() — supported in Chrome/Safari/Firefox.
   ============================================ */

/* Top-level buttons (pseudo-element borders) */
:has(> .crt-btn:is(:hover, :focus)) > .crt-btn.is-primary:not(:hover):not(:focus) {
  color: var(--crt-fg);
  text-shadow: var(--crt-glow);
}

:has(> .crt-btn:is(:hover, :focus)) > .crt-btn.is-primary:not(:hover):not(:focus)::before,
:has(> .crt-btn:is(:hover, :focus)) > .crt-btn.is-primary:not(:hover):not(:focus)::after {
  background: var(--crt-fg-dim);
  box-shadow: none;
}

/* ============================================
   DIALOG (modal panel)
   Brighter border by default — it's a focal element.
   ============================================ */
.crt-dialog {
  position: relative;
  margin: var(--crt-border);
  padding: 2rem;
  background: var(--crt-bg);
  color: var(--crt-fg);
  border: none;
  font-family: 'PrintChar21', 'Courier New', monospace;
  text-shadow: var(--crt-glow);
  min-width: 280px;
}

.crt-dialog::before,
.crt-dialog::after {
  content: '';
  position: absolute;
  z-index: -1;
  background: var(--crt-fg);
  box-shadow: 0 0 16px rgba(51, 255, 51, 0.5);
}

.crt-dialog::before {
  top: calc(var(--crt-border) * -1);
  right: 0;
  bottom: calc(var(--crt-border) * -1);
  left: 0;
}

.crt-dialog::after {
  top: 0;
  right: calc(var(--crt-border) * -1);
  bottom: 0;
  left: calc(var(--crt-border) * -1);
}

.crt-dialog .crt-title {
  display: block;
  margin-bottom: 1rem;
  text-align: center;
  color: var(--crt-fg-bright);
  text-shadow: var(--crt-glow-bright);
  font-size: 1.1rem;
}

.crt-dialog menu {
  display: flex;
  justify-content: center;
  gap: 8px;
  margin-top: 1.5rem;
  padding: 0;
  list-style: none;
}

/* ============================================
   Nested buttons — solid filled blocks.
   Buttons inside .crt-container or .crt-dialog can't use the
   pseudo-element border technique (z-index: -1 conflict — the
   parent's solid background covers the borders). Switch to filled
   inverse-video style instead. Also more obviously a button.
   ============================================ */
.crt-container .crt-btn::before,
.crt-container .crt-btn::after,
.crt-dialog .crt-btn::before,
.crt-dialog .crt-btn::after {
  display: none;
}

.crt-container .crt-btn,
.crt-dialog .crt-btn {
  background: var(--crt-fg-dim);
  color: var(--crt-bg);
  text-shadow: none;
  border: none;
  padding: 0.6rem 1.4rem;
  margin: 0;
  min-width: 100px;
  letter-spacing: 1px;
  font-family: 'PrintChar21', 'Courier New', monospace;
  font-size: 16px;
  transition: background 0.1s, box-shadow 0.1s, color 0.1s;
}

.crt-container .crt-btn:hover,
.crt-container .crt-btn:focus,
.crt-dialog .crt-btn:hover,
.crt-dialog .crt-btn:focus {
  background: var(--crt-fg);
  color: var(--crt-bg);
  box-shadow: 0 0 10px rgba(51, 255, 51, 0.45);
}

.crt-container .crt-btn.is-primary,
.crt-dialog .crt-btn.is-primary {
  background: var(--crt-fg);
  color: var(--crt-bg);
  box-shadow: 0 0 8px rgba(51, 255, 51, 0.35);
}

.crt-container .crt-btn.is-warning,
.crt-dialog .crt-btn.is-warning {
  background: var(--crt-amber-dim);
  color: var(--crt-bg);
}
.crt-container .crt-btn.is-warning:hover,
.crt-dialog .crt-btn.is-warning:hover {
  background: var(--crt-amber);
  color: var(--crt-bg);
  box-shadow: 0 0 10px rgba(255, 170, 0, 0.5);
}

.crt-container .crt-btn.is-error,
.crt-dialog .crt-btn.is-error {
  background: var(--crt-red-dim);
  color: var(--crt-bg);
}
.crt-container .crt-btn.is-error:hover,
.crt-dialog .crt-btn.is-error:hover {
  background: var(--crt-red);
  color: var(--crt-bg);
  box-shadow: 0 0 10px rgba(255, 68, 68, 0.5);
}

/* Single-selection: when any nested button is hovered/focused,
   .is-primary/.is-warning/.is-error siblings drop to default. */
.crt-dialog menu:has(.crt-btn:is(:hover, :focus)) .crt-btn.is-primary:not(:hover):not(:focus),
.crt-container:has(.crt-btn:is(:hover, :focus)) .crt-btn.is-primary:not(:hover):not(:focus) {
  background: var(--crt-fg-dim);
  box-shadow: none;
}

.crt-dialog menu:has(.crt-btn:is(:hover, :focus)) .crt-btn.is-warning:not(:hover):not(:focus),
.crt-container:has(.crt-btn:is(:hover, :focus)) .crt-btn.is-warning:not(:hover):not(:focus) {
  background: var(--crt-amber-dim);
  box-shadow: none;
}

.crt-dialog menu:has(.crt-btn:is(:hover, :focus)) .crt-btn.is-error:not(:hover):not(:focus),
.crt-container:has(.crt-btn:is(:hover, :focus)) .crt-btn.is-error:not(:hover):not(:focus) {
  background: var(--crt-red-dim);
  box-shadow: none;
}

/* ============================================
   PROGRESS BAR
   Native <progress> with chunky CRT styling.
   ============================================ */
.crt-progress {
  -webkit-appearance: none;
  -moz-appearance: none;
  appearance: none;
  width: 100%;
  height: 28px;
  margin: var(--crt-border);
  position: relative;
  border: var(--crt-border) solid var(--crt-fg-dim);
  background: var(--crt-bg);
}

.crt-progress::-webkit-progress-bar {
  background: var(--crt-bg);
  border-radius: 0;
}

/* Solid fill — fixed-width ticks would imply a unit, but bars
   represent variable scales (HP 4, HP 30, XP 1000), so smooth fill
   is the honest choice. */
.crt-progress::-webkit-progress-value {
  background-color: var(--crt-fg-dim);
  border-radius: 0;
}

.crt-progress::-moz-progress-bar {
  background: var(--crt-fg-dim);
  border-radius: 0;
}

/* Variants */
.crt-progress.is-success {
  border-color: var(--crt-fg);
  box-shadow: 0 0 8px rgba(51, 255, 51, 0.3);
}
.crt-progress.is-success::-webkit-progress-value { background-color: var(--crt-fg); }
.crt-progress.is-success::-moz-progress-bar     { background-color: var(--crt-fg); }

.crt-progress.is-warning { border-color: var(--crt-amber); }
.crt-progress.is-warning::-webkit-progress-value { background-color: var(--crt-amber); }
.crt-progress.is-warning::-moz-progress-bar     { background-color: var(--crt-amber); }

.crt-progress.is-error { border-color: var(--crt-red); }
.crt-progress.is-error::-webkit-progress-value { background-color: var(--crt-red); }
.crt-progress.is-error::-moz-progress-bar     { background-color: var(--crt-red); }

/* ============================================
   BADGE — small two-tone label
   Useful for stat tags, advantage indicators, etc.
   ============================================ */
.crt-badge {
  display: inline-flex;
  margin: 4px;
  font-family: 'PrintChar21', 'Courier New', monospace;
  /* PrintChar21 has a rendering threshold at 16px — never go below. */
  font-size: 16px;
  vertical-align: middle;
}

.crt-badge > span {
  display: inline-block;
  padding: 0.35rem 0.75rem;
  white-space: nowrap;
}

.crt-badge .label {
  background: var(--crt-fg-dim);
  color: var(--crt-bg);
}

.crt-badge .value {
  background: var(--crt-fg);
  color: var(--crt-bg);
  text-shadow: 0 0 2px rgba(51, 255, 51, 0.6);
}

.crt-badge.is-warning .label { background: var(--crt-amber-dim); }
.crt-badge.is-warning .value { background: var(--crt-amber); }
.crt-badge.is-error .label   { background: var(--crt-red-dim); }
.crt-badge.is-error .value   { background: var(--crt-red); }

/* ============================================
   MODAL — dialog floating over a game scene
   Authentic Apple II treatment: solid window
   with offset drop shadow + 50% dither backdrop.
   ============================================ */

.crt-modal-overlay {
  position: absolute;
  inset: 0;
  display: flex;
  align-items: center;
  justify-content: center;
  z-index: 100;
  /* 50% dither — Apple II didn't have alpha so they used
     stippled/checkered patterns to "dim" the background. */
  background-image:
    linear-gradient(45deg, rgba(0,0,0,0.7) 25%, transparent 25%, transparent 75%, rgba(0,0,0,0.7) 75%),
    linear-gradient(45deg, rgba(0,0,0,0.7) 25%, transparent 25%, transparent 75%, rgba(0,0,0,0.7) 75%);
  background-size: 4px 4px;
  background-position: 0 0, 2px 2px;
  /* Establish a stacking context so the dialog's ::before/::after
     borders paint above this dither, not behind it. */
  isolation: isolate;
}

/* Modal variant of dialog — adds offset drop shadow.
   IMPORTANT: must use box-shadow (not filter: drop-shadow) because
   filter creates a stacking context and would trap the pseudo-element
   borders above the dialog's background, rendering it as solid green. */
.crt-dialog.is-modal {
  /* Sharp rectangular shadow offset down-right — classic Apple IIgs
     / Mac Plus window shadow look. */
  box-shadow: 6px 6px 0 0 var(--crt-fg-dim);
}

/* Title divider — double line under the title, classic dialog convention */
.crt-divider {
  border: none;
  border-top: 4px double var(--crt-fg);
  margin: 0.5rem -0.5rem 1rem;
  opacity: 0.7;
}

.crt-dialog.is-modal .crt-divider {
  border-top-color: var(--crt-fg-bright);
  opacity: 0.85;
}

/* ============================================
   SCREEN MOCK — for previewing CRT content
   ============================================ */
.crt-screen-mock {
  position: relative;
  background: var(--crt-bg);
  padding: 24px 32px;
  font-family: 'PrintChar21', monospace;
  font-size: 16px;
  line-height: 1.4;
  color: var(--crt-fg);
  text-shadow: var(--crt-glow);
  overflow: hidden;
  min-height: 360px;
  border: 1px solid #1a1a1a;
}

.crt-screen-mock.with-scanlines::after {
  content: '';
  position: absolute;
  inset: 0;
  pointer-events: none;
  z-index: 99;
  background:
    linear-gradient(rgba(18, 16, 16, 0) 50%, rgba(0, 0, 0, 0.22) 50%),
    linear-gradient(90deg, rgba(255,0,0,0.04), rgba(0,255,0,0.015), rgba(0,0,255,0.04));
  background-size: 100% 2px, 3px 100%;
}

/* ============================================
   DICE — Polymath font renders die faces via OpenType ligatures.
   Each glyph in its own .crt-dice span so ligatures fire per-glyph.
   Supported ligatures: N_on_d4, N_on_d6, N_on_d8, N_on_d10, N_on_d12,
   N_on_d20. ALWAYS escape with separate spans, never run together.
   ============================================ */
.crt-dice-row {
  display: flex;
  gap: 6px;
  justify-content: center;
  align-items: center;
  flex-wrap: wrap;
  margin: 12px 0;
  font-family: 'Polymath', monospace;
  font-weight: bold;
  font-size: 72px;
  line-height: 1;
  /* Critical: enable OpenType ligatures or you'll see "3_on_d6" as text */
  font-feature-settings: "liga" 1, "clig" 1, "dlig" 1;
  font-variant-ligatures: common-ligatures discretionary-ligatures;
  text-rendering: optimizeLegibility;
  -webkit-font-smoothing: none;
  font-smooth: never;
}

/* Small dice — minimum readable size for Polymath. Below 56px the
   number-on-face glyph gets tiny relative to the die outline. */
.crt-dice-row.is-tiny   { font-size: 24px; }
.crt-dice-row.is-small  { font-size: 56px; }
.crt-dice-row.is-large  { font-size: 96px; }
.crt-dice-row.is-huge   { font-size: 128px; }

/* Rolling state — slight wobble so it reads as actively rolling */
.crt-dice.is-rolling {
  animation: dice-shake 0.08s infinite;
}
@keyframes dice-shake {
  0%, 100% { transform: translate(0, 0); }
  25% { transform: translate(-1px, 1px); }
  50% { transform: translate(1px, -1px); }
  75% { transform: translate(-1px, -1px); }
}

.crt-dice {
  display: inline-block;
  color: var(--crt-fg);
  text-shadow: var(--crt-glow);
}

/* Dimmed — for dice that were dropped (e.g. 4d6 drop lowest) */
.crt-dice.is-dropped { opacity: 0.25; }

/* Crit — natural max (20 on d20, 6 on d6, etc.) */
.crt-dice.is-crit {
  color: var(--crt-fg-bright);
  text-shadow: 0 0 14px rgba(102, 255, 102, 0.85),
               0 0 28px rgba(102, 255, 102, 0.4);
}

/* Fumble — natural 1 */
.crt-dice.is-fumble {
  color: var(--crt-red);
  text-shadow: var(--crt-glow-red);
}

/* Total/sum line below dice */
.crt-dice-total {
  text-align: center;
  font-family: 'PrintChar21', monospace;
  font-size: 18px;
  color: var(--crt-fg-bright);
  text-shadow: var(--crt-glow-bright);
  margin-top: 10px;
  letter-spacing: 1px;
}

.crt-dice-formula {
  text-align: center;
  font-family: 'PrintChar21', monospace;
  font-size: 16px;
  color: var(--crt-fg-dim);
  text-shadow: none;
  margin-bottom: 4px;
  letter-spacing: 1px;
}

/* ============================================
   LIST — bordered list with bullet glyphs
   ============================================ */
.crt-list {
  margin: var(--crt-border);
  padding: 0;
  list-style: none;
  font-family: 'PrintChar21', 'Courier New', monospace;
  color: var(--crt-fg);
  text-shadow: var(--crt-glow);
}

.crt-list li {
  padding: 0.25rem 0 0.25rem 1.5rem;
  position: relative;
}

.crt-list li::before {
  content: '>';
  position: absolute;
  left: 0.25rem;
  color: var(--crt-fg-dim);
  text-shadow: none;
}

.crt-list.is-numbered {
  counter-reset: crt-list;
}

.crt-list.is-numbered li {
  counter-increment: crt-list;
}

.crt-list.is-numbered li::before {
  content: counter(crt-list) ".";
  width: 1.25rem;
  text-align: right;
}

/* ============================================================
   TYPEWRITER — Apple II type-out, cursor, prompt, speaker labels.
   Driven by typewriter.js; this CSS handles the visual treatment.
   ============================================================ */

/* ── Blinking cursor — only at the ]▒ input prompt, not narrative.
   Uses visibility (not opacity) so Safari doesn't re-composite
   the parent's text-shadow on every blink frame. */
.tw-cursor {
  animation: tw-blink 0.53s step-end infinite;
}
@keyframes tw-blink {
  0%, 49% { visibility: visible; }
  50%, 100% { visibility: hidden; }
}

/* Narrative text line — no cursor, just streams on screen. */
.tw-line {
  display: block;
  white-space: pre-wrap;
  /* 6px gap between paragraphs.
     At 16px/1.2lh, this yields ~15 lines with 3 choices (no header),
     17 with LOCATION header, 16 with LOC+DESC, 14 with sprites. */
  margin-bottom: 10px;
}

/* Cutscene / prologue style: fade-in lines, left-aligned. */
.cutscene-wrap .tw-line.is-fade {
  opacity: 0;
  display: block;
  font-style: italic;
  animation: none;
}
.cutscene-wrap .tw-line.is-fade.is-visible {
  animation: crt-fade-in 2s steps(4, end) forwards;
}
@keyframes crt-fade-in {
  0%   { opacity: 0; }
  25%  { opacity: 0.2; }
  50%  { opacity: 0.5; }
  75%  { opacity: 0.8; }
  100% { opacity: 1; }
}
.cutscene-wrap .tw-line.is-dramatic {
  font-weight: bold;
  color: var(--crt-fg-bright);
  text-shadow: 0 0 6px rgba(102,255,102,0.5);
}

/* Apple II input prompt: ]▒  — only for keyboard entry (name, etc.)
   Bracket and cursor render at the same size so they line up. */
.tw-prompt {
  display: block;
  margin-top: 0.5em;
  font-size: inherit;
}
.tw-prompt-bracket {
  color: var(--crt-fg);
}
.tw-prompt-bracket,
.tw-prompt .tw-cursor,
.tw-prompt .tw-input-text {
  font-size: inherit;
  line-height: 1;
}

/* Speaker label — uses crt-badge with stick figure (🯅/🯉) + name.
   Created via Typewriter.speakerLabel(). */
.tw-line:has(.tw-speaker-badge) {
  margin-top: -4px;
}
.tw-speaker-badge {
  display: inline;
  margin-right: 0.3em;
}
.tw-speaker-badge .tw-speaker-name {
  display: inline;
  background: var(--crt-fg-dim, #1a8c1a);
  color: var(--crt-bg, #0a0a0a);
  padding: 2px 3px 1px;
  font-size: inherit;
  line-height: inherit;
}
.tw-speaker-badge .tw-inline-sprite {
  width: 36px !important;
  height: 36px !important;
  vertical-align: bottom;
  margin-right: 2px;
}

/* Text typed by the player at the ]▒ prompt. */
.tw-input-text {
  color: var(--crt-fg-bright);
  text-shadow: 0 0 6px rgba(102, 255, 102, 0.6);
}

/* Loading indicator — hourglass ⌛ centered on screen, only when
   legitimately waiting on a disk read. No blink — it just sits there
   with the disk light on until loading completes. */
.tw-loading {
  position: absolute;
  top: 50%;
  left: 50%;
  transform: translate(-50%, -50%);
  font-size: 48px;
  color: var(--crt-fg-dim);
  text-shadow: 0 0 8px rgba(51, 255, 51, 0.3);
  pointer-events: none;
}

/* ============================================
   SCROLL AREA — scrollable text region between
   header and choices. Hides native scrollbar;
   uses glyph-based scrollbar in column 47.
   ============================================ */
.scroll-area {
  flex: 1 1 auto;
  overflow-y: auto;
  overflow-x: hidden;
  position: relative;
  min-height: 0;
  padding-top: 6px;
  /* Snap to line boundaries for authentic Apple II feel */
  /* No CSS scroll-snap — we handle line-snapping in JS via wheel handler. */
  /* Hide native scrollbar across browsers */
  scrollbar-width: none; /* Firefox */
  -ms-overflow-style: none; /* IE/Edge */
}
.scroll-area::-webkit-scrollbar {
  display: none; /* Chrome/Safari */
}


/* Dither fade indicators — show when content is scrollable.
   Uses Apple II checkerboard dither pattern over the first/last line.
   Positioned via separate overlay divs injected by JS for precise control. */
.scroll-dither {
  position: absolute;
  left: 0;
  right: 0;
  height: 21px;
  pointer-events: none;
  z-index: 50;
  opacity: 0;
  background-image:
    linear-gradient(45deg, var(--crt-bg, #0a0a0a) 25%, transparent 25%, transparent 75%, var(--crt-bg, #0a0a0a) 75%),
    linear-gradient(45deg, var(--crt-bg, #0a0a0a) 25%, transparent 25%, transparent 75%, var(--crt-bg, #0a0a0a) 75%);
  background-size: 4px 4px;
  background-position: 0 0, 2px 2px;
}
.scroll-dither.top {
  top: 0;
}
.scroll-dither.bottom {
  bottom: 0;
}
.scroll-dither.visible {
  opacity: 1;
}

/* Glyph scrollbar — lives in crt-output (outside scroll-area),
   positioned in the gap between safe zone and CRT screen edge.
   Text stays at full 47 chars. */
.scroll-bar {
  position: absolute;
  z-index: 11;
  width: 1ch;
  display: flex;
  flex-direction: column;
  align-items: center;
  pointer-events: auto;
  z-index: 10;
  user-select: none;
  -webkit-user-select: none;
}

.scroll-bar-glyph {
  display: block;
  width: 1ch;
  text-align: center;
  line-height: 1.3;
  color: var(--crt-fg-dim);
  text-shadow: none;
  cursor: default;
  font-family: 'PrintChar21', 'Courier New', monospace;
  font-size: inherit;
}

.scroll-bar-glyph.scroll-arrow {
  cursor: pointer;
  color: var(--crt-fg);
}

.scroll-bar-glyph.scroll-arrow:hover {
  color: var(--crt-fg-bright);
  text-shadow: var(--crt-glow-bright);
}

.scroll-bar-glyph.scroll-thumb {
  color: var(--crt-fg);
}

.scroll-bar-glyph.scroll-track {
  cursor: pointer;
}

.scroll-bar-glyph.scroll-track:hover {
  color: var(--crt-fg);
}

/* Scrollbar is in column 48 (outside safe area) — no text shrinkage needed. */

/* ============================================
   TOUCH CONTROLS — mobile ▲ ▼ ⏎ bar
   Only visible on touch-capable devices at
   tablet/mobile breakpoints (≤1024px).
   ============================================ */
/* Wrapper holds polaroid + controls in the same space.
   Keeps polaroid's size in flow even when it animates away. */
.polaroid-wrap {
  position: relative;
  flex-shrink: 0;
  width: clamp(120px, 18vw, 200px);
  aspect-ratio: 576 / 683;
  overflow: visible;
  z-index: 2;
}
.polaroid-wrap .bio__polaroid {
  width: 100%;
}

/* Touch controls: centered over the polaroid area */
.touch-controls {
  display: none;
  position: absolute;
  top: 0;
  left: -80%;
  right: -80%;
  bottom: 0;
  justify-content: space-between;
  padding: 0 16px;
  flex-direction: row;
  justify-content: center;
  align-items: flex-start;
  gap: 16px;
  user-select: none;
  -webkit-user-select: none;
  -webkit-tap-highlight-color: transparent;
}

.touch-controls.is-visible {
  display: flex;
  animation: controls-pop 0.45s cubic-bezier(0.175, 0.885, 0.32, 1.275) both;
  animation-delay: 0.3s;
}

@keyframes controls-pop {
  0%   { transform: scale(0); opacity: 0; }
  60%  { transform: scale(1.06); opacity: 1; }
  80%  { transform: scale(0.97); }
  100% { transform: scale(1); opacity: 1; }
}

/* Game active: polaroid flies away, controls fade in to same spot */
.bio__polaroid {
  transition: none;
}
body.game-active .bio__polaroid {
  animation: polaroid-out 0.45s cubic-bezier(0.55, 0, 1, 0.45) forwards;
  pointer-events: none;
  /* Stay in flow to keep space reserved — no collapse/push */
}

.touch-controls.is-visible {
  animation: controls-in 0.4s cubic-bezier(0.34, 1.56, 0.64, 1) both;
}

@keyframes polaroid-out {
  0%   { transform: rotate(-6deg) translateX(0); opacity: 1; }
  100% { transform: rotate(12deg) translateX(250%); opacity: 0; }
}
@keyframes controls-in {
  0%   { transform: scale(0.7); opacity: 0; }
  100% { transform: scale(1); opacity: 1; }
}
@keyframes controls-out {
  0%   { transform: scale(1); opacity: 1; }
  100% { transform: scale(0.7); opacity: 0; }
}
@keyframes polaroid-in {
  0%   { transform: rotate(12deg) translateX(250%); opacity: 0; }
  50%  { opacity: 1; }
  100% { transform: rotate(-6deg) translateX(0); opacity: 1; }
}

/* D-pad: cross layout, as large as polaroid height allows */
.touch-dpad {
  display: grid;
  grid-template-columns: 44px 44px 44px;
  grid-template-rows: 44px 44px 44px;
  gap: 3px;
}

.touch-btn {
  display: flex;
  align-items: center;
  justify-content: center;
  background: #0a0a0a;
  border: 2px solid var(--crt-fg-dim);
  border-radius: 6px;
  color: var(--crt-fg);
  font-family: 'PrintChar21', 'Courier New', monospace;
  font-size: 20px;
  text-shadow: var(--crt-glow);
  cursor: pointer;
  -webkit-tap-highlight-color: transparent;
  touch-action: manipulation;
  transition: background 0.08s, border-color 0.08s;
}

.touch-btn:active {
  background: var(--crt-fg);
  color: var(--crt-bg);
  text-shadow: none;
  border-color: var(--crt-fg-bright);
}

/* D-pad positions */
.touch-btn-up    { grid-column: 2; grid-row: 1; }
.touch-btn-left  { grid-column: 1; grid-row: 2; }
.touch-btn-right { grid-column: 3; grid-row: 2; }
.touch-btn-down  { grid-column: 2; grid-row: 3; }

/* Action buttons: side by side, top-aligned with d-pad */
.touch-actions {
  display: flex;
  flex-direction: row;
  gap: 4px;
  align-self: flex-start;
}

.touch-btn-kbd {
  width: 56px;
  height: 60px;
}

.touch-btn-enter {
  width: 80px;
  height: 60px;
  font-size: 13px;
  letter-spacing: 1px;
}

.touch-btn-kbd {
  font-size: 52px;
  letter-spacing: 0;
  line-height: 1;
}

/* Hide on desktop even if JS added it */
@media (min-width: 1025px) and (pointer: fine) {
  .touch-controls { display: none !important; }
}

/* Mouse pointer override — shows the Apple II pointer (🮰) when
   hovering the CRT output area. */
.crt-output.show-pointer { cursor: none; }
.crt-output.show-pointer * { cursor: none; }

/* ============================================================
   SPRITE — Roguelike 1-Bit sheet. Uses background-image so it
   loads reliably across Safari/Chrome under file:// (mask-image
   can fail silently on local files). Tinting happens via a
   filter chain that converts white→target color.
   Set --sx (col) and --sy (row) inline. Coords in sprite-labels.json.
   ============================================================ */

.crt-sprite {
  display: inline-block;
  width: 16px;
  height: 16px;
  vertical-align: middle;
  background-image: url('sheets/sprites_1.png');
  background-repeat: no-repeat;
  background-position: calc(var(--sx, 0) * -16px) calc(var(--sy, 0) * -16px);
  /* Convert white pixels to CRT phosphor green (#33ff33).
     brightness(0) → black. invert(1) → white. Then sepia + hue-rotate
     dial the hue. The result is roughly --crt-fg with a slight glow. */
  filter:
    brightness(0) saturate(100%) invert(82%) sepia(56%) saturate(3878%)
    hue-rotate(63deg) brightness(101%) contrast(102%)
    drop-shadow(0 0 2px rgba(51, 255, 51, 0.6));
  image-rendering: pixelated;
}

.crt-sprite.is-2x {
  width: 32px; height: 32px;
  background-size: 2816px 1248px;
  background-position: calc(var(--sx, 0) * -32px) calc(var(--sy, 0) * -32px);
}
.crt-sprite.is-3x {
  width: 48px; height: 48px;
  background-size: 4224px 1872px;
  background-position: calc(var(--sx, 0) * -48px) calc(var(--sy, 0) * -48px);
}
.crt-sprite.is-4x {
  width: 64px; height: 64px;
  background-size: 5632px 2496px;
  background-position: calc(var(--sx, 0) * -64px) calc(var(--sy, 0) * -64px);
}
.crt-sprite.is-6x {
  width: 96px; height: 96px;
  background-size: 8448px 3744px;
  background-position: calc(var(--sx, 0) * -96px) calc(var(--sy, 0) * -96px);
}

/* Tints — different filter chains for each CRT palette entry. */
.crt-sprite.is-bright {
  filter:
    brightness(0) saturate(100%) invert(99%) sepia(48%) saturate(3000%)
    hue-rotate(60deg) brightness(110%) contrast(105%)
    drop-shadow(0 0 3px rgba(102, 255, 102, 0.8));
}
.crt-sprite.is-dim {
  filter:
    brightness(0) saturate(100%) invert(40%) sepia(80%) saturate(1500%)
    hue-rotate(80deg) brightness(85%) contrast(100%);
}
.crt-sprite.is-amber {
  filter:
    brightness(0) saturate(100%) invert(70%) sepia(96%) saturate(2400%)
    hue-rotate(2deg) brightness(105%) contrast(105%)
    drop-shadow(0 0 3px rgba(255, 170, 0, 0.7));
}
.crt-sprite.is-red {
  filter:
    brightness(0) saturate(100%) invert(45%) sepia(99%) saturate(4000%)
    hue-rotate(340deg) brightness(105%) contrast(105%)
    drop-shadow(0 0 3px rgba(255, 68, 68, 0.7));
}

/* Inline sprite in typewriter text — rendered as a canvas data URL
   with pure #00FF00 pixels. No CSS filter needed. */
.tw-inline-sprite {
  display: inline-block;
  vertical-align: baseline;
  image-rendering: pixelated;
  position: relative;
  top: 3px;
}

/* Sprite + label — flex helper for inventory rows, action buttons, etc. */
.crt-sprite-row {
  display: inline-flex;
  align-items: center;
  gap: 0.6em;
}

/* Portrait card — a sprite framed like a polaroid/character slot. */
.crt-portrait {
  display: inline-flex;
  flex-direction: column;
  align-items: center;
  padding: 8px 8px 6px;
  border: 2px solid var(--crt-fg-dim);
  background: var(--crt-bg);
  min-width: 80px;
}
.crt-portrait .crt-sprite { margin-bottom: 10px; }
.crt-portrait .label {
  font-family: 'PrintChar21', monospace;
  font-size: 16px;
  color: var(--crt-fg-bright);
  text-shadow: 0 0 4px rgba(51,255,51,0.5);
  letter-spacing: 0.05em;
}
.crt-portrait.is-down { border-color: var(--crt-red-dim); }
.crt-portrait.is-down .label { color: var(--crt-red); text-shadow: 0 0 4px rgba(255,68,68,0.5); }
.crt-portrait.is-warn { border-color: var(--crt-amber-dim); }
.crt-portrait.is-warn .label { color: var(--crt-amber); text-shadow: 0 0 4px rgba(255,170,0,0.5); }

/* Scene strip — landscape header for a location. Centers a row of
   sprites against a faint dotted ground line. */
.crt-scene-strip {
  display: flex;
  align-items: flex-end;
  justify-content: center;
  gap: 8px;
  padding: 16px 0 12px;
  border-bottom: 1px dashed var(--crt-fg-dim);
  margin-bottom: 12px;
}

/* ============================================
   RACE SELECTOR — full-screen overlay modal
   ============================================ */
.race-select-overlay {
  position: absolute;
  inset: 0;
  background: var(--crt-bg);
  display: flex;
  flex-direction: column;
  font-family: 'PrintChar21', 'Courier New', monospace;
  padding: 10px;
}
.race-stat-bar {
  font-size: 16px;
  padding-bottom: 8px;
  border-bottom: 1px solid var(--crt-fg-dim);
  text-align: center;
  white-space: pre;
  color: var(--crt-fg-dim);
  text-shadow: none;
  flex-shrink: 0;
}
.race-stat-bar .sb { color: var(--crt-fg-bright); text-shadow: 0 0 6px rgba(102,255,102,0.6); }
.race-info-panel {
  flex: 1;
  display: flex;
  flex-direction: column;
  justify-content: center;
  padding: 0 24px;
  min-height: 0;
}
.race-info-panel .ri-name {
  font-size: 20px;
  color: var(--crt-fg-bright);
  text-shadow: 0 0 10px rgba(51,255,51,0.8);
  margin-bottom: 10px;
}
.race-info-panel .ri-flavor {
  font-size: 16px;
  color: var(--crt-fg-dim);
  text-shadow: none;
  margin-bottom: 14px;
  line-height: 1.4;
}
.race-info-panel .ri-bonuses {
  font-size: 16px;
  color: var(--crt-fg);
  margin-bottom: 12px;
  line-height: 1.6;
}
.race-info-panel .ri-bonuses .bv { color: var(--crt-fg-bright); text-shadow: 0 0 6px rgba(102,255,102,0.6); }
.race-info-panel .ri-perks {
  font-size: 16px;
  color: var(--crt-fg);
  line-height: 1.6;
}
.race-info-panel .ri-perks .pn { color: var(--crt-fg-bright); text-shadow: 0 0 4px rgba(102,255,102,0.4); }
.race-info-panel .ri-perks .pd { color: var(--crt-fg-dim); text-shadow: none; }
.race-select-bottom {
  flex-shrink: 0;
}
.race-select-label {
  font-size: 14px;
  color: var(--crt-fg-dim);
  text-shadow: none;
  text-align: center;
  padding: 8px 0 6px;
  border-top: 1px solid var(--crt-fg-dim);
}
.race-grid {
  display: grid;
  grid-template-columns: repeat(4, 1fr);
  gap: 6px;
  padding: 0 8px 4px;
}
.race-grid .race-btn {
  text-align: center;
  padding: 5px 4px;
  color: var(--crt-fg-dim);
  cursor: pointer;
  font-family: 'PrintChar21', 'Courier New', monospace;
  font-size: 16px;
  border: 2px solid transparent;
  text-shadow: none;
}
.race-grid .race-btn.is-hover {
  color: var(--crt-fg);
  border-color: var(--crt-fg);
  text-shadow: var(--crt-glow);
}
.race-grid .race-btn.is-selected {
  background: var(--crt-fg);
  color: var(--crt-bg);
  border-color: var(--crt-fg);
  text-shadow: none;
}
