:root {
  color-scheme: light dark;
  --bg: #e8efe9;
  --panel: #ffffff;
  --text: #14201a;
  --muted: #5a6b62;
  --line: #c9d5ce;
  --accent: #0d6b4c;
  --on-accent: #ffffff;
  --felt: #0f6b4a;
  --felt-hi: #178a5e;
  --felt-edge: #0a3f2d;
  --wood-hi: #d2a86a;
  --wood-mid: #9a6430;
  --wood-lo: #5a3418;
  --brass: #c9a45a;
  --danger: #b91c1c;
  --you: #0ea5e9;
  --hand-gap: 0.16rem;
  --hand-row-gap: 0.7rem;
  /* Comfortable face width — do not keep growing with the viewport */
  --tile-face-max: 3rem;
  /*
   * Room for drawn slot + hand-row gap when shrinking-to-fit on wide felt.
   * (Must not under-count or the 16th hand tile clips under 新牌.)
   */
  --drawn-reserve: calc(var(--tile-face-max) + 1.6rem);
  /*
   * Whole game (main) width cap ≈ 16 hand + 1 drawn at max face
   * + main pad / wood rail / felt inset. Wider viewports center — no full-bleed.
   */
  --layout-max: calc(
    16 * var(--tile-face-max) + 15 * var(--hand-gap) + var(--hand-row-gap) +
      var(--drawn-reserve) + 5.5rem
  );
  /*
   * General table tile size. The player's hand has its own responsive grid:
   * 8 × 2 + drawn on phones, 16 + drawn in one row on wider screens.
   */
  --tile-face-w: clamp(2.45rem, 11.5vw, var(--tile-face-max));
  --tile-h: calc(var(--tile-face-w) * 4 / 3);
  --mini-h: calc(var(--tile-h) * 0.58);
  --mini-w: calc(var(--mini-h) * 0.75);
  /*
   * Selected discard candidate rises. overflow-x:auto forces y-clip, so
   * pad the scrollport by lift + outline/shadow chrome (not just the lift).
   */
  --select-lift: 0.8rem;
  --select-chrome: 0.4rem;
  --select-headroom: calc(var(--select-lift) + var(--select-chrome));
}

@media (prefers-color-scheme: dark) {
  :root {
    --bg: #120e0a;
    --panel: #1c1814;
    --text: #efe6d8;
    --muted: #a89880;
    --line: #3a3026;
    --accent: #34d399;
    --on-accent: #042f1e;
    --felt: #0c5540;
    --felt-hi: #127055;
    --felt-edge: #073528;
    --wood-hi: #b8894a;
    --wood-mid: #7a4e24;
    --wood-lo: #3d2410;
    --brass: #b89548;
  }
}

* {
  box-sizing: border-box;
}

body {
  margin: 0;
  min-height: 100dvh;
  font-family:
    "Avenir Next",
    "Segoe UI",
    "PingFang TC",
    "Noto Sans TC",
    sans-serif;
  /* Warm parlor around the table */
  background:
    radial-gradient(
      900px 480px at 50% 12%,
      color-mix(in oklab, var(--wood-mid) 22%, transparent),
      transparent 60%
    ),
    radial-gradient(
      120% 80% at 50% 100%,
      color-mix(in oklab, #000 16%, transparent),
      transparent 55%
    ),
    linear-gradient(
      180deg,
      color-mix(in oklab, var(--bg) 70%, #2a1c10),
      var(--bg)
    );
  color: var(--text);
}

main {
  /* Cap at ~17-tile comfort width; center when the viewport is wider */
  position: relative;
  /* Height of the floating top strip; the felt reserves it as headroom */
  --chrome-h: 44px;
  width: min(100%, var(--layout-max));
  max-width: var(--layout-max);
  margin: 0 auto;
  padding: 0.65rem 0.65rem max(0.75rem, env(safe-area-inset-bottom));
  display: flex;
  flex-direction: column;
  gap: 0.5rem;
  /*
   * One screen, no page scroll: the rivers scroll inside the felt so the
   * player's hand and the action bar are always where the thumb expects them.
   */
  height: 100dvh;
  overflow: hidden;
  box-sizing: border-box;
}

.hero h1 {
  margin: 0;
  font-family:
    "Iowan Old Style",
    Palatino,
    "Songti TC",
    serif;
  font-size: clamp(1.55rem, 5vw, 2rem);
  letter-spacing: 0.14em;
  text-align: center;
}

.tagline {
  margin: 0.2rem 0 0;
  text-align: center;
  color: var(--muted);
  font-size: 0.88rem;
}

.meter {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  gap: 0.5rem 0.85rem;
  padding: 0.55rem 0.75rem;
  background:
    linear-gradient(
      180deg,
      color-mix(in oklab, var(--panel) 94%, var(--wood-hi)),
      color-mix(in oklab, var(--panel) 90%, transparent)
    );
  border: 1px solid color-mix(in oklab, var(--line) 70%, var(--wood-mid));
  border-radius: 0.85rem;
  box-shadow: inset 0 1px 0 color-mix(in oklab, #fff 35%, transparent);
}

.meter-item {
  display: flex;
  flex-direction: column;
  gap: 0.1rem;
  min-width: 3rem;
}

.meter-item .label {
  font-size: 0.7rem;
  color: var(--muted);
}

.meter-actions {
  margin-left: auto;
}

.controls {
  display: flex;
  flex-wrap: wrap;
  gap: 0.5rem;
  align-items: center;
}

.online-meta {
  margin: 0;
  flex: 1 1 100%;
  font-size: 0.9rem;
  line-height: 1.35;
}

.muted {
  color: var(--muted);
}

body[data-layout="match"] .hero,
body[data-layout="match"] .tagline,
body[data-layout="over"] .hero,
body[data-layout="over"] .tagline {
  display: none;
}

body[data-pg-surface="room"] #solo-controls {
  display: none;
}

body[data-layout="match"] #online-controls.match-bar {
  display: flex;
}

body[data-online-role="spectator"] #btn-auto,
body[data-online-role="spectator"] .seat-you .hand-row,
body[data-online-role="spectator"] .seat-you #action-bar,
body[data-online-role="spectator"] #btn-rules,
body[data-online-role="guest"] #btn-rules {
  display: none;
}

