:root {
  --bg: #121624;
  --felt: #1b2133;
  --tile: #242c43;
  --tile-hi: #2d3753;
  --ink: #f1f3f9;
  --muted: #8f99b3;
  --p0: #ff6f5e;          /* player 1: coral */
  --p1: #2ec4b6;          /* player 2: teal */
  --gold: #ffd166;
  --radius: 22px;
  --tap: 64px;            /* minimum touch target */
  font-family: ui-rounded, "SF Pro Rounded", "Nunito", "Segoe UI", system-ui, sans-serif;
  color: var(--ink);
  background: var(--bg);
}

* { box-sizing: border-box; }
html, body { margin: 0; min-height: 100%; }
body {
  /* everything is sized to fit the screen; if it still can't (tiny / odd screens) the page scrolls */
  overflow-x: hidden;
  overflow-y: auto;
  background: radial-gradient(120% 90% at 50% 0%, #1f2742 0%, var(--bg) 60%);
  user-select: none;
  -webkit-user-select: none;
  touch-action: manipulation;
  -webkit-tap-highlight-color: transparent;
}
[hidden] { display: none !important; }

button, .file-btn {
  font: inherit;
  color: inherit;
  min-height: var(--tap);
  min-width: var(--tap);
  border: 0;
  border-radius: 16px;
  padding: 0 1.4em;
  font-size: 1.15rem;
  font-weight: 700;
  cursor: pointer;
  transition: transform .08s ease, background .15s ease, opacity .15s ease;
}
button:active:not(:disabled), .file-btn:active { transform: scale(.96); }
button:disabled { opacity: .35; cursor: default; }
.primary { background: var(--gold); color: #1d1a10; }
.ghost, .file-btn { background: var(--tile-hi); display: inline-flex; align-items: center; justify-content: center; }
.big { font-size: 1.35rem; min-height: 76px; padding: 0 2em; }
.row { display: flex; gap: 14px; justify-content: center; flex-wrap: wrap; }

/* ─── table layout: seat | board | seat ─────────────────────────────────── */
#table {
  /* dvh = the visible height (minus browser bars); below 540 px the page scrolls instead of squashing */
  height: max(540px, 100vh);
  height: max(540px, 100dvh);
  display: grid;
  grid-template-columns: minmax(180px, 1fr) minmax(0, 2.4fr) minmax(180px, 1fr);
  gap: clamp(12px, 2.5vw, 48px);
  align-items: center;
  justify-items: center;
  padding: clamp(12px, 3dvh, 40px) clamp(12px, 3vw, 48px);
}

.seat {
  --pc: var(--p0);
  width: min(100%, 340px);
  height: min(100%, 640px);
  background: var(--felt);
  border-radius: 28px;
  padding: clamp(14px, 2.5vh, 28px);
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  border: 3px solid transparent;
  box-shadow: 0 12px 40px rgba(0, 0, 0, .35);
  transition: border-color .2s ease, box-shadow .2s ease;
}
.seat[data-player="1"] { --pc: var(--p1); }
.seat.active {
  border-color: var(--pc);
  box-shadow: 0 0 0 6px color-mix(in srgb, var(--pc) 25%, transparent), 0 12px 40px rgba(0, 0, 0, .35);
}
.seat-head { width: 100%; position: relative; display: flex; justify-content: center; }
.seat-name { font-size: clamp(1.2rem, 2.2vw, 1.7rem); font-weight: 800; color: var(--pc); white-space: nowrap; }
.turn-badge {   /* overlays the seat's top edge, so it never squeezes the name */
  position: absolute; left: 50%; top: calc(-1 * clamp(14px, 2.5vh, 28px) - 16px); transform: translateX(-50%);
  font-size: .95rem; font-weight: 800; padding: 6px 14px; border-radius: 999px; white-space: nowrap;
  background: var(--pc); color: #10131d; opacity: 0; transition: opacity .2s ease;
}
.seat.active .turn-badge { opacity: 1; }
.score { margin: 0; color: var(--muted); font-size: 1.1rem; }
.score-n { font-size: 2.4rem; font-weight: 900; color: var(--ink); margin-right: 6px; }

.choices { display: grid; grid-template-columns: 1fr 1fr; gap: 12px; width: 100%; }
.choice {
  background: var(--tile); height: clamp(64px, 10dvh, 84px); padding: 0;
  display: flex; align-items: center; justify-content: center; gap: 10px;
  border: 3px solid transparent;
}
.choice.wide { grid-column: span 2; font-size: 1.2rem; }
.choice svg { width: 44px; height: 44px; }
.choice .sym { stroke: var(--pc); }
.choice.selected { border-color: var(--pc); background: var(--tile-hi); }

/* ─── tokens (pieces) ───────────────────────────────────────────────────── */
.token {
  --pc: var(--p0);
  aspect-ratio: 1;
  border-radius: 50%;
  display: flex;
  align-items: center;
  justify-content: center;
  overflow: hidden;
  flex: none;
}
.token.p1 { --pc: var(--p1); }
.token .sym { width: 100%; height: 100%; }
.sym { fill: none; stroke: var(--pc); stroke-width: 11; stroke-linecap: round; }
.token.avatar {
  background: radial-gradient(circle at 50% 35%, color-mix(in srgb, var(--pc) 35%, #fff) 0%, color-mix(in srgb, var(--pc) 70%, #000) 100%);
  box-shadow: inset 0 0 0 5px var(--pc);
}
.token.avatar img { width: 100%; height: 100%; object-fit: cover; object-position: 50% 30%; display: block; }
.token.empty { border: 3px dashed var(--muted); }
.token .q { font-size: 3rem; font-weight: 900; color: var(--muted); }
.token.t-big { width: clamp(76px, 22dvh, 220px); }
.token.t-mini { width: 44px; display: inline-flex; vertical-align: middle; }
.token.t-placed { width: 86%; animation: drop .22s cubic-bezier(.2, 1.4, .5, 1); }
@keyframes drop { from { transform: scale(.4); opacity: 0; } to { transform: scale(1); opacity: 1; } }

/* ─── centre: title, status, board ──────────────────────────────────────── */
/* the board takes whatever height title/status/draws leave: .board-area is a size container
   and the board is the largest square that fits in it */
.center {
  width: 100%; height: 100%; min-width: 0; min-height: 0;
  display: flex; flex-direction: column; align-items: center; gap: clamp(6px, 1.6dvh, 18px);
}
.title { margin: 0; font-size: clamp(1.5rem, 4.5dvh, 3rem); font-weight: 900; letter-spacing: .02em; }
.title span { color: var(--gold); margin: 0 .1em; }
.status { margin: 0; min-height: 48px; display: flex; align-items: center; gap: 10px; font-size: 1.3rem; font-weight: 700; color: var(--muted); }
.draws { margin: 0; color: var(--muted); font-weight: 700; font-size: 1.05rem; }
.draws span { color: var(--ink); font-size: 1.4rem; margin-left: 6px; }

.board-area { flex: 1 1 0; min-height: 0; width: 100%; container-type: size; display: grid; place-items: center; }
.board-wrap { position: relative; width: min(100cqw, 100cqh); aspect-ratio: 1; }
.board {
  width: 100%; height: 100%;
  display: grid; grid-template-columns: repeat(3, 1fr); grid-template-rows: repeat(3, 1fr);
  gap: clamp(8px, 1.5vh, 16px);
}
.cell {
  background: var(--tile); border-radius: var(--radius); padding: 0;
  display: grid; place-items: center;
  box-shadow: inset 0 -6px 0 rgba(0, 0, 0, .25);
}
.cell:disabled { opacity: 1; }
#table[data-phase="play"] .cell:not(.filled):active { background: var(--tile-hi); }
.cell.win { background: color-mix(in srgb, var(--gold) 30%, var(--tile)); animation: pulse 1s ease-in-out infinite alternate; }
@keyframes pulse { to { box-shadow: inset 0 -6px 0 rgba(0, 0, 0, .25), 0 0 0 5px var(--gold); } }
#table[data-phase="setup"] .board { opacity: .35; }

.board-cover {
  position: absolute; inset: 0; display: grid; place-items: center;
  border-radius: var(--radius);
}
#result { background: rgba(10, 13, 22, .55); backdrop-filter: blur(2px); animation: fade .25s ease; }
@keyframes fade { from { opacity: 0; } }
.result-card {
  background: var(--felt); border-radius: 28px; padding: 28px 36px;
  display: flex; flex-direction: column; align-items: center; gap: 14px;
  box-shadow: 0 20px 60px rgba(0, 0, 0, .5);
}
.result-token { display: flex; gap: 12px; }
.result-token .token.t-big { width: clamp(96px, 16vh, 160px); }
#result-text { margin: 0; font-size: 2rem; font-weight: 900; }

/* ─── camera overlay ─────────────────────────────────────────────────────── */
.overlay {
  position: fixed; inset: 0; z-index: 10;
  background: rgba(8, 10, 18, .88);
  display: flex;
  overflow-y: auto; overscroll-behavior: contain;   /* too tall -> scroll the overlay, not the game */
  padding: clamp(10px, 3dvh, 32px);
  animation: fade .2s ease;
}
.overlay[data-player="0"] { --pc: var(--p0); }
.overlay[data-player="1"] { --pc: var(--p1); }
.cam-panel {
  margin: auto;   /* centred while it fits, top-aligned + scrollable when it doesn't */
  width: min(100%, 1200px);
  background: var(--felt); border-radius: 28px; padding: clamp(14px, 2.5vh, 28px);
  display: flex; flex-direction: column; gap: clamp(10px, 2vh, 20px);
  border-top: 6px solid var(--pc);
}
.cam-head { display: flex; justify-content: space-between; align-items: center; gap: 12px; }
.cam-head h2 { margin: 0; font-size: clamp(1.3rem, 3vh, 2rem); }

.cam-live { display: flex; flex-direction: column; gap: clamp(10px, 2vh, 20px); }
.cam-stage {
  position: relative; background: #000; border-radius: 20px; overflow: hidden;
  height: clamp(200px, calc(100dvh - 300px), 640px); display: grid; place-items: center;
}
.cam-stage video { width: 100%; height: 100%; object-fit: cover; transform: scaleX(-1); }
.cam-live.nocam .cam-stage video, .cam-live.nocam .cam-guide { visibility: hidden; }
.cam-guide {
  position: absolute; width: min(38%, 300px); aspect-ratio: 3 / 4;
  border: 4px dashed rgba(255, 255, 255, .55); border-radius: 50%; pointer-events: none;
}
.countdown {
  position: absolute; font-size: 9rem; font-weight: 900; color: #fff;
  text-shadow: 0 6px 30px rgba(0, 0, 0, .6);
}
.countdown.pop { animation: pop .9s ease-out; }
@keyframes pop { from { transform: scale(1.6); opacity: 0; } 30% { opacity: 1; } to { transform: scale(.9); opacity: .2; } }
.busy, .cam-msg {
  position: absolute; inset: 0; display: flex; flex-direction: column; align-items: center; justify-content: center; gap: 18px;
  background: rgba(10, 13, 22, .7); font-size: 1.4rem; font-weight: 700; text-align: center; padding: 24px;
}
.cam-msg { inset: auto 16px 16px 16px; border-radius: 16px; background: rgba(10, 13, 22, .85); font-size: 1.15rem; }
.cam-live.nocam .cam-msg { inset: 0; border-radius: 0; }
.spinner {
  width: 64px; height: 64px; border-radius: 50%;
  border: 7px solid rgba(255, 255, 255, .2); border-top-color: var(--gold);
  animation: spin .8s linear infinite;
}
@keyframes spin { to { transform: rotate(360deg); } }
.file-btn { position: relative; overflow: hidden; }
.file-btn input { position: absolute; inset: 0; opacity: 0; cursor: pointer; font-size: 0; }

/* style picker */
.picker { display: flex; flex-direction: column; gap: clamp(12px, 2.5vh, 24px); position: relative; }
.picker.loading .styles { opacity: .4; pointer-events: none; }
.picker.loading::after {
  content: ""; position: absolute; top: 18%; left: calc(50% - 32px);
  width: 64px; height: 64px; border-radius: 50%;
  border: 7px solid rgba(255, 255, 255, .2); border-top-color: var(--gold);
  animation: spin .8s linear infinite;
}
.styles { display: grid; grid-template-columns: repeat(5, minmax(0, 1fr)); gap: clamp(8px, 1.2vw, 16px); }
.style-tile {
  background: var(--tile); padding: 8px; border-radius: 20px; min-width: 0;
  display: flex; flex-direction: column; align-items: center; gap: 8px;
  border: 4px solid transparent;
}
.style-tile.selected { border-color: var(--pc); background: var(--tile-hi); }
.style-img {   /* 2 rows of 5: sized by height too, so both rows + buttons fit on short screens */
  width: min(100%, max(76px, 15dvh)); aspect-ratio: 1; border-radius: 50%; overflow: hidden;
  background: radial-gradient(circle at 50% 35%, color-mix(in srgb, var(--pc) 35%, #fff) 0%, color-mix(in srgb, var(--pc) 70%, #000) 100%);
}
.style-img img { width: 100%; height: 100%; object-fit: cover; display: block; }
.style-name { font-size: 1rem; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; max-width: 100%; }
/* primary actions stay reachable while the overlay scrolls */
.cam-actions, .picker-actions {
  position: sticky; bottom: calc(-1 * clamp(14px, 2.5dvh, 28px)); z-index: 1;
  background: var(--felt); padding: 8px 0;
}
.chips-row { display: flex; align-items: center; gap: 14px; flex-wrap: wrap; justify-content: center; }
.chips-label { color: var(--muted); font-weight: 700; }
.chips { display: flex; gap: 10px; flex-wrap: wrap; justify-content: center; }
.chip { min-height: 56px; padding: 0 18px; font-size: 1rem; background: var(--tile); border: 3px solid transparent; }
.chip.selected { border-color: var(--pc); background: var(--tile-hi); }

/* narrow / portrait screens: both seats above the board; the board again takes the height that is
   left, but never shrinks below 260 px -- the page scrolls instead */
@media (max-aspect-ratio: 1/1) {
  #table {
    grid-template-columns: minmax(0, 1fr) minmax(0, 1fr); grid-template-rows: auto minmax(0, 1fr);
    height: auto; min-height: 100dvh; align-items: stretch;
  }
  .center { grid-column: span 2; grid-row: 2; }
  .board-area { min-height: 260px; }
  .seat { width: 100%; height: auto; gap: 8px; container-type: inline-size; }
  .token.t-big { width: clamp(64px, 10dvh, 150px); }
  @container (min-width: 300px) {   /* wide enough: X / O / Avatar in one row */
    .choices { grid-template-columns: repeat(3, 1fr); }
    .choice.wide { grid-column: auto; }
  }
}
@media (max-width: 640px) {
  .styles { grid-template-columns: repeat(auto-fill, minmax(96px, 1fr)); }
  .choices { gap: 8px; }
  button, .file-btn { padding: 0 1em; }
  .big { padding: 0 1.2em; }
}
