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

:root {
  --bg-main: #0f1a14;
  --bg-panel: #16241c;
  --bg-box: #1d3025;
  --felt: #1f4a32;
  --line: #2f5a43;
  --gold: #f0c040;
  --text: #eef3ee;
  --muted: #9db3a5;
  --primary: #3b8f5f;
  --primary-hover: #47a76f;
  --danger: #f85149;
  --win: #5fd38d;
  --radius: 10px;
  --tw: min(9.6vw, 42px);
}

body {
  min-height: 100vh;
  padding-top: 44px;
  background: var(--bg-main);
  color: var(--text);
  font-family: "Noto Sans TC", "PingFang TC", "Microsoft JhengHei", system-ui, sans-serif;
  line-height: 1.5;
  -webkit-tap-highlight-color: transparent;
}

/* TEND page shell */
.tend-game-nav {
  position: fixed;
  inset: 0 0 auto;
  z-index: 320; /* 蓋在結算與特效之上，結算時也按得到靜音 */
  display: flex;
  align-items: center;
  gap: 10px;
  min-height: 44px;
  padding: 8px 16px;
  background: rgba(15, 26, 20, 0.96);
  border-bottom: 1px solid var(--line);
  color: var(--muted);
  font-size: 0.85rem;
}

.tend-game-nav a {
  color: var(--text);
  text-decoration: none;
}

.tend-game-nav a:first-child {
  color: var(--gold);
  font-weight: 700;
  letter-spacing: 0.08em;
}

.nav-sound {
  margin-left: 10px;
  padding: 2px 10px;
  border: 1px solid var(--line);
  border-radius: 999px;
  background: var(--bg-box);
  color: var(--text);
  font: inherit;
  font-size: 0.8rem;
  cursor: pointer;
}

.nav-sound[aria-pressed="true"] {
  color: var(--muted);
}

.nav-room {
  margin-left: auto;
  color: var(--gold);
  font-weight: 700;
  letter-spacing: 0.1em;
}

.app {
  max-width: 720px;
  margin: 0 auto;
  padding: 16px 12px 40px;
}

.panel {
  background: var(--bg-panel);
  border: 1px solid var(--line);
  border-radius: var(--radius);
  padding: 20px 16px;
}

.title {
  font-size: 1.8rem;
  color: var(--gold);
}

.title small {
  font-size: 1rem;
  color: var(--muted);
  margin-left: 6px;
}

.lead {
  color: var(--muted);
  margin: 4px 0 16px;
}

.field {
  display: flex;
  flex-direction: column;
  gap: 4px;
  margin-bottom: 12px;
  font-size: 0.9rem;
  color: var(--muted);
}

.field input,
.field select {
  font: inherit;
  font-size: 1rem;
  color: var(--text);
  background: var(--bg-main);
  border: 1px solid var(--line);
  border-radius: 8px;
  padding: 10px 12px;
}

.suits {
  flex-direction: row;
  flex-wrap: wrap;
  gap: 6px;
  border: 0;
}

.suits legend {
  width: 100%;
  margin-bottom: 4px;
}

.suit-opt {
  display: flex;
  align-items: center;
  gap: 4px;
  padding: 6px 10px;
  border: 1px solid var(--line);
  border-radius: 8px;
  background: var(--bg-main);
  color: var(--text);
  font-size: 1.1rem;
  cursor: pointer;
}

.suit-opt.red {
  color: #ff6b6b;
}

.room-rules {
  margin-top: 12px;
  padding: 8px 10px;
  border-radius: 8px;
  background: var(--bg-box);
  color: var(--gold);
  font-size: 0.9rem;
}

.hand.leftover .card {
  opacity: 0.35;
  cursor: default;
}

.split {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 12px;
}

@media (max-width: 480px) {
  .split {
    grid-template-columns: 1fr;
  }
}

.box {
  background: var(--bg-box);
  border-radius: var(--radius);
  padding: 14px;
}

.box h2,
.sub {
  font-size: 1.05rem;
  margin-bottom: 10px;
}

.sub {
  margin-top: 18px;
}

.btn {
  font: inherit;
  font-size: 0.95rem;
  color: var(--text);
  background: var(--bg-box);
  border: 1px solid var(--line);
  border-radius: 8px;
  padding: 10px 14px;
  cursor: pointer;
  width: 100%;
}

.btn:hover:not(:disabled) {
  border-color: var(--gold);
}

.btn:disabled {
  opacity: 0.45;
  cursor: not-allowed;
}

.btn.primary {
  background: var(--primary);
  border-color: var(--primary);
  font-weight: 700;
}

.btn.primary:hover:not(:disabled) {
  background: var(--primary-hover);
}

