:root {
  --page: #f4f1ea;
  --surface: #fffdf8;
  --surface-soft: #f8f4ec;
  --surface-warm: #ead3ad;
  --line: #d8d0c3;
  --line-strong: #9d8464;
  --ink: #1f2523;
  --ink-soft: #626b66;
  --ink-muted: #8a918c;
  --red: #a93226;
  --red-strong: #84251d;
  --black-piece: #1f2523;
  --green: #2f6a4d;
  --amber: #b16b2c;
  --board-line: #65431f;
  --board-marker: rgba(101, 67, 31, 0.74);
  --board-highlight: rgba(169, 50, 38, 0.14);
  --shadow: 0 12px 30px rgba(31, 37, 35, 0.1);
  --piece-font: "FZKai-Z03", "KaiTi", "Kaiti SC", "STKaiti", "AR PL UKai CN", "Songti SC", serif;
  --piece-size: var(--board-piece-size, min(12.5cqw, 142px));
  --piece-font-size: calc(var(--piece-size) / 2);
  --piece-border-width: 3px;
  --piece-groove-width: 1.5px;
  --board-piece-pad: calc(var(--piece-size) / 2);
  --board-grid-inset: calc(var(--piece-size) + var(--piece-size) / 10);
  --piece-hit: calc(var(--piece-size) + 16px);
  color-scheme: light;
}

* {
  box-sizing: border-box;
}

html,
body {
  margin: 0;
  min-height: 100%;
}

body {
  min-height: 100vh;
  background: var(--page);
  color: var(--ink);
  font-family:
    "Inter",
    "PingFang SC",
    "Microsoft YaHei",
    "Noto Sans CJK SC",
    system-ui,
    sans-serif;
}

button,
input,
select,
textarea,
a {
  font: inherit;
}

button,
select,
input,
textarea {
  color: inherit;
}

code,
pre,
.fen-output,
.move-button {
  font-family: "SFMono-Regular", "Menlo", "Consolas", monospace;
}

a {
  color: inherit;
}

.app-shell {
  width: min(1380px, calc(100% - 32px));
  margin: 0 auto;
  padding: 18px 0 32px;
}

.topbar {
  min-height: 56px;
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 18px;
  padding: 8px 0 16px;
}

.brand {
  display: inline-flex;
  align-items: center;
  gap: 12px;
  color: var(--ink);
  text-decoration: none;
  min-width: 0;
}

.brand-mark {
  width: 40px;
  height: 40px;
  display: grid;
  place-items: center;
  border: 2px solid var(--red);
  border-radius: 50%;
  background: #f2dfbb;
  color: var(--red);
  font-family: var(--piece-font);
  font-size: 1.45rem;
  line-height: 1;
  box-shadow: inset 0 0 0 2px rgba(255, 253, 248, 0.7);
  flex: 0 0 auto;
}

.brand-title,
.brand-subtitle {
  display: block;
  line-height: 1.15;
}

.brand-title {
  font-size: 1.08rem;
  font-weight: 700;
}

.brand-subtitle {
  margin-top: 2px;
  color: var(--ink-soft);
  font-size: 0.78rem;
  letter-spacing: 0.08em;
  text-transform: uppercase;
}

.topbar-actions {
  display: flex;
  align-items: center;
  justify-content: flex-end;
  gap: 12px;
  min-width: 0;
}

.text-link,
.text-button {
  border: 0;
  background: transparent;
  color: var(--red);
  cursor: pointer;
  font-size: 0.92rem;
  text-decoration: none;
}

.text-link:hover,
.text-button:hover {
  text-decoration: underline;
}

.game-layout {
  display: grid;
  grid-template-columns: minmax(0, 1fr) minmax(310px, 392px);
  gap: 22px;
  align-items: start;
}

.board-panel {
  position: sticky;
  top: 16px;
  min-width: 0;
}

.side-panel {
  display: grid;
  gap: 12px;
  min-width: 0;
}

.board-card,
.card {
  border: 1px solid var(--line);
  border-radius: 8px;
  background: var(--surface);
  box-shadow: var(--shadow);
}

.board-card {
  padding: 14px;
}

.card {
  padding: 16px;
}

.board-meta,
.card-title-row,
.button-row,
.board-tools {
  display: flex;
  align-items: center;
  gap: 10px;
}

