/* Writing Studio – motion (Part 6)
   The rules:
   - Animate only in response to something you do (open a page, tap, type, get a score).
   - Ease-out cubic-bezier(.16,1,.3,1); 120–300 ms, never over 400 ms (the score ring: 900 ms).
   - Only transform and opacity move, so it stays smooth on a mid-range Android phone.
   - All movement sits inside @media (prefers-reduced-motion: no-preference). With reduced motion only fades are kept.
   - html[data-motion=off] turns every animation off: Settings → Animations off, and always in Strict Exam Mode
     (the exam's only animation is the final-minute header colour fade, in exam.css).
   Loading loops (shimmer, thinking dots) repeat every 1.2 s; each sweep or bounce moves for 400 ms, then rests. */

:root { --ease: cubic-bezier(.16,1,.3,1); }

@keyframes fade-in  { from { opacity: 0; } }
@keyframes fade-out { to { opacity: 0; } }
@keyframes vt-in    { from { opacity: 0; transform: translateY(8px); } }
@keyframes rise     { from { opacity: 0; transform: translateY(14px); } }
@keyframes sheet-in { from { opacity: 0; transform: translateY(16px); } }
@keyframes chip-in  { from { opacity: 0; transform: translateY(6px); } }
@keyframes toast-in { from { opacity: 0; transform: translate(-50%, -8px); } to { opacity: 1; transform: translate(-50%, 0); } }
@keyframes shimmer  { 0% { transform: translateX(-100%); } 33%, 100% { transform: translateX(100%); } }
@keyframes dot      { 0%, 34%, 100% { opacity: .35; transform: none; } 17% { opacity: 1; transform: translateY(-3px); } }
@keyframes pulse    { 0%, 100% { opacity: 1; } 17% { opacity: .55; } 34% { opacity: 1; } }
@keyframes dot-fade { 0%, 34%, 100% { opacity: .35; } 17% { opacity: 1; } }

/* ---------- Page changes, cover morph, theme (view transitions) ---------- */
/* The site header stays put while the page under it changes */
.topbar { view-transition-name: topbar; }
::view-transition-group(topbar) { animation: none; }
::view-transition-old(topbar) { display: none; }
::view-transition-new(topbar) { animation: none; }
/* named groups (book covers) are moved by js/app.js morph() with transform only */
::view-transition-group(*) { transform-origin: 0 0; animation-timing-function: var(--ease); }
::view-transition-old(*) { animation: fade-out 240ms var(--ease) both; }     /* plain opacity cross-fades */
::view-transition-new(*) { animation: fade-in 240ms var(--ease) both; }
/* the page: old view fades out, the new one fades in while rising 8 px (220 ms). The root group itself never moves. */
::view-transition-group(root) { animation: none; }
::view-transition-old(root) { animation: fade-out 180ms var(--ease) both; }
::view-transition-new(root) { animation: vt-in 220ms var(--ease) both; }
@media (prefers-reduced-motion: reduce) {
  ::view-transition-new(root) { animation-name: fade-in; }
  [data-vt] { view-transition-name: none !important; }     /* no morph: a plain cross-fade */
}
/* dark mode: everything cross-fades over 200 ms (only opacity moves) */
html.vt-theme .topbar, html.vt-theme [data-vt] { view-transition-name: none !important; }
html.vt-theme::view-transition-old(root) { animation: fade-out 200ms var(--ease) both; }
html.vt-theme::view-transition-new(root) { animation: fade-in 200ms var(--ease) both; }

/* ---------- Fades (kept with reduced motion) ---------- */
html[data-motion=on] dialog.sheet[open]::backdrop { animation: fade-in 240ms var(--ease); }
html[data-motion=on] .book::before,
html[data-motion=on] .test-card::after { transition: opacity 260ms var(--ease); }
html[data-motion=on] .mirror .sp-new { animation: fade-in 150ms ease-out both; }      /* new spelling underlines */
html[data-motion=on] .mirror .sp-fix { animation: fade-out 150ms ease-out forwards; }  /* a fixed word's underline */
html[data-motion=on] .pr-saved { transition: opacity 300ms var(--ease); }              /* "Saved ✓" fades out after 1.5 s */
html[data-motion=on] .sg-lvl { transition: opacity 120ms var(--ease); }
html[data-motion=on] .hp-switch[aria-busy=true] .hp-note::before { animation: pulse 1.2s var(--ease) infinite; }

