/* global React */
/* Haven Home Loans — shared UI primitives */

const { useState: useStateShared, useEffect: useEffectShared } = React;

// ---------- Logo ----------
function Logo({ dark = false, size = 28 }) {
  const navy = dark ? "#ffffff" : "#34495E";
  const windowColor = dark ? "#34495E" : "#ffffff";
  return (
    <svg viewBox="0 0 240 56" width={(240 / 56) * size} height={size} aria-label="Haven">
      <path d="M7 28 C7 14 18 4 30 4 C25 4 19 8 16 14 C13 20 12 26 14 32 C16 38 21 44 28 46 C18 46 7 40 7 28 Z" fill="#ff7a59" />
      <path d="M15 32 L26 22 L37 32 V46 H15 Z M22 46 V38 H31 V46" fill={navy} />
      <rect x="29" y="30" width="3" height="3" fill={windowColor} />
      <text x="54" y="39" fontFamily="Montserrat, 'Inter Tight', sans-serif" fontWeight="700" fontSize="26" letterSpacing="2.2" fill={navy}>HAVEN</text>
    </svg>
  );
}

// ---------- Buttons ----------
function PrimaryButton({ children, onClick, small = false, as = "button", href, style = {}, disabled = false }) {
  const s = {
    background: disabled ? "#cbd5df" : "var(--hv-coral, #ff7a59)", color: "#fff", border: "none",
    padding: small ? "6px 12px" : "9px 16px",
    borderRadius: 4,
    fontFamily: "'Inter Tight',sans-serif", fontFeatureSettings: "'ss01'",
    fontSize: small ? 14 : 16, fontWeight: 400, cursor: disabled ? "not-allowed" : "pointer", lineHeight: 1,
    textDecoration: "none", display: "inline-block",
    transition: "background 180ms cubic-bezier(0.2,0.8,0.2,1)",
    ...style,
  };
  const onEnter = (e) => { if (!disabled) e.currentTarget.style.background = "#e85f3d"; };
  const onLeave = (e) => { if (!disabled) e.currentTarget.style.background = "var(--hv-coral, #ff7a59)"; };
  if (as === "a") return <a href={href} onClick={onClick} onMouseEnter={onEnter} onMouseLeave={onLeave} style={s}>{children}</a>;
  return <button onClick={onClick} onMouseEnter={onEnter} onMouseLeave={onLeave} style={s} disabled={disabled}>{children}</button>;
}

function GhostButton({ children, onClick, small = false, as = "button", href, style = {} }) {
  const s = {
    background: "transparent", color: "#33475b",
    border: "1px solid #cbd5df",
    padding: small ? "5px 12px" : "8px 16px",
    borderRadius: 4,
    fontFamily: "'Inter Tight',sans-serif", fontFeatureSettings: "'ss01'",
    fontSize: small ? 14 : 16, fontWeight: 400, cursor: "pointer", lineHeight: 1,
    textDecoration: "none", display: "inline-block",
    transition: "background 180ms cubic-bezier(0.2,0.8,0.2,1)",
    ...style,
  };
  const onEnter = (e) => { e.currentTarget.style.background = "#f5f8fb"; };
  const onLeave = (e) => { e.currentTarget.style.background = "transparent"; };
  if (as === "a") return <a href={href} onClick={onClick} onMouseEnter={onEnter} onMouseLeave={onLeave} style={s}>{children}</a>;
  return <button onClick={onClick} onMouseEnter={onEnter} onMouseLeave={onLeave} style={s}>{children}</button>;
}

