/**
 * Global Modal System Styles
 * Responsive design for desktop, iPad, and mobile devices
 * Proper z-index management and animations
 */

/* ============================================
   Base Modal Styles
   ============================================ */

/* Force dark text on white background for all global modals */
.global-modal {
    color: #111827; /* Base text color */
}

/* Ensure modal content has proper colors */
.global-modal .modal {
    background: white !important;
    color: #111827 !important;
}

.global-modal .modal-header,
.global-modal .modal-body,
.global-modal .modal-footer {
    color: #111827 !important;
}

/* Ensure modal messages are dark enough to read */
.global-modal .modal-message {
    color: #374151 !important; /* Dark gray for good readability */
}

.global-modal .modal-title {
    color: #111827 !important; /* Near black for titles */
}

/* Button color overrides with proper specificity */
.global-modal .btn-primary {
    background: #3b82f6 !important;
    color: white !important;
    border: none !important;
}

.global-modal .btn-primary:hover:not(:disabled) {
    background: #2563eb !important;
    color: white !important;
}

.global-modal .btn-secondary {
    background: #f3f4f6 !important;
    color: #374151 !important;
    border: none !important;
}

.global-modal .btn-secondary:hover:not(:disabled) {
    background: #e5e7eb !important;
    color: #111827 !important;
}

.global-modal .btn-danger {
    background: #ef4444 !important;
    color: white !important;
    border: none !important;
}

.global-modal .btn-danger:hover:not(:disabled) {
    background: #dc2626 !important;
    color: white !important;
}

/* Modal close button */
.global-modal .modal-close {
    color: #6b7280 !important;
    background: transparent !important;
    min-width: 32px !important;
    min-height: 32px !important;
    display: flex !important;
    align-items: center !important;
    justify-content: center !important;
    font-size: 24px !important; /* For × character */
    line-height: 1 !important;
    font-weight: normal !important;
}

.global-modal .modal-close:hover {
    color: #374151 !important;
    background: #f3f4f6 !important;
}

/* Ensure SVG icons in close button are sized correctly */
.global-modal .modal-close svg {
    width: 24px !important;
    height: 24px !important;
}

/* Disabled button states */
.global-modal .btn:disabled {
    opacity: 0.5 !important;
    cursor: not-allowed !important;
}

/* Links in modals */
.global-modal a {
    color: #3b82f6 !important;
    text-decoration: underline;
}

.global-modal a:hover {
    color: #2563eb !important;
}

/* Additional button variants that might be used */
.global-modal .btn-success {
    background: #10b981 !important;
    color: white !important;
    border: none !important;
}

.global-modal .btn-success:hover:not(:disabled) {
    background: #059669 !important;
    color: white !important;
}

.global-modal .btn-warning {
    background: #f59e0b !important;
    color: white !important;
    border: none !important;
}

.global-modal .btn-warning:hover:not(:disabled) {
    background: #d97706 !important;
    color: white !important;
}

.global-modal .btn-info {
    background: #0ea5e9 !important;
    color: white !important;
    border: none !important;
}

.global-modal .btn-info:hover:not(:disabled) {
    background: #0284c7 !important;
    color: white !important;
}

/* Global modal overlay - use specific class to avoid conflicts */
.modal-overlay.global-modal {
    position: fixed;
    top: 0;
    left: 0;
    right: 0;
    bottom: 0;
    background: rgba(0, 0, 0, 0.5);
    display: flex;
    align-items: center;
    justify-content: center;
    padding: 20px;
    overflow-y: auto;
    -webkit-overflow-scrolling: touch; /* Smooth scrolling on iOS */
    opacity: 0;
    visibility: hidden;
    transition: opacity 0.15s ease, visibility 0.15s ease;
}

/* Support for order modals - they can override background if needed */
.modal-overlay:not(.global-modal) {
    /* Order modals use their own styles from order-modals.css */
}

/* Order modal specific styles when using GlobalModal */
.modal-overlay.global-modal .modal.order-modal-global,
.modal-overlay.global-modal .modal.modal-production {
    /* Preserve original order modal dimensions */
    width: 95vw !important;
    max-width: 1200px !important;
    height: 90vh !important;
    max-height: 90vh !important;
    background: white;
    border-radius: 12px;
    overflow: hidden;
    display: flex;
    flex-direction: column;
}

