/* ==========================================================================
   Modal Component - ITCSS Layer 4 (Components)
   ========================================================================== */

/* Modal overlay */
.modal-overlay {
    position: fixed;
    top: 0;
    left: 0;
    right: 0;
    bottom: 0;
    background-color: color-mix(in srgb, var(--color-black) 50%, transparent);
    z-index: var(--z-index-modal);
    display: flex;
    align-items: center;
    justify-content: center;
    padding: var(--spacing-lg);
    opacity: 0;
    visibility: hidden;
    transition: all var(--transition-speed-normal) var(--transition-easing);
    backdrop-filter: blur(4px);
}

.modal-overlay.is-open {
    opacity: 1;
    visibility: visible;
}

/* Modal container */
.modal {
    background-color: var(--sys-color-background-surface);
    border-radius: var(--border-radius-lg);
    box-shadow: var(--shadow-xl);
    max-width: 90vw;
    max-height: 90vh;
    width: 100%;
    position: relative;
    transform: scale(0.95) translateY(20px);
    transition: transform var(--transition-speed-normal) var(--transition-easing);
    overflow: hidden;
    display: flex;
    flex-direction: column;
}

.modal-overlay.is-open .modal {
    transform: scale(1) translateY(0);
}

/* Modal sizes */
.modal-sm {
    max-width: 400px;
}

.modal-md {
    max-width: 600px;
}

.modal-lg {
    max-width: 800px;
}

.modal-xl {
    max-width: 1200px;
}

.modal-full {
    max-width: 95vw;
    max-height: 95vh;
}

/* Modal header */
.modal-header {
    padding: var(--spacing-lg);
    border-bottom: var(--border-width-sm) solid var(--sys-color-divider-default);
    display: flex;
    align-items: center;
    justify-content: space-between;
    gap: var(--spacing-md);
    flex-shrink: 0;
}

.modal-title {
    font-size: var(--font-size-xl);
    font-weight: var(--font-weight-semibold);
    color: var(--sys-color-heading);
    margin: 0;
    line-height: var(--line-height-tight);
}

.modal-subtitle {
    font-size: var(--font-size-sm);
    color: var(--sys-color-text-secondary);
    margin: var(--spacing-xs) 0 0 0;
}

/* Modal close button */
.modal-close {
    background: none;
    border: none;
    padding: var(--spacing-xs);
    border-radius: var(--border-radius-md);
    color: var(--sys-color-text-secondary);
    cursor: pointer;
    transition: all var(--transition-speed-fast) var(--transition-easing);
    display: flex;
    align-items: center;
    justify-content: center;
    width: 32px;
    height: 32px;
    flex-shrink: 0;
}

.modal-close:hover {
    background-color: var(--sys-color-background-elevated);
    color: var(--sys-color-text-primary);
}

.modal-close:focus-visible {
    outline: 2px solid var(--sys-color-interactive);
    outline-offset: 2px;
}

/* Modal body */
.modal-body {
    padding: var(--spacing-lg);
    overflow-y: auto;
    flex: 1;
    color: var(--sys-color-text-primary);
    line-height: var(--line-height-normal);
}

.modal-body:first-child {
    border-radius: var(--border-radius-lg) var(--border-radius-lg) 0 0;
}

.modal-body:last-child {
    border-radius: 0 0 var(--border-radius-lg) var(--border-radius-lg);
}

.modal-body:only-child {
    border-radius: var(--border-radius-lg);
}

/* Modal footer */
.modal-footer {
    padding: var(--spacing-lg);
    border-top: var(--border-width-sm) solid var(--sys-color-divider-default);
    display: flex;
    align-items: center;
    justify-content: flex-end;
    gap: var(--spacing-md);
    flex-shrink: 0;
}

.modal-footer-start {
    justify-content: flex-start;
}

.modal-footer-center {
    justify-content: center;
}

.modal-footer-between {
    justify-content: space-between;
}

/* Modal variants */
.modal-danger .modal-header {
    background-color: var(--sys-color-status-error-background);
    color: var(--sys-color-status-error-text);
    border-bottom-color: var(--sys-color-status-error);
}

.modal-warning .modal-header {
    background-color: var(--sys-color-status-warning-background);
    color: var(--sys-color-status-warning-text);
    border-bottom-color: var(--sys-color-status-warning);
}

.modal-success .modal-header {
    background-color: var(--sys-color-status-success-background);
    color: var(--sys-color-status-success-text);
    border-bottom-color: var(--sys-color-status-success);
}

.modal-info .modal-header {
    background-color: var(--sys-color-status-info-background);
    color: var(--sys-color-status-info-text);
    border-bottom-color: var(--sys-color-status-info);
}

/* Modal without header */
.modal-headerless .modal-body {
    padding-top: var(--spacing-xl);
}

/* Modal without footer */
.modal-footerless .modal-body {
    padding-bottom: var(--spacing-xl);
}

/* Scrollable modal */
.modal-scrollable {
    max-height: 80vh;
}

.modal-scrollable .modal-body {
    max-height: 60vh;
    overflow-y: auto;
}

/* Loading state */
.modal-loading {
    position: relative;
    pointer-events: none;
}

.modal-loading::after {
    content: '';
    position: absolute;
    top: 0;
    left: 0;
    right: 0;
    bottom: 0;
    background-color: color-mix(in srgb, var(--sys-color-background-surface) 80%, transparent);
    display: flex;
    align-items: center;
    justify-content: center;
    z-index: 1;
}

.modal-loading::before {
    content: '';
    position: absolute;
    top: 50%;
    left: 50%;
    width: 32px;
    height: 32px;
    margin: -16px 0 0 -16px;
    border: 3px solid var(--sys-color-border-subtle);
    border-top-color: var(--sys-color-primary-default);
    border-radius: 50%;
    animation: spin 1s linear infinite;
    z-index: 2;
}

/* Responsive adjustments */
@media (max-width: 768px) {
    .modal-overlay {
        padding: var(--spacing-md);
        align-items: flex-end;
    }
    
    .modal {
        max-width: 100%;
        max-height: 90vh;
        border-radius: var(--border-radius-lg) var(--border-radius-lg) 0 0;
        transform: translateY(100%);
    }
    
    .modal-overlay.is-open .modal {
        transform: translateY(0);
    }
    
    .modal-header,
    .modal-body,
    .modal-footer {
        padding: var(--spacing-md);
    }
    
    .modal-footer {
        flex-direction: column;
        align-items: stretch;
    }
    
    .modal-footer .btn {
        width: 100%;
    }
}

/* Accessibility improvements */
@media (prefers-reduced-motion: reduce) {
    .modal-overlay,
    .modal {
        transition: none;
    }
    
    .modal-overlay.is-open .modal {
        transform: none;
    }
    
    .modal-loading::before {
        animation: none;
    }
}

/* High contrast mode */
@media (prefers-contrast: high) {
    .modal {
        border: var(--border-width-md) solid var(--sys-color-border-default);
    }
    
    .modal-header,
    .modal-footer {
        border-color: var(--sys-color-border-default);
    }
}

/* Focus management */
.modal[role="dialog"] {
    outline: none;
}

.modal-overlay[aria-hidden="true"] {
    display: none;
}

/* Prevent body scroll when modal is open */
body.modal-open {
    overflow: hidden;
    padding-right: var(--scrollbar-width, 0);
}