/* global React, TOKENS, FONTS, DarkCard, CircleCheck, Ring, Icon, hexA, Phone, PhoneHome, PhoneDaily, MacTodayWindow, WeekStrip, Eighty4Mark, Eighty4Wordmark, usePersona, PersonaSwitcher */
const { useState, useEffect } = React;

const MAXW = 1160;
function Container({ children, style = {} }) {
  return <div style={{ maxWidth: MAXW, margin: "0 auto", padding: "0 32px", ...style }}>{children}</div>;
}
function Eyebrow({ children, color, style = {} }) {
  const T = TOKENS;
  return <div style={{ fontFamily: FONTS.mono, fontSize: 11.5, fontWeight: 600, letterSpacing: 1, textTransform: "uppercase", color: color || T.accent, ...style }}>{children}</div>;
}
// Section title
function H2({ children, style = {} }) {
  return <h2 style={{ margin: 0, fontSize: "clamp(30px, 4.2vw, 46px)", fontWeight: 700, letterSpacing: -1.6, lineHeight: 1.06, color: TOKENS.ink, textWrap: "balance", ...style }}>{children}</h2>;
}
function Lead({ children, style = {} }) {
  return <p style={{ margin: 0, fontSize: 18, lineHeight: 1.6, color: TOKENS.textDim, letterSpacing: -0.2, textWrap: "pretty", ...style }}>{children}</p>;
}

// brand marks now come from brand.jsx — delegate for back-compat
function FMark({ size = 30, light = false }) {
  return <Eighty4Mark size={size} variant={light ? "default" : "default"} />;
}
function Wordmark({ size = 20, light = false }) {
  return <Eighty4Wordmark size={size} light={light} />;
}

// CTA button
function CTAButton({ children, primary, large, light, style = {} }) {
  const T = TOKENS;
  const h = large ? 52 : 44;
  const base = { display: "inline-flex", alignItems: "center", gap: 9, height: h, padding: large ? "0 22px" : "0 18px", borderRadius: T.rMd, fontSize: large ? 15.5 : 14, fontWeight: 600, letterSpacing: -0.2, textDecoration: "none", cursor: "pointer", border: "1px solid transparent", whiteSpace: "nowrap" };
  if (primary) return <a href="#join" onClick={(e) => { e.preventDefault(); window.dispatchEvent(new CustomEvent("eighty4-join")); }} style={{ ...base, background: T.accent, color: "#fff", boxShadow: `0 1px 2px ${hexA(T.accent, 0.4)}, inset 0 1px 0 rgba(255,255,255,0.18)`, ...style }}>{children}</a>;
  return <a href="#loop" style={{ ...base, background: light ? "rgba(255,255,255,0.08)" : T.soft, color: light ? "#fff" : T.ink, borderColor: light ? "rgba(255,255,255,0.16)" : T.hair, ...style }}>{children}</a>;
}

function AppleGlyph({ size = 16, color = "#fff" }) {
  return <svg width={size} height={size} viewBox="0 0 16 16" style={{ flexShrink: 0 }}><path fill={color} d="M11.2 8.4c0-1.6 1.3-2.4 1.4-2.4-.8-1.1-2-1.3-2.4-1.3-1-.1-2 .6-2.5.6-.5 0-1.3-.6-2.2-.6-1.1 0-2.2.7-2.8 1.7-1.2 2.1-.3 5.2.9 6.9.6.8 1.3 1.7 2.2 1.7.9 0 1.2-.6 2.2-.6 1 0 1.3.6 2.2.6.9 0 1.5-.8 2.1-1.7.6-.9.9-1.8.9-1.8s-1.8-.7-1.8-3.1zM9.8 4c.5-.6.8-1.4.7-2.2-.7 0-1.5.4-2 1-.4.5-.8 1.4-.7 2.1.7.1 1.5-.3 2-.9z"/></svg>;
}

/* ───────────────────────── NAV ───────────────────────── */
function Nav({ cfg }) {
  const T = TOKENS;
  const [scrolled, setScrolled] = useState(false);
  useEffect(() => {
    const f = () => setScrolled(window.scrollY > 8);
    window.addEventListener("scroll", f); f();
    return () => window.removeEventListener("scroll", f);
  }, []);
  return (
    <nav style={{ position: "fixed", top: 0, left: 0, right: 0, zIndex: 50, transition: "all 240ms cubic-bezier(.32,.72,0,1)", padding: scrolled ? "10px 0" : "16px 0", background: scrolled ? "color-mix(in oklab, #ECEBE6 82%, transparent)" : "transparent", backdropFilter: scrolled ? "blur(16px) saturate(1.4)" : "none", borderBottom: `1px solid ${scrolled ? T.hair : "transparent"}` }}>
      <Container style={{ display: "flex", alignItems: "center", gap: 24 }}>
        <a href="#top" style={{ textDecoration: "none" }}><Wordmark size={19} /></a>
        <div style={{ flex: 1 }} />
        <div style={{ display: "flex", gap: 4, marginRight: 8 }} className="nav-links">
          {[["The loop", "#loop"], ["Daily", "#daily"], ["Habits", "#habits"], ["iPhone & Mac", "#platforms"]].map(([n, h]) => (
            <a key={n} href={h} style={{ padding: "7px 11px", borderRadius: 8, fontSize: 13.5, fontWeight: 500, textDecoration: "none", color: T.textDim, whiteSpace: "nowrap" }}>{n}</a>
          ))}
        </div>
        <CTAButton primary><AppleGlyph size={15} />{cfg.cta}</CTAButton>
      </Container>
    </nav>
  );
}

