/**
 * Mobile-First Responsive CSS Framework
 * Optimized for all mobile devices with touch-friendly interfaces
 * 
 * Breakpoints:
 * - Base (Mobile): 0-479px
 * - Large Mobile: 480-767px  
 * - Tablet: 768-1023px
 * - Desktop: 1024px+
 * - Large Desktop: 1440px+
 */

/* ====================================
   BASE MOBILE STYLES (Mobile First)
   ==================================== */

/* Reset and Base Typography */
* {
    -webkit-tap-highlight-color: transparent;
    -webkit-touch-callout: none;
    touch-action: manipulation;
}

html {
    font-size: 16px; /* Prevent zoom on iOS */
    -webkit-text-size-adjust: 100%;
    -ms-text-size-adjust: 100%;
    /* Dynamic viewport height support for iOS */
    height: 100%;
}

body {
    /* Modern viewport height with fallbacks */
    min-height: 100vh; /* fallback for older browsers */
    min-height: 100dvh; /* dynamic viewport height - preferred for 2024+ */
    min-height: -webkit-fill-available; /* WebKit legacy fallback */
    overflow-x: hidden;
    -webkit-font-smoothing: antialiased;
    -moz-osx-font-smoothing: grayscale;
}

/* Support for dynamic viewport units */
@supports (height: 100dvh) {
    body {
        min-height: 100dvh;
    }
}

/* Responsive Typography Scale */
.text-responsive-xs {
    font-size: clamp(0.75rem, 2.5vw, 0.875rem);
}

.text-responsive-sm {
    font-size: clamp(0.875rem, 3vw, 1rem);
}

.text-responsive-base {
    font-size: clamp(1rem, 3.5vw, 1.125rem);
}

.text-responsive-lg {
    font-size: clamp(1.125rem, 4vw, 1.25rem);
}

.text-responsive-xl {
    font-size: clamp(1.25rem, 4.5vw, 1.5rem);
}

.text-responsive-2xl {
    font-size: clamp(1.5rem, 5vw, 2rem);
}

.text-responsive-3xl {
    font-size: clamp(1.875rem, 6vw, 2.5rem);
}

/* Touch-Friendly Target Sizes */
.touch-target {
    min-height: 44px;
    min-width: 44px;
    display: flex;
    align-items: center;
    justify-content: center;
}

button, 
a.button,
.btn,
input[type="submit"],
input[type="button"] {
    min-height: 48px;
    padding: 12px 20px;
    font-size: 16px; /* Prevent zoom on iOS */
}

input[type="text"],
input[type="email"],
input[type="password"],
input[type="number"],
input[type="tel"],
input[type="date"],
input[type="time"],
textarea,
select {
    min-height: 48px;
    padding: 12px 16px;
    font-size: 16px; /* Prevent zoom on iOS */
    width: 100%;
    border-radius: 8px;
}

/* Mobile Container */
.mobile-container {
    width: 100%;
    padding-left: 16px;
    padding-right: 16px;
    margin-left: auto;
    margin-right: auto;
}

/* Safe Area Insets for Notched Devices */
.safe-area-inset {
    padding-left: env(safe-area-inset-left);
    padding-right: env(safe-area-inset-right);
    padding-bottom: env(safe-area-inset-bottom);
}

.safe-area-inset-top {
    padding-top: env(safe-area-inset-top);
}

/* Mobile Navigation */
.mobile-nav {
    position: fixed;
    bottom: 0;
    left: 0;
    right: 0;
    background: rgba(17, 24, 39, 0.95);
    backdrop-filter: blur(20px);
    -webkit-backdrop-filter: blur(20px);
    border-top: 1px solid rgba(255, 255, 255, 0.1);
    z-index: 1000;
    display: flex;
    justify-content: space-around;
    padding: 8px 0;
    padding-bottom: calc(8px + env(safe-area-inset-bottom));
}

.mobile-nav-item {
    flex: 1;
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    min-height: 56px;
    color: #9ca3af;
    text-decoration: none;
    transition: all 0.2s ease;
}

.mobile-nav-item.active {
    color: #818cf8;
}

.mobile-nav-item i {
    font-size: 24px;
    margin-bottom: 4px;
}

.mobile-nav-item span {
    font-size: 11px;
    font-weight: 500;
}

