File size: 86,511 Bytes
e6cdb3e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
98bd008
e6cdb3e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
83cfada
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
d8c6e6c
83cfada
 
d8c6e6c
83cfada
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
e6cdb3e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
83cfada
e6cdb3e
 
83cfada
 
 
 
 
 
 
 
 
 
 
3c606c4
83cfada
3c606c4
83cfada
 
 
 
 
 
 
 
3c606c4
83cfada
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
3c606c4
83cfada
 
 
 
 
 
 
 
fcf1e23
83cfada
 
 
 
 
 
 
 
3c606c4
83cfada
 
 
abd20b2
83cfada
 
5e94278
3c606c4
83cfada
 
3c606c4
 
83cfada
3c606c4
83cfada
 
e6cdb3e
c2fd3c1
83cfada
 
 
 
e6cdb3e
 
 
 
 
 
83cfada
e6cdb3e
 
 
 
 
83cfada
e6cdb3e
 
83cfada
 
 
 
 
 
e6cdb3e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
83cfada
 
e6cdb3e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
abd20b2
 
 
5e94278
 
abd20b2
5e94278
abd20b2
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
5e94278
abd20b2
 
 
 
 
 
 
 
 
 
 
 
 
 
 
5e94278
abd20b2
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
5e94278
 
abd20b2
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
5e94278
abd20b2
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
d4a120c
abd20b2
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
5e94278
 
 
 
abd20b2
e6cdb3e
 
 
abd20b2
 
e6cdb3e
abd20b2
 
e6cdb3e
 
 
 
 
 
 
 
 
 
 
 
1f20379
abd20b2
 
 
 
 
 
 
 
 
 
 
e6cdb3e
 
1f20379
abd20b2
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
e6cdb3e
 
abd20b2
 
 
 
 
1f20379
abd20b2
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
e6cdb3e
abd20b2
 
 
 
 
 
 
 
 
 
 
 
 
 
 
e6cdb3e
abd20b2
e6cdb3e
abd20b2
 
 
 
 
 
 
 
 
 
 
1f20379
abd20b2
 
 
 
 
 
 
e6cdb3e
abd20b2
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
c2fd3c1
abd20b2
c2fd3c1
abd20b2
 
 
e6cdb3e
 
1f20379
abd20b2
 
e6cdb3e
abd20b2
 
 
 
 
 
 
e6cdb3e
abd20b2
 
 
 
 
 
 
 
 
 
 
 
 
 
e6cdb3e
abd20b2
e6cdb3e
abd20b2
 
 
e6cdb3e
abd20b2
 
 
 
 
 
 
 
 
 
 
 
e6cdb3e
abd20b2
 
 
 
 
 
 
 
 
 
 
 
 
e6cdb3e
abd20b2
1f20379
abd20b2
 
 
 
 
 
 
 
 
 
 
 
 
 
1f20379
abd20b2
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
927cd5c
abd20b2
 
c2fd3c1
e6cdb3e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
f94afff
 
de6f556
c2fd3c1
4ff9712
 
 
 
 
 
de6f556
c2fd3c1
5e94278
 
af5a7d4
5e94278
3070fa6
 
 
 
 
4a095b8
 
 
 
3070fa6
4a095b8
 
 
 
 
 
 
 
 
 
 
 
5e94278
af5a7d4
 
 
3070fa6
 
 
4a095b8
af5a7d4
 
4a095b8
3070fa6
 
4a095b8
3070fa6
4a095b8
3070fa6
4a095b8
3070fa6
 
 
4a095b8
af5a7d4
 
4a095b8
 
3070fa6
4a095b8
3070fa6
4a095b8
af5a7d4
 
4a095b8
af5a7d4
4a095b8
 
 
 
3070fa6
4a095b8
 
 
 
af5a7d4
 
 
 
 
3070fa6
 
 
 
af5a7d4
3070fa6
4a095b8
 
 
3070fa6
 
af5a7d4
 
3070fa6
4a095b8
3070fa6
4a095b8
af5a7d4
 
3070fa6
 
 
af5a7d4
 
 
 
3070fa6
24b6a35
af5a7d4
3070fa6
4a095b8
 
af5a7d4
4a095b8
 
af5a7d4
4a095b8
af5a7d4
 
3070fa6
 
4a095b8
af5a7d4
 
4a095b8
af5a7d4
4a095b8
af5a7d4
4a095b8
 
3070fa6
4a095b8
af5a7d4
4a095b8
af5a7d4
4a095b8
af5a7d4
 
 
 
 
4a095b8
 
3070fa6
4a095b8
 
3070fa6
 
af5a7d4
 
 
3070fa6
 
af5a7d4
 
 
 
4a095b8
3070fa6
af5a7d4
 
3070fa6
a5c06df
4a095b8
af5a7d4
4a095b8
 
3070fa6
 
 
 
af5a7d4
 
 
4a095b8
 
5e94278
3070fa6
a5c06df
3070fa6
 
 
 
 
 
 
 
 
 
 
 
 
 
dae4257
3070fa6
 
 
dae4257
3070fa6
a5c06df
 
dae4257
 
3070fa6
 
 
 
 
dae4257
 
 
de6f556
af5a7d4
f94afff
3fb3f80
516ec70
3fb3f80
 
 
 
 
 
 
516ec70
af5a7d4
3fb3f80
 
 
 
 
f94afff
dae4257
 
3fb3f80
 
 
 
 
 
a5c06df
3fb3f80
 
a5c06df
3fb3f80
cb8c269
3fb3f80
a5c06df
3fb3f80
 
 
 
c2fd3c1
3fb3f80
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
c2fd3c1
 
3fb3f80
 
 
 
a5c06df
3fb3f80
af5a7d4
3fb3f80
7fa8834
 
3fb3f80
 
 
 
 
4a095b8
3fb3f80
3070fa6
3fb3f80
 
 
 
 
 
 
 
 
 
 
 
 
7df4375
af5a7d4
 
4a095b8
3070fa6
 
 
4a095b8
 
2feab77
af5a7d4
1f20379
af5a7d4
4a095b8
f94afff
1f20379
af5a7d4
1ce6b30
f94afff
1f20379
f94afff
3070fa6
f94afff
 
3070fa6
 
f94afff
3070fa6
 
f94afff
 
1f20379
af5a7d4
f94afff
 
 
024f51a
 
7fa8834
024f51a
 
 
 
 
 
f94afff
024f51a
3070fa6
f94afff
 
 
 
024f51a
 
 
 
af5a7d4
 
c2fd3c1
4a095b8
 
3070fa6
4a095b8
af5a7d4
4a095b8
3070fa6
4a095b8
3070fa6
4a095b8
3070fa6
4a095b8
 
 
af5a7d4
4a095b8
 
 
3070fa6
4a095b8
 
 
3070fa6
4a095b8
3070fa6
4a095b8
 
 
 
 
 
 
 
3070fa6
4a095b8
 
 
 
 
 
 
 
af5a7d4
 
 
3070fa6
af5a7d4
 
 
 
 
3070fa6
5e94278
af5a7d4
 
3070fa6
 
 
af5a7d4
4a095b8
 
af5a7d4
 
4a095b8
 
5e94278
4a095b8
3070fa6
4a095b8
 
af5a7d4
4a095b8
 
3070fa6
 
4a095b8
 
 
3070fa6
4a095b8
af5a7d4
 
3070fa6
af5a7d4
3070fa6
4a095b8
 
af5a7d4
 
3070fa6
 
4a095b8
 
 
 
af5a7d4
4a095b8
 
 
3070fa6
4a095b8
 
 
 
 
 
 
 
3070fa6
4a095b8
af5a7d4
1f20379
 
 
 
 
 
 
c2fd3c1
1f20379
 
 
 
 
 
 
 
 
 
 
aa5d2a9
1f20379
 
 
 
63e2285
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
af5a7d4
584cf22
 
5e94278
 
af5a7d4
4a095b8
 
e6cdb3e
 
4a095b8
 
e6cdb3e
4a095b8
 
e6cdb3e
 
 
af5a7d4
4a095b8
1f20379
4a095b8
af5a7d4
 
3070fa6
1f20379
3070fa6
 
4a095b8
af5a7d4
1f20379
3070fa6
af5a7d4
 
 
4a095b8
1f20379
af5a7d4
 
 
 
 
 
1f20379
4a095b8
1f20379
e6cdb3e
 
f94afff
e6cdb3e
 
 
 
 
1f20379
e6cdb3e
 
 
 
 
 
f94afff
4a095b8
1f20379
e6cdb3e
4a095b8
e6cdb3e
4a095b8
 
c2fd3c1
e6cdb3e
3070fa6
 
4a095b8
af5a7d4
e6cdb3e
f94afff
e6cdb3e
af5a7d4
e6cdb3e
1f20379
e6cdb3e
 
c2fd3c1
e6cdb3e
f94afff
c2fd3c1
e6cdb3e
 
f94afff
e6cdb3e
 
 
 
 
 
 
f94afff
4a095b8
e6cdb3e
 
f94afff
4a095b8
e6cdb3e
 
 
f94afff
4a095b8
e6cdb3e
f94afff
4a095b8
1f20379
af5a7d4
4a095b8
1f20379
 
 
 
 
 
 
 
af5a7d4
e6cdb3e
1f20379
4a095b8
af5a7d4
 
 
 
 
 
 
 
63e2285
 
 
af5a7d4
4a095b8
1f20379
af5a7d4
 
 
f94afff
4a095b8
 
 
 
e6cdb3e
 
1f20379
f94afff
c2fd3c1
e6cdb3e
f94afff
e6cdb3e
4a095b8
1f20379
af5a7d4
4a095b8
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
f774800
4a095b8
c2fd3c1
0b58ed4
 
 
e6cdb3e
 
 
0b58ed4
 
de6f556
f94afff
4a095b8
 
0b58ed4
4a095b8
0b58ed4
af5a7d4
4a095b8
af5a7d4
 
 
4a095b8
 
 
e6cdb3e
f774800
e6cdb3e
af5a7d4
 
 
 
f774800
af5a7d4
 
 
 
f94afff
e6cdb3e
d8f485f
2861f8b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
af5a7d4
e6cdb3e
3070fa6
e6cdb3e
 
 
 
 
 
 
 
 
 
 
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
import streamlit as st
import os, json, datetime, hashlib
from langchain_community.vectorstores import FAISS
from langchain_community.embeddings import HuggingFaceEmbeddings
from langchain_groq import ChatGroq
from langchain.chains import LLMChain
from langchain.prompts import PromptTemplate
from gtts import gTTS
from pathlib import Path
from dotenv import load_dotenv
from sentence_transformers import SentenceTransformer, util
import altair as alt
import speech_recognition as sr
from transformers import pipeline
import torch
import pickle
import re
import pandas as pd 

# Load environment variables
load_dotenv()
GROQ_API_KEY = os.getenv("GROQ_API_KEY")

CRISIS_KEYWORDS = ["suicide", "kill myself", "end it all", "worthless", "can't go on", "hurt myself", "self harm", "want to disappear", "no reason to live"]

# Initialize session state
if "authenticated" not in st.session_state:
    st.session_state.authenticated = False
if "username" not in st.session_state:
    st.session_state.username = None
if "is_admin" not in st.session_state:
    st.session_state.is_admin = False
