/* ---- themes ------------------------------------------------------------ */

:root {
  --font: ui-rounded, "SF Pro Rounded", "Nunito", system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
  --cw: clamp(58px, min(9.2vw, 13.5vh), 104px);
  --card: #fffdf8;
  --card-edge: rgba(0, 0, 0, 0.16);
  /* Four-colour deck: each suit has its own ink. */
  --spade: #1c1f26;
  --heart: #d5352c;
  --club: #1b8a4b;
  --diamond: #1f6fd8;
  /* One colour per player, by where they sit from your chair: you, left, across, right. */
  --p0: #4f9ff0;
  --p1: #ee7961;
  --p2: #a98cf5;
  --p3: #38c6b4;
}
[data-deck="2"] { --club: var(--spade); --diamond: var(--heart); }

[data-theme="felt"] {
  --bg: #0b241c;
  --felt-1: #2e8262;
  --felt-2: #164c38;
  --ink: #f3f6f1;
  --muted: rgba(243, 246, 241, 0.66);
  --panel: rgba(6, 26, 19, 0.7);
  --panel-edge: rgba(255, 255, 255, 0.11);
  --sheet: #103528;
  --accent: #f4c95d;
  --accent-ink: #2b2104;
  --back-1: #2c4fa8;
  --back-2: #1c3577;
}

[data-theme="midnight"] {
  --bg: #090b14;
  --felt-1: #2a3357;
  --felt-2: #12162a;
  --ink: #eef0f8;
  --muted: rgba(238, 240, 248, 0.62);
  --panel: rgba(9, 11, 22, 0.7);
  --panel-edge: rgba(255, 255, 255, 0.1);
  --sheet: #1a2038;
  --accent: #ffb454;
  --accent-ink: #2a1800;
  --back-1: #b9473d;
  --back-2: #8a2e27;
}

[data-theme="linen"] {
  --bg: #e9e2d3;
  --felt-1: #fcf9f2;
  --felt-2: #ebe4d4;
  --ink: #26241e;
  --muted: rgba(38, 36, 30, 0.6);
  --panel: rgba(255, 255, 255, 0.88);
  --panel-edge: rgba(0, 0, 0, 0.09);
  --sheet: #fffdf8;
  --accent: #1f7a63;
  --accent-ink: #ffffff;
  --back-1: #2a4470;
  --back-2: #1b2e50;
  --p0: #2f7fd1;
  --p1: #d9573d;
  --p2: #7b5fd6;
  --p3: #17968a;
  --card: #ffffff;
}

/* ---- base -------------------------------------------------------------- */

* { box-sizing: border-box; }
html, body { height: 100%; margin: 0; }
body {
  background: var(--bg);
  color: var(--ink);
  font: 500 15px/1.3 var(--font);
  overflow: hidden;
  -webkit-user-select: none;
  user-select: none;
  -webkit-tap-highlight-color: transparent;
  touch-action: manipulation;
}
button, input { font: inherit; color: inherit; }
button { cursor: pointer; border: 0; background: none; padding: 0; }
h1, h2, h3, p { margin: 0; }
[hidden] { display: none !important; }

.p0 { --tc: var(--p0); }
.p1 { --tc: var(--p1); }
.p2 { --tc: var(--p2); }
.p3 { --tc: var(--p3); }

.btn {
  display: inline-flex; align-items: center; justify-content: center; gap: 8px;
  padding: 11px 20px; border-radius: 999px; font-weight: 700; white-space: nowrap;
  background: color-mix(in srgb, var(--ink) 10%, transparent);
  border: 1px solid var(--panel-edge);
  transition: transform 0.12s, filter 0.12s, background 0.12s;
}
.btn:hover { background: color-mix(in srgb, var(--ink) 16%, transparent); }
.btn:active { transform: scale(0.97); }
.btn.primary { background: var(--accent); color: var(--accent-ink); border-color: transparent; }
.btn.primary:hover { filter: brightness(1.07); }
.btn.small { padding: 7px 14px; font-size: 13px; }
.btn[disabled] { opacity: 0.5; pointer-events: none; }
.link { white-space: nowrap; color: var(--muted); font-size: 13px; font-weight: 600; text-decoration: underline; text-underline-offset: 3px; }
.link:hover { color: var(--ink); }

/* ---- cards ------------------------------------------------------------- */

.card {
  position: relative; flex: 0 0 var(--cw); width: var(--cw); height: calc(var(--cw) * 1.42);
  border-radius: calc(var(--cw) * 0.1);
  background: var(--card);
  box-shadow: 0 0 0 1px var(--card-edge), 0 2px 5px rgba(0, 0, 0, 0.22);
  color: var(--spade);
  font-family: var(--font);
}
.card.suit-H { color: var(--heart); }
.card.suit-C { color: var(--club); }
.card.suit-D { color: var(--diamond); }
.card .idx {
  position: absolute; left: 7%; top: 4%;
  display: flex; flex-direction: column; align-items: center; line-height: 1;
}
.card .idx b { font-size: calc(var(--cw) * 0.34); font-weight: 800; letter-spacing: -0.06em; }
.card.ten .idx b { font-size: calc(var(--cw) * 0.31); letter-spacing: -0.11em; margin: 3% 0 2% -6%; }
.card .idx i { display: block; width: calc(var(--cw) * 0.24); margin-top: 8%; }
.card .pip { position: absolute; right: 5%; bottom: 4%; width: calc(var(--cw) * 0.68); }
.card svg { display: block; width: 100%; height: auto; }

