* { box-sizing: border-box; }

body {
  margin: 0;
  font-family: system-ui, -apple-system, 'Segoe UI', sans-serif;
  background: #312e2b;
  color: #c3c2c1;
  display: flex;
  justify-content: center;
  min-height: 100vh;
}

main {
  padding: 20px;
  display: flex;
  flex-direction: column;
  align-items: flex-start;
}

h1 {
  margin: 0 0 14px;
  font-size: 19px;
  font-weight: 700;
  color: #ffffff;
  letter-spacing: .2px;
}

.controls {
  display: flex;
  gap: 10px;
  align-items: center;
  margin-bottom: 14px;
}

select {
  font: inherit;
  font-size: 14px;
  padding: 8px 12px;
  border: none;
  border-radius: 5px;
  background: #454341;
  color: #e8e7e6;
  cursor: pointer;
}

button#new {
  font: inherit;
  font-size: 14px;
  font-weight: 700;
  padding: 8px 20px;
  border: none;
  border-radius: 5px;
  background: #81b64c;
  color: #ffffff;
  box-shadow: 0 4px 0 #45753c;
  cursor: pointer;
  transition: filter .12s ease, transform .06s ease, box-shadow .06s ease;
}

button#new:hover { filter: brightness(1.07); }
button#new:active { transform: translateY(3px); box-shadow: 0 1px 0 #45753c; }

/* ---------- game column ---------- */

.game {
  width: min(94vw, calc(100vh - 270px), 760px);
}

/* Player bars mirror chess.com's avatar + name + captured-material rows,
   so a future OAuth profile only has to fill .avatar and .player-name. */
.player-bar {
  display: flex;
  align-items: center;
  gap: 10px;
  margin: 7px 0;
}

.avatar {
  width: 36px;
  height: 36px;
  border-radius: 4px;
  background-color: #454341;
  background-image: url("data:image/svg+xml,<svg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 24 24' fill='%23918f8d'><path d='M12 12.5a4.25 4.25 0 1 0-4.25-4.25A4.25 4.25 0 0 0 12 12.5zm0 2.2c-4.2 0-8 2.1-8 5v2.3h16v-2.3c0-2.9-3.8-5-8-5z'/></svg>");
  background-size: 62%;
  background-position: center;
  background-repeat: no-repeat;
}

.player-name {
  display: block;
  font-size: 14px;
  font-weight: 700;
  color: #ffffff;
  line-height: 1.2;
}

.captured {
  display: flex;
  align-items: center;
  min-height: 17px;
  margin-top: 2px;
}

.cap-group { display: flex; }
.cap-group + .cap-group { margin-left: 3px; }

.cap {
  width: 17px;
  height: 17px;
  background-size: cover;
  filter: drop-shadow(0 1px 1px rgba(0, 0, 0, .4));
}

.cap-group .cap:not(:first-child) { margin-left: -10px; }

.cap-score {
  margin-left: 6px;
  font-size: 12px;
  font-weight: 700;
  color: #a09e9c;
}

/* ---------- board ---------- */

#board {
  position: relative;
  width: 100%;
  aspect-ratio: 1;
  border-radius: 5px;
  overflow: hidden;
  box-shadow: 0 10px 34px rgba(0, 0, 0, .5);
  touch-action: none;
  user-select: none;
  -webkit-user-select: none;
  /* Two-square checker tile: top-left light, matching a8. */
  background-image: conic-gradient(#779556 0 25%, #ebecd0 0 50%, #779556 0 75%, #ebecd0 0);
  background-size: 25% 25%;
}

.square {
  position: absolute;
  width: 12.5%;
  height: 12.5%;
  pointer-events: none;
}

/* ---------- highlights ---------- */

.square::before {
  content: '';
  position: absolute;
  inset: 0;
  opacity: 0;
  transition: opacity .12s ease;
}

.square.selected::before,
.square.last::before {
  background: rgba(255, 255, 51, .5);
  opacity: 1;
}

