/* ===== RESPONSIVE STYLES ===== */

/* Footer Mobile - kompakter aber lesbar */
@media (max-width: 768px) {
    .app-footer {
        padding: 3px 10px;
        font-size: 10px;
        min-height: auto;
    }

    .footer-content {
        gap: 10px;
    }

    .footer-links a {
        padding: 2px 5px;
        font-size: 10px;
    }

    .footer-separator {
        font-size: 10px;
    }

    /* Logout-Bereich auf Mobile */
    .logout-area {
        position: static;
        margin-left: auto;
    }

    .logout-area .user-name {
        display: none; /* Benutzername auf Mobile ausblenden */
    }

    .logout-area .logout-link {
        font-size: 10px;
    }
}

/* Tablet Portrait und kleiner */
@media (max-width: 768px) {
    .sidebar {
        position: fixed !important; /* Fixed statt absolute */
        top: 50px !important; /* Unter dem App-Header */
        left: 0 !important;
        width: 300px !important;
        height: calc(100vh - 50px) !important;
        z-index: 999 !important;
        background: white !important;
        box-shadow: 2px 0 10px rgba(0,0,0,0.3) !important;
        display: flex !important;
        flex-direction: column !important;
    }

    /* Sidebar mit Iconbar: neben der Iconbar positionieren */
    .sidebar.has-iconbar {
        left: var(--iconbar-width, 48px) !important;
        width: calc(300px - var(--iconbar-width, 48px)) !important;
    }

    .sidebar.closed {
        transform: translateX(-100%) !important; /* Transform statt margin */
    }

    .sidebar.has-iconbar.closed {
        transform: translateX(calc(-100% - var(--iconbar-width, 48px))) !important;
    }

    /* Sidebar Header - Desktop-Styling beibehalten */
    .sidebar-header {
        display: flex !important;
        visibility: visible !important;
        opacity: 1 !important;
        /* Desktop-Styling beibehalten */
        background: var(--primary-color) !important;
        border-bottom: none !important;
        padding: 10px 15px !important;
        flex-shrink: 0 !important;
        width: 100% !important;
        box-sizing: border-box !important;
        justify-content: space-between !important;
        align-items: center !important;
    }

    .sidebar-header h2 {
        display: block !important;
        font-size: 14px !important;
        color: #fff !important; /* Weiß wie Desktop */
        font-weight: 600 !important;
        margin: 0 !important;
        line-height: 26px !important;
    }

    /* Panel Header Actions - Desktop-Styling beibehalten */
    .panel-header-actions {
        display: flex !important;
        gap: 6px !important;
    }

    /* Alle Buttons im Header - Desktop-Styling beibehalten */
    .search-toggle-btn,
    .sidebar-close-btn {
        width: 32px !important;
        height: 32px !important;
        font-size: 16px !important;
        background: rgba(255, 255, 255, 0.15) !important;
        border: none !important;
        color: #fff !important;
        cursor: pointer !important;
        display: flex !important;
        align-items: center !important;
        justify-content: center !important;
        border-radius: 4px !important;
    }

    .add-wms-btn {
        height: 32px !important;
        font-size: 16px !important;
        background: rgba(255, 255, 255, 0.15) !important;
        border: none !important;
        color: #fff !important;
        cursor: pointer !important;
        display: flex !important;
        align-items: center !important;
        justify-content: center !important;
        border-radius: 4px !important;
        padding: 0 8px 0 6px !important;
        gap: 4px !important;
    }

    .search-toggle-btn:hover,
    .add-wms-btn:hover,
    .sidebar-close-btn:hover {
        background: rgba(255, 255, 255, 0.3) !important;
    }

    .search-toggle-btn.active {
        background: rgba(255, 255, 255, 0.4) !important;
    }

    /* Layer Search Container auf Mobile - immer sichtbar */
    #layer-search-container {
        display: flex !important;
        align-items: center !important;
        gap: 2px !important;
        position: relative;
        margin: 0;
        padding: 8px 10px;
        background: #f5f5f5;
        border-bottom: 1px solid #e0e0e0;
        border-radius: 0;
        flex-shrink: 0;
        width: 100%;
        box-sizing: border-box;
    }

    /* Layer Search auf Mobile */
    .layer-search {
        width: 100% !important;
        font-size: 16px !important; /* Größer für Mobile - verhindert Zoom auf iOS */
        padding: 10px 35px 10px 12px !important;
        border: 1px solid #ddd !important;
        border-radius: 4px !important;
        box-sizing: border-box !important;
    }

    .search-clear-btn {
        position: absolute !important;
        right: 14px !important;
        top: 50% !important;
        transform: translateY(-50%) !important;
        padding: 8px !important;
        font-size: 16px !important;
        background: none !important;
        border: none !important;
        color: #999 !important;
        cursor: pointer !important;
    }

    .search-clear-btn.visible {
        display: block !important;
    }

    /* Baum auf-/zuklappen: nur Desktop. Auf Mobil wird das Panel ohnehin als
       Bottom-Sheet bedient — die beiden Icons wuerden im schmalen Suchfeld nur
       Platz kosten. */
    .tree-tools-btn { display: none !important; }

    /* Sidebar Content */
    .sidebar-content {
        display: block !important;
        flex: 1 !important;
        overflow-y: auto !important;
        padding: 8px !important;
    }

    /* Resize-Handle auf Mobile deaktivieren */
    .sidebar-resize-handle {
        display: none;
    }

    /* Layer-Baum: Desktop-Styling beibehalten, keine Größenänderungen */

    #map-container {
        width: 100%;
    }

    .map-controls {
        bottom: 70px !important; /* Deutlich über dem Footer */
        right: 10px;
        gap: 8px;
        z-index: 1500 !important;
        align-items: flex-end !important; /* Scalebar auf Höhe des untersten Buttons */
    }

    /* Einheitlicher Abstand zwischen allen Buttons */
    .controls-group {
        gap: 4px !important;
    }

    /* KEINE Button-Größen-Änderungen - Desktop-Größen beibehalten */

    .scale-selector {
        padding: 3px 5px;
    }

    .scale-dropdown {
        font-size: 11px;
        min-width: 85px;
    }

    .scale-line-container {
        font-size: 10px;
        padding: 4px 8px;
    }

    .scale-bar {
        padding: 6px 8px;
        min-width: 100px;
    }

    .scale-bar-text {
        font-size: 9px;
    }

    .controls-group {
        padding: 6px;
        gap: 4px;
    }

    /* KEINE Control-Icon-Größen-Änderungen - Desktop-Größen beibehalten */

    .ol-attribution {
        max-width: calc(100% - 200px);
        font-size: 10px;
    }

    /* Data Copyright auf Tablet */
    #dataCopyright {
        right: 10px;
        bottom: 32px;
        max-width: calc(100vw - 120px);
        font-size: 9px;
    }

    /* Measurement Controls auf Mobile - 3 Buttons nebeneinander */
    .measurement-controls {
        flex-wrap: nowrap !important;
    }

    .measurement-btn {
        font-size: 11px !important;
        padding: 6px 8px !important;
    }

    .measurement-btn span {
        display: inline !important;
    }
}

