/* IfOnly Mobile App Experience - Full Optimization */

/* ========================================
   MOBILE-FIRST FOUNDATION
   ======================================== */

/* Viewport and App-like Behavior */
:root {
    --mobile-safe-area-top: env(safe-area-inset-top, 0px);
    --mobile-safe-area-bottom: env(safe-area-inset-bottom, 0px);
    --mobile-safe-area-left: env(safe-area-inset-left, 0px);
    --mobile-safe-area-right: env(safe-area-inset-right, 0px);
    --mobile-tap-size: 48px;
    --mobile-font-base: 16px;
    --mobile-transition: all 0.3s cubic-bezier(0.4, 0, 0.2, 1);
}

/* Mobile Foundation */
@media (max-width: 768px) {
    /* Prevent horizontal scroll only, allow vertical scroll */
    html, body {
        overflow-x: hidden !important;
        overflow-y: auto !important;
        overscroll-behavior: none;
        -webkit-overflow-scrolling: touch;
        touch-action: pan-y;
        position: relative;
        width: 100%;
        max-width: 100vw;
    }
    
    /* App-like viewport */
    body {
        font-size: var(--mobile-font-base);
        -webkit-text-size-adjust: 100%;
        -ms-text-size-adjust: 100%;
        padding-top: var(--mobile-safe-area-top);
        padding-bottom: var(--mobile-safe-area-bottom);
        min-height: 100vh;
        min-height: -webkit-fill-available;
    }
    
    /* Remove margin/padding that causes scroll */
    .container, .container-fluid {
        padding-left: 16px !important;
        padding-right: 16px !important;
        max-width: 100% !important;
        width: 100% !important;
        box-sizing: border-box !important;
    }
    
    /* Fix any overflowing elements */
    * {
        max-width: 100vw !important;
    }
    
    /* Fix text overflow issues */
    h1, h2, h3, h4, h5, h6, p, span, div {
        word-wrap: break-word !important;
        overflow-wrap: break-word !important;
    }
}

/* ========================================
   MOBILE BUILDER FLOW - FULL SCREEN
   ======================================== */

@media (max-width: 768px) {
    /* Hide header/footer during builder */
    body.builder-active .navbar,
    body.builder-active footer,
    body.builder-active .footer {
        display: none !important;
    }
    
    /* Full-screen builder container */
    .builder-active .create-story-container,
    .builder-active .container.py-5 {
        position: fixed !important;
        top: 0 !important;
        left: 0 !important;
        right: 0 !important;
        bottom: 0 !important;
        width: 100vw !important;
        height: 100vh !important;
        height: 100dvh !important;
        max-width: 100% !important;
        padding: 0 !important;
        margin: 0 !important;
        overflow-y: auto !important;
        background: var(--bg-black) !important;
        z-index: 9999;
    }
    
    /* Each step takes full viewport - only when builder is active */
    .builder-active .event-step,
    .builder-active #event-steps > div,
    .builder-active .step-container {
        min-height: 100vh !important;
        min-height: 100dvh !important;
        display: flex !important;
        flex-direction: column !important;
        justify-content: center !important;
        padding: 20px 16px !important;
        position: relative;
    }
    
    /* Step transitions - slide effect */
    /* Removed slide animations - direct display only */
    .step-container {
        /* No animation */
    }
    
    .step-container.sliding-out {
        /* No animation */
    }
    
    /* Animations removed for direct display */
    /* @keyframes slideInFromRight and slideOutToLeft removed */
}

/* ========================================
   MOBILE TAP TARGETS - 44px+ HEIGHT
   ======================================== */

@media (max-width: 768px) {
    /* Mobile buttons - better touch targets */
    .btn {
        min-height: 44px !important;
        padding: 10px 16px !important;
        touch-action: manipulation;
        -webkit-tap-highlight-color: transparent;
    }
    
    /* Primary CTA buttons - extra large */
    .btn-primary.btn-lg,
    .create-story-btn,
    .submit-btn {
        min-height: 56px !important;
        font-size: 18px !important;
        padding: 16px 24px !important;
        width: 100% !important;
    }
    
    /* Form inputs - better mobile experience */
    input[type="text"],
    input[type="email"],
    input[type="number"],
    input[type="date"],
    input[type="tel"],
    textarea,
    select {
        font-size: 16px !important; /* Prevents zoom on iOS */
    }
    
    /* Sliders - larger touch area */
    input[type="range"] {
        height: 44px !important;
        -webkit-appearance: none;
        background: transparent;
    }
    
    input[type="range"]::-webkit-slider-thumb {
        -webkit-appearance: none;
        width: 32px !important;
        height: 32px !important;
        border-radius: 50%;
        background: var(--primary-color);
        cursor: pointer;
        margin-top: -14px;
        box-shadow: 0 2px 8px rgba(0,0,0,0.3);
    }
    
    input[type="range"]::-webkit-slider-runnable-track {
        width: 100%;
        height: 6px;
        background: var(--bg-input);
        border-radius: 3px;
    }
}

