:root {
  --bg: #0b0d12;
  --bg-lift: #151923;
  --fg: #f2f4f8;
  --dim: #7b8496;
  --accent: #ffcc33;
  --good: #4ade80;
  --bad: #ff4d4d;
  --line: #232936;
}

* { box-sizing: border-box; }

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

body {
  background: var(--bg);
  color: var(--fg);
  font-family: Archivo, "Helvetica Neue", Helvetica, Arial, sans-serif;
  font-weight: 700;
  overflow: hidden;
  -webkit-font-smoothing: antialiased;
}

/* ---------- stage ---------- */

#stage {
  position: fixed;
  inset: 0;
  display: grid;
  grid-template-rows: auto 1fr auto;
  padding: clamp(12px, 2.4vw, 40px);
  padding-bottom: clamp(90px, 12vh, 150px);
}

#topbar {
  display: flex;
  align-items: baseline;
  gap: 16px;
}

#counter {
  font-size: clamp(15px, 1.9vw, 30px);
  font-weight: 800;
  letter-spacing: 0.06em;
  color: var(--dim);
  font-variant-numeric: tabular-nums;
}

#counter-now { color: var(--fg); }

#counter-sep { margin: 0 0.35em; opacity: 0.5; }

#shuffle-flag {
  font-size: clamp(11px, 1.1vw, 16px);
  letter-spacing: 0.18em;
  text-transform: uppercase;
  color: var(--accent);
  border: 1px solid var(--accent);
  border-radius: 999px;
  padding: 3px 12px;
}

/* ---------- puzzle ---------- */

#puzzle-wrap {
  display: grid;
  place-items: center;
  min-height: 0;
  padding: clamp(8px, 2vh, 32px) 0;
}

#puzzle {
  width: 100%;
  max-width: 92vw;
  display: grid;
  place-items: center;
  text-align: center;
}

/* ---------- answer ---------- */

#answer-wrap {
  display: grid;
  place-items: center;
  min-height: clamp(56px, 11vh, 120px);
}

#answer {
  font-size: clamp(24px, 4.6vw, 88px);
  font-weight: 900;
  letter-spacing: -0.01em;
  color: var(--accent);
  text-align: center;
  opacity: 0;
  transform: translateY(18px);
  transition: opacity 0.28s ease, transform 0.28s ease;
  padding: 0 12px;
}

#answer.shown {
  opacity: 1;
  transform: translateY(0);
}

/* ---------- timer ---------- */

#timer {
  position: fixed;
  top: clamp(12px, 2.4vw, 40px);
  right: clamp(12px, 2.4vw, 40px);
  width: clamp(74px, 11vw, 170px);
  height: clamp(74px, 11vw, 170px);
  display: grid;
  place-items: center;
}

#timer svg {
  position: absolute;
  inset: 0;
  width: 100%;
  height: 100%;
  transform: rotate(-90deg);
}

.ring-track {
  fill: none;
  stroke: var(--line);
  stroke-width: 7;
}

.ring-fill {
  fill: none;
  stroke: var(--good);
  stroke-width: 7;
  stroke-linecap: round;
  stroke-dasharray: 276.46;
  stroke-dashoffset: 0;
  transition: stroke-dashoffset 0.25s linear, stroke 0.3s ease;
}

#timer-num {
  position: relative;
  font-size: clamp(26px, 4vw, 64px);
  font-weight: 900;
  font-variant-numeric: tabular-nums;
  color: var(--fg);
}

#timer[data-state="idle"] .ring-fill { stroke: var(--dim); }
#timer[data-state="paused"] .ring-fill { stroke: var(--accent); }
#timer[data-state="warn"] .ring-fill { stroke: var(--bad); }
#timer[data-state="warn"] #timer-num { color: var(--bad); }
#timer[data-state="warn"] { animation: pulse 1s ease-in-out infinite; }
#timer[data-state="done"] .ring-fill { stroke: var(--bad); }
#timer[data-state="done"] #timer-num { color: var(--bad); }

@keyframes pulse {
  0%, 100% { transform: scale(1); }
  50% { transform: scale(1.07); }
}

@media (prefers-reduced-motion: reduce) {
  #timer[data-state="warn"] { animation: none; }
  #answer { transition: none; }
}

/* ---------- controls ---------- */

#controls {
  position: fixed;
  left: 0;
  right: 0;
  bottom: 0;
  display: flex;
  align-items: center;
  gap: clamp(6px, 0.9vw, 14px);
  padding: clamp(10px, 1.4vw, 20px) clamp(12px, 2.4vw, 40px);
  background: linear-gradient(to top, rgba(11, 13, 18, 0.97), rgba(11, 13, 18, 0));
  transition: opacity 0.45s ease;
  flex-wrap: wrap;
}

body.dim-controls #controls { opacity: 0.12; }
body.dim-controls #controls:hover { opacity: 1; }
body.dim-controls #help { opacity: 0; }

