:root {
  --bg: #f8f7f4;
  --surface: #ffffff;
  --surface-muted: #efede8;
  --border: #dedad2;
  --border-strong: #c8c3b8;
  --text: #1c1b19;
  --text-muted: #66635c;
  --accent: #1f5eff;
  --accent-contrast: #ffffff;
  --p0-color: #d93829;
  --p0-hi: #ff6b5b;
  --p0-soft: #fdebe9;
  --p1-color: #c98a00;
  --p1-disc: #f5b800;
  --p1-hi: #ffe074;
  --p1-soft: #fef6dc;
  --c4-frame: #23498d;
  --c4-frame-dark: #173263;
  --c4-slot: #e6e4de;
  --chess-light: #f3efe6;
  --chess-dark: #b99976;
  --chess-select: rgba(31, 94, 255, 0.35);
  --chess-last: rgba(245, 184, 0, 0.34);
  --chess-check: #e03131;
  --chess-white-fill: #ffffff;
  --chess-white-stroke: #22201d;
  --chess-black-fill: #24221f;
  --chess-black-stroke: #f8f7f4;
  --win-ring: #179b52;
  --online: #179b52;
  --waiting: #d49600;
  --radius: 14px;
}

@media (prefers-color-scheme: dark) {
  :root {
    --bg: #141413;
    --surface: #1f1f1d;
    --surface-muted: #2a2a27;
    --border: #383834;
    --border-strong: #4a4a44;
    --text: #f3f2ee;
    --text-muted: #a2a099;
    --accent: #4c82ff;
    --p0-color: #ff6454;
    --p0-hi: #ff9488;
    --p0-soft: #3a1f1c;
    --p1-color: #f7c948;
    --p1-disc: #f2be24;
    --p1-hi: #ffe58f;
    --p1-soft: #383018;
    --c4-frame: #1f3f7a;
    --c4-frame-dark: #132850;
    --c4-slot: #141413;
    --chess-light: #e5dec9;
    --chess-dark: #8c6d4f;
    --chess-select: rgba(76, 130, 255, 0.42);
    --chess-last: rgba(247, 201, 72, 0.34);
    --chess-check: #ff5252;
    --win-ring: #32c671;
  }
}

*,
*::before,
*::after {
  box-sizing: border-box;
}

html,
body {
  margin: 0;
  padding: 0;
  background: var(--bg);
  color: var(--text);
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Helvetica, Arial, sans-serif;
  -webkit-font-smoothing: antialiased;
}

button,
#board {
  touch-action: manipulation;
  -webkit-tap-highlight-color: transparent;
  user-select: none;
  -webkit-user-select: none;
}

button {
  font: inherit;
  cursor: pointer;
}

button:disabled {
  cursor: default;
}

/* First-fold game viewport */
.arena {
  min-height: 100dvh;
  max-width: 500px;
  margin: 0 auto;
  padding: calc(env(safe-area-inset-top) + 12px) 16px calc(env(safe-area-inset-bottom) + 14px);
  display: flex;
  flex-direction: column;
  justify-content: space-between;
  gap: 10px;
}

.topbar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 8px;
}

.topbar-actions {
  display: flex;
  align-items: center;
  gap: 6px;
  flex-shrink: 0;
}

.room-pill {
  display: inline-flex;
  align-items: center;
  gap: 8px;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 999px;
  padding: 5px 8px 5px 12px;
  min-width: 0;
}

.room-dot {
  width: 9px;
  height: 9px;
  border-radius: 50%;
  background: var(--waiting);
  flex-shrink: 0;
  transition: background-color 0.2s ease;
}

.room-dot.online {
  background: var(--online);
}

