:root {
  color-scheme: dark;
  --bg: #0f0f23;
  --panel: #16213e;
  --cell: #1a1a3e;
  --bar: #1a1a2e;
  --line: #2a2a4e;
  --text: #e0e0e0;
  --muted: #8f8fb0;
  --accent: #e94560;
  --gold: #fbbf24;
  --good: #4ade80;
  --bad: #ef4444;
  --grid-width: min(100vw - 32px, 448px);
  --tab-height: 56px;
}

* { box-sizing: border-box; }

[hidden] { display: none !important; }

html { -webkit-text-size-adjust: 100%; }

body {
  margin: 0;
  min-height: 100vh;
  min-height: 100dvh;
  background: var(--bg);
  color: var(--text);
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, "PingFang SC", "Microsoft YaHei", sans-serif;
  -webkit-tap-highlight-color: transparent;
}

h1, h2, p, ol { margin: 0; padding: 0; }

button {
  border: 0;
  background: none;
  color: inherit;
  font: inherit;
  cursor: pointer;
  touch-action: manipulation;
}

button:focus-visible, a:focus-visible {
  outline: 2px solid var(--gold);
  outline-offset: 2px;
}

.container {
  display: flex;
  flex-direction: column;
  align-items: center;
  width: 100%;
  max-width: 480px;
  margin: 0 auto;
  padding: 12px 16px calc(var(--tab-height) + 20px + env(safe-area-inset-bottom));
}

.header { text-align: center; margin: 8px 0 4px; animation: fade-in .4s ease; }

