/* ---------- Design tokens ---------- */
:root {
  --bg: #f5f6f8;
  --bg-glow: #e6ecf5;
  --surface: #ffffff;
  --surface-2: #f0f2f5;
  --text: #14171c;
  --muted: #69717d;
  --line: #e2e5ea;
  --accent: #0a66d8;
  --accent-soft: #e5effc;
  --good: #12a150;
  --good-soft: #e3f6ea;
  --bad: #e5484d;
  --bad-soft: #fdeceb;
  --gold: #f5a524;
  --shadow-sm: 0 1px 2px rgba(16, 24, 40, 0.06);
  --shadow-md: 0 1px 2px rgba(16, 24, 40, 0.05), 0 8px 24px -10px rgba(16, 24, 40, 0.18);
  --shadow-lg: 0 2px 4px rgba(16, 24, 40, 0.05), 0 18px 40px -16px rgba(16, 24, 40, 0.28);
  --radius: 16px;
  --ease: cubic-bezier(0.2, 0.8, 0.2, 1);
  --spring: cubic-bezier(0.34, 1.56, 0.64, 1);
}

@media (prefers-color-scheme: dark) {
  :root {
    --bg: #0f1216;
    --bg-glow: #172131;
    --surface: #181c22;
    --surface-2: #20252d;
    --text: #eef1f5;
    --muted: #949daa;
    --line: #2a3039;
    --accent: #4c9bff;
    --accent-soft: #16263d;
    --good: #3ccf7a;
    --good-soft: #13291d;
    --bad: #ff6b6f;
    --bad-soft: #321a1b;
    --shadow-sm: 0 1px 2px rgba(0, 0, 0, 0.4);
    --shadow-md: 0 1px 2px rgba(0, 0, 0, 0.35), 0 8px 24px -10px rgba(0, 0, 0, 0.6);
    --shadow-lg: 0 2px 4px rgba(0, 0, 0, 0.35), 0 18px 40px -16px rgba(0, 0, 0, 0.7);
  }
}

/* ---------- Base ---------- */
* { box-sizing: border-box; }

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

body {
  margin: 0;
  min-height: 100vh;
  background: radial-gradient(1200px 520px at 50% -160px, var(--bg-glow), transparent 70%) var(--bg);
  color: var(--text);
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", "PingFang SC", "Hiragino Sans GB", "Microsoft YaHei", system-ui, sans-serif;
  line-height: 1.5;
  -webkit-font-smoothing: antialiased;
  -webkit-tap-highlight-color: transparent;
}

button {
  font: inherit;
  color: var(--text);
  cursor: pointer;
}

button:focus-visible {
  outline: 3px solid color-mix(in srgb, var(--accent) 45%, transparent);
  outline-offset: 2px;
}

.app {
  max-width: 600px;
  margin: 0 auto;
  padding: 72px 20px 48px;
  transition: padding-bottom 0.3s var(--ease);
}
body.sheet-open .app { padding-bottom: 160px; }

.screen { display: none; }
.screen.active { display: block; animation: screen-in 0.45s var(--ease) backwards; }

@keyframes screen-in {
  from { opacity: 0; transform: translateY(12px) scale(0.99); }
  to { opacity: 1; transform: none; }
}

