// Landing — promo page at /landing.
// Neumorphic style matching the rest of the product. Linked from the wordmark.

// Each hero slide drives the left-side h1 tagline. `title: null` keeps the
// original "Every send, compliant before it leaves your outbox." tagline
// (used for the angel/score slide).
const SLIDES = [
  { title: null },
  { title: "See how compliant you've been — historically." },
  { title: "Compose with the regulator looking over your shoulder." },
  { title: "Train an AI to run your inbox." },
  { title: "Wake up to an inbox that's already triaged." },
];

const LandingPage = () => {
  const connected = (typeof useConnected === "function") ? useConnected() : null;
  const ctaHref = connected ? "/dashboard" : "/connect";
  const ctaLabel = connected ? "Open your dashboard" : "Connect your inbox";
  const [idx, setIdx] = React.useState(0);
  const slideTitle = SLIDES[idx].title;

  return (
    <div style={{
      width: "100%", minHeight: "100vh",
      background: "var(--white-pure)",
      position: "relative", overflowX: "hidden",
    }}>
      {/* Ambient red wash */}
      <div style={{
        position: "absolute", inset: 0, pointerEvents: "none",
        background: "radial-gradient(ellipse 80% 50% at 100% 0%, rgba(230,57,70,0.10) 0%, rgba(230,57,70,0) 70%)",
      }}/>

      {/* TopBar — matches the app TopBar geometry (height 64, padding 0 32px,
          dot 12, text 18) so the wordmark sits in exactly the same spot
          across landing and product pages. */}
      <header style={{
        position: "relative", zIndex: 5,
        height: 64, display: "flex", alignItems: "center",
        padding: "0 32px", justifyContent: "space-between",
      }}>
        <a href="/landing" data-link style={{
          display: "flex", alignItems: "center", gap: 11,
          textDecoration: "none", color: "inherit",
        }}>
          <span style={{
            width: 12, height: 12, borderRadius: 999, background: "var(--red-core)",
            boxShadow: "0 0 0 5px rgba(230,57,70,0.18)",
          }}/>
          <span style={{ fontSize: 18, fontWeight: 600, letterSpacing: "-0.01em" }}>RedGone</span>
        </a>

        <nav style={{ display: "flex", alignItems: "center", gap: 8 }}>
          <a href="/dashboard" data-link className="neu-btn neu-btn-sm" style={{
            textDecoration: "none", color: "var(--midnight-soft)",
          }}>Try the demo</a>
          {connected ? (
            <a href="/dashboard" data-link className="neu-btn-redglow neu-btn-redglow-sm">
              <span>Open dashboard</span>
              <span style={{ opacity: 0.85 }}>→</span>
            </a>
          ) : (
            <a href="/connect" data-link className="neu-btn-redglow neu-btn-redglow-sm">
              <span>Connect Email</span>
              <span style={{ opacity: 0.85 }}>→</span>
            </a>
          )}
        </nav>
      </header>

      {/* Hero — wrapped in a full-width relative container so the prev/next
          chevrons can sit on the LEFT and RIGHT edges of the viewport,
          vertically centered against the hero card. */}
      <div style={{ position: "relative" }}>
        <button
          onClick={() => setIdx((i) => (i - 1 + SLIDES.length) % SLIDES.length)}
          aria-label="Previous slide"
          className="neu-btn"
          style={{
            position: "absolute",
            left: 24, top: "50%",
            transform: "translateY(-50%)",
            width: 44, height: 44, padding: 0, borderRadius: 999,
            fontSize: 20, lineHeight: 1, zIndex: 4,
          }}>‹</button>
        <button
          onClick={() => setIdx((i) => (i + 1) % SLIDES.length)}
          aria-label="Next slide"
          className="neu-btn"
          style={{
            position: "absolute",
            right: 24, top: "50%",
            transform: "translateY(-50%)",
            width: 44, height: 44, padding: 0, borderRadius: 999,
            fontSize: 20, lineHeight: 1, zIndex: 4,
          }}>›</button>

      <section style={{
        position: "relative", zIndex: 1,
        padding: "60px 48px 80px",
        display: "grid",
        gridTemplateColumns: "minmax(0, 1.1fr) minmax(0, 1fr)",
        gap: 56,
        maxWidth: 1280, margin: "0 auto",
        alignItems: "center",
      }}>
        <div>
          <div className="micro">Compliance · for email</div>
          <h1 style={{
            fontFamily: "Manrope, Plus Jakarta Sans, Inter, sans-serif",
            fontSize: "clamp(40px, 5vw, 68px)",
            fontWeight: 600, lineHeight: 1.05,
            letterSpacing: "-0.03em",
            margin: "16px 0 20px",
            color: "var(--midnight)",
            minHeight: "calc(3 * 1.05em)",
          }}>
            {slideTitle
              ? slideTitle
              : <>Every send, compliant<br/>before it leaves<br/>your outbox.</>}
          </h1>
          <p className="body" style={{
            fontSize: 18, lineHeight: 1.5, maxWidth: 520,
            color: "var(--midnight-soft)",
          }}>
            RedGone reads your inbox, scores every email against FCA, SEC, or your firm's
            internal policy, and triages incoming messages so the only thing in front of
            you is what actually needs you.
          </p>

          <div style={{ display: "flex", gap: 14, marginTop: 32, flexWrap: "wrap" }}>
            <a href={ctaHref} data-link className="neu-btn-redglow neu-btn-redglow-lg">
              <span>{ctaLabel}</span>
              <span style={{ opacity: 0.85 }}>→</span>
            </a>
            <a href="/dashboard" data-link className="neu-btn neu-btn-primary" style={{
              textDecoration: "none",
              padding: "16px 22px", fontSize: 14,
              display: "inline-flex", alignItems: "center", gap: 10,
            }}>
              <span>See it in action</span>
              <span style={{ opacity: 0.85 }}>→</span>
            </a>
          </div>

          <div className="mono" style={{
            marginTop: 28, fontSize: 11, letterSpacing: "0.06em",
            color: "var(--midnight-soft)",
          }}>
            FCA · SEC · INTERNAL POLICY READY
          </div>
        </div>

        {/* Hero visual — swipeable card. Each slide is one point about the app.
            idx state is lifted into LandingPage so the left-side h1 tagline
            can mirror the active slide's title. */}
        <HeroCard idx={idx} setIdx={setIdx} total={SLIDES.length}/>
      </section>
      </div>

      {/* Pinned features (click-throughs) */}
      <section style={{
        padding: "20px 48px 80px",
        maxWidth: 1280, margin: "0 auto",
      }}>
        <div className="micro" style={{ marginBottom: 24 }}>Try it now</div>
        <div style={{
          display: "grid",
          gridTemplateColumns: "repeat(auto-fit, minmax(280px, 1fr))",
          gap: 24,
        }}>
          <FeatureCard
            tag="01"
            title="Compose, scored live."
            body="Like Grammarly, but for regulatory language. Every phrase gets a citation — SEC Rule 482, FCA fair-clear-balanced, your firm's banned-phrase list. Click Fix-All. The AI rewrites everything. Score jumps 60 → 96 in a second."
            href="/compose"
          />
          <FeatureCard
            tag="02"
            title="Inbox, triaged for you."
            body="Every incoming email gets a category — Auto-Reply, AI Draft, Human Send, Human Required — with a confidence score. The two emails that actually need you stay. The 47 that don't, disappear into folders."
            href="/inbox"
          />
          <FeatureCard
            tag="03"
            title="Rules, written by the AI."
            body="Talk to RedGone about how your inbox should work. It writes the policy file. Re-train any time, edit by hand, or upload your firm's PDF. Built for compliance officers who don't want to maintain a regex zoo."
            href="/spec"
          />
        </div>
      </section>

      {/* Bottom CTA strip */}
      <section style={{
        padding: "0 48px 80px",
        maxWidth: 1280, margin: "0 auto",
      }}>
        <div className="pillow hero" style={{
          padding: "48px 48px",
          display: "flex", alignItems: "center", justifyContent: "space-between",
          gap: 32, flexWrap: "wrap",
          background: "linear-gradient(135deg, #FFFFFF 0%, #FAF8F4 100%)",
        }}>
          <div style={{ flex: "1 1 360px" }}>
            <div className="h2">Built for analysts, compliance teams, and anyone who sends an email a regulator might read.</div>
            <div className="body" style={{ color: "var(--midnight-soft)", marginTop: 10, maxWidth: 600 }}>
              Read-only Gmail access. Your emails never leave your account. Compliance happens at the edge.
            </div>
          </div>
          <a href={ctaHref} data-link className="neu-btn-redglow neu-btn-redglow-lg">
            <span>{ctaLabel}</span>
            <span style={{ opacity: 0.85 }}>→</span>
          </a>
        </div>
      </section>

      <footer style={{
        padding: "32px 48px 48px",
        display: "flex", alignItems: "center", justifyContent: "space-between",
        gap: 16, flexWrap: "wrap",
        borderTop: "1px solid var(--gray-whisper)",
        maxWidth: 1280, margin: "0 auto",
      }}>
        <div style={{ display: "flex", alignItems: "center", gap: 10 }}>
          <span style={{
            width: 9, height: 9, borderRadius: 999, background: "var(--red-core)",
            boxShadow: "0 0 0 3px rgba(230,57,70,0.18)",
          }}/>
          <span style={{ fontSize: 14, fontWeight: 600 }}>RedGone</span>
          <span className="small" style={{ color: "var(--midnight-soft)" }}>· compliance-first email</span>
        </div>
        <div className="mono" style={{ fontSize: 11, color: "var(--midnight-soft)", letterSpacing: "0.06em" }}>
          DEMO · BUILT FOR HACKATHON
        </div>
      </footer>
    </div>
  );
};

