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

Key Responsibilities:
- Application settings persistence (prompts, model parameters)
- User mode management (user/advanced/developer visibility levels)
- Offline mode configuration and model caching
- Dynamic list management (vaccine types, pharmacy labels)
- Crew login credentials management
- Auto-save with debouncing
- Live status indicators in section headers
- Default dataset export/import

Settings Architecture:
---------------------
Settings are stored in settings.json on the backend and cached in window.CACHED_SETTINGS
on the frontend. Auto-save triggers 800ms after user input to minimize write operations.

User Modes:
1. USER MODE: Simplified interface, hides advanced configuration
   - Basic prompts visible
   - Simple status indicators
   - Essential functionality only

2. ADVANCED MODE (Default): Shows model parameters and detailed configuration
   - Temperature, token limits, top-p visible
   - Detailed status with metrics
   - Import/export features

3. DEVELOPER MODE: Full debugging and development features
   - All dev-tag elements visible
   - Raw data views
   - Debug logging
   - Developer-only sections

Offline Mode System:
-------------------
Enables complete offline operation for remote sailing:

1. Model Caching:
   - medgemma-1.5-4b-it (primary triage/inquiry model)
   - medgemma-27b-text-it (advanced model for complex cases)
   - (No photo/OCR models required)

2. Environment Flags:
   - HF_HUB_OFFLINE=1: Disables Hugging Face Hub connectivity checks
   - TRANSFORMERS_OFFLINE=1: Prevents transformer model downloads

3. Workflow:
   - While online: Download all required models
   - Before departure: Create cache backup
   - At sea: Enable offline flags, verify cache status
   - If corruption: Restore from backup

4. Cache Management:
   - Check cache status: Verifies all models present
   - Download missing: Fetches any missing models (requires internet)
   - Backup cache: Creates timestamped backup
   - Restore backup: Recovers from latest backup

Dynamic Lists:
-------------
Vaccine types and pharmacy labels are user-customizable lists that populate
dropdowns throughout the application. Changes sync immediately across all modules.

- Vaccine Types: Used in crew.js for vaccine record tracking
- Pharmacy Labels: Used in pharmacy.js for medication categorization
- Duplicate prevention and normalization on save
- Reorderable with up/down buttons

Crew Credentials:
----------------
Allows setting per-crew-member login credentials for future multi-user support.
Currently informational, will enable authentication in future releases.

Data Flow:
- Settings: /api/data/settings (settings.json)
- Crew Credentials: /api/crew/credentials
- Offline Status: /api/offline/check, /api/offline/ensure
- Default Export: /api/default/export

Integration Points:
- pharmacy.js: Pharmacy labels
- crew.js: Vaccine types
- chat.js: Triage/inquiry prompts, model parameters
- main.js: User mode visibility, initialization
*/

const DEFAULT_SETTINGS = {
    triage_instruction: "Act as Lead Clinician. Priority: Life-saving protocols. Format: ## ASSESSMENT, ## PROTOCOL.",
    inquiry_instruction: "Act as Medical Librarian. Focus: Academic research and pharmacology.",
    tr_temp: 0.1,
    tr_tok: 1024,
    tr_p: 0.9,
    tr_k: 50,
    in_temp: 0.6,
    in_tok: 2048,
    in_p: 0.95,
    in_k: 50,
    mission_context: "Isolated Medical Station offshore.",
    rep_penalty: 1.1,
    user_mode: "advanced",
    last_prompt_verbatim: "",
    vaccine_types: ["MMR", "DTaP", "HepB", "HepA", "Td/Tdap", "Influenza", "COVID-19"],
    pharmacy_labels: ["Antibiotic", "Analgesic", "Cardiac", "Respiratory", "Gastrointestinal", "Endocrine", "Emergency"],
    equipment_categories: [],
    consumable_categories: [],
    offline_force_flags: false,
    db_write_lock: false,
    db_write_lock_forced: false,
};

// ============================================================================
// STATE MANAGEMENT
// ============================================================================

let settingsDirty = false;           // Track unsaved changes
let settingsLoaded = false;          // Prevent premature saves during init
let settingsAutoSaveTimer = null;    // Debounce timer for auto-save
let settingsSaveSequence = 0;        // Monotonic save request id
let offlineStatusCache = null;       // Cached offline status for UI updates
let vaccineTypeList = [...DEFAULT_SETTINGS.vaccine_types];   // Active vaccine types
let pharmacyLabelList = [...DEFAULT_SETTINGS.pharmacy_labels]; // Active pharmacy labels
let equipmentCategoryList = [...DEFAULT_SETTINGS.equipment_categories];
let consumableCategoryList = [...DEFAULT_SETTINGS.consumable_categories];
let offlineInitialized = false;     // Prevent duplicate offline checks
const PROMPT_TEMPLATE_CONFIG = {
    triage_instruction: {
        textareaId: 'triage_instruction',
        nameInputId: 'triage_prompt_template_name',
        selectId: 'triage_prompt_template_select',
        statusId: 'triage_prompt_template_status',
        emptyLabel: 'Select saved triage prompt…',
    },
    inquiry_instruction: {
        textareaId: 'inquiry_instruction',
        nameInputId: 'inquiry_prompt_template_name',
        selectId: 'inquiry_prompt_template_select',
        statusId: 'inquiry_prompt_template_status',
        emptyLabel: 'Select saved inquiry prompt…',
    },
};
const promptTemplateCache = {
    triage_instruction: [],
    inquiry_instruction: [],
};
let triagePromptTreePayload = null;
let triageTreeEditorState = {
    domain: '',
    problem: '',
    selectedOptionKeys: {
        anatomy_guardrails: '',
        severity_modifiers: '',
        mechanism_modifiers: '',
    },
};
const TRIAGE_TREE_OPTION_CATEGORY_LABELS = {
    anatomy_guardrails: 'Anatomy',
    severity_modifiers: 'Mechanism / Cause',
    mechanism_modifiers: 'Severity / Complication',
};
const TRIAGE_TREE_CATEGORY_EDITOR_IDS = {
    anatomy_guardrails: {
        select: 'triage-tree-anatomy-key-select',
        input: 'triage-tree-anatomy-key-new',
        text: 'triage-tree-anatomy-text',
    },
    severity_modifiers: {
        select: 'triage-tree-severity-key-select',
        input: 'triage-tree-severity-key-new',
        text: 'triage-tree-severity-text',
    },
    mechanism_modifiers: {
        select: 'triage-tree-mechanism-key-select',
        input: 'triage-tree-mechanism-key-new',
        text: 'triage-tree-mechanism-text',
    },
};

/**
 * Set application user mode and update visibility.
 * 
 * User Mode Behaviors:
 * - USER: Simple interface, essential features only
 * - ADVANCED: Model parameters, detailed metrics, import/export
 * - DEVELOPER: Full debugging, all dev-tag elements, raw data views
 * 
 * UI Changes:
 * 1. Adds/removes mode-specific classes to body and html
 * 2. Shows/hides .developer-only elements
 * 3. Shows/hides .dev-tag elements
 * 4. Updates triage sample inline visibility
 * 5. Refreshes settings meta badges
 * 6. Persists to localStorage
 * 
 * Triggered By:
 * - Settings form user_mode select change
 * - Page load (from localStorage or server)
 * - Reset section
 * 
 * @param {string} mode - 'user', 'advanced', or 'developer'
 */
function setUserMode(mode) {
    const body = document.body;
    const normalized = ['advanced', 'developer'].includes((mode || '').toLowerCase()) ? mode.toLowerCase() : 'user';
    body.classList.remove('mode-user', 'mode-advanced', 'mode-developer');
    body.classList.add(`mode-${normalized}`);
    document.documentElement.classList.remove('mode-user', 'mode-advanced', 'mode-developer');
    document.documentElement.classList.add(`mode-${normalized}`);
    document.querySelectorAll('.dev-tag').forEach(el => {
        el.style.display = normalized === 'developer' ? 'inline-block' : '';
    });
    document.querySelectorAll('.developer-only').forEach(el => {
        el.style.display = normalized === 'developer' ? '' : 'none';
    });
    updateSettingsMeta(window.CACHED_SETTINGS || {});
    try {
        localStorage.setItem('user_mode', normalized);
    } catch (err) { /* ignore */ }
}

/**
 * Update live status badges in settings section headers.
 * 
 * Status Badges Display:
 * - Offline meta: Cached model count or online/offline mode
 * - Vaccine meta: Number of vaccine types configured
 * - Pharmacy meta: Number of user labels defined
 * - User mode meta: Current visibility mode
 * - Model meta: Token limits (advanced mode only)
 * 
 * Advanced Mode Differences:
 * Shows detailed technical info (token counts, model names) instead
 * of simplified summaries.
 * 
 * Called After:
 * - Settings load/save
 * - User mode change
 * - List modifications (vaccine types, pharmacy labels)
 * - Offline status updates
 * 
 * @param {Object} settings - Current settings object
 */
function updateSettingsMeta(settings = {}) {
    const isAdvanced = document.body.classList.contains('mode-advanced') || document.body.classList.contains('mode-developer');
    // Offline meta
    const offlineEl = document.getElementById('offline-meta');
    if (offlineEl) {
        const flagsOn = !!settings.offline_force_flags;
        if (isAdvanced && offlineStatusCache) {
            const models = Array.isArray(offlineStatusCache.models) ? offlineStatusCache.models : [];
            const cached = models.filter((m) => !!m.cached).length;
            const total = models.length || (Number(offlineStatusCache.total_models) || 0);
            offlineEl.textContent = `Cached models: ${cached}/${total || '?'} | Flags ${flagsOn ? 'ON' : 'OFF'}`;
        } else {
            const isOffline = !!((offlineStatusCache && offlineStatusCache.offline_mode) || flagsOn);
            const mode = isOffline ? 'Offline' : 'Online';
            offlineEl.textContent = `Mode: ${mode}`;
        }
    }
    // Vaccine types
    const vaxEl = document.getElementById('vax-meta');
    if (vaxEl) vaxEl.textContent = `${vaccineTypeList.length} types`;
    // Pharmacy labels
    const pharmEl = document.getElementById('pharm-meta');
    if (pharmEl) pharmEl.textContent = `${pharmacyLabelList.length} labels`;
    // Equipment categories
    const equipCatEl = document.getElementById('equipment-cat-meta');
    if (equipCatEl) equipCatEl.textContent = `${equipmentCategoryList.length} categories`;
    // Consumable categories
    const consCatEl = document.getElementById('consumable-cat-meta');
    if (consCatEl) consCatEl.textContent = `${consumableCategoryList.length} categories`;
    // User mode
    const modeEl = document.getElementById('user-mode-meta');
    if (modeEl) modeEl.textContent = `Mode: ${settings.user_mode || 'advanced'}`;
    // Crew creds (updated when crew list loads)
    // Model params
    const modelEl = document.getElementById('model-meta');
    if (modelEl) {
        if (isAdvanced) {
            const triTok = Number(settings.tr_tok || settings.tr_tok === 0 ? settings.tr_tok : DEFAULT_SETTINGS.tr_tok);
            const inTok = Number(settings.in_tok || settings.in_tok === 0 ? settings.in_tok : DEFAULT_SETTINGS.in_tok);
            modelEl.textContent = `Tokens - Triage: ${triTok} | Inquiry: ${inTok}`;
        } else {
            modelEl.textContent = '';
        }
    }
}

/**
 * Update crew credentials count badge.
 * 
 * Shows how many crew members have configured login credentials.
 * 
 * @param {number} count - Number of crew with credentials
 */
function updateCrewCredMeta(count) {
    const el = document.getElementById('crew-creds-meta');
    if (el) el.textContent = `${count} credentialed`;
}

/**
 * Apply settings data to UI form fields.
 * 
 * Application Process:
 * 1. Merges with defaults (fills missing fields)
 * 2. Normalizes vaccine types and pharmacy labels
 * 3. Renders dynamic lists
 * 4. Populates form inputs (text, number, checkbox, select)
 * 5. Sets user mode and updates visibility
 * 6. Caches in window.CACHED_SETTINGS
 * 7. Updates status badges
 * 8. Clears dirty flag
 * 
 * Field Type Handling:
 * - Checkbox: Sets checked property
 * - Others: Sets value property
 * 
 * Called By:
 * - loadSettingsUI (on page load)
 * - saveSettings (after successful save)
 * - Reset functions
 * 
 * @param {Object} data - Settings object from server or defaults
 */
function applySettingsToUI(data = {}) {
    const merged = { ...DEFAULT_SETTINGS, ...(data || {}) };
    vaccineTypeList = normalizeVaccineTypes(merged.vaccine_types);
    pharmacyLabelList = normalizePharmacyLabels(merged.pharmacy_labels);
    equipmentCategoryList = normalizeEquipmentCategories(merged.equipment_categories);
    consumableCategoryList = normalizeConsumableCategories(merged.consumable_categories);
    renderVaccineTypes();
    renderPharmacyLabels();
    renderEquipmentCategories();
    renderConsumableCategories();
    Object.keys(merged).forEach(k => {
        const el = document.getElementById(k);
        if (el) {
            if (el.type === 'checkbox') {
                el.checked = Boolean(merged[k]);
            } else {
                el.value = merged[k];
            }
        }
    });
    const dbWriteLockEl = document.getElementById('db_write_lock');
    const dbWriteLockForcedNote = document.getElementById('db-write-lock-forced-note');
    if (dbWriteLockEl) {
        const forced = !!merged.db_write_lock_forced;
        dbWriteLockEl.disabled = forced;
        dbWriteLockEl.title = forced ? 'Locked by environment variable DB_WRITE_LOCK' : '';
        if (dbWriteLockForcedNote) {
            dbWriteLockForcedNote.style.display = forced ? 'block' : 'none';
        }
    }
    setUserMode(merged.user_mode);
    try { localStorage.setItem('user_mode', merged.user_mode || 'advanced'); } catch (err) { /* ignore */ }
    window.CACHED_SETTINGS = merged;
    settingsDirty = false;
    settingsLoaded = true;
    updateSettingsMeta(merged);
    updateOfflineFlagButton();
}

