:root {
  --bg: #f3efe6;          /* warm paper */
  --surface: #fffdf7;
  --ink: #17130d;         /* near-black, warm */
  --muted: #6d6456;
  --line: #17130d;        /* hard borders use ink */
  --soft: #ddd5c5;
  --accent: #d8401f;      /* klaxon vermilion */
  --accent-deep: #b3300f;
  --ok: #2f7d4f;
  --warn: #9a6400;
  --bad: #c0271f;
  --display: "Space Grotesk", "Segoe UI", system-ui, sans-serif;
  --sans: "IBM Plex Sans", "Segoe UI", system-ui, sans-serif;
  --mono: "IBM Plex Mono", ui-monospace, "SFMono-Regular", Menlo, monospace;
  --radius: 4px;
  --shadow: 5px 5px 0 var(--ink);
}

* { box-sizing: border-box; }
html, body { margin: 0; padding: 0; }
body {
  font-family: var(--sans); font-size: 16px; line-height: 1.5;
  background: var(--bg); color: var(--ink);
  -webkit-tap-highlight-color: transparent;
}
h1, h2, h3 { font-family: var(--display); font-weight: 700; margin: 0 0 .4em; letter-spacing: -.01em; }
h2 { font-size: 1.15rem; text-transform: uppercase; letter-spacing: .04em; }
h3 { font-size: .8rem; text-transform: uppercase; letter-spacing: .08em; color: var(--muted); }

