.modal {
    position: fixed;
    top: 0;
    left: 0;
    width: 100%;
    height: 100vh;
    z-index: 10000;
    display: none;
    align-items: center;
    justify-content: center;
    opacity: 0;
    transition: opacity var(--transition-cubic);
}

.modal.modal-show {
    opacity: 1;
}

.modal.modal-hide {
    opacity: 0;
}

.modal-backdrop {
    position: absolute;
    top: 0;
    left: 0;
    width: 100%;
    height: 100%;
    background: rgba(0, 0, 0, var(--opacity-50));
    backdrop-filter: blur(2px);
    cursor: pointer;
}

.modal-content {
    position: relative;
    background: var(--color-bg-primary);
    border-radius: var(--radius-lg);
    box-shadow: var(--shadow-xl);
    max-height: 90vh;
    overflow: hidden;
    display: flex;
    flex-direction: column;
    z-index: 1;
    transform: scale(0.9) translateY(-20px);
    transition: transform var(--transition-cubic);
}

.modal.modal-show .modal-content {
    transform: scale(1) translateY(0);
}

.modal.modal-hide .modal-content {
    transform: scale(0.9) translateY(-20px);
}

.modal-content.modal-small {
    width: 90%;
    max-width: 400px;
}

.modal-content.modal-medium {
    width: 90%;
    max-width: 500px;
}

.modal-content.modal-large {
    width: 90%;
    max-width: 700px;
}

.modal-header {
    padding: var(--spacing-xl) var(--spacing-xl) var(--spacing-lg);
    border-bottom: 1px solid var(--color-border-light);
    flex-shrink: 0;
}

.modal-header h3 {
    margin: 0;
    color: var(--color-text-primary);
    font-size: 1.25rem;
    font-weight: 600;
    line-height: 1.3;
}

.modal-body {
    padding: var(--spacing-xl);
    flex: 1;
    overflow-y: auto;
    color: var(--color-text-secondary);
    line-height: 1.6;
}

.modal-body p {
    margin-bottom: var(--spacing-lg);
}

.modal-body p:last-child {
    margin-bottom: 0;
}

.modal-footer {
    padding: var(--spacing-lg) var(--spacing-xl) var(--spacing-xl);
    border-top: 1px solid var(--color-border-light);
    display: flex;
    gap: var(--spacing-md);
    justify-content: flex-end;
    flex-shrink: 0;
}

.modal-close {
    position: absolute;
    top: var(--spacing-lg);
    right: var(--spacing-lg);
    width: 32px;
    height: 32px;
    border: none;
    border-radius: 50%;
    background: var(--color-bg-secondary);
    color: var(--color-text-secondary);
    cursor: pointer;
    display: flex;
    align-items: center;
    justify-content: center;
    font-size: 16px;
    font-weight: bold;
    transition: all var(--transition-base);
    z-index: 2;
}

.modal-close:hover {
    background: var(--color-bg-tertiary);
    color: var(--color-text-primary);
    transform: scale(1.1);
}

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

.close-icon {
    line-height: 1;
}

.modal-fullscreen .modal-content {
    width: 100vw;
    height: 100vh;
    max-width: 100vw;
    max-height: 100vh;
    border-radius: 0;
}

.modal-fullscreen .modal-close {
    position: fixed;
    top: var(--spacing-xl);
    right: var(--spacing-xl);
    background: var(--color-bg-primary);
    border: 2px solid var(--color-border-medium);
    box-shadow: var(--shadow-lg);
    z-index: 10001;
}

.modal-fullscreen .modal-close:hover {
    background: var(--color-bg-secondary);
    border-color: var(--color-border-dark);
}

.modal-body .form-group {
    margin-bottom: var(--spacing-lg);
}

.modal-body label {
    display: block;
    margin-bottom: var(--spacing-sm);
    color: var(--color-text-primary);
    font-weight: 500;
}