if "transcribed_text" not in st.session_state:
    st.session_state.transcribed_text = ""

# Admin configuration
ADMIN_USERNAME = os.getenv("ADMIN_USERNAME")  # Set in HF Spaces secrets
ADMIN_PASSWORD = os.getenv("ADMIN_PASSWORD")  # Set in HF Spaces secrets

# --- UI/UX Modifications (Applied globally) ---
def set_background_and_styles():
    st.markdown(
        """
        <style>
        @import url('https://fonts.googleapis.com/css2?family=Montserrat:wght@300;400;600;700&display=swap');
        @import url('https://fonts.googleapis.com/css2?family=Merriweather:wght@300;400;700&display=swap');
        .stApp {
            background: linear-gradient(135deg, #e0e7ff 0%, #c6e2ff 50%, #b0c4de 100%); /* Light blueish gradient */
            background-size: cover;
            background-position: center;
            background-repeat: no-repeat;
            background-attachment: fixed;
            font-family: 'Montserrat', sans-serif;
            color: #333; /* Default text color to dark for light background */
        }
        
        h1, h2, h3, h4, h5, h6, .stMarkdown, label {
            font-family: 'Merriweather', serif;
            color: #1a237e; /* Darker blue for headings */
        }
        
        /* Ensure text area label is dark */
        .stTextArea > label {
            color: #333 !important;
        }
        .stButton>button {
            background-image: linear-gradient(to right, #6a11cb 0%, #2575fc 100%); /* Blue-purple gradient */
            color: white;
            border-radius: 8px;
            border: none;
            padding: 10px 20px;
            font-size: 16px;
            font-weight: bold;
            transition: all 0.2s ease-in-out;
            box-shadow: 0 4px 6px rgba(0, 0, 0, 0.1);
        }
        .stButton>button:hover {
            transform: translateY(-2px);
            box-shadow: 0 6px 12px rgba(0, 0, 0, 0.2);
        }
        
        /* Secondary button style for "Explore Opportunities" */
        .secondary-button > button {
            background-color: white;
            color: #6a11cb;
            border: 1px solid #6a11cb;
            box-shadow: none;
        }
        .secondary-button > button:hover {
            background-color: #f0f4f8; /* Light hover */
            color: #2575fc;
            box-shadow: 0 2px 4px rgba(0, 0, 0, 0.1);
        }
        .stTextInput>div>div>input, .stTextArea>div>div>textarea, .stSelectbox>div>div>div {
            border-radius: 8px;
            border: 1px solid #b3e5fc; /* Light blue border */
            padding: 10px;
            background-color: rgba(255, 255, 255, 0.95); /* Nearly opaque white */
            color: #333; /* Input text color dark */
        }
        /* Adjusted stSuccess and stInfo for white text on darker transparent black background */
        .stSuccess { 
            border-left: 5px solid #28a745; 
            background-color: rgba(0, 0, 0, 0.6); /* Transparent black */
            color: white; /* Make text white */
            border-radius: 8px;
            padding: 10px;
            margin-bottom: 10px;
        }
        .stInfo { 
            border-left: 5px solid #17a2b8; 
            background-color: rgba(0, 0, 0, 0.6); /* Transparent black */
            color: white; /* Make text white */
            border-radius: 8px;
            padding: 10px;
            margin-bottom: 10px;
        }
        /* Keeping warning/error as original light background for now, as black transparent might not suit warnings well */
        .stWarning { 
            border-left: 5px solid #ffc107; 
            background-color: rgba(255, 255, 255, 0.9); 
            color: #333; 
            border-radius: 8px;
            padding: 10px;
            margin-bottom: 10px;
        }
        .stError { 
            border-left: 5px solid #dc3545; 
            background-color: rgba(255, 255, 255, 0.9); 
            color: #333; 
            border-radius: 8px;
            padding: 10px;
            margin-bottom: 10px;
        }
        /* Custom container for content with blur background - used for auth form */
        .auth-content-container {
            background-color: rgba(255, 255, 255, 0.8); /* Slightly transparent white */
            backdrop-filter: blur(8px); /* Blur effect */
            border-radius: 15px;
            padding: 30px;
            margin: 20px auto;
            max-width: 450px; /* Slimmer for auth forms */
            box-shadow: 0 10px 25px rgba(0, 0, 0, 0.1);
            text-align: center; /* Center content within */
        }
        
        .feature-grid {
            display: grid;
            grid-template-columns: repeat(auto-fit, minmax(200px, 1fr)); /* Adjusted for more flexibility */
            gap: 20px;
            margin-top: 30px;
            margin-bottom: 30px;
            max-width: 900px; /* Max width for the grid itself */
            margin-left: auto;
            margin-right: auto;
        }
        .feature-item {
            background-color: rgba(255, 255, 255, 0.9);
            border-radius: 12px;
            padding: 20px;
            text-align: center;
            box-shadow: 0 4px 10px rgba(0, 0, 0, 0.08);
            transition: transform 0.2s ease-in-out;
            border: 1px solid #e0e0e0;
        }
        .feature-item:hover {
            transform: translateY(-5px);
            box-shadow: 0 8px 15px rgba(0, 0, 0, 0.12);
        }
        .feature-item .icon-img {
            height: 60px; /* Adjust icon size as needed */
            margin-bottom: 10px;
            display: block; /* Ensure icon is on its own line */
            margin-left: auto;
            margin-right: auto;
        }
        .feature-item h3 {
            font-size: 1.2em;
            color: #1a237e;
            margin-bottom: 8px;
        }
        .feature-item p {
            font-size: 0.9em;
            color: #555;
            line-height: 1.5;
        }
        /* Main app content container */
        .main-app-content-container {
            background-color: rgba(255, 255, 255, 0.8); /* Slightly transparent white */
            backdrop-filter: blur(8px); /* Blur effect */
            border-radius: 15px;
            padding: 30px;
            margin: 20px auto;
            max-width: 800px; /* Wider for main app content */
            box-shadow: 0 10px 25px rgba(0, 0, 0, 0.1);
            color: #333; /* Dark text inside */
        }
        /* Ensure headers within main app container are dark */
        .main-app-content-container h1, .main-app-content-container h2, 
        .main-app-content-container h3, .main-app-content-container h4, 
        .main-app-content-container h5, .main-app-content-container h6,
        .main-app-content-container .stMarkdown, .main-app-content-container label {
            color: #1a237e; /* Darker blue for headers in main app */
        }
        /* Specifically target text within main-app-content-container to be dark */
        .main-app-content-container p, .main-app-content-container li, .main-app-content-container div {
            color: #333;
        }
        /* Override specific Streamlit elements that don't pick up general styles for main app */
        .main-app-content-container .st-emotion-cache-1jmve6n, /* st.subheader */
        .main-app-content-container .st-emotion-cache-1gcs47q, /* st.text or similar */
        .main-app-content-container .st-emotion-cache-10q7f27, /* st.info text */
        .main-app-content-container .st-emotion-cache-1j0qsvo { /* more specific text */
             color: #333 !important;
        }
        /* Navbar Styling */
        .navbar {
            display: flex;
            justify-content: center; /* Center the logo and title */
            align-items: center;
            padding: 15px 40px;
            background-color: rgba(255, 255, 255, 0.8); /* Slightly transparent white */
            backdrop-filter: blur(5px);
            border-bottom: 1px solid rgba(0, 0, 0, 0.05);
            box-shadow: 0 2px 10px rgba(0, 0, 0, 0.05);
            border-radius: 10px;
            margin-bottom: 20px;
            /* position: relative; Removed absolute positioning as logout button is gone */
        }
        .navbar-logo-container {
            display: flex;
            align-items: center;
            justify-content: center; /* Center the logo and text */
            flex-grow: 1; /* Allows it to take available space for centering */
        }
        .navbar-logo-text {
            font-family: 'Merriweather', serif;
            font-size: 24px;
            font-weight: bold;
            color: #1a237e;
            margin-left: 10px; /* Space between logo image and text */
        }
        .navbar-logo-img {
            height: 30px; /* Size of your logo image */
            vertical-align: middle;
        }
        .navbar-links { /* This block might not be needed if links are removed */
            display: flex;
            gap: 30px;
        }
        .navbar-link { /* This block might not be needed if links are removed */
            font-family: 'Montserrat', sans-serif;
            font-size: 16px;
            color: #3f51b5; /* Medium blue */
            text-decoration: none;
            padding: 5px 10px;
            transition: color 0.2s ease-in-out;
            cursor: pointer;
        }
        .navbar-link:hover { /* This block might not be needed if links are removed */
            color: #6a11cb; /* Purple hover */
        }
        .navbar-button { /* This block might not be needed if links are removed */
            background-image: linear-gradient(to right, #6a11cb 0%, #2575fc 100%);
            color: white;
            border-radius: 8px;
            padding: 8px 15px;
            font-size: 14px;
            font-weight: bold;
            text-decoration: none;
            transition: all 0.2s ease-in-out;
            box-shadow: 0 2px 4px rgba(0, 0, 0, 0.1);
            cursor: pointer;
        }
        .navbar-button:hover { /* This block might not be needed if links are removed */
            transform: translateY(-1px);
            box_shadow: 0 4px 8px rgba(0, 0, 0, 0.15);
        }
        /* AI Powered Educational Platform banner style */
        .ai-powered-banner {
            display: inline-flex; /* Use inline-flex to shrink-wrap content */
            align-items: center;
            background-color: rgba(255, 255, 255, 0.7); /* Light, semi-transparent */
            border-radius: 20px;
            padding: 8px 15px;
            margin-bottom: 20px;
            font-size: 14px;
            font-weight: 600;
            color: #3f51b5; /* Blue text */
            box-shadow: 0 2px 5px rgba(0,0,0,0.05);
            text-align: center; /* Ensure text is centered if container grows */
            margin-left: auto; /* Center horizontally */
            margin-right: auto; /* Center horizontally */
        }
        .centered-container {
            display: flex;
            justify-content: center;
            width: 100%;
        }
        /* Metrics section styles (50K+ Students Helped, etc.) */
        .metrics-container {
            display: flex;
            justify-content: space-around;
            flex-wrap: wrap;
            margin-top: 40px;
            gap: 20px;
            /* border: 1px solid red; REMOVED for debugging */ 
        }
        .metric-item {
            text-align: center;
            background-color: rgba(255, 255, 255, 0.7);
            padding: 20px;
            border-radius: 10px;
            box-shadow: 0 4px 10px rgba(0,0,0,0.05);
            flex: 1; /* Allow items to grow/shrink */
            min-width: 180px; /* Minimum width before wrapping */
        }
        .metric-item h2 {
            font-size: 32px;
            color: #6a11cb; /* Purple */
            margin-bottom: 5px;
        }
        .metric-item p {
            font-size: 16px;
            color: #555;
        }
        .metric-item img { /* Style for icon images if used */
            height: 40px;
            margin-bottom: 10px;
        }
        /* Center plots */
        .st-emotion-cache-1pxazr6 { /* Specific Streamlit container for pyplot */
            display: flex;
            justify-content: center;
        }
        /* Override specific Streamlit elements that don't pick up general styles */
        .st-emotion-cache-1jmve6n { /* This class is often for st.subheader */
            color: #1a237e !important; /* Darker blue */
        }
        .st-emotion-cache-1gcs47q { /* This class can be for specific text elements */
             color: #333 !important;
        }
        .st-emotion-cache-10q7f27 { /* Example for st.info text */
             color: #333 !important;
        }
        /* Hide the top grey bar */
        header.st-emotion-cache-1gh8zsi {
            display: none !important;
        }
        div.st-emotion-cache-fis6y8 { 
            padding-top: 0 !important;
        }
        div.st-emotion-cache-z5inrg { 
            display: none !important;
        }
        /* Adjust overall container width */
        .st-emotion-cache-fg4lbf { 
            max-width: 1000px !important; /* Adjusted for wider layout */
            padding-left: 0 !important; 
            padding-right: 0 !important;
        }
        .block-container { 
            padding-left: 1rem;
            padding-right: 1rem;
            color: #333; /* Default text color within block-container */
        }
        /* Specific style for button alignment */
        /* Updated .button-row to stack and add spacing */
        .button-row {
            display: flex;
            flex-direction: column; /* Stack buttons vertically */
            align-items: center; /* Center buttons horizontally */
            gap: 20px; /* Space between buttons */
            margin-top: 20px;
            width: 100%; /* Ensure row takes full width to center its content */
        }
        .button-row > div { /* Target Streamlit column divs within button-row if still used */
            width: 100%; /* Make columns take full width to center buttons */
            display: flex;
            justify-content: center; /* Center the buttons inside their columns */
        }
        /* Custom footer style */
        .app-footer {
            position: fixed;
            left: 0;
            bottom: 0;
            width: 100%;
            background-color: #f0f2f6; /* Streamlit's default background color */
            color: #31333F; /* Streamlit's default text color */
            text-align: center;
            padding: 10px;
            font-size: 14px;
            border-top: 1px solid #d3d3d3;
            z-index: 1000; /* Ensure footer is on top */
        }
        /* Adjust app padding to prevent content from being hidden by footer */
        .stApp {
            padding-bottom: 50px; /* Approx height of footer */
        }
        /* History entry styling */
        .history-entry {
            background-color: rgba(255, 255, 255, 0.9);
            border-radius: 10px;
            padding: 15px;
            margin-bottom: 10px;
            box-shadow: 0 2px 8px rgba(0,0,0,0.1);
            border: 1px solid #e0e0e0;
        }
        .history-entry h4 {
            color: #2575fc; /* Blue for history entry title */
            margin-top: 0;
            margin-bottom: 5px;
        }
        .history-entry p {
            color: #444;
            font-size: 0.95em;
        }
        .history-entry strong {
            color: #1a237e;
        }
        /* New style for output text color from DilBot */
        .dilbot-response-text {
            color: black !important; /* Force black text */
        }
        </style>
        """,
        unsafe_allow_html=True
    )

