// ─────────────────────────────────────────────────────────
//  App — orchestrates layout + scroll-linked section transitions
//        Single-line topnav with live Jammu time
// ─────────────────────────────────────────────────────────

const { useEffect: useEffectA, useRef: useRefA, useState: useStateA } = React;

function SoundToggle() {
  const [on, setOn] = useStateA(false);
  return (
    <button
      id="sound-toggle"
      className={`magnetic-trigger ${on ? 'on' : ''}`}
      onClick={() => setOn(window.__ambient.toggle())}
      aria-label="Toggle ambient sound"
    >
      <span className="ico">{on ? '◉' : '○'}</span>
      <span>{on ? 'Sound' : 'Sound'}</span>
    </button>
  );
}

function LiveTime() {
  const [now, setNow] = useStateA(new Date());
  useEffectA(() => {
    const id = setInterval(() => setNow(new Date()), 30000);
    return () => clearInterval(id);
  }, []);
  const time = now.toLocaleTimeString('en-IN', {
    hour: '2-digit', minute: '2-digit', hour12: false, timeZone: 'Asia/Kolkata',
  });
  return (
    <span className="topnav-time">
      <span className="dot"/>
      <span>Jammu</span>
      <span className="v">{time}</span>
    </span>
  );
}

function TopNav() {
  return (
    <div className="topnav">
      <a href="#top" className="brand magnetic-trigger" aria-label="WebBrandify · Home">
        <span className="brand-mark">
          <svg viewBox="0 0 40 40" fill="none" aria-hidden="true">
            <g stroke="currentColor" strokeWidth="1.6" strokeLinecap="round" strokeLinejoin="round">
              <path d="M 12 4 L 4 4 L 4 12"/>
              <path d="M 28 4 L 36 4 L 36 12"/>
              <path d="M 4 28 L 4 36 L 12 36"/>
              <path d="M 36 28 L 36 36 L 28 36"/>
            </g>
            <circle cx="20" cy="20" r="7" fill="none" stroke="#00F2FE" strokeWidth="0.8" opacity="0.35"/>
            <circle cx="20" cy="20" r="3.2" fill="#00F2FE" className="brand-pulse"/>
          </svg>
        </span>
        <span className="brand-text">
          <span className="brand-name">webbrandify</span>
          <span className="brand-sub">Rohit Sharma · v.04</span>
        </span>
      </a>
      <nav>
        <a href="#audit"     className="magnetic-trigger">Audit</a>
        <a href="#works"     className="magnetic-trigger">Work</a>
        <a href="#tenders"   className="magnetic-trigger">Tenders</a>
        <a href="#founder"   className="magnetic-trigger">Founder</a>
        <a href="#product"   className="magnetic-trigger" style={{color:'var(--accent)'}}>Product↗</a>
        <a href="#pricing"   className="magnetic-trigger">Pricing</a>
        <a href="#faq"       className="magnetic-trigger">FAQ</a>
        <a href="#booking"   className="magnetic-trigger">Book</a>
      </nav>
      <div className="topnav-meta">
        <LiveTime/>
        <SoundToggle/>
        <div className="status">
          <span className="dot"/>
          <span>2 slots Q3</span>
        </div>
      </div>
    </div>
  );
}

