:root {
  --ink: #1c2430;
  --ink-soft: #445066;
  --room: #35577a;
  --room-deep: #243e5c;
  --sky: #d5e4ec;
  --paper: #f7f1e6;
  --wood: #c9864a;
  --wood-deep: #8a4e2b;
  --wood-shadow: #6e3c20;
  --ivory: #fff8ef;
  --ivory-edge: #e4d3c0;
  --ebony: #241f1b;
  --gold: #e6b23c;
  --gold-deep: #a87412;
  --display: "Iowan Old Style", Palatino, "Palatino Linotype", Georgia, serif;
  --ui: "Avenir Next", Avenir, "Trebuchet MS", "Segoe UI", sans-serif;
  --bead: 46px;
}

* { box-sizing: border-box; }
[hidden] { display: none !important; }
html, body { height: 100%; margin: 0; }
body {
  font-family: var(--ui);
  color: var(--ink);
  background:
    radial-gradient(1100px 480px at 8% -10%, rgba(255, 255, 255, 0.16), transparent 50%),
    radial-gradient(800px 520px at 110% 120%, rgba(0, 0, 0, 0.2), transparent 46%),
    var(--room);
  overflow: hidden;
  touch-action: manipulation;
  -webkit-user-select: none;
  user-select: none;
  -webkit-touch-callout: none;
}
button, input { font-family: inherit; }
button { cursor: pointer; }
:focus-visible { outline: 3px solid var(--gold); outline-offset: 3px; }

#app {
  height: 100%;
  display: flex;
  flex-direction: column;
  padding:
    env(safe-area-inset-top)
    env(safe-area-inset-right)
    env(safe-area-inset-bottom)
    env(safe-area-inset-left);
}

.top {
  display: flex;
  align-items: center;
  gap: 12px;
  padding: 8px 14px 6px;
  min-height: 62px;
  color: var(--ivory);
}
.brand { flex: 1; min-width: 0; }
.title-row { display: flex; align-items: center; min-width: 0; gap: 8px; }
h1 {
  margin: 0;
  font-family: var(--display);
  font-weight: 600;
  font-size: clamp(1.35rem, 2.5vw, 2rem);
  letter-spacing: -0.02em;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}
#subhead { margin: 1px 0 0; color: #d7e6ef; font-size: 0.92rem; }
.top-actions { display: flex; gap: 8px; flex: none; }
#score {
  font-weight: 800;
  color: var(--gold);
  font-size: 1.35rem;
  flex: none;
}

.logo-bead {
  width: 34px;
  height: 34px;
  border-radius: 50%;
  background: #d64545;
  color: #fffaf6;
  display: grid;
  place-items: center;
  font-weight: 800;
  flex: none;
  box-shadow: inset 0 -4px 0 rgba(0, 0, 0, 0.16);
}
@media (prefers-reduced-motion: no-preference) {
  .logo-bead { animation: bead-in 0.55s ease; }
}
@keyframes bead-in {
  from { transform: translateY(-16px); opacity: 0; }
  to { transform: none; opacity: 1; }
}

.btn {
  border: 0;
  background: var(--ivory);
  color: var(--ink);
  font-weight: 800;
  font-size: 1rem;
  border-radius: 12px;
  padding: 10px 14px;
  box-shadow: 4px 4px 0 var(--room-deep);
}
.btn:active { transform: translate(2px, 2px); box-shadow: 2px 2px 0 var(--room-deep); }
.btn.main {
  background: var(--gold);
  box-shadow: 4px 4px 0 var(--wood-shadow);
}
.btn.main:active { box-shadow: 2px 2px 0 var(--wood-shadow); }
.btn.quiet {
  background: transparent;
  color: var(--ivory);
  box-shadow: none;
  padding-inline: 8px;
}
.sheet .btn.quiet,
.intro .btn.quiet { color: var(--ink); }

#lyric {
  margin: 0 14px 8px;
  background: var(--paper);
  color: var(--ink);
  border-radius: 12px;
  min-height: 44px;
  padding: 8px 16px;
  text-align: center;
  font-weight: 800;
  font-size: 1.35rem;
  line-height: 1.35;
}
#lyric .on { font-size: 1.45rem; }