# User management functions
def hash_password(password):
    """Hash password using SHA-256 with salt"""
    salt = "dilbot_secure_salt_2024"  # You can change this
    return hashlib.sha256((password + salt).encode()).hexdigest()

def get_secure_users_path():
    """Get path to users file in a hidden directory"""
    secure_dir = ".secure_data"
    os.makedirs(secure_dir, exist_ok=True)
    return os.path.join(secure_dir, "users_encrypted.json")

def load_users():
    """Load users from secure file"""
    users_path = get_secure_users_path()
    if os.path.exists(users_path):
        try:
            with open(users_path, "r") as f:
                return json.load(f)
        except:
            return {}
    return {}

def save_users(users):
    """Save users to secure file"""
    users_path = get_secure_users_path()
    with open(users_path, "w") as f:
        json.dump(users, f, indent=4)

def create_user_directory(username):
    """Create user-specific directory structure"""
    user_dir = f"users/{username}"
    os.makedirs(user_dir, exist_ok=True)
    return user_dir

def get_user_file_path(username, filename):
    """Get path to user-specific file"""
    user_dir = f"users/{username}"
    return os.path.join(user_dir, filename)

def signup(username, password, email):
    """Register new user"""
    users = load_users()
    if username in users:
        return False, "Username already exists"
    
    email_pattern = r"^[\w\.-]+@[\w\.-]+\.\w+$"
    if not re.match(email_pattern, email):
         return False, "Invalid email format"
        
    users[username] = {
        "password": hash_password(password),
        "email": email,
        "created_at": str(datetime.datetime.now())
    }
    save_users(users)
    create_user_directory(username)
    return True, "Account created successfully!"

def login(username, password):
    """Authenticate user or admin"""
    # Check if admin login
    if username == ADMIN_USERNAME and password == ADMIN_PASSWORD:
        return True, "Admin login successful!", True
    
    # Regular user login
    users = load_users()
    if username not in users:
        return False, "User not found.Please signup.", False
    
    if users[username]["password"] == hash_password(password):
        return True, "Login successful!", False
    return False, "Incorrect password", False

# Emotion detection
@st.cache_resource
def load_emotion_model():
    return pipeline(
        "text-classification",
        model="j-hartmann/emotion-english-distilroberta-base",
        top_k=1,
        device=-1
    )

def detect_emotion(text):
    emotion_pipeline = load_emotion_model()
    prediction = emotion_pipeline(text)[0][0]
    return prediction['label'].lower(), prediction['score']


# Authentication UI
def show_auth_page():
    set_background_and_styles()
    st.markdown(
        """
        <div class="navbar">
            <div class="navbar-logo-container">
                <img src="https://e7.pngegg.com/pngimages/498/917/png-clipart-computer-icons-desktop-chatbot-icon-blue-angle-thumbnail.png" class="navbar-logo-img">
                <div class="navbar-logo-text">DilBot</div>
            </div>
        </div>
        """, unsafe_allow_html=True
    )

    st.markdown("<div class='centered-container'><div class='ai-powered-banner'><span>✨</span>AI-Powered Emotional Companion</div></div>", unsafe_allow_html=True)

    st.markdown(
        """
        <h1 style='text-align: center; font-size: 3.5em; color: #1a237e;'>Transform Your Emotional Journey</h1>
        <p style='text-align: center; font-size: 1.2em; color: #555;'>
            DilBot empowers individuals with AI-driven insights for emotional well-being, personal growth, and self-discovery worldwide.
        </p>
        """, unsafe_allow_html=True
    )

    st.markdown('<div class="button-row">', unsafe_allow_html=True)
    
    # Buttons placed directly in a single column layout
    if st.button("Paid Plan", key="paid_plan_btn", use_container_width=True):
        st.info("Paid version coming soon") # Text for paid plan button
    st.markdown('<div class="secondary-button" style="margin-top: 20px;">', unsafe_allow_html=True) # Added margin for spacing
    if st.button("Login Below FOR FREE", key="login_free_btn", use_container_width=True):
        st.info("Slide down below") # Text for free login button
    st.markdown('</div>', unsafe_allow_html=True)
    st.markdown('</div>', unsafe_allow_html=True) # Close button-row


    # --- Section for icons and descriptions ---
    st.markdown("<div class='feature-grid'>", unsafe_allow_html=True)
    
    # First Image: Users vast amount satisfied (https://cdn-icons-png.flaticon.com/512/33/33308.png)
    st.markdown(f"""
        <div class="feature-item">
            <img src="https://cdn-icons-png.flaticon.com/512/33/33308.png" class="icon-img">
            <h3>Users Vastly Satisfied</h3>
            <p>Our community experiences significant positive emotional shifts.</p>
        </div>
    """, unsafe_allow_html=True)

    # Second Image: Successful User's Emotion's results a happy lifestyle (https://cdn-icons-png.flaticon.com/512/10809/10809501.png)
    st.markdown(f"""
        <div class="feature-item">
            <img src="https://cdn-icons-png.flaticon.com/512/10809/10809501.png" class="icon-img">
            <h3>Happy Lifestyle</h3>
            <p>Empowering users to achieve emotional well-being and a happier life.</p>
        </div>
    """, unsafe_allow_html=True)

    # Third Image: 10,000+ Daily Users (https://www.clipartmax.com/png/middle/225-2254363_checklist-comments-form-approved-icon.png)
    st.markdown(f"""
        <div class="feature-item">
            <img src="https://www.clipartmax.com/png/middle/225-2254363_checklist-comments-form-appr.png" class="icon-img">
            <h3>10,000+ Daily Users</h3>
            <p>Join our growing community finding support and growth daily.</p>
        </div>
    """, unsafe_allow_html=True)

    # Re-adding the original Empathetic Chatbot feature from previous response for completeness
    st.markdown("""
        <div class="feature-item">
            <span class="icon-img"></span> <h3>Empathetic Chatbot</h3>
            <p>Connect with an AI that listens and understands your feelings.</p>
        </div>
    """, unsafe_allow_html=True)

    st.markdown("</div>", unsafe_allow_html=True)
    # --- End of icon and description section ---


    st.markdown("<div class='auth-content-container'>", unsafe_allow_html=True) # Container for login/signup

    tab1, tab2 = st.tabs(["Login", "Sign Up"])
    
    with tab1:
        st.subheader("Login to Your Account( Admin will use own creditionals )")
        login_username = st.text_input("Username", key="login_user")
        login_password = st.text_input("Password", type="password", key="login_pass")
        
        if st.button("Login", key="login_btn"):
            if login_username and login_password:
                success, message, is_admin = login(login_username, login_password)
                if success:
                    st.session_state.authenticated = True
                    st.session_state.username = login_username
                    st.session_state.is_admin = is_admin
                    st.session_state.page = "main_app" if not is_admin else "admin_dashboard" # Direct to correct page
                    st.rerun()
                else:
                    st.error(message)
            else:
                st.warning("Please fill in all fields")
    
    with tab2:
        st.subheader("Create New Account")
        signup_username = st.text_input("Choose Username", key="signup_user")
        signup_email = st.text_input("Email Address", key="signup_email")
        signup_password = st.text_input("Choose Password", type="password", key="signup_pass")
        signup_confirm = st.text_input("Confirm Password", type="password", key="signup_confirm")
        
        if st.button("Create Account", key="signup_btn"):
            if all([signup_username, signup_email, signup_password, signup_confirm]):
                if signup_password != signup_confirm:
                    st.error("Passwords don't match!")
                elif len(signup_password) < 6:
                    st.error("Password must be at least 6 characters long!")
                else:
                    success, message = signup(signup_username, signup_password, signup_email)
                    if success:
                        st.success(message)
                        st.info("You can now login with your credentials!")
                    else:
                        st.error(message)
            else:
                st.warning("Please fill in all fields")

    st.markdown("</div>", unsafe_allow_html=True)
    
# Main app functions
def build_user_vectorstore(username, quotes):
    """Build and save user-specific vectorstore"""
    embeddings = HuggingFaceEmbeddings(model_name="sentence-transformers/all-MiniLM-L6-v2")
    vectorstore = FAISS.from_texts(quotes, embedding=embeddings)
    
    # Save vectorstore for user
    vectorstore_path = get_user_file_path(username, "vectorstore")
    vectorstore.save_local(vectorstore_path)
    return vectorstore

def load_user_vectorstore(username):
    """Load user-specific vectorstore"""
    vectorstore_path = get_user_file_path(username, "vectorstore")
    if os.path.exists(vectorstore_path):
        embeddings = HuggingFaceEmbeddings(model_name="sentence-transformers/all-MiniLM-L6-v2")
        return FAISS.load_local(vectorstore_path, embeddings, allow_dangerous_deserialization=True)
    return None

