/* macOS Moonlight app mockup — closer to the real design, with real audio playback */
const { useState, useRef, useEffect, useCallback } = React;

// ---------- Icons (minimal stroke / fill) ----------
const Ico = {
  nowPlaying: (
    <svg width="15" height="15" viewBox="0 0 16 16" fill="none">
      <path d="M5.5 12V4l7-1.5V11" stroke="currentColor" strokeWidth="1.3" strokeLinejoin="round" strokeLinecap="round"/>
      <circle cx="4" cy="12" r="1.7" stroke="currentColor" strokeWidth="1.3"/>
      <circle cx="11" cy="11" r="1.7" stroke="currentColor" strokeWidth="1.3"/>
    </svg>
  ),
  albums: (
    <svg width="15" height="15" viewBox="0 0 16 16" fill="none">
      <rect x="1.5" y="1.5" width="5.5" height="5.5" rx="0.8" stroke="currentColor" strokeWidth="1.3"/>
      <rect x="9" y="1.5" width="5.5" height="5.5" rx="0.8" stroke="currentColor" strokeWidth="1.3"/>
      <rect x="1.5" y="9" width="5.5" height="5.5" rx="0.8" stroke="currentColor" strokeWidth="1.3"/>
      <rect x="9" y="9" width="5.5" height="5.5" rx="0.8" stroke="currentColor" strokeWidth="1.3"/>
    </svg>
  ),
  artist: (
    <svg width="15" height="15" viewBox="0 0 16 16" fill="none">
      <path d="M9.5 3a1.5 1.5 0 11-3 0 1.5 1.5 0 013 0z" stroke="currentColor" strokeWidth="1.3"/>
      <path d="M8 4.5v6" stroke="currentColor" strokeWidth="1.3" strokeLinecap="round"/>
      <path d="M5 14l3-3.5L11 14" stroke="currentColor" strokeWidth="1.3" strokeLinecap="round" strokeLinejoin="round"/>
    </svg>
  ),
  songs: (
    <svg width="15" height="15" viewBox="0 0 16 16" fill="none">
      <path d="M6 12V4l6-1.5V10.5" stroke="currentColor" strokeWidth="1.3" strokeLinejoin="round"/>
      <ellipse cx="4.5" cy="12" rx="1.7" ry="1.5" stroke="currentColor" strokeWidth="1.3"/>
      <ellipse cx="10.5" cy="10.5" rx="1.7" ry="1.5" stroke="currentColor" strokeWidth="1.3"/>
    </svg>
  ),
  favorite: (
    <svg width="15" height="15" viewBox="0 0 16 16" fill="none">
      <path d="M8 13.5s-5-2.8-5-6.4A2.6 2.6 0 015.5 4.5 2.9 2.9 0 018 6a2.9 2.9 0 012.5-1.5A2.6 2.6 0 0113 7.1c0 3.6-5 6.4-5 6.4z" stroke="currentColor" strokeWidth="1.3" strokeLinejoin="round"/>
    </svg>
  ),
  composer: (
    <svg width="15" height="15" viewBox="0 0 16 16" fill="none">
      <circle cx="6" cy="5" r="2.2" stroke="currentColor" strokeWidth="1.3"/>
      <path d="M1.5 14c0-2.5 2-4 4.5-4s4.5 1.5 4.5 4" stroke="currentColor" strokeWidth="1.3" strokeLinecap="round"/>
      <path d="M11.5 5l3 3M14.5 5l-3 3" stroke="currentColor" strokeWidth="1.3" strokeLinecap="round"/>
    </svg>
  ),
  genres: (
    <svg width="15" height="15" viewBox="0 0 16 16" fill="none">
      <circle cx="8" cy="8" r="6" stroke="currentColor" strokeWidth="1.3"/>
      <circle cx="8" cy="8" r="2" stroke="currentColor" strokeWidth="1.3"/>
      <path d="M8 2v3M8 11v3M2 8h3M11 8h3" stroke="currentColor" strokeWidth="1.3" strokeLinecap="round"/>
    </svg>
  ),
  playlist: (
    <svg width="15" height="15" viewBox="0 0 16 16" fill="none">
      <path d="M3 4h7M3 8h7M3 12h4" stroke="currentColor" strokeWidth="1.3" strokeLinecap="round"/>
      <path d="M12 9v4M10.5 11.5h3" stroke="currentColor" strokeWidth="1.3" strokeLinecap="round"/>
    </svg>
  ),
  sidebar: (
    <svg width="14" height="14" viewBox="0 0 16 16" fill="none">
      <rect x="1.5" y="3" width="13" height="10" rx="1.5" stroke="currentColor" strokeWidth="1.3"/>
      <path d="M6 3v10" stroke="currentColor" strokeWidth="1.3"/>
    </svg>
  ),
  plus: (
    <svg width="14" height="14" viewBox="0 0 16 16" fill="none">
      <path d="M8 3.5v9M3.5 8h9" stroke="currentColor" strokeWidth="1.4" strokeLinecap="round"/>
    </svg>
  ),
  search: (
    <svg width="13" height="13" viewBox="0 0 16 16" fill="none">
      <circle cx="7" cy="7" r="4.5" stroke="currentColor" strokeWidth="1.3"/>
      <path d="M10.5 10.5l3 3" stroke="currentColor" strokeWidth="1.3" strokeLinecap="round"/>
    </svg>
  ),
  play: (
    <svg width="14" height="14" viewBox="0 0 16 16" fill="currentColor"><path d="M4.5 3v10l9-5z"/></svg>
  ),
  pause: (
    <svg width="14" height="14" viewBox="0 0 16 16" fill="currentColor"><rect x="4" y="3" width="3" height="10" rx="0.6"/><rect x="9" y="3" width="3" height="10" rx="0.6"/></svg>
  ),
  prev: (
    <svg width="16" height="16" viewBox="0 0 16 16" fill="currentColor">
      <rect x="3" y="4" width="1.6" height="8" rx="0.4"/>
      <path d="M13.5 4.2v7.6L6 8z"/>
    </svg>
  ),
  next: (
    <svg width="16" height="16" viewBox="0 0 16 16" fill="currentColor">
      <rect x="11.4" y="4" width="1.6" height="8" rx="0.4"/>
      <path d="M2.5 4.2v7.6L10 8z"/>
    </svg>
  ),
  shuffle: (
    <svg width="16" height="16" viewBox="0 0 16 16" fill="none">
      <path d="M2 4h2.5l7 8H14M2 12h2.5l1.5-1.7M14 4h-2.5l-1.5 1.7" stroke="currentColor" strokeWidth="1.3" strokeLinecap="round" strokeLinejoin="round"/>
      <path d="M12 2.3L14 4l-2 1.7M12 10.3L14 12l-2 1.7" stroke="currentColor" strokeWidth="1.3" strokeLinecap="round" strokeLinejoin="round"/>
    </svg>
  ),
  repeat: (
    <svg width="16" height="16" viewBox="0 0 16 16" fill="none">
      <path d="M3 6V5a2 2 0 012-2h8M13 10v1a2 2 0 01-2 2H3" stroke="currentColor" strokeWidth="1.3" strokeLinecap="round"/>
      <path d="M11 1.3L13 3l-2 1.7M5 11.3L3 13l2 1.7" stroke="currentColor" strokeWidth="1.3" strokeLinecap="round" strokeLinejoin="round"/>
    </svg>
  ),
  queue: (
    <svg width="16" height="16" viewBox="0 0 16 16" fill="none">
      <path d="M3 4h10M3 8h10M3 12h6" stroke="currentColor" strokeWidth="1.3" strokeLinecap="round"/>
      <circle cx="12.5" cy="12" r="1.3" stroke="currentColor" strokeWidth="1.3"/>
    </svg>
  ),
  airplay: (
    <svg width="16" height="16" viewBox="0 0 16 16" fill="none">
      <path d="M3 11.5H2.5a1.5 1.5 0 01-1.5-1.5V4a1.5 1.5 0 011.5-1.5h11A1.5 1.5 0 0115 4v6a1.5 1.5 0 01-1.5 1.5H13" stroke="currentColor" strokeWidth="1.3" strokeLinecap="round"/>
      <path d="M5 14l3-3.5L11 14z" stroke="currentColor" strokeWidth="1.3" strokeLinejoin="round"/>
    </svg>
  ),
  speaker: (
    <svg width="14" height="14" viewBox="0 0 16 16" fill="none">
      <path d="M3 6h2l3-2.5v9L5 10H3z" stroke="currentColor" strokeWidth="1.3" strokeLinejoin="round"/>
      <path d="M10.5 6c.7.5 1 1.2 1 2s-.3 1.5-1 2" stroke="currentColor" strokeWidth="1.3" strokeLinecap="round"/>
    </svg>
  ),
  backArrow: (
    <svg width="10" height="10" viewBox="0 0 10 10"><path d="M6.5 1.5L3 5l3.5 3.5" stroke="currentColor" strokeWidth="1.3" fill="none" strokeLinecap="round" strokeLinejoin="round"/></svg>
  ),
  fwdArrow: (
    <svg width="10" height="10" viewBox="0 0 10 10"><path d="M3.5 1.5L7 5l-3.5 3.5" stroke="currentColor" strokeWidth="1.3" fill="none" strokeLinecap="round" strokeLinejoin="round"/></svg>
  ),
};

