// Shared React components for Saela landing pages
const { useState, useEffect, useRef } = React;

// ============== Saela Logo (inline SVG) ==============
function SaelaLogo({ height = 28, color }) {
  return (
    <svg height={height} viewBox="0 0 218.23 94.97" xmlns="http://www.w3.org/2000/svg" style={{ color, display: "block" }} aria-label="Saela Pest Control">
      <path fill="currentColor" d="M13.79,16.85c0-3.66,4.6-6.21,11.49-6.21,8,0,13.36,3.4,14.47,8.6l11-5.62C47.49,5.36,38,0,25.36,0c-14.3,0-24,6.89-24,17.87C1.36,39.66,40,33.19,40,45.87c0,4.77-5.19,7.74-12.26,7.74-9.11,0-16-4.85-16.17-12.09L0,46.73C1.7,57.45,12.43,64.35,27.15,64.35c15.49,0,25.7-7.66,25.7-19.49,0-23-39.06-17.29-39.06-28" />
      <rect fill="currentColor" x="157.49" y="1.78" width="11.06" height="61.45" />
      <path fill="currentColor" d="M130.35,23.16c3.74,0,6.26.95,7.83,2.52a6.48,6.48,0,0,1,1.92,4.26A4.37,4.37,0,0,1,139,33.2c-2.77,3-12.5,2.31-19,2l-2.23-.12c.88-5.38,4.68-11.89,12.61-11.89M77.74,54.55c-3.74,0-6.26-.95-7.83-2.52A6.48,6.48,0,0,1,68,47.77a4.37,4.37,0,0,1,1.09-3.26c2.77-3,12.5-2.31,19-2l2.23.12C89.47,48,85.66,54.55,77.74,54.55m52.61-.1c-6.2,0-10.46-4.6-11.75-9.62l1.18.07c10.38.55,21.12,1.12,27-4.73a13,13,0,0,0,3.68-10.24,16.21,16.21,0,0,0-5.56-11.51c-4.21-3.7-9.19-5-14.56-5-13.7,0-21.23,10.44-23.14,20.7H107c-1.06-.09-4.85-.4-6.22-.5-.92-5.12-3.07-10.4-7.16-14.07-4.64-4.16-12-6.11-15.94-6.11-9.63,0-15.2,3-19.5,8.63l8.44,6.6c1.29-2.73,4.8-5.42,11.09-5.42s10.46,4.6,11.74,9.62l-1.18-.07c-10.38-.55-21.12-1.12-27,4.73a13,13,0,0,0-3.68,10.24,16.21,16.21,0,0,0,5.56,11.51c4.21,3.7,9.19,5,14.56,5,13.7,0,21.23-10.44,23.14-20.7h.18c1,.09,4.85.4,6.22.5.92,5.12,3.07,10.4,7.16,14.07,4.64,4.16,11.95,6.11,15.94,6.11,9.63,0,15.2-3,19.5-8.63L141.43,49c-1.28,2.72-4.79,5.42-11.08,5.42" />
      <path fill="currentColor" d="M206.48,43.58c0,6-6.81,11.41-14,11.41-4.68,0-7.4-2.3-7.4-5.7,0-3.92,3.49-6,12.68-7.41l8.68-1.36Zm10.64,9.1V30.13c0-10.72-7.15-16.68-19.75-16.68-11,0-18,4.51-21.62,12.17l9.54,4.88c2.19-5.16,5.77-7.26,11.66-7.26,6,0,9.53,2.55,9.53,7.32v.94l-12.09,1.87C180.69,35.5,174.05,41,174.05,50c0,8.68,6.72,14.3,16.6,14.3,6.81,0,12.17-2.72,15.83-7.66a35.78,35.78,0,0,0,.77,6.64h11a45.79,45.79,0,0,1-1.11-10.57" />
      <path fill="currentColor" d="M23.62,94.66H20.16V75.27H32.21c4.32,0,7.16,2,7.16,5.94s-2.84,5.94-7.16,5.94H23.62ZM32,84.18c2.49,0,3.94-1.08,3.94-3s-1.46-3-3.94-3h-8.4v5.94Z" />
      <path fill="currentColor" d="M52.58,89.29l2.59,1.38C54.31,92.88,51.63,95,47.82,95c-4.73,0-8.18-3-8.18-7.37,0-4.16,3.35-7.37,8.18-7.37,4.67,0,7.62,3.11,7.62,7.18v1.13H42.72c.43,2.27,2.46,3.67,5.1,3.67A4.85,4.85,0,0,0,52.58,89.29ZM42.86,86.1h9.45c-.22-1.49-2-3.19-4.65-3.19A4.7,4.7,0,0,0,42.86,86.1Z" />
      <path fill="currentColor" d="M58.44,89.58a7,7,0,0,0,6,2.7c2.24,0,3.75-.51,3.75-1.73s-1.22-1.27-4.59-1.67c-3.7-.46-6.51-1.08-6.51-4.08,0-2.67,2.59-4.59,6.7-4.59,3.4,0,6.05,1.3,7.27,2.92l-2.08,2a6.48,6.48,0,0,0-5.32-2.24c-2.35,0-3.46.65-3.46,1.62s1,1.13,4.16,1.51c3.81.46,7,.92,7,4.21S68,95,64.44,95s-6.73-1.19-8.29-3.3Z" />
      <path fill="currentColor" d="M75.3,83.24H71.73V80.48H75.3V76.32h3.19v4.16h6.24v2.76H78.49V89.5c0,1.84,1,2.4,3.13,2.4a10.18,10.18,0,0,0,3.11-.59l.54,2.76a13.28,13.28,0,0,1-4.21.76c-3,0-5.75-1.22-5.75-5V83.24Z" />
      <path fill="currentColor" d="M104.87,75c4.94,0,8.64,2.27,10.13,6l-2.86,1.57C110.9,79.62,108.41,78,104.9,78,100.33,78,97,80.84,97,85s3.35,6.94,7.91,6.94c3.51,0,6-1.59,7.24-4.51L115,89c-1.49,3.73-5.19,6-10.13,6-6.51,0-11.43-4.24-11.43-10S98.36,75,104.87,75Z" />
      <path fill="currentColor" d="M132.13,87.58c0,4.4-3.59,7.37-8.08,7.37s-8.1-3-8.1-7.37,3.59-7.37,8.1-7.37S132.13,83.18,132.13,87.58Zm-3.11,0c0-2.89-2.21-4.59-5-4.59s-5,1.7-5,4.59,2.21,4.59,5,4.59S129,90.47,129,87.58Z" />
      <path fill="currentColor" d="M134.4,80.48h3.24v1.86a6.78,6.78,0,0,1,5.24-2.13c3.94,0,6.16,2.76,6.16,6.73v7.72H145.8V87.47c0-2.51-1.3-4.29-3.92-4.29s-4.24,1.73-4.24,4.35v7.13H134.4Z" />
      <path fill="currentColor" d="M153.76,83.24h-3.57V80.48h3.57V76.32H157v4.16h6.24v2.76H157V89.5c0,1.84,1,2.4,3.13,2.4a10.18,10.18,0,0,0,3.11-.59l.54,2.76a13.28,13.28,0,0,1-4.21.76c-3,0-5.75-1.22-5.75-5V83.24Z" />
      <path fill="currentColor" d="M176.05,83.86a5.67,5.67,0,0,0-1.67-.24c-3.24,0-5.35,1.38-5.35,4.75v6.29h-3.24V80.48H169v2.57a6.66,6.66,0,0,1,7-2.43v3.24Z" />
      <path fill="currentColor" d="M192.82,87.58c0,4.4-3.59,7.37-8.08,7.37s-8.1-3-8.1-7.37,3.59-7.37,8.1-7.37S192.82,83.18,192.82,87.58Zm-3.11,0c0-2.89-2.21-4.59-5-4.59s-5,1.7-5,4.59,2.21,4.59,5,4.59S189.71,90.47,189.71,87.58Z" />
      <path fill="currentColor" d="M198.33,94.66h-3.24V75.27h3.24Z" />
    </svg>
  );
}

