/* Reset & Base */
*, *::before, *::after {
  box-sizing: border-box;
  margin: 0;
  padding: 0;
}

:root {
  --bg: #f8f9fa;
  --surface: #ffffff;
  --text: #1a1a2e;
  --text-secondary: #6c757d;
  --primary: #4361ee;
  --primary-hover: #3a56d4;
  --correct: #2ecc71;
  --correct-bg: #d4edda;
  --wrong: #e74c3c;
  --wrong-bg: #f8d7da;
  --border: #dee2e6;
  --shadow: 0 2px 8px rgba(0, 0, 0, 0.08);
  --radius: 8px;
}

@media (prefers-color-scheme: dark) {
  :root {
    --bg: #1a1a2e;
    --surface: #16213e;
    --text: #e8e8e8;
    --text-secondary: #a0a0b0;
    --primary: #4895ef;
    --primary-hover: #4361ee;
    --correct: #2ecc71;
    --correct-bg: #1a3a2a;
    --wrong: #e74c3c;
    --wrong-bg: #3a1a1a;
    --border: #2a2a4a;
    --shadow: 0 2px 8px rgba(0, 0, 0, 0.3);
  }
}

body {
  font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, sans-serif;
  background: var(--bg);
  color: var(--text);
  min-height: 100vh;
  display: flex;
  flex-direction: column;
  align-items: center;
}

/* Header */
header {
  text-align: center;
  padding: 24px 16px 8px;
}

header h1 {
  font-size: 1.6rem;
  font-weight: 700;
  letter-spacing: -0.02em;
}

header p {
  color: var(--text-secondary);
  font-size: 0.9rem;
  margin-top: 4px;
}

/* Navigation tabs */
.nav-tabs {
  display: flex;
  gap: 8px;
  justify-content: center;
  margin-top: 12px;
}

.nav-tab {
  padding: 6px 16px;
  border-radius: var(--radius);
  background: var(--surface);
  color: var(--text);
  text-decoration: none;
  font-size: 0.85rem;
  font-weight: 600;
  border: 2px solid var(--border);
  transition: all 0.15s ease;
}

.nav-tab:hover {
  border-color: var(--primary);
  background: var(--primary);
  color: white;
}

.nav-tab.active {
  background: var(--primary);
  color: white;
  border-color: var(--primary);
}

/* Main container */
.container {
  width: 100%;
  max-width: 600px;
  padding: 0 16px;
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 16px;
}

/* Controls row */
.controls {
  display: flex;
  flex-wrap: wrap;
  gap: 8px;
  justify-content: center;
  align-items: center;
}

/* Clef selector buttons */
.clef-btn {
  padding: 8px 16px;
  border: 2px solid var(--border);
  border-radius: var(--radius);
  background: var(--surface);
  color: var(--text);
  font-size: 0.85rem;
  font-weight: 600;
  cursor: pointer;
  transition: all 0.15s ease;
}

.clef-btn:hover {
  border-color: var(--primary);
  color: var(--primary);
}

.clef-btn.active {
  background: var(--primary);
  color: #fff;
  border-color: var(--primary);
}

/* Instrument mode buttons — same shape as clef buttons, slightly smaller */
.instrument-btn {
  padding: 6px 14px;
  border: 2px solid var(--border);
  border-radius: var(--radius);
  background: var(--surface);
  color: var(--text);
  font-size: 0.82rem;
  font-weight: 600;
  cursor: pointer;
  transition: all 0.15s ease;
}
.instrument-btn:hover  { border-color: var(--primary); color: var(--primary); }
.instrument-btn.active { background: var(--primary); color: #fff; border-color: var(--primary); }

/* Staff display */
.staff-container {
  background: var(--surface);
  border-radius: var(--radius);
  box-shadow: var(--shadow);
  padding: 20px 16px;
  width: 100%;
  display: flex;
  justify-content: center;
  align-items: center;
  min-height: 180px;
}

#staff {
  width: 100%;
  display: flex;
  justify-content: center;
}

#staff svg {
  /* viewBox-based scaling handled by staffRenderer.js */
}

/* Feedback */
.feedback {
  font-size: 1.2rem;
  font-weight: 700;
  min-height: 2em;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 8px 24px;
  border-radius: var(--radius);
  transition: all 0.2s ease;
}

.feedback.correct {
  color: var(--correct);
  background: var(--correct-bg);
  animation: feedbackPop 0.3s ease;
}

.feedback.wrong {
  color: var(--wrong);
  background: var(--wrong-bg);
  animation: feedbackShake 0.4s ease;
}

@keyframes feedbackPop {
  0% { transform: scale(0.9); opacity: 0; }
  50% { transform: scale(1.05); }
  100% { transform: scale(1); opacity: 1; }
}

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