/* ========================================
   MOBILE EXPENSE GRID - OPTIMIZED
   ======================================== */

@media (max-width: 768px) {
    /* Only apply to mobile builder steps */
    .builder-active .expense-grid {
        display: grid !important;
        grid-template-columns: repeat(3, 1fr) !important;
        gap: 12px !important;
        margin: 0 auto !important;
        max-width: 100% !important;
    }
    
    /* Don't override the original expense card styles too much */
    .builder-active .expense-card {
        min-height: 80px !important;
        padding: 12px 8px !important;
    }
    
    .builder-active .expense-emoji {
        font-size: 28px !important;
    }
    
    .builder-active .expense-label {
        font-size: 11px !important;
        line-height: 1.2 !important;
    }
}

/* iPhone SE (320px) and smaller screens */
@media (max-width: 375px) {
    .builder-active .expense-grid {
        grid-template-columns: repeat(2, 1fr) !important;
        gap: 8px !important;
        padding: 0 8px !important;
    }
    
    .builder-active .expense-card {
        min-height: 85px !important;
        padding: 10px 6px !important;
    }
    
    .builder-active .expense-emoji {
        font-size: 28px !important;
    }
    
    .builder-active .expense-label {
        font-size: 11px !important;
    }
    
    /* Adjust container padding for smallest screens */
    .container, .container-fluid {
        padding-left: 12px !important;
        padding-right: 12px !important;
    }
}

/* Extra small screens (320px exactly - iPhone SE) */
@media (max-width: 320px) {
    .builder-active .expense-grid {
        gap: 6px !important;
        padding: 0 4px !important;
    }
    
    .builder-active .expense-card {
        min-height: 80px !important;
        padding: 8px 4px !important;
    }
    
    .builder-active .expense-emoji {
        font-size: 24px !important;
    }
    
    .builder-active .expense-label {
        font-size: 10px !important;
    }
    
    /* Smaller padding on tiny screens */
    .container, .container-fluid {
        padding-left: 10px !important;
        padding-right: 10px !important;
    }
}

/* ========================================
   MOBILE AMOUNT SELECTOR
   ======================================== */

@media (max-width: 768px) {
    /* Only adjust layout, not styles */
    .amount-grid {
        display: grid !important;
        grid-template-columns: repeat(3, 1fr) !important;
        gap: 10px !important;
        margin: 16px 0 !important;
        width: 100% !important;
        box-sizing: border-box !important;
    }
    
    .amount-chip {
        min-height: 48px !important;
        padding: 10px 4px !important;
        font-size: 13px !important;
    }
    
    .amount-chip.active {
        background: var(--primary-color) !important;
        border-color: var(--primary-color) !important;
        color: white !important;
        transform: scale(1.05);
    }
    
    /* Amount slider - mobile optimized */
    .amount-slider-container {
        padding: 20px 0 !important;
    }
    
    .slider-value-display {
        font-size: 32px !important;
        font-weight: 700 !important;
        color: var(--primary-color) !important;
        text-align: center !important;
        margin-bottom: 20px !important;
    }
}

/* ========================================
   MOBILE TYPOGRAPHY - READABLE
   ======================================== */

@media (max-width: 768px) {
    /* Headings */
    h1, .display-1, .display-2, .display-3 {
        font-size: 26px !important;
        line-height: 1.2 !important;
        font-weight: 700 !important;
        word-break: break-word !important;
        overflow-wrap: break-word !important;
    }
    
    h2, .display-4 {
        font-size: 22px !important;
        line-height: 1.3 !important;
        font-weight: 600 !important;
        word-break: break-word !important;
    }
    
    h3, .display-5 {
        font-size: 20px !important;
        line-height: 1.4 !important;
        font-weight: 600 !important;
        word-break: break-word !important;
    }
    
    h4, h5, h6 {
        font-size: 18px !important;
        line-height: 1.4 !important;
        font-weight: 600 !important;
    }
    
    /* Body text */
    p, .lead {
        font-size: 16px !important;
        line-height: 1.6 !important;
    }
    
    small, .small {
        font-size: 14px !important;
    }
    
    /* Labels and form text */
    .form-label, label {
        font-size: 16px !important;
        font-weight: 600 !important;
        margin-bottom: 8px !important;
        color: var(--text-white) !important;
    }
}

