:root {
  --bg: #0b0b10;
  --bg-raised: #15151e;
  --bg-elev: #1e1e2a;
  --border: #2a2a38;
  --text: #e9e9f0;
  --text-muted: #9a9aaf;
  --primary: #7c8cff;
  --primary-dim: #5867d6;
  --success: #4fd88a;
  --warn: #ffb74d;
  --danger: #ff6b7a;
  --accent: #ffd166;
  --radius: 12px;
  --safe-top: env(safe-area-inset-top, 0px);
  --safe-bot: env(safe-area-inset-bottom, 0px);
}

* { box-sizing: border-box; }
html, body { margin: 0; padding: 0; background: var(--bg); color: var(--text); font-family: -apple-system, BlinkMacSystemFont, "SF Pro Text", system-ui, sans-serif; -webkit-font-smoothing: antialiased; }
body { padding-bottom: calc(24px + var(--safe-bot)); }

#top-bar {
  position: sticky;
  top: 0;
  z-index: 20;
  padding: calc(var(--safe-top) + 10px) 16px 10px;
  display: flex;
  justify-content: space-between;
  align-items: center;
  background: rgba(11,11,16,0.9);
  backdrop-filter: blur(14px);
  -webkit-backdrop-filter: blur(14px);
  border-bottom: 1px solid var(--border);
  font-size: 13px;
}
#phase-label { font-weight: 600; letter-spacing: 0.02em; text-transform: uppercase; color: var(--text-muted); }
#timers { font-variant-numeric: tabular-nums; color: var(--text-muted); }
#timer-cutoff.urgent { color: var(--danger); font-weight: 700; }

main#app { padding: 16px; max-width: 720px; margin: 0 auto; }

.mode { animation: fade 0.15s ease-out; }
.mode.hidden { display: none; }
@keyframes fade { from { opacity: 0; transform: translateY(4px); } to { opacity: 1; transform: none; } }

h1 { font-size: 28px; margin: 18px 0 12px; letter-spacing: -0.01em; }
h2 { font-size: 22px; margin: 16px 0 10px; letter-spacing: -0.01em; }
h3 { font-size: 16px; margin: 18px 0 8px; color: var(--text-muted); text-transform: uppercase; letter-spacing: 0.05em; }

p { line-height: 1.55; }
p.big { font-size: 18px; }
p.muted, .muted { color: var(--text-muted); }
.small { font-size: 13px; }

hr { border: none; border-top: 1px solid var(--border); margin: 18px 0; }

