:root {
  --bg: #0b0d12;
  --bg-elev: #141821;
  --bg-panel: #1a2030;
  --fg: #e8edf5;
  --muted: #8b95a8;
  --line: #2a3344;
  --amber: #e6b325;
  --phosphor: #5ee0a0;
  --danger: #e85d4c;
  --radius: 14px;
  --font-display: "Rajdhani", "Arial Narrow", sans-serif;
  --font-body: "IBM Plex Sans", "Segoe UI", sans-serif;
  --font-mono: "IBM Plex Mono", ui-monospace, monospace;
  --accent: var(--amber);
  --sammy: #3a7bd5;
  --table: #5cb85c;
  --practice: #e67e22;
  --aipractice: #00a37a;
  --words: #16a085;
  --qsowords: #2e8b57;
  --callsigns: #2c7da0;
  --tetris: #8e44ad;
  --phonetics: #c47a3a;
  --qcodes: #3d7ea6;
  --about: #4a5568;
  --space: 8px;
  --topbar-h: 56px;
}

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

html,
body {
  margin: 0;
  min-height: 100%;
  background: var(--bg);
  color: var(--fg);
  font-family: var(--font-body);
  font-size: 16px;
  line-height: 1.5;
  -webkit-font-smoothing: antialiased;
  overflow-anchor: none;
}

body {
  min-height: 100dvh;
  background:
    radial-gradient(900px 480px at 50% -80px, rgba(230, 179, 37, 0.07), transparent 60%),
    var(--bg);
  overflow-x: hidden;
}

#app {
  min-height: 100dvh;
}

body[data-app="sammymorse"] { --accent: var(--sammy); }
body[data-app="table"] { --accent: var(--table); }
body[data-app="practice"] { --accent: var(--practice); }
body[data-app="aipractice"] { --accent: var(--aipractice); }
body[data-app="words"] { --accent: var(--words); }
body[data-app="qsowords"] { --accent: var(--qsowords); }
body[data-app="callsigns"] { --accent: var(--callsigns); }
body[data-app="tetris"] { --accent: var(--tetris); }
body[data-app="phonetics"] { --accent: var(--phonetics); }
body[data-app="qsignals"] { --accent: var(--qcodes); }
body[data-app="qcodes"] { --accent: var(--qcodes); }
body[data-app="about"] { --accent: var(--about); }

button,
[role="button"] {
  cursor: pointer;
}

button:disabled {
  cursor: not-allowed;
  opacity: 0.55;
}

h1, h2, h3 {
  font-family: var(--font-display);
  letter-spacing: 0.04em;
  line-height: 1.15;
  margin: 0;
}

a { color: var(--accent); }

.shell {
  min-height: 100dvh;
  display: flex;
  flex-direction: column;
}

.topbar {
  min-height: var(--topbar-h);
  display: grid;
  grid-template-columns: minmax(72px, 1fr) auto minmax(72px, 1fr);
  align-items: center;
  gap: 8px;
  padding: 0 12px;
  padding-top: env(safe-area-inset-top);
  border-bottom: 1px solid var(--line);
  background: rgba(11, 13, 18, 0.92);
  position: sticky;
  top: 0;
  z-index: 20;
}

.topbar h1 {
  font-size: 18px;
  font-weight: 700;
  text-align: center;
  white-space: nowrap;
}

.back {
  justify-self: start;
  display: inline-flex;
  align-items: center;
  gap: 6px;
  min-height: 44px;
  padding: 0 10px;
  border: 0;
  background: transparent;
  color: var(--muted);
  font: 500 13px/1 var(--font-body);
  border-radius: 10px;
}

.back:hover,
.back:focus-visible {
  color: var(--fg);
  background: var(--bg-elev);
  outline: none;
}

.lamp {
  justify-self: end;
  width: 44px;
  height: 44px;
  display: grid;
  place-items: center;
}

.lamp span {
  width: 10px;
  height: 10px;
  border-radius: 50%;
  background: #243018;
  box-shadow: inset 0 0 4px #000;
  display: block;
}

.lamp.on span {
  background: var(--phosphor);
  box-shadow: 0 0 10px var(--phosphor);
}

.stage {
  flex: 1;
  width: 100%;
  max-width: 720px;
  margin: 0 auto;
  padding: 20px 16px 32px;
  padding-bottom: calc(32px + env(safe-area-inset-bottom));
}

.stage.wide {
  max-width: 960px;
}