.btn.gold {
  background: var(--gold);
  border-color: var(--gold);
  color: #2a1f00;
  font-weight: 700;
}

.btn.small {
  width: auto;
  padding: 4px 10px;
  font-size: 0.8rem;
}

.row-btns {
  display: flex;
  gap: 10px;
  margin-top: 14px;
}

.msg {
  min-height: 1.5em;
  margin-top: 10px;
  color: var(--gold);
  font-size: 0.9rem;
}

.hint {
  margin-top: 8px;
  color: var(--muted);
  font-size: 0.8rem;
}

.rules {
  margin-top: 16px;
  color: var(--muted);
  font-size: 0.88rem;
}

.rules summary {
  cursor: pointer;
  color: var(--text);
}

.rules ul {
  margin: 8px 0 0 1.2em;
}

/* 等候室 */
.room-head {
  display: flex;
  justify-content: space-between;
  align-items: center;
  gap: 12px;
}

.label {
  color: var(--muted);
  font-size: 0.85rem;
}

.room-code {
  font-size: 2.6rem;
  font-weight: 800;
  letter-spacing: 0.18em;
  color: var(--gold);
}

.qr {
  background: #fff;
  padding: 6px;
  border-radius: 6px;
  line-height: 0;
}

.player-list {
  list-style: none;
}

.player-list li {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 8px 10px;
  border-bottom: 1px solid var(--line);
}


/* 牌桌 */
.app {
  max-width: 1100px;
}

.table-top {
  display: flex;
  align-items: center;
  gap: 10px;
  font-weight: 700;
  margin-bottom: 8px;
}

.wall {
  color: var(--muted);
  font-weight: 400;
  font-size: 0.85rem;
}

.timer {
  margin-left: auto;
  min-width: 2.6em;
  text-align: center;
  padding: 2px 10px;
  border-radius: 999px;
  background: var(--bg-box);
}

.timer:empty {
  visibility: hidden;
}

.timer.urgent {
  background: var(--danger);
}

.opponents {
  display: grid;
  grid-template-columns: repeat(3, minmax(0, 1fr));
  gap: 6px;
}

.seat-panel,
.me {
  background: var(--felt);
  border: 2px solid transparent;
  border-radius: var(--radius);
  padding: 6px;
  min-width: 0;
}

.seat-panel.active,
.me.active {
  border-color: var(--gold);
}

.seat-rel {
  font-size: 0.7rem;
  color: var(--muted);
}

.seat-head {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  gap: 4px;
  font-size: 0.82rem;
  margin-bottom: 4px;
}

.seat-head strong {
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
  max-width: 7em;
}

.seat-head .wind {
  color: var(--gold);
}

.seat-head .score {
  margin-left: auto;
  font-variant-numeric: tabular-nums;
}

.badge {
  font-size: 0.68rem;
  padding: 0 6px;
  border-radius: 999px;
  background: var(--bg-box);
  color: var(--muted);
}

.badge.gold {
  background: var(--gold);
  color: #2a1f00;
}

.hidden-hand,
.melds,
.river,
.my-hand,
.win-line {
  display: flex;
  flex-wrap: wrap;
  gap: 2px;
}

.seat-head .count {
  font-size: 0.72rem;
  color: var(--muted);
}

.trays {
  display: flex;
  flex-direction: column;
  gap: 3px;
  margin: 4px 0;
}

.tray {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  gap: 4px;
  padding: 3px;
  border-radius: 6px;
  background: rgba(0, 0, 0, 0.22);
}

.tray-label {
  font-size: 0.66rem;
  color: var(--muted);
  margin-right: 2px;
}

.river .tray-label {
  width: 100%;
}

.meld.new {
  outline: 2px solid var(--gold);
  outline-offset: 1px;
  border-radius: 3px;
}

.claim-banner .event {
  color: var(--gold);
  font-size: 1.1rem;
}

.meld {
  display: inline-flex;
  gap: 1px;
}

.river {
  margin-top: 4px;
  min-height: 1.5em;
}

.claim-banner {
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 8px;
  margin: 8px 0;
  font-weight: 700;
}

.claim-banner .muted {
  font-weight: 400;
}

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

.me {
  margin-top: 6px;
}

.my-hand {
  margin-top: 6px;
  gap: 3px;
  justify-content: center;
}

