// Shared components for the Zeejay Lab UI kit
// All export to window for cross-script use (Babel scopes are isolated).

const { useState, useRef, useEffect } = React;

const useIsMobile = () => {
  const [isMobile, setIsMobile] = useState(window.innerWidth <= 768);
  useEffect(() => {
    const handleResize = () => setIsMobile(window.innerWidth <= 768);
    window.addEventListener("resize", handleResize);
    return () => window.removeEventListener("resize", handleResize);
  }, []);
  return isMobile;
};
window.useIsMobile = useIsMobile;

// ── Icons (inline Lucide-style SVGs, 2px stroke) ─────────────────────────
const Icon = ({ d, stroke = "currentColor", size = 16, children, ...rest }) => (
  <svg width={size} height={size} viewBox="0 0 24 24" fill="none" stroke={stroke}
    strokeWidth="2" strokeLinecap="round" strokeLinejoin="round" {...rest}>
    {children || <path d={d}/>}
  </svg>
);
const ArrowRight = (p) => <Icon {...p}><line x1="5" y1="12" x2="19" y2="12"/><polyline points="12 5 19 12 12 19"/></Icon>;
const ArrowLeft  = (p) => <Icon {...p}><line x1="19" y1="12" x2="5" y2="12"/><polyline points="12 19 5 12 12 5"/></Icon>;
const ChevL     = (p) => <Icon {...p}><polyline points="15 18 9 12 15 6"/></Icon>;
const Brain     = (p) => <Icon {...p}><path d="M9.5 2A2.5 2.5 0 0 1 12 4.5v15a2.5 2.5 0 0 1-4.96.44 2.5 2.5 0 0 1-2.96-3.08 3 3 0 0 1-.34-5.58 2.5 2.5 0 0 1 1.32-4.24 2.5 2.5 0 0 1 1.98-3A2.5 2.5 0 0 1 9.5 2Z"/><path d="M14.5 2A2.5 2.5 0 0 0 12 4.5v15a2.5 2.5 0 0 0 4.96.44 2.5 2.5 0 0 0 2.96-3.08 3 3 0 0 0 .34-5.58 2.5 2.5 0 0 0-1.32-4.24 2.5 2.5 0 0 0-1.98-3A2.5 2.5 0 0 0 14.5 2Z"/></Icon>;
const Chart     = (p) => <Icon {...p}><path d="M3 3v18h18"/><path d="M7 16V8"/><path d="M12 16v-5"/><path d="M17 16v-3"/></Icon>;
const Sparkles  = (p) => <Icon {...p}><path d="m12 3-1.9 5.8a2 2 0 0 1-1.3 1.3L3 12l5.8 1.9a2 2 0 0 1 1.3 1.3L12 21l1.9-5.8a2 2 0 0 1 1.3-1.3L21 12l-5.8-1.9a2 2 0 0 1-1.3-1.3Z"/></Icon>;
const Clock     = (p) => <Icon {...p}><circle cx="12" cy="12" r="10"/><polyline points="12 6 12 12 16 14"/></Icon>;
const Check     = (p) => <Icon {...p}><polyline points="20 6 9 17 4 12"/></Icon>;
const Zap       = (p) => <Icon {...p}><polygon points="13 2 3 14 12 14 11 22 21 10 12 10 13 2"/></Icon>;
const Book      = (p) => <Icon {...p}><path d="M2 3h6a4 4 0 0 1 4 4v14a3 3 0 0 0-3-3H2Z"/><path d="M22 3h-6a4 4 0 0 0-4 4v14a3 3 0 0 1 3-3h7Z"/></Icon>;
const Github    = (p) => <Icon {...p}><path d="M15 22v-4a4.8 4.8 0 0 0-1-3.5c3 0 6-2 6-5.5.08-1.25-.27-2.48-1-3.5.28-1.15.28-2.35 0-3.5 0 0-1 0-3 1.5-2.64-.5-5.36-.5-8 0C6 2 5 2 5 2c-.3 1.15-.3 2.35 0 3.5A5.4 5.4 0 0 0 4 9c0 3.5 3 5.5 6 5.5-.39.49-.68 1.05-.85 1.65S8.93 17.42 9 18v4"/></Icon>;

