:root {
  color-scheme: dark;
  --ink: #f5f3ed;
  --muted: #bbb8b0;
  --panel: #383632;
  --panel-border: #504d47;
  --accent: #aeca83;
  --accent-bright: #c4dc9f;
  --dark-square: #849e62;
  --light-square: #f0f1d8;
  --board-width: min(40vw, calc((100vh - 190px) / 2), 430px);
  font-family: Inter, ui-sans-serif, system-ui, -apple-system, BlinkMacSystemFont, "Segoe UI", sans-serif;
}

* { box-sizing: border-box; }

body {
  margin: 0;
  min-height: 100vh;
  color: var(--ink);
  background: #2d2b28;
}

button, select { font: inherit; }

.page-glow {
  position: fixed;
  inset: 0;
  pointer-events: none;
  opacity: 0;
}

.site-header {
  position: relative;
  z-index: 1;
  max-width: 1180px;
  margin: 0 auto;
  padding: 24px 28px 12px;
  display: flex;
  justify-content: space-between;
  align-items: center;
}

.eyebrow {
  margin: 0 0 2px;
  color: var(--accent);
  text-transform: uppercase;
  letter-spacing: .17em;
  font-size: .75rem;
  font-weight: 750;
}

h1 {
  margin: 0;
  font-family: Georgia, "Times New Roman", serif;
  font-size: clamp(2rem, 4.2vw, 3.25rem);
  font-weight: 600;
  line-height: 1;
  letter-spacing: -.04em;
}

.wasm-badge {
  display: inline-flex;
  align-items: center;
  gap: 9px;
  padding: 9px 13px;
  border: 1px solid var(--panel-border);
  border-radius: 999px;
  background: #373531;
  color: var(--muted);
  font-size: .8rem;
  font-weight: 650;
}

.wasm-badge span {
  width: 7px;
  height: 7px;
  border-radius: 50%;
  background: var(--accent-bright);
  box-shadow: 0 0 0 4px rgba(196, 220, 159, .12);
}

.game-layout {
  position: relative;
  z-index: 1;
  max-width: 1180px;
  margin: 0 auto;
  padding: 8px 28px 48px;
  display: grid;
  grid-template-columns: minmax(330px, 510px) minmax(300px, 410px);
  gap: clamp(34px, 6vw, 78px);
  align-items: start;
}

.board-section { width: var(--board-width); justify-self: center; }

.player-strip {
  display: flex;
  align-items: center;
  gap: 11px;
  padding: 10px 2px;
}

.player-strip strong, .player-strip span { display: block; }
.player-strip strong { font-size: .95rem; }
.player-strip span { color: var(--muted); font-size: .78rem; margin-top: 1px; }
.player-strip .turn-dot { margin-left: auto; width: 8px; height: 8px; border-radius: 50%; background: transparent; }
.player-strip.active .turn-dot { background: var(--accent-bright); box-shadow: 0 0 0 5px rgba(196, 220, 159, .14); }

.player-icon {
  display: grid;
  place-items: center;
  width: 38px;
  height: 38px;
  border-radius: 50%;
  color: #5c5b58;
  background: #a7a49d;
  font: 1.55rem/1 Georgia, serif;
  -webkit-text-stroke: 1px #22211f;
}

