:root {
  --bg: #0f1117;
  --bg2: #1a1d27;
  --bg3: #242836;
  --surface: #2a2e3d;
  --surface2: #333849;
  --surface3: #393f52;
  --text: #e8eaf0;
  --text2: #9ca3b4;
  --accent: #6c8cff;
  --accent2: #8ba4ff;
  --red-piece: #e74c5e;
  --red-piece-dark: #b53040;
  --black-piece: #2d3244;
  --black-piece-dark: #1a1e2a;
  --board-dark: #2a2040;
  --board-light: #d4c8a8;
  --highlight: rgba(108,140,255,0.3);
  --valid-glow: rgba(108,140,255,0.5);
  --shadow: rgba(0,0,0,0.4);
  color-scheme: dark;
}

html[data-theme="light"] {
  --bg: #f0f2f5;
  --bg2: #ffffff;
  --bg3: #e8eaef;
  --surface: #ffffff;
  --surface2: #f5f6f8;
  --surface3: #e2e5ea;
  --text: #1a1d27;
  --text2: #6b7280;
  --accent: #4a6cf7;
  --accent2: #3355e0;
  --board-dark: #7c6a4f;
  --board-light: #f0e6d0;
  --highlight: rgba(74,108,247,0.2);
  --valid-glow: rgba(74,108,247,0.45);
  --shadow: rgba(0,0,0,0.15);
  color-scheme: light;
}

@media (prefers-color-scheme: light) {
  html:not([data-theme="dark"]) {
    --bg: #f0f2f5;
    --bg2: #ffffff;
    --bg3: #e8eaef;
    --surface: #ffffff;
    --surface2: #f5f6f8;
    --surface3: #e2e5ea;
    --text: #1a1d27;
    --text2: #6b7280;
    --accent: #4a6cf7;
    --accent2: #3355e0;
    --board-dark: #7c6a4f;
    --board-light: #f0e6d0;
    --highlight: rgba(74,108,247,0.2);
    --valid-glow: rgba(74,108,247,0.45);
    --shadow: rgba(0,0,0,0.15);
    color-scheme: light;
  }
}

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

html, body {
  height: 100%;
  overflow: hidden;
  font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', system-ui, sans-serif;
  background: var(--bg);
  color: var(--text);
  -webkit-font-smoothing: antialiased;
  touch-action: manipulation;
}

body {
  display: flex;
  flex-direction: column;
  align-items: center;
}

#app {
  width: 100%;
  height: 100%;
  display: flex;
  flex-direction: column;
  align-items: center;
  padding: 8px;
  gap: 6px;
}

#header {
  width: 100%;
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 4px 4px;
  gap: 8px;
  flex-wrap: wrap;
}

#title {
  font-size: 1.4rem;
  font-weight: 700;
  letter-spacing: -0.02em;
  background: linear-gradient(135deg, var(--accent), #a78bfa);
  -webkit-background-clip: text;
  -webkit-text-fill-color: transparent;
  background-clip: text;
  flex-shrink: 0;
}

#meta {
  display: flex;
  align-items: center;
  gap: 8px;
  flex-wrap: wrap;
}

#status {
  font-size: 0.85rem;
  color: var(--text2);
  font-weight: 500;
}

select {
  font-family: inherit;
  font-size: 0.78rem;
  font-weight: 600;
  padding: 6px 14px;
  border: 2px solid var(--surface2);
  border-radius: 8px;
  background: var(--surface2);
  color: var(--text);
  cursor: pointer;
  transition: all 0.15s ease;
  letter-spacing: 0.01em;
  appearance: none;
  -webkit-appearance: none;
  background-image: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' width='12' height='12' viewBox='0 0 12 12'%3E%3Cpath fill='%239ca3b4' d='M2.5 4.5L6 8L9.5 4.5'/%3E%3C/svg%3E");
  background-repeat: no-repeat;
  background-position: right 10px center;
  padding-right: 30px;
}

select:hover {
  border-color: var(--accent);
}

select:focus-visible {
  outline: 2px solid var(--accent);
  outline-offset: 2px;
}