// ── Tomato logo mark ─────────────────────────────────────────────────────
const TomatoMark = ({ size = 28 }) => (
  <svg viewBox="0 0 60 66" width={size} height={size * 1.1} aria-hidden="true">
    <line x1="30" y1="14" x2="30" y2="4" stroke="#3a8c3a" strokeWidth="3.5" strokeLinecap="round"/>
    <path d="M30 10 C24 4, 16 6, 18 12" stroke="#3a8c3a" strokeWidth="2.5" strokeLinecap="round" fill="none"/>
    <path d="M30 10 C36 4, 44 6, 42 12" stroke="#3a8c3a" strokeWidth="2.5" strokeLinecap="round" fill="none"/>
    <ellipse cx="30" cy="40" rx="24" ry="22" fill="#e8342a"/>
    <ellipse cx="21" cy="30" rx="6" ry="5" fill="#f06a60" opacity="0.55"/>
  </svg>
);

// ── Primitives ───────────────────────────────────────────────────────────
const Kicker = ({ children }) => (
  <p style={{ fontSize: "0.72rem", fontWeight: 600, color: "var(--fg-4)", letterSpacing: "0.3em", textTransform: "uppercase" }}>{children}</p>
);
const Hairline = ({ maxWidth }) => (
  <div style={{ height: 1, background: "var(--border)", width: "100%", maxWidth, margin: "16px 0 24px" }}/>
);
const Button = ({ children, variant = "primary", size = "md", href, onClick, icon, style }) => {
  const btnStyles = {
    primary: { background: "var(--brand-primary)", color: "#fff", border: "none" },
    outline: { background: "#fff", color: "var(--fg-1)", border: "1px solid var(--border)" },
    ghost:   { background: "transparent", color: "var(--fg-3)", border: "none" },
  }[variant];
  const pad = size === "lg" ? "14px 26px" : "10px 20px";
  const fs = size === "lg" ? 16 : 14;
  const Tag = href ? "a" : "button";
  return (
    <Tag href={href} onClick={onClick} style={{
      ...btnStyles, padding: pad, fontSize: fs, fontWeight: 500, fontFamily: "inherit",
      display: "inline-flex", alignItems: "center", gap: 8, borderRadius: 9999,
      cursor: "pointer", textDecoration: "none", transition: "all 200ms ease",
      ...style
    }}
    onMouseEnter={(e) => { if (variant==="primary") e.currentTarget.style.background = "#12325c"; }}
    onMouseLeave={(e) => { if (variant==="primary") e.currentTarget.style.background = "var(--brand-primary)"; }}
    >
      {children}
      {icon}
    </Tag>
  );
};

const FolioCard = ({ children, style, onClick, hover = true, ...rest }) => {
  const [h, setH] = useState(false);
  return (
    <article onClick={onClick}
      onMouseEnter={() => setH(true)} onMouseLeave={() => setH(false)}
      style={{
        background: "#fff",
        border: "1px solid var(--border)",
        borderRadius: 24,
        padding: 24,
        boxShadow: hover && h ? "0 8px 32px rgba(22,59,107,0.12)" : "0 1px 0 rgba(15,23,42,0.02)",
        transform: hover && h ? "scale(1.015)" : "scale(1)",
        transition: "all 300ms cubic-bezier(0.4,0,0.2,1)",
        display: "flex", flexDirection: "column", gap: 16,
        cursor: onClick ? "pointer" : "default",
        ...style
      }} {...rest}>
      {children}
    </article>
  );
};

