/* See DESIGN.md for the reasoning behind these tokens. */
:root {
  --felt: #0f5132;
  --felt-deep: #0a3b24;
  --felt-light: #17694a;
  --zone: rgba(4, 30, 18, 0.34);
  --ivory: #fbf7ea;
  --ivory-edge: #d9d1b8;
  --tile-back: #1f8a5b;
  --ink: #1c1c1c;
  --gold: #f2c14e;
  --gold-ink: #3a2a00;
  --red: #c2362b;
  --text: #f4f1e6;
  --text-dim: #c4d8cc;

  --space-1: 4px;
  --space-2: 8px;
  --space-3: 12px;
  --space-4: 16px;
  --space-6: 24px;
  --radius-control: 16px;
  --radius-zone: 18px;

  /* Tile widths: as large as the screen allows in both directions.
     Her hand: 14 tiles plus the gap before the drawn tile must span the width. */
  --hand-w: min(calc((100vw - 56px) / 14.6), 10.4vh, 104px);
  --meld-w: calc(var(--hand-w) * 0.74);
  --small-w: clamp(26px, min(4.25vw, 5.9vh), 62px);
  /* Opponents' shown sets sit in their seat beside the table, a little smaller than discards. */
  --side-w: calc(var(--small-w) * 0.74);
  /* Opponents' hands: standing tile backs, sized so 14 fit down the side of the table. */
  --back-w: clamp(14px, min(1.9vw, 2.5vh), 30px);
  --tiny-w: clamp(24px, 2.8vw, 38px);

  --ease-out: cubic-bezier(0.16, 1, 0.3, 1);
  --ease-bounce: cubic-bezier(0.2, 1.5, 0.4, 1);
}

@media (orientation: portrait) {
  :root { --small-w: clamp(26px, min(6.1vw, 4.1vh), 62px); }
}

* { box-sizing: border-box; }

html, body {
  margin: 0;
  height: 100%;
  overflow: hidden;
  background: var(--felt-deep);
  color: var(--text);
  font-family: -apple-system, "PingFang HK", "PingFang TC", "Microsoft JhengHei", "Noto Sans TC", sans-serif;
  -webkit-user-select: none;
  user-select: none;
  -webkit-tap-highlight-color: transparent;
  touch-action: manipulation;
}

#app { height: 100dvh; }

:focus-visible { outline: 3px solid var(--gold); outline-offset: 3px; }

/* ---------- bilingual labels: Cantonese leads, English is a caption ---------- */

.bi { display: inline-flex; flex-direction: column; align-items: center; line-height: 1.12; }
.bi .zh { font-weight: 600; }
.bi .en { font-size: max(0.46em, 12px); font-weight: 400; opacity: 0.85; white-space: nowrap; }

/* ---------- table ---------- */

.table {
  height: 100%;
  display: grid;
  grid-template-rows: auto minmax(0, 1fr) auto;
  gap: var(--space-2);
  padding:
    max(var(--space-2), env(safe-area-inset-top))
    max(var(--space-3), env(safe-area-inset-right))
    max(var(--space-2), env(safe-area-inset-bottom))
    max(var(--space-3), env(safe-area-inset-left));
  background: radial-gradient(ellipse at 50% 40%, var(--felt-light), var(--felt) 65%, var(--felt-deep));
}

.topbar { display: flex; justify-content: space-between; align-items: center; gap: var(--space-3); }
.topbar-info { display: flex; gap: var(--space-6); font-size: 22px; color: var(--text-dim); }
.topbar-info .bi { align-items: flex-start; }
.topbar-buttons { display: flex; gap: var(--space-2); }

/* ---------- the field: a real table seen from her seat ---------- */

/* Left and right players run the full height; the player across sits above the table. */
.field {
  display: grid; min-height: 0;
  grid-template-columns: auto minmax(0, 1fr) auto;
  grid-template-rows: auto minmax(0, 1fr);
  grid-template-areas: "left top right" "left board right";
  gap: var(--space-2);
}

/* An opponent's seat: name chip, the sets they have shown, and their hand of tile backs. */
.zone {
  position: relative; min-width: 0; min-height: 0;
  display: flex; align-items: center; gap: var(--space-2);
  padding: var(--space-2);
  border-radius: var(--radius-zone);
  background: var(--zone);
  border: 2px solid transparent;
  overflow: hidden;
}
.zone-active { border-color: var(--gold); background: rgba(4, 30, 18, 0.5); }
/* Tall enough for a row of shown sets from the start, so flowers or a first pung don't push the table down. */
.zone-top { grid-area: top; justify-content: center; gap: var(--space-4); min-height: calc(var(--side-w) * 1.36 + 30px); }
.zone-left { grid-area: left; }
.zone-right { grid-area: right; flex-direction: row-reverse; }
.zone-info { display: flex; flex-direction: column; align-items: center; gap: var(--space-2); min-width: 0; }
.zone-top .zone-info { flex-direction: row; }

