/* ======================================== */
/* COMPREHENSIVE RESPONSIVE DESIGN */
/* ======================================== */

/* ======================================== */
/* LARGE DESKTOP (1920px+) */
/* ======================================== */
@media (min-width: 1920px) {
    :root {
        --sidebar-width: 380px;
        --font-size-base: 1.0625rem; /* Slightly larger base font */
    }
    
    .sidebar {
        width: 380px;
    }
    
    .modal-content {
        max-width: 800px;
    }
    
    .info-content {
        max-width: 900px;
    }
}

/* ======================================== */
/* DESKTOP (1200px - 1919px) */
/* ======================================== */
@media (min-width: 1200px) and (max-width: 1919px) {
    :root {
        --sidebar-width: 340px;
    }
}

/* ======================================== */
/* TABLET LANDSCAPE (1024px - 1199px) */
/* ======================================== */
@media (min-width: 1024px) and (max-width: 1199px) {
    :root {
        --sidebar-width: 300px;
    }
    
    .sidebar {
        width: 300px;
    }
    
    #map {
        left: 300px;
    }
}

/* ======================================== */
/* TABLET PORTRAIT (768px - 1023px) */
/* ======================================== */
@media (min-width: 768px) and (max-width: 1023px) {
    :root {
        --sidebar-width: 280px;
    }
    
    .sidebar {
        width: 280px;
    }
    
    #map {
        left: 280px;
    }
    
    .sidebar-header h1 {
        font-size: var(--font-size-xl);
    }
    
    .sidebar-content {
        padding: var(--space-md);
    }
    
    .btn-grid {
        grid-template-columns: repeat(2, 1fr);
    }
}

/* ======================================== */
/* MOBILE LANDSCAPE (568px - 767px) */
/* ======================================== */
@media (min-width: 568px) and (max-width: 767px) and (orientation: landscape) {
    :root {
        --sidebar-width: 45vw;
    }
    
    .sidebar {
        width: 45vw;
        height: 100vh;
        max-height: 100vh;
    }
    
    #map {
        left: 45vw;
        top: 0;
        height: 100vh;
    }
    
    .sidebar-header {
        padding: var(--space-md);
    }
    
    .sidebar-header h1 {
        font-size: var(--font-size-lg);
        margin-bottom: 0.25rem;
    }
    
    .subtitle {
        font-size: var(--font-size-xs);
    }
    
    .sidebar-content {
        padding: var(--space-sm);
    }
    
    .control-section {
        margin-bottom: var(--space-md);
        padding-bottom: var(--space-md);
    }
    
    .btn-grid {
        grid-template-columns: repeat(3, 1fr);
        gap: 0.375rem;
    }
    
    .btn-filter,
    .btn-action {
        padding: 0.5rem 0.375rem;
        font-size: var(--font-size-xs);
    }
}

/* ======================================== */
/* MOBILE PORTRAIT (481px - 767px) */
/* ======================================== */
@media (min-width: 481px) and (max-width: 767px) and (orientation: portrait) {
    .sidebar {
        width: 100%;
        height: auto;
        max-height: 55vh;
        position: relative;
        box-shadow: 0 4px 16px rgba(0, 0, 0, 0.2);
    }
    
    #map {
        left: 0;
        top: 55vh;
        height: 45vh;
        position: relative;
    }
    
    .sidebar-header {
        padding: var(--space-lg) var(--space-md);
    }
    
    .sidebar-header h1 {
        font-size: var(--font-size-xl);
    }
    
    .sidebar-content {
        padding: var(--space-md);
        max-height: calc(55vh - 120px);
        overflow-y: auto;
    }
    
    .btn-grid {
        grid-template-columns: repeat(3, 1fr);
        gap: 0.5rem;
    }
    
    .modal-content,
    .info-content {
        max-height: 75vh;
        padding: var(--space-lg) var(--space-md);
    }
}