const ImagePanel = ({ src, alt, ratio = "4/3", contentFit = "contain", pad = 20, style }) => (
  <div style={{
    aspectRatio: ratio, background: "#f5f7fb", border: "1px solid var(--border)",
    borderRadius: 20, padding: pad, overflow: "hidden", display: "flex",
    alignItems: "center", justifyContent: "center", ...style,
  }}>
    {src && <img src={src} alt={alt} style={{ width: "100%", height: "100%", objectFit: contentFit }}/>}
  </div>
);

const StatPill = ({ children, style }) => {
  const [h, setH] = useState(false);
  return (
    <div onMouseEnter={() => setH(true)} onMouseLeave={() => setH(false)}
      style={{
        border: "1px solid var(--border)",
        background: h ? "var(--brand-primary)" : "#fff",
        borderRadius: 16, padding: "12px 16px", fontSize: 14,
        color: h ? "#fff" : "var(--fg-2)",
        boxShadow: "0 2px 4px rgba(15,23,42,0.03)",
        transition: "all 200ms ease", cursor: "default",
        ...style
      }}>{children}</div>
  );
};

const Chip = ({ icon, children, style }) => {
  const [h, setH] = useState(false);
  return (
    <span onMouseEnter={() => setH(true)} onMouseLeave={() => setH(false)}
      style={{
        display: "inline-flex", alignItems: "center", gap: 8,
        border: "1px solid var(--border)",
        background: h ? "var(--brand-primary)" : "#fff",
        padding: "8px 16px", borderRadius: 9999, fontSize: 13,
        color: h ? "#fff" : "var(--fg-3)",
        transition: "all 200ms ease", cursor: "default",
        ...style
      }}>
      {icon && <span style={{ color: h ? "#fff" : "var(--brand-primary)", display: "inline-flex", transition: "color 200ms ease" }}>{icon}</span>}
      {children}
    </span>
  );
};

const SectionHead = ({ kicker, title, description }) => (
  <div>
    <Kicker>{kicker}</Kicker>
    <Hairline/>
    <h2 style={{ color: "var(--brand-primary)", fontWeight: 500, letterSpacing: "-0.05em", fontSize: 44, lineHeight: 1.05 }}>{title}</h2>
    {description && <p style={{ color: "var(--fg-3)", fontSize: 18, lineHeight: 1.75, marginTop: 16, maxWidth: 640 }}>{description}</p>}
  </div>
);

// Flip card — signature interaction
const FlipCard = ({ front, back, size = 380 }) => {
  const [open, setOpen] = useState(false);
  return (
    <div style={{ perspective: 1000, width: size, height: size }}
      onMouseEnter={() => setOpen(true)}
      onMouseLeave={() => setOpen(false)}
      onClick={() => setOpen(o => !o)}
    >
      <div style={{
        width: "100%", height: "100%", position: "relative",
        transformStyle: "preserve-3d",
        transform: open ? "rotateY(180deg)" : "rotateY(0deg)",
        transition: "transform 600ms cubic-bezier(0.4, 0, 0.2, 1)",
        cursor: "pointer",
      }}>
        <div style={{
          position: "absolute", inset: 0, backfaceVisibility: "hidden",
          WebkitBackfaceVisibility: "hidden",
          background: "#fff", border: "1px solid var(--border)", borderRadius: 24, overflow: "hidden",
          boxShadow: open ? "0 8px 32px rgba(22,59,107,0.18)" : "0 1px 0 rgba(15,23,42,0.02)",
        }}>{front}</div>
        <div style={{
          position: "absolute", inset: 0, backfaceVisibility: "hidden",
          WebkitBackfaceVisibility: "hidden",
          transform: "rotateY(-180deg)",
          background: "#fff", border: "1px solid var(--border)", borderRadius: 24, padding: 20,
          display: "flex", flexDirection: "column", justifyContent: "center", gap: 10,
          boxShadow: "0 8px 32px rgba(22,59,107,0.18)",
        }}>{back}</div>
      </div>
    </div>
  );
};

