/**
 * Extended Mobile Optimization for Modern Smartphones
 * Samsung Galaxy, Google Pixel, OnePlus + iOS optimizations
 * 
 * Device Specifications:
 * - iPhone 14: 390px (6.1"), 16px baseline, A16 Bionic
 * - Samsung Galaxy S24: 412px (6.2"), AMOLED, Exynos/Snapdragon
 * - Google Pixel 8: 412px (6.2"), OLED, Tensor G3
 * - OnePlus 12: 412px (6.7"), AMOLED, Snapdragon 8 Gen 3
 * - iPhone 15 Pro: 393px (6.1"), 16px baseline, A17 Pro
 */

/* ===== SAMSUNG GALAXY & GOOGLE PIXEL SPECIFIC (412px) ===== */
@media (min-width: 410px) and (max-width: 480px) {
    :root {
        /* Samsung/Pixel fonts - slightly larger space */
        --font-size-base: 15.5px;
        --font-size-sm: 13px;
        --line-height-normal: 1.6;
        --spacing-unit: 8px;
    }
    
    /* Samsung AMOLED optimization - reduce power usage */
    body {
        background-color: #0a0a0a;
        color: #f0f0f0;
    }
    
    /* Form inputs for Pixel/Galaxy */
    input[type="text"],
    input[type="email"],
    input[type="tel"],
    input[type="number"],
    textarea,
    select {
        font-size: 16px; /* Prevent iOS zoom, also works on Android */
        line-height: 1.6;
        min-height: 44px;
        padding: 10px 12px;
        border: 1px solid #ccc;
        border-radius: 6px;
        font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, sans-serif;
        
        /* Android Chrome optimization */
        -webkit-appearance: none;
        -moz-appearance: none;
        appearance: none;
    }
    
    /* Samsung keyboard handling - better spacing */
    textarea {
        min-height: 120px;
        resize: vertical;
        max-height: 200px;
    }
    
    /* Pixel 8 - Better form button sizing */
    button[type="submit"],
    .btn-primary {
        min-height: 44px;
        padding: 12px 20px;
        font-size: 15px;
        font-weight: 600;
        border-radius: 6px;
        transition: all 0.3s ease;
        -webkit-user-select: none;
        user-select: none;
    }
    
    button[type="submit"]:active {
        transform: scale(0.98);
    }
    
    /* IHK/Trust section for Galaxy/Pixel */
    .ihk-badge,
    .trust-marker {
        font-size: 13px;
        padding: 6px 10px;
        display: inline-flex;
        align-items: center;
        gap: 4px;
    }
    
    .ihk-date {
        font-size: 12px;
        line-height: 1.4;
    }
    
    /* Navigation optimization for 412px */
    nav {
        gap: 12px;
    }
    
    /* Footer for Galaxy/Pixel - single line */
    footer {
        padding: 16px;
        font-size: 11px;
        line-height: 1.4;
    }
}

/* ===== ONEPLUS 12 SPECIFIC (412px with AMOLED) ===== */
@media (device-width: 412px) and (device-height: 915px) {
    :root {
        /* OnePlus 12 optimization */
        --color-amoled-bg: #0d0d0d;
        --color-amoled-text: #f5f5f5;
    }
    
    /* AMOLED power saving mode */
    body {
        background: var(--color-amoled-bg);
        color: var(--color-amoled-text);
    }
    
    /* Remove shadows on OnePlus AMOLED (power consumption) */
    .card,
    .form-group,
    button {
        box-shadow: none;
        border: 1px solid rgba(255, 255, 255, 0.1);
    }
    
    /* Form optimization for OnePlus */
    input,
    textarea,
    select {
        background: rgba(255, 255, 255, 0.05);
        border: 1px solid rgba(255, 255, 255, 0.15);
        color: #f0f0f0;
        font-size: 16px;
    }
    
    input::placeholder,
    textarea::placeholder {
        color: rgba(255, 255, 255, 0.4);
    }
    
    /* OnePlus gesture navigation safe area */
    body {
        padding-bottom: 20px; /* Safe area for gesture bar */
    }
}

