:root {
  --bg: #0b1220;
  --card: #141d33;
  --line: #26334f;
  --txt: #e6edf7;
  --mut: #8ea4c6;
  --accent: #4da3ff;
  --warn: #fbbf24;
  --good: #34d399;
  --bad: #f87171;
  --orange: #f08019;
  --blue: #2f6bdd;

  /* 手机上一屏放得下 4 列，桌面最多 118px */
  --cell: clamp(56px, 20vw, 118px);
  --gap: clamp(6px, 1.6vw, 12px);
}

* { box-sizing: border-box; -webkit-tap-highlight-color: transparent; }

html, body { height: 100%; }

body {
  margin: 0;
  background: var(--bg);
  color: var(--txt);
  font-family: "Microsoft YaHei UI", "PingFang SC", "Hiragino Sans GB", system-ui, sans-serif;
}

.app {
  display: flex;
  gap: clamp(12px, 2vw, 24px);
  padding: clamp(14px, 2vw, 26px);
  min-height: 100vh;
  align-items: flex-start;
  align-content: flex-start;
  justify-content: center;
  flex-wrap: wrap;
}

/* ---------------- 左：棋盘 ---------------- */

.left {
  flex: 0 0 auto;
  display: flex;
  flex-direction: column;
  align-items: center;
  max-width: 100%;
}

.turn {
  font-size: clamp(19px, 4.6vw, 30px);
  font-weight: 800;
  color: var(--accent);
  margin-bottom: 8px;
  letter-spacing: .5px;
  align-self: flex-start;
  white-space: nowrap;
}
.turn.enemy { color: var(--orange); }
.turn.setup { color: var(--mut); font-size: clamp(15px, 3.6vw, 24px); white-space: normal; }
.turn.win { color: var(--good); }

.board {
  display: grid;
  /* 4 列自适应：格子用 1fr 撑开，永远塞得进容器 */
  grid-template-columns: repeat(4, 1fr);
  gap: var(--gap);
  padding: clamp(5px, 1.2vw, 10px);
  background: #0d1526;
  border: 1px solid var(--line);
  border-radius: 14px;
  width: min(100%, calc(4 * 118px + 3 * 12px + 20px));
  margin: 0 auto;
  touch-action: manipulation;
}

