/**
 * Mobile Optimization Stylesheet
 * Hardware Engineer Knowledge Base
 * Enhanced mobile experience with touch-friendly UI
 */

/* ============================================
   Mobile-First Enhancements
   ============================================ */

@media (max-width: 768px) {

    /* === Touch-Friendly Interactions === */

    /* Increase tap targets to at least 44x44px (Apple HIG) */
    button,
    a,
    input,
    select,
    textarea,
    .nav-link,
    .formula-card,
    .case-card,
    .tool-card {
        min-height: 44px;
        min-width: 44px;
    }

    /* Better touch feedback */
    button:active,
    a:active,
    .nav-link:active {
        transform: scale(0.96);
        transition: transform 0.1s;
    }

    /* === Typography Optimization === */

    body {
        font-size: 16px; /* Prevent iOS zoom on input focus */
        line-height: 1.6;
    }

    h1 {
        font-size: 28px;
        line-height: 1.2;
    }

    h2 {
        font-size: 24px;
        line-height: 1.3;
    }

    h3 {
        font-size: 20px;
        line-height: 1.4;
    }

    h4 {
        font-size: 18px;
    }

    p {
        font-size: 16px;
        line-height: 1.6;
    }

    /* === Navbar Optimization === */

    .navbar {
        position: sticky;
        top: 0;
        z-index: 1000;
        padding: 12px 0;
    }

    .nav-container {
        padding: 0 16px;
    }

    .logo {
        font-size: 18px;
        white-space: nowrap;
        overflow: hidden;
        text-overflow: ellipsis;
        max-width: 200px;
    }

    .nav-links {
        position: fixed;
        top: 0;
        left: -100%;
        width: 280px;
        height: 100vh;
        background: white;
        flex-direction: column;
        padding: 80px 20px 20px;
        transition: left 0.3s;
        box-shadow: 2px 0 10px rgba(0,0,0,0.1);
        overflow-y: auto;
        z-index: 1001;
    }

    .nav-links.active {
        left: 0;
    }

    .nav-links li {
        margin: 0;
        width: 100%;
    }

    .nav-links a {
        display: block;
        padding: 14px 16px;
        margin: 4px 0;
        border-radius: 8px;
        transition: all 0.2s;
    }

    .nav-links a:hover,
    .nav-links a.active {
        background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
        color: white;
    }

    /* Mobile menu toggle */
    .mobile-menu-toggle {
        display: flex;
        align-items: center;
        justify-content: center;
        width: 44px;
        height: 44px;
        background: transparent;
        border: none;
        cursor: pointer;
        color: inherit;
        font-size: 24px;
        margin-right: 8px;
    }

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

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

    /* === Hero Section === */

    .hero {
        padding: 40px 20px;
        min-height: auto;
    }

    .hero h1 {
        font-size: 32px;
        margin-bottom: 16px;
    }

    .hero p {
        font-size: 16px;
        margin-bottom: 24px;
    }

    .hero-features {
        grid-template-columns: 1fr;
        gap: 16px;
        margin-top: 32px;
    }

    /* === Search Optimization === */

    .search-container {
        padding: 20px 16px;
    }

    .search-box {
        flex-direction: column;
        gap: 12px;
    }

    .search-input {
        font-size: 16px; /* Prevent zoom on iOS */
        padding: 14px 16px;
    }

    .search-btn {
        width: 100%;
        justify-content: center;
    }

    #searchResults {
        margin-top: 16px;
    }

    .search-result-item {
        padding: 16px;
        margin-bottom: 12px;
    }

    /* === Cards Grid === */

    .formula-grid,
    .case-grid,
    .tools-grid {
        grid-template-columns: 1fr;
        gap: 16px;
        padding: 0 16px;
    }

    .formula-card,
    .case-card,
    .tool-card {
        padding: 20px;
        margin-bottom: 0;
    }

    /* === Floating Action Buttons === */

    .back-to-top,
    .share-button,
    .feedback-button {
        width: 48px;
        height: 48px;
        font-size: 20px;
        bottom: 20px;
        box-shadow: 0 4px 16px rgba(0,0,0,0.3);
    }

    .back-to-top {
        right: 20px;
        bottom: 20px;
    }

    .share-button {
        right: 20px;
        bottom: 80px;
    }

    .feedback-button {
        right: 20px;
        bottom: 140px;
    }

    /* Stack FABs on smaller screens */
    @media (max-width: 480px) {
        .back-to-top,
        .share-button,
        .feedback-button {
            right: 16px;
        }
    }

    /* === Modal Optimization === */

    .modal,
    .share-modal,
    .feedback-modal {
        padding: 16px;
        align-items: flex-end;
    }

    .modal-content,
    .share-content,
    .feedback-content {
        max-height: 85vh;
        width: 100%;
        max-width: 100%;
        padding: 24px;
        border-radius: 16px 16px 0 0;
    }

    /* === Forms === */

    input,
    textarea,
    select {
        font-size: 16px; /* Prevent iOS zoom */
        padding: 14px;
    }

    textarea {
        min-height: 120px;
    }

    /* === Tables === */

    table {
        display: block;
        overflow-x: auto;
        -webkit-overflow-scrolling: touch;
        white-space: nowrap;
    }

    /* === Footer === */

    .footer {
        padding: 40px 20px;
    }

    .footer-content {
        grid-template-columns: 1fr;
        gap: 32px;
    }

    .footer-section {
        text-align: center;
    }

    /* === Cookie Consent Banner === */

    .cookie-consent-banner {
        bottom: 0;
        left: 0;
        right: 0;
        border-radius: 16px 16px 0 0;
        max-width: 100%;
        width: 100%;
    }

    .cookie-consent-content {
        flex-direction: column;
        padding: 20px;
    }

    .cookie-consent-buttons {
        width: 100%;
        flex-direction: column;
    }

    .cookie-btn {
        width: 100%;
    }

    /* === Code Blocks === */

    pre,
    code {
        font-size: 13px;
        overflow-x: auto;
        -webkit-overflow-scrolling: touch;
    }

    /* === Images === */

    img {
        max-width: 100%;
        height: auto;
    }

    /* === Loading States === */

    .loading {
        padding: 40px 20px;
    }

    /* === Error States === */

    .error-message {
        margin: 20px 16px;
        padding: 20px;
    }
}

