/* global React, TOKENS, FONTS, DarkCard, CircleCheck, Ring, Icon, hexA, WeekStrip, Container, Eyebrow, H2, Lead, Wordmark, FMark, Eighty4Wordmark, CTAButton, AppleGlyph, HabitCardFront, MacTodayWindow, Phone, PhoneDaily, usePersona, personaStats, PersonaSwitcher */
const { useState: useStateB } = React;

/* ───────────────────────── WEEKLY & GOALS ───────────────────────── */
function WeeklyGoals() {
  const T = TOKENS;
  const persona = usePersona();
  const goals = persona.goals.map((g, i) => ({ n: `Outcome 0${i + 1}`, t: g.title, p: g.pct, steps: g.ratio, next: g.next }));
  return (
    <section style={{ padding: "100px 0", background: T.bgDeep, borderTop: `1px solid ${T.hair}`, borderBottom: `1px solid ${T.hair}` }}>
      <Container>
        <div style={{ maxWidth: 680, marginBottom: 48 }}>
          <Eyebrow>Weekly & Goals</Eyebrow>
          <H2 style={{ marginTop: 16 }}>Today is grounded in a bigger plan.</H2>
          <Lead style={{ marginTop: 18 }}>
            Each week you pick the one priority that has to finish, and you pull the next move straight from a goal. Up to three outcomes define the cycle, and each one is a small status board with its own steps and deadline.
          </Lead>
        </div>

        <div style={{ display: "grid", gridTemplateColumns: "0.85fr 1.15fr", gap: 48, alignItems: "stretch" }} className="wg-grid">
          {/* week priority instrument */}
          <div style={{ display: "flex", flexDirection: "column", gap: 16 }}>
            <DarkCard pressable={false} style={{ padding: "22px 22px 20px" }}>
              <div style={{ display: "flex", alignItems: "center", gap: 8, marginBottom: 14 }}>
                <span style={{ fontFamily: FONTS.mono, fontSize: 10, fontWeight: 600, letterSpacing: 0.6, textTransform: "uppercase", color: "rgba(255,255,255,0.85)" }}>Week 3 priority</span>
                <span style={{ fontFamily: FONTS.mono, fontSize: 9, fontWeight: 600, letterSpacing: 0.5, textTransform: "uppercase", color: T.accent, background: hexA(T.accent, 0.2), padding: "2px 7px", borderRadius: 999 }}>Current</span>
              </div>
              <div style={{ fontSize: 20, fontWeight: 600, color: "#fff", letterSpacing: -0.4, lineHeight: "26px" }}>{persona.weekPriority}</div>
              <div style={{ marginTop: 18, paddingTop: 16, borderTop: "1px solid rgba(255,255,255,0.1)", display: "flex", alignItems: "center", justifyContent: "space-between" }}>
                <WeekStrip week={3} total={12} color={T.accent} />
                <span style={{ fontFamily: FONTS.mono, fontSize: 10.5, color: "rgba(255,255,255,0.5)" }}>9 wks left</span>
              </div>
            </DarkCard>
            <div style={{ padding: "0 4px" }}>
              <div style={{ fontFamily: FONTS.mono, fontSize: 10.5, fontWeight: 600, letterSpacing: 0.6, textTransform: "uppercase", color: T.textMuted, marginBottom: 12 }}>Supporting · {persona.weekSupport.length}</div>
              {persona.weekSupport.map(({ t, s }, i, arr) => (
                <div key={i} style={{ display: "flex", alignItems: "center", gap: 11, padding: "10px 0", borderBottom: i < arr.length - 1 ? `1px solid ${T.hair}` : "none" }}>
                  <StatusGlyph status={s} />
                  <span style={{ flex: 1, fontSize: 14, color: s === "done" ? T.textMuted : T.ink, letterSpacing: -0.2, textDecoration: s === "done" ? "line-through" : "none" }}>{t}</span>
                </div>
              ))}
            </div>
          </div>

          {/* goals board */}
          <div>
            <div style={{ fontFamily: FONTS.mono, fontSize: 10.5, fontWeight: 600, letterSpacing: 0.6, textTransform: "uppercase", color: T.textMuted, marginBottom: 6 }}>Outcome goals · 3</div>
            {goals.map((g, i) => (
              <div key={i} style={{ display: "flex", gap: 16, padding: "20px 0", borderBottom: i < goals.length - 1 ? `1px solid ${T.hair}` : "none" }}>
                <div style={{ width: 3, alignSelf: "stretch", borderRadius: 2, background: T.accent, flexShrink: 0 }} />
                <Ring size={46} stroke={4} progress={g.p / 100} color={T.accent}><span style={{ fontFamily: FONTS.mono, fontSize: 11, fontWeight: 700, color: T.accent }}>{g.p}</span></Ring>
                <div style={{ flex: 1, minWidth: 0 }}>
                  <div style={{ display: "flex", alignItems: "center", justifyContent: "space-between" }}>
                    <span style={{ fontFamily: FONTS.mono, fontSize: 10, fontWeight: 600, letterSpacing: 0.4, textTransform: "uppercase", color: T.accent }}>{g.n}</span>
                    <span style={{ fontFamily: FONTS.mono, fontSize: 10.5, color: T.textMuted }}>{g.steps} steps</span>
                  </div>
                  <div style={{ fontSize: 16, fontWeight: 600, color: T.ink, letterSpacing: -0.3, lineHeight: "21px", marginTop: 5 }}>{g.t}</div>
                  <div style={{ fontSize: 13, color: T.textDim, marginTop: 6 }}><span style={{ color: T.textMuted }}>Next:</span> {g.next}</div>
                </div>
              </div>
            ))}
          </div>
        </div>
      </Container>
    </section>
  );
}
function StatusGlyph({ status }) {
  const T = TOKENS;
  if (status === "done") return <span style={{ width: 18, height: 18, borderRadius: 999, background: T.accent, display: "inline-flex", alignItems: "center", justifyContent: "center", flexShrink: 0 }}><Icon name="habits" size={10} color="#fff" strokeWidth={2.4} /></span>;
  if (status === "doing") return <span style={{ width: 18, height: 18, borderRadius: 999, border: `1.5px solid ${T.amber}`, background: `conic-gradient(${T.amber} 50%, transparent 50%)`, flexShrink: 0 }} />;
  return <span style={{ width: 18, height: 18, borderRadius: 999, border: `1.5px solid ${T.hairStrong}`, flexShrink: 0 }} />;
}

