/* Base and CSS Variables */
:root {
    --bg-surface: rgba(255, 255, 255, 0.88);
    --bg-card: rgba(255, 255, 255, 0.6);
    --text-main: #1a1a1a;
    --text-muted: #666666;
    --border-color: rgba(0, 0, 0, 0.1);
    --shadow-sm: 0 4px 6px rgba(0,0,0,0.05);
    --shadow-lg: 0 10px 25px rgba(0,0,0,0.12);
    --accent: #2563eb;
    --radius-lg: 12px;
    --radius-md: 8px;
    --blur: blur(14px);
    --transition: all 0.3s cubic-bezier(0.4, 0, 0.2, 1);
}

/* Dark mode overrides */
[data-theme="dark"] {
    --bg-surface: rgba(20, 22, 28, 0.92);
    --bg-card: rgba(255, 255, 255, 0.06);
    --text-main: #e8eaf0;
    --text-muted: #8b92a5;
    --border-color: rgba(255, 255, 255, 0.1);
    --shadow-lg: 0 10px 30px rgba(0,0,0,0.4);
    --accent: #3b82f6;
}

body, html {
    margin: 0;
    padding: 0;
    width: 100vw;
    height: 100vh;
    font-family: 'Inter', -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Helvetica, Arial, sans-serif;
    background: #1a1c20;
    overflow: hidden;
    color: var(--text-main);
}

/* Glassmorphism Panel Base */
.glass-panel {
    background: var(--bg-surface);
    backdrop-filter: var(--blur);
    -webkit-backdrop-filter: var(--blur);
    border: 1px solid var(--border-color);
    box-shadow: var(--shadow-lg);
    border-radius: var(--radius-lg);
    z-index: 10;
}

/*  Header  */
.app-header {
    position: absolute;
    top: 20px;
    left: 20px;
    padding: 0;
    display: flex;
    align-items: stretch;
    gap: 0;
    z-index: 20;
}

.app-header .panel-header {
    border-bottom: none;
    padding: 8px 12px;
    min-height: unset;
}

.app-header .panel-body {
    display: flex;
    align-items: center;
    gap: 12px;
    padding: 8px 14px;
    flex: 1;
}

.app-header .loading-spinner {
    display: none;
}

.app-header .loading-badge {
    margin: 0;
    padding: 3px 10px;
    font-size: 0.7rem;
}

.app-header .basemap-toggle select {
    padding: 4px 8px;
    font-size: 0.8rem;
}

.drag-handle-header {
    display: flex;
    align-items: center;
    gap: 8px;
    flex-shrink: 0;
}

.app-header h1 {
    margin: 0;
    font-size: 1.15rem;
    font-weight: 700;
    letter-spacing: -0.5px;
}

.loading-spinner {
    width: 18px;
    height: 18px;
    border: 2px solid var(--border-color);
    border-top-color: var(--accent);
    border-radius: 50%;
    animation: spin 1s linear infinite;
    opacity: 0;
    transition: opacity 0.2s ease;
}

.loading-spinner.active { opacity: 1; }

@keyframes spin { to { transform: rotate(360deg); } }

/* Loading Badge */
.loading-badge {
    display: flex;
    align-items: center;
    gap: 8px;
    background: rgba(var(--accent-rgb, 37, 99, 235), 0.15);
    border: 1px solid var(--accent);
    padding: 4px 12px;
    border-radius: 20px;
    font-size: 0.75rem;
    font-weight: 600;
    color: var(--accent);
    animation: pulse 1.5s infinite;
    backdrop-filter: var(--blur);
}

@keyframes pulse {
    0% { opacity: 0.7; transform: scale(0.98); }
    50% { opacity: 1; transform: scale(1); }
    100% { opacity: 0.7; transform: scale(0.98); }
}

.mini-spinner {
    width: 12px;
    height: 12px;
    border: 2px solid var(--accent);
    border-top-color: transparent;
    border-radius: 50%;
    animation: spin 0.8s linear infinite;
}

.basemap-toggle select {
    padding: 5px 10px;
    border-radius: var(--radius-md);
    border: 1px solid var(--border-color);
    background: var(--bg-card);
    color: var(--text-main);
    font-family: inherit;
    font-size: 0.85rem;
    cursor: pointer;
    outline: none;
}

/* Dark mode toggle button */
.dark-mode-btn {
    background: var(--bg-card);
    border: 1px solid var(--border-color);
    border-radius: var(--radius-md);
    color: var(--text-main);
    cursor: pointer;
    font-size: 1rem;
    padding: 4px 8px;
    line-height: 1;
    transition: background 0.2s;
}
.dark-mode-btn:hover { background: var(--border-color); }

.terrain-3d-btn {
    background: var(--bg-card);
    border: 1px solid var(--border-color);
    border-radius: var(--radius-md);
    color: var(--text-main);
    cursor: pointer;
    font-size: 0.75rem;
    font-weight: 700;
    padding: 5px 10px;
    line-height: 1;
    transition: all 0.2s;
}
.terrain-3d-btn:hover { background: var(--border-color); }
.terrain-3d-btn.active {
    background: var(--accent);
    color: white;
    border-color: var(--accent);
}

