:root {
  --ink: #0b2540;
  --sea: #0b74a8;
  --sea-light: #e6f4fb;
  --card: #ffffff;
  --accent: #ffb703;
  --good: #2a9d5c;
  --bad: #d64545;
}

* { box-sizing: border-box; }

body {
  margin: 0;
  font-family: -apple-system, "Segoe UI", Roboto, sans-serif;
  background: linear-gradient(180deg, var(--sea-light), #ffffff 60%);
  color: var(--ink);
  min-height: 100vh;
}

/* Side padding leaves room for the two floated buttons, so the centred
   title can never run under them. */
header {
  position: relative;
  text-align: center;
  /* Side padding reserves room for the floated buttons so the centred
     title can never run under them. Four of them now — measured, not
     estimated: 4 x 46px plus their margins plus the row's own offset. */
  padding: 1.25rem 15rem 0.25rem;
}
header h1 { font-size: clamp(1.6rem, 5vw, 2.4rem); margin: 0; }

/* Neither the wrapper nor the buttons use flexbox here, on purpose.
   Making a <button> a flex container is unreliable in older Safari — it
   can fail to lay its content out at all, which is how both header
   buttons vanished on an older iPad while working everywhere else.
   Inline-block with line-height centring behaves the same everywhere. */
.header-btns {
  position: absolute;
  top: 1rem;
  right: 0.75rem;
  white-space: nowrap;
}

#sound-btn, #help-btn, #skin-btn, #home-btn {
  display: inline-block;
  vertical-align: middle;
  width: 46px;
  height: 46px;
  padding: 0;
  margin-left: 0.5rem;
  font-size: 1.25rem;
  line-height: 42px;      /* height minus the 2px borders */
  text-align: center;
  border: 2px solid var(--sea);
  border-radius: 50%;
  background: #fff;
  color: var(--ink);
  cursor: pointer;
  overflow: visible;
  -webkit-appearance: none;
  appearance: none;
}

#help-btn {
  font-weight: 900;
  color: var(--sea);
  font-size: 1.4rem;
}

#sound-btn[aria-pressed="false"] { opacity: 0.55; }

/* Both added by script rather than written into every page, so a topic
   that doesn't exist yet gets them too. */
#skin-btn { font-size: 1.15rem; }
#home-btn { font-size: 1.15rem; text-decoration: none; }

/* Below this the buttons can't be floated over a centred title without
   colliding with it, so the header becomes an ordinary row — title left,
   buttons right — and the collision stops being possible.
   
   The breakpoint has climbed with each button added: 560, 700, 780, and
   now 1000px. That is not arbitrary. Keeping the title centred means
   reserving the button row's width on BOTH sides, and only the right
   side actually holds buttons — so on a mid-sized screen the title gets
   squeezed into a narrow column with a wide empty margin beside it,
   which reads as the title shoved to the right. It shows up first in a
   skin with a wide display face, but the fault is here, not in the skin.
   
   If a fifth button is ever added, raise this again — or give up on
   centring the title and use the row layout everywhere. */
@media (max-width: 1000px) {
  header {
    display: flex;
    align-items: center;
    justify-content: space-between;
    gap: 0.5rem;
    padding: 0.85rem 0.6rem 0.25rem;
  }
  header h1 { font-size: 1.35rem; text-align: left; }
  /* Positioned, so the controls always paint above whatever a skin
     decorates the header with. A brass rivet strip drawn across the
     buttons is the sort of thing that only appears when the header is
     short, which is exactly this layout. */
  .header-btns { position: relative; z-index: 2; flex: 0 0 auto; }
  #sound-btn, #help-btn, #skin-btn, #home-btn {
    width: 38px; height: 38px; line-height: 34px; font-size: 1.05rem;
    margin-left: 0.35rem;
  }
  #help-btn { font-size: 1.15rem; }
}

main { max-width: 1000px; margin: 0 auto; padding: 1rem; }

/* ---------- Setup ---------- */

#quiz-setup { text-align: center; }

