/* =========================================================
   Section 2 — The primitive: what a phased escrow is.
   Two short paragraphs + one generic three-phase diagram.
   ========================================================= */
function PrimitiveSection() {
  return (
    <section
      id="primitive"
      data-screen-label="The primitive"
      style={{
        background: "var(--az-parchment)",
        padding: "120px 40px",
        borderTop: "1px solid rgba(26,20,0,0.12)",
      }}
    >
      <div style={{ maxWidth: 1280, margin: "0 auto" }}>
        <SectionHead
          number="02"
          label="The primitive"
          title="A phase is a tuple of entry, exit, and state delta."
        />
        <div
          style={{
            marginTop: 56,
            display: "grid",
            gridTemplateColumns: "minmax(0, 0.95fr) minmax(0, 1.05fr)",
            gap: 80,
            alignItems: "flex-start",
          }}
          className="primitive-grid"
        >
          <div style={{ display: "flex", flexDirection: "column", gap: 24 }}>
            <p
              style={{
                fontFamily: "var(--ff-serif)",
                fontWeight: 300,
                fontSize: 19,
                lineHeight: 1.55,
                color: "var(--az-ink)",
                maxWidth: 540,
                margin: 0,
              }}
            >
              An escrow is a state machine. The Aztec Escrow Kit makes that explicit. You
              declare a set of phases. Each phase carries an entry condition that pulls
              funds in, an exit condition that releases them, and a state delta that
              records what changed when the transition fired.
            </p>
            <p
              style={{
                fontFamily: "var(--ff-serif)",
                fontWeight: 300,
                fontSize: 19,
                lineHeight: 1.55,
                color: "var(--az-ink)",
                maxWidth: 540,
                margin: 0,
              }}
            >
              The Kit ships private state by default: balances, parties, and phase metadata
              live in Aztec's encrypted state tree. The transitions are public enough for
              verification, private enough that nobody on the outside can reconstruct who
              traded what.
            </p>
            <div
              style={{
                marginTop: 24,
                display: "grid",
                gridTemplateColumns: "1fr 1fr 1fr",
                gap: 0,
                border: "1px solid var(--az-ink)",
              }}
            >
              {[
                {
                  k: "Entry",
                  v: "What brings funds into this phase.",
                },
                {
                  k: "Exit",
                  v: "What unlocks transition out.",
                },
                {
                  k: "State Δ",
                  v: "What mutates on transition.",
                },
              ].map((c, i) => (
                <div
                  key={c.k}
                  style={{
                    padding: "20px 18px",
                    borderRight: i < 2 ? "1px solid var(--az-ink)" : "none",
                    background: i === 1 ? "var(--az-chartreuse-tint)" : "var(--az-parchment-2)",
                  }}
                >
                  <div
                    style={{
                      fontFamily: "var(--ff-display-alt)",
                      fontSize: 11,
                      letterSpacing: "0.08em",
                      textTransform: "uppercase",
                      color: "var(--az-stone)",
                      marginBottom: 8,
                    }}
                  >
                    {c.k}
                  </div>
                  <div
                    style={{
                      fontFamily: "var(--ff-sans)",
                      fontWeight: 500,
                      fontSize: 14,
                      lineHeight: 1.4,
                      color: "var(--az-ink)",
                    }}
                  >
                    {c.v}
                  </div>
                </div>
              ))}
            </div>
          </div>

          {/* Right: generic 3-phase diagram, named */}
          <GenericPhaseDiagram />
        </div>
      </div>
    </section>
  );
}

