:root {
  --bg: #f4f4f6;
  --bg-2: #f1f1f4;
  --card: #ffffff;
  --card-2: #fafafb;
  --line: #ececef;
  --line-2: #dfdfe4;
  --text: #1c1c1e;
  --text-dim: #8a8a90;
  --x: #e5484d;
  --x-ink: #c92a2a;
  --o: #3a7bd5;
  --o-ink: #2b62ad;
  --accent: #1c1c1e;
}

* { 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(900px 520px at 50% -12%, #ffffff 0%, transparent 62%),
    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: 19px;
  font-weight: 600;
  letter-spacing: 5px;
  color: var(--text);
}

.modes {
  display: flex;
  gap: 4px;
  background: var(--card);
  padding: 4px;
  border-radius: 999px;
  border: 1px solid var(--line);
  box-shadow: 0 1px 2px rgba(0,0,0,.03);
}

.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;
}

/*
 * 棋盘是一块纯白的圆角板：不打底纹、不加渐变，只有一层很轻的投影把它从
 * 浅灰底上托起来。15×15 条等距格线，没有棋子位标记。
 * border-radius 由 layout() 按棋盘实际尺寸写入，跟 SVG 外框的圆角同步缩放。
 */
.board {
  position: relative;
  border-radius: 22px;      /* JS 会覆盖；这里只是没跑 JS 时的兜底 */
  background: #ffffff;
  box-shadow:
    0 0 0 1px rgba(28, 28, 30, .05),
    0 1px 2px rgba(28, 28, 30, .04),
    0 14px 38px rgba(28, 28, 30, .10);
  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 {
  stroke: #dcdee4;
  stroke-width: 1.5;
  stroke-linecap: round;
  fill: none;
  vector-effect: non-scaling-stroke;
}
.board-svg .board-frame {
  stroke: #b6b8c2;
  stroke-width: 2.5;
  fill: none;
  vector-effect: non-scaling-stroke;
}

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

/* ---- 棋子 ----
 * 外层 .piece 只管定位：用 transform 而非 left/top，动画能交给合成层跑，
 * 主线程即使被占住棋子也能滑到位。
 * 棋子本身不接指针事件——点格子由棋盘统一处理，免得悬停预览被棋子挡住。
 */
.piece {
  position: absolute;
  left: 0;
  top: 0;
  width: var(--piece, 36px);
  height: var(--piece, 36px);
  transform: translate(-9999px, -9999px);
  transition: transform .22s cubic-bezier(.3, .8, .4, 1);
  pointer-events: none;
  z-index: 2;
}

.piece svg { width: 100%; height: 100%; display: block; overflow: visible; }

.piece .stroke {
  fill: none;
  stroke: currentColor;
  stroke-width: 15;
  stroke-linecap: round;
  stroke-linejoin: round;
  transition: opacity .18s ease;
}

.piece.x { color: var(--x); }
.piece.o { color: var(--o); }

/*
 * 落子时把笔画"画"出来。pathLength="100" 把叉和圈的路径长度都归一到 100，
 * 所以描边动画对两种棋子是同一套数字，不用分别量周长。
 */
.piece.enter .stroke {
  stroke-dasharray: 100;
  animation: drawIn .3s cubic-bezier(.35, .7, .4, 1) both;
}
@keyframes drawIn {
  from { stroke-dashoffset: 100; }
  to   { stroke-dashoffset: 0; }
}

/* 悔棋时棋子淡出 */
.piece.captured .stroke { opacity: 0; }

/* 重排（窗口缩放）时临时关掉过渡，否则棋子会满屏乱飞 */
.no-anim .piece { transition: none !important; }

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

/* 最后一手：淡淡的灰框 */
.mark.last {
  width: calc(var(--cs, 44px) * 0.94);
  aspect-ratio: 1;
  background: rgba(28, 28, 30, .04);
  box-shadow: 0 0 0 2px rgba(28, 28, 30, .20);
  z-index: 1;
}

/* 悬停预览：半透明的 X / O，点下去之前就知道会画在哪一格 */
.mark.ghost {
  width: var(--piece, 36px);
  aspect-ratio: 1;
  opacity: .26;
  z-index: 2;
}
.mark.ghost svg { width: 100%; height: 100%; display: block; }
.mark.ghost .stroke {
  fill: none;
  stroke: currentColor;
  stroke-width: 15;
  stroke-linecap: round;
  stroke-linejoin: round;
}
.mark.ghost.x { color: var(--x); }
.mark.ghost.o { color: var(--o); }

/* 对手已经有四：必须堵的点，红圈闪一闪，相当于象棋里的"被将军" */
.mark.block {
  width: calc(var(--cs, 44px) * 0.94);
  aspect-ratio: 1;
  box-shadow: 0 0 0 3px rgba(229, 72, 77, .9), 0 0 14px 3px rgba(229, 72, 77, .3);
  z-index: 3;
  animation: blockPulse 1s ease-in-out infinite;
}
@keyframes blockPulse {
  0%, 100% { opacity: 1; }
  50%      { opacity: .45; }
}

/* 自己已经有四：这一步落下去就五连，镶个金边 */
.mark.kill {
  width: calc(var(--cs, 44px) * 0.94);
  aspect-ratio: 1;
  box-shadow: 0 0 0 3px rgba(229, 165, 10, .95), 0 0 16px 4px rgba(229, 165, 10, .35);
  z-index: 3;
  animation: blockPulse 1s ease-in-out infinite;
}

/* 终局五连：一直亮着 */
.mark.win {
  width: calc(var(--cs, 44px) * 0.98);
  aspect-ratio: 1;
  background: rgba(229, 165, 10, .18);
  box-shadow: 0 0 0 2px rgba(229, 165, 10, .75), 0 0 16px 4px rgba(229, 165, 10, .28);
  z-index: 4;
  animation: winPulse 1.3s ease-in-out infinite;
}
@keyframes winPulse {
  0%, 100% { background: rgba(229, 165, 10, .18); }
  50%      { background: rgba(229, 165, 10, .34); }
}

/* ---- 侧栏 ---- */
.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: #d8d8de; 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: 0 1px 2px rgba(28,28,30,.03);
}