/* Simple order modal for direct GlobalModal usage */
.modal-overlay.global-modal .modal.order-modal-simple {
    width: 90vw !important;
    max-width: 800px !important;
    max-height: 80vh !important;
    background: white;
    border-radius: 12px;
    overflow: auto;
}

.order-modal-header {
    display: flex;
    justify-content: space-between;
    align-items: center;
    padding: 20px;
    border-bottom: 1px solid #e5e7eb;
    background: #f9fafb;
}

.order-modal-header h2 {
    margin: 0;
    color: #111827;
}

.order-modal-header .close-btn {
    width: 32px;
    height: 32px;
    border: none;
    background: none;
    font-size: 24px;
    cursor: pointer;
    color: #6b7280;
}

.order-modal-header .close-btn:hover {
    color: #111827;
}

.order-modal-body {
    flex: 1;
    overflow-y: auto;
    color: #374151;
}

.order-modal-footer {
    background: #f9fafb;
}

.modal-overlay.modal-visible {
    opacity: 1 !important;
    visibility: visible !important;
    display: flex !important;
}

/* Global modal content box */
.global-modal .modal {
    background: white;
    color: #111827; /* Ensure dark text on white background */
    border-radius: 12px;
    box-shadow: 0 20px 25px -5px rgba(0, 0, 0, 0.1), 0 10px 10px -5px rgba(0, 0, 0, 0.04);
    max-width: 600px;
    width: 100%;
    max-height: 90vh;
    display: flex;
    flex-direction: column;
    position: relative;
    transform: scale(0.95) translateY(20px);
    transition: transform 0.15s ease;
}

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

/* Animation classes */
.modal-overlay.modal-animating {
    pointer-events: none;
}

.modal-overlay.modal-in {
    animation: modalFadeIn 0.15s ease forwards;
}

.modal-overlay.modal-out {
    animation: modalFadeOut 0.15s ease forwards;
}

@keyframes modalFadeIn {
    from {
        opacity: 0;
    }
    to {
        opacity: 1;
    }
}

@keyframes modalFadeOut {
    from {
        opacity: 1;
    }
    to {
        opacity: 0;
    }
}

/* ============================================
   Modal Structure
   ============================================ */

.modal-header {
    padding: 20px 24px;
    border-bottom: 1px solid #e5e7eb;
    display: flex;
    align-items: center;
    justify-content: space-between;
    flex-shrink: 0;
}

.modal-title {
    margin: 0;
    font-size: 20px;
    font-weight: 600;
    color: #111827;
    line-height: 1.3;
}

.modal-close {
    background: none;
    border: none;
    padding: 8px;
    cursor: pointer;
    color: #6b7280;
    transition: color 0.2s ease, background-color 0.2s ease;
    border-radius: 6px;
    display: flex;
    align-items: center;
    justify-content: center;
    margin-left: 12px;
    flex-shrink: 0;
}

.modal-close:hover {
    color: #374151;
    background-color: #f3f4f6;
}

.modal-close:active {
    background-color: #e5e7eb;
}

.modal-body {
    padding: 24px;
    overflow-y: auto;
    flex: 1;
    -webkit-overflow-scrolling: touch;
    color: #374151; /* Ensure body text is dark */
}

.modal-footer {
    padding: 16px 24px;
    border-top: 1px solid #e5e7eb;
    display: flex;
    gap: 12px;
    justify-content: flex-end;
    flex-shrink: 0;
}

/* ============================================
   Modal Types
   ============================================ */

/* Confirm Modal */
.modal-confirm .modal-body,
.modal-alert .modal-body {
    text-align: center;
    padding: 32px 24px;
}

.modal-icon {
    margin-bottom: 16px;
    color: #3b82f6;
}

.modal-message {
    margin: 0;
    font-size: 16px;
    line-height: 1.5;
    color: #374151; /* Changed from #4b5563 to darker gray for better readability */
}

