:root {
  --bg: #161512;
  --panel: #262421;
  --panel-2: #1f1d1b;
  --btn: #3a3733;
  --btn-hover: #4a4641;
  --text: #d8d4cf;
  --muted: #8b8782;
  --accent: #7fa650;
  --danger: #d9694f;
  --bar: 52px;
  --board: min(calc(100vh - 2 * var(--bar) - 40px), calc(100vw - 32px), 820px);
}

@media (min-width: 960px) {
  :root { --board: min(calc(100vh - 2 * var(--bar) - 40px), calc(100vw - 440px), 820px); }
}

* { box-sizing: border-box; }

body {
  margin: 0;
  min-height: 100vh;
  background: var(--bg);
  color: var(--text);
  font: 15px/1.45 system-ui, -apple-system, 'Segoe UI', sans-serif;
}

main {
  display: flex;
  flex-wrap: wrap;
  gap: 24px;
  justify-content: center;
  align-items: flex-start; /* don't stretch the board to the sidebar's height */
  padding: 20px 16px;
}

.play { width: var(--board); flex: none; }

.board-wrap {
  position: relative;
  width: var(--board);
  height: var(--board);
}
#board { width: 100%; height: 100%; }
cg-board { border-radius: 4px; }

/* Player bars */
.player {
  height: var(--bar);
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 0 2px;
}
.avatar {
  width: 36px;
  height: 36px;
  border-radius: 5px;
  background: var(--btn);
  display: grid;
  place-items: center;
  font-weight: 700;
  color: var(--text);
  flex: none;
}
.player.is-jev .avatar { background: #4b3f6b; }
.who { display: flex; flex-direction: column; line-height: 1.2; min-width: 0; }
.who .name { font-weight: 600; }
.who .sub { font-size: 12px; color: var(--muted); white-space: nowrap; }
.captures.cg-wrap { display: flex; align-items: center; flex-wrap: wrap; min-width: 0; height: 24px; }
.captures piece {
  position: static;
  display: block;
  width: 22px;
  height: 22px;
  background-size: cover;
  margin-left: -10px;
  flex: none;
}
.captures piece.gap, .captures piece:first-child { margin-left: 0; }
.adv { font-size: 13px; color: var(--muted); font-variant-numeric: tabular-nums; }
.turn-tag {
  margin-left: auto;
  font-size: 12px;
  padding: 3px 9px;
  border-radius: 99px;
  color: var(--muted);
  white-space: nowrap;
}
.player.to-move .turn-tag { background: var(--accent); color: #fff; }
.player.thinking .turn-tag::after { content: ''; animation: dots 1.2s steps(4, end) infinite; }
@keyframes dots { 0% { content: ''; } 25% { content: '.'; } 50% { content: '..'; } 75% { content: '...'; } }

/* Sidebar */
aside {
  width: min(380px, calc(100vw - 32px));
  background: var(--panel);
  border-radius: 8px;
  padding: 16px;
  display: flex;
  flex-direction: column;
  gap: 14px;
}
@media (min-width: 960px) {
  aside { height: calc(var(--board) + 2 * var(--bar)); overflow-y: auto; }
  .moves-panel { flex: 1; min-height: 150px; }
}

h1 { margin: 0 0 2px; font-size: 20px; }
h2 {
  margin: 0 0 8px;
  font-size: 12px;
  letter-spacing: .08em;
  text-transform: uppercase;
  color: var(--muted);
}
h2 span { text-transform: none; letter-spacing: 0; }

.muted { color: var(--muted); }
.status { margin: 0; min-height: 1.45em; }
.status.error { color: var(--danger); }
.status.over { color: var(--accent); font-weight: 600; }

button {
  font: inherit;
  padding: 7px 12px;
  border: 0;
  border-radius: 5px;
  background: var(--btn);
  color: var(--text);
  cursor: pointer;
}
button:hover:not(:disabled) { background: var(--btn-hover); }
button:disabled { opacity: .45; cursor: default; }
button.active { background: var(--accent); color: #fff; }

.newgame { display: flex; align-items: center; gap: 8px; flex-wrap: wrap; }
.newgame span { color: var(--muted); font-size: 13px; margin-right: 2px; }

.panel { background: var(--panel-2); border-radius: 6px; padding: 12px; display: flex; flex-direction: column; min-height: 0; }

.moves {
  margin: 0;
  padding: 0;
  list-style: none;
  flex: 1;
  min-height: 0;
  max-height: 260px;
  overflow-y: auto;
  display: grid;
  grid-template-columns: 36px 1fr 1fr;
  align-content: start;
  row-gap: 1px;
  font-variant-numeric: tabular-nums;
}
@media (min-width: 960px) { .moves { max-height: none; } }
.moves li { padding: 3px 6px; border-radius: 4px; }
.moves .n { color: var(--muted); }
.moves .mv { cursor: pointer; font-weight: 600; }
.moves .mv:hover { background: var(--btn); }
.moves .mv.current { background: var(--btn-hover); color: #fff; }

.nav { display: grid; grid-template-columns: repeat(4, 1fr); gap: 6px; margin-top: 10px; }
.nav button { padding: 8px 0; }

.jev .row {
  display: grid;
  grid-template-columns: 56px 1fr 48px;
  align-items: center;
  gap: 8px;
  margin: 3px 0;
  font-variant-numeric: tabular-nums;
}
.jev .bar { height: 8px; border-radius: 4px; background: var(--btn); overflow: hidden; }
.jev .bar i { display: block; height: 100%; background: var(--accent); }
.jev .row.picked b { color: var(--accent); }
.jev .meta { margin-top: 8px; font-size: 13px; color: var(--muted); }

.actions { display: flex; gap: 8px; flex-wrap: wrap; }
.actions button { flex: 1 1 auto; font-size: 14px; }
.actions [hidden] { display: none; }
.actions select {
  flex: 1 1 auto;
  font: inherit;
  font-size: 14px;
  padding: 7px 8px;
  border: 0;
  border-radius: 5px;
  background: var(--btn);
  color: var(--text);
  cursor: pointer;
}

.load summary { cursor: pointer; color: var(--muted); font-size: 13px; }
.load form { display: flex; gap: 8px; margin: 8px 0; }
.load input {
  flex: 1;
  min-width: 0;
  font: 12px ui-monospace, monospace;
  padding: 7px 8px;
  border-radius: 5px;
  border: 1px solid var(--btn);
  background: var(--bg);
  color: var(--text);
}
.fen { display: block; font-size: 12px; word-break: break-all; color: var(--muted); }

/* Promotion picker */
.promo {
  position: absolute;
  inset: 0;
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 8px;
  background: rgba(0, 0, 0, .55);
  z-index: 10;
}
.promo[hidden] { display: none; }
.promo button { flex: none; width: min(96px, 20%); aspect-ratio: 1; padding: 0; background: #f0d9b5; }
.promo button:hover { background: #fff; }
/* chessground's base css makes pieces absolute 12.5% tiles; undo that inside the picker */
.promo .cg-wrap { width: 100%; height: 100%; }
.promo piece { position: static; display: block; width: 100%; height: 100%; background-size: cover; }

/* Keep coordinates inside the board (chessground's defaults hang outside it and overflow the page) */
.cg-wrap coords { font-size: 11px; font-weight: 700; opacity: .9; }
.cg-wrap coords.ranks { top: 0; left: 3px; }
.cg-wrap coords.ranks coord { transform: none; padding-top: 2px; }
.cg-wrap coords.files { left: 0; bottom: 1px; text-align: right; }
.cg-wrap coords.files coord { padding-right: 4px; }

/* "How it works" dialog */
.title-row { display: flex; align-items: baseline; justify-content: space-between; gap: 12px; }
button.link { background: none; padding: 0; color: var(--accent); font-size: 13px; text-decoration: underline; text-underline-offset: 3px; }
button.link:hover:not(:disabled) { background: none; color: #a3c97a; }

dialog#about {
  width: min(620px, calc(100vw - 32px));
  max-height: min(84vh, 760px);
  padding: 0;
  border: 1px solid var(--btn);
  border-radius: 10px;
  background: var(--panel);
  color: var(--text);
  overflow: hidden;
}
dialog#about[open] { display: flex; flex-direction: column; }
dialog#about::backdrop { background: rgba(0, 0, 0, .6); }
.about-head {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  padding: 16px 20px;
  border-bottom: 1px solid var(--btn);
}
.about-head h2 { margin: 0; font-size: 17px; letter-spacing: 0; text-transform: none; color: var(--text); }
.about-head .close { padding: 4px 9px; }
.about-body { padding: 4px 20px 20px; overflow-y: auto; line-height: 1.55; }
.about-body h3 { margin: 20px 0 6px; font-size: 12px; letter-spacing: .08em; text-transform: uppercase; color: var(--muted); }
.about-body p { margin: 8px 0; }
.about-body .lead { font-size: 16px; margin-top: 16px; }
.about-body ol, .about-body ul { margin: 8px 0; padding-left: 20px; }
.about-body li { margin: 6px 0; }
.about-body a { color: var(--accent); }
.about-body .credits { margin-top: 22px; padding-top: 14px; border-top: 1px solid var(--btn); font-size: 12px; color: var(--muted); }
