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

:root {
  --light-sq: #e8dcc8;
  --dark-sq: #9e7b5b;
  --select: rgba(255, 255, 0, 0.45);
  --legal: rgba(0, 0, 0, 0.18);
  --last-move: rgba(155, 199, 0, 0.42);
  --check: radial-gradient(ellipse at center, rgba(255,0,0,0.6) 0%, rgba(255,0,0,0) 70%);
  --illegal-flash: rgba(255, 60, 60, 0.6);
}

html, body {
  height: 100%;
  background: #1a1a2e;
  color: #eee;
  font-family: system-ui, -apple-system, sans-serif;
  touch-action: manipulation;
  -webkit-tap-highlight-color: transparent;
  user-select: none;
  -webkit-user-select: none;
  overflow: hidden;
}

#app {
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  height: 100%;
  height: 100dvh;
  padding: env(safe-area-inset-top) 8px env(safe-area-inset-bottom) 8px;
  gap: 12px;
}

#board-container {
  display: flex;
  flex-direction: column;
  align-items: center;
}

.coords {
  display: flex;
  font-size: 11px;
  color: #aaa;
  font-weight: 600;
}

.coords-files {
  flex-direction: row;
  width: var(--board-size);
  padding: 0 2px;
}

.coords-files span {
  flex: 1;
  text-align: center;
  line-height: 18px;
}

.coords-ranks {
  flex-direction: column;
  height: var(--board-size);
  width: 18px;
}

.coords-ranks span {
  flex: 1;
  display: flex;
  align-items: center;
  justify-content: center;
}

#board-row {
  display: flex;
  align-items: center;
}

#board {
  width: var(--board-size);
  height: var(--board-size);
  display: grid;
  grid-template-columns: repeat(8, 1fr);
  grid-template-rows: repeat(8, 1fr);
  border: 2px solid #333;
  border-radius: 3px;
  overflow: hidden;
  position: relative;
}

.square {
  position: relative;
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: var(--piece-size);
  line-height: 1;
  cursor: pointer;
  -webkit-tap-highlight-color: transparent;
}

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

.square.selected::after {
  content: '';
  position: absolute;
  inset: 0;
  background: var(--select);
  pointer-events: none;
}

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

.square.legal-move::before {
  content: '';
  position: absolute;
  width: 30%;
  height: 30%;
  border-radius: 50%;
  background: var(--legal);
  pointer-events: none;
  z-index: 1;
}

.square.legal-capture::before {
  content: '';
  position: absolute;
  width: 85%;
  height: 85%;
  border-radius: 50%;
  border: 5px solid var(--legal);
  background: transparent;
  pointer-events: none;
  z-index: 1;
}

.square.in-check {
  background-image: var(--check);
}

.square.illegal-flash {
  animation: flashIllegal 0.35s ease-out;
}

@keyframes flashIllegal {
  0% { background-color: var(--illegal-flash); }
  100% {}
}

.piece {
  position: relative;
  z-index: 2;
  pointer-events: none;
  filter: drop-shadow(1px 1px 1px rgba(0,0,0,0.3));
}

#status-bar {
  text-align: center;
  font-size: 16px;
  font-weight: 600;
  min-height: 28px;
  display: flex;
  align-items: center;
  justify-content: center;
}

#controls {
  display: flex;
  gap: 12px;
}

#btn-new {
  padding: 10px 28px;
  font-size: 15px;
  font-weight: 600;
  background: #2a2a4a;
  color: #eee;
  border: 1px solid #444;
  border-radius: 8px;
  cursor: pointer;
  -webkit-tap-highlight-color: transparent;
  transition: background 0.15s;
}

#btn-new:active {
  background: #3a3a5a;
}

#promotion-modal {
  position: fixed;
  inset: 0;
  background: rgba(0,0,0,0.7);
  display: flex;
  align-items: center;
  justify-content: center;
  z-index: 100;
}

#promotion-modal.hidden { display: none; }

#promotion-choices {
  display: flex;
  gap: 8px;
  background: #2a2a4a;
  padding: 16px;
  border-radius: 12px;
  border: 1px solid #555;
}

.promo-btn {
  width: 64px;
  height: 64px;
  font-size: 48px;
  line-height: 1;
  display: flex;
  align-items: center;
  justify-content: center;
  background: var(--light-sq);
  border: 2px solid #888;
  border-radius: 8px;
  cursor: pointer;
}

.promo-btn:active {
  border-color: #fff;
}

@media (orientation: landscape) {
  :root {
    --board-size: min(80vh, 80vw);
    --piece-size: calc(var(--board-size) / 9.5);
  }
}

@media (orientation: portrait) {
  :root {
    --board-size: min(96vw, 96vw);
    --piece-size: calc(var(--board-size) / 9.5);
  }
}
