:root {
  --bg: #14120f;
  --panel: #211e1a;
  --panel-2: #2a2621;
  --text: #f4efe6;
  --muted: #a79f92;
  --accent: #f0b35a;
  --accent-2: #d9923f;
  --danger: #e36b5d;
  --light: #f0d9b5;
  --dark: #b58863;
  --radius: 16px;
  --shadow: 0 18px 50px rgba(0, 0, 0, .38);
}

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

html, body {
  margin: 0;
  min-height: 100%;
  background:
    radial-gradient(1000px 600px at 50% -200px, rgba(240, 179, 90, .12), transparent 60%),
    var(--bg);
  color: var(--text);
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif;
}

body { padding: max(12px, env(safe-area-inset-top)) 12px max(18px, env(safe-area-inset-bottom)); }

button { font: inherit; color: inherit; }

.app {
  width: min(100%, 560px);
  margin: 0 auto;
  display: flex;
  flex-direction: column;
  gap: 12px;
  position: relative;
}

.topbar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 4px 4px 0;
}

.brand {
  color: var(--accent);
  font-size: 12px;
  font-weight: 800;
  letter-spacing: .18em;
}

.game-title {
  font-size: 20px;
  font-weight: 750;
  margin-top: 3px;
}

.icon-btn {
  width: 42px;
  height: 42px;
  border: 1px solid rgba(255,255,255,.08);
  background: var(--panel);
  border-radius: 14px;
  font-size: 20px;
  cursor: pointer;
}
.icon-btn:active { transform: scale(.96); }

.players {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 8px;
}

.player {
  background: linear-gradient(180deg, rgba(255,255,255,.035), transparent), var(--panel);
  border: 1px solid rgba(255,255,255,.06);
  border-radius: 14px;
  padding: 10px 12px;
  min-width: 0;
}

.player.active {
  border-color: rgba(240, 179, 90, .55);
  box-shadow: 0 0 0 1px rgba(240, 179, 90, .12), 0 8px 24px rgba(240, 179, 90, .08);
}

.player .side {
  font-size: 11px;
  color: var(--muted);
  text-transform: uppercase;
  letter-spacing: .08em;
  display: flex;
  gap: 6px;
  align-items: center;
}

