:root {
  --bg: #f5f1e8;
  --panel: #fffaf0;
  --ink: #211b16;
  --muted: #766b5c;
  --line: #d6c4aa;
  --red: #b42b25;
  --black: #202020;
  --gold: #b88636;
  --green: #2f6f4e;
  --shadow: 0 18px 55px rgba(65, 42, 20, 0.16);
  font-family: ui-serif, Georgia, "Times New Roman", "Noto Serif SC", serif;
}

* {
  box-sizing: border-box;
}

body {
  margin: 0;
  min-height: 100vh;
  color: var(--ink);
  background:
    radial-gradient(circle at 12% 12%, rgba(184, 134, 54, 0.12), transparent 28rem),
    linear-gradient(135deg, #fbf7ef, var(--bg));
}

button,
select {
  font: inherit;
}

button {
  border: 1px solid var(--line);
  border-radius: 8px;
  background: #fff8ea;
  color: var(--ink);
  cursor: pointer;
  padding: 0.52rem 0.78rem;
}

button:hover {
  border-color: var(--gold);
}

button:disabled {
  cursor: wait;
  opacity: 0.6;
}

select {
  border: 1px solid var(--line);
  border-radius: 7px;
  background: #fffdf8;
  padding: 0.42rem 0.5rem;
}

.app-shell {
  display: grid;
  grid-template-columns: minmax(560px, 1fr) 430px;
  gap: 1.1rem;
  width: min(1440px, calc(100vw - 2rem));
  margin: 0 auto;
  padding: 1rem 0;
}

.board-panel,
.side-panel {
  min-width: 0;
}

.topbar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 1rem;
  margin-bottom: 0.9rem;
}

h1,
h2,
h3,
p {
  margin: 0;
}

h1 {
  font-size: clamp(1.35rem, 2vw, 2rem);
  letter-spacing: 0;
}

h2 {
  font-size: 1rem;
}

h3 {
  font-size: 0.9rem;
}

#statusLine,
.muted,
.analysis-status,
.selection-tools p {
  color: var(--muted);
  font-size: 0.92rem;
}

.topbar-actions {
  display: flex;
  align-items: center;
  gap: 0.55rem;
  flex-wrap: wrap;
  justify-content: flex-end;
}

.select-label,
.settings-row label {
  display: flex;
  align-items: center;
  gap: 0.45rem;
  color: var(--muted);
  font-size: 0.92rem;
}

.settings-row {
  display: flex;
  align-items: center;
  flex-wrap: wrap;
  gap: 0.65rem 0.9rem;
  margin-top: 0.65rem;
}

.checkbox-label input {
  accent-color: var(--green);
}

.board-wrap {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 0.38rem;
  padding: min(2vw, 1.25rem);
  border: 1px solid var(--line);
  border-radius: 8px;
  background:
    linear-gradient(90deg, rgba(255, 255, 255, 0.42), transparent),
    #e6c58e;
  box-shadow: var(--shadow);
}

.board {
  display: grid;
  grid-template-columns: repeat(9, minmax(42px, 1fr));
  grid-template-rows: repeat(10, minmax(42px, 1fr));
  width: min(82vh, 760px, calc(100vw - 3rem));
  aspect-ratio: 9 / 10;
  border: 2px solid #6e4720;
  background: #e8c88f;
  isolation: isolate;
  overflow: hidden;
  position: relative;
}

.board-stage {
  position: relative;
  width: min(82vh, 760px, calc(100vw - 3rem));
  aspect-ratio: 9 / 10;
}

.board-stage .board {
  width: 100%;
}

.board::after {
  content: "楚河        汉界";
  position: absolute;
  left: 0;
  right: 0;
  top: 45%;
  height: 10%;
  display: flex;
  align-items: center;
  justify-content: center;
  pointer-events: none;
  color: rgba(87, 52, 19, 0.34);
  font-size: clamp(1rem, 3vw, 2.2rem);
  letter-spacing: 0.5rem;
  z-index: 0;
}

.board-status {
  position: absolute;
  left: 50%;
  top: 50%;
  z-index: 3;
  transform: translate(-50%, -50%);
  border: 1px solid rgba(122, 75, 30, 0.22);
  border-radius: 999px;
  background: rgba(255, 250, 240, 0.82);
  color: rgba(77, 59, 38, 0.72);
  padding: 0.42rem 0.78rem;
  font-size: clamp(0.8rem, 1.4vw, 1rem);
  font-weight: 700;
  pointer-events: none;
  box-shadow: 0 8px 22px rgba(65, 42, 20, 0.12);
}