.back {
  border-radius: 3px;
  background:
    repeating-linear-gradient(45deg, rgba(255, 255, 255, 0.1) 0 2px, transparent 2px 6px),
    linear-gradient(160deg, var(--back-1), var(--back-2));
  box-shadow: 0 0 0 1px rgba(255, 255, 255, 0.7), 0 1px 2px rgba(0, 0, 0, 0.3);
}

/* ---- room -------------------------------------------------------------- */

.room { height: 100dvh; display: grid; grid-template-columns: minmax(0, 1fr); grid-template-rows: auto minmax(0, 1fr) auto; }

.bar {
  position: relative; z-index: 5; display: flex; align-items: center; gap: 8px;
  padding: max(10px, env(safe-area-inset-top)) 12px 8px;
}
.bar .spacer { flex: 1; }
.chip {
  display: inline-flex; align-items: center; gap: 8px; height: 40px; padding: 0 14px;
  border-radius: 999px; background: var(--panel); border: 1px solid var(--panel-edge);
  font-weight: 700; white-space: nowrap;
}
.chip small { color: var(--muted); font-weight: 600; font-size: 12px; }
.chip.icon { width: 40px; padding: 0; justify-content: center; font-size: 18px; }
.chip:hover { border-color: color-mix(in srgb, var(--ink) 30%, transparent); }

.felt {
  position: relative; margin: 0 10px; min-height: 0; border-radius: 28px;
  container-type: size; /* lets the trick scale to the table, not the window */
  /* The size of a card in the trick. Set here so the seats can keep clear of it. */
  --ts: 0.94;
  --cap: 27cqh;
  --th: min(calc(var(--cw) * var(--ts) * 1.42), var(--cap), 27vw);
  --tw: calc(var(--th) / 1.42);
  background: radial-gradient(ellipse 80% 70% at 50% 42%, var(--felt-1), var(--felt-2));
  box-shadow:
    inset 0 0 0 1px rgba(255, 255, 255, 0.07),
    inset 0 0 90px rgba(0, 0, 0, 0.22),
    0 14px 40px rgba(0, 0, 0, 0.28);
}

/* Felt fibres: a seamless grey tile laid over whichever theme colour is underneath. */
.felt::before, .home::before {
  content: ""; position: absolute; inset: 0; z-index: 0; border-radius: inherit; pointer-events: none;
  background: url(/felt.webp) 0 0 / 640px 320px;
  mix-blend-mode: overlay; opacity: 0.125;
}
.felt { transition: box-shadow 0.15s; }
.drop-ok .felt { box-shadow: inset 0 0 0 3px var(--accent), inset 0 0 90px rgba(0, 0, 0, 0.22), 0 14px 40px rgba(0, 0, 0, 0.28); }

/* ---- scoreboard -------------------------------------------------------- */

/* Lives in the top bar's markup; on a wide screen it hangs just below it, over the table's corner.
   A row per player: name, score, and the points taken so far this hand. */
.board {
  position: absolute; top: calc(100% + 12px); right: 22px;
  display: grid; grid-template-columns: auto auto auto; align-items: stretch;
  border-radius: 14px; overflow: hidden; text-align: center;
  background: var(--panel); border: 1px solid var(--panel-edge);
  -webkit-backdrop-filter: blur(8px); backdrop-filter: blur(8px);
  font-variant-numeric: tabular-nums;
}
.board .pl { display: contents; }
.board .pl > * {
  display: grid; place-items: center; padding: 3px 11px; min-height: 32px;
  background: color-mix(in srgb, var(--tc) 30%, transparent); border-top: 1px solid var(--panel-edge);
}
.board .head > * { min-height: 0; padding: 5px 11px 4px; background: none; border-top: 0; font-size: 10.5px; font-weight: 700; letter-spacing: 0.05em; text-transform: uppercase; color: var(--muted); }
.board .pl > .tn { display: block; text-align: left; font-size: 13px; font-weight: 700; max-width: 14ch; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; line-height: 26px; }
.board b { font-size: 18px; font-weight: 800; }
.board .hp { font-size: 14px; font-weight: 700; }
.board:hover { border-color: color-mix(in srgb, var(--ink) 30%, transparent); }

/* ---- seats ------------------------------------------------------------- */