/* ---------- Buttons ---------- */
.btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 8px;
  min-height: 48px;
  padding: 0 22px;
  border: 1px solid var(--line);
  border-radius: 999px;
  background: var(--surface);
  font-weight: 600;
  box-shadow: var(--shadow-sm);
  transition: transform 0.15s var(--ease), box-shadow 0.2s var(--ease), background 0.2s, border-color 0.2s;
}
.btn:hover { box-shadow: var(--shadow-md); transform: translateY(-1px); }
.btn:active { transform: scale(0.97); }
.btn.primary { background: var(--accent); border-color: transparent; color: #fff; }
.btn.ghost { background: transparent; box-shadow: none; border-color: transparent; color: var(--muted); }
.btn.ghost:hover { color: var(--text); background: var(--surface-2); }

.icon-btn {
  flex: none;
  display: grid;
  place-items: center;
  width: 40px;
  height: 40px;
  border: none;
  border-radius: 12px;
  background: transparent;
  color: var(--muted);
  transition: background 0.2s, color 0.2s, transform 0.15s var(--ease);
}
.icon-btn:hover { background: var(--surface-2); color: var(--text); }
.icon-btn:active { transform: scale(0.92); }
.icon-btn svg, .chevron {
  width: 22px;
  height: 22px;
  fill: none;
  stroke: currentColor;
  stroke-width: 2.2;
  stroke-linecap: round;
  stroke-linejoin: round;
}

/* ---------- Language switch ---------- */
.lang-switch {
  position: fixed;
  top: 16px;
  right: 16px;
  z-index: 20;
  display: flex;
  padding: 3px;
  border: 1px solid var(--line);
  border-radius: 999px;
  background: color-mix(in srgb, var(--surface) 80%, transparent);
  backdrop-filter: blur(12px);
  -webkit-backdrop-filter: blur(12px);
  box-shadow: var(--shadow-sm);
}
.lang-switch button {
  border: none;
  background: transparent;
  padding: 4px 12px;
  border-radius: 999px;
  font-size: 13px;
  font-weight: 600;
  color: var(--muted);
  transition: background 0.25s var(--ease), color 0.25s, box-shadow 0.25s;
}
.lang-switch button.active {
  background: var(--surface);
  color: var(--text);
  box-shadow: var(--shadow-sm), 0 0 0 1px var(--line);
}

/* ---------- Start screen ---------- */
.hero { text-align: center; }

.logo-stage {
  position: relative;
  width: 168px;
  margin: 0 auto 12px;
}
.logo {
  display: block;
  image-rendering: pixelated;
}
.logo-stage .logo {
  width: 128px;
  height: 128px;
  margin: 0 auto;
  animation: arrive 0.9s var(--ease) backwards, idle 2.4s ease-in-out 0.9s infinite;
}
.rail {
  height: 6px;
  margin-top: -14px;
  border-radius: 3px;
  background: repeating-linear-gradient(90deg, var(--line) 0 10px, transparent 10px 16px);
  mask-image: linear-gradient(90deg, transparent, #000 25%, #000 75%, transparent);
  -webkit-mask-image: linear-gradient(90deg, transparent, #000 25%, #000 75%, transparent);
}

@keyframes arrive {
  from { opacity: 0; transform: translateY(-10px) scale(0.6); }
  60% { opacity: 1; transform: translateY(2px) scale(1.04); }
  to { transform: none; }
}
@keyframes idle {
  0%, 100% { transform: translateY(0); }
  50% { transform: translateY(-3px); }
}

h1 {
  margin: 0;
  font-size: clamp(34px, 7vw, 46px);
  font-weight: 800;
  letter-spacing: -0.02em;
  line-height: 1.1;
}
.subtitle {
  margin: 10px auto 0;
  max-width: 420px;
  color: var(--muted);
  font-size: 17px;
}

.legend {
  display: flex;
  flex-wrap: wrap;
  justify-content: center;
  gap: 6px;
  margin: 22px 0 36px;
}
.legend .line-chip { animation: rise 0.5s var(--ease) backwards; animation-delay: calc(var(--i, 0) * 40ms + 200ms); }

.section-label {
  margin: 0 0 12px 4px;
  font-size: 13px;
  font-weight: 600;
  letter-spacing: 0.06em;
  text-transform: uppercase;
  color: var(--muted);
}

.difficulty { display: grid; gap: 12px; }

.diff-card {
  display: grid;
  grid-template-columns: auto 1fr auto auto;
  align-items: center;
  gap: 16px;
  padding: 18px 18px 18px 20px;
  text-align: left;
  border: 1px solid var(--line);
  border-radius: var(--radius);
  background: var(--surface);
  box-shadow: var(--shadow-sm);
  animation: rise 0.5s var(--ease) backwards;
  transition: transform 0.2s var(--ease), box-shadow 0.25s var(--ease), border-color 0.2s;
}
.diff-card:nth-child(1) { animation-delay: 0.3s; }
.diff-card:nth-child(2) { animation-delay: 0.36s; }
.diff-card:nth-child(3) { animation-delay: 0.42s; }
.diff-card:hover {
  transform: translateY(-2px);
  box-shadow: var(--shadow-lg);
  border-color: color-mix(in srgb, var(--accent) 40%, var(--line));
}
.diff-card:active { transform: scale(0.98); }
.diff-text { display: grid; gap: 2px; }
.diff-text b { font-size: 18px; }
.diff-text span { font-size: 14px; color: var(--muted); }
.diff-card .best {
  font-style: normal;
  font-size: 13px;
  font-weight: 600;
  color: var(--muted);
  font-variant-numeric: tabular-nums;
}
.diff-card .chevron { color: var(--muted); transition: transform 0.25s var(--ease), color 0.2s; }
.diff-card:hover .chevron { transform: translateX(3px); color: var(--accent); }

.bars { display: flex; align-items: flex-end; gap: 3px; height: 22px; }
.bars i { width: 6px; border-radius: 2px; background: var(--line); }
.bars i:nth-child(1) { height: 9px; }
.bars i:nth-child(2) { height: 15px; }
.bars i:nth-child(3) { height: 22px; }
.bars i.on { background: var(--accent); }

.note { margin-top: 28px; text-align: center; font-size: 12px; color: var(--muted); }

@keyframes rise {
  from { opacity: 0; transform: translateY(10px); }
  to { opacity: 1; transform: none; }
}

/* ---------- Line chips & code badges ---------- */
.line-chip {
  display: inline-flex;
  align-items: center;
  gap: 5px;
  padding: 2px 10px;
  border-radius: 999px;
  background: var(--c);
  color: var(--t);
  font-size: 0.82em;
  font-weight: 700;
  white-space: nowrap;
  vertical-align: 0.12em;
  line-height: 1.6;
}
.line-chip small { font-weight: 500; opacity: 0.85; }

.codes { display: inline-flex; gap: 4px; flex-wrap: wrap; }
.code {
  padding: 1px 6px;
  border-radius: 5px;
  background: var(--c);
  color: var(--t);
  font-size: 12px;
  font-weight: 700;
  font-family: ui-monospace, "SF Mono", Consolas, monospace;
  letter-spacing: 0.02em;
}

/* ---------- Quiz screen ---------- */
.quiz-top { display: flex; align-items: center; gap: 12px; }

.progress { flex: 1; display: flex; gap: 4px; }
.progress .seg {
  flex: 1;
  height: 8px;
  border-radius: 4px;
  background: var(--line);
  transition: background 0.35s var(--ease), transform 0.35s var(--spring);
}
.progress .seg.current { background: color-mix(in srgb, var(--accent) 45%, var(--line)); transform: scaleY(1.25); }
.progress .seg.right { background: var(--good); }
.progress .seg.miss { background: var(--bad); }

.score-pill {
  flex: none;
  min-width: 72px;
  padding: 6px 12px;
  border-radius: 999px;
  background: var(--surface);
  border: 1px solid var(--line);
  text-align: center;
  font-weight: 700;
  font-size: 14px;
  font-variant-numeric: tabular-nums;
}
.score-pill.bump { animation: bump 0.45s var(--spring); }
@keyframes bump {
  40% { transform: scale(1.15); color: var(--good); }
}

.timer {
  height: 4px;
  margin: 14px 0 0;
  border-radius: 2px;
  background: var(--line);
  overflow: hidden;
}
#timer-bar {
  height: 100%;
  background: var(--accent);
  transform-origin: left;
  border-radius: 2px;
}
#timer-bar.low { background: var(--bad); }

.quiz-meta {
  display: flex;
  justify-content: space-between;
  align-items: center;
  min-height: 28px;
  margin: 18px 0 6px;
  font-size: 14px;
  font-weight: 600;
  color: var(--muted);
}
.combo {
  padding: 2px 10px;
  border-radius: 999px;
  background: color-mix(in srgb, var(--gold) 18%, transparent);
  color: #b26b00;
}
.combo:empty { display: none; }
.combo.pop { animation: bump 0.45s var(--spring); }
@media (prefers-color-scheme: dark) { .combo { color: var(--gold); } }

.prompt {
  margin: 0 0 24px;
  min-height: 2.9em;
  font-size: clamp(21px, 4.6vw, 26px);
  font-weight: 700;
  line-height: 1.45;
  letter-spacing: -0.01em;
  animation: rise 0.4s var(--ease) backwards;
}
.prompt .station-name { color: var(--accent); }
.prompt em { font-style: normal; color: var(--bad); text-decoration: underline 3px; text-underline-offset: 4px; }

.options { display: grid; gap: 12px; }

.option {
  position: relative;
  display: flex;
  align-items: center;
  gap: 14px;
  min-height: 64px;
  padding: 12px 16px;
  border: 1.5px solid var(--line);
  border-radius: var(--radius);
  background: var(--surface);
  box-shadow: var(--shadow-sm);
  text-align: left;
  font-size: 17px;
  font-weight: 600;
  animation: rise 0.4s var(--ease) backwards;
  animation-delay: calc(var(--i) * 55ms + 80ms);
  transition: transform 0.18s var(--ease), box-shadow 0.25s var(--ease), border-color 0.2s, background 0.25s, opacity 0.3s;
}
.option:hover:not(:disabled) {
  transform: translateY(-2px);
  border-color: color-mix(in srgb, var(--accent) 50%, var(--line));
  box-shadow: var(--shadow-md);
}
.option:active:not(:disabled) { transform: scale(0.98); }
.option:disabled { cursor: default; }

.option .key {
  flex: none;
  display: grid;
  place-items: center;
  width: 30px;
  height: 30px;
  border-radius: 9px;
  background: var(--surface-2);
  color: var(--muted);
  font-size: 13px;
  font-weight: 700;
  transition: background 0.25s, color 0.25s, transform 0.3s var(--spring);
}
.option .label { flex: 1; display: flex; align-items: center; flex-wrap: wrap; gap: 8px; }
.option .codes { opacity: 0; transform: translateX(-4px); transition: opacity 0.3s var(--ease), transform 0.3s var(--ease); }
.option.revealed .codes { opacity: 1; transform: none; }

.option.correct { border-color: var(--good); background: var(--good-soft); animation: pop 0.45s var(--spring); }
.option.correct .key { background: var(--good); color: #fff; transform: scale(1.08); }
.option.wrong { border-color: var(--bad); background: var(--bad-soft); animation: shake 0.42s var(--ease); }
.option.wrong .key { background: var(--bad); color: #fff; }
/* Drop the entrance animation, whose fill would otherwise pin opacity at 1 */
.option.dim { animation: none; opacity: 0.5; box-shadow: none; }

@keyframes pop {
  0% { transform: scale(1); }
  40% { transform: scale(1.03); }
  100% { transform: scale(1); }
}
@keyframes shake {
  0%, 100% { transform: translateX(0); }
  20% { transform: translateX(-7px); }
  40% { transform: translateX(6px); }
  60% { transform: translateX(-4px); }
  80% { transform: translateX(2px); }
}

/* ---------- Feedback sheet ---------- */
.sheet {
  position: fixed;
  left: 0;
  right: 0;
  bottom: 0;
  z-index: 10;
  padding: 18px 20px calc(18px + env(safe-area-inset-bottom));
  border-top: 1px solid var(--line);
  background: var(--surface);
  box-shadow: 0 -12px 32px -16px rgba(16, 24, 40, 0.25);
  transform: translateY(110%);
  transition: transform 0.45s var(--spring), background 0.3s;
}
.sheet.show { transform: none; }
.sheet.good { background: var(--good-soft); border-top-color: color-mix(in srgb, var(--good) 35%, transparent); }
.sheet.bad { background: var(--bad-soft); border-top-color: color-mix(in srgb, var(--bad) 35%, transparent); }

.sheet-inner {
  max-width: 560px;
  margin: 0 auto;
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 16px;
}
.sheet-text { display: flex; align-items: center; gap: 12px; min-width: 0; }
.sheet-text p { margin: 0; }
#sheet-title { font-size: 18px; font-weight: 800; }
.sheet.good #sheet-title { color: var(--good); }
.sheet.bad #sheet-title { color: var(--bad); }
.sheet-detail { display: flex; flex-wrap: wrap; align-items: center; gap: 6px; font-size: 14px; color: var(--muted); }
.sheet-detail:empty { display: none; }

.sheet-icon {
  flex: none;
  display: grid;
  place-items: center;
  width: 40px;
  height: 40px;
  border-radius: 50%;
  color: #fff;
  font-size: 20px;
  font-weight: 800;
}
.sheet.show .sheet-icon { animation: icon-in 0.5s var(--spring) 0.1s backwards; }
.sheet.good .sheet-icon { background: var(--good); }
.sheet.bad .sheet-icon { background: var(--bad); }
@keyframes icon-in {
  from { transform: scale(0) rotate(-30deg); }
  to { transform: none; }
}

.sheet .btn { flex: none; min-width: 132px; border: none; color: #fff; }
.sheet.good .btn { background: var(--good); }
.sheet.bad .btn { background: var(--bad); }

/* ---------- Result screen ---------- */
.result-card {
  padding: 32px 24px 24px;
  border: 1px solid var(--line);
  border-radius: 24px;
  background: var(--surface);
  box-shadow: var(--shadow-lg);
  text-align: center;
}
.logo.small { width: 72px; height: 72px; margin: 0 auto 8px; animation: arrive 0.8s var(--ease) backwards; }
.result-card h2 { margin: 0; font-size: 18px; color: var(--muted); font-weight: 600; }

.result-score {
  display: flex;
  justify-content: center;
  align-items: baseline;
  gap: 6px;
  margin-top: 4px;
}
#final-score {
  font-size: clamp(64px, 16vw, 84px);
  font-weight: 800;
  letter-spacing: -0.04em;
  line-height: 1.05;
  font-variant-numeric: tabular-nums;
  background: linear-gradient(135deg, var(--accent), color-mix(in srgb, var(--accent) 50%, #9b5cff));
  -webkit-background-clip: text;
  background-clip: text;
  color: transparent;
}
.result-score .unit { font-size: 20px; font-weight: 700; color: var(--muted); }

.new-best { margin: 8px 0 0; }
.new-best span {
  display: inline-block;
  padding: 5px 14px;
  border-radius: 999px;
  font-size: 14px;
  font-weight: 700;
  color: #6b3d00;
  background: linear-gradient(110deg, #ffd66b 0%, #ffb547 45%, #ffe7a3 50%, #ffb547 55%, #ffd66b 100%);
  background-size: 250% 100%;
  animation: rise 0.5s var(--spring) backwards, shimmer 2.4s linear 0.5s infinite;
}
@keyframes shimmer {
  from { background-position: 100% 0; }
  to { background-position: -150% 0; }
}
.best-line { margin: 8px 0 0; font-size: 14px; color: var(--muted); }
.best-line:empty { display: none; }

.stats {
  display: grid;
  grid-template-columns: repeat(3, 1fr);
  gap: 8px;
  margin: 22px 0 24px;
}
.stats div {
  display: grid;
  gap: 2px;
  padding: 12px 6px;
  border-radius: 14px;
  background: var(--surface-2);
  animation: rise 0.45s var(--ease) backwards;
}
.stats div:nth-child(1) { animation-delay: 0.15s; }
.stats div:nth-child(2) { animation-delay: 0.22s; }
.stats div:nth-child(3) { animation-delay: 0.29s; }
.stats span { font-size: 20px; font-weight: 800; font-variant-numeric: tabular-nums; }
.stats small { font-size: 12px; color: var(--muted); }

.result-actions { display: grid; grid-template-columns: 1fr 1fr; gap: 10px; }
.result-actions .ghost { grid-column: 1 / -1; }

.review { margin-top: 28px; }
.review h3 { margin: 0 0 12px 4px; font-size: 13px; font-weight: 600; letter-spacing: 0.06em; text-transform: uppercase; color: var(--muted); }
.review-item {
  padding: 14px 16px;
  margin-bottom: 10px;
  border: 1px solid var(--line);
  border-radius: 14px;
  background: var(--surface);
  animation: rise 0.4s var(--ease) backwards;
  animation-delay: calc(var(--i, 0) * 50ms + 300ms);
}
.review-item .q { margin-bottom: 6px; font-weight: 600; }
.review-item .a { display: flex; flex-wrap: wrap; align-items: center; gap: 6px; font-size: 14px; color: var(--muted); }
.review-item .a b { color: var(--good); }
.review-item .tag { padding: 0 8px; border-radius: 999px; background: var(--bad-soft); color: var(--bad); font-size: 12px; font-weight: 600; }
.all-right { text-align: center; color: var(--good); font-weight: 700; }

/* ---------- Toast & confetti ---------- */
.toast {
  position: fixed;
  left: 50%;
  bottom: calc(28px + env(safe-area-inset-bottom));
  z-index: 30;
  padding: 10px 18px;
  border-radius: 999px;
  background: var(--text);
  color: var(--bg);
  font-size: 14px;
  font-weight: 600;
  box-shadow: var(--shadow-lg);
  opacity: 0;
  pointer-events: none;
  transform: translate(-50%, 16px);
  transition: opacity 0.3s var(--ease), transform 0.35s var(--spring);
}
.toast.show { opacity: 1; transform: translate(-50%, 0); }

#confetti {
  position: fixed;
  inset: 0;
  z-index: 40;
  width: 100%;
  height: 100%;
  pointer-events: none;
}

/* ---------- Small screens ---------- */
@media (max-width: 480px) {
  .app { padding: 64px 16px 40px; }
  .diff-card { grid-template-columns: auto 1fr auto; gap: 14px; }
  .diff-card .best { grid-column: 2; grid-row: 2; margin-top: -8px; }
  .diff-card .chevron { grid-column: 3; grid-row: 1 / span 2; }
  .sheet .btn { min-width: 108px; padding: 0 16px; }
  .result-card { padding: 28px 16px 20px; }
}

@media (prefers-reduced-motion: reduce) {
  *, *::before, *::after {
    animation-duration: 0.01ms !important;
    animation-iteration-count: 1 !important;
    animation-delay: 0ms !important;
    transition-duration: 0.01ms !important;
  }
}
