:root {
  --bg0: #1a120b;
  --bg1: #2a1c12;
  --bg2: #3d2818;
  --wood: #8b5a2b;
  --wood-light: #c4a574;
  --wood-dark: #5c3a1e;
  --board: #d4a574;
  --board-line: #6b4226;
  --red: #c23b22;
  --red-deep: #8b1e12;
  --black: #1c1c1c;
  --gold: #e8c872;
  --gold-dim: #a68b3d;
  --ink: #f5efe6;
  --muted: #b8a48a;
  --panel: rgba(28, 18, 10, 0.88);
  --ok: #3d9b6a;
  --danger: #c23b22;
  --shadow: 0 12px 40px rgba(0, 0, 0, 0.45);
  --radius: 14px;
  --font-serif: "Noto Serif TC", "Songti TC", "SimSun", serif;
  --font-sans: "Noto Sans TC", "Microsoft JhengHei", sans-serif;
}

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

html,
body {
  margin: 0;
  min-height: 100%;
  font-family: var(--font-sans);
  color: var(--ink);
  background:
    radial-gradient(ellipse 80% 60% at 50% -10%, rgba(232, 200, 114, 0.12), transparent 55%),
    radial-gradient(ellipse 50% 40% at 100% 100%, rgba(194, 59, 34, 0.1), transparent 50%),
    linear-gradient(165deg, var(--bg0), var(--bg1) 45%, #120c08);
}

button,
select {
  font: inherit;
  color: inherit;
}

button {
  cursor: pointer;
  border: none;
  background: none;
}

.app {
  max-width: 1200px;
  margin: 0 auto;
  padding: 1rem 1.25rem 2rem;
  min-height: 100vh;
  display: flex;
  flex-direction: column;
}

/* Header */
.header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 1rem;
  margin-bottom: 1.25rem;
}

.brand {
  display: flex;
  align-items: center;
  gap: 0.85rem;
}

.brand-mark {
  width: 52px;
  height: 52px;
  border-radius: 50%;
  display: grid;
  place-items: center;
  font-family: var(--font-serif);
  font-weight: 900;
  font-size: 1.55rem;
  color: var(--red);
  background: radial-gradient(circle at 35% 30%, #fff8e8, var(--wood-light) 55%, var(--wood));
  border: 3px solid var(--gold);
  box-shadow: 0 4px 16px rgba(0, 0, 0, 0.35), inset 0 1px 0 rgba(255, 255, 255, 0.45);
}

.brand h1 {
  margin: 0;
  font-family: var(--font-serif);
  font-size: 1.75rem;
  font-weight: 900;
  letter-spacing: 0.12em;
  color: var(--gold);
}

.tagline {
  margin: 0.15rem 0 0;
  font-size: 0.82rem;
  color: var(--muted);
  letter-spacing: 0.04em;
}

.header-actions {
  display: flex;
  gap: 0.5rem;
}

/* Buttons */
.btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 0.4rem;
  padding: 0.55rem 1rem;
  border-radius: 999px;
  background: rgba(255, 255, 255, 0.06);
  border: 1px solid rgba(232, 200, 114, 0.28);
  color: var(--ink);
  transition: background 0.15s, transform 0.1s, border-color 0.15s;
}

.btn:hover:not(:disabled) {
  background: rgba(232, 200, 114, 0.12);
  border-color: rgba(232, 200, 114, 0.5);
}

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

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