.seat {
  position: absolute; display: flex; align-items: center; gap: 9px;
  padding: 6px 14px 6px 6px; border-radius: 999px;
  background: color-mix(in srgb, var(--tc) 26%, var(--panel));
  border: 1px solid color-mix(in srgb, var(--tc) 50%, transparent);
  -webkit-backdrop-filter: blur(8px); backdrop-filter: blur(8px);
  transition: box-shadow 0.25s, background 0.25s, transform 0.25s;
  z-index: 2;
}
.seat.pos-top { top: 12px; left: 50%; transform: translateX(-50%); }
.seat.pos-bottom { bottom: 10px; left: 50%; transform: translateX(-50%); }
.seat.pos-left { left: 10px; top: 45%; transform: translateY(-50%); }
.seat.pos-right { right: 10px; top: 45%; transform: translateY(-50%); }
.seat.turn { box-shadow: 0 0 0 2px var(--accent), 0 0 24px color-mix(in srgb, var(--accent) 50%, transparent); }
.seat.won { box-shadow: 0 0 0 3px var(--accent); } /* whoever takes the trick wears the winning card's gold ring */
.seat.away .avatar, .seat.away .who { opacity: 0.5; }
.seat.empty { padding: 6px; gap: 6px; border-style: dashed; }
.seat.empty .link { padding: 0 10px 0 4px; }

.avatar {
  position: relative; flex: none; width: 36px; height: 36px; border-radius: 50%;
  display: grid; place-items: center; font-weight: 800; font-size: 16px;
  color: #fff; background: var(--tc);
  text-shadow: 0 1px 1px rgba(0, 0, 0, 0.25);
}
.avatar > svg:not(.ring) { width: 23px; height: 23px; filter: drop-shadow(0 1px 1px rgba(0, 0, 0, 0.2)); }
.ring { position: absolute; inset: -5px; width: calc(100% + 10px); height: calc(100% + 10px); transform: rotate(-90deg); pointer-events: none; }
.ring circle {
  fill: none; stroke: var(--accent); stroke-width: 3.5; stroke-linecap: round;
  stroke-dasharray: 100; stroke-dashoffset: var(--from);
  animation: drain linear forwards;
}
@keyframes drain { to { stroke-dashoffset: 100; } }

.who { min-width: 0; }
.who .name { display: flex; align-items: center; gap: 6px; font-weight: 700; white-space: nowrap; }
.who .name span { overflow: hidden; text-overflow: ellipsis; max-width: 13ch; }
.who .stat { font-size: 12.5px; color: var(--muted); font-weight: 600; font-variant-numeric: tabular-nums; white-space: nowrap; }
.seat.turn .stat { color: var(--accent); }
.tag {
  flex: none; font-size: 10px; font-weight: 800; font-style: normal; letter-spacing: 0.04em; line-height: 1;
  padding: 3px 5px; border-radius: 5px;
  background: color-mix(in srgb, var(--ink) 16%, transparent); color: var(--ink);
}
.seat .x { width: 24px; height: 24px; border-radius: 50%; color: var(--muted); font-size: 16px; line-height: 1; margin-right: -6px; }
.seat .x:hover { background: color-mix(in srgb, var(--ink) 14%, transparent); color: var(--ink); }

/* When the table is waiting on you, your own seat becomes the banner. */
.seat.me.turn {
  background: var(--accent); border-color: transparent; color: var(--accent-ink);
  padding: 8px 22px 8px 8px; transform: translateX(-50%) scale(1.12); transform-origin: 50% 100%;
  animation: beckon 1.6s ease-in-out infinite;
}
.seat.me.turn .name { font-size: 18px; font-weight: 800; }
.seat.me.turn .stat { color: inherit; opacity: 0.75; }
.seat.me.turn .avatar { background: var(--accent-ink); color: var(--accent); text-shadow: none; }
.seat.me.turn .ring circle { stroke: var(--accent-ink); }
@keyframes beckon {
  0%, 100% { box-shadow: 0 0 0 0 color-mix(in srgb, var(--accent) 70%, transparent), 0 8px 24px rgba(0, 0, 0, 0.3); }
  50% { box-shadow: 0 0 0 12px color-mix(in srgb, var(--accent) 0%, transparent), 0 8px 24px rgba(0, 0, 0, 0.3); }
}

/* A small fan of card backs beside each opponent. */
.fan { position: absolute; display: flex; pointer-events: none; }
.fan .back { flex: none; width: 20px; height: 28px; margin-left: -14px; }
.fan .back:first-child { margin-left: 0; }
.pos-top .fan { right: calc(100% + 12px); top: 50%; transform: translateY(-50%); }
.pos-left .fan { left: 6px; top: calc(100% + 26px); transform-origin: 0 0; transform: rotate(90deg) translate(0, -100%); }
.pos-right .fan { right: 6px; top: calc(100% + 26px); transform-origin: 100% 0; transform: rotate(-90deg) translate(0, -100%); }
.pos-bottom .fan { display: none; }

/* ---- the trick --------------------------------------------------------- */

