* {
  box-sizing: border-box;
  margin: 0;
  padding: 0;
}

body {
  font-family: "Segoe UI", system-ui, sans-serif;
  background: #1a1a2e;
  color: #eee;
  min-height: 100vh;
}

#app {
  display: flex;
  flex-direction: column;
  min-height: 100vh;
  align-items: center;
}

.toolbar {
  display: flex;
  align-items: center;
  gap: 1rem;
  width: 100%;
  max-width: 560px;
  padding: 0.75rem 1rem;
  background: #16213e;
  border-bottom: 1px solid #2a3a5c;
}

.toolbar h1 {
  font-size: 1.25rem;
  font-weight: 700;
  flex: 1;
}

.toolbar button,
.overlay-card button,
.promo-buttons button {
  padding: 0.4rem 0.9rem;
  background: #3a506b;
  color: #eee;
  border: 1px solid #5a7a9a;
  border-radius: 6px;
  cursor: pointer;
  font-size: 0.9rem;
}

.toolbar button:hover,
.overlay-card button:hover,
.promo-buttons button:hover {
  background: #4a6080;
}

.game-main {
  flex: 1;
  display: flex;
  flex-direction: column;
  align-items: center;
  padding: 1rem;
  gap: 0.75rem;
}

.status-panel {
  width: 100%;
  max-width: 480px;
  text-align: center;
}

.status-main {
  font-size: 1.1rem;
  font-weight: 600;
}

.status-sub {
  color: #e74c3c;
  font-weight: 600;
  margin-top: 0.25rem;
}

.status-last {
  font-size: 0.85rem;
  color: #aaa;
  margin-top: 0.35rem;
}

.board-container {
  width: 100%;
  max-width: 480px;
}

.board-with-coords {
  display: flex;
  gap: 0.35rem;
}

.rank-labels {
  display: flex;
  flex-direction: column;
  justify-content: space-around;
  padding: 0 0.15rem;
  font-size: 0.7rem;
  color: #888;
  height: min(90vw, 480px);
  max-height: 480px;
}

.rank-labels span {
  flex: 1;
  display: flex;
  align-items: center;
}

.grid-wrap {
  flex: 1;
  display: flex;
  flex-direction: column;
  gap: 0.25rem;
}

.board-grid {
  display: grid;
  grid-template-columns: repeat(8, 1fr);
  grid-template-rows: repeat(8, 1fr);
  width: min(90vw, 480px);
  height: min(90vw, 480px);
  max-width: 480px;
  max-height: 480px;
  border: 3px solid #2a2a40;
  border-radius: 4px;
  overflow: hidden;
  box-shadow: 0 8px 24px rgba(0, 0, 0, 0.4);
}

.file-labels {
  display: grid;
  grid-template-columns: repeat(8, 1fr);
  font-size: 0.7rem;
  color: #888;
  text-align: center;
}

.square {
  position: relative;
  border: none;
  padding: 0;
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: center;
  aspect-ratio: 1;
}

.square.light {
  background: #f0d9b5;
}

.square.dark {
  background: #b58863;
}

.square.selected {
  background-color: #7b9ec8 !important;
  box-shadow: inset 0 0 0 3px #4a7ab8;
}

.square.legal {
  cursor: pointer;
}

.square.last-move {
  background-color: #cdd26a !important;
}

.square.in-check {
  background-color: #e88 !important;
  box-shadow: inset 0 0 0 3px #c44;
}

.piece {
  font-size: clamp(1.5rem, 6vw, 2.75rem);
  line-height: 1;
  user-select: none;
  pointer-events: none;
}

.piece.w {
  color: #fff;
  text-shadow: 0 1px 2px #333, 0 0 1px #000;
}

.piece.b {
  color: #222;
}

.move-hint {
  position: absolute;
  width: 28%;
  height: 28%;
  border-radius: 50%;
  background: rgba(0, 0, 0, 0.25);
  pointer-events: none;
}

.square.legal:has(.piece) .move-hint {
  width: 85%;
  height: 85%;
  border-radius: 4px;
  background: transparent;
  box-shadow: inset 0 0 0 4px rgba(0, 0, 0, 0.35);
}

