:root {
  --bg: #0f1d2b;
  --bg-2: #18304a;
  --panel: #1d3550;
  --panel-hover: #26445f;
  --text: #f4ecdc;
  --muted: #9fb0c2;
  --accent: #c0392b; /* 4.6:1 with --text, 5.4:1 with white */
  --accent-hover: #a93226; /* darker on hover so contrast goes up, not down */
  --accent-text: #ff8a7d; /* red for text on dark backgrounds */
  --line: rgba(255, 255, 255, .45); /* control borders, ≈3.9:1 on the dark background */
  --accent-shadow: rgba(192, 57, 43, .4); /* glow under red buttons and the error pill */
  --ring-track: rgba(192, 57, 43, .18); /* faint full circle behind the spinning / counting ring */
  --divider: rgba(255, 255, 255, .22); /* light splitter lines and empty Previous slots */
  --gold: #f2b134;
  --ball: #fdf6e6;
  --ink: #1b2a38;
  --draft-ring: #cdbb93; /* ring of an empty ball or a typed (not yet called) number */
  --draft-ink: #c2ae85; /* "?" on an empty or spinning ball */
  --radius: 16px;

  /* Animation lengths, shared with app.js (which reads them), so the two can't drift apart. */
  --clear-ms: 300ms; /* digital draw: the old number fades out */
  --reveal-ms: 700ms; /* digital draw / tapped call: pulse and number drop */
  --confirm-ms: 400ms; /* typed call confirmed in place; short ring pulse */
  --shake-ms: 400ms; /* refused typed number */
  --locate-ms: 2200ms; /* an already-called number points itself out on the board */

  /* Sizing: the room the page's other parts take, used to fit the ball and the board on screen. */
  --board-aspect: 1.1; /* board width : height (10 columns, 9 rows) */
  --board-pad-x: 3rem; /* the board panel's padding, both sides */
  --chrome-h: 18.5rem; /* header, settings, panel padding, counter and page margins around the board */
  --stage-col: 32vw; /* the ball column (two-column layout) */
  --stage-below: 30rem; /* room the ball column needs besides the ball: nickname, button, hint, margins */
  --board-max-w: calc(68vw - 7rem); /* what remains beside the ball column, the gap and the page padding */
  --mini-size: clamp(2.7rem, 3.2vw, 4.2rem); /* Previous balls and their empty slots */
  --side-col: var(--mini-size); /* width of the Previous column: one ball wide */
  --side-gap: 1.75rem; /* space between the big ball and the Previous column */
  --recent-gap: .8rem; /* space between Previous balls */
  color-scheme: dark;
  font-family: system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
}

* { box-sizing: border-box; }

[hidden] { display: none !important; }

/* While the page sets itself up (removed by app.js after the first frame), nothing transitions,
   so the restored game appears at once instead of fading or blinking in. */
.is-loading *, .is-loading *::before, .is-loading *::after { transition: none !important; }

html, body { margin: 0; min-height: 100%; }

body {
  background: radial-gradient(ellipse at top, var(--bg-2), var(--bg) 70%) fixed;
  color: var(--text);
}

.app {
  max-width: 2400px; /* grows with big screens and projectors */
  margin: 0 auto;
  padding: 1rem clamp(1rem, 3vw, 2rem) 2rem;
  display: grid;
  gap: 1.5rem 2.5rem;
  grid-template-columns: 1fr;
  align-items: start;
}

@media (min-width: 900px) {
  /* Ball column + board column sized to their content, centred as a group. */
  .app { grid-template-columns: minmax(320px, var(--stage-col)) auto; column-gap: 2rem; justify-content: center; }
  .top, .toolbar { grid-column: 1 / -1; }
  /* Keep the whole board visible: limit the panel's width by the screen height (9 rows, 10 columns),
     so the panel hugs the board instead of leaving empty space beside it. (Widths need no
     percentages here: an auto-sized column can't resolve them.) */
  .board-section {
    width: min(var(--board-max-w), max(360px, calc((100svh - var(--chrome-h)) * var(--board-aspect) + var(--board-pad-x))));
  }
}

/* ---------- header ---------- */

.top {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  justify-content: space-between;
  gap: 1rem;
}

/* Page actions (Present, New game) together at the top right. */
.top-actions {
  display: flex;
  align-items: center;
  gap: 1rem;
}

/* Light vertical line between the two actions (same style as the splitters in the settings). */
.divider {
  align-self: stretch;
  width: 1px;
  margin: .2rem 0;
  background: var(--divider);
}

/* Button with a small icon before its label. */
.btn-icon {
  display: inline-flex;
  align-items: center;
  gap: .4rem;
}

