:root {
  --bg: #f4f4f6;
  --card: #fff;
  --card-2: #fafafa;
  --line: #e0e0e2;
  --text: #1c1c1e;
  --text-dim: #8e8e93;
  --red: #c0392b;
  --black: #1c1c1e;
  --gold: #b8894a;
  --accent: #1c1c1e;
  --shadow: 0 2px 8px rgba(28,28,30,.06), 0 1px 2px rgba(28,28,30,.04);
  --shadow-hover: 0 4px 16px rgba(28,28,30,.08), 0 2px 4px rgba(28,28,30,.05);
}

* { box-sizing: border-box; }

html, body {
  margin: 0;
  height: 100%;
  background: var(--bg);
  color: var(--text);
  font-family: "PingFang SC", "Microsoft YaHei", "Hiragino Sans GB", system-ui, -apple-system, sans-serif;
  -webkit-font-smoothing: antialiased;
  overflow: hidden;
}

.app {
  height: 100%;
  display: flex;
  flex-direction: column;
  padding: 14px clamp(12px, 3vw, 28px) 18px;
  gap: 12px;
}

/* ---- 顶栏 ---- */
.topbar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 16px;
  flex-wrap: wrap;
  flex: none;
}

.topbar h1 {
  margin: 0;
  font-size: 20px;
  font-weight: 600;
  letter-spacing: 4px;
  color: var(--text);
}

.modes {
  display: flex;
  gap: 4px;
  background: var(--card);
  border: 1px solid var(--line);
  padding: 4px;
  border-radius: 999px;
}