/* Note buttons — tile grid */
.note-buttons {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(60px, 1fr));
  gap: 10px;
  max-width: 480px;
  width: 100%;
  margin: 0 auto;
  padding: 0 12px;
}

.note-btn {
  aspect-ratio: 1;
  border: 2px solid var(--border);
  border-radius: 12px;
  background: var(--surface);
  color: var(--text);
  font-size: 1.2rem;
  font-weight: 700;
  cursor: pointer;
  transition: all 0.12s ease;
  display: flex;
  align-items: center;
  justify-content: center;
  white-space: nowrap;
  box-shadow: 0 2px 6px rgba(0,0,0,0.1);
  user-select: none;
  -webkit-tap-highlight-color: transparent;
}

.note-btn:hover {
  border-color: var(--primary);
  color: var(--primary);
  transform: translateY(-2px);
  box-shadow: 0 4px 12px rgba(0,0,0,0.15);
}

.note-btn:active,
.note-btn.pressed {
  transform: scale(0.95);
  background: var(--primary);
  color: #fff;
  border-color: var(--primary);
  box-shadow: none;
}

.note-btn:disabled {
  opacity: 0.5;
  cursor: not-allowed;
  transform: none;
}

/* Stats */
.stats {
  display: flex;
  gap: 20px;
  flex-wrap: wrap;
  align-items: center;
  justify-content: center;
  padding: 12px 20px;
  background: var(--surface);
  border-radius: var(--radius);
  box-shadow: var(--shadow);
  width: 100%;
}

.stat {
  text-align: center;
}

.stat-label {
  font-size: 0.7rem;
  text-transform: uppercase;
  letter-spacing: 0.06em;
  color: var(--text-secondary);
  font-weight: 600;
}

.stat-value {
  font-size: 1.2rem;
  font-weight: 700;
}

/* Share button inside stats */
.share-btn {
  display: flex;
  align-items: center;
  gap: 6px;
  background: rgba(255,255,255,0.08);
  color: var(--text-secondary);
  border: 1px solid rgba(255,255,255,0.15);
  border-radius: 8px;
  padding: 6px 14px;
  font-size: 0.78rem;
  font-weight: 600;
  cursor: pointer;
  transition: background 0.2s, color 0.2s;
  white-space: nowrap;
}
.share-btn:hover {
  background: rgba(255,255,255,0.16);
  color: var(--text-primary);
}

/* QR Popup Overlay */
.qr-overlay {
  position: fixed;
  inset: 0;
  z-index: 9999;
  display: flex;
  align-items: center;
  justify-content: center;
  background: rgba(0,0,0,0.6);
  backdrop-filter: blur(4px);
  -webkit-backdrop-filter: blur(4px);
}
.qr-overlay.hidden { display: none; }

.qr-popup {
  position: relative;
  background: var(--surface);
  border-radius: 16px;
  padding: 32px 36px 28px;
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 12px;
  box-shadow: 0 12px 40px rgba(0,0,0,0.5);
}

.qr-close {
  position: absolute;
  top: 8px;
  right: 12px;
  background: none;
  border: none;
  color: var(--text-secondary);
  font-size: 1.5rem;
  cursor: pointer;
  line-height: 1;
  padding: 4px;
}
.qr-close:hover { color: var(--text-primary); }

.qr-label {
  font-size: 0.85rem;
  color: var(--text-secondary);
  letter-spacing: 0.03em;
}

#qr-code {
  border-radius: 8px;
  background: #fff;
  padding: 10px;
}

/* MIDI + MIC */
.midi-section,
.mic-section {
  display: flex;
  align-items: center;
  gap: 12px;
  padding: 8px 0;
}

.mic-section { flex-wrap: wrap; }

.mic-controls {
  width: 100%;
  display: flex;
  flex-direction: column;
  gap: 8px;
  padding-top: 4px;
}
.mic-meter {
  position: relative;
  height: 8px;
  background: var(--border);
  border-radius: 4px;
}
.mic-meter-fill {
  height: 100%;
  width: 0%;
  background: var(--primary);
  border-radius: 4px;
  transition: width 0.05s linear;
}
.mic-meter-threshold {
  position: absolute;
  top: -3px; bottom: -3px;
  width: 2px;
  background: var(--wrong);
  border-radius: 1px;
}
.mic-threshold-label {
  display: flex;
  align-items: center;
  gap: 8px;
  font-size: 0.78rem;
  color: var(--text-secondary);
}
.mic-threshold-label input[type="range"] {
  flex: 1;
  accent-color: var(--primary);
}

.midi-toggle-label {
  display: flex;
  align-items: center;
  gap: 8px;
  font-size: 0.85rem;
  font-weight: 600;
  cursor: pointer;
}

