:root {
  --bg: #f5f6f8;
  --app-bg: radial-gradient(circle at 50% 28%, #fcfdff 0%, #eff2f7 72%);
  --surface: #ffffff;
  --text: #1c2333;
  --text-dim: #6b7385;
  --label: #596172;
  --line: #e9ecf2;
  --line-strong: #d3d9e4;
  --accent: #2f6fed;
  --accent-soft: #e8effd;
  --peer: #eef3fb;
  --same: #d3e2fd;
  --error: #e5484d;
  --error-soft: #fdecec;
  --hint-unit: #fdf7e3;
  --hint: #fdeeb8;
  --hint-line: rgba(214, 143, 0, 0.9);
  --ripple: rgba(47, 111, 237, 0.22);
  --given: #334155;
  --user: #2563eb;
  --key-bg: linear-gradient(180deg, #ffffff, #f6f7fa);
  --shadow: 0 1px 2px rgba(20, 28, 45, 0.06), 0 10px 30px rgba(20, 28, 45, 0.07);
  --shadow-sm: 0 1px 2px rgba(20, 28, 45, 0.08);
  --radius: 14px;
}

/* 深色主题：由 JS 写到 <html data-theme="dark">，不依赖系统设置 */
:root[data-theme="dark"] {
  --bg: #11141b;
  --app-bg: radial-gradient(circle at 50% 24%, #181d27 0%, #0d1016 78%);
  --surface: #1a1f29;
  --text: #e8ecf4;
  --text-dim: #97a1b5;
  --label: #a7b0c2;
  --line: #2e3747;
  --line-strong: #414d63;
  --accent: #6c9bff;
  --accent-soft: #1d2942;
  --peer: #202935;
  --same: #2d405c;
  --error: #ff6b70;
  --error-soft: #3a1f22;
  --hint-unit: #2f2716;
  --hint: #3d3218;
  --hint-line: rgba(255, 193, 71, 0.9);
  --ripple: rgba(108, 155, 255, 0.22);
  --given: #e2e8f0;
  --user: #7ea6ff;
  --key-bg: linear-gradient(180deg, #202834, #1b212b);
  --shadow: none;
  --shadow-sm: none;
}

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

html,
body {
  margin: 0;
  height: 100%;
}

body {
  background: var(--app-bg);
  background-attachment: fixed;
  color: var(--text);
  font-family: system-ui, -apple-system, "PingFang SC", "Hiragino Sans GB", "Microsoft YaHei", sans-serif;
  overscroll-behavior: none;
  -webkit-user-select: none;
  user-select: none;
  touch-action: manipulation;
}

#app {
  display: flex;
  flex-direction: column;
  height: 100vh;
  height: 100dvh;
  max-width: 560px;
  margin: 0 auto;
  padding: env(safe-area-inset-top) 12px env(safe-area-inset-bottom);
}

/* ---------- 顶栏 ---------- */
.topbar {
  flex: 0 0 auto;
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 10px 2px 6px;
}

.badge {
  border: none;
  background: var(--accent-soft);
  color: var(--accent);
  font: inherit;
  font-size: 13px;
  font-weight: 600;
  padding: 7px 14px;
  border-radius: 999px;
  transition: transform 0.1s;
}

.badge:active {
  transform: scale(0.96);
}

.timer {
  flex: 1;
  text-align: center;
  font-size: 15px;
  font-weight: 600;
  color: var(--text-dim);
  font-variant-numeric: tabular-nums;
  letter-spacing: 0.04em;
}

.icon-btn {
  width: 40px;
  height: 40px;
  display: grid;
  place-items: center;
  border: none;
  background: transparent;
  color: var(--text-dim);
  border-radius: 12px;
  transition: background-color 0.12s;
}

.icon-btn:active {
  background: var(--peer);
}

/* 主题按钮：浅色显示月亮，深色显示太阳 */
#theme-btn .icon-sun {
  display: none;
}

:root[data-theme="dark"] #theme-btn .icon-moon {
  display: none;
}

:root[data-theme="dark"] #theme-btn .icon-sun {
  display: block;
}

/* ---------- 棋盘 ---------- */
.stage {
  flex: 1 1 auto;
  display: flex;
  align-items: center;
  justify-content: center;
  min-height: 0;
  padding: 2px 0 6px;
}

.board {
  width: 100%;
  max-width: 460px;
  aspect-ratio: 1;
  display: grid;
  grid-template-columns: repeat(3, minmax(0, 1fr));
  grid-template-rows: repeat(3, minmax(0, 1fr));
  gap: 3px;
  padding: 3px;
  background: var(--line-strong);
  border-radius: 0;
  box-shadow: var(--shadow), inset 0 1px 0 rgba(255, 255, 255, 0.5);
  overflow: hidden;
}