/* ───────────────────────── HERO ───────────────────────── */
function Hero({ cfg }) {
  const T = TOKENS;
  return (
    <section id="top" style={{ paddingTop: 138, paddingBottom: 80, position: "relative", overflow: "hidden" }}>
      <div style={{ position: "absolute", top: -120, left: "50%", transform: "translateX(-50%)", width: 1100, height: 620, pointerEvents: "none", background: `radial-gradient(ellipse at center, ${hexA(T.accent, 0.13)} 0%, transparent 60%)` }} />
      <Container style={{ position: "relative" }}>
        <div style={{ display: "inline-flex", alignItems: "center", gap: 8, padding: "5px 13px 5px 9px", marginBottom: 26, borderRadius: 999, border: `1px solid ${T.hair}`, background: T.card, boxShadow: T.shadowSm }}>
          <span style={{ width: 6, height: 6, borderRadius: 999, background: T.accent }} />
          <span style={{ fontFamily: FONTS.mono, fontSize: 11.5, fontWeight: 600, letterSpacing: 0.4, textTransform: "uppercase", color: T.textDim, whiteSpace: "nowrap" }}>Now in TestFlight · iPhone & Mac</span>
        </div>

        <h1 style={{ margin: 0, fontSize: "clamp(38px, 6.2vw, 72px)", fontWeight: 700, letterSpacing: -2.4, lineHeight: 1.02, maxWidth: 880, color: T.ink, textWrap: "balance" }}>
          {cfg.headline}
        </h1>
        <Lead style={{ marginTop: 22, marginBottom: 32, maxWidth: 600, fontSize: 19 }}>
          Eighty4 gives a 12-week cycle one clear focus each day. Your vision, goals, weekly priority, and daily tasks live in one native iPhone and Mac app, so the plan you make on Sunday is still running your Thursday.
        </Lead>

        <div style={{ display: "flex", gap: 12, alignItems: "center", flexWrap: "wrap" }}>
          <CTAButton primary large><AppleGlyph size={17} />{cfg.cta}</CTAButton>
          <CTAButton large>See how it works <Icon name="arrowR" size={15} color={T.ink} /></CTAButton>
          <span style={{ fontSize: 13, color: T.textMuted, marginLeft: 4 }}>Free during the beta · iOS 18 · macOS 15</span>
        </div>

        {/* persona switcher */}
        <div style={{ marginTop: 46 }}><PersonaSwitcher /></div>

        {/* hero visual */}
        <div className="hero-visual" style={{ marginTop: 40, position: "relative", display: "flex", justifyContent: "center" }}>
          <div className="app-mac"><MacTodayWindow /></div>
          <div className="hero-phone" style={{ position: "absolute", right: 0, bottom: -28 }}><Phone scale={0.62}><PhoneHome /></Phone></div>
        </div>
      </Container>
    </section>
  );
}

