/**
 * Shared Mobile Responsiveness Styles for ArcGIS JS Maps
 * 
 * This stylesheet provides consistent mobile-responsive patterns across all demos.
 * Include this file after your main styles to apply mobile optimizations.
 * 
 * Usage:
 * <link rel="stylesheet" href="../shared/mobile-responsive.css">
 */

/* ============================================
   MOBILE BREAKPOINTS
   ============================================ */
:root {
    --mobile-small: 480px;
    --mobile: 768px;
    --tablet: 1024px;
    
    /* Panel dimensions */
    --panel-mobile-height: 60vh;
    --panel-desktop-width: 350px;
    --panel-padding: 20px;
    
    /* Z-index layers */
    --z-header: 1001;
    --z-panel: 1000;
    --z-overlay: 999;
    --z-stats: 1000;
}

/* ============================================
   DYNAMIC VIEWPORT HEIGHT SUPPORT
   ============================================ */
body {
    /* Use dynamic viewport height for better mobile browser support */
    height: 100dvh;
    height: 100vh; /* Fallback */
}

.map-container {
    height: 100%;
}

/* ============================================
   COLLAPSIBLE CONTROL PANEL PATTERN
   ============================================ */

/* Toggle button for mobile */
.panel-toggle-button {
    display: none;
    position: absolute;
    top: 15px;
    left: 15px;
    z-index: calc(var(--z-panel) + 1);
    background: white;
    border: none;
    border-radius: 8px;
    padding: 12px;
    box-shadow: 0 4px 12px rgba(0,0,0,0.15);
    cursor: pointer;
    transition: all 0.3s ease;
}

.panel-toggle-button:hover {
    box-shadow: 0 6px 16px rgba(0,0,0,0.2);
    transform: scale(1.05);
}