.midi-toggle-label input[type="checkbox"] {
  width: 18px;
  height: 18px;
  accent-color: var(--primary);
}

.midi-status {
  font-size: 0.8rem;
  font-weight: 600;
  padding: 4px 10px;
  border-radius: 12px;
}

.midi-status.connected {
  color: var(--correct);
  background: var(--correct-bg);
}

.midi-status.disconnected {
  color: var(--text-secondary);
  background: var(--bg);
}

/* Keyboard hint */
.hint {
  font-size: 0.75rem;
  color: var(--text-secondary);
  text-align: center;
}

/* Fingering hint */
.fingering-row {
  width: 100%;
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 10px;
}

.hint-btn {
  padding: 4px 12px;
  border: 1px solid var(--border);
  border-radius: var(--radius);
  background: transparent;
  color: var(--text-secondary);
  font-size: 0.78rem;
  font-weight: 600;
  cursor: pointer;
  transition: all 0.15s ease;
}
.hint-btn:hover { border-color: var(--primary); color: var(--primary); }

.fingering-hint {
  background: var(--surface);
  border-radius: var(--radius);
  padding: 12px 20px;
  box-shadow: var(--shadow);
  display: flex;
  justify-content: center;
  align-items: center;
}

/* Level section */
.level-section {
  width: 100%;
  background: var(--surface);
  border-radius: var(--radius);
  box-shadow: var(--shadow);
  padding: 12px 16px;
  position: relative;
}

.level-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
}

.level-info {
  display: flex;
  align-items: center;
  gap: 10px;
}

.level-badge {
  background: var(--primary);
  color: #fff;
  font-size: 0.7rem;
  font-weight: 700;
  padding: 3px 10px;
  border-radius: 12px;
  text-transform: uppercase;
  letter-spacing: 0.04em;
}

.level-name {
  font-size: 0.95rem;
  font-weight: 700;
}

.level-description {
  font-size: 0.8rem;
  color: var(--text-secondary);
  margin-top: 4px;
}

.level-selector-btn {
  background: none;
  border: 2px solid var(--border);
  border-radius: var(--radius);
  color: var(--text-secondary);
  width: 34px;
  height: 34px;
  display: flex;
  align-items: center;
  justify-content: center;
  cursor: pointer;
  transition: all 0.15s ease;
}

.level-selector-btn:hover {
  border-color: var(--primary);
  color: var(--primary);
}

/* Progress bar */
.progress-bar-container {
  width: 100%;
  height: 6px;
  background: var(--border);
  border-radius: 3px;
  margin-top: 10px;
  overflow: hidden;
}

.progress-bar {
  height: 100%;
  width: 0%;
  background: var(--primary);
  border-radius: 3px;
  transition: width 0.4s ease, background-color 0.3s ease;
}

.progress-bar.met {
  background: var(--correct);
}

.progress-text {
  font-size: 0.7rem;
  color: var(--text-secondary);
  margin-top: 4px;
  text-align: center;
}

/* Level dropdown */
.level-dropdown {
  width: 100%;
  background: var(--surface);
  border-radius: var(--radius);
  box-shadow: var(--shadow);
  border: 1px solid var(--border);
  overflow: hidden;
}

.level-option {
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 10px 16px;
  cursor: pointer;
  transition: background 0.12s ease;
  font-size: 0.85rem;
  border-bottom: 1px solid var(--border);
}

.level-option:last-child {
  border-bottom: none;
}

.level-option:hover {
  background: var(--bg);
}

.level-option.current {
  background: var(--bg);
  font-weight: 700;
  border-left: 3px solid var(--primary);
}

.level-option.locked {
  opacity: 0.45;
  cursor: not-allowed;
}

.level-option-num {
  font-weight: 700;
  font-size: 0.75rem;
  color: var(--primary);
  min-width: 20px;
}

.level-option.locked .level-option-num {
  color: var(--text-secondary);
}

.level-option-name {
  flex: 1;
}

.level-option-lock {
  font-size: 0.8rem;
  color: var(--text-secondary);
}

/* Advancement prompt overlay */
.advance-prompt-overlay {
  position: fixed;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
  background: rgba(0, 0, 0, 0.6);
  display: flex;
  align-items: center;
  justify-content: center;
  z-index: 1000;
  animation: overlayFadeIn 0.3s ease;
}

.advance-prompt-card {
  background: var(--surface);
  border-radius: 16px;
  padding: 28px 36px;
  text-align: center;
  box-shadow: 0 8px 32px rgba(0, 0, 0, 0.25);
  animation: levelUpBounce 0.5s ease;
  min-width: 260px;
  max-width: 340px;
}

