File size: 110,974 Bytes
f55ba0b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
55d4d8d
 
 
cff6a7e
55d4d8d
06f5e79
55d4d8d
 
 
cff6a7e
 
f55ba0b
 
 
 
fa64699
f55ba0b
 
 
 
 
 
 
 
 
 
 
 
55d4d8d
06f5e79
 
f55ba0b
 
 
 
 
 
 
55d4d8d
cff6a7e
55d4d8d
cff6a7e
 
 
 
 
f55ba0b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
cff6a7e
 
 
 
 
 
 
55d4d8d
cff6a7e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
55d4d8d
cff6a7e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
55d4d8d
cff6a7e
 
 
 
 
 
 
f55ba0b
cff6a7e
06f5e79
f55ba0b
 
06f5e79
 
 
f55ba0b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
06f5e79
 
 
cff6a7e
 
 
 
f55ba0b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
cff6a7e
 
55d4d8d
f55ba0b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
cff6a7e
f55ba0b
 
 
 
 
55d4d8d
cff6a7e
 
f55ba0b
 
55d4d8d
f55ba0b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
06f5e79
 
f55ba0b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
55d4d8d
 
 
 
06f5e79
 
f55ba0b
 
 
55d4d8d
 
 
 
 
 
a8914cd
55d4d8d
a8914cd
 
55d4d8d
 
cff6a7e
 
f55ba0b
 
 
 
cff6a7e
55d4d8d
 
f55ba0b
 
 
cff6a7e
926e731
f55ba0b
 
 
 
926e731
f55ba0b
926e731
06f5e79
926e731
f55ba0b
 
 
 
 
 
 
 
 
 
926e731
06f5e79
926e731
 
 
 
 
 
 
 
 
 
f55ba0b
 
 
 
 
 
926e731
 
f55ba0b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
926e731
 
f55ba0b
 
 
 
926e731
06f5e79
 
926e731
f55ba0b
 
 
 
 
 
 
 
 
 
926e731
06f5e79
926e731
 
 
 
 
 
 
 
 
 
f55ba0b
 
 
 
 
 
926e731
 
f55ba0b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
926e731
cff6a7e
 
 
 
f55ba0b
 
 
55d4d8d
cff6a7e
 
55d4d8d
cff6a7e
 
 
 
55d4d8d
cff6a7e
 
55d4d8d
cff6a7e
55d4d8d
 
f55ba0b
 
55d4d8d
f55ba0b
55d4d8d
 
f55ba0b
 
 
 
 
 
 
55d4d8d
 
 
 
f55ba0b
ff99726
06f5e79
cff6a7e
ff99726
 
 
 
cff6a7e
ff99726
06f5e79
 
 
ff99726
 
 
 
 
 
cff6a7e
06f5e79
 
 
 
 
cff6a7e
06f5e79
fa64699
 
cff6a7e
fa64699
06f5e79
cff6a7e
ff99726
 
 
 
 
 
 
f55ba0b
 
 
 
06f5e79
 
ff99726
06f5e79
ff99726
 
 
 
 
f55ba0b
ff99726
 
 
06f5e79
 
 
 
 
 
 
 
ff99726
 
 
 
 
 
 
 
 
 
 
 
 
 
06f5e79
fa64699
06f5e79
 
ff99726
 
 
 
 
 
06f5e79
ff99726
 
 
 
 
 
 
 
06f5e79
 
f55ba0b
 
 
 
 
 
ff99726
 
 
06f5e79
 
ff99726
 
 
 
 
06f5e79
ff99726
 
 
06f5e79
ff99726
 
 
 
 
 
 
 
 
 
 
 
 
 
06f5e79
ff99726
 
 
 
 
 
 
 
 
06f5e79
ff99726
 
 
 
 
06f5e79
 
 
 
 
ff99726
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
f55ba0b
 
 
 
ff99726
 
 
06f5e79
ff99726
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
06f5e79
ff99726
 
06f5e79
ff99726
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
f55ba0b
 
 
 
 
 
ff99726
 
 
 
06f5e79
ff99726
 
 
 
 
 
 
 
 
 
 
 
06f5e79
ff99726
 
 
f55ba0b
 
 
 
 
 
ff99726
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
06f5e79
ff99726
 
 
 
 
 
 
 
 
 
06f5e79
f55ba0b
 
 
 
 
ff99726
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
f55ba0b
 
 
 
 
 
 
 
 
 
 
 
06f5e79
ff99726
06f5e79
ff99726
 
 
 
06f5e79
ff99726
 
 
 
 
 
 
06f5e79
 
 
 
 
 
 
 
 
ff99726
 
06f5e79
 
 
 
f55ba0b
ff99726
 
06f5e79
 
ff99726
 
06f5e79
ff99726
06f5e79
 
ff99726
 
 
 
 
 
06f5e79
ff99726
06f5e79
 
ff99726
 
f55ba0b
 
ff99726
 
f55ba0b
 
 
 
 
 
 
 
ff99726
 
f55ba0b
 
ff99726
 
 
 
 
 
f55ba0b
ff99726
 
 
 
 
 
 
 
f55ba0b
 
 
 
ff99726
 
 
 
 
 
 
 
 
f55ba0b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
ff99726
 
f55ba0b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
ff99726
 
 
 
cff6a7e
fa64699
ed28f2d
ff99726
06f5e79
cff6a7e
f55ba0b
ff99726
cff6a7e
55d4d8d
ff99726
 
cff6a7e
 
55d4d8d
06f5e79
ff99726
06f5e79
 
 
 
 
 
 
 
 
 
 
 
 
 
 
ff99726
 
 
 
 
 
 
 
 
 
 
 
 
 
06f5e79
ff99726
 
 
 
06f5e79
ff99726
06f5e79
 
 
ff99726
cff6a7e
ff99726
 
 
 
 
 
 
 
 
 
 
 
 
06f5e79
ff99726
 
 
06f5e79
ff99726
 
 
06f5e79
ff99726
 
 
 
06f5e79
ff99726
 
 
 
06f5e79
ff99726
06f5e79
 
 
ff99726
ed28f2d
ff99726
 
f55ba0b
ff99726
 
 
 
06f5e79
 
f55ba0b
06f5e79
 
 
 
 
 
ff99726
 
 
 
06f5e79
 
 
 
 
 
 
 
f55ba0b
06f5e79
 
 
 
 
 
 
 
 
 
 
 
f55ba0b
06f5e79
 
 
 
ff99726
06f5e79
 
ff99726
06f5e79
 
 
 
 
 
 
ff99726
 
06f5e79
 
 
 
 
 
 
 
 
 
 
ff99726
 
06f5e79
 
 
 
 
 
 
 
 
 
 
f55ba0b
06f5e79
 
ff99726
06f5e79
 
 
 
f55ba0b
06f5e79
 
 
f55ba0b
 
06f5e79
 
f55ba0b
06f5e79
 
 
 
 
f55ba0b
06f5e79
 
 
 
f55ba0b
06f5e79
 
 
ff99726
06f5e79
 
ff99726
06f5e79
 
 
f55ba0b
 
 
 
 
 
 
 
 
 
 
ff99726
 
06f5e79
ff99726
06f5e79
 
 
 
ff99726
 
06f5e79
 
 
 
 
ff99726
06f5e79
 
 
 
 
f55ba0b
 
06f5e79
 
 
 
 
 
f55ba0b
06f5e79
 
 
 
 
 
f55ba0b
06f5e79
 
 
 
 
f55ba0b
 
 
 
 
 
 
 
 
 
 
 
06f5e79
 
 
 
 
 
 
 
f55ba0b
 
 
 
 
ff99726
 
06f5e79
ff99726
06f5e79
 
 
 
ff99726
 
06f5e79
 
 
 
 
 
f55ba0b
 
 
 
 
 
06f5e79
 
 
 
 
 
f55ba0b
 
 
06f5e79
 
 
 
f55ba0b
06f5e79
 
 
 
ff99726
f55ba0b
 
 
 
 
 
 
 
 
 
 
 
 
06f5e79
 
 
 
 
 
 
 
f55ba0b
 
 
 
 
 
 
 
cff6a7e
f55ba0b
06f5e79
ff99726
06f5e79
 
f55ba0b
06f5e79
ff99726
 
06f5e79
 
f55ba0b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
cff6a7e
f55ba0b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
926e731
f55ba0b
ff99726
f55ba0b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
ed28f2d
f55ba0b
 
 
 
 
 
 
 
ed28f2d
06f5e79
f55ba0b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
ff99726
f55ba0b
 
06f5e79
 
f55ba0b
ff99726
 
 
926e731
ff99726
06f5e79
 
 
ff99726
f55ba0b
926e731
 
f55ba0b
ff99726
f55ba0b
 
 
ff99726
 
 
 
 
 
 
06f5e79
ff99726
06f5e79
 
ff99726
f55ba0b
ff99726
 
f55ba0b
ff99726
926e731
ff99726
 
 
 
 
 
926e731
ff99726
 
 
 
 
 
 
 
 
fa64699
06f5e79
fa64699
f55ba0b
 
 
 
 
fa64699
 
06f5e79
 
 
fa64699
f55ba0b
fa64699
 
 
 
 
ff99726
f55ba0b
 
ff99726
926e731
06f5e79
 
 
 
 
f55ba0b
06f5e79
 
 
ff99726
f55ba0b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
ff99726
f55ba0b
ff99726
 
f55ba0b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
ff99726
f55ba0b
ff99726
f55ba0b
ff99726
f55ba0b
ff99726
 
f55ba0b
 
 
 
 
 
 
 
 
 
ff99726
f55ba0b
ff99726
 
f55ba0b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
ff99726
f55ba0b
 
 
 
 
ff99726
 
f55ba0b
 
 
 
 
 
 
 
 
 
 
 
 
ff99726
 
f55ba0b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
ff99726
 
 
06f5e79
f55ba0b
ff99726
 
 
06f5e79
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
ff99726
 
926e731
06f5e79
926e731
 
ff99726
 
926e731
06f5e79
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
926e731
 
06f5e79
 
 
 
 
 
 
926e731
 
 
ff99726
926e731
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
06f5e79
926e731
 
f55ba0b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
06f5e79
926e731
 
f55ba0b
926e731
 
 
06f5e79
926e731
 
f55ba0b
926e731
 
ed28f2d
06f5e79
ff99726
 
 
 
 
f55ba0b
 
 
 
 
 
ff99726
926e731
 
f55ba0b
926e731
ff99726
926e731
ff99726
f55ba0b
 
 
ed28f2d
f55ba0b
 
 
 
 
 
 
 
 
 
 
fa64699
f55ba0b
 
 
 
 
 
ff99726
f55ba0b
 
ed28f2d
 
f55ba0b
 
 
 
 
 
 
 
926e731
 
f55ba0b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
926e731
f55ba0b
926e731
ff99726
ed28f2d
f55ba0b
 
 
06f5e79
f55ba0b
 
