:root {
  /* Walnut (the default): a warm wooden table with a linen board. */
  --bg: #2a1c12;
  --bg-2: #3a2718;
  --bar: #1d130c;
  --panel: #2f2017;
  --panel-2: #3d2a1e;
  --line: rgba(255, 236, 210, 0.1);
  --line-strong: rgba(255, 236, 210, 0.2);
  --text: #f7ecdc;
  --muted: #cdb59b;
  --accent: #f4b740;
  --accent-2: #ffcf6b;
  --accent-ink: #2a1a05;
  --good: #86d993;
  --bad: #ff8f7f;
  --button: #4a3324;
  --button-hover: #5a3f2d;
  --button-border: #6b4c36;

  --frame-1: #6a4527;
  --frame-2: #4a2d17;
  --sq: #f1e6cf;
  --sq-gap: #d5c19c;
  --sq-ink: #9b8566;
  --tw: #c64f3b;
  --tw-ink: #fff3ec;
  --dw: #eb9a8f;
  --dw-ink: #6b2118;
  --tl: #2f7d8c;
  --tl-ink: #ecfbff;
  --dl: #9fd0d7;
  --dl-ink: #194a53;
  --star-ink: #c4851a;

  --tile-1: #fdf2d8;
  --tile-2: #f0d8a2;
  --tile-edge: #c4955a;
  --tile-ink: #2b1c10;
  --tile-blank: #8b6f52;

  --p0: #f4b740;
  --p1: #6fc3df;
  --p2: #ee93b8;
  --p3: #a3d97d;

  --texture: url("data:image/svg+xml;utf8,<svg xmlns='http://www.w3.org/2000/svg' width='220' height='220'><filter id='n'><feTurbulence type='fractalNoise' baseFrequency='0.012 0.9' numOctaves='3' stitchTiles='stitch'/><feColorMatrix values='0 0 0 0 0  0 0 0 0 0  0 0 0 0 0  0 0 0 0.16 0'/></filter><rect width='100%25' height='100%25' filter='url(%23n)'/></svg>");
  --cell: 36px;
  --gap: 2px;
  --rack-tile: 52px;
  color-scheme: dark;
}

/* Garden: green baize, a pale sage board. */
:root[data-theme="garden"] {
  --bg: #12301f;
  --bg-2: #1b4630;
  --bar: #0b2014;
  --panel: #12301f;
  --panel-2: #1a422c;
  --muted: #b5cdb9;
  --button: #1f4e35;
  --button-hover: #275f41;
  --button-border: #33704f;
  --frame-1: #2b5b3e;
  --frame-2: #173a26;
  --sq: #eaeedb;
  --sq-gap: #c4ccae;
  --sq-ink: #7d8a6c;
  --tw: #d0573d;
  --dw: #f0b38c;
  --dw-ink: #6e3215;
  --tl: #3c6fb0;
  --dl: #a9c9ee;
  --dl-ink: #1f3f68;
  --star-ink: #b98419;
  --texture: url("data:image/svg+xml;utf8,<svg xmlns='http://www.w3.org/2000/svg' width='180' height='180'><filter id='n'><feTurbulence type='fractalNoise' baseFrequency='0.85' numOctaves='2' stitchTiles='stitch'/><feColorMatrix values='0 0 0 0 0  0 0 0 0 0  0 0 0 0 0  0 0 0 0.12 0'/></filter><rect width='100%25' height='100%25' filter='url(%23n)'/></svg>");
}

/* Midnight: deep blue, a dark board with jewel-coloured squares. */
:root[data-theme="midnight"] {
  --bg: #0e1530;
  --bg-2: #182349;
  --bar: #090f24;
  --panel: #111a3a;
  --panel-2: #1a2652;
  --muted: #aab6da;
  --button: #22336b;
  --button-hover: #2b3f82;
  --button-border: #3a5096;
  --frame-1: #26356e;
  --frame-2: #141e45;
  --sq: #1e2a56;
  --sq-gap: #121a3b;
  --sq-ink: #5f6fa6;
  --tw: #c9435a;
  --tw-ink: #fff0f3;
  --dw: #8b4a86;
  --dw-ink: #ffe6fb;
  --tl: #1f8fb0;
  --tl-ink: #eafaff;
  --dl: #33588f;
  --dl-ink: #dcebff;
  --star-ink: #f4b740;
  --texture: none;
}

* { box-sizing: border-box; }

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

body {
  display: grid;
  grid-template: "hud" auto "table" minmax(0, 1fr) / minmax(0, 1fr);
  background: var(--bg);
  background-image: var(--texture), radial-gradient(120% 90% at 50% 30%, var(--bg-2), var(--bg) 70%);
  color: var(--text);
  font: 15px/1.4 system-ui, -apple-system, "Segoe UI", Roboto, Helvetica, Arial, sans-serif;
  overflow: hidden;
  touch-action: manipulation;
  -webkit-user-select: none;
  user-select: none;
  -webkit-tap-highlight-color: transparent;
}

