/* ted.jsx — Ted Kaczynski · the artifact DISPENSARY.
   AIRS Agent Artifact scanning is done MANUALLY in Strata Cloud Manager — so
   this pillar is a pickup counter, NOT a scanner. Three download stations
   (skills · mcp · agents); each hands out sample artifacts the user downloads,
   uploads to SCM, and scans THERE. No live scan, no backend, no verdict UI.

   • TedCard     — hub card (kraft/amber), routes to #/ted/<cat>.
   • TedStation  — the download-station page: header + "what is this", a neutral
                   package list (NEVER hinting which are booby-trapped), a slide-in
                   read DRAWER (tabs + read-only monospace, raw text fetched at
                   runtime from the static URL), and a collapsible "how to scan in SCM".

   Downloads use real <a href download> anchors. URL conventions (static assets
   attached at deploy — we just link to them):
     per-package zip  /downloads/ted/<cat>/<name>.zip
     combined zip     /downloads/ted/<cat>/ted-<cat>.zip
     raw file         /downloads/ted/<cat>/<name>/<file>
*/

// ---- palette (kraft paper + hazard amber — the parcel-X-ray motif) --------
// Each category carries its OWN warm hue (cat.c1/c2) within this family:
// skills = amber, mcp = terracotta, agents = olive-bronze. tedBand builds the
// X-ray band gradient from a category's two colors.
const TED_AMBER = "#d98a26";  // default fallback (skills' accent)
const tedBand = (c1, c2) => `linear-gradient(120deg, ${c1}, color-mix(in oklab, ${c2} 62%, #1c1206))`;
const tedSoftBand = (c1, c2) => `linear-gradient(120deg, color-mix(in oklab, ${c1} 22%, transparent), color-mix(in oklab, ${c2} 22%, transparent))`;

// ---- static download URLs (linked, never fetched-to-backend) --------------
const tedZip = (cat, name) => `/downloads/ted/${cat}/${name}.zip`;
const tedAllZip = (cat) => `/downloads/ted/${cat}/ted-${cat}.zip`;
const tedRaw = (cat, name, file) => `/downloads/ted/${cat}/${name}/${file}`;
const TED_CATS = ["skills", "mcp", "agents"];

// Per-category brand mark: skills/agents are colorful 3D PNGs; MCP is the
// official ModelContextProtocol line logo (inline so it inherits `color`).
function TedMark({ cat, size = 24, style }) {
  if (cat === "mcp") {
    return (
      <svg viewBox="0 0 24 24" width={size} height={size} fill="currentColor" aria-hidden="true" style={style}>
        <path d="M15.688 2.343a2.588 2.588 0 00-3.61 0l-9.626 9.44a.863.863 0 01-1.203 0 .823.823 0 010-1.18l9.626-9.44a4.313 4.313 0 016.016 0 4.116 4.116 0 011.204 3.54 4.3 4.3 0 013.609 1.18l.05.05a4.115 4.115 0 010 5.9l-8.706 8.537a.274.274 0 000 .393l1.788 1.754a.823.823 0 010 1.18.863.863 0 01-1.203 0l-1.788-1.753a1.92 1.92 0 010-2.754l8.706-8.538a2.47 2.47 0 000-3.54l-.05-.049a2.588 2.588 0 00-3.607-.003l-7.172 7.034-.002.002-.098.097a.863.863 0 01-1.204 0 .823.823 0 010-1.18l7.273-7.133a2.47 2.47 0 00-.003-3.537z" />
        <path d="M14.485 4.703a.823.823 0 000-1.18.863.863 0 00-1.204 0l-7.119 6.982a4.115 4.115 0 000 5.9 4.314 4.314 0 006.016 0l7.12-6.982a.823.823 0 000-1.18.863.863 0 00-1.204 0l-7.119 6.982a2.588 2.588 0 01-3.61 0 2.47 2.47 0 010-3.54l7.12-6.982z" />
      </svg>
    );
  }
  const src = cat === "skills" ? "images/ted-skills.png" : "images/ted-agent.png";
  return <img src={src} width={size} height={size} alt="" aria-hidden="true" style={{ objectFit: "contain", ...style }} />;
}

