/* Demo modal — auto-advancing animated slides, plays like a product video. */

const { useState, useEffect, useRef } = React;

const NAVY = "#0d1f2d";
const BRAND_BLUE = "#1a6fb5";
const SLIDE_MS = 4000;
const N_SLIDES = 6;

const Wordmark = ({ size = 28 }) => (
  <div style={{ display: "flex", alignItems: "center", gap: 10 }}>
    <span style={{
      width: size * 0.32, height: size * 0.32,
      borderRadius: "50%",
      background: BRAND_BLUE,
      boxShadow: `0 0 12px ${BRAND_BLUE}88`,
    }} />
    <span style={{
      fontFamily: "Syne, serif",
      fontWeight: 800,
      fontSize: size,
      letterSpacing: "-0.03em",
      color: "#ffffff",
    }}>NovuRoy</span>
  </div>
);

/* ---- Slides ---- */

const Slide1 = ({ active }) => (
  <div style={{
    height: "100%", display: "flex", flexDirection: "column",
    alignItems: "center", justifyContent: "center", gap: 28,
    transform: active ? "scale(1)" : "scale(0.98)",
    transition: "transform 1200ms cubic-bezier(.2,.8,.2,1)",
  }}>
    <Wordmark size={88} />
    <h2 style={{
      fontFamily: "Syne, serif", fontWeight: 700, fontSize: "clamp(36px, 4.4vw, 64px)",
      letterSpacing: "-0.03em", color: "#fff", textAlign: "center", margin: 0,
      maxWidth: 900,
    }}>Your personal AI college counselor.</h2>
    <p style={{ fontSize: 18, color: "rgba(255,255,255,0.55)", margin: 0 }}>
      Built for ambitious students 14–17.
    </p>
  </div>
);

const Slide2 = ({ active }) => (
  <div style={{
    height: "100%", display: "grid", gridTemplateColumns: "1fr 1fr",
    gap: 80, alignItems: "center", padding: "0 80px",
  }}>
    <div style={{
      display: "flex", flexDirection: "column", gap: 14,
      transform: active ? "translateX(0)" : "translateX(-20px)",
      opacity: active ? 1 : 0,
      transition: "all 800ms cubic-bezier(.2,.8,.2,1) 200ms",
    }}>
      <div style={{
        alignSelf: "flex-end", maxWidth: "85%",
        background: BRAND_BLUE, color: "#fff",
        padding: "14px 18px", borderRadius: "18px 18px 4px 18px",
        fontSize: 14, lineHeight: 1.5,
      }}>What are my chances at Cornell given my current GPA?</div>
      <div style={{
        alignSelf: "flex-start", maxWidth: "92%",
        background: "rgba(255,255,255,0.06)", border: "1px solid rgba(255,255,255,0.10)",
        color: "#fff",
        padding: "14px 18px", borderRadius: "18px 18px 18px 4px",
        fontSize: 14, lineHeight: 1.55,
      }}>
        With your 3.85 unweighted and an upward trend, Cornell CAS sits in your reach band.
        Your robotics work strengthens the engineering schools more — let's sharpen that angle.
      </div>
      <div style={{ display: "flex", gap: 6, marginTop: 6, paddingLeft: 8 }}>
        {[0,1,2].map(i => <span key={i} style={{
          width: 6, height: 6, borderRadius: "50%",
          background: BRAND_BLUE,
          opacity: 0.3,
          animation: active ? `dotPulse 1.4s ease-in-out ${i * 0.18}s infinite` : "none",
        }} />)}
      </div>
    </div>

    <div style={{
      transform: active ? "translateY(0)" : "translateY(20px)",
      opacity: active ? 1 : 0,
      transition: "all 800ms cubic-bezier(.2,.8,.2,1) 400ms",
    }}>
      <div style={{
        fontFamily: "JetBrains Mono, monospace", fontSize: 11, letterSpacing: "0.16em",
        textTransform: "uppercase", color: BRAND_BLUE, marginBottom: 22,
      }}>The Counselor</div>
      <h2 style={{
        fontFamily: "Syne, serif", fontWeight: 700, fontSize: "clamp(40px, 4.6vw, 68px)",
        letterSpacing: "-0.03em", lineHeight: 1, color: "#fff", marginBottom: 22,
      }}>It actually knows you.</h2>
      <p style={{ fontSize: 17, color: "rgba(255,255,255,0.6)", lineHeight: 1.6 }}>
        Answers come from your profile — not a generic playbook.
      </p>
    </div>
  </div>
);