.trick { position: absolute; left: 50%; top: 47%; width: 0; height: 0; z-index: 1; }
.trick .card {
  position: absolute; width: var(--tw); height: var(--th);
  left: calc(var(--tw) / -2); top: calc(var(--th) / -2);
  --cw: var(--tw);
  transform: translate(var(--x), var(--y)) rotate(var(--r));
  --settle: 0.45s cubic-bezier(0.22, 1, 0.36, 1); /* a dragged card easing into its place */
  transition: transform 0.34s cubic-bezier(0.2, 0.8, 0.2, 1), translate var(--settle), scale var(--settle), rotate var(--settle), opacity 0.2s, box-shadow 0.2s;
  box-shadow: 0 0 0 1px var(--card-edge), 0 6px 14px rgba(0, 0, 0, 0.3);
}
/* Stacked by seat rather than by who played first, so no card ever sits on another's corner index. */
.trick .slot-bottom { --x: 0px; --y: calc(var(--th) * 0.54); --r: -2deg; z-index: 3; }
.trick .slot-top { --x: 0px; --y: calc(var(--th) * -0.54); --r: 2deg; z-index: 2; }
.trick .slot-left { --x: calc(var(--tw) * -0.98); --y: 0px; --r: -5deg; z-index: 1; }
.trick .slot-right { --x: calc(var(--tw) * 0.98); --y: 0px; --r: 5deg; z-index: 4; }
.trick .enter { opacity: 0; }
.trick .enter.slot-bottom, .trick .exit.to-bottom { --x: 0px; --y: 42vh; }
.trick .enter.slot-top, .trick .exit.to-top { --x: 0px; --y: -42vh; }
.trick .enter.slot-left, .trick .exit.to-left { --x: -46vw; --y: 0px; }
.trick .enter.slot-right, .trick .exit.to-right { --x: 46vw; --y: 0px; }
.trick .exit { --r: 0deg; opacity: 0; transition: transform 0.45s cubic-bezier(0.5, 0, 0.9, 0.4), opacity 0.45s ease-in; }
.trick .win { box-shadow: 0 0 0 3px var(--accent), 0 8px 22px rgba(0, 0, 0, 0.4); }

/* ---- centre of the table ---------------------------------------------- */

.center {
  position: absolute; left: 50%; top: 47%; transform: translate(-50%, -50%);
  width: min(92%, 460px); text-align: center; z-index: 3; pointer-events: none;
}
.center > * { pointer-events: auto; }
.panel {
  max-height: calc(100cqh - 16px); overflow-y: auto;
  padding: 20px; border-radius: 22px;
  background: var(--panel); border: 1px solid var(--panel-edge);
  -webkit-backdrop-filter: blur(12px); backdrop-filter: blur(12px);
  box-shadow: 0 16px 40px rgba(0, 0, 0, 0.25);
}
.panel h2 { font-size: 22px; font-weight: 800; letter-spacing: -0.01em; }
.panel h2 code { font-family: ui-monospace, "SF Mono", Menlo, monospace; letter-spacing: 0.08em; }
.panel p { color: var(--muted); margin-top: 6px; }
.panel .row { display: flex; align-items: center; justify-content: center; gap: 10px; flex-wrap: wrap; margin-top: 16px; }
.status {
  display: inline-block; max-width: 100%; padding: 9px 18px; border-radius: 22px; font-weight: 700; font-size: 15px; overflow-wrap: anywhere;
  background: var(--panel); border: 1px solid var(--panel-edge);
}

/* During play the middle belongs to the cards, so notices sit just above your seat. */
:is([data-phase="playing"], [data-phase="trickEnd"]) .center { top: auto; bottom: 64px; transform: translateX(-50%); }

.btn.wide { width: 100%; padding: 13px 20px; font-size: 16px; }
/* Built once and updated in place. Nothing is clipped, so cards can fly in and out of its tray. */
.panel.pass { max-width: 340px; max-height: none; margin: 0 auto; overflow: visible; }
.panel.pass .row { margin-top: 14px; }
/* The three cards chosen to pass wait here, under the button, until everyone is ready. */
.tray { display: flex; justify-content: center; gap: 10px; margin-top: 14px; --cw: clamp(44px, 12cqh, 62px); }
.tray .slot {
  flex: none; width: var(--cw); height: calc(var(--cw) * 1.42); border-radius: calc(var(--cw) * 0.1);
  outline: 1.5px dashed color-mix(in srgb, var(--ink) 26%, transparent); outline-offset: -1.5px;
}
.tray .card { cursor: pointer; box-shadow: 0 0 0 1px var(--card-edge), 0 5px 12px rgba(0, 0, 0, 0.3); }
.tray .card:hover { box-shadow: 0 0 0 2px var(--accent), 0 5px 12px rgba(0, 0, 0, 0.3); }
.tray.locked .card { cursor: default; box-shadow: 0 0 0 2px var(--accent), 0 5px 12px rgba(0, 0, 0, 0.3); }
/* A card on its way to another player, lifted out of the page. */
.card.flying { position: fixed; z-index: 40; margin: 0; pointer-events: none; box-shadow: 0 0 0 1px var(--card-edge), 0 10px 22px rgba(0, 0, 0, 0.35); }
.panel.pass h2 { font-size: 20px; }
/* Only ever shown on your turn, when your seat below has grown into the banner: keep clear of it. */
.btn.tram { padding: 12px 22px; margin-bottom: 20px; font-size: 16px; box-shadow: 0 10px 26px rgba(0, 0, 0, 0.3); }
.btn.tram small { font-size: 13px; font-weight: 600; opacity: 0.75; }

