/**
 * Mobile Features Stylesheet
 * Scroll to top, bottom nav, swipe gestures, pull-to-refresh
 * Optimized to prevent overlapping elements
 */

/* ===================================
   Z-INDEX HIERARCHY
   =================================== */
:root {
    --z-swipe-indicator: 997;
    --z-bottom-nav: 998;
    --z-scroll-top: 999;
    --z-navbar: 1000;
    --z-pull-refresh: 1001;
    --z-chat-widget: 996; /* Below everything else */
}

/* ===================================
   SCROLL TO TOP BUTTON
   =================================== */
.scroll-to-top {
    position: fixed;
    bottom: 30px;
    right: 30px;
    width: 50px;
    height: 50px;
    background: var(--color-accent-gradient);
    border-radius: 50%;
    border: none;
    cursor: pointer;
    display: flex;
    align-items: center;
    justify-content: center;
    box-shadow: 0 4px 20px var(--color-glow);
    opacity: 0;
    visibility: hidden;
    transform: translateY(20px);
    transition: all 0.3s ease;
    z-index: var(--z-scroll-top);
}

.scroll-to-top.visible {
    opacity: 1;
    visibility: visible;
    transform: translateY(0);
}

.scroll-to-top:hover {
    transform: translateY(-5px);
    box-shadow: 0 6px 30px rgba(99, 102, 241, 0.4);
}

.scroll-to-top svg {
    width: 24px;
    height: 24px;
    fill: white;
}

/* Mobile adjustment - above bottom nav with safe spacing */
@media (max-width: 768px) {
    .scroll-to-top {
        bottom: 100px; /* Safe distance above bottom nav */
        right: 15px; /* Closer to edge to avoid other floating buttons */
        width: 48px;
        height: 48px;
    }
    
    .scroll-to-top:hover {
        transform: translateY(0) scale(1.05);
    }
}

/* ===================================
   BOTTOM NAVIGATION BAR (MOBILE)
   =================================== */
.bottom-nav {
    position: fixed;
    bottom: 0;
    left: 0;
    right: 0;
    background: rgba(19, 19, 26, 0.98);
    border-top: 1px solid var(--color-border);
    padding: 8px 0 max(8px, env(safe-area-inset-bottom));
    display: none;
    z-index: var(--z-bottom-nav);
    backdrop-filter: blur(20px);
    -webkit-backdrop-filter: blur(20px);
    box-shadow: 0 -4px 20px rgba(0, 0, 0, 0.3);
}

.bottom-nav-content {
    display: flex;
    justify-content: space-evenly; /* Better distribution */
    align-items: center;
    max-width: 100%;
    margin: 0 auto;
    padding: 0 8px;
}

.bottom-nav-item {
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    padding: 10px 6px; /* Reduced horizontal padding */
    color: var(--color-text-secondary);
    text-decoration: none;
    transition: all 0.2s ease;
    flex: 1;
    position: relative;
    min-height: 56px;
    max-width: 75px; /* Prevent stretching */
    min-width: 60px; /* Ensure minimum size */
}

.bottom-nav-item:hover,
.bottom-nav-item.active {
    color: var(--color-accent-primary);
}

.bottom-nav-item.active::before {
    content: '';
    position: absolute;
    top: 0;
    left: 50%;
    transform: translateX(-50%);
    width: 35px;
    height: 3px;
    background: var(--color-accent-gradient);
    border-radius: 0 0 3px 3px;
}

.bottom-nav-icon {
    width: 24px;
    height: 24px;
    margin-bottom: 4px;
    transition: transform 0.2s ease;
}

.bottom-nav-item.active .bottom-nav-icon {
    transform: scale(1.15);
}

.bottom-nav-label {
    font-size: 10px;
    font-weight: 500;
    white-space: nowrap;
    text-overflow: ellipsis;
    overflow: hidden;
    max-width: 100%;
}

/* Show on mobile */
@media (max-width: 768px) {
    .bottom-nav {
        display: block;
    }
    
    /* Add padding to body to prevent content hiding behind bottom nav */
    body {
        padding-bottom: 85px; /* Extra spacing for safe zone */
    }
    
    /* Ensure main content doesn't overlap */
    main {
        min-height: calc(100vh - 85px);
    }
    
    /* Hide floating chat button on mobile to reduce clutter */
    .chat-widget-button,
    .floating-chat-btn,
    [class*="chat-float"] {
        display: none !important;
    }
}