/*  Legend Widget  */
.legend-widget {
    position: absolute;
    top: 120px;
    left: 20px;
    padding: 14px 16px;
    min-width: 190px;
    max-width: 280px;
}

.legend-title {
    font-size: 0.78rem;
    font-weight: 600;
    text-transform: uppercase;
    letter-spacing: 0.5px;
    color: var(--text-muted);
    margin-bottom: 10px;
}

.legend-item {
    display: flex;
    align-items: center;
    gap: 10px;
    margin-bottom: 6px;
    font-size: 0.82rem;
    color: var(--text-main);
}

.legend-item:last-child { margin-bottom: 0; }

.color-swatch {
    width: 14px;
    height: 14px;
    border-radius: 3px;
    flex-shrink: 0;
    border: 1px solid rgba(0,0,0,0.12);
}

/*  Layer Controls Widget  */
.layer-controls-widget {
    position: absolute;
    top: 330px;
    left: 20px;
    padding: 10px 12px;
    min-width: 210px;
    max-width: 270px;
}

/* Each layer group gets a top border only when expanded (via CSS :has or Alpine class) */
.layer-group {
    padding-top: 7px;
    margin-top: 7px;
}

/* Top border only shown when the group is not the first */
.layer-group + .layer-group {
    border-top: 1px solid var(--border-color);
}

.layer-group-label { margin-bottom: 5px; }

.lc-toggle {
    display: flex;
    align-items: center;
    gap: 7px;
    font-size: 0.82rem;
    font-weight: 600;
    color: var(--text-main);
    cursor: pointer;
}

.lc-toggle input[type=checkbox] {
    width: 13px;
    height: 13px;
    cursor: pointer;
    accent-color: var(--accent);
}

.lc-row {
    display: flex;
    align-items: center;
    gap: 6px;
    margin-top: 4px;
}

.lc-label {
    font-size: 0.73rem;
    color: var(--text-muted);
    min-width: 66px;
}

.lc-slider {
    flex: 1;
    height: 4px;
    accent-color: var(--accent);
    cursor: pointer;
}

.lc-val {
    font-size: 0.73rem;
    color: var(--text-muted);
    min-width: 28px;
    text-align: right;
}

.color-presets { display: flex; gap: 5px; align-items: center; }

.color-preset {
    width: 16px;
    height: 16px;
    border-radius: 50%;
    border: 2px solid transparent;
    cursor: pointer;
    padding: 0;
    outline: none;
    transition: border-color 0.15s, transform 0.1s;
}

.color-preset:hover { transform: scale(1.2); }
.color-preset.active {
    border-color: var(--accent);
    box-shadow: 0 0 0 1px rgba(255,255,255,0.5);
}

/*  Shared drag handle  */
.drag-handle {
    display: flex;
    align-items: center;
    cursor: grab;
    user-select: none;
    padding-bottom: 7px;
    margin-bottom: 5px;
    border-bottom: 1px solid var(--border-color);
    gap: 6px;
    padding: 6px 0;
    min-height: 32px;
    transition: background 0.15s ease;
}

.drag-handle:hover {
    background: rgba(255, 255, 255, 0.03);
    border-radius: 4px;
}

.drag-handle:active {
    cursor: grabbing;
    background: rgba(255, 255, 255, 0.06);
}

.drag-dots {
    opacity: 0.6;
    font-size: 0.85rem;
    font-weight: bold;
    letter-spacing: -0.5px;
    transition: opacity 0.15s ease;
}

.drag-handle:hover .drag-dots {
    opacity: 0.85;
}

/*  Map Info Widget  */
.map-info-widget {
    position: absolute;
    bottom: 30px;
    right: 20px;
    padding: 9px 12px;
    font-size: 0.83rem;
    color: var(--text-main);
    min-width: 190px;
}

.widget-drag-row {
    display: flex;
    align-items: center;
    gap: 8px;
    border-bottom: none;
    padding-bottom: 0;
    margin-bottom: 0;
}

.scale-snap-row {
    display: flex;
    align-items: center;
    gap: 6px;
    margin-top: 7px;
    border-top: 1px solid var(--border-color);
    padding-top: 6px;
}

.scale-select {
    flex: 1;
    font-size: 0.73rem;
    padding: 3px 6px;
    border-radius: var(--radius-md);
    border: 1px solid var(--border-color);
    background: var(--bg-card);
    color: var(--text-main);
    cursor: pointer;
}

.map-info-widget .divider { color: var(--text-muted); }

/* Search widget */
.search-widget {
    position: absolute;
    top: 20px;
    left: 50%;
    transform: translateX(-50%);
    padding: 10px 16px;
    min-width: 350px;
    max-width: 550px;
    z-index: 20;
}

.search-input {
    flex: 1;
    font-size: 0.95rem;
    padding: 8px 12px;
    border-radius: var(--radius-md);
    border: 1px solid var(--border-color);
    background: var(--bg-card);
    color: var(--text-main);
    outline: none;
}