.menu {
  max-width: 560px;
  margin: 0 auto;
  padding: 28px 18px 40px;
  padding-bottom: calc(40px + env(safe-area-inset-bottom));
}

.brand {
  text-align: center;
  margin-bottom: 28px;
}

.brand .kicker {
  font-family: var(--font-mono);
  font-size: 11px;
  letter-spacing: 0.22em;
  text-transform: uppercase;
  color: var(--amber);
  margin: 0 0 8px;
}

.brand h1 {
  font-size: clamp(32px, 8vw, 48px);
  font-weight: 700;
  margin-bottom: 10px;
}

.brand p {
  margin: 0 auto;
  max-width: 42ch;
  color: var(--muted);
  font-size: 15px;
}

.rack {
  display: flex;
  flex-direction: column;
  gap: 12px;
}

.rack-item {
  display: grid;
  grid-template-columns: 8px 1fr;
  border: 1px solid var(--line);
  background: var(--bg-elev);
  border-radius: var(--radius);
  overflow: hidden;
  text-align: left;
  color: inherit;
  min-height: 88px;
  padding: 0;
  width: 100%;
  touch-action: manipulation;
  -webkit-tap-highlight-color: transparent;
  transition: transform 160ms ease, border-color 160ms ease;
}

.rack-item:hover,
.rack-item:focus-visible {
  transform: translateY(-1px);
  border-color: color-mix(in srgb, var(--tile) 55%, var(--line));
  outline: none;
}

.rack-item .stripe {
  background: var(--tile);
}

.rack-item .body {
  padding: 16px 16px 16px 18px;
  display: flex;
  flex-direction: column;
  justify-content: center;
  gap: 4px;
}

.rack-item h2 {
  font-size: 22px;
  font-weight: 700;
}

.rack-item p {
  margin: 0;
  color: var(--muted);
  font-size: 13.5px;
}

.menu-foot {
  margin-top: 28px;
  text-align: center;
  color: var(--muted);
  font-size: 12px;
  font-family: var(--font-mono);
  letter-spacing: 0.04em;
}

.thumb-btn {
  display: inline-flex;
  align-items: center;
  gap: 8px;
  margin: 0 auto 14px;
  min-height: 44px;
  padding: 0 16px;
  border: 1px solid var(--line);
  border-radius: 999px;
  background: var(--bg-elev);
  color: var(--fg);
  font: 600 14px/1 var(--font-body);
  letter-spacing: 0;
}

.thumb-btn span[aria-hidden="true"] {
  font-size: 18px;
}

.thumb-btn.on {
  border-color: var(--amber);
  background: color-mix(in srgb, var(--amber) 18%, var(--bg-elev));
}

.menu-foot [data-visits],
.about-copy [data-visits] {
  display: inline-block;
  margin-top: 6px;
  min-height: 1em;
}

.welcome {
  min-height: calc(100dvh - var(--topbar-h));
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  text-align: center;
  padding: 32px 20px;
  background: var(--accent);
  color: #fff;
}

.welcome h1 {
  font-size: clamp(40px, 10vw, 64px);
  line-height: 0.95;
}

.welcome .to {
  font-size: 22px;
  opacity: 0.85;
  margin: 6px 0 4px;
  font-family: var(--font-display);
}

.welcome .byline {
  margin-top: 22px;
  font-size: 15px;
  opacity: 0.92;
}

.welcome .prompt {
  margin-top: 18px;
  font-style: italic;
  font-size: 14px;
  opacity: 0.9;
  max-width: 28em;
  padding: 0 16px;
  line-height: 1.4;
}

.welcome .copy {
  margin-top: 36px;
  font-size: 13px;
  opacity: 0.8;
}

.btn {
  appearance: none;
  border: 0;
  border-radius: 12px;
  min-height: 44px;
  padding: 0 18px;
  font: 600 15px/1 var(--font-body);
  background: var(--accent);
  color: #0b0d12;
}

.btn.ghost {
  background: transparent;
  color: #fff;
  border: 1px solid rgba(255, 255, 255, 0.45);
}

.welcome .btn {
  margin-top: 20px;
  background: #fff;
  color: #111;
}

.welcome-back {
  position: absolute;
  top: 8px;
  left: 8px;
  color: #fff;
}

.welcome {
  position: relative;
}

.panel {
  background: var(--bg-elev);
  border: 1px solid var(--line);
  border-radius: var(--radius);
  padding: 16px;
}

