:root {
  --bg-deep: #070a12;
  --bg-panel: #101726;
  --bg-elevated: #172033;
  --border-subtle: rgba(148, 163, 184, 0.16);
  --border-gold: rgba(212, 175, 55, 0.42);
  --text-primary: #f1f5f9;
  --text-secondary: #94a3b8;
  --text-muted: #64748b;
  --accent-gold: #d4af37;
  --accent-gold-bright: #eab308;
  --accent-cyan: #38bdf8;
  --accent-danger: #ef4444;
  --accent-success: #10b981;
  --font-display: 'Cinzel', Georgia, serif;
  --font-sans: 'Inter', -apple-system, BlinkMacSystemFont, sans-serif;
  --font-mono: 'JetBrains Mono', monospace;
}

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

html {
  scroll-behavior: smooth;
}

body {
  background-color: var(--bg-deep);
  background-image:
    radial-gradient(circle at 50% 0%, rgba(56, 189, 248, 0.08), transparent 55%),
    radial-gradient(circle at 85% 30%, rgba(212, 175, 55, 0.06), transparent 45%);
  color: var(--text-primary);
  font-family: var(--font-sans);
  line-height: 1.5;
  min-height: 100vh;
}

.hidden {
  display: none !important;
}

.app-shell {
  max-width: 1060px;
  margin: 0 auto;
  padding: 1.5rem 1.25rem 3rem;
  display: flex;
  flex-direction: column;
  gap: 1.25rem;
}

.top-header {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  justify-content: space-between;
  gap: 1rem;
  padding-bottom: 1rem;
  border-bottom: 1px solid var(--border-subtle);
}

.brand-group {
  display: flex;
  align-items: center;
  gap: 0.9rem;
}

.brand-emblem {
  width: 44px;
  height: 44px;
  border-radius: 50%;
  background: rgba(212, 175, 55, 0.08);
  border: 1px solid var(--border-gold);
  display: grid;
  place-items: center;
}

.brand-title-row {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  gap: 0.55rem;
}

.brand-title-row h1 {
  font-family: var(--font-display);
  font-size: 1.45rem;
  letter-spacing: 0.08em;
  color: var(--text-primary);
}

.domain-pill {
  font-family: var(--font-mono);
  font-size: 0.72rem;
  color: var(--accent-cyan);
  background: rgba(56, 189, 248, 0.1);
  border: 1px solid rgba(56, 189, 248, 0.25);
  padding: 0.15rem 0.55rem;
  border-radius: 999px;
}

.live-badge {
  font-family: var(--font-mono);
  font-size: 0.68rem;
  font-weight: 600;
  color: #6ee7b7;
  background: rgba(16, 185, 129, 0.12);
  border: 1px solid rgba(16, 185, 129, 0.35);
  padding: 0.15rem 0.5rem;
  border-radius: 999px;
}

.brand-subtitle {
  font-size: 0.82rem;
  color: var(--text-secondary);
}

.header-actions {
  display: flex;
  align-items: center;
  gap: 0.65rem;
}

.panel {
  background: var(--bg-panel);
  border: 1px solid var(--border-subtle);
  border-radius: 14px;
  padding: 1.35rem;
  box-shadow: 0 12px 30px rgba(0, 0, 0, 0.35);
  transition: border-color 0.35s ease, box-shadow 0.35s ease;
}

.panel.highlight-pulse {
  border-color: var(--accent-gold);
  box-shadow: 0 0 0 2px rgba(212, 175, 55, 0.28), 0 14px 34px rgba(0, 0, 0, 0.45);
}

.panel-header {
  display: flex;
  flex-wrap: wrap;
  justify-content: space-between;
  align-items: flex-start;
  gap: 0.75rem;
  margin-bottom: 1.1rem;
}

.panel-header h2 {
  font-size: 1rem;
  font-weight: 600;
  color: var(--accent-gold);
  letter-spacing: 0.02em;
}

.panel-hint {
  font-size: 0.8rem;
  color: var(--text-secondary);
}

.match-status {
  display: flex;
  align-items: center;
  gap: 0.75rem;
}

.match-pill {
  font-family: var(--font-mono);
  font-size: 0.78rem;
  font-weight: 600;
  padding: 0.3rem 0.7rem;
  border-radius: 6px;
  background: rgba(56, 189, 248, 0.12);
  color: var(--accent-cyan);
  border: 1px solid rgba(56, 189, 248, 0.3);
}

.match-pill.zero-matches {
  background: rgba(239, 68, 68, 0.15);
  color: #fca5a5;
  border-color: rgba(239, 68, 68, 0.45);
}

.filters-grid {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(165px, 1fr));
  gap: 0.85rem;
}

.filter-group {
  display: flex;
  flex-direction: column;
  gap: 0.35rem;
}

.filter-group label {
  font-size: 0.73rem;
  font-weight: 600;
  text-transform: uppercase;
  letter-spacing: 0.05em;
  color: var(--text-secondary);
}

select {
  background-color: var(--bg-elevated);
  color: var(--text-primary);
  border: 1px solid var(--border-subtle);
  border-radius: 8px;
  padding: 0.55rem 0.75rem;
  font-family: var(--font-sans);
  font-size: 0.85rem;
  cursor: pointer;
  transition: border-color 0.2s ease;
}

select:hover,
select:focus {
  outline: none;
  border-color: var(--accent-gold);
}

.icon-btn,
.secondary-btn {
  background: var(--bg-elevated);
  color: var(--text-primary);
  border: 1px solid var(--border-subtle);
  border-radius: 8px;
  padding: 0.5rem 0.85rem;
  font-size: 0.84rem;
  font-weight: 500;
  cursor: pointer;
  display: inline-flex;
  align-items: center;
  gap: 0.45rem;
  transition: all 0.18s ease;
}

.icon-btn:hover,
.secondary-btn:hover {
  border-color: var(--accent-gold);
  background: #1e293b;
}

.text-btn {
  background: none;
  border: none;
  color: var(--text-secondary);
  font-size: 0.8rem;
  text-decoration: underline;
  cursor: pointer;
}

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

