/* ==========================================================================
   Radiology loading animations
   --------------------------------------------------------------------------
   Reusable, CSS-only, radiology-themed loading indicators.
   Loaded globally from templates/base.html.

   Usage (see generic/templates/generic/partials/_loader.html):
       <div class="rad-loader rad-loader--ct rad-loader--lg" role="status">
           <span class="rad-loader__stage rad-loader__stage--ct" aria-hidden="true">
               <span class="rad-loader__gantry"></span>
               <span class="rad-loader__bore"></span>
               <span class="rad-loader__couch"></span>
           </span>
           <span class="rad-loader__label">Acquiring…</span>
       </div>

   Variants: --ct  --xray  --ecg  --orbit
   Sizes:    --sm  --md  --lg  --xl
   ========================================================================== */

.rad-loader {
    --rad-accent: #38bdf8;
    --rad-accent-2: #a78bfa;
    --rad-glow: rgba(56, 189, 248, 0.55);
    --rad-track: rgba(148, 163, 184, 0.28);
    --rad-stage: 46px;

    display: inline-flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    gap: 0.65rem;
    color: var(--rad-accent);
    font-size: 0.8rem;
    line-height: 1.25;
    text-align: center;
}

/* ------------------------------------------------------------------ Sizes */
.rad-loader--sm {
    --rad-stage: 26px;
    gap: 0.35rem;
    font-size: 0.7rem;
}

.rad-loader--lg {
    --rad-stage: 76px;
    gap: 0.85rem;
    font-size: 0.85rem;
}

.rad-loader--xl {
    --rad-stage: 112px;
    gap: 1rem;
    font-size: 0.95rem;
}