/* Extra small screens */
@media (max-width: 375px) {
    .bottom-nav-label {
        font-size: 10px;
    }
    
    .bottom-nav-icon {
        width: 22px;
        height: 22px;
    }
}

/* ===================================
   PULL TO REFRESH
   =================================== */
.pull-to-refresh {
    position: fixed;
    top: -80px;
    left: 50%;
    transform: translateX(-50%);
    width: 60px;
    height: 60px;
    background: var(--color-bg-secondary);
    border-radius: 50%;
    display: flex;
    align-items: center;
    justify-content: center;
    box-shadow: 0 4px 20px rgba(0, 0, 0, 0.3);
    transition: top 0.3s ease;
    z-index: var(--z-pull-refresh);
    pointer-events: none; /* Don't interfere with other elements */
}

.pull-to-refresh.active {
    top: max(20px, env(safe-area-inset-top)); /* Safe area for notched phones */
}

.pull-to-refresh .refresh-icon {
    width: 30px;
    height: 30px;
    animation: spin 1s linear infinite;
    fill: var(--color-accent-primary);
}

.pull-to-refresh.ready .refresh-icon {
    animation: none;
    transform: rotate(180deg);
}

@keyframes spin {
    from { transform: rotate(0deg); }
    to { transform: rotate(360deg); }
}

/* Only show on mobile touch devices */
@media (max-width: 768px) and (hover: none) {
    .pull-to-refresh {
        display: flex;
    }
}

@media (min-width: 769px) {
    .pull-to-refresh {
        display: none !important;
    }
}

/* ===================================
   SWIPE GESTURE INDICATORS
   =================================== */
.swipe-indicator {
    position: fixed;
    top: 50%;
    transform: translateY(-50%);
    width: 50px;
    height: 80px;
    background: var(--color-bg-card);
    border-radius: var(--radius-md);
    display: flex;
    align-items: center;
    justify-content: center;
    opacity: 0;
    pointer-events: none;
    transition: opacity 0.2s ease;
    z-index: var(--z-swipe-indicator);
}

.swipe-indicator.left {
    left: 10px; /* Closer to edge for less interference */
}

.swipe-indicator.right {
    right: 10px; /* Closer to edge for less interference */
}

.swipe-indicator.visible {
    opacity: 0.8; /* Slightly more visible */
}

.swipe-indicator svg {
    width: 30px;
    height: 30px;
    fill: var(--color-accent-primary);
}

/* Adjust on mobile to avoid bottom nav area */
@media (max-width: 768px) {
    .swipe-indicator {
        top: calc(50% - 40px); /* Shift up to avoid bottom nav zone */
        height: 70px;
    }
}

/* Hide on desktop */
@media (min-width: 769px) {
    .swipe-indicator {
        display: none !important;
    }
}

/* ===================================
   STICKY HEADER ENHANCEMENTS
   =================================== */
.navbar {
    transition: transform 0.3s ease, background 0.3s ease, box-shadow 0.3s ease;
    position: fixed;
    top: 0;
    left: 0;
    right: 0;
    z-index: var(--z-navbar);
}

.navbar.scrolled {
    background: rgba(19, 19, 26, 0.95);
    backdrop-filter: blur(10px);
    -webkit-backdrop-filter: blur(10px);
    box-shadow: 0 4px 20px rgba(0, 0, 0, 0.3);
}

/* Hide nav on scroll down, show on scroll up */
.navbar.hidden {
    transform: translateY(-100%);
}

/* Ensure content doesn't hide under navbar */
body {
    padding-top: 60px; /* Adjust based on your navbar height */
}

@media (max-width: 768px) {
    body {
        padding-top: 56px; /* Slightly smaller on mobile */
    }
}

/* ===================================
   TOUCH OPTIMIZATIONS
   =================================== */
/* Larger touch targets for mobile */
@media (max-width: 768px) {
    /* Only apply to actual interactive elements, not all links */
    .bottom-nav-item,
    button:not(.nav-toggle),
    .scroll-to-top {
        min-height: 44px;
        min-width: 44px;
    }
    
    /* Prevent text selection during swipes */
    body.swiping {
        user-select: none;
        -webkit-user-select: none;
        -webkit-touch-callout: none;
    }
    
    /* Prevent overscroll bounce during pull to refresh */
    body.pulling {
        overflow: hidden;
        position: fixed;
        width: 100%;
    }
}

/* ===================================
   SAFE ZONES & SPACING
   =================================== */