.seg { display: inline-flex; padding: 3px; border-radius: 999px; background: color-mix(in srgb, var(--ink) 9%, transparent); }
.seg button { padding: 6px 11px; border-radius: 999px; font-weight: 700; font-size: 13px; color: var(--muted); }
.seg button.on { background: var(--ink); color: var(--bg); }
.opts { display: grid; gap: 6px; margin-top: 14px; justify-content: center; }
.opt { display: grid; grid-template-columns: 116px auto; align-items: center; justify-items: start; gap: 10px; font-size: 13px; font-weight: 600; color: var(--muted); text-align: right; }
.opt { justify-items: start; }

.corner { position: absolute; left: 12px; bottom: 12px; z-index: 4; }
.corner .chip { height: 34px; font-size: 13px; padding: 0 12px; }
.peek {
  position: absolute; left: 0; bottom: 42px; padding: 12px 14px; border-radius: 16px;
  background: var(--panel); border: 1px solid var(--panel-edge);
  -webkit-backdrop-filter: blur(12px); backdrop-filter: blur(12px);
  display: flex; gap: 6px; --cw: 46px;
}
.peek figure { margin: 0; text-align: center; font-size: 11px; font-weight: 700; color: var(--muted); }
.peek figure.won { color: var(--accent); }
.peek figcaption { margin-top: 5px; max-width: 46px; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }

/* ---- your hand --------------------------------------------------------- */

.hand {
  --cw: clamp(62px, min(10.5vw, 18vh), 132px);
  position: relative; height: calc(var(--cw) * 1.58 + 30px);
  padding-bottom: env(safe-area-inset-bottom); box-sizing: content-box;
}
/* Nothing is dealt yet in the lobby, so the table gets the room. */
[data-phase="lobby"] .hand { height: 14px; }
.hand .cards { position: absolute; left: 14px; right: 14px; top: 16px; height: calc(var(--cw) * 1.42); display: flex; justify-content: center; }
.hand .card {
  --lift: 0px;
  transform: translateY(calc(var(--dy, 0px) + var(--lift))) rotate(var(--rot, 0deg));
  transition: transform 0.18s, translate 0.2s, filter 0.2s, opacity 0.14s, box-shadow 0.2s;
  touch-action: none; /* so a finger can drag a card rather than pan the page */
}
.hand .card.fresh { transition: none; }
.hand .card.dragging {
  z-index: 50; cursor: grabbing; --lift: 0px !important;
  transition: box-shadow 0.12s;
  box-shadow: 0 0 0 2px var(--accent), 0 18px 34px rgba(0, 0, 0, 0.45);
}
.hand.live .card.legal { cursor: grab; }
/* Not your turn: the hand sits back a little. Your turn: it lights up. */
.hand.waiting .card { filter: brightness(0.84); }
.hand.live .card.legal { cursor: grab; }
/* Mouse only: on a touch screen the hover sticks to whichever card ends up where you last tapped. */
@media (hover: hover) { .hand.live .card.legal:hover { --lift: calc(var(--cw) * -0.22); } }
.hand.live .card:not(.legal) { filter: brightness(0.6) saturate(0.7); }
[data-theme="linen"] .hand.live .card:not(.legal) { filter: none; opacity: 0.42; }
.hand .card.sent { --lift: calc(var(--cw) * -0.5); opacity: 0; }
.hand .card.nope { animation: nope 0.3s; }
@keyframes nope { 25% { translate: -5px 0; } 75% { translate: 5px 0; } }
/* Your turn: the cards you may play stand a little proud with a gold edge. */
.hand.live .card.legal { --lift: calc(var(--cw) * -0.06); box-shadow: 0 0 0 2px var(--accent), 0 4px 12px rgba(0, 0, 0, 0.3); }

/* Passing: tap a card to send it up to the tray under the pass button. */
.hand.picking .card { cursor: pointer; }
@media (hover: hover) { .hand.picking .card:hover { --lift: calc(var(--cw) * -0.09); } }

/* Shown in place of a hand when you are not seated. */
.watching { position: absolute; inset: 0; display: grid; place-items: center; margin: 0; padding: 0 24px; text-align: center; font-weight: 600; color: var(--muted); }

/* ---- sheets, menu, toast ---------------------------------------------- */

.overlay {
  position: fixed; inset: 0; z-index: 20; display: grid; place-items: center; padding: 16px;
  background: rgba(0, 0, 0, 0.45); animation: fade 0.2s;
}
.sheet {
  width: min(100%, 460px); max-height: 100%; overflow: hidden auto; padding: 24px; border-radius: 24px; text-align: center;
  background: var(--sheet); border: 1px solid var(--panel-edge); box-shadow: 0 30px 80px rgba(0, 0, 0, 0.45);
  animation: rise 0.25s cubic-bezier(0.2, 0.8, 0.2, 1);
}
.sheet.wide { width: min(100%, 600px); }
.sheet h2 { font-size: 24px; font-weight: 800; overflow-wrap: anywhere; }
.sheet p { color: var(--muted); margin-top: 6px; }
.sheet .row { display: flex; justify-content: center; gap: 10px; flex-wrap: wrap; margin-top: 20px; }
.sheet input {
  width: 100%; margin-top: 16px; padding: 13px 16px; border-radius: 14px; text-align: center; font-weight: 700; font-size: 17px;
  background: color-mix(in srgb, var(--ink) 8%, transparent); border: 1px solid var(--panel-edge); outline: none;
  -webkit-user-select: text; user-select: text;
}
.sheet input:focus { border-color: var(--accent); }
@keyframes fade { from { opacity: 0; } }
@keyframes rise { from { opacity: 0; transform: translateY(14px) scale(0.98); } }

