/*
  Homepage data flow in #lokal / #local. Markup: scripts/privacy-flow.mjs. Motion: privacy-flow.js.
  Loaded on the homepage only, after product-demos.css (reuses .sample-portrait for the export).

  Picture: a line-drawn laptop (original video, export, timeline) on the left; four small chips on
  wires to the BashoClip service on the right; "Clip suggestions" come back. A dashed line means
  exactly one thing: conditional (the audio track). Everything else is solid.

  Geometry rules (checked frame by frame at 320–1440px in both locales):
  - The laptop is two grid items: .pf-screen shares a row with the lanes, .pf-base sits in the row
    below. If the lanes are taller than the screen, the screen stretches (its display centres the
    content), so no lane, wire or arrow can ever land on the base.
  - Chips are never clipped and never cross the laptop: they only fade in with a short 12px slide
    inside their own lane (the lane's 24px inset keeps a 12px clearance to the screen).

  The CSS default is the final state. JS sets data-flow-state on the figure:
    armed  start state (hidden/offset), only under html.motion and only below the fold at init
    play   transitions run once, back to the default values
    done   identical to no attribute
  Only transform and opacity are animated, and only on screen: print always shows the final state.
*/
.privacy-flow {
  --pf-draw: var(--ease-standard, cubic-bezier(.4, 0, .6, 1));
  --pf-travel-ease: var(--ease-media, cubic-bezier(.165, .84, .44, 1));
  --pf-fade: var(--ease-out-soft, cubic-bezier(.33, 1, .68, 1));
  --pf-edge: 1.25px solid var(--line-strong);
  --pf-chip-x: var(--space-5);
  display: grid;
  grid-template-columns: minmax(0, 30fr) minmax(0, 36fr);
  grid-template-areas: "stays goes" "screen link" "base ." "assistant assistant";
  margin: 0 0 var(--space-7);
}

/* Place titles: Bricolage lead in ink, detail in ink-2, read before the picture. */
.pf-caption { display: grid; align-content: end; gap: 2px; margin: 0 0 var(--space-5); }
.pf-caption strong { font-family: var(--heading); font-size: var(--fs-title); line-height: var(--lh-title); letter-spacing: var(--tracking-title); font-weight: 600; color: var(--ink); }
.pf-caption span { font-size: var(--fs-ui); line-height: var(--lh-ui); color: var(--ink-2); }
.pf-caption-stays { grid-area: stays; }
.pf-caption-goes { grid-area: goes; padding-left: var(--pf-chip-x); }

/* Laptop: the screen is inset 4.5% on the left and the base reaches 4.5% past the right edge (into
   the empty cell under the lanes), so the laptop is symmetric, never leaves the 1120px container,
   and the wires start at the screen. */
.pf-screen { grid-area: screen; position: relative; z-index: 1; display: flex; flex-direction: column; min-width: 0; margin-left: 4.5%; padding: 10px; border: var(--pf-edge); border-radius: 18px 18px 6px 6px; background: var(--surface); }
.pf-base { grid-area: base; position: relative; height: 14px; margin-right: -4.5%; border: var(--pf-edge); border-radius: 2px 2px 16px 16px; background: var(--surface); }
.pf-base::before { content: ''; position: absolute; top: -1px; left: 50%; width: 76px; height: 5px; margin-left: -38px; border: var(--pf-edge); border-top: 0; border-radius: 0 0 6px 6px; background: var(--bg); }