/* Prevent content from hiding behind mobile UI elements */
@media (max-width: 768px) {
    /* Safe zone for scroll to top button */
    .scroll-zone-safe {
        padding-bottom: 120px; /* Extra space at bottom for scroll button */
    }
    
    /* Ensure sections have proper spacing from bottom */
    section:last-of-type {
        padding-bottom: 100px; /* Extra padding on last section */
    }
    
    /* Prevent fixed elements from covering important content */
    .content-wrapper {
        padding: 0 16px;
        margin-bottom: 100px; /* Space for bottom nav */
    }
}

/* ===================================
   LOADING STATE
   =================================== */
body.refreshing {
    pointer-events: none;
    overflow: hidden;
}

body.refreshing .pull-to-refresh {
    opacity: 1;
}

body.refreshing::after {
    content: '';
    position: fixed;
    top: 0;
    left: 0;
    right: 0;
    bottom: 0;
    background: rgba(0, 0, 0, 0.2);
    z-index: 1000;
    pointer-events: none;
}

/* ===================================
   HAPTIC FEEDBACK (iOS Safari)
   =================================== */
@media (hover: none) and (pointer: coarse) {
    .bottom-nav-item:active {
        transform: scale(0.92);
        transition: transform 0.1s ease;
    }
    
    .scroll-to-top:active {
        transform: scale(0.9);
        transition: transform 0.1s ease;
    }
    
    button:active {
        transform: scale(0.95);
        transition: transform 0.1s ease;
    }
}

/* ===================================
   VISIBILITY CONTROLS
   =================================== */
/* Hide mobile features on desktop */
@media (min-width: 769px) {
    .bottom-nav,
    .pull-to-refresh,
    .swipe-indicator {
        display: none !important;
    }
    
    body {
        padding-bottom: 0 !important;
    }
}

/* Show features only when JavaScript is enabled */
.no-js .scroll-to-top,
.no-js .bottom-nav,
.no-js .pull-to-refresh,
.no-js .swipe-indicator {
    display: none !important;
}

/* ===================================
   NAVBAR MOBILE FIXES
   =================================== */
@media (max-width: 768px) {
    /* Fix navbar layout to prevent overlap */
    .nav-content {
        position: relative;
        display: grid;
        grid-template-columns: auto 1fr auto;
        align-items: center;
        gap: 1rem;
        padding: 0.75rem 0;
    }
    
    .nav-logo {
        grid-column: 1;
        font-size: 1.25rem;
    }
    
    .nav-toggle {
        grid-column: 3;
        justify-self: end;
    }
    
    /* Hide theme toggle on mobile or move it */
    .theme-toggle {
        display: none !important;
    }
    
    /* Or if you want to keep it, move it to menu */
    .nav-menu .theme-toggle {
        display: block;
        width: 100%;
        margin-top: 1rem;
    }
    
    /* Ensure buttons don't overlap */
    .nav-content > button,
    .nav-content > .theme-toggle {
        margin-left: 0.5rem;
    }
}

/* ===================================
   ACCESSIBILITY
   =================================== */
.scroll-to-top:focus,
.bottom-nav-item:focus {
    outline: 2px solid var(--color-accent-primary);
    outline-offset: 2px;
}

/* Reduce motion for users who prefer it */
@media (prefers-reduced-motion: reduce) {
    .scroll-to-top,
    .bottom-nav-item,
    .pull-to-refresh,
    .swipe-indicator {
        transition: none;
        animation: none;
    }
}

/* ===================================
   MOBILE CLEANUP - HIDE CONFLICTING ELEMENTS
   =================================== */
@media (max-width: 768px) {
    /* Hide all floating chat widgets and buttons that might overlap */
    #chat-widget,
    .chat-widget,
    .chat-button,
    .floating-chat,
    [id*="chat-float"],
    [class*="chat-float"],
    [id*="tawk"],
    [class*="tawk"],
    .crisp-client,
    #crisp-chatbox,
    .intercom-launcher,
    #intercom-container {
        display: none !important;
        visibility: hidden !important;
    }
    
    /* Hide duplicate scroll to top buttons */
    #scrollTop,
    .scroll-top,
    .back-to-top,
    [id*="scroll-top"]:not(.scroll-to-top) {
        display: none !important;
    }
    
    /* Hide theme customizer if it overlaps */
    #theme-customizer-panel {
        display: none;
    }
    
    /* Keep dark mode toggle but ensure it doesn't overlap */
    #darkModeToggle {
        position: fixed;
        top: 70px; /* Below navbar */
        right: 15px;
        width: 40px;
        height: 40px;
        z-index: 999;
    }
}