/* ───────────────────────── THE PROBLEM ───────────────────────── */
function Problem() {
  const T = TOKENS;
  const p = usePersona();
  const scattered = ["A goals doc you never reopen", "Tasks in three different apps", "A calendar that forgot the goal", "Notes lost by Friday", "A habit tracker on its own island", "A vision you wrote once in January"];
  return (
    <section style={{ padding: "90px 0" }}>
      <Container>
        <div style={{ maxWidth: 680 }}>
          <Eyebrow>The problem</Eyebrow>
          <H2 style={{ marginTop: 16 }}>Most plans fall apart between the goal and the day.</H2>
          <Lead style={{ marginTop: 18, maxWidth: 600 }}>
            Writing the 12-week goal was never the hard part. The hard part is keeping it in front of you when you sit down and decide what to actually do today. When the plan lives in six places, none of them wins.
          </Lead>
        </div>

        <div style={{ marginTop: 46, display: "grid", gridTemplateColumns: "1.1fr auto 1fr", gap: 36, alignItems: "center" }} className="problem-grid">
          <div style={{ display: "grid", gridTemplateColumns: "1fr 1fr", gap: 10 }}>
            {scattered.map((s) => (
              <div key={s} style={{ display: "flex", alignItems: "center", gap: 10, padding: "14px 15px", background: T.bgDeep, border: `1px dashed ${T.hairDash}`, borderRadius: T.rMd, color: T.textMuted, fontSize: 13.5, fontWeight: 500, lineHeight: 1.35 }}>
                <span style={{ width: 6, height: 6, borderRadius: 999, background: T.textFaint, flexShrink: 0 }} />{s}
              </div>
            ))}
          </div>

          <div className="problem-arrow" style={{ display: "flex", flexDirection: "column", alignItems: "center", gap: 7 }}>
            <Icon name="arrowR" size={26} color={T.accent} />
            <span style={{ fontFamily: FONTS.mono, fontSize: 10.5, fontWeight: 600, letterSpacing: 0.6, textTransform: "uppercase", color: T.textMuted }}>Eighty4</span>
          </div>

          {/* one instrument */}
          <DarkCard pressable={false} style={{ padding: "26px 24px" }}>
            <span style={{ fontFamily: FONTS.mono, fontSize: 10.5, fontWeight: 600, letterSpacing: 0.7, textTransform: "uppercase", color: "rgba(255,255,255,0.85)" }}>Today's mission</span>
            <div style={{ fontSize: 21, fontWeight: 600, color: "#fff", letterSpacing: -0.5, lineHeight: "27px", marginTop: 12 }}>{p.mission}</div>
            <div style={{ marginTop: 18, paddingTop: 16, borderTop: "1px solid rgba(255,255,255,0.1)" }}>
              <p style={{ margin: 0, fontSize: 14, lineHeight: 1.55, color: "rgba(255,255,255,0.65)" }}>
                One focus, chosen from the goal that needs it. Everything else supports it or waits.
              </p>
            </div>
          </DarkCard>
        </div>
      </Container>
    </section>
  );
}

/* ───────────────────────── THE LOOP / ALTITUDES ───────────────────────── */
const ALTITUDES = [
  { label: "Vision", eyebrow: "This cycle's vision", text: "Ship a v1 people pay for, hold a 25 mi/week running base, and protect my evenings.", meta: "The why and where", icon: "vision" },
  { label: "Goals", eyebrow: "Outcome 01 · 40%", text: "Ship Eighty4 v1 to TestFlight with 5 external testers.", meta: "The commitments", icon: "goals" },
  { label: "Weekly", eyebrow: "Week 3 priority", text: "Get the internal TestFlight build out the door.", meta: "The one move this week", icon: "weekly" },
  { label: "Daily", eyebrow: "Today's mission", text: "Build Eighty4's daily + home screens.", meta: "The one thing today", icon: "daily" },
];

