File size: 24,389 Bytes
9492321
5ab527f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
9492321
 
5ab527f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
9492321
 
 
 
5ab527f
 
 
 
 
 
 
 
 
 
72c8847
5ab527f
 
 
9492321
 
 
 
 
 
 
 
5ab527f
 
9492321
 
 
 
 
 
 
 
 
5ab527f
 
 
 
 
 
 
 
9492321
 
5ab527f
 
 
 
 
 
 
 
 
 
 
 
 
 
9492321
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
5ab527f
 
72c8847
 
 
 
 
 
 
4bf8d80
72c8847
 
 
 
 
 
 
 
5ab527f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
9492321
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
5ab527f
 
9492321
 
 
 
 
 
5ab527f
9492321
5ab527f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
9492321
 
 
 
 
 
 
5ab527f
 
9492321
5ab527f
 
 
 
 
 
 
 
 
 
 
 
9492321
 
 
5ab527f
9492321
5ab527f
 
 
 
 
 
 
 
 
 
 
 
 
9492321
 
 
 
 
 
 
 
5ab527f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
9492321
 
5ab527f
 
 
 
 
9492321
 
 
 
 
 
5ab527f
9492321
5ab527f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
9492321
 
 
 
 
 
 
 
 
 
 
5ab527f
9492321
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
5ab527f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
9492321
 
 
 
 
 
5ab527f
9492321
5ab527f
 
 
 
 
 
 
9492321
5ab527f
 
 
 
 
 
 
 
 
9492321
 
 
 
 
 
 
5ab527f
 
 
9492321
5ab527f
 
 
 
 
 
9492321
 
5ab527f
 
9492321
 
5ab527f
 
 
 
 
 
 
 
 
 
 
9492321
 
 
5ab527f
9492321
5ab527f
9492321
 
 
 
 
5ab527f
 
9492321
 
 
 
 
5ab527f
 
 
 
9492321
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
5ab527f
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
import React, { useState, useEffect, useCallback, useMemo } from 'react'
import { X, ChevronDown, ChevronUp, RotateCcw, Search } from 'lucide-react'

const ENTITY_TYPES = [
  { value: 'startup', label: 'Startups', icon: '🚀', color: '#3B82F6' },
  { value: 'sme', label: 'SMEs', icon: '🏢', color: '#10B981' },
  { value: 'college_ecell', label: 'College E-Cells', icon: '🎓', color: '#FBBF24' },
  { value: 'incubator', label: 'Incubators', icon: '🧪', color: '#A855F7' },
  { value: 'accelerator', label: 'Accelerators', icon: '⚡', color: '#EC4899' },
]

const TOP_SECTORS = [
  { value: 'fintech', label: 'FinTech', icon: '💳', color: '#3B82F6' },
  { value: 'saas_ai', label: 'SaaS / AI', icon: '☁️', color: '#6366F1' },
  { value: 'ecommerce', label: 'E-Commerce', icon: '🛒', color: '#F59E0B' },
  { value: 'healthcare', label: 'Healthcare', icon: '🏥', color: '#10B981' },
  { value: 'manufacturing', label: 'Manufacturing', icon: '🏭', color: '#78716C' },
]

const DPIIT_CATEGORIES = [
  { value: 'cleantech', label: 'CleanTech', icon: '🌿' },
  { value: 'agritech', label: 'AgriTech', icon: '🌾' },
  { value: 'edtech', label: 'EdTech', icon: '📚' },
  { value: 'healthtech', label: 'HealthTech', icon: '💊' },
  { value: 'deeptech', label: 'DeepTech', icon: '🔬' },
  { value: 'ai_ml', label: 'AI / ML', icon: '🤖' },
  { value: 'cybersecurity', label: 'Cybersecurity', icon: '🔒' },
  { value: 'foodtech', label: 'FoodTech', icon: '🍔' },
  { value: 'logistics', label: 'Logistics', icon: '🚛' },
  { value: 'mobility', label: 'Mobility', icon: '🚗' },
  { value: 'proptech', label: 'PropTech', icon: '🏠' },
  { value: 'spacetech', label: 'SpaceTech', icon: '🚀' },
  { value: 'biotech', label: 'BioTech', icon: '🧬' },
  { value: 'ev', label: 'EV / E-Mobility', icon: '🔋' },
  { value: 'gaming', label: 'Gaming', icon: '🎮' },
  { value: 'mediatech', label: 'MediaTech', icon: '📺' },
  { value: 'iot', label: 'IoT', icon: '📡' },
  { value: 'drone_tech', label: 'Drone Tech', icon: '🛸' },
]

