/* global React, PROGRAMS, PrimaryButton, GhostButton, Eyebrow, Card */
/* Home-page sections: Hero, StatsStrip, ProductGrid, Pillars, Process, Charity, Testimonials, FinalCTA */

const { useState: useStateHome, useEffect: useEffectHome, useRef: useRefHome } = React;

// ---------- JOURNEY HERO (scroll-cross-fade through 4 photos) ----------
const JOURNEY_FRAMES = [
  {
    src: "assets/journey-1-forsale.png",
    eyebrow: "Step one",
    headline: "This is the part we work for.",
    sub: "Long before the application, long before the rate lock — there's a sidewalk, a sign, and a feeling. We're here to get you from this moment to the front door.",
    objectPosition: "25% center", // pan left so the pregnant wife is visible alongside the husband
  },
  {
    src: "assets/journey-2-docs.png",
    eyebrow: "Step two",
    headline: "From the kitchen table.",
    sub: "Snap a photo of your W-2. Upload your bank statements. The whole application — from your couch, in minutes. No printer, no fax, no driving anywhere.",
  },
  {
    src: "assets/journey-3-closing.png",
    eyebrow: "Step three",
    headline: "The last signature.",
    sub: "Months of work come down to a single afternoon at a wooden table. We're with you for every page, every question, every signature.",
  },
  {
    src: "assets/journey-4-nursery.png",
    eyebrow: "Step four",
    headline: "This is what a mortgage is for.",
    sub: "Not the rate. Not the paperwork. The room at the end of the hall, ready and waiting. We helped you get here.",
  },
];

