// Top-level App: nav, sticky scroll, theme, footer, CTA, and animation styles

const { useState, useEffect, useRef } = React;

function App() {
  const [tweaks, setTweak] = window.useTweaks({ theme: "light" });
  const [waitlistOpen, setWaitlistOpen] = useState(false);
  const [scrolled, setScrolled] = useState(false);

  useEffect(() => {
    document.documentElement.setAttribute("data-theme", tweaks.theme);
  }, [tweaks.theme]);

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

  // Reveal-on-scroll
  useEffect(() => {
    const els = document.querySelectorAll(".reveal");
    const io = new IntersectionObserver(
      (entries) => {
        entries.forEach((e) => {
          if (e.isIntersecting) e.target.classList.add("in");
        });
      },
      { threshold: 0.12 },
    );
    els.forEach((el) => io.observe(el));
    return () => io.disconnect();
  }, []);

  // Analytics listener (mock)
  useEffect(() => {
    const handler = (e) => console.log("[analytics]", e.detail);
    window.addEventListener("analytics", handler);
    return () => window.removeEventListener("analytics", handler);
  }, []);

  const openWaitlist = () => {
    window.dispatchEvent(
      new CustomEvent("analytics", {
        detail: { event: "cta_click", source: "page" },
      }),
    );
    setWaitlistOpen(true);
  };

  return (
    <React.Fragment>
      <Nav
        scrolled={scrolled}
        onJoin={openWaitlist}
        theme={tweaks.theme}
        setTheme={(t) => setTweak("theme", t)}
      />
      <Hero onJoin={openWaitlist} />
      <Problem />
      <HowItWorks />
      <Features />
      <SocialProof />
      <Pricing onJoin={openWaitlist} />
      <FAQ />
      <FinalCTA onJoin={openWaitlist} />
      <Footer />
      <Waitlist open={waitlistOpen} onClose={() => setWaitlistOpen(false)} />
    </React.Fragment>
  );
}

function Nav({ scrolled, onJoin, theme, setTheme }) {
  const [mobileOpen, setMobileOpen] = React.useState(false);
  const solid = scrolled || mobileOpen;

  const navLinks = [
    { href: "#problem", label: "Problem" },
    { href: "#how", label: "How it works" },
    { href: "#features", label: "Features" },
    { href: "#pricing", label: "Pricing" },
    { href: "#faq", label: "FAQ" },
  ];

  return (
    <header
      style={{
        position: "fixed",
        top: 0,
        left: 0,
        right: 0,
        zIndex: 50,
        backdropFilter: solid ? "blur(12px) saturate(1.4)" : "none",
        background: solid
          ? "color-mix(in oklch, var(--bg) 90%, transparent)"
          : "transparent",
        borderBottom: solid ? "1px solid var(--line)" : "1px solid transparent",
        transition: "background 0.25s ease, border-color 0.25s ease",
      }}
    >
      <div
        className="container"
        style={{
          display: "flex",
          alignItems: "center",
          justifyContent: "space-between",
          height: 64,
        }}
      >
        <a
          href="#"
          style={{
            display: "flex",
            alignItems: "center",
            gap: 10,
            fontWeight: 500,
          }}
        >
          <Logo />
          <span>Calign</span>
        </a>
        <nav
          className="nav-links"
          style={{
            display: "flex",
            gap: 32,
            fontSize: 14,
            color: "var(--ink-2)",
          }}
        >
          {navLinks.map((l) => (
            <a key={l.href} href={l.href} className="nav-link">
              {l.label}
            </a>
          ))}
        </nav>
        <div style={{ display: "flex", alignItems: "center", gap: 8 }}>
          <button
            onClick={() => setTheme(theme === "light" ? "dark" : "light")}
            style={{
              width: 36,
              height: 36,
              borderRadius: 8,
              display: "flex",
              alignItems: "center",
              justifyContent: "center",
              color: "var(--ink-2)",
            }}
            aria-label="Toggle theme"
          >
            {theme === "light" ? <IMoon size={16} /> : <ISun size={16} />}
          </button>
          <button
            onClick={onJoin}
            className="btn btn-primary nav-cta"
            style={{ padding: "8px 14px", fontSize: 14 }}
          >
            Get early access
          </button>
          <button
            className="nav-hamburger"
            onClick={() => setMobileOpen((o) => !o)}
            aria-label="Toggle menu"
            style={{
              width: 36,
              height: 36,
              borderRadius: 8,
              display: "none",
              alignItems: "center",
              justifyContent: "center",
              color: "var(--ink-2)",
            }}
          >
            {mobileOpen ? <IClose size={18} /> : <HamburgerIcon />}
          </button>
        </div>
      </div>
      {mobileOpen && (
        <div
          style={{
            borderTop: "1px solid var(--line)",
            background: "var(--bg-elev)",
            padding: "12px 24px 24px",
          }}
        >
          {navLinks.map((l) => (
            <a
              key={l.href}
              href={l.href}
              onClick={() => setMobileOpen(false)}
              style={{
                display: "block",
                padding: "13px 0",
                fontSize: 16,
                color: "var(--ink-2)",
                borderBottom: "1px solid var(--line)",
              }}
            >
              {l.label}
            </a>
          ))}
          <button
            onClick={() => {
              onJoin();
              setMobileOpen(false);
            }}
            className="btn btn-primary"
            style={{ width: "100%", marginTop: 16, padding: "14px 20px" }}
          >
            Get early access
          </button>
        </div>
      )}
    </header>
  );
}

