/* ==========================================================================
   Shared Modal System
   Used across all pages for consistent modal appearance.
   ========================================================================== */

/* --- Overlay --- */
.modal-overlay {
    position: fixed;
    inset: 0;
    background: rgba(15, 23, 42, 0.8);
    display: flex;
    align-items: center;
    justify-content: center;
    z-index: 9999;
    opacity: 0;
    visibility: hidden;
    transition: all 0.3s cubic-bezier(0.4, 0, 0.2, 1);
}

@supports (backdrop-filter: blur(1px)) or (-webkit-backdrop-filter: blur(1px)) {
    .modal-overlay {
        background: rgba(15, 23, 42, 0.6);
        backdrop-filter: blur(4px);
        -webkit-backdrop-filter: blur(4px);
    }
}

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

html.dark .modal-overlay {
    background: rgba(0, 0, 0, 0.75);
}

/* --- Content Box --- */
.modal-content {
    background: var(--bg-white, #ffffff);
    border-radius: 24px;
    box-shadow: 0 25px 80px rgba(99, 102, 241, 0.25), 0 0 0 1px rgba(99, 102, 241, 0.1);
    width: 100%;
    max-width: 480px;
    margin: 16px;
    transform: scale(0.9) translateY(20px);
    transition: all 0.3s cubic-bezier(0.34, 1.56, 0.64, 1);
    overflow: hidden;
}

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

.modal-content.modal-medium {
    max-width: 600px;
}

.modal-content.modal-wide {
    max-width: 720px;
}

html.dark .modal-content {
    box-shadow: 0 25px 80px rgba(0, 0, 0, 0.5), 0 0 0 1px rgba(99, 102, 241, 0.3);
}

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

/* --- Header with gradient --- */
.modal-header {
    background: linear-gradient(135deg, #6366f1 0%, #8b5cf6 100%);
    padding: 24px 28px 20px;
    position: relative;
}

.modal-header::after {
    content: '';
    position: absolute;
    bottom: -20px;
    left: 0;
    right: 0;
    height: 40px;
    background: inherit;
    border-radius: 0 0 50% 50% / 0 0 100% 100%;
}

.modal-header-content {
    position: relative;
    z-index: 1;
    display: flex;
    justify-content: space-between;
    align-items: flex-start;
}

/* --- Header Icon --- */
.modal-icon {
    width: 48px;
    height: 48px;
    background: rgba(255, 255, 255, 0.2);
    border-radius: 14px;
    display: flex;
    align-items: center;
    justify-content: center;
    margin-bottom: 12px;
}

.modal-icon i {
    font-size: 22px;
    color: #fff;
}

/* --- Header Title & Subtitle --- */
.modal-title {
    font-size: 22px;
    font-weight: 700;
    color: #fff;
    margin: 0;
}

.modal-subtitle {
    font-size: 14px;
    color: rgba(255, 255, 255, 0.8);
    margin-top: 4px;
}

/* --- Close Button --- */
.modal-close {
    background: rgba(255, 255, 255, 0.15);
    border: none;
    width: 32px;
    height: 32px;
    border-radius: 10px;
    color: #fff;
    cursor: pointer;
    display: flex;
    align-items: center;
    justify-content: center;
    transition: 0.2s;
    position: relative;
    z-index: 2;
}

.modal-close:hover {
    background: rgba(255, 255, 255, 0.25);
    transform: rotate(90deg);
}

.modal-close i {
    font-size: 14px;
}

/* --- Body --- */
.modal-body {
    padding: 32px 28px 24px;
}

/* --- Footer --- */
.modal-footer {
    display: flex;
    justify-content: flex-end;
    gap: 0.75rem;
    padding: 16px 28px;
    border-top: 1px solid var(--border, #e2e8f0);
}

/* --- Footer Buttons --- */
.modal-btn-cancel {
    padding: 0.625rem 1.25rem;
    background: var(--bg-secondary, #f1f5f9);
    border: none;
    border-radius: 10px;
    color: var(--text-secondary, #64748b);
    font-size: 0.875rem;
    font-weight: 500;
    cursor: pointer;
    transition: all 0.2s;
}

.modal-btn-cancel:hover {
    background: var(--bg-tertiary, #e2e8f0);
    color: var(--text-primary, #1e293b);
}

.modal-btn-save {
    padding: 0.625rem 1.25rem;
    background: linear-gradient(135deg, #6366f1, #8b5cf6);
    border: none;
    border-radius: 10px;
    color: #fff;
    font-size: 0.875rem;
    font-weight: 600;
    cursor: pointer;
    transition: all 0.2s;
}

.modal-btn-save:hover {
    filter: brightness(1.1);
    transform: translateY(-1px);
}

.modal-btn-save:disabled {
    opacity: 0.5;
    cursor: not-allowed;
    transform: none;
}

/* --- Form elements inside modals --- */
:is(.modal-body, .modal-body-scroll) .form-row {
    display: flex;
    gap: 0.75rem;
    margin-bottom: 0.5rem;
}

:is(.modal-body, .modal-body-scroll) .form-group {
    display: flex;
    flex-direction: column;
    gap: 0.25rem;
    position: relative;
}

:is(.modal-body, .modal-body-scroll) .form-group.flex-1 { flex: 1; }
:is(.modal-body, .modal-body-scroll) .form-group.flex-2 { flex: 2; }

:is(.modal-body, .modal-body-scroll) .form-group label {
    font-size: 0.8rem;
    font-weight: 600;
    color: var(--text-secondary, #64748b);
}

:is(.modal-body, .modal-body-scroll) .form-group input,
:is(.modal-body, .modal-body-scroll) .form-group select,
:is(.modal-body, .modal-body-scroll) .form-group textarea {
    padding: 0.5rem 0.75rem;
    border: 1px solid var(--border, #e2e8f0);
    border-radius: 8px;
    font-size: 0.85rem;
    background: var(--bg-white, #fff);
    color: var(--text-primary, #1e293b);
    transition: border-color 0.2s;
}

:is(.modal-body, .modal-body-scroll) .form-group input:focus,
:is(.modal-body, .modal-body-scroll) .form-group select:focus,
:is(.modal-body, .modal-body-scroll) .form-group textarea:focus {
    outline: none;
    border-color: #6366f1;
    box-shadow: 0 0 0 3px rgba(99, 102, 241, 0.15);
}

:is(.modal-body, .modal-body-scroll) .form-group textarea {
    resize: vertical;
    min-height: 50px;
}

/* --- Settings groups (fleet-map, device-dashboard) --- */
:is(.modal-body, .modal-body-scroll) .settings-group {
    display: flex;
    align-items: center;
    justify-content: space-between;
    padding: 0.75rem 0;
    border-bottom: 1px solid var(--border, #e2e8f0);
}

:is(.modal-body, .modal-body-scroll) .settings-group:last-child {
    border-bottom: none;
}

:is(.modal-body, .modal-body-scroll) .settings-label {
    display: flex;
    align-items: center;
    gap: 0.5rem;
    font-size: 0.875rem;
    font-weight: 500;
    color: var(--text-primary, #1e293b);
}

:is(.modal-body, .modal-body-scroll) .settings-label i {
    color: var(--text-secondary, #64748b);
    width: 18px;
    text-align: center;
}

:is(.modal-body, .modal-body-scroll) .settings-select {
    padding: 0.4rem 0.75rem;
    border: 1px solid var(--border, #e2e8f0);
    border-radius: 8px;
    font-size: 0.85rem;
    background: var(--bg-white, #fff);
    color: var(--text-primary, #1e293b);
}

/* --- Toggle Switch --- */
.toggle-switch {
    position: relative;
    display: inline-block;
    width: 44px;
    height: 24px;
}

.toggle-switch input {
    opacity: 0;
    width: 0;
    height: 0;
}

.toggle-slider {
    position: absolute;
    cursor: pointer;
    inset: 0;
    background: var(--bg-secondary, #e2e8f0);
    border-radius: 24px;
    transition: 0.3s;
}

.toggle-slider:before {
    position: absolute;
    content: "";
    height: 18px;
    width: 18px;
    left: 3px;
    bottom: 3px;
    background: white;
    border-radius: 50%;
    transition: 0.3s;
    box-shadow: 0 1px 3px rgba(0, 0, 0, 0.2);
}

.toggle-switch input:checked + .toggle-slider {
    background: var(--primary, #6366f1);
}

.toggle-switch input:checked + .toggle-slider:before {
    transform: translateX(20px);
}

/* --- Scrollable body for large modals --- */
.modal-body-scroll {
    padding: 32px 28px 24px;
    max-height: 60vh;
    overflow-y: auto;
}

/* --- Mobile responsive --- */
@media (max-width: 640px) {
    .modal-content {
        max-width: calc(100% - 32px);
        border-radius: 20px;
    }

    .modal-header {
        padding: 20px 20px 16px;
    }

    .modal-body,
    .modal-body-scroll {
        padding: 24px 20px 20px;
    }

    .modal-footer {
        padding: 12px 20px;
    }

    :is(.modal-body, .modal-body-scroll) .form-row {
        flex-direction: column;
    }

    .modal-title {
        font-size: 18px;
    }
}
