/**
 * TEM-RR Mobile Responsiveness Enhancements
 * Optimized styles for mobile devices and touch interactions
 */

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

/* Prevent zoom on form inputs on iOS */
@media screen and (-webkit-min-device-pixel-ratio: 0) {
    select, textarea, input[type="text"], input[type="password"],
    input[type="datetime"], input[type="datetime-local"],
    input[type="date"], input[type="month"], input[type="time"],
    input[type="week"], input[type="number"], input[type="email"],
    input[type="url"] {
        font-size: 16px !important;
    }
}

/* Touch-friendly scrolling */
.table-responsive {
    -webkit-overflow-scrolling: touch;
}

/* Prevent horizontal overflow on mobile */
@media (max-width: 767.98px) {
    body {
        overflow-x: hidden;
    }
}

/* Improve touch targets - exclude checkboxes */
.btn, .form-control, .form-select {
    min-height: 44px;
}

/* Checkboxes should remain normal size */
.form-check-input {
    width: 1.25rem;
    height: 1.25rem;
    min-height: auto;
}

/* ==========================================================================
   Navigation Enhancements
   ========================================================================== */

@media (max-width: 767.98px) {
    /* Mobile layout adjustments */
    .main-content {
        margin-left: 0 !important;
        padding: 0.5rem !important;
        width: 100% !important;
    }
    
    /* Sidebar adjustments for mobile */
    .sidebar {
        transform: translateX(-100%);
        transition: transform 0.3s ease-in-out;
        z-index: 1040 !important;
        position: fixed;
        top: 0;
        left: 0;
        height: 100vh;
        width: 280px;
        box-shadow: 4px 0 20px rgba(0, 0, 0, 0.2);
        overflow-y: auto;
    }
    
    .sidebar.show {
        transform: translateX(0);
    }
    
    /* Sidebar backdrop */
    .sidebar-backdrop {
        position: fixed;
        top: 0;
        left: 0;
        width: 100%;
        height: 100%;
        background: rgba(0, 0, 0, 0.5);
        z-index: 1035 !important;
        opacity: 0;
        visibility: hidden;
        transition: opacity 0.3s ease-in-out;
    }
    
    .sidebar-backdrop.show {
        opacity: 1;
        visibility: visible;
    }
    
    /* Mobile navbar toggle */
    .navbar-toggler {
        border: none;
        padding: 0.5rem;
        font-size: 1.25rem;
        background: none;
    }
    
    .navbar-toggler:focus {
        box-shadow: 0 0 0 0.25rem rgba(0, 123, 255, 0.25);
    }
    
    /* Ensure mobile navbar doesn't interfere with sidebar */
    .top-navbar {
        padding: 0.5rem 0.75rem;
        z-index: 1030 !important;
        position: relative;
    }
    
    .navbar-brand {
        font-size: 1rem;
    }
    
    /* Ensure main content doesn't interfere with sidebar */
    .main-content {
        position: relative;
        z-index: 1;
    }
}

/* ==========================================================================
   Calendar Mobile Optimizations
   ========================================================================== */

@media (max-width: 767.98px) {
    /* FullCalendar mobile adjustments */
    .fc {
        font-size: 0.875rem;
    }
    
    .fc-header-toolbar {
        flex-direction: column;
        gap: 0.5rem;
    }
    
    .fc-toolbar-chunk {
        display: flex;
        justify-content: center;
        flex-wrap: wrap;
        gap: 0.25rem;
    }
    
    .fc-button-group .fc-button {
        padding: 0.375rem 0.5rem;
        font-size: 0.75rem;
    }
    
    .fc-daygrid-event {
        font-size: 0.75rem;
        padding: 0.25rem;
    }
    
    .fc-event-title {
        font-size: 0.75rem;
        line-height: 1.2;
    }
    
    /* Calendar controls mobile layout */
    .calendar-controls {
        padding: 0.75rem;
    }
    
    .calendar-controls .row > * {
        margin-bottom: 1rem;
    }
    
    .booking-legend {
        flex-direction: column;
        align-items: flex-start !important;
        gap: 0.5rem !important;
    }
    
    .legend-item {
        font-size: 0.75rem !important;
    }
}

/* ==========================================================================
   Tables Mobile Optimizations
   ========================================================================== */

