File size: 33,080 Bytes
9b44273
8052574
 
5e37f3b
8052574
5e37f3b
8052574
 
 
 
 
 
 
 
 
 
 
f8a3980
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
8052574
 
 
05b80f3
8052574
 
 
5e37f3b
 
9b44273
12b4775
8052574
 
5e37f3b
 
c1f9028
 
 
 
 
 
 
 
5e37f3b
8052574
 
 
9b44273
df61c55
9b44273
8052574
12b4775
 
 
 
 
 
 
8052574
 
 
 
 
 
 
 
 
 
 
 
 
 
 
05b80f3
0761dec
 
 
 
 
5df7ed5
0761dec
 
 
 
5df7ed5
0761dec
8052574
 
9b44273
 
 
 
 
 
 
 
 
 
5e37f3b
9b44273
 
 
 
 
5e37f3b
 
 
9b44273
 
 
 
 
5e37f3b
 
 
 
 
9b44273
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
8052574
 
9b44273
8052574
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
9b44273
5e37f3b
8052574
 
 
9b44273
 
 
8052574
9b44273
8052574
 
 
 
9b44273
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
8052574
 
5e37f3b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
8052574
 
5e37f3b
 
 
 
 
 
 
 
 
 
 
 
 
8052574
 
 
c1f9028
8052574
 
 
 
c1f9028
 
8052574
 
c1f9028
 
 
8052574
c1f9028
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
8052574
c1f9028
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
8052574
 
 
 
 
 
 
 
 
5c4239f
8052574
5c4239f
8052574
5c4239f
8052574
5c4239f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
12b4775
5c4239f
 
 
 
 
 
 
 
 
 
 
 
12b4775
5c4239f
 
 
 
 
 
 
 
 
12b4775
5c4239f
 
 
 
 
 
 
 
8052574
 
 
5e37f3b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
8052574
 
 
 
0b8155d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
8052574
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
40fe0ea
8052574
 
 
 
fd28988
 
 
 
91c2e16
fd28988
 
 
 
91c2e16
fd28988
 
91c2e16
 
fd28988
91c2e16
 
 
fd28988
 
 
 
 
 
 
 
 
 
 
 
8052574
 
 
 
 
 
 
 
 
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
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
import React, { useState, useEffect, useRef, useCallback } from 'react';
import { useUIStore, useReaderStore, useThemeStore } from '../../stores/appStore';
import { useSearchStore } from '../../stores/searchStore';
import type { SearchMode } from '../../stores/searchStore';
import api from '../../lib/api';
import { X, Book, Search, BarChart2, ChevronRight, Loader2, ChevronDown, Zap, Sparkles } from 'lucide-react';
import { motion, AnimatePresence } from 'framer-motion';

// NavDrawer bg matches Shell bg per theme — seamless sidebar
// Dark:   bg=#1a1a2e  text=#e0e0e0  border=#3a3a5e
// Light:  bg=#f8f6f0  text=#1a1a1a  border=#e0d0b0
// Classic:bg=#f5edd8  text=#1a1a1a  border=#d4c4a0
const DRAWER_STYLES: Record<string, { bg: string; text: string; border: string; tab: string; input: string }> = {
  dark:    { bg: 'bg-[#1a1a2e]', text: 'text-[#e0e0e0]', border: 'border-[#3a3a5e]', tab: 'bg-[#222242]', input: 'bg-[#222242]' },
  light:   { bg: 'bg-[#f8f6f0]', text: 'text-[#1a1a1a]', border: 'border-[#e0d0b0]', tab: 'bg-[#fdfaf5]', input: 'bg-white' },
  classic: { bg: 'bg-[#f5edd8]', text: 'text-[#1a1a1a]', border: 'border-[#d4c4a0]', tab: 'bg-[#fdfaf5]', input: 'bg-white' },
};
/** Helper to extract all highlighted terms inside <mark> tags from search snippets,
 *  so we can highlight individual matched words in the reader page.
 *  Falls back to the original query if no marks are found. */
