/* ================================================================
   PWA & MOBILE OPTIMIZATIONS
   Owner & Programmer: Onyeka E. Henry
   Copyright (c) 2025 Onyeka E. Henry. All rights reserved.
   ================================================================ */

/* PWA Safe Areas — only for real installed PWA, NOT the native WebView.
   In the native app, React Native handles safe areas at the RN layer.
   Guard: .native-app class AND [data-native] attribute are both injected by
   injectedJavaScriptBeforeContentLoaded so at least one is always present on
   all Android/Samsung WebView versions, even when CSS parses before JS runs. */
html:not(.native-app):not([data-native]) body.pwa-mode {
    padding-bottom: env(safe-area-inset-bottom, 0px);
    padding-left: env(safe-area-inset-left, 0px);
    padding-right: env(safe-area-inset-right, 0px);
}

/* Belt-and-suspenders: even if pwa-mode somehow gets applied in native app, zero it out */
html.native-app body.pwa-mode {
    padding-top: 0 !important;
    padding-bottom: 0 !important;
}

/* PWA Install Button */
.pwa-install-button {
    position: fixed;
    top: 80px;
    right: 20px;
    z-index: 1000;
    padding: 12px 24px;
    border-radius: var(--radius-md);
    background: linear-gradient(135deg, var(--primary-color), var(--secondary-color));
    color: white;
    border: none;
    box-shadow: var(--shadow-lg);
    font-weight: 600;
    display: flex;
    align-items: center;
    gap: 8px;
    cursor: pointer;
    transition: all 0.3s ease;
    animation: pulse-install 2s infinite;
}

.pwa-install-button:hover {
    transform: translateY(-2px);
    box-shadow: var(--shadow-xl);
}

.pwa-install-button i {
    font-size: 1.2em;
}

@keyframes pulse-install {
    0%, 100% {
        box-shadow: 0 0 0 0 rgba(14, 165, 233, 0.7);
    }
    50% {
        box-shadow: 0 0 0 10px rgba(14, 165, 233, 0);
    }
}

/* PWA Install Badge */
.pwa-install-badge {
    position: fixed;
    bottom: -200px;
    left: 0;
    right: 0;
    background: white;
    box-shadow: 0 -4px 20px rgba(0, 0, 0, 0.15);
    padding: 20px;
    z-index: 9999;
    transition: bottom 0.4s cubic-bezier(0.68, -0.55, 0.265, 1.55);
}

.pwa-install-badge.show {
    bottom: 0;
}

/* Raise Ask Me FAB so it clears the install banner (JS sets inline style; this is a fallback) */
.support-widget {
    transition: bottom 0.4s cubic-bezier(0.68, -0.55, 0.265, 1.55);
}

.pwa-install-badge .badge-content {
    display: flex;
    align-items: center;
    gap: 15px;
    max-width: 1200px;
    margin: 0 auto;
}

.pwa-install-badge i {
    font-size: 2.5em;
    color: var(--primary-color);
}

.pwa-install-badge strong {
    display: block;
    font-size: 1.1em;
    margin-bottom: 4px;
}

.pwa-install-badge p {
    margin: 0;
    color: var(--text-secondary);
    font-size: 0.9em;
}

.install-badge-btn {
    margin-left: auto;
    padding: 10px 24px;
    background: var(--primary-color);
    color: white;
    border: none;
    border-radius: var(--radius-md);
    font-weight: 600;
    cursor: pointer;
    transition: all 0.3s ease;
}

.install-badge-btn:hover {
    background: var(--primary-dark);
    transform: scale(1.05);
}

.close-badge-btn {
    background: none;
    border: none;
    font-size: 1.5em;
    color: var(--text-light);
    cursor: pointer;
    padding: 0 10px;
    transition: color 0.3s ease;
}

.close-badge-btn:hover {
    color: var(--text-primary);
}

