/* Shelfmark motion. Spec: MOTION.md. Research: .agents/research/motion-2026.md.
   Transform and opacity only. Everything here is an enhancement: without View Transitions, linear() or
   scroll timelines the pages work as before. Reduced motion: page changes become a 150ms crossfade, the rest is off. */

/* ---- tokens ---- */
:root {
  --m-k: 1;                                  /* 1 normally; motion.js sets 6 with ?slowmo */
  --m-press: calc(100ms * var(--m-k));       /* press down */
  --m-fast: calc(150ms * var(--m-k));        /* exits, fades, small feedback */
  --m-base: calc(240ms * var(--m-k));        /* things arriving in place */
  --m-page: calc(300ms * var(--m-k));        /* page slide (M3 shared axis) */
  --m-spring-t: calc(400ms * var(--m-k));    /* settle time of --m-spring (perceived ~300ms) */
  --m-sheet-t: calc(480ms * var(--m-k));     /* settle time of --m-spring-sheet (perceived ~350ms) */
  --m-pop-t: calc(500ms * var(--m-k));       /* settle time of --m-spring-pop (perceived ~250ms) */
  --m-reveal: calc(420ms * var(--m-k));      /* + button grows into the add screen */
  --m-shift: 24px;                           /* page slide distance */
  --m-rise: 8px;                             /* list items settling in */

  /* easings: M3 emphasized decelerate / accelerate, standard */
  --m-out: cubic-bezier(0.05, 0.7, 0.1, 1);
  --m-in: cubic-bezier(0.3, 0, 0.8, 0.15);
  --m-std: cubic-bezier(0.2, 0, 0, 1);

  /* springs: cubic fallbacks first, linear() below where supported */
  --m-spring: var(--m-std);
  --m-spring-sheet: var(--m-out);
  --m-spring-pop: cubic-bezier(0.34, 1.4, 0.64, 1);
}
@supports (animation-timing-function: linear(0, 1)) {
  :root {
    /* duration 0.32s, bounce 0 (SwiftUI .smooth shape). Sampled every 12ms. */
    --m-spring: linear(0, 0.024, 0.083, 0.161, 0.247, 0.334, 0.418, 0.496, 0.567, 0.631, 0.687, 0.736, 0.778, 0.814, 0.845, 0.871, 0.893, 0.912, 0.927, 0.94, 0.951, 0.96, 0.967, 0.973, 0.978, 0.982, 0.985, 0.988, 0.99, 0.992, 0.994, 0.995, 0.996, 1);
    /* duration 0.42s, bounce 0.15 (SwiftUI .snappy shape) */
    --m-spring-sheet: linear(0, 0.015, 0.053, 0.107, 0.172, 0.242, 0.315, 0.388, 0.459, 0.526, 0.588, 0.646, 0.698, 0.745, 0.786, 0.823, 0.855, 0.882, 0.906, 0.926, 0.943, 0.956, 0.968, 0.977, 0.985, 0.991, 0.995, 0.999, 1.002, 1.004, 1.005, 1.006, 1.006, 1.006, 1.006, 1.006, 1.006, 1.005, 1.005, 1.004, 1);
    /* duration 0.35s, bounce 0.35 (SwiftUI .bouncy shape, used only for small confirmations) */
    --m-spring-pop: linear(0, 0.021, 0.075, 0.154, 0.246, 0.347, 0.448, 0.548, 0.641, 0.725, 0.8, 0.865, 0.92, 0.964, 0.999, 1.026, 1.045, 1.058, 1.065, 1.068, 1.067, 1.064, 1.06, 1.054, 1.047, 1.04, 1.033, 1.027, 1.021, 1.015, 1.011, 1.007, 1.003, 1.001, 0.999, 0.997, 0.996, 0.996, 0.995, 0.995, 0.996, 0.996, 1);
  }
}

@keyframes m-fade-in { from { opacity: 0; } }
@keyframes m-fade-out { to { opacity: 0; } }
@keyframes m-hold { to { opacity: 1; } }

/* ---- page to page (cross-document View Transitions; Chrome 126+, Safari 18.2+) ---- */
@view-transition { navigation: auto; }

