/**
 * ============================================================================
 * MOBILE OPTIMIZATION - REFINISH CRM v2.0
 * ============================================================================
 * Optimized for field work: Customer management & Activity logging on mobile
 * 
 * Target devices:
 * - iPhone 12/13/14 (390×844)
 * - Samsung Galaxy (360×800)
 * - Small phones (320×568)
 * 
 * @version 2.0.0
 * @date 2025-01-08
 * @changelog v2: Added text labels to sidebar menu and dashboard buttons for better UX
 * ============================================================================
 */

/* ============================================================================
   1. MOBILE NAVIGATION - ENHANCED
   ============================================================================ */

@media (max-width: 767px) {
    /* Header - Compact & Touch-Friendly */
    .header-v2 {
        height: 56px !important;
        padding: 0 12px !important;
        position: sticky !important;
        top: 0;
        z-index: 1000;
        box-shadow: 0 2px 8px rgba(0, 0, 0, 0.1);
    }

    .header-left {
        flex: 1;
        display: flex;
        align-items: center;
        gap: 12px;
    }

    .mobile-menu-toggle {
        width: 44px !important;
        height: 44px !important;
        display: flex !important;
        align-items: center;
        justify-content: center;
        background: transparent;
        border: none;
        cursor: pointer;
        border-radius: 8px;
        transition: background 0.2s;
    }

    .mobile-menu-toggle:active {
        background: rgba(0, 0, 0, 0.05);
    }

    .mobile-menu-toggle i {
        font-size: 24px;
        color: #2d3748;
    }

    .logo-v2 h1 {
        font-size: 16px !important;
        font-weight: 700;
        margin: 0;
    }

    .version-badge-v2 {
        font-size: 10px !important;
        padding: 2px 6px !important;
    }

    /* Hide desktop search in header */
    .header-center {
        display: none !important;
    }

    /* Header Right - Icons Only */
    .header-right {
        display: flex;
        gap: 8px;
    }

    .header-icon-btn {
        width: 44px !important;
        height: 44px !important;
        padding: 0 !important;
        display: flex;
        align-items: center;
        justify-content: center;
        font-size: 20px;
    }

    /* Hide some header buttons on mobile */
    .header-icon-btn[title="Load Sample Pipeline Data"] {
        display: none !important;
    }

    .user-menu-v2 .user-avatar {
        padding: 8px 12px !important;
        font-size: 14px !important;
    }

    .user-menu-v2 .user-avatar span {
        display: none !important; /* Hide username text */
    }

    /* Sidebar - Full Screen Overlay */
    .sidebar-v2 {
        position: fixed !important;
        top: 0;
        left: -280px; /* Hidden by default */
        width: 280px !important;
        height: 100vh !important;
        background: #ffffff;
        box-shadow: 4px 0 12px rgba(0, 0, 0, 0.15);
        transition: left 0.3s cubic-bezier(0.4, 0, 0.2, 1);
        z-index: 1100;
        overflow-y: auto;
        -webkit-overflow-scrolling: touch;
    }

    .sidebar-v2.mobile-open {
        left: 0 !important;
    }

    /* Sidebar Overlay */
    .mobile-sidebar-overlay {
        position: fixed;
        top: 0;
        left: 0;
        right: 0;
        bottom: 0;
        background: rgba(0, 0, 0, 0.5);
        opacity: 0;
        visibility: hidden;
        transition: opacity 0.3s, visibility 0.3s;
        z-index: 1050;
    }

    .mobile-sidebar-overlay.active {
        opacity: 1;
        visibility: visible;
    }

    /* Sidebar Header */
    .sidebar-v2::before {
        content: '';
        display: block;
        height: 56px;
        background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
        margin-bottom: 16px;
    }

    /* Nav Items - Touch Optimized */
    .nav-item-v2 {
        margin: 0 12px 8px 12px !important;
    }

    .nav-item-v2 a {
        padding: 14px 16px !important;
        border-radius: 8px !important;
        font-size: 15px !important;
        min-height: 48px; /* Touch target */
        display: flex;
        align-items: center;
    }

    .nav-item-v2 a i {
        font-size: 20px !important;
        margin-right: 12px !important;
        flex-shrink: 0;
    }

    /* SHOW TEXT LABELS ON MOBILE (override desktop collapsed sidebar) */
    .nav-item-v2 span {
        display: inline !important;
        font-size: 15px !important;
        font-weight: 500 !important;
        color: inherit !important;
    }

    .nav-section-title {
        display: block !important; /* Show section titles */
        padding: 12px 16px 8px 16px !important;
        font-size: 12px !important;
        margin-top: 16px !important;
        text-transform: uppercase !important;
        font-weight: 700 !important;
        color: #6C757D !important;
        letter-spacing: 0.5px !important;
    }

    /* Show nav badges */
    .nav-badge {
        display: inline-block !important;
        margin-left: auto !important;
        padding: 2px 8px !important;
        font-size: 10px !important;
        font-weight: 600 !important;
        border-radius: 12px !important;
        background: #28A745 !important;
        color: white !important;
    }

    .nav-badge.new {
        background: #17A2B8 !important;
    }

    /* Main Content - Full Width */
    .main-content-v2 {
        margin-left: 0 !important;
        padding: 16px 12px !important;
        padding-bottom: 80px !important; /* Space for FAB */
    }

    /* Page Header - Compact */
    .page-header-v2 {
        flex-direction: column !important;
        gap: 12px !important;
        margin-bottom: 16px !important;
        padding: 16px !important;
        background: #ffffff;
        border-radius: 12px;
        box-shadow: 0 2px 8px rgba(0, 0, 0, 0.08);
    }

    .page-header-v2 h1 {
        font-size: 20px !important;
        margin: 0 !important;
    }

    .page-subtitle {
        font-size: 13px !important;
        margin: 4px 0 0 0 !important;
    }

    .page-actions {
        width: 100%;
        display: flex;
        gap: 8px;
    }

    .page-actions button,
    .page-actions .btn-primary,
    .page-actions .btn-secondary {
        flex: 1;
        justify-content: center;
        min-height: 44px;
        font-size: 14px !important;
        padding: 12px 16px !important;
    }

    /* ============================================================================
       2. CUSTOMER LIST - MOBILE CARDS
       ============================================================================ */

    /* Customer Grid - Single Column */
    .customers-grid {
        grid-template-columns: 1fr !important;
        gap: 12px !important;
    }

    /* Customer Card - Touch Optimized */
    .customer-card {
        padding: 16px !important;
        border-radius: 12px !important;
        box-shadow: 0 2px 8px rgba(0, 0, 0, 0.08) !important;
        transition: all 0.2s !important;
    }

    .customer-card:active {
        transform: scale(0.98);
        box-shadow: 0 1px 4px rgba(0, 0, 0, 0.1) !important;
    }

    .customer-card-header {
        flex-direction: column !important;
        align-items: flex-start !important;
        gap: 8px !important;
        margin-bottom: 12px !important;
    }

    .customer-name {
        font-size: 18px !important;
        font-weight: 700;
    }

    .customer-type {
        font-size: 12px !important;
        padding: 4px 10px !important;
    }

    .customer-score {
        position: absolute;
        top: 16px;
        right: 16px;
        font-size: 24px !important;
        font-weight: 800;
    }

    .customer-card-body {
        padding: 12px 0 !important;
    }

    .customer-meta {
        display: grid !important;
        grid-template-columns: 1fr 1fr !important;
        gap: 8px !important;
        font-size: 13px !important;
    }

    .customer-meta-item {
        padding: 8px !important;
        background: #f7fafc;
        border-radius: 6px;
        border-left: 3px solid #667eea;
    }

    .customer-meta-item i {
        font-size: 14px !important;
        margin-right: 6px !important;
    }

    /* Filters Bar - Mobile Sticky */
    .filters-bar-v2 {
        position: sticky;
        top: 56px;
        z-index: 900;
        background: #ffffff;
        padding: 12px !important;
        margin: -16px -12px 16px -12px !important;
        border-bottom: 1px solid #e2e8f0;
        box-shadow: 0 2px 4px rgba(0, 0, 0, 0.05);
    }

    .search-box-v2 {
        margin-bottom: 12px !important;
    }

    .search-box-v2 input {
        height: 44px !important;
        font-size: 16px !important; /* Prevent zoom on iOS */
        padding: 12px 16px 12px 40px !important;
        border-radius: 10px !important;
    }

    .filters-group {
        display: flex;
        flex-direction: column;
        gap: 8px;
    }

    .filter-select {
        height: 44px !important;
        font-size: 16px !important;
        padding: 10px 16px !important;
        border-radius: 8px !important;
        background: #ffffff;
        border: 2px solid #e2e8f0;
    }

    /* ============================================================================
       3. ACTIVITY FORMS - MOBILE OPTIMIZED
       ============================================================================ */

    /* Activity Timeline - Compact */
    .activity-timeline {
        padding: 0 !important;
    }

    .activity-timeline-item {
        padding: 12px !important;
        margin-bottom: 12px !important;
        border-radius: 10px !important;
    }

    .activity-timeline-item:active {
        background: #f7fafc;
    }

    .activity-icon {
        width: 40px !important;
        height: 40px !important;
        font-size: 18px !important;
    }

    .activity-content {
        margin-left: 12px !important;
    }

    .activity-title {
        font-size: 15px !important;
        font-weight: 600;
        margin-bottom: 4px;
    }

    .activity-description {
        font-size: 13px !important;
        line-height: 1.5;
    }

    .activity-meta {
        font-size: 12px !important;
        margin-top: 8px;
    }

    /* Activity Form - Full Screen Modal on Mobile */
    .modal.large .modal-content {
        width: 100vw !important;
        height: 100vh !important;
        max-width: 100vw !important;
        max-height: 100vh !important;
        margin: 0 !important;
        border-radius: 0 !important;
    }

    .modal-header {
        padding: 16px !important;
        position: sticky;
        top: 0;
        background: #ffffff;
        z-index: 10;
        border-bottom: 1px solid #e2e8f0;
    }

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

    .modal-body {
        padding: 16px !important;
        overflow-y: auto;
        -webkit-overflow-scrolling: touch;
    }

    .modal-footer {
        padding: 16px !important;
        position: sticky;
        bottom: 0;
        background: #ffffff;
        border-top: 1px solid #e2e8f0;
        display: flex;
        gap: 8px;
    }

    .modal-footer button {
        flex: 1;
        min-height: 48px !important;
        font-size: 16px !important;
    }

    /* Form Elements - Touch Friendly */
    .form-group {
        margin-bottom: 16px !important;
    }

    .form-group label {
        font-size: 14px !important;
        font-weight: 600;
        margin-bottom: 8px !important;
        display: block;
    }

    .form-control,
    input[type="text"],
    input[type="email"],
    input[type="tel"],
    input[type="date"],
    input[type="number"],
    select,
    textarea {
        height: 48px !important;
        font-size: 16px !important; /* Prevent iOS zoom */
        padding: 12px 16px !important;
        border-radius: 10px !important;
        border: 2px solid #e2e8f0 !important;
        width: 100% !important;
        -webkit-appearance: none;
        appearance: none;
    }

    textarea.form-control {
        height: 120px !important;
        min-height: 120px !important;
        resize: vertical;
    }

    select.form-control {
        background-image: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' width='12' height='12' viewBox='0 0 12 12'%3E%3Cpath fill='%23667eea' d='M6 9L1 4h10z'/%3E%3C/svg%3E");
        background-repeat: no-repeat;
        background-position: right 16px center;
        padding-right: 40px !important;
    }

    .form-control:focus,
    input:focus,
    select:focus,
    textarea:focus {
        border-color: #667eea !important;
        box-shadow: 0 0 0 3px rgba(102, 126, 234, 0.1) !important;
        outline: none;
    }

    /* Form Row - Stack on Mobile */
    .form-row {
        flex-direction: column !important;
        gap: 0 !important;
    }

    .form-row .form-group {
        width: 100% !important;
        margin-right: 0 !important;
    }

    /* ============================================================================
       4. FLOATING ACTION BUTTON (FAB)
       ============================================================================ */

    .mobile-fab {
        position: fixed;
        bottom: 20px;
        right: 20px;
        width: 60px;
        height: 60px;
        background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
        border-radius: 50%;
        box-shadow: 0 6px 20px rgba(102, 126, 234, 0.4);
        display: flex;
        align-items: center;
        justify-content: center;
        color: #ffffff;
        font-size: 28px;
        border: none;
        cursor: pointer;
        z-index: 900;
        transition: all 0.3s cubic-bezier(0.4, 0, 0.2, 1);
        -webkit-tap-highlight-color: transparent;
    }

    .mobile-fab:active {
        transform: scale(0.9);
    }

    .mobile-fab.open {
        transform: rotate(45deg);
    }

    /* FAB Menu */
    .mobile-fab-menu {
        position: fixed;
        bottom: 90px;
        right: 20px;
        display: flex;
        flex-direction: column;
        gap: 12px;
        opacity: 0;
        visibility: hidden;
        transform: translateY(20px);
        transition: all 0.3s cubic-bezier(0.4, 0, 0.2, 1);
        z-index: 899;
    }

    .mobile-fab-menu.open {
        opacity: 1;
        visibility: visible;
        transform: translateY(0);
    }

    .mobile-fab-menu-item {
        display: flex;
        align-items: center;
        gap: 12px;
        background: #ffffff;
        padding: 12px 20px;
        border-radius: 24px;
        box-shadow: 0 4px 12px rgba(0, 0, 0, 0.15);
        cursor: pointer;
        transition: all 0.2s;
        white-space: nowrap;
    }

    .mobile-fab-menu-item:active {
        transform: scale(0.95);
    }

    .mobile-fab-menu-item i {
        width: 24px;
        height: 24px;
        display: flex;
        align-items: center;
        justify-content: center;
        background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
        color: #ffffff;
        border-radius: 50%;
        font-size: 12px;
    }

    .mobile-fab-menu-item span {
        font-size: 14px;
        font-weight: 600;
        color: #2d3748;
    }

    /* FAB Backdrop */
    .mobile-fab-backdrop {
        position: fixed;
        top: 0;
        left: 0;
        right: 0;
        bottom: 0;
        background: rgba(0, 0, 0, 0.3);
        opacity: 0;
        visibility: hidden;
        transition: opacity 0.3s, visibility 0.3s;
        z-index: 898;
    }

    .mobile-fab-backdrop.open {
        opacity: 1;
        visibility: visible;
    }

    /* ============================================================================
       5. TOUCH GESTURES & INTERACTIONS
       ============================================================================ */

    /* Swipeable Cards */
    .swipeable-card {
        position: relative;
        touch-action: pan-y;
    }

    /* Pull to Refresh */
    .pull-to-refresh {
        position: absolute;
        top: -60px;
        left: 0;
        right: 0;
        height: 60px;
        display: flex;
        align-items: center;
        justify-content: center;
        color: #667eea;
        font-size: 14px;
        font-weight: 600;
        transition: transform 0.3s;
    }

    .pull-to-refresh.pulling {
        transform: translateY(60px);
    }

    .pull-to-refresh i {
        animation: spin 1s linear infinite;
    }

    /* Long Press Actions */
    .long-pressable {
        -webkit-user-select: none;
        user-select: none;
    }

    /* Haptic Feedback Classes */
    .haptic-light:active {
        animation: haptic-pulse 0.1s;
    }

    @keyframes haptic-pulse {
        0%, 100% { transform: scale(1); }
        50% { transform: scale(0.98); }
    }

    /* ============================================================================
       6. MOBILE DASHBOARD - SIMPLIFIED
       ============================================================================ */

    /* Dashboard Hero - More Compact */
    .dashboard-hero-header {
        padding: 20px 16px !important;
        margin-bottom: 16px !important;
        border-radius: 12px !important;
    }

    .dashboard-hero-text h1 {
        font-size: 22px !important;
    }

    .dashboard-hero-text p {
        font-size: 13px !important;
    }

    .dashboard-hero-stats {
        display: grid !important;
        grid-template-columns: repeat(3, 1fr) !important;
        gap: 8px !important;
        margin-top: 12px !important;
    }

    .dashboard-hero-stat {
        padding: 12px !important;
        min-width: auto !important;
    }

    .dashboard-hero-stat-label {
        font-size: 10px !important;
    }

    .dashboard-hero-stat-value {
        font-size: 20px !important;
    }

    /* KPI Cards - 1 Column */
    .dashboard-kpi-grid {
        grid-template-columns: 1fr !important;
        gap: 12px !important;
    }

    .kpi-card {
        padding: 16px !important;
    }

    /* Quick Actions - Horizontal Scroll */
    .quick-actions-bar {
        display: flex !important;
        overflow-x: auto !important;
        -webkit-overflow-scrolling: touch;
        gap: 12px !important;
        padding: 16px 0 !important;
        margin: 0 -12px !important;
        padding-left: 12px !important;
        scroll-snap-type: x mandatory;
    }

    .quick-action {
        min-width: 140px !important;
        flex-shrink: 0;
        scroll-snap-align: start;
        padding: 16px !important;
        border-radius: 12px !important;
    }

    /* SHOW TEXT IN QUICK ACTION BUTTONS (override dashboard-hybrid.css) */
    .quick-action-btn {
        display: flex !important;
        flex-direction: column !important;
        align-items: center !important;
        gap: 8px !important;
        padding: 12px 16px !important;
        min-width: 110px !important;
        flex-shrink: 0;
        scroll-snap-align: start;
    }

    .quick-action-btn i {
        font-size: 24px !important;
        margin-bottom: 4px !important;
    }

    .quick-action-btn span {
        display: block !important; /* SHOW text labels */
        font-size: 13px !important;
        font-weight: 500 !important;
        text-align: center !important;
        line-height: 1.2 !important;
        white-space: normal !important;
        word-wrap: break-word !important;
    }

    /* Charts - Stack */
    .dashboard-charts-grid {
        grid-template-columns: 1fr !important;
        gap: 16px !important;
    }

    .chart-card {
        min-height: 280px !important;
    }

    /* Bottom Grid - Stack */
    .dashboard-bottom-grid {
        grid-template-columns: 1fr !important;
        gap: 16px !important;
    }

    /* ============================================================================
       7. MOBILE-SPECIFIC UTILITIES
       ============================================================================ */

    /* Hide Desktop-Only Elements */
    .desktop-only {
        display: none !important;
    }

    /* Show Mobile-Only Elements */
    .mobile-only {
        display: block !important;
    }

    /* Scrollbar - Minimal */
    ::-webkit-scrollbar {
        width: 4px;
        height: 4px;
    }

    ::-webkit-scrollbar-track {
        background: transparent;
    }

    ::-webkit-scrollbar-thumb {
        background: rgba(0, 0, 0, 0.2);
        border-radius: 2px;
    }

    /* Safe Area Insets (iPhone notch) */
    @supports (padding-top: env(safe-area-inset-top)) {
        .header-v2 {
            padding-top: calc(env(safe-area-inset-top) + 8px) !important;
            height: calc(56px + env(safe-area-inset-top)) !important;
        }

        .sidebar-v2 {
            padding-top: env(safe-area-inset-top) !important;
        }

        .mobile-fab {
            bottom: calc(20px + env(safe-area-inset-bottom)) !important;
        }

        .modal.large .modal-content {
            padding-top: env(safe-area-inset-top) !important;
            padding-bottom: env(safe-area-inset-bottom) !important;
        }
    }

    /* Prevent Zoom on Input Focus (iOS) */
    input[type="text"],
    input[type="email"],
    input[type="tel"],
    input[type="number"],
    input[type="date"],
    select,
    textarea {
        font-size: 16px !important;
    }

    /* Loading Spinner - Mobile */
    .mobile-loading {
        position: fixed;
        top: 50%;
        left: 50%;
        transform: translate(-50%, -50%);
        z-index: 9999;
    }

    .mobile-loading-spinner {
        width: 48px;
        height: 48px;
        border: 4px solid #f3f4f6;
        border-top-color: #667eea;
        border-radius: 50%;
        animation: spin 0.8s linear infinite;
    }

    /* Toast Notifications - Bottom */
    .notification {
        bottom: 80px !important; /* Above FAB */
        left: 12px !important;
        right: 12px !important;
        width: auto !important;
        border-radius: 12px !important;
        padding: 16px !important;
        font-size: 14px !important;
    }

    /* Empty States */
    .empty-state {
        padding: 40px 20px !important;
        text-align: center;
    }

    .empty-state i {
        font-size: 48px !important;
        margin-bottom: 16px;
    }

    .empty-state p {
        font-size: 14px !important;
    }

    /* ============================================================================
       8. PERFORMANCE OPTIMIZATIONS
       ============================================================================ */

    /* Reduce animations for better performance */
    @media (prefers-reduced-motion: reduce) {
        * {
            animation-duration: 0.01ms !important;
            animation-iteration-count: 1 !important;
            transition-duration: 0.01ms !important;
        }
    }

    /* Hardware acceleration */
    .customer-card,
    .activity-timeline-item,
    .modal,
    .mobile-fab,
    .sidebar-v2 {
        transform: translateZ(0);
        -webkit-transform: translateZ(0);
        will-change: transform;
    }
}

