:root {
  --bg: #0e1117;
  --panel: #171d27;
  --line: #232c3a;
  --text: #e8ecf3;
  --muted: #8692a6;
  --light: #ebd9b8;
  --dark: #a8794f;
  --accent: #7fb0ff;
  --cell: 64px;
}

* {
  box-sizing: border-box;
}

body {
  margin: 0;
  min-height: 100vh;
  background: radial-gradient(1200px 620px at 50% -12%, #1c2432 0%, var(--bg) 58%);
  color: var(--text);
  font-family: "Segoe UI", "PingFang SC", "Microsoft YaHei", system-ui, sans-serif;
  -webkit-font-smoothing: antialiased;
}

.app {
  max-width: 1040px;
  margin: 0 auto;
  padding: 26px 20px 44px;
}

.topbar {
  display: flex;
  align-items: center;
  gap: 14px;
  margin-bottom: 22px;
}

.brand-mark {
  flex: 0 0 auto;
  width: 52px;
  height: 52px;
  border-radius: 14px;
  overflow: hidden;
  background: #161c26;
  box-shadow: 0 10px 24px rgba(0, 0, 0, 0.45), 0 0 0 1px rgba(255, 255, 255, 0.08);
}

.brand-mark img {
  display: block;
  width: 100%;
  height: 100%;
  object-fit: cover;
}

h1 {
  margin: 0;
  font-size: 22px;
  font-weight: 600;
  letter-spacing: 0.06em;
}

.layout {
  display: flex;
  gap: 26px;
  align-items: stretch;
}

/* ---------------- 棋盘 ---------------- */

.board-wrap {
  position: relative;
  flex: 0 0 auto;
  width: min(70vh, 100%);
  max-width: 600px;
}

.board {
  position: relative;
  width: 100%;
  aspect-ratio: 1 / 1;
  border-radius: 10px;
  overflow: hidden;
  touch-action: none;
  user-select: none;
  box-shadow: 0 26px 60px rgba(0, 0, 0, 0.55), 0 0 0 1px rgba(255, 255, 255, 0.05);
}

.squares {
  position: absolute;
  inset: 0;
  display: grid;
  grid-template-columns: repeat(8, 1fr);
  grid-template-rows: repeat(8, 1fr);
}

.square {
  position: relative;
}

.square.light {
  background: var(--light);
}

.square.dark {
  background: var(--dark);
}

.square.last::after {
  content: '';
  position: absolute;
  inset: 0;
  background: rgba(255, 206, 92, 0.32);
}

.square.selected::after {
  content: '';
  position: absolute;
  inset: 0;
  background: rgba(116, 192, 255, 0.38);
  box-shadow: inset 0 0 0 3px rgba(116, 192, 255, 0.85);
}

.square.check {
  box-shadow: inset 0 0 0 3px rgba(255, 92, 92, 0.95), inset 0 0 34px rgba(255, 60, 60, 0.55);
}

.square.target::before {
  content: '';
  position: absolute;
  left: 50%;
  top: 50%;
  width: 24%;
  height: 24%;
  transform: translate(-50%, -50%);
  border-radius: 50%;
  background: rgba(18, 26, 36, 0.32);
}

.square.target-capture::before {
  content: '';
  position: absolute;
  inset: 7%;
  border-radius: 50%;
  border: calc(var(--cell) * 0.08) solid rgba(18, 26, 36, 0.32);
}

.coord {
  position: absolute;
  font-size: calc(var(--cell) * 0.15);
  font-weight: 600;
  letter-spacing: 0.02em;
  color: rgba(52, 34, 16, 0.42);
  pointer-events: none;
  z-index: 3;
}

.coord-rank {
  top: 3%;
  left: 5%;
}

.coord-file {
  bottom: 2%;
  right: 5%;
}

.square.dark .coord {
  color: rgba(255, 250, 240, 0.55);
}

.pieces {
  position: absolute;
  inset: 0;
  pointer-events: none;
}

.piece {
  position: absolute;
  width: 12.5%;
  height: 12.5%;
  display: grid;
  place-items: center;
  line-height: 1;
  font-size: calc(var(--cell) * 0.8);
  font-family: "Segoe UI Symbol", "Noto Sans Symbols 2", "Apple Symbols", serif;
  will-change: transform;
}

.piece.white {
  color: #fdfaf2;
  -webkit-text-stroke: calc(var(--cell) * 0.03) #3a2f22;
  paint-order: stroke fill;
  filter: drop-shadow(0 3px 4px rgba(0, 0, 0, 0.32));
}

.piece.black {
  color: #16191f;
  -webkit-text-stroke: calc(var(--cell) * 0.014) rgba(255, 255, 255, 0.28);
  paint-order: stroke fill;
  filter: drop-shadow(0 3px 4px rgba(0, 0, 0, 0.35));
}

.piece.grabbed {
  opacity: 0.22;
}

.piece.ghost {
  position: fixed;
  width: var(--cell);
  height: var(--cell);
  transform: translate(-50%, -50%);
  pointer-events: none;
  z-index: 80;
  opacity: 0.95;
  filter: drop-shadow(0 10px 14px rgba(0, 0, 0, 0.45));
}

/* ---------------- 侧栏 ---------------- */

.panel {
  flex: 1 1 280px;
  min-width: 250px;
  height: var(--board-size, auto);
  overflow: hidden;
  background: var(--panel);
  border: 1px solid var(--line);
  border-radius: 14px;
  padding: 18px;
  display: flex;
  flex-direction: column;
  gap: 14px;
  box-shadow: 0 20px 40px rgba(0, 0, 0, 0.3);
}

.turn {
  display: flex;
  align-items: center;
  gap: 10px;
  font-size: 15px;
  font-weight: 600;
}

.turn-dot {
  width: 11px;
  height: 11px;
  border-radius: 50%;
  background: #ffffff;
  box-shadow: 0 0 0 2px rgba(255, 255, 255, 0.18);
}

.turn[data-side='black'] .turn-dot {
  background: #2c3444;
  box-shadow: 0 0 0 2px rgba(255, 255, 255, 0.45);
}

.turn[data-side='draw'] .turn-dot {
  background: #9aa6b8;
  box-shadow: 0 0 0 2px rgba(255, 255, 255, 0.2);
}

.turn.is-check {
  color: #ff8d8d;
}

.turn.is-over {
  color: #ffd479;
}

.field {
  display: flex;
  align-items: center;
  gap: 10px;
  font-size: 13px;
  color: var(--muted);
}

.field select {
  flex: 1;
  font: inherit;
  font-size: 13px;
  color: var(--text);
  background: #1e2632;
  border: 1px solid var(--line);
  border-radius: 9px;
  padding: 8px 10px;
  cursor: pointer;
  appearance: none;
  background-image: linear-gradient(45deg, transparent 50%, #8692a6 50%),
    linear-gradient(135deg, #8692a6 50%, transparent 50%);
  background-position: calc(100% - 16px) 50%, calc(100% - 11px) 50%;
  background-size: 5px 5px, 5px 5px;
  background-repeat: no-repeat;
}

.field select:hover {
  background-color: #26303e;
  border-color: #334154;
}

.moves-wrap {
  position: relative;
  flex: 1 1 auto;
  min-height: 0;
  padding: 6px;
  border-radius: 10px;
  background: rgba(255, 255, 255, 0.025);
  border: 1px solid rgba(255, 255, 255, 0.05);
}

.moves {
  position: absolute;
  inset: 0;
  list-style: none;
  margin: 0;
  padding: 0 4px 0 0;
  overflow-y: auto;
  display: flex;
  flex-direction: column;
  gap: 1px;
  font-variant-numeric: tabular-nums;
  scrollbar-width: thin;
}

.move-row {
  display: grid;
  grid-template-columns: 34px 1fr 1fr;
  gap: 8px;
  padding: 4px 8px;
  border-radius: 7px;
  font-size: 13.5px;
}

.move-row:nth-child(odd) {
  background: rgba(255, 255, 255, 0.035);
}

.move-no {
  color: var(--muted);
}

.move-san {
  font-family: "Cascadia Mono", ui-monospace, SFMono-Regular, monospace;
}

.actions {
  display: flex;
  gap: 8px;
  flex-wrap: wrap;
}

button {
  font: inherit;
  font-size: 13px;
  padding: 9px 14px;
  border-radius: 9px;
  border: 1px solid var(--line);
  background: #1e2632;
  color: var(--text);
  cursor: pointer;
  transition: background 0.15s, border-color 0.15s, transform 0.1s;
}

button:hover {
  background: #26303e;
  border-color: #334154;
}

button:active {
  transform: translateY(1px);
}

button.primary {
  background: linear-gradient(180deg, #3f74d8, #2f57ab);
  border-color: #4a80e6;
}

button.primary:hover {
  background: linear-gradient(180deg, #4a80e6, #345fb8);
}

.online {
  display: grid;
  gap: 10px;
  padding: 12px;
  border-radius: 12px;
  background: rgba(126, 176, 255, 0.06);
  border: 1px solid rgba(126, 176, 255, 0.22);
}

.online[hidden] {
  display: none;
}

.online-head {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 10px;
  font-size: 13px;
  font-weight: 600;
}

.online button.mini {
  padding: 4px 10px;
  font-size: 12px;
  font-weight: 400;
}

.online-line {
  margin: 0;
  font-size: 12.5px;
  line-height: 1.6;
  color: var(--text);
}

.online-line.muted {
  color: var(--muted);
}

.online-line.ok {
  color: #7fd6a3;
}

.online-line.warn {
  color: #ffb26b;
}

.online-code {
  font-family: "Cascadia Mono", ui-monospace, monospace;
  font-size: 20px;
  letter-spacing: 0.22em;
  color: var(--accent);
}

.online-idle {
  display: grid;
  gap: 10px;
}

.online-idle > button {
  width: 100%;
}

.online-info {
  display: grid;
  gap: 6px;
  justify-items: start;
}

.online-info > button {
  margin-top: 2px;
}

/* display:grid 会覆盖 hidden 属性，这里显式兜底 */
.online-idle[hidden],
.online-info[hidden],
.online-code[hidden],
.online-info > button[hidden] {
  display: none;
}

.online-join {
  display: flex;
  gap: 8px;
}

.online-join input {
  flex: 1;
  min-width: 0;
  font: inherit;
  font-size: 13px;
  letter-spacing: 0.12em;
  text-transform: uppercase;
  color: var(--text);
  background: #121821;
  border: 1px solid var(--line);
  border-radius: 9px;
  padding: 8px 10px;
}

.online-join input:focus {
  outline: none;
  border-color: #3f74d8;
}

.io {
  display: flex;
  gap: 8px;
}

.io button {
  flex: 1;
  padding: 7px 8px;
  font-size: 12.5px;
}

.io-text {
  width: 100%;
  min-height: 46px;
  max-height: 140px;
  resize: vertical;
  font-family: "Cascadia Mono", ui-monospace, SFMono-Regular, monospace;
  font-size: 11.5px;
  line-height: 1.5;
  color: var(--text);
  background: #121821;
  border: 1px solid var(--line);
  border-radius: 9px;
  padding: 8px 10px;
}

.io-text:focus {
  outline: none;
  border-color: #3f74d8;
}

.io-status {
  margin: 0;
  min-height: 15px;
  font-size: 11.5px;
  color: var(--muted);
}

.io-status.is-error {
  color: #ff8d8d;
}

/* ---------------- 升变 ---------------- */

.promo {
  position: absolute;
  inset: 0;
  z-index: 20;
  display: grid;
  align-content: center;
  justify-items: center;
  gap: 14px;
  border-radius: 10px;
  background: rgba(8, 11, 16, 0.74);
  backdrop-filter: blur(4px);
}

.promo-card {
  display: grid;
  justify-items: center;
  gap: 14px;
  padding: 22px 26px;
  border-radius: 14px;
  background: rgba(23, 29, 39, 0.92);
  border: 1px solid #2b3546;
  box-shadow: 0 20px 50px rgba(0, 0, 0, 0.5);
}

.promo[hidden] {
  display: none;
}

.promo-title {
  margin: 0;
  font-size: 13px;
  color: #cfd8e6;
  text-align: center;
}

.promo-row {
  display: flex;
  gap: 10px;
  justify-content: center;
}

.promo-btn {
  width: 60px;
  height: 60px;
  padding: 0;
  font-size: 38px;
  line-height: 1;
  display: grid;
  place-items: center;
  font-family: "Segoe UI Symbol", "Noto Sans Symbols 2", serif;
  background: #f1e7d3;
  color: #1b1f27;
  border: 1px solid #d8cbb2;
  border-radius: 11px;
}

.promo-btn:hover {
  background: #ffffff;
  transform: translateY(-2px);
}

/* ---------------- 结算画面 ---------------- */

.result {
  position: absolute;
  inset: 0;
  z-index: 30;
  display: grid;
  place-items: center;
  border-radius: 10px;
  background: rgba(8, 11, 16, 0.72);
  backdrop-filter: blur(3px);
  animation: result-in 0.24s ease-out;
}

.result[hidden] {
  display: none;
}

@keyframes result-in {
  from {
    opacity: 0;
    transform: scale(0.96);
  }
  to {
    opacity: 1;
    transform: scale(1);
  }
}

.result-card {
  position: relative;
  width: min(86%, 320px);
  padding: 28px 26px 22px;
  text-align: center;
  border-radius: 18px;
  background: linear-gradient(180deg, #1c2432, #141a24);
  border: 1px solid #2b3546;
  box-shadow: 0 30px 70px rgba(0, 0, 0, 0.6), inset 0 1px 0 rgba(255, 255, 255, 0.06);
  overflow: hidden;
}

.result-card::before {
  content: '';
  position: absolute;
  inset: -40% 0 auto 0;
  height: 180px;
  background: radial-gradient(closest-side, var(--result-glow, rgba(255, 212, 121, 0.18)), transparent);
  pointer-events: none;
}

.result-badge {
  width: 74px;
  height: 74px;
  margin: 0 auto 14px;
  display: grid;
  place-items: center;
  border-radius: 50%;
  background: rgba(255, 255, 255, 0.04);
  border: 1px solid rgba(255, 255, 255, 0.09);
  box-shadow: 0 10px 24px rgba(0, 0, 0, 0.35);
  position: relative;
}

.result-badge.white {
  background: radial-gradient(circle at 50% 35%, #fdfaf2, #d9cdb8);
  border-color: rgba(255, 255, 255, 0.5);
}

.result-badge.black {
  background: radial-gradient(circle at 50% 35%, #2a303c, #12151c);
  border-color: rgba(255, 255, 255, 0.16);
}

.result-badge.draw {
  background: radial-gradient(circle at 50% 35%, #3a4250, #1d232d);
  border-color: rgba(255, 255, 255, 0.16);
}

.result-mark {
  font-size: 40px;
  line-height: 1;
  font-family: "Segoe UI Symbol", "Noto Sans Symbols 2", serif;
}

.result-badge.white .result-mark {
  color: #26201a;
}

.result-badge.black .result-mark {
  color: #f2f5fa;
}

.result-badge.draw .result-mark {
  font-size: 26px;
  color: #dbe3ef;
  letter-spacing: 0.06em;
}

.result-label {
  margin: 0;
  font-size: 11px;
  letter-spacing: 0.26em;
  text-transform: uppercase;
  color: var(--muted);
}

.result-title {
  margin: 8px 0 6px;
  font-size: 32px;
  letter-spacing: 0.06em;
  color: #ffd479;
  text-shadow: 0 8px 24px rgba(255, 196, 66, 0.25);
}

.result-reason {
  display: inline-block;
  margin: 0 0 10px;
  padding: 3px 10px;
  font-size: 12px;
  color: #e7ecf5;
  background: rgba(255, 255, 255, 0.07);
  border-radius: 999px;
}

.result-meta {
  margin: 0 0 20px;
  font-size: 12px;
  color: var(--muted);
  font-variant-numeric: tabular-nums;
}

.result-actions {
  display: flex;
  gap: 10px;
  justify-content: center;
}

.result-actions button {
  flex: 1;
  min-width: 0;
  padding: 10px 12px;
}

.result-copy {
  margin-top: 12px;
  padding: 4px 8px;
  font-size: 12px;
  color: var(--muted);
  background: none;
  border: none;
  text-decoration: underline;
  text-underline-offset: 3px;
  cursor: pointer;
}

.result-copy:hover {
  color: var(--text);
  background: none;
  border: none;
}

/* ---------------- 响应式 ---------------- */

@media (max-width: 860px) {
  .app {
    padding: 18px 14px 32px;
  }

  .layout {
    flex-direction: column;
    align-items: center;
  }

  .board-wrap {
    width: 100%;
    max-width: 520px;
  }

  .panel {
    width: 100%;
    max-width: 520px;
    height: auto;
  }

  .moves-wrap {
    flex: 0 0 auto;
    height: 176px;
  }

  /* iOS 上输入框字号小于 16px 会在聚焦时自动放大整个页面 */
  .online-join input,
  .io-text,
  .field select {
    font-size: 16px;
  }

  .result-card {
    width: min(88%, 300px);
    padding: 22px 18px 18px;
  }

  .result-badge {
    width: 62px;
    height: 62px;
    margin-bottom: 12px;
  }

  .result-mark {
    font-size: 34px;
  }

  .result-title {
    font-size: 26px;
  }
}
