:root {
  --bg0: #1a1510;
  --bg1: #2a2118;
  --ink: #f4efe6;
  --muted: #b5a894;
  --line: rgba(255,255,255,0.12);
  --light: #f0d9b5;
  --dark: #b58863;
  --hint: rgba(20, 85, 30, 0.55);
  --auto: #2ecc71;
  --font: "Outfit", system-ui, sans-serif;
  --display: "Nunito", "Outfit", sans-serif;
}
* { box-sizing: border-box; }
html, body {
  margin: 0;
  min-height: 100%;
  font-family: var(--font);
  color: var(--ink);
  background:
    radial-gradient(700px 420px at 10% -10%, #3a2a18 0%, transparent 55%),
    radial-gradient(600px 380px at 100% 0%, #1e3a2a 0%, transparent 50%),
    linear-gradient(165deg, var(--bg0), var(--bg1));
}
.hidden { display: none !important; }
.app {
  min-height: 100dvh;
  display: flex;
  flex-direction: column;
  padding: 10px 10px 14px;
  max-width: 920px;
  margin: 0 auto;
}
.topbar {
  display: flex;
  align-items: center;
  gap: 8px;
  margin-bottom: 10px;
}
.topbar-spacer { flex: 1; min-width: 4px; }
.title-block {
  display: grid;
  min-width: 0;
  flex: 0 1 auto;
}
.title-block strong {
  font-family: var(--display);
  font-size: 1.1rem;
}
.status { color: var(--muted); font-size: 0.82rem; font-weight: 600; }

.side-player {
  display: flex;
  align-items: center;
  gap: 8px;
  min-width: 0;
}
.side-player .clock {
  font-variant-numeric: tabular-nums;
  font-weight: 800;
  font-size: 0.95rem;
  min-width: 3.2em;
  text-align: right;
  color: var(--muted);
  padding: 4px 6px;
  border-radius: 8px;
  background: rgba(0,0,0,0.25);
}
.side-player .clock.active {
  color: #f6c453;
  background: rgba(246, 196, 83, 0.12);
}
.side-player .clock.low {
  color: #ff6b6b;
}

.player-chip {
  display: flex;
  align-items: center;
  gap: 8px;
  min-width: 0;
  max-width: 200px;
  padding: 4px 10px 4px 4px;
  border: 1px solid var(--line);
  border-radius: 999px;
  background: rgba(0,0,0,0.28);
}
.player-chip .ava {
  width: 32px;
  height: 32px;
  border-radius: 50%;
  object-fit: cover;
  background: #3a3228;
  flex-shrink: 0;
}
.player-chip .pinfo {
  display: grid;
  min-width: 0;
  line-height: 1.15;
}
.player-chip .pname {
  font-weight: 700;
  font-size: 0.88rem;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}
.player-chip .pchips {
  font-size: 0.72rem;
  font-weight: 700;
  color: #f6c453;
  white-space: nowrap;
}

.layout {
  flex: 1;
  display: grid;
  grid-template-columns: minmax(240px, 1fr) minmax(200px, 280px);
  gap: 2px;
  align-items: start;
}

.board-col {
  display: grid;
  gap: 8px;
  align-content: start;
  min-width: 0;
}
.board-top-row {
  display: flex;
  align-items: center;
  justify-content: flex-end;
  gap: 10px;
  width: 100%;
  max-width: 560px;
  min-height: 40px;
}
.board-wrap {
  width: 100%;
  max-width: 560px;
  aspect-ratio: 1;
  position: relative;
}
.board {
  width: 100%;
  height: 100%;
  display: grid;
  grid-template-columns: repeat(8, 1fr);
  grid-template-rows: repeat(8, 1fr);
  border-radius: 4px;
  overflow: hidden;
  box-shadow: 0 16px 40px rgba(0,0,0,0.35);
  position: relative;
  user-select: none;
  touch-action: manipulation;
}
.board.blurred {
  filter: blur(5px);
  pointer-events: none;
}
.pause-overlay {
  position: absolute;
  inset: 0;
  display: grid;
  place-items: center;
  font-family: var(--display);
  font-weight: 900;
  font-size: 1.6rem;
  letter-spacing: 0.04em;
  color: #fff;
  background: rgba(20, 16, 12, 0.35);
  z-index: 5;
  border-radius: 4px;
}
.hint-label {
  position: absolute;
  left: 8px;
  bottom: 8px;
  z-index: 4;
  font-size: 0.78rem;
  font-weight: 800;
  padding: 4px 8px;
  border-radius: 8px;
  background: rgba(46, 204, 113, 0.92);
  color: #0b1a10;
  pointer-events: none;
}
.sq {
  position: relative;
  display: grid;
  place-items: center;
}
.sq.light { background: var(--light); }
.sq.dark { background: var(--dark); }
.sq.last { box-shadow: inset 0 0 0 999px rgba(186, 202, 68, 0.55); }
.sq.selected { box-shadow: inset 0 0 0 999px rgba(20, 85, 30, 0.35); }
.sq.hint-from { box-shadow: inset 0 0 0 999px rgba(46, 204, 113, 0.45); }
.sq.hint-to { box-shadow: inset 0 0 0 999px rgba(46, 204, 113, 0.7); }
.sq.legal::after {
  content: "";
  width: 28%;
  height: 28%;
  border-radius: 50%;
  background: var(--hint);
  position: absolute;
}
.sq.legal.capture::after {
  width: 86%;
  height: 86%;
  background: transparent;
  border: 5px solid var(--hint);
  border-radius: 50%;
}
.coord {
  position: absolute;
  font-size: 0.65rem;
  font-weight: 700;
  opacity: 0.75;
  pointer-events: none;
  color: rgba(0,0,0,0.55);
}
.sq.dark .coord { color: rgba(240, 217, 181, 0.8); }
.coord.file { right: 3px; bottom: 2px; }
.coord.rank { left: 3px; top: 2px; }

.piece {
  width: 92%;
  height: 92%;
  cursor: pointer;
  filter: drop-shadow(0 1px 1px rgba(0,0,0,0.25));
  z-index: 1;
}
.piece svg { width: 100%; height: 100%; display: block; }

.bottom-row {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 10px;
  width: 100%;
  max-width: 560px;
}
.actions {
  display: flex;
  flex-wrap: wrap;
  gap: 8px;
  min-width: 0;
}

.log-col {
  border: 1px solid var(--line);
  border-radius: 10px;
  background: rgba(0,0,0,0.28);
  padding: 8px 8px 10px;
  display: flex;
  flex-direction: column;
  min-height: 0;
  align-self: start;
}
.move-log {
  list-style: none;
  margin: 0;
  padding: 0;
  overflow: auto;
  flex: 1;
  font-variant-numeric: tabular-nums;
  line-height: 1.5;
  font-size: 0.92rem;
  min-height: 0;
}
.move-log li { padding: 1px 0; }
.result {
  margin: 8px 0 0;
  font-weight: 800;
  color: #f6c453;
  font-size: 0.88rem;
}

.btn {
  border: 1px solid var(--line);
  border-radius: 12px;
  background: rgba(255,255,255,0.06);
  color: var(--ink);
  font: inherit;
  font-weight: 700;
  padding: 9px 11px;
  cursor: pointer;
  white-space: nowrap;
}
.btn.primary {
  background: linear-gradient(135deg, #3d8b5a, #1f5a38);
  border-color: transparent;
}
.btn.ghost { background: transparent; color: var(--muted); }
.btn.icon-btn {
  min-width: 42px;
  padding: 8px 10px;
  font-size: 1rem;
  line-height: 1;
}
.btn.auto-on {
  background: var(--auto);
  border-color: transparent;
  color: #0b1a10;
}
.btn:disabled { opacity: 0.5; cursor: not-allowed; }

.modal {
  position: fixed;
  inset: 0;
  background: rgba(0,0,0,0.55);
  display: grid;
  place-items: center;
  z-index: 60;
  padding: 16px;
}
.modal-card {
  width: min(360px, 100%);
  background: #2a2118;
  border: 1px solid var(--line);
  border-radius: 16px;
  padding: 18px 16px;
}
.modal-card h3 { margin: 0 0 8px; font-family: var(--display); }
.modal-card p { margin: 0 0 14px; color: var(--muted); }
.modal-actions { display: flex; gap: 8px; justify-content: flex-end; }
.field {
  display: grid;
  gap: 6px;
  margin: 12px 0 8px;
  font-weight: 700;
  font-size: 0.9rem;
}
.field input {
  font: inherit;
  font-weight: 700;
  padding: 10px 12px;
  border-radius: 10px;
  border: 1px solid var(--line);
  background: rgba(0,0,0,0.35);
  color: var(--ink);
}
.field-hint {
  margin: 0 0 12px !important;
  font-size: 0.8rem !important;
  font-weight: 600;
}

.toast {
  position: fixed;
  left: 50%;
  bottom: 24px;
  transform: translateX(-50%);
  background: rgba(20,16,12,0.92);
  border: 1px solid var(--line);
  padding: 10px 16px;
  border-radius: 12px;
  z-index: 50;
  font-weight: 700;
}

@media (max-width: 760px) {
  .layout {
    grid-template-columns: 1fr;
    gap: 10px;
  }
  .board-wrap { max-width: none; }
  .board-top-row { max-width: none; }
  .bottom-row { max-width: none; }
  .log-col {
    height: auto !important;
    max-height: 34dvh;
  }
  .topbar { flex-wrap: wrap; }
  .player-chip { max-width: 42vw; }
}