function JourneyHero() {
  const sectionRef = useRefHome(null);
  const [progress, setProgress] = useStateHome(0); // 0..1 across the entire scrollytelling section

  useEffectHome(() => {
    let raf = 0;
    const tick = () => {
      const el = sectionRef.current;
      if (!el) { raf = requestAnimationFrame(tick); return; }
      const rect = el.getBoundingClientRect();
      const total = el.offsetHeight - window.innerHeight;
      // distance scrolled past top of section, clamped 0..1
      const scrolled = Math.max(0, Math.min(total, -rect.top));
      const p = total > 0 ? scrolled / total : 0;
      setProgress(p);
      raf = requestAnimationFrame(tick);
    };
    raf = requestAnimationFrame(tick);
    return () => cancelAnimationFrame(raf);
  }, []);

  // Preload images so cross-fades don't pop
  useEffectHome(() => {
    JOURNEY_FRAMES.forEach(f => { const img = new Image(); img.src = f.src; });
  }, []);

  // For 4 frames, each owns 1/(N) of the timeline.
  const N = JOURNEY_FRAMES.length;
  // continuous scroll position across frames: 0..N-1
  const t = progress * (N - 1);

  // opacity of frame i, with cross-fade overlap
  const frameOpacity = (i) => {
    const d = Math.abs(t - i);
    // hard 1 within ±0.18; cross-fade between
    if (d >= 0.85) return 0;
    if (d <= 0.15) return 1;
    // linear cross-fade
    return 1 - (d - 0.15) / (0.85 - 0.15);
  };

  // text opacity: peak in the middle of each frame's window
  const textOpacity = (i) => {
    const d = Math.abs(t - i);
    if (d >= 0.55) return 0;
    if (d <= 0.10) return 1;
    return 1 - (d - 0.10) / (0.55 - 0.10);
  };

  // text y-translate (subtle parallax)
  const textTranslate = (i) => {
    const d = t - i; // signed
    return d * 40; // -40..+40 range as it slides past
  };

  // Image scale — slow ken-burns within each frame's active window
  const frameScale = (i) => {
    const d = t - i;
    return 1 + Math.max(-0.05, Math.min(0.05, d * 0.08));
  };

  return (
    <section ref={sectionRef} aria-label="The Haven journey" style={{
      position: "relative",
      height: `${100 * N}vh`, // one viewport per frame
      marginTop: 0,
      background: "#0c1722",
    }}>
      <div style={{
        position: "sticky", top: 0,
        width: "100%", height: "100vh",
        overflow: "hidden",
        background: "#0c1722",
      }}>
        {/* Stacked images */}
        {JOURNEY_FRAMES.map((f, i) => (
          <div key={f.src} aria-hidden style={{
            position: "absolute", inset: 0,
            opacity: frameOpacity(i),
            transition: "opacity 60ms linear",
            willChange: "opacity, transform",
          }}>
            <img src={f.src} alt=""
              style={{
                position: "absolute", inset: 0,
                width: "100%", height: "100%",
                objectFit: "cover", objectPosition: f.objectPosition || "center",
                transform: `scale(${frameScale(i)})`,
                transition: "transform 120ms linear",
              }}/>
            {/* Gradient scrim for text legibility */}
            <div style={{
              position: "absolute", inset: 0,
              background: "linear-gradient(180deg, rgba(12,23,34,0.55) 0%, rgba(12,23,34,0.15) 30%, rgba(12,23,34,0.20) 55%, rgba(12,23,34,0.78) 100%)",
            }}/>
          </div>
        ))}

        {/* Top bar overlay — thin progress + step counter */}
        <div style={{
          position: "absolute", top: 0, left: 0, right: 0,
          padding: "calc(72px + 12px) 32px 0",
          display: "flex", justifyContent: "space-between", alignItems: "center",
          gap: 20, pointerEvents: "none",
        }}>
          <div style={{
            fontFamily: "'Source Code Pro',monospace", fontSize: 11, fontWeight: 500,
            color: "rgba(255,255,255,0.7)", textTransform: "uppercase", letterSpacing: "0.14em",
          }}>
            {String(Math.min(N, Math.round(t) + 1)).padStart(2,"0")} / {String(N).padStart(2,"0")}
          </div>
          <div style={{ flex: 1, height: 2, background: "rgba(255,255,255,0.16)", borderRadius: 1, maxWidth: 480 }}>
            <div style={{
              height: "100%", background: "#FF7A59", borderRadius: 1,
              width: `${Math.max(2, Math.min(100, progress * 100))}%`,
              transition: "width 60ms linear",
            }}/>
          </div>
          <div style={{
            fontFamily: "Montserrat,'Inter Tight',sans-serif", fontSize: 11, fontWeight: 700,
            color: "rgba(255,255,255,0.85)", textTransform: "uppercase", letterSpacing: "0.18em",
          }}>
            The Haven journey
          </div>
        </div>

        {/* Stacked text — one per frame, fading in/out */}
        <div style={{
          position: "absolute", inset: 0,
          display: "flex", alignItems: "flex-end",
          padding: "0 32px 96px",
        }}>
          <div style={{ position: "relative", maxWidth: 1080, margin: "0 auto", width: "100%" }}>
            {JOURNEY_FRAMES.map((f, i) => (
              <div key={f.src} style={{
                position: i === 0 ? "relative" : "absolute",
                top: 0, left: 0, right: 0,
                opacity: textOpacity(i),
                transform: `translateY(${textTranslate(i)}px)`,
                transition: "opacity 80ms linear, transform 80ms linear",
                pointerEvents: textOpacity(i) > 0.5 ? "auto" : "none",
                maxWidth: 720,
              }}>
                <div style={{
                  fontFamily: "'Source Code Pro',monospace", fontSize: 11, fontWeight: 500,
                  color: "#FF7A59", textTransform: "uppercase", letterSpacing: "0.14em",
                  marginBottom: 14,
                }}>{f.eyebrow}</div>
                <h1 style={{
                  fontFamily: "Montserrat,'Inter Tight',sans-serif",
                  fontSize: 72, fontWeight: 700, lineHeight: 1.02, letterSpacing: "-1.6px",
                  color: "#fff", margin: 0, textShadow: "0 2px 24px rgba(0,0,0,0.35)",
                }}>{f.headline}</h1>
                <p style={{
                  marginTop: 22, fontSize: 19, lineHeight: 1.55,
                  color: "rgba(255,255,255,0.86)", fontWeight: 400,
                  maxWidth: 560, textShadow: "0 1px 12px rgba(0,0,0,0.4)",
                }}>{f.sub}</p>
              </div>
            ))}
          </div>
        </div>

        {/* Final-frame CTA — appears with the last frame */}
        <div style={{
          position: "absolute", right: 32, bottom: 96,
          opacity: textOpacity(N - 1),
          transition: "opacity 80ms linear",
          pointerEvents: textOpacity(N - 1) > 0.5 ? "auto" : "none",
          display: "flex", gap: 12,
        }}>
          <PrimaryButton as="a" href="contact.html">Start a 15-minute call</PrimaryButton>
          <a href="products.html" style={{
            padding: "9px 16px", borderRadius: 4, border: "1px solid rgba(255,255,255,0.32)",
            color: "#fff", fontFamily: "'Inter Tight',sans-serif", fontSize: 16, fontWeight: 500,
            textDecoration: "none", background: "rgba(255,255,255,0.04)",
          }}>See programs</a>
        </div>

        {/* Scroll affordance — first frame only */}
        <div style={{
          position: "absolute", left: "50%", bottom: 32, transform: "translateX(-50%)",
          opacity: progress < 0.04 ? 1 : 0,
          transition: "opacity 200ms linear",
          pointerEvents: "none",
          display: "flex", flexDirection: "column", alignItems: "center", gap: 8,
        }}>
          <div style={{
            fontFamily: "'Source Code Pro',monospace", fontSize: 10, fontWeight: 500,
            color: "rgba(255,255,255,0.7)", textTransform: "uppercase", letterSpacing: "0.18em",
          }}>Scroll</div>
          <svg width="14" height="22" viewBox="0 0 14 22" fill="none" stroke="#fff" strokeWidth="1.4" strokeLinecap="round">
            <rect x="1" y="1" width="12" height="20" rx="6"/>
            <line x1="7" y1="6" x2="7" y2="10">
              <animate attributeName="y1" values="6;10;6" dur="1.6s" repeatCount="indefinite"/>
              <animate attributeName="y2" values="10;14;10" dur="1.6s" repeatCount="indefinite"/>
            </line>
          </svg>
        </div>
      </div>
    </section>
  );
}