button {
  font: inherit;
  border: none;
  border-radius: var(--radius);
  padding: 14px 18px;
  background: var(--bg-elev);
  color: var(--text);
  font-weight: 600;
  cursor: pointer;
  margin: 6px 0;
  display: inline-block;
  min-height: 48px;
  -webkit-tap-highlight-color: rgba(124,140,255,0.15);
  transition: transform 0.08s, background 0.15s;
}
button:active { transform: scale(0.97); }
button.primary { background: var(--primary); color: #0b0b10; width: 100%; }
button.primary:disabled { opacity: 0.4; }
button.secondary { background: var(--bg-elev); color: var(--text); width: 100%; border: 1px solid var(--border); }
button.ghost { background: transparent; color: var(--text-muted); border: 1px solid var(--border); }
button.back { background: transparent; color: var(--text-muted); padding: 8px 0; font-size: 14px; }

input[type="time"], input[type="text"], select, textarea {
  font: inherit;
  background: var(--bg-elev);
  color: var(--text);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 12px;
  width: 100%;
  margin: 6px 0;
  -webkit-appearance: none;
}
textarea { font-family: -apple-system, ui-monospace, monospace; resize: vertical; min-height: 200px; line-height: 1.55; }
input[type="range"] { width: 100%; accent-color: var(--primary); }

details {
  background: var(--bg-raised);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 10px 14px;
  margin: 10px 0;
}
details summary { cursor: pointer; color: var(--text-muted); font-weight: 600; font-size: 14px; list-style: none; display: flex; align-items: center; justify-content: space-between; gap: 10px; }
details summary::-webkit-details-marker { display: none; }
details summary > span:first-child { flex: 1; }
details[open] summary { margin-bottom: 8px; }
details p, details .answer-text { margin: 8px 0 0; line-height: 1.6; font-size: 15px; }

.prompt-text { font-style: italic; color: var(--text-muted); }
.answer-text { font-size: 15px; line-height: 1.65; }

/* encode list */
.q-card {
  background: var(--bg-raised);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 14px;
  margin: 10px 0;
  cursor: pointer;
  transition: background 0.15s;
}
.q-card:active { background: var(--bg-elev); }
.q-card .q-num { color: var(--text-muted); font-size: 12px; text-transform: uppercase; letter-spacing: 0.05em; }
.q-card .q-title { font-weight: 600; margin: 4px 0; }
.q-card .q-progress { color: var(--text-muted); font-size: 13px; margin-top: 6px; }
.q-card .q-progress.done { color: var(--success); }
.q-card.leitner-1 { border-left: 3px solid var(--danger); }
.q-card.leitner-2 { border-left: 3px solid var(--warn); }
.q-card.leitner-3 { border-left: 3px solid var(--success); }

/* beats */
.beat {
  background: var(--bg-raised);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 12px 14px;
  margin: 8px 0;
  display: flex;
  gap: 12px;
  align-items: flex-start;
  cursor: pointer;
}
.beat.checked { background: rgba(79,216,138,0.08); border-color: rgba(79,216,138,0.3); }
.beat .check {
  width: 24px; height: 24px;
  border-radius: 50%;
  border: 2px solid var(--border);
  flex-shrink: 0;
  display: flex; align-items: center; justify-content: center;
  font-size: 14px;
  color: var(--bg);
}
.beat.checked .check { background: var(--success); border-color: var(--success); }
.beat.checked .check::before { content: "✓"; font-weight: 700; }
.beat .beat-id { font-family: ui-monospace, monospace; font-size: 11px; color: var(--text-muted); margin-bottom: 2px; }
.beat .beat-text { font-size: 15px; line-height: 1.5; }

/* terms pill */
.term-pill {
  display: inline-block;
  background: var(--bg-elev);
  border: 1px solid var(--border);
  padding: 4px 10px;
  border-radius: 999px;
  font-size: 13px;
  margin: 3px;
}

/* glossary */
.glossary-item {
  background: var(--bg-raised);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 14px;
  margin: 8px 0;
}
.glossary-item summary { font-weight: 600; color: var(--text); font-size: 15px; }
.glossary-item p { font-size: 14px; line-height: 1.55; color: var(--text-muted); }

/* sleep */
.sleep-hero { padding: 24px 0; text-align: center; }
.sleep-hero h1 { font-size: 42px; }
#sleep-config { text-align: left; margin: 24px 0; }
#sleep-locked { text-align: center; padding: 40px 0; }
#sleep-locked h1 { font-size: 72px; margin: 0; }

/* drill */
#drill-stage .drill-meta { display: flex; justify-content: space-between; margin-bottom: 8px; }
.drill-controls { display: flex; gap: 10px; margin: 14px 0; }
.drill-controls button { flex: 1; margin: 0; }
.drill-nav { display: flex; gap: 8px; flex-wrap: wrap; }
.drill-nav button { flex: 1; font-size: 13px; padding: 10px; min-width: 100px; }

/* drill result */
.score-row { display: flex; align-items: center; gap: 18px; margin: 12px 0; }
.score-big { font-size: 52px; font-weight: 800; letter-spacing: -0.02em; font-variant-numeric: tabular-nums; }
.score-big.good { color: var(--success); }
.score-big.mid { color: var(--warn); }
.score-big.bad { color: var(--danger); }
.score-meta { flex: 1; }

#beats-hit li, #beats-missed li {
  list-style: none;
  padding: 8px 12px;
  margin: 4px 0;
  border-radius: 8px;
  font-size: 14px;
  line-height: 1.4;
}
#beats-hit li { background: rgba(79,216,138,0.1); color: #9cecbb; }
#beats-hit li::before { content: "✓ "; color: var(--success); }
#beats-missed li { background: rgba(255,107,122,0.08); color: #ffb5bc; }
#beats-missed li::before { content: "✗ "; color: var(--danger); }
ul { padding: 0; margin: 8px 0; }

