:root {
  --bg: #f4f0ff;
  --bg2: #e6ddff;
  --ink: #241a4a;
  --ink-soft: #5a4f80;
  --brand: #5b3cc4;
  --brand-dark: #432a9b;
  --accent: #ff8a3d;
  --good: #1fa463;
  --bad: #e2445c;
  --card: #ffffff;
  --star: #ffc21a;
  --star-empty: #d9d2ef;
  --lane: #2a1f5c;
  --lane2: #34277040;
  --shadow: 0 6px 0 rgba(36, 26, 74, 0.18);
  --radius: 22px;
  --safe-top: env(safe-area-inset-top, 0px);
  --safe-bottom: env(safe-area-inset-bottom, 0px);
  --safe-left: env(safe-area-inset-left, 0px);
  --safe-right: env(safe-area-inset-right, 0px);
}

* { box-sizing: border-box; }
html, body {
  margin: 0;
  height: 100%;
  overflow: hidden;
  background: var(--bg);
  color: var(--ink);
  font-family: ui-rounded, "SF Pro Rounded", "Nunito", system-ui, -apple-system, "Segoe UI", sans-serif;
  font-size: 20px;
  line-height: 1.4;
  -webkit-text-size-adjust: 100%;
  -webkit-tap-highlight-color: transparent;
  -webkit-user-select: none;
  user-select: none;
  -webkit-touch-callout: none;
  touch-action: manipulation;
  overscroll-behavior: none;
}
button { font: inherit; color: inherit; cursor: pointer; touch-action: manipulation; }
button:focus-visible, input:focus-visible { outline: 4px solid var(--accent); outline-offset: 3px; }
h1, h2, h3 { margin: 0; line-height: 1.15; }
[hidden] { display: none !important; }