/* Standing tiles seen across the table: green backs, with the ivory face edge toward their owner. */
.backs { display: flex; gap: 2px; flex: none; }
.back {
  width: var(--back-w); height: calc(var(--back-w) * 1.3); border-radius: 0.16em;
  font-size: var(--back-w);
  background: linear-gradient(180deg, var(--ivory) 0 18%, #2aa06c 18%, var(--tile-back));
  box-shadow: 0 2px 3px rgba(0, 0, 0, 0.35);
}
/* Space for the backs is held even before any are dealt, so the table never shifts as hands fill. */
.zone-top .backs { min-height: calc(var(--back-w) * 1.3); }
.zone-left .backs, .zone-right .backs { flex-direction: column; min-width: calc(var(--back-w) * 1.3); }
.zone-left .back, .zone-right .back { width: calc(var(--back-w) * 1.3); height: var(--back-w); }
.zone-left .back { background: linear-gradient(90deg, var(--ivory) 0 18%, #2aa06c 18%, var(--tile-back)); }
.zone-right .back { background: linear-gradient(270deg, var(--ivory) 0 18%, #2aa06c 18%, var(--tile-back)); }
.back-in { animation: back-in calc(0.4s * var(--motion, 1)) var(--ease-out) var(--anim-delay, 0s); }
@keyframes back-in { from { transform: scale(1.6); opacity: 0; } }

/* Sets a player has shown (pung/kong/chow, flowers): ringed so they never read as discards. */
.shown { display: inline-flex; flex-wrap: wrap; gap: var(--space-2); align-items: center; justify-content: center; padding: 3px var(--space-2); border-radius: 12px; border: 2px dashed rgba(242, 193, 78, 0.55); }
.zone-left .shown, .zone-right .shown { flex-direction: column; }
.meld { display: inline-flex; gap: 1px; }

/* The table itself. Sized as a container so the walls can be measured from it. */
.board {
  grid-area: board; position: relative; min-width: 0; min-height: 0;
  container-type: size;
  border-radius: var(--radius-zone);
  background: rgba(4, 30, 18, 0.2);
  box-shadow: inset 0 0 0 2px rgba(255, 255, 255, 0.05);
}
/* One wall stack: 18 to a side, sized so a side wall fits the table's height. */
/* Along a long side the stacks may stretch a little, so the walls span more of a wide (or tall) table. */
.board > * {
  --wall-w: calc(min(100cqw, 100cqh) / 21); --wall-d: calc(var(--wall-w) * 1.05);
  --wall-across: min(calc(100cqw / 24), calc(var(--wall-w) * 1.5));
  --wall-down: min(calc(100cqh / 24), calc(var(--wall-w) * 1.5));
}

.wall { position: absolute; display: flex; gap: 1px; }
.wall-bottom { bottom: 4px; left: 50%; transform: translateX(-50%); }
.wall-top { top: 4px; left: 50%; transform: translateX(-50%); }
.wall-left { left: 4px; top: 50%; transform: translateY(-50%); flex-direction: column; }
.wall-right { right: 4px; top: 50%; transform: translateY(-50%); flex-direction: column; }
/* Two tiles high shows a thick lower edge; one tile high sits lower with a thin edge; empty is just felt. */
.stack {
  position: relative; width: var(--wall-w); height: calc(var(--wall-d) - 3px);
  border-radius: calc(var(--wall-w) * 0.16);
  background: linear-gradient(180deg, #2aa06c, var(--tile-back));
  box-shadow: 0 3px 0 #0d4f31, 0 4px 4px rgba(0, 0, 0, 0.35);
}
.wall-top .stack, .wall-bottom .stack { width: var(--wall-across); }
.wall-left .stack, .wall-right .stack { width: calc(var(--wall-d) - 3px); height: calc(var(--wall-down) - 3px); margin-bottom: 3px; }
.stack-1 { transform: translateY(2px); box-shadow: 0 1px 0 #0d4f31, 0 2px 3px rgba(0, 0, 0, 0.3); }
.stack-0 { background: none; box-shadow: inset 0 0 0 1px rgba(255, 255, 255, 0.06); }
/* A tile coming off the wall: lifts and fades away. */
.wall-take::after {
  content: ''; position: absolute; inset: 0; border-radius: inherit;
  background: linear-gradient(180deg, #2aa06c, var(--tile-back));
  opacity: 0; pointer-events: none;
  animation: wall-take calc(0.5s * var(--motion, 1)) var(--ease-out) var(--anim-delay, 0s);
}
@keyframes wall-take {
  0% { opacity: 1; transform: translateY(-3px); }
  100% { opacity: 0; transform: translateY(-120%) scale(1.4); }
}

/* Discards inside the walls, in a pinwheel: each player's pile sits along their own side. */
.board-inner {
  position: absolute; inset: calc(var(--wall-d) + 10px);
  display: grid; gap: var(--space-2);
  grid-template-columns: minmax(0, 1fr) auto minmax(0, 1fr);
  grid-template-rows: minmax(0, 1fr) auto minmax(0, 1fr);
  grid-template-areas: "top top right" "left centre right" "left bottom bottom";
}
/* Each pile sits on its own faint mat, so neighbouring piles never run together. */
.pile {
  position: relative; min-width: 0; min-height: 0; display: flex; flex-wrap: wrap; gap: 3px; align-items: center;
  padding: var(--space-1); border-radius: 12px; background: rgba(0, 0, 0, 0.13);
}
.pile-top { grid-area: top; justify-content: flex-end; align-content: flex-start; }
.pile-right { grid-area: right; justify-content: flex-end; align-content: flex-end; }
.pile-bottom { grid-area: bottom; justify-content: flex-start; align-content: flex-end; }
.pile-left { grid-area: left; justify-content: flex-start; align-content: flex-start; }

/* Tiles left in the wall, with a gold edge on the side of whoever's turn it is. */
.centre {
  grid-area: centre; align-self: center; justify-self: center;
  padding: var(--space-2) var(--space-4); border-radius: 14px;
  background: rgba(0, 0, 0, 0.32); font-size: 24px; color: var(--text-dim); white-space: nowrap;
  border: 4px solid rgba(255, 255, 255, 0.06);
  transition: border-color 0.3s ease-out;
}
.centre.turn-top { border-top-color: var(--gold); }
.centre.turn-bottom { border-bottom-color: var(--gold); }
.centre.turn-left { border-left-color: var(--gold); }
.centre.turn-right { border-right-color: var(--gold); }

/* ---------- player chips ---------- */

.chip {
  flex: none; display: flex; align-items: center; gap: var(--space-3);
  padding: var(--space-1) var(--space-3); border-radius: 14px;
  background: rgba(0, 0, 0, 0.32);
  font-size: 26px; white-space: nowrap;
}
.chip-left, .chip-right { flex-direction: column; gap: var(--space-1); padding: var(--space-2); min-width: 92px; }
.chip-active { background: var(--gold); color: var(--gold-ink); }
.chip-wind { color: var(--gold); }
.chip-active .chip-wind { color: var(--gold-ink); }
.chip-dealer {
  display: inline-flex; align-items: center; gap: 6px;
  padding: 3px 8px 3px 3px; border-radius: 10px;
  background: linear-gradient(180deg, #d9463a, #9e2a21); color: #fff;
  box-shadow: inset 0 1px 0 rgba(255, 255, 255, 0.3), 0 3px 6px rgba(0, 0, 0, 0.35);
}
/* The little tile in the holder: the round's wind, carved like a real tile face. */
.dealer-tile {
  display: grid; place-items: center; width: 1.25em; height: 1.55em;
  border-radius: 5px; background: linear-gradient(180deg, #fffdf5, var(--ivory));
  box-shadow: 0 2px 0 #b9b092; color: var(--ink);
  font-family: "Kaiti TC", "STKaiti", "Songti TC", "PingFang HK", serif; font-weight: 700; font-size: 26px; line-height: 1;
}
.dealer-label .zh { font-size: 22px; }
.dealer-label .en { opacity: 1; }
/* Passed along to the new dealer: it slides in and settles. */
.dealer-arrive { animation: dealer-arrive calc(0.6s * var(--motion, 1)) var(--ease-bounce) var(--anim-delay, 0s); }
@keyframes dealer-arrive {
  0% { transform: translateY(-60%) rotate(-25deg) scale(0.5); opacity: 0; }
  60% { transform: translateY(6%) rotate(4deg) scale(1.08); opacity: 1; }
}
.chip-score { font-variant-numeric: tabular-nums; }

.bubble {
  position: absolute; z-index: 5; left: 50%; top: 50%; transform: translate(-50%, -50%);
  padding: var(--space-2) 34px; border-radius: 999px;
  background: var(--gold); color: var(--gold-ink); font-size: clamp(48px, 8vh, 84px);
  box-shadow: 0 10px 28px rgba(0, 0, 0, 0.5);
  pointer-events: none; white-space: nowrap;
  animation: bubble 0.5s var(--ease-bounce);
}
.bubble .en { font-size: 0.34em; opacity: 1; }
/* Over the table, on the side of whoever called. */
.bubble-top { top: 28%; }
.bubble-left { left: 27%; }
.bubble-right { left: 73%; }

/* ---------- her side: status + buttons, then the hand across the full width ---------- */

.me { display: flex; flex-direction: column; gap: var(--space-1); }
.me-top { position: relative; display: flex; align-items: center; gap: var(--space-3); min-height: 86px; }
/* Wraps onto a second line rather than ever running off the right edge of the screen. */
.actions { margin-left: auto; flex: 0 1 auto; min-width: 0; display: flex; flex-wrap: wrap; align-items: center; justify-content: flex-end; gap: var(--space-2) var(--space-3); }
/* Many claim choices at once: smaller buttons, and each chow shows its label above its three tiles. */
.actions-crowded { column-gap: var(--space-2); }
.actions-crowded .btn { min-width: 84px; min-height: 68px; padding: var(--space-1) var(--space-3); font-size: 28px; }
.actions-crowded .btn-chow { flex-direction: column; gap: var(--space-1); font-size: 22px; }
.actions-crowded .claim-tile { flex: 0 1 auto; min-width: 0; font-size: 20px; }
.actions-crowded .claim-tile .en { white-space: normal; }
.notice { font-size: 26px; color: var(--text); text-align: right; }
.notice .bi { align-items: flex-end; }
.claim-tile { display: flex; align-items: center; gap: var(--space-2); }

/* Always one hand-tile tall (plus the lift room above), even while her tiles are still being dealt. */
.hand-row { display: flex; justify-content: center; align-items: flex-end; gap: var(--space-3); padding-top: calc(var(--hand-w) * 0.2); min-height: calc(var(--hand-w) * 1.56); }
.hand { display: flex; align-items: flex-end; gap: 2px; }
.hand-active .tile { cursor: pointer; }
.hand-active .tile:active { transform: translateY(-8%); }
.tile-drawn { margin-left: calc(var(--hand-w) * 0.3); }
.tile-selected { transform: translateY(-20%); box-shadow: 0 0 0 4px var(--gold), 0 12px 16px rgba(0, 0, 0, 0.45); }

/* ---------- tiles (everything inside is sized in em from the tile width) ---------- */

.tile {
  position: relative; flex: none;
  width: 1em; height: 1.36em;
  border-radius: 0.13em;
  background: linear-gradient(180deg, #fffdf5, var(--ivory));
  border: 1px solid var(--ivory-edge);
  /* The first shadow is the physical side of the tile, the second its cast shadow. */
  box-shadow: 0 0.06em 0 #b9b092, 0 0.12em 0.14em rgba(0, 0, 0, 0.35);
  color: var(--ink);
  transition: transform 0.12s var(--ease-out);
}
.tile-hand { font-size: var(--hand-w); }
.tile-meld { font-size: var(--meld-w); }
.tile-small { font-size: var(--small-w); }
.pile .tile-small { font-size: calc(var(--small-w) * var(--zone-scale, 1)); }
.tile-side { font-size: calc(var(--side-w) * var(--zone-scale, 1)); }
.tile-tiny { font-size: var(--tiny-w); }
.tile-back { background: linear-gradient(180deg, #2aa06c, var(--tile-back)); border-color: #14633f; }
.tile-latest { box-shadow: 0 0 0 0.1em var(--gold), 0 0.12em 0.5em rgba(0, 0, 0, 0.5); z-index: 1; }

.face {
  position: absolute; inset: 0.06em 0.05em;
  display: flex; flex-direction: column; align-items: center; justify-content: center;
  font-family: "Kaiti TC", "STKaiti", "Songti TC", "PingFang HK", serif;
  font-weight: 700; line-height: 1;
}
.pips { width: 84%; height: 88%; }
.glyph.num { font-size: 0.52em; color: #1a2b55; }
.glyph.wan { font-size: 0.52em; color: var(--red); margin-top: 0.03em; }
.glyph.big { font-size: 0.78em; }
.glyph.red { color: var(--red); }
.glyph.green { color: #1b7a45; }
.glyph.blue { color: #1d4e9e; }
.white-frame { width: 0.62em; height: 0.92em; border: 0.07em solid #1d4e9e; border-radius: 0.08em; outline: 0.03em solid #1d4e9e; outline-offset: -0.14em; }

.hint {
  position: absolute; z-index: 1; top: 0.03em; left: 0.06em;
  font: 700 0.24em/1 -apple-system, "Helvetica Neue", Arial, sans-serif;
  color: #5c5646; background: rgba(251, 247, 234, 0.85); border-radius: 0.3em; padding: 0 0.15em;
}
.hints-off .hint { display: none; }

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

.btn {
  appearance: none; border: 0; cursor: pointer;
  min-width: 124px; min-height: 80px; padding: var(--space-2) var(--space-6);
  border-radius: var(--radius-control);
  background: var(--ivory); color: #1e2b24;
  font: inherit; font-size: 36px;
  box-shadow: 0 5px 12px rgba(0, 0, 0, 0.4);
  transition: transform 0.1s var(--ease-out);
}
.btn:active { transform: scale(0.95); }
.btn-small { min-width: 0; min-height: 52px; padding: var(--space-1) var(--space-4); font-size: 22px; background: rgba(255, 255, 255, 0.16); color: var(--text); box-shadow: none; }
.btn-primary { background: var(--gold); color: var(--gold-ink); }
.btn-win { background: var(--red); color: #fff; }
.btn-call { background: #ffe9a8; color: var(--gold-ink); }
.btn-pass { background: rgba(255, 255, 255, 0.22); color: var(--text); }
.btn-chow { display: inline-flex; align-items: center; gap: var(--space-3); }
.btn-large { min-width: 240px; min-height: 92px; font-size: 40px; }
.btn-on { background: var(--gold); color: var(--gold-ink); }

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

.overlay {
  position: fixed; inset: 0; z-index: 20;
  display: grid; place-items: center; padding: var(--space-4);
  background: rgba(4, 22, 14, 0.78);
  animation: fade-in 0.2s var(--ease-out);
}
.panel {
  width: min(1000px, 100%); max-height: 100%; overflow: auto;
  display: flex; flex-direction: column; align-items: center; gap: var(--space-4);
  padding: var(--space-6) 28px; border-radius: 26px;
  background: #123f2b; border: 2px solid rgba(255, 255, 255, 0.16);
  box-shadow: 0 20px 60px rgba(0, 0, 0, 0.5);
  text-align: center;
  animation: panel-in 0.35s var(--ease-bounce);
}
h2 { margin: 0; font-size: 52px; color: var(--gold); }
.subtitle { margin: 0; font-size: 28px; color: var(--text-dim); }
.credit { margin: 0; font-size: 22px; color: var(--text-dim); }
.credit .en { white-space: normal; }
.panel-buttons { display: flex; flex-wrap: wrap; gap: var(--space-4); justify-content: center; }

.result-tiles { display: flex; flex-wrap: wrap; gap: var(--space-3); justify-content: center; align-items: center; }
.result-columns { display: grid; grid-template-columns: 1fr 1fr; gap: var(--space-6); width: 100%; }
.list { list-style: none; margin: 0; padding: 0; font-size: 28px; text-align: left; }
.list li { display: flex; align-items: center; gap: var(--space-3); padding: 6px var(--space-2); border-bottom: 1px solid rgba(255, 255, 255, 0.12); }
.list li > .bi { align-items: flex-start; margin-right: auto; }
.list-head span .bi { align-items: flex-end; }
.list .faan, .list .delta { min-width: 92px; text-align: right; font-variant-numeric: tabular-nums; }
.list .total, .list .winner { color: var(--gold); }
.list .total { border-bottom: 0; font-size: 1.15em; }
.list-head { color: var(--text-dim); font-size: 0.75em; }
.list-head > span { min-width: 92px; text-align: right; }
.delta.plus { color: #8fe3b0; }
.delta.minus { color: #ffada3; }
.final-list { width: min(560px, 100%); font-size: 34px; }
.final-medal { min-width: 1.4em; text-align: center; font-size: 1.1em; }

.setting { display: flex; align-items: center; justify-content: space-between; gap: var(--space-4); width: 100%; font-size: 28px; }
.setting > .bi { align-items: flex-start; text-align: left; }
.segmented { display: flex; gap: var(--space-2); }
.segmented .btn { min-width: 104px; min-height: 64px; font-size: 26px; background: rgba(255, 255, 255, 0.16); color: var(--text); box-shadow: none; }
.segmented .btn.btn-on { background: var(--gold); color: var(--gold-ink); }

.fatal { padding: 40px; font-size: 28px; text-align: center; }

@media (max-width: 760px) {
  .result-columns { grid-template-columns: 1fr; }
  .setting { flex-direction: column; align-items: flex-start; }
  .topbar-info { gap: var(--space-3); font-size: 17px; }
  .chip { font-size: 20px; }
  .btn { min-width: 96px; min-height: 68px; font-size: 28px; }
  .notice { font-size: 20px; }
}

/* ---------- motion and celebration ---------- */

.confetti { position: fixed; inset: 0; z-index: 40; pointer-events: none; }

/* --motion (set from the speed setting) stretches every in-game animation: 1.2 slow, 1 normal, 0.65 fast. */

/* Tiles turn over: the green back shows until the tile is edge-on, then the face.
   Both animations hit their 50% keyframe together, so the swap happens exactly at 90°. */
.tile-drop::after, .tile-draw-in::after {
  content: ''; position: absolute; inset: -1px; z-index: 3; border-radius: inherit;
  background: linear-gradient(180deg, #2aa06c, var(--tile-back));
  opacity: 0; pointer-events: none;
  animation: flip-cover var(--flip-time) linear var(--anim-delay, 0s);
}
@keyframes flip-cover { 0%, 49.9% { opacity: 1; } 50%, 100% { opacity: 0; } }

/* A discarded tile slides in from the side of the player who threw it, flipping face up as it lands. */
.tile-drop { --flip-time: calc(0.46s * var(--motion, 1)); animation: drop var(--flip-time) var(--ease-out) var(--anim-delay, 0s); }
.pile-bottom .tile-drop { --from-x: 0; --from-y: 160%; }
.pile-top .tile-drop { --from-x: 0; --from-y: -160%; }
.pile-left .tile-drop { --from-x: -160%; --from-y: 0; }
.pile-right .tile-drop { --from-x: 160%; --from-y: 0; }
@keyframes drop {
  0% { transform: perspective(5em) translate(var(--from-x, 0), var(--from-y, 0)) scale(1.35) rotateY(180deg); opacity: 0; }
  25% { opacity: 1; }
  50% { transform: perspective(5em) translate(calc(var(--from-x, 0) * 0.3), calc(var(--from-y, 0) * 0.3)) scale(1.2) rotateY(90deg); }
  80% { transform: perspective(5em) scale(0.96) rotateY(0deg); }
  100% { transform: perspective(5em) scale(1) rotateY(0deg); }
}

/* Her new tile comes off the wall face down and turns over into her hand. */
.tile-draw-in { --flip-time: calc(0.5s * var(--motion, 1)); animation: draw-in var(--flip-time) var(--ease-out) var(--anim-delay, 0s); }
@keyframes draw-in {
  0% { transform: perspective(6em) translateY(-60%) rotateY(180deg); opacity: 0; }
  20% { opacity: 1; }
  50% { transform: perspective(6em) translateY(-30%) rotateY(90deg); }
  100% { transform: perspective(6em) translateY(0) rotateY(0deg); }
}

/* Picking a tile lifts it; changing your mind lowers it back. */
.tile-lift { animation: tile-lift calc(0.18s * var(--motion, 1)) var(--ease-out); }
@keyframes tile-lift { from { transform: translateY(0); } }
.tile-lower { animation: tile-lower calc(0.18s * var(--motion, 1)) var(--ease-out); }
@keyframes tile-lower { from { transform: translateY(-20%); } }

/* The turn passing: the old seat's gold fades out while the new seat's glows in. */
.zone-arrive { animation: zone-arrive calc(0.55s * var(--motion, 1)) var(--ease-out) var(--anim-delay, 0s); }
@keyframes zone-arrive {
  0% { border-color: transparent; background: var(--zone); box-shadow: 0 0 0 0 rgba(242, 193, 78, 0); }
  45% { box-shadow: 0 0 26px 4px rgba(242, 193, 78, 0.5); }
  100% { box-shadow: 0 0 0 0 rgba(242, 193, 78, 0); }
}
.zone-leave { animation: zone-leave calc(0.45s * var(--motion, 1)) ease-out var(--anim-delay, 0s); }
@keyframes zone-leave { from { border-color: var(--gold); background: rgba(4, 30, 18, 0.5); } }

.chip-arrive { animation: chip-arrive calc(0.5s * var(--motion, 1)) var(--ease-bounce) var(--anim-delay, 0s); }
@keyframes chip-arrive {
  0% { background: rgba(0, 0, 0, 0.32); color: var(--text); }
  50% { transform: scale(1.08); }
}
.chip-leave { animation: chip-leave calc(0.45s * var(--motion, 1)) ease-out var(--anim-delay, 0s); }
@keyframes chip-leave { from { background: var(--gold); color: var(--gold-ink); } }

.tile-dealt { animation: dealt 0.3s var(--ease-out) backwards; }
@keyframes dealt { from { transform: translateY(60%) scale(0.7); opacity: 0; } }

.meld-new { animation: slam 0.4s var(--ease-bounce) var(--anim-delay, 0s); }
@keyframes slam {
  from { transform: scale(1.9); opacity: 0; filter: brightness(1.6); }
  70% { transform: scale(0.95); }
}

.bubble-out { animation: bubble-out calc(0.22s * var(--motion, 1)) ease-in forwards; }
@keyframes bubble-out { to { transform: translate(-50%, -50%) scale(0.6); opacity: 0; } }

@keyframes bubble {
  from { transform: translate(-50%, -50%) scale(0.3) rotate(-12deg); opacity: 0; }
  60% { transform: translate(-50%, -50%) scale(1.15) rotate(4deg); opacity: 1; }
}

/* Buttons and prompts pop in only when they first appear, not every time a tile is tapped. */
.actions .btn.action-new { animation: btn-in 0.25s var(--ease-bounce) backwards; }
.actions .notice.action-new { animation: fade-in 0.3s var(--ease-out) backwards; }
@keyframes btn-in { from { transform: scale(0.6); opacity: 0; } }
.actions .btn-win { animation: win-pulse 0.9s ease-in-out infinite; }
.actions .btn-win.action-new { animation: btn-in 0.25s var(--ease-bounce) backwards, win-pulse 0.9s ease-in-out 0.25s infinite; }
@keyframes win-pulse { 50% { transform: scale(1.1); } }

.ready-badge {
  padding: var(--space-1) var(--space-4); border-radius: 999px;
  background: var(--gold); color: var(--gold-ink); font-size: 28px;
  animation: btn-in 0.3s var(--ease-bounce), ready-pulse 1.6s ease-in-out 0.3s infinite;
}
.ready-badge .en { opacity: 1; }
@keyframes ready-pulse { 50% { background: #ffe9a8; transform: scale(1.05); } }

@keyframes fade-in { from { opacity: 0; } }
@keyframes panel-in { from { transform: translateY(30px) scale(0.92); opacity: 0; } }

/* Her win: the panel lands with a bump and its gold halo breathes; the title slams in, then glows. */
.overlay-win .panel {
  border-color: var(--gold);
  box-shadow: 0 20px 60px rgba(0, 0, 0, 0.5), 0 0 90px rgba(242, 193, 78, 0.35);
  animation: win-panel 0.7s var(--ease-bounce), win-halo 1.8s ease-in-out 0.7s 3;
}
@keyframes win-panel {
  0% { transform: scale(0.6); opacity: 0; }
  55% { transform: scale(1.04); opacity: 1; }
  75% { transform: scale(0.99) rotate(-0.4deg); }
  90% { transform: rotate(0.3deg); }
}
@keyframes win-halo { 50% { box-shadow: 0 20px 60px rgba(0, 0, 0, 0.5), 0 0 140px 20px rgba(242, 193, 78, 0.6); } }
.win-title { font-size: 76px; animation: win-title 0.9s var(--ease-bounce), win-glow 1.4s ease-in-out 0.9s 4; }
@keyframes win-title {
  0% { transform: scale(0.2) rotate(-10deg); opacity: 0; }
  60% { transform: scale(1.25) rotate(3deg); opacity: 1; text-shadow: 0 0 40px rgba(255, 236, 170, 0.9); }
}
@keyframes win-glow { 50% { transform: scale(1.06); text-shadow: 0 0 28px rgba(255, 220, 120, 0.85); } }

/* Someone else won: the panel drops in with a heavy thud, and the title shakes its head. */
.overlay-lose .panel { animation: lose-panel 0.6s var(--ease-out); }
@keyframes lose-panel {
  0% { transform: translateY(-40%); opacity: 0; }
  55% { transform: translateY(3%); opacity: 1; }
  75% { transform: translateY(-1%); }
}
.lose-title { animation: lose-shake 0.7s ease-in-out 0.55s; }
@keyframes lose-shake {
  20%, 60% { transform: translateX(-10px) rotate(-1.5deg); }
  40%, 80% { transform: translateX(10px) rotate(1.5deg); }
}
.streak { padding: var(--space-1) 20px; border-radius: 999px; background: var(--red); color: #fff; font-size: 30px; animation: btn-in 0.4s var(--ease-bounce) 0.5s backwards; }
.streak .en { opacity: 1; }
.row-in { animation: row-in 0.3s var(--ease-out) backwards; }
@keyframes row-in { from { transform: translateX(-24px); opacity: 0; } }


/* The start of each hand, played on the table: loose face-down tiles fly over the redrawn table. */
.fly-layer { position: fixed; inset: 0; z-index: 15; pointer-events: none; }
.fly-tile {
  position: fixed; border-radius: 4px;
  background: linear-gradient(180deg, #2aa06c, var(--tile-back));
  box-shadow: 0 3px 0 #0d4f31, 0 5px 8px rgba(0, 0, 0, 0.35);
  will-change: transform;
}
/* The dealer's dice, thrown into the middle of the table. */
.table-dice { position: fixed; transform: translate(-50%, -50%); display: flex; gap: 18px; }
.table-dice .die { width: 64px; height: 64px; padding: 8px; }
/* The wall the dice picked to break glows briefly while the tiles are dealt from it. */
.wall-chosen .stack-2, .wall-chosen .stack-1 { box-shadow: 0 3px 0 #0d4f31, 0 0 10px 2px rgba(242, 193, 78, 0.55); }

/* New game: roll three dice, then count around the table to find the first dealer. */
.dealer-table {
  display: grid; width: min(620px, 100%); gap: var(--space-3);
  grid-template-columns: auto 1fr auto;
  grid-template-areas: ". top ." "left centre right" ". bottom .";
  align-items: center; justify-items: center;
}
.seat-top { grid-area: top; }
.seat-left { grid-area: left; }
.seat-right { grid-area: right; }
.seat-bottom { grid-area: bottom; }
.dealer-centre { grid-area: centre; display: flex; flex-direction: column; align-items: center; gap: var(--space-3); padding: var(--space-4); border-radius: 18px; background: rgba(4, 30, 18, 0.45); width: 100%; }
.seat-badge {
  position: relative; display: flex; align-items: center; gap: var(--space-3);
  min-width: 132px; min-height: 76px; justify-content: center;
  padding: var(--space-2) var(--space-4); border-radius: 14px;
  background: rgba(0, 0, 0, 0.32); font-size: 28px; white-space: nowrap;
  border: 3px solid transparent;
  transition: background 0.15s ease-out, transform 0.15s ease-out;
}
.seat-count { min-width: 1.2em; font-size: 34px; font-weight: 700; color: var(--gold); font-variant-numeric: tabular-nums; }
.seat-count:empty { display: none; }
.seat-on { background: var(--gold); color: var(--gold-ink); transform: scale(1.08); }
.seat-on .seat-count { color: var(--gold-ink); }
.seat-dealer { border-color: #fff; box-shadow: 0 0 30px rgba(242, 193, 78, 0.6); animation: btn-in 0.35s var(--ease-bounce); }
.seat-crown { position: absolute; top: -26px; right: -22px; font-size: 26px; }
.dice-tray { display: flex; gap: 22px; justify-content: center; min-height: 92px; align-items: center; }
.die {
  width: 76px; height: 76px; padding: 9px;
  display: grid; grid-template-columns: repeat(3, 1fr); grid-template-rows: repeat(3, 1fr); place-items: center;
  border-radius: 16px; background: linear-gradient(160deg, #ffffff, #ece6d2);
  box-shadow: inset 0 -4px 0 #cfc6aa, 0 6px 12px rgba(0, 0, 0, 0.4);
}
.pip { width: 14px; height: 14px; border-radius: 50%; }
.pip-on { background: #1c1c1c; }
.die-red .pip-on { background: var(--red); }
.die[data-value="1"] .pip-on { width: 24px; height: 24px; }
.dice-total { min-height: 54px; font-size: 36px; color: var(--gold); }
.dice-total .bi { animation: btn-in 0.3s var(--ease-bounce); }
.dealer-result { font-size: 40px; color: var(--gold); }
/* Keep the dialog the same height while the Roll button is replaced by the result. */
.overlay-dice .panel-buttons { min-height: 92px; align-items: center; }
.dealer-result .bi { animation: btn-in 0.35s var(--ease-bounce); }

/* ---------- characters ---------- */

.avatar {
  position: relative; overflow: hidden; flex: none; display: inline-grid; place-items: center;
  width: 1.7em; height: 1.7em; border-radius: 50%;
  background: var(--avatar-bg); color: var(--avatar-ink);
  box-shadow: inset 0 -3px 0 rgba(0, 0, 0, 0.18), 0 2px 5px rgba(0, 0, 0, 0.35);
  border: 2px solid rgba(255, 255, 255, 0.7);
  font-family: "Kaiti TC", "STKaiti", "Songti TC", "PingFang HK", serif; font-weight: 700; line-height: 1;
}
.avatar-clip {
  position: absolute; inset: 0; border-radius: 50%; overflow: hidden;
  clip-path: circle(50% at 50% 50%);
  -webkit-mask-image: -webkit-radial-gradient(white, black);
}
.avatar-portrait {
  position: absolute; inset: 0; width: 100%; height: 100%; object-fit: cover; border-radius: 50%;
}
.chip .avatar-portrait {
  animation: avatar-idle 7.6s ease-in-out var(--avatar-idle-delay, 0s) infinite;
}
@keyframes avatar-idle {
  0%, 30%, 68%, 100% { transform: scale(1.04); }
  15% { transform: translateY(-1px) rotate(-3deg) scale(1.09); }
  50% { transform: translateY(0.5px) rotate(2deg) scale(1.06); }
}
.avatar-nod {
  animation: avatar-nod calc(0.55s * var(--motion, 1)) ease-in-out var(--anim-delay, 0s);
}
@keyframes avatar-nod {
  30% { transform: translateY(-2px) rotate(-5deg); }
  63% { transform: translateY(1px) rotate(3deg); }
}
.avatar-chip { font-size: 26px; }
.avatar-mid { font-size: 46px; }
.avatar-large { font-size: 44px; }
.avatar-spin { animation: avatar-spin calc(0.55s * var(--motion, 1)) var(--ease-bounce) var(--anim-delay, 0s) both; }
@keyframes avatar-spin {
  0% { transform: rotateY(90deg) scale(0.6); }
  100% { transform: none; }
}

.overlay-pick .panel { width: min(1100px, 100%); gap: var(--space-2); padding: var(--space-4) 24px; }
.overlay-pick h2 { font-size: 40px; }
.overlay-pick .subtitle { font-size: 24px; }
.overlay-pick .btn-large { min-height: 76px; font-size: 34px; }
.pick-grid { display: grid; grid-template-columns: repeat(4, 1fr); gap: var(--space-3); width: 100%; }
.pick-card {
  display: flex; flex-direction: column; align-items: center; gap: var(--space-2);
  border-radius: 18px; cursor: pointer;
  background: rgba(0, 0, 0, 0.28); color: var(--text); font: inherit;
  border: 3px solid transparent; touch-action: manipulation;
  transition: background 0.12s ease-out, border-color 0.12s ease-out, opacity 0.12s ease-out;
}
.pick-card { position: relative; font-size: 24px; gap: 4px; padding: var(--space-2) var(--space-2) var(--space-3); }
.pick-card:active { transform: scale(0.97); }
.pick-card-on { border-color: var(--gold); background: rgba(242, 193, 78, 0.18); box-shadow: 0 0 22px rgba(242, 193, 78, 0.35); }
.pick-card:not(.pick-card-on) { opacity: 0.82; }
.pick-card-on .pick-name .zh { color: var(--gold); }
.pick-warn { color: #ffada3; animation: btn-in 0.3s var(--ease-bounce); }
.pick-tag {
  position: absolute; top: 8px; right: 8px; padding: 2px 8px; border-radius: 10px;
  background: var(--gold); color: var(--gold-ink); font-size: 18px;
}
.pick-style { font-size: 18px; color: var(--text-dim); }
.pick-style .en { white-space: normal; }
.pick-level { display: inline-flex; align-items: center; gap: var(--space-2); font-size: 19px; }
.pick-level .bi { flex-direction: row; gap: 6px; align-items: baseline; }
.stars { color: var(--gold); letter-spacing: 1px; }
.level-easy .bi { color: #8fe3b0; }
.level-medium .bi { color: #ffe9a8; }
.level-hard .bi { color: #ffada3; }

@media (max-width: 760px) {
  .pick-grid { grid-template-columns: repeat(2, 1fr); }
  .avatar-large { font-size: 40px; }
}

/* ---------- opening screen ---------- */

.start-screen {
  height: 100%;
  display: flex;
  overflow: auto;
  padding:
    max(24px, env(safe-area-inset-top))
    max(clamp(24px, 6vw, 96px), env(safe-area-inset-right))
    max(24px, env(safe-area-inset-bottom))
    max(clamp(24px, 6vw, 96px), env(safe-area-inset-left));
  background:
    radial-gradient(ellipse at 73% 42%, rgba(54, 133, 91, 0.4), transparent 58%),
    repeating-linear-gradient(45deg, rgba(255, 255, 255, 0.017) 0 1px, transparent 1px 4px),
    linear-gradient(130deg, #123d2c, #061c14 80%);
  box-shadow: inset 0 0 0 1px #334c3c, inset 0 0 100px rgba(1, 14, 9, 0.6);
}

.start-layout {
  width: min(1200px, 100%);
  margin: auto;
  display: grid;
  grid-template: "heading art" auto "actions art" auto / minmax(0, 0.95fr) minmax(0, 1.05fr);
  align-items: center;
  gap: clamp(20px, 5vh, 52px) clamp(16px, 4vw, 62px);
}

.start-heading { grid-area: heading; align-self: end; }
.start-heading .bi { align-items: flex-start; }
.start-heading h1 {
  margin: 0;
  font-size: clamp(76px, min(11vw, 19vh), 172px);
  font-weight: 600;
  color: var(--ivory);
  text-shadow: 0 5px 32px rgba(0, 0, 0, 0.32);
}
.start-heading h1 .bi { display: flex; }
.start-heading h1 .en {
  margin-top: 0.38em;
  font-size: clamp(19px, 2.6vw, 32px);
  font-family: Georgia, serif;
  letter-spacing: 0.24em;
  text-transform: uppercase;
  color: #dad3ba;
  opacity: 1;
}
.start-description {
  margin: clamp(18px, 4vh, 36px) 0 0;
  font-size: clamp(22px, 2.35vw, 29px);
  color: #d1e4d8;
}
.start-description::before {
  content: '';
  display: block;
  width: 48px;
  height: 1px;
  margin-bottom: clamp(14px, 3vh, 26px);
  background: #9a9676;
}
.start-description .bi { gap: 6px; }
.start-description .en { font-size: clamp(12px, 1.45vw, 16px); white-space: normal; }

.start-actions {
  grid-area: actions;
  align-self: start;
  display: grid;
  gap: 14px;
  width: min(100%, 390px);
}
.start-actions .btn {
  min-width: 0;
  min-height: 60px;
  padding: 8px 12px;
  font-size: 24px;
  border: 1px solid rgba(205, 230, 213, 0.35);
  background: rgba(16, 58, 41, 0.8);
  color: var(--ivory);
}
.start-actions .start-play {
  min-height: 84px;
  font-size: 36px;
  background: linear-gradient(180deg, #ffdf8a, var(--gold));
  color: var(--gold-ink);
  border-color: #ffe9a8;
  box-shadow: inset 0 -4px 0 rgba(104, 67, 4, 0.2), 0 10px 25px rgba(0, 0, 0, 0.32);
}
.start-actions .en { white-space: normal; }
.start-secondary { display: flex; gap: 10px; }
.start-secondary > .btn { flex: 1; }
.start-secondary > .btn:only-child { max-width: 50%; }

.start-art { grid-area: art; min-width: 0; pointer-events: none; }
.start-medallion {
  position: relative;
  isolation: isolate;
  width: min(100%, 74vh);
  aspect-ratio: 1;
  margin-inline: auto;
  container-type: inline-size;
  border: 1px solid #6d785c;
  border-radius: 50%;
  background:
    repeating-linear-gradient(135deg, transparent 0 3px, rgba(230, 255, 233, 0.035) 3px 4px),
    radial-gradient(circle at 46% 37%, #286647 0, #174b35 50%, #103626 75%);
  box-shadow: 0 24px 60px rgba(0, 0, 0, 0.42), inset 0 0 0 7px #193f2e, inset 0 0 45px #0a281c;
}
.start-medallion::before,
.start-medallion::after {
  content: '';
  position: absolute;
  z-index: -1;
  border-radius: inherit;
}
.start-medallion::before { inset: 4%; border: 1px solid rgba(200, 203, 166, 0.34); }
.start-medallion::after {
  inset: 8%;
  border: 1px solid rgba(200, 203, 166, 0.22);
  background: radial-gradient(circle, transparent 46%, rgba(7, 37, 24, 0.34) 100%);
}
.start-tiles {
  position: absolute;
  inset: 23% 0 auto;
  display: flex;
  align-items: center;
  justify-content: center;
}
.start-tiles .tile {
  font-size: 22cqw;
  margin-inline: -0.025em;
  background: linear-gradient(145deg, #fffdf4 5%, #f7ebcd 92%);
  border-color: #fff1d4;
  box-shadow:
    inset 0 0 0 0.025em rgba(255, 255, 255, 0.65),
    0 0.045em 0 #cfc1a0, 0 0.11em 0 #9d9e7c, 0 0.15em 0 #206347,
    0 0.24em 0.24em rgba(0, 18, 10, 0.65);
  animation: start-tile-arrive 620ms var(--ease-out) backwards;
}
.start-tiles .hint { display: none; }
.start-tiles .tile:first-child { rotate: -17deg; translate: 0 17%; }
.start-tiles .tile:nth-child(2) { z-index: 1; rotate: -3deg; animation-delay: 90ms; }
.start-tiles .tile:last-child { rotate: 16deg; translate: 0 18%; animation-delay: 150ms; }
@keyframes start-tile-arrive {
  from { opacity: 0; transform: translateY(38%) rotate(-14deg) scale(0.9); }
}

.start-dice {
  position: absolute;
  inset: 65% 0 auto;
  display: flex;
  justify-content: center;
  gap: 3cqw;
}
.start-dice .die {
  width: 9cqw;
  height: 9cqw;
  padding: 1.1cqw;
  border-radius: 1.8cqw;
  rotate: -18deg;
}
.start-dice .die:nth-child(2) { rotate: 12deg; translate: 0 24%; }
.start-dice .die:last-child { rotate: 25deg; translate: 0 -12%; }
.start-dice .pip { width: 1.25cqw; height: 1.25cqw; }
.start-dice .die[data-value="1"] .pip-on { width: 2.7cqw; height: 2.7cqw; }

@media (orientation: portrait) {
  .start-layout {
    grid-template: "heading" "art" "actions" / minmax(0, 1fr);
    justify-items: center;
    text-align: center;
    gap: clamp(16px, 3vh, 30px);
  }
  .start-heading .bi { align-items: center; }
  .start-heading h1 { font-size: clamp(72px, 13vw, 130px); }
  .start-description { margin-top: 16px; }
  .start-description::before { display: none; }
  .start-art { width: min(100%, 38vh, 420px); }
  .start-actions .start-play { min-height: 72px; }
  .start-secondary { justify-content: center; }
}

@media (orientation: portrait) and (max-height: 650px) {
  .start-screen { padding-block: max(16px, env(safe-area-inset-top)) max(16px, env(safe-area-inset-bottom)); }
  .start-layout { gap: 14px; }
  .start-heading h1 { font-size: 64px; }
  .start-description { margin-top: 12px; }
  .start-art { width: min(100%, 27vh); }
  .start-actions { gap: 10px; }
}

@media (orientation: landscape) and (max-height: 500px) {
  .start-screen { padding-block: max(16px, env(safe-area-inset-top)) max(16px, env(safe-area-inset-bottom)); }
  .start-layout { row-gap: 14px; }
  .start-heading h1 { font-size: clamp(52px, 15vh, 76px); }
  .start-description { margin-top: 10px; }
  .start-description::before { display: none; }
  .start-actions { gap: 8px; }
  .start-actions .btn { min-height: 50px; }
  .start-actions .start-play { min-height: 60px; font-size: 28px; }
}

/* Respect the iPad "Reduce Motion" accessibility setting. */
@media (prefers-reduced-motion: reduce) {
  *, *::before, *::after { animation: none !important; transition: none !important; }
}