button {
  font-family: inherit;
  font-size: 0.78rem;
  font-weight: 600;
  padding: 6px 14px;
  border: none;
  border-radius: 8px;
  background: var(--surface2);
  color: var(--text);
  cursor: pointer;
  transition: all 0.15s ease;
  letter-spacing: 0.01em;
}

button:hover {
  background: var(--accent);
  color: white;
  transform: translateY(-1px);
}

button:active {
  transform: translateY(0);
}

button.hidden {
  display: none !important;
}

button:focus-visible {
  outline: 2px solid var(--accent);
  outline-offset: 2px;
}

#board-container {
  flex: 1;
  display: flex;
  align-items: center;
  justify-content: center;
  width: 100%;
  min-height: 0;
  padding: 4px;
}

#board {
  display: grid;
  grid-template-columns: repeat(8, 1fr);
  grid-template-rows: repeat(8, 1fr);
  aspect-ratio: 1;
  width: 100%;
  max-width: min(92vw, 560px);
  max-height: min(92vw, 560px);
  border-radius: 12px;
  overflow: hidden;
  box-shadow: 0 8px 32px var(--shadow), 0 2px 8px var(--shadow);
  touch-action: none;
  position: relative;
}

.cell {
  position: relative;
  display: flex;
  align-items: center;
  justify-content: center;
  transition: background 0.15s ease;
}

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

.cell.selected {
  background: var(--highlight) !important;
}

.cell.valid-move {
  cursor: pointer;
  z-index: 2;
}

.cell.valid-move::after {
  content: '';
  position: absolute;
  width: 32%;
  height: 32%;
  border-radius: 50%;
  background: var(--valid-glow);
  box-shadow: 0 0 12px var(--valid-glow);
  animation: pulse-glow 1.5s ease-in-out infinite;
  pointer-events: none;
}

@keyframes pulse-glow {
  0%, 100% { opacity: 0.6; transform: scale(1); }
  50% { opacity: 1; transform: scale(1.15); }
}

.cell.last-move.dark {
  background: #3d2a52;
}

.cell.last-move.light {
  background: #e4d8c0;
}

html[data-theme="light"] .cell.last-move.dark { background: #6b5942; }
html[data-theme="light"] .cell.last-move.light { background: #e2d5b8; }

.piece {
  width: 78%;
  height: 78%;
  border-radius: 50%;
  position: relative;
  transition: transform 0.2s ease, box-shadow 0.2s ease;
  cursor: pointer;
  z-index: 3;
}

.piece.red {
  background: radial-gradient(circle at 35% 30%, #f8717a, var(--red-piece) 50%, var(--red-piece-dark));
  box-shadow: 0 3px 8px rgba(231,76,94,0.4), inset 0 -2px 4px rgba(0,0,0,0.3), inset 0 2px 4px rgba(255,255,255,0.3);
}

.piece.black {
  background: radial-gradient(circle at 35% 30%, #6b7280, var(--black-piece) 50%, var(--black-piece-dark));
  box-shadow: 0 3px 8px rgba(0,0,0,0.5), inset 0 -2px 4px rgba(0,0,0,0.4), inset 0 2px 4px rgba(255,255,255,0.15);
}

.piece.king::after {
  content: '♛';
  position: absolute;
  top: 50%;
  left: 50%;
  transform: translate(-50%, -50%);
  font-size: clamp(0.9rem, 3.5vw, 1.8rem);
  color: rgba(255,255,255,0.85);
  text-shadow: 0 1px 3px rgba(0,0,0,0.6);
  pointer-events: none;
}

.piece:hover {
  transform: scale(1.07);
  filter: brightness(1.1);
}

.piece.selected-piece {
  transform: scale(1.1);
  filter: brightness(1.15) drop-shadow(0 0 8px var(--valid-glow));
}

.piece.not-allowed {
  opacity: 0.5;
  cursor: not-allowed;
}

#footer {
  width: 100%;
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 20px;
  padding: 6px 8px;
  flex-shrink: 0;
}

#score {
  display: flex;
  align-items: center;
  gap: 10px;
  font-size: 0.82rem;
  color: var(--text2);
  font-weight: 500;
}

.color-indicator {
  width: 16px;
  height: 16px;
  border-radius: 50%;
  display: inline-block;
  vertical-align: middle;
}

.color-indicator.red {
  background: var(--red-piece);
  box-shadow: 0 1px 4px rgba(231,76,94,0.4);
}

.color-indicator.black {
  background: var(--black-piece);
  box-shadow: 0 1px 4px rgba(0,0,0,0.3);
}

#score span {
  min-width: 8px;
}

/* Game Over overlay — pointer-events: none so background doesn't block header */
#game-over-overlay {
  position: fixed;
  inset: 0;
  background: rgba(0,0,0,0.65);
  backdrop-filter: blur(8px);
  display: flex;
  align-items: center;
  justify-content: center;
  z-index: 100;
  animation: fadeIn 0.3s ease;
  pointer-events: none;
}

