/**
 * Global Loading Overlay Styles
 * Features: light/dark theme support, animations, accessibility, blur effects
 * Compatible with the existing styles.css color scheme
 */

/* ========== CSS VARIABLES FOR THEMING ========== */
:root {
    /* Loading overlay specific variables */
    --loading-overlay-bg: rgba(255, 255, 255, 0.85);
    --loading-overlay-backdrop: rgba(0, 0, 0, 0.2);
    --loading-content-bg: var(--surface, #FFFFFF);
    --loading-content-shadow: 0 4px 20px rgba(0, 0, 0, 0.15);
    --loading-text-color: var(--text-primary, #333333);
    --loading-text-secondary: var(--text-secondary, #757575);
    --loading-spinner-primary: var(--primary, #1976D2);
    --loading-spinner-secondary: var(--primary-light, #e3f2fd);
    --loading-border-radius: 12px;
    --loading-animation-duration: 1.4s;
}

/* Dark theme support */
@media (prefers-color-scheme: dark) {
    :root {
        --loading-overlay-bg: rgba(33, 33, 33, 0.9);
        --loading-overlay-backdrop: rgba(0, 0, 0, 0.6);
        --loading-content-bg: #424242;
        --loading-content-shadow: 0 4px 20px rgba(0, 0, 0, 0.4);
        --loading-text-color: #FFFFFF;
        --loading-text-secondary: #CCCCCC;
        --loading-spinner-primary: #64B5F6;
        --loading-spinner-secondary: #1E3A8A;
    }
}

/* Manual dark theme class (for programmatic control) */
.dark-theme {
    --loading-overlay-bg: rgba(33, 33, 33, 0.9);
    --loading-overlay-backdrop: rgba(0, 0, 0, 0.6);
    --loading-content-bg: #424242;
    --loading-content-shadow: 0 4px 20px rgba(0, 0, 0, 0.4);
    --loading-text-color: #FFFFFF;
    --loading-text-secondary: #CCCCCC;
    --loading-spinner-primary: #64B5F6;
    --loading-spinner-secondary: #1E3A8A;
}

/* ========== LOADING OVERLAY ========== */
.loading-overlay {
    position: fixed;
    top: 0;
    left: 0;
    right: 0;
    bottom: 0;
    z-index: 9999;
    
    /* Background with backdrop blur support */
    background: var(--loading-overlay-bg);
    backdrop-filter: blur(3px);
    -webkit-backdrop-filter: blur(3px);
    
    /* Flexbox centering */
    display: flex;
    align-items: center;
    justify-content: center;
    
    /* Animation */
    animation: loadingOverlayFadeIn 0.3s ease-out;
    
    /* Accessibility */
    outline: none;
    
    /* Prevent text selection */
    user-select: none;
    -webkit-user-select: none;
    -moz-user-select: none;
    -ms-user-select: none;
}

/* Loading content container */
.loading-content {
    background: var(--loading-content-bg);
    border-radius: var(--loading-border-radius);
    box-shadow: var(--loading-content-shadow);
    padding: 32px 40px;
    text-align: center;
    max-width: 400px;
    min-width: 280px;
    margin: 20px;
    
    /* Smooth appearance */
    transform: scale(0.95);
    animation: loadingContentAppear 0.4s ease-out 0.1s forwards;
}

/* ========== SPINNER ANIMATION ========== */
.loading-spinner {
    display: flex;
    justify-content: center;
    align-items: center;
    margin-bottom: 20px;
    height: 40px;
}

.loading-spinner-icon {
    width: 45px;
    height: 45px;
    background-image: url('/img/loading.ico');
    background-size: contain;
    background-repeat: no-repeat;
    background-position: center;
    animation: spinFavicon 2s ease-in-out infinite;
    /* Fallback for browsers that don't support .ico */
    background-image: url('/img/loading.ico'), url('/img/loading.ico');
    /* Remove any shadow */
    filter: none;
    box-shadow: none;
}

/* Fallback dots for when favicon is not available */
.loading-dot {
    width: 8px;
    height: 8px;
    border-radius: 50%;
    background: var(--loading-spinner-primary);
    margin: 0 3px;
    animation: loadingDotBounce var(--loading-animation-duration) ease-in-out infinite both;
    display: none; /* Hidden by default, shown when favicon fails */
}

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

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

.loading-dot:nth-child(3) {
    animation-delay: 0s;
}

/* ========== MESSAGE TEXT ========== */
.loading-message {
    color: var(--loading-text-color);
    font-size: 14px;
    font-weight: 500;
    line-height: 1.4;
    margin: 0;
    
    /* Smooth text changes */
    transition: opacity 0.2s ease;
}

/* ========== ANIMATIONS ========== */
@keyframes loadingOverlayFadeIn {
    from {
        opacity: 0;
        backdrop-filter: blur(0px);
        -webkit-backdrop-filter: blur(0px);
    }
    to {
        opacity: 1;
        backdrop-filter: blur(3px);
        -webkit-backdrop-filter: blur(3px);
    }
}

@keyframes loadingContentAppear {
    from {
        opacity: 0;
        transform: scale(0.95) translateY(10px);
    }
    to {
        opacity: 1;
        transform: scale(1) translateY(0);
    }
}

@keyframes spinFavicon {
    0% {
        transform: rotate(0deg);
        animation-timing-function: ease-out;
    }
    25% {
        transform: rotate(180deg);
        animation-timing-function: ease-in-out;
    }
    50% {
        transform: rotate(270deg);
        animation-timing-function: ease-in;
    }
    75% {
        transform: rotate(450deg);
        animation-timing-function: ease-out;
    }
    100% {
        transform: rotate(720deg);
        animation-timing-function: ease-out;
    }
}

@keyframes loadingDotBounce {
    0%, 80%, 100% {
        transform: scale(0.8);
        opacity: 0.6;
    }
    40% {
        transform: scale(1.2);
        opacity: 1;
    }
}

/* ========== RESPONSIVE DESIGN ========== */
@media (max-width: 480px) {
    .loading-content {
        margin: 16px;
        padding: 24px 28px;
        min-width: auto;
        max-width: calc(100vw - 32px);
    }
    
    .loading-message {
        font-size: 13px;
    }
    
    .loading-spinner {
        margin-bottom: 16px;
        height: 32px;
    }
    
    .loading-dot {
        width: 6px;
        height: 6px;
        margin: 0 2px;
    }
}

@media (max-width: 320px) {
    .loading-content {
        padding: 20px 24px;
    }
    
    .loading-message {
        font-size: 12px;
    }
}

/* ========== ACCESSIBILITY ENHANCEMENTS ========== */
/* High contrast mode support */
@media (prefers-contrast: high) {
    .loading-overlay {
        background: rgba(0, 0, 0, 0.8);
    }
    
    .loading-content {
        border: 2px solid var(--loading-text-color);
    }
    
    .loading-dot {
        background: var(--loading-text-color);
    }
}

/* Reduced motion support */
@media (prefers-reduced-motion: reduce) {
    .loading-overlay {
        animation: none;
        backdrop-filter: none;
        -webkit-backdrop-filter: none;
    }
    
    .loading-content {
        animation: none;
        transform: scale(1);
    }
    
    .loading-spinner-icon {
        animation: none;
        opacity: 0.8;
    }
    
    .loading-dot {
        animation: none;
        opacity: 1;
        transform: scale(1);
    }
    
    .loading-spinner .loading-dot:nth-child(2) {
        opacity: 0.7;
    }
    
    .loading-spinner .loading-dot:nth-child(3) {
        opacity: 0.4;
    }
}

/* Focus indicators for accessibility */
.loading-overlay:focus {
    outline: none; /* We handle focus visually through the content */
}

.loading-content:focus-within {
    box-shadow: var(--loading-content-shadow), 0 0 0 3px var(--loading-spinner-primary);
}

/* ========== PRINT STYLES ========== */
@media print {
    .loading-overlay {
        display: none !important;
    }
}

/* ========== LOADING STATES FOR FORMS ========== */
/* Add subtle styling for forms while loading is active */
[data-loading-disabled="true"] {
    opacity: 0.6;
    pointer-events: none;
}

/* Specific button styling when disabled by loading */
button[data-loading-disabled="true"],
input[type="submit"][data-loading-disabled="true"] {
    cursor: not-allowed;
    opacity: 0.5;
}

/* ========== ALTERNATIVE SPINNER STYLES ========== */
/* Alternative circular spinner (can be switched by changing the class) */
.loading-spinner.circular {
    height: 32px;
}

.loading-spinner.circular::before {
    content: '';
    width: 24px;
    height: 24px;
    border: 3px solid var(--loading-spinner-secondary);
    border-top-color: var(--loading-spinner-primary);
    border-radius: 50%;
    animation: spin 1s linear infinite;
}

.loading-spinner.circular .loading-dot,
.loading-spinner.circular .loading-spinner-icon {
    display: none;
}

/* Linear spinning alternative for favicon */
.loading-spinner.linear-spin .loading-spinner-icon {
    animation: spin 1s linear infinite;
}

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

/* ========== UTILITY CLASSES ========== */
/* For debugging or custom implementations */
.loading-overlay.debug {
    background: rgba(255, 0, 0, 0.1);
    border: 2px dashed red;
}

.loading-content.compact {
    padding: 20px 24px;
    min-width: 200px;
}

.loading-message.large {
    font-size: 16px;
    font-weight: 600;
}

.loading-message.small {
    font-size: 12px;
    font-weight: 400;
}