// Method section (5 steps) — sticky-scroll storytelling

function MethodSection() {
  const [active, setActive] = useState(0);
  const stepRefs = useRef([]);

  useEffect(() => {
    const observers = [];
    stepRefs.current.forEach((el, i) => {
      if (!el) return;
      const io = new IntersectionObserver(
        ([e]) => {
          if (e.isIntersecting) setActive(i);
        },
        { threshold: 0.55, rootMargin: "-20% 0px -30% 0px" }
      );
      io.observe(el);
      observers.push(io);
    });
    return () => observers.forEach((o) => o.disconnect());
  }, []);

  return (
    <section id="metodo" style={{ padding: "140px 0 80px", position: "relative" }}>
      <div className="container">
        {/* Section heading */}
        <Reveal stagger>
          <div style={{ display: "flex", alignItems: "baseline", gap: 14, marginBottom: 22 }}>
            <Mono>El método · 5 fases</Mono>
            <span style={{ flex: 1, height: 1, background: "var(--line)" }} />
          </div>
          <h2
            className="display"
            style={{
              fontSize: "clamp(40px, 6vw, 80px)",
              maxWidth: 980,
              marginBottom: 28,
            }}
          >
            Cinco pasos, en orden.<br />
            Como lo haría <em>una agencia</em>,<br />
            pero contigo al mando.
          </h2>
          <p
            style={{
              fontSize: 19,
              lineHeight: 1.55,
              color: "var(--ink-soft)",
              maxWidth: 680,
              marginBottom: 64,
            }}
          >
            Las herramientas genéricas te dejan delante de una hoja en blanco. El método
            Media Planning Canvas te guía paso a paso —del contexto al plan de
            medios— exactamente igual que aprenderías en una escuela de negocios.
          </p>
        </Reveal>

        <div
          style={{
            display: "grid",
            gridTemplateColumns: "minmax(0, 1fr) minmax(0, 1.25fr)",
            gap: 80,
            alignItems: "start",
          }}
          className="method-grid"
        >
          {/* Left: step list */}
          <div>
            <div style={{ display: "flex", flexDirection: "column", gap: 80 }}>
              {METHOD_STEPS.map((step, i) => (
                <div
                  key={step.n}
                  ref={(el) => (stepRefs.current[i] = el)}
                  style={{ minHeight: 320 }}
                >
                  <div
                    style={{
                      display: "flex",
                      alignItems: "center",
                      gap: 14,
                      marginBottom: 20,
                    }}
                  >
                    <span
                      style={{
                        fontFamily: "var(--font-mono)",
                        fontSize: 12,
                        letterSpacing: "0.16em",
                        color: step.accent,
                        fontWeight: 500,
                      }}
                    >
                      FASE {step.n}
                    </span>
                    <span style={{ flex: 1, height: 1, background: step.accent, opacity: 0.4 }} />
                    <span
                      style={{
                        fontFamily: "var(--font-mono)",
                        fontSize: 11,
                        letterSpacing: "0.14em",
                        color: "var(--ink-muted)",
                      }}
                    >
                      {step.name.toUpperCase()}
                    </span>
                  </div>
                  <h3
                    className="display"
                    style={{ fontSize: "clamp(30px, 4vw, 48px)", marginBottom: 18 }}
                  >
                    {step.title}
                  </h3>
                  <p
                    style={{
                      fontSize: 17,
                      lineHeight: 1.6,
                      color: "var(--ink-soft)",
                      marginBottom: 22,
                    }}
                  >
                    {step.body}
                  </p>
                  <ul
                    style={{
                      listStyle: "none",
                      padding: 0,
                      margin: 0,
                      display: "flex",
                      flexDirection: "column",
                      gap: 10,
                    }}
                  >
                    {step.bullets.map((b, j) => (
                      <li
                        key={j}
                        style={{
                          display: "flex",
                          gap: 12,
                          alignItems: "flex-start",
                          fontSize: 15,
                          color: "var(--ink-soft)",
                        }}
                      >
                        <span
                          style={{
                            width: 16,
                            height: 16,
                            borderRadius: 4,
                            background: step.accent,
                            flexShrink: 0,
                            marginTop: 3,
                            display: "grid",
                            placeItems: "center",
                            color: "white",
                            fontSize: 10,
                          }}
                        >
                          ✓
                        </span>
                        {b}
                      </li>
                    ))}
                  </ul>

                  <div style={{ marginTop: 28 }}>
                    <a
                      href="https://app.mediaplanningcanvas.com/registro"
                      className="btn btn-ink"
                      style={{ fontSize: 14, padding: "11px 22px" }}
                    >
                      Empezar ahora gratis →
                    </a>
                  </div>
                </div>
              ))}
            </div>
          </div>

          {/* Right: sticky screenshot */}
          <div className="method-sticky" style={{ position: "sticky", top: 100, height: "fit-content" }}>
            <div style={{ marginBottom: 16, display: "flex", alignItems: "center", gap: 12 }}>
              <div
                style={{
                  display: "flex",
                  gap: 6,
                }}
              >
                {METHOD_STEPS.map((_, i) => (
                  <span
                    key={i}
                    style={{
                      width: i === active ? 28 : 10,
                      height: 4,
                      borderRadius: 2,
                      background: i === active ? METHOD_STEPS[active].accent : "var(--line-strong)",
                      transition: "all 0.4s ease",
                    }}
                  />
                ))}
              </div>
              <span
                className="mono"
                style={{ color: METHOD_STEPS[active].accent, fontWeight: 500 }}
              >
                FASE {METHOD_STEPS[active].n} · {METHOD_STEPS[active].name}
              </span>
            </div>

            <div
              style={{
                position: "relative",
                borderRadius: 14,
                overflow: "hidden",
                border: "1px solid var(--line)",
                boxShadow:
                  "0 30px 60px -25px rgba(14,34,54,0.32), 0 6px 18px -6px rgba(14,34,54,0.12)",
                background: "white",
                aspectRatio: "10 / 12",
              }}
            >
              {METHOD_STEPS.map((s, i) => (
                <img
                  key={s.n}
                  src={s.img}
                  alt={s.name}
                  style={{
                    position: "absolute",
                    inset: 0,
                    width: "100%",
                    height: "100%",
                    objectFit: "cover",
                    objectPosition: "top",
                    opacity: i === active ? 1 : 0,
                    transition: "opacity 0.6s ease",
                  }}
                />
              ))}
            </div>
          </div>
        </div>

        <style>{`
          @media (max-width: 960px) {
            .method-grid { grid-template-columns: 1fr !important; gap: 32px !important; }
            .method-sticky { position: relative !important; top: 0 !important; }
          }
        `}</style>
      </div>
    </section>
  );
}

function PlayBadge({ accent = "var(--accent)" }) {
  return (
    <span
      style={{
        width: 36,
        height: 36,
        borderRadius: 8,
        background: "var(--ink)",
        color: "var(--cream)",
        display: "grid",
        placeItems: "center",
        position: "relative",
      }}
    >
      <svg width="11" height="12" viewBox="0 0 11 12" fill="none">
        <path d="M0 0L11 6L0 12V0Z" fill="currentColor" />
      </svg>
      <span
        style={{
          position: "absolute",
          inset: -3,
          borderRadius: 10,
          border: `1.5px solid ${accent}`,
          opacity: 0.5,
        }}
      />
    </span>
  );
}

Object.assign(window, { MethodSection, PlayBadge });