.board-meta,
.card-title-row {
  justify-content: space-between;
}

.board-meta > div,
.card-title-row > h2 {
  min-width: 0;
}

.meta-label,
.card-kicker {
  margin: 0;
  color: var(--ink-muted);
  font-size: 0.76rem;
  letter-spacing: 0.12em;
  text-transform: uppercase;
}

.turn-banner {
  margin: 4px 0 0;
  font-size: 1.14rem;
  font-weight: 700;
  line-height: 1.25;
}

.turn-pill {
  min-height: 34px;
  display: inline-flex;
  align-items: center;
  max-width: 100%;
  padding: 6px 10px;
  border: 1px solid var(--line);
  border-radius: 8px;
  background: var(--surface);
  color: var(--ink-soft);
  font-size: 0.86rem;
  white-space: nowrap;
}

.turn-pill.ready {
  border-color: rgba(47, 106, 77, 0.28);
  color: var(--green);
}

.turn-pill.thinking {
  border-color: rgba(177, 107, 44, 0.32);
  color: var(--amber);
}

.board-tools {
  flex: 0 0 auto;
}

.tool-button {
  width: 36px;
  height: 36px;
  border: 1px solid var(--line);
  border-radius: 8px;
  background: var(--surface-soft);
  color: var(--ink);
  cursor: pointer;
  font-size: 1rem;
  line-height: 1;
}

.tool-button:hover:not(:disabled) {
  border-color: var(--line-strong);
  background: #fffaf0;
}

.board-shell {
  margin-top: 12px;
  padding: 14px;
  border: 1px solid #b59464;
  border-radius: 8px;
  background:
    linear-gradient(90deg, rgba(255, 255, 255, 0.18), rgba(255, 255, 255, 0) 24%),
    #c7904d;
  box-shadow:
    inset 0 0 0 4px rgba(112, 73, 35, 0.12),
    inset 0 -18px 38px rgba(72, 45, 18, 0.13);
  container-type: inline-size;
}

.board {
  position: relative;
  width: min(100%, 760px);
  margin: 0 auto;
  aspect-ratio: 8 / 9;
  padding: var(--board-piece-pad);
  border-radius: 8px;
  background: #e8c48b;
  box-shadow:
    inset 0 0 0 1px rgba(101, 67, 31, 0.2),
    inset 0 18px 34px rgba(255, 255, 255, 0.18),
    inset 0 -18px 34px rgba(79, 47, 20, 0.08);
  container-type: inline-size;
}

.board-svg {
  position: absolute;
  inset: var(--board-piece-pad);
  width: calc(100% - var(--board-grid-inset));
  height: calc(100% - var(--board-grid-inset));
  overflow: visible;
  pointer-events: none;
}

.board-overlay {
  position: absolute;
  inset: var(--board-piece-pad);
}

.board-border-outer {
  fill: none;
  stroke: rgba(101, 67, 31, 0.28);
  stroke-width: 12;
}

.board-border-inner {
  fill: none;
  stroke: var(--board-line);
  stroke-width: 3.5;
}

.board-line {
  fill: none;
  stroke: var(--board-line);
  stroke-width: 2.65;
  stroke-linecap: square;
}

.board-line.palace {
  stroke-width: 2.2;
}

.board-river {
  fill: rgba(255, 250, 239, 0.34);
  stroke: none;
}

.board-marker {
  fill: none;
  stroke: var(--board-marker);
  stroke-width: 2.6;
  stroke-linecap: square;
}

.river-text {
  fill: rgba(101, 67, 31, 0.46);
  font-family: var(--piece-font);
  font-size: 56px;
  letter-spacing: 24px;
}

.board-spot {
  appearance: none;
  -webkit-appearance: none;
  position: absolute;
  z-index: 1;
  width: var(--piece-hit);
  height: var(--piece-hit);
  margin: 0;
  padding: 0;
  border: 0;
  border-radius: 0;
  background: transparent;
  box-shadow: none;
  transform: translate(-50%, -50%);
  cursor: pointer;
}

.board-spot::before,
.board-spot::after {
  content: "";
  position: absolute;
  left: 50%;
  top: 50%;
  transform: translate(-50%, -50%);
  pointer-events: none;
}

