/* global React, Icon, Eyebrow, Serif, useCapture */
/* =====================================================================
   Interactions · set 1 — Bridge (02), Futures (03), Assumptions (04)
   ===================================================================== */
const { useState: useS1 } = React;

/* ---------------------------------------------------------------- BRIDGE */
function BridgeObject({ data }) {
  const [laid, setLaid] = useS1(() => new Set());
  const [sel, setSel] = useS1(null); // {type, i}
  const allLaid = laid.size === data.deck.length;

  const togglePlate = (i) => {
    setLaid((prev) => {
      const n = new Set(prev);
      n.has(i) ? n.delete(i) : n.add(i);
      return n;
    });
    setSel({ type: "plate", i });
  };

  const Pillar = ({ side }) => (
    <div style={{
      flex: "0 0 clamp(120px,15vw,180px)", border: "1px solid var(--ink)",
      background: "var(--bone)", padding: "16px 16px 18px", display: "flex",
      flexDirection: "column", minWidth: 0,
    }}>
      <div style={{ fontFamily: "var(--font-mono)", fontSize: 11, letterSpacing: "0.14em",
        textTransform: "uppercase", color: "var(--ink)", fontWeight: 500 }}>{side.label}</div>
      <div style={{ height: 1, background: "var(--fog)", margin: "12px 0 14px" }} />
      <div style={{ display: "flex", flexDirection: "column", gap: 7 }}>
        {side.items.map((it) => (
          <div key={it} style={{ fontSize: 14, color: "var(--ash)", lineHeight: 1.2 }}>{it}</div>
        ))}
      </div>
    </div>
  );

  const note = sel
    ? (sel.type === "plate" ? data.deck[sel.i] : data.canyon[sel.i])
    : null;

  return (
    <div>
      <div style={{ display: "flex", alignItems: "stretch", gap: 0, minHeight: 320 }}>
        <Pillar side={data.left} />

        {/* canyon */}
        <div style={{ flex: 1, position: "relative", minWidth: 0,
          borderTop: "1px solid var(--ink)", borderBottom: "1px solid var(--ink)",
          background: "var(--bone)",
          backgroundImage: "repeating-linear-gradient(var(--fog) 0 1px, transparent 1px 26px)",
          backgroundPosition: "0 64px",
          display: "flex", flexDirection: "column", justifyContent: "space-between",
          padding: "0",
        }}>
          {/* bridge deck */}
          <div style={{ display: "flex", borderBottom: "1px solid var(--ink)" }}>
            {data.deck.map((d, i) => {
              const on = laid.has(i);
              const active = sel && sel.type === "plate" && sel.i === i;
              return (
                <button key={i} onClick={() => togglePlate(i)} className="focus-ring"
                  style={{
                    all: "unset", cursor: "pointer", flex: 1, minWidth: 0, textAlign: "center",
                    padding: "13px 6px", boxSizing: "border-box",
                    borderRight: i < data.deck.length - 1 ? "1px solid var(--ink)" : "none",
                    background: on ? "var(--ink)" : "transparent",
                    color: on ? "var(--paper)" : "var(--ash)",
                    outline: active ? "2px solid var(--signal)" : "none", outlineOffset: -2,
                    transition: "background var(--dur-fast) var(--ease), color var(--dur-fast) var(--ease)",
                  }}>
                  <div style={{ fontFamily: "var(--font-mono)", fontSize: 9, letterSpacing: "0.1em",
                    opacity: 0.6, marginBottom: 4 }}>{on ? "LAID" : "PLATE " + (i + 1)}</div>
                  <div style={{ fontSize: 12.5, fontWeight: 500, lineHeight: 1.15,
                    letterSpacing: "-0.01em" }}>{d.plate}</div>
                </button>
              );
            })}
          </div>

          {/* canyon floor */}
          <div style={{ padding: "20px 16px 18px", display: "flex", flexDirection: "column",
            gap: 14, alignItems: "center" }}>
            <div style={{ fontFamily: "var(--font-mono)", fontSize: 10, letterSpacing: "0.18em",
              textTransform: "uppercase", color: "var(--fog)" }}>The canyon</div>
            <div style={{ display: "flex", flexWrap: "wrap", gap: 8, justifyContent: "center" }}>
              {data.canyon.map((c, i) => {
                const active = sel && sel.type === "friction" && sel.i === i;
                return (
                  <button key={i} onClick={() => setSel({ type: "friction", i })} className="focus-ring"
                    style={{
                      all: "unset", cursor: "pointer", padding: "6px 12px",
                      border: `1px solid ${active ? "var(--signal)" : "var(--fog)"}`,
                      color: active ? "var(--signal)" : "var(--ash)",
                      fontFamily: "var(--font-mono)", fontSize: 12, letterSpacing: "0.04em",
                      background: "var(--surface)",
                      transition: "all var(--dur-fast) var(--ease)",
                    }}>{c.word}</button>
                );
              })}
            </div>
          </div>
        </div>

        <Pillar side={data.right} />
      </div>

      {/* detail + caption strip */}
      <div style={{ display: "flex", alignItems: "center", justifyContent: "space-between",
        gap: 24, marginTop: 18, borderTop: "1px solid var(--fog)", paddingTop: 16, flexWrap: "wrap" }}>
        <div style={{ minHeight: 38, flex: "1 1 320px" }}>
          {note ? (
            <div style={{ display: "flex", gap: 12, alignItems: "baseline", maxWidth: "62ch" }}>
              <span style={{ fontFamily: "var(--font-mono)", fontSize: 11, letterSpacing: "0.1em",
                textTransform: "uppercase", color: "var(--signal)", flex: "0 0 auto" }}>
                {sel.type === "plate" ? note.plate : note.word}
              </span>
              <span style={{ color: "var(--ash)", fontSize: 15, lineHeight: 1.45 }}>{note.note}</span>
            </div>
          ) : (
            <div style={{ color: "var(--ash)", fontSize: 15, lineHeight: 1.45, maxWidth: "58ch" }}>
              Tap a word in the canyon to see what blocks the crossing. Lay each plate of the bridge —
              that is what the accelerator actually builds.
            </div>
          )}
        </div>
        <div style={{ fontFamily: "var(--font-serif)", fontStyle: "italic", fontSize: 24,
          color: allLaid ? "var(--signal)" : "var(--fog)", letterSpacing: 0,
          transition: "color var(--dur) var(--ease)", whiteSpace: "nowrap" }}>
          {data.caption}
        </div>
      </div>
    </div>
  );
}

