/* ===== 主题变量 ===== */
:root {
  --bg: #f4f6fb;
  --panel: #ffffff;
  --text: #1f2733;
  --muted: #6b7686;
  --line: #c7cdd6;
  --line-bold: #4a5160;
  --given: #1f2733;
  --user: #2d6cdf;
  --cell-bg: #ffffff;
  --peer: #e8eefb;          /* 行列宫辅助高亮 */
  --same: #cfe0ff;          /* 同值高亮 */
  --selected: #aecbff;      /* 选中格 */
  --conflict: #ffd9d9;      /* 冲突 */
  --conflict-text: #d12c2c;
  --accent: #2d6cdf;
  --btn: #eef1f7;
  --cand: #1f9d57;          /* 候选数 / 候选模式绿色主题 */
  --cand-soft: #d8f0e2;
  --shadow: 0 4px 16px rgba(0, 0, 0, .08);
}

body.dark {
  --bg: #11151c;
  --panel: #1b212b;
  --text: #e6e9ef;
  --muted: #9aa4b2;
  --line: #38404d;
  --line-bold: #8b95a6;
  --given: #e6e9ef;
  --user: #6ea8ff;
  --cell-bg: #1b212b;
  --peer: #232c3a;
  --same: #2b3d5e;
  --selected: #355289;
  --conflict: #5a2530;
  --conflict-text: #ff8a8a;
  --accent: #6ea8ff;
  --btn: #262d39;
  --cand: #4fcf8a;
  --cand-soft: #20392c;
  --shadow: 0 4px 16px rgba(0, 0, 0, .4);
}

* { box-sizing: border-box; }

body {
  margin: 0;
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", "PingFang SC",
    "Microsoft YaHei", sans-serif;
  background: var(--bg);
  color: var(--text);
  -webkit-tap-highlight-color: transparent;
  transition: background .25s, color .25s;
}

.app {
  max-width: 520px;
  margin: 0 auto;
  padding: 12px 12px 32px;
}

/* ===== 顶部栏 ===== */
.topbar {
  display: flex;
  align-items: center;
  gap: 8px;
  flex-wrap: wrap;
  margin-bottom: 10px;
}
.topbar h1 {
  font-size: 20px;
  margin: 0;
  flex: 1 1 auto;
}
.topbar .info {
  display: flex;
  align-items: center;
  gap: 10px;
}
#timer {
  font-variant-numeric: tabular-nums;
  font-size: 18px;
  font-weight: 600;
  min-width: 52px;
  text-align: center;
}
#mistakes { font-size: 13px; color: var(--conflict-text); min-width: 0; }

select, .ctrl-btn, .pad-btn, .icon-btn {
  font-family: inherit;
  cursor: pointer;
  border: 1px solid var(--line);
  background: var(--panel);
  color: var(--text);
  border-radius: 10px;
}

.settings {
  display: flex;
  align-items: center;
  gap: 14px;
  flex-wrap: wrap;
  margin-bottom: 10px;
  font-size: 13px;
  color: var(--muted);
}
.settings label { display: inline-flex; align-items: center; gap: 5px; cursor: pointer; }
.settings select { padding: 6px 8px; font-size: 13px; }
.icon-btn { padding: 6px 12px; font-size: 13px; }

/* ===== 棋盘 ===== */
.board-wrap { position: relative; }

#board {
  display: grid;
  grid-template-columns: repeat(9, 1fr);
  aspect-ratio: 1 / 1;
  width: 100%;
  background: var(--line-bold);
  border: 3px solid var(--line-bold);
  border-radius: 8px;
  overflow: hidden;
  touch-action: manipulation;
}

.cell {
  position: relative;
  display: flex;
  align-items: center;
  justify-content: center;
  background: var(--cell-bg);
  border: 0;
  border-right: 1px solid var(--line);
  border-bottom: 1px solid var(--line);
  font-size: clamp(18px, 6vw, 30px);
  font-weight: 500;
  color: var(--user);
  padding: 0;
  transition: background .12s;
  user-select: none;
}
/* 3x3 宫格粗边界 */
.cell.bl { border-left: 2px solid var(--line-bold); }
.cell.bt { border-top: 2px solid var(--line-bold); }
.cell.bl:not(.br) { margin-left: -1px; }

.cell.given { color: var(--given); font-weight: 700; }
.cell.user { color: var(--user); }

/* 高亮分层（CSS 顺序即视觉叠加优先级，靠后覆盖靠前） */
.cell.peer { background: var(--peer); }
.cell.same { background: var(--same); }
.cell.conflict { background: var(--conflict); color: var(--conflict-text); }
.cell.selected {
  background: var(--selected);
  box-shadow: inset 0 0 0 2px var(--accent);
  z-index: 1;
}
/* 色弱友好：冲突除颜色外加下划虚线纹理 */
.cell.conflict::after {
  content: "";
  position: absolute;
  left: 18%; right: 18%; bottom: 14%;
  border-bottom: 2px dotted var(--conflict-text);
}
.cell.flash-error {
  animation: flash 0.4s ease-in-out 0s 3;
}
@keyframes flash {
  0%, 100% { background: var(--cell-bg); }
  50% { background: var(--conflict); }
}