.room-code {
  font-family: ui-monospace, SFMono-Regular, Menlo, monospace;
  font-size: 0.84rem;
  font-weight: 600;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.ghost-btn {
  background: var(--surface-muted);
  color: var(--text-muted);
  border: none;
  border-radius: 999px;
  padding: 4px 10px;
  font-size: 0.76rem;
  font-weight: 600;
  white-space: nowrap;
}

.ghost-btn:hover {
  color: var(--text);
}

.ghost-btn.attention {
  background: var(--accent);
  color: var(--accent-contrast);
}

.icon-btn {
  background: var(--surface);
  color: var(--text);
  border: 1px solid var(--border);
  border-radius: 999px;
  width: 35px;
  height: 35px;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  font-size: 0.95rem;
  padding: 0;
}

.primary-btn {
  background: var(--accent);
  color: var(--accent-contrast);
  border: none;
  border-radius: 999px;
  padding: 8px 16px;
  font-size: 0.88rem;
  font-weight: 600;
  white-space: nowrap;
}

.secondary-btn {
  background: var(--surface);
  color: var(--text);
  border: 1px solid var(--border);
  border-radius: 999px;
  padding: 7px 15px;
  font-size: 0.85rem;
  font-weight: 600;
}

.secondary-btn:disabled {
  opacity: 0.45;
}

/* Segmented game switcher */
.game-selector {
  display: grid;
  grid-template-columns: repeat(3, 1fr);
  background: var(--surface-muted);
  padding: 4px;
  border-radius: 11px;
  gap: 4px;
}

.seg-btn {
  border: none;
  background: transparent;
  color: var(--text-muted);
  padding: 7px 8px;
  border-radius: 8px;
  font-size: 0.85rem;
  font-weight: 600;
  white-space: nowrap;
}

.seg-btn.active {
  background: var(--surface);
  color: var(--text);
  box-shadow: 0 1px 2px rgba(0, 0, 0, 0.08);
}

/* Score bar & seat cards */
.score-bar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 6px;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 6px 8px;
}

.seat-card {
  display: flex;
  align-items: center;
  gap: 8px;
  font-size: 0.88rem;
  padding: 5px 9px;
  border-radius: 10px;
  border: 2px solid transparent;
  min-width: 0;
  transition: border-color 0.18s ease, background-color 0.18s ease;
}

.seat-card.is-me {
  cursor: pointer;
}

.seat-card.is-me .seat-label {
  color: var(--text);
  font-weight: 700;
  text-decoration: underline dotted var(--border-strong);
  text-underline-offset: 3px;
}

#seat-0.active-turn {
  border-color: var(--p0-color);
  background: var(--p0-soft);
}

#seat-1.active-turn {
  border-color: var(--p1-color);
  background: var(--p1-soft);
}

.seat-marker {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  width: 24px;
  height: 24px;
  border-radius: 50%;
  font-weight: 800;
  font-size: 0.82rem;
  flex-shrink: 0;
}

#seat-0 .seat-marker {
  background: var(--p0-soft);
  color: var(--p0-color);
}

#seat-1 .seat-marker {
  background: var(--p1-soft);
  color: var(--p1-color);
}

.seat-label {
  color: var(--text-muted);
  font-weight: 500;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
  max-width: 112px;
}

.seat-score {
  font-size: 1.05rem;
  font-variant-numeric: tabular-nums;
}

.score-divider {
  color: var(--text-muted);
  flex-shrink: 0;
}

/* Status banner */
.status-banner {
  margin: 0;
  text-align: center;
  font-size: 0.95rem;
  font-weight: 600;
  min-height: 1.4em;
  color: var(--text);
}

.status-banner.muted {
  color: var(--text-muted);
  font-weight: 500;
}

/* Board container */
#board {
  width: min(92vw, 430px);
  max-height: 56dvh;
  margin: auto;
  display: grid;
  position: relative;
  transition: opacity 0.2s ease;
}

#board.waiting-turn {
  opacity: 0.86;
}

