/* The chess section of the CV page (#chess). Builds on ../styles.css tokens. */

:root {
  --sq-light: #f3ece0;
  --sq-dark: #c9b79c;
  --white-share: 0.5;
  --think: 2500ms;
}

/* ---------- page layout: CV on the left, chess on the right ---------- */

.chess-sheet {
  container-type: inline-size;
  margin-block-end: 2.5rem;
}

/* Wide screens: the CV stays centred and the chess panel sits centred in the free
   space to its right, as big as that space allows (board-only when it is narrow).
   Hovering the panel (or tabbing into it) pushes the CV aside and the panel grows.
   The CV only slides (no reflow); the panel animates its width. */
.chess-peek { display: none; }

@media screen and (min-width: 75rem) {
  :root {
    --duo-gap: 1.25rem;
    --page-w: calc(100vw - var(--sbw, 0px));
    --chess-w: clamp(12rem, (var(--page-w) - 68rem) / 2 - 2 * var(--duo-gap), 28rem);
    --cv-w: min(76rem, var(--page-w) - 2 * (var(--chess-w) + 2 * var(--duo-gap)));
    --gutter: calc((var(--page-w) - var(--cv-w)) / 2);
    --chess-right: calc((var(--gutter) - var(--chess-w)) / 2);
    /* open: CV slides to the left margin and the panel takes the rest; on smaller
       screens at least 40rem, even if the CV has to slide partly out of view */
    --open-w: clamp(40rem, var(--page-w) - var(--chess-right) - var(--cv-w) - 2 * var(--duo-gap), 76rem);
  }
  .cv-col,
  .layout > .colophon { inline-size: var(--cv-w); margin-inline: auto; }
  .cv-col > .sheet { inline-size: 100%; }
  .cv-col { transition: translate var(--morph-open, 0.5s) cubic-bezier(0.2, 0.8, 0.2, 1); }

  .chess-sheet {
    position: fixed;
    z-index: 60;
    inset-block-start: 50%;
    inset-inline-end: var(--chess-right);
    translate: 0 -50%;
    inline-size: var(--chess-w);
    max-block-size: calc(100svh - 2rem);
    margin: 0;
    padding: 1.4rem;
    overflow-y: auto;
    overscroll-behavior: contain;
    scrollbar-width: thin;
    box-shadow: 0 1px 2px rgb(24 24 27 / 0.06), 0 30px 80px -20px rgb(24 24 27 / 0.3);
    border-radius: 1.75rem;
  }
  .chess-sheet .chess-head h2 { font-size: 1.6rem; }
  .chess-sheet .play { inline-size: 100%; min-inline-size: 0; }

  /* open: the panel grows to the left and the CV moves out of its way */
  .chess-pushed .cv-col {
    translate: calc(var(--gutter) - var(--chess-right) - var(--open-w) - var(--duo-gap)) 0;
  }
  .chess-sheet.is-open {
    inline-size: var(--open-w);
    padding: 1.4rem;
  }
  .chess-sheet.is-open .play { inline-size: min(100%, calc(100svh - 9rem)); }

  /* narrow free space: just the board and a caption until you hover */
  .chess-peek {
    grid-column: 1 / -1;
    margin-block-start: 0.6rem;
    font-size: 0.8rem;
    font-weight: 700;
    line-height: 1.3;
    text-align: center;
    color: var(--ink-2);
  }
  .chess-sheet:not(.is-open) { padding: 1.75rem 1.75rem 2rem; }

  /* Everything below 1440p-class screens (also 1080p with the browser zoomed out).
     Only screens that are both very wide and tall keep the full resting panel and
     the side-by-side open panel. */
  @media (max-height: 77.99rem), (max-width: 139.99rem) {
    /* resting: just the heading, the board and the status line */
    .chess-sheet:not(.is-open) .panel > :not(.status-card) { display: none; }
    .chess-sheet:not(.is-open) .chess { gap: 1rem; }
    .chess-sheet:not(.is-open) { overflow: hidden; }

    /* open: sits right under the top bar like the CV, and only as wide as the space
       left once the CV has slid to the left margin (so the CV stays on screen).
       Board on the left, the small controls stacked beside it, the rest below. */
    :root {
      --open-w: max(34rem, var(--page-w) - var(--chess-right) - var(--cv-w) - 2 * var(--duo-gap));
    }
    .chess-sheet.is-open .chess-head { margin-block-end: 1rem; }
    .chess-sheet.is-open .chess {
      grid-template-columns: minmax(0, 1fr) 12.5rem;
      grid-template-rows: auto 1fr;
      gap: 1rem 1.1rem;
    }
    .chess-sheet.is-open .panel { display: contents; }
    .chess-sheet.is-open .play {
      grid-column: 1;
      grid-row: 1 / span 2;
      inline-size: 100%;
      min-inline-size: 0;
    }
    .chess-sheet.is-open .status-card { grid-column: 2; grid-row: 1; }
    .chess-sheet.is-open .controls { grid-column: 2; grid-row: 2; flex-direction: column; gap: 0.9rem; }
    .chess-sheet.is-open .controls .seg { inline-size: 100%; }
    .chess-sheet.is-open .controls .seg label { flex: 1; }
    .chess-sheet.is-open .controls .seg span { text-align: center; padding-inline: 0.5rem; }
    .chess-sheet.is-open .controls .buttons {
      flex-basis: auto;
      display: grid;
      grid-template-columns: 1fr auto;
      grid-auto-flow: column;
      grid-template-rows: auto auto;
    }
    .chess-sheet.is-open .panel > :not(.status-card, .controls) { grid-column: 1 / -1; }
    .chess-sheet.is-open .panel > .engine-card { grid-column: 1 / 2; }
    .chess-sheet.is-open .panel > .moves-card { grid-column: 2 / 3; }
  }
  /* the compact board-only state (narrow free space) can sit closer to the edge */
  @media (max-width: 113rem) {
    .chess-sheet:not(.is-open) { padding: 0.9rem; }
  }
  @container (max-width: 19.9rem) {
    .chess-sheet .chess-head, .chess-sheet .panel { display: none; }
    .chess-sheet .chess-peek { display: block; }
    .chess-sheet .play { grid-template-columns: 0.4rem 1fr; gap: 0.4rem; }
  }
}
@media (prefers-reduced-motion: reduce) {
  .cv-col { transition: none; }
}

