File size: 176,879 Bytes
e218aaf
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
42905e5
e218aaf
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
42905e5
e218aaf
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
42905e5
 
 
 
 
 
 
 
 
e218aaf
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
42905e5
 
 
 
 
 
 
 
 
 
 
 
 
 
e218aaf
 
42905e5
e218aaf
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
42905e5
 
 
 
 
 
 
 
 
e218aaf
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
2374
2375
2376
2377
2378
2379
2380
2381
2382
2383
2384
2385
2386
2387
2388
2389
2390
2391
2392
2393
2394
2395
2396
2397
2398
2399
2400
2401
2402
2403
2404
2405
2406
2407
2408
2409
2410
2411
2412
2413
2414
2415
2416
2417
2418
2419
2420
2421
2422
2423
2424
2425
2426
2427
2428
2429
2430
2431
2432
2433
2434
2435
2436
2437
2438
2439
2440
2441
2442
2443
2444
2445
2446
2447
2448
2449
2450
2451
2452
2453
2454
2455
2456
2457
2458
2459
2460
2461
2462
2463
2464
2465
2466
2467
2468
2469
2470
2471
2472
2473
2474
2475
2476
2477
2478
2479
2480
2481
2482
2483
2484
2485
2486
2487
2488
2489
2490
2491
2492
2493
2494
2495
2496
2497
2498
2499
2500
2501
2502
2503
2504
2505
2506
2507
2508
2509
2510
2511
2512
2513
2514
2515
2516
2517
2518
2519
2520
2521
2522
2523
2524
2525
2526
2527
2528
2529
2530
2531
2532
2533
2534
2535
2536
2537
2538
2539
2540
2541
2542
2543
2544
2545
2546
2547
2548
2549
2550
2551
2552
2553
2554
2555
2556
2557
2558
2559
2560
2561
2562
2563
2564
2565
2566
2567
2568
2569
2570
2571
2572
2573
2574
2575
2576
2577
2578
2579
2580
2581
2582
2583
2584
2585
2586
2587
2588
2589
2590
2591
2592
2593
2594
2595
2596
2597
2598
2599
2600
2601
2602
2603
2604
2605
2606
2607
2608
2609
2610
2611
2612
2613
2614
2615
2616
2617
2618
2619
2620
2621
2622
2623
2624
2625
2626
2627
2628
2629
2630
2631
2632
2633
2634
2635
2636
2637
2638
2639
2640
2641
2642
2643
2644
2645
2646
2647
2648
2649
2650
2651
2652
2653
2654
2655
2656
2657
2658
2659
2660
2661
2662
2663
2664
2665
2666
2667
2668
2669
2670
2671
2672
2673
2674
2675
2676
2677
2678
2679
2680
2681
2682
2683
2684
2685
2686
2687
2688
2689
2690
2691
2692
2693
2694
2695
2696
2697
2698
2699
2700
2701
2702
2703
2704
2705
2706
2707
2708
2709
2710
2711
2712
2713
2714
2715
2716
2717
2718
2719
2720
2721
2722
2723
2724
2725
2726
2727
2728
2729
2730
2731
2732
2733
2734
2735
2736
2737
2738
2739
2740
2741
2742
2743
2744
2745
2746
2747
2748
2749
2750
2751
2752
2753
2754
2755
2756
2757
2758
2759
2760
2761
2762
2763
2764
2765
2766
2767
2768
2769
2770
2771
2772
2773
2774
2775
2776
2777
2778
2779
2780
2781
2782
2783
2784
2785
2786
2787
2788
2789
2790
2791
2792
2793
2794
2795
2796
2797
2798
2799
2800
2801
2802
2803
2804
2805
2806
2807
2808
2809
2810
2811
2812
2813
2814
2815
2816
2817
2818
2819
2820
2821
2822
2823
2824
2825
2826
2827
2828
2829
2830
2831
2832
2833
2834
2835
2836
2837
2838
2839
2840
2841
2842
2843
2844
2845
2846
2847
2848
2849
2850
2851
2852
2853
2854
2855
2856
2857
2858
2859
2860
2861
2862
2863
2864
2865
2866
2867
2868
2869
2870
2871
2872
2873
2874
2875
2876
2877
2878
2879
2880
2881
2882
2883
2884
2885
2886
2887
2888
2889
2890
2891
2892
2893
2894
2895
2896
2897
2898
2899
2900
2901
2902
2903
2904
2905
2906
2907
2908
2909
2910
2911
2912
2913
2914
2915
2916
2917
2918
2919
2920
2921
2922
2923
2924
2925
2926
2927
2928
2929
2930
2931
2932
2933
2934
2935
2936
2937
2938
2939
2940
2941
2942
2943
2944
2945
2946
2947
2948
2949
2950
2951
2952
2953
2954
2955
2956
2957
2958
2959
2960
2961
2962
2963
2964
2965
2966
2967
2968
2969
2970
2971
2972
2973
2974
2975
2976
2977
2978
2979
2980
2981
2982
2983
2984
2985
2986
2987
2988
2989
2990
2991
2992
2993
2994
2995
2996
2997
2998
2999
3000
3001
3002
3003
3004
3005
3006
3007
3008
3009
3010
3011
3012
3013
3014
3015
3016
3017
3018
3019
3020
3021
3022
3023
3024
3025
3026
3027
3028
3029
3030
3031
3032
3033
3034
3035
3036
3037
3038
3039
3040
3041
3042
3043
3044
3045
3046
3047
3048
3049
3050
3051
3052
3053
3054
3055
3056
3057
3058
3059
3060
3061
3062
3063
3064
3065
3066
3067
3068
3069
3070
3071
3072
3073
3074
3075
3076
3077
3078
3079
3080
3081
3082
3083
3084
3085
3086
3087
3088
3089
3090
3091
3092
3093
3094
3095
3096
3097
3098
3099
3100
3101
3102
3103
3104
3105
3106
3107
3108
3109
3110
3111
3112
3113
3114
3115
3116
3117
3118
3119
3120
3121
3122
3123
3124
3125
3126
3127
3128
3129
3130
3131
3132
3133
3134
3135
3136
3137
3138
3139
3140
3141
3142
3143
3144
3145
3146
3147
3148
3149
3150
3151
3152
3153
3154
3155
3156
3157
3158
3159
3160
3161
3162
3163
3164
3165
3166
3167
3168
3169
3170
3171
3172
3173
3174
3175
3176
3177
3178
3179
3180
3181
3182
3183
3184
3185
3186
3187
3188
3189
3190
3191
3192
3193
3194
3195
3196
3197
3198
3199
3200
3201
3202
3203
3204
3205
3206
3207
3208
3209
3210
3211
3212
3213
3214
3215
3216
3217
3218
3219
3220
3221
3222
3223
3224
3225
3226
3227
3228
3229
3230
3231
3232
3233
3234
3235
3236
3237
3238
3239
3240
3241
3242
3243
3244
3245
3246
3247
3248
3249
3250
3251
3252
3253
3254
3255
3256
3257
3258
3259
3260
3261
3262
3263
3264
3265
3266
3267
3268
3269
3270
3271
3272
3273
3274
3275
3276
3277
3278
3279
3280
3281
3282
3283
3284
3285
3286
3287
3288
3289
3290
3291
3292
3293
3294
3295
3296
3297
3298
3299
3300
3301
3302
3303
3304
3305
3306
3307
3308
3309
3310
3311
3312
3313
3314
3315
3316
3317
3318
3319
3320
3321
3322
3323
3324
3325
3326
3327
3328
3329
3330
3331
3332
3333
3334
3335
3336
3337
3338
3339
3340
3341
3342
3343
3344
3345
3346
3347
3348
3349
3350
3351
3352
3353
3354
3355
3356
3357
3358
3359
3360
3361
3362
3363
3364
3365
3366
3367
3368
3369
3370
3371
3372
3373
3374
3375
3376
3377
3378
3379
3380
3381
3382
3383
3384
3385
3386
3387
3388
3389
3390
3391
3392
3393
3394
3395
3396
3397
3398
3399
3400
3401
3402
3403
3404
3405
3406
3407
3408
3409
3410
3411
3412
3413
3414
3415
3416
3417
3418
3419
3420
3421
3422
3423
3424
3425
3426
3427
3428
3429
3430
3431
3432
3433
3434
3435
3436
3437
3438
3439
3440
3441
3442
3443
3444
3445
3446
3447
3448
3449
3450
3451
3452
3453
3454
3455
3456
3457
3458
3459
3460
3461
3462
3463
3464
3465
3466
3467
3468
3469
3470
3471
3472
3473
3474
3475
3476
3477
3478
3479
3480
3481
3482
3483
3484
3485
3486
3487
3488
3489
3490
3491
3492
3493
3494
3495
3496
3497
3498
3499
'use client';

import { useState, useEffect, useCallback, useRef, useMemo } from 'react';
import { useAppStore } from '@/stores/app-store';
import type { InvoiceRow, View, ChatMessage } from '@/stores/app-store';
import {
  Upload,
  FileText,
  BarChart3,
  Bot,
  Download,
  CreditCard,
  Key,
  User,
  LogOut,
  Menu,
  X,
  ChevronDown,
  ChevronUp,
  Search,
  Trash2,
  Send,
  Copy,
  Eye,
  EyeOff,
  AlertCircle,
  CheckCircle,
  Loader2,
  ArrowRight,
  Sparkles,
  Shield,
  Zap,
  Globe,
  Layers,
  RefreshCw,
  FileDown,
  FileJson,
  Quote,
  ChevronRight,
  ScanLine,
  Database,
  Lock,
  Mail,
  UserPlus,
  Inbox,
  MessageSquare,
  CircleDollarSign,
  CopyCheck,
  Terminal,
  ArrowLeft,
  Crown,
  Building2,
  Check,
  MousePointerClick,
  TrendingUp,
  Bell,
  Clock,
  Pencil,
  LogIn,
  Sun,
  Moon,
  ArrowUpDown,
  Command,
  Settings,
} from 'lucide-react';
import type { LucideIcon } from 'lucide-react';
import { toast } from 'sonner';
import {
  AreaChart, Area, BarChart, Bar, PieChart, Pie, Cell,
  XAxis, YAxis, Tooltip, ResponsiveContainer, CartesianGrid,
} from 'recharts';
import { PLAN_LIMITS, PLAN_LABELS, PLAN_PRICES } from '@/lib/auth';
import { useTheme } from 'next-themes';

// ─── Helpers ────────────────────────────────────────────────────────────────────

const currencyFmt = new Intl.NumberFormat('en-US', {
  style: 'currency',
  currency: 'USD',
});

function formatCurrency(amount: number | null, currency?: string): string {
  if (amount === null || amount === undefined) return '--';
  try {
    return new Intl.NumberFormat('en-US', {
      style: 'currency',
      currency: currency || 'USD',
    }).format(amount);
  } catch {
    return currency ? `${currency} ${amount.toFixed(2)}` : `$${amount.toFixed(2)}`;
  }
}

function getPasswordStrength(pw: string): { label: string; color: string; width: string } {
  if (!pw) return { label: '', color: 'bg-zinc-700', width: 'w-0' };
  let score = 0;
  if (pw.length >= 6) score++;
  if (pw.length >= 10) score++;
  if (/[A-Z]/.test(pw)) score++;
  if (/[0-9]/.test(pw)) score++;
  if (/[^A-Za-z0-9]/.test(pw)) score++;
  if (score <= 2) return { label: 'Weak', color: 'bg-red-500', width: 'w-1/3' };
  if (score <= 3) return { label: 'Medium', color: 'bg-amber-500', width: 'w-2/3' };
  return { label: 'Strong', color: 'bg-green-500', width: 'w-full' };
}

const DASH_TABS = [
  { id: 'upload', label: 'Upload', icon: Upload },
  { id: 'invoices', label: 'Invoices', icon: FileText },
  { id: 'analytics', label: 'Analytics', icon: BarChart3 },
  { id: 'chat', label: 'AI Chat', icon: Bot },
  { id: 'export', label: 'Export', icon: Download },
  { id: 'upgrade', label: 'Upgrade', icon: Crown },
  { id: 'api', label: 'API Access', icon: Key },
  { id: 'profile', label: 'Profile', icon: User },
  { id: 'activity', label: 'Activity', icon: Clock },
  { id: 'settings', label: 'Settings', icon: Settings },
] as const;

function relativeTime(dateStr: string): string {
  const now = Date.now();
  const then = new Date(dateStr).getTime();
  const diffMs = now - then;
  const diffMins = Math.floor(diffMs / 60000);
  if (diffMins < 1) return 'Just now';
  if (diffMins < 60) return `${diffMins}m ago`;
  const diffHours = Math.floor(diffMins / 60);
  if (diffHours < 24) return `${diffHours}h ago`;
  const diffDays = Math.floor(diffHours / 24);
  if (diffDays < 30) return `${diffDays}d ago`;
  return new Date(dateStr).toLocaleDateString('en-US', { month: 'short', day: 'numeric' });
}

// ─── Main Page Component ────────────────────────────────────────────────────────