.btn.primary {
  background: linear-gradient(135deg, #c9a227, #8b6914);
  border-color: transparent;
  color: #1a120b;
  font-weight: 700;
  box-shadow: 0 4px 18px rgba(201, 162, 39, 0.35);
}

.btn.primary:hover:not(:disabled) {
  background: linear-gradient(135deg, #e0b82e, #a07a18);
}

.btn.ghost {
  background: transparent;
}

.btn.danger {
  border-color: rgba(194, 59, 34, 0.45);
  color: #f0a090;
}

.btn.danger:hover:not(:disabled) {
  background: rgba(194, 59, 34, 0.15);
}

.btn.lg {
  padding: 0.85rem 2rem;
  font-size: 1.05rem;
  min-width: 200px;
}

/* Setup */
.main {
  flex: 1;
}

.setup {
  max-width: 640px;
  margin: 2rem auto;
  padding: 2rem;
  background: var(--panel);
  border: 1px solid rgba(232, 200, 114, 0.18);
  border-radius: calc(var(--radius) + 4px);
  box-shadow: var(--shadow);
  text-align: center;
}

.setup h2 {
  margin: 0 0 1.5rem;
  font-family: var(--font-serif);
  font-weight: 700;
  color: var(--gold);
  letter-spacing: 0.08em;
}

.mode-grid {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 1rem;
  margin-bottom: 1.25rem;
}

.mode-card {
  text-align: left;
  padding: 1.25rem;
  border-radius: var(--radius);
  background: rgba(255, 255, 255, 0.03);
  border: 2px solid rgba(232, 200, 114, 0.15);
  transition: border-color 0.15s, background 0.15s, transform 0.15s;
}

.mode-card:hover {
  border-color: rgba(232, 200, 114, 0.4);
  transform: translateY(-2px);
}

.mode-card.selected {
  border-color: var(--gold);
  background: rgba(232, 200, 114, 0.1);
  box-shadow: 0 0 0 1px rgba(232, 200, 114, 0.25);
}

.mode-icon {
  width: 42px;
  height: 42px;
  border-radius: 10px;
  display: grid;
  place-items: center;
  font-family: var(--font-serif);
  font-weight: 900;
  font-size: 1.2rem;
  margin-bottom: 0.75rem;
  background: linear-gradient(145deg, var(--wood-light), var(--wood));
  color: var(--bg0);
}

.mode-card h3 {
  margin: 0 0 0.35rem;
  font-size: 1.05rem;
}

.mode-card p {
  margin: 0;
  font-size: 0.85rem;
  color: var(--muted);
}

.setup-options {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 1rem;
  margin-bottom: 1.5rem;
  text-align: left;
}

.field {
  display: flex;
  flex-direction: column;
  gap: 0.4rem;
  font-size: 0.88rem;
  color: var(--muted);
}

/* Custom select — native <option> colors are unreliable on Windows Chrome */
.custom-select {
  position: relative;
}

.custom-select-trigger {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 0.5rem;
  width: 100%;
  padding: 0.55rem 0.75rem;
  border-radius: 10px;
  background: rgba(0, 0, 0, 0.5);
  border: 1px solid rgba(232, 200, 114, 0.25);
  color: var(--ink);
  text-align: left;
  outline: none;
  transition: border-color 0.15s ease, box-shadow 0.15s ease;
}

.custom-select-trigger:hover {
  border-color: rgba(232, 200, 114, 0.45);
}

.custom-select-trigger:focus,
.custom-select.open .custom-select-trigger {
  border-color: var(--gold);
  box-shadow: 0 0 0 2px rgba(232, 200, 114, 0.18);
}

.custom-select-label {
  flex: 1;
  min-width: 0;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.custom-select-caret {
  flex-shrink: 0;
  font-size: 0.75rem;
  color: var(--gold-dim);
  line-height: 1;
}

.custom-select.open .custom-select-caret {
  transform: rotate(180deg);
}

.custom-select-menu {
  position: absolute;
  z-index: 40;
  top: calc(100% + 4px);
  left: 0;
  right: 0;
  margin: 0;
  padding: 0.35rem;
  list-style: none;
  border-radius: 10px;
  background: #2a1c12;
  border: 1px solid rgba(232, 200, 114, 0.35);
  box-shadow: 0 10px 28px rgba(0, 0, 0, 0.55);
}

.custom-select-menu[hidden] {
  display: none;
}

.custom-select-menu [role="option"] {
  padding: 0.5rem 0.7rem;
  border-radius: 8px;
  color: #f5efe6;
  cursor: pointer;
  outline: none;
}

.custom-select-menu [role="option"]:hover,
.custom-select-menu [role="option"]:focus {
  background: rgba(232, 200, 114, 0.16);
  color: #fff8e8;
}

.custom-select-menu [role="option"][aria-selected="true"] {
  background: rgba(232, 200, 114, 0.28);
  color: #ffe9a8;
  font-weight: 600;
}

/* Play layout */
.play {
  display: grid;
  grid-template-columns: 200px 1fr 220px;
  gap: 1rem;
  align-items: start;
}

.side-panel {
  display: flex;
  flex-direction: column;
  gap: 0.85rem;
}

.player-card {
  padding: 1rem;
  border-radius: var(--radius);
  background: var(--panel);
  border: 1px solid rgba(232, 200, 114, 0.15);
  display: grid;
  grid-template-columns: auto 1fr;
  gap: 0.55rem 0.7rem;
  align-items: center;
}

.player-card.active {
  border-color: var(--gold);
  box-shadow: 0 0 0 1px rgba(232, 200, 114, 0.35), 0 0 24px rgba(232, 200, 114, 0.12);
}

.player-color {
  width: 14px;
  height: 14px;
  border-radius: 50%;
  grid-row: span 1;
}

.red-dot {
  background: var(--red);
  box-shadow: 0 0 8px rgba(194, 59, 34, 0.7);
}

.black-dot {
  background: #e8e4dc;
  box-shadow: 0 0 8px rgba(255, 255, 255, 0.25);
}

.player-name {
  font-family: var(--font-serif);
  font-weight: 700;
  font-size: 1.05rem;
}

.player-meta {
  font-size: 0.78rem;
  color: var(--muted);
}

.captured-row {
  grid-column: 1 / -1;
  display: flex;
  flex-wrap: wrap;
  gap: 0.25rem;
  min-height: 28px;
}

.captured-piece {
  width: 26px;
  height: 26px;
  border-radius: 50%;
  display: grid;
  place-items: center;
  font-family: var(--font-serif);
  font-size: 0.72rem;
  font-weight: 700;
  opacity: 0.9;
}

.captured-piece.red {
  color: var(--red);
  background: #f5ebe0;
  border: 1px solid #c9a070;
}

.captured-piece.black {
  color: #111;
  background: #e8e4dc;
  border: 1px solid #888;
}

.stats {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 0.5rem;
}

.stat {
  padding: 0.65rem 0.75rem;
  border-radius: 12px;
  background: var(--panel);
  border: 1px solid rgba(232, 200, 114, 0.12);
  display: flex;
  flex-direction: column;
  gap: 0.2rem;
}

.stat span {
  font-size: 0.72rem;
  color: var(--muted);
}

.stat strong {
  font-size: 0.95rem;
  font-weight: 700;
}

.log-box {
  flex: 1;
  min-height: 180px;
  max-height: 320px;
  overflow: auto;
  padding: 0.75rem;
  border-radius: var(--radius);
  background: var(--panel);
  border: 1px solid rgba(232, 200, 114, 0.12);
}

.log-title {
  font-size: 0.78rem;
  color: var(--muted);
  margin-bottom: 0.5rem;
  letter-spacing: 0.08em;
}

.log-list {
  margin: 0;
  padding: 0 0 0 1.2rem;
  font-size: 0.8rem;
  line-height: 1.55;
  color: #e0d4c0;
}

.log-list li {
  margin-bottom: 0.2rem;
}

.play-actions {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 0.5rem;
}

/* Board */
.board-wrap {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 0.75rem;
}

.status-bar {
  width: 100%;
  max-width: 560px;
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 0.75rem;
  padding: 0.65rem 1rem;
  border-radius: 12px;
  background: var(--panel);
  border: 1px solid rgba(232, 200, 114, 0.15);
}

.status-text {
  font-size: 0.92rem;
  font-weight: 500;
}

.turn-badge {
  padding: 0.25rem 0.7rem;
  border-radius: 999px;
  font-size: 0.78rem;
  font-weight: 700;
  background: rgba(232, 200, 114, 0.15);
  border: 1px solid rgba(232, 200, 114, 0.35);
  color: var(--gold);
  white-space: nowrap;
}

.turn-badge.red {
  color: #ffb4a8;
  border-color: rgba(194, 59, 34, 0.5);
  background: rgba(194, 59, 34, 0.15);
}

.turn-badge.black {
  color: #f0ece4;
  border-color: rgba(255, 255, 255, 0.25);
  background: rgba(255, 255, 255, 0.08);
}

.board {
  --cell: min(11.5vw, 68px);
  display: grid;
  grid-template-columns: repeat(8, var(--cell));
  grid-template-rows: repeat(4, var(--cell));
  gap: 0;
  padding: 14px;
  border-radius: 16px;
  background:
    linear-gradient(145deg, #6b4226, #4a2c14 40%, #3d2410);
  box-shadow:
    var(--shadow),
    inset 0 1px 0 rgba(255, 220, 160, 0.25),
    0 0 0 3px #2a180c;
  position: relative;
}

.board::before {
  content: "";
  position: absolute;
  inset: 8px;
  border-radius: 10px;
  border: 1px solid rgba(232, 200, 114, 0.25);
  pointer-events: none;
}

.cell {
  position: relative;
  display: grid;
  place-items: center;
  background:
    linear-gradient(160deg, rgba(255, 230, 180, 0.12), transparent 50%),
    #c9955a;
  border: 1px solid rgba(80, 45, 18, 0.45);
  cursor: pointer;
  transition: filter 0.12s, box-shadow 0.12s;
  user-select: none;
  -webkit-tap-highlight-color: transparent;
}

.cell:nth-child(16n + 1),
.cell:nth-child(16n + 3),
.cell:nth-child(16n + 5),
.cell:nth-child(16n + 7),
.cell:nth-child(16n + 10),
.cell:nth-child(16n + 12),
.cell:nth-child(16n + 14),
.cell:nth-child(16n + 16) {
  background:
    linear-gradient(160deg, rgba(255, 230, 180, 0.08), transparent 50%),
    #b8844a;
}

.cell:hover:not(.disabled) {
  filter: brightness(1.08);
}

.cell.selected {
  box-shadow: inset 0 0 0 3px var(--gold);
  z-index: 1;
}

.cell.legal {
  box-shadow: inset 0 0 0 3px rgba(61, 155, 106, 0.85);
  z-index: 1;
}

.cell.legal::after {
  content: "";
  position: absolute;
  width: 18%;
  height: 18%;
  border-radius: 50%;
  background: rgba(61, 155, 106, 0.75);
  pointer-events: none;
}

.cell.legal.has-piece::after {
  width: auto;
  height: auto;
  inset: 10%;
  border-radius: 50%;
  background: transparent;
  border: 3px solid rgba(194, 59, 34, 0.75);
  box-shadow: 0 0 12px rgba(194, 59, 34, 0.35);
}

.cell.last-move {
  background-image: linear-gradient(160deg, rgba(232, 200, 114, 0.35), rgba(232, 200, 114, 0.12));
}

.piece {
  width: 82%;
  height: 82%;
  border-radius: 50%;
  display: grid;
  place-items: center;
  font-family: var(--font-serif);
  font-weight: 900;
  font-size: calc(var(--cell) * 0.42);
  line-height: 1;
  transition: transform 0.18s ease, box-shadow 0.18s;
  position: relative;
  z-index: 2;
}

.piece.hidden-piece {
  background:
    radial-gradient(circle at 32% 28%, #5a3a22, #2a180c 70%);
  border: 2px solid #8b6a3a;
  box-shadow:
    0 3px 6px rgba(0, 0, 0, 0.4),
    inset 0 1px 0 rgba(255, 220, 150, 0.15);
  color: transparent;
}

.piece.hidden-piece::before {
  content: "暗";
  color: rgba(232, 200, 114, 0.35);
  font-size: calc(var(--cell) * 0.28);
  font-weight: 700;
}

.piece.red {
  color: var(--red-deep);
  background:
    radial-gradient(circle at 32% 28%, #fff9f0, #f0d8b8 55%, #d4a574);
  border: 2.5px solid var(--red);
  box-shadow:
    0 3px 8px rgba(0, 0, 0, 0.35),
    inset 0 1px 0 rgba(255, 255, 255, 0.55),
    0 0 0 1px rgba(139, 30, 18, 0.25);
  text-shadow: 0 1px 0 rgba(255, 255, 255, 0.35);
}

.piece.black {
  color: #111;
  background:
    radial-gradient(circle at 32% 28%, #ffffff, #e8e4dc 55%, #c8c4bc);
  border: 2.5px solid #2a2a2a;
  box-shadow:
    0 3px 8px rgba(0, 0, 0, 0.35),
    inset 0 1px 0 rgba(255, 255, 255, 0.7);
}

.piece.flip-anim {
  animation: flipIn 0.4s ease;
}

@keyframes flipIn {
  0% {
    transform: rotateY(90deg) scale(0.85);
  }
  100% {
    transform: rotateY(0) scale(1);
  }
}

.piece.capture-pulse {
  animation: capturePulse 0.35s ease;
}

@keyframes capturePulse {
  0% {
    transform: scale(1);
  }
  40% {
    transform: scale(1.15);
  }
  100% {
    transform: scale(1);
  }
}

.board-hint {
  font-size: 0.82rem;
  color: var(--muted);
  text-align: center;
  min-height: 1.2em;
}

.footer {
  margin-top: 1.5rem;
  text-align: center;
  font-size: 0.78rem;
  color: rgba(184, 164, 138, 0.7);
}

/* Modal */
.modal {
  border: none;
  padding: 0;
  background: transparent;
  max-width: min(520px, 92vw);
  color: var(--ink);
}

.modal::backdrop {
  background: rgba(0, 0, 0, 0.65);
  backdrop-filter: blur(4px);
}

.modal-card {
  padding: 1.5rem 1.5rem 1.25rem;
  border-radius: 16px;
  background: linear-gradient(165deg, #2e1e12, #1a120b);
  border: 1px solid rgba(232, 200, 114, 0.3);
  box-shadow: var(--shadow);
}

.modal-card h2 {
  margin: 0 0 1rem;
  font-family: var(--font-serif);
  color: var(--gold);
  letter-spacing: 0.08em;
}

.rules-body {
  max-height: min(55vh, 420px);
  overflow: auto;
  margin-bottom: 1.25rem;
  font-size: 0.9rem;
  line-height: 1.6;
  color: #e0d4c0;
}

.rules-body h3 {
  margin: 1rem 0 0.4rem;
  font-size: 0.95rem;
  color: var(--gold-dim);
}

.rules-body p,
.rules-body ul {
  margin: 0.35rem 0;
}

.rules-body ul {
  padding-left: 1.2rem;
}

.end-card {
  text-align: center;
}

.end-icon {
  width: 72px;
  height: 72px;
  margin: 0 auto 0.75rem;
  border-radius: 50%;
  display: grid;
  place-items: center;
  font-family: var(--font-serif);
  font-weight: 900;
  font-size: 1.8rem;
  color: var(--bg0);
  background: linear-gradient(145deg, var(--gold), #a07a18);
  box-shadow: 0 6px 24px rgba(201, 162, 39, 0.4);
}

.end-card h2 {
  margin-bottom: 0.4rem;
}

.end-card p {
  margin: 0 0 1.25rem;
  color: var(--muted);
}

.end-actions {
  display: flex;
  gap: 0.6rem;
  justify-content: center;
  flex-wrap: wrap;
}

/* Thinking overlay */
.board.thinking {
  pointer-events: none;
  opacity: 0.92;
}

.board.thinking::after {
  content: "AI 思考中…";
  position: absolute;
  left: 50%;
  top: 50%;
  transform: translate(-50%, -50%);
  padding: 0.5rem 1rem;
  border-radius: 999px;
  background: rgba(0, 0, 0, 0.7);
  color: var(--gold);
  font-size: 0.9rem;
  z-index: 10;
  white-space: nowrap;
  pointer-events: none;
}

/* Responsive */
@media (max-width: 960px) {
  .play {
    grid-template-columns: 1fr;
  }

  .side-panel.left,
  .side-panel.right {
    flex-direction: row;
    flex-wrap: wrap;
  }

  .player-card {
    flex: 1;
    min-width: 180px;
  }

  .log-box {
    width: 100%;
    max-height: 160px;
  }

  .play-actions {
    width: 100%;
  }
}

@media (max-width: 560px) {
  .mode-grid,
  .setup-options {
    grid-template-columns: 1fr;
  }

  .setup {
    padding: 1.25rem;
    margin: 0.5rem auto;
  }

  .brand-mark {
    width: 44px;
    height: 44px;
    font-size: 1.3rem;
  }

  .brand h1 {
    font-size: 1.4rem;
  }

  .board {
    --cell: min(11vw, 52px);
    padding: 10px;
  }
}