/* Mobile Touch Improvements */
@media (hover: none) {
    /* Remove hover effects on touch devices */
    .btn:hover,
    .card:hover {
        transform: none;
    }
    
    /* Add active states for touch */
    .btn:active {
        transform: scale(0.98);
    }
    
    .card:active {
        transform: scale(0.99);
    }
}

/* Touch-Friendly Buttons */
.btn {
    min-height: 44px;
    min-width: 44px;
    -webkit-tap-highlight-color: transparent;
    user-select: none;
    -webkit-user-select: none;
}

/* Network Status Notifications */
.network-notification {
    position: fixed;
    top: -100px;
    left: 50%;
    transform: translateX(-50%);
    padding: 12px 24px;
    border-radius: var(--radius-md);
    box-shadow: var(--shadow-lg);
    z-index: 9999;
    transition: top 0.4s ease;
    display: flex;
    align-items: center;
    gap: 10px;
    font-weight: 600;
    color: white;
}

.network-notification.show {
    top: 20px;
}

.network-notification.online {
    background: var(--success-color);
}

.network-notification.offline {
    background: var(--error-color);
}

/* Standalone PWA Mode — only for real installed PWA, NOT native WebView.
   React Native WebView matches display-mode:standalone but handles safe area
   itself; applying env(safe-area-inset-top) here (which can be ~400dp on
   Samsung S9+/Android 9-10 or Android 15 edge-to-edge devices) pushes content
   to the lower half.
   Double guard: .native-app class AND [data-native] attribute must BOTH be
   absent for this rule to fire — the native app sets both via JS injection. */
@media (display-mode: standalone) {
    html:not(.native-app):not([data-native]) body {
        padding-bottom: env(safe-area-inset-bottom, 0px);
    }
    
    /* Hide install prompts when already installed */
    .pwa-install-button,
    .pwa-install-badge {
        display: none !important;
    }
}

/* Native app WebView — Chromium on Android matches display-mode:standalone when running
   full-screen inside a native activity, which causes env(safe-area-inset-top) to be
   applied as body padding-top (often huge on Android 15 edge-to-edge devices).
   Override it to zero so the WebView content is never pushed down. */
html.native-app body {
    padding-top: 0 !important;
}

/* Mobile Bottom Navigation */
.mobile-bottom-nav {
    display: none;
}

html.native-app .native-app-icon {
    display: inline-flex;
    align-items: center;
    justify-content: center;
    line-height: 1;
    vertical-align: middle;
}

html.native-app .native-icon-svg {
    width: 1em;
    height: 1em;
    display: block;
}

/* Hide web bottom nav in native app — replaced by the native tab bar */
html.native-app .mobile-bottom-nav {
    display: none !important;
}

html.native-app .mobile-bottom-nav::-webkit-scrollbar {
    display: none;
}

html.native-app .mobile-bottom-nav a {
    flex: 0 0 auto;
    min-width: 60px;
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    padding: 5px 8px;
    color: rgba(255,255,255,0.7);
    text-decoration: none;
    font-size: 0.65em;
    font-weight: 500;
    transition: color 0.3s ease;
    white-space: nowrap;
}

html.native-app .mobile-bottom-nav a.active {
    color: #ffffff;
}

html.native-app .mobile-bottom-nav a i {
    font-size: 1.3em;
    margin-bottom: 2px;
}

html.native-app .mobile-bottom-nav a .mobile-nav-icon {
    display: inline-flex;
    align-items: center;
    justify-content: center;
    font-size: 1.3em;
    line-height: 1;
    margin-bottom: 2px;
    min-height: 1.1em;
}

html.native-app .mobile-bottom-nav a .mobile-icon-svg {
    width: 1.15em;
    height: 1.15em;
    display: block;
}

html.native-app main,
html.native-app .main-content,
html.native-app .container {
    padding-bottom: calc(70px + env(safe-area-inset-bottom));
}

