function App() {
  const [tweaks] = React.useState(window.BUZYBEE_TWEAKS || {});
  const [open, setOpen] = React.useState(false);
  const [privacyOpen, setPrivacyOpen] = React.useState(false);

  const onCTA = () => {
    if (window.fbq) fbq('track', 'Contact');
    setOpen(true);
  };

  React.useEffect(() => {
    const handler = () => { if (window.fbq) fbq('track', 'Contact'); setOpen(true); };
    document.addEventListener('open-estimate', handler);
    return () => document.removeEventListener('open-estimate', handler);
  }, []);

  React.useEffect(() => {
    const handler = () => setPrivacyOpen(true);
    document.addEventListener('open-privacy', handler);
    return () => document.removeEventListener('open-privacy', handler);
  }, []);

  React.useEffect(() => {
    if (!open && !privacyOpen) return;
    const scrollY = window.scrollY;
    const prevOverflow = document.body.style.overflow;
    const prevPos = document.body.style.position;
    const prevTop = document.body.style.top;
    const prevWidth = document.body.style.width;
    document.body.style.position = 'fixed';
    document.body.style.top = `-${scrollY}px`;
    document.body.style.width = '100%';
    document.body.style.overflow = 'hidden';
    return () => {
      document.body.style.overflow = prevOverflow;
      document.body.style.position = prevPos;
      document.body.style.top = prevTop;
      document.body.style.width = prevWidth;
      window.scrollTo(0, scrollY);
    };
  }, [open, privacyOpen]);

  return (
    <>
      <Hero onCTA={onCTA} tweaks={tweaks} />
      <Footer />
      <div className="mobile-cta">
        <button className="btn btn-primary btn-lg mobile-cta-main" onClick={onCTA} style={{width:'100%'}}>
          {tweaks.formCta || "Get Free Estimate & Offer"} <Ic.arrow />
        </button>
      </div>
      <EstimateModal
        open={open}
        onClose={() => setOpen(false)}
        ctaLabel={tweaks.formCta}
        offerPage={tweaks.offer || 'unknown'}
      />
      <PrivacyModal open={privacyOpen} onClose={() => setPrivacyOpen(false)} />
    </>
  );
}

const root = ReactDOM.createRoot(document.getElementById('root'));
root.render(<App />);