/* ============================================================================
   9. LANDSCAPE MODE OPTIMIZATIONS
   ============================================================================ */

@media (max-width: 767px) and (orientation: landscape) {
    .dashboard-hero-header {
        padding: 16px !important;
    }

    .dashboard-hero-stats {
        grid-template-columns: repeat(3, 1fr) !important;
    }

    .modal.large .modal-content {
        overflow-y: auto;
    }

    .modal-body {
        max-height: 60vh;
    }
}

/* ============================================================================
   10. ACCESSIBILITY
   ============================================================================ */

@media (max-width: 767px) {
    /* Focus Visible */
    *:focus-visible {
        outline: 3px solid #667eea;
        outline-offset: 2px;
    }

    /* Skip to Content */
    .skip-to-content {
        position: fixed;
        top: -100px;
        left: 0;
        background: #667eea;
        color: white;
        padding: 12px 20px;
        z-index: 10000;
        transition: top 0.3s;
    }

    .skip-to-content:focus {
        top: 0;
    }

    /* Screen Reader Only */
    .sr-only {
        position: absolute;
        width: 1px;
        height: 1px;
        padding: 0;
        margin: -1px;
        overflow: hidden;
        clip: rect(0, 0, 0, 0);
        white-space: nowrap;
        border: 0;
    }
}