const Slide3 = ({ active }) => {
  const milestones = [
    { x: 80,   label: "Sep 2025",  sub: "Start essays" },
    { x: 280,  label: "Oct 2025",  sub: "SAT retake" },
    { x: 480,  label: "Nov 2025",  sub: "Early decision" },
    { x: 680,  label: "Jan 2026",  sub: "Regular apps" },
    { x: 860,  label: "Apr 2026",  sub: "Decisions" },
  ];
  return (
    <div style={{
      height: "100%", display: "flex", flexDirection: "column",
      justifyContent: "center", padding: "0 80px",
    }}>
      <div style={{
        opacity: active ? 1 : 0, transform: active ? "translateY(0)" : "translateY(20px)",
        transition: "all 800ms cubic-bezier(.2,.8,.2,1) 200ms",
        textAlign: "center", marginBottom: 60,
      }}>
        <div style={{
          fontFamily: "JetBrains Mono, monospace", fontSize: 11, letterSpacing: "0.16em",
          textTransform: "uppercase", color: BRAND_BLUE, marginBottom: 22,
        }}>The Timeline</div>
        <h2 style={{
          fontFamily: "Syne, serif", fontWeight: 700, fontSize: "clamp(40px, 4.6vw, 68px)",
          letterSpacing: "-0.03em", lineHeight: 1, color: "#fff", marginBottom: 16,
        }}>Your map, alive.</h2>
        <p style={{ fontSize: 17, color: "rgba(255,255,255,0.6)" }}>
          Milestones move and rebuild as your goals shift.
        </p>
      </div>

      <svg viewBox="0 0 940 220" style={{ width: "100%", height: 220 }}>
        <defs>
          <linearGradient id="demoTimeline" x1="0" y1="0" x2="1" y2="0">
            <stop offset="0%" stopColor={BRAND_BLUE} stopOpacity="0" />
            <stop offset="50%" stopColor={BRAND_BLUE} stopOpacity="1" />
            <stop offset="100%" stopColor={BRAND_BLUE} stopOpacity="0" />
          </linearGradient>
        </defs>
        <path d="M 80 110 Q 280 40 480 110 T 860 110"
              stroke="rgba(255,255,255,0.08)" strokeWidth="1" fill="none" strokeDasharray="3 6" />
        <path d="M 80 110 Q 280 40 480 110 T 860 110"
              stroke="url(#demoTimeline)" strokeWidth="1.5" fill="none"
              strokeDasharray="1200"
              strokeDashoffset={active ? 0 : 1200}
              style={{ transition: "stroke-dashoffset 1800ms cubic-bezier(.2,.8,.2,1) 400ms" }} />
        {milestones.map((m, i) => {
          const t = i / (milestones.length - 1);
          const y = 110 + (i % 2 === 0 ? -1 : 1) * Math.sin(t * Math.PI) * 40;
          return (
            <g key={i} style={{
              opacity: active ? 1 : 0,
              transition: `opacity 400ms cubic-bezier(.2,.8,.2,1) ${800 + i * 180}ms`,
            }}>
              <circle cx={m.x} cy={y} r="14" fill="none" stroke={BRAND_BLUE} strokeOpacity="0.35" />
              <circle cx={m.x} cy={y} r="6" fill={BRAND_BLUE} />
              <text x={m.x} y={y - 28} fontSize="11" fontFamily="JetBrains Mono, monospace"
                    fill="rgba(255,255,255,0.85)" textAnchor="middle">{m.label}</text>
              <text x={m.x} y={y + 38} fontSize="12" fontFamily="Inter, sans-serif"
                    fill="rgba(255,255,255,0.55)" textAnchor="middle">{m.sub}</text>
            </g>
          );
        })}
      </svg>
    </div>
  );
};

