Dough Git

clove/musicPlayer

web/client/src/app.ts

back to web/client/src

// Single-page app: library, dedicated upload, browse shared playlists,
// per-user + shared playlists, and a full player bar.

import {
  api,
  type Song,
  type PendingSong,
  type SongEditRequest,
  type Playlist,
  type Me,
  type Lyrics,
  type Artist,
  type ArtistDetail,
  type LinkRequest,
  type DuplicateReview,
} from "@musicapp/shared";
import {
  Player,
  formatTime,
} from "@musicapp/player";
import { readTags } from "./metadata.ts";
import {
  initDesktopBridge,
  initDesktopAuth,
  startDesktopLogin,
  startDesktopLastfmConnect,
  onLastfmConnect,
  clearDesktopToken,
  isDesktop,
  openExternal,
  syncDiscordPresence,
} from "./desktop.ts";
import { initNative, isNativeAndroid } from "./native.ts";

initDesktopBridge();

// True when the web app is running inside one of the packaged shells (Electron
// desktop or the Android WebView) rather than a plain browser tab. Each shell
// exposes its own bridge global on the window, which is the signal. Used to
// hide "Get the app" prompts — pointless once you're already in the app.
const isInsideApp = isDesktop || isNativeAndroid;

const root = document.getElementById("app")!;
const player = new Player();
// Inside the native Android shell, route lock-screen/notification transport
// buttons into the queue. No-op on web/desktop.
initNative(player);

type View =
  | { kind: "library" }
  | { kind: "liked" }
  | { kind: "song"; id: string }
  | { kind: "browse" }
  | { kind: "playlist"; id: string }
  | { kind: "artists" }
  | { kind: "artist"; id: string }
  | { kind: "duplicates" }
  | { kind: "linkRequests" }
  | { kind: "pendingSongs" }
  | { kind: "editRequests" }
  | { kind: "downloads" }
  | { kind: "settings" };

// Admin-only views, and views that need any account, gated in boot()/popstate.
const ADMIN_VIEWS = new Set<View["kind"]>([
  "duplicates",
  "linkRequests",
  "pendingSongs",
  "editRequests",
]);
const ACCOUNT_VIEWS = new Set<View["kind"]>(["liked", "settings"]);

function allowedView(v: View): View {
  if (ADMIN_VIEWS.has(v.kind) && !state.me?.isAdmin) return { kind: "library" };
  if (ACCOUNT_VIEWS.has(v.kind) && !state.me) return { kind: "library" };
  return v;
}

// --- URL routing ------------------------------------------------------------
// Every view maps to a real, hotlinkable path (the server's catch-all route
// serves index.html for any of these, so a deep link works on first load too
// — see web/server/src/server.ts). Kept as simple bidirectional mappers
// rather than a router library: the view set is small and fixed.

function viewToPath(view: View): string {
  switch (view.kind) {
    case "library": return "/";
    case "liked": return "/liked";
    case "song": return `/song/${view.id}`;
    case "browse": return "/browse";
    case "artists": return "/artists";
    case "artist": return `/artist/${view.id}`;
    case "playlist": return `/playlist/${view.id}`;
    case "duplicates": return "/admin/duplicates";
    case "linkRequests": return "/admin/link-requests";
    case "pendingSongs": return "/admin/pending";
    case "editRequests": return "/admin/edit-requests";
    case "downloads": return "/downloads";
    case "settings": return "/settings";
  }
}

function pathToView(pathname: string): View {
  const [head, id] = pathname.split("/").filter(Boolean);
  if (head === "liked") return { kind: "liked" };
  if (head === "song" && id) return { kind: "song", id };
  if (head === "browse") return { kind: "browse" };
  if (head === "artists") return { kind: "artists" };
  if (head === "artist" && id) return { kind: "artist", id };
  if (head === "playlist" && id) return { kind: "playlist", id };
  if (head === "admin" && id === "duplicates") return { kind: "duplicates" };
  if (head === "admin" && id === "link-requests") return { kind: "linkRequests" };
  if (head === "admin" && id === "pending") return { kind: "pendingSongs" };
  if (head === "admin" && id === "edit-requests") return { kind: "editRequests" };
  if (head === "downloads") return { kind: "downloads" };
  if (head === "settings") return { kind: "settings" };
  return { kind: "library" };
}

// Position in our own in-app history, mirrored into history.state, so the
// header's back/forward arrows can tell when they're at an end. It's a
// counter, not the browser's real history length (which we can't read).
let navPos = 0;
let navMax = 0;

// Every user-triggered view change goes through here so the address bar
// always matches what's on screen (and so it's shareable/hotlinkable).
// popstate (back/forward) sets state.view + renders directly instead, since
// the URL there is already correct — see the listener below.
function navigate(view: View): void {
  state.view = view;
  navPos += 1;
  navMax = navPos; // a new navigation truncates any forward stack
  history.pushState({ navPos }, "", viewToPath(view));
  render();
}

// In-app back/forward. history.back()/forward() fire popstate, which just
// re-renders the SPA — no page load, so audio keeps playing.
function goBack(): void {
  if (navPos > 0) history.back();
}
function goForward(): void {
  if (navPos < navMax) history.forward();
}

const state = {
  me: null as Me | null,
  songs: [] as Song[],
  playlists: [] as Playlist[],
  likedSongs: [] as Song[],
  likedIds: new Set<string>(),
  view: { kind: "library" } as View,
  visibleSongs: [] as Song[],
  adminCounts: { duplicates: 0, linkRequests: 0, pendingSongs: 0, editRequests: 0 },
};

// Discord presence is refreshed whenever the *song* or the *play/pause* state
// changes — not on every timeupdate — so pausing freezes/removes the elapsed
// bar and resuming a already-loaded track re-activates presence. Deduped by
// (songId, playing) so the constant "change" stream doesn't spam Discord.
let presSongId: string | null = null;
let presPlaying = false;
let presInit = false;
function maybeUpdatePresence(): void {
  const song = player.current;
  const songId = song?.id ?? null;
  const playing = player.playing;
  if (presInit && songId === presSongId && playing === presPlaying) return;
  presInit = true;
  presSongId = songId;
  presPlaying = playing;
  // Clear presence when there's no song OR when paused. Discord shows its own
  // idle "elapsed" timer for a Listening activity that has no timestamps, so
  // the only way to show *nothing* while paused is to remove the activity. It
  // comes back with a proper progress bar the moment playback resumes.
  if (!song || !playing) {
    void syncDiscordPresence(null);
    return;
  }
  void syncDiscordPresence(song, { playing: true, positionS: player.progress.current });
}

let lastSave = 0;
player.events.on("change", () => {
  renderPlayerBar();
  syncLyrics();
  checkScrobbleThreshold();
  maybeUpdatePresence();
  // Cheap: just keep the active row's play/pause (animation) state in sync.
  // Which row is active is handled on trackchange, so no full sweep here.
  document
    .querySelector(".song.playing")
    ?.classList.toggle("paused", !player.playing);
  const now = Date.now();
  if (now - lastSave > 2000) {
    lastSave = now;
    saveNowPlaying();
  }
});
player.events.on("error", (msg) => flash(msg));

// --- Last.fm scrobbling -----------------------------------------------------
// Last.fm's own rules: send "now playing" as soon as a track starts, then
// scrobble once the lesser of (half the track's duration, 4 minutes) has
// elapsed — and never for tracks under 30s. See lib/lastfm.ts server-side.

let scrobbleTrack: { song: Song; startedAt: number; scrobbled: boolean } | null = null;

function onTrackStarted(song: Song): void {
  scrobbleTrack = { song, startedAt: Math.floor(Date.now() / 1000), scrobbled: false };
  if (!state.me?.lastfmConnected) return;
  api
    .lastfmNowPlaying({
      title: song.title,
      artist: song.artist,
      album: song.album,
      durationS: song.durationS,
    })
    .catch(() => {
      /* best-effort — never blocks playback */
    });
}
player.events.on("trackchange", (song) => {
  onTrackStarted(song);
  // Push presence immediately for the new track (with its current position),
  // rather than waiting for the next "change" tick. Deduped downstream.
  maybeUpdatePresence();
  highlightPlayingRow(); // move the now-playing bars to the new track's row
});

function checkScrobbleThreshold(): void {
  if (!scrobbleTrack || scrobbleTrack.scrobbled || !state.me?.lastfmConnected) return;
  const { current, duration } = player.progress;
  if (!duration || duration < 30) return; // Last.fm: no scrobbling under 30s

  const threshold = Math.min(duration / 2, 4 * 60);
  if (current < threshold) return;

  scrobbleTrack.scrobbled = true; // mark before the request settles, not after
  const { song, startedAt } = scrobbleTrack;
  api
    .lastfmScrobble({
      title: song.title,
      artist: song.artist,
      album: song.album,
      durationS: song.durationS,
      startedAt,
    })
    .catch(() => {
      /* best-effort */
    });
}

// Persist / restore the current session so a forced reload can resume.
const NOW_PLAYING_KEY = "music:nowplaying";

function saveNowPlaying(): void {
  try {
    const snap = player.snapshot();
    if (snap) localStorage.setItem(NOW_PLAYING_KEY, JSON.stringify(snap));
  } catch {
    /* ignore */
  }
}

function restoreNowPlaying(): void {
  try {
    const raw = localStorage.getItem(NOW_PLAYING_KEY);
    if (!raw) return;
    const snap = JSON.parse(raw) as {
      ids: string[];
      index: number;
      position: number;
    };
    const byId = new Map(state.songs.map((s) => [s.id, s]));
    const songs = snap.ids
      .map((id) => byId.get(id))
      .filter((s): s is Song => Boolean(s));
    if (!songs.length) return;
    const targetId = snap.ids[snap.index];
    const idx = targetId ? songs.findIndex((s) => s.id === targetId) : 0;
    player.restore(songs, idx >= 0 ? idx : 0, snap.position || 0);
  } catch {
    /* ignore */
  }
}

window.addEventListener("pagehide", saveNowPlaying);
window.addEventListener("beforeunload", saveNowPlaying);

// "Hide explicit" preference (public-friendly), persisted locally.
let hideExplicit = false;
try {
  hideExplicit = localStorage.getItem("music:hideExplicit") === "1";
} catch {
  /* ignore */
}
function filterExplicit<T extends { explicit: boolean }>(songs: T[]): T[] {
  return hideExplicit ? songs.filter((s) => !s.explicit) : songs;
}

// Space toggles play/pause anywhere except when typing in a field.
document.addEventListener("keydown", (e) => {
  if (e.code !== "Space" && e.key !== " ") return;
  const t = e.target as HTMLElement | null;
  const tag = t?.tagName;
  if (tag === "INPUT" || tag === "TEXTAREA" || t?.isContentEditable) return;
  e.preventDefault(); // stop page scroll + focused-button double toggle
  player.toggle();
});

// --- boot -----------------------------------------------------------------

async function boot(): Promise<void> {
  // Streaming/browsing is open to everyone: a null `me` is a valid anonymous
  // session, not a reason to show a login wall.
  state.me = await api.me();
  await Promise.all([loadSongs(), loadPlaylists(), loadLiked()]);
  if (state.me?.isAdmin) await refreshAdminCounts();

  state.view = allowedView(pathToView(window.location.pathname));

  restoreNowPlaying();
  handleLastfmRedirect();
  // Stamp the landing entry so the back/forward arrows have a baseline.
  history.replaceState({ navPos }, "", window.location.pathname + window.location.search);
  render();
}

// Back/forward: the URL is already correct, so just re-derive the view from
// it and render — no history.pushState (that would double up entries).
window.addEventListener("popstate", (e) => {
  const p = (e.state as { navPos?: number } | null)?.navPos;
  navPos = typeof p === "number" ? p : 0;
  state.view = allowedView(pathToView(window.location.pathname));
  render();
});

// Alt+Arrow back/forward (browsers already do this on web; belt-and-braces
// for the Electron shell where there's no browser chrome).
document.addEventListener("keydown", (e) => {
  if (!e.altKey || e.metaKey || e.ctrlKey) return;
  if (e.key === "ArrowLeft") {
    e.preventDefault();
    goBack();
  } else if (e.key === "ArrowRight") {
    e.preventDefault();
    goForward();
  }
});

// After the Last.fm "Connect" handshake, the backend redirects back here
// with ?lastfm=connected|error. Surface that, land on Settings, and drop
// the param so a refresh doesn't re-show the toast.
function handleLastfmRedirect(): void {
  const params = new URLSearchParams(window.location.search);
  const result = params.get("lastfm");
  if (!result) return;

  state.view = { kind: "settings" };
  if (result === "connected") flash("Last.fm connected.");
  else if (result === "error") flash("Couldn't connect Last.fm. Try again?");

  history.replaceState({ navPos }, "", viewToPath(state.view));
}

async function loadSongs(): Promise<void> {
  state.songs = await api.listSongs();
}

async function loadPlaylists(): Promise<void> {
  if (!state.me) {
    state.playlists = [];
    return;
  }
  state.playlists = await api.listPlaylists();
}

async function loadLiked(): Promise<void> {
  if (!state.me) {
    state.likedSongs = [];
    state.likedIds = new Set();
    return;
  }
  state.likedSongs = await api.listLiked();
  state.likedIds = new Set(state.likedSongs.map((s) => s.id));
}