@media (max-width: 768px) {
    .mobile-bottom-nav {
        display: flex !important;
        position: fixed !important;
        bottom: 0 !important;
        left: 0;
        right: 0;
        background: #0d0d0d;
        border-top: 1px solid rgba(255,255,255,0.1);
        padding: 4px 0;
        padding-bottom: max(4px, env(safe-area-inset-bottom));
        align-items: stretch;
        z-index: 99999;
        box-shadow: 0 -2px 10px rgba(0, 0, 0, 0.3);
        transform: none !important;
        will-change: auto;
        contain: layout;
        overflow-x: auto;
        overflow-y: hidden;
        -webkit-overflow-scrolling: touch;
        scrollbar-width: none;
        -ms-overflow-style: none;
        flex-wrap: nowrap;
        justify-content: space-around;
    }

    .mobile-bottom-nav::-webkit-scrollbar {
        display: none;
    }
    
    .mobile-bottom-nav a {
        flex: 0 0 auto;
        min-width: 60px;
        display: flex;
        flex-direction: column;
        align-items: center;
        justify-content: center;
        padding: 5px 8px;
        color: rgba(255,255,255,0.7);
        text-decoration: none;
        font-size: 0.65em;
        font-weight: 500;
        transition: color 0.3s ease;
        white-space: nowrap;
    }
    
    .mobile-bottom-nav a.active {
        color: #ffffff;
    }
    
    .mobile-bottom-nav a i {
        font-size: 1.3em;
        margin-bottom: 2px;
    }

    .mobile-bottom-nav a .mobile-nav-icon {
        display: inline-flex;
        align-items: center;
        justify-content: center;
        font-size: 1.3em;
        line-height: 1;
        margin-bottom: 2px;
        min-height: 1.1em;
    }

    .mobile-bottom-nav a .mobile-icon-svg {
        width: 1.15em;
        height: 1.15em;
        display: block;
    }

    /* Emoji fallback when Font Awesome hasn't loaded yet */
    .mobile-bottom-nav a i[data-emoji]::before {
        font-family: 'Font Awesome 6 Free', 'Font Awesome 6 Brands', sans-serif;
    }
    .mobile-bottom-nav a i[data-emoji]:empty::before,
    .mobile-bottom-nav a i[data-emoji].fa-home:not(.fa)::before {
        /* Default FA handles this, no override needed */
    }
    @supports not (font-family: 'Font Awesome 6 Free') {
        .mobile-bottom-nav a i[data-emoji]::before {
            content: attr(data-emoji);
            font-family: inherit;
            font-style: normal;
        }
    }
    /* JS-based fallback: if FA icon has zero width, show emoji */
    .mobile-bottom-nav a i.fa-fallback {
        font-style: normal;
        font-family: inherit;
    }
    .mobile-bottom-nav a i.fa-fallback::before {
        content: attr(data-emoji) !important;
        font-family: inherit !important;
    }
}

/* Global FA emoji fallback - works on ALL pages */
i.fa-fallback, .fa-fallback {
    font-style: normal !important;
    font-family: inherit !important;
    -moz-osx-font-smoothing: auto !important;
    -webkit-font-smoothing: auto !important;
}
i.fa-fallback::before, .fa-fallback::before {
    content: attr(data-emoji) !important;
    font-family: inherit !important;
    font-weight: normal !important;
    font-style: normal !important;
}
/* Hide FA spin animation on fallback icons */
i.fa-fallback.fa-spin::before {
    animation: none !important;
}

@media (max-width: 768px) {
    body.pwa-mode main,
    body.pwa-mode .container {
        padding-bottom: calc(70px + env(safe-area-inset-bottom));
    }

    /* Ensure bottom padding for all mobile users with bottom nav */
    main, .main-content {
        padding-bottom: calc(70px + env(safe-area-inset-bottom));
    }

    /* Currency button style in bottom nav */
    .mobile-bottom-nav .mobile-currency-btn {
        background: none;
        border: none;
    }

    /* Logout button style in bottom nav */
    .mobile-bottom-nav .mobile-logout-btn {
        color: #ff6b6b !important;
    }
    .mobile-bottom-nav .mobile-logout-btn i {
        color: #ff6b6b !important;
    }
}