// ---------- Nav ----------
function Nav({ current, home = "index.html" }) {
  const links = [
    ["products.html", "Products", "products"],
    ["process.html", "Process", "process"],
    ["about.html", "About", "about"],
    ["community.html", "Community", "community"],
    ["reviews.html", "Reviews", "reviews"],
    ["blog.html", "Blog", "blog"],
  ];

  const [menuOpen, setMenuOpen] = useStateShared(false);

  // Lock body scroll when overlay is open
  useEffectShared(() => {
    if (menuOpen) {
      const prev = document.body.style.overflow;
      document.body.style.overflow = "hidden";
      return () => { document.body.style.overflow = prev; };
    }
  }, [menuOpen]);

  return (
    <React.Fragment>
    <header style={{ position: "sticky", top: 12, zIndex: 20, margin: "12px auto 0", maxWidth: 1080, padding: "0 16px" }}>
      <nav style={{
        display: "flex", alignItems: "center", justifyContent: "space-between",
        padding: "10px 16px",
        background: "rgba(255,255,255,0.88)",
        backdropFilter: "blur(12px)", WebkitBackdropFilter: "blur(12px)",
        border: "1px solid #e1e7ef", borderRadius: 6,
        boxShadow: "rgba(31,45,61,0.06) 0 2px 4px",
      }}>
        <div style={{ display: "flex", alignItems: "center", gap: 28 }}>
          <a href={home} style={{ textDecoration: "none", display: "flex", alignItems: "center" }}>
            <Logo size={26} />
          </a>
          <div className="hv-nav-links" style={{ display: "flex", gap: 22, fontSize: 14, fontWeight: 500 }}>
            {links.map(([href, label, key]) => (
              <a key={key} href={href}
                style={{
                  color: current === key ? "#c64a2b" : "#34495E",
                  textDecoration: "none",
                  fontFamily: "'Inter Tight',sans-serif",
                  whiteSpace: "nowrap",
                }}>{label}</a>
            ))}
          </div>
        </div>
        <div style={{ display: "flex", gap: 10, alignItems: "center", flexShrink: 0 }}>
          <a className="hv-nav-broker" href="admin.html" style={{
            fontSize: 13, fontWeight: 400, color: "#6b7c92",
            fontFamily: "'Inter Tight',sans-serif",
            textDecoration: "none", whiteSpace: "nowrap",
          }}>Broker login</a>
          <PrimaryButton as="a" href="contact.html" small style={{ whiteSpace: "nowrap" }}>Start now</PrimaryButton>
          {/* Hamburger — hidden on desktop via responsive.css, shown on ≤768px */}
          <button
            type="button"
            className="hv-nav-hamburger"
            aria-label="Open menu"
            aria-expanded={menuOpen}
            onClick={() => setMenuOpen(true)}
            style={{
              display: "none",
              alignItems: "center", justifyContent: "center",
              background: "transparent", border: "none",
              cursor: "pointer", padding: 6, marginLeft: 2,
            }}>
            <svg width="22" height="14" viewBox="0 0 22 14" fill="none" aria-hidden="true">
              <line x1="0" y1="2"  x2="22" y2="2"  stroke="#34495E" strokeWidth="2" strokeLinecap="round"/>
              <line x1="0" y1="7"  x2="22" y2="7"  stroke="#34495E" strokeWidth="2" strokeLinecap="round"/>
              <line x1="0" y1="12" x2="22" y2="12" stroke="#34495E" strokeWidth="2" strokeLinecap="round"/>
            </svg>
          </button>
        </div>
      </nav>
    </header>

    {/* Mobile overlay menu — hidden on desktop via responsive.css */}
    {menuOpen && (
      <div className="hv-mobile-menu" role="dialog" aria-modal="true" aria-label="Menu" style={{
        position: "fixed", inset: 0, zIndex: 100,
        background: "#ffffff",
        padding: "20px 24px 32px",
        display: "flex", flexDirection: "column",
        fontFamily: "'Inter Tight',sans-serif",
      }}>
        <div style={{ display: "flex", justifyContent: "space-between", alignItems: "center", marginBottom: 36 }}>
          <a href={home} onClick={() => setMenuOpen(false)} style={{ textDecoration: "none", display: "flex", alignItems: "center" }}>
            <Logo size={28}/>
          </a>
          <button
            type="button"
            onClick={() => setMenuOpen(false)}
            aria-label="Close menu"
            style={{
              display: "inline-flex", alignItems: "center", justifyContent: "center",
              background: "transparent", border: "none", cursor: "pointer", padding: 6,
            }}>
            <svg width="22" height="22" viewBox="0 0 22 22" fill="none" aria-hidden="true">
              <line x1="3" y1="3" x2="19" y2="19" stroke="#34495E" strokeWidth="2" strokeLinecap="round"/>
              <line x1="19" y1="3" x2="3" y2="19" stroke="#34495E" strokeWidth="2" strokeLinecap="round"/>
            </svg>
          </button>
        </div>
        <nav style={{ display: "flex", flexDirection: "column", gap: 22 }}>
          {links.map(([href, label, key]) => (
            <a key={key} href={href} onClick={() => setMenuOpen(false)}
              style={{
                fontSize: 26, lineHeight: 1.15,
                fontFamily: "Montserrat,'Inter Tight',sans-serif",
                fontWeight: 700, letterSpacing: "-0.3px",
                color: current === key ? "#c64a2b" : "#34495E",
                textDecoration: "none",
              }}>{label}</a>
          ))}
        </nav>
        <div style={{ marginTop: "auto", paddingTop: 28, borderTop: "1px solid #e1e7ef", display: "flex", flexDirection: "column", gap: 14 }}>
          <a href="admin.html" onClick={() => setMenuOpen(false)}
            style={{ fontSize: 14, color: "#6b7c92", textDecoration: "none" }}>Broker login</a>
          <PrimaryButton as="a" href="contact.html" onClick={() => setMenuOpen(false)}
            style={{ width: "100%", textAlign: "center", padding: "12px 16px", fontSize: 16 }}>
            Start now
          </PrimaryButton>
        </div>
      </div>
    )}
    </React.Fragment>
  );
}