/* ======================================== */
/* SMALL MOBILE (320px - 480px) */
/* ======================================== */
@media (max-width: 480px) {
    :root {
        --space-xs: 0.25rem;
        --space-sm: 0.375rem;
        --space-md: 0.75rem;
        --space-lg: 1rem;
        --space-xl: 1.25rem;
    }
    
    .sidebar {
        width: 100%;
        height: auto;
        max-height: 50vh;
        position: relative;
    }
    
    #map {
        left: 0;
        top: 50vh;
        height: 50vh;
        position: relative;
    }
    
    .sidebar-header {
        padding: var(--space-md);
    }
    
    .sidebar-header h1 {
        font-size: var(--font-size-lg);
        margin-bottom: var(--space-xs);
    }
    
    .subtitle {
        font-size: var(--font-size-xs);
    }
    
    .sidebar-content {
        padding: var(--space-sm);
        max-height: calc(50vh - 90px);
    }
    
    .control-section {
        margin-bottom: var(--space-md);
        padding-bottom: var(--space-md);
    }
    
    .control-section label {
        font-size: var(--font-size-xs);
        margin-bottom: var(--space-sm);
    }
    
    .control-section h3 {
        font-size: var(--font-size-base);
        margin-bottom: var(--space-sm);
    }
    
    select,
    input[type="number"] {
        padding: var(--space-sm);
        font-size: var(--font-size-xs);
    }
    
    .btn-grid {
        grid-template-columns: repeat(2, 1fr);
        gap: 0.375rem;
    }
    
    .btn-filter,
    .btn-action {
        padding: var(--space-sm);
        font-size: 0.7rem;
    }
    
    .modal-content {
        padding: var(--space-lg) var(--space-sm);
        max-height: 80vh;
        margin: 0;
        border-radius: var(--border-radius) var(--border-radius) 0 0;
    }
    
    .info-content {
        padding: var(--space-lg) var(--space-sm);
        max-height: 80vh;
    }
    
    .legend-section {
        padding: var(--space-sm);
    }
    
    .legend-item {
        gap: var(--space-sm);
        font-size: 0.7rem;
    }
    
    .legend-color {
        width: 32px;
        height: 18px;
    }
    
    .audio-player {
        padding: var(--space-sm);
        gap: var(--space-sm);
    }
    
    .play-btn {
        width: 40px;
        height: 40px;
    }
    
    .info-grid {
        grid-template-columns: 1fr;
        gap: var(--space-sm);
    }
    
    .info-item {
        padding: var(--space-sm);
    }
}

/* ======================================== */
/* EXTRA SMALL MOBILE (< 360px) */
/* ======================================== */
@media (max-width: 359px) {
    .sidebar-header h1 {
        font-size: var(--font-size-base);
    }
    
    .subtitle {
        font-size: 0.65rem;
    }
    
    .btn-grid {
        grid-template-columns: 1fr 1fr;
        gap: 0.25rem;
    }
    
    .btn-filter,
    .btn-action {
        padding: 0.375rem 0.25rem;
        font-size: 0.65rem;
    }
    
    .legend-color {
        width: 28px;
        height: 16px;
    }
}

/* ======================================== */
/* TOUCH DEVICE OPTIMIZATIONS */
/* ======================================== */
@media (hover: none) and (pointer: coarse) {
    /* Increase touch target sizes */
    .btn-filter,
    .btn-action,
    button,
    a {
        min-height: 44px; /* Apple's recommended minimum */
        min-width: 44px;
    }
    
    select,
    input[type="number"] {
        min-height: 44px;
    }
    
    .close-btn {
        min-width: 44px;
        min-height: 44px;
    }
    
    .play-btn {
        min-width: 56px;
        min-height: 56px;
    }
    
    /* Disable hover effects */
    .btn-filter:hover,
    .btn-action:hover,
    .emotion-card:hover,
    .info-item:hover {
        transform: none;
    }
    
    /* Use active states instead */
    .btn-filter:active,
    .btn-action:active {
        transform: scale(0.95);
        opacity: 0.8;
    }
    
    /* Larger touch areas for sliders */
    input[type="range"] {
        height: 32px;
    }
    
    /* Prevent text selection on touch */
    .btn-filter,
    .btn-action,
    .legend-item,
    .emotion-card {
        -webkit-user-select: none;
        user-select: none;
        -webkit-tap-highlight-color: transparent;
    }
}

/* ======================================== */
/* HIGH DPI DISPLAYS (RETINA) */
/* ======================================== */
@media (-webkit-min-device-pixel-ratio: 2),
       (min-resolution: 192dpi) {
    body {
        -webkit-font-smoothing: antialiased;
        -moz-osx-font-smoothing: grayscale;
    }
    
    .country-flag,
    .emotion-icon {
        image-rendering: -webkit-optimize-contrast;
        image-rendering: crisp-edges;
    }
}