.btn-icon svg {
  width: 1em;
  height: 1em;
  fill: none;
  stroke: currentColor;
  stroke-width: 2.2;
  stroke-linecap: round;
  stroke-linejoin: round;
}

h1 {
  margin: 0;
  font-size: 1.3rem;
  letter-spacing: .1em;
  text-transform: uppercase;
  color: var(--gold);
}

/* ---------- settings ---------- */

/* Row holding the settings panel. */
.toolbar {
  display: flex;
  align-items: flex-start;
  margin-bottom: .5rem; /* with the grid gap: 2rem between the toolbar and the board */
}

/* The settings panel spans the full width of the row. */
.settings {
  flex: 1;
  min-width: 0;
  border-radius: var(--radius);
  background: rgba(0, 0, 0, .2);
}

.settings > summary {
  display: flex;
  align-items: center;
  gap: .6rem;
  padding: .75rem 1.1rem;
  font-weight: 600;
  list-style: none;
  cursor: pointer;
}

.settings > summary::-webkit-details-marker { display: none; }

.settings > summary::after {
  content: "▾";
  margin-left: auto;
  color: var(--muted);
  transition: transform .2s;
}

.settings[open] > summary::after { transform: rotate(180deg); }

.settings-state { font-size: .9rem; font-weight: 400; color: var(--muted); }

.settings-body {
  display: flex;
  flex-direction: column;
  align-items: flex-start;
  gap: .9rem;
  padding: 0 1.1rem 1rem;
}

.setting-group {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  gap: .6rem;
}

/* Wide screens: settings always shown in one row, groups split by light lines. (Not on phones in
   landscape, which are wide but too short: there they fold, see the end of the layout rules.
   app.js uses the same query to keep them open.) */
@media (min-width: 700px) and (min-height: 500.02px) {
  .settings > summary { display: none; }
  .settings-body { flex-direction: row; flex-wrap: wrap; align-items: center; gap: .9rem 1.5rem; padding: .75rem 1.25rem; }
  .setting-group:not([hidden]) ~ .setting-group:not([hidden]) {
    padding-left: 1.5rem;
    border-left: 1px solid var(--divider);
  }
}

.mode {
  display: inline-flex;
  padding: 3px;
  border: 1px solid var(--line);
  border-radius: 999px;
}

.mode-btn {
  padding: .3rem .85rem;
  border: none;
  border-radius: 999px;
  background: transparent;
  color: var(--muted);
  font: inherit;
  font-size: .85rem;
  font-weight: 600;
  cursor: pointer;
  transition: background-color .2s, color .2s;
}

.mode-btn:hover:not(:disabled) { color: var(--text); }
.mode-btn[aria-pressed="true"] { background: var(--panel-hover); color: var(--text); }
.mode-btn:disabled { cursor: default; }

.counter {
  margin: .4rem 1rem 0 0;
  text-align: right; /* near the board panel's right edge */
  color: var(--muted);
  font-variant-numeric: tabular-nums;
}

.counter span { color: var(--text); font-weight: 700; font-size: 1.2rem; }

/* ---------- ball ---------- */

.stage {
  --ball-size: clamp(170px, 45vw, 240px); /* shared by the ball and the main button */
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 1.1rem;
}

/* Two-column layout: the ball grows with the screen, up to 440px, but leaves room
   - beside it for the Previous column on the right (and the same width on the left, so the ball
     stays centred): 32vw column minus 2 × (Previous column + gap), and
   - below it in the column's height for the nickname, Draw button and hint. */
@media (min-width: 900px) {
  .stage { --ball-size: clamp(200px, min(22vw, calc(var(--stage-col) - 2 * (var(--side-col) + var(--side-gap))), calc(100svh - var(--stage-below))), 440px); }
}

/* Phones and narrow screens (one column): the Previous calls run as a row, newest on the left,
   between the nickname and the main button, so the nickname stays right under its number. The
   ball row's own box is dissolved (display: contents), so the ball, nickname, Previous row,
   button and hint line up as one column, in this order. */
@media (max-width: 899.98px) and (orientation: portrait), (max-width: 899.98px) and (min-height: 500.02px) {
  .ball-row { display: contents; }
  .ball-row .ball { order: 1; }
  .stage > .nickname { order: 2; }
  .ball-row .previous { order: 3; }
  .stage > .draw { order: 4; }
  .stage > .hint { order: 5; }
}

/* Wider screens, and phones in landscape: equal empty column | ball | Previous column, so the
   ball is centred in the stage. */