function GenericPhaseDiagram() {
  const ink = "var(--az-ink)";
  const muted = "var(--az-stone)";
  return (
    <div>
      <svg
        viewBox="0 0 640 360"
        width="100%"
        style={{ display: "block" }}
        role="img"
        aria-label="Generic three-phase escrow with labeled transitions."
      >
        {/* labels above */}
        <text
          x={20}
          y={28}
          style={{
            fontFamily: "var(--ff-display-alt)",
            fontSize: 11,
            letterSpacing: "0.07em",
            fill: "var(--az-stone)",
            textTransform: "uppercase",
          }}
        >
          Indicative shape · not the final API
        </text>

        {[
          { x: 30, label: "Phase 01", n: "Open" },
          { x: 240, label: "Phase 02", n: "Locked" },
          { x: 450, label: "Phase 03", n: "Released" },
        ].map((p, i) => (
          <g key={i} transform={`translate(${p.x}, 100)`}>
            <rect
              x={0}
              y={0}
              width={160}
              height={160}
              fill={i === 1 ? "var(--az-chartreuse-tint)" : "var(--az-parchment-2)"}
              stroke={ink}
              strokeWidth={1}
            />
            <text
              x={14}
              y={26}
              style={{
                fontFamily: "var(--ff-display-alt)",
                fontSize: 11,
                letterSpacing: "0.07em",
                fill: muted,
                textTransform: "uppercase",
              }}
            >
              {p.label}
            </text>
            <text
              x={80}
              y={94}
              textAnchor="middle"
              style={{
                fontFamily: "ABC Arizona Serif, Georgia, serif",
                fontWeight: 300,
                fontSize: 30,
                letterSpacing: "-0.03em",
                fill: ink,
              }}
            >
              {p.n}
            </text>
            <text
              x={80}
              y={132}
              textAnchor="middle"
              style={{
                fontFamily: "var(--ff-sans)",
                fontSize: 11,
                fill: muted,
              }}
            >
              entry / exit / Δ
            </text>
          </g>
        ))}

        {/* connectors */}
        {[
          { x1: 190, x2: 240 },
          { x1: 400, x2: 450 },
        ].map((c, i) => (
          <g key={i}>
            <line x1={c.x1} y1={180} x2={c.x2 - 4} y2={180} stroke={ink} strokeWidth={1} />
            <polygon
              points={`${c.x2 - 4},180 ${c.x2 - 12},176 ${c.x2 - 12},184`}
              fill={ink}
            />
            <text
              x={(c.x1 + c.x2) / 2}
              y={172}
              textAnchor="middle"
              style={{
                fontFamily: "var(--ff-display-alt)",
                fontSize: 10,
                letterSpacing: "0.07em",
                fill: muted,
                textTransform: "uppercase",
              }}
            >
              trigger
            </text>
          </g>
        ))}
      </svg>
    </div>
  );
}

/* =========================================================
   Section 3 — Anatomy of a phase.
   Interactive: hover/tap a phase to surface its entry, exit, Δ.
   ========================================================= */
const ANATOMY_PHASES = [
  {
    id: "open",
    num: "01",
    name: "Open",
    note: "Listing is live. Funds not yet in escrow.",
    entry: {
      title: "Entry",
      lines: [
        "Seller publishes the listing with a zero-knowledge proof of ownership.",
        "Anyone can see the item and the asking price.",
      ],
    },
    exit: {
      title: "Exit",
      lines: [
        "Buyer matches the listing and locks stablecoins into escrow.",
        "Or: seller cancels the listing.",
      ],
    },
    delta: {
      title: "State Δ",
      lines: [
        "Listing transitions to Locked.",
        "Buyer's identity becomes private state.",
        "Item and price stay public.",
      ],
    },
  },
  {
    id: "locked",
    num: "02",
    name: "Locked",
    note: "Funds held privately. Counterparty revealed only to the matched seller.",
    entry: {
      title: "Entry",
      lines: [
        "Buyer's stablecoins are locked into escrow.",
        "Seller (and only the seller) learns the buyer's Steam ID.",
      ],
    },
    exit: {
      title: "Exit",
      lines: [
        "Released: a zero-knowledge proof of the Steam trade is submitted.",
        "Refunded: 48 hours pass with no valid proof.",
      ],
    },
    delta: {
      title: "State Δ",
      lines: [
        "Phase records when the lock happened.",
        "The deal pairs buyer and seller, privately.",
      ],
    },
  },
  {
    id: "released",
    num: "03",
    name: "Released",
    note: "Terminal. Funds paid out to the seller's private balance.",
    entry: {
      title: "Entry",
      lines: [
        "The on-chain verifier accepts the proof of the trade.",
        "The proof links the listing, the buyer's Steam ID, and the item.",
      ],
    },
    exit: {
      title: "Exit",
      lines: [
        "No further transitions; this phase is terminal.",
        "Withdrawal destination stays private.",
      ],
    },
    delta: {
      title: "State Δ",
      lines: [
        "Stablecoins move to the seller's private balance.",
        "Listing flips to Released, publicly.",
      ],
    },
  },
];

