:root {
  --left: #3b5bdb;
  --right: #d6336c;
  --ink: #222;
  --accent: #7a3b8f;
  --accent-soft: #f1e6f6;
  --paper: #fffdf8;
  --bg: #f4ecdf;
  --good: #2f9e44;
  --close: #e8590c;
  --safe-bottom: env(safe-area-inset-bottom, 0px);
}
* { box-sizing: border-box; }
html, body { margin: 0; height: 100%; }
body {
  background: var(--bg);
  color: var(--ink);
  font-family: system-ui, -apple-system, 'Segoe UI', Roboto, sans-serif;
  -webkit-user-select: none;
  user-select: none;
  -webkit-touch-callout: none;
  -webkit-tap-highlight-color: transparent;
  overflow: hidden;
}
button { font: inherit; color: inherit; touch-action: manipulation; cursor: pointer; }
button:disabled { opacity: .35; cursor: default; }
button:focus-visible { outline: 3px solid #f0b400; outline-offset: 2px; }
[hidden] { display: none !important; }

.screen { height: 100dvh; display: flex; flex-direction: column; }
.topbar { display: flex; align-items: center; gap: 8px; padding: 8px 10px; background: var(--accent); color: #fff; }
.topbar h1 { margin: 0; font-size: 22px; flex: 1; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
#home .topbar { padding-right: 146px; }
.topbar .spacer { width: 56px; }
.icon-btn { min-width: 56px; min-height: 56px; border: 0; border-radius: 14px; background: #ffffff26; color: #fff; font-size: 24px; display: inline-flex; align-items: center; justify-content: center; text-decoration: none; }
.name-btn { flex: 1; min-height: 56px; border: 0; border-radius: 14px; background: transparent; color: #fff; font-size: 20px; font-weight: 800; text-align: left; padding: 0 8px; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }

/* ---------- home ---------- */
.home-body { flex: 1; overflow: auto; padding: 16px 16px calc(24px + var(--safe-bottom)); }
.home-body h2 { font-size: 20px; margin: 22px 4px 10px; color: var(--accent); }
.big-actions { display: grid; grid-template-columns: 1fr 1fr; gap: 12px; max-width: 720px; }
.big-btn { min-height: 120px; border: 0; border-radius: 22px; background: #fff; box-shadow: 0 4px 0 #0002; font-size: 19px; font-weight: 800; display: flex; flex-direction: column; align-items: center; justify-content: center; gap: 6px; }
.big-btn .emoji { font-size: 44px; line-height: 1; }
.big-btn.make { background: #ffe8a3; }
.big-btn.mic { background: #c3f0d2; }
.song-grid { display: grid; grid-template-columns: repeat(auto-fill, minmax(240px, 1fr)); gap: 12px; }
.song-card { display: flex; background: #fff; border-radius: 18px; box-shadow: 0 3px 0 #0001; overflow: hidden; }
.song-open { flex: 1; min-height: 76px; border: 0; background: transparent; text-align: left; padding: 10px 14px; display: flex; flex-direction: column; justify-content: center; gap: 4px; }
.song-open b { font-size: 18px; }
.song-open span { color: var(--accent); letter-spacing: 2px; }
.song-play { width: 76px; border: 0; background: var(--accent-soft); color: var(--accent); font-size: 28px; }

/* ---------- song screen ---------- */
#song { display: grid; grid-template-columns: minmax(0, 1fr); grid-template-rows: auto auto minmax(0, 1fr) auto; }
.play-top { background: #c3f0d2; color: var(--accent); }
.throw { margin-top: 18px; font-size: 16px; padding: 0 16px; }
.modes { display: flex; gap: 8px; min-width: 0; padding: 8px 10px; background: #fff8ec; border-bottom: 2px solid #eadfcd; }
.mode { min-height: 52px; padding: 0 12px; white-space: nowrap; border: 2px solid #d9c9e3; border-radius: 26px; background: #fff; font-weight: 800; font-size: 16px; }
.mode[aria-pressed="true"] { background: var(--accent); border-color: var(--accent); color: #fff; }
.mode.action { margin-left: auto; background: #c3f0d2; border-color: #8fd6a8; }
.sheet { overflow: auto; background: var(--paper); padding: 6px 4px; touch-action: pan-x pan-y; }
.sheet-svg { display: block; margin: 0 auto; }

.panel { background: var(--accent-soft); border-top: 2px solid #dccbe6; padding: 8px 10px calc(10px + var(--safe-bottom)); display: flex; flex-direction: column; gap: 8px; }
.recorder-wrap { display: flex; justify-content: center; }
#recorder { width: 100%; max-width: 560px; height: auto; touch-action: none; }
#recorder.dim { opacity: .45; }
.lengths { display: flex; gap: 8px; align-items: stretch; }
.note-big { width: 76px; min-height: 64px; border: 3px solid var(--accent); border-radius: 16px; background: #fff; display: flex; flex-direction: column; align-items: center; justify-content: center; line-height: 1; }
.note-big b { font-size: 28px; color: var(--accent); }
.note-big small { font-size: 11px; color: #777; }
.note-big.unknown { border-style: dashed; }
.length { flex: 1; min-height: 64px; border: 3px solid #d9c9e3; border-radius: 16px; background: #fff; padding: 2px; }
.length svg { width: 100%; max-width: 64px; height: 44px; }
.length[aria-pressed="true"] { border-color: var(--accent); background: #fbf3ff; box-shadow: inset 0 0 0 2px var(--accent); }

.thumbs { display: flex; justify-content: space-between; align-items: flex-end; gap: 10px; }
.thumb-left, .thumb-right { display: flex; gap: 8px; align-items: flex-end; }
.pad { min-width: 64px; min-height: 64px; border: 0; border-radius: 18px; background: #fff; box-shadow: 0 3px 0 #0002; font-size: 26px; font-weight: 800; touch-action: none; }
.pad:active:not(:disabled) { transform: translateY(2px); box-shadow: 0 1px 0 #0002; }
.pad.primary { min-width: 84px; min-height: 84px; background: #ffd43b; font-size: 38px; }
.pad.play { background: #c3f0d2; }
.pad.wide { min-width: 120px; }
.pad.danger { background: #ffc9c9; }

/* ---------- practice ---------- */
.practice-now { display: flex; gap: 10px; align-items: stretch; min-height: 150px; }
.target, .heard { background: #fff; border-radius: 18px; display: flex; align-items: center; justify-content: center; gap: 10px; padding: 6px 12px; }
.target { flex: 1.3; border: 3px solid #f0b400; }
.target svg { height: 150px; width: 46px; }
.target b { font-size: 46px; color: var(--accent); }
.heard { flex: 1; flex-direction: column; gap: 2px; }
.heard .ear { font-size: 26px; }
.heard b { font-size: 34px; }
.level { width: 80%; height: 10px; background: #eee; border-radius: 5px; overflow: hidden; }
.level i { display: block; height: 100%; width: 0; background: var(--good); transition: width .08s; }
.summary { flex: 1; background: #fff; border-radius: 18px; display: flex; flex-wrap: wrap; align-items: center; justify-content: center; gap: 8px; padding: 6px; }
.summary p { width: 100%; text-align: center; margin: 0; font-size: 20px; }
.summary b { font-size: 30px; color: var(--good); }
.listening #practice-go { background: #ff8787; animation: breathe 1.4s ease-in-out infinite; }
@keyframes breathe { 50% { box-shadow: 0 0 0 8px #ff878766, 0 3px 0 #0002; } }

/* ---------- sheet drawing ---------- */
.staff { stroke: #666; stroke-width: 1.1; }
.bar { stroke: #666; stroke-width: 1.2; }
.final { fill: #333; }
.head { fill: var(--ink); }
.head.hollow { fill: #fff; stroke: var(--ink); stroke-width: 2.2; }
.stem { stroke: var(--ink); stroke-width: 1.5; }
.dot, .rest, .ink { fill: var(--ink); }
.ink-line { fill: none; stroke: var(--ink); stroke-linecap: round; stroke-linejoin: round; }
.label { font-size: 15px; font-weight: 800; fill: #444; }
.rest-label { font-size: 11px; font-weight: 600; fill: #999; }
.hold { font-size: 15px; font-weight: 800; fill: #bbb; }
.time { font-size: 22px; font-weight: 800; font-family: Georgia, serif; fill: var(--ink); }
.hole.on.left { fill: var(--left); }
.hole.on.right { fill: var(--right); }
.hole.off { fill: #fff; stroke: #9a9a9a; stroke-width: 1.4; }
.hole.half-ring.left { fill: #fff; stroke: var(--left); stroke-width: 1.4; }
.hole.half-ring.right { fill: #fff; stroke: var(--right); stroke-width: 1.4; }
.hit { fill: transparent; }
.col { cursor: pointer; }
.col.selected .hit { fill: #f3d9ff; stroke: #b57edc; stroke-width: 2; }
.col.now .hit { fill: #fff3bf; stroke: #f0b400; stroke-width: 2.5; }
.col.playing .hit { fill: #d0ebff; }
.col.right .hit { fill: #d3f9d8; }
.col.miss .hit { fill: #ffe8cc; }
.mark { font-size: 18px; font-weight: 900; }
.mark.right { fill: var(--good); }
.mark.miss { fill: var(--close); }
.empty-hint { font-size: 16px; fill: #888; font-weight: 700; }

/* ---------- recorder drawing ---------- */
.wood { fill: #e9c79a; stroke: #a57a45; stroke-width: 2; }
.window { fill: #6b4a2a; }
.joint { stroke: #a57a45; stroke-width: 1.5; }
.thumb-link { stroke: #a57a45; stroke-width: 2; stroke-dasharray: 3 3; }
.thumb-label { font-size: 15px; }
.hole-back { fill: transparent; }
#recorder .hole.off { stroke: #7a5a33; stroke-width: 2.5; fill: #fff8ee; }
.icon-head { fill: var(--ink); }
.icon-head.hollow { fill: #fff; stroke: var(--ink); stroke-width: 2.4; }
.icon-stem { stroke: var(--ink); stroke-width: 2; }
.icon-dot { fill: var(--ink); }
.beat-dot { fill: var(--accent); }

/* ---------- help hand, dialogs, toast ---------- */
.hand { position: fixed; z-index: 50; font-size: 46px; pointer-events: none; filter: drop-shadow(0 3px 2px #0004); }
.hand.tap { animation: tap 1.1s ease-in-out 3; }
@keyframes tap { 0%, 100% { transform: translate(0, 18px); } 45% { transform: translate(0, 0) scale(.9); } 55% { transform: translate(0, 0) scale(.9); } }

dialog { border: 0; border-radius: 24px; padding: 20px; width: min(420px, calc(100vw - 24px)); text-align: center; box-shadow: 0 10px 40px #0005; }
dialog::backdrop { background: #2a1838aa; }
dialog h2 { margin: 0 0 14px; color: var(--accent); }
.dialog-actions { display: flex; justify-content: center; gap: 12px; margin-top: 14px; }
.dialog-actions.column { flex-direction: column; align-items: stretch; }
.dialog-actions.column .big-btn { min-height: 84px; flex-direction: row; background: var(--accent-soft); }
.dialog-actions.column .pad { align-self: center; }
#name-input { width: 100%; font-size: 22px; padding: 12px; border: 3px solid #d9c9e3; border-radius: 14px; -webkit-user-select: text; user-select: text; }
.listen-ring { --level: 0; width: 160px; height: 160px; margin: 0 auto; border-radius: 50%; background: radial-gradient(circle, #c3f0d2 0, #c3f0d2 calc(35% + var(--level) * 30%), #f1e6f6 calc(36% + var(--level) * 30%)); display: flex; align-items: center; justify-content: center; border: 4px solid var(--accent); }
.listen-ring b { font-size: 48px; color: var(--accent); }
.listen-ring.pop { animation: pop .25s ease-out; }
@keyframes pop { 50% { transform: scale(1.08); } }
.listen-heard { min-height: 1.4em; font-weight: 800; letter-spacing: 1px; color: #555; word-break: break-word; }
.listen-status { color: #555; }
.toast { position: fixed; left: 50%; top: 84px; transform: translateX(-50%); background: #2a1838; color: #fff; padding: 12px 18px; border-radius: 16px; font-weight: 700; z-index: 60; max-width: calc(100vw - 32px); text-align: center; }
body:has(#song:not([hidden])) footer { display: none; }
/* Arcade-wide shop overlay: in the home top bar, and out of the way while making or practising. */
body:has(#home:not([hidden])) .arcade-shop-link { top: calc(10px + env(safe-area-inset-top, 0px)) !important; right: 10px !important; }
body:has(#song:not([hidden])) .arcade-shop-link, body:has(#song:not([hidden])) .arcade-reward-badge { display: none !important; }
footer { position: fixed; left: 6px; bottom: 2px; font-size: 9px; color: #0006; pointer-events: none; }

/* ---------- phone landscape: sheet on the left, controls on the right under the right thumb ---------- */
@media (orientation: landscape) and (max-height: 600px) {
  #song { grid-template-columns: minmax(0, 1fr) min(400px, 50vw); grid-template-rows: auto auto minmax(0, 1fr); }
  #song .topbar { grid-column: 1 / -1; padding: 4px 10px; }
  #song .icon-btn, #song .name-btn { min-height: 48px; }
  .modes { grid-column: 1; padding: 6px 10px; }
  .sheet { grid-column: 1; grid-row: 3; }
  .panel { grid-column: 2; grid-row: 2 / 4; border-top: 0; border-left: 2px solid #dccbe6; overflow: hidden; }
  .panel .thumbs { margin-top: auto; }
  .practice-now { min-height: 0; flex: 1; }
  /* Left thumb gets its own corner over the sheet. */
  .panel .thumb-left { position: fixed; left: calc(10px + env(safe-area-inset-left, 0px)); bottom: calc(10px + var(--safe-bottom)); z-index: 5; }
  .sheet { padding-bottom: 96px; }
  .panel .thumb-right { margin-left: auto; }
  .target svg { height: 120px; width: 37px; }
}
@media (max-width: 380px) {
  .pad { min-width: 58px; }
  .pad.primary { min-width: 76px; }
}
@media (max-width: 420px) {
  #home .title-icon { display: none; }
  #home .topbar h1 { font-size: 20px; }
}
