/* Mobile Device Fixes and Enhancements */
/* Response365 Mobile Optimization CSS */

/* ===========================================
   Z-INDEX HIERARCHY (CRITICAL) - CSS VARIABLES
   ========================================== */
:root {
    /* Z-index scale - predictable and maintainable */
    --z-base: 1;
    --z-sidebar: 40;
    --z-header: 50;
    --z-dropdown: 60;
    --z-tooltip: 70;
    --z-modal-backdrop: 80;
    --z-modal: 90;
    --z-mobile-menu-overlay: 100;
    --z-mobile-menu-panel: 101;
    --z-notification: 110;
}

/* Mobile menu */
#mobile-menu-panel {
    z-index: var(--z-mobile-menu-panel) !important;
}

#mobile-menu-panel > .fixed.inset-0 {
    /* Backdrop must stay BELOW the actual menu panel content, otherwise it blocks clicks */
    z-index: calc(var(--z-mobile-menu-panel) - 1) !important;
}

/* Prevent background scroll when the mobile menu is open */
html.mobile-menu-open,
body.mobile-menu-open {
    overflow: hidden !important;
}

/* Header/navbar */
header.fixed {
    z-index: var(--z-header) !important;
}

/* Widget modals and notifications */
.widget-notification,
#widget-selector-modal {
    z-index: var(--z-notification) !important;
}

.modal-backdrop {
    z-index: var(--z-modal-backdrop) !important;
}

.modal {
    z-index: var(--z-modal) !important;
}

/* Bottom navigation/search */
.mobile-bottom-nav,
.mobile-search-bar {
    z-index: var(--z-sidebar) !important;
}

/* Sidebar tooltips and balloons */
.sidebar-balloon,
.tooltip {
    z-index: var(--z-tooltip) !important;
}

/* Dropdowns */
.dropdown,
.dropdown-menu,
[role="listbox"] {
    z-index: var(--z-dropdown) !important;
}

/* ===========================================
   MOBILE VIEWPORT AND TOUCH OPTIMIZATIONS
   ========================================== */

/* Mobile-only tap + gesture ergonomics (safe defaults) */
@media (max-width: 1023px) {
    /* Reduce tap highlight noise */
    a,
    button,
    [role="button"],
    input,
    select,
    textarea {
        -webkit-tap-highlight-color: rgba(0, 0, 0, 0);
        tap-highlight-color: rgba(0, 0, 0, 0);
        touch-action: manipulation;
    }

    /* Prevent scroll chaining on the document itself (helps drawers/menus feel native) */
    html,
    body {
        overscroll-behavior: contain;
    }
}

/* Prevent zoom on form inputs on iOS */
@media screen and (max-width: 768px) {
    input[type="text"],
    input[type="email"],
    input[type="number"],
    input[type="password"],
    input[type="search"],
    input[type="tel"],
    input[type="url"],
    select,
    textarea {
        font-size: 16px !important; /* Prevents iOS zoom */
    }
}

/* Touch-friendly tap targets (minimum 44px) - MOBILE ONLY
   Do NOT enforce globally on desktop; it can break dense UIs and table toolbars.
*/
@media (max-width: 1023px) {
    .mobile-tap-target,
    .btn-mobile,
    button,
    .clickable,
    a[role="button"] {
        min-height: 44px;
        min-width: 44px;
    }
}

/* Improve touch scrolling on iOS */
.mobile-scroll {
    -webkit-overflow-scrolling: touch;
    overscroll-behavior: contain;
}

/* System-wide: any horizontal overflow container should scroll nicely on mobile.
   This avoids having to remember to add `.mobile-scroll` everywhere. */
@media (max-width: 1023px) {
    .overflow-x-auto {
        -webkit-overflow-scrolling: touch;
        overscroll-behavior: contain;
    }
}

/* ===========================================
   MOBILE MENU PANEL FIXES
   ========================================== */

/* Ensure mobile menu panel is properly positioned */
#mobile-menu-panel {
    position: fixed !important;
    top: 0 !important;
    left: 0 !important;
    width: 100% !important;
    height: 100vh !important;
    height: 100dvh !important; /* Dynamic viewport height for mobile browsers */
}

/* Mobile menu content area */
#mobile-menu-panel .menu-panel {
    max-width: 320px;
    width: 85%;
    height: 100%;
    overflow-y: auto;
    -webkit-overflow-scrolling: touch;
    position: relative;
    z-index: var(--z-mobile-menu-panel) !important;
}

/* Fix for mobile menu content spacing */
@media (max-width: 768px) {
    #mobile-menu-panel .menu-panel {
        width: 90%;
        max-width: none;
    }
}

/* ===========================================
   WIDGET DASHBOARD MOBILE OPTIMIZATIONS
   ========================================== */