const ScoreRow = ({ label, pct, severity }) => {
  const color = severity === "high" ? "#E63946" : severity === "mid" ? "#F4811F" : "#F4C430";
  return (
    <div>
      <div style={{ display: "flex", justifyContent: "space-between", alignItems: "baseline", marginBottom: 6 }}>
        <span style={{ fontSize: 13, fontWeight: 500 }}>{label}</span>
        <span className="mono" style={{ fontSize: 11, color: "var(--midnight-soft)" }}>{Math.round(pct * 100)}%</span>
      </div>
      <div style={{ height: 6, background: "var(--white-soft)", borderRadius: 999, overflow: "hidden" }}>
        <div style={{
          width: `${pct * 100}%`, height: "100%",
          background: `linear-gradient(90deg, ${color}, ${color}cc)`,
        }}/>
      </div>
    </div>
  );
};

const FeatureCard = ({ tag, title, body, href }) => (
  <a href={href} data-link className="pillow" style={{
    padding: 32, textDecoration: "none", color: "inherit",
    display: "flex", flexDirection: "column",
    transition: "transform 220ms var(--ease-soft), box-shadow 220ms var(--ease-soft)",
    cursor: "pointer",
  }}
    onMouseEnter={(e) => {
      e.currentTarget.style.transform = "translateY(-3px)";
      e.currentTarget.style.boxShadow = "var(--pillow-shadow-hover)";
    }}
    onMouseLeave={(e) => {
      e.currentTarget.style.transform = "translateY(0)";
      e.currentTarget.style.boxShadow = "";
    }}
  >
    <span className="mono" style={{ fontSize: 11, color: "var(--midnight-soft)", letterSpacing: "0.08em" }}>{tag}</span>
    <div className="h3" style={{ marginTop: 12, fontSize: 22 }}>{title}</div>
    <div className="body" style={{ color: "var(--midnight-soft)", marginTop: 10, flex: 1 }}>{body}</div>
    <div style={{
      marginTop: 18, display: "inline-flex", alignItems: "center", gap: 6,
      color: "var(--midnight)", fontSize: 13, fontWeight: 600,
    }}>
      <span>See it</span>
      <span style={{ opacity: 0.6 }}>→</span>
    </div>
  </a>
);