export default function Home() {
  const store = useAppStore();
  const { theme, setTheme } = useTheme();

  // ── Local Auth State ──
  const [authMode, setAuthMode] = useState<'login' | 'signup'>('login');
  const [loginEmail, setLoginEmail] = useState('');
  const [loginPassword, setLoginPassword] = useState('');
  const [signupName, setSignupName] = useState('');
  const [signupEmail, setSignupEmail] = useState('');
  const [signupPassword, setSignupPassword] = useState('');
  const [signupConfirm, setSignupConfirm] = useState('');
  const [signupTerms, setSignupTerms] = useState(false);
  const [authLoading, setAuthLoading] = useState(false);
  const [authError, setAuthError] = useState('');

  // ── Dashboard Local State ──
  const [invoiceSearch, setInvoiceSearch] = useState('');
  const [invoiceFilter, setInvoiceFilter] = useState('all');
  const [expandedInvoice, setExpandedInvoice] = useState<string | null>(null);
  const [selectedInvoices, setSelectedInvoices] = useState<Set<string>>(new Set());
  const [chatInput, setChatInput] = useState('');
  const [showApiKey, setShowApiKey] = useState(false);
  const [profileCurrentPw, setProfileCurrentPw] = useState('');
  const [profileNewPw, setProfileNewPw] = useState('');
  const [profileConfirmPw, setProfileConfirmPw] = useState('');
  const [profilePwLoading, setProfilePwLoading] = useState(false);

  const [showScrollTop, setShowScrollTop] = useState(false);

  // ── Landing Mobile Nav ──
  const [mobileMenuOpen, setMobileMenuOpen] = useState(false);
  const [dashMobileMenu, setDashMobileMenu] = useState(false);

  const [editingInvoice, setEditingInvoice] = useState<InvoiceRow | null>(null);
  const [editForm, setEditForm] = useState<Record<string, string>>({});
  const [editSaving, setEditSaving] = useState(false);
  const [dateFrom, setDateFrom] = useState('');
  const [dateTo, setDateTo] = useState('');
  const [sortField, setSortField] = useState<string>('createdAt');
  const [sortDir, setSortDir] = useState<'asc' | 'desc'>('desc');

  // ── Activity Items (real API) ──
  const [activityLogs, setActivityLogs] = useState<Array<{id:string;type:string;title:string;description:string;createdAt:string;metadata:string|null}>>([]);
  const [activityLoading, setActivityLoading] = useState(false);

  const fetchActivity = useCallback(async () => {
    setActivityLoading(true);
    try {
      const res = await fetch('/api/activity');
      if (res.ok) {
        const data = await res.json();
        setActivityLogs(data.activities);
      }
    } catch { /* silent */ }
    finally { setActivityLoading(false); }
  }, []);

  useEffect(() => {
    if (store.view === 'dashboard' && store.activeDashTab === 'activity') {
      fetchActivity();
    }
  }, [store.view, store.activeDashTab, fetchActivity]);

  const [showNotifications, setShowNotifications] = useState(false);
  const [showShortcuts, setShowShortcuts] = useState(false);
  const [showCommandPalette, setShowCommandPalette] = useState(false);
  const [commandSearch, setCommandSearch] = useState('');

  const notifications = [
    ...(store.invoices.length > 0 ? [{
      id: '1',
      title: 'Invoices processed',
      description: `${store.invoices.length} invoice(s) in your account`,
      time: 'Just now',
      icon: FileText,
      color: 'text-teal-500',
    }] : []),
    {
      id: '2',
      title: 'Welcome to OmniParse',
      description: 'Upload your first invoice to get started',
      time: 'Getting started',
      icon: Sparkles,
      color: 'text-amber-500',
    },
    {
      id: '3',
      title: 'AI Chat available',
      description: store.user?.plan === 'free'
        ? 'Upgrade to Pro to unlock AI Chat'
        : 'Ask questions about your invoices',
      time: 'Feature',
      icon: Bot,
      color: 'text-amber-500',
    },
  ];

  // ── Settings State ──
  const [dateFormat, setDateFormat] = useState<'MM/DD/YYYY' | 'DD/MM/YYYY' | 'YYYY-MM-DD'>('MM/DD/YYYY');
  const [defaultCurrency, setDefaultCurrency] = useState('USD');
  const [compactView, setCompactView] = useState(false);
  const [settingsSaved, setSettingsSaved] = useState(false);

  const saveSettings = useCallback(() => {
    setSettingsSaved(true);
    toast.success('Settings saved successfully');
    setTimeout(() => setSettingsSaved(false), 2000);
  }, []);

  // ── Upload State ──
  const [selectedFiles, setSelectedFiles] = useState<File[]>([]);
  const [dragOver, setDragOver] = useState(false);
  const fileInputRef = useRef<HTMLInputElement>(null);

  const toggleSort = useCallback((field: string) => {
    if (sortField === field) {
      setSortDir(d => d === 'asc' ? 'desc' : 'asc');
    } else {
      setSortField(field);
      setSortDir('asc');
    }
  }, [sortField]);

  // ── Derived invoice data (before callbacks so they can reference it) ──
  const filteredInvoices = useMemo(() => {
    let result = store.invoices.filter((inv) => {
      if (invoiceSearch && !inv.vendor?.toLowerCase().includes(invoiceSearch.toLowerCase())) return false;
      if (invoiceFilter === 'done') return inv.status === 'done' && !inv.isDuplicate;
      if (invoiceFilter === 'review') return inv.status === 'review' || inv.isDuplicate;
      if (invoiceFilter === 'duplicates') return inv.isDuplicate;
      if (dateFrom && inv.invDate && inv.invDate < dateFrom) return false;
      if (dateTo && inv.invDate && inv.invDate > dateTo) return false;
      return true;
    });
    result.sort((a, b) => {
      let cmp = 0;
      const f = sortField;
      if (f === 'vendor') cmp = (a.vendor || '').localeCompare(b.vendor || '');
      else if (f === 'invNumber') cmp = (a.invNumber || '').localeCompare(b.invNumber || '');
      else if (f === 'invDate') cmp = (a.invDate || '').localeCompare(b.invDate || '');
      else if (f === 'amount') cmp = (a.amount ?? 0) - (b.amount ?? 0);
      else if (f === 'total') cmp = (a.total ?? 0) - (b.total ?? 0);
      else if (f === 'confidence') cmp = (a.confidence ?? 0) - (b.confidence ?? 0);
      else if (f === 'status') cmp = a.status.localeCompare(b.status);
      else cmp = new Date(b.createdAt).getTime() - new Date(a.createdAt).getTime();
      return sortDir === 'asc' ? cmp : -cmp;
    });
    return result;
  }, [store.invoices, invoiceSearch, invoiceFilter, dateFrom, dateTo, sortField, sortDir]);

  // ── Chat scroll ref ──
  const chatEndRef = useRef<HTMLDivElement>(null);

  // ── Scroll refs for landing ──
  const featuresRef = useRef<HTMLElement>(null);
  const pricingRef = useRef<HTMLElement>(null);

  // ──────────────────────────────────────────────────────────────────────────────
  // API Callbacks
  // ──────────────────────────────────────────────────────────────────────────────

  const fetchMe = useCallback(async () => {
    const { setUser, setView, setLoading } = useAppStore.getState();
    try {
      const res = await fetch('/api/auth/me');
      if (res.ok) {
        const data = await res.json();
        setUser(data.user);
        setView('dashboard');
      } else {
        setUser(null);
        setLoading(false);
      }
    } catch {
      setUser(null);
      setLoading(false);
    }
  }, []);

  const fetchLogin = useCallback(async () => {
    setAuthError('');
    setAuthLoading(true);
    try {
      const res = await fetch('/api/auth/login', {
        method: 'POST',
        headers: { 'Content-Type': 'application/json' },
        body: JSON.stringify({ email: loginEmail, password: loginPassword }),
      });
      const data = await res.json();
      if (!res.ok) {
        setAuthError(data.error || 'Login failed');
        setAuthLoading(false);
        return;
      }
      const { setUser, setView } = useAppStore.getState();
      setUser(data.user);
      setView('dashboard');
      toast.success('Welcome back!');
    } catch {
      setAuthError('Network error. Please try again.');
    } finally {
      setAuthLoading(false);
    }
  }, [loginEmail, loginPassword]);

  const fetchSignup = useCallback(async () => {
    setAuthError('');
    if (signupPassword !== signupConfirm) {
      setAuthError('Passwords do not match');
      return;
    }
    setAuthLoading(true);
    try {
      const res = await fetch('/api/auth/signup', {
        method: 'POST',
        headers: { 'Content-Type': 'application/json' },
        body: JSON.stringify({ name: signupName, email: signupEmail, password: signupPassword, termsAccepted: true }),
      });
      const data = await res.json();
      if (!res.ok) {
        setAuthError(data.error || 'Signup failed');
        setAuthLoading(false);
        return;
      }
      const { setUser, setView } = useAppStore.getState();
      setUser(data.user);
      setView('dashboard');
      toast.success('Account created successfully!');
    } catch {
      setAuthError('Network error. Please try again.');
    } finally {
      setAuthLoading(false);
    }
  }, [signupName, signupEmail, signupPassword, signupConfirm]);

  const fetchLogout = useCallback(() => {
    fetch('/api/auth/logout', { method: 'POST' }).catch(() => {});
    toast.success('Logged out');
    window.location.href = '/';
  }, []);

  const fetchInvoices = useCallback(async (filter?: string) => {
    const { setLoading, setInvoices } = useAppStore.getState();
    setLoading(true);
    try {
      const params = filter && filter !== 'all' ? `?filter=${filter}` : '';
      const res = await fetch(`/api/invoices${params}`);
      if (res.ok) {
        const data = await res.json();
        setInvoices(data.invoices);
      }
    } catch {
      toast.error('Failed to load invoices');
    } finally {
      setLoading(false);
    }
  }, []);

  const fetchUpload = useCallback(async () => {
    if (selectedFiles.length === 0) return;
    const { setUploadLoading, setUploadResults } = useAppStore.getState();
    setUploadLoading(true);
    try {
      const formData = new FormData();
      for (const file of selectedFiles) {
        formData.append('files', file);
      }
      const res = await fetch('/api/invoices/upload', {
        method: 'POST',
        body: formData,
      });
      const data = await res.json();
      if (!res.ok) {
        toast.error(data.error || 'Upload failed');
        setUploadLoading(false);
        return;
      }
      setUploadResults(data.invoices);
      setSelectedFiles([]);
      toast.success(`${data.invoices.length} invoice(s) processed successfully!`);
      fetchInvoices(invoiceFilter);
    } catch {
      toast.error('Upload failed. Please try again.');
    } finally {
      setUploadLoading(false);
    }
  }, [selectedFiles, invoiceFilter, fetchInvoices]);

  const fetchDeleteInvoice = useCallback(async (id: string) => {
    if (!window.confirm('Are you sure you want to delete this invoice? This action cannot be undone.')) return;
    try {
      const res = await fetch('/api/invoices', {
        method: 'DELETE',
        headers: { 'Content-Type': 'application/json' },
        body: JSON.stringify({ id }),
      });
      if (res.ok) {
        toast.success('Invoice deleted');
        setSelectedInvoices(prev => { const n = new Set(prev); n.delete(id); return n; });
        fetchInvoices(invoiceFilter);
        if (expandedInvoice === id) setExpandedInvoice(null);
      } else {
        const data = await res.json();
        toast.error(data.error || 'Failed to delete');
      }
    } catch {
      toast.error('Failed to delete invoice');
    }
  }, [invoiceFilter, expandedInvoice, fetchInvoices]);

  const fetchBatchDelete = useCallback(async () => {
    const ids = Array.from(selectedInvoices);
    if (ids.length === 0) return;
    if (!window.confirm(`Delete ${ids.length} invoice(s)? This action cannot be undone.`)) return;
    try {
      await Promise.all(ids.map(id =>
        fetch('/api/invoices', {
          method: 'DELETE',
          headers: { 'Content-Type': 'application/json' },
          body: JSON.stringify({ id }),
        })
      ));
      toast.success(`${ids.length} invoice(s) deleted`);
      setSelectedInvoices(new Set());
      fetchInvoices(invoiceFilter);
    } catch {
      toast.error('Failed to delete some invoices');
    }
  }, [selectedInvoices, invoiceFilter, fetchInvoices]);

  const toggleSelect = useCallback((id: string) => {
    setSelectedInvoices(prev => {
      const next = new Set(prev);
      if (next.has(id)) next.delete(id);
      else next.add(id);
      return next;
    });
  }, []);

  const toggleSelectAll = useCallback(() => {
    if (selectedInvoices.size === filteredInvoices.length) {
      setSelectedInvoices(new Set());
    } else {
      setSelectedInvoices(new Set(filteredInvoices.map(i => i.id)));
    }
  }, [selectedInvoices.size, filteredInvoices]);

  const fetchChat = useCallback(async () => {
    if (!chatInput.trim()) return;
    const { addChatMessage, chatHistory, setChatLoading } = useAppStore.getState();
    const userMsg: ChatMessage = { role: 'user', content: chatInput.trim() };
    addChatMessage(userMsg);
    const historyForApi = [...chatHistory, userMsg].map((m) => ({ role: m.role, content: m.content }));
    setChatInput('');
    setChatLoading(true);
    try {
      const res = await fetch('/api/chat', {
        method: 'POST',
        headers: { 'Content-Type': 'application/json' },
        body: JSON.stringify({ message: userMsg.content, history: historyForApi.slice(0, -1) }),
      });
      const data = await res.json();
      if (!res.ok) {
        addChatMessage({ role: 'assistant', content: data.error || 'Something went wrong.' });
      } else {
        addChatMessage({ role: 'assistant', content: data.reply });
      }
    } catch {
      addChatMessage({ role: 'assistant', content: 'Network error. Please try again.' });
    } finally {
      setChatLoading(false);
    }
  }, [chatInput]);

  const exportCsv = useCallback(() => {
    window.open('/api/invoices/export/csv', '_blank');
    toast.success('CSV download started');
  }, []);

  const exportJson = useCallback(() => {
    const { user } = useAppStore.getState();
    if (user?.plan === 'free') {
      toast.error('JSON export is available on Pro+ plans. Please upgrade.');
      return;
    }
    window.open('/api/invoices/export/json', '_blank');
    toast.success('JSON download started');
  }, []);

  const openEditModal = useCallback((inv: InvoiceRow) => {
    setEditingInvoice(inv);
    setEditForm({
      vendor: inv.vendor || '',
      invNumber: inv.invNumber || '',
      invDate: inv.invDate || '',
      dueDate: inv.dueDate || '',
      amount: String(inv.amount || ''),
      vatAmount: String(inv.vatAmount || ''),
      total: String(inv.total || ''),
      currency: inv.currency,
      status: inv.status === 'review' ? 'review' : 'done',
    });
  }, []);

  const saveEdit = useCallback(async () => {
    if (!editingInvoice) return;
    setEditSaving(true);
    try {
      const res = await fetch('/api/invoices/update', {
        method: 'POST',
        headers: { 'Content-Type': 'application/json' },
        body: JSON.stringify({ id: editingInvoice.id, ...editForm }),
      });
      if (res.ok) {
        toast.success('Invoice updated');
        setEditingInvoice(null);
        fetchInvoices(invoiceFilter);
      } else {
        const data = await res.json();
        toast.error(data.error || 'Update failed');
      }
    } catch {
      toast.error('Failed to update invoice');
    } finally {
      setEditSaving(false);
    }
  }, [editingInvoice, editForm, invoiceFilter, fetchInvoices]);

  // ── Effects ──

  useEffect(() => {
    fetchMe();
  }, [fetchMe]);

  useEffect(() => {
    if (store.view === 'dashboard' && store.activeDashTab === 'invoices') {
      fetchInvoices(invoiceFilter);
    }
  }, [store.view, store.activeDashTab, invoiceFilter, fetchInvoices]);

  useEffect(() => {
    chatEndRef.current?.scrollIntoView({ behavior: 'smooth' });
  }, [store.chatHistory, store.chatLoading]);

  // ── Drag-and-drop handlers ──

  const handleDragOver = useCallback((e: React.DragEvent) => {
    e.preventDefault();
    setDragOver(true);
  }, []);

  const handleDragLeave = useCallback((e: React.DragEvent) => {
    e.preventDefault();
    setDragOver(false);
  }, []);

  const handleDrop = useCallback((e: React.DragEvent) => {
    e.preventDefault();
    setDragOver(false);
    const files = Array.from(e.dataTransfer.files).filter(
      (f) => f.type === 'application/pdf' || f.type.startsWith('image/')
    );
    setSelectedFiles((prev) => [...prev, ...files]);
  }, []);

  const handleFileSelect = useCallback((e: React.ChangeEvent<HTMLInputElement>) => {
    if (e.target.files) {
      setSelectedFiles((prev) => [...prev, ...Array.from(e.target.files!)]);
    }
  }, []);

  const removeFile = useCallback((index: number) => {
    setSelectedFiles((prev) => prev.filter((_, i) => i !== index));
  }, []);

  // ── Scroll helpers ──

  const scrollToFeatures = useCallback(() => {
    featuresRef.current?.scrollIntoView({ behavior: 'smooth' });
    setMobileMenuOpen(false);
  }, []);

  const scrollToPricing = useCallback(() => {
    pricingRef.current?.scrollIntoView({ behavior: 'smooth' });
    setMobileMenuOpen(false);
  }, []);

  // ── Keyboard shortcut for search (decorative hint, but functional) ──
  useEffect(() => {
    const handler = (e: KeyboardEvent) => {
      if ((e.ctrlKey || e.metaKey) && e.key === 'k') {
        e.preventDefault();
        const { view } = useAppStore.getState();
        if (view === 'dashboard') {
          setShowCommandPalette(true);
        }
      }
      if ((e.ctrlKey || e.metaKey) && e.key === '/') {
        e.preventDefault();
        const { view } = useAppStore.getState();
        if (view === 'dashboard') {
          setShowShortcuts(true);
        }
      }
    };
    window.addEventListener('keydown', handler);
    return () => window.removeEventListener('keydown', handler);
  }, []);

  useEffect(() => {
    const handleScroll = () => {
      setShowScrollTop(window.scrollY > 400);
    };
    window.addEventListener('scroll', handleScroll, { passive: true });
    return () => window.removeEventListener('scroll', handleScroll);
  }, []);

  // ── Loading screen ──

  if (store.isLoading) {
    return (
      <div className="min-h-screen flex items-center justify-center">
        <Loader2 className="w-8 h-8 animate-spin text-amber-500" />
      </div>
    );
  }

  // ──────────────────────────────────────────────────────────────────────────────
  // LANDING VIEW
  // ──────────────────────────────────────────────────────────────────────────────

  if (store.view === 'landing') {
    return (
      <div className="min-h-screen flex flex-col noise-overlay">
        {/* ── Navbar ── */}
        <header className="fixed top-0 left-0 right-0 z-50 nav-blur">
          <nav className="max-w-7xl mx-auto px-4 sm:px-6 lg:px-8 h-16 flex items-center justify-between">
            <div className="flex items-center gap-2">
              <div className="logo-glow w-8 h-8 rounded-lg bg-amber-500 flex items-center justify-center">
                <ScanLine className="w-5 h-5 text-zinc-900" />
              </div>
              <span className="text-lg font-bold text-foreground">OmniParse</span>
            </div>
            <div className="hidden md:flex items-center gap-8">
              <button onClick={scrollToFeatures} className="btn-ghost text-sm">
                Features
              </button>
              <button onClick={scrollToPricing} className="btn-ghost text-sm">
                Pricing
              </button>
            </div>
            <div className="hidden md:flex items-center gap-3">
              <button
                onClick={() => setTheme(theme === 'dark' ? 'light' : 'dark')}
                className="p-2 rounded-lg text-muted-foreground hover:text-foreground hover:bg-white/[0.04] transition-colors"
                aria-label={theme === 'dark' ? 'Switch to light mode' : 'Switch to dark mode'}
              >
                {theme === 'dark' ? <Sun className="w-4 h-4" /> : <Moon className="w-4 h-4" />}
              </button>
              <button
                onClick={() => { setAuthMode('login'); store.setView('auth'); }}
                className="btn-ghost text-sm"
              >
                Log in
              </button>
              <button
                onClick={() => { setAuthMode('signup'); store.setView('auth'); }}
                className="btn-primary text-sm"
              >
                Get Started
              </button>
            </div>
            <button
              className="md:hidden text-muted-foreground hover:text-foreground"
              onClick={() => setMobileMenuOpen(!mobileMenuOpen)}
              aria-label="Toggle navigation menu"
            >
              {mobileMenuOpen ? <X className="w-6 h-6" /> : <Menu className="w-6 h-6" />}
            </button>
          </nav>
          {mobileMenuOpen && (
            <div className="md:hidden border-t border-white/[0.06] bg-background/95 backdrop-blur-md fade-in mobile-nav-slide">
              <div className="px-4 py-4 space-y-3">
                <button onClick={scrollToFeatures} className="block w-full text-left text-sm text-muted-foreground hover:text-foreground py-2">
                  Features
                </button>
                <button onClick={scrollToPricing} className="block w-full text-left text-sm text-muted-foreground hover:text-foreground py-2">
                  Pricing
                </button>
                <div className="pt-2 border-t border-white/[0.06] space-y-2">
                  <button
                    onClick={() => { setTheme(theme === 'dark' ? 'light' : 'dark'); }}
                    className="flex items-center gap-3 w-full text-sm text-muted-foreground hover:text-foreground py-2"
                  >
                    {theme === 'dark' ? <Sun className="w-4 h-4" /> : <Moon className="w-4 h-4" />}
                    {theme === 'dark' ? 'Light mode' : 'Dark mode'}
                  </button>
                  <button
                    onClick={() => { setAuthMode('login'); store.setView('auth'); setMobileMenuOpen(false); }}
                    className="block w-full text-sm text-muted-foreground hover:text-foreground py-2"
                  >
                    Log in
                  </button>
                  <button
                    onClick={() => { setAuthMode('signup'); store.setView('auth'); setMobileMenuOpen(false); }}
                    className="btn-primary text-sm block w-full text-center"
                  >
                    Get Started
                  </button>
                </div>
              </div>
            </div>
          )}
        </header>

        <main className="flex-1">
          {/* ── Hero Section ── */}
          <section className="relative min-h-[90vh] flex items-center justify-center grid-bg hero-grid-enhanced overflow-hidden pt-16">
            <div className="hero-orb" />
            <div className="hero-orb-2" />
            <div className="relative z-10 max-w-4xl mx-auto px-4 text-center">
              <div className="inline-flex items-center gap-2 px-4 py-1.5 rounded-full border border-white/[0.08] bg-white/[0.03] text-sm text-muted-foreground mb-8 fade-in float-animation">
                <Sparkles className="w-4 h-4 text-amber-500" />
                Powered by advanced AI extraction
              </div>
              <h1 className="text-4xl sm:text-5xl md:text-6xl lg:text-7xl font-bold tracking-tight mb-6 fade-in">
                Invoice Processing,{' '}
                <span className="gradient-text">Reimagined</span>
              </h1>
              <p className="text-lg sm:text-xl text-muted-foreground max-w-2xl mx-auto mb-10 fade-in">
                Upload any invoice and let AI extract vendor details, amounts, dates, and line items in seconds. Export structured data to CSV, JSON, or connect via API.
              </p>
              <div className="flex flex-col sm:flex-row items-center justify-center gap-4 fade-in">
                <button
                  onClick={() => { setAuthMode('signup'); store.setView('auth'); }}
                  className="btn-shine pulse-glow flex items-center gap-2 px-8 py-3.5 rounded-xl bg-amber-500 text-zinc-900 font-semibold text-base hover:bg-amber-400 transition-all hover:shadow-lg hover:shadow-amber-500/20"
                >
                  Start Free Trial
                  <ArrowRight className="w-5 h-5" />
                </button>
                <button
                  onClick={scrollToFeatures}
                  className="flex items-center gap-2 px-8 py-3.5 rounded-xl border border-white/[0.1] text-foreground font-medium text-base hover:bg-white/[0.04] transition-all"
                >
                  See How It Works
                  <ChevronDown className="w-5 h-5" />
                </button>
              </div>
            </div>
            <div className="hidden lg:block absolute top-32 right-16 float-animation-delayed">
              <div className="glass-card p-3 flex items-center gap-2 text-xs text-muted-foreground">
                <Shield className="w-4 h-4 text-teal-500" />
                <span>SOC 2 Compliant</span>
              </div>
            </div>
          </section>

          {/* ── Stats Bar ── */}
          <section className="border-y border-white/[0.06] bg-white/[0.01]">
            <div className="max-w-7xl mx-auto px-4 sm:px-6 lg:px-8 py-8 grid grid-cols-1 sm:grid-cols-3 gap-8">
              <div className="stat-animate text-center">
                <div className="text-3xl sm:text-4xl font-bold text-foreground">10K+</div>
                <div className="text-sm text-muted-foreground mt-1">Invoices Processed</div>
              </div>
              <div className="stat-animate text-center">
                <div className="text-3xl sm:text-4xl font-bold text-foreground">99.2%</div>
                <div className="text-sm text-muted-foreground mt-1">Accuracy Rate</div>
              </div>
              <div className="stat-animate text-center">
                <div className="text-3xl sm:text-4xl font-bold text-foreground">500+</div>
                <div className="text-sm text-muted-foreground mt-1">Companies Trust Us</div>
              </div>
            </div>
          </section>

          <hr className="section-glow-divider my-0" />

          {/* ── How It Works ── */}
          <section className="py-20 sm:py-28">
            <div className="max-w-7xl mx-auto px-4 sm:px-6 lg:px-8">
              <div className="text-center mb-16">
                <h2 className="text-3xl sm:text-4xl font-bold mb-4">How It Works</h2>
                <p className="text-muted-foreground text-lg max-w-xl mx-auto">Three simple steps to transform your invoices into structured data</p>
              </div>
              <div className="relative grid grid-cols-1 md:grid-cols-3 gap-8">
                <article className="step-connector text-center section-reveal">
                  <div className="w-14 h-14 rounded-full bg-amber-500/10 border border-amber-500/20 flex items-center justify-center mx-auto mb-5">
                    <span className="text-xl font-bold text-amber-500">1</span>
                  </div>
                  <h3 className="text-xl font-semibold mb-3">Upload</h3>
                  <p className="text-muted-foreground text-sm leading-relaxed">
                    Drag and drop your invoice PDFs or images. We support batch uploads of multiple files at once.
                  </p>
                </article>
                <article className="step-connector text-center section-reveal" style={{ animationDelay: '0.15s' }}>
                  <div className="w-14 h-14 rounded-full bg-teal-500/10 border border-teal-500/20 flex items-center justify-center mx-auto mb-5">
                    <span className="text-xl font-bold text-teal-500">2</span>
                  </div>
                  <h3 className="text-xl font-semibold mb-3">Extract</h3>
                  <p className="text-muted-foreground text-sm leading-relaxed">
                    Our AI automatically extracts vendor details, line items, amounts, dates, and detects duplicates.
                  </p>
                </article>
                <article className="step-connector text-center section-reveal" style={{ animationDelay: '0.3s' }}>
                  <div className="w-14 h-14 rounded-full bg-amber-500/10 border border-amber-500/20 flex items-center justify-center mx-auto mb-5">
                    <span className="text-xl font-bold text-amber-500">3</span>
                  </div>
                  <h3 className="text-xl font-semibold mb-3">Export</h3>
                  <p className="text-muted-foreground text-sm leading-relaxed">
                    Download your data as CSV or JSON, or integrate directly with your systems using our REST API.
                  </p>
                </article>
              </div>
            </div>
          </section>

          <hr className="section-glow-divider my-0" />

          {/* ── Features Section ── */}
          <section ref={featuresRef} className="py-20 sm:py-28 bg-white/[0.01]">
            <div className="max-w-7xl mx-auto px-4 sm:px-6 lg:px-8">
              <div className="text-center mb-16">
                <h2 className="text-3xl sm:text-4xl font-bold mb-4">Everything You Need</h2>
                <p className="text-muted-foreground text-lg max-w-xl mx-auto">Powerful features designed to streamline your invoice processing workflow</p>
              </div>
              <div className="grid grid-cols-1 sm:grid-cols-2 lg:grid-cols-3 gap-6">
                <article className="feature-card-gradient feature-amber p-6 card-enter">
                  <div className="feature-icon w-11 h-11 rounded-lg flex items-center justify-center mb-4">
                    <Zap className="w-5 h-5" />
                  </div>
                  <h3 className="text-base font-semibold mb-2">Instant Extraction</h3>
                  <p className="text-sm text-muted-foreground leading-relaxed">
                    Extract all invoice fields in under 3 seconds per document with near-perfect accuracy.
                  </p>
                </article>
                <article className="feature-card-gradient feature-teal p-6 card-enter">
                  <div className="feature-icon w-11 h-11 rounded-lg flex items-center justify-center mb-4">
                    <Shield className="w-5 h-5" />
                  </div>
                  <h3 className="text-base font-semibold mb-2">Duplicate Detection</h3>
                  <p className="text-sm text-muted-foreground leading-relaxed">
                    Automatically flags potential duplicate invoices based on vendor and amount matching.
                  </p>
                </article>
                <article className="feature-card-gradient feature-red p-6 card-enter">
                  <div className="feature-icon w-11 h-11 rounded-lg flex items-center justify-center mb-4">
                    <Bot className="w-5 h-5" />
                  </div>
                  <h3 className="text-base font-semibold mb-2">AI Chat Assistant</h3>
                  <p className="text-sm text-muted-foreground leading-relaxed">
                    Ask questions about your invoices, get spending insights, and analyze vendor relationships.
                  </p>
                </article>
                <article className="feature-card-gradient feature-green p-6 card-enter">
                  <div className="feature-icon w-11 h-11 rounded-lg flex items-center justify-center mb-4">
                    <Globe className="w-5 h-5" />
                  </div>
                  <h3 className="text-base font-semibold mb-2">Multi-Currency</h3>
                  <p className="text-sm text-muted-foreground leading-relaxed">
                    Automatically detects and processes invoices in USD, EUR, GBP, CAD, AUD, and more.
                  </p>
                </article>
                <article className="feature-card-gradient feature-sky p-6 card-enter">
                  <div className="feature-icon w-11 h-11 rounded-lg flex items-center justify-center mb-4">
                    <Layers className="w-5 h-5" />
                  </div>
                  <h3 className="text-base font-semibold mb-2">Bulk Processing</h3>
                  <p className="text-sm text-muted-foreground leading-relaxed">
                    Upload dozens of invoices at once. Our system processes them in parallel for maximum speed.
                  </p>
                </article>
                <article className="feature-card-gradient feature-rose p-6 card-enter">
                  <div className="feature-icon w-11 h-11 rounded-lg flex items-center justify-center mb-4">
                    <Lock className="w-5 h-5" />
                  </div>
                  <h3 className="text-base font-semibold mb-2">Enterprise Security</h3>
                  <p className="text-sm text-muted-foreground leading-relaxed">
                    Your data is encrypted at rest and in transit. SOC 2 compliant infrastructure.
                  </p>
                </article>
              </div>
            </div>
          </section>

          <hr className="section-glow-divider my-0" />

          {/* ── Testimonials ── */}
          <section className="py-20 sm:py-28">
            <div className="max-w-7xl mx-auto px-4 sm:px-6 lg:px-8">
              <div className="text-center mb-16">
                <h2 className="text-3xl sm:text-4xl font-bold mb-4">Trusted by Teams Worldwide</h2>
                <p className="text-muted-foreground text-lg max-w-xl mx-auto">See what our customers have to say</p>
              </div>
              <div className="grid grid-cols-1 md:grid-cols-3 gap-6">
                <blockquote className="testimonial-card p-6 card-enter" style={{ animationDelay: '0ms' }}>
                  <Quote className="w-8 h-8 text-amber-500/40 mb-4" />
                  <p className="text-sm text-muted-foreground leading-relaxed mb-6">
                    &ldquo;OmniParse reduced our invoice processing time from hours to minutes. The accuracy is outstanding and the duplicate detection has saved us from overpaying vendors multiple times.&rdquo;
                  </p>
                  <footer className="flex items-center gap-3">
                    <div className="w-10 h-10 rounded-full bg-amber-500/10 flex items-center justify-center">
                      <span className="text-sm font-semibold text-amber-500">SM</span>
                    </div>
                    <div>
                      <div className="text-sm font-medium">Sarah Mitchell</div>
                      <div className="text-xs text-muted-foreground">CFO, TechVentures Inc.</div>
                    </div>
                  </footer>
                </blockquote>
                <blockquote className="testimonial-card p-6 card-enter" style={{ animationDelay: '100ms' }}>
                  <Quote className="w-8 h-8 text-teal-500/40 mb-4" />
                  <p className="text-sm text-muted-foreground leading-relaxed mb-6">
                    &ldquo;We process over 500 invoices per month. The API integration was seamless and the structured JSON output fits perfectly into our ERP system. A game changer for our accounting team.&rdquo;
                  </p>
                  <footer className="flex items-center gap-3">
                    <div className="w-10 h-10 rounded-full bg-teal-500/10 flex items-center justify-center">
                      <span className="text-sm font-semibold text-teal-500">JK</span>
                    </div>
                    <div>
                      <div className="text-sm font-medium">James Kim</div>
                      <div className="text-xs text-muted-foreground">CTO, DataFlow Systems</div>
                    </div>
                  </footer>
                </blockquote>
                <blockquote className="testimonial-card p-6 card-enter" style={{ animationDelay: '200ms' }}>
                  <Quote className="w-8 h-8 text-amber-500/40 mb-4" />
                  <p className="text-sm text-muted-foreground leading-relaxed mb-6">
                    &ldquo;The AI chat feature is incredible. I can ask questions like &apos;Which vendor had the highest spending this quarter?&apos; and get instant answers. It&apos;s like having a financial analyst on demand.&rdquo;
                  </p>
                  <footer className="flex items-center gap-3">
                    <div className="w-10 h-10 rounded-full bg-amber-500/10 flex items-center justify-center">
                      <span className="text-sm font-semibold text-amber-500">RP</span>
                    </div>
                    <div>
                      <div className="text-sm font-medium">Rachel Patel</div>
                      <div className="text-xs text-muted-foreground">VP Finance, Meridian Group</div>
                    </div>
                  </footer>
                </blockquote>
              </div>
            </div>
          </section>

          <hr className="section-glow-divider my-0" />

          {/* ── FAQ ── */}
          <section className="py-20 sm:py-28 bg-white/[0.01]">
            <div className="max-w-3xl mx-auto px-4 sm:px-6 lg:px-8">
              <div className="text-center mb-16">
                <h2 className="text-3xl sm:text-4xl font-bold mb-4">Frequently Asked Questions</h2>
              </div>
              <div className="space-y-3">
                <details className="glass-card group">
                  <summary className="flex items-center justify-between cursor-pointer p-5 text-sm font-medium">
                    What file formats are supported?
                    <ChevronDown className="w-4 h-4 text-muted-foreground group-open:rotate-180 transition-transform" />
                  </summary>
                  <div className="px-5 pb-5 text-sm text-muted-foreground leading-relaxed">
                    We support PDF files and common image formats including PNG, JPG, and JPEG. You can upload multiple files at once for batch processing.
                  </div>
                </details>
                <details className="glass-card group">
                  <summary className="flex items-center justify-between cursor-pointer p-5 text-sm font-medium">
                    How accurate is the extraction?
                    <ChevronDown className="w-4 h-4 text-muted-foreground group-open:rotate-180 transition-transform" />
                  </summary>
                  <div className="px-5 pb-5 text-sm text-muted-foreground leading-relaxed">
                    Our AI achieves 99.2% accuracy on standard invoices. Each extraction includes a confidence score so you can review lower-confidence results. The system also flags potential duplicates automatically.
                  </div>
                </details>
                <details className="glass-card group">
                  <summary className="flex items-center justify-between cursor-pointer p-5 text-sm font-medium">
                    Is my data secure?
                    <ChevronDown className="w-4 h-4 text-muted-foreground group-open:rotate-180 transition-transform" />
                  </summary>
                  <div className="px-5 pb-5 text-sm text-muted-foreground leading-relaxed">
                    Absolutely. All data is encrypted at rest and in transit. We use SOC 2 compliant infrastructure and your documents are never shared with third parties. You can delete your data at any time.
                  </div>
                </details>
                <details className="glass-card group">
                  <summary className="flex items-center justify-between cursor-pointer p-5 text-sm font-medium">
                    Can I integrate OmniParse with my existing tools?
                    <ChevronDown className="w-4 h-4 text-muted-foreground group-open:rotate-180 transition-transform" />
                  </summary>
                  <div className="px-5 pb-5 text-sm text-muted-foreground leading-relaxed">
                    Yes! We provide a REST API with your unique API key. You can upload invoices, retrieve processed data, and trigger exports programmatically. Full API documentation is available in your dashboard.
                  </div>
                </details>
                <details className="glass-card group">
                  <summary className="flex items-center justify-between cursor-pointer p-5 text-sm font-medium">
                    What happens when I reach my plan limit?
                    <ChevronDown className="w-4 h-4 text-muted-foreground group-open:rotate-180 transition-transform" />
                  </summary>
                  <div className="px-5 pb-5 text-sm text-muted-foreground leading-relaxed">
                    You will receive a notification when you are approaching your monthly limit. Once reached, you can upgrade your plan or wait for the next billing cycle. Enterprise plans have unlimited processing.
                  </div>
                </details>
              </div>
            </div>
          </section>

          <hr className="section-glow-divider my-0" />

          {/* ── Pricing Section ── */}
          <section ref={pricingRef} className="py-20 sm:py-28">
            <div className="max-w-7xl mx-auto px-4 sm:px-6 lg:px-8">
              <div className="text-center mb-16">
                <h2 className="text-3xl sm:text-4xl font-bold mb-4">Simple, Transparent Pricing</h2>
                <p className="text-muted-foreground text-lg max-w-xl mx-auto">Choose the plan that fits your needs. Upgrade or downgrade anytime.</p>
              </div>
              <div className="grid grid-cols-1 sm:grid-cols-2 lg:grid-cols-4 gap-6">
                {[
                  { name: 'Free', price: 0, limit: '20 invoices/mo', features: ['Basic extraction', 'CSV export', 'Email support', '1 user'] },
                  { name: 'Pro', price: 129, limit: '2,000 invoices/mo', features: ['Advanced extraction', 'CSV & JSON export', 'AI Chat assistant', 'API access', 'Priority support', '5 users'], highlight: true },
                  { name: 'Business', price: 29, limit: '200 invoices/mo', features: ['Standard extraction', 'CSV & JSON export', 'API access', 'Email & chat support', '3 users'] },
                  { name: 'Enterprise', price: 499, limit: 'Unlimited', features: ['Custom AI models', 'All export formats', 'Dedicated AI Chat', 'Full API access', 'Dedicated support', 'Unlimited users', 'SLA guarantee'] },
                ].map((plan) => (
                  <article
                    key={plan.name}
                    className={`glass-card p-6 flex flex-col relative ${plan.highlight ? 'pricing-highlight amber-glow' : ''}`}
                  >
                    {plan.highlight && <div className="pricing-ribbon">Most Popular</div>}
                    <div className="mb-5">
                      <h3 className="text-lg font-semibold mb-1">{plan.name}</h3>
                      <div className="text-3xl font-bold text-foreground">
                        ${plan.price}<span className="text-sm font-normal text-muted-foreground">/mo</span>
                      </div>
                      <div className="text-sm text-muted-foreground mt-1">{plan.limit}</div>
                    </div>
                    <ul className="space-y-3 mb-8 flex-1">
                      {plan.features.map((f) => (
                        <li key={f} className="flex items-center gap-2 text-sm text-muted-foreground">
                          <Check className="w-4 h-4 text-teal-500 shrink-0" />
                          {f}
                        </li>
                      ))}
                    </ul>
                    <button
                      onClick={() => {
                        if (plan.name === 'Free') {
                          setAuthMode('signup');
                          store.setView('auth');
                        } else {
                          toast.info('Coming soon! This plan will be available shortly.');
                        }
                      }}
                      className={`btn-shine w-full py-2.5 rounded-lg text-sm font-medium transition-colors ${
                        plan.highlight
                          ? 'bg-amber-500 text-zinc-900 hover:bg-amber-400'
                          : 'border border-white/[0.1] text-foreground hover:bg-white/[0.04]'
                      }`}
                    >
                      {plan.name === 'Free' ? 'Get Started' : 'Coming Soon'}
                    </button>
                  </article>
                ))}
              </div>
            </div>
          </section>
        </main>

        {/* ── Footer ── */}
        <footer className="border-t border-white/[0.06] bg-white/[0.01] mt-auto">
          <div className="max-w-7xl mx-auto px-4 sm:px-6 lg:px-8 py-12 grid grid-cols-2 md:grid-cols-4 gap-8">
            <div>
              <h4 className="text-sm font-semibold mb-4">Product</h4>
              <ul className="space-y-2 text-sm">
                <li><span className="footer-link-underline">Features</span></li>
                <li><span className="footer-link-underline">Pricing</span></li>
                <li><span className="footer-link-underline">API Docs</span></li>
                <li><span className="footer-link-underline">Changelog</span></li>
              </ul>
            </div>
            <div>
              <h4 className="text-sm font-semibold mb-4">Company</h4>
              <ul className="space-y-2 text-sm">
                <li><span className="footer-link-underline">About</span></li>
                <li><span className="footer-link-underline">Blog</span></li>
                <li><span className="footer-link-underline">Careers</span></li>
                <li><span className="footer-link-underline">Contact</span></li>
              </ul>
            </div>
            <div>
              <h4 className="text-sm font-semibold mb-4">Resources</h4>
              <ul className="space-y-2 text-sm">
                <li><span className="footer-link-underline">Documentation</span></li>
                <li><span className="footer-link-underline">Help Center</span></li>
                <li><span className="footer-link-underline">Status Page</span></li>
              </ul>
            </div>
            <div>
              <h4 className="text-sm font-semibold mb-4">Legal</h4>
              <ul className="space-y-2 text-sm">
                <li><span className="footer-link-underline">Privacy Policy</span></li>
                <li><span className="footer-link-underline">Terms of Service</span></li>
                <li><span className="footer-link-underline">Security</span></li>
              </ul>
            </div>
          </div>
          <div className="border-t border-white/[0.06]">
            <div className="max-w-7xl mx-auto px-4 sm:px-6 lg:px-8 py-6 flex flex-col sm:flex-row items-center justify-between gap-4">
              <div className="flex items-center gap-2">
                <div className="w-6 h-6 rounded-md bg-amber-500 flex items-center justify-center">
                  <ScanLine className="w-3.5 h-3.5 text-zinc-900" />
                </div>
                <span className="text-sm text-muted-foreground">OmniParse AI</span>
              </div>
              <p className="text-xs text-muted-foreground">
                &copy; {new Date().getFullYear()} OmniParse AI. All rights reserved.
              </p>
            </div>
          </div>
        </footer>

        {/* ── Scroll to Top ── */}
        <button
          onClick={() => window.scrollTo({ top: 0, behavior: 'smooth' })}
          className={`scroll-top-btn ${showScrollTop ? 'visible' : ''}`}
          aria-label="Scroll to top"
        >
          <ChevronUp className="w-5 h-5" />
        </button>

        {/* ── HF Space Download ── */}
        <a
          href="/omniparse-hf-space.zip"
          download
          className="fixed bottom-20 right-5 z-50 bg-amber-500 hover:bg-amber-400 text-black font-semibold text-xs px-3 py-2 rounded-lg shadow-lg transition-colors"
        >
          ⬇ Download HF Space Files (.zip)
        </a>
      </div>
    );
  }

  // ──────────────────────────────────────────────────────────────────────────────
  // AUTH VIEW
  // ──────────────────────────────────────────────────────────────────────────────

  if (store.view === 'auth') {
    const pwStrength = getPasswordStrength(authMode === 'signup' ? signupPassword : '');
    return (
      <div className="min-h-screen flex flex-col items-center justify-center px-4 py-12 noise-overlay">
        <div className="w-full max-w-md">
          <button
            onClick={() => store.setView('landing')}
            className="flex items-center gap-2 text-sm text-muted-foreground hover:text-foreground mb-8 transition-colors"
          >
            <ArrowLeft className="w-4 h-4" />
            Back to home
          </button>
          <div className="glass-card p-6 sm:p-8 amber-glow view-enter">
            <div className="text-center mb-8">
              <div className="w-12 h-12 rounded-xl bg-amber-500 flex items-center justify-center mx-auto mb-4">
                <ScanLine className="w-7 h-7 text-zinc-900" />
              </div>
              <h1 className="text-2xl font-bold">
                {authMode === 'login' ? 'Welcome back' : 'Create your account'}
              </h1>
              <p className="text-sm text-muted-foreground mt-1">
                {authMode === 'login' ? 'Sign in to your OmniParse account' : 'Get started with OmniParse for free'}
              </p>
            </div>

            {/* Tab Toggle */}
            <div className="flex border border-white/[0.08] rounded-lg p-1 mb-6">
              <button
                onClick={() => { setAuthMode('login'); setAuthError(''); }}
                className={`flex-1 py-2 text-sm font-medium rounded-md transition-colors ${
                  authMode === 'login' ? 'bg-amber-500 text-zinc-900' : 'text-muted-foreground hover:text-foreground'
                }`}
              >
                Log in
              </button>
              <button
                onClick={() => { setAuthMode('signup'); setAuthError(''); }}
                className={`flex-1 py-2 text-sm font-medium rounded-md transition-colors ${
                  authMode === 'signup' ? 'bg-amber-500 text-zinc-900' : 'text-muted-foreground hover:text-foreground'
                }`}
              >
                Sign up
              </button>
            </div>

            {/* Error Display */}
            {authError && (
              <div className="flex items-center gap-2 p-3 mb-6 rounded-lg bg-red-500/10 border border-red-500/20 text-sm text-red-400 fade-in">
                <AlertCircle className="w-4 h-4 shrink-0" />
                {authError}
              </div>
            )}

            {authMode === 'login' ? (
              <form
                onSubmit={(e) => { e.preventDefault(); fetchLogin(); }}
                className="space-y-4"
              >
                <div>
                  <label className="input-label" htmlFor="login-email">
                    Email
                  </label>
                  <div className="relative">
                    <Mail className="absolute left-3 top-1/2 -translate-y-1/2 w-4 h-4 text-muted-foreground" />
                    <input
                      id="login-email"
                      type="email"
                      value={loginEmail}
                      onChange={(e) => setLoginEmail(e.target.value)}
                      required
                      placeholder="you@company.com"
                      className="input-field input-field-glow pl-10"
                    />
                  </div>
                </div>
                <div>
                  <label className="input-label" htmlFor="login-password">
                    Password
                  </label>
                  <div className="relative">
                    <Lock className="absolute left-3 top-1/2 -translate-y-1/2 w-4 h-4 text-muted-foreground" />
                    <input
                      id="login-password"
                      type="password"
                      value={loginPassword}
                      onChange={(e) => setLoginPassword(e.target.value)}
                      required
                      placeholder="Enter your password"
                      className="input-field input-field-glow pl-10"
                    />
                  </div>
                </div>
                <button
                  type="submit"
                  disabled={authLoading}
                  className="btn-primary text-sm w-full flex items-center justify-center gap-2 disabled:opacity-50"
                >
                  {authLoading && <Loader2 className="w-4 h-4 animate-spin" />}
                  Sign In
                </button>
                <div className="text-center">
                  <button
                    type="button"
                    onClick={() => toast.info('Password reset feature coming soon')}
                    className="text-xs text-muted-foreground hover:text-amber-500 transition-colors"
                  >
                    Forgot your password?
                  </button>
                </div>
                <div className="text-center">
                  <p className="text-xs text-muted-foreground mt-4">
                    Demo credentials: <span className="text-amber-500">demo@omniparse.ai</span> / <span className="text-amber-500">demo1234</span>
                  </p>
                </div>
              </form>
            ) : (
              <form
                onSubmit={(e) => { e.preventDefault(); fetchSignup(); }}
                className="space-y-4"
              >
                <div>
                  <label className="input-label" htmlFor="signup-name">
                    Name
                  </label>
                  <div className="relative">
                    <User className="absolute left-3 top-1/2 -translate-y-1/2 w-4 h-4 text-muted-foreground" />
                    <input
                      id="signup-name"
                      type="text"
                      value={signupName}
                      onChange={(e) => setSignupName(e.target.value)}
                      required
                      placeholder="Your full name"
                      className="input-field input-field-glow pl-10"
                    />
                  </div>
                </div>
                <div>
                  <label className="input-label" htmlFor="signup-email">
                    Email
                  </label>
                  <div className="relative">
                    <Mail className="absolute left-3 top-1/2 -translate-y-1/2 w-4 h-4 text-muted-foreground" />
                    <input
                      id="signup-email"
                      type="email"
                      value={signupEmail}
                      onChange={(e) => setSignupEmail(e.target.value)}
                      required
                      placeholder="you@company.com"
                      className="input-field input-field-glow pl-10"
                    />
                  </div>
                </div>
                <div>
                  <label className="input-label" htmlFor="signup-password">
                    Password
                  </label>
                  <div className="relative">
                    <Lock className="absolute left-3 top-1/2 -translate-y-1/2 w-4 h-4 text-muted-foreground" />
                    <input
                      id="signup-password"
                      type="password"
                      value={signupPassword}
                      onChange={(e) => setSignupPassword(e.target.value)}
                      required
                      minLength={6}
                      placeholder="At least 6 characters"
                      className="input-field input-field-glow pl-10"
                    />
                  </div>
                  {signupPassword && (
                    <div className="mt-2">
                      <div className="h-1.5 w-full bg-zinc-800 rounded-full overflow-hidden">
                        <div className={`h-full ${pwStrength.color} ${pwStrength.width} rounded-full transition-all duration-300`} />
                      </div>
                      <p className="text-xs text-muted-foreground mt-1">{pwStrength.label}</p>
                    </div>
                  )}
                </div>
                <div>
                  <label className="input-label" htmlFor="signup-confirm">
                    Confirm Password
                  </label>
                  <div className="relative">
                    <Lock className="absolute left-3 top-1/2 -translate-y-1/2 w-4 h-4 text-muted-foreground" />
                    <input
                      id="signup-confirm"
                      type="password"
                      value={signupConfirm}
                      onChange={(e) => setSignupConfirm(e.target.value)}
                      required
                      placeholder="Confirm your password"
                      className="input-field input-field-glow pl-10"
                    />
                  </div>
                </div>
                <label className="flex items-start gap-2.5 cursor-pointer group">
                  <input
                    type="checkbox"
                    checked={signupTerms}
                    onChange={(e) => setSignupTerms(e.target.checked)}
                    className="mt-0.5 h-4 w-4 rounded border-zinc-600 bg-zinc-800 text-amber-500 focus:ring-amber-500 focus:ring-offset-0 cursor-pointer"
                  />
                  <span className="text-xs text-muted-foreground leading-relaxed">
                    I agree to the{' '}
                    <span className="text-amber-500 hover:text-amber-400 underline underline-offset-2">Terms of Service</span>
                    {' '}and{' '}
                    <span className="text-amber-500 hover:text-amber-400 underline underline-offset-2">Privacy Policy</span>
                  </span>
                </label>
                <button
                  type="submit"
                  disabled={authLoading || !signupTerms}
                  className="btn-primary text-sm w-full flex items-center justify-center gap-2 disabled:opacity-50"
                >
                  {authLoading && <Loader2 className="w-4 h-4 animate-spin" />}
                  Create Account
                </button>
              </form>
            )}
          </div>
        </div>
      </div>
    );
  }

  // ──────────────────────────────────────────────────────────────────────────────
  // DASHBOARD VIEW
  // ──────────────────────────────────────────────────────────────────────────────

  if (store.view === 'dashboard' && store.user) {
    const planLimit = PLAN_LIMITS[store.user.plan] ?? PLAN_LIMITS.free;
    const planLabel = PLAN_LABELS[store.user.plan] ?? store.user.plan;
    const invoiceCount = store.invoices.length;
    const totalAmount = store.invoices.reduce((s, i) => s + (i.total ?? 0), 0);
    const dupCount = store.invoices.filter((i) => i.isDuplicate).length;
    const avgConfidence = store.invoices.length > 0
      ? store.invoices.reduce((s, i) => s + (i.confidence ?? 0), 0) / store.invoices.length
      : 0;
    const usagePercent = planLimit === Infinity ? 0 : Math.min((invoiceCount / planLimit) * 100, 100);

    return (
      <div className="min-h-screen flex flex-col noise-overlay">
        {/* ── Dashboard Header ── */}
        <header className="sticky top-0 z-50 nav-blur">
          <div className="max-w-7xl mx-auto px-4 sm:px-6 lg:px-8 h-16 flex items-center justify-between">
            <div className="flex items-center gap-3">
              <div className="logo-glow w-8 h-8 rounded-lg bg-amber-500 flex items-center justify-center">
                <ScanLine className="w-5 h-5 text-zinc-900" />
              </div>
              <span className="font-bold text-foreground hidden sm:inline">OmniParse</span>
              <span className={`${store.user.plan === 'free' || store.user.plan === 'Free' ? 'badge-muted' : 'badge-amber'}`}>
                {planLabel}
              </span>
            </div>
            <div className="flex items-center gap-3">
              <button
                onClick={() => setTheme(theme === 'dark' ? 'light' : 'dark')}
                className="p-2 rounded-lg text-muted-foreground hover:text-foreground hover:bg-white/[0.04] transition-colors"
                aria-label={theme === 'dark' ? 'Switch to light mode' : 'Switch to dark mode'}
              >
                {theme === 'dark' ? <Sun className="w-4 h-4" /> : <Moon className="w-4 h-4" />}
              </button>
              <button
                onClick={() => { store.setActiveDashTab('upload'); }}
                className="hidden sm:flex items-center gap-1.5 text-xs font-medium px-3 py-1.5 rounded-lg bg-amber-500/10 text-amber-500 hover:bg-amber-500/20 transition-colors"
              >
                <Upload className="w-3.5 h-3.5" />
                Upload
              </button>
              <button
                onClick={() => setShowCommandPalette(true)}
                className="hidden sm:flex items-center gap-1.5 text-xs font-medium px-3 py-1.5 rounded-lg bg-white/[0.04] text-muted-foreground hover:text-foreground hover:bg-white/[0.06] border border-white/[0.06] transition-colors"
              >
                <Search className="w-3.5 h-3.5" />
                Search...
                <kbd className="kbd text-[10px] ml-1">Ctrl+K</kbd>
              </button>
              <button
                onClick={exportCsv}
                className="hidden sm:flex items-center gap-1.5 text-xs font-medium px-3 py-1.5 rounded-lg bg-teal-500/10 text-teal-500 hover:bg-teal-500/20 transition-colors"
              >
                <FileDown className="w-3.5 h-3.5" />
                Export CSV
              </button>
              <button
                onClick={() => { window.open('/api/invoices/export/xlsx', '_blank'); toast.success('Excel download started'); }}
                className="hidden sm:flex items-center gap-1.5 text-xs font-medium px-3 py-1.5 rounded-lg bg-amber-500/10 text-amber-500 hover:bg-amber-500/20 transition-colors"
              >
                <FileJson className="w-3.5 h-3.5" />
                Export XLSX
              </button>
              <div className="relative">
                <button
                  onClick={() => setShowNotifications(!showNotifications)}
                  className="p-2 rounded-lg text-muted-foreground hover:text-foreground hover:bg-white/[0.04] transition-colors"
                  aria-label="Notifications"
                >
                  <Bell className="w-4 h-4" />
                  {store.invoices.length > 0 && (
                    <span className="notification-badge" />
                  )}
                </button>
                {showNotifications && (
                  <>
                    <div className="fixed inset-0 z-40" onClick={() => setShowNotifications(false)} />
                    <div className="notification-dropdown z-50">
                      <div className="px-4 py-3 border-b border-white/[0.06]">
                        <h3 className="text-sm font-semibold">Notifications</h3>
                      </div>
                      <div className="max-h-80 overflow-y-auto">
                        {notifications.map((n) => {
                          const NIcon = n.icon;
                          return (
                            <div key={n.id} className="notification-item flex items-start gap-3">
                              <div className={`mt-0.5 ${n.color}`}>
                                <NIcon className="w-4 h-4" />
                              </div>
                              <div className="min-w-0 flex-1">
                                <div className="text-sm font-medium">{n.title}</div>
                                <div className="text-xs text-muted-foreground mt-0.5">{n.description}</div>
                              </div>
                            </div>
                          );
                        })}
                      </div>
                    </div>
                  </>
                )}
              </div>
              <div className="flex items-center gap-2 ml-2">
                <div className="w-8 h-8 rounded-full bg-white/[0.06] flex items-center justify-center text-sm font-medium text-amber-500">
                  {store.user.name.charAt(0).toUpperCase()}
                </div>
                <span className="text-sm font-medium hidden sm:inline">{store.user.name}</span>
              </div>
              <button
                onClick={fetchLogout}
                className="p-2 rounded-lg text-muted-foreground hover:text-foreground hover:bg-white/[0.04] transition-colors"
                aria-label="Log out"
                title="Log out"
              >
                <LogOut className="w-4 h-4" />
              </button>
              <button
                className="md:hidden p-2 rounded-lg text-muted-foreground hover:text-foreground"
                onClick={() => setDashMobileMenu(!dashMobileMenu)}
                aria-label="Toggle dashboard menu"
              >
                {dashMobileMenu ? <X className="w-5 h-5" /> : <Menu className="w-5 h-5" />}
              </button>
            </div>
          </div>

          {/* Mobile tab menu */}
          {dashMobileMenu && (
            <div className="md:hidden border-t border-white/[0.06] bg-background/95 backdrop-blur-md fade-in mobile-nav-slide">
              <nav className="px-4 py-3 space-y-1 overflow-x-auto">
                <button
                  onClick={() => { setTheme(theme === 'dark' ? 'light' : 'dark'); setDashMobileMenu(false); }}
                  className="flex items-center gap-3 w-full text-left px-3 py-2.5 rounded-lg text-sm text-muted-foreground hover:text-foreground hover:bg-white/[0.03] transition-colors"
                >
                  {theme === 'dark' ? <Sun className="w-4 h-4" /> : <Moon className="w-4 h-4" />}
                  {theme === 'dark' ? 'Light mode' : 'Dark mode'}
                </button>
                <div className="h-px bg-white/[0.06] my-1" />
                {DASH_TABS.map((tab) => {
                  const Icon = tab.icon;
                  return (
                    <button
                      key={tab.id}
                      onClick={() => { store.setActiveDashTab(tab.id); setDashMobileMenu(false); }}
                      className={`flex items-center gap-3 w-full text-left px-3 py-2.5 rounded-lg text-sm transition-colors ${
                        store.activeDashTab === tab.id
                          ? 'bg-amber-500/10 text-amber-500'
                          : 'text-muted-foreground hover:text-foreground hover:bg-white/[0.03]'
                      }`}
                    >
                      <Icon className="w-4 h-4" />
                      {tab.label}
                    </button>
                  );
                })}
              </nav>
            </div>
          )}
        </header>

        {/* ── Dashboard Tabs (desktop) ── */}
        <div className="hidden md:block border-b border-white/[0.06] bg-white/[0.01]">
          <div className="max-w-7xl mx-auto px-4 sm:px-6 lg:px-8">
            <nav className="flex gap-1 -mb-px overflow-x-auto" aria-label="Dashboard tabs">
              {DASH_TABS.map((tab) => {
                const Icon = tab.icon;
                return (
                  <button
                    key={tab.id}
                    onClick={() => store.setActiveDashTab(tab.id)}
                    className={`dash-tab flex items-center gap-2 ${store.activeDashTab === tab.id ? 'active' : ''}`}
                  >
                    <Icon className="w-4 h-4" />
                    {tab.label}
                  </button>
                );
              })}
            </nav>
          </div>
        </div>

        {/* ── Dashboard Content ── */}
        <main className="flex-1 max-w-7xl mx-auto w-full px-4 sm:px-6 lg:px-8 py-6 sm:py-8">
          <div className="view-enter">
          {/* ── Stats Overview ── */}
          <section className="grid grid-cols-2 lg:grid-cols-4 gap-3 mb-6" aria-label="Dashboard statistics">
            <div className="stat-card-glass card-accent-amber card-enter">
              <div className="flex items-center gap-2 mb-1">
                <FileText className="w-4 h-4 text-amber-500" />
                <span className="text-xs text-muted-foreground font-medium uppercase tracking-wider">Total Invoices</span>
              </div>
              <div className="text-2xl font-bold text-foreground">{store.invoices.length}</div>
            </div>
            <div className="stat-card-glass card-accent-teal card-enter">
              <div className="flex items-center gap-2 mb-1">
                <CircleDollarSign className="w-4 h-4 text-teal-500" />
                <span className="text-xs text-muted-foreground font-medium uppercase tracking-wider">Total Amount</span>
              </div>
              <div className="text-2xl font-bold text-foreground">
                {formatCurrency(store.invoices.reduce((sum, inv) => sum + (inv.total ?? 0), 0))}
              </div>
            </div>
            <div className="stat-card-glass card-accent-red card-enter">
              <div className="flex items-center gap-2 mb-1">
                <AlertCircle className="w-4 h-4 text-red-400" />
                <span className="text-xs text-muted-foreground font-medium uppercase tracking-wider">Duplicates</span>
              </div>
              <div className="text-2xl font-bold text-foreground">{store.invoices.filter(i => i.isDuplicate).length}</div>
            </div>
            <div className="stat-card-glass card-accent-green card-enter">
              <div className="flex items-center gap-2 mb-1">
                <TrendingUp className="w-4 h-4 text-green-400" />
                <span className="text-xs text-muted-foreground font-medium uppercase tracking-wider">Avg Confidence</span>
              </div>
              <div className="text-2xl font-bold text-foreground">
                {store.invoices.length > 0
                  ? `${((store.invoices.reduce((sum, inv) => sum + (inv.confidence ?? 0), 0) / store.invoices.length) * 100).toFixed(1)}%`
                  : '--'}
              </div>
            </div>
          </section>

          {/* ═══════════════════════════════════════════════════════════════════════ */}
          {/* TAB 1: UPLOAD                                                          */}
          {/* ═══════════════════════════════════════════════════════════════════════ */}
          {store.activeDashTab === 'upload' && (
            <section className="fade-in tab-content-area">
              {/* Welcome Panel */}
              <div className="glass-card welcome-panel p-5 mb-6 card-enter">
                <div className="relative">
                  <div className="flex items-center gap-3 mb-3">
                    <div className="w-10 h-10 rounded-xl bg-amber-500/10 flex items-center justify-center">
                      <Sparkles className="w-5 h-5 text-amber-500" />
                    </div>
                    <div>
                      <h2 className="text-lg font-bold">Welcome back, {store.user?.name?.split(' ')[0] || 'there'}!</h2>
                      <p className="text-sm text-muted-foreground">Here&apos;s a quick overview of your account</p>
                    </div>
                    <span className="ml-auto hidden sm:inline-flex items-center gap-1.5 px-2.5 py-1 rounded-full text-xs font-medium bg-amber-500/10 text-amber-500 capitalize badge-pill">
                      <span className="badge-pill-dot bg-amber-500" />
                      <Crown className="w-3 h-3" />
                      {store.user?.plan || 'Free'} Plan
                    </span>
                  </div>
                  <div className="grid grid-cols-2 sm:grid-cols-4 gap-3 mt-4">
                    <button onClick={() => store.setActiveDashTab('invoices')} className="welcome-stat-btn">
                      <FileText className="w-4 h-4 welcome-stat-icon" />
                      <div>
                        <div className="text-lg font-bold">{store.invoices.length}</div>
                        <div className="text-xs text-muted-foreground">Invoices</div>
                      </div>
                    </button>
                    <button onClick={() => store.setActiveDashTab('analytics')} className="welcome-stat-btn">
                      <TrendingUp className="w-4 h-4 welcome-stat-icon" />
                      <div>
                        <div className="text-lg font-bold">{formatCurrency(store.invoices.reduce((s, i) => s + (i.total ?? 0), 0))}</div>
                        <div className="text-xs text-muted-foreground">Total Spent</div>
                      </div>
                    </button>
                    <button onClick={() => store.setActiveDashTab('invoices')} className="welcome-stat-btn">
                      <AlertCircle className="w-4 h-4 welcome-stat-icon" />
                      <div>
                        <div className="text-lg font-bold">{store.invoices.filter(i => i.isDuplicate).length}</div>
                        <div className="text-xs text-muted-foreground">Duplicates</div>
                      </div>
                    </button>
                    <button onClick={() => store.setActiveDashTab('activity')} className="welcome-stat-btn">
                      <Clock className="w-4 h-4 welcome-stat-icon" />
                      <div>
                        <div className="text-lg font-bold">{activityLogs.length}</div>
                        <div className="text-xs text-muted-foreground">Activities</div>
                      </div>
                    </button>
                  </div>
                </div>
              </div>
              <div className="mb-4">
                <h2 className="text-xl font-bold">Upload Invoices</h2>
                <p className="text-sm text-muted-foreground mt-1">Drag and drop PDF or image files to extract invoice data</p>
              </div>

              {/* Usage Progress */}
              <div className="glass-card p-4 mb-6">
                <div className="flex items-center justify-between mb-2">
                  <span className="text-sm text-muted-foreground">Monthly usage</span>
                  <span className="text-sm font-medium">
                    {invoiceCount} / {planLimit === Infinity ? 'Unlimited' : planLimit}
                  </span>
                </div>
                <div className="h-2 w-full bg-zinc-800 rounded-full overflow-hidden">
                  <div
                    className={`progress-bar ${usagePercent > 90 ? '!bg-red-500' : ''}`}
                    style={{ width: `${planLimit === Infinity ? 0 : usagePercent}%` }}
                  />
                </div>
              </div>

              {/* Drop Zone */}
              <div
                className={`drop-zone drop-zone-animated p-8 sm:p-12 text-center ${dragOver ? 'drag-over' : ''}`}
                onDragOver={handleDragOver}
                onDragLeave={handleDragLeave}
                onDrop={handleDrop}
                onClick={() => fileInputRef.current?.click()}
                role="button"
                tabIndex={0}
                aria-label="Click or drag files to upload"
                onKeyDown={(e) => { if (e.key === 'Enter' || e.key === ' ') fileInputRef.current?.click(); }}
              >
                <input
                  ref={fileInputRef}
                  type="file"
                  multiple
                  accept=".pdf,.png,.jpg,.jpeg"
                  className="hidden"
                  onChange={handleFileSelect}
                />
                <Upload className="w-10 h-10 text-amber-500/60 mx-auto mb-4" />
                <p className="text-sm font-medium mb-1">
                  Drop your invoice files here, or click to browse
                </p>
                <p className="text-xs text-muted-foreground">
                  Supports PDF, PNG, JPG, JPEG. You can upload multiple files at once.
                </p>
              </div>

              {/* Selected Files List */}
              {selectedFiles.length > 0 && (
                <div className="glass-card p-4 mt-4 fade-in">
                  <h3 className="text-sm font-medium mb-3">{selectedFiles.length} file(s) selected</h3>
                  <ul className="space-y-2">
                    {selectedFiles.map((file, idx) => (
                      <li key={`${file.name}-${idx}`} className="flex items-center justify-between text-sm py-2 px-3 rounded-lg bg-white/[0.02]">
                        <div className="flex items-center gap-2 min-w-0">
                          <FileText className="w-4 h-4 text-amber-500 shrink-0" />
                          <span className="truncate">{file.name}</span>
                          <span className="text-xs text-muted-foreground shrink-0">
                            {(file.size / 1024).toFixed(1)} KB
                          </span>
                        </div>
                        <button
                          onClick={(e) => { e.stopPropagation(); removeFile(idx); }}
                          className="p-1 rounded text-muted-foreground hover:text-red-400 transition-colors shrink-0"
                          aria-label={`Remove ${file.name}`}
                        >
                          <X className="w-4 h-4" />
                        </button>
                      </li>
                    ))}
                  </ul>
                  <button
                    onClick={fetchUpload}
                    disabled={store.uploadLoading}
                    className="btn-primary text-sm mt-4 w-full flex items-center justify-center gap-2 disabled:opacity-50"
                  >
                    {store.uploadLoading ? (
                      <>
                        <Loader2 className="w-4 h-4 animate-spin" />
                        Processing...
                      </>
                    ) : (
                      <>
                        <Upload className="w-4 h-4" />
                        Upload and Extract
                      </>
                    )}
                  </button>
                </div>
              )}

              {/* Upload Results */}
              {store.uploadResults && store.uploadResults.length > 0 && (
                <div className="mt-6 fade-in">
                  <h3 className="text-sm font-medium mb-3 flex items-center gap-2">
                    <CheckCircle className="w-4 h-4 text-green-500" />
                    Results
                  </h3>
                  <div className="glass-card overflow-hidden">
                    <div className="max-h-96 overflow-y-auto scrollbar-thin">
                      <table className="data-table text-sm">
                        <thead className="border-b border-white/[0.06] bg-white/[0.02]">
                          <tr>
                            <th className="text-left px-4 py-3 font-medium text-muted-foreground">Vendor</th>
                            <th className="text-left px-4 py-3 font-medium text-muted-foreground">Invoice #</th>
                            <th className="text-right px-4 py-3 font-medium text-muted-foreground">Amount</th>
                            <th className="text-right px-4 py-3 font-medium text-muted-foreground">Confidence</th>
                            <th className="text-center px-4 py-3 font-medium text-muted-foreground">Status</th>
                          </tr>
                        </thead>
                        <tbody>
                          {store.uploadResults.map((inv) => (
                            <tr key={inv.id} className="border-b border-white/[0.04] hover:bg-white/[0.02]">
                              <td className="px-4 py-3">{inv.vendor || '--'}</td>
                              <td className="px-4 py-3 text-muted-foreground">{inv.invNumber || '--'}</td>
                              <td className="px-4 py-3 text-right font-medium">{formatCurrency(inv.total, inv.currency)}</td>
                              <td className="px-4 py-3 text-right">{inv.confidence ? `${(inv.confidence * 100).toFixed(1)}%` : '--'}</td>
                              <td className="px-4 py-3 text-center">
                                <span className={`inline-block px-2.5 py-0.5 rounded-full text-xs font-medium ${
                                  inv.isDuplicate ? 'status-duplicate' : 'status-done'
                                }`}>
                                  {inv.isDuplicate ? 'Duplicate' : 'Done'}
                                </span>
                              </td>
                            </tr>
                          ))}
                        </tbody>
                      </table>
                    </div>
                  </div>
                </div>
              )}

              {/* Empty State */}
              {!store.uploadLoading && (!store.uploadResults || store.uploadResults.length === 0) && selectedFiles.length === 0 && (
                <div className="text-center py-8 text-muted-foreground">
                  <Inbox className="w-10 h-10 mx-auto mb-3 opacity-40" />
                  <p className="text-sm">Upload your first invoice to get started</p>
                </div>
              )}
            </section>
          )}

          {/* ═══════════════════════════════════════════════════════════════════════ */}
          {/* TAB 2: INVOICES                                                        */}
          {/* ═══════════════════════════════════════════════════════════════════════ */}
          {store.activeDashTab === 'invoices' && (
            <section className="fade-in tab-content-area">
              <div className="flex flex-col sm:flex-row sm:items-center sm:justify-between gap-4 mb-6">
                <div>
                  <h2 className="text-xl font-bold">Invoices</h2>
                  <p className="text-sm text-muted-foreground mt-1">
                    {filteredInvoices.length} invoice(s) found
                    <span className="ml-2 text-xs text-muted-foreground/60">Ctrl+K</span>
                  </p>
                </div>
                <button
                  onClick={() => fetchInvoices(invoiceFilter)}
                  className="flex items-center gap-2 text-sm text-muted-foreground hover:text-foreground transition-colors self-start"
                >
                  <RefreshCw className="w-4 h-4" />
                  Refresh
                </button>
              </div>

              {/* Search and Filter */}
              <div className="flex flex-col sm:flex-row gap-3 mb-4">
                <div className="relative flex-1">
                  <Search className="absolute left-3 top-1/2 -translate-y-1/2 w-4 h-4 text-muted-foreground" />
                  <input
                    type="text"
                    value={invoiceSearch}
                    onChange={(e) => setInvoiceSearch(e.target.value)}
                    placeholder="Search by vendor name..."
                    className="input-field input-field-glow pl-10"
                  />
                </div>
                <div className="flex gap-2">
                  {(['all', 'done', 'review', 'duplicates'] as const).map((f) => (
                    <button
                      key={f}
                      onClick={() => setInvoiceFilter(f)}
                      className={`px-3 py-2 rounded-lg text-xs font-medium transition-colors ${
                        invoiceFilter === f
                          ? 'bg-amber-500 text-zinc-900'
                          : 'bg-white/[0.04] text-muted-foreground hover:text-foreground border border-white/[0.08]'
                      }`}
                    >
                      {f === 'all' ? 'All' : f === 'done' ? 'Done' : f === 'review' ? 'Review' : 'Duplicates'}
                    </button>
                  ))}
                </div>
              </div>

              {/* Date Range Filter */}
              <div className="flex flex-col sm:flex-row gap-3 mb-4">
                <div className="flex-1">
                  <label className="input-label">From Date</label>
                  <input
                    type="date"
                    value={dateFrom}
                    onChange={(e) => setDateFrom(e.target.value)}
                    className="input-field input-field-glow"
                  />
                </div>
                <div className="flex-1">
                  <label className="input-label">To Date</label>
                  <input
                    type="date"
                    value={dateTo}
                    onChange={(e) => setDateTo(e.target.value)}
                    className="input-field input-field-glow"
                  />
                </div>
                {(dateFrom || dateTo) && (
                  <div className="flex items-end">
                    <button
                      onClick={() => { setDateFrom(''); setDateTo(''); }}
                      className="px-3 py-2.5 rounded-lg text-xs font-medium text-muted-foreground hover:text-foreground border border-white/[0.08] hover:border-white/[0.15] transition-colors"
                    >
                      Clear dates
                    </button>
                  </div>
                )}
              </div>

              {/* Duplicate Alert Panel */}
{!store.isLoading && store.invoices.some(i => i.isDuplicate) && (
  <div className="glass-card duplicate-alert p-4 mb-4 card-enter">
    <div className="flex items-start gap-3">
      <div className="w-10 h-10 rounded-xl bg-amber-500/10 flex items-center justify-center shrink-0">
        <CopyCheck className="w-5 h-5 text-amber-500" />
      </div>
      <div className="flex-1 min-w-0">
        <h3 className="text-sm font-semibold text-amber-500">Duplicate Invoices Detected</h3>
        <p className="text-xs text-muted-foreground mt-1">
          {store.invoices.filter(i => i.isDuplicate).length} potential duplicate(s) found. Review and decide whether to keep or remove them.
        </p>
        <div className="flex flex-wrap gap-2 mt-3">
          <button
            onClick={() => { setInvoiceFilter('duplicates'); }}
            className="flex items-center gap-1.5 text-xs font-medium px-3 py-1.5 rounded-lg bg-amber-500/10 text-amber-400 hover:bg-amber-500/20 transition-colors"
          >
            <Eye className="w-3.5 h-3.5" />
            View Duplicates
          </button>
          <button
            onClick={() => {
              const dupIds = store.invoices.filter(i => i.isDuplicate).map(i => i.id);
              if (window.confirm(`Delete ${dupIds.length} duplicate invoice(s)?`)) {
                Promise.all(dupIds.map(id => fetch('/api/invoices', { method: 'DELETE', headers: { 'Content-Type': 'application/json' }, body: JSON.stringify({ id }) })))
                  .then(() => { toast.success(`${dupIds.length} duplicate(s) removed`); fetchInvoices(invoiceFilter); })
                  .catch(() => toast.error('Failed to remove duplicates'));
              }
            }}
            className="flex items-center gap-1.5 text-xs font-medium px-3 py-1.5 rounded-lg bg-red-500/10 text-red-400 hover:bg-red-500/20 transition-colors"
          >
            <Trash2 className="w-3.5 h-3.5" />
            Remove All Duplicates
          </button>
        </div>
      </div>
    </div>
  </div>
)}

              {/* Selection Toolbar */}
              {selectedInvoices.size > 0 && (
                <div className="flex items-center justify-between p-3 mb-4 rounded-lg bg-amber-500/10 border border-amber-500/20 fade-in">
                  <span className="text-sm font-medium text-amber-500">
                    {selectedInvoices.size} invoice(s) selected
                  </span>
                  <div className="flex items-center gap-2">
                    <button
                      onClick={() => setSelectedInvoices(new Set())}
                      className="text-xs text-muted-foreground hover:text-foreground transition-colors px-2 py-1"
                    >
                      Clear selection
                    </button>
                    <button
                      onClick={fetchBatchDelete}
                      className="flex items-center gap-1.5 text-xs font-medium px-3 py-1.5 rounded-lg bg-red-500/10 text-red-400 hover:bg-red-500/20 transition-colors"
                    >
                      <Trash2 className="w-3.5 h-3.5" />
                      Delete Selected
                    </button>
                  </div>
                </div>
              )}

              {/* Loading Skeletons */}
              {store.isLoading && (
                <div className="glass-card p-4 space-y-3">
                  {Array.from({ length: 5 }).map((_, i) => (
                    <div key={i} className="skeleton-shimmer h-12 w-full" />
                  ))}
                </div>
              )}

              {/* Invoice Table */}
              {!store.isLoading && filteredInvoices.length > 0 && (
                <div className="glass-card overflow-hidden">
                  <div className="max-h-96 overflow-y-auto scrollbar-thin">
                    <table className="data-table text-sm">
                      <thead className="border-b border-white/[0.06] bg-white/[0.02] sticky top-0">
                        <tr>
                          <th className="px-3 py-3 w-10">
                            <input
                              type="checkbox"
                              checked={selectedInvoices.size === filteredInvoices.length && filteredInvoices.length > 0}
                              onChange={toggleSelectAll}
                              className="invoice-checkbox"
                              aria-label="Select all invoices"
                            />
                          </th>
                          <th
                            className="sort-header text-left px-4 py-3 font-medium text-muted-foreground"
                            onClick={() => toggleSort('vendor')}
                          >
                            <div className="flex items-center gap-1">
                              Vendor
                              {sortField === 'vendor' ? (
                                <span className="sort-indicator">{sortDir === 'asc' ? '↑' : '↓'}</span>
                              ) : (
                                <span className="sort-header-inactive"></span>
                              )}
                            </div>
                          </th>
                          <th
                            className="sort-header text-left px-4 py-3 font-medium text-muted-foreground hidden sm:table-cell"
                            onClick={() => toggleSort('invNumber')}
                          >
                            <div className="flex items-center gap-1">
                              Invoice #
                              {sortField === 'invNumber' ? (
                                <span className="sort-indicator">{sortDir === 'asc' ? '↑' : '↓'}</span>
                              ) : (
                                <span className="sort-header-inactive"></span>
                              )}
                            </div>
                          </th>
                          <th
                            className="sort-header text-left px-4 py-3 font-medium text-muted-foreground hidden md:table-cell"
                            onClick={() => toggleSort('invDate')}
                          >
                            <div className="flex items-center gap-1">
                              Date
                              {sortField === 'invDate' ? (
                                <span className="sort-indicator">{sortDir === 'asc' ? '↑' : '↓'}</span>
                              ) : (
                                <span className="sort-header-inactive"></span>
                              )}
                            </div>
                          </th>
                          <th
                            className="sort-header text-right px-4 py-3 font-medium text-muted-foreground"
                            onClick={() => toggleSort('amount')}
                          >
                            <div className="flex items-center justify-end gap-1">
                              Amount
                              {sortField === 'amount' ? (
                                <span className="sort-indicator">{sortDir === 'asc' ? '↑' : '↓'}</span>
                              ) : (
                                <span className="sort-header-inactive"></span>
                              )}
                            </div>
                          </th>
                          <th
                            className="sort-header text-right px-4 py-3 font-medium text-muted-foreground hidden sm:table-cell"
                            onClick={() => toggleSort('total')}
                          >
                            <div className="flex items-center justify-end gap-1">
                              Total
                              {sortField === 'total' ? (
                                <span className="sort-indicator">{sortDir === 'asc' ? '↑' : '↓'}</span>
                              ) : (
                                <span className="sort-header-inactive"></span>
                              )}
                            </div>
                          </th>
                          <th
                            className="sort-header text-center px-4 py-3 font-medium text-muted-foreground"
                            onClick={() => toggleSort('status')}
                          >
                            <div className="flex items-center justify-center gap-1">
                              Status
                              {sortField === 'status' ? (
                                <span className="sort-indicator">{sortDir === 'asc' ? '↑' : '↓'}</span>
                              ) : (
                                <span className="sort-header-inactive"></span>
                              )}
                            </div>
                          </th>
                          <th
                            className="sort-header text-right px-4 py-3 font-medium text-muted-foreground hidden lg:table-cell"
                            onClick={() => toggleSort('confidence')}
                          >
                            <div className="flex items-center justify-end gap-1">
                              Confidence
                              {sortField === 'confidence' ? (
                                <span className="sort-indicator">{sortDir === 'asc' ? '↑' : '↓'}</span>
                              ) : (
                                <span className="sort-header-inactive"></span>
                              )}
                            </div>
                          </th>
                          <th className="text-center px-4 py-3 font-medium text-muted-foreground" colSpan={2}>Actions</th>
                        </tr>
                      </thead>
                      <tbody>
                        {filteredInvoices.map((inv) => (
                          <InvoiceTableBody
                            key={inv.id}
                            invoice={inv}
                            isExpanded={expandedInvoice === inv.id}
                            onToggle={() => setExpandedInvoice(expandedInvoice === inv.id ? null : inv.id)}
                            onDelete={() => fetchDeleteInvoice(inv.id)}
                            isSelected={selectedInvoices.has(inv.id)}
                            onSelect={() => toggleSelect(inv.id)}
                            onEdit={() => openEditModal(inv)}
                          />
                        ))}
                      </tbody>
                    </table>
                  </div>
                </div>
              )}

              {/* Empty State */}
              {!store.isLoading && filteredInvoices.length === 0 && (
                <div className="empty-state">
                  <Inbox className="w-12 h-12" />
                  <p className="text-sm mt-2">No invoices found</p>
                  <p className="text-xs text-muted-foreground mt-1">Upload your first invoice to get started</p>
                </div>
              )}
            </section>
          )}

          {/* ═══════════════════════════════════════════════════════════════════════ */}
          {/* TAB 3: ANALYTICS                                                        */}
          {/* ═══════════════════════════════════════════════════════════════════════ */}
          {store.activeDashTab === 'analytics' && (
            <AnalyticsTab invoices={store.invoices} />
          )}

          {/* ═══════════════════════════════════════════════════════════════════════ */}
          {/* TAB 4: AI CHAT                                                         */}
          {/* ═══════════════════════════════════════════════════════════════════════ */}
          {store.activeDashTab === 'chat' && (
            <section className="fade-in tab-content-area">
              <div className="flex items-center justify-between mb-6">
                <div>
                  <h2 className="text-xl font-bold">AI Chat</h2>
                  <p className="text-sm text-muted-foreground mt-1">Ask questions about your invoices</p>
                </div>
                {store.chatHistory.length > 0 && (
                  <button
                    onClick={() => { store.clearChat(); toast.success('Chat cleared'); }}
                    className="text-sm text-muted-foreground hover:text-foreground transition-colors flex items-center gap-1"
                  >
                    <Trash2 className="w-3.5 h-3.5" />
                    Clear
                  </button>
                )}
              </div>

              {/* Plan gate */}
              {store.user.plan === 'free' && (
                <div className="glass-card p-8 text-center mb-6">
                  <Bot className="w-12 h-12 text-amber-500/40 mx-auto mb-4" />
                  <h3 className="text-lg font-semibold mb-2">Upgrade to Use AI Chat</h3>
                  <p className="text-sm text-muted-foreground mb-4">
                    AI Chat is available on Pro and Enterprise plans. Get instant insights about your invoices, spending analysis, and more.
                  </p>
                  <button
                    onClick={() => store.setActiveDashTab('upgrade')}
                    className="btn-primary text-sm inline-flex items-center gap-2"
                  >
                    <Crown className="w-4 h-4" />
                    Upgrade Plan
                  </button>
                </div>
              )}

              {/* Chat Messages */}
              {store.chatHistory.length === 0 && store.user.plan !== 'free' && (
                <div className="glass-card p-12 text-center mb-4">
                  <MessageSquare className="w-12 h-12 text-muted-foreground/30 mx-auto mb-4" />
                  <h3 className="text-base font-medium mb-2">Start a conversation</h3>
                  <p className="text-sm text-muted-foreground mb-6">
                    Ask about your invoices, get spending insights, or request analysis.
                  </p>
                  <div className="flex flex-wrap justify-center gap-2">
                    {['Summarize my invoices', 'Who is my top vendor?', 'Show spending trends'].map((suggestion) => (
                      <button
                        key={suggestion}
                        onClick={() => setChatInput(suggestion)}
                        className="px-3 py-1.5 rounded-full text-xs font-medium bg-white/[0.04] border border-white/[0.08] text-muted-foreground hover:text-foreground hover:bg-white/[0.06] transition-colors"
                      >
                        {suggestion}
                      </button>
                    ))}
                  </div>
                </div>
              )}

              <div className="space-y-4 mb-4 max-h-[500px] overflow-y-auto">
                {store.chatHistory.map((msg, idx) => (
                  <div
                    key={idx}
                    className={`chat-bubble flex ${msg.role === 'user' ? 'justify-end' : 'justify-start'}`}
                  >
                    <div className="flex flex-col">
                      <div className="flex items-end gap-2">
                        {msg.role === 'assistant' && (
                          <div className="w-6 h-6 rounded-full bg-teal-500/20 flex items-center justify-center shrink-0">
                            <Bot className="w-3.5 h-3.5 text-teal-500" />
                          </div>
                        )}
                        <div className={msg.role === 'user' ? 'chat-message-user' : 'chat-message-assistant'}>
                          <p className="whitespace-pre-wrap text-sm leading-relaxed">{msg.content}</p>
                        </div>
                      </div>
                    </div>
                  </div>
                ))}
                {store.chatLoading && (
                  <div className="chat-bubble flex justify-start">
                    <div className="bg-white/[0.06] rounded-2xl rounded-bl-md px-4 py-3 flex items-center gap-1.5">
                      <span className="w-2 h-2 rounded-full bg-muted-foreground typing-dot" />
                      <span className="w-2 h-2 rounded-full bg-muted-foreground typing-dot" />
                      <span className="w-2 h-2 rounded-full bg-muted-foreground typing-dot" />
                    </div>
                  </div>
                )}
                <div ref={chatEndRef} />
              </div>

              {/* Chat Input */}
              {store.user.plan !== 'free' && (
                <div className="flex gap-3">
                  <input
                    type="text"
                    value={chatInput}
                    onChange={(e) => setChatInput(e.target.value)}
                    onKeyDown={(e) => { if (e.key === 'Enter' && !e.shiftKey) { e.preventDefault(); fetchChat(); } }}
                    placeholder="Ask about your invoices..."
                    disabled={store.chatLoading}
                    className="input-field input-field-glow rounded-xl disabled:opacity-50"
                  />
                  <div className="tooltip-wrapper">
                    <button
                      onClick={fetchChat}
                      disabled={store.chatLoading || !chatInput.trim()}
                      className="p-3 rounded-xl bg-amber-500 text-zinc-900 hover:bg-amber-400 transition-colors disabled:opacity-50"
                      aria-label="Send message"
                    >
                      <Send className="w-5 h-5" />
                    </button>
                    <span className="tooltip-text">Send</span>
                  </div>
                </div>
              )}
            </section>
          )}

          {/* ═══════════════════════════════════════════════════════════════════════ */}
          {/* TAB 5: EXPORT                                                          */}
          {/* ═══════════════════════════════════════════════════════════════════════ */}
          {store.activeDashTab === 'export' && (
            <section className="fade-in tab-content-area">
              <div className="mb-6">
                <h2 className="text-xl font-bold">Export Data</h2>
                <p className="text-sm text-muted-foreground mt-1">Download your invoice data in your preferred format</p>
              </div>

              <div className="grid grid-cols-1 sm:grid-cols-2 lg:grid-cols-3 gap-6">
                {/* CSV Export */}
                <article className="glass-card p-6">
                  <div className="flex items-center gap-3 mb-4">
                    <div className="w-11 h-11 rounded-lg bg-teal-500/10 flex items-center justify-center">
                      <FileDown className="w-5 h-5 text-teal-500" />
                    </div>
                    <div>
                      <h3 className="text-base font-semibold">CSV Export</h3>
                      <p className="text-xs text-muted-foreground">Comma-separated values</p>
                    </div>
                  </div>
                  <p className="text-sm text-muted-foreground mb-6 leading-relaxed">
                    Download all your invoices as a CSV file, compatible with Excel, Google Sheets, and any spreadsheet application. Includes all fields: vendor, amounts, dates, status, and confidence scores.
                  </p>
                  <button
                    onClick={exportCsv}
                    className="w-full py-2.5 rounded-lg bg-teal-500 text-zinc-900 font-medium text-sm hover:bg-teal-400 transition-colors flex items-center justify-center gap-2"
                  >
                    <Download className="w-4 h-4" />
                    Download CSV
                  </button>
                </article>

                {/* JSON Export */}
                <article className="glass-card p-6">
                  <div className="flex items-center gap-3 mb-4">
                    <div className="w-11 h-11 rounded-lg bg-amber-500/10 flex items-center justify-center">
                      <FileJson className="w-5 h-5 text-amber-500" />
                    </div>
                    <div>
                      <div className="flex items-center gap-2">
                        <h3 className="text-base font-semibold">JSON Export</h3>
                        {store.user?.plan === 'free' && (
                          <span className="text-xs font-medium px-2 py-0.5 rounded-full bg-amber-500/10 text-amber-500 border border-amber-500/20">
                            Pro+
                          </span>
                        )}
                      </div>
                      <p className="text-xs text-muted-foreground">Structured JSON data with raw extraction</p>
                    </div>
                  </div>
                  <p className="text-sm text-muted-foreground mb-6 leading-relaxed">
                    Download structured JSON data including raw extraction results, line items, and bank details. Perfect for API integrations and programmatic processing.
                  </p>
                  <button
                    onClick={exportJson}
                    className="btn-primary text-sm w-full flex items-center justify-center gap-2 disabled:opacity-50 disabled:cursor-not-allowed"
                    disabled={store.user?.plan === 'free'}
                  >
                    <Download className="w-4 h-4" />
                    {store.user?.plan === 'free' ? 'Upgrade Required' : 'Download JSON'}
                  </button>
                </article>
              {/* XLSX Export */}
                <article className="glass-card p-6">
                  <div className="flex items-center gap-3 mb-4">
                    <div className="w-11 h-11 rounded-lg bg-amber-500/10 flex items-center justify-center">
                      <FileJson className="w-5 h-5 text-amber-500" />
                    </div>
                    <div>
                      <h3 className="text-base font-semibold">Excel Export</h3>
                      <p className="text-xs text-muted-foreground">XLSX spreadsheet format</p>
                    </div>
                  </div>
                  <p className="text-sm text-muted-foreground mb-6 leading-relaxed">
                    Download a professionally formatted Excel spreadsheet with styled headers, currency formatting, color-coded statuses, and a summary total row. Compatible with Microsoft Excel, Google Sheets, and LibreOffice Calc.
                  </p>
                  <button
                    onClick={() => { window.open('/api/invoices/export/xlsx', '_blank'); toast.success('Excel download started'); }}
                    className="btn-primary text-sm w-full flex items-center justify-center gap-2"
                  >
                    <Download className="w-4 h-4" />
                    Download Excel
                  </button>
                </article>
              </div>

              {/* Empty state hint */}
              {invoiceCount === 0 && (
                <div className="glass-card p-8 text-center mt-6">
                  <Database className="w-10 h-10 text-muted-foreground/30 mx-auto mb-3" />
                  <p className="text-sm text-muted-foreground">No invoices to export yet. Upload some invoices first.</p>
                </div>
              )}
            </section>
          )}

          {/* ═══════════════════════════════════════════════════════════════════════ */}
          {/* TAB 6: UPGRADE                                                         */}
          {/* ═══════════════════════════════════════════════════════════════════════ */}
          {store.activeDashTab === 'upgrade' && (
            <section className="fade-in tab-content-area">
              <div className="mb-6">
                <h2 className="text-xl font-bold">Upgrade Your Plan</h2>
                <p className="text-sm text-muted-foreground mt-1">You are currently on the <span className="text-amber-500 font-medium">{planLabel}</span> plan</p>
              </div>

              <div className="grid grid-cols-1 sm:grid-cols-2 lg:grid-cols-4 gap-6">
                {[
                  { name: 'Free', price: 0, limit: '20 invoices/mo', features: ['Basic extraction', 'CSV export', 'Email support'] },
                  { name: 'Pro', price: 129, limit: '2,000 invoices/mo', features: ['Advanced extraction', 'CSV & JSON export', 'AI Chat assistant', 'API access', 'Priority support'] },
                  { name: 'Business', price: 29, limit: '200 invoices/mo', features: ['Standard extraction', 'CSV & JSON export', 'API access', 'Email & chat support'] },
                  { name: 'Enterprise', price: 499, limit: 'Unlimited', features: ['Custom AI models', 'All export formats', 'Dedicated AI Chat', 'Full API access', 'Dedicated support', 'SLA guarantee'] },
                ].map((plan) => {
                  const isCurrent = plan.name.toLowerCase() === store.user.plan;
                  return (
                    <article
                      key={plan.name}
                      className={`glass-card p-6 flex flex-col relative ${plan.name === 'Pro' ? 'pricing-highlight' : ''}`}
                    >
                      {plan.name === 'Pro' && <div className="pricing-ribbon">Most Popular</div>}
                      <div className="mb-5">
                        <div className="flex items-center justify-between mb-1">
                          <h3 className="text-lg font-semibold">{plan.name}</h3>
                          {isCurrent && (
                            <span className="text-xs font-medium px-2 py-0.5 rounded-full bg-teal-500/10 text-teal-500 border border-teal-500/20">
                              Current
                            </span>
                          )}
                        </div>
                        <div className="text-3xl font-bold text-foreground">
                          ${plan.price}<span className="text-sm font-normal text-muted-foreground">/mo</span>
                        </div>
                        <div className="text-sm text-muted-foreground mt-1">{plan.limit}</div>
                      </div>
                      <ul className="space-y-3 mb-6 flex-1">
                        {plan.features.map((f) => (
                          <li key={f} className="flex items-center gap-2 text-sm text-muted-foreground">
                            <Check className="w-4 h-4 text-teal-500 shrink-0" />
                            {f}
                          </li>
                        ))}
                      </ul>
                      <button
                        onClick={() => {
                          if (isCurrent) return;
                          toast.info('Coming soon! Plan upgrades will be available shortly.');
                        }}
                        disabled={isCurrent}
                        className={`w-full py-2.5 rounded-lg text-sm font-medium transition-colors ${
                          isCurrent
                            ? 'bg-white/[0.04] text-muted-foreground cursor-not-allowed'
                            : 'bg-amber-500 text-zinc-900 hover:bg-amber-400'
                        }`}
                      >
                        {isCurrent ? 'Current Plan' : 'Upgrade'}
                      </button>
                    </article>
                  );
                })}
              </div>
            </section>
          )}

          {/* ═══════════════════════════════════════════════════════════════════════ */}
          {/* TAB 7: API ACCESS                                                      */}
          {/* ═══════════════════════════════════════════════════════════════════════ */}
          {store.activeDashTab === 'api' && (
            <section className="fade-in tab-content-area">
              <div className="mb-6">
                <h2 className="text-xl font-bold">API Access</h2>
                <p className="text-sm text-muted-foreground mt-1">Use your API key to integrate OmniParse into your workflow</p>
              </div>

              {/* API Key Display */}
              <div className="glass-card p-6 mb-6">
                <h3 className="text-sm font-semibold mb-3">Your API Key</h3>
                <div className="flex items-center gap-3">
                  <div className="flex-1 bg-white/[0.04] border border-white/[0.08] rounded-lg px-4 py-2.5 font-mono text-sm overflow-hidden">
                    {showApiKey
                      ? (store.user.apiKey || 'Not available')
                      : (store.user.apiKey ? store.user.apiKey.slice(0, 10) + '...' + store.user.apiKey.slice(-4) : 'Not available')
                    }
                  </div>
                  <div className="tooltip-wrapper">
                    <button
                      onClick={() => setShowApiKey(!showApiKey)}
                      className="p-2.5 rounded-lg bg-white/[0.04] border border-white/[0.08] text-muted-foreground hover:text-foreground transition-colors"
                      aria-label={showApiKey ? 'Hide API key' : 'Show API key'}
                    >
                      {showApiKey ? <EyeOff className="w-4 h-4" /> : <Eye className="w-4 h-4" />}
                    </button>
                    <span className="tooltip-text">{showApiKey ? 'Hide' : 'Show'}</span>
                  </div>
                  <div className="tooltip-wrapper">
                    <button
                      onClick={() => {
                        if (store.user.apiKey) {
                          navigator.clipboard.writeText(store.user.apiKey);
                          toast.success('API key copied to clipboard');
                        }
                      }}
                      className="p-2.5 rounded-lg bg-white/[0.04] border border-white/[0.08] text-muted-foreground hover:text-foreground transition-colors"
                      aria-label="Copy API key"
                    >
                      <Copy className="w-4 h-4" />
                    </button>
                    <span className="tooltip-text">Copy</span>
                  </div>
                </div>
              </div>

              {/* cURL Example */}
              <div className="glass-card p-6 mb-6">
                <h3 className="text-sm font-semibold mb-3 flex items-center gap-2">
                  <Terminal className="w-4 h-4 text-amber-500" />
                  Quick Start Example
                </h3>
                <div className="code-block">
                  <pre>{`curl -X POST /api/invoices/upload \\
  -H "Cookie: op_session=YOUR_SESSION" \\
  -F "files=@invoice.pdf"`}</pre>
                </div>
              </div>

              {/* Endpoint Documentation */}
              <div className="glass-card p-6">
                <h3 className="text-sm font-semibold mb-4">Available Endpoints</h3>
                <div className="space-y-4">
                  {[
                    { method: 'GET', path: '/api/auth/me', desc: 'Get current user info' },
                    { method: 'POST', path: '/api/invoices/upload', desc: 'Upload and process invoice files' },
                    { method: 'GET', path: '/api/invoices', desc: 'List all invoices with optional filters' },
                    { method: 'DELETE', path: '/api/invoices', desc: 'Delete an invoice by ID' },
                    { method: 'GET', path: '/api/invoices/export/csv', desc: 'Export all invoices as CSV' },
                    { method: 'GET', path: '/api/invoices/export/json', desc: 'Export all invoices as JSON (Pro+)' },
                    { method: 'POST', path: '/api/chat', desc: 'Send a message to the AI assistant (Pro+)' },
                  ].map((ep) => (
                    <div key={ep.path + ep.method} className="flex items-start gap-3 py-2 border-b border-white/[0.04] last:border-0">
                      <span className={`text-xs font-bold px-2 py-0.5 rounded ${
                        ep.method === 'GET' ? 'bg-teal-500/10 text-teal-500' :
                        ep.method === 'POST' ? 'bg-amber-500/10 text-amber-500' :
                        'bg-red-500/10 text-red-500'
                      }`}>
                        {ep.method}
                      </span>
                      <div className="min-w-0">
                        <div className="text-sm font-mono text-foreground">{ep.path}</div>
                        <div className="text-xs text-muted-foreground">{ep.desc}</div>
                      </div>
                    </div>
                  ))}
                </div>
              </div>
            </section>
          )}

          {/* ═══════════════════════════════════════════════════════════════════════ */}
          {/* TAB 8: PROFILE                                                         */}
          {/* ═══════════════════════════════════════════════════════════════════════ */}
          {store.activeDashTab === 'profile' && (
            <section className="fade-in tab-content-area">
              <div className="mb-6">
                <h2 className="text-xl font-bold">Profile</h2>
                <p className="text-sm text-muted-foreground mt-1">Manage your account settings</p>
              </div>

              {/* User Info Grid */}
              <div className="glass-card p-6 mb-6">
                <h3 className="text-sm font-semibold mb-4">Account Information</h3>
                <div className="grid grid-cols-1 sm:grid-cols-2 gap-4">
                  <div className="p-3 rounded-lg bg-white/[0.02]">
                    <div className="text-xs text-muted-foreground mb-1">Email</div>
                    <div className="text-sm font-medium flex items-center gap-2">
                      <Mail className="w-4 h-4 text-amber-500" />
                      {store.user.email}
                    </div>
                  </div>
                  <div className="p-3 rounded-lg bg-white/[0.02]">
                    <div className="text-xs text-muted-foreground mb-1">Name</div>
                    <div className="text-sm font-medium flex items-center gap-2">
                      <User className="w-4 h-4 text-amber-500" />
                      {store.user.name}
                    </div>
                  </div>
                  <div className="p-3 rounded-lg bg-white/[0.02]">
                    <div className="text-xs text-muted-foreground mb-1">Plan</div>
                    <div className="text-sm font-medium flex items-center gap-2">
                      <Crown className="w-4 h-4 text-amber-500" />
                      {planLabel}
                    </div>
                  </div>
                  <div className="p-3 rounded-lg bg-white/[0.02]">
                    <div className="text-xs text-muted-foreground mb-1">Member Since</div>
                    <div className="text-sm font-medium flex items-center gap-2">
                      <Building2 className="w-4 h-4 text-amber-500" />
                      {new Date(store.user.createdAt).toLocaleDateString('en-US', { year: 'numeric', month: 'long', day: 'numeric' })}
                    </div>
                  </div>
                  <div className="p-3 rounded-lg bg-white/[0.02] sm:col-span-2">
                    <div className="text-xs text-muted-foreground mb-1">API Key</div>
                    <div className="text-sm font-mono text-muted-foreground flex items-center gap-2">
                      <Key className="w-4 h-4 text-amber-500 shrink-0" />
                      <span className="truncate">{store.user.apiKey ? store.user.apiKey.slice(0, 20) + '...' : 'Not available'}</span>
                      {store.user.apiKey && (
                        <div className="tooltip-wrapper">
                          <button
                            onClick={() => {
                              navigator.clipboard.writeText(store.user.apiKey || '');
                              toast.success('Copied to clipboard');
                            }}
                            className="p-1 rounded hover:bg-white/[0.06] transition-colors shrink-0"
                            aria-label="Copy API key"
                          >
                            <Copy className="w-3.5 h-3.5" />
                          </button>
                          <span className="tooltip-text">Copy</span>
                        </div>
                      )}
                    </div>
                  </div>
                </div>
              </div>

              {/* Change Password */}
              <div className="glass-card p-6 mb-6">
                <h3 className="text-sm font-semibold mb-4">Change Password</h3>
                <form
                  onSubmit={async (e) => {
                    e.preventDefault();
                    if (!profileCurrentPw || !profileNewPw || !profileConfirmPw) {
                      toast.error('Please fill in all password fields');
                      return;
                    }
                    setProfilePwLoading(true);
                    try {
                      const res = await fetch('/api/auth/password', {
                        method: 'POST',
                        headers: { 'Content-Type': 'application/json' },
                        body: JSON.stringify({
                          currentPassword: profileCurrentPw,
                          newPassword: profileNewPw,
                          confirmPassword: profileConfirmPw,
                        }),
                      });
                      const data = await res.json();
                      if (!res.ok) {
                        toast.error(data.error || 'Failed to update password');
                        return;
                      }
                      toast.success('Password updated successfully. Please log in again.');
                      setProfileCurrentPw('');
                      setProfileNewPw('');
                      setProfileConfirmPw('');
                      setTimeout(() => { fetchLogout(); }, 1500);
                    } catch {
                      toast.error('Network error. Please try again.');
                    } finally {
                      setProfilePwLoading(false);
                    }
                  }}
                  className="space-y-4"
                >
                  <div>
                    <label className="input-label" htmlFor="current-pw">Current Password</label>
                    <input
                      id="current-pw"
                      type="password"
                      value={profileCurrentPw}
                      onChange={(e) => setProfileCurrentPw(e.target.value)}
                      placeholder="Enter current password"
                      className="input-field input-field-glow"
                    />
                  </div>
                  <div>
                    <label className="input-label" htmlFor="new-pw">New Password</label>
                    <input
                      id="new-pw"
                      type="password"
                      value={profileNewPw}
                      onChange={(e) => setProfileNewPw(e.target.value)}
                      placeholder="Enter new password"
                      className="input-field input-field-glow"
                    />
                    {profileNewPw && (() => {
                      const strength = getPasswordStrength(profileNewPw);
                      return (
                        <div className="mt-2">
                          <div className="h-1.5 w-full bg-zinc-800 rounded-full overflow-hidden">
                            <div className={`h-full ${strength.color} ${strength.width} rounded-full transition-all duration-300`} />
                          </div>
                          <p className="text-xs text-muted-foreground mt-1">{strength.label}</p>
                        </div>
                      );
                    })()}
                  </div>
                  <div>
                    <label className="input-label" htmlFor="confirm-new-pw">Confirm New Password</label>
                    <input
                      id="confirm-new-pw"
                      type="password"
                      value={profileConfirmPw}
                      onChange={(e) => setProfileConfirmPw(e.target.value)}
                      placeholder="Confirm new password"
                      className="input-field input-field-glow"
                    />
                  </div>
                  <button
                    type="submit"
                    disabled={profilePwLoading}
                    className="btn-primary text-sm flex items-center gap-2 disabled:opacity-50"
                  >
                    {profilePwLoading && <Loader2 className="w-4 h-4 animate-spin" />}
                    Update Password
                  </button>
                </form>
              </div>

              {/* Danger Zone */}
              <div className="glass-card p-6 border border-red-500/20">
                <h3 className="text-sm font-semibold mb-2 text-red-400">Danger Zone</h3>
                <p className="text-sm text-muted-foreground mb-4">
                  Permanently delete your account and all associated data. This action cannot be undone.
                </p>
                <button
                  onClick={() => {
                    if (window.confirm('Are you sure you want to delete your account? All your invoices and data will be permanently removed.')) {
                      toast.info('Account deletion feature coming soon');
                    }
                  }}
                  className="px-6 py-2.5 rounded-lg bg-red-500/10 text-red-400 border border-red-500/20 font-medium text-sm hover:bg-red-500/20 transition-colors"
                >
                  Delete Account
                </button>
              </div>
            </section>
          )}

          {/* ═══════════════════════════════════════════════════════════════════════ */}
          {/* TAB 10: SETTINGS                                                       */}
          {/* ═══════════════════════════════════════════════════════════════════════ */}
          {store.activeDashTab === 'settings' && (
            <section className="fade-in tab-content-area">
              <div className="mb-6">
                <h2 className="text-xl font-bold">Settings</h2>
                <p className="text-sm text-muted-foreground mt-1">Customize your dashboard experience</p>
              </div>

              <div className="space-y-6">
                {/* Appearance */}
                <div className="glass-card card-depth p-6 card-enter">
                  <h3 className="text-sm font-semibold mb-4 flex items-center gap-2">
                    <Sun className="w-4 h-4 text-amber-500" />
                    Appearance
                  </h3>
                  <div className="space-y-4">
                    <div className="flex items-center justify-between">
                      <div>
                        <div className="text-sm font-medium">Theme</div>
                        <div className="text-xs text-muted-foreground mt-0.5">Switch between light and dark mode</div>
                      </div>
                      <button
                        onClick={() => setTheme(theme === 'dark' ? 'light' : 'dark')}
                        className="flex items-center gap-2 px-4 py-2 rounded-lg bg-white/[0.04] border border-white/[0.08] hover:border-white/[0.15] transition-colors text-sm"
                      >
                        {theme === 'dark' ? <Sun className="w-4 h-4" /> : <Moon className="w-4 h-4" />}
                        {theme === 'dark' ? 'Light Mode' : 'Dark Mode'}
                      </button>
                    </div>
                    <div className="flex items-center justify-between">
                      <div>
                        <div className="text-sm font-medium">Compact Table View</div>
                        <div className="text-xs text-muted-foreground mt-0.5">Reduce row height in invoice tables</div>
                      </div>
                      <button
                        onClick={() => setCompactView(!compactView)}
                        className={`toggle-switch ${compactView ? 'active' : ''}`}
                      >
                        <span className="toggle-switch-knob" />
                      </button>
                    </div>
                  </div>
                </div>

                {/* Regional */}
                <div className="glass-card card-depth p-6 card-enter" style={{ animationDelay: '0.1s' }}>
                  <h3 className="text-sm font-semibold mb-4 flex items-center gap-2">
                    <Globe className="w-4 h-4 text-teal-500" />
                    Regional
                  </h3>
                  <div className="space-y-4">
                    <div>
                      <label className="text-sm font-medium block mb-1.5">Date Format</label>
                      <div className="flex gap-2">
                        {(['MM/DD/YYYY', 'DD/MM/YYYY', 'YYYY-MM-DD'] as const).map((fmt) => (
                          <button
                            key={fmt}
                            onClick={() => setDateFormat(fmt)}
                            className={`px-3 py-2 rounded-lg text-xs font-medium transition-colors ${
                              dateFormat === fmt
                                ? 'bg-amber-500 text-zinc-900'
                                : 'bg-white/[0.04] text-muted-foreground hover:text-foreground border border-white/[0.08]'
                            }`}
                          >
                            {fmt}
                          </button>
                        ))}
                      </div>
                    </div>
                    <div>
                      <label className="text-sm font-medium block mb-1.5">Default Currency</label>
                      <select
                        value={defaultCurrency}
                        onChange={(e) => setDefaultCurrency(e.target.value)}
                        className="select-field w-full sm:w-48"
                      >
                        <option value="USD">USD - US Dollar</option>
                        <option value="EUR">EUR - Euro</option>
                        <option value="GBP">GBP - British Pound</option>
                        <option value="CAD">CAD - Canadian Dollar</option>
                        <option value="AUD">AUD - Australian Dollar</option>
                        <option value="JPY">JPY - Japanese Yen</option>
                        <option value="CNY">CNY - Chinese Yuan</option>
                      </select>
                    </div>
                  </div>
                </div>

                {/* Notifications & Keyboard */}
                <div className="glass-card card-depth p-6 card-enter" style={{ animationDelay: '0.2s' }}>
                  <h3 className="text-sm font-semibold mb-4 flex items-center gap-2">
                    <Bell className="w-4 h-4 text-amber-500" />
                    Notifications & Shortcuts
                  </h3>
                  <div className="space-y-3">
                    <div className="flex items-center justify-between py-2">
                      <span className="text-sm text-muted-foreground">Global Search</span>
                      <kbd className="kbd">Ctrl+K</kbd>
                    </div>
                    <div className="flex items-center justify-between py-2">
                      <span className="text-sm text-muted-foreground">Keyboard Shortcuts</span>
                      <kbd className="kbd">Ctrl+/</kbd>
                    </div>
                    <div className="flex items-center justify-between py-2">
                      <span className="text-sm text-muted-foreground">Go to Invoices</span>
                      <kbd className="kbd">Ctrl+K</kbd>
                    </div>
                  </div>
                </div>

                {/* Save Button */}
                <div className="flex justify-end">
                  <button
                    onClick={saveSettings}
                    className={`btn-primary px-6 py-2.5 text-sm font-medium ${settingsSaved ? '!bg-green-500' : ''}`}
                  >
                    {settingsSaved ? (
                      <span className="flex items-center gap-2"><Check className="w-4 h-4" /> Saved!</span>
                    ) : 'Save Settings'}
                  </button>
                </div>
              </div>
            </section>
          )}

          {/* ═══════════════════════════════════════════════════════════════════════ */}
          {/* TAB 9: ACTIVITY                                                        */}
          {/* ═══════════════════════════════════════════════════════════════════════ */}
          {store.activeDashTab === 'activity' && (
            <section className="fade-in tab-content-area">
              <div className="mb-6">
                <h2 className="text-xl font-bold">Activity</h2>
                <p className="text-sm text-muted-foreground mt-1">Recent actions and events</p>
              </div>
              <div className="glass-card p-6">
                {activityLoading ? (
                  <div className="space-y-6">
                    {Array.from({ length: 3 }).map((_, i) => (
                      <div key={i} className="flex gap-4">
                        <div className="w-8 h-8 rounded-full bg-white/[0.04] border border-white/[0.08] shrink-0 skeleton-shimmer" />
                        <div className="flex-1 space-y-2 pt-0.5">
                          <div className="skeleton-shimmer h-4 w-2/5" />
                          <div className="skeleton-shimmer h-3 w-3/5" />
                        </div>
                      </div>
                    ))}
                  </div>
                ) : activityLogs.length > 0 ? (
                  <div className="relative">
                    {/* Vertical line */}
                    <div className="absolute left-[15px] top-2 bottom-2 w-px bg-white/[0.06]" />
                    <div className="space-y-6">
                      {activityLogs.map((log) => {
                        const iconMap: Record<string, LucideIcon> = {
                          login: LogIn,
                          upload: Upload,
                          delete: Trash2,
                          export: Download,
                          edit: Pencil,
                          signup: UserPlus,
                          password_change: Lock,
                        };
                        const colorMap: Record<string, string> = {
                          login: 'text-amber-500',
                          upload: 'text-teal-500',
                          delete: 'text-red-400',
                          export: 'text-amber-500',
                          edit: 'text-blue-400',
                          signup: 'text-green-400',
                          password_change: 'text-amber-500',
                        };
                        const ItemIcon = iconMap[log.type] || Clock;
                        const itemColor = colorMap[log.type] || 'text-muted-foreground';
                        return (
                          <div key={log.id} className="relative flex gap-4">
                            <div className="relative z-10 w-8 h-8 rounded-full bg-white/[0.04] border border-white/[0.08] flex items-center justify-center shrink-0">
                              <ItemIcon className={`w-3.5 h-3.5 ${itemColor}`} />
                            </div>
                            <div className="min-w-0 flex-1 pt-0.5">
                              <div className="flex items-center justify-between gap-2">
                                <h4 className="text-sm font-medium">{log.title}</h4>
                                <span className="text-xs text-muted-foreground shrink-0">{relativeTime(log.createdAt)}</span>
                              </div>
                              <p className="text-xs text-muted-foreground mt-0.5">{log.description}</p>
                            </div>
                          </div>
                        );
                      })}
                    </div>
                  </div>
                ) : (
                  <div className="text-center py-8 text-muted-foreground">
                    <Clock className="w-10 h-10 mx-auto mb-3 opacity-40" />
                    <p className="text-sm">No activity yet. Actions will appear here as you use the app.</p>
                  </div>
                )}
              </div>
            </section>
          )}
          </div>
        </main>

        {showCommandPalette && (
          <div className="command-palette-backdrop" onClick={() => setShowCommandPalette(false)}>
            <div className="command-palette" onClick={e => e.stopPropagation()}>
              <div className="command-palette-input">
                <Command className="w-5 h-5 text-muted-foreground shrink-0" />
                <input
                  autoFocus
                  type="text"
                  value={commandSearch}
                  onChange={e => setCommandSearch(e.target.value)}
                  placeholder="Search invoices, navigate tabs..."
                  onKeyDown={e => { if (e.key === 'Escape') setShowCommandPalette(false); }}
                />
                <kbd className="kbd">ESC</kbd>
              </div>
              <div className="command-palette-results scrollbar-thin">
                <div className="command-palette-section-title">Quick Actions</div>
                {DASH_TABS.filter(t => !commandSearch || t.label.toLowerCase().includes(commandSearch.toLowerCase())).map(tab => {
                  const Icon = tab.icon;
                  return (
                    <button
                      key={tab.id}
                      onClick={() => { store.setActiveDashTab(tab.id); setShowCommandPalette(false); setCommandSearch(''); }}
                      className="command-palette-item"
                    >
                      <Icon className="w-4 h-4" />
                      {tab.label}
                    </button>
                  );
                })}
                {commandSearch && (
                  <>
                    <div className="command-palette-section-title" style={{ marginTop: 8 }}>Invoice Results</div>
                    {store.invoices
                      .filter(inv => !commandSearch || inv.vendor?.toLowerCase().includes(commandSearch.toLowerCase()) || inv.invNumber?.toLowerCase().includes(commandSearch.toLowerCase()))
                      .slice(0, 5)
                      .map(inv => (
                        <button
                          key={inv.id}
                          onClick={() => { store.setActiveDashTab('invoices'); setExpandedInvoice(inv.id); setShowCommandPalette(false); setCommandSearch(''); }}
                          className="command-palette-item justify-between"
                        >
                          <div>
                            <div className="font-medium text-foreground">{inv.vendor || 'Unknown'}</div>
                            <div className="command-palette-result-sub">{inv.invNumber || 'No invoice #'}</div>
                          </div>
                          <div className="text-right">
                            <div className="font-medium">{formatCurrency(inv.total, inv.currency)}</div>
                            <div className="command-palette-result-sub">{inv.invDate || ''}</div>
                          </div>
                        </button>
                      ))
                    }
                    {store.invoices.filter(inv => inv.vendor?.toLowerCase().includes(commandSearch.toLowerCase()) || inv.invNumber?.toLowerCase().includes(commandSearch.toLowerCase())).length === 0 && (
                      <div className="px-3 py-4 text-center text-sm text-muted-foreground">No matching invoices</div>
                    )}
                  </>
                )}
              </div>
            </div>
          </div>
        )}

        <footer className="dash-footer py-4 pb-[env(safe-area-inset-bottom)] px-4 sm:px-6 lg:px-8">
          <div className="max-w-7xl mx-auto flex flex-col sm:flex-row items-center justify-between gap-2 text-xs text-muted-foreground">
            <div className="flex items-center gap-2">
              <div className="w-4 h-4 rounded-md bg-amber-500 flex items-center justify-center">
                <ScanLine className="w-2.5 h-2.5 text-zinc-900" />
              </div>
              <span>OmniParse AI</span>
            </div>
            <div className="flex items-center gap-4">
              <span>Privacy</span>
              <span>Terms</span>
              <span>Support</span>
            </div>
          </div>
        </footer>

        {showShortcuts && (
          <div className="modal-overlay" onClick={() => setShowShortcuts(false)}>
            <div className="modal-content" onClick={(e) => e.stopPropagation()}>
              <div className="flex items-center justify-between mb-6">
                <h2 className="text-lg font-bold">Keyboard Shortcuts</h2>
                <button
                  onClick={() => setShowShortcuts(false)}
                  className="p-1 rounded-lg text-muted-foreground hover:text-foreground hover:bg-white/[0.06] transition-colors"
                  aria-label="Close"
                >
                  <X className="w-4 h-4" />
                </button>
              </div>
              <div className="space-y-3">
                {[
                  { keys: ['Ctrl', 'K'], desc: 'Go to Invoices' },
                  { keys: ['Ctrl', '/'], desc: 'Show shortcuts' },
                  { keys: ['Enter'], desc: 'Send chat message' },
                ].map((s) => (
                  <div key={s.desc} className="flex items-center justify-between py-2">
                    <span className="text-sm text-muted-foreground">{s.desc}</span>
                    <div className="flex items-center gap-1">
                      {s.keys.map((k, i) => (
                        <span key={k}>
                          <span className="kbd">{k}</span>
                          {i < s.keys.length - 1 && <span className="text-muted-foreground mx-1">+</span>}
                        </span>
                      ))}
                    </div>
                  </div>
                ))}
              </div>
            </div>
          </div>
        )}

        {editingInvoice && (
          <div className="modal-overlay" onClick={() => setEditingInvoice(null)}>
            <div className="modal-content max-w-lg" onClick={(e) => e.stopPropagation()}>
              <div className="flex items-center justify-between mb-6">
                <h2 className="text-lg font-bold">Edit Invoice</h2>
                <button onClick={() => setEditingInvoice(null)} className="p-1 rounded-lg text-muted-foreground hover:text-foreground hover:bg-white/[0.06] transition-colors" aria-label="Close">
                  <X className="w-4 h-4" />
                </button>
              </div>
              <div className="space-y-4">
                {[
                  { key: 'vendor', label: 'Vendor' },
                  { key: 'invNumber', label: 'Invoice Number' },
                  { key: 'invDate', label: 'Invoice Date' },
                  { key: 'dueDate', label: 'Due Date' },
                  { key: 'amount', label: 'Amount' },
                  { key: 'vatAmount', label: 'VAT Amount' },
                  { key: 'total', label: 'Total' },
                  { key: 'currency', label: 'Currency' },
                ].map(({ key, label }) => (
                  <div key={key}>
                    <label className="input-label">{label}</label>
                    <input
                      type="text"
                      value={editForm[key] || ''}
                      onChange={(e) => setEditForm(prev => ({ ...prev, [key]: e.target.value }))}
                      className="input-field input-field-glow"
                    />
                  </div>
                ))}
                <div>
                  <label className="input-label">Status</label>
                  <div className="flex gap-3">
                    {['done', 'review'].map((s) => (
                      <button
                        key={s}
                        onClick={() => setEditForm(prev => ({ ...prev, status: s }))}
                        className={`flex-1 py-2.5 rounded-lg text-sm font-medium transition-colors ${
                          editForm.status === s
                            ? s === 'done' ? 'bg-teal-500 text-zinc-900' : 'bg-amber-500 text-zinc-900'
                            : 'bg-white/[0.04] text-muted-foreground border border-white/[0.08]'
                        }`}
                      >
                        {s === 'done' ? 'Done' : 'Review'}
                      </button>
                    ))}
                  </div>
                </div>
              </div>
              <div className="flex gap-3 mt-6">
                <button onClick={() => setEditingInvoice(null)} className="btn-ghost text-sm flex-1">Cancel</button>
                <button onClick={saveEdit} disabled={editSaving} className="btn-primary text-sm flex-1 flex items-center justify-center gap-2 disabled:opacity-50">
                  {editSaving && <Loader2 className="w-4 h-4 animate-spin" />}
                  Save Changes
                </button>
              </div>
            </div>
          </div>
        )}

        {/* ── HF Space Download ── */}
        <a
          href="/omniparse-hf-space.zip"
          download
          className="fixed bottom-5 right-5 z-50 bg-amber-500 hover:bg-amber-400 text-black font-semibold text-xs px-3 py-2 rounded-lg shadow-lg transition-colors"
        >
          ⬇ Download HF Space Files (.zip)
        </a>
      </div>
    );
  }
  return null;
}