/* Mobile Header */
.mobile-header {
    position: sticky;
    top: 0;
    left: 0;
    right: 0;
    background: rgba(17, 24, 39, 0.95);
    backdrop-filter: blur(20px);
    -webkit-backdrop-filter: blur(20px);
    border-bottom: 1px solid rgba(255, 255, 255, 0.1);
    z-index: 999;
    padding: 12px 16px;
    padding-top: calc(12px + env(safe-area-inset-top));
}

/* Hamburger Menu */
.hamburger-menu {
    width: 32px;
    height: 32px;
    display: flex;
    flex-direction: column;
    justify-content: center;
    gap: 4px;
    cursor: pointer;
    padding: 4px;
}

.hamburger-line {
    width: 24px;
    height: 2px;
    background: #f9fafb;
    border-radius: 2px;
    transition: all 0.3s ease;
}

.hamburger-menu.active .hamburger-line:nth-child(1) {
    transform: rotate(45deg) translateY(6px);
}

.hamburger-menu.active .hamburger-line:nth-child(2) {
    opacity: 0;
}

.hamburger-menu.active .hamburger-line:nth-child(3) {
    transform: rotate(-45deg) translateY(-6px);
}

/* Adjust main content for mobile header */
@media (max-width: 767px) {
    #main-app {
        margin-top: calc(60px + env(safe-area-inset-top)) !important;
    }
    
    body.authenticated #main-app {
        margin-top: calc(60px + env(safe-area-inset-top)) !important;
    }
}

/* Mobile Sidebar */
.mobile-sidebar {
    position: fixed;
    top: 0;
    left: -100%;
    width: 85%;
    max-width: 320px;
    height: 100vh; /* fallback */
    height: 100dvh; /* dynamic viewport height */
    background: rgba(17, 24, 39, 0.98);
    backdrop-filter: blur(20px);
    -webkit-backdrop-filter: blur(20px);
    z-index: 1001;
    transition: left 0.3s ease;
    overflow-y: auto;
    padding: 20px;
    padding-top: calc(20px + env(safe-area-inset-top));
}

.mobile-sidebar.active {
    left: 0;
    box-shadow: 2px 0 20px rgba(0, 0, 0, 0.3);
}

.mobile-overlay {
    position: fixed;
    top: 0;
    left: 0;
    right: 0;
    bottom: 0;
    background: rgba(0, 0, 0, 0.5);
    z-index: 1000;
    opacity: 0;
    visibility: hidden;
    transition: all 0.3s ease;
}

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

/* Cards and Content Blocks */
.mobile-card {
    background: rgba(255, 255, 255, 0.05);
    backdrop-filter: blur(20px);
    -webkit-backdrop-filter: blur(20px);
    border: 1px solid rgba(255, 255, 255, 0.1);
    border-radius: 16px;
    padding: 16px;
    margin-bottom: 16px;
}

/* Mobile Grid System */
.mobile-grid {
    display: grid;
    gap: 12px;
    grid-template-columns: repeat(auto-fit, minmax(140px, 1fr));
}

/* Swipeable Containers */
.swipeable-container {
    overflow-x: auto;
    scroll-snap-type: x mandatory;
    -webkit-overflow-scrolling: touch;
    scrollbar-width: none;
    -ms-overflow-style: none;
}

.swipeable-container::-webkit-scrollbar {
    display: none;
}

.swipeable-item {
    scroll-snap-align: start;
    flex-shrink: 0;
}

/* Floating Action Button */
.fab {
    position: fixed;
    bottom: 80px;
    right: 16px;
    width: 56px;
    height: 56px;
    border-radius: 50%;
    background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
    color: white;
    display: flex;
    align-items: center;
    justify-content: center;
    box-shadow: 0 4px 12px rgba(102, 126, 234, 0.4);
    z-index: 999;
    cursor: pointer;
    transition: all 0.3s ease;
}

.fab:active {
    transform: scale(0.95);
}

/* Mobile Forms */
.mobile-form-group {
    margin-bottom: 20px;
}

.mobile-form-label {
    display: block;
    margin-bottom: 8px;
    font-size: 14px;
    font-weight: 500;
    color: #9ca3af;
}

/* Mobile Modals */
.mobile-modal {
    position: fixed;
    bottom: 0;
    left: 0;
    right: 0;
    background: rgba(17, 24, 39, 0.98);
    backdrop-filter: blur(20px);
    -webkit-backdrop-filter: blur(20px);
    border-top-left-radius: 24px;
    border-top-right-radius: 24px;
    padding: 20px;
    padding-bottom: calc(20px + env(safe-area-inset-bottom));
    transform: translateY(100%);
    transition: transform 0.3s ease;
    z-index: 1002;
    max-height: 90vh;
    overflow-y: auto;
}

