:root {
  --light: #f0d9b5;
  --dark: #b58863;
  --sel: rgba(20, 85, 30, 0.45);
  --lastmove: rgba(255, 214, 0, 0.42);
  --check: rgba(220, 40, 40, 0.55);
  --dot: rgba(20, 20, 20, 0.30);
  --bg: var(--tg-theme-bg-color, #17181c);
  --text: var(--tg-theme-text-color, #eaeaea);
  --btn: var(--tg-theme-button-color, #3390ec);
  --btn-text: var(--tg-theme-button-text-color, #fff);
}

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

body {
  margin: 0;
  background: var(--bg);
  color: var(--text);
  font-family: -apple-system, system-ui, "Segoe UI", Roboto, sans-serif;
  overscroll-behavior: none;
  touch-action: manipulation;
}

#app {
  max-width: 480px;
  margin: 0 auto;
  padding: 12px;
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 12px;
  min-height: 100vh;
}

.status {
  font-size: 16px;
  font-weight: 600;
  min-height: 22px;
  text-align: center;
}

#board-wrap { width: min(94vw, 456px); }

#board {
  width: 100%;
  aspect-ratio: 1 / 1;
  display: grid;
  grid-template-columns: repeat(8, 1fr);
  grid-template-rows: repeat(8, 1fr);
  border-radius: 8px;
  overflow: hidden;
  box-shadow: 0 8px 28px rgba(0, 0, 0, 0.45);
  user-select: none;
}

.sq {
  position: relative;
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: min(10.8vw, 52px);
  line-height: 1;
  cursor: pointer;
}
.sq.light { background: var(--light); }
.sq.dark  { background: var(--dark); }

.sq.sel::after,
.sq.lastmove::before,
.sq.check::before {
  content: "";
  position: absolute;
  inset: 0;
}
.sq.lastmove::before { background: var(--lastmove); }
.sq.sel::after       { background: var(--sel); }
.sq.check::before    { background: var(--check); border-radius: 50%; }

/* legal-move markers sit above the square tint, below the piece */
.sq.target::after {
  content: "";
  position: absolute;
  width: 32%;
  height: 32%;
  border-radius: 50%;
  background: var(--dot);
}
.sq.target.capture::after {
  width: 86%;
  height: 86%;
  background: transparent;
  border: 6px solid var(--dot);
}

.piece {
  position: relative;
  z-index: 2;
  pointer-events: none;
  /* Outline via text-shadow, NOT -webkit-text-stroke: iOS WebKit doesn't
     honour `paint-order` for HTML text, so a stroke there paints over the
     fill and swallows thin glyphs (white pawns looked black on iOS). A
     4-corner shadow draws the outline around the glyph on every platform. */
  -webkit-text-stroke: 0;
}
.piece.w {
  color: #f7f6f2;
  text-shadow: -1px -1px 0 #3a3a3a, 1px -1px 0 #3a3a3a,
               -1px 1px 0 #3a3a3a, 1px 1px 0 #3a3a3a, 0 2px 2px rgba(0,0,0,0.28);
}
.piece.b {
  color: #1b1e23;
  text-shadow: -1px -1px 0 #000, 1px -1px 0 #000,
               -1px 1px 0 #000, 1px 1px 0 #000, 0 2px 2px rgba(0,0,0,0.22);
}

.coord {
  position: absolute;
  font-size: 9px;
  font-weight: 700;
  opacity: 0.6;
  z-index: 1;
}
.coord.file { right: 3px; bottom: 1px; }
.coord.rank { left: 2px;  top: 1px; }
.sq.light .coord { color: #7a5b39; }
.sq.dark  .coord { color: #f0d9b5; }

/* captured-piece trays above and below the board */
.tray {
  width: min(94vw, 456px);
  min-height: 24px;
  display: flex;
  align-items: center;
  gap: 1px;
  font-size: 20px;
  line-height: 1;
}
.tray .cap.w {
  color: #f7f6f2;
  text-shadow: -1px -1px 0 #333, 1px -1px 0 #333, -1px 1px 0 #333, 1px 1px 0 #333;
}
.tray .cap.b { color: #9aa0aa; }  /* muted silhouette, visible on dark bg */
.tray .score { margin-left: 6px; font-size: 13px; font-weight: 700; opacity: 0.8; }

.controls { display: flex; gap: 8px; flex-wrap: wrap; justify-content: center; }
.btn.sm { padding: 9px 14px; font-size: 14px; border-radius: 9px; }
.btn.danger { background: #c0392b; color: #fff; }
.btn:disabled { opacity: 0.4; pointer-events: none; }

.banner {
  width: min(94vw, 456px);
  background: rgba(255,255,255,0.07);
  border: 1px solid rgba(255,255,255,0.16);
  border-radius: 12px;
  padding: 12px 14px;
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 10px;
  font-size: 14px;
  font-weight: 600;
}
.banner.hidden { display: none; }
.banner.win { background: rgba(46,160,67,0.18); border-color: rgba(46,160,67,0.5); }
.banner .banner-actions { display: flex; gap: 8px; flex-shrink: 0; }

.history {
  width: min(94vw, 456px);
  max-height: 96px;
  overflow-y: auto;
  background: rgba(255,255,255,0.05);
  border-radius: 10px;
  padding: 8px 12px;
  font-size: 13px;
  line-height: 1.7;
  font-variant-numeric: tabular-nums;
  color: var(--text);
}
.history:empty { display: none; }
.history .mv-no { opacity: 0.5; margin-right: 3px; }
.history .mv { margin-right: 10px; }

.toast {
  position: fixed;
  bottom: 24px;
  left: 50%;
  transform: translateX(-50%);
  background: rgba(0,0,0,0.85);
  color: #fff;
  padding: 10px 16px;
  border-radius: 20px;
  font-size: 14px;
  z-index: 20;
  transition: opacity 0.3s;
}
.toast.hidden { opacity: 0; pointer-events: none; }

.muted { opacity: 0.7; font-size: 13px; }
.btn {
  background: var(--btn);
  color: var(--btn-text);
  border: none;
  border-radius: 10px;
  padding: 11px 18px;
  font-size: 15px;
  font-weight: 600;
  cursor: pointer;
}
.btn.ghost { background: transparent; color: var(--text); border: 1px solid rgba(255,255,255,0.25); }
.btn.big { width: 100%; padding: 15px; font-size: 17px; }

.overlay {
  position: fixed;
  inset: 0;
  background: rgba(0, 0, 0, 0.62);
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 20px;
  z-index: 10;
}
.overlay.hidden { display: none; }
.card {
  background: var(--bg);
  border: 1px solid rgba(255,255,255,0.12);
  border-radius: 16px;
  padding: 22px;
  width: 100%;
  max-width: 340px;
  display: flex;
  flex-direction: column;
  gap: 14px;
  text-align: center;
}
.card h1 { margin: 0 0 4px; font-size: 24px; }
.seg-row { display: flex; flex-direction: column; gap: 8px; align-items: center; }
.seg-row > span { font-size: 13px; opacity: 0.75; }
.seg-group { display: flex; width: 100%; gap: 6px; }
.seg {
  flex: 1;
  background: rgba(255,255,255,0.08);
  border: 1px solid rgba(255,255,255,0.16);
  color: var(--text);
  border-radius: 10px;
  padding: 9px 4px;
  font-size: 14px;
  font-weight: 600;
  cursor: pointer;
}
.seg.active { background: var(--btn); color: var(--btn-text); border-color: var(--btn); }

.levels { display: flex; align-items: center; gap: 10px; font-size: 14px; justify-content: center; }
.levels input { flex: 1; }
.join { display: flex; gap: 8px; }
.join input { flex: 1; }
input[type=text], .join input, #join-code {
  background: rgba(255,255,255,0.08);
  border: 1px solid rgba(255,255,255,0.18);
  color: var(--text);
  border-radius: 10px;
  padding: 10px;
  font-size: 15px;
}
.promo-card { max-width: 280px; }
.promo-row { display: flex; justify-content: space-around; }
.promo-row .pc {
  font-size: 44px;
  cursor: pointer;
  color: #f7f6f2;
  text-shadow: -1px -1px 0 #3a3a3a, 1px -1px 0 #3a3a3a,
               -1px 1px 0 #3a3a3a, 1px 1px 0 #3a3a3a;
}