.player-icon.light { color: #fff; background: #efefe7; }

.board-wrap {
  position: relative;
  padding: 0;
  border: 0;
  border-radius: 14px;
  overflow: hidden;
  background: var(--dark-square);
  box-shadow: 0 18px 36px rgba(0,0,0,.28);
}

.board {
  width: 100%;
  aspect-ratio: 1 / 2;
  container-type: inline-size;
  display: grid;
  grid-template-columns: repeat(4, 1fr);
  grid-template-rows: repeat(8, 1fr);
  overflow: hidden;
  border-radius: 0;
}

.square {
  position: relative;
  display: grid;
  place-items: center;
  width: 100%;
  min-width: 0;
  min-height: 0;
  overflow: hidden;
  border: 0;
  padding: 0;
  color: #292825;
  cursor: pointer;
  isolation: isolate;
}

.square.light { background: var(--light-square); }
.square.dark { background: var(--dark-square); }
.square:hover { filter: brightness(1.045); }
.square:focus-visible { outline: 3px solid var(--accent-bright); outline-offset: -3px; z-index: 5; }
.square.selected::before, .square.last::before {
  content: "";
  position: absolute;
  inset: 0;
  background: rgba(239, 198, 81, .55);
  z-index: -1;
}
.square.last::before { background: rgba(237, 198, 89, .34); }
.square.target::after {
  content: "";
  position: absolute;
  width: 24%;
  aspect-ratio: 1;
  border-radius: 50%;
  background: rgba(14, 26, 22, .35);
  z-index: -1;
}
.square.capture::after {
  width: 76%;
  background: transparent;
  border: clamp(3px, .65vw, 6px) solid rgba(19, 29, 25, .35);
}

.piece {
  font-family: "Arial Unicode MS", "Apple Symbols", "Segoe UI Symbol", "Noto Sans Symbols 2", Georgia, serif;
  font-size: clamp(1.9rem, 22cqw, 4.7rem);
  line-height: 1;
  transform: translateY(-2%);
  user-select: none;
}

.piece.white {
  color: #fafafa;
  -webkit-text-stroke: 1.35px #41403d;
  text-shadow: 0 3px 0 #d9d9d4, 0 5px 3px rgba(0,0,0,.22);
}
.piece.black {
  color: #5b5a57;
  -webkit-text-stroke: 1.35px #242321;
  text-shadow: 0 3px 0 #3b3a38, 0 5px 3px rgba(0,0,0,.26);
}

.coordinate {
  position: absolute;
  z-index: 2;
  font-size: clamp(.65rem, 3.5cqw, .75rem);
  font-weight: 800;
  opacity: .72;
  pointer-events: none;
}
.coordinate.rank { top: 4px; left: 5px; }
.coordinate.file { bottom: 3px; right: 5px; }
.square.light .coordinate { color: var(--dark-square); }
.square.dark .coordinate { color: var(--light-square); }

.thinking {
  position: absolute;
  inset: 0;
  z-index: 10;
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 6px;
  background: rgba(45, 43, 40, .36);
  backdrop-filter: blur(1px);
}
.thinking[hidden] { display: none; }
.thinking span { width: 7px; height: 7px; border-radius: 50%; background: var(--accent-bright); animation: pulse 1s infinite ease-in-out; }
.thinking span:nth-child(2) { animation-delay: .13s; }
.thinking span:nth-child(3) { animation-delay: .26s; }
.thinking b { margin-left: 5px; font-size: .8rem; letter-spacing: .08em; text-transform: uppercase; }
@keyframes pulse { 0%, 70%, 100% { transform: translateY(0); opacity: .45; } 35% { transform: translateY(-6px); opacity: 1; } }

.game-panel { display: grid; gap: 14px; padding-top: 6px; }
.status-card, .controls-card, .moves-card, .rules-card {
  border: 1px solid var(--panel-border);
  border-radius: 10px;
  background: var(--panel);
  box-shadow: none;
}
.status-card { padding: 24px; }
.status-label { margin: 0 0 8px; color: var(--accent); text-transform: uppercase; letter-spacing: .13em; font-size: .75rem; font-weight: 800; }
.status-card h2 { margin: 0; font: 500 clamp(1.45rem, 4vw, 2.05rem)/1.15 Georgia, serif; }
.status-card p:last-child { margin: 9px 0 0; color: var(--muted); font-size: 1rem; line-height: 1.5; }

.controls-card { padding: 18px; display: grid; grid-template-columns: 1fr 1fr; gap: 13px; }
label span { display: block; margin: 0 0 6px 2px; color: var(--muted); font-size: .78rem; font-weight: 700; text-transform: uppercase; letter-spacing: .08em; }
select {
  width: 100%;
  color: var(--ink);
  background: #2e2c29;
  border: 1px solid rgba(255,255,255,.12);
  border-radius: 9px;
  padding: 10px 32px 10px 10px;
}
.button-row { grid-column: 1 / -1; display: grid; grid-template-columns: 1fr 1fr auto; gap: 9px; margin-top: 2px; }
.cache-row {
  grid-column: 1 / -1;
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 14px;
  padding-top: 12px;
  border-top: 1px solid var(--panel-border);
}
.cache-row span {
  display: block;
  color: var(--muted);
  font-size: .75rem;
  font-weight: 700;
  letter-spacing: .08em;
  text-transform: uppercase;
}
.cache-row strong {
  display: block;
  margin-top: 2px;
  font-size: .88rem;
  font-weight: 650;
}
.cache-row button { padding: 7px 12px; font-size: .82rem; }
button {
  color: var(--ink);
  border: 1px solid rgba(255,255,255,.13);
  border-radius: 9px;
  background: rgba(255,255,255,.055);
  padding: 10px 14px;
  font-weight: 750;
  cursor: pointer;
}
button:hover { background: rgba(255,255,255,.1); }
button:disabled { opacity: .4; cursor: not-allowed; }
button.primary { color: #1c211c; background: var(--accent); border-color: var(--accent); }
button.primary:hover { background: var(--accent-bright); }
.icon-button { width: 43px; padding: 0; font-size: 1.25rem; }

.moves-card { overflow: hidden; }
.section-heading { padding: 16px 18px 12px; display: flex; align-items: baseline; justify-content: space-between; border-bottom: 1px solid var(--panel-border); }
.section-heading h3 { margin: 0; font-size: .9rem; text-transform: uppercase; letter-spacing: .09em; }
.section-heading span { color: var(--muted); font-size: .78rem; }
.move-list {
  margin: 0;
  padding: 12px 18px 14px 44px;
  min-height: 88px;
  max-height: 180px;
  overflow-y: auto;
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 6px 25px;
  color: var(--muted);
  font: .9rem/1.4 ui-monospace, SFMono-Regular, Menlo, monospace;
}
.move-list li::marker { color: #66756f; }
.move-list .empty-history { grid-column: 1 / -1; list-style: none; margin-left: -25px; font-family: inherit; }

.rules-card { padding: 0 18px; color: var(--muted); }
.rules-card summary { padding: 15px 0; color: var(--ink); cursor: pointer; font-size: .9rem; font-weight: 750; }
.rules-card ul { margin: 0 0 17px; padding-left: 19px; font-size: .9rem; line-height: 1.55; }

.promotion-dialog {
  color: var(--ink);
  border: 1px solid rgba(255,255,255,.15);
  border-radius: 12px;
  background: var(--panel);
  box-shadow: 0 30px 100px rgba(0,0,0,.65);
  padding: 0;
}
.promotion-dialog::backdrop { background: rgba(4,8,6,.72); backdrop-filter: blur(3px); }
.promotion-dialog form { padding: 24px; }
.promotion-dialog p { margin: 0 0 16px; text-align: center; font: 1.2rem Georgia, serif; }
.promotion-options { display: grid; grid-template-columns: repeat(4, 60px); gap: 8px; }
.promotion-options button { padding: 9px 5px; font: 2rem Georgia, serif; background: #d9d2bd; color: #151917; }
.promotion-options button.white-piece {
  color: #fff;
  -webkit-text-stroke: 1px #343330;
}
.cancel-promotion { width: 100%; margin-top: 12px; padding: 8px; }

.load-error { max-width: 620px; margin: 20px auto; padding: 18px; border: 1px solid #934f46; border-radius: 12px; background: #321d1a; text-align: center; }
.load-error strong, .load-error span { display: block; }
.load-error span { margin-top: 5px; color: #deb8b1; font-size: .85rem; }

@media (max-width: 800px) {
  .site-header { padding: 22px 18px 12px; }
  .wasm-badge { padding: 8px; font-size: 0; }
  .game-layout { grid-template-columns: 1fr; padding: 10px 18px 40px; gap: 22px; }
  .board-section { width: min(82vw, 390px); }
  .game-panel { width: min(100%, 520px); justify-self: center; }
}

@media (max-width: 430px) {
  .controls-card { grid-template-columns: 1fr; }
  .button-row { grid-column: 1; }
  .cache-row { grid-column: 1; }
  .site-header { align-items: flex-end; }
}
