/* Project data + case-study modal */

const { useState: _useState, useEffect: _useEffect } = React;
const useState = React.useState;
const useEffect = React.useEffect;
const { navigate } = window;

/*
  ── Adding a card image ──────────────────────────────────────────────
  Each project supports an optional `image` (a screenshot / cover photo).
  Drop the file into  assets/work/  and set the path, e.g.

      image: "assets/work/loyalty-app.png",

  Leave it as "" to fall back to the gradient (`hero`) treatment.

  ── Live links ───────────────────────────────────────────────────────
  `links` is an array of { label, href, kind }. kind is one of
  "play" | "apple" | "web" and only controls the little icon.
*/

const PROJECTS = [
{
  id: "loyalty-app",
  title: "Loyalty App",
  subtitle: "Mobile companion for a retail loyalty programme — points, rewards, and member tiers.",
  kind: "Mobile · iOS & Android",
  year: "2026",
  client: "Retail · Auto Works",
  stack: ["Flutter", "Supabase", "Resend", "Twilio"],
  timeline: "21 weeks",
  hero: "linear-gradient(135deg, #00FFC2 0%, #00b894 55%, #0a3a30 100%)",
  bg: "#062e25",
  image: "", // e.g. "assets/work/loyalty-app.png"
  links: [
  { label: "Google Play", href: "https://play.google.com/store/apps/details?id=com.m2_tech.mccarthy_loyalty_app", kind: "play" },
  { label: "App Store", href: "https://apps.apple.com/za/app/mccarthy-loyalty-app/id6760186943", kind: "apple" }],

  summary:
  "A consumer-facing loyalty app that turns everyday purchases into points, tiers and personalised rewards. McCarthy Auto Trimmers ensures you get rewarded for keeping your vehicle in top shape. Register yourself and your vehicles to start earning points, tracking your loyalty status, and unlocking exclusive promotions right from your pocket.",
  outcomes: [
  { k: "2", v: "Automobile Client" },
  { k: "2", v: "different app stores Google Play & Apple App Store" },
  { k: "24/7", v: "support for client updates" }],

  highlights: [
  "Onboarding in under 30 seconds with magic-link sign-in",
  "QR scanner for in-store point collection, works offline",
  "Tiered rewards engine with configurable rules from the dashboard",
  "Email notifications for OTP"]

},
{
  id: "loyalty-dashboard",
  title: "Loyalty Dashboard",
  subtitle: "Desktop operations centre for running campaigns, issuing rewards and reading behaviour.",
  kind: "Desktop · Web App",
  year: "2026",
  client: "ZA Automobile Stores",
  stack: ["React", "Javascript", "PostgreSQL", "Recharts"],
  timeline: "8 weeks",
  hero: "linear-gradient(135deg, #1a1a1a 0%, #2c2c2c 50%, #00FFC2 200%)",
  bg: "#141413",
  image: "", // e.g. "assets/work/loyalty-dashboard.png"
  links: [
  { label: "Open dashboard", href: "https://mccarthy-dashboard.web.app/dashboard", kind: "web" }],

  summary:
  "The operations dashboard that sits behind the consumer app — a single pane of glass for the marketing team to spin up campaigns, audit point ledgers, manage member tiers and read the numbers that matter.",
  outcomes: [
  { k: "< 200ms", v: "median query latency" },
  { k: "14", v: "role-based permission levels" },
  { k: "5×", v: "faster campaign creation vs. previous tool" }],

  highlights: [
  "Real-time analytics with drill-down on every KPI",
  "Service income tracker, with specific client spending habitats",
  "Redeemable rewards in dashboard",
  "Exportable audit trail for finance and compliance sign-off"]

},
{
  id: "portuguese-site",
  title: "Portuguese Club Site",
  subtitle: "A multi-page website for a Portuguese client — bilingual, CMS-driven, SEO-tuned.",
  kind: "Website · Corporate",
  year: "2026",
  client: "Services · Lisbon, PT",
  stack: ["Next.js", "Sanity CMS", "Tailwind", "Vercel"],
  timeline: "15 working days",
  hero: "linear-gradient(135deg, #F6F4EF 0%, #E5E1D7 60%, #00FFC2 180%)",
  bg: "#E5E1D7",
  image: "", // e.g. "assets/work/portuguese-site.png"
  links: [
  { label: "Visit website", href: "https://www.apcabo.co.za/", kind: "web" }],

  summary:
  "A bilingual site (PT / EN) built to inform Portuguese Club members about events, highlights, contact us and download registration form. Custom admin upload tool to facilitate event uploads and gallery uploads.",
  outcomes: [
  { k: "15", v: "join enquiries for the 1st month" },
  { k: "+212%", v: "organic traffic in 90 days" },
  { k: "15 days", v: "from kickoff to launch" }],

  highlights: [
  "Bilingual content model with a single publish workflow",
  "CMS tool to add events and gallery images",
  "On-page SEO foundation: schema, sitemaps, Core Web Vitals",
  "Custom contact funnel with lead-quality scoring"]

}];