/* ───────────────────────── HABITS ───────────────────────── */
function Habits() {
  const T = TOKENS;
  const p = usePersona();
  const doneHabit = p.habits.find((h) => h.done) || p.habits[0];
  const openHabit = p.habits.find((h) => !h.done) || p.habits[1];
  return (
    <section id="habits" style={{ padding: "100px 0" }}>
      <Container>
        <div style={{ display: "grid", gridTemplateColumns: "0.95fr 1.05fr", gap: 60, alignItems: "center" }} className="feature-grid">
          <div style={{ display: "flex", justifyContent: "center", gap: 16 }} className="habit-stack">
            <div style={{ marginTop: 28 }}><HabitCardFront name={doneHabit.name} streak={doneHabit.streak} done={true} /></div>
            <div className="habit-second"><HabitCardFront name={openHabit.name} streak={openHabit.streak} done={false} /></div>
          </div>
          <div>
            <Eyebrow>Habits</Eyebrow>
            <H2 style={{ marginTop: 16 }}>Don't break the chain.</H2>
            <Lead style={{ marginTop: 18, maxWidth: 470 }}>
              Each habit is a card you check off once a day. Finish it and the card flips over to show the month's chain, the small reward that makes the streak worth protecting. Streaks are the one place a warm amber shows up.
            </Lead>
            <div style={{ marginTop: 26, display: "flex", flexDirection: "column", gap: 16 }}>
              {[
                ["One question a day", "“Did you do this today?” One tap to answer."],
                ["The chain, revealed", "Finishing flips the card to the month grid you're keeping alive."],
                ["Streaks in amber", "The only color besides the accent, and you have to earn it."],
              ].map(([t, d]) => (
                <div key={t} style={{ display: "flex", gap: 12, alignItems: "flex-start" }}>
                  <span style={{ width: 21, height: 21, borderRadius: 999, background: T.amberSoft, display: "inline-flex", alignItems: "center", justifyContent: "center", flexShrink: 0, marginTop: 1 }}>
                    <Icon name="flame" size={12} color={T.amber} strokeWidth={1.5} />
                  </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>
      </Container>
    </section>
  );
}

/* ───────────────────────── NOTES ───────────────────────── */
function Notes() {
  const T = TOKENS;
  const p = usePersona();
  return (
    <section style={{ padding: "100px 0", background: T.bgDeep, borderTop: `1px solid ${T.hair}`, borderBottom: `1px solid ${T.hair}` }}>
      <Container>
        <div style={{ display: "grid", gridTemplateColumns: "1fr 1fr", gap: 60, alignItems: "center" }} className="feature-grid">
          <div>
            <Eyebrow>Notes</Eyebrow>
            <H2 style={{ marginTop: 16 }}>Think on the page, one day at a time.</H2>
            <Lead style={{ marginTop: 18, maxWidth: 460 }}>
              Every day has a running note and quick captures. It's the same note that shows up on that day's Daily screen, so the reasoning behind a decision stays attached to the day it happened. Search back across the whole cycle.
            </Lead>
          </div>
          <div style={{ display: "flex", justifyContent: "center" }}>
            <div style={{ width: "100%", maxWidth: 440, background: T.card, border: `1px solid ${T.hair}`, borderRadius: T.rXl, boxShadow: T.shadowMd, overflow: "hidden" }}>
              <div style={{ padding: "16px 20px", borderBottom: `1px solid ${T.hair}`, display: "flex", alignItems: "center", justifyContent: "space-between" }}>
                <div>
                  <div style={{ fontSize: 15.5, fontWeight: 600, letterSpacing: -0.3 }}>Today</div>
                  <div style={{ fontSize: 12.5, color: T.textMuted }}>Friday, May 30</div>
                </div>
                <Icon name="notes" size={17} color={T.textMuted} />
              </div>
              <div style={{ padding: "16px 20px" }}>
                <div style={{ background: T.input, border: `1px solid ${T.hair}`, borderRadius: T.rMd, padding: "13px 14px" }}>
                  <p style={{ margin: 0, fontSize: 14.5, lineHeight: 1.6, color: T.ink }}>
                    {p.note}
                  </p>
                </div>
                <div style={{ fontFamily: FONTS.mono, fontSize: 10, fontWeight: 600, letterSpacing: 0.5, textTransform: "uppercase", color: T.textMuted, margin: "18px 0 10px" }}>Captures · {p.captures.length}</div>
                {p.captures.map(({ time: t, text: n }) => (
                  <div key={t} style={{ display: "flex", gap: 12, padding: "9px 0", borderTop: `1px solid ${T.hair}` }}>
                    <span style={{ fontFamily: FONTS.mono, fontSize: 11, color: T.accent, flexShrink: 0, paddingTop: 1 }}>{t}</span>
                    <span style={{ fontSize: 13.5, color: T.textDim, lineHeight: 1.45 }}>{n}</span>
                  </div>
                ))}
                <div style={{ display: "flex", alignItems: "center", gap: 7, marginTop: 14, fontSize: 12, color: T.textMuted }}>
                  <Icon name="daily" size={13} color={T.textMuted} /> This is the same note as that day's Daily screen.
                </div>
              </div>
            </div>
          </div>
        </div>
      </Container>
    </section>
  );
}

/* ───────────────────────── PLATFORMS ───────────────────────── */
function Platforms() {
  const T = TOKENS;
  return (
    <section id="platforms" style={{ padding: "100px 0" }}>
      <Container>
        <div style={{ textAlign: "center", maxWidth: 640, margin: "0 auto 52px" }}>
          <Eyebrow style={{ display: "flex", justifyContent: "center" }}>iPhone & Mac</Eyebrow>
          <H2 style={{ marginTop: 16 }}>Plan on the Mac. Run the day on iPhone.</H2>
          <Lead style={{ marginTop: 18 }}>
            On the Mac it's a keyboard-driven app with a ⌘K command palette and a live agenda rail. On iPhone it's calmer, the thing you actually open in the morning. Same 12-week plan, synced through your own iCloud account.
          </Lead>
        </div>
        <div style={{ position: "relative", display: "flex", justifyContent: "center" }} className="platform-stage">
          <div className="app-mac platform-mac"><MacTodayWindow /></div>
          <div className="platform-phone" style={{ position: "absolute", right: 0, bottom: -28, zIndex: 2 }}><Phone scale={0.62}><PhoneDaily /></Phone></div>
        </div>
        <div style={{ display: "flex", justifyContent: "center", gap: 34, marginTop: 40, flexWrap: "wrap" }}>
          {[["vision", "Private iCloud sync"], ["person", "Built for one person"], ["habits", "Native on both platforms"]].map(([ic, l]) => (
            <div key={l} style={{ display: "flex", alignItems: "center", gap: 9, fontSize: 14.5, color: T.textDim, fontWeight: 500 }}>
              <Icon name={ic} size={16} color={T.accent} />{l}
            </div>
          ))}
        </div>
      </Container>
    </section>
  );
}

/* ───────────────────────── EXAMPLE MODE ───────────────────────── */
function ExampleMode() {
  const T = TOKENS;
  const p = usePersona();
  const best = p.habits.reduce((a, b) => (b.streak > a.streak ? b : a));
  return (
    <section style={{ padding: "92px 0", background: T.bgDeep, borderTop: `1px solid ${T.hair}`, borderBottom: `1px solid ${T.hair}` }}>
      <Container>
        <div style={{ display: "grid", gridTemplateColumns: "1fr 1fr", gap: 56, alignItems: "center" }} className="feature-grid">
          <div>
            <Eyebrow>Example mode</Eyebrow>
            <H2 style={{ marginTop: 16 }}>Start from a real plan, not a blank screen.</H2>
            <Lead style={{ marginTop: 18, maxWidth: 450 }}>
              Eighty4 comes with a full example cycle you can poke around in, then clear with one tap. See what a finished system looks like before you write a single word of your own. It never touches your real data.
            </Lead>
            <div style={{ marginTop: 26 }}><PersonaSwitcher label="Pick an example to explore" /></div>
          </div>
          <div style={{ display: "flex", flexWrap: "wrap", gap: 12 }}>
            {[["Cycle", p.cycle], ["This week", p.weekPriority], ["Today", p.mission], ["Best streak", `${best.name} · ${best.streak} days`]].map(([k, v]) => (
              <div key={k} style={{ flex: "1 1 200px", background: T.card, border: `1px solid ${T.hair}`, borderRadius: T.rLg, padding: "16px 17px", boxShadow: T.shadowSm }}>
                <div style={{ fontFamily: FONTS.mono, fontSize: 9.5, fontWeight: 600, letterSpacing: 0.5, textTransform: "uppercase", color: T.textMuted, marginBottom: 8 }}>{k}</div>
                <div style={{ fontSize: 15, fontWeight: 600, color: T.ink, letterSpacing: -0.3, lineHeight: 1.3 }}>{v}</div>
              </div>
            ))}
          </div>
        </div>
      </Container>
    </section>
  );
}

/* ───────────────────────── PRIVACY ───────────────────────── */
function Privacy() {
  const T = TOKENS;
  return (
    <section style={{ padding: "92px 0" }}>
      <Container style={{ maxWidth: 720 }}>
        <div style={{ textAlign: "center" }}>
          <span style={{ display: "inline-flex", width: 50, height: 50, borderRadius: T.rMd, background: T.accentSoft, alignItems: "center", justifyContent: "center", marginBottom: 20 }}>
            <Icon name="person" size={22} color={T.accent} />
          </span>
          <H2 style={{ fontSize: "clamp(26px, 3.4vw, 38px)" }}>Your plan is yours. Nobody sees it.</H2>
          <Lead style={{ margin: "18px auto 0", maxWidth: 580 }}>
            Your cycle, notes, habits, and schedule live in your own private iCloud account, on your own devices. We don't run a server that holds your data, so there is nothing on our side to read, sell, or train on. There is no AI in Eighty4, and there never will be. It's built for one person: you.
          </Lead>
          <div style={{ display: "flex", justifyContent: "center", gap: 10, marginTop: 28, flexWrap: "wrap" }}>
            {["No AI, ever", "Private iCloud sync", "Never trained on your data"].map((t) => (
              <span key={t} style={{ display: "inline-flex", alignItems: "center", gap: 7, padding: "8px 14px", borderRadius: 999, background: T.card, border: `1px solid ${T.hair}`, boxShadow: T.shadowSm, fontSize: 13.5, fontWeight: 600, color: T.ink }}>
                <Icon name="habits" size={13} color={T.accent} strokeWidth={2} />{t}
              </span>
            ))}
          </div>
        </div>
      </Container>
    </section>
  );
}

/* ───────────────────────── FAQ ───────────────────────── */
function FAQ() {
  const T = TOKENS;
  const items = [
    ["Is there any AI in it?", "No, and there never will be. Eighty4 doesn't run your plan through a model, write your tasks, or suggest what to do next. It's a quiet tool for your own thinking, not a co-pilot trying to do the thinking for you."],
    ["Who can see my data?", "Only you. Your plan, notes, habits, and schedule live in your own private iCloud account, on your own devices. We don't run a server that holds your data, so there is nothing on our side to look at. We don't read it, we don't sell it, and we never train anything on it. That will not change."],
    ["Is Eighty4 a task manager?", "It includes tasks, but it's built around the whole 12-week loop: vision, goals, weekly planning, daily execution, habits, and notes. The day's tasks are just the bottom of that ladder."],
    ["Is it for teams?", "No. Eighty4 is built for private, single-person planning. There are no shared boards, assignments, or comments."],
    ["Does it replace my calendar?", "Not entirely. It has schedule blocks for planning the day inside your cycle, but it isn't a full calendar replacement."],
    ["Does it sync between iPhone and Mac?", "Yes, through your own iCloud account. Your data moves between your devices over Apple's encrypted sync and never passes through a server of ours."],
    ["How do I learn it?", "There's no heavy walkthrough. A reversible example cycle shows you what a populated plan looks like, then you clear it and make it yours."],
    ["Is it available now?", "Eighty4 is in TestFlight beta. You can join today, and App Store availability will follow."],
  ];
  const [open, setOpen] = useStateB(0);
  return (
    <section style={{ padding: "92px 0", background: T.bgDeep, borderTop: `1px solid ${T.hair}` }}>
      <Container style={{ maxWidth: 760 }}>
        <div style={{ textAlign: "center", marginBottom: 42 }}>
          <Eyebrow style={{ display: "flex", justifyContent: "center" }}>Common questions</Eyebrow>
          <H2 style={{ marginTop: 16, fontSize: "clamp(26px, 3.4vw, 38px)" }}>Before you join the beta</H2>
        </div>
        <div style={{ display: "flex", flexDirection: "column", gap: 10 }}>
          {items.map(([q, a], i) => {
            const isOpen = open === i;
            return (
              <div key={i} style={{ background: T.card, border: `1px solid ${T.hair}`, borderRadius: T.rLg, overflow: "hidden", boxShadow: T.shadowSm }}>
                <button onClick={() => setOpen(isOpen ? -1 : i)} style={{ width: "100%", display: "flex", alignItems: "center", justifyContent: "space-between", gap: 16, padding: "17px 20px", background: "none", border: "none", cursor: "pointer", textAlign: "left", fontSize: 16, fontWeight: 600, color: T.ink, letterSpacing: -0.3, fontFamily: FONTS.sans }}>
                  {q}
                  <Icon name="chevD" size={16} color={T.textMuted} style={{ transform: isOpen ? "rotate(180deg)" : "none", transition: "transform 200ms cubic-bezier(.32,.72,0,1)" }} />
                </button>
                {isOpen && <div style={{ padding: "0 20px 18px", fontSize: 15, lineHeight: 1.6, color: T.textDim, maxWidth: 640 }}>{a}</div>}
              </div>
            );
          })}
        </div>
      </Container>
    </section>
  );
}

/* ───────────────────────── CTA + FOOTER ───────────────────────── */
function CTA({ cfg }) {
  const T = TOKENS;
  return (
    <section id="join" style={{ padding: "30px 0 0" }}>
      <Container>
        <DarkCard pressable={false} style={{ borderRadius: T.rXl, padding: "78px 40px", textAlign: "center", position: "relative", overflow: "hidden" }}>
          <div style={{ position: "relative" }}>
            <div style={{ display: "flex", justifyContent: "center", marginBottom: 22 }}><Eighty4Wordmark size={34} light /></div>
            <h2 style={{ margin: 0, fontSize: "clamp(30px, 4.4vw, 52px)", fontWeight: 700, letterSpacing: -2, lineHeight: 1.05, color: "#fff", textWrap: "balance" }}>
              84 days. One thing at a time.
            </h2>
            <p style={{ margin: "20px auto 0", fontSize: 18, color: "rgba(255,255,255,0.7)", maxWidth: 520, lineHeight: 1.55 }}>
              Know what matters this week and what matters today, and keep moving without rebuilding the plan every morning.
            </p>
            <div style={{ marginTop: 32, display: "flex", gap: 12, justifyContent: "center", flexWrap: "wrap" }}>
              <CTAButton primary large><AppleGlyph size={17} />{cfg.cta}</CTAButton>
              <CTAButton large light>See the loop</CTAButton>
            </div>
            <div style={{ marginTop: 22, fontFamily: FONTS.mono, fontSize: 11.5, color: "rgba(255,255,255,0.5)", letterSpacing: 0.3 }}>FREE DURING THE BETA · IPHONE & MAC · iOS 18 / macOS 15</div>
          </div>
        </DarkCard>
      </Container>
    </section>
  );
}

function Footer() {
  const T = TOKENS;
  return (
    <footer style={{ padding: "52px 0 60px" }}>
      <Container style={{ display: "flex", justifyContent: "space-between", alignItems: "flex-start", flexWrap: "wrap", gap: 24 }}>
        <div>
          <Wordmark size={18} />
          <p style={{ margin: "12px 0 0", fontSize: 13.5, color: T.textMuted, maxWidth: 320, lineHeight: 1.5 }}>
            A native iPhone and Mac app for running a 12-week cycle from vision to daily execution.
          </p>
        </div>
        <div style={{ display: "flex", gap: 40, flexWrap: "wrap" }}>
          {[["Product", [["The loop", "#loop"], ["Daily", "#daily"], ["Habits", "#habits"], ["iPhone & Mac", "#platforms"]]], ["More", [["Privacy", "Privacy%20Policy.html"], ["TestFlight", "#join"], ["Contact", "mailto:hello@eighty4.app"]]]].map(([h, links]) => (
            <div key={h}>
              <div style={{ fontFamily: FONTS.mono, fontSize: 10, fontWeight: 600, letterSpacing: 0.6, textTransform: "uppercase", color: T.textMuted, marginBottom: 12 }}>{h}</div>
              <div style={{ display: "flex", flexDirection: "column", gap: 9 }}>
                {links.map(([l, href]) => <a key={l} href={href} style={{ fontSize: 14, color: T.textDim, textDecoration: "none" }}>{l}</a>)}
              </div>
            </div>
          ))}
        </div>
      </Container>
      <Container style={{ marginTop: 38, paddingTop: 22, borderTop: `1px solid ${T.hair}`, display: "flex", justifyContent: "space-between", flexWrap: "wrap", gap: 12 }}>
        <span style={{ fontSize: 13, color: T.textMuted }}>© 2026 Eighty4. Built for people who finish.</span>
        <span style={{ fontSize: 13, color: T.textMuted }}>Made on iPhone and Mac.</span>
      </Container>
    </footer>
  );
}

Object.assign(window, { WeeklyGoals, Habits, Notes, Platforms, ExampleMode, Privacy, FAQ, CTA, Footer });