.box {
  display: grid;
  grid-template-columns: repeat(3, minmax(0, 1fr));
  grid-template-rows: repeat(3, minmax(0, 1fr));
  gap: 1px;
  background: var(--line);
  min-width: 0;
  min-height: 0;
}

.cell {
  position: relative;
  display: grid;
  place-items: center;
  background: var(--surface);
  cursor: pointer;
  transition: background-color 0.14s ease;
  overflow: hidden;
  min-width: 0;
  min-height: 0;
}

.cell .val {
  font-size: var(--val-size, 34px);
  line-height: 1;
  font-weight: 500;
  color: var(--user);
  transition: transform 0.1s;
}

.cell.given .val {
  color: var(--given);
  font-weight: 500;
}

.cell.err .val {
  color: var(--error);
}

.cell.err {
  background: var(--error-soft);
}

.cell.peer {
  background: var(--peer);
}

.cell.same {
  background: var(--same);
}

.cell.sel {
  background: var(--accent-soft);
}

.cell.sel::after {
  content: "";
  position: absolute;
  inset: 0;
  box-shadow: inset 0 0 0 2px var(--accent);
  pointer-events: none;
}

/* 提示高亮：整条推理线 + 目标格 */
.cell.hint-unit {
  background: var(--hint-unit);
}

.cell.hint {
  background: var(--hint);
  animation: hintPulse 1.4s ease-in-out infinite;
}

.cell.hint::after {
  display: none;
}

@keyframes hintPulse {
  0%,
  100% {
    box-shadow: inset 0 0 0 2px var(--hint-line);
  }
  50% {
    box-shadow: inset 0 0 0 4px var(--hint-line);
  }
}

.cell.pop .val {
  animation: pop 0.18s ease-out;
}

@keyframes pop {
  0% {
    transform: scale(0.6);
  }
  60% {
    transform: scale(1.12);
  }
  100% {
    transform: scale(1);
  }
}

/* 填错时抖动 */
.cell.shake {
  animation: shake 0.34s cubic-bezier(0.36, 0.07, 0.19, 0.97);
}

@keyframes shake {
  0%,
  100% {
    transform: translateX(0);
  }
  18% {
    transform: translateX(-4px);
  }
  36% {
    transform: translateX(4px);
  }
  54% {
    transform: translateX(-3px);
  }
  72% {
    transform: translateX(3px);
  }
  88% {
    transform: translateX(-1px);
  }
}

/* 完成时按对角线波浪亮起 */
.cell.celebrate {
  animation: celebrateGlow 0.62s ease both;
  animation-delay: var(--delay, 0ms);
}

@keyframes celebrateGlow {
  0%,
  100% {
    background: var(--surface);
  }
  38% {
    background: var(--same);
  }
}

/* 候选数 */
.notes {
  position: absolute;
  inset: 3%;
  display: grid;
  grid-template-columns: repeat(3, 1fr);
  grid-template-rows: repeat(3, 1fr);
  pointer-events: none;
}

.notes i {
  display: grid;
  place-items: center;
  font-style: normal;
  font-size: var(--note-size, 10px);
  line-height: 1;
  color: var(--text-dim);
  opacity: 0;
  transition: opacity 0.1s;
}

.notes i.on {
  opacity: 1;
}

/* 深色下的层次：棋盘和按键都稍微浮起来一点 */
:root[data-theme="dark"] .board {
  box-shadow: 0 0 0 1px rgba(255, 255, 255, 0.045), 0 10px 34px rgba(0, 0, 0, 0.45);
}

:root[data-theme="dark"] .key {
  box-shadow: 0 0 0 1px rgba(255, 255, 255, 0.035);
}

:root[data-theme="dark"] .key:active {
  background: var(--accent-soft);
}

/* ---------- 控制区 ---------- */
.controls {
  position: relative;
  flex: 0 0 auto;
  padding-bottom: 6px;
}

/* 条件满足时浮出的一键收尾按钮 */
.finish-btn {
  position: absolute;
  left: 50%;
  bottom: calc(100% + 8px);
  z-index: 5;
  padding: 9px 16px;
  border: none;
  border-radius: 999px;
  background: var(--accent);
  color: #ffffff;
  font: inherit;
  font-size: 13px;
  font-weight: 600;
  white-space: nowrap;
  box-shadow: 0 6px 18px rgba(47, 111, 237, 0.32);
  opacity: 0;
  transform: translate(-50%, 6px);
  pointer-events: none;
  transition: opacity 0.18s, transform 0.18s;
}

.finish-btn.show {
  opacity: 1;
  transform: translate(-50%, 0);
  pointer-events: auto;
}

.finish-btn:active {
  transform: translate(-50%, 1px) scale(0.97);
}