/* ───── tiny inline icons for link kinds ───── */
const LinkIcon = ({ kind }) => {
  if (kind === "play")
  return <svg width="15" height="15" viewBox="0 0 24 24" fill="currentColor"><path d="M3 2.5v19a.8.8 0 0 0 1.22.69l16.5-9.5a.8.8 0 0 0 0-1.38L4.22 1.81A.8.8 0 0 0 3 2.5z" /></svg>;
  if (kind === "apple")
  return <svg width="15" height="15" viewBox="0 0 24 24" fill="currentColor"><path d="M16.4 12.6c0-2.2 1.8-3.3 1.9-3.4-1-1.5-2.6-1.7-3.2-1.7-1.4-.1-2.7.8-3.3.8-.7 0-1.7-.8-2.8-.8-1.4 0-2.8.8-3.5 2.1-1.5 2.6-.4 6.5 1.1 8.6.7 1 1.5 2.2 2.6 2.1 1-.04 1.4-.7 2.7-.7s1.6.7 2.7.6c1.1-.02 1.8-1 2.5-2 .8-1.2 1.1-2.3 1.1-2.4-.02-.01-2.1-.8-2.1-3.2zM14.3 5.9c.6-.7 1-1.7.9-2.7-.86.03-1.9.57-2.5 1.3-.5.6-1 1.6-.9 2.6.96.07 1.9-.49 2.5-1.2z" /></svg>;
  return <svg width="15" height="15" viewBox="0 0 24 24" fill="none" stroke="currentColor" strokeWidth="2"><path d="M7 17L17 7M9 7h8v8" /></svg>;
};

/* ───── Project card (shared) ───── */
const ProjectCard = ({ p, big = false, onClick }) =>
<button onClick={onClick} className="reveal" style={{
  position: "relative",
  textAlign: "left",
  padding: 0,
  overflow: "hidden",
  borderRadius: 18,
  border: "1px solid var(--line)",
  background: p.bg || "var(--paper-2)",
  minHeight: big ? 520 : 250,
  display: "flex",
  flexDirection: "column",
  justifyContent: "space-between",
  cursor: "pointer",
  transition: "all 0.4s var(--ease)",
  fontFamily: "inherit"
}}
onMouseEnter={(e) => {e.currentTarget.style.transform = "translateY(-4px)";e.currentTarget.style.borderColor = "var(--ink)";}}
onMouseLeave={(e) => {e.currentTarget.style.transform = "";e.currentTarget.style.borderColor = "var(--line)";}}>

    {/* gradient base */}
    <div style={{ position: "absolute", inset: 0, background: p.hero, backgroundSize: "cover", backgroundPosition: "center" }} />
    {/* optional image layer */}
    {p.image &&
  <div style={{ position: "absolute", inset: 0, backgroundImage: `url('${p.image}')`, backgroundSize: "cover", backgroundPosition: "center" }} />
  }
    {/* legibility overlay */}
    <div style={{ position: "absolute", inset: 0, background: "linear-gradient(180deg, rgba(11,11,11,0) 40%, rgba(11,11,11,0.85) 100%)" }} />

    <div style={{ position: "relative", padding: big ? 32 : 24, color: "var(--paper)", display: "flex", justifyContent: "space-between", alignItems: "start", gap: 10 }}>
      <div style={{ display: "flex", gap: 8, flexWrap: "wrap" }}>
        <span className="mono" style={{ fontSize: 11, letterSpacing: "0.12em", padding: "6px 10px", background: "rgba(255,255,255,0.15)", backdropFilter: "blur(8px)", borderRadius: 999 }}>{p.kind}</span>
        {p.links && p.links.length > 0 &&
      <span className="mono" style={{ fontSize: 11, letterSpacing: "0.12em", padding: "6px 10px", background: "var(--accent)", color: "var(--ink)", borderRadius: 999, display: "inline-flex", alignItems: "center", gap: 6 }}>
            <span style={{ width: 6, height: 6, borderRadius: 999, background: "var(--ink)" }} /> LIVE
          </span>
      }
      </div>
      <span style={{ width: 40, height: 40, borderRadius: 999, background: "var(--accent)", color: "var(--ink)", display: "inline-flex", alignItems: "center", justifyContent: "center", flexShrink: 0 }}>
        <svg width="16" height="16" viewBox="0 0 24 24" fill="none" stroke="currentColor" strokeWidth="2"><path d="M7 17L17 7M7 7h10v10" /></svg>
      </span>
    </div>
    <div style={{ position: "relative", padding: big ? 32 : 24, color: "var(--paper)" }}>
      <h3 className="display" style={{ fontSize: big ? 56 : 32, margin: "0 0 8px", lineHeight: 0.95, fontFamily: "Roboto" }}>{p.title}</h3>
      <p style={{ fontSize: 14, margin: 0, color: "rgba(246,244,239,0.75)", maxWidth: "48ch" }}>{p.subtitle}</p>
    </div>
  </button>;


