// NekoShop — pages: Home, Category, Browse, Product detail

const { useState: useStateP, useEffect: useEffectP, useMemo: useMemoP, useRef: useRefP } = React;

// ──────────────────────────────────────────────────────────────────
// HOME
// ──────────────────────────────────────────────────────────────────
function HomePage() {
  const { go } = useRouter();
  const { currency } = useCurrency();
  const [q, setQ] = useStateP("");
  const data = window.NEKO_DATA;
  const recent = data.products.slice(0, 7);
  const flagship = data.products.find(p => p.id === "nekoclubkit");
  const featured = ["nekodono-v3", "nekovip-gift-advanced", "nekodj-v4", "nekocarry-v6", "avabooth-v2", "nekovip-nekodono-v3", "advance-confessionboard", "nekocampro"].map(id => data.products.find(p => p.id === id));

  return (
    <main>
      {/* HERO */}
      <section style={{ paddingTop: 88, paddingBottom: 56 }}>
        <div className="container" style={{ display: "grid", gridTemplateColumns: "1.15fr 1fr", gap: 80, alignItems: "center" }}>
          <div>
            <div style={{ fontFamily: "var(--mono)", fontSize: 11, letterSpacing: "0.16em", textTransform: "uppercase", color: "var(--muted)", marginBottom: 24, display: "flex", alignItems: "center", gap: 10 }}>
              <span style={{ width: 24, height: 1, background: "var(--muted)" }} />
              Marketplace — Roblox systems
            </div>
            <h1 style={{
              fontSize: "clamp(48px, 6vw, 76px)", lineHeight: 1.02, letterSpacing: "-0.035em",
              margin: 0, fontWeight: 600,
            }}>
              <em style={{ fontStyle: "normal", color: "var(--accent)" }}>NoMen</em> Roblox
              <br />paid systems.
            </h1>
            <p style={{ fontSize: 17, color: "var(--muted)", lineHeight: 1.55, marginTop: 28, maxWidth: 460 }}>
              Donation flows, VIP gifting, synced DJ booths, club kits — production-ready modules built in Jakarta. Install today, ship tonight.
            </p>
            <div style={{ display: "flex", gap: 12, marginTop: 36 }}>
              <Btn primary onClick={() => go("/browse")}>Browse the catalog →</Btn>
              <Btn onClick={() => go("/p/nekoclubkit")}>See the flagship</Btn>
            </div>
            <div style={{ display: "flex", gap: 28, marginTop: 56, color: "var(--muted)", fontSize: 12, fontFamily: "var(--mono)", letterSpacing: "0.06em", textTransform: "uppercase" }}>
              <span>24 systems</span>
              <span style={{ width: 1, background: "var(--border)" }} />
              <span>QRIS + PayPal</span>
              <span style={{ width: 1, background: "var(--border)" }} />
              <span>Lifetime updates</span>
            </div>
          </div>

          {/* Hero composition: real youtube thumb of NekoClubKit + supporting card */}
          <div style={{ position: "relative", minHeight: 440 }}>
            <div style={{ position: "absolute", top: 0, right: 0, width: "82%", borderRadius: 4, overflow: "hidden", background: "var(--ink)", aspectRatio: "16/9" }}>
              <img src="https://i.ytimg.com/vi/jsidSfpK6NI/hqdefault.jpg" alt="NekoClubKit" style={{ width: "100%", height: "100%", objectFit: "cover", display: "block" }} />
              <div style={{ position: "absolute", inset: 0, display: "flex", alignItems: "center", justifyContent: "center", background: "rgba(0,0,0,0.15)" }}>
                <div style={{ width: 64, height: 64, borderRadius: "50%", background: "var(--bg)", display: "flex", alignItems: "center", justifyContent: "center", boxShadow: "0 12px 36px rgba(0,0,0,0.35)" }}>
                  <svg width="22" height="22" viewBox="0 0 22 22"><polygon points="6,3 6,19 19,11" fill="var(--accent)" /></svg>
                </div>
              </div>
            </div>
            <div style={{ position: "absolute", bottom: 0, left: 0, width: "55%", boxShadow: "0 20px 60px rgba(0,0,0,0.10)", borderRadius: 4, overflow: "hidden", background: "var(--bg)", border: "1px solid var(--border)", aspectRatio: "16/9" }}>
              <img src="https://i.ytimg.com/vi/1Qi298pUcxU/hqdefault.jpg" alt="NekoVIP" style={{ width: "100%", height: "100%", objectFit: "cover", display: "block" }} />
            </div>
            <div style={{ position: "absolute", top: "44%", right: "-3%", background: "var(--ink)", color: "var(--bg)", padding: "14px 18px", borderRadius: 4, fontFamily: "var(--mono)", fontSize: 11, letterSpacing: "0.04em", boxShadow: "0 16px 40px rgba(0,0,0,0.18)" }}>
              <div style={{ color: "oklch(0.7 0.005 60)", marginBottom: 6, fontSize: 10 }}>NOW INSTALLED</div>
              <div style={{ display: "flex", alignItems: "center", gap: 8 }}>
                <PixelCat size={18} inkColor="var(--bg)" accentColor="var(--accent)" />
                NekoClubKit v1.2
              </div>
            </div>
          </div>
        </div>
      </section>

      {/* SEARCH */}
      <section style={{ paddingTop: 12, paddingBottom: 48 }}>
        <div className="container">
          <div style={{
            display: "flex", alignItems: "center", gap: 14, padding: "16px 22px",
            border: "1px solid var(--border)", borderRadius: 999, background: "var(--bg)",
            transition: "border-color 160ms",
          }}>
            <svg width="18" height="18" viewBox="0 0 18 18" fill="none" stroke="var(--muted)" strokeWidth="1.5"><circle cx="8" cy="8" r="5.5"/><path d="M12 12l4 4"/></svg>
            <input
              value={q}
              onChange={(e) => setQ(e.target.value)}
              placeholder="Search for systems — try ‘DJ’, ‘donation’, ‘camera’…"
              onKeyDown={(e) => { if (e.key === "Enter" && q) go("/browse"); }}
              style={{ flex: 1, border: "none", outline: "none", fontFamily: "var(--sans)", fontSize: 15, background: "transparent", color: "var(--ink)" }}
            />
            <kbd style={{ fontFamily: "var(--mono)", fontSize: 11, color: "var(--muted)", border: "1px solid var(--border)", borderRadius: 4, padding: "3px 7px" }}>↵</kbd>
          </div>
          {q && (
            <div style={{ marginTop: 16, display: "flex", flexDirection: "column", gap: 4 }}>
              {data.products
                .filter(p => p.name.toLowerCase().includes(q.toLowerCase()))
                .slice(0, 5)
                .map(p => (
                  <button key={p.id} onClick={() => go(`/p/${p.id}`)} style={{
                    display: "flex", alignItems: "center", justifyContent: "space-between",
                    padding: "10px 18px", border: "1px solid var(--border)", borderRadius: 999,
                    background: "var(--bg)", cursor: "pointer", textAlign: "left",
                  }}>
                    <span style={{ fontSize: 14, fontWeight: 500 }}>{p.name}</span>
                    <PriceLabel product={p} currency={currency} size="sm" />
                  </button>
                ))}
            </div>
          )}
        </div>
      </section>

      {/* CATEGORIES */}
      <section style={{ paddingTop: 32, paddingBottom: 24 }}>
        <div className="container">
          <SectionHeading kicker="01 — Categories" title="Browse by what it does." />
          <div style={{ display: "grid", gridTemplateColumns: "repeat(4, 1fr)", gap: 12 }}>
            {Object.values(data.cats).map((c, i) => (
              <button key={c.id} onClick={() => go(`/c/${c.id}`)} style={{
                textAlign: "left", padding: "22px 22px 22px", border: "1px solid var(--border)",
                borderRadius: 6, background: "var(--bg)", cursor: "pointer", color: "var(--ink)",
                display: "flex", flexDirection: "column", justifyContent: "space-between",
                minHeight: 130,
                transition: "border-color 140ms, background 140ms",
              }}
              onMouseEnter={(e)=>{e.currentTarget.style.borderColor="var(--ink)";}}
              onMouseLeave={(e)=>{e.currentTarget.style.borderColor="var(--border)";}}>
                <div>
                  <div style={{ fontFamily: "var(--mono)", fontSize: 10, color: "var(--muted)", letterSpacing: "0.08em", marginBottom: 8 }}>
                    {String(i+1).padStart(2,"0")} / {String(Object.values(data.cats).length).padStart(2,"0")}
                  </div>
                  <div style={{ fontSize: 16, fontWeight: 600, letterSpacing: "-0.01em" }}>{c.label}</div>
                  <div style={{ fontSize: 11, color: "var(--muted)", marginTop: 2, fontStyle: "italic" }}>{c.id_label}</div>
                </div>
                <div style={{ display: "flex", justifyContent: "space-between", alignItems: "flex-end", marginTop: 18 }}>
                  <span style={{ fontSize: 12, color: "var(--muted)" }}>
                    {data.products.filter(p => p.cat === c.id).length} systems
                  </span>
                  <span style={{ color: "var(--accent)", fontSize: 18 }}>→</span>
                </div>
              </button>
            ))}
          </div>
        </div>
      </section>

      {/* FLAGSHIP — NekoClubKit */}
      {flagship && (
        <section style={{ paddingTop: 80, paddingBottom: 32 }}>
          <div className="container">
            <SectionHeading kicker="02 — Flagship" title="The whole club, packaged." />
            <div style={{
              display: "grid", gridTemplateColumns: "1.2fr 1fr", gap: 48, alignItems: "center",
              background: "var(--ink)", color: "var(--bg)", borderRadius: 8, padding: 48,
            }}>
              <div>
                <div style={{ fontFamily: "var(--mono)", fontSize: 10, letterSpacing: "0.1em", textTransform: "uppercase", color: "oklch(0.7 0.005 60)", marginBottom: 18 }}>
                  Bundled — paket bundel — flagship
                </div>
                <h3 style={{ fontSize: 44, fontWeight: 600, letterSpacing: "-0.025em", margin: 0, lineHeight: 1.05 }}>
                  {flagship.name}
                </h3>
                <p style={{ fontSize: 15, color: "oklch(0.78 0.005 60)", marginTop: 20, maxWidth: 460, lineHeight: 1.5 }}>
                  {flagship.tagline}
                </p>
                <ul style={{ listStyle: "none", padding: 0, margin: "28px 0 0", display: "grid", gridTemplateColumns: "1fr 1fr", gap: "8px 24px" }}>
                  {flagship.features.map(f => (
                    <li key={f} style={{ fontSize: 13, color: "oklch(0.85 0.005 60)", display: "flex", gap: 8, alignItems: "flex-start" }}>
                      <span style={{ color: "var(--accent)", fontFamily: "var(--mono)" }}>+</span> {f}
                    </li>
                  ))}
                </ul>
                <div style={{ display: "flex", alignItems: "center", gap: 24, marginTop: 36 }}>
                  <PriceLabel product={flagship} currency={currency} size="lg" color="var(--bg)" />
                  <Btn primary onClick={() => go(`/p/${flagship.id}`)} style={{ background: "var(--accent)", borderColor: "var(--accent)", color: "white" }}>
                    Buy NekoClubKit →
                  </Btn>
                </div>
              </div>
              <div style={{ position: "relative" }}>
                <div style={{ position: "relative", aspectRatio: "16/9", borderRadius: 4, overflow: "hidden", background: "black" }}>
                  <img src={flagship.video && flagship.video.kind === "youtube" ? `https://i.ytimg.com/vi/${flagship.video.id}/hqdefault.jpg` : ""} alt="" style={{ width: "100%", height: "100%", objectFit: "cover", display: "block" }} />
                  <div style={{ position: "absolute", inset: 0, display: "flex", alignItems: "center", justifyContent: "center", background: "rgba(0,0,0,0.18)" }}>
                    <div style={{ width: 56, height: 56, borderRadius: "50%", background: "var(--accent)", display: "flex", alignItems: "center", justifyContent: "center" }}>
                      <svg width="20" height="20" viewBox="0 0 22 22"><polygon points="6,3 6,19 19,11" fill="white" /></svg>
                    </div>
                  </div>
                  <div style={{ position: "absolute", top: 12, left: 12, fontFamily: "var(--mono)", fontSize: 10, letterSpacing: "0.06em", textTransform: "uppercase", color: "white", background: "var(--accent)", padding: "3px 7px", borderRadius: 2 }}>Flagship</div>
                </div>
              </div>
            </div>
          </div>
        </section>
      )}

      {/* RECENTLY UPDATED — horizontal */}
      <section style={{ paddingTop: 80, paddingBottom: 24 }}>
        <div className="container">
          <SectionHeading kicker="03 — Recently updated" title="Fresh drops from the studio." action={<button onClick={() => go("/browse")} style={{ background: "transparent", border: "none", cursor: "pointer", fontSize: 13, color: "var(--muted)", fontFamily: "var(--sans)" }}>View all →</button>} />
          <div style={{ display: "grid", gridTemplateColumns: "repeat(4, 1fr)", gap: 16 }}>
            {recent.slice(0, 4).map(p => <ProductCard key={p.id} product={p} onClick={() => go(`/p/${p.id}`)} />)}
          </div>
        </div>
      </section>

      {/* MOST POPULAR */}
      <section style={{ paddingTop: 64, paddingBottom: 24 }}>
        <div className="container">
          <SectionHeading kicker="04 — Most popular" title="What communities are buying." />
          <div style={{ display: "grid", gridTemplateColumns: "repeat(4, 1fr)", gap: 16 }}>
            {featured.slice(0, 8).map(p => <ProductCard key={p.id} product={p} onClick={() => go(`/p/${p.id}`)} />)}
          </div>
        </div>
      </section>

      {/* HOW PAYMENT WORKS */}
      <section style={{ paddingTop: 96, paddingBottom: 24 }}>
        <div className="container">
          <SectionHeading kicker="05 — Pay your way" title="QRIS, PayPal, or Robux gamepass." />
          <div style={{ display: "grid", gridTemplateColumns: "repeat(3, 1fr)", gap: 16 }}>
            <PaymentMethodCard
              title="QRIS"
              subtitle="Bayar dengan e-wallet Indonesia"
              body="Scan dengan GoPay, OVO, DANA, ShopeePay, LinkAja atau aplikasi mobile banking. Instan, no fees."
              icon={<QrisIcon />}
            />
            <PaymentMethodCard
              title="PayPal"
              subtitle="International orders"
              body="Send to Dancerblade70@gmail.com (Sufrina Nofrianti · @mightyxeno). Friends &amp; Family preferred."
              icon={<PaypalIcon />}
            />
            <PaymentMethodCard
              title="Robux"
              subtitle="Pay inside Roblox"
              body="Buy the matching gamepass on our Roblox storefront. Prices include the 30% marketplace fee."
              icon={<RobuxIcon />}
            />
          </div>
        </div>
      </section>
    </main>
  );
}