.search-input:focus {
    border-color: var(--accent, #4a9eff);
}

.search-clear-btn {
    background: none;
    border: none;
    cursor: pointer;
    color: var(--text-muted);
    font-size: 0.8rem;
    padding: 0 2px;
}

.search-status {
    font-size: 0.7rem;
    color: var(--text-muted);
    margin-top: 4px;
}

.search-results {
    margin-top: 4px;
    display: flex;
    flex-direction: column;
    gap: 2px;
}

.search-result-item {
    font-size: 0.78rem;
    padding: 5px 8px;
    border-radius: var(--radius-md);
    cursor: pointer;
    color: var(--text-main);
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
}

.search-result-item:hover {
    background: var(--bg-hover, rgba(255, 255, 255, 0.12));
}

/* Map info widget extras */
.copy-btn {
    background: none;
    border: none;
    cursor: pointer;
    color: var(--text-muted);
    font-size: 0.75rem;
    padding: 0 2px;
}

.copy-btn:hover {
    color: var(--text-main);
}

.bbox-toggle-btn {
    background: none;
    border: none;
    cursor: pointer;
    color: var(--text-muted);
    font-size: 0.7rem;
}

.bbox-detail {
    font-size: 0.7rem;
    font-family: monospace;
    color: var(--text-muted);
    padding-top: 4px;
    display: flex;
    flex-direction: column;
    gap: 1px;
}

.bbox-copy-row {
    display: flex;
    gap: 4px;
    margin-top: 4px;
}

.bbox-copy-row .copy-btn {
    flex: 1;
    padding: 2px 4px;
    font-size: 0.65rem;
}

/*  Feature Details Widget (floating, draggable)  */
.feature-sidebar {
    position: fixed;
    top: 20px;
    right: 20px;
    width: 360px;
    max-height: calc(100vh - 80px);
    padding: 0;
    display: flex;
    flex-direction: column;
    overflow: hidden;
    opacity: 0;
    pointer-events: none;
    transform: translateY(-6px);
    transition: opacity 0.25s ease, transform 0.25s ease;
}

.feature-sidebar.open {
    opacity: 1;
    pointer-events: all;
    transform: translateY(0);
}

.sidebar-drag-handle {
    display: flex;
    align-items: center;
    gap: 8px;
    padding: 12px 14px 10px;
    border-bottom: 1px solid var(--border-color);
    cursor: grab;
    user-select: none;
    flex-shrink: 0;
    min-height: 40px;
    transition: background 0.15s ease;
}

.sidebar-drag-handle:hover {
    background: rgba(255, 255, 255, 0.03);
}

.sidebar-drag-handle:active {
    cursor: grabbing;
    background: rgba(255, 255, 255, 0.06);
}

.sidebar-drag-title {
    font-size: 0.78rem;
    font-weight: 600;
    text-transform: uppercase;
    letter-spacing: 0.4px;
    color: var(--text-muted);
    flex: 1;
}

.sidebar-close {
    background: none;
    border: none;
    font-size: 1.2rem;
    cursor: pointer;
    color: var(--text-muted);
    padding: 0 2px;
    border-radius: var(--radius-md);
    line-height: 1;
    transition: color 0.15s;
}

.sidebar-close:hover { color: var(--text-main); }

.sidebar-scroll {
    overflow-y: auto;
    flex: 1;
    padding: 14px 16px;
}

.sidebar-header {
    margin-bottom: 10px;
    padding-right: 4px;
}

.sidebar-header h2 {
    margin: 0 0 4px 0;
    font-size: 1.05rem;
    font-weight: 700;
    color: var(--text-main);
    line-height: 1.3;
}

/*  Badges  */
.mu-meta { display: flex; flex-wrap: wrap; gap: 5px; margin: 5px 0; }

.badge {
    font-size: 0.75rem;
    font-weight: 700;
    padding: 2px 7px;
    border-radius: 10px;
    background: var(--accent);
    color: white;
    font-family: monospace;
}

.badge-muted {
    background: var(--bg-card);
    border: 1px solid var(--border-color);
    color: var(--text-muted);
    font-weight: 600;
}

.badge-compkind {
    display: inline-block;
    text-decoration: none;
    cursor: pointer;
    transition: opacity 0.2s;
}

.badge-compkind:hover {
    opacity: 0.85;
    text-decoration: underline;
}

.mu-farmland {
    font-size: 0.78rem;
    color: var(--text-muted);
    margin-top: 3px;
    font-style: italic;
}

/*  Component Cards  */
.component-card {
    background: var(--bg-card);
    border: 1px solid var(--border-color);
    border-radius: var(--radius-md);
    padding: 9px 11px;
    margin-bottom: 7px;
}

.component-header {
    display: flex;
    justify-content: space-between;
    align-items: center;
    margin-bottom: 5px;
    border-bottom: 1px solid var(--border-color);
    padding-bottom: 4px;
}

.component-name { font-weight: 600; font-size: 0.88rem; color: var(--text-main); }

.component-pct {
    background: var(--accent);
    color: white;
    padding: 1px 6px;
    border-radius: 10px;
    font-size: 0.72rem;
    font-weight: 600;
}

.component-meta {
    display: flex;
    gap: 7px;
    font-size: 0.72rem;
    color: var(--text-muted);
    margin-bottom: 5px;
    flex-wrap: wrap;
}

.taxon { font-style: italic; }

.component-props {
    margin-bottom: 5px;
    border-top: 1px solid var(--border-color);
    padding-top: 4px;
}

.prop-row {
    display: flex;
    justify-content: space-between;
    font-size: 0.73rem;
    padding: 2px 0;
    color: var(--text-main);
}

.prop-label {
    color: var(--text-muted);
    font-weight: 500;
    min-width: 80px;
}

.component-link {
    color: var(--accent);
    text-decoration: none;
    font-weight: 500;
    border-bottom: 1px solid transparent;
    transition: border-bottom-color 0.2s;
}

.component-link:hover {
    border-bottom-color: var(--accent);
}

/* ESD Panel Tab Styling */
.esd-tab-button {
    background: none;
    border: none;
    border-bottom: 2px solid transparent;
    color: var(--text-muted);
    cursor: pointer;
    padding: 8px 4px;
    font-size: 0.7rem;
    font-weight: 600;
    text-transform: uppercase;
    letter-spacing: 0.3px;
    transition: var(--transition);
}

.esd-tab-button:hover {
    color: var(--text-main);
}

.esd-tab-button.active {
    color: var(--accent);
    border-bottom-color: var(--accent);
}

.esd-tab-content {
    animation: fadeIn 0.2s ease-in;
}

@keyframes fadeIn {
    from { opacity: 0; }
    to { opacity: 1; }
}

.key-criteria-list {
    list-style-type: none;
    padding-left: 0;
}

.key-criteria-list li {
    padding-left: 12px;
    position: relative;
}

.key-criteria-list li::before {
    content: "•";
    position: absolute;
    left: 0;
    color: var(--accent);
}

.species-table {
    width: 100%;
    border-collapse: collapse;
    font-size: 0.7rem;
}

.species-table th {
    text-align: left;
    font-weight: 600;
    color: var(--text-muted);
    padding: 4px 2px;
    border-bottom: 1px solid var(--border-color);
    font-size: 0.65rem;
    text-transform: uppercase;
    letter-spacing: 0.2px;
}

.species-table td {
    padding: 4px 2px;
    border-bottom: 1px solid rgba(0, 0, 0, 0.05);
    color: var(--text-main);
}

.stm-image-container {
    max-width: 100%;
    border: 1px solid var(--border-color);
    border-radius: var(--radius-md);
    overflow: hidden;
    margin-bottom: 12px;
}

.stm-image-container img {
    width: 100%;
    height: auto;
    display: block;
}

.stm-image-container figcaption {
    font-size: 0.65rem;
    color: var(--text-muted);
    padding: 4px 8px;
    background: var(--bg-card);
    border-top: 1px solid var(--border-color);
}

/*  Horizon Table  */
.hz-table { width: 100%; border-collapse: collapse; font-size: 0.68rem; }

.hz-table th {
    text-align: right;
    font-weight: 600;
    color: var(--text-muted);
    padding: 2px 4px;
    border-bottom: 1px solid var(--border-color);
    font-size: 0.62rem;
    text-transform: uppercase;
    letter-spacing: 0.3px;
}

.hz-table th:first-child { text-align: left; }

.hz-table td {
    padding: 2px 4px;
    text-align: right;
    border-bottom: 1px solid rgba(0,0,0,0.04);
    font-variant-numeric: tabular-nums;
    color: var(--text-main);
}

.hz-table td:first-child { text-align: left; font-weight: 600; }
.hz-table tr:last-child td { border-bottom: none; }

/*  Widget header (shared collapse handle)  */
.widget-header {
    display: flex;
    align-items: center;
    gap: 6px;
    padding-bottom: 6px;
    margin-bottom: 4px;
}

/* Only show divider when content is expanded */
.widget-header.with-divider {
    border-bottom: 1px solid var(--border-color);
}

/*  Empty State  */
.empty-state {
    color: var(--text-muted);
    text-align: center;
    margin-top: 30px;
    font-size: 0.85rem;
}

.sa-label-marker {
    color: #ffff00;
    font-family: 'Open Sans Bold', 'Inter', sans-serif;
    font-weight: 800;
    font-size: 14px;
    text-shadow: 0 0 4px #000, 0 0 2px #000;
    pointer-events: none;
    user-select: none;
    white-space: nowrap;
    z-index: 1000;
}

/*  MapLibre Controls Override  */
.maplibregl-ctrl-top-right {
    top: 20px !important;
    right: 400px !important;
}

/* Profile mode buttons */
.profile-mode-btn {
    font-size: 0.7rem;
    padding: 4px 10px;
    border: 1px solid var(--border-color);
    border-radius: 10px;
    background: var(--bg-card);
    color: var(--text-muted);
    cursor: pointer;
    transition: all 0.2s ease;
    touch-action: manipulation;
}

.profile-mode-btn:hover {
    border-color: var(--accent);
    color: var(--accent);
}

.profile-mode-btn:active {
    transform: scale(0.98);
}

.profile-mode-btn.active {
    background: var(--accent);
    color: white;
    border-color: var(--accent);
}

/* Profile canvas containers - allow overflow with scroll */
[id^="profile-canvas-"],
[id="comparison-canvas"],
[id="series-profile-canvas"] {
    overflow-x: auto;
    -webkit-overflow-scrolling: touch;
    width: 100%;
    display: block;
    min-height: 300px;
}

/* Ensure SVG elements render properly */
[id^="profile-canvas-"] svg,
[id="comparison-canvas"] svg,
[id="series-profile-canvas"] svg {
    display: block;
    width: 100%;
    height: auto;
}

/* ===== RESPONSIVE MOBILE DESIGN ===== */

/* Tablets: 480px - 768px */
@media (max-width: 768px) {
    .feature-sidebar,
    #series-panel,
    #comparison-panel {
        width: calc(100vw - 40px) !important;
        max-height: calc(100vh - 120px) !important;
        right: 20px !important;
        left: auto !important;
    }

    #comparison-panel {
        right: 20px !important;
        top: auto !important;
        bottom: 20px !important;
    }

    .legend-widget,
    .layer-controls-widget {
        max-width: calc(100vw - 40px);
    }

    .app-header {
        left: 10px !important;
        right: 10px !important;
        width: auto;
        max-width: 100%;
    }

    .profile-mode-btn {
        font-size: 0.75rem;
        padding: 6px 12px;
        min-height: 36px;
    }

    .component-card {
        padding: 8px 10px;
    }

    .sidebar-scroll {
        padding: 10px 12px;
    }
}

