:root {
  --felt: #1a5c3a;
  --felt-deep: #0f3d28;
  --felt-light: #2a7a4e;
  --wood: #5c3a1e;
  --cream: #f3efe4;
  --ink: #1a1814;
  --tile: #faf7f0;
  --tile-edge: #c9c0b0;
  --tile-face-bg: linear-gradient(180deg, #fffefc 0%, #f7f0e0 58%, #e8d9bc 100%);
  --accent: #c45c26;
  --danger: #b42318;
  --safe: #0f6b4c;
  --radius: 6px;
  --tile-w: 2.35rem;
  --tile-h: 3.15rem;
  /* 立牌厚度（俯視） */
  --tile-thick: calc(var(--tile-w) * 0.34);
  /* 中央牌牆／牌尾：先 2/3 */
  --tile-sm-w: calc(var(--tile-w) * 2 / 3);
  --tile-sm-h: calc(var(--tile-h) * 2 / 3);
  --tile-sm-thick: calc(var(--tile-sm-w) * 0.34);
  --font-display: "Noto Serif TC", "Source Han Serif TC", "PingFang TC", "Microsoft JhengHei", serif;
  --font-body: "Noto Sans TC", "PingFang TC", "Microsoft JhengHei", sans-serif;
}

*,
*::before,
*::after {
  box-sizing: border-box;
}

html,
body {
  margin: 0;
  height: 100%;
  overflow: hidden;
  background: linear-gradient(160deg, #2a1810 0%, #1a100c 40%, #0d1a14 100%);
  color: var(--cream);
  font-family: var(--font-body);
  -webkit-tap-highlight-color: transparent;
}

.app {
  height: 100dvh;
  max-height: 100dvh;
  overflow: hidden;
  display: grid;
  grid-template-columns: minmax(0, 1fr) clamp(17rem, 24vw, 21rem);
  gap: 0.75rem;
  max-width: 1440px;
  margin: 0 auto;
  padding: 0.55rem;
  padding-bottom: max(0.4rem, env(safe-area-inset-bottom));
}

.game-area {
  min-width: 0;
  min-height: 0;
  height: 100%;
  overflow: hidden;
}

.game-area .table {
  width: 100%;
  height: 100%;
}

.info-panel {
  min-width: 0;
  min-height: 0;
  height: 100%;
  display: flex;
  flex-direction: column;
  gap: 0.65rem;
  padding: 0.85rem;
  overflow: hidden;
  color: #e9dfc8;
  background:
    linear-gradient(180deg, rgba(12, 29, 20, 0.98), rgba(8, 23, 16, 0.98));
  border: 1px solid rgba(203, 170, 86, 0.34);
  border-radius: 12px;
  box-shadow:
    inset 0 0 24px rgba(0, 0, 0, 0.25),
    0 8px 24px rgba(0, 0, 0, 0.25);
}

.info-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 0.65rem;
  flex: 0 0 auto;
}

.brand-subtitle {
  margin-top: 0.16rem;
  color: rgba(233, 223, 200, 0.58);
  font-size: 0.72rem;
  letter-spacing: 0.08em;
}

.info-card {
  flex: 0 0 auto;
  min-width: 0;
  padding: 0.75rem 0.8rem;
  background: rgba(13, 37, 25, 0.72);
  border: 1px solid rgba(203, 170, 86, 0.28);
  border-radius: 10px;
}

.info-card h2 {
  margin: 0 0 0.48rem;
  color: #caae62;
  font-family: var(--font-display);
  font-size: 0.92rem;
  letter-spacing: 0.08em;
}

.info-card--message {
  min-height: 7.25rem;
}

.info-card--log {
  flex: 1 1 auto;
  min-height: 0;
  display: flex;
  flex-direction: column;
}

.game-stats {
  margin: 0;
}

.game-stats > div {
  min-height: 2rem;
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 0.75rem;
  border-bottom: 1px solid rgba(233, 223, 200, 0.08);
}

.game-stats > div:last-child {
  border-bottom: 0;
}

.game-stats dt {
  color: rgba(233, 223, 200, 0.68);
}

.game-stats dd {
  margin: 0;
  color: #e5cc83;
  font-weight: 600;
}

.score-list {
  display: grid;
  gap: 0.25rem;
  margin: 0;
  padding: 0;
  list-style: none;
}

.score-list li {
  display: flex;
  justify-content: space-between;
  gap: 0.75rem;
  color: rgba(233, 223, 200, 0.78);
}

.score-list strong {
  color: #e5cc83;
  font-variant-numeric: tabular-nums;
}

.game-log {
  min-height: 0;
  margin: 0;
  padding: 0;
  overflow-y: auto;
  list-style: none;
  scrollbar-width: thin;
}

.game-log li {
  padding: 0.38rem 0;
  color: rgba(233, 223, 200, 0.76);
  font-size: 0.8rem;
  line-height: 1.35;
  border-bottom: 1px solid rgba(233, 223, 200, 0.07);
}

.topbar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 0.5rem;
  flex-wrap: wrap;
  flex: 0 0 auto;
}

.brand {
  font-family: var(--font-display);
  font-size: clamp(1.1rem, 3vw, 1.45rem);
  font-weight: 700;
  letter-spacing: 0.08em;
  color: #f0e6d2;
}

.topbar-meta {
  display: flex;
  align-items: center;
  gap: 0.6rem;
  font-size: 0.82rem;
}

.toggle {
  display: flex;
  align-items: center;
  gap: 0.35rem;
  cursor: pointer;
  user-select: none;
}

.btn {
  appearance: none;
  border: 1px solid rgba(240, 230, 210, 0.35);
  background: rgba(255, 255, 255, 0.08);
  color: var(--cream);
  border-radius: var(--radius);
  padding: 0.4rem 0.85rem;
  font: inherit;
  cursor: pointer;
}

.btn:active {
  transform: scale(0.98);
}

.btn--primary {
  background: var(--accent);
  border-color: transparent;
  color: #fff;
}

