* { margin: 0; padding: 0; box-sizing: border-box; -webkit-tap-highlight-color: transparent; }

:root {
  --bg: #171a38; --panel: #212549; --panel2: #2a2f5e;
  --ink: #eef0ff; --muted: rgba(238,240,255,.6);
  --yellow: #fcc419;
  --sqLight: #ecebd7; --sqDark: #6f9b57;
}

html, body {
  width: 100%; height: 100%; overflow: hidden;
  background: radial-gradient(circle at 50% 20%, #232860, var(--bg) 70%);
  font-family: -apple-system, "SF Pro Display", "Segoe UI", Roboto, sans-serif;
  color: var(--ink);
  user-select: none; -webkit-user-select: none;
  touch-action: manipulation;
}

.hidden { display: none !important; }
.screen { position: fixed; inset: 0; display: flex; align-items: center; justify-content: center; overflow-y: auto; -webkit-overflow-scrolling: touch; z-index: 10; padding: max(12px, env(safe-area-inset-top)) 12px max(12px, env(safe-area-inset-bottom)); }
#game.screen { z-index: 5; padding: 0; }
.panel { position: relative; z-index: 1; text-align: center; padding: 26px; width: min(400px, 94vw); margin: auto; animation: pin .3s ease; }

/* ambient chess theming in the dead bands around panels and board */
#home::before, #home::after, #game::before, #game::after {
  position: fixed; z-index: 0; pointer-events: none;
  line-height: 1; opacity: .05; filter: blur(1px);
  font-size: 36vmin;
}
#home::before { content: '\265C'; top: -5vmin; left: -7vmin; transform: rotate(-16deg); color: #a9b8ff; }
#home::after { content: '\265D'; bottom: -6vmin; right: -7vmin; transform: rotate(14deg); color: #ffd75e; }
#game { background: radial-gradient(ellipse at 50% 46%, rgba(90, 110, 220, .16), transparent 62%); }
#game::before { content: '\265E'; top: -4vmin; left: -7vmin; transform: rotate(-14deg); color: #a9b8ff; }
#game::after { content: '\265B'; bottom: -5vmin; right: -7vmin; transform: rotate(12deg); color: #ffd75e; }
@keyframes pin { from { transform: translateY(16px) scale(.97); opacity: 0; } }

