:root {
  color-scheme: dark;
  font-family: Inter, ui-sans-serif, system-ui, -apple-system, BlinkMacSystemFont, "Segoe UI", sans-serif;
  background: #262421;
  color: #f3f1eb;
  font-synthesis: none;
  text-rendering: optimizeLegibility;
  --surface: #302e2b;
  --surface-raised: #383632;
  --text-muted: #aaa69d;
  --green: #81a45e;
  --board-dark: #769656;
  --board-light: #eeeed2;
  --board-size: min(100%, 660px);
}

* { box-sizing: border-box; }

body {
  min-width: 320px;
  min-height: 100vh;
  margin: 0;
  background:
    radial-gradient(ellipse at 50% -15%, rgb(108 133 78 / 10%), transparent 48rem),
    #262421;
}

button { font: inherit; }

.app-layout {
  min-height: 100vh;
  display: grid;
  grid-template-columns: 220px minmax(0, 1fr);
}

.sidebar {
  position: sticky;
  top: 0;
  height: 100vh;
  min-height: 540px;
  padding: 28px 18px 20px;
  display: flex;
  flex-direction: column;
  border-right: 1px solid #ffffff0d;
  background: #24221f;
}

.brand {
  display: inline-flex;
  align-items: center;
  gap: 10px;
  color: #f3f1eb;
  font-size: 18px;
  font-weight: 760;
  letter-spacing: -.7px;
  text-decoration: none;
}

