:root {
  --app-bg: #17191d;
  --panel-bg: #23262c;
  --panel-bg-2: #2b2f36;
  --panel-border: #3a3f47;
  --text: #e8e6e1;
  --text-dim: #a2a7b0;
  --accent: #c8913f;
  --accent-strong: #e0a94f;
  --board-bg: #e7c994;
  --board-bg-2: #dfbd84;
  --line: #6b4a2f;
  --river-text: #7a5636;
  --red: #b3261e;
  --red-dark: #7d1712;
  --black: #23272e;
  --black-dark: #0f1115;
  --piece-face: #f6e7c8;
  --piece-edge: #b9925b;
  --highlight: #4caf50;
  --last-move: #d9a441;
}

body[data-theme="jade"] {
  --board-bg: #cfdfc9;
  --board-bg-2: #bcd2b4;
  --line: #335b45;
  --river-text: #3d6b51;
  --piece-face: #f3f0df;
  --piece-edge: #8ba88c;
  --app-bg: #101a16;
  --panel-bg: #1a2722;
  --panel-bg-2: #21332c;
  --panel-border: #345046;
  --accent: #6fbf8b;
}

body[data-theme="night"] {
  --app-bg: #0c1016;
  --panel-bg: #151c26;
  --panel-bg-2: #1c2531;
  --panel-border: #2c3949;
  --board-bg: #2c3946;
  --board-bg-2: #27333f;
  --line: #93a6b5;
  --river-text: #9fb2c0;
  --red: #ff6b5e;
  --red-dark: #a8322a;
  --black: #b9c6d2;
  --black-dark: #6e7f8e;
  --piece-face: #e7e3d7;
  --piece-edge: #7c8893;
  --text: #dfe6ee;
  --text-dim: #93a4b5;
  --accent: #74b3e8;
}

body[data-theme="paper"] {
  --app-bg: #ded8c8;
  --panel-bg: #f3eee0;
  --panel-bg-2: #ece5d2;
  --panel-border: #cfc4a8;
  --text: #3a3327;
  --text-dim: #776c58;
  --board-bg: #f6efd9;
  --board-bg-2: #ece1c3;
  --line: #5f5139;
  --river-text: #6d5c40;
  --piece-face: #fffaf0;
  --piece-edge: #c2a66d;
  --accent: #a8772a;
}

* { box-sizing: border-box; }

html, body { height: 100%; }

body {
  margin: 0;
  background: var(--app-bg);
  color: var(--text);
  font-family: "Inter", "PingFang SC", "Microsoft YaHei", system-ui, sans-serif;
  font-size: 14px;
  overflow: hidden;
}

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

.app {
  height: 100vh;
  max-width: 1480px;
  margin: 0 auto;
  display: flex;
  flex-direction: column;
  background: var(--app-bg);
}

.topbar {
  height: 58px;
  flex: none;
  display: flex;
  align-items: center;
  gap: 24px;
  padding: 0 18px;
  background: var(--panel-bg);
  border-bottom: 1px solid var(--panel-border);
}

.brand { display: flex; align-items: center; gap: 9px; white-space: nowrap; }
.brand-mark {
  width: 32px; height: 32px; border-radius: 8px;
  display: grid; place-items: center;
  background: var(--red); color: #fff5e0;
  font-family: "STKaiti", "KaiTi", serif; font-size: 21px; font-weight: 700;
  box-shadow: 0 2px 6px rgba(0,0,0,.35);
}
.brand-name { font-weight: 700; font-size: 17px; letter-spacing: .3px; }
.brand-sub { color: var(--text-dim); font-size: 12px; }

.topmenu { display: flex; gap: 4px; flex: 1; }
.topmenu button {
  background: transparent; border: 0; color: var(--text-dim);
  padding: 8px 12px; border-radius: 6px; cursor: pointer;
}
.topmenu button:hover, .topmenu button:focus-visible { background: var(--panel-bg-2); color: var(--text); outline: none; }

.top-status { display: flex; gap: 6px; }
.badge {
  font-size: 12px; padding: 4px 9px; border-radius: 999px;
  background: rgba(200,145,63,.16); color: var(--accent-strong);
  border: 1px solid rgba(200,145,63,.35);
}
.badge-alt { background: var(--panel-bg-2); color: var(--text-dim); border-color: var(--panel-border); }

.workspace {
  flex: 1;
  min-height: 0;
  display: grid;
  grid-template-columns: 220px minmax(0, 1fr) 280px;
  gap: 14px;
  padding: 14px;
}