/* Mobile: < 480px */
@media (max-width: 480px) {
    /* Full-width panels on mobile */
    .feature-sidebar,
    #series-panel,
    #comparison-panel {
        position: fixed !important;
        width: calc(100vw - 20px) !important;
        max-height: calc(100vh - 100px) !important;
        left: 10px !important;
        right: 10px !important;
        top: 60px !important;
        bottom: auto !important;
        z-index: 30;
    }

    #comparison-panel {
        top: auto !important;
        bottom: 10px !important;
        max-height: 45vh !important;
    }

    #series-panel {
        top: 60px !important;
        max-height: 50vh !important;
    }

    /* Increase touch targets to 44px minimum */
    .profile-mode-btn,
    button,
    input[type="checkbox"],
    input[type="range"],
    select {
        min-height: 44px;
        min-width: 44px;
        font-size: 16px;
        padding: 8px 12px;
    }

    .profile-mode-btn {
        flex: 1;
        font-size: 0.8rem;
        padding: 8px 6px;
        min-height: 40px;
    }

    .sidebar-close {
        min-height: 44px;
        min-width: 44px;
        display: flex;
        align-items: center;
        justify-content: center;
        font-size: 1.4rem;
    }

    .sidebar-drag-handle {
        padding: 12px 12px 10px;
        gap: 10px;
    }

    .drag-handle {
        padding: 10px 0;
        min-height: 44px;
        align-items: center;
    }

    .drag-dots {
        font-size: 0.9rem;
        opacity: 0.5;
    }

    /* Compact legend and layer controls */
    .legend-widget,
    .layer-controls-widget {
        width: auto;
        max-width: 90vw;
        padding: 10px 12px;
        left: 10px !important;
        top: auto;
        bottom: auto;
    }

    .legend-widget {
        bottom: 100px;
    }

    .layer-controls-widget {
        bottom: 200px;
    }

    .map-info-widget {
        right: 10px !important;
        bottom: 20px !important;
        font-size: 0.75rem;
        padding: 8px 10px;
    }

    .component-card {
        margin-bottom: 10px;
        padding: 8px;
    }

    .component-header {
        flex-direction: column;
        align-items: flex-start;
        gap: 4px;
    }

    .component-pct {
        align-self: flex-start;
    }

    .hz-table {
        font-size: 0.7rem;
    }

    .hz-table th,
    .hz-table td {
        padding: 4px 2px;
    }

    /* Sidebar scroll area margins */
    .sidebar-scroll {
        padding: 12px;
    }

    .sidebar-header {
        margin-bottom: 8px;
    }

    .sidebar-header h2 {
        font-size: 0.95rem;
        margin-bottom: 4px;
    }

    /* Reduce text size slightly for mobile */
    .badge {
        font-size: 0.65rem;
        padding: 1px 5px;
    }

    .component-name {
        font-size: 0.82rem;
    }

    .component-meta {
        gap: 4px;
    }

    /* App header adjustments */
    .app-header h1 {
        font-size: 0.95rem;
    }

    .app-header {
        gap: 8px;
        padding: 6px 10px;
    }

    .basemap-toggle select,
    .terrain-3d-btn,
    .dark-mode-btn {
        font-size: 0.75rem;
        padding: 6px 8px;
        min-height: 36px;
    }

    /* Prevent horizontal scrolling on body */
    body {
        overflow-x: hidden;
    }

    /* SVG sizing for mobile */
    svg {
        max-width: 100%;
        height: auto !important;
    }
}