/* ======================================== */
/* PRINT STYLES */
/* ======================================== */
@media print {
    .sidebar,
    .modal,
    .info-panel,
    .tooltip,
    .btn-action,
    .leaflet-control-zoom {
        display: none !important;
    }
    
    #map {
        left: 0;
        width: 100%;
        height: 100vh;
        position: static;
    }
    
    body {
        background: white;
    }
}

/* ======================================== */
/* REDUCED MOTION */
/* ======================================== */
@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;
    }
}

/* ======================================== */
/* DARK MODE (OPTIONAL) */
/* ======================================== */
@media (prefers-color-scheme: dark) {
    /* Uncomment to enable dark mode support
    :root {
        --color-bg: #1a1a1a;
        --color-bg-secondary: #2a2a2a;
        --color-text: #e0e0e0;
        --color-text-light: #a0a0a0;
        --color-border: #3a3a3a;
    }
    
    body {
        background: var(--color-bg);
        color: var(--color-text);
    }
    
    .sidebar,
    .modal-content,
    .info-content {
        background: #2a2a2a;
        color: var(--color-text);
    }
    
    select,
    input[type="number"] {
        background: #3a3a3a;
        color: var(--color-text);
        border-color: var(--color-border);
    }
    */
}

/* ======================================== */
/* SAFE AREA INSETS (iPhone X+) */
/* ======================================== */
@supports (padding: max(0px)) {
    .sidebar {
        padding-left: max(0px, env(safe-area-inset-left));
        padding-right: max(0px, env(safe-area-inset-right));
    }
    
    .modal-content,
    .info-content {
        padding-left: max(var(--space-lg), env(safe-area-inset-left));
        padding-right: max(var(--space-lg), env(safe-area-inset-right));
        padding-bottom: max(var(--space-lg), env(safe-area-inset-bottom));
    }
    
    @media (max-width: 768px) {
        .sidebar-content {
            padding-bottom: max(var(--space-md), env(safe-area-inset-bottom));
        }
    }
}

/* ======================================== */
/* ACCESSIBILITY IMPROVEMENTS */
/* ======================================== */

/* High contrast mode support */
@media (prefers-contrast: high) {
    .btn-filter,
    .btn-action {
        border-width: 3px;
    }
    
    .btn-filter.active {
        border-width: 4px;
    }
}

/* Focus visible for keyboard navigation */
@supports selector(:focus-visible) {
    *:focus {
        outline: none;
    }
    
    *:focus-visible {
        outline: 3px solid var(--color-primary);
        outline-offset: 2px;
    }
}

/* Ensure proper contrast */
@media (prefers-contrast: more) {
    :root {
        --color-text: #000000;
        --color-text-light: #333333;
        --color-border: #666666;
    }
}

/* ======================================== */
/* ORIENTATION CHANGE OPTIMIZATION */
/* ======================================== */
@media (max-width: 768px) {
    @media (orientation: landscape) {
        .sidebar {
            max-height: 100vh;
            height: 100vh;
        }
        
        .sidebar-content {
            max-height: calc(100vh - 120px);
        }
    }
    
    @media (orientation: portrait) {
        .sidebar {
            max-height: 55vh;
        }
        
        .sidebar-content {
            max-height: calc(55vh - 100px);
        }
    }
}

/* ======================================== */
/* BROWSER-SPECIFIC FIXES */
/* ======================================== */

/* Safari iOS fixes */
@supports (-webkit-touch-callout: none) {
    /* Fix 100vh on mobile Safari */
    #map {
        height: -webkit-fill-available;
    }
    
    .modal-content,
    .info-content {
        max-height: -webkit-fill-available;
    }
}

/* Firefox-specific fixes */
@-moz-document url-prefix() {
    select {
        padding-right: 2rem;
    }
}

/* IE11 fallbacks (if needed) */
@media screen and (-ms-high-contrast: active), (-ms-high-contrast: none) {
    .btn-grid {
        display: -ms-flexbox;
        -ms-flex-wrap: wrap;
    }
    
    .btn-filter {
        -ms-flex: 0 0 48%;
    }
}