.visualizer-panel {
  background: linear-gradient(180deg, #101726 0%, #0b101c 100%);
  border-color: var(--border-gold);
}

.turntable-stage {
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 1.25rem;
  padding: 0.5rem 0;
}

.platter-wrapper {
  position: relative;
  width: 250px;
  height: 250px;
  display: grid;
  place-items: center;
}

.vinyl-disc {
  width: 236px;
  height: 236px;
  border-radius: 50%;
  background: radial-gradient(circle, #18181b 0%, #09090b 65%, #1f242d 100%);
  border: 2px solid #27272a;
  box-shadow: 0 12px 32px rgba(0, 0, 0, 0.75);
  position: relative;
  display: grid;
  place-items: center;
  transition: transform 0.4s ease;
}

.vinyl-disc.spinning {
  animation: spinVinyl 0.9s linear infinite;
}

@keyframes spinVinyl {
  from { transform: rotate(0deg); }
  to { transform: rotate(360deg); }
}

.vinyl-grooves {
  position: absolute;
  inset: 12px;
  border-radius: 50%;
  background: repeating-radial-gradient(
    circle at center,
    transparent 0px,
    transparent 3px,
    rgba(255, 255, 255, 0.04) 4px,
    transparent 5px
  );
  pointer-events: none;
}

.globe-spindle {
  width: 132px;
  height: 132px;
  border-radius: 50%;
  background: #060d1a;
  border: 2px solid var(--accent-gold);
  box-shadow: 0 0 20px rgba(56, 189, 248, 0.25);
  overflow: hidden;
  display: grid;
  place-items: center;
  z-index: 2;
}

#globeCanvas {
  width: 132px;
  height: 132px;
  display: block;
}

.tonearm {
  position: absolute;
  top: 6px;
  right: -8px;
  width: 90px;
  height: 150px;
  pointer-events: none;
  transform-origin: 72px 18px;
  transform: rotate(0deg);
  transition: transform 0.55s cubic-bezier(0.22, 1, 0.36, 1);
  z-index: 4;
}

.tonearm.engaged {
  transform: rotate(24deg);
}

.tonearm-pivot {
  position: absolute;
  top: 8px;
  right: 8px;
  width: 22px;
  height: 22px;
  border-radius: 50%;
  background: var(--accent-gold);
  border: 3px solid #1e293b;
}

.tonearm-rod {
  position: absolute;
  top: 24px;
  right: 17px;
  width: 4px;
  height: 105px;
  background: linear-gradient(90deg, #94a3b8, #e2e8f0);
  border-radius: 2px;
}

.tonearm-headshell {
  position: absolute;
  bottom: 12px;
  right: 13px;
  width: 12px;
  height: 20px;
  background: var(--accent-gold-bright);
  border-radius: 2px;
  transform: rotate(18deg);
}

.spin-controls {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 0.5rem;
}

.primary-spin-btn {
  background: linear-gradient(135deg, #d4af37 0%, #b48a1e 100%);
  color: #070a12;
  border: none;
  border-radius: 999px;
  padding: 0.85rem 2.1rem;
  font-family: var(--font-display);
  font-size: 1.05rem;
  font-weight: 700;
  letter-spacing: 0.08em;
  cursor: pointer;
  display: inline-flex;
  align-items: center;
  gap: 0.6rem;
  box-shadow: 0 6px 20px rgba(212, 175, 55, 0.35);
  transition: transform 0.15s ease, box-shadow 0.15s ease, opacity 0.15s ease;
}

.primary-spin-btn:hover:not(:disabled) {
  transform: translateY(-2px);
  box-shadow: 0 10px 26px rgba(212, 175, 55, 0.5);
}

.primary-spin-btn:disabled {
  opacity: 0.65;
  cursor: wait;
}

.shortcut-hint {
  font-size: 0.76rem;
  color: var(--text-muted);
}

kbd {
  background: var(--bg-elevated);
  border: 1px solid var(--border-subtle);
  border-radius: 4px;
  padding: 0.1rem 0.35rem;
  font-family: var(--font-mono);
  font-size: 0.7rem;
  color: var(--text-secondary);
}

.state-box {
  text-align: center;
  padding: 2rem 1rem;
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 0.65rem;
}

.state-icon {
  font-size: 2rem;
}

.spinner-ring {
  width: 36px;
  height: 36px;
  border: 3px solid rgba(56, 189, 248, 0.2);
  border-top-color: var(--accent-cyan);
  border-radius: 50%;
  animation: spinVinyl 0.8s linear infinite;
}

.state-box h3 {
  font-size: 1.1rem;
  color: var(--text-primary);
}

.state-box p {
  max-width: 540px;
  font-size: 0.88rem;
  color: var(--text-secondary);
}

.error-state {
  background: rgba(239, 68, 68, 0.08);
  border: 1px solid rgba(239, 68, 68, 0.4);
  border-radius: 10px;
}

.error-state h3 {
  color: #fca5a5;
}

.album-card {
  display: grid;
  grid-template-columns: 230px 1fr;
  gap: 1.5rem;
  align-items: center;
}

@media (max-width: 680px) {
  .album-card {
    grid-template-columns: 1fr;
  }
}

.album-sleeve {
  width: 100%;
  aspect-ratio: 1 / 1;
  border-radius: 10px;
  padding: 1.1rem;
  background: linear-gradient(135deg, #1e293b 0%, #0f172a 100%);
  border: 1px solid var(--border-gold);
  position: relative;
  display: flex;
  flex-direction: column;
  justify-content: space-between;
  overflow: hidden;
  box-shadow: 0 10px 25px rgba(0, 0, 0, 0.45);
}

.cover-art-img {
  position: absolute;
  inset: 0;
  width: 100%;
  height: 100%;
  object-fit: cover;
  z-index: 3;
}

.sleeve-rings {
  position: absolute;
  right: -35%;
  bottom: -35%;
  width: 180px;
  height: 180px;
  border-radius: 50%;
  border: 18px solid rgba(255, 255, 255, 0.04);
  pointer-events: none;
}

.sleeve-meta {
  display: flex;
  justify-content: space-between;
  align-items: center;
  z-index: 1;
}

.sleeve-year {
  font-family: var(--font-mono);
  font-size: 0.78rem;
  background: rgba(0, 0, 0, 0.45);
  padding: 0.15rem 0.5rem;
  border-radius: 4px;
  color: var(--accent-gold);
}

.sleeve-flag {
  font-size: 1.5rem;
}

.sleeve-center-text {
  z-index: 1;
}

.sleeve-artist {
  font-size: 0.76rem;
  text-transform: uppercase;
  letter-spacing: 0.08em;
  color: rgba(255, 255, 255, 0.75);
  margin-bottom: 0.2rem;
}

.sleeve-title {
  font-family: var(--font-display);
  font-size: 1.05rem;
  font-weight: 700;
  color: #ffffff;
  line-height: 1.25;
}

.geo-banner {
  display: flex;
  flex-wrap: wrap;
  gap: 0.5rem;
  margin-bottom: 0.65rem;
}

.country-pill {
  font-size: 0.8rem;
  font-weight: 600;
  color: var(--accent-gold-bright);
  background: rgba(212, 175, 55, 0.12);
  border: 1px solid rgba(212, 175, 55, 0.35);
  padding: 0.2rem 0.65rem;
  border-radius: 999px;
}

.coords-pill,
.archive-pill {
  font-family: var(--font-mono);
  font-size: 0.74rem;
  color: var(--accent-cyan);
  background: rgba(56, 189, 248, 0.1);
  border: 1px solid rgba(56, 189, 248, 0.25);
  padding: 0.2rem 0.65rem;
  border-radius: 999px;
}

.archive-pill {
  color: #cbd5e1;
  background: rgba(148, 163, 184, 0.1);
  border-color: rgba(148, 163, 184, 0.25);
}

.album-title {
  font-family: var(--font-display);
  font-size: 1.55rem;
  color: var(--text-primary);
  margin-bottom: 0.2rem;
}

.artist-name {
  font-size: 1.05rem;
  font-weight: 500;
  color: var(--text-secondary);
  margin-bottom: 0.85rem;
}

.tags-row {
  display: flex;
  flex-wrap: wrap;
  gap: 0.45rem;
  margin-bottom: 1rem;
}

.meta-tag {
  font-size: 0.76rem;
  font-family: var(--font-mono);
  background: var(--bg-elevated);
  border: 1px solid var(--border-subtle);
  color: var(--text-secondary);
  padding: 0.2rem 0.55rem;
  border-radius: 6px;
}

.preview-audio-box {
  display: flex;
  align-items: center;
  gap: 0.75rem;
  padding: 0.55rem 0.85rem;
  margin-bottom: 1rem;
  background: rgba(56, 189, 248, 0.08);
  border: 1px solid rgba(56, 189, 248, 0.3);
  border-radius: 8px;
}

.preview-play-btn {
  background: var(--accent-cyan);
  color: #070a12;
  border: none;
  border-radius: 6px;
  padding: 0.35rem 0.75rem;
  font-size: 0.8rem;
  font-weight: 700;
  cursor: pointer;
  white-space: nowrap;
  transition: opacity 0.15s ease;
}

.preview-play-btn:hover {
  opacity: 0.9;
}

.preview-track-name {
  font-family: var(--font-mono);
  font-size: 0.76rem;
  color: var(--text-secondary);
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.field-notes {
  border-left: 3px solid var(--accent-gold);
  padding: 0.5rem 0.85rem;
  background: rgba(255, 255, 255, 0.02);
  color: var(--text-secondary);
  font-size: 0.88rem;
  margin-bottom: 1.15rem;
}

.card-actions {
  display: flex;
  flex-wrap: wrap;
  gap: 0.55rem;
}

.ext-link {
  text-decoration: none;
  font-size: 0.82rem;
  font-weight: 500;
  padding: 0.5rem 0.8rem;
  border-radius: 8px;
  border: 1px solid var(--border-subtle);
  background: var(--bg-elevated);
  color: var(--text-primary);
  transition: border-color 0.18s ease;
}

.ext-link:hover {
  border-color: var(--accent-cyan);
}

/* SESSION RECENT SPINS STRIP */
.recent-spins-wrap {
  margin-top: 1.35rem;
  padding-top: 1.1rem;
  border-top: 1px solid var(--border-subtle);
}

.recent-spins-header {
  display: flex;
  flex-wrap: wrap;
  justify-content: space-between;
  align-items: center;
  gap: 0.5rem;
  margin-bottom: 0.75rem;
}

.recent-spins-title {
  font-family: var(--font-mono);
  font-size: 0.72rem;
  font-weight: 600;
  letter-spacing: 0.08em;
  color: var(--accent-gold);
}

.recent-spins-hint {
  font-size: 0.75rem;
  color: var(--text-muted);
}

.recent-spins-strip {
  display: flex;
  gap: 0.75rem;
  overflow-x: auto;
  padding-bottom: 0.35rem;
  scrollbar-width: thin;
  scrollbar-color: rgba(212, 175, 55, 0.4) transparent;
}

.spin-thumb-btn {
  flex: 0 0 132px;
  background: var(--bg-elevated);
  border: 1px solid var(--border-subtle);
  border-radius: 10px;
  padding: 0.55rem;
  text-align: left;
  cursor: pointer;
  color: var(--text-primary);
  display: flex;
  flex-direction: column;
  gap: 0.4rem;
  transition: border-color 0.18s ease, transform 0.18s ease;
}

.spin-thumb-btn:hover {
  border-color: var(--accent-gold);
  transform: translateY(-2px);
}

.spin-thumb-btn.active {
  border-color: var(--accent-gold);
  box-shadow: 0 0 0 1px rgba(212, 175, 55, 0.45);
}

.spin-thumb-art {
  width: 100%;
  aspect-ratio: 1 / 1;
  border-radius: 6px;
  position: relative;
  overflow: hidden;
  display: flex;
  flex-direction: column;
  justify-content: space-between;
  padding: 0.45rem;
  border: 1px solid rgba(255, 255, 255, 0.1);
}

.spin-thumb-art img {
  position: absolute;
  inset: 0;
  width: 100%;
  height: 100%;
  object-fit: cover;
  z-index: 2;
}

.spin-thumb-flag {
  font-size: 1rem;
  z-index: 1;
}

.spin-thumb-year {
  font-family: var(--font-mono);
  font-size: 0.65rem;
  color: var(--accent-gold);
  background: rgba(0, 0, 0, 0.55);
  padding: 0.1rem 0.35rem;
  border-radius: 4px;
  align-self: flex-start;
  z-index: 1;
}

.spin-thumb-title {
  font-size: 0.78rem;
  font-weight: 600;
  color: var(--text-primary);
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.spin-thumb-artist {
  font-size: 0.7rem;
  color: var(--text-secondary);
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.app-footer {
  display: flex;
  flex-wrap: wrap;
  justify-content: space-between;
  gap: 0.5rem;
  padding-top: 1rem;
  border-top: 1px solid var(--border-subtle);
  font-size: 0.78rem;
  color: var(--text-muted);
}

.app-footer a {
  color: var(--accent-cyan);
  text-decoration: none;
}
```

### `app.js`[cite: 16]
```javascript
// Record Drift (recorddrift.com) — Live MusicBrainz & Cover Art Archive Edition
// Clean Discovery Mode: Smooth Auto-Scroll, Shareable Filter Deep-Links, 30s Previews, and Session Recent Spins

const COUNTRIES = [
  { code: "DZ", name: "Algeria", region: "Africa", coords: "LAT 28.03°N • LNG 1.65°E" },
  { code: "AR", name: "Argentina", region: "South America", coords: "LAT 34.60°S • LNG 58.38°W" },
  { code: "AU", name: "Australia", region: "Oceania", coords: "LAT 37.81°S • LNG 144.96°E" },
  { code: "AT", name: "Austria", region: "Europe", coords: "LAT 48.20°N • LNG 16.37°E" },
  { code: "BE", name: "Belgium", region: "Europe", coords: "LAT 50.85°N • LNG 4.35°E" },
  { code: "BO", name: "Bolivia", region: "South America", coords: "LAT 16.50°S • LNG 68.15°W" },
  { code: "BR", name: "Brazil", region: "South America", coords: "LAT 22.91°S • LNG 43.17°W" },
  { code: "BG", name: "Bulgaria", region: "Europe", coords: "LAT 42.69°N • LNG 23.32°E" },
  { code: "CM", name: "Cameroon", region: "Africa", coords: "LAT 3.84°N • LNG 11.50°E" },
  { code: "CA", name: "Canada", region: "North America", coords: "LAT 43.65°N • LNG 79.38°W" },
  { code: "CL", name: "Chile", region: "South America", coords: "LAT 33.44°S • LNG 70.66°W" },
  { code: "CN", name: "China", region: "Asia", coords: "LAT 39.90°N • LNG 116.40°E" },
  { code: "CO", name: "Colombia", region: "South America", coords: "LAT 4.71°N • LNG 74.07°W" },
  { code: "HR", name: "Croatia", region: "Europe", coords: "LAT 45.81°N • LNG 15.98°E" },
  { code: "CU", name: "Cuba", region: "North America", coords: "LAT 23.11°N • LNG 82.36°W" },
  { code: "CZ", name: "Czechia", region: "Europe", coords: "LAT 50.07°N • LNG 14.43°E" },
  { code: "CD", name: "DR Congo", region: "Africa", coords: "LAT 4.44°S • LNG 15.26°E" },
  { code: "DK", name: "Denmark", region: "Europe", coords: "LAT 55.67°N • LNG 12.56°E" },
  { code: "EC", name: "Ecuador", region: "South America", coords: "LAT 0.18°S • LNG 78.46°W" },
  { code: "EG", name: "Egypt", region: "Africa", coords: "LAT 30.04°N • LNG 31.23°E" },
  { code: "EE", name: "Estonia", region: "Europe", coords: "LAT 59.43°N • LNG 24.75°E" },
  { code: "ET", name: "Ethiopia", region: "Africa", coords: "LAT 9.03°N • LNG 38.74°E" },
  { code: "FI", name: "Finland", region: "Europe", coords: "LAT 60.16°N • LNG 24.93°E" },
  { code: "FR", name: "France", region: "Europe", coords: "LAT 48.85°N • LNG 2.35°E" },
  { code: "DE", name: "Germany", region: "Europe", coords: "LAT 52.52°N • LNG 13.40°E" },
  { code: "GH", name: "Ghana", region: "Africa", coords: "LAT 5.60°N • LNG 0.18°W" },
  { code: "GR", name: "Greece", region: "Europe", coords: "LAT 37.98°N • LNG 23.72°E" },
  { code: "HU", name: "Hungary", region: "Europe", coords: "LAT 47.49°N • LNG 19.04°E" },
  { code: "IS", name: "Iceland", region: "Europe", coords: "LAT 64.14°N • LNG 21.94°W" },
  { code: "IN", name: "India", region: "Asia", coords: "LAT 19.07°N • LNG 72.87°E" },
  { code: "ID", name: "Indonesia", region: "Asia", coords: "LAT 6.20°S • LNG 106.84°E" },
  { code: "IR", name: "Iran", region: "Asia", coords: "LAT 35.68°N • LNG 51.38°E" },
  { code: "IE", name: "Ireland", region: "Europe", coords: "LAT 53.34°N • LNG 6.26°W" },
  { code: "IL", name: "Israel", region: "Asia", coords: "LAT 32.08°N • LNG 34.78°E" },
  { code: "IT", name: "Italy", region: "Europe", coords: "LAT 41.90°N • LNG 12.49°E" },
  { code: "JM", name: "Jamaica", region: "North America", coords: "LAT 18.01°N • LNG 76.80°W" },
  { code: "JP", name: "Japan", region: "Asia", coords: "LAT 35.67°N • LNG 139.65°E" },
  { code: "KE", name: "Kenya", region: "Africa", coords: "LAT 1.29°S • LNG 36.82°E" },
  { code: "LB", name: "Lebanon", region: "Asia", coords: "LAT 33.89°N • LNG 35.50°E" },
  { code: "MY", name: "Malaysia", region: "Asia", coords: "LAT 3.13°N • LNG 101.68°E" },
  { code: "ML", name: "Mali", region: "Africa", coords: "LAT 12.63°N • LNG 8.00°W" },
  { code: "MX", name: "Mexico", region: "North America", coords: "LAT 19.43°N • LNG 99.13°W" },
  { code: "MA", name: "Morocco", region: "Africa", coords: "LAT 33.57°N • LNG 7.58°W" },
  { code: "NL", name: "Netherlands", region: "Europe", coords: "LAT 52.36°N • LNG 4.90°E" },
  { code: "NZ", name: "New Zealand", region: "Oceania", coords: "LAT 36.84°S • LNG 174.76°E" },
  { code: "NG", name: "Nigeria", region: "Africa", coords: "LAT 6.52°N • LNG 3.37°E" },
  { code: "NO", name: "Norway", region: "Europe", coords: "LAT 59.91°N • LNG 10.75°E" },
  { code: "PK", name: "Pakistan", region: "Asia", coords: "LAT 24.86°N • LNG 67.00°E" },
  { code: "PE", name: "Peru", region: "South America", coords: "LAT 12.04°S • LNG 77.04°W" },
  { code: "PH", name: "Philippines", region: "Asia", coords: "LAT 14.59°N • LNG 120.98°E" },
  { code: "PL", name: "Poland", region: "Europe", coords: "LAT 52.22°N • LNG 21.01°E" },
  { code: "PT", name: "Portugal", region: "Europe", coords: "LAT 38.72°N • LNG 9.13°W" },
  { code: "RO", name: "Romania", region: "Europe", coords: "LAT 44.42°N • LNG 26.10°E" },
  { code: "SN", name: "Senegal", region: "Africa", coords: "LAT 14.71°N • LNG 17.46°W" },
  { code: "RS", name: "Serbia", region: "Europe", coords: "LAT 44.78°N • LNG 20.44°E" },
  { code: "ZA", name: "South Africa", region: "Africa", coords: "LAT 26.20°S • LNG 28.04°E" },
  { code: "KR", name: "South Korea", region: "Asia", coords: "LAT 37.56°N • LNG 126.97°E" },
  { code: "ES", name: "Spain", region: "Europe", coords: "LAT 40.41°N • LNG 3.70°W" },
  { code: "SE", name: "Sweden", region: "Europe", coords: "LAT 59.32°N • LNG 18.06°E" },
  { code: "CH", name: "Switzerland", region: "Europe", coords: "LAT 47.37°N • LNG 8.54°E" },
  { code: "TW", name: "Taiwan", region: "Asia", coords: "LAT 25.03°N • LNG 121.56°E" },
  { code: "TZ", name: "Tanzania", region: "Africa", coords: "LAT 6.79°S • LNG 39.20°E" },
  { code: "TH", name: "Thailand", region: "Asia", coords: "LAT 13.75°N • LNG 100.50°E" },
  { code: "TN", name: "Tunisia", region: "Africa", coords: "LAT 36.80°N • LNG 10.18°E" },
  { code: "TR", name: "Turkey", region: "Asia", coords: "LAT 41.01°N • LNG 28.97°E" },
  { code: "UA", name: "Ukraine", region: "Europe", coords: "LAT 50.45°N • LNG 30.52°E" },
  { code: "GB", name: "United Kingdom", region: "Europe", coords: "LAT 51.50°N • LNG 0.12°W" },
  { code: "US", name: "United States", region: "North America", coords: "LAT 40.71°N • LNG 74.00°W" },
  { code: "UY", name: "Uruguay", region: "South America", coords: "LAT 34.90°S • LNG 56.16°W" },
  { code: "VE", name: "Venezuela", region: "South America", coords: "LAT 10.48°N • LNG 66.90°W" },
  { code: "VN", name: "Vietnam", region: "Asia", coords: "LAT 21.02°N • LNG 105.83°E" },
  { code: "ZM", name: "Zambia", region: "Africa", coords: "LAT 15.38°S • LNG 28.32°E" },
  { code: "ZW", name: "Zimbabwe", region: "Africa", coords: "LAT 17.82°S • LNG 31.05°E" }
];

const SLEEVE_PALETTES = [
  ["#78350f", "#b45309"],
  ["#064e3b", "#047857"],
  ["#1e3a8a", "#1d4ed8"],
  ["#14532d", "#ca8a04"],
  ["#7f1d1d", "#b45309"],
  ["#881337", "#be123c"],
  ["#0f172a", "#0284c7"],
  ["#581c87", "#9333ea"],
  ["#312e81", "#4f46e5"],
  ["#7c2d12", "#c2410c"]
];

function countryCodeToFlag(code) {
  if (!code || code.length !== 2) return "🌍";
  const upper = code.toUpperCase();
  return String.fromCodePoint(...[...upper].map(c => 127397 + c.charCodeAt(0)));
}

let currentRecord = null;
let isSpinning = false;
let audioEnabled = true;
let globeAngle = 0;
let globeSpeed = 0.012;
let lastApiCallTimestamp = 0;
const queryCache = new Map();
let recentSpins = [];

const countryLetterSelect = document.getElementById("countryLetterSelect");
const countrySelect = document.getElementById("countrySelect");
const artistLetterSelect = document.getElementById("artistLetterSelect");
const regionSelect = document.getElementById("regionSelect");
const decadeSelect = document.getElementById("decadeSelect");
const genreSelect = document.getElementById("genreSelect");
const matchCounter = document.getElementById("matchCounter");
const resetFiltersBtn = document.getElementById("resetFiltersBtn");
const alertResetBtn = document.getElementById("alertResetBtn");

const driftBtn = document.getElementById("driftBtn");
const driftBtnLabel = document.getElementById("driftBtnLabel");
const vinylDisc = document.getElementById("vinylDisc");
const tonearm = document.getElementById("tonearm");
const globeCanvas = document.getElementById("globeCanvas");
const ctx = globeCanvas.getContext("2d");

const resultsSection = document.getElementById("resultsSection");
const emptyPromptState = document.getElementById("emptyPromptState");
const loadingState = document.getElementById("loadingState");
const loadingTitle = document.getElementById("loadingTitle");
const loadingSub = document.getElementById("loadingSub");
const noMatchAlert = document.getElementById("noMatchAlert");
const alertHeading = document.getElementById("alertHeading");
const alertMessage = document.getElementById("alertMessage");
const albumCard = document.getElementById("albumCard");

const previewAudioBox = document.getElementById("previewAudioBox");
const previewPlayBtn = document.getElementById("previewPlayBtn");
const previewTrackName = document.getElementById("previewTrackName");
const previewAudioEl = document.getElementById("previewAudioEl");

const recentSpinsWrap = document.getElementById("recentSpinsWrap");
const recentSpinsStrip = document.getElementById("recentSpinsStrip");

const audioToggleBtn = document.getElementById("audioToggleBtn");
const audioIcon = document.getElementById("audioIcon");
const shareRecordBtn = document.getElementById("shareRecordBtn");

function revealResultsPanel(targetEl = resultsSection) {
  if (!resultsSection) return;
  resultsSection.classList.remove("highlight-pulse");
  void resultsSection.offsetWidth;
  resultsSection.classList.add("highlight-pulse");

  const scrollTarget = targetEl || resultsSection;
  requestAnimationFrame(() => {
    setTimeout(() => {
      scrollTarget.scrollIntoView({ behavior: "smooth", block: "center" });
    }, 80);
  });
}

function syncUrlParams() {
  const params = new URLSearchParams();
  if (regionSelect.value !== "ALL") params.set("region", regionSelect.value);
  if (countryLetterSelect.value !== "ALL") params.set("cletter", countryLetterSelect.value);
  if (countrySelect.value !== "ALL") params.set("country", countrySelect.value);
  if (artistLetterSelect.value !== "ALL") params.set("aletter", artistLetterSelect.value);
  if (decadeSelect.value !== "ALL") params.set("decade", decadeSelect.value);
  if (genreSelect.value !== "ALL") params.set("genre", genreSelect.value);

  const queryString = params.toString();
  const newUrl = queryString ? `${window.location.pathname}?${queryString}` : window.location.pathname;
  window.history.replaceState({}, "", newUrl);
}

function applyUrlParamsOnLoad() {
  const params = new URLSearchParams(window.location.search);
  let hasFilters = false;

  if (params.has("region")) {
    regionSelect.value = params.get("region");
    hasFilters = true;
  }
  if (params.has("cletter")) {
    countryLetterSelect.value = params.get("cletter").toUpperCase();
    hasFilters = true;
  }

  populateCountryDropdown();

  if (params.has("country")) {
    const cCode = params.get("country").toUpperCase();
    if ([...countrySelect.options].some(opt => opt.value === cCode)) {
      countrySelect.value = cCode;
      hasFilters = true;
    }
  }
  if (params.has("aletter")) {
    artistLetterSelect.value = params.get("aletter").toUpperCase();
    hasFilters = true;
  }
  if (params.has("decade")) {
    decadeSelect.value = params.get("decade");
    hasFilters = true;
  }
  if (params.has("genre")) {
    genreSelect.value = params.get("genre");
    hasFilters = true;
  }

  updateEligibilityStatus();

  if (hasFilters) {
    setTimeout(() => triggerDrift(), 250);
  }
}

function initFilters() {
  const letters = "ABCDEFGHIJKLMNOPQRSTUVWXYZ".split("");
  letters.forEach(letter => {
    const opt1 = document.createElement("option");
    opt1.value = letter;
    opt1.textContent = letter;
    countryLetterSelect.appendChild(opt1);

    const opt2 = document.createElement("option");
    opt2.value = letter;
    opt2.textContent = letter;
    artistLetterSelect.appendChild(opt2);
  });

  const regions = [...new Set(COUNTRIES.map(c => c.region))].sort();
  regions.forEach(r => {
    const opt = document.createElement("option");
    opt.value = r;
    opt.textContent = r;
    regionSelect.appendChild(opt);
  });

  populateCountryDropdown();
}

function getEligibleCountries() {
  const cLetter = countryLetterSelect.value;
  const region = regionSelect.value;

  return COUNTRIES.filter(c => {
    const firstChar = c.name.trim().charAt(0).toUpperCase();
    if (cLetter !== "ALL" && firstChar !== cLetter) return false;
    if (region !== "ALL" && c.region !== region) return false;
    return true;
  });
}

function populateCountryDropdown() {
  const previousValue = countrySelect.value;
  const eligible = getEligibleCountries();

  countrySelect.innerHTML = `<option value="ALL">Any Country in Filter (${eligible.length})</option>`;
  eligible.forEach(c => {
    const opt = document.createElement("option");
    opt.value = c.code;
    opt.textContent = `${countryCodeToFlag(c.code)} ${c.name}`;
    countrySelect.appendChild(opt);
  });

  if (eligible.some(c => c.code === previousValue)) {
    countrySelect.value = previousValue;
  } else {
    countrySelect.value = "ALL";
  }

  updateEligibilityStatus();
}

function updateEligibilityStatus() {
  const eligible = getEligibleCountries();
  if (eligible.length === 0) {
    matchCounter.textContent = "0 Eligible Countries";
    matchCounter.classList.add("zero-matches");
  } else if (countrySelect.value !== "ALL") {
    const chosen = eligible.find(c => c.code === countrySelect.value);
    matchCounter.textContent = `Target: ${chosen.name} (Live API)`;
    matchCounter.classList.remove("zero-matches");
  } else {
    matchCounter.textContent = `${eligible.length} Countries • Live API Pool`;
    matchCounter.classList.remove("zero-matches");
  }
  syncUrlParams();
}

function playNeedleSound(type = "drop") {
  if (!audioEnabled) return;
  try {
    const AudioCtx = window.AudioContext || window.webkitAudioContext;
    const audioCtx = new AudioCtx();
    const osc = audioCtx.createOscillator();
    const gain = audioCtx.createGain();

    if (type === "error") {
      osc.type = "sawtooth";
      osc.frequency.setValueAtTime(150, audioCtx.currentTime);
      osc.frequency.exponentialRampToValueAtTime(95, audioCtx.currentTime + 0.25);
      gain.gain.setValueAtTime(0.12, audioCtx.currentTime);
      gain.gain.exponentialRampToValueAtTime(0.001, audioCtx.currentTime + 0.26);
    } else {
      osc.type = "sine";
      osc.frequency.setValueAtTime(320, audioCtx.currentTime);
      osc.frequency.exponentialRampToValueAtTime(640, audioCtx.currentTime + 0.18);
      gain.gain.setValueAtTime(0.1, audioCtx.currentTime);
      gain.gain.exponentialRampToValueAtTime(0.001, audioCtx.currentTime + 0.2);
    }

    osc.connect(gain);
    gain.connect(audioCtx.destination);
    osc.start();
    osc.stop(audioCtx.currentTime + 0.28);
  } catch (e) {}
}

function renderGlobeLoop() {
  const w = globeCanvas.width;
  const h = globeCanvas.height;
  const cx = w / 2;
  const cy = h / 2;
  const radius = 58;

  ctx.clearRect(0, 0, w, h);

  const grad = ctx.createRadialGradient(cx - 15, cy - 15, 8, cx, cy, radius);
  grad.addColorStop(0, "#1e3a8a");
  grad.addColorStop(1, "#050914");
  ctx.fillStyle = grad;
  ctx.beginPath();
  ctx.arc(cx, cy, radius, 0, Math.PI * 2);
  ctx.fill();

  ctx.strokeStyle = "rgba(56, 189, 248, 0.35)";
  ctx.lineWidth = 1;
  [-0.6, -0.3, 0, 0.3, 0.6].forEach(lat => {
    const y = cy + lat * radius;
    const halfWidth = Math.sqrt(Math.max(0, radius * radius - (lat * radius) ** 2));
    ctx.beginPath();
    ctx.moveTo(cx - halfWidth, y);
    ctx.lineTo(cx + halfWidth, y);
    ctx.stroke();
  });

  for (let i = 0; i < 6; i++) {
    const angle = globeAngle + (i * Math.PI) / 6;
    const rx = Math.cos(angle) * radius;
    ctx.strokeStyle = i === 0 ? "rgba(212, 175, 55, 0.75)" : "rgba(56, 189, 248, 0.32)";
    ctx.beginPath();
    ctx.ellipse(cx, cy, Math.abs(rx), radius, 0, 0, Math.PI * 2);
    ctx.stroke();
  }

  ctx.strokeStyle = "rgba(212, 175, 55, 0.8)";
  ctx.lineWidth = 1.5;
  ctx.beginPath();
  ctx.arc(cx, cy, radius, 0, Math.PI * 2);
  ctx.stroke();

  globeAngle += globeSpeed;
  requestAnimationFrame(renderGlobeLoop);
}

async function politeMusicBrainzFetch(url) {
  const now = Date.now();
  const elapsed = now - lastApiCallTimestamp;
  if (elapsed < 1150) {
    await new Promise(resolve => setTimeout(resolve, 1150 - elapsed));
  }
  lastApiCallTimestamp = Date.now();

  const response = await fetch(url, {
    headers: { "Accept": "application/json" }
  });

  if (response.status === 503 || response.status === 429) {
    await new Promise(resolve => setTimeout(resolve, 1600));
    lastApiCallTimestamp = Date.now();
    const retryResp = await fetch(url, {
      headers: { "Accept": "application/json" }
    });
    if (!retryResp.ok) {
      throw new Error(`MusicBrainz HTTP ${retryResp.status}`);
    }
    return retryResp.json();
  }

  if (!response.ok) {
    throw new Error(`MusicBrainz HTTP ${response.status}`);
  }
  return response.json();
}

async function loadAudioPreview(artist, albumTitle) {
  previewAudioEl.pause();
  previewAudioEl.src = "";
  previewAudioBox.classList.add("hidden");
  previewPlayBtn.textContent = "▶ Play 30s Preview";

  try {
    const term = encodeURIComponent(`${artist} ${albumTitle}`);
    const resp = await fetch(`https://itunes.apple.com/search?term=${term}&media=music&entity=song&limit=5`);
    if (!resp.ok) return;
    const data = await resp.json();
    const match = (data.results || []).find(
      track => track.previewUrl && track.artistName && track.artistName.toLowerCase().includes(artist.toLowerCase().slice(0, 6))
    ) || (data.results || []).find(track => track.previewUrl);

    if (match && match.previewUrl) {
      previewAudioEl.src = match.previewUrl;
      previewTrackName.textContent = `Preview: "${match.trackName}"`;
      previewAudioBox.classList.remove("hidden");
    }
  } catch (e) {}
}

async function fetchAlbumsByArtistOrigin(countryObj) {
  const aLetter = artistLetterSelect.value;
  const decade = decadeSelect.value;
  const genre = genreSelect.value;

  const cacheKey = `${countryObj.code}|${aLetter}|${decade}|${genre}`;
  if (queryCache.has(cacheKey)) {
    const cached = queryCache.get(cacheKey);
    if (cached.albums.length > 0) {
      return cached;
    }
  }

  const artistQueryParts = [`country:${countryObj.code}`];
  if (aLetter !== "ALL") {
    artistQueryParts.push(`artist:${aLetter.toLowerCase()}*`);
  }
  if (genre !== "ALL") {
    artistQueryParts.push(`tag:"${genre}"`);
  }

  const artistQuery = artistQueryParts.join(" AND ");
  const artistUrl = `https://musicbrainz.org/ws/2/artist/?query=${encodeURIComponent(artistQuery)}&limit=50&fmt=json`;
  const artistData = await politeMusicBrainzFetch(artistUrl);

  let artists = artistData.artists || [];
  if (aLetter !== "ALL") {
    artists = artists.filter(a => a.name && a.name.trim().charAt(0).toUpperCase() === aLetter);
  }

  if (artists.length === 0) {
    return { totalArtists: 0, albums: [] };
  }

  const shuffledArtists = [...artists].sort(() => Math.random() - 0.5).slice(0, 4);
  let collectedAlbums = [];

  for (const artist of shuffledArtists) {
    const rgUrl = `https://musicbrainz.org/ws/2/release-group?artist=${artist.id}&type=album&inc=tags&limit=50&fmt=json`;
    const rgData = await politeMusicBrainzFetch(rgUrl);
    const rgs = rgData["release-groups"] || [];

    const matchingRgs = rgs.filter(rg => {
      const secondary = rg["secondary-types"] || [];
      if (secondary.includes("Compilation") || secondary.includes("Interview") || secondary.includes("Audiobook")) {
        return false;
      }

      if (decade !== "ALL") {
        const relDate = rg["first-release-date"] || "";
        const year = parseInt(relDate.slice(0, 4), 10);
        const startYear = parseInt(decade.slice(0, 4), 10);
        if (isNaN(year) || year < startYear || year > startYear + 9) {
          return false;
        }
      }

      return true;
    });

    matchingRgs.forEach(rg => {
      collectedAlbums.push({
        rg,
        artistName: artist.name,
        artistArea: artist.area?.name || countryObj.name,
        artistTags: (artist.tags || []).map(t => t.name)
      });
    });

    if (collectedAlbums.length >= 12) break;
  }

  const resultPayload = {
    totalArtists: artistData.count || artists.length,
    albums: collectedAlbums
  };

  if (collectedAlbums.length > 0) {
    queryCache.set(cacheKey, resultPayload);
  }

  return resultPayload;
}

function recordSessionSpin(rec) {
  recentSpins = recentSpins.filter(item => item.id !== rec.id);
  recentSpins.unshift(rec);
  if (recentSpins.length > 12) {
    recentSpins.pop();
  }
  renderRecentSpins();
}

function renderRecentSpins() {
  if (!recentSpinsWrap || !recentSpinsStrip) return;
  if (recentSpins.length <= 1) {
    recentSpinsWrap.classList.add("hidden");
    return;
  }

  recentSpinsWrap.classList.remove("hidden");
  recentSpinsStrip.innerHTML = "";

  recentSpins.forEach(item => {
    const btn = document.createElement("button");
    btn.type = "button";
    btn.className = "spin-thumb-btn" + (currentRecord && currentRecord.id === item.id ? " active" : "");
    btn.title = `${item.title} — ${item.artist} (${item.country})`;

    const art = document.createElement("div");
    art.className = "spin-thumb-art";
    art.style.background = `linear-gradient(135deg, ${item.palette[0]} 0%, ${item.palette[1]} 100%)`;

    const img = document.createElement("img");
    img.src = `https://coverartarchive.org/release-group/${item.id}/front-250`;
    img.alt = "";
    img.loading = "lazy";
    img.onerror = () => img.remove();

    const flagSpan = document.createElement("span");
    flagSpan.className = "spin-thumb-flag";
    flagSpan.textContent = item.flag;

    const yearSpan = document.createElement("span");
    yearSpan.className = "spin-thumb-year";
    yearSpan.textContent = item.year;

    art.appendChild(img);
    art.appendChild(flagSpan);
    art.appendChild(yearSpan);

    const titleDiv = document.createElement("div");
    titleDiv.className = "spin-thumb-title";
    titleDiv.textContent = item.title;

    const artistDiv = document.createElement("div");
    artistDiv.className = "spin-thumb-artist";
    artistDiv.textContent = `${item.artist} · ${item.country}`;

    btn.appendChild(art);
    btn.appendChild(titleDiv);
    btn.appendChild(artistDiv);

    btn.addEventListener("click", () => {
      if (isSpinning) return;
      noMatchAlert.classList.add("hidden");
      emptyPromptState.classList.add("hidden");
      currentRecord = item;
      playNeedleSound("drop");
      displayRecord(item, false);
      renderRecentSpins();
    });

    recentSpinsStrip.appendChild(btn);
  });
}

async function triggerDrift() {
  if (isSpinning) return;

  previewAudioEl.pause();

  const eligibleCountries = getEligibleCountries();
  if (eligibleCountries.length === 0) {
    showStrictError(
      "No Eligible Countries",
      "Zero countries match your Country First Letter and Continental Zone combination. Adjust your filters and drift again."
    );
    return;
  }

  isSpinning = true;
  driftBtn.disabled = true;
  driftBtnLabel.textContent = "DRIFTING...";

  emptyPromptState.classList.add("hidden");
  noMatchAlert.classList.add("hidden");
  albumCard.classList.add("hidden");
  loadingState.classList.remove("hidden");

  revealResultsPanel();

  vinylDisc.classList.add("spinning");
  tonearm.classList.add("engaged");
  globeSpeed = 0.095;
  playNeedleSound("drop");

  let targetCountry = null;
  if (countrySelect.value !== "ALL") {
    targetCountry = eligibleCountries.find(c => c.code === countrySelect.value);
  } else {
    targetCountry = eligibleCountries[Math.floor(Math.random() * eligibleCountries.length)];
  }

  loadingTitle.textContent = `Locking Coordinates: ${countryCodeToFlag(targetCountry.code)} ${targetCountry.name}`;
  loadingSub.textContent = `Finding artists originating in ${targetCountry.name} & browsing their discography...`;

  try {
    const result = await fetchAlbumsByArtistOrigin(targetCountry);

    if (!result.albums || result.albums.length === 0) {
      stopSpinner();
      showStrictError(
        `No Matching Albums Found in ${targetCountry.name}`,
        `MusicBrainz returned 0 official albums by artists from ${targetCountry.name} matching your exact artist letter, decade, and genre filters. Strict mode is active—adjust your filters or spin again.`
      );
      return;
    }

    let pool = result.albums;
    if (pool.length > 1 && currentRecord) {
      pool = pool.filter(entry => entry.rg.id !== currentRecord.id);
    }

    const chosenEntry = pool[Math.floor(Math.random() * pool.length)];
    const picked = chosenEntry.rg;
    const releaseDate = picked["first-release-date"] || "Unknown Year";
    const releaseYear = releaseDate.slice(0, 4) || "—";

    const rgTags = (picked.tags || [])
      .sort((a, b) => (b.count || 0) - (a.count || 0))
      .slice(0, 3)
      .map(t => t.name);

    const combinedTags = rgTags.length > 0 ? rgTags : chosenEntry.artistTags.slice(0, 3);
    const palette = SLEEVE_PALETTES[Math.floor(Math.random() * SLEEVE_PALETTES.length)];
    const flag = countryCodeToFlag(targetCountry.code);

    const recordObj = {
      id: picked.id,
      title: picked.title,
      artist: chosenEntry.artistName,
      country: targetCountry.name,
      countryCode: targetCountry.code,
      flag: flag,
      region: targetCountry.region,
      year: releaseYear,
      genre: combinedTags.length > 0 ? combinedTags.join(" / ") : (genreSelect.value !== "ALL" ? genreSelect.value : "Studio Album"),
      coords: targetCountry.coords,
      palette: palette,
      archiveTotal: `${result.totalArtists.toLocaleString()} artists`,
      notes: `Verified artist from ${chosenEntry.artistArea}, ${targetCountry.name} (${result.totalArtists.toLocaleString()} artists indexed in ${targetCountry.name} on MusicBrainz).`
    };

    stopSpinner();
    currentRecord = recordObj;
    matchCounter.textContent = `${result.totalArtists.toLocaleString()} Artists in ${targetCountry.name}`;
    matchCounter.classList.remove("zero-matches");
    displayRecord(recordObj, true);
  } catch (err) {
    stopSpinner();
    showStrictError(
      "MusicBrainz Rate Limit or Network Pause",
      "MusicBrainz briefly paused the request due to traffic limits. Wait 2 seconds and click Drift Record again."
    );
  }
}

function stopSpinner() {
  isSpinning = false;
  driftBtn.disabled = false;
  driftBtnLabel.textContent = "DRIFT RECORD";
  vinylDisc.classList.remove("spinning");
  loadingState.classList.add("hidden");
  globeSpeed = 0.012;
}

function showStrictError(heading, message) {
  playNeedleSound("error");
  tonearm.classList.remove("engaged");
  emptyPromptState.classList.add("hidden");
  loadingState.classList.add("hidden");
  albumCard.classList.add("hidden");
  alertHeading.textContent = heading;
  alertMessage.textContent = message;
  noMatchAlert.classList.remove("hidden");
  matchCounter.textContent = "0 Matching Records";
  matchCounter.classList.add("zero-matches");
  revealResultsPanel(noMatchAlert);
}

function displayRecord(rec, addToHistory = true) {
  albumCard.classList.remove("hidden");

  const sleeve = document.getElementById("albumSleeve");
  sleeve.style.background = `linear-gradient(135deg, ${rec.palette[0]} 0%, ${rec.palette[1]} 100%)`;

  const coverImg = document.getElementById("coverArtImg");
  coverImg.classList.add("hidden");
  coverImg.src = `https://coverartarchive.org/release-group/${rec.id}/front-250`;
  coverImg.onload = () => {
    coverImg.classList.remove("hidden");
  };
  coverImg.onerror = () => {
    coverImg.classList.add("hidden");
  };

  document.getElementById("sleeveYear").textContent = rec.year;
  document.getElementById("sleeveFlag").textContent = rec.flag;
  document.getElementById("sleeveArtist").textContent = rec.artist;
  document.getElementById("sleeveTitle").textContent = rec.title;

  document.getElementById("countryBadge").textContent = `${rec.flag} ${rec.country}`;
  document.getElementById("coordsBadge").textContent = rec.coords;
  document.getElementById("archiveTotalBadge").textContent = `Pool: ${rec.archiveTotal}`;
  document.getElementById("albumTitle").textContent = rec.title;
  document.getElementById("artistName").textContent = rec.artist;
  document.getElementById("yearTag").textContent = `Released: ${rec.year}`;
  document.getElementById("regionTag").textContent = rec.region;
  document.getElementById("genreTag").textContent = rec.genre;
  document.getElementById("fieldNotes").textContent = rec.notes;

  const query = encodeURIComponent(`${rec.artist} ${rec.title}`);
  document.getElementById("spotifyLink").href = `https://open.spotify.com/search/${query}`;
  document.getElementById("youtubeLink").href = `https://www.youtube.com/results?search_query=${query}`;
  document.getElementById("discogsLink").href = `https://www.discogs.com/search/?q=${query}&type=release`;
  document.getElementById("mbLink").href = `https://musicbrainz.org/release-group/${rec.id}`;

  if (addToHistory) {
    recordSessionSpin(rec);
  }

  revealResultsPanel(albumCard);
  loadAudioPreview(rec.artist, rec.title);
}

function resetAllFilters() {
  countryLetterSelect.value = "ALL";
  regionSelect.value = "ALL";
  artistLetterSelect.value = "ALL";
  decadeSelect.value = "ALL";
  genreSelect.value = "ALL";
  noMatchAlert.classList.add("hidden");
  populateCountryDropdown();
}

countryLetterSelect.addEventListener("change", populateCountryDropdown);
regionSelect.addEventListener("change", populateCountryDropdown);
countrySelect.addEventListener("change", updateEligibilityStatus);
artistLetterSelect.addEventListener("change", syncUrlParams);
decadeSelect.addEventListener("change", syncUrlParams);
genreSelect.addEventListener("change", syncUrlParams);

resetFiltersBtn.addEventListener("click", resetAllFilters);
alertResetBtn.addEventListener("click", resetAllFilters);
driftBtn.addEventListener("click", triggerDrift);

shareRecordBtn.addEventListener("click", async () => {
  syncUrlParams();
  const shareUrl = window.location.href;
  try {
    await navigator.clipboard.writeText(shareUrl);
    shareRecordBtn.innerHTML = "<span>✓ Link Copied!</span>";
    setTimeout(() => {
      shareRecordBtn.innerHTML = "<span>🔗 Share Filter</span>";
    }, 2000);
  } catch (e) {
    prompt("Copy this shareable Record Drift link:", shareUrl);
  }
});

previewPlayBtn.addEventListener("click", () => {
  if (previewAudioEl.paused) {
    previewAudioEl.play();
    previewPlayBtn.textContent = "⏸ Pause Preview";
  } else {
    previewAudioEl.pause();
    previewPlayBtn.textContent = "▶ Play 30s Preview";
  }
});

previewAudioEl.addEventListener("ended", () => {
  previewPlayBtn.textContent = "▶ Play 30s Preview";
});

document.addEventListener("keydown", e => {
  if (e.code === "Space" && document.activeElement.tagName !== "SELECT" && document.activeElement.tagName !== "BUTTON") {
    e.preventDefault();
    triggerDrift();
  }
});

audioToggleBtn.addEventListener("click", () => {
  audioEnabled = !audioEnabled;
  audioToggleBtn.setAttribute("aria-pressed", String(audioEnabled));
  audioIcon.textContent = audioEnabled ? "🔊" : "🔇";
  audioToggleBtn.querySelector(".btn-label").textContent = audioEnabled ? "Audio: ON" : "Audio: OFF";
});

initFilters();
renderGlobeLoop();
applyUrlParamsOnLoad();
