:root {
  --bg: #1d1a17;
  --bg-2: #262220;
  --card: #2e2925;
  --card-2: #383230;
  --line: #453d37;
  --text: #ece5da;
  --text-dim: #a89c8c;
  --red: #c0392b;
  --black: #2b2b2b;
  --gold: #c8a55b;
  --accent: #b8894a;
}

* { 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;
}

body {
  background:
    radial-gradient(1200px 700px at 50% -10%, #322b25 0%, transparent 60%),
    var(--bg);
}

.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(--gold);
  text-shadow: 0 1px 0 #000;
}

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

.mode-btn {
  appearance: none;
  border: 0;
  background: transparent;
  color: var(--text-dim);
  padding: 7px 18px;
  border-radius: 7px;
  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; box-shadow: 0 2px 8px rgba(184,137,74,.35); }

/* ---- 主区域 ---- */
.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: 8px;
  background:
    repeating-linear-gradient(90deg, rgba(140,100,50,.045) 0 2px, transparent 2px 7px),
    linear-gradient(160deg, #f4dfb4 0%, #ecd0a0 45%, #e3c290 100%);
  box-shadow:
    0 0 0 1px rgba(0,0,0,.5),
    0 18px 45px rgba(0,0,0,.55),
    inset 0 0 60px rgba(150,105,50,.22);
  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: #8a6636;
  stroke-width: 2;
  stroke-linecap: round;
  fill: none;
  vector-effect: non-scaling-stroke;
}
.board-svg .board-frame {
  stroke: #6d4f28;
  stroke-width: 3;
  fill: none;
  vector-effect: non-scaling-stroke;
}
.board-svg .river-text {
  fill: #96703c;
  font-family: "KaiTi", "STKaiti", "Kaiti SC", serif;
  font-size: 62px;
  letter-spacing: 14px;
  opacity: .75;
}

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

/* ---- 棋子 ----
 * 外层 .piece 只管定位：用 transform 而非 left/top，动画能交给合成层跑，
 * 主线程即使被占住棋子也照样滑到位。
 * 内层 .coin 管外观，这样"被吃缩放"不会跟定位用的 transform 打架。
 */
.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;
}

/* 只在移动过程中提升为合成层；长期挂着会让 32 个棋子的文字发虚 */
.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 #b99a63,
    0 3px 7px rgba(60,35,10,.42),
    inset 0 -2px 5px rgba(150,115,60,.35),
    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;
  /* --cs 由 JS 按棋盘实际尺寸写入，等于一个格子的像素宽度 */
  font-size: calc(var(--cs, 40px) * 0.6);
  line-height: 1;
}

.piece.red { color: var(--red); }
.piece.black { color: #2f3542; }

.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 #b99a63, 0 3px 7px rgba(60,35,10,.42); }
  50%      { box-shadow: 0 0 0 3px #d9483b, 0 0 16px 4px rgba(217,72,59,.6), 0 3px 7px rgba(60,35,10,.42); }
}

/* ---- 提示层 ---- */
.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(214, 168, 62, .30);
  box-shadow: 0 0 0 2px rgba(190, 140, 40, .55);
  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,.55);
  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,.5);
  z-index: 3;
}

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

/* 提示着法：用金色虚线环区分于"可走点" */
.mark.hint-from, .mark.hint-to {
  width: 9.8%;
  aspect-ratio: 1;
  background: transparent;
  box-shadow: 0 0 0 3px rgba(240, 196, 25, .95), 0 0 18px 4px rgba(240, 196, 25, .5);
  z-index: 4;
  animation: hintPulse 1s ease-in-out infinite;
}
@keyframes hintPulse {
  0%, 100% { opacity: .95; }
  50%      { opacity: .45; }
}

/* ---- 侧栏 ---- */
.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: #4d453e; border-radius: 3px; }

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

.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,.75); }
.turn-dot.black { background: #8d99ae; box-shadow: 0 0 10px rgba(141,153,174,.7); }
.turn-dot.over { background: var(--gold); box-shadow: 0 0 10px rgba(200,165,91,.7); }

.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: #f0846f; }
.status-text.think { color: var(--gold); }
.status-text.over { color: var(--gold); font-weight: 600; }

.group {
  background: var(--card);
  border: 1px solid var(--line);
  border-radius: 12px;
  padding: 12px 14px;
  flex: none;
}
.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-2); padding: 4px; border-radius: 9px; }