.advance-prompt-emoji {
  font-size: 2.5rem;
  margin-bottom: 6px;
}

.advance-prompt-title {
  font-size: 1.4rem;
  font-weight: 800;
  color: var(--primary);
  margin-bottom: 4px;
}

.advance-prompt-text {
  font-size: 0.95rem;
  color: var(--text-secondary);
  margin-bottom: 8px;
}

.advance-prompt-level {
  font-size: 0.9rem;
  font-weight: 600;
  color: var(--text);
  margin-bottom: 16px;
}

.advance-prompt-buttons {
  display: flex;
  gap: 10px;
  justify-content: center;
  margin-bottom: 14px;
}

.advance-btn {
  padding: 10px 22px;
  border-radius: 8px;
  border: none;
  font-size: 0.95rem;
  font-weight: 700;
  cursor: pointer;
  transition: transform 0.1s, box-shadow 0.15s;
}

.advance-btn:active {
  transform: scale(0.96);
}

.advance-btn-yes {
  background: var(--primary);
  color: #fff;
  box-shadow: 0 2px 8px rgba(76, 175, 80, 0.35);
}

.advance-btn-yes:hover {
  box-shadow: 0 4px 14px rgba(76, 175, 80, 0.5);
}

.advance-btn-no {
  background: rgba(255, 255, 255, 0.12);
  color: #fff;
  border: 1px solid rgba(255, 255, 255, 0.25);
}

.advance-timer-track {
  height: 4px;
  background: var(--bg-alt, #e0e0e0);
  border-radius: 2px;
  overflow: hidden;
}

.advance-timer-bar {
  height: 100%;
  width: 100%;
  background: var(--primary);
  border-radius: 2px;
  transform-origin: left;
  /* animation set via JS */
}

@keyframes advanceTimerShrink {
  from { transform: scaleX(1); }
  to   { transform: scaleX(0); }
}

/* Level-up celebration overlay */
.level-up-overlay {
  position: fixed;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
  background: rgba(0, 0, 0, 0.6);
  display: flex;
  align-items: center;
  justify-content: center;
  z-index: 1000;
  animation: overlayFadeIn 0.3s ease;
}

.level-up-card {
  background: var(--surface);
  border-radius: 16px;
  padding: 32px 40px;
  text-align: center;
  box-shadow: 0 8px 32px rgba(0, 0, 0, 0.2);
  animation: levelUpBounce 0.5s ease;
}

.level-up-trophy {
  font-size: 3rem;
  margin-bottom: 8px;
}

.level-up-title {
  font-size: 1.6rem;
  font-weight: 800;
  color: var(--primary);
  margin-bottom: 4px;
}

.level-up-level {
  font-size: 1rem;
  font-weight: 700;
  margin-bottom: 2px;
}

.level-up-name {
  font-size: 0.95rem;
  font-weight: 600;
  color: var(--text);
}

.level-up-desc {
  font-size: 0.8rem;
  color: var(--text-secondary);
  margin-top: 4px;
}

@keyframes overlayFadeIn {
  from { opacity: 0; }
  to { opacity: 1; }
}

@keyframes levelUpBounce {
  0% { transform: scale(0.5); opacity: 0; }
  60% { transform: scale(1.08); }
  100% { transform: scale(1); opacity: 1; }
}

/* Utilities */
.hidden {
  display: none !important;
}

/* Dark mode VexFlow SVG adjustments */
@media (prefers-color-scheme: dark) {
  #staff svg text,
  #composition-staff svg text,
  #practice-staff svg text {
    fill: var(--text) !important;
  }

  #staff svg path,
  #staff svg rect,
  #composition-staff svg path,
  #composition-staff svg rect,
  #practice-staff svg path,
  #practice-staff svg rect {
    fill: var(--text) !important;
    stroke: var(--text) !important;
  }

  #staff svg line,
  #composition-staff svg line,
  #practice-staff svg line {
    stroke: var(--text) !important;
  }

  /* Staff lines should be subtler */
  #staff svg .vf-stave line,
  #composition-staff svg .vf-stave line,
  #practice-staff svg .vf-stave line {
    stroke: var(--text-secondary) !important;
  }
}

/* Responsive */
@media (max-width: 480px) {
  header h1 {
    font-size: 1.3rem;
  }

  .note-buttons {
    grid-template-columns: repeat(auto-fit, minmax(56px, 1fr));
    gap: 8px;
    padding: 0 8px;
  }

  .note-btn {
    font-size: 1.15rem;
    border-radius: 10px;
  }

  .stats {
    gap: 12px;
    padding: 10px 12px;
  }

  .stat-value {
    font-size: 1rem;
  }

  .share-btn {
    display: none;
  }
}
