# Onboarding guide

A setup guide that stays open beside the chat while the user works through 3–7 steps in your app. The agent explains the current step; only your app ticks a step off.

{/* Generated by scripts/build-ui-catalog.mjs from src/ui-catalog/. Do not edit. */}

A setup guide that stays open beside the chat while the user works through 3–7 steps in your app. The agent explains the current step; only your app ticks a step off.

## When to use it

**Use it when:** A new user needs to finish 3–7 setup steps in your app, in order. The guide stays open beside the chat while the user works and the agent explains the current step.

**Use something else when:** One step (tell the user in chat, or spotlight it). Steps the agent does itself (that's an action, not a guide). Mobile: the canvas isn't available; use the checklist card (mountMobile).

## What the agent receives

| The template calls | The agent gets | When |
| --- | --- | --- |
| `canvas.setState(patch)` | `Canvas state` | A step is done (your app, or “I've done this”). The agent sees it with the user's next message. |
| `displayed()` | `FUNCTION_TOOL_DISPLAYED` | Mobile checklist card: as soon as it shows. |

## States

Besides its data, the template draws these as itself: `mountState()` on the canvas, `mountMobileState()` in the mobile card. Pick one on the preview with **State**.

| State | When it shows |
| --- | --- |
| Loading | The guide opens at once while your app reads which steps the user already did, and again after Try again. |
| Empty | Your app has no setup steps for this user, for example a teammate who joined a workspace an admin already set up. |
| Error | Your app couldn't read the user's progress. Try again loads the steps in place; steps already done stay done. |

```ts
mountState({ root, head, title: "…", state: { kind: "error", text: "…", retry } });
mountMobileState({ root, head, title: "…", state: { kind: "error", text: "…", retry } });
```

The hosted action and the in-page handler below show each case. The state types are `LoadingState`, `EmptyState` and `ErrorState` in fs-kit.

## Data

### OnboardingState

| Field | Type | Notes |
| --- | --- | --- |
| `title` | `string` |  |
| `intro` (optional) | `string` |  |
| `canvasTitle` (optional) | `string` | The canvas header (setTitle, up to 80 characters). |
| `eyebrow` (optional) | `string` |  |
| `doneTitle` (optional) | `string` |  |
| `doneText` (optional) | `string` |  |
| `steps` | `OnboardingStep[]` |  |
| `current` | `string \| null` | The step the user is on. The agent moves it only when the user asks. |

### OnboardingStep

| Field | Type | Notes |
| --- | --- | --- |
| `id` | `string` |  |
| `title` | `string` |  |
| `detail` | `string` |  |
| `done` | `boolean` | Only your app sets this, when the user really did the step. The agent never does. |
| `minutes` (optional) | `number` |  |
| `cta` (optional) | `{ label: string; href?: string }` | The step's button. Without actions.go, href is opened. |
| `auto` (optional) | `boolean` | Your app detects this step itself: hides "I've done this". |

### OnboardingActions

| Field | Type | Notes |
| --- | --- | --- |
| `go()` (optional) | `(step: OnboardingStep) => void` | Take the user to the step in your app (navigate, spotlight). |
| `finish()` (optional) | `() => void` | The last screen's button. Default: close the canvas. |
| `subscribe()` (optional) | `(onChange: (patch: Partial<OnboardingState>) => void) => () => void` | Your app reports progress here (a step the user really finished). The guide redraws and shares it with the agent through canvas.setState. Return the unsubscribe. |

## Install

Copy the template and the shared kit into your code: `agent/ui/` in an agent repo, or anywhere in your app. The template imports only `./fs-kit.js`. Then wire it up from the hosted action or the in-page handler.

- Hosted action: [TypeScript](/ui-catalog/files/ts/usage/onboarding-canvas.hosted.ts), [JavaScript](/ui-catalog/files/js/usage/onboarding-canvas.hosted.js)
- In-page handler: [TypeScript](/ui-catalog/files/ts/usage/onboarding-canvas.canvas.ts), [JavaScript](/ui-catalog/files/js/usage/onboarding-canvas.canvas.js)
- onboarding-canvas: [TypeScript](/ui-catalog/files/ts/onboarding-canvas.ts), [JavaScript](/ui-catalog/files/js/onboarding-canvas.js)
- fs-kit: [TypeScript](/ui-catalog/files/ts/fs-kit.ts), [JavaScript](/ui-catalog/files/js/fs-kit.js)

The hosted action needs an agent repo with one folder per action (`agent/actions/<action_key>/index.ts`). If your repo keeps its actions in `agent/actions/index.ts`, use the in-page handler: it runs the same template in your app.

All templates in machine-readable form: [/ui-catalog/catalog.json](/ui-catalog/catalog.json).

## Code

### Hosted action

```ts title="agent/actions/open_setup_guide/index.ts"
// agent/actions/open_setup_guide/index.ts: a hosted canvas action (desktop).
// Copy the template (onboarding-canvas.ts) and fs-kit.ts into agent/ui/.

type Result = OnboardingState | { error: string };

// The canvas's first state: the steps and which ones the user already did, from your app.
// A failure goes back as data: the agent explains it, and the canvas shows it.
export async function execute(args: {}, ctx: ActionContext): Promise<Result> {
  try {
    return { title: "Set up your workspace", canvasTitle: "Getting started", ...(await progress(ctx)) };
  } catch {
    return { error: "Your setup steps didn't load. Nothing changed." };
  }
}

async function progress(ctx: ActionContext): Promise<Pick<OnboardingState, "steps" | "current">> {
  const res = await ctx.fetch("/api/onboarding");
  if (!res.ok) throw new Error(`onboarding ${res.status}`);
  return res.json(); // { steps: [{ id, title, detail, done }], current }
}

export function renderCanvas(ctx: CanvasActionContext) {
  const canvas = liveCanvas(ctx.canvas); // canvas.state follows your setState calls and the agent's edits
  canvas.setType("ONBOARDING");
  const at = { root: canvas.root, head: canvas.head, canvas, title: "Set up your workspace" };
  let cleanup: Cleanup = () => {};

  // Error, no steps for this user (an admin already set the workspace up), or the guide.
  const draw = (): void => {
    cleanup();
    const st = canvas.state;
    if (st.error) {
      cleanup = mountState({ ...at, state: { kind: "error", title: "Couldn't load your setup steps", text: st.error, retry } });
    } else if (!st.steps?.length) {
      cleanup = mountState({ ...at, state: { kind: "empty", icon: "check", title: "Nothing to set up",
        text: "Your admin already set up this workspace. Ask me anything about getting started.",
        action: { label: "Close guide", onClick: () => canvas.close() } } });
    } else {
      // READ_MODE (default): the agent explains steps but can't tick them. Your app does, through setState.
      cleanup = mount({
        ...at,
        actions: {
          go: (step) => { if (step.cta?.href) ctx.nav.go(step.cta.href); },
          // Each time the user moves on in your app, re-read which steps are really done.
          subscribe: (onChange) => {
            const off = ctx.page.on({ event: "urlchange" }, async () => onChange(await progress(ctx)));
            return () => { off.then((stop) => stop()); };
          },
        },
      });
    }
  };

  // Try again: the steps as bones while they load, then the guide, or the error again.
  async function retry(): Promise<void> {
    cleanup();
    cleanup = mountState({ ...at, state: { kind: "loading", label: "Loading your setup steps…" } });
    const next = await progress(ctx).then((p) => ({ ...p, error: null })).catch(() => ({ error: "Still not loading. Try again in a minute." }));
    canvas.setState(next); // the agent reads the canvas state
    draw();
  }

  draw();
  return () => cleanup();
}
```

```js title="agent/actions/open_setup_guide/index.js"
// agent/actions/open_setup_guide/index.js: a hosted canvas action (desktop).
// Copy the template (onboarding-canvas.ts) and fs-kit.ts into agent/ui/.

// The canvas's first state: the steps and which ones the user already did, from your app.
// A failure goes back as data: the agent explains it, and the canvas shows it.
export async function execute(args, ctx) {
  try {
    return { title: "Set up your workspace", canvasTitle: "Getting started", ...(await progress(ctx)) };
  } catch {
    return { error: "Your setup steps didn't load. Nothing changed." };
  }
}

async function progress(ctx) {
  const res = await ctx.fetch("/api/onboarding");
  if (!res.ok) throw new Error(`onboarding ${res.status}`);
  return res.json(); // { steps: [{ id, title, detail, done }], current }
}

export function renderCanvas(ctx) {
  const canvas = liveCanvas(ctx.canvas); // canvas.state follows your setState calls and the agent's edits
  canvas.setType("ONBOARDING");
  const at = { root: canvas.root, head: canvas.head, canvas, title: "Set up your workspace" };
  let cleanup = () => {};

  // Error, no steps for this user (an admin already set the workspace up), or the guide.
  const draw = () => {
    cleanup();
    const st = canvas.state;
    if (st.error) {
      cleanup = mountState({ ...at, state: { kind: "error", title: "Couldn't load your setup steps", text: st.error, retry } });
    } else if (!st.steps?.length) {
      cleanup = mountState({ ...at, state: { kind: "empty", icon: "check", title: "Nothing to set up",
        text: "Your admin already set up this workspace. Ask me anything about getting started.",
        action: { label: "Close guide", onClick: () => canvas.close() } } });
    } else {
      // READ_MODE (default): the agent explains steps but can't tick them. Your app does, through setState.
      cleanup = mount({
        ...at,
        actions: {
          go: (step) => { if (step.cta?.href) ctx.nav.go(step.cta.href); },
          // Each time the user moves on in your app, re-read which steps are really done.
          subscribe: (onChange) => {
            const off = ctx.page.on({ event: "urlchange" }, async () => onChange(await progress(ctx)));
            return () => { off.then((stop) => stop()); };
          },
        },
      });
    }
  };

  // Try again: the steps as bones while they load, then the guide, or the error again.
  async function retry() {
    cleanup();
    cleanup = mountState({ ...at, state: { kind: "loading", label: "Loading your setup steps…" } });
    const next = await progress(ctx).then((p) => ({ ...p, error: null })).catch(() => ({ error: "Still not loading. Try again in a minute." }));
    canvas.setState(next); // the agent reads the canvas state
    draw();
  }

  draw();
  return () => cleanup();
}
```

### In-page handler

```ts title="in-page handler.ts"
// An in-page canvas action (desktop). The SDK leaves canvas actions out on mobile,
// so mobile gets the checklist card below.

type Result = OnboardingState | { error: string };

const TITLE = "Set up your workspace";
// A failure goes back as data, so the agent can explain it.
const load = (): Promise<Result> => loadOnboarding().catch(() => ({ error: "Your setup steps didn't load. Nothing changed." }));
const NOTHING: EmptyState = { kind: "empty", icon: "check", title: "Nothing to set up", text: "Your admin already set up this workspace. Ask me anything about getting started." };

foldspace("when", "ready", () => {
  foldspace.agent({ /* …common setup… */ }).addActionHandlers({
    open_setup_guide: {
      execute: async (): Promise<Result> => load(), // { title, steps, current }, or { error }

      renderCanvas: (sdk: CanvasContext) => {
        const ctx = liveCanvas(sdk); // ctx.state follows your setState calls and the agent's edits
        ctx.setType("ONBOARDING");
        // READ_MODE (the default): the agent explains the steps but can't tick one or move `current`. Your app does, through setState.
        const at = { root: ctx.host, head: ctx.header, canvas: ctx, title: TITLE };
        let cleanup: Cleanup = () => {};
        // The canvas state is what execute returned: an error, no steps for this user, or the guide.
        const draw = (): void => {
          cleanup();
          const st = ctx.state;
          if (st.error) cleanup = mountState({ ...at, state: { kind: "error", title: "Couldn't load your setup steps", text: st.error, retry } });
          else if (!st.steps?.length) cleanup = mountState({ ...at, state: { ...NOTHING, action: { label: "Close guide", onClick: () => ctx.close() } } });
          else cleanup = mount({
            ...at,
            actions: {
              go: (step) => router.push(step.cta?.href ?? "/"),
              // Your app ticks a step when the user really does it. The agent never does.
              subscribe: (onChange) => onboardingStore.subscribe((s: OnboardingState) => onChange({ steps: s.steps, current: s.current })),
            },
          });
        };
        // Try again: the steps as bones while they load, then the guide (or the error again).
        async function retry(): Promise<void> {
          cleanup();
          cleanup = mountState({ ...at, state: { kind: "loading", label: "Loading your setup steps…" } });
          const next = await load();
          ctx.setState("error" in next ? next : { ...next, error: null }); // the agent reads the canvas state
          draw();
        }
        draw();
        return () => cleanup();
      },
    },

    // Mobile: a read-only checklist card with the same data, or the same states as a card.
    show_setup_checklist: {
      execute: async (): Promise<Result> => load(),
      renderComponent: ({ data, host, header, readyState }: ComponentContext<Result>) => {
        if (!("error" in data) && data.steps.length) return void mountMobile({ root: host, head: header, data, api: { displayed: () => readyState(data) } });
        mountMobileState({ root: host, head: header, title: TITLE, state: "error" in data ? { kind: "error", text: data.error } : NOTHING });
        readyState(data); // read-only: the agent goes on once it's shown
      },
    },
  });
});
```

```js title="in-page handler.js"
// An in-page canvas action (desktop). The SDK leaves canvas actions out on mobile,
// so mobile gets the checklist card below.

const TITLE = "Set up your workspace";
// A failure goes back as data, so the agent can explain it.
const load = () => loadOnboarding().catch(() => ({ error: "Your setup steps didn't load. Nothing changed." }));
const NOTHING = { kind: "empty", icon: "check", title: "Nothing to set up", text: "Your admin already set up this workspace. Ask me anything about getting started." };

foldspace("when", "ready", () => {
  foldspace.agent({ /* …common setup… */ }).addActionHandlers({
    open_setup_guide: {
      execute: async () => load(), // { title, steps, current }, or { error }

      renderCanvas: (sdk) => {
        const ctx = liveCanvas(sdk); // ctx.state follows your setState calls and the agent's edits
        ctx.setType("ONBOARDING");
        // READ_MODE (the default): the agent explains the steps but can't tick one or move `current`. Your app does, through setState.
        const at = { root: ctx.host, head: ctx.header, canvas: ctx, title: TITLE };
        let cleanup = () => {};
        // The canvas state is what execute returned: an error, no steps for this user, or the guide.
        const draw = () => {
          cleanup();
          const st = ctx.state;
          if (st.error) cleanup = mountState({ ...at, state: { kind: "error", title: "Couldn't load your setup steps", text: st.error, retry } });
          else if (!st.steps?.length) cleanup = mountState({ ...at, state: { ...NOTHING, action: { label: "Close guide", onClick: () => ctx.close() } } });
          else cleanup = mount({
            ...at,
            actions: {
              go: (step) => router.push(step.cta?.href ?? "/"),
              // Your app ticks a step when the user really does it. The agent never does.
              subscribe: (onChange) => onboardingStore.subscribe((s) => onChange({ steps: s.steps, current: s.current })),
            },
          });
        };
        // Try again: the steps as bones while they load, then the guide (or the error again).
        async function retry() {
          cleanup();
          cleanup = mountState({ ...at, state: { kind: "loading", label: "Loading your setup steps…" } });
          const next = await load();
          ctx.setState("error" in next ? next : { ...next, error: null }); // the agent reads the canvas state
          draw();
        }
        draw();
        return () => cleanup();
      },
    },

    // Mobile: a read-only checklist card with the same data, or the same states as a card.
    show_setup_checklist: {
      execute: async () => load(),
      renderComponent: ({ data, host, header, readyState }) => {
        if (!("error" in data) && data.steps.length) return void mountMobile({ root: host, head: header, data, api: { displayed: () => readyState(data) } });
        mountMobileState({ root: host, head: header, title: TITLE, state: "error" in data ? { kind: "error", text: data.error } : NOTHING });
        readyState(data); // read-only: the agent goes on once it's shown
      },
    },
  });
});
```

### onboarding-canvas

```ts title="agent/ui/onboarding-canvas.ts"
// Template: onboarding-canvas. An ONBOARDING canvas guide next to the chat (desktop), plus its mobile checklist card.

export interface OnboardingStep {
  id: string;
  title: string;
  detail: string;
  /** Only your app sets this, when the user really did the step. The agent never does. */
  done: boolean;
  minutes?: number;
  /** The step's button. Without actions.go, href is opened. */
  cta?: { label: string; href?: string };
  /** Your app detects this step itself: hides "I've done this". */
  auto?: boolean;
}

/** The SDK's ONBOARDING convention ({ steps, current }), plus display fields. */
export interface OnboardingState {
  title: string;
  intro?: string;
  /** The canvas header (setTitle, up to 80 characters). */
  canvasTitle?: string;
  eyebrow?: string;
  doneTitle?: string;
  doneText?: string;
  steps: OnboardingStep[];
  /** The step the user is on. The agent moves it only when the user asks. */
  current: string | null;
}

export interface OnboardingActions {
  /** Take the user to the step in your app (navigate, spotlight). */
  go?(step: OnboardingStep): void;
  /** The last screen's button. Default: close the canvas. */
  finish?(): void;
  /**
   * Your app reports progress here (a step the user really finished). The guide redraws
   * and shares it with the agent through canvas.setState. Return the unsubscribe.
   */
  subscribe?(onChange: (patch: Partial<OnboardingState>) => void): () => void;
}

export const meta = {
  id: "onboarding-canvas",
  title: "Onboarding guide",
  surface: "canvas",
  canvasType: "ONBOARDING",
  kind: "interactive",
  mobile: "fallback-card",
  whenToUse: "A new user needs to finish 3–7 setup steps in your app, in order. The guide stays open beside the chat while the user works and the agent explains the current step.",
  whenNotToUse: "One step (tell the user in chat, or spotlight it). Steps the agent does itself (that's an action, not a guide). Mobile: the canvas isn't available; use the checklist card (mountMobile).",
};

const CSS = `
.fsk-onb { min-height: 100%; background: var(--k-paper); padding: 28px 32px 40px; }
.fsk-onb-inner { max-width: 620px; margin: 0 auto; }
.fsk-onb-title { margin: 12px 0 6px; font-size: 1.6em; font-weight: 650; letter-spacing: -0.022em; line-height: 1.2; }
.fsk-onb-intro { margin: 0 0 22px; color: var(--k-text-2); max-width: 54ch; }
.fsk-progress { display: flex; gap: 4px; }
.fsk-seg { flex: 1; height: 6px; border-radius: 999px; background: var(--k-hover); transition: background .4s var(--k-ease); }
.fsk-seg--done { background: var(--k-primary); }
.fsk-seg--current { background: color-mix(in srgb, var(--k-primary) 32%, var(--k-paper)); }
.fsk-progress-label { display: flex; justify-content: space-between; gap: 8px; margin: 8px 0 24px; font-size: .86em; color: var(--k-text-2); }
.fsk-progress-label strong { color: var(--k-text); font-weight: 600; }
.fsk-steps { list-style: none; margin: 0; padding: 0; }
.fsk-step { position: relative; display: grid; grid-template-columns: 28px 1fr; column-gap: 12px; padding-bottom: 6px; }
.fsk-step:not(:last-child)::before {
  content: ""; position: absolute; left: 13px; top: 40px; bottom: -6px; width: 2px; border-radius: 2px; background: var(--k-divider);
}
.fsk-step--done:not(:last-child)::before { background: color-mix(in srgb, var(--k-primary) 55%, var(--k-paper)); }
.fsk-dot {
  position: relative; z-index: 1; width: 28px; height: 28px; margin-top: 8px; border-radius: 50%;
  display: grid; place-items: center; font-size: .79em; font-weight: 600;
  background: var(--k-paper); border: 1.5px solid var(--k-border); color: var(--k-text-2);
  transition: background .25s, border-color .25s, color .25s;
}
.fsk-step--done .fsk-dot { background: var(--k-primary); border-color: var(--k-primary); color: var(--k-on-primary); }
.fsk-step--current .fsk-dot {
  border: 2px solid var(--k-primary); color: var(--k-primary); background: var(--k-tint);
  box-shadow: 0 0 0 4px color-mix(in srgb, var(--k-primary) 14%, transparent);
}
.fsk-step-main { min-width: 0; border-radius: var(--k-r-lg); border: 1px solid transparent; transition: background .2s, border-color .2s, box-shadow .2s; }
.fsk-step--open .fsk-step-main { background: var(--k-sunken); }
.fsk-step--current.fsk-step--open .fsk-step-main { background: var(--k-paper); border-color: var(--k-border); box-shadow: var(--k-shadow); }
.fsk-step-btn {
  appearance: none; display: flex; align-items: center; gap: 10px; width: 100%; padding: 12px 14px;
  background: none; border: 0; border-radius: var(--k-r-lg); font: inherit; color: inherit; text-align: left; cursor: pointer;
}
.fsk-step:not(.fsk-step--open) .fsk-step-btn:hover { background: var(--k-hover); }
.fsk-step-title { flex: 1; min-width: 0; font-weight: 600; }
.fsk-step--done .fsk-step-title { color: var(--k-text-2); font-weight: 500; }
.fsk-step-meta { display: inline-flex; align-items: center; gap: 4px; font-size: .79em; color: var(--k-text-2); white-space: nowrap; }
.fsk-step--done .fsk-step-meta { color: var(--k-success); }
.fsk-step-body { padding: 0 14px 14px; }
.fsk-step-detail { margin: 0; color: var(--k-text-2); max-width: 60ch; }
.fsk-step-actions { display: flex; flex-wrap: wrap; gap: 8px; margin-top: 14px; }
.fsk-step-auto { display: flex; align-items: center; gap: 6px; margin-top: 12px; font-size: .79em; color: var(--k-text-2); }
.fsk-step-auto::before { content: ""; width: 6px; height: 6px; border-radius: 50%; background: var(--k-primary); animation: fsk-pulse 1.6s ease-in-out infinite; }
.fsk-onb-done { text-align: center; padding: 40px 16px 8px; animation: fsk-in .35s var(--k-ease) both; }
.fsk-onb-done .fsk-big {
  display: inline-grid; place-items: center; width: 56px; height: 56px; border-radius: 50%; margin-bottom: 14px;
  background: color-mix(in srgb, var(--k-success) 14%, var(--k-paper)); color: var(--k-success);
}
.fsk-onb-done h2 { margin: 0 0 6px; font-size: 1.4em; letter-spacing: -0.02em; }
.fsk-onb-done p { margin: 0 auto 18px; color: var(--k-text-2); max-width: 40ch; }
@keyframes fsk-pulse { 50% { opacity: .35; transform: scale(.8); } }

/* Mobile checklist card */
.fsk-onbc .fsk-progress { margin: 0 0 12px; }
.fsk-onbc ul { list-style: none; margin: 0; padding: 0; display: flex; flex-direction: column; gap: 2px; }
.fsk-onbc li { display: flex; align-items: center; gap: 10px; padding: 9px 10px; border-radius: var(--k-r-md); min-height: 44px; }
.fsk-onbc .fsk-dot { width: 22px; height: 22px; margin: 0; font-size: .72em; flex: none; }
.fsk-onbc li.fsk-step--done span.fsk-onbc-title { color: var(--k-text-2); text-decoration: line-through; text-decoration-color: color-mix(in srgb, var(--k-text-2) 50%, transparent); }
.fsk-onbc li.fsk-step--current { background: var(--k-tint); }
.fsk-onbc li.fsk-step--current .fsk-dot { box-shadow: none; }
.fsk-onbc .fsk-onbc-title { flex: 1; min-width: 0; }
.fsk-onbc li.fsk-step--current .fsk-onbc-title { font-weight: 600; }
.fsk-onbc .fsk-onbc-foot { margin: 12px 0 0; font-size: .79em; color: var(--k-text-2); display: flex; align-items: center; gap: 6px; }

/* States (mountState): the guide's own page, with a skeleton in its shape or a centered block */
.fsk-onb-st { display: flex; flex-direction: column; }
.fsk-onb-st > .fsk-onb-inner { flex: 1; display: flex; flex-direction: column; width: 100%; }
.fsk-onb-st .fsk-badge { align-self: flex-start; }
.fsk-onb-st .fsk-onb-title .fsk-bone { margin: 4px 0; }
.fsk-onb-st .fsk-progress > .fsk-bone, .fsk-onbc .fsk-progress > .fsk-bone { flex: 1; border-radius: 999px; }
.fsk-onb-st-box { flex: 1; display: flex; align-items: center; justify-content: center; padding: 24px 0 12%; }
.fsk-onb-st-panel { width: 100%; max-width: 440px; animation: fsk-in .32s var(--k-ease) both; }
.fsk-onb-st-panel > .fsk-error { padding: 20px; border: 1px solid var(--k-border); border-radius: var(--k-r-lg); background: var(--k-paper); box-shadow: var(--k-shadow); }
.fsk-onb-st-panel > .fsk-empty { padding: 8px 0; }
.fsk-onb-st-panel .fsk-empty-art { width: 60px; height: 60px; }
.fsk-onb-st-panel .fsk-empty-title { font-size: 1.15em; }
.fsk-onb-sk-step { display: grid; grid-template-columns: 28px 1fr; column-gap: 12px; align-items: center; min-height: 52px; }
.fsk-onb-sk-step .fsk-sk-stack { padding: 0 14px; }
.fsk-onb-sk-open { align-items: start; margin: 2px 0 6px; }
.fsk-onb-sk-open > .fsk-sk-stack { gap: 10px; padding: 14px; border: 1px solid var(--k-border); border-radius: var(--k-r-lg); box-shadow: var(--k-shadow); }
.fsk-onb-sk-open > .fsk-bone { margin-top: 12px; }
.fsk-onb-sk-btns { display: flex; gap: 8px; margin-top: 6px; }
.fsk-onbc .fsk-onb-sk-row { display: flex; align-items: center; gap: 10px; min-height: 40px; padding: 0 10px; }
`;

const doneCount = (s: OnboardingState): number => s.steps.filter((x) => x.done).length;
const minutesLeft = (s: OnboardingState): number => s.steps.filter((x) => !x.done).reduce((t, x) => t + (x.minutes ?? 0), 0);
const currentIndex = (s: OnboardingState): number => {
  const i = s.steps.findIndex((x) => x.id === s.current);
  return i >= 0 ? i : s.steps.findIndex((x) => !x.done);
};

function progress(state: OnboardingState): HTMLElement {
  const cur = currentIndex(state);
  return h("div", { class: "fsk-progress", role: "progressbar", "aria-valuemin": 0, "aria-valuemax": state.steps.length, "aria-valuenow": doneCount(state), "aria-label": "Setup progress" },
    state.steps.map((x, i) => h("span", { class: `fsk-seg${x.done ? " fsk-seg--done" : i === cur ? " fsk-seg--current" : ""}` })));
}

export interface OnboardingCanvasProps {
  root: HTMLElement;
  head: StyleHost;
  /** The canvas: the in-page renderCanvas ctx or a hosted ctx.canvas. Its state wins over data. */
  canvas?: CanvasApi;
  data?: OnboardingState;
  actions?: OnboardingActions;
}

export interface OnboardingCardProps {
  root: HTMLElement;
  head: StyleHost;
  data: OnboardingState;
  api?: CardApi;
  actions?: OnboardingActions;
}

/**
 * Desktop canvas. The state comes from canvas.state (else data). The agent never marks a step done:
 * your app calls canvas.setState when the user really did it.
 */
export function mount({ root, head, data, canvas, actions }: OnboardingCanvasProps): Cleanup {
  ensureStyles(head, "fsk-base", BASE_CSS);
  ensureStyles(head, "fsk-onb", CSS);
  let state = structuredClone(canvas?.state ?? data) as OnboardingState;
  let open: string | null = null; // a step the user expanded to read; null = the current step
  canvas?.setTitle?.(state.canvasTitle ?? "Getting started");
  const off = canvas?.onStateChange?.((next) => { state = structuredClone(next) as OnboardingState; open = null; render(); });
  const offApp = actions?.subscribe?.((patch) => commit({ ...state, ...patch }));

  function commit(next: OnboardingState, focusId?: string): void {
    state = next;
    canvas?.setState?.(structuredClone(state));
    open = null;
    render(focusId);
  }

  function markDone(step: OnboardingStep): void {
    const steps = state.steps.map((x) => (x.id === step.id ? { ...x, done: true } : x));
    const after = steps.findIndex((x, i) => i > steps.findIndex((y) => y.id === step.id) && !x.done);
    const next = after >= 0 ? steps[after] : steps.find((x) => !x.done);
    commit({ ...state, steps, current: next?.id ?? null }, next?.id);
  }

  function go(step: OnboardingStep): void {
    if (actions?.go) actions.go(step);
    else if (step.cta?.href && safeUrl(step.cta.href) != null) root.ownerDocument.defaultView?.open(step.cta.href, "_top");
  }

  function stepItem(step: OnboardingStep, i: number, cur: number): HTMLElement {
    const isCurrent = i === cur;
    const isOpen = open ? open === step.id : isCurrent;
    const bodyId = `fsk-onb-${step.id}`;
    const cls = ["fsk-step", step.done && "fsk-step--done", isCurrent && "fsk-step--current", isOpen && "fsk-step--open"].filter(Boolean).join(" ");
    return h("li", { class: cls, "data-step": step.id },
      h("span", { class: "fsk-dot", "aria-hidden": "true" }, step.done ? icon("check", 14) : String(i + 1)),
      h("div", { class: "fsk-step-main" },
        h("button", {
          class: "fsk-step-btn", type: "button", "aria-expanded": String(isOpen), "aria-controls": bodyId,
          onclick: () => { open = isOpen && !isCurrent ? null : step.id; render(); },
        },
          h("span", { class: "fsk-step-title" }, step.title),
          h("span", { class: "fsk-step-meta" },
            step.done ? [icon("check", 12), "Done"] : isCurrent ? "Up next" : step.minutes ? [icon("clock", 12), `${step.minutes} min`] : "")),
        isOpen && h("div", { class: "fsk-step-body", id: bodyId },
          h("p", { class: "fsk-step-detail" }, step.detail),
          !step.done && isCurrent && h("div", { class: "fsk-step-actions" },
            (step.cta || actions?.go) && h("button", { class: "fsk-btn fsk-btn--primary", type: "button", "data-primary": "", onclick: () => go(step) },
              step.cta?.label ?? "Show me where", icon("chevron", 14)),
            !step.auto && h("button", { class: "fsk-btn fsk-btn--ghost", type: "button", onclick: () => markDone(step) }, icon("check", 14), "I've done this")),
          !step.done && isCurrent && step.auto && h("p", { class: "fsk-step-auto" }, "Checks off by itself when you finish it"))));
  }

  function render(focusId?: string): void {
    const total = state.steps.length;
    const done = doneCount(state);
    const cur = currentIndex(state);
    const left = minutesLeft(state);
    const inner = done === total
      ? h("div", { class: "fsk-onb-done" },
          h("span", { class: "fsk-big" }, icon("check", 28)),
          h("h2", {}, state.doneTitle ?? "You're all set"),
          h("p", {}, state.doneText ?? "Every step is done. Ask me anything about what to do next."),
          h("button", { class: "fsk-btn fsk-btn--primary", type: "button", onclick: () => (actions?.finish ? actions.finish() : canvas?.close?.()) }, "Close guide"))
      : [
          h("span", { class: "fsk-badge fsk-badge--agent" }, icon("flag", 12), state.eyebrow ?? "Setup guide"),
          h("h2", { class: "fsk-onb-title" }, state.title),
          state.intro && h("p", { class: "fsk-onb-intro" }, state.intro),
          progress(state),
          h("div", { class: "fsk-progress-label" },
            h("span", {}, h("strong", {}, `${done} of ${total}`), " done"),
            left > 0 && h("span", {}, `About ${left} min left`)),
          h("ol", { class: "fsk-steps" }, state.steps.map((s, i) => stepItem(s, i, cur))),
        ];
    root.replaceChildren(h("div", { class: "fsk fsk-onb" }, h("div", { class: "fsk-onb-inner" }, inner)));
    if (focusId) (root.querySelector<HTMLElement>(`[data-step="${focusId}"] [data-primary]`) ?? root.querySelector<HTMLElement>(`[data-step="${focusId}"] .fsk-step-btn`))?.focus();
  }

  render();
  return () => { off?.(); offApp?.(); root.replaceChildren(); };
}

/** Mobile has no canvas: a read-only checklist card (a chat card action) with the current step's button. */
export function mountMobile({ root, head, data, api, actions }: OnboardingCardProps): Cleanup {
  ensureStyles(head, "fsk-base", BASE_CSS);
  ensureStyles(head, "fsk-onb", CSS);
  const cur = currentIndex(data);
  const step = data.steps[cur];
  const done = doneCount(data);
  const left = minutesLeft(data);
  root.replaceChildren(h("section", { class: "fsk fsk-card fsk-onbc", "aria-label": data.title },
    h("header", { class: "fsk-head" },
      h("span", { class: "fsk-chip" }, icon("flag", 18)),
      h("div", {}, h("h3", { class: "fsk-title" }, data.title),
        h("p", { class: "fsk-sub" }, `${done} of ${data.steps.length} done${left ? ` · about ${left} min left` : ""}`))),
    progress(data),
    h("ul", {}, data.steps.map((s, i) => h("li", { class: `${s.done ? "fsk-step--done" : ""}${i === cur ? " fsk-step--current" : ""}` },
      h("span", { class: "fsk-dot", "aria-hidden": "true" }, s.done ? icon("check", 12) : String(i + 1)),
      h("span", { class: "fsk-onbc-title" }, s.title, s.done && h("span", { class: "fsk-sr" }, " (done)")),
      i === cur && (s.cta || actions?.go) && h("button", { class: "fsk-btn fsk-btn--link", type: "button", onclick: () => (actions?.go ? actions.go(s) : s.cta?.href && safeUrl(s.cta.href) != null && root.ownerDocument.defaultView?.open(s.cta.href, "_top")) }, "Show me", icon("chevron", 14))))),
    step && h("p", { class: "fsk-onbc-foot" }, icon("message", 12), "Ask me about any step.")));
  api?.displayed?.();
  return () => root.replaceChildren();
}

/** The states this guide draws besides its steps. */
export const states: StateKind[] = ["loading", "empty", "error"];

/** mountState's props: the kit's StateProps for the canvas. Mobile uses mountMobileState. */
export interface OnboardingCanvasStateProps extends Omit<StateProps, "surface"> {
  /** The canvas: mountState sets its title. */
  canvas?: CanvasApi;
  /** The canvas header (setTitle). Default: "Getting started". */
  canvasTitle?: string;
  /** The badge above the title. Default: "Setup guide". */
  eyebrow?: string;
}

const stateLabel = (s: CardState): string => (s.kind === "loading" ? s.label ?? "Loading your setup steps…" : "");

/** The steps as bones: a progress bar, then five steps with the current one open. */
function stepsSkeleton(): Child {
  return [
    h("div", { class: "fsk-progress" }, Array.from({ length: 5 }, (_, i) => bone("auto", "6px", { i }))),
    h("div", { class: "fsk-progress-label" }, bone("72px", "10px"), bone("96px", "10px")),
    h("div", {}, Array.from({ length: 5 }, (_, i) => i === 2
      ? h("div", { class: "fsk-onb-sk-step fsk-onb-sk-open" }, bone("28px", "28px", { cls: "fsk-bone--round", i }),
          h("div", { class: "fsk-sk-stack" }, bone("44%", "12px", { i }), bone("92%", "9px", { i }), bone("70%", "9px", { i }),
            h("div", { class: "fsk-onb-sk-btns" }, bone("132px", "36px", { cls: "fsk-bone--box", i }), bone("118px", "36px", { cls: "fsk-bone--box", i }))))
      : h("div", { class: "fsk-onb-sk-step" }, bone("28px", "28px", { cls: "fsk-bone--round", i }),
          h("div", { class: "fsk-sk-stack" }, bone(`${[46, 38, 0, 52, 42][i]}%`, "12px", { i }))))),
  ];
}

/**
 * Desktop canvas in a state: the guide's header (badge, title) above the steps as bones while they load,
 * or a centered empty or error block. Returns the cleanup; mount the steps once they're in.
 */
export function mountState({ root, head, state, title, subtitle, canvas, canvasTitle, eyebrow }: OnboardingCanvasStateProps): Cleanup {
  ensureStyles(head, "fsk-base", BASE_CSS);
  ensureStyles(head, "fsk-onb", CSS);
  canvas?.setTitle?.(canvasTitle ?? "Getting started");
  const loading = state.kind === "loading";
  let dispose = (): void => {};
  let body: Child;
  if (state.kind === "loading") body = stepsSkeleton();
  else if (state.kind === "empty") body = h("div", { class: "fsk-onb-st-box" }, h("div", { class: "fsk-onb-st-panel" }, emptyBlock(state)));
  else {
    const e = errorBlock(state);
    dispose = e.dispose;
    body = h("div", { class: "fsk-onb-st-box" }, h("div", { class: "fsk-onb-st-panel" }, e.el));
  }
  root.replaceChildren(h("div", {
    class: "fsk fsk-onb fsk-onb-st", "data-state": state.kind,
    role: loading ? "status" : null, "aria-busy": loading ? "true" : null, "aria-live": loading ? "polite" : null,
  },
    loading && h("span", { class: "fsk-sr" }, stateLabel(state)),
    h("div", { class: "fsk-onb-inner" },
      h("span", { class: "fsk-badge fsk-badge--agent" }, icon("flag", 12), eyebrow ?? "Setup guide"),
      h("h2", { class: "fsk-onb-title" }, title ?? (loading ? bone("58%", "24px") : "Setup guide")),
      subtitle ? h("p", { class: "fsk-onb-intro" }, subtitle) : loading && h("div", { class: "fsk-onb-intro fsk-sk-lines", "aria-hidden": "true" }, bone("88%", "10px"), bone("62%", "10px")),
      loading ? h("div", { "aria-hidden": "true" }, body) : body)));
  return () => { dispose(); root.replaceChildren(); };
}

/** Mobile checklist card in a state: its header above checklist bones, or an empty or error block. Read-only: report displayed() yourself. */
export function mountMobileState({ root, head, state, title, subtitle, surface = "mobile" }: StateProps): Cleanup {
  ensureStyles(head, "fsk-base", BASE_CSS);
  ensureStyles(head, "fsk-onb", CSS);
  const card = stateCard({
    state: state.kind === "loading" ? { ...state, label: stateLabel(state) } : state, cls: "fsk-onbc", icon: "flag", title, subtitle, surface,
    skeleton: () => [
      h("div", { class: "fsk-progress" }, Array.from({ length: 5 }, (_, i) => bone("auto", "6px", { i }))),
      Array.from({ length: 5 }, (_, i) => h("div", { class: "fsk-onb-sk-row" }, bone("22px", "22px", { cls: "fsk-bone--round", i }), bone(`${[52, 44, 60, 48, 40][i]}%`, "10px", { i }))),
    ],
  });
  root.replaceChildren(card.el);
  return () => { card.dispose(); root.replaceChildren(); };
}

export const fixtures: Record<string, OnboardingState> = {
  default: {
    canvasTitle: "Getting started",
    title: "Set up your workspace",
    intro: "Five short steps to get your team's first project live. I'll explain each one as you go, and you can stop any time.",
    steps: [
      { id: "profile", title: "Complete your profile", detail: "Add your name, role and photo so teammates know who's who.", minutes: 1, done: true },
      { id: "invite", title: "Invite your team", detail: "Add teammates by email or share an invite link. They get access to every project you create.", minutes: 2, done: true },
      { id: "calendar", title: "Connect your calendar", detail: "Sync Google or Outlook so deadlines and meetings show up next to your tasks. You'll need to approve access once.", minutes: 2, done: false, auto: true, cta: { label: "Open integrations", href: "#integrations" } },
      { id: "project", title: "Create your first project", detail: "Pick a template or start blank. Add at least three tasks so your team has something to start on.", minutes: 3, done: false, cta: { label: "New project", href: "#new" } },
      { id: "notify", title: "Choose your notifications", detail: "Decide what you hear about and where: email, Slack or in the app.", minutes: 1, done: false, cta: { label: "Notification settings", href: "#notifications" } },
    ],
    current: "calendar",
  },
};
```

```js title="agent/ui/onboarding-canvas.js"
// Template: onboarding-canvas. An ONBOARDING canvas guide next to the chat (desktop), plus its mobile checklist card.

export const meta = {
  id: "onboarding-canvas",
  title: "Onboarding guide",
  surface: "canvas",
  canvasType: "ONBOARDING",
  kind: "interactive",
  mobile: "fallback-card",
  whenToUse: "A new user needs to finish 3–7 setup steps in your app, in order. The guide stays open beside the chat while the user works and the agent explains the current step.",
  whenNotToUse: "One step (tell the user in chat, or spotlight it). Steps the agent does itself (that's an action, not a guide). Mobile: the canvas isn't available; use the checklist card (mountMobile).",
};

const CSS = `
.fsk-onb { min-height: 100%; background: var(--k-paper); padding: 28px 32px 40px; }
.fsk-onb-inner { max-width: 620px; margin: 0 auto; }
.fsk-onb-title { margin: 12px 0 6px; font-size: 1.6em; font-weight: 650; letter-spacing: -0.022em; line-height: 1.2; }
.fsk-onb-intro { margin: 0 0 22px; color: var(--k-text-2); max-width: 54ch; }
.fsk-progress { display: flex; gap: 4px; }
.fsk-seg { flex: 1; height: 6px; border-radius: 999px; background: var(--k-hover); transition: background .4s var(--k-ease); }
.fsk-seg--done { background: var(--k-primary); }
.fsk-seg--current { background: color-mix(in srgb, var(--k-primary) 32%, var(--k-paper)); }
.fsk-progress-label { display: flex; justify-content: space-between; gap: 8px; margin: 8px 0 24px; font-size: .86em; color: var(--k-text-2); }
.fsk-progress-label strong { color: var(--k-text); font-weight: 600; }
.fsk-steps { list-style: none; margin: 0; padding: 0; }
.fsk-step { position: relative; display: grid; grid-template-columns: 28px 1fr; column-gap: 12px; padding-bottom: 6px; }
.fsk-step:not(:last-child)::before {
  content: ""; position: absolute; left: 13px; top: 40px; bottom: -6px; width: 2px; border-radius: 2px; background: var(--k-divider);
}
.fsk-step--done:not(:last-child)::before { background: color-mix(in srgb, var(--k-primary) 55%, var(--k-paper)); }
.fsk-dot {
  position: relative; z-index: 1; width: 28px; height: 28px; margin-top: 8px; border-radius: 50%;
  display: grid; place-items: center; font-size: .79em; font-weight: 600;
  background: var(--k-paper); border: 1.5px solid var(--k-border); color: var(--k-text-2);
  transition: background .25s, border-color .25s, color .25s;
}
.fsk-step--done .fsk-dot { background: var(--k-primary); border-color: var(--k-primary); color: var(--k-on-primary); }
.fsk-step--current .fsk-dot {
  border: 2px solid var(--k-primary); color: var(--k-primary); background: var(--k-tint);
  box-shadow: 0 0 0 4px color-mix(in srgb, var(--k-primary) 14%, transparent);
}
.fsk-step-main { min-width: 0; border-radius: var(--k-r-lg); border: 1px solid transparent; transition: background .2s, border-color .2s, box-shadow .2s; }
.fsk-step--open .fsk-step-main { background: var(--k-sunken); }
.fsk-step--current.fsk-step--open .fsk-step-main { background: var(--k-paper); border-color: var(--k-border); box-shadow: var(--k-shadow); }
.fsk-step-btn {
  appearance: none; display: flex; align-items: center; gap: 10px; width: 100%; padding: 12px 14px;
  background: none; border: 0; border-radius: var(--k-r-lg); font: inherit; color: inherit; text-align: left; cursor: pointer;
}
.fsk-step:not(.fsk-step--open) .fsk-step-btn:hover { background: var(--k-hover); }
.fsk-step-title { flex: 1; min-width: 0; font-weight: 600; }
.fsk-step--done .fsk-step-title { color: var(--k-text-2); font-weight: 500; }
.fsk-step-meta { display: inline-flex; align-items: center; gap: 4px; font-size: .79em; color: var(--k-text-2); white-space: nowrap; }
.fsk-step--done .fsk-step-meta { color: var(--k-success); }
.fsk-step-body { padding: 0 14px 14px; }
.fsk-step-detail { margin: 0; color: var(--k-text-2); max-width: 60ch; }
.fsk-step-actions { display: flex; flex-wrap: wrap; gap: 8px; margin-top: 14px; }
.fsk-step-auto { display: flex; align-items: center; gap: 6px; margin-top: 12px; font-size: .79em; color: var(--k-text-2); }
.fsk-step-auto::before { content: ""; width: 6px; height: 6px; border-radius: 50%; background: var(--k-primary); animation: fsk-pulse 1.6s ease-in-out infinite; }
.fsk-onb-done { text-align: center; padding: 40px 16px 8px; animation: fsk-in .35s var(--k-ease) both; }
.fsk-onb-done .fsk-big {
  display: inline-grid; place-items: center; width: 56px; height: 56px; border-radius: 50%; margin-bottom: 14px;
  background: color-mix(in srgb, var(--k-success) 14%, var(--k-paper)); color: var(--k-success);
}
.fsk-onb-done h2 { margin: 0 0 6px; font-size: 1.4em; letter-spacing: -0.02em; }
.fsk-onb-done p { margin: 0 auto 18px; color: var(--k-text-2); max-width: 40ch; }
@keyframes fsk-pulse { 50% { opacity: .35; transform: scale(.8); } }

/* Mobile checklist card */
.fsk-onbc .fsk-progress { margin: 0 0 12px; }
.fsk-onbc ul { list-style: none; margin: 0; padding: 0; display: flex; flex-direction: column; gap: 2px; }
.fsk-onbc li { display: flex; align-items: center; gap: 10px; padding: 9px 10px; border-radius: var(--k-r-md); min-height: 44px; }
.fsk-onbc .fsk-dot { width: 22px; height: 22px; margin: 0; font-size: .72em; flex: none; }
.fsk-onbc li.fsk-step--done span.fsk-onbc-title { color: var(--k-text-2); text-decoration: line-through; text-decoration-color: color-mix(in srgb, var(--k-text-2) 50%, transparent); }
.fsk-onbc li.fsk-step--current { background: var(--k-tint); }
.fsk-onbc li.fsk-step--current .fsk-dot { box-shadow: none; }
.fsk-onbc .fsk-onbc-title { flex: 1; min-width: 0; }
.fsk-onbc li.fsk-step--current .fsk-onbc-title { font-weight: 600; }
.fsk-onbc .fsk-onbc-foot { margin: 12px 0 0; font-size: .79em; color: var(--k-text-2); display: flex; align-items: center; gap: 6px; }

/* States (mountState): the guide's own page, with a skeleton in its shape or a centered block */
.fsk-onb-st { display: flex; flex-direction: column; }
.fsk-onb-st > .fsk-onb-inner { flex: 1; display: flex; flex-direction: column; width: 100%; }
.fsk-onb-st .fsk-badge { align-self: flex-start; }
.fsk-onb-st .fsk-onb-title .fsk-bone { margin: 4px 0; }
.fsk-onb-st .fsk-progress > .fsk-bone, .fsk-onbc .fsk-progress > .fsk-bone { flex: 1; border-radius: 999px; }
.fsk-onb-st-box { flex: 1; display: flex; align-items: center; justify-content: center; padding: 24px 0 12%; }
.fsk-onb-st-panel { width: 100%; max-width: 440px; animation: fsk-in .32s var(--k-ease) both; }
.fsk-onb-st-panel > .fsk-error { padding: 20px; border: 1px solid var(--k-border); border-radius: var(--k-r-lg); background: var(--k-paper); box-shadow: var(--k-shadow); }
.fsk-onb-st-panel > .fsk-empty { padding: 8px 0; }
.fsk-onb-st-panel .fsk-empty-art { width: 60px; height: 60px; }
.fsk-onb-st-panel .fsk-empty-title { font-size: 1.15em; }
.fsk-onb-sk-step { display: grid; grid-template-columns: 28px 1fr; column-gap: 12px; align-items: center; min-height: 52px; }
.fsk-onb-sk-step .fsk-sk-stack { padding: 0 14px; }
.fsk-onb-sk-open { align-items: start; margin: 2px 0 6px; }
.fsk-onb-sk-open > .fsk-sk-stack { gap: 10px; padding: 14px; border: 1px solid var(--k-border); border-radius: var(--k-r-lg); box-shadow: var(--k-shadow); }
.fsk-onb-sk-open > .fsk-bone { margin-top: 12px; }
.fsk-onb-sk-btns { display: flex; gap: 8px; margin-top: 6px; }
.fsk-onbc .fsk-onb-sk-row { display: flex; align-items: center; gap: 10px; min-height: 40px; padding: 0 10px; }
`;

const doneCount = (s) => s.steps.filter((x) => x.done).length;
const minutesLeft = (s) => s.steps.filter((x) => !x.done).reduce((t, x) => t + (x.minutes ?? 0), 0);
const currentIndex = (s) => {
  const i = s.steps.findIndex((x) => x.id === s.current);
  return i >= 0 ? i : s.steps.findIndex((x) => !x.done);
};

function progress(state) {
  const cur = currentIndex(state);
  return h("div", { class: "fsk-progress", role: "progressbar", "aria-valuemin": 0, "aria-valuemax": state.steps.length, "aria-valuenow": doneCount(state), "aria-label": "Setup progress" },
    state.steps.map((x, i) => h("span", { class: `fsk-seg${x.done ? " fsk-seg--done" : i === cur ? " fsk-seg--current" : ""}` })));
}

/**
 * Desktop canvas. The state comes from canvas.state (else data). The agent never marks a step done:
 * your app calls canvas.setState when the user really did it.
 */
export function mount({ root, head, data, canvas, actions }) {
  ensureStyles(head, "fsk-base", BASE_CSS);
  ensureStyles(head, "fsk-onb", CSS);
  let state = structuredClone(canvas?.state ?? data);
  let open = null; // a step the user expanded to read; null = the current step
  canvas?.setTitle?.(state.canvasTitle ?? "Getting started");
  const off = canvas?.onStateChange?.((next) => { state = structuredClone(next); open = null; render(); });
  const offApp = actions?.subscribe?.((patch) => commit({ ...state, ...patch }));

  function commit(next, focusId) {
    state = next;
    canvas?.setState?.(structuredClone(state));
    open = null;
    render(focusId);
  }

  function markDone(step) {
    const steps = state.steps.map((x) => (x.id === step.id ? { ...x, done: true } : x));
    const after = steps.findIndex((x, i) => i > steps.findIndex((y) => y.id === step.id) && !x.done);
    const next = after >= 0 ? steps[after] : steps.find((x) => !x.done);
    commit({ ...state, steps, current: next?.id ?? null }, next?.id);
  }

  function go(step) {
    if (actions?.go) actions.go(step);
    else if (step.cta?.href && safeUrl(step.cta.href) != null) root.ownerDocument.defaultView?.open(step.cta.href, "_top");
  }

  function stepItem(step, i, cur) {
    const isCurrent = i === cur;
    const isOpen = open ? open === step.id : isCurrent;
    const bodyId = `fsk-onb-${step.id}`;
    const cls = ["fsk-step", step.done && "fsk-step--done", isCurrent && "fsk-step--current", isOpen && "fsk-step--open"].filter(Boolean).join(" ");
    return h("li", { class: cls, "data-step": step.id },
      h("span", { class: "fsk-dot", "aria-hidden": "true" }, step.done ? icon("check", 14) : String(i + 1)),
      h("div", { class: "fsk-step-main" },
        h("button", {
          class: "fsk-step-btn", type: "button", "aria-expanded": String(isOpen), "aria-controls": bodyId,
          onclick: () => { open = isOpen && !isCurrent ? null : step.id; render(); },
        },
          h("span", { class: "fsk-step-title" }, step.title),
          h("span", { class: "fsk-step-meta" },
            step.done ? [icon("check", 12), "Done"] : isCurrent ? "Up next" : step.minutes ? [icon("clock", 12), `${step.minutes} min`] : "")),
        isOpen && h("div", { class: "fsk-step-body", id: bodyId },
          h("p", { class: "fsk-step-detail" }, step.detail),
          !step.done && isCurrent && h("div", { class: "fsk-step-actions" },
            (step.cta || actions?.go) && h("button", { class: "fsk-btn fsk-btn--primary", type: "button", "data-primary": "", onclick: () => go(step) },
              step.cta?.label ?? "Show me where", icon("chevron", 14)),
            !step.auto && h("button", { class: "fsk-btn fsk-btn--ghost", type: "button", onclick: () => markDone(step) }, icon("check", 14), "I've done this")),
          !step.done && isCurrent && step.auto && h("p", { class: "fsk-step-auto" }, "Checks off by itself when you finish it"))));
  }

  function render(focusId) {
    const total = state.steps.length;
    const done = doneCount(state);
    const cur = currentIndex(state);
    const left = minutesLeft(state);
    const inner = done === total
      ? h("div", { class: "fsk-onb-done" },
          h("span", { class: "fsk-big" }, icon("check", 28)),
          h("h2", {}, state.doneTitle ?? "You're all set"),
          h("p", {}, state.doneText ?? "Every step is done. Ask me anything about what to do next."),
          h("button", { class: "fsk-btn fsk-btn--primary", type: "button", onclick: () => (actions?.finish ? actions.finish() : canvas?.close?.()) }, "Close guide"))
      : [
          h("span", { class: "fsk-badge fsk-badge--agent" }, icon("flag", 12), state.eyebrow ?? "Setup guide"),
          h("h2", { class: "fsk-onb-title" }, state.title),
          state.intro && h("p", { class: "fsk-onb-intro" }, state.intro),
          progress(state),
          h("div", { class: "fsk-progress-label" },
            h("span", {}, h("strong", {}, `${done} of ${total}`), " done"),
            left > 0 && h("span", {}, `About ${left} min left`)),
          h("ol", { class: "fsk-steps" }, state.steps.map((s, i) => stepItem(s, i, cur))),
        ];
    root.replaceChildren(h("div", { class: "fsk fsk-onb" }, h("div", { class: "fsk-onb-inner" }, inner)));
    if (focusId) (root.querySelector(`[data-step="${focusId}"] [data-primary]`) ?? root.querySelector(`[data-step="${focusId}"] .fsk-step-btn`))?.focus();
  }

  render();
  return () => { off?.(); offApp?.(); root.replaceChildren(); };
}

/** Mobile has no canvas: a read-only checklist card (a chat card action) with the current step's button. */
export function mountMobile({ root, head, data, api, actions }) {
  ensureStyles(head, "fsk-base", BASE_CSS);
  ensureStyles(head, "fsk-onb", CSS);
  const cur = currentIndex(data);
  const step = data.steps[cur];
  const done = doneCount(data);
  const left = minutesLeft(data);
  root.replaceChildren(h("section", { class: "fsk fsk-card fsk-onbc", "aria-label": data.title },
    h("header", { class: "fsk-head" },
      h("span", { class: "fsk-chip" }, icon("flag", 18)),
      h("div", {}, h("h3", { class: "fsk-title" }, data.title),
        h("p", { class: "fsk-sub" }, `${done} of ${data.steps.length} done${left ? ` · about ${left} min left` : ""}`))),
    progress(data),
    h("ul", {}, data.steps.map((s, i) => h("li", { class: `${s.done ? "fsk-step--done" : ""}${i === cur ? " fsk-step--current" : ""}` },
      h("span", { class: "fsk-dot", "aria-hidden": "true" }, s.done ? icon("check", 12) : String(i + 1)),
      h("span", { class: "fsk-onbc-title" }, s.title, s.done && h("span", { class: "fsk-sr" }, " (done)")),
      i === cur && (s.cta || actions?.go) && h("button", { class: "fsk-btn fsk-btn--link", type: "button", onclick: () => (actions?.go ? actions.go(s) : s.cta?.href && safeUrl(s.cta.href) != null && root.ownerDocument.defaultView?.open(s.cta.href, "_top")) }, "Show me", icon("chevron", 14))))),
    step && h("p", { class: "fsk-onbc-foot" }, icon("message", 12), "Ask me about any step.")));
  api?.displayed?.();
  return () => root.replaceChildren();
}

/** The states this guide draws besides its steps. */
export const states = ["loading", "empty", "error"];

const stateLabel = (s) => (s.kind === "loading" ? s.label ?? "Loading your setup steps…" : "");

/** The steps as bones: a progress bar, then five steps with the current one open. */
function stepsSkeleton() {
  return [
    h("div", { class: "fsk-progress" }, Array.from({ length: 5 }, (_, i) => bone("auto", "6px", { i }))),
    h("div", { class: "fsk-progress-label" }, bone("72px", "10px"), bone("96px", "10px")),
    h("div", {}, Array.from({ length: 5 }, (_, i) => i === 2
      ? h("div", { class: "fsk-onb-sk-step fsk-onb-sk-open" }, bone("28px", "28px", { cls: "fsk-bone--round", i }),
          h("div", { class: "fsk-sk-stack" }, bone("44%", "12px", { i }), bone("92%", "9px", { i }), bone("70%", "9px", { i }),
            h("div", { class: "fsk-onb-sk-btns" }, bone("132px", "36px", { cls: "fsk-bone--box", i }), bone("118px", "36px", { cls: "fsk-bone--box", i }))))
      : h("div", { class: "fsk-onb-sk-step" }, bone("28px", "28px", { cls: "fsk-bone--round", i }),
          h("div", { class: "fsk-sk-stack" }, bone(`${[46, 38, 0, 52, 42][i]}%`, "12px", { i }))))),
  ];
}

/**
 * Desktop canvas in a state: the guide's header (badge, title) above the steps as bones while they load,
 * or a centered empty or error block. Returns the cleanup; mount the steps once they're in.
 */
export function mountState({ root, head, state, title, subtitle, canvas, canvasTitle, eyebrow }) {
  ensureStyles(head, "fsk-base", BASE_CSS);
  ensureStyles(head, "fsk-onb", CSS);
  canvas?.setTitle?.(canvasTitle ?? "Getting started");
  const loading = state.kind === "loading";
  let dispose = () => {};
  let body;
  if (state.kind === "loading") body = stepsSkeleton();
  else if (state.kind === "empty") body = h("div", { class: "fsk-onb-st-box" }, h("div", { class: "fsk-onb-st-panel" }, emptyBlock(state)));
  else {
    const e = errorBlock(state);
    dispose = e.dispose;
    body = h("div", { class: "fsk-onb-st-box" }, h("div", { class: "fsk-onb-st-panel" }, e.el));
  }
  root.replaceChildren(h("div", {
    class: "fsk fsk-onb fsk-onb-st", "data-state": state.kind,
    role: loading ? "status" : null, "aria-busy": loading ? "true" : null, "aria-live": loading ? "polite" : null,
  },
    loading && h("span", { class: "fsk-sr" }, stateLabel(state)),
    h("div", { class: "fsk-onb-inner" },
      h("span", { class: "fsk-badge fsk-badge--agent" }, icon("flag", 12), eyebrow ?? "Setup guide"),
      h("h2", { class: "fsk-onb-title" }, title ?? (loading ? bone("58%", "24px") : "Setup guide")),
      subtitle ? h("p", { class: "fsk-onb-intro" }, subtitle) : loading && h("div", { class: "fsk-onb-intro fsk-sk-lines", "aria-hidden": "true" }, bone("88%", "10px"), bone("62%", "10px")),
      loading ? h("div", { "aria-hidden": "true" }, body) : body)));
  return () => { dispose(); root.replaceChildren(); };
}

/** Mobile checklist card in a state: its header above checklist bones, or an empty or error block. Read-only: report displayed() yourself. */
export function mountMobileState({ root, head, state, title, subtitle, surface = "mobile" }) {
  ensureStyles(head, "fsk-base", BASE_CSS);
  ensureStyles(head, "fsk-onb", CSS);
  const card = stateCard({
    state: state.kind === "loading" ? { ...state, label: stateLabel(state) } : state, cls: "fsk-onbc", icon: "flag", title, subtitle, surface,
    skeleton: () => [
      h("div", { class: "fsk-progress" }, Array.from({ length: 5 }, (_, i) => bone("auto", "6px", { i }))),
      Array.from({ length: 5 }, (_, i) => h("div", { class: "fsk-onb-sk-row" }, bone("22px", "22px", { cls: "fsk-bone--round", i }), bone(`${[52, 44, 60, 48, 40][i]}%`, "10px", { i }))),
    ],
  });
  root.replaceChildren(card.el);
  return () => { card.dispose(); root.replaceChildren(); };
}

export const fixtures = {
  default: {
    canvasTitle: "Getting started",
    title: "Set up your workspace",
    intro: "Five short steps to get your team's first project live. I'll explain each one as you go, and you can stop any time.",
    steps: [
      { id: "profile", title: "Complete your profile", detail: "Add your name, role and photo so teammates know who's who.", minutes: 1, done: true },
      { id: "invite", title: "Invite your team", detail: "Add teammates by email or share an invite link. They get access to every project you create.", minutes: 2, done: true },
      { id: "calendar", title: "Connect your calendar", detail: "Sync Google or Outlook so deadlines and meetings show up next to your tasks. You'll need to approve access once.", minutes: 2, done: false, auto: true, cta: { label: "Open integrations", href: "#integrations" } },
      { id: "project", title: "Create your first project", detail: "Pick a template or start blank. Add at least three tasks so your team has something to start on.", minutes: 3, done: false, cta: { label: "New project", href: "#new" } },
      { id: "notify", title: "Choose your notifications", detail: "Decide what you hear about and where: email, Slack or in the app.", minutes: 1, done: false, cta: { label: "Notification settings", href: "#notifications" } },
    ],
    current: "calendar",
  },
};
```

### fs-kit (shared by every template)

TypeScript: /ui-catalog/files/ts/fs-kit.ts, JavaScript: /ui-catalog/files/js/fs-kit.js