.modal-body input,
.modal-body textarea,
.modal-body select {
    width: 100%;
    padding: var(--spacing-md);
    border: 1px solid var(--color-border-medium);
    border-radius: var(--radius-md);
    font-size: 14px;
    transition: border-color var(--transition-base);
    background: var(--color-bg-primary);
    color: var(--color-text-primary);
}

.modal-body input:focus,
.modal-body textarea:focus,
.modal-body select:focus {
    outline: none;
    border-color: var(--color-primary);
    box-shadow: 0 0 0 2px rgba(var(--color-primary-rgb), var(--opacity-20));
}

.modal-body small {
    display: block;
    margin-top: var(--spacing-sm);
    color: var(--color-text-tertiary);
    font-size: 12px;
}

.modal-body .error {
    background: var(--gradient-error-subtle);
    color: var(--color-error-darker);
    padding: var(--spacing-sm) var(--spacing-md);
    border-radius: var(--radius-sm);
    border: 1px solid var(--color-error-border);
    margin-top: var(--spacing-sm);
    font-size: 14px;
}

.modal-body input.invalid,
.modal-body textarea.invalid {
    border-color: var(--color-error);
    box-shadow: 0 0 0 2px rgba(var(--color-error-rgb), var(--opacity-10));
}

.dark-mode .modal-body input,
.dark-mode .modal-body textarea,
.dark-mode .modal-body select {
    background: var(--color-bg-tertiary);
    border-color: var(--color-border-medium);
    color: var(--color-text-primary);
}

.dark-mode .modal-body input:focus,
.dark-mode .modal-body textarea:focus,
.dark-mode .modal-body select:focus {
    border-color: var(--color-primary);
    background: var(--color-bg-primary);
    box-shadow: 0 0 0 2px rgba(var(--color-primary-rgb), var(--opacity-30));
}

.dark-mode .modal-body input:hover,
.dark-mode .modal-body textarea:hover,
.dark-mode .modal-body select:hover {
    border-color: var(--color-border-dark);
    background: var(--color-bg-primary);
}

.dark-mode .modal-body label {
    color: var(--color-text-secondary);
    font-weight: 600;
}

.dark-mode .modal-body small {
    color: var(--color-text-tertiary);
}

.dark-mode .modal-body .error {
    background: var(--gradient-error);
    color: var(--color-text-light);
    border-color: var(--color-error);
}

.dark-mode .modal-body input.invalid,
.dark-mode .modal-body textarea.invalid {
    border-color: var(--color-error);
    background: var(--color-bg-primary);
    box-shadow: 0 0 0 2px rgba(var(--color-error-rgb), var(--opacity-20));
}