.chess-head { margin-block-end: 1.25rem; }
/* "Side project", highlighted because it literally sits on the side */
.side-word {
  display: inline-block;
  padding: 0.12em 0.45em 0.08em;
  margin-inline-end: 0.1em;
  border-radius: 0.4em;
  background: var(--accent);
  color: #fff;
  rotate: -4deg;
  transition: translate 0.3s var(--ease), rotate 0.3s var(--ease);
}
.chess-sheet.is-open .side-word { translate: 0.2em 0; rotate: 3deg; }
@media (prefers-reduced-motion: reduce) { .side-word { transition: none; } }
.chess-kicker {
  font-size: 0.78rem;
  font-weight: 700;
  letter-spacing: 0.08em;
  text-transform: uppercase;
  color: var(--accent-ink);
}
.chess-head h2 {
  font-size: clamp(1.6rem, 1.2rem + 1.2vw, 2.4rem);
  font-weight: 800;
  line-height: 1.05;
  letter-spacing: -0.035em;
  text-wrap: balance;
}
.chess-head h2 em { font-family: var(--serif); font-weight: 500; letter-spacing: -0.02em; color: var(--accent-ink); }

.chess {
  display: grid;
  gap: clamp(1.25rem, 0.75rem + 1.5vw, 2.5rem);
  align-items: start;
}
/* board and panel side by side once the chess column is wide enough */
@container (min-width: 46rem) {
  .chess { grid-template-columns: minmax(0, 1fr) minmax(17rem, 22rem); }
}

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

.play {
  display: grid;
  grid-template-columns: 0.75rem 1fr;
  gap: 0.75rem;
  /* the board is as large as the viewport height allows */
  inline-size: min(100%, calc(100svh - 10.5rem));
  min-inline-size: min(100%, 24rem);
  margin-inline: auto;
}

.evalbar {
  position: relative;
  border-radius: var(--r-pill);
  background: var(--ink);
  overflow: hidden;
  outline: 1px solid rgb(24 24 27 / 0.14);
}
.evalbar span {
  position: absolute;
  inset: auto 0 0;
  block-size: calc(var(--white-share) * 100%);
  background: #fff;
  border-block-start: 1px solid var(--line);
  transition: block-size 0.6s var(--ease);
}
.flipped-eval .evalbar span { inset: 0 0 auto; }

.board-wrap {
  position: relative;
  border-radius: var(--r-lg);
  box-shadow: 0 0 0 1px rgb(24 24 27 / 0.08), var(--shadow);
  overflow: hidden;
}

.board {
  display: grid;
  grid-template-columns: repeat(8, 1fr);
  grid-template-rows: repeat(8, 1fr);
  aspect-ratio: 1;
  touch-action: none;
  user-select: none;
}