/**
 * getPromptTemplateConfig: function-level behavior note for maintainers.
 * Keep this block synchronized with implementation changes.
 */
function getPromptTemplateConfig(promptKey) {
    return PROMPT_TEMPLATE_CONFIG[promptKey] || null;
}

/**
 * updatePromptTemplateStatus: function-level behavior note for maintainers.
 * Keep this block synchronized with implementation changes.
 */
function updatePromptTemplateStatus(promptKey, message, isError = false) {
    const cfg = getPromptTemplateConfig(promptKey);
    if (!cfg) return;
    const el = document.getElementById(cfg.statusId);
    if (!el) return;
    el.textContent = message || '';
    el.style.color = isError ? 'var(--red)' : '#4a5568';
}

/**
 * syncPromptTemplateSelectToEditor: function-level behavior note for maintainers.
 * Keep this block synchronized with implementation changes.
 */
function syncPromptTemplateSelectToEditor(promptKey) {
    const cfg = getPromptTemplateConfig(promptKey);
    if (!cfg) return;
    const textarea = document.getElementById(cfg.textareaId);
    const select = document.getElementById(cfg.selectId);
    if (!textarea || !select) return;
    const currentText = (textarea.value || '').trim();
    const items = promptTemplateCache[promptKey] || [];
    const match = items.find((item) => (item.prompt_text || '').trim() === currentText);
    select.value = match ? String(match.id) : '';
}

/**
 * renderPromptTemplateOptions: function-level behavior note for maintainers.
 * Keep this block synchronized with implementation changes.
 */
function renderPromptTemplateOptions(promptKey) {
    const cfg = getPromptTemplateConfig(promptKey);
    if (!cfg) return;
    const select = document.getElementById(cfg.selectId);
    if (!select) return;
    const prev = select.value;
    const items = promptTemplateCache[promptKey] || [];
    const options = [
        `<option value="">${cfg.emptyLabel}</option>`,
        ...items.map((item) => {
            const updated = item.updated_at ? new Date(item.updated_at).toLocaleString() : '';
            const updatedSuffix = updated ? ` (${updated})` : '';
            return `<option value="${item.id}">${item.name}${updatedSuffix}</option>`;
        }),
    ];
    select.innerHTML = options.join('');
    if (items.some((item) => String(item.id) === String(prev))) {
        select.value = String(prev);
    }
    syncPromptTemplateSelectToEditor(promptKey);
}

async function loadPromptTemplates(promptKey = null) {
    const cfg = promptKey ? getPromptTemplateConfig(promptKey) : null;
    if (promptKey && !cfg) return;
    try {
        const query = promptKey ? `?prompt_key=${encodeURIComponent(promptKey)}` : '';
        const res = await fetch(`/api/settings/prompt-library${query}`, { credentials: 'same-origin' });
        if (!res.ok) throw new Error(`Prompt library load failed (${res.status})`);
        const payload = await res.json();
        const items = Array.isArray(payload?.items) ? payload.items : [];
        if (promptKey) {
            promptTemplateCache[promptKey] = items;
            renderPromptTemplateOptions(promptKey);
            updatePromptTemplateStatus(promptKey, '');
            return;
        }
        Object.keys(promptTemplateCache).forEach((key) => {
            promptTemplateCache[key] = items.filter((item) => item.prompt_key === key);
            renderPromptTemplateOptions(key);
            updatePromptTemplateStatus(key, '');
        });
    } catch (err) {
        if (promptKey) {
            updatePromptTemplateStatus(promptKey, `Load failed: ${err.message}`, true);
        } else {
            Object.keys(promptTemplateCache).forEach((key) => {
                updatePromptTemplateStatus(key, `Load failed: ${err.message}`, true);
            });
        }
    }
}

async function savePromptTemplate(promptKey) {
    const cfg = getPromptTemplateConfig(promptKey);
    if (!cfg) return;
    const nameEl = document.getElementById(cfg.nameInputId);
    const textEl = document.getElementById(cfg.textareaId);
    if (!nameEl || !textEl) return;
    const name = (nameEl.value || '').trim();
    const promptText = (textEl.value || '').trim();
    if (!name) {
        updatePromptTemplateStatus(promptKey, 'Enter a prompt name before saving.', true);
        return;
    }
    if (!promptText) {
        updatePromptTemplateStatus(promptKey, 'Prompt text is empty.', true);
        return;
    }
    try {
        updatePromptTemplateStatus(promptKey, 'Saving prompt version…');
        const res = await fetch('/api/settings/prompt-library', {
            method: 'POST',
            headers: { 'Content-Type': 'application/json' },
            credentials: 'same-origin',
            body: JSON.stringify({
                prompt_key: promptKey,
                name,
                prompt_text: promptText,
            }),
        });
        if (!res.ok) {
            let detail = '';
            try {
                const err = await res.json();
                detail = err?.error ? `: ${err.error}` : '';
            } catch (_) { /* ignore */ }
            throw new Error(`Save failed (${res.status})${detail}`);
        }
        const saved = await res.json();
        await loadPromptTemplates(promptKey);
        const select = document.getElementById(cfg.selectId);
        if (select && saved?.id) {
            select.value = String(saved.id);
        }
        updatePromptTemplateStatus(promptKey, `Saved "${saved?.name || name}".`);
    } catch (err) {
        updatePromptTemplateStatus(promptKey, `Save failed: ${err.message}`, true);
    }
}

/**
 * loadSelectedPromptTemplate: function-level behavior note for maintainers.
 * Keep this block synchronized with implementation changes.
 */
function loadSelectedPromptTemplate(promptKey) {
    const cfg = getPromptTemplateConfig(promptKey);
    if (!cfg) return;
    const select = document.getElementById(cfg.selectId);
    const textEl = document.getElementById(cfg.textareaId);
    const nameEl = document.getElementById(cfg.nameInputId);
    if (!select || !textEl) return;
    const selectedId = String(select.value || '');
    if (!selectedId) {
        updatePromptTemplateStatus(promptKey, 'Select a saved prompt first.', true);
        return;
    }
    const items = promptTemplateCache[promptKey] || [];
    const chosen = items.find((item) => String(item.id) === selectedId);
    if (!chosen) {
        updatePromptTemplateStatus(promptKey, 'Saved prompt not found. Reload prompt list.', true);
        return;
    }
    textEl.value = chosen.prompt_text || '';
    if (nameEl) {
        nameEl.value = chosen.name || '';
    }
    textEl.dispatchEvent(new Event('input', { bubbles: true }));
    updatePromptTemplateStatus(promptKey, `Loaded "${chosen.name}" into active settings.`);
}

/**
 * updateTriageTreeStatus: function-level behavior note for maintainers.
 * Keep this block synchronized with implementation changes.
 */
function updateTriageTreeStatus(message, isError = false) {
    const el = document.getElementById('triage-tree-status');
    if (!el) return;
    el.textContent = message || '';
    el.style.color = isError ? 'var(--red)' : '#4a5568';
}

/**
 * saveTriageTreeOnFieldExit: function-level behavior note for maintainers.
 * Keep this block synchronized with implementation changes.
 */
function saveTriageTreeOnFieldExit(reason = 'field-exit') {
    if (!triagePromptTreePayload) return;
    saveTriagePromptTree({ silentStatus: true, reason }).catch(() => {});
}

/**
 * normalizeTriageTreePayload: function-level behavior note for maintainers.
 * Keep this block synchronized with implementation changes.
 */
function normalizeTriageTreePayload(raw) {
    if (!raw || typeof raw !== 'object') {
        throw new Error('Payload must be a JSON object.');
    }
    const baseDoctrine = typeof raw.base_doctrine === 'string' ? raw.base_doctrine.trim() : '';
    const tree = raw.tree;
    if (!tree || typeof tree !== 'object' || Array.isArray(tree) || !Object.keys(tree).length) {
        throw new Error('Payload must include a non-empty "tree" object.');
    }
    return {
        base_doctrine: baseDoctrine,
        tree,
    };
}

/**
 * ensureTriageTreeRoot: function-level behavior note for maintainers.
 * Keep this block synchronized with implementation changes.
 */
function ensureTriageTreeRoot() {
    if (!triagePromptTreePayload || typeof triagePromptTreePayload !== 'object') {
        triagePromptTreePayload = { base_doctrine: '', tree: {} };
    }
    if (!triagePromptTreePayload.tree || typeof triagePromptTreePayload.tree !== 'object' || Array.isArray(triagePromptTreePayload.tree)) {
        triagePromptTreePayload.tree = {};
    }
    return triagePromptTreePayload.tree;
}

/**
 * getTriageTreeDomainNode: function-level behavior note for maintainers.
 * Keep this block synchronized with implementation changes.
 */
function getTriageTreeDomainNode(domainKey, create = false) {
    const tree = ensureTriageTreeRoot();
    if (!domainKey) return null;
    if (!tree[domainKey] || typeof tree[domainKey] !== 'object' || Array.isArray(tree[domainKey])) {
        if (!create) return null;
        tree[domainKey] = { mindset: '', problems: {} };
    }
    if (!tree[domainKey].problems || typeof tree[domainKey].problems !== 'object' || Array.isArray(tree[domainKey].problems)) {
        tree[domainKey].problems = {};
    }
    return tree[domainKey];
}

/**
 * getTriageTreeProblemNode: function-level behavior note for maintainers.
 * Keep this block synchronized with implementation changes.
 */
function getTriageTreeProblemNode(domainKey, problemKey, create = false) {
    const domainNode = getTriageTreeDomainNode(domainKey, create);
    if (!domainNode || !problemKey) return null;
    if (!domainNode.problems[problemKey] || typeof domainNode.problems[problemKey] !== 'object' || Array.isArray(domainNode.problems[problemKey])) {
        if (!create) return null;
        domainNode.problems[problemKey] = {
            procedure: '',
            anatomy_guardrails: {},
            severity_modifiers: {},
            mechanism_modifiers: {},
        };
    }
    const problemNode = domainNode.problems[problemKey];
    if (Object.prototype.hasOwnProperty.call(problemNode, 'exclusions')) {
        delete problemNode.exclusions;
    }
    ['anatomy_guardrails', 'severity_modifiers', 'mechanism_modifiers'].forEach((category) => {
        if (!problemNode[category] || typeof problemNode[category] !== 'object' || Array.isArray(problemNode[category])) {
            problemNode[category] = {};
        }
    });
    return problemNode;
}

/**
 * getTriageTreeOptionMap: function-level behavior note for maintainers.
 * Keep this block synchronized with implementation changes.
 */
function getTriageTreeOptionMap(domainKey, problemKey, categoryKey, create = false) {
    const problemNode = getTriageTreeProblemNode(domainKey, problemKey, create);
    if (!problemNode) return {};
    if (!problemNode[categoryKey] || typeof problemNode[categoryKey] !== 'object' || Array.isArray(problemNode[categoryKey])) {
        if (!create) return {};
        problemNode[categoryKey] = {};
    }
    return problemNode[categoryKey];
}

/**
 * setTriageTreeSelectOptions: function-level behavior note for maintainers.
 * Keep this block synchronized with implementation changes.
 */
function setTriageTreeSelectOptions(selectId, options, selectedValue, emptyLabel) {
    const select = document.getElementById(selectId);
    if (!select) return '';
    const values = Array.isArray(options) ? options.slice() : [];
    const selected = values.includes(selectedValue) ? selectedValue : '';
    select.innerHTML = `<option value="">${emptyLabel}</option>`;
    values.forEach((value) => {
        const opt = document.createElement('option');
        opt.value = value;
        opt.textContent = value;
        select.appendChild(opt);
    });
    select.value = selected;
    return selected;
}

/**
 * setTriageTreePathHint: function-level behavior note for maintainers.
 * Keep this block synchronized with implementation changes.
 */
function setTriageTreePathHint(elementId, segments, fallbackText) {
    const el = document.getElementById(elementId);
    if (!el) return;
    const cleaned = (Array.isArray(segments) ? segments : [])
        .map((segment) => String(segment || '').trim())
        .filter((segment) => !!segment);
    el.textContent = cleaned.length
        ? `Path: ${cleaned.join(' -> ')}`
        : `Path: ${fallbackText}`;
}

/**
 * syncTriageTreeEditorState: function-level behavior note for maintainers.
 * Keep this block synchronized with implementation changes.
 */
function syncTriageTreeEditorState() {
    const tree = ensureTriageTreeRoot();
    const domains = Object.keys(tree);
    if (!domains.includes(triageTreeEditorState.domain)) {
        triageTreeEditorState.domain = domains[0] || '';
    }
    if (!triageTreeEditorState.selectedOptionKeys || typeof triageTreeEditorState.selectedOptionKeys !== 'object') {
        triageTreeEditorState.selectedOptionKeys = {
            anatomy_guardrails: '',
            severity_modifiers: '',
            mechanism_modifiers: '',
        };
    }
    const domainNode = getTriageTreeDomainNode(triageTreeEditorState.domain, false);
    const problems = domainNode ? Object.keys(domainNode.problems || {}) : [];
    if (!problems.includes(triageTreeEditorState.problem)) {
        triageTreeEditorState.problem = problems[0] || '';
    }
    Object.keys(TRIAGE_TREE_OPTION_CATEGORY_LABELS).forEach((categoryKey) => {
        const optionMap = getTriageTreeOptionMap(
            triageTreeEditorState.domain,
            triageTreeEditorState.problem,
            categoryKey,
            false
        );
        const optionKeys = Object.keys(optionMap || {});
        const current = triageTreeEditorState.selectedOptionKeys[categoryKey] || '';
        if (!optionKeys.includes(current)) {
            triageTreeEditorState.selectedOptionKeys[categoryKey] = optionKeys[0] || '';
        }
    });
}