/* Smallest screens (320px) */
@media (max-width: 375px) {
    h1, .display-1, .display-2, .display-3 {
        font-size: 24px !important;
    }
    
    h2, .display-4 {
        font-size: 20px !important;
    }
    
    h3, .display-5 {
        font-size: 18px !important;
    }
    
    p, .lead {
        font-size: 15px !important;
    }
}

/* ========================================
   MOBILE HERO SECTION - FIX ALIGNMENT
   ======================================== */

@media (max-width: 768px) {
    /* Hero section */
    .hero-section, .jumbotron {
        padding: 40px 16px !important;
        margin: 0 !important;
        width: 100% !important;
        box-sizing: border-box !important;
    }
    
    /* Bitcoin price ticker */
    .bitcoin-price-ticker, .btc-price-display {
        padding: 12px !important;
        margin: 0 auto !important;
        max-width: calc(100vw - 32px) !important;
        font-size: 14px !important;
        overflow: hidden !important;
        text-overflow: ellipsis !important;
    }
    
    /* Cards and info boxes */
    .card {
        margin: 0 !important;
        width: 100% !important;
        border-radius: 12px !important;
    }
    
    .card-body {
        padding: 16px !important;
    }
    
    /* Example stories cards */
    .story-card, .example-story {
        margin-bottom: 12px !important;
        padding: 12px !important;
        width: 100% !important;
        box-sizing: border-box !important;
    }
    
    /* Story value displays */
    .story-value, .current-value {
        font-size: 18px !important;
        word-break: break-all !important;
    }
    
    /* Buttons in cards */
    .card .btn {
        width: 100% !important;
        margin-top: 8px !important;
    }
    
    /* Currency selector */
    .currency-selector {
        display: flex !important;
        gap: 8px !important;
        flex-wrap: wrap !important;
        justify-content: center !important;
        margin: 12px 0 !important;
    }
    
    .currency-btn {
        min-width: 60px !important;
        padding: 8px 12px !important;
        font-size: 14px !important;
    }
    
    /* Trust features section */
    .trust-features {
        padding: 20px 0 !important;
    }
    
    .trust-features .col-md-4 {
        margin-bottom: 16px !important;
    }
    
    /* Popular stories grid */
    .popular-stories {
        padding: 16px 0 !important;
    }
    
    .row.g-4 {
        --bs-gutter-x: 12px !important;
        --bs-gutter-y: 12px !important;
        margin: 0 !important;
    }
    
    /* Fix row margins */
    .row {
        margin-left: 0 !important;
        margin-right: 0 !important;
    }
    
    .col, [class*="col-"] {
        padding-left: 6px !important;
        padding-right: 6px !important;
    }
}

/* iPhone SE specific fixes */
@media (max-width: 375px) {
    .hero-section, .jumbotron {
        padding: 30px 12px !important;
    }
    
    .bitcoin-price-ticker {
        font-size: 12px !important;
        padding: 10px !important;
    }
    
    .story-value {
        font-size: 16px !important;
    }
    
    .currency-btn {
        min-width: 50px !important;
        padding: 6px 8px !important;
        font-size: 12px !important;
    }
}

/* ========================================
   MOBILE NAVIGATION - STICKY PROGRESS
   ======================================== */

@media (max-width: 768px) {
    /* Progress bar for multi-step forms */
    .mobile-progress-bar {
        position: fixed;
        top: 0;
        left: 0;
        right: 0;
        height: 56px;
        background: var(--bg-black);
        border-bottom: 1px solid var(--border-color);
        z-index: 1000;
        display: flex;
        align-items: center;
        justify-content: space-between;
        padding: 0 16px;
    }
    
    .mobile-progress-bar .back-btn {
        width: 40px;
        height: 40px;
        display: flex;
        align-items: center;
        justify-content: center;
        background: #000000;
        border: 2px solid #000000;
        border-radius: 8px;
        color: #F7931A;
        font-size: 20px;
        transition: all 0.2s ease;
    }
    
    .mobile-progress-bar .back-btn:hover,
    .mobile-progress-bar .back-btn:active {
        background: #1a1a1a;
        border-color: #F7931A;
        color: #F7931A;
        transform: scale(0.95);
    }
    
    .mobile-progress-bar .step-indicator {
        font-size: 14px;
        color: var(--text-muted);
    }
    
    .mobile-progress-bar .progress-line {
        position: absolute;
        bottom: 0;
        left: 0;
        height: 3px;
        background: var(--primary-color);
        transition: width 0.3s ease;
    }
    
    /* Adjust content for progress bar */
    .builder-active .step-container {
        padding-top: 76px !important;
    }
}

