/* ========================================
   MODAL SYSTEM - REUSABLE ACROSS APP
   ======================================== */

/* Modal Overlay */
.modal {
    display: none;
    position: fixed;
    top: 0;
    left: 0;
    width: 100%;
    height: 100%;
    background: rgba(0, 0, 0, 0.6);
    backdrop-filter: blur(4px);
    z-index: 1000;
    align-items: center;
    justify-content: center;
    opacity: 0;
    transition: opacity 0.3s ease;
}

/* Inert attribute support for better accessibility */
.modal[inert] {
    pointer-events: none;
}

.modal[inert] * {
    pointer-events: none;
}

.modal.show {
    display: flex;
    opacity: 1;
}

/* Modal Content */
.modal-content {
    background: var(--bg-primary, #ffffff);
    border-radius: 16px;
    box-shadow: 
        0 25px 50px -12px rgba(0, 0, 0, 0.25),
        0 0 0 1px rgba(255, 255, 255, 0.05);
    max-width: 500px;
    width: 90%;
    max-height: 90vh;
    min-height: auto; /* Allow modal to be as small as needed */
    overflow: hidden; /* Prevent modal from overflowing viewport */
    transform: scale(0.95) translateY(20px);
    transition: transform 0.3s ease;
    position: relative;
    padding-bottom: 0; /* Ensure no extra padding on content */
    display: flex;
    flex-direction: column; /* Ensure proper flex layout for header, body, footer */
}

.modal.show .modal-content {
    transform: scale(1) translateY(0);
}

/* Modal Header */
.modal-header {
    display: flex;
    justify-content: space-between;
    align-items: center;
    padding: 1.5rem 2rem;
    border-bottom: 1px solid var(--border-color, #e2e8f0);
    background: var(--bg-primary, #ffffff);
    flex-shrink: 0; /* Prevent header from shrinking */
}

.modal-header h3 {
    margin: 0;
    color: var(--text-primary, #1e293b);
    font-size: 1.25rem;
    font-weight: 600;
    line-height: 1.4;
}

.modal-close {
    background: none;
    border: none;
    color: var(--text-secondary, #64748b);
    cursor: pointer;
    padding: 0.5rem;
    border-radius: 8px;
    transition: all 0.2s ease;
    display: flex;
    align-items: center;
    justify-content: center;
    width: 32px;
    height: 32px;
    font-size: 1.25rem;
    line-height: 1;
}

.modal-close:hover {
    background: var(--hover-bg, #f8fafc);
    color: var(--text-primary, #1e293b);
    transform: scale(1.1);
}

/* Modal Body */
.modal-body {
    padding: 2rem;
    color: var(--text-primary, #1e293b);
    line-height: 1.6;
    flex: 1; /* Allow modal body to grow and fill available space */
    overflow-y: auto; /* Allow scrolling if content is too long */
    min-height: 0; /* Allow flex item to shrink below content size */
}

.modal-body p {
    margin: 0 0 1rem 0;
}

.modal-body p:last-child {
    margin-bottom: 0;
}

/* Modal Footer */
.modal-footer {
    display: flex;
    justify-content: flex-end;
    gap: 0.75rem;
    padding: 1.5rem 2rem; /* Remove extra bottom padding to prevent footer from being cut off */
    border-top: 1px solid var(--border-color, #e2e8f0);
    background: var(--bg-secondary, #f8fafc);
    align-items: stretch; /* Ensure buttons have same height */
    flex-shrink: 0; /* Prevent footer from shrinking */
}

/* Ensure buttons in modal footer have proper spacing and alignment */
.modal-footer .btn {
    margin-bottom: 0; /* Remove bottom margin to prevent buttons from being cut off */
    text-align: center; /* Center text within buttons */
}

/* Specific fix for modals without proper footer structure */
.modal-content form {
    padding-bottom: 0; /* Remove bottom padding to prevent footer from being cut off */
    display: flex;
    flex-direction: column;
    height: 100%;
    min-height: 0; /* Allow form to shrink if needed */
}

/* Ensure last element in modal body has proper spacing */
.modal-body > *:last-child {
    margin-bottom: 1rem;
}


.modal-body::-webkit-scrollbar {
    width: 6px;
}

.modal-body::-webkit-scrollbar-track {
    background: var(--bg-secondary, #f1f5f9);
    border-radius: 3px;
}

.modal-body::-webkit-scrollbar-thumb {
    background: var(--border-color, #cbd5e1);
    border-radius: 3px;
}

.modal-body::-webkit-scrollbar-thumb:hover {
    background: var(--text-secondary, #94a3b8);
}

/* Specific fix for edit user modal and similar forms */
#editUserModal .modal-content,
#editWorkplaceModal .modal-content {
    padding-bottom: 0 ; /* Remove bottom padding to prevent footer from being cut off */
}

/* Ensure buttons at the bottom of forms have proper spacing */
.modal-content form .btn:last-child,
.modal-content .btn:last-child {
    margin-bottom: 0 ; /* Remove bottom margin to prevent buttons from being cut off */
}

/* Equal width buttons in modal footers - when there are multiple buttons */
.modal-footer .btn + .btn {
    flex: 1 1 0 ; /* Equal width for multiple buttons */
    min-width: 0 ; /* Allow flex to control width */
    max-width: none ; /* Remove max-width constraint */
    width: auto ; /* Let flex control the width */
    height: 44px ; /* Force exact same height */
    min-height: 44px ;
    max-height: 44px ;
    display: flex ;
    align-items: center ;
    justify-content: center ;
    box-sizing: border-box ;
}

/* Apply to all buttons in modal footer when there are multiple */
.modal-footer .btn:first-child {
    flex: 1 1 0 ;
    min-width: 0 ;
    max-width: none ;
    width: auto ;
    height: 44px ; /* Force exact same height */
    min-height: 44px ;
    max-height: 44px ;
    display: flex ;
    align-items: center ;
    justify-content: center ;
    box-sizing: border-box ;
}

/* Ensure buttons in forms have consistent sizing */
.modal-content form .btn {
    min-width: 120px;
    text-align: center;
}

/* Specific fix for edit user modal buttons */
#editUserModal .modal-footer .btn,
#editWorkplaceModal .modal-footer .btn {
    flex: 1 1 0 ;
    min-width: 0 ;
    max-width: none ;
    width: auto ;
    height: 44px ; /* Force exact same height */
    min-height: 44px ;
    max-height: 44px ;
    text-align: center ;
    display: flex ;
    align-items: center ;
    justify-content: center ;
    box-sizing: border-box ;
}

/* Force equal width for any buttons in modal footers */
.modal-footer {
    display: flex ;
    gap: 0.75rem ;
    align-items: stretch ; /* Ensure buttons stretch to same height */
}

.modal-footer .btn {
    flex: 1 1 0 ;
    min-width: 0 ;
    max-width: none ;
    width: auto ;
    height: 44px ; /* Force exact same height */
    min-height: 44px ;
    max-height: 44px ;
    display: flex ;
    align-items: center ;
    justify-content: center ;
    box-sizing: border-box ;
}

/* ========================================
   MODERN BUTTON STYLES
   ======================================== */

/* Base Button Styles */
.btn {
    display: inline-flex;
    align-items: center;
    justify-content: center;
    padding: 0.75rem 1.5rem;
    border: none;
    border-radius: 10px;
    font-size: 0.9rem;
    font-weight: 500;
    line-height: 1.2;
    cursor: pointer;
    transition: all 0.2s ease;
    text-decoration: none;
    min-height: 44px;
    height: 44px; /* Fixed height for consistency */
    gap: 0.5rem;
    position: relative;
    overflow: hidden;
    box-sizing: border-box; /* Include padding in height calculation */
}

.btn:focus {
    outline: none;
    box-shadow: 0 0 0 3px rgba(59, 130, 246, 0.1);
}

.btn:disabled {
    opacity: 0.6;
    cursor: not-allowed;
    transform: none ;
}

/* Button Variants */
.btn-primary {
    background: linear-gradient(135deg, #3f51b5 0%, #1d4ed8 100%);
    color: white;
    box-shadow: 0 4px 14px 0 rgba(59, 130, 246, 0.3);
}

.btn-primary:hover:not(:disabled) {
    background: linear-gradient(135deg, #3035a5 0%, #1e40af 100%);
    transform: translateY(-1px);
    box-shadow: 0 6px 20px 0 rgba(59, 130, 246, 0.4);
}

.btn-secondary {
    background: var(--bg-primary, #ffffff);
    color: var(--text-primary, #374151);
    border: 1px solid var(--border-color, #d1d5db);
    box-shadow: 0 1px 3px 0 rgba(0, 0, 0, 0.1);
}

.btn-secondary:hover:not(:disabled) {
    background: var(--hover-bg, #f9fafb);
    border-color: var(--border-hover, #9ca3af);
    transform: translateY(-1px);
    box-shadow: 0 4px 12px 0 rgba(0, 0, 0, 0.15);
}

.btn-danger {
    background: linear-gradient(135deg, #ef4444 0%, #dc2626 100%);
    color: white;
    box-shadow: 0 4px 14px 0 rgba(239, 68, 68, 0.3);
}

.btn-danger:hover:not(:disabled) {
    background: linear-gradient(135deg, #dc2626 0%, #b91c1c 100%);
    transform: translateY(-1px);
    box-shadow: 0 6px 20px 0 rgba(239, 68, 68, 0.4);
}

.btn-success {
    background: linear-gradient(135deg, #10b981 0%, #059669 100%);
    color: white;
    box-shadow: 0 4px 14px 0 rgba(16, 185, 129, 0.3);
}

.btn-success:hover:not(:disabled) {
    background: linear-gradient(135deg, #059669 0%, #047857 100%);
    transform: translateY(-1px);
    box-shadow: 0 6px 20px 0 rgba(16, 185, 129, 0.4);
}

/* Button Sizes */
.btn-sm {
    padding: 0.5rem 1rem;
    font-size: 0.8rem;
    min-height: 36px;
}

.btn-lg {
    padding: 1rem 2rem;
    font-size: 1rem;
    min-height: 52px;
}

/* ========================================
   DARK MODE SUPPORT
   ======================================== */

.dark-mode .modal {
    background: rgba(0, 0, 0, 0.8);
}

.dark-mode .modal-content {
    background: var(--bg-primary, #1f2937);
    border: 1px solid var(--border-color, #374151);
}

.dark-mode .modal-header {
    background: var(--bg-primary, #1f2937);
    border-bottom-color: var(--border-color, #374151);
}

.dark-mode .modal-header h3 {
    color: var(--text-primary, #f9fafb);
}

.dark-mode .modal-close {
    color: var(--text-secondary, #9ca3af);
}

.dark-mode .modal-close:hover {
    background: var(--hover-bg, #374151);
    color: var(--text-primary, #f9fafb);
}

.dark-mode .modal-body {
    color: var(--text-primary, #f9fafb);
}

.dark-mode .modal-footer {
    background: var(--bg-secondary, #111827);
    border-top-color: var(--border-color, #374151);
}

.dark-mode .btn-secondary {
    background: var(--bg-primary, #374151);
    color: var(--text-primary, #f9fafb);
    border-color: var(--border-color, #4b5563);
}

.dark-mode .btn-secondary:hover:not(:disabled) {
    background: var(--hover-bg, #4b5563);
    border-color: var(--border-hover, #6b7280);
}

/* ========================================
   RESPONSIVE DESIGN
   ======================================== */

@media (max-width: 640px) {
    .modal-content {
        width: 95%;
        margin: 1rem;
        max-height: calc(100vh - 2rem);
        min-height: auto; /* Allow modal to be smaller on mobile */
    }
    
    .modal-header,
    .modal-body {
        padding: 1rem 1.5rem;
    }
    
    .modal-footer {
        padding: 1rem 1.5rem; /* Remove extra bottom padding on mobile */
        flex-direction: column;
        gap: 0.5rem;
    }
    
    .modal-footer .btn {
        width: 100%;
        min-width: auto; /* Remove min-width on mobile */
        max-width: none; /* Remove max-width on mobile */
        justify-content: center;
        flex: none; /* Reset flex on mobile */
    }
}

/* ========================================
   SPECIAL MODAL TYPES
   ======================================== */

/* Confirmation Modal */
.modal-confirmation .modal-content {
    max-width: 400px;
}

.modal-confirmation .modal-body {
    text-align: center;
}

.modal-confirmation .modal-body strong {
    color: var(--text-primary, #1e293b);
    font-weight: 600;
}

.modal-confirmation .modal-footer {
    justify-content: center;
    gap: 1rem;
}

/* Large Modal */
.modal-large .modal-content {
    max-width: 700px;
}

/* Small Modal */
.modal-small .modal-content {
    max-width: 350px;
}

/* Dynamic Modal Sizing */
.modal-content {
    /* Auto-adjust height based on content */
    height: auto;
}

/* For modals with minimal content, ensure they don't take up too much space */
.modal-content:not(.modal-large) {
    min-height: auto;
}

/* Ensure modals with lots of form fields can scroll properly */
.modal-content form .modal-body {
    max-height: 60vh; /* Limit body height to allow for header and footer */
    overflow-y: auto;
}

/* Form input styling within modals */
.modal .form-group input,
.modal .form-group select,
.modal .form-group textarea {
    width: 100%;
    box-sizing: border-box; /* Ensure padding is included in width calculation */
    margin: 0; /* Remove any default margins */
}

/* Ensure form groups have proper spacing */
.modal .form-group {
    margin-bottom: 1.5rem;
}

.modal .form-group:last-child {
    margin-bottom: 0;
}

/* ========================================
   ANIMATION KEYFRAMES
   ======================================== */

@keyframes modalSlideIn {
    from {
        opacity: 0;
        transform: scale(0.9) translateY(-20px);
    }
    to {
        opacity: 1;
        transform: scale(1) translateY(0);
    }
}

@keyframes modalSlideOut {
    from {
        opacity: 1;
        transform: scale(1) translateY(0);
    }
    to {
        opacity: 0;
        transform: scale(0.9) translateY(-20px);
    }
}

/* ========================================
   UTILITY CLASSES
   ======================================== */

.modal-backdrop {
    position: fixed;
    top: 0;
    left: 0;
    width: 100%;
    height: 100%;
    background: rgba(0, 0, 0, 0.6);
    backdrop-filter: blur(4px);
    z-index: 999;
}

.text-center {
    text-align: center;
}

.text-muted {
    color: var(--text-secondary, #6b7280);
    font-size: 0.875rem;
}

.mb-0 {
    margin-bottom: 0;
}

.mt-1 {
    margin-top: 0.5rem;
}