function HamburgerIcon() {
  return (
    <svg width="18" height="18" viewBox="0 0 18 18" fill="none">
      <line
        x1="2"
        y1="4.5"
        x2="16"
        y2="4.5"
        stroke="currentColor"
        strokeWidth="1.5"
        strokeLinecap="round"
      />
      <line
        x1="2"
        y1="9"
        x2="16"
        y2="9"
        stroke="currentColor"
        strokeWidth="1.5"
        strokeLinecap="round"
      />
      <line
        x1="2"
        y1="13.5"
        x2="16"
        y2="13.5"
        stroke="currentColor"
        strokeWidth="1.5"
        strokeLinecap="round"
      />
    </svg>
  );
}

function Logo() {
  return (
    <svg width="26" height="26" viewBox="0 0 26 26" fill="none">
      <rect x="1" y="1" width="24" height="24" rx="7" fill="var(--primary)" />
      <path
        d="M8 13 L11.5 16.5 L18 10"
        stroke="#ffffff"
        strokeWidth="2"
        strokeLinecap="round"
        strokeLinejoin="round"
      />
    </svg>
  );
}

function FinalCTA({ onJoin }) {
  return (
    <section
      style={{
        borderTop: "1px solid var(--line)",
        paddingTop: 120,
        paddingBottom: 120,
      }}
    >
      <div className="container" style={{ textAlign: "center", maxWidth: 760 }}>
        <h2
          className="reveal"
          style={{ fontSize: "clamp(2rem, 4.5vw, 3.5rem)", marginBottom: 20 }}
        >
          You're allowed to stop keeping
          <br />
          everything in your head.
        </h2>
        <p
          className="reveal"
          style={{
            fontSize: "clamp(1rem, 1.4vw, 1.2rem)",
            color: "var(--muted)",
            marginBottom: 36,
            lineHeight: 1.55,
          }}
        >
          Free during private beta. Founding members keep their plan free for
          life.
        </p>
        <div
          className="reveal"
          style={{
            display: "flex",
            gap: 12,
            justifyContent: "center",
            flexWrap: "wrap",
          }}
        >
          <button
            className="btn btn-primary"
            onClick={onJoin}
            style={{ padding: "14px 24px", fontSize: 15 }}
          >
            Reserve my spot <IArrow size={16} />
          </button>
          <a className="btn btn-ghost" href="#how">
            See how it works
          </a>
        </div>
        <div
          className="reveal mono"
          style={{ marginTop: 28, fontSize: 12, color: "var(--muted)" }}
        >
          247 ON THE WAITLIST · INVITES IN WAVES
        </div>
      </div>
    </section>
  );
}