const Slide4 = ({ active }) => (
  <div style={{
    height: "100%", display: "flex", flexDirection: "column",
    alignItems: "center", justifyContent: "center", gap: 36, padding: "0 80px",
  }}>
    <div style={{
      width: 140, height: 100, position: "relative",
      transform: active ? "translateY(0)" : "translateY(20px)",
      opacity: active ? 1 : 0,
      transition: "all 800ms cubic-bezier(.2,.8,.2,1) 200ms",
    }}>
      {/* Envelope body */}
      <div style={{
        position: "absolute", inset: 0,
        border: `1.5px solid ${BRAND_BLUE}`,
        borderRadius: 8,
        background: "rgba(26,111,181,0.08)",
      }} />
      {/* Flap (animates open) */}
      <div style={{
        position: "absolute", top: 0, left: 0, width: "100%", height: "55%",
        transformOrigin: "top center",
        transform: active ? "rotateX(160deg)" : "rotateX(0deg)",
        transition: "transform 1200ms cubic-bezier(.4,1.4,.4,1) 600ms",
        background: NAVY,
        borderTop: `1.5px solid ${BRAND_BLUE}`,
        borderLeft: `1.5px solid ${BRAND_BLUE}`,
        borderRight: `1.5px solid ${BRAND_BLUE}`,
        clipPath: "polygon(0 0, 50% 100%, 100% 0)",
      }} />
      {/* Letter peeking */}
      <div style={{
        position: "absolute", left: 14, right: 14, top: 16, bottom: 14,
        background: "#fff", borderRadius: 4,
        opacity: active ? 1 : 0,
        transform: active ? "translateY(-6px)" : "translateY(8px)",
        transition: "all 800ms cubic-bezier(.2,.8,.2,1) 1400ms",
        padding: 10,
      }}>
        <div style={{ height: 4, background: BRAND_BLUE, opacity: 0.5, marginBottom: 4, width: "70%" }} />
        <div style={{ height: 3, background: NAVY, opacity: 0.25, marginBottom: 3 }} />
        <div style={{ height: 3, background: NAVY, opacity: 0.25, marginBottom: 3 }} />
        <div style={{ height: 3, background: NAVY, opacity: 0.25, width: "60%" }} />
      </div>
    </div>
    <div style={{
      textAlign: "center",
      opacity: active ? 1 : 0,
      transform: active ? "translateY(0)" : "translateY(20px)",
      transition: "all 800ms cubic-bezier(.2,.8,.2,1) 800ms",
    }}>
      <div style={{
        fontFamily: "JetBrains Mono, monospace", fontSize: 11, letterSpacing: "0.16em",
        textTransform: "uppercase", color: BRAND_BLUE, marginBottom: 22,
      }}>The Overnight Agent</div>
      <h2 style={{
        fontFamily: "Syne, serif", fontWeight: 700, fontSize: "clamp(40px, 4.6vw, 68px)",
        letterSpacing: "-0.03em", lineHeight: 1, color: "#fff", marginBottom: 18,
      }}>One email. Every morning.</h2>
      <p style={{ fontSize: 17, color: "rgba(255,255,255,0.6)", maxWidth: 600, margin: "0 auto" }}>
        Your daily briefing — what to do today, what's coming, what you're missing.
      </p>
    </div>
  </div>
);

const PriceCard = ({ tier, price, lines, recommended, active, delay }) => (
  <div style={{
    border: recommended ? `1px solid ${BRAND_BLUE}` : "1px solid rgba(255,255,255,0.14)",
    borderRadius: 22, padding: 36,
    background: recommended ? `radial-gradient(circle at 0% 0%, ${BRAND_BLUE}25, rgba(255,255,255,0.02) 65%)` : "rgba(255,255,255,0.02)",
    display: "flex", flexDirection: "column", gap: 20, minHeight: 380,
    opacity: active ? 1 : 0,
    transform: active ? "translateY(0)" : "translateY(20px)",
    transition: `all 800ms cubic-bezier(.2,.8,.2,1) ${delay}ms`,
  }}>
    <div style={{
      fontFamily: "JetBrains Mono, monospace", fontSize: 11, letterSpacing: "0.16em",
      textTransform: "uppercase", color: recommended ? BRAND_BLUE : "rgba(255,255,255,0.5)",
    }}>{tier}</div>
    <div>
      <span style={{ fontFamily: "Syne, serif", fontSize: 56, fontWeight: 700, letterSpacing: "-0.03em", color: "#fff" }}>{price}</span>
      <span style={{ color: "rgba(255,255,255,0.4)", marginLeft: 6 }}>/month</span>
    </div>
    <div style={{ display: "flex", flexDirection: "column", gap: 12, fontSize: 14, color: "rgba(255,255,255,0.7)" }}>
      {lines.map((l, i) => (
        <div key={i} style={{ display: "flex", alignItems: "center", gap: 12 }}>
          <span style={{ width: 6, height: 6, borderRadius: "50%", background: BRAND_BLUE, boxShadow: `0 0 8px ${BRAND_BLUE}` }} />
          <span>{l}</span>
        </div>
      ))}
    </div>
  </div>
);

