// pages/home-sections.jsx — modular sections used by home (and reused by inner pages)

// ─── trusted-by marquee ───
function MarqueeStrip() {
  const items = [
    "REAL ESTATE AGENCIES",
    "LAW FIRMS",
    "MORTGAGE BROKERS",
    "MARKETING AGENCIES",
    "ACROSS AUSTRALIA"
  ];
  return (
    <section style={{ padding: "60px 0", borderTop: "1px solid var(--line)", borderBottom: "1px solid var(--line)", overflow: "hidden" }}>
      <div className="wrap" style={{ marginBottom: 28 }}>
        <div className="kicker" style={{ textAlign: "center", fontWeight: 700 }}>Trusted by</div>
      </div>
      <div style={{ display: "flex", maskImage: "linear-gradient(90deg, transparent 0%, #000 10%, #000 90%, transparent 100%)" }}>
        <div className="marquee">
          {[...items, ...items].map((it, i) => (
            <span key={i} style={{
              fontFamily: "var(--f-mono)", fontSize: 22, fontWeight: 500,
              color: "var(--ink-3)", letterSpacing: "0.04em",
              display: "inline-flex", alignItems: "center", gap: 56,
              whiteSpace: "nowrap", flexShrink: 0
            }}>
              {it}
              <span style={{ width: 6, height: 6, borderRadius: "50%", background: "var(--ink-5)" }}></span>
            </span>
          ))}
        </div>
      </div>
    </section>
  );
}

// ─── section header ───
function SectionHeader({ eyebrow, title, sub, align = "left", action }) {
  return (
    <div className="reveal" style={{ textAlign: align, marginBottom: 56 }}>
      <div className="kicker" style={{ marginBottom: 18 }}>{eyebrow}</div>
      <div style={{ display: "flex", alignItems: "end", justifyContent: align === "center" ? "center" : "space-between", gap: 40, flexWrap: "wrap" }}>
        <h2 className="h2" style={{ margin: 0, maxWidth: 880 }}>{title}</h2>
        {action}
      </div>
      {sub && (
        <p style={{ marginTop: 18, fontSize: 18, color: "var(--ink-3)", maxWidth: 680, lineHeight: 1.5 }}>
          {sub}
        </p>
      )}
    </div>
  );
}

// ─── services preview (home) ───
function ServicesPreview() {
  const services = window.WHITEN_DATA.services;
  return (
    <section className="section" id="services">
      <div className="wrap">
        <SectionHeader
          eyebrow="04 services / one outcome"
          title={<>Four services. <span className="serif-i" style={{ color: "var(--ink-3)" }}>One outcome:</span> your team only does the work that compounds.</>}
          action={<a href="services.html" className="btn btn-ghost">All services <Arrow /></a>}
        />

        <div style={{ display: "grid", gridTemplateColumns: "repeat(2, 1fr)", gap: "var(--gap)" }}>
          {services.map((s, i) => (
            <ServiceCard key={s.id} s={s} delay={i * 0.06} />
          ))}
        </div>
      </div>
    </section>
  );
}

function ServiceCard({ s, delay = 0 }) {
  return (
    <a href={`services.html#${s.id}`} className="reveal card service-card" style={{
      "--rd": `${delay}s`,
      display: "flex", flexDirection: "column", gap: 20, padding: 36,
      position: "relative", overflow: "hidden", minHeight: 320
    }}>
      <div style={{ display: "flex", justifyContent: "space-between", alignItems: "start" }}>
        <span className="mono" style={{ color: "var(--ink-4)", fontSize: 13 }}>{s.num}</span>
        <span className="tag">{s.tag}</span>
      </div>

      <div style={{ marginTop: "auto" }}>
        <h3 className="h2" style={{ margin: 0, fontSize: "clamp(28px, 3vw, 40px)" }}>{s.name}</h3>
        <p style={{ marginTop: 12, color: "var(--ink-3)", fontSize: 16, lineHeight: 1.5, maxWidth: 460 }}>
          {s.blurb}
        </p>
      </div>

      <div style={{
        display: "flex", justifyContent: "space-between", alignItems: "center",
        paddingTop: 20, borderTop: "1px solid var(--line)",
        fontSize: 13, color: "var(--ink-3)", fontFamily: "var(--f-mono)"
      }}>
        <span>{s.tools.slice(0, 3).join(" · ")}</span>
        <span style={{ display: "inline-flex", alignItems: "center", gap: 8, color: "var(--ink)" }}>
          See how it works <Arrow />
        </span>
      </div>
    </a>
  );
}