// Hero card — swipeable. One point about the app per slide. Fixed height so
// the card stays visually constant as slides cycle. Prev/next handled by
// chevron buttons on the page sides (in LandingPage). Pointer events on
// the card itself add touch-/trackpad-/mouse-drag swipe navigation.
const SWIPE_THRESHOLD = 48;

const HeroCard = ({ idx, setIdx, total }) => {
  const startX = React.useRef(null);

  const onPointerDown = (e) => { startX.current = e.clientX; };
  const onPointerUp = (e) => {
    if (startX.current == null) return;
    const dx = e.clientX - startX.current;
    startX.current = null;
    if (Math.abs(dx) < SWIPE_THRESHOLD) return;
    setIdx((i) => (dx < 0 ? (i + 1) % total : (i - 1 + total) % total));
  };
  const onPointerCancel = () => { startX.current = null; };

  return (
    <div
      className="pillow hero float-in"
      onPointerDown={onPointerDown}
      onPointerUp={onPointerUp}
      onPointerCancel={onPointerCancel}
      style={{
        padding: 0, position: "relative",
        height: 580,
        display: "flex", flexDirection: "column",
        overflow: "hidden",
        touchAction: "pan-y", // let vertical scroll pass through; we handle horizontal
        userSelect: "none",
      }}>
      {/* Slide content */}
      <div style={{ flex: 1, padding: 36, display: "flex", flexDirection: "column", minHeight: 0, overflow: "hidden", position: "relative" }}>
        {idx === 0 && <SlideScore/>}
        {idx === 1 && <SlideHistorical/>}
        {idx === 2 && <SlideCompose/>}
        {idx === 3 && <SlideAutomation/>}
        {idx === 4 && <SlideInbox/>}
      </div>

      {/* Bottom controls — dots only. The prev/next chevrons live on the
          sides of the viewport, outside the card. */}
      <div style={{
        display: "flex", alignItems: "center", justifyContent: "center",
        padding: "14px 24px 22px",
        borderTop: "1px solid var(--gray-whisper)",
      }}>
        <div style={{ display: "flex", gap: 6 }}>
          {Array.from({ length: total }).map((_, i) => (
            <span key={i} style={{
              width: i === idx ? 22 : 6, height: 6, borderRadius: 999,
              background: i === idx ? "var(--red-core)" : "var(--gray-whisper)",
              transition: "all 220ms var(--ease-soft)",
            }}/>
          ))}
        </div>
      </div>
    </div>
  );
};

