:root {
  --felt: #14532d;
  --felt-dark: #0b3a1f;
  --tile-face: #fdfcf7;
  --tile-edge: #d6d0bd;
  --ink: #1c1917;
  --gold: #d4a72c;
  --danger: #dc2626;
  --panel: rgba(0, 0, 0, .38);
}

/* ---- 淺色 theme ----
 * 約定：兩態、掛在 <html>、沒有「跟隨系統」。
 * ⚠️ 這裡**只動外框 UI 與牌桌**。牌面顏色（紅中／發財／花牌）刻意不在任何 theme 區塊裡，
 * 因為那是牌的身分，不是介面裝飾。
 */
:root[data-theme="light"] {
  --felt: #3f7d5a;
  --felt-dark: #2f6247;
  --panel: rgba(255, 255, 255, .18);
}

* { box-sizing: border-box; }

body {
  margin: 0;
  min-height: 100dvh;
  font-family: "PingFang TC", "Noto Sans TC", system-ui, sans-serif;
  background: radial-gradient(ellipse at center, var(--felt) 0%, var(--felt-dark) 100%);
  color: #f5f5f4;
  overflow: hidden;
}

/* ---- 牌 ---- */
.tile {
  /* 麻將牌用 Unicode 字元，字型缺字時 fallback 到牌名文字 */
  font-family: "Apple Symbols", "Segoe UI Symbol", "Noto Sans Symbols 2", sans-serif;
  font-variant-emoji: text;
  background: var(--tile-face);
  color: var(--ink);
  border: 1px solid var(--tile-edge);
  border-radius: 6px;
  line-height: 1;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  user-select: none;
  box-shadow: 0 2px 3px rgba(0, 0, 0, .35);
}

.tile.lg { font-size: 46px; width: 44px; height: 62px; padding-bottom: 4px; }
.tile.sm { font-size: 26px; width: 26px; height: 36px; padding-bottom: 2px; }
.tile.xs { font-size: 19px; width: 19px; height: 26px; }