/* ---------- Movement (only without "reduce motion") ---------- */
@media (prefers-reduced-motion: no-preference) {
  /* every button: a small press */
  html[data-motion=on] :is(button, .btn) { transition: transform 120ms var(--ease); }
  html[data-motion=on] :is(button, .btn):active:not(:disabled) { transform: scale(.97); transition-duration: 120ms; }

  /* shelf: books rise in one after another on the first page of a visit (30 ms apart) */
  html[data-motion=on] .bookcase.is-entering .book { animation: rise 280ms var(--ease) backwards; animation-delay: calc(var(--i, 0) * 30ms); }
  /* hover or focus: the book lifts and turns a little; the deeper shadow fades in (app.css) */
  html[data-motion=on] .book { transition: transform 260ms var(--ease); }
  html[data-motion=on] .book:is(:hover, :focus-visible) { transform: perspective(800px) translateY(-6px) rotateY(-8deg); }
  html[data-motion=on] .book:active { transform: perspective(800px) translateY(-3px) rotateY(-4deg) scale(.98); transition-duration: 120ms; }

  /* test cards and the mode sheet */
  html[data-motion=on] .test-card { transition: transform 220ms var(--ease); }
  html[data-motion=on] .test-card:hover { transform: translateY(-3px); }
  html[data-motion=on] .test-card:active { transform: translateY(-1px) scale(.97); transition-duration: 120ms; }
  html[data-motion=on] dialog.sheet[open] { animation: sheet-in 240ms var(--ease); }
  html[data-motion=on] .mode-choice { transition: transform 200ms var(--ease); }
  html[data-motion=on] .mode-choice:hover { transform: scale(1.02); }
  html[data-motion=on] button.mode-choice:active { transform: scale(.97); transition-duration: 120ms; }

  /* switches and the plan chevron */
  html[data-motion=on] .hp-thumb, html[data-motion=on] input.switch::after { transition: transform 200ms var(--ease); }
  html[data-motion=on] .sg-plan > summary::after { transition: transform 200ms var(--ease); }

  /* writing: the word-count pill fills (the number roll and the pop are in practice.js) */
  html[data-motion=on] .wc-fill { transition: transform 180ms var(--ease); }

  /* suggestion chips fade in and rise 6 px, 40 ms apart */
  html[data-motion=on] .sg-chip.sg-in { animation: chip-in 220ms var(--ease) backwards; animation-delay: calc(var(--i, 0) * 40ms); }
  html[data-motion=on] .sg-lvl { transform: translateY(2px); transition: opacity 120ms var(--ease), transform 120ms var(--ease); }
  html[data-motion=on] .sg-chip:is(:hover, :focus-visible) .sg-lvl { transform: none; }

  /* shimmer on skeletons and loading chips */
  html[data-motion=on] .sk::after, html[data-motion=on] .sg-ghost::after {
    content: ''; position: absolute; inset: 0; transform: translateX(-100%);
    background: linear-gradient(90deg, transparent 0%, var(--skeleton-shine) 50%, transparent 100%);
    animation: shimmer 1.2s var(--ease) infinite;
  }
  html[data-motion=on] .sg-ghost { position: relative; }

  /* the 3-dot "thinking" indicator */
  html[data-motion=on] .dots3 i { animation: dot 1.2s var(--ease) infinite; }
  html[data-motion=on] .dots3 i:nth-child(2) { animation-delay: .13s; }
  html[data-motion=on] .dots3 i:nth-child(3) { animation-delay: .26s; }

  /* toasts slide down 8 px and fade in */
  html[data-motion=on] .toast:not([hidden]) { animation: toast-in 200ms var(--ease); }
}

/* ---------- Reduced motion: the same moments, as fades ---------- */
@media (prefers-reduced-motion: reduce) {
  html[data-motion=on] .bookcase.is-entering .book { animation: fade-in 240ms var(--ease) backwards; animation-delay: calc(var(--i, 0) * 30ms); }
  html[data-motion=on] dialog.sheet[open] { animation: fade-in 240ms var(--ease); }
  html[data-motion=on] .sg-chip.sg-in { animation: fade-in 200ms var(--ease) backwards; animation-delay: calc(var(--i, 0) * 40ms); }
  html[data-motion=on] .toast:not([hidden]) { animation: fade-in 200ms var(--ease); }
  html[data-motion=on] .sk, html[data-motion=on] .sg-ghost { animation: pulse 1.2s var(--ease) infinite; }
  html[data-motion=on] .dots3 i { animation: dot-fade 1.2s var(--ease) infinite; }
  html[data-motion=on] .dots3 i:nth-child(2) { animation-delay: .13s; }
  html[data-motion=on] .dots3 i:nth-child(3) { animation-delay: .26s; }
}
