/*
  Assistant demo over two real BashoClip editor captures (1280 × 800 capture pixels).
  Markup: scripts/editor-demo.mjs. Behaviour: editor-demo.js. Homepage only.

  Three layers:
  1. Static (no JS, reduced motion, layout check, print): the final step. Step list, the editor
     after the edit, the result line, the Before/After toggle (once JS is ready), the honesty
     caption, notes. With scripting off, both pictures are shown side by side instead.
  2. Pinned (desktop, see PIN below): the same stage is sticky inside .demo-pin, which motion.js
     makes 240vh tall ([data-pinned]) and steps through with data-step. The stage layout comes
     from a media query that includes `scripting: enabled`, so it is in place on the first frame.
  3. States: [data-scene] (0–3), [data-phase] (focus | prompt | editing | edited | done) and
     [data-layout] on the figure, written by editor-demo.js. Only transform and opacity animate
     (plus label colour); the scroll-scrubbed part is the hairline of the active step.

  .demo-window is the size container. --u is one capture pixel (100cqi / 1280), so the chat is
  placed in capture pixels; overlays inside .demo-shot use percentages of the capture. Capture
  geometry: assistant button x 93, y 528, 197 × 32; 9:16 preview x 683, y 144, 246 × 439.
  Phones (<= 640px) crop the capture to the preview (x 526–1086, y 118–596) and show the two
  assistant buttons as a passive panel below it; while the assistant works, the chat folds down
  over that panel so the preview change stays visible.
  The scroll position picks the step everywhere (editor-demo.js); only transform, opacity and
  clip-path animate (the after capture is revealed by its clip, never crossfaded).
*/
.editor-demo { margin: 0; min-width: 0; }
.demo-pinstage { display: grid; grid-template-columns: minmax(0, 1fr); gap: var(--space-5); }
.demo-view { display: grid; grid-template-columns: minmax(0, 1fr); gap: var(--space-4); min-width: 0; }

/* Lines that change with the step share one grid cell (the tallest reserves the space). Without
   a scene the result (step 4) shows. The status line has a fifth line for the Before view. */
.demo-stack { display: grid; }
.demo-stack > span { grid-area: 1 / 1; opacity: 0; }
.editor-demo:not([data-scene]) .demo-stack > [data-s="3"],
[data-scene="0"] .demo-stack > [data-s="0"],
[data-scene="1"] .demo-stack > [data-s="1"],
[data-scene="2"] .demo-stack > [data-s="2"],
[data-scene="3"] .demo-stack > [data-s="3"] { opacity: 1; }
.editor-demo:is(:not([data-scene]), [data-scene="3"])[data-layout=original] .demo-status > [data-s="3"] { opacity: 0; }
.editor-demo:is(:not([data-scene]), [data-scene="3"])[data-layout=original] .demo-status > [data-s="b"] { opacity: 1; }

/* Four steps: a hairline and a label each. Upcoming steps are muted, the current one is ink. */
.demo-steps { display: grid; grid-template-columns: repeat(4, minmax(0, 1fr)); column-gap: var(--space-5); margin: 0; padding: 0; list-style: none; }
.demo-steps li { --fill: 1; display: grid; align-content: start; gap: var(--space-3); min-width: 0; }
.demo-steps li[data-upcoming] { --fill: 0; }
.demo-step-bar { display: block; height: 2px; overflow: hidden; border-radius: 1px; background: var(--line-strong); }
.demo-step-bar > span { display: block; height: 100%; background: var(--ink); transform: scaleX(var(--fill)); transform-origin: left; }
.demo-step-label { font-size: var(--fs-ui); line-height: var(--lh-ui); color: var(--ink-2); }
.demo-step-number { margin-right: 2px; font-variant-numeric: tabular-nums; }
.demo-steps [aria-current=step] .demo-step-label { color: var(--ink); }
.demo-steps [data-upcoming] .demo-step-label { color: var(--muted); }
.demo-sr { position: absolute; width: 1px; height: 1px; overflow: hidden; clip: rect(0 0 0 0); clip-path: inset(50%); white-space: nowrap; }
.demo-step-current { display: none; }