// ──────────────────────────────────────────────────────────────────────────────
// ─── Analytics Tab Component ────────────────────────────────────────────────────

const CHART_COLORS = {
  amber: '#F59E0B',
  teal: '#14B8A6',
  red: '#EF4444',
  green: '#22C55E',
  amberLight: 'rgba(245, 158, 11, 0.15)',
};

function AnalyticsTab({ invoices }: { invoices: InvoiceRow[] }) {
  // ── Computed analytics data ──
  const monthlyData = useMemo(() => {
    const map = new Map<string, number>();
    invoices.forEach((inv) => {
      const dateStr = inv.invDate || inv.createdAt;
      if (!dateStr) return;
      const monthKey = dateStr.substring(0, 7); // YYYY-MM
      map.set(monthKey, (map.get(monthKey) || 0) + (inv.total ?? 0));
    });
    return Array.from(map.entries())
      .sort(([a], [b]) => a.localeCompare(b))
      .map(([month, total]) => {
        const [y, m] = month.split('-');
        const label = new Date(Number(y), Number(m) - 1).toLocaleString('en-US', {
          month: 'short', year: '2-digit',
        });
        return { month: label, total: Math.round(total * 100) / 100 };
      });
  }, [invoices]);

  const vendorData = useMemo(() => {
    const map = new Map<string, number>();
    invoices.forEach((inv) => {
      const vendor = inv.vendor || 'Unknown';
      map.set(vendor, (map.get(vendor) || 0) + (inv.total ?? 0));
    });
    return Array.from(map.entries())
      .sort((a, b) => b[1] - a[1])
      .slice(0, 8)
      .map(([name, total]) => ({ name: name.length > 18 ? name.slice(0, 16) + '…' : name, total: Math.round(total * 100) / 100 }));
  }, [invoices]);

  const statusData = useMemo(() => {
    const counts = { Done: 0, Review: 0, Duplicate: 0 };
    invoices.forEach((inv) => {
      if (inv.status === 'done') counts.Done++;
      else if (inv.status === 'review') counts.Review++;
      else if (inv.status === 'duplicate' || inv.isDuplicate) counts.Duplicate++;
    });
    return [
      { name: 'Done', value: counts.Done, color: CHART_COLORS.teal },
      { name: 'Review', value: counts.Review, color: CHART_COLORS.amber },
      { name: 'Duplicate', value: counts.Duplicate, color: CHART_COLORS.red },
    ].filter((d) => d.value > 0);
  }, [invoices]);

  const currencyData = useMemo(() => {
    const map = new Map<string, number>();
    invoices.forEach((inv) => map.set(inv.currency, (map.get(inv.currency) || 0) + 1));
    const entries = Array.from(map.entries()).sort((a, b) => b[1] - a[1]);
    const maxCount = entries.length > 0 ? entries[0][1] : 1;
    return entries.map(([currency, count]) => ({ currency, count, pct: Math.round((count / maxCount) * 100) }));
  }, [invoices]);

  const quickStats = useMemo(() => {
    const totals = invoices.map((inv) => inv.total ?? 0).filter((v) => v > 0);
    const vendors = invoices.filter((inv) => inv.vendor);
    const vendorCounts = new Map<string, number>();
    vendors.forEach((inv) => {
      const v = inv.vendor as string;
      vendorCounts.set(v, (vendorCounts.get(v) || 0) + 1);
    });
    let mostActive = '--';
    let maxCount = 0;
    vendorCounts.forEach((count, name) => {
      if (count > maxCount) { mostActive = name; maxCount = count; }
    });
    return {
      highest: totals.length > 0 ? Math.max(...totals) : null,
      lowest: totals.length > 0 ? Math.min(...totals) : null,
      mostActiveVendor: mostActive,
      average: totals.length > 0 ? totals.reduce((a, b) => a + b, 0) / totals.length : null,
    };
  }, [invoices]);

  const hasData = invoices.length > 0;

  return (
    <section className="fade-in">
      {/* Section Header */}
      <div className="mb-6">
        <h2 className="text-xl font-bold">Analytics</h2>
        <p className="text-sm text-muted-foreground mt-1">Visual insights into your invoice data</p>
      </div>

      {!hasData ? (
        <div className="glass-card p-12 text-center empty-state">
          <BarChart3 className="w-12 h-12" />
          <p className="text-sm mt-2">No invoice data yet</p>
          <p className="text-xs text-muted-foreground mt-1">Upload invoices to see analytics here</p>
        </div>
      ) : (
        <>
          {/* ── Quick Stats Summary ── */}
          <div className="grid grid-cols-2 lg:grid-cols-4 gap-3 mb-6" aria-label="Quick statistics">
            <div className="stat-card-glass card-accent-amber card-enter">
              <div className="flex items-center gap-2 mb-1">
                <TrendingUp className="w-4 h-4 text-amber-500" />
                <span className="text-xs text-muted-foreground font-medium uppercase tracking-wider">Highest Invoice</span>
              </div>
              <div className="text-lg font-bold text-foreground">
                {quickStats.highest !== null ? formatCurrency(quickStats.highest) : '--'}
              </div>
            </div>
            <div className="stat-card-glass card-accent-teal card-enter">
              <div className="flex items-center gap-2 mb-1">
                <TrendingUp className="w-4 h-4 text-teal-500" />
                <span className="text-xs text-muted-foreground font-medium uppercase tracking-wider">Lowest Invoice</span>
              </div>
              <div className="text-lg font-bold text-foreground">
                {quickStats.lowest !== null ? formatCurrency(quickStats.lowest) : '--'}
              </div>
            </div>
            <div className="stat-card-glass card-accent-green card-enter">
              <div className="flex items-center gap-2 mb-1">
                <Building2 className="w-4 h-4 text-green-400" />
                <span className="text-xs text-muted-foreground font-medium uppercase tracking-wider">Most Active Vendor</span>
              </div>
              <div className="text-lg font-bold text-foreground truncate" title={quickStats.mostActiveVendor}>
                {quickStats.mostActiveVendor}
              </div>
            </div>
            <div className="stat-card-glass card-accent-amber card-enter">
              <div className="flex items-center gap-2 mb-1">
                <CircleDollarSign className="w-4 h-4 text-amber-500" />
                <span className="text-xs text-muted-foreground font-medium uppercase tracking-wider">Average Amount</span>
              </div>
              <div className="text-lg font-bold text-foreground">
                {quickStats.average !== null ? formatCurrency(quickStats.average) : '--'}
              </div>
            </div>
          </div>

          {/* ── Charts Grid ── */}
          <div className="grid grid-cols-1 lg:grid-cols-2 gap-6 mb-6">
            {/* a) Monthly Spending Trend */}
            <div className="glass-card p-6">
              <h3 className="text-sm font-semibold mb-3">Monthly Spending Trend</h3>
              {monthlyData.length === 0 ? (
                <div className="h-48 flex items-center justify-center text-muted-foreground text-xs">
                  No date data available
                </div>
              ) : (
                <ResponsiveContainer width="100%" height={220}>
                  <AreaChart data={monthlyData} margin={{ top: 5, right: 10, left: 0, bottom: 0 }}>
                    <defs>
                      <linearGradient id="amberGrad" x1="0" y1="0" x2="0" y2="1">
                        <stop offset="0%" stopColor={CHART_COLORS.amber} stopOpacity={0.3} />
                        <stop offset="100%" stopColor={CHART_COLORS.amber} stopOpacity={0.02} />
                      </linearGradient>
                    </defs>
                    <CartesianGrid strokeDasharray="3 3" stroke="rgba(255,255,255,0.06)" />
                    <XAxis dataKey="month" tick={{ fontSize: 11, fill: '#a1a1aa' }} axisLine={{ stroke: 'rgba(255,255,255,0.1)' }} />
                    <YAxis tick={{ fontSize: 11, fill: '#a1a1aa' }} axisLine={{ stroke: 'rgba(255,255,255,0.1)' }} tickFormatter={(v: number) => `$${(v / 1000).toFixed(v >= 1000 ? 1 : 0)}k`} />
                    <Tooltip
                      contentStyle={{ className: 'recharts-tooltip-custom' }}
                      wrapperStyle={{ outline: 'none' }}
                      formatter={(value: number) => [formatCurrency(value), 'Total']}
                    />
                    <Area type="monotone" dataKey="total" stroke={CHART_COLORS.amber} strokeWidth={2} fill="url(#amberGrad)" />
                  </AreaChart>
                </ResponsiveContainer>
              )}
            </div>

            {/* b) Vendor Spending Breakdown */}
            <div className="glass-card p-6">
              <h3 className="text-sm font-semibold mb-3">Top Vendors by Spending</h3>
              {vendorData.length === 0 ? (
                <div className="h-48 flex items-center justify-center text-muted-foreground text-xs">
                  No vendor data available
                </div>
              ) : (
                <ResponsiveContainer width="100%" height={220}>
                  <BarChart data={vendorData} layout="vertical" margin={{ top: 5, right: 10, left: 0, bottom: 0 }}>
                    <CartesianGrid strokeDasharray="3 3" stroke="rgba(255,255,255,0.06)" horizontal={false} />
                    <XAxis type="number" tick={{ fontSize: 11, fill: '#a1a1aa' }} axisLine={{ stroke: 'rgba(255,255,255,0.1)' }} tickFormatter={(v: number) => `$${(v / 1000).toFixed(v >= 1000 ? 1 : 0)}k`} />
                    <YAxis type="category" dataKey="name" tick={{ fontSize: 11, fill: '#a1a1aa' }} width={110} axisLine={{ stroke: 'rgba(255,255,255,0.1)' }} />
                    <Tooltip
                      contentStyle={{ className: 'recharts-tooltip-custom' }}
                      wrapperStyle={{ outline: 'none' }}
                      formatter={(value: number) => [formatCurrency(value), 'Total']}
                    />
                    <Bar dataKey="total" fill={CHART_COLORS.teal} radius={[0, 4, 4, 0]} barSize={16} />
                  </BarChart>
                </ResponsiveContainer>
              )}
            </div>
          </div>

          <div className="grid grid-cols-1 lg:grid-cols-2 gap-6">
            {/* c) Status Distribution */}
            <div className="glass-card p-6">
              <h3 className="text-sm font-semibold mb-3">Status Distribution</h3>
              {statusData.length === 0 ? (
                <div className="h-48 flex items-center justify-center text-muted-foreground text-xs">
                  No status data available
                </div>
              ) : (
                <div className="flex items-center gap-6">
                  <div className="flex-1" style={{ minHeight: 180 }}>
                    <ResponsiveContainer width="100%" height={180}>
                      <PieChart>
                        <Pie
                          data={statusData}
                          cx="50%"
                          cy="50%"
                          innerRadius={50}
                          outerRadius={75}
                          paddingAngle={3}
                          dataKey="value"
                          stroke="none"
                        >
                          {statusData.map((entry, idx) => (
                            <Cell key={`cell-${idx}`} fill={entry.color} />
                          ))}
                        </Pie>
                        <Tooltip
                          contentStyle={{ className: 'recharts-tooltip-custom' }}
                          wrapperStyle={{ outline: 'none' }}
                          formatter={(value: number) => [`${value} invoices`, 'Count']}
                        />
                      </PieChart>
                    </ResponsiveContainer>
                  </div>
                  <div className="flex flex-col gap-3 min-w-[120px]">
                    {statusData.map((entry) => (
                      <div key={entry.name} className="flex items-center gap-2">
                        <span className="w-3 h-3 rounded-full flex-shrink-0" style={{ backgroundColor: entry.color }} />
                        <div>
                          <span className="text-sm text-foreground font-medium">{entry.name}</span>
                          <span className="text-xs text-muted-foreground ml-2">{entry.value}</span>
                        </div>
                      </div>
                    ))}
                  </div>
                </div>
              )}
            </div>

            {/* d) Currency Distribution */}
            <div className="glass-card p-6">
              <h3 className="text-sm font-semibold mb-3">Currency Distribution</h3>
              {currencyData.length === 0 ? (
                <div className="h-48 flex items-center justify-center text-muted-foreground text-xs">
                  No currency data available
                </div>
              ) : (
                <div className="space-y-3 mt-1">
                  {currencyData.map(({ currency, count, pct }) => (
                    <div key={currency} className="flex items-center gap-3">
                      <span className="text-sm text-foreground font-medium w-16 text-right">{currency}</span>
                      <div className="flex-1 h-6 bg-white/[0.04] rounded overflow-hidden">
                        <div
                          className="progress-bar h-full rounded"
                          style={{ width: `${pct}%`, backgroundColor: CHART_COLORS.teal }}
                        />
                      </div>
                      <span className="text-xs text-muted-foreground w-8 text-right">{count}</span>
                    </div>
                  ))}
                </div>
              )}
            </div>
          </div>
        </>
      )}
    </section>
  );
}

