/* motion.css (TSK-2513). Values only through var(--token) from tokens.css.
   Two parts: Transitions (U4, TSK-2499) and, later, Animations (U5). */

/* Transitions (U4) ------------------------------------------------------------
   In-page state changes. Only transform and opacity move. A thing comes in with
   --ease-enter and goes out with --ease-exit, in --dur-fast, --dur-base or
   --dur-slow. The state rules are outside the media query, so with
   prefers-reduced-motion: reduce the state changes at once and nothing moves.
   The JS (menu.js, attach.js, actions.js) adds or removes the state class and
   waits for the computed transition time before it removes an element.
   Page change: the spiral veil and the view-transition cross-fade (spiral.css). */

/* --- composer grow (chat.js: .cs-growing while it moves, .cs-grow-start for one frame) */
.cs-composer.cs-growing { overflow: clip; }
.cs-composer.cs-grow-start .cs-field { transform: translateY(var(--cs-grow)); }

/* --- menu drawer and scrim (menu.js: .mn-layer gets .mn-open) --------------- */
.mn-layer .mn-backdrop { opacity: 0; }
.mn-layer .mn-drawer { transform: translateX(-100%); }
[dir="rtl"] .mn-layer .mn-drawer { transform: translateX(100%); }
.mn-layer.mn-open .mn-backdrop { opacity: 1; }
.mn-layer.mn-open .mn-drawer { transform: none; }
.mn-layer:not(.mn-open) { pointer-events: none; }  /* a closing drawer takes no tap */

/* --- attach sheet (attach.js: .at-sheet gets .at-open; states in attach.css) - */
.at-sheet:not(.at-open):not(.at-page) { pointer-events: none; }

/* --- picture to full size and back (details.mx-zoom; actions.js sets
   .mx-opening for one frame on open, .mx-closing before the close) ---------- */
.mx-zoom.mx-opening > summary,
.mx-zoom.mx-closing > summary { opacity: 0; transform: scale(var(--scale-from)); }

/* --- source chip open and close (details.mx-src; actions.js: .mx-closing) --- */
.mx-src.mx-closing .mx-src-body { opacity: 0; transform: translateY(calc(var(--shift-enter) * -1)); }

@media (prefers-reduced-motion: no-preference) {
  .mn-layer .mn-backdrop { transition: opacity var(--dur-fast) var(--ease-exit); }
  .mn-layer .mn-drawer { transition: transform var(--dur-fast) var(--ease-exit); }
  .mn-layer.mn-open .mn-backdrop { transition: opacity var(--dur-fast) var(--ease-enter); }
  .mn-layer.mn-open .mn-drawer { transition: transform var(--dur-base) var(--ease-enter); }

  .at-backdrop { transition: opacity var(--dur-fast) var(--ease-exit); }
  .at-panel { transition: transform var(--dur-fast) var(--ease-exit); }
  .at-sheet.at-open .at-backdrop { transition: opacity var(--dur-fast) var(--ease-enter); }
  .at-sheet.at-open .at-panel { transition: transform var(--dur-base) var(--ease-enter); }
  .at-page .at-panel { transition: none; }

  .mx-zoom[open] > summary {
    transition: opacity var(--dur-slow) var(--ease-enter), transform var(--dur-slow) var(--ease-enter);
  }
  .mx-zoom.mx-opening > summary { transition: none; }  /* jump to the start, then grow */
  .mx-zoom.mx-closing > summary {
    transition: opacity var(--dur-fast) var(--ease-exit), transform var(--dur-fast) var(--ease-exit);
  }

  .mx-src[open] .mx-src-body {
    transition: opacity var(--dur-fast) var(--ease-enter), transform var(--dur-fast) var(--ease-enter);
  }
  @starting-style {
    .mx-src[open] .mx-src-body { opacity: 0; transform: translateY(calc(var(--shift-enter) * -1)); }
  }
  .mx-src.mx-closing .mx-src-body {
    transition: opacity var(--dur-fast) var(--ease-exit), transform var(--dur-fast) var(--ease-exit);
  }

  /* chip to sent message: the chip gives under the finger, the sent message
     comes up from the composer. */
  .cs-chip, .mx-chip { transition: transform var(--dur-fast) var(--ease-exit); }
  .cs-chip:active, .msg .mx-chip:active { transform: scale(var(--scale-press)); }
  .cs-thread > .msg-person.cs-mine {
    animation-name: none;  /* the transition below moves it, with the enter easing */
    transition: opacity var(--dur-base) var(--ease-enter), transform var(--dur-base) var(--ease-enter);
  }
  @starting-style {
    .cs-thread > .msg-person.cs-mine { opacity: 0; transform: translateY(var(--shift-enter)); }
  }
}

