<!-- SS · part 82 -->
-rec-row').hidden = true; songsLoaded = false; toast('Exported to My Songs.'); })
      .catch(function (e) { showX('Export failed: ' + (e && e.message || e)); })
      .then(function () { busy(btn, false); });
  });
  var recBlob = null, recUrl = null, recording = false;
  $('#es-rec').addEventListener('click', function () {
    var btn = this;
    unlock().then(function () {
      if (!E.recorder) return toast('This browser cannot record audio — use Export instead.');
      if (recording) return finishRec();
      recording = true; btn.classList.add('is-on'); btn.textContent = '■ STOP';
      E.recorder.start(); play(0);
      showX('Recording live… press ■ STOP (or let the song finish).');
      E.T.scheduleOnce(function (t) { Tone.getDraw().schedule(function () { if (recording) finishRec(); }, t); }, totalSec() + 1.5);
    }).catch(function () { toast('Tap again to start audio.'); });
  });
  function finishRec() {
    recording = false; var btn = $('#es-rec'); btn.classList.remove('is-on'); btn.textContent = '● REC'; stop();
    setTimeout(function () { E.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>' : '') +
          (s.url ? '<button type="button" class="es-btn es-btn-sm es-sellbtn" data-act="sell">Sell…</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 s