:root {
  --bg: #0f1220;
  --bg-elevated: #1a1f35;
  --bg-panel: #151a2e;
  --border: #2a3355;
  --text: #e8ecf8;
  --text-muted: #8b95b5;
  --accent: #5b8cff;
  --accent-hover: #7aa3ff;
  --danger: #ff6b7a;
  --warn: #ffc857;
  --success: #5ddea6;
  --glow: rgba(91, 140, 255, 0.35);
  --light-sq: #f0d9b5;
  --dark-sq: #b58863;
  --light-sq-hi: #f7e6c8;
  --dark-sq-hi: #c49a74;
  --select: rgba(120, 190, 90, 0.55);
  --move: rgba(100, 70, 40, 0.35);
  --last: rgba(246, 204, 90, 0.55);
  --check: rgba(220, 80, 70, 0.7);
  --piece-white: #fffef5;
  --piece-black: #1a1208;
  --board-frame: #5d4030;
  --radius: 14px;
  --font: "Segoe UI", "PingFang TC", "Noto Sans TC", "Microsoft JhengHei",
    system-ui, sans-serif;
  --piece-size: clamp(1.7rem, 7.2vw, 3.1rem);
  --board-size: min(92vw, 520px, 72vh);
}

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

html,
body {
  margin: 0;
  padding: 0;
  min-height: 100%;
  background: radial-gradient(ellipse at top, #1a2240 0%, var(--bg) 55%);
  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.08em;
}

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

.layout {
  display: grid;
  grid-template-columns: minmax(160px, 220px) minmax(280px, var(--board-size)) 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.25);
  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(91, 140, 255, 0.45);
}

.player-card.opponent {
  border-color: rgba(255, 107, 122, 0.35);
}

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

.captured {
  min-height: 1.6rem;
  font-size: 1.15rem;
  letter-spacing: 0.02em;
  line-height: 1.4;
  word-break: break-all;
  color: var(--text);
}

.material {
  margin-top: 4px;
  color: var(--text-muted);
  font-size: 0.85rem;
  min-height: 1.1rem;
}

.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: #1e2644;
}

.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));
}

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

.board-wrap {
  position: relative;
  width: 100%;
  aspect-ratio: 1;
  border-radius: 16px;
  padding: 10px 10px 28px 28px;
  background: linear-gradient(145deg, #3e2a1f 0%, #2a1c14 100%);
  border: 1px solid #6b4a34;
  box-shadow: 0 12px 40px rgba(0, 0, 0, 0.35), inset 0 1px 0 rgba(255, 255, 255, 0.06);
}

.board {
  width: 100%;
  height: 100%;
  display: grid;
  grid-template-columns: repeat(8, 1fr);
  grid-template-rows: repeat(8, 1fr);
  border-radius: 8px;
  overflow: hidden;
  box-shadow: 0 0 0 2px var(--board-frame);
  user-select: none;
}

.square {
  position: relative;
  display: flex;
  align-items: center;
  justify-content: center;
  cursor: pointer;
  font-size: var(--piece-size);
  line-height: 1;
  transition: background 0.12s;
}

.square.light {
  background: var(--light-sq);
}

.square.dark {
  background: var(--dark-sq);
}

.square.selected {
  box-shadow: inset 0 0 0 3px #2ecc71;
  background: var(--select) !important;
}

.square.last {
  background: var(--last) !important;
}

.square.check {
  background: var(--check) !important;
}

.square.legal::after {
  content: "";
  position: absolute;
  width: 28%;
  height: 28%;
  border-radius: 50%;
  background: rgba(60, 40, 20, 0.4);
  pointer-events: none;
}

.square.legal.capture::after {
  width: 82%;
  height: 82%;
  background: transparent;
  border: 4px solid rgba(60, 40, 20, 0.45);
  border-radius: 50%;
}

.square.legal:hover,
.square:hover:not(.selected) {
  filter: brightness(1.06);
}

.piece {
  pointer-events: none;
  z-index: 1;
  font-weight: 600;
  line-height: 1;
}

.piece-w {
  color: var(--piece-white);
  text-shadow:
    -1px -1px 0 #2b1d12,
     1px -1px 0 #2b1d12,
    -1px  1px 0 #2b1d12,
     1px  1px 0 #2b1d12,
     0 2px 3px rgba(0, 0, 0, 0.35);
}

.piece-b {
  color: var(--piece-black);
  text-shadow:
    -0.5px -0.5px 0 rgba(255, 248, 230, 0.35),
     0.5px -0.5px 0 rgba(255, 248, 230, 0.35),
    -0.5px  0.5px 0 rgba(255, 248, 230, 0.35),
     0.5px  0.5px 0 rgba(255, 248, 230, 0.35),
     0 2px 2px rgba(0, 0, 0, 0.25);
}

.captured .piece-w,
.captured .piece-b {
  font-size: 1.15rem;
  margin-right: 1px;
}

.promo-choices button.piece-w {
  color: var(--piece-white);
  text-shadow:
    -1px -1px 0 #2b1d12,
     1px -1px 0 #2b1d12,
    -1px  1px 0 #2b1d12,
     1px  1px 0 #2b1d12;
}

.coords-files {
  position: absolute;
  left: 28px;
  right: 10px;
  bottom: 6px;
  display: grid;
  grid-template-columns: repeat(8, 1fr);
  text-align: center;
  color: #e8c9a0;
  font-size: 0.72rem;
  pointer-events: none;
}

.coords-ranks {
  position: absolute;
  top: 10px;
  bottom: 28px;
  left: 6px;
  display: grid;
  grid-template-rows: repeat(8, 1fr);
  align-items: center;
  color: #e8c9a0;
  font-size: 0.72rem;
  pointer-events: 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: 280px;
  overflow: auto;
  font-family: ui-monospace, "SF Mono", Menlo, Consolas, monospace;
  font-size: 0.85rem;
  color: var(--text-muted);
  counter-reset: move;
}

.move-list li {
  display: grid;
  grid-template-columns: 2rem 1fr 1fr;
  gap: 6px;
  padding: 3px 0;
  border-bottom: 1px solid rgba(42, 51, 85, 0.45);
}

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

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

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

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

.overlay.hidden,
.promo-modal.hidden {
  display: none;
}

.overlay-card,
.promo-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.4);
}

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

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

.promo-card p {
  margin: 0 0 12px;
  font-weight: 600;
}

.promo-choices {
  display: grid;
  grid-template-columns: repeat(4, 1fr);
  gap: 8px;
}

.promo-choices button {
  appearance: none;
  border: 1px solid var(--border);
  background: var(--bg-panel);
  border-radius: 12px;
  font-size: 2rem;
  padding: 10px 0;
  cursor: pointer;
  color: var(--text);
}

.promo-choices button:hover {
  border-color: var(--accent);
  background: #1e2644;
}

.thinking #board {
  opacity: 0.92;
}

.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-size), 100%);
    max-width: 520px;
  }

  .board-section {
    order: -1;
  }

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

  .actions {
    flex-direction: row;
  }

  .actions .btn {
    flex: 1;
  }
}

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

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

  .coords-files {
    left: 22px;
    right: 8px;
  }
}