// ============== Stars ==============
function Stars({ n = 5, color = "#EA9230" }) {
  return (
    <span style={{ display: "inline-flex", gap: 2, color }}>
      {Array.from({ length: n }).map((_, i) =>
        <svg key={i} width="14" height="14" viewBox="0 0 24 24" fill="currentColor"><path d="M12 2l3.09 6.26L22 9.27l-5 4.87 1.18 6.88L12 17.77l-6.18 3.25L7 14.14 2 9.27l6.91-1.01L12 2z" /></svg>
      )}
    </span>
  );
}

// ============== Top Nav ==============
function TopNav() {
  const navItems = [
    { label: "Residential", href: "#" },
    { label: "Commercial", href: "#" },
    { label: "Services", href: "#pests" },
    { label: "Pricing", href: "#" },
    { label: "About", href: "#why" },
    { label: "Reviews", href: "#reviews" },
    { label: "Contact", href: "#quote" },
  ];
  const [mobileOpen, setMobileOpen] = useState(false);
  return (
    <header style={{ background: "#FFFFFF", color: "#26453D", borderBottom: "1px solid rgba(38,69,61,0.08)", padding: "20px 40px", display: "flex", alignItems: "center", justifyContent: "space-between", position: "sticky", top: 0, zIndex: 40, gap: 32 }}>
      <a href="/" style={{ display: "flex", flexDirection: "column", alignItems: "flex-start", textDecoration: "none", color: "#26453D", lineHeight: 1 }}>
        <SaelaLogo height={36} color="#26453D" />
      </a>
      <nav style={{ display: "flex", gap: 36, alignItems: "center", fontSize: 15, fontFamily: "Inter, sans-serif", flex: 1, justifyContent: "center" }}>
        {navItems.map((item) =>
          <a key={item.label} href={item.href} style={{ color: "#26453D", textDecoration: "none", fontWeight: 400 }}>{item.label}</a>
        )}
      </nav>
      <a href="#quote" style={{ background: "#EA9230", color: "#FFFFFF", textDecoration: "none", padding: "14px 22px", borderRadius: 6, fontWeight: 600, fontSize: 14, fontFamily: "Inter, sans-serif", whiteSpace: "nowrap" }}>Get a Free Inspection</a>
    </header>
  );
}

