/* Performance and Responsive Design Enhancements */

/* Image Optimization and Lazy Loading */
.lazy-image {
    opacity: 0;
    transition: opacity 0.3s ease;
}

.lazy-image.loaded {
    opacity: 1;
}

/* WebP Support with Fallbacks */
picture {
    display: block;
    width: 100%;
}

/* Critical CSS Inlining Support */
.critical-css {
    display: block !important;
}

/* Non-Critical CSS Loading */
.non-critical {
    display: none;
}

.non-critical.loaded {
    display: block;
}

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

.will-change-opacity {
    will-change: opacity;
}

/* GPU Acceleration */
.gpu-accelerated {
    transform: translateZ(0);
    backface-visibility: hidden;
    perspective: 1000px;
}

/* Reduced Motion Support */
@media (prefers-reduced-motion: reduce) {
    *,
    *::before,
    *::after {
        animation-duration: 0.01ms !important;
        animation-iteration-count: 1 !important;
        transition-duration: 0.01ms !important;
        scroll-behavior: auto !important;
    }
    
    .carousel-item,
    .fade-in,
    .slide-up {
        animation: none !important;
        transition: none !important;
    }
}

/* High Contrast Mode Support */
@media (prefers-contrast: high) {
    :root {
        --primary-green: #006600;
        --earth-brown: #663300;
        --text-dark: #000000;
        --text-light: #333333;
        --border-color: #000000;
    }
    
    .btn {
        border: 2px solid currentColor;
    }
    
    .card {
        border: 2px solid var(--border-color);
    }
}

/* Dark Mode Support */
@media (prefers-color-scheme: dark) {
    :root {
        --bg-color: #1a1a1a;
        --text-dark: #ffffff;
        --text-light: #cccccc;
        --background-cream: #2d2d2d;
        --border-color: #404040;
        --shadow-light: rgba(0, 0, 0, 0.3);
        --shadow-medium: rgba(0, 0, 0, 0.5);
    }
    
    body {
        background-color: var(--bg-color);
        color: var(--text-dark);
    }
    
    .header {
        background-color: rgba(26, 26, 26, 0.95);
    }
    
    .card,
    .article-content,
    .sidebar-section {
        background-color: #2d2d2d;
        color: var(--text-dark);
    }
    
    .btn-primary {
        background-color: var(--primary-green);
    }
    
    .btn-secondary {
        background-color: transparent;
        border-color: var(--primary-green);
        color: var(--primary-green);
    }
}

/* Mobile-First Responsive Design */

/* Extra Small Devices (phones, less than 375px) */
@media (max-width: 374px) {
    .container {
        padding: 0 10px;
    }
    
    .header {
        padding: 10px 0;
    }
    
    .logo {
        font-size: 1.2rem;
    }
    
    .hero-title {
        font-size: 1.8rem;
    }
    
    .card {
        margin-bottom: 15px;
    }
    
    .article-title {
        font-size: 1.5rem;
    }
    
    .btn {
        padding: 8px 16px;
        font-size: 0.9rem;
    }
    
    .carousel {
        height: 200px;
    }
    
    .stats-grid {
        grid-template-columns: 1fr;
        gap: 10px;
    }
    
    .category-nav {
        grid-template-columns: repeat(2, 1fr);
        gap: 1rem;
    }
    
    .articles-grid {
        grid-template-columns: 1fr;
        gap: 1rem;
    }
    
    .video-grid {
        grid-template-columns: 1fr;
    }
    
    /* Additional small screen optimizations */
    .banner-carousel {
        height: 180px;
    }
    
    .banner-content h2 {
        font-size: 1.1rem;
    }
    
    .banner-content p {
        font-size: 0.8rem;
    }
    
    .search-box input {
        font-size: 14px;
        padding: 12px 40px 12px 15px;
    }
    
    .search-box button {
        width: 30px;
        height: 30px;
        font-size: 12px;
    }
    
    h1 { font-size: 1.5rem; }
    h2 { font-size: 1.3rem; }
    h3 { font-size: 1.1rem; }
    
    .article-card,
    .category-card {
        margin-bottom: 0.8rem;
    }
    
    .video-card .video-info h3 {
        font-size: 1rem;
    }
    
    .video-card .video-info p {
        font-size: 0.85rem;
    }
    
    .newsletter-form input,
    .newsletter-form button {
        font-size: 14px;
        padding: 12px;
    }
}