/**
 * syncTriageTreeInputsToState: function-level behavior note for maintainers.
 * Keep this block synchronized with implementation changes.
 */
function syncTriageTreeInputsToState() {
    if (!triagePromptTreePayload) return;
    const baseEl = document.getElementById('triage-tree-base-doctrine');
    if (baseEl) {
        triagePromptTreePayload.base_doctrine = (baseEl.value || '').trim();
    }
    const domainNode = getTriageTreeDomainNode(triageTreeEditorState.domain, false);
    const mindsetEl = document.getElementById('triage-tree-domain-mindset');
    if (domainNode && mindsetEl) {
        domainNode.mindset = (mindsetEl.value || '').trim();
    }
    const problemNode = getTriageTreeProblemNode(triageTreeEditorState.domain, triageTreeEditorState.problem, false);
    const procedureEl = document.getElementById('triage-tree-procedure');
    if (problemNode) {
        if (procedureEl) {
            problemNode.procedure = (procedureEl.value || '').trim();
        }
    }
    if (problemNode) {
        Object.entries(TRIAGE_TREE_CATEGORY_EDITOR_IDS).forEach(([categoryKey, ids]) => {
            const selectedKey = triageTreeEditorState.selectedOptionKeys?.[categoryKey] || '';
            const textEl = document.getElementById(ids.text);
            if (!selectedKey || !textEl) return;
            const optionMap = getTriageTreeOptionMap(
                triageTreeEditorState.domain,
                triageTreeEditorState.problem,
                categoryKey,
                true
            );
            optionMap[selectedKey] = (textEl.value || '').trim();
        });
    }
}

/**
 * clearTriageTreeSelectedOptions: function-level behavior note for maintainers.
 * Keep this block synchronized with implementation changes.
 */
function clearTriageTreeSelectedOptions() {
    triageTreeEditorState.selectedOptionKeys = {
        anatomy_guardrails: '',
        severity_modifiers: '',
        mechanism_modifiers: '',
    };
}

/**
 * bindTriageTreeEditorHandlers: function-level behavior note for maintainers.
 * Keep this block synchronized with implementation changes.
 */
function bindTriageTreeEditorHandlers() {
    const domainSelect = document.getElementById('triage-tree-domain-select');
    if (domainSelect && !domainSelect.dataset.bound) {
        domainSelect.dataset.bound = '1';
        domainSelect.addEventListener('change', () => {
            syncTriageTreeInputsToState();
            triageTreeEditorState.domain = domainSelect.value || '';
            triageTreeEditorState.problem = '';
            clearTriageTreeSelectedOptions();
            renderTriageTreeEditorControls();
        });
        domainSelect.addEventListener('blur', () => {
            saveTriageTreeOnFieldExit('domain-blur');
        });
    }

    const problemSelect = document.getElementById('triage-tree-problem-select');
    if (problemSelect && !problemSelect.dataset.bound) {
        problemSelect.dataset.bound = '1';
        problemSelect.addEventListener('change', () => {
            syncTriageTreeInputsToState();
            triageTreeEditorState.problem = problemSelect.value || '';
            clearTriageTreeSelectedOptions();
            renderTriageTreeEditorControls();
        });
        problemSelect.addEventListener('blur', () => {
            saveTriageTreeOnFieldExit('problem-blur');
        });
    }

    Object.entries(TRIAGE_TREE_CATEGORY_EDITOR_IDS).forEach(([categoryKey, ids]) => {
        const optionSelect = document.getElementById(ids.select);
        if (optionSelect && !optionSelect.dataset.bound) {
            optionSelect.dataset.bound = '1';
            optionSelect.addEventListener('change', () => {
                syncTriageTreeInputsToState();
                triageTreeEditorState.selectedOptionKeys[categoryKey] = optionSelect.value || '';
                renderTriageTreeEditorControls();
            });
            optionSelect.addEventListener('blur', () => {
                saveTriageTreeOnFieldExit(`${categoryKey}-blur`);
            });
        }
    });

    const baseEl = document.getElementById('triage-tree-base-doctrine');
    if (baseEl && !baseEl.dataset.bound) {
        baseEl.dataset.bound = '1';
        baseEl.addEventListener('input', () => {
            if (!triagePromptTreePayload) return;
            triagePromptTreePayload.base_doctrine = (baseEl.value || '').trim();
        });
        baseEl.addEventListener('blur', () => {
            saveTriageTreeOnFieldExit('base-blur');
        });
    }

    const mindsetEl = document.getElementById('triage-tree-domain-mindset');
    if (mindsetEl && !mindsetEl.dataset.bound) {
        mindsetEl.dataset.bound = '1';
        mindsetEl.addEventListener('input', () => {
            const domainNode = getTriageTreeDomainNode(triageTreeEditorState.domain, false);
            if (domainNode) {
                domainNode.mindset = (mindsetEl.value || '').trim();
            }
        });
        mindsetEl.addEventListener('blur', () => {
            saveTriageTreeOnFieldExit('mindset-blur');
        });
    }

    const procedureEl = document.getElementById('triage-tree-procedure');
    if (procedureEl && !procedureEl.dataset.bound) {
        procedureEl.dataset.bound = '1';
        procedureEl.addEventListener('input', () => {
            const problemNode = getTriageTreeProblemNode(triageTreeEditorState.domain, triageTreeEditorState.problem, false);
            if (problemNode) {
                problemNode.procedure = (procedureEl.value || '').trim();
            }
        });
        procedureEl.addEventListener('blur', () => {
            saveTriageTreeOnFieldExit('procedure-blur');
        });
    }

    Object.entries(TRIAGE_TREE_CATEGORY_EDITOR_IDS).forEach(([categoryKey, ids]) => {
        const textEl = document.getElementById(ids.text);
        if (textEl && !textEl.dataset.bound) {
            textEl.dataset.bound = '1';
            textEl.addEventListener('input', () => {
                const selectedKey = triageTreeEditorState.selectedOptionKeys?.[categoryKey] || '';
                if (!selectedKey) return;
                const optionMap = getTriageTreeOptionMap(
                    triageTreeEditorState.domain,
                    triageTreeEditorState.problem,
                    categoryKey,
                    true
                );
                optionMap[selectedKey] = (textEl.value || '').trim();
            });
            textEl.addEventListener('blur', () => {
                saveTriageTreeOnFieldExit(`${categoryKey}-text-blur`);
            });
        }
    });
}

/**
 * renderTriageTreeEditor: function-level behavior note for maintainers.
 * Keep this block synchronized with implementation changes.
 */
function renderTriageTreeEditor(payload) {
    const normalized = normalizeTriageTreePayload(payload);
    triagePromptTreePayload = JSON.parse(JSON.stringify(normalized));
    bindTriageTreeEditorHandlers();
    renderTriageTreeEditorControls();
}

/**
 * renderTriageTreeCategoryControls: function-level behavior note for maintainers.
 * Keep this block synchronized with implementation changes.
 */
function renderTriageTreeCategoryControls(categoryKey, selectedDomain, selectedProblem, problemNode) {
    const ids = TRIAGE_TREE_CATEGORY_EDITOR_IDS[categoryKey];
    if (!ids) return;
    const optionMap = getTriageTreeOptionMap(
        selectedDomain,
        selectedProblem,
        categoryKey,
        false
    );
    const optionKeys = Object.keys(optionMap || {});
    const selectedOption = setTriageTreeSelectOptions(
        ids.select,
        optionKeys,
        triageTreeEditorState.selectedOptionKeys?.[categoryKey] || '',
        `Select ${TRIAGE_TREE_OPTION_CATEGORY_LABELS[categoryKey] || 'option'}...`
    );
    triageTreeEditorState.selectedOptionKeys[categoryKey] = selectedOption;

    const selectEl = document.getElementById(ids.select);
    if (selectEl) {
        selectEl.disabled = !problemNode;
    }

    const inputEl = document.getElementById(ids.input);
    if (inputEl) {
        inputEl.disabled = !problemNode;
    }

    const textEl = document.getElementById(ids.text);
    if (textEl) {
        textEl.value = selectedOption ? (optionMap[selectedOption] || '') : '';
        textEl.disabled = !problemNode || !selectedOption;
    }
}

/**
 * renderTriageTreeEditorControls: function-level behavior note for maintainers.
 * Keep this block synchronized with implementation changes.
 */
function renderTriageTreeEditorControls() {
    if (!triagePromptTreePayload) return;
    syncTriageTreeEditorState();
    const tree = ensureTriageTreeRoot();
    const domains = Object.keys(tree);
    const selectedDomain = setTriageTreeSelectOptions(
        'triage-tree-domain-select',
        domains,
        triageTreeEditorState.domain,
        'Select domain...'
    );
    triageTreeEditorState.domain = selectedDomain;
    const domainNode = getTriageTreeDomainNode(selectedDomain, false);

    const mindsetEl = document.getElementById('triage-tree-domain-mindset');
    if (mindsetEl) {
        mindsetEl.value = domainNode ? (domainNode.mindset || '') : '';
        mindsetEl.disabled = !domainNode;
    }

    const problems = domainNode ? Object.keys(domainNode.problems || {}) : [];
    const selectedProblem = setTriageTreeSelectOptions(
        'triage-tree-problem-select',
        problems,
        triageTreeEditorState.problem,
        'Select problem...'
    );
    triageTreeEditorState.problem = selectedProblem;
    const problemSelectEl = document.getElementById('triage-tree-problem-select');
    if (problemSelectEl) {
        problemSelectEl.disabled = !domainNode;
    }
    const problemNode = getTriageTreeProblemNode(selectedDomain, selectedProblem, false);

    const procedureEl = document.getElementById('triage-tree-procedure');
    if (procedureEl) {
        procedureEl.value = problemNode ? (problemNode.procedure || '') : '';
        procedureEl.disabled = !problemNode;
    }

    const baseEl = document.getElementById('triage-tree-base-doctrine');
    if (baseEl && document.activeElement !== baseEl) {
        baseEl.value = triagePromptTreePayload.base_doctrine || '';
    }

    ['anatomy_guardrails', 'severity_modifiers', 'mechanism_modifiers'].forEach((categoryKey) => {
        renderTriageTreeCategoryControls(categoryKey, selectedDomain, selectedProblem, problemNode);
    });

    const selectedAnatomy = triageTreeEditorState.selectedOptionKeys?.anatomy_guardrails || '';
    const selectedMechanism = triageTreeEditorState.selectedOptionKeys?.severity_modifiers || '';
    const selectedSeverity = triageTreeEditorState.selectedOptionKeys?.mechanism_modifiers || '';

    setTriageTreePathHint('triage-tree-base-path', [], 'Global (all triage paths)');
    setTriageTreePathHint('triage-tree-domain-path', [selectedDomain], '(select Domain)');
    setTriageTreePathHint('triage-tree-procedure-path', [selectedDomain, selectedProblem], '(select Domain and Problem)');
    setTriageTreePathHint('triage-tree-anatomy-path', [selectedDomain, selectedProblem, selectedAnatomy], '(select Domain, Problem, and Anatomy)');
    setTriageTreePathHint('triage-tree-severity-path', [selectedDomain, selectedProblem, selectedAnatomy, selectedMechanism], '(select Domain, Problem, Anatomy, and Mechanism)');
    setTriageTreePathHint('triage-tree-mechanism-path', [selectedDomain, selectedProblem, selectedAnatomy, selectedMechanism, selectedSeverity], '(select Domain, Problem, Anatomy, Mechanism, and Severity)');
}

/**
 * addTriageTreeDomain: function-level behavior note for maintainers.
 * Keep this block synchronized with implementation changes.
 */
function addTriageTreeDomain() {
    if (!triagePromptTreePayload) return;
    syncTriageTreeInputsToState();
    const input = document.getElementById('triage-tree-domain-new');
    const name = (input?.value || '').trim();
    if (!name) {
        updateTriageTreeStatus('Enter a domain name first.', true);
        return;
    }
    const tree = ensureTriageTreeRoot();
    const existing = Object.keys(tree).find((key) => key.trim().toLowerCase() === name.toLowerCase());
    if (existing) {
        updateTriageTreeStatus(`Domain "${existing}" already exists.`, true);
        return;
    }
    tree[name] = { mindset: '', problems: {} };
    triageTreeEditorState.domain = name;
    triageTreeEditorState.problem = '';
    clearTriageTreeSelectedOptions();
    if (input) input.value = '';
    renderTriageTreeEditorControls();
    updateTriageTreeStatus(`Added domain "${name}".`);
    saveTriageTreeOnFieldExit('domain-add');
}

/**
 * deleteTriageTreeDomain: function-level behavior note for maintainers.
 * Keep this block synchronized with implementation changes.
 */
function deleteTriageTreeDomain() {
    if (!triagePromptTreePayload) return;
    syncTriageTreeInputsToState();
    const key = triageTreeEditorState.domain;
    if (!key) {
        updateTriageTreeStatus('Select a domain to delete.', true);
        return;
    }
    if (!confirm(`Delete domain "${key}" and all its problems?`)) return;
    const tree = ensureTriageTreeRoot();
    delete tree[key];
    triageTreeEditorState.domain = '';
    triageTreeEditorState.problem = '';
    clearTriageTreeSelectedOptions();
    renderTriageTreeEditorControls();
    updateTriageTreeStatus(`Deleted domain "${key}".`);
    saveTriageTreeOnFieldExit('domain-delete');
}

/**
 * addTriageTreeProblem: function-level behavior note for maintainers.
 * Keep this block synchronized with implementation changes.
 */