// ---------- HERO ----------
function LiveQuoteCard({ coralIntensity = "balanced" }) {
  const coralBg = coralIntensity === "loud" ? "rgba(255,122,89,0.18)"
    : coralIntensity === "muted" ? "rgba(255,122,89,0.06)"
    : "rgba(255,122,89,0.10)";
  return (
    <div style={{
      background: "#fff", border: "1px solid #e1e7ef", borderRadius: 8, padding: 22,
      boxShadow: "rgba(31,45,61,0.22) 0 30px 45px -30px, rgba(0,0,0,0.08) 0 18px 36px -18px",
      width: "100%", maxWidth: 440,
    }}>
      <div style={{ display: "flex", alignItems: "center", justifyContent: "space-between", marginBottom: 10 }}>
        <div style={{ fontFamily: "'Source Code Pro',monospace", fontSize: 11, fontWeight: 500, color: "#6b7c92", textTransform: "uppercase", letterSpacing: "0.08em" }}>Pre-qualification preview</div>
        <div style={{
          display: "inline-flex", alignItems: "center", gap: 6,
          padding: "2px 8px", borderRadius: 3,
          background: coralBg, color: "#c64a2b",
          fontFamily: "'Source Code Pro',monospace", fontSize: 10, fontWeight: 500, textTransform: "uppercase", letterSpacing: "0.08em",
        }}>
          <span style={{ width: 5, height: 5, borderRadius: "50%", background: "#ff7a59" }}></span>
          Live shop
        </div>
      </div>
      <div style={{ fontSize: 22, fontWeight: 300, letterSpacing: "-0.22px", lineHeight: 1.15, color: "#1f2d3d", fontFamily: "'Inter Tight',sans-serif", fontFeatureSettings: "'ss01'" }}>
        Self-employed · 740 FICO · $875,000 purchase
      </div>
      <div style={{ height: 1, background: "#e1e7ef", margin: "18px 0" }} />
      <div style={{ display: "flex", flexDirection: "column", gap: 10, fontSize: 14 }}>
        {[
          ["Matched program", "Bank Statement"],
          ["Docs required", "24 mo deposits"],
          ["Est. LTV", "85%"],
          ["Seasoning", "None"],
          ["Lenders shopped", "40+"],
        ].map(([k, v]) => (
          <div key={k} style={{ display: "flex", justifyContent: "space-between", fontFamily: "'Inter Tight',sans-serif", fontFeatureSettings: "'ss01'" }}>
            <span style={{ color: "#6b7c92", fontWeight: 300 }}>{k}</span>
            <span style={{ color: "#1f2d3d", fontWeight: 400, fontFeatureSettings: /\d/.test(v) ? "'tnum'" : "'ss01'" }}>{v}</span>
          </div>
        ))}
      </div>
      <div style={{ marginTop: 18, display: "flex", alignItems: "center", gap: 10, paddingTop: 14, borderTop: "1px solid #e1e7ef" }}>
        <span style={{
          background: "rgba(21,190,83,0.14)", color: "#0f7a3a",
          border: "1px solid rgba(21,190,83,0.36)",
          borderRadius: 4, padding: "2px 8px", fontSize: 10, fontWeight: 400,
          fontFamily: "'Inter Tight',sans-serif", fontFeatureSettings: "'ss01'",
        }}>Likely eligible</span>
        <span style={{ fontSize: 12, color: "#6b7c92", fontFamily: "'Inter Tight',sans-serif", fontFeatureSettings: "'ss01'" }}>No soft pull at this stage</span>
      </div>
    </div>
  );
}

function PhoneMock({ title, lines, tilt = -6, accent = "#ff7a59" }) {
  return (
    <div style={{
      width: 180, height: 360, borderRadius: 28, background: "#0f1a26",
      border: "6px solid #14212e", padding: 10, transform: `rotate(${tilt}deg)`,
      boxShadow: "rgba(0,0,0,0.35) 0 30px 60px -20px, rgba(255,122,89,0.18) 0 10px 40px -10px",
      fontFamily: "'Inter Tight',sans-serif", fontFeatureSettings: "'ss01'",
    }}>
      <div style={{
        width: "100%", height: "100%", borderRadius: 20, background: "#fff",
        padding: 16, display: "flex", flexDirection: "column", gap: 10,
      }}>
        <div style={{ fontFamily: "'Source Code Pro',monospace", fontSize: 9, color: "#6b7c92", textTransform: "uppercase", letterSpacing: "0.08em" }}>Haven · match</div>
        <div style={{ fontSize: 15, fontWeight: 400, color: "#1f2d3d", letterSpacing: "-0.15px", lineHeight: 1.2 }}>{title}</div>
        <div style={{ height: 1, background: "#e1e7ef" }}/>
        {lines.map(([k,v],i) => (
          <div key={i} style={{ display: "flex", justifyContent: "space-between", fontSize: 11 }}>
            <span style={{ color: "#6b7c92" }}>{k}</span>
            <span style={{ color: "#1f2d3d", fontWeight: 400, fontFeatureSettings: "'tnum'" }}>{v}</span>
          </div>
        ))}
        <div style={{ marginTop: "auto", padding: "8px 10px", background: accent, color: "#fff", borderRadius: 6, fontSize: 12, fontWeight: 400, textAlign: "center" }}>View program</div>
      </div>
    </div>
  );
}

