:root {
  --light-sq: #e8d9b5;
  --dark-sq: #6e4423;
  --highlight: #f7ec6b;
  --select: #6fa8dc99;
  --target: #3a7d3acc;
  --check: #e05555;
  --bg: #1e1e24;
  --panel-bg: #2a2a33;
  --text: #eee;
  --muted: #999;
  --accent: #5b8def;
  --frame-dark: #2c1a0d;
  --frame-light: #5c3a1e;
  --frame-label: #e8d9b0;
  --board-border: #1c1108;
}

* { box-sizing: border-box; }

.hidden { display: none !important; }

body {
  margin: 0;
  background: var(--bg);
  color: var(--text);
  font-family: -apple-system, "Segoe UI", Pretendard, sans-serif;
  min-height: 100vh;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 20px;
}

.app { width: 100%; max-width: 980px; }

header { text-align: center; margin-bottom: 16px; }
header h1 { margin: 0; font-size: 1.8rem; }

.layout {
  display: flex;
  gap: 24px;
  flex-wrap: wrap;
  justify-content: center;
  align-items: flex-start;
}

.board-wrap {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 12px;
}

.board-frame {
  display: inline-grid;
  grid-template-columns: 22px auto;
  grid-template-rows: auto 22px;
  grid-template-areas:
    "ranks board"
    ".     files";
  gap: 4px 6px;
  padding: 14px;
  background: linear-gradient(160deg, var(--frame-light), var(--frame-dark));
  border-radius: 8px;
  box-shadow: 0 12px 34px rgba(0,0,0,0.55), inset 0 0 0 2px rgba(255,255,255,0.06);
}

.rank-labels { grid-area: ranks; display: flex; flex-direction: column; }
.file-labels { grid-area: files; display: flex; flex-direction: row; }
.rank-labels span, .file-labels span {
  flex: 1;
  display: flex;
  align-items: center;
  justify-content: center;
  color: var(--frame-label);
  font-family: Georgia, "Times New Roman", serif;
  font-weight: 700;
  font-size: 13px;
}

.board {
  grid-area: board;
  display: grid;
  grid-template-columns: repeat(8, minmax(36px, 64px));
  grid-template-rows: repeat(8, minmax(36px, 64px));
  border: 3px solid var(--board-border);
  border-radius: 2px;
  width: min(80vw, 512px);
  height: min(80vw, 512px);
  aspect-ratio: 1 / 1;
}

/* ---- Board themes ---- */
.board-frame[data-theme="walnut"] {
  --light-sq: #e8d9b5; --dark-sq: #6e4423;
  --frame-light: #5c3a1e; --frame-dark: #2c1a0d;
  --frame-label: #e8d9b0; --board-border: #1c1108;
}
.board-frame[data-theme="green"] {
  --light-sq: #eeeed2; --dark-sq: #4b7339;
  --frame-light: #6b4a2c; --frame-dark: #33220f;
  --frame-label: #f0e6c8; --board-border: #241a0c;
}
.board-frame[data-theme="blue"] {
  --light-sq: #dbe7f2; --dark-sq: #37618e;
  --frame-light: #1b2a44; --frame-dark: #0a1120;
  --frame-label: #cfe0f5; --board-border: #060a14;
}
.board-frame[data-theme="marble"] {
  --light-sq: #f4f2ee; --dark-sq: #9a9a94;
  --frame-light: #6b6b66; --frame-dark: #34342f;
  --frame-label: #f2f0ea; --board-border: #232320;
}
.board-frame[data-theme="slate"] {
  --light-sq: #59606d; --dark-sq: #2c313b;
  --frame-light: #23262c; --frame-dark: #101216;
  --frame-label: #c7cdd6; --board-border: #08090b;
}
.board-frame[data-theme="coral"] {
  --light-sq: #fdece1; --dark-sq: #e08368;
  --frame-light: #7a4a3a; --frame-dark: #3d2419;
  --frame-label: #fbe3d3; --board-border: #2a180f;
}

.square {
  position: relative;
  display: flex;
  align-items: center;
  justify-content: center;
  cursor: pointer;
  user-select: none;
}
.square.light { background: var(--light-sq); }
.square.dark { background: var(--dark-sq); }

.square.selected { box-shadow: inset 0 0 0 4px var(--select); }
.square.last-move::after {
  content: "";
  position: absolute;
  inset: 0;
  background: var(--highlight);
  opacity: 0.45;
}
.square.king-check::after {
  content: "";
  position: absolute;
  inset: 0;
  background: radial-gradient(circle, var(--check) 0%, transparent 70%);
  opacity: 0.85;
}

.square.move-target::before {
  content: "";
  position: absolute;
  width: 28%;
  height: 28%;
  border-radius: 50%;
  background: var(--target);
  z-index: 2;
}
.square.capture-target::before {
  content: "";
  position: absolute;
  inset: 6%;
  border-radius: 50%;
  border: 4px solid var(--target);
  z-index: 2;
}