.countdown { margin-top: 18px; font-size: 13px; font-weight: 600; color: var(--muted); }
.countdown i { display: block; height: 4px; margin-top: 8px; border-radius: 2px; background: var(--accent); transform-origin: 0 50%; animation: shrink linear forwards; }
@keyframes shrink { to { transform: scaleX(0); } }

.sheet .story { color: var(--ink); font-weight: 600; }
.tally { width: 100%; margin-top: 16px; border-collapse: collapse; table-layout: fixed; font-variant-numeric: tabular-nums; }
.tally th:first-child { width: 25%; }
.tally th, .tally td { padding: 7px 6px; text-align: right; }
.tally th:first-child, .tally td:first-child { text-align: left; color: var(--muted); font-weight: 600; white-space: nowrap; }
.tally thead th { font-size: 13px; font-weight: 700; color: var(--tc); border-bottom: 1px solid var(--panel-edge); }
.tally thead th span { display: block; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.tally .sum td { border-top: 1px solid var(--panel-edge); font-weight: 700; }
.tally .total td { font-size: 20px; font-weight: 800; }
.tally .total td:first-child { font-size: 15px; }

/* The full score sheet: current standings, then a row per hand with a column per player. */
.standings { display: grid; grid-template-columns: 1fr 1fr; gap: 8px; margin-top: 16px; }
.standing {
  display: grid; grid-template-columns: auto 1fr auto; align-items: center; gap: 8px; padding: 8px 14px 8px 12px; border-radius: 14px;
  background: color-mix(in srgb, var(--tc) 22%, transparent); text-align: left; min-width: 0;
}
.standing span { font-size: 13px; font-weight: 700; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.standing b { font-size: 22px; font-weight: 800; font-variant-numeric: tabular-nums; }
.dot { flex: none; width: 10px; height: 10px; border-radius: 50%; background: var(--tc); }
.scroll { max-height: min(46vh, 420px); overflow: auto; margin-top: 10px; }
.tally.log { margin-top: 0; }
.tally.log thead th { position: sticky; top: 0; z-index: 1; background: var(--sheet); font-size: 12px; }
.tally.log tbody td { padding: 8px 6px; border-top: 1px solid var(--panel-edge); vertical-align: top; }
.tally.log tbody tr:first-child td { border-top: 0; }
.tally.log td b { display: block; font-size: 16px; font-weight: 800; color: var(--ink); }
.tally.log td small { display: block; font-size: 11.5px; font-weight: 600; color: var(--muted); }
.tally.log td:first-child b { font-size: 13px; font-weight: 700; }
.tally.log td.shot small { color: var(--accent); }

.menu {
  position: fixed; right: 12px; top: calc(max(10px, env(safe-area-inset-top)) + 46px); z-index: 15; width: 250px; padding: 8px;
  max-height: calc(100dvh - 70px); overflow-y: auto;
  border-radius: 18px; background: var(--sheet); border: 1px solid var(--panel-edge); box-shadow: 0 20px 50px rgba(0, 0, 0, 0.4);
  animation: rise 0.16s;
}
.menu .item { display: flex; align-items: center; gap: 8px; width: 100%; text-align: left; padding: 10px 12px; border-radius: 11px; font-weight: 600; }
.menu button.item:hover { background: color-mix(in srgb, var(--ink) 9%, transparent); }
.menu .item.static { justify-content: space-between; padding: 6px 8px 6px 12px; }
.menu .seg button { padding: 5px 8px; font-size: 12px; }
.menu .check { width: 16px; font-style: normal; font-weight: 800; color: var(--accent); }
.menu hr { border: 0; border-top: 1px solid var(--panel-edge); margin: 6px 4px; }
.menu .swatches { display: flex; gap: 8px; padding: 8px 12px 10px; }
.swatch { width: 34px; height: 34px; border-radius: 50%; border: 2px solid transparent; box-shadow: 0 0 0 1px var(--panel-edge); }
.swatch.on { border-color: var(--accent); }
.swatch[data-set-theme="felt"] { background: radial-gradient(#2e8262, #164c38); }
.swatch[data-set-theme="midnight"] { background: radial-gradient(#2a3357, #12162a); }
.swatch[data-set-theme="linen"] { background: radial-gradient(#fcf9f2, #e0d7c3); }

.toast {
  position: fixed; left: 50%; bottom: 46%; transform: translateX(-50%); z-index: 30;
  padding: 10px 16px; border-radius: 999px; font-weight: 700; font-size: 14px; white-space: nowrap;
  background: var(--ink); color: var(--bg); box-shadow: 0 10px 30px rgba(0, 0, 0, 0.35);
  animation: rise 0.2s;
}

/* ---- home -------------------------------------------------------------- */

.home { position: relative; isolation: isolate; height: 100dvh; display: grid; place-items: center; padding: 20px; background: radial-gradient(ellipse at 50% 30%, var(--felt-1), var(--felt-2) 70%); }
.home .sheet { position: relative; z-index: 1; animation: none; }
.home .logo { width: 64px; margin: 0 auto; color: var(--heart); }
.home .logo svg { display: block; width: 100%; height: auto; }
.home h1 { font-size: 34px; font-weight: 800; letter-spacing: -0.02em; margin-top: 4px; }
.home .btn.primary { width: 100%; margin-top: 12px; padding: 14px; font-size: 17px; }
.home .join { display: flex; gap: 8px; margin-top: 18px; padding-top: 18px; border-top: 1px solid var(--panel-edge); }
.home .join input { margin: 0; text-transform: uppercase; letter-spacing: 0.12em; }
.home .join .btn { flex: none; }

/* A tablet held upright: plenty of height, so the cards can grow. */
@media (min-width: 641px) and (orientation: portrait) {
  .hand { --cw: clamp(62px, 13vw, 132px); }
  .felt { --ts: 1.35; }
}

/* ---- short windows ----------------------------------------------------- */

@media (max-height: 620px) and (min-width: 641px) {
  .hand { --cw: clamp(62px, min(10.5vw, 15vh), 132px); }
  .board .pl > * { min-height: 25px; padding: 1px 10px; }
  .board .head > * { min-height: 0; padding: 4px 10px 3px; }
  .board b { font-size: 16px; }
  .board .pl > .tn { line-height: 22px; }
  .seat.pos-left, .seat.pos-right { top: 62%; }
  .fan { display: none; }
  .felt { --cap: 24cqh; }
  .trick { top: 52%; }
}

/* Narrower than a laptop: the scoreboard's names get less room. */
@media (max-width: 900px) {
  .board .pl > .tn { max-width: 9ch; }
}

/* ---- small screens ----------------------------------------------------- */

@media (max-width: 640px) {
  .chip.table small { display: none; }
  .bar { gap: 6px; padding-left: 8px; padding-right: 8px; }
  .felt { margin: 0 6px; border-radius: 22px; }
  .fan { display: none; }
  .hand .cards { left: 10px; right: 10px; }
  .panel { padding: 16px; }
  .panel .row { margin-top: 12px; }

  /* On a phone the scoreboard sits in the top bar, so the table is clear and your partner is dead centre. */
  .bar .spacer { display: none; }
  .board { position: static; display: flex; flex: 1 1 0; min-width: 0; max-width: 300px; margin-left: auto; height: 46px; }
  .board .pl {
    display: grid; align-content: center; justify-items: center; gap: 2px; flex: 1 1 0; min-width: 0; padding: 0 5px;
    background: color-mix(in srgb, var(--tc) 30%, transparent); border-left: 1px solid var(--panel-edge);
  }
  .board .pl.head + .pl { border-left: 0; }
  .board .pl > * { display: block; min-height: 0; padding: 0; background: none; border: 0; }
  .board .pl.head, .board .pl > .hp { display: none; }
  .board .pl > .tn { max-width: 100%; font-size: 10.5px; line-height: 1.1; text-align: center; color: var(--muted); }
  .board b { font-size: 18px; line-height: 1; }
  .chip.table { flex-direction: column; align-items: flex-start; justify-content: center; gap: 0; height: 46px; border-radius: 14px; line-height: 1.1; }
  .chip.table .word { font-size: 10.5px; font-weight: 700; letter-spacing: 0.05em; text-transform: uppercase; color: var(--muted); }

  /* Bigger cards, smaller name plates. */
  .hand { --cw: clamp(64px, 20vw, 92px); }
  .hand .card .idx { left: 5%; }
  .hand .card.ten .idx b { transform: scaleX(0.86); transform-origin: 0 50%; }
  .felt { --ts: 1.25; }
  /* The side plates ride above their cards, so the group sits lower: the same gap under your partner as above you. */
  .felt { --ty: calc(50% + 47px - var(--th) * 0.27); }
  .trick { top: var(--ty); }
  .seat { gap: 7px; padding: 4px 11px 4px 4px; }
  .avatar { width: 30px; height: 30px; font-size: 14px; }
  .avatar > svg:not(.ring) { width: 19px; height: 19px; }
  .who .name { font-size: 14px; }
  .who .stat { font-size: 11.5px; }
  .seat.me.turn { padding: 6px 18px 6px 6px; }
  .seat.me.turn .name { font-size: 16px; }
  .tally th, .tally td { padding: 7px 4px; }
  .tally th:first-child { width: 27%; }
  .tally .total td { font-size: 18px; }
  .standing { padding: 7px 10px; }
  .standing b { font-size: 19px; }
  .sheet { padding: 20px 16px; }
  .corner { left: 8px; bottom: 66px; }
  :is([data-phase="playing"], [data-phase="trickEnd"]) .center { bottom: 108px; }
  .seat.me.turn { transform: translateX(-50%) scale(1.04); }
  .seat.pos-top { top: 8px; }
  .seat.pos-top .who .name span { max-width: 9ch; }

  /* While cards are in play the side seats stack vertically and sit just above their own cards, so nothing is hidden. */
  .room:not([data-phase="lobby"]):not([data-phase="passing"]) :is(.pos-left, .pos-right) {
    flex-direction: column; gap: 4px; padding: 6px 8px 8px; border-radius: 18px; text-align: center;
    top: calc(var(--ty) - var(--th) / 2 - 14px); transform: translateY(-100%);
  }
  .room:not([data-phase="lobby"]):not([data-phase="passing"]) :is(.pos-left, .pos-right) { width: min(84px, 23vw); }
  .room:not([data-phase="lobby"]):not([data-phase="passing"]) :is(.pos-left, .pos-right) .who { width: 100%; }
  .room:not([data-phase="lobby"]):not([data-phase="passing"]) :is(.pos-left, .pos-right) .name { justify-content: center; }
  .room:not([data-phase="lobby"]):not([data-phase="passing"]) :is(.pos-left, .pos-right) .name span { max-width: 100%; }
  .room:not([data-phase="lobby"]):not([data-phase="passing"]) :is(.pos-left, .pos-right) .tag { position: absolute; top: 3px; right: 5px; }
  .room:not([data-phase="lobby"]):not([data-phase="passing"]) :is(.pos-left, .pos-right) .btn.small { padding: 5px 8px; font-size: 11.5px; }
  .seat.pos-left { left: 6px; }
  .seat.pos-right { right: 6px; }

  /* In the lobby and while passing a panel fills the middle, so they drop below it. */
  :is([data-phase="lobby"], [data-phase="passing"]) :is(.pos-left, .pos-right) { top: auto; bottom: 76px; transform: none; max-width: 47%; }
  :is([data-phase="lobby"], [data-phase="passing"]) .who .name span { max-width: 9ch; }
  :is([data-phase="lobby"], [data-phase="passing"]) .center { top: calc((100cqh - 68px) / 2); }
  .panel { max-height: calc(100cqh - 170px); }
}

/* Short phones: the panels tighten up so every seat stays visible. */
@media (max-width: 640px) and (max-height: 700px) {
  .panel { padding: 12px 14px; border-radius: 18px; }
  .panel h2 { font-size: 19px; }
  .panel p { display: none; }
  .panel .row { margin-top: 10px; }
  .opts { margin-top: 10px; gap: 4px; }
  .btn.wide { padding: 10px 16px; font-size: 15px; }
}

/* A phone on its side: very little height, so everything flattens out. */
@media (max-height: 480px) and (min-width: 641px) {
  .bar { padding-top: 6px; padding-bottom: 6px; }
  .chip { height: 34px; }
  .chip.icon { width: 34px; }
  .hand { --cw: clamp(56px, 9vw, 76px); height: calc(var(--cw) * 1.2 + 10px); }
  .hand .cards { top: 6px; }
  .felt { margin: 0 8px; border-radius: 20px; }
  .seat { padding: 3px 10px 3px 3px; gap: 6px; }
  .avatar { width: 28px; height: 28px; font-size: 13px; }
  .who .name { font-size: 13px; }
  .who .stat { font-size: 11px; }
  .seat.pos-top { top: 6px; }
  /* Your own seat moves to the corner so the trick has the middle to itself. */
  .seat.pos-bottom, .seat.me.turn { left: 8px; bottom: 6px; transform: none; transform-origin: 0 100%; }
  .seat.me.turn { padding: 4px 14px 4px 4px; }
  .seat.me.turn .name { font-size: 15px; }
  .corner { left: auto; right: 8px; bottom: 6px; }
  .peek { left: auto; right: 0; }
  /* The scoreboard joins the top bar as a strip: name and score, side by side. */
  .board { position: static; display: flex; height: 34px; }
  .board .pl {
    display: flex; align-items: center; gap: 6px; padding: 0 10px;
    background: color-mix(in srgb, var(--tc) 30%, transparent); border-left: 1px solid var(--panel-edge);
  }
  .board .pl.head + .pl { border-left: 0; }
  .board .pl > * { display: block; min-height: 0; padding: 0; background: none; border: 0; }
  .board .pl.head, .board .pl > .hp { display: none; }
  .board .pl > .tn { max-width: 7ch; font-size: 12px; line-height: 1.2; }
  .board b { font-size: 15px; }
  .seat.pos-left, .seat.pos-right { top: 56%; }
  .felt { --cap: 36cqh; }
  .trick { top: 58%; }
  :is([data-phase="playing"], [data-phase="trickEnd"]) .center { bottom: auto; top: 6px; left: 30%; }
  .panel { padding: 10px 14px; border-radius: 16px; max-height: calc(100cqh - 8px); }
  .panel h2 { font-size: 17px; }
  .panel p { display: none; }
  .panel .row { margin-top: 8px; }
  .center { width: min(62%, 540px); top: 50%; }
  .btn.wide { padding: 8px 16px; font-size: 14px; }
  .opts { margin-top: 8px; gap: 3px; grid-template-columns: auto auto; column-gap: 18px; }
  [data-phase="lobby"] .hand { height: 8px; }
}
