/*
 * Storefront motion layer. Doc/25-motion-system.md is the contract; its rules
 * in short:
 *
 *  1. Content is visible without this file. Nothing rests at opacity 0.
 *  2. Only transform, opacity and filter animate. Never `transition: all`.
 *  3. Hover lives inside (hover: hover) and (pointer: fine); touch gets :active.
 *  4. prefers-reduced-motion: reduce removes movement.
 *  5. Checkout, payment and the customer area stay still.
 *  6. Everything sits behind @supports or a feature check.
 *  7. Shared rules use :where() so any theme stylesheet overrides them.
 *
 * The tokens below are the "subtle" level. The head partial overrides only
 * what another level changes, and only when motion is not reduced.
 */

:root {
    --motion-fast: 140ms;      /* colour, opacity, small icons       */
    --motion-base: 220ms;      /* hover lift, buttons, drawers       */
    --motion-slow: 420ms;      /* page content, reveal, image travel */
    --motion-page: 180ms;      /* page-to-page crossfade             */
    --motion-ease-out: cubic-bezier(.22, 1, .36, 1);    /* things arriving */
    --motion-ease-in-out: cubic-bezier(.65, 0, .35, 1); /* things moving   */
    --motion-ease-press: cubic-bezier(.3, 0, .5, 1);
    --motion-lift: -2px;
    --motion-zoom: 1.04;
    --motion-rise: 0px;
    --motion-press: .97;
    --motion-shadow-hover: 0 10px 24px -8px rgb(0 0 0 / .18);
    --header-height: 0px;
}

/* Movement is gone; colour and opacity changes stay, shortened. */
@media (prefers-reduced-motion: reduce) {
    :root {
        --motion-fast: 80ms;
        --motion-base: 80ms;
        --motion-slow: 1ms;
        --motion-page: 1ms;
        --motion-lift: 0px;
        --motion-zoom: 1;
        --motion-rise: 0px;
        --motion-press: 1;
    }
}

/* ------------------------------------------------------------------ */
/* Page-to-page transitions (cross-document View Transitions)          */
/* ------------------------------------------------------------------ */

/*
 * The head partial opts a page in with @view-transition and marks <html>
 * data-motion-pages. The header and footer get their own names so they stay
 * put while only <main> changes. Names are only given on pages that take
 * part, and only to one element each, so they stay unique.
 */
