function Hero({ headline, underlined, subhead, heroBg, tweaks }) {
  // headline: "Building Your Backyard Into An Outdoor Masterpiece"
  // underlined: the word(s) to underline with the gold accent
  const renderHeadline = () => {
    if (!underlined) return headline;
    const idx = headline.indexOf(underlined);
    if (idx === -1) return headline;
    const before = headline.slice(0, idx);
    const after = headline.slice(idx + underlined.length);
    return (
      <>
        {before}
        <span className="vp-underlined">{underlined}</span>
        {after}
      </>
    );
  };

  return (
    <section className="vp-hero" style={{ "--hero-bg": "url('assets/deck-hero.jpg')" }}>
      {/* Diagonal gold slash */}
      <div className="vp-slash" />
      <div className="vp-hero-scrim" />

      {/* Sunburst — rays fan toward the quote card */}
      <svg className="vp-sunburst" viewBox="0 0 600 600" aria-hidden="true">
        <defs>
          <radialGradient id="sunGrad" cx="0%" cy="100%" r="75%">
            <stop offset="0%" stopColor="rgba(201,169,110,0.18)"/>
            <stop offset="50%" stopColor="rgba(201,169,110,0.03)"/>
            <stop offset="100%" stopColor="rgba(201,169,110,0)"/>
          </radialGradient>
        </defs>
        <circle cx="0" cy="600" r="560" fill="url(#sunGrad)" />
        {Array.from({ length: 10 }).map((_, i) => {
          // rays fan from lower-left origin, sweeping from -8° (rightward) up to -78° (almost straight up)
          const t = i / 9;
          const angle = (-8 - t * 70) * Math.PI / 180;
          const len = 360 + (i % 2) * 30;
          const opacity = 0.04 + (1 - Math.abs(t - 0.5) * 1.4) * 0.06;
          return (
            <line
              key={i}
              x1="0" y1="600"
              x2={0 + len * Math.cos(angle)}
              y2={600 + len * Math.sin(angle)}
              stroke={`rgba(201,169,110,${Math.max(0.03, opacity).toFixed(3)})`}
              strokeWidth="0.8"
              strokeLinecap="round"
            />
          );
        })}
      </svg>

      <div className="vp-hero-inner">
        <div className="vp-hero-text">
          <div className="vp-eyebrow vp-anim vp-anim-1">Custom Decks · Patios · Outdoor Kitchens</div>
          <h1 className="vp-headline vp-anim vp-anim-2">{renderHeadline()}</h1>
          <p className="vp-subhead vp-anim vp-anim-3">{subhead}</p>
        </div>

        <div className="vp-hero-quote vp-anim vp-anim-5" id="quote">
          <div className="vp-hero-quote-bg" aria-hidden="true" />
          <div className="vp-hero-quote-overlay" aria-hidden="true" />
          <QuoteBar />
        </div>

        <div className="vp-trust-card vp-anim vp-anim-4">
          <div className="vp-trust-icons">
            <span className="badge" style={{ background: "#1877F2" }}><Icon.Facebook size={16} /></span>
            <span className="badge" style={{ background: "white", color: "#4285F4" }}><Icon.Google size={16} /></span>
            <span className="badge" style={{ background: "#D32323" }}><Icon.Yelp size={16} /></span>
            <span className="badge" style={{ background: "#0F4D92", fontSize: 10 }}>BBB</span>
            <span className="badge" style={{ background: "#1A7F3C", fontSize: 10 }}>Angi</span>
          </div>
          <div className="vp-trust-text">
            <div className="row1">Trusted on Google, Facebook, Yelp, BBB &amp; Angi</div>
            <div className="row2">
              <span className="stars">
                <Icon.Star size={11} /><Icon.Star size={11} /><Icon.Star size={11} /><Icon.Star size={11} /><Icon.Star size={11} />
                &nbsp;4.9 Rating
              </span>
              <span className="pipe">|</span>
              <span>A+ BBB</span>
              <span className="pipe">|</span>
              <span>Based on 100+ reviews</span>
            </div>
          </div>
        </div>
      </div>
    </section>
  );
}

