:root {
  --bg: #1a1410;
  --bg-elevated: #2a2118;
  --bg-panel: #221b14;
  --border: #4a3a28;
  --text: #f3e6d0;
  --text-muted: #a89070;
  --accent: #c45c26;
  --accent-hover: #e07038;
  --danger: #e85d5d;
  --warn: #e0b84a;
  --success: #6dbf8a;
  --glow: rgba(196, 92, 38, 0.35);
  --board-wood: #c4a574;
  --board-wood-dark: #a67c52;
  --board-line: #5c3317;
  --piece-cream: #f5ebd6;
  --piece-red: #c41e3a;
  --piece-black: #1a1a1a;
  --select: rgba(80, 180, 90, 0.55);
  --last: rgba(240, 190, 70, 0.5);
  --check: rgba(220, 60, 50, 0.55);
  --radius: 14px;
  --font: "Segoe UI", "PingFang TC", "Noto Sans TC", "Microsoft JhengHei",
    "Source Han Sans TC", system-ui, sans-serif;
  --board-w: min(92vw, 480px, 68vh);
}

*,
*::before,
*::after {
  box-sizing: border-box;
}

html,
body {
  margin: 0;
  padding: 0;
  min-height: 100%;
  background:
    radial-gradient(ellipse at 30% 0%, #3a2a1c 0%, transparent 50%),
    radial-gradient(ellipse at 70% 100%, #2a1810 0%, transparent 45%),
    var(--bg);
  color: var(--text);
  font-family: var(--font);
  -webkit-tap-highlight-color: transparent;
  touch-action: manipulation;
}

body {
  display: flex;
  justify-content: center;
  padding: 16px 12px 24px;
}

.app {
  width: min(1100px, 100%);
  display: flex;
  flex-direction: column;
  gap: 16px;
}

.header {
  text-align: center;
}

.title {
  margin: 0;
  font-size: clamp(1.5rem, 4vw, 2rem);
  font-weight: 700;
  letter-spacing: 0.12em;
  color: #f0d9b0;
  text-shadow: 0 2px 8px rgba(0, 0, 0, 0.4);
}

.subtitle {
  margin: 4px 0 0;
  color: var(--text-muted);
  font-size: 0.9rem;
  letter-spacing: 0.25em;
  text-transform: uppercase;
}

.layout {
  display: grid;
  grid-template-columns: minmax(160px, 220px) minmax(280px, var(--board-w)) minmax(160px, 220px);
  gap: 16px;
  align-items: start;
  justify-content: center;
}

.panel {
  background: var(--bg-panel);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 14px;
  display: flex;
  flex-direction: column;
  gap: 14px;
  box-shadow: 0 8px 28px rgba(0, 0, 0, 0.35);
  min-height: 0;
}

.player-card {
  background: var(--bg-elevated);
  border-radius: 12px;
  padding: 12px;
  border: 1px solid var(--border);
}

.player-card.you {
  border-color: rgba(196, 92, 38, 0.55);
}

.player-card.opponent {
  border-color: rgba(168, 144, 112, 0.4);
}

.player-card.active-turn {
  box-shadow: 0 0 0 2px var(--accent);
}

.player-name {
  font-weight: 600;
  margin-bottom: 8px;
  font-size: 0.95rem;
}

.captured {
  min-height: 1.8rem;
  display: flex;
  flex-wrap: wrap;
  gap: 4px;
  align-items: center;
}

.captured-piece {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  width: 1.55rem;
  height: 1.55rem;
  border-radius: 50%;
  font-size: 0.72rem;
  font-weight: 700;
  background: var(--piece-cream);
  border: 2px solid;
  line-height: 1;
}

.captured-piece.red {
  color: var(--piece-red);
  border-color: var(--piece-red);
}

.captured-piece.black {
  color: var(--piece-black);
  border-color: #333;
}

.stat-block {
  display: flex;
  flex-direction: column;
  gap: 10px;
}

.stat {
  display: flex;
  flex-direction: column;
  gap: 4px;
}

.stat-label {
  color: var(--text-muted);
  font-size: 0.78rem;
  letter-spacing: 0.06em;
}

.stat-value {
  font-size: 1.05rem;
  font-weight: 600;
}

.stat-value.check {
  color: var(--danger);
}

.stat-value.mate {
  color: var(--danger);
}

.stat-value.draw {
  color: var(--warn);
}

.select {
  appearance: none;
  background: var(--bg-elevated);
  color: var(--text);
  border: 1px solid var(--border);
  border-radius: 10px;
  padding: 8px 12px;
  font: inherit;
  font-size: 0.95rem;
  cursor: pointer;
}

.select:focus {
  outline: 2px solid var(--accent);
  outline-offset: 1px;
}

.actions {
  display: flex;
  flex-direction: column;
  gap: 8px;
}

.btn {
  appearance: none;
  border: 1px solid var(--border);
  background: var(--bg-elevated);
  color: var(--text);
  border-radius: 12px;
  padding: 10px 14px;
  font: inherit;
  font-weight: 600;
  cursor: pointer;
  transition: background 0.15s, border-color 0.15s, transform 0.1s;
}

.btn:hover:not(:disabled) {
  border-color: var(--accent);
  background: #332618;
}

.btn:active:not(:disabled) {
  transform: scale(0.98);
}

.btn:disabled {
  opacity: 0.45;
  cursor: not-allowed;
}

.btn-primary {
  background: linear-gradient(180deg, var(--accent-hover), var(--accent));
  border-color: transparent;
  color: #fff;
  box-shadow: 0 6px 18px var(--glow);
}

.btn-primary:hover:not(:disabled) {
  filter: brightness(1.06);
  background: linear-gradient(180deg, var(--accent-hover), var(--accent));
}

.btn-danger {
  border-color: rgba(232, 93, 93, 0.45);
  color: #ffb0b0;
}

.board-section {
  position: relative;
  width: var(--board-w);
  max-width: 100%;
  justify-self: center;
}

.board-wrap {
  position: relative;
  width: 100%;
  aspect-ratio: 9 / 10;
  border-radius: 16px;
  padding: 12px;
  background: linear-gradient(145deg, #3e2a1a 0%, #24180f 100%);
  border: 1px solid #6b4a30;
  box-shadow:
    0 12px 40px rgba(0, 0, 0, 0.45),
    inset 0 1px 0 rgba(255, 255, 255, 0.06);
}

.board {
  display: block;
  width: 100%;
  height: 100%;
  border-radius: 8px;
  cursor: pointer;
  touch-action: none;
  user-select: none;
  box-shadow: 0 0 0 2px #5c4030;
  background: #c9a66b;
}

.thinking-badge {
  position: absolute;
  left: 50%;
  bottom: 18px;
  transform: translateX(-50%);
  background: rgba(26, 20, 16, 0.88);
  color: var(--warn);
  border: 1px solid var(--border);
  border-radius: 999px;
  padding: 6px 14px;
  font-size: 0.85rem;
  font-weight: 600;
  z-index: 5;
  pointer-events: none;
}

.thinking-badge.hidden {
  display: none;
}

.move-list-wrap {
  display: flex;
  flex-direction: column;
  gap: 8px;
  min-height: 180px;
  flex: 1;
}

.move-list {
  margin: 0;
  padding: 8px 10px;
  list-style: none;
  background: var(--bg-elevated);
  border: 1px solid var(--border);
  border-radius: 10px;
  max-height: 320px;
  overflow: auto;
  font-family: ui-monospace, "SF Mono", Menlo, "Noto Sans TC", monospace;
  font-size: 0.85rem;
  color: var(--text-muted);
  counter-reset: round;
}

.move-list li {
  display: grid;
  grid-template-columns: 2.2rem 1fr 1fr;
  gap: 6px;
  padding: 4px 0;
  border-bottom: 1px solid rgba(74, 58, 40, 0.45);
}

.move-list li:last-child {
  border-bottom: none;
}

.move-list li::before {
  counter-increment: round;
  content: counter(round) ".";
  color: var(--text-muted);
}

.move-list .san {
  color: var(--text);
}

.move-list .san.red {
  color: #ff8a7a;
}

.move-list .san.black {
  color: #d8c8b0;
}

.overlay {
  position: absolute;
  inset: 0;
  display: flex;
  align-items: center;
  justify-content: center;
  background: rgba(12, 8, 6, 0.72);
  backdrop-filter: blur(4px);
  border-radius: 16px;
  z-index: 20;
  padding: 16px;
}

.overlay.hidden {
  display: none;
}

.overlay-card {
  background: var(--bg-elevated);
  border: 1px solid var(--border);
  border-radius: 16px;
  padding: 22px 20px;
  text-align: center;
  max-width: 320px;
  width: 100%;
  box-shadow: 0 16px 40px rgba(0, 0, 0, 0.5);
}

.overlay-card h2 {
  margin: 0 0 8px;
  font-size: 1.35rem;
  color: #f0d9b0;
}

.overlay-card p {
  margin: 0 0 16px;
  color: var(--text-muted);
  line-height: 1.5;
}

.overlay-actions {
  display: flex;
  flex-direction: column;
  gap: 8px;
}

.help {
  text-align: center;
  color: var(--text-muted);
  font-size: 0.82rem;
  line-height: 1.5;
}

.help p {
  margin: 0;
}

@media (max-width: 900px) {
  .layout {
    grid-template-columns: 1fr;
    justify-items: center;
  }

  .panel-side {
    width: min(var(--board-w), 100%);
    max-width: 480px;
  }

  .board-section {
    order: -1;
  }

  .move-list {
    max-height: 140px;
  }

  .actions {
    flex-direction: row;
    flex-wrap: wrap;
  }

  .actions .btn {
    flex: 1 1 40%;
  }
}

@media (max-width: 420px) {
  body {
    padding: 10px 8px 18px;
  }

  .board-wrap {
    padding: 8px;
  }
}
