:root {
  --bg: var(--tg-theme-bg-color, #ffffff);
  --text: var(--tg-theme-text-color, #1c1c1e);
  --hint: var(--tg-theme-hint-color, #8e8e93);
  --accent: var(--tg-theme-button-color, #2481cc);
  --accent-text: var(--tg-theme-button-text-color, #ffffff);
  --panel: var(--tg-theme-secondary-bg-color, #f2f2f7);
  --light-square: #eeeed2;
  --dark-square: #769656;
  --selected: rgba(255, 214, 10, 0.55);
  --last-move: rgba(255, 214, 10, 0.3);
  --check: radial-gradient(circle, rgba(255, 59, 48, 0.85) 0%, rgba(255, 59, 48, 0) 75%);
}

* { box-sizing: border-box; }

body {
  margin: 0;
  background: var(--bg);
  color: var(--text);
  font: 15px/1.4 -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, sans-serif;
  -webkit-tap-highlight-color: transparent;
}

.app {
  max-width: 480px;
  margin: 0 auto;
  padding: 12px 12px 24px;
}

.status {
  min-height: 44px;
  padding: 10px 12px;
  margin-bottom: 12px;
  border-radius: 12px;
  background: var(--panel);
  font-weight: 600;
  text-align: center;
}

.status.my-turn {
  background: var(--accent);
  color: var(--accent-text);
}

.board-wrap { position: relative; }

.material {
  display: flex;
  align-items: center;
  min-height: 26px;
  padding: 2px 4px;
}

.material .piece {
  width: 22px;
  height: 22px;
  margin-right: -7px; /* overlap like a captured-pieces tray */
}

/* black pieces vanish on Telegram's dark background without a light halo */
.material .piece.b {
  filter: drop-shadow(0 0 1px rgba(255, 255, 255, 0.9)) drop-shadow(0 0 1px rgba(255, 255, 255, 0.9));
}

.material .advantage {
  margin-left: 12px;
  font-weight: 700;
  color: var(--hint);
}

.board {
  display: grid;
  grid-template-columns: repeat(8, minmax(0, 1fr));
  grid-template-rows: repeat(8, minmax(0, 1fr));
  aspect-ratio: 1;
  border-radius: 8px;
  overflow: hidden;
  user-select: none;
}

.square {
  position: relative;
  display: flex;
  align-items: center;
  justify-content: center;
}

.square.light { background: var(--light-square); }
.square.dark { background: var(--dark-square); }
.square.last-move::before,
.square.selected::before { content: ""; position: absolute; inset: 0; }
.square.last-move::before { background: var(--last-move); }
.square.selected::before { background: var(--selected); }
.square.check { background-image: var(--check); }

.square.target::after {
  content: "";
  position: absolute;
  width: 30%;
  height: 30%;
  border-radius: 50%;
  background: rgba(0, 0, 0, 0.2);
}

.square.target.capture::after {
  width: 90%;
  height: 90%;
  background: transparent;
  border: 4px solid rgba(0, 0, 0, 0.2);
}

.piece {
  position: relative;
  z-index: 1;
  width: 90%;
  height: 90%;
  pointer-events: none;
}

.coord {
  position: absolute;
  font-size: 10px;
  font-weight: 600;
  opacity: 0.7;
  color: #333;
}
.coord.file { right: 3px; bottom: 1px; }
.coord.rank { left: 3px; top: 1px; }

.promotion {
  position: absolute;
  inset: 0;
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 8px;
  background: rgba(0, 0, 0, 0.55);
  border-radius: 8px;
}

.promotion button {
  display: flex;
  align-items: center;
  justify-content: center;
  width: 64px;
  height: 64px;
  padding: 4px;
  border: none;
  border-radius: 12px;
  background: var(--light-square);
  cursor: pointer;
}

.confirm {
  display: flex;
  align-items: center;
  gap: 8px;
  margin-top: 12px;
  padding: 10px 12px;
  border-radius: 12px;
  background: var(--accent);
  color: var(--accent-text);
}

.confirm-text {
  flex: 1;
  font-weight: 700;
}

.btn {
  padding: 8px 14px;
  border: none;
  border-radius: 10px;
  font: inherit;
  font-weight: 600;
  cursor: pointer;
}

.btn.primary { background: var(--accent-text); color: var(--accent); }
.btn.secondary { background: transparent; color: var(--accent-text); border: 1px solid currentColor; }
.btn.danger { width: 100%; background: transparent; color: #e53935; border: 1px solid #e53935; }

.controls { display: grid; gap: 10px; }

.switch {
  display: flex;
  align-items: center;
  gap: 10px;
  cursor: pointer;
}

.switch input {
  width: 20px;
  height: 20px;
  accent-color: var(--accent);
}

.teams {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 8px;
  margin: 12px 0;
}

.team, .panel {
  padding: 10px 12px;
  border-radius: 12px;
  background: var(--panel);
}

.team h3, .panel h2 {
  margin: 0 0 6px;
  font-size: 13px;
  font-weight: 600;
  text-transform: uppercase;
  color: var(--hint);
}

.team ul { margin: 0; padding-left: 18px; }
.team li.next { font-weight: 700; }
.panel + .panel { margin-top: 8px; }

.moves {
  margin: 0;
  padding: 0;
  list-style: none;
  max-height: 220px;
  overflow-y: auto;
}

.moves li { display: grid; grid-template-columns: 32px 1fr 1fr; gap: 8px; }
.moves .num { color: var(--hint); }
.moves .who { color: var(--hint); font-size: 12px; margin-left: 4px; }

.best { margin: 0; padding-left: 0; list-style: none; }
.best li { padding: 2px 0; }

.toast {
  position: fixed;
  left: 50%;
  bottom: 24px;
  transform: translateX(-50%);
  padding: 10px 16px;
  border-radius: 10px;
  background: rgba(0, 0, 0, 0.8);
  color: #fff;
}

.hidden { display: none !important; }