.modal {
  position: fixed;
  inset: 0;
  display: flex;
  align-items: center;
  justify-content: center;
  z-index: 100;
}

.modal.hidden {
  display: none;
}

.modal-backdrop {
  position: absolute;
  inset: 0;
  background: rgba(0, 0, 0, 0.6);
}

.modal-content {
  position: relative;
  background: #16213e;
  border: 1px solid #3a506b;
  border-radius: 10px;
  padding: 1.5rem;
  z-index: 1;
  text-align: center;
}

.promo-card h2 {
  margin-bottom: 0.5rem;
}

.promo-buttons {
  display: flex;
  flex-wrap: wrap;
  gap: 0.5rem;
  justify-content: center;
  margin-top: 1rem;
}

.promo-buttons button {
  min-width: 5rem;
  font-size: 1rem;
}

.overlay {
  position: fixed;
  inset: 0;
  background: rgba(0, 0, 0, 0.75);
  display: flex;
  align-items: center;
  justify-content: center;
  z-index: 200;
}

.overlay.hidden {
  display: none;
}

.overlay-card {
  background: #16213e;
  border: 1px solid #3a506b;
  border-radius: 12px;
  padding: 2rem;
  text-align: center;
  max-width: 320px;
}

.overlay-card h2 {
  margin-bottom: 0.75rem;
  font-size: 1.5rem;
}

.overlay-card p {
  margin-bottom: 1.25rem;
  color: #ccc;
}

.overlay-card button {
  font-size: 1rem;
}

/* —— Start screen —— */
.start-card {
  max-width: 380px;
  text-align: left;
}

.start-card h2 {
  text-align: center;
  margin-bottom: 0.25rem;
}

.start-sub {
  text-align: center;
  color: #aaa;
  font-size: 0.9rem;
  margin-bottom: 1.25rem;
}

.start-field {
  border: none;
  margin-bottom: 1rem;
}

.start-field legend {
  font-weight: 600;
  font-size: 0.85rem;
  color: #ccc;
  margin-bottom: 0.4rem;
}

.start-option {
  display: block;
  margin: 0.35rem 0;
  font-size: 0.9rem;
  cursor: pointer;
}

.start-option input {
  margin-right: 0.5rem;
}

.start-field.hidden {
  display: none;
}

.start-btn {
  display: block;
  width: 100%;
  margin-top: 0.5rem;
  padding: 0.6rem 1rem;
  background: #3a7a4a;
  color: #fff;
  border: 1px solid #5a9a6a;
  border-radius: 8px;
  font-size: 1rem;
  font-weight: 600;
  cursor: pointer;
}

.start-btn:hover {
  background: #4a8a5a;
}

/* —— Board flip (play as Black) —— */
.board-grid.flipped {
  transform: rotate(180deg);
}

.board-grid.flipped .piece {
  transform: rotate(180deg);
}

.board-container.board-flipped .rank-labels,
.board-container.board-flipped .file-labels {
  transform: rotate(180deg);
}

/* —— Special effects —— */
#fx-layer {
  position: fixed;
  inset: 0;
  pointer-events: none;
  z-index: 150;
  overflow: hidden;
}

@keyframes squarePulse {
  0% { box-shadow: inset 0 0 0 0 rgba(76, 175, 80, 0.8); }
  50% { box-shadow: inset 0 0 0 6px rgba(76, 175, 80, 0.5); }
  100% { box-shadow: inset 0 0 0 0 rgba(76, 175, 80, 0); }
}

@keyframes pieceSlide {
  from { transform: translate(var(--slide-dx, 0), var(--slide-dy, 0)); }
  to { transform: translate(0, 0); }
}

