/* ============================================================
   Procedural album cover art
   Each `Cover` component picks a style and renders an SVG
   ============================================================ */

const { useId, useMemo, useSyncExternalStore } = React;

// Subscribe to MetaStore for live cover updates
function useAlbumCoverUrl(album) {
  return useSyncExternalStore(
    (fn) => window.MetaStore.subscribe(fn),
    () => window.MetaStore.getAlbumCoverUrl(album),
    () => null,
  );
}

// helper to lighten/darken (simple HSL shift via canvas not avail; use alpha mixes)
function mix(c1, c2, t) {
  // hex -> rgb
  const parse = h => {
    const n = parseInt(h.replace("#", ""), 16);
    return [(n >> 16) & 0xff, (n >> 8) & 0xff, n & 0xff];
  };
  const [r1, g1, b1] = parse(c1);
  const [r2, g2, b2] = parse(c2);
  const r = Math.round(r1 + (r2 - r1) * t);
  const g = Math.round(g1 + (g2 - g1) * t);
  const b = Math.round(b1 + (b2 - b1) * t);
  return `rgb(${r}, ${g}, ${b})`;
}

// =============================================================
// <Cover album={...} text={true/false}>
// =============================================================
function Cover({ album, text = true, size = "auto", overrideUrl = null }) {
  const id = useId().replace(/:/g, "");
  const storeUrl = useAlbumCoverUrl(album);
  if (!album) return <div style={{ background: "#1a0c17", width: "100%", height: "100%" }} />;
  const cov = album.cover || { style: "sunburst", c1: "#ff3d9a", c2: "#f5c14b", c3: "#08040a" };

  // Priority: explicit override > extracted-from-file > static imageUrl on cover > procedural
  const effectiveUrl = overrideUrl || storeUrl || (cov.style === "image" ? cov.imageUrl : null);
  if (effectiveUrl) {
    const effectiveCov = { ...cov, style: "image", imageUrl: effectiveUrl };
    return <CoverImage id={id} album={album} cov={effectiveCov} text={text} />;
  }

  const props = { id, album, cov, text };

  switch (cov.style) {
    case "image":      return <CoverImage {...props} />;
    case "sunburst":   return <CoverSunburst {...props} />;
    case "halftone":   return <CoverHalftone {...props} />;
    case "mirrorball": return <CoverMirrorball {...props} />;
    case "grid":       return <CoverGrid {...props} />;
    case "lipgloss":   return <CoverLipgloss {...props} />;
    case "neon":       return <CoverNeon {...props} />;
    case "stripes":    return <CoverStripes {...props} />;
    case "portrait":   return <CoverPortrait {...props} />;
    case "sunset":     return <CoverSunset {...props} />;
    case "diamond":    return <CoverDiamond {...props} />;
    default:           return <CoverSunburst {...props} />;
  }
}

// shared text wrap
function CoverText({ album, color = "#fff", bg = null, align = "center" }) {
  if (!album) return null;
  const t = album.title;
  return (
    <div className="cover-text" style={{
      position: "absolute",
      inset: 0,
      display: "flex",
      flexDirection: "column",
      justifyContent: "flex-end",
      alignItems: align === "center" ? "center" : "flex-start",
      padding: "8% 8% 10%",
      pointerEvents: "none",
      textAlign: align,
    }}>
      <div style={{
        fontFamily: "var(--f-display)",
        fontWeight: 900,
        fontSize: "13.5cqw",
        lineHeight: 0.9,
        textTransform: "uppercase",
        color,
        textShadow: bg ? "0 2px 0 " + bg : "0 2px 8px rgba(0,0,0,0.5)",
        letterSpacing: "0.005em",
        textWrap: "balance",
      }}>{t}</div>
      <div style={{
        fontFamily: "var(--f-body)",
        fontSize: "5cqw",
        fontWeight: 600,
        color,
        opacity: 0.75,
        marginTop: "3%",
        letterSpacing: "0.08em",
        textTransform: "uppercase",
      }}>{album.artist}</div>
    </div>
  );
}