/* Mobile Portrait */
@media (max-width: 480px) {
    .sidebar {
        width: 85% !important; /* 85% der Bildschirmbreite */
        height: calc(100vh - 50px) !important;
        top: 50px !important;
        position: fixed !important;
        left: 0 !important;
    }

    .sidebar.has-iconbar {
        left: var(--iconbar-width, 42px) !important;
        width: calc(85vw - var(--iconbar-width, 42px)) !important;
    }

    .sidebar.closed {
        transform: translateX(-100%) !important; /* Vollständig verstecken */
    }

    .sidebar.has-iconbar.closed {
        transform: translateX(calc(-100% - var(--iconbar-width, 42px))) !important;
    }
    
    #header h1 {
        font-size: 16px;
    }
    
    /* NETGIS-Style Controls für Mobile - Nur Position ändern */
    .map-controls {
        position: absolute !important;
        bottom: 70px !important; /* Deutlich über dem Footer */
        right: 5px !important;
        gap: 8px !important;
        z-index: 1500 !important;
        align-items: flex-end !important; /* Scalebar auf Höhe des untersten Buttons */
    }

    /* Einheitlicher Abstand zwischen allen Buttons */
    .controls-group {
        gap: 4px !important;
    }

    /* KEINE Button- oder Icon-Größen-Änderungen - Desktop beibehalten */

    /* Data Copyright auf kleinen Bildschirmen ausblenden */
    #dataCopyright {
        display: none;
    }

    /* Scale Container NICHT skalieren - Desktop-Größe beibehalten */
    .scale-container {
        /* transform entfernt - normale Größe */
        margin-bottom: 4mm !important; /* Nur Abstand anpassen */
    }
    
    /* Sidebar Header - Desktop-Styling beibehalten */
    .sidebar-header {
        display: flex !important;
        visibility: visible !important;
        opacity: 1 !important;
        background: var(--primary-color) !important;
        border-bottom: none !important;
        padding: 10px 15px !important;
        flex-shrink: 0 !important;
        width: 100% !important;
        box-sizing: border-box !important;
        justify-content: space-between !important;
        align-items: center !important;
    }

    .sidebar-header h2 {
        display: block !important;
        font-size: 14px !important;
        color: #fff !important; /* Weiß wie Desktop */
        font-weight: 600 !important;
        margin: 0 !important;
        padding: 0 !important;
        line-height: 26px !important;
        visibility: visible !important;
        opacity: 1 !important;
    }

    /* Panel Header Actions - Desktop-Styling beibehalten */
    .panel-header-actions {
        display: flex !important;
        gap: 6px !important;
    }

    /* Alle Buttons im Header - Desktop-Styling beibehalten */
    .search-toggle-btn,
    .sidebar-close-btn {
        width: 32px !important;
        height: 32px !important;
        font-size: 16px !important;
        background: rgba(255, 255, 255, 0.15) !important;
        border: none !important;
        color: #fff !important;
        cursor: pointer !important;
        display: flex !important;
        align-items: center !important;
        justify-content: center !important;
        border-radius: 4px !important;
    }

    .add-wms-btn {
        height: 32px !important;
        font-size: 16px !important;
        background: rgba(255, 255, 255, 0.15) !important;
        border: none !important;
        color: #fff !important;
        cursor: pointer !important;
        display: flex !important;
        align-items: center !important;
        justify-content: center !important;
        border-radius: 4px !important;
        padding: 0 8px 0 6px !important;
        gap: 4px !important;
    }

    /* Layer Search Container auf Mobile - immer sichtbar */
    #layer-search-container {
        display: flex !important;
        align-items: center !important;
        gap: 2px !important;
    }

    /* Layer Search Input auf Mobile */
    .layer-search {
        width: 100% !important;
        font-size: 16px !important; /* Verhindert Zoom auf iOS */
        padding: 10px 35px 10px 12px !important;
        border: 1px solid #ccc !important;
        border-radius: 4px !important;
        box-sizing: border-box !important;
    }

    .search-clear-btn {
        position: absolute !important;
        right: 14px !important;
        top: 50% !important;
        transform: translateY(-50%) !important;
        padding: 8px !important;
        font-size: 16px !important;
    }

    /* Sidebar Content */
    .sidebar-content {
        display: block !important;
        flex: 1 !important;
        overflow-y: auto !important;
        padding: 8px !important;
        margin: 0 !important;
    }
}

