:root {
  --bg: #f4f4f7;
  --fg: #222;
  --muted: #666;
  --cell-bg: #fff;
  --x: #2563eb;
  --o: #dc2626;
  --panel-bg: #fff;
  --highlight: #fef3c7;
  --turn-bg: #bbf7d0;
  --turn-accent: #16a34a;
  --overlay-bg: rgba(24, 24, 27, 0.4);
  --cell-size: 96px;
}

@media (prefers-color-scheme: dark) {
  :root {
    --bg: #18181b;
    --fg: #f4f4f5;
    --muted: #a1a1aa;
    --cell-bg: #27272a;
    --x: #60a5fa;
    --o: #f87171;
    --panel-bg: #27272a;
    --highlight: #3f3a1e;
    --turn-bg: #14532d;
    --turn-accent: #4ade80;
    --overlay-bg: rgba(0, 0, 0, 0.5);
  }
}

* {
  box-sizing: border-box;
}

body {
  margin: 0;
  padding: 24px 16px;
  font-family: system-ui, sans-serif;
  min-height: 100vh;
  background: var(--bg);
  color: var(--fg);
  transition: background-color 0.25s ease;
}

/* The whole screen turns green when it's this player's move. */
body.my-turn {
  background: var(--turn-bg);
}

body.my-turn .status {
  color: var(--turn-accent);
  font-weight: 700;
}

body.my-turn .board {
  border-radius: 12px;
  box-shadow: 0 0 0 4px var(--turn-accent);
}

main {
  display: flex;
  flex-direction: column;
  align-items: center;
}

h1 {
  margin: 0 0 8px;
}

.status {
  min-height: 1.5em;
  margin: 8px 0 4px;
  font-size: 1.2rem;
  text-align: center;
}

.detail {
  min-height: 1.2em;
  margin: 0 0 16px;
  color: var(--muted);
}

.board {
  display: grid;
  grid-template-columns: repeat(3, var(--cell-size));
  grid-template-rows: repeat(3, var(--cell-size));
  gap: 6px;
}

.board-wrap {
  position: relative;
}

/* Covers the board while watching from the line, so it's clear you can't move. */
.spectate-overlay {
  position: absolute;
  inset: -6px;
  display: flex;
  align-items: center;
  justify-content: center;
  border-radius: 12px;
  background: var(--overlay-bg);
  pointer-events: none;
}

.spectate-overlay[hidden] {
  display: none;
}

.spectate-overlay__label {
  padding: 8px 16px;
  border-radius: 999px;
  background: var(--panel-bg);
  color: var(--fg);
  font-weight: 700;
  box-shadow: 0 2px 6px rgba(0, 0, 0, 0.25);
}

.board--inactive {
  opacity: 0.35;
}

.cell {
  border: none;
  border-radius: 8px;
  background: var(--cell-bg);
  color: var(--fg);
  font-size: 3rem;
  font-weight: bold;
  cursor: pointer;
  box-shadow: 0 1px 3px rgba(0, 0, 0, 0.15);
}

.cell:disabled {
  cursor: default;
}

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

.cell--X {
  color: var(--x);
}

.cell--O {
  color: var(--o);
}

.me {
  margin: 0 0 4px;
  font-weight: 600;
}

.matchup {
  min-height: 1.4em;
  margin: 4px 0;
  text-align: center;
}

.matchup__player {
  padding: 2px 6px;
  border-radius: 6px;
}

.matchup__player--turn {
  background: var(--highlight);
  font-weight: 600;
}

.line {
  width: 100%;
  max-width: 320px;
  margin-top: 24px;
  padding: 12px 16px;
  border-radius: 8px;
  background: var(--panel-bg);
  box-shadow: 0 1px 3px rgba(0, 0, 0, 0.15);
}

.line h2 {
  margin: 0 0 8px;
  font-size: 1rem;
}

.line__list {
  margin: 0;
  padding-left: 1.5em;
}

.line__list li {
  padding: 2px 0;
}

.line__you {
  font-weight: 600;
}

.line__empty {
  margin: 0;
  color: var(--muted);
}

.line__empty[hidden],
.line__list[hidden] {
  display: none;
}

/* ---------- Win / lose animations ----------
 * body.game-won / body.game-lost are set by client.js for the two players when
 * a game ends with a winner. Cells in the winning line get .cell--win.
 */

:root {
  --win-accent: #f59e0b;
  --win-bg: #fef9c3;
  --lose-bg: #e4e4e7;
}

@media (prefers-color-scheme: dark) {
  :root {
    --win-accent: #fbbf24;
    --win-bg: #422006;
    --lose-bg: #0f0f11;
  }
}

/* Winning line: every player sees it pop, staggered across the three cells. */
.cell--win {
  position: relative;
  animation: win-cell-pop 0.6s cubic-bezier(0.34, 1.56, 0.64, 1) both;
  box-shadow: 0 0 0 3px currentColor, 0 0 18px currentColor;
}