function SectionHeading({ kicker, title, action }) {
  return (
    <div style={{ display: "flex", alignItems: "flex-end", justifyContent: "space-between", marginBottom: 28 }}>
      <div>
        <div style={{ fontFamily: "var(--mono)", fontSize: 11, letterSpacing: "0.12em", textTransform: "uppercase", color: "var(--muted)", marginBottom: 12 }}>{kicker}</div>
        <h2 style={{ fontSize: 30, fontWeight: 600, letterSpacing: "-0.02em", margin: 0 }}>{title}</h2>
      </div>
      {action}
    </div>
  );
}

function PaymentMethodCard({ title, subtitle, body, icon }) {
  return (
    <div style={{
      padding: 32, border: "1px solid var(--border)", borderRadius: 6, background: "var(--bg)",
      display: "flex", gap: 24, alignItems: "flex-start",
    }}>
      <div style={{ width: 56, height: 56, border: "1px solid var(--border)", borderRadius: 6, display: "flex", alignItems: "center", justifyContent: "center", flexShrink: 0 }}>
        {icon}
      </div>
      <div>
        <div style={{ fontWeight: 600, fontSize: 18, letterSpacing: "-0.01em" }}>{title}</div>
        <div style={{ fontSize: 12, color: "var(--muted)", marginTop: 2, fontStyle: "italic" }}>{subtitle}</div>
        <p style={{ fontSize: 13, color: "var(--muted)", lineHeight: 1.6, marginTop: 12, marginBottom: 0 }} dangerouslySetInnerHTML={{ __html: body }} />
      </div>
    </div>
  );
}
function QrisIcon() {
  // simple QR-ish glyph
  return (
    <svg width="28" height="28" viewBox="0 0 14 14" shapeRendering="crispEdges">
      {[[0,0,3,3],[11,0,3,3],[0,11,3,3],[5,5,1,1],[7,5,2,2],[5,8,2,2],[8,8,1,1],[10,8,1,1],[10,10,1,1]].map((r,i) => <rect key={i} x={r[0]} y={r[1]} width={r[2]} height={r[3]} fill="var(--ink)" />)}
      {[[1,1,1,1],[12,1,1,1],[1,12,1,1]].map((r,i) => <rect key={"i"+i} x={r[0]} y={r[1]} width={r[2]} height={r[3]} fill="var(--bg)" />)}
    </svg>
  );
}
function PaypalIcon() {
  return (
    <svg width="28" height="28" viewBox="0 0 24 24" fill="none">
      <path d="M7 19l1.5-10h5c2 0 3.5 1.5 3 3.5C16 15 14 16 12 16h-2l-.5 3H7z" fill="var(--ink)"/>
      <path d="M9 17l1.5-10h5c2 0 3.5 1.5 3 3.5C18 13 16 14 14 14h-2l-.5 3H9z" fill="var(--accent)" opacity="0.75"/>
    </svg>
  );
}
function RobuxIcon() {
  return (
    <svg width="28" height="28" viewBox="0 0 24 24">
      <rect x="3" y="3" width="18" height="18" rx="3" transform="rotate(8 12 12)" fill="var(--ink)" />
      <text x="12" y="16.5" textAnchor="middle" fontSize="10" fontWeight="700" fill="var(--bg)" fontFamily="JetBrains Mono, monospace">R$</text>
    </svg>
  );
}
Object.assign(window, { QrisIcon, PaypalIcon, RobuxIcon });

