/* ============ 基础 ============ */
* { box-sizing: border-box; -webkit-tap-highlight-color: transparent; }
:root {
  --primary: #4f46e5;
  --primary-dark: #4338ca;
  --primary-soft: #eef0ff;
  --text: #1f2430;
  --muted: #7a8197;
  --line: #d9deea;
  --thick: #333c52;
  --bg: #eef1f8;
  --card: #ffffff;
  --danger: #dc2626;
  --radius: 16px;
}
html, body {
  margin: 0; padding: 0;
  background: var(--bg);
  color: var(--text);
  font-family: -apple-system, BlinkMacSystemFont, "PingFang SC", "HarmonyOS Sans SC",
               "Microsoft YaHei", "Segoe UI", Roboto, sans-serif;
  -webkit-font-smoothing: antialiased;
  overscroll-behavior: contain;
}
button { font-family: inherit; cursor: pointer; border: none; background: none; }
.app {
  max-width: 480px;
  margin: 0 auto;
  min-height: 100vh;
  min-height: 100dvh;
  padding: env(safe-area-inset-top) 16px env(safe-area-inset-bottom);
}
.screen { animation: fade .2s ease; }
@keyframes fade { from { opacity: 0; transform: translateY(4px); } to { opacity: 1; transform: none; } }

