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

body {
  margin: 0;
  min-height: 100vh;
  display: flex;
  flex-direction: column;
  align-items: center;
  background: var(--color-secondary);
  color: var(--color-neutral-light);
  font-family: var(--font-ui);
  font-size: var(--font-size-base);
  line-height: var(--line-height-normal);
}

.sr-only {
  position: absolute;
  width: 1px;
  height: 1px;
  padding: 0;
  margin: -1px;
  overflow: hidden;
  clip: rect(0, 0, 0, 0);
  border: 0;
}

/* ── Header ── */

header {
  padding: var(--space-6) var(--space-6) var(--space-2);
  text-align: center;
  order: 1;
}

header h1 {
  margin: 0;
  font-family: var(--font-display);
  font-size: 2.4rem;
  font-weight: var(--font-weight-bold);
  color: var(--color-primary);
  letter-spacing: 1em;
  text-indent: 1em;
}

/* ── Settings Bar ── */

.settings-bar {
  display: flex;
  align-items: center;
  gap: var(--space-1);
  padding: var(--space-2) var(--space-4);
  background: var(--color-secondary-light);
  border-radius: 999px;
  order: 2;
  margin-bottom: var(--space-4);
}

.setting {
  display: flex;
  align-items: center;
  gap: var(--space-2);
  padding: var(--space-1) var(--space-2);
  border-radius: var(--radius-sm);
  transition: background var(--transition-fast);
}

.setting:hover {
  background: rgba(255, 255, 255, 0.06);
}

.setting-label {
  font-size: var(--font-size-xs);
  font-weight: var(--font-weight-semi);
  color: var(--color-neutral);
  text-transform: uppercase;
  letter-spacing: 0.05em;
}

.setting select {
  appearance: none;
  background: var(--color-secondary-light);
  border: none;
  color: var(--color-neutral-light);
  font-family: var(--font-ui);
  font-size: var(--font-size-sm);
  cursor: pointer;
  padding: var(--space-1) var(--space-3) var(--space-1) var(--space-1);
}

.setting select option {
  background: var(--color-secondary-light);
  color: var(--color-neutral-light);
}

.setting select:focus-visible {
  outline: 2px solid var(--color-primary);
  outline-offset: 2px;
  border-radius: var(--radius-sm);
}

.setting-divider {
  width: 1px;
  height: 18px;
  background: var(--color-neutral-dark);
  opacity: 0.4;
  margin: 0 var(--space-1);
}

#mute-btn {
  background: none;
  border: none;
  font-size: var(--font-size-base);
  cursor: pointer;
  padding: var(--space-1) var(--space-2);
  border-radius: var(--radius-sm);
  transition: background var(--transition-fast);
}

#mute-btn:hover {
  background: rgba(255, 255, 255, 0.06);
}

/* ── Game Container ── */

.game-container {
  display: flex;
  align-items: flex-start;
  gap: var(--space-6);
  order: 3;
  max-width: 900px;
}

.side-panel {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: var(--space-2);
  width: 80px;
  min-height: 120px;
  padding-top: var(--space-4);
}

.panel-label {
  font-size: var(--font-size-xs);
  font-weight: var(--font-weight-semi);
  color: var(--color-neutral);
  text-transform: uppercase;
  letter-spacing: 0.08em;
  text-align: center;
}

.capture-tray {
  display: flex;
  flex-wrap: wrap;
  justify-content: center;
  gap: 2px;
  font-size: 1.1rem;
  line-height: 1;
}

.capture-tray .captured-piece {
  opacity: 0.8;
  text-shadow: 0 1px 2px rgba(0, 0, 0, 0.6);
}

.capture-tray .material-diff {
  display: block;
  width: 100%;
  text-align: center;
  font-size: var(--font-size-xs);
  font-weight: var(--font-weight-semi);
  color: var(--color-neutral);
  margin-top: var(--space-1);
}

/* ── Board ── */

.board-section {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: var(--space-3);
}

.board {
  display: grid;
  grid-template-columns: repeat(8, var(--square-size));
  grid-template-rows: repeat(8, var(--square-size));
  border: 4px solid var(--color-primary-dark);
  border-radius: var(--radius-sm);
  box-shadow:
    inset 0 0 8px rgba(0, 0, 0, 0.2),
    var(--shadow-medium),
    var(--shadow-strong);
  overflow: hidden;
}