/* ------------------------------------------------------------------ Label */
.rad-loader__label {
    color: var(--bs-secondary-color, #adb5bd);
    letter-spacing: 0.03em;
    font-weight: 500;
    text-shadow: 0 1px 2px rgba(0, 0, 0, 0.45);
}

/* ----------------------------------------------------------------- Stage */
.rad-loader__stage {
    position: relative;
    display: block;
    flex: 0 0 auto;
    width: var(--rad-stage);
    height: var(--rad-stage);
}

/* --------------------------------------------------------- Surface / card */
.rad-loader-surface {
    display: flex;
    align-items: center;
    justify-content: center;
    min-height: 180px;
    padding: 2rem 1rem;
    text-align: center;
}

/* ----------------------------------------------------- Centered overlay */
.rad-loader-overlay {
    position: absolute;
    inset: 0;
    z-index: 10;
    display: flex;
    align-items: center;
    justify-content: center;
    border-radius: inherit;
    background: radial-gradient(circle at 50% 45%, rgba(15, 23, 42, 0.82), rgba(2, 6, 23, 0.94));
    -webkit-backdrop-filter: blur(2px);
    backdrop-filter: blur(2px);
}

.rad-loader-overlay--full {
    position: fixed;
    z-index: 1080;
}

/* ==========================================================================
   CT / gantry: rotating detector ring with a glowing bore and moving couch
   ========================================================================== */
.rad-loader__stage--ct .rad-loader__gantry {
    position: absolute;
    inset: 0;
    border-radius: 50%;
    background: repeating-conic-gradient(from 0deg,
            var(--rad-accent) 0deg 7deg,
            transparent 7deg 30deg);
    -webkit-mask: radial-gradient(farthest-side, transparent calc(100% - 26%), #000 calc(100% - 23%));
    mask: radial-gradient(farthest-side, transparent calc(100% - 26%), #000 calc(100% - 23%));
    animation: rad-spin 1.5s linear infinite;
}

.rad-loader__stage--ct .rad-loader__bore {
    position: absolute;
    inset: 28%;
    border-radius: 50%;
    background: radial-gradient(circle at 50% 50%, rgba(56, 189, 248, 0.32), rgba(56, 189, 248, 0) 70%);
    box-shadow: inset 0 0 0 1px var(--rad-track), 0 0 16px var(--rad-glow);
    animation: rad-pulse 1.5s ease-in-out infinite;
}

.rad-loader__stage--ct .rad-loader__couch {
    position: absolute;
    left: 6%;
    right: 6%;
    top: calc(50% - 1.5px);
    height: 3px;
    border-radius: 2px;
    background: linear-gradient(90deg, transparent, var(--rad-accent), transparent);
    animation: rad-couch 1.5s ease-in-out infinite;
}

@keyframes rad-couch {
    0%,
    100% {
        transform: translateX(-40%) scaleX(0.55);
        opacity: 0.35;
    }

    50% {
        transform: translateX(40%) scaleX(1);
        opacity: 1;
    }
}

/* ==========================================================================
   X-ray: cassette with a bright scan line sweeping the film
   ========================================================================== */
.rad-loader__stage--xray .rad-loader__film {
    position: absolute;
    inset: 2px;
    border-radius: 6px;
    border: 1px solid var(--rad-track);
    overflow: hidden;
    background:
        repeating-linear-gradient(0deg, transparent 0 6px, rgba(148, 163, 184, 0.12) 6px 7px),
        repeating-linear-gradient(90deg, transparent 0 6px, rgba(148, 163, 184, 0.12) 6px 7px),
        linear-gradient(180deg, rgba(56, 189, 248, 0.08), rgba(56, 189, 248, 0.02));
}

.rad-loader__stage--xray .rad-loader__film::before {
    content: "";
    position: absolute;
    left: 0;
    right: 0;
    height: 30%;
    background: linear-gradient(180deg, transparent, rgba(56, 189, 248, 0.9), transparent);
    filter: blur(1px);
    animation: rad-scan 1.6s cubic-bezier(0.45, 0, 0.55, 1) infinite;
}

.rad-loader__stage--xray .rad-loader__scanline {
    position: absolute;
    left: 12%;
    right: 12%;
    bottom: 12%;
    height: 3px;
    border-radius: 2px;
    background: linear-gradient(90deg, transparent, var(--rad-accent-2), transparent);
    opacity: 0.7;
    animation: rad-scanline 1.6s cubic-bezier(0.45, 0, 0.55, 1) infinite;
}

@keyframes rad-scan {
    0% {
        top: -30%;
    }

    100% {
        top: 100%;
    }
}

@keyframes rad-scanline {
    0% {
        transform: scaleX(0.2);
        opacity: 0.15;
    }

    50% {
        transform: scaleX(1);
        opacity: 0.75;
    }

    100% {
        transform: scaleX(0.2);
        opacity: 0.15;
    }
}

/* ==========================================================================
   ECG: cardiac trace drawing across the monitor
   ========================================================================== */
.rad-loader__stage--ecg {
    width: calc(var(--rad-stage) * 2.6);
    height: var(--rad-stage);
}

.rad-loader__stage--ecg svg {
    width: 100%;
    height: 100%;
    overflow: visible;
}

.rad-loader__ecg-base {
    fill: none;
    stroke: var(--rad-track);
    stroke-width: 2;
    stroke-linecap: round;
    stroke-linejoin: round;
}

.rad-loader__ecg-trace {
    fill: none;
    stroke: var(--rad-accent);
    stroke-width: 3;
    stroke-linecap: round;
    stroke-linejoin: round;
    filter: drop-shadow(0 0 4px var(--rad-glow));
    stroke-dasharray: 16 84;
    stroke-dashoffset: 100;
    animation: rad-ecg 1.7s linear infinite;
}

@keyframes rad-ecg {
    0% {
        stroke-dashoffset: 100;
    }

    100% {
        stroke-dashoffset: -100;
    }
}

/* ==========================================================================
   Orbit: electrons orbiting the nucleus (physics / radionuclide)
   ========================================================================== */
.rad-loader__stage--orbit .rad-loader__nucleus {
    position: absolute;
    top: 50%;
    left: 50%;
    width: 20%;
    height: 20%;
    transform: translate(-50%, -50%);
    border-radius: 50%;
    background: var(--rad-accent);
    box-shadow: 0 0 14px var(--rad-glow);
    animation: rad-pulse 1.6s ease-in-out infinite;
}

.rad-loader__stage--orbit .rad-loader__orbit {
    position: absolute;
    inset: 0;
    border: 1px solid var(--rad-track);
    border-radius: 50%;
}

.rad-loader__stage--orbit .rad-loader__orbit--1 {
    transform: rotate(0deg) scaleY(0.42);
}

.rad-loader__stage--orbit .rad-loader__orbit--2 {
    transform: rotate(60deg) scaleY(0.42);
}

.rad-loader__stage--orbit .rad-loader__orbit--3 {
    transform: rotate(120deg) scaleY(0.42);
}

.rad-loader__stage--orbit .rad-loader__orbit-spin {
    position: absolute;
    inset: 0;
    display: block;
}

.rad-loader__orbit--1 .rad-loader__orbit-spin {
    animation: rad-spin 2s linear infinite;
}

.rad-loader__orbit--2 .rad-loader__orbit-spin {
    animation: rad-spin 2.6s linear infinite reverse;
}

.rad-loader__orbit--3 .rad-loader__orbit-spin {
    animation: rad-spin 3.2s linear infinite;
}

.rad-loader__stage--orbit .rad-loader__orbit-spin i {
    position: absolute;
    top: -3px;
    left: 50%;
    width: 7px;
    height: 7px;
    margin-left: -3.5px;
    border-radius: 50%;
    background: var(--rad-accent-2);
    box-shadow: 0 0 8px var(--rad-accent-2);
}

/* ============================================================ Shared keys */
@keyframes rad-spin {
    to {
        transform: rotate(360deg);
    }
}

@keyframes rad-pulse {
    0%,
    100% {
        opacity: 0.45;
    }

    50% {
        opacity: 1;
    }
}

/* ================================================= Accessibility fallback */
@media (prefers-reduced-motion: reduce) {
    .rad-loader *,
    .rad-loader *::before,
    .rad-loader *::after {
        animation-duration: 0.001ms !important;
        animation-iteration-count: 1 !important;
        transition-duration: 0.001ms !important;
    }

    .rad-loader__stage--xray .rad-loader__film::before {
        top: 35%;
    }

    .rad-loader__ecg-trace {
        stroke-dasharray: none;
        stroke-dashoffset: 0;
    }
}

/* Native htmx request indicator for the .exam-loading spinners (replaces the
   custom htmx:before/after:request JS that toggled .d-none). */
.exam-loading.htmx-request {
    display: inline-block !important;
}