/* ===== 暂停遮罩 ===== */
.overlay {
  position: absolute;
  inset: 0;
  display: none;
  align-items: center;
  justify-content: center;
  flex-direction: column;
  gap: 8px;
  background: rgba(20, 25, 35, .82);
  color: #fff;
  border-radius: 8px;
  font-size: 18px;
  cursor: pointer;
  z-index: 5;
}
.overlay.show { display: flex; }
.overlay .hint { font-size: 13px; opacity: .8; }

/* ===== 候选数缩微九宫格 ===== */
.cand {
  display: grid;
  grid-template-columns: repeat(3, 1fr);
  grid-template-rows: repeat(3, 1fr);
  width: 100%;
  height: 100%;
  font-style: normal;
}
.cand i {
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: clamp(8px, 2.3vw, 12px);
  font-weight: 600;
  line-height: 1;
  color: var(--cand);
  font-style: normal;
}

/* ===== 数字面板 + 模式切换行 ===== */
.pad-row {
  display: flex;
  gap: 6px;
  margin-top: 12px;
  align-items: stretch;
}
#numpad {
  display: grid;
  grid-template-columns: repeat(9, 1fr);
  gap: 6px;
  flex: 1 1 auto;
}

.mode-btn {
  flex: 0 0 auto;
  width: 62px;
  font-size: 13px;
  font-weight: 600;
  line-height: 1.25;
  padding: 4px 6px;
  color: var(--text);
  transition: background .15s, color .15s, border-color .15s;
}
.mode-btn.on {
  background: var(--cand);
  color: #fff;
  border-color: var(--cand);
}

/* 候选模式：数字面板统一绿色主题（FR-C02） */
body.candidate-mode .pad-btn {
  color: var(--cand);
  border-color: var(--cand);
}
body.candidate-mode .pad-btn.active {
  background: var(--cand-soft);
}
body.candidate-mode .pad-btn.disabled {
  color: var(--muted);
  border-color: var(--line);
}
.pad-btn {
  position: relative;
  aspect-ratio: 1 / 1.15;
  font-size: clamp(18px, 5.5vw, 26px);
  font-weight: 600;
  color: var(--accent);
  display: flex;
  align-items: center;
  justify-content: center;
  transition: transform .08s, background .15s;
}
.pad-btn:active { transform: scale(.94); }
.pad-btn.active { background: var(--same); border-color: var(--accent); }
.pad-btn.disabled {
  color: var(--muted);
  opacity: .4;
  cursor: not-allowed;
  background: var(--btn);
}
/* 剩余可填数角标 */
.pad-btn::after {
  content: attr(data-remain);
  position: absolute;
  top: 3px; right: 5px;
  font-size: 10px;
  font-weight: 400;
  color: var(--muted);
}
.pad-btn.disabled::after { content: ""; }

/* ===== 控制栏 ===== */
.controls {
  display: grid;
  grid-template-columns: repeat(3, 1fr);
  gap: 8px;
  margin-top: 12px;
}
.ctrl-btn {
  padding: 12px 8px;
  font-size: 14px;
  font-weight: 500;
  transition: transform .08s, background .15s;
}
.ctrl-btn:active { transform: scale(.97); }
.ctrl-btn.primary { background: var(--accent); color: #fff; border-color: var(--accent); }

#status {
  margin-top: 10px;
  min-height: 20px;
  font-size: 13px;
  color: var(--muted);
  text-align: center;
}

/* ===== 弹窗 ===== */
.modal {
  position: fixed;
  inset: 0;
  display: none;
  align-items: center;
  justify-content: center;
  background: rgba(0, 0, 0, .45);
  z-index: 20;
  padding: 20px;
}
.modal.show { display: flex; }
.modal-card {
  background: var(--panel);
  color: var(--text);
  border-radius: 16px;
  padding: 24px;
  width: 100%;
  max-width: 340px;
  box-shadow: var(--shadow);
  text-align: center;
}
.modal-card h2 { margin: 0 0 12px; }
.modal-card .stats { margin: 12px 0 20px; line-height: 1.9; font-size: 15px; }
.modal-card .stats b { color: var(--accent); }
.modal-actions { display: flex; gap: 10px; }
.modal-actions .ctrl-btn { flex: 1; }

/* ===== 响应式：横屏并排 ===== */
@media (min-width: 760px) and (orientation: landscape) {
  .app { max-width: 900px; }
}