function Hero({ variant = "split", coralIntensity = "balanced", typeTone = "light" }) {
  if (variant === "dark-phones") {
    return (
      <section style={{ position: "relative", marginTop: 16, padding: "72px 16px 120px",
        background: "linear-gradient(180deg, #14212e 0%, #1f2d3d 100%)",
        overflow: "hidden",
      }}>
        <div aria-hidden style={{
          position: "absolute", inset: 0, opacity: 0.14,
          background: "radial-gradient(900px 400px at 50% -20%, #ff7a59 0%, transparent 60%)",
        }}/>
        <div style={{ position: "relative", maxWidth: 1080, margin: "0 auto", textAlign: "center" }}>
          <h1 style={{
            fontFamily: "'Inter Tight',sans-serif", fontFeatureSettings: "'ss01'",
            fontSize: 68, fontWeight: 300, letterSpacing: "-1.8px", lineHeight: 1.02,
            color: "#fff", margin: 0, maxWidth: 820, marginLeft: "auto", marginRight: "auto",
          }}>
            Borrow Smarter, Not Harder.
          </h1>
          <p style={{
            fontSize: 18, fontWeight: 300, lineHeight: 1.5, color: "rgba(255,255,255,0.72)",
            fontFamily: "'Inter Tight',sans-serif", fontFeatureSettings: "'ss01'",
            margin: "22px auto 32px", maxWidth: 560,
          }}>
            Experience the future of non-QM lending. Manage payments, match programs, and track approvals — all from one broker.
          </p>
          <div style={{ display: "flex", gap: 12, justifyContent: "center" }}>
            <PrimaryButton as="a" href="contact.html">Start a 15-minute call</PrimaryButton>
            <a href="products.html" style={{
              padding: "9px 16px", borderRadius: 4, border: "1px solid rgba(255,255,255,0.28)",
              color: "#fff", fontFamily: "'Inter Tight',sans-serif", fontSize: 16, fontWeight: 400,
              textDecoration: "none",
            }}>See the nine programs</a>
          </div>

          <div style={{ marginTop: 72, display: "flex", justifyContent: "center", gap: 32, alignItems: "flex-end" }}>
            <PhoneMock tilt={-8} title="Bank Statement · approved"
              lines={[["Program","Bank Stmt"],["LTV","85%"],["Term","30 yr"],["Status","CTC"]]}/>
            <PhoneMock tilt={6} title="Asset Qualifier · preview"
              lines={[["Program","Asset Q."],["Loan","$1,250,000"],["Reserves","24 mo"],["Status","Review"]]}
              accent="#33475b"/>
          </div>
        </div>
      </section>
    );
  }


  const heavy = typeTone === "heavy";
  const heroWeight = heavy ? 400 : 300;
  const pillFill = coralIntensity === "loud" ? "rgba(255,122,89,0.14)"
    : coralIntensity === "muted" ? "rgba(255,122,89,0.04)"
    : "rgba(255,122,89,0.08)";

  if (variant === "light") {
    return (
      <section style={{ maxWidth: 900, margin: "80px auto 0", padding: "0 16px", textAlign: "center" }}>
        <div style={{
          display: "inline-flex", alignItems: "center", gap: 8, margin: "0 auto 24px",
          padding: "3px 12px", borderRadius: 4,
          background: pillFill, border: "1px solid rgba(255,122,89,0.24)",
          fontFamily: "'Inter Tight',sans-serif", fontFeatureSettings: "'ss01'",
          fontSize: 12, fontWeight: 400, color: "#c64a2b",
        }}>
          <span style={{ width: 6, height: 6, borderRadius: "50%", background: "#ff7a59" }}></span>
          Atlanta · Licensed in GA · FL · TX
        </div>
        <h1 style={{
          fontFamily: "'Inter Tight',sans-serif", fontFeatureSettings: "'ss01'",
          fontSize: 72, fontWeight: heroWeight, letterSpacing: "-2px", lineHeight: 1.0,
          color: "#1f2d3d", margin: 0, maxWidth: 820, marginLeft: "auto", marginRight: "auto",
        }}>
          Better options, better service, better experience.
        </h1>
        <p style={{
          fontSize: 20, fontWeight: 300, lineHeight: 1.45, color: "#6b7c92",
          fontFamily: "'Inter Tight',sans-serif", fontFeatureSettings: "'ss01'",
          margin: "24px auto 32px", maxWidth: 620,
        }}>
          A boutique Atlanta brokerage. We shop 40+ wholesale lenders in real time and recommend the one that actually fits your file.
        </p>
        <div style={{ display: "flex", gap: 12, justifyContent: "center" }}>
          <PrimaryButton as="a" href="contact.html">Start a 15-minute call</PrimaryButton>
          <GhostButton as="a" href="products.html">See the nine programs</GhostButton>
        </div>
      </section>
    );
  }

  if (variant === "photo") {
    return (
      <section style={{ maxWidth: 1080, margin: "56px auto 0", padding: "0 16px" }}>
        <div style={{
          position: "relative", borderRadius: 8, overflow: "hidden",
          border: "1px solid #e1e7ef",
          background: "linear-gradient(135deg, #33475b 0%, #1f2d3d 100%)",
          minHeight: 520, padding: "64px 56px",
          color: "#fff", display: "flex", flexDirection: "column", justifyContent: "flex-end",
          boxShadow: "rgba(31,45,61,0.22) 0 30px 45px -30px, rgba(0,0,0,0.12) 0 18px 36px -18px",
        }}>
          {/* placeholder for photography */}
          <div aria-hidden style={{
            position: "absolute", inset: 0, opacity: 0.18,
            background: "radial-gradient(1200px 400px at 80% -10%, #ff7a59 0%, transparent 55%)",
          }} />
          <div style={{ position: "relative", maxWidth: 640 }}>
            <div style={{
              display: "inline-flex", alignItems: "center", gap: 8, marginBottom: 24,
              padding: "3px 12px", borderRadius: 4,
              background: "rgba(255,255,255,0.08)", border: "1px solid rgba(255,255,255,0.18)",
              fontFamily: "'Inter Tight',sans-serif", fontFeatureSettings: "'ss01'",
              fontSize: 12, fontWeight: 400, color: "rgba(255,255,255,0.8)",
            }}>
              <span style={{ width: 6, height: 6, borderRadius: "50%", background: "#ff7a59" }}></span>
              Atlanta · NMLS #2431561
            </div>
            <h1 style={{
              fontFamily: "'Inter Tight',sans-serif", fontFeatureSettings: "'ss01'",
              fontSize: 62, fontWeight: heroWeight, letterSpacing: "-1.6px", lineHeight: 1.02,
              color: "#fff", margin: 0,
            }}>
              Loans for borrowers who don't fit a box.
            </h1>
            <p style={{
              fontSize: 18, fontWeight: 300, lineHeight: 1.5, color: "rgba(255,255,255,0.78)",
              fontFamily: "'Inter Tight',sans-serif", fontFeatureSettings: "'ss01'",
              margin: "22px 0 30px", maxWidth: 520,
            }}>
              Self-employed. Asset-rich. Rebuilding. Over the conforming limit. We shop 40+ wholesale lenders and recommend the one that actually fits.
            </p>
            <div style={{ display: "flex", gap: 12 }}>
              <PrimaryButton as="a" href="contact.html">Start a 15-minute call</PrimaryButton>
              <a href="products.html" style={{
                padding: "9px 16px", borderRadius: 4, border: "1px solid rgba(255,255,255,0.28)",
                color: "#fff", fontFamily: "'Inter Tight',sans-serif", fontSize: 16, fontWeight: 400,
                textDecoration: "none",
              }}>See the nine programs</a>
            </div>
          </div>
        </div>
      </section>
    );
  }

  // default: "split"
  return (
    <section style={{ maxWidth: 1080, margin: "56px auto 0", padding: "0 16px",
      display: "grid", gridTemplateColumns: "1.1fr 1fr", gap: 56, alignItems: "center" }}>
      <div>
        <div style={{
          display: "inline-flex", alignItems: "center", gap: 8,
          padding: "3px 12px", borderRadius: 4,
          background: "#fff", border: "1px solid #e1e7ef",
          fontFamily: "'Inter Tight',sans-serif", fontFeatureSettings: "'ss01'",
          fontSize: 12, fontWeight: 400, color: "#425a73", marginBottom: 22,
        }}>
          <span style={{ width: 6, height: 6, borderRadius: "50%", background: "#15be53" }}></span>
          Atlanta · Licensed in GA · FL · TX
        </div>
        <h1 style={{
          fontFamily: "'Inter Tight',sans-serif", fontFeatureSettings: "'ss01'",
          fontSize: 62, fontWeight: heroWeight, letterSpacing: "-1.6px", lineHeight: 1.02,
          color: "#1f2d3d", margin: 0,
        }}>
          Loans for borrowers who don't fit a box.
        </h1>
        <p style={{
          fontSize: 18, fontWeight: 300, lineHeight: 1.45, color: "#6b7c92",
          fontFamily: "'Inter Tight',sans-serif", fontFeatureSettings: "'ss01'",
          margin: "22px 0 30px", maxWidth: 520,
        }}>
          Self-employed. Asset-rich. Rebuilding. Over the conforming limit. We shop 40+ wholesale lenders in real time and recommend the one that actually fits.
        </p>
        <div style={{ display: "flex", gap: 12 }}>
          <PrimaryButton as="a" href="contact.html">Start a 15-minute call</PrimaryButton>
          <GhostButton as="a" href="products.html">See the nine programs</GhostButton>
        </div>
        <div style={{
          marginTop: 28, fontSize: 13, color: "#6b7c92",
          fontFamily: "'Inter Tight',sans-serif", fontFeatureSettings: "'ss01'",
        }}>Veteran loan officers. No learners on your file.</div>
      </div>
      <LiveQuoteCard coralIntensity={coralIntensity} />
    </section>
  );
}