@keyframes captureFlash {
  0%, 100% { background-color: inherit; }
  25% { background-color: #e74c3c !important; box-shadow: inset 0 0 12px rgba(231, 76, 60, 0.9); }
  50% { background-color: #c0392b !important; }
}

@keyframes captureShake {
  0%, 100% { transform: translateX(0) rotate(0deg); }
  20% { transform: translateX(-5px) rotate(-8deg); }
  40% { transform: translateX(5px) rotate(8deg); }
  60% { transform: translateX(-4px) rotate(-5deg); }
  80% { transform: translateX(4px) rotate(5deg); }
}

@keyframes capturedFade {
  0% { opacity: 1; transform: scale(1); }
  70% { opacity: 1; }
  100% { opacity: 0; transform: scale(0.4); }
}

@keyframes boardShake {
  0%, 100% { transform: translate(0, 0); }
  15% { transform: translate(-6px, 2px); }
  30% { transform: translate(6px, -2px); }
  45% { transform: translate(-4px, -2px); }
  60% { transform: translate(4px, 2px); }
  75% { transform: translate(-2px, 1px); }
}

@keyframes overlayPop {
  0% { transform: scale(0.6); opacity: 0; }
  60% { transform: scale(1.05); opacity: 1; }
  100% { transform: scale(1); opacity: 1; }
}

@keyframes overlayGlow {
  0% { box-shadow: 0 0 0 rgba(241, 196, 15, 0); }
  50% { box-shadow: 0 0 40px rgba(241, 196, 15, 0.6); }
  100% { box-shadow: 0 0 20px rgba(241, 196, 15, 0.3); }
}

@keyframes confettiFall {
  0% {
    transform: translateY(-10vh) translateX(0) rotate(0deg);
    opacity: 1;
  }
  100% {
    transform: translateY(110vh) translateX(var(--confetti-drift, 0)) rotate(720deg);
    opacity: 0.6;
  }
}

.square.fx-move-from,
.square.fx-move-to {
  animation: squarePulse 0.5s ease-out;
}

.piece.fx-sliding {
  animation: pieceSlide 0.35s ease-out;
  z-index: 2;
  position: relative;
}

.square.fx-capture {
  animation: captureFlash 0.45s ease-out;
}

.square.fx-capture-from {
  animation: squarePulse 0.4s ease-out;
}

.piece.fx-captured-ghost {
  position: absolute;
  inset: 0;
  margin: auto;
  width: fit-content;
  height: fit-content;
  z-index: 3;
  pointer-events: none;
  animation: captureShake 0.4s ease-out, capturedFade 0.5s ease-out forwards;
}

.board-grid.flipped .piece.fx-captured-ghost {
  transform: rotate(180deg);
  animation: captureShakeFlipped 0.4s ease-out, capturedFadeFlipped 0.5s ease-out forwards;
}

@keyframes captureShakeFlipped {
  0%, 100% { transform: rotate(180deg) translateX(0); }
  20% { transform: rotate(180deg) translateX(-5px); }
  40% { transform: rotate(180deg) translateX(5px); }
  60% { transform: rotate(180deg) translateX(-4px); }
  80% { transform: rotate(180deg) translateX(4px); }
}

@keyframes capturedFadeFlipped {
  0% { opacity: 1; transform: rotate(180deg) scale(1); }
  70% { opacity: 1; }
  100% { opacity: 0; transform: rotate(180deg) scale(0.4); }
}

.board-grid.fx-checkmate-board {
  animation: boardShake 0.65s ease-out;
}

.overlay.fx-checkmate-overlay {
  animation: overlayGlow 1s ease-out;
}

.overlay-card.fx-checkmate-pop {
  animation: overlayPop 0.55s cubic-bezier(0.34, 1.56, 0.64, 1),
    overlayGlow 1.2s ease-out;
}

.confetti {
  position: absolute;
  top: -12px;
  width: 10px;
  height: 14px;
  border-radius: 2px;
  opacity: 0.95;
  animation: confettiFall 1.8s ease-in forwards;
}

@media (prefers-reduced-motion: reduce) {
  .piece.fx-sliding {
    animation: none;
    transform: none;
  }

  .square.fx-capture {
    animation: none;
    background-color: #d4a574 !important;
  }

  .piece.fx-captured-ghost {
    animation: none;
    opacity: 0;
  }

  .board-grid.fx-checkmate-board {
    animation: none;
  }

  .overlay-card.fx-checkmate-pop {
    animation: none;
  }

  .confetti {
    display: none;
  }

  .square.fx-move-from,
  .square.fx-move-to {
    animation: squarePulse 0.3s ease-out;
  }
}