// ---------- Equal Housing Lender mark ----------
function EqualHousingMark({ size = 14, color = "#425a73", labelColor }) {
  return (
    <span style={{ display: "inline-flex", alignItems: "center", gap: 6 }}
      role="img" aria-label="Equal Housing Lender logo">
      <svg width={size} height={size} viewBox="0 0 24 24" fill="none" stroke={color} strokeWidth="1.8" aria-hidden="true">
        <path d="M3 10 L12 4 L21 10 V20 H3 Z"/><rect x="10" y="13" width="4" height="7"/>
        <path d="M6 14 H8 M16 14 H18 M6 17 H8 M16 17 H18" strokeWidth="1.2"/>
      </svg>
      <span style={{ color: labelColor || color, fontSize: size - 1, fontWeight: 400, fontFamily: "'Inter Tight',sans-serif", fontFeatureSettings: "'ss01'" }}>
        Equal Housing Lender
      </span>
    </span>
  );
}

// ---------- Cookie consent banner ----------
function CookieBanner() {
  const [show, setShow] = useStateShared(false);
  useEffectShared(() => {
    try { if (!localStorage.getItem("hv-cookie-consent")) setShow(true); } catch (e) {}
  }, []);
  const set = (v) => { try { localStorage.setItem("hv-cookie-consent", v); } catch (e) {} setShow(false); };
  if (!show) return null;
  return (
    <div role="dialog" aria-label="Cookie preferences" style={{
      position: "fixed", bottom: 16, left: 16, right: 16, zIndex: 60,
      maxWidth: 720, margin: "0 auto", padding: "16px 20px",
      background: "#fff", border: "1px solid #e1e7ef", borderRadius: 8,
      boxShadow: "rgba(31,45,61,0.22) 0 30px 45px -20px, rgba(0,0,0,0.12) 0 18px 36px -18px",
      display: "flex", alignItems: "center", gap: 16, flexWrap: "wrap",
      fontFamily: "'Inter Tight',sans-serif", fontFeatureSettings: "'ss01'",
    }}>
      <div style={{ flex: "1 1 280px", fontSize: 13, fontWeight: 300, color: "#425a73", lineHeight: 1.5 }}>
        We use cookies for analytics and to improve this site. You can accept all, accept only essentials, or read our <a href="privacy.html" style={{ color: "#c64a2b", textDecoration: "underline" }}>Privacy Notice</a>.
      </div>
      <div style={{ display: "flex", gap: 8 }}>
        <GhostButton small onClick={() => set("essential")}>Essentials only</GhostButton>
        <PrimaryButton small onClick={() => set("all")}>Accept all</PrimaryButton>
      </div>
    </div>
  );
}