.board-status.active {
  background: rgba(180, 43, 37, 0.94);
  color: #fffdf8;
  border-color: rgba(255, 255, 255, 0.34);
  animation: pulseStatus 0.9s ease-in-out infinite alternate;
}

.board-status.pending {
  background: rgba(184, 134, 54, 0.9);
  color: #fffdf8;
}

.board-status.warning {
  background: rgba(207, 107, 31, 0.94);
  color: #fffdf8;
  border-color: rgba(255, 255, 255, 0.38);
  box-shadow:
    0 8px 24px rgba(207, 107, 31, 0.28),
    0 0 0 4px rgba(255, 250, 240, 0.72);
}

.board-status.terminal {
  background: rgba(180, 43, 37, 0.96);
  color: #fffdf8;
  border-color: rgba(255, 255, 255, 0.42);
  box-shadow:
    0 8px 24px rgba(180, 43, 37, 0.28),
    0 0 0 4px rgba(255, 250, 240, 0.78);
}

.board-status.ready,
.board-status.idle {
  top: 51%;
  opacity: 0.78;
}

.board-lines {
  position: absolute;
  inset: 0;
  pointer-events: none;
  z-index: 0;
}

.board-line {
  position: absolute;
  display: block;
  background: rgba(122, 75, 30, 0.78);
}

.board-line.horizontal {
  height: 1.5px;
  transform: translateY(-50%);
}

.board-line.vertical {
  width: 1.5px;
  transform: translateX(-50%);
}

.captured-row {
  width: min(82vh, 760px, calc(100vw - 3rem));
  min-height: 2.1rem;
  display: flex;
}

.captured-top {
  justify-content: flex-end;
}

.captured-bottom {
  justify-content: flex-start;
}

.captured-tray {
  display: flex;
  align-items: center;
  gap: 0.42rem;
  width: auto;
  max-width: 68%;
  border: 1px solid rgba(122, 75, 30, 0.22);
  border-radius: 8px;
  background: rgba(255, 250, 240, 0.58);
  padding: 0.26rem 0.36rem;
  pointer-events: none;
}

.captured-title {
  flex: 0 0 auto;
  color: rgba(77, 59, 38, 0.75);
  font-size: 0.62rem;
  font-weight: 700;
}

.captured-pieces {
  display: flex;
  flex-wrap: wrap;
  gap: 0.16rem;
  min-width: 0;
}

.captured-opponent-took-mine {
  flex-direction: row-reverse;
}

.captured-opponent-took-mine .captured-pieces {
  flex-direction: row-reverse;
}

.captured-empty {
  color: rgba(77, 59, 38, 0.5);
  font-size: 0.66rem;
}

.mini-piece {
  display: grid;
  place-items: center;
  width: 1.28rem;
  aspect-ratio: 1;
  border: 1px solid rgba(94, 58, 21, 0.66);
  border-radius: 999px;
  background: rgba(255, 246, 222, 0.92);
  font-family: "PingFang TC", "PingFang HK", "PingFang SC", "Noto Sans CJK TC", sans-serif;
  font-variant-east-asian: traditional;
  font-size: 0.78rem;
  font-weight: 800;
  line-height: 1;
}

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

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

.mini-piece.captured-hidden {
  background: rgba(87, 63, 38, 0.86);
  border-color: rgba(56, 38, 22, 0.88);
  color: rgba(255, 246, 222, 0.9);
}

.cell {
  position: relative;
  display: grid;
  place-items: center;
  padding: 0;
  border: 0;
  border-radius: 0;
  background: transparent;
  min-width: 0;
  z-index: 1;
}

.cell.warm {
  background: rgba(255, 245, 218, 0.12);
}

.cell.selected {
  background: rgba(47, 111, 78, 0.22);
}

.cell.legal-target::before {
  content: "";
  position: absolute;
  width: 34%;
  aspect-ratio: 1;
  border-radius: 50%;
  background: rgba(47, 111, 78, 0.26);
}

.cell.hint-from,
.cell.pending-from {
  background: rgba(180, 43, 37, 0.08);
}