/* ===== CROSS-DEVICE ANDROID OPTIMIZATIONS ===== */
@media (hover: none) and (pointer: coarse) {
    /* All touch devices (Android) */
    
    /* Remove hover states for touch */
    button:hover,
    a:hover,
    .btn-primary:hover {
        background-color: inherit; /* Use active state instead */
    }
    
    /* Enhance active states for touch */
    button:active,
    a:active,
    .btn-primary:active {
        opacity: 0.85;
        transform: scale(0.98);
    }
    
    /* Better touch feedback */
    input:focus,
    textarea:focus,
    select:focus {
        outline: none;
        border-color: #1E5DB8;
        box-shadow: 0 0 0 3px rgba(30, 93, 184, 0.1);
        background-color: rgba(30, 93, 184, 0.02);
    }
    
    /* Remove 300ms tap delay for better UX */
    button,
    a,
    input,
    select,
    textarea {
        touch-action: manipulation;
    }
    
    /* Better form focus for Android */
    input[type="text"]:focus,
    input[type="email"]:focus,
    input[type="tel"]:focus,
    textarea:focus {
        font-size: 16px; /* Prevent zoom */
        background: rgba(255, 255, 255, 0.98);
    }
}

/* ===== SAMSUNG DEX MODE (External Display) ===== */
@media (min-width: 1024px) and (max-width: 1366px) and (device-width: 412px) {
    /* DeX mode on external display */
    body {
        max-width: 800px;
        margin: 0 auto;
        padding: 20px;
    }
    
    .container {
        width: 100%;
    }
}

/* ===== iOS SPECIFIC OPTIMIZATIONS ===== */
@supports (-webkit-touch-callout: none) {
    /* iOS Safari specific fixes */
    
    body {
        -webkit-user-select: none;
        user-select: none;
    }
    
    input,
    textarea,
    select {
        -webkit-user-select: text;
        user-select: text;
    }
    
    /* Prevent zoom on input focus - iOS */
    input,
    select,
    textarea {
        font-size: 16px !important;
    }
    
    /* iOS button styling */
    button {
        -webkit-appearance: none;
        appearance: none;
        border-radius: 6px;
    }
    
    /* iOS text input optimization */
    input[type="text"],
    input[type="email"],
    input[type="tel"],
    textarea {
        -webkit-appearance: none;
        border-radius: 6px;
    }
    
    /* Fix iOS input background flickering */
    input:autofill {
        -webkit-box-shadow: 0 0 0 1000px white inset;
        box-shadow: 0 0 0 1000px white inset;
    }
    
    /* iOS form keyboard optimization */
    textarea {
        min-height: 120px;
    }
}

/* ===== CHROME ANDROID SPECIFIC ===== */
@supports (-webkit-appearance: none) {
    /* Remove Android Chrome input styles */
    input::-webkit-outer-spin-button,
    input::-webkit-inner-spin-button {
        -webkit-appearance: none;
        margin: 0;
    }
    
    input[type="number"] {
        -moz-appearance: textfield;
    }
    
    /* Chrome Android autofill styling */
    input:-webkit-autofill,
    input:-webkit-autofill:hover,
    input:-webkit-autofill:focus {
        -webkit-box-shadow: 0 0 0 1000px white inset !important;
        box-shadow: 0 0 0 1000px white inset !important;
        -webkit-text-fill-color: #333 !important;
    }
}

/* ===== LANDSCAPE MODE OPTIMIZATION ===== */
@media (orientation: landscape) and (max-width: 850px) {
    :root {
        --font-size-base: 14px;
        --spacing-vertical: 12px;
    }
    
    body {
        padding: 12px;
    }
    
    .form-group {
        margin-bottom: 12px;
    }
    
    input,
    textarea {
        min-height: 40px;
        padding: 8px 10px;
    }
    
    button {
        min-height: 40px;
        padding: 8px 16px;
    }
    
    header,
    nav {
        padding: 8px 12px;
    }
    
    footer {
        padding: 8px;
        font-size: 10px;
    }
}