@media (prefers-reduced-motion: no-preference) {
  /* Static names, so pairs form even when a page's first frame comes before motion.js runs.
     motion.js names the rest (the tapped row's sticker) and clears or overrides these when they shouldn't pair. */
  body:not(.page-add) .bar { view-transition-name: m-bar; }
  .fab { view-transition-name: m-fab; }
  .page-watch .d-head .sq, .page-edit .e-head .sq { view-transition-name: m-hero; }
  .page-cat .cat-key { view-transition-name: m-cat; }

  /* shared axis X: the old page leaves fast, the new one slides in and settles */
  @keyframes m-out-left { to { opacity: 0; transform: translateX(calc(var(--m-shift) * -1)); } }
  @keyframes m-out-right { to { opacity: 0; transform: translateX(var(--m-shift)); } }
  @keyframes m-in-right { 0% { opacity: 0; transform: translateX(var(--m-shift)); } 25% { opacity: 0; } 100% { opacity: 1; transform: none; } }
  @keyframes m-in-left { 0% { opacity: 0; transform: translateX(calc(var(--m-shift) * -1)); } 25% { opacity: 0; } 100% { opacity: 1; transform: none; } }

  html.vt-fwd::view-transition-old(root) { animation: m-out-left var(--m-fast) var(--m-in) both; }
  html.vt-fwd::view-transition-new(root) { animation: m-in-right var(--m-page) var(--m-out) both; }
  html.vt-back::view-transition-old(root) { animation: m-out-right var(--m-fast) var(--m-in) both; }
  html.vt-back::view-transition-new(root) { animation: m-in-left var(--m-page) var(--m-out) both; }
  html.vt-fade::view-transition-old(root) { animation: m-fade-out var(--m-fast) linear both; }
  html.vt-fade::view-transition-new(root) { animation: m-fade-in var(--m-base) linear both; }

  /* shared elements: the sticker grows into the page header, the category key into the category title */
  ::view-transition-group(m-hero), ::view-transition-group(m-cat) {
    animation-duration: var(--m-spring-t); animation-timing-function: var(--m-spring);
  }
  ::view-transition-old(m-hero), ::view-transition-old(m-cat) { animation: m-fade-out var(--m-fast) linear both; }
  ::view-transition-new(m-hero), ::view-transition-new(m-cat) { animation: none; }
  ::view-transition-old(m-hero):only-child, ::view-transition-old(m-cat):only-child { animation: m-fade-out var(--m-fast) var(--m-in) both; }
  html.vt-fwd::view-transition-new(m-hero):only-child, html.vt-fwd::view-transition-new(m-cat):only-child { animation: m-in-right var(--m-page) var(--m-out) both; }
  html.vt-back::view-transition-new(m-hero):only-child, html.vt-back::view-transition-new(m-cat):only-child { animation: m-in-left var(--m-page) var(--m-out) both; }

  /* the top bar holds still while the page under it moves */
  ::view-transition-group(m-bar) { animation-duration: var(--m-page); animation-timing-function: var(--m-std); }
  ::view-transition-old(m-bar) { animation: m-fade-out var(--m-fast) linear both; }
  ::view-transition-new(m-bar) { animation: m-fade-in var(--m-base) linear both; }

  /* + button: the add screen opens as a circle from where you tapped */
  @keyframes m-circle-open {
    from { clip-path: circle(calc(var(--fab) / 2) at var(--m-ox, calc(100% - var(--gutter) - var(--fab) / 2)) var(--m-oy, calc(100% - var(--gutter) - var(--fab) / 2))); }
    to { clip-path: circle(125vmax at var(--m-ox, calc(100% - var(--gutter) - var(--fab) / 2)) var(--m-oy, calc(100% - var(--gutter) - var(--fab) / 2))); }
  }
  @keyframes m-circle-close {
    from { clip-path: circle(125vmax at calc(100% - var(--gutter) - var(--fab) / 2) calc(100% - var(--gutter) - var(--fab) / 2)); }
    to { clip-path: circle(calc(var(--fab) / 2) at calc(100% - var(--gutter) - var(--fab) / 2) calc(100% - var(--gutter) - var(--fab) / 2)); }
  }
  @keyframes m-shrink-out { to { opacity: 0; transform: scale(0.6); } }
  @keyframes m-pop-in { 0% { opacity: 0; transform: scale(0.5); } 30% { opacity: 1; } 100% { transform: none; } }
  @keyframes m-recede { to { transform: scale(0.97); opacity: 0.6; } }

  html.vt-to-add::view-transition-old(root) { animation: m-recede var(--m-reveal) var(--m-std) both; }
  html.vt-to-add::view-transition-new(root) { animation: m-circle-open var(--m-reveal) var(--m-std) both; }
  html.vt-to-add::view-transition-old(m-fab) { animation: m-shrink-out var(--m-fast) var(--m-in) both; }
  html.vt-from-add::view-transition-old(root) { z-index: 1; animation: m-circle-close var(--m-page) var(--m-std) both; }
  html.vt-from-add::view-transition-new(root) { animation: m-hold var(--m-page) linear both; }
  html.vt-from-add::view-transition-new(m-bar) { animation: m-fade-in var(--m-fast) linear calc(var(--m-page) * 0.6) both; }
  html.vt-from-add::view-transition-new(m-fab) { animation: m-pop-in var(--m-pop-t) var(--m-spring-pop) calc(var(--m-page) * 0.6) both; }
  ::view-transition-group(m-fab) { animation-duration: var(--m-page); }

  /* same page, new state (pause, resume): parts glide to their new places, the state panel drops in */
  @keyframes m-drop-in { from { opacity: 0; transform: translateY(-8px); } }
  html.vt-same::view-transition-group(*) { animation-duration: var(--m-spring-t); animation-timing-function: var(--m-spring); }
  html.vt-same::view-transition-old(root) { animation: m-fade-out var(--m-fast) linear both; }
  html.vt-same::view-transition-new(root) { animation: m-fade-in var(--m-base) linear both; }
  html.vt-same::view-transition-new(m-dt-state):only-child { animation: m-drop-in var(--m-base) var(--m-out) var(--m-press) both; }
  html.vt-same::view-transition-old(m-dt-state):only-child { animation: m-fade-out var(--m-fast) var(--m-in) both; }
}