function addTriageTreeProblem() {
    if (!triagePromptTreePayload) return;
    syncTriageTreeInputsToState();
    const domainKey = triageTreeEditorState.domain;
    if (!domainKey) {
        updateTriageTreeStatus('Select a domain first.', true);
        return;
    }
    const input = document.getElementById('triage-tree-problem-new');
    const name = (input?.value || '').trim();
    if (!name) {
        updateTriageTreeStatus('Enter a problem name first.', true);
        return;
    }
    const domainNode = getTriageTreeDomainNode(domainKey, true);
    const existing = Object.keys(domainNode.problems || {}).find((key) => key.trim().toLowerCase() === name.toLowerCase());
    if (existing) {
        updateTriageTreeStatus(`Problem "${existing}" already exists in ${domainKey}.`, true);
        return;
    }
    domainNode.problems[name] = {
        procedure: '',
        anatomy_guardrails: {},
        severity_modifiers: {},
        mechanism_modifiers: {},
    };
    triageTreeEditorState.problem = name;
    clearTriageTreeSelectedOptions();
    if (input) input.value = '';
    renderTriageTreeEditorControls();
    updateTriageTreeStatus(`Added problem "${name}" under ${domainKey}.`);
    saveTriageTreeOnFieldExit('problem-add');
}

/**
 * deleteTriageTreeProblem: function-level behavior note for maintainers.
 * Keep this block synchronized with implementation changes.
 */
function deleteTriageTreeProblem() {
    if (!triagePromptTreePayload) return;
    syncTriageTreeInputsToState();
    const domainKey = triageTreeEditorState.domain;
    const problemKey = triageTreeEditorState.problem;
    if (!domainKey || !problemKey) {
        updateTriageTreeStatus('Select a problem to delete.', true);
        return;
    }
    if (!confirm(`Delete problem "${problemKey}" from "${domainKey}"?`)) return;
    const domainNode = getTriageTreeDomainNode(domainKey, false);
    if (!domainNode || !domainNode.problems) return;
    delete domainNode.problems[problemKey];
    triageTreeEditorState.problem = '';
    clearTriageTreeSelectedOptions();
    renderTriageTreeEditorControls();
    updateTriageTreeStatus(`Deleted problem "${problemKey}".`);
    saveTriageTreeOnFieldExit('problem-delete');
}

/**
 * addTriageTreeCategoryOption: function-level behavior note for maintainers.
 * Keep this block synchronized with implementation changes.
 */
function addTriageTreeCategoryOption(categoryKey) {
    if (!triagePromptTreePayload) return;
    if (!TRIAGE_TREE_OPTION_CATEGORY_LABELS[categoryKey]) return;
    syncTriageTreeInputsToState();
    const domainKey = triageTreeEditorState.domain;
    const problemKey = triageTreeEditorState.problem;
    if (!domainKey || !problemKey) {
        updateTriageTreeStatus('Select a domain and problem first.', true);
        return;
    }
    const input = document.getElementById(TRIAGE_TREE_CATEGORY_EDITOR_IDS[categoryKey].input);
    const key = (input?.value || '').trim();
    if (!key) {
        updateTriageTreeStatus('Enter an option name first.', true);
        return;
    }
    const optionMap = getTriageTreeOptionMap(domainKey, problemKey, categoryKey, true);
    const existing = Object.keys(optionMap).find((name) => name.trim().toLowerCase() === key.toLowerCase());
    if (existing) {
        updateTriageTreeStatus(`Option "${existing}" already exists in ${TRIAGE_TREE_OPTION_CATEGORY_LABELS[categoryKey]}.`, true);
        return;
    }
    optionMap[key] = '';
    triageTreeEditorState.selectedOptionKeys[categoryKey] = key;
    if (input) input.value = '';
    renderTriageTreeEditorControls();
    updateTriageTreeStatus(`Added option "${key}" to ${TRIAGE_TREE_OPTION_CATEGORY_LABELS[categoryKey]}.`);
    saveTriageTreeOnFieldExit(`${categoryKey}-add`);
}

/**
 * deleteTriageTreeCategoryOption: function-level behavior note for maintainers.
 * Keep this block synchronized with implementation changes.
 */
function deleteTriageTreeCategoryOption(categoryKey) {
    if (!triagePromptTreePayload) return;
    if (!TRIAGE_TREE_OPTION_CATEGORY_LABELS[categoryKey]) return;
    syncTriageTreeInputsToState();
    const domainKey = triageTreeEditorState.domain;
    const problemKey = triageTreeEditorState.problem;
    const optionKey = triageTreeEditorState.selectedOptionKeys?.[categoryKey] || '';
    if (!domainKey || !problemKey || !optionKey) {
        updateTriageTreeStatus('Select an option to delete.', true);
        return;
    }
    if (!confirm(`Delete option "${optionKey}" from ${TRIAGE_TREE_OPTION_CATEGORY_LABELS[categoryKey]}?`)) return;
    const optionMap = getTriageTreeOptionMap(domainKey, problemKey, categoryKey, false);
    delete optionMap[optionKey];
    triageTreeEditorState.selectedOptionKeys[categoryKey] = '';
    renderTriageTreeEditorControls();
    updateTriageTreeStatus(`Deleted option "${optionKey}".`);
    saveTriageTreeOnFieldExit(`${categoryKey}-delete`);
}

/**
 * addTriageTreeAnatomyOption: function-level behavior note for maintainers.
 * Keep this block synchronized with implementation changes.
 */
function addTriageTreeAnatomyOption() {
    addTriageTreeCategoryOption('anatomy_guardrails');
}

/**
 * deleteTriageTreeAnatomyOption: function-level behavior note for maintainers.
 * Keep this block synchronized with implementation changes.
 */
function deleteTriageTreeAnatomyOption() {
    deleteTriageTreeCategoryOption('anatomy_guardrails');
}

/**
 * addTriageTreeSeverityOption: function-level behavior note for maintainers.
 * Keep this block synchronized with implementation changes.
 */
function addTriageTreeSeverityOption() {
    addTriageTreeCategoryOption('severity_modifiers');
}

/**
 * deleteTriageTreeSeverityOption: function-level behavior note for maintainers.
 * Keep this block synchronized with implementation changes.
 */
function deleteTriageTreeSeverityOption() {
    deleteTriageTreeCategoryOption('severity_modifiers');
}

/**
 * addTriageTreeMechanismOption: function-level behavior note for maintainers.
 * Keep this block synchronized with implementation changes.
 */
function addTriageTreeMechanismOption() {
    addTriageTreeCategoryOption('mechanism_modifiers');
}

/**
 * deleteTriageTreeMechanismOption: function-level behavior note for maintainers.
 * Keep this block synchronized with implementation changes.
 */
function deleteTriageTreeMechanismOption() {
    deleteTriageTreeCategoryOption('mechanism_modifiers');
}

async function loadTriagePromptTree() {
    try {
        updateTriageTreeStatus('Loading triage decision tree...');
        const res = await fetch('/api/settings/triage-tree', { credentials: 'same-origin' });
        if (!res.ok) throw new Error(`Load failed (${res.status})`);
        const payload = await res.json();
        const normalized = normalizeTriageTreePayload(payload?.payload || payload);
        renderTriageTreeEditor(normalized);
        const domainCount = Object.keys(normalized.tree || {}).length;
        updateTriageTreeStatus(`Loaded triage tree (${domainCount} domain${domainCount === 1 ? '' : 's'}).`);
    } catch (err) {
        updateTriageTreeStatus(`Load failed: ${err.message}`, true);
    }
}

async function saveTriagePromptTree(options = {}) {
    const silentStatus = !!options.silentStatus;
    const reason = options.reason || 'manual';
    if (!triagePromptTreePayload) {
        updateTriageTreeStatus('No triage tree is loaded.', true);
        return;
    }
    syncTriageTreeInputsToState();

    let normalized;
    try {
        normalized = normalizeTriageTreePayload(triagePromptTreePayload);
    } catch (err) {
        updateTriageTreeStatus(err.message || 'Invalid triage tree payload.', true);
        return;
    }

    try {
        if (!silentStatus) {
            updateTriageTreeStatus('Saving triage decision tree...');
        }
        const res = await fetch('/api/settings/triage-tree', {
            method: 'POST',
            headers: { 'Content-Type': 'application/json' },
            credentials: 'same-origin',
            body: JSON.stringify({ payload: normalized }),
        });
        if (!res.ok) {
            let detail = '';
            try {
                const err = await res.json();
                detail = err?.error ? `: ${err.error}` : '';
            } catch (_) { /* ignore */ }
            throw new Error(`Save failed (${res.status})${detail}`);
        }
        const payload = await res.json();
        const saved = normalizeTriageTreePayload(payload?.payload || normalized);
        renderTriageTreeEditor(saved);
        const domainCount = Object.keys(saved.tree || {}).length;
        if (silentStatus) {
            updateTriageTreeStatus(
                `Auto-saved triage tree (${domainCount} domain${domainCount === 1 ? '' : 's'}) at ${new Date().toLocaleTimeString()}.`
            );
        } else {
            updateTriageTreeStatus(`Saved triage tree (${domainCount} domain${domainCount === 1 ? '' : 's'}).`);
        }
        if (typeof refreshPromptPreview === 'function') {
            refreshPromptPreview(true);
        }
    } catch (err) {
        updateTriageTreeStatus(`Save failed: ${err.message}`, true);
    }
}

function exportTriagePromptTreeJson() {
    if (!triagePromptTreePayload) {
        updateTriageTreeStatus('No triage tree is loaded.', true);
        return;
    }
    syncTriageTreeInputsToState();
    let normalized;
    try {
        normalized = normalizeTriageTreePayload(triagePromptTreePayload);
    } catch (err) {
        updateTriageTreeStatus(err.message || 'Unable to export triage tree.', true);
        return;
    }
    const stamp = new Date().toISOString().replace(/[:.]/g, '-');
    const filename = `clinical_triage_pathway_${stamp}.json`;
    const blob = new Blob([JSON.stringify(normalized, null, 2)], { type: 'application/json' });
    const url = URL.createObjectURL(blob);
    const anchor = document.createElement('a');
    anchor.href = url;
    anchor.download = filename;
    document.body.appendChild(anchor);
    anchor.click();
    anchor.remove();
    URL.revokeObjectURL(url);
    updateTriageTreeStatus(`Exported triage pathway JSON (${filename}).`);
}

function triggerTriagePromptTreeImportPicker() {
    const input = document.getElementById('triage-tree-import-file');
    if (!input) {
        updateTriageTreeStatus('Import input is unavailable.', true);
        return;
    }
    input.value = '';
    input.click();
}

async function importTriagePromptTreeJson(event) {
    const file = event?.target?.files?.[0];
    if (!file) return;
    try {
        updateTriageTreeStatus(`Importing ${file.name}...`);
        const raw = await file.text();
        let parsed;
        try {
            parsed = JSON.parse(raw || '{}');
        } catch (err) {
            throw new Error('Selected file is not valid JSON.');
        }
        const normalized = normalizeTriageTreePayload(parsed?.payload || parsed);
        renderTriageTreeEditor(normalized);
        await saveTriagePromptTree({ silentStatus: false, reason: 'import-json' });
        updateTriageTreeStatus(`Imported triage pathway from ${file.name}.`);
    } catch (err) {
        updateTriageTreeStatus(`Import failed: ${err.message}`, true);
    } finally {
        if (event?.target) event.target.value = '';
    }
}

async function resetTriagePromptTreeToDefault() {
    if (!confirm('Reset the entire Clinical Triage Pathway to the default JSON file? This will overwrite current pathway edits.')) {
        return;
    }
    try {
        updateTriageTreeStatus('Resetting triage pathway to default JSON...');
        const res = await fetch('/api/settings/triage-tree/reset', {
            method: 'POST',
            credentials: 'same-origin',
        });
        if (!res.ok) {
            let detail = '';
            try {
                const err = await res.json();
                detail = err?.error ? `: ${err.error}` : '';
            } catch (_) { /* ignore */ }
            throw new Error(`Reset failed (${res.status})${detail}`);
        }
        const payload = await res.json();
        const normalized = normalizeTriageTreePayload(payload?.payload || payload);
        renderTriageTreeEditor(normalized);
        const domainCount = Object.keys(normalized.tree || {}).length;
        updateTriageTreeStatus(`Reset complete. Loaded default triage tree (${domainCount} domain${domainCount === 1 ? '' : 's'}).`);
        if (typeof refreshPromptPreview === 'function') {
            refreshPromptPreview(true);
        }
    } catch (err) {
        updateTriageTreeStatus(`Reset failed: ${err.message}`, true);
    }
}

function setRuntimeLogStatus(message, isError = false) {
    const el = document.getElementById('runtime-log-status');
    if (!el) return;
    el.textContent = message || '';
    el.style.color = isError ? 'var(--red)' : '#2c3e50';
}

function updateRuntimeLogMeta(meta = {}) {
    const el = document.getElementById('runtime-log-meta');
    if (!el) return;
    const exists = !!meta.exists;
    const size = Number(meta.size || 0);
    if (!exists) {
        el.textContent = 'No log file yet';
        return;
    }
    const kb = (size / 1024).toFixed(1);
    el.textContent = `${kb} KB`;
}

function getRuntimeLogLinesRequested() {
    const el = document.getElementById('runtime-log-lines');
    const parsed = Number(el?.value || 600);
    if (Number.isFinite(parsed) && parsed >= 50 && parsed <= 5000) {
        return Math.floor(parsed);
    }
    return 600;
}