// ============== Breadcrumb ==============
function Breadcrumb({ trail }) {
  return (
    <div style={{ background: "#F5F5EB", borderBottom: "1px solid rgba(38,69,61,0.06)" }}>
      <div style={{ maxWidth: 1200, margin: "0 auto", padding: "14px 40px", fontSize: 13, color: "rgba(38,69,61,0.6)", fontFamily: "Inter, sans-serif" }}>
        {trail.map((t, i) => (
          <span key={i}>
            {i > 0 && <span style={{ margin: "0 8px", opacity: 0.4 }}>/</span>}
            {t.href ? <a href={t.href} style={{ color: "inherit", textDecoration: "none" }}>{t.label}</a> : <strong style={{ color: "#26453D", fontWeight: 500 }}>{t.label}</strong>}
          </span>
        ))}
      </div>
    </div>
  );
}

// ============== Multi-step Lead Form ==============
function LeadForm({ cityName, phone, phoneRaw, variant = "light" }) {
  const [step, setStep] = useState(0);
  const [data, setData] = useState({ pest: null, urgency: null, name: "", phone: "", zip: "" });
  const [submitted, setSubmitted] = useState(false);
  const totalSteps = 3;
  const progress = (step + (submitted ? 1 : 0)) / totalSteps * 100;
  const isDark = variant === "dark";
  const bg = isDark ? "#26453D" : "#FFFFFF";
  const fg = isDark ? "#F5F5EB" : "#26453D";
  const muted = isDark ? "rgba(245,245,235,0.7)" : "rgba(38,69,61,0.6)";
  const border = isDark ? "rgba(245,245,235,0.18)" : "rgba(38,69,61,0.14)";
  const inputBg = isDark ? "rgba(245,245,235,0.06)" : "#F5F5EB";
  const next = () => setStep((s) => Math.min(s + 1, totalSteps - 1));
  const back = () => setStep((s) => Math.max(s - 1, 0));
  const submit = (e) => { e.preventDefault(); setSubmitted(true); };

  const pests = [
    { id: "ants", label: "Ants" }, { id: "wasps", label: "Wasps" },
    { id: "spiders", label: "Spiders" }, { id: "rodents", label: "Rodents" },
    { id: "roaches", label: "Roaches" }, { id: "bedbugs", label: "Bed bugs" },
    { id: "other", label: "Other" }, { id: "unsure", label: "Not sure" },
  ];
  const urgency = [
    { id: "today", label: "Today / ASAP" }, { id: "week", label: "This week" },
    { id: "month", label: "Within a month" }, { id: "planning", label: "Planning ahead" },
  ];
  const inpStyle = { padding: "12px 14px", borderRadius: 8, border: "1px solid " + border, background: inputBg, color: fg, fontSize: 14, fontFamily: "Inter, sans-serif", outline: "none", width: "100%", boxSizing: "border-box" };

  if (submitted) {
    return (
      <div style={{ background: bg, color: fg, borderRadius: 14, padding: 28, border: "1px solid " + border }}>
        <div style={{ fontFamily: "'DM Serif Display', serif", fontSize: 26, lineHeight: 1.15, marginBottom: 8 }}>Thanks — we'll call within 15 minutes.</div>
        <p style={{ color: muted, fontSize: 14, lineHeight: 1.5 }}>A {cityName} inspector will reach out at <strong style={{ color: fg }}>{data.phone || "your number"}</strong> to confirm the inspection window.</p>
      </div>
    );
  }

  return (
    <div style={{ background: bg, color: fg, borderRadius: 14, padding: 26, border: "1px solid " + border, boxShadow: isDark ? "none" : "0 18px 40px -22px rgba(38,69,61,0.35)" }}>
      <div style={{ display: "flex", justifyContent: "space-between", marginBottom: 12 }}>
        <div style={{ fontSize: 11, fontWeight: 600, letterSpacing: 1.2, textTransform: "uppercase", color: muted }}>Get pricing · Step {step + 1} of {totalSteps}</div>
        <div style={{ fontSize: 11, color: muted }}>~30 sec</div>
      </div>
      <div style={{ height: 3, background: border, borderRadius: 2, marginBottom: 18, overflow: "hidden" }}>
        <div style={{ width: progress + "%", height: "100%", background: "#EA9230", transition: "width 240ms" }}></div>
      </div>
      {step === 0 && (
        <div>
          <div style={{ fontFamily: "'DM Serif Display', serif", fontSize: 24, lineHeight: 1.15, marginBottom: 12 }}>What's the pest?</div>
          <div style={{ display: "grid", gridTemplateColumns: "repeat(2, 1fr)", gap: 8 }}>
            {pests.map((p) => {
              var sel = data.pest === p.id;
              return (
                <button key={p.id} onClick={() => { setData({ ...data, pest: p.id }); setTimeout(next, 160); }}
                  style={{ textAlign: "left", padding: "12px 14px", borderRadius: 8, border: "1px solid " + (sel ? "#EA9230" : border), background: sel ? "rgba(234,146,48,0.12)" : inputBg, color: fg, fontSize: 14, fontWeight: 500, cursor: "pointer", fontFamily: "Inter, sans-serif" }}>{p.label}</button>
              );
            })}
          </div>
        </div>
      )}
      {step === 1 && (
        <div>
          <div style={{ fontFamily: "'DM Serif Display', serif", fontSize: 24, lineHeight: 1.15, marginBottom: 12 }}>How soon?</div>
          <div style={{ display: "flex", flexDirection: "column", gap: 8 }}>
            {urgency.map((u) => {
              var sel = data.urgency === u.id;
              return (
                <button key={u.id} onClick={() => { setData({ ...data, urgency: u.id }); setTimeout(next, 160); }}
                  style={{ textAlign: "left", padding: "14px 16px", borderRadius: 8, border: "1px solid " + (sel ? "#EA9230" : border), background: sel ? "rgba(234,146,48,0.12)" : inputBg, color: fg, fontSize: 14, fontWeight: 500, cursor: "pointer", fontFamily: "Inter, sans-serif" }}>{u.label}</button>
              );
            })}
          </div>
          <button onClick={back} style={{ marginTop: 12, background: "none", border: "none", color: muted, fontSize: 13, cursor: "pointer", padding: 0, fontFamily: "Inter, sans-serif" }}>← Back</button>
        </div>
      )}
      {step === 2 && (
        <form onSubmit={submit}>
          <div style={{ fontFamily: "'DM Serif Display', serif", fontSize: 24, lineHeight: 1.15, marginBottom: 12 }}>Where do we send the quote?</div>
          <div style={{ display: "flex", flexDirection: "column", gap: 10 }}>
            <input required placeholder="Full name" value={data.name} onChange={(e) => setData({ ...data, name: e.target.value })} style={inpStyle} />
            <input required type="tel" placeholder="Phone number" value={data.phone} onChange={(e) => setData({ ...data, phone: e.target.value })} style={inpStyle} />
            <input required placeholder="ZIP code" value={data.zip} onChange={(e) => setData({ ...data, zip: e.target.value })} style={inpStyle} />
          </div>
          <button type="submit" style={{ marginTop: 14, width: "100%", background: "#EA9230", color: "#FFFFFF", border: "none", padding: "14px 18px", borderRadius: 8, fontSize: 15, fontWeight: 600, cursor: "pointer", fontFamily: "Inter, sans-serif", letterSpacing: 0.2 }}>Get my free inspection →</button>
          <button type="button" onClick={back} style={{ marginTop: 10, background: "none", border: "none", color: muted, fontSize: 13, cursor: "pointer", padding: 0, fontFamily: "Inter, sans-serif" }}>← Back</button>
          <div style={{ marginTop: 10, fontSize: 11, color: muted, lineHeight: 1.5 }}>By submitting you agree to receive a call from a Saela {cityName} inspector.</div>
        </form>
      )}
    </div>
  );
}