/* Tic-Tac-Toe Tactile Keycap Board */
#board.board-ttt {
  aspect-ratio: 1 / 1;
  width: min(86vw, 360px, 52dvh);
  grid-template-columns: repeat(3, 1fr);
  grid-template-rows: repeat(3, 1fr);
  gap: 11px;
}

.ttt-cell {
  background: var(--surface);
  border: 1.5px solid var(--border-strong);
  border-radius: var(--radius);
  box-shadow: 0 3px 0 var(--border-strong);
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 18%;
  transition: transform 0.08s ease, box-shadow 0.08s ease, border-color 0.15s ease;
}

.ttt-cell:not(:disabled):active {
  transform: translateY(2px);
  box-shadow: 0 1px 0 var(--border-strong);
}

.ttt-mark {
  width: 100%;
  height: 100%;
  overflow: visible;
}

.ttt-stroke {
  fill: none;
  stroke-width: 9;
  stroke-linecap: round;
  stroke-linejoin: round;
}

.ttt-cell.seat-0 .ttt-stroke {
  stroke: var(--p0-color);
}

.ttt-cell.seat-1 .ttt-stroke {
  stroke: var(--p1-color);
}

.ttt-cell.just-placed .ttt-stroke {
  stroke-dasharray: 140;
  stroke-dashoffset: 140;
  animation: stroke-draw 180ms ease-out forwards;
}

.ttt-cell.just-placed .ttt-stroke.stroke-2 {
  animation-delay: 75ms;
}

.ttt-cell.win-cell {
  border-color: var(--win-ring);
  box-shadow: 0 3px 0 var(--win-ring), inset 0 0 0 2px var(--win-ring);
  animation: win-pop 320ms cubic-bezier(0.22, 1, 0.36, 1) both;
  animation-delay: calc(var(--win-idx, 0) * 70ms);
}

/* Connect Four Recessed Rack & Molded Discs */
#board.board-c4 {
  aspect-ratio: 7 / 6;
  width: min(92vw, 430px, calc(54dvh * 7 / 6));
  grid-template-columns: repeat(7, 1fr);
  grid-template-rows: repeat(6, 1fr);
  gap: clamp(5px, 1.4vw, 8px);
  padding: clamp(8px, 2vw, 12px);
  background: linear-gradient(180deg, var(--c4-frame), var(--c4-frame-dark));
  border-radius: 16px;
  box-shadow:
    inset 0 2px 3px rgba(255, 255, 255, 0.16),
    0 5px 0 var(--c4-frame-dark),
    0 8px 18px rgba(0, 0, 0, 0.16);
  overflow: hidden;
}

.c4-cell {
  border: none;
  border-radius: 50%;
  background: var(--c4-slot);
  box-shadow: inset 0 3px 5px rgba(0, 0, 0, 0.3);
  padding: 0;
  position: relative;
}

.c4-disc {
  position: absolute;
  inset: 0;
  border-radius: 50%;
  pointer-events: none;
}

.c4-cell.seat-0 .c4-disc {
  background: radial-gradient(circle at 32% 28%, var(--p0-hi), var(--p0-color) 68%);
  box-shadow: inset 0 -3px 4px rgba(0, 0, 0, 0.25), 0 1px 2px rgba(0, 0, 0, 0.25);
}

.c4-cell.seat-1 .c4-disc {
  background: radial-gradient(circle at 32% 28%, var(--p1-hi), var(--p1-disc) 68%);
  box-shadow: inset 0 -3px 4px rgba(0, 0, 0, 0.22), 0 1px 2px rgba(0, 0, 0, 0.25);
}

.c4-cell.just-dropped .c4-disc {
  animation: c4-drop calc(110ms + var(--drop-rows, 1) * 32ms) cubic-bezier(0.22, 1.12, 0.36, 1) both;
}

.c4-cell.last-move .c4-disc::after {
  content: "";
  position: absolute;
  inset: 31%;
  border-radius: 50%;
  background: rgba(255, 255, 255, 0.45);
}

