:root {
  color-scheme: light;
  --page: #eee8df;
  --ink: #201a18;
  --muted: #74675d;
  --paper: #f6f4ef;
  --panel: #fffdf8;
  --line: #d8ccc0;
  --board: #dfb56d;
  --board-dark: #7a4624;
  --board-line: #5f321b;
  --red: #b5252a;
  --black: #171717;
  --green: #1f6f5b;
  --gold: #f3d78c;
  --shadow: 0 12px 30px rgba(45, 31, 20, 0.12);
  --board-max: 488px;
  --side-width: 300px;
  --layout-gap: 24px;
}

* {
  box-sizing: border-box;
}

body {
  min-width: 320px;
  min-height: 100vh;
  margin: 0;
  display: grid;
  place-items: center;
  background: var(--page);
  color: var(--ink);
  font-family:
    Inter, ui-sans-serif, system-ui, -apple-system, BlinkMacSystemFont, "Segoe UI",
    "PingFang SC", "Microsoft YaHei", sans-serif;
}

button {
  font: inherit;
}

.app-shell {
  width: min(calc(var(--board-max) + var(--side-width) + var(--layout-gap)), calc(100vw - 32px));
  margin: 0 auto;
  padding: 8px 0 10px;
}

.topbar {
  width: 100%;
  margin-bottom: 10px;
  padding: 0;
  background: transparent;
}

.panel-label {
  margin: 0;
  color: var(--muted);
  font-size: 0.78rem;
  font-weight: 700;
  letter-spacing: 0;
}

.brand-mark {
  margin: 0 0 4px;
  color: #3a3632;
  font-size: 1rem;
  font-weight: 900;
  letter-spacing: 0;
}

.title-line {
  display: flex;
  align-items: flex-end;
  gap: 24px;
}

h1 {
  margin: 0;
  color: #050505;
  font-size: clamp(4.2rem, 7.2vw, 5.8rem);
  font-weight: 950;
  line-height: 0.9;
  letter-spacing: 0;
}

.tagline {
  margin: 0 0 8px;
  color: #35312d;
  font-size: clamp(1.4rem, 2vw, 1.9rem);
  font-weight: 900;
  letter-spacing: 0;
  white-space: nowrap;
}

.actions {
  display: flex;
  gap: 10px;
  flex-wrap: wrap;
  justify-content: flex-start;
}

.board-actions {
  display: grid;
  grid-template-columns: repeat(3, minmax(0, 1fr));
  gap: 7px;
  width: 100%;
  margin: 0;
}

.board-actions .tool-button {
  min-width: 0;
  min-height: 34px;
  gap: 4px;
  padding: 0 4px;
  font-size: 0.68rem;
}

.board-actions .tool-button span:last-child {
  min-width: 0;
  overflow: hidden;
  text-overflow: ellipsis;
}

.connect-panel {
  margin-bottom: 20px;
  padding: 0;
  border: 0;
  background: transparent;
  box-shadow: none;
}

.connect-form {
  display: grid;
  grid-template-columns: repeat(4, minmax(0, 1fr));
  gap: 9px 12px;
  align-items: end;
}

.connect-form label {
  display: grid;
  gap: 6px;
  min-width: 0;
}

.connect-form label span {
  color: var(--muted);
  font-size: 0.78rem;
  font-weight: 700;
}

.connect-form input {
  width: 100%;
  min-height: 38px;
  border: 1px solid #c9beb4;
  border-radius: 8px;
  padding: 0 12px;
  background: rgba(255, 253, 248, 0.92);
  color: var(--ink);
  font: inherit;
}

.connect-form input:focus {
  border-color: var(--green);
  outline: 3px solid rgba(31, 111, 91, 0.18);
}

.connection-state {
  display: flex;
  align-items: center;
  gap: 6px;
  min-width: 0;
  min-height: 38px;
  color: var(--muted);
  font-size: 0.88rem;
  overflow: hidden;
}