/* Screen content: 16:9 original and 9:16 export at equal height (.3164 = 81/256), timeline below. */
.pf-display { flex: 1 0 auto; display: grid; grid-template-columns: minmax(0, 1fr) minmax(0, .3164fr); align-content: center; gap: var(--space-3); padding: var(--space-4); border-radius: 9px; background: var(--bg); }
.pf-video, .pf-export { position: relative; }
.pf-video img { display: block; width: 100%; height: auto; aspect-ratio: var(--ar-video); object-fit: cover; border-radius: var(--radius-sm); }
.pf-export { border-radius: var(--radius-sm); outline: 1px solid var(--line); outline-offset: -1px; }
.pf-export .sample-portrait { width: 100%; }
.pf-tag { position: absolute; top: var(--space-2); left: var(--space-2); z-index: 2; padding: 2px 6px; border-radius: 4px; background: var(--scrim); color: var(--ink); font-size: var(--fs-badge); line-height: 1.4; font-weight: 500; white-space: nowrap; }
.pf-timeline { grid-column: 1 / -1; display: grid; grid-template-columns: auto minmax(0, 1fr); align-items: center; gap: var(--space-3); }
.pf-timeline-label { font-size: var(--fs-badge); line-height: 1.4; color: var(--muted); }
.pf-track { position: relative; height: 26px; border: 1px solid var(--line); border-radius: 5px; background: repeating-linear-gradient(90deg, #ffffff12 0 1px, transparent 1px 7px); }
.pf-track i { position: absolute; top: 3px; bottom: 3px; left: var(--x); width: var(--w); border-radius: 3px; background: var(--fill); transform-origin: 50% 100%; }

/* Link = lanes + service. The service is exactly as tall as the lanes, so every arrow points into it.
   Lanes are kept compact (34px rows) so the screen rarely has to stretch to match them. */
.pf-link { grid-area: link; align-self: center; display: grid; grid-template-columns: minmax(0, 1fr) 176px; min-width: 0; padding-block: var(--space-1); }
.pf-out, .pf-back { grid-column: 1; min-width: 0; container-type: inline-size; }
.pf-back { margin-top: var(--space-4); }
.pf-back-label { display: flex; align-items: center; gap: var(--space-1); margin: 0 0 var(--space-2); padding-left: var(--pf-chip-x); font-size: var(--fs-meta); line-height: 1.4; color: var(--muted); }
.pf-arrow { flex: none; width: 14px; height: 14px; fill: none; stroke: currentColor; stroke-width: 1.3; stroke-linecap: round; stroke-linejoin: round; }
.pf-link ul { display: grid; gap: var(--space-2); margin: 0; padding: 0; list-style: none; }
.pf-row { position: relative; display: flex; align-items: center; min-height: 34px; padding-left: var(--pf-chip-x); }
.pf-wire { position: absolute; inset-inline: 0; top: 50%; height: 1px; background: var(--line-strong); transform-origin: 0 50%; }
.pf-row::after { content: ''; position: absolute; top: calc(50% - 3px); right: 1px; width: 7px; height: 7px; border: solid var(--ink-2); border-width: 1px 1px 0 0; transform: rotate(45deg); }
/* Packet: a 5px dot that runs along a wire once while the figure plays (see Motion). Invisible at rest. */
.pf-row::before { content: ''; position: absolute; top: calc(50% - 2.5px); left: 0; width: 5px; height: 5px; border-radius: 50%; background: var(--ink); opacity: 0; pointer-events: none; }
.pf-back .pf-wire { transform-origin: 100% 50%; }
.pf-back .pf-row::after { right: auto; left: 1px; border-width: 0 0 1px 1px; }
.pf-row.is-conditional .pf-wire { background: repeating-linear-gradient(90deg, var(--ink-2) 0 3px, transparent 3px 7px); opacity: .6; }

.pf-chip { position: relative; display: inline-flex; align-items: center; gap: var(--space-2); padding: 6px 14px 6px 10px; border: 1px solid var(--line-strong); border-radius: var(--radius-pill); background: var(--bg); font-size: var(--fs-ui); line-height: 1.4; color: var(--ink); white-space: nowrap; }
.pf-chip svg { flex: none; width: 16px; height: 16px; fill: none; stroke: var(--ink-2); stroke-width: 1.3; stroke-linecap: round; stroke-linejoin: round; }
.pf-chip small { margin-left: 4px; font-size: var(--fs-meta); color: var(--muted); }
.is-conditional .pf-chip { border-style: dashed; }
/* Narrow desktop lanes: the condition drops under "Tonspur" instead of being clipped. */
@container (max-width: 380px) {
  .is-conditional .pf-chip { align-items: flex-start; white-space: normal; border-radius: 12px; }
  .is-conditional .pf-chip svg { margin-top: 2px; }
  .is-conditional .pf-chip small { display: block; margin: 0; }
}
.pf-frames { display: inline-flex; flex: none; gap: 2px; }
.pf-frames i { width: 14px; height: 9px; border-radius: 2px; background: url('/assets/editor-demo-source.jpg') no-repeat; background-size: 300% auto; }
.pf-frames i:nth-child(1) { background-position: 10% 40%; }
.pf-frames i:nth-child(2) { background-position: 50% 55%; }
.pf-frames i:nth-child(3) { background-position: 0 100%; }
.pf-back .pf-chip { border-color: var(--ink); font-weight: 500; } /* outlined: the filled pill is reserved for actions */
.pf-back .pf-chip svg { stroke: var(--ink); }

/* The optional assistant: dashed like the audio track, because it only applies when connected. */
.pf-assistant { grid-area: assistant; max-width: var(--measure); margin: var(--space-6) 0 0; padding-top: var(--space-4); border-top: 1px dashed var(--line-strong); font-size: var(--fs-meta); line-height: 1.6; color: var(--ink-2); }

.pf-service { grid-column: 2; grid-row: 1 / span 2; display: flex; flex-direction: column; align-items: center; justify-content: center; gap: var(--space-3); padding: var(--space-5) var(--space-4); border: 1px solid var(--line-strong); border-radius: var(--radius-md); background: var(--surface-2); text-align: center; }
.pf-service strong { font-size: var(--fs-body); line-height: var(--lh-title); font-weight: 600; color: var(--ink); }

/* ---------- Phone and tablet (≤960px): one column, no edge trunks ----------
   Reading order: stays title, laptop, ↓ connector, goes title, four chips, hairline, ↑ comes back.
   The service box is hidden: the title above the chips names it. */
@media (max-width: 960px) {
  .privacy-flow { grid-template-columns: minmax(0, 1fr); grid-template-areas: "stays" "screen" "base" "goes" "link" "assistant"; max-width: 560px; }
  .pf-assistant { margin-top: var(--space-5); }
  .pf-caption-stays { margin-bottom: var(--space-4); }
  .pf-screen { margin-inline: 4%; }
  .pf-base { margin-right: 0; }
  .pf-caption-goes { position: relative; margin-bottom: 0; padding: 52px 0 0; }
  .pf-caption-goes::before { content: ''; position: absolute; top: var(--space-2); left: 18px; width: 1px; height: 32px; background: var(--line-strong); transform-origin: 50% 0; }
  .pf-caption-goes::after { content: ''; position: absolute; top: 33px; left: 15px; width: 7px; height: 7px; border: solid var(--ink-2); border-width: 0 1px 1px 0; transform: rotate(45deg); }
  .pf-link { display: block; margin-top: var(--space-4); padding-block: 0; }
  .pf-service, .pf-wire, .pf-row::before, .pf-row::after { display: none; }
  .pf-out, .pf-back { container-type: normal; }
  .pf-row { padding-left: 0; min-height: 36px; }
  .pf-back { display: flex; flex-wrap: wrap; align-items: center; gap: var(--space-3); margin-top: var(--space-5); padding-top: var(--space-4); border-top: 1px solid var(--line); }
  .pf-back-label { padding-left: 0; margin: 0; }
  .pf-arrow { transform: rotate(90deg); } /* ← becomes ↑ (towards the laptop above) */
}
@media (max-width: 640px) {
  .pf-display { padding: var(--space-3); gap: var(--space-2); }
  .pf-screen { padding: 7px; border-radius: 14px 14px 5px 5px; }
  .pf-base { height: 11px; }
  .pf-base::before { width: 56px; margin-left: -28px; }
  .pf-chip { padding: 6px 12px 6px 9px; }
  .is-conditional .pf-chip { align-items: flex-start; white-space: normal; border-radius: 12px; }
  .is-conditional .pf-chip svg { margin-top: 2px; }
  .pf-chip small { display: block; margin: 0; }
  .pf-tag { top: 6px; left: 6px; }
  .pf-export .pf-tag { left: 50%; transform: translateX(-50%); }
  .pf-timeline { gap: var(--space-2); }
  .pf-track { height: 22px; }
}

/* ---------- Motion: desktop (≥961px), one sequence in three overlapping beats, about 2.7s ----------
   1 what leaves: each chip fades in next to the screen with a 12px slide inside its lane (never
     clipped, never over the laptop), its wire draws on to the service, then a packet dot runs
     along the wire into the service, which lights up
   2 what comes back: label and "Clip suggestions" chip, then the wire is drawn from the service
     and a packet runs back to the laptop
   Chips always appear before their wire is drawn under them, so no line shows through a chip
   while it fades.
   3 lands locally: clip markers rise on the timeline, the export fades in
   Step between lanes: --pf-step. Every value lands on the CSS default, so `done` = no attribute. */
@media screen and (min-width: 961px) {
  .privacy-flow { --pf-step: 120ms; }
  .motion .privacy-flow[data-flow-state=armed] .pf-wire { transform: scaleX(0); }
  .motion .privacy-flow[data-flow-state=armed] .pf-row::after,
  .motion .privacy-flow[data-flow-state=armed] .pf-back-label,
  .motion .privacy-flow[data-flow-state=armed] .pf-export .pf-tag { opacity: 0; }
  .motion .privacy-flow[data-flow-state=armed] .pf-out .pf-chip { opacity: 0; transform: translateX(-12px); }
  .motion .privacy-flow[data-flow-state=armed] .pf-back .pf-chip { opacity: 0; transform: translateX(12px); }
  .motion .privacy-flow[data-flow-state=armed] .pf-service > * { opacity: .4; }
  .motion .privacy-flow[data-flow-state=armed] .pf-track i { opacity: 0; transform: scaleY(0); }
  .motion .privacy-flow[data-flow-state=armed] .pf-export .sample-portrait { opacity: 0; transform: scale(var(--media-scale-from, .94)); }
  .motion .privacy-flow:is([data-flow-state=armed], [data-flow-state=play]) :is(.pf-wire, .pf-chip) { will-change: transform, opacity; }

  /* Beat 1: what leaves */
  .motion .privacy-flow[data-flow-state=play] .pf-out .pf-wire { transition: transform 560ms var(--pf-draw) calc(180ms + var(--i) * var(--pf-step)); }
  .motion .privacy-flow[data-flow-state=play] .pf-out .pf-chip { transition: transform 400ms var(--pf-travel-ease) calc(var(--i) * var(--pf-step)), opacity 400ms var(--pf-fade) calc(var(--i) * var(--pf-step)); }
  .motion .privacy-flow[data-flow-state=play] .pf-out .pf-row::before { animation: pf-packet-out 560ms var(--pf-draw) calc(520ms + var(--i) * var(--pf-step)) both; }
  .motion .privacy-flow[data-flow-state=play] .pf-out .pf-row::after { transition: opacity var(--dur-ui, 240ms) var(--pf-fade) calc(600ms + var(--i) * var(--pf-step)); }
  .motion .privacy-flow[data-flow-state=play] .pf-service > * { transition: opacity var(--dur-fade, 320ms) var(--pf-draw) 900ms; }
  /* Beat 2: what comes back (starts while the last packets are still arriving) */
  .motion .privacy-flow[data-flow-state=play] .pf-back-label { transition: opacity var(--dur-fade, 320ms) var(--pf-fade) 1050ms; }
  .motion .privacy-flow[data-flow-state=play] .pf-back .pf-wire { transition: transform 480ms var(--pf-draw) 1300ms; }
  .motion .privacy-flow[data-flow-state=play] .pf-back .pf-row::before { animation: pf-packet-back 520ms var(--pf-draw) 1400ms both; }
  .motion .privacy-flow[data-flow-state=play] .pf-back .pf-row::after { transition: opacity var(--dur-ui, 240ms) var(--pf-fade) 1750ms; }
  .motion .privacy-flow[data-flow-state=play] .pf-back .pf-chip { transition: transform 400ms var(--pf-travel-ease) 1100ms, opacity 400ms var(--pf-fade) 1100ms; }
  /* Beat 3: lands locally: the markers rise and the export appears together */
  .motion .privacy-flow[data-flow-state=play] .pf-track i { transition: transform var(--dur-fade, 320ms) var(--pf-fade) calc(1850ms + var(--i) * 80ms), opacity var(--dur-fade, 320ms) var(--pf-fade) calc(1850ms + var(--i) * 80ms); }
  .motion .privacy-flow[data-flow-state=play] .pf-export .sample-portrait { transition: opacity var(--dur-reveal-fade, 840ms) var(--pf-travel-ease) 1850ms, transform var(--dur-reveal, 640ms) var(--pf-travel-ease) 1850ms; }
  .motion .privacy-flow[data-flow-state=play] .pf-export .pf-tag { transition: opacity var(--dur-fade, 320ms) var(--pf-fade) 2000ms; }
}
/* The packet travels the lane's width (1cqw = 1% of .pf-out / .pf-back) and ends invisible. */
@keyframes pf-packet-out {
  0% { opacity: 0; transform: translateX(0); }
  20%, 80% { opacity: 1; }
  100% { opacity: 0; transform: translateX(calc(100cqw - 12px)); }
}
@keyframes pf-packet-back {
  0% { opacity: 0; transform: translateX(calc(100cqw - 12px)); }
  20%, 80% { opacity: 1; }
  100% { opacity: 0; transform: translateX(0); }
}
@supports not (width: 1cqw) { .pf-row::before { display: none; } } /* no container units: no packets */

/* ---------- Motion: phone and tablet (≤960px), the remote half only, about 1.6s ----------
   The laptop internals are never armed here: they have usually scrolled away by then. */
@media (max-width: 960px) {
  /* Delays: titles first, then the four chips, then the return row (stagger is 110ms on phones). */
  .pf-caption-goes strong { --pf-delay: 120ms; }
  .pf-caption-goes span { --pf-delay: calc(120ms + var(--stagger, 150ms)); }
  .pf-out .pf-chip { --pf-delay: calc(270ms + var(--i, 0) * var(--stagger, 150ms)); }
  .pf-back { --pf-delay: calc(270ms + 4 * var(--stagger, 150ms)); }
}
@media screen and (max-width: 960px) {
  .motion .privacy-flow[data-flow-state=armed] .pf-caption-goes::before { transform: scaleY(0); }
  .motion .privacy-flow[data-flow-state=armed] .pf-caption-goes::after { opacity: 0; }
  .motion .privacy-flow[data-flow-state=armed] .pf-caption-goes > *,
  .motion .privacy-flow[data-flow-state=armed] .pf-out .pf-chip,
  .motion .privacy-flow[data-flow-state=armed] .pf-back { opacity: 0; transform: translate3d(0, var(--reveal-y, 20px), 0); }

  .motion .privacy-flow[data-flow-state=play] .pf-caption-goes::before { transition: transform var(--dur-reveal, 640ms) var(--pf-draw); }
  .motion .privacy-flow[data-flow-state=play] .pf-caption-goes::after { transition: opacity var(--dur-ui, 240ms) var(--pf-fade) 480ms; }
  .motion .privacy-flow[data-flow-state=play] .pf-caption-goes > *,
  .motion .privacy-flow[data-flow-state=play] .pf-out .pf-chip,
  .motion .privacy-flow[data-flow-state=play] .pf-back { transition: opacity var(--dur-reveal-fade, 840ms) var(--pf-draw) var(--pf-delay), transform var(--dur-reveal, 640ms) var(--pf-draw) var(--pf-delay); }
}
