// FireTail portfolio — Nav + Footer
const { Button } = window.FireTailDesignSystem_df7a18;

function FlameMark({ size = 34 }) {
  return (
    <svg className="ft-logo-flame" width={size} height={size} viewBox="0 0 64 64" fill="none" role="img" aria-label="FireTail"
      style={{ flexShrink: 0, display: "block", overflow: "visible" }}>
      <defs>
        <linearGradient id="ftFlameNav" x1="32" y1="6" x2="32" y2="58" gradientUnits="userSpaceOnUse">
          <stop offset="0" stopColor="#9fdcff" /><stop offset="0.5" stopColor="#1f9bff" /><stop offset="1" stopColor="#0e5db4" />
        </linearGradient>
        <linearGradient id="ftCoreNav" x1="32" y1="26" x2="32" y2="51" gradientUnits="userSpaceOnUse">
          <stop offset="0" stopColor="#eafcff" /><stop offset="1" stopColor="#45e2d2" />
        </linearGradient>
      </defs>
      <path d="M32 6 C 30 18 44 24 44 40 C 44 49 39 56 32 56 C 25 56 20 49 20 40 C 20 33 25 31 27 25 C 28 31 33 31 33 23 C 33 16 32 11 32 6 Z" fill="url(#ftFlameNav)" />
      <path className="ft-logo-core" d="M32 27 C 31 34 38 35 38 43 C 38 48 35 51 32 51 C 28 51 25 48 25 43 C 25 39 28 38 29 34 C 30 38 33 37 33 32 C 33 29 32 28 32 27 Z" fill="url(#ftCoreNav)" />
    </svg>
  );
}

function ThemeToggle() {
  const osPrefersDark = () => window.matchMedia("(prefers-color-scheme: dark)").matches;

  const getEffective = () => {
    const stored = localStorage.getItem("firetail-theme");
    if (stored) return stored;
    return osPrefersDark() ? "dark" : "light";
  };

  const [mode, setMode] = React.useState(getEffective);

  const apply = (m) => {
    const cl = document.documentElement.classList;
    cl.remove("theme-light", "theme-dark");
    cl.add(m === "light" ? "theme-light" : "theme-dark");
  };

  React.useEffect(() => {
    apply(mode);
    const mq = window.matchMedia("(prefers-color-scheme: dark)");
    const onChange = () => {
      if (!localStorage.getItem("firetail-theme")) {
        const next = mq.matches ? "dark" : "light";
        setMode(next);
        apply(next);
      }
    };
    mq.addEventListener("change", onChange);
    return () => mq.removeEventListener("change", onChange);
  }, []);

  const toggle = () => {
    const next = mode === "dark" ? "light" : "dark";
    localStorage.setItem("firetail-theme", next);
    setMode(next);
    apply(next);
  };

  return (
    <button
      className="ft-theme-toggle"
      onClick={toggle}
      aria-label={mode === "dark" ? "Switch to light mode" : "Switch to dark mode"}
    >
      {mode === "dark" ? (
        <svg width="16" height="16" viewBox="0 0 24 24" fill="none" stroke="currentColor" strokeWidth="2" strokeLinecap="round" strokeLinejoin="round">
          <circle cx="12" cy="12" r="5"/><line x1="12" y1="1" x2="12" y2="3"/><line x1="12" y1="21" x2="12" y2="23"/><line x1="4.22" y1="4.22" x2="5.64" y2="5.64"/><line x1="18.36" y1="18.36" x2="19.78" y2="19.78"/><line x1="1" y1="12" x2="3" y2="12"/><line x1="21" y1="12" x2="23" y2="12"/><line x1="4.22" y1="19.78" x2="5.64" y2="18.36"/><line x1="18.36" y1="5.64" x2="19.78" y2="4.22"/>
        </svg>
      ) : (
        <svg width="16" height="16" viewBox="0 0 24 24" fill="none" stroke="currentColor" strokeWidth="2" strokeLinecap="round" strokeLinejoin="round">
          <path d="M21 12.79A9 9 0 1 1 11.21 3 7 7 0 0 0 21 12.79z"/>
        </svg>
      )}
    </button>
  );
}

