:root {
  color-scheme: light;
  --canvas: #ffffff;
  --canvas-subtle: #f6f8fa;
  --overlay: #ffffff;
  --border: #d1d9e0;
  --border-muted: #e4e8ec;
  --fg: #1f2328;
  --fg-muted: #59636e;
  --accent: #0969da;
  --accent-emphasis: #0969da;
  --accent-subtle: #ddf4ff;
  --btn-bg: #f6f8fa;
  --btn-hover: #eef1f4;
  --btn-active: #e6eaef;
  --edge: #c3ccd5;
  --shadow-sm: 0 1px 0 rgba(31, 35, 40, 0.04);
  --shadow-overlay: 0 1px 3px rgba(31, 35, 40, 0.12), 0 8px 24px rgba(66, 74, 83, 0.12);
  --eco-a: #8250df;
  --eco-b: #bc4c00;
  --eco-c: #cf222e;
  --eco-d: #1a7f37;
  --eco-e: #1b7c83;
  --eco-none: #59636e;
  --result-white: #ffffff;
  --result-draw: #afb8c1;
  --result-black: #1f2328;
  --square-light: #dee3e6;
  --square-dark: #8ca2ad;
  --square-last: rgba(9, 105, 218, 0.24);
  --square-pick: rgba(9, 105, 218, 0.42);
  --square-hint: rgba(31, 35, 40, 0.26);
  --sans: -apple-system, BlinkMacSystemFont, "Segoe UI", "Noto Sans", Helvetica, Arial, sans-serif;
  --radius: 6px;
}

:root[data-theme="dark"] {
  color-scheme: dark;
  --canvas: #0d1117;
  --canvas-subtle: #151b23;
  --overlay: #151b23;
  --border: #3d444d;
  --border-muted: #2a3039;
  --fg: #f0f6fc;
  --fg-muted: #9198a1;
  --accent: #4493f8;
  --accent-emphasis: #1f6feb;
  --accent-subtle: rgba(56, 139, 253, 0.15);
  --btn-bg: #212830;
  --btn-hover: #262c36;
  --btn-active: #2a313c;
  --edge: #343b44;
  --shadow-sm: 0 0 transparent;
  --shadow-overlay: 0 1px 3px rgba(1, 4, 9, 0.5), 0 8px 24px rgba(1, 4, 9, 0.6);
  --eco-a: #a371f7;
  --eco-b: #db6d28;
  --eco-c: #f85149;
  --eco-d: #3fb950;
  --eco-e: #39c5cf;
  --eco-none: #9198a1;
  --result-white: #f0f6fc;
  --result-draw: #656c76;
  --result-black: #010409;
  --square-light: #c4ccd3;
  --square-dark: #7d939e;
  --square-last: rgba(56, 139, 253, 0.36);
  --square-pick: rgba(56, 139, 253, 0.55);
  --square-hint: rgba(1, 4, 9, 0.32);
}

* { box-sizing: border-box; }
[hidden] { display: none !important; }

html, body { height: 100%; }

body {
  margin: 0;
  display: flex;
  flex-direction: column;
  overflow: hidden;
  background: var(--canvas);
  color: var(--fg);
  font: 14px/1.5 var(--sans);
  font-variant-numeric: tabular-nums;
}

h1, h2 { margin: 0; font-size: 14px; font-weight: 600; }

button { font: inherit; color: inherit; }

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

.topbar {
  flex: none;
  display: flex;
  align-items: center;
  gap: 12px;
  min-height: 56px;
  padding: 0 16px;
  background: var(--canvas-subtle);
  border-bottom: 1px solid var(--border);
}

.brand { display: flex; align-items: center; gap: 8px; color: var(--fg); font-size: 16px; font-weight: 600; text-decoration: none; }
.mark { width: 20px; height: 20px; fill: var(--fg); stroke: var(--fg); stroke-width: 1.25; }
.summary { color: var(--fg-muted); font-size: 13px; }

.search { position: relative; margin-left: auto; width: min(320px, 40vw); }
.actions { display: flex; align-items: center; gap: 8px; }
.btn-icon svg.filled { fill: currentColor; stroke: none; }

.dialog {
  width: min(520px, calc(100vw - 32px));
  max-height: calc(100vh - 64px);
  padding: 0;
  overflow: auto;
  background: var(--overlay);
  color: var(--fg);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  box-shadow: var(--shadow-overlay);
}
.dialog::backdrop { background: rgba(1, 4, 9, 0.45); }
.dialog .box-head .btn-icon { width: 28px; height: 28px; margin-right: -8px; border-color: transparent; background: none; box-shadow: none; }
.dialog .box-head .btn-icon:hover { background: var(--btn-hover); }
.prose p { margin: 0 0 12px; }
.prose h3 { margin: 16px 0 8px; font-size: 14px; font-weight: 600; }
.prose ul { margin: 0; padding: 0; list-style: none; }
.prose li { padding: 8px 0; border-top: 1px solid var(--border-muted); }
.prose li:first-child { border-top: 0; }
.prose li span { display: block; color: var(--fg-muted); font-size: 13px; }
.prose a { color: var(--accent); text-decoration: none; }
.prose a:hover { text-decoration: underline; text-underline-offset: 0.2em; }