/* ========================================
   MOBILE MODALS & OVERLAYS
   ======================================== */

@media (max-width: 768px) {
    /* Full-screen modals */
    .modal-dialog {
        margin: 0 !important;
        max-width: 100% !important;
        height: 100vh !important;
        height: 100dvh !important;
    }
    
    .modal-content {
        height: 100% !important;
        border-radius: 0 !important;
        background: var(--bg-black) !important;
    }
    
    /* Loading modal - centered with lighter backdrop for better visibility */
    #loadingModal.modal {
        background: rgba(0, 0, 0, 0.4) !important;
    }
    
    #loadingModal .modal-dialog {
        margin: 0 !important;
        max-width: 320px !important;
        width: 85% !important;
        height: auto !important;
        position: fixed !important;
        top: 50% !important;
        left: 50% !important;
        transform: translate(-50%, -50%) !important;
    }
    
    #loadingModal .modal-content {
        background: #000000 !important;
        border: 2px solid #F7931A !important;
        border-radius: 1.5rem !important;
        height: auto !important;
        min-height: 320px !important;
        display: flex !important;
        align-items: center !important;
        justify-content: center !important;
        box-shadow: 0 10px 40px rgba(0, 0, 0, 0.5) !important;
    }
    
    #loadingModal .modal-body {
        padding: 2rem !important;
    }
    
    .modal-header {
        padding: 20px 16px !important;
        border-bottom: 1px solid var(--border-color) !important;
    }
    
    .modal-body {
        padding: 20px 16px !important;
        overflow-y: auto !important;
    }
    
    .modal-footer {
        padding: 16px !important;
        border-top: 1px solid var(--border-color) !important;
    }
    
    /* Bottom sheet style for some modals */
    .bottom-sheet {
        position: fixed !important;
        bottom: 0 !important;
        left: 0 !important;
        right: 0 !important;
        transform: translateY(100%);
        transition: transform 0.3s ease-out;
        border-radius: 24px 24px 0 0 !important;
        max-height: 90vh !important;
    }
    
    .bottom-sheet.show {
        transform: translateY(0);
    }
}

/* ========================================
   MOBILE STORY VIEW - OPTIMIZED
   ======================================== */

@media (max-width: 768px) {
    /* Story timeline - vertical only */
    .timeline-container {
        padding: 16px !important;
    }
    
    .timeline-event-card {
        margin-bottom: 20px !important;
        padding: 20px !important;
        border-radius: 16px !important;
    }
    
    /* Story sharing - mobile optimized */
    .share-buttons-row {
        display: flex !important;
        gap: 10px !important;
        flex-wrap: wrap !important;
        justify-content: center !important;
    }
    
    .share-btn {
        flex: 1 !important;
        min-width: calc(50% - 5px) !important;
        max-width: calc(50% - 5px) !important;
        min-height: 48px !important;
        font-size: 14px !important;
    }
    
    /* Story results - mobile cards */
    .result-card {
        padding: 20px !important;
        margin-bottom: 16px !important;
        border-radius: 16px !important;
        background: var(--bg-card) !important;
    }
    
    .btc-amount {
        font-size: 28px !important;
        font-weight: 700 !important;
        color: var(--primary-color) !important;
    }
    
    .current-value {
        font-size: 32px !important;
        font-weight: 700 !important;
        color: var(--success-color) !important;
    }
}

/* ========================================
   MOBILE LOADING STATES
   ======================================== */

@media (max-width: 768px) {
    /* Loading spinner - centered */
    .loading-container {
        position: fixed !important;
        top: 50% !important;
        left: 50% !important;
        transform: translate(-50%, -50%) !important;
        z-index: 10000 !important;
    }
    
    .spinner-border {
        width: 48px !important;
        height: 48px !important;
        border-width: 4px !important;
    }
    
    /* Skeleton loading */
    .skeleton {
        background: linear-gradient(90deg, 
            var(--bg-card) 25%, 
            var(--bg-input) 50%, 
            var(--bg-card) 75%);
        background-size: 200% 100%;
        animation: skeleton-loading 1.5s infinite;
    }
    
    @keyframes skeleton-loading {
        0% { background-position: 200% 0; }
        100% { background-position: -200% 0; }
    }
}

