:root {
  --ink: #3d2f1f;
  --ink-soft: #6a5840;
  --ink-faded: #75603e;
  --paper: #ebe5d4;
  --paper-warm: #e0d8c2;
  --accent: #3a5a3a;
  --rule: #c9bfa3;
  --red-piece: #7a2e1e;
  --black-piece: #2a241c;
  --font-display: 'Cormorant Garamond', 'Noto Serif HK', Georgia, serif;
  --font-body: 'Noto Serif HK', 'Cormorant Garamond', Georgia, serif;
  --font-mono: 'JetBrains Mono', 'SF Mono', Menlo, monospace;
}

* { box-sizing: border-box; }

html {
  -webkit-font-smoothing: antialiased;
}

body {
  margin: 0;
  min-height: 100vh;
  background: var(--paper);
  color: var(--ink);
  font-family: var(--font-body);
  background-image:
    radial-gradient(at 20% 30%, rgba(58, 90, 58, 0.02) 0px, transparent 50%),
    radial-gradient(at 80% 70%, rgba(61, 47, 31, 0.03) 0px, transparent 50%);
}

button, select {
  font: inherit;
  color: inherit;
}

.xq-shell {
  max-width: 1100px;
  margin: 0 auto;
  padding: 28px 20px 48px;
}

.xq-top {
  display: flex;
  align-items: baseline;
  justify-content: space-between;
  gap: 16px;
  border-bottom: 1px solid var(--rule);
  padding-bottom: 20px;
  margin-bottom: 16px;
}

.xq-back,
.xq-home {
  font-family: var(--font-mono);
  font-size: 12px;
  letter-spacing: 0.02em;
  color: var(--ink-faded);
  text-decoration: none;
  white-space: nowrap;
}

.xq-back:hover,
.xq-home:hover {
  color: var(--accent);
}

.xq-brand {
  text-align: center;
  flex: 1;
}

.xq-title {
  margin: 0;
  font-family: var(--font-display);
  font-weight: 500;
  font-size: clamp(28px, 4vw, 40px);
  letter-spacing: -0.02em;
  line-height: 1.1;
}

.xq-title-en {
  margin: 4px 0 0;
  font-family: var(--font-display);
  font-style: italic;
  font-size: 15px;
  color: var(--ink-faded);
}

.xq-lead {
  margin: 0 0 28px;
  font-size: 15px;
  color: var(--ink-soft);
  text-align: center;
}

.xq-lead span {
  color: var(--ink-faded);
}

.xq-layout {
  display: grid;
  grid-template-columns: minmax(0, 1fr) minmax(220px, 280px);
  gap: 28px;
  align-items: start;
}

@media (max-width: 820px) {
  .xq-layout {
    grid-template-columns: 1fr;
  }
}

.xq-board-wrap {
  position: relative;
  background: var(--paper-warm);
  border: 1px solid var(--rule);
  padding: 18px 14px 22px;
  box-shadow: 0 1px 2px rgba(61, 47, 31, 0.05), 0 8px 24px rgba(61, 47, 31, 0.07);
}

.xq-palace-lines {
  position: absolute;
  /* match board box: padding 18/14/22/14, board width = 9*cell centered */
  left: 50%;
  top: 18px;
  width: calc(9 * min(9.2vw, 52px));
  height: calc(10 * min(9.2vw, 52px));
  transform: translateX(-50%);
  pointer-events: none;
  z-index: 1;
  overflow: visible;
}

.xq-palace-lines path {
  fill: none;
  stroke: rgba(61, 47, 31, 0.4);
  stroke-width: 0.06;
}

.xq-board-wrap:has(.xq-board.is-flipped) .xq-palace-lines {
  transform: translateX(-50%) rotate(180deg);
}

.xq-board {
  --cell: min(9.2vw, 52px);
  display: grid;
  grid-template-columns: repeat(9, var(--cell));
  grid-template-rows: repeat(10, var(--cell));
  gap: 0;
  width: calc(9 * var(--cell));
  margin: 0 auto;
  position: relative;
  background:
    linear-gradient(var(--ink-soft), var(--ink-soft)) center / 1px 100% no-repeat;
  transition: transform 0.45s ease;
}

.xq-board.is-flipped {
  transform: rotate(180deg);
}

.xq-board.is-flipped .xq-piece {
  /* Must repeat the centring translate — a bare rotate() would replace it
     and shove every piece half a cell off its intersection. */
  transform: translate(-50%, -50%) rotate(180deg);
}

/* Grid lines via cell borders — intersections feel */
.xq-cell {
  position: relative;
  width: var(--cell);
  height: var(--cell);
  padding: 0;
  border: none;
  background: transparent;
  cursor: pointer;
  appearance: none;
  -webkit-appearance: none;
}

.xq-cell::before {
  content: '';
  position: absolute;
  left: 50%;
  top: 0;
  bottom: 0;
  width: 1px;
  background: rgba(61, 47, 31, 0.35);
  transform: translateX(-50%);
}

