* { box-sizing: border-box; margin: 0; padding: 0; }

:root {
  --bg-1: #0b0820;
  --bg-2: #2a1854;
  --bg-3: #66308f;
  --gold: #ffd97a;
  --pink: #ff8fc7;
  --cream: #fff6e0;
  --ink: #2b1242;
}

html {
  height: 100%;
  height: 100dvh;
  overscroll-behavior: none;
}
body {
  position: fixed;
  inset: 0;
  width: 100vw;
  height: 100vh;
  height: 100dvh;       /* dynamic vh accounts for mobile address bar */
  background:
    radial-gradient(ellipse at 20% 10%, #4f1e8a 0%, transparent 55%),
    radial-gradient(ellipse at 80% 90%, #a14fc4 0%, transparent 50%),
    linear-gradient(180deg, #0b0820 0%, #1c0e3f 60%, #2a1854 100%);
  background-attachment: fixed;
  font-family: "Trebuchet MS", "Segoe UI", system-ui, sans-serif;
  color: var(--cream);
  overflow: hidden;
  user-select: none;
  -webkit-user-select: none;
  -webkit-touch-callout: none;
  -webkit-tap-highlight-color: transparent;
  touch-action: none;        /* let the game handle every gesture */
  overscroll-behavior: none;
  /* respect notches on iPhone */
  padding: env(safe-area-inset-top) env(safe-area-inset-right) env(safe-area-inset-bottom) env(safe-area-inset-left);
  box-sizing: border-box;
}

/*
 * Fluid 16:9 scaling — game stays at internal 1280x720 resolution but the
 * displayed size fits any viewport. min() keeps it within both axes so it
 * never overflows portrait phones.
 */
#game-container {
  position: absolute;
  inset: 0;
  display: flex;
  align-items: center;
  justify-content: center;
  background: transparent;
  overflow: hidden;
}

/*
 * Shell = sized box that holds the HUD bar + the game stage. Maintains
 * aspect ratio 1280:776 (16:9 stage + 56px bar at native size) so the bar
 * proportions stay consistent across viewports.
 */
#game-shell {
  position: relative;
  display: flex;
  flex-direction: column;
  /* Desktop: maintain shell aspect 1280:760 (16:9 stage + 40px bar) so the
     game looks like a clean console. The aspect lock would letterbox phones
     though — the @media rule below overrides for narrow viewports. */
  width: min(100vw, calc(100dvh * 1280 / 760));
  height: min(100dvh, calc(100vw * 760 / 1280));
  max-width: 1280px;
  max-height: 760px;
  border-radius: clamp(0px, 1.5vmin, 22px);
  box-shadow: 0 30px 90px rgba(0,0,0,0.7), 0 0 0 4px rgba(255,217,122,0.25);
  overflow: hidden;
  background: #0b0820;
  /* Container query root so child media queries can target the shell's
     real width (the viewport width is misleading — the shell is letterbox-
     fit and often narrower than the viewport on landscape phones). */
  container-type: inline-size;
  container-name: shell;
}
/* Phone viewports: ditch the aspect lock and let the shell fill the safe
   area edge-to-edge. The canvas inside scales (slight anisotropic
   stretch on extreme aspects is unnoticeable on a procedural maze) and
   the purple side padding disappears. */
@media (max-width: 960px) {
  #game-shell {
    width: 100%;
    height: 100%;
    max-width: 100%;
    max-height: 100%;
    border-radius: 0;
    box-shadow: none;
  }
}
/*
 * Stage = the actual play area. Takes the remaining flex space below the
 * HUD bar. Internal canvas is fixed 1280x720; CSS scales it to fill.
 */
#game-stage {
  position: relative;
  flex: 1 1 auto;
  width: 100%;
  background: #0b0820;
  overflow: hidden;
  /* Soft inner border so the boundary between bar and play area reads */
  border-top: 1px solid rgba(255,217,122,0.18);
}

#game-canvas {
  display: block;
  width: 100%;
  height: 100%;
  background: linear-gradient(180deg, #1d0b3f 0%, #4b1d80 60%, #8a3da6 100%);
  image-rendering: -webkit-optimize-contrast;
  touch-action: none;
}

/* HUD bar — top strip, separated from the play area */
#hud {
  flex: 0 0 auto;
  width: 100%;
  min-height: 40px;
  padding: 4px 12px;
  display: flex;
  align-items: center;
  gap: 10px;
  background:
    linear-gradient(180deg, rgba(28,14,63,0.95) 0%, rgba(11,8,32,0.92) 100%);
  border-bottom: 2px solid rgba(255,217,122,0.35);
  box-shadow: 0 2px 14px rgba(0,0,0,0.45) inset;
  z-index: 12;
  flex-wrap: nowrap;
  overflow: hidden;
}
.hud-brand {
  display: none;
  font-weight: bold;
  letter-spacing: 0.5px;
  color: #ffd97a;
  font-size: 15px;
  white-space: nowrap;
  text-shadow: 0 1px 4px rgba(0,0,0,0.6);
}
#hud.hud-inactive .hud-brand { display: block; }
#hud.hud-inactive .hud-pills,
#hud.hud-inactive .hud-actions { display: none; }
.hud-pills {
  display: flex;
  align-items: center;
  gap: 8px;
  flex-wrap: nowrap;
  flex: 1 1 auto;
  min-width: 0;
  overflow: hidden;
}
.hud-actions {
  display: flex;
  align-items: center;
  gap: 8px;
  flex: 0 0 auto;
  margin-left: auto;
}
/* Mid-tier shells (narrow desktops / large tablets) — tighten padding
   so the full pill set still fits one row. */
@container shell (max-width: 1080px) {
  #hud { padding: 4px 10px; gap: 6px; }
  .hud-pills { gap: 5px; }
  .hud-actions { gap: 5px; }
  .hud-pill { padding: 3px 9px; font-size: 12px; max-width: 28ch;
              overflow: hidden; text-overflow: ellipsis; }
  .hud-btn { padding: 4px 10px; font-size: 12px; }
}
/* Bar tightens when the shell itself is narrow (covers landscape phones
   whose viewport is wider than the letterbox-fit shell). Uses container
   queries so we react to the actual bar width, not the viewport. */