/* Widget grid responsive fixes */
@media (max-width: 768px) {
    .widget-grid,
    .dashboard-grid {
        display: block !important;
        grid-template-columns: 1fr !important;
    }
    
    .widget-container {
        margin-bottom: 1rem;
        width: 100% !important;
        min-height: auto !important;
    }
    
    /* Widget charts - make them responsive */
    .widget-chart,
    .chart-container {
        height: 250px !important;
        min-height: 250px !important;
    }
    
    /* Widget metrics - compact on mobile */
    .widget-metric {
        min-height: 120px !important;
        padding: 1rem !important;
    }
    
    /* Widget lists - shorter on mobile */
    .widget-list {
        min-height: 200px !important;
        max-height: 300px !important;
        overflow-y: auto;
    }
}

/* ===========================================
   MOBILE SAFE AREA HANDLING
   ========================================== */

/* iOS safe area support */
.mobile-safe-top {
    padding-top: env(safe-area-inset-top);
}

.mobile-safe-bottom {
    padding-bottom: env(safe-area-inset-bottom);
}

.mobile-safe-left {
    padding-left: env(safe-area-inset-left);
}

.mobile-safe-right {
    padding-right: env(safe-area-inset-right);
}

/* Adjust main content for mobile header and bottom nav */
@media (max-width: 1023px) {
    .main-content {
        padding-top: 4rem; /* Account for fixed header */
        padding-bottom: 5rem; /* Account for bottom nav */
        min-height: calc(100vh - 9rem);
    }
    
    /* For pages with bottom search/nav */
    .main-content.has-bottom-nav {
        padding-bottom: 6rem;
    }
}

/* ===========================================
   MOBILE FORM IMPROVEMENTS
   ========================================== */

/* Better mobile form styling */
@media (max-width: 768px) {
    .form-control,
    .form-input,
    input,
    textarea,
    select {
        padding: 0.75rem !important;
        border-radius: 0.5rem !important;
        border: 2px solid #e5e7eb !important;
    }
    
    .form-control:focus,
    .form-input:focus,
    input:focus,
    textarea:focus,
    select:focus {
        border-color: #3b82f6 !important;
        box-shadow: 0 0 0 3px rgba(59, 130, 246, 0.1) !important;
    }
    
    /* Stack form elements vertically */
    .form-row,
    .form-group {
        flex-direction: column !important;
    }
    
    .form-row > * {
        width: 100% !important;
        margin-bottom: 1rem !important;
    }
}

/* ===========================================
   MOBILE NAVIGATION IMPROVEMENTS
   ========================================== */

/* Mobile hamburger button improvements */
.mobile-menu-button {
    padding: 0.75rem !important;
    border-radius: 0.5rem !important;
    transition: all 0.2s ease !important;
}

.mobile-menu-button:hover,
.mobile-menu-button:focus {
    background-color: rgba(59, 130, 246, 0.1) !important;
    transform: scale(1.05) !important;
}

/* Mobile menu animation improvements */
@media (max-width: 768px) {
    .mobile-slide-in {
        animation: slideInFromLeft 0.3s ease-out;
    }
    
    .mobile-fade-in {
        animation: fadeIn 0.2s ease-out;
    }
}

@keyframes slideInFromLeft {
    from {
        transform: translateX(-100%);
        opacity: 0;
    }
    to {
        transform: translateX(0);
        opacity: 1;
    }
}

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

/* ===========================================
   MOBILE TABLE IMPROVEMENTS
   ========================================== */

/* Responsive tables */
@media (max-width: 768px) {
    .table-responsive {
        display: block !important;
        width: 100% !important;
        overflow-x: auto !important;
        -webkit-overflow-scrolling: touch !important;
    }
    
    .table-responsive table {
        min-width: 600px !important;
    }
    
    /* Card-style table rows for mobile */
    .table-mobile-cards {
        display: block !important;
    }
    
    .table-mobile-cards thead {
        display: none !important;
    }
    
    .table-mobile-cards tbody {
        display: block !important;
    }
    
    .table-mobile-cards tr {
        display: block !important;
        border: 1px solid #e5e7eb !important;
        border-radius: 0.5rem !important;
        margin-bottom: 1rem !important;
        padding: 1rem !important;
        background: white !important;
        box-shadow: 0 1px 3px rgba(0, 0, 0, 0.1) !important;
    }
    
    .table-mobile-cards td {
        display: flex !important;
        justify-content: space-between !important;
        align-items: center !important;
        padding: 0.5rem 0 !important;
        border: none !important;
        border-bottom: 1px solid #f3f4f6 !important;
    }
    
    .table-mobile-cards td:last-child {
        border-bottom: none !important;
    }
    
    .table-mobile-cards td::before {
        content: attr(data-label) ": " !important;
        font-weight: 600 !important;
        color: #6b7280 !important;
    }
}

/* ===========================================
   MOBILE BUTTON AND CONTROL IMPROVEMENTS
   ========================================== */