.numpad {
  display: grid;
  grid-template-columns: repeat(9, 1fr);
  gap: 4px;
  margin-bottom: 8px;
}

.key {
  position: relative;
  overflow: hidden;
  height: 52px;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 1px;
  padding: 0;
  border: none;
  border-radius: var(--radius);
  background: var(--key-bg);
  color: var(--accent);
  font: inherit;
  font-size: 22px;
  font-weight: 600;
  line-height: 1;
  box-shadow: var(--shadow-sm);
  transition: transform 0.08s, background-color 0.12s, opacity 0.15s;
}

.key::after {
  content: "";
  position: absolute;
  inset: 0;
  border-radius: inherit;
  background: radial-gradient(circle at 50% 55%, var(--ripple), transparent 70%);
  opacity: 0;
  transition: opacity 0.24s;
  pointer-events: none;
}

.key:active::after {
  opacity: 1;
  transition: opacity 0s;
}

.key .digit {
  font-size: 22px;
  font-weight: 600;
  line-height: 1;
}

.key .left {
  font-size: 10px;
  font-weight: 600;
  line-height: 1;
  color: var(--label);
  font-variant-numeric: tabular-nums;
  letter-spacing: 0.02em;
}

.key:active {
  transform: scale(0.93);
  background: var(--accent-soft);
}

.key.done {
  opacity: 0.3;
  box-shadow: none;
}

/* 某个数字刚好填满 9 个时的完成反馈 */
.key.just-done {
  animation: keyDone 0.6s ease;
}

@keyframes keyDone {
  0% {
    opacity: 1;
    transform: scale(1);
  }
  35% {
    opacity: 1;
    transform: scale(1.12);
    background: var(--accent-soft);
  }
  100% {
    opacity: 0.3;
    transform: scale(1);
  }
}

.tools {
  display: grid;
  grid-template-columns: repeat(4, 1fr);
  gap: 6px;
}

.tool {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 5px;
  padding: 10px 0 8px;
  border: none;
  border-radius: var(--radius);
  background: transparent;
  color: var(--label);
  font: inherit;
  font-size: 12px;
  font-weight: 500;
  letter-spacing: 0.02em;
  transition: background-color 0.12s, color 0.12s;
}

.tool svg {
  width: 22px;
  height: 22px;
  flex: 0 0 auto;
}

.tool:active {
  background: var(--peer);
}

.tool.on {
  color: var(--accent);
  background: var(--accent-soft);
}

.tool:disabled {
  opacity: 0.35;
}

/* ---------- 提示文字条 ---------- */
.hint-bar {
  position: fixed;
  left: 50%;
  top: calc(env(safe-area-inset-top) + 60px);
  z-index: 40;
  max-width: 86vw;
  padding: 9px 16px;
  border-radius: 999px;
  background: var(--text);
  color: var(--bg);
  font-size: 13px;
  line-height: 1.45;
  text-align: center;
  opacity: 0;
  transform: translateX(-50%) translateY(-6px);
  transition: opacity 0.18s, transform 0.18s;
  pointer-events: none;
}

.hint-bar.show {
  opacity: 1;
  transform: translateX(-50%) translateY(0);
}

/* ---------- 遮罩 / 弹层 ---------- */
.overlay {
  position: fixed;
  inset: 0;
  z-index: 50;
  display: grid;
  place-items: center;
  padding: 24px;
  background: rgba(13, 18, 28, 0.44);
  -webkit-backdrop-filter: blur(6px);
  backdrop-filter: blur(6px);
  animation: fade 0.16s ease-out;
}

.overlay.hidden {
  display: none;
}

/* 开场选难度：整屏不透明，不露出后面的空棋盘 */
.overlay.start-mode {
  background: var(--bg);
  -webkit-backdrop-filter: none;
  backdrop-filter: none;
}

.start-sheet {
  text-align: center;
}

.start-head {
  margin-bottom: 18px;
}

.start-title {
  margin: 6px 0 8px;
  font-size: 32px;
  font-weight: 700;
  letter-spacing: 0.16em;
  text-indent: 0.16em;
}

.start-sub {
  margin: 0;
  font-size: 13px;
  color: var(--text-dim);
}

.diff-item.selected {
  border-color: var(--accent);
  background: var(--accent-soft);
  position: relative;
}

.diff-item.selected::before {
  content: "";
  position: absolute;
  left: 0;
  top: 50%;
  width: 3px;
  height: 56%;
  transform: translateY(-50%);
  border-radius: 0 3px 3px 0;
  background: var(--accent);
}

.diff-item.selected .name {
  color: var(--accent);
}

@keyframes fade {
  from {
    opacity: 0;
  }
}