@container shell (max-width: 760px) {
  #hud { min-height: 36px; padding: 3px 8px; gap: 6px; flex-wrap: nowrap; }
  .hud-brand { font-size: 12px; }
  .hud-pills { gap: 4px; flex-wrap: nowrap; overflow: hidden; }
  .hud-actions { gap: 4px; }
  .hud-pill { padding: 2px 8px; font-size: 11px; max-width: 22ch;
              white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
  .hud-btn { padding: 3px 9px; font-size: 11px; white-space: nowrap; }
  /* Hide non-critical pills so the bar fits one row. hud-title stays
     (ranks are part of the gameplay loop) but truncates with ellipsis. */
  #hud-player  { display: none !important; }
  /* Trim button labels to icons */
  #open-notebook-btn { font-size: 0; padding: 3px 9px; }
  #open-notebook-btn::before { content: '📒'; font-size: 13px; }
  #pause-btn        { font-size: 0; padding: 3px 9px; }
  #pause-btn::before        { content: '⏸'; font-size: 13px; }
  #mute-btn         { font-size: 0; padding: 3px 9px; }
  #mute-btn::before         { content: '🔊'; font-size: 13px; }
}
/* Very narrow shells (small phones): also shrink the world name */
@container shell (max-width: 580px) {
  #hud-world  { max-width: 14ch; }
  #hud-title  { max-width: 14ch; }
  #hud-streak { max-width: 12ch; }
}
.hud-pill {
  background: rgba(11,8,32,0.75);
  border: 2px solid rgba(255,217,122,0.45);
  color: var(--cream);
  padding: 3px 11px;
  border-radius: 999px;
  font-weight: bold;
  font-size: 13px;
  letter-spacing: 0.4px;
  backdrop-filter: blur(6px);
  text-shadow: 0 1px 2px rgba(0,0,0,0.6);
  white-space: nowrap;
  flex: 0 0 auto;
}
#hud-baton-active {
  background: linear-gradient(135deg, #ff8fc7, #ffd97a);
  color: #2b1242;
  border-color: #fff;
  animation: pulse 1.2s ease-in-out infinite;
}
.hud-btn {
  background: rgba(255,217,122,0.18);
  color: var(--cream);
  border: 2px solid rgba(255,217,122,0.5);
  padding: 4px 12px;
  border-radius: 10px;
  font-weight: bold;
  font-size: 13px;
  cursor: pointer;
  transition: all 0.15s;
}
.hud-btn:hover { background: rgba(255,217,122,0.32); transform: translateY(-1px); }

/* Overlays — `overflow-y: auto` lets tall panels scroll instead of getting
   top-clipped. `margin: auto` on direct children centers them when they fit
   in the viewport, and anchors them to the top-left when they overflow
   (auto margins resolve to 0 when negative space isn't available). This is
   the bulletproof scroll-or-center pattern. */
.overlay {
  position: absolute;
  inset: 0;
  background: rgba(7,4,20,0.78);
  backdrop-filter: blur(10px);
  display: flex;
  padding: max(16px, env(safe-area-inset-top)) 12px max(16px, env(safe-area-inset-bottom));
  overflow-y: auto;
  overscroll-behavior: contain;
  z-index: 20;
  animation: fadeIn 0.35s ease;
}
.overlay > * { margin: auto; }
/* Full-canvas cinematics opt out — they own the whole stage and shouldn't scroll */
#compose-screen.compose-cinema,
#world-transition-screen.world-transition-overlay {
  padding: 0;
  overflow: hidden;
}
#compose-screen.compose-cinema > *,
#world-transition-screen.world-transition-overlay > * { margin: 0; }
/* Levelup canvas backdrop is absolute-positioned; margin auto doesn't apply.
   Reset it explicitly so it spans the overlay. */
.levelup-bg { margin: 0 !important; }
.hidden { display: none !important; }

@keyframes fadeIn { from { opacity: 0 } to { opacity: 1 } }
@keyframes pulse { 0%,100% { transform: scale(1) } 50% { transform: scale(1.08) } }
@keyframes float { 0%,100% { transform: translateY(0) } 50% { transform: translateY(-8px) } }
@keyframes sparkle { 0%,100% { opacity: 0.2; transform: scale(0.7) } 50% { opacity: 1; transform: scale(1.1) } }
@keyframes spin { from { transform: rotate(0deg) } to { transform: rotate(360deg) } }