926e731
06f5e79
 
 
 
 
 
 
 
 
 
 
 
 
f55ba0b
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
2374
2375
2376
2377
2378
2379
2380
2381
2382
2383
2384
2385
2386
2387
2388
2389
2390
2391
2392
2393
2394
2395
2396
2397
2398
2399
2400
2401
2402
2403
2404
2405
2406
2407
2408
2409
2410
2411
2412
2413
2414
2415
2416
2417
2418
2419
2420
2421
2422
2423
2424
2425
2426
2427
2428
2429
2430
2431
2432
2433
2434
2435
2436
2437
2438
2439
2440
2441
2442
2443
2444
2445
2446
2447
2448
2449
2450
2451
2452
2453
2454
2455
2456
def create_multipage_app():
    with gr.Blocks(
        theme=gr.themes.Soft(
            primary_hue=gr.themes.colors.blue,
            secondary_hue=gr.themes.colors.green,
            neutral_hue=gr.themes.colors.slate,
        ),
        title="⚡ Power Systems Consultant - Enhanced",
        css=ENHANCED_CSS
    ) as app:
        
        # Global state management
        current_page = gr.State("cover")
        user_state = gr.State(None)
        session_state = gr.State(None)
        
        # Page 1: Cover Page (Always visible initially)
        with gr.Column(visible=True, elem_classes=["cover-page"]) as cover_page:
            with gr.HTML() as cover_content:
                gr.HTML("""
                    <div class="cover-hero">
                        <div class="cover-icon">⚡</div>
                        <h1 class="cover-title">Power Systems Consultant</h1>
                        <p class="cover-subtitle">
                            Advanced AI-powered platform for electrical power systems analysis, fault calculations, 
                            protection design, and engineering excellence. Experience the future of power systems consulting.
                        </p>
                    </div>
                """)
            
            with gr.Row():
                signin_nav_btn = gr.Button("🔐 Sign Inimport gradio as gr
import os
import json
import time
import uuid
from datetime import datetime
from groq import Groq
import pandas as pd
from typing import Dict, List, Tuple, Optional
import re
import hashlib
import sqlite3
import sys

# Add utils directory to path
sys.path.append(os.path.join(os.path.dirname(__file__), 'utils'))

# Import your external utilities
try:
    from utils.diagram_generator import DiagramGenerator as ExternalDiagramGenerator
    from utils.rag_system import RAGSystem
    EXTERNAL_UTILS_AVAILABLE = True
    print("✅ External utilities loaded successfully!")
except ImportError as e:
    print(f"⚠️ External utilities not found: {e}")
    print("   Using internal implementations...")
    EXTERNAL_UTILS_AVAILABLE = False

# Initialize components with better error handling
def get_groq_client():
    api_key = os.getenv("GROQ_API_KEY")
    if not api_key:
        print("⚠️ GROQ_API_KEY not found. Using demo mode.")
        return None
    try:
        return Groq(api_key=api_key)
    except Exception as e:
        print(f"❌ Groq client initialization failed: {e}")
        return None

class UserManager:
    def __init__(self):
        self.db_path = 'users.db'
        self.init_database()
    
    def init_database(self):
        """Initialize SQLite database for users"""
        try:
            conn = sqlite3.connect(self.db_path)
            cursor = conn.cursor()
            
            cursor.execute('''
                CREATE TABLE IF NOT EXISTS users (
                    id INTEGER PRIMARY KEY AUTOINCREMENT,
                    username TEXT UNIQUE NOT NULL,
                    email TEXT UNIQUE NOT NULL,
                    password_hash TEXT NOT NULL,
                    full_name TEXT,
                    created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
                    last_login TIMESTAMP
                )
            ''')
            
            cursor.execute('''
                CREATE TABLE IF NOT EXISTS chat_sessions (
                    id INTEGER PRIMARY KEY AUTOINCREMENT,
                    user_id INTEGER,
                    session_id TEXT UNIQUE,
                    title TEXT,
                    created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
                    updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
                    FOREIGN KEY (user_id) REFERENCES users (id)
                )
            ''')
            
            cursor.execute('''
                CREATE TABLE IF NOT EXISTS messages (
                    id INTEGER PRIMARY KEY AUTOINCREMENT,
                    session_id TEXT,
                    role TEXT,
                    content TEXT,
                    timestamp TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
                    FOREIGN KEY (session_id) REFERENCES chat_sessions (session_id)
                )
            ''')
            
            conn.commit()
            conn.close()
            print("✅ Database initialized successfully")
        except Exception as e:
            print(f"❌ Database initialization failed: {e}")
    
    def hash_password(self, password: str) -> str:
        """Hash password using SHA-256"""
        return hashlib.sha256(password.encode()).hexdigest()
    
    def create_user(self, username: str, email: str, password: str, full_name: str = "") -> Tuple[bool, str]:
        """Create new user account"""
        try:
            conn = sqlite3.connect(self.db_path)
            cursor = conn.cursor()
            
            password_hash = self.hash_password(password)
            cursor.execute(
                "INSERT INTO users (username, email, password_hash, full_name) VALUES (?, ?, ?, ?)",
                (username, email, password_hash, full_name)
            )
            
            conn.commit()
            conn.close()
            return True, "Account created successfully!"
            
        except sqlite3.IntegrityError as e:
            if "username" in str(e):
                return False, "Username already exists"
            elif "email" in str(e):
                return False, "Email already registered"
            else:
                return False, "Registration failed"
        except Exception as e:
            return False, f"Error: {str(e)}"
    
    def authenticate_user(self, username: str, password: str) -> Tuple[bool, Optional[Dict]]:
        """Authenticate user login"""
        try:
            conn = sqlite3.connect(self.db_path)
            cursor = conn.cursor()
            
            password_hash = self.hash_password(password)
            cursor.execute(
                "SELECT id, username, email, full_name FROM users WHERE username = ? AND password_hash = ?",
                (username, password_hash)
            )
            
            user = cursor.fetchone()
            
            if user:
                cursor.execute(
                    "UPDATE users SET last_login = CURRENT_TIMESTAMP WHERE id = ?",
                    (user[0],)
                )
                conn.commit()
                
                user_data = {
                    'id': user[0],
                    'username': user[1],
                    'email': user[2],
                    'full_name': user[3] or user[1]
                }
                conn.close()
                return True, user_data
            
            conn.close()
            return False, None
            
        except Exception as e:
            print(f"Authentication error: {e}")
            return False, None

# Enhanced diagram generator with more diagram types
class InternalDiagramGenerator:
    def generate_single_line_diagram(self, config: Dict) -> str:
        """Generate single line diagram SVG"""
        return f"""
        <svg width="800" height="500" viewBox="0 0 800 500" xmlns="http://www.w3.org/2000/svg">
            <!-- Background -->
            <rect width="800" height="500" fill="#f8fafc" stroke="#cbd5e1" stroke-width="2" rx="10"/>
            <text x="400" y="30" text-anchor="middle" font-family="Arial" font-size="20" font-weight="bold" fill="#1e293b">
                Single Line Diagram - Power System
            </text>
            
            <!-- Generator -->
            <circle cx="100" cy="250" r="40" fill="none" stroke="#3b82f6" stroke-width="3"/>
            <text x="100" y="255" text-anchor="middle" font-family="Arial" font-size="14" font-weight="bold" fill="#3b82f6">G</text>
            <text x="100" y="300" text-anchor="middle" font-family="Arial" font-size="10" fill="#3b82f6">Generator</text>
            
            <!-- Connection to Transformer 1 -->
            <line x1="140" y1="250" x2="200" y2="250" stroke="#1e293b" stroke-width="3"/>
            
            <!-- Transformer 1 -->
            <circle cx="220" cy="230" r="15" fill="none" stroke="#ef4444" stroke-width="2"/>
            <circle cx="220" cy="270" r="15" fill="none" stroke="#ef4444" stroke-width="2"/>
            <line x1="205" y1="250" x2="235" y2="250" stroke="#ef4444" stroke-width="2"/>
            <text x="220" y="205" text-anchor="middle" font-family="Arial" font-size="12" font-weight="bold" fill="#ef4444">T1</text>
            <text x="220" y="300" text-anchor="middle" font-family="Arial" font-size="9" fill="#ef4444">25kV/138kV</text>
            
            <!-- Connection to Bus -->
            <line x1="235" y1="250" x2="320" y2="250" stroke="#1e293b" stroke-width="3"/>
            
            <!-- Main Bus -->
            <rect x="320" y="240" width="160" height="20" fill="none" stroke="#10b981" stroke-width="4"/>
            <text x="400" y="235" text-anchor="middle" font-family="Arial" font-size="12" font-weight="bold" fill="#10b981">138kV Bus</text>
            
            <!-- Transmission Line 1 -->
            <line x1="480" y1="250" x2="550" y2="250" stroke="#1e293b" stroke-width="3"/>
            <line x1="515" y1="235" x2="515" y2="265" stroke="#8b5cf6" stroke-width="2"/>
            <line x1="510" y1="240" x2="520" y2="240" stroke="#8b5cf6" stroke-width="2"/>
            <line x1="510" y1="260" x2="520" y2="260" stroke="#8b5cf6" stroke-width="2"/>
            <text x="515" y="220" text-anchor="middle" font-family="Arial" font-size="10" fill="#8b5cf6">Line 1</text>
            
            <!-- Load Bus -->
            <circle cx="600" cy="250" r="25" fill="none" stroke="#f59e0b" stroke-width="3"/>
            <text x="600" y="255" text-anchor="middle" font-family="Arial" font-size="12" font-weight="bold" fill="#f59e0b">Load</text>
            <text x="600" y="285" text-anchor="middle" font-family="Arial" font-size="9" fill="#f59e0b">Industrial</text>
            
            <!-- Protection Elements -->
            <rect x="170" y="240" width="20" height="20" fill="none" stroke="#dc2626" stroke-width="2"/>
            <text x="180" y="252" text-anchor="middle" font-family="Arial" font-size="8" fill="#dc2626">CB</text>
            
            <rect x="290" y="240" width="20" height="20" fill="none" stroke="#dc2626" stroke-width="2"/>
            <text x="300" y="252" text-anchor="middle" font-family="Arial" font-size="8" fill="#dc2626">CB</text>
            
            <!-- Legend -->
            <rect x="20" y="350" width="200" height="120" fill="white" stroke="#cbd5e1" stroke-width="1" rx="5"/>
            <text x="120" y="370" text-anchor="middle" font-family="Arial" font-size="12" font-weight="bold" fill="#1e293b">Legend</text>
            
            <circle cx="40" cy="390" r="8" fill="none" stroke="#3b82f6" stroke-width="2"/>
            <text x="55" y="395" font-family="Arial" font-size="10" fill="#1e293b">Generator</text>
            
            <rect x="32" y="405" width="16" height="10" fill="none" stroke="#dc2626" stroke-width="1"/>
            <text x="55" y="413" font-family="Arial" font-size="10" fill="#1e293b">Circuit Breaker</text>
            
            <circle cx="35" cy="430" r="5" fill="none" stroke="#ef4444" stroke-width="1"/>
            <circle cx="45" cy="430" r="5" fill="none" stroke="#ef4444" stroke-width="1"/>
            <text x="55" y="435" font-family="Arial" font-size="10" fill="#1e293b">Transformer</text>
            
            <rect x="32" y="445" width="16" height="8" fill="none" stroke="#10b981" stroke-width="2"/>
            <text x="55" y="452" font-family="Arial" font-size="10" fill="#1e293b">Bus</text>
        </svg>
        """
    
    def generate_protection_diagram(self, config: Dict) -> str:
        """Generate protection scheme diagram"""
        return """
        <svg width="900" height="600" viewBox="0 0 900 600" xmlns="http://www.w3.org/2000/svg">
            <!-- Background -->
            <rect width="900" height="600" fill="#f8fafc" stroke="#cbd5e1" stroke-width="2" rx="10"/>
            <text x="450" y="30" text-anchor="middle" font-family="Arial" font-size="20" font-weight="bold" fill="#1e293b">
                Power System Protection Scheme
            </text>
            
            <!-- Generator and Protection -->
            <g transform="translate(50,100)">
                <!-- Generator -->
                <circle cx="60" cy="100" r="40" fill="none" stroke="#3b82f6" stroke-width="3"/>
                <text x="60" y="105" text-anchor="middle" font-family="Arial" font-size="14" fill="#3b82f6">G</text>
                
                <!-- Generator Protection -->
                <rect x="40" y="50" width="40" height="25" fill="#fef3c7" stroke="#f59e0b" stroke-width="2" rx="3"/>
                <text x="60" y="67" text-anchor="middle" font-family="Arial" font-size="9" fill="#92400e">87G</text>
                
                <!-- Overcurrent Relay -->
                <rect x="110" y="85" width="30" height="30" fill="#ddd6fe" stroke="#8b5cf6" stroke-width="2" rx="3"/>
                <text x="125" y="105" text-anchor="middle" font-family="Arial" font-size="10" fill="#6b46c1">51</text>
                
                <!-- Circuit Breaker -->
                <rect x="150" y="90" width="20" height="20" fill="none" stroke="#dc2626" stroke-width="3"/>
                <line x1="155" y1="95" x2="165" y2="105" stroke="#dc2626" stroke-width="2"/>
                <text x="160" y="125" text-anchor="middle" font-family="Arial" font-size="8" fill="#dc2626">52G</text>
            </g>
            
            <!-- Transformer and Protection -->
            <g transform="translate(300,100)">
                <!-- Transformer -->
                <circle cx="40" cy="80" r="20" fill="none" stroke="#ef4444" stroke-width="2"/>
                <circle cx="40" cy="120" r="20" fill="none" stroke="#ef4444" stroke-width="2"/>
                <text x="40" y="65" text-anchor="middle" font-family="Arial" font-size="12" fill="#ef4444">T1</text>
                
                <!-- Differential Protection -->
                <rect x="15" y="40" width="50" height="20" fill="#fef3c7" stroke="#f59e0b" stroke-width="2" rx="3"/>
                <text x="40" y="53" text-anchor="middle" font-family="Arial" font-size="10" fill="#92400e">87T</text>
                
                <!-- Buchholz Relay -->
                <rect x="70" y="90" width="35" height="20" fill="#dcfce7" stroke="#16a34a" stroke-width="2" rx="3"/>
                <text x="87" y="103" text-anchor="middle" font-family="Arial" font-size="9" fill="#15803d">63</text>
                
                <!-- OLTC Protection -->
                <rect x="15" y="150" width="50" height="20" fill="#dbeafe" stroke="#2563eb" stroke-width="2" rx="3"/>
                <text x="40" y="163" text-anchor="middle" font-family="Arial" font-size="9" fill="#1d4ed8">OLTC</text>
            </g>
            
            <!-- Transmission Line and Protection -->
            <g transform="translate(450,100)">
                <!-- Line -->
                <line x1="0" y1="100" x2="150" y2="100" stroke="#1e293b" stroke-width="3"/>
                <line x1="75" y1="85" x2="75" y2="115" stroke="#8b5cf6" stroke-width="2"/>
                
                <!-- Distance Protection -->
                <rect x="30" y="60" width="30" height="25" fill="#fde68a" stroke="#f59e0b" stroke-width="2" rx="3"/>
                <text x="45" y="77" text-anchor="middle" font-family="Arial" font-size="10" fill="#92400e">21</text>
                
                <!-- Pilot Protection -->
                <rect x="90" y="60" width="30" height="25" fill="#fecaca" stroke="#ef4444" stroke-width="2" rx="3"/>
                <text x="105" y="77" text-anchor="middle" font-family="Arial" font-size="10" fill="#dc2626">85</text>
                
                <!-- Communication -->
                <path d="M 45 60 L 105 60" stroke="#6b7280" stroke-width="1" stroke-dasharray="3,3"/>
                <text x="75" y="45" text-anchor="middle" font-family="Arial" font-size="8" fill="#6b7280">Pilot Wire</text>
            </g>
            
            <!-- Bus Protection -->
            <g transform="translate(650,100)">
                <!-- Bus -->
                <rect x="0" y="90" width="100" height="20" fill="none" stroke="#10b981" stroke-width="4"/>
                <text x="50" y="85" text-anchor="middle" font-family="Arial" font-size="12" fill="#10b981">Main Bus</text>
                
                <!-- Bus Differential -->
                <rect x="25" y="50" width="50" height="25" fill="#fef3c7" stroke="#f59e0b" stroke-width="2" rx="3"/>
                <text x="50" y="67" text-anchor="middle" font-family="Arial" font-size="10" fill="#92400e">87B</text>
                
                <!-- Bus Protection -->
                <rect x="25" y="130" width="50" height="25" fill="#e0e7ff" stroke="#6366f1" stroke-width="2" rx="3"/>
                <text x="50" y="147" text-anchor="middle" font-family="Arial" font-size="10" fill="#4f46e5">50BF</text>
            </g>
            
            <!-- Protection Coordination Chart -->
            <g transform="translate(50,350)">
                <rect x="0" y="0" width="400" height="200" fill="white" stroke="#cbd5e1" stroke-width="2" rx="5"/>
                <text x="200" y="20" text-anchor="middle" font-family="Arial" font-size="14" font-weight="bold" fill="#1e293b">
                    Time-Current Coordination
                </text>
                
                <!-- Axes -->
                <line x1="50" y1="180" x2="350" y2="180" stroke="#374151" stroke-width="2"/>
                <line x1="50" y1="50" x2="50" y2="180" stroke="#374151" stroke-width="2"/>
                
                <!-- Labels -->
                <text x="200" y="200" text-anchor="middle" font-family="Arial" font-size="10" fill="#374151">Current (A)</text>
                <text x="20" y="115" text-anchor="middle" font-family="Arial" font-size="10" fill="#374151" transform="rotate(-90, 20, 115)">Time (s)</text>
                
                <!-- Curves -->
                <path d="M 70 160 Q 150 120 250 80 Q 300 60 330 50" stroke="#ef4444" stroke-width="2" fill="none"/>
                <text x="270" y="70" font-family="Arial" font-size="8" fill="#ef4444">Generator (51G)</text>
                
                <path d="M 90 170 Q 180 130 280 90 Q 320 70 340 60" stroke="#8b5cf6" stroke-width="2" fill="none"/>
                <text x="290" y="90" font-family="Arial" font-size="8" fill="#8b5cf6">Feeder (51F)</text>
            </g>
            
            <!-- Device Legend -->
            <rect x="500" y="350" width="350" height="200" fill="white" stroke="#cbd5e1" stroke-width="2" rx="5"/>
            <text x="675" y="370" text-anchor="middle" font-family="Arial" font-size="14" font-weight="bold" fill="#1e293b">
                IEEE Device Numbers
            </text>
            
            <g transform="translate(520,380)">
                <text x="0" y="15" font-family="Arial" font-size="11" font-weight="bold" fill="#ef4444">21 - Distance Relay</text>
                <text x="0" y="35" font-family="Arial" font-size="11" font-weight="bold" fill="#8b5cf6">51 - AC Time Overcurrent</text>
                <text x="0" y="55" font-family="Arial" font-size="11" font-weight="bold" fill="#dc2626">52 - AC Circuit Breaker</text>
                <text x="0" y="75" font-family="Arial" font-size="11" font-weight="bold" fill="#16a34a">63 - Pressure Switch</text>
                <text x="0" y="95" font-family="Arial" font-size="11" font-weight="bold" fill="#ef4444">85 - Carrier/Pilot Relay</text>
                
                <text x="170" y="15" font-family="Arial" font-size="11" font-weight="bold" fill="#f59e0b">87 - Differential Relay</text>
                <text x="170" y="35" font-family="Arial" font-size="11" font-weight="bold" fill="#f59e0b">87G - Generator Differential</text>
                <text x="170" y="55" font-family="Arial" font-size="11" font-weight="bold" fill="#f59e0b">87T - Transformer Differential</text>
                <text x="170" y="75" font-family="Arial" font-size="11" font-weight="bold" fill="#f59e0b">87B - Bus Differential</text>
                <text x="170" y="95" font-family="Arial" font-size="11" font-weight="bold" fill="#6366f1">50BF - Breaker Failure</text>
            </g>
            
            <text x="450" y="580" text-anchor="middle" font-family="Arial" font-size="10" fill="#64748b">
                Comprehensive protection scheme with coordination and backup protection
            </text>
        </svg>
        """

class PowerSystemsConsultant:
    def __init__(self):
        self.groq_client = get_groq_client()
        
        # Initialize diagram generator (external or internal)
        if EXTERNAL_UTILS_AVAILABLE:
            try:
                self.diagram_generator = ExternalDiagramGenerator()
                print("✅ Using external DiagramGenerator")
            except Exception as e:
                print(f"⚠️ External DiagramGenerator failed: {e}")
                self.diagram_generator = InternalDiagramGenerator()
                print("   Falling back to internal DiagramGenerator")
        else:
            self.diagram_generator = InternalDiagramGenerator()
            print("📐 Using internal DiagramGenerator")
        
        # Initialize RAG system if available
        if EXTERNAL_UTILS_AVAILABLE:
            try:
                self.rag_system = RAGSystem()
                print("✅ RAG System initialized successfully")
                self.has_rag = True
            except Exception as e:
                print(f"⚠️ RAG System initialization failed: {e}")
                print("   Continuing without RAG capabilities")
                self.rag_system = None
                self.has_rag = False
        else:
            self.rag_system = None
            self.has_rag = False
            print("📚 RAG System not available")
        
        try:
            self.user_manager = UserManager()
        except Exception as e:
            print(f"❌ UserManager initialization failed: {e}")
            self.user_manager = None
        
        self.current_user = None
        self.current_session = None
    
    def generate_demo_response(self, user_query: str, chat_history: List[Tuple[str, str]]) -> Tuple[str, str]:
        """Generate demo response when Groq API is not available"""
        demo_responses = {
            "fault": "**Fault Analysis Demo Response:**\n\nFor a three-phase fault, the fault current is calculated as:\n\n`I_fault = V_nominal / Z_total`\n\nWhere:\n- V_nominal is the system nominal voltage\n- Z_total is the total impedance to the fault point\n\nThis includes positive sequence impedance of generators, transformers, and lines up to the fault location.",
            "protection": "**Protection System Demo Response:**\n\nPower system protection involves multiple layers:\n\n1. **Primary Protection**: Fastest, most selective (e.g., differential relays)\n2. **Backup Protection**: Slower but covers larger area (e.g., overcurrent relays)\n3. **Emergency Protection**: Last resort (e.g., under-frequency load shedding)\n\nCoordination ensures proper sequence and selectivity between protection devices.",
            "transformer": "**Transformer Demo Response:**\n\nTransformer protection typically includes:\n\n- **87T**: Differential protection (primary)\n- **51**: Overcurrent protection (backup)\n- **63**: Buchholz relay (gas accumulation)\n- **26**: Thermal protection\n- **71**: Gas density relay (SF6)\n\nThe transformer equivalent circuit uses T or π models for analysis.",
            "default": "**Power Systems AI Consultant (Demo Mode):**\n\nI can help with fault analysis, protection systems, load flow studies, stability analysis, and more. This is demo mode - please set GROQ_API_KEY for full functionality.\n\n**Common Topics:**\n- Short circuit calculations\n- Protection coordination\n- Power quality analysis\n- Equipment sizing\n- Standards interpretation"
        }
        
        # Simple keyword matching for demo
        query_lower = user_query.lower()
        if any(word in query_lower for word in ['fault', 'short circuit']):
            response = demo_responses["fault"]
        elif any(word in query_lower for word in ['protection', 'relay', 'coordination']):
            response = demo_responses["protection"]
        elif any(word in query_lower for word in ['transformer', 'differential']):
            response = demo_responses["transformer"]
        else:
            response = demo_responses["default"]
        
        # Check for diagram request
        diagram_svg = None
        if any(keyword in query_lower for keyword in ['diagram', 'single line', 'drawing', 'circuit', 'protection scheme']):
            try:
                if 'protection' in query_lower:
                    diagram_svg = self.diagram_generator.generate_protection_diagram({})
                else:
                    diagram_svg = self.diagram_generator.generate_single_line_diagram({})
            except Exception as e:
                print(f"Demo diagram generation error: {e}")
        
        return response, diagram_svg
    
    def generate_response(self, user_query: str, chat_history: List[Tuple[str, str]], session_id: str = None) -> Tuple[str, str]:
        """Generate response using Groq LLM with optional RAG enhancement"""
        # Use demo mode if Groq client is not available
        if not self.groq_client:
            return self.generate_demo_response(user_query, chat_history)
        
        try:
            # Check if query is asking for a diagram
            diagram_svg = None
            diagram_requested = any(keyword in user_query.lower() for keyword in 
                                  ['diagram', 'single line', 'drawing', 'circuit', 'protection scheme'])
            
            if diagram_requested:
                try:
                    # Try different diagram types based on query
                    if 'protection' in user_query.lower():
                        if hasattr(self.diagram_generator, 'generate_protection_diagram'):
                            diagram_svg = self.diagram_generator.generate_protection_diagram({})
                        else:
                            diagram_svg = self.diagram_generator.generate_single_line_diagram({})
                    else:
                        diagram_svg = self.diagram_generator.generate_single_line_diagram({})
                except Exception as e:
                    print(f"Diagram generation error: {e}")
                    diagram_svg = None
            
            # Enhanced system prompt
            system_prompt = """You are a Power Systems Expert AI assistant specializing in electrical power systems. 
            You help with fault analysis, protection systems, standards interpretation, and engineering calculations.
            Provide clear, technical explanations with practical examples and safety considerations.
            
            When users request diagrams, explain that diagrams are being generated separately.
            Focus on providing detailed technical explanations alongside visual representations."""
            
            # Use RAG for enhanced context if available
            if self.has_rag and self.rag_system:
                try:
                    # Get relevant context from RAG system
                    relevant_docs = self.rag_system.query(user_query, top_k=3)
                    if relevant_docs:
                        context = "\n".join([doc['content'] for doc in relevant_docs])
                        system_prompt += f"\n\nRelevant technical context:\n{context}"
                except Exception as e:
                    print(f"RAG query error: {e}")
            
            # Prepare conversation context
            messages = [{"role": "system", "content": system_prompt}]
            
            # Add chat history (last 5 exchanges)
            for human_msg, ai_msg in chat_history[-5:]:
                if human_msg and ai_msg:  # Ensure messages are not None
                    messages.append({"role": "user", "content": human_msg})
                    messages.append({"role": "assistant", "content": ai_msg})
            
            # Add current query
            messages.append({"role": "user", "content": user_query})
            
            # Generate response using Groq
            response = self.groq_client.chat.completions.create(
                model="llama3-70b-8192",
                messages=messages,
                max_tokens=2000,
                temperature=0.8
            )
            
            text_response = response.choices[0].message.content
            
            # Add RAG attribution if used
            if self.has_rag and any(keyword in user_query.lower() for keyword in ['standard', 'code', 'regulation']):
                text_response += "\n\n*Enhanced with technical documentation database*"
            
            return text_response, diagram_svg
            
        except Exception as e:
            error_msg = f"Error generating response: {str(e)}. Falling back to demo mode."
            print(error_msg)
            return self.generate_demo_response(user_query, chat_history)
    
    def generate_practice_pack(self, topic: str, difficulty: str, num_questions: int) -> str:
        """Generate practice questions pack with optional RAG enhancement"""
        if not self.groq_client:
            return self.generate_demo_practice_pack(topic, difficulty, num_questions)
        
        try:
            # Enhanced prompt with RAG context if available
            practice_prompt = f"""Generate {num_questions} power systems practice questions about {topic} at {difficulty} level.
            Format with numbered questions, multiple choice options, and detailed solutions."""
            
            # Add RAG context for practice questions if available
            if self.has_rag and self.rag_system:
                try:
                    topic_docs = self.rag_system.query(f"{topic} practice questions examples", top_k=2)
                    if topic_docs:
                        context = "\n".join([doc['content'][:500] for doc in topic_docs])
                        practice_prompt += f"\n\nReference context:\n{context}"
                except Exception as e:
                    print(f"RAG practice enhancement error: {e}")
            
            messages = [
                {"role": "system", "content": "You are an expert power systems engineer creating practice questions."},
                {"role": "user", "content": practice_prompt}
            ]
            
            response = self.groq_client.chat.completions.create(
                model="llama3-70b-8192",
                messages=messages,
                max_tokens=3000,
                temperature=0.7
            )
            
            result = response.choices[0].message.content
            
            if self.has_rag:
                result += "\n\n*Practice questions enhanced with technical documentation*"
            
            return result
            
        except Exception as e:
            print(f"Practice generation error: {e}")
            return self.generate_demo_practice_pack(topic, difficulty, num_questions)
    
    def generate_demo_practice_pack(self, topic: str, difficulty: str, num_questions: int) -> str:
        """Generate demo practice questions when API is not available"""
        demo_questions = {
            "Fault Analysis": f"""
# {topic} Practice Pack - {difficulty} Level (Demo Mode)

## Question 1: Three-Phase Fault Analysis
A three-phase fault occurs at a bus with the following system data:
- System voltage: 138 kV
- Generator reactance: j0.15 pu
- Transformer reactance: j0.10 pu
- Line reactance: j0.08 pu

**Calculate the fault current in amperes.**

**Options:**
A) 12,500 A
B) 15,240 A  
C) 18,960 A
D) 21,340 A

**Solution:**
Total reactance = 0.15 + 0.10 + 0.08 = 0.33 pu
Base current = 100 MVA / (√3 × 138 kV) = 418.4 A
Fault current = 418.4 / 0.33 = 12,677 A ≈ **12,500 A (Answer: A)**

---

## Question 2: Symmetrical Components
For an unbalanced system with line currents:
- Ia = 1000° A
- Ib = 80∠-130° A  
- Ic = 60110° A

**Calculate the positive sequence current I₁.**

**Solution:**
I₁ = (1/3)[Ia + a×Ib + a²×Ic]
Where a = 1120°

This is a demo version. Set GROQ_API_KEY for complete practice packs.
            """,
            "Protection Systems": f"""
# {topic} Practice Pack - {difficulty} Level (Demo Mode)

## Question 1: Overcurrent Relay Coordination
Two overcurrent relays are installed in series:
- Relay A: CT ratio 800:5, Time dial 0.5
- Relay B: CT ratio 400:5, Time dial 0.3
- Coordination time interval: 0.3 seconds

**For a fault current of 6000 A, calculate the operating time of each relay.**

**Solution:**
Primary current through Relay A = 6000 A
Secondary current = 6000 × (5/800) = 37.5 A
Using standard inverse curve equation...

This is a demo version. Set GROQ_API_KEY for complete solutions.

---

## Question 2: Distance Relay Zones
A 100-mile transmission line has the following protection zones:
- Zone 1: 80% of line
- Zone 2: 120% of line + 50% of next line
- Zone 3: 200% of line

**Calculate the reach settings for each zone if line impedance is 0.8 Ω/mile.**

This is a demo version with {num_questions} questions requested.
            """
        }
        
        return demo_questions.get(topic, f"""
# {topic} Practice Pack - {difficulty} Level (Demo Mode)

## Demo Content
This is a demo version of the practice pack generator.

**Topics Available:**
- Fault Analysis
- Protection Systems  
- Power Flow Studies
- Stability Analysis
- Harmonics & Power Quality

**To unlock full functionality:**
1. Set your GROQ_API_KEY environment variable
2. Restart the application
3. Generate comprehensive practice packs with detailed solutions

**Demo Features:**
- {num_questions} questions requested
- {difficulty} difficulty level
- Topic: {topic}

Set GROQ_API_KEY for complete practice generation!
        """)
    
    def explain_standard(self, standard: str) -> str:
        """Explain power systems standard with RAG enhancement"""
        if not self.groq_client:
            return self.generate_demo_standard_explanation(standard)
        
        try:
            standard_prompt = f"""Provide a comprehensive explanation of the {standard} standard including 
            purpose, key requirements, practical applications, and implementation considerations."""
            
            # Enhanced with RAG if available
            if self.has_rag and self.rag_system:
                try:
                    standard_docs = self.rag_system.query(f"{standard} standard explanation", top_k=3)
                    if standard_docs:
                        context = "\n".join([doc['content'] for doc in standard_docs])
                        standard_prompt += f"\n\nTechnical documentation context:\n{context}"
                except Exception as e:
                    print(f"RAG standard enhancement error: {e}")
            
            messages = [
                {"role": "system", "content": "You are an expert in power systems standards."},
                {"role": "user", "content": standard_prompt}
            ]
            
            response = self.groq_client.chat.completions.create(
                model="llama3-70b-8192",
                messages=messages,
                max_tokens=2500,
                temperature=0.6
            )
            
            result = response.choices[0].message.content
            
            if self.has_rag:
                result += "\n\n*Enhanced with official standards documentation*"
            
            return result
            
        except Exception as e:
            print(f"Standard explanation error: {e}")
            return self.generate_demo_standard_explanation(standard)
    
    def generate_demo_standard_explanation(self, standard: str) -> str:
        """Generate demo standard explanation when API is not available"""
        demo_standards = {
            "IEEE C37.2 - Device Function Numbers": """
# IEEE C37.2 - Device Function Numbers (Demo Mode)

## Purpose
IEEE C37.2 defines standard device function numbers and contact designations for power system devices. This standard provides a universal numbering system for protective and control devices.

## Key Device Numbers

### Protection Devices
- **21** - Distance Relay
- **50** - Instantaneous Overcurrent
- **51** - Time Overcurrent  
- **67** - Directional Overcurrent
- **87** - Differential Relay

### Control & Monitoring
- **25** - Synchronizing Check
- **27** - Undervoltage Relay
- **59** - Overvoltage Relay
- **81** - Frequency Relay

### Circuit Breakers & Switches
- **52** - AC Circuit Breaker
- **89** - Line Switch
- **94** - Tripping Relay

## Practical Applications
- Standardized protection schemes
- Universal documentation
- Simplified maintenance
- International compatibility

**This is demo mode. Set GROQ_API_KEY for comprehensive standard explanations.**
            """,
            "IEEE 1547 - Distributed Generation": """
# IEEE 1547 - Distributed Generation (Demo Mode)

## Purpose
IEEE 1547 establishes criteria and requirements for interconnection of distributed resources (DR) with electric power systems (EPS).

## Key Requirements
### Voltage Regulation
- Voltage ride-through capabilities
- Power factor requirements
- Harmonic limits

### Protection Requirements  
- Anti-islanding protection
- Overvoltage/undervoltage protection
- Frequency protection

### Power Quality
- Total harmonic distortion limits
- Flicker requirements
- DC injection limits

**This is demo mode. Set GROQ_API_KEY for detailed standard analysis.**
            """
        }
        
        return demo_standards.get(standard, f"""
# {standard} (Demo Mode)

## Standard Overview
This standard covers important aspects of power system design, operation, or protection.

## Demo Content Available
The full explanation of {standard} would include:

- **Purpose & Scope**: Why this standard exists
- **Key Requirements**: Technical specifications
- **Applications**: Where and how it's used
- **Implementation**: Practical guidance
- **Updates**: Recent revisions and changes

## Access Full Content
Set GROQ_API_KEY environment variable to access:
- Comprehensive explanations
- Detailed technical requirements
- Implementation examples
- Related standards references
- Practical applications

**Demo mode provides limited information.**
        """)
    
    def create_chat_session(self, user_id: int, title: str = None) -> str:
        """Create new chat session"""
        session_id = str(uuid.uuid4())
        
        if not self.user_manager:
            return session_id
        
        try:
            conn = sqlite3.connect(self.user_manager.db_path)
            cursor = conn.cursor()
            
            cursor.execute(
                "INSERT INTO chat_sessions (user_id, session_id, title) VALUES (?, ?, ?)",
                (user_id, session_id, title or f"Chat {datetime.now().strftime('%m/%d %H:%M')}")
            )
            
            conn.commit()
            conn.close()
            
            return session_id
            
        except Exception as e:
            print(f"Session creation error: {e}")
            return session_id

# Initialize the consultant with enhanced error handling
try:
    consultant = PowerSystemsConsultant()
    if EXTERNAL_UTILS_AVAILABLE:
        if consultant.groq_client:
            initialization_status = "✅ Power Systems Consultant initialized with full functionality!"
        else:
            initialization_status = "⚠️ Power Systems Consultant initialized in demo mode (no GROQ_API_KEY)"
    else:
        initialization_status = "✅ Power Systems Consultant initialized with internal utilities!"
except Exception as e:
    consultant = None
    initialization_status = f"❌ Initialization failed: {str(e)}"

# Enhanced CSS with better responsiveness and accessibility
ENHANCED_CSS = """
@import url('https://fonts.googleapis.com/css2?family=Inter:wght@300;400;500;600;700;800&display=swap');

* {
    font-family: 'Inter', -apple-system, BlinkMacSystemFont, sans-serif;
    box-sizing: border-box;
}

:root {
    --primary: #3b82f6;
    --primary-light: #60a5fa;
    --primary-dark: #2563eb;
    --secondary: #10b981;
    --secondary-light: #34d399;
    --accent: #8b5cf6;
    --warning: #f59e0b;
    --danger: #ef4444;
    --success: #10b981;
    
    --bg-primary: #f8fafc;
    --bg-secondary: #f1f5f9;
    --bg-tertiary: #e2e8f0;
    --bg-card: rgba(255, 255, 255, 0.95);
    --bg-glass: rgba(255, 255, 255, 0.8);
    
    --text-primary: #0f172a;
    --text-secondary: #475569;
    --text-muted: #64748b;
    
    --border: rgba(148, 163, 184, 0.3);
    --border-light: rgba(148, 163, 184, 0.2);
    
    --shadow-sm: 0 1px 2px 0 rgba(0, 0, 0, 0.05);
    --shadow: 0 4px 6px -1px rgba(0, 0, 0, 0.1);
    --shadow-lg: 0 10px 15px -3px rgba(0, 0, 0, 0.1);
    --shadow-xl: 0 20px 25px -5px rgba(0, 0, 0, 0.1);
    --shadow-2xl: 0 25px 50px -12px rgba(0, 0, 0, 0.25);
}

/* Enhanced accessibility */
*:focus {
    outline: 2px solid var(--primary);
    outline-offset: 2px;
}

.gradio-container {
    background: linear-gradient(135deg, var(--bg-primary) 0%, var(--bg-secondary) 50%, var(--bg-tertiary) 100%);
    color: var(--text-primary);
    min-height: 100vh;
    padding: 0;
}

/* Cover page with fixed positioning */
.cover-page {
    min-height: 100vh;
    background: linear-gradient(135deg, 
        #f8fafc 0%, 
        #e2e8f0 30%,
        #cbd5e1 60%,
        #94a3b8 100%);
    display: flex !important;
    flex-direction: column !important;
    align-items: center !important;
    justify-content: center !important;
    text-align: center;
    padding: 2rem;
    position: relative;
    overflow: hidden;
}

.cover-page::before {
    content: '';
    position: absolute;
    top: 0;
    left: 0;
    right: 0;
    bottom: 0;
    background: 
        radial-gradient(circle at 20% 80%, rgba(59, 130, 246, 0.1) 0%, transparent 50%),
        radial-gradient(circle at 80% 20%, rgba(139, 92, 246, 0.1) 0%, transparent 50%),
        radial-gradient(circle at 40% 40%, rgba(16, 185, 129, 0.05) 0%, transparent 50%);
    pointer-events: none;
}

.cover-hero {
    z-index: 10;
    max-width: 800px;
    margin-bottom: 4rem;
    position: relative;
}

.cover-icon {
    font-size: 6rem;
    background: linear-gradient(135deg, var(--primary), var(--accent));
    -webkit-background-clip: text;
    -webkit-text-fill-color: transparent;
    margin-bottom: 2rem;
    display: block;
    line-height: 1;
    animation: pulse 2s ease-in-out infinite alternate;
}

@keyframes pulse {
    0% { transform: scale(1); }
    100% { transform: scale(1.05); }
}

.cover-title {
    font-size: clamp(2.5rem, 6vw, 4.5rem);
    font-weight: 800;
    background: linear-gradient(135deg, var(--text-primary), var(--text-secondary));
    -webkit-background-clip: text;
    -webkit-text-fill-color: transparent;
    margin-bottom: 1.5rem;
    line-height: 1.1;
    position: relative;
}

.cover-subtitle {
    font-size: clamp(1rem, 2.5vw, 1.25rem);
    color: var(--text-secondary);
    margin-bottom: 3rem;
    line-height: 1.6;
    max-width: 600px;
    margin-left: auto;
    margin-right: auto;
}

.cover-buttons {
    display: flex;
    gap: 2rem;
    flex-wrap: wrap;
    justify-content: center;
    z-index: 10;
    position: relative;
}

.cover-btn {
    padding: 1.25rem 3rem !important;
    font-size: 1.1rem !important;
    font-weight: 600 !important;
    border-radius: 50px !important;
    border: none !important;
    cursor: pointer !important;
    transition: all 0.3s ease !important;
    text-transform: uppercase !important;
    letter-spacing: 0.5px !important;
    position: relative !important;
    overflow: hidden !important;
    min-width: 200px !important;
    text-decoration: none !important;
    display: inline-flex !important;
    align-items: center !important;
    justify-content: center !important;
    gap: 0.5rem !important;
}

.cover-btn-primary {
    background: linear-gradient(135deg, var(--primary), var(--primary-light)) !important;
    color: white !important;
    box-shadow: var(--shadow-lg) !important;
}

.cover-btn-secondary {
    background: linear-gradient(135deg, var(--secondary), var(--secondary-light)) !important;
    color: white !important;
    box-shadow: var(--shadow-lg) !important;
}

.cover-btn:hover {
    transform: translateY(-3px) scale(1.05) !important;
    box-shadow: var(--shadow-xl) !important;
}

.cover-btn:active {
    transform: translateY(-1px) scale(1.02) !important;
}

/* Authentication Pages */
.auth-page {
    min-height: 100vh;
    background: linear-gradient(135deg, #f8fafc, #e2e8f0);
    display: flex;
    align-items: center;
    justify-content: center;
    padding: 2rem;
}

.auth-container {
    background: var(--bg-card);
    backdrop-filter: blur(20px);
    border: 1px solid var(--border);
    border-radius: 24px;
    padding: 3rem;
    width: 100%;
    max-width: 450px;
    box-shadow: var(--shadow-2xl);
}

.auth-header {
    text-align: center;
    margin-bottom: 2.5rem;
}

.auth-icon {
    font-size: 3rem;
    background: linear-gradient(135deg, var(--primary), var(--accent));
    -webkit-background-clip: text;
    -webkit-text-fill-color: transparent;
    margin-bottom: 1rem;
}

.auth-title {
    font-size: 2rem;
    font-weight: 700;
    color: var(--text-primary);
    margin-bottom: 0.5rem;
}

.auth-subtitle {
    color: var(--text-secondary);
    font-size: 0.95rem;
}

/* Services Page */
.services-page {
    min-height: 100vh;
    background: linear-gradient(135deg, #f8fafc, #e2e8f0, #cbd5e1);
    padding: 2rem;
}

.services-header {
    text-align: center;
    margin-bottom: 4rem;
    max-width: 800px;
    margin-left: auto;
    margin-right: auto;
}

.services-title {
    font-size: clamp(2.5rem, 6vw, 4rem);
    font-weight: 800;
    background: linear-gradient(135deg, var(--text-primary), var(--text-secondary));
    -webkit-background-clip: text;
    -webkit-text-fill-color: transparent;
    margin-bottom: 1rem;
}

.services-subtitle {
    font-size: 1.2rem;
    color: var(--text-secondary);
    margin-bottom: 2rem;
}

.user-welcome {
    background: var(--bg-card);
    border: 1px solid var(--border);
    border-radius: 16px;
    padding: 1.5rem;
    margin-bottom: 2rem;
    text-align: center;
    backdrop-filter: blur(20px);
    animation: fadeIn 0.5s ease-out;
}

@keyframes fadeIn {
    0% { opacity: 0; transform: translateY(20px); }
    100% { opacity: 1; transform: translateY(0); }
}

.services-grid {
    display: grid;
    grid-template-columns: repeat(auto-fit, minmax(300px, 1fr));
    gap: 2rem;
    max-width: 1200px;
    margin: 0 auto;
}

.service-card {
    background: var(--bg-card);
    backdrop-filter: blur(20px);
    border: 1px solid var(--border);
    border-radius: 20px;
    padding: 2.5rem;
    text-align: center;
    transition: all 0.4s ease;
    cursor: pointer;
    position: relative;
    overflow: hidden;
    animation: slideIn 0.6s ease-out;
}

@keyframes slideIn {
    0% { opacity: 0; transform: translateY(30px); }
    100% { opacity: 1; transform: translateY(0); }
}

.service-card::before {
    content: '';
    position: absolute;
    top: 0;
    left: 0;
    right: 0;
    height: 4px;
    background: linear-gradient(135deg, var(--primary), var(--accent));
    transform: scaleX(0);
    transition: transform 0.3s ease;
}

.service-card:hover::before {
    transform: scaleX(1);
}

.service-card:hover {
    transform: translateY(-8px);
    border-color: var(--primary);
    box-shadow: var(--shadow-2xl);
}

.service-icon {
    font-size: 4rem;
    background: linear-gradient(135deg, var(--primary), var(--accent));
    -webkit-background-clip: text;
    -webkit-text-fill-color: transparent;
    margin-bottom: 1.5rem;
    display: block;
    transition: transform 0.3s ease;
}

.service-card:hover .service-icon {
    transform: scale(1.1) rotate(5deg);
}

.service-title {
    font-size: 1.5rem;
    font-weight: 700;
    color: var(--text-primary);
    margin-bottom: 1rem;
}

.service-description {
    color: var(--text-secondary);
    line-height: 1.6;
    margin-bottom: 2rem;
}

.service-button {
    background: linear-gradient(135deg, var(--primary), var(--primary-light)) !important;
    border: none !important;
    border-radius: 50px !important;
    padding: 0.875rem 2rem !important;
    font-weight: 600 !important;
    color: white !important;
    text-transform: uppercase !important;
    letter-spacing: 0.5px !important;
    transition: all 0.3s ease !important;
    cursor: pointer !important;
}

.service-button:hover {
    transform: translateY(-2px) !important;
    box-shadow: var(--shadow-lg) !important;
}

/* Chat Interface - Dark section */
.dark-section {
    --bg-primary: #0f172a;
    --bg-secondary: #1e293b;
    --bg-tertiary: #334155;
    --bg-card: rgba(30, 41, 59, 0.9);
    --text-primary: #f8fafc;
    --text-secondary: #cbd5e1;
    --text-muted: #94a3b8;
    --border: rgba(255, 255, 255, 0.1);
}

.dark-section .chatbot-page {
    background: linear-gradient(135deg, var(--bg-primary), var(--bg-secondary));
    min-height: 100vh;
    display: flex;
    flex-direction: column;
}

.dark-section .chatbot-header {
    background: var(--bg-card);
    backdrop-filter: blur(20px);
    border-bottom: 1px solid var(--border);
    padding: 1.5rem 2rem;
    color: var(--text-primary);
}

/* Enhanced Form Inputs */
.gradio-textbox input, .gradio-textbox textarea {
    background: rgba(255, 255, 255, 0.95) !important;
    border: 2px solid var(--border) !important;
    border-radius: 12px !important;
    padding: 1rem !important;
    color: var(--text-primary) !important;
    font-size: 1rem !important;
    transition: all 0.3s ease !important;
}

.gradio-textbox input:focus, .gradio-textbox textarea:focus {
    border-color: var(--primary) !important;
    box-shadow: 0 0 0 4px rgba(59, 130, 246, 0.1) !important;
    background: white !important;
    transform: translateY(-1px) !important;
}

.gradio-button {
    background: linear-gradient(135deg, var(--primary), var(--primary-light)) !important;
    border: none !important;
    border-radius: 12px !important;
    padding: 1rem 2rem !important;
    font-weight: 600 !important;
    font-size: 1rem !important;
    color: white !important;
    cursor: pointer !important;
    transition: all 0.3s ease !important;
    text-transform: uppercase !important;
    letter-spacing: 0.5px !important;
}

.gradio-button:hover {
    transform: translateY(-2px) !important;
    box-shadow: var(--shadow-xl) !important;
    background: linear-gradient(135deg, var(--primary-light), var(--primary)) !important;
}

.gradio-button:active {
    transform: translateY(0px) !important;
}

/* Enhanced dropdown and slider styling */
.gradio-dropdown select {
    background: rgba(255, 255, 255, 0.95) !important;
    border: 2px solid var(--border) !important;
    border-radius: 12px !important;
    padding: 1rem !important;
    color: var(--text-primary) !important;
    font-size: 1rem !important;
}

.gradio-slider input[type="range"] {
    background: var(--primary) !important;
}

/* Loading states */
.loading {
    opacity: 0.7;
    pointer-events: none;
    position: relative;
}

.loading::after {
    content: '';
    position: absolute;
    top: 50%;
    left: 50%;
    transform: translate(-50%, -50%);
    width: 24px;
    height: 24px;
    border: 3px solid var(--primary);
    border-top: 3px solid transparent;
    border-radius: 50%;
    animation: spin 1s linear infinite;
}

@keyframes spin {
    0% { transform: translate(-50%, -50%) rotate(0deg); }
    100% { transform: translate(-50%, -50%) rotate(360deg); }
}

/* Responsive Design */
@media (max-width: 768px) {
    .cover-buttons {
        flex-direction: column;
        align-items: center;
    }
    
    .cover-btn {
        width: 100% !important;
        max-width: 300px !important;
    }
    
    .services-grid {
        grid-template-columns: 1fr;
        gap: 1.5rem;
    }
    
    .auth-container {
        padding: 2rem;
        margin: 1rem;
    }
}

@media (max-width: 480px) {
    .cover-page {
        padding: 1rem;
    }
    
    .cover-icon {
        font-size: 4rem;
    }
    
    .service-card {
        padding: 2rem;
    }
}

/* Print styles */
@media print {
    .cover-page::before,
    .service-card::before {
        display: none;
    }
    
    * {
        background: white !important;
        color: black !important;
    }
}

/* High contrast mode support */
@media (prefers-contrast: high) {
    :root {
        --border: #000;
        --text-secondary: #000;
    }
}

/* Reduced motion support */
@media (prefers-reduced-motion: reduce) {
    *,
    *::before,
    *::after {
        animation-duration: 0.01ms !important;
        animation-iteration-count: 1 !important;
        transition-duration: 0.01ms !important;
    }
}
"""

def create_multipage_app():
    with gr.Blocks(
        theme=gr.themes.Soft(
            primary_hue=gr.themes.colors.blue,
            secondary_hue=gr.themes.colors.green,
            neutral_hue=gr.themes.colors.slate,
        ),
        title="⚡ Power Systems Consultant - Enhanced",
        css=ENHANCED_CSS
    ) as app:
        
        # Global state management
        current_page = gr.State("cover")
        user_state = gr.State(None)
        session_state = gr.State(None)
        
        # Page 1: Cover Page (Always visible initially)
        with gr.Column(visible=True, elem_classes=["cover-page"]) as cover_page:
            with gr.HTML() as cover_content:
                gr.HTML("""
                    <div class="cover-hero">
                        <div class="cover-icon">⚡</div>
                        <h1 class="cover-title">Power Systems Consultant</h1>
                        <p class="cover-subtitle">
                            Advanced AI-powered platform for electrical power systems analysis, fault calculations, 
                            protection design, and engineering excellence. Experience the future of power systems consulting.
                        </p>
                    </div>
                """)
            
            with gr.Row():
                signin_nav_btn = gr.Button("🔐 Sign In", elem_classes=["cover-btn", "cover-btn-primary"])
                signup_nav_btn = gr.Button("🚀 Create Account", elem_classes=["cover-btn", "cover-btn-secondary"])
        
        # Page 2: Sign In Page
        with gr.Column(visible=False, elem_classes=["auth-page"]) as signin_page:
            with gr.Column(elem_classes=["auth-container"]):
                gr.HTML("""
                    <div class="auth-header">
                        <div class="auth-icon">🔐</div>
                        <h2 class="auth-title">Welcome Back</h2>
                        <p class="auth-subtitle">Sign in to your Power Systems workspace</p>
                    </div>
                """)
                
                login_username = gr.Textbox(
                    label="Username",
                    placeholder="Enter your username"
                )
                login_password = gr.Textbox(
                    label="Password",
                    type="password",
                    placeholder="Enter your password"
                )
                with gr.Row():
                    signin_btn = gr.Button("Sign In", variant="primary")
                    back_cover_signin = gr.Button("← Back", variant="secondary")
                signin_status = gr.Textbox(label="Status", interactive=False, visible=False)
        
        # Page 3: Sign Up Page
        with gr.Column(visible=False, elem_classes=["auth-page"]) as signup_page:
            with gr.Column(elem_classes=["auth-container"]):
                gr.HTML("""
                    <div class="auth-header">
                        <div class="auth-icon">🚀</div>
                        <h2 class="auth-title">Join Our Community</h2>
                        <p class="auth-subtitle">Create your Power Systems workspace</p>
                    </div>
                """)
                
                reg_username = gr.Textbox(
                    label="Username",
                    placeholder="Choose a unique username"
                )
                reg_email = gr.Textbox(
                    label="Email",
                    placeholder="[email protected]"
                )
                reg_full_name = gr.Textbox(
                    label="Full Name",
                    placeholder="Your full name"
                )
                reg_password = gr.Textbox(
                    label="Password",
                    type="password",
                    placeholder="Create a strong password (min 6 chars)"
                )
                reg_confirm_password = gr.Textbox(
                    label="Confirm Password",
                    type="password",
                    placeholder="Confirm your password"
                )
                with gr.Row():
                    signup_btn = gr.Button("Create Account", variant="primary")
                    back_cover_signup = gr.Button("← Back", variant="secondary")
                signup_status = gr.Textbox(label="Status", interactive=False, visible=False)
        
        # Page 4: Services Selection Page
        with gr.Column(visible=False, elem_classes=["services-page"]) as services_page:
            user_welcome_display = gr.HTML("""
                <div class="services-header">
                    <h1 class="services-title">Power Systems Workspace</h1>
                    <p class="services-subtitle">Choose your engineering tool and start analyzing power systems</p>
                    <div class="user-welcome" id="user-welcome-display">
                        <div style="display: flex; align-items: center; justify-content: center; gap: 1rem;">
                            <div style="background: linear-gradient(135deg, var(--success), var(--secondary-light)); border-radius: 50%; width: 50px; height: 50px; display: flex; align-items: center; justify-content: center;">
                                <span style="color: white; font-size: 1.2rem;">👤</span>
                            </div>
                            <div>
                                <h3 style="margin: 0; color: var(--text-primary);">Welcome to Power Systems!</h3>
                                <p style="margin: 0; color: var(--text-secondary); font-size: 0.9rem;">Ready to tackle power systems challenges?</p>
                            </div>
                        </div>
                    </div>
                </div>
            """)
            
            with gr.Row():
                with gr.Column(elem_classes=["service-card"]):
                    gr.HTML("""
                        <div style="text-align: center;">
                            <div class="service-icon">🤖</div>
                            <h3 class="service-title">AI Consultant</h3>
                            <p class="service-description">
                                Interactive chat with AI expert for fault analysis, protection systems, 
                                calculations, and engineering guidance with professional diagram generation.
                            </p>
                        </div>
                    """)
                    chatbot_service_btn = gr.Button("Launch Consultant", elem_classes=["service-button"])
                
                with gr.Column(elem_classes=["service-card"]):
                    gr.HTML("""
                        <div style="text-align: center;">
                            <div class="service-icon">📚</div>
                            <h3 class="service-title">Practice Generator</h3>
                            <p class="service-description">
                                Generate customized practice questions with detailed solutions, 
                                step-by-step calculations, and standards references for exam preparation.
                            </p>
                        </div>
                    """)
                    practice_service_btn = gr.Button("Generate Practice", elem_classes=["service-button"])
            
            with gr.Row():
                with gr.Column(elem_classes=["service-card"]):
                    gr.HTML("""
                        <div style="text-align: center;">
                            <div class="service-icon">📋</div>
                            <h3 class="service-title">Standards Explorer</h3>
                            <p class="service-description">
                                Comprehensive explanations of IEEE, IEC, and international standards 
                                with practical applications and implementation guidelines.
                            </p>
                        </div>
                    """)
                    standards_service_btn = gr.Button("Explore Standards", elem_classes=["service-button"])
                
                with gr.Column(elem_classes=["service-card"]):
                    gr.HTML("""
                        <div style="text-align: center;">
                            <div class="service-icon">🎓</div>
                            <h3 class="service-title">Study Resources</h3>
                            <p class="service-description">
                                Essential formulas, concepts, calculations, and comprehensive 
                                exam preparation materials for power systems engineering.
                            </p>
                        </div>
                    """)
                    study_service_btn = gr.Button("Access Resources", elem_classes=["service-button"])
            
            with gr.Row():
                logout_btn = gr.Button("🚪 Sign Out", variant="secondary")
        
        # Page 5: Chatbot Interface
        with gr.Column(visible=False, elem_classes=["dark-section"]) as chatbot_page:
            with gr.Column(elem_classes=["chatbot-page"]):
                gr.HTML("""
                    <div class="chatbot-header">
                        <h2 style="color: var(--text-primary); margin: 0;">🤖 AI Power Systems Consultant</h2>
                        <p style="color: var(--text-secondary); margin: 0.5rem 0 0 0;">Ask about fault analysis, protection systems, calculations, or request diagrams</p>
                    </div>
                """)
                
                with gr.Row():
                    back_services_chat = gr.Button("← Back to Services", variant="secondary")
                    new_chat_btn = gr.Button("➕ New Chat", variant="primary")
                
                # Chat interface with enhanced styling
                chatbot = gr.Chatbot(
                    height=400,
                    show_label=False,
                    avatar_images=("👤", "🤖"),
                    bubble_full_width=False
                )
                
                # Diagram display with loading state
                diagram_display = gr.HTML("""
                    <div style="background: var(--bg-card); border: 1px solid var(--border); border-radius: 16px; padding: 2rem; margin: 1rem 0; text-align: center;">
                        <div style="color: var(--text-muted); padding: 2rem;">
                            <div style="font-size: 3rem; margin-bottom: 1rem;">📐</div>
                            <p>Professional engineering diagrams will appear here</p>
                            <small>Ask for single line diagrams, protection schemes, or circuit drawings</small>
                        </div>
                    </div>
                """)
                
                # Enhanced input area
                with gr.Row():
                    msg = gr.Textbox(
                        placeholder="Ask about faults, protection, calculations, or request diagrams...",
                        show_label=False,
                        lines=3,
                        scale=4
                    )
                    with gr.Column(scale=1):
                        submit_btn = gr.Button("🚀 Send", variant="primary")
                        clear_btn = gr.Button("🗑️ Clear", variant="secondary")
                
                # Quick action buttons
                with gr.Row():
                    with gr.Column():
                        gr.HTML("<p><strong>Quick Actions:</strong></p>")
                        quick_fault_btn = gr.Button("⚡ Fault Analysis", size="sm")
                        quick_protection_btn = gr.Button("🛡️ Protection", size="sm")
                    with gr.Column():
                        gr.HTML("<p><strong>Diagrams:</strong></p>")
                        quick_sld_btn = gr.Button("📐 Single Line", size="sm")
                        quick_protection_diagram_btn = gr.Button("🔧 Protection Scheme", size="sm")
        
        # Page 6: Practice Generator
        with gr.Column(visible=False, elem_classes=["services-page"]) as practice_page:
            gr.HTML("""
                <div class="services-header">
                    <h1 class="services-title">📚 Practice Generator</h1>
                    <p class="services-subtitle">AI-generated practice questions with detailed solutions</p>
                </div>
            """)
            
            back_services_practice = gr.Button("← Back to Services", variant="secondary")
            
            with gr.Row():
                with gr.Column(scale=1):
                    gr.HTML('<h3 style="color: var(--text-primary); margin-bottom: 2rem;">⚙️ Customize Practice Pack</h3>')
                    
                    topic_input = gr.Dropdown(
                        choices=[
                            "Fault Analysis", "Protection Systems", "Power Flow",
                            "Stability Studies", "Relay Coordination", "Transformers",
                            "Transmission Lines", "Load Flow", "Short Circuit Analysis",
                            "Harmonics", "Power Quality", "SCADA Systems", "Synchronous Machines",
                            "Power Electronics", "Renewable Energy Integration"
                        ],
                        label="Select Topic",
                        value="Fault Analysis"
                    )
                    
                    difficulty_input = gr.Radio(
                        choices=["Beginner", "Intermediate", "Advanced", "Expert"],
                        label="Difficulty Level",
                        value="Intermediate"
                    )
                    
                    num_questions_input = gr.Slider(
                        minimum=5,
                        maximum=25,
                        step=1,
                        value=10,
                        label="Number of Questions"
                    )
                    
                    include_solutions = gr.Checkbox(
                        label="Include detailed solutions",
                        value=True
                    )
                    
                    generate_btn = gr.Button("🚀 Generate Practice Pack", variant="primary")
                    
                    # Practice pack templates
                    with gr.Accordion("📋 Practice Templates", open=False):
                        template_pe_exam = gr.Button("PE Exam Prep", variant="secondary", size="sm")
                        template_ieee_standards = gr.Button("IEEE Standards", variant="secondary", size="sm")
                        template_fault_calc = gr.Button("Fault Calculations", variant="secondary", size="sm")
                
                with gr.Column(scale=2):
                    practice_output = gr.Textbox(
                        label="Generated Practice Pack",
                        lines=25,
                        placeholder="Your customized practice questions will appear here...",
                        show_label=False
                    )
                    
                    # Export options
                    with gr.Row():
                        export_pdf_btn = gr.Button("📄 Export PDF", variant="secondary", size="sm")
                        export_txt_btn = gr.Button("📝 Export Text", variant="secondary", size="sm")
        
        # Page 7: Standards Explorer
        with gr.Column(visible=False, elem_classes=["services-page"]) as standards_page:
            gr.HTML("""
                <div class="services-header">
                    <h1 class="services-title">📋 Standards Explorer</h1>
                    <p class="services-subtitle">Comprehensive power systems standards library</p>
                </div>
            """)
            
            back_services_standards = gr.Button("← Back to Services", variant="secondary")
            
            with gr.Row():
                with gr.Column(scale=1):
                    gr.HTML('<h3 style="color: var(--text-primary); margin-bottom: 2rem;">📖 Explore Standards</h3>')
                    
                    standard_category = gr.Radio(
                        choices=["IEEE Standards", "IEC Standards", "ANSI Standards", "All Standards"],
                        label="Category",
                        value="IEEE Standards"
                    )
                    
                    standard_input = gr.Dropdown(
                        choices=[
                            "IEEE C37.2 - Device Function Numbers",
                            "IEEE 1547 - Distributed Generation",
                            "IEEE 519 - Harmonic Control",
                            "IEEE C57.12.00 - Transformers",
                            "IEEE 242 - Protection & Coordination",
                            "IEEE 399 - Industrial Power Systems",
                            "IEEE C37.010 - Application Guide",
                            "IEC 61850 - Substation Automation",
                            "IEC 60909 - Short-Circuit Calculations",
                            "IEC 61131 - PLC Programming",
                            "IEC 60255 - Electrical Relays",
                            "ANSI C84.1 - Voltage Ratings"
                        ],
                        label="Select Standard",
                        value="IEEE C37.2 - Device Function Numbers"
                    )
                    
                    detail_level = gr.Radio(
                        choices=["Overview", "Detailed", "Implementation Guide"],
                        label="Detail Level",
                        value="Detailed"
                    )
                    
                    explain_btn = gr.Button("🔍 Explain Standard", variant="primary")
                    
                    # Quick access buttons
                    with gr.Accordion("🚀 Quick Access", open=False):
                        quick_protection_standard = gr.Button("Protection Standards", variant="secondary", size="sm")
                        quick_safety_standard = gr.Button("Safety Standards", variant="secondary", size="sm")
                        quick_testing_standard = gr.Button("Testing Standards", variant="secondary", size="sm")
                
                with gr.Column(scale=2):
                    standard_output = gr.Textbox(
                        label="Standard Explanation",
                        lines=25,
                        placeholder="Detailed standard explanation will appear here...",
                        show_label=False
                    )
                    
                    # Related standards
                    related_standards = gr.HTML("""
                        <div style="background: var(--bg-card); border: 1px solid var(--border); border-radius: 12px; padding: 1rem; margin-top: 1rem;">
                            <h4 style="color: var(--text-primary); margin: 0 0 1rem 0;">🔗 Related Standards</h4>
                            <p style="color: var(--text-secondary); margin: 0;">Select a standard to see related documents</p>
                        </div>
                    """)
        
        # Page 8: Enhanced Study Resources
        with gr.Column(visible=False, elem_classes=["services-page"]) as study_page:
            gr.HTML("""
                <div class="services-header">
                    <h1 class="services-title">🎓 Study Resources</h1>
                    <p class="services-subtitle">Essential formulas, concepts, and exam preparation materials</p>
                </div>
            """)
            
            back_services_study = gr.Button("← Back to Services", variant="secondary")
            
            # Enhanced study sections
            with gr.Tabs():
                with gr.Tab("⚡ Formulas & Equations"):
                    with gr.Accordion("⚡ Fault Analysis Formulas", open=True):
                        gr.HTML("""
                            <div style="background: rgba(59, 130, 246, 0.1); border: 1px solid rgba(59, 130, 246, 0.3); border-radius: 16px; padding: 2rem; margin: 1.5rem 0;">
                                <h4 style="color: var(--primary); margin-bottom: 1rem; display: flex; align-items: center; gap: 0.75rem;">
                                    ⚡ Three-Phase Fault Current
                                </h4>
                                <div style="background: rgba(0,0,0,0.1); border-radius: 8px; padding: 1rem; margin: 1rem 0;">
                                    <code style="color: var(--primary); font-size: 1.1rem; font-weight: bold;">
                                        I_fault = V_nominal / Z_total
                                    </code>
                                </div>
                                <p style="color: var(--text-secondary); margin: 0.5rem 0;">
                                    Where: V_nominal = System nominal voltage, Z_total = Total impedance to fault
                                </p>
                                
                                <h4 style="color: var(--primary); margin: 2rem 0 1rem 0;">⚠️ Line-to-Ground Fault</h4>
                                <div style="background: rgba(0,0,0,0.1); border-radius: 8px; padding: 1rem; margin: 1rem 0;">
                                    <code style="color: var(--primary); font-size: 1.1rem; font-weight: bold;">
                                        I_fault = 3 × V_nominal / (Z1 + Z2 + Z0)
                                    </code>
                                </div>
                                
                                <h4 style="color: var(--primary); margin: 2rem 0 1rem 0;">↔️ Line-to-Line Fault</h4>
                                <div style="background: rgba(0,0,0,0.1); border-radius: 8px; padding: 1rem; margin: 1rem 0;">
                                    <code style="color: var(--primary); font-size: 1.1rem; font-weight: bold;">
                                        I_fault = √3 × V_nominal / (Z1 + Z2)
                                    </code>
                                </div>
                            </div>
                        """)
                    
                    with gr.Accordion("🧮 Power System Calculations", open=False):
                        gr.HTML("""
                            <div style="background: rgba(16, 185, 129, 0.1); border: 1px solid rgba(16, 185, 129, 0.3); border-radius: 16px; padding: 2rem; margin: 1.5rem 0;">
                                <h4 style="color: var(--secondary); margin-bottom: 1rem;">🧮 Three-Phase Power Formulas</h4>
                                <div style="display: grid; grid-template-columns: 1fr 1fr; gap: 2rem; margin: 2rem 0;">
                                    <div>
                                        <h6 style="color: var(--secondary); margin: 0 0 1rem 0;">Real Power</h6>
                                        <div style="background: rgba(0,0,0,0.1); border-radius: 8px; padding: 1rem;">
                                            <code style="color: var(--secondary); font-weight: bold;">P = √3 × V × I × cos(φ)</code>
                                        </div>
                                    </div>
                                    <div>
                                        <h6 style="color: var(--secondary); margin: 0 0 1rem 0;">Reactive Power</h6>
                                        <div style="background: rgba(0,0,0,0.1); border-radius: 8px; padding: 1rem;">
                                            <code style="color: var(--secondary); font-weight: bold;">Q = √3 × V × I × sin(φ)</code>
                                        </div>
                                    </div>
                                </div>
                                <div style="margin-top: 2rem;">
                                    <h6 style="color: var(--secondary); margin: 0 0 1rem 0;">Apparent Power</h6>
                                    <div style="background: rgba(0,0,0,0.1); border-radius: 8px; padding: 1rem;">
                                        <code style="color: var(--secondary); font-weight: bold;">S = √(P² + Q²) = √3 × V × I</code>
                                    </div>
                                </div>
                            </div>
                        """)
                
                with gr.Tab("🛡️ Protection Systems"):
                    with gr.Accordion("🛡️ Protection Principles", open=True):
                        gr.HTML("""
                            <div style="background: rgba(139, 92, 246, 0.1); border: 1px solid rgba(139, 92, 246, 0.3); border-radius: 16px; padding: 2rem; margin: 1.5rem 0;">
                                <h4 style="color: var(--accent); margin-bottom: 2rem;">Protection System Requirements</h4>
                                <div style="display: grid; grid-template-columns: repeat(auto-fit, minmax(250px, 1fr)); gap: 1.5rem;">
                                    <div style="background: rgba(59, 130, 246, 0.1); padding: 1.5rem; border-radius: 10px;">
                                        <h6 style="color: var(--primary); margin: 0 0 1rem 0;">🎯 Selectivity</h6>
                                        <p style="color: var(--text-secondary); margin: 0; line-height: 1.6;">
                                            Only the protection closest to the fault should operate, isolating the minimum system area.
                                        </p>
                                    </div>
                                    <div style="background: rgba(16, 185, 129, 0.1); padding: 1.5rem; border-radius: 10px;">
                                        <h6 style="color: var(--secondary); margin: 0 0 1rem 0;">⚡ Speed</h6>
                                        <p style="color: var(--text-secondary); margin: 0; line-height: 1.6;">
                                            Fast fault clearing to maintain system stability and minimize equipment damage.
                                        </p>
                                    </div>
                                    <div style="background: rgba(245, 158, 11, 0.1); padding: 1.5rem; border-radius: 10px;">
                                        <h6 style="color: var(--warning); margin: 0 0 1rem 0;">🔒 Security</h6>
                                        <p style="color: var(--text-secondary); margin: 0; line-height: 1.6;">
                                            Protection should not operate for external faults or during normal conditions.
                                        </p>
                                    </div>
                                    <div style="background: rgba(239, 68, 68, 0.1); padding: 1.5rem; border-radius: 10px;">
                                        <h6 style="color: var(--danger); margin: 0 0 1rem 0;">🛡️ Dependability</h6>
                                        <p style="color: var(--text-secondary); margin: 0; line-height: 1.6;">
                                            Protection must operate when required, with backup protection available.
                                        </p>
                                    </div>
                                </div>
                            </div>
                        """)
                
                with gr.Tab("🧮 Interactive Calculator"):
                    gr.HTML("""
                        <div style="background: var(--bg-card); border: 1px solid var(--border); border-radius: 20px; padding: 2.5rem; margin: 2rem 0;">
                            <h3 style="color: var(--text-primary); margin-bottom: 2rem; text-align: center;">
                                🧮 Power Systems Calculator
                            </h3>
                        </div>
                    """)
                    
                    with gr.Tabs():
                        with gr.Tab("⚡ Power Calculations"):
                            with gr.Row():
                                with gr.Column():
                                    calc_voltage = gr.Number(label="Voltage (V)", value=480)
                                    calc_current = gr.Number(label="Current (A)", value=100)
                                    calc_pf = gr.Slider(0.1, 1.0, 0.85, label="Power Factor", step=0.01)
                                    calc_btn = gr.Button("🧮 Calculate Power", variant="primary")
                                
                                with gr.Column():
                                    calc_results = gr.HTML("""
                                        <div style="background: var(--bg-card); padding: 2rem; border-radius: 15px; border: 1px solid var(--border); min-height: 250px; display: flex; align-items: center; justify-content: center;">
                                            <div style="text-align: center; color: var(--text-secondary);">
                                                <div style="font-size: 3rem; margin-bottom: 1rem; opacity: 0.5;">🧮</div>
                                                <p>Enter values and click Calculate</p>
                                            </div>
                                        </div>
                                    """)
                        
                        with gr.Tab("🔧 Fault Current"):
                            with gr.Row():
                                with gr.Column():
                                    fault_voltage = gr.Number(label="System Voltage (kV)", value=138)
                                    fault_mva = gr.Number(label="MVA Base", value=100)
                                    fault_z1 = gr.Number(label="Positive Sequence Z (pu)", value=0.15)
                                    fault_z2 = gr.Number(label="Negative Sequence Z (pu)", value=0.15)
                                    fault_z0 = gr.Number(label="Zero Sequence Z (pu)", value=0.05)
                                    fault_calc_btn = gr.Button("⚡ Calculate Fault Current", variant="primary")
                                
                                with gr.Column():
                                    fault_results = gr.HTML("""
                                        <div style="background: var(--bg-card); padding: 2rem; border-radius: 15px; border: 1px solid var(--border); min-height: 250px; display: flex; align-items: center; justify-content: center;">
                                            <div style="text-align: center; color: var(--text-secondary);">
                                                <div style="font-size: 3rem; margin-bottom: 1rem; opacity: 0.5;">⚡</div>
                                                <p>Enter system parameters</p>
                                            </div>
                                        </div>
                                    """)

        # Event handler functions with enhanced error handling
        def handle_signin(username, password):
            if not consultant or not consultant.user_manager:
                return gr.update(), gr.update(), gr.update(), gr.update(), None, "⚠️ System not fully initialized"
            
            if not username or not password:
                return gr.update(), gr.update(), gr.update(), gr.update(), None, "⚠️ Please enter both username and password"
            
            success, user_data = consultant.user_manager.authenticate_user(username, password)
            if success:
                return (
                    gr.update(visible=False),  # Hide signin page
                    gr.update(visible=False),  # Hide signup page  
                    gr.update(visible=False),  # Hide cover page
                    gr.update(visible=True),   # Show services page
                    user_data,  # Store user data
                    f"✅ Welcome back, {user_data['full_name']}!"
                )
            else:
                return gr.update(), gr.update(), gr.update(), gr.update(), None, "❌ Invalid credentials"

            # Email validation
            if "@" not in email or "." not in email:
                return gr.update(), gr.update(), gr.update(), gr.update(), None, "⚠️ Please enter a valid email address"
            
            success, message = consultant.user_manager.create_user(username, email, password, full_name)
            if success:
                # Auto-login after successful registration
                success, user_data = consultant.user_manager.authenticate_user(username, password)
                if success:
                    return (
                        gr.update(visible=False),  # Hide signin page
                        gr.update(visible=False),  # Hide signup page
                        gr.update(visible=False),  # Hide cover page
                        gr.update(visible=True),   # Show services page
                        user_data,  # Store user data
                        f"✅ Account created successfully! Welcome {user_data['full_name']}!"
                    )
            
            return gr.update(), gr.update(), gr.update(), gr.update(), None, f"❌ {message}"

        def handle_new_chat(user_data):
            if not consultant or not user_data:
                return None, []
            
            session_id = consultant.create_chat_session(user_data['id'])
            return session_id, []

        def handle_chat_message(message, history, user_data, session_id):
            if not consultant or not user_data:
                return history, "", None
            
            if not message.strip():
                return history, message, None
            
            try:
                response, diagram_svg = consultant.generate_response(message, history, session_id)
                new_history = history + [[message, response]]
                
                # Update diagram display if available
                if diagram_svg:
                    diagram_html = f"""
                        <div style="background: white; padding: 1rem; border-radius: 12px; margin: 1rem 0; box-shadow: var(--shadow);">
                            {diagram_svg}
                        </div>
                    """
                else:
                    diagram_html = """
                        <div style="background: var(--bg-card); border: 1px solid var(--border); border-radius: 16px; padding: 2rem; margin: 1rem 0; text-align: center;">
                            <div style="color: var(--text-muted); padding: 2rem;">
                                <div style="font-size: 3rem; margin-bottom: 1rem;">📐</div>
                                <p>Professional engineering diagrams will appear here</p>
                                <small>Ask for single line diagrams, protection schemes, or circuit drawings</small>
                            </div>
                        </div>
                    """
                
                return new_history, "", diagram_html
            except Exception as e:
                error_response = f"❌ Error: {str(e)}"
                return history + [[message, error_response]], "", None

        def clear_chat():
            return [], "", """
                <div style="background: var(--bg-card); border: 1px solid var(--border); border-radius: 16px; padding: 2rem; margin: 1rem 0; text-align: center;">
                    <div style="color: var(--text-muted); padding: 2rem;">
                        <div style="font-size: 3rem; margin-bottom: 1rem;">📐</div>
                        <p>Professional engineering diagrams will appear here</p>
                        <small>Ask for single line diagrams, protection schemes, or circuit drawings</small>
                    </div>
                </div>
            """

        def handle_quick_action(action, history, user_data, session_id):
            """Handle quick action buttons"""
            quick_messages = {
                "fault": "Explain three-phase fault analysis and show me a single line diagram",
                "protection": "Describe power system protection coordination and show a protection scheme",
                "sld": "Generate a single line diagram for a typical power system",
                "protection_diagram": "Create a comprehensive protection scheme diagram"
            }
            
            message = quick_messages.get(action, "")
            if message:
                return handle_chat_message(message, history, user_data, session_id)
            return history, "", None

        def handle_practice_generation(topic, difficulty, num_questions, include_solutions, user_data):
            if not consultant or not user_data:
                return "⚠️ Please log in to access this feature."
            
            try:
                result = consultant.generate_practice_pack(topic, difficulty, int(num_questions))
                if not include_solutions:
                    # Simple processing to remove solution sections
                    lines = result.split('\n')
                    filtered_lines = []
                    skip_solution = False
                    
                    for line in lines:
                        if line.strip().lower().startswith('**solution'):
                            skip_solution = True
                            continue
                        elif line.strip().startswith('---') or line.strip().startswith('##'):
                            skip_solution = False
                        
                        if not skip_solution:
                            filtered_lines.append(line)
                    
                    result = '\n'.join(filtered_lines)
                
                return result
            except Exception as e:
                return f"❌ Error generating practice pack: {str(e)}"

        def handle_standard_explanation(standard, detail_level, user_data):
            if not consultant or not user_data:
                return "⚠️ Please log in to access this feature."
            
            try:
                result = consultant.explain_standard(standard)
                
                # Add detail level processing
                if detail_level == "Overview":
                    lines = result.split('\n')[:15]  # First 15 lines for overview
                    result = '\n'.join(lines) + "\n\n*This is an overview. Select 'Detailed' for complete information.*"
                elif detail_level == "Implementation Guide":
                    result += "\n\n## Implementation Checklist\n- Review standard requirements\n- Assess current system compliance\n- Plan implementation phases\n- Conduct testing and validation\n- Document compliance"
                
                return result
            except Exception as e:
                return f"❌ Error explaining standard: {str(e)}"

        def calculate_power(voltage, current, pf):
            if voltage and current and pf and voltage > 0 and current > 0:
                try:
                    p = (3**0.5) * voltage * current * pf / 1000  # kW
                    q = (3**0.5) * voltage * current * ((1-pf**2)**0.5) / 1000  # kVAR
                    s = (3**0.5) * voltage * current / 1000  # kVA
                    
                    return f"""
                        <div style="background: var(--bg-card); padding: 2rem; border-radius: 15px; border: 1px solid var(--border);">
                            <h4 style="color: var(--text-primary); margin-bottom: 2rem; text-align: center;">
                                📊 Power Calculation Results
                            </h4>
                            <div style="display: grid; grid-template-columns: repeat(auto-fit, minmax(200px, 1fr)); gap: 1.5rem;">
                                <div style="background: rgba(16, 185, 129, 0.1); padding: 1.5rem; border-radius: 10px; text-align: center;">
                                    <h6 style="color: var(--secondary); margin: 0 0 0.5rem 0;">Real Power</h6>
                                    <p style="font-size: 1.5rem; font-weight: 700; margin: 0; color: var(--text-primary);">{p:.2f} kW</p>
                                </div>
                                <div style="background: rgba(245, 158, 11, 0.1); padding: 1.5rem; border-radius: 10px; text-align: center;">
                                    <h6 style="color: var(--warning); margin: 0 0 0.5rem 0;">Reactive Power</h6>
                                    <p style="font-size: 1.5rem; font-weight: 700; margin: 0; color: var(--text-primary);">{q:.2f} kVAR</p>
                                </div>
                                <div style="background: rgba(59, 130, 246, 0.1); padding: 1.5rem; border-radius: 10px; text-align: center;">
                                    <h6 style="color: var(--primary); margin: 0 0 0.5rem 0;">Apparent Power</h6>
                                    <p style="font-size: 1.5rem; font-weight: 700; margin: 0; color: var(--text-primary);">{s:.2f} kVA</p>
                                </div>
                            </div>
                            <div style="margin-top: 2rem; padding: 1rem; background: rgba(0,0,0,0.05); border-radius: 8px;">
                                <h6 style="color: var(--text-primary); margin: 0 0 1rem 0;">Calculation Details:</h6>
                                <p style="margin: 0.5rem 0; color: var(--text-secondary);">P = √3 × V × I × cos(φ) = √3 × {voltage} × {current} × {pf:.3f} = {p:.2f} kW</p>
                                <p style="margin: 0.5rem 0; color: var(--text-secondary);">Q = √3 × V × I × sin(φ) = √3 × {voltage} × {current} × {((1-pf**2)**0.5):.3f} = {q:.2f} kVAR</p>
                                <p style="margin: 0.5rem 0; color: var(--text-secondary);">S = √3 × V × I = √3 × {voltage} × {current} = {s:.2f} kVA</p>
                            </div>
                        </div>
                    """
                except Exception as e:
                    return f"""
                        <div style="background: var(--bg-card); padding: 2rem; border-radius: 15px; border: 1px solid var(--border); text-align: center;">
                            <div style="font-size: 3rem; color: var(--danger); margin-bottom: 1rem;">❌</div>
                            <p>Calculation error: {str(e)}</p>
                        </div>
                    """
            else:
                return """
                    <div style="background: var(--bg-card); padding: 2rem; border-radius: 15px; border: 1px solid var(--border); text-align: center;">
                        <div style="font-size: 3rem; color: var(--warning); margin-bottom: 1rem;">⚠️</div>
                        <p>Please enter valid positive values for all fields</p>
                    </div>
                """

        def calculate_fault_current(voltage, mva_base, z1, z2, z0):
            if all(v is not None and v > 0 for v in [voltage, mva_base, z1]):
                try:
                    # Base current calculation
                    base_current = mva_base * 1000 / (voltage * (3**0.5))  # Base current in A
                    
                    # Three-phase fault
                    i_3ph = base_current / z1
                    
                    # Single line-to-ground fault
                    if z2 is not None and z0 is not None and z2 > 0 and z0 > 0:
                        i_slg = 3 * base_current / (z1 + z2 + z0)
                    else:
                        i_slg = None
                    
                    # Line-to-line fault
                    if z2 is not None and z2 > 0:
                        i_ll = (3**0.5) * base_current / (z1 + z2)
                    else:
                        i_ll = None
                    
                    result_html = f"""
                        <div style="background: var(--bg-card); padding: 2rem; border-radius: 15px; border: 1px solid var(--border);">
                            <h4 style="color: var(--text-primary); margin-bottom: 2rem; text-align: center;">
                                ⚡ Fault Current Analysis
                            </h4>
                            <div style="margin-bottom: 2rem; padding: 1rem; background: rgba(59, 130, 246, 0.1); border-radius: 8px;">
                                <h6 style="color: var(--primary); margin: 0 0 1rem 0;">System Parameters:</h6>
                                <p style="margin: 0.3rem 0; color: var(--text-secondary);">Voltage: {voltage} kV</p>
                                <p style="margin: 0.3rem 0; color: var(--text-secondary);">MVA Base: {mva_base} MVA</p>
                                <p style="margin: 0.3rem 0; color: var(--text-secondary);">Base Current: {base_current:.1f} A</p>
                            </div>
                            <div style="display: grid; grid-template-columns: repeat(auto-fit, minmax(250px, 1fr)); gap: 1.5rem;">
                                <div style="background: rgba(239, 68, 68, 0.1); padding: 1.5rem; border-radius: 10px; text-align: center;">
                                    <h6 style="color: var(--danger); margin: 0 0 0.5rem 0;">Three-Phase Fault</h6>
                                    <p style="font-size: 1.3rem; font-weight: 700; margin: 0; color: var(--text-primary);">{i_3ph:.0f} A</p>
                                    <small style="color: var(--text-secondary);">I = I_base / Z₁</small>
                                </div>
                    """
                    
                    if i_slg is not None:
                        result_html += f"""
                                <div style="background: rgba(245, 158, 11, 0.1); padding: 1.5rem; border-radius: 10px; text-align: center;">
                                    <h6 style="color: var(--warning); margin: 0 0 0.5rem 0;">Line-to-Ground</h6>
                                    <p style="font-size: 1.3rem; font-weight: 700; margin: 0; color: var(--text-primary);">{i_slg:.0f} A</p>
                                    <small style="color: var(--text-secondary);">I = 3×I_base / (Z₁+Z₂+Z₀)</small>
                                </div>
                        """
                    
                    if i_ll is not None:
                        result_html += f"""
                                <div style="background: rgba(139, 92, 246, 0.1); padding: 1.5rem; border-radius: 10px; text-align: center;">
                                    <h6 style="color: var(--accent); margin: 0 0 0.5rem 0;">Line-to-Line</h6>
                                    <p style="font-size: 1.3rem; font-weight: 700; margin: 0; color: var(--text-primary);">{i_ll:.0f} A</p>
                                    <small style="color: var(--text-secondary);">I = √3×I_base / (Z₁+Z₂)</small>
                                </div>
                        """
                    
                    result_html += """
                            </div>
                        </div>
                    """
                    
                    return result_html
                    
                except Exception as e:
                    return f"""
                        <div style="background: var(--bg-card); padding: 2rem; border-radius: 15px; border: 1px solid var(--border); text-align: center;">
                            <div style="font-size: 3rem; color: var(--danger); margin-bottom: 1rem;">❌</div>
                            <p>Calculation error: {str(e)}</p>
                        </div>
                    """
            else:
                return """
                    <div style="background: var(--bg-card); padding: 2rem; border-radius: 15px; border: 1px solid var(--border); text-align: center;">
                        <div style="font-size: 3rem; color: var(--warning); margin-bottom: 1rem;">⚠️</div>
                        <p>Please enter valid positive values for voltage, MVA base, and impedances</p>
                    </div>
                """

        # Navigation event handlers
        signin_nav_btn.click(
            fn=lambda: (gr.update(visible=False), gr.update(visible=True), gr.update(visible=False)),
            outputs=[cover_page, signin_page, signup_page]
        )
        
        signup_nav_btn.click(
            fn=lambda: (gr.update(visible=False), gr.update(visible=False), gr.update(visible=True)),
            outputs=[cover_page, signin_page, signup_page]
        )
        
        back_cover_signin.click(
            fn=lambda: (gr.update(visible=True), gr.update(visible=False), gr.update(visible=False)),
            outputs=[cover_page, signin_page, signup_page]
        )
        
        back_cover_signup.click(
            fn=lambda: (gr.update(visible=True), gr.update(visible=False), gr.update(visible=False)),
            outputs=[cover_page, signin_page, signup_page]
        )
        
        # Authentication event handlers
        signin_btn.click(
            fn=handle_signin,
            inputs=[login_username, login_password],
            outputs=[signin_page, signup_page, cover_page, services_page, user_state, signin_status]
        )
        
        signup_btn.click(
            fn=handle_signup,
            inputs=[reg_username, reg_email, reg_password, reg_confirm_password, reg_full_name],
            outputs=[signin_page, signup_page, cover_page, services_page, user_state, signup_status]
        )
        
        # Service navigation
        chatbot_service_btn.click(
            fn=lambda: (gr.update(visible=False), gr.update(visible=True)),
            outputs=[services_page, chatbot_page]
        )
        
        practice_service_btn.click(
            fn=lambda: (gr.update(visible=False), gr.update(visible=True)),
            outputs=[services_page, practice_page]
        )
        
        standards_service_btn.click(
            fn=lambda: (gr.update(visible=False), gr.update(visible=True)),
            outputs=[services_page, standards_page]
        )
        
        study_service_btn.click(
            fn=lambda: (gr.update(visible=False), gr.update(visible=True)),
            outputs=[services_page, study_page]
        )
        
        # Back to services navigation
        back_services_chat.click(
            fn=lambda: (gr.update(visible=True), gr.update(visible=False)),
            outputs=[services_page, chatbot_page]
        )
        
        back_services_practice.click(
            fn=lambda: (gr.update(visible=True), gr.update(visible=False)),
            outputs=[services_page, practice_page]
        )
        
        back_services_standards.click(
            fn=lambda: (gr.update(visible=True), gr.update(visible=False)),
            outputs=[services_page, standards_page]
        )
        
        back_services_study.click(
            fn=lambda: (gr.update(visible=True), gr.update(visible=False)),
            outputs=[services_page, study_page]
        )
        
        # Logout handler
        logout_btn.click(
            fn=lambda: (gr.update(visible=True), gr.update(visible=False), None, None),
            outputs=[cover_page, services_page, user_state, session_state]
        )
        
        # Chat functionality
        new_chat_btn.click(
            fn=handle_new_chat,
            inputs=[user_state],
            outputs=[session_state, chatbot]
        )
        
        submit_btn.click(
            fn=handle_chat_message,
            inputs=[msg, chatbot, user_state, session_state],
            outputs=[chatbot, msg, diagram_display]
        )
        
        msg.submit(
            fn=handle_chat_message,
            inputs=[msg, chatbot, user_state, session_state],
            outputs=[chatbot, msg, diagram_display]
        )
        
        clear_btn.click(
            fn=clear_chat,
            outputs=[chatbot, msg, diagram_display]
        )
        
        # Quick action buttons
        quick_fault_btn.click(
            fn=lambda h, u, s: handle_quick_action("fault", h, u, s),
            inputs=[chatbot, user_state, session_state],
            outputs=[chatbot, msg, diagram_display]
        )
        
        quick_protection_btn.click(
            fn=lambda h, u, s: handle_quick_action("protection", h, u, s),
            inputs=[chatbot, user_state, session_state],
            outputs=[chatbot, msg, diagram_display]
        )
        
        quick_sld_btn.click(
            fn=lambda h, u, s: handle_quick_action("sld", h, u, s),
            inputs=[chatbot, user_state, session_state],
            outputs=[chatbot, msg, diagram_display]
        )
        
        quick_protection_diagram_btn.click(
            fn=lambda h, u, s: handle_quick_action("protection_diagram", h, u, s),
            inputs=[chatbot, user_state, session_state],
            outputs=[chatbot, msg, diagram_display]
        )
        
        # Practice generator functionality
        generate_btn.click(
            fn=handle_practice_generation,
            inputs=[topic_input, difficulty_input, num_questions_input, include_solutions, user_state],
            outputs=[practice_output]
        )
        
        # Standards explorer functionality
        explain_btn.click(
            fn=handle_standard_explanation,
            inputs=[standard_input, detail_level, user_state],
            outputs=[standard_output]
        )
        
        # Calculator functionality
        calc_btn.click(
            fn=calculate_power,
            inputs=[calc_voltage, calc_current, calc_pf],
            outputs=[calc_results]
        )
        
        fault_calc_btn.click(
            fn=calculate_fault_current,
            inputs=[fault_voltage, fault_mva, fault_z1, fault_z2, fault_z0],
            outputs=[fault_results]
        )

    return app

# Launch the enhanced application
if __name__ == "__main__":
    app = create_multipage_app()
    
    # Enhanced startup information
    print("⚡" * 80)
    print("🚀 POWER SYSTEMS CONSULTANT v5.0 - PRODUCTION READY")
    print("⚡" * 80)
    print()
    print("🔧 LATEST ENHANCEMENTS:")
    print("   ✅ Enhanced error handling and fallback modes")
    print("   ✅ Demo mode when GROQ_API_KEY not available")
    print("   ✅ Improved diagram generation with multiple types")
    print("   ✅ Advanced interactive calculators")
    print("   ✅ Quick action buttons for common tasks")
    print("   ✅ Enhanced practice generator with templates")
    print("   ✅ Comprehensive standards library")
    print("   ✅ Better accessibility and responsive design")
    print("   ✅ Improved authentication with validation")
    print("   ✅ Professional animations and transitions")
    print()
    print("🎨 MODERN INTERFACE:")
    print("   ✨ Responsive multi-page design")
    print("   🔐 Secure user authentication system")
    print("   🎯 Interactive service dashboard") 
    print("   💬 Enhanced chatbot with quick actions")
    print("   📚 Advanced practice generator")
    print("   📋 Comprehensive standards explorer")
    print("   🎓 Interactive study resources")
    print("   🧮 Multiple calculators (power, fault current)")
    print()
    print("🔧 CORE FEATURES:")
    print("   🤖 AI-powered consultant (Groq LLM)")
    print("   📊 Professional SVG diagrams")
    print("   🎯 Customizable practice questions")
    print("   📋 Standards explanations")
    print("   🧮 Engineering calculators")
    print("   💾 SQLite user database")
    print("   📜 Chat session management")
    print("   🛡️ Fallback modes for reliability")
    print()
    
    # System status
    if consultant:
        if consultant.groq_client:
            print("🤖 GROQ API: ✅ Connected - Full AI functionality available")
        else:
            print("🤖 GROQ API: ⚠️ Demo mode - Set GROQ_API_KEY for full functionality")
        
        if consultant.user_manager:
            print("💾 Database: ✅ SQLite ready")
        else:
            print("💾 Database: ⚠️ Limited functionality")
        
        if EXTERNAL_UTILS_AVAILABLE:
            print("🔧 External Utils: ✅ Available")
            if hasattr(consultant, 'has_rag') and consultant.has_rag:
                print("📚 RAG System: ✅ Enhanced responses")
            else:
                print("📚 RAG System: ⚠️ Not available")
        else:
            print("🔧 External Utils: ⚠️ Using internal implementations")
    else:
        print("❌ SYSTEM ERROR: Consultant not initialized")
    
    print()
    print("🌐 ACCESS INFORMATION:")
    print("   📍 URL: http://localhost:7860")
    print("   🎯 Flow: Cover → Auth → Services → Tools")
    print("   💡 Demo: Works without API keys")
    print()
    print("🚀 PROFESSIONAL POWER SYSTEMS PLATFORM")
    print("⚡" * 80)
    
    try:
        app.launch(
            server_name="0.0.0.0",
            server_port=7860,
            share=False,
            show_api=False,
            show_error=True,
            quiet=False,
            favicon_path=None,
            ssl_verify=False
        )
    except Exception as e:
        print(f"❌ Launch Error: {str(e)}")
        print("💡 Try different port or check system requirements")