function Footer() {
  return (
    <footer className="footer" id="contact">
      <div className="footer-grid">
        <div>
          <span className="kicker">§Outro · The handoff</span>
          <h2>
            Independent studio.<br/>
            <em>Enterprise output.</em>
          </h2>
          <p style={{
            marginTop:24, color:'var(--muted)', fontSize:17, lineHeight:1.55, maxWidth:'48ch',
          }}>
            We take two engagements per quarter. The shortlist closes when it closes.
            If you&rsquo;re building something the world should pay attention to — let&rsquo;s talk.
          </p>
        </div>

        <div className="footer-col">
          <h5>Studio</h5>
          <ul>
            <li><a className="magnetic-trigger" href="#case">Selected work</a></li>
            <li><a className="magnetic-trigger" href="#tenders">India &amp; global tenders</a></li>
            <li><a className="magnetic-trigger" href="#work">Capabilities</a></li>
            <li><a className="magnetic-trigger" href="#sandbox">Live sandbox</a></li>
            <li><a className="magnetic-trigger" href="#audit">Free site audit</a></li>
            <li><a className="magnetic-trigger" href="legal/privacy.html">Privacy policy</a></li>
            <li><a className="magnetic-trigger" href="legal/terms.html">Terms of service</a></li>
            <li><a className="magnetic-trigger" href="legal/cookies.html">Cookie policy</a></li>
            <li><a className="magnetic-trigger" href="legal/refund.html">Refund &amp; cancellation</a></li>
          </ul>
        </div>

        <div className="footer-col">
          <h5>Direct</h5>
          <ul className="contact-list">
            <li>
              <a className="magnetic-trigger" href="mailto:rohit@webbrandify.com">
                <svg viewBox="0 0 24 24" fill="none" stroke="currentColor" strokeWidth="1.6" strokeLinecap="round" strokeLinejoin="round">
                  <rect x="3" y="5" width="18" height="14" rx="2"/>
                  <path d="M3 7l9 6 9-6"/>
                </svg>
                <span>rohit@webbrandify.com</span>
              </a>
            </li>
            <li>
              <a className="magnetic-trigger" href="tel:+919186057407">
                <svg viewBox="0 0 24 24" fill="none" stroke="currentColor" strokeWidth="1.6" strokeLinecap="round" strokeLinejoin="round">
                  <path d="M22 16.92v3a2 2 0 01-2.18 2 19.79 19.79 0 01-8.63-3.07 19.5 19.5 0 01-6-6 19.79 19.79 0 01-3.07-8.67A2 2 0 014.11 2h3a2 2 0 012 1.72c.13.96.36 1.9.7 2.81a2 2 0 01-.45 2.11L8.09 9.91a16 16 0 006 6l1.27-1.27a2 2 0 012.11-.45c.91.34 1.85.57 2.81.7A2 2 0 0122 16.92z"/>
                </svg>
                <span>+91 91860 57407</span>
              </a>
            </li>
            <li>
              <a className="magnetic-trigger" href="https://www.linkedin.com/in/rohit-sharma-webbrandify" target="_blank" rel="noopener">
                <svg viewBox="0 0 24 24" fill="currentColor">
                  <path d="M19 3a2 2 0 012 2v14a2 2 0 01-2 2H5a2 2 0 01-2-2V5a2 2 0 012-2h14zM8 17v-7H6v7h2zM7 8.5A1.25 1.25 0 107 6a1.25 1.25 0 000 2.5zM18 17v-4.2c0-2-1.1-2.9-2.5-2.9-1.2 0-1.7.6-2 1V10H11.5v7H14v-3.6c0-1 .2-1.9 1.4-1.9s1.2 1.1 1.2 2V17H18z"/>
                </svg>
                <span>@rohit-sharma</span>
              </a>
            </li>
            <li>
              <a className="magnetic-trigger" href="https://github.com/webbrandify" target="_blank" rel="noopener">
                <svg viewBox="0 0 24 24" fill="currentColor">
                  <path d="M12 .5C5.65.5.5 5.65.5 12c0 5.08 3.29 9.39 7.86 10.91.58.11.79-.25.79-.56v-2c-3.2.7-3.87-1.37-3.87-1.37-.52-1.34-1.28-1.7-1.28-1.7-1.05-.72.08-.7.08-.7 1.16.08 1.77 1.19 1.77 1.19 1.03 1.77 2.7 1.26 3.36.96.1-.75.4-1.26.73-1.55-2.55-.29-5.24-1.28-5.24-5.69 0-1.26.45-2.29 1.19-3.1-.12-.29-.52-1.47.11-3.07 0 0 .97-.31 3.18 1.18a11 11 0 015.79 0c2.21-1.49 3.18-1.18 3.18-1.18.63 1.6.23 2.78.11 3.07.74.81 1.19 1.84 1.19 3.1 0 4.42-2.7 5.39-5.27 5.68.41.36.78 1.06.78 2.15v3.18c0 .31.21.68.8.56C20.21 21.38 23.5 17.07 23.5 12c0-6.35-5.15-11.5-11.5-11.5z"/>
                </svg>
                <span>@webbrandify</span>
              </a>
            </li>
            <li>
              <a className="magnetic-trigger" href="https://twitter.com/webbrandify" target="_blank" rel="noopener">
                <svg viewBox="0 0 24 24" fill="currentColor">
                  <path d="M18.244 2.25h3.308l-7.227 8.26 8.502 11.24H16.17l-5.214-6.817L4.99 21.75H1.68l7.73-8.835L1.254 2.25H8.08l4.713 6.231zm-1.161 17.52h1.833L7.084 4.126H5.117l11.966 15.644z"/>
                </svg>
                <span>@webbrandify</span>
              </a>
            </li>
            <li>
              <a className="magnetic-trigger" href="#booking">
                <svg viewBox="0 0 24 24" fill="none" stroke="currentColor" strokeWidth="1.6" strokeLinecap="round" strokeLinejoin="round">
                  <rect x="3" y="4" width="18" height="18" rx="2"/>
                  <path d="M16 2v4M8 2v4M3 10h18"/>
                </svg>
                <span>Book walkthrough →</span>
              </a>
            </li>
          </ul>
        </div>
      </div>

      <div className="footer-bottom">
        <span>© WebBrandify 2026 · Jammu · India · Worldwide</span>
        <span className="footer-mark">
          <span className="rs-orb" aria-hidden="true">
            <svg viewBox="0 0 48 48" fill="none">
              <defs>
                <linearGradient id="rs-foot-grad" x1="0" y1="0" x2="1" y2="1">
                  <stop offset="0%"   stopColor="#00F2FE"/>
                  <stop offset="100%" stopColor="#5EEAEF"/>
                </linearGradient>
              </defs>
              <ellipse cx="24" cy="24" rx="16" ry="9" fill="#00F2FE" opacity="0.1"/>
              <text x="8" y="34"
                fontFamily="Instrument Serif, Times New Roman, serif"
                fontStyle="italic" fontSize="30" fontWeight="400"
                fill="url(#rs-foot-grad)">R</text>
              <circle cx="25.5" cy="36" r="1.1" fill="#00F2FE"/>
              <text x="28" y="35"
                fontFamily="Instrument Serif, Times New Roman, serif"
                fontStyle="italic" fontSize="22" fontWeight="400"
                fill="url(#rs-foot-grad)" opacity="0.92">s</text>
            </svg>
          </span>
          <span>
            Founder · Rohit Sharma ·{' '}
            <a className="magnetic-trigger fb-link" href="https://www.webbrandify.com" target="_blank" rel="noopener">webbrandify.com</a>
            {' '}·{' '}
            <a className="magnetic-trigger fb-link" href="https://www.jkrealtors.org" target="_blank" rel="noopener">JK Realtors</a>
          </span>
        </span>
      </div>
    </footer>
  );
}