#confidence-value { font-size: 42px; font-weight: 800; text-align: center; }

/* handwrite */
.file-label {
  display: block;
  background: var(--primary);
  color: var(--bg);
  padding: 14px;
  border-radius: var(--radius);
  text-align: center;
  font-weight: 600;
  cursor: pointer;
  margin: 10px 0;
}
#handwrite-file { display: none; }
#handwrite-img { width: 100%; border-radius: var(--radius); margin: 10px 0; }
#ocr-text { background: var(--bg-raised); padding: 12px; border-radius: var(--radius); white-space: pre-wrap; font-size: 13px; line-height: 1.5; overflow-x: auto; border: 1px solid var(--border); }

/* stop */
.stop-hero { padding: 60px 0; text-align: center; }
.stop-hero h1 { font-size: 64px; color: var(--danger); }

/* dashboard */
.dash-row {
  background: var(--bg-raised);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 12px 14px;
  margin: 8px 0;
  display: flex;
  justify-content: space-between;
  align-items: center;
}
.dash-row .dash-score { font-weight: 700; font-variant-numeric: tabular-nums; }
.dash-row .dash-score.good { color: var(--success); }
.dash-row .dash-score.mid { color: var(--warn); }
.dash-row .dash-score.bad { color: var(--danger); }

/* TTS play button */
.tts-btn {
  background: var(--bg-elev);
  color: var(--primary);
  border: 1px solid var(--border);
  border-radius: 50%;
  width: 34px;
  height: 34px;
  min-height: 34px;
  padding: 0;
  margin: 0;
  font-size: 14px;
  flex-shrink: 0;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  font-weight: 700;
  line-height: 1;
}
.tts-btn[data-tts-state="playing"] { background: var(--primary); color: var(--bg); border-color: var(--primary); }
.tts-btn[data-tts-state="loading"] { opacity: 0.6; }
.tts-btn[data-tts-state="error"] { background: var(--danger); color: var(--bg); border-color: var(--danger); }

.prompt-row { display: flex; gap: 10px; align-items: flex-start; }
.prompt-row h3 { flex: 1; margin: 0; text-transform: none; color: var(--text); font-weight: 500; font-size: 17px; letter-spacing: 0; line-height: 1.45; }

/* toast */
#toast {
  position: fixed;
  bottom: calc(20px + var(--safe-bot));
  left: 50%;
  transform: translateX(-50%);
  background: var(--bg-elev);
  border: 1px solid var(--border);
  color: var(--text);
  padding: 12px 20px;
  border-radius: var(--radius);
  z-index: 50;
  font-size: 14px;
  box-shadow: 0 8px 20px rgba(0,0,0,0.4);
  max-width: 90%;
}
#toast.hidden { display: none; }

/* loading spinner */
.spinner {
  width: 18px; height: 18px;
  border: 2px solid var(--border);
  border-top-color: var(--primary);
  border-radius: 50%;
  animation: spin 0.8s linear infinite;
  display: inline-block;
  vertical-align: middle;
  margin-right: 8px;
}
@keyframes spin { to { transform: rotate(360deg); } }

/* carousel */
#warmup-carousel { }
.warmup-card {
  background: var(--bg-raised);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 16px;
  margin: 10px 0;
}
.warmup-card h3 { margin-top: 0; }
.warmup-card ol { margin: 8px 0 0 20px; padding: 0; }
.warmup-card li { margin: 6px 0; line-height: 1.5; font-size: 14px; }