/* 牌 */
.tile {
  --w: var(--tw);
  position: relative;
  width: var(--w);
  height: calc(var(--w) * 1.32);
  flex: none;
  display: inline-flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  border-radius: calc(var(--w) * 0.14);
  background: linear-gradient(#fffef8, #efe9d6);
  border: 1px solid #b9b19a;
  box-shadow: 0 2px 0 #8d8770;
  color: #1a1a1a;
  line-height: 1;
  user-select: none;
  transition: transform 0.12s ease;
}

.tile.sm {
  --w: min(6.4vw, 28px);
}

.tile.xs {
  --w: min(4.2vw, 19px);
  box-shadow: none;
}

.tile.face {
  background: #f5f3ea url("img/tiles.webp?v=1") no-repeat;
  background-size: 900% 500%;
  background-position: calc(var(--c) * 12.5%) calc(var(--r) * 25%);
  border-color: #c9c3ad;
}

.tile.back {
  background: linear-gradient(#2f8f5b, #1f6b42);
  border-color: #17502f;
}

.tile.pick {
  cursor: pointer;
}

.tile.drawn {
  margin-left: calc(var(--w) * 0.3);
  outline: 2px solid var(--gold);
}

.tile.selected {
  transform: translateY(-10px);
  box-shadow: 0 0 0 3px var(--gold);
}

.tile.last {
  outline: 2px solid var(--danger);
}

.action-bar:empty {
  display: none;
}

.action-bar {
  display: flex;
  flex-wrap: wrap;
  justify-content: center;
  gap: 8px;
  margin-top: 10px;
}

.action-bar .btn {
  width: auto;
  min-width: 5em;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 4px;
  font-size: 1.05rem;
}

.overlay-box.result-box {
  max-width: 520px;
  max-height: 90vh;
  overflow-y: auto;
  text-align: left;
}

.result-box h2 {
  color: var(--gold);
  margin-bottom: 4px;
}

.result-box h3 {
  margin-top: 12px;
}

.fan-list,
.pay-list,
.rank-list {
  margin: 8px 0 0 1.4em;
}

.pay-list .win {
  color: var(--win);
}

.pay-list .lose {
  color: var(--danger);
}

.total {
  margin-top: 6px;
  font-weight: 700;
}

@media (orientation: landscape) and (min-width: 700px) {
  .tile.xs {
    --w: min(2.6vw, 4.6vh, 24px);
  }

  .tile.sm {
    --w: min(3.4vw, 6.4vh, 30px);
  }

  .tile {
    --w: min(5vw, 9.6vh, 46px);
  }
}

/* 手機橫放：高度很矮，壓縮間距 */
@media (orientation: landscape) and (max-height: 500px) {
  body {
    padding-top: 0;
  }

  .tend-game-nav {
    position: static;
    min-height: 0;
    padding: 2px 12px;
  }

  .app {
    padding: 4px 8px 12px;
  }

  .table-top {
    margin-bottom: 4px;
    font-size: 0.85rem;
  }

  .claim-banner {
    margin: 4px 0;
  }

  .seat-panel,
  .me {
    padding: 4px;
  }

  .seat-rel {
    display: none;
  }

  .seat-head {
    margin-bottom: 2px;
  }

  .trays {
    flex-direction: row;
    flex-wrap: wrap;
    margin: 2px 0;
  }

  .tray {
    padding: 2px;
  }

  .river {
    margin-top: 2px;
  }

  .river .tray-label {
    width: auto;
  }

  .claim-banner .tile {
    --w: min(3.4vw, 7vh, 30px);
  }

  .msg {
    min-height: 0;
    margin-top: 2px;
  }

  .action-bar {
    margin-top: 4px;
  }
}

/* 聽牌、自摸、胡牌特效 */
.badge.ting {
  background: #3fa9ff;
  color: #fff;
  box-shadow: 0 0 8px #3fa9ff;
}

.btn.ting-btn {
  background: linear-gradient(135deg, #3fa9ff, #b388ff);
  border-color: #7df9ff;
  color: #fff;
  font-weight: 800;
  box-shadow: 0 0 12px rgba(63, 169, 255, 0.7);
  position: relative;
}

/* 光暈放在另一層、只改透明度（不用每格重畫陰影），閃 4 下就停 */
.btn.ting-btn::after {
  content: '';
  position: absolute;
  inset: 0;
  border-radius: inherit;
  box-shadow: 0 0 22px rgba(125, 249, 255, 0.95);
  opacity: 0;
  pointer-events: none;
  animation: ting-pulse 1.2s ease-in-out 4;
}

@keyframes ting-pulse {
  50% {
    opacity: 1;
  }
}

.fx {
  position: fixed;
  inset: 0;
  z-index: 280;
  pointer-events: none;
  overflow: hidden;
}

.fx-canvas {
  position: absolute;
  inset: 0;
}

.fx-layer {
  position: absolute;
  inset: 0;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  background: radial-gradient(circle, rgba(0, 0, 0, 0.15), rgba(0, 0, 0, 0.75));
  animation: fx-fade var(--fx-ms) ease forwards;
}

.fx-rays {
  position: absolute;
  width: 220vmax;
  height: 220vmax;
  background: repeating-conic-gradient(from 0deg, color-mix(in srgb, var(--glow) 55%, transparent) 0deg 8deg, transparent 8deg 22deg);
  mask-image: radial-gradient(circle, #000 0%, transparent 55%);
  -webkit-mask-image: radial-gradient(circle, #000 0%, transparent 55%);
  animation: fx-rays var(--fx-ms) linear forwards;
}

.fx-text {
  position: relative;
  font-size: min(34vw, 46vh);
  font-weight: 900;
  line-height: 1;
  letter-spacing: 0.06em;
  color: #fff;
  -webkit-text-stroke: 1.5px rgba(255, 255, 255, 0.7);
  filter: drop-shadow(0 0 14px var(--glow)) drop-shadow(0 0 40px var(--glow));
  animation: fx-slam 0.55s cubic-bezier(0.2, 1.6, 0.4, 1) both, fx-shake 0.35s 0.5s linear;
}

.fx-ting .fx-text {
  font-size: min(24vw, 34vh);
  background: linear-gradient(180deg, #ffffff, #7df9ff 45%, #3fa9ff);
  -webkit-background-clip: text;
  background-clip: text;
  color: transparent;
}

.fx-zimo .fx-text {
  font-size: min(26vw, 38vh);
  background: linear-gradient(180deg, #fffde7, #ffd54f 45%, #ff8f00);
  -webkit-background-clip: text;
  background-clip: text;
  color: transparent;
}

.fx-hu .fx-text {
  background: linear-gradient(180deg, #fff3e0, #ff9100 40%, #d50000);
  -webkit-background-clip: text;
  background-clip: text;
  color: transparent;
}

.fx-sub {
  position: relative;
  margin-top: 2vh;
  padding: 6px 18px;
  border-radius: 999px;
  background: rgba(0, 0, 0, 0.55);
  border: 1px solid var(--glow);
  color: #fff;
  font-size: clamp(1rem, 4vw, 1.6rem);
  font-weight: 800;
  box-shadow: 0 0 18px var(--glow);
  animation: fx-sub 0.5s 0.35s ease both;
}

@keyframes fx-slam {
  0% {
    transform: scale(4) rotate(-8deg);
    opacity: 0;
  }
  60% {
    opacity: 1;
  }
  100% {
    transform: scale(1) rotate(0deg);
    opacity: 1;
  }
}

@keyframes fx-shake {
  0%, 100% { transform: translate(0, 0); }
  20% { transform: translate(-10px, 6px); }
  40% { transform: translate(9px, -7px); }
  60% { transform: translate(-6px, -4px); }
  80% { transform: translate(5px, 5px); }
}

@keyframes fx-sub {
  from {
    transform: translateY(30px);
    opacity: 0;
  }
  to {
    transform: none;
    opacity: 1;
  }
}

@keyframes fx-rays {
  from {
    transform: rotate(0deg) scale(0.2);
    opacity: 0;
  }
  15% {
    opacity: 1;
  }
  to {
    transform: rotate(70deg) scale(1);
    opacity: 0.9;
  }
}

@keyframes fx-fade {
  0%, 82% {
    opacity: 1;
  }
  100% {
    opacity: 0;
  }
}

@media (prefers-reduced-motion: reduce) {
  .fx-text,
  .fx-rays,
  .fx-sub,
  .btn.ting-btn::after {
    animation-duration: 0.01ms;
  }
}

.overlay {
  position: fixed;
  inset: 0;
  z-index: 300;
  background: rgba(0, 0, 0, 0.7);
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 16px;
}

.overlay-box {
  background: var(--bg-panel);
  border: 1px solid var(--line);
  border-radius: var(--radius);
  padding: 20px;
  max-width: 340px;
  width: 100%;
  text-align: center;
}

.overlay-box p {
  margin-bottom: 14px;
}

[hidden] {
  display: none !important;
}

.nav-room[hidden] + .nav-sound {
  margin-left: auto;
}

/* 省電模式（nav 的「🔋 省電」） */
.nav-sound.eco-btn[aria-pressed="true"] {
  color: var(--gold);
  border-color: var(--gold);
}

.eco .fx-rays {
  display: none;
}

.eco .fx-text {
  filter: none;
}

.eco .btn.ting-btn::after {
  animation: none;
}

/* 導覽列不斷行；手機上聲音、省電按鈕只留圖示 */
.tend-game-nav a,
.nav-room,
.nav-sound {
  white-space: nowrap;
}

@media (max-width: 480px) {
  .nav-label {
    display: none;
  }

  .nav-sound {
    margin-left: 6px;
    padding: 2px 8px;
  }
}
