File size: 63,072 Bytes
6ea020b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
<!-- templates/base.html -->
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>{% block title %}Lab System{% endblock %}</title>
    <!-- Tailwind CSS -->
    <script src="/static/vendor/tailwind/tailwindcss.js"></script>
    <!-- Google Fonts -->
    <link href="/static/vendor/fonts/fonts.css" rel="stylesheet">
    <style>

        body { font-family: 'Inter', sans-serif; }

        .sidebar-link:hover { background-color: rgba(255,255,255,0.1); }

        .sidebar-link.active { background-color: #4f46e5; }

        

        /* Submenu Styles */

        .sidebar-submenu {

            max-height: 0;

            overflow: hidden;

            transition: all 0.3s cubic-bezier(0, 1, 0, 1);

        }

        .sidebar-submenu.open {

            max-height: 1000px;

            transition: all 0.3s cubic-bezier(1, 0, 1, 0);

        }

        .chevron {

            transition: transform 0.3s ease;

        }

        .chevron.rotate {

            transform: rotate(180deg);

        }

        .submenu-item {

            position: relative;

            padding-left: 3rem !important;

        }

        .submenu-item::before {

            content: '';

            position: absolute;

            left: 1.75rem;

            top: 50%;

            width: 4px;

            height: 4px;

            background: rgba(255,255,255,0.3);

            border-radius: 50%;

            transform: translateY(-50%);

        }

        .submenu-item.active::before {

            background: #818cf8;

            box-shadow: 0 0 8px #818cf8;

        }



        /* Custom Scrollbar for Sidebar */

        .custom-scrollbar::-webkit-scrollbar {

            width: 4px;

        }

        .custom-scrollbar::-webkit-scrollbar-track {

            background: rgba(255,255,255,0.05);

        }

        .custom-scrollbar::-webkit-scrollbar-thumb {

            background: rgba(255,255,255,0.1);

            border-radius: 10px;

        }

        .custom-scrollbar::-webkit-scrollbar-thumb:hover {

            background: rgba(255,255,255,0.2);

        }

        

        /* Modal Styles */

        .modal-overlay {

            display: none;

            position: fixed;

            top: 0;

            left: 0;

            width: 100%;

            height: 100%;

            background: rgba(0, 0, 0, 0.5);

            z-index: 1000;

            justify-content: center;

            align-items: center;

            backdrop-filter: blur(4px);

        }

        .modal-overlay.active {

            display: flex;

        }

        .modal-content {

            background: white;

            border-radius: 16px;

            padding: 2rem;

            max-width: 400px;

            width: 90%;

            text-align: center;

            box-shadow: 0 20px 60px rgba(0,0,0,0.3);

            animation: modalSlideIn 0.3s ease-out;

        }

        @keyframes modalSlideIn {

            from {

                opacity: 0;

                transform: translateY(-50px) scale(0.9);

            }

            to {

                opacity: 1;

                transform: translateY(0) scale(1);

            }

        }

        .modal-icon {

            width: 80px;

            height: 80px;

            border-radius: 50%;

            display: flex;

            align-items: center;

            justify-content: center;

            margin: 0 auto 1.5rem;

        }

        .modal-icon.success {

            background: #10b981;

            color: white;

        }

        .modal-icon.error {

            background: #ef4444;

            color: white;

        }

        .modal-icon.warning {

            background: #f59e0b;

            color: white;

        }

        .modal-icon svg {

            width: 40px;

            height: 40px;

        }



        /* ─── Global Fixed-Size Table Styles ─────────────────────────────── */

        /* Wrapper: horizontal scroll if content still overflows, but table

           itself won't grow beyond the viewport/container width.            */

        .overflow-x-auto {

            overflow-x: auto;

        }



        /* Make every data table fixed-layout so columns don't stretch to

           fit content. Combined with overflow-x-auto on the wrapper this

           keeps the table at a stable width while allowing a horizontal

           scrollbar only when necessary.                                     */

        table {

            table-layout: fixed;

            width: 100%;

            min-width: 600px; /* stay readable on narrow containers */

            border-collapse: collapse;

        }



        /* All header cells: wrap text, consistent padding.                  */

        table thead th {

            white-space: normal !important;

            word-break: break-word;

            overflow-wrap: break-word;

            vertical-align: bottom;

        }



        /* All body cells: allow text to wrap so rows expand vertically

           instead of pushing the table wider.                               */

        table tbody td {

            white-space: normal !important;

            word-break: break-word;

            overflow-wrap: break-word;

            vertical-align: top;

        }



        /* Default column width distribution – each column gets an equal

           share. Individual pages can override with inline width/style on

           <th> elements when they need custom proportions.                   */

        table th,

        table td {

            max-width: 0;   /* forces fixed-layout to honour table-layout:fixed */

        }

        /* ──────────────────────────────────────────────────────────────── */



        /* ─── Range Hover Tooltip ─────────────────────────────────────── */

        .range-hover-wrapper {

            position: relative;

            display: inline-block;

        }

        .range-hover-btn {

            background: #64748b;

            color: #fff;

            border: none;

            padding: 2px 10px;

            border-radius: 6px;

            font-size: 11px;

            font-weight: 700;

            cursor: default;

            letter-spacing: 0.3px;

            transition: background 0.2s;

        }

        .range-hover-wrapper:hover .range-hover-btn {

            background: #3b82f6;

        }

        .range-hover-bubble {

            display: none;

            position: fixed;

            background: #fef3c7;

            color: #92400e;

            border: 1px solid #fcd34d;

            border-radius: 10px;

            padding: 10px 16px;

            font-size: 12px;

            font-weight: 600;

            white-space: normal;

            word-break: break-word;

            width: max-content;

            max-width: 320px;

            min-width: 120px;

            text-align: center;

            line-height: 1.5;

            z-index: 9999;

            box-shadow: 0 4px 16px rgba(0,0,0,0.15);

            pointer-events: none;

        }

        .range-hover-bubble::after {

            content: '';

            position: absolute;

            top: 100%;

            left: 50%;

            transform: translateX(-50%);

            border: 6px solid transparent;

            border-top-color: #fcd34d;

        }

        /* ──────────────────────────────────────────────────────────────── */

    </style>
    <script>

        /* Range hover tooltip positioning — uses position:fixed so the

           bubble escapes any overflow:hidden / overflow:auto ancestor. */

        document.addEventListener('mouseenter', function(e) {

            const wrapper = e.target.closest('.range-hover-wrapper');

            if (!wrapper) return;

            const bubble = wrapper.querySelector('.range-hover-bubble');

            if (!bubble) return;

            const rect = wrapper.getBoundingClientRect();

            bubble.style.display = 'block';

            /* Position above the button, centered */

            const bubbleRect = bubble.getBoundingClientRect();

            let left = rect.left + rect.width / 2 - bubbleRect.width / 2;

            let top = rect.top - bubbleRect.height - 8;

            /* Keep within viewport */

            if (left < 4) left = 4;

            if (left + bubbleRect.width > window.innerWidth - 4) left = window.innerWidth - bubbleRect.width - 4;

            if (top < 4) top = rect.bottom + 8; /* flip below if no room above */

            bubble.style.left = left + 'px';

            bubble.style.top = top + 'px';

        }, true);

        document.addEventListener('mouseleave', function(e) {

            const wrapper = e.target.closest('.range-hover-wrapper');

            if (!wrapper) return;

            const bubble = wrapper.querySelector('.range-hover-bubble');

            if (bubble) bubble.style.display = 'none';

        }, true);

    </script>
    <script>

        tailwind.config = {

            theme: {

                extend: {

                    colors: {

                        primary: '#4f46e5',

                        secondary: '#1e293b',

                        accent: '#818cf8',

                        background: '#f3f4f6'

                    }

                }

            }

        }

    </script>
</head>
<body class="bg-background text-slate-800">
    
    <div class="flex h-screen overflow-hidden">
        
        <!-- Sidebar Overlay (Mobile) -->
        <div id="sidebarOverlay" class="fixed inset-0 bg-black bg-opacity-50 z-40 hidden lg:hidden transition-opacity duration-300" onclick="toggleSidebar()"></div>

        <!-- Sidebar -->
        <aside id="sidebar" class="fixed inset-y-0 left-0 z-50 w-64 bg-secondary text-white flex flex-col shadow-xl transform -translate-x-full lg:static lg:translate-x-0 transition-transform duration-300 ease-in-out">
            <div class="h-16 flex items-center justify-between border-b border-slate-700 px-4">
                <h1 class="text-xl font-bold tracking-wider">🔬 <span class="text-accent">NEX</span>LAB</h1>
                <button onclick="toggleSidebar()" class="lg:hidden text-slate-400 hover:text-white focus:outline-none">
                    <svg class="w-6 h-6" fill="none" stroke="currentColor" viewBox="0 0 24 24"><path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M6 18L18 6M6 6l12 12"></path></svg>
                </button>
            </div>
            
            <nav class="flex-1 overflow-y-auto py-4 custom-scrollbar">
                <!-- Main -->
                <ul class="space-y-1">
                    {% if has_page('dashboard') %}
                    <li>
                        <a href="/" class="sidebar-link flex items-center px-6 py-3 transition-colors {% if request.url.path == '/' %}active{% endif %}">
                            <svg class="w-5 h-5 mr-3" fill="none" stroke="currentColor" viewBox="0 0 24 24"><path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M4 6a2 2 0 012-2h2a2 2 0 012 2v2a2 2 0 01-2 2H6a2 2 0 01-2-2V6zM14 6a2 2 0 012-2h2a2 2 0 012 2v2a2 2 0 01-2 2h-2a2 2 0 01-2-2V6zM4 16a2 2 0 012-2h2a2 2 0 012 2v2a2 2 0 01-2 2H6a2 2 0 01-2-2v-2zM14 16a2 2 0 012-2h2a2 2 0 012 2v2a2 2 0 01-2 2h-2a2 2 0 01-2-2v-2z"></path></svg>
                            Dashboard
                        </a>
                    </li>
                    {% endif %}
                </ul>

                <!-- Patient Management -->
                {% if has_page('patient_registration') or has_page('patients') or has_page('deleted_patients') or has_page('deleted_tests') or has_page('patient_history') or has_page('patient_status') %}
                <div class="mt-4">
                    <button onclick="toggleSubMenu('patientMenu')" class="w-full flex items-center justify-between px-6 py-3 text-slate-300 hover:text-white hover:bg-slate-800 transition-all group">
                        <div class="flex items-center">
                            <svg class="w-5 h-5 mr-3 text-slate-400 group-hover:text-accent transition-colors" fill="none" stroke="currentColor" viewBox="0 0 24 24"><path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M12 4.354a4 4 0 110 5.292M15 21H3v-1a6 6 0 0112 0v1zm0 0h6v-1a6 6 0 00-9-5.197M13 7a4 4 0 11-8 0 4 4 0 018 0z"></path></svg>
                            <span class="text-sm font-medium">Patient Management</span>
                        </div>
                        <svg id="patientMenu-chevron" class="w-4 h-4 chevron" fill="none" stroke="currentColor" viewBox="0 0 24 24"><path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M19 9l-7 7-7-7"></path></svg>
                    </button>
                    <ul id="patientMenu" class="sidebar-submenu space-y-1">
                        {% if has_page('patient_registration') %}<li>
                            <a href="/patient-registration" class="sidebar-link flex items-center px-6 py-3 submenu-item transition-colors {% if request.url.path == '/patient-registration' %}active{% endif %}">
                                Patient Registration
                            </a>
                        </li>{% endif %}
                        {% if has_page('patients') %}<li>
                            <a href="/patients" class="sidebar-link flex items-center px-6 py-3 submenu-item transition-colors {% if request.url.path == '/patients' or request.url.path.startswith('/patients/edit') %}active{% endif %}">
                                Patients
                            </a>
                        </li>{% endif %}
                        {% if has_page('deleted_patients') %}<li>
                            <a href="/patients/deleted" class="sidebar-link flex items-center px-6 py-3 submenu-item transition-colors {% if request.url.path.startswith('/patients/deleted') or request.url.path.startswith('/patients/view-deleted') %}active{% endif %}">
                                Deleted Patients
                            </a>
                        </li>{% endif %}
                        {% if has_page('deleted_tests') %}<li>
                            <a href="/deleted_tests_page" class="sidebar-link flex items-center px-6 py-3 submenu-item transition-colors {% if request.url.path.startswith('/deleted_tests') %}active{% endif %}">
                                Deleted Tests
                            </a>
                        </li>{% endif %}
                        {% if has_page('patient_history') %}<li>
                            <a href="/patient-history" class="sidebar-link flex items-center px-6 py-3 submenu-item transition-colors {% if request.url.path.startswith('/patient-history') %}active{% endif %}">
                                Patient History
                            </a>
                        </li>{% endif %}
                        {% if has_page('patient_status') %}<li>
                            <a href="/patient-status" class="sidebar-link flex items-center px-6 py-3 submenu-item transition-colors {% if request.url.path.startswith('/patient-status') %}active{% endif %}">
                                Patient Status
                            </a>
                        </li>{% endif %}
                    </ul>
                </div>
                {% endif %}

                <!-- Results -->
                {% if has_page('result_entry') or has_page('double_auth') or has_page('no_sample') %}
                <div class="mt-2">
                    <button onclick="toggleSubMenu('resultsMenu')" class="w-full flex items-center justify-between px-6 py-3 text-slate-300 hover:text-white hover:bg-slate-800 transition-all group">
                        <div class="flex items-center">
                            <svg class="w-5 h-5 mr-3 text-slate-400 group-hover:text-accent transition-colors" fill="none" stroke="currentColor" viewBox="0 0 24 24"><path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2m-6 9l2 2 4-4"></path></svg>
                            <span class="text-sm font-medium">Results</span>
                        </div>
                        <svg id="resultsMenu-chevron" class="w-4 h-4 chevron" fill="none" stroke="currentColor" viewBox="0 0 24 24"><path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M19 9l-7 7-7-7"></path></svg>
                    </button>
                    <ul id="resultsMenu" class="sidebar-submenu space-y-1">
                        {% if has_page('result_entry') %}<li>
                            <a href="/result-entry" class="sidebar-link flex items-center px-6 py-3 submenu-item transition-colors {% if request.url.path.startswith('/result-entry') %}active{% endif %}">
                                Result Entry
                            </a>
                        </li>{% endif %}
                        {% if has_page('double_auth') %}<li>
                            <a href="/double-auth" class="sidebar-link flex items-center px-6 py-3 submenu-item transition-colors {% if request.url.path.startswith('/double-auth') %}active{% endif %}">
                                Double Authorisation
                            </a>
                        </li>{% endif %}
                        {% if has_page('no_sample') %}<li>
                            <a href="/no-sample" class="sidebar-link flex items-center px-6 py-3 submenu-item transition-colors {% if request.url.path.startswith('/no-sample') %}active{% endif %}">
                                No Sample
                            </a>
                        </li>{% endif %}
                    </ul>
                </div>
                {% endif %}

                <!-- Printing & Design -->
                {% if has_page('print_barcode') or has_page('barcode_designer') or has_page('print_receipt') or has_page('receipt_designer') or has_page('print_results') or has_page('call_centre') or has_page('result_designer') %}
                <div class="mt-2">
                    <button onclick="toggleSubMenu('printMenu')" class="w-full flex items-center justify-between px-6 py-3 text-slate-300 hover:text-white hover:bg-slate-800 transition-all group">
                        <div class="flex items-center">
                            <svg class="w-5 h-5 mr-3 text-slate-400 group-hover:text-accent transition-colors" fill="none" stroke="currentColor" viewBox="0 0 24 24"><path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M17 17h2.457a2 2 0 001.414-.586l.007-.007a2 2 0 000-2.828L12 4.685 3.121 13.565a2 2 0 000 2.828l.007.007a2 2 0 001.414.586H7m10 0a2 2 0 012 2v2a2 2 0 01-2 2H7a2 2 0 01-2-2v-2a2 2 0 012-2m10 0V7a3 3 0 00-3-3h-4a3 3 0 00-3 3v10"></path></svg>
                            <span class="text-sm font-medium">Printing & Design</span>
                        </div>
                        <svg id="printMenu-chevron" class="w-4 h-4 chevron" fill="none" stroke="currentColor" viewBox="0 0 24 24"><path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M19 9l-7 7-7-7"></path></svg>
                    </button>
                    <ul id="printMenu" class="sidebar-submenu space-y-1">
                        {% if has_page('print_barcode') %}<li>
                            <a href="/print-barcode" class="sidebar-link flex items-center px-6 py-3 submenu-item transition-colors {% if request.url.path == '/print-barcode' %}active{% endif %}">
                                Print Barcode
                            </a>
                        </li>{% endif %}
                        {% if has_page('barcode_designer') %}<li>
                            <a href="/print-barcode-designer" class="sidebar-link flex items-center px-6 py-3 submenu-item transition-colors {% if request.url.path == '/print-barcode-designer' %}active{% endif %}">
                                Barcode Designer
                            </a>
                        </li>{% endif %}
                        {% if has_page('print_receipt') %}<li>
                            <a href="/print-receipt" class="sidebar-link flex items-center px-6 py-3 submenu-item transition-colors {% if request.url.path == '/print-receipt' %}active{% endif %}">
                                Print Receipt
                            </a>
                        </li>{% endif %}
                        {% if has_page('receipt_designer') %}<li>
                            <a href="/print-receipt-designer" class="sidebar-link flex items-center px-6 py-3 submenu-item transition-colors {% if request.url.path == '/print-receipt-designer' %}active{% endif %}">
                                Receipt Designer
                            </a>
                        </li>{% endif %}
                        {% if has_page('print_results') %}<li>
                            <a href="/print-results" class="sidebar-link flex items-center px-6 py-3 submenu-item transition-colors {% if request.url.path == '/print-results' %}active{% endif %}">
                                Print Results
                            </a>
                        </li>{% endif %}
                        {% if has_page('call_centre') %}<li>
                            <a href="/call-centre" class="sidebar-link flex items-center px-6 py-3 submenu-item transition-colors {% if request.url.path == '/call-centre' %}active{% endif %}">
                                Call Centre
                            </a>
                        </li>{% endif %}
                        {% if has_page('result_designer') %}<li>
                            <a href="/print-result-designer" class="sidebar-link flex items-center px-6 py-3 submenu-item transition-colors {% if request.url.path == '/print-result-designer' %}active{% endif %}">
                                Result Designer
                            </a>
                        </li>{% endif %}
                    </ul>
                </div>
                {% endif %}

                <!-- Lab Setup -->
                {% if has_page('departments') or has_page('sample_types') or has_page('devices') or has_page('parameters') or has_page('report_notes') %}
                <div class="mt-2">
                    <button onclick="toggleSubMenu('setupMenu')" class="w-full flex items-center justify-between px-6 py-3 text-slate-300 hover:text-white hover:bg-slate-800 transition-all group">
                        <div class="flex items-center">
                            <svg class="w-5 h-5 mr-3 text-slate-400 group-hover:text-accent transition-colors" fill="none" stroke="currentColor" viewBox="0 0 24 24"><path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M10.325 4.317c.426-1.756 2.924-1.756 3.35 0a1.724 1.724 0 002.573 1.066c1.543-.94 3.31.826 2.37 2.37a1.724 1.724 0 001.065 2.572c1.756.426 1.756 2.924 0 3.35a1.724 1.724 0 00-1.066 2.573c.94 1.543-.826 3.31-2.37 2.37a1.724 1.724 0 00-2.572 1.065c-.426 1.756-2.924 1.756-3.35 0a1.724 1.724 0 00-2.573-1.066c-1.543.94-3.31-.826-2.37-2.37a1.724 1.724 0 00-1.065-2.572c-1.756-.426-1.756-2.924 0-3.35a1.724 1.724 0 001.066-2.573c-.94-1.543.826-3.31 2.37-2.37.996.608 2.296.07 2.572-1.065z"></path><path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M15 12a3 3 0 11-6 0 3 3 0 016 0z"></path></svg>
                            <span class="text-sm font-medium">Lab Setup</span>
                        </div>
                        <svg id="setupMenu-chevron" class="w-4 h-4 chevron" fill="none" stroke="currentColor" viewBox="0 0 24 24"><path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M19 9l-7 7-7-7"></path></svg>
                    </button>
                    <ul id="setupMenu" class="sidebar-submenu space-y-1">
                        {% if has_page('departments') %}<li>
                            <a href="/departments" class="sidebar-link flex items-center px-6 py-3 submenu-item transition-colors {% if request.url.path == '/departments' %}active{% endif %}">
                                Departments
                            </a>
                        </li>{% endif %}
                        {% if has_page('sample_types') %}<li>
                            <a href="/sample-types" class="sidebar-link flex items-center px-6 py-3 submenu-item transition-colors {% if request.url.path == '/sample-types' %}active{% endif %}">
                                Sample Types
                            </a>
                        </li>{% endif %}
                        {% if has_page('devices') %}<li>
                            <a href="/devices" class="sidebar-link flex items-center px-6 py-3 submenu-item transition-colors {% if request.url.path == '/devices' %}active{% endif %}">
                                Devices
                            </a>
                        </li>{% endif %}
                        {% if has_page('parameters') %}<li>
                            <a href="/parameters" class="sidebar-link flex items-center px-6 py-3 submenu-item transition-colors {% if request.url.path == '/parameters' %}active{% endif %}">
                                Parameters
                            </a>
                        </li>{% endif %}
                        {% if has_page('report_notes') %}<li>
                            <a href="/report-notes" class="sidebar-link flex items-center px-6 py-3 submenu-item transition-colors {% if request.url.path == '/report-notes' %}active{% endif %}">
                                Report Notes
                            </a>
                        </li>{% endif %}
                        {% if has_page('supplies') %}<li>
                            <a href="/supplies" class="sidebar-link flex items-center px-6 py-3 submenu-item transition-colors {% if request.url.path == '/supplies' %}active{% endif %}">
                                Supplies
                            </a>
                        </li>{% endif %}
                        {% if has_page('inventory') %}<li>
                            <a href="/inventory" class="sidebar-link flex items-center px-6 py-3 submenu-item transition-colors {% if request.url.path == '/inventory' %}active{% endif %}">
                                Inventory
                            </a>
                        </li>{% endif %}
                        {% if has_page('inventory') or has_page('tests') %}<li>
                            <a href="/cal-control" class="sidebar-link flex items-center px-6 py-3 submenu-item transition-colors {% if request.url.path == '/cal-control' %}active{% endif %}">
                                Cal & Control
                            </a>
                        </li>{% endif %}
                    </ul>
                </div>
                {% endif %}

                <!-- Tests & Packages -->
                {% if has_page('tests') or has_page('formulas') or has_page('test_ranges') or has_page('test_result_types') or has_page('packages') %}
                <div class="mt-2">
                    <button onclick="toggleSubMenu('testsMenu')" class="w-full flex items-center justify-between px-6 py-3 text-slate-300 hover:text-white hover:bg-slate-800 transition-all group">
                        <div class="flex items-center">
                            <svg class="w-5 h-5 mr-3 text-slate-400 group-hover:text-accent transition-colors" fill="none" stroke="currentColor" viewBox="0 0 24 24"><path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2m-6 9l2 2 4-4"></path></svg>
                            <span class="text-sm font-medium">Tests & Packages</span>
                        </div>
                        <svg id="testsMenu-chevron" class="w-4 h-4 chevron" fill="none" stroke="currentColor" viewBox="0 0 24 24"><path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M19 9l-7 7-7-7"></path></svg>
                    </button>
                    <ul id="testsMenu" class="sidebar-submenu space-y-1">
                        {% if has_page('tests') %}<li>
                            <a href="/tests" class="sidebar-link flex items-center px-6 py-3 submenu-item transition-colors {% if request.url.path == '/tests' %}active{% endif %}">
                                Tests
                            </a>
                        </li>{% endif %}
                        {% if has_page('formulas') %}<li>
                            <a href="/formulas" class="sidebar-link flex items-center px-6 py-3 submenu-item transition-colors {% if request.url.path == '/formulas' %}active{% endif %}">
                                Formulas
                            </a>
                        </li>{% endif %}
                        {% if has_page('test_ranges') %}<li>
                            <a href="/test-ranges" class="sidebar-link flex items-center px-6 py-3 submenu-item transition-colors {% if request.url.path == '/test-ranges' %}active{% endif %}">
                                Test Ranges
                            </a>
                        </li>{% endif %}
                        {% if has_page('test_result_types') %}<li>
                            <a href="/test-result-types" class="sidebar-link flex items-center px-6 py-3 submenu-item transition-colors {% if request.url.path == '/test-result-types' %}active{% endif %}">
                                Result Types
                            </a>
                        </li>{% endif %}
                        {% if has_page('packages') %}<li>
                            <a href="/packages" class="sidebar-link flex items-center px-6 py-3 submenu-item transition-colors {% if request.url.path == '/packages' %}active{% endif %}">
                                Packages
                            </a>
                        </li>{% endif %}
                    </ul>
                </div>
                {% endif %}

                <!-- Financials -->
                {% if has_page('expenses_types') or has_page('expenses_entry') %}
                <div class="mt-2">
                    <button onclick="toggleSubMenu('financialsMenu')" class="w-full flex items-center justify-between px-6 py-3 text-slate-300 hover:text-white hover:bg-slate-800 transition-all group">
                        <div class="flex items-center">
                            <svg class="w-5 h-5 mr-3 text-slate-400 group-hover:text-accent transition-colors" fill="none" stroke="currentColor" viewBox="0 0 24 24"><path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M12 8c-1.657 0-3 .895-3 2s1.343 2 3 2 3 .895 3 2-1.343 2-3 2m0-8c1.11 0 2.08.402 2.599 1M12 8V7m0 1v8m0 0v1m0-1c-1.11 0-2.08-.402-2.599-1M21 12a9 9 0 11-18 0 9 9 0 0118 0z"></path></svg>
                            <span class="text-sm font-medium">Financials</span>
                        </div>
                        <svg id="financialsMenu-chevron" class="w-4 h-4 chevron" fill="none" stroke="currentColor" viewBox="0 0 24 24"><path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M19 9l-7 7-7-7"></path></svg>
                    </button>
                    <ul id="financialsMenu" class="sidebar-submenu space-y-1">
                        {% if has_page('expenses_types') %}<li>
                            <a href="/expenses-types" class="sidebar-link flex items-center px-6 py-3 submenu-item transition-colors {% if request.url.path == '/expenses-types' %}active{% endif %}">
                                Expenses Types
                            </a>
                        </li>{% endif %}
                        {% if has_page('expenses_entry') %}<li>
                            <a href="/expenses-entry" class="sidebar-link flex items-center px-6 py-3 submenu-item transition-colors {% if request.url.path == '/expenses-entry' %}active{% endif %}">
                                Expenses Entry
                            </a>
                        </li>{% endif %}
                    </ul>
                </div>
                {% endif %}

                <!-- Reports -->
                {% if has_page('detailed_income') or has_page('net_amounts') or has_page('payment_records') or has_page('patients_number') or has_page('patient_region') or has_page('tests_number') or has_page('discount_report') or has_page('expenses_report') or has_page('remain_report') %}
                <div class="mt-2">
                    <button onclick="toggleSubMenu('reportsMenu')" class="w-full flex items-center justify-between px-6 py-3 text-slate-300 hover:text-white hover:bg-slate-800 transition-all group">
                        <div class="flex items-center">
                            <svg class="w-5 h-5 mr-3 text-slate-400 group-hover:text-accent transition-colors" fill="none" stroke="currentColor" viewBox="0 0 24 24"><path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M9 17v-2m3 2v-4m3 4v-6m2 10H7a2 2 0 01-2-2V5a2 2 0 012-2h5.586a1 1 0 01.707.293l5.414 5.414a1 1 0 01.293.707V19a2 2 0 01-2 2z"></path></svg>
                            <span class="text-sm font-medium">Reports</span>
                        </div>
                        <svg id="reportsMenu-chevron" class="w-4 h-4 chevron" fill="none" stroke="currentColor" viewBox="0 0 24 24"><path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M19 9l-7 7-7-7"></path></svg>
                    </button>
                    <ul id="reportsMenu" class="sidebar-submenu space-y-1">
                        {% if has_page('discount_report') %}<li>
                            <a href="/reports/discount" class="sidebar-link flex items-center px-6 py-3 submenu-item transition-colors {% if request.url.path == '/reports/discount' %}active{% endif %}">
                                Discount Report
                            </a>
                        </li>{% endif %}
                        {% if has_page('detailed_income') %}<li>
                            <a href="/reports/detailed-income" class="sidebar-link flex items-center px-6 py-3 submenu-item transition-colors {% if request.url.path == '/reports/detailed-income' %}active{% endif %}">
                                Detailed Income
                            </a>
                        </li>{% endif %}
                        {% if has_page('net_amounts') %}<li>
                            <a href="/reports/net-amounts" class="sidebar-link flex items-center px-6 py-3 submenu-item transition-colors {% if request.url.path == '/reports/net-amounts' %}active{% endif %}">
                                Net Amounts
                            </a>
                        </li>{% endif %}
                        {% if has_page('payment_records') %}<li>
                            <a href="/reports/payment-records" class="sidebar-link flex items-center px-6 py-3 submenu-item transition-colors {% if request.url.path == '/reports/payment-records' %}active{% endif %}">
                                Payment Records
                            </a>
                        </li>{% endif %}
                        {% if has_page('patients_number') %}<li>
                            <a href="/reports/patients-number" class="sidebar-link flex items-center px-6 py-3 submenu-item transition-colors {% if request.url.path == '/reports/patients-number' %}active{% endif %}">
                                Patients Number
                            </a>
                        </li>{% endif %}
                        {% if has_page('patient_region') %}<li>
                            <a href="/reports/patient-region" class="sidebar-link flex items-center px-6 py-3 submenu-item transition-colors {% if request.url.path == '/reports/patient-region' %}active{% endif %}">
                                Patient Region
                            </a>
                        </li>{% endif %}
                        {% if has_page('tests_number') %}<li>
                            <a href="/reports/tests-number" class="sidebar-link flex items-center px-6 py-3 submenu-item transition-colors {% if request.url.path == '/reports/tests-number' %}active{% endif %}">
                                Test Number
                            </a>
                        </li>{% endif %}
                        {% if has_page('expenses_report') %}<li>
                            <a href="/reports/expenses" class="sidebar-link flex items-center px-6 py-3 submenu-item transition-colors {% if request.url.path == '/reports/expenses' %}active{% endif %}">
                                Expenses Report
                            </a>
                        </li>{% endif %}
                        {% if has_page('remain_report') %}<li>
                            <a href="/reports/remain" class="sidebar-link flex items-center px-6 py-3 submenu-item transition-colors {% if request.url.path == '/reports/remain' %}active{% endif %}">
                                Remain
                            </a>
                        </li>{% endif %}
                    </ul>
                </div>
                {% endif %}

                <!-- Geography & Partners -->
                {% if has_page('partners') or has_page('provinces') or has_page('regions') %}
                <div class="mt-2">
                    <button onclick="toggleSubMenu('geoMenu')" class="w-full flex items-center justify-between px-6 py-3 text-slate-300 hover:text-white hover:bg-slate-800 transition-all group">
                        <div class="flex items-center">
                            <svg class="w-5 h-5 mr-3 text-slate-400 group-hover:text-accent transition-colors" fill="none" stroke="currentColor" viewBox="0 0 24 24"><path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M17.657 16.657L13.414 20.9a1.998 1.998 0 01-2.827 0l-4.244-4.243a8 8 0 1111.314 0z"></path><path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M15 11a3 3 0 11-6 0 3 3 0 016 0z"></path></svg>
                            <span class="text-sm font-medium">Geography & Partners</span>
                        </div>
                        <svg id="geoMenu-chevron" class="w-4 h-4 chevron" fill="none" stroke="currentColor" viewBox="0 0 24 24"><path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M19 9l-7 7-7-7"></path></svg>
                    </button>
                    <ul id="geoMenu" class="sidebar-submenu space-y-1">
                        {% if has_page('partners') %}<li>
                            <a href="/partners" class="sidebar-link flex items-center px-6 py-3 submenu-item transition-colors {% if request.url.path == '/partners' %}active{% endif %}">
                                Partners
                            </a>
                        </li>{% endif %}
                        {% if has_page('provinces') %}<li>
                            <a href="/provinces" class="sidebar-link flex items-center px-6 py-3 submenu-item transition-colors {% if request.url.path == '/provinces' %}active{% endif %}">
                                Provinces
                            </a>
                        </li>{% endif %}
                        {% if has_page('regions') %}<li>
                            <a href="/regions" class="sidebar-link flex items-center px-6 py-3 submenu-item transition-colors {% if request.url.path == '/regions' %}active{% endif %}">
                                Regions
                            </a>
                        </li>{% endif %}
                    </ul>
                </div>
                {% endif %}

                <!-- HR & Staff -->
                <div class="mt-2">
                    <button onclick="toggleSubMenu('hrMenu')" class="w-full flex items-center justify-between px-6 py-3 text-slate-300 hover:text-white hover:bg-slate-800 transition-all group">
                        <div class="flex items-center">
                            <svg class="w-5 h-5 mr-3 text-slate-400 group-hover:text-accent transition-colors" fill="none" stroke="currentColor" viewBox="0 0 24 24"><path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M12 4.354a4 4 0 110 5.292M15 21H3v-1a6 6 0 0112 0v1zm0 0h6v-1a6 6 0 00-9-5.197M13 7a4 4 0 11-8 0 4 4 0 018 0z"></path></svg>
                            <span class="text-sm font-medium">HR & Staff</span>
                        </div>
                        <svg id="hrMenu-chevron" class="w-4 h-4 chevron" fill="none" stroke="currentColor" viewBox="0 0 24 24"><path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M19 9l-7 7-7-7"></path></svg>
                    </button>
                    <ul id="hrMenu" class="sidebar-submenu space-y-1">
                        <li>
                            <a href="/hr" class="sidebar-link flex items-center px-6 py-3 submenu-item transition-colors {% if request.url.path.startswith('/hr') %}active{% endif %}">
                                Employees
                            </a>
                        </li>
                    </ul>
                </div>

                <!-- Settings -->
                {% if has_page('lab_info') or has_page('user_management') or has_page('activity_logs') %}
                <div class="mt-2">
                    <button onclick="toggleSubMenu('settingsMenu')" class="w-full flex items-center justify-between px-6 py-3 text-slate-300 hover:text-white hover:bg-slate-800 transition-all group">
                        <div class="flex items-center">
                            <svg class="w-5 h-5 mr-3 text-slate-400 group-hover:text-accent transition-colors" fill="none" stroke="currentColor" viewBox="0 0 24 24"><path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M10.325 4.317c.426-1.756 2.924-1.756 3.35 0a1.724 1.724 0 002.573 1.066c1.543-.94 3.31.826 2.37 2.37a1.724 1.724 0 001.065 2.572c1.756.426 1.756 2.924 0 3.35a1.724 1.724 0 00-1.066 2.573c.94 1.543-.826 3.31-2.37 2.37a1.724 1.724 0 00-2.572 1.065c-.426 1.756-2.924 1.756-3.35 0a1.724 1.724 0 00-2.573-1.066c-1.543.94-3.31-.826-2.37-2.37a1.724 1.724 0 00-1.065-2.572c-1.756-.426-1.756-2.924 0-3.35a1.724 1.724 0 001.066-2.573c-.94-1.543.826-3.31 2.37-2.37.996.608 2.296.07 2.572-1.065z"></path><path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M15 12a3 3 0 11-6 0 3 3 0 016 0z"></path></svg>
                            <span class="text-sm font-medium">Settings</span>
                        </div>
                        <svg id="settingsMenu-chevron" class="w-4 h-4 chevron" fill="none" stroke="currentColor" viewBox="0 0 24 24"><path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M19 9l-7 7-7-7"></path></svg>
                    </button>
                    <ul id="settingsMenu" class="sidebar-submenu space-y-1">
                        {% if has_page('lab_info') %}<li>
                            <a href="/lab-info" class="sidebar-link flex items-center px-6 py-3 submenu-item transition-colors {% if request.url.path == '/lab-info' %}active{% endif %}">
                                Lab Info
                            </a>
                        </li>{% endif %}
                        {% if has_page('user_management') %}<li>
                            <a href="/users" class="sidebar-link flex items-center px-6 py-3 submenu-item transition-colors {% if request.url.path == '/users' %}active{% endif %}">
                                User Management
                            </a>
                        </li>{% endif %}
                        {% if has_page('activity_logs') %}<li>
                            <a href="/activity-logs" class="sidebar-link flex items-center px-6 py-3 submenu-item transition-colors {% if request.url.path == '/activity-logs' %}active{% endif %}">
                                Activity Logs
                            </a>
                        </li>{% endif %}
                    </ul>
                </div>
                {% endif %}
            </nav>
            
            <div class="p-4 border-t border-slate-700">
                <a href="/logout" class="flex items-center px-4 py-2 text-red-400 hover:text-red-300 transition-colors">
                    <svg class="w-5 h-5 mr-3" fill="none" stroke="currentColor" viewBox="0 0 24 24"><path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M17 16l4-4m0 0l-4-4m4 4H7m6 4v1a3 3 0 01-3 3H6a3 3 0 01-3-3V7a3 3 0 013-3h4a3 3 0 013 3v1"></path></svg>
                    Logout
                </a>
            </div>
        </aside>

        <!-- Main Content -->
        <div class="flex-1 flex flex-col overflow-hidden">
            <!-- Topbar -->
            <header class="h-16 bg-white shadow-sm flex items-center justify-between px-4 lg:px-6 z-10 relative">
                <div class="flex items-center">
                    <button id="sidebarToggle" onclick="toggleSidebar()" class="text-slate-500 hover:text-slate-700 focus:outline-none lg:hidden mr-3">
                        <svg class="w-6 h-6" fill="none" stroke="currentColor" viewBox="0 0 24 24"><path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M4 6h16M4 12h16M4 18h16"></path></svg>
                    </button>
                    <h2 class="text-base lg:text-lg font-semibold text-slate-700 truncate max-w-[120px] sm:max-w-[200px] md:max-w-none">{% block page_title %}Dashboard{% endblock %}</h2>
                </div>
                
                <!-- Center: Lab Name -->
                <div class="absolute left-1/2 -translate-x-1/2 hidden md:block">
                    {% if global_lab_name %}
                    <div class="bg-indigo-600 text-white px-5 py-1.5 rounded-lg text-sm font-bold tracking-wide shadow-sm border border-indigo-700 whitespace-nowrap">
                        🔬 {{ global_lab_name }}
                    </div>
                    {% endif %}
                </div>
                
                <div class="flex items-center space-x-4">
                    <!-- Chat Widget Toggle -->
                    {% if current_user %}
                    <button onclick="toggleChatWidget()" class="relative p-2 text-slate-500 hover:text-primary transition-colors focus:outline-none" title="Messages">
                        <svg class="w-6 h-6" fill="none" stroke="currentColor" viewBox="0 0 24 24"><path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M8 12h.01M12 12h.01M16 12h.01M21 12c0 4.418-4.03 8-9 8a9.863 9.863 0 01-4.255-.949L3 20l1.395-3.72C3.512 15.042 3 13.574 3 12c0-4.418 4.03-8 9-8s9 3.582 9 8z"></path></svg>
                        <span id="globalUnreadBadge" class="absolute top-0 right-0 inline-flex items-center justify-center px-1.5 py-0.5 text-[10px] font-bold leading-none text-white bg-red-500 rounded-full hidden border-2 border-white shadow-sm transform translate-x-1 -translate-y-1">0</span>
                    </button>
                    {% endif %}
                    
                    <div class="flex items-center space-x-2 border-l border-slate-200 pl-4">
                        <div class="w-8 h-8 rounded-full bg-primary text-white flex items-center justify-center font-bold">{{ current_user.full_name[0]|upper if current_user else 'A' }}</div>
                        <span class="text-sm font-medium text-slate-600">{{ current_user.full_name if current_user else 'Admin' }}</span>
                    </div>
                </div>
            </header>

            <!-- Page Content -->
            <main class="flex-1 overflow-x-hidden overflow-y-auto bg-background p-4 lg:p-6">
                {% block content %}{% endblock %}
            </main>
        </div>
    </div>

    <!-- Modal Popup -->
    <div id="modalOverlay" class="modal-overlay">
        <div class="modal-content">
            <div id="modalIcon" class="modal-icon success">
                <svg fill="none" stroke="currentColor" viewBox="0 0 24 24">
                    <path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M5 13l4 4L19 7"></path>
                </svg>
            </div>
            <h3 id="modalTitle" class="text-2xl font-bold mb-2">Success!</h3>
            <p id="modalMessage" class="text-slate-600 mb-6">Operation completed successfully.</p>
            <button id="modalButton" onclick="closeModal()" class="w-full py-3 px-4 rounded-lg font-medium transition duration-200">
                Continue
            </button>
        </div>
    </div>

    <!-- Confirmation Modal -->
    <div id="confirmModalOverlay" class="modal-overlay">
        <div class="modal-content">
            <div class="modal-icon warning">
                <svg fill="none" stroke="currentColor" viewBox="0 0 24 24">
                    <path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M12 9v2m0 4h.01m-6.938 4h13.856c1.54 0 2.502-1.667 1.732-3L13.732 4c-.77-1.333-2.694-1.333-3.464 0L3.34 16c-.77 1.333.192 3 1.732 3z"></path>
                </svg>
            </div>
            <h3 class="text-2xl font-bold mb-2 text-slate-800">Confirm Delete</h3>
            <p id="confirmMessage" class="text-slate-600 mb-6">Are you sure you want to delete this item? This action cannot be undone.</p>
            <div class="flex gap-3">
                <button id="confirmCancel" onclick="closeConfirmModal()" class="flex-1 py-3 px-4 rounded-lg font-medium transition duration-200 bg-slate-200 text-slate-700 hover:bg-slate-300">
                    Cancel
                </button>
                <button id="confirmConfirm" class="flex-1 py-3 px-4 rounded-lg font-medium transition duration-200 bg-red-600 text-white hover:bg-red-700">
                    Delete
                </button>
            </div>
        </div>
    </div>

    <script>

        // Sidebar Toggle

        function toggleSidebar() {

            const sidebar = document.getElementById('sidebar');

            const overlay = document.getElementById('sidebarOverlay');

            const isClosed = sidebar.classList.contains('-translate-x-full');

            

            if (isClosed) {

                sidebar.classList.remove('-translate-x-full');

                overlay.classList.remove('hidden');

                document.body.style.overflow = 'hidden'; // Prevent background scroll

            } else {

                sidebar.classList.add('-translate-x-full');

                overlay.classList.add('hidden');

                document.body.style.overflow = '';

            }

        }



        // Close sidebar on escape key

        document.addEventListener('keydown', (e) => {

            if (e.key === 'Escape' && !document.getElementById('sidebar').classList.contains('-translate-x-full')) {

                toggleSidebar();

            }

        });



        // Modal Functions

        function showModal(type, title, message, callback = null) {

            const overlay = document.getElementById('modalOverlay');

            const icon = document.getElementById('modalIcon');

            const titleEl = document.getElementById('modalTitle');

            const messageEl = document.getElementById('modalMessage');

            const button = document.getElementById('modalButton');

            

            icon.className = 'modal-icon ' + type;

            if (type === 'success') {

                button.className = 'w-full py-3 px-4 rounded-lg font-medium transition duration-200 bg-green-600 text-white hover:bg-green-700';

                icon.innerHTML = '<svg fill="none" stroke="currentColor" viewBox="0 0 24 24"><path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M5 13l4 4L19 7"></path></svg>';

            } else if (type === 'error') {

                button.className = 'w-full py-3 px-4 rounded-lg font-medium transition duration-200 bg-red-600 text-white hover:bg-red-700';

                icon.innerHTML = '<svg fill="none" stroke="currentColor" viewBox="0 0 24 24"><path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M6 18L18 6M6 6l12 12"></path></svg>';

            }

            

            titleEl.textContent = title;

            messageEl.textContent = message;

            overlay.classList.add('active');

            

            if (callback) {

                button.onclick = function() {

                    closeModal();

                    callback();

                };

            } else {

                button.onclick = closeModal;

            }

        }

        

        function closeModal() {

            document.getElementById('modalOverlay').classList.remove('active');

        }

        

        function showConfirmModal(message, onConfirm) {

            const overlay = document.getElementById('confirmModalOverlay');

            const messageEl = document.getElementById('confirmMessage');

            const confirmBtn = document.getElementById('confirmConfirm');

            const cancelBtn = document.getElementById('confirmCancel');

            

            messageEl.textContent = message;

            overlay.classList.add('active');

            

            confirmBtn.onclick = function() {

                closeConfirmModal();

                if (onConfirm) onConfirm();

            };

            

            cancelBtn.onclick = closeConfirmModal;

        }

        

        function closeConfirmModal() {

            document.getElementById('confirmModalOverlay').classList.remove('active');

        }

        

        // Check for URL parameters on page load

        window.addEventListener('DOMContentLoaded', function() {

            const urlParams = new URLSearchParams(window.location.search);

            const success = urlParams.get('success');

            const error = urlParams.get('error');

            

            if (success) {

                showModal('success', 'Success!', decodeURIComponent(success));

                window.history.replaceState({}, document.title, window.location.pathname);

            } else if (error) {

                showModal('error', 'Error!', decodeURIComponent(error));

                window.history.replaceState({}, document.title, window.location.pathname);

            }

        });

        

        // Replace confirm() with modal

        function confirmDelete(url) {

            showConfirmModal('Are you sure you want to delete this item? This action cannot be undone.', function() {

                var form = document.createElement('form');

                form.method = 'POST';

                form.action = url;

                form.style.display = 'none';

                document.body.appendChild(form);

                form.submit();

            });

        }

    </script>
    
    <!-- Sidebar Scripts -->
    <script>

        function toggleSidebar() {

            const sidebar = document.getElementById('sidebar');

            const overlay = document.getElementById('sidebarOverlay');

            sidebar.classList.toggle('-translate-x-full');

            overlay.classList.toggle('hidden');

        }



        function toggleSubMenu(id) {

            const menu = document.getElementById(id);

            const chevron = document.getElementById(id + '-chevron');

            

            if (menu) menu.classList.toggle('open');

            if (chevron) chevron.classList.toggle('rotate');

        }



        document.addEventListener('DOMContentLoaded', () => {

            // Auto-expand active menu

            const activeLink = document.querySelector('.sidebar-link.active');

            if (activeLink) {

                const subMenu = activeLink.closest('.sidebar-submenu');

                if (subMenu) {

                    subMenu.classList.add('open');

                    const chevron = document.getElementById(subMenu.id + '-chevron');

                    if (chevron) chevron.classList.add('rotate');

                }

            }



            // Escape to close sidebar

            document.addEventListener('keydown', (e) => {

                if (e.key === 'Escape') {

                    const sidebar = document.getElementById('sidebar');

                    if (sidebar && !sidebar.classList.contains('-translate-x-full')) {

                        toggleSidebar();

                    }

                }

            });

        });

    </script>

    <!-- Excel Export Utils -->
    <script>

        function exportTableToExcel(btnElement) {

            let table = btnElement.closest('table');

            if (!table) return;



            // Get a filename context from the h2 or default

            let titleEl = document.querySelector('h2');

            let baseName = titleEl ? titleEl.innerText.trim().replace(/[^a-zA-Z0-9\u0600-\u06FF\s_]/g, '') : 'Export';

            let filename = baseName.replace(/\s+/g, '_') + '.xls';



            let clonedTable = table.cloneNode(true);



            // Remove elements not needed in Excel (e.g. actions dropdowns, icons, no-excel)

            clonedTable.querySelectorAll('.no-excel, button, input:not([type="text"]), i, a, svg').forEach(el => el.remove());



            // Handle text inputs appropriately 

            clonedTable.querySelectorAll('input[type="text"]').forEach(el => {

                let parent = el.parentNode;

                parent.innerText = el.value || '';

            });



            // Convert to Excel HTML

            let html = clonedTable.outerHTML;

            let dataType = 'application/vnd.ms-excel';



            // HTML wrapper with meta charset for correct Arabic UTF-8 Encoding

            let tableHtml = `<html xmlns:o="urn:schemas-microsoft-com:office:office" xmlns:x="urn:schemas-microsoft-com:office:excel" xmlns="http://www.w3.org/TR/REC-html40">

            <head><meta charset="utf-8"/></head><body>${html}</body></html>`;



            let downloadLink = document.createElement("a");

            document.body.appendChild(downloadLink);



            if (navigator.msSaveOrOpenBlob) {

                let blob = new Blob(['\ufeff', tableHtml], { type: dataType });

                navigator.msSaveOrOpenBlob(blob, filename);

            } else {

                // btoa + unescape + encodeURIComponent trick for full UTF-8 base64 encoding support

                downloadLink.href = 'data:' + dataType + ';base64,' + btoa(unescape(encodeURIComponent(tableHtml)));

                downloadLink.download = filename;

                downloadLink.click();

            }

            document.body.removeChild(downloadLink);

        }



        document.addEventListener('DOMContentLoaded', function() {

            const currentPath = window.location.pathname;

            

            // Exclude logic

            let isExcluded = false;

            const startsWithExcludes = [

                '/patient-registration',

                '/result-entry',

                '/double-auth',

                '/call-centre',

                '/patient-history',

                '/users',

                '/activity-logs',

                '/no-sample',

                '/print',

                '/view-results',

                '/patient-status',

                '/view-call-centre'

            ];

            

            if (startsWithExcludes.some(p => currentPath.startsWith(p))) {

                isExcluded = true;

            }

            if (currentPath === '/patients' || currentPath.startsWith('/patients/edit')) {

                isExcluded = true;

            }



            if (!isExcluded) {

                document.querySelectorAll('table').forEach((table) => {

                    let theadTr = table.querySelector('thead tr');

                    if (!theadTr) return;

                    

                    let lastTh = theadTr.lastElementChild;

                    if (lastTh && lastTh.tagName === 'TH') { 

                        let btn = document.createElement('button');

                        btn.className = 'absolute right-2 top-1/2 -translate-y-1/2 bg-blue-500 hover:bg-blue-600 text-white shadow-sm flex items-center justify-center w-7 h-7 rounded no-excel transition-transform hover:scale-105 active:scale-95 z-10';

                        btn.innerHTML = `<svg class="w-4 h-4 text-white pointer-events-none" fill="none" stroke="currentColor" viewBox="0 0 24 24" xmlns="http://www.w3.org/2000/svg"><path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M4 16v1a3 3 0 003 3h10a3 3 0 003-3v-1m-4-4l-4 4m0 0l-4-4m4 4V4"></path></svg>`;

                        btn.title = 'Download Table to Excel';

                        btn.type = 'button';

                        btn.onclick = function(e) {

                            e.stopPropagation();

                            e.preventDefault();

                            exportTableToExcel(this);

                        };

                        

                        lastTh.style.position = 'relative'; // Ensure button correctly pins to TH bounds

                        // Prevent overlap with actual column text padding

                        const paddingRight = parseFloat(window.getComputedStyle(lastTh).paddingRight);

                        if (paddingRight < 36) {

                            lastTh.style.paddingRight = '36px';

                        }



                        lastTh.appendChild(btn);

                    }

                });

            }

        });

    </script>
    
    {% block scripts %}{% endblock %}
    
    {% if current_user %}
    {% include 'chat_widget.html' %}
    {% endif %}
    
</body>
</html>