/* ============ 开始页 ============ */
.hero { text-align: center; padding: 34px 0 18px; }
.hero h1 {
  margin: 0; font-size: 40px; letter-spacing: 8px;
  background: linear-gradient(135deg, #6366f1, #8b5cf6);
  -webkit-background-clip: text; background-clip: text; color: transparent;
}
.hero p { margin: 8px 0 0; color: var(--muted); font-size: 13px; }

.card {
  background: var(--card);
  border-radius: var(--radius);
  padding: 16px;
  margin-bottom: 14px;
  box-shadow: 0 4px 18px rgba(31, 36, 48, .06);
}
.card-title { margin: 0 0 12px; font-size: 15px; }
.tip { font-size: 12px; color: var(--muted); font-weight: normal; margin: 10px 0 0; }

.segmented {
  display: grid;
  grid-template-columns: repeat(4, 1fr);
  gap: 8px;
}
.segmented button {
  padding: 12px 0;
  border-radius: 12px;
  font-size: 15px;
  font-weight: 600;
  color: var(--muted);
  background: #f1f3f9;
  transition: all .15s;
}
.segmented button.active {
  background: var(--primary);
  color: #fff;
  box-shadow: 0 4px 12px rgba(79, 70, 229, .35);
}

/* 开关 */
.switch-row {
  display: flex; align-items: center;
  padding: 11px 2px;
  border-bottom: 1px solid #f0f2f7;
  font-size: 15px;
  position: relative;
}
.switch-row:last-of-type { border-bottom: none; }
.switch-row > span:first-child { display: flex; flex-direction: column; flex: 1; }
.switch-row small { color: var(--muted); font-size: 11px; margin-top: 2px; }
.switch-row input { position: absolute; opacity: 0; width: 100%; height: 100%; }
.switch {
  width: 46px; height: 27px; border-radius: 999px;
  background: #d3d8e6; position: relative; transition: background .18s; flex: none;
}
.switch::after {
  content: ""; position: absolute; top: 3px; left: 3px;
  width: 21px; height: 21px; border-radius: 50%;
  background: #fff; box-shadow: 0 1px 4px rgba(0,0,0,.25);
  transition: left .18s;
}
.switch-row input:checked + .switch { background: var(--primary); }
.switch-row input:checked + .switch::after { left: 22px; }

/* 继续对局 */
.continue-card {
  display: flex; align-items: center; gap: 10px;
  border: 1.5px dashed #c7cdf5;
  background: var(--primary-soft);
}
.continue-info { flex: 1; display: flex; flex-direction: column; gap: 3px; }
.continue-info small { color: var(--muted); font-size: 12px; }
.continue-actions { display: flex; gap: 8px; flex: none; }
.btn-small {
  background: var(--primary); color: #fff; font-weight: 600;
  padding: 9px 18px; border-radius: 10px; font-size: 14px;
}
.best-line { text-align: center; color: var(--muted); font-size: 13px; margin: 4px 0 12px; min-height: 18px; }
.best-line b { color: var(--primary-dark); }

.btn-primary {
  width: 100%;
  padding: 15px 0;
  border-radius: 14px;
  background: linear-gradient(135deg, #6366f1, #7c3aed);
  color: #fff; font-size: 17px; font-weight: 700; letter-spacing: 4px;
  box-shadow: 0 8px 20px rgba(99, 102, 241, .35);
  transition: transform .1s;
}
.btn-primary:active { transform: scale(.98); }
.btn-primary:disabled { opacity: .45; box-shadow: none; }
.foot-tip { text-align: center; color: #a0a6b8; font-size: 11px; margin: 14px 0 0; }

/* ============ 游戏页 ============ */
.game-top {
  display: flex; align-items: center; gap: 10px;
  padding: 12px 0 10px;
}
.icon-btn {
  width: 42px; height: 42px; flex: none;
  border-radius: 12px; background: #fff;
  font-size: 24px; color: var(--primary-dark); line-height: 1;
  box-shadow: 0 2px 8px rgba(31,36,48,.08);
}
.icon-btn:active { background: #f0f1f8; }
.game-meta { flex: 1; display: flex; align-items: baseline; justify-content: space-between; padding: 0 4px; }
#meta-config { font-size: 15px; font-weight: 600; }
.timer {
  font-variant-numeric: tabular-nums;
  font-size: 20px; font-weight: 700; color: var(--primary-dark);
  letter-spacing: 1px;
}

.board-wrap {
  display: flex; justify-content: center;
  padding: 6px 0 14px;
}
#board {
  width: min(94vw, 448px);
  aspect-ratio: 1 / 1;
  display: grid;
  border: 2.5px solid var(--thick);
  border-radius: 10px;
  overflow: hidden;
  background: #fff;
  box-shadow: 0 8px 24px rgba(31,36,48,.10);
  touch-action: manipulation;
}
.cell {
  position: relative;
  display: flex; align-items: center; justify-content: center;
  background: #fff;
  font-weight: 600;
  color: var(--primary);
  padding: 0;
  border-radius: 0;
  user-select: none; -webkit-user-select: none;
}
.cell.given { color: #111827; font-weight: 800; }
.cell.peer { box-shadow: inset 0 0 0 999px rgba(79, 70, 229, .07); }
.cell.same { box-shadow: inset 0 0 0 999px rgba(79, 70, 229, .16); }
.cell.selected { box-shadow: inset 0 0 0 3px var(--primary), inset 0 0 0 999px rgba(79, 70, 229, .10); }
.cell.conflict { color: var(--danger) !important; }
.cell.conflict::after {
  content: ""; position: absolute; inset: 0;
  box-shadow: inset 0 0 0 999px rgba(220, 38, 38, .13);
  pointer-events: none;
}

/* 数字键盘 */
.pad-remain {
  text-align: center; font-size: 12px; color: var(--muted);
  min-height: 16px; margin-bottom: 8px;
}
.numpad {
  display: grid;
  gap: 6px;
  width: min(94vw, 448px);
  margin: 0 auto;
}
.numpad button {
  aspect-ratio: 1 / 1.05;
  border-radius: 12px;
  background: #fff;
  font-size: 22px;
  font-weight: 700;
  color: var(--primary-dark);
  box-shadow: 0 2px 8px rgba(31,36,48,.08);
  display: flex; flex-direction: column; align-items: center; justify-content: center;
  transition: transform .08s, background .1s;
}
.numpad button:active { transform: scale(.93); background: var(--primary-soft); }
.numpad button.done { color: #b6bccb; }
.numpad button small { font-size: 10px; font-weight: 500; color: var(--muted); margin-top: 1px; }
.tools { display: flex; justify-content: center; margin-top: 12px; }
.tool-btn {
  padding: 10px 26px; border-radius: 12px;
  background: #fff; color: var(--danger);
  font-size: 15px; font-weight: 600;
  box-shadow: 0 2px 8px rgba(31,36,48,.08);
}

/* ============ 遮罩 / 弹窗 ============ */
.overlay {
  position: fixed; inset: 0; z-index: 50;
  background: rgba(20, 24, 38, .45);
  display: flex; align-items: center; justify-content: center;
  padding: 24px; backdrop-filter: blur(3px);
}
.overlay[hidden] { display: none; }
.loading-card {
  background: #fff; border-radius: 18px; padding: 28px 34px;
  display: flex; flex-direction: column; align-items: center; gap: 14px;
}
.spinner {
  width: 38px; height: 38px; border-radius: 50%;
  border: 4px solid #e0e3f2; border-top-color: var(--primary);
  animation: spin .8s linear infinite;
}
@keyframes spin { to { transform: rotate(360deg); } }
.loading-card p { margin: 0; color: var(--muted); font-size: 14px; }

.win-card {
  background: #fff; border-radius: 22px; padding: 30px 26px 24px;
  width: 100%; max-width: 320px; text-align: center;
  animation: pop .25s ease;
}
@keyframes pop { from { transform: scale(.85); opacity: 0; } to { transform: none; opacity: 1; } }
.win-badge { font-size: 52px; line-height: 1; }
.win-card h2 { margin: 10px 0 6px; font-size: 24px; }
.win-time { margin: 0; font-size: 40px; font-weight: 800; color: var(--primary-dark); font-variant-numeric: tabular-nums; }
.win-best { margin: 8px 0 20px; font-size: 14px; color: var(--muted); min-height: 20px; }
.win-best.new { color: #d97706; font-weight: 700; }
.win-actions { display: flex; flex-direction: column; gap: 10px; }
.btn-ghost {
  padding: 9px 16px; border-radius: 10px; font-size: 14px;
  color: var(--primary-dark); background: #fff;
  border: 1.5px solid #c8cdf0;
}
.btn-ghost-dark {
  padding: 14px 0; border-radius: 12px; font-size: 15px; font-weight: 600;
  color: var(--muted); background: #f1f3f9; width: 100%;
}