// ---------- Album cover (CSS art) ----------
function AlbumCover({ hue1, hue2, motif = "discs", className = "cover", style }) {
  const motifs = {
    discs: (
      <>
        <div style={{position:'absolute', inset:'18%', borderRadius:'50%', border:'1px solid rgba(255,255,255,0.18)'}}/>
        <div style={{position:'absolute', inset:'34%', borderRadius:'50%', border:'1px solid rgba(255,255,255,0.12)'}}/>
        <div style={{position:'absolute', inset:'48%', borderRadius:'50%', background:'rgba(255,255,255,0.55)'}}/>
      </>
    ),
    bars: (
      <div style={{position:'absolute', left:'15%', right:'15%', bottom:'18%', display:'flex', gap:3, alignItems:'flex-end', height:'40%'}}>
        {[0.4,0.7,0.3,0.85,0.55,0.95,0.45,0.6,0.35,0.75].map((h,i)=>(
          <div key={i} style={{flex:1, height:`${h*100}%`, background:'rgba(255,255,255,0.7)', borderRadius:1}}/>
        ))}
      </div>
    ),
    wave: (
      <svg viewBox="0 0 100 100" style={{position:'absolute', inset:0, width:'100%', height:'100%'}}>
        <path d="M5 50 Q 25 20 50 50 T 95 50" stroke="rgba(255,255,255,0.7)" strokeWidth="1.5" fill="none"/>
        <path d="M5 60 Q 25 30 50 60 T 95 60" stroke="rgba(255,255,255,0.4)" strokeWidth="1.5" fill="none"/>
      </svg>
    ),
    moon: (
      <div style={{position:'absolute', right:'20%', top:'20%', width:'46%', aspectRatio:'1', borderRadius:'50%', background:'radial-gradient(circle at 30% 30%, rgba(255,255,255,0.9), rgba(255,255,255,0.1) 70%)'}}/>
    ),
    star: (
      <svg viewBox="0 0 100 100" style={{position:'absolute', inset:0, width:'100%', height:'100%'}}>
        <circle cx="50" cy="50" r="2" fill="rgba(255,255,255,0.9)"/>
        <circle cx="30" cy="35" r="1" fill="rgba(255,255,255,0.7)"/>
        <circle cx="70" cy="60" r="1.5" fill="rgba(255,255,255,0.8)"/>
        <circle cx="65" cy="25" r="0.8" fill="rgba(255,255,255,0.6)"/>
        <circle cx="25" cy="65" r="1.2" fill="rgba(255,255,255,0.7)"/>
        <circle cx="80" cy="80" r="0.8" fill="rgba(255,255,255,0.5)"/>
      </svg>
    ),
    pillar: (
      <div style={{position:'absolute', inset:'25% 30%', borderTop:'1px solid rgba(255,255,255,0.5)', borderLeft:'1px solid rgba(255,255,255,0.3)', borderRight:'1px solid rgba(255,255,255,0.3)'}}/>
    ),
    cross: (
      <>
        <div style={{position:'absolute', left:'50%', top:'18%', bottom:'18%', width:1, background:'rgba(255,255,255,0.4)'}}/>
        <div style={{position:'absolute', top:'50%', left:'18%', right:'18%', height:1, background:'rgba(255,255,255,0.4)'}}/>
      </>
    ),
    arc: (
      <svg viewBox="0 0 100 100" style={{position:'absolute', inset:0, width:'100%', height:'100%'}}>
        <path d="M 15 80 A 35 35 0 0 1 85 80" stroke="rgba(255,255,255,0.6)" strokeWidth="1.2" fill="none"/>
        <path d="M 25 80 A 25 25 0 0 1 75 80" stroke="rgba(255,255,255,0.4)" strokeWidth="1.2" fill="none"/>
      </svg>
    ),
    portrait: (
      <>
        {/* abstract portrait silhouette */}
        <div style={{position:'absolute', bottom:0, left:'18%', right:'18%', height:'55%', background:'rgba(0,0,0,0.35)', borderRadius:'50% 50% 0 0 / 70% 70% 0 0'}}/>
        <div style={{position:'absolute', bottom:'40%', left:'30%', width:'40%', aspectRatio:'1', borderRadius:'50%', background:'rgba(0,0,0,0.35)'}}/>
      </>
    ),
    landscape: (
      <>
        <div style={{position:'absolute', inset:'0 0 35% 0', background:'linear-gradient(180deg, transparent 30%, rgba(255,255,255,0.18))'}}/>
        <div style={{position:'absolute', left:'10%', right:'10%', top:'45%', height:1, background:'rgba(255,255,255,0.3)'}}/>
        <svg viewBox="0 0 100 100" style={{position:'absolute', inset:0, width:'100%', height:'100%'}}>
          <path d="M0 65 L20 55 L35 62 L55 48 L75 58 L100 50 L100 100 L0 100z" fill="rgba(0,0,0,0.35)"/>
        </svg>
      </>
    ),
  };
  return (
    <div className={className} style={{
      ...style,
      position: 'relative',
      overflow: 'hidden',
      background: `linear-gradient(135deg, ${hue1} 0%, ${hue2} 100%)`,
      boxShadow: 'inset 0 0 0 1px rgba(255,255,255,0.06), 0 4px 14px -6px rgba(0,0,0,0.5)',
    }}>
      {motifs[motif] || motifs.discs}
      <div style={{position:'absolute', inset:0, background:`radial-gradient(120% 80% at 30% 0%, rgba(255,255,255,0.16), transparent 50%), linear-gradient(180deg, transparent 60%, rgba(0,0,0,0.25))`}}/>
    </div>
  );
}

