// Variation 1: Orbit - big headline, floating pastel widgets, bento features
// With hero entrance animation, floating drift, and scroll-reveal sections.

const { useEffect: useEffectVO, useRef: useRefVO } = React;

// Hook: reveal element when it scrolls into view
function useReveal() {
  const ref = useRefVO(null);
  useEffectVO(() => {
    const el = ref.current;
    if (!el) return;
    if (typeof IntersectionObserver === "undefined") {
      el.classList.add("is-in");
      return;
    }
    const io = new IntersectionObserver((entries) => {
      entries.forEach((e) => {
        if (e.isIntersecting) {
          e.target.classList.add("is-in");
          io.unobserve(e.target);
        }
      });
    }, { threshold: 0.12, rootMargin: "0px 0px -60px 0px" });
    io.observe(el);
    return () => io.disconnect();
  }, []);
  return ref;
}

function Reveal({ as: Tag = "div", className = "", stagger = false, children, ...rest }) {
  const ref = useReveal();
  const cls = (stagger ? "vo-reveal-stagger " : "vo-reveal ") + className;
  return <Tag ref={ref} className={cls} {...rest}>{children}</Tag>;
}

function SeamlessTextLoop({ texts, interval = 3000 }) {
  const [index, setIndex] = React.useState(0);

  React.useEffect(() => {
    const timer = setInterval(() => {
      setIndex(prev => (prev + 1) % texts.length);
    }, interval);
    return () => clearInterval(timer);
  }, [texts.length, interval]);

  return (
    <span style={{ display: "inline-grid", gridTemplateColumns: "1fr", alignItems: "center" }}>
      {texts.map((text, i) => {
        const isActive = i === index;
        const isPrev = i === (index - 1 + texts.length) % texts.length;

        let yPos = "150%";
        if (isActive) yPos = "0%";
        else if (isPrev) yPos = "-150%";

        const words = text.split(" ");

        return (
          <span
            key={i}
            aria-hidden={!isActive}
            style={{
              gridArea: "1 / 1",
              pointerEvents: isActive ? "auto" : "none",
              display: "flex",
              justifyContent: "center",
              flexWrap: "wrap",
              // We keep it in the DOM but hide it when not animating to prevent artifacts
              visibility: (isActive || isPrev) ? "visible" : "hidden",
            }}
          >
            {words.map((word, wIdx) => {
              const delay = wIdx * 0.05;
              return (
                <span
                  key={wIdx}
                  style={{
                    display: "inline-block",
                    overflow: "hidden",
                    verticalAlign: "bottom",
                    lineHeight: 1.1,
                    marginRight: wIdx === words.length - 1 ? "0" : "0.25em",
                    paddingBottom: "0.2em",
                    marginBottom: "-0.2em",
                  }}
                >
                  <span
                    style={{
                      display: "inline-block",
                      transform: `translateY(${yPos})`,
                      opacity: isActive ? 1 : 0,
                      transition: isActive
                        ? `transform 0.8s cubic-bezier(0.16, 1, 0.3, 1) ${delay}s, opacity 0s 0s`
                        : `transform 0.8s cubic-bezier(0.16, 1, 0.3, 1) ${delay}s, opacity 0s ${0.8 + delay}s`,
                    }}
                  >
                    {word}
                  </span>
                </span>
              );
            })}
          </span>
        );
      })}
    </span>
  );
}