.message {
  min-height: 2.7em;
  margin: 0;
  color: #f1eadb;
  font-size: 1rem;
  font-weight: 600;
  line-height: 1.4;
  text-align: left;
}

.table {
  flex: 1 1 auto;
  min-height: 0;
  display: grid;
  /* 固定各區尺寸：手牌在外緣；門前區另掛向桌心，不佔手牌空間 */
  --seat-side-w: calc(var(--tile-thick) * 2 + 1.35rem);
  --seat-top-h: calc(var(--tile-thick) + 2.2rem);
  --seat-bottom-h: calc(var(--tile-h) * 1.85 + var(--tile-sm-h) + 3.2rem);
  grid-template-columns: var(--seat-side-w) minmax(0, 1fr) var(--seat-side-w);
  grid-template-rows: var(--seat-top-h) minmax(0, 1fr) var(--seat-bottom-h);
  grid-template-areas:
    "top top top"
    "left center right"
    "bottom bottom bottom";
  gap: 0.35rem 0.55rem;
  background:
    radial-gradient(ellipse at center, var(--felt-light) 0%, var(--felt) 45%, var(--felt-deep) 100%);
  border: 5px solid var(--wood);
  border-radius: 14px;
  padding: 0.55rem 0.65rem;
  padding-bottom: max(0.55rem, env(safe-area-inset-bottom));
  box-shadow: inset 0 0 40px rgba(0, 0, 0, 0.25);
  position: relative;
  overflow: hidden;
}

.fx-layer {
  position: absolute;
  inset: 0;
  pointer-events: none;
  overflow: visible;
  z-index: 60;
}

.seat--top {
  grid-area: top;
  justify-self: stretch;
  align-self: stretch;
  width: 100%;
  height: 100%;
  max-width: 100%;
  overflow: visible;
  z-index: 2;
}

.seat--left {
  grid-area: left;
  justify-self: stretch;
  align-self: stretch;
  width: 100%;
  height: 100%;
  overflow: visible;
  z-index: 2;
}

.seat--right {
  grid-area: right;
  justify-self: stretch;
  align-self: stretch;
  width: 100%;
  height: 100%;
  overflow: visible;
  z-index: 2;
}

.seat--bottom {
  grid-area: bottom;
  justify-self: stretch;
  align-self: stretch;
  width: 100%;
  height: 100%;
  min-width: 0;
  overflow: visible;
  z-index: 2;
  justify-content: flex-start;
}

/* 玩家：門前在上、手牌在下，分區固定高度 */
.seat--bottom .seat-name {
  flex: 0 0 auto;
}

.seat--bottom .seat-front--player {
  position: relative;
  inset: auto;
  transform: none;
  flex: 0 0 auto;
  width: 100%;
  min-height: calc(var(--tile-sm-h) + 0.5rem);
  max-height: calc(var(--tile-sm-h) + 0.85rem);
  justify-content: center;
  z-index: 3;
}

.seat--bottom .player-melds {
  justify-content: center;
  flex: 0 0 auto;
  min-height: 0;
  max-height: none;
  overflow: visible;
}

.seat--bottom .player-hand-wrap {
  width: 100%;
  min-width: 0;
  flex: 0 0 auto;
  height: calc(var(--tile-h) * 1.7);
  min-height: calc(var(--tile-h) * 1.7);
  overflow: visible;
}

/* 進行中：手牌＋門前 */
.seat-play {
  display: contents;
}

/* 結局：擴大各區，專用翻牌版面 */
.table--round-end {
  --seat-side-w: min(42%, calc(var(--tile-w) * 4.2 + 2.5rem));
  --seat-top-h: calc(var(--tile-sm-h) * 2.2 + var(--tile-h) * 1.15 + 2.4rem);
  --seat-bottom-h: calc(var(--tile-sm-h) * 2.2 + var(--tile-h) * 1.35 + 2.8rem);
}

.table--round-end .seat-play {
  display: none;
}

.table--round-end .seat-reveal {
  display: flex;
  flex: 1 1 auto;
  min-width: 0;
  min-height: 0;
  width: 100%;
  gap: 0.35rem;
  padding: 0.15rem 0.25rem;
  box-sizing: border-box;
}

.seat-reveal {
  display: none;
}

.seat-reveal__melds,
.seat-reveal__hand {
  display: flex;
  flex-wrap: wrap;
  gap: 4px;
  align-items: center;
  align-content: flex-start;
  padding: 3px;
  border-radius: 6px;
  box-sizing: border-box;
}

.seat-reveal__melds {
  background: rgba(0, 0, 0, 0.14);
  border: 1px solid rgba(230, 198, 110, 0.2);
}

.seat-reveal__hand {
  background: rgba(0, 0, 0, 0.1);
}

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

.seat-reveal .meld {
  display: flex;
  gap: 3px;
  margin: 0;
  padding: 2px;
  background: rgba(0, 0, 0, 0.12);
  border-radius: 4px;
}

.seat-reveal .meld .tile {
  width: var(--tile-sm-w);
  height: var(--tile-sm-h);
  margin: 0;
}

.seat-reveal__hand .tile {
  width: var(--tile-w);
  height: var(--tile-h);
  margin: 0;
  flex: 0 0 auto;
}

/* 對家結局：門前在上、手牌在下，橫向完整展開 */
.table--round-end .seat--top {
  flex-direction: column;
  justify-content: flex-start;
}

.table--round-end .seat--top .seat-reveal {
  flex-direction: column;
  align-items: stretch;
}

.table--round-end .seat--top .seat-reveal__melds,
.table--round-end .seat--top .seat-reveal__hand {
  justify-content: center;
  width: 100%;
}

.table--round-end .seat--top .seat-reveal__hand {
  min-height: calc(var(--tile-h) + 0.35rem);
}

/* 上家結局：手牌靠左、門前靠右，直向展開 */
.table--round-end .seat--left {
  align-items: stretch;
}

