/* OpenSyn homepage — Hero (dark opsroom shell + WebGL flow field) */
const { Button: OSButton, Badge: OSBadge, Symbol: OSSymbol } = window.OpenSynDesignSystem_a3458e;
const { useRef, useEffect } = React;

const GITHUB_URL = "https://github.com/a-lfre-do/OpenSyn";

/* ---- shared chrome: top navigation bar -------------------------------- */
function TopNav({ dark = true, theme = "light", onToggleTheme }) {
  const ink = dark ? "var(--c-cyber-white)" : "var(--text-body)";
  const line = dark ? "rgba(255,255,255,0.12)" : "var(--border-soft)";
  const mark = dark ? "assets/opensyn-mark-light.svg" : "assets/opensyn-mark.svg";
  const links = [
    ["The connection", "#connection"],
    ["Principles", "#principles"],
    ["Architecture", "#architecture"],
  ];
  return (
    <nav style={{
      position: "absolute", top: 0, left: 0, right: 0, zIndex: 6,
      display: "flex", alignItems: "center", gap: "16px",
      padding: "18px clamp(20px, 5vw, 56px)",
    }}>
      <a href="#top" style={{ display: "flex", alignItems: "center", gap: "11px", textDecoration: "none" }}>
        <img src={mark} width="30" height="30" alt="OpenSyn mark" style={{ display: "block" }} />
        <span style={{
          fontFamily: "var(--font-display)", fontWeight: 700, fontSize: "20px",
          letterSpacing: "-0.01em", color: ink,
        }}>OpenSyn</span>
        <span style={{
          fontFamily: "var(--font-mono)", fontSize: "10px", letterSpacing: "0.16em",
          textTransform: "uppercase", color: dark ? "#8b93bd" : "var(--text-faint)",
          border: `1px solid ${line}`, padding: "2px 6px", borderRadius: "var(--radius-sm)",
          marginLeft: "2px",
        }}>SYNCO</span>
      </a>
      <div className="os-nav-links" style={{ marginLeft: "auto", display: "flex", alignItems: "center", gap: "26px" }}>
        {links.map(([label, href]) => (
          <a key={href} href={href} style={{
            fontFamily: "var(--font-mono)", fontSize: "12px", fontWeight: 500,
            letterSpacing: "0.08em", textTransform: "uppercase", textDecoration: "none",
            color: dark ? "rgba(246,244,239,0.78)" : "var(--text-muted)",
          }}>{label}</a>
        ))}
      </div>
      <button onClick={onToggleTheme} aria-label="Toggle light or dark theme" style={{
        display: "inline-flex", alignItems: "center", gap: "7px", cursor: "pointer",
        marginLeft: dark === undefined ? 0 : "0",
        fontFamily: "var(--font-mono)", fontSize: "10px", fontWeight: 600,
        letterSpacing: "0.14em", textTransform: "uppercase", color: ink,
        background: "transparent", border: `1px solid ${line}`,
        borderRadius: "var(--radius-pill)", padding: "6px 11px",
      }}>
        <span style={{ width: "8px", height: "8px", borderRadius: "50%", background: theme === "dark" ? "#aeb6e0" : "var(--c-signal-orange)", boxShadow: theme === "dark" ? "none" : "0 0 8px var(--c-signal-orange)", display: "inline-block", flex: "none" }} />
        {theme === "dark" ? "Dark" : "Light"}
      </button>
      <a href={GITHUB_URL} target="_blank" rel="noopener" style={{ textDecoration: "none" }}>
        <OSButton variant="primary" size="sm" iconLeft={<GithubGlyph />}>GitHub</OSButton>
      </a>
    </nav>
  );
}