@media (max-width: 767.98px) {
    /* Responsive table wrapper */
    .table-responsive {
        border: none;
        margin-bottom: 1rem;
    }
    
    /* Stack table for mobile */
    .table-mobile-stack thead {
        display: none;
    }
    
    .table-mobile-stack tbody,
    .table-mobile-stack tr,
    .table-mobile-stack td {
        display: block;
        width: 100%;
    }
    
    .table-mobile-stack tr {
        border: 1px solid #dee2e6;
        border-radius: 0.375rem;
        margin-bottom: 1rem;
        padding: 1rem;
        background: white;
        box-shadow: 0 1px 3px rgba(0, 0, 0, 0.1);
    }
    
    .table-mobile-stack td {
        border: none;
        padding: 0.5rem 0;
        text-align: left !important;
    }
    
    .table-mobile-stack td:before {
        content: attr(data-label) ": ";
        font-weight: 600;
        margin-right: 0.5rem;
        color: var(--primary-navy);
    }
    
    /* Card optimizations */
    .card {
        margin-bottom: 1rem;
        border-radius: 0.5rem;
    }
    
    .card-columns {
        column-count: 1;
    }
    
    /* User cards responsive */
    .user-card {
        margin-bottom: 1rem;
    }
}

/* ==========================================================================
   Dashboard Mobile Optimizations
   ========================================================================== */

@media (max-width: 767.98px) {
    /* Statistics cards */
    .card-stat {
        margin-bottom: 0.75rem;
    }
    
    .card-stat .h3 {
        font-size: 1.5rem;
    }
    
    /* Content wrapper */
    .content-wrapper {
        padding: 0.75rem;
    }
    
    /* Page headers */
    .page-header {
        margin-bottom: 1rem;
        padding: 0.75rem 1rem;
    }
    
    .page-header .d-flex {
        flex-direction: column;
        align-items: flex-start !important;
        gap: 0.75rem;
    }
    
    .page-header .btn {
        width: 100%;
        justify-content: center;
    }
    
    .page-title {
        font-size: 1.35rem;
    }
}

/* ==========================================================================
   Tablet Optimizations (768px - 991px)
   ========================================================================== */

@media (min-width: 768px) and (max-width: 991.98px) {
    .page-header {
        padding: 1.75rem;
    }
}

/* ==========================================================================
   General Mobile Improvements
   ========================================================================== */

@media (max-width: 767.98px) {
    /* Text and spacing */
    .container-fluid {
        padding-left: 0.5rem;
        padding-right: 0.5rem;
    }
    
    /* Hide unnecessary elements on mobile */
    .d-mobile-none {
        display: none !important;
    }
    
    /* Show mobile-only elements */
    .d-mobile-block {
        display: block !important;
    }
    
    .d-mobile-flex {
        display: flex !important;
    }
    
    /* Mobile spacing utilities */
    .mb-mobile-3 {
        margin-bottom: 1rem !important;
    }
    
    .p-mobile-2 {
        padding: 0.5rem !important;
    }
    
    /* List groups */
    .list-group-item {
        padding: 1rem;
        border-left: none;
        border-right: none;
    }
    
    .list-group-item:first-child {
        border-top-left-radius: 0;
        border-top-right-radius: 0;
    }
    
    .list-group-item:last-child {
        border-bottom-left-radius: 0;
        border-bottom-right-radius: 0;
    }
}

@media (max-width: 767.98px) {
    /* Form spacing and sizing */
    .form-group,
    .mb-3 {
        margin-bottom: 1rem !important;
    }
    
    .form-control,
    .form-select {
        padding: 0.5rem 0.75rem;
        font-size: 16px; /* Prevent zoom on iOS */
    }
    
    .form-label {
        font-weight: 600;
        margin-bottom: 0.25rem;
        font-size: 0.875rem;
    }
    
    /* Quick booking form mobile */
    .quick-book-form {
        width: 95% !important;
        max-width: none !important;
        margin: 0.5rem;
        border-radius: 1rem;
    }
    
    .quick-book-form .card-body {
        padding: 1.5rem 1rem;
    }
    
    /* Datetime inputs enhancement */
    input[type="datetime-local"] {
        width: 100%;
    }
    
    /* Button improvements */
    .btn-group-mobile {
        display: flex;
        flex-direction: column;
        gap: 0.5rem;
        width: 100%;
    }
    
    .btn-group-mobile .btn {
        width: 100%;
        justify-content: center;
    }
    
    /* Form validation feedback */
    .invalid-feedback,
    .valid-feedback {
        font-size: 0.875rem;
        margin-top: 0.25rem;
    }
}