/* Form Modal */
.modal-form .modal-body {
    padding: 24px;
}

.form-group {
    margin-bottom: 20px;
}

.form-group:last-child {
    margin-bottom: 0;
}

.form-group label {
    display: block;
    margin-bottom: 8px;
    font-weight: 500;
    color: #374151;
    font-size: 14px;
}

.form-group input,
.form-group textarea,
.form-group select {
    width: 100%;
    padding: 10px 12px;
    border: 1px solid #d1d5db;
    border-radius: 6px;
    font-size: 14px;
    transition: border-color 0.2s ease, box-shadow 0.2s ease;
    background: white;
    color: #111827;
}

/* Ensure proper colors in global modals */
.global-modal .form-group input,
.global-modal .form-group textarea,
.global-modal .form-group select {
    background: white !important;
    color: #111827 !important;
    border: 1px solid #d1d5db !important;
}

.global-modal .form-group input::placeholder,
.global-modal .form-group textarea::placeholder {
    color: #9ca3af !important;
}

.form-group input:focus,
.form-group textarea:focus,
.form-group select:focus {
    outline: none;
    border-color: #3b82f6;
    box-shadow: 0 0 0 3px rgba(59, 130, 246, 0.1);
}

.form-group textarea {
    min-height: 100px;
    resize: vertical;
}

/* Loading Modal */
.modal-loading {
    padding: 40px;
    text-align: center;
    background: white;
    color: #111827; /* Ensure text is dark */
    min-width: 200px;
}

.modal-loading-overlay {
    background: rgba(0, 0, 0, 0.7);
}

.modal-spinner {
    margin-bottom: 20px;
}

.spinner {
    width: 40px;
    height: 40px;
    border: 4px solid #e5e7eb;
    border-top-color: #3b82f6;
    border-radius: 50%;
    animation: spin 1s linear infinite;
    margin: 0 auto;
}

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

.modal-loading .modal-message {
    color: #374151;
    font-size: 16px;
}

/* Image Modal */
.modal-image {
    max-width: 90vw;
    max-height: 90vh;
}

.modal-image .modal-body {
    padding: 0;
    display: flex;
    flex-direction: column;
    align-items: center;
}

.modal-image img {
    max-width: 100%;
    max-height: 70vh;
    object-fit: contain;
    display: block;
}

.modal-caption {
    padding: 16px 24px;
    margin: 0;
    color: #6b7280;
    text-align: center;
    font-size: 14px;
}

/* ============================================
   Buttons
   ============================================ */

.btn {
    padding: 10px 20px;
    border-radius: 6px;
    font-size: 14px;
    font-weight: 500;
    cursor: pointer;
    transition: all 0.2s ease;
    border: none;
    outline: none;
    display: inline-flex;
    align-items: center;
    justify-content: center;
    gap: 8px;
    min-height: 40px;
    white-space: nowrap;
}

.btn:disabled {
    opacity: 0.5;
    cursor: not-allowed;
}

.btn-primary {
    background: #3b82f6;
    color: white;
}

.btn-primary:hover:not(:disabled) {
    background: #2563eb;
}

.btn-primary:active:not(:disabled) {
    background: #1d4ed8;
}

.btn-secondary {
    background: #f3f4f6;
    color: #374151;
}

.btn-secondary:hover:not(:disabled) {
    background: #e5e7eb;
}

.btn-secondary:active:not(:disabled) {
    background: #d1d5db;
}

.btn-danger {
    background: #ef4444;
    color: white;
}

.btn-danger:hover:not(:disabled) {
    background: #dc2626;
}

/* ============================================
   Z-Index Management
   ============================================ */

/* Base z-index levels */
.modal-overlay {
    z-index: 9000; /* Base modal z-index */
}

/* Each subsequent modal gets higher z-index (handled by JS) */

/* ============================================
   iPad Specific Styles
   ============================================ */