.mode-btn {
  appearance: none;
  border: 0;
  background: transparent;
  color: var(--text-dim);
  padding: 7px 18px;
  border-radius: 999px;
  font-size: 14px;
  cursor: pointer;
  transition: background .18s, color .18s;
  font-family: inherit;
}
.mode-btn:hover { color: var(--text); }
.mode-btn.active { background: var(--accent); color: #fff; }

/* ---- 主区域 ---- */
.main {
  flex: 1;
  min-height: 0;
  display: flex;
  gap: clamp(14px, 2.5vw, 28px);
  align-items: stretch;
}

.board-area {
  flex: 1;
  min-width: 0;
  min-height: 0;
  display: flex;
  align-items: center;
  justify-content: center;
}

.board-wrap {
  width: 100%;
  height: 100%;
  display: flex;
  align-items: center;
  justify-content: center;
  min-height: 0;
  min-width: 0;
}

.board {
  position: relative;
  border-radius: 12px;
  background:
    repeating-linear-gradient(90deg, rgba(28,28,30,.03) 0 2px, transparent 2px 7px),
    linear-gradient(160deg, #f4dfb4 0%, #ecd0a0 45%, #e3c290 100%);
  box-shadow:
    0 0 0 1px rgba(28,28,30,.08),
    0 12px 32px rgba(28,28,30,.12),
    inset 0 0 60px rgba(28,28,30,.04);
  touch-action: manipulation;
  user-select: none;
  -webkit-user-select: none;
  -webkit-tap-highlight-color: transparent;
}

.board-svg { position: absolute; inset: 0; width: 100%; height: 100%; display: block; }

.board-svg .grid line,
.board-svg .grid path {
  stroke: #c8a56d;
  stroke-width: 2;
  stroke-linecap: round;
  fill: none;
  vector-effect: non-scaling-stroke;
}
.board-svg .board-frame {
  stroke: #b89258;
  stroke-width: 3;
  fill: none;
  vector-effect: non-scaling-stroke;
}
.board-svg .river-text {
  fill: #b89258;
  font-family: "KaiTi", "STKaiti", "Kaiti SC", serif;
  font-size: 62px;
  letter-spacing: 14px;
  opacity: .6;
}

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

/* ---- 棋子 ---- */
.piece {
  position: absolute;
  left: 0;
  top: 0;
  width: var(--piece, 40px);
  height: var(--piece, 40px);
  transform: translate(-9999px, -9999px);
  transition: transform .26s cubic-bezier(.3, .8, .4, 1);
  cursor: pointer;
  pointer-events: auto;
  z-index: 2;
}

.piece.moving { will-change: transform; }
.no-anim .piece { transition: none !important; }

.coin {
  position: relative;
  width: 100%;
  height: 100%;
  border-radius: 50%;
  display: flex;
  align-items: center;
  justify-content: center;
  transition: transform .2s ease, opacity .2s ease;
  background:
    radial-gradient(circle at 34% 28%, #fffdf6 0%, #f6e8c9 42%, #e6d0a4 78%, #d3b988 100%);
  box-shadow:
    0 0 0 1.5px #c8a56d,
    0 3px 7px rgba(28,28,30,.25),
    inset 0 -2px 5px rgba(150,115,60,.3),
    inset 0 2px 4px rgba(255,255,255,.7);
}

.coin::after {
  content: "";
  position: absolute;
  inset: 9%;
  border-radius: 50%;
  border: 1.5px solid currentColor;
  opacity: .5;
}

.coin span {
  position: relative;
  z-index: 1;
  font-family: "KaiTi", "STKaiti", "Kaiti SC", "SimSun", serif;
  font-weight: 700;
  font-size: calc(var(--cs, 40px) * 0.6);
  line-height: 1;
}

.piece.red { color: var(--red); }
.piece.black { color: #1c1c1e; }

.piece.captured { pointer-events: none; }
.piece.captured .coin { opacity: 0; transform: scale(.4); }

.board.is-check .piece[data-code="1"] .coin,
.board.is-check .piece[data-code="-1"] .coin {
  animation: kingPulse .9s ease-in-out infinite;
}

@keyframes kingPulse {
  0%, 100% { box-shadow: 0 0 0 1.5px #c8a56d, 0 3px 7px rgba(28,28,30,.25); }
  50%      { box-shadow: 0 0 0 3px #d9483b, 0 0 16px 4px rgba(217,72,59,.5), 0 3px 7px rgba(28,28,30,.25); }
}

/* ---- 提示层 ---- */
.mark {
  position: absolute;
  transform: translate(-50%, -50%);
  border-radius: 50%;
  pointer-events: none;
  z-index: 1;
}

.mark.last-from, .mark.last-to {
  width: 8.4%;
  aspect-ratio: 1;
  background: rgba(28,28,30,.12);
  box-shadow: 0 0 0 2px rgba(28,28,30,.2);
  z-index: 0;
}

.mark.selected {
  width: 9.6%;
  aspect-ratio: 1;
  box-shadow: 0 0 0 3px #2e8b57, 0 0 16px 3px rgba(46,139,87,.4);
  z-index: 3;
}

.mark.dest {
  width: 2.6%;
  aspect-ratio: 1;
  background: rgba(46, 139, 87, .8);
  box-shadow: 0 0 8px 2px rgba(46,139,87,.4);
  z-index: 3;
}

.mark.capture {
  width: 9.4%;
  aspect-ratio: 1;
  background: transparent;
  box-shadow: 0 0 0 3.5px rgba(200, 60, 45, .75), 0 0 14px 3px rgba(200,60,45,.4);
  z-index: 3;
}

.mark.hint-from, .mark.hint-to {
  width: 9.8%;
  aspect-ratio: 1;
  background: transparent;
  box-shadow: 0 0 0 3px rgba(28,28,30,.9), 0 0 18px 4px rgba(28,28,30,.35);
  z-index: 4;
  animation: hintPulse 1s ease-in-out infinite;
}
@keyframes hintPulse {
  0%, 100% { opacity: .9; }
  50%      { opacity: .4; }
}

/* ---- 侧栏 ---- */
.panel {
  flex: none;
  width: clamp(252px, 26vw, 312px);
  display: flex;
  flex-direction: column;
  gap: 12px;
  min-height: 0;
  overflow-y: auto;
}
.panel::-webkit-scrollbar { width: 6px; }
.panel::-webkit-scrollbar-thumb { background: #d1d1d4; border-radius: 3px; }

.status-card {
  display: flex;
  align-items: center;
  gap: 10px;
  background: var(--card);
  border: 1px solid var(--line);
  border-radius: 16px;
  padding: 13px 15px;
  flex: none;
  box-shadow: var(--shadow);
}

.turn-dot {
  width: 12px; height: 12px;
  border-radius: 50%;
  flex: none;
  transition: background .2s, box-shadow .2s;
}
.turn-dot.red { background: #d9483b; box-shadow: 0 0 10px rgba(217,72,59,.5); }
.turn-dot.black { background: #1c1c1e; box-shadow: 0 0 10px rgba(28,28,30,.5); }
.turn-dot.over { background: var(--gold); box-shadow: 0 0 10px rgba(184,137,74,.5); }

.status-main { min-width: 0; flex: 1; }

.status-text { font-size: 14.5px; font-weight: 500; }

.engine-info {
  font-size: 11px;
  color: var(--text-dim);
  margin-top: 3px;
  font-variant-numeric: tabular-nums;
}
.engine-info[hidden] { display: none; }
.status-text.check { color: #d9483b; }
.status-text.think { color: var(--accent); }
.status-text.over { color: var(--accent); font-weight: 600; }

.group {
  background: var(--card);
  border: 1px solid var(--line);
  border-radius: 16px;
  padding: 12px 14px;
  flex: none;
  box-shadow: var(--shadow);
}
.group[hidden] { display: none; }

.group-title {
  display: block;
  font-size: 11.5px;
  letter-spacing: 1.6px;
  color: var(--text-dim);
  margin-bottom: 9px;
}

.seg { display: flex; gap: 4px; background: var(--bg); padding: 4px; border-radius: 999px; }

.seg button {
  flex: 1;
  min-width: 0;
  appearance: none;
  border: 0;
  background: transparent;
  color: var(--text-dim);
  padding: 7px 2px;
  border-radius: 999px;
  font-size: 12.5px;
  cursor: pointer;
  font-family: inherit;
  transition: background .16s, color .16s;
  white-space: nowrap;
}

#diffSeg button { font-size: 12.5px; }

.seg button:hover { color: var(--text); }
.seg button.active { background: var(--accent); color: #fff; }

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

.btn {
  flex: 1;
  appearance: none;
  border: 1px solid var(--line);
  background: var(--card);
  color: var(--text);
  padding: 10px;
  border-radius: 12px;
  font-size: 14px;
  cursor: pointer;
  font-family: inherit;
  transition: background .16s, opacity .16s, border-color .16s, box-shadow .16s;
}
.btn:hover:not(:disabled) { background: var(--card-2); border-color: #d1d1d4; box-shadow: var(--shadow-hover); }
.btn:disabled { opacity: .38; cursor: not-allowed; }
.btn.primary { background: var(--accent); border-color: var(--accent); color: #fff; }
.btn.primary:hover:not(:disabled) { background: #2a2a2c; border-color: #2a2a2c; }
.btn.on { border-color: var(--accent); color: var(--accent); background: #f5f5f6; }

.moves { flex: 1; min-height: 90px; display: flex; flex-direction: column; }

.move-list {
  flex: 1;
  overflow-y: auto;
  min-height: 0;
  font-size: 13px;
  margin: -2px -4px;
  padding: 2px 4px;
}
.move-list::-webkit-scrollbar { width: 6px; }
.move-list::-webkit-scrollbar-thumb { background: #d1d1d4; border-radius: 3px; }

.mv {
  display: flex;
  gap: 6px;
  padding: 3px 4px;
  border-radius: 6px;
  line-height: 1.5;
}
.mv:nth-child(odd) { background: rgba(28,28,30,.03); }
.mv .no { color: var(--text-dim); width: 24px; flex: none; font-variant-numeric: tabular-nums; }
.mv .m { flex: 1; font-family: "KaiTi", "STKaiti", "Kaiti SC", serif; font-size: 14px; }

.mv .badge {
  font-style: normal;
  font-family: system-ui, sans-serif;
  font-size: 10px;
  font-weight: 700;
  margin-left: 3px;
  padding: 0 2px;
  border-radius: 3px;
  vertical-align: 1px;
}
.badge.inaccuracy { color: #b8894a; }
.badge.mistake { color: #e08a3c; }
.badge.blunder { color: #d9483b; background: rgba(217,72,59,.12); }
.mv .m.red { color: #d9483b; }
.mv .m.black { color: #1c1c1e; }
.mv.empty { color: var(--text-dim); font-size: 12.5px; justify-content: center; }

/* ---- 实时胜率 ---- */
.eval-group { padding: 11px 14px 13px; }

.eval-head {
  display: flex;
  align-items: baseline;
  justify-content: space-between;
  font-size: 12px;
  margin-bottom: 7px;
  font-variant-numeric: tabular-nums;
}
.eval-side.red { color: #d9483b; }
.eval-side.black { color: #1c1c1e; }
.eval-side b { font-size: 13.5px; font-weight: 600; }
.eval-title { font-size: 11px; color: var(--text-dim); letter-spacing: 1px; }

.eval-score {
  font-size: 12px;
  font-weight: 600;
  color: var(--text-dim);
  font-variant-numeric: tabular-nums;
}
.eval-score.red { color: #d9483b; }
.eval-score.black { color: #1c1c1e; }

.eval-track {
  height: 8px;
  border-radius: 4px;
  background: #e8e8ea;
  overflow: hidden;
}
.eval-fill {
  height: 100%;
  width: 50%;
  border-radius: 4px;
  background: linear-gradient(90deg, #b8352a, #d9483b);
  transition: width .45s cubic-bezier(.3, .8, .4, 1);
}

.eval-canvas {
  display: block;
  width: 100%;
  height: 62px;
  margin-top: 9px;
  border-radius: 10px;
  background: var(--bg);
}

/* ---- 对局评分 ---- */
.score-result {
  font-size: 13px;
  color: var(--text);
  font-weight: 600;
  margin-bottom: 10px;
}

.score-card {
  background: var(--bg);
  border-radius: 12px;
  padding: 10px 12px;
  margin-bottom: 8px;
  border-left: 3px solid var(--line);
}
.score-card:last-child { margin-bottom: 0; }
.score-card.red { border-left-color: #d9483b; }
.score-card.black { border-left-color: #1c1c1e; }

.score-head {
  display: flex;
  align-items: baseline;
  justify-content: space-between;
}
.score-side { font-size: 13px; font-weight: 600; }
.score-card.red .score-side { color: #d9483b; }
.score-card.black .score-side { color: #1c1c1e; }
.score-total { font-size: 24px; font-weight: 700; color: var(--text); line-height: 1; }
.score-total small { font-size: 11px; font-weight: 400; margin-left: 2px; color: var(--text-dim); }

.score-grade {
  font-size: 11.5px;
  color: var(--text-dim);
  margin: 3px 0 8px;
}

.score-rows { font-size: 11.5px; }
.score-rows > div {
  display: flex;
  justify-content: space-between;
  padding: 2px 0;
  color: var(--text-dim);
}
.score-rows b { color: var(--text); font-weight: 500; font-variant-numeric: tabular-nums; }

/* ---- 聊天框 ---- */
.chat-group { padding: 11px 14px 12px; }

.chat-group .group-title {
  display: flex;
  align-items: center;
  justify-content: space-between;
  margin-bottom: 8px;
}

.chat-list {
  height: 132px;
  overflow-y: auto;
  display: flex;
  flex-direction: column;
  gap: 6px;
  padding: 8px;
  border-radius: 12px;
  background: var(--bg);
  font-size: 12.5px;
  line-height: 1.5;
}
.chat-list::-webkit-scrollbar { width: 5px; }
.chat-list::-webkit-scrollbar-thumb { background: #d1d1d4; border-radius: 3px; }

.chat-msg { display: flex; }
.chat-msg.me { justify-content: flex-end; }

.chat-bubble {
  max-width: 82%;
  padding: 5px 9px;
  border-radius: 12px;
  word-break: break-word;
  white-space: pre-wrap;
}
.chat-msg.them .chat-bubble {
  background: #f0f0f2;
  color: var(--text);
  border-bottom-left-radius: 4px;
}
.chat-msg.me .chat-bubble {
  background: var(--accent);
  color: #fff;
  border-bottom-right-radius: 4px;
}
.chat-msg.ai .chat-bubble {
  background: #f5f0ee;
  color: #3a3a3c;
  border-bottom-left-radius: 4px;
  border-left: 2px solid var(--red);
}

.chat-sys {
  text-align: center;
  font-size: 11px;
  color: var(--text-dim);
  padding: 2px 0;
}

.chat-input-row { display: flex; gap: 6px; margin-top: 8px; }

.chat-input {
  flex: 1;
  min-width: 0;
  background: var(--card);
  border: 1px solid var(--line);
  border-radius: 12px;
  color: var(--text);
  font-family: inherit;
  font-size: 12.5px;
  padding: 8px 10px;
}
.chat-input::placeholder { color: #b0b0b3; }
.chat-input:focus { outline: none; border-color: var(--accent); }

.chat-send { flex: none; padding: 8px 14px; font-size: 12.5px; border-radius: 12px; }

/* ---- 联机对战 ---- */
.online-hint {
  font-size: 12px;
  color: var(--text-dim);
  line-height: 1.65;
  margin: 0 0 8px;
}
.online-hint b { color: var(--text); font-weight: 600; }

.room-code {
  font-family: ui-monospace, Consolas, "Courier New", monospace;
  font-size: 34px;
  font-weight: 700;
  letter-spacing: 9px;
  text-indent: 9px;
  text-align: center;
  color: var(--text);
  background: var(--card);
  border: 1px dashed var(--line);
  border-radius: 16px;
  padding: 12px 6px;
  margin-bottom: 9px;
  -webkit-user-select: text;
  user-select: text;
  box-shadow: var(--shadow);
}

.room-input {
  flex: 1;
  min-width: 0;
  background: var(--card);
  border: 1px solid var(--line);
  border-radius: 12px;
  color: var(--text);
  font-family: ui-monospace, Consolas, "Courier New", monospace;
  font-size: 21px;
  font-weight: 700;
  letter-spacing: 6px;
  text-indent: 6px;
  text-align: center;
  padding: 8px 6px;
}
.room-input::placeholder {
  color: #b0b0b3;
  font-size: 13px;
  font-weight: 400;
  letter-spacing: 0;
  font-family: inherit;
}
.room-input:focus { outline: none; border-color: var(--accent); }

.online-step { margin-bottom: 12px; }
.online-step:last-child { margin-bottom: 0; }
.online-step[hidden] { display: none; }

.online-status {
  font-size: 12.5px;
  line-height: 1.6;
  margin-top: 10px;
  color: var(--text-dim);
}
.online-status.ok { color: #34c759; }
.online-status.err { color: #d9483b; }
.online-status.busy { color: var(--gold); }

/* ---- 窄屏 ---- */
html.is-narrow, html.is-narrow body { overflow: auto; }
html.is-narrow .app { height: auto; min-height: 100%; }
html.is-narrow .main { flex-direction: column; }
html.is-narrow .board-area { flex: none; }
html.is-narrow .board-wrap { height: 62vh; }
html.is-narrow .panel { width: 100%; }
html.is-narrow .moves { min-height: 130px; max-height: 200px; }
html.is-narrow .topbar h1 { font-size: 17px; letter-spacing: 3px; }