/* Small tablets: 480px - 600px */
@media (min-width: 481px) and (max-width: 600px) {
    .feature-sidebar {
        width: calc(100vw - 40px) !important;
        max-height: 70vh !important;
    }

    .profile-mode-btn {
        font-size: 0.75rem;
        padding: 6px 10px;
        min-height: 40px;
    }
}

/* Comparison panel legend container */
#comparison-legend {
    width: 100%;
    padding: 10px 12px;
    border-top: 1px solid var(--border-color);
    background-color: var(--bg-card);
    font-size: 0.85rem;
    box-sizing: border-box;
    display: flex;
    flex-direction: column;
    gap: 0;
    overflow-y: auto;
    overflow-x: hidden;
}

#comparison-legend > div {
    display: flex;
    align-items: center;
    gap: 8px;
    width: 100%;
    margin-bottom: 4px;
    padding: 4px 8px;
    box-sizing: border-box;
    height: auto;
    flex-shrink: 0;
}

#comparison-legend svg {
    width: 100%;
    display: block;
    flex-shrink: 0;
}

/* Mobile/tablet responsive */
@media (max-width: 768px) {
    #comparison-legend {
        max-height: 120px;
        font-size: 0.85rem;
        padding: 8px 10px;
    }
}

/* Small screens */
@media (max-width: 480px) {
    #comparison-legend {
        max-height: 100px;
        font-size: 0.8rem;
    }
}