.table--round-end .seat--left .seat-reveal {
  flex-direction: row;
  align-items: stretch;
  height: 100%;
}

.table--round-end .seat--left .seat-reveal__hand {
  flex-direction: column;
  flex: 0 0 auto;
  max-width: calc(var(--tile-w) * 2 + 0.5rem);
  justify-content: flex-start;
  overflow-y: auto;
}

.table--round-end .seat--left .seat-reveal__melds {
  flex: 1 1 auto;
  flex-direction: column;
  min-width: 0;
  overflow-y: auto;
}

.table--round-end .seat--left .seat-reveal__hand .tile {
  width: var(--tile-sm-w);
  height: var(--tile-sm-h);
}

/* 下家結局：門前靠左、手牌靠右 */
.table--round-end .seat--right {
  align-items: stretch;
}

.table--round-end .seat--right .seat-reveal {
  flex-direction: row;
  align-items: stretch;
  height: 100%;
}

.table--round-end .seat--right .seat-reveal__melds {
  flex: 1 1 auto;
  flex-direction: column;
  align-items: flex-end;
  min-width: 0;
  overflow-y: auto;
}

.table--round-end .seat--right .seat-reveal__hand {
  flex-direction: column;
  flex: 0 0 auto;
  max-width: calc(var(--tile-w) * 2 + 0.5rem);
  align-items: flex-end;
  justify-content: flex-start;
  overflow-y: auto;
}

.table--round-end .seat--right .seat-reveal__hand .tile {
  width: var(--tile-sm-w);
  height: var(--tile-sm-h);
}

/* 玩家結局：門前在上、手牌在下，完整橫排 */
.table--round-end .seat--bottom {
  justify-content: flex-start;
}

.table--round-end .seat--bottom .seat-reveal {
  flex-direction: column;
  width: 100%;
}

.table--round-end .seat--bottom .seat-reveal__melds,
.table--round-end .seat--bottom .seat-reveal__hand {
  justify-content: center;
  width: 100%;
}

.table--round-end .seat--bottom .seat-reveal__hand {
  min-height: calc(var(--tile-h) + 0.5rem);
  max-height: calc(var(--tile-h) * 2.35 + 0.75rem);
  overflow-x: auto;
  overflow-y: visible;
  flex-wrap: wrap;
  justify-content: center;
  padding: 0.35rem;
}

.table--round-end .seat--top .seat-reveal__hand {
  flex-wrap: wrap;
  max-height: calc(var(--tile-h) * 2.2 + 0.5rem);
  overflow-y: auto;
}

.table--round-end .seat--winner .seat-reveal__hand {
  background: rgba(255, 210, 77, 0.1);
  box-shadow: inset 0 0 0 1px rgba(255, 210, 77, 0.35);
}

.seat {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 0.12rem;
  position: relative;
  border-radius: 8px;
  min-width: 0;
  min-height: 0;
  box-sizing: border-box;
}

.table-center {
  grid-area: center;
  place-self: center;
  /* 牆牌墩：寬 = 手牌一半，墩與墩之間留縫，不交疊 */
  --stack-w: calc(var(--tile-w) * 0.5);
  --stack-d: calc(var(--tile-h) * 0.5);
  --stack-gap: 2px;
  --stack-step: calc(var(--stack-w) + var(--stack-gap));
  --outer: calc(17 * var(--stack-step) - var(--stack-gap) + var(--stack-d));
  width: min(100%, var(--outer));
  height: min(100%, var(--outer));
  aspect-ratio: 1;
  min-width: 0;
  min-height: 0;
  display: grid;
  place-items: center;
  position: relative;
  overflow: visible;
  padding: 0;
  z-index: 1;
}

/* 風車框：固定尺寸方框，四面絕對定位、不置中、不交疊 */
.walls {
  position: relative;
  width: var(--outer);
  height: var(--outer);
  flex: 0 0 auto;
}

.wall-side {
  position: absolute;
  inset: 0;
}

.wall-stack {
  position: absolute;
  box-sizing: border-box;
  overflow: visible;
}

.wall-stack--h {
  width: var(--stack-w);
  height: var(--stack-d);
}

.wall-stack--v {
  width: var(--stack-d);
  height: var(--stack-w);
}

.wall-tile {
  display: block;
  width: 100%;
  height: 100%;
  margin: 0;
  padding: 0;
  border: none;
  border-radius: 2px;
  background: transparent;
  box-shadow:
    0 1px 0 #1a3554,
    0 2px 3px rgba(0, 0, 0, 0.35);
  overflow: hidden;
  position: relative;
}

.wall-stack--single .wall-tile {
  opacity: 0.85;
}

.wall-tile .tile-img {
  display: block;
  width: 100%;
  height: 100%;
  object-fit: fill;
  pointer-events: none;
  border-radius: 2px;
}

/*
 * 左右牆：容器已轉成橫向，但背面 SVG 是直向。
 * 不可直接把圖拉扁（花紋角度會錯），應旋轉整張背面圖。
 */
.wall-stack--v .wall-tile {
  overflow: hidden;
}

.wall-stack--v .wall-tile .tile-img {
  position: absolute;
  left: 50%;
  top: 50%;
  width: var(--stack-w);
  height: var(--stack-d);
  object-fit: fill;
}

/* 左牆：牌頂朝向桌心（右） */
.wall-side--left .wall-stack--v .wall-tile .tile-img {
  transform: translate(-50%, -50%) rotate(-90deg);
}

/* 右牆：牌頂朝向桌心（左） */
.wall-side--right .wall-stack--v .wall-tile .tile-img {
  transform: translate(-50%, -50%) rotate(90deg);
}

/* 牌河：框內中央 */
.river-block {
  position: absolute;
  left: calc(var(--stack-d) + 2px);
  top: calc(var(--stack-d) + 2px);
  right: calc(var(--stack-d) + 2px);
  bottom: calc(var(--stack-d) + 2px);
  display: flex;
  align-items: center;
  justify-content: center;
  overflow: visible;
  z-index: 2;
}