@keyframes win-cell-pop {
  0% { transform: scale(1); }
  40% { transform: scale(1.25) rotate(-4deg); }
  70% { transform: scale(0.95) rotate(2deg); }
  100% { transform: scale(1.08); }
}

/* --- Win --- */
body.game-won {
  background: var(--win-bg);
}

body.game-won .status {
  color: var(--win-accent);
  font-weight: 800;
  animation: win-status 0.8s cubic-bezier(0.34, 1.56, 0.64, 1) both,
    win-glow 1.6s ease-in-out 0.8s infinite alternate;
}

body.game-won .board {
  border-radius: 12px;
  animation: win-board 0.9s ease-out both;
}

body.game-won .cell--win {
  animation: win-cell-pop 0.6s cubic-bezier(0.34, 1.56, 0.64, 1) both,
    win-cell-pulse 1.2s ease-in-out 0.6s infinite alternate;
}

@keyframes win-status {
  0% { transform: scale(0.4) translateY(10px); opacity: 0; }
  60% { transform: scale(1.35) translateY(-6px); opacity: 1; }
  100% { transform: scale(1.2); }
}

@keyframes win-glow {
  from { text-shadow: 0 0 0 transparent; transform: scale(1.2); }
  to { text-shadow: 0 0 14px var(--win-accent); transform: scale(1.28); }
}

@keyframes win-board {
  0% { box-shadow: 0 0 0 0 var(--win-accent); }
  50% { box-shadow: 0 0 0 10px var(--win-accent); }
  100% { box-shadow: 0 0 0 4px var(--win-accent); }
}

@keyframes win-cell-pulse {
  from { transform: scale(1.08); filter: brightness(1); }
  to { transform: scale(1.14); filter: brightness(1.25); }
}

/* Confetti rains down only for the winner. */
.confetti {
  position: fixed;
  inset: 0;
  overflow: hidden;
  pointer-events: none;
  display: none;
}

body.game-won .confetti {
  display: block;
}

.confetti__piece {
  position: absolute;
  top: -20px;
  left: var(--x);
  width: 10px;
  height: 16px;
  border-radius: 2px;
  background: hsl(var(--hue) 90% 55%);
  opacity: 0;
  animation: confetti-fall 2.6s cubic-bezier(0.25, 0.6, 0.5, 1) var(--delay) forwards;
}

.confetti__piece:nth-child(3n) {
  width: 8px;
  height: 8px;
  border-radius: 50%;
}

@keyframes confetti-fall {
  0% { transform: translate(0, 0) rotate(0); opacity: 1; }
  85% { opacity: 1; }
  100% { transform: translate(var(--drift), 105vh) rotate(var(--spin)); opacity: 0; }
}

/* --- Lose --- */
body.game-lost {
  background: var(--lose-bg);
}

body.game-lost .status {
  color: var(--muted);
  animation: lose-status 1s ease-in both;
}

body.game-lost .board {
  animation: lose-shake 0.5s cubic-bezier(0.36, 0.07, 0.19, 0.97) both,
    lose-fade 0.8s ease-out 0.5s forwards;
}

/* The opponent's winning line stays vivid while the rest of the board fades. */
body.game-lost .cell:not(.cell--win) {
  animation: lose-cell-grey 0.8s ease-out 0.5s forwards;
}

body.game-lost .cell--win {
  animation: lose-cell-thud 0.5s ease-out 0.3s both;
  box-shadow: 0 0 0 3px currentColor;
}

@keyframes lose-shake {
  10%, 90% { transform: translateX(-2px); }
  20%, 80% { transform: translateX(4px); }
  30%, 50%, 70% { transform: translateX(-8px) rotate(-1deg); }
  40%, 60% { transform: translateX(8px) rotate(1deg); }
}

@keyframes lose-fade {
  to { transform: scale(0.96); }
}

@keyframes lose-cell-grey {
  to { filter: grayscale(1); opacity: 0.45; }
}

@keyframes lose-cell-thud {
  0% { transform: scale(1.2); }
  60% { transform: scale(0.92); }
  100% { transform: scale(1); }
}

@keyframes lose-status {
  0% { transform: translateY(-12px); opacity: 0; }
  50% { transform: translateY(6px) rotate(-2deg); opacity: 1; }
  75% { transform: translateY(3px) rotate(1deg); }
  100% { transform: translateY(4px) rotate(0); }
}

/* Keep the colours, drop the movement, for people who ask for less motion. */
@media (prefers-reduced-motion: reduce) {
  .cell--win,
  body.game-won .status,
  body.game-won .board,
  body.game-won .cell--win,
  body.game-lost .status,
  body.game-lost .board,
  body.game-lost .cell--win {
    animation: none;
  }

  body.game-won .confetti {
    display: none;
  }

  body.game-lost .cell:not(.cell--win) {
    animation: none;
    filter: grayscale(1);
    opacity: 0.45;
  }
}
