/* Material 3 Expressive motion (https://m3.material.io/styles/motion): things that move, grow or
   change shape ride springs (tokens.css → --md-sys-motion-spring-*); fades stay on plain easing.
   Exits keep M3's quick accelerate curves, so leaving never bounces.
   Transition patterns: fade through (top-level destinations), shared axis X (steps) and Z
   (into / out of full-screen flows), container transform (chapter card ↔ chapter screen),
   plus a page turn for lesson slides. Easing + durations follow the M3 tokens in tokens.css. */

@property --page-shade { syntax: "<number>"; inherits: true; initial-value: 0; }

/* ---------- Screen transitions (View Transitions API; see motion.js screenTransition) ---------- */
.m3-nav-bar { view-transition-name: nav-bar; }   /* navigation stays put while screens change */
.m3-nav-rail { view-transition-name: nav-rail; }

::view-transition-group(*) {
  animation-duration: var(--md-sys-motion-spring-spatial-default-duration);
  animation-timing-function: var(--md-sys-motion-spring-spatial-default);
}

/* (Material's own timings: those with a token use it; fade-through's 105ms, the 90ms and 80ms delays and the
   container's 350ms fades have no token and stay as numbers) */
/* Fade through - between Learn / Revise / Sounds / Profile */
html[data-vt="fade-through"]::view-transition-old(root) { animation: 105ms var(--md-sys-motion-easing-emphasized-accelerate) both vt-fade-out; }
html[data-vt="fade-through"]::view-transition-new(root) { animation: var(--md-sys-motion-spring-spatial-default-duration) var(--md-sys-motion-spring-spatial-default) 105ms both vt-fade-in-grow; }

/* Shared axis X - forward / back within a flow */
html[data-vt="forward"]::view-transition-old(root) { animation: var(--md-sys-motion-duration-short3) var(--md-sys-motion-easing-emphasized-accelerate) both vt-out-start; }
html[data-vt="forward"]::view-transition-new(root) { animation: var(--md-sys-motion-spring-spatial-default-duration) var(--md-sys-motion-spring-spatial-default) 90ms both vt-in-end; }
html[data-vt="back"]::view-transition-old(root) { animation: var(--md-sys-motion-duration-short3) var(--md-sys-motion-easing-emphasized-accelerate) both vt-out-end; }
html[data-vt="back"]::view-transition-new(root) { animation: var(--md-sys-motion-spring-spatial-default-duration) var(--md-sys-motion-spring-spatial-default) 90ms both vt-in-start; }

/* Shared axis Z - into a lesson / quiz / conversation, and back out */
html[data-vt="z-in"]::view-transition-old(root) { animation: var(--md-sys-motion-duration-short4) var(--md-sys-motion-easing-emphasized-accelerate) both vt-z-out-grow; }
html[data-vt="z-in"]::view-transition-new(root) { animation: var(--md-sys-motion-spring-spatial-slow-duration) var(--md-sys-motion-spring-spatial-slow) 80ms both vt-z-in-from-small; }
html[data-vt="z-out"]::view-transition-old(root) { animation: var(--md-sys-motion-duration-short4) var(--md-sys-motion-easing-emphasized-accelerate) both vt-z-out-shrink; }
html[data-vt="z-out"]::view-transition-new(root) { animation: var(--md-sys-motion-spring-spatial-slow-duration) var(--md-sys-motion-spring-spatial-slow) 80ms both vt-z-in-from-large; }

/* Container transform - the tapped chapter card grows into the chapter screen (and back) */
html[data-vt="container"]::view-transition-group(vt-container) {
  animation-duration: var(--md-sys-motion-spring-spatial-slow-duration);
  animation-timing-function: var(--md-sys-motion-spring-spatial-slow);
  border-radius: var(--md-sys-shape-corner-extra-large-increased);
  overflow: hidden;
}
html[data-vt="container"]::view-transition-old(vt-container),
html[data-vt="container"]::view-transition-new(vt-container) { height: 100%; object-fit: cover; object-position: top left; }
html[data-vt="container"]::view-transition-old(vt-container) { animation: var(--md-sys-motion-duration-short3) linear both vt-fade-out; }
html[data-vt="container"]::view-transition-new(vt-container) { animation: 350ms linear 150ms both vt-fade-in; }
html[data-vt="container"]::view-transition-old(root) { animation: var(--md-sys-motion-duration-short4) var(--md-sys-motion-easing-emphasized-accelerate) both vt-fade-out; }
html[data-vt="container"]::view-transition-new(root) { animation: 350ms var(--md-sys-motion-easing-emphasized-decelerate) 150ms both vt-fade-in; }

