/**
 * Mobile Base Styles
 * Applies to all mobile devices (both portrait and landscape)
 * Increased breakpoint to 1024px to catch high-DPI landscape phones
 */

@media (max-width: 1024px) {
    /* Default: Panels start CLOSED on mobile */
    .left-panel {
        transform: translateX(-100%) !important;
        margin-right: 0 !important;
    }

    .right-panel {
        transform: translateX(100%) !important;
        margin-left: 0 !important;
    }

    /* Keep panels closed */
    .left-panel.collapsed {
        transform: translateX(-100%) !important;
        margin-right: 0 !important;
    }

    .right-panel.collapsed {
        transform: translateX(100%) !important;
        margin-left: 0 !important;
    }

    /* Open state - slides in */
    .left-panel.open {
        transform: translateX(0) !important;
    }

    .right-panel.open {
        transform: translateX(0) !important;
    }

    /* Hide legend by default on mobile */
    .legend-box {
        display: none !important;
    }

    /* Show legend when explicitly opened - use most of screen */
    .legend-box.mobile-visible {
        display: block !important;
        position: fixed;
        left: 50%;
        top: 50%;
        transform: translate(-50%, -50%) !important;
        z-index: 2000; /* Above backdrop (1999) but below modals */
        width: 92% !important;
        max-width: 500px !important;
        max-height: 75vh;
        overflow-y: auto;
        box-shadow: 0 8px 32px rgba(0, 0, 0, 0.4);
        padding: 16px !important;
    }

    /* Legend header */
    .legend-box .legend-header {
        font-size: 15px !important;
        padding: 8px 0 !important;
        margin-bottom: 12px !important;
    }

    /* Legend sections */
    .legend-box .legend-section {
        margin-bottom: 16px !important;
    }

    .legend-box .legend-subtitle {
        font-size: 12px !important;
        margin-bottom: 10px !important;
        font-weight: 600 !important;
    }

    /* Depth scale - grid layout for better spacing */
    .legend-box .depth-scale {
        display: grid !important;
        grid-template-columns: repeat(4, 1fr);
        gap: 10px 8px !important;
        justify-items: start !important;
    }

    .legend-box .depth-item {
        display: flex !important;
        align-items: center !important;
        gap: 6px !important;
        font-size: 11px !important;
    }

    .legend-box .depth-bar {
        width: 18px !important;
        height: 18px !important;
        min-width: 18px !important;
        min-height: 18px !important;
        flex-shrink: 0 !important;
    }

    /* Magnitude circles - 2x4 grid with proper spacing */
    .legend-box .magnitude-scale {
        display: grid !important;
        grid-template-columns: repeat(4, 1fr);
        grid-template-rows: repeat(2, 1fr);
        gap: 16px 12px;
        justify-items: center;
        align-items: center;
        max-width: 100%;
        padding: 8px 0;
    }

    .legend-box .magnitude-item {
        display: flex;
        flex-direction: column;
        align-items: center;
        gap: 6px;
        font-size: 11px !important;
    }

    /* Magnitude circles - reasonable sizes that fit */
    .legend-box .magnitude-circle[style*="width: 6px"] {
        width: 8px !important;
        height: 8px !important;
    }

    .legend-box .magnitude-circle[style*="width: 9px"] {
        width: 10px !important;
        height: 10px !important;
    }

    .legend-box .magnitude-circle[style*="width: 17px"] {
        width: 16px !important;
        height: 16px !important;
    }

    .legend-box .magnitude-circle[style*="width: 23px"] {
        width: 22px !important;
        height: 22px !important;
    }

    .legend-box .magnitude-circle[style*="width: 31px"] {
        width: 28px !important;
        height: 28px !important;
    }

    .legend-box .magnitude-circle[style*="width: 39px"] {
        width: 34px !important;
        height: 34px !important;
    }

    .legend-box .magnitude-circle[style*="width: 45px"] {
        width: 40px !important;
        height: 40px !important;
    }

    .legend-box .magnitude-circle[style*="width: 53px"] {
        width: 46px !important;
        height: 46px !important;
    }

    /* Legend button for mobile */
    .mobile-legend-btn {
        position: fixed;
        bottom: 20px;
        left: 50%;
        transform: translateX(-50%);
        background: var(--primary);
        color: #ffffff;
        border: none;
        padding: 10px 20px;
        border-radius: 24px;
        font-size: 13px;
        font-weight: 600;
        z-index: 1500;
        box-shadow: 0 4px 12px rgba(0, 0, 0, 0.3);
        display: flex;
        align-items: center;
        gap: 6px;
        cursor: pointer;
    }

    .mobile-legend-btn i {
        font-size: 16px;
    }

    /* Panel toggles - hide on mobile, use FABs instead */
    .panel-toggle {
        display: none;
    }

    /* Map fills entire viewport on mobile */
    .app-container {
        position: fixed;
        top: 0;
        left: 0;
        right: 0;
        bottom: 0;
        width: 100vw;
        height: 100vh;
    }

    .map-container {
        position: absolute;
        top: 0;
        left: 0;
        right: 0;
        bottom: 0;
        width: 100vw;
        height: 100vh;
    }

    #map {
        width: 100%;
        height: 100%;
    }

    /* Ensure panels don't affect map layout when closed */
    .left-panel,
    .right-panel {
        position: fixed;
        margin: 0;
        top: 0;
        bottom: 0;
    }

    .left-panel {
        left: 0;
    }

    .right-panel {
        right: 0;
    }

    .left-panel.collapsed,
    .right-panel.collapsed {
        margin: 0;
    }

    /* Stats section - make more compact */
    .stats-section {
        padding: 10px;
    }

    .stats-grid {
        gap: 8px;
    }

    .stat-card {
        padding: 8px;
    }

    .stat-value {
        font-size: 16px;
    }

    .stat-label {
        font-size: 10px;
    }

    /* Top controls - span full width on mobile */
    .top-controls {
        position: fixed;
        top: 0;
        left: 0;
        right: 0;
        width: 100%;
        max-width: 100vw;
        margin: 0;
        transform: none;
        padding: 4px;
        gap: 3px;
        border-radius: 0;
        border-left: none;
        border-right: none;
        border-top: none;
        justify-content: center;
        flex-wrap: nowrap;
        overflow-x: auto;
        box-sizing: border-box;
        transition: top 0.3s ease;
    }

    /* Adjust toolbar when rotation banner is visible */
    body:has(.rotation-banner) .top-controls {
        top: 52px;
    }

    .control-btn {
        min-width: 32px;
        max-width: 44px;
        min-height: 32px;
        padding: 6px;
        flex: 0 1 auto;
        box-sizing: border-box;
        display: flex;
        align-items: center;
        justify-content: center;
    }

    .control-btn i {
        font-size: 16px;
        display: flex;
        align-items: center;
        justify-content: center;
    }

    /* Hide button text on very small screens */
    .control-btn span:not(.live-dot) {
        display: none;
    }

    /* Live status indicator - smaller, positioned above Leaflet attribution */
    .live-status-indicator {
        padding: 6px 10px;
        font-size: 10px;
        bottom: 35px; /* Space for Leaflet attribution */
        right: 10px;
    }

    /* Leaflet attribution control */
    .leaflet-control-attribution {
        margin-bottom: 0 !important;
        margin-right: 0 !important;
    }

    /* Earthquake list - larger touch targets */
    .earthquake-item {
        min-height: 64px;
        padding: 12px;
        margin-bottom: 8px;
    }

    .eq-magnitude {
        min-width: 48px;
        min-height: 48px;
        font-size: 18px;
    }

    .eq-location {
        font-size: 13px;
    }

    .eq-time {
        font-size: 11px;
    }

    /* Filter inputs - larger for touch */
    .date-input,
    .search-input {
        min-height: 44px;
        font-size: 16px; /* Prevents zoom on iOS */
    }

    .range-input {
        height: 10px;
        border-radius: 5px;
    }

    .range-input::-webkit-slider-runnable-track {
        height: 10px;
        border-radius: 5px;
    }

    .range-input::-moz-range-track {
        height: 10px;
        background: var(--glass);
        border-radius: 5px;
    }

    .range-input::-moz-range-progress {
        height: 10px;
        background: var(--primary);
        border-radius: 5px 0 0 5px;
    }

    .range-input::-webkit-slider-thumb {
        margin-top: -6px;
        width: 22px;
        height: 22px;
        box-shadow: 0 3px 10px rgba(59, 130, 246, 0.6);
    }

    .range-input::-moz-range-thumb {
        width: 22px;
        height: 22px;
        box-shadow: 0 3px 10px rgba(59, 130, 246, 0.6);
    }

    /* Chips and buttons - larger touch targets */
    .source-chip,
    .layer-chip,
    .preset-btn {
        min-height: 40px;
        padding: 8px 12px;
        font-size: 12px;
    }

    /* Toggle switch - larger */
    .toggle-slider {
        width: 52px;
        height: 28px;
    }

    .toggle-slider::after {
        width: 24px;
        height: 24px;
    }

    input[type="checkbox"]:checked + .toggle-slider::after {
        left: 26px;
    }

    /* Leaflet zoom controls - position below top toolbar */
    .leaflet-control-zoom {
        top: 48px !important;
        left: 10px !important;
        transition: top 0.3s ease;
    }

    .leaflet-control-zoom a {
        width: 36px !important;
        height: 36px !important;
        line-height: 36px !important;
        font-size: 18px !important;
    }

    /* Adjust zoom controls when rotation banner is visible */
    body:has(.rotation-banner) .leaflet-control-zoom {
        top: 104px !important; /* Below both banner (~52px) and toolbar (~48px) */
    }

    /* Graph Modal - Mobile Optimized */
    .graph-modal {
        width: 96% !important;
        height: auto !important;
        max-height: 90vh !important;
        max-width: 100vw !important;
        padding: 12px !important;
        border-radius: 12px !important;
    }

    .graph-modal-header {
        margin-bottom: 12px !important;
        padding-bottom: 10px !important;
        flex-direction: column;
        align-items: flex-start !important;
        gap: 8px;
    }

    .graph-modal-title {
        font-size: 16px !important;
        line-height: 1.3 !important;
    }

    .graph-modal-subtitle {
        font-size: 11px !important;
        gap: 8px !important;
        flex-direction: column !important;
        align-items: flex-start !important;
        width: 100%;
    }

    .graph-modal-stat {
        font-size: 11px !important;
        line-height: 1.4 !important;
    }

    .graph-modal-stat i {
        font-size: 12px !important;
        min-width: 14px;
    }

    .graph-modal-close {
        font-size: 20px !important;
        width: 28px !important;
        height: 28px !important;
    }

    .graph-container {
        height: 320px !important;
        padding: 6px !important;
        overflow: hidden !important;
        box-sizing: border-box !important;
    }

    .graph-canvas {
        width: 100% !important;
        height: 100% !important;
        max-width: 100% !important;
        max-height: 100% !important;
    }
}

