    /* ==========================================================================
       COLLAPSIBLE LEGEND STYLES (Self-contained, positioned below layer control)
       ========================================================================== */
    #legend-container {
      position: absolute;
      top: 62px; /* Positioned directly below the collapsed layer selector (15px + ~43px + 4px) */
      right: 15px;
      z-index: 9; /* Underneath layer control (z-index 10) so expanding layers covers it cleanly */
      user-select: none;
    }

    /* Collapsed state pill button */
    .legend-collapsed-btn {
      display: flex;
      align-items: center;
      gap: 8px;
      background: rgba(255, 255, 255, 0.97);
      border: 1px solid rgba(0, 0, 0, 0.1);
      color: #334155;
      padding: 7px 12px;
      border-radius: 6px;
      box-shadow: 0 2px 8px rgba(0, 0, 0, 0.15);
      cursor: pointer;
      font-size: 13px;
      font-weight: 600;
      transition: all 0.15s ease;
    }

    .legend-collapsed-btn:hover {
      background: #ffffff;
      color: #1e293b;
      box-shadow: 0 4px 12px rgba(0, 0, 0, 0.2);
    }

    .legend-collapsed-icon-wrap {
      width: 22px;
      height: 22px;
      background: #eef2ff;
      border-radius: 4px;
      display: flex;
      align-items: center;
      justify-content: center;
      color: #4f46e5;
    }

    /* Expanded legend window */
    .legend-card {
      width: 270px;
      background: rgba(255, 255, 255, 0.98);
      border-radius: 8px;
      box-shadow: 0 4px 16px rgba(0, 0, 0, 0.2);
      border: 1px solid rgba(0, 0, 0, 0.08);
      display: flex;
      flex-col;
      flex-direction: column;
      max-height: calc(85vh - 75px);
      overflow: hidden;
      font-size: 12px;
    }

    .legend-header {
      padding: 9px 12px;
      background: #f8fafc;
      border-bottom: 1px solid #e2e8f0;
      display: flex;
      align-items: center;
      justify-content: space-between;
      flex-shrink: 0;
    }

    .legend-header-title {
      font-weight: 700;
      color: #1e293b;
      font-size: 13px;
      display: flex;
      align-items: center;
      gap: 6px;
    }

    .legend-close-btn {
      background: transparent;
      border: none;
      cursor: pointer;
      padding: 4px;
      border-radius: 4px;
      color: #64748b;
      display: flex;
      align-items: center;
      justify-content: center;
    }

    .legend-close-btn:hover {
      background: #e2e8f0;
      color: #0f172a;
    }

    /* Filter search bar */
    .legend-search-box {
      padding: 6px 10px;
      border-bottom: 1px solid #f1f5f9;
      background: #ffffff;
      display: flex;
      align-items: center;
      gap: 6px;
    }

    .legend-search-input {
      width: 100%;
      border: 1px solid #e2e8f0;
      border-radius: 4px;
      padding: 4px 8px;
      font-size: 12px;
      outline: none;
      color: #334155;
      box-sizing: border-box;
    }

    .legend-search-input:focus {
      border-color: #6366f1;
    }

    /* Scrollable items */
    .legend-body {
      padding: 6px 8px;
      overflow-y: auto;
      flex: 1;
    }

    /* Hierarchy & Nesting Levels */
    .legend-group {
      border-bottom: 1px solid #f1f5f9;
      margin-bottom: 2px;
    }

    .legend-group:last-child {
      border-bottom: none;
    }

    .legend-group-header {
      width: 100%;
      background: transparent;
      border: none;
      padding: 6px 4px;
      cursor: pointer;
      display: flex;
      align-items: center;
      justify-content: space-between;
      text-align: left;
      font-size: 12px;
      font-weight: 600;
      color: #334155;
      border-radius: 4px;
    }

    .legend-group-header:hover {
      background: #f8fafc;
      color: #4338ca;
    }

    .legend-group-title {
      display: flex;
      align-items: center;
      gap: 6px;
    }

    .legend-group-chevron {
      transition: transform 0.2s ease;
      display: inline-flex;
    }

    .legend-group.collapsed .legend-group-chevron {
      transform: rotate(-90deg);
    }

    .legend-group.collapsed .legend-group-children {
      display: none;
    }

    .legend-group-badge {
      font-size: 10px;
      background: #f1f5f9;
      color: #64748b;
      padding: 1px 5px;
      border-radius: 10px;
      font-weight: normal;
    }

    /* Indentations for nesting levels */
    .legend-level-0 { padding-left: 0px; }
    .legend-level-1 { padding-left: 14px; }
    .legend-level-2 { padding-left: 26px; }
    .legend-level-3 { padding-left: 38px; }

    .legend-group-children {
      padding-bottom: 4px;
    }

    /* Leaf Item row */
    .legend-item-row {
      display: flex;
      align-items: center;
      gap: 8px;
      padding: 3.5px 4px;
      border-radius: 4px;
      font-size: 11.5px;
      color: #475569;
    }

    .legend-item-row:hover {
      background: #f8fafc;
    }

    .legend-symbol-wrapper {
      width: 22px;
      height: 18px;
      display: flex;
      align-items: center;
      justify-content: center;
      flex-shrink: 0;
    }

    /* Custom scrollbar */
    .legend-body::-webkit-scrollbar,
    .overlay-content::-webkit-scrollbar {
      width: 5px;
    }
    .legend-body::-webkit-scrollbar-track,
    .overlay-content::-webkit-scrollbar-track {
      background: rgba(0, 0, 0, 0.04);
      border-radius: 4px;
    }
    .legend-body::-webkit-scrollbar-thumb,
    .overlay-content::-webkit-scrollbar-thumb {
      background: rgba(0, 0, 0, 0.2);
      border-radius: 4px;
    }

    @media (max-width: 768px) {
      #inspector {
        display: none;
      }
      .map-overlay {
        top: 10px;
        right: 10px;
        width: 220px;
      }
      #legend-container {
        top: 55px;
        right: 10px;
      }
      .legend-card {
        width: 230px;
      }
    }