function OrbitVariant({ t, onJoin, joinState, waitlistCount }) {
  return (
    <>
      {/* HERO */}
      <section className="vo-hero">
        <div className="vo-floats">
          <div className="vo-f1 vo-float"><div className="vo-float-inner">
            <TimerCard name="Harper Studio" hours="02:14:37" rate={75} color="sky" earned="168.75" running={true} />
          </div></div>
          <div className="vo-f2 vo-float"><div className="vo-float-inner">
            <StatWidget label={t.widget_earned} value="$324.80" sub="3 sessions today" color="mint" icon="$" />
          </div></div>
          <div className="vo-f3 vo-float"><div className="vo-float-inner">
            <NudgeWidget />
          </div></div>
          <div className="vo-f4 vo-float"><div className="vo-float-inner">
            <AFKWidget t={t} />
          </div></div>
          <div className="vo-f6 vo-float" style={{ width: 240 }}><div className="vo-float-inner">
            <MiniChart color="lavender" />
          </div></div>
        </div>

        <div className="vo-hero-inner">
          <div style={{ textAlign: "center", position: "relative", zIndex: 2 }}>
            <div className="vo-eyebrow vo-rise vo-rise-1">
              <span className="vo-eyebrow-dot" />
              <span>{t.hero_eyebrow}</span>
            </div>
          </div>

          <h1 className="vo-headline" style={{ position: "relative", zIndex: 2 }}>
            <span className="vo-line">{t.hero_title_1}</span>
            <span className="vo-line">
              <span className="vo-accent" style={{ display: "inline-block" }}>
                <SeamlessTextLoop 
                  texts={[t.hero_title_loop_1, t.hero_title_loop_2, t.hero_title_loop_3]} 
                  interval={3500} 
                />
              </span>
            </span>
          </h1>

          <p className="vo-sub vo-rise vo-rise-3" style={{ position: "relative", zIndex: 2 }}>{t.hero_sub}</p>

          <div className="vo-form-wrap vo-rise vo-rise-4" style={{ position: "relative", zIndex: 2 }}>
            <EmailForm t={t} onJoin={onJoin} joinState={joinState} variant="orbit" />
          </div>

          <div className="vo-social vo-rise vo-rise-5" style={{ position: "relative", zIndex: 2 }}>
            <div className="vo-avatars">
              <span style={{ background: "oklch(86% 0.08 20)" }} />
              <span style={{ background: "oklch(86% 0.08 160)" }} />
              <span style={{ background: "oklch(86% 0.08 230)" }} />
              <span style={{ background: "oklch(86% 0.1 95)" }} />
              <span style={{ background: "oklch(86% 0.08 290)" }} />
            </div>
            <span><span className="mono" style={{ color: "var(--ink)" }}>{waitlistCount.toLocaleString("en-US")}</span> {t.waitlist_count}</span>
          </div>
        </div>
      </section>

      {/* BENTO FEATURES */}
      <section className="vo-section" id="features">
        <Reveal className="vo-section-head">
          <div className="vo-section-eye">{t.section_features_eyebrow}</div>
          <h2 className="vo-section-title">{t.section_features_title}</h2>
          <p className="vo-section-sub">{t.section_features_sub}</p>
        </Reveal>

        <Reveal stagger className="vo-bento">
          {/* 01 · Timers - big tile */}
          <div className="vo-bento-card span-4 tall bg-tint" style={{ "--tint-hue": 230 }}>
            <div className="vo-bento-label">01 · Timers</div>
            <h3 className="vo-bento-title">{t.f1_title}</h3>
            <p className="vo-bento-body">{t.f1_body}</p>
            <div className="vo-bento-visual" style={{ gap: 12, flexWrap: "wrap" }}>
              <TimerCard name="Harper" hours="02:14:37" rate={75} color="sky" earned="168.75" running={true} style={{ transform: "rotate(-3deg)" }} />
              <div style={{ display: "flex", flexDirection: "column", gap: 10, transform: "translateY(10px)" }}>
                <TimerCard name="Nilsson Co." hours="00:42:12" rate={60} color="mint" earned="42.20" running={false} style={{ width: 200, transform: "rotate(2deg)" }} />
              </div>
            </div>
          </div>

          {/* 02 · Invoices */}
          <div className="vo-bento-card span-2 tall">
            <div className="vo-bento-label">02 · Invoicing</div>
            <h3 className="vo-bento-title">{t.f2_title}</h3>
            <p className="vo-bento-body">{t.f2_body}</p>
            <div className="vo-bento-visual">
              <InvoiceWidget style={{ transform: "rotate(-2deg)" }} />
            </div>
          </div>

          {/* 03 · Projects & tasks calendar */}
          <div className="vo-bento-card span-2">
            <div className="vo-bento-label">03 · Projects</div>
            <h3 className="vo-bento-title">{t.f3_title}</h3>
            <p className="vo-bento-body">{t.f3_body}</p>
            <div className="vo-bento-visual">
              <CalendarWidget style={{ transform: "rotate(-2deg)", marginTop: 4 }} />
            </div>
          </div>

          {/* 04 · Analytics */}
          <div className="vo-bento-card span-2 bg-tint" style={{ "--tint-hue": 290 }}>
            <div className="vo-bento-label">04 · Analytics</div>
            <h3 className="vo-bento-title">{t.f4_title}</h3>
            <p className="vo-bento-body">{t.f4_body}</p>
            <div className="vo-bento-visual">
              <MiniChart color="lavender" style={{ transform: "rotate(1deg)" }} />
            </div>
          </div>

          {/* 05 · App detection */}
          <div className="vo-bento-card span-2 bg-tint" style={{ "--tint-hue": 160 }}>
            <div className="vo-bento-label">05 · Detection</div>
            <h3 className="vo-bento-title">{t.f5_title}</h3>
            <p className="vo-bento-body">{t.f5_body}</p>
            <div className="vo-bento-visual">
              <AppDetectWidget t={t} style={{ transform: "rotate(-1deg)", width: 260 }} />
            </div>
          </div>

          {/* 06 · AFK reclaim - FULL ROW */}
          <div className="vo-bento-card span-6 bg-tint" style={{ "--tint-hue": 20 }}>
            <div style={{ display: "grid", gridTemplateColumns: "1fr 1.2fr", gap: 28, alignItems: "center" }}>
              <div>
                <div className="vo-bento-label">06 · Time reclaim</div>
                <h3 className="vo-bento-title" style={{ fontSize: 32 }}>{t.f6_title}</h3>
                <p className="vo-bento-body" style={{ maxWidth: 420, fontSize: 15 }}>{t.f6_body}</p>
              </div>
              <div style={{ display: "flex", gap: 20, justifyContent: "center", alignItems: "center", flexWrap: "wrap" }}>
                <AFKWidget t={t} style={{ transform: "rotate(-2deg)" }} />
                <NudgeWidget style={{ transform: "translateY(10px) rotate(1deg)" }} />
              </div>
            </div>
          </div>
        </Reveal>
      </section>

      {/* HOW IT WORKS */}
      <section className="vo-how" id="how">
        <div className="vo-how-inner">
          <Reveal className="vo-section-head" style={{ marginBottom: 0, textAlign: "center" }}>
            <div className="vo-section-eye">{t.how_eyebrow}</div>
            <h2 className="vo-section-title">{t.how_title}</h2>
          </Reveal>
          <Reveal stagger className="vo-steps">
            <div className="vo-step">
              <div className="vo-step-num">STEP 01</div>
              <div className="vo-step-title">{t.step1_title}</div>
              <div className="vo-step-body">{t.step1_body}</div>
              <div className="vo-step-visual">
                <div style={{ display: "flex", gap: 4 }}>
                  {["rose","butter","mint","sky","lavender"].map(c => (
                    <div key={c} style={{ width: 32, height: 32, borderRadius: 10, background: LUCHI_PASTELS[c].bg, border: `1px solid ${LUCHI_PASTELS[c].ring}` }} />
                  ))}
                </div>
              </div>
            </div>
            <div className="vo-step">
              <div className="vo-step-num">STEP 02</div>
              <div className="vo-step-title">{t.step2_title}</div>
              <div className="vo-step-body">{t.step2_body}</div>
              <div className="vo-step-visual">
                <div className="mono" style={{ fontSize: 28, letterSpacing: "-0.02em" }}>00:42:18</div>
              </div>
            </div>
            <div className="vo-step">
              <div className="vo-step-num">STEP 03</div>
              <div className="vo-step-title">{t.step3_title}</div>
              <div className="vo-step-body">{t.step3_body}</div>
              <div className="vo-step-visual">
                <InvoiceWidget style={{ transform: "scale(0.75)", marginTop: -20 }} />
              </div>
            </div>
          </Reveal>
        </div>
      </section>

      {/* FOCUS GUARDRAILS */}
      <section className="vo-focus">
        <Reveal className="vo-focus-copy">
          <div className="vo-section-eye">{t.focus_eyebrow}</div>
          <h2 className="vo-section-title" style={{ textAlign: "left" }}>{t.focus_title}</h2>
          <p style={{ color: "var(--muted)", fontSize: 17, lineHeight: 1.55, textWrap: "pretty" }}>{t.focus_body}</p>
          <div className="vo-focus-tags">
            <span className="vo-focus-tag">● {t.focus_tag1}</span>
            <span className="vo-focus-tag">● {t.focus_tag2}</span>
            <span className="vo-focus-tag">● {t.focus_tag3}</span>
          </div>
        </Reveal>
        <Reveal className="vo-focus-stage">
          <div className="vo-fs1"><AppDetectWidget t={t} /></div>
          <div className="vo-fs2"><AFKWidget t={t} /></div>
          <div className="vo-fs3"><NudgeWidget /></div>
          <div className="vo-fs4"><StatWidget label="FOCUS STREAK" value="4h 22m" sub="longest this week" color="butter" icon="★" /></div>
        </Reveal>
      </section>

      {/* FINAL CTA */}
      <section className="vo-final" id="join">
        <Reveal className="vo-final-inner">
          <h2 className="vo-final-title">
            {t.final_title_1 ? (
              <>{t.final_title_1} <span className="vo-accent">{t.final_title_accent}</span></>
            ) : (
              t.final_title
            )}
          </h2>
          <p className="vo-final-sub">{t.final_sub}</p>
          <div className="vo-form-wrap">
            <EmailForm t={t} onJoin={onJoin} joinState={joinState} variant="orbit" />
          </div>
        </Reveal>
      </section>

      <footer className="vo-footer">
        <div className="vo-footer-inner">
          <div className="vo-footer-brand">
             <div className="lt-logo">
               <span className="lt-logo-mark">L</span>
               <span style={{ color: "var(--ink)" }}>LuchiTimer</span>
             </div>
             <p>{t.footer_made}</p>
          </div>
          <div className="vo-footer-links">
            <a href="privacy.html">{t.footer_privacy}</a>
            <a href="terms.html">{t.footer_terms}</a>
            <a href="cookies.html">{t.footer_cookies}</a>
          </div>
          <div className="vo-footer-social">
            <a href="#" aria-label="X (Twitter)">
              <svg width="16" height="16" viewBox="0 0 24 24" fill="currentColor">
                <path d="M18.244 2.25h3.308l-7.227 8.26 8.502 11.24H16.17l-5.214-6.817L4.99 21.75H1.68l7.73-8.835L1.254 2.25H8.08l4.713 6.231zm-1.161 17.52h1.833L7.084 4.126H5.117z"></path>
              </svg>
            </a>
            <a href="#" aria-label="Instagram">
              <svg width="16" height="16" viewBox="0 0 24 24" fill="currentColor">
                <path d="M12 2.163c3.204 0 3.584.012 4.85.07 3.252.148 4.771 1.691 4.919 4.919.058 1.265.069 1.645.069 4.849 0 3.205-.012 3.584-.069 4.849-.149 3.225-1.664 4.771-4.919 4.919-1.266.058-1.644.07-4.85.07-3.204 0-3.584-.012-4.849-.07-3.26-.149-4.771-1.699-4.919-4.92-.058-1.265-.07-1.644-.07-4.849 0-3.204.013-3.583.07-4.849.149-3.227 1.664-4.771 4.919-4.919 1.266-.057 1.645-.069 4.849-.069zM12 0C8.741 0 8.333.014 7.053.072 2.695.272.273 2.69.073 7.052.014 8.333 0 8.741 0 12c0 3.259.014 3.668.072 4.948.2 4.358 2.618 6.78 6.98 6.98C8.333 23.986 8.741 24 12 24c3.259 0 3.668-.014 4.948-.072 4.354-.2 6.782-2.618 6.979-6.98.059-1.28.073-1.689.073-4.948 0-3.259-.014-3.667-.072-4.947-.196-4.354-2.617-6.78-6.979-6.98C15.668.014 15.259 0 12 0zm0 5.838a6.162 6.162 0 100 12.324 6.162 6.162 0 000-12.324zM12 16a4 4 0 110-8 4 4 0 010 8zm3.98-10.822a1.44 1.44 0 100 2.881 1.44 1.44 0 000-2.881z"></path>
              </svg>
            </a>
          </div>
        </div>
      </footer>
    </>
  );
}

window.OrbitVariant = OrbitVariant;