/* Mobile Bottom Navigation (Portrait Mode) */
.mobile-bottom-nav {
    position: fixed;
    bottom: 0;
    left: 0;
    right: 0;
    background: var(--panel-bg);
    border-top: 1px solid var(--border-color);
    display: flex;
    justify-content: space-around;
    align-items: center;
    padding: 8px 0;
    z-index: 1500;
    box-shadow: 0 -2px 12px rgba(0, 0, 0, 0.15);
}

.bottom-nav-btn {
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    gap: 4px;
    flex: 1;
    padding: 8px 12px;
    background: transparent;
    border: none;
    color: var(--text-secondary);
    cursor: pointer;
    transition: all 0.2s ease;
    min-height: 60px;
}

.bottom-nav-btn:active {
    background: rgba(var(--primary-rgb), 0.1);
    transform: scale(0.95);
}

.bottom-nav-btn i {
    font-size: 24px;
    margin-bottom: 2px;
}

.bottom-nav-btn span {
    font-size: 11px;
    font-weight: 500;
    text-align: center;
}

.bottom-nav-btn.active {
    color: var(--primary);
}

/* Adjust map container to account for bottom nav in portrait */
@media (max-width: 1024px) and (orientation: portrait) {
    .map-container {
        bottom: 76px; /* Height of bottom nav */
    }

    /* Full-screen panels in portrait mode */
    .left-panel,
    .right-panel {
        width: 100vw !important;
        height: 100vh !important;
        max-width: 100vw;
        top: 0;
        bottom: 0;
        left: 0;
        right: 0;
        z-index: 2001; /* Above backdrop */
    }

    /* When open, panels cover entire screen */
    .left-panel.open,
    .right-panel.open {
        transform: translateX(0) !important;
    }

    /* Panel close buttons - position in top-right corner */
    .panel-close-btn {
        position: absolute;
        top: 12px;
        right: 12px;
        z-index: 10;
        background: rgba(0, 0, 0, 0.1);
        width: 40px;
        height: 40px;
        border-radius: 50%;
        display: flex;
        align-items: center;
        justify-content: center;
        border: none;
        cursor: pointer;
    }

    .panel-close-btn i {
        font-size: 20px;
        color: var(--text-primary);
    }

    /* Panel content should be scrollable */
    .panel-content,
    .left-panel > div,
    .right-panel > div {
        overflow-y: auto;
        max-height: 100vh;
    }

    /* Search input at top of left panel */
    .search-container {
        position: sticky;
        top: 0;
        z-index: 5;
        background: var(--panel-bg);
        padding: 12px;
        border-bottom: 1px solid var(--border-color);
    }

    /* Earthquake list should fill available space */
    .earthquake-list {
        flex: 1;
        overflow-y: auto;
        padding-bottom: 20px;
    }

    /* Panel header */
    .panel-header {
        position: sticky;
        top: 0;
        z-index: 5;
        background: var(--panel-bg);
        border-bottom: 1px solid var(--border-color);
        padding: 16px;
        padding-top: 60px; /* Space for close button */
    }

    /* Stats section - adjust for portrait */
    .stats-section {
        padding: 12px;
    }

    .stats-grid {
        display: grid;
        grid-template-columns: repeat(2, 1fr);
        gap: 10px;
    }

    /* Filter section - ensure proper spacing */
    .filter-section {
        padding: 12px;
        margin-bottom: 8px;
    }

    .filter-section-header {
        margin-bottom: 12px;
    }

    /* Date inputs - stack vertically for better UX */
    .date-range {
        display: flex;
        flex-direction: column;
        gap: 12px;
    }

    .date-field {
        width: 100%;
    }

    /* Preset buttons - wrap if needed */
    .preset-buttons {
        display: flex;
        flex-wrap: wrap;
        gap: 8px;
    }

    .preset-btn {
        flex: 1 1 calc(33.333% - 8px);
        min-width: 80px;
    }

    /* Source chips - wrap properly */
    .source-chips {
        display: flex;
        flex-wrap: wrap;
        gap: 8px;
    }

    .source-chip {
        flex: 0 1 auto;
    }

    /* Layer chips - wrap properly */
    .layer-chips {
        display: flex;
        flex-wrap: wrap;
        gap: 8px;
    }

    .layer-chip {
        flex: 0 1 auto;
    }

    /* Earthquake items - full width with better touch targets */
    .earthquake-item {
        width: 100%;
        min-height: 70px;
        padding: 14px;
        margin-bottom: 10px;
    }

    /* Logo - scale down for portrait */
    .panel-logo {
        max-width: 200px;
        margin: 0 auto;
    }

    .panel-logo img {
        width: 100%;
        height: auto;
    }

    /* Ensure panels are properly layered */
    .left-panel.open,
    .right-panel.open {
        z-index: 2001;
    }

    /* Hide bottom nav when panel is open */
    body:has(.left-panel.open) .mobile-bottom-nav,
    body:has(.right-panel.open) .mobile-bottom-nav {
        display: none;
    }

    /* Left panel specific - make content scrollable */
    .left-panel {
        display: flex;
        flex-direction: column;
    }

    /* Logo container - compact */
    .left-panel > div:first-of-type {
        padding: 12px 16px !important;
        flex-shrink: 0;
    }

    .left-panel img#sismikLogo {
        max-width: 180px !important;
        height: auto !important;
    }

    /* Search section */
    .search-section {
        flex-shrink: 0;
        padding: 12px;
    }

    .search-box {
        position: relative;
    }

    /* Filter section - scrollable */
    .filter-section {
        flex: 1;
        overflow-y: auto;
        overflow-x: hidden;
        padding: 12px;
    }

    /* Filter groups */
    .filter-group {
        margin-bottom: 16px;
    }

    .filter-label {
        display: block;
        margin-bottom: 8px;
        font-weight: 600;
    }

    /* Date range container */
    #customDateRange {
        padding: 12px !important;
    }

    /* Date inputs side by side but smaller gap */
    #customDateRange > div:first-child {
        display: flex !important;
        gap: 8px !important;
        margin-bottom: 12px !important;
    }

    /* Apply button */
    .apply-btn {
        width: 100%;
        padding: 12px;
        margin-bottom: 12px !important;
    }

    /* Historical presets grid */
    .historical-presets > div {
        display: grid !important;
        grid-template-columns: repeat(3, 1fr) !important;
        gap: 6px !important;
    }

    /* Range sliders */
    .range-slider {
        margin: 12px 0;
    }

    .range-input {
        width: 100%;
    }

    /* Stats section - compact grid */
    .stats-section .stats-grid {
        grid-template-columns: repeat(2, 1fr);
    }

    /* Right panel specific */
    .right-panel {
        display: flex;
        flex-direction: column;
    }

    /* Right panel header - compact */
    .right-panel .panel-header {
        flex-shrink: 0;
        padding: 16px;
        padding-top: 60px;
    }

    /* Earthquake list container */
    .right-panel .earthquake-list-container {
        flex: 1;
        overflow-y: auto;
        overflow-x: hidden;
    }

    /* Graph Modal - Portrait Optimizations */
    .graph-modal {
        width: 98% !important;
        padding: 10px !important;
    }

    .graph-modal-header {
        margin-bottom: 8px !important;
        padding-bottom: 8px !important;
    }

    .graph-container {
        height: 450px !important;
        padding: 4px !important;
    }
}

