@font-face {
  font-family: "Geist";
  src: url(fonts/Geist-Variable.woff2) format("woff2");
  font-weight: 100 900;
  font-display: swap;
}
@font-face {
  font-family: "Geist Mono";
  src: url(fonts/GeistMono-Variable.woff2) format("woff2");
  font-weight: 100 900;
  font-display: swap;
}

/* Zen: ink, paper, and one vermilion accent, the colour of the seal. */
:root {
  --ink: #0e0f0d;
  --raise: #161713;
  --raise-2: #1d1e1a;
  --line: #262722;
  --text: #cdc7ba;
  --strong: #ece6d9;
  --muted: #7f7b70;
  --faint: #55534b;
  --accent: #d8644a;
  --sans: "Geist", -apple-system, BlinkMacSystemFont, "Segoe UI", sans-serif;
  --mono: "Geist Mono", ui-monospace, "SF Mono", Menlo, monospace;
  --gutter: 16px;

  /* Board themes; stone is the default. */
  --light-sq: #e2ddd1;
  --dark-sq: #a8a293;
  --last-move: rgba(216, 100, 74, 0.34);
  --selected: rgba(14, 15, 13, 0.2);
  --dest: rgba(14, 15, 13, 0.26);
  color-scheme: dark;
}
:root[data-board="slate"] { --light-sq: #dee3ea; --dark-sq: #8796ab; }
:root[data-board="wood"] { --light-sq: #f0d9b5; --dark-sq: #b58863; }
:root[data-board="green"] { --light-sq: #ebecd0; --dark-sq: #739552; --last-move: rgba(255, 220, 60, 0.42); --selected: rgba(255, 220, 60, 0.55); }

* { box-sizing: border-box; }
[hidden] { display: none !important; }

html, body {
  margin: 0;
  background: var(--ink);
  color: var(--text);
  font: 15px/1.5 var(--sans);
  -webkit-font-smoothing: antialiased;
  -webkit-tap-highlight-color: transparent;
  -webkit-text-size-adjust: 100%;
}
body {
  min-height: 100dvh;
  padding: env(safe-area-inset-top) env(safe-area-inset-right) env(safe-area-inset-bottom) env(safe-area-inset-left);
  opacity: 0;
  transition: opacity 0.4s;
}
body.ready { opacity: 1; }

.mono { font-family: var(--mono); }
.muted { color: var(--muted); }
a { color: var(--strong); text-decoration: underline; text-decoration-color: var(--faint); text-underline-offset: 3px; }

button {
  font: inherit;
  color: inherit;
  background: none;
  border: 0;
  cursor: pointer;
  touch-action: manipulation;
}
button:disabled { opacity: 0.3; cursor: default; }
button:focus-visible, a:focus-visible, input:focus-visible { outline: 1px solid var(--accent); outline-offset: 2px; }
svg { width: 20px; height: 20px; flex: none; }

/* ---- header ---- */

.bar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  height: 56px;
  padding: 0 var(--gutter);
  max-width: 1080px;
  margin: 0 auto;
}
.brand, .lockup { display: flex; align-items: center; gap: 10px; color: var(--strong); text-decoration: none; }
.brand img, .lockup img { border-radius: 6px; }
.wordmark { font: 500 19px/1 var(--sans); letter-spacing: 0.01em; }
.icon {
  display: grid;
  place-items: center;
  width: 40px;
  height: 40px;
  border-radius: 50%;
  color: var(--muted);
  transition: color 0.2s, background 0.2s;
}
.icon:not(:disabled):hover { color: var(--strong); background: var(--raise); }

/* ---- layout ---- */

.app {
  display: flex;
  flex-direction: column;
  gap: 4px;
  max-width: 1080px;
  margin: 0 auto;
  padding: 0 0 24px;
}
.play { width: 100%; }
.side { display: flex; flex-direction: column; gap: 4px; padding: 0 var(--gutter); }

@media (min-width: 860px) {
  .app { flex-direction: row; align-items: center; padding: 12px var(--gutter) 40px; gap: 48px; }
  .play { width: min(calc(100dvh - 150px), 680px); flex: none; }
  .side { flex: 1; min-width: 240px; max-width: 320px; padding: 0; gap: 12px; }
}

/* ---- players: one quiet line each ---- */

.player {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  min-height: 44px;
  padding: 6px var(--gutter);
}
@media (min-width: 860px) { .player { padding: 6px 0; } }
.who { display: flex; align-items: baseline; gap: 10px; min-width: 0; white-space: nowrap; }
.name { color: var(--strong); font-weight: 500; }
.meta { font-size: 13px; color: var(--muted); overflow: hidden; text-overflow: ellipsis; }
.meta .score { font-family: var(--mono); font-size: 12px; color: var(--text); }
.breath {
  display: inline-block;
  width: 7px;
  height: 7px;
  border-radius: 50%;
  background: var(--accent);
  margin-right: 6px;
  vertical-align: 1px;
  animation: breathe 2.4s ease-in-out infinite;
}
@keyframes breathe { 0%, 100% { opacity: 0.25; transform: scale(0.8); } 50% { opacity: 1; transform: scale(1); } }
.turn { width: 5px; height: 5px; border-radius: 50%; background: transparent; flex: none; align-self: center; }
.player.to-move .turn { background: var(--accent); }
.captured { display: flex; align-items: center; min-height: 18px; flex-wrap: wrap; justify-content: flex-end; opacity: 0.75; }
.mini { display: inline-block; width: 18px; height: 18px; margin-left: -6px; background-size: cover; }
.mini:first-child { margin-left: 0; }
.lead { margin-left: 6px; font: 12px var(--mono); color: var(--muted); }

/* ---- board ---- */

.board-wrap { position: relative; width: 100%; aspect-ratio: 1; }
.board {
  position: absolute;
  inset: 0;
  background: repeating-conic-gradient(var(--dark-sq) 0 25%, var(--light-sq) 0 50%) 0 0 / 25% 25%;
  touch-action: none;
  user-select: none;
  -webkit-user-select: none;
  -webkit-touch-callout: none;
}
@media (min-width: 860px) { .board { border-radius: 6px; overflow: hidden; } }
.board > div { position: absolute; inset: 0; pointer-events: none; }
.square, .piece { position: absolute; top: 0; left: 0; width: 12.5%; height: 12.5%; }
.last-move { background: var(--last-move); }
.selected { background: var(--selected); }
.check { background: radial-gradient(ellipse at center, rgba(216, 70, 50, 0.95) 0%, rgba(216, 70, 50, 0.6) 35%, rgba(216, 70, 50, 0) 75%); }
.dest { background: radial-gradient(var(--dest) 17%, rgba(0, 0, 0, 0) 18%); }
.dest.capture { background: radial-gradient(transparent 0%, transparent 78%, var(--dest) 79%); }
.dest.hover { background: var(--dest); }
.board .pieces { z-index: 2; }
.board .hints { z-index: 3; }
.piece { background-size: cover; will-change: transform; }
.piece.animating { transition: transform 0.22s cubic-bezier(0.25, 0.8, 0.25, 1); }
.piece.dragging { z-index: 10; transition: none; filter: drop-shadow(0 8px 10px rgba(0, 0, 0, 0.35)); }

.coord { position: absolute; font: 500 clamp(8px, 2vw, 10.5px)/1 var(--mono); opacity: 0.85; }
.coord.file { bottom: 3px; width: 12.5%; text-align: right; padding-right: 4px; }
.coord.rank { left: 4px; height: 12.5%; padding-top: 4px; }
.coord.on-light { color: var(--dark-sq); }
.coord.on-dark { color: var(--light-sq); }

.wK { background-image: url(pieces/wK.svg); }
.wQ { background-image: url(pieces/wQ.svg); }
.wR { background-image: url(pieces/wR.svg); }
.wB { background-image: url(pieces/wB.svg); }
.wN { background-image: url(pieces/wN.svg); }
.wP { background-image: url(pieces/wP.svg); }
.bK { background-image: url(pieces/bK.svg); }
.bQ { background-image: url(pieces/bQ.svg); }
.bR { background-image: url(pieces/bR.svg); }
.bB { background-image: url(pieces/bB.svg); }
.bN { background-image: url(pieces/bN.svg); }
.bP { background-image: url(pieces/bP.svg); }

#promotion { position: absolute; inset: 0; z-index: 20; background: rgba(14, 15, 13, 0.6); }
#promotion button {
  position: absolute;
  width: 12.5%;
  height: 12.5%;
  border-radius: 50%;
  background-color: var(--strong);
  background-size: 80%;
  background-position: center;
  background-repeat: no-repeat;
  transition: border-radius 0.2s, background-color 0.2s;
}
#promotion button:hover { border-radius: 12%; background-color: var(--accent); }

#gameover { position: absolute; inset: 0; z-index: 15; display: grid; place-items: center; pointer-events: none; }
#gameover .card {
  pointer-events: auto;
  background: rgba(14, 15, 13, 0.88);
  backdrop-filter: blur(8px);
  -webkit-backdrop-filter: blur(8px);
  padding: 24px 28px 22px;
  border-radius: 14px;
  text-align: center;
  min-width: 240px;
}
.result-code { font-size: 13px; color: var(--accent); letter-spacing: 0.08em; }
.headline { font-size: 26px; font-weight: 500; color: var(--strong); margin-top: 4px; }
.reason { color: var(--muted); font-size: 14px; }
.actions { display: flex; gap: 8px; margin-top: 20px; justify-content: center; }
.btn { padding: 9px 18px; border-radius: 999px; font-weight: 500; color: var(--strong); box-shadow: inset 0 0 0 1px var(--line); }
.btn:hover { background: var(--raise); }
.btn.primary { background: var(--strong); color: var(--ink); box-shadow: none; }
.btn.primary:hover { background: #fff; }

/* ---- moves and actions ---- */

.moves {
  display: flex;
  overflow-x: auto;
  gap: 2px;
  min-height: 40px;
  align-items: center;
  scrollbar-width: none;
  font: 14px var(--mono);
}
.moves::-webkit-scrollbar { display: none; }
.moves:empty::before { content: "your move"; color: var(--faint); font: 13px var(--sans); padding: 0 2px; }
.row { display: flex; align-items: center; flex: none; }
.num { color: var(--faint); padding: 0 2px 0 8px; font-size: 12px; }
.row:first-child .num { padding-left: 0; }
.mv { padding: 4px 6px; border-radius: 6px; color: var(--text); white-space: nowrap; }
.mv:hover:not(:empty) { color: var(--strong); background: var(--raise); }
.mv:empty { display: none; }
.mv.current { color: var(--accent); }

@media (min-width: 860px) {
  .moves { flex-direction: column; align-items: stretch; overflow-x: hidden; overflow-y: auto; max-height: 52vh; min-height: 0; gap: 0; }
  .moves:empty { min-height: 40px; justify-content: center; }
  .row { display: grid; grid-template-columns: 36px 1fr 1fr; }
  .num { padding: 5px 8px 5px 0; text-align: right; }
  .row:first-child .num { padding-left: 0; }
  .mv { text-align: left; padding: 5px 8px; }
  .mv:empty { display: block; }
}

.history { display: flex; justify-content: space-between; }
.history .icon { flex: 1; border-radius: 10px; height: 40px; }

.controls { display: flex; gap: 8px; }
.controls button {
  flex: 1;
  padding: 11px 0;
  border-radius: 999px;
  font-size: 14px;
  font-weight: 500;
  color: var(--text);
  box-shadow: inset 0 0 0 1px var(--line);
  transition: background 0.2s, color 0.2s;
}
.controls button:not(:disabled):hover { background: var(--raise); color: var(--strong); }
.controls #new { color: var(--strong); }

/* ---- dialogs ---- */

dialog {
  border: 0;
  border-radius: 18px;
  background: var(--raise);
  color: var(--text);
  padding: 0;
  width: min(440px, calc(100vw - 32px));
  box-shadow: 0 30px 80px rgba(0, 0, 0, 0.6);
}
dialog::backdrop { background: rgba(8, 8, 7, 0.72); backdrop-filter: blur(6px); -webkit-backdrop-filter: blur(6px); }
dialog form { padding: 20px 22px 22px; display: flex; flex-direction: column; gap: 10px; }
.dialog-head { display: flex; align-items: center; justify-content: space-between; margin-bottom: 2px; }
dialog h2 { margin: 0; color: var(--strong); font-size: 19px; font-weight: 500; }
.close { margin-right: -10px; }
.label { font-size: 12px; color: var(--muted); margin-top: 8px; }

.levels { display: grid; grid-template-columns: repeat(4, 1fr); gap: 6px; }
.lvl {
  display: flex;
  flex-direction: column;
  align-items: flex-start;
  padding: 9px 10px;
  border-radius: 12px;
  box-shadow: inset 0 0 0 1px var(--line);
  text-align: left;
  transition: background 0.2s;
}
.lvl b { font: 500 17px/1.15 var(--mono); color: var(--strong); }
.lvl span { font-size: 11px; color: var(--muted); white-space: nowrap; overflow: hidden; text-overflow: ellipsis; max-width: 100%; }
.lvl:hover { background: var(--raise-2); }
.lvl.on { box-shadow: inset 0 0 0 1px var(--accent); }
.lvl.on b { color: var(--accent); }

.colors { display: grid; grid-template-columns: repeat(3, 1fr); gap: 8px; }
.color {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 4px;
  padding: 12px 0 10px;
  border-radius: 14px;
  box-shadow: inset 0 0 0 1px var(--line);
  font-size: 13px;
  transition: background 0.2s;
}
.color:hover { background: var(--raise-2); color: var(--strong); }
.color .piece, .halves { position: relative; width: 46px; height: 46px; }
.halves .piece { position: absolute; inset: 0; width: 100%; height: 100%; }
.halves .half:first-child { clip-path: inset(0 50% 0 0); }
.halves .half:last-child { clip-path: inset(0 0 0 50%); }

/* The menu: a full-screen overlay, the board still faintly behind it. */
dialog.overlay {
  width: 100vw;
  height: 100dvh;
  max-width: none;
  max-height: none;
  margin: 0;
  border-radius: 0;
  background: rgba(14, 15, 13, 0.94);
  backdrop-filter: blur(14px);
  -webkit-backdrop-filter: blur(14px);
  box-shadow: none;
  overflow-y: auto;
}
dialog.overlay::backdrop { background: transparent; backdrop-filter: none; -webkit-backdrop-filter: none; }
dialog.overlay[open] { animation: fade 0.25s ease-out; }
@keyframes fade { from { opacity: 0; } }
dialog.overlay form {
  min-height: 100%;
  max-width: 560px;
  margin: 0 auto;
  padding: calc(env(safe-area-inset-top) + 8px) max(var(--gutter), env(safe-area-inset-right)) calc(env(safe-area-inset-bottom) + 32px) max(var(--gutter), env(safe-area-inset-left));
  gap: 0;
}
.overlay-head { display: flex; align-items: center; justify-content: space-between; height: 56px; }
.overlay-body { display: flex; flex-direction: column; gap: 28px; padding-top: 20px; }
.overlay-body section { display: flex; flex-direction: column; gap: 10px; }
.overlay-body .label { margin: 0; }

.tiles { display: grid; grid-template-columns: repeat(3, 1fr); gap: 8px; }
.tile {
  display: flex;
  flex-direction: column;
  align-items: flex-start;
  justify-content: space-between;
  gap: 18px;
  padding: 14px;
  min-height: 92px;
  border-radius: 16px;
  background: rgba(236, 230, 217, 0.05);
  color: var(--strong);
  font-size: 14px;
  text-align: left;
  transition: background 0.2s;
}
.tile svg { width: 22px; height: 22px; color: var(--muted); }
.tile:not(:disabled):hover { background: rgba(236, 230, 217, 0.09); }
.tile.danger:not(:disabled):hover svg { color: var(--accent); }

.themes { display: grid; grid-template-columns: repeat(4, 1fr); gap: 8px; }
.theme {
  display: flex;
  flex-direction: column;
  gap: 8px;
  padding: 10px;
  border-radius: 14px;
  background: rgba(236, 230, 217, 0.05);
  cursor: pointer;
  font-size: 13px;
  box-shadow: inset 0 0 0 1px transparent;
  transition: box-shadow 0.2s;
}
.theme input { position: absolute; opacity: 0; pointer-events: none; }
.theme:has(input:checked) { box-shadow: inset 0 0 0 1px var(--accent); }
.theme:has(input:focus-visible) { outline: 1px solid var(--accent); outline-offset: 2px; }
.swatch { width: 100%; aspect-ratio: 1; border-radius: 8px; }
.swatch.stone { background: repeating-conic-gradient(#a8a293 0 25%, #e2ddd1 0 50%) 0 0 / 50% 50%; }
.swatch.slate { background: repeating-conic-gradient(#8796ab 0 25%, #dee3ea 0 50%) 0 0 / 50% 50%; }
.swatch.wood { background: repeating-conic-gradient(#b58863 0 25%, #f0d9b5 0 50%) 0 0 / 50% 50%; }
.swatch.green { background: repeating-conic-gradient(#739552 0 25%, #ebecd0 0 50%) 0 0 / 50% 50%; }

.toggle {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 10px;
  padding: 14px 0;
  border-bottom: 1px solid var(--line);
  cursor: pointer;
  color: var(--strong);
}
.toggle:first-child { border-top: 1px solid var(--line); }
.toggle input {
  appearance: none;
  -webkit-appearance: none;
  width: 42px;
  height: 24px;
  border-radius: 999px;
  background: var(--raise-2);
  position: relative;
  transition: background 0.2s;
  cursor: pointer;
  flex: none;
}
.toggle input::after {
  content: "";
  position: absolute;
  top: 3px;
  left: 3px;
  width: 18px;
  height: 18px;
  border-radius: 50%;
  background: var(--muted);
  transition: transform 0.2s, background 0.2s;
}
.toggle input:checked { background: var(--accent); }
.toggle input:checked::after { transform: translateX(18px); background: var(--strong); }

.engine { display: grid; grid-template-columns: auto 1fr; gap: 6px 16px; margin: 0; font-size: 13px; }
.engine dt { color: var(--muted); }
.engine dd { margin: 0; color: var(--text); }
.about { font-size: 14px; line-height: 1.6; }
.about p { margin: 0; }

#toast {
  position: fixed;
  left: 50%;
  bottom: calc(24px + env(safe-area-inset-bottom));
  transform: translateX(-50%);
  background: var(--strong);
  color: var(--ink);
  font-size: 13px;
  padding: 9px 16px;
  border-radius: 999px;
  z-index: 100;
}

@media (prefers-reduced-motion: reduce) {
  .breath, dialog.overlay[open] { animation: none; }
  .piece.animating { transition: none; }
}