// ---------- STATS ----------
function StatsStrip() {
  const stats = [
    ["40+", "Wholesale lenders shopped"],
    ["9", "Loan programs"],
    ["15", "Minutes to first call"],
    ["100%", "Closings include a charity donation"],
  ];
  return (
    <section style={{ maxWidth: 1080, margin: "72px auto 0", padding: "0 16px" }}>
      <div style={{ display: "grid", gridTemplateColumns: "repeat(4, 1fr)", border: "1px solid #e1e7ef", borderRadius: 6, background: "#fff", overflow: "hidden" }}>
        {stats.map(([n, label], i) => (
          <div key={i} style={{
            padding: "26px 22px",
            borderLeft: i === 0 ? "none" : "1px solid #e1e7ef",
            fontFamily: "'Inter Tight',sans-serif", fontFeatureSettings: "'ss01'",
          }}>
            <div style={{
              fontSize: 44, fontWeight: 300, letterSpacing: "-0.9px",
              lineHeight: 1, color: "#1f2d3d", fontFeatureSettings: "'tnum'",
            }}>{n}</div>
            <div style={{ marginTop: 10, fontSize: 13, fontWeight: 400, color: "#6b7c92" }}>{label}</div>
          </div>
        ))}
      </div>
    </section>
  );
}