@media (min-width: 900px), (orientation: landscape) and (max-height: 500px) {
  .ball-row {
    display: grid;
    grid-template-columns: 1fr auto 1fr;
    align-items: center;
    column-gap: var(--side-gap);
    width: 100%;
  }

  .ball-row .ball { grid-column: 2; grid-row: 1; }
  .ball-row .previous {
    grid-column: 3;
    grid-row: 1;
    justify-self: start;
    width: var(--side-col);
    position: relative;
    top: 2rem; /* where the newest Previous ball sits, measured from the top of the ball */
    /* As tall as the ball, with the (taller) column overflowing downwards, so the row is only as
       tall as the ball and the nickname sits right under it. Anchored at the top (not centred),
       so the newest ball never moves when the stack briefly holds an extra ball. */
    height: var(--ball-size);
    min-height: 0; /* grid items otherwise refuse to be shorter than their content */
    justify-content: flex-start;
    align-self: start;
  }
}

.ball {
  position: relative;
  z-index: 1; /* the flying Previous ball starts hidden behind it */
  width: var(--ball-size);
  aspect-ratio: 1;
  border-radius: 50%;
  display: grid;
  place-items: center;
  background: radial-gradient(circle at 32% 28%, #fff 0 7%, var(--ball) 30%, #e6d8b8 72%, #b8a47b 100%);
  box-shadow: 0 20px 40px rgba(0, 0, 0, .45), inset 0 -12px 26px rgba(0, 0, 0, .14);
}

/* Printed face (ring + number) while the ball stands still. */
.ball-face {
  position: absolute;
  inset: 0;
  display: grid;
  place-items: center;
}

/* Digital draw:
   1. the old number fades out (CLEAR_MS), */
/* (color: a typed physical number turns from draft colour to ink when confirmed) */
.ball-face .ball-number { transition: opacity var(--clear-ms) ease-out, color var(--clear-ms) ease-out; }
.ball.is-clearing .ball-face .ball-number { opacity: 0; }

/* 2. the ring becomes a spinning arc (rotated by app.js) around a faint "?",
   (the track and arc are shared with the auto-draw countdown, see "Ring around the number") */
.ring { transform-origin: 50% 50%; }
.ball.is-spinning .ring-progress { stroke-dasharray: 28 72; }
.ball.is-spinning .ball-face .ball-number { color: var(--draft-ink); }

/* Physical mode: a gentle pulse while a number is about to be called, i.e. while a number on
   the board is held down or a typed number waits for Enter. */
.ball.is-anticipating { animation: ball-anticipate 1s ease-in-out infinite; }

@keyframes ball-anticipate {
  0%, 100% { transform: scale(1); }
  50% { transform: scale(1.02); }
}

/* 3. the ring snaps closed with a pulse and the number drops in. */
.ball.is-revealing { animation: ring-pulse var(--reveal-ms) ease-out; }
.ball.is-revealing .ball-face .ball-number { animation: number-drop var(--reveal-ms) cubic-bezier(.2, .9, .3, 1.3); }

/* Confirming a typed physical number: a solid red line draws itself once around the ring,
   clockwise from the top, over the dashed draft ring ("sealing" it). */
.ball.is-sealing .ring-face { stroke: var(--draft-ring); stroke-dasharray: 2.5 1.8; }
.ball.is-sealing .ring-progress {
  display: inline;
  stroke: var(--accent);
  stroke-dasharray: 100;
  stroke-linecap: round;
  animation: ring-seal .35s ease-out forwards;
}

@keyframes ring-seal {
  from { stroke-dashoffset: 100; }
  to { stroke-dashoffset: 0; }
}

/* Enter on a typed number that can't be called: a short "no" shake. */
.ball.is-shaking { animation: ball-shake var(--shake-ms) ease-in-out; }

@keyframes ball-shake {
  0%, 100% { transform: none; }
  20% { transform: translateX(-8px); }
  40% { transform: translateX(8px); }
  60% { transform: translateX(-5px); }
  80% { transform: translateX(5px); }
}

/* Removing the current number (or undoing that), and confirming a typed physical number: the
   number is shown at once, with just a short ring pulse. */
.ball.is-returning { animation: ring-pulse var(--confirm-ms) ease-out; }

@keyframes ring-pulse {
  0% { transform: scale(1); }
  30% { transform: scale(1.05); }
  100% { transform: scale(1); }
}

@keyframes number-drop {
  0% { transform: translateY(-35%); opacity: 0; }
  20% { opacity: 1; }
  100% { transform: none; opacity: 1; }
}

/* Fixed light on top of the print: a soft highlight and darker edges, which give the ball its
   3D look. */
.ball::after {
  content: "";
  position: absolute;
  inset: 0;
  z-index: 2;
  border-radius: 50%;
  background:
    radial-gradient(circle at 32% 28%, rgba(255, 255, 255, .45) 0 5%, rgba(255, 255, 255, 0) 22%),
    radial-gradient(circle at 45% 42%, rgba(0, 0, 0, 0) 52%, rgba(70, 50, 20, .28) 100%);
  pointer-events: none;
}

/* Ring around the number; drains like a timer during auto draw. */
.ring {
  position: absolute;
  inset: 0;
  width: 100%;
  height: 100%;
  overflow: visible;
}

.ring ellipse, .ring circle { fill: none; stroke-width: 2.6; }

/* The visible ring: a plain closed ellipse (no dashes), so it can never show a seam. */
.ring-face { stroke: var(--accent); transition: stroke .3s; }

/* Track + arc: the spinning arc of a digital draw, and the auto-draw countdown (draining arc).
   They exist only then; the plain ring is hidden meanwhile. */
.ring-track, .ring-progress { display: none; }
.ball:is(.is-spinning, .is-counting) .ring-face { display: none; }
.ball:is(.is-spinning, .is-counting) .ring-track { display: inline; stroke: var(--ring-track); }
.ball:is(.is-spinning, .is-counting) .ring-progress { display: inline; stroke: var(--accent); stroke-linecap: round; }
.ball.is-counting .ring-progress { stroke-dasharray: 100; }

.ball-number {
  position: relative;
  font-size: calc(var(--ball-size) * .42); /* scales with the ball */
  font-weight: 800;
  line-height: 1;
  color: var(--ink);
  font-variant-numeric: tabular-nums;
}

.ball.is-empty .ring-face { stroke: var(--draft-ring); }
.ball.is-empty .ball-number { color: var(--draft-ink); }

/* Bag mode: digits typed on the keyboard but not yet called. */
.ball.is-typing .ring-face { stroke: var(--draft-ring); stroke-dasharray: 2.5 1.8; }
.ball.is-typing .ball-number { color: #857350; }

.nickname {
  --nickname-size: clamp(1.35rem, 1.6vw, 2.2rem);
  margin: 0;
  /* The same height whatever it shows (a nickname, a smaller hint or the error pill), so the
     button and board below never jump. Based on the nickname's size, not the text's own. */
  min-height: calc(var(--nickname-size) * 1.6);
  font-size: var(--nickname-size);
  font-style: italic;
  text-align: center;
  color: var(--gold);
  opacity: 0;
  transform: translateY(6px);
  transition: opacity .35s ease, transform .35s ease;
}

.nickname.show { opacity: 1; transform: none; }

/* Typewriter effect (app.js): the rest of the text keeps its place but stays invisible, and a thin
   cursor follows the last written letter. */
.nickname .unwritten { visibility: hidden; }

/* Hints (shown in place of a nickname): plain and quiet. */
.nickname.is-hint { font-style: normal; font-size: 1rem; color: var(--muted); }

/* Enter on a typed number that can't be called: the reason in a prominent solid red pill
   (white on --accent, 5.4:1), bold and nearly nickname-sized; it pops in to catch the eye and
   stays until the caller types again or presses Esc. (Error messages are hints too: this rule
   comes after the hint rule, so it wins.) */
.nickname.is-error {
  /* Fills the line's height, text centred in it. */
  display: flex;
  align-items: center;
  justify-content: center;
  padding: .35rem 1rem;
  border-radius: 999px;
  background: var(--accent);
  color: #fff;
  font-size: clamp(1.1rem, 1.4vw, 1.6rem);
  font-style: normal;
  font-weight: 700;
  box-shadow: 0 6px 18px var(--accent-shadow);
}

.nickname.is-flashing { animation: message-pop .35s cubic-bezier(.2, .9, .3, 1.4); }

@keyframes message-pop {
  0% { transform: scale(.85); }
  60% { transform: scale(1.06); }
  100% { transform: scale(1); }
}
.nickname.is-writing .written { box-shadow: 2px 0 0 currentColor; }
/* Nickname right under the ball: cancel the stage gap, and use a tight line height so there's no
   extra space above the text. */
.ball-row + .nickname { margin-top: -.1rem; line-height: 1.15; } /* 1rem below the ball (with the 1.1rem stage gap) */

/* ---------- buttons ---------- */

.btn {
  font: inherit;
  font-weight: 600;
  padding: .65rem 1.2rem;
  border-radius: 999px;
  border: 1px solid var(--line);
  background: var(--panel);
  color: var(--text);
  cursor: pointer;
  transition: transform .1s, background-color .2s, opacity .2s, color .2s;
}

.btn:hover:not(:disabled) { background: var(--panel-hover); }
.btn:active:not(:disabled) { transform: scale(.96); }
.btn:disabled { opacity: .4; cursor: not-allowed; }

.btn-small { padding: .4rem .95rem; font-size: .9rem; }

.btn-ghost { background: transparent; }

.btn-primary {
  width: var(--ball-size); /* same width as the ball above it */
  padding: .9rem 1rem;
  font-size: clamp(1.3rem, calc(var(--ball-size) * .085), 2rem);
  border-color: transparent;
  background: var(--accent);
  box-shadow: 0 6px 18px var(--accent-shadow);
}

.btn-primary:hover:not(:disabled) { background: var(--accent-hover); }

/* Press confirmation (0.5 s = MAIN_COOLDOWN_MS in app.js): the button dips in, a gold ring
   expands outward and fades, and a light sweeps across it. Shows "your press counted"
   without looking disabled. */
.btn-primary { position: relative; overflow: hidden; }
.btn-primary.is-pressed { animation: press-dip .5s ease-out; }

.btn-primary.is-pressed::after {
  content: "";
  position: absolute;
  inset: 0;
  background: linear-gradient(100deg, transparent 15%, rgba(255, 255, 255, .45) 50%, transparent 85%);
  transform: translateX(-100%);
  animation: press-sweep .5s ease-out forwards;
  pointer-events: none;
}

@keyframes press-dip {
  0% {
    transform: scale(1);
    box-shadow: 0 6px 18px var(--accent-shadow), 0 0 0 0 rgba(242, 177, 52, .8);
  }
  20% { transform: scale(.92); }
  100% {
    transform: scale(1);
    box-shadow: 0 6px 18px var(--accent-shadow), 0 0 0 16px rgba(242, 177, 52, 0);
  }
}

@keyframes press-sweep { to { transform: translateX(100%); } }

@keyframes press-flash { 0%, 60% { background-color: var(--accent-hover); } }

/* On/off switch: knob position and colour both show the state. */
.switch {
  position: relative;
  flex: none;
  width: 2.75rem;
  height: 1.5rem;
  padding: 0;
  border: 1px solid var(--line);
  border-radius: 999px;
  background: var(--panel);
  cursor: pointer;
  transition: background-color .2s, border-color .2s;
}

.switch::after {
  content: "";
  position: absolute;
  top: 50%;
  left: 3px;
  width: 1.05rem;
  height: 1.05rem;
  border-radius: 50%;
  background: var(--muted); /* 5.6:1 on the track */
  transform: translateY(-50%);
  transition: transform .2s, background-color .2s;
}

.switch:hover { border-color: var(--text); }
.switch[aria-checked="true"] { background: var(--gold); border-color: transparent; }
.switch[aria-checked="true"]::after { background: var(--ink); transform: translate(1.2rem, -50%); }

label.label { cursor: pointer; } /* clicking a switch's label toggles it */

:focus-visible { outline: 3px solid var(--gold); outline-offset: 2px; }

.interval {
  display: flex;
  align-items: center;
  gap: .4rem;
  color: var(--muted);
}

.interval input { width: 7rem; accent-color: var(--gold); }
.interval output { min-width: 2.6em; font-variant-numeric: tabular-nums; color: var(--text); }
.interval:has(input:disabled) { opacity: .45; } /* slider is inactive while Auto draw is off */

/* Outside app: Auto draw doesn't apply, so the whole group is shown greyed out (inactive). */
.setting-group.is-unavailable .label,
.setting-group.is-unavailable .switch { opacity: .45; }
.setting-group.is-unavailable .switch { cursor: not-allowed; }
.setting-group.is-unavailable label.label { cursor: not-allowed; }

/* ---------- recent calls ---------- */

/* Previous calls: a column right of the ball, newest at the top (a row on phones, see below). */
.previous {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: .6rem;
}

/* The column is only as tall as the ball (see .ball-row .previous) and its content overflows it:
   never shrink the balls or slots to fit, or they get squashed. */
.previous > *,
.recent > * { flex-shrink: 0; }

/* Empty Previous slot: a faint dashed circle, filled as numbers are called. Decorative. */
.slot {
  flex: none;
  width: var(--mini-size);
  height: var(--mini-size);
  border: 2px dashed var(--divider);
  border-radius: 50%;
}

/* Keeps the Draw button clearly apart from the Previous calls above it. */
.stage .draw { margin-top: .75rem; }

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

.recent {
  list-style: none;
  margin: 0;
  padding: 0;
  display: flex;
  flex-direction: column; /* stacked, newest at the top */
  gap: var(--recent-gap);
}

/* Phones and narrow screens: a row under the nickname, newest on the left. */
@media (max-width: 899.98px) and (orientation: portrait), (max-width: 899.98px) and (min-height: 500.02px) {
  .recent {
    flex-direction: row;
    /* Always five places wide: a sixth ball, briefly there while the oldest leaves, overflows
       to the right instead of widening the row and shifting it sideways. */
    width: calc(5 * var(--mini-size) + 4 * var(--recent-gap));
  }
}

.mini {
  width: var(--mini-size);
  height: var(--mini-size);
  border-radius: 50%;
  display: grid;
  place-items: center;
  font-size: clamp(1rem, 1.3vw, 1.6rem);
  font-weight: 700;
  font-variant-numeric: tabular-nums;
  color: var(--ink);
  background: radial-gradient(circle at 35% 30%, #fff, var(--ball) 45%, #d8c8a2);
  box-shadow: 0 3px 8px rgba(0, 0, 0, .35);
}

.mini.leave { animation: shrink-out .3s ease-in forwards; }

@keyframes shrink-out { to { transform: scale(.4); opacity: 0; } }

/* Recent-sized ball flying from behind the big ball into the Recent row.
   z-index 0 keeps it above the page but below the big ball (z-index 1). */
.flyer {
  position: fixed;
  z-index: 0;
  margin: 0;
  pointer-events: none;
  box-shadow: 0 10px 24px rgba(0, 0, 0, .45);
}

/* ---------- board ---------- */

.board-section {
  padding: clamp(.9rem, 1.7vw, 1.5rem); /* = 3rem in the width formulas above (1.5rem on each side) */
  border-radius: var(--radius);
  background: rgba(0, 0, 0, .2);
}

.board {
  container-type: inline-size; /* cells size their digits from the board width */
  display: grid;
  grid-template-columns: repeat(10, 1fr);
  --cell-gap: clamp(4px, .7vw, 12px); /* a tap in it counts for the nearest number (app.js) */
  gap: var(--cell-gap); /* same spacing between rows and columns */
}

/* Small phones: the board reaches closer to the screen edges, so its numbers are bigger to tap. */
@media (max-width: 480px) {
  .board-section { margin-inline: -.5rem; padding: .5rem; }
  .board { --cell-gap: 3px; }
}

.cell {
  position: relative;
  aspect-ratio: 1;
  display: grid;
  place-items: center;
  padding: 0;
  border-radius: 50%;
  border: 1px solid rgba(255, 255, 255, .12);
  background: rgba(255, 255, 255, .04);
  color: var(--text); /* bright, so numbers survive a washed-out projector image */
  font: inherit;
  font-size: max(.8rem, 3.5cqi); /* about 45% of a cell */
  font-weight: 700;
  font-variant-numeric: tabular-nums;
  transition: background-color .35s, color .35s, border-color .35s;
}

.cell:disabled { cursor: default; }

/* Bag mode: uncalled numbers can be tapped to call them. */
.board.is-interactive .cell:not(:disabled):not(.called) { cursor: pointer; border-color: rgba(255, 255, 255, .45); }
.board.is-interactive .cell:not(:disabled):not(.called):hover { background: rgba(255, 255, 255, .14); border-color: var(--gold); }

.cell.called { background: var(--gold); color: var(--ink); border-color: transparent; cursor: pointer; }
.cell.latest { background: var(--accent); color: #fff; }

/* A called number opens its Remove menu when tapped: a light ring on hover and while it's open. */
.cell.called:hover,
.cell.has-menu { box-shadow: 0 0 0 2px var(--text); }

.cell.latest::after {
  content: "";
  position: absolute;
  inset: -3px;
  border-radius: 50%;
  border: 2px solid var(--accent);
  animation: pulse 1.6s ease-out infinite;
  pointer-events: none;
}

.cell.pop { animation: pop .45s cubic-bezier(.2, .9, .3, 1.4); }
.cell.ripple { animation: ripple .6s ease both; animation-delay: calc(var(--i) * 12ms); }

/* Physical mode, Enter on an already-called number: its circle points itself out with three
   pulses and a glowing gold ring. */
.cell.locate { animation: locate var(--locate-ms) ease-in-out; z-index: 1; }

@keyframes locate {
  0%, 33%, 66%, 100% { transform: scale(1); box-shadow: 0 0 0 0 rgba(242, 177, 52, 0); }
  16%, 49%, 82% { transform: scale(1.25); box-shadow: 0 0 0 6px rgba(242, 177, 52, .55); }
}

@keyframes pop {
  0% { transform: scale(.6); }
  60% { transform: scale(1.2); }
  100% { transform: scale(1); }
}

@keyframes pulse {
  from { transform: scale(1); opacity: .9; }
  to { transform: scale(1.5); opacity: 0; }
}

@keyframes ripple {
  50% { transform: scale(1.2); filter: brightness(1.3); }
}

.hint {
  margin: 0;
  text-align: center;
  font-size: .9rem;
  color: var(--muted);
}

.hint .keys-only { display: block; } /* keyboard tip on its own line */

/* Touch-only devices have no keyboard, so hide keyboard tips there. */
@media (hover: none) and (pointer: coarse) {
  .hint .keys-only { display: none; }
}

/* ---------- toast ---------- */

.toast {
  position: fixed;
  left: 50%;
  bottom: 1.5rem;
  max-width: calc(100vw - 2rem);
  padding: .7rem 1.2rem;
  border-radius: 999px;
  background: var(--panel);
  box-shadow: 0 8px 24px rgba(0, 0, 0, .45);
  opacity: 0;
  pointer-events: none;
  transform: translate(-50%, 20px);
  /* Hidden (not just transparent) once faded out, so its Undo button can't be reached with Tab. */
  visibility: hidden;
  transition: opacity .25s, transform .25s, visibility 0s .25s;
}

.toast.show { opacity: 1; transform: translate(-50%, 0); visibility: visible; transition-delay: 0s; }
.toast.show.has-action { pointer-events: auto; }
.toast.ok strong { color: var(--gold); }
.toast.no strong { color: var(--accent-text); }

/* Button inside a message, e.g. "37 removed. Undo". */
.toast-action {
  margin-left: .6rem;
  padding: .3rem .85rem;
  border: 1px solid var(--line);
  border-radius: 999px;
  background: transparent;
  color: var(--gold);
  font: inherit;
  font-weight: 700;
  cursor: pointer;
}

.toast-action:hover { background: var(--panel-hover); }

/* ---------- Remove menu (tap a called number) ---------- */

.cell-menu {
  position: fixed; /* placed next to its number by app.js */
  z-index: 20;
  padding: .3rem;
  border: 1px solid var(--line);
  border-radius: 12px;
  background: var(--panel);
  box-shadow: 0 10px 28px rgba(0, 0, 0, .5);
  animation: menu-in .12s ease-out;
}

@keyframes menu-in { from { opacity: 0; } }

.cell-menu-item {
  display: block;
  min-height: 44px; /* comfortable touch target */
  padding: .55rem 1.1rem;
  border: none;
  border-radius: 8px;
  background: transparent;
  color: var(--text);
  font: inherit;
  font-weight: 600;
  white-space: nowrap;
  cursor: pointer;
}

.cell-menu-item:hover { background: var(--panel-hover); }

.sr-only {
  position: absolute;
  width: 1px;
  height: 1px;
  overflow: hidden;
  clip-path: inset(50%);
  white-space: nowrap;
}

/* ---------- dialog ---------- */

.dialog {
  width: min(26rem, calc(100vw - 2rem));
  padding: 0; /* padding lives on the form, so clicks on the dialog element itself mean "backdrop" */
  border: 1px solid var(--line);
  border-radius: var(--radius);
  background: var(--panel);
  color: var(--text);
  box-shadow: 0 20px 50px rgba(0, 0, 0, .5);
}

.dialog::backdrop {
  background: rgba(5, 12, 20, .65);
  backdrop-filter: blur(2px);
}

.dialog[open] { animation: dialog-in .2s ease-out; }

@keyframes dialog-in { from { opacity: 0; transform: translateY(8px) scale(.98); } }

.dialog form { padding: 1.5rem; }
.dialog h2 { margin: 0 0 .5rem; font-size: 1.25rem; }
.dialog p { margin: 0 0 1.5rem; color: var(--muted); line-height: 1.45; } /* 5.6:1 on the panel */

.dialog-actions {
  display: flex;
  justify-content: flex-end;
  flex-wrap: wrap;
  gap: .75rem;
}

.btn-danger { background: var(--accent); border-color: transparent; } /* 4.6:1 */
.btn-danger:hover:not(:disabled) { background: var(--accent-hover); }

/* ---------- phones in landscape ---------- */

/* Short and wide (any width): one column would push the button and the board below the fold.
   Instead: header and folded settings share the top row, and the ball (with Previous beside it)
   and the board sit side by side, both sized by the screen height. (After the other layout
   rules, so it overrides them.) */
@media (orientation: landscape) and (max-height: 500px) {
  .app {
    grid-template-columns: auto auto;
    grid-template-areas: "top toolbar" "stage board";
    justify-content: center;
    align-items: start;
    gap: .75rem 1.5rem;
    padding: .5rem 1rem;
  }

  /* A compact header that stays on one line. */
  .top { grid-area: top; grid-column: auto; flex-wrap: nowrap; gap: .75rem; }
  .top-actions { gap: .5rem; }
  .top-actions .divider { display: none; }
  .top-actions .btn-small { padding: .35rem .75rem; white-space: nowrap; }
  h1 { font-size: .95rem; letter-spacing: .06em; white-space: nowrap; }
  .toolbar { grid-area: toolbar; grid-column: auto; margin: 0; }

  .stage {
    grid-area: stage;
    gap: .6rem;
    --ball-size: clamp(80px, calc(100svh - 14rem), 220px);
    /* Previous: five balls exactly as tall as the big ball, beside it from its top, so the column
       never hangs down into the nickname. */
    --recent-gap: .35rem;
    --mini-size: calc((var(--ball-size) - 4 * var(--recent-gap)) / 5);
    --side-col: var(--mini-size);
    --side-gap: 1rem;
  }
  .ball-row .previous { top: 0; }
  .mini { font-size: max(.6rem, calc(var(--mini-size) * .42)); } /* small phones: tiny balls, still-legible digits */
  .stage .draw { margin-top: .25rem; padding: .6rem 1rem; font-size: 1.15rem; min-width: 9rem; }
  .nickname { --nickname-size: 1.1rem; }
  .board-column { grid-area: board; }
  .board-section { width: calc((100svh - 10rem) * var(--board-aspect) + 1.5rem); padding: .75rem; }
  .board { --cell-gap: 3px; }
  .cell { font-size: max(.7rem, 3.5cqi); }
  .counter { margin-top: .15rem; line-height: 1.3; }
  .counter span { font-size: 1rem; }

  /* Opened settings drop down over the page instead of pushing everything down. */
  .settings { position: relative; }
  .settings[open] > .settings-body {
    position: absolute;
    top: calc(100% + .4rem);
    right: 0;
    z-index: 5;
    width: max-content;
    max-width: calc(100vw - 2rem);
    padding-top: 1rem;
    border-radius: var(--radius);
    background: var(--panel);
    box-shadow: 0 12px 30px rgba(0, 0, 0, .5);
  }
}

/* ---------- presentation mode (projector / TV) ---------- */

.present-exit { display: none; }

body.presenting .present-exit {
  display: inline-block;
  position: fixed;
  top: 1rem;
  right: 1rem;
  z-index: 10;
  padding: .65rem 1.5rem;
  font-size: 1rem;
}

/* Only what the game needs: ball, main button, keyboard hint, previous calls, board, counter. */
body.presenting .top,
body.presenting .toolbar { display: none; }

/* The board seen from across a room, on a projector or TV: bigger digits; numbers not yet called
   step back, so the called ones stand out; and the latest call is the brightest thing on the
   board (white with a thick gold ring), not a dark red that a washed-out projector turns muddy. */
body.presenting .cell { font-size: max(.8rem, 4.3cqi); }
body.presenting .cell:not(.called) { color: var(--muted); border-color: rgba(255, 255, 255, .08); }
body.presenting .cell.latest { background: #fff; color: var(--ink); box-shadow: 0 0 0 6px var(--gold), 0 0 24px 6px rgba(242, 177, 52, .55); z-index: 1; }
body.presenting .cell.latest::after { border-color: #fff; }

body.presenting .app {
  min-height: 100svh;
  padding: 2rem;
  align-content: center;
}

@media (min-width: 900px) {
  /* Without header and settings: a wider ball column, and less room needed around the board. */
  body.presenting {
    --stage-col: 36vw;
    --stage-below: 22.5rem;
    --chrome-h: 10.5rem;
    --board-max-w: calc(64vw - 6rem);
  }

  body.presenting .app { align-items: center; }

  /* Ball as big as the column (minus the Previous column on each side) and the screen height allow. */
  body.presenting .stage { --ball-size: min(26vw, calc(var(--stage-col) - 2 * (var(--side-col) + var(--side-gap))), calc(100svh - var(--stage-below))); }
  body.presenting .nickname { --nickname-size: clamp(1.35rem, calc(var(--ball-size) * .1), 3rem); }
}

/* ---------- reduced motion: no bouncing or flicker, fades only ---------- */

@media (prefers-reduced-motion: reduce) {
  .cell.pop, .cell.ripple, .cell.locate, .mini.leave, .dialog[open], .cell-menu, .ball.is-sealing .ring-progress { animation: none; }
  .cell.latest::after { animation: none; opacity: .6; }
  .nickname { transform: none; }
  .toast, .toast.show { transform: translate(-50%, 0); }
  .btn:active:not(:disabled) { transform: none; }
  /* Press confirmation without motion: a brief darker flash instead of dip and sweep. */
  .btn-primary.is-pressed { animation: press-flash .3s ease-out; }
  .btn-primary.is-pressed::after { display: none; }
}