/* Better mobile button spacing */
@media (max-width: 768px) {
    .btn-group {
        flex-direction: column !important;
        gap: 0.5rem !important;
    }
    
    .btn-group .btn {
        width: 100% !important;
        margin: 0 !important;
    }
    
    /* Mobile-specific button sizing */
    .btn-mobile-lg {
        padding: 1rem 1.5rem !important;
        font-size: 1.125rem !important;
        border-radius: 0.75rem !important;
    }
    
    .btn-mobile-full {
        width: 100% !important;
        display: block !important;
        text-align: center !important;
    }
}

/* ===========================================
   MOBILE PERFORMANCE OPTIMIZATIONS
   ========================================== */

/* Reduce animations on mobile for better performance */
@media (max-width: 768px) and (prefers-reduced-motion: no-preference) {
    * {
        animation-duration: 0.2s !important;
        transition-duration: 0.2s !important;
    }
}

/* Disable hover effects on touch devices */
@media (hover: none) {
    .hover\:bg-gray-100:hover,
    .hover\:bg-gray-200:hover,
    .hover\:scale-105:hover {
        background-color: inherit !important;
        transform: none !important;
    }
}

/* ===========================================
   MOBILE ACCESSIBILITY IMPROVEMENTS
   ========================================== */

/* Better focus indicators on mobile */
@media (max-width: 768px) {
    button:focus,
    input:focus,
    select:focus,
    textarea:focus,
    a:focus {
        outline: 3px solid #3b82f6 !important;
        outline-offset: 2px !important;
    }
    
    /* Skip to content link for mobile */
    .skip-to-content {
        position: absolute !important;
        top: -40px !important;
        left: 6px !important;
        background: #3b82f6 !important;
        color: white !important;
        padding: 8px !important;
        text-decoration: none !important;
        border-radius: 4px !important;
        z-index: 10000 !important;
    }
    
    .skip-to-content:focus {
        top: 6px !important;
    }
}

/* ===========================================
   DARK MODE MOBILE ADJUSTMENTS
   ========================================== */

@media (max-width: 768px) {
    .dark .mobile-menu-panel {
        background-color: #1f2937 !important;
    }
    
    .dark .mobile-bottom-nav {
        background-color: #1f2937 !important;
        border-color: #374151 !important;
    }
    
    .dark .widget-container {
        background-color: #374151 !important;
        border-color: #4b5563 !important;
    }
}

/* ===========================================
   MOBILE WIDGET NOTIFICATION FIXES
   ========================================== */

/* Fix widget notifications positioning on mobile */
@media (max-width: 768px) {
    #widget-notifications-container {
        top: 4rem !important;
        left: 1rem !important;
        right: 1rem !important;
        max-width: none !important;
    }
    
    .widget-notification {
        margin-bottom: 0.5rem !important;
        font-size: 0.875rem !important;
    }
}

/* ===========================================
   MOBILE LANDSCAPE OPTIMIZATIONS
   ========================================== */

/* Landscape mobile optimizations */
@media (max-width: 896px) and (orientation: landscape) {
    .main-content {
        padding-top: 3rem !important;
        padding-bottom: 3rem !important;
    }
    
    #mobile-menu-panel .menu-panel {
        width: 60% !important;
        max-width: 400px !important;
    }
    
    .widget-container {
        min-height: 200px !important;
    }
}

/* ===========================================
   MOBILE UTILITY CLASSES
   ========================================== */

/* Mobile-only display utilities */
.mobile-only {
    display: none !important;
}

@media (max-width: 768px) {
    .mobile-only {
        display: block !important;
    }
    
    .mobile-hidden {
        display: none !important;
    }
    
    .mobile-flex {
        display: flex !important;
    }
    
    .mobile-grid {
        display: grid !important;
    }
}

/* Mobile spacing utilities */
@media (max-width: 768px) {
    .mobile-p-4 { padding: 1rem !important; }
    .mobile-p-6 { padding: 1.5rem !important; }
    .mobile-m-2 { margin: 0.5rem !important; }
    .mobile-m-4 { margin: 1rem !important; }
    .mobile-mt-16 { margin-top: 4rem !important; }
    .mobile-mb-16 { margin-bottom: 4rem !important; }
}

/* ===========================================
   MOBILE MENU PERFORMANCE OPTIMIZATIONS
   ========================================== */

/* Force GPU acceleration for smooth animations */
#mobile-menu-panel {
    will-change: transform, opacity;
    transform: translateZ(0);
    backface-visibility: hidden;
    -webkit-backface-visibility: hidden;
}

#mobile-menu-panel .relative {
    will-change: transform;
    transform: translateZ(0);
    backface-visibility: hidden;
    -webkit-backface-visibility: hidden;
}

/* Smooth transitions */
#mobile-menu-panel,
#mobile-menu-panel .relative,
#mobile-menu-panel button,
#mobile-menu-panel a {
    -webkit-font-smoothing: antialiased;
    -moz-osx-font-smoothing: grayscale;
}

/* Prevent flicker during transitions */
@media (max-width: 1023px) {
    .lg\:hidden {
        transform: translateZ(0);
    }
}