async function loadRuntimeLog(lines = null) {
    const textarea = document.getElementById('runtime-log-text');
    const lineCount = lines == null ? getRuntimeLogLinesRequested() : Number(lines);
    if (textarea) {
        textarea.value = '';
    }
    setRuntimeLogStatus('Loading runtime log...');
    try {
        const res = await fetch(`/api/runtime-log?lines=${encodeURIComponent(lineCount)}`, {
            credentials: 'same-origin',
        });
        const payload = await res.json();
        if (!res.ok || payload?.error) {
            throw new Error(payload?.error || `Status ${res.status}`);
        }
        const text = String(payload?.text || '');
        if (textarea) {
            textarea.value = text || '[Runtime log is currently empty]';
            textarea.scrollTop = textarea.scrollHeight;
        }
        updateRuntimeLogMeta(payload || {});
        const loadedLines = text ? text.split('\n').length : 0;
        const pathLabel = payload?.path ? ` (${payload.path})` : '';
        setRuntimeLogStatus(`Loaded ${loadedLines} line${loadedLines === 1 ? '' : 's'}${pathLabel}.`);
    } catch (err) {
        setRuntimeLogStatus(`Runtime log load failed: ${err.message}`, true);
    }
}

function downloadRuntimeLog() {
    setRuntimeLogStatus('Downloading runtime log...');
    const anchor = document.createElement('a');
    anchor.href = '/api/runtime-log/download';
    anchor.download = 'runtime.log';
    document.body.appendChild(anchor);
    anchor.click();
    anchor.remove();
    setTimeout(() => {
        setRuntimeLogStatus('Runtime log download requested.');
    }, 100);
}

async function clearRuntimeLog() {
    if (!confirm('Clear the runtime log file?')) return;
    setRuntimeLogStatus('Clearing runtime log...');
    try {
        const res = await fetch('/api/runtime-log/clear', {
            method: 'POST',
            credentials: 'same-origin',
        });
        const payload = await res.json();
        if (!res.ok || payload?.error) {
            throw new Error(payload?.error || `Status ${res.status}`);
        }
        await loadRuntimeLog(getRuntimeLogLinesRequested());
        setRuntimeLogStatus('Runtime log cleared.');
    } catch (err) {
        setRuntimeLogStatus(`Runtime log clear failed: ${err.message}`, true);
    }
}

if (document.readyState !== 'loading') {
    applyStoredUserMode();
    loadSettingsUI().catch(() => {});
    bindSettingsDirtyTracking();
} else {
    document.addEventListener('DOMContentLoaded', () => {
        applyStoredUserMode();
        loadSettingsUI().catch(() => {});
        bindSettingsDirtyTracking();
    }, { once: true });
}

/**
 * Bind change/input handlers to all settings form fields.
 * 
 * Tracking Strategy:
 * - Listens to both 'input' (typing) and 'change' (select, checkbox)
 * - Sets settingsDirty flag immediately
 * - Schedules auto-save (800ms debounce)
 * - Special handling for user_mode (immediate save)
 * - Updates status badges on change
 * 
 * Uses dataset flag to prevent duplicate binding.
 * 
 * Special Cases:
 * - user_mode: Immediate save + visibility update
 * - developer-only fields: Trigger mode refresh
 * 
 * Called On:
 * - DOMContentLoaded
 * - After dynamic content updates
 */
function bindSettingsDirtyTracking() {
    const fields = document.querySelectorAll('#Settings input, #Settings textarea, #Settings select');
    fields.forEach(el => {
        if (el.dataset.settingsNoAutosave === '1') return;
        if (el.dataset.settingsDirtyBound) return;
        el.dataset.settingsDirtyBound = 'true';
        el.addEventListener('input', () => {
            settingsDirty = true;
            if (el.id === 'user_mode') {
                setUserMode(el.value);
                // Persist immediately to avoid losing mode if navigation happens quickly
                saveSettings(false, 'user-mode-change').catch(() => {});
            }
            if (el.classList.contains('developer-only')) {
                // Keep developer-only components in sync
                setUserMode(document.getElementById('user_mode')?.value || 'advanced');
            }
            updateSettingsMeta(window.CACHED_SETTINGS || {});
        });
        el.addEventListener('change', () => {
            settingsDirty = true;
            if (el.id === 'user_mode') {
                setUserMode(el.value);
                saveSettings(false, 'user-mode-change').catch(() => {});
            }
            if (el.classList.contains('developer-only')) {
                setUserMode(document.getElementById('user_mode')?.value || 'advanced');
            }
            updateSettingsMeta(window.CACHED_SETTINGS || {});
        });
        el.addEventListener('blur', () => {
            if (!settingsDirty) return;
            if (el.id === 'user_mode') return;
            saveSettings(false, 'field-blur').catch(() => {});
        });
    });
}

/**
 * Load settings from server and apply to UI.
 * 
 * Loading Flow:
 * 1. Fetches from /api/data/settings
 * 2. Applies to UI form fields
 * 3. Persists user_mode to localStorage
 * 4. Initializes offline check (first load only)
 * 5. Updates all status badges
 * 
 * Fallback Behavior:
 * If server load fails:
 * - Applies DEFAULT_SETTINGS
 * - Restores user_mode from localStorage
 * - Shows error alert
 * 
 * Offline Check:
 * On first load (offlineInitialized=false), automatically runs offline
 * status check to populate cache status. Subsequent loads skip this.
 * 
 * Called On:
 * - DOMContentLoaded
 * - Page initialization
 * - After applying stored user mode
 */
async function loadSettingsUI() {
    try {
        const url = '/api/data/settings';
        const res = await fetch(url, { credentials: 'same-origin' });
        if (!res.ok) throw new Error(`Settings load failed (${res.status})`);
        const s = await res.json();
        applySettingsToUI(s);
        loadPromptTemplates().catch(() => {});
        loadTriagePromptTree().catch(() => {});
        loadRuntimeLog().catch(() => {});
        try { localStorage.setItem('user_mode', s.user_mode || 'advanced'); } catch (err) { /* ignore */ }
        if (!offlineInitialized) {
            offlineInitialized = true;
            runOfflineCheck(false).catch((err) => {
                renderOfflineStatus(`Offline check failed: ${err.message}`, true);
            });
        }
    } catch (err) {
        console.error('Settings load error', err);
        alert(`Unable to load settings: ${err.message}`);
        const localMode = (() => {
            try { return localStorage.getItem('user_mode') || 'advanced'; } catch (e) { return 'advanced'; }
        })();
        applySettingsToUI({ ...DEFAULT_SETTINGS, user_mode: localMode });
        loadPromptTemplates().catch(() => {});
        loadTriagePromptTree().catch(() => {});
        loadRuntimeLog().catch(() => {});
    }
}

/**
 * Update settings save status indicator.
 * 
 * Shows:
 * - "Saving…" during save operation
 * - "Saved at [time]" on success
 * - "Save error: [message]" on failure
 * 
 * @param {string} message - Status message to display
 * @param {boolean} isError - If true, shows in red
 */
function updateSettingsStatus(message, isError = false) {
    const el = document.getElementById('settings-save-status');
    if (!el) return;
    el.textContent = message || '';
    el.style.color = isError ? 'var(--red)' : '#2c3e50';
}

/**
 * Apply user mode from localStorage on page load.
 * 
 * Ensures user mode persists across sessions before settings fully load.
 * Falls back to 'user' if localStorage unavailable or empty.
 */
function applyStoredUserMode() {
    try {
        const stored = localStorage.getItem('user_mode') || 'advanced';
        setUserMode(stored);
    } catch (err) { /* ignore */ }
}

/**
 * Schedule debounced auto-save of settings.
 * 
 * Debounce Period: 800ms
 * 
 * Prevents excessive save requests during rapid user input (typing,
 * slider adjustments). Each new input resets the timer.
 * 
 * @param {string} reason - Reason for save (for logging)
 */
function scheduleAutoSave(reason = 'auto') {
    if (settingsAutoSaveTimer) clearTimeout(settingsAutoSaveTimer);
    settingsAutoSaveTimer = setTimeout(() => saveSettings(false, reason), 1500);
}

/**
 * Save settings to server with validation and side effects.
 * 
 * Save Process:
 * 1. Collects form values
 * 2. Validates and normalizes numeric fields
 * 3. Includes vaccine types and pharmacy labels
 * 4. POSTs to /api/data/settings
 * 5. Applies returned settings to UI
 * 6. Persists user_mode to localStorage
 * 7. Clears dirty flag
 * 8. Updates status indicator
 * 9. Triggers prompt preview refresh (if applicable)
 * 
 * Numeric Fields:
 * Validated and coerced to numbers. Falls back to defaults if invalid.
 * Includes: tr_temp, tr_tok, tr_p, tr_k, in_temp, in_tok, in_p, in_k, rep_penalty
 * 
 * Side Effects:
 * - Updates window.CACHED_SETTINGS
 * - Triggers refreshPromptPreview() in chat.js (if prompt not manually edited)
 * - Updates all status meta badges
 * - Shows success/error in status indicator
 * 
 * User Mode Preservation:
 * Preserves locally selected user_mode over server echo to prevent flicker
 * if server has stale data.
 * 
 * @param {boolean} showAlert - Show success/error alert dialog
 * @param {string} reason - Reason for save (for logging and debugging)
 */
async function saveSettings(showAlert = true, reason = 'manual') {
    const saveSeq = ++settingsSaveSequence;
    try {
        const s = {};
        const numeric = new Set(['tr_temp','tr_tok','tr_p','tr_k','in_temp','in_tok','in_p','in_k','rep_penalty']);
        ['triage_instruction','inquiry_instruction','tr_temp','tr_tok','tr_p','tr_k','in_temp','in_tok','in_p','in_k','mission_context','rep_penalty','user_mode','db_write_lock'].forEach(k => {
            const el = document.getElementById(k);
            if (!el) return;
            const val = el.type === 'checkbox' ? el.checked : el.value;
            if (numeric.has(k)) {
                const num = val === '' ? '' : Number(val);
                s[k] = Number.isFinite(num) ? num : DEFAULT_SETTINGS[k];
            } else {
                s[k] = val;
            }
        });
        const offlineToggle = document.getElementById('offline-force-flags');
        if (offlineToggle) s.offline_force_flags = !!offlineToggle.checked;
        s.vaccine_types = normalizeVaccineTypes(vaccineTypeList);
        s.pharmacy_labels = normalizePharmacyLabels(pharmacyLabelList);
        updateSettingsStatus('Saving…', false);
        const headers = { 'Content-Type': 'application/json' };
        const url = '/api/data/settings';
        const res = await fetch(url, {
            method:'POST',
            headers,
            body:JSON.stringify(s),
            credentials:'same-origin'
        });
        if (!res.ok) {
            let detail = '';
            try {
                const err = await res.json();
                detail = err?.error ? `: ${err.error}` : '';
            } catch (_) { /* ignore parse errors */ }
            throw new Error(`Save failed (${res.status})${detail}`);
        }
        const updated = await res.json();
        if (saveSeq !== settingsSaveSequence) {
            return false;
        }
        // Preserve the locally selected user_mode to avoid flicker if the server echoes stale data
        const merged = {
            ...updated,
            user_mode: s.user_mode || updated.user_mode,
            vaccine_types: s.vaccine_types || updated.vaccine_types,
            pharmacy_labels: s.pharmacy_labels || updated.pharmacy_labels
        };
        applySettingsToUI(merged);
        try { localStorage.setItem('user_mode', updated.user_mode || 'advanced'); } catch (err) { /* ignore */ }
        settingsDirty = false;
        updateSettingsStatus(`Saved at ${new Date().toLocaleTimeString()}`);
        if (showAlert) {
            alert("Configuration synchronized.");
        }
        if (typeof refreshPromptPreview === 'function') {
            refreshPromptPreview();
        }
        updateSettingsMeta(updated);
        return true;
    } catch (err) {
        if (saveSeq !== settingsSaveSequence) {
            return false;
        }
        if (showAlert) {
            alert(`Unable to save settings: ${err.message}`);
        }
        updateSettingsStatus(`Save error: ${err.message}`, true);
        console.error('Settings save error', err);
        return false;
    }
}

/**
 * Ensure pending settings changes are persisted before chat submission.
 *
 * This prevents a race where triage/inquiry requests use older persisted
 * model parameters if the user edits Settings and submits chat immediately.
 *
 * @returns {Promise<boolean>} True when settings are synchronized.
 */
async function flushSettingsBeforeChat() {
    if (!settingsLoaded) return true;
    if (settingsAutoSaveTimer) {
        clearTimeout(settingsAutoSaveTimer);
        settingsAutoSaveTimer = null;
    }
    if (!settingsDirty) return true;
    updateSettingsStatus('Saving before consultation…');
    return await saveSettings(false, 'pre-chat-flush');
}

/**
 * Reset a settings section to default values.
 * 
 * Sections:
 * - 'triage': Triage prompt and parameters (temp, tokens, top-p, top-k)
 * - 'inquiry': Inquiry prompt and parameters
 * - 'mission': Mission context description
 * 
 * Immediately saves after reset to persist changes.
 * 
 * @param {string} section - Section identifier
 */
function resetSection(section) {
    if (section === 'triage') {
        document.getElementById('triage_instruction').value = DEFAULT_SETTINGS.triage_instruction;
        document.getElementById('tr_temp').value = DEFAULT_SETTINGS.tr_temp;
        document.getElementById('tr_tok').value = DEFAULT_SETTINGS.tr_tok;
        document.getElementById('tr_p').value = DEFAULT_SETTINGS.tr_p;
        document.getElementById('tr_k').value = DEFAULT_SETTINGS.tr_k;
    } else if (section === 'inquiry') {
        document.getElementById('inquiry_instruction').value = DEFAULT_SETTINGS.inquiry_instruction;
        document.getElementById('in_temp').value = DEFAULT_SETTINGS.in_temp;
        document.getElementById('in_tok').value = DEFAULT_SETTINGS.in_tok;
        document.getElementById('in_p').value = DEFAULT_SETTINGS.in_p;
        document.getElementById('in_k').value = DEFAULT_SETTINGS.in_k;
    } else if (section === 'mission') {
        document.getElementById('mission_context').value = DEFAULT_SETTINGS.mission_context;
    }
    saveSettings();
}