.cell.hint-to,
.cell.pending-to {
  background: rgba(180, 43, 37, 0.1);
}

.cell.hint-from::after,
.cell.hint-to::after,
.cell.pending-from::after,
.cell.pending-to::after {
  content: "";
  position: absolute;
  left: 50%;
  top: 50%;
  z-index: 4;
  width: min(86%, 70px);
  aspect-ratio: 1;
  transform: translate(-50%, -50%);
  border: 3px solid rgba(180, 43, 37, 0.95);
  border-radius: 6px;
  box-shadow:
    0 0 0 2px rgba(255, 250, 240, 0.88),
    0 0 15px rgba(180, 43, 37, 0.24);
  pointer-events: none;
}

.cell.hint-from::after,
.cell.pending-from::after {
  border-style: dashed;
}

.cell.hint-to::after,
.cell.pending-to::after {
  border-width: 4px;
}

.cell.last-from,
.cell.last-to {
  box-shadow: inset 0 0 0 2px rgba(184, 134, 54, 0.38);
}

.coord {
  position: absolute;
  left: 3px;
  top: 2px;
  color: rgba(72, 50, 28, 0.45);
  font-size: 0.62rem;
  pointer-events: none;
}

.piece {
  z-index: 2;
  display: grid;
  place-items: center;
  width: 78%;
  max-width: 58px;
  aspect-ratio: 1;
  border-radius: 999px;
  border: 2px solid #7d4f20;
  background:
    radial-gradient(circle at 35% 28%, #fff9e9, #e8c78b 70%),
    #e8c78b;
  box-shadow: 0 5px 12px rgba(58, 34, 11, 0.28);
  font-family: "PingFang TC", "PingFang HK", "PingFang SC", "Noto Sans CJK TC", sans-serif;
  font-variant-east-asian: traditional;
  line-height: 1;
  pointer-events: none;
}

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

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

.piece.hidden {
  color: #6b5138;
  background:
    radial-gradient(circle at 35% 28%, #dec291, #8b5f2f 74%),
    #8b5f2f;
}

.preview-piece {
  box-shadow:
    0 5px 12px rgba(58, 34, 11, 0.28),
    0 0 0 3px rgba(255, 250, 240, 0.9);
  transform: scale(1.05);
}

.toast {
  position: fixed;
  left: 50%;
  top: 1rem;
  z-index: 20;
  transform: translateX(-50%);
  border: 1px solid rgba(255, 255, 255, 0.5);
  border-radius: 999px;
  background: rgba(33, 27, 22, 0.9);
  color: #fffdf8;
  padding: 0.6rem 0.95rem;
  font-weight: 700;
  box-shadow: 0 14px 35px rgba(33, 27, 22, 0.24);
  animation: toastFlash 0.95s ease forwards;
}

.toast[hidden] {
  display: none;
}

@keyframes toastFlash {
  0% {
    opacity: 0;
    transform: translate(-50%, -0.35rem);
  }
  16%,
  72% {
    opacity: 1;
    transform: translate(-50%, 0);
  }
  100% {
    opacity: 0;
    transform: translate(-50%, -0.25rem);
  }
}

@keyframes pulseStatus {
  from {
    box-shadow: 0 8px 22px rgba(65, 42, 20, 0.12);
  }
  to {
    box-shadow: 0 8px 28px rgba(180, 43, 37, 0.36);
  }
}

.piece-main {
  font-weight: 800;
  font-size: clamp(1.15rem, 3vw, 1.85rem);
}

.piece-origin {
  margin-top: -0.2rem;
  color: rgba(255, 248, 226, 0.9);
  font-size: clamp(0.48rem, 1.1vw, 0.68rem);
}

.side-panel {
  display: flex;
  flex-direction: column;
  gap: 0.75rem;
  max-height: calc(100vh - 2rem);
  overflow: auto;
  padding-right: 0.2rem;
}

.panel-block {
  border: 1px solid var(--line);
  border-radius: 8px;
  background: rgba(255, 250, 240, 0.9);
  padding: 0.9rem;
}

.panel-title-row,
.rec-footer,
.tool-row {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 0.65rem;
}

.panel-heading {
  display: flex;
  align-items: center;
  gap: 0.55rem;
  min-width: 0;
}

.engine-version {
  border: 1px solid rgba(47, 111, 78, 0.25);
  border-radius: 999px;
  background: rgba(47, 111, 78, 0.08);
  color: var(--green);
  font-size: 0.72rem;
  line-height: 1;
  padding: 0.25rem 0.45rem;
  white-space: nowrap;
}

.analysis-status {
  margin-top: 0.65rem;
}

.pending-move-panel {
  display: flex;
  flex-direction: column;
  gap: 0.7rem;
  margin-top: 0.75rem;
  border: 1px solid rgba(47, 111, 78, 0.3);
  border-radius: 8px;
  background: rgba(47, 111, 78, 0.08);
  padding: 0.78rem;
}

.pending-move-panel.needs-input {
  border: 2px solid rgba(180, 43, 37, 0.95);
  background: rgba(180, 43, 37, 0.08);
  box-shadow:
    0 0 0 3px rgba(255, 250, 240, 0.92),
    0 0 0 6px rgba(180, 43, 37, 0.22),
    0 12px 28px rgba(180, 43, 37, 0.18);
}

.pending-move-panel.needs-input .pending-header strong {
  color: var(--red);
}

.pending-move-panel.auto-executed {
  border-color: rgba(184, 134, 54, 0.48);
  background: rgba(184, 134, 54, 0.08);
}

.pending-move-panel[hidden] {
  display: none;
}

.floating-pending-panel {
  position: absolute;
  z-index: 12;
  width: min(310px, 72%);
  max-height: 78%;
  margin: 0;
  overflow: auto;
  background: rgba(255, 250, 240, 0.98);
  backdrop-filter: blur(4px);
}

.floating-pending-panel.anchor-right { transform: translate(var(--piece-clearance, 3rem), -50%); }
.floating-pending-panel.anchor-left { transform: translate(calc(-100% - var(--piece-clearance, 3rem)), -50%); }
.floating-pending-panel.anchor-right.anchor-top { transform: translate(var(--piece-clearance, 3rem), 0); }
.floating-pending-panel.anchor-left.anchor-top { transform: translate(calc(-100% - var(--piece-clearance, 3rem)), 0); }
.floating-pending-panel.anchor-right.anchor-bottom { transform: translate(var(--piece-clearance, 3rem), -100%); }
.floating-pending-panel.anchor-left.anchor-bottom { transform: translate(calc(-100% - var(--piece-clearance, 3rem)), -100%); }

.mate-alert {
  border-color: rgba(180, 43, 37, 0.72);
  background: rgba(180, 43, 37, 0.1);
  color: var(--red);
  font-weight: 800;
}

.pending-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 0.65rem;
}

.pending-summary {
  color: #4d4238;
  font-size: 0.88rem;
  line-height: 1.45;
}

.recommendations {
  display: flex;
  flex-direction: column;
  gap: 0.65rem;
  margin-top: 0.75rem;
}

.rec-card {
  border: 1px solid #e0ceb1;
  border-radius: 8px;
  background: #fffdf8;
  padding: 0.78rem;
}

.rec-card.active-preview {
  border-color: rgba(180, 43, 37, 0.85);
  background: rgba(180, 43, 37, 0.06);
  box-shadow:
    0 0 0 2px rgba(255, 250, 240, 0.95),
    0 0 0 4px rgba(180, 43, 37, 0.18);
}

.rec-title {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 0.55rem;
  font-size: 0.92rem;
}

.mate-tag {
  flex: 0 0 auto;
  border-radius: 999px;
  background: var(--red);
  color: #fffdf8;
  padding: 0.2rem 0.45rem;
  font-size: 0.72rem;
  font-weight: 800;
}

.rec-score {
  color: var(--green);
  font-size: 0.84rem;
  margin-top: 0.35rem;
}

.rec-reasons {
  margin: 0.55rem 0;
  padding-left: 1.1rem;
  color: #4e4236;
  font-size: 0.86rem;
}

.rec-reasons li + li {
  margin-top: 0.22rem;
}

.rec-footer {
  color: var(--muted);
  font-size: 0.8rem;
}

.win-rate-chart {
  margin-top: 0.75rem;
  border: 1px solid rgba(47, 111, 78, 0.22);
  border-radius: 8px;
  background:
    linear-gradient(180deg, rgba(47, 111, 78, 0.07), rgba(255, 253, 248, 0.92)),
    #fffdf8;
  padding: 0.65rem;
}

.win-rate-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 0.6rem;
  color: #3b3328;
  font-size: 0.86rem;
}

