// Shared site chrome — Nav, Footer, ThemeToggle, Reveal observer, Tweaks panel host.

const { useState, useEffect, useRef, useCallback, useMemo } = React;

// ---------- Theme ----------
window.useTheme = function useTheme() {
  const [theme, setTheme] = useState(() => {
    try { return localStorage.getItem('tc-theme') || 'dark'; } catch { return 'dark'; }
  });
  useEffect(() => {
    document.documentElement.setAttribute('data-theme', theme);
    try { localStorage.setItem('tc-theme', theme); } catch {}
  }, [theme]);
  const toggle = useCallback(() => setTheme(t => t === 'dark' ? 'light' : 'dark'), []);
  return { theme, setTheme, toggle };
};

// ---------- Reveal on scroll ----------
// Progressive-enhancement fade-in. Content is visible by default (opacity:1 in
// .reveal) so nothing is ever hidden if JS or the observer misbehaves — the
// observer just adds a tiny transform-ease as sections enter the viewport.
// Re-scans periodically so sections that mount after first paint still animate.
window.useReveal = function useReveal() {
  useEffect(() => {
    const reduced = window.matchMedia && window.matchMedia('(prefers-reduced-motion: reduce)').matches;
    if (reduced) {
      document.querySelectorAll('.reveal').forEach(e => e.classList.add('in'));
      return;
    }
    if (!('IntersectionObserver' in window)) {
      document.querySelectorAll('.reveal').forEach(e => e.classList.add('in'));
      return;
    }
    // Opt-in the CSS that allows the choreography; without this flag content
    // remains visible by default (i.e., everything still reads if JS dies).
    document.documentElement.setAttribute('data-reveal-ready', '1');
    // threshold:0 + rootMargin bottom trigger = fires the moment any part of
    // the element crosses the viewport bottom. Works for short and tall sections.
    const io = new IntersectionObserver((entries) => {
      entries.forEach(e => {
        if (e.isIntersecting) { e.target.classList.add('in'); io.unobserve(e.target); }
      });
    }, { threshold: 0, rootMargin: '0px 0px -8% 0px' });
    const observeAll = () => {
      document.querySelectorAll('.reveal:not(.in)').forEach(e => {
        if (!e.dataset.ccObserved) {
          e.dataset.ccObserved = '1';
          io.observe(e);
        }
      });
    };
    observeAll();
    // Catch late-rendered sections
    const id1 = setTimeout(observeAll, 200);
    const id2 = setTimeout(observeAll, 800);
    // Safety net: if something ever goes >2s past load without revealing, force all in.
    const safety = setTimeout(() => {
      document.querySelectorAll('.reveal:not(.in)').forEach(e => {
        const r = e.getBoundingClientRect();
        if (r.top < window.innerHeight) e.classList.add('in');
      });
    }, 2000);
    return () => {
      io.disconnect();
      clearTimeout(id1); clearTimeout(id2); clearTimeout(safety);
    };
  }, []);
};

// ---------- Nav ----------
window.Nav = function Nav({ current = 'home' }) {
  const { theme, toggle } = window.useTheme();
  const [scrolled, setScrolled] = useState(false);
  const [mobileOpen, setMobileOpen] = useState(false);
  const TCMark = window.TCMark;

  useEffect(() => {
    const onScroll = () => setScrolled(window.scrollY > 8);
    onScroll();
    window.addEventListener('scroll', onScroll, { passive: true });
    return () => window.removeEventListener('scroll', onScroll);
  }, []);

  const links = [
    { key: 'solutions', label: 'Solutions', href: 'solutions.html' },
    { key: 'cycle', label: 'The Cycle', href: 'the-cycle.html' },
    { key: 'partners', label: 'Partners', href: 'partners.html' },
    { key: 'about', label: 'About', href: 'about.html' },
    { key: 'results', label: 'Results', href: 'results.html' },
    { key: 'security', label: 'Security', href: 'security.html' },
    { key: 'contact', label: 'Contact', href: 'contact.html' },
  ];

  return (
    <>
      <a href="#main" className="skip-link">Skip to main content</a>
      {window.ScrollProgress ? <window.ScrollProgress /> : null}
      {window.CustomCursor ? <window.CustomCursor /> : null}
      {window.SectionNav ? <window.SectionNav /> : null}
    <header className={`nav ${scrolled ? 'nav-scrolled' : ''}`}>
      <div className="nav-inner">
        <a href="index.html" className="nav-logo">
          <TCMark size={36} alt="" />
          <span className="nav-logo-text">Technology Cycle</span>
        </a>
        <nav className="nav-links" aria-label="Primary">
          {links.map(l => (
            <a
              key={l.key}
              href={l.href}
              className={`nav-link ${current === l.key ? 'nav-link-active' : ''}`}
            >
              {l.label}
            </a>
          ))}
        </nav>
        <div className="nav-actions">
          <button
            onClick={toggle}
            className="theme-toggle"
            aria-label={`Switch to ${theme === 'dark' ? 'light' : 'dark'} theme`}
            title={`${theme === 'dark' ? 'Light' : 'Dark'} mode`}
          >
            <ThemeIcon theme={theme} />
          </button>
          <a href="the-cycle.html" className="nav-link-ghost">
            See how we work <span aria-hidden="true">→</span>
          </a>
          <a href="contact.html" className="btn btn-primary btn-sm nav-cta">
            Book Strategy Call
            <span aria-hidden="true" style={{ opacity: 0.7 }}>↗</span>
          </a>
          <button
            className="nav-mobile-btn"
            aria-label="Menu"
            onClick={() => setMobileOpen(o => !o)}
          >
            <span className={`hamburger ${mobileOpen ? 'open' : ''}`}>
              <span></span><span></span>
            </span>
          </button>
        </div>
      </div>
      {mobileOpen && (
        <div className="nav-mobile-menu">
          {links.map(l => (
            <a key={l.key} href={l.href} className="nav-mobile-link">{l.label}</a>
          ))}
          <a href="contact.html" className="btn btn-primary" style={{ marginTop: 16 }}>Book Strategy Call</a>
        </div>
      )}
    </header>
    </>
  );
};