.mobile-modal.active {
    transform: translateY(0);
}

.mobile-modal-handle {
    width: 40px;
    height: 4px;
    background: rgba(255, 255, 255, 0.3);
    border-radius: 2px;
    margin: 0 auto 20px;
}

/* Loading States */
.skeleton-loader {
    background: linear-gradient(90deg, 
        rgba(255, 255, 255, 0.05) 25%, 
        rgba(255, 255, 255, 0.1) 50%, 
        rgba(255, 255, 255, 0.05) 75%);
    background-size: 200% 100%;
    animation: skeleton-shimmer 1.5s infinite;
    border-radius: 8px;
}

@keyframes skeleton-shimmer {
    0% { background-position: 200% 0; }
    100% { background-position: -200% 0; }
}

/* Pull to Refresh */
.pull-to-refresh {
    position: absolute;
    top: -60px;
    left: 50%;
    transform: translateX(-50%);
    width: 40px;
    height: 40px;
    display: flex;
    align-items: center;
    justify-content: center;
    transition: transform 0.3s ease;
}

.pull-to-refresh.refreshing {
    transform: translateX(-50%) translateY(80px);
}

/* ====================================
   LARGE MOBILE (480px+)
   ==================================== */
@media (min-width: 480px) {
    .mobile-container {
        padding-left: 20px;
        padding-right: 20px;
    }
    
    .mobile-grid {
        grid-template-columns: repeat(auto-fit, minmax(200px, 1fr));
        gap: 16px;
    }
    
    .mobile-card {
        padding: 20px;
    }
}

/* ====================================
   TABLET (768px+)
   ==================================== */
@media (min-width: 768px) {
    .mobile-container {
        padding-left: 24px;
        padding-right: 24px;
        max-width: 720px;
    }
    
    /* Hide mobile nav on tablets */
    .mobile-nav {
        display: none;
    }
    
    /* Show desktop nav */
    .desktop-nav {
        display: flex;
    }
    
    /* Adjust FAB position */
    .fab {
        bottom: 24px;
        right: 24px;
    }
    
    /* Two column layout for tablets */
    .tablet-grid-2 {
        display: grid;
        grid-template-columns: repeat(2, 1fr);
        gap: 20px;
    }
    
    /* Modal becomes centered */
    .mobile-modal {
        bottom: auto;
        top: 50%;
        left: 50%;
        transform: translate(-50%, -50%);
        width: 90%;
        max-width: 500px;
        border-radius: 24px;
        max-height: 80vh;
    }
    
    .mobile-modal.active {
        transform: translate(-50%, -50%);
    }
}

/* ====================================
   DESKTOP (1024px+)
   ==================================== */
@media (min-width: 1024px) {
    .mobile-container {
        padding-left: 32px;
        padding-right: 32px;
        max-width: 960px;
    }
    
    /* Three column layout for desktop */
    .desktop-grid-3 {
        display: grid;
        grid-template-columns: repeat(3, 1fr);
        gap: 24px;
    }
    
    /* Larger touch targets not needed */
    button,
    a.button,
    .btn {
        min-height: 40px;
        padding: 10px 20px;
    }
    
    input[type="text"],
    input[type="email"],
    input[type="password"],
    textarea,
    select {
        min-height: 40px;
        padding: 10px 16px;
    }
}

/* ====================================
   LARGE DESKTOP (1440px+)
   ==================================== */
@media (min-width: 1440px) {
    .mobile-container {
        max-width: 1280px;
    }
    
    /* Four column layout for large screens */
    .xl-grid-4 {
        display: grid;
        grid-template-columns: repeat(4, 1fr);
        gap: 32px;
    }
}

/* ====================================
   UTILITY CLASSES
   ==================================== */

/* Hide/Show Utilities */
.mobile-only {
    display: block;
}

.tablet-only {
    display: none;
}

.desktop-only {
    display: none;
}

@media (min-width: 768px) {
    .mobile-only {
        display: none;
    }
    
    .tablet-only {
        display: block;
    }
}

@media (min-width: 1024px) {
    .tablet-only {
        display: none;
    }
    
    .desktop-only {
        display: block;
    }
}

/* Spacing Utilities */
.p-mobile-sm { padding: 12px; }
.p-mobile-md { padding: 16px; }
.p-mobile-lg { padding: 20px; }