.player .name {
  margin-top: 4px;
  font-weight: 700;
  font-size: 15px;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.player .rating {
  font-size: 12px;
  color: var(--muted);
  margin-top: 2px;
}

.player .you {
  color: var(--accent);
  font-size: 11px;
  font-weight: 700;
}

.status-card {
  background: var(--panel);
  border: 1px solid rgba(255,255,255,.06);
  border-radius: var(--radius);
  padding: 12px 14px;
}

.status-main { font-weight: 700; font-size: 15px; }
.status-main.your-turn { color: var(--accent); }
.status-sub { color: var(--muted); font-size: 12px; margin-top: 4px; line-height: 1.4; }
.material { color: #d9d0c1; font-size: 12px; margin-top: 7px; }
.material b { color: var(--text); }

.board-wrap {
  position: relative;
  width: 100%;
  aspect-ratio: 1;
  border-radius: 18px;
  overflow: hidden;
  box-shadow: var(--shadow);
  border: 1px solid rgba(255,255,255,.10);
  background: #2b241d;
}

.board {
  width: 100%;
  height: 100%;
  display: grid;
  grid-template-columns: repeat(8, 1fr);
  grid-template-rows: repeat(8, 1fr);
  touch-action: manipulation;
}

.square {
  position: relative;
  display: flex;
  align-items: center;
  justify-content: center;
  border: 0;
  padding: 0;
  margin: 0;
  cursor: pointer;
  overflow: hidden;
}

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

.square.last::before {
  content: "";
  position: absolute;
  inset: 0;
  background: rgba(246, 196, 83, .28);
  pointer-events: none;
}

.square.selected::after {
  content: "";
  position: absolute;
  inset: 0;
  box-shadow: inset 0 0 0 4px var(--accent);
  pointer-events: none;
  z-index: 3;
}

.square.legal::after {
  content: "";
  position: absolute;
  width: 26%;
  height: 26%;
  border-radius: 50%;
  background: rgba(20, 20, 20, .30);
  pointer-events: none;
  z-index: 2;
}

.square.capture::after {
  width: 78%;
  height: 78%;
  background: transparent;
  border: 5px solid rgba(20, 20, 20, .28);
  box-sizing: border-box;
}

.piece {
  width: 84%;
  height: 84%;
  z-index: 1;
  pointer-events: none;
  filter: drop-shadow(0 2px 1px rgba(0,0,0,.28));
}

.piece.white { --fill: #fbf8f1; --stroke: #1e1b18; }
.piece.black { --fill: #1c1917; --stroke: #f1e7d0; }

.piece svg { width: 100%; height: 100%; display: block; }

.promotion {
  position: absolute;
  inset: 0;
  z-index: 10;
  background: rgba(12, 10, 8, .82);
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 14px;
  backdrop-filter: blur(4px);
}
.promotion-title { font-weight: 700; color: var(--text); }
.promotion-choices { display: flex; flex-wrap: wrap; gap: 10px; justify-content: center; max-width: 330px; }
.promotion-choices button {
  width: 62px;
  height: 62px;
  border-radius: 14px;
  border: 1px solid rgba(255,255,255,.14);
  background: #302b25;
  cursor: pointer;
}
.promotion-choices button .piece { width: 80%; height: 80%; margin: auto; }
.promotion-choices .btn { flex: 0 0 100%; width: auto; height: 40px; }

.controls {
  display: flex;
  flex-wrap: wrap;
  gap: 8px;
}

.btn {
  border: 1px solid rgba(255,255,255,.10);
  background: var(--panel-2);
  border-radius: 12px;
  padding: 10px 14px;
  font-size: 13px;
  font-weight: 650;
  cursor: pointer;
  flex: 1 1 auto;
}
.btn:active { transform: scale(.98); }
.btn:disabled { opacity: .5; cursor: default; }
.btn.primary { background: linear-gradient(180deg, #f1b75f, #d9923f); color: #241a0d; border-color: transparent; }
.btn.ghost { background: transparent; }
.btn.danger { color: #ffd9d4; border-color: rgba(227,107,93,.35); background: rgba(227,107,93,.12); }
.btn.success { color: #d9ffd9; border-color: rgba(110,200,120,.35); background: rgba(110,200,120,.12); }

.moves-card {
  background: var(--panel);
  border: 1px solid rgba(255,255,255,.06);
  border-radius: var(--radius);
  padding: 10px 14px 12px;
}
.moves-title { color: var(--muted); font-size: 11px; text-transform: uppercase; letter-spacing: .08em; }
.moves {
  margin-top: 5px;
  font-family: ui-monospace, SFMono-Regular, Menlo, Consolas, monospace;
  font-size: 12px;
  line-height: 1.65;
  color: #ded5c7;
  max-height: 74px;
  overflow-y: auto;
  word-break: break-word;
}

.empty {
  text-align: center;
  padding: 30px 18px 26px;
  background: var(--panel);
  border: 1px solid rgba(255,255,255,.06);
  border-radius: var(--radius);
}
.empty-icon { font-size: 48px; color: var(--accent); line-height: 1; }
.empty h2 { margin: 10px 0 6px; font-size: 20px; }
.empty p { color: var(--muted); font-size: 14px; line-height: 1.5; margin: 0 0 18px; }
.empty .btn { flex: 0 0 auto; min-width: 200px; }

.toast {
  position: fixed;
  left: 50%;
  bottom: max(22px, env(safe-area-inset-bottom));
  transform: translateX(-50%);
  background: #322c25;
  color: var(--text);
  border: 1px solid rgba(255,255,255,.12);
  border-radius: 999px;
  padding: 10px 16px;
  font-size: 13px;
  box-shadow: 0 12px 30px rgba(0,0,0,.4);
  z-index: 50;
  max-width: 88vw;
  text-align: center;
}

.loading {
  position: fixed;
  inset: 0;
  background: rgba(15, 13, 10, .55);
  display: flex;
  align-items: center;
  justify-content: center;
  z-index: 40;
  backdrop-filter: blur(2px);
}

.spinner {
  width: 34px;
  height: 34px;
  border-radius: 50%;
  border: 3px solid rgba(255,255,255,.18);
  border-top-color: var(--accent);
  animation: spin .8s linear infinite;
}

@keyframes spin { to { transform: rotate(360deg); } }

.hidden { display: none !important; }

@media (max-width: 380px) {
  .app { gap: 9px; }
  .status-card, .moves-card { padding: 10px 12px; }
  .btn { padding: 9px 11px; font-size: 12px; }
}