// ============== Accordion ==============
function Accordion({ items, variant = "light" }) {
  const [open, setOpen] = useState(0);
  const isDark = variant === "dark";
  const fg = isDark ? "#F5F5EB" : "#26453D";
  const border = isDark ? "rgba(245,245,235,0.16)" : "rgba(38,69,61,0.14)";
  const muted = isDark ? "rgba(245,245,235,0.72)" : "rgba(38,69,61,0.72)";
  return (
    <div style={{ borderTop: "1px solid " + border }}>
      {items.map((item, i) => {
        var isOpen = open === i;
        return (
          <div key={i} style={{ borderBottom: "1px solid " + border }}>
            <button onClick={() => setOpen(isOpen ? -1 : i)}
              style={{ width: "100%", textAlign: "left", padding: "20px 0", background: "none", border: "none", cursor: "pointer", display: "flex", justifyContent: "space-between", alignItems: "center", color: fg, fontFamily: "Inter, sans-serif", fontSize: 16, fontWeight: 500, gap: 16 }}>
              <span>{item.q || item.title}</span>
              <span style={{ color: "#EA9230", fontSize: 22, fontWeight: 300, lineHeight: 1, transition: "transform 200ms", transform: isOpen ? "rotate(45deg)" : "rotate(0)" }}>+</span>
            </button>
            {isOpen && <div style={{ paddingBottom: 22, color: muted, fontSize: 15, lineHeight: 1.6, maxWidth: 720 }}>{item.a || item.body}</div>}
          </div>
        );
      })}
    </div>
  );
}