const BUSINESS_MODELS = [
  { value: 'lifestyle', label: 'Lifestyle', icon: '🌟', desc: 'Self-funded, profitable' },
  { value: 'scalable', label: 'Scalable', icon: '📈', desc: 'VC-funded, high-growth' },
  { value: 'social', label: 'Social Enterprise', icon: '🌍', desc: 'Impact-first' },
  { value: 'large_company', label: 'Large Company', icon: '🏢', desc: 'Corporate spin-off' },
]

const STAGES = [
  { value: 'ideation', label: 'Ideation', color: '#64748B' },
  { value: 'validation', label: 'Validation', color: '#60A5FA' },
  { value: 'early_traction', label: 'Early Traction', color: '#34D399' },
  { value: 'scaling', label: 'Scaling', color: '#FBBF24' },
  { value: 'mature', label: 'Mature', color: '#F87171' },
]

export default function Sidebar({ filters, facets, onFilterChange, onReset, onClose, isMobile }) {
  const [sectors, setSectors] = useState([])
  const [states, setStates] = useState([])
  const [expandedSections, setExpandedSections] = useState({
    type: true,
    top_sector: false,
    dpiit_cat: false,
    biz_model: false,
    awards: false,
    stage: false,
    location: false,
    unicorn: false,
  })

  // In-filter search states for all searchable sections
  const [searchTerms, setSearchTerms] = useState({
    dpiit: '',
    sector: '',
    stage: '',
    location: '',
    biz_model: '',
    type: '',
  })

  useEffect(() => {
    fetch('/api/entities/sectors').then(r => r.json()).then(setSectors).catch(console.error)
    fetch('/api/entities/locations/states').then(r => r.json()).then(setStates).catch(console.error)
  }, [])

  const toggleSection = (key) => setExpandedSections(prev => ({ ...prev, [key]: !prev[key] }))

  const updateSearch = (key, value) => setSearchTerms(prev => ({ ...prev, [key]: value }))

  const toggleArrayFilter = useCallback((key, value) => {
    const current = filters[key]
    const next = current.includes(value) ? current.filter(v => v !== value) : [...current, value]
    onFilterChange(key, next)
  }, [filters, onFilterChange])

  const activeFilterCount = [
    filters.entity_type.length, filters.sector.length, filters.stage.length,
    filters.dpiit_category.length, filters.business_model.length, filters.unicorn_status.length,
    filters.dpiit_only ? 1 : 0, filters.is_women_led ? 1 : 0,
    filters.is_rural_impact ? 1 : 0, filters.is_campus_startup ? 1 : 0,
    filters.nsa_winner ? 1 : 0, filters.state ? 1 : 0,
  ].reduce((a, b) => a + b, 0)

  // Filtered lists
  const filteredDpiitCats = useMemo(() => {
    const q = searchTerms.dpiit.toLowerCase()
    return q ? DPIIT_CATEGORIES.filter(c =>
      c.label.toLowerCase().includes(q) || c.value.includes(q)
    ) : DPIIT_CATEGORIES
  }, [searchTerms.dpiit])

  const filteredTypes = useMemo(() => {
    const q = searchTerms.type.toLowerCase()
    return q ? ENTITY_TYPES.filter(t =>
      t.label.toLowerCase().includes(q) || t.value.includes(q)
    ) : ENTITY_TYPES
  }, [searchTerms.type])

  const filteredStages = useMemo(() => {
    const q = searchTerms.stage.toLowerCase()
    return q ? STAGES.filter(s =>
      s.label.toLowerCase().includes(q) || s.value.includes(q)
    ) : STAGES
  }, [searchTerms.stage])

  const filteredStates = useMemo(() => {
    const q = searchTerms.location.toLowerCase()
    return q ? states.filter(s =>
      s.state.toLowerCase().includes(q)
    ) : states
  }, [searchTerms.location, states])

  const filteredBizModels = useMemo(() => {
    const q = searchTerms.biz_model.toLowerCase()
    return q ? BUSINESS_MODELS.filter(b =>
      b.label.toLowerCase().includes(q) || b.value.includes(q)
    ) : BUSINESS_MODELS
  }, [searchTerms.biz_model])

  // Helper: count active filters in a section (shows badge on collapsed sections)
  const sectionBadge = (count) => count > 0 ? (
    <span className="bg-brand-500 text-white text-[10px] font-bold w-4 h-4 rounded-full flex items-center justify-center">{count}</span>
  ) : null

  return (
    <div className={
      isMobile
        ? "fixed inset-x-0 bottom-0 z-30 max-h-[75vh] flex flex-col pointer-events-auto animate-slide-up"
        : "absolute top-16 left-3 bottom-4 w-80 z-20 animate-slide-in-left flex flex-col pointer-events-none"
    }>
      <div className={
        isMobile
          ? "bg-atlas-bg/95 border-t border-atlas-border rounded-t-2xl shadow-2xl flex flex-col overflow-hidden max-h-[75vh]"
          : "glass rounded-2xl shadow-2xl flex flex-col overflow-hidden pointer-events-auto max-h-full"
      }>
        {/* Mobile drag handle */}
        {isMobile && (
          <div className="flex justify-center py-2 flex-shrink-0">
            <div className="w-10 h-1 rounded-full bg-atlas-border" />
          </div>
        )}
        {/* Header */}
        <div className="flex items-center justify-between px-4 py-3 border-b border-atlas-border flex-shrink-0">
          <div className="flex items-center gap-2">
            <span className="text-sm font-semibold text-atlas-text">Filters</span>
            {activeFilterCount > 0 && (
              <span className="bg-brand-500 text-white text-xs font-bold px-2 py-0.5 rounded-full">{activeFilterCount}</span>
            )}
          </div>
          <div className="flex items-center gap-1">
            {activeFilterCount > 0 && (
              <button onClick={onReset} className="text-xs text-brand-500 hover:text-brand-400 flex items-center gap-1 px-2 py-1 rounded hover:bg-brand-500/10">
                <RotateCcw size={12} /> Reset
              </button>
            )}
            <button onClick={onClose} className="p-1 hover:bg-atlas-surface rounded-lg transition-colors">
              <X size={16} className="text-atlas-muted" />
            </button>
          </div>
        </div>

        {/* Quick filters bar — always visible, no scroll needed */}
        <div className="px-3 py-2 border-b border-atlas-border/50 flex-shrink-0">
          <div className="flex flex-wrap gap-1.5">
            {/* Quick toggle pills */}
            <QuickPill
              active={filters.unicorn_status.includes('unicorn')}
              onClick={() => toggleArrayFilter('unicorn_status', 'unicorn')}
              icon="🦄" label="Unicorns"
              count={facets?.awards?.unicorns}
            />
            <QuickPill
              active={filters.is_women_led}
              onClick={() => onFilterChange('is_women_led', !filters.is_women_led)}
              icon="👩" label="Women-led"
              count={facets?.awards?.women_led}
            />
            <QuickPill
              active={filters.dpiit_only}
              onClick={() => onFilterChange('dpiit_only', !filters.dpiit_only)}
              icon="🏛️" label="DPIIT"
            />
            <QuickPill
              active={filters.nsa_winner}
              onClick={() => onFilterChange('nsa_winner', !filters.nsa_winner)}
              icon="🏆" label="NSA"
              count={facets?.awards?.nsa_winners}
            />
          </div>
        </div>

        {/* Scrollable filter sections */}
        <div className="flex-1 overflow-y-auto px-4 py-2 space-y-0.5 scrollbar-thin">

          {/* Entity Type */}
          <FilterSection
            title="Entity Type"
            expanded={expandedSections.type}
            onToggle={() => toggleSection('type')}
            badge={sectionBadge(filters.entity_type.length)}
          >
            <div className="space-y-1">
              {filteredTypes.map(type => {
                const count = facets?.entity_type?.[type.value] || 0
                const active = filters.entity_type.includes(type.value)
                return (
                  <button key={type.value} onClick={() => toggleArrayFilter('entity_type', type.value)}
                    className={`w-full flex items-center gap-2.5 px-3 py-2 rounded-xl text-sm transition-all ${
                      active ? 'bg-brand-500/15 ring-1 ring-brand-500/30 text-brand-400' : 'hover:bg-atlas-surface text-atlas-muted'
                    }`}>
                    <span className="text-base">{type.icon}</span>
                    <span className="flex-1 text-left font-medium">{type.label}</span>
                    <span className={`text-xs font-mono ${active ? 'text-brand-500' : 'text-atlas-muted/60'}`}>{count.toLocaleString()}</span>
                  </button>
                )
              })}
            </div>
          </FilterSection>

          {/* Top Sectors */}
          <FilterSection
            title="Sectors"
            expanded={expandedSections.top_sector}
            onToggle={() => toggleSection('top_sector')}
            badge={sectionBadge(filters.sector.length)}
          >
            <InFilterSearch value={searchTerms.sector} onChange={(v) => updateSearch('sector', v)} placeholder="Search sectors..." />
            <div className="flex flex-wrap gap-1.5">
              {TOP_SECTORS.filter(s =>
                !searchTerms.sector || s.label.toLowerCase().includes(searchTerms.sector.toLowerCase()) || s.value.includes(searchTerms.sector.toLowerCase())
              ).map(s => {
                const active = filters.sector.includes(s.value)
                return (
                  <button key={s.value} onClick={() => toggleArrayFilter('sector', s.value)}
                    className={`inline-flex items-center gap-1 px-2.5 py-1.5 rounded-lg text-xs font-medium transition-all ${
                      active ? 'ring-1 text-white shadow-sm' : 'bg-atlas-surface text-atlas-muted hover:bg-atlas-border'
                    }`}
                    style={active ? { backgroundColor: s.color, ringColor: s.color } : {}}>
                    <span>{s.icon}</span><span>{s.label}</span>
                  </button>
                )
              })}
              {/* Show DPIIT categories matching search */}
              {searchTerms.sector && sectors.filter(s => s.category === 'dpiit_category' &&
                (s.label.toLowerCase().includes(searchTerms.sector.toLowerCase()) || s.slug.includes(searchTerms.sector.toLowerCase())) &&
                !TOP_SECTORS.find(ts => ts.value === s.slug)
              ).slice(0, 8).map(s => {
                const active = filters.sector.includes(s.slug)
                return (
                  <button key={s.slug} onClick={() => toggleArrayFilter('sector', s.slug)}
                    className={`inline-flex items-center gap-1 px-2.5 py-1.5 rounded-lg text-xs font-medium transition-all ${
                      active ? 'ring-1 text-white shadow-sm bg-brand-500' : 'bg-atlas-surface text-atlas-muted hover:bg-atlas-border'
                    }`}>
                    <span>{s.icon || '🏷️'}</span><span>{s.label}</span>
                  </button>
                )
              })}
            </div>
          </FilterSection>

          {/* DPIIT Categories */}
          <FilterSection
            title="DPIIT Categories"
            expanded={expandedSections.dpiit_cat}
            onToggle={() => toggleSection('dpiit_cat')}
            badge={sectionBadge(filters.dpiit_category.length)}
          >
            <InFilterSearch value={searchTerms.dpiit} onChange={(v) => updateSearch('dpiit', v)} placeholder="Search DPIIT categories..." />
            <div className="flex flex-wrap gap-1.5">
              {filteredDpiitCats.map(cat => {
                const active = filters.dpiit_category.includes(cat.value)
                const count = facets?.dpiit_category?.[cat.value] || 0
                return (
                  <button key={cat.value} onClick={() => toggleArrayFilter('dpiit_category', cat.value)}
                    className={`inline-flex items-center gap-1 px-2 py-1.5 rounded-lg text-xs font-medium transition-all ${
                      active ? 'bg-brand-500/20 ring-1 ring-brand-500/30 text-brand-400' : 'bg-atlas-surface text-atlas-muted hover:bg-atlas-border'
                    }`}>
                    <span>{cat.icon}</span>
                    <span>{cat.label}</span>
                    {count > 0 && <span className="text-[10px] opacity-60">({count.toLocaleString()})</span>}
                  </button>
                )
              })}
              {searchTerms.dpiit && filteredDpiitCats.length === 0 && (
                <p className="text-xs text-atlas-muted/50 py-1">No categories match "{searchTerms.dpiit}"</p>
              )}
            </div>
          </FilterSection>

          {/* Business Models */}
          <FilterSection
            title="Business Model"
            expanded={expandedSections.biz_model}
            onToggle={() => toggleSection('biz_model')}
            badge={sectionBadge(filters.business_model.length)}
          >
            <div className="space-y-1">
              {filteredBizModels.map(bm => {
                const active = filters.business_model.includes(bm.value)
                const count = facets?.business_model?.[bm.value] || 0
                return (
                  <button key={bm.value} onClick={() => toggleArrayFilter('business_model', bm.value)}
                    className={`w-full flex items-center gap-2.5 px-3 py-2 rounded-xl text-sm transition-all ${
                      active ? 'bg-brand-500/15 ring-1 ring-brand-500/30 text-brand-400' : 'hover:bg-atlas-surface text-atlas-muted'
                    }`}>
                    <span>{bm.icon}</span>
                    <div className="flex-1 text-left">
                      <span className="font-medium">{bm.label}</span>
                      <p className="text-[10px] opacity-60 mt-0.5">{bm.desc}</p>
                    </div>
                    <span className="text-xs font-mono opacity-60">{count.toLocaleString()}</span>
                  </button>
                )
              })}
            </div>
          </FilterSection>

          {/* Awards & Recognition — merged unicorn + special */}
          <FilterSection
            title="Awards & Recognition"
            expanded={expandedSections.awards}
            onToggle={() => toggleSection('awards')}
            badge={sectionBadge(
              (filters.is_women_led ? 1 : 0) + (filters.is_rural_impact ? 1 : 0) +
              (filters.is_campus_startup ? 1 : 0) + (filters.nsa_winner ? 1 : 0) +
              filters.unicorn_status.length
            )}
          >
            <div className="space-y-1">
              {/* Unicorn status */}
              {[
                { value: 'unicorn', label: 'Unicorns', icon: '🦄', count: facets?.awards?.unicorns || 0 },
                { value: 'soonicorn', label: 'Soonicorns', icon: '🌟', count: facets?.awards?.soonicorns || 0 },
              ].map(u => {
                const active = filters.unicorn_status.includes(u.value)
                return (
                  <button key={u.value} onClick={() => toggleArrayFilter('unicorn_status', u.value)}
                    className={`w-full flex items-center gap-2.5 px-3 py-2 rounded-xl text-sm transition-all ${
                      active ? 'bg-purple-500/15 ring-1 ring-purple-500/30 text-purple-400' : 'hover:bg-atlas-surface text-atlas-muted'
                    }`}>
                    <span>{u.icon}</span>
                    <span className="flex-1 text-left font-medium">{u.label}</span>
                    <span className="text-xs font-mono opacity-60">{u.count.toLocaleString()}</span>
                  </button>
                )
              })}

              <div className="h-px bg-atlas-border/30 my-1" />

              <ToggleButton
                active={filters.is_women_led}
                onClick={() => onFilterChange('is_women_led', !filters.is_women_led)}
                icon="👩" label="Women-led Startups"
                count={facets?.awards?.women_led || 0}
                activeColor="bg-pink-500/15 ring-pink-500/30 text-pink-400"
              />
              <ToggleButton
                active={filters.is_rural_impact}
                onClick={() => onFilterChange('is_rural_impact', !filters.is_rural_impact)}
                icon="🌾" label="Rural Impact"
                count={facets?.awards?.rural_impact || 0}
                activeColor="bg-green-500/15 ring-green-500/30 text-green-400"
              />
              <ToggleButton
                active={filters.is_campus_startup}
                onClick={() => onFilterChange('is_campus_startup', !filters.is_campus_startup)}
                icon="🎓" label="Campus Startups"
                count={facets?.awards?.campus_startup || 0}
                activeColor="bg-blue-500/15 ring-blue-500/30 text-blue-400"
              />
              <ToggleButton
                active={filters.nsa_winner}
                onClick={() => onFilterChange('nsa_winner', !filters.nsa_winner)}
                icon="🏆" label="NSA 5.0 Winners"
                count={facets?.awards?.nsa_winners || 0}
                activeColor="bg-amber-500/15 ring-amber-500/30 text-amber-400"
              />
            </div>
          </FilterSection>

          {/* Stage */}
          <FilterSection
            title="Startup Stage"
            expanded={expandedSections.stage}
            onToggle={() => toggleSection('stage')}
            badge={sectionBadge(filters.stage.length)}
          >
            <div className="space-y-1">
              {filteredStages.map(stage => {
                const count = facets?.stage?.[stage.value] || 0
                const active = filters.stage.includes(stage.value)
                return (
                  <button key={stage.value} onClick={() => toggleArrayFilter('stage', stage.value)}
                    className={`w-full flex items-center gap-2.5 px-3 py-2 rounded-xl text-sm transition-all ${
                      active ? 'bg-brand-500/15 ring-1 ring-brand-500/30' : 'hover:bg-atlas-surface'
                    }`}>
                    <span className="w-2.5 h-2.5 rounded-full flex-shrink-0" style={{ backgroundColor: stage.color }} />
                    <span className="flex-1 text-left text-atlas-muted font-medium">{stage.label}</span>
                    <span className="text-xs text-atlas-muted/60 font-mono">{count.toLocaleString()}</span>
                  </button>
                )
              })}
            </div>
          </FilterSection>

          {/* Location */}
          <FilterSection
            title="Location"
            expanded={expandedSections.location}
            onToggle={() => toggleSection('location')}
            badge={sectionBadge(filters.state ? 1 : 0)}
          >
            <InFilterSearch value={searchTerms.location} onChange={(v) => updateSearch('location', v)} placeholder="Search states..." />
            <select value={filters.state} onChange={(e) => onFilterChange('state', e.target.value)}
              className="w-full px-3 py-2 rounded-xl border border-atlas-border bg-atlas-surface text-atlas-text text-sm focus:ring-2 focus:ring-brand-500 outline-none">
              <option value="">All States</option>
              {filteredStates.map(s => (
                <option key={s.state} value={s.state}>{s.state} ({s.count.toLocaleString()})</option>
              ))}
            </select>
          </FilterSection>
        </div>

        {/* Legend — compact */}
        <div className="px-4 py-2 border-t border-atlas-border flex-shrink-0">
          <div className="flex flex-wrap gap-x-3 gap-y-1">
            {ENTITY_TYPES.map(t => (
              <div key={t.value} className="flex items-center gap-1.5 text-[10px] text-atlas-muted">
                <div className="w-2 h-2 rounded-full" style={{ backgroundColor: t.color }} />
                {t.label}
              </div>
            ))}
          </div>
        </div>
      </div>
    </div>
  )
}