/* Small Devices (phones, 375px and up) */
@media (min-width: 375px) {
    .category-nav {
        grid-template-columns: repeat(3, 1fr);
        gap: 1rem;
    }
    
    .stats-grid {
        grid-template-columns: repeat(2, 1fr);
    }
}

/* Small Devices (landscape phones, 576px and up) */
@media (min-width: 576px) {
    .hero-title {
        font-size: 2.5rem;
    }
    
    .category-nav {
        grid-template-columns: repeat(3, 1fr);
        gap: 1rem;
    }
    
    .articles-grid {
        grid-template-columns: repeat(2, 1fr);
        gap: 1.5rem;
    }
    
    .video-grid {
        grid-template-columns: repeat(2, 1fr);
    }
    
    .footer-content {
        grid-template-columns: repeat(2, 1fr);
    }
}

/* Medium Devices (tablets, 768px and up) */
@media (min-width: 769px) and (max-width: 991px) {
    .container {
        padding: 0 20px;
    }
    
    .header {
        padding: 15px 0;
    }
    
    .nav-menu {
        display: flex;
        position: static;
        background: none;
        width: auto;
        height: auto;
        flex-direction: row;
        box-shadow: none;
        transform: none;
        transition: none;
    }
    
    .nav-menu li {
        margin: 0 15px;
    }
    
    .hamburger {
        display: none;
    }
    
    .hero-title {
        font-size: 3rem;
    }
    
    .hero-subtitle {
        font-size: 1.3rem;
    }
    
    .search-box {
        max-width: 600px;
    }
    
    .carousel {
        height: 400px;
    }
    
    .articles-grid {
        grid-template-columns: repeat(2, 1fr);
    }
    
    .category-nav {
        grid-template-columns: repeat(3, 1fr);
        gap: 1rem;
    }
    
    .stats-grid {
        grid-template-columns: repeat(4, 1fr);
    }
    
    .video-grid {
        grid-template-columns: repeat(3, 1fr);
    }
    
    .footer-content {
        grid-template-columns: repeat(4, 1fr);
    }
    
    .article-layout {
        grid-template-columns: 250px 1fr 300px;
    }
    
    .table-of-contents {
        display: block;
    }
    
    .content-layout {
        grid-template-columns: 300px 1fr;
    }
    
    .filters-sidebar {
        display: block;
    }
}

/* Large Devices (desktops, 992px and up) */
@media (min-width: 992px) {
    .container {
        max-width: 960px;
    }
    
    .hero-title {
        font-size: 3.5rem;
    }
    
    .articles-grid {
        grid-template-columns: repeat(3, 1fr);
    }
    
    .category-nav {
        grid-template-columns: repeat(3, 1fr);
        gap: 1rem;
    }
}

/* Extra Large Devices (large desktops, 1200px and up) */
@media (min-width: 1200px) {
    .container {
        max-width: 1140px;
    }
    
    .hero-title {
        font-size: 4rem;
    }
    
    .articles-grid {
        grid-template-columns: repeat(3, 1fr);
        gap: 2rem;
    }
    
    .article-detail {
        max-width: 1200px;
    }
}

/* Ultra Wide Screens (1440px and up) */
@media (min-width: 1440px) {
    .container {
        max-width: 1320px;
    }
    
    .hero-title {
        font-size: 4.5rem;
    }
}

/* Print Styles */
@media print {
    * {
        background: transparent !important;
        color: black !important;
        box-shadow: none !important;
        text-shadow: none !important;
    }
    
    .header,
    .footer,
    .sidebar,
    .comments-section,
    .article-actions,
    .social-links,
    .share-options,
    .advertisement,
    .modal,
    .hamburger,
    .pagination,
    .filters-sidebar {
        display: none !important;
    }
    
    .container {
        width: 100% !important;
        max-width: none !important;
        padding: 0 !important;
        margin: 0 !important;
    }
    
    .article-content {
        box-shadow: none !important;
        border: none !important;
        padding: 0 !important;
    }
    
    .article-title {
        font-size: 24pt !important;
        margin-bottom: 10pt !important;
    }
    
    .article-content h2 {
        font-size: 18pt !important;
        page-break-before: always;
    }
    
    .article-content h3 {
        font-size: 16pt !important;
    }
    
    .article-content p {
        font-size: 12pt !important;
        line-height: 1.4 !important;
        orphans: 3;
        widows: 3;
    }
    
    .article-image img {
        max-width: 100% !important;
        height: auto !important;
        page-break-inside: avoid;
    }
    
    a[href]:after {
        content: " (" attr(href) ")";
        font-size: 10pt;
    }
    
    .article-quote {
        border: 1px solid #000;
        page-break-inside: avoid;
    }
}