.wall-stack--dead .wall-tile {
  filter: brightness(0.92);
}

.center-caption {
  font-family: var(--font-display);
  font-size: 0.58rem;
  letter-spacing: 0.12em;
  opacity: 0.5;
  text-align: center;
  margin-bottom: 0.1rem;
}

/* 丟牌區：單一固定格盤，丟牌時隨機落在空格，不分家 */
.river {
  --river-cols: 11;
  --river-rows: 6;
  --river-gap: 3px;
  display: grid;
  grid-template-columns: repeat(var(--river-cols), var(--tile-sm-w));
  grid-template-rows: repeat(var(--river-rows), var(--tile-sm-h));
  gap: var(--river-gap);
  align-content: start;
  justify-content: start;
  box-sizing: content-box;
  /* 留白給紅框／金框 outline，避免被裁切 */
  padding: 6px;
  margin: auto;
  overflow: visible;
}

.river .tile {
  width: var(--tile-sm-w);
  height: var(--tile-sm-h);
  flex: 0 0 auto;
  outline-offset: 1px;
}

.river .tile--fresh,
.river .tile--flash {
  z-index: 5;
  outline-width: 2px;
  outline-offset: 1px;
}

.dice-stage {
  position: absolute;
  inset: 0;
  display: grid;
  place-content: center;
  gap: 0.35rem;
  background: transparent;
  z-index: 25;
  pointer-events: none;
}

.dice-stage[hidden] {
  display: none;
}

.dice-label {
  text-align: center;
  font-family: var(--font-display);
  letter-spacing: 0.15em;
  color: #f0e6d2;
  text-shadow: 0 1px 4px rgba(0, 0, 0, 0.65);
  font-size: 0.82rem;
}

.dice-row {
  display: flex;
  gap: 0.75rem;
  justify-content: center;
}

.die {
  display: grid;
  place-items: center;
  width: 3rem;
  height: 3rem;
  border-radius: 10px;
  background: #f7f0e0;
  color: #1a1814;
  font-size: 2rem;
  line-height: 1;
  box-shadow: 0 4px 10px rgba(0, 0, 0, 0.4);
}

.die.is-rolling {
  animation: die-shake 0.12s linear infinite;
}

@keyframes die-shake {
  0% { transform: translate(0, 0) rotate(0deg); }
  25% { transform: translate(2px, -2px) rotate(-8deg); }
  50% { transform: translate(-2px, 1px) rotate(6deg); }
  75% { transform: translate(1px, 2px) rotate(-4deg); }
  100% { transform: translate(0, 0) rotate(0deg); }
}

.dice-sum {
  text-align: center;
  color: #ffd24d;
  font-weight: 800;
  font-size: 1rem;
}


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

.seat--right {
  align-items: flex-end;
  justify-content: center;
}

.seat--left .seat-name,
.seat--right .seat-name {
  writing-mode: horizontal-tb;
  align-self: center;
}

.seat-name {
  font-size: 0.85rem;
  font-weight: 600;
  opacity: 0.9;
  line-height: 1.2;
  transition: color 0.15s ease, font-size 0.15s ease, font-weight 0.15s ease;
}

.seat-name--turn {
  color: #ff3b3b;
  font-size: 1.35rem;
  font-weight: 900;
  opacity: 1;
  text-shadow: 0 0 10px rgba(255, 60, 60, 0.45);
}

.seat-name--claim {
  color: #7dffb3;
  font-size: 1.1rem;
  font-weight: 800;
  opacity: 1;
}

.seat-name--winner {
  color: #ffd24d;
  font-size: 1.25rem;
  font-weight: 900;
  opacity: 1;
}

.seat--winner {
  background: rgba(255, 210, 77, 0.12);
  border-radius: 8px;
  box-shadow: 0 0 0 1px rgba(255, 210, 77, 0.35);
}

.player-panel--winner {
  border-radius: 10px;
  background: rgba(255, 210, 77, 0.08);
}

.tile--win {
  outline: 3px solid #ffd24d;
  outline-offset: 2px;
  box-shadow: 0 0 14px rgba(255, 210, 77, 0.75);
  z-index: 4;
}

.turn-indicator {
  display: block;
  margin-top: 0.45rem;
  color: #ff6b6b;
  font-weight: 700;
  letter-spacing: 0.02em;
}

.seat-fx,
.player-fx {
  position: absolute;
  inset: 0;
  display: grid;
  place-items: center;
  pointer-events: none;
  z-index: 8;
}

.player-panel {
  margin-top: 0.3rem;
  display: flex;
  flex-direction: column;
  gap: 0.25rem;
  position: relative;
  flex: 0 0 auto;
  overflow: visible;
}

.player-fx {
  inset: auto;
  left: 0;
  right: 0;
  top: 0;
  height: 4.5rem;
}

.fx-callout {
  position: absolute;
  display: grid;
  place-items: center;
  border-radius: 999px;
  background:
    radial-gradient(circle at 35% 30%, rgba(255, 255, 255, 0.18), transparent 34%),
    rgba(8, 22, 16, 0.62);
  border: 1px solid rgba(240, 230, 210, 0.32);
  box-shadow:
    0 12px 28px rgba(0, 0, 0, 0.36),
    inset 0 0 18px rgba(255, 255, 255, 0.05);
  backdrop-filter: blur(3px);
  -webkit-backdrop-filter: blur(3px);
  font-family: var(--font-display);
  font-size: clamp(1.9rem, 5vw, 3.2rem);
  font-weight: 900;
  line-height: 1;
  letter-spacing: 0.08em;
  color: #ffe08a;
  text-shadow:
    0 0 18px rgba(0, 0, 0, 0.55),
    0 3px 0 #6b1a10,
    0 0 28px rgba(255, 210, 100, 0.65);
  animation: fx-pop 1.05s ease-out forwards;
  will-change: transform, opacity;
  pointer-events: none;
  z-index: 4;
}