.square {
  display: flex;
  align-items: center;
  justify-content: center;
  width: var(--square-size);
  height: var(--square-size);
  border: none;
  padding: 0;
  margin: 0;
  font-size: calc(var(--square-size) * 0.7);
  line-height: 1;
  cursor: default;
  position: relative;
  user-select: none;
  transition: box-shadow var(--transition-fast);
  background: var(--board-light);
}

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

.square.has-player-piece {
  cursor: pointer;
}

/* Piece colors */
.square.white-piece {
  color: var(--piece-white);
  text-shadow:
    0 1px 3px rgba(0, 0, 0, 0.7),
    0 0 1px rgba(0, 0, 0, 0.5);
}

.square.black-piece {
  color: var(--piece-black);
  text-shadow:
    0 1px 2px rgba(255, 255, 255, 0.25),
    0 0 1px rgba(255, 255, 255, 0.15);
}

/* Highlights */
.square.selected {
  box-shadow: inset 0 0 0 3px var(--highlight-selected), inset 0 0 12px var(--highlight-selected);
}

.square.valid-move::after {
  content: '';
  position: absolute;
  width: 28%;
  height: 28%;
  background: var(--highlight-valid);
  border-radius: var(--radius-full);
  animation: fadeIn 150ms ease;
}

.square.valid-capture::after {
  content: '';
  position: absolute;
  width: 80%;
  height: 80%;
  background: transparent;
  border: 3px solid var(--highlight-valid);
  border-radius: var(--radius-full);
  animation: fadeIn 150ms ease;
}

.square.last-move::before {
  content: '';
  position: absolute;
  inset: 0;
  background: var(--highlight-last);
  pointer-events: none;
}

.square.in-check {
  box-shadow: inset 0 0 0 3px var(--highlight-check), inset 0 0 16px var(--highlight-check);
}

.square.hint-from {
  box-shadow: inset 0 0 0 3px var(--highlight-hint), inset 0 0 12px var(--highlight-hint);
}

.square.hint-to::after {
  content: '';
  position: absolute;
  width: 28%;
  height: 28%;
  background: var(--highlight-hint);
  border-radius: var(--radius-full);
  animation: pulseHint 1.2s ease-in-out infinite;
}

.square:focus-visible {
  outline: 2px solid var(--color-primary);
  outline-offset: -2px;
  z-index: 1;
}

/* ── Board Footer ── */

.board-footer {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: var(--space-1);
  min-height: 40px;
}

.turn-indicator {
  font-size: var(--font-size-sm);
  font-weight: var(--font-weight-semi);
  color: var(--color-neutral);
  text-transform: uppercase;
  letter-spacing: 0.08em;
}

.turn-indicator.thinking::after {
  content: '';
  animation: dotPulse 1.5s infinite;
}

.status-bar {
  font-size: var(--font-size-xs);
  color: var(--color-neutral);
  opacity: 0.7;
  min-height: 1.2em;
  transition: opacity var(--transition-fast);
}

.status-bar.fade-out {
  opacity: 0;
}

.status-bar.shake {
  animation: shake 300ms ease;
}

/* ── Controls ── */

.controls {
  display: flex;
  gap: var(--space-3);
  margin-top: var(--space-6);
  order: 4;
}

.control-btn {
  padding: var(--space-2) var(--space-6);
  background: transparent;
  color: var(--color-neutral-light);
  border: 1px solid var(--color-neutral-dark);
  border-radius: 999px;
  font-family: var(--font-ui);
  font-size: var(--font-size-base);
  cursor: pointer;
  transition: all 200ms ease;
}

.control-btn:hover:not(:disabled) {
  background: var(--color-primary);
  color: var(--color-secondary-dark);
  border-color: var(--color-primary);
}

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

.control-btn.confirming {
  background: var(--color-error);
  color: var(--color-neutral-light);
  border-color: var(--color-error);
}

/* ── Modal ── */

.modal {
  position: fixed;
  inset: 0;
  display: none;
  align-items: center;
  justify-content: center;
  background: none;
  border: none;
  padding: 0;
  max-width: 100vw;
  max-height: 100vh;
  width: 100%;
  height: 100%;
}

.modal[open] {
  display: flex;
}

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

.modal-content {
  background: var(--color-secondary-light);
  color: var(--color-neutral-light);
  border: 1px solid rgba(255, 255, 255, 0.08);
  border-radius: var(--radius-lg);
  padding: var(--space-8);
  max-width: 380px;
  width: 90vw;
  text-align: center;
  box-shadow: 0 24px 80px rgba(0, 0, 0, 0.6);
  animation: modalOpen 350ms cubic-bezier(0.34, 1.56, 0.64, 1);
}

