<!-- SS · part 96 -->
recorder.stop().then(function (blob) { recBlob = blob; if (recUrl) URL.revokeObjectURL(recUrl); recUrl = URL.createObjectURL(blob); var a = $('#es-rec-audio'); a.src = recUrl; a.hidden = false; $('#es-rec-row').hidden = false; showX('Recorded. Listen, then save it to My Songs.'); }); }, 700);
  }
  $('#es-rec-discard').addEventListener('click', function () { recBlob = null; $('#es-rec-row').hidden = true; $('#es-rec-audio').hidden = true; $('#es-xstatus').textContent = ''; });
  $('#es-rec-save').addEventListener('click', function () {
    var btn = this; if (!recBlob || !ensureTitle()) return; var type = recBlob.type || 'audio/webm';
    busy(btn, true); api('POST', '/api/ss/save', projectForm(recBlob, 'es-live.' + (/mp4|aac/.test(type) ? 'm4a' : /ogg/.test(type) ? 'ogg' : 'webm')))
      .then(function (j) { toast('Saved "' + j.song.title + '".'); $('#es-rec-row').hidden = true; recBlob = null; songsLoaded = false; }).catch(function (e) { toast(e.message, 5000); }).then(function () { busy(btn, false); });
  });
  $('#es-save-draft').addEventListener('click', function () { var b = this; if (!ensureTitle()) return; busy(b, true); api('POST', '/api/ss/save', projectForm(null)).then(function (j) { toast('Project "' + j.song.title + '" saved to My Songs.'); songsLoaded = false; }).catch(function (e) { toast(e.message); }).then(function () { busy(b, false); }); });

  // ------------------------------------------------------------------ my songs
  var songsLoaded = false;
  if (IS_ADMIN) $('#es-all-wrap').hidden = false;
  $('#es-all').addEventListener('change', function () { songsLoaded = false; loadSongs(); });
  function loadSongs() {
    if (songsLoaded) return; songsLoaded = true; var ul = $('#es-songlist'); ul.innerHTML = '<li class="es-hint">Loading…</li>';
    api('GET', '/api/ss/songs' + (IS_ADMIN && $('#es-all').checked ? '?all=1' : '')).then(function (j) {
      var songs = j.songs || [];
      ul.innerHTML = songs.length ? songs.map(function (s) {
        return '<li class="es-song" data-id="' + s.id + '"><div class="es-song-h"><span class="es-song-t">' + esc(s.title) + '</span><span class="es-song-m">' + esc((s.owner ? s.owner + ' · ' : '') + s.bpm + ' BPM · ' + s.key + ' · ' + fmtDate(s.created_at)) + '</span></div>' +
          (s.url ? '<audio controls preload="none" src="' + esc(s.url) + '"></audio>' : '<div class="es-hint">Project only (no audio)</div>') +
          '<div class="es-prompt-row"><button type="button" class="es-btn es-btn-sm" data-act="load">Open</button><button type="button" class="es-btn es-btn-sm" data-act="rename">Rename</button>' +
          (s.url ? '<a class="es-btn es-btn-sm" href="' + esc(s.url) + '" download>Download</a>' : '') +
          (IS_ADMIN && s.url ? '<button type="button" class="es-btn es-btn-sm" data-act="radio">Send to Radio</button>' : '') +
          '<button type="button" class="es-btn es-btn-sm" data-act="delete">Delete</button></div></li>';
      }).join('') : '<li class="es-hint">No songs yet. Generate one, then Export.</li>';
    }).catch(function (e) { songsLoaded = false; ul.innerHTML = '<li class="es-hint">' + esc(e.message) + '</li>'; });
  }
  $('#es-songlist').addEventListener('click', function (e) {
    var b = e.target.closest('[data-act]'); if (!b) return; var li = b.closest('.es-song'), id = li.getAttribute('data-id'), act = b.getAttribute('data-act');
    if (act === 'rename') { var t = prompt('New title', li.querySelector('.es-song-t').textContent); if (!t || !t.trim()) return; api('POST', '/api/ss/songs/' + id + '/rename', { title: t.trim() }).then(function () { li.querySelector('.es-song-t').textContent = t.trim(); toast('Renamed.'); }).catch(function (x) { toast(x.message); }); }
    else if (act === 'delete') { if (!confirm('Delete this song for good?')) return; api('POST', '/api/ss/songs/' + id + '/delete', {}).then(function () { li.remove(); toast('Deleted.'); }).catch(function (x) { toast(x.message); }); }
    else if (act === 'radio') { if (!confirm('Add this song to the radio library?')) return; busy(b, true); api('POST', '/api/ss/songs/' + id + '/radio', {}).then(function () { b.textContent = 'On the radio ✓'; toast('Added to the radio library.'); }).catch(function (x) { busy(b, false); toast(x.message); }); }
    else if (act === 'load') {
      api('GET', '/api/ss/songs/' + id).then(function (j) {
        var s = j.song, st = s.studio || {}; stop();
        S.title = s.title; S.lyrics = s.lyrics || ''; S.bpm = s.bpm; S.key = parseKey(s.key).name;
        if (s.sections && s.arrangement && s.arrangement.length) { SECS.forEach(function (n) { if (s.sections[n]) S.sections[n] = { bars: s.sections[n].bars, patterns: normPat(s.sections[n].patterns) }; }); S.arrangement = s.arrangement.slice(); }
        else { SECS.forEach(function (n) { S.sections[n].patterns = normPat(s.patterns); }); }
        ['sounds', 'vols', 'mutes', 'solos', 'master', 'voice'].forEach(function (k) { if (st[k]) S[k] = Object.assign({}