input, textarea { -webkit-user-select: text; user-select: text; }

/* --- top bar ---------------------------------------------------------------- */

.hud {
  grid-area: hud;
  display: flex;
  align-items: center;
  gap: 10px 16px;
  padding: 8px max(16px, env(safe-area-inset-right)) 8px max(16px, env(safe-area-inset-left));
  background: var(--bar);
  box-shadow: 0 1px 0 var(--line), 0 6px 18px rgba(0, 0, 0, 0.25);
  position: relative;
  z-index: 5;
}

.brand {
  display: flex;
  align-items: center;
  gap: 6px;
  font-weight: 800;
  font-size: 20px;
  letter-spacing: 0.02em;
  flex: none;
}

.brand-tile {
  display: grid;
  place-items: center;
  width: 30px;
  height: 30px;
  border-radius: 6px;
  background: linear-gradient(var(--tile-1), var(--tile-2));
  box-shadow: inset 0 -3px 0 var(--tile-edge), 0 2px 4px rgba(0, 0, 0, 0.4);
  color: var(--tile-ink);
  font-family: "Avenir Next", "Futura", ui-rounded, "Nunito", system-ui, sans-serif;
  font-size: 19px;
  font-weight: 800;
  transform: rotate(-6deg);
}

.scoreboard {
  display: flex;
  gap: 8px;
  flex: 1;
  min-width: 0;
  justify-content: center;
  flex-wrap: wrap;
}

.chip {
  display: inline-block;
}