/* Landscape Mode Optimizations */
@media (max-width: 767px) and (orientation: landscape) {
    .banner-carousel {
        height: 300px;
    }
    
    .nav-menu {
        max-height: 70vh;
        overflow-y: auto;
        -webkit-overflow-scrolling: touch;
    }
    
    .nav-menu a {
        padding: 0.8rem;
        font-size: 1rem;
    }
    
    .articles-grid {
        grid-template-columns: repeat(2, 1fr);
        gap: 1rem;
    }
    
    .video-grid {
        grid-template-columns: repeat(2, 1fr);
    }
    
    .category-nav {
        grid-template-columns: repeat(3, 1fr);
        gap: 1rem;
    }
}

/* Mobile Devices (phones, 375px and up) */
@media (min-width: 375px) {
    .articles-grid {
        grid-template-columns: repeat(2, 1fr);
        gap: 1rem;
    }
    
    .video-grid {
        grid-template-columns: repeat(2, 1fr);
    }
    
    .category-nav {
        grid-template-columns: repeat(3, 1fr);
        gap: 1rem;
    }
    
    .banner-carousel {
        height: 350px;
    }
    
    .banner-content h2 {
        font-size: 1.8rem;
    }
    
    .banner-content p {
        font-size: 1.1rem;
    }
}

/* Enhanced Touch Interactions */
@media (max-width: 768px) {
    /* Smooth scrolling for mobile */
    html {
        scroll-behavior: smooth;
        -webkit-overflow-scrolling: touch;
    }
    
    /* Better touch feedback */
    .btn:active,
    .article-card:active,
    .category-card:active,
    .video-card:active,
    .nav-menu a:active {
        transform: scale(0.98);
        transition: transform 0.1s ease;
    }
    
    /* Prevent text selection on interactive elements */
    .btn,
    .article-card,
    .category-card,
    .video-card,
    .nav-menu a,
    .banner-btn {
        -webkit-user-select: none;
        -moz-user-select: none;
        -ms-user-select: none;
        user-select: none;
        -webkit-touch-callout: none;
    }
    
    /* Better mobile focus states */
    .btn:focus,
    input:focus,
    textarea:focus,
    select:focus {
        outline: 2px solid var(--primary-green);
        outline-offset: 2px;
    }
    
    /* Mobile loading states */
    .loading-mobile {
        position: fixed;
        top: 50%;
        left: 50%;
        transform: translate(-50%, -50%);
        background: rgba(255, 255, 255, 0.95);
        padding: 2rem;
        border-radius: 12px;
        box-shadow: var(--shadow-medium);
        z-index: 9999;
    }
}

/* Landscape Orientation Optimizations */
@media (orientation: landscape) and (max-height: 500px) {
    .header {
        position: fixed;
        top: 0;
        left: 0;
        right: 0;
        z-index: 1000;
    }
    
    main {
        margin-top: 80px;
    }
    
    .hero {
        min-height: 60vh;
    }
    
    .carousel {
        height: 300px;
    }
    
    .footer {
        margin-top: 2rem;
    }
}

/* Touch Device Optimizations */
@media (hover: none) and (pointer: coarse) {
    .btn,
    .card,
    .nav-menu a {
        min-height: 44px;
        min-width: 44px;
    }
    
    .card:hover {
        transform: none;
    }
    
    .btn:hover {
        transform: none;
    }
    
    .nav-menu a:hover {
        background: none;
    }
    
    .dropdown-menu {
        position: fixed;
        top: 50%;
        left: 50%;
        transform: translate(-50%, -50%);
        width: 90%;
        max-width: 300px;
        box-shadow: var(--shadow-medium);
    }
}