const SlideScore = () => (
  <>
    <div className="micro">YOUR COMPLIANCE SCORE · LIVE</div>
    {/* Angel floats as an overlay so it doesn't push the score / bars down. */}
    <span style={{
      position: "absolute", top: 0, right: 0,
      animation: "rg-float-soft 2.6s ease-in-out infinite",
      display: "inline-flex",
      pointerEvents: "none",
    }}>
      <AngelRobot size={138} view="front"/>
    </span>
    <div style={{ display: "flex", alignItems: "flex-end", gap: 10, marginTop: 70 }}>
      <div className="serif" style={{ fontSize: 132, lineHeight: 0.9, letterSpacing: "-0.04em", color: "var(--midnight)" }}>92</div>
      <div className="serif" style={{ fontSize: 32, color: "var(--midnight-soft)", lineHeight: 1.3, paddingBottom: 12 }}>/100</div>
    </div>
    <div style={{ display: "flex", alignItems: "center", gap: 8, marginTop: 6 }}>
      <span className="mono" style={{ fontSize: 12, color: "var(--success-mint)", fontWeight: 600 }}>↑ +4 this week</span>
    </div>
    <div style={{ marginTop: 20, display: "flex", flexDirection: "column", gap: 10 }}>
      <ScoreRow label="Unhedged performance claims" pct={0.92} severity="high"/>
      <ScoreRow label="Implied guarantees" pct={0.7} severity="high"/>
      <ScoreRow label="Missing risk disclaimers" pct={0.55} severity="mid"/>
      <ScoreRow label="Superlatives without basis" pct={0.35} severity="low"/>
    </div>
  </>
);