.fx-callout--mo {
  color: #9ad7ff;
  text-shadow:
    0 0 18px rgba(0, 0, 0, 0.55),
    0 3px 0 #123a5c,
    0 0 28px rgba(120, 200, 255, 0.7);
}

.fx-callout--chi {
  color: #7dffb3;
  text-shadow:
    0 0 18px rgba(0, 0, 0, 0.55),
    0 3px 0 #0d4a32,
    0 0 28px rgba(100, 255, 180, 0.7);
}

.fx-callout--pong {
  color: #ffd36a;
  text-shadow:
    0 0 18px rgba(0, 0, 0, 0.55),
    0 3px 0 #7a3d08,
    0 0 28px rgba(255, 200, 80, 0.75);
}

.fx-callout--kong {
  color: #ff9d6a;
  text-shadow:
    0 0 18px rgba(0, 0, 0, 0.55),
    0 3px 0 #6b2a0a,
    0 0 28px rgba(255, 150, 80, 0.7);
}

.fx-callout--hu {
  color: #ff6b6b;
  text-shadow:
    0 0 18px rgba(0, 0, 0, 0.55),
    0 3px 0 #6b1010,
    0 0 32px rgba(255, 100, 100, 0.8);
}

@keyframes fx-pop {
  0% {
    transform: scale(0.35) translateY(8px);
    opacity: 0;
  }
  25% {
    transform: scale(1.3) translateY(0);
    opacity: 1;
  }
  70% {
    transform: scale(1) translateY(0);
    opacity: 1;
  }
  100% {
    transform: scale(1.05) translateY(-6px);
    opacity: 0;
  }
}

.fx-flying-tile {
  position: fixed;
  z-index: 200;
  pointer-events: none;
  margin: 0;
  padding: 0;
  opacity: 1;
  border-radius: 4px;
  background: var(--tile-face-bg);
  box-shadow:
    0 0 0 1px rgba(255, 255, 255, 0.75),
    0 14px 28px rgba(0, 0, 0, 0.55);
  overflow: hidden;
}

.fx-flying-tile .tile-img {
  display: block;
  width: 100%;
  height: 100%;
  object-fit: fill;
  border-radius: 4px;
}

.fx-flying-tile--go {
  opacity: 1;
}

.tile--fresh {
  outline: 2px solid #e53935;
  outline-offset: 2px;
  box-shadow:
    0 0 0 2px rgba(229, 57, 53, 0.35),
    0 0 14px rgba(229, 57, 53, 0.8);
  z-index: 3;
}

.tile--claimed {
  outline: 3px solid #1e88e5;
  outline-offset: 2px;
  box-shadow:
    0 0 0 2px rgba(30, 136, 229, 0.35),
    0 0 14px rgba(30, 136, 229, 0.75);
  z-index: 3;
}

/* Drawn tile: gold rim（用 inset 邊框，避免 outline 點擊穿透） */
.tile--drawn {
  outline: none;
  box-shadow:
    inset 0 0 0 2px #f0c040,
    0 0 0 1px rgba(240, 192, 64, 0.45),
    0 3px 5px rgba(0, 0, 0, 0.3);
  z-index: 3;
}

.tile--flash {
  animation: tile-blink 0.5s ease-in-out 5;
}

.tile--drawn.tile--flash {
  animation: tile-blink-draw 0.4s ease-in-out 5;
}

.tile--claimed.tile--flash {
  animation: tile-blink-blue 0.5s ease-in-out 5;
}

.tile--anim-hide {
  opacity: 0 !important;
}

@keyframes tile-blink {
  0%,
  100% {
    filter: brightness(1);
    outline-color: #e53935;
  }
  50% {
    filter: brightness(1.45);
    outline-color: #ff8a80;
    box-shadow: 0 0 18px rgba(255, 80, 80, 0.95);
  }
}

@keyframes tile-blink-draw {
  0%,
  100% {
    filter: brightness(1);
  }
  50% {
    filter: brightness(1.4);
    box-shadow:
      inset 0 0 0 2px #ffe082,
      0 0 0 1px rgba(240, 192, 64, 0.45),
      0 0 16px rgba(240, 192, 64, 0.9),
      0 3px 5px rgba(0, 0, 0, 0.3);
  }
}

@keyframes tile-blink-blue {
  0%,
  100% {
    filter: brightness(1);
    outline-color: #1e88e5;
  }
  50% {
    filter: brightness(1.45);
    outline-color: #90caf9;
    box-shadow: 0 0 18px rgba(66, 165, 245, 0.95);
  }
}

.seat-hand,
.seat-melds,
.player-hand,
.player-melds {
  display: flex;
  flex-wrap: wrap;
  gap: 6px;
  justify-content: center;
  align-items: center;
  padding: 2px;
}

/* 門前區：掛在座位朝桌心一側，不佔手牌 flex 空間 */
.seat-front {
  position: absolute;
  z-index: 4;
  display: flex;
  pointer-events: none;
}

.seat-front .seat-melds:not(:empty),
.seat-front .player-melds:not(:empty) {
  pointer-events: auto;
  flex-wrap: nowrap;
  gap: 0.35rem;
  padding: 0.15rem 0.25rem;
  background: rgba(0, 0, 0, 0.18);
  border-radius: 6px;
  border: 1px solid rgba(230, 198, 110, 0.22);
}

.seat-front .meld {
  margin: 0;
  padding: 2px;
  gap: 2px;
  background: rgba(0, 0, 0, 0.16);
}

.seat-front .tile {
  width: var(--tile-sm-w);
  height: var(--tile-sm-h);
  margin: 0;
  flex: 0 0 auto;
}

.seat-front .meld--anKong .tile--back,
.seat-reveal .meld--anKong .tile--back {
  box-shadow:
    0 1px 0 #1a3554,
    0 2px 3px rgba(0, 0, 0, 0.35);
}