#main {
  flex: 1;
  min-height: 0;
  margin: 0 14px;
  position: relative;
}
#main.home-scroll,
#main.reader,
#main.song-screen { overflow: auto; }
#main.song-screen {
  display: flex;
  flex-direction: column;
  justify-content: center;
}

.intro {
  display: flex;
  align-items: center;
  gap: 12px;
  background: var(--paper);
  border-radius: 14px;
  padding: 10px 12px;
  margin-bottom: 12px;
  box-shadow: 4px 4px 0 var(--room-deep);
}
.intro p { margin: 0; flex: 1; line-height: 1.35; }

.shelf {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(150px, 1fr));
  gap: 14px;
  padding: 2px 2px 18px;
}
.book {
  position: relative;
  display: flex;
  flex-direction: column;
  text-align: left;
  padding: 0;
  border: 0;
  border-radius: 8px 16px 14px 8px;
  background: var(--paper);
  color: var(--ink);
  box-shadow: 5px 5px 0 var(--room-deep);
  overflow: hidden;
  min-height: 168px;
}
.book:active { transform: translate(2px, 2px); box-shadow: 3px 3px 0 var(--room-deep); }
.book-band {
  height: 86px;
  display: grid;
  place-items: center;
  background: var(--band);
}
.book-art {
  width: 70px;
  height: 70px;
  border-radius: 50%;
  background: var(--ivory);
  display: grid;
  place-items: center;
  box-shadow: 3px 3px 0 rgba(28, 36, 48, 0.18);
}
.book-art svg { width: 54px; height: 54px; }
.book-body { padding: 8px 10px 12px; display: flex; flex-direction: column; gap: 2px; }
.book-title {
  font-family: var(--display);
  font-size: 1.2rem;
  line-height: 1.05;
}
.book-blurb { color: var(--ink-soft); font-size: 0.8rem; font-weight: 700; }
.ribbon {
  position: absolute;
  z-index: 2;
  top: 8px;
  left: 0;
  background: var(--gold);
  color: var(--ink);
  font-size: 0.75rem;
  font-weight: 800;
  padding: 3px 8px 3px 6px;
}
.star {
  position: absolute;
  z-index: 2;
  top: 4px;
  right: 8px;
  color: #fff8ef;
  font-size: 1.15rem;
  text-shadow: 0 1px 0 rgba(28, 36, 48, 0.45);
}

.song-pick {
  max-width: 640px;
  margin: 8px auto 16px;
  display: flex;
  flex-direction: column;
  align-items: center;
  text-align: center;
  gap: 12px;
}
.song-hero {
  width: 112px;
  height: 112px;
  border-radius: 50%;
  background: var(--ivory);
  display: grid;
  place-items: center;
  box-shadow: 5px 5px 0 var(--room-deep);
}
.song-hero svg { width: 78px; height: 78px; }
.best { margin: 0; color: var(--ivory); font-weight: 700; }
.mode-grid {
  width: min(460px, 100%);
  display: grid;
  grid-template-columns: 1fr;
  gap: 12px;
}
.mode-grid .btn { padding: 14px 16px; font-size: 1.15rem; }
.mode-grid .main { font-size: 1.55rem; padding: 16px 18px; }
.hint {
  margin: 0;
  max-width: 46ch;
  color: #d7e6ef;
  font-size: 0.92rem;
  line-height: 1.35;
}