// =========================================================
// IMAGE — real cover art (uploaded JPEG/PNG)
// =========================================================
function CoverImage({ id, album, cov, text }) {
  return (
    <div className="cover-root" style={{ position: "relative", width: "100%", height: "100%", containerType: "inline-size", background: cov.c3 || "#08040a", overflow: "hidden" }}>
      <img
        src={cov.imageUrl}
        alt={album?.title || ""}
        style={{ position: "absolute", inset: 0, width: "100%", height: "100%", objectFit: "cover", display: "block" }}
        draggable={false}
      />
    </div>
  );
}

// =========================================================
// SUNBURST — radial rays from a sun, big disco look
// =========================================================
function CoverSunburst({ id, album, cov, text }) {
  const rays = 24;
  return (
    <div className="cover-root" style={{ position: "relative", width: "100%", height: "100%", containerType: "inline-size", background: cov.c3, overflow: "hidden" }}>
      <svg viewBox="0 0 100 100" preserveAspectRatio="xMidYMid slice" style={{ position: "absolute", inset: 0, width: "100%", height: "100%" }}>
        <defs>
          <radialGradient id={`g-${id}`} cx="50%" cy="38%" r="60%">
            <stop offset="0%" stopColor={cov.c2} />
            <stop offset="40%" stopColor={cov.c1} />
            <stop offset="100%" stopColor={cov.c3} />
          </radialGradient>
          <radialGradient id={`sun-${id}`} cx="50%" cy="50%" r="50%">
            <stop offset="0%" stopColor={cov.c2} />
            <stop offset="60%" stopColor={cov.c1} />
            <stop offset="100%" stopColor={cov.c1} stopOpacity="0" />
          </radialGradient>
        </defs>
        <rect x="0" y="0" width="100" height="100" fill={`url(#g-${id})`} />
        {/* rays */}
        <g style={{ transformOrigin: "50px 38px" }}>
          {Array.from({ length: rays }).map((_, i) => {
            const a = (i / rays) * 360;
            return (
              <rect
                key={i}
                x="49.6" y="-20" width="0.8" height="60"
                fill={cov.c2}
                opacity={i % 2 ? 0.35 : 0.6}
                transform={`rotate(${a} 50 38)`}
              />
            );
          })}
        </g>
        {/* sun disc */}
        <circle cx="50" cy="38" r="22" fill={`url(#sun-${id})`} />
        <circle cx="50" cy="38" r="14" fill={cov.c2} opacity="0.6" />
        {/* horizon */}
        <rect x="0" y="62" width="100" height="38" fill={cov.c3} opacity="0.45" />
      </svg>
      {text && <CoverText album={album} color="#fff" />}
    </div>
  );
}

// =========================================================
// HALFTONE — big dot gradient
// =========================================================
function CoverHalftone({ id, album, cov, text }) {
  return (
    <div className="cover-root" style={{ position: "relative", width: "100%", height: "100%", containerType: "inline-size", background: cov.c1, overflow: "hidden" }}>
      <svg viewBox="0 0 100 100" preserveAspectRatio="xMidYMid slice" style={{ position: "absolute", inset: 0, width: "100%", height: "100%" }}>
        <defs>
          <radialGradient id={`bg-${id}`} cx="100%" cy="0%" r="120%">
            <stop offset="0%" stopColor={cov.c2} />
            <stop offset="100%" stopColor={cov.c1} />
          </radialGradient>
          <pattern id={`dots-${id}`} x="0" y="0" width="6" height="6" patternUnits="userSpaceOnUse">
            <circle cx="3" cy="3" r="1.8" fill={cov.c3} />
          </pattern>
          <linearGradient id={`mask-${id}`} x1="0" y1="0" x2="0" y2="1">
            <stop offset="0" stopColor="white" />
            <stop offset="100%" stopColor="black" />
          </linearGradient>
          <mask id={`m-${id}`}>
            <rect width="100" height="100" fill={`url(#mask-${id})`} />
          </mask>
        </defs>
        <rect width="100" height="100" fill={`url(#bg-${id})`} />
        <rect width="100" height="100" fill={`url(#dots-${id})`} mask={`url(#m-${id})`} />
      </svg>
      {text && <CoverText album={album} color={cov.c3} />}
    </div>
  );
}