.c4-cell.win-cell .c4-disc {
  box-shadow: 0 0 0 2.5px #ffffff, 0 0 0 4.5px var(--win-ring);
  animation: win-pop 320ms cubic-bezier(0.22, 1, 0.36, 1) both;
  animation-delay: calc(var(--win-idx, 0) * 70ms);
}

/* Chess 8x8 Board */
#board.board-chess {
  aspect-ratio: 1 / 1;
  width: min(90vw, 410px, 54dvh);
  grid-template-columns: repeat(8, 1fr);
  grid-template-rows: repeat(8, 1fr);
  border: 2px solid var(--border-strong);
  border-radius: 12px;
  overflow: hidden;
  box-shadow: 0 5px 16px rgba(0, 0, 0, 0.16);
}

.chess-sq {
  border: none;
  padding: 11%;
  margin: 0;
  position: relative;
  display: flex;
  align-items: center;
  justify-content: center;
}

.chess-sq.light {
  background: var(--chess-light);
}

.chess-sq.dark {
  background: var(--chess-dark);
}

.chess-coord {
  position: absolute;
  font-size: clamp(0.5rem, 1.5vw, 0.62rem);
  font-weight: 700;
  line-height: 1;
  opacity: 0.55;
  pointer-events: none;
  z-index: 2;
}

.chess-rank {
  top: 2px;
  left: 3px;
}

.chess-file {
  bottom: 2px;
  right: 3px;
}

.chess-sq.light .chess-coord {
  color: #4a3f33;
}

.chess-sq.dark .chess-coord {
  color: #fffaf0;
}

.chess-sq.last-move::before {
  content: "";
  position: absolute;
  inset: 0;
  background: var(--chess-last);
  pointer-events: none;
}

.chess-sq.selected::before {
  content: "";
  position: absolute;
  inset: 0;
  background: var(--chess-select);
  box-shadow: inset 0 0 0 2px var(--accent);
  pointer-events: none;
}

.chess-sq.check-king {
  background: var(--chess-check);
}

/* Pulse only while the game is still on (not on the final checkmate position) */
.chess-sq.check-king:not(.win-cell) {
  animation: check-pulse 900ms ease-in-out infinite;
}

.chess-sq.win-cell {
  box-shadow: inset 0 0 0 3px var(--win-ring);
}

.chess-sq.legal-move::after {
  content: "";
  width: 30%;
  height: 30%;
  border-radius: 50%;
  background: rgba(28, 27, 25, 0.36);
  pointer-events: none;
  z-index: 2;
}

.chess-sq.legal-capture {
  box-shadow: inset 0 0 0 3px rgba(217, 56, 41, 0.82);
}

.chess-piece {
  width: 100%;
  height: 100%;
  position: relative;
  z-index: 1;
  filter: drop-shadow(0 1.5px 1.5px rgba(0, 0, 0, 0.28));
}

.chess-piece.piece-w {
  fill: #fffdf8;
  stroke: #231f1c;
  stroke-width: 2.3;
  stroke-linejoin: round;
}

.chess-piece.piece-b {
  fill: #262930;
  stroke: #d5dce6;
  stroke-width: 1.75;
  stroke-linejoin: round;
}

.chess-piece .chess-detail {
  fill: none;
  stroke-width: 1.6;
  stroke-linecap: round;
  opacity: 0.7;
}

.chess-piece.piece-w .chess-detail {
  stroke: #231f1c;
}

.chess-piece.piece-b .chess-detail {
  stroke: #d5dce6;
}

.chess-piece .chess-eye {
  stroke: none;
}

.chess-piece.piece-w .chess-eye {
  fill: #231f1c;
}

.chess-piece.piece-b .chess-eye {
  fill: #f5b800;
}

.chess-sq.just-placed .chess-piece {
  animation: piece-hop 180ms cubic-bezier(0.22, 1.15, 0.36, 1) both;
}