.board-spot.selected::before,
.board-spot.check-king::before,
.board-spot.target.occupied::after {
  width: calc(var(--piece-size) + 10px);
  height: calc(var(--piece-size) + 10px);
  border-radius: 50%;
}

.board-spot.selected::before {
  background: var(--board-highlight);
  box-shadow: 0 0 0 2px rgba(169, 50, 38, 0.42);
}

.board-spot.last-move::before {
  --last-move-mark: rgba(34, 109, 190, 0.56);

  width: var(--piece-size);
  height: var(--piece-size);
  border-radius: 4px;
  background:
    linear-gradient(var(--last-move-mark), var(--last-move-mark)) left top / 12px 1.25px no-repeat,
    linear-gradient(var(--last-move-mark), var(--last-move-mark)) left top / 1.25px 12px no-repeat,
    linear-gradient(var(--last-move-mark), var(--last-move-mark)) right top / 12px 1.25px no-repeat,
    linear-gradient(var(--last-move-mark), var(--last-move-mark)) right top / 1.25px 12px no-repeat,
    linear-gradient(var(--last-move-mark), var(--last-move-mark)) left bottom / 12px 1.25px no-repeat,
    linear-gradient(var(--last-move-mark), var(--last-move-mark)) left bottom / 1.25px 12px no-repeat,
    linear-gradient(var(--last-move-mark), var(--last-move-mark)) right bottom / 12px 1.25px no-repeat,
    linear-gradient(var(--last-move-mark), var(--last-move-mark)) right bottom / 1.25px 12px no-repeat;
  opacity: 0.9;
}

.board-spot.check-king::before {
  background: rgba(169, 50, 38, 0.16);
  box-shadow: 0 0 0 2px rgba(169, 50, 38, 0.28);
}

.board-spot.target:not(.occupied)::after {
  width: 13px;
  height: 13px;
  border-radius: 50%;
  background: rgba(169, 50, 38, 0.58);
  box-shadow: 0 0 0 6px rgba(169, 50, 38, 0.1);
}

.board-spot.target.occupied::after {
  box-shadow: 0 0 0 2px rgba(169, 50, 38, 0.56);
}

.board-spot.selectable:hover .piece,
.board-spot.selected .piece {
  transform: translate(-50%, -50%) scale(1.035);
  box-shadow:
    inset 0 2px 4px rgba(255, 255, 255, 0.62),
    inset 0 0 0 1px rgba(255, 255, 255, 0.28),
    0 8px 18px rgba(39, 27, 17, 0.24),
    0 3px 6px rgba(39, 27, 17, 0.18);
}

.board-spot:disabled {
  cursor: default;
}

.piece {
  position: absolute;
  left: 50%;
  top: 50%;
  width: var(--piece-size);
  height: var(--piece-size);
  display: grid;
  place-items: center;
  border: var(--piece-border-width) solid rgba(63, 40, 18, 0.66);
  border-radius: 50%;
  overflow: hidden;
  background:
    radial-gradient(
      circle at 32% 24%,
      #fffef8 0%,
      #fff9e8 18%,
      #f8e4bc 42%,
      #e4c285 72%,
      #a8753e 100%
    );
  box-shadow:
    inset 0 2px 4px rgba(255, 255, 255, 0.58),
    inset 0 0 0 1px rgba(255, 255, 255, 0.24),
    0 6px 12px rgba(39, 27, 17, 0.18),
    0 2px 4px rgba(39, 27, 17, 0.14);
  font-family: var(--piece-font);
  font-size: var(--piece-font-size);
  font-weight: 600;
  line-height: 1;
  transform: translate(-50%, -50%);
  transition:
    transform 140ms ease,
    box-shadow 140ms ease;
}

.piece::before {
  content: "";
  position: absolute;
  border-radius: 50%;
  pointer-events: none;
}

.piece::before {
  inset: 3.5%;
  border: var(--piece-groove-width) solid rgba(84, 52, 23, 0.4);
  box-shadow:
    0 1px 1px rgba(255, 255, 255, 0.34),
    0 -1px 2px rgba(70, 43, 18, 0.2);
}

.piece.red {
  color: var(--red);
}

.piece.black {
  color: var(--black-piece);
}

.card h2 {
  margin: 0;
  font-size: 1.02rem;
  line-height: 1.2;
}