function Nav({ onHome }) {
  const [scrolled, setScrolled] = React.useState(false);
  React.useEffect(() => {
    const el = document.querySelector("#ft-scroll");
    const t = el || window;
    const fn = () => setScrolled((el ? el.scrollTop : window.scrollY) > 20);
    fn();
    t.addEventListener("scroll", fn, { passive: true });
    return () => t.removeEventListener("scroll", fn);
  }, []);
  const links = ["Work", "About", "Contact"];
  return (
    <header style={{
      position: "sticky", top: 0, zIndex: 50, height: "var(--nav-h)",
      display: "flex", alignItems: "center", justifyContent: "space-between",
      padding: "0 clamp(20px, 5vw, 48px)",
      background: scrolled ? "var(--surface-overlay)" : "transparent",
      backdropFilter: scrolled ? "blur(14px)" : "none",
      borderBottom: `1px solid ${scrolled ? "var(--border)" : "transparent"}`,
      transition: "background var(--dur-base) var(--ease-out), border-color var(--dur-base) var(--ease-out)",
    }}>
      <a href="#top" onClick={onHome} style={{ display: "inline-flex", alignItems: "center", gap: 12, textDecoration: "none" }}>
        <span id="ft-nav-flamedock" style={{ display: "inline-block", width: 44, height: 44, flexShrink: 0, visibility: "hidden" }} />
        <span id="ft-nav-namedock" style={{
          visibility: "hidden", fontFamily: "var(--font-display)", fontWeight: 600,
          fontSize: 24, letterSpacing: "-0.01em", lineHeight: 1, whiteSpace: "nowrap",
        }}>Smit Waghela</span>
      </a>
      <nav style={{ display: "flex", alignItems: "center", gap: "clamp(16px, 3vw, 34px)" }}>
        {links.map((l) => (
          <a key={l} href={"#" + l.toLowerCase()} style={{
            fontFamily: "var(--font-mono)", fontSize: 12, letterSpacing: "0.12em",
            textTransform: "uppercase", color: "var(--text-muted)", textDecoration: "none",
            transition: "color var(--dur-base) var(--ease-out)",
          }}
          onMouseEnter={(e) => e.currentTarget.style.color = "var(--text-strong)"}
          onMouseLeave={(e) => e.currentTarget.style.color = "var(--text-muted)"}>{l}</a>
        ))}
        <ThemeToggle />
        <Button as="a" href="#contact" size="sm">Open to work</Button>
      </nav>
    </header>
  );
}

function Footer() {
  const [narrow, setNarrow] = React.useState(() => window.innerWidth < 768);
  React.useEffect(() => {
    const mq = window.matchMedia("(max-width: 767px)");
    const fn = (e) => setNarrow(e.matches);
    mq.addEventListener("change", fn);
    return () => mq.removeEventListener("change", fn);
  }, []);

  const brand = (
    <div style={{ display: "flex", alignItems: "center", gap: 12, marginBottom: 14 }}>
      <img src="../../assets/firetail-mark.svg" width="30" height="30" alt="" />
      <span style={{ display: "flex", flexDirection: "column", lineHeight: 1.05 }}>
        <span style={{ fontFamily: "var(--font-display)", fontWeight: 600, fontSize: 18, color: "var(--text-strong)" }}>Smit Waghela</span>
        <span style={{ fontFamily: "var(--font-mono)", fontSize: 9.5, letterSpacing: "0.22em", textTransform: "uppercase", color: "var(--text-faint)" }}>FireTail Studio</span>
      </span>
    </div>
  );
  const tagline = (
    <p style={{ margin: 0, fontFamily: "var(--font-sans)", fontSize: 14, color: "var(--text-faint)", maxWidth: 340, lineHeight: 1.6 }}>
      Gameplay systems &amp; tools. Quiet fires, loud results.
    </p>
  );
  const copy = (
    <div style={{ fontFamily: "var(--font-mono)", fontSize: 11, letterSpacing: "0.08em", color: "var(--text-faint)" }}>
      © 2026 SMIT WAGHELA
    </div>
  );

  return (
    <footer style={{ borderTop: "1px solid var(--border)", padding: "56px clamp(20px,5vw,48px) 40px", marginTop: 40, background: "var(--surface-overlay)", backdropFilter: "blur(14px)", position: "relative", zIndex: 2 }}>
      <div style={{ maxWidth: "var(--container)", margin: "0 auto" }}>
        {narrow ? (
          <>
            {brand}
            {tagline}
            <div id="ft-anchor-footer" style={{ height: 48, marginTop: 20, display: "flex", alignItems: "center" }} />
            <div style={{ marginTop: 20 }}>{copy}</div>
          </>
        ) : (
          <>
            <div style={{ display: "flex", flexWrap: "wrap", gap: "20px 32px", justifyContent: "space-between", alignItems: "flex-start" }}>
              <div style={{ flex: "1 1 300px", minWidth: 0 }}>{brand}{tagline}</div>
              <div id="ft-anchor-footer" style={{ height: 30, minWidth: 46, display: "flex", alignItems: "center", flexShrink: 0 }} />
            </div>
            <div style={{ marginTop: 34 }}>{copy}</div>
          </>
        )}
      </div>
    </footer>
  );
}

Object.assign(window, { Nav, Footer });