// =========================================================
// MIRRORBALL — chrome ball with facets
// =========================================================
function CoverMirrorball({ id, album, cov, text }) {
  return (
    <div className="cover-root" style={{ position: "relative", width: "100%", height: "100%", containerType: "inline-size", background: cov.c3, overflow: "hidden" }}>
      <svg viewBox="0 0 100 100" preserveAspectRatio="xMidYMid slice" style={{ position: "absolute", inset: 0, width: "100%", height: "100%" }}>
        <defs>
          <radialGradient id={`bg-${id}`} cx="50%" cy="20%" r="80%">
            <stop offset="0%" stopColor={cov.c2} stopOpacity="0.4" />
            <stop offset="100%" stopColor={cov.c3} />
          </radialGradient>
          <radialGradient id={`ball-${id}`} cx="35%" cy="30%" r="70%">
            <stop offset="0%" stopColor="#ffffff" />
            <stop offset="35%" stopColor={cov.c1} />
            <stop offset="100%" stopColor="#2a1626" />
          </radialGradient>
          <radialGradient id={`glow-${id}`} cx="50%" cy="38%" r="50%">
            <stop offset="0%" stopColor={cov.c2} stopOpacity="0.6" />
            <stop offset="100%" stopColor={cov.c2} stopOpacity="0" />
          </radialGradient>
          <pattern id={`facets-${id}`} x="0" y="0" width="6" height="6" patternUnits="userSpaceOnUse" patternTransform="rotate(45)">
            <rect width="6" height="6" fill="transparent" />
            <line x1="0" y1="0" x2="6" y2="0" stroke="rgba(0,0,0,0.3)" strokeWidth="0.4" />
            <line x1="0" y1="0" x2="0" y2="6" stroke="rgba(255,255,255,0.18)" strokeWidth="0.4" />
          </pattern>
        </defs>
        <rect width="100" height="100" fill={`url(#bg-${id})`} />
        <ellipse cx="50" cy="42" rx="40" ry="40" fill={`url(#glow-${id})`} />
        <circle cx="50" cy="40" r="28" fill={`url(#ball-${id})`} />
        <circle cx="50" cy="40" r="28" fill={`url(#facets-${id})`} />
        <circle cx="42" cy="32" r="6" fill="rgba(255,255,255,0.6)" />
        {/* string */}
        <line x1="50" y1="0" x2="50" y2="12" stroke={cov.c1} strokeWidth="0.6" opacity="0.6" />
      </svg>
      {text && <CoverText album={album} color={cov.c1} />}
    </div>
  );
}

// =========================================================
// GRID — geometric grid / cityscape
// =========================================================
function CoverGrid({ id, album, cov, text }) {
  return (
    <div className="cover-root" style={{ position: "relative", width: "100%", height: "100%", containerType: "inline-size", background: cov.c3, overflow: "hidden" }}>
      <svg viewBox="0 0 100 100" preserveAspectRatio="xMidYMid slice" style={{ position: "absolute", inset: 0, width: "100%", height: "100%" }}>
        <defs>
          <linearGradient id={`g-${id}`} x1="0" y1="0" x2="0" y2="1">
            <stop offset="0%" stopColor={cov.c2} />
            <stop offset="55%" stopColor={cov.c1} />
            <stop offset="100%" stopColor={cov.c3} />
          </linearGradient>
        </defs>
        <rect width="100" height="100" fill={`url(#g-${id})`} />
        {/* perspective grid receding to horizon */}
        <g stroke={cov.c3} strokeWidth="0.4" opacity="0.5">
          {/* horizontals */}
          {[60, 65, 71, 78, 86, 95].map((y, i) => (
            <line key={`h${i}`} x1="0" y1={y} x2="100" y2={y} />
          ))}
          {/* radials */}
          {Array.from({ length: 13 }).map((_, i) => {
            const x = (i / 12) * 100;
            return <line key={`v${i}`} x1={x} y1="100" x2="50" y2="60" />;
          })}
        </g>
        {/* horizon line */}
        <line x1="0" y1="60" x2="100" y2="60" stroke={cov.c2} strokeWidth="0.4" />
        {/* sun */}
        <circle cx="50" cy="40" r="18" fill={cov.c2} opacity="0.5" />
        <circle cx="50" cy="40" r="10" fill={cov.c2} />
      </svg>
      {text && <CoverText album={album} color="#fff" align="left" />}
    </div>
  );
}