#controls button {
  font-family: inherit;
  font-weight: 800;
  font-size: clamp(14px, 1.5vw, 22px);
  color: var(--fg);
  background: var(--bg-lift);
  border: 1px solid var(--line);
  border-radius: 12px;
  padding: clamp(9px, 1.1vw, 16px) clamp(13px, 1.6vw, 24px);
  cursor: pointer;
  line-height: 1;
  transition: background 0.15s ease, border-color 0.15s ease, transform 0.08s ease;
}

#controls button:hover { background: #1d2330; border-color: #39415440; }
#controls button:active { transform: translateY(1px); }

#controls button:focus-visible {
  outline: 3px solid var(--accent);
  outline-offset: 2px;
}

#controls button.primary {
  background: var(--fg);
  color: var(--bg);
  border-color: var(--fg);
  min-width: clamp(92px, 9vw, 150px);
}

#controls button.reveal {
  border-color: var(--accent);
  color: var(--accent);
}

#controls button.reveal[data-on="1"] {
  background: var(--accent);
  color: var(--bg);
}

#controls .spacer { flex: 1; }

/* duration slider */

#dur, #hold {
  display: flex;
  align-items: center;
  gap: clamp(8px, 1vw, 14px);
  background: var(--bg-lift);
  border: 1px solid var(--line);
  border-radius: 12px;
  padding: clamp(8px, 1vw, 14px) clamp(12px, 1.4vw, 20px);
}

/* The answer-hold slider only matters while auto-play is on. */
#hold { opacity: 0.4; transition: opacity 0.2s ease; }
body.auto-on #hold { opacity: 1; }

#btn-auto {
  letter-spacing: 0.1em;
  font-size: clamp(12px, 1.3vw, 18px) !important;
}

#btn-mute[aria-pressed="true"] { color: #4d5567; border-color: #1c2130; }

#btn-auto[aria-pressed="true"] {
  background: var(--good);
  color: var(--bg);
  border-color: var(--good);
}

#dur-range, #hold-range {
  -webkit-appearance: none;
  appearance: none;
  width: clamp(64px, 8vw, 140px);
  height: 5px;
  border-radius: 999px;
  background: var(--line);
  outline: none;
  cursor: pointer;
}

#dur-range::-webkit-slider-thumb, #hold-range::-webkit-slider-thumb {
  -webkit-appearance: none;
  appearance: none;
  width: clamp(16px, 1.5vw, 22px);
  height: clamp(16px, 1.5vw, 22px);
  border-radius: 50%;
  background: var(--accent);
  cursor: pointer;
  border: none;
}

#dur-range::-moz-range-thumb, #hold-range::-moz-range-thumb {
  width: clamp(16px, 1.5vw, 22px);
  height: clamp(16px, 1.5vw, 22px);
  border-radius: 50%;
  background: var(--accent);
  cursor: pointer;
  border: none;
}

#dur-range:focus-visible, #hold-range:focus-visible {
  outline: 3px solid var(--accent);
  outline-offset: 4px;
}

#dur-val, #hold-val {
  font-size: clamp(13px, 1.4vw, 20px);
  font-weight: 800;
  color: var(--accent);
  font-variant-numeric: tabular-nums;
  min-width: 3.2ch;
  text-align: right;
}

/* ---------- grid overlay ---------- */

#grid {
  position: fixed;
  inset: 0;
  background: rgba(5, 6, 9, 0.93);
  display: grid;
  place-items: center;
  z-index: 20;
  padding: 20px;
}

#grid[hidden] { display: none; }

#grid-inner {
  width: min(900px, 94vw);
  max-height: 88vh;
  overflow-y: auto;
  text-align: center;
}

#grid-inner h2 {
  font-size: clamp(16px, 2vw, 28px);
  letter-spacing: 0.12em;
  text-transform: uppercase;
  color: var(--dim);
  font-weight: 800;
  margin: 0 0 clamp(14px, 2vw, 26px);
}

#grid-cells {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(clamp(58px, 7vw, 92px), 1fr));
  gap: clamp(8px, 1vw, 14px);
}

#grid-cells button {
  font-family: inherit;
  font-weight: 900;
  font-size: clamp(17px, 2.2vw, 32px);
  aspect-ratio: 1;
  color: var(--fg);
  background: var(--bg-lift);
  border: 1px solid var(--line);
  border-radius: 14px;
  cursor: pointer;
  font-variant-numeric: tabular-nums;
}

#grid-cells button:hover { border-color: var(--accent); }

#grid-cells button[aria-current="true"] {
  background: var(--accent);
  color: var(--bg);
  border-color: var(--accent);
}

#grid-close {
  font-family: inherit;
  font-weight: 800;
  font-size: clamp(14px, 1.5vw, 20px);
  margin-top: clamp(16px, 2.4vw, 30px);
  padding: 12px 28px;
  color: var(--fg);
  background: transparent;
  border: 1px solid var(--line);
  border-radius: 12px;
  cursor: pointer;
}

/* ---------- help ---------- */