// Invoice Table Row Component (with expandable detail panel)
// ──────────────────────────────────────────────────────────────────────────────

function InvoiceTableBody({
  invoice,
  isExpanded,
  onToggle,
  onDelete,
  isSelected,
  onSelect,
  onEdit,
}: {
  invoice: InvoiceRow;
  isExpanded: boolean;
  onToggle: () => void;
  onDelete: () => void;
  isSelected: boolean;
  onSelect: () => void;
  onEdit?: () => void;
}) {
  const statusBadgeClass = invoice.isDuplicate ? 'badge-amber-enhanced' : invoice.status === 'review' ? 'badge-amber-enhanced' : 'badge-teal-enhanced';
  const statusLabel = invoice.isDuplicate ? 'Duplicate' : invoice.status === 'review' ? 'Review' : 'Done';

  return (
    <>
      <tr
        className="invoice-row-enhanced border-b border-white/[0.04]"
        onClick={onToggle}
        role="button"
        tabIndex={0}
        onKeyDown={(e) => { if (e.key === 'Enter' || e.key === ' ') onToggle(); }}
        aria-expanded={isExpanded}
      >
        <td className="px-3 py-3 w-10">
          <input
            type="checkbox"
            checked={isSelected}
            onChange={(e) => { e.stopPropagation(); onSelect(); }}
            onClick={(e) => e.stopPropagation()}
            className="invoice-checkbox"
            aria-label={`Select invoice ${invoice.vendor || invoice.invNumber}`}
          />
        </td>
        <td className="px-4 py-3">
          <div className="flex items-center gap-2">
            {isExpanded ? <ChevronUp className="w-3.5 h-3.5 text-muted-foreground shrink-0" /> : <ChevronDown className="w-3.5 h-3.5 text-muted-foreground shrink-0" />}
            <span className="truncate max-w-[120px] sm:max-w-none">{invoice.vendor || '--'}</span>
          </div>
        </td>
        <td className="px-4 py-3 text-muted-foreground hidden sm:table-cell">{invoice.invNumber || '--'}</td>
        <td className="px-4 py-3 text-muted-foreground hidden md:table-cell">{invoice.invDate || '--'}</td>
        <td className="px-4 py-3 text-right font-medium">{formatCurrency(invoice.amount, invoice.currency)}</td>
        <td className="px-4 py-3 text-right font-medium hidden sm:table-cell">{formatCurrency(invoice.total, invoice.currency)}</td>
        <td className="px-4 py-3 text-center">
          <span className={`badge-pill ${statusBadgeClass}`}>
            <span className={`badge-pill-dot ${invoice.isDuplicate ? 'bg-amber-500' : invoice.status === 'review' ? 'bg-amber-500' : 'bg-teal-500'}`} />
            {statusLabel}
          </span>
        </td>
        <td className="px-4 py-3 text-right hidden lg:table-cell">
          {invoice.confidence ? `${(invoice.confidence * 100).toFixed(1)}%` : '--'}
        </td>
        <td className="px-4 py-3 text-center">
          <div className="flex items-center justify-center gap-1">
            <div className="tooltip-wrapper">
              <button
                onClick={(e) => { e.stopPropagation(); onEdit?.(); }}
                className="p-1.5 rounded-lg text-muted-foreground hover:text-amber-500 hover:bg-amber-500/10 transition-colors"
                aria-label="Edit invoice"
              >
                <Pencil className="w-4 h-4" />
              </button>
              <span className="tooltip-text">Edit</span>
            </div>
            <div className="tooltip-wrapper">
              <button
                onClick={(e) => { e.stopPropagation(); onDelete(); }}
                className="p-1.5 rounded-lg text-muted-foreground hover:text-red-400 hover:bg-red-500/10 transition-colors"
                aria-label="Delete invoice"
              >
                <Trash2 className="w-4 h-4" />
              </button>
              <span className="tooltip-text">Delete</span>
            </div>
          </div>
        </td>
      </tr>
      {isExpanded && (
        <tr>
          <td colSpan={10} className="p-0">
            <div className="px-4 py-4 bg-white/[0.02] border-b border-white/[0.04] fade-in">
              <h4 className="text-xs font-semibold text-muted-foreground uppercase tracking-wider mb-3">Invoice Details</h4>
              <div className="grid grid-cols-2 sm:grid-cols-4 gap-4 mb-4">
                <div>
                  <div className="text-xs text-muted-foreground">Filename</div>
                  <div className="text-sm mt-0.5">{invoice.filename || '--'}</div>
                </div>
                <div>
                  <div className="text-xs text-muted-foreground">Due Date</div>
                  <div className="text-sm mt-0.5">{invoice.dueDate || '--'}</div>
                </div>
                <div>
                  <div className="text-xs text-muted-foreground">VAT Amount</div>
                  <div className="text-sm mt-0.5">{formatCurrency(invoice.vatAmount, invoice.currency)}</div>
                </div>
                <div>
                  <div className="text-xs text-muted-foreground">Created</div>
                  <div className="text-sm mt-0.5">{new Date(invoice.createdAt).toLocaleDateString()}</div>
                </div>
              </div>
              <h4 className="text-xs font-semibold text-muted-foreground uppercase tracking-wider mb-2">Raw JSON</h4>
              <div className="bg-zinc-950 rounded-lg p-4 font-mono text-xs text-muted-foreground max-h-48 overflow-y-auto">
                <pre>{JSON.stringify({
                  vendor: invoice.vendor,
                  invNumber: invoice.invNumber,
                  invDate: invoice.invDate,
                  dueDate: invoice.dueDate,
                  amount: invoice.amount,
                  vatAmount: invoice.vatAmount,
                  total: invoice.total,
                  currency: invoice.currency,
                  confidence: invoice.confidence,
                  status: invoice.status,
                  isDuplicate: invoice.isDuplicate,
                  filename: invoice.filename,
                }, null, 2)}</pre>
              </div>
            </div>
          </td>
        </tr>
      )}
    </>
  );
}