.modal-title {
  margin: 0 0 var(--space-6);
  font-family: var(--font-display);
  font-size: var(--font-size-lg);
}

.modal-title.win { color: var(--color-success); }
.modal-title.loss { color: var(--color-error); }
.modal-title.draw { color: var(--color-primary); }

.modal-stats {
  list-style: none;
  padding: 0;
  margin: 0 0 var(--space-6);
  text-align: left;
}

.modal-stats li {
  display: flex;
  justify-content: space-between;
  padding: var(--space-2) 0;
  border-bottom: 1px solid rgba(255, 255, 255, 0.06);
  font-size: var(--font-size-sm);
}

.modal-stats .stat-label { color: var(--color-neutral); }
.modal-stats .stat-value { color: var(--color-neutral-light); font-weight: var(--font-weight-semi); }

.play-again-btn {
  padding: var(--space-3) var(--space-8);
  background: var(--color-primary);
  color: var(--color-secondary-dark);
  border: none;
  border-radius: 999px;
  font-family: var(--font-ui);
  font-size: var(--font-size-base);
  font-weight: var(--font-weight-bold);
  cursor: pointer;
  transition: all 200ms ease;
}

.play-again-btn:hover {
  background: var(--color-primary-light);
  transform: translateY(-1px);
  box-shadow: 0 4px 16px rgba(226, 180, 90, 0.3);
}

/* ── Promotion Modal ── */

.promotion-content {
  max-width: 440px;
}

.promotion-choices {
  display: grid;
  grid-template-columns: repeat(4, 1fr);
  gap: var(--space-3);
  margin-top: var(--space-4);
}

.promotion-btn {
  display: flex;
  align-items: center;
  justify-content: center;
  aspect-ratio: 1;
  font-size: 3.2rem;
  background: var(--board-light);
  border: 2px solid var(--color-primary);
  border-radius: var(--radius-md);
  cursor: pointer;
  transition: all var(--transition-fast);
}

.promotion-btn:hover {
  border-color: var(--color-primary-light);
  background: var(--color-primary-light);
  transform: scale(1.08);
  box-shadow: 0 0 12px rgba(245, 216, 140, 0.5);
}

.promotion-btn.white-piece {
  color: #FFFFFF;
  text-shadow:
    0 0 2px #000,
    0 1px 3px rgba(0, 0, 0, 0.9),
    0 0 1px #000;
}

.promotion-btn.black-piece {
  color: #000000;
  text-shadow: 0 1px 2px rgba(255, 255, 255, 0.4);
}

/* ── Animations ── */

@keyframes fadeIn {
  from { transform: scale(0.5); opacity: 0; }
  to { transform: scale(1); opacity: 1; }
}

@keyframes pulseHint {
  0%, 100% { transform: scale(1); opacity: 0.6; }
  50% { transform: scale(1.3); opacity: 1; }
}

@keyframes dotPulse {
  0% { content: ''; }
  25% { content: '.'; }
  50% { content: '..'; }
  75% { content: '...'; }
}

@keyframes shake {
  0%, 100% { transform: translateX(0); }
  25% { transform: translateX(-4px); }
  75% { transform: translateX(4px); }
}

@keyframes modalOpen {
  from { transform: scale(0.85); opacity: 0; }
  to { transform: scale(1); opacity: 1; }
}

@keyframes undoPulse {
  0% { opacity: 1; }
  50% { opacity: 0.4; }
  100% { opacity: 1; }
}

/* ── Responsive ── */

@media (max-width: 1023px) {
  .game-container {
    flex-direction: column;
    align-items: center;
  }
  .side-panel {
    flex-direction: row;
    width: auto;
    min-height: auto;
    padding-top: 0;
    gap: var(--space-3);
  }
}

@media (max-width: 767px) {
  header { padding: var(--space-4) var(--space-4) var(--space-1); }
  header h1 { font-size: var(--font-size-lg); }
  .settings-bar { border-radius: var(--radius-md); }
  .setting-label { display: none; }
  .controls { margin-top: var(--space-4); }
  .control-btn { padding: var(--space-2) var(--space-4); font-size: var(--font-size-sm); }
}

@media (prefers-reduced-motion: reduce) {
  *, *::before, *::after {
    animation-duration: 0.01ms !important;
    transition-duration: 0.01ms !important;
  }
}