@media (min-width: 768px) and (max-width: 1024px) {
    /* iPad portrait and landscape */
    .modal {
        max-width: 700px;
        margin: 40px auto;
    }

    .modal-header {
        padding: 24px;
    }

    .modal-close {
        padding: 12px;
        min-width: 44px;
        min-height: 44px; /* Apple's recommended touch target size */
    }

    .modal-body {
        padding: 24px;
    }

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

    .btn {
        min-height: 44px; /* Apple's recommended touch target size */
        padding: 12px 24px;
        font-size: 16px; /* Prevent zoom on iOS */
    }

    .form-group input,
    .form-group textarea,
    .form-group select {
        font-size: 16px; /* Prevent zoom on iOS */
        padding: 12px;
        min-height: 44px;
    }

    /* Better scrolling on iPad */
    .modal-body {
        -webkit-overflow-scrolling: touch;
        scroll-behavior: smooth;
    }
}

/* iPad specific touch class */
.modal-touch .modal-close,
.modal-touch .btn {
    -webkit-tap-highlight-color: transparent;
}

.modal-ios .modal {
    /* Prevent rubber band scrolling on iOS */
    overscroll-behavior: contain;
}

/* ============================================
   Customer Note Modal Specific Styles
   ============================================ */

.modal-overlay.customer-note-modal {
    display: flex !important;
    align-items: center !important;
    justify-content: center !important;
    background: transparent !important;
    backdrop-filter: none !important;
}

.modal-overlay.customer-note-modal .modal-content {
    background: transparent !important;
    box-shadow: none !important;
    max-width: 500px !important;
    padding: 0 !important;
    width: auto !important;
}

.order-note-panel {
    animation: noteModalSlideIn 0.3s ease-out;
}

@keyframes noteModalSlideIn {
    from {
        transform: translateY(-30px) scale(0.95);
        opacity: 0;
    }
    to {
        transform: translateY(0) scale(1);
        opacity: 1;
    }
}

/* ============================================
   Mobile Styles
   ============================================ */

@media (max-width: 767px) {
    .modal-overlay {
        padding: 0;
        align-items: flex-end;
    }

    .modal {
        max-width: 100%;
        width: 100%;
        max-height: 95vh;
        border-radius: 12px 12px 0 0;
        margin: 0;
        transform: translateY(100%);
    }

    .modal-overlay.modal-visible .modal {
        transform: translateY(0);
    }

    .modal-mobile .modal {
        animation: slideUp 0.3s ease forwards;
    }

    @keyframes slideUp {
        from {
            transform: translateY(100%);
        }
        to {
            transform: translateY(0);
        }
    }

    .modal-header {
        padding: 16px;
        position: sticky;
        top: 0;
        background: white;
        z-index: 10;
        border-radius: 12px 12px 0 0;
    }

    /* Swipe indicator for mobile */
    .modal-mobile .modal::before {
        content: '';
        position: absolute;
        top: 8px;
        left: 50%;
        transform: translateX(-50%);
        width: 36px;
        height: 4px;
        background: #d1d5db;
        border-radius: 2px;
    }

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

    .modal-close {
        min-width: 44px;
        min-height: 44px;
    }

    .modal-body {
        padding: 16px;
    }

    .modal-footer {
        padding: 16px;
        position: sticky;
        bottom: 0;
        background: white;
        border-top: 1px solid #e5e7eb;
        flex-direction: column-reverse;
        gap: 8px;
    }

    .modal-footer .btn {
        width: 100%;
        min-height: 48px;
        font-size: 16px;
    }

    /* Full screen modals on mobile */
    .modal-fullscreen-mobile {
        max-height: 100vh;
        height: 100vh;
        border-radius: 0;
    }

    /* Form adjustments for mobile */
    .form-group label {
        font-size: 16px;
    }

    .form-group input,
    .form-group textarea,
    .form-group select {
        font-size: 16px; /* Prevent zoom on iOS */
        padding: 14px;
        min-height: 48px;
    }

    /* Prevent body scroll when modal is open on mobile */
    body.modal-open {
        overflow: hidden;
        /* Don't use position: fixed as it causes background shift */
    }
}

/* ============================================
   Desktop Styles (Enhancement)
   ============================================ */