.piece-svg {
  width: 76%;
  height: 76%;
  overflow: visible;
  z-index: 3;
  pointer-events: none;
  filter: drop-shadow(0 2px 2px rgba(0,0,0,0.45));
}
.piece-svg.white-piece { fill: url(#whiteWood); stroke: #3b2412; stroke-width: 1; }
.piece-svg.black-piece { fill: url(#blackWood); stroke: #000; stroke-width: 1; }

/* ---- Piece themes (scoped to body so board + promotion picker match) ---- */
body[data-piece-theme="flat"] .piece-svg.white-piece { fill: #f6f1e4; stroke: #2a1a0a; stroke-width: 1.2; }
body[data-piece-theme="flat"] .piece-svg.black-piece { fill: #1c1c1c; stroke: #000; stroke-width: 1.2; }

body[data-piece-theme="neon"] .piece-svg { filter: none; }
body[data-piece-theme="neon"] .piece-svg.white-piece {
  fill: rgba(56,189,248,0.12); stroke: #38bdf8; stroke-width: 1.4;
  filter: drop-shadow(0 0 5px #38bdf8) drop-shadow(0 0 1px #bff0ff);
}
body[data-piece-theme="neon"] .piece-svg.black-piece {
  fill: rgba(251,113,133,0.12); stroke: #fb7185; stroke-width: 1.4;
  filter: drop-shadow(0 0 5px #fb7185) drop-shadow(0 0 1px #ffd0d8);
}

body[data-piece-theme="minimal"] .piece-svg { filter: drop-shadow(0 2px 3px rgba(0,0,0,0.4)); }
body[data-piece-theme="minimal"] .piece-svg.white-piece { fill: #f5f5f0; stroke: #1a1a1a; stroke-width: 1.6; }
body[data-piece-theme="minimal"] .piece-svg.black-piece { fill: #1a1a1a; stroke: #f5f5f0; stroke-width: 1.6; }

.piece-unicode {
  font-size: clamp(24px, 6vw, 46px);
  line-height: 1;
  z-index: 3;
  pointer-events: none;
  filter: drop-shadow(0 2px 2px rgba(0,0,0,0.4));
}
.piece-unicode.white-piece { color: #fdfdfd; }
.piece-unicode.black-piece { color: #151515; }

.status {
  font-size: 1.05rem;
  font-weight: 600;
  padding: 8px 16px;
  border-radius: 8px;
  background: var(--panel-bg);
  min-width: 200px;
  text-align: center;
}
.status.status-over { background: var(--check); color: #fff; }

.panel {
  background: var(--panel-bg);
  border-radius: 10px;
  padding: 18px;
  width: 260px;
  display: flex;
  flex-direction: column;
  gap: 16px;
}

.control-group { display: flex; flex-direction: column; gap: 6px; }
.control-group label { font-size: 0.8rem; color: var(--muted); }
.control-group select {
  padding: 8px;
  border-radius: 6px;
  border: 1px solid #444;
  background: #1e1e24;
  color: var(--text);
  font-size: 0.9rem;
}

.button-row { display: flex; gap: 8px; flex-wrap: wrap; }
.button-row button {
  flex: 1;
  padding: 9px 8px;
  border: none;
  border-radius: 6px;
  background: var(--accent);
  color: #fff;
  font-weight: 600;
  cursor: pointer;
  font-size: 0.85rem;
}
.button-row button:hover { filter: brightness(1.1); }
.button-row button:disabled { opacity: 0.5; cursor: not-allowed; }

.move-list-wrap { display: flex; flex-direction: column; gap: 6px; min-height: 0; }
.move-list-wrap h2 { margin: 0; font-size: 0.9rem; color: var(--muted); }
.move-list {
  list-style: none;
  margin: 0;
  padding: 8px;
  background: #1e1e24;
  border-radius: 6px;
  max-height: 260px;
  overflow-y: auto;
  font-family: "Courier New", monospace;
  font-size: 0.85rem;
}
.move-list li { padding: 2px 0; }

.modal {
  position: fixed;
  inset: 0;
  background: rgba(0,0,0,0.6);
  display: flex;
  align-items: center;
  justify-content: center;
  z-index: 10;
}
.modal.hidden { display: none; }
.modal-content {
  background: var(--panel-bg);
  padding: 20px;
  border-radius: 10px;
  text-align: center;
}
.modal-close {
  background: none;
  border: none;
  color: var(--text);
  font-size: 1.6rem;
  line-height: 1;
  cursor: pointer;
  padding: 2px 6px;
}
.modal-close:hover { color: var(--check); }

.saved-games-content {
  width: min(92vw, 480px);
  max-height: 82vh;
  display: flex;
  flex-direction: column;
  text-align: left;
}
.saved-games-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
}
.saved-games-header h2 { margin: 0; font-size: 1.2rem; }
.integ-desc { color: var(--muted); font-size: 0.82rem; margin: 6px 0 12px; }

.saved-games-list {
  list-style: none;
  margin: 0;
  padding: 0;
  overflow-y: auto;
  display: flex;
  flex-direction: column;
  gap: 8px;
  flex: 1;
}
.saved-games-list .empty-note { color: var(--muted); padding: 12px 0; text-align: center; }
.saved-game-item {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 10px;
  background: #1e1e24;
  border-radius: 8px;
  padding: 10px 12px;
}
.saved-game-info { font-size: 0.82rem; line-height: 1.5; }
.saved-game-btns { display: flex; gap: 6px; flex-shrink: 0; }
.saved-game-btns button {
  padding: 7px 10px;
  border: none;
  border-radius: 6px;
  background: var(--accent);
  color: #fff;
  font-size: 0.8rem;
  cursor: pointer;
  font-weight: 600;
}
.saved-game-btns button:hover { filter: brightness(1.1); }
.danger-btn { background: var(--check) !important; }

#replayControls { display: flex; flex-direction: column; gap: 14px; }
.replay-label { margin: 0; font-weight: 700; color: var(--accent); }
.replay-info { font-size: 0.85rem; color: var(--muted); }

.promo-choices { display: flex; gap: 10px; margin-top: 10px; }
.promo-btn {
  font-size: 2rem;
  width: 60px;
  height: 60px;
  border-radius: 8px;
  border: 2px solid var(--accent);
  background: #1e1e24;
  color: var(--text);
  cursor: pointer;
}
.promo-btn:hover { background: var(--accent); }

@media (max-width: 700px) {
  .layout { flex-direction: column; align-items: center; }
  .panel { width: 100%; max-width: 512px; }
}