function QuoteBar() {
  const [form, setForm] = React.useState({ firstName: "", lastName: "", email: "", phone: "", help: "" });
  const [smsTransactional, setSmsTransactional] = React.useState(false);
  const [smsMarketing, setSmsMarketing] = React.useState(false);
  const [errors, setErrors] = React.useState({});
  const [submitted, setSubmitted] = React.useState(false);

  const EMAIL_RE = /^[^\s@]+@[^\s@]+\.[a-zA-Z]{2,}$/;

  const formatPhone = (raw) => {
    const digits = (raw || "").replace(/\D/g, "").slice(0, 10);
    if (digits.length < 4) return digits;
    if (digits.length < 7) return `(${digits.slice(0, 3)}) ${digits.slice(3)}`;
    return `(${digits.slice(0, 3)}) ${digits.slice(3, 6)}-${digits.slice(6)}`;
  };
  const phoneDigits = (raw) => (raw || "").replace(/\D/g, "");

  const onChange = (k) => (e) => {
    let value = e.target.value;
    if (k === "phone") value = formatPhone(value);
    setForm((f) => ({ ...f, [k]: value }));
    if (errors[k]) setErrors((er) => ({ ...er, [k]: undefined }));
  };

  const validate = () => {
    const er = {};
    if (!form.firstName.trim()) er.firstName = "Required";
    if (!form.lastName.trim()) er.lastName = "Required";
    if (!form.email.trim()) er.email = "Required";
    else if (!EMAIL_RE.test(form.email.trim())) er.email = "Enter a valid email address";
    const d = phoneDigits(form.phone);
    if (!d) er.phone = "Required";
    else if (d.length !== 10) er.phone = "Enter a 10-digit phone number";
    else if (!/^[2-9]/.test(d)) er.phone = "Enter a valid phone number";
    return er;
  };

  const onSubmit = (e) => {
    e.preventDefault();
    const er = validate();
    if (Object.keys(er).length) {
      setErrors(er);
      return;
    }
    const fullName = `${form.firstName} ${form.lastName}`.trim();
    const digits = phoneDigits(form.phone);
    const payload = {
      first_name: form.firstName.trim(),
      last_name: form.lastName.trim(),
      name: fullName,
      full_name: fullName,
      email: form.email.trim(),
      phone: form.phone,
      phone_e164: `+1${digits}`,
      phone_digits: digits,
      message: form.help.trim(),
      sms_consent_transactional: smsTransactional,
      sms_consent_marketing: smsMarketing,
      sms_consent_text: "I consent to receive [type] messages from Victory Pro Deck Builders at the phone number provided. Message frequency may vary. Message and data rates may apply. Reply HELP for help or STOP to opt-out.",
      consent_timestamp: new Date().toISOString(),
      source: "victoryprodeckbuilders.com, Hero Quote Form",
      page_url: typeof window !== "undefined" ? window.location.href : "",
      submitted_at: new Date().toISOString(),
    };
    try {
      fetch("https://services.leadconnectorhq.com/hooks/wFKub3WIwDYc1MGsKWHi/webhook-trigger/5a75157b-3bc0-407a-ba92-dd972552de6e", {
        method: "POST",
        headers: { "Content-Type": "application/json" },
        body: JSON.stringify(payload),
        keepalive: true,
      }).catch(() => {});
    } catch (_) {}
    setSubmitted(true);
  };

  if (submitted) {
    const firstName = (form.firstName || "").trim();
    return (
      <div className="vp-quote-bar vp-quote-bar--success" role="status" aria-live="polite">
        <div className="vp-success-card">
          <div className="vp-success-check">
            <svg width="40" height="40" viewBox="0 0 24 24" fill="none" stroke="currentColor" strokeWidth="2.6" strokeLinecap="round" strokeLinejoin="round">
              <polyline points="20 6 9 17 4 12" />
            </svg>
          </div>
          <h2 className="vp-success-title">
            {firstName ? `Thanks, ${firstName}.` : "Thanks."} <span className="accent">We got your request.</span>
          </h2>
          <p className="vp-success-body">
            A member of the Victory Pro team will be in touch in <strong>just a couple minutes</strong>. Keep an eye on your phone, we'll send you a quick text shortly to continue the conversation.
          </p>
          <div className="vp-success-meta">
            <span><svg width="14" height="14" viewBox="0 0 24 24" fill="none" stroke="currentColor" strokeWidth="2.4" strokeLinecap="round" strokeLinejoin="round"><path d="M12 22s8-4 8-10V5l-8-3-8 3v7c0 6 8 10 8 10z"/></svg> Licensed &amp; Insured</span>
            <span className="dot-sep" />
            <span><svg width="14" height="14" viewBox="0 0 24 24" fill="currentColor"><polygon points="12 2 15.09 8.26 22 9.27 17 14.14 18.18 21.02 12 17.77 5.82 21.02 7 14.14 2 9.27 8.91 8.26 12 2"/></svg> 5 Stars on Google</span>
          </div>
        </div>
      </div>
    );
  }

  return (
    <form className="vp-quote-bar" onSubmit={onSubmit} noValidate>
      <div className="vp-quote-header">
        <span className="vp-quote-eyebrow">
          <span className="dot" />
          Free · No Obligation
        </span>
        <h2>Get A <span className="accent">Free Quote</span></h2>
        <p className="vp-quote-sub">Tell us about your project — we'll respond within 24 hours.</p>
      </div>
      <div className="vp-form-grid">
        <div className={`vp-input${errors.firstName ? " vp-input--error" : ""}`}>
          <span className="ic"><Icon.User size={14} /></span>
          <input type="text" placeholder="First Name" value={form.firstName} onChange={onChange("firstName")} autoComplete="given-name" aria-invalid={!!errors.firstName} />
        </div>
        <div className={`vp-input vp-input--no-ic${errors.lastName ? " vp-input--error" : ""}`}>
          <input type="text" placeholder="Last Name" value={form.lastName} onChange={onChange("lastName")} autoComplete="family-name" aria-invalid={!!errors.lastName} />
        </div>
        <div className={`vp-input full${errors.email ? " vp-input--error" : ""}`}>
          <span className="ic"><Icon.Mail size={14} /></span>
          <input type="email" inputMode="email" placeholder="Email Address" value={form.email} onChange={onChange("email")} autoComplete="email" aria-invalid={!!errors.email} />
        </div>
        {errors.email && <div className="vp-field-error full">{errors.email}</div>}
        <div className={`vp-input full${errors.phone ? " vp-input--error" : ""}`}>
          <span className="ic"><Icon.Phone size={14} /></span>
          <input type="tel" inputMode="numeric" placeholder="Phone Number" value={form.phone} onChange={onChange("phone")} autoComplete="tel" maxLength="14" aria-invalid={!!errors.phone} />
        </div>
        {errors.phone && <div className="vp-field-error full">{errors.phone}</div>}
        <div className="vp-input textarea full">
          <span className="ic"><Icon.MessageSquare size={14} /></span>
          <textarea rows="2" placeholder="How can we help? Tell us about your project…" value={form.help} onChange={onChange("help")} />
        </div>
        <div className="vp-sms-consent full">
          <label className="vp-sms-check">
            <input
              type="checkbox"
              checked={smsTransactional}
              onChange={(e) => setSmsTransactional(e.target.checked)}
            />
            <span className="vp-sms-box" aria-hidden="true" />
            <span className="vp-sms-text">
              I consent to receive transactional messages from Victory Pro Deck Builders at the phone number provided, such as appointment reminders, quote updates, and project notifications. Message frequency may vary. Message and data rates may apply. Reply HELP for help or STOP to opt-out.
            </span>
          </label>
          <label className="vp-sms-check">
            <input
              type="checkbox"
              checked={smsMarketing}
              onChange={(e) => setSmsMarketing(e.target.checked)}
            />
            <span className="vp-sms-box" aria-hidden="true" />
            <span className="vp-sms-text">
              I consent to receive marketing and promotional messages from Victory Pro Deck Builders at the phone number provided. Message frequency may vary. Message and data rates may apply. Reply HELP for help or STOP to opt-out.
            </span>
          </label>
        </div>
        <button type="submit" className="vp-submit-pill full">
          <span className="vp-submit-shine" />
          <span className="vp-submit-content">
            Submit My Request
            <Icon.ArrowRight size={16} />
          </span>
        </button>
        <p className="vp-consent full">
          By submitting this form, you agree to be contacted by Victory Pro Deck Builders about your project. SMS opt-in is not required to receive a quote and is not a condition of purchase. We do not share your mobile information with third parties or affiliates for marketing purposes. See our <a href="/privacy.html">Privacy Policy</a> and <a href="/terms.html">Terms of Service</a>.
        </p>
      </div>
    </form>
  );
}

window.Hero = Hero;