/* ========================================
   MOBILE LANDSCAPE MODE
   ======================================== */

@media (max-width: 768px) and (orientation: landscape) {
    /* Adjust for landscape */
    .step-container {
        min-height: auto !important;
        padding: 10px 20px !important;
    }
    
    .expense-grid {
        grid-template-columns: repeat(4, 1fr) !important;
        gap: 8px !important;
    }
    
    .expense-card {
        min-height: 60px !important;
        padding: 8px !important;
    }
    
    .expense-emoji {
        font-size: 24px !important;
    }
    
    .expense-label {
        font-size: 10px !important;
    }
    
    h1, .display-3 {
        font-size: 24px !important;
    }
    
    .btn-primary {
        min-height: 48px !important;
        font-size: 16px !important;
    }
}

/* ========================================
   MOBILE PERFORMANCE OPTIMIZATIONS
   ======================================== */

@media (max-width: 768px) {
    /* Hardware acceleration for smooth scrolling */
    * {
        -webkit-transform: translateZ(0);
        -webkit-backface-visibility: hidden;
        -webkit-perspective: 1000;
    }
    
    /* Optimize touch scrolling */
    .scrollable {
        -webkit-overflow-scrolling: touch;
        overflow-y: auto;
        overflow-x: hidden;
    }
    
    /* Reduce animations for low-end devices */
    @media (prefers-reduced-motion: reduce) {
        * {
            animation-duration: 0.01ms !important;
            animation-iteration-count: 1 !important;
            transition-duration: 0.01ms !important;
        }
    }
}

/* ========================================
   MOBILE DATE PICKER - NATIVE
   ======================================== */

@media (max-width: 768px) {
    /* Use native date pickers on mobile */
    input[type="date"],
    input[type="month"] {
        -webkit-appearance: none;
        position: relative;
        padding-right: 40px !important;
    }
    
    input[type="date"]::-webkit-calendar-picker-indicator,
    input[type="month"]::-webkit-calendar-picker-indicator {
        position: absolute;
        right: 10px;
        width: 20px;
        height: 20px;
        opacity: 0.6;
    }
}

/* ========================================
   MOBILE ACCESSIBILITY
   ======================================== */

@media (max-width: 768px) {
    /* Focus states - visible on mobile */
    button:focus,
    .btn:focus,
    input:focus,
    select:focus,
    textarea:focus,
    [role="button"]:focus {
        outline: 3px solid var(--primary-color) !important;
        outline-offset: 2px !important;
    }
    
    /* Skip link for accessibility */
    .skip-link {
        position: absolute;
        top: -40px;
        left: 0;
        background: var(--primary-color);
        color: white;
        padding: 8px;
        text-decoration: none;
        z-index: 100;
    }
    
    .skip-link:focus {
        top: 0;
    }
}

/* ========================================
   SPECIFIC DEVICE OPTIMIZATIONS
   ======================================== */

/* iPhone SE (320px) */
@media (max-width: 320px) {
    .container {
        padding-left: 12px !important;
        padding-right: 12px !important;
    }
    
    .expense-grid {
        grid-template-columns: repeat(2, 1fr) !important;
    }
    
    .amount-grid {
        grid-template-columns: repeat(2, 1fr) !important;
    }
    
    .btn-primary {
        font-size: 16px !important;
        padding: 14px 16px !important;
    }
}

/* iPhone 12/13 Pro Max (428px) */
@media (min-width: 375px) and (max-width: 428px) {
    .expense-grid {
        grid-template-columns: repeat(3, 1fr) !important;
    }
}

/* iPad Mini (768px) */
@media (min-width: 744px) and (max-width: 768px) {
    .expense-grid {
        grid-template-columns: repeat(4, 1fr) !important;
    }
    
    .amount-grid {
        grid-template-columns: repeat(4, 1fr) !important;
    }
}

/* ========================================
   MOBILE UTILITIES
   ======================================== */

@media (max-width: 768px) {
    /* Hide on mobile */
    .hide-mobile {
        display: none !important;
    }
    
    /* Show only on mobile */
    .show-mobile {
        display: block !important;
    }
    
    /* Full width on mobile */
    .full-width-mobile {
        width: 100% !important;
        max-width: 100% !important;
    }
    
    /* No margin on mobile */
    .no-margin-mobile {
        margin: 0 !important;
    }
    
    /* Center text on mobile */
    .text-center-mobile {
        text-align: center !important;
    }
    
    /* Stack on mobile */
    .stack-mobile {
        flex-direction: column !important;
    }
}