body[data-online-role="host"] #btn-rules {
  display: inline-flex;
}

.more-menu {
  position: relative;
  margin-left: auto;
}

.more-menu summary {
  display: grid;
  place-items: center;
  min-width: 4.5rem;
  min-height: 44px;
  padding: 0.55rem 1rem;
  border: 1px solid var(--line);
  border-radius: 0.7rem;
  background: var(--panel);
  color: var(--text);
  font-weight: 600;
  cursor: pointer;
  list-style: none;
}

.more-menu summary::-webkit-details-marker {
  display: none;
}

.more-menu[open] summary {
  border-color: var(--accent);
}

.more-menu-popover {
  position: absolute;
  top: calc(100% + 0.35rem);
  right: 0;
  z-index: 30;
  display: grid;
  gap: 0.35rem;
  width: min(12rem, calc(100vw - 1.3rem));
  padding: 0.45rem;
  border: 1px solid var(--line);
  border-radius: 0.8rem;
  background: var(--panel);
  box-shadow: 0 10px 30px rgba(0, 0, 0, 0.28);
}

.more-menu-popover button {
  width: 100%;
  text-align: left;
}

#btn-auto[aria-pressed="true"] {
  border-color: #fbbf24;
  background: #78350f;
  color: #fef3c7;
}

.game-active main {
  gap: 0.28rem;
  padding-top: 0.35rem;
}

/* Mid-hand the table is the product; the title bar is not */
.game-active .hero,
.game-active .tagline,
.game-active #btn-deal {
  display: none;
}

/*
 * One floating top-edge strip: meter supplies the translucent backdrop and
 * controls sit inside its right edge. Both leave normal flow, so the table
 * receives all available height and never resizes when a menu opens.
 * The felt reserves --chrome-h so the far seat is never hidden underneath.
 */
.game-active .table-felt {
  padding-top: calc(var(--chrome-h) + 0.35rem);
}

