:root {
  --bg: #fbf6ee;
  --surface: #fffaf3;
  --surface-2: #f3e8d8;
  --ink: #2b1d14;
  --muted: #7a6656;
  --line: #e6d6c1;
  --accent: #d2611a;
  --accent-ink: #fff;
  --maroon: #8c2f39;
  --good: #2f8f5b;
  --bad: #c0392b;
  --gold: #c99a1c;
  --step: #e9d5b8;
  --step-reached: #e7a86a;
  --shadow: 0 1px 2px rgba(60, 30, 10, 0.08), 0 6px 20px rgba(60, 30, 10, 0.06);
  --radius: 14px;
}

@media (prefers-color-scheme: dark) {
  :root {
    --bg: #17120e;
    --surface: #211a15;
    --surface-2: #2c231c;
    --ink: #f2e8db;
    --muted: #b29d8a;
    --line: #3a2f26;
    --accent: #f08a3c;
    --accent-ink: #1b120b;
    --maroon: #e0828b;
    --good: #4fbf83;
    --bad: #ef6b5b;
    --gold: #e3b84a;
    --step: #3a2f26;
    --step-reached: #a0612d;
    --shadow: none;
  }
}

* { box-sizing: border-box; }

body {
  margin: 0;
  background: var(--bg);
  color: var(--ink);
  font: 16px/1.5 system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
}

button, select, input { font: inherit; color: inherit; }

button {
  cursor: pointer;
  border: 1px solid var(--line);
  background: var(--surface);
  border-radius: 999px;
  padding: 10px 20px;
}
button:disabled { opacity: 0.45; cursor: default; }
button.primary {
  background: var(--accent);
  border-color: var(--accent);
  color: var(--accent-ink);
  font-weight: 600;
}
button.link { border: none; background: none; padding: 4px 0; color: var(--muted); }

.app { max-width: 760px; margin: 0 auto; padding: 16px; }

/* Header */
.topbar {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  padding-bottom: 14px;
  border-bottom: 1px solid var(--line);
}
.brand {
  border: none;
  background: none;
  padding: 0;
  font-size: 1.3rem;
  font-weight: 700;
  color: var(--maroon);
  letter-spacing: 0.01em;
}
.brand span { color: var(--accent); }
.settings { display: flex; flex-wrap: wrap; gap: 8px 14px; align-items: center; font-size: 0.9rem; color: var(--muted); }
.settings label { display: flex; align-items: center; gap: 6px; }
.settings select {
  background: var(--surface);
  border: 1px solid var(--line);
  border-radius: 8px;
  padding: 4px 6px;
}
.settings input[type="checkbox"] { accent-color: var(--accent); }

/* Home */
.intro h1 { font-size: 1.6rem; line-height: 1.25; margin: 28px 0 8px; }
.intro p { color: var(--muted); margin: 0 0 24px; }

.levels { display: grid; gap: 12px; }
.level-card {
  display: flex;
  align-items: center;
  gap: 16px;
  text-align: left;
  border-radius: var(--radius);
  padding: 16px 18px;
  background: var(--surface);
  box-shadow: var(--shadow);
}
.level-card:not(:disabled):hover { border-color: var(--accent); }
.level-num {
  flex: none;
  width: 44px;
  height: 44px;
  border-radius: 50%;
  display: grid;
  place-items: center;
  font-weight: 700;
  font-size: 1.2rem;
  background: var(--surface-2);
  color: var(--maroon);
}
.level-body { flex: 1; display: flex; flex-direction: column; min-width: 0; }
.level-title { font-weight: 600; }
.level-sub { color: var(--muted); font-size: 0.9rem; }
.level-status { flex: none; font-size: 0.9rem; font-weight: 600; color: var(--accent); }
.level-status.cleared { color: var(--good); }
.tip { color: var(--muted); font-size: 0.85rem; margin-top: 20px; }

/* Game header */
.game-head { display: flex; align-items: flex-start; gap: 16px; margin: 16px 0 8px; }
.game-title { flex: 1; min-width: 0; }
.game-title h2 { margin: 0; font-size: 1.2rem; }
.game-title p { margin: 2px 0 0; color: var(--muted); font-size: 0.9rem; }
.step-count {
  flex: none;
  font-weight: 700;
  font-variant-numeric: tabular-nums;
  background: var(--surface-2);
  border-radius: 999px;
  padding: 4px 12px;
  font-size: 0.9rem;
}

/* 3D scene (falls back to the CSS staircase without WebGL) */
.scene {
  height: clamp(180px, 34vh, 320px); /* leaves room for the controls on short screens */
  margin: 8px 0 16px;
  border-radius: var(--radius);
  overflow: hidden;
  background: #cfe9f7;
}
.scene canvas { display: block; }
.scene[hidden], .stairs[hidden] { display: none; }

/* Staircase */
.stairs {
  position: relative;
  display: flex;
  align-items: flex-end;
  height: 160px;
  padding-top: 40px;
  margin: 8px 0 16px;
}
.step {
  flex: 1;
  margin: 0 1.5px;
  background: var(--step);
  border-radius: 6px 6px 2px 2px;
  transition: background 0.3s;
  position: relative;
}
.step.reached { background: var(--step-reached); }
.step.ground { background: var(--line); }
.step.top::after {
  content: "🪷";
  position: absolute;
  bottom: 100%;
  left: 50%;
  transform: translateX(-50%);
  font-size: 1.6rem;
  line-height: 1;
  margin-bottom: 30px;
}
.token {
  position: absolute;
  width: 28px;
  height: 28px;
  margin-bottom: 2px;
  transform: translateX(-50%);
  border-radius: 50%;
  display: grid;
  place-items: center;
  background: var(--maroon);
  color: #fff;
  font-weight: 700;
  box-shadow: 0 2px 6px rgba(0, 0, 0, 0.25);
  transition: left 0.45s cubic-bezier(.3, 1.4, .5, 1), bottom 0.45s cubic-bezier(.3, 1.4, .5, 1);
  z-index: 1;
}

