/* 廟口榕樹下・象棋 — 樣式 */

:root {
  --temple-red: #8b1a1a;
  --temple-red-dark: #5c0f0f;
  --lacquer: #b0141c;
  --gold: #d4a017;
  --gold-light: #f0c95a;
  --banyan: #2f4f2f;
  --banyan-dark: #1c301c;
  --wood: #c8a165;
  --wood-light: #e3c48f;
  --wood-dark: #7a5428;
  --stone: #8f8a84;
  --paper: #f5e6c8;
  --paper-dark: #e6d0a4;
  --ink: #2a1c10;
  --muted: #7a6650;
  --radius: 10px;
  --header-h: 92px;
  --font-kai: "BiauKai", "DFKai-SB", "標楷體", "KaiTi", "STKaiti", serif;
  --font-sans: "Noto Sans TC", "Microsoft JhengHei", "PingFang TC", "Heiti TC", system-ui, sans-serif;
  color-scheme: light;
}

* { box-sizing: border-box; }
[hidden] { display: none !important; }

html, body { margin: 0; padding: 0; }

body {
  min-height: 100vh;
  min-height: 100svh;
  font-family: var(--font-sans);
  color: var(--ink);
  background:
    radial-gradient(ellipse at 50% -10%, rgba(255, 190, 110, 0.35), transparent 55%),
    linear-gradient(180deg, #2c4a2c 0%, #3b4630 35%, #5b4a34 75%, #4a3a2a 100%);
  background-attachment: fixed;
  overflow-x: hidden;
  -webkit-tap-highlight-color: transparent;
}

/* 榕樹葉影（CSS 漸層堆疊，緩慢擺動） */
body::before,
body::after {
  content: "";
  position: fixed;
  inset: -10%;
  pointer-events: none;
  z-index: 0;
}
body::before {
  background:
    radial-gradient(ellipse 140px 70px at 8% 12%, rgba(10, 25, 10, 0.45), transparent 70%),
    radial-gradient(ellipse 90px 46px at 18% 30%, rgba(10, 25, 10, 0.35), transparent 70%),
    radial-gradient(ellipse 160px 80px at 30% 8%, rgba(10, 25, 10, 0.4), transparent 70%),
    radial-gradient(ellipse 120px 60px at 70% 10%, rgba(10, 25, 10, 0.42), transparent 70%),
    radial-gradient(ellipse 100px 52px at 86% 24%, rgba(10, 25, 10, 0.38), transparent 70%),
    radial-gradient(ellipse 180px 70px at 92% 60%, rgba(10, 25, 10, 0.3), transparent 70%),
    radial-gradient(ellipse 130px 60px at 4% 70%, rgba(10, 25, 10, 0.32), transparent 70%),
    radial-gradient(ellipse 110px 50px at 60% 92%, rgba(10, 25, 10, 0.25), transparent 70%),
    radial-gradient(ellipse 70px 36px at 45% 22%, rgba(10, 25, 10, 0.25), transparent 70%),
    radial-gradient(ellipse 80px 40px at 52% 60%, rgba(10, 25, 10, 0.18), transparent 70%);
  animation: leaf-sway 14s ease-in-out infinite alternate;
}
body::after {
  /* 葉隙光斑 */
  background:
    radial-gradient(circle 26px at 14% 22%, rgba(255, 214, 150, 0.16), transparent 70%),
    radial-gradient(circle 18px at 24% 46%, rgba(255, 214, 150, 0.12), transparent 70%),
    radial-gradient(circle 30px at 76% 18%, rgba(255, 214, 150, 0.15), transparent 70%),
    radial-gradient(circle 20px at 88% 44%, rgba(255, 214, 150, 0.12), transparent 70%),
    radial-gradient(circle 24px at 36% 84%, rgba(255, 214, 150, 0.1), transparent 70%),
    radial-gradient(circle 16px at 64% 74%, rgba(255, 214, 150, 0.1), transparent 70%);
  animation: leaf-sway 11s ease-in-out infinite alternate-reverse;
}
@keyframes leaf-sway {
  from { transform: translate(0, 0) rotate(0deg); }
  to { transform: translate(18px, 10px) rotate(0.8deg); }
}

/* ---------- 屋簷標題 ---------- */
.eave {
  position: relative;
  z-index: 2;
  height: var(--header-h);
  color: #fff3d6;
}
.eave-roof {
  position: absolute;
  inset: 0 0 auto 0;
  width: 100%;
  height: 72%;
  filter: drop-shadow(0 6px 8px rgba(0, 0, 0, 0.45));
}
.eave-content {
  position: relative;
  height: 100%;
  display: flex;
  align-items: flex-start;
  justify-content: center;
  gap: clamp(12px, 4vw, 60px);
  padding-top: 6px;
}
.title {
  margin: 0;
  font-family: var(--font-kai);
  font-weight: 700;
  line-height: 1;
  text-align: center;
  padding: 8px 22px 10px;
  background: linear-gradient(180deg, #a8241f, var(--temple-red));
  border: 3px solid var(--gold);
  border-radius: 6px;
  box-shadow: 0 0 0 3px var(--temple-red-dark), 0 6px 14px rgba(0, 0, 0, 0.5), inset 0 0 12px rgba(0, 0, 0, 0.35);
  color: var(--gold-light);
  text-shadow: 0 2px 0 rgba(0, 0, 0, 0.5);
  white-space: nowrap;
}
.title-main { font-size: clamp(20px, 3.4vw, 34px); letter-spacing: 0.12em; }
.title-sub {
  font-size: clamp(14px, 2vw, 20px);
  margin-left: 0.5em;
  padding-left: 0.5em;
  border-left: 2px solid rgba(212, 160, 23, 0.6);
  letter-spacing: 0.2em;
}
.lantern-row { display: flex; gap: clamp(8px, 3vw, 36px); padding-top: 18px; }

/* 紅燈籠 */
.lantern {
  --w: 46px;
  position: relative;
  display: grid;
  place-items: center;
  width: var(--w);
  height: calc(var(--w) * 0.86);
  margin-top: 16px;
  border-radius: 50% / 50%;
  background:
    repeating-linear-gradient(90deg, transparent 0 8px, rgba(90, 10, 10, 0.35) 8px 9px),
    radial-gradient(circle at 35% 30%, #ff7a45, #d8201a 55%, #7d0c0c 100%);
  box-shadow: 0 0 22px rgba(255, 110, 40, 0.55), 0 0 4px rgba(255, 160, 80, 0.6) inset;
  color: #ffe7a0;
  font-family: var(--font-kai);
  font-weight: 700;
  font-size: calc(var(--w) * 0.42);
  transform-origin: 50% -26px;
  animation: lantern-swing 3.6s ease-in-out infinite alternate;
  animation-delay: var(--d, 0s);
}
.lantern::before {
  /* 頂蓋＋吊繩 */
  content: "";
  position: absolute;
  top: -22px;
  left: 50%;
  width: 40%;
  height: 22px;
  transform: translateX(-50%);
  background:
    linear-gradient(#2b1410, #2b1410) center top / 2px 16px no-repeat,
    linear-gradient(180deg, var(--gold-light), #9a7410) center bottom / 100% 6px no-repeat;
}
.lantern::after {
  /* 底蓋＋流蘇 */
  content: "";
  position: absolute;
  bottom: -22px;
  left: 50%;
  width: 40%;
  height: 22px;
  transform: translateX(-50%);
  background:
    linear-gradient(180deg, #9a7410, var(--gold-light)) center top / 100% 5px no-repeat,
    repeating-linear-gradient(90deg, var(--gold) 0 2px, transparent 2px 4px) center 5px / 70% 17px no-repeat;
}
.lantern.small { --w: 36px; margin-top: 26px; }
@keyframes lantern-swing {
  from { transform: rotate(-4deg); }
  to { transform: rotate(4deg); }
}

/* ---------- 工具列 ---------- */
.toolbar {
  position: relative;
  z-index: 3;
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  justify-content: center;
  gap: 8px;
  padding: 6px 12px 10px;
}

.btn, .seg-btn, select {
  font: inherit;
  font-size: 15px;
  line-height: 1;
  color: var(--ink);
  border: 1px solid var(--wood-dark);
  border-radius: 8px;
  padding: 9px 14px;
  min-height: 38px;
  background: linear-gradient(180deg, var(--wood-light), var(--wood));
  box-shadow: 0 2px 0 var(--wood-dark), inset 0 1px 0 rgba(255, 255, 255, 0.4);
  cursor: pointer;
  transition: transform 0.08s, filter 0.15s, box-shadow 0.08s;
  white-space: nowrap;
}
.btn:hover:not(:disabled), .seg-btn:hover:not(.active), select:hover { filter: brightness(1.07); }
.btn:active:not(:disabled), .seg-btn:active { transform: translateY(2px); box-shadow: 0 0 0 var(--wood-dark); }
.btn:disabled { opacity: 0.45; cursor: not-allowed; }
.btn:focus-visible, .seg-btn:focus-visible, select:focus-visible, .switch input:focus-visible + span {
  outline: 3px solid var(--gold-light);
  outline-offset: 2px;
}
.btn.primary {
  color: var(--gold-light);
  border-color: var(--temple-red-dark);
  background: linear-gradient(180deg, #b52a22, var(--temple-red));
  box-shadow: 0 2px 0 var(--temple-red-dark), inset 0 1px 0 rgba(255, 200, 150, 0.35);
}
.btn.gold {
  color: #3a2400;
  border-color: #8a6508;
  background: linear-gradient(180deg, var(--gold-light), var(--gold));
  box-shadow: 0 2px 0 #8a6508, inset 0 1px 0 rgba(255, 255, 255, 0.5);
}
.btn.small { padding: 6px 10px; min-height: 30px; font-size: 13px; }

.seg { display: inline-flex; border-radius: 9px; box-shadow: 0 2px 0 var(--wood-dark); }
.seg-btn { border-radius: 0; box-shadow: inset 0 1px 0 rgba(255, 255, 255, 0.4); }
.seg-btn:first-child { border-radius: 8px 0 0 8px; }
.seg-btn:last-child { border-radius: 0 8px 8px 0; border-left: none; }
.seg-btn.active {
  color: var(--gold-light);
  background: linear-gradient(180deg, #2f5a2f, var(--banyan));
  border-color: var(--banyan-dark);
}

select {
  appearance: none;
  -webkit-appearance: none;
  padding-right: 30px;
  background:
    linear-gradient(45deg, transparent 50%, var(--ink) 50%) right 14px center / 6px 6px no-repeat,
    linear-gradient(-45deg, transparent 50%, var(--ink) 50%) right 9px center / 6px 6px no-repeat,
    linear-gradient(180deg, var(--wood-light), var(--wood));
}
select:disabled { opacity: 0.6; cursor: not-allowed; }

.sr-only {
  position: absolute; width: 1px; height: 1px; overflow: hidden;
  clip: rect(0 0 0 0); white-space: nowrap; border: 0; padding: 0; margin: -1px;
}

body[data-mode="banqi"] .only-xiangqi { display: none !important; }
body[data-mode="xiangqi"] .only-banqi { display: none !important; }

/* ---------- 主版面 ---------- */
.layout {
  position: relative;
  z-index: 1;
  display: grid;
  grid-template-columns: minmax(0, 1fr) 320px;
  gap: 16px;
  max-width: 1400px;
  margin: 0 auto;
  padding: 0 16px 12px;
}

.board-area {
  position: relative;
  height: calc(100vh - var(--header-h) - 120px);
  height: calc(100svh - var(--header-h) - 120px);
  min-height: 420px;
  border-radius: 14px;
  overflow: hidden;
  background:
    radial-gradient(ellipse at 50% 40%, rgba(255, 220, 160, 0.12), transparent 70%),
    rgba(20, 30, 18, 0.35);
  box-shadow: inset 0 0 40px rgba(0, 0, 0, 0.35);
}
.board { position: absolute; inset: 0; }
.board-canvas { display: block; touch-action: manipulation; user-select: none; -webkit-user-select: none; }
.board-3d { touch-action: none; }

/* 載入、思考中、提示 */
.loading {
  position: absolute; inset: 0;
  display: flex; flex-direction: column; align-items: center; justify-content: center; gap: 12px;
  color: var(--gold-light);
  font-size: 16px;
  background: rgba(20, 16, 10, 0.55);
  z-index: 5;
}
.loading-spinner {
  width: 46px; height: 46px; border-radius: 50%;
  border: 4px solid rgba(212, 160, 23, 0.25);
  border-top-color: var(--gold);
  animation: spin 0.9s linear infinite;
}
@keyframes spin { to { transform: rotate(360deg); } }

.thinking {
  position: absolute;
  top: 12px; left: 50%;
  transform: translateX(-50%);
  z-index: 6;
  display: flex; align-items: center; gap: 8px;
  padding: 8px 16px;
  border-radius: 999px;
  color: var(--gold-light);
  background: rgba(60, 12, 12, 0.88);
  border: 1px solid var(--gold);
  box-shadow: 0 4px 14px rgba(0, 0, 0, 0.4);
  font-size: 15px;
  pointer-events: none;
}
.thinking-dot {
  width: 10px; height: 10px; border-radius: 50%;
  background: var(--gold);
  animation: pulse 1s ease-in-out infinite;
}
.dots i { font-style: normal; animation: blink 1.2s infinite; }
.dots i:nth-child(2) { animation-delay: 0.2s; }
.dots i:nth-child(3) { animation-delay: 0.4s; }
@keyframes blink { 0%, 100% { opacity: 0.2; } 50% { opacity: 1; } }
@keyframes pulse { 0%, 100% { transform: scale(0.7); opacity: 0.6; } 50% { transform: scale(1.1); opacity: 1; } }

.check-toast {
  position: absolute;
  left: 50%; top: 42%;
  z-index: 7;
  transform: translate(-50%, -50%) scale(0.4);
  opacity: 0;
  pointer-events: none;
  font-family: var(--font-kai);
  font-weight: 700;
  font-size: clamp(54px, 11vw, 110px);
  color: #ffdf7a;
  -webkit-text-stroke: 2px #5c0f0f;
  text-shadow: 0 0 18px rgba(255, 60, 20, 0.85), 0 6px 0 #5c0f0f, 0 10px 18px rgba(0, 0, 0, 0.6);
  letter-spacing: 0.1em;
  white-space: nowrap;
}
.check-toast.show { animation: check-pop 1.3s cubic-bezier(.2, 1.4, .4, 1) forwards; }
@keyframes check-pop {
  0% { opacity: 0; transform: translate(-50%, -50%) scale(0.4) rotate(-8deg); }
  18% { opacity: 1; transform: translate(-50%, -50%) scale(1.12) rotate(2deg); }
  30% { transform: translate(-50%, -50%) scale(1) rotate(0); }
  80% { opacity: 1; }
  100% { opacity: 0; transform: translate(-50%, -60%) scale(1); }
}

.toast {
  /* 固定在視窗頂端（標題裝飾區），避免蓋住棋盤上的棋子 */
  position: fixed;
  left: 50%; top: 10px;
  z-index: 60;
  width: max-content;
  max-width: min(92vw, 520px);
  transform: translate(-50%, -20px);
  opacity: 0;
  padding: 10px 16px;
  border-radius: 10px;
  color: #fff3d6;
  background: rgba(30, 20, 12, 0.92);
  border: 1px solid rgba(212, 160, 23, 0.7);
  box-shadow: 0 6px 18px rgba(0, 0, 0, 0.45);
  font-size: 15px;
  line-height: 1.5;
  text-align: center;
  transition: opacity 0.25s, transform 0.25s;
  pointer-events: none;
}
.toast.show { opacity: 1; transform: translate(-50%, 0); }

.endgame-hint {
  margin: 0 0 8px;
  padding: 8px 10px;
  border-radius: 8px;
  font-size: 13px;
  line-height: 1.6;
  color: #5a2a0a;
  background: rgba(212, 160, 23, 0.16);
  border: 1px dashed rgba(139, 26, 26, 0.45);
}

/* ---------- 側欄 ---------- */
.side {
  display: flex;
  flex-direction: column;
  gap: 10px;
  min-width: 0;
  max-height: calc(100svh - var(--header-h) - 120px);
  overflow: auto;
}
.panel {
  margin: 0;
  padding: 10px 12px;
  border-radius: var(--radius);
  background: linear-gradient(180deg, var(--paper), var(--paper-dark));
  border: 1px solid #b08a52;
  border-top: 4px solid var(--temple-red);
  box-shadow: 0 4px 12px rgba(0, 0, 0, 0.3);
  min-width: 0;
}
fieldset.panel { border-top: 4px solid var(--temple-red); }
.panel-title {
  display: flex; align-items: baseline; justify-content: space-between; gap: 8px;
  margin: 0 0 6px;
  padding: 0;
  font-family: var(--font-kai);
  font-weight: 700;
  font-size: 17px;
  color: var(--temple-red);
}
legend.panel-title { float: left; width: 100%; }
legend.panel-title + * { clear: both; }
.muted { color: var(--muted); font-family: var(--font-sans); font-weight: 400; font-size: 12px; }

.turn-indicator { display: flex; align-items: center; gap: 10px; font-size: 17px; font-weight: 700; }
.turn-chip {
  width: 18px; height: 18px; border-radius: 50%;
  background: var(--stone);
  box-shadow: 0 0 0 2px rgba(0, 0, 0, 0.15);
  flex: none;
}
.turn-chip.r { background: radial-gradient(circle at 35% 35%, #ff5a4a, var(--lacquer)); }
.turn-chip.b { background: radial-gradient(circle at 35% 35%, #555, #111); }
.turn-chip.thinking { animation: pulse 1s ease-in-out infinite; }
.banqi-colors { margin-top: 6px; font-size: 14px; color: var(--muted); }
.banqi-colors b.r { color: var(--lacquer); }
.banqi-colors b.b { color: #111; }
/* 暗棋連吃選擇列：桌機浮在棋盤區底部中央；窄螢幕（見 RWD）改到棋盤正下方，避免蓋住棋子 */
.board-col { position: relative; min-width: 0; }
.chain-bar {
  position: absolute;
  left: 50%; bottom: 8px;
  transform: translateX(-50%);
  z-index: 6;
  display: flex; align-items: center; gap: 12px;
  max-width: calc(100% - 16px);
  padding: 6px 6px 6px 16px;
  border-radius: 999px;
  color: #fff3d6;
  background: rgba(30, 20, 12, 0.9);
  border: 2px solid var(--gold);
  box-shadow: 0 6px 18px rgba(0, 0, 0, 0.45);
  font-size: 15px;
  white-space: nowrap;
}
.chain-bar.off, body:not([data-mode="banqi"]) .chain-bar { display: none; }
.chain-text b { color: var(--gold-light); }
.endchain {
  font-size: 17px; font-weight: 700;
  min-height: 42px; padding: 10px 18px;
  border-radius: 999px;
  animation: glow 1.1s ease-in-out infinite alternate;
}
@keyframes glow {
  from { box-shadow: 0 2px 0 var(--temple-red-dark), 0 0 0 rgba(240, 201, 90, 0); }
  to { box-shadow: 0 2px 0 var(--temple-red-dark), 0 0 18px rgba(240, 201, 90, 0.95); }
}

.endgame-name { font-family: var(--font-kai); font-size: 22px; font-weight: 700; }
.endgame-desc { font-size: 13px; color: var(--muted); margin: 4px 0 8px; line-height: 1.5; }
.endgame-actions { display: flex; gap: 6px; align-items: center; flex-wrap: wrap; }
.goal-tag {
  display: inline-block;
  padding: 3px 8px;
  border-radius: 4px;
  background: var(--temple-red);
  color: var(--gold-light);
  font-size: 13px;
  font-weight: 700;
}

.cap-row { display: flex; align-items: center; gap: 8px; min-height: 34px; }
.cap-label { flex: none; width: 3.6em; font-size: 13px; color: var(--muted); }
.cap-list { display: flex; flex-wrap: wrap; gap: 2px; min-width: 0; }
.cap-list canvas { width: 28px; height: 28px; filter: drop-shadow(0 1px 1px rgba(0, 0, 0, 0.4)); }

.moves-panel { flex: 1 1 auto; min-height: 140px; display: flex; flex-direction: column; }
.movelist {
  list-style: none;
  margin: 0;
  padding: 0;
  overflow-y: auto;
  max-height: 260px;
  font-family: var(--font-kai);
  font-size: 16px;
  counter-reset: none;
}
.movelist li {
  display: grid;
  grid-template-columns: 2.2em 1fr 1fr;
  gap: 4px;
  padding: 3px 4px;
  border-bottom: 1px dashed rgba(139, 90, 43, 0.25);
}
.movelist.banqi li { grid-template-columns: 2.2em 3em 1fr; }
.movelist li:last-child { background: rgba(212, 160, 23, 0.2); border-radius: 4px; }
.movelist .no { color: var(--muted); font-family: var(--font-sans); font-size: 12px; align-self: center; }
.movelist .r { color: var(--lacquer); }
.movelist .b { color: #111; }
.movelist .who { font-family: var(--font-sans); font-size: 12px; color: var(--muted); align-self: center; }

.rules-panel { display: flex; flex-direction: column; gap: 6px; }
.rules-panel > .panel-title { white-space: nowrap; }
.rule-blind { display: flex; flex-direction: column; gap: 6px; margin-top: 4px; container-type: inline-size; }
.rule-label { font-size: 15px; }
.seg-full { display: flex; width: 100%; }
.seg-full .seg-btn { flex: 1 1 0; min-width: 0; padding-left: 6px; padding-right: 6px; font-size: 14px; }
.rule-desc { margin: 0; line-height: 1.5; }
/* 側欄很窄（手機）時兩個選項改成上下排列，避免文字被擠成兩行 */
@container (max-width: 260px) {
  .seg-full { flex-direction: column; }
  .seg-full .seg-btn:first-child { border-radius: 8px 8px 0 0; }
  .seg-full .seg-btn:last-child { border-radius: 0 0 8px 8px; border-left: 1px solid var(--wood-dark); border-top: none; }
  .seg-full .seg-btn.active:last-child { border-left-color: var(--banyan-dark); }
}
.switch { display: flex; align-items: center; gap: 10px; cursor: pointer; font-size: 15px; }
.switch input { position: absolute; opacity: 0; width: 1px; height: 1px; }
.switch span::before {
  content: "";
  display: inline-block;
  vertical-align: middle;
  width: 38px; height: 22px;
  margin-right: 8px;
  border-radius: 999px;
  background: #b9a585;
  box-shadow: inset 0 1px 3px rgba(0, 0, 0, 0.35);
  transition: background 0.2s;
}
.switch span { position: relative; }
.switch span::after {
  content: "";
  position: absolute;
  left: 3px; top: 50%;
  width: 16px; height: 16px;
  margin-top: -8px;
  border-radius: 50%;
  background: #fff;
  box-shadow: 0 1px 2px rgba(0, 0, 0, 0.4);
  transition: transform 0.2s;
}
.switch input:checked + span::before { background: var(--banyan); }
.switch input:checked + span::after { transform: translateX(16px); }

.stats-summary { font-size: 14px; line-height: 1.6; }
.stats-summary b { color: var(--temple-red); }

.foot {
  position: relative;
  z-index: 1;
  text-align: center;
  padding: 4px 12px 14px;
  font-family: var(--font-kai);
  color: rgba(255, 233, 180, 0.7);
  letter-spacing: 0.2em;
  font-size: 14px;
}

/* ---------- Modal ---------- */
.modal {
  position: fixed;
  inset: 0;
  z-index: 50;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 16px;
  background: rgba(10, 6, 4, 0.7);
  backdrop-filter: blur(3px);
  animation: fade-in 0.25s ease-out;
}
@keyframes fade-in { from { opacity: 0; } to { opacity: 1; } }
.modal-card {
  width: min(560px, 100%);
  max-height: calc(100svh - 32px);
  overflow: auto;
  padding: 16px;
  border-radius: 14px;
  background: linear-gradient(180deg, var(--paper), var(--paper-dark));
  border: 3px solid var(--gold);
  box-shadow: 0 0 0 4px var(--temple-red), 0 18px 40px rgba(0, 0, 0, 0.6);
  animation: pop-in 0.3s cubic-bezier(.2, 1.3, .4, 1);
}
@keyframes pop-in { from { transform: scale(0.9); opacity: 0; } to { transform: scale(1); opacity: 1; } }
.modal-title {
  margin: 0 0 8px;
  text-align: center;
  font-family: var(--font-kai);
  font-size: 26px;
  color: var(--temple-red);
}
.modal-sub { margin: 0 0 12px; text-align: center; color: var(--muted); font-size: 14px; }
.modal-actions { display: flex; flex-wrap: wrap; justify-content: center; gap: 10px; margin-top: 14px; }
.result-holder { display: flex; justify-content: center; }
.result-holder canvas {
  width: auto;
  max-width: 100%;
  max-height: min(62svh, 640px);
  aspect-ratio: 1080 / 1350;
  border-radius: 8px;
  box-shadow: 0 6px 20px rgba(0, 0, 0, 0.45);
}

.endgame-card { width: min(720px, 100%); }
.endgame-list {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(200px, 1fr));
  gap: 10px;
}
.endgame-item {
  position: relative;
  display: flex;
  flex-direction: column;
  gap: 4px;
  text-align: left;
  font: inherit;
  padding: 10px 12px;
  border-radius: 10px;
  border: 1px solid var(--wood-dark);
  background: linear-gradient(180deg, #fbf0dc, #ecd8b0);
  cursor: pointer;
  box-shadow: 0 2px 0 var(--wood-dark);
  transition: transform 0.1s, box-shadow 0.1s;
}
.endgame-item:hover { transform: translateY(-2px); box-shadow: 0 4px 0 var(--wood-dark); }
.endgame-item:focus-visible { outline: 3px solid var(--gold); outline-offset: 2px; }
.endgame-item.current { border-color: var(--temple-red); box-shadow: 0 0 0 2px var(--temple-red); }
.endgame-item .lv { font-size: 12px; color: var(--muted); }
.endgame-item .nm { font-family: var(--font-kai); font-size: 20px; font-weight: 700; color: var(--temple-red); }
.endgame-item .ds { font-size: 12px; color: var(--ink); line-height: 1.45; }
.endgame-item .cleared {
  position: absolute; top: 8px; right: 8px;
  padding: 2px 6px;
  border: 2px solid #c4161c;
  border-radius: 4px;
  color: #c4161c;
  font-family: var(--font-kai);
  font-size: 13px;
  font-weight: 700;
  transform: rotate(8deg);
}

/* 抖動（effects.js 的 CSS 後援） */
.shake { animation: shake 0.4s ease-out; }
@keyframes shake {
  0% { transform: translate(0, 0); }
  15% { transform: translate(calc(var(--shake, 6px) * -1), calc(var(--shake, 6px) * 0.4)); }
  30% { transform: translate(var(--shake, 6px), calc(var(--shake, 6px) * -0.5)); }
  45% { transform: translate(calc(var(--shake, 6px) * -0.6), calc(var(--shake, 6px) * 0.3)); }
  60% { transform: translate(calc(var(--shake, 6px) * 0.4), calc(var(--shake, 6px) * -0.2)); }
  80% { transform: translate(calc(var(--shake, 6px) * -0.15), 0); }
  100% { transform: translate(0, 0); }
}

.noscript { color: #fff; text-align: center; }

/* ---------- RWD ---------- */
@media (max-width: 960px) {
  .layout { grid-template-columns: 1fr; padding: 0 8px 10px; gap: 10px; }
  .board-area {
    height: min(calc(100vw * 1.12), calc(100svh - var(--header-h) - 110px));
    min-height: 340px;
  }
  /* 暗棋：棋盤下方固定預留連吃按鈕列（約 62px），按鈕一出現就在畫面內、版面也不會跳動 */
  body[data-mode="banqi"] .board-area {
    height: min(calc(100vw * 1.5), calc(100svh - var(--header-h) - 172px));
  }
  body[data-mode="banqi"] .chain-bar.off { display: flex; visibility: hidden; }
  .side { max-height: none; overflow: visible; }
  .moves-panel { min-height: 0; }
  .chain-bar {
    position: static;
    transform: none;
    max-width: none;
    margin-top: 8px;
    gap: 10px;
    padding: 6px 6px 6px 12px;
    border-radius: 12px;
  }
  .chain-bar .endchain { flex: 1 1 auto; padding-left: 10px; padding-right: 10px; }
  .chain-text .long { display: none; }
  .movelist { max-height: 180px; }
}
@media (min-width: 961px) {
  .chain-text .short { display: none; }
}

@media (max-width: 600px) {
  :root { --header-h: 64px; }
  .lantern-row { display: none; }
  .eave-content { padding-top: 4px; }
  .title { padding: 6px 14px 8px; }
  .toolbar { gap: 6px; padding: 4px 6px 8px; }
  .btn, .seg-btn, select { font-size: 14px; padding: 8px 10px; min-height: 36px; }
  select { padding-right: 26px; }
  .board-area { border-radius: 10px; min-height: 300px; }
  .side {
    display: grid;
    grid-template-columns: 1fr 1fr;
  }
  .side > .turn-panel, .side > .endgame-info, .side > .moves-panel { grid-column: 1 / -1; }
  .side > .captured-panel { grid-column: 1 / -1; }
}

@media (prefers-reduced-motion: reduce) {
  body::before, body::after, .lantern, .endchain { animation: none; }
}
