/* Smart Apothiki — motion layer.
 *
 * Rules this file keeps:
 *  - Only opacity and transform are animated.
 *  - Entrance animations use fill-mode "backwards": once they end, no transform
 *    stays on the element, so position:fixed children (modals, drawers,
 *    dropdowns) are never trapped. <main> and layout wrappers are never animated.
 *  - Elements that contain fixed/modal children are skipped entirely.
 *  - prefers-reduced-motion: everything becomes instant (bottom of the file).
 */

:root {
    --m-ease-out: cubic-bezier(0.22, 1, 0.36, 1);
    --m-ease-in-out: cubic-bezier(0.65, 0, 0.35, 1);
    --m-fast: 150ms;
    --m-base: 280ms;
    --m-step: 30ms;
}

/* ---------- Page entrance ------------------------------------------------ */

@keyframes m-rise {
    from { opacity: 0; transform: translateY(8px); }
    to   { opacity: 1; transform: none; }
}

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

/* Leaf cards and stand-alone tables inside the page content. Nested cards are
   left alone (their parent already moves). Cards holding fixed/modal children
   are skipped. Total: 280ms + up to 4 x 30ms delay = 400ms. */
main .card:not(.card .card):not(:has(.fixed, [class*="modal"], [x-data*="sidebar"])),
main .table-wrap:not(.card .table-wrap):not(:has(.fixed, [class*="modal"])) {
    animation: m-rise var(--m-base) var(--m-ease-out) backwards;
}

/* Stagger by position among siblings (grids of KPI tiles, stacked cards). */
main :is(.card, .table-wrap):nth-child(2) { animation-delay: calc(var(--m-step) * 1); }
main :is(.card, .table-wrap):nth-child(3) { animation-delay: calc(var(--m-step) * 2); }
main :is(.card, .table-wrap):nth-child(4) { animation-delay: calc(var(--m-step) * 3); }
main :is(.card, .table-wrap):nth-child(n+5) { animation-delay: calc(var(--m-step) * 4); }

/* ---------- Micro-interactions ------------------------------------------ */

/* Cards: smooth shadow/border changes everywhere; lift only on devices with a
   real pointer, and only for clickable cards and small KPI tiles (not forms or
   tables, where a moving surface under the cursor is distracting).
   "main .card" outranks Tailwind's transition-colors utility on link cards. */
main .card {
    transition: box-shadow var(--m-base) var(--m-ease-out),
                border-color var(--m-fast) ease,
                transform var(--m-base) var(--m-ease-out);
}

@media (hover: hover) and (pointer: fine) {
    a.card:hover,
    main .grid > .card.card-pad:not(:has(form, table, input, select, textarea, .fixed, [class*="modal"])):hover {
        transform: translateY(-2px);
        box-shadow: 0 10px 24px -12px rgba(7, 27, 102, 0.22), 0 2px 6px -2px rgba(15, 23, 42, 0.08);
    }
}

/* Buttons: colour fades plus a small press. */
body .btn,
body button[type="submit"] {
    transition: background-color var(--m-fast) ease, border-color var(--m-fast) ease,
                color var(--m-fast) ease, box-shadow var(--m-fast) ease,
                opacity var(--m-fast) ease, transform 120ms var(--m-ease-out);
}
.btn:active:not(:disabled),
button[type="submit"]:active:not(:disabled) {
    transform: scale(0.97);
}

/* Sidebar links: colour fades, icon nudges on hover. */
body .nav-link {
    transition: background-color 180ms ease, color 180ms ease;
}
.nav-link i {
    transition: color 180ms ease, transform 180ms var(--m-ease-out);
}
@media (hover: hover) {
    .nav-link:hover i { transform: translateX(2px); }
}

/* Table rows: soft background fade (colour only, no movement). */
.table tbody tr,
table tbody tr {
    transition: background-color 150ms ease;
}

/* Focus rings stay visible for keyboard users. Buttons keep their own ring. */
a:focus-visible,
.nav-link:focus-visible,
summary:focus-visible {
    outline: 2px solid rgb(var(--sa-brand));
    outline-offset: 2px;
    border-radius: 0.5rem;
}

/* ---------- Mobile sidebar drawer --------------------------------------- */

/* The Alpine toggle swaps translate-x-0 / -translate-x-full on the <aside>;
   these rules only change the timing curve and add depth while it is open. */