// inline `code` spans inside the "what is this" paragraph
function TedRich({ text, th }) {
  const parts = String(text).split(/(`[^`]+`)/g);
  return (
    <>
      {parts.map((p, i) =>
        /^`[^`]+`$/.test(p) ? (
          <code key={i} className="rounded px-1 py-0.5 text-[0.88em]" style={{ background: th.chip, color: th.amberText, fontFamily: "'JetBrains Mono', monospace" }}>
            {p.replace(/`/g, "")}
          </code>
        ) : (
          <React.Fragment key={i}>{p}</React.Fragment>
        )
      )}
    </>
  );
}

// =========================================================================
// HUB CARD — live; routes into the station (#/ted/<cat>)
// =========================================================================
function TedCard({ item, index, th, mode }) {
  const cat = window.getTedCategory(item.cat);
  const [hover, setHover] = React.useState(false);
  const dark = mode !== "light";
  if (!cat) return null;
  const route = "#/ted/" + cat.cat;
  const c1 = cat.c1, c2 = cat.c2;
  const band = tedBand(c1, c2);
  const softBand = tedSoftBand(c1, c2);
  const bandInk = dark ? cat.textDark : cat.textLight;
  const count = cat.packages.length;
  return (
    <div className="relative h-full a-fade-up" style={{ animationDelay: index * 90 + "ms" }} onMouseEnter={() => setHover(true)} onMouseLeave={() => setHover(false)}>
      <a
        href={route}
        className="group relative flex h-full flex-col overflow-hidden rounded-3xl text-left transition-[transform,box-shadow] duration-300"
        style={{ background: th.card, border: `1px solid ${hover ? c2 + "66" : th.cardBorder}`, boxShadow: hover ? th.cardShadowHover : th.cardShadow, transform: hover ? "translateY(-4px)" : "none" }}
      >
        {/* kraft / X-ray band */}
        <div className="relative h-28 w-full overflow-hidden" style={{ background: band }}>
          <div className="absolute -right-6 -top-10" style={{ opacity: 0.2 }}>
            <window.Icon name="PackageSearch" size={150} style={{ color: "#fff" }} strokeWidth={1.1} />
          </div>
          <div className="absolute left-5 top-5 flex h-11 w-11 items-center justify-center rounded-2xl" style={{ background: "rgba(28,18,6,0.32)", color: "#fff", border: "1px solid rgba(255,255,255,0.22)" }}>
            <window.Icon name={cat.icon} size={22} strokeWidth={2} />
          </div>
          <span className="absolute right-5 top-4 inline-flex items-center gap-1.5 rounded-full px-2.5 py-1 text-[10px] font-bold uppercase tracking-[0.14em]" style={{ background: "rgba(28,18,6,0.34)", color: "#fff", fontFamily: "'JetBrains Mono', monospace" }}>
            <window.Icon name="Package" size={11} /> {count} packages
          </span>
        </div>

        <div className="flex flex-1 flex-col p-5">
          <div className="text-[11px] font-semibold uppercase tracking-[0.16em]" style={{ color: th.faint, fontFamily: "'JetBrains Mono', monospace" }}>{cat.eyebrow}</div>
          <div className="mt-0.5 flex items-center gap-2">
            <span className="text-[19px] font-extrabold leading-tight tracking-[-0.01em]" style={{ fontFamily: "'Manrope', sans-serif", color: th.inkStrong }}>{cat.title}</span>
            <TedMark cat={cat.cat} size={21} style={{ color: c2 }} />
          </div>
          <p className="mt-1.5 flex-1 text-[13.5px] leading-relaxed" style={{ color: th.ink }}>{cat.cardBlurb}</p>
          <div className="mt-5 flex items-center justify-between">
            <span className="inline-flex items-center gap-1.5 text-[12px]" style={{ color: th.faint }}>
              <span className="inline-block h-1.5 w-1.5 rounded-full" style={{ background: c2 }} /> Live · pickup counter
            </span>
            <span
              className="inline-flex items-center gap-1.5 rounded-xl px-3.5 py-2 text-[13px] font-semibold transition-all"
              style={{ background: hover ? band : softBand, color: hover ? "#fff" : bandInk }}
            >
              Open packages <window.Icon name="ArrowRight" size={15} style={{ transform: hover ? "translateX(2px)" : "none", transition: "transform .2s" }} />
            </span>
          </div>
        </div>
      </a>
      <window.OpenInNewTab href={route} className="absolute right-4 top-4 z-10 h-7 w-7" title={"Open " + cat.title + " in new tab"} style={{ background: "rgba(28,18,6,0.34)", color: "#fff", border: "1px solid rgba(255,255,255,0.22)" }} />
    </div>
  );
}

// =========================================================================
// shared bits
// =========================================================================
function TedDownloadLink({ href, children, solid, th, title }) {
  return (
    <a
      href={href}
      download
      title={title}
      onClick={(e) => e.stopPropagation()}
      className="inline-flex shrink-0 items-center gap-1.5 rounded-xl px-3 py-2 text-[12.5px] font-semibold transition-all"
      style={solid
        ? { background: th.amber, color: "#1c1206", border: "1px solid transparent" }
        : { background: th.chip, color: th.ink, border: `1px solid ${th.cardBorder}` }}
    >
      {children}
    </a>
  );
}

// one package row — name · blurb · Read · .zip  (NEVER hints clean vs. booby-trapped)
function TedRow({ cat, pkg, th, onRead, last }) {
  const [hover, setHover] = React.useState(false);
  const multi = pkg.files.length > 1;
  return (
    <div
      className="flex flex-wrap items-center gap-x-4 gap-y-3 px-4 py-4 transition-colors sm:flex-nowrap"
      style={{ borderBottom: last ? "none" : `1px solid ${th.cardBorder}`, background: hover ? th.rowHover : "transparent" }}
      onMouseEnter={() => setHover(true)}
      onMouseLeave={() => setHover(false)}
    >
      <div className="flex min-w-0 flex-1 items-center gap-3">
        <span className="flex h-9 w-9 shrink-0 items-center justify-center rounded-xl" style={{ background: th.chip, color: th.amberText, border: `1px solid ${th.cardBorder}` }}>
          <window.Icon name="Package" size={16} />
        </span>
        <div className="min-w-0">
          <div className="flex items-center gap-2">
            <span className="truncate text-[14px] font-bold" style={{ color: th.inkStrong, fontFamily: "'JetBrains Mono', monospace" }}>{pkg.name}</span>
            <span className="hidden shrink-0 items-center gap-1 rounded-md px-1.5 py-0.5 text-[10px] font-semibold sm:inline-flex" style={{ background: th.chip, color: th.faint, fontFamily: "'JetBrains Mono', monospace" }}>
              {multi ? pkg.files.length + " files" : "1 file"}
            </span>
          </div>
          <div className="mt-0.5 truncate text-[12.5px]" style={{ color: th.ink }}>{pkg.blurb}</div>
        </div>
      </div>
      <div className="flex shrink-0 items-center gap-2">
        <button
          onClick={() => onRead(pkg)}
          className="inline-flex items-center gap-1.5 rounded-xl px-3 py-2 text-[12.5px] font-semibold transition-colors"
          style={{ background: th.chip, color: th.ink, border: `1px solid ${th.cardBorder}` }}
        >
          <window.Icon name="Eye" size={15} /> Read
        </button>
        <TedDownloadLink href={tedZip(cat, pkg.name)} th={th} title={"Download " + pkg.name + ".zip"}>
          <window.Icon name="Download" size={15} /> .zip
        </TedDownloadLink>
      </div>
    </div>
  );
}

// =========================================================================
// READ DRAWER — slides in from the right; tabs across files; read-only mono.
// Raw text is fetched at runtime from the static URL; a failed fetch (e.g. raw
// preview before deploy) shows a quiet note instead of erroring.
// =========================================================================
function TedDrawer({ cat, pkg, th, onClose }) {
  const [enter, setEnter] = React.useState(false);
  const [active, setActive] = React.useState(0);
  const [texts, setTexts] = React.useState({}); // file -> { state:"loading"|"ok"|"miss", text }
  const file = pkg.files[active];

  // mount → animate in
  React.useEffect(() => { const r = requestAnimationFrame(() => setEnter(true)); return () => cancelAnimationFrame(r); }, []);

  // graceful close (animate out, then unmount)
  const requestClose = React.useCallback(() => { setEnter(false); setTimeout(onClose, 230); }, [onClose]);

  // Esc to close
  React.useEffect(() => {
    const onKey = (e) => { if (e.key === "Escape") requestClose(); };
    window.addEventListener("keydown", onKey);
    return () => window.removeEventListener("keydown", onKey);
  }, [requestClose]);

  // fetch the active file's raw text once
  React.useEffect(() => {
    if (texts[file]) return;
    let alive = true;
    setTexts((m) => ({ ...m, [file]: { state: "loading" } }));
    fetch(tedRaw(cat, pkg.name, file))
      .then((r) => { if (!r.ok) throw new Error(String(r.status)); return r.text(); })
      .then((t) => { if (alive) setTexts((m) => ({ ...m, [file]: { state: "ok", text: t } })); })
      .catch(() => { if (alive) setTexts((m) => ({ ...m, [file]: { state: "miss" } })); });
    return () => { alive = false; };
  }, [cat, pkg.name, file]); // eslint-disable-line

  const cur = texts[file] || { state: "loading" };

  return (
    <div className="fixed inset-0 z-50" aria-modal="true" role="dialog">
      {/* backdrop */}
      <div
        onClick={requestClose}
        className="absolute inset-0 transition-opacity duration-200"
        style={{ background: "rgba(8,5,3,0.55)", backdropFilter: "blur(2px)", opacity: enter ? 1 : 0 }}
      />
      {/* panel */}
      <div
        className="absolute right-0 top-0 flex h-full w-full max-w-[680px] flex-col shadow-2xl transition-transform duration-[230ms]"
        style={{ background: th.drawerBg, borderLeft: `1px solid ${th.cardBorder}`, transform: enter ? "translateX(0)" : "translateX(100%)", transitionTimingFunction: "cubic-bezier(.22,1,.36,1)" }}
        onClick={(e) => e.stopPropagation()}
      >
        {/* header */}
        <div className="flex items-center justify-between gap-3 border-b px-5 py-4" style={{ borderColor: th.cardBorder }}>
          <div className="flex min-w-0 items-center gap-3">
            <span className="flex h-9 w-9 shrink-0 items-center justify-center rounded-xl" style={{ background: th.chip, color: th.amberText, border: `1px solid ${th.cardBorder}` }}>
              <window.Icon name="Package" size={17} />
            </span>
            <div className="min-w-0">
              <div className="truncate text-[15px] font-bold" style={{ color: th.inkStrong, fontFamily: "'JetBrains Mono', monospace" }}>{pkg.name}</div>
              <div className="truncate text-[12px]" style={{ color: th.faint }}>{pkg.blurb}</div>
            </div>
          </div>
          <div className="flex shrink-0 items-center gap-2">
            <TedDownloadLink href={tedZip(cat, pkg.name)} th={th} title={"Download " + pkg.name + ".zip"}>
              <window.Icon name="Download" size={15} /> .zip
            </TedDownloadLink>
            <button onClick={requestClose} aria-label="Close" className="flex h-9 w-9 items-center justify-center rounded-xl transition-colors" style={{ background: th.chip, color: th.ink, border: `1px solid ${th.cardBorder}` }}>
              <window.Icon name="X" size={17} />
            </button>
          </div>
        </div>

        {/* file tabs */}
        {pkg.files.length > 1 && (
          <div className="flex items-center gap-1.5 overflow-x-auto border-b px-4 py-2.5 lr-scroll" style={{ borderColor: th.cardBorder }}>
            {pkg.files.map((f, i) => (
              <button
                key={f}
                onClick={() => setActive(i)}
                className="inline-flex shrink-0 items-center gap-1.5 rounded-lg px-3 py-1.5 text-[12px] font-semibold transition-colors"
                style={i === active
                  ? { background: th.chip, color: th.inkStrong, border: `1px solid ${th.amber}66`, fontFamily: "'JetBrains Mono', monospace" }
                  : { background: "transparent", color: th.faint, border: "1px solid transparent", fontFamily: "'JetBrains Mono', monospace" }}
              >
                <window.Icon name={/\.py$/.test(f) ? "FileCode2" : "FileText"} size={13} /> {f}
              </button>
            ))}
          </div>
        )}

        {/* code body */}
        <div className="flex min-h-0 flex-1 flex-col" style={{ background: th.codeBg }}>
          <div className="flex items-center justify-between gap-3 px-4 py-2.5" style={{ borderBottom: `1px solid ${th.codeBorder}` }}>
            <span className="inline-flex items-center gap-1.5 text-[11.5px] font-semibold" style={{ color: "#9fb0c2", fontFamily: "'JetBrains Mono', monospace" }}>
              <window.Icon name={/\.py$/.test(file) ? "FileCode2" : "FileText"} size={13} /> {file}
              <span className="ml-1 rounded px-1.5 py-0.5 text-[10px] uppercase tracking-[0.12em]" style={{ background: "rgba(255,255,255,0.06)", color: "#8794a3" }}>read-only</span>
            </span>
            <a
              href={tedRaw(cat, pkg.name, file)}
              download
              title={"Download " + file}
              className="inline-flex shrink-0 items-center gap-1.5 rounded-lg px-2.5 py-1.5 text-[11.5px] font-semibold transition-colors"
              style={{ background: "rgba(255,255,255,0.06)", color: "#cdd6e0", border: "1px solid rgba(255,255,255,0.1)", fontFamily: "'JetBrains Mono', monospace" }}
            >
              <window.Icon name="Download" size={13} />
            </a>
          </div>
          <div className="min-h-0 flex-1 overflow-auto lr-scroll p-4">
            {cur.state === "loading" && (
              <div className="flex items-center gap-2 text-[12.5px]" style={{ color: "#8794a3", fontFamily: "'JetBrains Mono', monospace" }}>
                <window.Icon name="Loader" size={14} /> loading {file}…
              </div>
            )}
            {cur.state === "miss" && (
              <div className="flex items-start gap-2.5 rounded-xl px-3.5 py-3 text-[12.5px]" style={{ background: `color-mix(in oklab, ${th.amber} 12%, transparent)`, border: `1px solid color-mix(in oklab, ${th.amber} 32%, transparent)`, color: th.amberText }}>
                <window.Icon name="Info" size={15} className="mt-0.5 shrink-0" />
                <span>This file's contents load on the deployed site. The download links above still work once the static artifacts are attached.</span>
              </div>
            )}
            {cur.state === "ok" && (
              <pre className="whitespace-pre-wrap break-words text-[12.5px] leading-relaxed" style={{ color: th.codeInk, fontFamily: "'JetBrains Mono', monospace", margin: 0 }}>{cur.text}</pre>
            )}
          </div>
        </div>
      </div>
    </div>
  );
}

// =========================================================================
// HOW TO SCAN IN SCM — collapsible, honest footnote
// =========================================================================
function TedHowTo({ th }) {
  const [open, setOpen] = React.useState(false);
  const steps = [
    { icon: "Download", text: "Download a package — the .zip, or the raw files from the read drawer." },
    { icon: "CloudUpload", text: "In Strata Cloud Manager → AI Runtime Security → Agent Artifact Security, upload or point at it and run the scan." },
    { icon: "ScanSearch", text: "The booby-trapped ones get flagged; the clean control passes — none screamed \u201Cmalware\u201D on a casual read." },
  ];
  return (
    <div className="overflow-hidden rounded-2xl" style={{ background: th.card, border: `1px solid ${th.cardBorder}` }}>
      <button onClick={() => setOpen((o) => !o)} className="flex w-full items-center justify-between gap-3 px-5 py-4 text-left">
        <span className="inline-flex items-center gap-2.5 text-[13.5px] font-bold" style={{ color: th.inkStrong }}>
          <window.Icon name={open ? "ChevronDown" : "ChevronRight"} size={17} style={{ color: th.amberText }} />
          How to scan in SCM
        </span>
        <span className="inline-flex items-center gap-1.5 text-[11px] font-semibold uppercase tracking-[0.14em]" style={{ color: th.faint, fontFamily: "'JetBrains Mono', monospace" }}>
          <window.Icon name="ScanSearch" size={13} /> Strata Cloud Manager
        </span>
      </button>
      {open && (
        <div className="border-t px-5 pb-5 pt-4 a-fade" style={{ borderColor: th.cardBorder }}>
          <ol className="space-y-3">
            {steps.map((s, i) => (
              <li key={i} className="flex items-start gap-3">
                <span className="flex h-7 w-7 shrink-0 items-center justify-center rounded-lg text-[12px] font-bold" style={{ background: th.chip, color: th.amberText, fontFamily: "'JetBrains Mono', monospace" }}>{i + 1}</span>
                <span className="pt-0.5 text-[13.5px] leading-relaxed" style={{ color: th.ink }}>{s.text}</span>
              </li>
            ))}
          </ol>
          <div className="mt-4 flex items-center gap-2 border-t pt-3.5 text-[12px] italic" style={{ borderColor: th.cardBorder, color: th.faint }}>
            <window.Icon name="Info" size={13} /> Scanning happens in SCM, not here.
          </div>
        </div>
      )}
    </div>
  );
}

// =========================================================================
// STATION PAGE (#/ted/<cat>)
// =========================================================================
function TedStation({ cat: catId, mode, navigate, onBack }) {
  const base = window.SHELL_THEMES[mode] || window.SHELL_THEMES.dark;
  const cat = window.getTedCategory(catId);
  React.useEffect(() => { if (!cat) navigate("#/hub"); }, [cat]);
  const [openPkg, setOpenPkg] = React.useState(null);
  if (!cat) return null;

  const dark = mode !== "light";
  const c1 = cat.c1, c2 = cat.c2;
  const band = tedBand(c1, c2);
  const th = {
    ...base,
    faint: base.sub,
    amber: c2,
    amberText: dark ? cat.textDark : cat.textLight,
    rowHover: dark ? "rgba(255,255,255,0.03)" : "rgba(42,28,34,0.025)",
    drawerBg: dark ? "#161019" : "#ffffff",
    codeBg: dark ? "#0c0a12" : "#0f172a",
    codeInk: dark ? "#e7ddcf" : "#e8eef6",
    codeBorder: dark ? "rgba(255,255,255,0.08)" : "rgba(255,255,255,0.12)",
  };

  return (
    <div className="h-full w-full overflow-y-auto lr-scroll" style={{ background: th.page, fontFamily: "'Manrope', sans-serif" }}>
      <div className="pointer-events-none fixed inset-0">
        <div className="absolute -top-40 left-1/2 h-[520px] w-[860px] -translate-x-1/2 rounded-full" style={{ background: `radial-gradient(circle, color-mix(in oklab, ${c2} 18%, transparent), transparent 65%)` }} />
      </div>

      {/* top bar */}
      <header className="sticky top-0 z-20 border-b" style={{ borderColor: th.headerBorder, background: th.header, backdropFilter: "blur(14px)" }}>
        <div className="mx-auto flex max-w-5xl items-center justify-between px-6 py-3.5">
          <a href="#/hub" onClick={(e) => { if (e.metaKey || e.ctrlKey || e.shiftKey || e.button !== 0) return; e.preventDefault(); onBack(); }}
            className="inline-flex items-center gap-2 rounded-xl px-3 py-2 text-[13px] font-medium transition-colors" style={{ color: th.ink, background: th.chip, border: `1px solid ${th.cardBorder}` }}>
            <window.Icon name="ArrowLeft" size={15} /> Showroom
          </a>
          <div className="flex items-center gap-2.5">
            <span className="hidden items-center gap-1.5 text-[12px] font-semibold uppercase tracking-[0.16em] sm:inline-flex" style={{ color: th.faint, fontFamily: "'JetBrains Mono', monospace" }}>
              <window.Icon name="PackageSearch" size={14} /> Ted Kaczynski
            </span>
            <window.OpenInNewTab href={"#/ted/" + cat.cat} size={14} className="h-8 w-8" title="Open this page in a new tab" style={{ color: th.faint, background: th.chip, border: `1px solid ${th.cardBorder}` }} />
          </div>
        </div>
      </header>

      <main className="relative z-10 mx-auto max-w-5xl px-6 pb-24 pt-10">
        {/* category switcher (real anchors) */}
        <div className="mb-7 flex flex-wrap items-center gap-2 a-fade-up">
          {TED_CATS.map((c) => {
            const cc = window.getTedCategory(c);
            const on = c === cat.cat;
            return (
              <a key={c} href={"#/ted/" + c}
                className="inline-flex items-center gap-2 rounded-full px-3.5 py-2 text-[12.5px] font-semibold transition-colors"
                style={on
                  ? { background: cc.c2, color: "#1c1206", border: "1px solid transparent" }
                  : { background: th.chip, color: th.ink, border: `1px solid ${th.cardBorder}` }}>
                <window.Icon name={cc.icon} size={14} /> {cc.title}
              </a>
            );
          })}
        </div>

        {/* header */}
        <div className="overflow-hidden rounded-3xl a-fade-up" style={{ background: th.card, border: `1px solid ${th.cardBorder}`, boxShadow: th.cardShadow }}>
          <div className="relative h-24 w-full overflow-hidden" style={{ background: band }}>
            <div className="absolute -right-6 -top-10" style={{ opacity: 0.2 }}><window.Icon name="PackageSearch" size={150} style={{ color: "#fff" }} strokeWidth={1.1} /></div>
            <div className="absolute left-6 top-1/2 flex -translate-y-1/2 items-center gap-3.5">
              <div className="flex h-12 w-12 items-center justify-center rounded-2xl" style={{ background: "rgba(28,18,6,0.32)", color: "#fff", border: "1px solid rgba(255,255,255,0.22)" }}>
                <window.Icon name={cat.icon} size={24} strokeWidth={2} />
              </div>
              <div>
                <div className="whitespace-nowrap text-[10.5px] font-bold uppercase tracking-[0.18em]" style={{ color: "rgba(255,255,255,0.85)", fontFamily: "'JetBrains Mono', monospace" }}>{cat.eyebrow}</div>
                <div className="mt-1 flex items-center gap-2.5">
                  <h1 className="text-[28px] font-extrabold leading-none text-white" style={{ fontFamily: "'Manrope', sans-serif", letterSpacing: "-0.01em" }}>{cat.title}</h1>
                  <TedMark cat={cat.cat} size={24} style={{ color: "#fff" }} />
                </div>
              </div>
            </div>
          </div>

          <div className="p-6 sm:p-7">
            <p className="max-w-3xl text-[14.5px] leading-relaxed" style={{ color: th.ink }}>
              <TedRich text={cat.what} th={th} />
            </p>
            <div className="mt-5 flex flex-wrap items-center justify-between gap-4">
              <p className="max-w-xl text-[13.5px] font-medium italic leading-relaxed" style={{ color: th.amberText }}>
                These look ordinary. Some are clean, some aren't — can you tell which by eye? AIRS can.
              </p>
              <a
                href={tedAllZip(cat.cat)}
                download
                className="inline-flex shrink-0 items-center gap-2 rounded-xl px-4 py-2.5 text-[13.5px] font-bold transition-all"
                style={{ background: c2, color: "#1c1206", border: "1px solid transparent" }}
              >
                <window.Icon name="Download" size={16} /> Download all (.zip)
              </a>
            </div>
          </div>
        </div>

        {/* package list */}
        <div className="mt-6 overflow-hidden rounded-3xl a-fade-up" style={{ background: th.card, border: `1px solid ${th.cardBorder}`, boxShadow: th.cardShadow, animationDelay: "60ms" }}>
          <div className="flex items-center justify-between gap-3 border-b px-5 py-3.5" style={{ borderColor: th.cardBorder }}>
            <span className="inline-flex items-center gap-2 text-[12px] font-bold uppercase tracking-[0.16em]" style={{ color: th.faint, fontFamily: "'JetBrains Mono', monospace" }}>
              <window.Icon name="Boxes" size={14} /> {cat.packages.length} packages
            </span>
            <span className="text-[12px]" style={{ color: th.faint }}>Read it, or download the .zip</span>
          </div>
          {cat.packages.map((pkg, i) => (
            <TedRow key={pkg.name} cat={cat.cat} pkg={pkg} th={th} onRead={setOpenPkg} last={i === cat.packages.length - 1} />
          ))}
        </div>

        {/* how to scan */}
        <div className="mt-6 a-fade-up" style={{ animationDelay: "120ms" }}>
          <TedHowTo th={th} />
        </div>
      </main>

      {openPkg && <TedDrawer cat={cat.cat} pkg={openPkg} th={th} onClose={() => setOpenPkg(null)} />}
    </div>
  );
}

Object.assign(window, { TedCard, TedStation });
