/* ScrollRail — vertical section indicator on the right. window.ScrollRail */
(function () {
  const { useState, useEffect, useRef } = React;

  window.ScrollRail = function ScrollRail({ items }) {
    const [active, setActive] = useState(0);
    const [hover, setHover] = useState(null);
    const elsRef = useRef([]);

    useEffect(() => {
      elsRef.current = items.map((it) => document.getElementById(it.id));
      let raf = 0;
      const onScroll = () => {
        cancelAnimationFrame(raf);
        raf = requestAnimationFrame(() => {
          const mid = window.scrollY + window.innerHeight * 0.32;
          let idx = 0;
          elsRef.current.forEach((el, i) => { if (el && el.offsetTop <= mid) idx = i; });
          setActive(idx);
        });
      };
      onScroll();
      window.addEventListener("scroll", onScroll, { passive: true });
      window.addEventListener("resize", onScroll);
      return () => { window.removeEventListener("scroll", onScroll); window.removeEventListener("resize", onScroll); };
    }, [items.length, items.map(i => i.id).join("|")]);

    return (
      <nav aria-label="Section navigation" className="ema-rail"
        style={{ position: "fixed", right: 18, top: "50%", transform: "translateY(-50%)", zIndex: 80,
          display: "flex", flexDirection: "column", gap: 2, padding: "10px 8px",
          background: "color-mix(in srgb, var(--surface) 78%, transparent)",
          backdropFilter: "blur(8px) saturate(140%)",
          WebkitBackdropFilter: "blur(8px) saturate(140%)",
          border: "1px solid var(--hairline)", borderRadius: 99,
          boxShadow: "0 14px 28px -18px rgba(14,31,58,.25)" }}>
        {items.map((it, i) => {
          const on = i === active;
          const hov = hover === i;
          return (
            <a key={it.id} href={"#" + it.id}
              onMouseEnter={() => setHover(i)} onMouseLeave={() => setHover(null)}
              aria-label={it.label}
              style={{ position: "relative", padding: "6px 8px", display: "flex", alignItems: "center", justifyContent: "flex-end", gap: 10 }}>
              {/* label appears on hover */}
              <span style={{
                position: "absolute", right: 28, top: "50%", transform: "translateY(-50%)",
                fontFamily: "var(--mono)", fontSize: 11, letterSpacing: ".08em", textTransform: "uppercase",
                color: "var(--ink)", whiteSpace: "nowrap",
                background: "var(--surface)", border: "1px solid var(--hairline)",
                padding: "5px 10px", borderRadius: 99,
                opacity: hov ? 1 : 0, pointerEvents: "none",
                transition: "opacity .2s, transform .2s",
                transitionDelay: hov ? "60ms" : "0ms",
                boxShadow: "0 6px 14px -8px rgba(14,31,58,.25)",
              }}>{it.label}</span>
              {/* dot/bar */}
              <span style={{
                display: "block",
                width: on ? 22 : 14, height: 2,
                background: on ? "var(--gold-500)" : "var(--slate-600)",
                opacity: on ? 1 : (hov ? .9 : .5),
                borderRadius: 2,
                transition: "all .25s var(--ease)",
              }}></span>
            </a>
          );
        })}
      </nav>
    );
  };
})();