.seg button {
  flex: 1;
  min-width: 0;
  appearance: none;
  border: 0;
  background: transparent;
  color: var(--text-dim);
  padding: 7px 2px;
  border-radius: 6px;
  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-2);
  color: var(--text);
  padding: 10px;
  border-radius: 9px;
  font-size: 14px;
  cursor: pointer;
  font-family: inherit;
  transition: background .16s, opacity .16s, border-color .16s;
}
.btn:hover:not(:disabled) { background: #46403c; border-color: #5a5148; }
.btn:disabled { opacity: .38; cursor: not-allowed; }
.btn.primary { background: var(--accent); border-color: var(--accent); color: #fff; }
.btn.primary:hover:not(:disabled) { background: #c9974f; border-color: #c9974f; }
.btn.on { border-color: var(--accent); color: var(--gold); background: #3a322b; }

.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: #4d453e; border-radius: 3px; }

.mv {
  display: flex;
  gap: 6px;
  padding: 3px 4px;
  border-radius: 5px;
  line-height: 1.5;
}
.mv:nth-child(odd) { background: rgba(255,255,255,.028); }
.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: #d8b34a; }
.badge.mistake { color: #e08a3c; }
.badge.blunder { color: #ef6a55; background: rgba(239, 106, 85, .14); }
.mv .m.red { color: #e0796a; }
.mv .m.black { color: #9aa7bd; }
.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: #e0796a; }
.eval-side.black { color: #9aa7bd; }
.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: #e0796a; }
.eval-score.black { color: #9aa7bd; }

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

/* 整盘胜率走势图 */
.eval-canvas {
  display: block;
  width: 100%;
  height: 62px;
  margin-top: 9px;
  border-radius: 6px;
  background: rgba(0, 0, 0, .22);
}

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

.score-card {
  background: var(--bg-2);
  border-radius: 9px;
  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: #c0392b; }
.score-card.black { border-left-color: #8d99ae; }

.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: #e0796a; }
.score-card.black .score-side { color: #9aa7bd; }
.score-total { font-size: 24px; font-weight: 700; color: var(--gold); 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: 8px;
  background: var(--bg-2);
  font-size: 12.5px;
  line-height: 1.5;
}
.chat-list::-webkit-scrollbar { width: 5px; }
.chat-list::-webkit-scrollbar-thumb { background: #4d453e; border-radius: 3px; }

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

.chat-bubble {
  max-width: 82%;
  padding: 5px 9px;
  border-radius: 10px;
  word-break: break-word;
  white-space: pre-wrap;
}
.chat-msg.them .chat-bubble {
  background: #3d3733;
  color: var(--text);
  border-bottom-left-radius: 3px;
}
.chat-msg.me .chat-bubble {
  background: var(--accent);
  color: #fff;
  border-bottom-right-radius: 3px;
}
.chat-msg.ai .chat-bubble {
  background: #42332e;
  color: #f0cfc6;
  border-bottom-left-radius: 3px;
  border-left: 2px solid #8c2a1e;
}

.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(--bg-2);
  border: 1px solid var(--line);
  border-radius: 8px;
  color: var(--text);
  font-family: inherit;
  font-size: 12.5px;
  padding: 8px 10px;
}
.chat-input::placeholder { color: #6d6459; }
.chat-input:focus { outline: none; border-color: var(--accent); }

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

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

/* 5 位房号：大字等宽，方便念给对方听 */
.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(--gold);
  background: var(--bg-2);
  border: 1px dashed var(--accent);
  border-radius: 10px;
  padding: 12px 6px;
  margin-bottom: 9px;
  -webkit-user-select: text;
  user-select: text;
}

.room-input {
  flex: 1;
  min-width: 0;
  background: var(--bg-2);
  border: 1px solid var(--line);
  border-radius: 8px;
  color: var(--gold);
  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: #6d6459;
  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: #6fbf73; }
.online-status.err { color: #f0846f; }
.online-status.busy { color: var(--gold); }

/*
 * ---- 窄屏 ----
 * 断点由 JS 统一判定（NARROW_QUERY）后挂在 <html> 上。
 * 不在这里写 @media，是为了避免断点在 CSS 和 JS 各写一份、
 * 两边一旦不同步棋盘就会塌成 0 尺寸。
 */
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; }
/* 给一个确定高度，棋盘尺寸仍由 JS 在同一套逻辑里算 */
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; }
