/*
 * 麻將16 - 奢華賭場版樣式
 * 深綠天鵝绒牌桌 + 胡桃木邊框 + 象牙白瓷牌
 * 響應式: 手機 / 平板 / 電腦
 */

/* ============================================
   字体導入
   ============================================ */
@import url('https://fonts.googleapis.com/css2?family=Noto+Sans+TC:wght@300;400;500;700;900&display=swap');

/* ============================================
   CSS 變數
   ============================================ */
:root {
  /* 牌桌天鵝绒深綠 */
  --felt-dark: #1a3d1a;
  --felt-green: #2d6a2d;
  --felt-light: #3d8a3d;
  --felt-highlight: #5cb85c;
  --felt-shine: #7fcc7f;

  /* 胡桃木邊框 */
  --wood-dark: #1a0f0a;
  --wood-mid: #3d2010;
  --wood-light: #6b3a1f;
  --wood-grain: #8b5a2b;
  --gold-accent: #d4a843;
  --gold-bright: #f0d078;

  /* 牌面象牙白 */
  --tile-cream: #f5f0e8;
  --tile-cream-dark: #e8e0d0;
  --tile-shadow: rgba(0, 0, 0, 0.5);
  --tile-border: #c8b89a;

  /* 字體顏色 */
  --text-bright: #ffffff;
  --text-gold: #f0d078;
  --text-muted: rgba(255, 255, 255, 0.65);

  /* 碰槓胡按鈕 */
  --btn-red: #c0392b;
  --btn-blue: #2980b9;
  --btn-gold: #d4a843;

  /* 磨砂玻璃 */
  --glass-bg: rgba(30, 80, 30, 0.85);
  --glass-border: rgba(255, 255, 255, 0.3);
  --glass-shadow: rgba(0, 0, 0, 0.3);

  /* 間距 */
  --sp-xs: 3px;
  --sp-sm: 8px;
  --sp-md: 16px;
  --sp-lg: 24px;
  --sp-xl: 32px;

  /* 圓角 */
  --r-sm: 4px;
  --r-md: 10px;
  --r-lg: 16px;
  --r-round: 50%;

  /* 動畫 */
  --ease-fast: 120ms ease;
  --ease-norm: 250ms ease;
  --ease-slow: 400ms ease;

  /* 字體 */
  --font-main: 'Noto Sans TC', 'PingFang TC', 'Microsoft JhengHei', sans-serif;
  --font-num: 'Roboto Mono', 'Consolas', monospace;
}

/* ============================================
   重置
   ============================================ */
*, *::before, *::after { margin: 0; padding: 0; box-sizing: border-box; }

html { font-size: 16px; -webkit-text-size-adjust: 100%; }

body {
  font-family: var(--font-main);
  color: var(--text-bright);
  min-height: 100dvh;
  overflow-x: hidden;
  margin: 0;
  background: #4a8a4a; /* 明亮背景 */
  /* 防止選取抖動 */
  -webkit-tap-highlight-color: transparent;
}

/* ============================================
   牌桌容器
   ============================================ */
.game-container {
  position: relative;
  width: 100%;
  min-height: 100dvh;
  display: flex;
  flex-direction: column;
  overflow: hidden;
}

/* ============================================
   牌桌背景 — 明亮版
   ============================================ */
.table-surface {
  position: fixed;
  inset: 0;
  z-index: 0;
  background: #5aba5a;
}

/* 木紋邊框 */
.table-border {
  position: fixed;
  inset: 0;
  z-index: 1;
  pointer-events: none;
  border: clamp(10px, 2.5vw, 24px) solid var(--wood-dark);
  box-shadow:
    inset 0 0 0 3px var(--wood-mid),
    inset 0 0 0 5px var(--wood-grain),
    inset 0 0 20px rgba(0,0,0,0.3),
    0 0 40px rgba(0,0,0,0.4);
}

/* 天鵝绒織紋疊加 */
.table-surface::before {
  content: '';
  position: absolute;
  inset: 0;
  background-image:
    repeating-linear-gradient(43deg, transparent, transparent 3px, rgba(255,255,255,0.012) 3px, rgba(255,255,255,0.012) 4px),
    repeating-linear-gradient(-43deg, transparent, transparent 3px, rgba(255,255,255,0.008) 3px, rgba(255,255,255,0.008) 4px);
  pointer-events: none;
}

/* 天鵝绒光澤 */
.table-surface::after {
  content: '';
  position: absolute;
  inset: 0;
  background:
    linear-gradient(178deg, rgba(255,255,255,0.07) 0%, transparent 18%),
    radial-gradient(ellipse 50% 30% at 20% 8%, rgba(255,255,255,0.05) 0%, transparent 60%);
  pointer-events: none;
}

/* ============================================
   木紋線 + 金線點綴
   ============================================ */
.table-border::before {
  content: '';
  position: absolute;
  inset: 0;
  background:
    repeating-linear-gradient(87deg, transparent, transparent 12px, rgba(107,58,31,0.25) 12px, rgba(107,58,31,0.25) 14px),
    repeating-linear-gradient(93deg, transparent, transparent 25px, rgba(139,90,43,0.18) 25px, rgba(139,90,43,0.18) 27px);
  pointer-events: none;
}

/* 金線點綴 */
.table-border::after {
  content: '';
  position: absolute;
  inset: -2px;
  border: 2px solid transparent;
  background:
    linear-gradient(var(--wood-dark), var(--wood-dark)) padding-box,
    linear-gradient(125deg, var(--gold-accent) 0%, transparent 25%, transparent 75%, var(--gold-accent) 100%) border-box;
  pointer-events: none;
  opacity: 0.6;
}

/* ============================================
   遊戲區域
   ============================================ */
.game-area {
  position: relative;
  z-index: 10;
  flex: 1;
  display: flex;
  flex-direction: column;
  min-height: 100dvh;
  padding: var(--sp-sm);
  gap: var(--sp-xs);
}

/* ============================================
   資訊列 (info-bar)
   ============================================ */
