/* ============================================================
   XOX — noughts and crosses with real faces
   ============================================================ */

:root {
  --bg:        #1b1033;
  --bg-2:      #2b1657;
  --ink:       #fff6e5;
  --pa:        #ffd23f;   /* seat A — roster overrides this at boot */
  --pb:        #4ecdc4;   /* seat B — roster overrides this at boot */
  --hot:       #ff5d8f;
  --line:      rgba(255, 246, 229, 0.16);
  --cell:      rgba(255, 255, 255, 0.06);
  --shadow:    0 10px 0 rgba(0,0,0,.28);
}

* { box-sizing: border-box; -webkit-tap-highlight-color: transparent; }

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

body {
  font-family: 'Nunito', system-ui, -apple-system, sans-serif;
  color: var(--ink);
  background:
    radial-gradient(120% 80% at 50% -10%, var(--bg-2) 0%, var(--bg) 62%),
    var(--bg);
  user-select: none;
}

/* confetti-ish background dots */
body::before {
  content: '';
  position: fixed;
  inset: 0;
  background-image:
    radial-gradient(3px 3px at 12% 22%, var(--pa) 50%, transparent 51%),
    radial-gradient(3px 3px at 82% 14%, var(--pb) 50%, transparent 51%),
    radial-gradient(2px 2px at 68% 78%, var(--hot) 50%, transparent 51%),
    radial-gradient(2px 2px at 24% 84%, var(--pb) 50%, transparent 51%),
    radial-gradient(2px 2px at 92% 56%, var(--pa) 50%, transparent 51%),
    radial-gradient(2px 2px at 6% 58%, var(--hot) 50%, transparent 51%);
  opacity: .5;
  pointer-events: none;
  animation: drift 14s ease-in-out infinite alternate;
}
@keyframes drift { to { transform: translateY(-14px); } }

/* The roster arrives over fetch, so the page starts blank rather than
   flashing the default names and faces for a frame. */
body:not(.is-ready) #app { visibility: hidden; }

#app {
  position: relative;
  height: 100%;
  padding: 0;
}
@supports (height: 100dvh) {
  #app { height: 100dvh; }
}

/* ---------- screens ---------- */
.screen {
  position: absolute;
  inset: 0;
  display: none;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 22px;
  padding:
    calc(24px + env(safe-area-inset-top, 0px))
    calc(20px + env(safe-area-inset-right, 0px))
    calc(24px + env(safe-area-inset-bottom, 0px))
    calc(20px + env(safe-area-inset-left, 0px));
  text-align: center;
}
.screen.is-active { display: flex; animation: screenIn .32s ease both; }
@keyframes screenIn { from { opacity: 0; transform: translateY(10px) scale(.98); } }

/* ---------- title ---------- */
.logo {
  margin: 0;
  font-family: 'Luckiest Guy', cursive;
  font-weight: 400;
  letter-spacing: .02em;
  line-height: .92;
  font-size: clamp(2.6rem, 13vw, 5.4rem);
  display: flex;
  flex-direction: column;
}
.logo-word { display: block; text-shadow: 0 5px 0 rgba(0,0,0,.35); }
.logo-word--a     { color: var(--pa);   transform: rotate(-3deg); }
.logo-word--tac   { color: var(--ink);   font-size: .62em; letter-spacing: .18em; }
.logo-word--b   { color: var(--pb); transform: rotate(2.5deg); }

.tagline {
  margin: -6px 0 0;
  font-weight: 700;
  opacity: .6;
  letter-spacing: .12em;
  text-transform: uppercase;
  font-size: .8rem;
}

.title-faces { display: flex; gap: clamp(10px, 6vw, 40px); align-items: flex-end; }
.title-face {
  width: clamp(84px, 26vw, 150px);
  aspect-ratio: 1;
  object-fit: contain;
  filter: drop-shadow(0 8px 12px rgba(0,0,0,.45));
}
.title-face--left  { animation: bobA 2.4s ease-in-out infinite; }
.title-face--right { animation: bobB 2.4s ease-in-out infinite .3s; }
@keyframes bobA { 0%,100% { transform: translateY(0) rotate(-6deg); } 50% { transform: translateY(-12px) rotate(-2deg); } }
@keyframes bobB { 0%,100% { transform: translateY(0) rotate(6deg); }  50% { transform: translateY(-12px) rotate(2deg); } }