// ---------- Footer ----------
function Footer() {
  return (
    <React.Fragment>
    {/* Brand pillar ribbon — Local Expertise · Personal Service · Smart Solutions */}
    <section style={{ marginTop: 96, padding: "32px 16px", background: "#34495E", color: "#fff" }}>
      <div style={{ maxWidth: 1080, margin: "0 auto", display: "grid", gridTemplateColumns: "repeat(3, 1fr)", gap: 24, alignItems: "center" }}>
        {[
          ["Local Expertise", "Atlanta-based, Atlanta-rooted. We know your zip code, your school zone, and your closing attorney.",
            <svg width="22" height="22" viewBox="0 0 24 24" fill="none" stroke="#FF7A59" strokeWidth="1.6" strokeLinecap="round" strokeLinejoin="round" key="house"><path d="M3 11 L12 3 L21 11"/><path d="M5 10 V21 H19 V10"/><path d="M10 21 V14 H14 V21"/></svg>],
          ["Personal Service", "Same loan officer from first call to closing table. No relay race, no 800 numbers.",
            <svg width="24" height="24" viewBox="0 0 24 24" fill="none" stroke="#FF7A59" strokeWidth="1.6" strokeLinecap="round" strokeLinejoin="round" key="hands"><path d="M3 13 L7 9 L11 12 L13 11 L17 14"/><path d="M21 13 L17 9 L13 11"/><path d="M3 13 V18 H7"/><path d="M21 13 V18 H17"/></svg>],
          ["Smart Solutions", "Nine programs under one roof — non-QM, jumbo, DSCR, foreign national. Right tool, not the only tool.",
            <svg width="22" height="22" viewBox="0 0 24 24" fill="none" stroke="#FF7A59" strokeWidth="1.6" strokeLinecap="round" strokeLinejoin="round" key="brief"><rect x="3" y="7" width="18" height="13" rx="1.5"/><path d="M9 7 V5 a1 1 0 0 1 1 -1 h4 a1 1 0 0 1 1 1 V7"/><path d="M3 13 H21"/></svg>],
        ].map(([title, body, icon]) => (
          <div key={title} style={{ display: "flex", alignItems: "flex-start", gap: 14 }}>
            <div style={{ flexShrink: 0, marginTop: 2 }}>{icon}</div>
            <div>
              <div style={{ fontFamily: "Montserrat, 'Inter Tight', sans-serif", fontSize: 14, fontWeight: 700, letterSpacing: "0.06em", textTransform: "uppercase", color: "#fff" }}>{title}</div>
              <div style={{ marginTop: 6, fontSize: 13, fontWeight: 400, color: "rgba(255,255,255,0.75)", lineHeight: 1.5 }}>{body}</div>
            </div>
          </div>
        ))}
      </div>
    </section>

    <footer style={{ padding: "48px 16px 28px", borderTop: "1px solid #e1e7ef", background: "#fff",
      fontFamily: "'Inter Tight',sans-serif", fontFeatureSettings: "'ss01'" }}>
      <div style={{ maxWidth: 1080, margin: "0 auto", display: "grid", gridTemplateColumns: "1.4fr 1fr 1fr 1fr", gap: 40 }}>
        <div>
          <Logo size={28} />
          <p style={{ marginTop: 14, fontSize: 13, fontWeight: 300, color: "#6b7c92", lineHeight: 1.55, maxWidth: 300 }}>
            Boutique mortgage brokerage specializing in non-QM loans for borrowers who don't fit a conventional box.
          </p>
          <address style={{ marginTop: 10, fontStyle: "normal", fontSize: 12, fontWeight: 300, color: "#8ba0b8", lineHeight: 1.55, maxWidth: 300 }}>
            Haven Home Loans, LLC<br/>
            2964 Peachtree Road NW, Suite 530<br/>
            Atlanta, GA 30305<br/>
            (678) 884-7873 · info@havenhomeloansllc.com
          </address>
          <div style={{ marginTop: 14 }}>
            <EqualHousingMark size={14} color="#425a73"/>
          </div>
        </div>
        {[
          ["Programs", [
            ["Bank Statement", "product.html?id=bank-statement"],
            ["Asset Qualifier", "product.html?id=asset-qualifier"],
            ["10% Down Jumbo", "product.html?id=jumbo"],
            ["Investor Cash Flow", "product.html?id=dscr"],
            ["Foreign National", "product.html?id=foreign"],
            ["Recovery", "product.html?id=recovery"],
          ]],
          ["Company", [
            ["About", "about.html"],
            ["Process", "process.html"],
            ["Community", "community.html"],
            ["Reviews", "reviews.html"],
            ["Blog", "blog.html"],
            ["Contact", "contact.html"],
          ]],
          ["Legal", [
            ["Disclosures", "disclosures.html"],
            ["Privacy Notice", "privacy.html"],
            ["Terms of Use", "terms.html"],
            ["Accessibility", "accessibility.html"],
            ["NMLS Consumer Access ↗", "https://www.nmlsconsumeraccess.org/EntityDetails.aspx/COMPANY/2431561"],
          ]],
        ].map(([head, items]) => (
          <div key={head}>
            <div style={{ fontFamily: "'Source Code Pro',monospace", fontSize: 11, fontWeight: 500, color: "#425a73", textTransform: "uppercase", letterSpacing: "0.08em", marginBottom: 12 }}>{head}</div>
            <div style={{ display: "flex", flexDirection: "column", gap: 8 }}>
              {items.map(([label, href]) => {
                const ext = href.startsWith("http");
                return (
                  <a key={label} href={href}
                    target={ext ? "_blank" : undefined}
                    rel={ext ? "noopener noreferrer" : undefined}
                    style={{ fontSize: 13, fontWeight: 400, color: "#1f2d3d", textDecoration: "none" }}>{label}</a>
                );
              })}
            </div>
          </div>
        ))}
      </div>

      {/* Compliance block */}
      <div style={{ maxWidth: 1080, margin: "32px auto 0", paddingTop: 20, borderTop: "1px solid #e1e7ef",
        display: "grid", gridTemplateColumns: "1.4fr 1fr", gap: 32 }}>
        <div style={{ fontSize: 11, fontWeight: 300, color: "#6b7c92", lineHeight: 1.6 }}>
          <div style={{ fontFamily: "'Source Code Pro',monospace", fontSize: 10, fontWeight: 500, color: "#425a73", textTransform: "uppercase", letterSpacing: "0.08em", marginBottom: 6 }}>Licensing</div>
          Haven Home Loans, LLC · NMLS <a href="https://www.nmlsconsumeraccess.org/EntityDetails.aspx/COMPANY/2431561" target="_blank" rel="noopener noreferrer" style={{ color: "#c64a2b", textDecoration: "none" }}>#2431561</a><br/>
          Georgia Mortgage Broker License <span style={{ fontFeatureSettings: "'tnum'" }}>#2431561</span> · Florida Mortgage Broker License <span style={{ fontFeatureSettings: "'tnum'" }}>#MBR6228</span> · Texas SML Mortgage Company License <span style={{ fontFeatureSettings: "'tnum'" }}>#2431561</span><br/>
          Licensed in Georgia, Florida, and Texas. This site is not an offer to lend in any state where Haven is not licensed.
        </div>
        <div style={{ fontSize: 11, fontWeight: 300, color: "#6b7c92", lineHeight: 1.6 }}>
          <div style={{ fontFamily: "'Source Code Pro',monospace", fontSize: 10, fontWeight: 500, color: "#425a73", textTransform: "uppercase", letterSpacing: "0.08em", marginBottom: 6 }}>Verify us</div>
          NMLS Consumer Access:&nbsp;
          <a href="https://www.nmlsconsumeraccess.org" target="_blank" rel="noopener noreferrer" style={{ color: "#c64a2b", textDecoration: "none" }}>www.nmlsconsumeraccess.org</a>
        </div>
      </div>

      <div style={{ maxWidth: 1080, margin: "20px auto 0", fontSize: 10, fontWeight: 300, color: "#8ba0b8", lineHeight: 1.55 }}>
        Haven Home Loans, LLC is a licensed mortgage broker, not a lender. Loans arranged through third-party lenders. Not a commitment to lend. All loans subject to credit approval, program guidelines, underwriting, and property appraisal. Rates and terms vary by borrower, program, property, and market conditions, and are subject to change without notice. Haven works with multiple wholesale lenders and presents loan options based on your circumstances; other loan products may be available beyond what is shown on this site. Equal Housing Lender. © 2026 Haven Home Loans, LLC.
      </div>
    </footer>
    <CookieBanner/>
    </React.Fragment>
  );
}

