/* =============================================================
   Interactions — the small responses that make controls feel alive.

   Each one answers a user action, and nothing here moves on its own:
     - a 150ms baseline so colour, border and shadow changes ease rather
       than snap;
     - buttons lift 1px under the pointer and press to 97% when clicked;
     - clickable cards lift 2px (cards that only display data stay still);
     - one focus ring for keyboard focus;
     - menus scale and fade in from where they open;
     - the active tab's underline grows into place;
     - accordions open and close on the same 250ms curve;
     - an invalid field shakes once when a form is submitted;
     - the thin progress bar at the top (app/components/motion.js) for
       navigations and section loads that take longer than 200ms;
     - scroll reveal and count-up, both opt-in (see motion.js).

   Movement uses the individual `translate` and `scale` properties rather
   than `transform`, so it composes with any transform a page or Popper
   already sets instead of replacing it. Every selector is wrapped in
   :where(), so any page rule still wins.

   Modal open/close motion lives with the rest of the dialog rules in
   css/site.css; toasts keep their own entrance in css/app-toasts.css.

   Under prefers-reduced-motion, or the in-app preference, nothing moves:
   colour changes stay, lifts, presses, shakes and reveals do not.
   ============================================================= */

:root {
    --app-ease: cubic-bezier(.2, .8, .2, 1);
    --app-duration-fast: 150ms;
    --app-duration-base: 250ms;
    /* Movement distances, zeroed under reduced motion (end of file). */
    --app-lift: -1px;
    --app-press: .97;
    --app-card-lift: -2px;
    --app-reveal-shift: 12px;
    --app-shake: 6px;
}

/* ---- Baseline ---- */
:where(a, button, .btn, .nav-link, .dropdown-item, .page-link, .list-group-item-action,
       .form-control, .form-select, .form-check-input, input, select, textarea) {
    transition-property: color, background-color, border-color, box-shadow, opacity, translate, scale;
    transition-duration: var(--app-duration-fast);
    transition-timing-function: var(--app-ease);
}

/* ---- Buttons: hover lift, press ---- */
@media (hover: hover) {
    :where(.btn:not(.btn-link):not(.btn-close):not(:disabled):not(.disabled),
           .app-btn:not(:disabled), .doc-btn:not(:disabled), .lm-btn-dark:not(:disabled),
           .emp-btn-primary:not(:disabled), .emp-btn-outline:not(:disabled),
           .fg-dashboard-btn):hover {
        translate: 0 var(--app-lift);
    }
}

:where(.btn:not(.btn-link):not(.btn-close):not(:disabled):not(.disabled),
       .app-btn:not(:disabled), .doc-btn:not(:disabled), .lm-btn-dark:not(:disabled),
       .emp-btn-primary:not(:disabled), .emp-btn-outline:not(:disabled),
       .fg-dashboard-btn):active {
    translate: 0 0;
    scale: var(--app-press);
    transition-duration: 80ms;
}

/* ---- Clickable cards lift ---- */
@media (hover: hover) {
    :where(a.card, .card[role="button"], .card:has(> .stretched-link, .card-body > .stretched-link),
           .doc-folder-card, .fg-dashboard-card, [data-card-lift]) {
        transition-property: color, background-color, border-color, box-shadow, translate;
        transition-duration: var(--app-duration-fast);
        transition-timing-function: var(--app-ease);
    }

    :where(a.card, .card[role="button"], .card:has(> .stretched-link, .card-body > .stretched-link),
           .doc-folder-card, .fg-dashboard-card, [data-card-lift]):hover {
        translate: 0 var(--app-card-lift);
        box-shadow: var(--shadow-md);
    }
}

/* ---- One focus ring for keyboard focus ----
   Inputs already take --focus-ring on :focus (app/components/forms.css);
   this covers buttons, links and other controls reached by keyboard. */