.m-mobile-sm { margin: 12px; }
.m-mobile-md { margin: 16px; }
.m-mobile-lg { margin: 20px; }

/* Prevent Horizontal Scroll */
.no-scroll-x {
    overflow-x: hidden;
}

.scroll-x-auto {
    overflow-x: auto;
    -webkit-overflow-scrolling: touch;
}

/* Touch Gestures */
.touch-pan-y {
    touch-action: pan-y;
}

.touch-pan-x {
    touch-action: pan-x;
}

.touch-pinch-zoom {
    touch-action: pinch-zoom;
}

/* Performance Optimizations */
.will-change-transform {
    will-change: transform;
}

.hardware-accelerated {
    transform: translateZ(0);
    -webkit-transform: translateZ(0);
}

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

/* Focus Visible for Keyboard Navigation */
*:focus-visible {
    outline: 2px solid #818cf8;
    outline-offset: 2px;
}

/* Remove focus for touch/mouse */
*:focus:not(:focus-visible) {
    outline: none;
}

/* ====================================
   iPhone 15 Pro Max Specific Optimizations
   ==================================== */

/* iPhone 15 Pro Max (430px × 932px CSS pixels) */
@media only screen and (width: 430px) and (height: 932px) and (-webkit-device-pixel-ratio: 3) {
    /* Enhanced touch targets for large display */
    .touch-target {
        min-height: 48px;
        min-width: 48px;
    }
    
    /* Optimized typography for large screen */
    .text-responsive-base {
        font-size: clamp(1rem, 3.2vw, 1.2rem);
    }
    
    /* Better spacing for large screens */
    .mobile-container {
        padding-left: 20px;
        padding-right: 20px;
    }
}

/* All large iPhones (iPhone 14 Pro Max, 15 Pro Max) */
@media only screen and (min-width: 428px) and (max-width: 430px) and (-webkit-device-pixel-ratio: 3) {
    /* Ensure content doesn't feel cramped on large displays */
    .mobile-grid {
        grid-template-columns: repeat(2, 1fr);
        gap: 16px;
    }
    
    /* Better task card sizing */
    .task-item {
        min-height: 60px;
    }
}

/* Dynamic Viewport Height Support for All iOS Devices */
@supports (height: 100dvh) {
    .full-height,
    .mobile-container,
    #app {
        min-height: 100dvh;
    }
    
    .mobile-sidebar {
        height: 100dvh;
    }
}

/* JavaScript-controlled viewport height (fallback for problematic browsers) */
.full-height {
    height: calc(var(--vh, 1vh) * 100);
}

.mobile-container,
#app {
    min-height: calc(var(--vh, 1vh) * 100);
    min-height: var(--app-height, 100vh);
}

.mobile-sidebar {
    height: calc(var(--vh, 1vh) * 100);
    height: var(--app-height, 100vh);
}

/* iPhone 15 Pro Max specific class optimizations */
html.iphone-15-pro-max {
    /* Enhanced safe area handling for large screen */
    --safe-area-multiplier: 1.2;
}

html.iphone-15-pro-max .mobile-container {
    padding-left: max(20px, calc(env(safe-area-inset-left) * var(--safe-area-multiplier, 1)));
    padding-right: max(20px, calc(env(safe-area-inset-right) * var(--safe-area-multiplier, 1)));
}

html.has-safe-area {
    /* Ensure proper safe area support */
    padding-top: env(safe-area-inset-top);
    padding-bottom: env(safe-area-inset-bottom);
}

/* Enhanced Safe Area Support */
@supports (padding: max(0px)) {
    .safe-area-enhanced {
        padding-left: max(16px, env(safe-area-inset-left));
        padding-right: max(16px, env(safe-area-inset-right));
        padding-top: max(20px, env(safe-area-inset-top));
        padding-bottom: max(20px, env(safe-area-inset-bottom));
    }
}

/* Fix for iOS Safari Address Bar Issues */
@media screen and (max-width: 430px) {
    html {
        height: -webkit-fill-available;
    }
    
    body {
        min-height: -webkit-fill-available;
    }
    
    /* Prevent zoom on inputs (iOS Safari fix) */
    input[type="text"],
    input[type="email"],
    input[type="password"],
    textarea,
    select {
        font-size: max(16px, 1rem); /* Prevent zoom */
        transform: translateZ(0); /* Force hardware acceleration */
    }
}