function scrollToHash(offset = 80) {
  const hash = window.location.hash;
  if (!hash || hash === '#top' || hash === '#') return;
  const target = document.querySelector(hash);
  if (!target) return;
  const top = target.getBoundingClientRect().top + window.scrollY - offset;
  window.scrollTo({ top: Math.max(0, top), behavior: 'instant' });
}
window.__scrollToHash = scrollToHash;

function App() {
  const stackRef = useRefA(null);

  // Land on #booking / #audit etc. when opened via direct link (browser does not scroll SPA)
  useEffectA(() => {
    scrollToHash();
    const delays = [300, 1200, 3500, 6000].map((ms) => setTimeout(scrollToHash, ms));
    const onHash = () => scrollToHash();
    window.addEventListener('hashchange', onHash);
    return () => {
      delays.forEach(clearTimeout);
      window.removeEventListener('hashchange', onHash);
    };
  }, []);

  // Smooth view-transition scrolling on nav anchor clicks
  useEffectA(() => {
    const onClick = (e) => {
      const a = e.target.closest('a[href^="#"]');
      if (!a) return;
      const href = a.getAttribute('href');
      if (href === '#' || href === '#top') {
        e.preventDefault();
        window.__withTransition(() => window.scrollTo({ top: 0, behavior: 'smooth' }));
        return;
      }
      const target = document.querySelector(href);
      if (target) {
        e.preventDefault();
        const top = target.getBoundingClientRect().top + window.scrollY - 80;
        window.__withTransition(() => window.scrollTo({ top, behavior: 'smooth' }));
      }
    };
    document.addEventListener('click', onClick);
    return () => document.removeEventListener('click', onClick);
  }, []);

  // Scroll-linked section transitions
  useEffectA(() => {
    const stack = stackRef.current;
    if (!stack) return;
    const sections = Array.from(stack.querySelectorAll('.section'));

    const onScroll = () => {
      const vh = window.innerHeight;
      sections.forEach((sec, i) => {
        // Blur/opacity on parent breaks Cal iframe + booking UI
        if (sec.id === 'booking') {
          sec.style.transform = '';
          sec.style.filter = '';
          sec.style.opacity = '';
          return;
        }
        const next = sections[i + 1];
        if (!next) {
          sec.style.transform = '';
          sec.style.filter = '';
          sec.style.opacity = '';
          return;
        }
        const nextRect = next.getBoundingClientRect();
        // Trigger blur LATER: only when next section is 70% into the viewport
        // (i.e. its top is within 30% from the top of screen)
        const startAt = vh * 0.3;
        const endAt   = -vh * 0.1;
        const progress = Math.max(0, Math.min(1,
          (startAt - nextRect.top) / (startAt - endAt)
        ));
        const scale  = 1 - progress * 0.05;       // 1 → 0.95
        const blur   = progress * 6;              // 0 → 6px (was 8)
        const dim    = progress * 0.45;
        const ty     = -progress * 32;
        sec.style.transform = `translate3d(0, ${ty}px, 0) scale(${scale})`;
        sec.style.filter    = `blur(${blur}px) brightness(${1 - dim * 0.35})`;
        sec.style.opacity   = String(1 - dim * 0.6);
      });
    };
    onScroll();
    window.addEventListener('scroll', onScroll, { passive: true });
    window.addEventListener('resize', onScroll);
    return () => {
      window.removeEventListener('scroll', onScroll);
      window.removeEventListener('resize', onScroll);
    };
  }, []);

  return (
    <>
      <TopNav/>
      <main ref={stackRef} id="top">
        <Hero/>
        <Manifesto/>
        <Recognition/>
        <div id="audit"><AISiteAudit/></div>
        <div id="work"><Bento/></div>
        <div id="works"><Works/></div>
        <div id="case"><CaseStudy/></div>
        <div id="tenders"><Tenders/></div>
        <div id="founder"><FounderDossier/></div>
        <Process/>
        <div id="sandbox"><Playground/></div>
        <div id="product"><SaasProduct/></div>
        <div id="pricing"><Pricing/></div>
        <div id="writing"><Writing/></div>
        <FAQ/>
        <div id="engage"><LeadForm/></div>
        <Booking/>
      </main>
      <Footer/>
      <ChatBot/>
    </>
  );
}

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