/*
 * Motion, the page-change loader and modals. Loaded last on every page.
 * Uses the separate translate and scale properties so it adds to any transform an element already has.
 */

:root {
    --kl-ease: cubic-bezier(0.22, 1, 0.36, 1);
    --kl-nav-bg: rgba(243, 244, 249, 0.9);
    --kl-nav-ink: #141a33;
    --kl-modal-bg: #fff;
    --kl-modal-ink: #141a33;
    --kl-modal-soft: #5c6485;
    --kl-modal-line: #e2e4ef;
    --kl-modal-accent: #2f45e0;
    --kl-modal-backdrop: rgba(20, 26, 51, 0.5);
}

html.dark,
html[data-theme="dark"] {
    --kl-nav-bg: rgba(12, 15, 32, 0.9);
    --kl-nav-ink: #f4f5fb;
    --kl-modal-bg: #171b33;
    --kl-modal-ink: #f4f5fb;
    --kl-modal-soft: #a4abc6;
    --kl-modal-line: rgba(255, 255, 255, 0.12);
    --kl-modal-accent: #8b9aff;
    --kl-modal-backdrop: rgba(4, 6, 16, 0.68);
}

/* ------------------------------------------------------- page-change loader */

.kl-nav {
    position: fixed;
    inset: 0;
    z-index: 2147483000;
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    gap: 0.75rem;
    background: var(--kl-nav-bg);
    -webkit-backdrop-filter: blur(8px);
    backdrop-filter: blur(8px);
    opacity: 0;
    visibility: hidden;
    pointer-events: none;
    transition: opacity 220ms ease, visibility 220ms;
}

.kl-nav.is-on,
html.kl-arriving .kl-nav {
    opacity: 1;
    visibility: visible;
    pointer-events: auto;
}

/* Never stay over a page whose script failed to load. */
html.kl-arriving .kl-nav {
    animation: kl-nav-giveup 220ms ease 5s forwards;
}

.kl-nav__mark {
    width: 4.5rem;
    height: 4.5rem;
}

.kl-nav__stem,
.kl-nav__message {
    fill: var(--kl-modal-accent);
}

.kl-nav__reply {
    fill: #f5a524;
}

.kl-nav__message,
.kl-nav__reply {
    transform-origin: 21.5px 32px;
    animation: kl-nav-message 1.3s var(--kl-ease) infinite both;
}

.kl-nav__reply {
    animation-name: kl-nav-reply;
}

.kl-nav__word {
    font-family: 'Lexend', system-ui, sans-serif;
    font-size: 1.25rem;
    font-weight: 600;
    color: var(--kl-nav-ink);
    direction: ltr;
}

@keyframes kl-nav-message {
    0% { transform: scale(0); }
    25%, 85% { transform: scale(1); }
    100% { transform: scale(0); }
}

@keyframes kl-nav-reply {
    0%, 30% { transform: scale(0); }
    55%, 85% { transform: scale(1); }
    100% { transform: scale(0); }
}

@keyframes kl-nav-giveup {
    to { opacity: 0; visibility: hidden; pointer-events: none; }
}

/* ------------------------------------------------------------------- modals */

/* Every dialog on the system fades and scales in and out. */
dialog {
    transition:
        opacity 200ms ease,
        scale 220ms var(--kl-ease),
        overlay 220ms allow-discrete,
        display 220ms allow-discrete;
    opacity: 0;
    scale: 0.96;
}

dialog[open] {
    opacity: 1;
    scale: 1;
}

@starting-style {
    dialog[open] {
        opacity: 0;
        scale: 0.96;
    }
}

dialog::backdrop {
    transition:
        background-color 200ms ease,
        overlay 220ms allow-discrete,
        display 220ms allow-discrete;
    background-color: rgba(0, 0, 0, 0);
}

dialog[open]::backdrop {
    background-color: var(--kl-modal-backdrop);
}

@starting-style {
    dialog[open]::backdrop {
        background-color: rgba(0, 0, 0, 0);
    }
}

/* The older modals are full-screen divs switched on and off. */
.fixed.inset-0[role="dialog"]:not(.hidden):not([hidden]) {
    animation: kl-fade 180ms ease;
}

.fixed.inset-0[role="dialog"]:not(.hidden):not([hidden]) > :not(.absolute):not(.fixed) {
    animation: kl-pop 220ms var(--kl-ease);
}

