/* =====================================================================
   LOADING STATES - Sistema de estados de carga
   =====================================================================
   Spinners, skeletons, y estados de carga para botones/formularios
   ===================================================================== */

:root {
    --loading-primary: #3b82f6;
    --loading-secondary: #e5e7eb;
    --loading-bg: rgba(255, 255, 255, 0.9);
    --loading-text: #6b7280;
}

/* =====================================================================
   SPINNER - Animación de carga circular
   ===================================================================== */

.spinner {
    display: inline-block;
    width: 20px;
    height: 20px;
    border: 2px solid var(--loading-secondary);
    border-top-color: var(--loading-primary);
    border-radius: 50%;
    animation: spin 0.8s linear infinite;
}

.spinner--sm {
    width: 16px;
    height: 16px;
    border-width: 2px;
}

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

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

.spinner--white {
    border-color: rgba(255, 255, 255, 0.3);
    border-top-color: white;
}

.spinner--primary {
    border-top-color: var(--loading-primary);
}

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

/* =====================================================================
   LOADING OVERLAY - Overlay de carga sobre contenedores
   ===================================================================== */

.loading-overlay {
    position: absolute;
    top: 0;
    left: 0;
    right: 0;
    bottom: 0;
    background: var(--loading-bg);
    backdrop-filter: blur(2px);
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    z-index: 100;
    opacity: 0;
    visibility: hidden;
    transition: opacity 0.2s ease, visibility 0.2s ease;
}

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

.loading-overlay__text {
    margin-top: 0.75rem;
    font-size: 0.875rem;
    color: var(--loading-text);
    font-weight: 500;
}

/* Contenedor debe ser relativo para el overlay */
.has-loading-overlay {
    position: relative;
}

/* =====================================================================
   BUTTON LOADING STATE - Botones con estado de carga
   ===================================================================== */

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

.btn-loading::after,
button.is-loading::after,
.modern-btn.is-loading::after {
    content: '';
    position: absolute;
    top: 50%;
    left: 50%;
    width: 18px;
    height: 18px;
    margin: -9px 0 0 -9px;
    border: 2px solid rgba(255, 255, 255, 0.3);
    border-top-color: white;
    border-radius: 50%;
    animation: spin 0.8s linear infinite;
}

/* Para botones secundarios/outline */
.btn-secondary.is-loading::after,
.modern-btn-secondary.is-loading::after {
    border-color: rgba(0, 0, 0, 0.1);
    border-top-color: var(--loading-primary);
}

/* =====================================================================
   SKELETON LOADER - Placeholder animado para contenido
   ===================================================================== */

.skeleton {
    background: linear-gradient(
        90deg,
        #f0f0f0 25%,
        #e0e0e0 50%,
        #f0f0f0 75%
    );
    background-size: 200% 100%;
    animation: skeleton-shimmer 1.5s ease-in-out infinite;
    border-radius: 4px;
}

.skeleton-text {
    height: 1rem;
    margin-bottom: 0.5rem;
    border-radius: 4px;
}

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

.skeleton-title {
    height: 1.5rem;
    width: 60%;
    margin-bottom: 1rem;
}

.skeleton-avatar {
    width: 40px;
    height: 40px;
    border-radius: 50%;
}

.skeleton-avatar--lg {
    width: 64px;
    height: 64px;
}

.skeleton-button {
    height: 40px;
    width: 120px;
    border-radius: 6px;
}

.skeleton-card {
    height: 200px;
    border-radius: 12px;
}

.skeleton-image {
    height: 150px;
    border-radius: 8px;
}

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

/* =====================================================================
   HTMX LOADING STATES - Integración con HTMX
   ===================================================================== */

/* Indicador de carga para elementos con hx-indicator */
.htmx-indicator {
    display: none;
}

.htmx-request .htmx-indicator,
.htmx-request.htmx-indicator {
    display: inline-flex;
    align-items: center;
    gap: 0.5rem;
}

/* Elemento que dispara la request obtiene clase htmx-request */
.htmx-request {
    pointer-events: none;
    opacity: 0.7;
}

/* Botón con HTMX request activo */
button.htmx-request,
a.htmx-request,
.btn.htmx-request {
    position: relative;
}

