:root {
  --bg: #0f1a14;
  --felt: #1d4a32;
  --felt-dark: #163a27;
  --panel: #18231d;
  --text: #e8efe9;
  --muted: #9fb3a6;
  --accent: #f2c14e;
  --good: #5fd38d;
  --bad: #ff7a6b;
  --tile: #fbf7ec;
  --tile-edge: #cfc6ad;
  --tile-text: #1b1b1b;
  --aka: #d22f2f;
  --border: #2c3d33;
}
* { box-sizing: border-box; }
body {
  margin: 0; background: var(--bg); color: var(--text); touch-action: manipulation;
  font-family: "Segoe UI", "Microsoft JhengHei", "Noto Sans TC", sans-serif; font-size: 14px;
}
header {
  display: flex; align-items: center; justify-content: space-between; flex-wrap: wrap; gap: 8px;
  padding: 8px 16px; border-bottom: 1px solid var(--border); background: #111c16;
}
header h1 { font-size: 18px; margin: 0; }
header h1 small { color: var(--muted); font-weight: normal; }
.controls { display: flex; gap: 12px; align-items: center; flex-wrap: wrap; }
#settings { display: flex; gap: 12px; align-items: center; flex-wrap: wrap; }
button.icon { display: none; padding: 6px 10px; }
.badge { padding: 2px 8px; border-radius: 10px; background: #33443a; color: var(--muted); font-size: 12px; }
.badge.ready { background: #1f5536; color: #bff3d2; }
.badge.err { background: #5a2420; color: #ffd2cc; }
button {
  background: var(--accent); color: #1d1600; border: 0; border-radius: 6px; padding: 6px 14px;
  font-weight: 600; cursor: pointer; font-size: 14px;
}
button.secondary { background: #2f4337; color: var(--text); }
button:disabled { opacity: .4; cursor: default; }
select, input { background: #0e1712; color: var(--text); border: 1px solid var(--border); border-radius: 4px; padding: 3px 6px; }

main { display: grid; grid-template-columns: minmax(0, 1fr) 320px; gap: 12px; padding: 12px 16px 0; }
@media (max-width: 1000px) { main { grid-template-columns: 1fr; } }

#table {
  display: grid; gap: 6px; padding: 10px; border-radius: 12px;
  background: radial-gradient(circle at center, var(--felt) 0%, var(--felt-dark) 100%);
  grid-template-columns: 1fr 1.4fr 1fr; grid-template-rows: auto auto auto;
  grid-template-areas: "tl top tr" "left center right" "bl bottom br";
  min-height: 460px;
}
.seat { padding: 6px; border-radius: 8px; min-height: 90px; }
.seat.top { grid-area: top; }
.seat.left { grid-area: left; }
.seat.right { grid-area: right; }
.seat.bottom { grid-area: bottom; }
.seat.active { box-shadow: 0 0 0 2px var(--accent) inset; }
.seat-head { display: flex; gap: 6px; align-items: baseline; margin-bottom: 4px; flex-wrap: wrap; }
.seat-head .wind { color: var(--accent); font-weight: 700; }
.seat-head .score { font-variant-numeric: tabular-nums; }
.seat-head .rank { color: var(--muted); font-size: 12px; }
.seat-head .riichi { background: var(--bad); color: #fff; font-size: 11px; padding: 0 5px; border-radius: 3px; }
.river { display: flex; flex-direction: column; gap: 2px; }
.river-row { display: flex; gap: 2px; align-items: flex-end; }
.melds { display: flex; gap: 6px; margin-top: 4px; flex-wrap: wrap; }
.meld { display: flex; gap: 1px; align-items: flex-end; }
.hidden-hand { color: var(--muted); font-size: 12px; margin-top: 4px; }
.revealed { display: flex; gap: 1px; margin-top: 4px; flex-wrap: wrap; }

.center {
  grid-area: center; background: rgba(0,0,0,.25); border-radius: 10px; padding: 10px;
  display: flex; flex-direction: column; align-items: center; justify-content: center; gap: 6px; text-align: center;
}
.center .round { font-size: 20px; font-weight: 700; }
.center .sticks { color: var(--muted); }
.center .dora { display: flex; gap: 2px; align-items: center; }
.empty { color: var(--muted); }

/* 牌面：FluffyStuff/riichi-mahjong-tiles（SVG，3:4），底圖已含牌框與立體感，這裡只加陰影 */
.tile {
  display: inline-block; width: 30px; height: 40px; flex: none;
  background-color: transparent; background-size: 100% 100%; background-repeat: no-repeat;
  border-radius: 4px; box-shadow: 0 1px 2px rgba(0,0,0,.45); user-select: none;
}
.tile.sm { width: 24px; height: 32px; border-radius: 3px; }
.tile.tsumogiri { filter: brightness(.78); }
/* 橫放的牌（立直宣言牌、副露的被鳴牌）：外框為橫向尺寸，內部的牌轉 90° */
.tile-side { display: inline-flex; flex-direction: column; flex: none; }
.tile-side .side-cell { width: 40px; height: 30px; display: flex; align-items: center; justify-content: center; }
.tile-side.sm .side-cell { width: 32px; height: 24px; }
.tile-side .side-cell > .tile { transform: rotate(-90deg); }
#hand .meld .tile { width: 30px; height: 40px; border-radius: 4px; }
#hand .meld .tile-side .side-cell { width: 40px; height: 30px; }
.tile.called { opacity: .35; }
.tile.dora-glow { box-shadow: 0 0 0 2px var(--accent); }
.tile.back { background: #2f6d4a; border: 1px solid #23533a; border-bottom: 3px solid #1b4430; }

#hand-area { padding: 10px 16px 20px; }
#hand { display: flex; gap: 3px; align-items: flex-end; flex-wrap: wrap; }
#hand .tile { width: 45px; height: 60px; border-radius: 6px; box-shadow: 0 2px 4px rgba(0,0,0,.5); cursor: default; transition: transform .08s; }
#hand .tile.playable { cursor: pointer; }
#hand .tile.playable:hover { transform: translateY(-8px); }
#hand .tile.blocked { filter: grayscale(1) brightness(.6); }
#hand .tile.hinted { box-shadow: 0 0 0 3px var(--good); }
#hand .gap { width: 14px; }
#hand .meld-sep { width: 18px; }
#actions { display: flex; gap: 8px; margin-bottom: 8px; min-height: 34px; flex-wrap: wrap; }
#actions button.call { background: #f08a3c; }
#actions button.win { background: var(--bad); color: #fff; }
#actions button.pass { background: #2f4337; color: var(--text); }

.panel { background: var(--panel); border: 1px solid var(--border); border-radius: 10px; padding: 10px 12px; margin-bottom: 10px; }
.panel h3 { margin: 0 0 8px; font-size: 14px; color: var(--accent); }
.muted { color: var(--muted); }
.qrow { display: grid; grid-template-columns: 1fr 52px; gap: 6px; align-items: center; margin: 3px 0; font-size: 13px; }
.qbar { height: 6px; background: #2b3a31; border-radius: 3px; overflow: hidden; margin-top: 2px; }
.qbar > i { display: block; height: 100%; background: var(--good); }
.qrow.chosen .qlabel { color: var(--accent); font-weight: 700; }
.qrow .qval { text-align: right; font-variant-numeric: tabular-nums; color: var(--muted); font-size: 12px; }
.verdict { font-weight: 700; }
.verdict.ok { color: var(--good); }
.verdict.ng { color: var(--bad); }

#modal { position: fixed; inset: 0; background: rgba(0,0,0,.55); display: flex; align-items: center; justify-content: center; z-index: 10; }
#modal[hidden] { display: none; }
.modal-box { background: var(--panel); border: 1px solid var(--border); border-radius: 12px; padding: 18px 20px; width: min(720px, 94vw); max-height: 88vh; overflow: auto; }
.modal-actions { display: flex; justify-content: flex-end; gap: 8px; margin-top: 14px; }
.result-win { margin-bottom: 12px; }
.result-win .yaku { display: flex; flex-wrap: wrap; gap: 6px 14px; margin: 6px 0; }
.deltas { width: 100%; border-collapse: collapse; margin-top: 8px; }
.deltas td, .deltas th { padding: 4px 6px; border-bottom: 1px solid var(--border); text-align: left; }
.deltas .plus { color: var(--good); }
.deltas .minus { color: var(--bad); }
dialog { background: var(--panel); color: var(--text); border: 1px solid var(--border); border-radius: 12px; width: min(420px, 92vw); }
dialog::backdrop { background: rgba(0,0,0,.5); }
dialog label { display: flex; flex-direction: column; gap: 4px; margin: 10px 0; }
.stat-grid { display: grid; grid-template-columns: repeat(auto-fill, minmax(150px, 1fr)); gap: 8px; margin: 10px 0; }
.stat { background: #111a15; border-radius: 8px; padding: 8px; }
.stat b { display: block; font-size: 18px; }
.stat span { color: var(--muted); font-size: 12px; }
.advice { border-left: 3px solid var(--muted); padding: 4px 10px; margin: 8px 0; }
.advice.high { border-color: var(--bad); }
.advice.medium { border-color: var(--accent); }
.advice.low { border-color: var(--good); }

/* ---------------------------------------------------------------- 登入、頁尾、選取 */
#login { position: fixed; inset: 0; background: var(--bg); display: flex; align-items: center; justify-content: center; z-index: 20; }
#login[hidden] { display: none; }
.login-box { width: min(360px, 92vw); }
.login-box input { width: 100%; font-size: 16px; padding: 8px 10px; }
.error { color: var(--bad); min-height: 1.4em; margin-top: 6px; }
footer { padding: 6px 16px 14px; color: var(--muted); font-size: 12px; }
footer a { color: var(--muted); }
#mini-feedback { display: none; }
#hand .tile.selected { transform: translateY(-10px); box-shadow: 0 0 0 3px var(--accent); }
#actions button.confirm { background: var(--accent); }

/* ---------------------------------------------------------------- 手機版（直向） */
@media (max-width: 760px) and (orientation: portrait) {
  body { font-size: 13px; }
  header { padding: 6px 10px; gap: 6px; }
  header h1 { font-size: 15px; }
  header h1 small { display: none; }
  .controls { gap: 6px; }
  button { padding: 8px 12px; min-height: 40px; }
  button.icon { display: inline-block; }
  #settings {
    display: none; position: absolute; top: 52px; right: 8px; z-index: 15;
    flex-direction: column; align-items: flex-start; gap: 12px;
    background: var(--panel); border: 1px solid var(--border); border-radius: 10px; padding: 12px 14px;
  }
  #settings.open { display: flex; }
  #settings input[type=checkbox] { width: 20px; height: 20px; vertical-align: middle; }
  #settings select { font-size: 16px; }

  /* 手機上的順序：牌桌 → 手牌 → 對照與資訊面板 → 頁尾 */
  body { display: flex; flex-direction: column; min-height: 100vh; }
  main { display: contents; }
  header { order: 0; }
  #table { order: 1; margin: 6px 6px 0; }
  #hand-area { order: 2; }
  #side { order: 3; padding: 8px 6px 0; }
  footer { order: 4; }
  #table {
    grid-template-columns: 1fr 1fr; grid-template-rows: none;
    grid-template-areas: "top top" "left right" "center center" "bottom bottom";
    min-height: 0; padding: 6px; gap: 4px;
  }
  .seat { min-height: 0; padding: 4px; }
  .seat-head { gap: 4px; font-size: 12px; }
  .seat-head .rank { font-size: 11px; }
  .center { flex-direction: row; flex-wrap: wrap; gap: 4px 10px; padding: 6px; }
  .center .round { font-size: 15px; }
  .tile.sm { width: 19px; height: 25px; border-radius: 2px; }
  .tile-side.sm .side-cell { width: 25px; height: 19px; }
  .river, .river-row { gap: 1px; }
  .melds { gap: 4px; }

  /* 手牌區固定在畫面下緣 */
  #hand-area {
    position: sticky; bottom: 0; z-index: 5; padding: 6px 6px calc(8px + env(safe-area-inset-bottom));
    background: rgba(15, 26, 20, .96); border-top: 1px solid var(--border);
  }
  #mini-feedback { display: block; font-size: 12px; min-height: 1.3em; margin-bottom: 4px; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
  #mini-feedback .ok { color: var(--good); }
  #mini-feedback .ng { color: var(--bad); }
  #actions { gap: 6px; min-height: 0; margin-bottom: 6px; }
  #actions button { flex: 1 1 auto; min-height: 44px; font-size: 15px; }
  #actions .muted { align-self: center; font-size: 12px; }
  #hand { flex-wrap: nowrap; gap: 2px; }
  #hand .tile {
    width: calc((100vw - 12px - 13 * 2px - 10px) / 14); max-width: 46px;
    height: auto; aspect-ratio: 3 / 4; border-radius: 3px;
  }
  #hand .tile.playable:hover { transform: none; }
  #hand .tile.selected { transform: translateY(-8px); }
  #hand .gap { width: 8px; flex: none; }
  #hand .meld, #hand .meld-sep { display: none; }   /* 自己的副露已顯示在牌桌上 */
  .modal-box { padding: 14px; max-height: 92vh; }
  .stat-grid { grid-template-columns: repeat(2, 1fr); }
}

/* ---------------------------------------------------------------- 手機版（橫向）
   一頁放完不捲動：標題列 → 三欄牌桌（上家｜對家／場況／自家｜下家）→ 手牌與動作列 */
#settings-source, #hdr-info { display: none; }
@media (orientation: landscape) and (max-height: 500px) {
  body { height: 100vh; height: 100dvh; overflow: hidden; display: flex; flex-direction: column; font-size: 12px; }
  header { flex: none; padding: 2px 8px; gap: 6px; }
  header h1 { font-size: 13px; }
  header h1 small { display: none; }
  .controls { gap: 6px; }
  .badge { font-size: 11px; }
  button { padding: 4px 10px; min-height: 30px; font-size: 13px; }
  button.icon { display: inline-block; }
  #settings {
    display: none; position: absolute; top: 38px; right: 8px; z-index: 15;
    flex-direction: column; align-items: flex-start; gap: 12px;
    background: var(--panel); border: 1px solid var(--border); border-radius: 10px; padding: 12px 14px;
  }
  #settings.open { display: flex; }
  #settings input[type=checkbox] { width: 20px; height: 20px; vertical-align: middle; }
  #settings-source:not(:empty) { display: block; color: var(--muted); }
  #settings-source a { color: var(--muted); }

  main { display: block; flex: 1 1 auto; min-height: 0; padding: 4px 6px 0; }
  #side, footer { display: none; }
  #hdr-info { display: flex; align-items: center; gap: 6px; font-size: 12px; margin-right: auto; }
  #hdr-info .tile.sm { width: 16px; height: 21px; }
  .center { display: none; }
  #table {
    height: 100%; min-height: 0; overflow: auto; padding: 4px; gap: 4px;
    grid-template-columns: 1fr 1.25fr 1fr; grid-template-rows: 1fr 1fr;
    grid-template-areas: "left top right" "left bottom right";
  }
  /* 每家：名字一列，下面牌河在左、副露在右（橫向時寬度比高度多） */
  .seat {
    min-height: 0; padding: 2px 4px;
    display: grid; grid-template-columns: auto 1fr; grid-template-rows: auto 1fr; column-gap: 6px; align-content: start;
  }
  .seat-head, .seat .revealed { grid-column: 1 / -1; }
  .seat .melds { margin-top: 0; align-self: start; align-content: flex-start; }
  .seat-head { font-size: 11px; gap: 4px; margin-bottom: 2px; }
  .seat-head .rank { font-size: 10px; }
  .hidden-hand { display: none; }
  .tile.sm { width: 18px; height: 24px; border-radius: 2px; }
  .tile-side.sm .side-cell { width: 24px; height: 18px; }
  .river, .river-row { gap: 1px; }
  .melds { gap: 4px; margin-top: 2px; }

  #hand-area {
    flex: none; display: grid; grid-template-columns: 1fr auto; grid-template-areas: "mini mini" "hand actions";
    align-items: end; gap: 2px 8px; padding: 2px 8px calc(4px + env(safe-area-inset-bottom));
    background: rgba(15, 26, 20, .96); border-top: 1px solid var(--border);
  }
  #mini-feedback { grid-area: mini; display: block; font-size: 11px; min-height: 1.2em; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
  #mini-feedback .ok { color: var(--good); }
  #mini-feedback .ng { color: var(--bad); }
  #hand { grid-area: hand; flex-wrap: nowrap; gap: 2px; }
  #hand .tile {
    width: min(40px, calc((100vw - 260px) / 14.6)); height: auto; aspect-ratio: 3 / 4; border-radius: 3px;
  }
  #hand .tile.playable:hover { transform: none; }
  #hand .tile.selected { transform: translateY(-6px); }
  #hand .gap { width: 6px; flex: none; }
  #hand .meld, #hand .meld-sep { display: none; }   /* 自己的副露已顯示在牌桌上 */
  #actions { grid-area: actions; margin: 0; min-height: 0; gap: 4px; max-width: 240px; flex-wrap: wrap; justify-content: flex-end; }
  #actions button { min-height: 40px; font-size: 13px; padding: 4px 8px; }
  #actions .muted { font-size: 11px; align-self: center; }
  .modal-box { padding: 12px; max-height: 94vh; }
}