.kl-modal {
    width: min(26rem, calc(100vw - 2rem));
    max-width: none;
    max-height: calc(100dvh - 2rem);
    padding: 0;
    overflow: visible;
    border: 0;
    border-radius: 18px;
    background: transparent;
    color: var(--kl-modal-ink);
    font: inherit;
}

.kl-modal--lg {
    width: min(46rem, calc(100vw - 2rem));
}

.kl-modal--xl {
    width: min(68rem, calc(100vw - 2rem));
}

.kl-modal__card {
    display: flex;
    flex-direction: column;
    max-height: calc(100dvh - 2rem);
    overflow: hidden;
    border: 1px solid var(--kl-modal-line);
    border-radius: 18px;
    background: var(--kl-modal-bg);
    box-shadow: 0 24px 60px rgba(10, 14, 34, 0.32);
}

.kl-modal__head {
    display: flex;
    align-items: center;
    justify-content: space-between;
    gap: 1rem;
    padding: 1rem 1.25rem;
    border-bottom: 1px solid var(--kl-modal-line);
}

.kl-modal__title {
    margin: 0;
    font-size: 1rem;
    font-weight: 600;
    line-height: 1.3;
}

.kl-modal__close {
    display: inline-flex;
    flex: none;
    align-items: center;
    justify-content: center;
    width: 2rem;
    height: 2rem;
    padding: 0;
    border: 0;
    border-radius: 9px;
    background: transparent;
    color: var(--kl-modal-soft);
    cursor: pointer;
}

.kl-modal__close:hover {
    background: rgba(127, 134, 170, 0.14);
    color: var(--kl-modal-ink);
}

.kl-modal__close svg {
    display: block;
    width: 1.125rem;
    height: 1.125rem;
}

.kl-modal__body {
    min-height: 0;
    padding: 1.25rem;
    overflow-y: auto;
    scrollbar-width: thin;
}

.kl-modal__body pre {
    margin: 0;
    font: inherit;
    font-size: 0.875rem;
    line-height: 1.7;
    white-space: pre-wrap;
}

.kl-confirm__text {
    margin: 0;
    font-size: 0.9375rem;
    line-height: 1.6;
    white-space: pre-line;
}

.kl-confirm__actions {
    display: flex;
    justify-content: flex-end;
    gap: 0.5rem;
    margin-top: 1.25rem;
}

.kl-mbtn {
    min-height: 2.5rem;
    padding: 0 1.125rem;
    border: 1px solid transparent;
    border-radius: 10px;
    background: var(--kl-modal-accent);
    color: #fff;
    font: inherit;
    font-size: 0.875rem;
    font-weight: 600;
    cursor: pointer;
}

html.dark .kl-mbtn,
html[data-theme="dark"] .kl-mbtn {
    color: #0c0f20;
}

.kl-mbtn--ghost,
html.dark .kl-mbtn--ghost,
html[data-theme="dark"] .kl-mbtn--ghost {
    border-color: var(--kl-modal-line);
    background: transparent;
    color: var(--kl-modal-ink);
}

.kl-confirm.is-danger .kl-mbtn:not(.kl-mbtn--ghost) {
    background: #c81e1e;
    color: #fff;
}

@media (max-width: 760px) {
    .kl-modal__body .sa-filters {
        display: flex !important;
        flex-direction: column;
        flex-wrap: nowrap;
        gap: 0.5rem;
    }

    .kl-modal__body .sa-filters :is(.sa-input, .sa-select) {
        width: 100%;
        max-width: none;
        flex: none;
    }

    .kl-modal__body .sa-daterange {
        display: grid;
        grid-template-columns: repeat(2, minmax(0, 1fr));
        grid-column: auto;
    }

    .kl-modal__body .sa-filters .sa-dates input {
        min-width: 0;
        padding-inline: 0.5rem;
    }
}

.kl-remote-busy {
    opacity: 0.6;
    pointer-events: none;
}

.kl-modal-open {
    display: inline-flex;
    align-items: center;
    gap: 0.375rem;
}

.kl-modal-open svg {
    width: 1rem;
    height: 1rem;
}

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