.game-active .meter {
  position: absolute;
  top: 0.45rem;
  inset-inline: 0.8rem;
  z-index: 6;
  flex-wrap: nowrap;
  gap: 0.15rem 0.45rem;
  min-height: var(--chrome-h);
  padding: 0.35rem 6.75rem 0.35rem 0.6rem;
  border-color: color-mix(in oklab, var(--brass) 30%, transparent);
  border-radius: 0.65rem;
  background: color-mix(in oklab, #111 14%, transparent);
  color: #f8fafc;
  font-size: 0.78rem;
  text-shadow: 0 1px 3px rgba(0, 0, 0, 0.85);
  box-shadow: none;
  backdrop-filter: blur(6px);
  pointer-events: none;
}

.game-active .meter-item {
  flex-direction: row;
  align-items: baseline;
  gap: 0.15rem;
  min-width: 0;
  white-space: nowrap;
}

.game-active .meter-item .label {
  display: none;
}

.game-active .meter-item:nth-child(2)::before {
  content: "· 莊";
  color: color-mix(in oklab, var(--brass) 75%, #fff);
}

.game-active .meter-item:nth-child(3)::before {
  content: "· 山";
  color: color-mix(in oklab, var(--brass) 75%, #fff);
}

.game-active .meter-rule {
  display: none;
}

.game-active .controls {
  position: absolute;
  top: 0.45rem;
  right: 0.95rem;
  z-index: 7;
  flex-wrap: nowrap;
  gap: 0.25rem;
  min-height: var(--chrome-h);
  margin: 0;
}

.game-active .controls button,
.game-active .more-menu summary {
  min-width: 44px;
  min-height: var(--chrome-h);
  padding: 0.35rem 0.6rem;
  border-color: color-mix(in oklab, var(--brass) 32%, transparent);
  background: color-mix(in oklab, #111 22%, transparent);
  color: #f8fafc;
  font-size: 0.78rem;
  text-shadow: 0 1px 3px rgba(0, 0, 0, 0.85);
  backdrop-filter: blur(6px);
}

.game-active #btn-auto {
  font-size: 0;
}

.game-active #btn-auto::before {
  content: "託管";
  font-size: 0.78rem;
}

.game-active #btn-auto[aria-pressed="true"]::before {
  content: "接手";
}

.game-active .more-menu {
  margin-left: 0;
}

.game-active .more-menu summary {
  width: 44px;
  padding: 0;
  font-size: 0;
}

.game-active .more-menu summary::before {
  content: "⋯";
  font-size: 1.15rem;
  line-height: 1;
}

/* Mid-hand the felt is the product; keep only a thin wood lip */
.game-active .table {
  --rail: clamp(0.22rem, 0.9vw, 0.38rem);
  border-radius: 0.75rem;
}

.game-active .table-studs span {
  width: 0.4rem;
  height: 0.4rem;
}

/*
 * During play the felt centre and the action bar carry the state, so the
 * narration line only stays for screen readers instead of repeating it.
 */
.game-active .status {
  position: absolute;
  width: 1px;
  height: 1px;
  margin: -1px;
  padding: 0;
  overflow: hidden;
  clip-path: inset(50%);
  white-space: nowrap;
}

.game-active .flash {
  position: absolute;
  top: calc(var(--chrome-h) + 0.85rem);
  left: 50%;
  z-index: 12;
  width: max-content;
  max-width: calc(100% - 2rem);
  margin: 0;
  padding: 0.4rem 0.65rem;
  transform: translateX(-50%);
  border: 1px solid color-mix(in oklab, #f59e0b 45%, transparent);
  border-radius: 999px;
  background: color-mix(in oklab, #111 34%, transparent);
  color: #fde68a;
  box-shadow: 0 5px 16px rgba(0, 0, 0, 0.22);
  text-align: center;
  backdrop-filter: blur(12px);
}

button {
  min-height: 44px;
  min-width: 44px;
  padding: 0.55rem 1rem;
  border-radius: 0.7rem;
  border: 1px solid transparent;
  background: var(--accent);
  color: var(--on-accent);
  font: inherit;
  font-weight: 600;
  cursor: pointer;
}

button.secondary {
  background: transparent;
  color: var(--text);
  border-color: var(--line);
}

button.accent {
  background: #b45309;
  color: #fff7ed;
}

button:disabled {
  opacity: 0.45;
  cursor: not-allowed;
}

button:focus-visible,
.more-menu summary:focus-visible {
  outline: 3px solid color-mix(in oklab, #fbbf24 85%, #fff);
  outline-offset: 2px;
}

.status {
  margin: 0;
  font-size: 0.9rem;
  color: var(--muted);
  min-height: 1.35em;
}

.flash {
  margin: 0;
  font-size: 0.88rem;
  color: #b45309;
  min-height: 1.2em;
}

.auto-badge {
  font-size: 0.72rem;
  font-weight: 700;
  letter-spacing: 0.04em;
  color: #fde68a;
  animation: auto-pulse 1.8s ease-in-out infinite;
}

.rule-badge {
  padding: 0.08rem 0.35rem;
  border-radius: 999px;
  background: #9a3412;
  color: #ffedd5;
  font-size: 0.68rem;
  font-weight: 800;
}

@keyframes auto-pulse {
  50% {
    opacity: 0.55;
  }
}

/* 聽牌提示：牌面優先，數字＝該牌還沒現身的張數 */
.wait-hint {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  justify-content: center;
  gap: 0.25rem 0.3rem;
  margin: 0.1rem 0 0;
  padding: 0.25rem 0.4rem;
  border-radius: 0.55rem;
  background: color-mix(in oklab, #000 26%, transparent);
  box-shadow: inset 0 0 0 1px color-mix(in oklab, #fde68a 30%, transparent);
}

.wait-hint[hidden] {
  display: none;
}

.wait-label {
  font-size: clamp(0.7rem, 2.6vw, 0.82rem);
  font-weight: 800;
  letter-spacing: 0.12em;
  color: color-mix(in oklab, #fde68a 88%, #fff);
}

.wait-tile {
  display: inline-flex;
  align-items: center;
  gap: 0.12rem;
  padding: 0.1rem 0.22rem 0.1rem 0.12rem;
  border-radius: 0.4rem;
  background: color-mix(in oklab, #fde68a 16%, transparent);
}

.wait-count {
  font-size: 0.68rem;
  font-weight: 800;
  line-height: 1;
  color: color-mix(in oklab, #fde68a 90%, #fff);
}

.wait-tile.dead {
  background: color-mix(in oklab, #000 30%, transparent);
}

.wait-tile.dead .tile {
  opacity: 0.42;
}

.wait-tile.dead .wait-count {
  color: color-mix(in oklab, var(--danger) 55%, #fca5a5);
}

/* Outer mahjong table: wood rail, brass lip, green felt well */
.table {
  /* Thin lip — width-keyed, but capped so it never eats the felt */
  --rail: clamp(0.3rem, 1.1vw, 0.5rem);
  position: relative;
  /* Contain felt/seat z-index so they can’t paint over sticky 打牌列 */
  z-index: 0;
  isolation: isolate;
  display: flex;
  flex-direction: column;
  padding: var(--rail);
  border-radius: 0.9rem;
  background:
    linear-gradient(
      155deg,
      color-mix(in oklab, var(--wood-hi) 92%, #fff) 0%,
      var(--wood-mid) 38%,
      var(--wood-lo) 100%
    );
  border: 1px solid color-mix(in oklab, var(--wood-lo) 70%, #000);
  box-shadow:
    inset 0 1px 0 color-mix(in oklab, #fff 42%, transparent),
    inset 0 -3px 5px rgba(0, 0, 0, 0.28),
    0 1px 0 color-mix(in oklab, var(--wood-lo) 80%, #000),
    0 16px 40px rgba(0, 0, 0, 0.32),
    0 4px 10px rgba(0, 0, 0, 0.18);
  color: #e8f5ef;
  flex: 1 1 auto;
  min-height: 0;
  width: 100%;
  box-sizing: border-box;
}

/*
 * Rivers scroll inside the felt, so their scrollbars sit on the baize: keep
 * them thin and dark instead of the platform default light slab.
 */
.table-play,
.seat,
.seat-discards {
  scrollbar-width: thin;
  scrollbar-color: color-mix(in oklab, #000 32%, transparent) transparent;
}

.table-play::-webkit-scrollbar,
.seat::-webkit-scrollbar,
.seat-discards::-webkit-scrollbar {
  width: 6px;
  height: 6px;
}

.table-play::-webkit-scrollbar-track,
.seat::-webkit-scrollbar-track,
.seat-discards::-webkit-scrollbar-track {
  background: transparent;
}

.table-play::-webkit-scrollbar-thumb,
.seat::-webkit-scrollbar-thumb,
.seat-discards::-webkit-scrollbar-thumb {
  border-radius: 999px;
  background: color-mix(in oklab, #000 32%, transparent);
}

/* The scrolling half of the felt: opponents, their melds, and the rivers */
.table-play {
  display: flex;
  flex-direction: column;
  gap: 0.45rem;
  flex: 1 1 auto;
  min-height: 0;
  overflow-y: auto;
  overscroll-behavior: contain;
  /* Keep lifted/selected chrome from being clipped by the scrollport */
  padding-inline: 0.1rem;
}

.table-felt {
  position: relative;
  z-index: 1;
  display: flex;
  flex-direction: column;
  gap: 0.45rem;
  flex: 1 1 auto;
  min-height: 0;
  padding: 0.55rem 0.5rem 0.6rem;
  border-radius: 0.75rem;
  /* Casino baize + soft grain */
  background:
    radial-gradient(
      130% 90% at 50% 35%,
      color-mix(in oklab, var(--felt-hi) 75%, #1a9a6a) 0%,
      var(--felt) 48%,
      var(--felt-edge) 100%
    ),
    repeating-linear-gradient(
      0deg,
      transparent,
      transparent 2px,
      color-mix(in oklab, #000 4%, transparent) 2px,
      color-mix(in oklab, #000 4%, transparent) 3px
    );
  box-shadow:
    inset 0 0 0 2px color-mix(in oklab, var(--brass) 55%, var(--wood-lo)),
    inset 0 0 0 4px color-mix(in oklab, var(--wood-lo) 85%, #000),
    inset 0 3px 10px rgba(0, 0, 0, 0.35),
    inset 0 -2px 6px color-mix(in oklab, var(--felt-hi) 35%, transparent);
}

.table-studs {
  position: absolute;
  inset: 0.35rem;
  pointer-events: none;
  z-index: 0;
}

.table-studs span {
  position: absolute;
  width: 0.55rem;
  height: 0.55rem;
  border-radius: 50%;
  background:
    radial-gradient(
      circle at 35% 30%,
      color-mix(in oklab, var(--brass) 80%, #fff),
      var(--brass) 45%,
      color-mix(in oklab, var(--brass) 50%, #5a3a10)
    );
  box-shadow:
    0 0 0 1px color-mix(in oklab, var(--wood-lo) 60%, #000),
    0 1px 2px rgba(0, 0, 0, 0.35);
}

.table-studs span:nth-child(1) {
  top: 0;
  left: 0;
}
.table-studs span:nth-child(2) {
  top: 0;
  right: 0;
}
.table-studs span:nth-child(3) {
  bottom: 0;
  left: 0;
}
.table-studs span:nth-child(4) {
  bottom: 0;
  right: 0;
}

.seat {
  position: relative;
  z-index: 1;
  min-width: 0;
  border-radius: 0.65rem;
  transition:
    background 0.15s ease,
    box-shadow 0.15s ease;
}

/*
 * A side lane stretches to the full felt height, so a ring around the whole
 * seat reads as a big empty frame. Cue the turn on the name pill and let the
 * wash fade out just below it.
 */
.seat.active-turn {
  background: linear-gradient(
    180deg,
    color-mix(in oklab, #fbbf24 14%, transparent) 0,
    transparent 4rem
  );
}

.seat-top {
  display: flex;
  flex-direction: column;
  align-items: center;
  text-align: center;
  gap: 0.25rem;
  margin-top: auto;
}

.seat-top .seat-melds,
.seat-top .seat-flowers,
.seat-top .seat-discards {
  justify-content: center;
  width: 100%;
}

.seat-mid {
  display: grid;
  grid-template-columns: minmax(0, 1fr) minmax(5.5rem, 0.5fr) minmax(0, 1fr);
  gap: 0.5rem;
  align-items: stretch;
  /* Auto margins keep the table centred on tall screens and stay scroll-safe */
  flex: 0 0 auto;
  margin-bottom: auto;
  min-height: calc(var(--tile-h) * 2.6);
}

.seat-left,
.seat-right {
  display: flex;
  flex-direction: column;
  gap: 0.25rem;
  min-height: 100%;
}

.seat-left {
  align-items: flex-start;
}

.seat-right {
  align-items: flex-end;
  text-align: right;
}

.seat-right .seat-melds,
.seat-right .seat-flowers,
.seat-right .seat-discards {
  justify-content: flex-end;
}

.seat-you {
  display: flex;
  flex-direction: column;
  gap: 0.3rem;
  margin-top: auto;
  overflow: visible;
  /* Hand block hugs its content; spare felt goes to the rivers above */
  flex: 0 0 auto;
}

.seat-melds:empty,
.seat-flowers:empty {
  display: none;
}

.seat-meta {
  display: inline-flex;
  flex-wrap: wrap;
  align-items: baseline;
  gap: 0.3rem 0.45rem;
  font-size: clamp(0.72rem, 1.3vw, 0.88rem);
  margin-bottom: 0.15rem;
  padding: 0.2rem 0.55rem;
  border-radius: 999px;
  background:
    linear-gradient(
      180deg,
      color-mix(in oklab, var(--wood-hi) 55%, transparent),
      color-mix(in oklab, var(--wood-lo) 70%, #000)
    );
  border: 1px solid color-mix(in oklab, var(--brass) 40%, transparent);
  box-shadow:
    inset 0 1px 0 color-mix(in oklab, #fff 25%, transparent),
    0 1px 2px rgba(0, 0, 0, 0.25);
  color: #f3e6cf;
}

.seat-name {
  font-weight: 700;
  letter-spacing: 0.04em;
}

.seat-wind {
  opacity: 0.9;
  color: color-mix(in oklab, var(--brass) 85%, #fff);
  font-weight: 700;
}

.seat-count,
.seat-score {
  opacity: 0.85;
}

.seat-you .seat-meta {
  background:
    linear-gradient(
      180deg,
      color-mix(in oklab, #3b82f6 35%, var(--wood-hi)),
      color-mix(in oklab, #1e3a5f 70%, var(--wood-lo))
    );
  border-color: color-mix(in oklab, #7dd3fc 45%, var(--brass));
}

.seat-you .seat-name {
  color: #e0f2fe;
}

/* After the seat-you pill so the amber turn cue wins on your own seat too */
.seat.active-turn .seat-meta {
  border-color: #fbbf24;
  box-shadow:
    inset 0 1px 0 color-mix(in oklab, #fff 25%, transparent),
    0 0 0 1px color-mix(in oklab, #fbbf24 70%, transparent),
    0 0 14px color-mix(in oklab, #fbbf24 50%, transparent);
}

/*
 * Central discard well — recessed wood tray on the felt.
 * Sticky so the tile in play stays on screen while the rivers scroll.
 */
.center-felt {
  position: sticky;
  top: 0;
  z-index: 2;
  align-self: start;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 0.35rem;
  padding: 0.55rem 0.45rem;
  border-radius: 0.65rem;
  background:
    linear-gradient(
      160deg,
      color-mix(in oklab, var(--wood-mid) 88%, #000),
      color-mix(in oklab, var(--wood-lo) 90%, #1a0e06)
    );
  border: 1px solid color-mix(in oklab, var(--brass) 50%, var(--wood-lo));
  box-shadow:
    inset 0 2px 6px rgba(0, 0, 0, 0.45),
    inset 0 -1px 0 color-mix(in oklab, var(--wood-hi) 35%, transparent),
    0 0 0 2px color-mix(in oklab, var(--felt-edge) 80%, #000),
    0 2px 4px rgba(0, 0, 0, 0.2);
}

.trick-hint {
  margin: 0;
  font-size: clamp(0.7rem, 1.15vw, 0.85rem);
  opacity: 0.9;
  text-align: center;
  color: color-mix(in oklab, var(--brass) 70%, #f5e6c8);
  letter-spacing: 0.06em;
}

/* Whose move it is — the felt centre answers this before anything else */
.turn-hint {
  margin: 0;
  min-height: 1.1em;
  font-size: clamp(0.72rem, 1.2vw, 0.9rem);
  font-weight: 800;
  letter-spacing: 0.08em;
  text-align: center;
  color: color-mix(in oklab, #fde68a 88%, #fff);
}

.last-tile .tile.fresh .tile-face {
  animation: tile-land 0.24s ease-out;
  box-shadow:
    0 0 0 2px color-mix(in oklab, #fde68a 80%, transparent),
    0 3px 8px rgba(0, 0, 0, 0.4);
}

@keyframes tile-land {
  from {
    transform: translateY(-28%) scale(0.9);
    opacity: 0.5;
  }
}

@media (prefers-reduced-motion: reduce) {
  .last-tile .tile.fresh .tile-face {
    animation: none;
  }

  .action-bar {
    transition: none;
  }
}

.last-tile {
  min-height: calc(var(--tile-h) * 1.05);
  display: flex;
  align-items: center;
  justify-content: center;
}

.last-tile .tile {
  width: var(--tile-face-w);
  height: var(--tile-h);
}

.last-tile .tile .tile-face {
  width: 100%;
  height: 100%;
}

.seat-melds,
.seat-flowers,
.seat-discards {
  display: flex;
  flex-wrap: wrap;
  gap: 0.18rem;
  margin-bottom: 0.15rem;
}

.seat-discards > .tile {
  opacity: 0.56;
}

.seat-discards > .tile.recent {
  opacity: 1;
}

/* Opponent rivers stay whole — reading the river is the point of a river */
.seat-discards {
  opacity: 0.95;
  flex: 1 1 auto;
  align-content: flex-start;
  width: 100%;
}

/* Your own river is the one you already know; keep it short and out of the way */
.seat-you .seat-discards {
  flex: 0 0 auto;
  max-height: calc(var(--mini-h) * 1.6);
  overflow: auto;
  opacity: 0.8;
}

/* Opponent hands read as tile backs, so the table shows how much is still held */
.seat-backs {
  display: flex;
  flex-wrap: wrap;
  gap: 0.08rem;
  width: 100%;
  margin-bottom: 0.15rem;
}

.seat-backs:empty {
  display: none;
}

.seat-top .seat-backs {
  justify-content: center;
}

.seat-right .seat-backs {
  justify-content: flex-end;
}

/* Backs are context, not content — they must never crowd out the rivers */
.seat-backs .tile.back {
  width: calc(var(--mini-w) * 0.56);
  height: calc(var(--mini-h) * 0.56);
  opacity: 0.92;
}

.seat-left .seat-backs .tile.back,
.seat-right .seat-backs .tile.back {
  width: calc(var(--mini-w) * 0.46);
  height: calc(var(--mini-h) * 0.46);
}

.seat-backs .tile.drawn-back {
  margin-inline-start: 0.3rem;
}

/*
 * Always reserve the 9th／17th column for the drawn tile. Collapsing that
 * column on discard made every other face jump when a new tile arrived.
 */
.hand-row {
  display: grid;
  grid-template-columns: repeat(8, minmax(0, 1fr)) minmax(0, 1fr);
  column-gap: var(--hand-gap);
  row-gap: var(--hand-gap);
  align-items: flex-end;
  width: 100%;
  margin: 0.15rem 0 0.15rem;
  box-sizing: border-box;
  /* Room above the row for lifted tiles + selection chrome */
  padding-top: var(--select-headroom);
}

.hand-scroll {
  grid-column: 1 / span 8;
  width: 100%;
  min-width: 0;
  overflow: visible;
  padding: var(--select-headroom) 0 0.15rem;
  margin-top: calc(-1 * var(--select-headroom));
}

.hand {
  display: grid;
  grid-template-columns: repeat(8, minmax(0, 1fr));
  align-items: flex-end;
  gap: var(--hand-gap);
  width: 100%;
  min-height: var(--tile-h);
  padding: 0;
}

.drawn-slot {
  grid-column: 9;
  align-self: end;
  display: flex;
  align-items: flex-end;
  justify-content: center;
  position: relative;
  z-index: 0;
  width: 100%;
  min-width: 0;
  min-height: calc(100% + var(--select-headroom));
  padding: var(--select-headroom) 0.1rem 0.15rem;
  margin-top: calc(-1 * var(--select-headroom));
  box-sizing: border-box;
  overflow: visible;
  border-radius: 0.55rem;
  background:
    linear-gradient(
      180deg,
      color-mix(in oklab, #1e3a5f 55%, #000),
      color-mix(in oklab, #0f172a 80%, #000)
    );
  border: 1px solid color-mix(in oklab, #7dd3fc 50%, var(--brass));
  box-shadow:
    inset 0 2px 5px rgba(0, 0, 0, 0.4),
    0 0 0 1px color-mix(in oklab, var(--felt-edge) 70%, #000);
}

.drawn-slot::before {
  content: "新";
  position: absolute;
  top: 0.08rem;
  left: 50%;
  z-index: 3;
  transform: translateX(-50%);
  color: #bae6fd;
  font-size: 0.64rem;
  font-weight: 800;
  line-height: 1;
}

/*
 * Keep the reserved column when empty. The [hidden] attribute would remove
 * the box from layout; override so existing faces stay put across a draw.
 */
.drawn-slot[hidden] {
  display: flex !important;
  visibility: hidden;
  pointer-events: none;
  background: transparent;
  border-color: transparent;
  box-shadow: none;
}

.drawn-slot[hidden]::before {
  content: none;
}

.tile {
  width: calc(var(--tile-h) * 0.75);
  height: var(--tile-h);
  border: none;
  padding: 0;
  background: transparent;
  border-radius: 0.25rem;
  flex: 0 0 auto;
  cursor: default;
  position: relative;
}

.tile .tile-face {
  width: 100%;
  height: 100%;
  border-radius: 0.2rem;
  box-shadow: 0 1px 2px rgba(0, 0, 0, 0.35);
  pointer-events: none;
}

.tile.mini {
  width: var(--mini-w);
  height: var(--mini-h);
}

.tile.back {
  width: calc(var(--mini-w) * 0.72);
  height: calc(var(--mini-h) * 0.72);
}

.hand .tile {
  cursor: pointer;
  width: 100%;
  height: auto;
  min-width: 0;
  min-height: 44px;
  aspect-ratio: 3 / 4;
  padding: 0;
}

.hand .tile,
.drawn-slot .tile {
  transition: transform 0.12s ease;
}

.hand .tile.selected,
.drawn-slot .tile.selected {
  transform: translateY(calc(-1 * var(--select-lift)));
  z-index: 2;
  /* box-shadow ring only — outline can still paint outside the padded scrollport */
  outline: none;
}

.hand .tile.selected .tile-face,
.drawn-slot .tile.selected .tile-face {
  box-shadow:
    0 0 0 2px #fbbf24,
    0 4px 8px rgba(0, 0, 0, 0.35);
}

.drawn-slot .tile {
  cursor: pointer;
  flex: 1 1 auto;
  width: 100%;
  height: auto;
  min-width: 0;
  min-height: 44px;
  aspect-ratio: 3 / 4;
  padding: 0;
  background: transparent;
  border: none;
}

.meld-group {
  display: inline-flex;
  gap: 0.08rem;
  margin-right: 0.25rem;
  padding: 0.12rem 0.15rem;
  border-radius: 0.3rem;
  background: color-mix(in oklab, #000 22%, transparent);
  box-shadow: inset 0 1px 3px rgba(0, 0, 0, 0.25);
}

/*
 * Actor bar floats immediately above the player's seat. It never participates
 * in page/table layout, so a claim window cannot resize the felt.
 */
.action-bar {
  display: grid;
  gap: 0.4rem;
  position: absolute;
  inset-inline: 0.4rem;
  bottom: calc(100% + 0.35rem);
  z-index: 10;
  width: auto;
  max-width: 28rem;
  margin-inline: auto;
  padding: 0.45rem;
  border: 1px solid color-mix(in oklab, var(--line) 70%, transparent);
  border-radius: 0.8rem;
  background: color-mix(in oklab, var(--panel) 88%, transparent);
  color: var(--text);
  backdrop-filter: blur(10px);
  box-shadow: 0 8px 24px rgba(0, 0, 0, 0.35);
  transition:
    opacity 0.12s ease-out,
    transform 0.12s ease-out;
}

/* display: grid would otherwise beat [hidden] and leave an empty plate on the felt */
.action-bar[hidden] {
  display: none;
}

/* A seat creates its own stacking context; lift that context with the actor. */
.seat-you:has(.action-bar:not([hidden])) {
  z-index: 5;
}

@starting-style {
  .action-bar:not([hidden]) {
    opacity: 0;
    transform: translateY(0.4rem);
  }
}

.action-buttons {
  display: flex;
  flex-wrap: wrap;
  gap: 0.4rem;
}

/* Claim buttons share a row; a lone 碰／吃 grows to fill it */
.action-buttons button {
  flex: 1 1 6rem;
  min-width: 0;
}

/* 胡 outranks everything on the table, so it reads that way */
#btn-hu {
  flex-basis: 100%;
  min-height: 54px;
  font-size: 1.2rem;
  letter-spacing: 0.35em;
  text-indent: 0.35em;
  background: linear-gradient(180deg, #dc2626, #991b1b);
  border-color: color-mix(in oklab, var(--brass) 70%, #fff);
  color: #fff7ed;
  box-shadow:
    inset 0 1px 0 color-mix(in oklab, #fff 35%, transparent),
    0 3px 10px color-mix(in oklab, #dc2626 40%, transparent);
}

/* 略過 stays reachable but never competes with a claim */
#btn-pass {
  flex-basis: 100%;
  min-height: 44px;
  background: transparent;
  border-color: color-mix(in oklab, var(--line) 80%, transparent);
  color: var(--muted);
  font-weight: 600;
}

.action-prompt {
  margin: 0;
  color: var(--muted);
  font-size: 0.8rem;
  text-align: center;
}

.chi-picker {
  display: flex;
  flex-wrap: wrap;
  justify-content: center;
  gap: 0.4rem;
}

.chi-picker[hidden] {
  display: none;
}

.chi-option {
  display: inline-flex;
  gap: 0.15rem;
  align-items: center;
  padding: 0.35rem 0.5rem;
  border: 1px solid var(--line);
  border-radius: 0.65rem;
  background: var(--panel);
  color: var(--text);
  min-height: 44px;
}

.panel {
  position: fixed;
  inset: 0;
  background: rgba(0, 0, 0, 0.45);
  display: grid;
  place-items: end center;
  padding: 0.75rem;
  z-index: 20;
}

.panel[hidden] {
  display: none;
}

.panel-card {
  width: min(100%, 24rem);
  background: var(--panel);
  color: var(--text);
  border-radius: 1rem 1rem 0.5rem 0.5rem;
  padding: 1rem 1rem 1.1rem;
  border: 1px solid var(--line);
  margin-bottom: env(safe-area-inset-bottom);
  max-height: min(88dvh, 40rem);
  overflow: auto;
}

.panel-card-wide {
  width: min(100%, 28rem);
}

.panel-note {
  margin: 0 0 0.75rem;
  color: var(--muted);
  font-size: 0.88rem;
}

.rules-form {
  display: grid;
  gap: 0.55rem;
  margin-bottom: 0.85rem;
}

.rules-form label {
  display: flex;
  flex-direction: column;
  gap: 0.25rem;
  font-size: 0.88rem;
}

.rules-form label.check {
  flex-direction: row;
  align-items: center;
  gap: 0.5rem;
  min-height: 44px;
}

.rules-form input[type="number"],
.rules-form select {
  min-height: 44px;
  padding: 0.45rem 0.6rem;
  border-radius: 0.55rem;
  border: 1px solid var(--line);
  background: var(--panel);
  color: var(--text);
  font: inherit;
}

.about-body {
  color: var(--muted);
  font-size: 0.92rem;
  display: grid;
  gap: 0.55rem;
  margin-bottom: 0.75rem;
}

.about-body p {
  margin: 0;
}

.panel-card h2 {
  margin: 0 0 0.4rem;
  font-size: 1.15rem;
}

.panel-card p {
  margin: 0 0 0.55rem;
  color: var(--muted);
  font-size: 0.92rem;
}

.tai-list {
  margin: 0 0 0.75rem;
  padding-left: 1.1rem;
  font-size: 0.9rem;
}

.payment-positive {
  color: #15803d;
  font-weight: 800;
}

.payment-negative {
  color: #b91c1c;
}

.result-hand {
  display: flex;
  flex-wrap: wrap;
  gap: 0.12rem;
  margin: 0 0 0.7rem;
  padding: 0.5rem;
  border-radius: 0.65rem;
  background: color-mix(in oklab, var(--felt) 80%, #000);
}

.result-hand:empty {
  display: none;
}

.panel-actions {
  display: flex;
  flex-wrap: wrap;
  gap: 0.45rem;
  justify-content: flex-end;
}

@media (min-width: 640px) {
  :root {
    /* Prefer readable faces; never above --tile-face-max / --layout-max */
    --tile-face-w: clamp(2.55rem, 5.5vw, var(--tile-face-max));
    --mini-h: calc(var(--tile-h) * 0.64);
  }

  .hero {
    display: flex;
    flex-wrap: wrap;
    align-items: baseline;
    justify-content: center;
    gap: 0.35rem 0.85rem;
  }

  .hero h1 {
    font-size: clamp(1.35rem, 2.4vw, 1.75rem);
  }

  .tagline {
    margin: 0;
  }

  .meter,
  .controls,
  .status {
    flex-shrink: 0;
  }

  .table {
    --rail: clamp(0.35rem, 0.9vw, 0.55rem);
  }

  .table-felt {
    padding: 0.55rem 0.6rem 0.65rem;
    gap: 0.55rem;
  }

  .seat-mid {
    min-height: calc(var(--tile-h) * 4);
    gap: 0.65rem;
  }

  .seat-you .seat-discards {
    max-height: calc(var(--mini-h) * 2.2);
  }

  .hand-row {
    justify-content: center;
    /* 16 hand faces + reserved drawn column — never collapse the 17th */
    grid-template-columns: repeat(16, minmax(0, 1fr)) minmax(0, 1fr);
    column-gap: var(--hand-row-gap);
  }

  .hand-scroll {
    grid-column: 1 / span 16;
  }

  .hand {
    grid-template-columns: repeat(16, minmax(0, 1fr));
  }

  .drawn-slot {
    grid-column: 17;
  }

  .panel {
    place-items: center;
  }

  .panel-card {
    border-radius: 1rem;
  }
}

/* Wide enough: shrink-to-fit 16+1 if needed; never grow past --tile-face-max */
@media (min-width: 960px) {
  :root {
    --mini-h: calc(var(--tile-h) * 0.7);
  }

  main {
    padding-inline: 1rem;
    gap: 0.45rem;
    /* Still capped by --layout-max; only shrink tiles if the felt is tight */
  }

  .table {
    --rail: 0.55rem;
    width: 100%;
  }

  .table-felt {
    padding: 0.65rem 0.75rem 0.75rem;
    gap: 0.65rem;
    container-type: inline-size;
    container-name: felt;
    /* Fit 16+1 inside felt; stop at --tile-face-max (layout already capped) */
    --tile-face-w: clamp(
      2.35rem,
      calc((100cqi - var(--drawn-reserve)) / 16.4),
      var(--tile-face-max)
    );
    --tile-h: calc(var(--tile-face-w) * 4 / 3);
    --mini-h: calc(var(--tile-h) * 0.7);
    --mini-w: calc(var(--mini-h) * 0.75);
  }

  .seat-mid {
    grid-template-columns: minmax(0, 1.15fr) minmax(7.5rem, 0.42fr) minmax(0, 1.15fr);
    gap: 0.85rem;
    min-height: calc(var(--tile-h) * 5);
  }

  .center-felt {
    padding: 0.75rem;
    gap: 0.55rem;
  }

  .last-tile .tile {
    width: calc(var(--tile-face-w) * 1.08);
    height: calc(var(--tile-h) * 1.08);
  }

  .seat-you .seat-discards {
    max-height: calc(var(--mini-h) * 2.6);
  }
}

/*
 * Landscape phones: height is the scarce axis, so tiles size off the viewport
 * height and the least useful row (your own river) steps aside.
 */
@media (max-height: 560px) {
  :root {
    --tile-face-max: 2rem;
    --tile-face-w: clamp(1.35rem, 6vh, var(--tile-face-max));
    --select-lift: 0.4rem;
  }

  /*
   * Landscape: tiles size off vh, so the rail must too — never follow vw,
   * or the wood lip looks fat around a short table.
   */
  .table,
  .game-active .table {
    --rail: clamp(0.14rem, 1vh, 0.26rem);
    border-radius: 0.55rem;
  }

  .table-studs {
    inset: 0.18rem;
  }

  .table-studs span,
  .game-active .table-studs span {
    width: 0.28rem;
    height: 0.28rem;
  }

  .table-felt {
    --tile-face-w: clamp(1.35rem, 6vh, var(--tile-face-max));
    --tile-h: calc(var(--tile-face-w) * 4 / 3);
    --mini-h: calc(var(--tile-h) * 0.6);
    --mini-w: calc(var(--mini-h) * 0.75);
    padding: 0.28rem 0.35rem 0.32rem;
    /* Slimmer brass lip when vertical room is scarce */
    box-shadow:
      inset 0 0 0 1px color-mix(in oklab, var(--brass) 55%, var(--wood-lo)),
      inset 0 0 0 2px color-mix(in oklab, var(--wood-lo) 85%, #000),
      inset 0 2px 6px rgba(0, 0, 0, 0.35),
      inset 0 -1px 4px color-mix(in oklab, var(--felt-hi) 35%, transparent);
  }

  /* The buttons themselves are the prompt when vertical room runs out */
  .action-prompt {
    display: none;
  }

  .controls button,
  .more-menu summary {
    padding-block: 0.3rem;
  }

  .seat-you .seat-discards {
    display: none;
  }

  /*
   * Landscape has no height to spare: instead of reserving a band above the
   * far seat, the strip splits into two corner docks and the far seat's
   * centred name／張數 stays readable between them.
   */
  body.game-active main {
    --chrome-h: 30px;
  }

  .game-active .table-felt {
    padding-top: 0.28rem;
  }

  .game-active .meter {
    display: flex;
    top: 0.3rem;
    right: auto;
    left: 0.55rem;
    width: max-content;
    max-width: 40%;
    padding: 0.15rem 0.5rem;
    border-color: transparent;
    background: none;
    font-size: 0.72rem;
    backdrop-filter: none;
  }

  .game-active .controls {
    top: 0.3rem;
    right: 0.65rem;
  }

  /* Never let the far seat shrink away; its own river scrolls instead */
  .game-active .seat-top {
    flex: 0 0 auto;
    min-height: calc(var(--mini-h) * 2.2);
  }

  .game-active .controls button,
  .game-active .more-menu summary {
    min-width: 38px;
    padding: 0.2rem 0.5rem;
  }

  .game-active .more-menu summary {
    width: 40px;
  }

  /* Tile backs are redundant here — 張數 in each seat label carries the same fact */
  .seat-backs {
    display: none;
  }

  .seat-left .seat-discards,
  .seat-right .seat-discards {
    max-height: calc(var(--mini-h) * 1.7);
    overflow: auto;
  }

  /* Cap the far seat so the centre tray — the tile in play — stays on screen */
  .seat-top {
    max-height: calc(var(--mini-h) * 3.4);
    overflow: auto;
  }

  .seat-mid {
    min-height: calc(var(--tile-h) * 2);
  }

  .center-felt {
    padding: 0.3rem 0.3rem;
    gap: 0.15rem;
  }

  .turn-hint,
  .trick-hint {
    font-size: 0.66rem;
  }

  .last-tile {
    min-height: calc(var(--tile-h) * 0.9);
  }

  #btn-hu {
    min-height: 46px;
    font-size: 1.05rem;
  }

  /*
   * One row, docked to the thumb side: stacked full-width claims would cover
   * the centre tray, and the tile you are claiming has to stay visible.
   */
  .action-bar {
    inset-inline: auto 0.35rem;
    bottom: calc(100% + 0.2rem);
    width: max-content;
    max-width: calc(100% - 0.7rem);
    margin-inline: 0;
    gap: 0.25rem;
    padding: 0.3rem;
  }

  .action-buttons {
    flex-wrap: nowrap;
  }

  .action-buttons button,
  #btn-hu,
  #btn-pass {
    flex: 0 1 auto;
    min-width: 3.6rem;
  }

  #btn-pass {
    min-height: 44px;
  }
}

/*
 * Landscape landing: controls form a compact left rail while the empty table
 * gets the full right pane. Once a hand starts, .game-active returns to the
 * single-screen play layout above.
 */
@media (orientation: landscape) and (max-height: 560px) {
  body:not(.game-active) main {
    display: grid;
    grid-template-columns: clamp(12rem, 29vw, 16rem) minmax(0, 1fr);
    grid-template-rows: auto auto auto minmax(0, 1fr);
    align-items: start;
    gap: 0.35rem 0.55rem;
    padding: 0.4rem max(0.45rem, env(safe-area-inset-right))
      max(0.4rem, env(safe-area-inset-bottom))
      max(0.45rem, env(safe-area-inset-left));
  }

  body:not(.game-active) .hero,
  body:not(.game-active) .meter,
  body:not(.game-active) .controls,
  body:not(.game-active) .status,
  body:not(.game-active) .flash {
    grid-column: 1;
    min-width: 0;
  }

  body:not(.game-active) .hero {
    align-self: end;
  }

  body:not(.game-active) .hero h1 {
    font-size: clamp(1.2rem, 5vh, 1.65rem);
    text-align: left;
  }

  body:not(.game-active) .tagline {
    margin-top: 0.1rem;
    font-size: 0.74rem;
    text-align: left;
  }

  body:not(.game-active) .meter {
    display: grid;
    grid-template-columns: repeat(2, minmax(0, 1fr));
    gap: 0.25rem 0.5rem;
    padding: 0.4rem 0.5rem;
  }

  body:not(.game-active) .meter-item {
    flex-direction: row;
    align-items: baseline;
    gap: 0.25rem;
    min-width: 0;
  }

  body:not(.game-active) .controls {
    display: grid;
    grid-template-columns: repeat(2, minmax(0, 1fr));
    gap: 0.35rem;
  }

  body:not(.game-active) .controls > button,
  body:not(.game-active) .more-menu,
  body:not(.game-active) .more-menu summary {
    width: 100%;
    min-width: 0;
  }

  body:not(.game-active) #btn-deal {
    grid-column: 1 / -1;
  }

  body:not(.game-active) .more-menu {
    margin-left: 0;
  }

  body:not(.game-active) .status {
    grid-row: 4;
    margin-top: 0.1rem;
    font-size: 0.78rem;
  }

  body:not(.game-active) .flash {
    grid-row: 4;
    align-self: start;
  }

  body:not(.game-active) main:has(.flash:not([hidden])) .status {
    visibility: hidden;
  }

  body:not(.game-active) .table {
    grid-column: 2;
    grid-row: 1 / -1;
    align-self: stretch;
    height: 100%;
    min-height: 0;
  }

  body:not(.game-active) .table-felt {
    min-height: 0;
  }
}
