/**
 * MLNF Mobile Viewport Optimization
 * Comprehensive mobile-first responsive enhancements
 */

/* ===== MOBILE VIEWPORT FIXES ===== */

/* Prevent zoom on input focus (iOS) */
@media screen and (max-width: 768px) {
    input[type="text"],
    input[type="email"],
    input[type="password"],
    input[type="tel"],
    input[type="url"],
    input[type="search"],
    textarea,
    select {
        font-size: 16px !important; /* Prevents iOS zoom */
    }
}

/* Fix viewport height on mobile (iOS Safari) */
:root {
    --mobile-vh: 1vh;
}

@supports (-webkit-touch-callout: none) {
    /* iOS-specific fixes */
    body {
        min-height: -webkit-fill-available;
    }
}

/* Prevent horizontal scroll */
body {
    overflow-x: hidden;
    max-width: 100vw;
}

/* Safe area insets for notched devices */
@supports (padding: max(0px)) {
    body {
        padding-left: max(0px, env(safe-area-inset-left));
        padding-right: max(0px, env(safe-area-inset-right));
        padding-bottom: max(0px, env(safe-area-inset-bottom));
    }

    .site-header {
        padding-left: max(var(--space-md), env(safe-area-inset-left));
        padding-right: max(var(--space-md), env(safe-area-inset-right));
    }
}

/* ===== MOBILE-OPTIMIZED TYPOGRAPHY ===== */

@media (max-width: 768px) {
    /* Improve readability on mobile */
    body {
        font-size: 16px;
        line-height: 1.6;
        -webkit-text-size-adjust: 100%;
        text-size-adjust: 100%;
    }

    h1 {
        font-size: 2rem;
        line-height: 1.2;
        margin-bottom: 1rem;
    }

    h2 {
        font-size: 1.5rem;
        line-height: 1.3;
        margin-bottom: 0.875rem;
    }

    h3 {
        font-size: 1.25rem;
        line-height: 1.4;
        margin-bottom: 0.75rem;
    }

    p {
        font-size: 1rem;
        line-height: 1.6;
        margin-bottom: 1rem;
    }
}

/* ===== TOUCH-OPTIMIZED INTERACTIONS ===== */

/* Minimum touch target size: 44x44px (Apple/Google guidelines) */
@media (max-width: 768px) {
    button,
    a.btn,
    .btn-viking-primary,
    .btn-viking-secondary,
    .btn-viking-danger,
    .btn-viking-icon,
    input[type="button"],
    input[type="submit"],
    .mobile-menu-toggle {
        min-height: 44px;
        min-width: 44px;
        padding: 12px 20px;
    }

    /* Icon-only buttons */
    .btn-icon,
    .btn-viking-icon {
        min-width: 44px;
        min-height: 44px;
        padding: 10px;
    }

    /* Links in navigation */
    .site-nav a,
    .nav-link {
        min-height: 44px;
        display: flex;
        align-items: center;
        padding: 12px 16px;
    }
}

/* Remove tap highlight on interactive elements */
* {
    -webkit-tap-highlight-color: transparent;
    -webkit-touch-callout: none;
}

/* But add custom highlight for buttons */
button:active,
a:active,
.btn-viking-primary:active,
.btn-viking-secondary:active {
    opacity: 0.7;
    transform: scale(0.98);
}

/* ===== MOBILE LAYOUT OPTIMIZATIONS ===== */

@media (max-width: 768px) {
    /* Container padding */
    .container {
        padding-left: 16px;
        padding-right: 16px;
        max-width: 100%;
    }

    /* Section spacing */
    section {
        padding-top: 2rem !important;
        padding-bottom: 2rem !important;
    }

    /* Grid to single column */
    .grid,
    .grid-2,
    .grid-3,
    .grid-4 {
        grid-template-columns: 1fr !important;
        gap: 1rem !important;
    }

    /* Flexbox wrapping */
    .flex {
        flex-wrap: wrap;
    }

    /* Full-width buttons */
    .btn-viking-primary,
    .btn-viking-secondary,
    .btn-viking-danger {
        width: 100%;
        justify-content: center;
    }

    /* Card spacing */
    .card,
    .viking-card {
        margin-bottom: 1rem;
    }
}

