:root {
  color-scheme: dark;
  font: 100%/1.5 system-ui, sans-serif;
  --page: #131722;
  --surface: #202635;
  --line: #3b465a;
  --text: #f8f4ec;
  --muted: #bcc7da;
  --accent: #fdc768;
  --green: #bdf5af;
}

* { box-sizing: border-box; }
[hidden] { display: none !important; }
body { margin: 0; background: var(--page); color: var(--text); }
main { width: min(100% - 2rem, 76rem); margin: 0 auto; padding: 2.5rem 0; }
.masthead, .track-heading, .controls, .room-subheading {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 0.85rem;
}
.masthead { align-items: end; margin-bottom: 1.6rem; }
h1, h2, h3, p { margin: 0; }
h1 {
  font-size: clamp(2.75rem, 7vw, 5.5rem);
  font-weight: 900;
  letter-spacing: -0.075em;
  line-height: 0.95;
}
h1 span { color: var(--accent); }
h2 { font-size: 1.25rem; }
h3 { font-size: 1rem; }
#role { margin-top: 0.65rem; color: var(--muted); }
.connection {
  border: 1px solid var(--line);
  border-radius: 100px;
  padding: 0.35rem 0.75rem;
  color: var(--muted);
  text-align: center;
  font-size: 0.875rem;
}
.connection.online { color: var(--green); }
.layout { display: grid; grid-template-columns: minmax(0, 1fr) minmax(16rem, 19rem); gap: 1rem; align-items: start; }
.play, .room, .scoreboard { border: 1px solid var(--line); border-radius: 1rem; background: var(--surface); }
.play { overflow: hidden; }
.track-heading, .controls { padding: 0.85rem 1rem; }
#phase, #distance { font: 700 0.98rem/1.4 ui-monospace, monospace; }
#distance { color: var(--accent); white-space: nowrap; }
.stage { position: relative; background: #a85748; }
canvas { display: block; width: 100%; height: auto; aspect-ratio: 3 / 1; touch-action: manipulation; }
.overlay {
  position: absolute;
  top: 9%;
  left: 44%;
  min-width: 1.25em;
  text-align: center;
  font-weight: 900;
  font-size: clamp(2rem, 10vw, 6rem);
  line-height: 1.1;
  color: #fff3c9;
  text-shadow: 0 3px #582c41, 0 5px 15px #462e43;
  pointer-events: none;
}
.controls p { color: var(--muted); }
.room, .scoreboard { padding: 1rem; }
.room h2 { margin-bottom: 0.8rem; }
.room-subheading { margin: 1rem 0 0.4rem; }
#count { color: var(--muted); font: 0.9rem ui-monospace, monospace; }
form { display: grid; gap: 0.45rem; }
.online-entry { display: grid; gap: 0.35rem; margin-bottom: 0.65rem; }
#join-code { letter-spacing: 0.12em; text-transform: uppercase; }
input, button {
  min-height: 2.85rem;
  border-radius: 0.55rem;
  padding: 0.45rem 0.75rem;
  font: inherit;
}
input { width: 100%; background: #151922; border: 1px solid #68758d; color: var(--text); }
button { border: 1px solid transparent; background: var(--accent); color: #261e18; font-weight: 750; cursor: pointer; touch-action: manipulation; }
button:not(:disabled):hover { background: #ffda94; }
button:disabled { background: #373f51; color: #c1cbdf; cursor: default; }
:focus-visible { outline: 3px solid #bdf5af; outline-offset: 3px; }
.player-actions { display: flex; gap: 0.5rem; }
.player-actions button { min-width: 6rem; }
#duck:not(:disabled) { background: #46556e; color: var(--text); border-color: #64758f; }
#duck:not(:disabled):hover { background: #566885; }
#teacher-controls { display: grid; gap: 0.45rem; margin-top: 0.7rem; }
.secondary-action:not(:disabled) { background: #343f53; color: var(--text); border-color: #64758f; }
.secondary-action:not(:disabled):hover { background: #46556e; }
#spectator-admin { position: fixed; z-index: 2; right: 0.65rem; bottom: 0.65rem; }
#spectator-admin summary {
  cursor: pointer;
  padding: 0.4rem 0.7rem;
  border: 1px solid #64758f;
  border-radius: 0.45rem;
  background: var(--surface);
}
#spectator-admin div { display: grid; gap: 0.4rem; margin-top: 0.45rem; padding: 0.45rem; background: var(--page); }
#notice { margin-top: 0.55rem; color: #ffd4a5; overflow-wrap: anywhere; }
.players { list-style: none; margin: 0; padding: 0; max-height: 25rem; overflow-y: auto; }
.players li { display: flex; gap: 0.45rem 0.7rem; flex-wrap: wrap; justify-content: space-between; padding: 0.32rem 0; border-top: 1px solid var(--line); }
.players span:first-child { overflow-wrap: anywhere; }
.players span:last-child { color: var(--muted); font-size: 0.88rem; }
.players .own-name { color: var(--green); font-weight: 700; }
.teacher-tracks { border-top: 1px solid var(--line); padding: 0.85rem 1rem 1rem; }
.teacher-tracks .room-subheading { margin: 0 0 0.7rem; }
#teacher-track-count { color: var(--muted); }
.teacher-track-list {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(min(100%, 11rem), 1fr));
  gap: 0.55rem;
  list-style: none;
  margin: 0;
  padding: 0;
}
.teacher-track-list li { min-width: 0; overflow: hidden; border: 1px solid var(--line); border-radius: 0.6rem; }
.teacher-track-label { display: grid; gap: 0.05rem; padding: 0.3rem 0.55rem; background: #19202d; }
.teacher-track-heading { display: flex; align-items: baseline; gap: 0.45rem; min-width: 0; }
.teacher-track-label strong { overflow-wrap: anywhere; }
.teacher-track-label span { color: var(--muted); font-size: 0.875rem; }
.teacher-track-label .teacher-track-score { color: var(--accent); white-space: nowrap; font-variant-numeric: tabular-nums; }
.teacher-track-score:empty { display: none; }
.teacher-track-list canvas { touch-action: auto; }
.spectator-view main { width: 100%; padding: 8px; }
.spectator-view .layout { display: block; }
.spectator-view .masthead,
.spectator-view .room,
.spectator-view .track-heading,
.spectator-view .teacher-tracks .room-subheading { display: none; }
.spectator-view .play { border: 0; border-radius: 0; background: none; }
.spectator-view .teacher-tracks { border: 0; padding: 0; }
.spectator-view .teacher-track-list {
  grid-template-columns: repeat(var(--spectator-columns, 1), minmax(0, 1fr));
  width: var(--spectator-width, 100%);
  max-width: 100%;
  margin-inline: auto;
  gap: 8px;
}
.spectator-view .teacher-track-label { display: flex; justify-content: space-between; align-items: center; gap: 0.45rem; }
.spectator-view .teacher-track-label strong { overflow: hidden; white-space: nowrap; text-overflow: ellipsis; }
.spectator-view .teacher-track-label span { flex-shrink: 0; }
#spectator-countdown {
  position: fixed;
  z-index: 1;
  inset: 50% auto auto 50%;
  transform: translate(-50%, -50%);
  padding: 0.45em 0.7em;
  border-radius: 0.2em;
  background: #131722ee;
  color: var(--accent);
  font-size: clamp(2rem, 9vw, 5rem);
  font-weight: 900;
  pointer-events: none;
}
.scoreboard { margin-top: 1rem; }
#result-summary { margin: 0.3rem 0 0.65rem; color: var(--green); overflow-wrap: anywhere; }
table { width: 100%; border-collapse: collapse; text-align: left; }
th, td { border-bottom: 1px solid var(--line); padding: 0.45rem; overflow-wrap: anywhere; }
th { color: var(--muted); }
tbody tr:last-child td { border-bottom: none; }

@media (max-width: 52rem) {
  .layout { grid-template-columns: 1fr; }
  .room { order: 1; }
  main { padding-top: 1.25rem; }
}

@media (max-width: 34rem) {
  main { width: calc(100% - 1rem); }
  .masthead { margin-bottom: 0.9rem; }
  .track-heading, .controls, .room, .teacher-tracks, .scoreboard { padding: 0.65rem; }
  .controls { flex-wrap: wrap; }
  .player-actions { width: 100%; }
  .player-actions button { flex: 1; min-height: 3.5rem; }
  table { font-size: 0.875rem; }
  th, td { padding: 0.3rem 0.2rem; }
}