const getHighlightQuery = (originalQuery: string, results: any[]): string => {
  const terms = new Set<string>();
  const regex = /<mark>(.*?)<\/mark>/gi;
  
  results.forEach(res => {
    if (res.snippet) {
      let match;
      regex.lastIndex = 0;
      while ((match = regex.exec(res.snippet)) !== null) {
        const term = match[1].replace(/<[^>]+>/g, '').trim(); // clean any nested tags
        if (term.length > 1) {
          terms.add(term);
        }
      }
    }
  });

  if (terms.size > 0) {
    return Array.from(terms).join(' ');
  }
  return originalQuery;
};

const NavDrawer: React.FC = () => {
  const { isNavOpen, setNavOpen, activeTab, setActiveTab } = useUIStore();
  const { currentVolume, setVolume, setPage, setHighlightQuery } = useReaderStore();
  const { theme } = useThemeStore();
  const {
    query, setQuery, results, totalResults,
    isLoading, isAiLoading, performSearch, performAiSearch,
    breakdown, timeTaken, searchMode, preferredMode, setPreferredMode,
    suggestions, showSuggestions, fetchSuggestions, setShowSuggestions,
    highlightWords,
  } = useSearchStore();

  const [modeDropdownOpen, setModeDropdownOpen] = useState(false);
  const modeDropdownRef = useRef<HTMLDivElement>(null);
  const [expandedVols, setExpandedVols] = useState<Record<number, boolean>>({});

  const toggleVolExpand = (volId: number) => {
    setExpandedVols(prev => ({
      ...prev,
      [volId]: !prev[volId]
    }));
  };

  const [toc, setToc] = useState<any[]>([]);
  const [loadingToc, setLoadingToc] = useState(false);
  const [volumes, setVolumes] = useState<any[]>([]);
  const [activeSuggestion, setActiveSuggestion] = useState(-1);
  const searchRef = useRef<HTMLFormElement>(null);
  const debounceRef = useRef<ReturnType<typeof setTimeout> | null>(null);

  const getHighlightString = () => {
    if (highlightWords && highlightWords.length > 0) {
      return highlightWords.join(' ');
    }
    return getHighlightQuery(query, results);
  };

  const s = DRAWER_STYLES[theme] ?? DRAWER_STYLES.dark;

  useEffect(() => {
    api.get('/api/pages/volumes').then(res => setVolumes(res.data));
  }, []);

  useEffect(() => {
    if (activeTab !== 'toc') return;
    setLoadingToc(true);
    // volume_number = currentVolume → matches WHERE v.volume_number = ?
    api.get(`/api/pages/volumes/${currentVolume}/toc`)
      .then(res => setToc(res.data))
      .finally(() => setLoadingToc(false));
  }, [activeTab, currentVolume]);

  const jumpToPage = (volumeNumber: number, pageNum: number, highlight?: string) => {
    if (window.innerWidth < 1024) {
      setNavOpen(false);
      setTimeout(() => {
        setVolume(volumeNumber);
        setPage(pageNum);
        setHighlightQuery(highlight ?? '');
      }, 250);
    } else {
      setVolume(volumeNumber);
      setPage(pageNum);
      setHighlightQuery(highlight ?? '');
    }
  };

  // Debounced suggestion fetch
  const handleQueryChange = useCallback((value: string) => {
    setQuery(value);
    setActiveSuggestion(-1);
    if (debounceRef.current) clearTimeout(debounceRef.current);
    debounceRef.current = setTimeout(() => {
      fetchSuggestions(value);
    }, 280);
  }, [setQuery, fetchSuggestions]);

  // Close suggestions + mode dropdown on outside click
  useEffect(() => {
    const handler = (e: MouseEvent) => {
      if (searchRef.current && !searchRef.current.contains(e.target as Node)) {
        setShowSuggestions(false);
      }
      if (modeDropdownRef.current && !modeDropdownRef.current.contains(e.target as Node)) {
        setModeDropdownOpen(false);
      }
    };
    document.addEventListener('mousedown', handler);
    return () => document.removeEventListener('mousedown', handler);
  }, [setShowSuggestions]);

  const handleModeSelect = (mode: SearchMode) => {
    setPreferredMode(mode);
    setModeDropdownOpen(false);
  };

  const selectSuggestion = (term: string) => {
    setQuery(term);
    setShowSuggestions(false);
    setActiveSuggestion(-1);
    setActiveTab('search');
    setTimeout(() => performSearch(), 0);
  };

  const handleKeyDown = (e: React.KeyboardEvent<HTMLInputElement>) => {
    if (!showSuggestions || suggestions.length === 0) {
      if (e.key === 'Enter') {
        e.preventDefault();
        setActiveTab('search');
        performSearch();
      }
      return;
    }
    if (e.key === 'ArrowDown') {
      e.preventDefault();
      setActiveSuggestion(i => Math.min(i + 1, suggestions.length - 1));
    } else if (e.key === 'ArrowUp') {
      e.preventDefault();
      setActiveSuggestion(i => Math.max(i - 1, -1));
    } else if (e.key === 'Enter') {
      e.preventDefault();
      if (activeSuggestion >= 0) {
        selectSuggestion(suggestions[activeSuggestion]);
      } else {
        setShowSuggestions(false);
        setActiveTab('search');
        performSearch();
      }
    } else if (e.key === 'Escape') {
      setShowSuggestions(false);
      setActiveSuggestion(-1);
    }
  };

  const handleSearch = (e: React.FormEvent) => {
    e.preventDefault();
    setShowSuggestions(false);
    setActiveTab('search');
    performSearch();
  };

  const tabs = [
    { id: 'toc',      icon: <Book size={18} />,     label: 'สารบัญ' },
    { id: 'search',   icon: <Search size={18} />,   label: 'ค้นหา' },
    { id: 'overview', icon: <BarChart2 size={18} />, label: 'ภาพรวม' },
  ] as const;

  return (
    <>
      {/* Mobile backdrop */}
      <AnimatePresence>
        {isNavOpen && (
          <motion.div
            key="backdrop"
            initial={{ opacity: 0 }}
            animate={{ opacity: 1 }}
            exit={{ opacity: 0 }}
            onClick={() => setNavOpen(false)}
            className="fixed inset-0 bg-black/50 z-40 lg:hidden"
          />
        )}
      </AnimatePresence>

      {/* Drawer */}
      <motion.aside
        initial={false}
        animate={{ x: isNavOpen ? 0 : -320, width: isNavOpen ? 320 : 0 }}
        transition={{ type: 'tween', ease: 'easeOut', duration: 0.25 }}
        className={`fixed lg:relative inset-y-0 left-0 z-50 flex flex-col overflow-hidden border-r ${s.bg} ${s.text} ${s.border}`}
      >
        {/* Header */}
        <div className={`flex items-center justify-between px-4 py-3 border-b ${s.border}`}>
          <div className="flex flex-col">
            <span className="font-bold text-sm leading-tight">พระไตรปิฎก</span>
            <span className="text-[10px] opacity-60 leading-tight">ฉบับมหาจุฬาลงกรณราชวิทยาลัย</span>
          </div>
          {/* X button — ทุก screen size (ไม่ใช่แค่ mobile) */}
          <button onClick={() => setNavOpen(false)} className="p-1 rounded hover:bg-black/5">
            <X size={18} />
          </button>
        </div>

        {/* Tab bar */}
        <div className={`flex border-b ${s.border}`}>
          {tabs.map(tab => (
            <button
              key={tab.id}
              onClick={() => setActiveTab(tab.id)}
              className={`flex-1 flex flex-col items-center py-2.5 gap-1 text-[10px] font-bold uppercase tracking-wider transition-colors ${
                activeTab === tab.id
                  ? 'text-[#c8860a] border-b-2 border-[#c8860a]'
                  : 'text-[#888] hover:text-[#555]'
              }`}
            >
              {tab.icon}
              {tab.label}
            </button>
          ))}
        </div>

        {/* Content */}
        <div className="flex-1 overflow-y-auto">

          {/* ── TOC ── */}
          {activeTab === 'toc' && (
            <div className="p-3 space-y-3">
              <div>
                <label className="text-[10px] font-bold text-[#888] uppercase block mb-1">เลือกเล่ม</label>
                <select
                  value={currentVolume}
                  onChange={e => setVolume(Number(e.target.value))}
                  className={`w-full p-2 rounded-lg text-sm outline-none border ${s.border} ${s.input} ${s.text}`}
                >
                  {volumes.map(v => (
                    <option key={v.id} value={v.volume_number}>
                      เล่ม {v.volume_number} — {v.title}
                    </option>
                  ))}
                </select>
              </div>

              {loadingToc ? (
                <div className="flex justify-center py-12">
                  <Loader2 size={20} className="animate-spin text-[#c8860a]" />
                </div>
              ) : (
                <div className="space-y-0.5">
                  {toc.map((item, i) => (
                    <button
                      key={`${item.id}-${i}`}
                      onClick={() => jumpToPage(currentVolume, item.page_number)}
                      style={{ paddingLeft: `${(item.level ?? 0) * 12 + 8}px` }}
                      className="w-full text-left py-2 pr-3 text-sm rounded flex items-start gap-2 hover:bg-white/10 transition-colors group"
                    >
                      <ChevronRight size={12} className="mt-1 opacity-30 group-hover:opacity-80 flex-shrink-0 text-[#c8860a]" />
                      <span className="flex-1 leading-snug">{item.title}</span>
                      <span className="text-[10px] opacity-40 font-mono mt-0.5">{item.page_number}</span>
                    </button>
                  ))}
                </div>
              )}
            </div>
          )}

          {/* ── Search ── */}
          {activeTab === 'search' && (
            <div className="p-3 space-y-3">
              <form onSubmit={handleSearch} className="relative" ref={searchRef}>
                {/* Search input */}
                <input
                  type="text"
                  value={query}
                  onChange={e => handleQueryChange(e.target.value)}
                  onKeyDown={handleKeyDown}
                  onFocus={() => query && fetchSuggestions(query)}
                  placeholder="ค้นหาข้อความ เช่น อริยสัจ…"
                  autoComplete="off"
                  className={`w-full pl-9 pr-3 py-2.5 rounded-xl text-sm outline-none border ${s.border} ${s.input} ${s.text} focus:border-[#c8860a] transition-colors`}
                />
                <Search size={16} className="absolute left-3 top-3 text-[#888]" />
                {isLoading && <Loader2 size={14} className="absolute right-3 top-3.5 animate-spin text-[#c8860a]" />}

                {/* Suggestions dropdown */}
                <AnimatePresence>
                  {showSuggestions && suggestions.length > 0 && (
                    <motion.ul
                      initial={{ opacity: 0, y: -4 }}
                      animate={{ opacity: 1, y: 0 }}
                      exit={{ opacity: 0, y: -4 }}
                      transition={{ duration: 0.12 }}
                      className={`absolute left-0 right-0 top-full mt-1 z-50 rounded-xl border shadow-lg overflow-hidden ${s.bg} ${s.border}`}
                    >
                      {suggestions.map((term, i) => (
                        <li key={term}>
                          <button
                            type="button"
                            onMouseDown={() => selectSuggestion(term)}
                            className={`w-full text-left px-4 py-2.5 text-sm flex items-center gap-2 transition-colors ${
                              i === activeSuggestion
                                ? 'bg-[#c8860a]/15 text-[#c8860a]'
                                : `${s.text} hover:bg-white/10`
                            }`}
                          >
                            <Search size={12} className="opacity-40 flex-shrink-0" />
                            {term}
                          </button>
                        </li>
                      ))}
                    </motion.ul>
                  )}
                </AnimatePresence>
              </form>

              {/* Split search button with mode dropdown */}
              <div className="flex gap-1.5" ref={modeDropdownRef}>
                {/* Main search button */}
                <button
                  type="button"
                  onClick={() => { setModeDropdownOpen(false); performSearch(); }}
                  disabled={isLoading || isAiLoading || !query.trim()}
                  className="flex-1 flex items-center justify-center gap-1.5 py-2 px-3 rounded-xl text-sm font-bold bg-[#c8860a] hover:bg-[#a86e08] disabled:opacity-50 disabled:cursor-not-allowed text-white transition-colors"
                >
                  {isLoading ? (
                    <Loader2 size={14} className="animate-spin" />
                  ) : preferredMode === 'ai' ? (
                    <Sparkles size={14} />
                  ) : (
                    <Zap size={14} />
                  )}
                  {preferredMode === 'ai' ? 'ค้นหาความหมาย' : 'ค้นหาคำ'}
                </button>

                {/* Mode dropdown toggle */}
                <div className="relative">
                  <button
                    type="button"
                    onClick={() => setModeDropdownOpen(o => !o)}
                    className={`h-full px-2.5 rounded-xl border font-bold text-sm transition-colors ${s.border} ${s.tab} ${s.text} hover:border-[#c8860a]`}
                    title="เลือกโหมดการค้นหา"
                  >
                    <ChevronDown size={14} className={`transition-transform ${modeDropdownOpen ? 'rotate-180' : ''}`} />
                  </button>

                  <AnimatePresence>
                    {modeDropdownOpen && (
                      <motion.div
                        initial={{ opacity: 0, y: -4 }}
                        animate={{ opacity: 1, y: 0 }}
                        exit={{ opacity: 0, y: -4 }}
                        transition={{ duration: 0.12 }}
                        className={`absolute right-0 top-full mt-1 z-50 w-56 rounded-xl border shadow-lg overflow-hidden ${s.bg} ${s.border}`}
                      >
                        <button
                          type="button"
                          onClick={() => handleModeSelect('fts')}
                          className={`w-full text-left px-4 py-3 text-sm flex items-start gap-3 transition-colors ${
                            preferredMode === 'fts' ? 'bg-[#c8860a]/15' : `hover:bg-white/10`
                          }`}
                        >
                          <Zap size={16} className="mt-0.5 text-[#c8860a] flex-shrink-0" />
                          <div>
                            <p className={`font-bold text-sm ${s.text}`}>ค้นหาคำ <span className="text-[10px] font-normal opacity-60">(เร็ว)</span></p>
                            <p className="text-[10px] text-[#888] mt-0.5">ค้นชื่อคน สถานที่ คำบาลี</p>
                          </div>
                          {preferredMode === 'fts' && <span className="ml-auto text-[#c8860a] text-xs"></span>}
                        </button>
                        <div className={`h-px ${s.border} bg-current opacity-20`} />
                        <button
                          type="button"
                          onClick={() => handleModeSelect('ai')}
                          className={`w-full text-left px-4 py-3 text-sm flex items-start gap-3 transition-colors ${
                            preferredMode === 'ai' ? 'bg-[#c8860a]/15' : `hover:bg-white/10`
                          }`}
                        >
                          <Sparkles size={16} className="mt-0.5 text-[#c8860a] flex-shrink-0" />
                          <div>
                            <p className={`font-bold text-sm ${s.text}`}>ค้นหาความหมาย <span className="text-[10px] font-normal opacity-60">(AI)</span></p>
                            <p className="text-[10px] text-[#888] mt-0.5">ค้นหาตามใจความ เหมาะกับคำถาม</p>
                          </div>
                          {preferredMode === 'ai' && <span className="ml-auto text-[#c8860a] text-xs"></span>}
                        </button>
                      </motion.div>
                    )}
                  </AnimatePresence>
                </div>
              </div>

              {results.length > 0 && (
                <>
                  {/* Result count + mode badge */}
                  <div className="flex items-center justify-between">
                    <p className="text-[10px] text-[#888] font-bold uppercase">
                      พบ {totalResults} หน้า ({timeTaken.toFixed(0)}ms)
                    </p>
                    <span className={`text-[9px] font-bold px-2 py-0.5 rounded-full ${
                      searchMode === 'ai'
                        ? 'bg-purple-500/20 text-purple-400'
                        : 'bg-amber-500/20 text-amber-500'
                    }`}>
                      {searchMode === 'ai' ? '✦ AI' : '⚡ FTS'}
                    </span>
                  </div>

                  {/* Volume breakdown bars — แสดงทุกเล่มที่พบ ไม่จำกัดจำนวน */}
                  {Object.keys(breakdown).length > 1 && (
                    <div className={`p-3 rounded-xl border ${s.border} ${s.tab} space-y-2`}>
                      <p className="text-[10px] font-bold text-[#888] uppercase mb-1">การกระจายตัว</p>
                      {Object.entries(breakdown)
                        .sort((a, b) => Number(a[0]) - Number(b[0]))
                        .map(([volId, stats]) => {
                          const numericVolId = Number(volId);
                          const vol = volumes.find(v => v.id === numericVolId);
                          const maxCount = Math.max(...Object.values(breakdown).map(s => s.count));
                          const pct = (stats.count / maxCount) * 100;
                          const isExpanded = !!expandedVols[numericVolId];
                          const hasPages = stats.pages && stats.pages.length > 0;

                          return (
                            <div key={volId} className="space-y-1.5 border-b border-white/5 last:border-b-0 pb-1.5 last:pb-0">
                              {/* Header Bar Area (คลิกเพื่อขยาย) */}
                              <div
                                onClick={() => toggleVolExpand(numericVolId)}
                                className="w-full text-left space-y-0.5 cursor-pointer group/bar"
                              >
                                <div className="flex justify-between text-[9px] text-[#888] group-hover/bar:text-[#c8860a] transition-colors font-bold">
                                  <span className="truncate max-w-[185px]">เล่ม {vol?.volume_number} — {vol?.title}</span>
                                  <span className="flex items-center gap-1">
                                    <span>{stats.count}</span>
                                    {hasPages && (
                                      <ChevronDown size={10} className={`transform transition-transform ${isExpanded ? 'rotate-180 text-[#c8860a]' : 'opacity-40'}`} />
                                    )}
                                  </span>
                                </div>
                                <div className="h-1 bg-white/10 rounded-full">
                                  <motion.div
                                    initial={{ width: 0 }}
                                    animate={{ width: `${pct}%` }}
                                    className="h-full bg-[#c8860a] rounded-full group-hover/bar:bg-[#9a6307] transition-colors"
                                  />
                                </div>
                              </div>

                              {/* Expanded Page Chips */}
                              <AnimatePresence>
                                {isExpanded && hasPages && (
                                  <motion.div
                                    initial={{ height: 0, opacity: 0 }}
                                    animate={{ height: 'auto', opacity: 1 }}
                                    exit={{ height: 0, opacity: 0 }}
                                    transition={{ duration: 0.2 }}
                                    className="overflow-hidden pl-1 pr-1 pt-1"
                                  >
                                    <div className="flex flex-wrap gap-1 max-h-36 overflow-y-auto custom-scrollbar">
                                      {stats.pages?.map((pageNum) => (
                                        <button
                                          key={pageNum}
                                          type="button"
                                          onClick={() => vol && jumpToPage(vol.volume_number, pageNum, getHighlightString())}
                                          className="text-[9px] font-mono px-1.5 py-0.5 rounded bg-white/10 hover:bg-[#c8860a] hover:text-white transition-colors text-center min-w-[32px] border border-white/5"
                                        >
                                          {pageNum}
                                        </button>
                                      ))}
                                    </div>
                                  </motion.div>
                                )}
                              </AnimatePresence>
                            </div>
                          );
                        })}
                    </div>
                  )}

                  {/* Result cards */}
                  <div className="space-y-2">
                    {results.map((res, i) => {
                      const vol = volumes.find(v => v.id === res.volume_id);
                      const volNum = res.volume_number ?? vol?.volume_number;
                      return (
                        <div
                          key={i}
                          className={`w-full text-left p-3 rounded-xl border ${s.border} ${s.tab} hover:border-[#c8860a] hover:shadow-sm transition-all`}
                        >
                          {/* Google-style breadcrumb */}
                          <div className="flex items-center flex-wrap gap-x-0.5 gap-y-0.5 mb-1.5 text-[10px] font-bold">
                            {/* เล่ม N */}
                            <button
                              type="button"
                              onClick={() => volNum && jumpToPage(volNum, 1)}
                              className="text-[#c8860a] hover:underline hover:text-[#e0980f] transition-colors"
                            >
                              เล่ม {volNum}
                            </button>
                            {/* › พระสูตร/หัวข้อ */}
                            {res.toc_title && (
                              <>
                                <span className="text-[#888] mx-0.5"></span>
                                <button
                                  type="button"
                                  onClick={() => volNum && res.toc_page && jumpToPage(volNum, res.toc_page, getHighlightString())}
                                  className="text-[#c8860a] hover:underline hover:text-[#e0980f] transition-colors max-w-[160px] truncate text-left"
                                  title={res.toc_title}
                                >
                                  {res.toc_title}
                                </button>
                              </>
                            )}
                            {/* › หน้า N */}
                            <>
                              <span className="text-[#888] mx-0.5"></span>
                              <button
                                type="button"
                                onClick={() => volNum && jumpToPage(volNum, res.page_number, getHighlightString())}
                                className="text-[#888] hover:text-[#c8860a] hover:underline transition-colors"
                              >
                                หน้า {res.page_number}
                              </button>
                            </>
                          </div>
                          {/* Snippet — กดทั้ง card ไปหน้าที่พบ */}
                          <button
                            type="button"
                            onClick={() => volNum && jumpToPage(volNum, res.page_number, getHighlightString())}
                            className="w-full text-left"
                          >
                            <span
                              className="block text-xs text-[#aaa] line-clamp-3 leading-relaxed"
                              dangerouslySetInnerHTML={{ __html: res.snippet }}
                            />
                          </button>
                        </div>
                      );
                    })}
                  </div>

                  {/* AI upgrade button — แสดงเฉพาะเมื่อผลเป็น FTS และ preferredMode ไม่ใช่ ai */}
                  {searchMode === 'fts' && (
                    <button
                      type="button"
                      onClick={() => performAiSearch()}
                      disabled={isAiLoading}
                      className={`w-full flex items-center justify-center gap-2 py-2.5 px-4 rounded-xl border text-sm font-bold transition-all ${
                        isAiLoading
                          ? `${s.border} opacity-60 cursor-not-allowed ${s.text}`
                          : `border-purple-500/40 text-purple-400 hover:bg-purple-500/10 hover:border-purple-500/70`
                      }`}
                    >
                      {isAiLoading ? (
                        <><Loader2 size={14} className="animate-spin" /> กำลังค้นหาด้วย AI…</>
                      ) : (
                        <><Sparkles size={14} /> ค้นหาเพิ่มเติมด้วย AI</>
                      )}
                    </button>
                  )}
                </>
              )}

              {!isLoading && results.length === 0 && query && (
                <div className="text-center py-12 space-y-4">
                  <div className="opacity-40">
                    <Search size={36} className="mx-auto mb-3 opacity-20" />
                    <p className="text-sm">ไม่พบ «{query}»</p>
                  </div>
                  
                  {searchMode === 'fts' && (
                    <div className="pt-2 px-4">
                      <button
                        type="button"
                        onClick={() => performAiSearch()}
                        disabled={isAiLoading}
                        className={`w-full flex items-center justify-center gap-2 py-2.5 px-4 rounded-xl border text-sm font-bold transition-all ${
                          isAiLoading
                            ? `${s.border} opacity-60 cursor-not-allowed ${s.text}`
                            : `border-purple-500/40 text-purple-400 hover:bg-purple-500/10 hover:border-purple-500/70`
                        }`}
                      >
                        {isAiLoading ? (
                          <><Loader2 size={14} className="animate-spin" /> กำลังค้นหาด้วย AI…</>
                        ) : (
                          <><Sparkles size={14} /> ลองค้นหาความหมายด้วย AI</>
                        )}
                      </button>
                    </div>
                  )}
                </div>
              )}
            </div>
          )}

          {/* ── Overview ── */}
          {activeTab === 'overview' && (
            <div className="p-4 space-y-4 text-sm">
              <div className={`p-4 rounded-xl border ${s.border} ${s.tab}`}>
                <p className="font-bold text-[#c8860a] mb-1">พระไตรปิฎก AI Expert</p>
                <p className="text-xs text-[#888] leading-relaxed">
                  โครงการสืบค้นพระไตรปิฎก ฉบับมหาจุฬาลงกรณราชวิทยาลัย ด้วย AI และ Semantic Search
                </p>
              </div>
              <div>
                <p className="text-[10px] font-bold text-[#888] uppercase mb-2">เทคโนโลยีที่ใช้</p>
                <div className="grid grid-cols-2 gap-2 text-[10px] font-bold text-[#aaa]">
                  {['FastAPI + Python', 'Vite + React', 'SQLite FTS5', 'Turbovec + Reranker'].map(t => (
                    <div key={t} className={`p-2 rounded-lg border ${s.border} ${s.tab}`}>{t}</div>
                  ))}
                </div>
              </div>
              <div>
                <p className="text-[10px] font-bold text-[#888] uppercase mb-2">แหล่งข้อมูลอ้างอิง</p>
                <div className={`p-4 rounded-xl border ${s.border} ${s.tab} text-xs text-[#888] space-y-3`}>
                  <p className="leading-relaxed">
                    พระไตรปิฎกฉบับมหาจุฬาลงกรณราชวิทยาลัย อ้างอิงต้นฉบับดิจิทัลและเผยแผ่โดยมีเว็บไซต์{' '}
                    <a
                      href="https://84000.org"
                      target="_blank"
                      rel="noopener noreferrer"
                      className="text-[#c8860a] hover:underline font-semibold"
                    >
                      84000.org
                    </a>{' '}
                    เป็นแหล่งข้อมูลปฐมภูมิ (Primary Data Source) ของโครงการ
                  </p>
                  <div className="p-2.5 rounded bg-black/10 text-[10px] text-[#aaa] border-l-2 border-[#c8860a]/60 leading-relaxed italic">
                    <strong>ข้อสงวนสิทธิ์ (Disclaimer):</strong> “84000.org เป็นผู้เอื้อเฟื้อข้อมูลต้นฉบับ แต่ไม่มีส่วนรับผิดชอบต่อผลลัพธ์ที่เกิดจากการประมวลผลของระบบ AI”
                  </div>
                  <hr className={`border-t ${s.border}`} />
                  <a
                    href="https://84000.org/tipitaka/read/m_siri.php?B=1&siri=1&fontsz=0"
                    target="_blank"
                    rel="noopener noreferrer"
                    className="inline-flex items-center text-[#c8860a] hover:text-[#e0980f] font-semibold transition-colors gap-1 text-[11px]"
                  >
                    <span>เข้าชมเว็บไซต์ต้นฉบับ</span>
                    <ChevronRight size={12} className="mt-[1px]" />
                  </a>
                </div>
              </div>
            </div>
          )}
        </div>
      </motion.aside>
    </>
  );
};

export default NavDrawer;