// ──────────────────────────────────────────────────────────────────
// BROWSE — all products with filters
// ──────────────────────────────────────────────────────────────────
function BrowsePage({ initialCat }) {
  const { go } = useRouter();
  const [cat, setCat] = useStateP(initialCat || "all");
  const [sort, setSort] = useStateP("popular");
  const [q, setQ] = useStateP("");
  const data = window.NEKO_DATA;

  const filtered = useMemoP(() => {
    let r = data.products;
    if (cat !== "all") r = r.filter(p => p.cat === cat);
    if (q) r = r.filter(p => p.name.toLowerCase().includes(q.toLowerCase()));
    if (sort === "price-low") r = [...r].sort((a,b) => a.price.idr - b.price.idr);
    if (sort === "price-high") r = [...r].sort((a,b) => b.price.idr - a.price.idr);
    if (sort === "name") r = [...r].sort((a,b) => a.name.localeCompare(b.name));
    return r;
  }, [cat, sort, q]);

  const catObj = cat === "all" ? null : data.cats[cat];

  return (
    <main className="container" style={{ paddingTop: 56, paddingBottom: 32 }}>
      {/* Breadcrumb */}
      <div style={{ fontFamily: "var(--mono)", fontSize: 11, color: "var(--muted)", letterSpacing: "0.06em", textTransform: "uppercase", marginBottom: 28 }}>
        <a href="#/" style={{ color: "var(--muted)", textDecoration: "none" }} onClick={(e)=>{e.preventDefault();go("/");}}>NekoShop</a>
        <span style={{ margin: "0 8px" }}>/</span>
        <span style={{ color: "var(--ink)" }}>{catObj ? catObj.label : "All systems"}</span>
      </div>

      {/* Header */}
      <div style={{ display: "flex", justifyContent: "space-between", alignItems: "flex-end", marginBottom: 32 }}>
        <div>
          <h1 style={{ fontSize: 44, fontWeight: 600, letterSpacing: "-0.025em", margin: 0, lineHeight: 1.05 }}>
            {catObj ? catObj.label : "All systems"}
          </h1>
          <p style={{ fontSize: 15, color: "var(--muted)", marginTop: 12, marginBottom: 0, maxWidth: 540 }}>
            {catObj ? catObj.blurb : "24 systems, all from NekoStudio. Pick what you need, pay with QRIS or PayPal."}
          </p>
        </div>
        <div style={{ fontFamily: "var(--mono)", fontSize: 12, color: "var(--muted)" }}>{filtered.length} results</div>
      </div>

      {/* Controls */}
      <div style={{ display: "flex", gap: 12, marginBottom: 28, paddingBottom: 24, borderBottom: "1px solid var(--border)", flexWrap: "wrap" }}>
        <div style={{ display: "flex", gap: 6, flexWrap: "wrap" }}>
          <FilterChip active={cat==="all"} onClick={() => setCat("all")}>All</FilterChip>
          {Object.values(data.cats).map(c => (
            <FilterChip key={c.id} active={cat===c.id} onClick={() => setCat(c.id)}>{c.label}</FilterChip>
          ))}
        </div>
        <div style={{ flex: 1 }} />
        <div style={{ display: "flex", alignItems: "center", gap: 8 }}>
          <input
            value={q}
            onChange={(e) => setQ(e.target.value)}
            placeholder="Search…"
            style={{
              padding: "8px 14px", border: "1px solid var(--border)", borderRadius: 999,
              fontFamily: "var(--sans)", fontSize: 13, outline: "none", background: "var(--bg)", color: "var(--ink)",
              width: 200,
            }}
          />
          <select value={sort} onChange={(e) => setSort(e.target.value)} style={{
            padding: "8px 14px", border: "1px solid var(--border)", borderRadius: 999,
            fontFamily: "var(--sans)", fontSize: 13, background: "var(--bg)", color: "var(--ink)", cursor: "pointer",
          }}>
            <option value="popular">Most popular</option>
            <option value="price-low">Price ↑</option>
            <option value="price-high">Price ↓</option>
            <option value="name">Name A–Z</option>
          </select>
        </div>
      </div>

      {/* Grid */}
      {filtered.length === 0 ? (
        <div style={{ padding: 80, textAlign: "center", color: "var(--muted)", fontFamily: "var(--mono)", fontSize: 12 }}>
          NOTHING MATCHES — try clearing filters.
        </div>
      ) : (
        <div style={{ display: "grid", gridTemplateColumns: "repeat(4, 1fr)", gap: 16 }}>
          {filtered.map(p => <ProductCard key={p.id} product={p} onClick={() => go(`/p/${p.id}`)} />)}
        </div>
      )}
    </main>
  );
}