/* Enhanced Mobile Optimizations */
@media (max-width: 767px) {
    /* Hide category navigation on mobile */
    .category-nav {
        display: none;
    }
    
    /* Better mobile typography */
    body {
        font-size: 16px; /* Prevent zoom on iOS */
        -webkit-text-size-adjust: 100%; /* Prevent text inflation */
        -webkit-font-smoothing: antialiased;
        -moz-osx-font-smoothing: grayscale;
    }
    
    /* Improved touch targets */
    .btn,
    button,
    .search-box button,
    .banner-btn,
    .newsletter-form button,
    .article-card,
    .category-card,
    .video-card {
        min-height: 44px;
        min-width: 44px;
        touch-action: manipulation; /* Remove double-tap delay */
        -webkit-tap-highlight-color: transparent; /* Remove tap highlight */
    }
    
    /* Better mobile navigation */
    .nav-menu {
        padding: 1rem 0;
        backdrop-filter: blur(10px);
        -webkit-backdrop-filter: blur(10px);
    }
    
    .nav-menu a {
        padding: 1rem;
        font-size: 1.1rem;
        border-bottom: 1px solid rgba(0,0,0,0.1);
    }
    
    .nav-menu li:last-child a {
        border-bottom: none;
    }
    
    /* Mobile-optimized search */
    .search-box {
        margin: 1rem auto;
        max-width: 100%;
    }
    
    .search-box input {
        font-size: 16px; /* Prevent zoom on iOS */
        padding: 15px 50px 15px 20px;
        border-radius: 25px;
    }
    
    .search-suggestions {
        max-height: 250px;
        font-size: 14px;
    }
    
    /* Better mobile cards */
    .article-card,
    .category-card,
    .video-card {
        margin-bottom: 1rem;
        border-radius: 12px;
        overflow: hidden;
        box-shadow: 0 2px 8px rgba(0,0,0,0.1);
    }
    
    .article-card:active,
    .category-card:active,
    .video-card:active {
        transform: scale(0.98);
        transition: transform 0.1s ease;
    }
    
    /* Mobile-optimized banner */
    .banner-carousel {
        height: 250px;
        border-radius: 12px;
        overflow: hidden;
    }
    
    .banner-content {
        padding: 1.5rem;
        background: linear-gradient(transparent, rgba(0,0,0,0.7));
    }
    
    .banner-content h2 {
        font-size: 1.3rem;
        margin-bottom: 0.5rem;
        line-height: 1.3;
    }
    
    .banner-content p {
        font-size: 0.9rem;
        line-height: 1.4;
    }
    
    /* Better mobile forms */
    .newsletter-form {
        flex-direction: column;
        gap: 1rem;
    }
    
    .newsletter-form input {
        width: 100%;
        font-size: 16px;
        padding: 15px;
        border-radius: 25px;
    }
    
    .newsletter-form button {
        width: 100%;
        padding: 15px;
        border-radius: 25px;
        font-size: 1rem;
    }
    
    /* Mobile-optimized footer */
    .footer-content {
        gap: 2rem;
    }
    
    .footer-section {
        text-align: center;
        padding: 0 1rem;
    }
    
    .footer-section h3 {
        font-size: 1.2rem;
        margin-bottom: 1rem;
    }
    
    /* Better mobile pagination */
    .pagination {
        flex-wrap: wrap;
        gap: 0.5rem;
    }
    
    .pagination button {
        min-width: 44px;
        min-height: 44px;
        padding: 0.8rem;
        font-size: 1rem;
        border-radius: 8px;
    }
    
    /* Mobile chart optimization */
    .chart-container {
        padding: 1rem;
        overflow-x: auto;
        -webkit-overflow-scrolling: touch;
    }
    
    #cropChart {
        max-width: 100%;
        height: auto;
    }
    
    /* Better mobile modal */
    .modal-content {
        width: 95%;
        max-width: 95%;
        margin: 1rem;
        border-radius: 12px;
        max-height: 90vh;
        overflow-y: auto;
        -webkit-overflow-scrolling: touch;
    }
    
    /* Mobile performance optimizations */
    .article-image {
        loading: lazy;
        decoding: async;
    }
    
    /* Safe area support for modern phones */
    .container {
        padding-left: max(15px, env(safe-area-inset-left));
        padding-right: max(15px, env(safe-area-inset-right));
    }
    
    /* Better mobile text readability */
    h1 { font-size: 1.8rem; }
    h2 { font-size: 1.5rem; }
    h3 { font-size: 1.3rem; }
    
    p {
        font-size: 1rem;
        line-height: 1.6;
    }
    
    /* Mobile video optimization */
    .video-grid {
        gap: 1rem;
    }
    
    .video-thumbnail {
        aspect-ratio: 16/9;
        object-fit: cover;
    }
    
    .play-button {
        width: 60px;
        height: 60px;
        font-size: 1.5rem;
    }
}

/* High DPI Displays */
@media (-webkit-min-device-pixel-ratio: 2), (min-resolution: 192dpi) {
    .logo-icon {
        image-rendering: -webkit-optimize-contrast;
        image-rendering: crisp-edges;
    }
    
    .author-avatar,
    .comment-avatar {
        image-rendering: -webkit-optimize-contrast;
    }
}