/* ====== UNIFIED PANEL SYSTEM ====== */

/* Inner toggle for layer controls (preserved from old design) */
.lc-inner-toggle {
    display: flex;
    align-items: center;
    padding: 8px 10px;
    border-bottom: 1px solid var(--border-color);
    margin-bottom: 8px;
    padding-bottom: 8px;
    font-weight: 600;
    font-size: 0.82rem;
}

/* Standard panel header — shared by all panels */
.panel-header {
    display: flex;
    align-items: center;
    gap: 6px;
    padding: 8px 10px;
    border-bottom: 1px solid var(--border-color);
    cursor: grab;
    user-select: none;
    flex-shrink: 0;
    min-height: 36px;
    transition: background 0.15s ease, border-bottom-color 0.2s;
}

.panel-header:active {
    cursor: grabbing;
}

.drag-dots {
    font-size: 0.7rem;
    color: var(--text-muted);
    flex-shrink: 0;
}

.panel-title {
    flex: 1;
    font-size: 0.78rem;
    font-weight: 600;
    text-transform: uppercase;
    letter-spacing: 0.4px;
    color: var(--text-muted);
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
}

.panel-header-btns {
    display: flex;
    align-items: center;
    gap: 2px;
    flex-shrink: 0;
}

.panel-btn {
    background: none;
    border: none;
    cursor: pointer;
    color: var(--text-muted);
    font-size: 0.75rem;
    padding: 2px 5px;
    border-radius: 4px;
    line-height: 1;
    transition: color 0.15s, background 0.15s;
}

.panel-btn:hover {
    color: var(--text-main);
    background: rgba(255, 255, 255, 0.08);
}

/* Collapsed state */
.glass-panel.panel-collapsed .panel-body {
    display: none !important;
}

.glass-panel.panel-collapsed .panel-header {
    border-bottom: none;
}

.panel-btn.collapse-btn {
    transition: transform 0.2s ease;
}

.glass-panel.panel-collapsed .panel-btn.collapse-btn {
    transform: rotate(-90deg);
}

/* Dock zone indicators (shown during drag) */
.dock-zone {
    position: fixed;
    z-index: 9000;
    pointer-events: none;
    background: rgba(37, 99, 235, 0.12);
    border: 2px dashed rgba(37, 99, 235, 0.45);
    transition: opacity 0.15s;
    opacity: 0;
    border-radius: 4px;
}

.dock-zone.active {
    opacity: 1;
}

.dock-zone-left {
    top: 0;
    left: 0;
    width: 80px;
    height: 100vh;
}

.dock-zone-right {
    top: 0;
    right: 0;
    width: 80px;
    height: 100vh;
}

.dock-zone-top {
    top: 0;
    left: 0;
    width: 100vw;
    height: 80px;
}

.dock-zone-bottom {
    bottom: 0;
    left: 0;
    width: 100vw;
    height: 80px;
}

/* Docked tab states */
.glass-panel[data-docked] {
    position: fixed !important;
    border-radius: 0 !important;
    overflow: hidden;
    transition: transform 0.3s cubic-bezier(0.4, 0, 0.2, 1),
                width 0.3s cubic-bezier(0.4, 0, 0.2, 1),
                height 0.3s cubic-bezier(0.4, 0, 0.2, 1);
}

/* Left edge tab */
.glass-panel[data-docked="left"] {
    left: 0;
    top: var(--dock-offset-top, 0px);
    width: 36px;
    height: auto;
    min-height: 120px;
    border-right: 1px solid var(--border-color);
    border-left: none;
    border-radius: 0 var(--radius-md) var(--radius-md) 0 !important;
}