label.slider-row {
  display: grid;
  gap: 6px;
  margin: 12px 0;
  color: var(--muted);
  font-size: 13px;
}

label.slider-row strong {
  color: var(--fg);
  font-weight: 600;
  font-variant-numeric: tabular-nums;
}

.repeat-row {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  margin: 4px 0 12px;
  color: var(--muted);
  font-size: 13px;
}

.repeat-row strong {
  color: var(--fg);
  font-weight: 600;
}

.check-row {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  margin: 0 0 12px;
  color: var(--muted);
  font-size: 13px;
  min-height: 44px;
}

.check-row input {
  width: 22px;
  height: 22px;
  flex-shrink: 0;
  accent-color: var(--callsigns);
  cursor: pointer;
}

.seg {
  display: inline-flex;
  flex-shrink: 0;
  border: 1px solid var(--line);
  border-radius: 10px;
  overflow: hidden;
  background: #0e121a;
}

.seg-btn {
  min-width: 44px;
  min-height: 40px;
  padding: 0;
  border: 0;
  border-right: 1px solid var(--line);
  background: transparent;
  color: var(--muted);
  font: 600 14px/1 var(--font-mono);
}

.seg-btn:last-child {
  border-right: 0;
}

.seg-btn.on,
.seg-btn[aria-pressed="true"] {
  background: var(--accent);
  color: #0b0d12;
}

.seg-btn:hover:not(.on) {
  color: var(--fg);
  background: var(--bg-panel);
}

input[type="range"] {
  -webkit-appearance: none;
  appearance: none;
  width: 100%;
  height: 6px;
  background: var(--line);
  border-radius: 99px;
  outline: none;
}

input[type="range"]::-webkit-slider-thumb {
  -webkit-appearance: none;
  width: 22px;
  height: 22px;
  border-radius: 50%;
  background: var(--accent);
  border: 2px solid var(--bg);
  cursor: pointer;
}

input[type="range"]::-moz-range-thumb {
  width: 22px;
  height: 22px;
  border-radius: 50%;
  background: var(--accent);
  border: 2px solid var(--bg);
  cursor: pointer;
}

.entry {
  width: 100%;
  min-height: 52px;
  border-radius: 12px;
  border: 1px solid var(--line);
  background: #0e121a;
  color: var(--fg);
  font: 600 22px/1 var(--font-mono);
  text-align: center;
  letter-spacing: 0.12em;
  text-transform: uppercase;
  padding: 8px 12px;
}

.entry:focus {
  outline: 2px solid color-mix(in srgb, var(--accent) 70%, transparent);
  border-color: var(--accent);
}

.status {
  min-height: 1.4em;
  margin: 10px 0 0;
  color: var(--phosphor);
  font-family: var(--font-mono);
  font-size: 13px;
  text-align: center;
}

.hint {
  margin-top: 16px;
  text-align: center;
  color: var(--muted);
  font-size: 12px;
}

.char-grid {
  display: grid;
  grid-template-columns: repeat(6, minmax(0, 1fr));
  gap: 8px;
  margin-top: 16px;
}

.char-btn {
  min-height: 48px;
  border-radius: 10px;
  border: 1px solid var(--line);
  background: #11151d;
  color: var(--fg);
  font: 700 15px/1 var(--font-display);
  letter-spacing: 0.04em;
}

.char-btn:hover:not(:disabled),
.char-btn:focus-visible {
  border-color: var(--phosphor);
  outline: none;
}

.char-btn.hot {
  background: var(--phosphor);
  color: #04140a;
  border-color: var(--phosphor);
}

.glyph-stage {
  min-height: 42vh;
  display: grid;
  place-items: center;
  text-align: center;
}

.glyph {
  font-family: var(--font-display);
  font-size: clamp(56px, 16vw, 112px);
  font-weight: 700;
  letter-spacing: 0.04em;
  word-break: break-word;
  padding: 0 8px;
}

.glyph.listening {
  font-family: var(--font-mono);
  font-size: 15px;
  font-weight: 500;
  letter-spacing: 0.28em;
  text-transform: uppercase;
  color: var(--muted);
  word-break: normal;
  white-space: nowrap;
}

.glyph.callsign:not(.listening) {
  font-family: var(--font-mono);
  font-size: clamp(28px, 8vw, 64px);
  letter-spacing: 0.08em;
}

.counter {
  color: var(--muted);
  font-family: var(--font-mono);
  font-size: 13px;
  text-align: center;
}

