/* =========================================================
   Section 8 — Quickstart. Fork, install, deploy. Three steps.
   ========================================================= */
const QUICKSTART_STEPS = [
  {
    n: "01",
    title: "Install the skill",
    note: "A Claude skill packaged with the Kit. The docs walk through the install.",
    mode: "link",
    code: "https://docs.aztec.network/escrow-kit/install-skill",
  },
  {
    n: "02",
    title: "Describe the escrow",
    note: "In Claude, run the skill on a use-case sentence. You get phase definitions, transition triggers, and an Aztec.nr project to edit.",
    mode: "prompt",
    code:
      "Design a private bug-bounty escrow that\nreleases when a maintainer's GitHub merges\nthe fix PR",
  },
  {
    n: "03",
    title: "Deploy from Claude",
    note: "Ask Claude to deploy the project to Aztec. The skill knows the toolchain.",
    mode: "prompt",
    code: "Deploy this project to Aztec",
    docsLink: {
      label: "or deploy manually →",
      href: "https://docs.aztec.network/developers/getting_started_on_testnet#step-4-deploy-a-contract",
    },
  },
];

function CopyableLine({ children, mode = "shell" }) {
  const [copied, setCopied] = useState(false);
  const onCopy = () => {
    if (!navigator.clipboard) return;
    navigator.clipboard.writeText(children).then(() => {
      setCopied(true);
      setTimeout(() => setCopied(false), 1400);
    });
  };
  const isPrompt = mode === "prompt";
  const isLink = mode === "link";

  if (isLink) {
    return (
      <a
        href={children}
        target="_blank"
        rel="noreferrer noopener"
        style={{
          background: "var(--az-ink)",
          color: "var(--az-parchment)",
          padding: "16px 18px",
          display: "flex",
          justifyContent: "space-between",
          alignItems: "center",
          gap: 12,
          borderTop: "1px solid var(--az-ink)",
          minHeight: 104,
          textDecoration: "none",
          fontFamily: "var(--ff-sans)",
          fontWeight: 500,
          fontSize: 13,
        }}
      >
        <span
          style={{
            color: "var(--az-parchment)",
            display: "inline-flex",
            alignItems: "center",
            gap: 10,
            flex: 1,
            minWidth: 0,
          }}
        >
          <span
            style={{
              color: "var(--az-chartreuse)",
              fontFamily: "var(--ff-mono)",
              flexShrink: 0,
            }}
          >
            →
          </span>
          <span style={{ overflow: "hidden", textOverflow: "ellipsis", whiteSpace: "nowrap" }}>
            {children}
          </span>
        </span>
        <span
          style={{
            border: "1px solid rgba(242,238,225,0.4)",
            color: "var(--az-chartreuse)",
            fontFamily: "var(--ff-display-alt)",
            fontSize: 10,
            letterSpacing: "0.07em",
            padding: "5px 10px",
            textTransform: "uppercase",
            flexShrink: 0,
          }}
        >
          Open
        </span>
      </a>
    );
  }

  return (
    <div
      style={{
        background: "var(--az-ink)",
        color: "var(--az-parchment)",
        padding: "16px 18px",
        display: "flex",
        justifyContent: "space-between",
        alignItems: "flex-start",
        gap: 12,
        borderTop: "1px solid var(--az-ink)",
        minHeight: 104,
      }}
    >
      <pre
        style={{
          margin: 0,
          fontFamily: isPrompt ? "var(--ff-serif)" : "var(--ff-mono)",
          fontWeight: isPrompt ? 300 : 400,
          fontStyle: isPrompt ? "italic" : "normal",
          fontSize: isPrompt ? 14 : 13,
          lineHeight: 1.5,
          color: "var(--az-parchment)",
          whiteSpace: "pre-wrap",
          overflowX: "auto",
          flex: 1,
        }}
      >
        <span
          style={{
            color: "var(--az-chartreuse)",
            fontFamily: "var(--ff-mono)",
            fontStyle: "normal",
            fontWeight: 400,
            marginRight: 8,
            display: "inline-block",
          }}
        >
          {isPrompt ? ">" : "$"}
        </span>
        {children}
      </pre>
      <button
        onClick={onCopy}
        style={{
          background: "transparent",
          border: "1px solid rgba(242,238,225,0.4)",
          color: copied ? "var(--az-chartreuse)" : "var(--az-parchment)",
          fontFamily: "var(--ff-display-alt)",
          fontSize: 10,
          letterSpacing: "0.07em",
          padding: "5px 10px",
          textTransform: "uppercase",
          cursor: "pointer",
          flexShrink: 0,
        }}
      >
        {copied ? "Copied" : "Copy"}
      </button>
    </div>
  );
}

