/**
 * Mobile-Friendly Enhancements - PharmAssist
 * Mobile-specific styles using @media queries
 * 
 * Applies only to mobile devices (max-width: 768px)
 * Does not affect desktop experience
 */

/* Pull to refresh - hidden by default (desktop), shown only on mobile via media query */
.pull-to-refresh {
    display: none;
}

/* Mobile-only styles */
@media (max-width: 768px) {
    
    /* Touch-friendly tap targets - Minimum 44px */
    button,
    .btn,
    a.button,
    input[type="button"],
    input[type="submit"],
    input[type="reset"],
    .clickable,
    .tap-target {
        min-height: 44px;
        min-width: 44px;
        padding: 12px 16px;
        font-size: 16px;
    }
    
    /* Larger form fields */
    input[type="text"],
    input[type="email"],
    input[type="tel"],
    input[type="number"],
    input[type="password"],
    input[type="search"],
    textarea,
    select {
        font-size: 16px; /* Prevents auto-zoom on iOS */
        min-height: 44px;
        padding: 12px 16px;
        border-radius: 8px;
    }
    
    /* Auto-zoom prevention */
    input,
    textarea,
    select {
        font-size: 16px !important; /* iOS won't zoom if 16px or larger */
    }
    
    /* Appropriate input types for better mobile keyboards */
    input[type="tel"] {
        /* Use tel input type for phone numbers */
    }
    
    input[type="number"] {
        /* Use number input for numeric fields */
    }
    
    input[type="email"] {
        /* Use email input for email fields */
    }
    
    /* Single-column layouts */
    .dashboard-grid,
    .grid,
    .card-grid {
        grid-template-columns: 1fr !important;
        gap: 16px;
    }
    
    .two-column,
    .three-column,
    .multi-column {
        display: block !important;
    }
    
    .two-column > *,
    .three-column > *,
    .multi-column > * {
        width: 100% !important;
        margin-bottom: 16px;
    }
    
    /* Mobile navigation - Bottom nav bar */
    .mobile-bottom-nav {
        position: fixed;
        bottom: 0;
        left: 0;
        right: 0;
        background: var(--bg-secondary);
        backdrop-filter: blur(10px);
        -webkit-backdrop-filter: blur(10px);
        display: flex;
        justify-content: space-around;
        align-items: center;
        padding: 8px 0;
        box-shadow: 0 -2px 10px rgba(0, 0, 0, 0.3);
        z-index: 1000;
        border-top: 1px solid var(--border-color);
    }
    
    .mobile-bottom-nav::before {
        content: '';
        position: absolute;
        top: 0;
        left: 0;
        right: 0;
        height: 2px;
        background: var(--accent-gradient);
    }
    
    .mobile-bottom-nav .nav-item {
        display: flex;
        flex-direction: column;
        align-items: center;
        justify-content: center;
        color: var(--text-secondary);
        font-size: 11px;
        text-decoration: none;
        padding: 8px 16px;
        min-width: 60px;
        transition: all 0.2s ease;
        border-radius: 8px;
        position: relative;
    }
    
    .mobile-bottom-nav .nav-item:active {
        transform: scale(0.95);
    }
    
    .mobile-bottom-nav .nav-item.active {
        color: var(--accent-primary);
        background: rgba(0, 212, 255, 0.1);
    }
    
    .mobile-bottom-nav .nav-item.active::before {
        content: '';
        position: absolute;
        top: -8px;
        left: 50%;
        transform: translateX(-50%);
        width: 30px;
        height: 3px;
        background: var(--accent-gradient);
        border-radius: 0 0 3px 3px;
    }
    
    .mobile-bottom-nav .nav-item-icon {
        font-size: 24px;
        margin-bottom: 4px;
        display: block;
    }
    
    .mobile-bottom-nav .nav-item-label {
        font-weight: 600;
        font-size: 10px;
        text-transform: uppercase;
        letter-spacing: 0.5px;
    }
    
    /* Add padding to body when bottom nav is present */
    body.has-bottom-nav {
        padding-bottom: 70px;
    }
    
    /* Tools Menu Modal */
    .tools-menu-modal {
        position: fixed;
        top: 0;
        left: 0;
        right: 0;
        bottom: 0;
        z-index: 10001;
        display: flex;
        align-items: flex-end;
        opacity: 0;
        transition: opacity 0.3s ease;
    }
    
    .tools-menu-modal.active {
        opacity: 1;
    }
    
    .tools-menu-overlay {
        position: absolute;
        top: 0;
        left: 0;
        right: 0;
        bottom: 0;
        background: rgba(0, 0, 0, 0.7);
        backdrop-filter: blur(5px);
        -webkit-backdrop-filter: blur(5px);
    }
    
    .tools-menu-content {
        position: relative;
        background: var(--bg-card);
        border-radius: 24px 24px 0 0;
        padding: 24px;
        max-height: 70vh;
        overflow-y: auto;
        width: 100%;
        transform: translateY(100%);
        transition: transform 0.3s cubic-bezier(0.4, 0, 0.2, 1);
        border-top: 2px solid var(--accent-primary);
    }
    
    .tools-menu-modal.active .tools-menu-content {
        transform: translateY(0);
    }
    
    .tools-menu-header {
        display: flex;
        justify-content: space-between;
        align-items: center;
        margin-bottom: 20px;
        padding-bottom: 16px;
        border-bottom: 1px solid var(--border-color);
    }
    
    .tools-menu-header h3 {
        color: var(--text-primary);
        font-size: 20px;
        font-weight: 700;
    }
    
    .tools-menu-close {
        background: var(--bg-tertiary);
        border: 1px solid var(--border-color);
        color: var(--text-primary);
        width: 36px;
        height: 36px;
        border-radius: 50%;
        font-size: 24px;
        display: flex;
        align-items: center;
        justify-content: center;
        cursor: pointer;
        transition: all 0.2s ease;
    }
    
    .tools-menu-close:active {
        transform: scale(0.95);
        background: var(--border-color);
    }
    
    .tools-menu-grid {
        display: grid;
        grid-template-columns: repeat(3, 1fr);
        gap: 16px;
    }
    
    .tool-item {
        display: flex;
        flex-direction: column;
        align-items: center;
        justify-content: center;
        padding: 16px 8px;
        background: var(--bg-tertiary);
        border: 1px solid var(--border-color);
        border-radius: 12px;
        text-decoration: none;
        color: var(--text-primary);
        transition: all 0.2s ease;
        min-height: 88px;
    }
    
    .tool-item:active {
        transform: scale(0.95);
        background: var(--border-color);
    }
    
    .tool-icon {
        font-size: 28px;
        margin-bottom: 8px;
    }
    
    .tool-label {
        font-size: 12px;
        font-weight: 600;
        text-align: center;
        color: var(--text-secondary);
    }
    
    /* Improved hamburger menu */
    .hamburger-menu {
        display: block;
        width: 30px;
        height: 24px;
        cursor: pointer;
        position: relative;
    }
    
    .hamburger-menu span {
        display: block;
        width: 100%;
        height: 3px;
        background: currentColor;
        border-radius: 2px;
        position: absolute;
        left: 0;
        transition: all 0.3s ease;
    }
    
    .hamburger-menu span:nth-child(1) {
        top: 0;
    }
    
    .hamburger-menu span:nth-child(2) {
        top: 10px;
    }
    
    .hamburger-menu span:nth-child(3) {
        top: 20px;
    }
    
    .hamburger-menu.open span:nth-child(1) {
        top: 10px;
        transform: rotate(45deg);
    }
    
    .hamburger-menu.open span:nth-child(2) {
        opacity: 0;
    }
    
    .hamburger-menu.open span:nth-child(3) {
        top: 10px;
        transform: rotate(-45deg);
    }
    
    /* Sticky action buttons */
    .sticky-action-btn {
        position: fixed;
        bottom: 80px; /* Above bottom nav */
        right: 16px;
        width: 56px;
        height: 56px;
        border-radius: 50%;
        background: linear-gradient(135deg, #667eea, #764ba2);
        color: white;
        display: flex;
        align-items: center;
        justify-content: center;
        font-size: 24px;
        box-shadow: 0 4px 12px rgba(102, 126, 234, 0.4);
        z-index: 999;
    }
    
    /* Collapsible sections - Collapsed by default on mobile */
    .mobile-collapsible {
        border: 1px solid rgba(255, 255, 255, 0.1);
        border-radius: 8px;
        margin-bottom: 12px;
        overflow: hidden;
    }
    
    .mobile-collapsible-header {
        display: flex;
        justify-content: space-between;
        align-items: center;
        padding: 16px;
        background: rgba(255, 255, 255, 0.05);
        cursor: pointer;
        user-select: none;
    }
    
    .mobile-collapsible-header::after {
        content: '▼';
        transition: transform 0.3s;
    }
    
    .mobile-collapsible.open .mobile-collapsible-header::after {
        transform: rotate(180deg);
    }
    
    .mobile-collapsible-content {
        max-height: 0;
        overflow: hidden;
        transition: max-height 0.3s ease;
    }
    
    .mobile-collapsible.open .mobile-collapsible-content {
        max-height: 2000px;
        padding: 16px;
    }
    
    /* Swipeable cards */
    .swipeable-card {
        position: relative;
        touch-action: pan-y;
        transition: transform 0.2s ease;
    }
    
    .swipeable-card.swiping {
        transition: none;
    }
    
    .swipeable-card-actions {
        position: absolute;
        top: 0;
        right: 0;
        bottom: 0;
        width: 80px;
        background: linear-gradient(90deg, transparent, rgba(102, 126, 234, 0.9));
        display: flex;
        align-items: center;
        justify-content: flex-end;
        padding-right: 16px;
        opacity: 0;
        pointer-events: none;
    }
    
    .swipeable-card.revealed .swipeable-card-actions {
        opacity: 1;
        pointer-events: auto;
    }
    
    /* Mobile-optimized tables - Horizontal Scroll */
    .table-scroll-wrapper {
        overflow-x: auto;
        -webkit-overflow-scrolling: touch;
        margin: 0 -12px;
        padding: 0 12px;
        scrollbar-width: thin;
        scrollbar-color: var(--accent-primary) transparent;
    }
    
    .table-scroll-wrapper::-webkit-scrollbar {
        height: 6px;
    }
    
    .table-scroll-wrapper::-webkit-scrollbar-track {
        background: rgba(255, 255, 255, 0.05);
        border-radius: 3px;
    }
    
    .table-scroll-wrapper::-webkit-scrollbar-thumb {
        background: var(--accent-primary);
        border-radius: 3px;
    }
    
    table.mobile-responsive {
        width: max-content;
        min-width: 100%;
        border: 1px solid var(--border-color);
        border-radius: 8px;
        border-collapse: collapse;
        background: var(--bg-card);
    }
    
    table.mobile-responsive th,
    table.mobile-responsive td {
        white-space: nowrap;
        padding: 12px 16px;
    }
    
    /* Card-based table layout (alternative) */
    table.mobile-stack {
        display: block;
        border: none;
    }
    
    table.mobile-stack thead {
        display: none;
    }
    
    table.mobile-stack tbody,
    table.mobile-stack tr,
    table.mobile-stack td {
        display: block;
    }
    
    table.mobile-stack tr {
        background: var(--bg-card);
        border: 1px solid var(--border-color);
        border-radius: 12px;
        margin-bottom: 16px;
        padding: 16px;
        box-shadow: 0 2px 8px rgba(0, 0, 0, 0.1);
    }
    
    table.mobile-stack td {
        text-align: left;
        padding: 10px 0;
        border: none;
        display: flex;
        justify-content: space-between;
        align-items: center;
    }
    
    table.mobile-stack td::before {
        content: attr(data-label);
        font-weight: 600;
        color: var(--accent-primary);
        margin-right: 12px;
        flex-shrink: 0;
    }
    
    table.mobile-stack td:not(:last-child) {
        border-bottom: 1px solid var(--border-color);
        padding-bottom: 10px;
        margin-bottom: 10px;
    }
    
    /* Pull to refresh */
    .pull-to-refresh {
        position: fixed;
        top: -80px;
        left: 0;
        right: 0;
        height: 60px;
        display: flex;
        align-items: center;
        justify-content: center;
        gap: 12px;
        background: var(--accent-gradient);
        color: white;
        transition: all 0.3s cubic-bezier(0.4, 0, 0.2, 1);
        z-index: 10000;
        opacity: 0;
        box-shadow: 0 2px 8px rgba(0, 0, 0, 0.2);
        font-weight: 600;
    }
    
    .pull-to-refresh.ready {
        opacity: 1;
    }
    
    .pull-to-refresh.active {
        top: 0;
        opacity: 1;
    }
    
    .pull-to-refresh-icon {
        font-size: 24px;
        animation: spin 1s linear infinite;
    }
    
    @keyframes spin {
        from { transform: rotate(0deg); }
        to { transform: rotate(360deg); }
    }
    
    /* Swipe gesture styles */
    .swipe-actions {
        position: absolute;
        top: 0;
        bottom: 0;
        display: flex;
        align-items: center;
        padding: 0 16px;
        opacity: 0;
        pointer-events: none;
        transition: opacity 0.2s ease;
    }
    
    .revealed .swipe-actions {
        opacity: 1;
        pointer-events: auto;
    }
    
    .swipe-action-btn {
        padding: 8px 16px;
        border: none;
        border-radius: 8px;
        color: white;
        font-weight: 600;
        font-size: 14px;
        cursor: pointer;
        display: flex;
        align-items: center;
        gap: 6px;
        min-width: 44px;
        min-height: 44px;
        transition: all 0.2s ease;
    }
    
    .swipe-action-btn.edit-btn {
        background: var(--info);
    }
    
    .swipe-action-btn.edit-btn:active {
        background: #2563eb;
        transform: scale(0.95);
    }
    
    .swipe-action-btn.delete-btn {
        background: var(--error);
    }
    
    .swipe-action-btn.delete-btn:active {
        background: #dc2626;
        transform: scale(0.95);
    }
    
    .swiping {
        transition: none;
        user-select: none;
    }
    
    /* Touch target optimization */
    .touch-target-optimized {
        min-width: 44px !important;
        min-height: 44px !important;
        display: inline-flex;
        align-items: center;
        justify-content: center;
    }
    
    /* Mobile-friendly modals */
    .modal,
    .dialog {
        width: 95vw !important;
        max-width: 95vw !important;
        max-height: 90vh !important;
        margin: 5vh auto;
    }
    
    /* Larger touch targets for close buttons */
    .modal-close,
    .dialog-close,
    .close-btn {
        min-width: 44px;
        min-height: 44px;
        font-size: 24px;
    }
    
    /* Mobile search bars */
    .search-bar,
    .global-search-input {
        font-size: 16px;
        padding: 12px 16px;
        border-radius: 24px;
    }
    
    /* Mobile cards */
    .card,
    .panel,
    .widget {
        margin: 12px 0;
        border-radius: 12px;
    }
    
    /* Reduce padding on mobile */
    .container,
    .app-container {
        padding: 16px 12px;
    }
    
    /* Mobile typography */
    h1 {
        font-size: 28px;
    }
    
    h2 {
        font-size: 24px;
    }
    
    h3 {
        font-size: 20px;
    }
    
    /* Mobile-optimized spacing */
    .section {
        margin-bottom: 24px;
    }
    
    /* Horizontal scrolling for wide content */
    .horizontal-scroll {
        overflow-x: auto;
        -webkit-overflow-scrolling: touch;
        padding-bottom: 8px;
    }
    
    .horizontal-scroll::-webkit-scrollbar {
        height: 4px;
    }
    
    .horizontal-scroll::-webkit-scrollbar-thumb {
        background: rgba(102, 126, 234, 0.5);
        border-radius: 2px;
    }
    
    /* Mobile tabs */
    .tabs {
        overflow-x: auto;
        -webkit-overflow-scrolling: touch;
        white-space: nowrap;
        border-bottom: 2px solid rgba(255, 255, 255, 0.1);
    }
    
    .tab {
        display: inline-block;
        padding: 12px 20px;
        min-width: 100px;
        text-align: center;
    }
    
    /* Mobile accordion */
    .accordion-item {
        border: 1px solid rgba(255, 255, 255, 0.1);
        border-radius: 8px;
        margin-bottom: 8px;
    }
    
    .accordion-header {
        padding: 16px;
        cursor: pointer;
        display: flex;
        justify-content: space-between;
        align-items: center;
        min-height: 44px;
    }
    
    /* Safe area insets for notched devices */
    @supports (padding: max(0px)) {
        body {
            padding-left: max(12px, env(safe-area-inset-left));
            padding-right: max(12px, env(safe-area-inset-right));
        }
        
        .mobile-bottom-nav {
            padding-bottom: max(8px, env(safe-area-inset-bottom));
        }
    }
    
    /* Prevent text selection on tap targets */
    button,
    .btn,
    .clickable {
        -webkit-tap-highlight-color: rgba(102, 126, 234, 0.3);
        user-select: none;
    }
    
    /* Loading states for mobile */
    .mobile-loading {
        display: flex;
        flex-direction: column;
        align-items: center;
        justify-content: center;
        padding: 40px 20px;
        text-align: center;
    }
    
    .mobile-loading-spinner {
        width: 40px;
        height: 40px;
        border: 4px solid rgba(102, 126, 234, 0.2);
        border-top-color: #667eea;
        border-radius: 50%;
        animation: spin 1s linear infinite;
    }
    
    /* Offline indicator */
    .offline-indicator {
        position: fixed;
        top: 0;
        left: 0;
        right: 0;
        background: #ea4335;
        color: white;
        padding: 12px;
        text-align: center;
        font-size: 14px;
        z-index: 10001;
        transform: translateY(-100%);
        transition: transform 0.3s ease;
    }
    
    .offline-indicator.visible {
        transform: translateY(0);
    }
    
    /* Mobile-optimized images */
    img {
        max-width: 100%;
        height: auto;
    }
    
    /* Lazy loading placeholder */
    img[loading="lazy"] {
        background: linear-gradient(90deg, #f0f0f0 25%, #e0e0e0 50%, #f0f0f0 75%);
        background-size: 200% 100%;
        animation: shimmer 1.5s infinite;
    }
    
    @keyframes shimmer {
        0% { background-position: -200% 0; }
        100% { background-position: 200% 0; }
    }
    
    /* ========================================================================
       MOBILE UX FIXES - Issues Resolution
       ======================================================================== */
    
    /* Fix 1: Hide duplicate hamburger menu button in header on mobile */
    #navModeToggle {
        display: none !important;
    }
    
    /* Fix 2: Hide date/time display in header on mobile */
    .header-datetime {
        display: none !important;
    }
    
    /* Fix 3: Hide duplicate dark mode toggle (keep only one in header) */
    /* The sidebar toggle is already hidden on mobile, keep the header one */
    .sidebar .dark-mode-btn {
        display: none !important;
    }
    
    /* Fix 4: Hide cards/list view toggle on mobile */
    .section-view-toggle {
        display: none !important;
    }
    
    /* Fix 6: Fix sunglasses easter egg message positioning */
    .easter-egg-message {
        position: fixed !important;
        top: auto !important;
        right: 8px !important;
        bottom: 80px !important; /* Above bottom nav */
        left: 8px !important;
        max-width: 90vw !important;
        min-width: auto !important;
        overflow-wrap: break-word;
    }
    
    .easter-egg-message::before {
        /* Reposition arrow for fixed positioning */
        top: auto !important;
        bottom: -6px !important;
        right: 20px !important;
        transform: rotate(225deg) !important;
    }
    
    /* Fix 7: Hide Quick Sig tab on mobile */
    .widget-tab[data-tab="quick-sig"],
    button[data-tab="quick-sig"] {
        display: none !important;
    }
    
    /* Optimize header spacing after removing date/time */
    .user-info-bar {
        gap: 8px;
        padding: 8px 12px;
    }
    
    .user-details {
        flex: 0 1 auto;
    }
    
    .global-search-container {
        flex: 1 1 auto;
        max-width: none;
        margin: 0 8px;
    }
    
    /* Fix 8: Prevent hamburger menu from overlapping header text on mobile */
    /* Ensure proper flexbox layout for navigation containers */
    /* Note: !important is required to override inline and higher-specificity styles from various pages */
    .top-nav,
    .nav-container,
    .mobile-header,
    .header,
    .header-content {
        display: flex !important;
        align-items: center !important;
        justify-content: flex-start !important; /* Changed from space-between to flex-start */
        flex-wrap: nowrap !important; /* Prevent wrapping */
        gap: 12px;
    }
    
    /* Show hamburger menu button on mobile */
    .mobile-menu-toggle,
    .hamburger-menu {
        display: flex !important; /* Show on mobile */
        position: relative !important; /* Ensure relative positioning */
        order: 2 !important; /* Hamburger comes after brand */
        margin-left: auto !important; /* Push to the right */
        flex-shrink: 0;
        min-width: 44px;
        min-height: 44px;
        z-index: 10;
    }
    
    /* Left-justify brand/title text and prevent it from extending under hamburger */
    .nav-brand,
    .logo-section,
    .header-left,
    .header h1,
    .header .title {
        order: 1 !important; /* Brand comes first */
        flex: 0 1 auto !important; /* Don't grow, allow shrink, auto basis */
        max-width: calc(100% - 60px) !important; /* Reserve space for hamburger */
        min-width: 0; /* Enables text truncation in flexbox */
        text-align: left !important; /* Left-justify text */
        justify-content: flex-start !important; /* Left-justify flexbox content */
        padding-right: 12px; /* Space for hamburger button */
        white-space: nowrap !important; /* Prevent wrapping for clean mobile UX */
        overflow: hidden !important; /* Hide overflow */
        text-overflow: ellipsis !important; /* Show ellipsis for long text */
    }
    
    /* Fix: Hide subtitle on mobile (index page) */
    .container .header > p {
        display: none !important;
    }
    
    /* Fix: Override title truncation for index page - show full "PharmAssist" */
    .container .header h1 {
        overflow: visible !important;
        text-overflow: unset !important;
        white-space: normal !important;
        max-width: none !important;
    }
    
    /* Hamburger menu styling improvements */
    .hamburger-menu {
        display: flex;
        flex-direction: column;
        justify-content: space-around;
        align-items: center;
        padding: 10px;
        cursor: pointer;
        background: transparent;
        border: none;
    }
    
    /* Hide nav-menu by default on mobile, show hamburger instead */
    .nav-menu {
        display: none !important;
        order: 3 !important;
    }
    
    /* Show nav-menu when active (expanded) */
    .nav-menu.active {
        display: flex !important;
        flex-direction: column !important;
        position: absolute !important;
        top: 70px !important;
        left: 0 !important;
        right: 0 !important;
        background: var(--bg-secondary) !important;
        border-top: 1px solid var(--border-color) !important;
        padding: 16px !important;
        z-index: 9 !important; /* Below hamburger */
    }
    
    /* Hide nav-actions on mobile to prevent layout conflicts */
    .nav-actions {
        display: none !important;
    }
    
    /* Hide other header buttons that compete for space */
    .header-btns,
    .header-buttons {
        display: none !important;
    }
}

/* Tablet-specific adjustments (768px - 1024px) */
@media (min-width: 769px) and (max-width: 1024px) {
    .dashboard-grid {
        grid-template-columns: repeat(2, 1fr) !important;
    }
    
    .container,
    .app-container {
        padding: 24px 20px;
    }
}

/* Small mobile devices (max 375px) */
@media (max-width: 375px) {
    button,
    .btn {
        font-size: 14px;
        padding: 10px 14px;
    }
    
    .container,
    .app-container {
        padding: 12px 8px;
    }
    
    h1 {
        font-size: 24px;
    }
    
    h2 {
        font-size: 20px;
    }
}

/* Landscape orientation on mobile */
@media (max-width: 768px) and (orientation: landscape) {
    .mobile-bottom-nav {
        display: none;
    }
    
    body.has-bottom-nav {
        padding-bottom: 0;
    }
    
    .sticky-action-btn {
        bottom: 16px;
    }
}

/* Touch event optimization */
@media (hover: none) and (pointer: coarse) {
    /* This targets touch devices */
    * {
        -webkit-tap-highlight-color: rgba(102, 126, 234, 0.2);
    }
    
    button,
    a,
    .clickable {
        cursor: pointer;
    }
}

/* Prefers reduced motion - Mobile */
@media (max-width: 768px) and (prefers-reduced-motion: reduce) {
    .mobile-collapsible-content,
    .swipeable-card,
    .pull-to-refresh,
    .hamburger-menu span {
        transition: none;
        animation: none;
    }
}