/* ===== MOBILE HEADER OPTIMIZATION ===== */

@media (max-width: 768px) {
    .site-header {
        position: sticky;
        top: 0;
        z-index: 1000;
        padding: 12px 16px;
    }

    .header-spacer {
        height: 0 !important;
        min-height: 0 !important;
        max-height: 0 !important;
        display: none !important;
    }

    .site-logo {
        font-size: 1.125rem;
    }

    .site-logo-icon {
        font-size: 1.25rem;
    }

    /* Hide desktop-only navigation items */
    .site-nav .notification-bell,
    .site-nav #themeToggle {
        display: none !important;
    }

    /* Mobile menu overlay */
    .site-nav.active {
        position: fixed;
        top: 60px;
        left: 0;
        right: 0;
        bottom: 0;
        background: var(--navy);
        padding: 1rem;
        overflow-y: auto;
        -webkit-overflow-scrolling: touch;
    }
}

/* ===== MOBILE FORMS OPTIMIZATION ===== */

@media (max-width: 768px) {
    /* Form fields */
    .viking-input,
    .viking-textarea,
    .viking-select,
    input,
    textarea,
    select {
        width: 100%;
        font-size: 16px !important; /* Prevents zoom */
        padding: 14px;
        border-radius: 8px;
    }

    .viking-textarea,
    textarea {
        min-height: 120px;
    }

    /* Form groups */
    .viking-form-group,
    .form-group {
        margin-bottom: 1.25rem;
    }

    /* Labels */
    .viking-label,
    label {
        font-size: 14px;
        margin-bottom: 6px;
    }

    /* Checkbox and radio */
    .viking-checkbox-wrapper {
        padding: 10px 0;
    }

    .viking-checkbox {
        min-width: 24px;
        min-height: 24px;
    }
}

/* ===== MOBILE VIDEO PLAYER ===== */

@media (max-width: 768px) {
    .viking-video-container,
    .video-player-wrapper {
        border-radius: 8px;
        margin: 0 -16px; /* Bleed to edges */
        width: calc(100% + 32px);
    }

    .viking-video-player,
    video {
        border-radius: 0;
    }

    /* Video controls larger for touch */
    video::-webkit-media-controls-panel {
        padding: 8px !important;
    }

    .viking-video-title {
        font-size: 1.125rem;
        bottom: 0.75rem;
        left: 0.75rem;
        right: 0.75rem;
    }

    .viking-video-meta {
        top: 0.75rem;
        right: 0.75rem;
        gap: 0.5rem;
    }

    .viking-video-stats {
        font-size: 0.75rem;
        padding: 0.375rem 0.625rem;
    }
}

/* ===== MOBILE CARDS OPTIMIZATION ===== */

@media (max-width: 768px) {
    .viking-card,
    .card {
        padding: 1rem;
        border-radius: 8px;
    }

    .viking-card-header,
    .card-header {
        font-size: 1.25rem;
        margin-bottom: 0.75rem;
        padding-bottom: 0.625rem;
    }

    .viking-card-body,
    .card-body {
        font-size: 0.9375rem;
    }
}

/* ===== MOBILE MODALS ===== */

@media (max-width: 768px) {
    .viking-modal {
        width: 95vw;
        max-height: 90vh;
        margin: 5vh auto;
        border-radius: 12px;
    }

    .viking-modal-header {
        padding: 1rem 1.25rem;
        border-radius: 12px 12px 0 0;
    }

    .viking-modal-title {
        font-size: 1.25rem;
    }

    .viking-modal-body {
        padding: 1.25rem;
        max-height: calc(90vh - 140px);
        overflow-y: auto;
        -webkit-overflow-scrolling: touch;
    }

    .viking-modal-footer {
        padding: 1rem 1.25rem;
        flex-direction: column;
        gap: 0.75rem;
    }

    .viking-modal-footer button {
        width: 100%;
    }
}