.primary-card {
  border-color: rgba(169, 50, 38, 0.2);
}

.controls-grid {
  display: grid;
  grid-template-columns: repeat(2, minmax(0, 1fr));
  gap: 12px;
  margin-top: 14px;
}

.controls-grid .field:first-child,
.controls-grid .full-field {
  grid-column: 1 / -1;
}

.field {
  display: grid;
  gap: 6px;
  color: var(--ink-soft);
  font-size: 0.9rem;
}

.field-block {
  margin-top: 12px;
}

.field span {
  line-height: 1.2;
}

.field input,
.field select,
.field textarea {
  width: 100%;
  min-height: 38px;
  border: 1px solid var(--line);
  border-radius: 6px;
  background: #ffffff;
  color: var(--ink);
  padding: 8px 10px;
  resize: vertical;
}

.field textarea {
  min-height: 84px;
  line-height: 1.45;
}

.button-row {
  margin-top: 14px;
  flex-wrap: wrap;
}

.control-button {
  min-height: 38px;
  border: 1px solid var(--red-strong);
  border-radius: 6px;
  padding: 8px 12px;
  background: var(--red);
  color: #fffdf8;
  cursor: pointer;
  font-size: 0.92rem;
  font-weight: 600;
  line-height: 1.15;
}

.control-button.secondary {
  border-color: var(--line);
  background: var(--surface-soft);
  color: var(--ink);
  font-weight: 500;
}

.control-button:hover:not(:disabled) {
  background: var(--red-strong);
}

.control-button.secondary:hover:not(:disabled) {
  border-color: var(--line-strong);
  background: #fffaf0;
}

.control-button:disabled,
.tool-button:disabled,
input:disabled,
select:disabled,
textarea:disabled {
  cursor: not-allowed;
  opacity: 0.55;
}

.stats-list {
  display: grid;
  gap: 8px;
  margin: 12px 0 0;
}

.stats-list div {
  display: grid;
  gap: 3px;
  padding: 9px 10px;
  border: 1px solid var(--line);
  border-radius: 6px;
  background: var(--surface-soft);
}

.compact-stats {
  grid-template-columns: repeat(2, minmax(0, 1fr));
}

.summary-card .compact-stats div:last-child,
.compact-stats .wide-stat {
  grid-column: 1 / -1;
}

.stats-list dt {
  color: var(--ink-muted);
  font-size: 0.75rem;
  letter-spacing: 0.08em;
  text-transform: uppercase;
}

.stats-list dd {
  min-width: 0;
  margin: 0;
  font-size: 0.96rem;
  line-height: 1.35;
  overflow-wrap: anywhere;
}

.fen-output {
  margin: 12px 0 0;
  color: var(--ink-soft);
  font-size: 0.82rem;
  line-height: 1.45;
  overflow-wrap: anywhere;
}

.status-log {
  max-height: 220px;
  min-height: 120px;
  margin: 12px 0 0;
  padding: 10px;
  overflow: auto;
  border-radius: 6px;
  background: #202622;
  color: #f5ead8;
  font-size: 0.82rem;
  line-height: 1.45;
  white-space: pre-wrap;
  word-break: break-word;
}

.settings-toggle-row {
  display: flex;
  justify-content: flex-start;
  padding: 2px 0;
}

.settings-drawer {
  display: grid;
  gap: 12px;
  opacity: 1;
}

.settings-drawer.collapsed {
  display: none;
  opacity: 0;
  pointer-events: none;
}

.viewer-side-panel {
  align-content: start;
}