fieldset { border: none; margin: 0 0 1.25rem; padding: 0; }
legend {
  font-weight: 700;
  font-size: 1.05rem;
  margin-bottom: 0.5rem;
  color: #33506b;
}

.opt-row {
  display: flex;
  justify-content: center;
  align-items: center;
  gap: 0.5rem;
  flex-wrap: wrap;
}

.opt-btn {
  display: flex;
  flex-direction: column;
  align-items: center;
  font-size: 1rem;
  font-weight: 700;
  padding: 0.6rem 1.2rem;
  border: 3px solid var(--sea);
  border-radius: 14px;
  background: #fff;
  color: var(--sea);
  cursor: pointer;
}
.opt-btn small { font-weight: 500; font-size: 0.75rem; opacity: 0.85; }
.opt-btn.active { background: var(--sea); color: #fff; }

.reel-slot {
  display: flex;
  flex-direction: column;
  align-items: center;
  min-width: 6.5rem;
  padding: 0.6rem 1rem;
  border: 3px dashed var(--sea);
  border-radius: 14px;
  font-weight: 700;
  color: var(--sea);
  background: #fff;
}

/* Generic category picture behind the label, for players who cannot read
   the label yet. Sized generously, kept faint so the word still wins for
   players who can. */
.opt-btn, .reel-slot {
  position: relative;
  overflow: hidden;
  justify-content: center;
  min-height: 4.4rem;
  min-width: 8rem;
}

.opt-bg {
  position: absolute;
  left: 50%;
  top: 50%;
  transform: translate(-50%, -50%);
  width: 96%;
  height: 96%;
  opacity: 0.4;
  pointer-events: none;
}

.opt-btn.active .opt-bg { opacity: 0.5; }

.opt-label { position: relative; z-index: 1; }

#swap-reels {
  font-size: 1.3rem;
  width: 46px;
  height: 46px;
  border-radius: 50%;
  border: none;
  background: var(--accent);
  color: var(--ink);
  cursor: pointer;
}

#quiz-start, #quiz-again {
  font-size: 1.15rem;
  font-weight: 800;
  padding: 0.7rem 2.4rem;
  border: none;
  border-radius: 999px;
  background: var(--accent);
  color: var(--ink);
  cursor: pointer;
}

#best-score {
  margin-top: 1rem;
  font-size: 0.98rem;
  color: #6a8199;
}

/* ---------- Play ---------- */

#play-bar {
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 0.75rem;
  flex-wrap: wrap;
  margin: 0 0 1rem;
}

#quiz-progress {
  text-align: center;
  font-weight: 700;
  color: #33506b;
  margin: 0;
}

#restart-btn {
  font-size: 0.9rem;
  font-weight: 700;
  padding: 0.35rem 0.9rem;
  border: 2px solid var(--sea);
  border-radius: 999px;
  background: #fff;
  color: var(--sea);
  cursor: pointer;
}

#hint-btn {
  font-size: 0.9rem;
  font-weight: 800;
  padding: 0.35rem 1rem;
  border: 2px solid var(--accent);
  border-radius: 999px;
  background: var(--accent);
  color: var(--ink);
  cursor: pointer;
}
#hint-btn[hidden] { display: none; }

/* A reel currently showing help, so it doesn't read as a normal guess. */
.guess-reel.hinted { border-color: var(--accent); }

/* The prompt's own arrows pick which item to attempt. They grey out once
   a guess is committed — see updatePromptNav(). */
.prompt-reel .scroll-window { cursor: pointer; }
.prompt-reel.locked .scroll-btn { opacity: 0.25; }

/* 264px is the measured floor for a legible reel: two 42px arrows plus
   gaps and padding (~124px) around a ~140px image window. Below that the
   silhouettes stop being tellable apart, so reels wrap instead of
   shrinking — three across on a tablet in landscape, two in portrait. */
#reels {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(264px, 1fr));
  gap: 1rem;
  align-items: start;
}
@media (max-width: 560px) {
  #reels { grid-template-columns: 1fr; }
}