/* ===== MOBILE TABLES ===== */

@media (max-width: 768px) {
    /* Make tables scrollable */
    table {
        display: block;
        width: 100%;
        overflow-x: auto;
        -webkit-overflow-scrolling: touch;
    }

    /* Or convert to stacked cards */
    .mobile-stack tbody,
    .mobile-stack tr,
    .mobile-stack td {
        display: block;
        width: 100%;
    }

    .mobile-stack thead {
        display: none;
    }

    .mobile-stack tr {
        margin-bottom: 1rem;
        border: 2px solid var(--battle-bronze);
        border-radius: 8px;
        padding: 1rem;
        background: var(--surface);
    }

    .mobile-stack td {
        padding: 0.5rem 0;
        border: none;
    }

    .mobile-stack td::before {
        content: attr(data-label);
        font-weight: 600;
        display: block;
        margin-bottom: 0.25rem;
        color: var(--indigo);
    }
}

/* ===== MOBILE MESSAGING ===== */

@media (max-width: 768px) {
    .raven-messenger-container {
        grid-template-columns: 1fr;
        height: calc(100vh - 60px);
    }

    .conversation-list {
        display: none;
    }

    /* Show conversation list when active */
    .raven-messenger-container.show-conversations .conversation-list {
        display: flex;
    }

    .raven-messenger-container.show-conversations .message-thread {
        display: none;
    }

    /* Back button in thread header */
    .message-thread-header {
        padding: 0.75rem 1rem;
    }

    .thread-user-info img {
        width: 36px;
        height: 36px;
    }

    .thread-user-info h4 {
        font-size: 1rem;
    }

    /* Message bubbles */
    .message-bubble {
        max-width: 85%;
        padding: 0.75rem 1rem;
        font-size: 0.9375rem;
    }

    .message-input-container textarea {
        font-size: 16px !important; /* Prevent zoom */
        padding: 0.75rem 1rem;
    }
}

/* ===== MOBILE DASHBOARD ===== */

@media (max-width: 768px) {
    .dashboard-sidebar {
        position: fixed;
        top: 60px;
        left: 0;
        right: 0;
        bottom: 0;
        z-index: 999;
        background: var(--surface);
        transform: translateX(-100%);
        transition: transform 0.3s ease;
    }

    .dashboard-sidebar.active {
        transform: translateX(0);
    }

    .stats-grid {
        grid-template-columns: 1fr !important;
    }

    .stat-card {
        padding: 1rem;
    }

    .stat-value {
        font-size: 2rem;
    }
}

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

@media (max-width: 768px) {
    /* Reduce animations on mobile for performance */
    @media (prefers-reduced-motion: reduce) {
        *,
        *::before,
        *::after {
            animation-duration: 0.01ms !important;
            animation-iteration-count: 1 !important;
            transition-duration: 0.01ms !important;
        }
    }

    /* Optimize images */
    img {
        max-width: 100%;
        height: auto;
        display: block;
    }

    /* Prevent layout shift */
    .video-thumbnail,
    .user-avatar,
    .conversation-avatar {
        aspect-ratio: attr(width) / attr(height);
    }
}

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

@media (max-width: 768px) {
    .mobile-hide {
        display: none !important;
    }

    .mobile-show {
        display: block !important;
    }

    .mobile-full-width {
        width: 100% !important;
    }

    .mobile-no-padding {
        padding: 0 !important;
    }

    .mobile-text-center {
        text-align: center !important;
    }

    .mobile-stack {
        flex-direction: column !important;
    }
}

/* ===== MOBILE LANDSCAPE OPTIMIZATION ===== */

@media (max-width: 768px) and (orientation: landscape) {
    /* Reduce header size in landscape */
    .site-header {
        padding: 8px 16px;
    }

    /* Adjust modal heights */
    .viking-modal {
        max-height: 85vh;
    }

    /* Video player full width */
    .viking-video-container {
        margin: 0;
        width: 100%;
    }
}