// ============== Placeholder Image ==============
function UPlaceholder({ label, ratio = "16/9", tone = "sand" }) {
  var p = {
    sand: { bg: "#F5F5EB", stripe: "rgba(38,69,61,0.06)", text: "rgba(38,69,61,0.55)" },
    dark: { bg: "rgba(245,245,235,0.06)", stripe: "rgba(245,245,235,0.04)", text: "rgba(245,245,235,0.65)" },
  }[tone] || { bg: "#F5F5EB", stripe: "rgba(38,69,61,0.06)", text: "rgba(38,69,61,0.55)" };
  return (
    <div style={{ aspectRatio: ratio, background: "repeating-linear-gradient(135deg, " + p.bg + " 0 12px, " + p.stripe + " 12px 24px)", borderRadius: 10, display: "grid", placeItems: "center", color: p.text, fontFamily: "ui-monospace, monospace", fontSize: 11, letterSpacing: 0.5, textTransform: "uppercase", border: "1px solid rgba(38,69,61,0.06)" }}>{label}</div>
  );
}

// ============== Check mark (for comparison table) ==============
function Check({ on }) {
  if (on) return <span style={{ display: "inline-grid", placeItems: "center", width: 22, height: 22, borderRadius: "50%", background: "#26453D", color: "#EA9230", fontSize: 13, fontWeight: 700 }}>✓</span>;
  return <span style={{ display: "inline-block", width: 14, height: 1, background: "rgba(38,69,61,0.25)" }}></span>;
}