@supports (view-transition-name: none) {
    :where(:root[data-motion-pages] .gadget-header) { view-transition-name: site-header; }
    :where(:root[data-motion-pages] .gadget-footer) { view-transition-name: site-footer; }
    :where(:root[data-motion-pages] #main) { view-transition-name: site-main; }

    ::view-transition-group(*) {
        animation-duration: var(--motion-page);
        animation-timing-function: var(--motion-ease-out);
    }

    ::view-transition-old(root),
    ::view-transition-new(root) {
        animation-duration: var(--motion-page);
    }

    ::view-transition-old(site-main) {
        animation: motion-page-out var(--motion-page) var(--motion-ease-in-out) both;
    }

    ::view-transition-new(site-main) {
        animation: motion-page-in var(--motion-page) var(--motion-ease-out) both;
    }
}

/* These run on the transition's snapshots only, never on the live page. */
@keyframes motion-page-out {
    to { opacity: 0; }
}

@keyframes motion-page-in {
    from { opacity: 0; transform: translateY(var(--motion-rise)); }
}

/* ------------------------------------------------------------------ */
/* Progress bar for slow navigations                                   */
/* ------------------------------------------------------------------ */

/* Created by motion.js only when a navigation has taken 300 ms already. */
/* Its sweep eases to a stop inside four seconds; the page that replaces it
   removes it. Nothing here moves for longer than five seconds. */
:where(.motion-progress) {
    position: fixed;
    z-index: 2147483000;
    inset: 0 0 auto;
    height: 2px;
    background: var(--motion-progress-color, currentColor);
    color: #111;
    pointer-events: none;
    transform: scaleX(0);
    transform-origin: 0 50%;
    transition: transform 4s cubic-bezier(.1, .7, .2, 1);
}

:where(.motion-progress[data-running]) {
    transform: scaleX(.85);
}

@media (prefers-reduced-motion: reduce) {
    /* Still shows that something is happening, without the sweep. */
    :where(.motion-progress) { transition: none; transform: scaleX(1); opacity: .6; }
}

/* ------------------------------------------------------------------ */
/* Hover and press                                                      */
/* ------------------------------------------------------------------ */

/*
 * Hook classes any theme can put on its markup (Doc/19-theme-creation-guide.md):
 *
 *   .motion-card          a product card; lifts, its .motion-card__media img zooms
 *   .motion-tile          a category tile; lifts, its img zooms
 *   .motion-button        a text button; lifts a little
 *   .motion-icon-button   a round icon button; grows a little
 *   .motion-chip          a pill or chip; lifts a little
 *   .motion-link          an inline link; underline sweeps in
 *
 * Every rule is :where(), so a theme's own rule for the same element wins
 * without !important. Movement comes from the level tokens: at "off" and
 * under reduced motion the lift and zoom are zero and only colours change.
 */
:where(.motion-card, .motion-tile, .motion-button, .motion-icon-button, .motion-chip) {
    transition:
        transform var(--motion-base) var(--motion-ease-out),
        box-shadow var(--motion-base) var(--motion-ease-out),
        border-color var(--motion-fast) var(--motion-ease-out),
        background-color var(--motion-fast) var(--motion-ease-out),
        color var(--motion-fast) var(--motion-ease-out),
        outline-offset var(--motion-fast) var(--motion-ease-out);
}

:where(.motion-card__media img, .motion-tile img) {
    transition: transform var(--motion-slow) var(--motion-ease-out);
}

:where(.motion-link) {
    position: relative;
    text-decoration: none;
}

:where(.motion-link)::after {
    position: absolute;
    right: 0;
    bottom: -2px;
    left: 0;
    height: 1px;
    background: currentColor;
    content: '';
    transform: scaleX(0);
    transform-origin: 0 50%;
    transition: transform var(--motion-base) var(--motion-ease-out);
}

@media (hover: hover) and (pointer: fine) {
    :where(.motion-card, .motion-tile):hover {
        transform: translateY(var(--motion-lift));
        box-shadow: var(--motion-shadow-hover);
    }

    :where(.motion-card:hover .motion-card__media img, .motion-tile:hover img) {
        transform: scale(var(--motion-zoom));
    }

    :where(.motion-button, .motion-chip):hover {
        transform: translateY(calc(var(--motion-lift) / 2));
    }

    :where(.motion-icon-button):hover {
        transform: scale(calc(1 + (var(--motion-zoom) - 1) * 2));
    }

    :where(.motion-link):hover::after {
        transform: scaleX(1);
    }
}

/*
 * Press feedback, on every device: the thing you pressed gives a little.
 * Listed with the existing themes' own button classes so they get it too.
 */
:where(
    .motion-button, .motion-icon-button, .motion-chip,
    .whl-cart, .whl-buy, .studio-cart, .studio-buy, .sprint-cart, .sprint-buy,
    .nocturne-cart, .nocturne-buy, .market-add-to-cart, .market-buy,
    .apl-cart, .apl-buy, .btq-cart, .btq-buy, .gadget-button, .gadget-add-button
):active:not(:disabled) {
    transform: scale(var(--motion-press));
    transition-duration: var(--motion-fast);
    transition-timing-function: var(--motion-ease-press);
}

:where(.motion-card, .motion-tile):active {
    transform: scale(calc(1 - (1 - var(--motion-press)) / 2));
    transition-duration: var(--motion-fast);
}

/*
 * Keyboard focus: the ring the theme or browser draws eases out from the
 * edge. Only on the way in (the transition belongs to the focused state), and
 * never on the hook classes above, whose own transition lists carry it.
 */
:where(a, button, summary, input, select, textarea, [role="button"], [tabindex]):focus-visible {
    outline-offset: 3px;
}

:where(a, button, summary, input, select, textarea, [role="button"], [tabindex]):where(:not(.motion-card, .motion-tile, .motion-button, .motion-icon-button, .motion-chip, .motion-link)):focus-visible {
    transition: outline-offset var(--motion-fast) var(--motion-ease-out);
}

/*
 * Premium: a product card with a second photograph shows it on hover. The
 * head partial prints the second image only at premium; it is decorative
 * (aria-hidden) and the card's own image stays visible underneath, so this
 * is the one element that rests at opacity 0 by design.
 */
:where(.motion-card__alt) {
    position: absolute;
    inset: 0;
    background: inherit;
    opacity: 0;
    pointer-events: none;
    transition:
        opacity var(--motion-base) var(--motion-ease-out),
        transform var(--motion-slow) var(--motion-ease-out);
}

@media (hover: hover) and (pointer: fine) {
    :where(.motion-card:hover .motion-card__alt[src]) {
        opacity: 1;
    }
}

/* ------------------------------------------------------------------ */
/* Scrolling                                                            */
/* ------------------------------------------------------------------ */

/* Anchors land below the sticky header; motion.js keeps the height live. */
:root {
    scroll-padding-top: calc(var(--header-height) + 12px);
}

/* Anchor and back-to-top smoothing, set by the head partial's script. */
@media (prefers-reduced-motion: no-preference) {
    :root[data-motion-scroll="native"],
    :root[data-motion-scroll="inertia"] {
        scroll-behavior: smooth;
    }
}

/* Header elevated once the page has scrolled (a paint change only). */
:where(:root[data-motion-scrolled] .gadget-header) {
    box-shadow: var(--motion-header-shadow, 0 6px 18px -12px rgb(0 0 0 / .35));
    transition: box-shadow var(--motion-base) var(--motion-ease-out);
}

:where(:root[data-motion="premium"][data-motion-scrolled] .gadget-header) {
    box-shadow: var(--motion-header-shadow-premium, 0 1px 0 rgb(0 0 0 / .04), 0 12px 28px -14px rgb(0 0 0 / .4));
}

/* Back to top: created by motion.js after two screens of scrolling. */
:where(.motion-top) {
    position: fixed;
    z-index: 900;
    right: max(16px, env(safe-area-inset-right));
    bottom: max(16px, env(safe-area-inset-bottom));
    display: grid;
    width: 44px;
    height: 44px;
    place-items: center;
    border: 1px solid rgb(0 0 0 / .08);
    border-radius: 50%;
    background: var(--motion-top-background, #fff);
    box-shadow: 0 8px 22px -10px rgb(0 0 0 / .35);
    color: var(--motion-top-color, #111);
    cursor: pointer;
    /* It arrives by moving into place, never by fading from invisible. */
    transform: translateY(12px) scale(.9);
    transition: transform var(--motion-base) var(--motion-ease-out);
}

:where(.motion-top[hidden]) {
    display: none;
}

:where(.motion-top[data-shown]) {
    transform: none;
}

@media (hover: hover) and (pointer: fine) {
    :where(.motion-top[data-shown]:hover) {
        transform: translateY(calc(var(--motion-lift) / 2));
    }
}

/*
 * Section reveal. motion.js marks only sections that were off-screen once it
 * had started, and removes the marks when they have arrived. These selectors
 * are deliberately stronger than a theme's (unlike the rest of this file):
 * the state is temporary, and a theme's own `transition` on a card must not
 * cut the reveal short.
 */
:root[data-motion-ready] [data-motion-reveal="pending"]:not([data-motion-reveal-mode="items"]),
:root[data-motion-ready] [data-motion-reveal="pending"] [data-motion-reveal-item] {
    opacity: 0;
    transform: translateY(12px);
}

:root[data-motion-ready] [data-motion-reveal="shown"]:not([data-motion-reveal-mode="items"]),
:root[data-motion-ready] [data-motion-reveal="shown"] [data-motion-reveal-item] {
    transition:
        opacity var(--motion-slow) var(--motion-ease-out) calc(var(--motion-i, 0) * 60ms),
        transform var(--motion-slow) var(--motion-ease-out) calc(var(--motion-i, 0) * 60ms);
}

@media print {
    [data-motion-reveal],
    [data-motion-reveal-item] {
        opacity: 1 !important;
        transform: none !important;
    }
}

/* ------------------------------------------------------------------ */
/* Component transitions                                               */
/* ------------------------------------------------------------------ */

/*
 * Bagisto's drawer, modal and flash messages already slide, scale and fade
 * through Vue <transition> with Tailwind classes. They are retimed onto the
 * tokens from outside (rule 8: no upstream edit). The selectors name the
 * utility classes those components carry while a transition runs, so they
 * need more specificity than one utility class. If an upgrade changes that
 * markup, these rules simply stop matching and Bagisto's own timing returns.
 * Only while the layer runs (data-motion), so level "off" keeps Bagisto's.
 */
:root[data-motion] .fixed.z-\[1000\].transition,
:root[data-motion] .fixed.inset-0.z-20.transition-opacity,
:root[data-motion] .fixed.inset-0.z-10.transition,
:root[data-motion] .fixed.inset-0.z-10.transition-opacity,
:root[data-motion] .fixed.z-\[1001\] .transition {
    transition-duration: var(--motion-base);
    transition-timing-function: var(--motion-ease-out);
}

/*
 * Disclosures: the mobile menu and any <details> in page content slide the
 * last few pixels into place as they open. Transform only, so their content
 * is never hidden by the motion; where neither @starting-style nor
 * ::details-content exists, they open instantly as before. The departments
 * panel stays instant (see the note in menu.css).
 */
:where(.gadget-header__mobile, #main details) > :where(:not(summary)) {
    transition: transform var(--motion-base) var(--motion-ease-out);
}

:where(.gadget-header__mobile:not([open]), #main details:not([open])) > :where(:not(summary)) {
    transform: translateY(-6px);
}

@starting-style {
    :where(.gadget-header__mobile[open], #main details[open]) > :where(:not(summary)) {
        transform: translateY(-6px);
    }
}

/*
 * Counts that change in place (a cart count): mark the element with
 * data-motion-count and motion.js gives it a short bump when its text
 * changes. Transform only.
 */
:root[data-motion] [data-motion-bump] {
    animation: motion-bump var(--motion-base) var(--motion-ease-out);
}

@keyframes motion-bump {
    40% { transform: scale(calc(1 + (var(--motion-zoom) - 1) * 4)); }
}

/* Lazy images: a quiet surface behind them while they load, never hidden. */
:where(.motion-card__media, .motion-tile) {
    background-color: var(--motion-placeholder, #f2f2ef);
}