.sheet {
  width: min(100%, 380px);
  max-height: 90vh;
  overflow-y: auto;
  background: var(--surface);
  border-radius: 22px;
  padding: 22px;
  box-shadow: 0 24px 70px rgba(10, 15, 25, 0.3);
  animation: rise 0.2s cubic-bezier(0.2, 0.9, 0.3, 1.2);
}

@keyframes rise {
  from {
    transform: translateY(14px) scale(0.97);
    opacity: 0;
  }
}

.sheet h2 {
  margin: 0 0 4px;
  font-size: 20px;
  font-weight: 700;
}

.sheet p {
  margin: 0 0 18px;
  font-size: 13px;
  color: var(--text-dim);
  line-height: 1.6;
}

.diff-list {
  display: grid;
  gap: 8px;
}

.diff-item {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  width: 100%;
  padding: 13px 16px;
  border: 1px solid var(--line);
  border-radius: var(--radius);
  background: transparent;
  color: var(--text);
  font: inherit;
  text-align: left;
  transition: background-color 0.12s, border-color 0.12s;
}

.diff-item:active {
  background: var(--peer);
}

.diff-item .name {
  font-size: 15px;
  font-weight: 600;
}

.diff-item .hint {
  font-size: 12px;
  color: var(--text-dim);
}

.sheet-actions {
  display: grid;
  gap: 8px;
  margin-top: 16px;
}

.btn {
  padding: 13px 16px;
  border: none;
  border-radius: var(--radius);
  font: inherit;
  font-size: 15px;
  font-weight: 600;
  background: var(--peer);
  color: var(--text);
  transition: transform 0.1s, opacity 0.15s;
}

.btn:active {
  transform: scale(0.98);
}

.btn.primary {
  background: var(--accent);
  color: #fff;
}

.btn.ghost {
  background: transparent;
  color: var(--text-dim);
  font-weight: 500;
}

.result-stats {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 10px;
  margin: 16px 0 4px;
}

.stat {
  padding: 14px;
  border-radius: var(--radius);
  background: var(--peer);
  text-align: center;
}

.stat .k {
  font-size: 11px;
  color: var(--text-dim);
  margin-bottom: 4px;
}

.stat .v {
  font-size: 20px;
  font-weight: 700;
  font-variant-numeric: tabular-nums;
}

.stat .v.best {
  color: var(--accent);
}

/* 战绩列表 */
.stats-grid {
  display: grid;
  gap: 8px;
  margin: 14px 0 4px;
}

.stat-row {
  padding: 12px 14px;
  border-radius: var(--radius);
  background: var(--peer);
  font-size: 14px;
}

.stat-top {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
}

.stat-name {
  font-weight: 600;
}

.stat-cnt {
  font-size: 12px;
  color: var(--text-dim);
}

.stat-bottom {
  display: flex;
  gap: 18px;
  margin-top: 6px;
  font-size: 12px;
  color: var(--text-dim);
}

.stat-bottom b {
  color: var(--text);
  font-weight: 600;
  font-variant-numeric: tabular-nums;
}

.recent-title {
  margin: 16px 0 6px;
  font-size: 12px;
  color: var(--text-dim);
}

.recent-list {
  display: grid;
  gap: 2px;
}

.recent-row {
  display: grid;
  grid-template-columns: 1fr auto auto;
  gap: 14px;
  padding: 7px 2px;
  font-size: 13px;
  color: var(--text-dim);
  border-bottom: 1px solid var(--line);
}

.recent-row:last-child {
  border-bottom: none;
}

.recent-row span:nth-child(2) {
  color: var(--text);
  font-variant-numeric: tabular-nums;
}

.spinner {
  width: 26px;
  height: 26px;
  margin: 18px auto 6px;
  border: 3px solid var(--line);
  border-top-color: var(--accent);
  border-radius: 50%;
  animation: spin 0.8s linear infinite;
}

@keyframes spin {
  to {
    transform: rotate(360deg);
  }
}

.loading-text {
  text-align: center;
  font-size: 13px;
  color: var(--text-dim);
}

/* 窄屏微调：图标和标签都保留，只收窄间距 */
@media (max-width: 360px) {
  .key {
    height: 48px;
  }
  .key .digit {
    font-size: 20px;
  }
  .key .left {
    font-size: 9px;
  }
  .numpad {
    gap: 4px;
  }
  .tool {
    padding: 9px 0 7px;
    gap: 4px;
  }
  .tool svg {
    width: 20px;
    height: 20px;
  }
  .tool span {
    font-size: 11.5px;
  }
}

/* 尊重系统的「减少动态效果」设置 */
@media (prefers-reduced-motion: reduce) {
  *,
  *::before,
  *::after {
    animation-duration: 0.01ms !important;
    animation-iteration-count: 1 !important;
    transition-duration: 0.01ms !important;
    scroll-behavior: auto !important;
  }
}