async function refreshAdminCounts(): Promise<void> {
  if (!state.me?.isAdmin) return;
  const [dupes, reqs, pending, edits] = await Promise.all([
    api.listDuplicates("pending"),
    api.listLinkRequests("pending"),
    api.listPendingSongs(),
    api.listSongEditRequests("pending"),
  ]);
  state.adminCounts = {
    duplicates: dupes.length,
    linkRequests: reqs.length,
    pendingSongs: pending.length,
    editRequests: edits.length,
  };
}

// --- top-level render ------------------------------------------------------

function render(): void {
  root.innerHTML = `
    <button class="hamburger" id="hamburger" aria-label="Menu"><i class="bi bi-list"></i></button>
    <button class="app-logo-corner" id="logo-home" aria-label="Home" title="Home">
      <img src="/favicon.png" alt="Doughmination Music" />
    </button>
    <div class="layout">
      <aside class="sidebar" id="sidebar"></aside>
      <div class="scrim" id="scrim"></div>
      <main class="main" id="main">
        <nav class="nav-arrows" aria-label="History">
          <button class="icon-btn" id="nav-back" title="Back (Alt+Left)" aria-label="Back">
            <i class="bi bi-chevron-left"></i></button>
          <button class="icon-btn" id="nav-fwd" title="Forward (Alt+Right)" aria-label="Forward">
            <i class="bi bi-chevron-right"></i></button>
          ${
            isInsideApp
              ? ""
              : `<button class="btn btn-sm nav-get-app" id="nav-get-app" title="Download the desktop &amp; mobile apps">
                   <i class="bi bi-download"></i> Get the app
                 </button>`
          }
        </nav>
        <div id="main-content"></div>
      </main>
    </div>
    <div class="playerbar" id="playerbar"></div>
  `;
  renderSidebar();
  renderMain();
  renderPlayerBar();

  const back = document.getElementById("nav-back") as HTMLButtonElement | null;
  const fwd = document.getElementById("nav-fwd") as HTMLButtonElement | null;
  if (back) {
    back.disabled = navPos <= 0;
    back.addEventListener("click", goBack);
  }
  if (fwd) {
    fwd.disabled = navPos >= navMax;
    fwd.addEventListener("click", goForward);
  }
  document
    .getElementById("nav-get-app")
    ?.addEventListener("click", () => navigate({ kind: "downloads" }));

  const sidebar = document.getElementById("sidebar");
  const scrim = document.getElementById("scrim");
  const closeDrawer = () => {
    sidebar?.classList.remove("open");
    scrim?.classList.remove("show");
  };
  document.getElementById("hamburger")?.addEventListener("click", () => {
    sidebar?.classList.toggle("open");
    scrim?.classList.toggle("show");
  });
  scrim?.addEventListener("click", closeDrawer);
  sidebar
    ?.querySelectorAll("[data-view],[data-playlist],#upload-btn")
    .forEach((b) => b.addEventListener("click", closeDrawer));

  document.getElementById("logo-home")?.addEventListener("click", () => {
    closeDrawer();
    navigate({ kind: "library" });
  });
}

// Web navigates the same tab straight to the login route (cookie flow).
// Desktop can't: SSO/passkeys don't work in the embedded webview and the cookie
// would land in the wrong jar — it opens the system browser and returns via the
// doughmination:// deeplink (see startDesktopLogin). Web carries the deep link
// the user is on through the SSO round trip so a shared /song/<id> etc. still
// opens after signing in. Server validates it's a same-origin path — see
// routes/auth.ts.
function loginHref(): string {
  const returnPath = window.location.pathname + window.location.search;
  return (
    "/api/auth/login" +
    (returnPath && returnPath !== "/"
      ? `?redirect=${encodeURIComponent(returnPath)}`
      : "")
  );
}

// A "Sign in" control that does the right thing on web vs desktop. `id` must be
// unique in the document; call wireSignIn(id) after inserting it.
function signInControlHtml(id: string, label = "Sign in", cls = "btn btn-primary"): string {
  return isDesktop
    ? `<button class="${cls}" id="${id}">${label}</button>`
    : `<a class="${cls}" id="${id}" href="${loginHref()}">${label}</a>`;
}

function wireSignIn(id: string): void {
  if (!isDesktop) return;
  document.getElementById(id)?.addEventListener("click", () => {
    startDesktopLogin().catch((err) => {
      console.error("failed to start login", err);
      flash("Couldn't open the sign-in page. Try again?");
    });
  });
}

// --- sidebar --------------------------------------------------------------

function avatarHtml(url: string | null, name: string | null, cls = ""): string {
  if (url) return `<img class="avatar ${cls}" src="${url}" alt="" />`;
  const initial = (name ?? "?").trim().charAt(0).toUpperCase() || "?";
  return `<div class="avatar avatar-empty ${cls}">${escapeHtml(initial)}</div>`;
}

function renderSidebar(): void {
  const el = document.getElementById("sidebar");
  if (!el) return;

  const nav = (kind: View["kind"], label: string) => `
    <button class="nav-link ${state.view.kind === kind ? "active" : ""}"
            data-view="${kind}">${label}</button>`;

  const items = state.playlists
    .map(
      (p) => `
      <li class="${activePlaylist(p.id) ? "active" : ""}">
        <button data-playlist="${p.id}">
          <span class="pl-name">${escapeHtml(p.name)}</span>
          ${p.isPublic ? `<span class="pl-badge" title="Shared">shared</span>` : ""}
        </button>
      </li>`,
    )
    .join("");

  const badge = (n: number) => (n ? ` <span class="pl-badge">${n}</span>` : "");
  const adminNav = (kind: View["kind"], label: string, n: number) => `
    <button class="nav-link ${state.view.kind === kind ? "active" : ""}" data-view="${kind}">
      ${label}${badge(n)}
    </button>`;

  el.innerHTML = `
    <button class="brand" id="brand-home" title="Home">
      <img class="brand-logo" src="/favicon.png" alt="" /> Doughmination Music
    </button>
    ${nav("library", "Library")}
    ${state.me ? nav("liked", `<i class="bi bi-heart-fill"></i> Liked Songs`) : ""}
    ${
      state.me
        ? `<button class="nav-link" id="upload-btn"><i class="bi bi-cloud-arrow-up"></i> Upload</button>`
        : ""
    }
    ${nav("browse", "Browse shared")}
    ${nav("artists", "Artists")}
    ${
      state.me?.isAdmin
        ? `
      <div class="section-head"><span>Admin</span></div>
      ${adminNav("duplicates", "Duplicates", state.adminCounts.duplicates)}
      ${adminNav("linkRequests", "Artist requests", state.adminCounts.linkRequests)}
      ${adminNav("pendingSongs", "Upload approvals", state.adminCounts.pendingSongs)}
      ${adminNav("editRequests", "Edit requests", state.adminCounts.editRequests)}`
        : ""
    }

    ${
      state.me
        ? `
    <div class="section-head">
      <span>Playlists</span>
      <button class="icon-btn" id="new-playlist" title="New playlist"><i class="bi bi-plus-lg"></i></button>
    </div>
    <ul class="playlists">${items}</ul>`
        : ""
    }

    <div class="sidebar-foot">
      ${
        state.me
          ? `
        ${avatarHtml(state.me.avatarUrl ?? null, state.me.name ?? null, "sm")}
        <span class="me-name">${escapeHtml(state.me.name ?? state.me.email ?? "You")}</span>
        <button class="icon-btn" data-view="settings" title="Settings"><i class="bi bi-gear"></i></button>
        <button class="link" id="logout">Log out</button>`
          : `
        <span class="me-name">Browsing as guest</span>
        ${signInControlHtml("sidebar-signin", "Sign in", "btn btn-primary btn-sm")}`
      }
    </div>
  `;

  el.querySelectorAll("[data-view]").forEach((btn) => {
    btn.addEventListener("click", () => {
      const kind = (btn as HTMLElement).dataset.view as
        | "library"
        | "liked"
        | "browse"
        | "artists"
        | "duplicates"
        | "linkRequests"
        | "pendingSongs"
        | "editRequests"
        | "settings";
      navigate({ kind });
    });
  });

  document.getElementById("brand-home")?.addEventListener("click", () => navigate({ kind: "library" }));
  document.getElementById("upload-btn")?.addEventListener("click", openUploadModal);
  wireSignIn("sidebar-signin");

  el.querySelectorAll("[data-playlist]").forEach((btn) => {
    btn.addEventListener("click", () => {
      navigate({ kind: "playlist", id: (btn as HTMLElement).dataset.playlist! });
    });
  });

  document.getElementById("new-playlist")?.addEventListener("click", openCreatePlaylistModal);

  document.getElementById("logout")?.addEventListener("click", async () => {
    const res = await api.logout();
    const data = (await res.json()) as { endSession: string | null };
    if (isDesktop) {
      // No cookie / no navigation: drop the token and drop back to guest view.
      clearDesktopToken();
      state.me = null;
      await Promise.all([loadSongs(), loadPlaylists(), loadLiked()]);
      navigate({ kind: "library" });
      return;
    }
    window.location.href = data.endSession ?? "/";
  });
}

// --- main area -------------------------------------------------------------

async function renderMain(): Promise<void> {
  const el = document.getElementById("main-content");
  if (!el) return;

  const view = state.view;
  if (view.kind === "library") renderLibrary(el);
  else if (view.kind === "liked") renderLikedView(el);
  else if (view.kind === "song") await renderSongView(el, view.id);
  else if (view.kind === "browse") await renderBrowse(el);
  else if (view.kind === "artists") await renderArtists(el);
  else if (view.kind === "artist") await renderArtistView(el, view.id);
  else if (view.kind === "duplicates") await renderDuplicates(el);
  else if (view.kind === "linkRequests") await renderLinkRequests(el);
  else if (view.kind === "pendingSongs") await renderPendingSongs(el);
  else if (view.kind === "editRequests") await renderEditRequests(el);
  else if (view.kind === "downloads") await renderDownloadsView(el);
  else if (view.kind === "settings") await renderSettings(el);
  else await renderPlaylistView(el, view.id);
}

function renderLikedView(el: HTMLElement): void {
  const shown = filterExplicit(state.likedSongs);
  state.visibleSongs = shown;
  el.innerHTML = `
    <header class="main-head">
      <h2><i class="bi bi-heart-fill accent"></i> Liked Songs</h2>
      <span class="song-dur">${shown.length} song${shown.length === 1 ? "" : "s"}</span>
    </header>
    <div id="songlist">${
      shown.length
        ? songTableHtml(shown)
        : `<p class="empty">Songs you like show up here. Tap the heart on any track.</p>`
    }</div>
  `;
  wireSongList();
}