/* Sehr kleine Bildschirme */
@media (max-width: 320px) {
    .header-content {
        padding: 0 10px;
    }
    
    .menu-btn {
        margin-right: 10px;
    }
    
    #header h1 {
        font-size: 14px;
    }
    
    /* NETGIS-Style für sehr kleine Bildschirme - Nur Position */
    .map-controls {
        bottom: 70px !important; /* Deutlich über dem Footer */
        right: 3px !important;
        gap: 8px !important;
        z-index: 1500 !important;
        align-items: flex-end !important; /* Scalebar auf Höhe des untersten Buttons */
    }

    /* Einheitlicher Abstand zwischen allen Buttons */
    .controls-group {
        gap: 4px !important;
    }

    /* KEINE Button- oder Icon-Größen-Änderungen */

    /* Scale NICHT verkleinern - Desktop-Größe beibehalten */
    .scale-container {
        /* transform entfernt - normale Größe */
        margin-bottom: 5mm !important;
    }
}

/* Landscape Phones */
@media (max-height: 500px) and (orientation: landscape) {
    .sidebar {
        width: 280px !important; /* Etwas schmaler im Landscape */
        height: calc(100vh - 50px) !important;
        top: 50px !important;
    }

    .sidebar.has-iconbar {
        left: var(--iconbar-width, 48px) !important;
        width: calc(280px - var(--iconbar-width, 48px)) !important;
    }

    .sidebar.closed {
        transform: translateX(-280px) !important;
    }

    .sidebar.has-iconbar.closed {
        transform: translateX(calc(-100% - var(--iconbar-width, 48px))) !important;
    }
    
    /* NETGIS-Style Controls für Landscape - Nur Position */
    .map-controls {
        bottom: 50px !important; /* Über dem Footer im Landscape */
        right: 4mm !important;
        gap: 8px !important;
        z-index: 1500 !important;
        align-items: flex-end !important; /* Scalebar auf Höhe des untersten Buttons */
    }

    /* Einheitlicher Abstand zwischen allen Buttons */
    .controls-group {
        gap: 4px !important;
    }

    /* KEINE Button-Größen-Änderungen */

    .scale-container {
        /* transform entfernt - Desktop-Größe beibehalten */
        margin-bottom: 3mm !important;
    }
}