/* ── Subcomponents ── */

function FilterSection({ title, expanded, onToggle, children, badge }) {
  return (
    <div className="border-b border-atlas-border/30 last:border-0">
      <button onClick={onToggle}
        className="w-full flex items-center justify-between py-2.5 text-xs font-semibold text-atlas-muted uppercase tracking-wider hover:text-atlas-text transition-colors">
        <span className="flex items-center gap-2">
          {title}
          {!expanded && badge}
        </span>
        {expanded ? <ChevronUp size={14} /> : <ChevronDown size={14} />}
      </button>
      {expanded && (
        <div className="pb-2 animate-fade-in">
          {children}
        </div>
      )}
    </div>
  )
}

function InFilterSearch({ value, onChange, placeholder }) {
  return (
    <div className="relative mb-2">
      <Search size={12} className="absolute left-2.5 top-1/2 -translate-y-1/2 text-atlas-muted/50" />
      <input
        value={value}
        onChange={(e) => onChange(e.target.value)}
        placeholder={placeholder}
        className="w-full pl-7 pr-3 py-1.5 rounded-lg bg-atlas-surface border border-atlas-border text-xs text-atlas-text placeholder:text-atlas-muted/40 focus:outline-none focus:ring-1 focus:ring-brand-500"
      />
      {value && (
        <button onClick={() => onChange('')} className="absolute right-2 top-1/2 -translate-y-1/2">
          <X size={10} className="text-atlas-muted/50 hover:text-atlas-muted" />
        </button>
      )}
    </div>
  )
}

function QuickPill({ active, onClick, icon, label, count }) {
  return (
    <button onClick={onClick}
      className={`inline-flex items-center gap-1 px-2 py-1 rounded-lg text-[11px] font-medium transition-all ${
        active
          ? 'bg-brand-500/20 ring-1 ring-brand-500/40 text-brand-400'
          : 'bg-atlas-surface/60 text-atlas-muted hover:bg-atlas-surface hover:text-atlas-text'
      }`}>
      <span>{icon}</span>
      <span>{label}</span>
      {count > 0 && <span className="text-[9px] opacity-60">{count.toLocaleString()}</span>}
    </button>
  )
}

function ToggleButton({ active, onClick, icon, label, count, activeColor }) {
  return (
    <button onClick={onClick}
      className={`w-full flex items-center gap-2.5 px-3 py-2 rounded-xl text-sm transition-all ${
        active ? `${activeColor} ring-1` : 'hover:bg-atlas-surface text-atlas-muted'
      }`}>
      <span>{icon}</span>
      <span className="flex-1 text-left font-medium">{label}</span>
      <span className="text-xs font-mono opacity-60">{count.toLocaleString()}</span>
    </button>
  )
}