.brand-mark { color: #9fbd79; font-size: 25px; line-height: 1; }
.brand-accent { color: #9fbd79; }
.sidebar-caption { margin: 9px 0 0 35px; color: #99958c; font-size: 11px; letter-spacing: .02em; }

.side-nav { display: grid; gap: 7px; margin-top: 36px; }
.nav-item {
  min-height: 42px;
  padding: 0 12px;
  display: flex;
  align-items: center;
  gap: 11px;
  border-radius: 7px;
  color: #b7b3aa;
  font-size: 12px;
  font-weight: 650;
  text-decoration: none;
}
.nav-item > span { width: 18px; color: #99958c; font-size: 17px; text-align: center; }
.nav-item:hover { background: #ffffff08; color: #f3f1eb; }
.nav-item.active { background: #34412c; color: #c8dfad; }
.nav-item.active > span { color: #a9c989; }

.sidebar-actions { display: grid; gap: 9px; margin-top: auto; }
.sidebar-note { margin: 14px 0 0; color: #8d8981; font-size: 10px; line-height: 1.5; text-align: center; }

.page-shell {
  width: 100%;
  min-width: 0;
  min-height: 100vh;
  margin: 0;
  padding: clamp(24px, 4vh, 48px) clamp(20px, 3.5vw, 52px) 20px;
  display: flex;
  flex-direction: column;
  justify-content: center;
}

.game-layout {
  width: min(100%, 1080px);
  margin: auto;
  display: grid;
  grid-template-columns: minmax(0, 660px) minmax(260px, 330px);
  justify-content: center;
  align-items: center;
  gap: clamp(24px, 3vw, 42px);
}

.board-column { min-width: 0; width: min(100%, 660px); }
.board-heading { min-height: 76px; display: flex; align-items: flex-start; justify-content: space-between; gap: 16px; }
.eyebrow { margin: 0 0 7px; color: #a9a59c; font-size: 10px; font-weight: 750; letter-spacing: .15em; }
h1, h2, h3, p { margin-top: 0; }
h1 { margin-bottom: 0; font-size: clamp(23px, 3vw, 30px); line-height: 1.12; letter-spacing: -.04em; }
h2 { margin: 0; font-size: 20px; letter-spacing: -.03em; }
h3 { margin: 0; font-size: 14px; }

.turn-pill {
  flex: 0 0 auto;
  max-width: min(52%, 220px);
  min-height: 40px;
  margin-top: 2px;
  padding: 9px 13px;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 9px;
  border: 1px solid #ffffff18;
  border-radius: 9px;
  background: #302e2b;
  color: #e5e1d8;
  font-size: 12px;
  font-weight: 750;
  line-height: 1.25;
  text-align: center;
}
.turn-pill::before {
  width: 8px;
  height: 8px;
  flex: 0 0 8px;
  border-radius: 50%;
  background: currentColor;
  content: "";
}
.turn-pill.active, .status-badge.active { border-color: #81a45e55; background: #34412c; color: #c4dda7; }
.turn-pill.check, .status-badge.check { border-color: #d78d4c66; background: #594034; color: #ffd09f; }
.turn-pill.checkmate, .status-badge.checkmate { border-color: #e16d5f70; background: #512f2d; color: #ffb6a9; }
.turn-pill.resigned, .status-badge.resigned { border-color: #e16d5f70; background: #512f2d; color: #ffb6a9; }
.turn-pill.stalemate, .status-badge.stalemate { border-color: #9b8bd066; background: #3e374f; color: #d2c6ff; }
.turn-pill.draw, .status-badge.draw { border-color: #7faec066; background: #293e45; color: #b9e2eb; }
.turn-pill.loading, .status-badge.loading, .turn-pill.unavailable, .status-badge.unavailable { border-color: #ffffff18; background: #302e2b; color: #c5c1b7; }
.turn-pill.unavailable::before { background: #e98578; }
.turn-pill.loading::before { background: #c5c1b7; }
.status-badge {
  padding: 6px 9px;
  border: 1px solid transparent;
  border-radius: 999px;
  background: #3e3c37;
  color: #c5c1b7;
  font-size: 10px;
  font-weight: 700;
  white-space: nowrap;
}

.player-strip { display: flex; align-items: center; height: 40px; gap: 9px; color: #dad7cf; }
.player-name { font-size: 13px; font-weight: 650; }
.player-state { color: #a9a59c; font-size: 11px; }
.player-avatar { display: inline-grid; flex: 0 0 auto; place-items: center; border-radius: 50%; font-weight: 700; }
.player-avatar-small { width: 25px; height: 25px; font-size: 10px; }
.player-avatar-large { width: 35px; height: 35px; font-size: 12px; }
.black-token { color: #25231f; background: #e5e1d7; }
.white-token { color: #f4f0e7; background: #514e47; }
.player-bottom { margin-top: 1px; }

.board {
  width: var(--board-size);
  aspect-ratio: 1;
  display: grid;
  grid-template-columns: repeat(8, minmax(0, 1fr));
  grid-template-rows: repeat(8, minmax(0, 1fr));
  overflow: hidden;
  border: 5px solid #201f1c;
  border-radius: 5px;
  background: #201f1c;
  box-shadow: 0 15px 38px #0005, 0 2px 5px #0005;
  touch-action: manipulation;
  user-select: none;
}

.square {
  position: relative;
  display: grid;
  min-width: 0;
  min-height: 0;
  place-items: center;
  padding: 0;
  border: 0;
  border-radius: 0;
  cursor: pointer;
  -webkit-tap-highlight-color: transparent;
}

.square.light { background: var(--board-light); }
.square.dark { background: var(--board-dark); }
.square:focus-visible { z-index: 3; outline: 3px solid #f8d66d; outline-offset: -4px; }
.square:disabled { cursor: default; }
.square:hover:not(:disabled) { filter: brightness(1.08); }
.square.selected::after, .square.last-from::after, .square.last-to::after {
  position: absolute;
  inset: 0;
  content: "";
  pointer-events: none;
}
.square.selected::after {
  background: rgb(247 211 91 / 40%);
  box-shadow: inset 0 0 0 4px #f3c94f;
}
.square.last-from::after, .square.last-to::after { background: rgb(246 211 91 / 30%); }
.square.selected.last-from::after, .square.selected.last-to::after {
  background: rgb(247 211 91 / 45%);
  box-shadow: inset 0 0 0 4px #f3c94f;
}
.square.in-check::after { background: radial-gradient(circle, #e75a4e 4%, #d54b4080 72%); }
.square.in-check.last-from::after, .square.in-check.last-to::after { background: radial-gradient(circle, #ef6553 4%, #d54b40a6 72%); }

.chess-piece {
  position: relative;
  z-index: 1;
  font-family: "Apple Symbols", "Segoe UI Symbol", "Noto Sans Symbols 2", serif;
  font-size: clamp(30px, 7.4vw, 62px);
  line-height: 1;
  pointer-events: none;
}
.chess-piece.white-piece { color: #fffdf5; text-shadow: 0 2px 2px #25231f, 0 0 2px #25231f; }
.chess-piece.black-piece { color: #282722; text-shadow: 0 1px 1px #f3f0e5; }
.piece-animation {
  position: fixed !important;
  z-index: 1100 !important;
  display: grid;
  place-items: center;
  margin: 0;
  pointer-events: none;
  will-change: transform;
}

.move-dot {
  position: absolute;
  z-index: 2;
  width: 30%;
  aspect-ratio: 1;
  border-radius: 50%;
  background: rgb(36 48 28 / 48%);
  box-shadow: 0 0 0 2px rgb(255 255 255 / 72%), 0 1px 5px rgb(0 0 0 / 48%);
  pointer-events: none;
}
.move-ring {
  position: absolute;
  z-index: 2;
  inset: 7%;
  border: clamp(4px, .8vw, 7px) solid rgb(36 48 28 / 58%);
  border-radius: 50%;
  box-shadow: inset 0 0 0 2px rgb(255 255 255 / 72%), 0 0 2px rgb(0 0 0 / 55%);
  pointer-events: none;
}
.coordinate {
  position: absolute;
  z-index: 2;
  left: 4px;
  bottom: 2px;
  font-size: clamp(8px, 1.5vw, 11px);
  font-weight: 750;
  line-height: 1;
  pointer-events: none;
}
.light .coordinate { color: #769656; }
.dark .coordinate { color: #eeeed2; }

.feedback {
  min-height: 36px;
  padding: 10px 2px 0;
  color: #aaa69d;
  font-size: 12px;
  line-height: 1.45;
}
.feedback.error { color: #f19a8c; }
.feedback.success { color: #b8d49a; }

.game-panel {
  margin-top: 1px;
  padding: 22px;
  border: 1px solid #ffffff0b;
  border-radius: 12px;
  background: linear-gradient(145deg, #32302d, #2e2c29);
  box-shadow: 0 10px 28px #0002;
}
.panel-title-row { display: flex; justify-content: space-between; align-items: center; gap: 10px; margin-bottom: 21px; }
.panel-title-row .eyebrow { margin-bottom: 5px; }

.player-card { display: flex; justify-content: space-between; align-items: center; min-height: 62px; padding: 10px 12px; border: 1px solid #ffffff0a; border-radius: 8px; background: #292825; }
.player-card + .player-card { margin-top: 8px; }
.player-card-side { display: flex; align-items: center; gap: 11px; }
.card-player-name, .card-player-sub { display: block; }
.card-player-name { color: #f1eee7; font-size: 12px; font-weight: 700; }
.card-player-sub { margin-top: 4px; color: #938f86; font-size: 10px; }
.turn-marker { width: 8px; height: 8px; border-radius: 50%; background: transparent; }
.turn-marker.active { background: #9fc37c; box-shadow: 0 0 0 4px #9fc37c1c; }

.game-actions { display: grid; grid-template-columns: 1fr 1fr; gap: 9px; margin-top: 16px; }
.button-action { border-color: #ffffff18; background: #3a3834; color: #e2dfd7; }
.button-action:hover:not(:disabled) { background: #46443f; }
.button-action:disabled { cursor: not-allowed; opacity: .45; }
.button-resign { border-color: #ab554c55; background: #412c29; color: #edaaa0; }
.button-resign:hover:not(:disabled) { border-color: #d66c60; background: #50312d; }
.button-resign:disabled { cursor: not-allowed; opacity: .45; }

.history-section { margin-top: 25px; }
.section-heading { display: flex; justify-content: space-between; align-items: center; margin-bottom: 10px; }
.move-count { color: #969188; font-size: 10px; }
.move-history { min-height: 112px; max-height: 230px; overflow: auto; overflow-anchor: none; scroll-behavior: smooth; border: 1px solid #ffffff0a; border-radius: 8px; background: #292825; scrollbar-color: #555149 transparent; scrollbar-width: thin; }
.history-empty { margin: 0; padding: 18px 12px; color: #938f86; font-size: 11px; text-align: center; }
.move-row { display: grid; grid-template-columns: 36px 1fr 1fr; min-height: 34px; align-items: center; padding: 0 8px; color: #dad7cf; font-size: 11px; }
.move-row:nth-child(even) { background: #ffffff04; }
.move-number { color: #8f8b82; font-variant-numeric: tabular-nums; }
.move-cell { padding: 6px 8px; border-radius: 4px; font-variant-numeric: tabular-nums; }
.move-cell.latest { background: #556b3f; color: white; font-weight: 700; }
.move-cell.empty { color: #615e57; }

.button { min-height: 40px; border: 1px solid transparent; border-radius: 7px; cursor: pointer; font-size: 11px; font-weight: 750; transition: background .15s ease, border-color .15s ease, transform .15s ease; }
.button:hover:not(:disabled) { transform: translateY(-1px); }
.button:focus-visible { outline: 2px solid #c7ddaa; outline-offset: 3px; }
.button:disabled { cursor: wait; opacity: .55; }
.button-primary { background: #81a45e; color: #172013; }
.button-primary:hover:not(:disabled) { background: #91b36d; }
.button-secondary { border-color: #ffffff18; background: #3a3834; color: #e2dfd7; }
.button-secondary:hover { background: #46443f; }
.panel-note { margin: 18px 0 0; color: #8d8981; font-size: 10px; line-height: 1.5; text-align: center; }
.page-footer { margin: 18px 0 20px; color: #77736b; font-size: 10px; text-align: center; }
.page-footer p { margin: 0; }
.page-footer p + p { margin-top: 5px; }
.page-footer a { color: #9fbd79; text-decoration: none; }
.page-footer a:hover { color: #c8dfad; text-decoration: underline; }
.page-footer a:focus-visible { border-radius: 2px; outline: 2px solid #c7ddaa; outline-offset: 3px; }

.promotion-dialog { width: min(360px, calc(100% - 32px)); padding: 25px; border: 1px solid #ffffff18; border-radius: 12px; background: #302e2b; color: #f3f1eb; box-shadow: 0 22px 80px #0009; }
.promotion-dialog::backdrop { background: #11100ed1; backdrop-filter: blur(3px); }
.promotion-dialog h2 { margin-bottom: 7px; }
.promotion-description { margin-bottom: 18px; color: #aaa69d; font-size: 12px; }
.promotion-options { display: grid; grid-template-columns: repeat(4, 1fr); gap: 8px; }
.promotion-option { display: grid; min-height: 68px; place-items: center; border: 1px solid #ffffff16; border-radius: 8px; background: #3a3834; color: #f3f1eb; cursor: pointer; font-size: 37px; }
.promotion-option:hover, .promotion-option:focus-visible { border-color: #9fc37c; background: #41473a; outline: none; }
.promotion-option span { font-size: 9px; font-weight: 700; }
.dialog-cancel { margin-top: 15px; padding: 7px 0; border: 0; background: transparent; color: #b6b2a9; cursor: pointer; font-size: 11px; }
.dialog-cancel:focus-visible { outline: 2px solid #c7ddaa; outline-offset: 3px; }

@media (max-width: 1100px) and (min-width: 821px) {
  .app-layout { grid-template-columns: 190px minmax(0, 1fr); }
  .sidebar { padding-right: 14px; padding-left: 14px; }
  .page-shell { padding-right: 22px; padding-left: 22px; }
  .game-layout { grid-template-columns: minmax(0, 1fr) minmax(250px, 290px); gap: 22px; }
}

@media (max-width: 820px) {
  .app-layout { grid-template-columns: minmax(0, 1fr); }
  .sidebar {
    position: static;
    height: auto;
    min-height: 0;
    padding: 13px 20px;
    flex-direction: row;
    align-items: center;
    gap: 22px;
    border-right: 0;
    border-bottom: 1px solid #ffffff0d;
  }
  .sidebar-caption, .sidebar-note { display: none; }
  .side-nav { display: flex; gap: 4px; margin: 0 auto 0 0; }
  .app-layout.game-active .nav-item[href="#history-title"] { display: none; }
  .nav-item { min-height: 38px; padding: 0 10px; }
  .sidebar-actions { display: flex; gap: 8px; margin: 0; }
  .sidebar-actions .button { min-height: 36px; padding: 0 12px; }
  .page-shell { min-height: auto; padding: 28px 20px 18px; }
  .game-layout { grid-template-columns: minmax(0, 660px); gap: 18px; }
  .game-panel { margin-top: 0; padding: 18px; }
  .move-history { min-height: 72px; max-height: 175px; }
}

@media (max-width: 480px) {
  .app-layout { min-height: 100vh; min-height: 100dvh; }
  .sidebar {
    min-height: 0;
    padding: max(7px, env(safe-area-inset-top)) 10px 7px;
    flex-wrap: nowrap;
    gap: 7px;
  }
  .brand { flex: 0 0 auto; margin: 0 auto 0 0; gap: 6px; font-size: 15px; }
  .brand-mark { font-size: 21px; }
  .side-nav { flex: 0 0 auto; }
  .nav-item { min-height: 32px; padding: 0 6px; gap: 3px; font-size: 9px; }
  .nav-item > span { width: 13px; font-size: 13px; }
  .sidebar-actions { flex: 0 0 auto; gap: 5px; }
  .sidebar-actions .button { min-height: 32px; padding: 0 8px; font-size: 10px; }
  .sidebar-actions #new-game { order: 2; border-color: #ffffff24; background: transparent; color: #d1cdc4; }
  .sidebar-actions #new-game:hover:not(:disabled) { background: #ffffff0a; }
  .page-shell { min-height: calc(100dvh - 48px); padding: 5px 8px max(10px, env(safe-area-inset-bottom)); }
  .game-layout { width: 100%; gap: 10px; }
  .board-column { width: 100%; }
  .board-heading { min-height: 44px; align-items: center; gap: 6px; }
  .board-heading .eyebrow { display: none; }
  h1 { font-size: 17px; letter-spacing: -.03em; }
  .turn-pill { max-width: 58%; min-height: 32px; margin: 0; padding: 6px 8px; gap: 6px; font-size: 10px; }
  .turn-pill::before { width: 6px; height: 6px; flex-basis: 6px; }
  .player-strip { height: 26px; gap: 7px; }
  .player-avatar-small { width: 21px; height: 21px; font-size: 9px; }
  .board {
    --board-size: min(100%, calc(100dvh - 185px));
    margin-inline: auto;
    border-width: 3px;
  }
  .chess-piece { font-size: clamp(28px, 10vw, 46px); }
  .coordinate { left: 2px; bottom: 1px; }
  .feedback { min-height: 26px; padding-top: 5px; font-size: 11px; }
  .game-panel { padding: 16px; }
}

@media (max-width: 360px) {
  .side-nav { display: none; }
  .sidebar-actions .button { padding: 0 7px; }
}

@media (prefers-reduced-motion: reduce) {
  *, *::before, *::after { scroll-behavior: auto !important; transition-duration: .01ms !important; }
}