/* ---------- buttons ---------- */
.heading {
  font-family: 'Luckiest Guy', cursive;
  font-weight: 400;
  font-size: clamp(1.5rem, 7vw, 2.4rem);
  margin: 0;
  text-shadow: 0 4px 0 rgba(0,0,0,.3);
}

.stack { display: flex; flex-direction: column; gap: 14px; width: min(340px, 88vw); }

.btn {
  font-family: 'Luckiest Guy', cursive;
  font-size: clamp(1.05rem, 4.6vw, 1.4rem);
  letter-spacing: .04em;
  color: #2a1350;
  background: var(--ink);
  border: 0;
  border-radius: 18px;
  padding: 16px 26px;
  cursor: pointer;
  box-shadow: var(--shadow);
  transition: transform .12s ease, box-shadow .12s ease, filter .12s ease;
  display: flex;
  flex-direction: column;
  gap: 2px;
  align-items: center;
  width: 100%;
}
.btn small {
  font-family: 'Nunito', sans-serif;
  font-weight: 700;
  font-size: .68rem;
  letter-spacing: .04em;
  text-transform: uppercase;
  opacity: .55;
}
.btn:active { transform: translateY(6px); box-shadow: 0 4px 0 rgba(0,0,0,.28); }
.btn--big { width: min(300px, 78vw); background: var(--hot); color: #fff; font-size: clamp(1.4rem, 6vw, 2rem); padding: 18px 32px; }
.btn--ghost {
  background: transparent;
  color: var(--ink);
  box-shadow: none;
  opacity: .6;
  font-family: 'Nunito', sans-serif;
  font-weight: 900;
  text-transform: uppercase;
  letter-spacing: .12em;
  font-size: .78rem;
  width: auto;
  padding: 10px 18px;
  min-height: 44px;
}
button { touch-action: manipulation; }
.btn--ghost:active { transform: none; opacity: 1; }

/* ---------- picker ---------- */
.picker { display: flex; gap: clamp(12px, 5vw, 28px); flex-wrap: wrap; justify-content: center; }
.pick-card {
  background: var(--cell);
  border: 3px solid var(--line);
  border-radius: 26px;
  padding: 18px 16px 14px;
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 10px;
  cursor: pointer;
  color: var(--ink);
  font-family: 'Luckiest Guy', cursive;
  font-size: 1.2rem;
  letter-spacing: .06em;
  transition: transform .14s ease, border-color .14s ease, background .14s ease;
  width: clamp(130px, 40vw, 168px);
}
.pick-card img { width: 100%; aspect-ratio: 1; object-fit: contain; filter: drop-shadow(0 6px 8px rgba(0,0,0,.4)); }
.pick-card:active { transform: scale(.95); }
@media (hover: hover) and (pointer: fine) {
  .pick-card[data-pick="a"]:hover   { border-color: var(--pa);   background: rgba(255,210,63,.12); }
  .pick-card[data-pick="b"]:hover { border-color: var(--pb); background: rgba(78,205,196,.12); }
}
.pick-card[data-pick="a"]:is(:active, :focus-visible)   { border-color: var(--pa);   background: rgba(255,210,63,.12); }
.pick-card[data-pick="b"]:is(:active, :focus-visible) { border-color: var(--pb); background: rgba(78,205,196,.12); }

/* ---------- game ---------- */
.screen--game {
  justify-content: center;
  gap: clamp(20px, 5vh, 44px);
  padding-top: calc(18px + env(safe-area-inset-top, 0px));
}
.screen--game .scoreboard { margin-bottom: auto; }
.screen--game .game-actions { margin-top: auto; }

.scoreboard {
  display: flex;
  align-items: center;
  justify-content: center;
  gap: clamp(14px, 6vw, 40px);
  width: min(420px, 92vw);
}
.score { display: flex; align-items: center; gap: 8px; opacity: .45; transition: opacity .2s ease, transform .2s ease; }
.score.is-turn { opacity: 1; transform: scale(1.08); }
.score img { width: clamp(38px, 12vw, 54px); aspect-ratio: 1; object-fit: contain; }
.score-num { font-family: 'Luckiest Guy', cursive; font-size: 1.5rem; }
.score-mid { display: flex; flex-direction: column; gap: 2px; min-width: 96px; }
.score-mid span {
  font-family: 'Luckiest Guy', cursive;
  font-size: .95rem;
  letter-spacing: .04em;
}
.score-mid small { font-size: .62rem; text-transform: uppercase; letter-spacing: .14em; opacity: .45; font-weight: 900; }

.board {
  display: grid;
  grid-template-columns: repeat(3, 1fr);
  grid-template-rows: repeat(3, 1fr);
  gap: clamp(8px, 2.4vw, 14px);
  width: min(88vw, 60vh, 440px);
  aspect-ratio: 1;
}
.cell {
  border: 0;
  border-radius: 20px;
  background: var(--cell);
  box-shadow: inset 0 0 0 3px var(--line);
  cursor: pointer;
  padding: 8%;
  display: grid;
  place-items: center;
  transition: background .15s ease;
}
.cell:not(.is-filled):active { background: rgba(255,255,255,.14); }
.cell img { width: 100%; height: 100%; object-fit: contain; filter: drop-shadow(0 4px 6px rgba(0,0,0,.45)); }
.cell.is-filled { cursor: default; }
.cell.just-placed img { animation: pop .34s cubic-bezier(.2,1.5,.4,1) both; }
@keyframes pop { from { transform: scale(0) rotate(-40deg); } 60% { transform: scale(1.18) rotate(8deg); } to { transform: scale(1) rotate(0); } }

.cell.is-win { background: rgba(255,255,255,.14); box-shadow: inset 0 0 0 4px currentColor; }
.cell.is-win img { animation: shake .6s ease-in-out infinite; }
.cell.is-lose img { animation: flop .6s cubic-bezier(.3,1.4,.5,1) both; }

@keyframes shake {
  0%, 100% { transform: translateX(0) rotate(-7deg) scale(1.06); }
  25%      { transform: translateX(-4px) rotate(7deg) scale(1.1); }
  50%      { transform: translateX(4px) rotate(-5deg) scale(1.06); }
  75%      { transform: translateX(-3px) rotate(6deg) scale(1.1); }
}
@keyframes flop { to { transform: scale(.82); filter: grayscale(1) drop-shadow(0 4px 6px rgba(0,0,0,.45)); } }
@keyframes wobble {
  0%, 100% { transform: rotate(-9deg); }
  50%      { transform: rotate(9deg); }
}

.game-actions {
  padding-bottom: 6px;
  display: flex;
  align-items: center;
  gap: 14px;
}

/* ---------- coach ----------
   The screen is a fixed-height column with overflow hidden, so the coach
   panel has to buy its space from the board rather than push the scoreboard
   and Quit off the top and bottom.                                        */
.screen--game.has-coach { gap: clamp(12px, 2.4vh, 22px); }
.screen--game.has-coach .board { width: min(88vw, 40vh, 360px); }

.coach {
  width: min(520px, 92vw);
  background: rgba(255, 246, 229, .1);
  border: 3px solid var(--line);
  border-radius: 22px;
  padding: 16px 18px;
  display: flex;
  flex-direction: column;
  gap: 12px;
  animation: coachIn .26s cubic-bezier(.3, 1.4, .5, 1) both;
}
.coach[hidden] { display: none; }
@keyframes coachIn { from { opacity: 0; transform: translateY(8px) scale(.97); } }

.coach-text {
  margin: 0;
  font-size: clamp(1.3rem, 5.2vw, 1.7rem);
  line-height: 1.35;
  font-weight: 700;
  text-wrap: balance;
}
.coach-text b { font-weight: 900; color: var(--hot); }

/* The computer thinking out loud — teal, no buttons, clearly not a hint. */
.coach.is-narration {
  border-color: rgba(78, 205, 196, .5);
  background: rgba(78, 205, 196, .1);
}
.coach.is-narration .coach-text { font-weight: 700; }

.coach-actions { display: flex; align-items: center; gap: 10px; }
.coach-actions[hidden] { display: none; }

.btn--coach {
  background: var(--hot);
  color: #fff;
  font-size: 1rem;
  padding: 12px 22px;
  width: auto;
}

.btn--help {
  background: transparent;
  color: var(--ink);
  border: 3px solid var(--line);
  box-shadow: none;
  font-size: .9rem;
  padding: 10px 20px;
  width: auto;
  min-height: 44px;
}
.btn--help:disabled { opacity: .28; cursor: default; }
.btn--help:active { transform: none; }

.btn--mute {
  background: transparent;
  border: 3px solid var(--line);
  box-shadow: none;
  font-size: 1.15rem;
  line-height: 1;
  padding: 10px 14px;
  width: auto;
  min-height: 44px;
  min-width: 48px;
}
.btn--mute:active { transform: none; }
.btn--mute.is-muted { opacity: .5; }

/* Suggested square, once he taps SHOW ME. It glows; it never plays itself. */
.cell.is-hint {
  box-shadow: inset 0 0 0 4px var(--hot);
  animation: hintPulse 1s ease-in-out infinite;
}
@keyframes hintPulse {
  0%, 100% { background: rgba(255, 93, 143, .12); }
  50%      { background: rgba(255, 93, 143, .3); }
}

/* ---------- overlay ---------- */
.overlay {
  position: absolute;
  inset: 0;
  z-index: 20;
  display: grid;
  place-items: center;
  background: rgba(12, 6, 26, .84);
  backdrop-filter: blur(10px);
  animation: screenIn .28s ease both;
  padding:
    calc(24px + env(safe-area-inset-top, 0px))
    calc(24px + env(safe-area-inset-right, 0px))
    calc(24px + env(safe-area-inset-bottom, 0px))
    calc(24px + env(safe-area-inset-left, 0px));
}
.overlay[hidden] { display: none; }
.overlay-inner { display: flex; flex-direction: column; align-items: center; gap: 22px; text-align: center; }

.result-faces { display: flex; gap: clamp(12px, 6vw, 34px); align-items: center; }
.result-face {
  width: clamp(96px, 30vw, 168px);
  aspect-ratio: 1;
  object-fit: contain;
  filter: drop-shadow(0 10px 14px rgba(0,0,0,.5));
}
.result-face.is-winner { animation: shake .58s ease-in-out infinite; }
.result-face.is-loser  { transform: scale(.8); filter: grayscale(1) drop-shadow(0 10px 14px rgba(0,0,0,.5)); }
.result-face.is-tie    { animation: wobble 1s ease-in-out infinite; }
.result-face.is-tie:nth-child(2) { animation-delay: .5s; }

.result-text {
  margin: 0;
  font-family: 'Luckiest Guy', cursive;
  font-weight: 400;
  font-size: clamp(1.8rem, 9vw, 3.2rem);
  text-shadow: 0 5px 0 rgba(0,0,0,.35);
  line-height: 1;
}
.result-text.a     { color: var(--pa); }
.result-text.b   { color: var(--pb); }
.result-text.tie   { color: var(--hot); }

@media (prefers-reduced-motion: reduce) {
  *, *::before, *::after {
    animation-duration: .01ms !important;
    animation-iteration-count: 1 !important;
    transition-duration: .01ms !important;
  }
}

/* ---------- tablet ---------- */
@media (min-width: 700px) and (min-height: 600px) {
  .board {
    width: min(76vw, 66vh, 560px);
    width: min(76vw, 66dvh, 560px);
  }
  .screen--game.has-coach .board {
    width: min(70vw, 52vh, 460px);
    width: min(70vw, 52dvh, 460px);
  }
  .screen--game .scoreboard  { margin-bottom: 0; }
  .screen--game .game-actions { margin-top: 0; }

  .title-face  { width: 180px; }
  .logo        { font-size: 6rem; }
  .stack       { width: min(380px, 80vw); }
  .pick-card   { width: 190px; }
  .result-face { width: clamp(178px, 24vmin, 220px); }
  .result-text { font-size: clamp(3.2rem, 6vmin, 4rem); }
  .coach       { width: min(600px, 86vw); padding: 20px 24px; }
  .coach-text  { font-size: clamp(1.6rem, 2.8vmin, 2rem); }
  .btn--coach  { font-size: 1.15rem; padding: 14px 26px; }
  .btn--help   { font-size: 1rem; }
}

/* ---------------- code entry ----------------
   Only on the preset roster's title screen. Someone who was handed a link
   never needs this; someone who typed the bare address does.              */
.code { margin-top: 14px; display: flex; flex-direction: column; align-items: center; gap: 10px; }
.code-form { display: flex; gap: 8px; align-items: center; }
/* A `display` of anything but none beats the `hidden` attribute, so these
   have to opt back out of it explicitly or the form shows before it's asked
   for. */
.code[hidden], .code-form[hidden] { display: none; }
.code-form input {
  font-family: 'Nunito', system-ui, sans-serif;
  font-weight: 700;
  font-size: 1.05rem;
  padding: 12px 14px;
  min-height: 44px;
  width: min(58vw, 220px);
  color: var(--bg);
  background: rgba(255,255,255,.92);
  border: 3px solid rgba(255,255,255,.35);
  border-radius: 14px;
  text-align: center;
}
.code-form input:focus-visible { outline: none; border-color: var(--pa); }
.code-error { margin: 0; font-weight: 700; font-size: .95rem; color: #ff8f8f; }