.xq-cell::after {
  content: '';
  position: absolute;
  top: 50%;
  left: 0;
  right: 0;
  height: 1px;
  background: rgba(61, 47, 31, 0.35);
  transform: translateY(-50%);
}

/* Outer frame edges: clip lines that would run past the board */
.xq-cell[data-rank="9"]::before { top: 50%; }
.xq-cell[data-rank="0"]::before { bottom: 50%; }
.xq-cell[data-file="0"]::after { left: 50%; }
.xq-cell[data-file="8"]::after { right: 50%; }

.xq-cell.is-palace::before,
.xq-cell.is-palace::after {
  background: rgba(61, 47, 31, 0.45);
}

.xq-cell.is-selected {
  z-index: 2;
}

.xq-cell.is-selected .xq-piece {
  box-shadow: 0 0 0 2px var(--accent);
}

.xq-cell.is-hint:not(:has(.xq-piece)) {
  background: radial-gradient(circle at center, rgba(58, 90, 58, 0.35) 0 14%, transparent 15%);
}

.xq-cell.is-hint:has(.xq-piece) .xq-piece {
  box-shadow: 0 0 0 2px rgba(58, 90, 58, 0.55);
}

.xq-cell.is-last-from .xq-piece,
.xq-cell.is-last-to .xq-piece {
  background: #e8d9b8;
}

.xq-piece {
  position: absolute;
  z-index: 3;
  left: 50%;
  top: 50%;
  width: calc(var(--cell) * 0.78);
  height: calc(var(--cell) * 0.78);
  margin: 0;
  transform: translate(-50%, -50%);
  border-radius: 50%;
  display: grid;
  place-items: center;
  font-family: var(--font-body);
  font-size: calc(var(--cell) * 0.38);
  font-weight: 600;
  line-height: 1;
  background: #f3ecda;
  border: 1.5px solid var(--rule);
  box-shadow: 0 1px 2px rgba(61, 47, 31, 0.12);
  pointer-events: none;
  transition: transform 0.45s ease, box-shadow 0.2s ease, background 0.2s ease;
}

.xq-piece--red {
  color: var(--red-piece);
  border-color: rgba(122, 46, 30, 0.35);
}

.xq-piece--black {
  color: var(--black-piece);
  border-color: rgba(42, 36, 28, 0.4);
}

.xq-river {
  position: absolute;
  left: 14px;
  right: 14px;
  top: 50%;
  transform: translateY(-50%);
  text-align: center;
  font-family: var(--font-display);
  font-size: 13px;
  letter-spacing: 0.35em;
  color: rgba(61, 47, 31, 0.28);
  pointer-events: none;
  z-index: 1;
}

.xq-side {
  display: flex;
  flex-direction: column;
  gap: 14px;
}

.xq-status {
  font-family: var(--font-display);
  font-size: 20px;
  font-weight: 500;
  min-height: 1.4em;
}

.xq-engine-status {
  width: fit-content;
  margin-top: -8px;
  padding: 3px 7px;
  border: 1px solid var(--rule);
  font-family: var(--font-mono);
  font-size: 10px;
  color: var(--ink-faded);
  letter-spacing: 0.03em;
}

.xq-engine-status[data-status="ready"] {
  border-color: rgba(58, 90, 58, 0.38);
  color: var(--accent);
}

.xq-eval-track {
  height: 6px;
  background: rgba(61, 47, 31, 0.12);
  border: 1px solid var(--rule);
  overflow: hidden;
}

.xq-eval-fill {
  height: 100%;
  width: 50%;
  background: var(--accent);
  opacity: 0.75;
  transition: width 0.35s ease;
}

.xq-eval-label {
  display: block;
  margin-top: 6px;
  font-family: var(--font-mono);
  font-size: 11px;
  color: var(--ink-faded);
  letter-spacing: 0.04em;
}

.xq-field {
  display: flex;
  flex-direction: column;
  gap: 6px;
  font-size: 13px;
  color: var(--ink-soft);
}

.xq-field select {
  background: var(--paper-warm);
  border: 1px solid var(--rule);
  padding: 8px 10px;
  border-radius: 0;
}

.xq-actions {
  display: flex;
  gap: 8px;
}

.xq-btn {
  flex: 1;
  padding: 10px 12px;
  background: var(--ink);
  color: var(--paper);
  border: 1px solid var(--ink);
  cursor: pointer;
  font-size: 14px;
  transition: background 0.2s ease, color 0.2s ease;
}

.xq-btn:hover {
  background: var(--accent);
  border-color: var(--accent);
}

.xq-btn-quiet {
  background: transparent;
  color: var(--ink);
}

.xq-btn-quiet:hover {
  background: var(--paper-warm);
  color: var(--ink);
  border-color: var(--rule);
}

.xq-btn:disabled {
  opacity: 0.4;
  cursor: not-allowed;
}

.xq-side-h {
  margin: 0 0 8px;
  font-family: var(--font-mono);
  font-size: 11px;
  font-weight: 500;
  letter-spacing: 0.06em;
  text-transform: uppercase;
  color: var(--ink-faded);
}