.win-rate-header span,
.win-rate-meta,
.win-rate-empty {
  color: var(--muted);
  font-size: 0.76rem;
}

.win-rate-chart svg {
  display: block;
  width: 100%;
  height: 118px;
  margin-top: 0.34rem;
}

.win-rate-chart text {
  fill: rgba(77, 59, 38, 0.48);
  font-size: 9px;
}

.grid-line {
  stroke: rgba(122, 75, 30, 0.16);
  stroke-width: 1;
}

.grid-line.mid {
  stroke: rgba(122, 75, 30, 0.3);
  stroke-dasharray: 4 4;
}

.win-rate-line {
  fill: none;
  stroke: var(--green);
  stroke-width: 3;
  stroke-linecap: round;
  stroke-linejoin: round;
  filter: drop-shadow(0 2px 4px rgba(47, 111, 78, 0.16));
}

.win-rate-dot {
  fill: var(--red);
  stroke: #fffdf8;
  stroke-width: 2;
}

.win-rate-meta {
  margin-top: 0.18rem;
}

.state-summary {
  display: grid;
  grid-template-columns: repeat(2, minmax(0, 1fr));
  gap: 0.5rem;
  margin-top: 0.75rem;
}

.state-summary div {
  border: 1px solid #eadcc6;
  border-radius: 7px;
  padding: 0.48rem;
  background: #fffdf8;
}