function GithubGlyph({ size = 15 }) {
  return (
    <svg width={size} height={size} viewBox="0 0 16 16" fill="currentColor" aria-hidden="true">
      <path d="M8 0C3.58 0 0 3.58 0 8c0 3.54 2.29 6.53 5.47 7.59.4.07.55-.17.55-.38 0-.19-.01-.82-.01-1.49-2.01.37-2.53-.49-2.69-.94-.09-.23-.48-.94-.82-1.13-.28-.15-.68-.52-.01-.53.63-.01 1.08.58 1.23.82.72 1.21 1.87.87 2.33.66.07-.52.28-.87.51-1.07-1.78-.2-3.64-.89-3.64-3.95 0-.87.31-1.59.82-2.15-.08-.2-.36-1.02.08-2.12 0 0 .67-.21 2.2.82.64-.18 1.32-.27 2-.27.68 0 1.36.09 2 .27 1.53-1.04 2.2-.82 2.2-.82.44 1.1.16 1.92.08 2.12.51.56.82 1.27.82 2.15 0 3.07-1.87 3.75-3.65 3.95.29.25.54.73.54 1.48 0 1.07-.01 1.93-.01 2.2 0 .21.15.46.55.38A8.01 8.01 0 0 0 16 8c0-4.42-3.58-8-8-8Z"></path>
    </svg>
  );
}

/* ---- canonical layer glyph: + / ○ / △ / ▢ / ⬠ ----------------------- */
/* L1 Tasks = plus, then a side is added progressively up the stack:
   L2 circle · L3 triangle · L4 square · L5 pentagon. The DS Symbol has
   no "plus", so we draw it to the same 24×24 / 2px geometric spec. */
function OSGlyph({ name = "circle", size = 20, color = "currentColor", filled = false, strokeWidth = 2, style = {} }) {
  if (name === "plus") {
    return (
      <svg viewBox="0 0 24 24" width={size} height={size} fill="none" aria-hidden="true"
        style={{ display: "inline-block", verticalAlign: "middle", flex: "none", ...style }}>
        <path d="M12 4.2 V19.8 M4.2 12 H19.8" stroke={color} strokeWidth={strokeWidth + 0.5} strokeLinecap="round" />
      </svg>
    );
  }
  return <OSSymbol name={name} size={size} color={color} filled={filled} strokeWidth={strokeWidth} style={style} />;
}