.sq {
  position: relative;
  display: grid;
  place-items: center;
  padding: 0;
  border: 0;
  background: var(--sq-light);
  cursor: pointer;
  font: inherit;
  min-block-size: 0;
  overflow: visible;
}
.sq.dark { background: var(--sq-dark); }
.sq:focus-visible { outline: 3px solid var(--accent); outline-offset: -3px; border-radius: 0; z-index: 2; }

.sq img {
  position: absolute;
  inset: 7%;
  inline-size: 86%;
  block-size: 86%;
  object-fit: contain;
  z-index: 1;
  pointer-events: none;
  transition: scale 0.12s var(--ease);
}
.sq img.dragging { z-index: 5; scale: 1.12; transition: none; filter: drop-shadow(0 8px 10px rgb(0 0 0 / 0.25)); }

.sq.last::before,
.sq.sel::before {
  content: "";
  position: absolute;
  inset: 0;
  background: rgb(255 91 53 / 0.22);
}
.sq.sel::before { background: rgb(255 91 53 / 0.42); }
.sq.check::before {
  content: "";
  position: absolute;
  inset: 0;
  background: radial-gradient(circle, rgb(220 38 38 / 0.85) 0 30%, rgb(220 38 38 / 0) 72%);
}
.sq.dot::after {
  content: "";
  position: absolute;
  inline-size: 30%;
  aspect-ratio: 1;
  border-radius: 50%;
  background: rgb(24 24 27 / 0.2);
}
.sq.hit::after {
  content: "";
  position: absolute;
  inset: 5%;
  border-radius: 50%;
  border: 0.35rem solid rgb(24 24 27 / 0.2);
}

/* coordinates in the corners */
.sq[data-rank]::before,
.sq[data-file]::after { font-size: clamp(0.6rem, 0.45rem + 0.4vw, 0.78rem); font-weight: 700; color: rgb(24 24 27 / 0.45); }
.sq[data-rank]:not(.last):not(.sel):not(.check)::before {
  content: attr(data-rank);
  position: absolute;
  inset: 0.2rem auto auto 0.3rem;
  background: none;
}
.sq[data-file]:not(.dot):not(.hit)::after {
  content: attr(data-file);
  position: absolute;
  inset: auto 0.3rem 0.15rem auto;
}

/* promotion and result overlays */
.promo,
.game-over {
  position: absolute;
  inset: 0;
  z-index: 10;
  display: grid;
  place-content: center;
  justify-items: center;
  gap: 1rem;
  background: rgb(253 252 250 / 0.82);
  -webkit-backdrop-filter: blur(6px);
  backdrop-filter: blur(6px);
  text-align: center;
}
.promo[hidden], .game-over[hidden] { display: none; }
.promo p { font-weight: 700; }
.promo-pieces { display: flex; gap: 0.5rem; }
.promo-pieces button {
  inline-size: clamp(3.5rem, 9vw, 5.5rem);
  aspect-ratio: 1;
  padding: 0.4rem;
  border: 1px solid var(--line);
  border-radius: var(--r-md);
  background: var(--sheet);
  cursor: pointer;
  transition: background 0.15s, translate 0.15s var(--ease);
}
.promo-pieces button:hover { background: var(--accent-tint); translate: 0 -2px; }
.promo-pieces img { inline-size: 100%; block-size: 100%; }
.game-over-title {
  padding-inline: 1.5rem;
  text-wrap: balance;
  font-size: clamp(1.6rem, 1.2rem + 2vw, 2.6rem);
  font-weight: 800;
  letter-spacing: -0.02em;
}

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

.panel { display: grid; gap: 1.1rem; }

.intro { color: var(--ink-2); font-size: 1rem; }
.intro strong { color: var(--ink); }

.label {
  display: block;
  margin-block-end: 0.4rem;
  font-size: 0.78rem;
  font-weight: 700;
  letter-spacing: 0.06em;
  text-transform: uppercase;
  color: var(--ink-2);
}