def save_user_journal(username, user_input, emotion, score, response):
    """Save journal entry for specific user"""
    journal_path = get_user_file_path(username, "journal.json")
    entry = {
        "date": str(datetime.date.today()),
        "timestamp": str(datetime.datetime.now()),
        "user_input": user_input,
        "emotion": emotion,
        "confidence": round(score * 100, 2),
        "response": response
    }
    
    journal = []
    if os.path.exists(journal_path):
        with open(journal_path, "r") as f:
            journal = json.load(f)
    
    journal.append(entry)
    with open(journal_path, "w") as f:
        json.dump(journal, f, indent=4)

def load_user_journal(username):
    """Load journal for specific user"""
    journal_path = get_user_file_path(username, "journal.json")
    if os.path.exists(journal_path):
        with open(journal_path, "r") as f:
            return json.load(f)
    return []

def get_admin_stats():
    """Get comprehensive admin statistics"""
    users = load_users()
    stats = {
        "total_users": len(users),
        "users_today": 0,
        "users_this_week": 0,
        "total_conversations": 0,
        "active_users": 0,
        "user_details": []
    }
    
    today = datetime.date.today()
    week_ago = today - datetime.timedelta(days=7)
    
    for username, user_data in users.items():
        created_date = datetime.datetime.fromisoformat(user_data["created_at"]).date()
        
        # Count registrations
        if created_date == today:
            stats["users_today"] += 1
        if created_date >= week_ago:
            stats["users_this_week"] += 1
        
        # Get user journal stats
        journal_data = load_user_journal(username)
        conversation_count = len(journal_data)
        stats["total_conversations"] += conversation_count
        
        if conversation_count > 0:
            stats["active_users"] += 1
            last_activity = journal_data[-1]["date"] if journal_data else "Never"
        else:
            last_activity = "Never"
        
        # Get emotion breakdown
        emotions = [entry['emotion'] for entry in journal_data]
        emotion_counts = {}
        for emotion in emotions:
            emotion_counts[emotion] = emotion_counts.get(emotion, 0) + 1
        
        most_common_emotion = max(emotion_counts, key=emotion_counts.get) if emotion_counts else "None"
        
        stats["user_details"].append({
            "username": username,
            "email": user_data["email"],
            "joined": created_date.strftime("%Y-%m-%d"),
            "conversations": conversation_count,
            "last_activity": last_activity,
            "most_common_emotion": most_common_emotion.capitalize(),
            "emotions_breakdown": emotion_counts
        })
    
    return stats

def log_admin_activity(action, details=""):
    """Log admin activities"""
    admin_log_path = "data/admin_log.json"  # Persistent and visible
    log_entry = {
        "timestamp": str(datetime.datetime.now()),
        "action": action,
        "details": details,
        "admin": st.session_state.username
    }
    
    admin_log = []
    if os.path.exists(admin_log_path):
        with open(admin_log_path, "r") as f:
            admin_log = json.load(f)
    
    admin_log.append(log_entry)
    
    # Keep only last 100 entries
    admin_log = admin_log[-100:]
    
    os.makedirs("data", exist_ok=True)
    with open(admin_log_path, "w") as f:
        json.dump(admin_log, f, indent=4)

def get_admin_logs():
    """Get admin activity logs"""
    admin_log_path = "data/admin_log.json"
    if os.path.exists(admin_log_path):
        with open(admin_log_path, "r") as f:
            return json.load(f)
    return []

def is_crisis(text):
    """Check for crisis keywords"""
    return any(phrase in text.lower() for phrase in CRISIS_KEYWORDS)