/* Start screen */
.title-block {
  text-align: center;
  max-width: 800px;
  padding: 30px;
}
.game-title {
  font-size: 64px;
  line-height: 1.05;
  font-weight: 900;
  letter-spacing: 1px;
  margin-bottom: 16px;
  filter: drop-shadow(0 6px 22px rgba(255,143,199,0.5));
}
.word { display: block; }
.w1 { color: #ffd97a; }
.w2 { color: #ff8fc7; }
.w3 { color: #a8e6ff; }
.tagline {
  font-size: 19px;
  margin: 14px 0 26px;
  color: #f3e6ff;
  font-style: italic;
}
.how-to {
  background: rgba(255,255,255,0.06);
  border: 2px solid rgba(255,217,122,0.35);
  border-radius: 14px;
  padding: 18px 24px;
  text-align: left;
  margin: 18px auto;
  max-width: 560px;
}
.how-row { padding: 5px 0; font-size: 16px; }
.how-row b { color: var(--gold); }
.audio-warn {
  margin-top: 16px;
  font-size: 13px;
  color: rgba(255,255,255,0.6);
}

/* Buttons */
.big-btn {
  background: linear-gradient(135deg, #ffd97a 0%, #ff8fc7 100%);
  color: var(--ink);
  border: none;
  padding: 14px 42px;
  border-radius: 16px;
  font-size: 18px;
  font-weight: 900;
  letter-spacing: 1px;
  cursor: pointer;
  box-shadow: 0 8px 22px rgba(255,143,199,0.45), inset 0 -3px 0 rgba(0,0,0,0.15);
  transition: transform 0.12s, box-shadow 0.12s;
}
.big-btn:hover {
  transform: translateY(-2px);
  box-shadow: 0 12px 28px rgba(255,143,199,0.6), inset 0 -3px 0 rgba(0,0,0,0.15);
}
.big-btn:active { transform: translateY(1px); }
.big-btn:disabled {
  opacity: 0.4;
  cursor: not-allowed;
  filter: grayscale(0.6);
}
.ghost-btn {
  background: transparent;
  color: var(--cream);
  border: 2px solid rgba(255,255,255,0.4);
  padding: 10px 28px;
  border-radius: 12px;
  font-weight: bold;
  cursor: pointer;
  transition: background 0.15s;
}
.ghost-btn:hover { background: rgba(255,255,255,0.1); }

/* Encounter */
.encounter-panel {
  display: flex;
  gap: 22px;
  background: linear-gradient(180deg, #2a1854, #1c0e3f);
  padding: 22px;
  border-radius: 22px;
  border: 3px solid rgba(255,217,122,0.5);
  box-shadow: 0 25px 70px rgba(0,0,0,0.7);
  max-width: 1100px;
}
.encounter-stage {
  background: radial-gradient(circle at center, #6b34a8 0%, #2a1854 70%);
  border-radius: 16px;
  border: 2px solid rgba(255,217,122,0.35);
  overflow: hidden;
}
#encounter-canvas {
  display: block;
}
.encounter-info {
  width: 340px;
  display: flex;
  flex-direction: column;
  gap: 12px;
  padding: 10px 6px;
}
.note-name {
  font-size: 36px;
  font-weight: 900;
  color: var(--gold);
}
.note-octave { font-size: 16px; color: #f3e6ff; }
.note-rarity {
  display: inline-block;
  align-self: flex-start;
  padding: 4px 12px;
  border-radius: 999px;
  font-size: 13px;
  font-weight: bold;
  letter-spacing: 1px;
  text-transform: uppercase;
  background: rgba(255,255,255,0.1);
}
.note-rarity.common  { background: #6f7d92; color: #fff; }
.note-rarity.uncommon{ background: #4caf76; color: #fff; }
.note-rarity.rare    { background: #4ca6e0; color: #fff; }
.note-rarity.epic    { background: #b46de2; color: #fff; }
.note-rarity.legendary { background: linear-gradient(90deg, #ffd97a, #ff8fc7); color: #2b1242; }

.resistance-row, .attempts-row {
  display: flex;
  align-items: center;
  gap: 10px;
  font-size: 14px;
}
.resistance-bar {
  flex: 1;
  height: 14px;
  background: rgba(0,0,0,0.4);
  border-radius: 7px;
  overflow: hidden;
  border: 1px solid rgba(255,255,255,0.2);
}
#resistance-fill {
  height: 100%;
  background: linear-gradient(90deg, #ffd97a, #ff8fc7);
  width: 100%;
  transition: width 0.4s ease;
}
#attempts-dots {
  display: flex;
  gap: 6px;
}
.attempt-dot {
  width: 18px; height: 22px;
  background: rgba(255,255,255,0.15);
  border: 2px solid rgba(255,217,122,0.5);
  border-radius: 4px;
}
.attempt-dot.used {
  background: rgba(255,80,120,0.5);
  border-color: rgba(255,120,160,0.8);
}
.capture-chance {
  background: rgba(0,0,0,0.3);
  padding: 8px 12px;
  border-radius: 10px;
  font-size: 15px;
}
.capture-chance b { color: var(--gold); }

.encounter-buttons {
  display: flex;
  flex-direction: column;
  gap: 10px;
  margin-top: 10px;
}

/* Notebook */
.notebook-panel {
  background: linear-gradient(180deg, #fff6e0, #ffe2bd);
  color: #2b1242;
  padding: 30px;
  border-radius: 22px;
  border: 4px solid #c08434;
  box-shadow: 0 25px 70px rgba(0,0,0,0.7), inset 0 0 0 2px #fff;
  max-width: 900px;
  width: 90%;
}
.notebook-panel h2 {
  font-size: 30px;
  margin-bottom: 16px;
  color: #6f3a14;
}
.notebook-grid {
  display: grid;
  grid-template-columns: repeat(var(--notebook-cols, 5), 1fr);
  gap: 10px;
  margin: 18px 0;
  /* Notebook scales up to 200 slots at L40 — scroll instead of pushing the
     panel off-screen. Keep actions + hint visible below the grid. */
  max-height: 52vh;
  overflow-y: auto;
  padding-right: 8px;
}
.notebook-grid::-webkit-scrollbar { width: 10px; }
.notebook-grid::-webkit-scrollbar-track { background: rgba(192,132,52,0.12); border-radius: 6px; }
.notebook-grid::-webkit-scrollbar-thumb { background: #c08434; border-radius: 6px; }
.notebook-slot {
  aspect-ratio: 1 / 1;
  background: #fffaf0;
  border: 2px dashed #c08434;
  border-radius: 10px;
  display: flex;
  align-items: center;
  justify-content: center;
  position: relative;
  font-size: 13px;
  color: #6f3a14;
}
.notebook-slot.filled {
  border-style: solid;
  background: #fff;
}
.notebook-slot canvas {
  width: 80%;
  height: 80%;
}
.notebook-slot .slot-label {
  position: absolute;
  bottom: 4px;
  right: 8px;
  font-size: 11px;
  color: #6f3a14;
  font-weight: bold;
}
.notebook-actions {
  display: flex;
  gap: 12px;
  margin-top: 12px;
}
.notebook-hint {
  margin-top: 10px;
  font-size: 13px;
  color: #6f3a14;
  text-align: center;
}
.notebook-panel .ghost-btn {
  color: #6f3a14;
  border-color: #c08434;
}

/* Level Up */
.levelup-panel {
  background: linear-gradient(135deg, #6b34a8, #ff8fc7);
  color: var(--cream);
  text-align: center;
  padding: 36px 50px;
  border-radius: 22px;
  border: 4px solid var(--gold);
  box-shadow: 0 25px 70px rgba(0,0,0,0.7);
}
.levelup-title {
  font-size: 48px;
  margin-bottom: 14px;
  color: var(--gold);
  text-shadow: 0 2px 10px rgba(0,0,0,0.4);
  animation: pulse 2s ease-in-out infinite;
  display: inline-block;
}
#levelup-text {
  font-size: 18px;
  margin-bottom: 18px;
}
#levelup-stars {
  font-size: 32px;
  margin: 16px 0 24px;
  letter-spacing: 6px;
}

/* Compose */
.compose-panel {
  background: linear-gradient(180deg, #2a1854, #4b1d80);
  padding: 30px 40px;
  border-radius: 22px;
  border: 3px solid var(--gold);
  text-align: center;
  min-width: 600px;
}
.compose-panel h2 {
  font-size: 30px;
  margin-bottom: 18px;
  color: var(--gold);
}
#compose-staff {
  display: flex;
  gap: 6px;
  justify-content: center;
  flex-wrap: wrap;
  margin: 20px 0;
}
.compose-note {
  width: 36px; height: 36px;
  border-radius: 50%;
  background: rgba(255,255,255,0.15);
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 12px;
  font-weight: bold;
  transition: all 0.3s;
}
.compose-note.active {
  background: linear-gradient(135deg, #ffd97a, #ff8fc7);
  color: #2b1242;
  transform: scale(1.3);
  box-shadow: 0 0 18px rgba(255,217,122,0.8);
}

/* Compose cinematic — canvas fills the overlay, no panel chrome */
#compose-screen.compose-cinema {
  padding: 0;
  background: rgba(7,4,20,0.96);
  align-items: stretch;
  justify-content: stretch;
}
#compose-canvas {
  display: block;
  width: 100%;
  height: 100%;
  image-rendering: -webkit-optimize-contrast;
}

/* Level-up animated background canvas — sits behind the panel */
.levelup-bg {
  position: absolute;
  inset: 0;
  width: 100%;
  height: 100%;
  pointer-events: none;
  object-fit: cover;
}
.compose-hint {
  font-size: 14px;
  color: rgba(255,255,255,0.7);
  font-style: italic;
}

/* Character Select */
.charsel-panel {
  background: linear-gradient(180deg, #2a1854, #1c0e3f);
  padding: 36px 44px;
  border-radius: 24px;
  border: 3px solid var(--gold);
  box-shadow: 0 30px 80px rgba(0,0,0,0.7);
  max-width: 920px;
  width: 92%;
  text-align: center;
}
.charsel-title {
  font-size: 36px;
  color: var(--gold);
  margin-bottom: 6px;
  text-shadow: 0 2px 10px rgba(0,0,0,0.4);
}
.charsel-sub {
  color: #f3e6ff;
  font-style: italic;
  margin-bottom: 24px;
}
.charsel-grid {
  display: grid;
  grid-template-columns: repeat(3, 1fr);
  gap: 18px;
}
.charsel-card {
  background: rgba(255,255,255,0.06);
  border: 3px solid rgba(255,217,122,0.3);
  border-radius: 18px;
  padding: 18px 14px;
  cursor: pointer;
  color: var(--cream);
  font-family: inherit;
  transition: all 0.18s;
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 6px;
}
.charsel-card:hover {
  transform: translateY(-4px);
  border-color: var(--gold);
  background: rgba(255,217,122,0.12);
  box-shadow: 0 12px 30px rgba(255,143,199,0.25);
}
.charsel-card:active { transform: translateY(0); }
.charsel-back-row {
  display: flex;
  justify-content: center;
  margin-top: 18px;
}
.charsel-portrait {
  width: 120px;
  height: 160px;
  background: radial-gradient(circle at center, #4a216f 0%, #1c0e3f 75%);
  border-radius: 12px;
  border: 2px solid rgba(255,217,122,0.35);
}
.charsel-name {
  font-size: 22px;
  font-weight: 900;
  color: var(--gold);
  margin-top: 6px;
  letter-spacing: 1px;
}
.charsel-vibe {
  font-size: 13px;
  color: #f3e6ff;
  font-style: italic;
}
.charsel-stats {
  list-style: none;
  padding: 0;
  margin: 6px 0 0;
  font-size: 13px;
  color: #d8c5ff;
  width: 100%;
}
.charsel-stats li {
  display: flex;
  justify-content: space-between;
  padding: 2px 8px;
  border-top: 1px solid rgba(255,255,255,0.08);
}
.charsel-stats li b { color: var(--gold); }

/* Hearts in HUD */
#hud-hearts {
  display: flex;
  gap: 4px;
  align-items: center;
  padding: 4px 12px;
  font-size: 18px;
}
#hud-hearts .heart {
  display: inline-block;
  text-shadow: 0 1px 2px rgba(0,0,0,0.6);
  transition: transform 0.2s;
}
#hud-hearts .heart.full { color: #ff5f8a; }
#hud-hearts .heart.empty { color: rgba(255,255,255,0.25); }
#hud-hearts.damaged { animation: shake 0.4s ease; }

@keyframes shake {
  0%, 100% { transform: translateX(0) }
  20% { transform: translateX(-4px) }
  40% { transform: translateX(4px) }
  60% { transform: translateX(-4px) }
  80% { transform: translateX(4px) }
}

/* Game Over */
.gameover-panel {
  background: linear-gradient(135deg, #2a0a1a, #6b1f3a);
  color: var(--cream);
  text-align: center;
  padding: 40px 60px;
  border-radius: 22px;
  border: 4px solid #ff5f8a;
  box-shadow: 0 25px 70px rgba(0,0,0,0.7);
  max-width: 520px;
}
.gameover-title {
  font-size: 42px;
  margin-bottom: 16px;
  color: #ff8fc7;
  text-shadow: 0 2px 10px rgba(0,0,0,0.4);
}
#gameover-text {
  font-size: 17px;
  margin-bottom: 14px;
  color: #f5d0e0;
}
.gameover-stats {
  background: rgba(0,0,0,0.3);
  padding: 12px 18px;
  border-radius: 10px;
  margin-bottom: 14px;
  font-size: 15px;
  color: #ffd97a;
}
.gameover-question {
  color: #f5d0e0;
  font-size: 15px;
  font-style: italic;
  margin-bottom: 14px;
}

/* Victory */
.victory-panel {
  background: linear-gradient(135deg, #ffd97a 0%, #ff8fc7 50%, #6b34a8 100%);
  color: #fff;
  text-align: center;
  padding: 44px 60px;
  border-radius: 26px;
  border: 4px solid #fff6e0;
  box-shadow: 0 30px 80px rgba(0,0,0,0.7), 0 0 60px rgba(255,217,122,0.5);
  max-width: 600px;
}
.victory-title {
  font-size: 52px;
  margin-bottom: 16px;
  color: #fff6e0;
  text-shadow: 0 3px 14px rgba(0,0,0,0.5);
  animation: pulse 2s ease-in-out infinite;
  display: inline-block;
}
.victory-stars {
  font-size: 38px;
  margin: 18px 0 24px;
  letter-spacing: 8px;
}

/* Character select name input */
.charsel-name-row {
  margin: 8px auto 18px;
  max-width: 480px;
  text-align: left;
}
.charsel-name-row label {
  display: block;
  font-size: 13px;
  color: #f3e6ff;
  margin-bottom: 6px;
  letter-spacing: 0.5px;
}
.charsel-name-row .optional {
  color: rgba(255,255,255,0.5);
  font-style: italic;
  font-weight: normal;
  margin-left: 4px;
}
.charsel-name-row input {
  width: 100%;
  background: rgba(255,255,255,0.08);
  border: 2px solid rgba(255,217,122,0.4);
  border-radius: 12px;
  padding: 10px 16px;
  color: var(--cream);
  font-family: inherit;
  font-size: 15px;
  letter-spacing: 0.5px;
  transition: border-color 0.15s;
  font-weight: 600;
}
.charsel-name-row input:focus {
  outline: none;
  border-color: var(--gold);
  background: rgba(255,217,122,0.1);
}

/* Start screen secondary actions */
.start-secondary-actions {
  display: flex;
  justify-content: center;
  gap: 10px;
  margin-top: 14px;
  flex-wrap: wrap;
}
.start-secondary-actions .ghost-btn { padding: 8px 18px; font-size: 13px; }

/* Pause panel */
.pause-panel {
  background: linear-gradient(180deg, #2a1854, #1c0e3f);
  padding: 36px 44px;
  border-radius: 22px;
  border: 3px solid var(--gold);
  text-align: center;
  max-width: 480px;
}
.pause-title {
  font-size: 38px;
  color: var(--gold);
  margin-bottom: 8px;
  letter-spacing: 2px;
}
.pause-sub {
  color: #f3e6ff;
  font-style: italic;
  margin-bottom: 18px;
}
.pause-stats {
  background: rgba(0,0,0,0.3);
  padding: 12px 16px;
  border-radius: 10px;
  margin-bottom: 20px;
  font-size: 14px;
  color: #ffd97a;
}
.pause-actions,
.endgame-actions,
.share-actions,
.leaderboard-actions {
  display: flex;
  flex-direction: column;
  gap: 10px;
  align-items: stretch;
}
.share-actions { flex-direction: column; }
.share-actions .big-btn,
.share-actions .ghost-btn,
.endgame-actions .big-btn,
.endgame-actions .ghost-btn,
.pause-actions .big-btn,
.pause-actions .ghost-btn {
  width: 100%;
}
.share-link {
  text-align: center;
  text-decoration: none;
}

/* Leaderboard */
.leaderboard-panel {
  background: linear-gradient(180deg, #2a1854, #1c0e3f);
  padding: 30px 36px;
  border-radius: 22px;
  border: 3px solid var(--gold);
  max-width: 720px;
  width: 92%;
  text-align: center;
}
.leaderboard-title {
  font-size: 34px;
  color: var(--gold);
  margin-bottom: 4px;
  letter-spacing: 1px;
}
.leaderboard-sub {
  color: #f3e6ff;
  font-style: italic;
  margin-bottom: 16px;
  font-size: 14px;
}
.leaderboard-table-wrap {
  max-height: 360px;
  overflow-y: auto;
  margin-bottom: 18px;
  border-radius: 10px;
  border: 1px solid rgba(255,217,122,0.2);
}
.leaderboard-table {
  width: 100%;
  border-collapse: collapse;
  color: var(--cream);
  font-size: 14px;
}
.leaderboard-table th,
.leaderboard-table td {
  padding: 8px 10px;
  text-align: left;
  border-bottom: 1px solid rgba(255,255,255,0.08);
}
.leaderboard-table th {
  background: rgba(255,217,122,0.1);
  color: var(--gold);
  font-size: 12px;
  letter-spacing: 1px;
  text-transform: uppercase;
  position: sticky;
  top: 0;
}
.leaderboard-table tbody tr:nth-child(1) { background: linear-gradient(90deg, rgba(255,217,122,0.18), transparent); }
.leaderboard-table tbody tr:nth-child(2) { background: linear-gradient(90deg, rgba(190,190,190,0.15), transparent); }
.leaderboard-table tbody tr:nth-child(3) { background: linear-gradient(90deg, rgba(205,127,50,0.18), transparent); }
.leaderboard-table .rank-medal { font-weight: 900; }
.leaderboard-table .end-victory { color: #ffd97a; }
.leaderboard-table .end-died { color: #ff7a9c; }
.leaderboard-empty {
  padding: 26px;
  color: rgba(255,255,255,0.6);
  font-style: italic;
}

/* Share */
.share-panel {
  background: linear-gradient(180deg, #2a1854, #1c0e3f);
  padding: 30px 36px;
  border-radius: 22px;
  border: 3px solid var(--gold);
  max-width: 480px;
  width: 92%;
  text-align: center;
}
.share-title {
  font-size: 30px;
  color: var(--gold);
  margin-bottom: 6px;
}
.share-sub {
  color: #f3e6ff;
  font-style: italic;
  margin-bottom: 14px;
  font-size: 14px;
}
.share-preview {
  background: rgba(0,0,0,0.3);
  border: 1px dashed rgba(255,217,122,0.4);
  border-radius: 10px;
  padding: 12px 16px;
  margin-bottom: 18px;
  font-size: 14px;
  color: #f3e6ff;
  text-align: left;
  white-space: pre-line;
}
.share-close {
  display: block;
  margin: 14px auto 0;
}

/* HUD player pill */
#hud-player {
  background: linear-gradient(135deg, rgba(255,217,122,0.25), rgba(255,143,199,0.15));
  border-color: rgba(255,217,122,0.6);
}

/* HUD title pill — shows the player's current composer title */
#hud-title {
  background: linear-gradient(135deg, rgba(255,143,199,0.35), rgba(176,107,243,0.18));
  border-color: rgba(255,143,199,0.65);
  color: #fff6e0;
}

/* HUD streak pill — escalates color as streak climbs (E4) */
#hud-streak {
  background: linear-gradient(135deg, rgba(255,217,122,0.35), rgba(255,143,199,0.18));
  border-color: rgba(255,217,122,0.65);
  color: #fff6e0;
  transition: all 0.2s;
}
#hud-streak.streak-hot {
  background: linear-gradient(135deg, rgba(255,143,60,0.55), rgba(255,80,80,0.30));
  border-color: rgba(255,143,60,0.85);
  box-shadow: 0 0 12px rgba(255,143,60,0.45);
}
/* Compose skip button (E1) */
.compose-skip-btn {
  position: absolute;
  top: 16px;
  right: 16px;
  background: rgba(11, 8, 32, 0.78);
  color: #ffd97a;
  border: 2px solid rgba(255,217,122,0.55);
  border-radius: 999px;
  padding: 8px 18px;
  font-weight: bold;
  cursor: pointer;
  z-index: 25;
  transition: all 0.15s;
}
.compose-skip-btn:hover {
  background: rgba(255,217,122,0.25);
  transform: translateY(-1px);
}

/* HUD minimap removed — actual maze sits right below the bar */

/* World-clear run summary (E23) */
.world-summary {
  margin-top: 22px;
  display: grid;
  grid-template-columns: repeat(2, 1fr);
  gap: 8px 24px;
  font-size: 14px;
  color: #fff6e0;
  background: rgba(11,8,32,0.55);
  border: 1px solid rgba(255,217,122,0.35);
  border-radius: 12px;
  padding: 14px 22px;
}
.world-summary b { color: #ffd97a; }

/* Collection album (E11) */
.collection-tally {
  display: flex;
  flex-wrap: wrap;
  gap: 6px;
  margin: 4px 0 10px;
}
.rarity-tag {
  font-size: 12px;
  padding: 3px 10px;
  border-radius: 999px;
  background: rgba(11,8,32,0.65);
  border: 1px solid rgba(255,217,122,0.4);
}
.rarity-common    { color: #c9c0a7; border-color: rgba(255,255,255,0.3); }
.rarity-uncommon  { color: #b1eda8; border-color: rgba(127,220,138,0.6); }
.rarity-rare      { color: #7cd9ff; border-color: rgba(93,190,240,0.65); }
.rarity-epic      { color: #c89dff; border-color: rgba(176,107,243,0.7); }
.rarity-legendary { color: #ffd97a; border-color: rgba(255,217,122,0.85); }
.collection-grid {
  display: grid;
  grid-template-columns: repeat(12, 1fr);
  gap: 3px;
  margin: 6px 0;
}
.collection-cell {
  aspect-ratio: 1;
  min-width: 18px;
  display: flex;
  align-items: center;
  justify-content: center;
  border-radius: 4px;
  background: rgba(11,8,32,0.55);
  border: 1px solid rgba(255,217,122,0.18);
  color: rgba(255, 246, 224, 0.55);
  font-size: 9px;
  font-weight: bold;
}
.collection-cell.coll-tier-1 { background: rgba(127,220,138,0.32); color: #c1f5cc; }
.collection-cell.coll-tier-2 { background: rgba(93,190,240,0.40);  color: #d8eeff; }
.collection-cell.coll-tier-3 { background: rgba(176,107,243,0.45); color: #ecdfff; }
.collection-cell.coll-tier-4 { background: rgba(255,143,199,0.55); color: #ffe9f4; }
.collection-cell.coll-tier-5 {
  background: linear-gradient(135deg, #ffd97a, #ff8fc7);
  color: #1c0e3f;
  box-shadow: 0 0 4px rgba(255,217,122,0.85);
}

/* Daily missions (E12) */
.daily-mission-list {
  list-style: none;
  padding: 0;
  margin: 4px 0 10px;
}
.daily-mission-list .mission {
  background: rgba(11,8,32,0.6);
  border-left: 3px solid rgba(255,217,122,0.55);
  border-radius: 8px;
  padding: 8px 12px;
  margin-bottom: 6px;
  display: grid;
  grid-template-columns: 1fr auto;
  grid-template-rows: auto auto;
  gap: 4px 12px;
}
.daily-mission-list .mission.done {
  border-left-color: #7fdc8a;
  background: rgba(127,220,138,0.22);
}
.daily-mission-list .mission-label { font-size: 13px; }
.daily-mission-list .mission-progress {
  font-size: 12px;
  color: #ffd97a;
  font-weight: bold;
  text-align: right;
}
.daily-mission-list .mission.done .mission-progress { color: #c1f5cc; }
.daily-mission-list .mission-bar {
  grid-column: 1 / -1;
  height: 4px;
  background: rgba(255,217,122,0.18);
  border-radius: 2px;
  overflow: hidden;
}
.daily-mission-list .mission-bar-fill {
  height: 100%;
  background: linear-gradient(90deg, #ffd97a, #ff8fc7);
  transition: width 0.4s;
}
.ach-sub { font-size: 11px; opacity: 0.7; font-weight: normal; margin-left: 4px; }

/* Locked-note Simon-says targets (E8) */
.locked-targets {
  display: flex;
  gap: 8px;
  justify-content: center;
  margin: 8px 0 12px;
  flex-wrap: wrap;
}
.locked-target {
  min-width: 44px;
  min-height: 44px;
  padding: 10px 12px;
  border-radius: 12px;
  border: 2px solid rgba(255,217,122,0.55);
  background: rgba(11,8,32,0.6);
  color: #fff6e0;
  font-weight: bold;
  font-size: 16px;
  cursor: pointer;
  transition: all 0.15s;
}
.locked-target:hover { background: rgba(107,52,168,0.55); transform: translateY(-1px); }
.locked-target.active {
  background: linear-gradient(135deg, #ffd97a, #ff8fc7);
  color: #1c0e3f;
  box-shadow: 0 0 18px rgba(255,217,122,0.85);
  transform: scale(1.1);
}
.locked-target.done {
  border-color: rgba(127,220,138,0.85);
  background: rgba(127,220,138,0.25);
  color: #c1f5cc;
}

#hud-streak.streak-fire {
  background: linear-gradient(135deg, rgba(255,80,80,0.65), rgba(176,30,30,0.55));
  border-color: #fff;
  color: #fff;
  box-shadow: 0 0 18px rgba(255,80,120,0.7);
  animation: streakPulse 0.8s ease-in-out infinite alternate;
}
@keyframes streakPulse {
  0%   { transform: scale(1.0); filter: brightness(1.0); }
  100% { transform: scale(1.06); filter: brightness(1.18); }
}

/* ===== World transition cinematic ===== */
.world-transition-overlay {
  background:
    radial-gradient(circle at 50% 35%, rgba(255, 217, 122, 0.18) 0%, transparent 50%),
    linear-gradient(180deg, rgba(11,8,32,0.92) 0%, rgba(28, 14, 63, 0.96) 60%, rgba(7, 4, 20, 0.98) 100%);
  backdrop-filter: blur(12px);
}
.world-transition-panel {
  text-align: center;
  padding: 40px;
  animation: worldFade 0.6s ease-out both;
}
.world-transition-from {
  font-size: 22px;
  font-weight: bold;
  color: rgba(255, 217, 122, 0.85);
  margin-bottom: 18px;
  letter-spacing: 1px;
  text-shadow: 0 2px 6px rgba(0,0,0,0.6);
  animation: worldFadeIn 0.6s 0.1s ease-out both;
}
.world-transition-to {
  font-size: 64px;
  font-weight: 900;
  letter-spacing: 1px;
  background: linear-gradient(135deg, #ffd97a 0%, #ff8fc7 40%, #a8e6ff 100%);
  -webkit-background-clip: text;
  background-clip: text;
  color: transparent;
  filter: drop-shadow(0 8px 24px rgba(255, 143, 199, 0.6));
  margin-bottom: 14px;
  animation: worldFadeIn 0.8s 0.4s ease-out both;
}
.world-transition-tagline {
  font-size: 19px;
  color: #f3e6ff;
  font-style: italic;
  margin-bottom: 28px;
  animation: worldFadeIn 0.8s 0.8s ease-out both;
}
.world-transition-spinner {
  position: relative;
  width: 120px;
  height: 120px;
  margin: 0 auto;
  animation: worldFadeIn 0.8s 1.0s ease-out both;
}
.world-transition-spinner .ring {
  position: absolute;
  inset: 0;
  border: 3px solid transparent;
  border-top-color: #ffd97a;
  border-radius: 50%;
  animation: worldSpin 1.6s linear infinite;
}
.world-transition-spinner .ring.r2 {
  inset: 12px;
  border-top-color: #ff8fc7;
  animation-duration: 1.2s;
  animation-direction: reverse;
}
.world-transition-spinner .ring.r3 {
  inset: 24px;
  border-top-color: #a8e6ff;
  animation-duration: 0.9s;
}
@keyframes worldFade { from { opacity: 0; transform: scale(0.96); } to { opacity: 1; transform: scale(1); } }
@keyframes worldFadeIn { from { opacity: 0; transform: translateY(14px); } to { opacity: 1; transform: translateY(0); } }
@keyframes worldSpin { to { transform: rotate(360deg); } }

/* HUD world pill — themed to the current world */
#hud-world {
  background: linear-gradient(135deg, rgba(168,230,255,0.30), rgba(107,52,168,0.18));
  border-color: rgba(168,230,255,0.6);
  color: #fff6e0;
}

/* Shared-song banner on the start screen */
.shared-song-banner {
  margin: 16px auto 0;
  padding: 12px 18px;
  background: linear-gradient(135deg, rgba(255,217,122,0.18), rgba(255,143,199,0.18));
  border: 2px solid rgba(255,217,122,0.5);
  border-radius: 14px;
  display: flex;
  align-items: center;
  gap: 14px;
  justify-content: center;
  flex-wrap: wrap;
  font-size: 15px;
  color: var(--cream);
}
.shared-song-banner p { margin: 0; }

/* Settings panel */
.settings-panel {
  background: linear-gradient(180deg, #2a1854 0%, #1c0e3f 100%);
  color: var(--cream);
  padding: 28px 36px;
  border-radius: 20px;
  border: 3px solid var(--gold);
  max-width: 480px;
  width: 90%;
  box-shadow: 0 25px 70px rgba(0,0,0,0.7);
}
.settings-title { color: var(--gold); font-size: 28px; margin-bottom: 4px; text-align: center; }
.settings-sub   { font-size: 13px; color: rgba(255,255,255,0.65); text-align: center; margin-bottom: 18px; }
.settings-row   { margin: 14px 0; }
.settings-row label {
  display: flex;
  justify-content: space-between;
  align-items: center;
  font-size: 14px;
  margin-bottom: 6px;
}
.settings-val { color: var(--gold); font-weight: bold; }
.settings-row input[type="range"] {
  width: 100%;
  appearance: none;
  -webkit-appearance: none;
  height: 8px;
  background: rgba(255,255,255,0.12);
  border-radius: 4px;
  outline: none;
}
.settings-row input[type="range"]::-webkit-slider-thumb {
  -webkit-appearance: none;
  width: 22px;
  height: 22px;
  background: linear-gradient(135deg, #ffd97a, #ff8fc7);
  border-radius: 50%;
  border: 2px solid #fff;
  cursor: pointer;
  box-shadow: 0 4px 12px rgba(255,143,199,0.45);
}
.settings-row input[type="range"]::-moz-range-thumb {
  width: 22px;
  height: 22px;
  background: linear-gradient(135deg, #ffd97a, #ff8fc7);
  border-radius: 50%;
  border: 2px solid #fff;
  cursor: pointer;
}
.settings-toggle-row { padding: 10px 0; }
.settings-toggle {
  display: flex;
  align-items: center;
  gap: 12px;
  cursor: pointer;
}
.settings-toggle input[type="checkbox"] {
  width: 20px;
  height: 20px;
  accent-color: var(--pink);
  cursor: pointer;
}
.settings-toggle small { color: rgba(255,255,255,0.6); font-weight: normal; }
.settings-actions {
  display: flex;
  gap: 12px;
  justify-content: center;
  margin-top: 18px;
  flex-wrap: wrap;
}

/* Leaderboard tabs */
.leaderboard-tabs {
  display: flex;
  justify-content: center;
  gap: 8px;
  margin: 12px 0 8px;
  flex-wrap: wrap;
}
.leaderboard-tab {
  background: rgba(255,255,255,0.08);
  color: var(--cream);
  border: 2px solid rgba(255,217,122,0.3);
  padding: 6px 18px;
  border-radius: 999px;
  font-weight: bold;
  font-size: 13px;
  cursor: pointer;
  transition: all 0.15s;
}
.leaderboard-tab:hover { background: rgba(255,217,122,0.18); }
.leaderboard-tab.active {
  background: linear-gradient(135deg, #ffd97a, #ff8fc7);
  color: #2b1242;
  border-color: #fff;
  box-shadow: 0 4px 14px rgba(255,143,199,0.35);
}
.leaderboard-loading {
  text-align: center;
  font-style: italic;
  color: rgba(255,255,255,0.65);
  padding: 18px;
}

/* Daily challenge ribbon on the start screen (purely cosmetic) */
.daily-ribbon {
  display: inline-block;
  padding: 4px 12px;
  background: linear-gradient(135deg, #5dbef0, #b675ff);
  color: #fff;
  font-size: 11px;
  font-weight: bold;
  letter-spacing: 1px;
  border-radius: 999px;
  vertical-align: middle;
  margin-left: 4px;
}

/* ===== Trophy popup ===== */
.trophy-overlay {
  background: radial-gradient(ellipse at center, rgba(107,52,168,0.6) 0%, rgba(7,4,20,0.92) 70%);
  backdrop-filter: blur(8px);
}
.trophy-panel {
  background: linear-gradient(135deg, #fff6e0 0%, #ffd97a 50%, #ff8fc7 100%);
  color: #2b1242;
  padding: 32px 48px;
  border-radius: 24px;
  border: 5px solid #fff;
  box-shadow:
    0 0 0 4px #ffd97a,
    0 25px 80px rgba(255, 143, 199, 0.6),
    inset 0 0 40px rgba(255, 255, 255, 0.4);
  text-align: center;
  max-width: 520px;
  animation: trophyPop 0.6s cubic-bezier(0.18, 1.2, 0.4, 1) both;
}
.trophy-emoji {
  font-size: 96px;
  line-height: 1;
  margin-bottom: 12px;
  filter: drop-shadow(0 6px 12px rgba(0,0,0,0.35));
  animation: trophyBounce 1.6s ease-in-out infinite;
}
.trophy-title {
  font-size: 38px;
  font-weight: 900;
  color: #6b34a8;
  letter-spacing: 0.5px;
  margin-bottom: 6px;
  text-shadow: 0 2px 0 #fff;
}
.trophy-flavor {
  font-size: 17px;
  margin-bottom: 22px;
  color: #2b1242;
}
.trophy-actions {
  display: flex;
  gap: 12px;
  justify-content: center;
  flex-wrap: wrap;
}
.trophy-panel .ghost-btn { color: #6b34a8; border-color: #6b34a8; }
@keyframes trophyPop {
  0%   { transform: scale(0.4) rotate(-8deg); opacity: 0; }
  60%  { transform: scale(1.08) rotate(2deg); opacity: 1; }
  100% { transform: scale(1) rotate(0); opacity: 1; }
}
@keyframes trophyBounce {
  0%,100% { transform: translateY(0) rotate(0deg); }
  50%     { transform: translateY(-10px) rotate(-4deg); }
}

/* ===== Achievements (Hall of Composers) panel ===== */
.achievements-panel {
  background: linear-gradient(180deg, #2a1854 0%, #1c0e3f 100%);
  color: var(--cream);
  padding: 28px 36px;
  border-radius: 22px;
  border: 3px solid var(--gold);
  box-shadow: 0 25px 70px rgba(0,0,0,0.7), inset 0 0 0 2px rgba(255,255,255,0.05);
  max-width: 760px;
  width: 92%;
  max-height: 88vh;
  overflow-y: auto;
}
.achievements-title {
  font-size: 34px;
  color: var(--gold);
  text-align: center;
  margin-bottom: 4px;
}
.achievements-sub {
  text-align: center;
  font-size: 14px;
  color: rgba(255,255,255,0.7);
  margin-bottom: 18px;
}
.achievements-section { margin: 18px 0; }
.achievements-section h3 {
  font-size: 17px;
  color: var(--pink);
  margin-bottom: 10px;
  letter-spacing: 1px;
}
.achievements-current-title {
  display: inline-block;
  padding: 8px 22px;
  background: linear-gradient(135deg, #ffd97a, #ff8fc7);
  color: #2b1242;
  border-radius: 999px;
  font-size: 22px;
  font-weight: 900;
  box-shadow: 0 6px 20px rgba(255,143,199,0.45);
}
.trophy-grid {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(140px, 1fr));
  gap: 12px;
}
.trophy-card {
  background: rgba(255,255,255,0.08);
  border: 2px solid rgba(255,217,122,0.4);
  border-radius: 14px;
  padding: 14px 8px;
  text-align: center;
  transition: transform 0.15s;
  position: relative;
}
.trophy-card.earned {
  background: linear-gradient(135deg, rgba(255,217,122,0.35), rgba(255,143,199,0.25));
  border-color: var(--gold);
  box-shadow: 0 6px 18px rgba(255,217,122,0.35);
}
.trophy-card.locked { opacity: 0.45; filter: grayscale(0.8); }
.trophy-card-emoji { font-size: 42px; line-height: 1; margin-bottom: 6px; }
.trophy-card-label { font-size: 13px; font-weight: bold; color: var(--cream); }
.trophy-card-hint  { font-size: 11px; color: rgba(255,255,255,0.6); margin-top: 4px; }
.title-list {
  list-style: none;
  padding: 0;
  display: flex;
  flex-direction: column;
  gap: 6px;
}
.title-list li {
  padding: 8px 14px;
  background: rgba(255,255,255,0.05);
  border-radius: 10px;
  border-left: 4px solid rgba(255,217,122,0.4);
  font-size: 14px;
  display: flex;
  justify-content: space-between;
  align-items: center;
}
.title-list li.earned {
  background: rgba(255,217,122,0.15);
  border-left-color: var(--gold);
  color: var(--cream);
}
.title-list li.current {
  background: linear-gradient(90deg, rgba(255,217,122,0.45), rgba(255,143,199,0.18));
  border-left-color: var(--pink);
  font-weight: bold;
}
.title-list-min { color: rgba(255,255,255,0.55); font-size: 12px; }
.achievements-actions {
  display: flex;
  gap: 12px;
  justify-content: center;
  margin-top: 18px;
  flex-wrap: wrap;
}

/* Level-up title pill (shown inside the levelup panel) */
.levelup-title-pill {
  display: inline-block;
  padding: 6px 18px;
  background: rgba(255,255,255,0.18);
  border: 2px solid var(--gold);
  border-radius: 999px;
  font-size: 15px;
  font-weight: bold;
  color: var(--gold);
  margin: 10px 0 6px;
}
.levelup-actions {
  display: flex;
  gap: 12px;
  justify-content: center;
  flex-wrap: wrap;
  margin-top: 4px;
}

/* On-screen joystick (touch devices only) */
#touch-joystick {
  position: absolute;
  bottom: max(16px, env(safe-area-inset-bottom));
  left: max(16px, env(safe-area-inset-left));
  width: 140px;
  height: 140px;
  display: none;
  align-items: center;
  justify-content: center;
  z-index: 12;
  touch-action: none;
}
#touch-joystick-base {
  position: relative;
  width: 130px;
  height: 130px;
  border-radius: 50%;
  background: rgba(11,8,32,0.55);
  border: 2px solid rgba(255,217,122,0.5);
  backdrop-filter: blur(6px);
}
#touch-joystick-knob {
  position: absolute;
  left: 50%;
  top: 50%;
  width: 60px;
  height: 60px;
  margin-left: -30px;
  margin-top: -30px;
  border-radius: 50%;
  background: linear-gradient(135deg, #ffd97a, #ff8fc7);
  border: 2px solid rgba(255,255,255,0.7);
  box-shadow: 0 4px 14px rgba(0,0,0,0.4);
  transition: transform 0.05s linear;
  pointer-events: none;
}

/* Mobile capture button */
#touch-capture-btn {
  position: absolute;
  bottom: max(28px, env(safe-area-inset-bottom));
  right: max(28px, env(safe-area-inset-right));
  width: 92px;
  height: 92px;
  border-radius: 50%;
  background: linear-gradient(135deg, #ffd97a, #ff8fc7);
  border: 3px solid rgba(255,255,255,0.85);
  box-shadow: 0 10px 28px rgba(255,143,199,0.5);
  font-size: 38px;
  cursor: pointer;
  display: none;
  align-items: center;
  justify-content: center;
  z-index: 12;
  touch-action: manipulation;
  transition: transform 0.1s;
  -webkit-tap-highlight-color: transparent;
}
#touch-capture-btn:active { transform: scale(0.92); }
@media (hover: none) and (pointer: coarse) {
  #touch-capture-btn { display: flex; }
}

/* Toast */
#toast {
  position: absolute;
  top: 80px;
  left: 50%;
  transform: translateX(-50%);
  background: rgba(11,8,32,0.92);
  border: 2px solid var(--gold);
  color: var(--cream);
  padding: 12px 26px;
  border-radius: 999px;
  font-weight: bold;
  z-index: 50;
  animation: fadeIn 0.25s ease;
  pointer-events: none;
  text-align: center;
}

/* Rotate-to-landscape prompt — only shown on small portrait screens */
#rotate-prompt {
  position: fixed;
  inset: 0;
  z-index: 1000;
  background:
    radial-gradient(ellipse at 50% 30%, #6b34a8 0%, transparent 60%),
    linear-gradient(180deg, #1c0e3f, #0b0820);
  color: var(--cream);
  display: none;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  text-align: center;
  padding: 32px;
}
#rotate-prompt h2 {
  font-size: 26px;
  color: var(--gold);
  margin-bottom: 8px;
}
#rotate-prompt p {
  font-size: 16px;
  max-width: 320px;
  line-height: 1.4;
  margin-bottom: 6px;
}
#rotate-prompt .rotate-hint {
  font-size: 13px;
  color: rgba(255,255,255,0.55);
  font-style: italic;
  margin-top: 14px;
}
#rotate-prompt .rotate-icon {
  font-size: 80px;
  margin-bottom: 12px;
  display: inline-block;
  animation: rotateIcon 2.4s ease-in-out infinite;
}
@keyframes rotateIcon {
  0%, 40% { transform: rotate(0); }
  60%, 100% { transform: rotate(90deg); }
}

/* Activate the prompt on small portrait viewports (typical phone). */
@media (orientation: portrait) and (max-width: 720px) {
  #rotate-prompt { display: flex; }
  #game-container { display: none; }
}

/* Inputs at >=16px font-size to prevent iOS auto-zoom on focus */
input, button, select, textarea { font-size: 16px; }

/* ==========================================
 * Mobile / small-viewport rules
 * Tier 1: <= 920px (small tablets, big phones in landscape)
 * Tier 2: <= 720px (most phones in landscape)
 * Tier 3: <= 500px (tiny phones in landscape, edge cases)
 * ========================================== */
@media (max-width: 920px) {
  .hud-pill { padding: 4px 10px; font-size: 12px; border-width: 1.5px; }
  #hud-hearts { padding: 3px 9px; gap: 3px; font-size: 15px; }
  .hud-btn { padding: 4px 10px; font-size: 12px; border-width: 1.5px; }
  /* legacy bar tuning kept only for very-old-browser fallbacks; the
     container queries above are the real source of truth */
  /* hide the big "BATON ACTIVE" text on phones — heart pulse is enough */
  #hud-baton-active { font-size: 10px; padding: 3px 8px; }

  .encounter-panel {
    flex-direction: column;
    padding: 8px;
    gap: 8px;
    width: 96%;
    max-width: 96%;
    height: 92vh;
    max-height: 92vh;
    overflow: hidden;
  }
  /* Canvas stage takes the most space and keeps 16:9 */
  .encounter-stage {
    width: 100%;
    aspect-ratio: 16 / 9;
    max-height: 50vh;
    flex: 0 0 auto;
    display: flex;
    align-items: center;
    justify-content: center;
  }
  #encounter-canvas {
    width: 100% !important;
    height: 100% !important;
    display: block;
  }
  .encounter-info {
    width: 100%;
    padding: 0;
    gap: 6px;
    flex: 1 1 auto;
    overflow-y: auto;
    min-height: 0;
  }
  .note-name { font-size: 22px; }
  .note-octave { font-size: 12px; }
  .note-rarity { font-size: 11px; padding: 3px 10px; }
  .resistance-row, .attempts-row { font-size: 12px; gap: 8px; }
  .capture-chance { padding: 6px 10px; font-size: 13px; }
  .encounter-buttons { gap: 8px; margin-top: 6px; }
  .encounter-buttons .big-btn { padding: 12px 20px; font-size: 15px; }
  .encounter-buttons .ghost-btn { padding: 9px 16px; font-size: 13px; }
  .attempt-dot { width: 14px; height: 18px; }

  .notebook-panel { padding: 16px; max-height: 92vh; overflow-y: auto; }
  .notebook-grid { grid-template-columns: repeat(5, 1fr); gap: 6px; }
  .notebook-slot .slot-label { font-size: 9px; }

  .charsel-panel { padding: 14px 12px; max-width: 96%; max-height: 92vh; overflow-y: auto; }
  .charsel-title { font-size: 24px; }
  .charsel-sub { font-size: 13px; margin-bottom: 12px; }
  .charsel-grid { grid-template-columns: 1fr; gap: 8px; }
  .charsel-card {
    padding: 8px 12px;
    flex-direction: row;
    gap: 12px;
    align-items: center;
    text-align: left;
  }
  .charsel-portrait { width: 70px; height: 96px; flex-shrink: 0; }
  .charsel-name { margin-top: 0; font-size: 17px; }
  .charsel-vibe { font-size: 11px; }
  .charsel-stats { font-size: 11px; flex-grow: 1; }
  .charsel-name-row { margin: 4px auto 12px; }
  .charsel-name-row label { font-size: 12px; }

  .title-block { padding: 18px; }
  .game-title { font-size: 36px; line-height: 1.05; margin-bottom: 8px; }
  .tagline { font-size: 13px; margin: 8px 0 14px; }
  .how-to { padding: 10px 14px; max-width: 95%; margin: 10px auto; }
  .how-row { font-size: 12px; padding: 2px 0; }
  .big-btn { padding: 11px 26px; font-size: 15px; }
  .ghost-btn { padding: 8px 18px; font-size: 13px; }
  .audio-warn { font-size: 11px; margin-top: 10px; }

  .levelup-panel, .gameover-panel, .victory-panel, .compose-panel,
  .pause-panel, .leaderboard-panel, .share-panel {
    padding: 18px 16px;
    max-width: 94%;
    max-height: 92vh;
    overflow-y: auto;
    min-width: 0;
  }
  .levelup-title, .victory-title { font-size: 28px; }
  .gameover-title { font-size: 26px; }
  .pause-title, .share-title, .leaderboard-title { font-size: 24px; }
  #compose-staff { gap: 4px; }
  .compose-note { width: 26px; height: 26px; font-size: 10px; }

  .leaderboard-table-wrap { max-height: 50vh; }
  .leaderboard-table th, .leaderboard-table td { padding: 5px 6px; font-size: 12px; }

  .endgame-actions, .pause-actions, .share-actions, .leaderboard-actions { gap: 8px; }

  /* Touch joystick scales down on very small visible game stages */
  #touch-joystick { width: 110px; height: 110px; }
  #touch-joystick-base { width: 100px; height: 100px; }
  #touch-joystick-knob { width: 50px; height: 50px; margin-left: -25px; margin-top: -25px; }
  #touch-capture-btn { width: 76px; height: 76px; font-size: 30px; }
}

@media (max-width: 500px) {
  .hud-pill { padding: 3px 7px; font-size: 10px; }
  #hud-hearts { font-size: 13px; gap: 2px; padding: 2px 6px; }
  .hud-btn { padding: 3px 8px; font-size: 10px; }

  .game-title { font-size: 28px; }
  .how-to { padding: 8px 10px; }
  .how-row { font-size: 11px; }

  .charsel-portrait { width: 60px; height: 80px; }
  .charsel-name { font-size: 15px; }
  .charsel-stats { font-size: 10px; }

  #touch-joystick { width: 96px; height: 96px; bottom: 12px; left: 12px; }
  #touch-joystick-base { width: 86px; height: 86px; }
  #touch-joystick-knob { width: 42px; height: 42px; margin-left: -21px; margin-top: -21px; }
  #touch-capture-btn { width: 64px; height: 64px; font-size: 26px; bottom: 16px; right: 16px; }
}

/* Force-show on-screen joystick on touch devices regardless of width */
@media (hover: none) and (pointer: coarse) {
  #touch-joystick { display: flex !important; }
}