/* Teacher stage */
.stage {
  background: var(--surface);
  border: 1px solid var(--line);
  border-radius: var(--radius);
  padding: 14px 16px;
  text-align: center;
  box-shadow: var(--shadow);
}
.status { font-weight: 600; min-height: 1.5em; color: var(--muted); }
.status.teach { color: var(--maroon); }
.status.listen { color: var(--accent); }
.status.good { color: var(--good); }
.status.bad { color: var(--bad); }

.tiles { display: flex; justify-content: center; gap: 6px; margin: 12px 0 8px; min-height: 68px; }
.tile {
  flex: 1 1 0;
  max-width: 64px;
  min-width: 0;
  height: 68px;
  border-radius: 10px;
  border: 2px solid var(--line);
  background: var(--surface-2);
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  transition: transform 0.15s, background 0.15s, border-color 0.15s;
}
.tile-letter { font-size: 1.5rem; font-weight: 700; line-height: 1.1; }
.tile-name { font-size: 0.75rem; color: var(--muted); }
.tile.hold { background: none; border-style: dashed; color: var(--muted); }
.tile.playing { transform: translateY(-6px); border-color: var(--maroon); background: var(--maroon); color: #fff; }
.tile.playing .tile-name { color: #fff; }
.tile.current { border-color: var(--accent); border-style: dashed; }
.tile.done { border-color: var(--good); background: var(--good); color: #fff; }
.tile.done .tile-name { color: #fff; }
.tile.wrong { border-color: var(--bad); background: var(--bad); color: #fff; animation: shake 0.35s; }
.tile.wrong .tile-name { color: #fff; }
@keyframes shake {
  25% { transform: translateX(-5px); }
  75% { transform: translateX(5px); }
}

.feedback { min-height: 1.5em; font-size: 0.95rem; }
.feedback.good { color: var(--good); font-weight: 600; }
.feedback.bad { color: var(--bad); }

/* Pitch meter */
.meter { margin: 16px 0; }
.meter-readout { display: flex; align-items: baseline; gap: 10px; margin-bottom: 8px; }
.heard { font-size: 1.4rem; font-weight: 700; min-width: 2.2em; }
.heard.in-tune { color: var(--good); }
.heard-detail { color: var(--muted); font-size: 0.9rem; font-variant-numeric: tabular-nums; }
.mic-level { margin-left: auto; width: 80px; height: 6px; border-radius: 3px; background: var(--surface-2); overflow: hidden; }
.mic-level div { height: 100%; width: 0; background: var(--accent); }

.strip {
  position: relative;
  height: 46px;
  margin: 0 12px;
  border-top: 2px solid var(--line);
}
.tick {
  position: absolute;
  top: -7px;
  transform: translateX(-50%);
  display: flex;
  flex-direction: column;
  align-items: center;
  font-size: 0.8rem;
  color: var(--muted);
}
.tick::before { content: ""; width: 2px; height: 12px; background: var(--line); margin-bottom: 4px; }
.tick.target { color: var(--accent); font-weight: 700; }
.tick.target::before { background: var(--accent); }
.tick.hit { color: var(--good); font-weight: 700; }
.marker {
  position: absolute;
  top: -8px;
  width: 14px;
  height: 14px;
  border-radius: 50%;
  transform: translateX(-50%);
  background: var(--muted);
  transition: left 0.06s linear;
}
.marker.in-tune { background: var(--good); }

.controls { display: flex; gap: 10px; justify-content: center; margin-top: 10px; }

/* Dialog */
.overlay {
  position: fixed;
  inset: 0;
  background: rgba(20, 10, 5, 0.5);
  display: grid;
  place-items: center;
  padding: 16px;
  z-index: 10;
}
.overlay[hidden] { display: none; }
.dialog {
  background: var(--surface);
  border-radius: var(--radius);
  padding: 28px 24px 22px;
  max-width: 380px;
  width: 100%;
  text-align: center;
  box-shadow: 0 20px 60px rgba(0, 0, 0, 0.3);
}
.dlg-icon { font-size: 2.6rem; line-height: 1; }
.dialog h2 { margin: 12px 0 6px; }
.dialog p { color: var(--muted); margin: 0 0 20px; }
.dlg-actions { display: flex; flex-wrap: wrap; gap: 8px; justify-content: center; }

@media (max-width: 520px) {
  .intro h1 { font-size: 1.35rem; }
  .game-head { flex-wrap: wrap; justify-content: space-between; }
  .game-title { order: 3; flex-basis: 100%; }
  .stairs { height: 140px; }
  .scene { height: clamp(180px, 30vh, 240px); }
  .token { width: 22px; height: 22px; font-size: 0.8rem; }
  .tile { height: 60px; }
  .tile-letter { font-size: 1.2rem; }
  .tile-name { font-size: 0.65rem; }
}