// Albums modeled to mirror the user's screenshot
const ALBUMS = [
  { title: "20th Century French Wind Trios", artist: "The Chicago Chamber Musi…", h1:"#5a78a8", h2:"#1f2940", motif:"pillar" },
  { title: "Alexandre Tharaud Plays Scarlatti", artist: "Alexandre Tharaud", h1:"#c6512d", h2:"#4a1408", motif:"portrait" },
  { title: "Bach, Handel, Scarlatti: G…", artist: "Steven Isserlis", h1:"#3a3a44", h2:"#15151c", motif:"portrait" },
  { title: "Bach: French Overture, Ita…", artist: "Angela Hewitt", h1:"#7c9a6a", h2:"#1f2c1a", motif:"landscape" },
  { title: "Bach: Goldberg Variations…", artist: "Murray Perahia", h1:"#2f3f6a", h2:"#0e1430", motif:"portrait" },
  { title: "Bach: Keyboard Concertos", artist: "Murray Perahia, Academy of…", h1:"#dbe1ea", h2:"#6b748a", motif:"portrait" },

  { title: "Bach: The Art Of Life", artist: "Daniil Trifonov", h1:"#a7b394", h2:"#3a3f2a", motif:"landscape" },
  { title: "Bach: The Well-Tempered…", artist: "Glenn Gould", h1:"#e2e2e0", h2:"#7d7c78", motif:"discs" },
  { title: "Bach: Violin Concertos", artist: "Hilary Hahn, Jeffrey Kahane,…", h1:"#5a83a8", h2:"#1d2c44", motif:"portrait" },
  { title: "Bach: Well Tempered Clavi…", artist: "András Schiff", h1:"#7a2920", h2:"#2a0c08", motif:"wave" },
  { title: "Barber & Meyer: Violin Co…", artist: "Hilary Hahn, Hugh Wolff, Sai…", h1:"#b8893a", h2:"#3a2a14", motif:"landscape" },
  { title: "Bartók: Concerto for orch…", artist: "Susanna Mälkki, Helsinki Phil…", h1:"#a8392a", h2:"#3a100a", motif:"bars" },
];