/* Desktop - Sidebar Resize nur auf Desktop aktiv */
@media (min-width: 769px) {
    .sidebar-resize-handle {
        display: block;
    }
    
    /* Stelle sicher, dass CSS-Variablen respektiert werden */
    .sidebar {
        width: var(--sidebar-width, 320px);
        min-width: var(--sidebar-min-width, 280px);
        max-width: var(--sidebar-max-width, 500px);
        top: 0; /* Beginnt am oberen Rand von main-content */
        height: 100%; /* Vollständige Höhe von main-content */
        position: relative; /* Relative Positionierung auf Desktop */
    }

    /* Sidebar mit Iconbar: left nicht noetig, Iconbar ist Flex-Child */
    .sidebar.has-iconbar {
        left: auto;
    }

    .sidebar.closed {
        margin-left: calc(-1 * var(--sidebar-width, 320px));
    }

    .sidebar.has-iconbar.closed {
        transform: none;
        margin-left: calc(-1 * var(--sidebar-width, 320px));
    }
    
    /* Desktop Controls normal - über dem Footer und dataCopyright */
    .map-controls {
        position: absolute;
        bottom: 55px; /* Platz für Footer + dataCopyright */
        right: 15px;
        gap: 10px;
        z-index: 1500; /* Unter Footer (1600) aber über Karte */
    }
}

/* High DPI Displays */
@media (-webkit-min-device-pixel-ratio: 2), (min-resolution: 192dpi) {
    .layer-group .layer-group::before {
        width: 0.5px;
    }
    
    .sidebar {
        border-right-width: 0.5px;
    }
    
    .sidebar-header {
        border-bottom-width: 0.5px;
    }
    
    /* Schärfere Icons auf High-DPI */
    .folder-icon img,
    .layer-icon img {
        image-rendering: -webkit-optimize-contrast;
        image-rendering: crisp-edges;
    }
}

/* Touch Device Optimizations */
@media (hover: none) and (pointer: coarse) {
    /* Layer-Baum: Desktop-Styling beibehalten, keine Größenänderungen */

    /* Verbessertes Touch-Handling für Checkboxen/Radios */
    .group-checkbox,
    .layer-checkbox,
    .layer-radio {
        touch-action: manipulation;
        -webkit-tap-highlight-color: transparent;
        /* Größeres Touch-Target ohne visuelle Änderung */
        position: relative;
    }

    /* Größeres unsichtbares Touch-Target */
    .group-checkbox::before,
    .layer-checkbox::before,
    .layer-radio::before {
        content: '';
        position: absolute;
        top: -8px;
        left: -8px;
        right: -8px;
        bottom: -8px;
        /* Unsichtbarer Klick-Bereich */
    }

    /* Resize-Handle für Touch größer machen */
    .sidebar-resize-handle {
        width: 12px;
        right: -6px;
    }

    /* Touch-Geräte: Controls über dem Footer */
    .map-controls {
        bottom: 70px !important;
        z-index: 1500 !important;
        align-items: flex-end !important; /* Scalebar auf Höhe des untersten Buttons */
    }

    /* Einheitlicher Abstand zwischen allen Buttons */
    .controls-group {
        gap: 4px !important;
    }
}

/* Reduzierte Animation für bessere Performance auf mobilen Geräten */
@media (max-width: 768px) {
    .sidebar,
    .layer-group-toggle > i,
    .folder-icon,
    .layer-item,
    .layer-group-header {
        transition: none;
    }
}

/* Print Styles */
@media print {
    .sidebar,
    .sidebar-iconbar,
    .map-controls,
    .sidebar-resize-handle {
        display: none !important;
    }
    
    #map-container {
        width: 100% !important;
        height: 100vh !important;
    }
}

/* ADDITIONAL FIX: Browser-UI kompensieren */
@media (max-width: 480px) {
    /* Für Browser mit dynamischer UI (Chrome Mobile) */
    .map-controls {
        bottom: max(70px, calc(70px + env(safe-area-inset-bottom))) !important;
    }
}

/* iOS Safari spezifische Fixes - NUR für echte iPhones */
@supports (-webkit-touch-callout: none) {
    /* iPhone Safari Detection mit User-Agent Check im JavaScript */
    .ios-device .map-controls {
        bottom: 75px !important; /* Deutlich über dem Footer */
        z-index: 1500 !important;
    }

    /* Einheitlicher Abstand zwischen allen Buttons */
    .ios-device .controls-group {
        gap: 4px !important;
    }

    .ios-device .scale-container {
        margin-bottom: 4mm !important;
    }

    /* Sehr kleine iPhones (iPhone SE, Mini) */
    @media (max-width: 375px) {
        .ios-device .map-controls {
            bottom: 80px !important;
        }
    }
}

/* iPhone Landscape Modus - NUR für iOS */
@media (max-width: 896px) and (max-height: 414px) and (orientation: landscape) {
    .ios-device .map-controls {
        bottom: 50px !important;
        right: 4mm !important;
        z-index: 1500 !important;
    }

    /* Einheitlicher Abstand zwischen allen Buttons */
    .ios-device .controls-group {
        gap: 4px !important;
    }
}