/**
 * Clean Mobile Layout System for Quickie
 * Mobile-first approach with proper safe area handling
 * Replaces: mobile-safe-areas.css, android-safe-area.css, inline CSS
 */

/* ==========================================================================
   CSS Variables - Enhanced Safe Area System with WebView Fallbacks
   ========================================================================== */

:root {
    /* Safe area variables (iOS/modern browsers with fallbacks) */
    --safe-top: env(safe-area-inset-top, 0px);
    --safe-bottom: env(safe-area-inset-bottom, 0px);
    --safe-left: env(safe-area-inset-left, 0px);
    --safe-right: env(safe-area-inset-right, 0px);
    
    /* Enhanced safe area calculations for different scenarios */
    --safe-top-header: max(env(safe-area-inset-top, 0px), 20px);
    --safe-top-minimal: max(env(safe-area-inset-top, 0px), 8px);
    --safe-bottom-nav: max(env(safe-area-inset-bottom, 0px), 20px);
    --safe-sides: max(env(safe-area-inset-left, 0px), env(safe-area-inset-right, 0px));
    
    /* App layout constants */
    --mobile-nav-height: 60px;
    --page-header-height: 56px;
    --status-bar-height: 24px;
    
    /* Clean background colors */
    --bg-primary: #ffffff;
    --bg-secondary: #f8f9fa;
    
    /* Blue gradient fallbacks for WebView */
    --primary-gradient: linear-gradient(135deg, #00A7E1 0%, #0086b3 100%);
    --primary-fallback: #00A7E1;
    
    /* Device detection via CSS (for enhanced safe area handling) */
    --ios-safe-area: env(safe-area-inset-top, 0px);
    --android-safe-area: 0px; /* Will be enhanced by JS */
}

/* ==========================================================================
   WebView Compatibility - Static Fallbacks
   ========================================================================== */

/* WebView detection and fallbacks */
@media screen and (-webkit-min-device-pixel-ratio: 0) {
    /* Address bar WebView fallback */
    .address-bar {
        background: #00A7E1 !important;
        padding-top: 40px !important;
        padding-left: 18px !important;
        padding-right: 18px !important;
        padding-bottom: 18px !important;
        min-height: 92px !important;
        color: white !important;
    }
    
    /* Status bar WebView fallback */
    body::before {
        background: #00A7E1 !important;
        height: 40px !important;
    }
    
    /* Mobile navigation WebView fallback */
    .mobile-nav {
        height: 60px !important;
        padding-bottom: 20px !important;
        padding-left: 0px !important;
        padding-right: 0px !important;
    }
    
    /* Search container WebView fallback */
    .search-container {
        padding-top: 14px !important;
        padding-left: 18px !important;
        padding-right: 18px !important;
        min-height: 74px !important;
    }
    
    /* Page header WebView fallback */
    .page-header {
        background: #00A7E1 !important;
        color: white !important;
        padding-top: 40px !important;
        padding-left: 16px !important;
        padding-right: 16px !important;
        min-height: 96px !important;
        border-bottom: 1px solid rgba(255, 255, 255, 0.2) !important;
    }
    
    /* Body padding WebView fallback */
    body {
        padding-bottom: 80px !important;
        padding-left: 0px !important;
        padding-right: 0px !important;
    }
    
    /* Back button WebView fallback */
    .back-button {
        background-color: white !important;
        color: #00A7E1 !important;
        border: 1px solid #e6f7fd !important;
    }
    
    .back-button:hover {
        background: #00A7E1 !important;
        color: white !important;
    }
}

/* Alternative WebView detection - Android WebView specific */
@supports not (padding: max(0px)) {
    .address-bar {
        background: #00A7E1;
        padding-top: 40px;
        padding-left: 18px;
        padding-right: 18px;
        min-height: 92px;
        color: white;
    }
    
    .page-header {
        background: #00A7E1;
        color: white;
        padding-top: 40px;
        padding-left: 16px;
        padding-right: 16px;
        min-height: 96px;
        border-bottom: 1px solid rgba(255, 255, 255, 0.2);
    }
    
    body::before {
        background: #00A7E1;
        height: 40px;
    }
    
    .mobile-nav {
        height: 60px;
        padding-bottom: 20px;
    }
    
    body {
        padding-bottom: 80px;
        padding-left: 0px;
        padding-right: 0px;
    }
}

/* JavaScript-detected WebView class */
.webview .address-bar {
    background: #00A7E1 !important;
    color: white !important;
    padding-top: 40px !important;
    padding-left: 18px !important;
    padding-right: 18px !important;
    min-height: 92px !important;
}

.webview body::before {
    background: #00A7E1 !important;
    height: 40px !important;
}

.webview .mobile-nav {
    height: 60px !important;
    padding-bottom: 20px !important;
}

.webview body {
    padding-bottom: 80px !important;
    padding-left: 0px !important;
    padding-right: 0px !important;
}

.webview .address-label,
.webview .address-text {
    color: white !important;
}

.webview .location-icon {
    background: rgba(255, 255, 255, 0.2) !important;
    border-color: rgba(255, 255, 255, 0.3) !important;
}

.webview .location-icon i,
.webview .address-arrow i {
    color: white !important;
}

.webview .address-arrow {
    background: rgba(255, 255, 255, 0.2) !important;
    border-color: rgba(255, 255, 255, 0.3) !important;
}

.webview .page-header {
    background: #00A7E1 !important;
    color: white !important;
    padding-top: 40px !important;
    min-height: 96px !important;
    border-bottom: 1px solid rgba(255, 255, 255, 0.2) !important;
}

.webview .page-title {
    color: white !important;
}

.webview .back-button {
    background-color: rgba(255, 255, 255, 0.2) !important;
    color: white !important;
    border: 1px solid rgba(255, 255, 255, 0.3) !important;
}

.webview .back-button:hover {
    background-color: rgba(255, 255, 255, 0.3) !important;
    color: white !important;
}

/* ==========================================================================
   Base Layout - Mobile First with Status Bar Blue Background
   ========================================================================== */

html {
    height: 100%;
    background-color: var(--bg-primary);
}

body {
    margin: 0;
    padding: 0;
    min-height: 100vh;
    background-color: var(--bg-primary);
    position: relative;
    
    /* Mobile navigation spacing - Fixed height for consistency */
    padding-bottom: calc(60px + max(env(safe-area-inset-bottom, 0px), 20px));
    
    /* Safe area support for sides (landscape) */
    padding-left: var(--safe-left);
    padding-right: var(--safe-right);
    
    /* NO top padding - handled by individual components */
    padding-top: 0;
}

/* Status Bar Blue Background - Fixed solid blue for system UI (notch) */
body::before {
    content: '';
    position: fixed;
    top: 0;
    left: 0;
    right: 0;
    height: var(--safe-top);
    background: #00A7E1;
    z-index: 9999;
    pointer-events: none;
}

/* Enhanced status bar background for devices with larger safe areas */
@supports (padding: max(0px)) {
    body::before {
        height: max(env(safe-area-inset-top, 0px), 0px);
        background: #00A7E1;
    }
}

/* Remove padding and status bar background on desktop */
@media (min-width: 992px) {
    body {
        padding-bottom: 0;
        padding-left: 0;
        padding-right: 0;
    }
    
    body::before {
        display: none;
    }
}

/* ==========================================================================
   Mobile Navigation - Fixed & Clean (Consolidated from footer.css)
   ========================================================================== */

.mobile-nav {
    position: fixed;
    bottom: 0;
    left: 0;
    right: 0;
    height: calc(60px + max(env(safe-area-inset-bottom, 0px), 20px));
    
    /* Balanced padding for proper centering */
    padding-top: 8px;
    padding-bottom: max(env(safe-area-inset-bottom, 0px), 20px);
    padding-left: env(safe-area-inset-left, 0px);
    padding-right: env(safe-area-inset-right, 0px);
    
    /* Clean styling */
    background-color: white;
    border-top: 1px solid #e9ecef;
    box-shadow: 0 -2px 10px rgba(0,0,0,0.07);
    
    /* Layout - Improved centering */
    display: flex;
    align-items: center;
    justify-content: space-evenly;
    z-index: 1000;
    
    /* Smooth transitions */
    will-change: transform;
    transition: transform 0.3s ease;
}

/* Mobile navigation items - Optimized for better centering */
.mobile-nav-item {
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    text-decoration: none;
    color: #aaa;
    font-size: 0.75rem;
    padding: 4px 8px;
    flex: 1;
    min-width: 0;
    text-align: center;
    position: relative;
    transition: color 0.2s ease;
    height: 52px;
}

.mobile-nav-item i {
    font-size: 1.25rem;
    margin-bottom: 3px;
    transition: transform 0.2s ease;
}

.mobile-nav-item.active {
    color: #00A7E1;
    font-weight: 500;
}

.mobile-nav-item:active i {
    transform: scale(0.9);
}

.mobile-nav-item span {
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
    width: 100%;
    line-height: 1.2;
}

/* Responsive adjustments for smaller screens */
@media (max-width: 390px) {
    .mobile-nav-item {
        font-size: 0.7rem;
        padding: 5px 1px;
    }
    
    .mobile-nav-item i {
        font-size: 1.1rem;
        margin-bottom: 2px;
    }
    
    .mobile-nav-item span {
        font-size: 0.7rem;
        line-height: 1.1;
    }
}

@media (max-width: 360px) {
    .mobile-nav-item {
        font-size: 0.65rem;
        padding: 4px 1px;
    }
    
    .mobile-nav-item i {
        font-size: 1rem;
        margin-bottom: 1px;
    }
    
    .mobile-nav-item span {
        font-size: 0.65rem;
        line-height: 1.1;
    }
}

/* Very small screens - hide text labels if needed */
@media (max-width: 320px) {
    .mobile-nav-item span {
        font-size: 0.6rem;
        line-height: 1;
    }
    
    .mobile-nav-item {
        padding: 4px 0px;
    }
}

/* Better spacing for larger screens */
@media (min-width: 430px) {
    .mobile-nav-item {
        padding: 10px 6px;
    }
    
    .mobile-nav-item i {
        font-size: 1.3rem;
        margin-bottom: 4px;
    }
}

.cart-badge {
    position: absolute;
    top: -3px;
    right: 50%;
    margin-right: -18px;
    background-color: #ff3b30;
    color: white;
    font-size: 0.7rem;
    min-width: 18px;
    height: 18px;
    border-radius: 9px;
    display: flex;
    align-items: center;
    justify-content: center;
    padding: 0 5px;
    font-weight: 600;
    box-shadow: 0 2px 4px rgba(0,0,0,0.1);
}

/* WebView-specific footer fixes - Consistent with normal styling */
body.webview {
    padding-bottom: 80px !important;
}

body.webview .mobile-nav {
    height: 80px !important;
    padding-top: 8px !important;
    padding-bottom: 20px !important;
    padding-left: 0px !important;
    padding-right: 0px !important;
    background-color: white !important;
    border-top: 1px solid #e9ecef !important;
    box-shadow: 0 -2px 10px rgba(0,0,0,0.07) !important;
    display: flex !important;
    align-items: center !important;
    justify-content: space-evenly !important;
}

body.webview .mobile-nav-item {
    flex: 1 !important;
    padding: 4px 8px !important;
    color: #aaa !important;
    font-size: 0.75rem !important;
    min-width: 0 !important;
    height: 52px !important;
    display: flex !important;
    flex-direction: column !important;
    align-items: center !important;
    justify-content: center !important;
}

body.webview .mobile-nav-item.active {
    color: #00A7E1 !important;
    font-weight: 500 !important;
}

body.webview .mobile-nav-item i {
    font-size: 1.25rem !important;
    margin-bottom: 3px !important;
}

body.webview .mobile-nav-item span {
    font-size: 0.75rem !important;
    white-space: nowrap !important;
    overflow: hidden !important;
    text-overflow: ellipsis !important;
    width: 100% !important;
}

body.webview .cart-badge {
    background-color: #ff3b30 !important;
    color: white !important;
    font-size: 0.7rem !important;
    min-width: 18px !important;
    height: 18px !important;
}

/* Comprehensive WebView fallback using CSS media queries */
@media screen and (-webkit-min-device-pixel-ratio: 0) {
    .mobile-nav {
        height: 80px !important;
        padding-top: 8px !important;
        padding-bottom: 20px !important;
        padding-left: 0px !important;
        padding-right: 0px !important;
        background-color: white !important;
        border-top: 1px solid #e9ecef !important;
        box-shadow: 0 -2px 10px rgba(0,0,0,0.07) !important;
        display: flex !important;
        align-items: center !important;
        justify-content: space-evenly !important;
    }
    
    .mobile-nav-item {
        flex: 1 !important;
        padding: 4px 8px !important;
        font-size: 0.75rem !important;
        color: #aaa !important;
        display: flex !important;
        flex-direction: column !important;
        align-items: center !important;
        justify-content: center !important;
        text-align: center !important;
        min-width: 0 !important;
        height: 52px !important;
    }
    
    .mobile-nav-item i {
        font-size: 1.2rem !important;
        margin-bottom: 2px !important;
    }
    
    .mobile-nav-item span {
        font-size: 0.7rem !important;
        white-space: nowrap !important;
        overflow: hidden !important;
        text-overflow: ellipsis !important;
        width: 100% !important;
        line-height: 1.1 !important;
    }
    
    .mobile-nav-item.active {
        color: #00A7E1 !important;
        font-weight: 500 !important;
    }
    
    .cart-badge {
        position: absolute !important;
        top: -3px !important;
        right: 50% !important;
        margin-right: -18px !important;
        background-color: #ff3b30 !important;
        color: white !important;
        font-size: 0.7rem !important;
        min-width: 18px !important;
        height: 18px !important;
        border-radius: 9px !important;
        display: flex !important;
        align-items: center !important;
        justify-content: center !important;
    }
}

/* Additional WebView detection - Older Android WebView */
@supports not (padding: max(0px)) {
    .mobile-nav {
        height: 60px !important;
        padding-bottom: 20px !important;
        padding-left: 0px !important;
        padding-right: 0px !important;
        background-color: white !important;
        border-top: 1px solid #e9ecef !important;
        display: flex !important;
        align-items: center !important;
        justify-content: space-around !important;
    }
    
    .mobile-nav-item {
        width: 20% !important;
        padding: 5px 1px !important;
        font-size: 0.75rem !important;
        color: #aaa !important;
        display: flex !important;
        flex-direction: column !important;
        align-items: center !important;
        text-align: center !important;
    }
    
    .mobile-nav-item i {
        font-size: 1.2rem !important;
        margin-bottom: 2px !important;
    }
    
    .mobile-nav-item span {
        font-size: 0.7rem !important;
        line-height: 1.1 !important;
    }
    
    .mobile-nav-spacer {
        height: 80px !important;
    }
}

/* Mobile navigation spacing - Consistent height */
.mobile-nav-spacer {
    height: calc(60px + max(env(safe-area-inset-bottom, 0px), 20px));
    min-height: 80px;
    flex-shrink: 0;
}

body.webview .mobile-nav-spacer {
    height: 80px;
    min-height: 80px;
}

/* Hide on desktop */
@media (min-width: 992px) {
    .mobile-nav {
        display: none;
    }
    
    .mobile-nav-spacer {
        display: none;
    }
}

/* ==========================================================================
   Header Components - Enhanced Safe Area Integration
   ========================================================================== */

/* Address bar - homepage only with full blue background */
.address-bar {
    padding: var(--safe-top-header) calc(18px + var(--safe-left)) 18px calc(18px + var(--safe-right));
    background: var(--primary-gradient);
    position: relative;
    min-height: calc(52px + var(--safe-top));
    color: white;
}

/* Remove the separate background extension - now unified */

/* Search container - homepage */
.search-container {
    padding: var(--safe-top-minimal) calc(18px + var(--safe-left)) 20px calc(18px + var(--safe-right));
    background-color: var(--bg-primary);
    box-shadow: 0 2px 8px rgba(0,0,0,0.05);
    min-height: calc(60px + var(--safe-top));
    position: relative;
}

/* Page header - inner pages with blue background */
.page-header {
    padding: var(--safe-top-header) calc(16px + var(--safe-left)) 16px calc(16px + var(--safe-right));
    background: var(--primary-gradient);
    border-bottom: 1px solid rgba(255, 255, 255, 0.2);
    min-height: calc(var(--page-header-height) + var(--safe-top));
    position: relative;
    z-index: 100;
    color: white;
}

/* Page header content layout */
.page-header-content {
    display: flex;
    align-items: center;
    gap: 16px;
    min-height: 40px;
}

/* Back button styling with blue theme integration */
.back-button {
    width: 40px;
    height: 40px;
    border-radius: 50%;
    display: flex;
    align-items: center;
    justify-content: center;
    border: 1px solid var(--primary-light);
    background-color: white;
    color: var(--primary);
    text-decoration: none;
    transition: all 0.3s ease;
    flex-shrink: 0;
    box-shadow: 0 2px 8px rgba(0, 167, 225, 0.1);
}

.back-button:hover,
.back-button:active {
    background: var(--primary-gradient);
    color: white;
    transform: scale(0.95);
    box-shadow: 0 4px 12px rgba(0, 167, 225, 0.3);
    border-color: var(--primary);
}

/* Page title styling with white text */
.page-title {
    font-size: 1.25rem;
    font-weight: 600;
    margin: 0;
    color: white;
    flex: 1;
    min-width: 0;
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
    line-height: 1.3;
}

/* Enhanced page header shadow for blue background */
.page-header {
    box-shadow: 0 2px 8px rgba(0, 167, 225, 0.3);
}

/* Responsive page header adjustments */
@media (max-width: 375px) {
    .page-title {
        font-size: 1.1rem;
    }
    
    .back-button {
        width: 36px;
        height: 36px;
    }
}

@media (min-width: 430px) {
    .page-title {
        font-size: 1.3rem;
    }
    
    .page-header-content {
        gap: 20px;
    }
}

/* ==========================================================================
   Content Areas - Clean Spacing
   ========================================================================== */

.app-container,
.main-content {
    background-color: var(--bg-primary);
    min-height: calc(100vh - var(--mobile-nav-height) - var(--safe-bottom));
}

/* Desktop adjustments */
@media (min-width: 992px) {
    .app-container,
    .main-content {
        min-height: 100vh;
    }
}

/* ==========================================================================
   Form & Modal Safe Areas
   ========================================================================== */

.modal-content {
    margin: var(--safe-top) var(--safe-left) var(--safe-bottom) var(--safe-right);
}

.form-container {
    padding: 20px calc(20px + var(--safe-left)) 20px calc(20px + var(--safe-right));
}

/* ==========================================================================
   Utilities - Clean Overrides
   ========================================================================== */

/* Ensure clean backgrounds */
*::before,
*::after {
    background-color: inherit;
}

/* Remove problematic positioning */
.no-sticky {
    position: static !important;
}

/* Safe area debug (development only) */
.safe-area-debug {
    outline: 2px solid red;
    outline-offset: -2px;
}

/* ==========================================================================
   Enhanced Device & Orientation Support
   ========================================================================== */

/* Landscape orientation adjustments with blue status bar */
@media (orientation: landscape) and (max-height: 480px) {
    :root {
        --safe-top-header: max(env(safe-area-inset-top, 0px), 12px);
        --safe-top-minimal: max(env(safe-area-inset-top, 0px), 4px);
        --mobile-nav-height: 50px;
        --page-header-height: 48px;
    }
    
    .address-bar {
        min-height: calc(44px + var(--safe-top));
    }
    
    .search-container {
        min-height: calc(48px + var(--safe-top));
    }
    
    .page-header {
        min-height: calc(48px + var(--safe-top));
    }
    
    .mobile-nav {
        height: var(--mobile-nav-height);
    }
    
    /* Ensure blue background extends in landscape mode */
    body::before {
        background: #00A7E1;
    }
    
    .address-bar,
    .page-header {
        background: var(--primary-gradient);
        color: white;
    }
}

/* iPhone X/11/12/13/14 series specific adjustments */
@supports (padding: max(0px)) {
    .address-bar {
        padding-top: max(env(safe-area-inset-top, 0px), 20px);
    }
    
    .search-container {
        padding-top: max(env(safe-area-inset-top, 0px), 14px);
    }
    
    .page-header {
        padding-top: max(env(safe-area-inset-top, 0px), 18px);
    }
}

/* Android devices with display cutouts */
@media (display-mode: standalone) {
    :root {
        --android-safe-area: env(safe-area-inset-top, 0px);
    }
    
    /* Enhanced padding for Android notch devices */
    .address-bar,
    .search-container,
    .page-header {
        padding-left: max(env(safe-area-inset-left, 0px), 16px);
        padding-right: max(env(safe-area-inset-right, 0px), 16px);
    }
    
    /* Ensure blue background covers Android cutouts properly */
    body::before {
        left: env(safe-area-inset-left, 0px);
        right: env(safe-area-inset-right, 0px);
        background: #00A7E1;
    }
    
    .address-bar,
    .page-header {
        background: var(--primary-gradient);
        color: white;
    }
}

/* Specific adjustments for devices with irregular notches */
@media (max-width: 430px) and (min-height: 800px) {
    /* iPhone 12 mini, 13 mini specific */
    body::before {
        background: #00A7E1;
    }
    
    .address-bar,
    .page-header {
        background: var(--primary-gradient);
        color: white;
    }
}

@media (min-width: 430px) and (min-height: 900px) {
    /* iPhone 14 Pro Max, 15 Pro Max specific */
    body::before {
        background: #00A7E1;
        height: max(env(safe-area-inset-top, 0px), 47px);
    }
    
    .address-bar,
    .page-header {
        background: var(--primary-gradient);
        color: white;
    }
}

/* ==========================================================================
   Legacy Support & Cleanup
   ========================================================================== */

/* Override any remaining legacy styles */
.legacy-override {
    padding-top: 0 !important;
    margin-top: 0 !important;
    background: var(--bg-primary) !important;
}

/* Safe area debug mode (development only) */
.safe-area-debug .address-bar,
.safe-area-debug .search-container,
.safe-area-debug .page-header {
    outline: 2px solid rgba(255, 0, 0, 0.5);
    outline-offset: -2px;
}

.safe-area-debug .mobile-nav {
    outline: 2px solid rgba(0, 255, 0, 0.5);
    outline-offset: -2px;
}