// =========================================================
// LIPGLOSS — close-up lips / kiss
// =========================================================
function CoverLipgloss({ id, album, cov, text }) {
  return (
    <div className="cover-root" style={{ position: "relative", width: "100%", height: "100%", containerType: "inline-size", background: cov.c3, overflow: "hidden" }}>
      <svg viewBox="0 0 100 100" preserveAspectRatio="xMidYMid slice" style={{ position: "absolute", inset: 0, width: "100%", height: "100%" }}>
        <defs>
          <radialGradient id={`bg-${id}`} cx="50%" cy="100%" r="80%">
            <stop offset="0%" stopColor={cov.c2} />
            <stop offset="100%" stopColor={cov.c3} />
          </radialGradient>
          <linearGradient id={`lip-${id}`} x1="0" y1="0" x2="0" y2="1">
            <stop offset="0%" stopColor={cov.c1} />
            <stop offset="60%" stopColor={cov.c2} />
            <stop offset="100%" stopColor={cov.c1} />
          </linearGradient>
        </defs>
        <rect width="100" height="100" fill={`url(#bg-${id})`} />
        {/* upper lip */}
        <path d="M 20 50 Q 30 38, 40 46 Q 50 38, 60 46 Q 70 38, 80 50 Q 70 54, 50 54 Q 30 54, 20 50 Z"
              fill={`url(#lip-${id})`} />
        {/* lower lip */}
        <path d="M 20 50 Q 35 70, 50 70 Q 65 70, 80 50 Q 65 60, 50 60 Q 35 60, 20 50 Z"
              fill={cov.c1} />
        {/* highlight */}
        <ellipse cx="45" cy="44" rx="6" ry="1.5" fill="white" opacity="0.5" />
        <ellipse cx="55" cy="62" rx="10" ry="1.2" fill="white" opacity="0.35" />
      </svg>
      {text && <CoverText album={album} color="#fff" />}
    </div>
  );
}

// =========================================================
// NEON — bright text on grid, synthwave
// =========================================================
function CoverNeon({ id, album, cov, text }) {
  return (
    <div className="cover-root" style={{ position: "relative", width: "100%", height: "100%", containerType: "inline-size", background: cov.c3, overflow: "hidden" }}>
      <svg viewBox="0 0 100 100" preserveAspectRatio="xMidYMid slice" style={{ position: "absolute", inset: 0, width: "100%", height: "100%" }}>
        <defs>
          <linearGradient id={`g-${id}`} x1="0" y1="0" x2="0" y2="1">
            <stop offset="0%" stopColor={cov.c3} />
            <stop offset="60%" stopColor={cov.c1} stopOpacity="0.3" />
            <stop offset="100%" stopColor={cov.c2} stopOpacity="0.5" />
          </linearGradient>
        </defs>
        <rect width="100" height="100" fill={`url(#g-${id})`} />
        {/* skyline */}
        <g fill={cov.c3}>
          <rect x="8" y="58" width="6" height="20" />
          <rect x="16" y="50" width="8" height="28" />
          <rect x="26" y="55" width="5" height="23" />
          <rect x="33" y="48" width="9" height="30" />
          <rect x="44" y="52" width="6" height="26" />
          <rect x="52" y="46" width="7" height="32" />
          <rect x="61" y="54" width="6" height="24" />
          <rect x="69" y="50" width="8" height="28" />
          <rect x="79" y="56" width="5" height="22" />
          <rect x="86" y="49" width="8" height="29" />
        </g>
        {/* lit windows */}
        <g fill={cov.c2}>
          {Array.from({ length: 40 }).map((_, i) => (
            <rect key={i} x={10 + (i % 10) * 8.5} y={56 + Math.floor(i / 10) * 5} width="1.5" height="1.5" opacity={(i % 7) ? 0.9 : 0.3} />
          ))}
        </g>
        {/* grid floor */}
        <g stroke={cov.c1} strokeWidth="0.3" opacity="0.7">
          {[80, 84, 88, 93, 100].map((y, i) => (
            <line key={i} x1="0" y1={y} x2="100" y2={y} />
          ))}
          {Array.from({ length: 9 }).map((_, i) => {
            const x = (i / 8) * 100;
            return <line key={`v${i}`} x1={x} y1="100" x2="50" y2="78" />;
          })}
        </g>
      </svg>
      {text && <CoverText album={album} color={cov.c2} />}
    </div>
  );
}