/* ============================================
   Small Mobile Devices (< 480px)
   ============================================ */

@media (max-width: 480px) {

    .hero h1 {
        font-size: 28px;
    }

    .hero p {
        font-size: 15px;
    }

    .logo {
        font-size: 16px;
        max-width: 150px;
    }

    .nav-links {
        width: 100%;
        max-width: 280px;
    }

    .formula-card h4,
    .case-card h4 {
        font-size: 16px;
    }

    .modal-content,
    .share-content,
    .feedback-content {
        padding: 20px;
    }
}

/* ============================================
   Landscape Orientation
   ============================================ */

@media (max-width: 768px) and (orientation: landscape) {

    .hero {
        padding: 30px 20px;
    }

    .modal,
    .share-modal,
    .feedback-modal {
        align-items: center;
    }

    .modal-content,
    .share-content,
    .feedback-content {
        max-height: 90vh;
        border-radius: 16px;
    }
}

/* ============================================
   Touch Optimization
   ============================================ */

/* Remove hover effects on touch devices */
@media (hover: none) {

    .formula-card:hover,
    .case-card:hover,
    .tool-card:hover {
        transform: none;
    }

    /* Use active state instead */
    .formula-card:active,
    .case-card:active,
    .tool-card:active {
        transform: scale(0.98);
    }
}

/* ============================================
   iOS Specific Fixes
   ============================================ */

/* Safe area insets for iPhone X and newer */
@supports (padding: max(0px)) {

    .navbar {
        padding-left: max(20px, env(safe-area-inset-left));
        padding-right: max(20px, env(safe-area-inset-right));
    }

    .hero,
    .content-section {
        padding-left: max(20px, env(safe-area-inset-left));
        padding-right: max(20px, env(safe-area-inset-right));
    }

    .back-to-top,
    .share-button,
    .feedback-button {
        right: max(20px, env(safe-area-inset-right));
        bottom: max(20px, env(safe-area-inset-bottom));
    }

    .cookie-consent-banner {
        padding-bottom: max(20px, env(safe-area-inset-bottom));
    }
}

/* Disable iOS text size adjustment */
html {
    -webkit-text-size-adjust: 100%;
}

/* Remove iOS input shadows */
input,
textarea,
select {
    -webkit-appearance: none;
    -moz-appearance: none;
    appearance: none;
}

/* ============================================
   Performance Optimizations
   ============================================ */

/* Use GPU acceleration for animations */
.back-to-top,
.share-button,
.feedback-button,
.modal,
.nav-links {
    will-change: transform;
    transform: translateZ(0);
}

/* Optimize scrolling */
.nav-links,
.modal-content,
.share-content,
.feedback-content {
    -webkit-overflow-scrolling: touch;
    overflow-scrolling: touch;
}

/* ============================================
   Accessibility on Mobile
   ============================================ */

/* Larger focus indicators */
@media (max-width: 768px) {

    *:focus {
        outline: 3px solid #667eea;
        outline-offset: 2px;
    }

    /* Skip to content link */
    .skip-to-content {
        position: absolute;
        top: -40px;
        left: 0;
        background: #667eea;
        color: white;
        padding: 12px 20px;
        text-decoration: none;
        z-index: 10000;
        border-radius: 0 0 8px 0;
    }

    .skip-to-content:focus {
        top: 0;
    }
}