/* ---- the hero --------------------------------------------------------- */
function Hero({ treatment, theme = "light", onToggleTheme }) {
  const fieldRef = useRef(null);
  const ctrlRef = useRef(null);

  useEffect(() => {
    if (!fieldRef.current || !window.OpenSynFlow) return;
    ctrlRef.current = window.OpenSynFlow.mount(fieldRef.current, { treatment, theme });
    return () => { ctrlRef.current && ctrlRef.current.destroy(); };
  }, []);

  useEffect(() => {
    if (ctrlRef.current) ctrlRef.current.setTreatment(treatment);
  }, [treatment]);

  useEffect(() => {
    if (ctrlRef.current && ctrlRef.current.setTheme) ctrlRef.current.setTheme(theme);
  }, [theme]);

  return (
    <header id="top" style={{
      position: "relative", minHeight: "100vh", overflow: "hidden",
      background: "var(--os-hero-bg)",
      display: "flex", flexDirection: "column",
    }}>
      {/* WebGL field */}
      <div ref={fieldRef} style={{ position: "absolute", inset: 0, zIndex: 1 }} aria-hidden="true" />
      {/* scanlines + vignette */}
      <div aria-hidden="true" style={{ position: "absolute", inset: 0, background: "var(--scanlines)", zIndex: 2, pointerEvents: "none", opacity: "var(--os-scanline-op, 0.7)" }} />
      <div aria-hidden="true" style={{
        position: "absolute", inset: 0, zIndex: 3, pointerEvents: "none",
        background: "var(--os-hero-vignette)",
      }} />

      <TopNav dark={theme === "dark"} theme={theme} onToggleTheme={onToggleTheme} />

      {/* hero content */}
      <div style={{
        position: "relative", zIndex: 5, flex: 1,
        display: "flex", flexDirection: "column", justifyContent: "center",
        maxWidth: "var(--container-max)", width: "100%", margin: "0 auto",
        padding: "120px clamp(20px, 5vw, 56px) 64px",
      }}>
        <div style={{ display: "flex", alignItems: "center", gap: "10px", marginBottom: "22px" }}>
          <span style={{ width: "28px", height: "2px", background: "var(--c-signal-orange)" }} />
          <span style={{
            fontFamily: "var(--font-mono)", fontSize: "12px", fontWeight: 600,
            letterSpacing: "0.18em", textTransform: "uppercase", color: "var(--os-dim2)",
          }}>Harness ontology · Multi-agent orchestration</span>
        </div>

        <h1 style={{
          fontFamily: "var(--font-display)", fontWeight: 700,
          fontSize: "clamp(54px, 10vw, 132px)", lineHeight: 0.92,
          letterSpacing: "-0.03em", color: "var(--os-ink)", margin: 0,
        }}>OpenSyn</h1>

        <p style={{
          fontFamily: "var(--font-sans)", fontWeight: 400,
          fontSize: "clamp(18px, 2.4vw, 26px)", lineHeight: 1.4,
          color: "rgba(var(--os-ink-rgb), 0.92)", margin: "26px 0 0",
          maxWidth: "30ch", textWrap: "balance",
        }}>
          A <strong style={{ fontWeight: 600, color: "var(--os-ink)" }}>viable organization</strong> of
          bounded agents, typed objects, and explicit control flow — specified the way an
          institution is, not the way a script is written.
        </p>

        <p style={{
          fontFamily: "var(--font-mono)", fontSize: "13px", lineHeight: 1.6,
          color: "var(--os-dim2)", margin: "18px 0 0", maxWidth: "52ch",
          letterSpacing: "0.01em",
        }}>
          Inspired by Project Cybersyn / SYNCO — Chile's 1971–73 operations room for
          governing a system by exception.
        </p>

        <div style={{ display: "flex", flexWrap: "wrap", gap: "12px", marginTop: "36px" }}>
          <a href={GITHUB_URL} target="_blank" rel="noopener" style={{ textDecoration: "none" }}>
            <OSButton variant="primary" size="lg" iconLeft={<GithubGlyph size={17} />}>View on GitHub</OSButton>
          </a>
          <a href="#architecture" style={{ textDecoration: "none" }}>
            <OSButton variant="secondary" size="lg" style={{
              background: "transparent", color: "var(--os-ink)",
              borderColor: "var(--os-btn2-border)",
            }}>Read the architecture →</OSButton>
          </a>
        </div>

        <div style={{ display: "flex", flexWrap: "wrap", gap: "10px", marginTop: "34px", alignItems: "center" }}>
          <OSBadge variant="info" dot>5-layer stack · L1–L5</OSBadge>
          <OSBadge variant="attention" dot>Design phase</OSBadge>
          <OSBadge variant="solid">MIT licensed</OSBadge>
        </div>
      </div>

      {/* bottom hairline + object key */}
      <div style={{
        position: "relative", zIndex: 5,
        borderTop: "1px solid rgba(var(--os-line-rgb), var(--os-line-op))",
        display: "flex", gap: "22px", flexWrap: "wrap",
        padding: "13px clamp(20px, 5vw, 56px)",
        maxWidth: "var(--container-max)", width: "100%", margin: "0 auto",
      }}>
        {[["plus", "L1 · Tasks", C2.blue], ["circle", "L2 · Coordination", C2.blue], ["triangle", "L3 · Operations", C2.green], ["square", "L4 · Intelligence", C2.yellow], ["pentagon", "L5 · Governance", C2.orange]].map(([n, label, col]) => (
          <span key={n} style={{ display: "inline-flex", alignItems: "center", gap: "7px" }}>
            <OSGlyph name={n} size={14} color={col} filled={n === "pentagon"} />
            <span style={{ fontFamily: "var(--font-mono)", fontSize: "10px", letterSpacing: "0.12em", textTransform: "uppercase", color: "rgba(var(--os-ink-rgb), 0.62)" }}>{label}</span>
          </span>
        ))}
      </div>
    </header>
  );
}

const C2 = {
  blue: "#4f6be0", white: "#F6F4EF", yellow: "#F0C83D", green: "#4C9B63", orange: "#E95C2B",
};

Object.assign(window, { Hero, TopNav, GithubGlyph, OSGlyph, GITHUB_URL });