function AnatomySection({ asSubsection = false }) {
  const [active, setActive] = useState("locked");
  const phase = ANATOMY_PHASES.find((p) => p.id === active);

  const Wrapper = asSubsection ? "div" : "section";
  const wrapperProps = asSubsection
    ? { style: { padding: "56px 0 24px" } }
    : {
        id: "anatomy",
        "data-screen-label": "Anatomy of a phase",
        style: {
          background: "var(--az-parchment-2)",
          padding: "120px 40px",
          borderTop: "1px solid rgba(26,20,0,0.12)",
        },
      };
  const innerStyle = asSubsection
    ? {}
    : { maxWidth: 1280, margin: "0 auto" };

  return (
    <Wrapper {...wrapperProps}>
      <div style={innerStyle}>
        <SectionHead
          number="03"
          label="Anatomy of a phase"
          title={asSubsection ? "Anatomy of a phase." : "Hover a phase. The entry, exit, and state delta surface."}
          sub={asSubsection
            ? "Hover a phase to surface its entry, exit, and state delta. Real shapes from the BAZaar reference flow; the Kit doesn't bind to these labels."
            : "Real shapes from the BAZaar reference flow. The Kit doesn't constrain naming, these are example labels."}
          asSubsection={asSubsection}
        />

        <div style={{ marginTop: 64 }}>
          <div>
            <div style={{ display: "grid", gridTemplateColumns: "repeat(3, 1fr)", gap: 16 }}>
              {ANATOMY_PHASES.map((p) => {
                const isActive = p.id === active;
                return (
                  <button
                    key={p.id}
                    onMouseEnter={() => setActive(p.id)}
                    onFocus={() => setActive(p.id)}
                    onClick={() => setActive(p.id)}
                    style={{
                      cursor: "pointer",
                      background: isActive ? "var(--az-ink)" : "var(--az-parchment)",
                      color: isActive ? "var(--az-parchment)" : "var(--az-ink)",
                      border: "1px solid var(--az-ink)",
                      padding: "24px 20px",
                      textAlign: "left",
                      display: "flex",
                      flexDirection: "column",
                      gap: 16,
                      minHeight: 200,
                      transition: "background 160ms linear, color 160ms linear",
                    }}
                  >
                    <div
                      style={{
                        display: "flex",
                        justifyContent: "space-between",
                        alignItems: "center",
                        fontFamily: "var(--ff-display-alt)",
                        fontSize: 11,
                        letterSpacing: "0.08em",
                        textTransform: "uppercase",
                        color: isActive ? "var(--az-chartreuse)" : "var(--az-stone)",
                      }}
                    >
                      <span>Phase {p.num}</span>
                      <span
                        style={{
                          width: 8,
                          height: 8,
                          background: isActive ? "var(--az-chartreuse)" : "var(--az-stone-2)",
                          display: "inline-block",
                          transform: "rotate(45deg)",
                        }}
                      />
                    </div>
                    <div
                      style={{
                        fontFamily: "ABC Arizona Serif, Georgia, serif",
                        fontWeight: 300,
                        fontSize: 40,
                        lineHeight: 0.95,
                        letterSpacing: "-0.04em",
                      }}
                    >
                      {p.name}
                    </div>
                    <div
                      style={{
                        fontFamily: "var(--ff-serif)",
                        fontSize: 14,
                        lineHeight: 1.4,
                        color: isActive ? "rgba(242,238,225,0.78)" : "var(--az-stone)",
                      }}
                    >
                      {p.note}
                    </div>
                  </button>
                );
              })}
            </div>

            {/* Detail card */}
            <div
              key={phase.id}
              style={{
                marginTop: 16,
                background: "var(--az-parchment)",
                border: "1px solid var(--az-ink)",
                padding: "32px 32px 28px",
                display: "grid",
                gridTemplateColumns: "repeat(3, 1fr)",
                gap: 32,
                animation: "fadeUp 220ms ease-out",
              }}
            >
              {["entry", "exit", "delta"].map((k) => {
                const block = phase[k];
                return (
                  <div key={k} style={{ display: "flex", flexDirection: "column", gap: 14 }}>
                    <div
                      style={{
                        display: "flex",
                        gap: 10,
                        alignItems: "center",
                      }}
                    >
                      <span
                        style={{
                          width: 10,
                          height: 10,
                          background:
                            k === "exit" ? "var(--az-chartreuse)" : "var(--az-ink)",
                          border: "1px solid var(--az-ink)",
                          display: "inline-block",
                          transform: "rotate(45deg)",
                        }}
                      />
                      <span
                        style={{
                          fontFamily: "var(--ff-display-alt)",
                          fontSize: 12,
                          letterSpacing: "0.08em",
                          textTransform: "uppercase",
                          color: "var(--az-ink)",
                        }}
                      >
                        {block.title}
                      </span>
                    </div>
                    <ul style={{ margin: 0, padding: 0, listStyle: "none", display: "flex", flexDirection: "column", gap: 8 }}>
                      {block.lines.map((line, i) => (
                        <li
                          key={i}
                          style={{
                            fontFamily: "var(--ff-serif)",
                            fontWeight: 300,
                            fontSize: 15,
                            lineHeight: 1.5,
                            color: "var(--az-ink)",
                            paddingLeft: 16,
                            position: "relative",
                          }}
                        >
                          <span
                            style={{
                              position: "absolute",
                              left: 0,
                              top: "0.7em",
                              width: 8,
                              height: 1,
                              background: "var(--az-stone)",
                            }}
                          />
                          {line}
                        </li>
                      ))}
                    </ul>
                  </div>
                );
              })}
            </div>
          </div>
        </div>
      </div>

      <style>{`
        @keyframes fadeUp {
          from { opacity: 0; transform: translateY(4px); }
          to   { opacity: 1; transform: translateY(0); }
        }
      `}</style>
    </Wrapper>
  );
}

window.PrimitiveSection = PrimitiveSection;
window.AnatomySection = AnatomySection;