const CaseModal = ({ project, onClose }) => {
  useEffect(() => {
    if (!project) return;
    const onKey = (e) => {if (e.key === "Escape") onClose();};
    document.body.style.overflow = "hidden";
    window.addEventListener("keydown", onKey);
    return () => {document.body.style.overflow = "";window.removeEventListener("keydown", onKey);};
  }, [project, onClose]);

  if (!project) return null;

  return (
    <div
      onClick={onClose}
      style={{
        position: "fixed", inset: 0, zIndex: 300,
        background: "rgba(11,11,11,0.7)",
        backdropFilter: "blur(10px)",
        display: "flex", alignItems: "flex-start", justifyContent: "center",
        padding: "40px 20px",
        overflowY: "auto",
        animation: "fade 0.3s var(--ease)"
      }}>
      
      <div onClick={(e) => e.stopPropagation()} style={{
        width: "100%",
        maxWidth: 1120,
        background: "var(--paper)",
        borderRadius: 20,
        overflow: "hidden",
        animation: "slideUp 0.5s var(--ease)",
        border: "1px solid var(--line)"
      }}>
        {/* hero banner */}
        <div style={{ position: "relative", minHeight: 320, background: project.hero, padding: 32 }}>
          {project.image &&
          <div style={{ position: "absolute", inset: 0, backgroundImage: `url('${project.image}')`, backgroundSize: "cover", backgroundPosition: "center" }} />
          }
          <div style={{ position: "absolute", inset: 0, background: "linear-gradient(180deg, rgba(11,11,11,0.15) 0%, rgba(11,11,11,0.65) 100%)" }} />
          <button onClick={onClose} aria-label="Close" style={{
            position: "absolute", top: 20, right: 20,
            width: 44, height: 44, borderRadius: 999,
            background: "var(--ink)", color: "var(--paper)",
            display: "inline-flex", alignItems: "center", justifyContent: "center",
            border: "1px solid var(--ink)", zIndex: 2
          }}>
            <svg width="18" height="18" viewBox="0 0 24 24" fill="none" stroke="currentColor" strokeWidth="2"><path d="M18 6 6 18M6 6l12 12" /></svg>
          </button>
          <div style={{ position: "relative", display: "flex", gap: 10, flexWrap: "wrap", zIndex: 1 }}>
            <span className="mono" style={{ fontSize: 11, letterSpacing: "0.14em", textTransform: "uppercase", padding: "6px 12px", background: "rgba(255,255,255,0.25)", backdropFilter: "blur(8px)", borderRadius: 999, color: "#fff" }}>{project.kind}</span>
            <span className="mono" style={{ fontSize: 11, letterSpacing: "0.14em", textTransform: "uppercase", padding: "6px 12px", background: "rgba(255,255,255,0.25)", backdropFilter: "blur(8px)", borderRadius: 999, color: "#fff" }}>{project.year}</span>
          </div>
          <div style={{ position: "absolute", left: 32, right: 32, bottom: 32, color: "#fff", zIndex: 1 }}>
            <h2 className="display" style={{ fontSize: "clamp(40px, 6vw, 72px)", margin: "0 0 12px", lineHeight: 0.95, letterSpacing: "-0.02em" }}>
              {project.title}
            </h2>
            <p style={{ fontSize: 17, margin: 0, maxWidth: "60ch", color: "rgba(255,255,255,0.9)" }}>{project.subtitle}</p>
          </div>
        </div>

        {/* meta strip */}
        <div style={{ display: "grid", gridTemplateColumns: "repeat(4, 1fr)", borderBottom: "1px solid var(--line)" }} className="case-meta">
          {[
          ["Client", project.client],
          ["Timeline", project.timeline],
          ["Discipline", project.kind.split("·")[0].trim()],
          ["Year", project.year]].
          map(([k, v], i) =>
          <div key={k} style={{ padding: 20, borderRight: i < 3 ? "1px solid var(--line)" : "none" }}>
              <div className="eyebrow" style={{ marginBottom: 6 }}>{k}</div>
              <div style={{ fontSize: 14, fontWeight: 600 }}>{v}</div>
            </div>
          )}
        </div>

        {/* body */}
        <div style={{ padding: 40, display: "grid", gridTemplateColumns: "1.4fr 1fr", gap: 40 }} className="case-body">
          <div>
            <div className="eyebrow" style={{ marginBottom: 12 }}>▸ Overview</div>
            <p style={{ fontSize: 18, lineHeight: 1.55, margin: "0 0 32px", color: "var(--ink-2)" }}>{project.summary}</p>
            <div className="eyebrow" style={{ marginBottom: 12 }}>▸ Highlights</div>
            <ul style={{ listStyle: "none", padding: 0, margin: 0, display: "flex", flexDirection: "column", gap: 12 }}>
              {project.highlights.map((h, i) =>
              <li key={i} style={{ display: "flex", gap: 14, fontSize: 15 }}>
                  <span style={{ width: 28, height: 28, borderRadius: 999, background: "var(--accent)", color: "var(--ink)", display: "inline-flex", alignItems: "center", justifyContent: "center", flexShrink: 0, fontSize: 12, fontWeight: 700 }}>{String(i + 1).padStart(2, "0")}</span>
                  <span>{h}</span>
                </li>
              )}
            </ul>
          </div>
          <aside style={{ display: "flex", flexDirection: "column", gap: 24 }}>
            {project.links && project.links.length > 0 &&
            <div style={{ background: "var(--ink)", color: "var(--paper)", borderRadius: 14, padding: 24 }}>
                <div className="eyebrow" style={{ marginBottom: 14, color: "rgba(246,244,239,0.6)" }}>▸ See it live</div>
                <div style={{ display: "flex", flexDirection: "column", gap: 10 }}>
                  {project.links.map((l) =>
                <a key={l.href} href={l.href} target="_blank" rel="noreferrer" style={{
                  display: "flex", alignItems: "center", justifyContent: "space-between", gap: 12,
                  padding: "12px 16px", borderRadius: 999, background: "var(--accent)", color: "var(--ink)",
                  fontSize: 14, fontWeight: 600, transition: "transform 0.2s var(--ease)"
                }}
                onMouseEnter={(e) => e.currentTarget.style.transform = "translateY(-2px)"}
                onMouseLeave={(e) => e.currentTarget.style.transform = ""}>
                      <span style={{ display: "inline-flex", alignItems: "center", gap: 10 }}><LinkIcon kind={l.kind} />{l.label}</span>
                      <svg width="14" height="14" viewBox="0 0 24 24" fill="none" stroke="currentColor" strokeWidth="2"><path d="M5 12h14M13 5l7 7-7 7" /></svg>
                    </a>
                )}
                </div>
              </div>
            }
            <div style={{ background: "var(--paper-2)", border: "1px solid var(--line)", borderRadius: 14, padding: 24 }}>
              <div className="eyebrow" style={{ marginBottom: 14 }}>Outcomes</div>
              <div style={{ display: "flex", flexDirection: "column", gap: 16 }}>
                {project.outcomes.map((o, i) =>
                <div key={i} style={{ borderTop: i > 0 ? "1px solid var(--line)" : "none", paddingTop: i > 0 ? 14 : 0 }}>
                    <div className="display" style={{ fontSize: 36, lineHeight: 1 }}>{o.k}</div>
                    <div style={{ color: "var(--muted)", fontSize: 13, marginTop: 4 }}>{o.v}</div>
                  </div>
                )}
              </div>
            </div>
            <div>
              <div className="eyebrow" style={{ marginBottom: 10 }}>Stack</div>
              <div style={{ display: "flex", flexWrap: "wrap", gap: 6 }}>
                {project.stack.map((s) =>
                <span key={s} className="mono" style={{ fontSize: 11, padding: "6px 10px", borderRadius: 999, border: "1px solid var(--line)", background: "var(--paper-2)" }}>{s}</span>
                )}
              </div>
            </div>
            <a href="#/contact" onClick={(e) => {e.preventDefault();onClose();navigate("contact");}} className="btn btn-primary" style={{ justifyContent: "center" }}>
              Start a similar project →
            </a>
          </aside>
        </div>
      </div>
      <style>{`
        @keyframes fade { from { opacity: 0; } }
        @keyframes slideUp { from { opacity: 0; transform: translateY(40px); } }
        @media (max-width: 820px) {
          .case-body { grid-template-columns: 1fr !important; padding: 28px !important; }
          .case-meta { grid-template-columns: 1fr 1fr !important; }
          .case-meta > *:nth-child(2) { border-right: none !important; }
        }
      `}</style>
    </div>);

};