// ---------- Eyebrow ----------
function Eyebrow({ children, tone = "muted" }) {
  const color = tone === "coral" ? "#c64a2b" : "#6b7c92";
  return (
    <div style={{
      fontFamily: "'Source Code Pro',monospace", fontSize: 11, fontWeight: 500,
      color, textTransform: "uppercase", letterSpacing: "0.08em",
    }}>{children}</div>
  );
}

// ---------- Card ----------
function Card({ children, padding = 20, hover = false, style = {} }) {
  const base = {
    background: "#fff", border: "1px solid #e1e7ef", borderRadius: 6,
    padding, 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'",
    ...style,
  };
  if (!hover) return <div style={base}>{children}</div>;
  return (
    <div style={base}
      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"}
    >{children}</div>
  );
}

// ---------- Page shell ----------
function PageHeader({ eyebrow, title, subtitle, maxWidth = 720 }) {
  return (
    <section style={{ maxWidth: 1080, margin: "64px auto 0", padding: "0 16px" }}>
      <div style={{ maxWidth }}>
        {eyebrow && <Eyebrow tone="coral">{eyebrow}</Eyebrow>}
        <h1 style={{
          fontFamily: "'Inter Tight',sans-serif", fontFeatureSettings: "'ss01'",
          fontSize: 56, fontWeight: 300, letterSpacing: "-1.4px", lineHeight: 1.05,
          color: "#1f2d3d", margin: eyebrow ? "12px 0 0" : "0",
        }}>{title}</h1>
        {subtitle && <p style={{
          marginTop: 18, fontSize: 18, fontWeight: 300, color: "#6b7c92", lineHeight: 1.45,
          fontFamily: "'Inter Tight',sans-serif", fontFeatureSettings: "'ss01'",
        }}>{subtitle}</p>}
      </div>
    </section>
  );
}

// ---------- URL query reader ----------
function useQueryParam(key) {
  const [value, setValue] = useStateShared(() => new URLSearchParams(window.location.search).get(key));
  useEffectShared(() => {
    const update = () => setValue(new URLSearchParams(window.location.search).get(key));
    window.addEventListener("popstate", update);
    return () => window.removeEventListener("popstate", update);
  }, [key]);
  return value;
}

Object.assign(window, {
  Logo, PrimaryButton, GhostButton, EqualHousingMark, CookieBanner,
  Nav, Footer, Eyebrow, Card, PageHeader, useQueryParam,
});
