:root {
  --bg: #14161a;
  --bg-elevated: #1c1f25;
  --text: #e7e9ec;
  --text-muted: #9aa1ab;
  --accent: #7dd3fc;
  --border: #2a2e37;
  --danger: #f87171;
  --max-width: 960px;
}

* {
  box-sizing: border-box;
}

/* Author-stylesheet display rules (e.g. .btn's display: inline-block)
   otherwise win over the UA stylesheet's [hidden] { display: none },
   since origin beats specificity in the cascade -- without this, toggling
   an element's `hidden` attribute stops actually hiding it the moment any
   class also sets `display`. */
[hidden] {
  display: none !important;
}

body {
  margin: 0;
  font-family: system-ui, -apple-system, "Segoe UI", sans-serif;
  background: var(--bg);
  color: var(--text);
  line-height: 1.5;
}

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

header {
  border-bottom: 1px solid var(--border);
}

nav {
  max-width: var(--max-width);
  margin: 0 auto;
  padding: 1rem 1.5rem;
  display: flex;
  align-items: center;
  gap: 1.5rem;
}

nav .brand {
  font-weight: 700;
  color: var(--text);
  text-decoration: none;
  font-size: 1.1rem;
}

nav .links {
  display: flex;
  gap: 1.25rem;
  margin-left: auto;
}

nav .links a {
  color: var(--text-muted);
  text-decoration: none;
}

nav .links a:hover,
nav .links a[aria-current="page"] {
  color: var(--text);
}

main {
  max-width: var(--max-width);
  margin: 0 auto;
  padding: 3rem 1.5rem;
  min-height: 60vh;
}

main h1 {
  margin-top: 0;
}

.card-grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(220px, 1fr));
  gap: 1rem;
  margin-top: 2rem;
}

.card {
  display: block;
  background: var(--bg-elevated);
  border: 1px solid var(--border);
  border-radius: 8px;
  padding: 1.25rem;
  color: inherit;
  text-decoration: none;
  transition: border-color 0.15s ease;
}

a.card:hover {
  border-color: var(--accent);
}

.card h2 {
  margin-top: 0;
  font-size: 1.1rem;
}

.card p {
  color: var(--text-muted);
  margin-bottom: 0;
}

.score-block {
  margin-top: 0.75rem;
  padding-top: 0.75rem;
  border-top: 1px solid var(--border);
}

/* Hide the label along with the list -- an empty .card-scores means no
   scores yet or the API wasn't reachable, and a "High Scores" heading over
   nothing would look broken rather than just quietly absent. */
.score-block:has(.card-scores:empty) {
  display: none;
}

.score-label {
  margin: 0 0 0.4rem;
  font-size: 0.75rem;
  font-weight: 600;
  text-transform: uppercase;
  letter-spacing: 0.08em;
  color: var(--text-muted);
}

.card-scores {
  list-style: none;
  margin: 0;
  padding: 0;
  font-size: 0.85rem;
}

.card-scores li {
  display: flex;
  align-items: baseline;
  gap: 0.5rem;
  padding: 0.15rem 0;
}

.card-scores .rank {
  width: 1.1rem;
  color: var(--text-muted);
}

.card-scores .initials {
  flex: 1;
  color: var(--text);
  letter-spacing: 0.05em;
  font-weight: 600;
}

.card-scores .points {
  color: var(--accent);
}

.game-frame,
.game-frame--tetris {
  background: var(--bg-elevated);
  border: 1px solid var(--border);
  border-radius: 8px;
  max-width: 100%;
  margin-top: 1.5rem;
  overflow: hidden;
}

/*
 * Sizing is width-driven by default (block-level width fills its container),
 * with no regard for viewport height -- fine for the 4:3 Asteroids frame, but
 * Tetris's 3:4 portrait ratio grows *taller* than it is wide, so on a wide-
 * but-short window it can overflow past the bottom of the viewport.
 *
 * Fix: compute width from a viewport-height budget (leaving room for nav/
 * heading/hint/footer) via the frame's own aspect-ratio, then take whichever
 * is smaller against the existing 100%-of-container width. This only ever
 * constrains width (a single value), letting aspect-ratio derive height from
 * it -- so the box's ratio (and therefore the canvas Scale.FIT sizes into)
 * is never distorted, unlike constraining width and max-height independently.
 */