// =========================================================
// STRIPES — diagonal stripes
// =========================================================
function CoverStripes({ id, album, cov, text }) {
  return (
    <div className="cover-root" style={{ position: "relative", width: "100%", height: "100%", containerType: "inline-size", background: cov.c3, overflow: "hidden" }}>
      <svg viewBox="0 0 100 100" preserveAspectRatio="xMidYMid slice" style={{ position: "absolute", inset: 0, width: "100%", height: "100%" }}>
        <defs>
          <pattern id={`stripes-${id}`} x="0" y="0" width="14" height="14" patternUnits="userSpaceOnUse" patternTransform="rotate(45)">
            <rect width="14" height="14" fill={cov.c1} />
            <rect y="7" width="14" height="7" fill={cov.c2} />
          </pattern>
        </defs>
        <rect width="100" height="100" fill={`url(#stripes-${id})`} />
        {/* circle frame */}
        <circle cx="50" cy="50" r="34" fill={cov.c3} />
        <circle cx="50" cy="50" r="32" fill="none" stroke={cov.c2} strokeWidth="0.6" />
      </svg>
      {text && (
        <div className="cover-text" style={{
          position: "absolute",
          inset: 0,
          display: "flex",
          flexDirection: "column",
          justifyContent: "center",
          alignItems: "center",
          padding: "8%",
          pointerEvents: "none",
          textAlign: "center",
        }}>
          <div style={{
            fontFamily: "var(--f-display)",
            fontWeight: 900,
            fontSize: "11cqw",
            lineHeight: 0.9,
            textTransform: "uppercase",
            color: cov.c2,
            letterSpacing: "0.005em",
            textWrap: "balance",
          }}>{album.title}</div>
          <div style={{
            fontFamily: "var(--f-body)",
            fontSize: "4.5cqw",
            fontWeight: 600,
            color: cov.c2,
            opacity: 0.8,
            marginTop: "5%",
            letterSpacing: "0.1em",
            textTransform: "uppercase",
          }}>{album.artist}</div>
        </div>
      )}
    </div>
  );
}

// =========================================================
// PORTRAIT — silhouette of a singer
// =========================================================
function CoverPortrait({ id, album, cov, text }) {
  return (
    <div className="cover-root" style={{ position: "relative", width: "100%", height: "100%", containerType: "inline-size", background: cov.c3, overflow: "hidden" }}>
      <svg viewBox="0 0 100 100" preserveAspectRatio="xMidYMid slice" style={{ position: "absolute", inset: 0, width: "100%", height: "100%" }}>
        <defs>
          <radialGradient id={`bg-${id}`} cx="60%" cy="40%" r="70%">
            <stop offset="0%" stopColor={cov.c1} />
            <stop offset="100%" stopColor={cov.c3} />
          </radialGradient>
          <linearGradient id={`sil-${id}`} x1="0" y1="0" x2="0" y2="1">
            <stop offset="0%" stopColor={cov.c2} />
            <stop offset="100%" stopColor={cov.c3} />
          </linearGradient>
        </defs>
        <rect width="100" height="100" fill={`url(#bg-${id})`} />
        {/* afro silhouette */}
        <path d="M 50 18
                 C 36 18, 27 28, 28 40
                 C 22 42, 22 50, 27 53
                 C 30 60, 38 64, 50 64
                 C 62 64, 70 60, 73 53
                 C 78 50, 78 42, 72 40
                 C 73 28, 64 18, 50 18 Z"
              fill={`url(#sil-${id})`} />
        {/* neck + shoulders */}
        <path d="M 42 60 L 42 70 C 30 73, 18 80, 14 100 L 86 100 C 82 80, 70 73, 58 70 L 58 60 Z" fill={`url(#sil-${id})`} />
        {/* hoop earring */}
        <circle cx="74" cy="48" r="3" fill="none" stroke={cov.c2} strokeWidth="0.6" />
      </svg>
      {text && (
        <div className="cover-text" style={{
          position: "absolute",
          top: "6%", left: "8%", right: "8%",
          pointerEvents: "none",
        }}>
          <div style={{
            fontFamily: "var(--f-display)",
            fontWeight: 900,
            fontSize: "11cqw",
            lineHeight: 0.85,
            textTransform: "uppercase",
            color: cov.c2,
            letterSpacing: "0.005em",
          }}>{album.title}</div>
        </div>
      )}
    </div>
  );
}