@media (prefers-color-scheme: dark) {
    html:not(.dark-mode) .modal-body input,
    html:not(.dark-mode) .modal-body textarea,
    html:not(.dark-mode) .modal-body select {
        background: #374151;
        border-color: #4b5563;
        color: #f9fafb;
    }

    html:not(.dark-mode) .modal-body input:focus,
    html:not(.dark-mode) .modal-body textarea:focus,
    html:not(.dark-mode) .modal-body select:focus {
        border-color: #3b82f6;
        background: #1f2937;
        box-shadow: 0 0 0 2px rgba(59, 130, 246, 0.3);
    }

    html:not(.dark-mode) .modal-body input:hover,
    html:not(.dark-mode) .modal-body textarea:hover,
    html:not(.dark-mode) .modal-body select:hover {
        border-color: #6b7280;
        background: #1f2937;
    }

    html:not(.dark-mode) .modal-body label {
        color: #d1d5db;
        font-weight: 600;
    }

    html:not(.dark-mode) .modal-body small {
        color: #9ca3af;
    }

    html:not(.dark-mode) .modal-body .error {
        background: linear-gradient(135deg, #7f1d1d 0%, #1f2937 100%);
        color: #f3f4f6;
        border-color: #ef4444;
    }

    html:not(.dark-mode) .modal-body input.invalid,
    html:not(.dark-mode) .modal-body textarea.invalid {
        border-color: #ef4444;
        background: #1f2937;
        box-shadow: 0 0 0 2px rgba(239, 68, 68, 0.2);
    }
}

@media (max-width: 768px) {
    .modal-content {
        margin: var(--spacing-lg);
        max-height: calc(100vh - 2 * var(--spacing-lg));
    }

    .modal-content.modal-small,
    .modal-content.modal-medium,
    .modal-content.modal-large {
        width: calc(100vw - 2 * var(--spacing-lg));
        max-width: none;
    }

    .modal-header,
    .modal-body,
    .modal-footer {
        padding-left: var(--spacing-lg);
        padding-right: var(--spacing-lg);
    }

    .modal-footer {
        flex-direction: column;
        align-items: stretch;
    }

    .modal-footer .btn {
        width: 100%;
        margin-bottom: var(--spacing-sm);
    }

    .modal-footer .btn:last-child {
        margin-bottom: 0;
    }

    .modal-close {
        top: var(--spacing-md);
        right: var(--spacing-md);
        width: 28px;
        height: 28px;
        font-size: 14px;
    }
}

@media (prefers-reduced-motion: reduce) {
    .modal,
    .modal-content {
        transition: opacity var(--transition-base);
    }

    .modal.modal-show .modal-content,
    .modal.modal-hide .modal-content {
        transform: none;
    }
}

.dark-mode .modal-backdrop {
    background: rgba(0, 0, 0, var(--opacity-70));
}

.dark-mode .modal-content {
    background: var(--color-bg-primary);
    box-shadow: var(--shadow-xl);
    border: 1px solid var(--color-border-light);
}

.dark-mode .modal-header {
    border-bottom-color: var(--color-border-light);
}

.dark-mode .modal-header h3 {
    color: var(--color-text-primary);
}

.dark-mode .modal-body {
    color: var(--color-text-secondary);
}

.dark-mode .modal-footer {
    border-top-color: var(--color-border-light);
}

.dark-mode .modal-close {
    background: var(--color-bg-tertiary);
    color: var(--color-text-secondary);
    border: 1px solid var(--color-border-medium);
}

.dark-mode .modal-close:hover {
    background: var(--color-bg-secondary);
    color: var(--color-text-primary);
    border-color: var(--color-border-dark);
}

.dark-mode .modal-close:focus {
    box-shadow: 0 0 0 2px var(--color-primary);
}

@media (prefers-color-scheme: dark) {
    html:not(.dark-mode) .modal-backdrop {
        background: rgba(0, 0, 0, var(--opacity-70));
    }

    html:not(.dark-mode) .modal-content {
        background: var(--color-bg-dark);
        box-shadow: var(--shadow-xl);
        border: 1px solid #4b5563;
    }

    html:not(.dark-mode) .modal-header {
        border-bottom-color: #4b5563;
    }

    html:not(.dark-mode) .modal-header h3 {
        color: #f9fafb;
    }

    html:not(.dark-mode) .modal-body {
        color: #d1d5db;
    }

    html:not(.dark-mode) .modal-footer {
        border-top-color: #4b5563;
    }

    html:not(.dark-mode) .modal-close {
        background: #374151;
        color: #d1d5db;
        border: 1px solid #4b5563;
    }

    html:not(.dark-mode) .modal-close:hover {
        background: #111827;
        color: #f9fafb;
        border-color: #6b7280;
    }

    html:not(.dark-mode) .modal-close:focus {
        box-shadow: 0 0 0 2px #3b82f6;
    }
}

@media (prefers-contrast: high) {
    .modal-content {
        border: 2px solid var(--color-border-dark);
    }

    .modal-close {
        border: 1px solid var(--color-text-secondary);
    }
}

@media print {
    .modal {
        display: none;
    }
}