.status-card,
.engine-card,
.moves-card {
  padding: 1rem 1.1rem;
  border-radius: var(--r-md);
  background: var(--well);
}
.status-card { position: relative; overflow: hidden; background: var(--ink); color: #fff; }
.chess-status { font-weight: 700; }
.think {
  position: absolute;
  inset: auto 0 0;
  block-size: 3px;
  opacity: 0;
}
.think span { display: block; block-size: 100%; inline-size: 0; background: var(--accent); }
.thinking .think { opacity: 1; }
.thinking .think span { animation: think var(--think) linear forwards; }
@keyframes think { to { inline-size: 100%; } }

.controls { display: flex; flex-wrap: wrap; gap: 0.9rem 1.5rem; }
.controls .buttons { flex-basis: 100%; }
.seg {
  display: inline-flex;
  padding: 0.25rem;
  border-radius: var(--r-pill);
  background: var(--well);
  gap: 0.15rem;
}
.seg label { position: relative; }
.seg input { position: absolute; opacity: 0; inset: 0; margin: 0; cursor: pointer; }
.seg span {
  display: block;
  padding: 0.35rem 0.95rem;
  border-radius: var(--r-pill);
  font-size: 0.92rem;
  font-weight: 600;
  color: var(--ink-2);
  transition: background 0.15s, color 0.15s;
}
.seg input:checked + span { background: var(--sheet); color: var(--ink); box-shadow: 0 1px 3px rgb(24 24 27 / 0.12); }
.seg input:focus-visible + span { outline: 2.5px solid var(--accent); outline-offset: 2px; }

.buttons { display: flex; flex-wrap: wrap; gap: 0.5rem; }
.btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 0.4rem;
  min-block-size: 2.75rem;
  padding: 0.5rem 1.15rem;
  border: 1px solid var(--line);
  border-radius: var(--r-pill);
  background: var(--sheet);
  color: var(--ink);
  font: inherit;
  font-size: 0.95rem;
  font-weight: 650;
  cursor: pointer;
  transition: background 0.15s, translate 0.15s var(--ease);
}
.btn:hover { background: var(--well); translate: 0 -1px; }
.btn-ink { background: var(--ink); border-color: var(--ink); color: #fff; }
.btn-ink:hover { background: var(--accent-ink); border-color: var(--accent-ink); }
.btn-icon { inline-size: 2.75rem; padding: 0; }
.btn-icon svg { inline-size: 1.15rem; fill: none; stroke: currentColor; stroke-width: 2; stroke-linecap: round; stroke-linejoin: round; }
.btn[aria-pressed="false"] { color: var(--ink-2); text-decoration: line-through; }
.btn[aria-pressed="false"] svg { opacity: 0.4; }

.engine-head { display: flex; justify-content: space-between; align-items: baseline; }
.eval-num { font-weight: 800; font-variant-numeric: tabular-nums; }
.lines, .moves { font-size: 0.95rem; }
.lines li span, .moves li { font-variant-numeric: tabular-nums; }
.lines li { display: flex; justify-content: space-between; padding-block: 0.15rem; }
.lines li:first-child:not(.muted) { font-weight: 700; }
.lines li span:last-child { color: var(--ink-2); }
.muted { color: var(--ink-2); font-size: 0.9rem; }

.moves {
  max-block-size: 9.5rem;
  overflow-y: auto;
  counter-reset: mv;
  scrollbar-width: thin;
}
.moves li {
  display: grid;
  grid-template-columns: 2rem 1fr 1fr;
  counter-increment: mv;
  padding-block: 0.1rem;
}
.moves li::before { content: counter(mv) "."; color: var(--ink-2); }
.moves:empty::after { content: "No moves yet."; color: var(--ink-2); font-size: 0.9rem; }

.how { font-size: 0.95rem; color: var(--ink-2); }
.how summary { cursor: pointer; font-weight: 700; color: var(--ink); }
.how ul { margin-block: 0.6rem; padding-inline-start: 1.1rem; list-style: disc; display: grid; gap: 0.25rem; }

.links { display: flex; gap: 1.25rem; flex-wrap: wrap; font-weight: 600; }

/* ---------- small screens ---------- */

@media (max-width: 40rem) {
  .play { grid-template-columns: 0.5rem 1fr; gap: 0.5rem; inline-size: 100%; }
  .board-wrap { border-radius: var(--r-md); }
  .sq.hit::after { border-width: 0.22rem; }
}

@media (prefers-reduced-motion: reduce) {
  .evalbar span, .sq img, .btn, .promo-pieces button { transition: none; }
}

@media print {
  .chess-sheet { display: none; }
  .layout { display: block; }
}

@media (max-width: 26rem) {
  .buttons .btn:not(.btn-icon) { padding-inline: 0.85rem; }
  .buttons { gap: 0.35rem; }
}

/* narrow chess panel (wide screens with the side panel): keep all buttons on one row */
@container (max-width: 24rem) {
  .buttons { gap: 0.35rem; flex-wrap: nowrap; }
  .buttons .btn:not(.btn-icon) { padding-inline: 0.75rem; font-size: 0.875rem; white-space: nowrap; flex: 1; }
  .buttons .btn-icon { inline-size: 2.4rem; min-block-size: 2.4rem; flex: none; }
  .buttons .btn { min-block-size: 2.4rem; }
}

/* frozen copy of the previous view, cross-faded out during the open/close morph */
.chess-ghost { pointer-events: none; z-index: 61 !important; }