function QuickstartSection({ asSubsection = false }) {
  const Wrapper = asSubsection ? "div" : "section";
  const wrapperProps = asSubsection
    ? { style: { padding: "56px 0 24px" } }
    : {
        id: "quickstart",
        "data-screen-label": "Quickstart",
        style: {
          background: "var(--az-parchment)",
          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="08"
          label="Quickstart"
          title={asSubsection ? "Quickstart." : "Fork. Build. Deploy. Before your coffee cools."}
          sub="From a use-case sentence to a deployed escrow on Aztec testnet. The Kit ships composable primitives and the skill scaffolds the rest."
          asSubsection={asSubsection}
        />

        <div
          style={{
            marginTop: 64,
            display: "grid",
            gridTemplateColumns: "repeat(3, 1fr)",
            gap: 0,
            border: "1px solid var(--az-ink)",
          }}
          className="qs-grid"
        >
          {QUICKSTART_STEPS.map((s, i) => (
            <div
              key={s.n}
              style={{
                display: "flex",
                flexDirection: "column",
                borderRight: i < 2 ? "1px solid var(--az-ink)" : "none",
                background: "var(--az-parchment-2)",
              }}
            >
              <div style={{ padding: "28px 28px 22px", display: "flex", flexDirection: "column", gap: 12, minHeight: 220 }}>
                <div style={{ display: "flex", alignItems: "center", gap: 12 }}>
                  <span
                    style={{
                      fontFamily: "var(--ff-display-alt)",
                      fontSize: 14,
                      letterSpacing: "0.05em",
                      color: "var(--az-ink-soft)",
                    }}
                  >
                    {s.n}
                  </span>
                  <span
                    style={{
                      width: 28,
                      height: 1,
                      background: "var(--az-ink)",
                      display: "inline-block",
                    }}
                  />
                  {s.mode === "prompt" && (
                    <span
                      style={{
                        fontFamily: "var(--ff-display-alt)",
                        fontSize: 10,
                        letterSpacing: "0.08em",
                        textTransform: "uppercase",
                        color: "var(--az-ink-soft)",
                        background: "var(--az-chartreuse-tint)",
                        border: "1px solid var(--az-chartreuse-deep)",
                        padding: "3px 7px",
                      }}
                    >
                      Claude prompt
                    </span>
                  )}
                </div>
                <h3
                  style={{
                    margin: 0,
                    fontFamily: "ABC Arizona Serif, Georgia, serif",
                    fontWeight: 300,
                    fontSize: 32,
                    lineHeight: 1,
                    letterSpacing: "-0.03em",
                    color: "var(--az-ink)",
                  }}
                >
                  {s.title}
                </h3>
                <p
                  style={{
                    margin: 0,
                    fontFamily: "var(--ff-serif)",
                    fontWeight: 300,
                    fontSize: 15,
                    lineHeight: 1.5,
                    color: "var(--az-ink-soft)",
                  }}
                >
                  {s.note}
                </p>
                {s.docsLink && (
                  <a
                    href={s.docsLink.href}
                    style={{
                      marginTop: "auto",
                      fontFamily: "var(--ff-sans)",
                      fontWeight: 500,
                      fontSize: 13,
                      color: "var(--az-ink)",
                      textDecoration: "none",
                      borderBottom: "1px solid var(--az-ink)",
                      paddingBottom: 2,
                      alignSelf: "flex-start",
                    }}
                  >
                    {s.docsLink.label}
                  </a>
                )}
              </div>
              <div style={{ marginTop: "auto" }}>
                <CopyableLine mode={s.mode}>{s.code}</CopyableLine>
              </div>
            </div>
          ))}
        </div>

      </div>
    </Wrapper>
  );
}

/* =========================================================
   Section 9 — What you can build.
   Quiet 9-card grid, one line + short paragraph.
   ========================================================= */
const USE_CASES = [
  { t: "Bug-bounty escrow", b: "Release on a zkTLS proof that the maintainer's PR landed and the issue is closed." },
  { t: "Freelance milestone escrow", b: "Release on verified delivery: signed acceptance, signed Git commit, posted artifact." },
  { t: "Subscription with refund-on-failure", b: "Release tranches when uptime feeds verify service-level guarantees." },
  { t: "Private prediction settlement", b: "Release on a verified outcome from a trusted feed; the order book stays private until resolution." },
  { t: "Coordination games", b: "Funded commitments that release when a group hits a verifiable threshold off-chain." },
  { t: "Conditional grants", b: "Release tranches on verified milestones: preprint posted, repo MIT-licensed, AZIP merged." },
];

function UseCasesSection({ asSubsection = false, dark = false, featuredIdx = -1 }) {
  const Wrapper = asSubsection ? "div" : "section";
  const wrapperProps = asSubsection
    ? { style: { padding: "8px 0 24px" } }
    : {
        id: "use-cases",
        "data-screen-label": "What you can build",
        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="09"
          color={dark ? "dark" : undefined}
          label="Build your version"
          title={asSubsection ? "Got an idea? Ship it." : "Prompts for the imagination, not promises."}
          sub="Coordination problems the Kit already fits. Pick the one you want to exist; the skill will shape the phases."
          asSubsection={asSubsection}
        />

        <div
          style={{
            marginTop: 56,
            display: "grid",
            gridTemplateColumns: "repeat(3, 1fr)",
            gap: 0,
            border: dark ? "1px solid rgba(242,238,225,0.18)" : "1px solid var(--az-ink)",
          }}
          className="usecase-grid"
        >
          {USE_CASES.map((u, i) => (
            <UseCard key={u.t} u={u} idx={i} total={USE_CASES.length} dark={dark} featured={i === featuredIdx} />
          ))}
        </div>
      </div>
    </Wrapper>
  );
}

function UseCard({ u, idx, total, dark = false, featured = false }) {
  const col = idx % 3;
  const row = Math.floor(idx / 3);
  const lastRow = Math.ceil(total / 3) - 1;
  const borderColor = dark ? "rgba(242,238,225,0.18)" : "var(--az-ink)";
  const bg = featured ? "var(--az-chartreuse-tint)" : "var(--az-parchment)";
  return (
    <div
      style={{
        padding: "32px 28px",
        borderRight: col < 2 ? `1px solid ${borderColor}` : "none",
        borderBottom: row < lastRow ? `1px solid ${borderColor}` : "none",
        background: bg,
        display: "flex",
        flexDirection: "column",
        gap: 16,
        minHeight: 220,
      }}
    >
      <div
        style={{
          fontFamily: "var(--ff-display-alt)",
          fontSize: 11,
          letterSpacing: "0.08em",
          color: featured ? "var(--az-ink-soft)" : "var(--az-stone)",
          textTransform: "uppercase",
        }}
      >
        {String(idx + 1).padStart(2, "0")}
      </div>
      <h3
        style={{
          margin: 0,
          fontFamily: "ABC Arizona Serif, Georgia, serif",
          fontWeight: 300,
          fontStyle: featured ? "italic" : "normal",
          fontSize: 28,
          lineHeight: 1.0,
          letterSpacing: "-0.03em",
          color: "var(--az-ink)",
        }}
      >
        {u.t}
      </h3>
      <p
        style={{
          margin: 0,
          fontFamily: "var(--ff-serif)",
          fontWeight: 300,
          fontSize: 15,
          lineHeight: 1.5,
          color: "var(--az-ink-soft)",
        }}
      >
        {u.b}
      </p>
    </div>
  );
}

/* =========================================================
   Section 10 — FAQ.
   ========================================================= */
const FAQ_ITEMS = [
  {
    q: "Why Aztec, not an EVM L2?",
    a: "Aztec extends Ethereum with native private state. EVM L2s expose every transition publicly; a private escrow on a transparent rollup is private only at the application layer, which leaks every change in account balance. On Aztec the encrypted state tree is the default; selective disclosure is opt-in.",
  },
  {
    q: "What does zkTLS actually prove?",
    a: "Primus zkTLS proves the content of an HTTPS response without revealing the session key. The verifier on-chain accepts a proof that says, in effect, this URL, signed by this TLS cert, returned this byte range. Useful for verifying off-chain state without trusting a single oracle operator.",
  },
  {
    q: "What happens if the trigger never fires?",
    a: "Every phase has an exit. The Kit's reference flows pair the primary trigger with a TimeLock fallback (48h in BAZaar), so funds either release on the verified fact or refund on the deadline. Your fork can compose other fallbacks: arbiter signature, partial refund, escalation to a second proof type.",
  },
  {
    q: "Can I use signatures or oracles instead of zkTLS?",
    a: "Yes. The Kit doesn't bind to a trigger type. The TriggerCardRow on this page shows the supported families today: zkTLS, signature, oracle, time lock, on-chain event. Add your own by implementing the trigger interface.",
  },
  {
    q: "Does the skill write the smart contract?",
    a: "Yes. From a use-case sentence, the skill produces an Aztec.nr implementation of the escrow you described, with the phases and triggers from your prompt wired into the contract. Treat the output as the basis to work from: the application logic, the trigger predicates, and the privacy decisions are yours to refine before deploying.",
  },
  {
    q: "Is BAZaar audited?",
    a: "No. BAZaar is a best-effort proof of concept and will not be audited. It's labeled as such in the app and the repo. Don't lock funds you can't afford to lose. The Kit itself goes through its own review track; BAZaar is the demo.",
  },
];

function FAQSection() {
  const [open, setOpen] = useState(0);
  return (
    <section
      id="faq"
      data-screen-label="FAQ"
      style={{
        background: "var(--az-parchment)",
        padding: "120px 40px",
        borderTop: "1px solid rgba(26,20,0,0.12)",
      }}
    >
      <div style={{ maxWidth: 1280, margin: "0 auto" }}>
        <div
          style={{
            display: "grid",
            gridTemplateColumns: "minmax(0, 0.65fr) minmax(0, 1fr)",
            gap: 64,
            alignItems: "flex-start",
          }}
          className="faq-grid"
        >
          <SectionHead
            number="10"
            label="FAQ"
            title="Short and technical."
          />
          <ul style={{ margin: 0, padding: 0, listStyle: "none", borderTop: "1px solid var(--az-ink)" }}>
            {FAQ_ITEMS.map((it, i) => {
              const isOpen = open === i;
              return (
                <li key={i} style={{ borderBottom: "1px solid var(--az-ink)" }}>
                  <button
                    onClick={() => setOpen(isOpen ? -1 : i)}
                    style={{
                      width: "100%",
                      background: "transparent",
                      border: 0,
                      cursor: "pointer",
                      padding: "22px 4px",
                      display: "flex",
                      justifyContent: "space-between",
                      alignItems: "center",
                      gap: 16,
                      textAlign: "left",
                    }}
                  >
                    <span
                      style={{
                        fontFamily: "var(--ff-display-alt)",
                        fontSize: 11,
                        letterSpacing: "0.08em",
                        textTransform: "uppercase",
                        color: "var(--az-stone)",
                        minWidth: 32,
                      }}
                    >
                      {String(i + 1).padStart(2, "0")}
                    </span>
                    <span
                      style={{
                        flex: 1,
                        fontFamily: "ABC Arizona Serif, Georgia, serif",
                        fontWeight: 300,
                        fontSize: 24,
                        lineHeight: 1.15,
                        letterSpacing: "-0.025em",
                        color: "var(--az-ink)",
                      }}
                    >
                      {it.q}
                    </span>
                    <span
                      style={{
                        width: 28,
                        height: 28,
                        border: "1px solid var(--az-ink)",
                        display: "inline-flex",
                        alignItems: "center",
                        justifyContent: "center",
                        flexShrink: 0,
                        background: isOpen ? "var(--az-ink)" : "transparent",
                        color: isOpen ? "var(--az-chartreuse)" : "var(--az-ink)",
                        transition: "background 120ms linear, color 120ms linear",
                      }}
                    >
                      <span
                        style={{
                          display: "inline-block",
                          fontFamily: "var(--ff-mono)",
                          fontSize: 16,
                          lineHeight: 1,
                        }}
                      >
                        {isOpen ? "–" : "+"}
                      </span>
                    </span>
                  </button>
                  <div
                    style={{
                      maxHeight: isOpen ? 400 : 0,
                      overflow: "hidden",
                      transition: "max-height 240ms ease",
                    }}
                  >
                    <p
                      style={{
                        margin: 0,
                        padding: "0 56px 26px 52px",
                        fontFamily: "var(--ff-serif)",
                        fontWeight: 300,
                        fontSize: 17,
                        lineHeight: 1.55,
                        color: "var(--az-ink-soft)",
                      }}
                    >
                      {it.a}
                    </p>
                  </div>
                </li>
              );
            })}
          </ul>
        </div>
      </div>
    </section>
  );
}

/* =========================================================
   Section 11 — Footer CTA.
   ========================================================= */
function FooterCTASection() {
  return (
    <section
      id="footer"
      data-screen-label="Footer CTA"
      data-surface="dark"
      style={{
        background: "var(--az-malachite)",
        color: "var(--az-parchment)",
        padding: "120px 40px 60px",
        position: "relative",
        overflow: "hidden",
      }}
    >
      <div style={{ maxWidth: 1280, margin: "0 auto", position: "relative" }}>
        <div
          style={{
            fontFamily: "var(--ff-display-alt)",
            fontSize: 12,
            letterSpacing: "0.08em",
            textTransform: "uppercase",
            color: "var(--az-chartreuse)",
            marginBottom: 32,
          }}
        >
          Ready when you are
        </div>
        <h2
          style={{
            margin: 0,
            fontFamily: "ABC Arizona Serif, Georgia, serif",
            fontWeight: 300,
            fontSize: "clamp(56px, 8vw, 132px)",
            lineHeight: 0.9,
            letterSpacing: "-0.045em",
            color: "var(--az-parchment)",
            maxWidth: 1100,
          }}
        >
          Install it. Ship something{" "}
          <em
            style={{
              fontStyle: "italic",
              color: "var(--az-chartreuse)",
            }}
          >
            private.
          </em>
        </h2>
        <p
          style={{
            marginTop: 32,
            fontFamily: "var(--ff-serif)",
            fontWeight: 300,
            fontStyle: "italic",
            fontSize: 22,
            lineHeight: 1.4,
            color: "rgba(242,238,225,0.72)",
            maxWidth: 720,
          }}
        >
          Aztec extends Ethereum. The Kit extends Aztec. Whatever you can verify on-chain
          can release the funds.
        </p>
        <div style={{ marginTop: 48, display: "flex", gap: 14, flexWrap: "wrap" }}>
          <CTA as="a" href="https://github.com/AztecProtocol/escrow-kit">
            Install the Escrow Kit →
          </CTA>
          <CTA as="a" href="https://discord.gg/aztec" variant="ghost-dark">
            Join our Discord
          </CTA>
        </div>

        <div
          style={{
            marginTop: 96,
            paddingTop: 28,
            borderTop: "1px solid rgba(242,238,225,0.18)",
            display: "flex",
            justifyContent: "space-between",
            alignItems: "center",
            flexWrap: "wrap",
            gap: 16,
          }}
        >
          <div style={{ display: "flex", alignItems: "center", gap: 14 }}>
            <BrandMark size={24} style={{ color: "var(--az-chartreuse)" }} />
            <img
              src="design/assets/logos/aztec-wordmark-light.svg"
              alt="Aztec"
              style={{ height: 22 }}
            />
            <span
              style={{
                fontFamily: "var(--ff-display-alt)",
                fontSize: 12,
                letterSpacing: "0.06em",
                textTransform: "uppercase",
                color: "var(--az-chartreuse)",
                padding: "4px 8px",
                border: "1px solid var(--az-chartreuse)",
              }}
            >
              Escrow Kit
            </span>
          </div>
          <div
            style={{
              fontFamily: "var(--ff-mono)",
              fontSize: 12,
              color: "rgba(242,238,225,0.6)",
            }}
          >
            MIT · built on Aztec
          </div>
        </div>
      </div>

      {/* Decorative diamond array, bottom-right */}
      <div
        aria-hidden="true"
        style={{
          position: "absolute",
          right: -40,
          bottom: -40,
          opacity: 0.06,
          color: "var(--az-chartreuse)",
        }}
      >
        <BrandMark size={280} />
      </div>
    </section>
  );
}

window.QuickstartSection = QuickstartSection;
window.UseCasesSection = UseCasesSection;
window.FAQSection = FAQSection;
window.FooterCTASection = FooterCTASection;