const Slide5 = ({ active }) => (
  <div style={{ height: "100%", display: "flex", flexDirection: "column", justifyContent: "center", padding: "0 100px" }}>
    <div style={{
      textAlign: "center", marginBottom: 48,
      opacity: active ? 1 : 0,
      transform: active ? "translateY(0)" : "translateY(20px)",
      transition: "all 800ms cubic-bezier(.2,.8,.2,1) 200ms",
    }}>
      <h2 style={{
        fontFamily: "Syne, serif", fontWeight: 700, fontSize: "clamp(36px, 4.4vw, 60px)",
        letterSpacing: "-0.03em", lineHeight: 1.05, color: "#fff",
      }}>Free to begin. Pro when you're serious.</h2>
    </div>
    <div style={{ display: "grid", gridTemplateColumns: "1fr 1fr", gap: 24, maxWidth: 880, margin: "0 auto", width: "100%" }}>
      <PriceCard tier="Free" price="$0" lines={["Limited messages", "5 timeline items", "Weekly email"]} active={active} delay={400} />
      <PriceCard tier="Pro" price="$19.99" lines={["Unlimited messages", "Unlimited timeline", "Daily email", "Essay review"]} recommended active={active} delay={550} />
    </div>
  </div>
);

const Slide6 = ({ active }) => (
  <div style={{
    height: "100%", display: "flex", flexDirection: "column",
    alignItems: "center", justifyContent: "center", gap: 36,
  }}>
    <div style={{
      transform: active ? "scale(1)" : "scale(0.92)",
      opacity: active ? 1 : 0,
      transition: "all 1000ms cubic-bezier(.2,.8,.2,1) 100ms",
    }}>
      <Wordmark size={88} />
    </div>
    <h2 style={{
      fontFamily: "Syne, serif", fontWeight: 700, fontSize: "clamp(36px, 4.4vw, 64px)",
      letterSpacing: "-0.03em", color: "#fff", textAlign: "center", margin: 0,
      maxWidth: 900,
      opacity: active ? 1 : 0,
      transform: active ? "translateY(0)" : "translateY(16px)",
      transition: "all 800ms cubic-bezier(.2,.8,.2,1) 400ms",
    }}>Start building your future today.</h2>
    <button style={{
      fontFamily: "Inter, sans-serif", fontSize: 15, fontWeight: 500,
      padding: "16px 32px", borderRadius: 999,
      background: BRAND_BLUE, color: "#fff", border: "none", cursor: "pointer",
      boxShadow: `0 0 0 0 ${BRAND_BLUE}, 0 0 40px ${BRAND_BLUE}80, 0 10px 30px rgba(0,0,0,0.4)`,
      animation: active ? "ctaGlow 2.4s ease-in-out infinite" : "none",
      opacity: active ? 1 : 0,
      transform: active ? "translateY(0)" : "translateY(16px)",
      transition: "all 800ms cubic-bezier(.2,.8,.2,1) 700ms",
    }}>Start for free →</button>
  </div>
);

const SLIDES = [Slide1, Slide2, Slide3, Slide4, Slide5, Slide6];

/* ---- Modal shell ---- */