.flash-stage {
  border-radius: var(--radius);
  transition: background-color 160ms ease;
  min-height: 28vh;
}

.flash-stage.flash-ok {
  background: color-mix(in srgb, #1e9e4a 55%, transparent);
}

.flash-stage.flash-bad {
  background: color-mix(in srgb, #c0392b 55%, transparent);
}

.mic-status {
  text-align: center;
  color: var(--phosphor);
  font-family: var(--font-mono);
  font-size: 13px;
  letter-spacing: 0.06em;
  min-height: 1.4em;
}

.voice-row {
  display: flex;
  gap: 8px;
  justify-content: center;
  align-items: center;
  width: min(100%, 360px);
  margin: 4px auto 0;
}

.speak-btn {
  flex: 0 0 auto;
}

.guess-visible {
  flex: 1;
  min-height: 44px;
  border-radius: 12px;
  border: 1px solid var(--line);
  background: var(--bg-elev);
  color: var(--fg);
  font: 600 16px/1 var(--font-mono);
  text-align: center;
  text-transform: uppercase;
  padding: 0 12px;
}

.about-copy {
  font-size: 16px;
  color: var(--fg);
  text-align: center;
}

.ref-stage {
  max-width: 640px;
  overflow: visible;
}

.ref-sec {
  margin-top: 22px;
}

.ref-sec h2 {
  font-family: var(--font-display);
  font-size: 18px;
  font-weight: 700;
  letter-spacing: 0.06em;
  text-transform: uppercase;
  color: var(--accent);
  margin: 0 0 6px;
}

.ref-note {
  margin: 0 0 10px;
  color: var(--muted);
  font-size: 13px;
  line-height: 1.45;
}

.ref-list {
  display: flex;
  flex-direction: column;
  gap: 6px;
}

.ref-row {
  display: flex;
  gap: 12px;
  align-items: flex-start;
  width: 100%;
  text-align: left;
  padding: 10px 12px;
  border-radius: 10px;
  border: 1px solid var(--line);
  background: #11151d;
  color: var(--fg);
  min-height: 48px;
}

.ref-row:hover,
.ref-row:focus-visible {
  border-color: var(--accent);
  outline: none;
}

.ref-row.hot {
  background: color-mix(in srgb, var(--accent) 28%, #11151d);
  border-color: var(--accent);
}

.ref-ch {
  flex: 0 0 3.2em;
  font-family: var(--font-display);
  font-size: 22px;
  font-weight: 700;
  letter-spacing: 0.04em;
  line-height: 1.1;
  color: var(--phosphor);
}

.ref-copy {
  display: flex;
  flex-direction: column;
  gap: 2px;
  min-width: 0;
}

.ref-word {
  font-weight: 600;
  font-size: 15px;
}

.ref-desc {
  color: var(--muted);
  font-size: 13px;
  line-height: 1.4;
}

.about-copy p {
  margin: 0 0 14px;
}

.about-copy .meta {
  color: var(--muted);
  margin-top: 24px;
}

.about-word {
  display: inline-block;
  margin-top: 8px;
  padding: 10px 16px;
  border-radius: 10px;
  border: 1px solid color-mix(in srgb, var(--accent) 50%, #000);
  background: color-mix(in srgb, var(--accent) 18%, #141821);
  color: var(--fg);
  font: 600 16px/1.2 var(--font-display);
  letter-spacing: 0.03em;
}

.tetris-wrap {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 10px;
}

.hud {
  display: flex;
  flex-wrap: wrap;
  justify-content: center;
  gap: 14px;
  font-family: var(--font-mono);
  font-size: 13px;
  color: #f2e9ff;
}

.hud span strong {
  color: #fff;
  font-weight: 600;
}

.board-frame {
  width: min(100%, 360px);
  aspect-ratio: 10 / 20;
  background: #1a1a1a;
  border: 2px solid color-mix(in srgb, var(--tetris) 55%, #000);
  border-radius: 8px;
  overflow: hidden;
  touch-action: none;
}

.board-frame canvas {
  display: block;
  width: 100%;
  height: 100%;
}

.typed {
  min-height: 28px;
  font-family: var(--font-mono);
  font-size: 20px;
  letter-spacing: 0.2em;
  color: #fff;
}

.instr {
  color: #cbb6de;
  font-size: 12px;
  text-align: center;
  max-width: 42ch;
  line-height: 1.45;
}

.touch-controls {
  display: none;
  width: min(100%, 420px);
}

.replay-btn {
  display: none;
  width: 100%;
  min-height: 40px;
  margin-bottom: 6px;
  border-radius: 10px;
  border: 1px solid rgba(255, 255, 255, 0.14);
  background: rgba(0, 0, 0, 0.28);
  color: #fff;
  font: 600 14px/1 var(--font-body);
  touch-action: manipulation;
}

.kb {
  display: none;
  width: 100%;
  gap: 4px;
}

.kb-row {
  display: grid;
  gap: 4px;
  margin-bottom: 4px;
}

.kb-row.n10 { grid-template-columns: repeat(10, minmax(0, 1fr)); }
.kb-row.n9 { grid-template-columns: repeat(9, minmax(0, 1fr)); max-width: 90%; margin-left: auto; margin-right: auto; }
.kb-row.n7 { grid-template-columns: repeat(7, minmax(0, 1fr)); max-width: 78%; margin-left: auto; margin-right: auto; }
.kb-row.n4 { grid-template-columns: repeat(4, minmax(0, 1fr)); max-width: 56%; margin-left: auto; margin-right: auto; }

.kb-key {
  min-height: 40px;
  border-radius: 7px;
  border: 1px solid rgba(255, 255, 255, 0.14);
  background: rgba(0, 0, 0, 0.34);
  color: #fff;
  font: 600 13px/1 var(--font-mono);
  touch-action: manipulation;
}

.kb-key:active {
  background: var(--accent);
  color: #0b0d12;
}

.move-row {
  display: grid;
  grid-template-columns: repeat(6, minmax(0, 1fr));
  gap: 6px;
}

.move-row button {
  min-height: 44px;
  border-radius: 10px;
  border: 1px solid rgba(255, 255, 255, 0.14);
  background: rgba(0, 0, 0, 0.28);
  color: #fff;
  font: 600 13px/1 var(--font-body);
  touch-action: manipulation;
}

body[data-app="tetris"] {
  height: 100dvh;
  height: 100svh;
  overflow: hidden;
  overscroll-behavior: none;
}

body[data-app="tetris"] #app,
body[data-app="tetris"] .shell {
  height: 100%;
  min-height: 0;
  overflow: hidden;
}

body[data-app="tetris"] .stage {
  flex: 1;
  min-height: 0;
  display: flex;
  flex-direction: column;
  overflow: hidden;
}

body[data-app="tetris"] .tetris-wrap {
  flex: 1;
  min-height: 0;
  width: 100%;
}

@media (pointer: coarse), (max-width: 720px) {
  .touch-controls {
    display: block;
    flex: 0 0 auto;
  }
  .kb {
    display: block;
  }
  .instr {
    display: none;
  }
  .move-row {
    display: none;
  }
  .replay-btn {
    display: block;
  }
  body[data-app="tetris"] .stage {
    padding: 6px 10px calc(6px + env(safe-area-inset-bottom, 0px));
  }
  body[data-app="tetris"] .tetris-wrap {
    gap: 4px;
  }
  body[data-app="tetris"] .board-frame {
    width: auto;
    max-width: 100%;
    max-height: none;
    flex: 0 0 auto;
  }
  body[data-app="tetris"] .hud {
    font-size: 12px;
    gap: 10px;
    flex: 0 0 auto;
  }
  .move-row {
    margin-bottom: 4px;
  }
  .kb-key {
    min-height: 34px;
    font-size: 12px;
  }
  .move-row button {
    min-height: 40px;
  }
}

@media (max-height: 700px) {
  .kb-key {
    min-height: 30px;
  }
  .move-row button {
    min-height: 36px;
  }
}

.guess-input {
  position: absolute;
  opacity: 0;
  pointer-events: none;
  width: 1px;
  height: 1px;
}

body[data-app="tetris"],
body[data-app="sammymorse"],
body[data-app="table"] {
  background: color-mix(in srgb, var(--accent) 18%, var(--bg));
}

body[data-app="tetris"] .stage {
  max-width: 520px;
}

@media (max-width: 480px) {
  .char-grid {
    grid-template-columns: repeat(5, minmax(0, 1fr));
    gap: 6px;
  }
  .char-btn {
    min-height: 44px;
    font-size: 13px;
  }
  .topbar h1 {
    font-size: 16px;
  }
  .repeat-row {
    flex-wrap: wrap;
  }
}

@media (prefers-reduced-motion: reduce) {
  .rack-item {
    transition: none;
  }
}