const SlideHistorical = () => (
  <>
    <div className="micro">BEFORE REDGONE · YOUR LAST 30 DAYS</div>
    <div className="body" style={{ color: "var(--midnight-soft)", marginTop: 12, maxWidth: 420 }}>
      Most analysts have never had a regulator's eye on their outbound. We score every send from your past 30 days against FCA, SEC, or your firm's policy.
    </div>
    <div style={{
      marginTop: 24, padding: 20, borderRadius: 14,
      background: "linear-gradient(135deg, rgba(230,57,70,0.08), rgba(230,57,70,0.02))",
      border: "1px solid rgba(230,57,70,0.18)",
    }}>
      <div style={{ display: "flex", alignItems: "flex-end", gap: 8 }}>
        <div className="serif" style={{ fontSize: 64, lineHeight: 0.9, color: "var(--red-deep)", letterSpacing: "-0.03em" }}>41</div>
        <div className="serif" style={{ fontSize: 22, color: "var(--midnight-soft)", paddingBottom: 6 }}>/100</div>
        <div className="small mono" style={{ marginLeft: 12, color: "var(--midnight-soft)", paddingBottom: 8 }}>SAMPLE BASELINE</div>
      </div>
      <div className="small" style={{ color: "var(--midnight-soft)", marginTop: 8 }}>
        18 implied guarantees · 14 missing risk disclaimers · 24 unhedged claims
      </div>
    </div>
    <div style={{ display: "flex", justifyContent: "center", alignItems: "center", gap: 8, marginTop: 18 }}>
      <span style={{ animation: "rg-float-tiny 2.2s ease-in-out infinite", display: "inline-flex" }}>
        <AngelRobot size={64} view="side"/>
      </span>
      <span style={{ animation: "rg-float-tiny 2.2s ease-in-out infinite 0.6s", display: "inline-flex" }}>
        <AngelRobot size={64} view="front"/>
      </span>
    </div>
    <div className="small mono" style={{ color: "var(--midnight-soft)", marginTop: "auto", paddingTop: 18, fontSize: 11 }}>
      If your starting score is low, you're not alone. Pre-RedGone is just no scrutiny.
    </div>
  </>
);

const SlideCompose = () => (
  <>
    <div className="micro">COMPOSE · LIVE LINT</div>
    <div className="body" style={{ color: "var(--midnight-soft)", marginTop: 12, maxWidth: 420 }}>
      Every draft is scored as you type. Phrases that violate the rules get flagged with the citation. Click Fix-All. The AI rewrites everything.
    </div>
    <div style={{
      marginTop: 20, padding: 16, borderRadius: 12,
      background: "var(--white-soft)", fontSize: 14, lineHeight: 1.5,
    }}>
      <div>This investment is{" "}
        <span style={{
          background: "rgba(230, 57, 70, 0.12)", borderRadius: 3, padding: "1px 4px",
          textDecoration: "underline wavy rgba(230,57,70,0.6)",
          textDecorationThickness: 2, textUnderlineOffset: 3,
        }}>safe and should return 10%</span>
      </div>
      <div className="small mono" style={{ marginTop: 8, color: "var(--red-deep)", fontSize: 11 }}>
        ↳ SEC RULE 482 · PERFORMANCE CLAIMS MUST INCLUDE RISK DISCLOSURES
      </div>
    </div>
    <div style={{
      marginTop: 12, padding: 14, borderRadius: 12,
      background: "rgba(52, 211, 153, 0.10)",
      border: "1px solid rgba(52, 211, 153, 0.20)",
    }}>
      <div className="small mono" style={{ color: "var(--success-mint)", fontWeight: 700, fontSize: 10 }}>SUGGESTED REWRITE</div>
      <div style={{ fontSize: 13, marginTop: 6, color: "var(--midnight)" }}>
        may target 8–12% returns over a 3-year horizon, subject to market conditions
      </div>
    </div>
    <div style={{ display: "flex", justifyContent: "space-between", alignItems: "center", marginTop: "auto", paddingTop: 16 }}>
      <span className="mono" style={{ fontSize: 11, color: "var(--midnight-soft)" }}>SCORE</span>
      <span className="serif" style={{ fontSize: 22, fontWeight: 600 }}>
        <span style={{ color: "var(--red-deep)" }}>64</span>
        <span style={{ color: "var(--midnight-soft)", margin: "0 8px" }}>→</span>
        <span style={{ color: "var(--success-mint)" }}>96</span>
      </span>
    </div>
  </>
);