const DemoModal = ({ open, onClose }) => {
  const [idx, setIdx] = useState(0);
  const [progress, setProgress] = useState(0);
  const startRef = useRef(0);
  const rafRef = useRef(0);

  useEffect(() => {
    if (!open) return;
    setIdx(0);
    setProgress(0);
    startRef.current = performance.now();

    const total = SLIDE_MS * N_SLIDES;
    const tick = (now) => {
      const elapsed = now - startRef.current;
      const p = Math.min(elapsed / total, 1);
      setProgress(p);
      const newIdx = Math.min(Math.floor(elapsed / SLIDE_MS), N_SLIDES - 1);
      setIdx(newIdx);
      if (p < 1) rafRef.current = requestAnimationFrame(tick);
    };
    rafRef.current = requestAnimationFrame(tick);

    const onKey = (e) => { if (e.key === "Escape") onClose(); };
    window.addEventListener("keydown", onKey);

    // Lock body scroll
    const prevOverflow = document.body.style.overflow;
    document.body.style.overflow = "hidden";

    return () => {
      cancelAnimationFrame(rafRef.current);
      window.removeEventListener("keydown", onKey);
      document.body.style.overflow = prevOverflow;
    };
  }, [open, onClose]);

  if (!open) return null;

  return (
    <div style={{
      position: "fixed", inset: 0, zIndex: 1000,
      background: NAVY,
      animation: "demoFadeIn 360ms cubic-bezier(.2,.8,.2,1)",
    }}>
      {/* Page-behind blur veil */}
      <div style={{
        position: "absolute", inset: 0,
        backdropFilter: "blur(20px)",
        WebkitBackdropFilter: "blur(20px)",
        background: `radial-gradient(ellipse at 50% 50%, rgba(26,111,181,0.10) 0%, transparent 60%), ${NAVY}`,
      }} />

      {/* Top-left wordmark */}
      <div style={{ position: "absolute", top: 28, left: 32, zIndex: 2 }}>
        <Wordmark size={22} />
      </div>

      {/* Close button */}
      <button onClick={onClose} aria-label="Close demo" style={{
        position: "absolute", top: 22, right: 24, zIndex: 2,
        width: 40, height: 40, borderRadius: "50%",
        background: "rgba(255,255,255,0.06)",
        border: "1px solid rgba(255,255,255,0.14)",
        color: "#fff", fontSize: 22, lineHeight: 1, cursor: "pointer",
        display: "flex", alignItems: "center", justifyContent: "center",
        transition: "background 200ms",
      }}
        onMouseEnter={(e) => e.currentTarget.style.background = "rgba(255,255,255,0.12)"}
        onMouseLeave={(e) => e.currentTarget.style.background = "rgba(255,255,255,0.06)"}
      >×</button>

      {/* Slide stage */}
      <div style={{ position: "absolute", inset: 0, zIndex: 1 }}>
        {SLIDES.map((Slide, i) => (
          <div key={i} style={{
            position: "absolute", inset: 0,
            opacity: i === idx ? 1 : 0,
            pointerEvents: i === idx ? "auto" : "none",
            transition: "opacity 700ms cubic-bezier(.2,.8,.2,1)",
          }}>
            <Slide active={i === idx} />
          </div>
        ))}
      </div>

      {/* Slide ticks */}
      <div style={{
        position: "absolute", bottom: 32, left: 0, right: 0,
        display: "flex", gap: 8, justifyContent: "center", zIndex: 2,
      }}>
        {Array.from({ length: N_SLIDES }).map((_, i) => (
          <span key={i} style={{
            width: i === idx ? 24 : 6, height: 2, borderRadius: 2,
            background: i <= idx ? "#ffffff" : "rgba(255,255,255,0.22)",
            transition: "all 400ms cubic-bezier(.2,.8,.2,1)",
          }} />
        ))}
      </div>

      {/* Bottom progress bar */}
      <div style={{
        position: "absolute", bottom: 0, left: 0, right: 0, height: 2,
        background: "rgba(255,255,255,0.08)", zIndex: 2,
      }}>
        <div style={{
          width: `${progress * 100}%`, height: "100%",
          background: BRAND_BLUE,
          boxShadow: `0 0 12px ${BRAND_BLUE}`,
          transition: "width 60ms linear",
        }} />
      </div>

      <style>{`
        @keyframes demoFadeIn { from { opacity: 0; } to { opacity: 1; } }
        @keyframes dotPulse {
          0%, 100% { opacity: 0.25; transform: translateY(0); }
          50% { opacity: 1; transform: translateY(-3px); }
        }
        @keyframes ctaGlow {
          0%, 100% { box-shadow: 0 0 40px ${BRAND_BLUE}80, 0 10px 30px rgba(0,0,0,0.4); }
          50% { box-shadow: 0 0 70px ${BRAND_BLUE}cc, 0 10px 30px rgba(0,0,0,0.5); }
        }
      `}</style>
    </div>
  );
};

window.DemoModal = DemoModal;