.state-summary span {
  display: block;
  color: var(--muted);
  font-size: 0.72rem;
}

.pool-summary {
  display: flex;
  flex-direction: column;
  gap: 0.75rem;
  margin-top: 0.75rem;
}

.pool-side h3 {
  color: #4d3b26;
  margin-bottom: 0.45rem;
}

.pool-grid {
  display: grid;
  grid-template-columns: repeat(3, 1fr);
  gap: 0.45rem;
}

.pool-item {
  border: 1px solid #eadcc6;
  border-radius: 7px;
  background: #fffdf8;
  padding: 0.45rem;
}

.pool-item span,
.pool-item small {
  display: block;
  color: var(--muted);
}

.pool-item strong {
  font-size: 1.1rem;
}

.selection-tools {
  display: flex;
  flex-direction: column;
  gap: 0.6rem;
  margin-top: 0.65rem;
}

.danger {
  border-color: rgba(180, 43, 37, 0.42);
  color: var(--red);
}

.move-log {
  margin: 0.7rem 0 0;
  padding-left: 1.25rem;
  color: #4d4238;
  font-size: 0.84rem;
}

.move-log li + li {
  margin-top: 0.38rem;
}

.role-field {
  display: grid;
  gap: 0.5rem;
  margin: 0;
  padding: 0;
  border: 0;
  color: var(--muted);
}

.role-field legend {
  padding: 0;
  color: #4d3b26;
  font-weight: 700;
}

.role-choice-grid {
  display: grid;
  grid-template-columns: repeat(3, minmax(0, 1fr));
  gap: 0.45rem;
}

.role-choice {
  display: grid;
  place-items: center;
  gap: 0.12rem;
  min-height: 4rem;
  border-color: #e0ceb1;
  background: #fffdf8;
  padding: 0.45rem;
}

.role-choice strong {
  color: var(--ink);
  font-size: 1.32rem;
  line-height: 1;
}

.role-choice span {
  color: var(--muted);
  font-size: 0.72rem;
}

.role-choice.selected {
  border-color: var(--green);
  background: rgba(47, 111, 78, 0.13);
  box-shadow: inset 0 0 0 1px rgba(47, 111, 78, 0.24);
}

#confirmMoveBtn:disabled {
  display: none;
}

.dialog-actions {
  display: flex;
  justify-content: flex-end;
  gap: 0.55rem;
}

@media (max-width: 1020px) {
  .app-shell {
    grid-template-columns: 1fr;
    width: min(100vw - 1rem, 820px);
  }

  .side-panel {
    max-height: none;
  }

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

@media (max-width: 620px) {
  .board,
  .board-stage {
    width: calc(100vw - 2rem);
  }

  .floating-pending-panel {
    width: min(300px, 82%);
  }

  .app-shell {
    padding-top: 0.5rem;
  }

  .panel-title-row {
    align-items: flex-start;
    flex-direction: column;
  }
}