.embed .topbar, .embed .side, .embed .stage-tools, .embed .hint { display: none; }
.embed.no-legend .legend { display: none; }
.search input {
  width: 100%;
  height: 32px;
  padding: 0 12px;
  border: 1px solid var(--border);
  border-radius: var(--radius);
  background: var(--canvas);
  color: var(--fg);
  font: 14px var(--sans);
}
.search input:focus { border-color: var(--accent-emphasis); outline: 1px solid var(--accent-emphasis); outline-offset: 0; }

.results {
  position: absolute;
  top: 36px;
  left: 0;
  right: 0;
  z-index: 20;
  max-height: 420px;
  overflow-y: auto;
  margin: 0;
  padding: 4px 0;
  list-style: none;
  background: var(--overlay);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  box-shadow: var(--shadow-overlay);
}
.results li { display: grid; grid-template-columns: 1fr auto; gap: 0 8px; padding: 6px 12px; cursor: pointer; }
.results li.active { background: var(--canvas-subtle); }
.results .name { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.results .meta { grid-column: 1 / -1; color: var(--fg-muted); font-size: 12px; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.results .none { color: var(--fg-muted); cursor: default; }

.layout { flex: 1; display: flex; min-height: 0; }

.stage { position: relative; flex: 1; min-width: 0; overflow: hidden; }
#graph { position: absolute; inset: 0; width: 100%; height: 100%; display: block; cursor: grab; touch-action: none; }
#graph.grabbing { cursor: grabbing; }
#graph.pointing { cursor: pointer; }

.stage-tools { position: absolute; top: 16px; left: 16px; display: flex; gap: 8px; }

.legend {
  position: absolute;
  left: 16px;
  bottom: 16px;
  margin: 0;
  padding: 8px 12px;
  list-style: none;
  background: var(--canvas);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  color: var(--fg-muted);
  font-size: 12px;
}
.legend li { display: flex; align-items: center; gap: 8px; }
.legend .dot { width: 8px; height: 8px; border-radius: 50%; }
.legend b { color: var(--fg); font-weight: 600; width: 10px; }

.hint { position: absolute; right: 16px; bottom: 16px; margin: 0; color: var(--fg-muted); font-size: 12px; }

.tooltip {
  position: fixed;
  z-index: 30;
  max-width: 340px;
  padding: 8px 10px;
  pointer-events: none;
  background: var(--overlay);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  box-shadow: var(--shadow-overlay);
  color: var(--fg-muted);
  font-size: 12px;
}
.tooltip .head { display: flex; gap: 8px; align-items: baseline; }
.tooltip b { color: var(--fg); font-weight: 600; }

.side {
  flex: none;
  width: 380px;
  display: flex;
  flex-direction: column;
  gap: 16px;
  padding: 16px;
  overflow-y: auto;
  border-left: 1px solid var(--border);
  scrollbar-color: var(--border) transparent;
}

.box { flex: none; border: 1px solid var(--border); border-radius: var(--radius); background: var(--canvas); }
.box-head {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 8px;
  min-height: 44px;
  padding: 8px 16px;
  background: var(--canvas-subtle);
  border-bottom: 1px solid var(--border);
  border-radius: var(--radius) var(--radius) 0 0;
}
.box-head h1 { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.box-body { padding: 16px; }
.caption { color: var(--fg-muted); font-size: 13px; white-space: nowrap; }

.pill {
  flex: none;
  display: inline-flex;
  align-items: center;
  height: 20px;
  padding: 0 7px;
  border: 1px solid var(--border);
  border-radius: 999px;
  color: var(--fg-muted);
  font-size: 12px;
  font-weight: 500;
}

.board {
  position: relative;
  display: grid;
  grid-template-columns: repeat(8, 1fr);
  aspect-ratio: 1;
  border-radius: 4px;
  overflow: hidden;
  user-select: none;
  touch-action: none;
}
.square { position: relative; }
.square.light { background: var(--square-light); }
.square.dark { background: var(--square-dark); }
.square.last::before, .square.pick::before { content: ""; position: absolute; inset: 0; background: var(--square-last); }
.square.pick::before { background: var(--square-pick); }
.square img { position: absolute; inset: 0; width: 100%; height: 100%; cursor: grab; }
.square img.lifted { opacity: 0.3; }
.square .hint-dot { position: absolute; inset: 38%; border-radius: 50%; background: var(--square-hint); pointer-events: none; }
.square .hint-ring { position: absolute; inset: 0; border-radius: 50%; box-shadow: inset 0 0 0 4px var(--square-hint); pointer-events: none; }
.square.target { cursor: pointer; }
.coord { position: absolute; font-size: 10px; font-weight: 600; line-height: 1; pointer-events: none; }
.coord.rank { top: 3px; left: 3px; }
.coord.file { right: 3px; bottom: 3px; }
.square.light .coord { color: var(--square-dark); }
.square.dark .coord { color: var(--square-light); }
.ghost { position: fixed; z-index: 40; pointer-events: none; }

.controls { display: flex; align-items: center; gap: 8px; margin-top: 12px; }
.status { margin-left: auto; color: var(--fg-muted); font-size: 13px; }

.btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 6px;
  height: 32px;
  padding: 0 12px;
  border: 1px solid var(--border);
  border-radius: var(--radius);
  background: var(--btn-bg);
  box-shadow: var(--shadow-sm);
  color: var(--fg);
  font-weight: 500;
  cursor: pointer;
  transition: background 80ms;
}
.btn:hover { background: var(--btn-hover); }
.btn:active { background: var(--btn-active); }
.btn:disabled { color: var(--fg-muted); cursor: default; background: var(--btn-bg); opacity: 0.6; }
.btn-sm { height: 28px; padding: 0 10px; font-size: 13px; }
.btn-icon { width: 32px; padding: 0; }
.btn-icon svg { width: 16px; height: 16px; fill: none; stroke: currentColor; stroke-width: 1.5; stroke-linecap: round; stroke-linejoin: round; }

.line { display: flex; flex-wrap: wrap; gap: 2px; margin: 12px 0 0; padding: 0; list-style: none; font-size: 13px; }
.line .num { padding: 1px 0 1px 4px; color: var(--fg-muted); }
.line button { padding: 1px 4px; border: 0; border-radius: 4px; background: none; cursor: pointer; }
.line button:hover { background: var(--btn-hover); }
.line button.current { background: var(--accent-subtle); color: var(--accent); font-weight: 600; }
.line .empty { color: var(--fg-muted); }

.bar { display: flex; height: 8px; overflow: hidden; border: 1px solid var(--border); border-radius: 999px; background: var(--border-muted); }
.bar span { height: 100%; }
.bar .w { background: var(--result-white); }
.bar .d { background: var(--result-draw); }
.bar .b { background: var(--result-black); }
.facts { display: flex; justify-content: space-between; margin: 8px 0 0; padding: 0; list-style: none; color: var(--fg-muted); font-size: 12px; }
.facts li { display: flex; align-items: center; gap: 6px; }
.facts i { width: 10px; height: 10px; border-radius: 2px; box-shadow: inset 0 0 0 1px var(--border); }
.facts b { color: var(--fg); font-weight: 600; }
.note { margin: 0; color: var(--fg-muted); font-size: 13px; }

.rows { margin: 0; padding: 0; list-style: none; }
.rows li { display: grid; grid-template-columns: 76px 1fr 96px; align-items: center; gap: 12px; padding: 8px 16px; border-top: 1px solid var(--border-muted); font-size: 13px; cursor: pointer; }
.rows li:first-child { border-top: 0; }
.rows li:hover { background: var(--canvas-subtle); }
.rows li.empty { display: block; color: var(--fg-muted); cursor: default; }
.rows li.empty:hover { background: none; }
.rows .move { font-weight: 600; white-space: nowrap; }
.rows .from { overflow: hidden; color: var(--fg-muted); text-overflow: ellipsis; white-space: nowrap; }
.rows .count { color: var(--fg-muted); text-align: right; white-space: nowrap; }
.rows .bar { height: 6px; }
.rows .share { display: flex; flex-direction: column; gap: 4px; }

@media (max-width: 900px) {
  body { overflow: auto; height: auto; }
  .layout { flex-direction: column; }
  .stage { flex: none; height: 62vh; }
  .side { width: auto; overflow: visible; border-left: 0; border-top: 1px solid var(--border); }
  .hint { display: none; }
}

@media (max-width: 760px) {
  .summary { display: none; }
}

@media (max-width: 560px) {
  .topbar { flex-wrap: wrap; padding: 8px 16px; }
  .search { order: 3; width: 100%; }
  .actions { margin-left: auto; }
  .legend { right: 16px; display: flex; flex-wrap: wrap; gap: 2px 12px; padding: 6px 10px; }
}