// =========================================================
// SUNSET — palm trees + sunset
// =========================================================
function CoverSunset({ id, album, cov, text }) {
  return (
    <div className="cover-root" style={{ position: "relative", width: "100%", height: "100%", containerType: "inline-size", background: cov.c3, overflow: "hidden" }}>
      <svg viewBox="0 0 100 100" preserveAspectRatio="xMidYMid slice" style={{ position: "absolute", inset: 0, width: "100%", height: "100%" }}>
        <defs>
          <linearGradient id={`sky-${id}`} x1="0" y1="0" x2="0" y2="1">
            <stop offset="0%" stopColor={cov.c2} />
            <stop offset="50%" stopColor={cov.c1} />
            <stop offset="100%" stopColor={cov.c3} />
          </linearGradient>
          <radialGradient id={`sun-${id}`} cx="50%" cy="50%" r="50%">
            <stop offset="0%" stopColor="#ffe8b0" />
            <stop offset="100%" stopColor={cov.c2} />
          </radialGradient>
        </defs>
        <rect width="100" height="100" fill={`url(#sky-${id})`} />
        <circle cx="50" cy="58" r="22" fill={`url(#sun-${id})`} />
        {/* stripes through sun */}
        <g fill={cov.c3}>
          <rect x="0" y="60" width="100" height="1.6" />
          <rect x="0" y="64" width="100" height="1.6" />
          <rect x="0" y="68" width="100" height="1.6" />
          <rect x="0" y="72" width="100" height="1.6" />
          <rect x="0" y="76" width="100" height="2" />
        </g>
        {/* palm */}
        <g fill={cov.c3}>
          <rect x="12" y="55" width="1.6" height="45" />
          <path d="M 13 55 Q 5 50, 0 54 Q 7 56, 13 58 Z" />
          <path d="M 13 55 Q 22 48, 28 52 Q 20 56, 13 58 Z" />
          <path d="M 13 55 Q 8 45, 12 38 Q 16 46, 14 58 Z" />
          <path d="M 13 55 Q 20 46, 26 40 Q 18 50, 14 58 Z" />
        </g>
        <g fill={cov.c3}>
          <rect x="88" y="62" width="1.4" height="38" />
          <path d="M 89 62 Q 96 58, 100 60 Q 94 62, 89 64 Z" />
          <path d="M 89 62 Q 82 55, 78 58 Q 84 62, 89 64 Z" />
          <path d="M 89 62 Q 86 53, 90 47 Q 92 54, 90 64 Z" />
        </g>
      </svg>
      {text && (
        <div className="cover-text" style={{
          position: "absolute",
          inset: 0,
          display: "flex",
          flexDirection: "column",
          justifyContent: "flex-end",
          alignItems: "center",
          padding: "8%",
          pointerEvents: "none",
          textAlign: "center",
        }}>
          <div style={{
            fontFamily: "var(--f-display)",
            fontWeight: 900,
            fontSize: "11cqw",
            lineHeight: 0.9,
            textTransform: "uppercase",
            color: cov.c3,
            letterSpacing: "0.005em",
          }}>{album.title}</div>
          <div style={{
            fontFamily: "var(--f-body)",
            fontSize: "4.5cqw",
            fontWeight: 600,
            color: cov.c3,
            opacity: 0.8,
            marginTop: "3%",
            letterSpacing: "0.1em",
            textTransform: "uppercase",
          }}>{album.artist}</div>
        </div>
      )}
    </div>
  );
}