// Hero background wrapper
const HeroBG = ({ children, style }) => (
  <section style={{
    position: "relative", overflow: "hidden",
    background: "linear-gradient(135deg, #ffffff 0%, #ffffff 46%, #f5f7fb 46%, #f5f7fb 100%)",
    ...style,
  }}>
    <div style={{ position: "absolute", inset: 0, pointerEvents: "none" }}>
      <div style={{ position: "absolute", left: 0, top: 0, height: 1, width: "100%", background: "var(--border)" }}/>
      <div style={{ position: "absolute", right: 0, top: 0, height: "100%", width: "50%",
        background: "radial-gradient(circle at top right, rgba(22,59,107,0.08), transparent 58%)" }}/>
    </div>
    <div style={{ position: "relative" }}>{children}</div>
  </section>
);

// Footer (shared across surfaces)
const Footer = ({ onNav }) => {
  const isMobile = useIsMobile();
  return (
    <footer style={{ borderTop: "1px solid var(--border)", background: "#fff", padding: isMobile ? "48px 0 64px" : "48px 0" }}>
      <div style={{ maxWidth: 1280, margin: "0 auto", padding: "0 32px",
        display: "flex", flexDirection: isMobile ? "column" : "row", flexWrap: "wrap", 
        justifyContent: "space-between", gap: isMobile ? 40 : 32, alignItems: isMobile ? "flex-start" : "center", textAlign: "left" }}>
        <div style={{ fontSize: 15, color: "var(--fg-3)", lineHeight: 1.6 }}>
          <p>Built by <b style={{ color: "var(--brand-primary)", fontWeight: 500 }}>Zohaib Javed</b> — Zeejay Lab</p>
          <p style={{ marginTop: 8 }}>
            <a href="https://zohaib26.gumroad.com/l/blvfug" target="_blank" rel="noopener"
               style={{ color: "var(--brand-primary)", textDecoration: "none", fontWeight: 500 }}>Gumroad page</a>
          </p>
        </div>
        <nav style={{ display: "flex", flexDirection: isMobile ? "column" : "row", gap: isMobile ? 20 : 24, alignItems: isMobile ? "flex-start" : "center", flexWrap: "wrap", width: isMobile ? "100%" : "auto" }}>
          {["about", "prs", "book", "products"].map(k => (
            <a key={k} href={`#${k}`} onClick={(e) => { e.preventDefault(); onNav && onNav(k); }}
               style={{ color: "var(--fg-2)", fontSize: 15, fontWeight: 500, textDecoration: "none", cursor: "pointer" }}>
              {{about:"About", prs:"PRS 3.0", book:"Open Source Book", products:"Products"}[k]}
            </a>
          ))}
          <div style={isMobile ? { width: "100%", marginTop: 8 } : {}}>
            <Button onClick={(e) => { e.preventDefault(); onNav && onNav("about", undefined, "contact-cards"); }} size="md" icon={<ArrowRight size={14}/>} style={isMobile ? { width: "100%", justifyContent: "center" } : {}}>Contact</Button>
          </div>
        </nav>
      </div>
    </footer>
  );
};

// Container
const Container = ({ children, style, innerStyle, pad = "py-hero" }) => {
  const innerPad =
    pad === "section" ? "64px 0" :
    pad === "compact" ? "32px 0" :
    pad === "tight"   ? "20px 0" :
    pad === "hero"    ? "40px 0" : 0;
  return (
    <div style={{ maxWidth: 1280, margin: "0 auto", padding: "0 32px", ...style }}>
      <div style={{ padding: innerPad, ...innerStyle }}>{children}</div>
    </div>
  );
};

Object.assign(window, {
  Icon, ArrowRight, ArrowLeft, ChevL, Brain, Chart, Sparkles, Clock, Check, Zap, Book, Github,
  TomatoMark, Kicker, Hairline, Button, FolioCard, ImagePanel, StatPill, Chip,
  SectionHead, FlipCard, HeroBG, Footer, Container,
});
