/* ──────────────────────────────────────────────────────────────────────────
   Loading indicators.

   Two distinct things, because they answer different questions:

   1. .mk-progress  — a slim top bar for navigation. Non-blocking: the page
      stays visible and interactive. Used when the user clicks a link and we
      do not know how long the next page will take.

   2. .mk-overlay   — a blocking veil for an operation the user explicitly
      started and must wait for (generate, upload, merge). Only shown when the
      UI genuinely cannot continue.

   What this replaces: a full-screen opaque panel shown on every page load and
   every link click. That hid content which had already rendered, made the
   loader itself the Largest Contentful Paint, and had no failsafe — a failed
   navigation left the user staring at a veil with no way out.
   ────────────────────────────────────────────────────────────────────────── */

/* ---------- 1. Top progress bar ---------- */

.mk-progress {
    position: fixed;
    inset-block-start: 0;
    inset-inline-start: 0;
    width: 100%;
    height: 3px;
    z-index: 10050;
    pointer-events: none;
    opacity: 0;
    transition: opacity .2s ease;
}

.mk-progress[data-active="true"] {
    opacity: 1;
}

.mk-progress__bar {
    height: 100%;
    width: 100%;
    transform: scaleX(0);
    transform-origin: left center;
    background: linear-gradient(90deg, var(--bs-primary, #2563eb), #60a5fa);
    /* transform + opacity only, so the bar is composited and costs the main
       thread nothing while a page is busy doing real work. */
    transition: transform .25s cubic-bezier(.4, 0, .2, 1);
    will-change: transform;
}

/* RTL: grow from the right edge, matching reading direction. */
[dir="rtl"] .mk-progress__bar {
    transform-origin: right center;
}

/* ---------- 2. Blocking overlay ---------- */

.mk-overlay {
    position: fixed;
    inset: 0;
    z-index: 10040;
    display: flex;
    align-items: center;
    justify-content: center;
    background: color-mix(in srgb, var(--bs-body-bg, #0b0b10) 55%, transparent);
    backdrop-filter: blur(6px) saturate(1.1);
    -webkit-backdrop-filter: blur(6px) saturate(1.1);
    opacity: 0;
    visibility: hidden;
    transition: opacity .2s ease, visibility .2s ease;
}

.mk-overlay[data-active="true"] {
    opacity: 1;
    visibility: visible;
}

/* Glass card with an accent glow; same shape language as the dashboard cards. */
.mk-overlay__card {
    position: relative;
    display: flex;
    flex-direction: column;
    align-items: center;
    gap: 14px;
    min-width: 240px;
    max-width: min(90vw, 340px);
    padding: 28px 36px 24px;
    border-radius: 20px;
    border: 1px solid rgba(var(--mk-accent-rgb, 71, 70, 180), .25);
    background: color-mix(in srgb, var(--bs-body-bg, #111) 88%, transparent);
    color: rgb(var(--bs-dark-rgb, 230, 236, 255));
    box-shadow:
        0 24px 60px rgba(0, 0, 0, .28),
        0 0 0 8px rgba(var(--mk-accent-rgb, 71, 70, 180), .05);
    text-align: center;
    overflow: hidden;
    transform: translateY(8px) scale(.97);
    transition: transform .22s cubic-bezier(.2, .8, .2, 1);
}

/* Indeterminate sweep along the top edge of the card. */
.mk-overlay__card::before {
    content: "";
    position: absolute;
    inset-inline: 0;
    top: 0;
    height: 2px;
    background: linear-gradient(90deg, transparent, rgb(var(--mk-accent-rgb, 71, 70, 180)), #a78bfa, transparent);
    background-size: 45% 100%;
    background-repeat: no-repeat;
    animation: mk-sweep 1.4s cubic-bezier(.45, 0, .55, 1) infinite;
}

.mk-overlay[data-active="true"] .mk-overlay__card {
    transform: none;
}

/* Equalizer orb: the product is audio, so the wait looks like audio. */
.mk-overlay__spinner {
    position: relative;
    width: 52px;
    height: 52px;
    border-radius: 50%;
    display: flex;
    align-items: center;
    justify-content: center;
    gap: 3px;
    background: linear-gradient(135deg, rgb(var(--mk-accent-rgb, 71, 70, 180)), #a78bfa);
    box-shadow: 0 0 28px rgba(var(--mk-accent-rgb, 71, 70, 180), .55);
}

.mk-overlay__spinner::after {
    content: "";
    position: absolute;
    inset: -6px;
    border-radius: 50%;
    border: 2px solid rgba(var(--mk-accent-rgb, 71, 70, 180), .45);
    animation: mk-ping 1.6s ease-out infinite;
}

.mk-overlay__spinner i {
    display: block;
    width: 3px;
    height: 18px;
    border-radius: 2px;
    background: #fff;
    animation: mk-eq 1s ease-in-out infinite;
}

.mk-overlay__spinner i:nth-child(2) { animation-delay: -.8s; }
.mk-overlay__spinner i:nth-child(3) { animation-delay: -.6s; }
.mk-overlay__spinner i:nth-child(4) { animation-delay: -.4s; }
.mk-overlay__spinner i:nth-child(5) { animation-delay: -.2s; }

.mk-overlay__text {
    margin: 4px 0 0;
    font-size: .98rem;
    font-weight: 700;
    line-height: 1.5;
    color: inherit;
}

/* The "still working" note only appears once an operation runs long. */
.mk-overlay__note {
    margin: 0;
    font-size: .8rem;
    color: rgba(var(--bs-dark-rgb, 230, 236, 255), .55);
    opacity: 0;
    max-height: 0;
    transition: opacity .3s ease, max-height .3s ease;
}

.mk-overlay__note[data-visible="true"] {
    opacity: 1;
    max-height: 3em;
}

@keyframes mk-spin {
    to { transform: rotate(360deg); }
}

@keyframes mk-eq {
    0%, 100% { transform: scaleY(.3); }
    50%      { transform: scaleY(1); }
}

@keyframes mk-ping {
    from { transform: scale(.85); opacity: 1; }
    to   { transform: scale(1.3); opacity: 0; }
}

@keyframes mk-sweep {
    from { background-position: -60% 0; }
    to   { background-position: 160% 0; }
}

/* ---------- Accessibility ---------- */

/* Honour the OS setting: no spin, no slide — the indicator still appears, it
   just does not move. */
@media (prefers-reduced-motion: reduce) {
    .mk-overlay__spinner::after,
    .mk-overlay__spinner i,
    .mk-overlay__card::before { animation: none; }
    .mk-progress__bar,
    .mk-overlay,
    .mk-overlay__card { transition: none; }
}

/* ---------- 3. Page-to-page transition ---------- */
/*
   The progress bar says "something is loading", but the swap itself was still
   an abrupt cut: the old page vanished and the new one appeared fully formed.

   Two mechanisms, because browser support splits:

   a) Cross-document View Transitions (Chrome 126+, Safari 18.2+). The browser
      snapshots the outgoing page, waits for the new document, and cross-fades
      between them. Nothing to wire up in JS — both pages just opt in.

   b) Everywhere else, an enter animation on the content container, started by
      loading.js on DOMContentLoaded.

   Deliberately NOT animated:

   - opacity from 0. An element at opacity 0 is not a Largest Contentful Paint
     candidate, so fading in from nothing pushes LCP out by the full duration
     of the animation. Both paths start at 0.6, which paints and so counts.
   - transform on <body> or <html>. A transformed ancestor becomes the
     containing block for position:fixed descendants, which would drag the
     sticky nav and any open modal out of place mid-animation.

   Durations are short on purpose: this is feedback, not choreography.
*/

@view-transition {
    navigation: auto;
}

::view-transition-old(root),
::view-transition-new(root) {
    /* The default is a 250ms cross-fade of the whole snapshot. Shorter, and
       opacity-only, so it stays on the compositor. */
    animation-duration: .18s;
    animation-timing-function: cubic-bezier(.4, 0, .2, 1);
}

/* Fallback path. .mk-anim is put on <html> by the inline script in
   theme-boot.html, before the first paint — loading.js is deferred and so runs
   too late to start an entrance.

   No animation-fill-mode: the end keyframe is the element's normal state
   anyway, so letting the animation release the element afterwards avoids
   keeping a compositor layer alive for the life of the page. */
.mk-anim main.app-content,
.mk-anim #mk-page {
    animation: mk-page-in .22s cubic-bezier(.4, 0, .2, 1);
}

@keyframes mk-page-in {
    from {
        opacity: .6;
        transform: translateY(6px);
    }
    to {
        opacity: 1;
        transform: none;
    }
}

/* Outgoing cue for browsers without view transitions: dim the content the
   moment a link is clicked, so the click registers visually even if the server
   takes a beat. Reverted on pageshow (including bfcache restores). */
.mk-leaving .mk-page-target {
    opacity: .72;
    transition: opacity .15s ease-out;
}

@media (prefers-reduced-motion: reduce) {
    ::view-transition-old(root),
    ::view-transition-new(root) {
        animation: none;
    }

    .mk-anim main.app-content,
    .mk-anim #mk-page {
        animation: none;
    }

    .mk-leaving .mk-page-target {
        opacity: 1;
        transition: none;
    }
}

/* ---------- 4. Theme swap ---------- */
/*
   theme.js adds .theme-transition to <html> for 300ms around a light/dark
   swap. It had no matching rule, so the class did nothing: every themed
   element animated its own colour on its own timing and the switch smeared.

   Suppressing transitions outright is the right call here — the swap should
   read as instant, and transitioning hundreds of colour properties at once is
   what made it look slow in the first place.
*/
.theme-transition,
.theme-transition *,
.theme-transition *::before,
.theme-transition *::after {
    transition: none !important;
}

/* ---------- 4. Downloads (loading.js "downloads") ----------
   The control that started a download shows a spinner and ignores clicks, and a
   toast says what is happening until the file actually starts. Tokens fall back
   to plain colours on pages without theme-v2. */

.mk-dl-busy {
    position: relative;
    pointer-events: none;
    cursor: progress;
    opacity: .75;
}
.mk-dl-busy::after {
    content: "";
    display: inline-block;
    width: 13px;
    height: 13px;
    margin-inline-start: 7px;
    vertical-align: -2px;
    border-radius: 50%;
    border: 2px solid currentColor;
    border-inline-end-color: transparent;
    animation: mk-dl-spin .7s linear infinite;
}
@keyframes mk-dl-spin { to { transform: rotate(360deg); } }

.mk-dl-toast {
    position: fixed;
    inset-inline-start: 50%;
    inset-block-end: 24px;
    z-index: 10060;
    display: flex;
    align-items: center;
    gap: 10px;
    max-width: calc(100vw - 32px);
    padding: 11px 16px;
    border-radius: 12px;
    background: var(--mk-text, #111);
    color: var(--mk-card, #fff);
    box-shadow: 0 14px 36px rgba(0, 0, 0, .28);
    font-size: 14px;
    font-weight: 600;
    line-height: 1.35;
    transform: translate(-50%, 12px);
    opacity: 0;
    pointer-events: none;
    transition: opacity .18s ease, transform .18s ease;
}
[dir="rtl"] .mk-dl-toast { transform: translate(50%, 12px); }
.mk-dl-toast[data-visible="true"] { opacity: 1; transform: translate(-50%, 0); }
[dir="rtl"] .mk-dl-toast[data-visible="true"] { transform: translate(50%, 0); }
.mk-dl-toast__ico {
    flex: none;
    width: 16px;
    height: 16px;
    border-radius: 50%;
}
.mk-dl-toast[data-state="busy"] .mk-dl-toast__ico {
    border: 2px solid currentColor;
    border-inline-end-color: transparent;
    animation: mk-dl-spin .7s linear infinite;
}
.mk-dl-toast[data-state="done"] .mk-dl-toast__ico {
    background: #22c55e;
    -webkit-mask: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 24 24' fill='none' stroke='black' stroke-width='3' stroke-linecap='round' stroke-linejoin='round'%3E%3Cpath d='M5 12.5l4.5 4.5L19 7'/%3E%3C/svg%3E") center / contain no-repeat;
            mask: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 24 24' fill='none' stroke='black' stroke-width='3' stroke-linecap='round' stroke-linejoin='round'%3E%3Cpath d='M5 12.5l4.5 4.5L19 7'/%3E%3C/svg%3E") center / contain no-repeat;
}
.mk-dl-toast[data-state="error"] .mk-dl-toast__ico {
    background: #f87171;
    -webkit-mask: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 24 24' fill='none' stroke='black' stroke-width='2.6' stroke-linecap='round'%3E%3Ccircle cx='12' cy='12' r='9'/%3E%3Cpath d='M12 7.5v5.5M12 16.5h.01'/%3E%3C/svg%3E") center / contain no-repeat;
            mask: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 24 24' fill='none' stroke='black' stroke-width='2.6' stroke-linecap='round'%3E%3Ccircle cx='12' cy='12' r='9'/%3E%3Cpath d='M12 7.5v5.5M12 16.5h.01'/%3E%3C/svg%3E") center / contain no-repeat;
}
/* clear the dashboard's mobile bottom nav */
@media (max-width: 991.98px) { body:has(.mobile-bottom-nav) .mk-dl-toast { inset-block-end: 84px; } }
@media (prefers-reduced-motion: reduce) {
    .mk-dl-busy::after, .mk-dl-toast[data-state="busy"] .mk-dl-toast__ico { animation-duration: 2s; }
}