#help {
  position: fixed;
  left: 50%;
  transform: translateX(-50%);
  bottom: clamp(72px, 8.4vh, 104px);
  font-size: clamp(10px, 1vw, 14px);
  font-weight: 600;
  letter-spacing: 0.04em;
  color: #4d5567;
  white-space: nowrap;
  pointer-events: none;
  transition: opacity 0.45s ease;
}

#help kbd {
  font-family: inherit;
  font-size: 0.95em;
  border: 1px solid #2b3242;
  border-radius: 5px;
  padding: 1px 5px;
  margin: 0 1px;
}

/* ---------- phone ----------
 *
 * The big-screen layout floats the timer over the stage and spreads the
 * controls across one row. Neither works on a phone: the timer lands on top
 * of the buttons and the row wraps into unreachable slivers. So the phone
 * gets its own stacked layout - timer inline in the header, puzzle taking all
 * remaining height, and controls as a grid of full-size touch targets.
 */

@media (max-width: 780px) {
  #help { display: none; }

  html, body { overflow: hidden; }

  body {
    display: flex;
    flex-direction: column;
    height: 100dvh;
  }

  #stage {
    position: static;
    flex: 1 1 auto;
    min-height: 0;
    display: grid;
    grid-template-rows: auto 1fr auto;
    padding: 12px 14px 0;
  }

  /* Counter and timer share the header row. */
  #topbar {
    display: flex;
    align-items: center;
    justify-content: space-between;
    gap: 10px;
  }

  #counter { font-size: 20px; margin-right: auto; }
  #shuffle-flag { font-size: 10px; padding: 2px 8px; }

  /* Relative, so the absolutely positioned ring sizes against this box. */
  #timer {
    position: relative;
    top: auto;
    right: auto;
    width: 52px;
    height: 52px;
    flex: 0 0 52px;
  }

  #timer svg { width: 52px; height: 52px; }
  #timer-num { font-size: 20px; }
  .ring-track, .ring-fill { stroke-width: 9; }

  /* The puzzle takes whatever height is left. */
  #puzzle-wrap { min-height: 0; padding: 6px 0; overflow: hidden; }
  #puzzle { max-width: 100%; }

  #answer-wrap { min-height: 50px; }
  #answer { font-size: 26px; }

  /* Controls: a grid of thumb-sized targets under the stage.
     minmax(0,1fr) rather than 1fr, so the buttons' intrinsic widths cannot
     stop the columns from sharing the row evenly. Explicit grid-row keeps
     the three rows stable regardless of source order. */
  #controls {
    position: static;
    display: grid;
    grid-template-columns: repeat(6, minmax(0, 1fr));
    gap: 8px;
    padding: 10px 14px calc(12px + env(safe-area-inset-bottom, 0px));
    background: var(--bg-lift);
    border-top: 1px solid var(--line);
    opacity: 1 !important;
  }

  #controls .spacer { display: none; }

  #controls button {
    font-size: 15px;
    padding: 0 4px;
    min-height: 52px;
    min-width: 0;
    border-radius: 10px;
  }

  /* Row 1 - the moment-to-moment controls. */
  #btn-prev   { grid-row: 1; grid-column: span 1; }
  #btn-timer  { grid-row: 1; grid-column: span 2; }
  #btn-next   { grid-row: 1; grid-column: span 1; }
  #btn-reset  { grid-row: 1; grid-column: span 1; }
  #btn-full   { grid-row: 1; grid-column: span 1; }

  /* Row 2 - reveal gets the width it deserves. */
  #btn-answer { grid-row: 2; grid-column: span 3; font-size: 16px; }
  #btn-grid   { grid-row: 2; grid-column: span 1; }
  #btn-shuffle{ grid-row: 2; grid-column: span 1; }

  /* Row 3 - auto-play and its two sliders. */
  #btn-auto   { grid-row: 3; grid-column: span 2; font-size: 13px !important; }
  #btn-mute   { grid-row: 2; grid-column: span 1; }
  #hold       { grid-row: 3; grid-column: span 2; }
  #dur        { grid-row: 3; grid-column: span 2; }

  #hold, #dur {
    padding: 0 10px;
    min-height: 52px;
    min-width: 0;
    gap: 6px;
  }

  #hold-range, #dur-range { width: 100%; min-width: 0; flex: 1; }
  #hold-val, #dur-val { font-size: 13px; min-width: 2.8ch; }
}

/* Landscape phone: shrink the chrome so the puzzle still fits. */
@media (max-width: 900px) and (orientation: landscape) and (max-height: 520px) {
  #stage { padding: 8px 12px 0; }
  #timer, #timer svg { width: 40px; height: 40px; }
  #timer { flex-basis: 40px; }
  #timer-num { font-size: 16px; }
  #answer-wrap { min-height: 34px; }
  #answer { font-size: 19px; }
  #controls { padding: 6px 12px calc(8px + env(safe-area-inset-bottom, 0px)); gap: 6px; }
  #controls button, #hold, #dur { min-height: 40px; font-size: 13px; }
}