async function renderSongView(el: HTMLElement, id: string): Promise<void> {
  el.innerHTML = `<p class="empty">Loading…</p>`;
  let song: Song;
  try {
    song = await api.getSong(id);
  } catch {
    el.innerHTML = `<p class="empty">That song isn't here anymore.</p>`;
    return;
  }
  state.visibleSongs = [song];

  const cover = song.coverUrl
    ? `<img class="song-page-cover" src="${song.coverUrl}" alt="" />`
    : `<div class="song-page-cover cover-empty"><i class="bi bi-music-note-beamed"></i></div>`;
  const artist = song.artistId
    ? `<button class="song-artist artist-link" id="song-page-artist">${escapeHtml(song.artist)}</button>`
    : `<span class="song-artist">${escapeHtml(song.artist)}</span>`;
  const bits = [
    song.album ? escapeHtml(song.album) : "",
    song.durationS ? formatTime(song.durationS) : "",
  ].filter(Boolean).join(" · ");

  el.innerHTML = `
    <div class="song-page">
      ${cover}
      <div class="song-page-meta">
        <span class="song-title">${escapeHtml(song.title)}${
          song.explicit ? ` <span class="tag-e" title="Explicit">E</span>` : ""
        }</span>
        ${artist}
        ${bits ? `<span class="song-dur">${bits}</span>` : ""}
        <div class="song-page-actions">
          <button class="btn btn-primary" id="song-page-play"><i class="bi bi-play-fill"></i> Play</button>
          ${state.me ? `<button class="btn" id="song-page-like"><i class="bi bi-heart${
            state.likedIds.has(song.id) ? "-fill" : ""
          }"></i> ${state.likedIds.has(song.id) ? "Liked" : "Like"}</button>` : ""}
          <button class="btn" id="song-page-copy"><i class="bi bi-link-45deg"></i> Copy link</button>
          <a class="btn" href="/song/${song.id}?download=1"><i class="bi bi-download"></i> Download</a>
        </div>
      </div>
    </div>
  `;

  document.getElementById("song-page-play")?.addEventListener("click", () => {
    if (player.current?.id === song.id) player.toggle();
    else player.playQueue([song], 0);
  });
  document.getElementById("song-page-copy")?.addEventListener("click", () => copySongLink(song.id));
  document.getElementById("song-page-like")?.addEventListener("click", () => {
    void toggleLike(song.id);
    renderMain();
  });
  document.getElementById("song-page-artist")?.addEventListener("click", () => {
    if (song.artistId) openArtist(song.artistId);
  });
}

function renderLibrary(el: HTMLElement): void {
  const shown = filterExplicit(state.songs);
  state.visibleSongs = shown;
  el.innerHTML = `
    <header class="main-head">
      <h2>Library</h2>
      <div class="head-actions">
        <label class="check-inline">
          <input type="checkbox" id="hide-explicit" ${hideExplicit ? "checked" : ""} />
          Hide explicit
        </label>
        <input id="search" class="search" placeholder="Search title or artist" />
      </div>
    </header>
    <div id="songlist">${songTableHtml(shown)}</div>
  `;

  wireSongList();

  document.getElementById("hide-explicit")?.addEventListener("change", (e) => {
    hideExplicit = (e.target as HTMLInputElement).checked;
    try {
      localStorage.setItem("music:hideExplicit", hideExplicit ? "1" : "0");
    } catch {
      /* ignore */
    }
    renderMain();
  });

  const search = document.getElementById("search") as HTMLInputElement | null;
  search?.addEventListener("input", async () => {
    const rows = filterExplicit(
      await api.listSongs(search.value.trim() || undefined),
    );
    state.visibleSongs = rows;
    const list = document.getElementById("songlist");
    if (list) {
      list.innerHTML = songTableHtml(rows);
      wireSongList();
    }
  });
}

// --- downloads (native apps) ----------------------------------------------

async function renderDownloadsView(el: HTMLElement): Promise<void> {
  el.innerHTML = `
    <header class="main-head"><h2>Get the app</h2></header>
    <p class="empty">Loading…</p>
  `;

  let data: Awaited<ReturnType<typeof api.getDownloads>>;
  try {
    data = await api.getDownloads();
  } catch {
    el.innerHTML = `
      <header class="main-head"><h2>Get the app</h2></header>
      <p class="empty">Couldn't load the download list. Try again later.</p>
    `;
    return;
  }

  const cards = data.platforms
    .map(
      (p) => `
      <a class="download-card" href="${escapeHtml(p.url)}" target="_blank" rel="noopener">
        <i class="bi bi-${escapeHtml(p.icon)}"></i>
        <span class="dl-label">${escapeHtml(p.label)}</span>
        <span class="dl-file">${escapeHtml(p.filename)}</span>
        <span class="dl-go"><i class="bi bi-download"></i> Download</span>
      </a>`,
    )
    .join("");

  el.innerHTML = `
    <header class="main-head"><h2>Get the app</h2></header>
    <p class="downloads-sub">
      Stream in any browser, or install the app for Discord Rich Presence, media
      keys and offline-friendly playback.
      ${data.tag ? `Latest release: <strong>${escapeHtml(data.codename)}</strong> (${escapeHtml(data.tag)}).` : ""}
    </p>
    <div class="downloads-grid">${cards || `<p class="empty">No builds published yet.</p>`}</div>
    <p class="downloads-foot">
      <a href="${escapeHtml(data.releaseUrl)}" target="_blank" rel="noopener">Release notes &amp; checksums</a>
      · <a href="${escapeHtml(data.repoUrl)}" target="_blank" rel="noopener">All versions</a>
    </p>
  `;

  // Desktop: a top-level <a> would navigate the Electron webview off the
  // app, so hand external links to the system browser instead.
  el.querySelectorAll<HTMLAnchorElement>("a[href^='http']").forEach((a) => {
    a.addEventListener("click", (e) => {
      if (openExternal(a.href)) e.preventDefault();
    });
  });
}

// --- playlist modals ---------------------------------------------------------

function openCreatePlaylistModal(): void {
  const overlay = document.createElement("div");
  overlay.className = "modal-overlay";
  overlay.innerHTML = `
    <div class="modal" role="dialog" aria-modal="true">
      <div class="modal-head">
        <h2>New playlist</h2>
        <button class="icon-btn" id="modal-close" title="Close"><i class="bi bi-x-lg"></i></button>
      </div>
      <form id="playlist-form" class="upload-card">
        <label class="field"><span>Name <em>(required)</em></span>
          <input name="name" required autofocus /></label>
        <button class="btn btn-primary" type="submit">Create</button>
        <span id="playlist-status" class="upload-status"></span>
      </form>
    </div>
  `;
  document.body.appendChild(overlay);

  const close = () => {
    overlay.remove();
    document.removeEventListener("keydown", onEsc);
  };
  const onEsc = (e: KeyboardEvent) => {
    if (e.key === "Escape") close();
  };
  document.addEventListener("keydown", onEsc);
  overlay.addEventListener("click", (e) => {
    if (e.target === overlay) close();
  });
  overlay.querySelector("#modal-close")?.addEventListener("click", close);

  const form = overlay.querySelector("#playlist-form") as HTMLFormElement;
  const status = overlay.querySelector("#playlist-status") as HTMLElement;

  form.addEventListener("submit", async (e) => {
    e.preventDefault();
    const name = (form.querySelector('[name="name"]') as HTMLInputElement).value.trim();
    if (!name) return;
    const btn = form.querySelector("button")!;
    btn.textContent = "Creating...";
    btn.setAttribute("disabled", "true");
    try {
      await api.createPlaylist(name);
      close();
      await loadPlaylists();
      renderSidebar();
    } catch (err) {
      status.textContent = `Something went wrong: ${(err as Error).message}`;
      btn.textContent = "Create";
      btn.removeAttribute("disabled");
    }
  });
}

// Fuzzy-filterable picker for "add this song to a playlist", replacing the
// old numbered prompt(). Reuses the same dice-coefficient scoring as the
// server-side artist/song search so a fast typo-tolerant filter doesn't need
// its own separate matching logic.
function openPlaylistPickerModal(playlists: Playlist[]): Promise<string | null> {
  return new Promise((resolve) => {
    const overlay = document.createElement("div");
    overlay.className = "modal-overlay";
    overlay.innerHTML = `
      <div class="modal" role="dialog" aria-modal="true">
        <div class="modal-head">
          <h2>Add to playlist</h2>
          <button class="icon-btn" id="modal-close" title="Close"><i class="bi bi-x-lg"></i></button>
        </div>
        <input id="playlist-filter" class="search" placeholder="Filter playlists" autofocus />
        <ul class="playlist-picker" id="playlist-picker-list"></ul>
      </div>
    `;
    document.body.appendChild(overlay);

    let resolved = false;
    const finish = (id: string | null) => {
      if (resolved) return;
      resolved = true;
      overlay.remove();
      document.removeEventListener("keydown", onEsc);
      resolve(id);
    };
    const onEsc = (e: KeyboardEvent) => {
      if (e.key === "Escape") finish(null);
    };
    document.addEventListener("keydown", onEsc);
    overlay.addEventListener("click", (e) => {
      if (e.target === overlay) finish(null);
    });
    overlay.querySelector("#modal-close")?.addEventListener("click", () => finish(null));

    const list = overlay.querySelector("#playlist-picker-list") as HTMLElement;
    const filterInput = overlay.querySelector("#playlist-filter") as HTMLInputElement;

    const draw = (query: string) => {
      const matches = fuzzyFilter(playlists, query, (p) => p.name);
      list.innerHTML = matches
        .map(
          (p) =>
            `<li><button type="button" class="playlist-pick" data-id="${p.id}">${escapeHtml(p.name)}</button></li>`,
        )
        .join("") || `<li class="empty">No playlists match.</li>`;
      list.querySelectorAll("[data-id]").forEach((btn) => {
        btn.addEventListener("click", () => finish((btn as HTMLElement).dataset.id!));
      });
    };
    draw("");
    filterInput.addEventListener("input", () => draw(filterInput.value));
  });
}

// --- upload modal ----------------------------------------------------------

function openUploadModal(): void {
  const overlay = document.createElement("div");
  overlay.className = "modal-overlay";
  overlay.innerHTML = `
    <div class="modal" role="dialog" aria-modal="true">
      <div class="modal-head">
        <h2>Upload</h2>
        <button class="icon-btn" id="modal-close" title="Close"><i class="bi bi-x-lg"></i></button>
      </div>
      <div class="seg" role="tablist">
        <button class="seg-btn active" id="tab-files" role="tab" aria-selected="true" type="button">Files</button>
        <button class="seg-btn" id="tab-yt" role="tab" aria-selected="false" type="button">YouTube link</button>
      </div>
      <div id="files-mode">
        <div class="dropzone" id="dropzone" tabindex="0" role="button" aria-label="Add audio files">
          <i class="bi bi-cloud-arrow-up dz-icon"></i>
          <span class="dz-text">Drag audio files here, or <span class="dz-link">browse</span></span>
          <span class="dz-hint">One song lets you edit details; multiple upload in bulk.</span>
          <input type="file" accept="audio/*" multiple hidden />
        </div>
        <div id="upload-body"></div>
      </div>
      <div id="yt-mode" hidden></div>
    </div>
  `;
  document.body.appendChild(overlay);

  const close = () => {
    overlay.remove();
    document.removeEventListener("keydown", onEsc);
  };
  const onEsc = (e: KeyboardEvent) => {
    if (e.key === "Escape") close();
  };
  document.addEventListener("keydown", onEsc);
  overlay.addEventListener("click", (e) => {
    if (e.target === overlay) close();
  });
  overlay.querySelector("#modal-close")?.addEventListener("click", close);

  const dropzone = overlay.querySelector("#dropzone") as HTMLElement;
  const fileInput = overlay.querySelector('input[type="file"]') as HTMLInputElement;
  const body = overlay.querySelector("#upload-body") as HTMLElement;

  const onFiles = (files: FileList | File[] | null | undefined) => {
    const list = files ? Array.from(files) : [];
    if (list.length === 0) return;
    if (list.length === 1) renderSingle(list[0]!);
    else renderBatch(list);
  };

  dropzone.addEventListener("click", () => fileInput.click());
  dropzone.addEventListener("keydown", (e) => {
    if (e.key === "Enter" || e.key === " ") {
      e.preventDefault();
      fileInput.click();
    }
  });
  fileInput.addEventListener("change", () => onFiles(fileInput.files));
  ["dragenter", "dragover"].forEach((ev) =>
    dropzone.addEventListener(ev, (e) => {
      e.preventDefault();
      dropzone.classList.add("drag");
    }),
  );
  ["dragleave", "dragend"].forEach((ev) =>
    dropzone.addEventListener(ev, () => dropzone.classList.remove("drag")),
  );
  dropzone.addEventListener("drop", (e) => {
    e.preventDefault();
    dropzone.classList.remove("drag");
    onFiles((e as DragEvent).dataTransfer?.files);
  });

  // --- Files / YouTube link mode switch ---
  const filesMode = overlay.querySelector("#files-mode") as HTMLElement;
  const ytMode = overlay.querySelector("#yt-mode") as HTMLElement;
  const tabFiles = overlay.querySelector("#tab-files") as HTMLButtonElement;
  const tabYt = overlay.querySelector("#tab-yt") as HTMLButtonElement;
  let ytRendered = false;
  const showMode = (yt: boolean) => {
    filesMode.hidden = yt;
    ytMode.hidden = !yt;
    tabFiles.classList.toggle("active", !yt);
    tabYt.classList.toggle("active", yt);
    tabFiles.setAttribute("aria-selected", String(!yt));
    tabYt.setAttribute("aria-selected", String(yt));
    if (yt && !ytRendered) {
      renderYouTube();
      ytRendered = true;
    }
  };
  tabFiles.addEventListener("click", () => showMode(false));
  tabYt.addEventListener("click", () => showMode(true));

  // --- YouTube link: server-side yt-dlp import ---
  function renderYouTube(): void {
    ytMode.innerHTML = `
      <form id="yt-form" class="upload-card">
        <label class="field"><span>YouTube link <em>(required)</em></span>
          <input name="url" type="url" inputmode="url"
            placeholder="https://www.youtube.com/watch?v=…" required /></label>
        <p class="dz-hint">Audio is downloaded on the server with yt-dlp. A playlist
          link imports only the video it points at, and it can take a minute.</p>
        <label class="field"><span>Song name <em>(optional — overrides YouTube)</em></span>
          <input name="title" /></label>
        <label class="field"><span>Artist <em>(optional — overrides YouTube)</em></span>
          <input name="artist" /></label>
        <label class="field"><span>Album <em>(optional)</em></span>
          <input name="album" /></label>
        <label class="check-row"><input type="checkbox" name="explicit" value="true" /> <span>Explicit content</span></label>
        <button class="btn btn-primary" type="submit">Import</button>
        <span id="yt-status" class="upload-status"></span>
      </form>
    `;
    const form = ytMode.querySelector("#yt-form") as HTMLFormElement;
    const status = ytMode.querySelector("#yt-status") as HTMLElement;

    form.addEventListener("submit", async (e) => {
      e.preventDefault();
      const fd = new FormData(form);
      const link = String(fd.get("url") ?? "").trim();
      if (!link) return;
      const btn = form.querySelector("button")!;
      btn.textContent = "Importing…";
      btn.setAttribute("disabled", "true");
      status.textContent = "Downloading from YouTube — this can take a minute…";
      try {
        const song = await api.importSong({
          url: link,
          title: String(fd.get("title") ?? "").trim() || undefined,
          artist: String(fd.get("artist") ?? "").trim() || undefined,
          album: String(fd.get("album") ?? "").trim() || undefined,
          explicit: fd.get("explicit") === "true",
        });
        await loadSongs();
        if (state.view.kind === "library") renderMain();
        close();
        flash(`Added "${song.title}" by ${song.artist}.`);
      } catch (err) {
        status.textContent = `Import failed: ${(err as Error).message}`;
        btn.textContent = "Import";
        btn.removeAttribute("disabled");
      }
    });
  }

  // --- single file: prefilled, editable form ---
  function renderSingle(file: File): void {
    body.innerHTML = `
      <form id="upload" class="upload-card">
        <p class="dz-file">${escapeHtml(file.name)}</p>
        <label class="field"><span>Song name <em>(required)</em></span>
          <input name="title" required /></label>
        <label class="field"><span>Artist <em>(required)</em></span>
          <input name="artist" required /></label>
        <label class="field"><span>Album <em>(optional)</em></span>
          <input name="album" /></label>
        <label class="check-row"><input type="checkbox" name="explicit" value="true" /> <span>Explicit content</span></label>
        <div class="field"><span>Cover image <em>(optional)</em></span>
          <label class="file-chip"><i class="bi bi-image"></i>
            <span id="cover-name">Choose image</span>
            <input type="file" name="cover" accept="image/*" hidden /></label>
        </div>
        <button class="btn btn-primary" type="submit">Upload</button>
        <span id="upload-status" class="upload-status"></span>
      </form>
    `;
    const form = body.querySelector("#upload") as HTMLFormElement;
    const status = body.querySelector("#upload-status");
    const titleInput = form.querySelector('[name="title"]') as HTMLInputElement;
    const artistInput = form.querySelector('[name="artist"]') as HTMLInputElement;
    const albumInput = form.querySelector('[name="album"]') as HTMLInputElement;
    const coverInput = form.querySelector('[name="cover"]') as HTMLInputElement;
    const coverName = form.querySelector("#cover-name") as HTMLElement;

    void readTags(file).then((tags) => {
      if (tags.title) titleInput.value = tags.title;
      if (tags.artist) artistInput.value = tags.artist;
      if (tags.album) albumInput.value = tags.album;
    });
    coverInput.addEventListener("change", () => {
      coverName.textContent = coverInput.files?.[0]?.name ?? "Choose image";
    });

    form.addEventListener("submit", async (e) => {
      e.preventDefault();
      const fd = new FormData(form);
      fd.set("file", file);
      const btn = form.querySelector("button")!;
      btn.textContent = "Uploading...";
      btn.setAttribute("disabled", "true");
      try {
        const song = await api.uploadSong(fd);
        await loadSongs();
        if (state.view.kind === "library") renderMain();
        close();
        flash(`Added "${song.title}" by ${song.artist}.`);
      } catch (err) {
        if (status) status.textContent = `Upload failed: ${(err as Error).message}`;
        btn.textContent = "Upload";
        btn.removeAttribute("disabled");
      }
    });
  }

  // --- many files: resilient sequential bulk upload ---
  function renderBatch(files: File[]): void {
    body.innerHTML = `
      <label class="check-row"><input type="checkbox" id="b-explicit" /> <span>Mark all explicit</span></label>
      <ul class="batch" id="batch">
        ${files
          .map(
            (f, i) => `<li class="b-row"><span class="b-name">${escapeHtml(f.name)}</span>
              <span class="b-stat" id="b-${i}">queued</span></li>`,
          )
          .join("")}
      </ul>
      <button class="btn btn-primary" id="b-start">Upload ${files.length} songs</button>
      <span id="b-summary" class="upload-status"></span>
    `;
    const startBtn = body.querySelector("#b-start") as HTMLButtonElement;
    const summary = body.querySelector("#b-summary") as HTMLElement;

    startBtn.addEventListener("click", async () => {
      startBtn.disabled = true;
      const explicitAll = (body.querySelector("#b-explicit") as HTMLInputElement).checked;
      const CONCURRENCY = 10;
      let ok = 0;
      let fail = 0;
      let done = 0;
      let next = 0;
      const failed: string[] = [];

      const uploadOne = async (i: number) => {
        const file = files[i]!;
        const stat = body.querySelector(`#b-${i}`) as HTMLElement;
        stat.textContent = "uploading…";
        stat.className = "b-stat";

        const tags = await readTags(file).catch(() => ({}) as Awaited<ReturnType<typeof readTags>>);
        const fd = new FormData();
        fd.set("file", file);
        fd.set("title", tags.title ?? file.name.replace(/\.[^.]+$/, ""));
        fd.set("artist", tags.artist ?? "Unknown Artist");
        fd.set("album", ""); // bulk: leave album blank (tags are unreliable)
        if (explicitAll) fd.set("explicit", "true");

        const success = await uploadWithRetry(fd, 2);
        done++;
        if (success) {
          ok++;
          stat.textContent = "✓";
          stat.classList.add("ok");
        } else {
          fail++;
          failed.push(file.name);
          stat.textContent = "failed";
          stat.classList.add("err");
        }
        summary.textContent = `${ok} uploaded, ${fail} failed, ${files.length - done} left`;
      };

      // Worker pool: each worker pulls the next file as it finishes one, so up
      // to CONCURRENCY uploads run at a time (small files don't wait on big ones).
      const worker = async () => {
        while (next < files.length) {
          await uploadOne(next++);
        }
      };
      await Promise.all(
        Array.from({ length: Math.min(CONCURRENCY, files.length) }, worker),
      );

      await loadSongs();
      if (state.view.kind === "library") renderMain();
      summary.textContent = `Done — ${ok} uploaded${fail ? `, ${fail} failed` : ""}.`;
      if (failed.length) console.warn("Failed uploads:", failed);
    });
  }
}