@keyframes vt-fade-out { to { opacity: 0; } }
@keyframes vt-fade-in { from { opacity: 0; } }
@keyframes vt-fade-in-grow { from { opacity: 0; transform: scale(0.92); } }
@keyframes vt-out-start { to { opacity: 0; transform: translateX(-30px); } }
@keyframes vt-out-end { to { opacity: 0; transform: translateX(30px); } }
@keyframes vt-in-end { from { opacity: 0; transform: translateX(30px); } }
@keyframes vt-in-start { from { opacity: 0; transform: translateX(-30px); } }
@keyframes vt-z-out-grow { to { opacity: 0; transform: scale(1.1); } }
@keyframes vt-z-in-from-small { from { opacity: 0; transform: scale(0.8); } }
@keyframes vt-z-out-shrink { to { opacity: 0; transform: scale(0.8); } }
@keyframes vt-z-in-from-large { from { opacity: 0; transform: scale(1.1); } }


/* ---------- Navigation bar / rail: the active indicator expands from its centre ---------- */
.nav-item[aria-current="page"] .indicator { background: transparent; }
.nav-item .indicator::after {
  content: ""; position: absolute; inset: 0; z-index: -2; border-radius: inherit;
  background: var(--md-sys-color-secondary-container);
  opacity: 0; transform: scaleX(0.4);
  transition: transform var(--md-sys-motion-spring-spatial-fast-duration) var(--md-sys-motion-spring-spatial-fast),
              opacity var(--md-sys-motion-duration-short2) linear;
}
.nav-item[aria-current="page"] .indicator::after { opacity: 1; transform: scaleX(1); }
.nav-item .icon { transition: font-variation-settings var(--md-sys-motion-duration-medium2) var(--md-sys-motion-easing-emphasized); }
.nav-item .nav-label { transition: color var(--md-sys-motion-duration-short4) linear; }

/* ---------- Lesson: pages of a book ---------- */
/* (the lesson shell's layout: app.css) */
.page-moving {
  position: absolute; top: 0; left: 0; right: 0; z-index: 2;
  background: var(--md-sys-color-surface);
  border-radius: 0 var(--md-sys-shape-corner-large) var(--md-sys-shape-corner-large) 0;
  box-shadow: var(--md-sys-elevation-level3);
  transform-origin: left center;
  backface-visibility: hidden;
  will-change: transform;
}
.page-moving::after { /* the curling page catches less light */
  content: ""; position: absolute; inset: 0; border-radius: inherit; pointer-events: none;
  background: linear-gradient(to right, transparent 0%, rgb(0 0 0 / calc(var(--page-shade) * 0.6)) 55%, rgb(0 0 0 / var(--page-shade)) 100%);
}
.lesson-stage.turning { pointer-events: none; }

/* answer appears on "Check" */
.reveal { animation: reveal-in var(--md-sys-motion-spring-spatial-default-duration) var(--md-sys-motion-spring-spatial-default) both; }
@keyframes reveal-in { from { opacity: 0; transform: translateY(16px) scale(0.98); } }

/* ---------- Quiz feedback ---------- */
.option { transition: background-color var(--md-sys-motion-duration-short4) linear, border-color var(--md-sys-motion-duration-short4) linear, color var(--md-sys-motion-duration-short4) linear,
  border-radius var(--md-sys-motion-spring-spatial-fast-duration) var(--md-sys-motion-spring-spatial-fast); }
/* (.options > ... : over the options' own entrance below, which would otherwise win and the pop never play) */
.options > .option.correct, .option.correct { animation: answer-pop var(--md-sys-motion-spring-spatial-fast-duration) var(--md-sys-motion-spring-spatial-fast); }
.options > .option.wrong, .option.wrong { animation: answer-nudge var(--md-sys-motion-duration-medium2) var(--md-sys-motion-easing-standard); }
/* a built phrase answers the same way: the whole answer pops when it's right and shakes when it's wrong */
.build-answer.correct { animation: answer-pop var(--md-sys-motion-spring-spatial-fast-duration) var(--md-sys-motion-spring-spatial-fast); }
.build-answer.wrong { animation: answer-nudge var(--md-sys-motion-duration-medium2) var(--md-sys-motion-easing-standard); }
@keyframes answer-pop { 40% { transform: scale(1.03); } }
@keyframes answer-nudge { 25% { transform: translateX(-5px); } 75% { transform: translateX(5px); } }
.quiz-footer { transition: background-color var(--md-sys-motion-duration-medium2) linear, color var(--md-sys-motion-duration-medium2) linear; }
.quiz-footer > * { animation: footer-in var(--md-sys-motion-spring-spatial-default-duration) var(--md-sys-motion-spring-spatial-default) both; }
@keyframes footer-in { from { opacity: 0; transform: translateY(8px); } }
.build-answer .word-chip { animation: chip-in var(--md-sys-motion-spring-spatial-fast-duration) var(--md-sys-motion-spring-spatial-fast); }
@keyframes chip-in { from { opacity: 0; transform: scale(0.85); } }