@media (prefers-reduced-motion: no-preference) {
    /* Anything you press. */
    :is(
        button,
        input[type="submit"],
        input[type="button"],
        [role="button"],
        summary.sa-btn,
        .sa-btn,
        .sa-iconbtn,
        .kaman-button,
        [class*="-btn"],
        a[class*="rounded"][class*="bg-"],
        a.sa-kpi,
        article.kaman-card
    ) {
        transition-property: translate, scale, box-shadow, background-color, border-color, color, opacity !important;
        transition-duration: 170ms !important;
        transition-timing-function: var(--kl-ease) !important;
    }

    @media (hover: hover) {
        :is(
            button,
            input[type="submit"],
            input[type="button"],
            [role="button"],
            summary.sa-btn,
            .sa-btn,
            .sa-iconbtn,
            .kaman-button,
            [class*="-btn"],
            a[class*="rounded"][class*="bg-"]
        ):not(:disabled, [aria-disabled="true"]):hover {
            translate: 0 -1px;
            box-shadow: 0 6px 16px -8px rgba(20, 26, 51, 0.45);
        }

        a.sa-kpi:hover,
        article.kaman-card:hover {
            translate: 0 -2px;
            box-shadow: 0 12px 28px -18px rgba(20, 26, 51, 0.5);
        }
    }

    :is(
        button,
        input[type="submit"],
        input[type="button"],
        [role="button"],
        summary.sa-btn,
        .sa-btn,
        .sa-iconbtn,
        .kaman-button,
        [class*="-btn"],
        a[class*="rounded"][class*="bg-"],
        a.sa-kpi,
        .sa-tab,
        .sa-seg a,
        .sa-custom-btn,
        .sa-chat,
        .kl-rail__link
    ):not(:disabled, [aria-disabled="true"]):active {
        translate: 0 0;
        scale: 0.97;
        box-shadow: none;
        transition-duration: 90ms !important;
    }

    /* Navigation and rows. */
    .kl-rail__link,
    .sa-tab,
    .sa-seg a,
    .sa-custom-btn {
        transition: background-color 170ms ease, color 170ms ease, scale 120ms var(--kl-ease) !important;
    }

    .kl-rail__link svg,
    .sa-tab svg {
        transition: scale 200ms var(--kl-ease);
    }

    .kl-rail__link:hover svg,
    .sa-tab:hover svg {
        scale: 1.12;
    }

    .sa-tab.is-on::after {
        animation: kl-tab-line 240ms var(--kl-ease);
    }

    table tbody tr {
        transition: background-color 150ms ease;
    }

    .sa-table--rows tbody tr:hover {
        background: rgba(127, 134, 170, 0.08);
    }

    :is(.sa-input, .sa-select, .kaman-input) {
        transition: border-color 170ms ease, box-shadow 170ms ease;
    }

    :is(.sa-input, .sa-select, .kaman-input):focus {
        box-shadow: 0 0 0 3px rgba(47, 69, 224, 0.18);
    }

    /* Pages arriving. */
    main > *,
    .sa-main > * {
        animation: kl-rise 260ms var(--kl-ease) backwards;
    }

    main > :nth-child(2),
    .sa-main > :nth-child(2) { animation-delay: 30ms; }

    main > :nth-child(3),
    .sa-main > :nth-child(3) { animation-delay: 60ms; }

    main > :nth-child(n + 4),
    .sa-main > :nth-child(n + 4) { animation-delay: 90ms; }

    details[open] > :not(summary) {
        animation: kl-rise 200ms var(--kl-ease);
    }

    .sa-chart__col i {
        transform-origin: bottom;
        animation: kl-bar 320ms var(--kl-ease) backwards;
    }

    .sa-msg {
        animation: kl-rise 200ms var(--kl-ease) backwards;
    }

    .sa-chat {
        transition: background-color 150ms ease, scale 120ms var(--kl-ease) !important;
    }

    @keyframes kl-rise {
        from { opacity: 0; translate: 0 6px; }
    }

    @keyframes kl-fade {
        from { opacity: 0; }
    }

    @keyframes kl-pop {
        from { opacity: 0; scale: 0.96; }
    }

    @keyframes kl-bar {
        from { scale: 1 0; }
    }

    @keyframes kl-tab-line {
        from { scale: 0 1; }
    }
}

@media (prefers-reduced-motion: reduce) {
    dialog,
    dialog::backdrop {
        transition: none;
    }

    .kl-nav {
        transition: none;
    }

    .kl-nav__message,
    .kl-nav__reply {
        animation: none;
    }
}