/* ==========================================================================
   Tables and Lists Mobile
   ========================================================================== */

@media (max-width: 767.98px) {
    /* Responsive table cards */
    .table-mobile-cards .table,
    .table-mobile-cards thead,
    .table-mobile-cards tbody,
    .table-mobile-cards th,
    .table-mobile-cards td,
    .table-mobile-cards tr {
        display: block;
    }
    
    .table-mobile-cards thead tr {
        position: absolute;
        top: -9999px;
        left: -9999px;
    }
    
    .table-mobile-cards tr {
        background: white;
        border-radius: 0.5rem;
        margin-bottom: 1rem;
        padding: 1rem;
        box-shadow: 0 2px 4px rgba(0, 0, 0, 0.1);
        border: 1px solid #e0e0e0;
    }
    
    .table-mobile-cards td {
        border: none !important;
        position: relative;
        padding: 0.5rem 0 !important;
        display: flex;
        justify-content: space-between;
        align-items: center;
    }
    
    .table-mobile-cards td:before {
        content: attr(data-label);
        font-weight: 600;
        color: #6c757d;
        font-size: 0.875rem;
        flex-shrink: 0;
        margin-right: 1rem;
        min-width: 100px;
    }
    
    .table-mobile-cards td:last-child {
        border-bottom: none !important;
    }
    
    /* List groups mobile enhancement */
    .list-group-item {
        padding: 1rem;
    }
    
    .list-group-item-action:hover,
    .list-group-item-action:focus {
        background-color: #f8f9fa;
    }
}

/* ==========================================================================
   Modal and Popover Mobile
   ========================================================================== */

@media (max-width: 767.98px) {
    /* Modal adjustments */
    .modal-dialog {
        margin: 0.5rem;
        max-width: calc(100% - 1rem);
    }
    
    .modal-content {
        border-radius: 1rem;
    }
    
    .modal-header {
        padding: 1rem;
        border-radius: 1rem 1rem 0 0;
    }
    
    .modal-body {
        padding: 1rem;
        max-height: 70vh;
        overflow-y: auto;
    }
    
    .modal-footer {
        padding: 1rem;
        flex-direction: column;
        gap: 0.5rem;
    }
    
    .modal-footer .btn {
        width: 100%;
    }
    
    /* Booking detail modal mobile */
    .booking-detail-modal .modal-dialog {
        height: 90vh;
    }
    
    .booking-detail-modal .modal-content {
        height: 100%;
        display: flex;
        flex-direction: column;
    }
    
    .booking-detail-modal .modal-body {
        flex: 1;
        overflow-y: auto;
    }
}

/* ==========================================================================
   Cards and Panels Mobile
   ========================================================================== */

@media (max-width: 767.98px) {
    /* Card spacing and sizing */
    .card {
        border-radius: 0.75rem;
        box-shadow: 0 2px 8px rgba(0, 0, 0, 0.1);
        margin-bottom: 0.75rem;
    }
    
    .card-header {
        padding: 0.75rem 1rem;
        border-radius: 0.75rem 0.75rem 0 0 !important;
    }
    
    .card-body {
        padding: 0.75rem 1rem;
    }
    
    .card-footer {
        padding: 0.75rem 1rem;
        border-radius: 0 0 0.75rem 0.75rem !important;
    }
    
    /* Environment cards mobile */
    .environment-card {
        margin-bottom: 0.75rem;
    }
    
    .environment-card .card-body {
        padding: 0.75rem 1rem;
    }
    
    .environment-actions {
        display: flex;
        flex-wrap: wrap;
        gap: 0.5rem;
        margin-top: 0.75rem;
    }
    
    .environment-actions .btn {
        flex: 1;
        min-width: calc(50% - 0.25rem);
        font-size: 0.875rem;
        padding: 0.5rem;
    }
}

/* ==========================================================================
   Dashboard Mobile Layout
   ========================================================================== */