// ============== Footer ==============
function Footer() {
  return (
    <footer style={{ background: "#1c322d", color: "rgba(245,245,235,0.7)", padding: "48px 32px 32px", fontSize: 13, fontFamily: "Inter, sans-serif" }}>
      <div style={{ maxWidth: 1200, margin: "0 auto" }}>
        <div style={{ display: "flex", justifyContent: "space-between", alignItems: "flex-start", flexWrap: "wrap", gap: 32, paddingBottom: 32, borderBottom: "1px solid rgba(245,245,235,0.12)" }}>
          <div style={{ maxWidth: 320 }}>
            <SaelaLogo height={26} color="#F5F5EB" />
            <p style={{ marginTop: 14, fontSize: 13, lineHeight: 1.55, color: "rgba(245,245,235,0.55)" }}>
              A scientific approach to pest control. Certified inspectors, board-certified entomologists, Integrated Pest Management.
            </p>
          </div>
          <div style={{ display: "flex", gap: 56, flexWrap: "wrap" }}>
            <div>
              <div style={{ fontSize: 11, fontWeight: 600, letterSpacing: 1.2, textTransform: "uppercase", color: "rgba(245,245,235,0.5)", marginBottom: 10 }}>Utah office</div>
              <a href="tel:3854425212" style={{ color: "#F5F5EB", textDecoration: "none", fontFamily: "'DM Serif Display', serif", fontSize: 18 }}>(385) 442-5212</a>
            </div>
            <div>
              <div style={{ fontSize: 11, fontWeight: 600, letterSpacing: 1.2, textTransform: "uppercase", color: "rgba(245,245,235,0.5)", marginBottom: 10 }}>Company</div>
              <div style={{ display: "flex", flexDirection: "column", gap: 6 }}>
                <a href="#" style={{ color: "inherit", textDecoration: "none" }}>About</a>
                <a href="/utah-pest-control/" style={{ color: "inherit", textDecoration: "none" }}>All locations</a>
                <a href="#" style={{ color: "inherit", textDecoration: "none" }}>Careers</a>
              </div>
            </div>
            <div>
              <div style={{ fontSize: 11, fontWeight: 600, letterSpacing: 1.2, textTransform: "uppercase", color: "rgba(245,245,235,0.5)", marginBottom: 10 }}>Legal</div>
              <div style={{ display: "flex", flexDirection: "column", gap: 6 }}>
                <a href="#" style={{ color: "inherit", textDecoration: "none" }}>Privacy</a>
                <a href="#" style={{ color: "inherit", textDecoration: "none" }}>Terms</a>
                <a href="#" style={{ color: "inherit", textDecoration: "none" }}>Accessibility</a>
              </div>
            </div>
          </div>
        </div>
        <div style={{ paddingTop: 24, fontSize: 12, color: "rgba(245,245,235,0.5)" }}>
          © {new Date().getFullYear()} Saela Pest Control · Utah
        </div>
      </div>
    </footer>
  );
}