.reel {
  position: relative;   /* anchors the ✓ / ✗ verdict mark */
  background: var(--card);
  border-radius: 16px;
  padding: 0.9rem;
  text-align: center;
  box-shadow: 0 4px 14px rgba(11,37,64,0.12);
  border: 4px solid transparent;
}
.reel h3 {
  margin: 0 0 0.6rem;
  font-size: 0.95rem;
  text-transform: uppercase;
  letter-spacing: 0.05em;
  color: #6a8199;
}

.prompt-reel { border-color: var(--accent); }

/* Below three columns the prompt spans the full width instead of
   leaving a hole in the grid, so the two guess reels stay side by side
   on a tablet in portrait. */
@media (max-width: 900px) {
  .prompt-reel { grid-column: 1 / -1; }
}

/* The prompt has to stay on screen while the player works the other
   reels — on a phone the reels stack, so without this the thing being
   asked about scrolls away exactly when it's needed. */
@media (max-width: 760px) {
  .prompt-reel {
    position: sticky;
    top: 0;
    z-index: 5;
    background: var(--card);
    display: flex;
    align-items: center;
    justify-content: center;
    gap: 0.75rem;
    padding: 0.5rem 0.75rem;
  }
  .prompt-reel h3 { margin: 0; white-space: nowrap; }
  .prompt-reel .scroller { flex: 1; gap: 0.25rem; }
  .prompt-reel .scroll-window { min-height: 64px; flex: 1; }
  .prompt-reel .face-img { max-height: 60px; }
  .prompt-reel .scroll-btn { width: 36px; height: 36px; flex: 0 0 36px; font-size: 1rem; }
  .reel { padding: 0.6rem; }
  .scroll-window { min-height: 104px; }
}

/* ---------- How to play ---------- */

#help-overlay[hidden] { display: none; }

#help-overlay {
  position: fixed;
  inset: 0;
  z-index: 70;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 1rem;
  background: rgba(11, 37, 64, 0.55);
}

#help-panel {
  background: #fff;
  border-radius: 18px;
  width: min(560px, 100%);
  max-height: 90vh;
  overflow: auto;
  /* No bottom padding: it sits inside the scrolling area, below where the
     sticky action bar can reach, so a step scrolling past showed as a
     sliver under the buttons. The bar carries that padding instead. */
  padding: 1.25rem 1.4rem 0;
  box-shadow: 0 12px 40px rgba(11, 37, 64, 0.35);
}

#help-panel h2 { margin: 0 0 0.9rem; text-align: center; }

#help-steps {
  list-style: none;
  margin: 0;
  padding: 0;
  display: flex;
  flex-direction: column;
  gap: 0.55rem;
}

#help-steps li {
  display: flex;
  align-items: flex-start;
  gap: 0.7rem;
  padding: 0.6rem 0.7rem;
  border-radius: 12px;
  font-size: 1.05rem;
  line-height: 1.35;
  background: var(--sea-light);
  transition: background 0.2s ease, transform 0.2s ease;
}

/* The step currently being read aloud, so a non-reader can follow. */
#help-steps li.reading {
  background: var(--accent);
  transform: scale(1.02);
}

.help-icon { font-size: 1.3rem; line-height: 1.2; flex: 0 0 auto; }

#help-grownups {
  margin: 1rem 0 0;
  padding: 0.7rem 0.85rem;
  border-left: 4px solid var(--sea);
  background: #f4f8fb;
  border-radius: 6px;
  font-size: 0.92rem;
  color: #33506b;
}
#help-grownups a { color: var(--sea); font-weight: 700; }

/* Pinned to the foot of the panel: on a phone the steps overflow, and a
   "Let's go!" you have to scroll to find is a dead end for the exact
   person this dialog exists for. */
#help-actions {
  position: sticky;
  bottom: 0;
  display: flex;
  justify-content: center;
  gap: 0.6rem;
  flex-wrap: wrap;
  margin-top: 1.1rem;
  /* The fade has to finish before the buttons start, or a step scrolling
     underneath shows through the middle of them and reads as a rendering
     fault rather than as content passing behind. */
  padding: 1.5rem 0 1.4rem;
  background: linear-gradient(to bottom, rgba(255,255,255,0), #fff 55%);
}

