const readRoute = () => { { const lm = location.hash.match(/^#\/?(privacidad|aviso-legal|cookies)\b/); if (lm) history.replaceState(null, "", "#/legal/" + ({ privacidad: "privacidad", "aviso-legal": "aviso", cookies: "cookies" })[lm[1]]); } { const m = location.hash.match(/^#\/?inversionistas\/guias(\/.*)?$/); if (m) history.replaceState(null, "", "#/publicaciones/guias" + (m[1] || "")); } if (/^#\/?publicaciones\/articulos/.test(location.hash)) history.replaceState(null, "", "#/publicaciones"); if (/^#\/?publicaciones\/temas/.test(location.hash)) history.replaceState(null, "", "#/publicaciones"); if (/^#\/?inversionistas\/ruta/.test(location.hash)) history.replaceState(null, "", "#/ruta"); if (location.hash === "#/inversionistas/asesoria") history.replaceState(null, "", "#/inversionistas/como-te-ayudamos"); const am = location.hash.match(/^#\/?areas(?:\/([^/]+))?/); if (am) { if (am[1] && AREA_REDIRECT[am[1]]) history.replaceState(null, "", AREA_REDIRECT[am[1]]); else { history.replaceState(null, "", "#/"); return { page: "", sub: "areas", rest: "" }; } } if (/^#\/?educacion/.test(location.hash)) history.replaceState(null, "", "#/academia"); if (/^#\/?personas/.test(location.hash)) history.replaceState(null, "", location.hash.replace(/^#\/?personas/, "#/inversionistas")); const p = (location.hash.replace(/^#\/?/, "") || "").split("/"); return { page: p[0] || "", sub: p[1] || "", rest: p[2] || "" }; };

// Scroll reveal: content is visible by default; only elements below the fold are hidden, and only once the observer is confirmed live.
function useReveal() {
  React.useEffect(() => {
    if (!("IntersectionObserver" in window) || matchMedia("(prefers-reduced-motion: reduce)").matches) return;
    let live = false;
    const show = (el) => { el.classList.remove("rv-hide"); };
    const io = new IntersectionObserver((es) => { live = true; es.forEach((e) => { if (e.isIntersecting) { show(e.target); io.unobserve(e.target); } }); }, { rootMargin: "0px 0px -6% 0px", threshold: 0.06 });
    const scan = () => document.querySelectorAll(".rv:not([data-rv])").forEach((el) => {
      el.setAttribute("data-rv", "");
      if (el.getBoundingClientRect().top < window.innerHeight * 0.92) return;
      el.classList.add("rv-hide"); io.observe(el);
    });
    scan();
    const mo = new MutationObserver(scan); mo.observe(document.body, { childList: true, subtree: true });
    const fallback = setTimeout(() => { if (!live) { io.disconnect(); mo.disconnect(); document.querySelectorAll(".rv-hide").forEach(show); } }, 1200);
    return () => { io.disconnect(); mo.disconnect(); clearTimeout(fallback); };
  }, []);
}

function App() {
  const [route, setRoute] = React.useState(readRoute);
  useReveal();
  React.useEffect(() => { const f = () => setRoute(readRoute()); addEventListener("hashchange", f); return () => removeEventListener("hashchange", f); }, []);
  React.useEffect(() => {
    if (!route.sub || route.rest) { window.scrollTo(0, 0); return; }
    const t = setTimeout(() => { const el = document.getElementById(route.sub); if (el) window.scrollTo({ top: el.getBoundingClientRect().top + window.scrollY - 96, behavior: "smooth" }); }, 80);
    return () => clearTimeout(t);
  }, [route.page, route.sub, route.rest]);

  const { page, sub, rest } = route;
  let view;
  if (page === "inversionistas" && sub === "problemas" && rest) view = <ProblemPage slug={rest} />;
  else if (page === "publicaciones" && sub === "guias" && rest) view = <GuidePage slug={rest} />;
  else if (page === "inversionistas" || (page === "servicios" && sub === "inversionistas")) view = <AudiencePage key="p" id="inversionistas" />;
  else if (page === "empresas" || page === "servicios") view = <AudiencePage key="e" id="empresas" />;
  else if (page === "ruta") view = <RutaPage />;
  else if (page === "academia") view = <AcademiaPage />;
  else if (page === "nosotros" || page === "sobre") view = <TeamPage />;
  else if (page === "publicaciones") view = <PublicationsPage />;
  else if (page === "contacto") view = <ContactPage />;
  else if (page === "legal") view = <LegalUnified />;
  else if (page === "libro-de-reclamaciones") view = <LibroPage />;
  else view = <HomePage />;

  return <><Header page={page} /><main key={page + "/" + (rest ? sub + "/" + rest : "")}>{view}</main><Footer /><MobileCTA page={page} /><CookieNotice /><ToastHost />
  </>;
}
ReactDOM.createRoot(document.getElementById("root")).render(<App />);