button {
  font-family: var(--display); font-weight: 700; font-size: .95rem; cursor: pointer;
  border: 2px solid var(--ink); border-radius: var(--radius);
  background: var(--surface); color: var(--ink); padding: .6em 1.1em;
  box-shadow: 3px 3px 0 var(--ink); transition: transform .05s, box-shadow .05s, background .12s;
}
button:hover { background: #fff; }
button:active { transform: translate(3px, 3px); box-shadow: 0 0 0 var(--ink); }
button:disabled { opacity: .45; cursor: not-allowed; box-shadow: 3px 3px 0 var(--soft); border-color: var(--soft); }
button.primary { background: var(--accent); color: #fff; border-color: var(--ink); }
button.primary:hover { background: var(--accent-deep); }
button.ghost { box-shadow: none; background: transparent; }
button.ghost:hover { background: rgba(0,0,0,.04); }
button.tiny { font-size: .75rem; padding: .3em .6em; box-shadow: 2px 2px 0 var(--ink); }
button.link-btn { border: 0; box-shadow: none; background: none; color: var(--accent); padding: 0; font-family: var(--sans); font-weight: 600; text-decoration: underline; }

input, select {
  font-family: var(--sans); font-size: 1rem; width: 100%;
  padding: .6em .7em; border: 2px solid var(--ink); border-radius: var(--radius);
  background: var(--surface); color: var(--ink);
}
input:focus, select:focus { outline: 3px solid var(--accent); outline-offset: 0; }
kbd { font-family: var(--mono); font-size: .85em; background: var(--surface); border: 1px solid var(--ink); border-radius: 3px; padding: 0 .35em; }
a { color: var(--accent); text-underline-offset: 2px; }
.hidden { display: none !important; }

/* ---------- landing ---------- */
.landing { display: grid; place-items: center; min-height: 100dvh; padding: 24px 16px; }
.card {
  background: var(--surface); border: 2px solid var(--ink); border-radius: var(--radius);
  box-shadow: var(--shadow); padding: 28px; width: 100%; max-width: 460px;
}
.wordmark {
  font-size: 2.6rem; letter-spacing: -.03em; margin: 0; line-height: 1;
  border-bottom: 4px solid var(--accent); display: inline-block; padding-bottom: 2px;
}
.tag { color: var(--muted); margin: .6em 0 1.4em; font-size: 1.02rem; }
.card section { margin: 20px 0; }
.row { display: flex; gap: 8px; flex-wrap: wrap; }
.row > input { flex: 1; min-width: 0; }
.stack { display: grid; gap: 8px; }
.hint { color: var(--muted); font-size: .85rem; }
details summary { cursor: pointer; color: var(--accent); margin: 12px 0; font-weight: 600; }
.msg { min-height: 1.2em; font-size: .9rem; }
.msg.good { color: var(--ok); }
.msg.bad { color: var(--bad); }
#join-code { text-transform: uppercase; letter-spacing: .15em; font-family: var(--mono); font-weight: 600; }

.site-foot { margin-top: 18px; text-align: center; }
.site-foot a { color: var(--muted); font-size: .8rem; text-decoration: none; }
.site-foot a:hover { color: var(--ink); text-decoration: underline; }

.card.prose { max-width: 640px; }
.prose p { margin: .8em 0; }
.prose .links { list-style: none; padding: 0; margin: 1em 0; display: grid; gap: 6px; }
.prose .links a { font-family: var(--mono); }
.prose .back { font-weight: 600; text-decoration: none; }

/* ---------- room ---------- */
.bar {
  display: flex; justify-content: space-between; align-items: center; gap: 10px;
  padding: 10px 14px; background: var(--surface); border-bottom: 2px solid var(--ink);
  position: sticky; top: 0; z-index: 5; padding-top: max(10px, env(safe-area-inset-top));
}
.bar-left { display: flex; align-items: baseline; gap: 8px; flex-wrap: wrap; }
.crumbs .crumb { color: var(--muted); text-decoration: none; font-size: .85rem; }
.crumbs .crumb:hover { color: var(--ink); text-decoration: underline; }
.crumb-mark { font-family: var(--display); font-weight: 700; color: var(--ink) !important; font-size: 1rem; letter-spacing: -.01em; }
.crumb-sep { color: var(--soft); }
.room-code { font-family: var(--mono); font-weight: 600; letter-spacing: .12em; color: var(--accent); }
.room-name { color: var(--muted); font-size: .9rem; }
.bar-right { display: flex; gap: 8px; align-items: center; }
.pill { font-family: var(--mono); font-size: .72rem; padding: .2em .55em; border: 1.5px solid var(--ink); border-radius: var(--radius); background: var(--surface); color: var(--muted); }
.pill.online { color: var(--ok); }
.pill.offline { color: var(--bad); border-color: var(--bad); }
.pill.sync { color: var(--warn); }
.pill-btn { cursor: pointer; box-shadow: none; font-weight: 600; }
.pill-btn:hover { background: #fff; color: var(--ink); }
.pill-btn.active { background: var(--accent); color: #fff; border-color: var(--ink); }

.gate { display: grid; place-items: center; min-height: 80dvh; padding: 16px; }
.gate input { margin-bottom: 10px; }

.stage {
  max-width: 680px; margin: 0 auto; display: grid; gap: 20px;
  /* Clear the iOS home indicator / gesture bar at the bottom on phones. */
  padding: 18px 16px max(18px, env(safe-area-inset-bottom));
}

.phase {
  font-family: var(--display); text-align: center; font-weight: 700; font-size: 1.05rem;
  text-transform: uppercase; letter-spacing: .06em; color: var(--muted); min-height: 1.4em;
}
.phase.ready { color: var(--muted); }
.phase.buzzed { color: var(--accent); }

/* The buzzer: gray/paper when "Ready to Buzz", red when someone has buzzed. */
.buzzer {
  display: block; width: min(68vw, 290px); aspect-ratio: 1; margin: 10px auto;
  border-radius: 50%; border: 3px solid var(--ink); box-shadow: 8px 8px 0 var(--ink);
  font-family: var(--display); font-weight: 700; font-size: clamp(1.4rem, 7vw, 2.4rem);
  letter-spacing: .04em; text-transform: uppercase;
  transition: transform .05s, box-shadow .05s, background .12s, color .12s;
  user-select: none; touch-action: manipulation;
  -webkit-user-select: none; -webkit-touch-callout: none; -webkit-tap-highlight-color: transparent;
}
.buzzer.ready { background: var(--surface); color: var(--ink); }
.buzzer.buzzed { background: var(--accent); color: #fff; }
.buzzer:not(:disabled) { cursor: pointer; }
.buzzer:disabled { opacity: .5; box-shadow: 8px 8px 0 var(--soft); border-color: #847b6b; }
.buzzer:not(:disabled):active,
.buzzer.pressed:not(:disabled) { transform: translate(8px, 8px); box-shadow: 0 0 0 var(--ink); }
.feedback { font-family: var(--display); text-align: center; font-weight: 500; min-height: 1.4em; color: var(--accent-deep); }

.badge { font-family: var(--mono); font-size: .68rem; padding: .12em .5em; border: 1.5px solid var(--ink); border-radius: var(--radius); text-transform: uppercase; }

/* panels / cards inside the room */
.panel, .share, .order, .tournament {
  background: var(--surface); border: 2px solid var(--ink); border-radius: var(--radius);
  box-shadow: var(--shadow); padding: 14px 16px;
}
.toggle { display: flex; align-items: center; gap: 8px; padding: 6px 0; cursor: pointer; font-weight: 500; }
.toggle input { width: auto; }
.opt-defaults { border: 2px solid var(--soft); border-radius: var(--radius); padding: 4px 12px 8px; margin: 0; }
.opt-defaults legend { font-family: var(--mono); font-size: .72rem; text-transform: uppercase; letter-spacing: .06em; color: var(--muted); padding: 0 6px; }
.sound-row { display: flex; gap: 10px; align-items: center; flex-wrap: wrap; }
.sound-row select { width: auto; flex: 1; min-width: 140px; }
.vol { display: flex; align-items: center; gap: 6px; color: var(--muted); font-size: .85rem; }
.vol input[type="range"] { width: 120px; }
/* Keep the checkbox a normal native size — otherwise it inherits the 120px/full
   width and renders as a stretched box that reflows when toggled. */
.vol input[type="checkbox"] { width: auto; }

.sound-warn {
  background: #fbedd6; color: var(--warn); border: 2px solid var(--ink); box-shadow: var(--shadow);
  border-radius: var(--radius); padding: 10px 12px; text-align: center; font-size: .9rem; font-weight: 500;
  display: flex; gap: 10px; align-items: center; justify-content: center; flex-wrap: wrap;
}

.order ol { padding-left: 1.4em; margin: .4em 0; }
.order li { padding: 2px 0; }
#queue-list .head { font-weight: 700; color: var(--accent); }

.muted-count { color: var(--muted); font-weight: 400; font-family: var(--mono); font-size: .8rem; }
.players { list-style: none; margin: 0; padding: 0; }
.players li { display: flex; align-items: center; justify-content: space-between; gap: 8px; padding: 6px 0; border-bottom: 1px solid var(--soft); }
.players li:last-child { border-bottom: 0; }
/* Disconnected player: make it LOUD, not dim. The reader must notice instantly. */
.players li.gone {
  background: #fbe6e3;
  border-left: 4px solid var(--bad);
  margin-left: -4px; padding-left: 8px;
}
.players li.gone .pname { color: var(--bad); }
.players .empty { color: var(--muted); justify-content: flex-start; }
.pname { font-weight: 500; }
.pcol { display: flex; flex-direction: column; gap: 1px; min-width: 0; }
.pname-wrap { display: inline-flex; align-items: center; gap: 8px; min-width: 0; }
.pjoined { font-size: .72rem; color: var(--muted); font-family: var(--mono); }

.offline-badge {
  flex: 0 0 auto;
  font-family: var(--display); font-weight: 700; font-size: .65rem; letter-spacing: .08em;
  text-transform: uppercase; color: #fff; background: var(--bad);
  border-radius: 3px; padding: 2px 6px; white-space: nowrap;
  animation: offline-pulse 1.1s ease-in-out infinite;
}
@keyframes offline-pulse {
  0%, 100% { opacity: 1; }
  50% { opacity: .45; }
}

/* Panel-level banner so a drop is obvious even if the list is scrolled. */
.offline-alert {
  margin: 6px 0 8px;
  font-family: var(--display); font-weight: 700; font-size: .8rem; letter-spacing: .03em;
  color: #fff; background: var(--bad); border: 2px solid var(--ink);
  border-radius: var(--radius); box-shadow: 3px 3px 0 var(--ink);
  padding: 6px 10px; text-align: center;
}

@media (prefers-reduced-motion: reduce) {
  .offline-badge { animation: none; }
}

/* ---------- compact view (staff, tiny window) ----------
   Strips everything but the buzz state, the buzzer/reset and the buzz order so
   a reader can dock Klaxon in a narrow column next to Zoom and the question doc. */
body.compact .panel,
body.compact .share,
body.compact #tournament-strip,
body.compact #sound-warn,
body.compact #player-hint,
body.compact #reader-hint,
body.compact #latency,
body.compact .room-name,
body.compact .crumbs .crumb,
body.compact .crumb-sep { display: none !important; }

body.compact .bar { padding: 4px 8px; padding-top: max(4px, env(safe-area-inset-top)); }
body.compact .stage { max-width: 100%; padding: 8px; gap: 8px; }
body.compact .buzzer {
  width: min(46vw, 132px); margin: 4px auto; border-width: 2px;
  box-shadow: 5px 5px 0 var(--ink); font-size: clamp(.85rem, 5vw, 1.15rem);
}
body.compact .buzzer:not(:disabled):active,
body.compact .buzzer.pressed:not(:disabled) { transform: translate(5px, 5px); box-shadow: 0 0 0 var(--ink); }
body.compact .phase { font-size: .95rem; min-height: auto; }
body.compact .feedback { min-height: auto; }
body.compact .order { padding: 10px 12px; box-shadow: 3px 3px 0 var(--ink); }
body.compact .order h3 { margin-bottom: .3em; }

button.active { background: var(--accent); color: #fff; }

/* tournament director console */
.rooms-list { display: grid; gap: 14px; }
.room-item { border: 2px solid var(--ink); border-radius: var(--radius); padding: 12px; background: var(--bg); }
.room-item-head { display: flex; gap: 10px; align-items: baseline; margin-bottom: 8px; }
.link-row { align-items: center; margin-bottom: 6px; }
.link-label { font-family: var(--mono); font-size: .72rem; text-transform: uppercase; color: var(--muted); width: 72px; flex: 0 0 72px; }
.link-row input { font-family: var(--mono); font-size: .78rem; }
.btnlink { display: inline-flex; align-items: center; text-decoration: none; color: var(--ink); border: 2px solid var(--ink); border-radius: var(--radius); padding: .3em .6em; box-shadow: 2px 2px 0 var(--ink); font-family: var(--display); font-weight: 700; font-size: .75rem; }
.btnlink:active { transform: translate(2px,2px); box-shadow: 0 0 0 var(--ink); }
#room-qty { width: 80px; flex: 0 0 80px; }
.bulk-btns { display: flex; gap: 8px; flex-wrap: wrap; align-items: center; margin-bottom: 10px; }
.tsv-preview { width: 100%; font-family: var(--mono); font-size: .72rem; white-space: pre; overflow: auto; resize: vertical; color: var(--muted); }

.share-row { display: grid; gap: 4px; margin-bottom: 10px; font-size: .85rem; color: var(--muted); }
.copy-field { display: flex; gap: 6px; }
.copy-field input { font-family: var(--mono); font-size: .8rem; }
.copy-btn { flex: 0 0 auto; }

.tournament .t-rooms { display: flex; flex-wrap: wrap; gap: 6px; margin-top: 8px; }
.chip { font-family: var(--mono); padding: .3em .7em; border: 1.5px solid var(--ink); border-radius: var(--radius); text-decoration: none; color: var(--ink); }
.chip.here { background: var(--accent); color: #fff; }
.jump { display: inline-block; padding: .5em .9em; background: var(--accent); color: #fff; border: 2px solid var(--ink); box-shadow: 3px 3px 0 var(--ink); border-radius: var(--radius); text-decoration: none; font-weight: 700; font-family: var(--display); }

@media (max-width: 480px) {
  .stage { padding: 14px 12px max(14px, env(safe-area-inset-bottom)); gap: 16px; }
  .card { padding: 22px; }
  /* Join form: let the button drop to its own full-width row instead of being
     squeezed next to two inputs on a narrow screen. */
  #join-form button { flex: 1 0 100%; }
}

/* Touch devices: ensure the smaller controls are still an easy, reliable tap. */
@media (pointer: coarse) {
  button.tiny { min-height: 40px; }
  .toggle { padding: 10px 0; }
  .copy-field input, .link-row input { font-size: 16px; } /* avoid iOS focus-zoom */
}