/* ===== ACCESSIBILITY FOR SMALL DEVICES ===== */
@media (max-width: 375px) {
    /* Extra small phones (iPhone SE, Galaxy A13, etc.) */
    
    :root {
        --font-size-base: 14px;
        --spacing-unit: 6px;
        --line-height-tight: 1.4;
    }
    
    /* Critical accessibility - WCAG AA */
    button,
    a,
    input[type="submit"],
    input[type="button"],
    input[type="checkbox"],
    input[type="radio"] {
        min-width: 44px;
        min-height: 44px;
        padding: 8px 12px;
    }
    
    /* Form optimization for tiny screens */
    input,
    textarea {
        width: 100%;
        font-size: 16px;
        line-height: 1.5;
    }
    
    /* Label optimization */
    label {
        display: block;
        margin-bottom: 4px;
        font-size: 13px;
        font-weight: 600;
    }
    
    /* Message help text */
    .form-help,
    .form-error,
    .form-hint {
        font-size: 11px;
        line-height: 1.4;
        margin-top: 2px;
    }
}

/* ===== REDUCED MOTION FOR ACCESSIBILITY ===== */
@media (prefers-reduced-motion: reduce) {
    *,
    *::before,
    *::after {
        animation-duration: 0.01ms !important;
        animation-iteration-count: 1 !important;
        transition-duration: 0.01ms !important;
    }
}

/* ===== DARK MODE OPTIMIZATION FOR AMOLED ===== */
@media (prefers-color-scheme: dark) {
    /* AMOLED safe dark mode */
    :root {
        --bg-primary: #0a0a0a;
        --bg-secondary: #1a1a1a;
        --text-primary: #f0f0f0;
        --text-secondary: #b0b0b0;
        --border-color: #333;
    }
    
    body {
        background: var(--bg-primary);
        color: var(--text-primary);
    }
    
    input,
    textarea,
    select {
        background: var(--bg-secondary);
        color: var(--text-primary);
        border-color: var(--border-color);
    }
    
    /* High contrast for readability */
    .ihk-badge,
    .trust-marker {
        background: rgba(30, 93, 184, 0.15);
        border-color: rgba(30, 93, 184, 0.3);
        color: #e0e0e0;
    }
}

/* ===== HIGH CONTRAST MODE ===== */
@media (prefers-contrast: more) {
    :root {
        --border-width: 2px;
    }
    
    input,
    textarea,
    select,
    button {
        border-width: 2px;
    }
    
    .ihk-badge {
        border-width: 2px;
        font-weight: 700;
    }
}

/* ===== LARGE TEXT MODE ===== */
@media (prefers-color-scheme: light) and (monochrome) {
    :root {
        --font-size-base: 16px;
    }
}

/* ===== DEVICE-SPECIFIC FIXES ===== */

/* Samsung Galaxy specific - keyboard overlap */
@media (device-width: 412px) and (device-height: 915px) and (-webkit-device-pixel-ratio: 2.625) {
    /* Galaxy S24 specific */
    body {
        padding-bottom: 20px;
    }
}

/* Google Pixel 8 specific - Material Design 3 */
@media (device-width: 412px) and (device-height: 915px) and (-webkit-device-pixel-ratio: 2.75) {
    /* Pixel 8 optimizations */
    button {
        border-radius: 8px;
        box-shadow: 0 1px 3px rgba(0, 0, 0, 0.12);
    }
}

/* iPhone specific - Dynamic Island accommodation */
@media (device-width: 393px) or (device-width: 430px) {
    body {
        padding-top: 0;
        padding-bottom: 20px;
    }
}

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

/* Reduce paint operations */
input:focus,
textarea:focus,
button:active {
    will-change: auto; /* Will-change removed for mobile */
}

/* GPU acceleration for smooth scrolling */
body {
    -webkit-font-smoothing: antialiased;
    -moz-osx-font-smoothing: grayscale;
}

/* Optimize form rendering */
form {
    contain: layout style paint;
}

/* ===== NETWORK OPTIMIZATION ===== */

/* Reduce animations on slow connections */
@media (prefers-reduced-data: reduce) {
    * {
        background-attachment: scroll !important;
        animation: none !important;
        transition: none !important;
    }
}