/* ───── Work page ───── */
const WorkPage = ({ openCase }) => {
  const [filter, setFilter] = useState("All");
  const filters = ["All", "Website", "Mobile", "Desktop"];
  const filtered = PROJECTS.filter((p) => filter === "All" || p.kind.toLowerCase().includes(filter.toLowerCase()));

  // The global scroll-reveal observer lives in <App> and does NOT re-run when this
  // page's local `filter` state changes — so cards shown by a new filter would stay
  // at opacity:0 (invisible, but still clickable). Reveal the current results here.
  useEffect(() => {
    const r = requestAnimationFrame(() => {
      document.querySelectorAll(".work-grid .reveal").forEach((el) => el.classList.add("in"));
    });
    return () => cancelAnimationFrame(r);
  }, [filter]);

  return (
    <section style={{ padding: "140px 0 80px" }}>
      <div className="wrap">
        <div style={{ marginBottom: 40 }}>
          <div className="eyebrow" style={{ marginBottom: 20 }}>▸ SELECTED WORK · 2025 – 2026</div>
          <h1 className="display" style={{ fontSize: "clamp(56px, 9vw, 140px)", margin: 0, lineHeight: 0.95, letterSpacing: "-0.02em", fontFamily: "Roboto" }}>
            Things we've<br /><em style={{ fontStyle: "italic", fontWeight: 400, fontFamily: "Roboto" }}>shipped</em>.
          </h1>
        </div>

        <div style={{ display: "flex", gap: 6, flexWrap: "wrap", marginBottom: 32, borderBottom: "1px solid var(--line)", paddingBottom: 16 }}>
          {filters.map((f) =>
          <button key={f} onClick={() => setFilter(f)} className={"nav-link" + (filter === f ? " active" : "")} style={{ fontSize: 13 }}>
              {f}
            </button>
          )}
          <div style={{ marginLeft: "auto", fontFamily: "var(--f-mono)", fontSize: 12, color: "var(--muted)", alignSelf: "center" }}>
            {filtered.length} projects
          </div>
        </div>

        <div style={{ display: "grid", gridTemplateColumns: "repeat(2, 1fr)", gap: 24 }} className="work-grid">
          {filtered.map((p, i) =>
          <ProjectCard key={p.id} p={p} big={i === 0} onClick={() => openCase(p)} />
          )}
        </div>

        <div style={{ marginTop: 80, padding: 40, background: "var(--paper-2)", borderRadius: 18, border: "1px solid var(--line)", display: "grid", gridTemplateColumns: "1fr auto", gap: 32, alignItems: "center" }} className="work-cta">
          <div>
            <h3 className="display" style={{ fontSize: 36, margin: "0 0 8px", lineHeight: 1, fontFamily: "Roboto" }}>Your project could be next.</h3>
            <p style={{ color: "var(--muted)", margin: 0, fontSize: 15 }}>Scoping calls are free. Proposals come back in 48 hours.</p>
          </div>
          <a href="#/contact" onClick={(e) => {e.preventDefault();navigate("contact");}} className="btn btn-primary">
            Tell us about it →
          </a>
        </div>
      </div>
      <style>{`
        @media (max-width: 820px) {
          .work-grid { grid-template-columns: 1fr !important; }
          .work-cta { grid-template-columns: 1fr !important; }
        }
      `}</style>
    </section>);

};

Object.assign(window, { PROJECTS, CaseModal, WorkPage, ProjectCard });