// Upload one file, retrying transient NetworkErrors (not HTTP errors).
async function uploadWithRetry(fd: FormData, retries: number): Promise<boolean> {
  for (let attempt = 0; attempt <= retries; attempt++) {
    try {
      const res = await fetch("/api/songs", { method: "POST", body: fd });
      if (res.ok) return true;
      if (res.status === 429 && attempt < retries) {
        await sleep(2000); // rate limited: back off and retry
        continue;
      }
      return false; // other HTTP error (e.g. 400 missing tags) — skip
    } catch {
      if (attempt === retries) return false; // network error — give up
      await sleep(600 * (attempt + 1));
    }
  }
  return false;
}

function sleep(ms: number): Promise<void> {
  return new Promise((r) => setTimeout(r, ms));
}

// Admins fix metadata directly; everyone else signed in submits it as a
// request an admin approves (mirrors the artist-link request flow).
function openEditModal(song: Song): void {
  const asRequest = !state.me?.isAdmin;
  const overlay = document.createElement("div");
  overlay.className = "modal-overlay";
  overlay.innerHTML = `
    <div class="modal" role="dialog" aria-modal="true">
      <div class="modal-head">
        <h2>${asRequest ? "Suggest an edit" : "Edit details"}</h2>
        <button class="icon-btn" id="modal-close" title="Close"><i class="bi bi-x-lg"></i></button>
      </div>
      <form id="edit" class="upload-card">
        ${
          asRequest
            ? `<p class="dz-hint">Your changes go to an admin for review before they go live.</p>`
            : ""
        }
        <label class="field">
          <span>Song name <em>(required)</em></span>
          <input name="title" value="${escapeHtml(song.title)}" required />
        </label>
        <label class="field">
          <span>Artist <em>(required)</em></span>
          <input name="artist" value="${escapeHtml(song.artist)}" required />
        </label>
        <label class="field">
          <span>Album <em>(optional)</em></span>
          <input name="album" value="${escapeHtml(song.album ?? "")}" />
        </label>
        <label class="check-row">
          <input type="checkbox" name="explicit" value="true" ${song.explicit ? "checked" : ""} />
          <span>Explicit content</span>
        </label>
        ${
          asRequest
            ? ""
            : `<div class="field">
          <span>Replace cover <em>(optional)</em></span>
          <label class="file-chip">
            <i class="bi bi-image"></i>
            <span id="cover-name">Choose image</span>
            <input type="file" name="cover" accept="image/*" hidden />
          </label>
        </div>`
        }
        <button class="btn btn-primary" type="submit">${asRequest ? "Send for review" : "Save"}</button>
        <span id="edit-status" class="upload-status"></span>
      </form>
    </div>
  `;
  document.body.appendChild(overlay);

  const close = () => {
    overlay.remove();
    document.removeEventListener("keydown", onEsc);
  };
  const onEsc = (e: KeyboardEvent) => {
    if (e.key === "Escape") close();
  };
  document.addEventListener("keydown", onEsc);
  overlay.addEventListener("click", (e) => {
    if (e.target === overlay) close();
  });
  overlay.querySelector("#modal-close")?.addEventListener("click", close);

  const coverInput = overlay.querySelector('input[name="cover"]') as HTMLInputElement | null;
  const coverName = overlay.querySelector("#cover-name") as HTMLElement | null;
  coverInput?.addEventListener("change", () => {
    if (coverName) coverName.textContent = coverInput.files?.[0]?.name ?? "Choose image";
  });

  // Ensure explicit is always sent (unchecked box would otherwise be omitted).
  const form = overlay.querySelector("#edit") as HTMLFormElement;
  const status = overlay.querySelector("#edit-status");
  form.addEventListener("submit", async (e) => {
    e.preventDefault();
    const btn = form.querySelector("button")!;
    const explicit = Boolean(form.querySelector<HTMLInputElement>('[name="explicit"]')?.checked);
    const title = (form.querySelector('[name="title"]') as HTMLInputElement).value.trim();
    const artist = (form.querySelector('[name="artist"]') as HTMLInputElement).value.trim();
    const album = (form.querySelector('[name="album"]') as HTMLInputElement).value.trim();
    btn.textContent = asRequest ? "Sending..." : "Saving...";
    btn.setAttribute("disabled", "true");
    if (status) status.textContent = "";
    try {
      if (asRequest) {
        const res = await api.requestSongEdit(song.id, { title, artist, album, explicit });
        if (!res.ok) throw new Error(String(res.status));
        close();
        flash("Edit sent for review.");
      } else {
        const fd = new FormData(form);
        fd.set("explicit", explicit ? "true" : "false");
        const updated = await api.updateSong(song.id, fd);
        await Promise.all([loadSongs(), loadLiked()]);
        renderMain();
        close();
        flash(`Updated "${updated.title}".`);
      }
    } catch (err) {
      if (status) status.textContent = `${asRequest ? "Send" : "Save"} failed: ${(err as Error).message}`;
      btn.textContent = asRequest ? "Send for review" : "Save";
      btn.removeAttribute("disabled");
    }
  });
}

// Small transient toast.
function flash(msg: string): void {
  const el = document.createElement("div");
  el.className = "toast";
  el.textContent = msg;
  document.body.appendChild(el);
  setTimeout(() => el.remove(), 3000);
}

// --- shared song actions ------------------------------------------------

// The public, hotlinkable URL for a song. Opening it cold hits the server's
// /song/:id route (unfurl metadata), which then boots the SPA into the song
// view; appending ?download=1 downloads the original master. See server.ts.
function songLink(id: string): string {
  return `${location.origin}/song/${id}`;
}

// Multi-artist tracks store a comma-separated string ("A, B, C"). The player
// bar is tight, so it shows only the first (primary) name.
function primaryArtist(name: string): string {
  return name.split(",")[0]!.trim() || name;
}

function copySongLink(id: string): void {
  const link = songLink(id);
  const done = () => flash("Link copied");
  if (navigator.clipboard?.writeText) {
    navigator.clipboard.writeText(link).then(done, () => fallbackCopy(link, done));
  } else {
    fallbackCopy(link, done);
  }
}

function fallbackCopy(text: string, done: () => void): void {
  const ta = document.createElement("textarea");
  ta.value = text;
  ta.style.position = "fixed";
  ta.style.opacity = "0";
  document.body.appendChild(ta);
  ta.select();
  try {
    document.execCommand("copy");
    done();
  } catch {
    flash("Couldn't copy — here's the link: " + text);
  }
  ta.remove();
}

// Optimistic like toggle: flip local state + every heart button for this song
// in the DOM, then persist. Reverts on failure.
async function toggleLike(songId: string): Promise<void> {
  if (!state.me) {
    flash("Sign in to like songs.");
    return;
  }
  const nowLiked = !state.likedIds.has(songId);
  if (nowLiked) state.likedIds.add(songId);
  else state.likedIds.delete(songId);
  paintLikeButtons(songId, nowLiked);

  const song = state.songs.find((s) => s.id === songId) ?? state.likedSongs.find((s) => s.id === songId);
  if (nowLiked && song && !state.likedSongs.some((s) => s.id === songId)) {
    state.likedSongs = [song, ...state.likedSongs];
  } else if (!nowLiked) {
    state.likedSongs = state.likedSongs.filter((s) => s.id !== songId);
  }

  try {
    const res = nowLiked ? await api.likeSong(songId) : await api.unlikeSong(songId);
    if (!res.ok) throw new Error(String(res.status));
  } catch {
    // Revert.
    if (nowLiked) state.likedIds.delete(songId);
    else state.likedIds.add(songId);
    paintLikeButtons(songId, !nowLiked);
    await loadLiked();
    flash("Couldn't update Liked Songs.");
  }
  if (state.view.kind === "liked") renderMain();
}