.player {
  --pc: var(--p0);
  display: grid;
  grid-template: "dot name score" auto "dot sub score" auto / auto minmax(0, 1fr) auto;
  column-gap: 8px;
  align-items: center;
  min-width: 128px;
  max-width: 220px;
  padding: 5px 12px 5px 9px;
  border-radius: 12px;
  background: var(--panel-2);
  border: 1px solid var(--line);
  transition: background 0.25s, border-color 0.25s, transform 0.25s;
}
.player[data-seat="1"] { --pc: var(--p1); }
.player[data-seat="2"] { --pc: var(--p2); }
.player[data-seat="3"] { --pc: var(--p3); }
.player .dot { grid-area: dot; width: 10px; height: 10px; border-radius: 50%; background: var(--pc); box-shadow: 0 0 0 3px color-mix(in srgb, var(--pc) 25%, transparent); }
.player .name { grid-area: name; font-weight: 650; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; font-size: 14px; }
.player .sub { grid-area: sub; color: var(--muted); font-size: 12px; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.player .score { grid-area: score; font-size: 24px; font-weight: 800; font-variant-numeric: tabular-nums; }
.player.active {
  border-color: var(--pc);
  background: color-mix(in srgb, var(--pc) 16%, var(--panel-2));
  box-shadow: 0 0 0 1px var(--pc), 0 4px 16px color-mix(in srgb, var(--pc) 30%, transparent);
}
.player.winner .score::after { content: " ★"; color: var(--accent); font-size: 16px; vertical-align: 3px; }
.thinking::after {
  content: "";
  display: inline-block;
  width: 1.2em;
  text-align: left;
  animation: dots 1.2s steps(4) infinite;
}
@keyframes dots {
  0% { content: ""; } 25% { content: "."; } 50% { content: ".."; } 75% { content: "..."; }
}
.bump { animation: bump 0.5s ease-out; }
@keyframes bump { 30% { transform: scale(1.12); } }

.hud-actions {
  display: flex;
  gap: 6px;
  flex: none;
}

/* --- buttons ------------------------------------------------------------------ */

button {
  font: inherit;
  color: inherit;
  background: var(--button);
  border: 1px solid var(--button-border);
  border-radius: 10px;
  padding: 7px 12px;
  cursor: pointer;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 6px;
  min-height: 38px;
  transition: background 0.15s, transform 0.08s, opacity 0.15s;
}
button:hover:not(:disabled) { background: var(--button-hover); }
button:active:not(:disabled) { transform: translateY(1px); }
button:disabled { opacity: 0.42; cursor: default; }
button:focus-visible, input:focus-visible { outline: 2px solid var(--accent); outline-offset: 2px; }
button.icon { width: 38px; padding: 0; font-weight: 800; font-size: 17px; }
button.primary {
  background: linear-gradient(var(--accent-2), var(--accent));
  border-color: var(--accent);
  color: var(--accent-ink);
  font-weight: 750;
}
button.primary:hover:not(:disabled) { background: var(--accent-2); }
button.subtle { background: transparent; border-color: var(--line-strong); color: var(--muted); }
button.big { font-size: 18px; padding: 12px 26px; }

.ico {
  width: 18px;
  height: 18px;
  fill: none;
  stroke: currentColor;
  stroke-width: 2;
  stroke-linecap: round;
  stroke-linejoin: round;
  flex: none;
}

kbd {
  font: 600 11px/1 ui-monospace, Menlo, monospace;
  padding: 2px 5px;
  border-radius: 4px;
  border: 1px solid var(--line-strong);
  color: var(--muted);
}

/* --- the table: board, rack, side panel --------------------------------------- */

.table {
  grid-area: table;
  display: grid;
  grid-template-columns: minmax(0, 1fr) 300px;
  gap: 16px;
  padding: 14px max(16px, env(safe-area-inset-right)) max(12px, env(safe-area-inset-bottom)) max(16px, env(safe-area-inset-left));
  min-height: 0;
}

/* main.js sizes the board to the space left after the rack and buttons (layout()). */
.play-area {
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  min-height: 0;
  min-width: 0;
  overflow: hidden;
}
.board-wrap { flex: none; }
.controls { display: flex; flex-direction: column; align-items: center; max-width: 100%; }

/* Phones on their side: board on the left, rack and buttons on the right. */
body.landscape-compact .play-area { flex-direction: row; gap: 16px; }
body.landscape-compact .controls { width: 290px; flex: none; }
body.landscape-compact .rack { flex-wrap: wrap; justify-content: center; width: calc(var(--rack-tile) * 4 + 18px); }
body.landscape-compact .actions { display: grid; grid-template-columns: repeat(3, 1fr); width: 100%; }
body.landscape-compact .actions .play { grid-column: 1 / -1; }
body.landscape-compact .actions .lbl { display: none; }
body.landscape-compact .hud { padding-top: 4px; padding-bottom: 4px; }
body.landscape-compact .player { padding-top: 2px; padding-bottom: 2px; }
body.landscape-compact .player .score { font-size: 19px; }
body.landscape-compact .table { padding-top: 6px; padding-bottom: 6px; }

.board-frame {
  position: relative;
  padding: calc(var(--cell) * 0.22);
  border-radius: calc(var(--cell) * 0.3);
  background: linear-gradient(145deg, var(--frame-1), var(--frame-2));
  box-shadow: inset 0 1px 0 rgba(255, 255, 255, 0.15), inset 0 -2px 0 rgba(0, 0, 0, 0.3), 0 14px 34px rgba(0, 0, 0, 0.45);
}

.board {
  display: grid;
  grid-template-columns: repeat(15, var(--cell));
  grid-template-rows: repeat(15, var(--cell));
  gap: var(--gap);
  padding: var(--gap);
  background: var(--sq-gap);
  border-radius: calc(var(--cell) * 0.14);
  touch-action: none;
}

.cell {
  position: relative;
  display: grid;
  place-items: center;
  background: var(--sq);
  border-radius: calc(var(--cell) * 0.1);
  color: var(--sq-ink);
  font: 800 calc(var(--cell) * 0.3) / 1 system-ui, sans-serif;
  letter-spacing: 0.02em;
  cursor: pointer;
}
.cell.TW { background: var(--tw); color: var(--tw-ink); }
.cell.DW { background: var(--dw); color: var(--dw-ink); }
.cell.TL { background: var(--tl); color: var(--tl-ink); }
.cell.DL { background: var(--dl); color: var(--dl-ink); }
.cell.star { background: var(--sq); color: var(--star-ink); font-size: calc(var(--cell) * 0.62); text-shadow: 0 1px 0 rgba(0, 0, 0, 0.25); }
.cell .lab { pointer-events: none; }
.cell.drop-target { box-shadow: inset 0 0 0 calc(var(--cell) * 0.08) var(--accent); }
.cell.cursor::after {
  content: "";
  position: absolute;
  inset: 0;
  border-radius: inherit;
  box-shadow: inset 0 0 0 2px var(--accent);
  animation: blink 1.1s ease-in-out infinite;
  pointer-events: none;
}
.cell.cursor::before {
  content: "▶";
  position: absolute;
  z-index: 1;
  color: var(--accent);
  font-size: calc(var(--cell) * 0.34);
  text-shadow: 0 0 4px rgba(0, 0, 0, 0.6);
  pointer-events: none;
}
.cell.cursor.down::before { content: "▼"; }
.cell.cursor.has-tile::before { right: 2%; top: 2%; font-size: calc(var(--cell) * 0.24); }
@keyframes blink { 50% { opacity: 0.35; } }

/* Tiles: on the board, on the rack, and while being dragged. */
.tile {
  --size: var(--cell);
  position: relative;
  display: grid;
  place-items: center;
  width: var(--size);
  height: var(--size);
  border-radius: calc(var(--size) * 0.14);
  background: linear-gradient(170deg, var(--tile-1) 10%, var(--tile-2));
  box-shadow:
    inset 0 calc(var(--size) * -0.07) 0 var(--tile-edge),
    inset 0 1px 0 rgba(255, 255, 255, 0.8),
    0 1px 2px rgba(0, 0, 0, 0.35);
  color: var(--tile-ink);
  font-family: "Avenir Next", "Futura", ui-rounded, "Nunito", system-ui, sans-serif;
  font-weight: 750;
  font-size: calc(var(--size) * 0.6);
  line-height: 1;
  padding-bottom: calc(var(--size) * 0.06);
  pointer-events: none;
}
.tile .v {
  position: absolute;
  right: 9%;
  bottom: 9%;
  font-size: calc(var(--size) * 0.24);
  font-weight: 700;
  font-family: system-ui, sans-serif;
}
.tile.blank { color: var(--tile-blank); }
.tile.blank .v { display: none; }
/* An unplayed blank: a faint ring so it reads as a tile, not an empty slot. */
.blank-mark { width: 38%; height: 38%; border-radius: 50%; border: 2px dashed color-mix(in srgb, var(--tile-blank) 55%, transparent); }
.tile.blank.unset::after { content: ""; }
.cell > .tile { position: absolute; inset: 0; width: 100%; height: 100%; }
.cell.last > .tile {
  box-shadow:
    inset 0 calc(var(--size) * -0.07) 0 var(--tile-edge),
    inset 0 0 0 2px var(--lastc, var(--accent)),
    0 0 8px color-mix(in srgb, var(--lastc, var(--accent)) 60%, transparent);
}
.cell.pending > .tile {
  background: linear-gradient(170deg, #fffbe8 10%, #ffe9a8);
  box-shadow:
    inset 0 calc(var(--size) * -0.07) 0 #d8a64c,
    0 0 0 2px var(--accent),
    0 4px 10px rgba(0, 0, 0, 0.35);
  transform: translateY(-1px);
}
.cell.pending.bad > .tile { box-shadow: inset 0 calc(var(--size) * -0.07) 0 #d8a64c, 0 0 0 2px var(--bad), 0 4px 10px rgba(0, 0, 0, 0.35); }
.cell.pending { cursor: grab; }
.cell.dropped > .tile { animation: drop 0.32s cubic-bezier(0.2, 1.4, 0.4, 1) both; }
@keyframes drop {
  from { transform: translateY(-40%) scale(1.25); opacity: 0; box-shadow: 0 12px 18px rgba(0, 0, 0, 0.3); }
}
.cell.word-flash > .tile { animation: flash 0.9s ease-out; }
@keyframes flash { 25% { filter: brightness(1.18) saturate(1.2); transform: translateY(-2px); } }

/* The score bubble on a pending play, and floating score pops. */
.fx { position: absolute; inset: 0; pointer-events: none; }
.bubble {
  position: absolute;
  transform: translate(-30%, -70%);
  min-width: calc(var(--cell) * 0.9);
  padding: 2px 7px;
  border-radius: 999px;
  background: var(--good);
  color: #0d2a13;
  font: 800 calc(max(var(--cell) * 0.38, 12px)) / 1.25 system-ui, sans-serif;
  text-align: center;
  box-shadow: 0 2px 8px rgba(0, 0, 0, 0.4);
  z-index: 3;
}
.bubble.bad { background: var(--bad); color: #3a0d07; }
.pop {
  position: absolute;
  transform: translate(-50%, -50%);
  font: 900 calc(max(var(--cell) * 0.9, 24px)) / 1 "Avenir Next", ui-rounded, system-ui, sans-serif;
  color: var(--accent-2);
  text-shadow: 0 2px 0 rgba(0, 0, 0, 0.35), 0 0 18px rgba(0, 0, 0, 0.5);
  animation: pop 1.4s ease-out forwards;
  white-space: nowrap;
  z-index: 4;
}
.pop.bingo { color: #fff; font-size: calc(max(var(--cell) * 1.3, 34px)); }
.pop small { display: block; font-size: 0.42em; letter-spacing: 0.2em; text-align: center; color: var(--accent-2); }
@keyframes pop {
  0% { opacity: 0; transform: translate(-50%, -20%) scale(0.6); }
  15% { opacity: 1; transform: translate(-50%, -50%) scale(1.08); }
  70% { opacity: 1; transform: translate(-50%, -80%) scale(1); }
  100% { opacity: 0; transform: translate(-50%, -120%) scale(0.95); }
}
.spark {
  position: absolute;
  width: 8px;
  height: 8px;
  border-radius: 2px;
  animation: spark 1.1s ease-out forwards;
  z-index: 4;
}
@keyframes spark {
  from { transform: translate(0, 0) rotate(0); opacity: 1; }
  to { transform: translate(var(--dx), var(--dy)) rotate(540deg); opacity: 0; }
}

.status {
  min-height: 1.4em;
  margin: 8px 0 6px;
  text-align: center;
  color: var(--muted);
  font-size: 14px;
  max-width: 100%;
}
.status.bad { color: var(--bad); }
.status.good { color: var(--good); }
.status b { color: var(--text); }

/* Rack and the buttons beside it. */
.rack-bar {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 10px;
  max-width: 100%;
}


.rack-shelf {
  padding: 8px 10px 10px;
  border-radius: 14px;
  background: linear-gradient(var(--frame-1), var(--frame-2));
  box-shadow: inset 0 1px 0 rgba(255, 255, 255, 0.14), 0 8px 18px rgba(0, 0, 0, 0.4);
}

.rack {
  display: flex;
  gap: 6px;
  min-height: var(--rack-tile);
  touch-action: none;
}
.rack.hidden-tiles .slot .tile { visibility: hidden; }

.slot {
  position: relative;
  width: var(--rack-tile);
  height: var(--rack-tile);
  border-radius: calc(var(--rack-tile) * 0.14);
  background: rgba(0, 0, 0, 0.22);
  box-shadow: inset 0 2px 4px rgba(0, 0, 0, 0.35);
  cursor: grab;
}
.slot .tile { --size: var(--rack-tile); transition: transform 0.15s; }
.slot.empty { cursor: default; }
.slot.selected .tile { transform: translateY(-8px); box-shadow: inset 0 calc(var(--size) * -0.07) 0 var(--tile-edge), 0 0 0 3px var(--accent), 0 8px 14px rgba(0, 0, 0, 0.45); }
.slot.drop-target { box-shadow: inset 0 0 0 2px var(--accent); }
.slot.lifted .tile { opacity: 0.25; }

.ghost {
  position: fixed;
  left: 0;
  top: 0;
  z-index: 50;
  pointer-events: none;
  filter: drop-shadow(0 10px 12px rgba(0, 0, 0, 0.45));
  transform-origin: 50% 50%;
}
.ghost .tile { --size: var(--ghost-size, 56px); }

.actions {
  display: flex;
  gap: 6px;
  flex-wrap: wrap;
  justify-content: center;
}
.tool { min-width: 44px; }
.play {
  min-width: 120px;
  font-weight: 800;
  font-size: 16px;
  background: linear-gradient(var(--accent-2), var(--accent));
  border-color: var(--accent);
  color: var(--accent-ink);
  box-shadow: 0 4px 14px color-mix(in srgb, var(--accent) 35%, transparent);
}
.play:hover:not(:disabled) { background: var(--accent-2); }
.play.ready { animation: ready 1.8s ease-in-out infinite; }
@keyframes ready { 50% { box-shadow: 0 4px 22px color-mix(in srgb, var(--accent) 70%, transparent); } }

/* Side panel: the moves so far, and the tiles not yet seen. */
.side {
  display: flex;
  flex-direction: column;
  min-height: 0;
  background: color-mix(in srgb, var(--panel) 92%, transparent);
  border: 1px solid var(--line);
  border-radius: 16px;
  overflow: hidden;
}
.side-head { display: flex; align-items: center; gap: 8px; padding: 8px; border-bottom: 1px solid var(--line); }
.side-close { display: none; }
.tabs { display: flex; gap: 4px; flex: 1; }
.tab { flex: 1; background: transparent; border-color: transparent; color: var(--muted); min-height: 34px; }
.tab[aria-selected="true"] { background: var(--panel-2); border-color: var(--line-strong); color: var(--text); font-weight: 650; }
.side-body { flex: 1; min-height: 0; overflow-y: auto; padding: 4px 0 10px; }

.log { list-style: none; margin: 0; padding: 0; }
.log li {
  --pc: var(--p0);
  display: grid;
  grid-template-columns: 26px minmax(0, 1fr) auto;
  gap: 2px 8px;
  align-items: baseline;
  padding: 7px 12px;
  border-bottom: 1px solid var(--line);
}
.log li[data-seat="1"] { --pc: var(--p1); }
.log li[data-seat="2"] { --pc: var(--p2); }
.log li[data-seat="3"] { --pc: var(--p3); }
.log .n { color: var(--muted); font-size: 12px; font-variant-numeric: tabular-nums; }
.log .who { font-size: 12px; color: var(--pc); font-weight: 650; }
.log .words { grid-column: 2; font-weight: 700; letter-spacing: 0.04em; overflow-wrap: anywhere; }
.log .words .extra { font-weight: 500; color: var(--muted); letter-spacing: 0; }
.log .pts { grid-row: 1 / span 2; grid-column: 3; font-weight: 800; font-size: 17px; font-variant-numeric: tabular-nums; align-self: center; }
.log .note { grid-column: 2 / span 2; font-size: 12px; color: var(--muted); }
.log .note.best b { color: var(--accent-2); }
.log .note.top { color: var(--good); }
.log .tag { display: inline-block; margin-left: 4px; padding: 0 6px; border-radius: 6px; font-size: 11px; font-weight: 800; background: var(--accent); color: var(--accent-ink); vertical-align: 1px; }
.log .tag.hint { background: var(--panel-2); color: var(--muted); border: 1px solid var(--line-strong); }
.log .empty-log { display: block; color: var(--muted); padding: 16px; border: 0; text-align: center; }
.log li.latest { background: color-mix(in srgb, var(--pc) 8%, transparent); }

.tracker { padding: 8px 12px; }
.tracker-head { margin: 4px 0 10px; color: var(--muted); }
.tracker-head b { color: var(--text); }
.tracker-grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(42px, 1fr));
  gap: 6px;
}
.track {
  display: grid;
  justify-items: center;
  gap: 2px;
  font-size: 12px;
  color: var(--muted);
  font-variant-numeric: tabular-nums;
}
.track .tile { --size: 34px; }
.track.gone { opacity: 0.28; }

/* --- pass-and-play handoff screen ---------------------------------------------- */

.handoff {
  position: fixed;
  inset: 0;
  z-index: 30;
  display: grid;
  place-items: center;
  padding: 16px;
  background: color-mix(in srgb, var(--bg) 82%, transparent);
  backdrop-filter: blur(14px);
  -webkit-backdrop-filter: blur(14px);
}
.handoff[hidden] { display: none; }
.handoff-box { text-align: center; max-width: 420px; }
.handoff-box h2 { font-size: 30px; margin: 4px 0 8px; }
.eyebrow { text-transform: uppercase; letter-spacing: 0.18em; font-size: 12px; color: var(--muted); margin: 0; }

/* --- dialogs ------------------------------------------------------------------- */

dialog {
  width: min(500px, calc(100vw - 32px));
  max-height: calc(100dvh - 32px);
  padding: 0;
  border: 1px solid var(--line-strong);
  border-radius: 18px;
  background: var(--panel);
  color: var(--text);
  box-shadow: 0 24px 70px rgba(0, 0, 0, 0.55);
  overflow: hidden;
}
dialog[open] { display: flex; flex-direction: column; animation: rise 0.22s ease-out; }
dialog.wide { width: min(720px, calc(100vw - 32px)); }
dialog::backdrop { background: rgba(0, 0, 0, 0.55); backdrop-filter: blur(3px); }
@keyframes rise { from { transform: translateY(14px) scale(0.98); opacity: 0; } }
.dialog-head { display: flex; align-items: center; justify-content: space-between; gap: 12px; padding: 14px 18px 8px; }
.dialog-head h2 { margin: 0; font-size: 21px; }
.dialog-body { padding: 4px 18px 14px; overflow-y: auto; min-height: 0; -webkit-user-select: text; user-select: text; }
.dialog-body h3 { font-size: 15px; margin: 18px 0 8px; color: var(--accent-2); }
.dialog-body p, .dialog-body li { color: color-mix(in srgb, var(--text) 88%, var(--muted)); }
.dialog-body ul, .dialog-body ol { padding-left: 20px; margin: 6px 0; }
.dialog-body li { margin: 4px 0; }
.dialog-actions {
  display: flex;
  justify-content: flex-end;
  flex-wrap: wrap;
  gap: 8px;
  padding: 12px 18px 16px;
  border-top: 1px solid var(--line);
}
.fine { font-size: 13px; color: var(--muted) !important; }
.warn { padding: 8px 12px; border-radius: 10px; background: color-mix(in srgb, var(--bad) 16%, transparent); border: 1px solid color-mix(in srgb, var(--bad) 40%, transparent); }

.daily-card {
  display: grid;
  grid-template-columns: auto minmax(0, 1fr) auto;
  align-items: center;
  gap: 14px;
  padding: 14px;
  margin-top: 6px;
  border-radius: 14px;
  background: linear-gradient(135deg, color-mix(in srgb, var(--accent) 22%, var(--panel-2)), var(--panel-2));
  border: 1px solid color-mix(in srgb, var(--accent) 40%, transparent);
}
.daily-card h3 { margin: 0 0 2px; color: var(--text); font-size: 17px; }
.daily-card p { margin: 2px 0; }
.daily-badge {
  display: grid;
  place-items: center;
  width: 54px;
  height: 54px;
  border-radius: 10px;
  background: linear-gradient(170deg, var(--tile-1), var(--tile-2));
  box-shadow: inset 0 -4px 0 var(--tile-edge), 0 3px 8px rgba(0, 0, 0, 0.4);
  color: var(--tile-ink);
  font: 800 24px/1 "Avenir Next", ui-rounded, system-ui, sans-serif;
  transform: rotate(-4deg);
}
.daily-badge.gold { background: linear-gradient(170deg, #fff0b0, #f2c53d); box-shadow: inset 0 -4px 0 #b58a12, 0 3px 10px rgba(242, 197, 61, 0.5); }
.daily-badge.silver { background: linear-gradient(170deg, #ffffff, #c9d1da); box-shadow: inset 0 -4px 0 #8c96a3, 0 3px 8px rgba(0, 0, 0, 0.4); }
.daily-badge.bronze { background: linear-gradient(170deg, #ffd9b8, #d08a4f); box-shadow: inset 0 -4px 0 #8f5426, 0 3px 8px rgba(0, 0, 0, 0.4); }

.mode-tabs { display: flex; gap: 6px; margin: 18px 0 12px; border-bottom: 1px solid var(--line); }
.mode-tab { background: transparent; border: 0; border-bottom: 2px solid transparent; border-radius: 0; color: var(--muted); padding: 8px 6px; }
.mode-tab[aria-selected="true"] { color: var(--text); border-bottom-color: var(--accent); font-weight: 700; }
.mode-tab:hover:not(:disabled) { background: transparent; color: var(--text); }

.level-grid { display: grid; grid-template-columns: repeat(auto-fit, minmax(118px, 1fr)); gap: 8px; }
.level {
  display: grid;
  justify-items: start;
  align-content: start;
  text-align: left;
  gap: 4px;
  padding: 10px 12px;
  border-radius: 12px;
  background: var(--panel-2);
  border: 1px solid var(--line);
}
.level b { font-size: 16px; }
.level .pips { letter-spacing: 2px; color: var(--accent); font-size: 12px; }
.level span:last-child { font-size: 13px; color: var(--muted); line-height: 1.35; }
.level .rec { font-size: 12px; color: var(--muted); }
.level[aria-checked="true"] { border-color: var(--accent); box-shadow: 0 0 0 1px var(--accent); background: color-mix(in srgb, var(--accent) 12%, var(--panel-2)); }

.row-inline { display: flex; align-items: center; flex-wrap: wrap; gap: 8px 12px; margin: 14px 0 4px; }
.field-label { color: var(--muted); font-size: 14px; }
.seg { display: inline-flex; border: 1px solid var(--button-border); border-radius: 10px; overflow: hidden; }
.seg button { border: 0; border-radius: 0; background: transparent; min-height: 34px; }
.seg button + button { border-left: 1px solid var(--button-border); }
.seg button[aria-checked="true"] { background: var(--accent); color: var(--accent-ink); font-weight: 700; }

.seats { display: grid; gap: 8px; margin: 10px 0; }
.seat { --pc: var(--p0); display: grid; grid-template-columns: 12px minmax(0, 1fr) auto auto; gap: 8px; align-items: center; }
.seat[data-seat="1"] { --pc: var(--p1); }
.seat[data-seat="2"] { --pc: var(--p2); }
.seat[data-seat="3"] { --pc: var(--p3); }
.seat .dot { width: 12px; height: 12px; border-radius: 50%; background: var(--pc); }
input[type="text"], select {
  font: inherit;
  color: var(--text);
  background: var(--bg);
  border: 1px solid var(--line-strong);
  border-radius: 10px;
  padding: 7px 10px;
  min-height: 38px;
  min-width: 0;
}
select { padding-right: 6px; }

.field { display: grid; grid-template-columns: auto minmax(0, 220px); align-items: center; gap: 12px; margin: 6px 0 10px; }
.switch { display: flex; align-items: center; gap: 10px; padding: 6px 0; cursor: pointer; }
.switch input { width: 18px; height: 18px; accent-color: var(--accent); }

.theme-row { display: flex; gap: 8px; flex-wrap: wrap; }
.theme-swatch { gap: 8px; }
.theme-swatch[aria-checked="true"] { border-color: var(--accent); box-shadow: 0 0 0 1px var(--accent); }
.chip-walnut, .chip-garden, .chip-midnight { width: 18px; height: 18px; border-radius: 5px; border: 1px solid rgba(255, 255, 255, 0.3); }
.chip-walnut { background: linear-gradient(135deg, #6a4527, #f1e6cf); }
.chip-garden { background: linear-gradient(135deg, #2b5b3e, #eaeedb); }
.chip-midnight { background: linear-gradient(135deg, #141e45, #c9435a); }

.word-check { display: flex; align-items: center; gap: 8px; flex-wrap: wrap; }
.word-check input { text-transform: uppercase; width: 180px; letter-spacing: 0.06em; }
.word-result { font-weight: 700; }
.word-result.yes { color: var(--good); }
.word-result.no { color: var(--bad); }

.stat-table, .over-table, .keys { width: 100%; border-collapse: collapse; font-variant-numeric: tabular-nums; }
.stat-table th, .stat-table td { text-align: right; padding: 6px 6px; border-bottom: 1px solid var(--line); font-size: 14px; }
.stat-table th:first-child, .stat-table td:first-child { text-align: left; }
.stat-table th { color: var(--muted); font-weight: 600; font-size: 12px; }
.stat-grid { display: grid; grid-template-columns: repeat(auto-fit, minmax(150px, 1fr)); gap: 8px; margin: 12px 0; }
.stat-grid div { background: var(--panel-2); border-radius: 10px; padding: 8px 10px; }
.stat-grid dt { font-size: 12px; color: var(--muted); }
.stat-grid dd { margin: 2px 0 0; font-weight: 750; font-size: 17px; overflow-wrap: anywhere; }
.keys td { padding: 5px 6px; vertical-align: top; font-size: 14px; }

.letter-grid { display: grid; grid-template-columns: repeat(auto-fill, minmax(44px, 1fr)); gap: 6px; }
.letter-grid button { padding: 0; min-height: 44px; font: 800 20px/1 "Avenir Next", ui-rounded, system-ui, sans-serif; background: linear-gradient(170deg, var(--tile-1), var(--tile-2)); color: var(--tile-blank); border: 0; box-shadow: inset 0 -3px 0 var(--tile-edge); }
.letter-grid button:hover { filter: brightness(1.06); background: linear-gradient(170deg, var(--tile-1), var(--tile-2)); }

.swap-rack { display: flex; gap: 8px; flex-wrap: wrap; justify-content: center; margin: 14px 0 6px; }
.swap-rack button { padding: 0; border: 0; background: none; min-height: 0; border-radius: 10px; }
.swap-rack button .tile { --size: 50px; transition: transform 0.15s; }
.swap-rack button[aria-pressed="true"] .tile { transform: translateY(-6px); box-shadow: inset 0 -3px 0 var(--tile-edge), 0 0 0 3px var(--accent), 0 6px 12px rgba(0, 0, 0, 0.4); }

.legend { display: grid; grid-template-columns: repeat(auto-fit, minmax(250px, 1fr)); gap: 6px 16px; }
.legend div { display: flex; align-items: center; gap: 10px; }
.legend p { margin: 0; }
.sq { display: grid; place-items: center; flex: none; width: 34px; height: 34px; border-radius: 5px; font: 800 12px/1 system-ui, sans-serif; }
.sq-TW { background: var(--tw); color: var(--tw-ink); }
.sq-DW { background: var(--dw); color: var(--dw-ink); }
.sq-TL { background: var(--tl); color: var(--tl-ink); }
.sq-DL { background: var(--dl); color: var(--dl-ink); }

.over { text-align: center; position: relative; padding-top: 22px; }
.over h2 { font-size: 30px; margin: 4px 0 6px; }
.over-table { max-width: 440px; margin: 14px auto 6px; }
.over-table td { padding: 7px 8px; border-bottom: 1px solid var(--line); text-align: right; }
.over-table td:first-child { text-align: left; font-weight: 650; }
.over-table td.adj { color: var(--muted); font-size: 13px; }
.over-table td.total { font-weight: 800; font-size: 19px; }
.over-table tr.win td.total { color: var(--accent-2); }
.over-notes { list-style: none; padding: 0 !important; margin: 10px 0 0; display: flex; flex-wrap: wrap; gap: 6px; justify-content: center; }
.over-notes li { padding: 5px 10px; border-radius: 999px; background: var(--panel-2); font-size: 13px; }
.over-burst { position: absolute; inset: 0; pointer-events: none; overflow: hidden; }

.sr-only { position: absolute; width: 1px; height: 1px; overflow: hidden; clip: rect(0 0 0 0); white-space: nowrap; }

/* --- smaller screens -------------------------------------------------------------- */

@media (max-width: 1000px), (orientation: portrait) {
  .table { grid-template-columns: minmax(0, 1fr); padding-top: 8px; }
  .side {
    position: fixed;
    top: 0;
    right: 0;
    bottom: 0;
    width: min(340px, 88vw);
    z-index: 20;
    border-radius: 16px 0 0 16px;
    transform: translateX(105%);
    transition: transform 0.25s ease;
    box-shadow: -10px 0 40px rgba(0, 0, 0, 0.5);
    background: var(--panel);
  }
  body.side-open .side { transform: none; }
  .side-close { display: inline-flex; }
}
@media (min-width: 1001px) and (orientation: landscape) {
  .side-toggle { display: none; }
}

@media (max-width: 720px) {
  .hud { gap: 8px; padding-top: max(6px, env(safe-area-inset-top)); padding-bottom: 6px; flex-wrap: wrap; }
  .brand-word { display: none; }
  .hud-actions .lbl { display: none; }
  .hud-actions button:first-child { width: 38px; padding: 0; }
  .scoreboard { order: 3; flex-basis: 100%; gap: 6px; flex-wrap: nowrap; }
  .player { min-width: 0; flex: 1; padding: 4px 9px 4px 8px; }
  .player .score { font-size: 20px; }
  .brand { order: 1; }
  .hud-actions { order: 2; margin-left: auto; }
  .actions .lbl { display: none; }
  .tool { width: 44px; padding: 0; }
  .rack-bar { gap: 8px; }
  .rack { gap: 4px; }
  .rack-shelf { padding: 6px 7px 8px; }
  .actions { width: 100%; flex-wrap: nowrap; }
  .play { flex: 1; min-width: 0; }
  .status { font-size: 13px; margin: 6px 0 4px; }
  .table { gap: 8px; padding-left: max(8px, env(safe-area-inset-left)); padding-right: max(8px, env(safe-area-inset-right)); }
  .daily-card { grid-template-columns: auto minmax(0, 1fr); }
  .daily-card button { grid-column: 1 / -1; }
  .field { grid-template-columns: minmax(0, 1fr); gap: 4px; }
  .stat-table th:nth-child(6), .stat-table td:nth-child(6), .stat-table th:nth-child(7), .stat-table td:nth-child(7) { display: none; }
}

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