// Shared legal page chrome — used by privacy.html and terms.html.

const { useState: useStateL, useEffect: useEffL } = React;

function LegalPage({ title, eyebrow, updated, summary, sections }) {
  useReveal();
  const [active, setActive] = useStateL(sections[0]?.id);

  // Sticky-toc highlight on scroll
  useEffL(() => {
    const ids = sections.map((s) => s.id);
    const onScroll = () => {
      let cur = ids[0];
      for (const id of ids) {
        const el = document.getElementById(id);
        if (el && el.getBoundingClientRect().top < 140) cur = id;
      }
      setActive(cur);
    };
    window.addEventListener("scroll", onScroll, { passive: true });
    onScroll();
    return () => window.removeEventListener("scroll", onScroll);
  }, []);

  return (
    <div data-screen-label={title}>
      <Nav active="" />

      {/* Header */}
      <section style={{ paddingTop: 120, paddingBottom: 80, borderBottom: "1px solid var(--line)" }}>
        <div className="wrap">
          <div className="reveal" style={{ marginBottom: 32 }}>
            <Eyebrow accent="sage">{eyebrow}</Eyebrow>
          </div>
          <h1 className="display reveal d1" style={{
            fontSize: "clamp(48px, 6vw, 96px)", maxWidth: "16ch", marginBottom: 32,
          }}>{title}</h1>
          <div className="reveal d2" style={{
            display: "flex", gap: 32, alignItems: "baseline", flexWrap: "wrap",
            paddingTop: 24, borderTop: "1px solid var(--line)", marginTop: 24,
          }}>
            <div>
              <div style={{
                fontFamily: "var(--mono)", fontSize: 11, letterSpacing: "0.14em",
                textTransform: "uppercase", color: "var(--ink-soft)", marginBottom: 8,
              }}>Last updated</div>
              <div style={{ fontFamily: "var(--serif)", fontSize: 22 }}>{updated}</div>
            </div>
            <div style={{ flex: 1, minWidth: 280, maxWidth: 640 }}>
              <p style={{
                fontFamily: "var(--sans)", fontSize: 16, lineHeight: 1.6,
                color: "var(--ink-soft)", margin: 0,
              }}>{summary}</p>
            </div>
          </div>
        </div>
      </section>

      {/* Body */}
      <section style={{ padding: "80px 0 120px" }}>
        <div className="wrap" style={{
          display: "grid", gridTemplateColumns: "260px 1fr", gap: 80,
          alignItems: "start",
        }}>
          {/* Sticky TOC */}
          <aside style={{ position: "sticky", top: 100, alignSelf: "start" }}>
            <div style={{
              fontFamily: "var(--mono)", fontSize: 11, letterSpacing: "0.14em",
              textTransform: "uppercase", color: "var(--ink-soft)", marginBottom: 20,
            }}>Contents</div>
            <ol style={{ listStyle: "none", padding: 0, margin: 0, display: "flex", flexDirection: "column", gap: 4 }}>
              {sections.map((s, i) => (
                <li key={s.id}>
                  <a href={`#${s.id}`} style={{
                    display: "flex", gap: 12, alignItems: "baseline",
                    padding: "8px 0",
                    fontFamily: "var(--sans)", fontSize: 14,
                    color: active === s.id ? "var(--ink)" : "var(--ink-soft)",
                    fontWeight: active === s.id ? 500 : 400,
                    transition: "color .2s",
                    borderLeft: active === s.id ? "2px solid var(--ink)" : "2px solid transparent",
                    paddingLeft: 12,
                  }}>
                    <span style={{
                      fontFamily: "var(--mono)", fontSize: 11, letterSpacing: "0.14em",
                      color: "var(--ink-soft)",
                    }}>{String(i + 1).padStart(2, "0")}</span>
                    {s.title}
                  </a>
                </li>
              ))}
            </ol>
          </aside>

          {/* Main copy */}
          <main style={{ maxWidth: 720 }}>
            {sections.map((s, i) => (
              <section key={s.id} id={s.id} className="reveal" style={{
                paddingTop: i === 0 ? 0 : 56,
                paddingBottom: 16,
                marginBottom: 16,
                borderTop: i === 0 ? "none" : "1px solid var(--line)",
              }}>
                <div style={{ display: "flex", alignItems: "baseline", gap: 16, marginBottom: 20, paddingTop: i === 0 ? 0 : 32 }}>
                  <span style={{
                    fontFamily: "var(--mono)", fontSize: 11, letterSpacing: "0.14em",
                    color: "var(--ink-soft)",
                  }}>{String(i + 1).padStart(2, "0")}</span>
                  <h2 className="h3" style={{ fontSize: "clamp(22px, 2.2vw, 30px)" }}>{s.title}</h2>
                </div>
                <div className="legal-body" style={{
                  fontFamily: "var(--sans)", fontSize: 16, lineHeight: 1.7,
                  color: "var(--ink)",
                }}>
                  {s.body}
                </div>
              </section>
            ))}

            {/* Contact card */}
            <div className="reveal" style={{
              marginTop: 60, padding: 32,
              background: "var(--cream-warm)", border: "1px solid var(--line)",
              borderRadius: 4,
            }}>
              <Eyebrow accent="terra">Questions?</Eyebrow>
              <h3 className="h3" style={{ marginTop: 16, marginBottom: 12 }}>
                We'd rather hear from you than have you guess.
              </h3>
              <p style={{ color: "var(--ink-soft)", marginTop: 0, marginBottom: 20, maxWidth: "44ch" }}>
                Email <a href="mailto:legal@congruence.cx" style={{ borderBottom: "1px solid var(--line-strong)" }}>legal@congruence.cx</a> for anything covered &mdash; or not covered &mdash; on this page.
              </p>
            </div>
          </main>
        </div>
      </section>

      <Footer />

      <style>{`
        .legal-body p { margin: 0 0 16px; }
        .legal-body p:last-child { margin-bottom: 0; }
        .legal-body ul { margin: 0 0 16px; padding-left: 20px; }
        .legal-body li { margin-bottom: 8px; }
        .legal-body strong { font-weight: 500; color: var(--ink); }
        @media (max-width: 860px) {
          .wrap aside { position: static !important; }
        }
      `}</style>
    </div>
  );
}

Object.assign(window, { LegalPage });