/* --------------------------------------------------------------- FUTURES */
function FuturesFork({ data }) {
  const [choice, setChoice] = useCapture("text:future", "");
  return (
    <div>
      <div style={{ display: "grid", gridTemplateColumns: "repeat(auto-fit, minmax(220px, 1fr))",
        gap: 0, border: "1px solid var(--ink)" }}>
        {data.map((f, i) => {
          const chosen = choice === f.id;
          return (
            <button key={f.id} onClick={() => setChoice(f.id)} className="focus-ring"
              style={{
                all: "unset", cursor: "pointer", padding: "24px 22px 22px", boxSizing: "border-box",
                borderRight: i < data.length - 1 ? "1px solid var(--ink)" : "none",
                background: chosen ? "var(--ink)" : "transparent",
                color: chosen ? "var(--paper)" : "var(--ink)",
                opacity: choice && !chosen ? 0.5 : 1,
                transition: "all var(--dur) var(--ease)", display: "flex", flexDirection: "column",
                minHeight: 220,
              }}>
              <div style={{ display: "flex", justifyContent: "space-between", alignItems: "center" }}>
                <span style={{ fontFamily: "var(--font-mono)", fontSize: 11, letterSpacing: "0.12em",
                  whiteSpace: "nowrap", color: chosen ? "rgba(242,240,235,0.6)" : "var(--fg2)" }}>FUTURE {i + 1}</span>
                {f.recommended && (
                  <span style={{ fontFamily: "var(--font-mono)", fontSize: 9, letterSpacing: "0.14em",
                    textTransform: "uppercase", color: "var(--signal)", border: "1px solid var(--signal)",
                    padding: "3px 6px", whiteSpace: "nowrap" }}>Best fit</span>
                )}
              </div>
              <div style={{ fontFamily: "var(--font-sans)", fontWeight: 700, fontSize: 21,
                letterSpacing: "-0.02em", lineHeight: 1.1, margin: "16px 0 12px" }}>{f.label}</div>
              <div style={{ fontSize: 14.5, lineHeight: 1.5,
                color: chosen ? "rgba(242,240,235,0.85)" : "var(--ash)" }}>{f.body}</div>
              <div style={{ flex: 1 }} />
              <div style={{ marginTop: 18, paddingTop: 12,
                borderTop: `1px solid ${chosen ? "rgba(242,240,235,0.25)" : "var(--fog)"}`,
                fontSize: 13, lineHeight: 1.45,
                color: chosen ? "rgba(242,240,235,0.7)" : "var(--fg2)" }}>
                <span style={{ fontFamily: "var(--font-mono)", fontSize: 10, letterSpacing: "0.1em",
                  textTransform: "uppercase", color: f.recommended ? "var(--signal)" : "inherit" }}>Risk · </span>
                {f.risk}
              </div>
              {chosen && (
                <div style={{ marginTop: 14, fontFamily: "var(--font-mono)", fontSize: 10,
                  letterSpacing: "0.14em", textTransform: "uppercase", color: "var(--signal)",
                  display: "flex", alignItems: "center", gap: 8 }}>
                  <Icon name="check" size={13} /> Chosen
                </div>
              )}
            </button>
          );
        })}
      </div>
      <div style={{ marginTop: 18, minHeight: 28 }}>
        {choice === "platform" && (
          <div style={{ display: "flex", gap: 12, alignItems: "baseline" }}>
            <span style={{ color: "var(--signal)", fontFamily: "var(--font-mono)", fontSize: 12 }}>—</span>
            <span style={{ fontFamily: "var(--font-serif)", fontStyle: "italic", fontSize: 20,
              color: "var(--ink)", maxWidth: "54ch" }}>
              The clearest envisioned choice. Let the accelerator be the form, not the purpose.
            </span>
          </div>
        )}
        {choice && choice !== "platform" && (
          <div style={{ color: "var(--ash)", fontSize: 15, maxWidth: "56ch" }}>
            A reasonable future — but ask what it intentionally gives up. The deeper opportunity sits in Future 3.
          </div>
        )}
      </div>
    </div>
  );
}