.cell {
  position: relative;
  aspect-ratio: 1 / 1;
  border-radius: 10px;
  border: clamp(2px, .5vw, 3px) solid;
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: center;
  user-select: none;
  -webkit-user-select: none;
  touch-action: manipulation;
}
.cell.blue { background: #12304f; border-color: #2f6fd0; }
.cell.orange { background: #4a2a10; border-color: #c97a1f; }

.cell .coord {
  position: absolute; left: 5px; top: 3px;
  font-family: Consolas, monospace;
  font-size: clamp(8px, 1.6vw, 11px);
  color: #7f93b5;
}

.piece {
  width: 68%;
  height: 68%;
  border-radius: 50%;
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: clamp(14px, 4.4vw, 26px);
  font-weight: 800;
  color: #fff;
  border: clamp(2px, .45vw, 3px) solid;
}
.piece.b { background: var(--blue); border-color: #a9c9ff; }
.piece.o { background: var(--orange); border-color: #ffd7a3; }

.cell.sel::after {
  content: "";
  position: absolute;
  inset: calc(var(--gap) * -.45);
  border: clamp(3px, .5vw, 4px) dashed;
  border-radius: 12px;
  pointer-events: none;
}
.cell.sel.b::after { border-color: var(--blue); }
.cell.sel.o::after { border-color: var(--orange); }

.cell.lastFrom { box-shadow: inset 0 0 0 3px #64748b; }
.cell.lastTo { box-shadow: inset 0 0 0 3px var(--good); }

.dot {
  position: absolute;
  width: clamp(9px, 3.6vw, 16px);
  height: clamp(9px, 3.6vw, 16px);
  border-radius: 50%;
  background: var(--good);
  pointer-events: none;
}
.dot.deploy { background: var(--warn); }

.hint, .moves {
  margin-top: 7px;
  color: var(--mut);
  font-size: clamp(10.5px, 1.5vw, 12.5px);
  text-align: center;
  line-height: 1.6;
}
.moves { color: #6f83a3; }

/* ---------------- 右：控制面板 ---------------- */

.right {
  flex: 0 0 460px;
  max-width: 100%;
  display: flex;
  flex-direction: column;
  gap: 10px;
}

.head { display: flex; align-items: center; justify-content: space-between; gap: 10px; }
.head h1 { font-size: clamp(16px, 2.4vw, 20px); margin: 0; line-height: 1.25; }

.modes { display: grid; grid-template-columns: 1fr 1fr; gap: 4px 10px; }
.modes label {
  display: flex; align-items: center; gap: 7px;
  font-size: clamp(13px, 1.9vw, 15px); font-weight: 700;
  cursor: pointer; padding: 5px 0;
}
.modes input { accent-color: var(--accent); width: 17px; height: 17px; flex: 0 0 auto; }

.row { display: flex; align-items: center; gap: 7px; flex-wrap: wrap; font-size: 13px; }
.label { color: var(--mut); font-weight: 700; }

.chip {
  background: #2a3450; color: #fff; border: 2px solid transparent;
  border-radius: 8px; padding: 7px 12px; font: inherit; font-size: 13px;
  font-weight: 700; cursor: pointer; touch-action: manipulation;
}
.chip:hover { border-color: #4a5b80; }
.chip.on { background: var(--blue); border-color: var(--warn); }

.vsHint { width: 100%; color: var(--mut); font-size: 12px; line-height: 1.7; }

.palette { display: flex; flex-direction: column; gap: 7px; }
.palRow { display: flex; align-items: center; gap: 6px; flex-wrap: wrap; }
.sideTag { width: 20px; font-weight: 800; }
.palBtn {
  width: clamp(38px, 9vw, 44px);
  height: clamp(34px, 8vw, 40px);
  border-radius: 8px; border: 2px solid transparent;
  color: #fff; font-size: 15px; font-weight: 800;
  cursor: pointer; touch-action: manipulation;
}
.palBtn.b { background: var(--blue); }
.palBtn.o { background: var(--orange); }
.palBtn.on { border-color: var(--warn); }
.palBtn.erase { width: 122px; background: #3a4560; font-size: 13px; white-space: nowrap; }
.palHint { color: var(--mut); font-size: 12px; line-height: 1.6; }

.reserve { display: flex; flex-direction: column; gap: 5px; }
.resRow { display: flex; align-items: center; gap: 6px; flex-wrap: wrap; font-size: 13px; }
.resTag { width: 58px; font-weight: 800; flex: 0 0 auto; }
.resBtn {
  min-width: 44px; height: 34px; border-radius: 7px; border: 2px solid transparent;
  color: #fff; font-size: 13px; font-weight: 700; cursor: pointer;
  touch-action: manipulation;
}
.resBtn.b { background: var(--blue); }
.resBtn.o { background: var(--orange); }
.resBtn.empty { background: #2a3450; color: var(--mut); }
.resBtn.on { border-color: var(--warn); }
.resBtn:disabled { opacity: .5; cursor: default; }
.resCount { color: var(--mut); font-size: 12px; }

.playPanel { display: flex; flex-direction: column; gap: 8px; }

.advice {
  background: var(--card);
  border: 1px solid var(--line);
  border-radius: 10px;
  padding: clamp(9px, 1.4vw, 14px) clamp(11px, 1.6vw, 16px);
  min-height: 92px;
}
.advice .big {
  font-size: clamp(16px, 2.6vw, 21px);
  font-weight: 800; line-height: 1.4;
}
.advice .small {
  margin-top: 6px; color: var(--mut);
  font-size: clamp(11.5px, 1.7vw, 12.5px); line-height: 1.65;
  white-space: pre-wrap;
}

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

button {
  background: var(--card); color: var(--mut);
  border: none; border-radius: 8px;
  padding: 8px 13px; min-height: 36px;
  font: inherit; font-size: 13px; cursor: pointer;
  touch-action: manipulation;
}
button:hover { background: #1d2942; color: var(--txt); }
button:active { background: #223052; }
button.good { color: var(--good); }
button b { color: var(--txt); }

.hidden { display: none !important; }

/* ---------------- 弹窗 ---------------- */

.overlay {
  position: fixed; inset: 0; background: #03060cd0;
  display: flex; align-items: center; justify-content: center;
  z-index: 50; padding: 16px;
}
.dialog {
  background: #101a2e;
  border: 1px solid var(--line);
  border-radius: 14px;
  padding: clamp(18px, 4vw, 26px) clamp(18px, 5vw, 30px) clamp(16px, 3vw, 22px);
  min-width: min(320px, 88vw);
  max-width: min(560px, 92vw);
  text-align: center;
  box-shadow: 0 20px 60px #000a;
}
.dlgTitle { font-size: clamp(20px, 5vw, 26px); font-weight: 800; margin-bottom: 6px; }
.dlgTitle.win { color: var(--good); }
.dlgBody { color: var(--mut); font-size: clamp(13px, 3.6vw, 15px); line-height: 1.7; white-space: pre-wrap; }
.dlgBtns { display: flex; gap: 10px; justify-content: center; margin-top: 18px; flex-wrap: wrap; }
.dlgBtns button { font-size: 14px; padding: 10px 18px; }
.dlgFoot { margin-top: 12px; color: #5f7292; font-size: 12px; cursor: pointer; padding: 6px; }
.dlgFoot:hover { color: var(--mut); text-decoration: underline; }

.logDialog { text-align: left; width: min(620px, 92vw); }
.logBody {
  background: #0d1526; border: 1px solid var(--line); border-radius: 8px;
  padding: 12px; height: min(320px, 45vh); overflow: auto; margin: 10px 0 0;
  font: 12px/1.7 Consolas, monospace; color: var(--mut); white-space: pre-wrap;
}

/* ---------------- 窄屏：竖着排 ---------------- */

@media (max-width: 920px) {
  .app {
    flex-direction: column;
    flex-wrap: nowrap;          /* 竖向堆叠时不要再换行，否则宽度会算歪 */
    align-items: stretch;
    justify-content: flex-start;
    align-content: flex-start;
    padding: 10px 10px 28px;
    gap: 14px;
    min-height: 0;
  }
  .left { width: 100%; max-width: 100%; }
  .right { flex: 1 1 auto; width: 100%; max-width: 100%; flex-basis: auto; }
  .board { width: 100%; max-width: 100%; }
  .turn { align-self: center; }
  .hint, .moves { display: none; }
  .advice { min-height: 0; }
}

/* 触屏：按钮再大一点，方便点 */
@media (pointer: coarse) {
  button { min-height: 42px; padding: 10px 15px; }
  .chip { padding: 9px 14px; }
  .palBtn { height: 42px; }
  .resBtn { height: 38px; min-width: 48px; }
  .modes label { padding: 7px 0; }
}

/* 手机横屏：屏幕很矮，改回左右两栏，别往下排 */
@media (min-width: 700px) and (max-height: 560px) {
  .app {
    flex-direction: row;
    flex-wrap: nowrap;
    align-items: flex-start;
    align-content: flex-start;
    justify-content: center;
    padding: 8px;
    gap: 12px;
    min-height: 0;
  }
  .left { width: auto; }
  .right { flex: 1 1 auto; width: auto; max-width: 460px; }
  .board { width: min(52vw, 380px); }
  .turn { font-size: 20px; }
  .hint, .moves { display: none; }
}