.chess-sq.capture-hit {
  animation: capture-nudge 200ms ease-out both;
}

.chess-promo-bar {
  position: absolute;
  inset-inline: 8%;
  top: 50%;
  transform: translateY(-50%);
  z-index: 10;
  display: grid;
  grid-template-columns: repeat(4, 1fr);
  gap: 8px;
  padding: 10px;
  background: var(--surface);
  border: 2px solid var(--accent);
  border-radius: var(--radius);
  box-shadow: 0 10px 26px rgba(0, 0, 0, 0.28);
}

.chess-promo-btn {
  border: 1px solid var(--border);
  background: var(--surface-muted);
  color: var(--text);
  border-radius: 10px;
  padding: 8px 4px;
  font-size: 1.6rem;
  line-height: 1;
}

@keyframes piece-hop {
  0% {
    transform: translateY(-5px) scale(0.94);
  }
  100% {
    transform: translateY(0) scale(1);
  }
}

@keyframes capture-nudge {
  0% {
    transform: scale(1.08);
  }
  50% {
    transform: scale(0.96);
  }
  100% {
    transform: scale(1);
  }
}

@keyframes check-pulse {
  0%, 100% {
    box-shadow: inset 0 0 0 2px rgba(255, 255, 255, 0.4);
  }
  50% {
    box-shadow: inset 0 0 0 3px rgba(255, 255, 255, 0.95);
  }
}

/* Desktop/Trackpad-only Hover Previews (prevents sticky hover on iOS touch) */
@media (hover: hover) and (pointer: fine) {
  #board[data-my-seat="0"] .ttt-cell:not(:disabled):hover {
    border-color: var(--p0-color);
    background: var(--p0-soft);
  }

  #board[data-my-seat="1"] .ttt-cell:not(:disabled):hover {
    border-color: var(--p1-color);
    background: var(--p1-soft);
  }

  #board[data-my-seat="0"] .c4-cell.col-hover:not(.seat-0):not(.seat-1) {
    box-shadow: inset 0 0 0 2.5px rgba(255, 107, 91, 0.65), inset 0 3px 5px rgba(0, 0, 0, 0.3);
  }

  #board[data-my-seat="1"] .c4-cell.col-hover:not(.seat-0):not(.seat-1) {
    box-shadow: inset 0 0 0 2.5px rgba(255, 224, 116, 0.75), inset 0 3px 5px rgba(0, 0, 0, 0.3);
  }
}

@keyframes stroke-draw {
  to {
    stroke-dashoffset: 0;
  }
}

@keyframes c4-drop {
  0% {
    transform: translateY(calc(var(--drop-rows, 1) * -112%));
  }
  100% {
    transform: translateY(0);
  }
}

@keyframes win-pop {
  0% {
    transform: scale(1);
  }
  50% {
    transform: scale(1.1);
  }
  100% {
    transform: scale(1);
  }
}

@media (prefers-reduced-motion: reduce) {
  .ttt-cell.just-placed .ttt-stroke,
  .c4-cell.just-dropped .c4-disc,
  .ttt-cell.win-cell,
  .c4-cell.win-cell .c4-disc,
  .chess-sq.just-placed .chess-piece,
  .chess-sq.capture-hit,
  .chess-sq.check-king:not(.win-cell) {
    animation: none !important;
    stroke-dashoffset: 0 !important;
  }
}

/* Confetti overlay scoped to board */
.confetti-canvas {
  position: fixed;
  pointer-events: none;
  z-index: 20;
}

/* Footer */
.arena-footer {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  padding-top: 4px;
}

.footer-links {
  display: flex;
  align-items: center;
  gap: 8px;
  font-size: 0.86rem;
  color: var(--text-muted);
}

.footer-links a {
  color: var(--text-muted);
  text-decoration: none;
}

.footer-links a:hover {
  color: var(--text);
  text-decoration: underline;
}