.xq-move-list {
  margin: 0;
  padding: 0;
  list-style: none;
  max-height: 160px;
  overflow: auto;
  font-size: 14px;
  color: var(--ink-soft);
  line-height: 1.7;
}

.xq-move-list li {
  display: grid;
  grid-template-columns: 2em 1fr 1fr;
  gap: 6px;
  padding: 3px 0;
  border-bottom: 1px solid rgba(201, 191, 163, 0.45);
}

.xq-turn {
  font-family: var(--font-mono);
  font-size: 11px;
  color: var(--ink-faded);
}

.xq-move-name {
  color: var(--ink);
  white-space: nowrap;
}

.xq-notation-help {
  margin-top: 8px;
  font-size: 11px;
  color: var(--ink-faded);
}

.xq-notation-help summary {
  width: fit-content;
  cursor: pointer;
  font-family: var(--font-mono);
}

.xq-notation-help p {
  margin: 6px 0 0;
  line-height: 1.6;
}

.xq-review {
  border-top: 1px solid var(--rule);
  padding-top: 14px;
}

.xq-review-progress {
  margin: 0;
  padding: 10px;
  background: rgba(224, 216, 194, 0.62);
  font-size: 13px;
  color: var(--ink-soft);
}

.xq-highlight {
  margin: 0 0 12px;
  padding: 12px 14px;
  background: var(--paper-warm);
  border-left: 2px solid var(--ink-faded);
}

.xq-highlight--blunder { border-left-color: var(--red-piece); }
.xq-highlight--mistake { border-left-color: #8a4b2a; }
.xq-highlight--inaccuracy { border-left-color: var(--ink-faded); }
.xq-highlight--best { border-left-color: var(--accent); }

.xq-highlight-tag {
  display: block;
  font-family: var(--font-mono);
  font-size: 10px;
  letter-spacing: 0.06em;
  text-transform: uppercase;
  color: var(--ink-faded);
  margin-bottom: 6px;
}

.xq-highlight p {
  margin: 0 0 8px;
  font-size: 13.5px;
  line-height: 1.65;
  color: var(--ink);
}

.xq-review-sum {
  margin: 0 0 10px;
  font-family: var(--font-mono);
  font-size: 12px;
  color: var(--ink-faded);
}

.xq-review-list {
  margin: 0;
  padding: 0;
  list-style: none;
  max-height: 220px;
  overflow: auto;
  font-size: 13px;
  line-height: 1.55;
}

.xq-note {
  padding: 10px 0;
  border-bottom: 1px solid rgba(201, 191, 163, 0.55);
  color: var(--ink-soft);
}

.xq-note-head {
  display: flex;
  justify-content: space-between;
  gap: 8px;
  color: var(--ink);
}

.xq-note p {
  margin: 4px 0 6px;
  font-size: 12px;
  color: var(--ink-soft);
}

.xq-note-line {
  margin: 4px 0;
  line-height: 1.55;
}

.xq-note-line strong {
  color: var(--ink);
}

.xq-note-cost {
  font-family: var(--font-mono);
  font-size: 10.5px;
  color: var(--ink-faded);
}

.xq-verdict {
  flex: 0 0 auto;
  font-family: var(--font-mono);
  font-size: 10px;
  letter-spacing: 0.04em;
}

.xq-note-actions {
  display: flex;
  gap: 12px;
}

.xq-note-actions button,
.xq-text-btn {
  padding: 0;
  border: 0;
  border-bottom: 1px solid var(--rule);
  background: transparent;
  color: var(--ink-faded);
  font-family: var(--font-mono);
  font-size: 10px;
  cursor: pointer;
}

.xq-note-actions button:hover,
.xq-text-btn:hover {
  color: var(--accent);
  border-color: var(--accent);
}

.xq-text-btn {
  margin-top: 10px;
}

.xq-note--best { color: var(--accent); }
.xq-note--blunder { color: var(--red-piece); }
.xq-note--mistake { color: #8a4b2a; }
.xq-note--inaccuracy { color: var(--ink); }

.xq-foot {
  margin-top: 36px;
  padding-top: 16px;
  border-top: 1px solid var(--rule);
  font-family: var(--font-mono);
  font-size: 12px;
  color: var(--ink-faded);
  display: flex;
  flex-wrap: wrap;
  gap: 6px;
  align-items: center;
}

.xq-foot a {
  color: inherit;
  text-decoration: none;
}

.xq-foot a:hover {
  color: var(--accent);
}

.xq-dot { opacity: 0.5; margin: 0 4px; }

.xq-colophon {
  flex-basis: 100%;
  margin-top: 8px;
}

.xq-colophon summary {
  width: fit-content;
  cursor: pointer;
  opacity: 0.7;
}

.xq-colophon summary:hover { color: var(--accent); }

.xq-colophon p {
  margin: 8px 0 0;
  font-size: 11px;
  line-height: 1.7;
  opacity: 0.85;
}

@media (prefers-reduced-motion: reduce) {
  .xq-board,
  .xq-piece,
  .xq-eval-fill,
  .xq-btn {
    transition: none !important;
  }
}

:focus-visible {
  outline: 2px solid var(--accent);
  outline-offset: 2px;
}