/* 對家：手牌在上緣，門前掛在座位下方朝桌心 */
.seat--top .seat-front {
  top: calc(100% + 0.15rem);
  left: 50%;
  transform: translateX(-50%);
  max-width: min(90%, 36rem);
}

.seat--top .seat-melds {
  flex-direction: row;
  justify-content: center;
  overflow-x: auto;
}

/* 上家：手牌在左緣，門前掛在右側朝桌心 */
.seat--left .seat-front {
  left: calc(100% + 0.15rem);
  top: 50%;
  transform: translateY(-50%);
  max-height: 90%;
}

.seat--left .seat-melds {
  flex-direction: column;
  align-items: flex-start;
  overflow-y: auto;
  max-height: min(70vh, 22rem);
}

.seat--left .meld {
  flex-direction: row;
}

/* 下家：手牌在右緣，門前掛在左側朝桌心 */
.seat--right .seat-front {
  right: calc(100% + 0.15rem);
  top: 50%;
  transform: translateY(-50%);
  max-height: 90%;
}

.seat--right .seat-melds {
  flex-direction: column;
  align-items: flex-end;
  overflow-y: auto;
  max-height: min(70vh, 22rem);
}

.seat--right .meld {
  flex-direction: row;
}

/* 玩家門前：縮小牌面，橫向排列 */
.seat--bottom .seat-front--player .player-melds {
  background: transparent;
  border: none;
  padding: 0.1rem 0;
}

.actions {
  display: flex;
  flex-wrap: wrap;
  gap: 0.35rem;
  justify-content: center;
  align-items: stretch;
  min-height: 0;
}

/* 桌面上的半透明詢問浮窗：不佔固定排版空間 */
.action-float {
  position: absolute;
  z-index: 45;
  left: 50%;
  bottom: calc(var(--seat-bottom-h) + 0.75rem);
  transform: translateX(-50%);
  min-width: min(18rem, 86%);
  max-width: min(28rem, 92%);
  padding: 0.35rem 0.55rem 0.55rem;
  background: rgba(10, 22, 16, 0.42);
  border: 1px solid rgba(230, 198, 110, 0.32);
  border-radius: 12px;
  box-shadow:
    0 10px 28px rgba(0, 0, 0, 0.22),
    inset 0 0 0 1px rgba(255, 255, 255, 0.04);
  backdrop-filter: blur(5px);
  -webkit-backdrop-filter: blur(5px);
  pointer-events: auto;
}

.action-float[hidden] {
  display: none;
}

.action-float__bar {
  margin-bottom: 0.35rem;
  padding: 0.2rem 0.35rem;
  color: #e6c66e;
  font-family: var(--font-display);
  font-size: 0.78rem;
  letter-spacing: 0.12em;
  text-align: center;
  cursor: grab;
  user-select: none;
  touch-action: none;
  border-radius: 6px;
  background: rgba(255, 255, 255, 0.04);
}

.action-float__bar:active {
  cursor: grabbing;
}

.action-float .action-btn--pass {
  color: #f0e6d2;
}

.action-btn {
  appearance: none;
  border: none;
  background: #f0e6d2;
  color: var(--ink);
  font: inherit;
  font-weight: 600;
  padding: 0.35rem 0.85rem;
  border-radius: 999px;
  cursor: pointer;
  min-height: 36px;
  font-size: 0.9rem;
}

.action-btn--hu {
  background: #f3c96b;
}

.action-btn--pass {
  background: transparent;
  color: var(--cream);
  border: 1px solid rgba(240, 230, 210, 0.4);
}

.action-btn--tiles {
  display: inline-flex;
  flex-direction: column;
  align-items: center;
  gap: 0.25rem;
  padding: 0.35rem 0.55rem 0.4rem;
  min-height: 0;
  border-radius: 12px;
}

.claim-tag {
  font-size: 0.85rem;
  font-weight: 800;
  letter-spacing: 0.08em;
  color: #1a1814;
}

.claim-tag--hu {
  color: #8a1a12;
}

.claim-tiles {
  display: flex;
  gap: 3px;
  align-items: center;
}

.claim-mini-tile {
  width: var(--tile-w);
  height: var(--tile-h);
  margin: 0;
  box-shadow: 0 1px 2px rgba(0, 0, 0, 0.25);
  pointer-events: none;
}

.claim-mini-tile .tile-img {
  border-radius: 3px;
}

.seat-hand .tile--drawn-slot,
.seat-hand .tile--drawn {
  outline: 2px solid rgba(240, 192, 64, 0.95);
  outline-offset: 1px;
  box-shadow:
    0 0 0 1px rgba(240, 192, 64, 0.35),
    0 2px 4px rgba(0, 0, 0, 0.35);
  z-index: 3;
}

/* 立牌俯視：與正面牌同寬，厚度為寬的約 1/3 */
.tile.tile--standing {
  width: var(--tile-w);
  height: var(--tile-thick);
  margin: 0;
  padding: 0;
  border: none;
  border-radius: 3px;
  background: linear-gradient(
    to bottom,
    #f7f0e0 0%,
    #ebe1cc 36%,
    #2a5f8f 40%,
    #163a5c 100%
  );
  box-shadow:
    0 1px 0 #9a8b6a,
    0 2px 3px rgba(0, 0, 0, 0.35);
  overflow: hidden;
  flex: 0 0 auto;
}

.tile.tile--standing .tile-img {
  display: none;
}

.seat-hand {
  gap: 3px;
  flex-wrap: nowrap;
  flex: 1 1 auto;
  min-width: 0;
  min-height: 0;
  width: 100%;
  box-sizing: border-box;
}

.seat--top .seat-hand {
  flex-direction: row;
  flex-wrap: nowrap;
  width: 100%;
  height: calc(var(--tile-thick) + 0.55rem);
  max-width: 100%;
  justify-content: center;
  align-items: center;
  overflow-x: auto;
  overflow-y: hidden;
}

.seat--top .tile.tile--standing.tile--drawn-slot,
.seat--top .tile.tile--standing.tile--drawn {
  margin-left: 0.55rem;
}