#help-read, #help-close {
  font-size: 1.05rem;
  font-weight: 800;
  padding: 0.6rem 1.4rem;
  border-radius: 999px;
  cursor: pointer;
  border: 3px solid var(--sea);
}
#help-read { background: #fff; color: var(--sea); }
#help-close { background: var(--accent); border-color: var(--accent); color: var(--ink); }

/* Subtitle pill for the spoken line. */
/* Anchored to the bottom: the top is taken by the title, and on a phone
   by the sticky prompt. */
#toast {
  position: fixed;
  left: 50%;
  bottom: 1.25rem;
  transform: translate(-50%, 12px);
  z-index: 60;
  padding: 0.5rem 1.1rem;
  border-radius: 999px;
  background: var(--ink);
  color: #fff;
  font-weight: 700;
  font-size: 1.05rem;
  box-shadow: 0 6px 20px rgba(11, 37, 64, 0.3);
  opacity: 0;
  transition: opacity 0.2s ease, transform 0.2s ease;
  pointer-events: none;
  max-width: 92vw;
  text-align: center;
}
#toast[hidden] { display: none; }
#toast.show { opacity: 1; transform: translate(-50%, 0); }

/* Verdict marks on a reel — shown whether or not sound is on. */
.mark {
  position: absolute;
  top: 6px;
  right: 8px;
  font-size: 1.9rem;
  font-weight: 900;
  line-height: 1;
  pointer-events: none;
  animation: mark-pop 1.1s ease-out forwards;
}
.mark-hit { color: var(--good); }
.mark-miss { color: var(--bad); }

@keyframes mark-pop {
  0%   { opacity: 0; transform: scale(0.5); }
  18%  { opacity: 1; transform: scale(1.15); }
  32%  { transform: scale(1); }
  75%  { opacity: 1; }
  100% { opacity: 0; transform: scale(1); }
}

.pip {
  display: inline-block;
  width: 11px;
  height: 11px;
  margin: 0 3px;
  border-radius: 50%;
  background: var(--good);
  vertical-align: middle;
}
.pip.spent { background: #d9e2ea; }
.guess-reel.solved { border-color: var(--good); }
.guess-reel.failed { border-color: var(--bad); }

.scroller {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 0.35rem;
}

.scroll-btn {
  font-size: 1.2rem;
  width: 42px;
  height: 42px;
  flex: 0 0 42px;
  border-radius: 50%;
  border: none;
  background: var(--sea-light);
  color: var(--ink);
  cursor: pointer;
}
.scroll-btn:disabled { opacity: 0.4; cursor: default; }

.scroll-window {
  flex: 1;
  min-height: 130px;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 0.35rem;
  overflow: hidden;
}

.face-img { width: 100%; max-height: 110px; }
.face-name { font-size: clamp(1.05rem, 3vw, 1.4rem); font-weight: 800; }
.face-caption { font-size: 0.95rem; font-weight: 700; color: var(--sea); }

.tries { margin: 0.5rem 0; font-size: 0.88rem; color: #6a8199; min-height: 1.1rem; }

.pick-btn {
  font-size: 1rem;
  font-weight: 700;
  padding: 0.5rem 1.2rem;
  border: none;
  border-radius: 999px;
  background: var(--sea);
  color: #fff;
  cursor: pointer;
}
.pick-btn:disabled { opacity: 0.45; cursor: default; }

#quiz-feedback {
  text-align: center;
  font-size: 1.15rem;
  font-weight: 700;
  min-height: 1.75rem;
  margin-top: 1rem;
}

#quiz-result { text-align: center; padding: 2rem 0; }
#quiz-score { font-size: 1.5rem; font-weight: 800; margin-bottom: 0.5rem; }
#quiz-best-note { color: var(--good); font-weight: 700; margin-bottom: 1.25rem; }

.shake { animation: shake 0.4s; }
@keyframes shake {
  0%, 100% { transform: translateX(0); }
  25% { transform: translateX(-6px); }
  75% { transform: translateX(6px); }
}