function paintLikeButtons(songId: string, liked: boolean): void {
  document.querySelectorAll<HTMLElement>(`[data-like="${CSS.escape(songId)}"]`).forEach((btn) => {
    btn.classList.toggle("liked", liked);
    btn.setAttribute("title", liked ? "Remove from Liked Songs" : "Add to Liked Songs");
    const i = btn.querySelector("i");
    if (i) i.className = liked ? "bi bi-heart-fill" : "bi bi-heart";
  });
}

type MenuItem = {
  label: string;
  icon: string;
  danger?: boolean;
  onClick: () => void;
};

// Lightweight popup menu anchored to a button. Closes on outside click, Escape,
// scroll, or resize. There's no menu primitive in the app otherwise — the "⋯"
// on every song row and the player bar both use this.
function openContextMenu(anchor: HTMLElement, items: MenuItem[]): void {
  document.querySelector(".ctx-menu")?.remove();

  const menu = document.createElement("div");
  menu.className = "ctx-menu";
  menu.innerHTML = items
    .map(
      (it, i) =>
        `<button class="ctx-item ${it.danger ? "danger" : ""}" data-i="${i}">
          <i class="bi ${it.icon}"></i> ${escapeHtml(it.label)}
        </button>`,
    )
    .join("");
  document.body.appendChild(menu);

  const r = anchor.getBoundingClientRect();
  const mw = menu.offsetWidth;
  const mh = menu.offsetHeight;
  let left = Math.min(r.right - mw, window.innerWidth - mw - 8);
  let top = r.bottom + 4;
  if (top + mh > window.innerHeight - 8) top = r.top - mh - 4;
  menu.style.left = `${Math.max(8, left)}px`;
  menu.style.top = `${Math.max(8, top)}px`;

  const close = () => {
    menu.remove();
    document.removeEventListener("pointerdown", onOutside, true);
    document.removeEventListener("keydown", onKey, true);
    window.removeEventListener("scroll", close, true);
    window.removeEventListener("resize", close);
  };
  const onOutside = (e: Event) => {
    if (!menu.contains(e.target as Node)) close();
  };
  const onKey = (e: KeyboardEvent) => {
    if (e.key === "Escape") close();
  };
  document.addEventListener("pointerdown", onOutside, true);
  document.addEventListener("keydown", onKey, true);
  window.addEventListener("scroll", close, true);
  window.addEventListener("resize", close);

  menu.querySelectorAll<HTMLElement>(".ctx-item").forEach((btn) => {
    btn.addEventListener("click", () => {
      const it = items[Number(btn.dataset.i)];
      close();
      it?.onClick();
    });
  });
}

// --- browse shared playlists ----------------------------------------------

async function renderBrowse(el: HTMLElement): Promise<void> {
  el.innerHTML = `
    <header class="main-head">
      <h2>Browse shared</h2>
      <input id="pl-search" class="search" placeholder="Search playlists or owners" />
    </header>
    <div id="pl-results"></div>
  `;

  const results = document.getElementById("pl-results")!;
  const draw = async (q?: string) => {
    const rows = await api.searchPublicPlaylists(q);
    results.innerHTML = rows.length
      ? `<div class="pl-grid">${rows
          .map(
            (p) => `
        <button class="pl-card" data-open="${p.id}">
          ${avatarHtml(p.ownerAvatar, p.ownerName)}
          <div class="pl-card-meta">
            <span class="pl-card-name">${escapeHtml(p.name)}</span>
            <span class="pl-card-sub">${escapeHtml(
              p.ownerName ?? "unknown",
            )} · ${p.songCount} song${p.songCount === 1 ? "" : "s"}</span>
          </div>
        </button>`,
          )
          .join("")}</div>`
      : `<p class="empty">No shared playlists found.</p>`;

    results.querySelectorAll("[data-open]").forEach((b) => {
      b.addEventListener("click", () => {
        navigate({ kind: "playlist", id: (b as HTMLElement).dataset.open! });
      });
    });
  };

  await draw();
  const search = document.getElementById("pl-search") as HTMLInputElement | null;
  search?.addEventListener("input", () => draw(search.value.trim() || undefined));
}

// --- artists -----------------------------------------------------------

async function renderArtists(el: HTMLElement): Promise<void> {
  el.innerHTML = `
    <header class="main-head">
      <h2>Artists</h2>
      <div class="head-actions">
        <input id="artist-search" class="search" placeholder="Search artists" />
        ${state.me ? `<button class="btn btn-primary" id="new-artist-btn">+ New artist</button>` : ""}
      </div>
    </header>
    <div id="artist-results"></div>
  `;

  const results = document.getElementById("artist-results")!;
  const draw = async (q?: string) => {
    const rows = await api.searchArtists(q);
    results.innerHTML = rows.length
      ? `<div class="pl-grid">${rows
          .map(
            (a) => `
        <button class="pl-card" data-open="${a.id}">
          ${avatarHtml(a.avatarUrl, a.name)}
          <div class="pl-card-meta">
            <span class="pl-card-name">${escapeHtml(a.name)}</span>
            <span class="pl-card-sub">${a.songCount} song${a.songCount === 1 ? "" : "s"}</span>
          </div>
        </button>`,
          )
          .join("")}</div>`
      : `<p class="empty">${q ? "No artists match that search." : "No artist pages yet."}</p>`;

    results.querySelectorAll("[data-open]").forEach((b) => {
      b.addEventListener("click", () => {
        navigate({ kind: "artist", id: (b as HTMLElement).dataset.open! });
      });
    });
  };

  await draw();
  const search = document.getElementById("artist-search") as HTMLInputElement | null;
  search?.addEventListener("input", () => draw(search.value.trim() || undefined));

  document.getElementById("new-artist-btn")?.addEventListener("click", () => {
    openArtistCreateModal();
  });
}

async function renderArtistView(el: HTMLElement, id: string): Promise<void> {
  const artist = await api.getArtist(id);
  const shownSongs = filterExplicit(artist.songs);
  state.visibleSongs = shownSongs;

  const editBtn = state.me?.isAdmin
    ? `<button class="icon-btn" id="edit-artist" title="Edit artist"><i class="bi bi-pencil-fill"></i></button>`
    : "";

  el.innerHTML = `
    <header class="main-head">
      <div class="main-head-title">
        ${avatarHtml(artist.avatarUrl, artist.name, "lg")}
        <h2>${escapeHtml(artist.name)}</h2>
      </div>
      <div class="head-actions">${editBtn}</div>
    </header>
    ${artist.bio ? `<p class="artist-bio">${escapeHtml(artist.bio)}</p>` : ""}
    <div id="songlist">${songTableHtml(shownSongs)}</div>
  `;

  wireSongList();

  document.getElementById("edit-artist")?.addEventListener("click", () => {
    openArtistEditModal(artist);
  });
}

// Shared "go to this artist's page" navigation, used by clickable artist
// names in song rows, the player bar, and the lyrics overlay.
function openArtist(id: string): void {
  if (lyricsState.open) toggleLyrics();
  navigate({ kind: "artist", id });
}

// Create a brand-new artist page. Shared by the "Artists" browse page and
// the "link song to artist" flow's "can't find it? create new" fallback —
// `onCreated` fires either with the newly-created artist, or with the
// existing one if the name turned out to already have a page.
function openArtistCreateModal(
  prefill = "",
  onCreated?: (artist: Artist) => void,
): void {
  const overlay = document.createElement("div");
  overlay.className = "modal-overlay";
  overlay.innerHTML = `
    <div class="modal" role="dialog" aria-modal="true">
      <div class="modal-head">
        <h2>New artist page</h2>
        <button class="icon-btn" id="modal-close" title="Close"><i class="bi bi-x-lg"></i></button>
      </div>
      <form id="artist-form" class="upload-card">
        <label class="field"><span>Artist name <em>(required)</em></span>
          <input name="name" value="${escapeHtml(prefill)}" required /></label>
        <label class="field"><span>Bio <em>(optional)</em></span>
          <input name="bio" /></label>
        <button class="btn btn-primary" type="submit">Create page</button>
        <span id="artist-status" class="upload-status"></span>
      </form>
    </div>
  `;
  document.body.appendChild(overlay);

  const close = () => {
    overlay.remove();
    document.removeEventListener("keydown", onEsc);
  };
  const onEsc = (e: KeyboardEvent) => {
    if (e.key === "Escape") close();
  };
  document.addEventListener("keydown", onEsc);
  overlay.addEventListener("click", (e) => {
    if (e.target === overlay) close();
  });
  overlay.querySelector("#modal-close")?.addEventListener("click", close);

  const form = overlay.querySelector("#artist-form") as HTMLFormElement;
  const status = overlay.querySelector("#artist-status") as HTMLElement;

  form.addEventListener("submit", async (e) => {
    e.preventDefault();
    const name = (form.querySelector('[name="name"]') as HTMLInputElement).value.trim();
    const bio = (form.querySelector('[name="bio"]') as HTMLInputElement).value.trim();
    const btn = form.querySelector("button")!;
    btn.textContent = "Creating...";
    btn.setAttribute("disabled", "true");

    const result = await api.createArtist(name, bio || undefined);
    if (result.ok) {
      close();
      flash(`Created artist page for "${result.artist.name}".`);
      onCreated?.(result.artist);
      if (state.view.kind === "artists") renderMain();
      return;
    }

    if (result.status === 409 && result.existing) {
      const existing = result.existing;
      status.innerHTML = `Already exists — <button type="button" class="link" id="use-existing">use "${escapeHtml(existing.name)}" instead</button>`;
      status.querySelector("#use-existing")?.addEventListener("click", () => {
        close();
        onCreated?.(existing);
      });
    } else {
      status.textContent = "Something went wrong. Try again.";
    }
    btn.textContent = "Create page";
    btn.removeAttribute("disabled");
  });
}

function openArtistEditModal(artist: ArtistDetail): void {
  const overlay = document.createElement("div");
  overlay.className = "modal-overlay";
  overlay.innerHTML = `
    <div class="modal" role="dialog" aria-modal="true">
      <div class="modal-head">
        <h2>Edit artist</h2>
        <button class="icon-btn" id="modal-close" title="Close"><i class="bi bi-x-lg"></i></button>
      </div>
      <div class="avatar-upload">
        ${avatarHtml(artist.avatarUrl, artist.name, "lg")}
        <label class="btn" for="artist-avatar-input">Change photo</label>
        <input type="file" id="artist-avatar-input" accept="image/*" hidden />
        <span id="artist-avatar-status" class="upload-status"></span>
      </div>
      <form id="artist-edit" class="upload-card">
        <label class="field"><span>Name <em>(required)</em></span>
          <input name="name" value="${escapeHtml(artist.name)}" required /></label>
        <label class="field"><span>Bio <em>(optional)</em></span>
          <input name="bio" value="${escapeHtml(artist.bio ?? "")}" /></label>
        <button class="btn btn-primary" type="submit">Save</button>
        <span id="artist-edit-status" class="upload-status"></span>
      </form>
    </div>
  `;
  document.body.appendChild(overlay);

  const close = () => {
    overlay.remove();
    document.removeEventListener("keydown", onEsc);
  };
  const onEsc = (e: KeyboardEvent) => {
    if (e.key === "Escape") close();
  };
  document.addEventListener("keydown", onEsc);
  overlay.addEventListener("click", (e) => {
    if (e.target === overlay) close();
  });
  overlay.querySelector("#modal-close")?.addEventListener("click", close);

  const avatarInput = overlay.querySelector("#artist-avatar-input") as HTMLInputElement;
  const avatarStatus = overlay.querySelector("#artist-avatar-status") as HTMLElement;
  avatarInput.addEventListener("change", async () => {
    const file = avatarInput.files?.[0];
    if (!file) return;
    avatarStatus.textContent = "Uploading...";
    try {
      const updated = await api.uploadArtistAvatar(artist.id, file);
      artist.avatarUrl = updated.avatarUrl;
      const preview = overlay.querySelector(".avatar-upload .avatar") as HTMLElement | null;
      if (preview) preview.outerHTML = avatarHtml(updated.avatarUrl, artist.name, "lg");
      avatarStatus.textContent = "";
      if (state.view.kind === "artist") renderMain();
    } catch (err) {
      avatarStatus.textContent = `Upload failed: ${(err as Error).message}`;
    }
  });

  const form = overlay.querySelector("#artist-edit") as HTMLFormElement;
  const status = overlay.querySelector("#artist-edit-status") as HTMLElement;

  form.addEventListener("submit", async (e) => {
    e.preventDefault();
    const name = (form.querySelector('[name="name"]') as HTMLInputElement).value.trim();
    const bio = (form.querySelector('[name="bio"]') as HTMLInputElement).value.trim();
    const btn = form.querySelector("button")!;
    btn.textContent = "Saving...";
    btn.setAttribute("disabled", "true");
    try {
      await api.updateArtist(artist.id, { name, bio });
      close();
      flash(`Updated "${name}".`);
      if (state.view.kind === "artist") renderMain();
    } catch (err) {
      status.textContent = `Save failed: ${(err as Error).message}`;
      btn.textContent = "Save";
      btn.removeAttribute("disabled");
    }
  });
}