// ---------- PRODUCT GRID ----------
function ProductCard({ program }) {
  return (
    <a href={`product.html?id=${program.id}`} style={{
      display: "block", textAlign: "left", textDecoration: "none",
      background: "#fff", border: "1px solid #e1e7ef", borderRadius: 6,
      padding: 22, boxShadow: "rgba(31,45,61,0.06) 0 2px 4px",
      transition: "box-shadow 180ms cubic-bezier(0.2,0.8,0.2,1)",
      fontFamily: "'Inter Tight',sans-serif", fontFeatureSettings: "'ss01'", height: "100%",
    }}
      onMouseEnter={(e) => e.currentTarget.style.boxShadow = "rgba(31,45,61,0.22) 0 30px 45px -30px, rgba(0,0,0,0.08) 0 18px 36px -18px"}
      onMouseLeave={(e) => e.currentTarget.style.boxShadow = "rgba(31,45,61,0.06) 0 2px 4px"}>
      <div style={{
        fontFamily: "'Source Code Pro',monospace", fontSize: 11, fontWeight: 500,
        color: "#c64a2b", textTransform: "uppercase", letterSpacing: "0.08em",
      }}>{program.tag}</div>
      <div style={{ marginTop: 14, fontSize: 22, fontWeight: 300, letterSpacing: "-0.22px", lineHeight: 1.15, color: "#1f2d3d" }}>{program.headline}</div>
      <div style={{ height: 1, background: "#e1e7ef", margin: "18px 0" }} />
      <div style={{ display: "flex", gap: 20, fontSize: 13 }}>
        <div>
          <div style={{ color: "#6b7c92", fontWeight: 300 }}>Docs</div>
          <div style={{ color: "#1f2d3d", fontWeight: 400, marginTop: 2 }}>{program.docs}</div>
        </div>
        <div>
          <div style={{ color: "#6b7c92", fontWeight: 300 }}>Max LTV</div>
          <div style={{ color: "#1f2d3d", fontWeight: 400, fontFeatureSettings: "'tnum'", marginTop: 2 }}>{program.ltv}</div>
        </div>
      </div>
    </a>
  );
}

function ProductGrid() {
  return (
    <section style={{ maxWidth: 1080, margin: "96px auto 0", padding: "0 16px" }}>
      <div style={{ display: "flex", alignItems: "end", justifyContent: "space-between", marginBottom: 28 }}>
        <div>
          <Eyebrow>Nine programs</Eyebrow>
          <h3 style={{ fontFamily: "'Inter Tight',sans-serif", fontFeatureSettings: "'ss01'", fontSize: 36, fontWeight: 300, letterSpacing: "-0.72px", lineHeight: 1.1, color: "#1f2d3d", margin: "8px 0 0", maxWidth: 560 }}>
            A program for most borrowers banks skip.
          </h3>
        </div>
        <GhostButton as="a" href="products.html">Compare all nine</GhostButton>
      </div>
      <div style={{ display: "grid", gridTemplateColumns: "repeat(3, 1fr)", gap: 16 }}>
        {PROGRAMS.map(p => <ProductCard key={p.id} program={p} />)}
      </div>
    </section>
  );
}

// ---------- PROCESS ----------
function ProcessSteps() {
  const steps = [
    ["01", "15-min call", "Talk to a veteran broker. No soft pull at this stage."],
    ["02", "Program match", "We shop 40+ lenders and bring back one honest recommendation."],
    ["03", "Docs + underwriting", "Transparent cost sheet up front. No surprises at closing."],
    ["04", "Close + charity", "Pick a nonprofit. We donate in your name at the table."],
  ];
  return (
    <section style={{ maxWidth: 1080, margin: "96px auto 0", padding: "0 16px" }}>
      <div style={{ display: "flex", alignItems: "end", justifyContent: "space-between", marginBottom: 28 }}>
        <div>
          <Eyebrow>How it works</Eyebrow>
          <h3 style={{ fontFamily: "'Inter Tight',sans-serif", fontFeatureSettings: "'ss01'", fontSize: 36, fontWeight: 300, letterSpacing: "-0.72px", lineHeight: 1.1, color: "#1f2d3d", margin: "8px 0 0", maxWidth: 560 }}>
            Four steps. No learners on your file.
          </h3>
        </div>
        <GhostButton as="a" href="process.html">Full process</GhostButton>
      </div>
      <div style={{ display: "grid", gridTemplateColumns: "repeat(4,1fr)", gap: 16 }}>
        {steps.map(([num, title, body]) => (
          <div key={num} style={{
            background: "#fff", border: "1px solid #e1e7ef", borderRadius: 6,
            padding: 22, fontFamily: "'Inter Tight',sans-serif", fontFeatureSettings: "'ss01'",
          }}>
            <div style={{
              fontFamily: "'Source Code Pro',monospace", fontSize: 11, fontWeight: 700,
              color: "#ff7a59", letterSpacing: "0.08em",
            }}>{num}</div>
            <div style={{ marginTop: 12, fontSize: 18, fontWeight: 400, color: "#1f2d3d", letterSpacing: "-0.18px" }}>{title}</div>
            <div style={{ marginTop: 8, fontSize: 14, fontWeight: 300, color: "#6b7c92", lineHeight: 1.5 }}>{body}</div>
          </div>
        ))}
      </div>
    </section>
  );
}