def show_admin_dashboard():
    """Admin dashboard for monitoring users and app usage"""
    st.set_page_config(page_title="DilBot Admin Dashboard", page_icon="👑", layout="wide")

    # --- ENHANCED CUSTOM CSS FOR ADMIN DASHBOARD (Consistent with main app) ---
    st.markdown("""
    <style>
    /* Global Styles & Background (Consistent with main app) */
    .stApp {
        background: linear-gradient(to bottom right, #f8f9fa, #e9ecef);
        color: #343a40;
        font-family: 'Segoe UI', Roboto, 'Helvetica Neue', Arial, sans-serif;
        -webkit-font-smoothing: antialiased;
        padding-top: 2rem;
    }

    /* Streamlit's main block container for content centering and width */
    .main .block-container {
        max-width: 1200px;
        padding-top: 2rem;
        padding-bottom: 2rem;
        padding-left: 1rem;
        padding-right: 1rem;
    }

    /* Hide Streamlit's header/footer if you want a fully custom layout */
    .stApp > header {
        display: none;
    }
    .stApp > footer {
        display: none;
    }

    /* Header & Titles (Consistent with main app) */
    h1, h2, h3, h4, h5, h6 {
        color: #212529;
        margin-top: 1.8rem;
        margin-bottom: 0.9rem;
        font-weight: 700;
    }
    h1 {
        font-size: 2.8em;
        font-weight: 800;
        color: #5d6dbe;
        letter-spacing: -0.8px;
        text-shadow: 1px 1px 2px rgba(0,0,0,0.05);
    }
    .header-tagline { /* Added for general sub-titles */
        font-size: 1.2em;
        color: #6c757d;
        margin-top: -0.5rem;
        margin-bottom: 2.5rem;
        font-weight: 400;
    }
    h2 {
        font-size: 2em;
        font-weight: 700;
        border-bottom: 2px solid #e9ecef;
        padding-bottom: 0.7rem;
        margin-bottom: 2rem;
        color: #343a40;
    }
    h3 {
        font-size: 1.6em;
        font-weight: 600;
        color: #495057;
        margin-top: 2rem;
        margin-bottom: 1.2rem;
    }
    h4 { /* Added for sub-sections */
        font-size: 1.3em;
        font-weight: 600;
        color: #343a40;
        margin-top: 1.5rem;
        margin-bottom: 1rem;
    }
    h5 {
        font-size: 1.1em;
        font-weight: 600;
        color: #495057;
        margin-bottom: 1rem;
    }

    /* Metrics (Consistent with main app) */
    [data-testid="stMetric"] {
        background-color: #ffffff;
        border: 1px solid #dee2e6;
        border-radius: 12px;
        padding: 20px;
        box-shadow: 0 8px 20px rgba(0,0,0,0.08);
        text-align: center;
        margin-bottom: 1.5rem;
        transition: transform 0.2s ease-in-out, box-shadow 0.2s ease-in-out;
    }
    [data-testid="stMetric"]:hover {
        transform: translateY(-5px);
        box-shadow: 0 12px 30px rgba(0,0,0,0.12);
    }
    [data-testid="stMetricLabel"] {
        font-size: 1em;
        color: #6c757d;
        margin-bottom: 8px;
        font-weight: 500;
    }
    [data-testid="stMetricValue"] { /* Overriding the admin-specific green with main app blue */
        font-size: 2.8em;
        font-weight: 800;
        color: #5d6dbe; /* Main app's primary blue */
    }

    /* Buttons (Consistent with main app) */
    .stButton>button {
        background-color: #5d6dbe;
        color: white;
        border: none;
        border-radius: 10px;
        padding: 12px 25px;
        font-size: 1.1em;
        font-weight: bold;
        transition: background-color 0.3s ease, transform 0.2s ease, box-shadow 0.3s ease;
        box-shadow: 0 5px 15px rgba(0,0,0,0.1);
        cursor: pointer;
        letter-spacing: 0.5px;
    }
    .stButton>button:hover {
        background-color: #4a5c9d;
        transform: translateY(-3px);
        box-shadow: 0 8px 20px rgba(0,0,0,0.15);
    }
    .stButton>button:active {
        background-color: #3b4b80;
        transform: translateY(0);
        box-shadow: 0 3px 10px rgba(0,0,0,0.1);
    }
    /* Logout button specific style */
    [key="admin_logout"] > button {
        background-color: #dc3545;
        box-shadow: 0 5px 15px rgba(220,53,69,0.2);
    }
    [key="admin_logout"] > button:hover {
        background-color: #c82333;
        box-shadow: 0 8px 20px rgba(220,53,69,0.3);
    }
    /* "Back to Main App" button style */
    [key="admin_back_btn"] > button {
        background-color: #6c757d; /* Grey for back button */
        box-shadow: 0 5px 15px rgba(108,117,125,0.2);
    }
    [key="admin_back_btn"] > button:hover {
        background-color: #5a6268;
        box-shadow: 0 8px 20px rgba(108,117,125,0.3);
    }
    /* Reset Data buttons for users */
    .stButton > button[kind="secondary"] { /* Target buttons that are not primary, e.g., Reset/Confirm */
        background-color: #ffc107; /* Warning yellow */
        color: #343a40; /* Dark text on yellow */
        border: none;
    }
    .stButton > button[kind="secondary"]:hover {
        background-color: #e0a800; /* Darker yellow on hover */
        transform: translateY(-2px);
    }
    .stButton > button[key*="confirm_reset_"] {
        background-color: #dc3545; /* Danger red for confirm reset */
        color: white;
    }
    .stButton > button[key*="confirm_reset_"]:hover {
        background-color: #c82333;
    }
    .stButton > button[key*="cancel_reset_"] {
        background-color: #6c757d; /* Grey for cancel */
        color: white;
    }
    .stButton > button[key*="cancel_reset_"]:hover {
        background-color: #5a6268;
    }


    /* Text Inputs and Text Areas (Consistent with main app) */
    .stTextInput>div>div>input, .stTextArea>div>div>textarea, .stNumberInput>div>div>input {
        border-radius: 10px;
        border: 1px solid #ced4da;
        padding: 14px 18px;
        font-size: 1.05em;
        color: #343a40;
        background-color: #000000;
        box-shadow: inset 0 2px 5px rgba(0,0,0,0.03);
        transition: border-color 0.3s ease, box-shadow 0.3s ease;
    }
    .stTextInput>div>div>input:focus, .stTextArea>div>div>textarea:focus, .stNumberInput>div>div>input:focus {
        border-color: #5d6dbe;
        box-shadow: 0 0 0 0.25rem rgba(93,109,190,0.25);
        outline: none;
    }
    .stTextInput>label, .stTextArea>label, .stNumberInput>label {
        font-weight: 600;
        color: #495057;
        margin-bottom: 0.6rem;
    }

    /* Information, Success, Error, Warning Boxes (Consistent with main app) */
    [data-testid="stAlert"] {
        border-radius: 10px;
        padding: 18px 25px;
        margin-bottom: 1.8rem;
        font-size: 1.05em;
        line-height: 1.6;
        text-align: left;
        box-shadow: 0 2px 10px rgba(0,0,0,0.05);
    }
    [data-testid="stAlert"] .streamlit-warning {
        background-color: #fef7e0; color: #7a5f00; border-left: 6px solid #ffcc00;
    }
    [data-testid="stAlert"] .streamlit-success {
        background-color: #e6ffe6;
        border-left: 6px solid #4CAF50;
        color: black !important;
    }
    [data-testid="stAlert"] .streamlit-success p,
    [data-testid="stAlert"] .streamlit-success span,
    [data-testid="stAlert"] .streamlit-success div,
    [data-testid="stAlert"] .streamlit-success strong {
        color: black !important;
        -webkit-text-fill-color: black !important;
        opacity: 1 !important;
    }
    [data-testid="stAlert"] .streamlit-error {
        background-color: #ffe6e6; color: #8c0a0a; border-left: 6px solid #e74c3c;
    }
    .stInfo { /* Ensure st.info is also styled consistently */
        background-color: #e6f7ff;
        border-left: 6px solid #64b5f6;
        color: black !important;
        border-radius: 10px;
        padding: 18px 25px;
        margin-top: 2rem;
        font-style: italic;
        font-size: 1.05em;
        box-shadow: 0 2px 10px rgba(0,0,0,0.05);
    }
    .stInfo p, .stInfo span, .stInfo strong, .stInfo div {
        color: black !important;
        -webkit-text-fill-color: black !important;
        opacity: 1 !important;
    }


    /* Container for sections (Consistent with main app) */
    .stContainer {
        border-radius: 12px;
        box-shadow: 0 8px 20px rgba(0,0,0,0.08);
        border: 1px solid #e0e0e0;
        padding: 2rem;
        margin-bottom: 2.5rem;
        background-color: #ffffff;
    }
    .stContainer.has-border {
        border: 1px solid #e0e0e0;
    }

    /* Expander styling (Consistent with main app) */
    .streamlit-expanderHeader {
        background-color: #ffffff;
        border: 1px solid #e0e0e0;
        border-radius: 10px;
        padding: 12px 18px;
        margin-bottom: 0.8rem;
        cursor: pointer;
        transition: background-color 0.2s ease, box-shadow 0.2s ease;
        box-shadow: 0 4px 10px rgba(0,0,0,0.05);
    }
    .streamlit-expanderHeader:hover {
        background-color: #f7f9fb;
        box-shadow: 0 6px 15px rgba(0,0,0,0.08);
    }
    .streamlit-expanderHeader > div > div > p {
        font-weight: 600;
        color: #343a40;
        font-size: 1.05em;
    }
    .streamlit-expanderContent {
        background-color: #f8f9fa;
        border-left: 4px solid #ced4da;
        padding: 15px 20px;
        border-bottom-left-radius: 10px;
        border-bottom-right-radius: 10px;
        margin-top: -10px;
        box-shadow: inset 0 2px 5px rgba(0,0,0,0.03);
        line-height: 1.6;
    }
    /* Ensure text within expander content is black */
    .streamlit-expanderContent p,
    .streamlit-expanderContent span,
    .streamlit-expanderContent div,
    .streamlit-expanderContent strong {
        color: black !important;
        -webkit-text-fill-color: black !important;
        opacity: 1 !important;
    }

    /* Table styling (for dataframes) */
    .stDataFrame {
        border: 1px solid #e9ecef;
        border-radius: 10px;
        overflow: hidden; /* Ensures rounded corners */
        box-shadow: 0 4px 10px rgba(0,0,0,0.05);
    }

    /* General text color in case Streamlit overrides */
    div[data-testid="stVerticalBlock"] div > p,
    div[data-testid="stHorizontalBlock"] div > p,
    div[data-testid="stText"] p {
        color: #343a40 !important;
    }
    </style>
    """, unsafe_allow_html=True)
    # --- END CUSTOM CSS ---

    # Header
    col1, col2 = st.columns([4, 1])
    with col1:
        st.markdown(f"<h1>👑 DilBot Admin Dashboard</h1>", unsafe_allow_html=True)
        st.markdown("<p class='header-tagline'>Monitor users, conversations, and app analytics</p>", unsafe_allow_html=True)
    with col2:
        st.markdown("<div style='height: 40px;'></div>", unsafe_allow_html=True) # Spacer
        if st.button("Logout", key="admin_logout", use_container_width=True):
            st.session_state.authenticated = False
            st.session_state.username = None
            st.session_state.is_admin = False
            st.rerun()
    
    # Log admin access
    log_admin_activity("Dashboard Access", "Viewed admin dashboard")
    
    # Get statistics
    stats = get_admin_stats()
    
    # Overview metrics
    st.markdown("<h2> Overview</h2>", unsafe_allow_html=True)
    with st.container(border=True): # Wrap metrics in a container for styling
        col1, col2, col3, col4 = st.columns(4)
        
        with col1:
            st.metric("Total Users", stats["total_users"])
        with col2:
            st.metric("Active Users", stats["active_users"])
        with col3:
            st.metric("New Today", stats["users_today"])
        with col4:
            st.metric("Total Conversations", stats["total_conversations"])
    
    # User registration trend
    st.markdown("<h2> User Registration Trend</h2>", unsafe_allow_html=True)
    with st.container(border=True): # Wrap chart in a container
        if stats["user_details"]:
            # Create registration data
            reg_data = {}
            for user in stats["user_details"]:
                date = user["joined"]
                reg_data[date] = reg_data.get(date, 0) + 1
            
            # Convert to DataFrame for Altair compatibility and sorting
            chart_data_list = [{"date": date, "registrations": count} for date, count in reg_data.items()]
            chart_df = pd.DataFrame(chart_data_list).sort_values("date")

            if not chart_df.empty:
                chart = alt.Chart(chart_df).mark_line(point=True).encode(
                    x=alt.X('date:T', title='Date'),
                    y=alt.Y('registrations:Q', title='New Registrations'),
                    tooltip=[alt.Tooltip('date:T', title='Date'), 'registrations:Q']
                ).properties(
                    title="Daily User Registrations"
                ).interactive() # Make interactive for zoom/pan
                st.altair_chart(chart, use_container_width=True)
            else:
                st.info("No registration data available to display trend.")
        else:
            st.info("No user data available to display registration trend.")
    
    # Detailed user table
    st.markdown("<h2>👥 User Details</h2>", unsafe_allow_html=True)
    with st.container(border=True): # Wrap user details section in a container
        # Search and filter
        col1, col2 = st.columns([2, 1])
        with col1:
            search_term = st.text_input(" Search users", placeholder="Search by username or email", label_visibility="visible")
        with col2:
            min_conversations = st.number_input("Min conversations", min_value=0, value=0, label_visibility="visible")
        
        # Filter users
        filtered_users = stats["user_details"]
        if search_term:
            filtered_users = [u for u in filtered_users if
                              search_term.lower() in u["username"].lower() or
                              search_term.lower() in u["email"].lower()]
        
        if min_conversations > 0:
            filtered_users = [u for u in filtered_users if u["conversations"] >= min_conversations]
        
        # Display user table
        if filtered_users:
            for user in filtered_users:
                with st.expander(f"👤 **{user['username']}** ({user['conversations']} conversations)"): # Bold username
                    col1_detail, col2_detail = st.columns(2) # Renamed columns to avoid conflict
                    
                    with col1_detail:
                        st.write(f"**Email:** {user['email']}")
                        st.write(f"**Joined:** {user['joined']}")
                        st.write(f"**Last Activity:** {user['last_activity']}")
                    
                    with col2_detail:
                        st.write(f"**Conversations:** {user['conversations']}")
                        st.write(f"**Most Common Emotion:** {user['most_common_emotion'].capitalize()}") # Capitalize emotion
                        
                        # Show emotion breakdown
                        if user['emotions_breakdown']:
                            st.write("**Emotion Breakdown:**")
                            # Convert to DataFrame for a nicer table display
                            emotions_df = pd.DataFrame([{"Emotion": e.capitalize(), "Count": c} for e, c in user['emotions_breakdown'].items()])
                            st.dataframe(emotions_df, hide_index=True, use_container_width=True)
                        else:
                            st.info("No emotion data for this user.")
                    
                    st.markdown("---") # Separator for actions
                    # Quick actions
                    col1_actions, col2_actions, col3_actions = st.columns(3) # Renamed columns
                    with col1_actions:
                        if st.button(f"View Journal", key=f"view_{user['username']}", use_container_width=True):
                            # Show user's recent conversations
                            user_journal = load_user_journal(user['username'])
                            if user_journal:
                                st.subheader(f"Recent conversations for {user['username']}")
                                for entry in user_journal[-5:]: # Last 5 activities
                                    st.text_area(
                                        f"{entry['date']} - {entry['emotion'].capitalize()} ({round(entry['confidence'] * 100)}/ confidence)",
                                        f"User: {entry['user_input']}\nDilBot: {entry['response']}",
                                        height=100,
                                        disabled=True,
                                        key=f"journal_entry_{user['username']}_{entry['date']}_{hash(entry['user_input'])}" # Unique key for text_area
                                    )
                            else:
                                st.info("No conversations found for this user.")
                    
                    with col2_actions:
                        reset_key = f"reset_{user['username']}"
                        confirm_key = f"confirm_{user['username']}"
                        
                        # Use a persistent state for confirmation
                        if confirm_key not in st.session_state:
                            st.session_state[confirm_key] = False

                        if st.button(f"Reset Data", key=reset_key, use_container_width=True):
                            st.session_state[confirm_key] = True  # Flag to show confirmation
                            st.rerun() # Rerun to display confirmation message/buttons
                        
                        if st.session_state.get(confirm_key, False):
                            st.warning(f"Are you sure you want to reset ALL data for {user['username']}? This action is irreversible.")
                            col_confirm_yes, col_confirm_no = st.columns(2)
                            with col_confirm_yes:
                                if st.button(f"Yes, Reset", key=f"confirm_reset_{user['username']}", use_container_width=True):
                                    # Clear user's journal
                                    journal_path = get_user_file_path(user['username'], "journal.json")
                                    if os.path.exists(journal_path):
                                        os.remove(journal_path)
                                        # Also remove vectorstore if it exists
                                        vectorstore_path = get_user_file_path(user['username'], "faiss_index")
                                        if os.path.exists(vectorstore_path):
                                            import shutil
                                            shutil.rmtree(vectorstore_path) # Remove directory for FAISS
                                        
                                    log_admin_activity("User Data Reset", f"Reset data for {user['username']}")
                                    st.success(f"Data reset for {user['username']}!")
                                    st.session_state[confirm_key] = False  # Reset confirmation flag
                                    st.rerun()
                            with col_confirm_no:
                                if st.button(f"Cancel", key=f"cancel_reset_{user['username']}", use_container_width=True):
                                    st.session_state[confirm_key] = False  # Cancel confirmation
                                    st.info(f"Reset for {user['username']} cancelled.")
                                    st.rerun() # Rerun to hide confirmation buttons

                    #with col3_actions:
                        # Placeholder for other actions like "Deactivate User"
                        #st.button("Deactivate User (WIP)", key=f"deactivate_{user['username']}", disabled=True, use_container_width=True)

        else:
            st.info("No users found matching your criteria.")
    
    # System Analytics
    st.markdown("<h2> System Analytics</h2>", unsafe_allow_html=True)
    with st.container(border=True): # Wrap system analytics in a container
        col1_analytics, col2_analytics = st.columns(2) # Renamed columns
        
        with col1_analytics:
            st.markdown("<h4>Emotion Distribution (All Users)</h4>", unsafe_allow_html=True)
            # Aggregate all emotions
            all_emotions = {}
            for user in stats["user_details"]:
                for emotion, count in user['emotions_breakdown'].items():
                    all_emotions[emotion] = all_emotions.get(emotion, 0) + count
            
            if all_emotions:
                emotion_chart_data = [{"emotion": emotion.capitalize(), "count": count}
                                      for emotion, count in all_emotions.items()]
                
                emotion_chart = alt.Chart(pd.DataFrame(emotion_chart_data)).mark_bar().encode(
                    x=alt.X('emotion:N', title='Emotion', sort='-y'), # Sort by count descending
                    y=alt.Y('count:Q', title='Frequency'),
                    color=alt.Color('emotion:N', legend=None, scale=alt.Scale(
                        range=['#4CAF50', '#FFC107', '#E74C3C', '#3498DB', '#9B59B6', '#1ABC9C', '#FF5733'])), # More colors
                    tooltip=['emotion:N', 'count:Q']
                ).properties(
                    title="Overall Emotion Distribution"
                ).interactive()
                st.altair_chart(emotion_chart, use_container_width=True)
            else:
                st.info("No emotion data available for overall analysis.")
        
        with col2_analytics:
            st.markdown("<h4>User Activity Levels</h4>", unsafe_allow_html=True)
            activity_levels = {"Inactive (0)": 0, "Light (1-5)": 0, "Moderate (6-20)": 0, "Heavy (21+)": 0}
            
            for user in stats["user_details"]:
                conv_count = user["conversations"]
                if conv_count == 0:
                    activity_levels["Inactive (0)"] += 1
                elif conv_count <= 5:
                    activity_levels["Light (1-5)"] += 1
                elif conv_count <= 20:
                    activity_levels["Moderate (6-20)"] += 1
                else:
                    activity_levels["Heavy (21+)"] += 1
            
            activity_data = [{"level": level, "users": count} for level, count in activity_levels.items()]
            
            if activity_data:
                activity_chart = alt.Chart(pd.DataFrame(activity_data)).mark_arc(outerRadius=120, innerRadius=80).encode( # Donut chart
                    theta=alt.Theta('users:Q'),
                    color=alt.Color('level:N', title="Activity Level", scale=alt.Scale(
                        range=['#6c757d', '#64b5f6', '#5d6dbe', '#4a5c9d'])), # Consistent color scheme
                    order=alt.Order('users:Q', sort='descending'),
                    tooltip=['level:N', 'users:Q']
                ).properties(
                    title="User Activity Distribution"
                ).interactive()
                st.altair_chart(activity_chart, use_container_width=True)
            else:
                st.info("No user activity data to display.")
    
    # Admin logs
    st.markdown("<h2> Admin Activity Logs</h2>", unsafe_allow_html=True)
    with st.container(border=True): # Wrap admin logs in a container
        admin_logs = get_admin_logs()
        
        if admin_logs:
            st.subheader("Recent Admin Activities (Last 10)")
            # Display logs in a more readable format, perhaps a table or structured text
            for log_entry in reversed(admin_logs[-10:]): # Last 10 activities, reversed to show newest first
                timestamp = datetime.datetime.fromisoformat(log_entry["timestamp"])
                st.markdown(f"**{timestamp.strftime('%Y-%m-%d %H:%M:%S')}** - **{log_entry['action']}**: `{log_entry['details']}`")
                st.markdown("---") # Small separator
        else:
            st.info("No admin activities logged yet.")
    
    # Data Export and Admin Log Clearing
    st.markdown("<h2>Data Management</h2>", unsafe_allow_html=True)
    with st.container(border=True): # Wrap data export in a container
        col1_export, col2_export = st.columns(2) # Renamed columns
        
        with col1_export:
            st.markdown("<h4>Export Application Data</h4>", unsafe_allow_html=True)
            if st.button("Export All Data (JSON)", key="export_data_btn", use_container_width=True):
                export_data = {
                    "export_timestamp": str(datetime.datetime.now()),
                    "statistics": stats,
                    "admin_logs": admin_logs
                }
                
                st.download_button(
                    label="Download Exported Data",
                    data=json.dumps(export_data, indent=4),
                    file_name=f"dilbot_admin_export_{datetime.date.today().isoformat()}.json",
                    mime="application/json",
                    key="download_export_btn",
                    use_container_width=True
                )
                
                log_admin_activity("Data Export", "Initiated data export")
                st.success("Data export ready for download!")
        
        with col2_export:
            st.markdown("<h4>Clear Admin Activity Logs</h4>", unsafe_allow_html=True)
            # Add a confirmation step for clearing logs
            clear_log_confirm_key = "clear_log_confirm"
            if clear_log_confirm_key not in st.session_state:
                st.session_state[clear_log_confirm_key] = False

            if st.button("Clear Admin Logs", key="clear_admin_logs_btn", use_container_width=True):
                st.session_state[clear_log_confirm_key] = True
                st.rerun()

            if st.session_state.get(clear_log_confirm_key, False):
                st.warning("Are you sure you want to clear ALL admin logs? This action is irreversible.")
                col_clear_yes, col_clear_no = st.columns(2)
                with col_clear_yes:
                    if st.button("Yes, Clear Logs", key="confirm_clear_logs_btn", use_container_width=True):
                        admin_log_path = "data/admin_log.json"
                        if os.path.exists(admin_log_path):
                            os.remove(admin_log_path)
                        st.success("Admin logs cleared successfully!")
                        log_admin_activity("Admin Logs Cleared", "All admin activity logs were cleared")
                        st.session_state[clear_log_confirm_key] = False
                        st.rerun()
                with col_clear_no:
                    if st.button("Cancel", key="cancel_clear_logs_btn", use_container_width=True):
                        st.session_state[clear_log_confirm_key] = False
                        st.info("Clearing admin logs cancelled.")
                        st.rerun()

    st.markdown("---")
    # Back to main app button (aligned left in a container)
    


    st.markdown("<p class='footer-caption'>DilBot Admin Panel | Built by Members of CSG Hackathon Team</p>", unsafe_allow_html=True)
            