/* ===== TABLET OPTIMIZATION (768px - 1024px) ===== */

@media (min-width: 769px) and (max-width: 1024px) {
    .container {
        max-width: 90%;
        padding: 0 24px;
    }

    .grid-3,
    .grid-4 {
        grid-template-columns: repeat(2, 1fr) !important;
    }

    /* Messaging in 2-column layout */
    .raven-messenger-container {
        grid-template-columns: 280px 1fr;
    }
}

/* ===== PHABLET OPTIMIZATION (375px - 425px) ===== */

@media (min-width: 375px) and (max-width: 425px) {
    .container {
        padding-left: 12px;
        padding-right: 12px;
    }

    .btn-viking-primary,
    .btn-viking-secondary {
        font-size: 0.9375rem;
        padding: 11px 18px;
    }
}

/* ===== SMALL MOBILE (< 375px) ===== */

@media (max-width: 374px) {
    body {
        font-size: 14px;
    }

    .container {
        padding-left: 8px;
        padding-right: 8px;
    }

    h1 {
        font-size: 1.75rem;
    }

    h2 {
        font-size: 1.375rem;
    }

    .btn-viking-primary,
    .btn-viking-secondary {
        font-size: 0.875rem;
        padding: 10px 16px;
    }

    .site-logo {
        font-size: 1rem;
    }
}

/* ===== PROGRESSIVE WEB APP (PWA) OPTIMIZATIONS ===== */

@media (display-mode: standalone) {
    /* Running as installed PWA */
    .site-header {
        padding-top: max(12px, env(safe-area-inset-top));
    }

    /* Hide browser-specific UI hints */
    .install-prompt {
        display: none;
    }
}

/* ===== HIGH DPI DISPLAYS ===== */

@media (-webkit-min-device-pixel-ratio: 2),
       (min-resolution: 192dpi) {
    /* Sharper borders on retina */
    .viking-card,
    .btn-viking-primary,
    input,
    textarea {
        border-width: 0.5px;
    }
}

/* ===== DARK MODE MOBILE ADJUSTMENTS ===== */

@media (max-width: 768px) and (prefers-color-scheme: dark) {
    [data-theme="dark"] .site-nav.active {
        background: var(--gray-900);
    }

    [data-theme="dark"] .viking-modal {
        background: var(--gray-800);
    }
}

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

@media (max-width: 768px) {
    /* Larger focus indicators */
    *:focus-visible {
        outline: 3px solid var(--torch-amber);
        outline-offset: 2px;
    }

    /* Skip to content link */
    .skip-to-content {
        position: fixed;
        top: -100px;
        left: 10px;
        z-index: 10000;
        padding: 12px 16px;
        background: var(--indigo);
        color: white;
        text-decoration: none;
        border-radius: 8px;
        transition: top 0.3s ease;
    }

    .skip-to-content:focus {
        top: 10px;
    }
}

/* ===== MOBILE SIDEBAR OVERLAY ===== */

@media screen and (max-width: 768px) {
    /* Overlay when sidebar is open on mobile */
    body.sidebar-mobile-open::before {
        content: '';
        position: fixed;
        top: 0;
        left: 0;
        right: 0;
        bottom: 0;
        background: rgba(0, 0, 0, 0.6);
        z-index: 1000;
        animation: vikingFadeIn 0.3s ease;
    }

    /* Prevent scrolling when sidebar is open */
    body.sidebar-mobile-open {
        overflow: hidden;
    }
}

/* ===== MOBILE PRINT STYLES ===== */

@media print {
    /* Hide mobile nav when printing */
    .mobile-menu-toggle,
    .site-nav,
    .sidebar,
    .viking-modal-overlay {
        display: none !important;
    }

    /* Optimize for printing */
    body {
        margin: 0;
        padding: 0;
    }

    .container {
        max-width: 100%;
    }
}