@media (max-width: 767.98px) {
    /* Dashboard stats cards */
    .stats-card {
        text-align: center;
        padding: 1.5rem 1rem;
        margin-bottom: 1rem;
    }
    
    .stats-card .display-6 {
        font-size: 2rem;
    }
    
    /* Dashboard recent activity */
    .recent-activity {
        max-height: 400px;
        overflow-y: auto;
    }
    
    .activity-item {
        padding: 0.75rem;
        border-bottom: 1px solid #e0e0e0;
    }
    
    .activity-item:last-child {
        border-bottom: none;
    }
    
    /* Quick actions mobile */
    .quick-actions {
        display: grid;
        grid-template-columns: 1fr;
        gap: 1rem;
        margin-top: 1rem;
    }
    
    .quick-action-btn {
        padding: 1rem;
        text-align: center;
        border-radius: 1rem;
        text-decoration: none;
        background: white;
        border: 2px solid #e0e0e0;
        color: #495057;
        transition: all 0.2s;
    }
    
    .quick-action-btn:hover,
    .quick-action-btn:focus {
        background: #f8f9fa;
        border-color: #007bff;
        color: #007bff;
        text-decoration: none;
        transform: translateY(-2px);
    }
    
    .quick-action-btn i {
        font-size: 1.5rem;
        margin-bottom: 0.5rem;
        display: block;
    }
}

/* ==========================================================================
   Touch Enhancements
   ========================================================================== */

/* Improve touch targets */
@media (pointer: coarse) {
    .btn {
        min-height: 48px;
        padding: 0.75rem 1rem;
    }
    
    .btn-sm {
        min-height: 40px;
        padding: 0.5rem 0.75rem;
    }
    
    .form-check-input {
        width: 1.25em;
        height: 1.25em;
    }
    
    /* Swipe gestures for calendar */
    .fc-daygrid,
    .fc-timegrid {
        touch-action: pan-x pan-y;
    }
    
    /* Better hover states for touch */
    .btn:active,
    .btn-check:checked + .btn,
    .btn.active,
    .btn.show {
        transform: scale(0.98);
    }
    
    /* Touch-friendly dropdowns */
    .dropdown-menu {
        min-width: 250px;
        padding: 0.5rem 0;
    }
    
    .dropdown-item {
        padding: 0.75rem 1rem;
        font-size: 1rem;
    }
}

/* ==========================================================================
   Toast Notifications Mobile
   ========================================================================== */

@media (max-width: 767.98px) {
    .toast {
        min-width: calc(100vw - 2rem) !important;
        max-width: calc(100vw - 2rem) !important;
        top: 1rem !important;
        left: 1rem !important;
        right: 1rem !important;
        position: fixed !important;
    }
    
    .toast-container {
        top: 1rem !important;
        right: 1rem !important;
        left: 1rem !important;
    }
}

/* ==========================================================================
   Signup Flow Mobile
   ========================================================================== */

@media (max-width: 767.98px) {
    .signup-container {
        padding: 1rem 0.5rem;
    }
    
    .signup-card {
        border-radius: 1rem;
        box-shadow: 0 4px 20px rgba(0, 0, 0, 0.15);
    }
    
    .signup-header {
        padding: 2rem 1rem;
        text-align: center;
    }
    
    .signup-header h1 {
        font-size: 1.75rem;
    }
    
    .signup-step {
        padding: 2rem 1rem;
    }
    
    .step-indicators {
        margin-bottom: 2rem;
        justify-content: center;
    }
    
    .step-indicator {
        width: 40px;
        height: 40px;
        line-height: 40px;
        font-size: 0.875rem;
    }
    
    .step-navigation {
        flex-direction: column;
        gap: 1rem;
        margin-top: 2rem;
    }
    
    .step-navigation .btn {
        width: 100%;
        order: 2;
    }
    
    .step-navigation .btn-secondary {
        order: 1;
    }
}

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

/* Focus indicators */
@media (prefers-reduced-motion: no-preference) {
    .btn:focus,
    .form-control:focus,
    .form-select:focus {
        transition: box-shadow 0.15s ease-in-out, border-color 0.15s ease-in-out;
    }
}

/* High contrast mode support */
@media (prefers-contrast: high) {
    .card,
    .btn,
    .form-control,
    .table th,
    .table td {
        border-width: 2px !important;
    }
    
    .btn-primary {
        background-color: #000 !important;
        border-color: #000 !important;
        color: #fff !important;
    }
}