.game-frame {
  aspect-ratio: 4 / 3;
  width: min(100%, max(280px, calc((100vh - 380px) * 4 / 3)));
}

.game-frame--tetris {
  aspect-ratio: 3 / 4;
  width: min(100%, max(280px, calc((100vh - 380px) * 3 / 4)));
}

.game-frame canvas,
.game-frame--tetris canvas {
  display: block;
  width: 100% !important;
  height: 100% !important;
  /* Without this, a touch-drag inside the canvas (Ragdoll's grab-and-fling,
     Asteroids' joystick) can also scroll/pinch-zoom the page underneath it. */
  touch-action: none;
}

.game-hint {
  color: var(--text-muted);
  font-size: 0.9rem;
}

.empty-state {
  color: var(--text-muted);
  border: 1px dashed var(--border);
  border-radius: 8px;
  padding: 1.5rem;
  margin-top: 2rem;
}

/* Multiplayer lobby UI -- first real HTML form/button surface on the
   site (every other interactive control is a Phaser canvas object with
   no DOM equivalent to style). */

.player-name-field {
  display: block;
  margin-bottom: 1.5rem;
  color: var(--text-muted);
  font-size: 0.9rem;
}

.player-name-field input,
.lobby-form input {
  display: block;
  width: 100%;
  max-width: 320px;
  margin-top: 0.35rem;
  padding: 0.5rem 0.65rem;
  background: var(--bg-elevated);
  border: 1px solid var(--border);
  border-radius: 6px;
  color: var(--text);
  font: inherit;
}

.player-name-field input:focus,
.lobby-form input:focus {
  outline: none;
  border-color: var(--accent);
}

.btn {
  display: inline-block;
  padding: 0.5rem 1rem;
  background: var(--bg-elevated);
  border: 1px solid var(--border);
  border-radius: 6px;
  color: var(--text);
  font: inherit;
  cursor: pointer;
  transition: border-color 0.15s ease;
}

.btn:hover {
  border-color: var(--accent);
}

.btn-primary {
  background: var(--accent);
  border-color: var(--accent);
  color: var(--bg);
  font-weight: 600;
}

.btn-primary:hover {
  opacity: 0.9;
}

.btn-danger {
  background: transparent;
  border-color: var(--danger);
  color: var(--danger);
}

.btn-danger:hover {
  background: var(--danger);
  color: var(--bg);
}

.lobby-list,
.member-list {
  list-style: none;
  margin: 0 0 1.5rem;
  padding: 0;
}

.lobby-row,
.member-row {
  display: flex;
  align-items: center;
  gap: 0.75rem;
  padding: 0.6rem 0.75rem;
  background: var(--bg-elevated);
  border: 1px solid var(--border);
  border-radius: 6px;
  margin-bottom: 0.5rem;
}

.lobby-row-name,
.member-row-name {
  flex: 1;
  font-weight: 600;
}

.lobby-row-count {
  color: var(--text-muted);
  font-size: 0.85rem;
}

.member-row-badge {
  font-size: 0.7rem;
  font-weight: 700;
  text-transform: uppercase;
  letter-spacing: 0.06em;
  color: var(--bg);
  background: var(--accent);
  padding: 0.15rem 0.5rem;
  border-radius: 999px;
}

.lobby-form {
  max-width: 320px;
  margin-top: 2rem;
  padding-top: 1.5rem;
  border-top: 1px solid var(--border);
}

.lobby-form label {
  display: block;
  margin-bottom: 1rem;
  color: var(--text-muted);
  font-size: 0.9rem;
}

.waiting-room-actions {
  display: flex;
  gap: 0.75rem;
  margin-top: 1rem;
}

.form-error {
  color: var(--danger);
  margin-top: 0.75rem;
}

.form-notice {
  color: var(--accent);
  margin-top: 0.75rem;
}

#back-to-lobby-btn {
  margin-top: 1rem;
}

footer {
  border-top: 1px solid var(--border);
  color: var(--text-muted);
  text-align: center;
  padding: 1.5rem;
  font-size: 0.9rem;
}