/* Animations (U5) -------------------------------------------------------------
   Each animation runs one time when its thing comes in; none loops (the spiral
   is the only loop, spiral.css). Only transform, opacity and, for the trend
   line, stroke-dashoffset move, with the motion tokens. The rest state is the
   normal style, so every rule lives under no-preference: with
   prefers-reduced-motion: reduce, or with no JavaScript, the content is there
   at once. The keyframes only say where a thing comes from (fill backwards).
   messages.js marks the streamed text (.an-chunk) and a message that takes the
   place of the same message (.an-keep: no entry again) or of its skeleton
   (.an-swap: a fade, no move). This is the one message entry (the old mx-in
   and cs-in are gone, TSK-2546); the U4 chip-to-sent rule stays first. */

@media (prefers-reduced-motion: no-preference) {
  @keyframes an-in { from { opacity: 0; transform: translateY(var(--shift-enter)); } }
  @keyframes an-fade { from { opacity: 0; } }
  @keyframes an-pop { from { opacity: 0; transform: scale(var(--scale-pop)); } }
  @keyframes an-draw { from { stroke-dashoffset: 1; } }

  /* --- message entry ------------------------------------------------------ */
  .msg, .cs-thread > .msg { animation: an-in var(--dur-base) var(--ease-enter) backwards; }
  .msg.an-swap, .cs-thread > .msg.an-swap { animation-name: an-fade; }
  .msg.an-keep, .cs-thread > .msg.an-keep { animation: none; }

  /* --- the items of a message come in one after the other (--stagger) ----- */
  :is(.mx-rows > li, .mx-plan-step, .mx-tl-item, .mx-snap-row, .mx-cmp-row, .mx-docs > li,
      .mx-chips > .mx-chip, .mx-used-chips > .mx-rchip, .mx-card-lines > li, .mx-vnum) {
    animation: an-in var(--dur-base) var(--ease-enter) backwards;
  }
  :is(.mx-rows > li, .mx-plan-step, .mx-tl-item, .mx-snap-row, .mx-cmp-row, .mx-docs > li,
      .mx-chips > .mx-chip, .mx-used-chips > .mx-rchip, .mx-card-lines > li):nth-child(2) { animation-delay: var(--stagger); }
  :is(.mx-rows > li, .mx-plan-step, .mx-tl-item, .mx-snap-row, .mx-cmp-row, .mx-docs > li,
      .mx-chips > .mx-chip, .mx-used-chips > .mx-rchip, .mx-card-lines > li):nth-child(3) { animation-delay: calc(var(--stagger) * 2); }
  :is(.mx-rows > li, .mx-plan-step, .mx-tl-item, .mx-snap-row, .mx-cmp-row, .mx-docs > li,
      .mx-chips > .mx-chip, .mx-used-chips > .mx-rchip, .mx-card-lines > li):nth-child(4) { animation-delay: calc(var(--stagger) * 3); }
  :is(.mx-rows > li, .mx-plan-step, .mx-tl-item, .mx-snap-row, .mx-cmp-row, .mx-docs > li,
      .mx-chips > .mx-chip, .mx-used-chips > .mx-rchip, .mx-card-lines > li):nth-child(5) { animation-delay: calc(var(--stagger) * 4); }
  :is(.mx-rows > li, .mx-plan-step, .mx-tl-item, .mx-snap-row, .mx-cmp-row, .mx-docs > li,
      .mx-chips > .mx-chip, .mx-used-chips > .mx-rchip, .mx-card-lines > li):nth-child(n+6) { animation-delay: calc(var(--stagger) * 5); }
  .mx-vnum { animation-delay: var(--stagger); }

  /* --- streamed answer text: each new piece fades in (messages.js) -------- */
  .cs-streaming .an-chunk { animation: an-fade var(--dur-fast) var(--ease-enter) backwards; }

  /* --- trend chart: the line draws from the first value to the last; the
     older dots come with it, the newest value and its ring come at the end --- */
  .mx-tr-line { stroke-dasharray: 1; animation: an-draw var(--dur-draw) var(--ease-standard) var(--dur-fast) backwards; }
  .mx-tr-dot { animation: an-fade var(--dur-draw) var(--ease-standard) var(--dur-fast) backwards; }
  .mx-tr-ring, .mx-tr-dot.mx-tr-new {
    transform-box: fill-box;
    transform-origin: center;
    animation: an-pop var(--dur-base) var(--ease-enter) calc(var(--dur-fast) + var(--dur-draw)) backwards;
  }

  /* --- scan marks come in order; line n of the list comes with mark n ----- */
  .mx-scan-mk { transform-box: fill-box; transform-origin: center; animation: an-pop var(--dur-base) var(--ease-enter) var(--dur-base) backwards; }
  .mx-scan-item { animation: an-in var(--dur-base) var(--ease-enter) var(--dur-base) backwards; }
  .mx-scan-mk:nth-of-type(2), .mx-scan-item:nth-child(2) { animation-delay: calc(var(--dur-base) + var(--dur-fast)); }
  .mx-scan-mk:nth-of-type(3), .mx-scan-item:nth-child(3) { animation-delay: calc(var(--dur-base) + var(--dur-fast) * 2); }
  .mx-scan-mk:nth-of-type(4), .mx-scan-item:nth-child(4) { animation-delay: calc(var(--dur-base) + var(--dur-fast) * 3); }
  .mx-scan-mk:nth-of-type(5), .mx-scan-item:nth-child(5) { animation-delay: calc(var(--dur-base) + var(--dur-fast) * 4); }
  .mx-scan-mk:nth-of-type(n+6), .mx-scan-item:nth-child(n+6) { animation-delay: calc(var(--dur-base) + var(--dur-fast) * 5); }

  /* --- emphasis: an out-of-range mark, a finding's badge and a new finding - */
  .mx-value:is([data-status="below"], [data-status="above"]) .mx-glyph,
  .mx-find .mx-badge {
    animation: an-pop var(--dur-base) var(--ease-enter) var(--dur-base) backwards;
  }
  .mx-find[data-relation="new"] .mx-find-rel {
    animation: an-pop var(--dur-base) var(--ease-enter) calc(var(--dur-base) + var(--stagger)) backwards;
  }

  /* a message that only took the place of itself: its parts do not come again */
  .an-keep :is(.mx-rows > li, .mx-plan-step, .mx-tl-item, .mx-snap-row, .mx-cmp-row, .mx-docs > li,
      .mx-chips > .mx-chip, .mx-used-chips > .mx-rchip, .mx-card-lines > li, .mx-vnum,
      .mx-tr-line, .mx-tr-dot, .mx-tr-ring, .mx-scan-mk, .mx-scan-item, .mx-glyph, .mx-badge, .mx-find-rel) {
    animation: none;
  }

  /* --- composer grow (chat.js settle, TSK-2537): a taller field starts at its
     old top and moves up; the composer clips it while it moves ------------- */
  .cs-field { transition: transform var(--dur-fast) var(--ease-enter); }
  .cs-composer.cs-grow-start .cs-field { transition: none; }  /* the start frame jumps */

  /* --- send button: the arrow grows when send is on, gives under the finger - */
  .cs-send { transition: transform var(--dur-fast) var(--ease-exit); }
  .cs-send .cs-ico { transition: transform var(--dur-fast) var(--ease-enter); }
  .cs-send:disabled .cs-ico { transition: transform var(--dur-fast) var(--ease-exit); transform: scale(var(--scale-rest)); }
  .cs-send:enabled:active { transform: scale(var(--scale-press)); }
}