function FilterChip({ active, onClick, children }) {
  return (
    <button onClick={onClick} style={{
      padding: "7px 14px", border: "1px solid", borderColor: active ? "var(--ink)" : "var(--border)",
      borderRadius: 999, background: active ? "var(--ink)" : "var(--bg)", color: active ? "var(--bg)" : "var(--ink)",
      fontFamily: "var(--sans)", fontSize: 13, fontWeight: 500, cursor: "pointer",
      transition: "all 120ms",
    }}>
      {children}
    </button>
  );
}

// ──────────────────────────────────────────────────────────────────
// PRODUCT DETAIL
// ──────────────────────────────────────────────────────────────────
// Media block: YouTube video, single image, or an image gallery w/ thumbs.
function ProductMedia({ p, hue }) {
  const gallery = Array.isArray(p.images) && p.images.length > 0 ? p.images : null;
  const [active, setActive] = useStateP(0);
  const ytId = p.video && p.video.kind === "youtube" ? p.video.id : undefined;

  // Plain YouTube or single-image (no gallery) → existing VideoPlayer handles it
  if (!gallery) {
    return (
      <VideoPlayer
        key={p.id+"-vid"}
        videoId={ytId}
        imageSrc={p.image}
        hue={hue}
        label={`${p.name} — trailer`}
        note={p.video && p.video.kind === "mediafire" ? "mediafire preview — click below" : "no preview available"}
      />
    );
  }

  // Image gallery — big image + thumbnail strip
  return (
    <div>
      <div style={{ position: "relative", width: "100%", aspectRatio: "16/9", borderRadius: 4, overflow: "hidden", background: "var(--ink)" }}>
        <img src={gallery[active]} alt={`${p.name} screenshot ${active+1}`} style={{ position: "absolute", inset: 0, width: "100%", height: "100%", objectFit: "cover" }} />
      </div>
      <div style={{ display: "flex", gap: 10, marginTop: 12 }}>
        {gallery.map((src, i) => (
          <button key={src} onClick={() => setActive(i)} style={{
            flex: "0 0 auto", width: 96, aspectRatio: "16/10", padding: 0, cursor: "pointer",
            border: "2px solid", borderColor: i === active ? "var(--accent)" : "var(--border)",
            borderRadius: 4, overflow: "hidden", background: "var(--ink)",
            transition: "border-color 140ms",
          }}>
            <img src={src} alt="" style={{ width: "100%", height: "100%", objectFit: "cover", display: "block" }} />
          </button>
        ))}
      </div>
    </div>
  );
}