.tile.back { background: linear-gradient(160deg, #1e6f43, #14512f); color: #14512f; border-color: #0d3d23; }

/* ---- 牌面顏色 ----
 * ⚠️ 這一段**刻意寫死，不使用任何 theme token，也不隨 theme 改變**。
 * 紅中是紅的、發財是綠的不是裝飾，是**牌的身分** —— 顏色錯了就是另一張牌。
 * （web-portal 2026-08-27 的約定：承載領域資訊的顏色不該被抹平。）
 *
 * 色盲可用性：牌面上本來就有字（中／發／花名），顏色只是加強，
 * **沒有任何一張牌只靠底色區分**。
 *
 * 只補傳統上真的有顏色的那幾張，其餘維持墨色。
 */
.tile[data-tile="45"] { color: #b91c1c; }              /* 中 */
.tile[data-tile="46"] { color: #15803d; }              /* 發 */
.tile[data-tile="31"] { color: #15803d; }              /* 一條（雀鳥）*/
.tile[data-tile="51"], .tile[data-tile="52"],
.tile[data-tile="53"], .tile[data-tile="54"] { color: #b45309; }  /* 春夏秋冬 */
.tile[data-tile="55"], .tile[data-tile="56"],
.tile[data-tile="57"], .tile[data-tile="58"] { color: #15803d; }  /* 梅蘭竹菊 */

/* 牌背蓋掉牌面顏色（別人的牌不該透出是哪一張）*/
.tile.back[data-tile] { color: #14512f; }

/* 手牌一定要排成一列 —— 17 張換行的話「摸進來的那張」就看不出來了 */
.hand {
  display: flex;
  flex-wrap: nowrap;
  gap: 3px;
  max-width: 100%;
  justify-content: center;
}
.hand .tile.lg { flex: 0 1 auto; min-width: 0; }

/* 自己的手牌可以點 */
.hand .tile.lg {
  cursor: pointer;
  transition: transform .12s, box-shadow .12s;
}
.hand.can-discard .tile.lg:hover { transform: translateY(-10px); box-shadow: 0 8px 14px rgba(0, 0, 0, .45); }
.hand .tile.lg.drawn { margin-left: 20px; box-shadow: 0 0 0 2px var(--gold), 0 2px 3px rgba(0,0,0,.35); }
.hand.locked .tile.lg { cursor: default; opacity: .75; }
.tile.wait { outline: 2px solid var(--gold); }

/* ---- 版面 ---- */
#table {
  display: grid;
  grid-template-areas: ". top ." "left center right" ". bottom .";
  grid-template-columns: minmax(90px, 1fr) 3fr minmax(90px, 1fr);
  grid-template-rows: auto 1fr auto;
  height: 100dvh;
  padding: 10px;
  gap: 8px;
}

#seat-top { grid-area: top; justify-self: center; }
#seat-left { grid-area: left; align-self: center; }
#seat-right { grid-area: right; align-self: center; }
#center { grid-area: center; display: flex; flex-direction: column; align-items: center; justify-content: center; gap: 8px; min-height: 0; }
#me { grid-area: bottom; display: flex; flex-direction: column; align-items: center; gap: 8px; }
#my-seat { display: flex; align-items: center; gap: 10px; font-size: 13px; }
#my-seat .turn-hint { color: var(--gold); font-weight: 600; }

.seat {
  background: var(--panel);
  border-radius: 10px;
  padding: 8px 10px;
  min-width: 120px;
  font-size: 13px;
}
.seat.turn { outline: 2px solid var(--gold); }
.seat .who { display: flex; align-items: center; gap: 6px; font-weight: 600; }
.seat .badge { font-size: 11px; padding: 1px 6px; border-radius: 999px; background: #00000055; font-weight: 400; }
.seat .badge.dealer { background: var(--gold); color: #1c1917; font-weight: 700; }
.seat .badge.off { background: var(--danger); }
.seat .score { font-variant-numeric: tabular-nums; opacity: .85; }
.seat .row { display: flex; flex-wrap: wrap; gap: 2px; margin-top: 4px; }
.meld { display: inline-flex; gap: 1px; padding: 2px; background: #00000033; border-radius: 4px; }
.meld.concealed { outline: 1px dashed #ffffff55; }

#discards {
  display: flex; flex-wrap: wrap; gap: 2px; justify-content: center; align-content: flex-start;
  width: min(560px, 100%); max-height: 200px; overflow-y: auto;
  background: #00000022; border-radius: 10px; padding: 8px; min-height: 54px;
}
/* 文字由 JS 用 data-empty 給，CSS 裡不寫死中文 */
#discards:empty::after { content: attr(data-empty); opacity: .35; font-size: 12px; }
.tile.just-discarded { outline: 2px solid var(--gold); }

#info { display: flex; gap: 14px; font-size: 13px; opacity: .9; align-items: center; }
#info b { font-weight: 600; }

/* ---- 動作 ---- */
#actions { display: flex; gap: 8px; flex-wrap: wrap; justify-content: center; min-height: 42px; align-items: center; }
button {
  font: inherit; font-weight: 600; padding: 9px 18px; border-radius: 8px;
  border: 1px solid #ffffff33; background: #ffffff1a; color: inherit; cursor: pointer;
}
button:hover:not(:disabled) { background: #ffffff2e; }
button:disabled { opacity: .4; cursor: not-allowed; }
button.primary { background: var(--gold); color: #1c1917; border-color: transparent; }
button.hu { background: var(--danger); border-color: transparent; }

#timer { height: 4px; width: 220px; background: #ffffff22; border-radius: 999px; overflow: hidden; }
#timer > div { height: 100%; background: var(--gold); transition: width .2s linear; }

/* ---- 覆蓋層 ---- */
.overlay {
  position: fixed; inset: 0; display: grid; place-items: center;
  background: #000000cc; backdrop-filter: blur(3px); z-index: 10; padding: 20px;
}
.overlay[hidden] { display: none; }
.card {
  background: #1c1917; border: 1px solid #ffffff1a; border-radius: 14px;
  padding: 26px; max-width: 420px; width: 100%;
}
.card h1 { margin: 0 0 6px; font-size: 21px; }
.card p { margin: 0 0 18px; opacity: .75; font-size: 14px; line-height: 1.6; }
.card label { display: block; font-size: 13px; margin-bottom: 6px; opacity: .8; }
.card input {
  font: inherit; width: 100%; padding: 10px 12px; border-radius: 8px;
  border: 1px solid #ffffff26; background: #00000055; color: inherit; margin-bottom: 14px;
}
.card .err { color: #fca5a5; font-size: 13px; min-height: 20px; margin: 0 0 10px; }
.card button { width: 100%; }

/* 紀錄放右上角 —— 放右下角會壓到手牌最後一張（實測踩過） */
#log { position: fixed; right: 10px; top: 10px; width: 230px; max-height: 150px; overflow-y: auto;
  font-size: 12px; background: var(--panel); border-radius: 8px; padding: 8px; line-height: 1.55; }
#log div { opacity: .85; }
#log div.hi { color: var(--gold); opacity: 1; }

/* 設定入口。⚠️ 牌桌上絕對不放語言切換器（會破壞沉浸感，dnd／atc 都是這樣）——
   只放一顆不起眼的齒輪，切換器收在設定區裡。 */
#btn-settings {
  position: fixed; left: 10px; top: 10px; z-index: 5;
  width: 34px; height: 34px; padding: 0; font-size: 16px; line-height: 1;
  border-radius: 999px; opacity: .5;
}
#btn-settings:hover { opacity: 1; }

.prefs { display: flex; gap: 8px; justify-content: center; margin-top: 16px; }
.prefs-row { display: flex; align-items: center; justify-content: space-between; gap: 12px; margin-bottom: 12px; font-size: 14px; }
/* ⚠️ 要蓋掉 .card button 的 width:100% —— 不然切換鈕會各佔一整行疊起來 */
.prefs button, .prefs-row button { width: auto; padding: 6px 14px; font-size: 13px; }
.prefs-row > span:last-child { display: inline-flex; gap: 6px; }
button.sel { background: var(--gold); color: #1c1917; border-color: transparent; }

/* 部署戳記：未登入也看得到，但不搶戲 */
#stamp {
  position: fixed; right: 8px; bottom: 6px; z-index: 30;
  font-size: 10px; opacity: .35; font-family: ui-monospace, SFMono-Regular, Menlo, monospace;
  pointer-events: none; letter-spacing: .02em;
}

#toast { position: fixed; top: 14px; left: 50%; transform: translateX(-50%);
  background: var(--danger); padding: 9px 16px; border-radius: 8px; font-size: 14px; z-index: 20; }
#toast[hidden] { display: none; }

/* 手牌 17 張 × (44 + 3) ≈ 800px，窄一點就要縮 */
@media (max-width: 900px) {
  .tile.lg { font-size: 38px; width: 36px; height: 52px; }
}
@media (max-width: 720px) {
  .tile.lg { font-size: 28px; width: 26px; height: 38px; }
  .hand { gap: 2px; }
  .hand .tile.lg.drawn { margin-left: 10px; }
  #table { grid-template-columns: 70px 1fr 70px; padding: 6px; }
  #log { display: none; }
}