@media (prefers-reduced-motion: reduce) {
  ::view-transition-group(*) { animation-duration: 0s !important; }
  ::view-transition-old(*) { animation: m-fade-out 150ms linear both !important; }
  ::view-transition-new(*) { animation: m-fade-in 150ms linear both !important; }
}

/* ---- in-page ---- */
@media (prefers-reduced-motion: no-preference) {
  /* press: down fast, back with a spring. Applies to everything you tap. */
  .btn, .chip, .fab, .icon-btn, .bar-back, .step, .tray-open, .w-row, .w-card, .see-all, .dt-state.is-match {
    transition: transform var(--m-spring-t) var(--m-spring);
  }
  .btn:active:not([disabled]), .chip:active, .icon-btn:active, .bar-back:active, .step:active, .see-all:active {
    transform: scale(var(--press)); transition-duration: var(--m-press); transition-timing-function: var(--m-std);
  }
  .fab:active { transform: scale(0.92); transition-duration: var(--m-press); transition-timing-function: var(--m-std); }
  .tray-open:active, .w-row:active, .w-card:active, .dt-state.is-match:active {
    transform: scale(0.985); transition-duration: var(--m-press); transition-timing-function: var(--m-std);
  }
  .tile-face { transition: transform var(--m-spring-t) var(--m-spring); }
  .tile:active .tile-face { transform: scale(0.95); transition-duration: var(--m-press); transition-timing-function: var(--m-std); }

  /* sheets rise on a spring; the scrim fades */
  .sheet { animation: m-sheet-in var(--m-sheet-t) var(--m-spring-sheet) both; }
  @keyframes m-sheet-in { from { transform: translateY(min(100%, 60vh)); } }
  .scrim { animation: m-fade-in var(--m-base) linear both; }
  .m-ghost { pointer-events: none; }

  /* tray: replaces the old 4px bump with motion.js's own */
  .tray { transition: none; }
  .tray.bump { transform: none; }

  /* loading preview row: a slow breath, not a shimmer sweep */
  .pv-load > span { animation: m-breathe calc(1400ms * var(--m-k)) ease-in-out infinite alternate; }
  @keyframes m-breathe { from { opacity: 0.55; } to { opacity: 1; } }

  /* top bar gets its hairline once the page scrolls under it (scroll-driven, Chrome 115+, Safari 26+) */
  @supports (animation-timeline: scroll()) {
    .bar::after {
      content: ""; position: absolute; left: 0; right: 0; bottom: 0; height: var(--line);
      background: var(--color-border); opacity: 0; pointer-events: none;
      animation: m-line-on linear both; animation-timeline: scroll(root block); animation-range: 0 var(--space-7);
    }
    @keyframes m-line-on { from { opacity: 0; } to { opacity: 1; } }
  }
}

/* ---- landing: sections settle in as they enter, once, driven by scroll. No parallax. ---- */
@media (prefers-reduced-motion: no-preference) {
  @supports (animation-timeline: view()) {
    @keyframes m-reveal { from { opacity: 0; transform: translateY(var(--space-6)); } }
    html.m-on .page-landing .l-sec-head,
    html.m-on .page-landing .l-wall-wrap,
    html.m-on .page-landing .l-reads > li,
    html.m-on .page-landing .l-step,
    html.m-on .page-landing .l-end > * {
      animation: m-reveal linear both;
      animation-timeline: view();
      animation-range: entry 0% entry 70%;
    }
  }
}