/**
 * Normalize and deduplicate vaccine types list.
 * 
 * Normalization:
 * 1. Ensures array (falls back to defaults if not)
 * 2. Converts all items to trimmed strings
 * 3. Filters empty strings
 * 4. Removes case-insensitive duplicates
 * 5. Preserves original case of first occurrence
 * 
 * Use Cases:
 * - After loading from server
 * - After user adds/removes types
 * - Before saving to server
 * 
 * @param {Array} list - Raw vaccine types array
 * @returns {Array<string>} Normalized unique vaccine types
 */
function normalizeVaccineTypes(list) {
    if (!Array.isArray(list)) return [...DEFAULT_SETTINGS.vaccine_types];
    const seen = new Set();
    return list
        .map((v) => (typeof v === 'string' ? v.trim() : ''))
        .filter((v) => !!v)
        .filter((v) => {
            const key = v.toLowerCase();
            if (seen.has(key)) return false;
            seen.add(key);
            return true;
        });
}

/**
 * Render vaccine types list with management controls.
 * 
 * Displays:
 * - Numbered list of vaccine types
 * - Up/Down buttons for reordering (disabled at boundaries)
 * - Remove button for each type
 * - Empty state message if no types
 * 
 * Button States:
 * - First item: Up button disabled
 * - Last item: Down button disabled
 * - Remove always enabled
 * 
 * Called After:
 * - Settings load
 * - Add/remove/reorder operations
 */
function renderVaccineTypes() {
    const container = document.getElementById('vaccine-types-list');
    if (!container) return;
    if (!vaccineTypeList.length) {
        container.innerHTML = '<div style="color:#666; font-size:12px;">No vaccine types defined. Add at least one to enable the dropdown.</div>';
        return;
    }
    container.innerHTML = vaccineTypeList
        .map((v, idx) => `
            <div style="display:flex; align-items:center; gap:8px; padding:6px 0; border-bottom:1px solid #eee;">
                <div style="width:28px; text-align:right; font-weight:700; color:#666;">${idx + 1}.</div>
                <div style="flex:1; font-weight:600;">${v}</div>
                <div style="display:flex; gap:6px; align-items:center;">
                    <button class="btn btn-sm" style="background:#607d8b;" ${idx === 0 ? 'disabled' : ''} onclick="moveVaccineType(${idx}, -1)">Up</button>
                    <button class="btn btn-sm" style="background:#607d8b;" ${idx === vaccineTypeList.length - 1 ? 'disabled' : ''} onclick="moveVaccineType(${idx}, 1)">Down</button>
                    <button class="btn btn-sm" style="background:var(--red);" onclick="removeVaccineType(${idx})">Remove Remove</button>
                </div>
            </div>
        `).join('');
}

/**
 * Add new vaccine type from input field.
 * 
 * Validation:
 * - Requires non-empty trimmed value
 * - Duplicate prevention via normalization
 * 
 * Process:
 * 1. Gets value from input
 * 2. Validates non-empty
 * 3. Appends to list
 * 4. Normalizes (deduplicates)
 * 5. Clears input
 * 6. Re-renders list
 * 7. Marks dirty + schedules auto-save
 * 
 * Side Effects:
 * - Updates crew.js vaccine dropdowns (via settings sync)
 * - Auto-saves after 800ms
 */
function addVaccineType() {
    const input = document.getElementById('vaccine-type-input');
    if (!input) return;
    const val = input.value.trim();
    if (!val) {
        alert('Enter a vaccine type before adding.');
        return;
    }
    vaccineTypeList = normalizeVaccineTypes([...vaccineTypeList, val]);
    input.value = '';
    renderVaccineTypes();
    settingsDirty = true;
    scheduleAutoSave('vaccine-type-add');
}

/**
 * Remove vaccine type by index.
 * 
 * Process:
 * 1. Validates index bounds
 * 2. Splices from array
 * 3. Normalizes remaining types
 * 4. Re-renders list
 * 5. Marks dirty + schedules auto-save
 * 
 * @param {number} idx - Index to remove
 */
function removeVaccineType(idx) {
    if (idx < 0 || idx >= vaccineTypeList.length) return;
    vaccineTypeList.splice(idx, 1);
    vaccineTypeList = normalizeVaccineTypes(vaccineTypeList);
    renderVaccineTypes();
    settingsDirty = true;
    scheduleAutoSave('vaccine-type-remove');
}

/**
 * Reorder vaccine type (move up or down).
 * 
 * Reordering Logic:
 * - delta=-1: Move up (earlier in list)
 * - delta=+1: Move down (later in list)
 * - Validates destination index is in bounds
 * 
 * Process:
 * 1. Calculates new index
 * 2. Validates bounds
 * 3. Removes item from old position
 * 4. Inserts at new position
 * 5. Normalizes list
 * 6. Re-renders
 * 7. Marks dirty + schedules auto-save
 * 
 * @param {number} idx - Current index
 * @param {number} delta - Direction (-1=up, +1=down)
 */
function moveVaccineType(idx, delta) {
    const newIndex = idx + delta;
    if (newIndex < 0 || newIndex >= vaccineTypeList.length) return;
    const nextList = [...vaccineTypeList];
    const [item] = nextList.splice(idx, 1);
    nextList.splice(newIndex, 0, item);
    vaccineTypeList = normalizeVaccineTypes(nextList);
    renderVaccineTypes();
    settingsDirty = true;
    scheduleAutoSave('vaccine-type-reorder');
}

/**
 * Normalize and deduplicate pharmacy labels list.
 * 
 * Identical logic to normalizeVaccineTypes but for pharmacy labels.
 * Ensures unique, non-empty, trimmed labels with case-insensitive
 * duplicate detection.
 * 
 * @param {Array} list - Raw pharmacy labels array
 * @returns {Array<string>} Normalized unique labels
 */
function normalizePharmacyLabels(list) {
    if (!Array.isArray(list)) return [...DEFAULT_SETTINGS.pharmacy_labels];
    const seen = new Set();
    return list
        .map((v) => (typeof v === 'string' ? v.trim() : ''))
        .filter((v) => !!v)
        .filter((v) => {
            const key = v.toLowerCase();
            if (seen.has(key)) return false;
            seen.add(key);
            return true;
        });
}

/**
 * Render pharmacy labels list with management controls.
 * 
 * Displays:
 * - Numbered list of labels
 * - Up/Down reorder buttons
 * - Remove button
 * - Empty state if no labels
 * 
 * Side Effects:
 * - Updates window.CACHED_SETTINGS.pharmacy_labels
 * - Triggers refreshPharmacyLabelsFromSettings() in pharmacy.js
 *   to sync label dropdowns across medication forms
 * 
 * Integration:
 * Any changes immediately propagate to:
 * - New medication form label dropdown
 * - Existing medication label fields
 * - Pharmacy filter/sort options
 */
function renderPharmacyLabels() {
    const container = document.getElementById('pharmacy-labels-list');
    if (!container) return;
    if (!pharmacyLabelList.length) {
        container.innerHTML = '<div style="color:#666; font-size:12px;">No user labels defined. Add at least one to enable the dropdown.</div>';
        return;
    }
    container.innerHTML = pharmacyLabelList
        .map((v, idx) => `
            <div style="display:flex; align-items:center; gap:8px; padding:6px 0; border-bottom:1px solid #eee;">
                <div style="width:28px; text-align:right; font-weight:700; color:#666;">${idx + 1}.</div>
                <div style="flex:1; font-weight:600;">${v}</div>
                <div style="display:flex; gap:6px; align-items:center;">
                    <button class="btn btn-sm" style="background:#607d8b;" ${idx === 0 ? 'disabled' : ''} onclick="movePharmacyLabel(${idx}, -1)">Up</button>
                    <button class="btn btn-sm" style="background:#607d8b;" ${idx === pharmacyLabelList.length - 1 ? 'disabled' : ''} onclick="movePharmacyLabel(${idx}, 1)">Down</button>
                    <button class="btn btn-sm" style="background:var(--red);" onclick="removePharmacyLabel(${idx})">Remove Remove</button>
                </div>
            </div>
        `).join('');
    window.CACHED_SETTINGS = window.CACHED_SETTINGS || {};
    window.CACHED_SETTINGS.pharmacy_labels = [...pharmacyLabelList];
    if (typeof window.refreshPharmacyLabelsFromSettings === 'function') {
        window.refreshPharmacyLabelsFromSettings(pharmacyLabelList);
    }
}

/**
 * Add new pharmacy label from input field.
 * 
 * Identical flow to addVaccineType but for pharmacy labels.
 * Validates, normalizes, renders, and auto-saves.
 */
function addPharmacyLabel() {
    const input = document.getElementById('pharmacy-label-input');
    if (!input) return;
    const val = input.value.trim();
    if (!val) {
        alert('Enter a label before adding.');
        return;
    }
    pharmacyLabelList = normalizePharmacyLabels([...pharmacyLabelList, val]);
    input.value = '';
    renderPharmacyLabels();
    settingsDirty = true;
    scheduleAutoSave('pharmacy-label-add');
}

/**
 * Remove pharmacy label by index.
 * 
 * @param {number} idx - Index to remove
 */
function removePharmacyLabel(idx) {
    if (idx < 0 || idx >= pharmacyLabelList.length) return;
    pharmacyLabelList.splice(idx, 1);
    pharmacyLabelList = normalizePharmacyLabels(pharmacyLabelList);
    renderPharmacyLabels();
    settingsDirty = true;
    scheduleAutoSave('pharmacy-label-remove');
}

/**
 * Reorder pharmacy label (move up or down).
 * 
 * @param {number} idx - Current index
 * @param {number} delta - Direction (-1=up, +1=down)
 */
function movePharmacyLabel(idx, delta) {
    const newIndex = idx + delta;
    if (newIndex < 0 || newIndex >= pharmacyLabelList.length) return;
    const nextList = [...pharmacyLabelList];
    const [item] = nextList.splice(idx, 1);
    nextList.splice(newIndex, 0, item);
    pharmacyLabelList = normalizePharmacyLabels(nextList);
    renderPharmacyLabels();
    settingsDirty = true;
    scheduleAutoSave('pharmacy-label-reorder');
}

// ============================================================================
// Equipment category settings
// ============================================================================

function normalizeEquipmentCategories(list) {
    if (!Array.isArray(list)) return [...DEFAULT_SETTINGS.equipment_categories];
    const seen = new Set();
    return list
        .map((v) => (typeof v === 'string' ? v.trim() : ''))
        .filter((v) => !!v)
        .filter((v) => {
            const key = v.toLowerCase();
            if (seen.has(key)) return false;
            seen.add(key);
            return true;
        });
}

/**
 * renderEquipmentCategories: function-level behavior note for maintainers.
 * Keep this block synchronized with implementation changes.
 */
function renderEquipmentCategories() {
    const container = document.getElementById('equipment-categories-list');
    if (!container) return;
    if (!equipmentCategoryList.length) {
        container.innerHTML = '<div style="color:#666; font-size:12px;">No equipment categories defined.</div>';
        updateSettingsMeta(window.CACHED_SETTINGS || {});
        return;
    }
    container.innerHTML = equipmentCategoryList
        .map((v, idx) => `
            <div style="display:flex; align-items:center; gap:8px; padding:6px 0; border-bottom:1px solid #eee;">
                <div style="width:28px; text-align:right; font-weight:700; color:#666;">${idx + 1}.</div>
                <div style="flex:1; font-weight:600;">${v}</div>
                <div style="display:flex; gap:6px; align-items:center;">
                    <button class="btn btn-sm" style="background:#607d8b;" ${idx === 0 ? 'disabled' : ''} onclick="moveEquipmentCategory(${idx}, -1)">Up</button>
                    <button class="btn btn-sm" style="background:#607d8b;" ${idx === equipmentCategoryList.length - 1 ? 'disabled' : ''} onclick="moveEquipmentCategory(${idx}, 1)">Down</button>
                    <button class="btn btn-sm" style="background:var(--red);" onclick="removeEquipmentCategory(${idx})">Remove</button>
                </div>
            </div>
        `).join('');
    window.CACHED_SETTINGS = window.CACHED_SETTINGS || {};
    window.CACHED_SETTINGS.equipment_categories = [...equipmentCategoryList];
    if (typeof window.refreshEquipmentCategoriesFromSettings === 'function') {
        window.refreshEquipmentCategoriesFromSettings(equipmentCategoryList, consumableCategoryList);
    }
    updateSettingsMeta(window.CACHED_SETTINGS || {});
}

/**
 * addEquipmentCategory: function-level behavior note for maintainers.
 * Keep this block synchronized with implementation changes.
 */
function addEquipmentCategory() {
    const input = document.getElementById('equipment-category-input');
    if (!input) return;
    const val = input.value.trim();
    if (!val) {
        alert('Enter a category before adding.');
        return;
    }
    equipmentCategoryList = normalizeEquipmentCategories([...equipmentCategoryList, val]);
    input.value = '';
    renderEquipmentCategories();
    settingsDirty = true;
    scheduleAutoSave('equipment-category-add');
}

/**
 * removeEquipmentCategory: function-level behavior note for maintainers.
 * Keep this block synchronized with implementation changes.
 */
function removeEquipmentCategory(idx) {
    if (idx < 0 || idx >= equipmentCategoryList.length) return;
    equipmentCategoryList.splice(idx, 1);
    equipmentCategoryList = normalizeEquipmentCategories(equipmentCategoryList);
    renderEquipmentCategories();
    settingsDirty = true;
    scheduleAutoSave('equipment-category-remove');
}

/**
 * moveEquipmentCategory: function-level behavior note for maintainers.
 * Keep this block synchronized with implementation changes.
 */