// Search-first "link this song to an artist" flow: search existing pages
// (fuzzy-ranked server-side) before offering to create a new one, so near-
// duplicate artist pages don't pile up.
function openLinkArtistModal(songId: string, songTitle: string): void {
  const overlay = document.createElement("div");
  overlay.className = "modal-overlay";
  overlay.innerHTML = `
    <div class="modal" role="dialog" aria-modal="true">
      <div class="modal-head">
        <h2>Link artist</h2>
        <button class="icon-btn" id="modal-close" title="Close"><i class="bi bi-x-lg"></i></button>
      </div>
      <p class="dz-file">${escapeHtml(songTitle)}</p>
      <input id="artist-link-search" class="search" placeholder="Search artists…" autofocus />
      <ul class="batch" id="artist-link-results"></ul>
      <button class="btn" id="create-new-artist" type="button">+ Create new artist</button>
    </div>
  `;
  document.body.appendChild(overlay);

  const close = () => {
    overlay.remove();
    document.removeEventListener("keydown", onEsc);
  };
  const onEsc = (e: KeyboardEvent) => {
    if (e.key === "Escape") close();
  };
  document.addEventListener("keydown", onEsc);
  overlay.addEventListener("click", (e) => {
    if (e.target === overlay) close();
  });
  overlay.querySelector("#modal-close")?.addEventListener("click", close);

  const search = overlay.querySelector("#artist-link-search") as HTMLInputElement;
  const results = overlay.querySelector("#artist-link-results") as HTMLElement;

  const requestLink = async (artist: Artist) => {
    const res = await api.requestArtistLink(artist.id, songId);
    close();
    flash(
      res.ok
        ? `Requested linking "${songTitle}" to ${artist.name} — an admin will review it.`
        : `"${songTitle}" is already linked (or already has a pending request) for ${artist.name}.`,
    );
  };

  const draw = async (q?: string) => {
    const rows = q ? await api.searchArtists(q) : [];
    results.innerHTML = rows.length
      ? rows
          .map(
            (a) => `
        <li class="b-row">
          <span class="b-name">${escapeHtml(a.name)} <span class="b-stat">${a.songCount} song${a.songCount === 1 ? "" : "s"}</span></span>
          <button class="btn" data-pick="${a.id}">Link</button>
        </li>`,
          )
          .join("")
      : q
        ? `<li class="b-row"><span class="b-name">No matches — create a new page instead?</span></li>`
        : "";

    results.querySelectorAll("[data-pick]").forEach((btn) => {
      btn.addEventListener("click", () => {
        const artist = rows.find((a) => a.id === (btn as HTMLElement).dataset.pick);
        if (artist) void requestLink(artist);
      });
    });
  };

  search.addEventListener("input", () => draw(search.value.trim() || undefined));

  overlay.querySelector("#create-new-artist")?.addEventListener("click", () => {
    close();
    openArtistCreateModal(search.value.trim(), (artist) => {
      void requestLink(artist);
    });
  });
}

// --- admin: duplicate review ------------------------------------------

async function renderDuplicates(el: HTMLElement): Promise<void> {
  const rows = await api.listDuplicates("pending");
  state.adminCounts.duplicates = rows.length;

  el.innerHTML = `
    <header class="main-head"><h2>Duplicate review</h2></header>
    <div id="dupe-list">${
      rows.length
        ? rows.map(dupeRowHtml).join("")
        : `<p class="empty">No possible duplicates right now.</p>`
    }</div>
  `;
  wireDuplicateActions();
}

function dupeSideHtml(s: DuplicateReview["newSong"]): string {
  return `
    <div class="dupe-side">
      <span class="song-title">${escapeHtml(s.title)}</span>
      <span class="song-artist">${escapeHtml(s.artist)}</span>
      ${s.album ? `<span class="song-dur">${escapeHtml(s.album)}</span>` : ""}
      ${s.durationS ? `<span class="song-dur">${formatTime(s.durationS)}</span>` : ""}
    </div>`;
}

function dupeRowHtml(r: DuplicateReview): string {
  return `
    <div class="dupe-row" data-review="${r.id}">
      <div class="dupe-score">${r.score}% match</div>
      <div class="dupe-compare">
        ${dupeSideHtml(r.newSong)}
        <i class="bi bi-arrow-left-right"></i>
        ${dupeSideHtml(r.existingSong)}
      </div>
      <ul class="dupe-reasons">${r.reasons.map((rn) => `<li>${escapeHtml(rn)}</li>`).join("")}</ul>
      <div class="dupe-actions">
        <button class="btn" data-different="${r.id}">Not a duplicate</button>
        <button class="btn btn-primary" data-duplicate="${r.id}">Mark duplicate</button>
      </div>
    </div>`;
}

function wireDuplicateActions(): void {
  document.querySelectorAll("[data-duplicate]").forEach((btn) => {
    btn.addEventListener("click", async () => {
      await api.decideDuplicate((btn as HTMLElement).dataset.duplicate!, "duplicate");
      await refreshAdminCounts();
      renderSidebar();
      renderMain();
    });
  });
  document.querySelectorAll("[data-different]").forEach((btn) => {
    btn.addEventListener("click", async () => {
      await api.decideDuplicate((btn as HTMLElement).dataset.different!, "different");
      await refreshAdminCounts();
      renderSidebar();
      renderMain();
    });
  });
}

// --- admin: artist link requests ----------------------------------------

async function renderLinkRequests(el: HTMLElement): Promise<void> {
  const rows = await api.listLinkRequests("pending");
  state.adminCounts.linkRequests = rows.length;

  el.innerHTML = `
    <header class="main-head"><h2>Artist link requests</h2></header>
    <div id="req-list">${
      rows.length
        ? `<ul class="batch">${rows.map(reqRowHtml).join("")}</ul>`
        : `<p class="empty">No pending requests.</p>`
    }</div>
  `;
  wireLinkRequestActions();
}

function reqRowHtml(r: LinkRequest): string {
  return `
    <li class="b-row req-row">
      <span class="b-name">
        “${escapeHtml(r.songTitle)}” by ${escapeHtml(r.songArtist)} → <strong>${escapeHtml(r.artistName)}</strong>
        ${r.requestedByName ? `<span class="req-by">requested by ${escapeHtml(r.requestedByName)}</span>` : ""}
      </span>
      <div class="dupe-actions">
        <button class="btn" data-reject="${r.id}">Reject</button>
        <button class="btn btn-primary" data-approve="${r.id}">Approve</button>
      </div>
    </li>`;
}

function wireLinkRequestActions(): void {
  document.querySelectorAll("[data-approve]").forEach((btn) => {
    btn.addEventListener("click", async () => {
      await api.decideLinkRequest((btn as HTMLElement).dataset.approve!, "approve");
      await Promise.all([refreshAdminCounts(), loadSongs()]);
      renderSidebar();
      renderMain();
    });
  });
  document.querySelectorAll("[data-reject]").forEach((btn) => {
    btn.addEventListener("click", async () => {
      await api.decideLinkRequest((btn as HTMLElement).dataset.reject!, "reject");
      await refreshAdminCounts();
      renderSidebar();
      renderMain();
    });
  });
}

// --- admin: pending uploads -------------------------------------------

async function renderPendingSongs(el: HTMLElement): Promise<void> {
  const rows = await api.listPendingSongs();
  state.adminCounts.pendingSongs = rows.length;
  state.visibleSongs = rows; // so the preview play buttons queue correctly

  el.innerHTML = `
    <header class="main-head"><h2>Upload approvals</h2></header>
    <div id="pending-list">${
      rows.length
        ? `<div class="songs">${rows.map(pendingRowHtml).join("")}</div>`
        : `<p class="empty">No uploads waiting for review.</p>`
    }</div>
  `;

  // Reuse the song-row play wiring for the cover buttons.
  el.querySelectorAll(".cover-play").forEach((btn) => {
    btn.addEventListener("click", () => playFromList(Number((btn as HTMLElement).dataset.play)));
  });
  el.querySelectorAll("[data-approve]").forEach((btn) => {
    btn.addEventListener("click", async () => {
      await api.decidePendingSong((btn as HTMLElement).dataset.approve!, "approve");
      await Promise.all([refreshAdminCounts(), loadSongs()]);
      renderSidebar();
      renderMain();
    });
  });
  el.querySelectorAll("[data-reject]").forEach((btn) => {
    btn.addEventListener("click", async () => {
      if (!confirm("Reject and delete this upload?")) return;
      await api.decidePendingSong((btn as HTMLElement).dataset.reject!, "reject");
      await refreshAdminCounts();
      renderSidebar();
      renderMain();
    });
  });
  highlightPlayingRow();
}

function pendingRowHtml(s: PendingSong, i: number): string {
  const art = s.coverUrl
    ? `<img class="cover" src="${s.coverUrl}" alt="" />`
    : `<div class="cover cover-empty"><i class="bi bi-music-note-beamed"></i></div>`;
  return `
    <div class="song" data-index="${i}" data-song-id="${s.id}">
      <div class="song-cover">
        ${art}
        <div class="np-bars" aria-hidden="true"><span></span><span></span><span></span><span></span></div>
        <button class="cover-play" data-play="${i}" title="Preview"><i class="bi bi-play-fill"></i></button>
      </div>
      <div class="song-meta">
        <span class="song-title">${escapeHtml(s.title)}${
          s.explicit ? ` <span class="tag-e" title="Explicit">E</span>` : ""
        }</span>
        <span class="song-artist">${escapeHtml(s.artist)}${
          s.album ? ` · ${escapeHtml(s.album)}` : ""
        }${s.uploaderName ? ` · uploaded by ${escapeHtml(s.uploaderName)}` : ""}</span>
      </div>
      <span class="song-dur">${s.durationS ? formatTime(s.durationS) : ""}</span>
      <div class="dupe-actions">
        <button class="btn" data-reject="${s.id}">Reject</button>
        <button class="btn btn-primary" data-approve="${s.id}">Approve</button>
      </div>
    </div>`;
}

// --- admin: song metadata edit requests -------------------------------

async function renderEditRequests(el: HTMLElement): Promise<void> {
  const rows = await api.listSongEditRequests("pending");
  state.adminCounts.editRequests = rows.length;

  el.innerHTML = `
    <header class="main-head"><h2>Edit requests</h2></header>
    <div id="edit-req-list">${
      rows.length
        ? rows.map(editReqRowHtml).join("")
        : `<p class="empty">No pending edit requests.</p>`
    }</div>
  `;

  el.querySelectorAll("[data-approve]").forEach((btn) => {
    btn.addEventListener("click", async () => {
      await api.decideSongEditRequest((btn as HTMLElement).dataset.approve!, "approve");
      await Promise.all([refreshAdminCounts(), loadSongs()]);
      renderSidebar();
      renderMain();
    });
  });
  el.querySelectorAll("[data-reject]").forEach((btn) => {
    btn.addEventListener("click", async () => {
      await api.decideSongEditRequest((btn as HTMLElement).dataset.reject!, "reject");
      await refreshAdminCounts();
      renderSidebar();
      renderMain();
    });
  });
}

function editReqRowHtml(r: SongEditRequest): string {
  const field = (label: string, cur: unknown, next: unknown) => {
    const changed = next !== null && next !== undefined && String(next) !== String(cur);
    return `<li${changed ? ' class="changed"' : ""}>
      <span class="er-label">${label}</span>
      <span class="er-cur">${escapeHtml(String(cur ?? "—"))}</span>
      ${changed ? `<i class="bi bi-arrow-right"></i> <span class="er-next">${escapeHtml(String(next))}</span>` : ""}
    </li>`;
  };
  return `
    <div class="dupe-row" data-req="${r.id}">
      <div class="dupe-score">Edit request${
        r.requestedByName ? ` · ${escapeHtml(r.requestedByName)}` : ""
      }</div>
      <ul class="er-fields">
        ${field("Title", r.current.title, r.proposed.title)}
        ${field("Artist", r.current.artist, r.proposed.artist)}
        ${field("Album", r.current.album, r.proposed.album)}
        ${field("Explicit", r.current.explicit, r.proposed.explicit)}
      </ul>
      <div class="dupe-actions">
        <button class="btn" data-reject="${r.id}">Reject</button>
        <button class="btn btn-primary" data-approve="${r.id}">Approve</button>
      </div>
    </div>`;
}

// --- playlist view ---------------------------------------------------------

