/* ---------------------------------------------------------------- reset --- */

/* `hidden` is only a user-agent rule, so any author `display` on the same
   element beats it. Without !important a layout class silently un-hides. */
[hidden] { display: none !important; }

*, *::before, *::after { box-sizing: border-box; }

html { -webkit-text-size-adjust: 100%; }

body {
  margin: 0;
  font-family: "Inter", -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Helvetica, Arial, sans-serif;
  font-size: 15px;
  line-height: 1.5;
  color: var(--ink);
  background: var(--bg);
  -webkit-font-smoothing: antialiased;
}

h1, h2, p, ol { margin: 0; }
ol { padding: 0; list-style: none; }

/* --------------------------------------------------------------- tokens --- */

:root {
  --bg: #f4f1ec;
  --surface: #ffffff;
  --surface-sunken: #efebe4;
  --border: #ddd6ca;
  --ink: #23201c;
  --ink-soft: #6d665c;
  --accent: #4f7d4a;
  --accent-ink: #ffffff;

  --sq-light: #eadfc8;
  --sq-dark: #ad8a67;
  --sq-light-active: #d6d17a;
  --sq-dark-active: #b6ab55;
  --piece-white: #fbf8f2;
  --piece-white-edge: #2f2a24;
  --piece-black: #262220;
  --piece-black-edge: #d9d2c6;

  --shadow: 0 1px 2px rgba(35, 32, 28, .08), 0 8px 24px rgba(35, 32, 28, .08);
  --radius: 10px;
}

@media (prefers-color-scheme: dark) {
  :root {
    --bg: #17161a;
    --surface: #201f24;
    --surface-sunken: #191a1e;
    --border: #33313a;
    --ink: #eceaf0;
    --ink-soft: #9d99a6;
    --accent: #7fae76;
    --accent-ink: #14210f;

    --sq-light: #b3a898;
    --sq-dark: #6f6153;
    --sq-light-active: #a8a05e;
    --sq-dark-active: #7d7343;
    --shadow: 0 1px 2px rgba(0, 0, 0, .4), 0 12px 32px rgba(0, 0, 0, .35);
  }
}

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

.skip-link {
  position: absolute;
  left: -9999px;
  top: 0;
  padding: .6rem 1rem;
  background: var(--accent);
  color: var(--accent-ink);
  border-radius: 0 0 var(--radius) 0;
  z-index: 20;
}
.skip-link:focus { left: 0; }

.app {
  max-width: 1120px;
  margin: 0 auto;
  padding: clamp(1rem, 3vw, 2.25rem) clamp(.75rem, 3vw, 2rem) 2rem;
}

.masthead { margin-bottom: 1.25rem; }
.masthead__title {
  font-size: clamp(1.5rem, 4vw, 2rem);
  font-weight: 650;
  letter-spacing: -.02em;
  display: flex;
  align-items: center;
  gap: .5rem;
}
.masthead__sub { color: var(--ink-soft); font-size: .95rem; }

.layout {
  display: grid;
  grid-template-columns: minmax(0, 1fr) minmax(260px, 320px);
  gap: clamp(1rem, 3vw, 2rem);
  align-items: start;
}

@media (max-width: 860px) {
  .layout { grid-template-columns: minmax(0, 1fr); }
}

.board-column { display: grid; gap: .5rem; min-width: 0; }

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

.board-frame {
  position: relative;
  width: 100%;
  aspect-ratio: 1;
  border-radius: var(--radius);
  overflow: hidden;
  box-shadow: var(--shadow);
  /* Pointer drags must not scroll the page out from under the piece. */
  touch-action: none;
}

.board {
  display: grid;
  grid-template-columns: repeat(8, 1fr);
  grid-template-rows: repeat(8, 1fr);
  width: 100%;
  height: 100%;
  user-select: none;
}

/* A role=grid only exposes cells that sit inside a role=row. `display: contents`
   keeps the rows in the accessibility tree while letting the cells be the grid
   container's own items. */
.board__row { display: contents; }

.square {
  position: relative;
  display: flex;
  align-items: center;
  justify-content: center;
  cursor: default;
  outline: none;
}
.square--light { background: var(--sq-light); }
.square--dark { background: var(--sq-dark); }
.square--last { background: var(--sq-light-active); }
.square--dark.square--last { background: var(--sq-dark-active); }

.square--movable { cursor: grab; }
.square--dragging { cursor: grabbing; }

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

.square:focus-visible::after {
  content: "";
  position: absolute;
  inset: 0;
  box-shadow: inset 0 0 0 3px #2f6fd0;
  pointer-events: none;
}

.square--check {
  background-image: radial-gradient(circle at 50% 50%, rgba(214, 63, 44, .95) 12%, rgba(214, 63, 44, 0) 72%);
}