:where(a, button, .btn, .nav-link, .dropdown-item, .page-link, [role="tab"], [tabindex]:not([tabindex="-1"])):focus-visible {
    outline: 2px solid var(--color-primary, #2563eb);
    outline-offset: 2px;
}

:where(.form-control, .form-select, textarea, select):focus-visible {
    outline: 0;
    box-shadow: var(--focus-ring);
}

/* ---- Menus ---- */
@media (prefers-reduced-motion: no-preference) {
    .dropdown-menu.show {
        animation: app-menu-in var(--app-duration-fast) var(--app-ease) both;
        transform-origin: top;
    }

    .dropup .dropdown-menu.show { transform-origin: bottom; }
}

@keyframes app-menu-in {
    from { opacity: 0; scale: .96; }
    to { opacity: 1; scale: 1; }
}

/* ---- Tabs: the active underline grows into place ---- */
:where(.nav-tabs, .nav-underline) .nav-link {
    position: relative;
}

:where(.nav-tabs, .nav-underline) .nav-link::after {
    content: "";
    position: absolute;
    left: 12%;
    right: 12%;
    bottom: -1px;
    height: 2px;
    border-radius: 2px;
    background: var(--color-primary, #2563eb);
    scale: 0 1;
    transition: scale var(--app-duration-base) var(--app-ease);
    pointer-events: none;
}

:where(.nav-tabs, .nav-underline) .nav-link.active::after {
    scale: 1 1;
}

/* ---- Accordions: one curve for open and close ---- */
.collapsing {
    transition-duration: var(--app-duration-base);
    transition-timing-function: var(--app-ease);
}

/* ---- Form errors: one shake when the field is flagged ---- */
.app-shake {
    animation: app-shake 320ms var(--app-ease);
}

@keyframes app-shake {
    0%, 100% { translate: 0 0; }
    20% { translate: calc(var(--app-shake) * -1) 0; }
    40% { translate: calc(var(--app-shake) * .8) 0; }
    60% { translate: calc(var(--app-shake) * -.5) 0; }
    80% { translate: calc(var(--app-shake) * .3) 0; }
}

/* ---- Top progress bar (driven by motion.js) ----
   A full-width bar slid in from the left with `translate`, so its glowing
   head is never stretched. While work runs it glides towards 90% on one
   long ease-out; when the work finishes it runs to the end in 300ms and
   fades. .is-reset parks it at the left edge with no transition. */
.app-progress {
    position: fixed;
    inset: 0 0 auto 0;
    z-index: 2000;
    height: 2px;
    overflow: hidden;
    pointer-events: none;
    opacity: 0;
    transition: opacity 300ms ease;
}

    .app-progress.is-active,
    .app-progress.is-done {
        opacity: 1;
    }

    .app-progress.is-done {
        opacity: 0;
        transition: opacity 300ms ease 300ms;
    }

.app-progress-bar {
    position: relative;
    height: 100%;
    width: 100%;
    border-radius: 0 2px 2px 0;
    background: linear-gradient(90deg,
        rgba(37, 99, 235, 0) 0%,
        var(--color-primary, #2563eb) 35%,
        #38bdf8 100%);
    translate: -100% 0;
}

    /* The head: a soft light at the leading edge. */
    .app-progress-bar::after {
        content: "";
        position: absolute;
        top: -3px;
        right: 0;
        width: 96px;
        height: 8px;
        border-radius: 50%;
        background: radial-gradient(closest-side, rgba(56, 189, 248, .75), rgba(56, 189, 248, 0));
        filter: blur(1px);
    }

.app-progress.is-active .app-progress-bar {
    translate: -10% 0;
    transition: translate 9s cubic-bezier(.08, .7, .15, 1);
}

.app-progress.is-done .app-progress-bar {
    translate: 0 0;
    transition: translate 300ms cubic-bezier(.2, .8, .2, 1);
}

.app-progress.is-reset .app-progress-bar {
    translate: -100% 0;
    transition: none;
}

[dir="rtl"] .app-progress { scale: -1 1; }

/* ---- Scroll reveal (opt-in, see motion.js) ----
   Hidden only once motion.js has started, so content never stays invisible
   if the script does not run. */
.app-motion-ready [data-reveal]:not(.is-revealed),
.app-motion-ready [data-reveal-stagger] > :not(.is-revealed) {
    opacity: 0;
    translate: 0 var(--app-reveal-shift);
}

.app-motion-ready :is([data-reveal], [data-reveal-stagger] > *).is-revealed {
    transition: opacity 400ms var(--app-ease), translate 400ms var(--app-ease);
    transition-delay: var(--reveal-delay, 0ms);
}

/* ---- Reduced motion: the OS setting or the in-app preference ----
   Distances drop to zero, so nothing travels; colour and shadow changes
   remain because they carry state, not motion. */
@media (prefers-reduced-motion: reduce) {
    :root {
        --app-lift: 0px;
        --app-press: 1;
        --app-card-lift: 0px;
        --app-reveal-shift: 0px;
        --app-shake: 0px;
    }

    :where(.nav-tabs, .nav-underline) .nav-link::after,
    .app-motion-ready :is([data-reveal], [data-reveal-stagger] > *).is-revealed {
        transition: none;
    }

    /* The bar still reports progress; it just jumps instead of gliding. */
    .app-progress .app-progress-bar { transition: none !important; }
}

:is([data-reduce-animations="true"], .reduce-motion) {
    --app-lift: 0px;
    --app-press: 1;
    --app-card-lift: 0px;
    --app-reveal-shift: 0px;
    --app-shake: 0px;
}

:is([data-reduce-animations="true"], .reduce-motion) :where(.nav-tabs, .nav-underline) .nav-link::after,
:is([data-reduce-animations="true"], .reduce-motion) :is(.dropdown-menu.show, .app-shake) {
    transition: none;
    animation: none;
}

:is([data-reduce-animations="true"], .reduce-motion) .app-progress .app-progress-bar {
    transition: none !important;
}