async function renderPlaylistView(el: HTMLElement, id: string): Promise<void> {
  const pl = await api.getPlaylist(id);
  const shownSongs = filterExplicit(pl.songs);
  state.visibleSongs = shownSongs;

  const ownerControls = pl.isOwner
    ? `
      <label class="share-toggle">
        <input type="checkbox" id="share" ${pl.isPublic ? "checked" : ""} />
        Shared
      </label>
      <button class="btn" id="del-playlist">Delete</button>`
    : `<span class="owner-tag">${avatarHtml(
        pl.ownerAvatar,
        pl.ownerName,
        "sm",
      )} ${escapeHtml(pl.ownerName ?? "unknown")}</span>`;

  const coverHtml = pl.coverUrl
    ? `<img class="playlist-cover" src="${pl.coverUrl}" alt="" />`
    : `<div class="playlist-cover playlist-cover-empty"><i class="bi bi-music-note-list"></i></div>`;

  const coverControls = pl.isOwner
    ? `<label class="btn btn-sm" for="playlist-cover-input">Change cover</label>
       <input type="file" id="playlist-cover-input" accept="image/*" hidden />`
    : "";

  el.innerHTML = `
    <header class="main-head">
      <div class="main-head-title">
        <div class="playlist-head-cover">
          ${coverHtml}
          ${coverControls}
          <span id="playlist-cover-status" class="upload-status"></span>
        </div>
        <h2>${escapeHtml(pl.name)} ${
          pl.isPublic ? `<span class="pl-badge">shared</span>` : ""
        }</h2>
      </div>
      <div class="head-actions">${ownerControls}</div>
    </header>
    <div id="songlist">${songTableHtml(shownSongs, pl.isOwner ? id : undefined)}</div>
  `;

  wireSongList(pl.isOwner ? id : undefined);

  document.getElementById("share")?.addEventListener("change", async (e) => {
    const on = (e.target as HTMLInputElement).checked;
    await api.updatePlaylist(id, { isPublic: on });
    await loadPlaylists();
    render();
  });

  document.getElementById("del-playlist")?.addEventListener("click", async () => {
    if (!confirm(`Delete "${pl.name}"?`)) return;
    await api.deletePlaylist(id);
    await loadPlaylists();
    navigate({ kind: "library" });
  });

  const coverInput = document.getElementById("playlist-cover-input") as HTMLInputElement | null;
  coverInput?.addEventListener("change", async () => {
    const file = coverInput.files?.[0];
    if (!file) return;
    const status = document.getElementById("playlist-cover-status");
    if (status) status.textContent = "Uploading...";
    try {
      await api.uploadPlaylistCover(id, file);
      await loadPlaylists();
      render();
    } catch (err) {
      if (status) status.textContent = `Upload failed: ${(err as Error).message}`;
    }
  });
}

// --- settings ----------------------------------------------------------

