<!-- SS · part 26 -->
key', S.key);
    fd.append('patterns', JSON.stringify(S.patterns));
    fd.append('studio', JSON.stringify({ sounds: S.sounds, vols: S.vols, mutes: S.mutes, swing: S.swing, chords: S.chords, suno: S.suno, genre: S.genre, mood: S.mood }));
    if (withAudio && recBlob) { var type = recBlob.type || 'audio/webm'; fd.append('audio', recBlob, 'ss-mix.' + (/mp4|aac/.test(type) ? 'm4a' : /ogg/.test(type) ? 'ogg' : 'webm')); }
    return fd;
  }
  function doSave(btn, withAudio) {
    if (!S.title) { var t = prompt('Song title?', 'Untitled'); if (t === null) return; S.title = t.trim() || 'Untitled'; save(); fillWrite(); }
    busy(btn, true);
    api('POST', '/api/ss/save', songForm(withAudio)).then(function (j) {
      toast('Saved “' + j.song.title + '” to My Songs.');
      if (withAudio) { $('#ss-rec-out').hidden = true; recBlob = null; }
      songsLoaded = false;
    }).catch(function (e) { toast(e.message, 5000); }).then(function () { busy(btn, false); });
  }
  $('#ss-save').addEventListener('click', function () { doSave(this, true); });
  $('#ss-save-draft').addEventListener('click', function () { doSave(this, false); });

  // ---------------------------------------------------------------- MY SONGS
  var songsLoaded = false;
  if (IS_ADMIN) $('#ss-all-wrap').hidden = false;
  $('#ss-all').addEventListener('change', function () { songsLoaded = false; loadSongs(); });
  function loadSongs() {
    if (songsLoaded) return;
    songsLoaded = true;
    var ul = $('#ss-songs'); ul.innerHTML = '<li class="ss-hint">Loading…</li>';
    api('GET', '/api/ss/songs' + (IS_ADMIN && $('#ss-all').checked ? '?all=1' : '')).then(function (j) {
      var songs = j.songs || [];
      ul.innerHTML = songs.length ? songs.map(function (s) {
        return '<li class="ss-song" data-id="' + s.id + '"><div class="ss-song-h"><span class="ss-song-t">' + esc(s.title) + '</span><span class="ss-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="ss-hint">No recording (draft)</div>') +
          '<div class="ss-actions"><button type="button" class="ss-btn ss-btn-sm" data-act="load">Open</button><button type="button" class="ss-btn ss-btn-sm" data-act="rename">Rename</button>' +
          (s.url ? '<a class="ss-btn ss-btn-sm" href="' + esc(s.url) + '" download>Download</a>' : '') +
          (IS_ADMIN && s.url ? '<button type="button" class="ss-btn ss-btn-sm" data-act="radio">Send to Radio</button>' : '') +
          '<button type="button" class="ss-btn ss-btn-sm" data-act="delete">Delete</button></div></li>';
      }).join('') : '<li class="ss-hint">No songs yet. Record one in the Studio tab and save it.</li>';
    }).catch(function (e) { songsLoaded = false; ul.innerHTML = '<li class="ss-hint">' + esc(e.message) + '</li>'; });
  }
  $('#ss-songs').addEventListener('click', function (e) {
    var b = e.target.closest('[data-act]'); if (!b) return;
    var li = b.closest('.ss-song'), id = li.getAttribute('data-id'), act = b.getAttribute('data-act');
    if (act === 'rename') {
      var t = prompt('New title', li.querySelector('.ss-song-t').textContent); if (!t || !t.trim()) return;
      api('POST', '/api/ss/songs/' + id + '/rename', { title: t.trim() }).then(function () { li.querySelector('.ss-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; S.patterns = s.patterns;
        if (st.sounds) S.sounds = st.sounds; if (st.vols) S.vols = st.vols; if (st.mutes) S.mutes = st.mutes;
        if (typeof st.swing === 'number') S.swing = st.swing; S.chords = st.chords || []; S.suno = st.suno || ''; S.genre = st.genre || ''; S.mood = st.mood || '';
        S.voxClips = null; save(); fillWrite();
        if (E) { ['drums', 'bass', 'chords', 'lead'].forEach(function (t) { setSound(t, S.sounds[t]); E.ch[t].volume.value = S.vols[t]; E.ch[t].mute = !!S.mutes[t]; }); }
        syncStudio(); showTab('studio'); toast('Opened “' + s.title + '”.');
      }).catch(function (x) { toast(x.message); 