.logoChess { font-size: 56px; margin-bottom: 0; filter: drop-shadow(0 6px 16px rgba(255,255,255,.25)); }
h1 { font-size: 44px; font-weight: 900; letter-spacing: 2px; line-height: .95; margin-bottom: 12px; }
h1 span {
  display: block; font-size: 21px; letter-spacing: 6px;
  background: linear-gradient(135deg, #ffe07a, #fcc419, #ff8a3d);
  -webkit-background-clip: text; background-clip: text; -webkit-text-fill-color: transparent;
}
h2 { font-size: 26px; font-weight: 900; letter-spacing: 3px; margin-bottom: 16px; }

input {
  width: 100%; padding: 13px 16px; margin: 10px 0 6px;
  border-radius: 14px; border: 2px solid var(--panel2);
  background: var(--panel); color: var(--ink);
  font-size: 16px; font-weight: 700; text-align: center; outline: none;
}
input:focus { border-color: var(--yellow); }

button { font-family: inherit; cursor: pointer; }
button.big {
  display: block; width: 100%; margin: 10px 0; padding: 14px 0;
  font-size: 17px; font-weight: 900; letter-spacing: 1.5px;
  color: #08331a; background: linear-gradient(135deg, #6dedb0, #22c76a);
  border: none; border-radius: 16px;
  box-shadow: 0 5px 0 #169148, 0 10px 24px rgba(0,0,0,.35);
  transition: transform .08s;
}
button.big:active { transform: translateY(3px); box-shadow: 0 2px 0 #169148; }
button.big.alt { color: #0b2d4a; background: linear-gradient(135deg, #74c0fc, #339af0); box-shadow: 0 5px 0 #1c7ed6, 0 10px 24px rgba(0,0,0,.35); }
button.big.alt2 { color: #4a2c00; background: linear-gradient(135deg, #ffe07a, #fcc419); box-shadow: 0 5px 0 #e0a800, 0 10px 24px rgba(0,0,0,.35); }
button.big:disabled { filter: grayscale(.8); opacity: .5; }
.ghost { display: inline-block; background: none; border: none; color: var(--muted); font-size: 14px; font-weight: 700; margin-top: 12px; text-decoration: none; padding: 8px; }
.mini { background: var(--panel2); color: var(--ink); border: none; padding: 12px 14px; min-height: 44px; border-radius: 12px; font-size: 13px; font-weight: 800; }
.mini.danger { background: rgba(224,49,49,.25); color: #ff9d9d; }
.mini:disabled { opacity: .4; }
.err { color: #ff8787; font-weight: 700; font-size: 14px; min-height: 20px; margin-top: 8px; }
.hint { color: var(--muted); font-size: 13px; font-weight: 600; }
.orSep { color: var(--muted); font-size: 13px; font-weight: 700; margin: 14px 0 6px; }
.joinRow { display: flex; gap: 8px; }
.joinRow input { flex: 1; margin: 0; text-transform: uppercase; letter-spacing: 4px; }
.joinRow button { padding: 0 22px; border: none; border-radius: 14px; font-weight: 900; background: var(--yellow); color: #4a2c00; font-size: 15px; }
.roomCode { font-size: 44px; font-weight: 900; letter-spacing: 10px; color: var(--yellow); margin: 4px 0 10px; text-shadow: 0 4px 18px rgba(252,196,25,.35); }
.lobbyPlayers { margin: 14px 0 10px; display: flex; flex-direction: column; gap: 8px; }
.lobbySeat { display: flex; align-items: center; gap: 10px; background: var(--panel); border-radius: 14px; padding: 8px 12px; font-weight: 800; font-size: 15px; }
.lobbySeat .seatAv { width: 38px; height: 38px; border-radius: 50%; flex: none; display: flex; align-items: center; justify-content: center; font-size: 21px; background: var(--panel2); }
.lobbySeat .tag { margin-left: auto; font-size: 11px; font-weight: 800; color: var(--muted); letter-spacing: 1px; }
.lobbySeat.empty { opacity: .35; font-weight: 600; }
.lobbyBtns { display: flex; gap: 8px; justify-content: center; margin: 10px 0; }
.countRow { display: flex; gap: 10px; justify-content: center; margin-bottom: 10px; flex-wrap: wrap; }
.countBtn {
  min-width: 62px; padding: 10px 8px; border-radius: 16px;
  border: 2px solid var(--panel2); background: var(--panel);
  color: var(--ink); font-size: 15px; font-weight: 800; line-height: 1.3;
}
.countBtn.sel { border-color: var(--yellow); background: var(--panel2); box-shadow: 0 0 18px rgba(252,196,25,.25); }
.promoBtn { font-size: 30px; }

/* ================= game ================= */
#gameWrap {
  position: relative; z-index: 1;
  width: 100%; height: 100%;
  display: flex; flex-direction: column; align-items: center; justify-content: center;
  padding: max(8px, env(safe-area-inset-top)) 0 max(8px, env(safe-area-inset-bottom));
  gap: 6px;
}
.pRow {
  width: min(94vmin, 560px);
  display: flex; align-items: center; justify-content: space-between;
}
.pInfo { display: flex; align-items: center; gap: 8px; min-width: 0; }
.pav { font-size: 26px; }
.pnm { font-weight: 800; font-size: 14px; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; max-width: 40vw; }
.caps { font-size: 13px; min-height: 18px; display: flex; align-items: center; }
.caps .capImg { width: 16px; height: 16px; margin-right: -5px; filter: drop-shadow(0 1px 1px rgba(0,0,0,.4)); }
.caps .diff { color: var(--yellow); font-weight: 800; font-size: 11px; margin-left: 4px; }
.clock {
  background: var(--panel); border: 2px solid transparent;
  border-radius: 12px; padding: 7px 14px;
  font-weight: 900; font-size: 18px; font-variant-numeric: tabular-nums;
}
.clock.active { border-color: var(--yellow); box-shadow: 0 0 16px rgba(252,196,25,.35); color: var(--yellow); }
.clock.low { color: #ff8787; animation: blink 1s ease infinite; }
@keyframes blink { 50% { opacity: .5; } }

#boardWrap {
  position: relative;
  width: min(94vmin, 560px);
  aspect-ratio: 1;
  border-radius: 2%;
  overflow: hidden;
  border: 5px solid #57402f;
  outline: 3px solid #3a2b22;
  box-shadow: 0 18px 48px rgba(0,0,0,.55), inset 0 0 40px rgba(60,40,10,.12);
  touch-action: none;
}
#pieceLayer { position: absolute; inset: 0; pointer-events: none; z-index: 3; }
.piece {
  position: absolute; left: 0; top: 0;
  width: 12.5%; height: 12.5%;
  display: flex; align-items: center; justify-content: center;
  pointer-events: auto; cursor: grab;
  transition: transform .16s cubic-bezier(.2,.8,.3,1);
  will-change: transform;
}
.piece img {
  width: 90%; height: 90%;
  filter: drop-shadow(0 2px 2px rgba(0,0,0,.35));
  pointer-events: none;
  transition: transform .12s, filter .12s;
}
.piece.drag { transition: none; z-index: 30; cursor: grabbing; }
.piece.drag img { transform: scale(1.22) translateY(-8%); filter: drop-shadow(0 12px 14px rgba(0,0,0,.45)); }
.piece.fade { opacity: 0; transition: opacity .18s ease, transform .18s ease; }
.piece.fade img { transform: scale(.6); }
#cboard {
  width: 100%; height: 100%;
  display: grid;
  grid-template: repeat(8, 1fr) / repeat(8, 1fr);
}
.sq {
  position: relative;
  display: flex; align-items: center; justify-content: center;
  line-height: 1;
}
@media (min-width: 640px) { .sq { font-size: 48px; } }
.sq.light { background: var(--sqLight); }
.sq.dark { background: var(--sqDark); }

.sq.lastmove::before { content: ''; position: absolute; inset: 0; background: rgba(246,220,80,.42); }
.sq.selected::before { content: ''; position: absolute; inset: 0; background: rgba(30,120,60,.35); box-shadow: inset 0 0 0 3px rgba(255,255,255,.55); }
.sq.check::before { content: ''; position: absolute; inset: 0; background: radial-gradient(circle, rgba(237,43,51,.75), rgba(237,43,51,.15)); }
.sq .dot {
  position: absolute; width: 30%; height: 30%;
  border-radius: 50%; background: radial-gradient(circle, rgba(0,0,0,.22), rgba(0,0,0,.16));
  z-index: 1;
}
.sq .ring {
  position: absolute; inset: 6%;
  border: 4px solid rgba(0,0,0,.3); border-radius: 50%;
  z-index: 1;
}
.sq.hintFrom::after, .sq.hintTo::after {
  content: ''; position: absolute; inset: 4%;
  border: 3px dashed rgba(39,174,255,.9); border-radius: 12%;
  animation: blink 1s ease infinite;
}
.sq .coord {
  position: absolute; font-size: 1.8vmin; font-weight: 800;
  opacity: .55; -webkit-text-stroke: 0;
}
.sq .coord.file { right: 4%; bottom: 2%; }
.sq .coord.rank { left: 5%; top: 3%; }

#chessActions { display: flex; gap: 8px; margin-top: 2px; }
#turnLabel { font-size: 14px; font-weight: 800; letter-spacing: 1px; color: var(--muted); min-height: 20px; }

.mini:active { transform: scale(.94); }

#confirmModal { background: rgba(13,16,40,.72); z-index: 40; }
.confirmPanel { background: var(--panel); border-radius: 20px; width: min(300px, 86vw); padding: 22px; box-shadow: 0 18px 48px rgba(0,0,0,.5); }
#confirmMsg { font-weight: 800; font-size: 16px; margin-bottom: 16px; }
.confirmBtn { min-width: 104px; padding: 13px 14px; font-size: 14px; }

#toast {
  position: absolute; top: 12%; left: 50%; transform: translateX(-50%);
  background: rgba(0,0,0,.7); color: #fff; font-weight: 800; font-size: 14px;
  padding: 8px 18px; border-radius: 18px; opacity: 0; transition: opacity .25s;
  pointer-events: none; white-space: nowrap; z-index: 30;
}
#toast.show { opacity: 1; }

#fx { position: fixed; inset: 0; pointer-events: none; z-index: 50; }

/* final-position recap shown dimmed behind the end modal */
.recapBoard {
  position: relative;
  width: min(86vmin, 460px); aspect-ratio: 1;
  border-radius: 2%; overflow: hidden;
  border: 5px solid #57402f; outline: 3px solid #3a2b22;
}
.recapBoard * { pointer-events: none !important; }
.recapBoard .recapGrid { width: 100%; height: 100%; display: grid; grid-template: repeat(8, 1fr) / repeat(8, 1fr); }
.recapBoard .recapPieces { position: absolute; inset: 0; }

/* ---------- rating / puzzles ---------- */
.statRow { display: flex; gap: 10px; justify-content: center; margin: 4px 0 12px; }
.statChip {
  background: var(--panel); border-radius: 14px; padding: 7px 14px;
  font-size: 13px; font-weight: 700; color: var(--muted);
}
.statChip b { color: var(--yellow); font-size: 15px; }
#puzzlePrompt {
  background: var(--panel); border: 2px solid var(--yellow);
  border-radius: 14px; padding: 8px 18px;
  font-weight: 900; font-size: 15px; letter-spacing: .5px;
  box-shadow: 0 0 18px rgba(252,196,25,.2);
}
#puzzleDone {
  display: flex; align-items: center; gap: 12px;
  background: linear-gradient(135deg, #6dedb0, #22c76a);
  color: #08331a; font-weight: 900; font-size: 15px;
  border-radius: 14px; padding: 8px 10px 8px 16px;
  animation: pin .3s ease;
}
#btnNextPuzzle {
  border: none; border-radius: 10px; padding: 8px 16px;
  background: #08331a; color: #6dedb0; font-weight: 900; font-size: 13px;
}
.puzzleMode .clock, .puzzleMode #btnResign, .puzzleMode #btnTakeback { display: none; }

/* ---------- post-game review (rendered inside the end modal card) ---------- */
.reviewBox { position: relative; margin: 12px 0 0; display: flex; flex-direction: column; gap: 6px; text-align: left; }
.rvTitle { text-align: center; font-size: 12px; font-weight: 900; letter-spacing: 2px; color: var(--muted); margin-bottom: 2px; }
.rvRow { display: flex; align-items: center; gap: 8px; background: var(--panel); border-radius: 12px; padding: 8px 12px; font-size: 13px; font-weight: 700; }
.rvRow .rvIc { font-size: 14px; width: 18px; text-align: center; flex: none; }
.rvRow.good .rvIc { color: #6dedb0; }
.rvRow.bad .rvIc { color: #ff8787; }
.rvRow .rvMv { font-weight: 900; white-space: nowrap; }
.rvRow .rvNote { color: var(--muted); font-size: 12px; margin-left: auto; text-align: right; }
.rvRow.clean .rvNote { color: #6dedb0; margin-left: 0; text-align: left; }

/* shop board themes */
body[data-ctheme="wood"] .sq.light { background: #e8d0b0; }
body[data-ctheme="wood"] .sq.dark { background: #9a6b45; }
body[data-ctheme="ice"] .sq.light { background: #e7f5ff; }
body[data-ctheme="ice"] .sq.dark { background: #74a9d8; }
