/*
  Sem Atrito · docs-motion.css · owner: dev-head. Pairs with shared/js/docs-motion.js (Design's v4.1 motion spec).
  Animates the before -> after document mock-ups in "Exemplos" (<div class="docs" data-docs-motion>).

  Progressive enhancement: every hidden state below needs .is-armed, which only the script adds (and never under
  prefers-reduced-motion). Without JS, or with reduced motion, the documents are simply shown finished.
  Animated: opacity, transform, clip-path, stroke-dashoffset, background-size, text-decoration-color only, so the
  layout is final from the first paint. Per element the script writes --d (delay), --t (duration), --n (typing steps);
  --dm-seek (on .docs) shifts every delay back, so a frozen frame (?docs=0.5) is "pause at that time".
*/
.docs[data-docs-motion] {
  --dm-settle: cubic-bezier(.16, 1, .3, 1);
  --dm-pop: cubic-bezier(.3, 1.6, .5, 1);
  --dm-accent: var(--c-accent, #2B44FF);
  --dm-wash: #FFF1C9;        /* the "doubtful" highlight (same as the static .q) */
  --dm-dots: #8A919E;        /* the dotted "messy" underline once flagged */
  --dm-seek: 0ms;
  position: relative;
}

/* Arrow: 60×33 in a 64 px column (Design v4.1; 44 px was too small to see the packet). */
.docs[data-docs-motion] > svg.arrow { width: 60px; height: 33px; overflow: visible; margin-left: auto; margin-right: auto; }
@media (min-width: 900px) {
  .docs[data-docs-motion] { grid-template-columns: minmax(0, 1fr) 64px minmax(0, 1fr); }
}
@media (max-width: 899px) {
  /* turned 90°: the drawn height is 60 px, so leave room above and below it */
  .docs[data-docs-motion] > svg.arrow { margin-top: 16px; margin-bottom: 16px; }
}
.docs[data-docs-motion] .dm-pk { fill: var(--dm-accent); opacity: 0; }

/* Wrappers the script adds: the text stays real DOM text, only its clip / opacity / transform change. */
.docs[data-docs-motion] .dm-t { display: inline-block; max-width: 100%; vertical-align: top; }
.docs[data-docs-motion] .dm-pop { display: inline-block; }
.docs[data-docs-motion] .dm-num { position: relative; display: inline-block; }
.docs[data-docs-motion] .dm-num-run { position: absolute; top: 0; left: 0; right: 0; opacity: 0; white-space: nowrap; }
.docs[data-docs-motion] td:last-child .dm-num-run { text-align: right; }
.docs.is-armed .dm-num.dm-counting .dm-num-v { opacity: 0; }
.docs.is-armed .dm-num.dm-counting .dm-num-run { opacity: 1; }

/* Flags redrawn so they can animate (the static look is a plain background / dotted underline). */
.docs.is-armed .doc.before .q.dm-fl {
  background: linear-gradient(var(--dm-wash), var(--dm-wash)) no-repeat 0 0 / 100% 100%;
}
.docs.is-armed .doc.before .mess.dm-fl { text-decoration-color: var(--dm-dots); }

/* The "depois" box border turns hair -> cobalt when it settles: a cobalt outline on top fades in. */
.docs.is-armed .doc.after { position: relative; border-color: var(--c-line, #E6E8EC); }
.docs.is-armed .doc.after::after {
  content: ""; position: absolute; inset: -1px; border: 1px solid var(--dm-accent); border-radius: inherit;
  pointer-events: none;
}

/* Stamp: its fill moves to ::before so the ring (::after) can sit behind it. */
.docs.is-armed .doc.after .dm-stamp { position: relative; isolation: isolate; background: transparent; }
.docs.is-armed .dm-stamp::before,
.docs.is-armed .dm-stamp::after { content: ""; position: absolute; border-radius: inherit; pointer-events: none; }
.docs.is-armed .dm-stamp::before { inset: 0; z-index: -1; background: var(--c-accent-soft, #E9ECFF); }
.docs.is-armed .dm-stamp::after { inset: -9px; z-index: -2; background: rgb(43 68 255 / .35); opacity: 0; }

/* ---------- armed, not yet played: the start frame ---------- */
.docs.is-armed .dm-t { clip-path: inset(-2px 100% -4px 0); }
.docs.is-armed .doc.before .q.dm-fl { background-size: 0 100%; }
.docs.is-armed .doc.before .mess.dm-fl { text-decoration-color: transparent; }
.docs.is-armed .arrow .dm-draw { stroke-dasharray: 1 1; stroke-dashoffset: 1; opacity: 0; }
.docs.is-armed .arrow .dm-head,
.docs.is-armed .dm-rw,
.docs.is-armed .dm-pop,
.docs.is-armed .dm-stamp,
.docs.is-armed .doc.after::after { opacity: 0; }

/* ---------- playing: each piece runs once, from its own delay, and holds its end state ---------- */
.docs.is-playing .dm-t,
.docs.is-playing .dm-fl,
.docs.is-playing .arrow .dm-draw,
.docs.is-playing .arrow .dm-head,
.docs.is-playing .dm-rw,
.docs.is-playing .dm-pop,
.docs.is-playing .dm-stamp,
.docs.is-playing .dm-stamp::after,
.docs.is-playing .doc.after::after {
  animation-duration: var(--t, 400ms);
  animation-delay: calc(var(--d, 0ms) - var(--dm-seek));
  animation-fill-mode: both;
  animation-timing-function: var(--dm-settle);
}
.docs.is-playing .dm-t { animation-name: dm-type; animation-timing-function: steps(var(--n, 12), end); }
.docs.is-playing .q.dm-fl { animation-name: dm-wash; }
.docs.is-playing .mess.dm-fl { animation-name: dm-dots; }
.docs.is-playing .arrow .dm-draw { animation-name: dm-draw; animation-timing-function: cubic-bezier(.45, 0, .25, 1); }
.docs.is-playing .arrow .dm-head { animation-name: dm-fade; }
.docs.is-playing .dm-rw { animation-name: dm-row; }
.docs.is-playing .dm-pop { animation-name: dm-pop; animation-timing-function: var(--dm-pop); }
.docs.is-playing .dm-stamp { animation-name: dm-stamp; }
.docs.is-playing .dm-stamp::after { animation-name: dm-ring; animation-timing-function: ease-out; }
.docs.is-playing .doc.after::after {
  animation-name: dm-fade; animation-delay: calc(var(--db, 0ms) - var(--dm-seek)); animation-duration: var(--tb, 700ms);
}

/* D-5 (cycle 7): once a block has finished, nothing is clipped any more (a cell that ends up wider than its typing
   box after a late font swap or resize would otherwise stay cut), and the first "depois" column keeps 10ch so short
   labels never squeeze into a clip. */
.docs[data-docs-motion].is-done .dm-t { clip-path: none; animation: none; }
.docs[data-docs-motion] .doc.after td:first-child { min-width: 10ch; }

/* frozen frame for screenshots (?docs=0.5 or data-docs-freeze="0.5") */
.docs.is-frozen *, .docs.is-frozen *::before, .docs.is-frozen *::after,
.docs.is-frozen .doc.after::after { animation-play-state: paused !important; }

@keyframes dm-type { from { clip-path: inset(-2px 100% -4px 0); } to { clip-path: inset(-2px -2px -4px 0); } }
@keyframes dm-wash { from { background-size: 0 100%; } to { background-size: 100% 100%; } }
@keyframes dm-dots { from { text-decoration-color: transparent; } to { text-decoration-color: var(--dm-dots); } }
/* opacity: a zero-length dash still paints its round cap as a dot, so the line is hidden until it starts */
@keyframes dm-draw { 0% { stroke-dashoffset: 1; opacity: 0; } 2% { opacity: 1; } 100% { stroke-dashoffset: 0; opacity: 1; } }
@keyframes dm-fade { from { opacity: 0; } to { opacity: 1; } }
@keyframes dm-row { from { opacity: 0; transform: translateX(-8px); } to { opacity: 1; transform: none; } }
@keyframes dm-pop { from { opacity: 0; transform: scale(.4); } to { opacity: 1; transform: none; } }
@keyframes dm-stamp {
  0% { opacity: 0; transform: translateY(6px); }
  45%, 100% { opacity: 1; transform: none; }
}
@keyframes dm-ring {
  0%, 44% { opacity: 0; transform: scale(var(--sx, .9), var(--sy, .5)); }
  45% { opacity: 1; transform: scale(var(--sx, .9), var(--sy, .5)); }
  100% { opacity: 0; transform: none; }
}

/* ---------- replay: a quiet real button under the block, shown once it has finished ---------- */
.docs[data-docs-motion] .dm-replay {
  position: absolute; top: 100%; right: -6px; margin-top: 4px;
  display: inline-flex; align-items: center; gap: 6px; min-height: 28px; padding: 2px 6px;
  background: none; border: 0; border-radius: 4px; cursor: pointer;
  font-family: var(--font-mono, ui-monospace, monospace); font-size: 11px; letter-spacing: .08em; text-transform: uppercase;
  color: var(--c-muted, #646B79);
  opacity: 0; visibility: hidden; transition: opacity .3s ease, color .2s ease, visibility 0s linear .3s;
}
.docs[data-docs-motion] .dm-replay__i { display: block; flex: none; }
.docs[data-docs-motion].is-done .dm-replay { opacity: 1; visibility: visible; transition: opacity .3s ease, color .2s ease; }
.docs[data-docs-motion] .dm-replay:hover { color: var(--dm-accent); }
.docs[data-docs-motion] .dm-replay:focus-visible { color: var(--dm-accent); outline: 2px solid var(--dm-accent); outline-offset: 2px; }
.docs[data-docs-motion].is-still .dm-replay { display: none; }

@media (prefers-reduced-motion: reduce) {
  .docs[data-docs-motion] .dm-replay { display: none; }
}