.side-panel {
  min-height: 0;
  display: flex;
  flex-direction: column;
  gap: 10px;
}

.player-card {
  display: flex; align-items: center; gap: 10px;
  background: var(--panel-bg); border: 1px solid var(--panel-border);
  border-radius: 10px; padding: 10px;
}
.player-card.active { border-color: var(--accent); box-shadow: 0 0 0 1px rgba(200,145,63,.25); }
.avatar {
  width: 40px; height: 40px; flex: none; border-radius: 50%;
  display: grid; place-items: center;
  font-family: "STKaiti", "KaiTi", serif; font-size: 23px; font-weight: 700;
  color: var(--piece-face);
}
.avatar-black { background: linear-gradient(145deg, #3a4048, var(--black-dark)); }
.avatar-red { background: linear-gradient(145deg, #d4554a, var(--red-dark)); }
.player-meta { min-width: 0; flex: 1; }
.player-name { font-weight: 600; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.player-detail { font-size: 11px; color: var(--text-dim); margin-top: 2px; }
.player-clock { font-variant-numeric: tabular-nums; font-size: 13px; color: var(--text-dim); }

.box-title {
  text-transform: uppercase; letter-spacing: .08em;
  font-size: 11px; color: var(--text-dim); font-weight: 600;
}
.captured-box, .info-card, .records-box {
  background: var(--panel-bg); border: 1px solid var(--panel-border);
  border-radius: 10px; padding: 10px;
}
.captured-pieces {
  margin-top: 8px; min-height: 32px;
  display: flex; flex-wrap: wrap; gap: 4px;
}
.cap-piece {
  width: 25px; height: 25px; border-radius: 50%;
  display: grid; place-items: center;
  font-family: "STKaiti", "KaiTi", serif; font-size: 15px; font-weight: 700;
  background: var(--piece-face); border: 1px solid var(--piece-edge);
  color: var(--black);
}
.cap-piece.red { color: var(--red); }
.panel-spacer { flex: 1; }

.info-list { margin: 8px 0 0; display: grid; gap: 6px; }
.info-list > div { display: flex; justify-content: space-between; gap: 8px; }
.info-list dt { color: var(--text-dim); }
.info-list dd { margin: 0; font-variant-numeric: tabular-nums; }

.records-box { flex: 1; min-height: 140px; display: flex; flex-direction: column; }
.records-head { display: flex; align-items: center; justify-content: space-between; }
.records-tools { display: flex; gap: 2px; }
.icon-btn {
  background: transparent; border: 0; color: var(--text-dim);
  cursor: pointer; padding: 2px 5px; border-radius: 4px; font-size: 13px;
}
.icon-btn:hover { background: var(--panel-bg-2); color: var(--text); }
.move-list {
  margin-top: 8px; overflow-y: auto; flex: 1;
  font-variant-numeric: tabular-nums;
}
.move-row {
  display: grid; grid-template-columns: 34px 1fr 1fr;
  gap: 4px; padding: 3px 4px; border-radius: 5px; font-size: 13px;
}
.move-row:nth-child(odd) { background: rgba(255,255,255,.025); }
.move-no { color: var(--text-dim); }
.move-cell { cursor: pointer; padding: 2px 4px; border-radius: 4px; white-space: nowrap; }
.move-cell:hover { background: var(--panel-bg-2); }
.move-cell.current { background: var(--accent); color: #1a1207; font-weight: 600; }
.move-cell.future { opacity: .45; }
.move-empty { color: var(--text-dim); text-align: center; padding: 20px 0; font-size: 12px; }

.board-section { min-width: 0; min-height: 0; display: flex; align-items: center; justify-content: center; }
.board-shell { width: min(100%, 700px); height: 100%; display: flex; flex-direction: column; justify-content: center; gap: 8px; }
.board-caption { text-align: center; color: var(--text-dim); font-size: 12px; letter-spacing: .1em; }

.board-frame {
  position: relative;
  width: 100%;
  aspect-ratio: 720 / 800;
  max-height: calc(100% - 52px);
  margin: 0 auto;
  border-radius: 10px;
  background:
    repeating-linear-gradient(92deg, rgba(255,255,255,.035) 0 2px, transparent 2px 7px),
    var(--board-bg);
  box-shadow: 0 14px 40px rgba(0,0,0,.45), inset 0 0 60px rgba(90,55,20,.18);
  user-select: none;
  -webkit-user-select: none;
}
.board { position: absolute; inset: 0; container-type: inline-size; }
.board svg { position: absolute; inset: 0; width: 100%; height: 100%; }
.board-lines { stroke: var(--line); stroke-width: 1.6; fill: none; stroke-linecap: round; }
.board-border { stroke-width: 2.6; }
.board-star { stroke-width: 1.4; }
.board-river { fill: var(--river-text); font-family: "STKaiti", "KaiTi", serif; font-size: 30px; letter-spacing: 12px; opacity: .85; }
.board-coord { fill: var(--river-text); font-family: system-ui, sans-serif; font-size: 13px; opacity: .65; }
.hide-coords .board-coord { display: none; }

.sq {
  position: absolute; width: 9.6%; aspect-ratio: 1;
  transform: translate(-50%, -50%); border-radius: 8px;
  z-index: 2; cursor: pointer;
}
.sq.last::before {
  content: ""; position: absolute; inset: 12%;
  border: 2px dashed var(--last-move); border-radius: 6px; opacity: .9;
}
.sq.legal::after {
  content: ""; position: absolute; left: 50%; top: 50%;
  width: 22%; height: 22%; transform: translate(-50%, -50%);
  background: rgba(46,139,87,.6); border-radius: 50%;
}
.sq.capture::after {
  content: ""; position: absolute; inset: 6%;
  border: 3px solid rgba(196,72,52,.75); border-radius: 50%;
}
.sq.hint::before {
  content: ""; position: absolute; inset: 8%;
  border: 3px solid #f0c04a; border-radius: 50%;
  animation: hintPulse 1s ease-in-out infinite;
}
@keyframes hintPulse { 50% { opacity: .35; transform: scale(1.08); } }

.piece {
  position: absolute; z-index: 3;
  width: 9.4%; aspect-ratio: 1;
  transform: translate(-50%, -50%);
  border-radius: 50%;
  display: grid; place-items: center;
  font-family: "STKaiti", "KaiTi", "Songti SC", serif;
  font-size: 4.3cqw; font-weight: 700; line-height: 1;
  cursor: pointer;
  background: radial-gradient(circle at 35% 28%, #fffaf0 0%, var(--piece-face) 55%, #d9c39a 100%);
  border: 2px solid var(--piece-edge);
  box-shadow: 0 3px 5px rgba(0,0,0,.4), inset 0 -2px 4px rgba(120,80,30,.22), inset 0 2px 3px rgba(255,255,255,.75);
  transition: transform .12s ease, box-shadow .12s ease;
}
.piece::after {
  content: ""; position: absolute; inset: 5%;
  border: 1px solid var(--piece-edge); border-radius: 50%; opacity: .6;
}
.piece.red { color: var(--red); }
.piece.black { color: var(--black); }
.piece:hover { transform: translate(-50%, -54%) scale(1.04); }
.piece.selected {
  transform: translate(-50%, -56%) scale(1.1);
  box-shadow: 0 6px 14px rgba(0,0,0,.5), 0 0 0 3px var(--accent), inset 0 -2px 4px rgba(120,80,30,.22);
}
.piece.in-check { box-shadow: 0 0 0 3px #e5484d, 0 0 18px rgba(229,72,77,.7); }
.piece.moving { animation: piecePop .18s ease; }
@keyframes piecePop { 50% { transform: translate(-50%, -60%) scale(1.12); } }

.statusbar {
  display: flex; align-items: center; gap: 8px;
  background: var(--panel-bg); border: 1px solid var(--panel-border);
  border-radius: 8px; padding: 8px 12px; font-size: 13px;
}
.status-dot { width: 9px; height: 9px; border-radius: 50%; background: var(--red); box-shadow: 0 0 8px var(--red); }
.status-dot.black { background: var(--black); box-shadow: 0 0 8px var(--black); }
.status-hint { margin-left: auto; color: var(--text-dim); font-size: 12px; }

.controlbar {
  flex: none; min-height: 62px;
  display: flex; align-items: center; justify-content: space-between; gap: 12px;
  padding: 10px 14px;
  background: var(--panel-bg); border-top: 1px solid var(--panel-border);
}
.control-group { display: flex; align-items: center; gap: 8px; flex-wrap: wrap; }
.control-center { justify-content: center; }
.control-right { justify-content: flex-end; }

.btn {
  background: var(--panel-bg-2); color: var(--text);
  border: 1px solid var(--panel-border);
  border-radius: 7px; padding: 8px 14px; cursor: pointer;
  transition: background .12s, border-color .12s, transform .08s;
}
.btn:hover { background: #3a4049; border-color: #555c66; }
.btn:active { transform: translateY(1px); }
.btn:disabled { opacity: .45; cursor: not-allowed; }
.btn-primary { background: var(--accent); color: #221705; border-color: var(--accent); font-weight: 600; }
.btn-primary:hover { background: var(--accent-strong); border-color: var(--accent-strong); }
.btn-ghost { background: transparent; border-color: transparent; color: var(--text-dim); }
.btn-ghost:hover { color: var(--text); border-color: var(--panel-border); background: var(--panel-bg-2); }
.select-label { color: var(--text-dim); font-size: 12px; display: flex; align-items: center; gap: 5px; }
.select-label select {
  background: var(--panel-bg-2); color: var(--text);
  border: 1px solid var(--panel-border); border-radius: 6px;
  padding: 6px 8px; cursor: pointer;
}

.result-overlay {
  position: absolute; inset: 0; z-index: 20;
  display: grid; place-items: center;
  background: rgba(10,10,12,.6); backdrop-filter: blur(2px);
  border-radius: 10px;
}
.result-card {
  background: var(--panel-bg); border: 1px solid var(--accent);
  border-radius: 12px; padding: 26px 32px; text-align: center;
  box-shadow: 0 18px 50px rgba(0,0,0,.5);
}
.result-title { font-size: 26px; font-weight: 700; color: var(--accent-strong); }
.result-desc { color: var(--text-dim); margin: 10px 0 20px; }
.result-actions { display: flex; gap: 10px; justify-content: center; }

.modal { position: fixed; inset: 0; z-index: 50; display: grid; place-items: center; }
.modal-backdrop { position: absolute; inset: 0; background: rgba(0,0,0,.65); }
.modal-card {
  position: relative; z-index: 1;
  width: min(92vw, 480px); max-height: 86vh; overflow-y: auto;
  background: var(--panel-bg); border: 1px solid var(--panel-border);
  border-radius: 12px; padding: 22px;
  box-shadow: 0 24px 70px rgba(0,0,0,.55);
}
.modal-title { margin: 0 0 6px; font-size: 19px; }
.modal-sub { color: var(--text-dim); font-size: 12px; margin-bottom: 18px; }
.field { margin-bottom: 16px; }
.field > label { display: block; font-size: 12px; color: var(--text-dim); margin-bottom: 6px; }
.seg { display: flex; gap: 6px; flex-wrap: wrap; }
.seg button {
  flex: 1; min-width: 80px;
  background: var(--panel-bg-2); color: var(--text-dim);
  border: 1px solid var(--panel-border); border-radius: 7px; padding: 9px 8px; cursor: pointer;
}
.seg button.active { background: var(--accent); color: #221705; border-color: var(--accent); font-weight: 600; }
.modal-actions { display: flex; justify-content: flex-end; gap: 8px; margin-top: 20px; }
textarea {
  width: 100%; min-height: 110px; resize: vertical;
  background: var(--app-bg); color: var(--text);
  border: 1px solid var(--panel-border); border-radius: 8px; padding: 10px;
  font-family: ui-monospace, "SFMono-Regular", Consolas, monospace; font-size: 12px;
}
.help-text { line-height: 1.8; color: var(--text-dim); }
.help-text b { color: var(--text); }
.help-text code { background: var(--panel-bg-2); padding: 1px 5px; border-radius: 4px; }

.toast {
  position: fixed; left: 50%; bottom: 84px; transform: translateX(-50%);
  z-index: 80; background: #2f333a; color: #fff;
  border: 1px solid #4b515b; border-radius: 8px;
  padding: 9px 16px; font-size: 13px;
  box-shadow: 0 8px 24px rgba(0,0,0,.4);
}
.hidden { display: none !important; }

@media (max-width: 1100px) {
  .workspace { grid-template-columns: 180px minmax(0,1fr) 230px; gap: 8px; padding: 8px; }
  .controlbar { flex-wrap: wrap; justify-content: center; }
  .control-center, .control-right { justify-content: center; }
}
@media (max-width: 800px) {
  body { overflow: auto; }
  .app { height: auto; min-height: 100vh; }
  .workspace { grid-template-columns: 1fr; }
  .side-left, .side-right { display: none; }
  .topmenu button { padding: 8px 7px; }
  .brand-sub, .top-status { display: none; }
  .board-shell { width: min(100%, 560px); }
  .controlbar { position: sticky; bottom: 0; }
}