function ThemeIcon({ theme }) {
  if (theme === 'dark') {
    return (
      <svg width="16" height="16" viewBox="0 0 16 16" fill="none">
        <path d="M13 9.5A5 5 0 1 1 6.5 3a4 4 0 0 0 6.5 6.5Z" stroke="currentColor" strokeWidth="1.3" strokeLinejoin="round"/>
      </svg>
    );
  }
  return (
    <svg width="16" height="16" viewBox="0 0 16 16" fill="none">
      <circle cx="8" cy="8" r="3" stroke="currentColor" strokeWidth="1.3"/>
      <path d="M8 1.5v2M8 12.5v2M1.5 8h2M12.5 8h2M3.3 3.3l1.4 1.4M11.3 11.3l1.4 1.4M3.3 12.7l1.4-1.4M11.3 4.7l1.4-1.4" stroke="currentColor" strokeWidth="1.3" strokeLinecap="round"/>
    </svg>
  );
}

// ---------- Footer ----------
window.Footer = function Footer() {
  const TCLogotypeFull = window.TCLogotypeFull;
  return (
    <footer className="footer">
      <div className="container">
        <div className="footer-grid">
          <div className="footer-brand">
            <div style={{ marginBottom: 20 }}>
              <TCLogotypeFull size={44} />
            </div>
            <p className="footer-tagline">
              Technology operations and AI implementation. Since 2016.
            </p>
            <div className="footer-contact">
              <a href="tel:8557483030" className="footer-contact-line">
                <span className="label-mono">Call</span> 855-748-3030
              </a>
              <a href="mailto:info@technologycycle.com" className="footer-contact-line">
                <span className="label-mono">Email</span> info@technologycycle.com
              </a>
              <div className="footer-contact-line">
                <span className="label-mono">HQ</span> Tampa, FL · Serving clients globally
              </div>
            </div>
          </div>
          <div className="footer-col">
            <div className="label-mono footer-col-title">Company</div>
            <a href="about.html">About</a>
            <a href="results.html">Results</a>
            <a href="partners.html">Partners</a>
            <a href="contact.html">Contact</a>
          </div>
          <div className="footer-col">
            <div className="label-mono footer-col-title">Practice</div>
            <a href="solutions.html">Solutions</a>
            <a href="the-cycle.html">The Cycle</a>
            <a href="security.html">Security</a>
          </div>
          <div className="footer-col">
            <div className="label-mono footer-col-title">Clients</div>
            <a href="https://tcc.billing.sbs" target="_blank" rel="noopener">Client Portal ↗</a>
            <a href="mailto:help@technologycycle.com">Support</a>
            <a href="status.html">Status</a>
          </div>
          <div className="footer-col">
            <div className="label-mono footer-col-title">Connect</div>
            <a href="https://www.linkedin.com/company/technology-cycle-corporation/" target="_blank" rel="noopener" className="footer-social" aria-label="LinkedIn">
              <svg width="14" height="14" viewBox="0 0 24 24" fill="currentColor" aria-hidden="true"><path d="M20.447 20.452h-3.554v-5.569c0-1.328-.027-3.037-1.852-3.037-1.853 0-2.136 1.445-2.136 2.939v5.667H9.351V9h3.414v1.561h.046c.477-.9 1.637-1.85 3.37-1.85 3.601 0 4.267 2.37 4.267 5.455v6.286zM5.337 7.433c-1.144 0-2.063-.926-2.063-2.065 0-1.138.92-2.063 2.063-2.063 1.14 0 2.064.925 2.064 2.063 0 1.139-.925 2.065-2.064 2.065zm1.782 13.019H3.555V9h3.564v11.452zM22.225 0H1.771C.792 0 0 .774 0 1.729v20.542C0 23.227.792 24 1.771 24h20.451C23.2 24 24 23.227 24 22.271V1.729C24 .774 23.2 0 22.222 0h.003z"/></svg>
              <span>LinkedIn</span>
              <span className="footer-social-arrow" aria-hidden="true">↗</span>
            </a>
            <a href="https://x.com/technologycycle" target="_blank" rel="noopener" className="footer-social" aria-label="X">
              <svg width="13" height="13" viewBox="0 0 24 24" fill="currentColor" aria-hidden="true"><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.117z"/></svg>
              <span>X</span>
              <span className="footer-social-arrow" aria-hidden="true">↗</span>
            </a>
          </div>
        </div>
        <div className="footer-base">
          <div className="footer-base-left">
            <span>© 2026 Technology Cycle Corporation</span>
            <span className="footer-dot">·</span>
            <a href="privacy-policy.html">Privacy</a>
            <span className="footer-dot">·</span>
            <a href="terms.html">Terms</a>
          </div>
          <a href="status.html" className="footer-base-right mono footer-status-link">
            <span className="tag-dot" style={{ marginRight: 8 }} aria-hidden="true"></span>
            All systems operational
          </a>
        </div>
      </div>
    </footer>
  );
};