def speak(text, username):
    """Generate and play audio response"""
    tts = gTTS(text=text, lang='en')
    audio_path = get_user_file_path(username, "response.mp3")
    tts.save(audio_path)
    st.audio(audio_path, format="audio/mp3")


def transcribe_audio_file(uploaded_audio):
    """Transcribe uploaded audio file"""
    recognizer = sr.Recognizer()
    try:
        with sr.AudioFile(uploaded_audio) as source:
            audio_data = recognizer.record(source)
            text = recognizer.recognize_google(audio_data)
            return text
    except Exception as e:
        return f"Error: {str(e)}"

def show_main_app():
    """Main DilBot application"""
    username = st.session_state.username

    st.set_page_config(page_title="DilBot - Emotional AI", page_icon="🧠", layout="wide")

    
    quote_categories = {
        "Grief": ["Grief is the price we pay for love.", "Tears are the silent language of grief.", "What we have once enjoyed we can never lose; all that we love deeply becomes a part of us."],
        "Motivation": ["Believe in yourself and all that you are.", "Tough times never last, but tough people do.", "The only way to do great work is to love what you do."],
        "Healing": ["Every wound has its own time to heal.", "It's okay to take your time to feel better.", "Healing is not linear, and that's perfectly okay."],
        "Relationships": ["The best relationships are built on trust.", "Love is not about possession but appreciation.", "Healthy relationships require both people to show up authentically."]
    }

    # ---  CUSTOM CSS  ---
    st.markdown("""
    <style>
    /* Global Styles & Background */
    .stApp {
        background: linear-gradient(to bottom right, #f8f9fa, #e9ecef);
        color: #343a40;
        font-family: 'Segoe UI', Roboto, 'Helvetica Neue', Arial, sans-serif;
        -webkit-font-smoothing: antialiased;
        padding-top: 2rem;
    }

    /* Streamlit's main block container for content centering and width */
    .main .block-container {
        max-width: 1200px;
        padding-top: 2rem;
        padding-bottom: 2rem;
        padding-left: 1rem;
        padding-right: 1rem;
    }

    /* Hide Streamlit's header/footer if you want a fully custom layout */
    .stApp > header {
        display: none;
    }
    .stApp > footer {
        display: none;
    }

    /* Header & Titles */
    h1, h2, h3, h4, h5, h6 {
        color: #212529;
        margin-top: 1.8rem;
        margin-bottom: 0.9rem;
        font-weight: 700;
    }
    h1 {
        font-size: 2.8em;
        font-weight: 800;
        color: #5d6dbe;
        letter-spacing: -0.8px;
        text-shadow: 1px 1px 2px rgba(0,0,0,0.05);
    }
    .header-tagline {
        font-size: 1.2em;
        color: #6c757d;
        margin-top: -0.5rem;
        margin-bottom: 2.5rem;
        font-weight: 400;
    }
    h2 {
        font-size: 2em;
        font-weight: 700;
        border-bottom: 2px solid #e9ecef;
        padding-bottom: 0.7rem;
        margin-bottom: 2rem;
        color: #343a40;
    }
    h3 {
        font-size: 1.6em;
        font-weight: 600;
        color: #495057;
        margin-top: 2rem;
        margin-bottom: 1.2rem;
    }
    h5 {
        font-size: 1.1em;
        font-weight: 600;
        color: #495057;
        margin-bottom: 1rem;
    }

    /* Metrics (Total Conversations, Most Common Emotion, Avg. Confidence) */
    [data-testid="stMetric"] {
        background-color: #ffffff;
        border: 1px solid #dee2e6;
        border-radius: 12px;
        padding: 20px;
        box-shadow: 0 8px 20px rgba(0,0,0,0.08);
        text-align: center;
        margin-bottom: 1.5rem;
        transition: transform 0.2s ease-in-out, box-shadow 0.2s ease-in-out;
    }
    [data-testid="stMetric"]:hover {
        transform: translateY(-5px);
        box-shadow: 0 12px 30px rgba(0,0,0,0.12);
    }
    [data-testid="stMetricLabel"] {
        font-size: 1em;
        color: #6c757d;
        margin-bottom: 8px;
        font-weight: 500;
    }
    [data-testid="stMetricValue"] {
        font-size: 2.8em;
        font-weight: 800;
        color: #5d6dbe;
    }

    /* Buttons */
    .stButton>button {
        background-color: #5d6dbe;
        color: black;
        border: none;
        border-radius: 10px;
        padding: 12px 25px;
        font-size: 1.1em;
        font-weight: bold;
        transition: background-color 0.3s ease, transform 0.2s ease, box-shadow 0.3s ease;
        box-shadow: 0 5px 15px rgba(0,0,0,0.1);
        cursor: pointer;
        letter-spacing: 0.5px;
    }
    .stButton>button:hover {
        background-color: #4a5c9d;
        transform: translateY(-3px);
        box-shadow: 0 8px 20px rgba(0,0,0,0.15);
    }
    .stButton>button:active {
        background-color: #3b4b80;
        transform: translateY(0);
        box-shadow: 0 3px 10px rgba(0,0,0,0.1);
    }
    /* Logout button specific style */
    [key="logout_btn"] > button {
        background-color: #dc3545;
        box-shadow: 0 5px 15px rgba(220,53,69,0.2);
    }
    [key="logout_btn"] > button:hover {
        background-color: #c82333;
        box-shadow: 0 8px 20px rgba(220,53,69,0.3);
    }


    /* Text Inputs and Text Areas */
    .stTextInput>div>div>input, .stTextArea>div>div>textarea {
        border-radius: 10px;
        border: 1px solid #ced4da;
        padding: 14px 18px;
        font-size: 1.05em;
        color: #343a40;
        background-color: #fcfcfc;
        box-shadow: inset 0 2px 5px rgba(0,0,0,0.03);
        transition: border-color 0.3s ease, box-shadow 0.3s ease;
    }
    .stTextInput>div>div>input:focus, .stTextArea>div>div>textarea:focus {
        border-color: #5d6dbe;
        box-shadow: 0 0 0 0.25rem rgba(93,109,190,0.25);
        outline: none;
    }
    .stTextInput>label, .stTextArea>label {
        font-weight: 600;
        color: #495057;
        margin-bottom: 0.6rem;
    }

    /* Selectbox (Dropdown) Text Color Fixes */
    [data-testid="stSelectbox"] > div:first-child > div:first-child {
        border-radius: 10px;
        border: 1px solid #ced4da;
        background-color: #fcfcfc;
        box-shadow: inset 0 2px 5px rgba(0,0,0,0.03);
        display: flex;
        align-items: center;
        min-height: 48px;
        padding: 0 10px;
    }
    .stSelectbox>label {
        font-weight: 600;
        color: #495057;
        margin-bottom: 0.6rem;
    }
    /* Force selected value text to black */
    [data-testid="stSelectbox"] input[type="text"] {
        color: black !important;
        -webkit-text-fill-color: black !important; /* For Webkit browsers */
        opacity: 1 !important;
        flex-grow: 1;
        padding: 12px 5px;
        font-size: 1.05em;
        line-height: 1.2em;
        min-height: 1.2em;
        background-color: transparent !important;
        border: none !important;
        outline: none !important;
        box-shadow: none !important;
    }
    /* Force dropdown arrow to black */
    [data-testid="stSelectbox"] button {
        background-color: transparent !important;
        border: none !important;
        padding: 0 5px;
        cursor: pointer;
        display: flex;
        align-items: center;
        justify-content: center;
    }
    [data-testid="stSelectbox"] svg {
        fill: black !important;
        color: black !important;
        font-size: 1.5em !important;
        width: 1em !important;
        height: 1em !important;
    }
    [data-testid="stSelectbox"] svg path {
        fill: black !important;
    }


     /* File Uploader with White Text */
    [data-testid="stFileUploaderDropzone"] {
    border-radius: 12px;
    border: 2px dashed #a0a8b4;
    background-color: #2c3e50; /* Dark background to contrast with white text */
    padding: 25px;
    transition: border-color 0.3s ease, background-color 0.3s ease;
    margin-bottom: 1.5rem;
    }

    [data-testid="stFileUploaderDropzone"]:hover {
    border-color: #5d6dbe;
    background-color: #34495e; /* Slightly lighter on hover */
    }

    /* Force ALL text inside file uploader dropzone to WHITE */
    [data-testid="stFileUploaderDropzone"] *,
    [data-testid="stFileUploaderDropzone"] p,
    [data-testid="stFileUploaderDropzone"] span,
    [data-testid="stFileUploaderDropzone"] div,
    [data-testid="stFileUploaderDropzone"] small {
    color: white !important;
    -webkit-text-fill-color: white !important;
    opacity: 1 !important;
    font-weight: normal !important;
    }

    /* Specific targeting for drag and drop text */
    [data-testid="stFileUploaderDropzone"] > div > div > div:nth-child(2) > div:first-child {
    color: white !important;
    }

    [data-testid="stFileUploaderDropzone"] > div > div > div > span {
    color: white !important;
    }

    /* Style the Browse files button with white text */
    [data-testid="stFileUploaderDropzone"] button {
    background-color: #5d6dbe !important;
    color: white !important;
    border: none !important;
    border-radius: 8px !important;
    padding: 10px 20px !important;
    font-weight: bold !important;
    transition: all 0.3s ease !important;
    }

    [data-testid="stFileUploaderDropzone"] button:hover {
    background-color: #4a5c9d !important;
    transform: translateY(-2px) !important;
    box-shadow: 0 4px 12px rgba(0,0,0,0.3) !important;
     }

    /* File uploader label (outside the dropzone) */
    [data-testid="stFileUploader"] label {
    color: black !important;
    -webkit-text-fill-color: black !important;
    opacity: 1 !important;
    font-weight: 600 !important;
    margin-bottom: 0.5rem !important;
     }

    /* Uploaded file names */
    [data-testid="stFileUploaderFileName"] {
    font-size: 0.95em;
    color: black !important;
    -webkit-text-fill-color: black !important;
    margin-top: 10px;
    word-break: break-all;
    background-color: rgba(255,255,255,0.1) !important;
    padding: 8px 12px !important;
    border-radius: 6px !important;
    }

    [data-testid="stFileUploaderFile"] {
    background-color: rgba(255,255,255,0.1) !important;
    border-radius: 8px;
    padding: 8px 12px;
    margin-top: 10px;
    box-shadow: 0 2px 5px rgba(0,0,0,0.05);
    }

    /* Style the cloud upload icon to be white */
    [data-testid="stFileUploaderDropzone"] svg {
    fill: white !important;
    color: white !important;
    }

    
    /* Information, Success, Error, Warning Boxes */
    [data-testid="stAlert"] {
        border-radius: 10px;
        padding: 18px 25px;
        margin-bottom: 1.8rem;
        font-size: 1.05em;
        line-height: 1.6;
        text-align: left;
        box-shadow: 0 2px 10px rgba(0,0,0,0.05));
    }
    /* Warning alert style  */
    [data-testid="stAlert"] .streamlit-warning {
        background-color: #fef7e0; color: #7a5f00; border-left: 6px solid #ffcc00;
    }

    [data-testid="stAlert"] .streamlit-success {
        background-color: #9aff9a;
        border-left: 6px solid #4CAF50;
        color: black !important; 
    }
    /* Aggressive targeting for specific text elements within st.success */
    [data-testid="stAlert"] .streamlit-success p,
    [data-testid="stAlert"] .streamlit-success span,
    [data-testid="stAlert"] .streamlit-success div,
    [data-testid="stAlert"] .streamlit-success strong { /* Added strong for bold text */
        color: black !important;
        -webkit-text-fill-color: black !important;
        opacity: 1 !important;
    }

    /* Error alert style  */
    [data-testid="stAlert"] .streamlit-error {
        background-color: #ffe6e6; color: #8c0a0a; border-left: 6px solid #e74c3c;
    }
    .stInfo {
    background-color: #e6f7ff;
    border-left: 6px solid #64b5f6;
    color: black !important; /* Changed from black to white */
    border-radius: 10px;
    padding: 18px 25px;
    margin-top: 2rem;
    font-style: italic;
    font-size: 1.05em;
    box-shadow: 0 2px 10px rgba(0,0,0,0.05);
    }

    /* Aggressive targeting for specific text elements within st.info */
    .stInfo p,
    .stInfo span,
    .stInfo strong,
    .stInfo div {
    color: white !important;
    -webkit-text-fill-color: white !important;
    opacity: 1 !important;
     }


    /* Chat Message Bubbles  */
    .user-message-container {
        display: flex;
        justify-content: flex-end;
        margin-bottom: 15px;
    }
    .user-message {
        background-color: #e0e6f6;
        color: #343a40;
        border-radius: 18px 18px 5px 18px;
        padding: 12px 18px;
        max-width: 75%;
        box-shadow: 0 4px 10px rgba(0,0,0,0.08);
        line-height: 1.5;
        text-align: left;
    }

    .bot-message-container {
        display: flex;
        justify-content: flex-start;
        margin-bottom: 15px;
    }
    .bot-message {
        background-color: #ffffff;
        color: #343a40;
        border-radius: 18px 18px 18px 5px;
        padding: 12px 18px;
        max-width: 75%;
        box-shadow: 0 4px 10px rgba(0,0,0,0.08);
        line-height: 1.5;
        text-align: left;
    }

    .chat-separator {
        border-bottom: 1px dashed #ced4da;
        margin: 25px 0;
    }
    .chat-title {
        font-size: 1.8em;
        font-weight: 700;
        color: #5d6dbe;
        text-align: center;
        margin-bottom: 2rem;
    }


    /* Audio Player (kept original styles) */
    .stAudio {
        margin-top: 1.5rem;
        margin-bottom: 2rem;
    }

    /* Expander styling (for conversations - kept original styles) */
    .streamlit-expanderHeader {
        background-color: #ffffff;
        border: 1px solid #e0e0e0;
        border-radius: 10px;
        padding: 12px 18px;
        margin-bottom: 0.8rem;
        cursor: pointer;
        transition: background-color 0.2s ease, box-shadow 0.2s ease;
        box-shadow: 0 4px 10px rgba(0,0,0,0.05);
    }
    .streamlit-expanderHeader:hover {
        background-color: #f7f9fb;
        box-shadow: 0 6px 15px rgba(0,0,0,0.08);
    }
    .streamlit-expanderHeader > div > div > p {
        font-weight: 600;
        color: #343a40;
        font-size: 1.05em;
    }
    .streamlit-expanderContent {
        background-color: #f8f9fa;
        border-left: 4px solid #ced4da;
        padding: 15px 20px;
        border-bottom-left-radius: 10px;
        border-bottom-right-radius: 10px;
        margin-top: -10px;
        box-shadow: inset 0 2px 5px rgba(0,0,0,0.03);
        line-height: 1.6;
    }

    /* Horizontal rule (kept original styles) */
    hr {
        border-top: 2px solid #e9ecef;
        margin-top: 3rem;
        margin-bottom: 3rem;
    }

    /* Caption (footer - kept original styles) */
    .footer-caption {
        color: #868e96;
        font-size: 0.9em;
        margin-top: 3rem;
        display: block;
        text-align: center;
        letter-spacing: 0.2px;
    }

    /* Container for sections (kept original styles) */
    .stContainer {
        border-radius: 12px;
        box-shadow: 0 8px 20px rgba(0,0,0,0.08);
        border: 1px solid #e0e0e0;
        padding: 2rem;
        margin-bottom: 2.5rem;
        background-color: #ffffff;
    }
    .stContainer.has-border {
        border: 1px solid #e0e0e0;
    }
    /*confidence color*/
    .black-text {
    color: black !important;
    -webkit-text-fill-color: black !important;
    opacity: 1 !important;
     }


    .stCustomSuccess {
    background-color: #e6ffe6;
    border-left: 6px solid #4CAF50;
    border-radius: 10px;
    padding: 18px 25px;
    margin-bottom: 1.8rem;
    font-size: 1.05em;
    line-height: 1.6;
    text-align: left;
    box-shadow: 0 2px 10px rgba(0,0,0,0.05);
     }
    .stCustomSuccess .black-text { 
    color: black !important;
    -webkit-text-fill-color: black !important;
    opacity: 1 !important;
      }
      /*for quotes */
      /* Custom CSS for a reusable "info-box" component */
    .custom-info-box {
    background-color: #e6f7ff;
    border-left: 6px solid #64b5f6;
    border-radius: 10px;
    padding: 18px 25px;
    margin-top: 2rem;
    font-size: 1.05em;
    box-shadow: 0 2px 10px rgba(0,0,0,0.05);
    display: flex;
    align-items: center;
     }

    /* Style for the icon within the custom info box */
    .custom-info-box .info-icon {
    font-size: 1.5em;
    margin-right: 10px;
    color: #64b5f6;
    }

/* Styles for the text content within the custom info box */
   .custom-info-box .info-text {
    color: black !important;
    -webkit-text-fill-color: black !important;
    opacity: 1 !important;
    font-style: italic; /* Apply italic here directly */
    margin: 0; /* Remove default paragraph margin */
      }

}



    </style>
    """, unsafe_allow_html=True)

    # --- Streamlit UI Components ---
    # Header with logout button
    col1, col2 = st.columns([4, 1])
    with col1:
        st.markdown(f"<h1>🧠 DilBot</h1>", unsafe_allow_html=True)
        st.markdown(f"<p class='header-tagline'>Welcome back, {username}! Your personal emotional AI companion</p>", unsafe_allow_html=True)
    with col2:
        st.markdown("<div style='height: 40px;'></div>", unsafe_allow_html=True) # Spacer
        if st.button("Logout", key="logout_btn", use_container_width=True):
            st.session_state.authenticated = False
            st.session_state.username = None
            st.rerun()

    # Input section wrapped in a styled container
    st.markdown("<h3>Input Your Thoughts</h3>", unsafe_allow_html=True)
    with st.container(border=True): # Streamlit's built-in container with border
        col1, col2 = st.columns(2)
        with col1:
            # Reverting to direct st.selectbox with a visible label
            st.markdown(" **Choose a quote theme:**") # Custom label for the selectbox
            selected_category = st.selectbox("Select Quote Theme", list(quote_categories.keys()), label_visibility="collapsed") # Original label hidden
            # The custom markdown above provides the visual label, while the st.selectbox's internal label is hidden.

        with col2:
            st.markdown("<h5> Custom Quotes & Voice Input</h5>", unsafe_allow_html=True)
            # Ensure file uploader labels are visible
            uploaded_quotes = st.file_uploader("Upload your own quotes (.txt)", type=["txt"], key="quote_uploader")
            uploaded_audio = st.file_uploader("Upload a voice message (.wav)", type=["wav"], key="audio_uploader")

            # Voice transcription button
            if uploaded_audio and st.button(" Transcribe Voice Message", key="transcribe_btn", use_container_width=True):
                with st.spinner("Transcribing your voice..."):
                    transcribed = transcribe_audio_file(uploaded_audio)
                    if transcribed.startswith("Error:"):
                        st.error(transcribed)
                    else:
                        st.session_state.transcribed_text = transcribed
                        st.success(" Voice transcribed successfully!")

    # Handle vectorstore 
    current_quotes = []
    vectorstore = None

    if uploaded_quotes:
        custom_quotes = uploaded_quotes.read().decode("utf-8").splitlines()
        custom_quotes = [quote.strip() for quote in custom_quotes if quote.strip()]
        vectorstore = build_user_vectorstore(username, custom_quotes)
        current_quotes = custom_quotes
        st.success(f" {len(custom_quotes)} custom quotes uploaded and saved!")
    else:
        default_quotes = quote_categories[selected_category]
        vectorstore = load_user_vectorstore(username)
        if vectorstore is None:
            vectorstore = build_user_vectorstore(username, default_quotes)
        current_quotes = default_quotes

    # Input area for user message
    st.markdown("<h3>What's on your mind?</h3>", unsafe_allow_html=True)
    user_input = st.text_area(
        "Share your thoughts, feelings, or experiences...",
        value=st.session_state.transcribed_text,
        height=180, # Increased height for more typing space
        placeholder="Type here or use your transcribed voice message...",

    )
    # Re-adding explicit label for text area if needed, to be safe
    # st.markdown("<label class='stTextArea>label' style='margin-bottom:0.6rem;padding-left:5px;'>Share your thoughts, feelings, or experiences...</label>", unsafe_allow_html=True)


    final_input = user_input.strip() or st.session_state.transcribed_text.strip()

    # Main interaction button
    if st.button("🧠 Talk to DilBot", type="primary", use_container_width=True):
        if not final_input:
            st.warning(" Please enter something to share or upload a voice message.")
        else:
            with st.spinner("DilBot is thinking and feeling..."):
                # Emotion detection 
                emotion, score = detect_emotion(final_input)

                # Get AI response
                prompt_template = PromptTemplate(
                    input_variables=["context", "user_input", "username"],
                    template="""You are DilBot, an empathetic emotional support AI companion for {username}.
Use the following emotional quote context to respond gently, supportively, and personally.
Context quotes:
{context}
User's message:
{user_input}
Respond as DilBot with warmth, empathy, and understanding. Keep it conversational and supportive."""
                )

                # Get similar quotes (ORIGINAL LOGIC - NO CHANGE)
                similar_docs = vectorstore.similarity_search(final_input, k=2)
                context = "\n".join([doc.page_content for doc in similar_docs])

                # Generate response (ORIGINAL LOGIC - NO CHANGE)
                groq_llm = ChatGroq(api_key=GROQ_API_KEY, model="llama3-70b-8192")
                chain = LLMChain(llm=groq_llm, prompt=prompt_template)
                response = chain.run(context=context, user_input=final_input, username=username)

                # Save to user's journal (ORIGINAL LOGIC - NO CHANGE)
                save_user_journal(username, final_input, emotion, score, response)

                # Display results with new chat bubble styling
                st.markdown("<h3 class='chat-title'>DilBot's Conversation:</h3>", unsafe_allow_html=True)
                with st.container(border=True): # Container for the conversation output
                    # User's input presented in a chat bubble
                    st.markdown(f"<div class='user-message-container'><div class='user-message'>You: {final_input}</div></div>", unsafe_allow_html=True)
                    #st.success(f"**Emotion Detected:** {emotion.capitalize()} ({round(score*100)}/ confidence)")
                    st.markdown(
                            f"""
                               <div class="stCustomSuccess">
                                    <p class="black-text">
                    <strong>Emotion Detected:</strong> {emotion.capitalize()} ({round(score*100)}% confidence)</p></div> """,unsafe_allow_html=True
                                )

                    if is_crisis(final_input):
                        st.error(" Crisis detected! Please reach out to a mental health professional immediately. "
                                 "You are not alone. Consider contacting a helpline like the National Suicide Prevention Lifeline (988 in the US) or a local emergency service.")

                    if current_quotes:
                        model = SentenceTransformer("all-MiniLM-L6-v2")
                        quote_embeddings = model.encode(current_quotes, convert_to_tensor=True)
                        user_embedding = model.encode(final_input, convert_to_tensor=True)
                        sims = util.pytorch_cos_sim(user_embedding, quote_embeddings)[0]
                        best_match = sims.argmax().item()
                        selected_quote = current_quotes[best_match]
                        #st.info(f" **Quote for you:** *{selected_quote}*")
                        st.markdown(f"""<div class="custom-info-box"><span class="info-icon">&#x2139;</span><p class="info-text">
                        <strong>Quote for you:</strong> {selected_quote}</p> </div> """, unsafe_allow_html=True)

                    # DilBot's response presented in a chat bubble
                    st.markdown(f"<div class='bot-message-container'><div class='bot-message'>DilBot: {response}</div></div>", unsafe_allow_html=True)

                    speak(response, username)
                    st.session_state.transcribed_text = ""

            # Add a visual separator after each conversation turn (optional)
            st.markdown("<div class='chat-separator'></div>", unsafe_allow_html=True)


    # User's personal dashboard
    st.markdown("---")
    st.header(" Your Personal Dashboard")

    # Load user's journal
    journal_data = load_user_journal(username)

    if journal_data:
        # Statistics
        st.subheader(" Your Emotional Statistics") # Moved statistics to the top of dashboard for prominence
        with st.container(border=True):
            col1, col2, col3 = st.columns(3)
            with col1:
                st.metric("Total Conversations", len(journal_data))
            with col2:
                emotions = [entry['emotion'] for entry in journal_data]
                most_common = max(set(emotions), key=emotions.count) if emotions else "None"
                st.metric("Most Common Emotion", most_common.capitalize())
            with col3:
                if journal_data:
                    avg_confidence = sum(entry['confidence'] for entry in journal_data) / len(journal_data)
                    st.metric("Avg. Confidence", f"{avg_confidence:.1f}%")
                else:
                    st.metric("Avg. Confidence", "N/A") # Handle case with no journal data for avg. confidence


        # Mood tracker
        st.subheader(" Your Daily Mood Tracker")
        with st.container(border=True): # Wrap chart in a container
            # Prepare data for chart 
            df_data = []
            for entry in journal_data:
                df_data.append({
                "date": entry["date"],
                "emotion": entry["emotion"].capitalize(),
                "confidence": entry["confidence"]
                })
            if df_data:
                df_chart = pd.DataFrame(df_data) # Use pandas DataFrame for better Altair integration

                chart = alt.Chart(df_chart).mark_bar().encode(
                x=alt.X('date:N', title='Date', sort=None), # Sort by date ensures correct order
                y=alt.Y('count():Q', title='Frequency'),
                color=alt.Color('emotion:N', title='Emotion', scale=alt.Scale(range=['#4CAF50', '#FFC107', '#E74C3C', '#3498DB', '#9B59B6', '#1ABC9C'])), # Custom colors
                tooltip=['date:N', 'emotion:N', 'count():Q']
                ).properties(
                    height=350, # Slightly increased height
                    title="Your Emotional Journey Over Time"
                ).interactive() # Make chart interactive for zoom/pan
                st.altair_chart(chart, use_container_width=True)
            else:
                st.info("No emotional data to display yet. Start interacting with DilBot!")

        # Recent conversations
        st.subheader("Recent Conversations")
        recent_entries = journal_data[-5:] if len(journal_data) >= 5 else journal_data

        with st.container(border=True): # Wrap recent conversations in a container
            if recent_entries:
                for i, entry in enumerate(reversed(recent_entries)):
                    with st.expander(f"{entry['date']} - {entry['emotion'].capitalize()} ({round(entry['confidence'] * 100)}%)"): # Round confidence for display
                        st.markdown(f"**You said:** {entry['user_input']}")
                        st.markdown(f"**DilBot replied:** {entry['response']}")
            else:
                st.info("No recent conversations yet. Start talking to DilBot!")

    else:
        st.markdown("""   <div style="background-color: #2c3e50;
            border-left: 6px solid #64b5f6;
            color: white;
            border-radius: 10px;
            padding: 18px 25px;
            margin-top: 2rem;
            font-style: italic;
            font-size: 1.05em;
            box-shadow: 0 2px 10px rgba(0,0,0,0.05);
            margin-bottom: 1.5rem;
              ">
             <p style="color: white; margin: 0; font-size: 1.05em;">
            Start your first conversation with DilBot to see your personal dashboard and insights!
                  </p>
              </div>
                   """, unsafe_allow_html=True)

    st.markdown("---")
    st.markdown("<p class='footer-caption'>Built by Members of CSG Hackathon Team | Your data is stored privately and securely</p>", unsafe_allow_html=True)
# Main app logic
def main():
    if not st.session_state.authenticated:
        show_auth_page()
    elif st.session_state.is_admin:
        show_admin_dashboard()
    else:
        show_main_app()

if __name__ == "__main__":
    main()