/* 上家：最左桌邊，直列雙排；左白右藍 */
.seat--left .seat-hand {
  flex-direction: column;
  flex-wrap: wrap;
  width: calc(var(--tile-thick) * 2 + 8px);
  max-width: 100%;
  height: 100%;
  max-height: 100%;
  align-items: flex-start;
  align-content: flex-start;
  justify-content: flex-start;
  overflow: hidden;
  gap: 3px;
}

.seat--left .tile.tile--standing {
  width: var(--tile-thick);
  height: var(--tile-w);
  background: linear-gradient(
    to right,
    #f7f0e0 0%,
    #ebe1cc 36%,
    #2a5f8f 40%,
    #163a5c 100%
  );
  box-shadow:
    1px 0 0 #9a8b6a,
    2px 0 3px rgba(0, 0, 0, 0.35);
}

.seat--left .tile.tile--standing.tile--drawn-slot,
.seat--left .tile.tile--standing.tile--drawn {
  margin-left: 0;
  margin-top: 0.45rem;
}

/* 下家：最右桌邊，直列雙排；左藍右白 */
.seat--right .seat-hand {
  flex-direction: column;
  flex-wrap: wrap;
  width: calc(var(--tile-thick) * 2 + 8px);
  max-width: 100%;
  height: 100%;
  max-height: 100%;
  align-items: flex-end;
  align-content: flex-end;
  justify-content: flex-start;
  overflow: hidden;
  gap: 3px;
}

.seat--right .tile.tile--standing {
  width: var(--tile-thick);
  height: var(--tile-w);
  background: linear-gradient(
    to right,
    #163a5c 0%,
    #2a5f8f 60%,
    #ebe1cc 64%,
    #f7f0e0 100%
  );
  box-shadow:
    -1px 0 0 #9a8b6a,
    -2px 0 3px rgba(0, 0, 0, 0.35);
}

.seat--right .tile.tile--standing.tile--drawn-slot,
.seat--right .tile.tile--standing.tile--drawn {
  margin-left: 0;
  margin-top: 0.45rem;
}

/* NPC 出牌暫顯於摸牌位：完整牌面 */
.seat-hand .tile--npc-discard-reveal {
  width: var(--tile-w);
  height: var(--tile-h);
  margin: 0.2rem 0 0 0.35rem;
  background: var(--tile-face-bg);
  box-shadow:
    0 2px 0 #b8a985,
    0 3px 4px rgba(0, 0, 0, 0.28);
}

.seat--left .tile--npc-discard-reveal,
.seat--right .tile--npc-discard-reveal {
  margin: 0.35rem 0 0;
}

.action-hint {
  font-size: 0.8rem;
  opacity: 0.75;
  align-self: center;
}

.player-hand-wrap {
  overflow: visible;
}

.player-hand {
  box-sizing: border-box;
  width: 100%;
  height: 100%;
  overflow-x: auto;
  overflow-y: visible;
  flex-wrap: nowrap;
  justify-content: flex-start;
  /* 上方留白給 hover／金邊上移，整張牌仍落在可視區內 */
  padding: calc(var(--tile-h) * 0.35) 0.3rem 0.35rem;
  -webkit-overflow-scrolling: touch;
  align-items: flex-end;
}

.player-hand .tile {
  width: var(--tile-w);
  height: var(--tile-h);
  flex: 0 0 auto;
  transition: transform 0.12s ease;
  transform: translateY(0);
}

.player-hand .tile--clickable:not(:disabled) {
  cursor: pointer;
}

/* PC：滑鼠指到上移；手機改點選（.tile--selected） */
@media (hover: hover) and (pointer: fine) {
  .player-hand .tile:not(:disabled):hover {
    transform: translateY(-25%);
    z-index: 5;
  }

  .player-hand .tile--drawn-slot:hover,
  .player-hand .tile--drawn:hover {
    transform: translateY(-25%);
    z-index: 6;
  }
}

.player-hand .tile--selected {
  transform: translateY(-25%);
  z-index: 5;
}

/* Claim option hover: lift matching hand tiles by 1/8 */
.player-hand .tile--claim-hint {
  transform: translateY(-12.5%);
  z-index: 4;
}

/* Drawn: gold rim already on .tile--drawn; rest lift 1/8 */
.player-hand .tile--drawn-slot,
.player-hand .tile--drawn {
  margin-left: 0.75rem;
  z-index: 4;
  transform: translateY(-12.5%);
}

.player-hand .tile--drawn-slot.tile--selected,
.player-hand .tile--drawn.tile--selected {
  transform: translateY(-25%);
  z-index: 6;
}

.tile {
  appearance: none;
  width: var(--tile-w);
  height: var(--tile-h);
  padding: 0;
  border: none;
  border-radius: 5px;
  background: var(--tile-face-bg);
  color: var(--ink);
  cursor: default;
  position: relative;
  overflow: visible;
  margin: 1px;
  box-shadow:
    0 2px 0 #b8a985,
    0 3px 4px rgba(0, 0, 0, 0.28);
}

.tile:not(:disabled) {
  cursor: pointer;
}

.tile--back {
  box-shadow:
    0 2px 0 #1a3554,
    0 4px 5px rgba(0, 0, 0, 0.35);
}

.tile-img {
  display: block;
  width: 100%;
  height: 100%;
  object-fit: fill;
  pointer-events: none;
  user-select: none;
  border-radius: 5px;
}

.meld {
  display: flex;
  gap: 6px;
  margin: 0 6px;
  padding: 4px;
  background: rgba(0, 0, 0, 0.12);
  border-radius: 4px;
}

.overlay {
  position: fixed;
  inset: 0;
  background: transparent;
  display: block;
  padding: 0;
  z-index: 25;
  pointer-events: none;
}

.overlay[hidden] {
  display: none;
}