@media (min-width: 1025px) {
    .modal {
        max-width: 600px;
    }

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

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

    .modal-sm {
        max-width: 400px;
    }

    /* Hover effects only on desktop */
    .modal-close:hover {
        transform: scale(1.1);
    }

    .btn:hover:not(:disabled) {
        transform: translateY(-1px);
        box-shadow: 0 4px 6px -1px rgba(0, 0, 0, 0.1), 0 2px 4px -1px rgba(0, 0, 0, 0.06);
    }

    .btn:active:not(:disabled) {
        transform: translateY(0);
        box-shadow: none;
    }
}

/* ============================================
   Accessibility
   ============================================ */

/* Focus styles */
.modal-close:focus,
.btn:focus {
    outline: none;
    box-shadow: 0 0 0 3px rgba(59, 130, 246, 0.5);
}

/* Reduced motion */
@media (prefers-reduced-motion: reduce) {
    .modal-overlay,
    .modal,
    .btn {
        transition: none;
        animation: none;
    }
}

/* High contrast mode */
@media (prefers-contrast: high) {
    .modal {
        border: 2px solid #000;
    }

    .modal-header,
    .modal-footer {
        border-color: #000;
    }

    .btn {
        border: 2px solid currentColor;
    }
}

/* Dark mode support - only apply if not a global modal */
@media (prefers-color-scheme: dark) {
    .modal:not(.global-modal .modal) {
        background: #1f2937;
        color: #f9fafb;
    }

    /* Global modals stay light even in dark mode */
    .global-modal .modal {
        background: white !important;
        color: #111827 !important;
    }

    .modal-header,
    .modal-footer {
        border-color: #374151;
    }

    .modal:not(.global-modal .modal) .modal-title {
        color: #f9fafb;
    }

    .modal:not(.global-modal .modal) .modal-message {
        color: #d1d5db;
    }

    .modal:not(.global-modal .modal) .modal-close {
        color: #9ca3af;
    }

    .modal:not(.global-modal .modal) .modal-close:hover {
        color: #f9fafb;
        background-color: #374151;
    }

    .modal:not(.global-modal .modal) .form-group label {
        color: #d1d5db;
    }

    .modal:not(.global-modal .modal) .form-group input,
    .modal:not(.global-modal .modal) .form-group textarea,
    .modal:not(.global-modal .modal) .form-group select {
        background: #374151;
        color: #f9fafb;
        border-color: #4b5563;
    }

    .modal:not(.global-modal .modal) .btn-secondary {
        background: #374151;
        color: #f9fafb;
    }

    .modal:not(.global-modal .modal) .btn-secondary:hover:not(:disabled) {
        background: #4b5563;
    }
}

/* =================================================================
   Transfer Modal Specific Styles (InOut Page)
   ================================================================= */

.out-transfer-modal-global .transfer-options {
    display: grid;
    grid-template-columns: repeat(auto-fit, minmax(150px, 1fr));
    gap: 15px;
    margin-top: 20px;
}

.out-transfer-modal-global .transfer-btn {
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    padding: 20px 15px;
    background: #f9fafb;
    border: 2px solid #e5e7eb;
    border-radius: 12px;
    color: #374151;
    font-weight: 600;
    font-size: 14px;
    cursor: pointer;
    transition: all 0.2s ease;
    min-height: 120px;
}

.out-transfer-modal-global .transfer-btn:hover {
    background: #e5e7eb;
    border-color: #9ca3af;
    transform: translateY(-2px);
}

.out-transfer-modal-global .transfer-btn:active {
    transform: translateY(0);
}

.out-transfer-modal-global .transfer-btn.customer-btn {
    background: #dcfce7;
    border-color: #86efac;
}

.out-transfer-modal-global .transfer-btn.customer-btn:hover {
    background: #bbf7d0;
    border-color: #4ade80;
}

.out-transfer-modal-global .transfer-btn.driver-btn {
    background: #dbeafe;
    border-color: #93c5fd;
}

.out-transfer-modal-global .transfer-btn.driver-btn:hover {
    background: #bfdbfe;
    border-color: #60a5fa;
}

.out-transfer-modal-global .transfer-btn svg {
    width: 32px;
    height: 32px;
    margin-bottom: 8px;
    opacity: 0.8;
}