button.htmx-request::before,
a.htmx-request::before,
.btn.htmx-request::before {
    content: '';
    position: absolute;
    top: 50%;
    left: 50%;
    width: 16px;
    height: 16px;
    margin: -8px 0 0 -8px;
    border: 2px solid currentColor;
    border-top-color: transparent;
    border-radius: 50%;
    animation: spin 0.8s linear infinite;
    opacity: 0.5;
}

/* =====================================================================
   FORM LOADING STATE - Formularios en proceso de envío
   ===================================================================== */

.form-submitting {
    position: relative;
}

.form-submitting::before {
    content: '';
    position: absolute;
    top: 0;
    left: 0;
    right: 0;
    bottom: 0;
    background: rgba(255, 255, 255, 0.8);
    z-index: 50;
    backdrop-filter: blur(1px);
}

.form-submitting::after {
    content: 'Procesando...';
    position: absolute;
    top: 50%;
    left: 50%;
    transform: translate(-50%, -50%);
    background: white;
    padding: 1rem 1.5rem;
    border-radius: 8px;
    box-shadow: 0 4px 12px rgba(0, 0, 0, 0.15);
    font-weight: 600;
    color: var(--loading-text);
    z-index: 51;
    display: flex;
    align-items: center;
    gap: 0.75rem;
}

/* =====================================================================
   LOADING DOTS - Puntos animados para "cargando..."
   ===================================================================== */

.loading-dots {
    display: inline-flex;
    align-items: center;
    gap: 4px;
}

.loading-dots span {
    width: 6px;
    height: 6px;
    background: currentColor;
    border-radius: 50%;
    animation: loading-dots 1.4s ease-in-out infinite both;
}

.loading-dots span:nth-child(1) {
    animation-delay: -0.32s;
}

.loading-dots span:nth-child(2) {
    animation-delay: -0.16s;
}

@keyframes loading-dots {
    0%, 80%, 100% {
        transform: scale(0);
        opacity: 0.5;
    }
    40% {
        transform: scale(1);
        opacity: 1;
    }
}

/* =====================================================================
   PROGRESS BAR - Barra de progreso animada
   ===================================================================== */

.progress-bar {
    height: 4px;
    background: var(--loading-secondary);
    border-radius: 2px;
    overflow: hidden;
    position: relative;
}

.progress-bar__fill {
    height: 100%;
    background: var(--loading-primary);
    border-radius: 2px;
    transition: width 0.3s ease;
}

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

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

/* =====================================================================
   INLINE LOADING MESSAGE - Mensaje de carga inline
   ===================================================================== */

.loading-message {
    display: inline-flex;
    align-items: center;
    gap: 0.5rem;
    padding: 0.5rem 1rem;
    background: #f0f9ff;
    border: 1px solid #bae6fd;
    border-radius: 6px;
    font-size: 0.875rem;
    color: #0369a1;
}

.loading-message .spinner {
    width: 14px;
    height: 14px;
    border-width: 2px;
    border-color: #bae6fd;
    border-top-color: #0369a1;
}

/* =====================================================================
   DARK MODE
   ===================================================================== */

[data-theme="dark"] {
    --loading-secondary: #374151;
    --loading-bg: rgba(15, 23, 42, 0.9);
    --loading-text: #9ca3af;
}

[data-theme="dark"] .skeleton {
    background: linear-gradient(
        90deg,
        #1f2937 25%,
        #374151 50%,
        #1f2937 75%
    );
    background-size: 200% 100%;
}

[data-theme="dark"] .loading-message {
    background: #1e3a5f;
    border-color: #1e40af;
    color: #93c5fd;
}

/* =====================================================================
   ACCESSIBILITY - Respetar preferencia de movimiento reducido
   ===================================================================== */

@media (prefers-reduced-motion: reduce) {
    .spinner,
    .skeleton,
    .loading-dots span,
    .progress-bar--indeterminate .progress-bar__fill,
    .btn-loading::after,
    button.is-loading::after,
    .htmx-request::before {
        animation: none;
    }

    .skeleton {
        background: #e5e7eb;
    }

    [data-theme="dark"] .skeleton {
        background: #374151;
    }
}
