*, *::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;
  --cw: 46px;
}

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;
  /* 快速連點按鈕或牌時，不要被當成「點兩下放大」 */
  touch-action: manipulation;
  -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;
}

.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;
}

/* 開房人數：3 人／4 人 */
.seg {
  display: flex;
  gap: 6px;
  margin-bottom: 10px;
}

.seg label {
  flex: 1;
}

.seg input {
  position: absolute;
  opacity: 0;
  pointer-events: none;
}

.seg span {
  display: block;
  text-align: center;
  padding: 6px 0;
  border: 1px solid var(--line);
  border-radius: 8px;
  color: var(--muted);
  cursor: pointer;
}

.seg input:checked + span {
  background: var(--gold);
  border-color: var(--gold);
  color: #2a1f00;
  font-weight: 700;
}

.seg input:focus-visible + span {
  outline: 2px solid var(--gold);
  outline-offset: 2px;
}

.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);
}


/* 特效 */
.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-plane .fx-text,
.fx-straight .fx-text,
.fx-pairs .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-rocket .fx-text,
.fx-landlord .fx-text,
.fx-lwin .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-bomb .fx-text,
.fx-spring .fx-text,
.fx-antispring .fx-text,
.fx-fwin .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,
  .fx-mini .fx-text {
    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;
}

/* 小特效：不壓暗畫面，字小一點 */
.fx-mini {
  background: none;
}

.fx-mini .fx-text {
  font-size: min(20vw, 26vh);
}

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

.fx-spring .fx-text,
.fx-antispring .fx-text {
  background: linear-gradient(180deg, #fff, #ff80ab 45%, #f50057);
  -webkit-background-clip: text;
  background-clip: text;
}

.fx-fwin .fx-text {
  background: linear-gradient(180deg, #fff, #b9f6ca 45%, #00c853);
  -webkit-background-clip: text;
  background-clip: text;
}

.fx-lwin .fx-text,
.fx-fwin .fx-text,
.fx-antispring .fx-text {
  font-size: min(20vw, 30vh);
}

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

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

.mult {
  color: var(--gold);
  font-size: 0.9rem;
}

.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);
}

.bottom-row {
  order: 9;
  width: 100%;
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 8px;
}

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

.bottom-cards,
.played {
  display: flex;
  justify-content: center;
}

.played .card + .card,
.revealed .card + .card {
  margin-left: calc(var(--cw) * -0.45);
}

.opponents {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 6px;
}

/* 4 人：上家｜對家｜下家 三欄，出的牌縮小，太長就換行 */
.opponents.four {
  grid-template-columns: 1fr 1fr 1fr;
  gap: 4px;
}

.opponents.four .seat-panel {
  padding: 4px;
}

.opponents.four .seat-head {
  font-size: 0.75rem;
}

.opponents.four .seat-head strong {
  max-width: 5.5em;
}

.opponents.four .count b {
  font-size: 1.15rem;
}

.opponents.four .play-area .card {
  --cw: 22px;
}

.opponents.four .played {
  flex-wrap: wrap;
}

.opponents.four .played .card + .card {
  margin-left: calc(var(--cw) * -0.55);
}

.seat-panel,
.me,
.my-play {
  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.85rem;
}

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

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

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

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

.badge.farmer {
  background: #2e7d32;
  color: #e8f5e9;
}

.count {
  margin: 4px 0;
  font-size: 0.85rem;
  color: var(--muted);
}

.count b {
  font-size: 1.4rem;
  color: var(--text);
  margin: 0 2px;
}

.count.warn b {
  color: var(--danger);
  animation: warn-blink 0.8s ease-in-out 4;
}

@keyframes warn-blink {
  50% {
    opacity: 0.35;
  }
}

.play-area {
  min-height: calc(var(--cw) * 1.4 * 0.75 + 22px);
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 2px;
  padding: 4px;
  border-radius: 8px;
  background: rgba(0, 0, 0, 0.2);
  font-size: 0.85rem;
}

.play-area .say {
  color: var(--gold);
  font-weight: 700;
}

.play-area .pass {
  color: var(--muted);
}

.play-area .ptype {
  font-size: 0.72rem;
  color: var(--gold);
}

.play-area .card {
  --cw: 32px;
}

.my-play {
  margin-top: 6px;
  padding: 4px;
  background: none;
}

.me {
  margin-top: 6px;
}

/* 撲克牌 */
.card {
  position: relative;
  width: var(--cw);
  height: calc(var(--cw) * 1.4);
  flex: none;
  border-radius: calc(var(--cw) * 0.12);
  border: 1px solid #bdbdbd;
  background: #fdfdfb;
  color: #1a1a1a;
  box-shadow: -1px 0 2px rgba(0, 0, 0, 0.25);
  line-height: 1;
  user-select: none;
  transition: transform 0.1s ease;
}

.card .rank {
  position: absolute;
  top: calc(var(--cw) * 0.07);
  left: calc(var(--cw) * 0.08);
  font-size: calc(var(--cw) * 0.36);
  font-weight: 800;
  letter-spacing: -0.04em;
}