function Footer() {
  return (
    <footer
      style={{
        borderTop: "1px solid var(--line)",
        padding: "48px 0 32px",
        background: "var(--bg-soft)",
      }}
    >
      <div className="container">
        <div
          className="footer-top"
          style={{
            display: "grid",
            gridTemplateColumns: "1.5fr 1fr 1fr 1fr",
            gap: 32,
            paddingBottom: 32,
            borderBottom: "1px solid var(--line)",
          }}
        >
          <div>
            <a
              href="#"
              style={{
                display: "flex",
                alignItems: "center",
                gap: 10,
                fontWeight: 500,
                marginBottom: 16,
              }}
            >
              <Logo />
              <span>Calign</span>
            </a>
            <p
              style={{
                fontSize: 13,
                color: "var(--muted)",
                maxWidth: 280,
                lineHeight: 1.55,
              }}
            >
              We handle the inbox. You handle the rest.
            </p>
          </div>
          {/* <FooterCol
            title="Product"
            links={["Features", "Pricing", "Roadmap", "Changelog"]}
          />
          <FooterCol
            title="Company"
            links={["About", "Contact", "Press kit", "Affiliates"]}
          />
          <FooterCol
            title="Legal"
            links={["Privacy", "Terms", "Security", "PIPEDA / GDPR"]}
          /> */}
        </div>
        <div
          style={{
            paddingTop: 24,
            display: "flex",
            justifyContent: "space-between",
            alignItems: "center",
            fontSize: 12,
            color: "var(--muted)",
            flexWrap: "wrap",
            gap: 12,
          }}
        >
          <span>© 2026 Calign. Made for overwhelmed humans.</span>
          <span className="mono">v3.0 · Toronto</span>
        </div>
      </div>
    </footer>
  );
}

function FooterCol({ title, links }) {
  return (
    <div>
      <div
        className="mono"
        style={{
          fontSize: 11,
          color: "var(--muted)",
          textTransform: "uppercase",
          letterSpacing: "0.1em",
          marginBottom: 14,
        }}
      >
        {title}
      </div>
      <div style={{ display: "flex", flexDirection: "column", gap: 10 }}>
        {links.map((l) => (
          <a key={l} href="#" style={{ fontSize: 13, color: "var(--ink-2)" }}>
            {l}
          </a>
        ))}
      </div>
    </div>
  );
}

// ============================================================
// Animation keyframes + responsive injections
// ============================================================

const styleEl = document.createElement("style");
styleEl.textContent = `
@keyframes scanSweep {
  0% { transform: translateX(-100%); }
  100% { transform: translateX(100%); }
}
@keyframes eventLand {
  0% { transform: scale(0.7) translateY(-8px); opacity: 0; }
  60% { transform: scale(1.05) translateY(0); opacity: 1; }
  100% { transform: scale(1); opacity: 1; }
}
@keyframes spin { to { transform: rotate(360deg); } }
@keyframes fadeIn { from { opacity: 0; } to { opacity: 1; } }
@keyframes modalIn { from { opacity: 0; transform: translateY(20px) scale(0.97); } to { opacity: 1; transform: none; } }
@keyframes pop {
  0% { transform: scale(0.5); opacity: 0; }
  60% { transform: scale(1.15); opacity: 1; }
  100% { transform: scale(1); }
}
@keyframes shimmer {
  0% { background-position: -200% center; }
  100% { background-position: 200% center; }
}
.beta-highlight {
  background: linear-gradient(90deg, var(--primary) 0%, var(--tertiary) 40%, var(--primary) 60%);
  background-size: 200% auto;
  -webkit-background-clip: text;
  -webkit-text-fill-color: transparent;
  background-clip: text;
  animation: shimmer 3s linear infinite;
  font-weight: 600;
}
.nav-link { transition: color 0.15s ease; position: relative; }
.nav-link:hover { color: var(--ink); }

@media (max-width: 880px) {
  .nav-links { display: none !important; }
  .nav-cta { display: none !important; }
  .nav-hamburger { display: flex !important; }
  .hero-grid { grid-template-columns: 1fr !important; }
  .how-grid { grid-template-columns: 1fr !important; gap: 24px !important; }
  .features-grid { grid-template-columns: 1fr !important; }
  .footer-top { grid-template-columns: 1fr 1fr !important; }
}
@media (max-width: 520px) {
  .footer-top { grid-template-columns: 1fr !important; }
  h1 { font-size: 2.2rem !important; }
}
`;
document.head.appendChild(styleEl);

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