function TheLoop() {
  const T = TOKENS;
  const p = usePersona();
  const alts = [
    { label: "Vision", eyebrow: "This cycle's vision", text: p.vision, meta: "The why and where", icon: "vision" },
    { label: "Goals", eyebrow: `Outcome 01 · ${p.goals[0].pct}%`, text: p.goals[0].title, meta: "The commitments", icon: "goals" },
    { label: "Weekly", eyebrow: "Week 3 priority", text: p.weekPriority, meta: "The one move this week", icon: "weekly" },
    { label: "Daily", eyebrow: "Today's mission", text: p.mission, meta: "The one thing today", icon: "daily" },
  ];
  return (
    <section id="loop" style={{ padding: "100px 0", background: T.bgDeep, borderTop: `1px solid ${T.hair}`, borderBottom: `1px solid ${T.hair}` }}>
      <Container>
        <div style={{ display: "grid", gridTemplateColumns: "0.9fr 1.1fr", gap: 64, alignItems: "start" }} className="loop-grid">
          <div style={{ position: "sticky", top: 100 }}>
            <Eyebrow>The loop</Eyebrow>
            <H2 style={{ marginTop: 16 }}>One focus at every altitude.</H2>
            <Lead style={{ marginTop: 18 }}>
              Eighty4 is built around one idea. Each level of planning sets up the one below it. Your vision points a direction, your goals name the few outcomes that count, and each week pulls the next move down into your days.
            </Lead>
            <div style={{ marginTop: 26, display: "flex", flexDirection: "column", gap: 14 }}>
              {[["vision", "Vision", "Where this cycle is headed."], ["goals", "Goals", "Up to three outcomes that define it."], ["weekly", "Weekly", "The priority that must finish this week."], ["daily", "Daily", "The one thing that moves today."], ["habits", "Habits", "The small reps that keep the chain alive."], ["notes", "Notes", "The day's thinking, kept with the day."]].map(([ic, t, d]) => (
                <div key={t} style={{ display: "flex", alignItems: "center", gap: 13 }}>
                  <div style={{ width: 32, height: 32, borderRadius: 9, background: T.accentSoft, display: "flex", alignItems: "center", justifyContent: "center", flexShrink: 0 }}><Icon name={ic} size={16} color={T.accent} /></div>
                  <span style={{ fontSize: 14.5, fontWeight: 600, color: T.ink, width: 66, flexShrink: 0 }}>{t}</span>
                  <span style={{ fontSize: 14, color: T.textDim }}>{d}</span>
                </div>
              ))}
            </div>
          </div>

          {/* the stack of instruments */}
          <div style={{ display: "flex", flexDirection: "column", gap: 14, position: "relative" }}>
            {alts.map((a, i) => (
              <div key={a.label} style={{ position: "relative" }}>
                {i < alts.length - 1 && (
                  <div style={{ position: "absolute", left: 34, bottom: -14, height: 14, width: 1.5, background: hexA(T.accent, 0.4), zIndex: 0 }} />
                )}
                <DarkCard pressable={false} style={{ padding: "20px 22px" }}>
                  <div style={{ display: "flex", alignItems: "center", gap: 11, marginBottom: 12 }}>
                    <div style={{ width: 30, height: 30, borderRadius: 8, background: "rgba(255,255,255,0.08)", border: "1px solid rgba(255,255,255,0.1)", display: "flex", alignItems: "center", justifyContent: "center", flexShrink: 0 }}>
                      <Icon name={a.icon} size={15} color={T.accent} />
                    </div>
                    <span style={{ fontFamily: FONTS.mono, fontSize: 10, fontWeight: 600, letterSpacing: 0.6, textTransform: "uppercase", color: "rgba(255,255,255,0.85)" }}>{a.eyebrow}</span>
                    <div style={{ flex: 1 }} />
                    <span style={{ fontFamily: FONTS.mono, fontSize: 9.5, fontWeight: 600, letterSpacing: 0.5, textTransform: "uppercase", color: "rgba(255,255,255,0.4)" }}>{a.label}</span>
                  </div>
                  <div style={{ fontSize: 18, fontWeight: 600, color: "#fff", letterSpacing: -0.4, lineHeight: "24px" }}>{a.text}</div>
                  <div style={{ marginTop: 12, fontFamily: FONTS.mono, fontSize: 10.5, color: "rgba(255,255,255,0.45)" }}>{a.meta}</div>
                </DarkCard>
              </div>
            ))}
          </div>
        </div>
      </Container>
    </section>
  );
}

/* ───────────────────────── DAILY ───────────────────────── */
function DailyFeature() {
  const T = TOKENS;
  return (
    <section id="daily" style={{ padding: "100px 0" }}>
      <Container>
        <div style={{ display: "grid", gridTemplateColumns: "1.05fr 0.95fr", gap: 60, alignItems: "center" }} className="feature-grid">
          <div>
            <Eyebrow>Daily workbench</Eyebrow>
            <H2 style={{ marginTop: 16 }}>You land on one thing, not a wall of tasks.</H2>
            <Lead style={{ marginTop: 18, maxWidth: 480 }}>
              The day's main task sits in the dark card at the top, big and editable so you can't lose track of it. Your schedule, supporting tasks, and notes open underneath. You can scrub back to any past day to review it or fix the record.
            </Lead>
            <div style={{ marginTop: 26, display: "flex", flexDirection: "column", gap: 16 }}>
              {[
                ["The one thing", "Your main task for the day, front and center."],
                ["Schedule", "Time blocks with a live “now” marker through the day."],
                ["Supporting tasks", "The work behind the focus, with a single tap to finish."],
                ["Notes that stay put", "A running note and quick captures, kept with the day."],
              ].map(([t, d]) => (
                <div key={t} style={{ display: "flex", gap: 12, alignItems: "flex-start" }}>
                  <span style={{ width: 21, height: 21, borderRadius: 999, background: T.accentSoft, display: "inline-flex", alignItems: "center", justifyContent: "center", flexShrink: 0, marginTop: 1 }}>
                    <Icon name="habits" size={12} color={T.accent} strokeWidth={2} />
                  </span>
                  <span style={{ fontSize: 15.5, lineHeight: 1.5, color: T.textDim }}><strong style={{ fontWeight: 600, color: T.ink }}>{t}.</strong> {d}</span>
                </div>
              ))}
            </div>
          </div>
          <div style={{ display: "flex", justifyContent: "center" }}>
            <Phone scale={0.74}><PhoneDaily /></Phone>
          </div>
        </div>
      </Container>
    </section>
  );
}

Object.assign(window, { Container, Eyebrow, H2, Lead, FMark, Wordmark, CTAButton, AppleGlyph, Nav, Hero, Problem, TheLoop, DailyFeature });