.turn-dot {
  width: 12px; height: 12px;
  border-radius: 50%;
  flex: none;
  transition: background .2s, box-shadow .2s;
}
.turn-dot.x { background: var(--x); box-shadow: 0 0 0 4px rgba(229,72,77,.14); }
.turn-dot.o { background: var(--o); box-shadow: 0 0 0 4px rgba(58,123,213,.14); }
.turn-dot.over { background: #b6b8c2; box-shadow: 0 0 0 4px rgba(28,28,30,.07); }

.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.alert { color: var(--x-ink); }
.status-text.good { color: #1f8a4c; }
.status-text.think { color: #9a7100; }
.status-text.over { color: var(--text); font-weight: 600; }

.group {
  background: var(--card);
  border: 1px solid var(--line);
  border-radius: 16px;
  padding: 12px 14px;
  flex: none;
  box-shadow: 0 1px 2px rgba(28,28,30,.03);
}
.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: 3px; background: var(--bg-2); padding: 3px; 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, box-shadow .16s;
  white-space: nowrap;
}

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

.seg button:hover { color: var(--text); }
.seg button.active {
  background: #fff;
  color: var(--text);
  font-weight: 500;
  box-shadow: 0 1px 3px rgba(28,28,30,.10), 0 0 0 1px rgba(28,28,30,.04);
}

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

.btn {
  flex: 1;
  appearance: none;
  border: 1px solid var(--line-2);
  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;
}
.btn:hover:not(:disabled) { background: var(--bg-2); border-color: #ceced6; }
.btn:disabled { opacity: .35; cursor: not-allowed; }
.btn.primary { background: var(--accent); border-color: var(--accent); color: #fff; }
.btn.primary:hover:not(:disabled) { background: #333338; border-color: #333338; }
.btn.on { border-color: var(--accent); color: var(--text); background: var(--bg-2); font-weight: 500; }

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

.mv {
  display: flex;
  gap: 6px;
  padding: 3px 4px;
  border-radius: 8px;
  line-height: 1.5;
}
.mv:nth-child(odd) { background: rgba(28,28,30,.025); }
.mv .no { color: var(--text-dim); width: 24px; flex: none; font-variant-numeric: tabular-nums; }
.mv .m {
  flex: 1;
  font-family: ui-monospace, Consolas, "Courier New", monospace;
  font-size: 13px;
  letter-spacing: .5px;
}

/* 着法质量标记 */
.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: 4px;
  vertical-align: 1px;
  letter-spacing: 0;
}
.badge.inaccuracy { color: #a07d10; }
.badge.mistake { color: #c26a15; }
.badge.blunder { color: #c92a2a; background: rgba(229, 72, 77, .10); }
.mv .m.x { color: var(--x-ink); }
.mv .m.o { color: var(--o-ink); }
.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.x { color: var(--x-ink); }
.eval-side.o { color: var(--o-ink); }
.eval-side b { font-size: 13.5px; font-weight: 600; }

.eval-score {
  font-size: 12px;
  font-weight: 600;
  color: var(--text-dim);
  font-variant-numeric: tabular-nums;
}
.eval-score.x { color: var(--x-ink); }
.eval-score.o { color: var(--o-ink); }

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

/* 整盘胜率走势图 */
.eval-canvas {
  display: block;
  width: 100%;
  height: 62px;
  margin-top: 9px;
  border-radius: 12px;
  background: var(--bg-2);
}

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

.score-card {
  background: var(--card-2);
  border-radius: 12px;
  padding: 10px 12px;
  margin-bottom: 8px;
  border: 1px solid var(--line);
  border-left: 3px solid var(--line-2);
}
.score-card:last-child { margin-bottom: 0; }
.score-card.x { border-left-color: var(--x); }
.score-card.o { border-left-color: var(--o); }

.score-head {
  display: flex;
  align-items: baseline;
  justify-content: space-between;
}
.score-side { font-size: 13px; font-weight: 600; }
.score-card.x .score-side { color: var(--x-ink); }
.score-card.o .score-side { color: var(--o-ink); }
.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: 14px;
  background: var(--bg-2);
  font-size: 12.5px;
  line-height: 1.5;
}
.chat-list::-webkit-scrollbar { width: 5px; }
.chat-list::-webkit-scrollbar-thumb { background: #d8d8de; border-radius: 3px; }

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

.chat-bubble {
  max-width: 82%;
  padding: 5px 10px;
  border-radius: 14px;
  word-break: break-word;
  white-space: pre-wrap;
}
.chat-msg.them .chat-bubble {
  background: #fff;
  color: var(--text);
  border: 1px solid var(--line);
  border-bottom-left-radius: 5px;
}
.chat-msg.me .chat-bubble {
  background: var(--accent);
  color: #fff;
  border-bottom-right-radius: 5px;
}

.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-2);
  border-radius: 10px;
  color: var(--text);
  font-family: inherit;
  font-size: 12.5px;
  padding: 8px 10px;
}
.chat-input::placeholder { color: #b0b0b8; }
.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(--text); 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(--text);
  background: var(--card-2);
  border: 1px dashed var(--line-2);
  border-radius: 14px;
  padding: 12px 6px;
  margin-bottom: 9px;
  -webkit-user-select: text;
  user-select: text;
}

.room-input {
  flex: 1;
  min-width: 0;
  background: var(--card);
  border: 1px solid var(--line-2);
  border-radius: 10px;
  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: #b0b0b8;
  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: #1f8a4c; }
.online-status.err { color: var(--x-ink); }
.online-status.busy { color: #9a7100; }

/*
 * ---- 窄屏 ----
 * 断点由 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: 68vh; }
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: 4px; }