.dialog {
  pointer-events: auto;
  position: absolute;
  left: 50%;
  top: 42%;
  transform: translate(-50%, -50%);
  background: rgba(28, 22, 16, 0.42);
  border: 2px solid rgba(240, 210, 120, 0.45);
  border-radius: 12px;
  padding: 0.55rem 1.1rem 1rem;
  width: max-content;
  max-width: min(92vw, 42rem);
  text-align: center;
  box-shadow: 0 12px 28px rgba(0, 0, 0, 0.28);
  backdrop-filter: blur(5px);
  -webkit-backdrop-filter: blur(5px);
}

.dialog__bar {
  margin: 0 0 0.65rem;
  padding: 0.35rem 0.5rem;
  color: #e6c66e;
  font-family: var(--font-display);
  font-size: 1.05rem;
  font-weight: 700;
  letter-spacing: 0.08em;
  cursor: grab;
  user-select: none;
  touch-action: none;
  border-radius: 8px;
  background: rgba(255, 255, 255, 0.05);
}

.dialog__bar:active {
  cursor: grabbing;
}

.dialog p {
  margin: 0 0 1.1rem;
  line-height: 1.5;
  white-space: nowrap;
}

.result-table {
  width: 100%;
  margin: 0 0 1rem;
  border-collapse: collapse;
  font-variant-numeric: tabular-nums;
}

.result-table th,
.result-table td {
  padding: 0.3rem 0.65rem;
  border-bottom: 1px solid rgba(233, 223, 200, 0.12);
  text-align: right;
}

.result-table th:first-child,
.result-table td:first-child {
  text-align: left;
}

.score-positive {
  color: #79d99a;
}

.score-negative {
  color: #ff9b8e;
}

.dialog__actions {
  display: flex;
  justify-content: center;
  gap: 0.65rem;
}

.overlay--settings {
  pointer-events: auto;
  background: rgba(0, 0, 0, 0.28);
  z-index: 30;
}

.dialog--settings {
  text-align: left;
  min-width: min(92vw, 18rem);
}

.settings-body {
  display: grid;
  gap: 0.85rem;
  margin: 0 0 1rem;
}

.settings-row {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 1rem;
  cursor: pointer;
}

.settings-speed {
  margin: 0;
  padding: 0.65rem 0.75rem;
  border: 1px solid rgba(233, 223, 200, 0.18);
  border-radius: 8px;
}

.settings-speed legend {
  padding: 0 0.35rem;
  color: #e6c66e;
  font-size: 0.92rem;
}

.settings-speed label {
  display: flex;
  align-items: center;
  gap: 0.45rem;
  margin: 0.35rem 0;
  cursor: pointer;
}

@media (min-width: 700px) {
  :root {
    --tile-w: 2.6rem;
    --tile-h: 3.5rem;
  }

  .player-hand {
    justify-content: center;
    flex-wrap: wrap;
  }
}

@media (max-width: 900px) {
  html,
  body {
    overflow: auto;
  }

  .app {
    height: auto;
    min-height: 100dvh;
    max-height: none;
    grid-template-columns: minmax(0, 1fr);
    grid-template-rows: minmax(38rem, 72dvh) minmax(24rem, 55dvh);
    overflow: visible;
  }

  .game-area,
  .info-panel {
    height: 100%;
  }
}

@media (max-width: 700px) {
  :root {
    /* 中央區較擠時改用 1/2 */
    --tile-sm-w: calc(var(--tile-w) * 0.5);
    --tile-sm-h: calc(var(--tile-h) * 0.5);
    --tile-sm-thick: calc(var(--tile-sm-w) * 0.34);
  }
}

@media (max-width: 480px) {
  :root {
    --tile-w: 2.2rem;
    --tile-h: 2.95rem;
  }

  .table {
    border-width: 4px;
  }

  .seat-name--turn {
    font-size: 1.15rem;
  }

  .topbar-meta {
    font-size: 0.75rem;
  }
}

/*
 * 手機／低高度視窗：以固定畫布等比例縮放，使整個遊戲介面留在螢幕內。
 * 尺寸及縮放比例由 main.js 依 visualViewport 即時更新。
 */
html.fit-screen {
  --tile-w: 2.6rem;
  --tile-h: 3.5rem;
  --tile-sm-w: calc(var(--tile-w) * 2 / 3);
  --tile-sm-h: calc(var(--tile-h) * 2 / 3);
  --tile-sm-thick: calc(var(--tile-sm-w) * 0.34);
}

html.fit-screen,
html.fit-screen body {
  width: 100%;
  height: 100%;
  overflow: hidden;
  overscroll-behavior: none;
}

html.fit-screen .app {
  position: fixed;
  left: 50%;
  top: 50%;
  width: var(--screen-width);
  height: var(--screen-height);
  min-height: 0;
  max-width: none;
  max-height: none;
  margin: 0;
  overflow: hidden;
  transform: translate(-50%, -50%) scale(var(--screen-scale));
  transform-origin: center;
}

html.fit-screen--landscape .app {
  grid-template-columns: minmax(0, 1fr) 20rem;
  grid-template-rows: minmax(0, 1fr);
}

html.fit-screen--portrait .app {
  grid-template-columns: minmax(0, 1fr);
  grid-template-rows: 720px 460px;
}

html.fit-screen .game-area,
html.fit-screen .info-panel {
  width: 100%;
  height: 100%;
}

html.fit-screen--portrait .info-panel {
  display: grid;
  grid-template-columns: repeat(2, minmax(0, 1fr));
  grid-template-rows: auto minmax(0, 1fr) minmax(0, 1fr);
  gap: 0.5rem;
  padding: 0.65rem;
}

html.fit-screen--portrait .info-header {
  grid-column: 1 / -1;
}

html.fit-screen--portrait .info-card {
  min-height: 0;
  padding: 0.55rem 0.7rem;
  overflow: hidden;
}

html.fit-screen--portrait .info-card--message {
  min-height: 0;
}

html.fit-screen--portrait .info-card--log {
  min-height: 0;
}