.title {
  font-size: 26px;
  font-weight: 700;
  letter-spacing: 2px;
  background: linear-gradient(135deg, #ff7a90, var(--accent));
  -webkit-background-clip: text;
  background-clip: text;
  color: transparent;
}

.subtitle { margin-top: 2px; font-size: 12px; color: var(--muted); letter-spacing: 4px; }

.size-bar {
  display: flex;
  gap: 6px;
  width: 100%;
  max-width: 340px;
  margin: 10px 0;
  padding: 5px;
  background: var(--panel);
  border-radius: 10px;
}

.size-btn {
  flex: 1;
  min-height: 34px;
  border-radius: 7px;
  font-size: 13px;
  font-weight: 500;
  color: var(--muted);
  transition: background-color .2s ease, color .2s ease;
}

.size-btn[aria-pressed="true"] {
  background: var(--accent);
  color: #fff;
  font-weight: 700;
  box-shadow: 0 2px 8px rgba(233, 69, 96, .4);
}

.timer-area { display: flex; align-items: baseline; gap: 4px; margin: 4px 0 2px; }

.timer {
  font-size: 42px;
  font-weight: 800;
  font-variant-numeric: tabular-nums;
  transition: color .2s;
}

.timer.running { color: var(--accent); text-shadow: 0 0 10px rgba(233, 69, 96, .3); }

.timer-unit { font-size: 14px; color: var(--muted); }

.progress {
  display: flex;
  align-items: center;
  gap: 10px;
  width: 100%;
  height: 18px;
}

.progress-track {
  flex: 1;
  height: 6px;
  background: var(--panel);
  border-radius: 3px;
  overflow: hidden;
}

.progress-fill {
  width: 0;
  height: 100%;
  background: linear-gradient(90deg, var(--accent), #ff7a90);
  border-radius: 3px;
  transition: width .2s ease;
}

.progress-text {
  min-width: 48px;
  text-align: right;
  font-size: 12px;
  color: var(--muted);
  font-variant-numeric: tabular-nums;
}

.hint {
  display: flex;
  align-items: center;
  justify-content: center;
  height: 18px;
  gap: 4px;
  margin: 6px 0 10px;
  font-size: 14px;
  color: var(--muted);
}

.next-num { font-size: 18px; font-weight: 800; color: var(--accent); }

.grid {
  display: grid;
  grid-template-columns: repeat(var(--n), 1fr);
  gap: 4px;
  width: var(--grid-width);
  padding: 4px;
  background: var(--panel);
  border-radius: 10px;
  box-shadow: 0 4px 16px rgba(0, 0, 0, .3);
  user-select: none;
  -webkit-user-select: none;
}

.cell {
  aspect-ratio: 1;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 0;
  background: var(--cell);
  border-radius: 7px;
  font-size: clamp(14px, calc(var(--grid-width) / var(--n) * .36), 28px);
  font-weight: 700;
  color: #c8c8d8;
  transition: background-color .15s ease, transform .1s ease, opacity .15s ease;
}

.cell:active { transform: scale(.95); }

@media (hover: hover) {
  .cell:not(.correct):hover { background: #232350; }
}

.cell span { transition: font-size .15s ease; }

.cell.correct {
  background: #0d2818;
  color: var(--good);
  opacity: .55;
  animation: cell-correct .25s ease;
}

.cell.correct span { font-size: .8em; }

.cell.wrong {
  background: #2d1b1b;
  color: var(--bad);
  opacity: 1;
  animation: cell-shake .3s ease;
}

.best-record {
  display: flex;
  align-items: center;
  gap: 8px;
  margin: 14px 0 0;
  padding: 8px 14px;
  background: var(--panel);
  border-radius: 8px;
}

.best-label { font-size: 13px; color: var(--muted); }

.best-time { font-size: 15px; font-weight: 700; color: var(--gold); font-variant-numeric: tabular-nums; }

.actions { margin-top: 12px; }

.btn {
  min-height: 42px;
  padding: 0 24px;
  background: var(--panel);
  border: 1px solid var(--line);
  border-radius: 8px;
  font-size: 14px;
  font-weight: 600;
  transition: transform .1s ease;
}

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

.records-content { width: 100%; }

.stats { display: flex; gap: 8px; margin: 8px 0; }

.stat-card {
  flex: 1;
  min-width: 0;
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 3px;
  padding: 10px 6px;
  background: var(--panel);
  border-radius: 10px;
}

.stat-icon { font-size: 18px; }

.stat-value { font-size: 18px; font-weight: 800; color: var(--accent); font-variant-numeric: tabular-nums; }

.stat-label { font-size: 11px; color: var(--muted); }

.list-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin: 14px 0 6px;
  padding: 0 4px;
}

.list-title { font-size: 14px; font-weight: 600; color: var(--muted); }

.clear-btn { min-height: 32px; padding: 0 8px; font-size: 13px; color: #ff7a90; }

.record-list { list-style: none; }

.record-item {
  display: flex;
  align-items: center;
  gap: 10px;
  margin-bottom: 4px;
  padding: 10px 12px;
  background: var(--panel);
  border-left: 2px solid transparent;
  border-radius: 6px;
}

.record-item.top3 { border-left-color: var(--accent); }

.rank { width: 30px; text-align: center; font-size: 14px; font-weight: 600; color: var(--muted); }

.rank.medal { font-size: 18px; }

.record-info {
  flex: 1;
  min-width: 0;
  display: flex;
  justify-content: space-between;
  align-items: center;
  gap: 8px;
}

.record-time { font-size: 16px; font-weight: 700; font-variant-numeric: tabular-nums; white-space: nowrap; }

.record-date { font-size: 11px; color: var(--muted); text-align: right; }

.empty {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 8px;
  margin-top: 60px;
  text-align: center;
}

.empty-icon { font-size: 40px; }

.empty-text { font-size: 16px; font-weight: 600; color: var(--muted); }

.empty-hint { padding: 0 20px; font-size: 12px; color: var(--muted); }

.tab-bar {
  position: fixed;
  left: 0;
  right: 0;
  bottom: 0;
  z-index: 10;
  display: flex;
  justify-content: center;
  padding-bottom: env(safe-area-inset-bottom);
  background: var(--bar);
  border-top: 1px solid #000;
}

.tab {
  flex: 1;
  max-width: 240px;
  height: var(--tab-height);
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 14px;
  color: var(--muted);
  text-decoration: none;
}

.tab[aria-current="page"] { color: var(--accent); font-weight: 700; }

.dialog {
  width: min(320px, calc(100vw - 48px));
  padding: 0;
  overflow: hidden;
  background: var(--bar);
  color: var(--text);
  border: 1px solid var(--line);
  border-radius: 12px;
  text-align: center;
}

.dialog::backdrop { background: rgba(0, 0, 0, .6); }

.dialog[open] { animation: celebrate .3s ease; }

.dialog-title { padding: 22px 20px 8px; font-size: 18px; }

.dialog-message {
  padding: 0 20px 22px;
  font-size: 14px;
  line-height: 1.6;
  color: #b8b8cc;
  white-space: pre-line;
}

.dialog-actions { display: flex; border-top: 1px solid var(--line); }

.dialog-btn { flex: 1; height: 50px; font-size: 15px; color: var(--muted); }

.dialog-btn + .dialog-btn { border-left: 1px solid var(--line); }

.dialog-btn.primary { color: #ff7a90; font-weight: 700; }

.toast {
  position: fixed;
  left: 50%;
  bottom: calc(var(--tab-height) + 24px + env(safe-area-inset-bottom));
  z-index: 20;
  transform: translateX(-50%);
  padding: 8px 18px;
  background: rgba(0, 0, 0, .85);
  border-radius: 8px;
  font-size: 14px;
}

@keyframes fade-in {
  from { opacity: 0; transform: translateY(10px); }
  to { opacity: 1; transform: translateY(0); }
}

@keyframes celebrate {
  from { opacity: 0; transform: scale(.8); }
  to { opacity: 1; transform: scale(1); }
}

@keyframes cell-correct {
  0% { transform: scale(1); }
  50% { transform: scale(1.08); background: #166534; }
  100% { transform: scale(1); }
}

@keyframes cell-shake {
  0%, 100% { transform: translateX(0); }
  25% { transform: translateX(-4px); }
  75% { transform: translateX(4px); }
}

@media (prefers-reduced-motion: reduce) {
  *, *::before, *::after {
    animation-duration: .01ms !important;
    transition-duration: .01ms !important;
  }
}