const SlideAutomation = () => (
  <>
    <div className="micro">AUTOMATION · CONVERSATIONAL</div>
    <div className="body" style={{ color: "var(--midnight-soft)", marginTop: 12, maxWidth: 420 }}>
      Talk to RedGone about how your inbox should work. It writes the spec file. Edit any time, or upload a PDF.
    </div>
    <div style={{ marginTop: 20, display: "flex", flexDirection: "column", gap: 10 }}>
      <div className="pillow" style={{ padding: "12px 14px", maxWidth: 320, borderRadius: 14, fontSize: 13.5, lineHeight: 1.4 }}>
        <div className="micro" style={{ marginBottom: 4 }}>REDGONE</div>
        For account access requests — what should I do?
      </div>
      <div style={{ alignSelf: "flex-end", padding: "12px 14px", maxWidth: 320, borderRadius: 14, fontSize: 13.5, lineHeight: 1.4, background: "var(--midnight)", color: "#FFFFFF", boxShadow: "0 8px 24px rgba(10,22,40,0.18)" }}>
        Always send those to me. They're often urgent.
      </div>
      <div className="pillow" style={{ padding: "12px 14px", maxWidth: 320, borderRadius: 14, fontSize: 13.5, lineHeight: 1.4 }}>
        <div className="micro" style={{ marginBottom: 4 }}>REDGONE</div>
        Got it. Account access → Human Required. ✓
      </div>
    </div>
    <div className="small mono" style={{ color: "var(--midnight-soft)", marginTop: "auto", paddingTop: 16, fontSize: 11 }}>
      Spec is human-readable. Your compliance team can review.
    </div>
  </>
);

const SlideInbox = () => (
  <>
    <div className="micro">TODAY'S INBOX · 47 EMAILS</div>
    <div className="body" style={{ color: "var(--midnight-soft)", marginTop: 12, maxWidth: 420 }}>
      RedGone categorized everything. Only what actually needs you stays in front of you.
    </div>
    <div style={{ marginTop: 22, display: "flex", flexDirection: "column", gap: 10 }}>
      <BucketRow label="Auto-Reply OK" count={23} color="#34D399" hint="Settlement confirms, newsletters"/>
      <BucketRow label="AI Drafts"      count={18} color="#0A1628" hint="Drafted for your sign-off"/>
      <BucketRow label="Human Sends"    count={6}  color="#F4811F" hint="RFPs, press inquiries"/>
      <BucketRow label="Human Required" count={2}  color="#E63946" hint="Complaints, account access" highlight/>
    </div>
    <div className="small mono" style={{ color: "var(--midnight-soft)", marginTop: "auto", paddingTop: 16, fontSize: 11 }}>
      Only 2 emails actually need you today.
    </div>
  </>
);

const BucketRow = ({ label, count, color, hint, highlight }) => (
  <div style={{
    display: "flex", alignItems: "center", gap: 12,
    padding: "10px 14px", borderRadius: 12,
    background: highlight ? "rgba(230,57,70,0.06)" : "var(--white-soft)",
    border: highlight ? "1px solid rgba(230,57,70,0.18)" : "1px solid transparent",
  }}>
    <span style={{ width: 8, height: 8, borderRadius: 999, background: color, boxShadow: `0 0 0 4px ${color}22` }}/>
    <span style={{ fontSize: 13, fontWeight: 600 }}>{label}</span>
    <span className="mono" style={{ fontSize: 11, color: "var(--midnight-soft)", marginLeft: "auto" }}>{count}</span>
    <span className="small" style={{ color: "var(--midnight-soft)", fontSize: 11, marginLeft: 8, display: "none" }}>{hint}</span>
  </div>
);

window.LandingPage = LandingPage;