// Format seconds → m:ss
function fmtTime(s) {
  if (!isFinite(s) || s < 0) s = 0;
  const m = Math.floor(s / 60);
  const sec = Math.floor(s % 60);
  return `${m}:${sec.toString().padStart(2, "0")}`;
}

// Parse "Artist - Title.ext" if possible
function parseFileName(name) {
  const base = name.replace(/\.[^.]+$/, "");
  if (base.includes(" - ")) {
    const [a, ...rest] = base.split(" - ");
    return { artist: a.trim(), title: rest.join(" - ").trim() };
  }
  return { artist: "", title: base };
}

// ---------- The mockup ----------
function MacOSMockup() {
  // Audio state
  const audioRef = useRef(null);
  const fileInputRef = useRef(null);
  // Default placeholder track — matches the real app's idle state
  const DEFAULT_TRACK = {
    title: "The Well‑Tempered Clavier, Book I: Prel…",
    artist: "András Schiff",
    url: null,
    hue1: "#7a2920", hue2: "#2a0c08", motif: "wave",
    placeholder: true,
  };
  const [track, setTrack] = useState(DEFAULT_TRACK);
  const [playing, setPlaying] = useState(false);
  const [currentTime, setCurrentTime] = useState(0);
  const [duration, setDuration] = useState(0);
  const [volume, setVolume] = useState(0.7);
  const [shuffle, setShuffle] = useState(false);
  const [repeat, setRepeat] = useState(false);
  const [dragging, setDragging] = useState(false);

  const sidebar = [
    { group: "Library", items: [
      { ico: "nowPlaying", label: "Now Playing" },
      { ico: "albums", label: "Albums", active: true },
      { ico: "artist", label: "Artists" },
      { ico: "songs", label: "Songs" },
      { ico: "favorite", label: "Favorites" },
      { ico: "composer", label: "Composers" },
      { ico: "genres", label: "Genres" },
    ]},
    { group: "Playlists", plus: true, items: [
      { ico: "playlist", label: "Ttest" },
    ]},
  ];

  // ---------- Audio plumbing ----------
  const loadFile = useCallback((file) => {
    if (!file || !file.type.startsWith("audio/")) return;
    const url = URL.createObjectURL(file);
    const { title, artist } = parseFileName(file.name);
    // Try ID3 via shake — not loading a parser, so fall back to filename
    setTrack(prev => {
      if (prev?.url) URL.revokeObjectURL(prev.url);
      return {
        title,
        artist: artist || "Local file",
        url, name: file.name,
        // Carry over chosen cover if the user picked one via an album tile
        hue1: prev?.hue1 || "#5a4a8a",
        hue2: prev?.hue2 || "#241b3e",
        motif: prev?.motif || "discs",
        placeholder: false,
      };
    });
    // auto-play
    requestAnimationFrame(() => {
      if (audioRef.current) {
        audioRef.current.src = url;
        audioRef.current.volume = volume;
        audioRef.current.play().catch(()=>{});
      }
    });
  }, [volume]);

  const openPicker = () => fileInputRef.current?.click();

  const togglePlay = () => {
    if (!track || track.placeholder || !track.url) { openPicker(); return; }
    const a = audioRef.current;
    if (!a) return;
    if (a.paused) a.play().catch(()=>{});
    else a.pause();
  };

  // Wire audio element events
  useEffect(() => {
    const a = audioRef.current;
    if (!a) return;
    const onPlay = () => setPlaying(true);
    const onPause = () => setPlaying(false);
    const onTime = () => setCurrentTime(a.currentTime);
    const onDur = () => setDuration(a.duration || 0);
    const onEnd = () => {
      if (repeat) { a.currentTime = 0; a.play().catch(()=>{}); }
      else setPlaying(false);
    };
    a.addEventListener("play", onPlay);
    a.addEventListener("pause", onPause);
    a.addEventListener("timeupdate", onTime);
    a.addEventListener("loadedmetadata", onDur);
    a.addEventListener("durationchange", onDur);
    a.addEventListener("ended", onEnd);
    return () => {
      a.removeEventListener("play", onPlay);
      a.removeEventListener("pause", onPause);
      a.removeEventListener("timeupdate", onTime);
      a.removeEventListener("loadedmetadata", onDur);
      a.removeEventListener("durationchange", onDur);
      a.removeEventListener("ended", onEnd);
    };
  }, [repeat]);

  useEffect(() => {
    if (audioRef.current) audioRef.current.volume = volume;
  }, [volume]);

  // Scrubber click/drag → seek
  const seekFromEvent = (e, el) => {
    const rect = el.getBoundingClientRect();
    const pct = Math.min(1, Math.max(0, (e.clientX - rect.left) / rect.width));
    if (audioRef.current && duration) audioRef.current.currentTime = pct * duration;
  };

  // Volume click → set
  const volFromEvent = (e, el) => {
    const rect = el.getBoundingClientRect();
    const pct = Math.min(1, Math.max(0, (e.clientX - rect.left) / rect.width));
    setVolume(pct);
  };

  // Drag-and-drop
  const onDragOver = (e) => { e.preventDefault(); setDragging(true); };
  const onDragLeave = (e) => {
    // only set false when leaving the actual mockup window
    if (e.target === e.currentTarget) setDragging(false);
  };
  const onDrop = (e) => {
    e.preventDefault();
    setDragging(false);
    const f = e.dataTransfer.files?.[0];
    if (f) loadFile(f);
  };

  // Keyboard space → play/pause (when focused)
  // Skip — could interfere with page scrolling.

  const progressPct = duration ? (currentTime / duration) : 0;

  // Click on an album tile: remember its colors for the now-playing art, then open picker.
  const onAlbumClick = (album) => {
    setTrack(t => ({
      ...(t || {}),
      hue1: album.h1, hue2: album.h2, motif: album.motif,
      // Tentatively show the album as the placeholder until a real file is loaded
      title: t?.placeholder ? album.title : t?.title || album.title,
      artist: t?.placeholder ? album.artist : t?.artist || album.artist,
      placeholder: t?.placeholder ? true : t?.placeholder,
    }));
    openPicker();
  };

  return (
    <div
      className={`mockup-window ${dragging ? "dragging" : ""}`}
      onDragOver={onDragOver}
      onDragLeave={onDragLeave}
      onDrop={onDrop}
    >
      {/* Hidden audio + file input */}
      <audio ref={audioRef} preload="metadata"/>
      <input
        ref={fileInputRef}
        type="file"
        accept="audio/*"
        style={{ display: "none" }}
        onChange={(e) => loadFile(e.target.files?.[0])}
      />

      {/* Drop overlay */}
      <div className="drop-overlay">Drop audio file to play</div>

      {/* Body — no separate titlebar; traffic lights live at the top of the sidebar */}
      <div className="mockup-body">
        <aside className="mockup-sidebar">
          <div className="traffic">
            <span className="tl red"/>
            <span className="tl yellow"/>
            <span className="tl green"/>
          </div>
          {sidebar.map((g, gi) => (
            <div key={gi} className="sb-group">
              <div className="sb-group-head">
                <div className="sb-group-label">{g.group}</div>
                {g.group === "Library" && (
                  <button className="sb-group-icon" aria-label="toggle sidebar">{Ico.sidebar}</button>
                )}
                {g.plus && (
                  <button className="sb-group-icon" aria-label="new playlist">{Ico.plus}</button>
                )}
              </div>
              {g.items.map((it, i) => (
                <div
                  key={i}
                  className={`sb-row ${it.active ? "active" : ""}`}
                  onClick={() => it.label === "Now Playing" && openPicker()}
                >
                  <span className="sb-ico">{Ico[it.ico]}</span>
                  <span className="sb-label">{it.label}</span>
                </div>
              ))}
            </div>
          ))}
        </aside>

        <main className="mockup-main">
          <div className="main-head">
            <div>
              <div className="main-head-title">Albums</div>
              <div className="main-head-sub">172 albums</div>
            </div>
            <div className="mockup-search-main">
              <span style={{display:'inline-flex', color:'rgba(255,255,255,0.55)'}}>{Ico.search}</span>
              <span>Search</span>
            </div>
          </div>

          <div className="album-grid">
            {ALBUMS.map((a, i) => (
              <div key={i} className="album-tile" onClick={() => onAlbumClick(a)}>
                <AlbumCover hue1={a.h1} hue2={a.h2} motif={a.motif} className="cover"/>
                <div className="album-meta">
                  <div className="album-title">{a.title}</div>
                  <div className="album-artist">{a.artist}</div>
                </div>
              </div>
            ))}
          </div>
        </main>
      </div>

      {/* Transport */}
      <div className="mockup-transport">
        {/* Left: now playing */}
        <div
          className={`np ${track?.placeholder ? "placeholder" : ""}`}
          onClick={openPicker}
          title={track?.placeholder ? "Click to load a real audio file" : track?.name}
        >
          <AlbumCover
            className="cover-mini"
            hue1={track?.hue1 || "#5a4a8a"}
            hue2={track?.hue2 || "#241b3e"}
            motif={track?.motif || "discs"}
          />
          <div className="np-meta">
            <div className="np-title">{track?.title}</div>
            <div className="np-sub">{track?.artist}</div>
          </div>
        </div>

        {/* Center: controls */}
        <div className="tr-center">
          <div className="tr-controls">
            <button
              className={`iconbtn ${shuffle ? "toggled" : ""}`}
              onClick={() => setShuffle(s => !s)}
              aria-label="shuffle"
            >{Ico.shuffle}</button>
            <button className="iconbtn" aria-label="prev" onClick={() => { if (audioRef.current) audioRef.current.currentTime = 0; }}>{Ico.prev}</button>
            <button className="iconbtn play" aria-label="play/pause" onClick={togglePlay}>
              {playing ? Ico.pause : Ico.play}
            </button>
            <button className="iconbtn" aria-label="next" onClick={openPicker}>{Ico.next}</button>
            <button
              className={`iconbtn ${repeat ? "toggled" : ""}`}
              onClick={() => setRepeat(r => !r)}
              aria-label="repeat"
            >{Ico.repeat}</button>
          </div>
        </div>

        {/* Right: queue, airplay, volume */}
        <div className="tr-right">
          <button className="iconbtn" aria-label="queue">{Ico.queue}</button>
          <button className="iconbtn" aria-label="airplay">{Ico.airplay}</button>
          <div className="vol">
            <span style={{display:'inline-flex'}}>{Ico.speaker}</span>
            <div
              className="vol-bar"
              onClick={(e) => volFromEvent(e, e.currentTarget)}
            >
              <div className="vol-fill" style={{ width: `${volume * 100}%` }}/>
            </div>
          </div>
        </div>

        {/* Full-width scrubber at the bottom */}
        <div className="tr-scrub">
          <span className="tr-time">{fmtTime(currentTime)}</span>
          <div className="tr-bar" onClick={(e) => seekFromEvent(e, e.currentTarget)}>
            <div className="tr-fill" style={{ width: `${progressPct * 100}%` }}/>
            <div className="tr-knob" style={{ left: `${progressPct * 100}%` }}/>
          </div>
          <span className="tr-time right">{duration ? `-${fmtTime(duration - currentTime)}` : "-0:00"}</span>
        </div>
      </div>
    </div>
  );
}

window.MacOSMockup = MacOSMockup;
window.AlbumCover = AlbumCover;
window.Ico = Ico;