/* Move hints: a dot on an empty square, a ring around a capture. */
.hint {
  position: absolute;
  inset: 0;
  pointer-events: none;
  display: flex;
  align-items: center;
  justify-content: center;
}
.hint::before {
  content: "";
  width: 30%;
  height: 30%;
  border-radius: 50%;
  background: rgba(20, 18, 16, .28);
}
.hint--capture::before {
  width: 92%;
  height: 92%;
  background: none;
  border: 7% solid rgba(20, 18, 16, .28);
  box-sizing: border-box;
}

.piece {
  font-size: min(9.4vw, 4.4rem);
  line-height: 1;
  pointer-events: none;
  transform: translateY(-2%);
}
@media (min-width: 861px) {
  .piece { font-size: min(5.2vw, 3.5rem); }
}

.piece--white {
  color: var(--piece-white);
  text-shadow:
    0 0 1px var(--piece-white-edge),
    1px 0 0 var(--piece-white-edge), -1px 0 0 var(--piece-white-edge),
    0 1px 0 var(--piece-white-edge), 0 -1px 0 var(--piece-white-edge),
    1px 1px 0 var(--piece-white-edge), -1px -1px 0 var(--piece-white-edge),
    1px -1px 0 var(--piece-white-edge), -1px 1px 0 var(--piece-white-edge);
}
.piece--black {
  color: var(--piece-black);
  text-shadow:
    0 0 1px var(--piece-black-edge),
    1px 0 0 rgba(217, 210, 198, .55), -1px 0 0 rgba(217, 210, 198, .55),
    0 1px 0 rgba(217, 210, 198, .55), 0 -1px 0 rgba(217, 210, 198, .55);
}

.coord {
  position: absolute;
  font-size: clamp(8px, 1.5vw, 11px);
  font-weight: 600;
  opacity: .65;
  pointer-events: none;
}
.coord--file { right: 3px; bottom: 1px; }
.coord--rank { left: 3px; top: 1px; }
.square--light .coord { color: var(--sq-dark); }
.square--dark .coord { color: var(--sq-light); }

.drag-ghost {
  position: fixed;
  z-index: 30;
  pointer-events: none;
  transform: translate(-50%, -50%);
  line-height: 1;
  filter: drop-shadow(0 6px 10px rgba(0, 0, 0, .35));
}

.board-veil {
  position: absolute;
  inset: 0;
  display: flex;
  align-items: center;
  justify-content: center;
  background: rgba(12, 11, 14, .2);
}
.board-veil__text {
  background: var(--surface);
  color: var(--ink);
  padding: .7rem 1.1rem;
  border-radius: 999px;
  font-weight: 600;
  box-shadow: var(--shadow);
}

/* --------------------------------------------------------------- roster --- */

.roster {
  display: flex;
  align-items: center;
  gap: .6rem;
  min-height: 1.9rem;
  font-size: .9rem;
}
.roster__name { font-weight: 600; }
.roster__captured {
  font-size: 1.15rem;
  line-height: 1;
  letter-spacing: -.12em;
  color: var(--ink-soft);
  overflow: hidden;
}
.roster__score { margin-left: auto; color: var(--ink-soft); font-variant-numeric: tabular-nums; }

/* --------------------------------------------------------------- panel ---- */

.panel {
  display: grid;
  gap: 1rem;
  padding: 1rem;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  box-shadow: var(--shadow);
}

.status {
  font-weight: 600;
  font-size: 1.02rem;
  padding: .6rem .75rem;
  border-radius: 8px;
  background: var(--surface-sunken);
}
.status--over { background: var(--accent); color: var(--accent-ink); }

.controls { display: flex; flex-wrap: wrap; gap: .5rem; }

.btn {
  font: inherit;
  font-weight: 550;
  padding: .5rem .85rem;
  color: var(--ink);
  background: var(--surface-sunken);
  border: 1px solid var(--border);
  border-radius: 8px;
  cursor: pointer;
}
.btn:hover:not(:disabled) { border-color: var(--ink-soft); }
.btn:disabled { opacity: .45; cursor: not-allowed; }
.btn--primary { background: var(--accent); color: var(--accent-ink); border-color: transparent; }
.btn--small { padding: .25rem .55rem; font-size: .82rem; }

.icon-btn {
  font: inherit;
  font-size: .8rem;
  padding: .25rem .5rem;
  color: var(--ink);
  background: var(--surface-sunken);
  border: 1px solid var(--border);
  border-radius: 6px;
  cursor: pointer;
}
.icon-btn:disabled { opacity: .35; cursor: not-allowed; }

.settings { display: grid; grid-template-columns: repeat(auto-fit, minmax(120px, 1fr)); gap: .6rem; }
.field { display: grid; gap: .25rem; min-width: 0; }
.field__label { font-size: .78rem; text-transform: uppercase; letter-spacing: .06em; color: var(--ink-soft); }
.field select {
  font: inherit;
  padding: .4rem .5rem;
  color: var(--ink);
  background: var(--surface-sunken);
  border: 1px solid var(--border);
  border-radius: 8px;
  width: 100%;
}
.settings__note { font-size: .78rem; color: var(--ink-soft); margin-top: -.6rem; }

/* --------------------------------------------------------------- moves ---- */