async function renderSettings(el: HTMLElement): Promise<void> {
  const status = await api.lastfmStatus();

  el.innerHTML = `
    <header class="main-head">
      <h2>Settings</h2>
    </header>

    <section class="settings-section">
      <h3>Last.fm</h3>
      ${
        !status.configured
          ? `<p class="empty">Last.fm scrobbling isn't set up on this server yet.</p>`
          : status.connected
            ? `
              <p>Connected as <strong>${escapeHtml(status.username ?? "")}</strong>.
                 Plays get scrobbled automatically.</p>
              <button class="btn" id="lastfm-disconnect">Disconnect</button>
            `
            : `
              <p>Connect your Last.fm account to scrobble what you play here.</p>
              ${
                isDesktop
                  ? `<button class="btn btn-primary" id="lastfm-connect">Connect Last.fm</button>`
                  : `<a class="btn btn-primary" href="${api.lastfmConnectUrl()}">Connect Last.fm</a>`
              }
            `
      }
      <span id="lastfm-status" class="upload-status"></span>
    </section>
  `;

  // Desktop: the connect flow runs in the system browser and returns via the
  // doughmination://lastfm/callback deep link (see desktop.ts).
  document.getElementById("lastfm-connect")?.addEventListener("click", () => {
    const s = document.getElementById("lastfm-status");
    if (s) s.textContent = "Opening Last.fm in your browser…";
    onLastfmConnect((r) => {
      if (r.ok) {
        flash("Last.fm connected.");
        if (state.view.kind === "settings") void renderSettings(el);
      } else {
        flash("Couldn't connect Last.fm. Try again?");
        if (s) s.textContent = "";
      }
    });
    if (!startDesktopLastfmConnect(api.lastfmConnectUrl(true)) && s) {
      s.textContent = "Couldn't open the browser.";
    }
  });

  document.getElementById("lastfm-disconnect")?.addEventListener("click", async () => {
    const s = document.getElementById("lastfm-status");
    try {
      await api.lastfmDisconnect();
      await renderSettings(el);
    } catch (err) {
      if (s) s.textContent = `Something went wrong: ${(err as Error).message}`;
    }
  });
}

// --- song list -------------------------------------------------------------

function songTableHtml(songs: Song[], editablePlaylistId?: string): string {
  if (songs.length === 0) return `<p class="empty">No songs yet.</p>`;

  const rows = songs
    .map((s, i) => {
      const art = s.coverUrl
        ? `<img class="cover" src="${s.coverUrl}" alt="" />`
        : `<div class="cover cover-empty"><i class="bi bi-music-note-beamed"></i></div>`;

      // Spotify-style: cover doubles as the play button on hover. The animated
      // bars overlay marks the currently-playing row (toggled via CSS classes
      // in highlightPlayingRow()).
      const cover = `
        <div class="song-cover">
          ${art}
          <div class="np-bars" aria-hidden="true"><span></span><span></span><span></span><span></span></div>
          <button class="cover-play" data-play="${i}" title="Play">
            <i class="bi bi-play-fill"></i>
          </button>
        </div>`;

      // Only the heart and the +/× stay on the row; everything else moves into
      // the "⋯" menu wired up in wireSongList().
      const heart = state.me
        ? `<button class="icon-btn like ${state.likedIds.has(s.id) ? "liked" : ""}" data-like="${s.id}"
             title="${state.likedIds.has(s.id) ? "Remove from Liked Songs" : "Add to Liked Songs"}">
             <i class="bi bi-heart${state.likedIds.has(s.id) ? "-fill" : ""}"></i></button>`
        : "";
      const plus = !state.me
        ? ""
        : editablePlaylistId
          ? `<button class="icon-btn" data-remove="${s.id}" title="Remove"><i class="bi bi-x-lg"></i></button>`
          : `<button class="icon-btn" data-add="${s.id}" title="Add to playlist"><i class="bi bi-plus-lg"></i></button>`;
      const menu = `<button class="icon-btn" data-menu="${s.id}" title="More"><i class="bi bi-three-dots-vertical"></i></button>`;

      const badge = s.explicit
        ? `<span class="tag-e" title="Explicit">E</span>`
        : "";
      const pending = s.status === "pending"
        ? `<span class="tag-pending" title="Waiting for admin approval">pending</span>`
        : "";

      const artist = s.artistId
        ? `<button class="song-artist artist-link" data-open-artist="${s.artistId}">${escapeHtml(s.artist)}</button>`
        : `<span class="song-artist">${escapeHtml(s.artist)}</span>`;

      return `
        <div class="song" data-index="${i}" data-song-id="${s.id}">
          ${cover}
          <div class="song-meta">
            <span class="song-title">${escapeHtml(s.title)}${badge}${pending}</span>
            ${artist}
          </div>
          <span class="song-dur">${s.durationS ? formatTime(s.durationS) : ""}</span>
          <div class="song-actions">${heart}${plus}${menu}</div>
        </div>`;
    })
    .join("");

  return `<div class="songs">${rows}</div>`;
}

function wireSongList(editablePlaylistId?: string): void {
  // Play from the cover's hover button (no more whole-row misclicks).
  document.querySelectorAll(".cover-play").forEach((btn) => {
    btn.addEventListener("click", (e) => {
      e.stopPropagation();
      playFromList(Number((btn as HTMLElement).dataset.play));
    });
  });

  // Double-clicking the row also plays, Spotify-style.
  document.querySelectorAll(".song").forEach((row) => {
    row.addEventListener("dblclick", (e) => {
      if ((e.target as HTMLElement).closest(".song-actions, .song-artist")) return;
      playFromList(Number((row as HTMLElement).dataset.index));
    });
  });

  document.querySelectorAll("[data-open-artist]").forEach((btn) => {
    btn.addEventListener("click", (e) => {
      e.stopPropagation();
      openArtist((btn as HTMLElement).dataset.openArtist!);
    });
  });

  document.querySelectorAll("[data-add]").forEach((btn) => {
    btn.addEventListener("click", async (e) => {
      e.stopPropagation();
      const songId = (btn as HTMLElement).dataset.add!;
      const target = await pickPlaylist();
      if (target) await api.addToPlaylist(target, songId);
    });
  });

  document.querySelectorAll("[data-remove]").forEach((btn) => {
    btn.addEventListener("click", async (e) => {
      e.stopPropagation();
      if (!editablePlaylistId) return;
      await api.removeFromPlaylist(editablePlaylistId, (btn as HTMLElement).dataset.remove!);
      renderMain();
    });
  });

  document.querySelectorAll("[data-like]").forEach((btn) => {
    btn.addEventListener("click", (e) => {
      e.stopPropagation();
      void toggleLike((btn as HTMLElement).dataset.like!);
    });
  });

  document.querySelectorAll("[data-menu]").forEach((btn) => {
    btn.addEventListener("click", (e) => {
      e.stopPropagation();
      const id = (btn as HTMLElement).dataset.menu!;
      const song = state.visibleSongs.find((s) => s.id === id);
      if (song) openContextMenu(btn as HTMLElement, songMenuItems(song));
    });
  });

  // Reflect the current track on the freshly-rendered list.
  highlightPlayingRow();
}

// The "⋯" menu for one song row: link + download for everyone; the account-only
// bits (link artist, edit/suggest edit, delete) appear per role.
function songMenuItems(song: Song): MenuItem[] {
  const items: MenuItem[] = [];
  if (song.artistId) {
    items.push({
      label: "Go to artist",
      icon: "bi-person",
      onClick: () => openArtist(song.artistId!),
    });
  }
  items.push({ label: "Copy song link", icon: "bi-link-45deg", onClick: () => copySongLink(song.id) });
  items.push({
    label: "Download original",
    icon: "bi-download",
    onClick: () => {
      window.location.href = `/song/${song.id}?download=1`;
    },
  });
  if (state.me) {
    items.push({
      label: "Link artist",
      icon: "bi-person-plus",
      onClick: () => openLinkArtistModal(song.id, song.title),
    });
    items.push({
      label: state.me.isAdmin ? "Edit details" : "Suggest an edit",
      icon: "bi-pencil",
      onClick: () => openEditModal(song),
    });
  }
  const isUploader = Boolean(song.uploadedBy) && song.uploadedBy === state.me?.id;
  if (isUploader || state.me?.isAdmin) {
    items.push({
      label: "Delete song",
      icon: "bi-trash",
      danger: true,
      onClick: async () => {
        const msg = isUploader
          ? "Delete this song for everyone?"
          : `Delete "${song.title}" by ${song.artist} for everyone? This removes the file.`;
        if (!confirm(msg)) return;
        await api.deleteSong(song.id);
        await Promise.all([loadSongs(), loadLiked()]);
        renderMain();
      },
    });
  }
  return items;
}

// Play a song from the visible list — but if it's already the current track,
// toggle play/pause instead of restarting it from the top.
function playFromList(index: number): void {
  const song = state.visibleSongs[index];
  if (song && player.current?.id === song.id) {
    player.toggle();
    return;
  }
  player.playQueue(state.visibleSongs, index);
}

// Add `.playing` (and `.paused` when stopped) to the row of the current track,
// clearing it from all others. Drives the animated now-playing bars via CSS.
function highlightPlayingRow(): void {
  const currentId = player.current?.id ?? null;
  const isPlaying = player.playing;
  document.querySelectorAll<HTMLElement>(".song").forEach((row) => {
    const isCurrent = currentId !== null && row.dataset.songId === currentId;
    row.classList.toggle("playing", isCurrent);
    row.classList.toggle("paused", isCurrent && !isPlaying);
  });
}

async function pickPlaylist(): Promise<string | null> {
  const mine = state.playlists;
  if (mine.length === 0) {
    flash("Create a playlist first.");
    return null;
  }
  return openPlaylistPickerModal(mine);
}

// Simple fuzzy filter for small in-memory lists (playlists, etc). Exact
// substring matches always win; otherwise falls back to a bigram
// dice-coefficient so "1800" still finds "1-800"-style names, and small
// typos don't come back empty.
function fuzzyFilter<T>(items: T[], query: string, key: (item: T) => string): T[] {
  const q = query.trim().toLowerCase();
  if (!q) return items;
  const normQ = q.replace(/[^a-z0-9]+/g, " ").trim();
  const tightQ = q.replace(/[^a-z0-9]+/g, "");

  return items
    .map((item) => {
      const raw = key(item).toLowerCase();
      const norm = raw.replace(/[^a-z0-9]+/g, " ").trim();
      const tight = raw.replace(/[^a-z0-9]+/g, "");
      const score =
        tight === tightQ || norm.includes(normQ) ? 1 : diceCoefficient(normQ, norm);
      return { item, score };
    })
    .filter((r) => r.score >= 0.35)
    .sort((a, b) => b.score - a.score)
    .map((r) => r.item);
}

// Sørensen–Dice coefficient over character bigrams, mirrored from the
// server's lib/text.ts (kept tiny + dependency-free for client-side use).
function diceCoefficient(a: string, b: string): number {
  if (a === b) return 1;
  if (a.length < 2 || b.length < 2) return 0;
  const bigrams = (s: string): Map<string, number> => {
    const map = new Map<string, number>();
    for (let i = 0; i < s.length - 1; i++) {
      const gram = s.slice(i, i + 2);
      map.set(gram, (map.get(gram) ?? 0) + 1);
    }
    return map;
  };
  const aGrams = bigrams(a);
  const bGrams = bigrams(b);
  let intersection = 0;
  for (const [gram, count] of aGrams) {
    const other = bGrams.get(gram);
    if (other) intersection += Math.min(count, other);
  }
  const total =
    [...aGrams.values()].reduce((s, n) => s + n, 0) +
    [...bGrams.values()].reduce((s, n) => s + n, 0);
  return total === 0 ? 0 : (2 * intersection) / total;
}

// --- player bar ------------------------------------------------------------

let pbSongId: string | null = null;
let seeking = false; // true while the user drags the seek slider

// Rebuild the bar only when the track changes; otherwise just update the
// dynamic bits. Re-rendering on every timeupdate would kill slider dragging
// (which is why volume/seek felt broken).
function renderPlayerBar(): void {
  const el = document.getElementById("playerbar");
  if (!el) return;

  const song = player.current;
  if (!song) {
    el.innerHTML = `<div class="pb-empty">Nothing playing</div>`;
    pbSongId = null;
    return;
  }

  if (song.id !== pbSongId || !el.querySelector(".pb-controls")) {
    mountPlayerBar(el, song);
    pbSongId = song.id;
  }
  updatePlayerBar();
}

function mountPlayerBar(el: HTMLElement, song: Song): void {
  el.innerHTML = `
    <div class="pb-song">
      <div class="pb-cover">
        ${
          song.coverUrl
            ? `<img class="cover" src="${song.coverUrl}" alt="" />`
            : `<div class="cover cover-empty"><i class="bi bi-music-note-beamed"></i></div>`
        }
      </div>
      <div class="song-meta">
        <span class="song-title">${escapeHtml(song.title)}</span>
        ${
          song.artistId
            ? `<button class="song-artist artist-link" id="pb-artist">${escapeHtml(primaryArtist(song.artist))}</button>`
            : `<span class="song-artist">${escapeHtml(primaryArtist(song.artist))}</span>`
        }
      </div>
      <div class="pb-song-actions">
        ${
          state.me
            ? `<button class="icon-btn like ${state.likedIds.has(song.id) ? "liked" : ""}" id="pb-like"
                 data-like="${song.id}" title="Like"><i class="bi bi-heart${
                   state.likedIds.has(song.id) ? "-fill" : ""
                 }"></i></button>`
            : ""
        }
        <button class="icon-btn" id="pb-menu" title="More"><i class="bi bi-three-dots-vertical"></i></button>
      </div>
    </div>

    <div class="pb-controls">
      <div class="pb-buttons">
        <button class="icon-btn" id="shuffle" title="Shuffle"><i class="bi bi-shuffle"></i></button>
        <button class="icon-btn" id="prev" title="Previous"><i class="bi bi-skip-start-fill"></i></button>
        <button class="icon-btn play" id="toggle"><i class="bi bi-play-fill"></i></button>
        <button class="icon-btn" id="next" title="Next"><i class="bi bi-skip-end-fill"></i></button>
        <button class="icon-btn" id="repeat" title="Repeat"><i class="bi bi-repeat"></i></button>
      </div>
      <div class="pb-seek">
        <button class="icon-btn pb-lyrics" id="lyrics-btn" title="Lyrics"><i class="bi bi-card-text"></i></button>
        <span class="pb-cur">0:00</span>
        <input type="range" id="seek" min="0" max="0" value="0" step="0.1" style="--pct:0%" />
        <span class="pb-dur">0:00</span>
      </div>
    </div>

    ${
      player.volumeSupported
        ? `<div class="pb-volume">
      <button class="icon-btn" id="mute" title="Mute"><i class="bi bi-volume-up-fill"></i></button>
      <input type="range" id="volume" min="0" max="1" step="0.01"
             value="${player.volume}" style="--pct:${player.volume * 100}%" />
    </div>`
        : ""
    }
  `;

  el.querySelector("#toggle")?.addEventListener("click", () => player.toggle());
  el.querySelector("#next")?.addEventListener("click", () => player.next());
  el.querySelector("#prev")?.addEventListener("click", () => player.prev());
  el.querySelector("#shuffle")?.addEventListener("click", () => player.toggleShuffle());
  el.querySelector("#repeat")?.addEventListener("click", () => player.cycleRepeat());

  const seek = el.querySelector("#seek") as HTMLInputElement | null;
  seek?.addEventListener("input", () => {
    seeking = true;
    const max = Number(seek.max) || 1;
    seek.style.setProperty("--pct", `${(Number(seek.value) / max) * 100}%`);
  });
  seek?.addEventListener("change", () => {
    player.seek(Number(seek.value));
    seeking = false;
  });

  const vol = el.querySelector("#volume") as HTMLInputElement | null;
  vol?.addEventListener("input", () => {
    player.setVolume(Number(vol.value));
    vol.style.setProperty("--pct", `${Number(vol.value) * 100}%`);
    updateVolumeIcon();
  });

  el.querySelector("#mute")?.addEventListener("click", () => {
    const next = player.volume > 0 ? 0 : 1;
    player.setVolume(next);
    if (vol) {
      vol.value = String(next);
      vol.style.setProperty("--pct", `${next * 100}%`);
    }
    updateVolumeIcon();
  });

  el.querySelector("#lyrics-btn")?.addEventListener("click", toggleLyrics);
  if (song.artistId) {
    el.querySelector("#pb-artist")?.addEventListener("click", () => openArtist(song.artistId!));
  }

  el.querySelector("#pb-like")?.addEventListener("click", () => void toggleLike(song.id));
  el.querySelector("#pb-menu")?.addEventListener("click", (e) => {
    openContextMenu(e.currentTarget as HTMLElement, [
      { label: "Copy song link", icon: "bi-link-45deg", onClick: () => copySongLink(song.id) },
      {
        label: "Download original",
        icon: "bi-download",
        onClick: () => {
          window.location.href = `/song/${song.id}?download=1`;
        },
      },
      ...(song.artistId
        ? [{ label: "Go to artist", icon: "bi-person", onClick: () => openArtist(song.artistId!) }]
        : []),
    ]);
  });
}

// Update only the changing pieces — never rebuild (keeps sliders draggable).
function updatePlayerBar(): void {
  const el = document.getElementById("playerbar");
  if (!el) return;

  const toggle = el.querySelector("#toggle i");
  if (toggle) toggle.className = player.playing ? "bi bi-pause-fill" : "bi bi-play-fill";

  el.querySelector("#shuffle")?.classList.toggle("on", player.shuffle);

  const repeatBtn = el.querySelector("#repeat");
  const repeatIcon = el.querySelector("#repeat i");
  if (repeatBtn && repeatIcon) {
    repeatBtn.classList.toggle("on", player.repeat !== "off");
    repeatIcon.className = player.repeat === "one" ? "bi bi-repeat-1" : "bi bi-repeat";
    (repeatBtn as HTMLElement).title = `Repeat: ${player.repeat}`;
  }

  const { current, duration } = player.progress;
  const cur = el.querySelector(".pb-cur");
  const dur = el.querySelector(".pb-dur");
  if (cur) cur.textContent = formatTime(current);
  if (dur) dur.textContent = formatTime(duration);

  const seek = el.querySelector("#seek") as HTMLInputElement | null;
  if (seek && !seeking) {
    seek.max = String(duration || 0);
    seek.value = String(current);
    seek.style.setProperty("--pct", `${duration ? (current / duration) * 100 : 0}%`);
  }

  el.querySelector("#lyrics-btn")?.classList.toggle("on", lyricsState.open);

  const pbLike = el.querySelector("#pb-like");
  if (pbLike && player.current) {
    const liked = state.likedIds.has(player.current.id);
    pbLike.classList.toggle("liked", liked);
    const i = pbLike.querySelector("i");
    if (i) i.className = liked ? "bi bi-heart-fill" : "bi bi-heart";
  }

  updateVolumeIcon();
}

function updateVolumeIcon(): void {
  const icon = document.querySelector("#mute i");
  if (!icon) return;
  const v = player.volume;
  icon.className =
    v === 0
      ? "bi bi-volume-mute-fill"
      : v < 0.5
        ? "bi bi-volume-down-fill"
        : "bi bi-volume-up-fill";
}

// --- lyrics ----------------------------------------------------------------

const lyricsState = {
  open: false,
  songId: null as string | null,
  data: null as Lyrics | null,
  active: -1,
};

function toggleLyrics(): void {
  lyricsState.open = !lyricsState.open;
  if (lyricsState.open) {
    renderLyricsOverlay();
    void loadLyricsForCurrent();
  } else {
    document.getElementById("lyrics-overlay")?.remove();
  }
  document.getElementById("lyrics-btn")?.classList.toggle("on", lyricsState.open);
}

function renderLyricsOverlay(): void {
  let ov = document.getElementById("lyrics-overlay");
  if (!ov) {
    ov = document.createElement("div");
    ov.id = "lyrics-overlay";
    ov.className = "lyrics-overlay";
    document.body.appendChild(ov);
  }
  const song = player.current;
  ov.innerHTML = `
    <div class="ly-head">
      <div class="ly-song">
        <span class="ly-title">${song ? escapeHtml(song.title) : "—"}</span>
        ${
          song?.artistId
            ? `<button class="ly-artist artist-link" id="ly-artist-btn">${escapeHtml(song.artist)}</button>`
            : `<span class="ly-artist">${song ? escapeHtml(song.artist) : ""}</span>`
        }
      </div>
      <button class="icon-btn" id="ly-close" title="Close"><i class="bi bi-x-lg"></i></button>
    </div>
    <div class="ly-body" id="ly-body"><p class="ly-note">Finding lyrics…</p></div>
  `;
  ov.querySelector("#ly-close")?.addEventListener("click", toggleLyrics);
  if (song?.artistId) {
    ov.querySelector("#ly-artist-btn")?.addEventListener("click", () => openArtist(song.artistId!));
  }
}

async function loadLyricsForCurrent(): Promise<void> {
  const song = player.current;
  const body = document.getElementById("ly-body");
  if (!song) {
    if (body) body.innerHTML = `<p class="ly-note">Play a song to see lyrics.</p>`;
    return;
  }
  lyricsState.songId = song.id;
  lyricsState.data = null;
  lyricsState.active = -1;
  try {
    lyricsState.data = await api.getLyrics(song.id);
  } catch {
    lyricsState.data = null;
  }
  // Bail if the user closed the panel or skipped tracks while we fetched.
  if (!lyricsState.open || player.current?.id !== song.id) return;
  renderLyricsBody();
}

function renderLyricsBody(): void {
  const body = document.getElementById("ly-body");
  if (!body) return;
  const d = lyricsState.data;

  if (!d || (!d.synced.length && !d.plain && !d.instrumental)) {
    body.className = "ly-body";
    body.innerHTML = `<p class="ly-note">No lyrics found for this one.</p>`;
    return;
  }
  if (d.instrumental) {
    body.className = "ly-body";
    body.innerHTML = `<p class="ly-note"><i class="bi bi-music-note-beamed"></i> Instrumental</p>`;
    return;
  }
  if (d.synced.length) {
    body.className = "ly-body is-synced";
    body.innerHTML = d.synced
      .map((l, i) => `<p class="ly-line" data-i="${i}">${escapeHtml(l.text || " ")}</p>`)
      .join("");
    return;
  }
  body.className = "ly-body";
  body.innerHTML = (d.plain ?? "")
    .split(/\r?\n/)
    .map((l) => `<p class="ly-line ly-static">${escapeHtml(l || " ")}</p>`)
    .join("");
}

// Called on every player tick: highlight + center the active synced line.
function syncLyrics(): void {
  if (!lyricsState.open) return;
  const song = player.current;
  if (!song) return;

  if (song.id !== lyricsState.songId) {
    renderLyricsOverlay();
    void loadLyricsForCurrent();
    return;
  }

  const d = lyricsState.data;
  if (!d || !d.synced.length) return;

  const posMs = player.progress.current * 1000;
  let i = -1;
  for (let k = 0; k < d.synced.length; k++) {
    if (d.synced[k]!.t <= posMs) i = k;
    else break;
  }
  if (i === lyricsState.active) return;

  const body = document.getElementById("ly-body");
  if (!body) return;
  const kids = body.children;
  if (lyricsState.active >= 0 && kids[lyricsState.active]) {
    kids[lyricsState.active]!.classList.remove("is-active");
  }
  lyricsState.active = i;
  const cur = kids[i] as HTMLElement | undefined;
  if (cur) {
    cur.classList.add("is-active");
    body.scrollTo({
      top: cur.offsetTop - body.clientHeight / 2 + cur.clientHeight / 2,
      behavior: "smooth",
    });
  }
}

// --- utils -----------------------------------------------------------------

function activePlaylist(id: string): boolean {
  return state.view.kind === "playlist" && state.view.id === id;
}

function escapeHtml(s: string): string {
  return s
    .replace(/&/g, "&amp;")
    .replace(/</g, "&lt;")
    .replace(/>/g, "&gt;")
    .replace(/"/g, "&quot;");
}

// Register the desktop sign-in deeplink handler exactly once, then boot.
// When the system browser bounces back with a token, re-boot into the
// signed-in app.
void initDesktopAuth(() => {
  void boot();
}).finally(() => {
  void boot();
});