import "./globals.css";
import type { Metadata } from "next";
import { Inter, Space_Grotesk, JetBrains_Mono } from "next/font/google";
import NextTopLoader from "nextjs-toploader";
import ClientRootLayout from "./ClientRootLayout";
import { defaultMetadata, organizationJsonLd } from "@/lib/seo";
import { LANDING_HASH_STORAGE_KEY } from "@/lib/landing-hash";

/**
 * Fonts are self-hosted by next/font instead of an @import in globals.css.
 * The @import was render-blocking on every navigation and caused a flash of
 * unstyled text; next/font inlines the @font-face and preloads the files.
 */
const inter = Inter({
  subsets: ["latin"],
  display: "swap",
  variable: "--font-sans",
});

const spaceGrotesk = Space_Grotesk({
  subsets: ["latin"],
  display: "swap",
  weight: ["500", "600", "700"],
  variable: "--font-display",
});

const jetbrainsMono = JetBrains_Mono({
  subsets: ["latin"],
  display: "swap",
  weight: ["400", "500", "600"],
  variable: "--font-mono",
});

export const metadata: Metadata = defaultMetadata;

export default function RootLayout({ children }: { children: React.ReactNode }) {
  return (
    <html
      lang="en"
      suppressHydrationWarning
      className={`${inter.variable} ${spaceGrotesk.variable} ${jetbrainsMono.variable}`}
    >
      <head>
        {/*
          Runs before the body is parsed, so it beats the browser's own
          "scroll to URL fragment" step: a direct load of /#product (or a
          reload, or a link from an external site) would otherwise jump the
          native scroll to a rough, unstyled position the instant the
          matching id is parsed — well before React hydrates — and then the
          landing page's own scrollToLandingSectionWhenReady() jumps again
          to the correct offset once ready, producing a visible double
          scroll. Stashing the fragment here and clearing it from the URL
          means there is nothing left for the browser to jump to; the stored
          value is picked up by peekLandingHash() exactly as if the visitor
          had arrived from an in-app nav click.
        */}
        <script
          dangerouslySetInnerHTML={{
            __html: `(function(){try{var h=window.location.hash;if(h&&h.length>1){sessionStorage.setItem(${JSON.stringify(
              LANDING_HASH_STORAGE_KEY
            )},h.slice(1));history.replaceState(null,"",window.location.pathname+window.location.search);}}catch(e){}})();`,
          }}
        />
        <script
          type="application/ld+json"
          dangerouslySetInnerHTML={{ __html: JSON.stringify(organizationJsonLd()) }}
        />
      </head>
      <body suppressHydrationWarning className="flex flex-col min-h-screen">
        {/*
          Route-change progress bar. This replaces the full-screen blocking
          overlay that used to cover the app on every fetch — that pattern
          froze the whole UI behind a scrim and hid the content the user was
          already reading. A top bar reports the same "work in progress"
          without taking the page away.
        */}
        <NextTopLoader
          // Brand green (#428B4D). Kept literal because the component writes
          // this into an injected <style> tag before the token layer is
          // available, so a var() reference would resolve to nothing.
          color="hsl(129 36% 40%)"
          height={2}
          shadow="0 0 8px hsl(129 36% 40% / 0.6)"
          showSpinner={false}
          speed={250}
          crawlSpeed={160}
          easing="cubic-bezier(0.22,1,0.36,1)"
          zIndex={1600}
          // A click on a same-page anchor (e.g. the landing page's nav)
          // never leaves the page — flashing a page-load progress bar for it
          // reads as a bug in its own right, independent of the scroll fix
          // above. This isn't what caused the double scroll (this component
          // only toggles nprogress's UI; it never touches scroll position),
          // but it's the correct configuration for anchor clicks regardless.
          showForHashAnchor={false}
        />
        <ClientRootLayout>{children}</ClientRootLayout>
      </body>
    </html>
  );
}