.square.check::before {
  background: radial-gradient(circle, rgba(255, 40, 40, .9) 0%, rgba(255, 40, 40, .55) 45%, rgba(255, 40, 40, 0) 78%);
  opacity: 1;
}

.square.hover::before {
  box-shadow: inset 0 0 0 4px rgba(255, 255, 255, .75);
  opacity: 1;
}

.square.premove::before {
  background: rgba(220, 60, 50, .45);
  opacity: 1;
}

.square.marked::before {
  background: var(--mark, rgba(235, 97, 80, .8));
  opacity: 1;
}

/* Legal-move dot on empty squares, ring on capture targets. */
.square::after {
  content: '';
  position: absolute;
  inset: 0;
  opacity: 0;
  transform: scale(.6);
  transition: opacity .12s ease, transform .12s ease;
}

.square.dest::after {
  inset: 33.5%;
  border-radius: 50%;
  background: rgba(0, 0, 0, .14);
  opacity: 1;
  transform: scale(1);
}

.square.capture::after {
  border-radius: 50%;
  border: 5.5px solid rgba(0, 0, 0, .14);
  opacity: 1;
  transform: scale(1);
}

/* ---------- coordinates ---------- */

.coord {
  position: absolute;
  font-size: clamp(9px, 1.6vmin, 13px);
  font-weight: 700;
  pointer-events: none;
}