.viewer-page .turn-banner {
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.viewer-page .turn-pill {
  overflow: hidden;
  text-overflow: ellipsis;
}

.viewer-page .board-shell {
  padding: 10px;
}

.replay-control-panel {
  display: grid;
  grid-template-columns: auto minmax(280px, 1fr) minmax(170px, 210px);
  gap: 10px;
  align-items: end;
  margin-top: 12px;
  padding-top: 12px;
  border-top: 1px solid var(--line);
}

.replay-control-panel h2 {
  margin: 0;
  font-size: 0.96rem;
}

.replay-control-panel .button-row,
.replay-control-panel .locate-row {
  margin-top: 0;
}

.replay-buttons {
  display: grid;
  grid-template-columns: repeat(4, minmax(0, 1fr));
  gap: 8px;
}

.replay-buttons .control-button {
  width: 100%;
  padding: 8px 6px;
  white-space: nowrap;
}

.locate-row {
  display: grid;
  grid-template-columns: minmax(0, 1fr) auto;
  gap: 8px;
  align-items: end;
}

.replay-control-panel .field {
  gap: 4px;
  font-size: 0.8rem;
}

.replay-control-panel .field input {
  min-height: 38px;
}

.move-list {
  display: grid;
  gap: 6px;
  max-height: min(380px, 45vh);
  margin-top: 12px;
  padding-right: 4px;
  overflow: auto;
}

.move-row {
  display: grid;
  grid-template-columns: 2.8rem minmax(0, 1fr) minmax(0, 1fr);
  gap: 6px;
  align-items: center;
}

.move-number {
  color: var(--ink-muted);
  font-variant-numeric: tabular-nums;
  text-align: right;
}

.move-button {
  min-width: 0;
  border: 1px solid var(--line);
  border-radius: 6px;
  background: var(--surface-soft);
  color: var(--ink);
  padding: 7px 8px;
  cursor: pointer;
  font-size: 0.86rem;
  line-height: 1.2;
  overflow: hidden;
  text-align: left;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.move-button:hover,
.move-button.active-move {
  border-color: rgba(169, 50, 38, 0.42);
  background: rgba(169, 50, 38, 0.1);
}

.empty-state {
  margin: 0;
  color: var(--ink-soft);
}

@media (min-width: 1180px) {
  .play-page .board {
    width: min(100%, 720px, calc((100vh - 170px) * 0.86));
  }

  .viewer-page .board {
    --piece-size: var(--board-piece-size, min(11cqw, 98px));
    --piece-hit: calc(var(--piece-size) + 12px);
    width: min(100%, 600px, calc((100vh - 285px) * 8 / 9));
  }
}

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

  .board-panel {
    position: static;
  }

  .side-panel {
    grid-template-columns: repeat(2, minmax(0, 1fr));
  }

  .settings-toggle-row,
  .settings-drawer,
  .moves-card,
  .logs-card {
    grid-column: 1 / -1;
  }
}

@media (max-width: 780px) {
  :root {
    --piece-hit: calc(var(--piece-size) + 10px);
  }

  .app-shell {
    width: min(100% - 18px, 780px);
    padding: 10px 0 24px;
  }

  .topbar {
    align-items: flex-start;
    flex-direction: column;
    gap: 10px;
  }

  .topbar-actions {
    width: 100%;
    justify-content: space-between;
  }

  .turn-pill {
    min-height: 32px;
    font-size: 0.8rem;
  }

  .board-card,
  .card {
    padding: 12px;
  }

  .board-meta {
    align-items: flex-start;
  }

  .turn-banner {
    font-size: 1rem;
  }

  .board-shell {
    padding: 9px;
  }

  .side-panel {
    grid-template-columns: 1fr;
  }

  .controls-grid,
  .compact-stats {
    grid-template-columns: 1fr;
  }

  .controls-grid .field:first-child,
  .controls-grid .full-field,
  .summary-card .compact-stats div:last-child {
    grid-column: auto;
  }

  .button-row {
    display: grid;
    grid-template-columns: repeat(2, minmax(0, 1fr));
  }

  .control-button {
    width: 100%;
  }

  .river-text {
    font-size: 42px;
    letter-spacing: 18px;
  }

  .replay-control-panel {
    grid-template-columns: 1fr;
  }

  .replay-buttons {
    grid-template-columns: repeat(4, minmax(0, 1fr));
  }

  .replay-buttons .control-button {
    min-height: 36px;
    padding: 7px 4px;
    font-size: 0.82rem;
  }

  .move-row {
    grid-template-columns: 2.3rem minmax(0, 1fr) minmax(0, 1fr);
  }
}

@media (max-width: 430px) {
  .brand-title {
    font-size: 1rem;
  }

  .topbar-actions {
    align-items: stretch;
    flex-direction: column;
    gap: 8px;
  }

  .turn-pill {
    width: 100%;
    justify-content: center;
  }

  .button-row {
    grid-template-columns: 1fr;
  }

  .replay-buttons {
    grid-template-columns: repeat(2, minmax(0, 1fr));
  }
}