.moves { display: grid; gap: .5rem; }
.moves__head { display: flex; align-items: center; justify-content: space-between; gap: .5rem; }
.moves__title, .export__title { font-size: .78rem; text-transform: uppercase; letter-spacing: .06em; color: var(--ink-soft); font-weight: 600; }
.moves__nav { display: flex; gap: .25rem; }
.moves__empty { font-size: .85rem; color: var(--ink-soft); }

.movelist {
  display: grid;
  gap: .1rem;
  align-content: start;
  max-height: 13rem;
  overflow-y: auto;
  font-variant-numeric: tabular-nums;
  padding-right: .25rem;
}
.movelist__row {
  display: grid;
  grid-template-columns: 2.2rem 1fr 1fr;
  gap: .3rem;
  align-items: center;
}
.movelist__num { color: var(--ink-soft); font-size: .85rem; padding: .15rem 0; }
.movelist__move {
  font: inherit;
  text-align: left;
  padding: .15rem .35rem;
  background: none;
  border: 0;
  border-radius: 5px;
  color: var(--ink);
  cursor: pointer;
}
.movelist__move:hover { background: var(--surface-sunken); }
.movelist__move--current { background: var(--accent); color: var(--accent-ink); font-weight: 600; }
.movelist__move:disabled { cursor: default; background: none; }

/* -------------------------------------------------------------- export ---- */

.export { display: grid; gap: .35rem; }
.export__row { display: flex; align-items: center; justify-content: space-between; gap: .5rem; }
.export__value {
  display: block;
  font-family: ui-monospace, SFMono-Regular, Menlo, Consolas, monospace;
  font-size: .74rem;
  line-height: 1.45;
  padding: .5rem .6rem;
  background: var(--surface-sunken);
  border-radius: 6px;
  word-break: break-all;
  color: var(--ink-soft);
  max-height: 4.5rem;
  overflow-y: auto;
}
.export__value--tall { max-height: 7rem; word-break: normal; }

/* --------------------------------------------------------------- modal ---- */

.modal {
  position: fixed;
  inset: 0;
  display: flex;
  align-items: center;
  justify-content: center;
  background: rgba(12, 11, 14, .5);
  z-index: 40;
  padding: 1rem;
}
.modal__card {
  background: var(--surface);
  border-radius: var(--radius);
  padding: 1.25rem;
  box-shadow: var(--shadow);
  display: grid;
  gap: .85rem;
  justify-items: center;
}
.modal__title { font-size: 1rem; }
.modal__choices { display: flex; gap: .4rem; }
.modal__choice {
  width: 4rem;
  height: 4rem;
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 2.6rem;
  line-height: 1;
  background: var(--sq-light);
  border: 2px solid transparent;
  border-radius: 8px;
  cursor: pointer;
}
.modal__choice:hover, .modal__choice:focus-visible { border-color: var(--accent); }

.colophon {
  margin-top: 1.5rem;
  font-size: .82rem;
  color: var(--ink-soft);
  text-align: center;
}

@media (prefers-reduced-motion: reduce) {
  * { transition: none !important; animation: none !important; }
}

/* ------------------------------------------------------------------ 3D --- */

/* A board seen in perspective projects wider than it is tall, so a square
   frame would band it with empty space top and bottom. This ratio is close to
   sin(elevation) at the default camera; the renderer refits itself to whatever
   shape it is given, so nothing breaks when the player orbits away from it. */
.board-frame--3d { aspect-ratio: 6 / 5; }

.board-3d {
  position: absolute;
  inset: 0;
  display: block;
  width: 100%;
  height: 100%;
  /* The renderer reads pointer drags for orbiting, so the page must not
     also scroll from the same gesture. */
  touch-action: none;
  cursor: grab;
}
.board-3d:active { cursor: grabbing; }

/* In 3D the canvas is the picture, but the grid stays in the document so the
   keyboard and screen-reader paths keep working exactly as they do in 2D.
   Clipped rather than `display: none`, which would make it unfocusable. */
.board--offscreen {
  position: absolute;
  width: 1px;
  height: 1px;
  overflow: hidden;
  clip-path: inset(50%);
  white-space: nowrap;
  pointer-events: none;
}

.segmented {
  display: flex;
  align-items: center;
  gap: .35rem;
  margin-top: -.4rem;
}
.segmented__label {
  font-size: .78rem;
  text-transform: uppercase;
  letter-spacing: .06em;
  color: var(--ink-soft);
  margin-right: .15rem;
}
.segmented__option {
  font: inherit;
  font-size: .84rem;
  font-weight: 550;
  padding: .28rem .7rem;
  color: var(--ink-soft);
  background: var(--surface-sunken);
  border: 1px solid var(--border);
  border-radius: 999px;
  cursor: pointer;
}
.segmented__option[aria-pressed="true"] {
  background: var(--accent);
  border-color: transparent;
  color: var(--accent-ink);
}
.segmented__option:disabled { opacity: .4; cursor: not-allowed; }