/* ----------------------------------------------------------- ASSUMPTIONS */
function AssumptionsGrid({ data, worryOptions }) {
  const [filter, setFilter] = useS1("all");
  const [open, setOpen] = useS1(null);
  const [worry, setWorry] = useCapture("text:worry", "");
  const shown = data.filter((a) => filter === "all" || a.kind === filter);

  const chip = (key, label) => (
    <button onClick={() => setFilter(key)} className="focus-ring" style={{
      all: "unset", cursor: "pointer", padding: "7px 14px",
      fontFamily: "var(--font-mono)", fontSize: 11, letterSpacing: "0.1em", textTransform: "uppercase",
      border: "1px solid var(--ink)",
      background: filter === key ? "var(--ink)" : "transparent",
      color: filter === key ? "var(--paper)" : "var(--ink)",
    }}>{label}</button>
  );

  return (
    <div>
      <div style={{ display: "flex", gap: 10, marginBottom: 18, alignItems: "center", flexWrap: "wrap" }}>
        <span style={{ fontFamily: "var(--font-mono)", fontSize: 10, letterSpacing: "0.14em",
          textTransform: "uppercase", color: "var(--fg2)", marginRight: 4 }}>Sort the pile</span>
        {chip("all", "All 9")}
        {chip("adoption", "Adoption")}
        {chip("activity", "Activity")}
      </div>

      <div style={{ display: "grid", gridTemplateColumns: "repeat(auto-fill, minmax(248px, 1fr))",
        gap: 0, borderTop: "1px solid var(--ink)", borderLeft: "1px solid var(--ink)" }}>
        {shown.map((a) => {
          const isOpen = open === a.id;
          return (
            <button key={a.id} onClick={() => setOpen(isOpen ? null : a.id)} className="focus-ring"
              style={{
                all: "unset", cursor: "pointer", padding: "16px 16px 18px", boxSizing: "border-box",
                borderRight: "1px solid var(--ink)", borderBottom: "1px solid var(--ink)",
                background: isOpen ? "var(--bone)" : "transparent", display: "flex",
                flexDirection: "column", gap: 10, minHeight: 132,
              }}>
              <div style={{ display: "flex", justifyContent: "space-between", alignItems: "center" }}>
                <span style={{ fontFamily: "var(--font-mono)", fontSize: 12, letterSpacing: "0.08em",
                  color: "var(--signal)" }}>{a.id}</span>
                <span style={{ fontFamily: "var(--font-mono)", fontSize: 9, letterSpacing: "0.12em",
                  textTransform: "uppercase", color: "var(--fg2)",
                  border: "1px solid var(--fog)", padding: "2px 6px" }}>{a.kind}</span>
              </div>
              <div style={{ fontSize: 15, lineHeight: 1.4, color: "var(--ink)", fontWeight: 500 }}>{a.a}</div>
              {isOpen ? (
                <div style={{ display: "flex", flexDirection: "column", gap: 10, marginTop: 2 }}>
                  <div>
                    <div style={{ fontFamily: "var(--font-mono)", fontSize: 9, letterSpacing: "0.12em",
                      textTransform: "uppercase", color: "var(--fg2)", marginBottom: 4 }}>If it's false</div>
                    <div style={{ fontSize: 13.5, lineHeight: 1.4, color: "var(--ash)" }}>{a.c}</div>
                  </div>
                  <div>
                    <div style={{ fontFamily: "var(--font-mono)", fontSize: 9, letterSpacing: "0.12em",
                      textTransform: "uppercase", color: "var(--signal)", marginBottom: 4 }}>Cheap test</div>
                    <div style={{ fontSize: 13.5, lineHeight: 1.4, color: "var(--ink)" }}>{a.t}</div>
                  </div>
                </div>
              ) : (
                <div style={{ marginTop: "auto", fontFamily: "var(--font-mono)", fontSize: 10,
                  letterSpacing: "0.1em", textTransform: "uppercase", color: "var(--fg2)",
                  display: "flex", alignItems: "center", gap: 8 }}>
                  <Icon name="plus" size={11} /> Counterfactual &amp; test
                </div>
              )}
            </button>
          );
        })}
      </div>

      {/* capture: which worries you most */}
      <div style={{ marginTop: 28 }}>
        <div style={{ fontFamily: "var(--font-mono)", fontSize: 11, letterSpacing: "0.12em",
          textTransform: "uppercase", color: "var(--signal)", marginBottom: 14,
          display: "flex", alignItems: "center", gap: 10 }}>
          <span style={{ width: 18, height: 18, border: "1px solid var(--signal)", display: "inline-flex",
            alignItems: "center", justifyContent: "center" }}><Icon name="arrowRight" size={11} /></span>
          Capture · which assumption worries you most?
        </div>
        <div style={{ display: "flex", flexWrap: "wrap", gap: 8 }}>
          {worryOptions.map((w) => {
            const on = worry === w;
            return (
              <button key={w} onClick={() => setWorry(on ? "" : w)} className="focus-ring" style={{
                all: "unset", cursor: "pointer", padding: "9px 14px", fontSize: 14,
                border: `1px solid ${on ? "var(--signal)" : "var(--fog)"}`,
                background: on ? "var(--signal)" : "transparent",
                color: on ? "var(--paper)" : "var(--ash)",
                transition: "all var(--dur-fast) var(--ease)",
              }}>{w}</button>
            );
          })}
        </div>
      </div>
    </div>
  );
}

Object.assign(window, { BridgeObject, FuturesFork, AssumptionsGrid });