[x-data] > aside.fixed {
    transition: transform 280ms var(--m-ease-out), box-shadow 280ms ease;
    will-change: transform;
}
@media (max-width: 1023.98px) {
    [x-data] > aside.fixed.translate-x-0 {
        box-shadow: 0 20px 48px -8px rgba(6, 19, 51, 0.35);
    }
    /* Links slide in a touch after the panel when it opens. */
    [x-data] > aside.fixed.translate-x-0 .nav-link {
        animation: m-drawer-link 260ms var(--m-ease-out) backwards;
        animation-delay: 60ms;
    }
}
@keyframes m-drawer-link {
    from { opacity: 0; transform: translateX(-6px); }
    to   { opacity: 1; transform: none; }
}

/* Backdrop: the Alpine x-transition.opacity drives it; slow it a little. */
[x-data] > div.fixed.inset-0[x-show] {
    transition-duration: 250ms !important;
    transition-timing-function: ease !important;
}

/* ---------- Flash messages ---------------------------------------------- */

@keyframes m-flash-in {
    from { opacity: 0; transform: translateY(-6px); }
    to   { opacity: 1; transform: none; }
}
[data-flash] {
    position: relative;
    animation: m-flash-in 300ms var(--m-ease-out) backwards;
}
[data-flash]:nth-child(2) { animation-delay: 60ms; }
[data-flash]:nth-child(n+3) { animation-delay: 120ms; }
[data-flash].m-has-close { padding-right: 2.5rem; }
[data-flash].m-leaving {
    opacity: 0;
    transform: translateY(-4px);
    transition: opacity 180ms ease, transform 180ms ease;
}
.m-flash-close {
    position: absolute;
    top: 50%;
    right: 0.5rem;
    width: 1.75rem;
    height: 1.75rem;
    margin-top: -0.875rem;
    display: inline-flex;
    align-items: center;
    justify-content: center;
    border-radius: 0.375rem;
    color: currentColor;
    opacity: 0.6;
    background: transparent;
    border: 0;
    cursor: pointer;
    transition: opacity 150ms ease, background-color 150ms ease;
}
.m-flash-close:hover { opacity: 1; background-color: rgba(15, 23, 42, 0.06); }
.m-flash-close:focus-visible { opacity: 1; outline: 2px solid currentColor; outline-offset: 1px; }

/* ---------- Login page --------------------------------------------------- */

@keyframes m-gradient-pan {
    0%   { background-position: 0% 0%; }
    50%  { background-position: 100% 100%; }
    100% { background-position: 0% 0%; }
}
/* Slow colour drift on the dark brand panel (background-position on a single
   layer is cheap; the panel has no children that depend on it). */
.m-login-panel {
    background-size: 160% 160%;
    animation: m-gradient-pan 18s var(--m-ease-in-out) infinite;
}

@keyframes m-float-a {
    0%, 100% { transform: translate3d(0, 0, 0) scale(1); }
    50%      { transform: translate3d(-24px, 18px, 0) scale(1.05); }
}
@keyframes m-float-b {
    0%, 100% { transform: translate3d(0, 0, 0) scale(1); }
    50%      { transform: translate3d(20px, -16px, 0) scale(0.96); }
}
.m-float-a { animation: m-float-a 14s ease-in-out infinite; will-change: transform; }
.m-float-b { animation: m-float-b 17s ease-in-out infinite; will-change: transform; }
.m-glow {
    position: absolute;
    border-radius: 9999px;
    pointer-events: none;
    filter: blur(48px);
    opacity: 0.45;
}

.m-fade-in { animation: m-fade 500ms ease-out 80ms backwards; }
.m-rise-in { animation: m-rise 420ms var(--m-ease-out) backwards; }
.m-rise-in-2 { animation: m-rise 420ms var(--m-ease-out) 120ms backwards; }
.m-rise-in-3 { animation: m-rise 420ms var(--m-ease-out) 200ms backwards; }

/* ---------- Reduced motion ---------------------------------------------- */

@media (prefers-reduced-motion: reduce) {
    *,
    *::before,
    *::after {
        animation-duration: 0.01ms !important;
        animation-delay: 0ms !important;
        animation-iteration-count: 1 !important;
        transition-duration: 0.01ms !important;
        transition-delay: 0ms !important;
        scroll-behavior: auto !important;
    }
    .m-login-panel,
    .m-float-a,
    .m-float-b { animation: none !important; }
    a.card:hover,
    main .grid > .card.card-pad:hover,
    .btn:active,
    button[type="submit"]:active,
    .nav-link:hover i { transform: none !important; }
}