#main.stage { overflow: hidden; }
#highway {
  position: absolute;
  inset: 0 0 6px 0;
  border-radius: 18px 18px 8px 8px;
  background: linear-gradient(#eef6f8, var(--sky) 70%);
  overflow: hidden;
}
#progress {
  position: absolute;
  top: 0;
  left: 0;
  right: 0;
  height: 6px;
  background: rgba(28, 36, 48, 0.08);
  z-index: 4;
}
#progress span {
  display: block;
  height: 100%;
  width: 0;
  background: var(--gold);
}
#countin {
  position: absolute;
  top: 16px;
  left: 0;
  right: 0;
  display: flex;
  justify-content: center;
  gap: 10px;
  z-index: 4;
}
#countin i {
  width: 12px;
  height: 12px;
  border-radius: 50%;
  background: rgba(28, 36, 48, 0.16);
}
#countin i.on { background: var(--gold); }
#lanes { position: absolute; top: 0; bottom: 0; left: 12px; right: 12px; }
.thread {
  position: absolute;
  top: 0;
  bottom: 0;
  width: 2px;
  margin-left: -1px;
  opacity: 0.3;
  z-index: 0;
}
.bead {
  position: absolute;
  z-index: 2;
  width: var(--bead);
  height: var(--bead);
  margin-left: calc(var(--bead) / -2);
  border-radius: 50%;
  display: grid;
  place-items: center;
  font-weight: 800;
  font-size: 1.25rem;
  box-shadow: inset 0 -5px 0 rgba(0, 0, 0, 0.14), 0 4px 0 rgba(28, 36, 48, 0.08);
}
.bead.high { box-shadow: inset 0 0 0 3px rgba(255, 255, 255, 0.92), inset 0 -5px 0 rgba(0, 0, 0, 0.14); }
.bead.no-letter .bead-letter { visibility: hidden; }
.bead .finger {
  position: absolute;
  top: -4px;
  right: -4px;
  width: 18px;
  height: 18px;
  border-radius: 50%;
  background: var(--ivory);
  color: var(--ink);
  font-style: normal;
  font-size: 11px;
  font-weight: 800;
  display: none;
  place-items: center;
  box-shadow: 0 1px 0 rgba(28, 36, 48, 0.25);
}
.bead.show-finger .finger { display: grid; }
.bead.wait { animation: bead-wait 0.8s ease-in-out infinite; }
.bead.pop { animation: bead-pop 0.34s ease forwards; z-index: 5; }
.bead.miss { animation: bead-miss 0.4s ease forwards; }
@keyframes bead-wait {
  50% { transform: translateY(-5px) scale(1.06); }
}
@keyframes bead-pop {
  to { transform: translateY(10px) scale(1.35); opacity: 0; }
}
@keyframes bead-miss {
  to { transform: translateY(16px); opacity: 0; }
}