.panel-toggle-button i {
    font-size: 1.2rem;
    color: var(--primary-color, #0078CA);
}

/* Mobile: Show toggle button, hide panel by default */
@media (max-width: 768px) {
    .panel-toggle-button {
        display: block;
    }
    
    .control-panel {
        position: fixed !important;
        top: auto !important;
        left: 0 !important;
        right: 0 !important;
        bottom: 0 !important;
        max-width: 100% !important;
        max-height: var(--panel-mobile-height) !important;
        border-radius: 16px 16px 0 0 !important;
        transform: translateY(100%);
        transition: transform 0.3s ease-in-out;
        box-shadow: 0 -4px 20px rgba(0,0,0,0.2) !important;
    }
    
    .control-panel.active {
        transform: translateY(0);
    }
    
    /* Add backdrop when panel is open */
    .panel-backdrop {
        display: none;
        position: fixed;
        top: 0;
        left: 0;
        right: 0;
        bottom: 0;
        background: rgba(0, 0, 0, 0.5);
        z-index: var(--z-overlay);
        opacity: 0;
        transition: opacity 0.3s ease;
    }
    
    .panel-backdrop.active {
        display: block;
        opacity: 1;
    }
    
    /* Close button for mobile panel */
    .panel-close-button {
        display: block;
        position: absolute;
        top: 10px;
        right: 10px;
        background: transparent;
        border: none;
        font-size: 1.5rem;
        color: #666;
        cursor: pointer;
        padding: 5px;
        line-height: 1;
    }
}

/* Desktop: Hide toggle button, show panel */
@media (min-width: 769px) {
    .panel-close-button {
        display: none;
    }
    
    .panel-backdrop {
        display: none !important;
    }
}

/* ============================================
   STATS PANEL RESPONSIVE POSITIONING
   ============================================ */
.stats-panel {
    position: absolute;
    bottom: 20px;
    left: 50%;
    transform: translateX(-50%);
    background: white;
    padding: 15px 25px;
    border-radius: 8px;
    box-shadow: 0 4px 12px rgba(0,0,0,0.15);
    z-index: var(--z-stats);
    display: flex;
    gap: 30px;
    align-items: center;
}

@media (max-width: 768px) {
    .stats-panel {
        top: 80px;
        bottom: auto;
        left: 15px;
        right: 15px;
        transform: none;
        flex-direction: row;
        justify-content: space-around;
        gap: 15px;
        padding: 10px 15px;
        font-size: 0.9rem;
    }
    
    .stat-value {
        font-size: 1.2rem !important;
    }
    
    .stat-label {
        font-size: 0.75rem !important;
    }
}

/* ============================================
   HEADER RESPONSIVE ADJUSTMENTS
   ============================================ */
@media (max-width: 768px) {
    .header {
        padding: 0.75rem 1rem;
    }
    
    .header h1 {
        font-size: 1.1rem !important;
    }
    
    .back-button {
        font-size: 0.9rem;
    }
    
    .back-button i {
        margin-right: 4px !important;
    }
}

@media (max-width: 480px) {
    .header h1 {
        font-size: 1rem !important;
    }
    
    .back-button span {
        display: none;
    }
    
    .back-button i {
        margin-right: 0 !important;
    }
}

/* ============================================
   TOUCH TARGET OPTIMIZATION
   ============================================ */
@media (max-width: 768px) {
    /* Ensure minimum touch target size of 44x44px */
    button,
    .amenity-category,
    .category-header,
    calcite-button {
        min-height: 44px;
        min-width: 44px;
    }
    
    /* Increase padding for better touch targets */
    .amenity-category {
        padding: 14px !important;
    }
    
    .search-button {
        padding: 14px !important;
        font-size: 1.05rem !important;
    }
}

/* ============================================
   LANDSCAPE ORIENTATION HANDLING
   ============================================ */
@media (max-width: 768px) and (orientation: landscape) {
    .control-panel {
        max-height: 80vh !important;
    }
    
    .stats-panel {
        top: 70px;
        padding: 8px 12px;
    }
    
    .stat-value {
        font-size: 1rem !important;
    }
    
    .stat-label {
        font-size: 0.7rem !important;
    }
}

/* ============================================
   LOADING OVERLAY RESPONSIVE
   ============================================ */
.loading-overlay {
    position: fixed;
    top: 0;
    left: 0;
    right: 0;
    bottom: 0;
    background: rgba(255, 255, 255, 0.9);
    display: none;
    align-items: center;
    justify-content: center;
    z-index: 2000;
}

.loading-overlay.active {
    display: flex;
}

@media (max-width: 768px) {
    .loading-overlay {
        background: rgba(255, 255, 255, 0.95);
    }
}

/* ============================================
   LEGEND RESPONSIVE POSITIONING
   ============================================ */
.legend {
    position: absolute;
    top: 15px;
    right: 15px;
    background: white;
    padding: 10px;
    border-radius: 4px;
    box-shadow: 0 2px 8px rgba(0,0,0,0.2);
    z-index: var(--z-panel);
}

@media (max-width: 768px) {
    .legend {
        top: 80px;
        right: 10px;
        padding: 8px;
        font-size: 0.9rem;
        max-width: calc(100vw - 20px);
    }
    
    .legend h3 {
        font-size: 0.95rem !important;
    }
    
    .legend-item {
        margin-bottom: 6px !important;
    }
}

/* ============================================
   ARCGIS MAP CONTROLS RESPONSIVE
   ============================================ */
@media (max-width: 768px) {
    /* Adjust zoom controls position */
    arcgis-zoom {
        --calcite-ui-brand: var(--primary-color, #0078CA);
    }
    
    /* Make map controls more compact */
    .esri-ui-corner .esri-component {
        margin: 8px;
    }
    
    .esri-ui-top-left {
        top: 70px;
    }
    
    .esri-ui-top-right {
        top: 70px;
    }
}

/* ============================================
   POPUP RESPONSIVE ADJUSTMENTS
   ============================================ */
@media (max-width: 768px) {
    .esri-popup__main-container {
        max-width: calc(100vw - 40px) !important;
    }
    
    .esri-popup__content {
        max-height: 40vh !important;
    }
}

/* ============================================
   SCROLLBAR STYLING FOR MOBILE
   ============================================ */
@media (max-width: 768px) {
    .control-panel::-webkit-scrollbar {
        width: 6px;
    }
    
    .control-panel::-webkit-scrollbar-track {
        background: #f1f1f1;
        border-radius: 3px;
    }
    
    .control-panel::-webkit-scrollbar-thumb {
        background: #888;
        border-radius: 3px;
    }
    
    .control-panel::-webkit-scrollbar-thumb:hover {
        background: #555;
    }
}

/* ============================================
   UTILITY CLASSES
   ============================================ */
.mobile-only {
    display: none;
}

.desktop-only {
    display: block;
}

@media (max-width: 768px) {
    .mobile-only {
        display: block;
    }
    
    .desktop-only {
        display: none;
    }
}

/* ============================================
   DARK MODE SUPPORT
   ============================================ */
body.calcite-mode-dark .panel-toggle-button {
    background: #2a2a2a;
    color: #f5f5f5;
}

body.calcite-mode-dark .panel-toggle-button i {
    color: #0078CA;
}

body.calcite-mode-dark .stats-panel {
    background: #2a2a2a;
    color: #f5f5f5;
}

body.calcite-mode-dark .legend {
    background: #2a2a2a;
    color: #f5f5f5;
}

body.calcite-mode-dark .panel-backdrop {
    background: rgba(0, 0, 0, 0.7);
}

/* ============================================
   TIME SLIDER RESPONSIVE PATTERN
   ============================================ */
.time-slider-container,
.time-slider-panel {
    position: absolute;
    bottom: 20px;
    left: 50%;
    transform: translateX(-50%);
    background: white;
    padding: 15px 20px;
    border-radius: 8px;
    box-shadow: 0 4px 12px rgba(0,0,0,0.15);
    z-index: var(--z-panel);
    min-width: 500px;
    max-width: 800px;
}

@media (max-width: 768px) {
    .time-slider-container,
    .time-slider-panel {
        bottom: 10px;
        left: 15px;
        right: 15px;
        transform: none;
        min-width: auto;
        max-width: none;
        width: calc(100% - 30px);
        padding: 12px 15px;
    }

    /* Compact time slider controls */
    #timeSlider {
        font-size: 0.9rem;
    }

    .esri-time-slider {
        font-size: 0.85rem !important;
    }

    .esri-time-slider__animation {
        padding: 8px !important;
    }

    .esri-time-slider__time-extent {
        font-size: 0.8rem !important;
    }
}

@media (max-width: 480px) {
    .time-slider-container,
    .time-slider-panel {
        left: 10px;
        right: 10px;
        bottom: 8px;
        width: calc(100% - 20px);
        padding: 10px 12px;
    }
}

/* Dark mode support for time slider */
body.calcite-mode-dark .time-slider-container,
body.calcite-mode-dark .time-slider-panel {
    background: #2a2a2a;
    color: #f5f5f5;
}

/* ============================================
   MULTIPLE PANEL STACKING PATTERN
   (For maps with 2-3 panels like timeline maps)
   ============================================ */

/* Timeline info panel */
.timeline-info-panel {
    position: absolute;
    top: 15px;
    left: 15px;
    background: white;
    padding: 15px;
    border-radius: 8px;
    box-shadow: 0 4px 12px rgba(0,0,0,0.15);
    z-index: var(--z-panel);
    max-width: 300px;
}

/* Legend panel */
.legend-panel {
    position: absolute;
    top: 15px;
    right: 15px;
    background: white;
    padding: 15px;
    border-radius: 8px;
    box-shadow: 0 4px 12px rgba(0,0,0,0.15);
    z-index: var(--z-panel);
    min-width: 250px;
}

@media (max-width: 768px) {
    /* Stack timeline info panel at top */
    .timeline-info-panel {
        top: 70px;  /* Below header */
        left: 10px;
        right: 10px;
        max-width: none;
        width: calc(100% - 20px);
        padding: 12px;
    }

    /* Move legend panel to avoid overlap */
    .legend-panel {
        top: auto;
        bottom: calc(var(--panel-mobile-height) + 80px);  /* Above time slider */
        right: 10px;
        left: auto;
        min-width: auto;
        max-width: calc(50% - 15px);
        padding: 10px;
        font-size: 0.9rem;
    }

    /* When control panel is active, hide legend to reduce clutter */
    .control-panel.active ~ .legend-panel {
        display: none;
    }

    /* Compact legend items */
    .legend-panel h3 {
        font-size: 0.95rem !important;
        margin-bottom: 8px !important;
    }

    .legend-item {
        font-size: 0.85rem !important;
        margin-bottom: 6px !important;
    }

    .legend-color {
        width: 16px !important;
        height: 16px !important;
        margin-right: 8px !important;
    }
}

@media (max-width: 480px) {
    .timeline-info-panel {
        top: 60px;
        padding: 10px;
        font-size: 0.9rem;
    }

    .legend-panel {
        max-width: calc(60% - 15px);
    }
}

/* Dark mode support */
body.calcite-mode-dark .timeline-info-panel,
body.calcite-mode-dark .legend-panel {
    background: #2a2a2a;
    color: #f5f5f5;
}

/* ============================================
   SIDEBAR TRANSFORMATION PATTERN
   (For demos using sidebar layout like power_plants_app)
   ============================================ */
.sidebar {
    position: relative;
    width: 350px;
    background: white;
    display: flex;
    flex-direction: column;
    box-shadow: 2px 0 8px rgba(0,0,0,0.1);
    z-index: var(--z-panel);
    overflow: hidden;
}

.sidebar-header {
    padding: 15px;
    background: var(--primary-color, #0078CA);
    color: white;
    font-weight: 600;
    font-size: 1.1rem;
}

.sidebar-content {
    flex: 1;
    overflow-y: auto;
    padding: 15px;
}

@media (max-width: 768px) {
    .sidebar {
        position: fixed !important;
        bottom: 0 !important;
        left: 0 !important;
        right: 0 !important;
        top: auto !important;
        width: 100% !important;
        height: var(--panel-mobile-height) !important;
        max-height: var(--panel-mobile-height) !important;
        border-radius: 16px 16px 0 0 !important;
        transform: translateY(100%);
        transition: transform 0.3s ease-in-out;
        box-shadow: 0 -4px 20px rgba(0,0,0,0.2) !important;
    }

    .sidebar.active {
        transform: translateY(0);
    }

    .sidebar-header {
        padding: 12px 15px;
        font-size: 1rem;
    }

    .sidebar-content {
        padding: 12px 15px;
    }

    /* Add toggle button for sidebar on mobile */
    .sidebar-toggle-button {
        display: block;
        position: absolute;
        bottom: 20px;
        right: 20px;
        z-index: calc(var(--z-panel) + 1);
        background: white;
        border: none;
        border-radius: 50%;
        width: 56px;
        height: 56px;
        box-shadow: 0 4px 12px rgba(0,0,0,0.3);
        cursor: pointer;
        transition: all 0.3s ease;
    }

    .sidebar-toggle-button:hover {
        box-shadow: 0 6px 16px rgba(0,0,0,0.4);
        transform: scale(1.05);
    }

    .sidebar-toggle-button i {
        font-size: 1.3rem;
        color: var(--primary-color, #0078CA);
    }
}

@media (min-width: 769px) {
    .sidebar-toggle-button {
        display: none;
    }
}

/* Dark mode support for sidebar */
body.calcite-mode-dark .sidebar {
    background: #2a2a2a;
    color: #f5f5f5;
}

body.calcite-mode-dark .sidebar-toggle-button {
    background: #2a2a2a;
}

/* ============================================
   ARCGIS EXPAND WIDGET RESPONSIVE ADJUSTMENTS
   ============================================ */
@media (max-width: 768px) {
    /* Adjust Expand widget positioning */
    .esri-expand {
        margin: 8px !important;
    }

    .esri-expand__container {
        max-width: calc(100vw - 80px) !important;
        max-height: 50vh !important;
    }

    .esri-expand__content {
        max-height: 45vh !important;
        overflow-y: auto !important;
    }

    /* LayerList widget mobile optimization */
    .esri-layer-list {
        max-height: 40vh !important;
        font-size: 0.9rem !important;
    }

    .esri-layer-list__item {
        padding: 10px 8px !important;
    }

    .esri-layer-list__item-title {
        font-size: 0.9rem !important;
    }

    .esri-layer-list__item-toggle {
        min-width: 40px !important;
        min-height: 40px !important;
    }

    /* Legend widget mobile optimization */
    .esri-legend {
        max-height: 40vh !important;
        font-size: 0.9rem !important;
    }

    .esri-legend__layer {
        padding: 8px !important;
    }

    .esri-legend__layer-caption {
        font-size: 0.85rem !important;
    }

    .esri-legend__service {
        padding: 6px !important;
    }

    /* Basemap Gallery mobile optimization */
    .esri-basemap-gallery {
        max-height: 45vh !important;
    }

    .esri-basemap-gallery__item {
        width: calc(50% - 4px) !important;
    }

    /* Search widget mobile optimization */
    .esri-search {
        width: calc(100vw - 100px) !important;
    }

    .esri-search__input {
        font-size: 0.95rem !important;
    }
}

@media (max-width: 480px) {
    .esri-expand__container {
        max-width: calc(100vw - 60px) !important;
    }

    .esri-layer-list,
    .esri-legend,
    .esri-expand__content {
        max-height: 35vh !important;
    }

    .esri-basemap-gallery__item {
        width: 100% !important;
    }
}

/* ============================================
   CALCITE COMPONENT RESPONSIVE OVERRIDES
   ============================================ */
@media (max-width: 768px) {
    /* Calcite button responsive sizing */
    calcite-button {
        --calcite-font-size--1: 0.9rem;
    }

    calcite-button[scale="l"] {
        --calcite-font-size-0: 0.95rem;
    }

    /* Calcite panel responsive behavior */
    calcite-panel {
        max-height: 50vh;
    }

    /* Calcite action responsive sizing */
    calcite-action {
        min-width: 44px !important;
        min-height: 44px !important;
    }

    /* Calcite input responsive sizing */
    calcite-input,
    calcite-input-text,
    calcite-select {
        font-size: 16px !important; /* Prevents zoom on iOS */
    }
}