/* ---------- Content arrives in a short stagger (M3 choreography) ---------- */
.content-pad.stack > *, .chapter-list > li, .bubbles > .bubble-row, .options > .option,
.sound-list > *, .lang-list > *, .ui-lang-list > *, .stats > *, .phrase-preview > li {
  animation: item-in var(--md-sys-motion-spring-spatial-default-duration) var(--md-sys-motion-spring-spatial-default) both;
  animation-delay: calc(60ms + var(--i, 0) * 40ms);
}
.bubbles > .bubble-row { animation-delay: calc(80ms + var(--i, 0) * 70ms); }
:is(.content-pad.stack, .chapter-list, .bubbles, .options, .sound-list, .lang-list, .ui-lang-list, .stats, .phrase-preview) > :nth-child(2) { --i: 1; }
:is(.content-pad.stack, .chapter-list, .bubbles, .options, .sound-list, .lang-list, .ui-lang-list, .stats, .phrase-preview) > :nth-child(3) { --i: 2; }
:is(.content-pad.stack, .chapter-list, .bubbles, .options, .sound-list, .lang-list, .ui-lang-list, .stats, .phrase-preview) > :nth-child(4) { --i: 3; }
:is(.content-pad.stack, .chapter-list, .bubbles, .options, .sound-list, .lang-list, .ui-lang-list, .stats, .phrase-preview) > :nth-child(5) { --i: 4; }
:is(.content-pad.stack, .chapter-list, .bubbles, .options, .sound-list, .lang-list, .ui-lang-list, .stats, .phrase-preview) > :nth-child(6) { --i: 5; }
:is(.content-pad.stack, .chapter-list, .bubbles, .options, .sound-list, .lang-list, .ui-lang-list, .stats, .phrase-preview) > :nth-child(n+7) { --i: 6; }
@keyframes item-in { from { opacity: 0; transform: translateY(12px); } }

.welcome-logo, .pitch-logo, .pick-logo { animation: logo-in var(--md-sys-motion-spring-spatial-slow-duration) var(--md-sys-motion-spring-spatial-slow) both; }
@keyframes logo-in { from { opacity: 0; transform: scale(0.85) translateY(8px); } }

/* FABs scale in once their screen has settled */
.screen-fab { animation: fab-in var(--md-sys-motion-spring-spatial-fast-duration) var(--md-sys-motion-spring-spatial-fast) 250ms both; }
@keyframes fab-in { from { opacity: 0; transform: scale(0.4); } }

/* ---------- Dialogs and sheets: enter and exit (exit classes set by ui.js) ---------- */
dialog.dialog[open]::backdrop, dialog.sheet[open]::backdrop { animation: scrim-in var(--md-sys-motion-duration-medium2) linear; }
dialog.closing::backdrop { animation: scrim-out var(--md-sys-motion-duration-short4) linear forwards; }
dialog.dialog.closing { animation: dialog-out var(--md-sys-motion-duration-short3) var(--md-sys-motion-easing-emphasized-accelerate) forwards; }
dialog.sheet.closing { animation: sheet-out var(--md-sys-motion-duration-short4) var(--md-sys-motion-easing-emphasized-accelerate) forwards; }
@keyframes scrim-in { from { opacity: 0; } }
@keyframes scrim-out { to { opacity: 0; } }
@keyframes dialog-out { to { opacity: 0; transform: scale(0.95); } }
@keyframes sheet-out { to { transform: translateY(100%); } }

/* ---------- Small state changes animate instead of snapping ---------- */
.chip, .segmented button, .card.state, .list-item, .btn, .icon-btn {
  transition: background-color var(--md-sys-motion-duration-short4) linear, color var(--md-sys-motion-duration-short4) linear,
              border-color var(--md-sys-motion-duration-short4) linear, box-shadow var(--md-sys-motion-duration-short4) var(--md-sys-motion-easing-standard),
              border-radius var(--md-sys-motion-spring-spatial-fast-duration) var(--md-sys-motion-spring-spatial-fast);
}

@media (prefers-reduced-motion: reduce) {
  ::view-transition-group(*), ::view-transition-old(*), ::view-transition-new(*) { animation: none !important; }
}