#game-over-overlay > * {
  pointer-events: auto;
}

.hidden { display: none !important; }

@keyframes fadeIn {
  from { opacity: 0; }
  to { opacity: 1; }
}

#game-over-box {
  background: var(--surface);
  border-radius: 20px;
  padding: 36px 44px;
  text-align: center;
  box-shadow: 0 24px 64px var(--shadow);
  animation: popIn 0.4s cubic-bezier(0.34, 1.56, 0.64, 1);
}

@keyframes popIn {
  from { transform: scale(0.8); opacity: 0; }
  to { transform: scale(1); opacity: 1; }
}

#game-over-text {
  font-size: 1.8rem;
  font-weight: 800;
  margin-bottom: 20px;
  letter-spacing: -0.02em;
  background: linear-gradient(135deg, var(--accent), #a78bfa);
  -webkit-background-clip: text;
  -webkit-text-fill-color: transparent;
  background-clip: text;
}

#game-over-buttons {
  display: flex;
  gap: 10px;
  justify-content: center;
  flex-wrap: wrap;
}

#game-over-buttons button {
  font-size: 0.9rem;
  padding: 10px 24px;
}

#game-over-buttons button:first-child {
  background: var(--accent);
  color: white;
  box-shadow: 0 4px 16px rgba(108,140,255,0.3);
}

#game-over-buttons button:first-child:hover {
  background: var(--accent2);
  transform: translateY(-2px);
  box-shadow: 0 6px 24px rgba(108,140,255,0.4);
}

#game-over-buttons button:last-child {
  background: var(--surface2);
  color: var(--text2);
}

#game-over-buttons button:last-child:hover {
  background: var(--surface3);
  color: var(--text);
  transform: translateY(-2px);
}

#thinking-overlay {
  position: fixed;
  inset: 0;
  z-index: 50;
  display: flex;
  align-items: center;
  justify-content: center;
  pointer-events: none;
}

#thinking-dots {
  display: flex;
  gap: 8px;
  padding: 12px 24px;
  background: var(--surface);
  border-radius: 40px;
  box-shadow: 0 4px 24px var(--shadow);
}

#thinking-dots span {
  font-size: 2rem;
  color: var(--accent);
  animation: dot-bounce 1.4s ease-in-out infinite;
}

#thinking-dots span:nth-child(2) { animation-delay: 0.2s; }
#thinking-dots span:nth-child(3) { animation-delay: 0.4s; }

@keyframes dot-bounce {
  0%, 80%, 100% { transform: translateY(0); opacity: 0.4; }
  40% { transform: translateY(-12px); opacity: 1; }
}

@media (max-width: 380px) {
  #app { padding: 6px; gap: 4px; }
  #header { padding: 2px 2px; }
  #title { font-size: 1.15rem; }
  #status { font-size: 0.75rem; }
  select, button { font-size: 0.7rem; padding: 5px 10px; }
  #footer { gap: 12px; }
  #game-over-box { padding: 28px 24px; }
  #game-over-text { font-size: 1.4rem; }
}

@media (min-width: 800px) {
  #app { padding: 16px; }
  #header { padding: 8px 8px; }
  #board {
    max-width: 560px;
    max-height: 560px;
  }
}

@media (min-width: 1200px) {
  #board {
    max-width: 600px;
    max-height: 600px;
  }
}