.screen {
  position: fixed;
  inset: 0;
  display: none;
  flex-direction: column;
  padding: var(--safe-top) var(--safe-right) var(--safe-bottom) var(--safe-left);
  background: radial-gradient(circle at 20% 0%, #fff 0%, var(--bg) 45%, var(--bg2) 100%);
}
.screen.active { display: flex; }

.ico { width: 1.4em; height: 1.4em; fill: currentColor; flex: none; }

/* ---------- Buttons ---------- */
.big-btn {
  display: flex;
  align-items: center;
  gap: 16px;
  min-height: 76px;
  padding: 14px 26px;
  border: none;
  border-radius: var(--radius);
  font-size: 22px;
  text-align: left;
  box-shadow: var(--shadow);
  transition: transform 0.08s;
}
.big-btn span { display: flex; flex-direction: column; }
.big-btn small { font-size: 18px; font-weight: 500; opacity: 0.85; }
.big-btn:active { transform: translateY(4px); box-shadow: 0 2px 0 rgba(36, 26, 74, 0.18); }
.big-btn .ico { width: 40px; height: 40px; }
.big-btn.primary { background: var(--brand); color: #fff; }
.big-btn.secondary { background: var(--card); color: var(--ink); }
.small-btn {
  display: inline-flex; align-items: center; gap: 8px;
  min-height: 56px; padding: 10px 18px;
  border: 2px solid #cfc4f2; border-radius: 16px; background: #fff;
  font-size: 18px; font-weight: 600;
}
.text-btn {
  background: none; border: none; min-height: 56px; padding: 10px 18px;
  font-size: 20px; font-weight: 700; color: var(--brand); text-decoration: underline;
}
.icon-btn {
  width: 60px; height: 60px; border-radius: 18px; border: none;
  background: #fff; box-shadow: var(--shadow); display: grid; place-items: center; flex: none;
}
.icon-btn svg { width: 32px; height: 32px; fill: var(--ink); }

/* ---------- Home ---------- */
.home-wrap {
  margin: auto; width: min(640px, calc(100% - 32px));
  display: flex; flex-direction: column; align-items: center; gap: 18px; text-align: center;
  padding: 16px 0; overflow-y: auto; max-height: 100%;
}
.home-wrap h1 { font-size: 56px; color: var(--brand-dark); letter-spacing: -0.5px; }
.lead { margin: 0; font-size: 22px; color: var(--ink-soft); }
.home-buttons { display: flex; flex-direction: column; gap: 16px; width: 100%; margin-top: 8px; }
.home-small { display: flex; flex-wrap: wrap; gap: 12px; justify-content: center; }
.msg-line { min-height: 1.4em; margin: 0; font-size: 19px; color: var(--ink-soft); }

.logo { position: relative; width: 150px; height: 104px; display: flex; gap: 4px; }
.logo .lk.w { flex: 1; background: #fff; border-radius: 0 0 12px 12px; box-shadow: var(--shadow); border: 3px solid var(--ink); }
.logo .lk.b { position: absolute; top: 0; width: 26px; height: 58px; background: var(--ink); border-radius: 0 0 8px 8px; }
.logo .b1 { left: 35px; } .logo .b2 { left: 87px; }
.logo-star { position: absolute; right: -28px; top: -26px; width: 64px; height: 64px; fill: var(--star); filter: drop-shadow(0 3px 0 rgba(0,0,0,0.15)); }

/* ---------- Panels ---------- */
.panel {
  margin: auto; width: min(720px, calc(100% - 32px)); max-height: calc(100% - 32px); overflow-y: auto;
  background: var(--card); border-radius: 28px; padding: 28px; box-shadow: var(--shadow);
  display: flex; flex-direction: column; gap: 16px; align-items: stretch;
}
.panel h2 { font-size: 34px; color: var(--brand-dark); }
.panel p { margin: 0; }
.help-steps { background: var(--bg); border-radius: 18px; padding: 16px 20px; display: flex; flex-direction: column; gap: 10px; }
.help-steps h3 { font-size: 20px; }
.row-buttons { display: flex; flex-wrap: wrap; gap: 14px; }
.row-buttons .big-btn { flex: 1 1 220px; justify-content: center; }
.col-buttons { display: flex; flex-direction: column; gap: 12px; }
.col-buttons .big-btn { justify-content: center; }

/* ---------- Top bar ---------- */
.topbar {
  display: flex; align-items: center; gap: 16px; padding: 14px 16px; flex: none;
}
.topbar h2 { flex: 1; font-size: 30px; color: var(--brand-dark); }
.mode-pill { font-size: 18px; font-weight: 700; padding: 8px 14px; border-radius: 99px; background: #fff; color: var(--ink-soft); display: flex; gap: 6px; align-items: center; }
.mode-pill .ico { width: 24px; height: 24px; }

/* ---------- Lesson map ---------- */
.map-scroll { flex: 1; overflow-y: auto; -webkit-overflow-scrolling: touch; padding: 4px 16px 24px; }
.lesson-list {
  list-style: none; margin: 0 auto; padding: 0; max-width: 980px;
  display: grid; grid-template-columns: repeat(auto-fill, minmax(280px, 1fr)); gap: 16px;
}
.lesson-card {
  width: 100%; min-height: 150px; border: none; border-radius: var(--radius); background: var(--card);
  box-shadow: var(--shadow); padding: 18px; display: flex; flex-direction: column; gap: 6px; text-align: left;
  position: relative;
}
.lesson-card:active { transform: translateY(4px); }
.lesson-card .num {
  width: 44px; height: 44px; border-radius: 50%; background: var(--brand); color: #fff;
  display: grid; place-items: center; font-weight: 800; font-size: 22px;
}
.lesson-card .t { font-size: 22px; font-weight: 800; }
.lesson-card .s { font-size: 18px; color: var(--ink-soft); }
.lesson-card .tag { font-size: 16px; font-weight: 700; color: var(--accent); }
.lesson-card .stars { position: absolute; right: 16px; top: 18px; display: flex; gap: 2px; }
.lesson-card .stars svg { width: 30px; height: 30px; fill: var(--star-empty); }
.lesson-card .stars svg.on { fill: var(--star); }
.lesson-card.locked { background: #ece8f7; color: #8d86a8; box-shadow: none; }
.lesson-card.locked .num { background: #b9b1d6; }
.lesson-card .lock { position: absolute; right: 18px; top: 18px; width: 34px; height: 34px; fill: #a59dc4; }
.lesson-card.next { outline: 4px solid var(--accent); }

/* ---------- Game ---------- */
.game-top { gap: 12px; padding-bottom: 6px; }
.game-title-wrap { flex: 1; min-width: 0; display: flex; flex-direction: column; gap: 8px; }
.game-title-wrap h2 { font-size: 24px; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.progress { height: 12px; border-radius: 99px; background: #fff; overflow: hidden; }
.progress-fill { height: 100%; width: 0; background: var(--good); border-radius: 99px; transition: width 0.3s; }
.game-stats { display: flex; gap: 10px; align-items: center; }
.stat { background: #fff; border-radius: 16px; padding: 4px 12px; text-align: center; min-width: 76px; box-shadow: var(--shadow); }
.stat-label { display: block; font-size: 14px; font-weight: 700; color: var(--ink-soft); text-transform: uppercase; letter-spacing: 0.5px; }
.stat-val { display: block; font-size: 26px; font-weight: 800; }
.mic-meter { display: flex; align-items: center; gap: 6px; background: #fff; border-radius: 16px; padding: 8px 12px; box-shadow: var(--shadow); }
.mic-meter .ico { width: 24px; height: 24px; color: var(--brand); }
.mic-meter .heard { font-weight: 800; min-width: 2.2em; text-align: center; }
.meter { display: inline-block; width: 50px; height: 12px; border-radius: 99px; background: #e8e3f7; overflow: hidden; vertical-align: middle; }
.meter.wide { width: min(360px, 60vw); height: 18px; }
.meter-fill { display: block; height: 100%; width: 0; background: var(--good); border-radius: 99px; }
.meter-fill.clear { background: var(--brand); }

.prompt { display: flex; align-items: center; justify-content: center; gap: 18px; padding: 2px 16px 8px; flex: none; }
.prompt-text { font-size: 34px; font-weight: 800; color: var(--ink); }
.prompt-text b { display: inline-block; min-width: 1.3em; padding: 0 10px; border-radius: 14px; color: #fff; text-align: center; }
.hear-btn { display: inline-flex; align-items: center; gap: 6px; min-height: 52px; padding: 6px 16px; border-radius: 16px; border: 2px solid #cfc4f2; background: #fff; font-size: 18px; font-weight: 700; }
.hear-btn .ico { width: 26px; height: 26px; }

.lane-wrap { position: relative; flex: 1; min-height: 120px; margin: 0 12px; border-radius: 22px 22px 0 0; overflow: hidden; background: var(--lane); }
#lane { position: absolute; inset: 0; width: 100%; height: 100%; display: block; }
.countdown { position: absolute; inset: 0; display: grid; place-items: center; font-size: 120px; font-weight: 900; color: #fff; pointer-events: none; text-shadow: 0 6px 0 rgba(0,0,0,0.3); }
.feedback { position: absolute; left: 0; right: 0; top: 14px; text-align: center; font-size: 30px; font-weight: 800; color: #fff; pointer-events: none; opacity: 0; transition: opacity 0.2s; text-shadow: 0 3px 0 rgba(0,0,0,0.35); }
.feedback.show { opacity: 1; }
.feedback.good { color: #9ff5c6; }
.feedback.try { color: #ffd2a8; }

/* Keyboard */
.keyboard {
  position: relative; flex: none; height: 30vh; min-height: 170px; max-height: 300px;
  margin: 0 12px 12px; background: var(--ink); border-radius: 0 0 22px 22px; padding: 6px 6px 8px;
  display: flex; gap: 4px; touch-action: none;
}
.key { border: none; padding: 0; position: relative; display: flex; flex-direction: column; justify-content: flex-end; align-items: center; }
.key.white {
  flex: 1; background: #fff; border-radius: 0 0 14px 14px; padding-bottom: 12px; box-shadow: inset 0 -8px 0 #e3ddf5;
  font-size: 26px; font-weight: 800; color: var(--ink-soft);
}
.key.black {
  position: absolute; top: 6px; height: 58%; background: #1a1433; border-radius: 0 0 10px 10px; z-index: 2;
  box-shadow: inset 0 -6px 0 #000; color: #9e95c4; font-size: 15px; font-weight: 700; padding-bottom: 8px;
}
.key .mid-c { font-size: 14px; font-weight: 800; color: var(--brand); margin-bottom: 4px; line-height: 1.1; text-align: center; }
.key.target .mid-c, .key.hit .mid-c { color: #fff; }
.key.pressed.white { background: #e9e3ff; box-shadow: inset 0 -3px 0 #d7cff3; }
.key.pressed.black { background: #3a2f6b; }
.key.target { animation: glow 1s ease-in-out infinite alternate; }
.key.target.white { background: var(--target-color, #ffe066); color: #fff; box-shadow: inset 0 -8px 0 rgba(0,0,0,0.15); }
.key.target.black { background: var(--target-color, #ffe066); }
.key.hit { background: var(--good) !important; color: #fff; }
.key.miss { background: #ffd0d7 !important; }
@keyframes glow { from { filter: brightness(1); } to { filter: brightness(1.15); } }

.overlay { position: absolute; inset: 0; background: rgba(36, 26, 74, 0.55); display: flex; z-index: 10; }

/* ---------- Results ---------- */
.results-panel { text-align: center; align-items: center; position: relative; z-index: 2; }
.results-panel h2 { font-size: 44px; }
.result-stars { display: flex; gap: 10px; justify-content: center; }
.result-stars svg { width: 96px; height: 96px; fill: var(--star-empty); }
.result-stars svg.on { fill: var(--star); animation: pop 0.5s cubic-bezier(.3,1.6,.5,1) both; }
.result-stars svg.on:nth-child(2) { animation-delay: 0.25s; }
.result-stars svg.on:nth-child(3) { animation-delay: 0.5s; }
@keyframes pop { from { transform: scale(0); } to { transform: scale(1); } }
.result-detail { font-size: 22px; }
.result-unlock { font-size: 22px; font-weight: 800; color: var(--good); min-height: 1.4em; }
.results-panel .row-buttons { width: 100%; }

.confetti { position: absolute; inset: 0; pointer-events: none; overflow: hidden; z-index: 1; }
.confetti i { position: absolute; top: -20px; width: 14px; height: 18px; border-radius: 4px; animation: fall 2.6s linear forwards; }
@keyframes fall { to { transform: translateY(110vh) rotate(540deg); } }

/* ---------- Tune-up ---------- */
.tune-body { flex: 1; min-height: 0; overflow-y: auto; display: flex; flex-direction: column; align-items: center; justify-content: center; gap: 14px; padding: 16px; text-align: center; }
.tune-hint { font-size: 24px; margin: 0; max-width: 640px; }
.tune-note { font-size: 160px; font-weight: 900; line-height: 1; color: var(--brand-dark); min-height: 1em; }
.tune-match { font-size: 26px; font-weight: 800; min-height: 1.4em; color: var(--good); }
.tune-bars { display: flex; flex-direction: column; gap: 10px; font-size: 20px; font-weight: 700; }
.tune-bars label { display: flex; align-items: center; gap: 12px; justify-content: space-between; }
.tune-detail { font-size: 18px; color: var(--ink-soft); margin: 0; }

/* ---------- Grown-ups ---------- */
.gu-panel { margin-top: 0; }
.toggle { display: flex; gap: 14px; align-items: flex-start; font-size: 20px; min-height: 48px; }
.toggle input { width: 32px; height: 32px; flex: none; accent-color: var(--brand); }
.gu-note { font-size: 18px; color: var(--ink-soft); }

/* ---------- Listening log & recorder ---------- */
.log-body { flex: 1; min-height: 0; display: flex; flex-direction: column; gap: 10px; padding: 0 16px 16px; }
.log-buttons .small-btn { flex: 0 1 auto; }
.log-text {
  flex: 1; min-height: 0; overflow: auto; -webkit-overflow-scrolling: touch; margin: 0; padding: 12px;
  background: #fff; border-radius: 16px; font: 15px/1.45 ui-monospace, Menlo, monospace; color: var(--ink);
  white-space: pre-wrap; word-break: break-word; -webkit-user-select: text; user-select: text;
}
.record-box { display: flex; flex-direction: column; align-items: center; gap: 8px; max-width: 640px; }
.record-msg { margin: 0; font-size: 18px; color: var(--ink-soft); }
a.small-btn { text-decoration: none; color: var(--ink); }

/* ---------- Smaller / portrait screens ---------- */
@media (max-height: 640px) {
  .home-wrap h1 { font-size: 42px; }
  .logo { display: none; }
  .keyboard { height: 34vh; min-height: 140px; }
  .prompt-text { font-size: 28px; }
}
@media (max-height: 460px) {
  .topbar { padding: 8px 12px; }
  .icon-btn { width: 52px; height: 52px; }
  .stat-val { font-size: 22px; }
  .prompt { padding-bottom: 4px; }
  .prompt-text { font-size: 24px; }
  .hear-btn { min-height: 44px; }
  .keyboard { height: 36vh; min-height: 120px; margin-bottom: 6px; }
  .panel { padding: 18px; gap: 10px; }
  .big-btn { min-height: 60px; }
}
@media (max-width: 700px) {
  .stat { min-width: 64px; padding: 4px 8px; }
  .mic-meter .meter { display: none; }
  .game-title-wrap h2 { font-size: 20px; }
  .key.white { font-size: 22px; }
}
@media (orientation: portrait) and (min-height: 900px) {
  .keyboard { height: 26vh; }
}

@media (prefers-reduced-motion: reduce) {
  *, *::before, *::after { animation: none !important; transition: none !important; }
  .confetti { display: none; }
}