function ProductPage({ id }) {
  const { go } = useRouter();
  const { currency } = useCurrency();
  const { add } = useCart();
  const data = window.NEKO_DATA;
  const p = data.products.find(x => x.id === id);
  const [added, setAdded] = useStateP(false);

  if (!p) {
    return (
      <main className="container" style={{ paddingTop: 96, paddingBottom: 96, textAlign: "center" }}>
        <h1 style={{ fontSize: 32, fontWeight: 600 }}>Not found</h1>
        <p style={{ color: "var(--muted)" }}>That system doesn't exist anymore. Try browsing.</p>
        <Btn onClick={() => go("/browse")}>← Back to browse</Btn>
      </main>
    );
  }
  const cat = data.cats[p.cat];
  const hue = data.hue[p.cat];
  const related = data.products.filter(x => x.cat === p.cat && x.id !== p.id).slice(0, 4);

  const handleAdd = () => {
    add(p.id, 1);
    setAdded(true);
    setTimeout(() => setAdded(false), 1600);
  };

  return (
    <main className="container" style={{ paddingTop: 40, paddingBottom: 32 }}>
      {/* Breadcrumb */}
      <div style={{ fontFamily: "var(--mono)", fontSize: 11, color: "var(--muted)", letterSpacing: "0.06em", textTransform: "uppercase", marginBottom: 28 }}>
        <a href="#/" style={{ color: "var(--muted)", textDecoration: "none" }} onClick={(e)=>{e.preventDefault();go("/");}}>NekoShop</a>
        <span style={{ margin: "0 8px" }}>/</span>
        <a href={`#/c/${cat.id}`} style={{ color: "var(--muted)", textDecoration: "none" }} onClick={(e)=>{e.preventDefault();go(`/c/${cat.id}`);}}>{cat.label}</a>
        <span style={{ margin: "0 8px" }}>/</span>
        <span style={{ color: "var(--ink)" }}>{p.name}</span>
      </div>

      <div style={{ display: "grid", gridTemplateColumns: "1.4fr 1fr", gap: 56 }}>
        {/* Media */}
        <div>
          <ProductMedia p={p} hue={hue} />
          {p.video && p.video.kind === "mediafire" && (
            <div style={{ marginTop: 12 }}>
              <a href={p.video.url} target="_blank" rel="noreferrer" style={{
                display: "inline-flex", alignItems: "center", gap: 10, padding: "10px 16px",
                border: "1px solid var(--border)", borderRadius: 999, fontSize: 13,
                textDecoration: "none", color: "var(--ink)", fontFamily: "var(--sans)", fontWeight: 500,
              }}>
                <svg width="14" height="14" viewBox="0 0 16 16" fill="none" stroke="currentColor" strokeWidth="1.5" strokeLinecap="round" strokeLinejoin="round"><path d="M8 2v9"/><path d="M4 8l4 4 4-4"/><path d="M2 14h12"/></svg>
                Watch preview on MediaFire ↗
              </a>
            </div>
          )}

          {/* Long description */}
          <section style={{ marginTop: 56 }}>
            <h2 style={{ fontSize: 20, fontWeight: 600, letterSpacing: "-0.015em", marginBottom: 14 }}>About this system</h2>
            <p style={{ fontSize: 15, color: "var(--muted)", lineHeight: 1.65, margin: 0 }}>
              {p.tagline} Delivered directly to your Roblox toolbox once payment is confirmed. Whitelist-protected — 1 account + 1 community included. For additional communities, purchase the <a href="#/p/whitelist-addition" style={{ color: "var(--accent)" }}>Whitelist Addition</a> add-on.
            </p>

            <h3 style={{ fontSize: 14, fontWeight: 600, letterSpacing: "0.04em", textTransform: "uppercase", color: "var(--muted)", marginTop: 36, marginBottom: 18, fontFamily: "var(--mono)" }}>What's included</h3>
            <ul style={{ listStyle: "none", padding: 0, margin: 0, display: "grid", gridTemplateColumns: "1fr 1fr", gap: "10px 28px" }}>
              {p.features.map(f => (
                <li key={f} style={{ display: "flex", gap: 10, fontSize: 14, color: "var(--ink)", alignItems: "flex-start" }}>
                  <span style={{ color: "var(--accent)", fontFamily: "var(--mono)", paddingTop: 2 }}>✓</span> {f}
                </li>
              ))}
            </ul>

            <h3 style={{ fontSize: 14, fontWeight: 600, letterSpacing: "0.04em", textTransform: "uppercase", color: "var(--muted)", marginTop: 36, marginBottom: 18, fontFamily: "var(--mono)" }}>Compatibility</h3>
            <div style={{ display: "grid", gridTemplateColumns: "repeat(3, 1fr)", gap: 12 }}>
              {[
                ["Engine", "Roblox 2025+"],
                ["Required", "Whitelist system"],
                ["License", "1 account + 1 community"],
                ["Mobile / PC", "Fully supported"],
                ["Delivery", "To your Toolbox"],
                ["Updates", "Patch updates included"],
              ].map(([k,v]) => (
                <div key={k} style={{ borderTop: "1px solid var(--border)", paddingTop: 10 }}>
                  <div style={{ fontFamily: "var(--mono)", fontSize: 10, color: "var(--muted)", letterSpacing: "0.06em", textTransform: "uppercase" }}>{k}</div>
                  <div style={{ fontSize: 14, marginTop: 4 }}>{v}</div>
                </div>
              ))}
            </div>
          </section>
        </div>

        {/* Buy panel — sticky */}
        <aside style={{ position: "sticky", top: 96, alignSelf: "flex-start", height: "fit-content" }}>
          {p.badge && (
            <div style={{ display: "inline-block", fontFamily: "var(--mono)", fontSize: 10, letterSpacing: "0.08em", textTransform: "uppercase", padding: "4px 9px", background: "var(--accent)", color: "white", borderRadius: 2, marginBottom: 16 }}>
              {p.badge}
            </div>
          )}
          <div style={{ fontFamily: "var(--mono)", fontSize: 11, color: "var(--muted)", letterSpacing: "0.08em", textTransform: "uppercase", marginBottom: 10 }}>
            {cat.label} — {cat.id_label}
          </div>
          <h1 style={{ fontSize: 36, fontWeight: 600, letterSpacing: "-0.025em", margin: 0, lineHeight: 1.1 }}>{p.name}</h1>
          <p style={{ fontSize: 15, color: "var(--muted)", lineHeight: 1.55, marginTop: 14, marginBottom: 0 }}>{p.tagline}</p>

          {/* Meta strip */}
          <div style={{ display: "flex", alignItems: "center", gap: 16, marginTop: 24, fontFamily: "var(--mono)", fontSize: 12, color: "var(--muted)" }}>
            <span style={{ display: "flex", alignItems: "center", gap: 4 }}><Star /> {p.rating.toFixed(1)}</span>
            <span style={{ width: 1, height: 12, background: "var(--border)" }} />
            <span>{p.sold} sold</span>
            <span style={{ width: 1, height: 12, background: "var(--border)" }} />
            <span>v{p.version}</span>
          </div>

          <div style={{ marginTop: 32, padding: 24, border: "1px solid var(--border)", borderRadius: 6 }}>
            <div style={{ display: "flex", alignItems: "flex-end", justifyContent: "space-between", marginBottom: 18 }}>
              <div>
                <div style={{ fontFamily: "var(--mono)", fontSize: 10, color: "var(--muted)", letterSpacing: "0.06em", textTransform: "uppercase", marginBottom: 4 }}>
                  {effectivePrice(p, currency).isEvent ? "Event price · one-time" : "Price · one-time"}
                </div>
                <div style={{ marginBottom: 4 }}>
                  <PriceLabel product={p} currency={currency} size="lg" />
                </div>
                <div style={{ fontSize: 11, color: "var(--muted)", marginTop: 4, fontFamily: "var(--mono)" }}>
                  {(() => {
                    const ep = effectivePrice(p, currency).price;
                    if (currency === "IDR")   return <>≈ ${ep.usd} USD · R$ {p.price.robux.toLocaleString("en-US")}</>;
                    if (currency === "USD")   return <>≈ Rp {ep.idr.toLocaleString("id-ID").replace(/,/g, ".")} · R$ {p.price.robux.toLocaleString("en-US")}</>;
                    return <>≈ Rp {p.price.idr.toLocaleString("id-ID").replace(/,/g, ".")} · ${p.price.usd} USD <em style={{ fontStyle: "normal", color: "var(--muted)" }}>· no Robux discount</em></>;
                  })()}
                </div>
              </div>
            </div>
            <div style={{ display: "flex", flexDirection: "column", gap: 10 }}>
              <Btn primary full onClick={() => { add(p.id, 1); go("/cart"); }}>Buy now</Btn>
              <Btn full onClick={handleAdd}>
                {added ? "✓ Added to cart" : "Add to cart"}
              </Btn>
            </div>
            <div style={{ marginTop: 18, paddingTop: 18, borderTop: "1px solid var(--border)", fontSize: 12, color: "var(--muted)", lineHeight: 1.6 }}>
              <div style={{ display: "flex", justifyContent: "space-between", marginBottom: 4 }}>
                <span>Delivery</span><span style={{ color: "var(--ink)" }}>Discord + Roblox</span>
              </div>
              <div style={{ display: "flex", justifyContent: "space-between", marginBottom: 4 }}>
                <span>License</span><span style={{ color: "var(--ink)" }}>Whitelist · 1 community</span>
              </div>
              <div style={{ display: "flex", justifyContent: "space-between" }}>
                <span>Pay with</span><span style={{ color: "var(--ink)" }}>QRIS · PayPal · Robux</span>
              </div>
            </div>
          </div>

          <div style={{ display: "flex", alignItems: "center", gap: 10, marginTop: 20, fontSize: 12, color: "var(--muted)" }}>
            <PixelCat size={20} accentColor="var(--accent)" />
            By <span style={{ color: "var(--ink)" }}>{p.creator}</span> · updated {p.updated}
          </div>
        </aside>
      </div>

      {/* Related */}
      {related.length > 0 && (
        <section style={{ marginTop: 96 }}>
          <SectionHeading kicker="More from this category" title={`Other ${cat.label.toLowerCase()}.`} />
          <div style={{ display: "grid", gridTemplateColumns: "repeat(4, 1fr)", gap: 16 }}>
            {related.map(r => <ProductCard key={r.id} product={r} onClick={() => go(`/p/${r.id}`)} />)}
          </div>
        </section>
      )}
    </main>
  );
}

function ThumbBtn({ active, onClick, children }) {
  return (
    <button onClick={onClick} style={{
      padding: 0, border: "1px solid", borderColor: active ? "var(--ink)" : "var(--border)",
      borderRadius: 4, background: "var(--bg)", cursor: "pointer", overflow: "hidden",
      position: "relative", display: "block",
    }}>
      {children}
    </button>
  );
}
function ThumbLabel({ children }) {
  return (
    <div style={{
      position: "absolute", left: 0, right: 0, bottom: 0, padding: "6px 8px",
      background: "linear-gradient(to top, rgba(0,0,0,0.65), transparent)",
      color: "white", fontFamily: "var(--mono)", fontSize: 9, letterSpacing: "0.04em",
      textAlign: "left",
    }}>{children}</div>
  );
}

Object.assign(window, {
  HomePage, BrowsePage, ProductPage, SectionHeading,
});