.card .suit {
  position: absolute;
  top: calc(var(--cw) * 0.46);
  left: calc(var(--cw) * 0.1);
  font-size: calc(var(--cw) * 0.3);
}

.card .pip {
  position: absolute;
  right: calc(var(--cw) * 0.08);
  bottom: calc(var(--cw) * 0.06);
  font-size: calc(var(--cw) * 0.62);
  opacity: 0.9;
}

.card.red {
  color: #c62828;
}

.card.joker .rank,
.card.joker .suit {
  font-size: calc(var(--cw) * 0.32);
  font-weight: 800;
}

.card.joker .suit {
  top: calc(var(--cw) * 0.44);
}

.card .rank.ten {
  font-size: calc(var(--cw) * 0.27);
  letter-spacing: -0.12em;
}

.card.joker .pip {
  font-size: calc(var(--cw) * 0.5);
}

.card.back {
  background: repeating-linear-gradient(45deg, #b71c1c 0 4px, #8e1414 4px 8px);
  border-color: #fff;
  box-shadow: inset 0 0 0 2px #fff;
}

.card.sm {
  --cw: 30px;
}

/* 手指在手牌上滑動是用來連選，不捲動畫面、不放大 */
.my-hand {
  --step: 26px;
  display: flex;
  flex-direction: column;
  align-items: center;
  padding-top: 20px;
  margin-top: 4px;
  touch-action: none;
  -webkit-user-select: none;
  user-select: none;
  -webkit-touch-callout: none;
}

.hand-row {
  display: flex;
  justify-content: center;
}

/* 下排壓住上排的下半部；多留 18px（選起時抬高的距離），下排選起後上排點數和花色還看得到 */
.my-hand.multi-rows .hand-row + .hand-row {
  margin-top: calc(var(--cw) * -0.62 + 18px);
}

.my-hand .card {
  cursor: pointer;
}

.hand-row .card + .card {
  margin-left: calc(var(--step) - var(--cw));
}

.my-hand .card.selected {
  transform: translateY(-18px);
  background: #fff6d6;
  box-shadow: 0 0 0 3px var(--gold), 0 4px 8px rgba(0, 0, 0, 0.35);
}

.my-hand .card.bottom-new::after {
  content: "";
  position: absolute;
  inset: 2px;
  border-radius: inherit;
  box-shadow: inset 0 0 0 2px var(--gold);
  pointer-events: none;
}

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

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

.action-bar .btn {
  width: auto;
  min-width: 5em;
  font-size: 1.05rem;
}

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

.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;
}

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

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

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

.revealed {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  gap: 4px 0;
  margin-top: 6px;
  font-size: 0.85rem;
}

.revealed .who {
  width: 100%;
  color: var(--muted);
}

.revealed .card {
  --cw: 28px;
}

/* 手機橫放：高度很矮，壓縮間距 */
@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 8px;
  }

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

  .bottom-row {
    order: 0;
    width: auto;
    margin-left: auto;
  }

  .bottom-cards .card {
    --cw: 22px;
  }

  /* 上家｜我出的牌｜下家 排成一列 */
  .table-mid {
    display: grid;
    grid-template-columns: 1fr 1.3fr 1fr;
    gap: 6px;
    align-items: stretch;
  }

  .opponents {
    display: contents;
  }

  .opponents .seat-panel:first-child {
    order: 1;
  }

  .my-play {
    order: 2;
    margin-top: 0;
    display: flex;
    align-items: center;
    justify-content: center;
  }

  .opponents .seat-panel:last-child {
    order: 3;
  }

  /* 4 人：上家｜對家｜我出的牌｜下家 */
  .four .table-mid {
    grid-template-columns: 1fr 1fr 1.2fr 1fr;
  }

  .four .opponents .seat-panel:nth-child(2) {
    order: 2;
  }

  .four .my-play {
    order: 3;
  }

  .four .opponents .seat-panel:last-child {
    order: 4;
  }

  /* 四欄太窄，張數放回名字下面，不疊在右上角 */
  .four .seat-panel .count {
    position: static;
    font-size: 0.75rem;
    line-height: 1.2;
  }

  .four .seat-panel .count b {
    font-size: 0.95rem;
  }

  .four .play-area {
    min-height: 40px;
  }

  .seat-panel .count {
    position: absolute;
    top: 4px;
    right: 8px;
  }

  .seat-panel {
    position: relative;
  }

  .seat-head .score {
    margin-left: 4px;
  }

  .count b {
    font-size: 1.1rem;
  }

  .play-area {
    min-height: 52px;
    margin-top: 2px;
  }

  .action-bar .btn {
    padding: 5px 12px;
    font-size: 0.95rem;
  }

  .me .seat-head {
    font-size: 0.75rem;
  }

  .seat-rel {
    display: none;
  }

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

  .count {
    margin: 0;
  }

  .play-area .card {
    --cw: 26px;
  }

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

  .my-hand {
    padding-top: 12px;
  }
}

/* 省電模式（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 .count.warn b {
  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;
  }
}