.info-bar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: var(--sp-sm);
  padding: var(--sp-sm) var(--sp-md);
  background: var(--glass-bg);
  backdrop-filter: blur(20px);
  -webkit-backdrop-filter: blur(20px);
  border: 1px solid var(--glass-border);
  border-radius: var(--r-md);
  box-shadow: 0 4px 24px var(--glass-shadow);
  flex-shrink: 0;
}

.info-item {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 2px;
}

.info-label {
  font-size: 0.65rem;
  color: var(--text-muted);
  text-transform: uppercase;
  letter-spacing: 0.08em;
}

.info-value {
  font-size: 1.1rem;
  font-weight: 700;
  color: var(--text-bright);
  font-family: var(--font-num);
  text-shadow: 0 1px 4px rgba(0,0,0,0.5);
}

.dealer-badge {
  background: linear-gradient(135deg, var(--btn-red), #e74c3c);
  color: #fff;
  padding: 2px 10px;
  border-radius: var(--r-sm);
  font-size: 0.9rem;
  box-shadow: 0 2px 8px rgba(192,57,43,0.5);
  font-family: var(--font-main);
}

.game-title {
  font-size: 1.1rem;
  font-weight: 900;
  color: var(--text-gold);
  text-shadow: 0 0 12px rgba(240,208,120,0.4), 0 2px 6px rgba(0,0,0,0.6);
  letter-spacing: 0.05em;
  white-space: nowrap;
}

.settings-btn {
  width: 38px;
  height: 38px;
  border-radius: var(--r-round);
  background: rgba(255,255,255,0.1);
  border: 1px solid var(--glass-border);
  color: var(--text-bright);
  font-size: 1.1rem;
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: center;
  transition: background var(--ease-fast), transform var(--ease-fast);
}

.settings-btn:hover {
  background: rgba(255,255,255,0.2);
  transform: rotate(30deg);
}

/* ============================================
   分數面板 (chip-panel)
   ============================================ */
.chip-panel {
  display: grid;
  grid-template-columns: repeat(4, 1fr);
  gap: var(--sp-xs);
  flex-shrink: 0;
}

.chip-item {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 2px;
  padding: var(--sp-xs) var(--sp-sm);
  background: var(--glass-bg);
  backdrop-filter: blur(12px);
  -webkit-backdrop-filter: blur(12px);
  border: 1px solid var(--glass-border);
  border-radius: var(--r-sm);
}

.chip-label {
  font-size: 0.6rem;
  color: var(--text-muted);
  font-weight: 500;
  letter-spacing: 0.05em;
}

.chip-value {
  font-size: 0.95rem;
  font-weight: 700;
  color: var(--text-gold);
  font-family: var(--font-num);
  text-shadow: 0 0 8px rgba(240,208,120,0.3);
}

/* ============================================
   桌面區 (table-area)
   ============================================ */
.table-area {
  flex: 1;
  display: grid;
  grid-template-rows: auto 1fr auto;
  gap: var(--sp-xs);
  min-height: 0;
}

/* AI 手牌 - 上方 */
.opponent-hand {
  position: relative;
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: var(--sp-xs);
  padding: var(--sp-xs);
}

.opponent-label {
  font-size: 0.65rem;
  color: var(--text-muted);
  font-weight: 600;
  letter-spacing: 0.05em;
}

.opponent-tiles {
  display: flex;
  justify-content: center;
  gap: 2px;
  flex-wrap: wrap;
}

/* 對面家的手牌 - 上方水平 */
.opponent-top .opponent-tiles { flex-direction: row; }

/* 左家和右家 - 垂直 */
.opponent-left, .opponent-right {
  display: none; /* 在較小螢幕先隱藏 */
}

/* 中間區域 */
.table-middle {
  display: grid;
  grid-template-columns: 1fr auto 1fr;
  align-items: center;
  gap: var(--sp-sm);
  min-height: 0;
}

/* ============================================
   中央區域
   ============================================ */
.table-center {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: var(--sp-xs);
  flex: 1;
  max-width: 320px;
  margin: 0 auto;
}

.discard-area {
  width: 100%;
  background: rgba(0,0,0,0.25);
  border: 1px solid rgba(255,255,255,0.08);
  border-radius: var(--r-md);
  padding: var(--sp-xs);
}

.discard-label {
  font-size: 0.6rem;
  color: var(--text-muted);
  text-align: center;
  margin-bottom: 2px;
  letter-spacing: 0.08em;
  text-transform: uppercase;
}

.discard-tiles {
  display: flex;
  flex-wrap: wrap;
  justify-content: center;
  gap: 2px;
  min-height: 32px;
}

.wall-info {
  font-size: 0.75rem;
  color: var(--text-muted);
  font-family: var(--font-num);
  background: rgba(0,0,0,0.3);
  padding: 2px 10px;
  border-radius: var(--r-sm);
  border: 1px solid rgba(255,255,255,0.06);
}

/* ============================================
   玩家手牌區 (hand-area)
   ============================================ */
.hand-area {
  flex-shrink: 0;
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: var(--sp-xs);
}

.hand-label {
  font-size: 0.65rem;
  color: var(--text-muted);
  letter-spacing: 0.06em;
  text-transform: uppercase;
}

.tiles-container {
  display: flex;
  justify-content: center;
  flex-wrap: nowrap;
  overflow-x: auto;
  overflow-y: hidden;
  gap: 3px;
  padding: var(--sp-sm);
  background: rgba(0,0,0,0.3);
  border: 1px solid rgba(255,255,255,0.08);
  border-radius: var(--r-md);
  min-height: 64px;
  max-width: 100%;
  -webkit-overflow-scrolling: touch;
  scrollbar-width: thin;
  scrollbar-color: rgba(255,255,255,0.3) transparent;
}

.meld-area {
  display: flex;
  justify-content: center;
  gap: 3px;
  min-height: 28px;
  flex-wrap: wrap;
}

/* ============================================
   SVG 瓷磚圖片樣式
   ============================================ */
.tile {
  /* 尺寸 - 響應式 */
  --tile-w: 42px;
  --tile-h: 58px;

  width: var(--tile-w);
  height: var(--tile-h);
  background: linear-gradient(160deg, #fefcf8 0%, var(--tile-cream) 40%, var(--tile-cream-dark) 100%);
  border-radius: var(--r-sm);
  border: 1px solid var(--tile-border);
  display: flex;
  align-items: center;
  justify-content: center;
  cursor: pointer;
  position: relative;
  flex-shrink: 0;
  user-select: none;
  overflow: hidden;
  /* 厚實陰影 - 營造深度 */
  box-shadow:
    0 6px 0 rgba(0,0,0,0.25),
    0 10px 20px rgba(0,0,0,0.45),
    0 3px 6px rgba(0,0,0,0.3),
    inset 0 1px 0 rgba(255,255,255,0.95);
  transition:
    transform var(--ease-fast),
    box-shadow var(--ease-fast);
  border-bottom-width: 3px;
}

/* 瓷磚 SVG 圖片 */
.tile-img {
  width: 100%;
  height: 100%;
  object-fit: contain;
  display: block;
  pointer-events: none;
}

/* 文字 fallback（SVG載入失敗時） */
.tile-text {
  font-family: var(--font-main);
  font-size: 1rem;
  font-weight: 700;
  color: #b52f2f;
}

/* 選中 / 摸到的牌 */
.tile:hover {
  transform: translateY(-6px);
  box-shadow:
    0 8px 0 rgba(0,0,0,0.2),
    0 12px 20px rgba(0,0,0,0.5),
    inset 0 1px 0 rgba(255,255,255,0.9),
    inset 0 -1px 0 rgba(0,0,0,0.08);
}

.tile.selected,
.tile.tile-selected {
  transform: translateY(-10px);
  box-shadow:
    0 10px 0 rgba(0,0,0,0.18),
    0 16px 28px rgba(0,0,0,0.55),
    0 0 0 3px var(--gold-accent),
    0 0 20px rgba(212,168,67,0.35),
    inset 0 1px 0 rgba(255,255,255,0.9);
}

.tile:focus {
  outline: none;
  box-shadow:
    0 6px 0 rgba(0,0,0,0.22),
    0 10px 20px rgba(0,0,0,0.4),
    0 0 0 3px #ff6b00,
    0 0 16px rgba(255,107,0,0.5) !important;
  transform: translateY(-6px);
  z-index: 10;
}

.tile:focus-visible {
  outline: 2px solid #ff6b00;
  outline-offset: 2px;
}

.tile.last-drawn,
.tile.tile-last-drawn {
  box-shadow:
    0 4px 0 rgba(0,0,0,0.25),
    0 6px 12px rgba(0,0,0,0.45),
    0 0 0 2px var(--felt-highlight),
    0 0 14px rgba(74,153,101,0.5),
    inset 0 1px 0 rgba(255,255,255,0.9);
  animation: lastDrawnPulse 1.5s ease-in-out infinite;
}

@keyframes lastDrawnPulse {
  0%, 100% { box-shadow: 0 4px 0 rgba(0,0,0,0.25), 0 6px 12px rgba(0,0,0,0.45), 0 0 0 2px var(--felt-highlight), 0 0 14px rgba(74,153,101,0.5), inset 0 1px 0 rgba(255,255,255,0.9); }
  50% { box-shadow: 0 4px 0 rgba(0,0,0,0.25), 0 6px 12px rgba(0,0,0,0.45), 0 0 0 2px var(--felt-shine), 0 0 24px rgba(74,153,101,0.7), inset 0 1px 0 rgba(255,255,255,0.9); }
}

/* ============================================
   牌背面 (.tile-back) - 使用 SVG 圖片
   ============================================ */
.tile-back {
  width: var(--tile-w);
  height: var(--tile-h);
  border-radius: var(--r-sm);
  background: repeating-linear-gradient(45deg, #5cb85c 0px, #5cb85c 6px, #3d8a3d 6px, #3d8a3d 12px);
  border: 1px solid #7fcc7f;
  box-shadow:
    0 3px 0 rgba(0,0,0,0.2),
    0 5px 10px rgba(0,0,0,0.25),
    inset 0 1px 0 rgba(255,255,255,0.2);
  flex-shrink: 0;
  overflow: hidden;
  display: flex;
  align-items: center;
  justify-content: center;
}

.tile-back .tile-img {
  width: 100%;
  height: 100%;
  object-fit: cover;
}

/* 小牌 (捨牌區) */
.tile-small {
  width: 30px;
  height: 41px;
  border-radius: 3px;
  box-shadow:
    0 2px 0 rgba(0,0,0,0.2),
    0 3px 6px rgba(0,0,0,0.35),
    inset 0 1px 0 rgba(255,255,255,0.9);
  flex-shrink: 0;
  overflow: hidden;
  display: flex;
  align-items: center;
  justify-content: center;
  background: linear-gradient(160deg, #fefcf8 0%, var(--tile-cream) 40%, var(--tile-cream-dark) 100%);
  border: 1px solid var(--tile-border);
}

.tile-img-small {
  width: 100%;
  height: 100%;
  object-fit: contain;
  display: block;
}

/* 碰/槓的面子展示 */
.meld-area .tile {
  width: 28px;
  height: 38px;
  font-size: 0.75rem;
}

/* AI 手牌 - 背面顯示 */
.opponent-tiles .tile-back {
  width: 30px;
  height: 42px;
}

/* ============================================
   操作區 (action-area)
   ============================================ */
.action-area {
  flex-shrink: 0;
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: var(--sp-sm);
  padding: var(--sp-md);
  background: rgba(30, 90, 30, 0.92);
  border-top: 2px solid rgba(100, 200, 100, 0.3);
  position: relative;
  z-index: 20;
}

.game-status {
  font-size: 0.8rem;
  color: var(--text-muted);
  text-align: center;
  padding: var(--sp-xs) var(--sp-md);
  background: var(--glass-bg);
  backdrop-filter: blur(12px);
  border: 1px solid var(--glass-border);
  border-radius: var(--r-sm);
  min-height: 28px;
  display: flex;
  align-items: center;
  justify-content: center;
  width: 100%;
  max-width: 400px;
}

.action-buttons {
  display: flex;
  justify-content: center;
  gap: var(--sp-sm);
  flex-wrap: wrap;
}

/* 按鈕基礎 */
.action-btn {
  padding: var(--sp-sm) var(--sp-md);
  border-radius: var(--r-md);
  font-family: var(--font-main);
  font-size: 0.875rem;
  font-weight: 700;
  cursor: pointer;
  border: none;
  transition:
    transform var(--ease-fast),
    box-shadow var(--ease-fast),
    opacity var(--ease-fast);
  letter-spacing: 0.03em;
  white-space: nowrap;
}

.action-btn:disabled {
  opacity: 0.45;
  cursor: not-allowed;
  transform: none !important;
  filter: saturate(0.3) brightness(0.7);
}

.action-btn:not(:disabled):hover {
  transform: translateY(-2px);
}

.action-btn:not(:disabled):active {
  transform: translateY(0px) scale(0.97);
}

/* 開始 - 超高對比亮色版 */
.btn-start {
  background: linear-gradient(135deg, #ffe066, #ffcc00) !important;
  color: #1a0500 !important;
  box-shadow: 0 4px 16px rgba(255,200,0,0.6), 0 0 30px rgba(255,200,0,0.3);
  font-size: 1.1rem !important;
  font-weight: 900 !important;
  padding: 12px 28px !important;
  letter-spacing: 0.05em;
  border: 2px solid rgba(255,255,255,0.4);
  border-radius: 10px;
}
.btn-start:not(:disabled):hover {
  background: linear-gradient(135deg, #fff070, #ffd010) !important;
  box-shadow: 0 6px 24px rgba(255,210,0,0.8), 0 0 40px rgba(255,210,0,0.4);
  color: #0d0300 !important;
}
.btn-start:disabled {
  background: linear-gradient(135deg, #888, #666) !important;
  color: #ccc !important;
  opacity: 0.7 !important;
}

/* 摸牌 */
.btn-draw {
  background: linear-gradient(135deg, #3d7a52, #2d5a3d) !important;
  color: #fff !important;
  border: 1px solid rgba(74,153,101,0.5);
}
.btn-draw:not(:disabled) {
  box-shadow: 0 4px 12px rgba(45,90,61,0.5);
}
.btn-draw:not(:disabled):hover {
  box-shadow: 0 6px 18px rgba(45,90,61,0.6);
}

/* 出牌 */
.btn-discard {
  background: linear-gradient(135deg, #8b6914, #6b4f10) !important;
  color: #fff !important;
  border: 1px solid rgba(212,168,67,0.5);
}
.btn-discard:not(:disabled) {
  box-shadow: 0 4px 12px rgba(107,79,16,0.5);
}
.btn-discard:not(:disabled):hover {
  background: linear-gradient(135deg, #9b7924, #7b5f20) !important;
}

/* 碰 */
.btn-pon {
  background: linear-gradient(135deg, #d44040, var(--btn-red));
  color: #fff;
  box-shadow: 0 4px 12px rgba(192,57,43,0.45);
}
.btn-pon:not(:disabled):hover {
  box-shadow: 0 6px 18px rgba(192,57,43,0.6);
}

/* 槓 */
.btn-kan {
  background: linear-gradient(135deg, var(--btn-blue), #1a5f8a);
  color: #fff;
  box-shadow: 0 4px 12px rgba(41,128,185,0.45);
}
.btn-kan:not(:disabled):hover {
  box-shadow: 0 6px 18px rgba(41,128,185,0.6);
}

/* 胡 */
.btn-hu {
  background: linear-gradient(135deg, var(--gold-accent), #b8902f);
  color: #1a0f0a;
  box-shadow: 0 4px 12px rgba(212,168,67,0.5);
  font-size: 1rem;
}
.btn-hu:not(:disabled):hover {
  box-shadow: 0 6px 20px rgba(212,168,67,0.65);
}

/* AI分析 */
.btn-ai {
  background: rgba(255,255,255,0.1);
  border: 1px solid var(--glass-border);
  color: var(--text-bright);
  box-shadow: 0 2px 8px rgba(0,0,0,0.25);
}
.btn-ai:not(:disabled):hover {
  background: rgba(255,255,255,0.18);
}

/* ============================================
   設定面板
   ============================================ */
.settings-overlay {
  position: fixed;
  inset: 0;
  background: rgba(0,0,0,0.65);
  backdrop-filter: blur(4px);
  z-index: 998;
  opacity: 0;
  visibility: hidden;
  transition: opacity var(--ease-norm), visibility var(--ease-norm);
}
.settings-overlay.visible { opacity: 1; visibility: visible; }

.settings-panel {
  position: fixed;
  top: 50%;
  left: 50%;
  transform: translate(-50%, -50%) scale(0.9);
  width: min(380px, 95vw);
  z-index: 999;
  background: rgba(10,20,14,0.96);
  backdrop-filter: blur(24px);
  border: 1px solid var(--glass-border);
  border-radius: var(--r-lg);
  box-shadow: 0 20px 60px rgba(0,0,0,0.7);
  padding: var(--sp-xl) var(--sp-lg);
  opacity: 0;
  visibility: hidden;
  transition: opacity var(--ease-norm), visibility var(--ease-norm), transform var(--ease-norm);
}
.settings-panel.open {
  opacity: 1;
  visibility: visible;
  transform: translate(-50%, -50%) scale(1);
}
.settings-panel h2 {
  font-size: 1.2rem;
  font-weight: 700;
  color: var(--text-gold);
  margin-bottom: var(--sp-lg);
  text-align: center;
  letter-spacing: 0.05em;
}
.setting-group {
  margin-bottom: var(--sp-md);
}
.setting-group label {
  display: block;
  font-size: 0.8rem;
  color: var(--text-muted);
  margin-bottom: var(--sp-xs);
}
.setting-group input,
.setting-group select {
  width: 100%;
  padding: var(--sp-sm) var(--sp-md);
  background: rgba(255,255,255,0.08);
  border: 1px solid rgba(255,255,255,0.15);
  border-radius: var(--r-sm);
  color: var(--text-bright);
  font-family: var(--font-main);
  font-size: 0.9rem;
  outline: none;
  transition: border-color var(--ease-fast);
}
.setting-group input:focus,
.setting-group select:focus {
  border-color: var(--gold-accent);
}
.setting-actions {
  display: flex;
  gap: var(--sp-sm);
  margin-top: var(--sp-lg);
}

/* ============================================
   提示訊息
   ============================================ */
.message-toast {
  position: fixed;
  bottom: 30%;
  left: 50%;
  transform: translateX(-50%) translateY(20px);
  background: rgba(10,20,14,0.92);
  backdrop-filter: blur(16px);
  border: 1px solid var(--gold-accent);
  border-radius: var(--r-md);
  padding: var(--sp-sm) var(--sp-lg);
  font-size: 0.9rem;
  color: var(--text-bright);
  z-index: 500;
  opacity: 0;
  visibility: hidden;
  transition: opacity var(--ease-norm), visibility var(--ease-norm), transform var(--ease-norm);
  text-align: center;
  max-width: 90vw;
  box-shadow: 0 8px 30px rgba(0,0,0,0.5);
  pointer-events: none;
}
.message-toast.show {
  opacity: 1;
  visibility: visible;
  transform: translateX(-50%) translateY(0);
}

/* ============================================
   AI 分析面板
   ============================================ */
.analysis-panel {
  position: fixed;
  bottom: 0;
  left: 0;
  right: 0;
  z-index: 400;
  background: rgba(10,18,12,0.97);
  backdrop-filter: blur(20px);
  border-top: 1px solid var(--glass-border);
  border-radius: var(--r-lg) var(--r-lg) 0 0;
  padding: var(--sp-lg);
  transform: translateY(100%);
  transition: transform var(--ease-slow);
  max-height: 60vh;
  overflow-y: auto;
}
.analysis-panel.show { transform: translateY(0); }
.analysis-panel h3 {
  font-size: 1rem;
  color: var(--text-gold);
  margin-bottom: var(--sp-md);
  letter-spacing: 0.05em;
}
#analysisContent { font-size: 0.85rem; color: var(--text-muted); line-height: 1.6; }
#btnCloseAnalysis {
  margin-top: var(--sp-md);
  width: 100%;
}

/* ============================================
   Chi 選擇面板
   ============================================ */
.chi-overlay {
  position: fixed;
  inset: 0;
  background: rgba(0, 0, 0, 0.7);
  display: flex;
  align-items: center;
  justify-content: center;
  z-index: 200;
  opacity: 0;
  visibility: hidden;
  transition: opacity 0.2s, visibility 0.2s;
}
.chi-overlay.visible {
  opacity: 1;
  visibility: visible;
}
.chi-panel {
  background: linear-gradient(145deg, #1a2e1a 0%, #0d1f0d 100%);
  border: 2px solid var(--gold);
  border-radius: 16px;
  padding: var(--sp-lg);
  width: min(340px, 90vw);
  box-shadow: 0 8px 32px rgba(0,0,0,0.6), 0 0 24px rgba(201,168,76,0.15);
}
.chi-title {
  text-align: center;
  font-size: 1.1rem;
  color: var(--text-gold);
  margin-bottom: var(--sp-md);
  letter-spacing: 0.05em;
}
.chi-options {
  display: flex;
  flex-direction: column;
  gap: var(--sp-sm);
  margin-bottom: var(--sp-md);
}
.chi-option-btn {
  background: linear-gradient(135deg, #2a4a2a 0%, #1a3a1a 100%);
  border: 1px solid rgba(201,168,76,0.4);
  border-radius: 10px;
  padding: var(--sp-sm) var(--sp-md);
  color: var(--text-light);
  font-size: 1.1rem;
  font-family: inherit;
  cursor: pointer;
  transition: all 0.15s;
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 6px;
}
.chi-option-btn:hover {
  background: linear-gradient(135deg, #3a6a3a 0%, #2a4a2a 100%);
  border-color: var(--gold);
  transform: translateY(-1px);
}
#btnChiCancel {
  width: 100%;
}

/* ============================================
   響應式 - 手機優先
   ============================================ */
@media (max-width: 480px) {
  .game-area { padding: var(--sp-xs); gap: 2px; }

  .info-bar {
    padding: var(--sp-xs) var(--sp-sm);
    gap: var(--sp-xs);
  }
  .info-value { font-size: 0.95rem; }
  .game-title { font-size: 0.95rem; }

  .chip-panel {
    grid-template-columns: repeat(4, 1fr);
    gap: 2px;
  }
  .chip-item { padding: 2px 4px; }
  .chip-value { font-size: 0.85rem; }
  .chip-label { font-size: 0.55rem; }

  .tile {
    --tile-w: 32px;
    --tile-h: 44px;
    --tile-font: 0.95rem;
    border-bottom-width: 2px;
    box-shadow:
      0 3px 0 rgba(0,0,0,0.25),
      0 4px 8px rgba(0,0,0,0.4),
      inset 0 1px 0 rgba(255,255,255,0.9),
      inset 0 -1px 0 rgba(0,0,0,0.08);
  }
  .tile:hover {
    transform: translateY(-4px);
    box-shadow:
      0 6px 0 rgba(0,0,0,0.2),
      0 8px 14px rgba(0,0,0,0.45),
      inset 0 1px 0 rgba(255,255,255,0.9);
  }
  .tile.selected {
    transform: translateY(-8px);
    box-shadow:
      0 8px 0 rgba(0,0,0,0.18),
      0 10px 18px rgba(0,0,0,0.5),
      0 0 0 2px var(--gold-accent),
      0 0 14px rgba(212,168,67,0.35),
      inset 0 1px 0 rgba(255,255,255,0.9);
  }

  .tiles-container {
    padding: var(--sp-xs);
    gap: 2px;
    min-height: 50px;
  }

  .opponent-tiles .tile-back {
    width: 26px;
    height: 36px;
  }

  .tile-small {
    width: 24px;
    height: 32px;
    font-size: 0.7rem;
  }

  .action-btn {
    padding: 10px 14px;
    font-size: 0.9rem;
    min-height: 44px;
    min-width: 52px;
  }

  /* 緊急按鈕（碰/吃/槓/胡）要更大 */
  .btn-pon, .btn-chi, .btn-kan, .btn-hu {
    min-height: 52px !important;
    min-width: 60px !important;
    font-size: 1rem !important;
    font-weight: 900 !important;
    letter-spacing: 0.05em;
  }

  .btn-hu {
    background: linear-gradient(135deg, #e74c3c, #c0392b) !important;
    animation: huPulse 1.5s ease-in-out infinite;
  }

  @keyframes huPulse {
    0%, 100% { box-shadow: 0 0 0 0 rgba(231,76,60,0.4); }
    50% { box-shadow: 0 0 0 8px rgba(231,76,60,0); }
  }

  .chi-panel {
    width: min(300px, 92vw);
    padding: var(--sp-md);
  }

  .chi-option-btn {
    padding: 12px 16px;
    font-size: 1.2rem;
    min-height: 52px;
  }
}

/* 平板 */
@media (min-width: 481px) and (max-width: 768px) {
  .tile {
    --tile-w: 36px;
    --tile-h: 50px;
    --tile-font: 1.05rem;
  }
}

/* 桌面 */
@media (min-width: 769px) {
  .tile {
    --tile-w: 42px;
    --tile-h: 58px;
    --tile-font: 1.2rem;
  }
}

/* 超大螢幕 */
@media (min-width: 1200px) {
  .tile {
    --tile-w: 48px;
    --tile-h: 66px;
    --tile-font: 1.35rem;
  }
  .game-area { padding: var(--sp-md); }
}

/* ============================================
   淡入動畫
   ============================================ */
@keyframes fadeInUp {
  from { opacity: 0; transform: translateY(12px); }
  to { opacity: 1; transform: translateY(0); }
}

.tile {
  animation: fadeInUp 0.2s ease-out backwards;
}
.tiles-container .tile:nth-child(n) { animation-delay: calc(n * 0.02s); }

/* ============================================
   CASINO UI OVERHAUL — New Layout
   ============================================ */

.game-area {
  position: relative;
  z-index: 10;
  flex: 1;
  display: flex;
  flex-direction: column;
  padding: 8px;
  gap: 6px;
  min-height: 0;
}

.opponent-row {
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 12px;
  padding: 4px 8px;
}

.opponent-tiles-h {
  display: flex;
  flex-direction: row;
  gap: 2px;
  justify-content: center;
  flex-wrap: wrap;
}

.opponent-tiles-h .tile-back {
  width: 24px;
  height: 32px;
}

.table-middle {
  display: grid;
  grid-template-columns: 1fr auto 1fr;
  align-items: center;
  gap: 8px;
  padding: 0 4px;
}

.opponent-col {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 6px;
}

.opponent-tiles-v {
  display: flex;
  flex-direction: column;
  gap: 2px;
  align-items: center;
}

.opponent-tiles-v .tile-back {
  width: 24px;
  height: 32px;
}

.opponent-meld {
  display: flex;
  flex-wrap: wrap;
  gap: 2px;
  justify-content: center;
  min-height: 24px;
}

.table-discard-row {
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 16px;
  padding: 4px;
}

.discard-area {
  background: rgba(0,0,0,0.2);
  border: 1px solid rgba(255,255,255,0.1);
  border-radius: 8px;
  padding: 6px 12px;
  min-width: 120px;
}

.discard-label {
  font-size: 0.6rem;
  color: rgba(255,255,255,0.5);
  text-align: center;
  margin-bottom: 4px;
}

.discard-tiles {
  display: flex;
  flex-wrap: wrap;
  gap: 2px;
  justify-content: center;
  min-height: 32px;
}

.wall-panel {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 2px;
  background: rgba(0,0,0,0.25);
  border: 1px solid rgba(255,255,255,0.1);
  border-radius: 8px;
  padding: 8px 16px;
}

.wall-label {
  font-size: 0.55rem;
  color: rgba(255,255,255,0.4);
  text-transform: uppercase;
  letter-spacing: 0.1em;
}

.wall-count {
  font-family: var(--font-num);
  font-size: 1.4rem;
  font-weight: 700;
  color: #00d4ff;
  text-shadow: 0 0 8px rgba(0,212,255,0.5);
  line-height: 1;
}

.wall-unit {
  font-size: 0.55rem;
  color: rgba(255,255,255,0.4);
}

.hand-row {
  display: flex;
  align-items: center;
  gap: 12px;
  padding: 6px 12px;
  background: rgba(0,0,0,0.15);
  border-radius: 12px 12px 0 0;
  border-top: 1px solid rgba(212,168,67,0.2);
}

/* Central Widget */
.central-widget {
  display: flex;
  align-items: center;
  justify-content: center;
  position: relative;
}

.widget-frame {
  width: clamp(80px, 16vw, 130px);
  height: clamp(80px, 16vw, 130px);
  background: linear-gradient(145deg, #2d2d2d, #1c1c1c);
  border-radius: 14px;
  border: 3px solid #555;
  box-shadow: 0 6px 24px rgba(0,0,0,0.7), inset 0 1px 0 rgba(255,255,255,0.12), inset 0 -2px 0 rgba(0,0,0,0.4);
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 2px;
  position: relative;
}

.direction-labels {
  position: absolute;
  inset: 0;
  pointer-events: none;
}

.dir {
  position: absolute;
  font-size: clamp(0.45rem, 1.8vw, 0.7rem);
  font-weight: 700;
  color: rgba(255,255,255,0.35);
  font-family: 'Noto Sans TC', sans-serif;
  transition: color 0.3s, text-shadow 0.3s;
}

.dir-n { top: 5px; left: 50%; transform: translateX(-50%); }
.dir-s { bottom: 5px; left: 50%; transform: translateX(-50%); }
.dir-e { right: 5px; top: 50%; transform: translateY(-50%); }
.dir-w { left: 5px; top: 50%; transform: translateY(-50%); }

.dir.active {
  color: var(--gold-accent);
  text-shadow: 0 0 10px rgba(212,168,67,0.8), 0 0 20px rgba(212,168,67,0.4);
}

.led-timer {
  font-family: 'Roboto Mono', 'Consolas', monospace;
  font-size: clamp(1.3rem, 4.5vw, 2.2rem);
  font-weight: 700;
  color: var(--gold-bright);
  text-shadow: 0 0 14px rgba(240,208,120,0.8), 0 0 28px rgba(240,208,120,0.4);
  letter-spacing: 0.03em;
  line-height: 1;
}

.widget-subtitle {
  font-size: clamp(0.4rem, 1.3vw, 0.6rem);
  color: rgba(255,255,255,0.6);
  text-shadow: 0 1px 3px rgba(0,0,0,0.9);
  white-space: nowrap;
}

/* Player Avatars */
.player-avatar {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 3px;
  background: rgba(10,20,15,0.75);
  backdrop-filter: blur(10px);
  border: 1px solid rgba(212,168,67,0.25);
  border-radius: 10px;
  padding: 7px 10px;
  min-width: 65px;
}

.avatar-ai { border-color: rgba(255,255,255,0.1); }
.avatar-player { border-color: rgba(212,168,67,0.4); }

.avatar-icon {
  font-size: 1.8rem;
  line-height: 1;
}

.avatar-name {
  font-size: 0.65rem;
  font-weight: 700;
  color: var(--text-gold);
}

.avatar-score {
  font-size: 0.8rem;
  font-weight: 700;
  color: #fff;
  font-family: var(--font-num);
  text-shadow: 0 1px 4px rgba(0,0,0,0.6);
}

.avatar-tiles-count {
  font-size: 0.55rem;
  color: rgba(255,255,255,0.5);
}

.avatar-tiles-count span {
  color: #00d4ff;
  font-weight: 700;
}

/* Listen Badge */
.listen-badge {
  position: absolute;
  top: -10px;
  right: -10px;
  background: linear-gradient(135deg, #ff6b00, #ff4500);
  color: #fff;
  font-size: 0.65rem;
  font-weight: 900;
  padding: 3px 8px;
  border-radius: 20px;
  box-shadow: 0 0 12px rgba(255,69,0,0.6);
  animation: pulse-listen 1.2s ease-in-out infinite;
  z-index: 20;
}

@keyframes pulse-listen {
  0%, 100% { box-shadow: 0 0 12px rgba(255,69,0,0.6); }
  50% { box-shadow: 0 0 20px rgba(255,69,0,0.9), 0 0 30px rgba(255,69,0,0.4); }
}

/* Meld type labels */
.meld-type {
  font-size: 0.55rem;
  color: var(--text-gold);
  background: rgba(0,0,0,0.4);
  padding: 1px 4px;
  border-radius: 3px;
  margin-left: 2px;
}

.meld-separator {
  width: 100%;
  height: 3px;
}

/* Discard last marker */
.discard-last-marker {
  position: absolute;
  top: -8px;
  left: 50%;
  transform: translateX(-50%);
  width: 0;
  height: 0;
  border-left: 5px solid transparent;
  border-right: 5px solid transparent;
  border-bottom: 8px solid #ff6b00;
}

/* ================================================
   Landscape Mobile CSS — 橫向手機
   策略：game-area 保持 column，手牌固定在底部中央
   ================================================ */
@media (max-height: 580px), (orientation: landscape) {
  /* 重置全域 */
  html, body {
    height: 100%;
    overflow: hidden;
    margin: 0 !important;
    padding: 0 !important;
  }

  /* 遊戲容器：固定充滿 viewport */
  .game-container {
    height: 100vh !important;
    width: 100vw !important;
    overflow: hidden;
  }

  /* game-area: 保持 column stack，置中 */
  .game-area {
    display: flex !important;
    flex-direction: column !important;
    justify-content: space-between;
    flex: 1 1 0;
    min-height: 0 !important;
    overflow: hidden;
    padding: 0 !important;
  }

  /* info-bar: 極壓 */
  .info-bar {
    flex-shrink: 0;
    padding: 2px 8px !important;
    min-height: 28px;
    font-size: 0.7rem !important;
    background: rgba(90,170,90,0.85) !important;
  }

  /* AI-3 頂部列：顯示，但壓縮 */
  .opponent-top {
    flex-shrink: 0;
    display: flex !important;
    justify-content: center;
    gap: 4px;
    padding: 2px 4px;
    background: rgba(70,150,70,0.7);
    min-height: 40px;
  }

  .opponent-top .player-avatar {
    transform: scale(0.7);
    transform-origin: center;
  }

  .opponent-top .opponent-tiles-h {
    transform: scale(0.6);
    transform-origin: center;
  }

  /* 中間區域：row 排列 */
  .table-middle {
    display: flex !important;
    flex-direction: row !important;
    flex: 1 1 0;
    min-height: 0;
    overflow: hidden;
    align-items: stretch;
  }

  /* 對手：隱藏（節省空間） */
  .opponent-left,
  .opponent-right {
    display: none !important;
  }

  /* 中央墊：明亮背景 */
  .central-widget {
    flex: 1 1 0;
    display: flex;
    justify-content: center;
    align-items: center;
    min-height: 0;
    background: #6abe6a !important;
  }

  /* 牌池 + 牆：水平排列在中央上方 */
  .table-discard-row {
    flex-shrink: 0;
    display: flex !important;
    flex-direction: row !important;
    justify-content: center !important;
    align-items: center !important;
    gap: 12px;
    padding: 2px 8px;
    min-height: 44px;
    background: rgba(80,160,80,0.7) !important;
  }

  .discard-area {
    background: rgba(255,255,255,0.15) !important;
    border: 1px solid rgba(255,255,255,0.25) !important;
    border-radius: 8px;
    padding: 2px 8px;
  }

  .wall-panel {
    background: rgba(255,255,255,0.15) !important;
    border: 1px solid rgba(255,255,255,0.25) !important;
    border-radius: 8px;
    padding: 2px 8px;
  }

  /* 玩家手牌區：水平排列，置中 */
  .hand-row {
    flex-shrink: 0;
    display: flex !important;
    flex-direction: row !important;
    align-items: flex-end !important;
    justify-content: center !important;
    gap: 4px;
    padding: 2px 8px 4px !important;
    min-height: 60px;
    background: rgba(80,160,80,0.8) !important;
  }

  .hand-row .player-avatar {
    display: none !important;
  }

  .meld-area {
    flex-shrink: 0;
  }

  /* 手牌 tiles：置中，可橫向滾動 */
  .tiles-container {
    display: flex !important;
    flex-wrap: nowrap !important;
    overflow-x: auto !important;
    overflow-y: hidden !important;
    justify-content: flex-start !important;
    align-items: flex-end !important;
    gap: 2px;
    padding: 2px 4px !important;
    min-height: 0;
    max-height: none !important;
    -webkit-overflow-scrolling: touch;
  }

  /* Tile 尺寸 */
  .tile {
    flex: 0 0 auto !important;
    --tile-w: 42px;
    --tile-h: 58px;
    width: var(--tile-w) !important;
    height: var(--tile-h) !important;
  }

  /* 動作區：固定可見 */
  .action-area {
    flex-shrink: 0 !important;
    background: rgba(60,130,60,0.95) !important;
    border-top: 2px solid rgba(120,220,120,0.4) !important;
    padding: 3px 6px !important;
    max-height: 90px;
    overflow: visible;
  }

  .game-status {
    font-size: 0.7rem !important;
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
  }

  .action-buttons {
    flex-wrap: wrap !important;
    gap: 4px !important;
    justify-content: center;
  }

  .action-btn {
    font-size: 0.75rem !important;
    padding: 5px 10px !important;
    min-width: 44px !important;
    min-height: 34px !important;
  }
}

/* ================================================
   Mobile Responsive - 確保按鈕區可見
   核心策略：遊戲區 max-height 限制 + 各區塊 flex-shrink
   ================================================ */
@media (max-width: 600px) {
  /* 遊戲容器：固定高度，超過就overflow */
  .game-container {
    height: 100dvh;
    min-height: 0;
    overflow: hidden;
  }

  /* 遊戲區域：flex-shrink: 1，讓它可以被壓縮 */
  .game-area {
    flex: 1 1 0 !important;
    min-height: 0 !important;
    overflow: hidden !important;
    display: flex;
    flex-direction: column;
    padding: 2px 4px !important;
    gap: 2px;
    /* 關鍵：max-height 限制 */
    max-height: calc(100dvh - 130px) !important;
  }

  /* 資訊列：極度壓縮 */
  .info-bar {
    flex-shrink: 0;
    padding: 2px 8px !important;
    font-size: 0.7rem !important;
    min-height: 24px;
    gap: 4px;
  }

  .info-value {
    font-size: 0.7rem !important;
  }

  /* 對手區：垂直壓縮 */
  .opponent-row {
    flex-shrink: 0;
  }

  .opponent-area {
    padding: 2px 4px !important;
  }

  .opponent-tiles {
    transform: scale(0.6) !important;
    transform-origin: center;
  }

  .opponent-avatar {
    width: 28px !important;
    height: 28px !important;
    font-size: 0.8rem !important;
  }

  .opponent-name,
  .opponent-score {
    font-size: 0.5rem !important;
  }

  .avatar-score,
  .avatar-tiles-count {
    font-size: 0.5rem !important;
  }

  /* 中央墊區：壓縮到最小 */
  .center-widget {
    flex-shrink: 0;
    transform: scale(0.5) !important;
    transform-origin: center;
    margin: 0 !important;
    min-height: 0;
  }

  .round-info,
  .wall-info {
    font-size: 0.6rem !important;
  }

  /* 玩家手牌區：flex-shrink: 1 */
  .player-area {
    flex-shrink: 1;
    min-height: 0;
    padding: 2px 4px 4px !important;
    overflow: hidden;
  }

  /* 手牌容器：可橫向滾動，設 max-height */
  .tiles-container {
    overflow-x: auto !important;
    overflow-y: hidden !important;
    -webkit-overflow-scrolling: touch;
    justify-content: flex-start;
    gap: 2px;
    padding: 2px 4px !important;
    max-height: 90px !important;
    min-height: 50px;
  }

  /* 手牌：使用 media query 的 tile 尺寸 */
  .tile {
    flex: 0 0 auto;
    /* tile CSS 變數在各自的 media query 中定義 */
  }

  /* 按鈕區：固定在底部，永遠可見 */
  .action-area {
    flex-shrink: 0 !important;
    position: relative !important;
    z-index: 20;
    background: rgba(30, 90, 30, 0.97) !important;
    border-top: 2px solid rgba(100, 200, 100, 0.4);
    padding: 4px 6px !important;
    max-height: 130px;
    overflow: visible;
  }

  .game-status {
    font-size: 0.7rem !important;
    padding: 1px 0 3px;
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
  }

  .action-buttons {
    flex-wrap: wrap !important;
    gap: 4px !important;
    justify-content: center;
  }

  .action-btn {
    font-size: 0.75rem !important;
    padding: 5px 10px !important;
    min-width: 44px !important;
    min-height: 34px !important;
  }

  /* 緊急按鈕稍大 */
  .btn-pon, .btn-chi, .btn-kan, .btn-hu {
    min-height: 38px !important;
    min-width: 48px !important;
    font-size: 0.8rem !important;
  }

  /* 吃牌面板在手機上 */
  .chi-panel {
    width: 95vw !important;
    max-width: 320px;
  }
}