.connection-state span:last-child {
  min-width: 0;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.connection-badge {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  min-height: 30px;
  padding: 0 10px;
  border: 1px solid rgba(116, 103, 93, 0.22);
  border-radius: 8px;
  font-size: 0.82rem;
  font-weight: 800;
}

.connection-badge.online {
  color: var(--green);
  background: rgba(31, 111, 91, 0.1);
}

.connection-badge.pending {
  color: #8a5a13;
  background: rgba(243, 215, 140, 0.3);
}

.connection-badge.error {
  color: var(--red);
  background: rgba(181, 37, 42, 0.08);
}

.connection-badge.offline {
  color: var(--muted);
  background: rgba(116, 103, 93, 0.08);
}

.tool-button {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 8px;
  min-height: 38px;
  padding: 0 12px;
  border: 1px solid #c9beb4;
  border-radius: 8px;
  background: rgba(255, 253, 248, 0.92);
  color: var(--ink);
  cursor: pointer;
  white-space: nowrap;
  transition:
    transform 150ms ease,
    border-color 150ms ease,
    background 150ms ease;
}

.connect-form .tool-button {
  width: 100%;
  min-width: 0;
  padding: 0 10px;
}

.tool-button:hover {
  transform: translateY(-1px);
  border-color: #8f8175;
  background: #ffffff;
}

.tool-button.primary {
  border-color: var(--red);
  background: var(--red);
  color: #fffaf5;
}

.tool-button.special.active {
  border-color: var(--green);
  background: rgba(31, 111, 91, 0.12);
  color: var(--green);
  box-shadow: inset 0 -3px 0 currentColor;
}

.tool-button.declare.active {
  border-color: var(--red);
  background: rgba(181, 37, 42, 0.12);
  color: var(--red);
}

.tool-button:disabled {
  cursor: not-allowed;
  opacity: 0.52;
  transform: none;
}

.game-layout {
  display: grid;
  grid-template-columns: minmax(0, var(--board-max)) minmax(0, var(--side-width));
  justify-content: center;
  gap: var(--layout-gap);
  align-items: start;
}

.board-area {
  display: grid;
  justify-items: center;
  min-width: 0;
}

.players {
  display: grid;
  grid-template-columns: repeat(2, minmax(0, 1fr));
  gap: 10px;
  width: 100%;
  margin-bottom: 0;
}

.player {
  display: flex;
  align-items: center;
  gap: 8px;
  min-width: 0;
  min-height: 58px;
  padding: 10px 12px;
  border: 1px solid var(--line);
  border-radius: 8px;
  background: rgba(255, 253, 248, 0.74);
}

.player strong,
.player small {
  display: block;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.player small {
  color: var(--muted);
  font-size: 0.78rem;
}

.player.active {
  border-color: currentColor;
  box-shadow: inset 0 -3px 0 currentColor;
}

.player.you {
  background: #fff8e8;
}

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

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

.side-dot {
  flex: 0 0 auto;
  width: 14px;
  height: 14px;
  border-radius: 50%;
}

.red-dot {
  background: var(--red);
}

.black-dot {
  background: var(--black);
}

.board {
  position: relative;
  width: min(100%, var(--board-max));
  aspect-ratio: 9 / 10;
  overflow: hidden;
  border: clamp(4px, 0.8vw, 8px) solid var(--board-dark);
  border-radius: 8px;
  background:
    linear-gradient(90deg, rgba(255, 255, 255, 0.16), transparent 22%, rgba(83, 43, 18, 0.1)),
    var(--board);
  box-shadow: var(--shadow);
  touch-action: manipulation;
}

#boardCanvas,
.board-grid {
  position: absolute;
  inset: 0;
}

#boardCanvas {
  width: 100%;
  height: 100%;
  pointer-events: none;
}

.board-grid {
  display: grid;
  grid-template-columns: repeat(9, 1fr);
  grid-template-rows: repeat(10, 1fr);
  padding: 0;
}

.cell {
  position: relative;
  display: flex;
  align-items: center;
  justify-content: center;
  min-width: 0;
  min-height: 0;
  border: 0;
  padding: 0;
  background: transparent;
  color: inherit;
  cursor: pointer;
}

.cell:focus-visible {
  outline: 3px solid rgba(31, 111, 91, 0.8);
  outline-offset: -5px;
}

.cell::before,
.cell::after {
  position: absolute;
  content: "";
  pointer-events: none;
}

.cell.last-from::before,
.cell.last-to::before {
  width: 42%;
  aspect-ratio: 1;
  border: 3px solid rgba(31, 111, 91, 0.82);
  border-radius: 50%;
}

.cell.target-empty::after {
  width: 18%;
  aspect-ratio: 1;
  border-radius: 50%;
  background: rgba(31, 111, 91, 0.78);
  box-shadow: 0 0 0 5px rgba(255, 255, 255, 0.18);
}

.cell.target-capture::after {
  width: 78%;
  aspect-ratio: 1;
  border: 4px solid rgba(31, 111, 91, 0.86);
  border-radius: 50%;
}

.piece {
  position: relative;
  z-index: 1;
  display: grid;
  place-items: center;
  width: min(76%, 58px);
  aspect-ratio: 1;
  border: 2px solid currentColor;
  border-radius: 50%;
  background:
    radial-gradient(circle at 34% 28%, rgba(255, 255, 255, 0.86), transparent 31%),
    linear-gradient(145deg, #fff5d8, #e7c878 56%, #bd8d42 100%);
  box-shadow:
    inset 0 0 0 3px rgba(255, 250, 229, 0.72),
    0 6px 12px rgba(47, 29, 12, 0.28);
  font-family:
    "STKaiti", "Kaiti SC", "KaiTi", "Songti SC", "Noto Serif CJK SC", serif;
  font-size: clamp(1rem, 3.2vmin, 1.45rem);
  font-weight: 800;
  line-height: 1;
  user-select: none;
}

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

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

.piece.combined {
  font-size: clamp(0.72rem, 2.5vmin, 1.05rem);
}

.piece.stealthed::after,
.piece.flat-footed::after {
  position: absolute;
  right: -8px;
  bottom: -6px;
  display: grid;
  place-items: center;
  min-width: 18px;
  height: 18px;
  padding: 0 3px;
  border: 1px solid currentColor;
  border-radius: 999px;
  background: #fffaf3;
  font-family: Inter, ui-sans-serif, system-ui, sans-serif;
  font-size: 0.68rem;
  font-weight: 900;
  line-height: 1;
}

.piece.stealthed::after {
  content: "伏";
}

.piece.flat-footed::after {
  content: "扁";
}

.cell.selected .piece {
  outline: 4px solid rgba(31, 111, 91, 0.8);
  outline-offset: 3px;
  transform: translateY(-2px);
}

.side-panel {
  display: grid;
  gap: 10px;
  align-content: start;
}

.status-panel,
.capture-panel,
.log-panel {
  border: 1px solid var(--line);
  border-radius: 8px;
  background: rgba(255, 253, 248, 0.82);
  box-shadow: 0 7px 16px rgba(45, 31, 20, 0.06);
}

.status-panel {
  padding: 14px 16px;
}

.status-panel h2 {
  margin: 6px 0 8px;
  font-size: 1.22rem;
  letter-spacing: 0;
}

.game-status {
  min-height: 1.4em;
  margin: 0;
  color: var(--muted);
  font-size: 0.95rem;
}

.capture-panel {
  display: grid;
  gap: 12px;
  padding: 14px 16px;
}

.captured-line {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  gap: 6px;
  min-height: 30px;
  margin-top: 6px;
}

.captured-piece {
  display: grid;
  place-items: center;
  width: 30px;
  height: 30px;
  border: 1px solid currentColor;
  border-radius: 50%;
  background: #f7e3a3;
  font-family: "STKaiti", "Kaiti SC", "KaiTi", "Songti SC", serif;
  font-size: 1rem;
  font-weight: 800;
}

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

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

.captured-piece.combined {
  font-size: 0.72rem;
}

.empty-captures {
  color: var(--muted);
  font-size: 0.88rem;
}

.log-panel {
  overflow: hidden;
}

.panel-heading {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  padding: 12px 16px;
  border-bottom: 1px solid var(--line);
}

.move-count {
  color: var(--green);
  font-size: 0.86rem;
  font-weight: 700;
}

.move-log {
  display: grid;
  gap: 2px;
  max-height: 155px;
  margin: 0;
  padding: 6px 0;
  overflow: auto;
  list-style-position: inside;
}

.move-log li {
  padding: 7px 16px;
  color: #3d342e;
  font-size: 0.92rem;
}

.move-log li:nth-child(odd) {
  background: rgba(223, 181, 109, 0.16);
}

.move-log .side-red {
  color: var(--red);
  font-weight: 700;
}

.move-log .side-black {
  color: var(--black);
  font-weight: 700;
}

.rules-panel {
  width: 100%;
  margin-top: 22px;
  padding: 18px;
  border: 1px solid rgba(216, 204, 192, 0.9);
  border-radius: 8px;
  background: rgba(255, 253, 248, 0.62);
  box-shadow: 0 7px 16px rgba(45, 31, 20, 0.05);
}

.rules-heading {
  max-width: 760px;
  margin-bottom: 16px;
}

.rules-heading h2 {
  margin: 5px 0 8px;
  color: #16110f;
  font-size: clamp(1.5rem, 2.5vw, 2rem);
  line-height: 1.15;
  letter-spacing: 0;
}

.rules-heading p:last-child {
  margin: 0;
  color: var(--muted);
  font-size: 0.96rem;
  line-height: 1.7;
}

.rules-grid {
  display: grid;
  grid-template-columns: repeat(2, minmax(0, 1fr));
  gap: 10px;
}

.rules-card {
  min-width: 0;
  padding: 14px 15px;
  border: 1px solid var(--line);
  border-radius: 8px;
  background: rgba(255, 253, 248, 0.88);
}

.rules-card h3 {
  margin: 0 0 8px;
  color: #2a211d;
  font-size: 1rem;
  line-height: 1.3;
  letter-spacing: 0;
}

.rules-card ul {
  display: grid;
  gap: 6px;
  margin: 0;
  padding-left: 1.2em;
  color: #43372f;
  font-size: 0.92rem;
  line-height: 1.58;
}

.rules-card li::marker {
  color: var(--red);
}

.check-explosion {
  position: fixed;
  inset: 0;
  z-index: 1000;
  display: grid;
  place-items: center;
  pointer-events: none;
  visibility: hidden;
  opacity: 0;
  background:
    radial-gradient(circle at 50% 50%, rgba(255, 248, 147, 0.98) 0 12%, rgba(230, 58, 41, 0.88) 13% 32%, rgba(65, 32, 24, 0.92) 33% 54%, rgba(20, 12, 10, 0.72) 55% 100%);
}

.check-explosion.show {
  visibility: visible;
  animation: checkExplosion 500ms ease-out forwards;
}

.check-explosion-text {
  padding: 16px 22px;
  border: 5px solid #fff3b4;
  border-radius: 8px;
  background: rgba(181, 37, 42, 0.95);
  color: #fff3b4;
  font-size: clamp(4.2rem, 14vw, 10rem);
  font-weight: 950;
  line-height: 0.95;
  letter-spacing: 0;
  text-align: center;
  text-shadow:
    0 5px 0 #4b120d,
    0 14px 34px rgba(0, 0, 0, 0.45);
  transform: rotate(-5deg);
}

@keyframes checkExplosion {
  0% {
    opacity: 0;
    transform: scale(0.65);
  }

  18% {
    opacity: 1;
    transform: scale(1.08);
  }

  72% {
    opacity: 1;
    transform: scale(1);
  }

  100% {
    opacity: 0;
    transform: scale(1.22);
  }
}

@media (prefers-reduced-motion: reduce) {
  .check-explosion.show {
    animation: checkExplosionReduced 500ms linear forwards;
  }

  .check-explosion-text {
    transform: none;
  }
}

@keyframes checkExplosionReduced {
  0%,
  85% {
    opacity: 1;
  }

  100% {
    opacity: 0;
  }
}

@media (max-width: 920px) {
  body {
    display: block;
  }

  .app-shell {
    width: min(100vw - 18px, 640px);
    padding: 16px 0;
  }

  .topbar {
    margin-bottom: 14px;
  }

  .title-line {
    gap: 16px;
  }

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

  .connection-state {
    align-items: flex-start;
  }

  .game-layout {
    grid-template-columns: 1fr;
  }

  .board-area {
    justify-items: center;
  }

  .board {
    margin-inline: auto;
  }

  .board-actions {
    margin-inline: auto;
  }

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

  .players,
  .board-actions {
    grid-column: 1 / -1;
  }

  .move-log {
    max-height: 250px;
  }

  .rules-panel {
    margin-top: 16px;
  }
}

@media (max-width: 700px) {
  .app-shell {
    width: min(100vw - 18px, 640px);
  }

  .title-line {
    display: block;
  }

  h1 {
    font-size: clamp(3.4rem, 18vw, 4.8rem);
    line-height: 0.96;
  }

  .tagline {
    margin-top: 8px;
    font-size: 1.25rem;
  }

  .actions {
    display: grid;
    grid-template-columns: repeat(2, minmax(0, 1fr));
    justify-content: stretch;
  }

  .connect-form {
    grid-template-columns: 1fr;
  }

  .tool-button {
    flex: 1 1 0;
  }

  .actions .tool-button {
    width: 100%;
  }

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

  .player {
    padding: 10px 12px;
  }

  .rules-panel {
    padding: 14px;
  }

  .rules-grid {
    grid-template-columns: 1fr;
  }

  .rules-card {
    padding: 13px 14px;
  }
}
