/**
 * LOADING STATES - PharmData Design System v2.0
 * Skeleton screens, spinners, progress indicators
 *
 * Optimizado para uso com HTMX onde estados de loading
 * são críticos para UX durante requisições ao servidor.
 */

:root {
    /* Loading colors */
    --skeleton-base: var(--soft-arctic, #E5E8EB);
    --skeleton-highlight: var(--cloud, #F5F8FA);
    --spinner-color: var(--teal-intense, #1a8b7d);
    --progress-bg: var(--soft-arctic, #E5E8EB);
    --progress-fill: var(--teal-intense, #1a8b7d);
}

/* ===================
   SKELETON BASE
   =================== */

.skeleton {
    background: linear-gradient(
        90deg,
        var(--skeleton-base) 0%,
        var(--skeleton-highlight) 50%,
        var(--skeleton-base) 100%
    );
    background-size: 200% 100%;
    animation: skeleton-shimmer 1.5s ease-in-out infinite;
    border-radius: var(--radius-sm);
    color: transparent !important;
    user-select: none;
    pointer-events: none;
}

.skeleton * {
    visibility: hidden;
}

@keyframes skeleton-shimmer {
    0% {
        background-position: 200% 0;
    }
    100% {
        background-position: -200% 0;
    }
}

/* ===================
   SKELETON VARIANTS
   =================== */

/* Text skeleton */
.skeleton-text {
    height: 1em;
    margin-bottom: var(--space-2);
    border-radius: var(--radius-sm);
}

.skeleton-text:last-child {
    width: 70%;
}

/* Heading skeleton */
.skeleton-heading {
    height: 1.5em;
    width: 60%;
    margin-bottom: var(--space-3);
    border-radius: var(--radius-sm);
}

/* Avatar skeleton */
.skeleton-avatar {
    width: 2.5rem;
    height: 2.5rem;
    border-radius: var(--radius-full);
    flex-shrink: 0;
}

.skeleton-avatar-sm {
    width: 2rem;
    height: 2rem;
}

.skeleton-avatar-lg {
    width: 3.5rem;
    height: 3.5rem;
}

/* Button skeleton */
.skeleton-button {
    height: 2.5rem;
    width: 6rem;
    border-radius: var(--radius-md);
}

/* Card skeleton */
.skeleton-card {
    padding: var(--space-4);
    border: 1px solid var(--border-subtle, rgba(183, 228, 213, 0.15));
    border-radius: var(--radius-lg);
}

/* Image skeleton */
.skeleton-image {
    width: 100%;
    aspect-ratio: 16/9;
    border-radius: var(--radius-md);
}

.skeleton-image-square {
    aspect-ratio: 1/1;
}

/* Table row skeleton */
.skeleton-row {
    display: grid;
    grid-template-columns: 1fr 2fr 1fr 1fr;
    gap: var(--space-4);
    padding: var(--space-3) var(--space-4);
    border-bottom: 1px solid var(--border-subtle, rgba(183, 228, 213, 0.15));
}

.skeleton-cell {
    height: 1rem;
    border-radius: var(--radius-sm);
}

/* Badge/Drop skeleton */
.skeleton-badge {
    height: 1.5rem;
    width: 4rem;
    border-radius: var(--radius-full);
}

/* ===================
   SPINNERS
   =================== */

.spinner {
    display: inline-block;
    width: 1.25rem;
    height: 1.25rem;
    border: 2px solid var(--skeleton-base);
    border-top-color: var(--spinner-color);
    border-radius: 50%;
    animation: spinner-rotate 0.8s linear infinite;
}

.spinner-sm {
    width: 1rem;
    height: 1rem;
    border-width: 1.5px;
}

.spinner-lg {
    width: 2rem;
    height: 2rem;
    border-width: 3px;
}

.spinner-xl {
    width: 3rem;
    height: 3rem;
    border-width: 4px;
}

@keyframes spinner-rotate {
    0% { transform: rotate(0deg); }
    100% { transform: rotate(360deg); }
}

/* Spinner inside button */
.btn .spinner {
    margin-right: var(--space-2);
}

.btn-loading {
    position: relative;
    color: transparent !important;
    pointer-events: none;
}

.btn-loading::after {
    content: '';
    position: absolute;
    top: 50%;
    left: 50%;
    width: 1.25rem;
    height: 1.25rem;
    margin: -0.625rem 0 0 -0.625rem;
    border: 2px solid currentColor;
    border-top-color: transparent;
    border-radius: 50%;
    animation: spinner-rotate 0.8s linear infinite;
    opacity: 0.8;
}

/* ===================
   PROGRESS BAR
   =================== */

.progress {
    width: 100%;
    height: 0.5rem;
    background: var(--progress-bg);
    border-radius: var(--radius-full);
    overflow: hidden;
}

.progress-bar {
    height: 100%;
    background: var(--progress-fill);
    border-radius: var(--radius-full);
    transition: width 0.3s ease;
}

/* Indeterminate progress */
.progress-indeterminate .progress-bar {
    width: 30%;
    animation: progress-indeterminate 1.5s ease-in-out infinite;
}

@keyframes progress-indeterminate {
    0% {
        transform: translateX(-100%);
    }
    100% {
        transform: translateX(400%);
    }
}

/* Progress sizes */
.progress-sm { height: 0.25rem; }
.progress-lg { height: 0.75rem; }

/* ===================
   LOADING OVERLAY
   =================== */

.loading-overlay {
    position: absolute;
    inset: 0;
    display: flex;
    align-items: center;
    justify-content: center;
    background: rgba(255, 255, 255, 0.8);
    backdrop-filter: blur(2px);
    z-index: var(--z-overlay, 300);
    opacity: 0;
    visibility: hidden;
    transition: opacity 0.2s, visibility 0.2s;
}

/* Dark mode loading overlay */
:root.dark-mode .loading-overlay,
[data-theme="dark"] .loading-overlay {
    background: rgba(17, 24, 39, 0.85);
}

.loading-overlay.active,
.is-loading > .loading-overlay {
    opacity: 1;
    visibility: visible;
}

/* Container that holds loading overlay */
.loading-container {
    position: relative;
}

/* ===================
   HTMX INTEGRATION
   =================== */

/* During HTMX request */
.htmx-request {
    opacity: 0.7;
    pointer-events: none;
    transition: opacity 0.2s;
}

.htmx-request .spinner {
    display: inline-block;
}

/* htmx indicator */
.htmx-indicator {
    display: none;
}

.htmx-request .htmx-indicator,
.htmx-request.htmx-indicator {
    display: inline-flex;
}

/* Skeleton during htmx swap */
[hx-swap] .skeleton {
    display: block;
}

/* ===================
   PULSE ANIMATION
   For subtle loading indication
   =================== */

.pulse {
    animation: pulse 2s cubic-bezier(0.4, 0, 0.6, 1) infinite;
}

@keyframes pulse {
    0%, 100% {
        opacity: 1;
    }
    50% {
        opacity: 0.5;
    }
}

/* ===================
   DOT LOADER
   =================== */

.dot-loader {
    display: inline-flex;
    gap: var(--space-1);
}

.dot-loader span {
    width: 0.5rem;
    height: 0.5rem;
    background: var(--spinner-color);
    border-radius: 50%;
    animation: dot-bounce 1.4s ease-in-out infinite both;
}

.dot-loader span:nth-child(1) { animation-delay: -0.32s; }
.dot-loader span:nth-child(2) { animation-delay: -0.16s; }
.dot-loader span:nth-child(3) { animation-delay: 0; }

@keyframes dot-bounce {
    0%, 80%, 100% {
        transform: scale(0);
    }
    40% {
        transform: scale(1);
    }
}

/* ===================
   ACCESSIBILITY
   =================== */

@media (prefers-reduced-motion: reduce) {
    .skeleton,
    .spinner,
    .progress-indeterminate .progress-bar,
    .pulse,
    .dot-loader span {
        animation: none;
    }

    .skeleton {
        background: var(--skeleton-base);
    }

    .spinner {
        border-color: var(--spinner-color);
        opacity: 0.6;
    }
}