/* Landscape mode - optimize for horizontal orientation */
@media (max-width: 1024px) and (orientation: landscape) {
    /* Graph Modal - Landscape Optimizations */
    .graph-modal {
        width: 90% !important;
        max-width: 900px !important;
        padding: 8px !important;
        padding-top: 8px !important; /* Minimal top padding */
    }

    .graph-modal-header {
        margin-bottom: 4px !important;
        padding-bottom: 4px !important;
        padding-top: 0 !important;
        margin-top: 0 !important;
        flex-direction: row !important;
        align-items: center !important;
        gap: 12px !important;
        flex-wrap: wrap;
    }

    .graph-modal-title {
        font-size: 13px !important;
        margin-bottom: 0 !important;
        line-height: 1.2 !important;
    }

    .graph-modal-subtitle {
        font-size: 9px !important;
        flex-direction: row !important;
        gap: 12px !important;
        margin-top: 0 !important;
    }

    .graph-modal-stat {
        font-size: 9px !important;
        white-space: nowrap;
        line-height: 1.2 !important;
    }

    .graph-modal-stat i {
        font-size: 10px !important;
    }

    .graph-modal-close {
        position: absolute !important;
        top: 6px !important;
        right: 6px !important;
        font-size: 18px !important;
        width: 24px !important;
        height: 24px !important;
        z-index: 10 !important;
    }

    .graph-container {
        height: auto !important;
        max-height: calc(90vh - 60px) !important;
        min-height: 280px !important;
        padding: 4px !important;
    }

    .graph-canvas {
        width: 100% !important;
        height: 100% !important;
    }
}