function moveEquipmentCategory(idx, delta) {
    const newIndex = idx + delta;
    if (newIndex < 0 || newIndex >= equipmentCategoryList.length) return;
    const nextList = [...equipmentCategoryList];
    const [item] = nextList.splice(idx, 1);
    nextList.splice(newIndex, 0, item);
    equipmentCategoryList = normalizeEquipmentCategories(nextList);
    renderEquipmentCategories();
    settingsDirty = true;
    scheduleAutoSave('equipment-category-reorder');
}

// ============================================================================
// Consumable category settings
// ============================================================================

function normalizeConsumableCategories(list) {
    if (!Array.isArray(list)) return [...DEFAULT_SETTINGS.consumable_categories];
    const seen = new Set();
    return list
        .map((v) => (typeof v === 'string' ? v.trim() : ''))
        .filter((v) => !!v)
        .filter((v) => {
            const key = v.toLowerCase();
            if (seen.has(key)) return false;
            seen.add(key);
            return true;
        });
}

/**
 * renderConsumableCategories: function-level behavior note for maintainers.
 * Keep this block synchronized with implementation changes.
 */
function renderConsumableCategories() {
    const container = document.getElementById('consumable-categories-list');
    if (!container) return;
    if (!consumableCategoryList.length) {
        container.innerHTML = '<div style="color:#666; font-size:12px;">No consumable categories defined.</div>';
        updateSettingsMeta(window.CACHED_SETTINGS || {});
        return;
    }
    container.innerHTML = consumableCategoryList
        .map((v, idx) => `
            <div style="display:flex; align-items:center; gap:8px; padding:6px 0; border-bottom:1px solid #eee;">
                <div style="width:28px; text-align:right; font-weight:700; color:#666;">${idx + 1}.</div>
                <div style="flex:1; font-weight:600;">${v}</div>
                <div style="display:flex; gap:6px; align-items:center;">
                    <button class="btn btn-sm" style="background:#607d8b;" ${idx === 0 ? 'disabled' : ''} onclick="moveConsumableCategory(${idx}, -1)">Up</button>
                    <button class="btn btn-sm" style="background:#607d8b;" ${idx === consumableCategoryList.length - 1 ? 'disabled' : ''} onclick="moveConsumableCategory(${idx}, 1)">Down</button>
                    <button class="btn btn-sm" style="background:var(--red);" onclick="removeConsumableCategory(${idx})">Remove</button>
                </div>
            </div>
        `).join('');
    window.CACHED_SETTINGS = window.CACHED_SETTINGS || {};
    window.CACHED_SETTINGS.consumable_categories = [...consumableCategoryList];
    if (typeof window.refreshEquipmentCategoriesFromSettings === 'function') {
        window.refreshEquipmentCategoriesFromSettings(equipmentCategoryList, consumableCategoryList);
    }
    updateSettingsMeta(window.CACHED_SETTINGS || {});
}

/**
 * addConsumableCategory: function-level behavior note for maintainers.
 * Keep this block synchronized with implementation changes.
 */
function addConsumableCategory() {
    const input = document.getElementById('consumable-category-input');
    if (!input) return;
    const val = input.value.trim();
    if (!val) {
        alert('Enter a category before adding.');
        return;
    }
    consumableCategoryList = normalizeConsumableCategories([...consumableCategoryList, val]);
    input.value = '';
    renderConsumableCategories();
    settingsDirty = true;
    scheduleAutoSave('consumable-category-add');
}

/**
 * removeConsumableCategory: function-level behavior note for maintainers.
 * Keep this block synchronized with implementation changes.
 */
function removeConsumableCategory(idx) {
    if (idx < 0 || idx >= consumableCategoryList.length) return;
    consumableCategoryList.splice(idx, 1);
    consumableCategoryList = normalizeConsumableCategories(consumableCategoryList);
    renderConsumableCategories();
    settingsDirty = true;
    scheduleAutoSave('consumable-category-remove');
}

/**
 * moveConsumableCategory: function-level behavior note for maintainers.
 * Keep this block synchronized with implementation changes.
 */
function moveConsumableCategory(idx, delta) {
    const newIndex = idx + delta;
    if (newIndex < 0 || newIndex >= consumableCategoryList.length) return;
    const nextList = [...consumableCategoryList];
    const [item] = nextList.splice(idx, 1);
    nextList.splice(newIndex, 0, item);
    consumableCategoryList = normalizeConsumableCategories(nextList);
    renderConsumableCategories();
    settingsDirty = true;
    scheduleAutoSave('consumable-category-reorder');
}

/**
 * Render offline status message in settings UI.
 * 
 * Shows:
 * - Check progress messages
 * - Download status with elapsed time
 * - Cache status summary
 * - Error messages
 * 
 * @param {string} msg - HTML message to display
 * @param {boolean} isError - If true, shows in red
 */
function renderOfflineStatus(msg, isError = false) {
    const box = document.getElementById('offline-status');
    if (!box) return;
    box.style.color = isError ? 'var(--red)' : '#2c3e50';
    box.innerHTML = msg;
}

/**
 * Enable/disable offline operation buttons.
 * 
 * Disables during async operations (checking, downloading, backing up)
 * to prevent concurrent operations that could corrupt cache.
 * 
 * Affected Buttons:
 * - offline-flag-btn: Toggle offline flags
 * - offline-download-btn: Download missing models
 * - offline-check-btn: Check cache status
 * - offline-backup-btn: Create backup
 * - offline-restore-btn: Restore from backup
 * 
 * @param {boolean} enabled - True to enable, false to disable
 */
function setOfflineControlsEnabled(enabled) {
    ['offline-flag-btn', 'offline-download-btn', 'offline-check-btn', 'offline-backup-btn', 'offline-restore-btn'].forEach(id => {
        const btn = document.getElementById(id);
        if (btn) {
            btn.disabled = !enabled;
            btn.style.opacity = enabled ? '1' : '0.6';
            btn.style.cursor = enabled ? 'pointer' : 'not-allowed';
        }
    });
    const chk = document.getElementById('offline-force-flags');
    if (chk) chk.disabled = !enabled;
}

/**
 * Create timestamped backup of model cache.
 * 
 * Backup Process:
 * 1. Calls /api/offline/backup (POST)
 * 2. Backend creates a timestamped ZIP snapshot
 * 3. Stores with timestamp in filename
 * 4. Returns backup filename
 * 
 * Use Cases:
 * - Before going offline (insurance against corruption)
 * - After successful model downloads
 * - Before major updates
 * 
 * Recovery:
 * Use restoreOfflineBackup() to restore from latest backup.
 */
async function createOfflineBackup() {
    renderOfflineStatus('Creating offline backup…');
    setOfflineControlsEnabled(false);
    try {
        const res = await fetch('/api/offline/backup', { method: 'POST', credentials: 'same-origin' });
        const data = await res.json();
        if (!res.ok || data.error) throw new Error(data.error || `Status ${res.status}`);
        const file = (data.backup || '').split('/').pop() || data.backup;
        renderOfflineStatus(`Backup created successfully: ${file}`);
    } catch (err) {
        renderOfflineStatus(`Backup failed: ${err.message}`, true);
    } finally {
        setOfflineControlsEnabled(true);
    }
}

/**
 * Restore model cache from latest backup.
 * 
 * Restoration Process:
 * 1. Calls /api/offline/restore (POST)
 * 2. Backend finds latest ZIP backup
 * 3. Extracts to cache directory (overwrites existing)
 * 4. Returns restored backup filename
 * 
 * Alerts user to reload app after restoration to clear in-memory caches.
 * 
 * Use Cases:
 * - Cache corruption detected
 * - Models mysteriously missing after system update
 * - Rollback after failed manual modifications
 */
async function restoreOfflineBackup() {
    renderOfflineStatus('Restoring latest backup…');
    setOfflineControlsEnabled(false);
    try {
        const res = await fetch('/api/offline/restore', { method: 'POST', credentials: 'same-origin' });
        const data = await res.json();
        if (!res.ok || data.error) throw new Error(data.error || `Status ${res.status}`);
        const file = (data.restored || '').split('/').pop() || data.restored;
        renderOfflineStatus(`Backup restored: ${file}`);
        alert('Cache restored. Please reload the app.');
    } catch (err) {
        renderOfflineStatus(`Restore failed: ${err.message}`, true);
    } finally {
        setOfflineControlsEnabled(true);
    }
}

/**
 * Format offline status data into HTML display.
 * 
 * Display Sections:
 * 1. Models: List with cache status (Cached ✅ / Missing ❌)
 * 2. Environment: HF_HUB_OFFLINE, TRANSFORMERS_OFFLINE flags
 * 3. Cache: Cache directory path
 * 4. Status Note: Missing models count or success message
 * 5. Offline Flags: Warning if not set for offline operation
 * 6. Disk Space: Free/total space for cache directory
 * 7. How-To: Step-by-step offline preparation instructions
 * 
 * Required Models:
 * - medgemma-1.5-4b-it: Primary chat model
 * - medgemma-27b-text-it: Advanced model (optional but recommended)
 * - (No photo/OCR model required)
 * 
 * @param {Object} data - Offline status from /api/offline/check
 * @returns {string} Formatted HTML
 */