/* ============================================
   Print Styles for Mobile
   ============================================ */

@media print {

    /* Hide unnecessary elements */
    .navbar,
    .back-to-top,
    .share-button,
    .feedback-button,
    .cookie-consent-banner,
    .mobile-menu-toggle,
    .search-container {
        display: none !important;
    }

    /* Optimize for print */
    body {
        font-size: 12pt;
        line-height: 1.5;
    }

    .formula-card,
    .case-card {
        page-break-inside: avoid;
        break-inside: avoid;
    }
}

/* ============================================
   PWA Install Prompt (Mobile)
   ============================================ */

@media (max-width: 768px) {

    .pwa-install-banner {
        position: fixed;
        bottom: 0;
        left: 0;
        right: 0;
        background: white;
        box-shadow: 0 -4px 20px rgba(0,0,0,0.15);
        padding: 20px;
        padding-bottom: max(20px, env(safe-area-inset-bottom));
        z-index: 999;
        animation: slideUpBanner 0.3s;
    }

    @keyframes slideUpBanner {
        from {
            transform: translateY(100%);
        }
        to {
            transform: translateY(0);
        }
    }

    .pwa-install-content {
        display: flex;
        align-items: center;
        gap: 16px;
    }

    .pwa-install-icon {
        width: 56px;
        height: 56px;
        border-radius: 12px;
        background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
        display: flex;
        align-items: center;
        justify-content: center;
        font-size: 28px;
        flex-shrink: 0;
    }

    .pwa-install-text {
        flex: 1;
    }

    .pwa-install-text h3 {
        font-size: 16px;
        margin: 0 0 4px 0;
    }

    .pwa-install-text p {
        font-size: 14px;
        margin: 0;
        color: #666;
    }

    .pwa-install-actions {
        display: flex;
        gap: 8px;
        margin-top: 16px;
    }

    .pwa-install-actions button {
        flex: 1;
        padding: 12px;
        border: none;
        border-radius: 8px;
        font-weight: 600;
        cursor: pointer;
    }

    .pwa-install-btn {
        background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
        color: white;
    }

    .pwa-dismiss-btn {
        background: #f5f5f5;
        color: #666;
    }
}

/* ============================================
   Dark Mode Mobile Adjustments
   ============================================ */

[data-theme="dark"] {

    @media (max-width: 768px) {

        .nav-links {
            background: #2a2a2a;
        }

        .modal-content,
        .share-content,
        .feedback-content {
            background: #2a2a2a;
        }

        .pwa-install-banner {
            background: #2a2a2a;
        }

        .pwa-install-text p {
            color: #999;
        }

        .pwa-dismiss-btn {
            background: #1e1e1e;
            color: #ccc;
        }
    }
}

/* ============================================
   Gesture Support
   ============================================ */

/* Swipe to close modals */
.modal-content,
.share-content,
.feedback-content {
    touch-action: pan-y;
}

/* Pull to refresh indicator (optional) */
@media (max-width: 768px) {

    .pull-to-refresh {
        position: fixed;
        top: 0;
        left: 50%;
        transform: translateX(-50%) translateY(-100%);
        background: rgba(102, 126, 234, 0.9);
        color: white;
        padding: 12px 24px;
        border-radius: 0 0 12px 12px;
        font-size: 14px;
        font-weight: 600;
        z-index: 10000;
        transition: transform 0.3s;
    }

    .pull-to-refresh.active {
        transform: translateX(-50%) translateY(0);
    }
}

/* ============================================
   Bottom Navigation (Alternative UI)
   ============================================ */

@media (max-width: 768px) {

    .bottom-nav {
        position: fixed;
        bottom: 0;
        left: 0;
        right: 0;
        background: white;
        box-shadow: 0 -2px 10px rgba(0,0,0,0.1);
        padding: 8px 0;
        padding-bottom: max(8px, env(safe-area-inset-bottom));
        display: none; /* Enable if needed */
        z-index: 998;
    }

    .bottom-nav-items {
        display: flex;
        justify-content: space-around;
        align-items: center;
    }

    .bottom-nav-item {
        flex: 1;
        display: flex;
        flex-direction: column;
        align-items: center;
        gap: 4px;
        padding: 8px;
        text-decoration: none;
        color: #666;
        font-size: 12px;
        transition: all 0.2s;
    }

    .bottom-nav-item i {
        font-size: 20px;
    }

    .bottom-nav-item.active {
        color: #667eea;
    }

    [data-theme="dark"] .bottom-nav {
        background: #2a2a2a;
    }

    [data-theme="dark"] .bottom-nav-item {
        color: #999;
    }

    [data-theme="dark"] .bottom-nav-item.active {
        color: #764ba2;
    }
}