#dock {
  margin: 0 14px 10px;
  height: clamp(148px, 28vh, 220px);
  display: flex;
  flex-direction: column;
  padding: 0 12px 8px;
  border-radius: 14px 14px 12px 12px;
  background:
    repeating-linear-gradient(90deg, rgba(255, 255, 255, 0.05) 0 2px, transparent 2px 8px),
    linear-gradient(#d39255, #c17a3e 42%, #b56b32);
  box-shadow: 0 7px 0 var(--wood-shadow);
}
.rail { position: relative; height: 24px; flex: none; }
.mc {
  position: absolute;
  top: 4px;
  color: #fff6ea;
  font-size: 11px;
  font-weight: 800;
  letter-spacing: 0.01em;
  white-space: nowrap;
}
.keys { position: relative; flex: 1; touch-action: none; }
.key {
  position: absolute;
  transform: translateX(-50%);
  border: 0;
  padding: 0;
  touch-action: none;
}
.key.white {
  bottom: 0;
  width: calc(100% / 14);
  height: 100%;
  background: linear-gradient(#fffdf9, var(--ivory));
  border-radius: 0 0 8px 8px;
  box-shadow: inset -2px 0 0 var(--ivory-edge), 0 2px 0 rgba(80, 48, 20, 0.18);
  z-index: 1;
}
.key.black {
  top: 0;
  width: calc(100% / 14 * 0.58);
  height: 62%;
  background: linear-gradient(#3c352f, var(--ebony) 55%);
  border-radius: 0 0 5px 5px;
  z-index: 2;
  box-shadow: inset 0 -3px 0 rgba(0, 0, 0, 0.35);
}
.key .dot {
  position: absolute;
  top: 8px;
  left: 50%;
  width: 8px;
  height: 8px;
  margin-left: -4px;
  border-radius: 50%;
}
.key .face {
  position: absolute;
  left: 0;
  right: 0;
  bottom: 8px;
  font-weight: 800;
  font-size: clamp(0.95rem, 2vh, 1.35rem);
}
.key.white.down { transform: translateX(-50%) translateY(6px); background: #ffe7b8; }
.key.black.down { transform: translateX(-50%) translateY(3px); filter: brightness(1.35); }
.key.white.go {
  background: #fff3cc;
  box-shadow: inset 0 0 0 4px var(--gold), inset -2px 0 0 var(--ivory-edge);
}
.key.white.soon { box-shadow: inset 0 0 0 3px rgba(230, 178, 60, 0.85), inset -2px 0 0 var(--ivory-edge); }
.key.go .face { transform: scale(1.12); }
.key.nudge { animation: nudge 0.28s ease; }
@keyframes nudge {
  30% { transform: translateX(calc(-50% - 5px)); }
  70% { transform: translateX(calc(-50% + 5px)); }
}

.reader { display: flex; flex-direction: column; gap: 8px; padding-bottom: 8px; }
.levels, .help-switch { display: flex; gap: 8px; }
.levels { overflow-x: auto; }
.levels button, .help-switch button { flex: 0 0 auto; }
.help-switch button { flex: 1; }
.levels button.on, .help-switch button.on { background: var(--gold); }
.read-hint { margin: 0; color: #d7e6ef; font-weight: 700; min-height: 1.2em; }
.staff-card {
  flex: 1;
  min-height: 180px;
  background: var(--paper);
  border-radius: 18px;
  box-shadow: 5px 5px 0 var(--room-deep);
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  padding: 6px 8px 2px;
}
.staff-svg { width: min(780px, 100%); height: auto; display: block; }
#note-name {
  margin: 0;
  min-height: 1.3em;
  font-family: var(--display);
  font-size: clamp(1.5rem, 3vw, 2.1rem);
}
#pips { display: flex; justify-content: center; gap: 8px; min-height: 22px; }
#pips i {
  width: 13px;
  height: 13px;
  border-radius: 50%;
  background: rgba(255, 248, 239, 0.35);
}
#pips i.on { background: var(--gold); }
.staff-card.shake { animation: card-shake 0.28s ease; }
@keyframes card-shake {
  30% { transform: translateX(-6px); }
  70% { transform: translateX(6px); }
}

#overlay {
  position: fixed;
  inset: 0;
  z-index: 20;
  background: rgba(20, 28, 40, 0.48);
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 22px;
}
.sheet {
  width: min(540px, 100%);
  max-height: min(86vh, 640px);
  overflow: auto;
  background: var(--paper);
  color: var(--ink);
  border-radius: 18px;
  padding: 20px 20px 16px;
  box-shadow: 6px 6px 0 var(--room-deep);
}
.sheet h2 {
  margin: 0 0 6px;
  font-family: var(--display);
  font-weight: 600;
  font-size: clamp(1.6rem, 3vw, 2.2rem);
}
.sheet > p { margin: 0 0 14px; line-height: 1.35; font-size: 1.05rem; }
.sheet-actions { display: flex; flex-wrap: wrap; gap: 10px; }
.parents .how { color: var(--ink-soft); font-size: 0.95rem; }
.parents .how p { margin: 0 0 8px; line-height: 1.4; }
.speed {
  display: grid;
  grid-template-columns: auto 1fr auto;
  gap: 10px;
  align-items: center;
  font-weight: 800;
  margin: 8px 0 12px;
}
.speed input { width: 100%; accent-color: var(--wood-deep); }
.check {
  display: flex;
  align-items: center;
  gap: 8px;
  font-weight: 700;
  margin: 0 0 8px;
}
.parents h2 { margin-bottom: 10px; }

#turn {
  display: none;
  position: fixed;
  inset: 0;
  z-index: 30;
  background: var(--room);
  color: var(--ivory);
  align-items: center;
  justify-content: center;
  text-align: center;
  padding: 28px;
}
.turn-title {
  font-family: var(--display);
  font-size: 2.3rem;
  margin: 0 0 8px;
}
body.need-turn #turn { display: flex; }
body.need-turn #app,
body.need-turn #overlay { display: none !important; }

@media (prefers-reduced-motion: reduce) {
  .bead.wait, .bead.pop, .bead.miss, .key.nudge, .logo-bead, .staff-card.shake {
    animation: none;
  }
}