// ---------- PILLARS ----------
function Pillars() {
  const pillars = [
    ["Broker, not bank", "We shop 40+ wholesale lenders. A retail bank sells one product — we pick the best one for you."],
    ["Non-QM specialists", "Most of our book is borrowers banks skipped. Bank Statement, Asset, DSCR, Foreign National — we do these every week."],
    ["Transparent pricing", "You see a cost sheet on day one. No last-minute fee creep, no rate-lock games."],
    ["Veterans on your file", "Loan officers with 10+ years of book. No trainees, no call-center queue."],
  ];
  return (
    <section style={{ maxWidth: 1080, margin: "96px auto 0", padding: "0 16px" }}>
      <div style={{ textAlign: "left", marginBottom: 36 }}>
        <Eyebrow>Why Haven</Eyebrow>
        <h3 style={{ fontFamily: "'Inter Tight',sans-serif", fontFeatureSettings: "'ss01'", fontSize: 36, fontWeight: 300, letterSpacing: "-0.72px", lineHeight: 1.1, color: "#1f2d3d", margin: "8px 0 0", maxWidth: 640 }}>
          A boutique shop, built the way you wish banks were.
        </h3>
      </div>
      <div style={{ display: "grid", gridTemplateColumns: "repeat(2, 1fr)", gap: 24 }}>
        {pillars.map(([title, body]) => (
          <div key={title} style={{
            padding: "24px 0", borderTop: "1px solid #e1e7ef",
            fontFamily: "'Inter Tight',sans-serif", fontFeatureSettings: "'ss01'",
          }}>
            <div style={{ fontSize: 22, fontWeight: 400, color: "#1f2d3d", letterSpacing: "-0.22px" }}>{title}</div>
            <div style={{ marginTop: 10, fontSize: 15, fontWeight: 300, color: "#6b7c92", lineHeight: 1.55, maxWidth: 480 }}>{body}</div>
          </div>
        ))}
      </div>
    </section>
  );
}

// ---------- CHARITY BAND ----------
function CharityBand() {
  const [picked, setPicked] = useStateHome(null);
  const charities = [
    { id: "rally", name: "Rally Foundation", sub: "Pediatric cancer research · Atlanta" },
    { id: "buddies", name: "Best Buddies", sub: "Friendships, jobs, leadership for IDD" },
    { id: "wounded", name: "Wounded Warrior Project", sub: "Post-9/11 injured service members" },
    { id: "ozzie", name: "Ozzie Albies Foundation", sub: "Atlanta youth & education" },
  ];

  return (
    <section style={{ background: "#1f2d3d", marginTop: 96, padding: "80px 0",
      fontFamily: "'Inter Tight',sans-serif", fontFeatureSettings: "'ss01'" }}>
      <div style={{ maxWidth: 1080, margin: "0 auto", padding: "0 16px" }}>
        <div style={{ display: "grid", gridTemplateColumns: "1fr 1.2fr", gap: 56 }}>
          <div>
            <div style={{ fontFamily: "'Source Code Pro',monospace", fontSize: 11, fontWeight: 500, color: "#ff7a59", textTransform: "uppercase", letterSpacing: "0.08em" }}>Charity at closing</div>
            <h3 style={{ fontSize: 40, fontWeight: 300, letterSpacing: "-0.9px", lineHeight: 1.05, color: "#fff", margin: "12px 0 18px" }}>
              Small ritual, real money, Atlanta-rooted.
            </h3>
            <p style={{ fontSize: 16, fontWeight: 300, lineHeight: 1.55, color: "rgba(255,255,255,0.72)", maxWidth: 420, margin: 0 }}>
              At every closing, the borrower picks one of four nonprofits. Haven donates in their name, at no cost to you.
            </p>
            <div style={{ marginTop: 24 }}>
              <GhostButton as="a" href="community.html" style={{
                color: "#fff", borderColor: "rgba(255,255,255,0.28)", background: "transparent",
              }}>More about the program</GhostButton>
            </div>
          </div>
          <div style={{ display: "grid", gridTemplateColumns: "1fr 1fr", gap: 12 }}>
            {charities.map(c => (
              <button key={c.id} onClick={() => setPicked(c.id)}
                style={{
                  all: "unset", cursor: "pointer", padding: 18, borderRadius: 6,
                  background: picked === c.id ? "rgba(255,122,89,0.15)" : "rgba(255,255,255,0.04)",
                  border: `1px solid ${picked === c.id ? "#ff7a59" : "rgba(255,255,255,0.14)"}`,
                  fontFamily: "'Inter Tight',sans-serif", fontFeatureSettings: "'ss01'",
                  transition: "all 180ms cubic-bezier(0.2,0.8,0.2,1)",
                }}>
                <div style={{ fontSize: 16, fontWeight: 400, color: "#fff", letterSpacing: "-0.12px" }}>{c.name}</div>
                <div style={{ marginTop: 6, fontSize: 13, fontWeight: 300, color: "rgba(255,255,255,0.62)", lineHeight: 1.45 }}>{c.sub}</div>
              </button>
            ))}
          </div>
        </div>
        {picked && (
          <div style={{ marginTop: 28, padding: "10px 14px",
            background: "rgba(255,122,89,0.12)", border: "1px solid rgba(255,122,89,0.3)",
            borderRadius: 4, display: "inline-flex", alignItems: "center", gap: 10,
            fontSize: 13, color: "#ffd9cd" }}>
            <span style={{ width: 6, height: 6, borderRadius: "50%", background: "#ff7a59" }} />
            Selected — we'll donate in your name at closing.
          </div>
        )}
      </div>
    </section>
  );
}