function formatOfflineStatus(data) {
    const models = data.models || [];
    const missing = data.missing || models.filter((m) => !m.cached);
    const offline = data.offline_mode;
    const env = data.env || {};
    const disk = data.disk || {};
    const esc = (value) => {
        const raw = value == null ? '' : String(value);
        if (window.escapeHtml) return window.escapeHtml(raw);
        return raw
            .replace(/&/g, '&amp;')
            .replace(/</g, '&lt;')
            .replace(/>/g, '&gt;')
            .replace(/"/g, '&quot;')
            .replace(/'/g, '&#39;');
    };
    const totalModels = Number(data.total_models) || models.length;
    const cachedModels = Number(data.cached_models) || models.filter((m) => !!m.cached).length;
    const ready = missing.length === 0;
    const readinessLabel = ready ? 'READY FOR OFFLINE USE' : 'NOT READY FOR OFFLINE USE';
    const readinessColor = ready ? 'var(--green)' : 'var(--red)';

    const modelRows = models.map((m) => {
        const state = m.cached ? 'Cached' : 'Missing';
        const stateColor = m.cached ? 'var(--green)' : 'var(--red)';
        const detailParts = [];
        if (m.downloaded) detailParts.push('downloaded now');
        if (m.error) detailParts.push(`error: ${m.error}`);
        const detail = detailParts.length ? detailParts.join(' | ') : '';
        return `
            <tr>
                <td style="padding:4px 6px; border-bottom:1px solid #e5e7eb;">${esc(m.model)}</td>
                <td style="padding:4px 6px; border-bottom:1px solid #e5e7eb; color:${stateColor}; font-weight:700;">${state}</td>
                <td style="padding:4px 6px; border-bottom:1px solid #e5e7eb; color:#475569;">${esc(detail)}</td>
            </tr>
        `;
    }).join('');

    let nextAction = '';
    if (!ready) {
        nextAction = offline
            ? 'Offline flags are enabled. Disable offline mode temporarily, connect to internet, and download missing models.'
            : 'Connect to internet and click "Download missing models", then re-run readiness check.';
    } else if (!offline) {
        nextAction = 'All models are cached. Before departure, enable offline mode so runtime will not attempt network access.';
    } else {
        nextAction = 'All checks passed. Keep current state for offshore use.';
    }

    const diskLine = disk.total_gb
        ? `${esc(disk.free_gb)} GB free of ${esc(disk.total_gb)} GB`
        : 'Disk information unavailable';

    const envLines = [
        `HF_HUB_OFFLINE=${env.HF_HUB_OFFLINE || 'unset'}`,
        `TRANSFORMERS_OFFLINE=${env.TRANSFORMERS_OFFLINE || 'unset'}`,
        `HF_HOME=${env.HF_HOME || 'unset'}`,
        `HUGGINGFACE_HUB_CACHE=${env.HUGGINGFACE_HUB_CACHE || 'unset'}`,
        `AUTO_DOWNLOAD_MODELS=${env.AUTO_DOWNLOAD_MODELS || 'unset'}`,
    ].map((line) => esc(line)).join('<br>');

    return `
        <div style="padding:10px; border:1px solid #d1d5db; border-radius:8px; background:#ffffff;">
            <div style="font-weight:800; color:${readinessColor}; margin-bottom:6px;">${readinessLabel}</div>
            <div style="font-size:13px; color:#1f2937; margin-bottom:6px;">
                Models cached: <strong>${cachedModels}/${totalModels || '?'}</strong>
            </div>
            <div style="font-size:13px; color:#1f2937; margin-bottom:6px;">
                Offline mode: <strong>${offline ? 'ENABLED' : 'DISABLED'}</strong>
            </div>
            <div style="font-size:13px; color:#1f2937; margin-bottom:8px;">
                Cache disk: <strong>${diskLine}</strong>
            </div>
            <div style="font-size:13px; color:${ready ? '#14532d' : '#991b1b'}; margin-bottom:8px;">
                Next action: ${esc(nextAction)}
            </div>
            <table style="width:100%; border-collapse:collapse; font-size:12px; margin-bottom:8px;">
                <thead>
                    <tr style="background:#f8fafc; text-align:left; color:#334155;">
                        <th style="padding:4px 6px; border-bottom:1px solid #cbd5e1;">Model</th>
                        <th style="padding:4px 6px; border-bottom:1px solid #cbd5e1;">Status</th>
                        <th style="padding:4px 6px; border-bottom:1px solid #cbd5e1;">Details</th>
                    </tr>
                </thead>
                <tbody>
                    ${modelRows || '<tr><td colspan="3" style="padding:6px;">No model status available.</td></tr>'}
                </tbody>
            </table>
            <details style="font-size:12px; color:#475569;">
                <summary style="cursor:pointer; font-weight:700;">Technical details</summary>
                <div style="margin-top:6px;">
                    <strong>Cache dir:</strong> ${esc(data.cache_dir || '')}<br>
                    ${envLines}
                </div>
            </details>
        </div>
    `;
}

/**
 * Update offline flag toggle button text and style.
 * 
 * Button States:
 * - Offline mode ON: "Disable offline flags" (gray background)
 * - Offline mode OFF: "Enable offline flags" (orange/warning background)
 * 
 * Also syncs checkbox state with cache status.
 */
function updateOfflineFlagButton() {
    const btn = document.getElementById('offline-flag-btn');
    const chk = document.getElementById('offline-force-flags');
    if (!btn) return;
    const offline = offlineStatusCache ? !!offlineStatusCache.offline_mode : false;
    btn.textContent = offline ? 'Disable offline mode' : 'Enable offline mode';
    btn.style.background = offline ? '#555' : '#b26a00';
    if (chk) {
        const persisted = !!(window.CACHED_SETTINGS && window.CACHED_SETTINGS.offline_force_flags);
        chk.checked = offlineStatusCache
            ? !!(offlineStatusCache.env?.HF_HUB_OFFLINE === '1' || offlineStatusCache.offline_mode || persisted)
            : persisted;
    }
}

/**
 * Check offline readiness or download missing models.
 * 
 * Two Modes:
 * 1. downloadMissing=false: Check only (GET /api/offline/check)
 *    - Verifies model presence
 *    - Checks environment flags
 *    - Reports disk space
 *    - Fast operation
 * 
 * 2. downloadMissing=true: Download + Check (POST /api/offline/ensure)
 *    - Downloads any missing models from Hugging Face
 *    - Shows progress spinner with elapsed time
 *    - Can take minutes for large models
 *    - Requires internet connection
 * 
 * Download Behavior:
 * - Skips download if offline flags enabled (shows warning)
 * - Downloads to HF_HOME cache directory
 * - Verifies integrity after download
 * - Updates status with completion message
 * 
 * UI Updates:
 * - Disables buttons during operation
 * - Shows spinner for downloads
 * - Updates cache status display
 * - Refreshes meta badges
 * 
 * Called By:
 * - Settings page load (check only, first time)
 * - "Check cache status" button (check only)
 * - "Download missing models" button (download mode)
 * 
 * @param {boolean} downloadMissing - If true, downloads missing models
 */
async function runOfflineCheck(downloadMissing = false) {
    renderOfflineStatus(downloadMissing ? 'Checking and downloading missing models…' : 'Checking offline requirements…');
    setOfflineControlsEnabled(false);
    let spinner = null;
    const start = Date.now();
    if (downloadMissing) {
        spinner = setInterval(() => {
            const el = document.getElementById('offline-status');
            if (!el) return;
            const secs = Math.floor((Date.now() - start) / 1000);
            const dots = '.'.repeat((secs % 3) + 1);
            el.innerHTML = `Downloading missing models${dots}<br><small>${secs}s elapsed</small>`;
        }, 500);
    }
    try {
        const endpoint = downloadMissing ? '/api/offline/ensure' : '/api/offline/check';
        const res = await fetch(endpoint, { credentials: 'same-origin', method: downloadMissing ? 'POST' : 'GET' });
        const data = await res.json();
        if (!res.ok || data.error) throw new Error(data.error || `Status ${res.status}`);
        offlineStatusCache = data;
        let message = formatOfflineStatus(data);
        if (downloadMissing && data.download_requested && !data.download_allowed) {
            message += '<div style="margin-top:6px; color:#b26a00;">Download was not allowed in the current mode. Disable offline mode and retry while connected.</div>';
        } else if (downloadMissing && data.missing && data.missing.length) {
            message += '<div style="margin-top:6px; color:#b26a00;">Some models still missing. Stay online and try again.</div>';
        } else if (downloadMissing && data.models && data.models.some(m => m.downloaded)) {
            message += '<div style="margin-top:6px; color:var(--green);">Downloads completed.</div>';
        } else if (downloadMissing && (!data.models || !data.models.some(m => m.downloaded))) {
            message += '<div style="margin-top:6px; color:#475569;">No downloads were needed; cache was already complete.</div>';
        }
        renderOfflineStatus(message);
        updateOfflineFlagButton();
        updateSettingsMeta(window.CACHED_SETTINGS || {});
        if (typeof window.refreshModelAvailability === 'function') {
            window.refreshModelAvailability({ silent: true }).catch(() => {});
        }
    } catch (err) {
        renderOfflineStatus(`Error: ${err.message}. If missing models persist, stay online and click "Download missing models".`, true);
    } finally {
        if (spinner) clearInterval(spinner);
        setOfflineControlsEnabled(true);
    }
}

/**
 * Toggle offline environment flags.
 * 
 * Flags Controlled:
 * - HF_HUB_OFFLINE: Prevents Hugging Face Hub connectivity checks
 * - TRANSFORMERS_OFFLINE: Blocks transformer library from downloading
 * 
 * Toggle Logic:
 * - If forceEnable specified: Sets to that value
 * - Otherwise: Toggles opposite of current state
 * 
 * Backend Implementation:
 * Sets environment variables in app process. May require app restart
 * in some deployment scenarios.
 * 
 * Use Cases:
 * - Enable before going offline (after downloads complete)
 * - Disable when returning to port (to allow updates)
 * 
 * @param {boolean} forceEnable - Optional explicit enable/disable
 */
async function toggleOfflineFlags(forceEnable) {
    const desired = typeof forceEnable === 'boolean'
        ? forceEnable
        : !(offlineStatusCache ? !!offlineStatusCache.offline_mode : false);
    renderOfflineStatus(desired ? 'Enabling offline flags…' : 'Disabling offline flags…');
    try {
        const res = await fetch('/api/offline/flags', {
            method: 'POST',
            credentials: 'same-origin',
            headers: { 'Content-Type': 'application/json' },
            body: JSON.stringify({ enable: desired })
        });
        const data = await res.json();
        if (!res.ok || data.error) throw new Error(data.error || `Status ${res.status}`);
        offlineStatusCache = data;
        renderOfflineStatus(formatOfflineStatus(data));
        updateOfflineFlagButton();
        updateSettingsMeta(window.CACHED_SETTINGS || {});
        if (typeof window.refreshModelAvailability === 'function') {
            window.refreshModelAvailability({ silent: true }).catch(() => {});
        }
    } catch (err) {
        renderOfflineStatus(`Unable to set offline flags: ${err.message}`, true);
    }
}

/**
 * Export default demo dataset to workspace.
 * 
 * Exports:
 * - Sample crew members with medical histories
 * - Example medications
 * - Sample chat history
 * 
 * Use Cases:
 * - New workspace initialization
 * - Testing after database corruption
 * - Demo/training scenarios
 * - Reference data restoration
 * 
 * Warns Before Overwrite:
 * Existing data may be overwritten. Backup first if needed.
 */
async function exportDefaultDataset() {
    const status = document.getElementById('default-export-status');
    if (status) {
        status.textContent = 'Exporting default dataset…';
        status.style.color = '#2c3e50';
    }
    try {
        const res = await fetch('/api/default/export', {
            method: 'POST',
            credentials: 'same-origin'
        });
        const data = await res.json().catch(() => ({}));
        if (!res.ok || data.error) {
            throw new Error(data.error || `Status ${res.status}`);
        }
        if (status) {
            status.textContent = `Exported: ${Array.isArray(data.written) ? data.written.join(', ') : 'default data'}`;
            status.style.color = '#2e7d32';
        } else {
            alert('Default dataset exported.');
        }
    } catch (err) {
        if (status) {
            status.textContent = `Export failed: ${err.message}`;
            status.style.color = 'var(--red)';
        } else {
            alert(`Export failed: ${err.message}`);
        }
    }
}

/**
 * Load and render crew credentials management UI.
 * 
 * Display:
 * - Lists all crew members
 * - Shows username/password inputs for each
 * - Auto-saves on input (400ms debounce)
 * - Shows save status per crew member
 * 
 * Credentials Purpose:
 * Future feature for per-crew-member authentication. Currently stored
 * but not enforced for login.
 * 
 * Integration:
 * Loads crew from /api/data/patients (same as crew.js)
 * Saves credentials to /api/crew/credentials
 * 
 * Called By:
 * - Settings page load
 * - Crew credentials section expansion
 */
async function loadCrewCredentials() {
    const container = document.getElementById('crew-credentials');
    if (!container) return;
    container.innerHTML = 'Loading crew...';
    try {
        const data = await (await fetch('/api/data/patients', {credentials:'same-origin'})).json();
        if (!data || data.length === 0) {
            container.innerHTML = '<div style="color:#666;">No crew members found.</div>';
            updateCrewCredMeta(0);
            return;
        }
        container.innerHTML = data.map(p => `
            <div style="display:flex; gap:10px; align-items:center; margin-bottom:10px; flex-wrap:wrap;">
                <div style="min-width:180px; font-weight:bold;">${p.firstName || ''} ${p.lastName || p.name || ''}</div>
                <input type="text" id="cred-user-${p.id}" value="${p.username || ''}" placeholder="Username" style="padding:8px; flex:1; min-width:140px;" oninput="debouncedSaveCred('${p.id}')">
                <input type="text" id="cred-pass-${p.id}" value="${p.password || ''}" placeholder="Password" style="padding:8px; flex:1; min-width:140px;" oninput="debouncedSaveCred('${p.id}')">
                <span id="cred-status-${p.id}" style="font-size:12px; color:#666;"></span>
            </div>
        `).join('');
        const credCount = data.filter(p => (p.username || '').trim() || (p.password || '').trim()).length;
        updateCrewCredMeta(credCount);
    } catch (err) {
        container.innerHTML = `<div style="color:red;">Error loading crew: ${err.message}</div>`;
        updateCrewCredMeta(0);
    }
}

/**
 * Save login credentials for a single crew member.
 * 
 * Save Process:
 * 1. Collects username and password from inputs
 * 2. POSTs to /api/crew/credentials
 * 3. Updates status indicator
 * 4. Clears status after 1.5s
 * 
 * Debounced via debouncedSaveCred (400ms) to prevent save spam
 * during typing.
 * 
 * @param {string} id - Crew member ID
 */
const credSaveTimers = {};
async function saveCrewCredential(id) {
    const userEl = document.getElementById(`cred-user-${id}`);
    const passEl = document.getElementById(`cred-pass-${id}`);
    const statusEl = document.getElementById(`cred-status-${id}`);
    if (!userEl || !passEl) return;
    statusEl.textContent = 'Saving…';
    statusEl.style.color = '#666';
    try {
        const res = await fetch('/api/crew/credentials', {
            method:'POST',
            headers:{'Content-Type':'application/json'},
            credentials:'same-origin',
            body: JSON.stringify({id, username: userEl.value, password: passEl.value})
        });
        const text = await res.text();
        if (!res.ok) throw new Error(text || `Status ${res.status}`);
        statusEl.textContent = 'Saved';
        statusEl.style.color = '#2e7d32';
        setTimeout(() => { if (statusEl.textContent === 'Saved') statusEl.textContent=''; }, 1500);
    } catch (err) {
        statusEl.textContent = `Save failed: ${err.message}`;
        statusEl.style.color = 'var(--red)';
    }
}

/**
 * Debounce wrapper for crew credential saves.
 * 
 * Debounce Period: 400ms
 * 
 * Each crew member has independent timer to allow editing multiple
 * crew credentials simultaneously without interference.
 * 
 * @param {string} id - Crew member ID
 */
function debouncedSaveCred(id) {
    clearTimeout(credSaveTimers[id]);
    credSaveTimers[id] = setTimeout(() => saveCrewCredential(id), 400);
}

/**
 * Return to login/splash from Settings.
 *
 * Behavior:
 * - If crew credentials exist, log out current session first so next access requires login.
 * - If no credentials exist, just navigate to the login/splash screen.
 */
async function returnToLoginSplash() {
    try {
        const res = await fetch('/api/auth/meta', { credentials: 'same-origin' });
        if (!res.ok) throw new Error(`Status ${res.status}`);
        const data = await res.json();
        window.location.href = data && data.has_credentials ? '/logout' : '/login';
    } catch (_) {
        // Fail safe: force logout to avoid keeping an authenticated session unexpectedly.
        window.location.href = '/logout';
    }
}

// Expose functions to window for inline onclick handlers
window.saveSettings = saveSettings;
window.flushSettingsBeforeChat = flushSettingsBeforeChat;
window.resetSection = resetSection;
window.loadCrewCredentials = loadCrewCredentials;
window.saveCrewCredential = saveCrewCredential;
window.returnToLoginSplash = returnToLoginSplash;
window.setUserMode = setUserMode;
window.runOfflineCheck = runOfflineCheck;
window.createOfflineBackup = createOfflineBackup;
window.restoreOfflineBackup = restoreOfflineBackup;
window.addVaccineType = addVaccineType;
window.removeVaccineType = removeVaccineType;
window.moveVaccineType = moveVaccineType;
window.addPharmacyLabel = addPharmacyLabel;
window.removePharmacyLabel = removePharmacyLabel;
window.movePharmacyLabel = movePharmacyLabel;
window.addEquipmentCategory = addEquipmentCategory;
window.removeEquipmentCategory = removeEquipmentCategory;
window.moveEquipmentCategory = moveEquipmentCategory;
window.addConsumableCategory = addConsumableCategory;
window.removeConsumableCategory = removeConsumableCategory;
window.moveConsumableCategory = moveConsumableCategory;
window.loadRuntimeLog = loadRuntimeLog;
window.downloadRuntimeLog = downloadRuntimeLog;
window.clearRuntimeLog = clearRuntimeLog;


//

// MAINTENANCE NOTE
// Historical auto-generated note blocks were removed because they were repetitive and
// obscured real logic changes during review. Keep focused comments close to behavior-
// critical code paths (UI state hydration, async fetch lifecycle, and mode-gated
// controls) so maintenance remains actionable.