.glass-panel[data-docked="left"] .panel-header {
    flex-direction: column;
    justify-content: center;
    align-items: center;
    padding: 12px 6px;
    border-bottom: none;
    cursor: pointer;
    min-height: unset;
    height: 100%;
    width: 36px;
    gap: 10px;
}

.glass-panel[data-docked="left"] .panel-title {
    writing-mode: vertical-rl;
    text-orientation: mixed;
    transform: rotate(180deg);
    white-space: nowrap;
    overflow: visible;
    text-overflow: clip;
    font-size: 0.72rem;
}

.glass-panel[data-docked="left"] .panel-header-btns,
.glass-panel[data-docked="left"] .drag-dots {
    display: none;
}

.glass-panel[data-docked="left"] .panel-body {
    display: none;
}

/* Right edge tab */
.glass-panel[data-docked="right"] {
    right: 0;
    left: auto !important;
    top: var(--dock-offset-top, 0px);
    width: 36px;
    height: auto;
    min-height: 120px;
    border-left: 1px solid var(--border-color);
    border-right: none;
    border-radius: var(--radius-md) 0 0 var(--radius-md) !important;
}

.glass-panel[data-docked="right"] .panel-header {
    flex-direction: column;
    justify-content: center;
    align-items: center;
    padding: 12px 6px;
    border-bottom: none;
    cursor: pointer;
    min-height: unset;
    height: 100%;
    width: 36px;
    gap: 10px;
}

.glass-panel[data-docked="right"] .panel-title {
    writing-mode: vertical-rl;
    text-orientation: mixed;
    white-space: nowrap;
    overflow: visible;
    text-overflow: clip;
    font-size: 0.72rem;
}

.glass-panel[data-docked="right"] .panel-header-btns,
.glass-panel[data-docked="right"] .drag-dots {
    display: none;
}

.glass-panel[data-docked="right"] .panel-body {
    display: none;
}

/* Top edge tab */
.glass-panel[data-docked="top"] {
    top: 0;
    left: var(--dock-offset-left, 0px);
    height: 36px;
    width: auto;
    min-width: 80px;
    border-bottom: 1px solid var(--border-color);
    border-top: none;
    border-radius: 0 0 var(--radius-md) var(--radius-md) !important;
}

.glass-panel[data-docked="top"] .panel-header {
    flex-direction: row;
    padding: 0 10px;
    height: 36px;
    border-bottom: none;
    cursor: pointer;
}

.glass-panel[data-docked="top"] .panel-header-btns,
.glass-panel[data-docked="top"] .drag-dots {
    display: none;
}

.glass-panel[data-docked="top"] .panel-body {
    display: none;
}

/* Bottom edge tab */
.glass-panel[data-docked="bottom"] {
    bottom: 0;
    top: auto !important;
    left: var(--dock-offset-left, 0px);
    height: 36px;
    width: auto;
    min-width: 80px;
    border-top: 1px solid var(--border-color);
    border-bottom: none;
    border-radius: var(--radius-md) var(--radius-md) 0 0 !important;
}

.glass-panel[data-docked="bottom"] .panel-header {
    flex-direction: row;
    padding: 0 10px;
    height: 36px;
    border-bottom: none;
    cursor: pointer;
}

.glass-panel[data-docked="bottom"] .panel-header-btns,
.glass-panel[data-docked="bottom"] .drag-dots {
    display: none;
}

.glass-panel[data-docked="bottom"] .panel-body {
    display: none;
}

/* Drawer open state — panel slides out from tab */
/* Left drawer open */
.glass-panel[data-docked="left"][data-drawer-open="true"] {
    width: var(--panel-restore-width, 280px);
    border-radius: 0 var(--radius-lg) var(--radius-lg) 0 !important;
}

.glass-panel[data-docked="left"][data-drawer-open="true"] .panel-header {
    flex-direction: row;
    writing-mode: horizontal-tb;
    width: auto;
    height: auto;
    border-bottom: 1px solid var(--border-color);
    cursor: grab;
    padding: 8px 10px;
    min-height: 36px;
}

.glass-panel[data-docked="left"][data-drawer-open="true"] .panel-title {
    writing-mode: horizontal-tb;
    transform: none;
    overflow: hidden;
    text-overflow: ellipsis;
}

.glass-panel[data-docked="left"][data-drawer-open="true"] .panel-header-btns,
.glass-panel[data-docked="left"][data-drawer-open="true"] .drag-dots {
    display: flex;
}

.glass-panel[data-docked="left"][data-drawer-open="true"] .panel-body {
    display: block;
    overflow-y: auto;
    max-height: calc(100vh - 50px);
}

/* Right drawer open */
.glass-panel[data-docked="right"][data-drawer-open="true"] {
    width: var(--panel-restore-width, 280px);
    right: 0;
    left: auto !important;
    border-radius: var(--radius-lg) 0 0 var(--radius-lg) !important;
}

.glass-panel[data-docked="right"][data-drawer-open="true"] .panel-header {
    flex-direction: row;
    width: auto;
    height: auto;
    border-bottom: 1px solid var(--border-color);
    padding: 8px 10px;
    min-height: 36px;
    cursor: grab;
}