/* Dark mode preparation */
@media (prefers-color-scheme: dark) {
    .dark-mode-ready {
        background-color: #1a1a1a;
        color: #f0f0f0;
    }
    
    .dark-mode-ready .card {
        background-color: #2d2d2d;
        border-color: #404040;
    }
    
    .dark-mode-ready .form-control {
        background-color: #2d2d2d;
        border-color: #404040;
        color: #f0f0f0;
    }
}

/* ==========================================================================
   Performance Optimizations
   ========================================================================== */

/* Reduce animations on slower devices */
@media (prefers-reduced-motion: reduce) {
    *,
    *::before,
    *::after {
        animation-duration: 0.01ms !important;
        animation-iteration-count: 1 !important;
        transition-duration: 0.01ms !important;
    }
}

/* GPU acceleration for smooth scrolling */
.main-content,
.sidebar,
.modal {
    will-change: transform;
    transform: translateZ(0);
}

/* Optimize calendar rendering */
.fc {
    contain: layout style paint;
}

/* ==========================================================================
   Print Styles
   ========================================================================== */

@media print {
    .sidebar,
    .btn,
    .quick-actions,
    .modal,
    .toast {
        display: none !important;
    }
    
    .main-content {
        margin-left: 0 !important;
        padding: 0 !important;
    }
    
    .card {
        border: 1px solid #000 !important;
        box-shadow: none !important;
        break-inside: avoid;
    }
    
    .table {
        border-collapse: collapse !important;
    }
    
    .table td,
    .table th {
        border: 1px solid #000 !important;
    }
}

/* ==========================================================================
   Dashboard Mobile Enhancements
   ========================================================================== */

.page-header {
    margin-bottom: 1rem;
}

.stat-card {
    transition: transform 0.2s ease, box-shadow 0.2s ease;
}

.stat-card:hover {
    transform: translateY(-2px);
    box-shadow: 0 8px 25px rgba(0,0,0,0.1);
}

/* Badge responsive positioning */
.badge.align-self-start {
    white-space: nowrap;
}

/* Quick actions on mobile */
@media (max-width: 575.98px) {
    .page-header h1.page-title {
        font-size: 1.75rem;
    }
    
    .stat-card .h3 {
        font-size: 1.5rem;
    }
    
    .card-header h5 {
        font-size: 1rem;
    }
    
    /* Make quick action buttons more touch-friendly */
    .btn[style*="min-height"] {
        min-height: 100px !important;
        padding: 1rem !important;
    }
    
    .btn .fa-2x {
        font-size: 1.5rem !important;
    }
    
    /* Compact booking/checklist items on mobile */
    .list-group-item {
        padding-top: 1rem !important;
        padding-bottom: 1rem !important;
    }
    
    .list-group-item h6 {
        font-size: 0.95rem;
    }
    
    .list-group-item p,
    .list-group-item small {
        font-size: 0.85rem;
    }
}

/* ==========================================================================
   Mobile Sidebar Fix - High Priority Override
   ========================================================================== */

@media (max-width: 767.98px) {
    /* Force sidebar to be properly positioned and visible - High Priority */
    .sidebar {
        transform: translateX(-100%) !important;
        transition: transform 0.3s ease-in-out !important;
        z-index: 1040 !important;
        position: fixed !important;
        top: 0 !important;
        left: 0 !important;
        height: 100vh !important;
        width: 280px !important;
        background: var(--primary-navy) !important;
        box-shadow: 0 0 20px rgba(0, 0, 0, 0.3) !important;
        overflow-y: auto !important;
        -webkit-overflow-scrolling: touch;
    }
    
    .sidebar.show {
        transform: translateX(0) !important;
    }
    
    /* High priority backdrop */
    .sidebar-backdrop {
        position: fixed !important;
        top: 0 !important;
        left: 0 !important;
        width: 100% !important;
        height: 100% !important;
        background: rgba(0, 0, 0, 0.5) !important;
        z-index: 1035 !important;
        opacity: 0 !important;
        visibility: hidden !important;
        transition: opacity 0.3s ease-in-out !important;
    }
    
    .sidebar-backdrop.show {
        opacity: 1 !important;
        visibility: visible !important;
    }
    
    /* Navbar must be below sidebar */
    .top-navbar {
        position: relative !important;
        z-index: 1030 !important;
    }
    
    /* Toggle button must be accessible */
    .navbar-toggler {
        z-index: 1031 !important;
        position: relative !important;
    }
    
    /* Prevent body scroll when sidebar is open */
    body.sidebar-open {
        overflow: hidden !important;
    }
}