.out-transfer-modal-global .transfer-btn span {
    text-align: center;
}

.out-transfer-modal-global .transfer-info {
    font-size: 16px;
    color: #4b5563;
    margin: 0;
    font-weight: 500;
}

/* Driver Selection Modal Styles */
.driver-selection-modal-global .drivers-list {
    display: grid;
    grid-template-columns: repeat(auto-fill, minmax(200px, 1fr));
    gap: 12px;
    max-height: 400px;
    overflow-y: auto;
    padding: 10px;
}

.driver-selection-modal-global .driver-tile {
    display: flex;
    align-items: center;
    gap: 12px;
    padding: 12px 16px;
    background: #f9fafb;
    border: 2px solid #e5e7eb;
    border-radius: 8px;
    cursor: pointer;
    transition: all 0.2s ease;
}

.driver-selection-modal-global .driver-tile:hover {
    background: #e5e7eb;
    border-color: #9ca3af;
}

.driver-selection-modal-global .driver-tile svg {
    width: 24px;
    height: 24px;
    flex-shrink: 0;
    opacity: 0.7;
}

.driver-selection-modal-global .driver-tile span {
    color: #374151;
    font-weight: 500;
}

/* Mobile Responsive for Transfer Modals */
@media (max-width: 767px) {
    .out-transfer-modal-global .transfer-options {
        grid-template-columns: repeat(2, 1fr);
        gap: 10px;
    }

    .out-transfer-modal-global .transfer-btn {
        min-height: 100px;
        padding: 15px 10px;
        font-size: 13px;
    }

    .driver-selection-modal-global .drivers-list {
        grid-template-columns: 1fr;
    }
}

/* iPad Specific for Transfer Modals */
@media (min-width: 768px) and (max-width: 1024px) {
    .out-transfer-modal-global .transfer-btn {
        min-height: 130px;
    }
}

/* =================================================================
   Search Modal Styles
   ================================================================= */

.search-modal-global .search-container {
    margin-bottom: 20px;
}

.search-modal-global .search-input {
    width: 100%;
    padding: 12px 16px;
    font-size: 16px;
    border: 2px solid #007bff;
    border-radius: 8px;
    background: white;
    color: #111827;
}

.search-modal-global .search-input:focus {
    outline: none;
    border-color: #0056b3;
    box-shadow: 0 0 0 3px rgba(0, 123, 255, 0.1);
}

.search-modal-global .search-results {
    max-height: 400px;
    overflow-y: auto;
    margin-top: 15px;
}

.search-modal-global .order-result {
    padding: 15px;
    margin-bottom: 10px;
    background: #f9fafb;
    border: 1px solid #e5e7eb;
    border-radius: 8px;
    cursor: pointer;
    transition: all 0.2s ease;
}

.search-modal-global .order-result:hover {
    background: #e5e7eb;
    border-color: #9ca3af;
}

.search-modal-global .empty-state {
    text-align: center;
    padding: 40px 20px;
    color: #6b7280;
}

/* Style the search modal close button */
.search-modal-global .search-modal-close-btn {
    position: absolute;
    top: 20px;
    right: 20px;
    width: 32px;
    height: 32px;
    background: transparent;
    border: none;
    color: #6b7280;
    font-size: 24px;
    cursor: pointer;
    display: flex;
    align-items: center;
    justify-content: center;
    border-radius: 4px;
    transition: all 0.2s ease;
}

.search-modal-global .search-modal-close-btn:hover {
    background: #f3f4f6;
    color: #111827;
}

/* =================================================================
   Confirmation Modal Styles
   ================================================================= */

.in-confirm-modal-global .confirm-message {
    font-size: 18px;
    color: #374151;
    text-align: center;
    margin: 20px 0;
}

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

/* Touch-friendly buttons for iPad */
@media (pointer: coarse) {
    .search-modal-global .search-input,
    .in-confirm-modal-global .btn,
    .out-transfer-modal-global .transfer-btn,
    .driver-selection-modal-global .driver-tile {
        min-height: 44px;
    }
}