/* Below-the-fold About section */
.about {
  max-width: 500px;
  margin: 0 auto;
  padding: 36px 16px 64px;
  border-top: 1px solid var(--border);
  line-height: 1.58;
  font-size: 0.95rem;
}

.about h1 {
  font-size: 1.25rem;
  margin: 0 0 10px;
  display: inline;
}

.about > summary {
  cursor: pointer;
  list-style-position: outside;
  margin-left: 18px;
}

.about[open] > summary {
  margin-bottom: 10px;
}

.about h2 {
  font-size: 1.05rem;
  margin: 26px 0 8px;
}

.about h3 {
  font-size: 0.95rem;
  margin: 16px 0 4px;
}

.about p,
.about ul {
  margin: 0 0 12px;
  color: var(--text-muted);
}

.about ul {
  padding-left: 20px;
}

.about li {
  margin-bottom: 6px;
}

.about strong {
  color: var(--text);
}
/* Cloudflare Turnstile check (shown only if it needs interaction) */
.ts-widget {
  position: fixed;
  bottom: 8px;
  left: 50%;
  transform: translateX(-50%);
  z-index: 10;
}

/* Game-over prompt: a big Rematch button right under the result */
.result-row {
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 10px;
  flex-wrap: wrap;
  animation: result-in 260ms cubic-bezier(0.22, 1, 0.36, 1) 420ms both;
}

.result-row[hidden] {
  display: none;
}

.result-btn {
  font-size: 1.02rem;
  padding: 11px 30px;
  box-shadow: 0 3px 0 rgba(0, 0, 0, 0.18);
}

.result-btn:active {
  transform: translateY(2px);
  box-shadow: 0 1px 0 rgba(0, 0, 0, 0.18);
}

.result-btn:disabled {
  opacity: 0.5;
}

.result-hint {
  color: var(--text-muted);
  font-size: 0.86rem;
}

@keyframes result-in {
  from {
    opacity: 0;
    transform: translateY(6px) scale(0.96);
  }
  to {
    opacity: 1;
    transform: none;
  }
}

@media (prefers-reduced-motion: reduce) {
  .result-row {
    animation: none;
  }
}

/* Footer Rematch steps aside while the big one is showing */
#rematch-btn.hidden-for-result {
  visibility: hidden;
}

/* Pencil on your own card: shows the name is editable */
.seat-edit {
  display: none;
  color: var(--text-muted);
  font-size: 0.85rem;
  line-height: 1;
}

.seat-card.is-me .seat-edit {
  display: inline;
}

.seat-card.is-me .seat-label {
  text-decoration: none;
}

/* Optional name field (shown while waiting, or when you tap your card) */
.name-row {
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 8px;
}

.name-row[hidden] {
  display: none;
}

.name-row label {
  color: var(--text-muted);
  font-size: 0.86rem;
  white-space: nowrap;
}

.name-row input {
  font: inherit;
  /* 16px or larger stops iOS Safari zooming the page on focus */
  font-size: 16px;
  width: 9.5em;
  min-width: 0;
  padding: 6px 12px;
  border: 1px solid var(--border-strong);
  border-radius: 999px;
  background: var(--surface);
  color: var(--text);
}

.name-row input:focus {
  outline: 2px solid var(--accent);
  outline-offset: 1px;
}

.name-save {
  padding: 6px 14px;
}

.name-chip {
  justify-self: center;
  align-self: center;
  background: none;
  border: none;
  color: var(--text-muted);
  font-size: 0.86rem;
  padding: 2px 8px;
}

.name-chip:hover {
  color: var(--text);
}

.name-chip[hidden] {
  display: none;
}

.name-roll {
  width: 33px;
  height: 33px;
  font-size: 1rem;
}

.footer-actions {
  display: flex;
  align-items: center;
  gap: 8px;
  flex-shrink: 0;
}

#takeback-btn.hidden-for-result {
  visibility: hidden;
}