// =========================================================
// DIAMOND — facet diamond shape
// =========================================================
function CoverDiamond({ id, album, cov, text }) {
  return (
    <div className="cover-root" style={{ position: "relative", width: "100%", height: "100%", containerType: "inline-size", background: cov.c3, overflow: "hidden" }}>
      <svg viewBox="0 0 100 100" preserveAspectRatio="xMidYMid slice" style={{ position: "absolute", inset: 0, width: "100%", height: "100%" }}>
        <defs>
          <linearGradient id={`bg-${id}`} x1="0" y1="0" x2="0" y2="1">
            <stop offset="0%" stopColor={cov.c3} />
            <stop offset="100%" stopColor={cov.c1} stopOpacity="0.4" />
          </linearGradient>
          <linearGradient id={`d1-${id}`} x1="0" y1="0" x2="1" y2="1">
            <stop offset="0%" stopColor={cov.c2} />
            <stop offset="100%" stopColor={cov.c1} />
          </linearGradient>
          <linearGradient id={`d2-${id}`} x1="0" y1="0" x2="1" y2="1">
            <stop offset="0%" stopColor={cov.c1} />
            <stop offset="100%" stopColor={cov.c3} />
          </linearGradient>
        </defs>
        <rect width="100" height="100" fill={`url(#bg-${id})`} />
        {/* diamond shape: split into facets */}
        <g transform="translate(50 40)">
          <polygon points="0,-26 -22,-6 0,-12" fill={`url(#d1-${id})`} />
          <polygon points="0,-26 22,-6 0,-12" fill={cov.c2} opacity="0.7" />
          <polygon points="-22,-6 0,-12 -8,0" fill={cov.c1} />
          <polygon points="22,-6 0,-12 8,0" fill={`url(#d2-${id})`} />
          <polygon points="-22,-6 -8,0 0,30" fill={cov.c2} opacity="0.85" />
          <polygon points="22,-6 8,0 0,30" fill={cov.c1} />
          <polygon points="-8,0 8,0 0,30" fill={`url(#d1-${id})`} />
          {/* outline */}
          <polygon points="0,-26 22,-6 0,30 -22,-6" fill="none" stroke={cov.c2} strokeWidth="0.4" />
        </g>
        {/* sparkles */}
        <g fill={cov.c2}>
          <circle cx="22" cy="22" r="0.8" />
          <circle cx="76" cy="28" r="1.2" />
          <circle cx="82" cy="74" r="0.8" />
          <circle cx="18" cy="80" r="1" />
        </g>
      </svg>
      {text && (
        <div className="cover-text" style={{
          position: "absolute",
          inset: 0,
          display: "flex",
          flexDirection: "column",
          justifyContent: "flex-end",
          alignItems: "center",
          padding: "8%",
          pointerEvents: "none",
          textAlign: "center",
        }}>
          <div style={{
            fontFamily: "var(--f-display)",
            fontWeight: 900,
            fontSize: "11cqw",
            lineHeight: 0.9,
            textTransform: "uppercase",
            color: cov.c2,
            letterSpacing: "0.005em",
          }}>{album.title}</div>
          <div style={{
            fontFamily: "var(--f-body)",
            fontSize: "4.5cqw",
            fontWeight: 600,
            color: cov.c2,
            opacity: 0.7,
            marginTop: "3%",
            letterSpacing: "0.1em",
            textTransform: "uppercase",
          }}>{album.artist}</div>
        </div>
      )}
    </div>
  );
}

// Make components global for use in other Babel files
Object.assign(window, { Cover });