/* Window and capture */
.demo-window { --u: calc(100cqi / 1280); --crop-x: 0; --crop-y: 0; --crop-w: 1280; --crop-h: 800; position: relative; isolation: isolate; container-type: inline-size; overflow: hidden; border: 1px solid var(--line-strong); border-radius: var(--radius-md); background: var(--surface-2); box-shadow: 0 24px 70px -30px #000c; }
.demo-editor { position: relative; }
.demo-capture { position: relative; overflow: hidden; aspect-ratio: var(--crop-w) / var(--crop-h); }
/* Zoom origin for step 1 (scale 1.4): the zoomed frame runs from just under the app's title bar
   (capture y 98) to the timeline header (y 669), so no text line is cut at its edges. */
.demo-shot { position: relative; width: calc(100% * 1280 / var(--crop-w)); margin: calc(-100% * var(--crop-y) / var(--crop-w)) 0 0 calc(-100% * var(--crop-x) / var(--crop-w)); aspect-ratio: 1280 / 800; transform-origin: 14.9% 42.9%; }
.demo-pane { position: absolute; inset: 0; }
.demo-pane img { display: block; width: 100%; height: auto; aspect-ratio: 1280 / 800; }
.demo-pane-label { display: none; }
/* The after capture is revealed by its clip (never a crossfade: the two captures have different
   timelines, so a fade would show both at once). Hidden, it is a line on the preview's top edge;
   it grows from there over the preview and then the whole editor. */
.demo-pane[data-pane=after] { clip-path: inset(18% 27.42% 82% 53.36%); }
[data-layout=edited] .demo-pane[data-pane=after] { clip-path: inset(0); }
/* With JS, the visible capture is named on the preview in the review step and the static state. */
.editor-demo[data-ready] .demo-pane-label { position: absolute; left: calc(53.36% + 4px); top: calc(18% + 4px); display: block; padding: 3px 8px; border-radius: 4px; background: #000000cc; color: var(--ink); font-size: var(--fs-badge); line-height: 1.3; font-weight: 500; opacity: 0; pointer-events: none; }
.editor-demo[data-ready]:is(:not([data-scene]), [data-phase=done]) .demo-pane-label { opacity: 1; }

/* Highlights inside the capture (they zoom with it) */
.demo-ring { position: absolute; border-radius: 6px; box-shadow: 0 0 0 2px var(--ink), 0 0 0 7px #ffffff2e; opacity: 0; pointer-events: none; }
.demo-ring-button { left: 7.27%; top: 66%; width: 15.39%; height: 4%; }
.demo-ring-preview { left: 53.36%; top: 18%; width: 19.22%; height: 54.88%; border-radius: 2px; }
.demo-cursor { position: absolute; left: 19.6%; top: 68.6%; width: 18px; height: 18px; opacity: 0; transform: translate3d(70px, 56px, 0); pointer-events: none; }
.demo-cursor svg { display: block; width: 18px; height: 18px; fill: var(--ink); stroke: #111; stroke-width: 1.5; filter: drop-shadow(0 3px 2px #0005); }

/* Assistant buttons as a panel under the cropped capture (phones only) */
.demo-assistant { display: none; }

/* Chat, docked over the right inspector so the 9:16 preview stays visible. Hidden unless a
   scene shows it, so the static state never has it. */
.demo-chat { position: absolute; z-index: 2; top: calc(var(--u) * 140); left: calc(var(--u) * 946); right: calc(var(--u) * 16); background: var(--surface-raised); border: 1px solid var(--line-strong); border-radius: var(--radius-md); box-shadow: 0 24px 55px #0009; opacity: 0; visibility: hidden; transform: translate3d(var(--chat-from-x, 24px), var(--chat-from-y, 0), 0); }
/* Narrow windows (the pinned stage on short screens, tablets): the inspector is too narrow; the
   chat sits over the left panel instead, clear of the preview. */
@container (max-width: 999px) {
  .demo-chat { --chat-from-x: -24px; top: calc(var(--u) * 112); left: calc(var(--u) * 16); right: auto; width: calc(var(--u) * 640); }
}
.demo-chat-heading { display: flex; justify-content: space-between; align-items: center; gap: var(--space-3); padding: var(--space-3) var(--space-4); border-bottom: 1px solid var(--line); font-size: var(--fs-ui); line-height: var(--lh-ui); }
.demo-chat-heading strong { font-weight: 500; }
.demo-chat-heading > span { font-size: var(--fs-meta); color: var(--muted); }
.demo-chat-body { padding: var(--space-4); }
.demo-attachment { display: flex; align-items: center; gap: var(--space-3); font-size: var(--fs-meta); line-height: 1.4; }
.demo-attachment img { flex: none; width: 54px; height: auto; aspect-ratio: 27 / 20; object-fit: cover; border-radius: var(--radius-sm); }
.demo-attachment small { display: block; margin-top: 2px; font-size: var(--fs-meta); color: var(--muted); }
.demo-composer { position: relative; margin-top: var(--space-4); }
/* The full request reserves its final height; the typed copy is laid over it. */
.demo-prompt { display: grid; margin: 0; padding: var(--space-3) var(--space-3) 52px; background: var(--surface-input); border-radius: 10px; color: var(--ink); font-size: var(--fs-ui); line-height: var(--lh-ui); }
.demo-prompt > span { grid-area: 1 / 1; }
.demo-prompt-full { color: transparent; }
.demo-prompt i { display: inline-block; width: 1px; height: 1em; margin-left: 2px; vertical-align: -2px; background: var(--ink); }
:is([data-phase=editing], [data-phase=edited]) .demo-prompt i { opacity: 0; }
.demo-send { position: absolute; right: var(--space-2); bottom: var(--space-2); display: grid; place-items: center; width: 36px; height: 36px; border-radius: 50%; background: var(--fill); color: var(--on-fill); opacity: .35; }
.demo-send svg { width: 16px; height: 16px; }
[data-typed][data-phase=prompt] .demo-send { opacity: 1; }
.demo-agent-work { display: flex; align-items: flex-start; gap: var(--space-3); margin-top: var(--space-4); opacity: 0; transform: translate3d(0, 6px, 0); }
.demo-agent-text { display: grid; margin: 0; font-size: var(--fs-meta); line-height: 1.5; color: var(--ink-2); }
.demo-agent-text > span { grid-area: 1 / 1; opacity: 0; }
[data-phase=editing] .demo-agent-text > [data-s="0"],
[data-phase=edited] .demo-agent-text > [data-s="1"] { opacity: 1; }
.demo-working-dot { flex: none; width: 6px; height: 6px; margin-top: 7px; border-radius: 50%; background: var(--ink-2); }

/* Result line and Before/After */
.demo-bar { display: flex; flex-wrap: wrap; align-items: center; justify-content: space-between; gap: var(--space-3) var(--space-5); }
.demo-status { flex: 1 1 280px; min-width: 0; margin: 0; font-size: var(--fs-body); line-height: var(--lh-ui); color: var(--ink); }
.demo-comparison { flex: none; }
.demo-comparison button:focus-visible { outline: 2px solid var(--ink); outline-offset: 2px; }
/* Usable once editor-demo.js runs (without JS both pictures are shown instead). */
.editor-demo:not([data-ready]) .demo-comparison { visibility: hidden; }
.demo-caption { margin: calc(var(--space-2) - var(--space-4)) 0 0; font-size: var(--fs-meta); line-height: 1.5; color: var(--muted); }

/* Notes share the four step columns. Baseline alignment keeps the first text line of every note
   (and the 44px link) on one line when one item wraps. */
.demo-notes { display: grid; grid-template-columns: repeat(4, minmax(0, 1fr)); align-items: baseline; column-gap: var(--space-5); row-gap: var(--space-1); margin: var(--space-6) 0 0; padding: var(--space-4) 0 0; border-top: 1px solid var(--line); list-style: none; font-size: var(--fs-ui); line-height: var(--lh-ui); color: var(--ink-2); }
.demo-notes li { display: flex; align-items: baseline; gap: var(--space-2); min-width: 0; }
.demo-notes li:not(.demo-notes-link)::before { content: '✓'; flex: none; color: var(--ink); }
.demo-notes-link .text-link { color: var(--ink); }

@media (max-width: 960px) {
  .demo-notes { grid-template-columns: repeat(2, minmax(0, 1fr)); }
}

@media (max-width: 640px) {
  .demo-pinstage { gap: var(--space-4); }
  .demo-window { --crop-x: 526; --crop-y: 118; --crop-w: 560; --crop-h: 478; }
  .demo-ring-button, .demo-cursor { display: none; }
  /* The app's two assistant buttons, redrawn as a passive part of the picture (no fill, no link
     icon, not tappable): the Before/After toggle stays the only filled control. */
  .demo-assistant { display: grid; gap: var(--space-2); padding: var(--space-3); border-top: 1px solid var(--line); background: var(--surface); pointer-events: none; }
  .demo-pill { display: flex; align-items: center; min-height: var(--touch); padding: 0 var(--space-3); border: 1px solid var(--line); border-radius: var(--radius-sm); font-size: var(--fs-ui); line-height: 1.3; color: var(--ink-2); opacity: .7; }
  .demo-chat { --chat-from-x: 0; --chat-from-y: 16px; top: auto; right: var(--space-3); bottom: var(--space-3); left: var(--space-3); width: auto; }
  /* While the assistant works, the chat folds down to its heading and the work line, over the
     assistant panel, so the whole preview stays visible while it changes. */
  .demo-chat-body { position: relative; }
  .demo-agent-work { position: absolute; top: var(--space-3); inset-inline: var(--space-4); margin-top: 0; }
  /* Bars only; the current step is named in one line below them. */
  .demo-steps { column-gap: var(--space-2); }
  .demo-steps li { gap: 0; }
  .demo-step-label { position: absolute; width: 1px; height: 1px; overflow: hidden; clip: rect(0 0 0 0); clip-path: inset(50%); white-space: nowrap; }
  .demo-step-current { display: grid; margin: calc(var(--space-1) - var(--space-4)) 0 0; font-size: var(--fs-ui); line-height: var(--lh-ui); color: var(--ink); }
  .demo-status { flex-basis: 100%; }
  .demo-notes { grid-template-columns: minmax(0, 1fr); margin-top: var(--space-5); }
}

/* No JS: no toggle, so the two pictures sit side by side, cropped to the preview and labelled. */
@media (scripting: none) {
  .demo-capture { aspect-ratio: auto; }
  .demo-shot { display: grid; grid-template-columns: repeat(2, minmax(0, 1fr)); gap: 1px; width: 100%; margin: 0; aspect-ratio: auto; background: var(--line-strong); }
  .demo-pane { position: relative; inset: auto; overflow: hidden; aspect-ratio: 560 / 478; background: var(--surface-2); }
  .demo-pane[data-pane=after] { clip-path: none; }
  .demo-pane img { width: calc(100% * 1280 / 560); max-width: none; margin: calc(-100% * 118 / 560) 0 0 calc(-100% * 526 / 560); }
  .demo-pane-label { position: absolute; left: var(--space-3); top: var(--space-3); display: block; padding: 3px 8px; border-radius: 4px; background: #000000b3; color: var(--ink); font-size: var(--fs-badge); line-height: 1.3; font-weight: 500; }
  .demo-ring, .demo-cursor, .demo-assistant, .demo-comparison { display: none; }
}

/* PIN. Keep this query identical to motion.js PIN_MEDIA (the default data-pin-media).
   Steps at the top; the window with its result line and caption takes the height left, sized by
   height (or width, whichever is smaller) and centred. */
@media screen and (scripting: enabled) and (prefers-reduced-motion: no-preference) and (min-width: 961px) and (min-height: 680px) {
  .demo-pin[data-pinned] { height: 240vh; }
  .demo-pinstage { position: sticky; top: var(--header-h); height: calc(100vh - var(--header-h)); grid-template-rows: auto minmax(0, 1fr); gap: clamp(12px, 2.6vh, 24px); padding-block: clamp(16px, 3.6vh, 36px) var(--space-4); }
  /* The window, the result line and the caption form one centred column as wide as the window
     can be: --chrome is the height of everything in it but the capture (bar, caption, gaps, border). */
  .demo-view { --chrome: 100px; align-content: center; min-height: 0; container-type: size; }
  /* Resolved on the children, so cqw/cqh refer to .demo-view. */
  .demo-view > * { --w: min(100cqw, calc((100cqh - var(--chrome)) * 1.6)); justify-self: center; width: var(--w); }
  /* The hairline of the active step follows the scroll; earlier ones are full, later ones empty. */
  .demo-pin[data-pinned] .demo-steps li { --fill: clamp(0, calc(var(--progress, 0) * 4 - var(--n)), 1); }
}

/* ---- States (JS only) ------------------------------------------------------------------------ */

@media screen {
  /* 0 Open the clip: the camera moves in on the assistant button; once the window is in view
     the cursor arrives and the button lights up. */
  [data-phase=focus] .demo-shot { transform: scale(1.4); }
  [data-phase=focus][data-cue] .demo-ring-button { opacity: 1; }
  [data-phase=focus][data-cue] .demo-cursor { opacity: 1; transform: none; }
  [data-phase=focus][data-cue] .demo-pill[data-client=ChatGPT] { border-color: var(--ink); color: var(--ink); opacity: 1; }

  /* 1–2 The chat opens over the inspector; the request is typed; the assistant works on the
     preview (ring), then the preview changes. */
  :is([data-phase=prompt], [data-phase=editing], [data-phase=edited]) .demo-chat { opacity: 1; visibility: visible; transform: none; }
  :is([data-phase=editing], [data-phase=edited]) .demo-agent-work { opacity: 1; transform: none; }
  :is([data-phase=editing], [data-phase=edited]) .demo-ring-preview { opacity: 1; }

  /* 3 Review: the chat has closed; Before/After appears. Before that it is invisible and only
     reachable by keyboard, which shows this step at once (editor-demo.js). */
  [data-phase]:not([data-phase=done]) .demo-comparison { opacity: 0; pointer-events: none; }

  /* Transitions, only once the first state has been applied without them. */
  [data-animate] .demo-shot { transition: transform var(--dur-media) var(--ease-media); }
  [data-animate] .demo-pane[data-pane=after] { transition: clip-path var(--dur-reveal-fade) var(--ease-standard); }
  [data-animate] .demo-pane-label { transition: opacity var(--dur-fade) var(--ease-standard); }
  [data-animate] .demo-ring { transition: opacity var(--dur-fade) var(--ease-standard); }
  [data-animate][data-phase=focus] .demo-ring-button { transition-delay: calc(var(--dur-reveal) + 120ms); }
  [data-animate] .demo-cursor { transition: opacity var(--dur-ui) var(--ease-standard), transform var(--dur-reveal) var(--ease-standard); }
  [data-animate][data-phase=focus] .demo-cursor { transition-delay: 300ms; }
  [data-animate] .demo-pill { transition: border-color var(--dur-ui) var(--ease-standard), color var(--dur-ui) var(--ease-standard), opacity var(--dur-ui) var(--ease-standard); }
  /* The chat slides in after the camera has started to pull back, and out before the review. */
  [data-animate] .demo-chat { transition: opacity var(--dur-fade) var(--ease-standard), transform var(--dur-reveal) var(--ease-standard), visibility 0s linear var(--dur-fade); }
  [data-animate][data-phase=prompt] .demo-chat { transition: opacity var(--dur-fade) var(--ease-standard) 300ms, transform var(--dur-reveal) var(--ease-standard) 300ms, visibility 0s linear 300ms; }
  [data-animate]:is([data-phase=editing], [data-phase=edited]) .demo-chat { transition: opacity var(--dur-fade) var(--ease-standard), transform var(--dur-reveal) var(--ease-standard), visibility 0s; }
  [data-animate] .demo-agent-work { transition: opacity var(--dur-fade) var(--ease-out-soft), transform var(--dur-fade) var(--ease-out-soft); }
  /* Changing lines: the old one leaves before the new one arrives (never two lines on top of each other). */
  [data-animate] :is(.demo-agent-text, .demo-stack) > span { transition: opacity var(--dur-micro) var(--ease-standard); }
  [data-animate][data-scene="0"] .demo-stack > [data-s="0"],
  [data-animate][data-scene="1"] .demo-stack > [data-s="1"],
  [data-animate][data-scene="2"] .demo-stack > [data-s="2"],
  [data-animate][data-scene="3"] .demo-stack > [data-s="3"],
  [data-animate][data-scene="3"][data-layout=original] .demo-status > [data-s="b"],
  [data-animate][data-phase=editing] .demo-agent-text > [data-s="0"],
  [data-animate][data-phase=edited] .demo-agent-text > [data-s="1"] { transition: opacity var(--dur-fade) var(--ease-standard) var(--dur-micro); }
  [data-animate] .demo-send { transition: opacity var(--dur-ui) var(--ease-standard); }
  [data-animate] .demo-comparison { transition: opacity var(--dur-fade) var(--ease-standard) 200ms; }
  [data-animate] .demo-step-label { transition: color var(--dur-fade) var(--ease-standard); }
  [data-animate] .demo-pin:not([data-pinned]) .demo-step-bar > span { transition: transform var(--dur-reveal) var(--ease-standard); }
  /* While the assistant works the dot breathes a few times; it never loops on its own. */
  .motion [data-phase=editing] .demo-working-dot { animation: demo-working var(--dur-reveal-fade) var(--ease-standard) 4 alternate; }
}
@keyframes demo-working { to { opacity: .3; } }

@media (max-width: 640px) {
  /* The crop has no room for the chat and the preview at once: once the request is sent the chat
     folds down over the assistant panel (only its heading and the work line stay in the window),
     so the preview change is seen with the chat saying "adjusted"; it leaves for the review. */
  :is([data-phase=editing], [data-phase=edited]) .demo-chat { transform: translate3d(0, calc(100% - 100px), 0); }
  [data-phase=done] .demo-chat { --chat-from-y: calc(100% - 84px); }
  :is([data-phase=editing], [data-phase=edited], [data-phase=done]) .demo-attachment { opacity: 0; }
  [data-animate] .demo-attachment { transition: opacity var(--dur-micro) var(--ease-standard); }
  [data-phase=focus] .demo-shot { transform: none; }
  [data-animate] .demo-assistant { transition: opacity var(--dur-fade) var(--ease-standard); }
  [data-phase=prompt] .demo-assistant { opacity: .3; }
}

/* Print / PDF: the static layout, never the pin. */
@media print {
  .demo-pin { height: auto !important; }
  .demo-pinstage { position: static !important; height: auto !important; }
}

@media (prefers-reduced-motion: reduce) {
  .demo-chat, .demo-cursor, .demo-shot { transform: none !important; }
}