/* Network-Aware Loading */
@media (prefers-reduced-data: reduce) {
    .video-player,
    .carousel,
    .article-image img {
        display: none;
    }
    
    .video-placeholder,
    .image-placeholder {
        display: flex;
        align-items: center;
        justify-content: center;
        background: var(--background-cream);
        color: var(--text-light);
        font-style: italic;
        min-height: 200px;
        border: 1px dashed var(--border-color);
    }
}

/* Battery-Aware Optimizations */
@media (prefers-reduced-battery: high) {
    .carousel,
    .animation,
    .video-player {
        animation: none !important;
        transition: none !important;
    }
    
    .carousel-item {
        position: static;
        display: none;
    }
    
    .carousel-item:first-child {
        display: block;
    }
}

/* Focus Management for Keyboard Navigation */
.btn:focus,
.nav-menu a:focus,
.card:focus,
input:focus,
textarea:focus,
select:focus {
    outline: 2px solid var(--primary-green);
    outline-offset: 2px;
}

/* Skip Links for Accessibility */
.skip-link {
    position: absolute;
    top: -40px;
    left: 6px;
    background: var(--primary-green);
    color: white;
    padding: 8px;
    text-decoration: none;
    z-index: 1000;
    border-radius: 4px;
}

.skip-link:focus {
    top: 6px;
}

/* Loading States */
.loading {
    position: relative;
    overflow: hidden;
}

.loading::after {
    content: '';
    position: absolute;
    top: 0;
    left: -100%;
    width: 100%;
    height: 100%;
    background: linear-gradient(
        90deg,
        transparent,
        rgba(255, 255, 255, 0.2),
        transparent
    );
    animation: loading-shimmer 1.5s infinite;
}

@keyframes loading-shimmer {
    0% {
        left: -100%;
    }
    100% {
        left: 100%;
    }
}

/* Skeleton Loading */
.skeleton {
    background: linear-gradient(
        90deg,
        var(--background-cream) 25%,
        #f0f0f0 50%,
        var(--background-cream) 75%
    );
    background-size: 200% 100%;
    animation: skeleton-loading 1.5s infinite;
}

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

/* Error States */
.error-state {
    background: #ffebee;
    border: 1px solid #f44336;
    color: #c62828;
    padding: 1rem;
    border-radius: 4px;
    margin: 1rem 0;
}

/* Success States */
.success-state {
    background: #e8f5e8;
    border: 1px solid #4caf50;
    color: #2e7d32;
    padding: 1rem;
    border-radius: 4px;
    margin: 1rem 0;
}

/* Progressive Enhancement */
.no-js .carousel,
.no-js .video-player,
.no-js .modal {
    display: none;
}

.no-js .carousel-item {
    display: block;
}

.no-js .dropdown-menu {
    position: static;
    display: block;
    opacity: 1;
    visibility: visible;
    transform: none;
    box-shadow: none;
}

/* Font Loading Optimization */
.font-loading {
    font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, sans-serif;
}

.font-loaded {
    font-family: 'Inter', 'Roboto', -apple-system, BlinkMacSystemFont, 'Segoe UI', sans-serif;
}

/* Container Queries (when supported) */
@supports (container-type: inline-size) {
    .article-card {
        container-type: inline-size;
    }
    
    @container (min-width: 400px) {
        .article-card h3 {
            font-size: 1.2rem;
        }
    }
    
    @container (min-width: 600px) {
        .article-card h3 {
            font-size: 1.4rem;
        }
    }
}

/* Viewport Meta Tag Support */
@viewport {
    width: device-width;
    zoom: 1.0;
}

/* CSS Grid Fallback for Older Browsers */
@supports not (display: grid) {
    .articles-grid {
        display: flex;
        flex-wrap: wrap;
        margin: -10px;
    }
    
    .card {
        flex: 1 1 300px;
        margin: 10px;
    }
    
    .category-nav {
        display: flex;
        flex-wrap: wrap;
        margin: -10px;
    }
    
    .category-card {
        flex: 1 1 200px;
        margin: 10px;
    }
}

/* Flexbox Fallback for Grid */
@supports not (display: flex) {
    .header nav {
        display: block;
        text-align: center;
    }
    
    .nav-menu {
        display: block;
    }
    
    .nav-menu li {
        display: inline-block;
        margin: 0 10px;
    }
}