// ---------- TESTIMONIALS ----------
function Testimonials() {
  const quotes = [
    {
      q: "Four banks had already told me no. Haven matched me to an Asset Qualifier in a week. The loan officer knew the program cold.",
      who: "Marcus A.", meta: "Buckhead · Asset Qualifier · closed Feb 2026",
    },
    {
      q: "Self-employed for nine years and every retail lender tried to shoehorn me into a conventional. Haven put me on a Bank Statement, closed in 28 days.",
      who: "Priya R.", meta: "Decatur · Bank Statement · closed Jan 2026",
    },
    {
      q: "I needed a DSCR on a two-unit with Airbnb income. They accepted AirDNA, vested in my LLC, and didn't flinch.",
      who: "Jordan T.", meta: "East Atlanta · Investor Cash Flow · closed Dec 2025",
    },
  ];

  return (
    <section style={{ maxWidth: 1080, margin: "96px auto 0", padding: "0 16px" }}>
      <div style={{ marginBottom: 32 }}>
        <Eyebrow>From borrowers</Eyebrow>
        <h3 style={{ fontFamily: "'Inter Tight',sans-serif", fontFeatureSettings: "'ss01'", fontSize: 36, fontWeight: 300, letterSpacing: "-0.72px", lineHeight: 1.1, color: "#1f2d3d", margin: "8px 0 0", maxWidth: 560 }}>
          Three closings, three programs you'll rarely see at a bank.
        </h3>
      </div>
      <div style={{ display: "grid", gridTemplateColumns: "repeat(3, 1fr)", gap: 16 }}>
        {quotes.map((q, i) => (
          <div key={i} style={{
            background: "#fff", border: "1px solid #e1e7ef", borderRadius: 6,
            padding: 24,
            boxShadow: "rgba(31,45,61,0.06) 0 2px 4px",
            fontFamily: "'Inter Tight',sans-serif", fontFeatureSettings: "'ss01'",
          }}>
            <div style={{ fontSize: 18, fontWeight: 300, letterSpacing: "-0.18px", lineHeight: 1.4, color: "#1f2d3d" }}>
              &ldquo;{q.q}&rdquo;
            </div>
            <div style={{ marginTop: 20, paddingTop: 16, borderTop: "1px solid #e1e7ef" }}>
              <div style={{ fontSize: 14, fontWeight: 400, color: "#1f2d3d" }}>{q.who}</div>
              <div style={{ fontSize: 12, fontWeight: 300, color: "#6b7c92", marginTop: 2 }}>{q.meta}</div>
            </div>
          </div>
        ))}
      </div>
    </section>
  );
}

// ---------- FINAL CTA ----------
function FinalCTA() {
  return (
    <section style={{ maxWidth: 1080, margin: "96px auto 0", padding: "0 16px" }}>
      <div style={{
        background: "#fff", border: "1px solid #e1e7ef", borderRadius: 8,
        padding: "48px 56px", display: "flex", justifyContent: "space-between", alignItems: "center", gap: 32,
        boxShadow: "rgba(31,45,61,0.22) 0 30px 45px -30px, rgba(0,0,0,0.08) 0 18px 36px -18px",
        fontFamily: "'Inter Tight',sans-serif", fontFeatureSettings: "'ss01'",
      }}>
        <div style={{ maxWidth: 540 }}>
          <Eyebrow>Fifteen minutes</Eyebrow>
          <div style={{ marginTop: 10, fontSize: 32, fontWeight: 300, letterSpacing: "-0.64px", lineHeight: 1.1, color: "#1f2d3d" }}>
            Tell us what you're trying to do. We'll tell you if we can help.
          </div>
          <div style={{ marginTop: 10, fontSize: 14, fontWeight: 300, color: "#6b7c92" }}>
            No soft pull at this stage. No script. A veteran broker, on a real call.
          </div>
        </div>
        <div style={{ display: "flex", gap: 12, flexShrink: 0 }}>
          <PrimaryButton as="a" href="contact.html">Start the call</PrimaryButton>
          <GhostButton as="a" href="products.html">Browse programs</GhostButton>
        </div>
      </div>
    </section>
  );
}

Object.assign(window, {
  Hero, StatsStrip, ProductGrid, ProductCard, ProcessSteps, Pillars,
  CharityBand, Testimonials, FinalCTA,
});