.coord.rank { top: 3%; left: 5%; }
.coord.file { bottom: 2%; right: 5%; }
.coord.on-light { color: #779556; }
.coord.on-dark { color: #ebecd0; }

/* ---------- pieces ---------- */

.piece {
  position: absolute;
  width: 12.5%;
  height: 12.5%;
  background-size: 100%;
  background-repeat: no-repeat;
  pointer-events: none;
  will-change: transform;
  transition: transform .16s cubic-bezier(.25, .8, .35, 1), opacity .18s ease;
  z-index: 2;
}

.piece.no-anim { transition: none; }
.piece.moving { z-index: 4; }

.piece.dragging {
  transition: none;
  z-index: 10;
  transform-origin: center;
  scale: 1.08;
  filter: drop-shadow(0 6px 10px rgba(0, 0, 0, .35));
}

.piece.fading { opacity: 0; z-index: 1; }
.piece.entering { opacity: 0; }

/* ---------- arrow layer ---------- */

.arrows {
  position: absolute;
  inset: 0;
  width: 100%;
  height: 100%;
  pointer-events: none;
  z-index: 6;
}

/* ---------- promotion popover ---------- */

.promo-backdrop {
  position: absolute;
  inset: 0;
  background: rgba(34, 32, 30, .6);
  z-index: 20;
}

.promo-panel {
  position: absolute;
  width: 12.5%;
  display: flex;
  flex-direction: column;
  background: #ffffff;
  border-radius: 4px;
  box-shadow: 0 4px 18px rgba(0, 0, 0, .5);
  overflow: hidden;
  z-index: 21;
  animation: promo-in .12s ease;
}

@keyframes promo-in {
  from { opacity: 0; transform: scale(.85); }
  to { opacity: 1; transform: scale(1); }
}

.promo-piece {
  aspect-ratio: 1;
  border: none;
  background-color: transparent;
  background-size: 84%;
  background-position: center;
  background-repeat: no-repeat;
  cursor: pointer;
  transition: background-color .1s ease;
}

.promo-piece:hover { background-color: #f1f1f0; }

/* ---------- game over modal ---------- */

.gameover-backdrop {
  position: absolute;
  inset: 0;
  display: flex;
  align-items: center;
  justify-content: center;
  background: rgba(22, 21, 18, .6);
  z-index: 30;
}

.gameover {
  position: relative;
  width: min(76%, 300px);
  background: #262522;
  border-radius: 12px;
  box-shadow: 0 12px 40px rgba(0, 0, 0, .6);
  overflow: hidden;
  text-align: center;
  animation: gameover-in .18s cubic-bezier(.2, .9, .35, 1.2);
}

@keyframes gameover-in {
  from { opacity: 0; transform: scale(.8) translateY(12px); }
  to { opacity: 1; transform: scale(1) translateY(0); }
}

.gameover-close {
  position: absolute;
  top: 6px;
  right: 10px;
  border: none;
  background: none;
  color: rgba(255, 255, 255, .65);
  font-size: 22px;
  line-height: 1;
  cursor: pointer;
  z-index: 1;
}

.gameover-close:hover { color: #ffffff; }

.gameover-head {
  padding: 22px 16px 14px;
}

.gameover-head.win { background: #81b64c; }
.gameover-head.loss { background: #56524e; }
.gameover-head.draw { background: #56524e; }

.gameover-title {
  font-size: 24px;
  font-weight: 800;
  color: #ffffff;
}

.gameover-sub {
  margin-top: 2px;
  font-size: 13px;
  font-weight: 600;
  color: rgba(255, 255, 255, .75);
}

.gameover-body {
  padding: 16px 20px 20px;
}

.side-label {
  font-size: 12px;
  font-weight: 700;
  color: #a09e9c;
  text-transform: uppercase;
  letter-spacing: .4px;
  margin-bottom: 8px;
}

.side-select {
  display: flex;
  justify-content: center;
  gap: 10px;
  margin-bottom: 16px;
}

.side-btn {
  width: 52px;
  height: 52px;
  border: 2px solid #454341;
  border-radius: 8px;
  background-color: #3a3734;
  background-size: 78%;
  background-position: center;
  background-repeat: no-repeat;
  cursor: pointer;
  transition: border-color .12s ease, transform .08s ease;
}

.side-btn:hover { transform: translateY(-2px); }
.side-btn.picked { border-color: #81b64c; box-shadow: 0 0 0 1px #81b64c inset; }

.rematch {
  width: 100%;
  font: inherit;
  font-size: 16px;
  font-weight: 800;
  padding: 12px 0;
  border: none;
  border-radius: 8px;
  background: #81b64c;
  color: #ffffff;
  box-shadow: 0 4px 0 #45753c;
  cursor: pointer;
  transition: filter .12s ease, transform .06s ease, box-shadow .06s ease;
}

.rematch:hover { filter: brightness(1.07); }
.rematch:active { transform: translateY(3px); box-shadow: 0 1px 0 #45753c; }

/* ---------- status and actions ---------- */

.action-row {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  margin-top: 12px;
}

#status {
  margin: 0;
  min-height: 22px;
  font-size: 15px;
  color: #c3c2c1;
}

#resign {
  display: inline-flex;
  align-items: center;
  gap: 6px;
  font: inherit;
  font-size: 13px;
  font-weight: 700;
  padding: 6px 14px;
  border: none;
  border-radius: 5px;
  background: #454341;
  color: #c3c2c1;
  cursor: pointer;
  transition: background .12s ease, color .12s ease;
}

#resign:hover:not(:disabled) { background: #56524e; color: #ffffff; }
#resign:disabled { opacity: .4; cursor: default; }

/* Two-step confirm: first click arms the button, second resigns. */
#resign.confirm { background: #e02828; color: #ffffff; }
#resign.confirm:hover:not(:disabled) { background: #f43c3c; }

#status.thinking::after {
  content: '';
  display: inline-block;
  width: 1.2em;
  text-align: left;
  animation: dots 1.2s steps(4, end) infinite;
}

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

/* ---------- small screens ---------- */

@media (max-width: 640px) {
  main { padding: 12px; }
  h1 { margin-bottom: 10px; }
  .controls { margin-bottom: 10px; }
  .game { width: min(96vw, calc(100vh - 210px)); }
  .avatar { width: 30px; height: 30px; }
  .player-name { font-size: 13px; }
  .cap { width: 14px; height: 14px; }
  .cap-group .cap:not(:first-child) { margin-left: -8px; }
}