.glass-panel[data-docked="right"][data-drawer-open="true"] .panel-title {
    writing-mode: horizontal-tb;
    transform: none;
}

.glass-panel[data-docked="right"][data-drawer-open="true"] .panel-header-btns,
.glass-panel[data-docked="right"][data-drawer-open="true"] .drag-dots {
    display: flex;
}

.glass-panel[data-docked="right"][data-drawer-open="true"] .panel-body {
    display: block;
    overflow-y: auto;
    max-height: calc(100vh - 50px);
}

/* Top/bottom drawers: expand height instead of width */
.glass-panel[data-docked="top"][data-drawer-open="true"] {
    height: var(--panel-restore-height, 300px);
    border-radius: 0 0 var(--radius-lg) var(--radius-lg) !important;
}

.glass-panel[data-docked="top"][data-drawer-open="true"] .panel-header {
    height: 36px;
    border-bottom: 1px solid var(--border-color);
}

.glass-panel[data-docked="top"][data-drawer-open="true"] .panel-header-btns,
.glass-panel[data-docked="top"][data-drawer-open="true"] .drag-dots {
    display: flex;
}

.glass-panel[data-docked="top"][data-drawer-open="true"] .panel-body {
    display: block;
}

.glass-panel[data-docked="bottom"][data-drawer-open="true"] {
    height: var(--panel-restore-height, 300px);
    bottom: 0;
    top: auto !important;
    border-radius: var(--radius-lg) var(--radius-lg) 0 0 !important;
}

.glass-panel[data-docked="bottom"][data-drawer-open="true"] .panel-header {
    height: 36px;
    border-bottom: 1px solid var(--border-color);
}

.glass-panel[data-docked="bottom"][data-drawer-open="true"] .panel-header-btns,
.glass-panel[data-docked="bottom"][data-drawer-open="true"] .drag-dots {
    display: flex;
}

.glass-panel[data-docked="bottom"][data-drawer-open="true"] .panel-body {
    display: block;
}

/* Three-mode docking system */
/* Compact mode: icon-tab only, body hidden */
.glass-panel[data-dock-mode="compact"] {
    /* Inherits docked tab styling from data-docked rules */
}

.glass-panel[data-dock-mode="compact"] .panel-body {
    display: none !important;
}

.glass-panel[data-dock-mode="compact"] .panel-header-btns .undock-btn {
    display: flex;
}

.glass-panel[data-dock-mode="compact"] .panel-header-btns .expand-btn {
    display: flex;
}

/* Expanded mode: drawer open with content visible */
.glass-panel[data-dock-mode="expanded"] {
    /* Inherits docked tab position, expands drawer */
}

.glass-panel[data-dock-mode="expanded"].data-docked-left,
.glass-panel[data-dock-mode="expanded"][data-docked="left"] {
    width: var(--panel-restore-width, 280px) !important;
    border-radius: 0 var(--radius-lg) var(--radius-lg) 0 !important;
}

.glass-panel[data-dock-mode="expanded"][data-docked="right"] {
    width: var(--panel-restore-width, 280px) !important;
    border-radius: var(--radius-lg) 0 0 var(--radius-lg) !important;
}

.glass-panel[data-dock-mode="expanded"][data-docked="top"] {
    height: var(--panel-restore-height, 300px) !important;
    border-radius: 0 0 var(--radius-lg) var(--radius-lg) !important;
}

.glass-panel[data-dock-mode="expanded"][data-docked="bottom"] {
    height: var(--panel-restore-height, 300px) !important;
    border-radius: var(--radius-lg) var(--radius-lg) 0 0 !important;
}

.glass-panel[data-dock-mode="expanded"] .panel-header {
    flex-direction: row !important;
    writing-mode: horizontal-tb !important;
    height: auto !important;
    border-bottom: 1px solid var(--border-color) !important;
    cursor: grab !important;
    padding: 8px 10px !important;
}

.glass-panel[data-dock-mode="expanded"] .panel-title {
    writing-mode: horizontal-tb !important;
    transform: none !important;
    overflow: hidden;
    text-overflow: ellipsis;
}

.glass-panel[data-dock-mode="expanded"] .panel-header-btns,
.glass-panel[data-dock-mode="expanded"] .drag-dots {
    display: flex !important;
}

.glass-panel[data-dock-mode="expanded"] .panel-body {
    display: block !important;
    overflow-y: auto;
    max-height: calc(100vh - 50px);
}

/* Mobile: disable docking on narrow screens */
@media (max-width: 600px) {
    .glass-panel[data-docked] {
        position: absolute !important;
        width: auto !important;
        height: auto !important;
        left: auto !important;
        right: auto !important;
        top: auto !important;
        bottom: auto !important;
        border-radius: var(--radius-lg) !important;
        data-docked: none !important;
    }

    .glass-panel[data-docked] .panel-body {
        display: block !important;
    }

    .glass-panel[data-docked] .panel-header {
        flex-direction: row;
        border-bottom: 1px solid var(--border-color);
    }

    .glass-panel[data-docked] .panel-header-btns,
    .glass-panel[data-docked] .drag-dots {
        display: flex !important;
    }
}