// ─── workflows preview (home): scrolling list ───
function WorkflowsPreview() {
  const wfs = window.WHITEN_DATA.workflows;
  const [filter, setFilter] = React.useState("all");
  const cats = ["all", ...new Set(wfs.map(w => w.category))];
  const shown = filter === "all" ? wfs : wfs.filter(w => w.category === filter);

  return (
    <section className="section" style={{ background: "var(--bg-deep)" }} id="workflows-preview">
      <div className="wrap">
        <SectionHeader
          eyebrow="16 workflows · ready to ship"
          title={<>A library of automations we've shipped, packaged so you can <span className="serif-i">point at one</span> and have it live in a fortnight.</>}
          action={<a href="workflows.html" className="btn btn-ghost">Browse all 16 <Arrow /></a>}
        />

        <div className="reveal" style={{ display: "flex", gap: 8, flexWrap: "wrap", marginBottom: 36 }}>
          {cats.map(c => (
            <button key={c} onClick={() => setFilter(c)}
              className="tag"
              style={{
                cursor: "pointer", border: "1px solid var(--line)",
                background: filter === c ? "var(--ink)" : "transparent",
                color: filter === c ? "var(--bg)" : "var(--ink-2)",
                padding: "8px 14px", fontSize: 12.5
              }}>
              {c === "all" ? "All" : c}
            </button>
          ))}
          <span style={{ marginLeft: "auto", alignSelf: "center", color: "var(--ink-3)", fontFamily: "var(--f-mono)", fontSize: 12 }}>
            {shown.length} of {wfs.length}
          </span>
        </div>

        <div style={{
          border: "1px solid var(--line)", borderRadius: 18, overflow: "hidden",
          background: "var(--bg-elev)"
        }}>
          {shown.slice(0, 8).map((w, i) => (
            <WorkflowRow key={w.id} w={w} i={i} isLast={i === Math.min(7, shown.length - 1)} />
          ))}
        </div>

        <div className="reveal" style={{
          marginTop: 96, textAlign: "center",
          display: "flex", flexDirection: "column", alignItems: "center", gap: 24
        }}>
          <p style={{
            margin: 0, maxWidth: 580,
            color: "var(--ink-3)", fontSize: 17, lineHeight: 1.5
          }}>
            Not sure which one fits your business? <span className="serif-i" style={{ fontSize: 19 }}>Book a free 15-minute call</span> — we'll tell you exactly which automation gives you the fastest return.
          </p>
          <a href="index.html#book" className="btn">
            Book a free assessment <Arrow />
          </a>
        </div>
      </div>
    </section>
  );
}

function WorkflowRow({ w, i, isLast }) {
  const [open, setOpen] = React.useState(false);
  return (
    <div className="reveal" style={{ "--rd": `${i * 0.03}s`, borderBottom: isLast ? "none" : "1px solid var(--line)" }}>
      <a href={`workflows.html#${w.id}`}
        onMouseEnter={() => setOpen(true)}
        onMouseLeave={() => setOpen(false)}
        style={{
          display: "grid",
          gridTemplateColumns: "60px 1.6fr 1fr 1.2fr 90px 110px 36px",
          alignItems: "center", gap: 24,
          padding: "20px 28px",
          background: open ? "color-mix(in oklab, var(--accent) 4%, transparent)" : "transparent",
          transition: "background .2s"
        }}>
        <span className="mono" style={{ color: "var(--ink-4)", fontSize: 13 }}>{w.num}</span>
        <span style={{ fontWeight: 500, fontSize: 16, color: open ? "var(--accent)" : "var(--ink)", transition: "color .2s" }}>
          {w.name}
        </span>
        <span style={{ color: "var(--ink-3)", fontSize: 13.5 }}>{w.category}</span>
        <span style={{ color: "var(--ink-3)", fontSize: 13, fontFamily: "var(--f-mono)" }}>
          {w.stack.join(" · ")}
        </span>
        <span style={{
          fontSize: 12, fontFamily: "var(--f-mono)", color: "var(--ink-2)",
          padding: "4px 10px", borderRadius: 999, background: "var(--accent-soft)", textAlign: "center"
        }}>
          {w.time}
        </span>
        <span className="tag" style={{ justifySelf: "start", whiteSpace: "nowrap" }}>
          {w.industry}
        </span>
        <span style={{
          opacity: open ? 1 : 0.4, transform: open ? "translateX(2px)" : "none",
          transition: "all .2s", color: open ? "var(--accent)" : "var(--ink-3)"
        }}>
          <Arrow />
        </span>
      </a>
    </div>
  );
}

// ─── case studies preview ───
function CasesPreview() {
  const cases = window.WHITEN_DATA.cases.slice(0, 3);
  return (
    <section className="section">
      <div className="wrap">
        <SectionHeader
          eyebrow="recent work · 2025"
          title={<>Real teams, <span className="serif-i">measurable hours back.</span></>}
          action={<a href="case-studies.html" className="btn btn-ghost">All case studies <Arrow /></a>}
        />

        <div style={{ display: "grid", gridTemplateColumns: "repeat(3, 1fr)", gap: "var(--gap)" }}>
          {cases.map((c, i) => (
            <CaseCard key={c.id} c={c} i={i} />
          ))}
        </div>
      </div>
    </section>
  );
}

function CaseCard({ c, i }) {
  return (
    <a href={`case-studies.html#${c.id}`} className="reveal" style={{ "--rd": `${i * 0.08}s`, display: "block" }}>
      <div style={{
        aspectRatio: "4 / 5",
        borderRadius: 18,
        background: `linear-gradient(135deg, ${["#2D5F5A", "#FF4B12", "#15130F"][i % 3]}, ${["#1A3A36", "#B83310", "#2F2B25"][i % 3]})`,
        position: "relative", overflow: "hidden",
        display: "flex", flexDirection: "column", justifyContent: "space-between",
        padding: 28, color: "#fff"
      }}>
        <svg width="100%" height="100%" viewBox="0 0 200 200" style={{ position: "absolute", inset: 0, opacity: 0.12 }}>
          <defs>
            <pattern id={`cs-${c.id}`} patternUnits="userSpaceOnUse" width="8" height="8" patternTransform="rotate(45)">
              <rect width="2" height="8" fill="#fff" />
            </pattern>
          </defs>
          <rect width="200" height="200" fill={`url(#cs-${c.id})`} />
        </svg>

        <div style={{ position: "relative", display: "flex", justifyContent: "space-between", alignItems: "start" }}>
          <span className="tag" style={{
            background: "rgba(255,255,255,.15)", color: "#fff", border: 0,
            fontSize: 11
          }}>{c.sector}</span>
          <span style={{ fontSize: 12, fontFamily: "var(--f-mono)", opacity: .7 }}>{c.year}</span>
        </div>
        <div style={{ position: "relative" }}>
          <div style={{ fontSize: "clamp(48px, 5vw, 72px)", fontWeight: 600, letterSpacing: "-0.04em", lineHeight: 1 }}>
            {c.metric}
          </div>
          <div style={{ fontSize: 14, opacity: .75, marginTop: 4, fontFamily: "var(--f-mono)" }}>{c.metricLabel}</div>
        </div>
      </div>
      <div style={{ marginTop: 18 }}>
        <div style={{ fontSize: 12, color: "var(--ink-3)", fontFamily: "var(--f-mono)", marginBottom: 6 }}>{c.client}</div>
        <div style={{ fontSize: 19, fontWeight: 500, letterSpacing: "-0.015em", lineHeight: 1.25, maxWidth: 360 }}>
          {c.headline}
        </div>
      </div>
    </a>
  );
}

// ─── how it works (3-step) ───
function HowItWorksPreview() {
  const steps = [
    {
      n: "01", t: "Map", k: "1 week",
      desc: "We sit with your team for two afternoons and watch how the work actually moves. Then we draw it — the official process and the real one.",
      side: "audit"
    },
    {
      n: "02", t: "Ship", k: "2–4 weeks",
      desc: "We build the first workflow end-to-end while you keep the lights on. You see weekly Looms; nothing goes live without your sign-off.",
      side: "build"
    },
    {
      n: "03", t: "Quiet", k: "ongoing",
      desc: "The automation runs itself. We babysit it for 30 days, then hand over keys. Your team forgets the manual version ever existed.",
      side: "calm"
    }
  ];

  return (
    <section className="section" id="how" style={{ background: "var(--bg-deep)" }}>
      <div className="wrap">
        <SectionHeader
          eyebrow="how it works"
          title={<>Three weeks. <span className="serif-i">From "we keep meaning to" to "running, untouched."</span></>}
          action={<a href="how-it-works.html" className="btn btn-ghost">Full process <Arrow /></a>}
        />

        <div style={{ display: "grid", gridTemplateColumns: "repeat(3, 1fr)", gap: "var(--gap)" }}>
          {steps.map((s, i) => (
            <div key={s.n} className="reveal card" style={{
              "--rd": `${i * 0.08}s`,
              padding: 32, display: "flex", flexDirection: "column", gap: 24, minHeight: 380
            }}>
              <div style={{ display: "flex", justifyContent: "space-between", alignItems: "center" }}>
                <span style={{
                  fontFamily: "var(--f-serif)", fontStyle: "italic", fontSize: 80, lineHeight: 1,
                  color: "var(--accent)", fontWeight: 400
                }}>{s.n}</span>
                <span className="tag">{s.k}</span>
              </div>
              <StepGlyph kind={s.side} />
              <div>
                <h3 className="h3" style={{ margin: 0 }}>{s.t}</h3>
                <p style={{ marginTop: 10, color: "var(--ink-3)", fontSize: 15, lineHeight: 1.55 }}>{s.desc}</p>
              </div>
            </div>
          ))}
        </div>
      </div>
    </section>
  );
}

function StepGlyph({ kind }) {
  // Simple geometric glyph per step
  if (kind === "audit") {
    return (
      <svg viewBox="0 0 200 80" width="100%" height="80">
        <line x1="0" y1="40" x2="200" y2="40" stroke="var(--line)" strokeWidth="1" strokeDasharray="2 4" />
        {[20, 60, 100, 140, 180].map((x, i) => (
          <g key={i}>
            <line x1={x} y1="40" x2={x} y2={[20, 55, 30, 60, 45][i]} stroke="var(--ink-3)" strokeWidth="1.5" />
            <circle cx={x} cy={[20, 55, 30, 60, 45][i]} r="4" fill="var(--accent)" />
          </g>
        ))}
      </svg>
    );
  }
  if (kind === "build") {
    return (
      <svg viewBox="0 0 200 80" width="100%" height="80">
        {[0, 1, 2, 3].map(i => (
          <g key={i} transform={`translate(${10 + i * 50}, 20)`}>
            <rect width="40" height="40" rx="6" fill={i < 3 ? "var(--accent)" : "var(--bg)"} stroke="var(--ink)" strokeWidth="1" />
            {i < 3 && <line x1="40" y1="20" x2="50" y2="20" stroke="var(--ink-3)" strokeWidth="1.5" />}
          </g>
        ))}
      </svg>
    );
  }
  // calm
  return (
    <svg viewBox="0 0 200 80" width="100%" height="80">
      <path d="M0 40 Q 50 38 100 40 T 200 40" fill="none" stroke="var(--ink-3)" strokeWidth="1.5" />
      <circle cx="100" cy="40" r="22" fill="none" stroke="var(--accent)" strokeWidth="1.5">
        <animate attributeName="r" values="18;28;18" dur="3s" repeatCount="indefinite" />
        <animate attributeName="opacity" values="1;0;1" dur="3s" repeatCount="indefinite" />
      </circle>
      <circle cx="100" cy="40" r="6" fill="var(--accent)" />
    </svg>
  );
}

// ─── pull quote ───
function PullQuote() {
  return (
    <section className="section-tight" style={{ borderTop: "1px solid var(--line)" }}>
      <div className="wrap">
        <div className="reveal" style={{
          textAlign: "center", maxWidth: 980, margin: "0 auto", padding: "40px 0"
        }}>
          <div className="kicker" style={{ marginBottom: 28 }}>What clients tell their peers</div>
          <p className="serif" style={{
            fontSize: "clamp(32px, 4vw, 56px)", lineHeight: 1.1,
            margin: 0, letterSpacing: "-0.02em", fontWeight: 400
          }}>
            "They didn't sell us AI. <span className="serif-i">They sold us back our Fridays</span> — and quietly handed our ops director a tool she now refuses to give up."
          </p>
          <div style={{
            marginTop: 36, display: "flex", justifyContent: "center", alignItems: "center", gap: 16
          }}>
            <div style={{
              width: 44, height: 44, borderRadius: "50%",
              background: "linear-gradient(135deg, #C8B8A6, #6B5E50)"
            }}></div>
            <div style={{ textAlign: "left" }}>
              <div style={{ fontSize: 14, fontWeight: 500 }}>Eira Halberd</div>
              <div style={{ fontSize: 12.5, color: "var(--ink-3)" }}>COO, Halberd Capital · 80 staff</div>
            </div>
          </div>
        </div>
      </div>
    </section>
  );
}

Object.assign(window, {
  MarqueeStrip, SectionHeader, ServicesPreview, ServiceCard,
  WorkflowsPreview, WorkflowRow, CasesPreview, CaseCard,
  HowItWorksPreview, PullQuote
});
