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

app = dash.Dash(__name__, suppress_callback_exceptions=True, external_stylesheets=['assets/styles.css'])
server = app.server

# Navigation menu component
def create_navbar(current_pathname="/"):
    return html.Div([
        html.Div([
            dcc.Link("Interdependence Analysis Table", href="/", className="nav-link" + (" active" if current_pathname == "/" else "")),
            dcc.Link("Assumptions", href="/assumptions", className="nav-link" + (" active" if current_pathname == "/assumptions" else ""))
        ], style={"textAlign": "left", "marginBottom": "20px"}),
    ])

DATA_FILE = "./V6/IA_V6.csv"
if os.path.exists(DATA_FILE):
    df = pd.read_csv(DATA_FILE)

color_options = ["red", "yellow", "green", "orange"]

# Updated dropdown options for all categorical columns
dropdowns = {
    # Original color-based columns
    "Human*": {
        'options': [{'label': c.capitalize(), 'value': c} for c in color_options]
    },
    "TARS": {
        'options': [{'label': c.capitalize(), 'value': c} for c in color_options]
    },
    "TARS*": {
        'options': [{'label': c.capitalize(), 'value': c} for c in color_options]
    },
    "Human": {
        'options': [{'label': c.capitalize(), 'value': c} for c in color_options]
    },
    # New categorical columns
    "Category": {
        'options': [
            {'label': 'NORM', 'value': 'NORM'},
            {'label': 'ABNORM', 'value': 'ABNORM'},
            {'label': 'EMER', 'value': 'EMER'}
        ]
    },
    "Type": {
        'options': [
            {'label': 'SOP', 'value': 'SOP'},
            {'label': 'Checklist', 'value': 'Checklist'},
            {'label': 'Memory Item', 'value': 'Memory Item'}
        ]
    },
    "Constraint Type": {
        'options': [
            {'label': 'Soft-time', 'value': 'Soft-time'},
            {'label': 'Hard-time', 'value': 'Hard-time'}
        ]
    },
    "Execution Type": {
        'options': [
            {'label': 'One-off', 'value': 'One-off'},
            {'label': 'Continuous', 'value': 'Continuous'},
            {'label': 'Cyclic', 'value': 'Cyclic'},
            {'label': 'Monitoring', 'value': 'Monitoring'}
        ]
    }
}
def style_table(df):
    styles = []
    # Only apply color styling to the color-based columns
    color_columns = ["Human*", "TARS", "TARS*", "Human"]
    
    for i, row in df.iterrows():
        for col in color_columns:
            if col in df.columns:
                value = row[col]
                if isinstance(value, str) and value.strip().lower() in ["red", "yellow", "green", "orange"]:
                    color = value.strip().lower()
                    styles.append({
                        'if': {'row_index': i, 'column_id': col},
                        'backgroundColor': color,
                        'color': color,  # Text color matches background (invisible text)
                        'textAlign': 'center',
                        'fontWeight': 'bold'
                    })
    return styles

# No need to assign Row since it already exists in the CSV
# df = df.assign(Row=lambda x: x.index + 1)

# Updated editable columns to include new fields
editable_columns = [
    "Procedure", "Category", "Type", "Task Object", "Value", 
    "Human*", "TARS", "TARS*", "Human", 
    "Observability", "Predictability", "Directability", 
    "Information Requirement", "Constraint Type", "Time constraint (in s)", "Execution Type"
]

table = dash_table.DataTable(
    id='responsibility-table',
    columns=[
        {"name": col, "id": col, "editable": col in editable_columns}
        for col in df.columns
    ],
    data=df.to_dict("records"),  # Remove the .assign() since Row already exists
    editable=True,
    row_deletable=True,
    dropdown=dropdowns,
    style_data_conditional=style_table(df),
    style_cell={'textAlign': 'left', 'padding': '5px', 'whiteSpace': 'normal'},
    style_cell_conditional=[
        {
            'if': {'column_id': 'Human*'},
            'borderLeft': '3px solid black'
        },
        {
            'if': {'column_id': 'Human'},
            'borderRight': '3px solid black'
        },
        {
            'if': {'column_id': 'TARS'},
            'borderRight': '2px solid black'
        }
    ],
    style_header={'fontWeight': 'bold', 'backgroundColor': '#f0f0f0'},
    style_table={'overflowX': 'auto', 'border': '1px solid lightgrey'},
    active_cell={"row": 0, "column_id":"Objective", "column":1}
)

columns=[
    {"name": "Row", "id": "Row", "editable": False},
    {"name": "Procedure", "id": "Procedure", "editable": True},
    {"name": "Category", "id": "Category", "editable": True, "presentation": "dropdown"},
    {"name": "Type", "id": "Type", "editable": True, "presentation": "dropdown"},
    {"name": "Task Object", "id": "Task Object", "editable": True},
    {"name": "Value", "id": "Value", "editable": True},
    {"name": "Human*", "id": "Human*", "editable": True, "presentation": "dropdown"},
    {"name": "TARS", "id": "TARS", "editable": True, "presentation": "dropdown"},
    {"name": "TARS*", "id": "TARS*", "editable": True, "presentation": "dropdown"},
    {"name": "Human", "id": "Human", "editable": True, "presentation": "dropdown"},
    {"name": "Observability", "id": "Observability", "editable": True},
    {"name": "Predictability", "id": "Predictability", "editable": True},
    {"name": "Directability", "id": "Directability", "editable": True},
    {"name": "Information Requirement", "id": "Information Requirement", "editable": True},
    {"name": "Constraint Type", "id": "Constraint Type", "editable": True, "presentation": "dropdown"},
    {"name": "Time constraint (in s)", "id": "Time constraint (in s)", "editable": True, "type": "numeric"},
    {"name": "Execution Type", "id": "Execution Type", "editable": True, "presentation": "dropdown"},
]

def wrap_text(text, max_width=30):
    import textwrap
    if not isinstance(text, str):
        return ""
    return '<br>'.join(textwrap.wrap(text, width=max_width))


def get_chosen_agent_with_override(row, highlight_track, category_overrides, agent_colors):
    """
    Determine the chosen agent for highlighting, considering category overrides.
    
    Args:
        row: DataFrame row containing task data
        highlight_track: The base highlight strategy
        category_overrides: Dict mapping category names to "HUMAN" or "TARS"
        agent_colors: Dict with "HUMAN*" and "TARS*" color values
    
    Returns:
        The chosen agent ("HUMAN*" or "TARS*") or None
    """
    category = row.get("Category", "")
    
    # Check if there's an override for this category
    if category in category_overrides:
        override = category_overrides[category]
        if override == "TARS":
            # Use TARS* if available (not red)
            if agent_colors["TARS*"] in ["green", "yellow", "orange"]:
                return "TARS*"
            # Fall back to HUMAN* if TARS* not available
            elif agent_colors["HUMAN*"] in ["green", "yellow", "orange"]:
                return "HUMAN*"
        elif override == "HUMAN":
            # Use HUMAN* if available (not red)
            if agent_colors["HUMAN*"] in ["green", "yellow", "orange"]:
                return "HUMAN*"
            # Fall back to TARS* if HUMAN* not available
            elif agent_colors["TARS*"] in ["green", "yellow", "orange"]:
                return "TARS*"
        return None
    
    # No override - use base strategy
    if highlight_track in ["human_baseline", "human_full_support"]:
        # Human performer only
        if agent_colors["HUMAN*"] in ["green", "yellow", "orange"]:
            return "HUMAN*"
    elif highlight_track in ["agent_whenever_possible", "agent_whenever_possible_full_support"]:
        # TARS* preferred when available
        if agent_colors["TARS*"] in ["green", "yellow", "orange"]:
            return "TARS*"
        elif agent_colors["HUMAN*"] in ["green", "yellow", "orange"]:
            return "HUMAN*"
    elif highlight_track == "most_reliable":
        # Most reliable (green > yellow preference, HUMAN* first)
        if agent_colors["HUMAN*"] == "green":
            return "HUMAN*"
        elif agent_colors["TARS*"] == "green":
            return "TARS*"
        elif agent_colors["HUMAN*"] == "yellow":
            return "HUMAN*"
        elif agent_colors["TARS*"] == "yellow":
            return "TARS*"
    
    return None


def build_performers_only_figure_base(df, procedure):
    """Build base figure for performers-only view (single procedure) without highlighting."""
    agents = ["HUMAN*", "TARS*"]
    VALID_COLORS = {"red", "yellow", "green", "orange", "black", "grey"}
    
    proc_df = df[df["Procedure"] == procedure].reset_index(drop=True)
    proc_df["task_idx"] = proc_df.index
    tasks = proc_df["Task Object"].tolist()
    height = 400 + len(tasks) * 60
    
    dots = []
    dashed_arrows = []
    black_to_black_arrows = []
    
    for idx, row in proc_df.iterrows():
        task_idx = row["task_idx"]
        black_points = []
        
        # HUMAN* performer
        human_performer_val = row["Human*"].strip().lower() if isinstance(row["Human*"], str) else ""
        if human_performer_val in VALID_COLORS and human_performer_val != "red":
            dots.append({"task": task_idx, "agent": "HUMAN*", "shape": "circle", "color": human_performer_val, "role": "performer"})
            black_points.append("HUMAN*")
        
        # TARS* performer
        tars_performer_val = row["TARS*"].strip().lower() if isinstance(row["TARS*"], str) else ""
        if tars_performer_val in VALID_COLORS and tars_performer_val != "red":
            dots.append({"task": task_idx, "agent": "TARS*", "shape": "circle", "color": tars_performer_val, "role": "performer"})
            black_points.append("TARS*")
        
        # TARS supporter (square in TARS* column)
        tars_supporter_val = row["TARS"].strip().lower() if isinstance(row["TARS"], str) and row["TARS"] else ""
        if tars_supporter_val in VALID_COLORS and tars_supporter_val != "red":
            dots.append({"task": task_idx, "agent": "TARS*", "shape": "square", "color": tars_supporter_val, "role": "supporter"})
        
        # HUMAN supporter (square in HUMAN* column)
        human_supporter_val = row["Human"].strip().lower() if isinstance(row["Human"], str) and row["Human"] else ""
        if human_supporter_val in VALID_COLORS and human_supporter_val != "red":
            dots.append({"task": task_idx, "agent": "HUMAN*", "shape": "square", "color": human_supporter_val, "role": "supporter"})
        
        # Dashed arrows for support relationships (going BETWEEN columns)
        # TARS supporter helps HUMAN* performer: arrow from TARS* to HUMAN*
        if "HUMAN*" in black_points and tars_supporter_val in VALID_COLORS and tars_supporter_val != "red":
            dashed_arrows.append({"start_agent": "TARS*", "end_agent": "HUMAN*", "task": task_idx, "is_support": True})
        
        # HUMAN supporter helps TARS* performer: arrow from HUMAN* to TARS*
        if "TARS*" in black_points and human_supporter_val in VALID_COLORS and human_supporter_val != "red":
            dashed_arrows.append({"start_agent": "HUMAN*", "end_agent": "TARS*", "task": task_idx, "is_support": True})
    
    # Black-to-black arrows between consecutive tasks
    for i in range(len(proc_df) - 1):
        current_performers = [d["agent"] for d in dots if d["task"] == i and d["shape"] == "circle"]
        next_performers = [d["agent"] for d in dots if d["task"] == i + 1 and d["shape"] == "circle"]
        for curr in current_performers:
            for nxt in next_performers:
                black_to_black_arrows.append({
                    "start_task": i, "start_agent": curr,
                    "end_task": i + 1, "end_agent": nxt
                })
    
    # Build figure
    agent_pos = {agent: i for i, agent in enumerate(agents)}
    fig = go.Figure()
    
    for dot in dots:
        row = proc_df.iloc[dot["task"]]
        hover_text = f"<b>Task:</b> {wrap_text(row['Task Object'])}<br><b>Agent:</b> {dot['agent']}<br><b>Role:</b> {dot['role']}"
        
        if dot["shape"] == "circle":
            fig.add_trace(go.Scatter(
                x=[agent_pos[dot["agent"]]], y=[dot["task"]], mode="markers",
                marker=dict(size=30, color=dot["color"]), showlegend=False,
                hoverinfo="text", hovertext=hover_text
            ))
        else:  # square - add black border for visibility
            # Draw black border square first
            fig.add_trace(go.Scatter(
                x=[agent_pos[dot["agent"]]], y=[dot["task"]], mode="markers",
                marker=dict(size=18, color="black", symbol="square"),
                showlegend=False, hoverinfo="skip"
            ))
            # Draw colored square on top
            fig.add_trace(go.Scatter(
                x=[agent_pos[dot["agent"]]], y=[dot["task"]], mode="markers",
                marker=dict(size=14, color=dot["color"], symbol="square"),
                showlegend=False, hoverinfo="text", hovertext=hover_text
            ))
    
    # Add dashed arrows (shapes) - stored for later highlighting
    # Group by task to apply vertical offset when there are two arrows at same task
    dashed_arrow_info = []
    arrows_by_task = {}
    for arrow in dashed_arrows:
        task = arrow["task"]
        if task not in arrows_by_task:
            arrows_by_task[task] = []
        arrows_by_task[task].append(arrow)
    
    for task, task_arrows in arrows_by_task.items():
        if len(task_arrows) == 2:
            offsets = [-0.08, 0.08]  # Vertical offset for distinguishability
        else:
            offsets = [0] * len(task_arrows)
        
        for arrow, offset in zip(task_arrows, offsets):
            fig.add_shape(
                type="line",
                x0=agent_pos[arrow["start_agent"]], y0=arrow["task"] + offset,
                x1=agent_pos[arrow["end_agent"]], y1=arrow["task"] + offset,
                line=dict(color="black", width=2, dash="dot")
            )
            dashed_arrow_info.append({"task": arrow["task"], "end_agent": arrow["end_agent"], "offset": offset})
    
    # Add black-to-black arrows (annotations) - stored for later highlighting
    for arrow in black_to_black_arrows:
        fig.add_annotation(
            x=agent_pos[arrow["end_agent"]], y=arrow["end_task"],
            ax=agent_pos[arrow["start_agent"]], ay=arrow["start_task"],
            xref="x", yref="y", axref="x", ayref="y",
            showarrow=True, arrowhead=3, arrowsize=1, arrowwidth=2, arrowcolor="black", opacity=0.9,
            standoff=18, startstandoff=18
        )
    
    fig.update_layout(
        title=f"Workflow for {procedure} - Performers Only",
        xaxis=dict(tickvals=list(agent_pos.values()), ticktext=list(agent_pos.keys()), title="Agent", showgrid=True, gridcolor='lightgrey', range=[-0.5, 1.5]),
        yaxis=dict(tickvals=list(range(len(tasks))), ticktext=[wrap_text(t) for t in tasks], title="Task", autorange="reversed", showgrid=True, gridcolor='lightgrey'),
        margin=dict(l=250, r=50, t=50, b=50), height=height, plot_bgcolor='white', paper_bgcolor='white'
    )
    
    arrow_info = {"dashed_arrows": dashed_arrow_info, "black_to_black_arrows": black_to_black_arrows}
    return fig, arrow_info


def build_performers_only_combined_figure_base(df):
    """Build base figure for performers-only combined view without highlighting."""
    agents = ["HUMAN*", "TARS*"]
    VALID_COLORS = {"red", "yellow", "green", "orange", "black", "grey"}
    
    df = df.reset_index(drop=True)
    df["task_idx"] = df.index
    tasks = df["Task Object"].tolist()
    height = len(tasks) * 100
    
    dots = []
    dashed_arrows = []
    black_to_black_arrows = []
    
    for idx, row in df.iterrows():
        task_idx = row["task_idx"]
        black_points = []
        
        human_performer_val = row["Human*"].strip().lower() if isinstance(row["Human*"], str) else ""
        if human_performer_val in VALID_COLORS and human_performer_val != "red":
            dots.append({"task": task_idx, "agent": "HUMAN*", "shape": "circle", "color": human_performer_val, "role": "performer"})
            black_points.append("HUMAN*")
        
        tars_performer_val = row["TARS*"].strip().lower() if isinstance(row["TARS*"], str) else ""
        if tars_performer_val in VALID_COLORS and tars_performer_val != "red":
            dots.append({"task": task_idx, "agent": "TARS*", "shape": "circle", "color": tars_performer_val, "role": "performer"})
            black_points.append("TARS*")
        
        tars_supporter_val = row["TARS"].strip().lower() if isinstance(row["TARS"], str) and row["TARS"] else ""
        if tars_supporter_val in VALID_COLORS and tars_supporter_val != "red":
            dots.append({"task": task_idx, "agent": "TARS*", "shape": "square", "color": tars_supporter_val, "role": "supporter"})
        
        human_supporter_val = row["Human"].strip().lower() if isinstance(row["Human"], str) and row["Human"] else ""
        if human_supporter_val in VALID_COLORS and human_supporter_val != "red":
            dots.append({"task": task_idx, "agent": "HUMAN*", "shape": "square", "color": human_supporter_val, "role": "supporter"})
        
        # Dashed arrows for support relationships (going BETWEEN columns)
        # TARS supporter helps HUMAN* performer: arrow from TARS* to HUMAN*
        if "HUMAN*" in black_points and tars_supporter_val in VALID_COLORS and tars_supporter_val != "red":
            dashed_arrows.append({"start_agent": "TARS*", "end_agent": "HUMAN*", "task": task_idx, "is_support": True})
        
        # HUMAN supporter helps TARS* performer: arrow from HUMAN* to TARS*
        if "TARS*" in black_points and human_supporter_val in VALID_COLORS and human_supporter_val != "red":
            dashed_arrows.append({"start_agent": "HUMAN*", "end_agent": "TARS*", "task": task_idx, "is_support": True})
    
    for i in range(len(df) - 1):
        current_performers = [d["agent"] for d in dots if d["task"] == i and d["shape"] == "circle"]
        next_performers = [d["agent"] for d in dots if d["task"] == i + 1 and d["shape"] == "circle"]
        for curr in current_performers:
            for nxt in next_performers:
                black_to_black_arrows.append({"start_task": i, "start_agent": curr, "end_task": i + 1, "end_agent": nxt})
    
    agent_pos = {agent: i for i, agent in enumerate(agents)}
    fig = go.Figure()
    
    for dot in dots:
        row = df.iloc[dot["task"]]
        hover_text = f"<b>Task:</b> {wrap_text(row['Task Object'])}<br><b>Agent:</b> {dot['agent']}<br><b>Role:</b> {dot['role']}"
        
        if dot["shape"] == "circle":
            fig.add_trace(go.Scatter(
                x=[agent_pos[dot["agent"]]], y=[dot["task"]], mode="markers",
                marker=dict(size=40, color=dot["color"]), showlegend=False,
                hoverinfo="text", hovertext=hover_text
            ))
        else:  # square - add black border for visibility
            # Draw black border square first
            fig.add_trace(go.Scatter(
                x=[agent_pos[dot["agent"]]], y=[dot["task"]], mode="markers",
                marker=dict(size=20, color="black", symbol="square"),
                showlegend=False, hoverinfo="skip"
            ))
            # Draw colored square on top
            fig.add_trace(go.Scatter(
                x=[agent_pos[dot["agent"]]], y=[dot["task"]], mode="markers",
                marker=dict(size=16, color=dot["color"], symbol="square"),
                showlegend=False, hoverinfo="text", hovertext=hover_text
            ))
    
    # Group dashed arrows by task for vertical offset when overlapping
    dashed_arrow_info = []
    arrows_by_task = {}
    for arrow in dashed_arrows:
        task = arrow["task"]
        if task not in arrows_by_task:
            arrows_by_task[task] = []
        arrows_by_task[task].append(arrow)
    
    for task, task_arrows in arrows_by_task.items():
        if len(task_arrows) == 2:
            offsets = [-0.08, 0.08]  # Vertical offset for distinguishability
        else:
            offsets = [0] * len(task_arrows)
        
        for arrow, offset in zip(task_arrows, offsets):
            fig.add_shape(
                type="line",
                x0=agent_pos[arrow["start_agent"]], y0=arrow["task"] + offset,
                x1=agent_pos[arrow["end_agent"]], y1=arrow["task"] + offset,
                line=dict(color="black", width=2, dash="dot")
            )
            dashed_arrow_info.append({"task": arrow["task"], "end_agent": arrow["end_agent"], "offset": offset})
    
    for arrow in black_to_black_arrows:
        fig.add_annotation(
            x=agent_pos[arrow["end_agent"]], y=arrow["end_task"],
            ax=agent_pos[arrow["start_agent"]], ay=arrow["start_task"],
            xref="x", yref="y", axref="x", ayref="y",
            showarrow=True, arrowhead=3, arrowsize=1, arrowwidth=2, arrowcolor="black", opacity=0.9, standoff=15
        )
    
    fig.update_layout(
        title="Combined Workflow Graph - Performers Only (All Procedures)",
        xaxis=dict(tickvals=list(agent_pos.values()), ticktext=list(agent_pos.keys()), title="Agent", showgrid=True, gridcolor='lightgrey', range=[-0.5, 1.5]),
        yaxis=dict(tickvals=list(df["task_idx"]), ticktext=[wrap_text(f"{p} | {t}") for p, t in zip(df["Procedure"], df["Task Object"])], title="Procedure | Task", autorange="reversed", showgrid=True, gridcolor='lightgrey'),
        margin=dict(l=250, r=50, t=50, b=50), height=height, plot_bgcolor='white', paper_bgcolor='white'
    )
    
    arrow_info = {"dashed_arrows": dashed_arrow_info, "black_to_black_arrows": black_to_black_arrows}
    return fig, arrow_info


def build_interdependence_figure_base(df, procedure):
    """Build base figure for full interdependence view (single procedure) without highlighting."""
    agents = ["HUMAN*", "TARS", "TARS*", "HUMAN"]
    VALID_COLORS = {"red", "yellow", "green", "orange", "black", "grey"}
    
    proc_df = df[df["Procedure"] == procedure].reset_index(drop=True)
    proc_df["task_idx"] = proc_df.index
    tasks = proc_df["Task Object"].tolist()
    height = 400 + len(tasks) * 60
    
    dots = []
    dashed_arrows = []
    black_to_black_arrows = []
    
    for idx, row in proc_df.iterrows():
        task_idx = row["task_idx"]
        black_points = []
        
        for col in ["Human*", "TARS*"]:
            val = row[col].strip().lower() if isinstance(row[col], str) else ""
            if val in VALID_COLORS:
                dots.append({"task": task_idx, "agent": col.upper(), "color": val})
                if val != "red":
                    black_points.append(col.upper())
        
        for col in ["Human", "TARS"]:
            val = row[col].strip().lower() if isinstance(row[col], str) else ""
            if val in VALID_COLORS:
                agent = col.upper()
                dots.append({"task": task_idx, "agent": agent, "color": val})
                
                if agent == "TARS" and "HUMAN*" in black_points and row["Human*"].strip().lower() != "red" and row["TARS"].strip().lower() != "red":
                    dashed_arrows.append({"start_agent": "TARS", "end_agent": "HUMAN*", "task": task_idx})
                elif agent == "HUMAN" and "TARS*" in black_points and row["TARS*"].strip().lower() != "red" and row["Human"].strip().lower() != "red":
                    dashed_arrows.append({"start_agent": "HUMAN", "end_agent": "TARS*", "task": task_idx})
    
    for i in range(len(proc_df) - 1):
        current_blacks = [d for d in dots if d["task"] == i and d["agent"] in ["HUMAN*", "TARS*"] and d["color"] != "red"]
        next_blacks = [d for d in dots if d["task"] == i + 1 and d["agent"] in ["HUMAN*", "TARS*"] and d["color"] != "red"]
        for curr in current_blacks:
            for nxt in next_blacks:
                black_to_black_arrows.append({"start_task": i, "start_agent": curr["agent"], "end_task": i + 1, "end_agent": nxt["agent"]})
    
    agent_pos = {agent: i for i, agent in enumerate(agents)}
    fig = go.Figure()
    
    for dot in dots:
        row = proc_df.iloc[dot["task"]]
        hover_text = f"<b>Task:</b> {wrap_text(row['Task Object'])}<br><b>Agent:</b> {dot['agent']}"
        fig.add_trace(go.Scatter(
            x=[agent_pos[dot["agent"]]], y=[dot["task"]], mode="markers",
            marker=dict(size=20, color=dot["color"]), showlegend=False,
            hoverinfo="text", hovertext=hover_text
        ))
    
    dashed_arrow_info = []
    for arrow in dashed_arrows:
        fig.add_shape(
            type="line",
            x0=agent_pos[arrow["start_agent"]], y0=arrow["task"],
            x1=agent_pos[arrow["end_agent"]], y1=arrow["task"],
            line=dict(color="black", width=2, dash="dot")
        )
        dashed_arrow_info.append({"task": arrow["task"], "end_agent": arrow["end_agent"]})
    
    for arrow in black_to_black_arrows:
        fig.add_annotation(
            x=agent_pos[arrow["end_agent"]], y=arrow["end_task"],
            ax=agent_pos[arrow["start_agent"]], ay=arrow["start_task"],
            xref="x", yref="y", axref="x", ayref="y",
            showarrow=True, arrowhead=3, arrowsize=1, arrowwidth=2, arrowcolor="black", opacity=0.9
        )
    
    fig.update_layout(
        title=f"Workflow for {procedure}",
        xaxis=dict(tickvals=list(agent_pos.values()), ticktext=list(agent_pos.keys()), title="Agent", showgrid=True, gridcolor='lightgrey'),
        yaxis=dict(tickvals=list(range(len(tasks))), ticktext=[wrap_text(t) for t in tasks], title="Task", autorange="reversed", showgrid=True, gridcolor='lightgrey'),
        margin=dict(l=250, r=50, t=50, b=50), height=height, plot_bgcolor='white', paper_bgcolor='white'
    )
    
    arrow_info = {"dashed_arrows": dashed_arrow_info, "black_to_black_arrows": black_to_black_arrows}
    return fig, arrow_info


def build_combined_interdependence_figure_base(df):
    """Build base figure for full interdependence combined view without highlighting."""
    agents = ["HUMAN*", "TARS", "TARS*", "HUMAN"]
    VALID_COLORS = {"red", "yellow", "green", "orange", "black", "grey"}
    
    df = df.reset_index(drop=True)
    df["task_idx"] = df.index
    tasks = df["Task Object"].tolist()
    height = len(tasks) * 90
    
    dots = []
    dashed_arrows = []
    black_to_black_arrows = []
    
    for idx, row in df.iterrows():
        task_idx = row["task_idx"]
        black_points = []
        
        for col in ["Human*", "TARS*"]:
            val = row[col].strip().lower() if isinstance(row[col], str) else ""
            if val in VALID_COLORS:
                dots.append({"task": task_idx, "agent": col.upper(), "color": val})
                if val != "red":
                    black_points.append(col.upper())
        
        for col in ["Human", "TARS"]:
            val = row[col].strip().lower() if isinstance(row[col], str) else ""
            if val in VALID_COLORS:
                agent = col.upper()
                dots.append({"task": task_idx, "agent": agent, "color": val})
                
                if agent == "TARS" and "HUMAN*" in black_points and row["Human*"].strip().lower() != "red" and row["TARS"].strip().lower() != "red":
                    dashed_arrows.append({"start_agent": "TARS", "end_agent": "HUMAN*", "task": task_idx})
                elif agent == "HUMAN" and "TARS*" in black_points and row["TARS*"].strip().lower() != "red" and row["Human"].strip().lower() != "red":
                    dashed_arrows.append({"start_agent": "HUMAN", "end_agent": "TARS*", "task": task_idx})
    
    for i in range(len(df) - 1):
        current_blacks = [d for d in dots if d["task"] == i and d["agent"] in ["HUMAN*", "TARS*"] and d["color"] != "red"]
        next_blacks = [d for d in dots if d["task"] == i + 1 and d["agent"] in ["HUMAN*", "TARS*"] and d["color"] != "red"]
        for curr in current_blacks:
            for nxt in next_blacks:
                black_to_black_arrows.append({"start_task": i, "start_agent": curr["agent"], "end_task": i + 1, "end_agent": nxt["agent"]})
    
    agent_pos = {agent: i for i, agent in enumerate(agents)}
    fig = go.Figure()
    
    for dot in dots:
        row = df.iloc[dot["task"]]
        hover_text = f"<b>Task:</b> {wrap_text(row['Task Object'])}<br><b>Agent:</b> {dot['agent']}"
        fig.add_trace(go.Scatter(
            x=[agent_pos[dot["agent"]]], y=[dot["task"]], mode="markers",
            marker=dict(size=20, color=dot["color"]), showlegend=False,
            hoverinfo="text", hovertext=hover_text
        ))
    
    dashed_arrow_info = []
    for arrow in dashed_arrows:
        fig.add_shape(
            type="line",
            x0=agent_pos[arrow["start_agent"]], y0=arrow["task"],
            x1=agent_pos[arrow["end_agent"]], y1=arrow["task"],
            line=dict(color="black", width=2, dash="dot")
        )
        dashed_arrow_info.append({"task": arrow["task"], "end_agent": arrow["end_agent"]})
    
    for arrow in black_to_black_arrows:
        fig.add_annotation(
            x=agent_pos[arrow["end_agent"]], y=arrow["end_task"],
            ax=agent_pos[arrow["start_agent"]], ay=arrow["start_task"],
            xref="x", yref="y", axref="x", ayref="y",
            showarrow=True, arrowhead=3, arrowsize=1, arrowwidth=2, arrowcolor="black", opacity=0.9
        )
    
    fig.update_layout(
        title="Combined Workflow Graph (All Procedures)",
        xaxis=dict(tickvals=list(agent_pos.values()), ticktext=list(agent_pos.keys()), title="Agent", showgrid=True, gridcolor='lightgrey'),
        yaxis=dict(tickvals=list(df["task_idx"]), ticktext=[wrap_text(f"{p} | {t}") for p, t in zip(df["Procedure"], df["Task Object"])], title="Procedure | Task", autorange="reversed", showgrid=True, gridcolor='lightgrey'),
        margin=dict(l=250, r=50, t=50, b=50), height=height, plot_bgcolor='white', paper_bgcolor='white'
    )
    
    arrow_info = {"dashed_arrows": dashed_arrow_info, "black_to_black_arrows": black_to_black_arrows}
    return fig, arrow_info


def build_performers_only_figures(df, highlight_track=None, category_overrides=None):
    """Build workflow graphs showing only HUMAN* and TARS* performers (no supporters)"""
    if category_overrides is None:
        category_overrides = {}
    
    agents = ["HUMAN*", "TARS*"]
    VALID_COLORS = {"red", "yellow", "green", "orange", "black", "grey"}

    figures = {}

    for procedure, proc_df in df.groupby("Procedure"):
        tasks = proc_df["Task Object"].tolist()
        height = 300 + len(tasks) * 100
        dots = []
        grey_to_black_arrows = []
        black_to_black_arrows = []
        highlight_track_list = []  # Unified track considering overrides
        dashed_arrows_to_highlight = []

        proc_df = proc_df.reset_index(drop=True)
        proc_df["task_idx"] = proc_df.index

        # Step 1: Add points for performers only
        for idx, row in proc_df.iterrows():
            task_idx = row["task_idx"]
            black_points = []

            agent_colors = {
                "HUMAN*": row["Human*"].strip().lower() if isinstance(row["Human*"], str) else "",
                "TARS*": row["TARS*"].strip().lower() if isinstance(row["TARS*"], str) else "",
            }

            # Determine chosen agent using the unified helper function
            chosen_agent = get_chosen_agent_with_override(row, highlight_track, category_overrides, agent_colors)
            if chosen_agent:
                highlight_track_list.append((task_idx, chosen_agent))

            # Add dots for each agent column showing their own capabilities
            # HUMAN* column: HUMAN* performer (circle)
            # TARS* column: TARS* performer (circle) + TARS supporter (square)
            
            # HUMAN* performer in HUMAN* column
            human_performer_val = row["Human*"].strip().lower() if isinstance(row["Human*"], str) else ""
            if human_performer_val in VALID_COLORS and human_performer_val != "red":
                dots.append({
                    "task": task_idx,
                    "agent": "HUMAN*",
                    "shape": "circle",
                    "color": human_performer_val,
                    "role": "performer"
                })
                black_points.append("HUMAN*")
            
            # TARS* performer in TARS* column
            tars_performer_val = row["TARS*"].strip().lower() if isinstance(row["TARS*"], str) else ""
            if tars_performer_val in VALID_COLORS and tars_performer_val != "red":
                dots.append({
                    "task": task_idx,
                    "agent": "TARS*",
                    "shape": "circle",
                    "color": tars_performer_val,
                    "role": "performer"
                })
                black_points.append("TARS*")
            
            # TARS supporter in TARS* column (as square)
            tars_supporter_val = row["TARS"].strip().lower() if isinstance(row["TARS"], str) and row["TARS"] else ""
            if tars_supporter_val in VALID_COLORS and tars_supporter_val != "red":
                dots.append({
                    "task": task_idx,
                    "agent": "TARS*",
                    "shape": "square",
                    "color": tars_supporter_val,
                    "role": "supporter"
                })
            
            # HUMAN supporter in HUMAN* column (as square)
            human_supporter_val = row["Human"].strip().lower() if isinstance(row["Human"], str) and row["Human"] else ""
            if human_supporter_val in VALID_COLORS and human_supporter_val != "red":
                dots.append({
                    "task": task_idx,
                    "agent": "HUMAN*",
                    "shape": "square",
                    "color": human_supporter_val,
                    "role": "supporter"
                })

            # Add dashed arrows from supporters to performers (even though supporters aren't shown)
            # TARS -> HUMAN*
            tars_val = row["TARS"].strip().lower() if isinstance(row["TARS"], str) else ""
            human_val = row["Human"].strip().lower() if isinstance(row["Human"], str) else ""
            
            if ("HUMAN*" in black_points and 
                row["Human*"].strip().lower() != "red" and 
                tars_val != "" and tars_val != "red"):
                dashed_arrow = {
                    "start_agent": "TARS*",  # TARS supporter helps HUMAN* performer
                    "end_agent": "HUMAN*",
                    "task": task_idx,
                    "is_support": True  # Flag to indicate this is a support relationship
                }
                grey_to_black_arrows.append(dashed_arrow)
                # Highlight support arrow if this task's performer is HUMAN* in the highlight track
                # and we're using a "full support" mode
                if highlight_track in ["human_full_support", "agent_whenever_possible_full_support", "most_reliable"]:
                    if (task_idx, "HUMAN*") in highlight_track_list:
                        dashed_arrows_to_highlight.append(dashed_arrow)

            # HUMAN -> TARS*
            if ("TARS*" in black_points and 
                row["TARS*"].strip().lower() != "red" and 
                human_val != "" and human_val != "red"):
                dashed_arrow = {
                    "start_agent": "HUMAN*",  # HUMAN supporter helps TARS* performer
                    "end_agent": "TARS*",
                    "task": task_idx,
                    "is_support": True
                }
                grey_to_black_arrows.append(dashed_arrow)
                # Highlight support arrow if this task's performer is TARS* in the highlight track
                # and we're using a "full support" mode
                if highlight_track in ["human_full_support", "agent_whenever_possible_full_support", "most_reliable"]:
                    if (task_idx, "TARS*") in highlight_track_list:
                        dashed_arrows_to_highlight.append(dashed_arrow)

        # Step 2: Black-to-black transitions between performers (circles only)
        for i in range(len(tasks) - 1):
            current_blacks = [d for d in dots if d["task"] == i and d["agent"] in ["HUMAN*", "TARS*"] and d["shape"] == "circle"]
            next_blacks = [d for d in dots if d["task"] == i + 1 and d["agent"] in ["HUMAN*", "TARS*"] and d["shape"] == "circle"]
            for curr in current_blacks:
                for nxt in next_blacks:
                    black_to_black_arrows.append({
                        "start_task": i,
                        "start_agent": curr["agent"],
                        "end_task": i + 1,
                        "end_agent": nxt["agent"]
                    })

        # Step 3: Create figure
        agent_pos = {agent: i for i, agent in enumerate(agents)}
        fig = go.Figure()

        for dot in dots:
            row = proc_df.iloc[dot["task"]]
            hover_text = (
                f"<b>Task:</b> {wrap_text(row['Task Object'])}<br>"
                f"<b>Agent:</b> {dot['agent']}<br>"
                f"<b>Role:</b> {dot['role'].capitalize()}<br><br>"
                f"<b>Observability:</b><br>{wrap_text(row.get('Observability', ''))}<br><br>"
                f"<b>Predictability:</b><br>{wrap_text(row.get('Predictability', ''))}<br><br>"
                f"<b>Directability:</b><br>{wrap_text(row.get('Directability', ''))}"
            )

            if dot["shape"] == "circle":
                # Draw performer circle
                fig.add_trace(go.Scatter(
                    x=[agent_pos[dot["agent"]]],
                    y=[dot["task"]],
                    mode="markers",
                    marker=dict(size=30, color=dot["color"]),
                    showlegend=False,
                    hoverinfo="text",
                    hovertext=hover_text
                ))
            else:  # square
                # Draw black border square
                fig.add_trace(go.Scatter(
                    x=[agent_pos[dot["agent"]]],
                    y=[dot["task"]],
                    mode="markers",
                    marker=dict(
                        size=20,
                        color="black",
                        symbol="square"
                    ),
                    showlegend=False,
                    hoverinfo="skip"
                ))
                # Draw colored square on top
                fig.add_trace(go.Scatter(
                    x=[agent_pos[dot["agent"]]],
                    y=[dot["task"]],
                    mode="markers",
                    marker=dict(
                        size=17,
                        color=dot["color"],
                        symbol="square"
                    ),
                    showlegend=False,
                    hoverinfo="text",
                    hovertext=hover_text
                ))

        # Add dashed horizontal lines for support relationships
        # Group arrows by task to detect overlaps
        arrows_by_task = {}
        for arrow in grey_to_black_arrows:
            if arrow.get("is_support"):
                task = arrow["task"]
                if task not in arrows_by_task:
                    arrows_by_task[task] = []
                arrows_by_task[task].append(arrow)
        
        for task, task_arrows in arrows_by_task.items():
            # If there are 2 arrows at the same task, offset them vertically
            if len(task_arrows) == 2:
                offsets = [-0.08, 0.08]  # Small vertical offset
            else:
                offsets = [0] * len(task_arrows)
            
            for arrow, offset in zip(task_arrows, offsets):
                is_highlighted = len(highlight_track_list) > 0 and arrow in dashed_arrows_to_highlight
                # Draw dashed line from start_agent to end_agent with offset
                fig.add_shape(
                    type="line",
                    x0=agent_pos[arrow["start_agent"]],
                    y0=arrow["task"] + offset,
                    x1=agent_pos[arrow["end_agent"]],
                    y1=arrow["task"] + offset,
                    line=dict(
                        color="crimson" if is_highlighted else "black",
                        width=4 if is_highlighted else 2,
                        dash="dot"
                    )
                )

        for arrow in black_to_black_arrows:
            # Use the unified highlight_track_list for all strategies including category overrides
            is_highlighted = (arrow["start_task"], arrow["start_agent"]) in highlight_track_list and \
                             (arrow["end_task"], arrow["end_agent"]) in highlight_track_list

            fig.add_annotation(
                x=agent_pos[arrow["end_agent"]],
                y=arrow["end_task"],
                ax=agent_pos[arrow["start_agent"]],
                ay=arrow["start_task"],
                xref="x", yref="y", axref="x", ayref="y",
                showarrow=True,
                arrowhead=3,
                arrowsize=1,
                arrowwidth=4 if is_highlighted else 2,
                arrowcolor="crimson" if is_highlighted else "black",
                opacity=0.9,
                standoff=18,  # Stop arrow at end circle edge (circle radius=15, add margin)
                startstandoff=18  # Start arrow from start circle edge
            )

        fig.update_layout(
            title=f"Workflow for {procedure} (Performers Only)",
            xaxis=dict(
                tickvals=list(agent_pos.values()),
                ticktext=list(agent_pos.keys()),
                title="Agent",
                showgrid=True,
                gridcolor='lightgrey',
                range=[-0.5, 1.5]  # Constrain x-axis to keep agents closer
            ),
            yaxis=dict(
                tickvals=list(range(len(tasks))),
                ticktext=[wrap_text(task) for task in tasks],
                title="Task",
                autorange="reversed",
                showgrid=True,
                gridcolor='lightgrey'
            ),
            height=height,
            margin=dict(l=200, r=50, t=50, b=50),
            plot_bgcolor='white',
            paper_bgcolor='white',
        )
        figures[procedure] = fig

    return figures


def build_performers_only_combined_figure(df, highlight_track=None, category_overrides=None):
    """Build combined workflow graph showing only HUMAN* and TARS* performers (no supporters)"""
    if category_overrides is None:
        category_overrides = {}
    
    agents = ["HUMAN*", "TARS*"]
    VALID_COLORS = {"red", "yellow", "green", "orange", "black", "grey"}
    tasks = df["Task Object"].tolist()
    height = 600 + len(tasks) * 100
    dots = []
    grey_to_black_arrows = []
    black_to_black_arrows = []

    df = df.reset_index(drop=True)
    df["task_idx"] = df.index

    highlight_track_list = []  # Unified track considering overrides
    dashed_arrows_to_highlight = []

    for idx, row in df.iterrows():
        task_idx = row["task_idx"]
        black_points = []

        agent_colors = {
            "HUMAN*": row["Human*"].strip().lower() if isinstance(row["Human*"], str) else "",
            "TARS*": row["TARS*"].strip().lower() if isinstance(row["TARS*"], str) else "",
        }

        # Determine chosen agent using the unified helper function
        chosen_agent = get_chosen_agent_with_override(row, highlight_track, category_overrides, agent_colors)
        if chosen_agent:
            highlight_track_list.append((task_idx, chosen_agent))

        # Add dots for each agent column showing their own capabilities
        # HUMAN* column: HUMAN* performer (circle)
        # TARS* column: TARS* performer (circle) + TARS supporter (square)
        
        # HUMAN* performer in HUMAN* column
        human_performer_val = row["Human*"].strip().lower() if isinstance(row["Human*"], str) else ""
        if human_performer_val in VALID_COLORS and human_performer_val != "red":
            dots.append({
                "task": task_idx,
                "agent": "HUMAN*",
                "shape": "circle",
                "color": human_performer_val,
                "role": "performer"
            })
            black_points.append("HUMAN*")
        
        # TARS* performer in TARS* column
        tars_performer_val = row["TARS*"].strip().lower() if isinstance(row["TARS*"], str) else ""
        if tars_performer_val in VALID_COLORS and tars_performer_val != "red":
            dots.append({
                "task": task_idx,
                "agent": "TARS*",
                "shape": "circle",
                "color": tars_performer_val,
                "role": "performer"
            })
            black_points.append("TARS*")
        
        # TARS supporter in TARS* column (as square)
        tars_supporter_val = row["TARS"].strip().lower() if isinstance(row["TARS"], str) and row["TARS"] else ""
        if tars_supporter_val in VALID_COLORS and tars_supporter_val != "red":
            dots.append({
                "task": task_idx,
                "agent": "TARS*",
                "shape": "square",
                "color": tars_supporter_val,
                "role": "supporter"
            })
        
        # HUMAN supporter in HUMAN* column (as square)
        human_supporter_val = row["Human"].strip().lower() if isinstance(row["Human"], str) and row["Human"] else ""
        if human_supporter_val in VALID_COLORS and human_supporter_val != "red":
            dots.append({
                "task": task_idx,
                "agent": "HUMAN*",
                "shape": "square",
                "color": human_supporter_val,
                "role": "supporter"
            })

        # Add dashed arrows from supporters to performers (even though supporters aren't shown)
        # TARS -> HUMAN*
        tars_val = row["TARS"].strip().lower() if isinstance(row["TARS"], str) else ""
        human_val = row["Human"].strip().lower() if isinstance(row["Human"], str) else ""
        
        if ("HUMAN*" in black_points and 
            row["Human*"].strip().lower() != "red" and 
            tars_val != "" and tars_val != "red"):
            dashed_arrow = {
                "start_agent": "TARS*",  # TARS supporter helps HUMAN* performer
                "end_agent": "HUMAN*",
                "task": task_idx,
                "is_support": True
            }
            grey_to_black_arrows.append(dashed_arrow)
            # Highlight support arrow if this task's performer is HUMAN* in the highlight track
            if highlight_track in ["human_full_support", "agent_whenever_possible_full_support", "most_reliable"]:
                if (task_idx, "HUMAN*") in highlight_track_list:
                    dashed_arrows_to_highlight.append(dashed_arrow)

        # HUMAN -> TARS*
        if ("TARS*" in black_points and 
            row["TARS*"].strip().lower() != "red" and 
            human_val != "" and human_val != "red"):
            dashed_arrow = {
                "start_agent": "HUMAN*",  # HUMAN supporter helps TARS* performer
                "end_agent": "TARS*",
                "task": task_idx,
                "is_support": True
            }
            grey_to_black_arrows.append(dashed_arrow)
            # Highlight support arrow if this task's performer is TARS* in the highlight track
            if highlight_track in ["human_full_support", "agent_whenever_possible_full_support", "most_reliable"]:
                if (task_idx, "TARS*") in highlight_track_list:
                    dashed_arrows_to_highlight.append(dashed_arrow)

    # Black-to-black transitions between performers (circles only)
    for i in range(len(df) - 1):
        current_blacks = [d for d in dots if d["task"] == i and d["agent"] in ["HUMAN*", "TARS*"] and d["shape"] == "circle"]
        next_blacks = [d for d in dots if d["task"] == i + 1 and d["agent"] in ["HUMAN*", "TARS*"] and d["shape"] == "circle"]
        for curr in current_blacks:
            for nxt in next_blacks:
                black_to_black_arrows.append({
                    "start_task": i,
                    "start_agent": curr["agent"],
                    "end_task": i + 1,
                    "end_agent": nxt["agent"]
                })

    agent_pos = {agent: i for i, agent in enumerate(agents)}
    fig = go.Figure()

    for dot in dots:
        row = df.iloc[dot["task"]]
        hover_text = (
            f"<b>Task:</b> {wrap_text(row['Task Object'])}<br>"
            f"<b>Agent:</b> {dot['agent']}<br>"
            f"<b>Role:</b> {dot['role'].capitalize()}<br><br>"
            f"<b>Observability:</b><br>{wrap_text(row.get('Observability', ''))}<br><br>"
            f"<b>Predictability:</b><br>{wrap_text(row.get('Predictability', ''))}<br><br>"
            f"<b>Directability:</b><br>{wrap_text(row.get('Directability', ''))}"
        )

        if dot["shape"] == "circle":
            # Draw performer circle
            fig.add_trace(go.Scatter(
                x=[agent_pos[dot["agent"]]],
                y=[dot["task"]],
                mode="markers",
                marker=dict(size=40, color=dot["color"]),
                showlegend=False,
                hoverinfo="text",
                hovertext=hover_text
            ))
        else:  # square
            # Draw black border square
            fig.add_trace(go.Scatter(
                x=[agent_pos[dot["agent"]]],
                y=[dot["task"]],
                mode="markers",
                marker=dict(
                    size=20,
                    color="black",
                    symbol="square"
                ),
                showlegend=False,
                hoverinfo="skip"
            ))
            # Draw colored square on top
            fig.add_trace(go.Scatter(
                x=[agent_pos[dot["agent"]]],
                y=[dot["task"]],
                mode="markers",
                marker=dict(
                    size=17,
                    color=dot["color"],
                    symbol="square"
                ),
                showlegend=False,
                hoverinfo="text",
                hovertext=hover_text
            ))

    # Add dashed horizontal lines for support relationships
    # Group arrows by task to detect overlaps
    arrows_by_task = {}
    for arrow in grey_to_black_arrows:
        if arrow.get("is_support"):
            task = arrow["task"]
            if task not in arrows_by_task:
                arrows_by_task[task] = []
            arrows_by_task[task].append(arrow)
    
    for task, task_arrows in arrows_by_task.items():
        # If there are 2 arrows at the same task, offset them vertically
        if len(task_arrows) == 2:
            offsets = [-0.08, 0.08]  # Small vertical offset
        else:
            offsets = [0] * len(task_arrows)
        
        for arrow, offset in zip(task_arrows, offsets):
            is_highlighted = len(highlight_track_list) > 0 and arrow in dashed_arrows_to_highlight
            # Draw dashed line from start_agent to end_agent with offset
            fig.add_shape(
                type="line",
                x0=agent_pos[arrow["start_agent"]],
                y0=arrow["task"] + offset,
                x1=agent_pos[arrow["end_agent"]],
                y1=arrow["task"] + offset,
                line=dict(
                    color="crimson" if is_highlighted else "black",
                    width=4 if is_highlighted else 2,
                    dash="dot"
                )
            )

    for arrow in black_to_black_arrows:
        # Use the unified highlight_track_list for all strategies including category overrides
        is_highlighted = (arrow["start_task"], arrow["start_agent"]) in highlight_track_list and \
                         (arrow["end_task"], arrow["end_agent"]) in highlight_track_list

        fig.add_annotation(
            x=agent_pos[arrow["end_agent"]],
            y=arrow["end_task"],
            ax=agent_pos[arrow["start_agent"]],
            ay=arrow["start_task"],
            xref="x", yref="y", axref="x", ayref="y",
            showarrow=True,
            arrowhead=3,
            arrowsize=1,
            arrowwidth=4 if is_highlighted else 2,
            arrowcolor="crimson" if is_highlighted else "black",
            opacity=0.9,
            standoff=15  # Stop arrow at circle edge
        )

    fig.update_layout(
        title="Combined Workflow Graph - Performers Only (All Procedures)",
        xaxis=dict(
            tickvals=list(agent_pos.values()),
            ticktext=list(agent_pos.keys()),
            title="Agent",
            showgrid=True,
            gridcolor='lightgrey',
            range=[-0.5, 1.5]  # Constrain x-axis to keep agents closer
        ),
        yaxis=dict(
            tickvals=list(df["task_idx"]),
            ticktext=[wrap_text(f"{p} | {t}") for p, t in zip(df["Procedure"], df["Task Object"])],
            title="Procedure | Task",
            autorange="reversed",
            showgrid=True,
            gridcolor='lightgrey'
        ),
        margin=dict(l=250, r=50, t=50, b=50),
        height=height,
        plot_bgcolor='white',
        paper_bgcolor='white',
    )
    return fig


def build_interdependence_figures(df, highlight_track=None, category_overrides=None):
    if category_overrides is None:
        category_overrides = {}
    
    agents = ["HUMAN*", "TARS", "TARS*", "HUMAN"]
    VALID_COLORS = {"red", "yellow", "green", "orange", "black", "grey"}

    figures = {}

    for procedure, proc_df in df.groupby("Procedure"):
        tasks = proc_df["Task Object"].tolist()
        height = 300 + len(tasks) * 100
        dots = []
        grey_to_black_arrows = []
        black_to_black_arrows = []
        highlight_track_list = []  # Unified track considering overrides
        dashed_arrows_to_highlight = []

        proc_df = proc_df.reset_index(drop=True)
        proc_df["task_idx"] = proc_df.index

        # Step 1: Add points
        for idx, row in proc_df.iterrows():
            task_idx = row["task_idx"]
            black_points = []

            agent_colors = {
                "HUMAN*": row["Human*"].strip().lower() if isinstance(row["Human*"], str) else "",
                "TARS*": row["TARS*"].strip().lower() if isinstance(row["TARS*"], str) else "",
            }

            # Determine chosen agent using the unified helper function
            chosen_agent = get_chosen_agent_with_override(row, highlight_track, category_overrides, agent_colors)
            if chosen_agent:
                highlight_track_list.append((task_idx, chosen_agent))

            for col in ["Human*", "TARS*"]:
                val = row[col].strip().lower() if isinstance(row[col], str) else ""
                if val in VALID_COLORS:
                    dots.append({"task": task_idx, "agent": col.upper(), "color": val})
                    if val != "red":
                        black_points.append(col.upper())

            for col in ["Human", "TARS"]:
                val = row[col].strip().lower() if isinstance(row[col], str) else ""
                if val in VALID_COLORS:
                    agent = col.upper()
                    dots.append({"task": task_idx, "agent": agent, "color": val})

                    if (agent == "TARS"
                        and "HUMAN*" in black_points
                        and row["Human*"].strip().lower() != "red"
                        and row["TARS"].strip().lower() != "red"):
                        dashed_arrow = {
                            "start_agent": "TARS",
                            "end_agent": "HUMAN*",
                            "task": task_idx
                        }
                        grey_to_black_arrows.append(dashed_arrow)
                        # Highlight support arrow if this task's performer is HUMAN* in the highlight track
                        if highlight_track in ["human_full_support", "agent_whenever_possible_full_support", "most_reliable"]:
                            if (task_idx, "HUMAN*") in highlight_track_list:
                                dashed_arrows_to_highlight.append(dashed_arrow)

                    elif (agent == "HUMAN"
                          and "TARS*" in black_points
                          and row["TARS*"].strip().lower() != "red"
                          and row["Human"].strip().lower() != "red"):
                        dashed_arrow = {
                            "start_agent": "HUMAN",
                            "end_agent": "TARS*",
                            "task": task_idx
                        }
                        grey_to_black_arrows.append(dashed_arrow)
                        # Highlight support arrow if this task's performer is TARS* in the highlight track
                        if highlight_track in ["human_full_support", "agent_whenever_possible_full_support", "most_reliable"]:
                            if (task_idx, "TARS*") in highlight_track_list:
                                dashed_arrows_to_highlight.append(dashed_arrow)

        # Step 2: Black-to-black transitions
        for i in range(len(tasks) - 1):
            current_blacks = [d for d in dots if d["task"] == i and d["agent"] in ["HUMAN*", "TARS*"] and d["color"] != "red"]
            next_blacks = [d for d in dots if d["task"] == i + 1 and d["agent"] in ["HUMAN*", "TARS*"] and d["color"] != "red"]
            for curr in current_blacks:
                for nxt in next_blacks:
                    black_to_black_arrows.append({
                        "start_task": i,
                        "start_agent": curr["agent"],
                        "end_task": i + 1,
                        "end_agent": nxt["agent"]
                    })

        # Step 3: Create figure
        agent_pos = {agent: i for i, agent in enumerate(agents)}
        fig = go.Figure()

        for dot in dots:
            row = proc_df.iloc[dot["task"]]  # get the row by task index
            hover_text = (
                f"<b>Task:</b> {wrap_text(row['Task Object'])}<br>"
                f"<b>Agent:</b> {dot['agent']}<br><br>"
                f"<b>Observability:</b><br>{wrap_text(row.get('Observability', ''))}<br><br>"
                f"<b>Predictability:</b><br>{wrap_text(row.get('Predictability', ''))}<br><br>"
                f"<b>Directability:</b><br>{wrap_text(row.get('Directability', ''))}"
            )


            fig.add_trace(go.Scatter(
                x=[agent_pos[dot["agent"]]],
                y=[dot["task"]],
                mode="markers",
                marker=dict(size=20, color=dot["color"]),
                showlegend=False,
                hoverinfo="text",
                hovertext=hover_text
            ))


        for arrow in grey_to_black_arrows:
            is_highlighted = len(highlight_track_list) > 0 and arrow in dashed_arrows_to_highlight
            fig.add_shape(
                type="line",
                x0=agent_pos[arrow["start_agent"]],
                y0=arrow["task"],
                x1=agent_pos[arrow["end_agent"]],
                y1=arrow["task"],
                line=dict(
                    color="crimson" if is_highlighted else "black",
                    width=4 if is_highlighted else 2,
                    dash="dot"
                )
            )

        for arrow in black_to_black_arrows:
            # Use the unified highlight_track_list for all strategies including category overrides
            is_highlighted = (arrow["start_task"], arrow["start_agent"]) in highlight_track_list and \
                             (arrow["end_task"], arrow["end_agent"]) in highlight_track_list

            fig.add_annotation(
                x=agent_pos[arrow["end_agent"]],
                y=arrow["end_task"],
                ax=agent_pos[arrow["start_agent"]],
                ay=arrow["start_task"],
                xref="x", yref="y", axref="x", ayref="y",
                showarrow=True,
                arrowhead=3,
                arrowsize=1,
                arrowwidth=4 if is_highlighted else 2,
                arrowcolor="crimson" if is_highlighted else "black",
                opacity=0.9
            )

        fig.update_layout(
            title=f"Workflow for {procedure}",
            xaxis=dict(
                tickvals=list(agent_pos.values()),
                ticktext=list(agent_pos.keys()),
                title="Agent",
                showgrid=True,
                gridcolor='lightgrey'
            ),
            yaxis=dict(
                tickvals=list(range(len(tasks))),
                ticktext=[wrap_text(task) for task in tasks],
                title="Task",
                autorange="reversed",
                showgrid=True,
                gridcolor='lightgrey'
            ),
            height=height,
            margin=dict(l=200, r=50, t=50, b=50),
            plot_bgcolor='white',
            paper_bgcolor='white',
        )
        figures[procedure] = fig
    return figures

def build_combined_interdependence_figure(df, highlight_track=None, category_overrides=None):
    if category_overrides is None:
        category_overrides = {}
    
    agents = ["HUMAN*", "TARS", "TARS*", "HUMAN"]
    VALID_COLORS = {"red", "yellow", "green", "orange", "black", "grey"}
    tasks = df["Task Object"].tolist()
    height = 600 + len(tasks) * 100
    dots = []
    grey_to_black_arrows = []
    black_to_black_arrows = []

    df = df.reset_index(drop=True)
    df["task_idx"] = df.index  # Unique index across all tasks

    # Unified track considering category overrides
    highlight_track_list = []
    dashed_arrows_to_highlight = []

    for idx, row in df.iterrows():
        task_idx = row["task_idx"]
        black_points = []

        agent_colors = {
            "HUMAN*": row["Human*"].strip().lower() if isinstance(row["Human*"], str) else "",
            "TARS*": row["TARS*"].strip().lower() if isinstance(row["TARS*"], str) else "",
        }

        # Determine chosen agent using the unified helper function
        chosen_agent = get_chosen_agent_with_override(row, highlight_track, category_overrides, agent_colors)
        if chosen_agent:
            highlight_track_list.append((task_idx, chosen_agent))

        for col in ["Human*", "TARS*"]:
            val = row[col].strip().lower() if isinstance(row[col], str) else ""
            if val in VALID_COLORS:
                dots.append({"task": task_idx, "agent": col.upper(), "color": val})
                if val != "red":
                    black_points.append(col.upper())

        for col in ["Human", "TARS"]:
            val = row[col].strip().lower() if isinstance(row[col], str) else ""
            if val in VALID_COLORS:
                agent = col.upper()
                dots.append({"task": task_idx, "agent": agent, "color": val})

                if (
                    agent == "TARS"
                    and "HUMAN*" in black_points
                    and row["Human*"].strip().lower() != "red"
                    and row["TARS"].strip().lower() != "red"
                ):
                    dashed_arrow = {
                        "start_agent": "TARS",
                        "end_agent": "HUMAN*",
                        "task": task_idx
                    }
                    grey_to_black_arrows.append(dashed_arrow)
                    # Highlight if this task's chosen agent is HUMAN*
                    if (task_idx, "HUMAN*") in highlight_track_list:
                        dashed_arrows_to_highlight.append(dashed_arrow)

                elif (
                    agent == "HUMAN"
                    and "TARS*" in black_points
                    and row["TARS*"].strip().lower() != "red"
                    and row["Human"].strip().lower() != "red"
                ):
                    dashed_arrow = {
                        "start_agent": "HUMAN",
                        "end_agent": "TARS*",
                        "task": task_idx
                    }
                    grey_to_black_arrows.append(dashed_arrow)
                    # Highlight if this task's chosen agent is TARS*
                    if (task_idx, "TARS*") in highlight_track_list:
                        dashed_arrows_to_highlight.append(dashed_arrow)

    for i in range(len(df) - 1):
        current_blacks = [d for d in dots if d["task"] == i and d["agent"] in ["HUMAN*", "TARS*"] and d["color"] != "red"]
        next_blacks = [d for d in dots if d["task"] == i + 1 and d["agent"] in ["HUMAN*", "TARS*"] and d["color"] != "red"]
        for curr in current_blacks:
            for nxt in next_blacks:
                black_to_black_arrows.append({
                    "start_task": i,
                    "start_agent": curr["agent"],
                    "end_task": i + 1,
                    "end_agent": nxt["agent"]
                })

    agent_pos = {agent: i for i, agent in enumerate(agents)}
    fig = go.Figure()

    for dot in dots:
        row = df.iloc[dot["task"]]
        hover_text = (
            f"<b>Task:</b> {wrap_text(row['Task Object'])}<br>"
            f"<b>Agent:</b> {dot['agent']}<br><br>"
            f"<b>Observability:</b><br>{wrap_text(row.get('Observability', ''))}<br><br>"
            f"<b>Predictability:</b><br>{wrap_text(row.get('Predictability', ''))}<br><br>"
            f"<b>Directability:</b><br>{wrap_text(row.get('Directability', ''))}"
        )


        fig.add_trace(go.Scatter(
            x=[agent_pos[dot["agent"]]],
            y=[dot["task"]],
            mode="markers",
            marker=dict(size=20, color=dot["color"]),
            showlegend=False,
            hoverinfo="text",
            hovertext=hover_text
        ))


    for arrow in grey_to_black_arrows:
        is_highlighted = len(highlight_track_list) > 0 and arrow in dashed_arrows_to_highlight
        fig.add_shape(
            type="line",
            x0=agent_pos[arrow["start_agent"]],
            y0=arrow["task"],
            x1=agent_pos[arrow["end_agent"]],
            y1=arrow["task"],
            line=dict(
                color="crimson" if is_highlighted else "black",
                width=4 if is_highlighted else 2,
                dash="dot"
            )
        )

    for arrow in black_to_black_arrows:
        # Use the unified highlight_track_list for all strategies including category overrides
        is_highlighted = (arrow["start_task"], arrow["start_agent"]) in highlight_track_list and \
                         (arrow["end_task"], arrow["end_agent"]) in highlight_track_list

        fig.add_annotation(
            x=agent_pos[arrow["end_agent"]],
            y=arrow["end_task"],
            ax=agent_pos[arrow["start_agent"]],
            ay=arrow["start_task"],
            xref="x", yref="y", axref="x", ayref="y",
            showarrow=True,
            arrowhead=3,
            arrowsize=1,
            arrowwidth=4 if is_highlighted else 2,
            arrowcolor="crimson" if is_highlighted else "black",
            opacity=0.9
        )

    fig.update_layout(
        title="Combined Workflow Graph (All Procedures)",
        xaxis=dict(
            tickvals=list(agent_pos.values()),
            ticktext=list(agent_pos.keys()),
            title="Agent",
            showgrid=True,
            gridcolor='lightgrey'
        ),
        yaxis=dict(
            tickvals=list(df["task_idx"]),
            ticktext=[wrap_text(f"{p} | {t}") for p, t in zip(df["Procedure"], df["Task Object"])],
            title="Procedure | Task",
            autorange="reversed",
            showgrid=True,
            gridcolor='lightgrey'
        ),
        margin=dict(l=250, r=50, t=50, b=50),
        height=height,
        plot_bgcolor='white',
        paper_bgcolor='white',
    )
    return fig


# Page layouts
def interdependence_analysis_page():
    return html.Div([
        # The table goes here
        html.H2("Interdependence Table - Engine Failure at Take Off", style={"textAlign": "center"}),
        html.Div(id="table-wrapper", children=[table]),
        html.Div([
            html.Div([
                # Left-aligned buttons
                html.Div([
                    dcc.Upload(
                        id='upload-data',
                        children=html.Button('Load Table', id='load-button', n_clicks=0),
                        multiple=False,
                        style={
                            'display': 'inline-block',
                            'marginRight': '10px'
                        }
                    ),
                    html.Button("Add Row", id="add-row-button", n_clicks=0),
                    html.Button("Copy Cell Down", id="copy-down-button", n_clicks=0),
                ], style={"display": "flex", "gap": "10px"}),

                # Right-aligned Save button
                html.Div([
                    html.Button("Save Table", disabled=False, id="save-button", n_clicks=0)
                ], style={"marginLeft": "auto"})  # pushes this div to the right
            ], style={"display": "flex", "width": "100%"}),
            html.Div(id="save-confirmation", style={"marginTop": "10px", "fontStyle": "italic"})
        ]),

        html.H2("Workflow graph", style={"textAlign": "center"}),

        # Dropdown menu to select procedure
        html.Div([
            dcc.Dropdown(
                id="procedure-dropdown",
                options=[{"label": proc, "value": proc} for proc in df["Procedure"].unique()],
                value=df["Procedure"].unique()[0],
                clearable=True,
                style={"width": "50%", "margin": "0 auto"}
            )
        ]),

        html.Div([
            dcc.RadioItems(
                id="view-selector",
                options=[
                    {"label": "Full View (All Agents)", "value": "full"},
                    {"label": "Performers Only (HUMAN* & TARS*)", "value": "performers"}
                ],
                value="full",
                labelStyle={'display': 'inline-block', 'margin-right': '20px'},
                style={"textAlign": "center", "marginTop": "20px"}
            )
        ]),

        dcc.RadioItems(
        id="highlight-selector",
        options=[
            {"label": "No highlight", "value": "none"},
            {"label": "Human-performer-only no support", "value": "human_baseline"},
            {"label": "Human-performer-only full support", "value": "human_full_support"},
            {"label": "Agent-performer-whenever-possible no support", "value": "agent_whenever_possible"},
            {"label": "Agent-performer-whenever-possible full support", "value": "agent_whenever_possible_full_support"},
            {"label": "Most reliable path", "value": "most_reliable"}
        ],
        value="none",
        labelStyle={'display': 'inline-block', 'margin-right': '20px'},
        style={"textAlign": "center", "marginTop": "20px"}
        ),

        # Category overrides section
        html.Div([
            html.H4("Category Overrides", style={"textAlign": "center", "marginTop": "30px", "marginBottom": "10px"}),
            html.P("Override performer assignment for specific task categories:", 
                   style={"textAlign": "center", "color": "#666", "fontSize": "14px"}),
            html.Div(id="category-overrides-container", style={
                "display": "flex",
                "flexWrap": "wrap",
                "justifyContent": "center",
                "gap": "20px",
                "marginTop": "15px",
                "marginBottom": "20px",
                "padding": "15px",
                "backgroundColor": "#f9f9f9",
                "borderRadius": "8px"
            })
        ]),

        # Automation Proportion Summary Box
        html.Div([
            html.Div([
                html.Span("Automation Proportion: ", style={"fontSize": "16px", "fontWeight": "bold"}),
                html.Span(id="ap-summary-value", children="--", style={"fontSize": "20px", "fontWeight": "bold", "color": "#2196F3"}),
            ], style={"display": "inline-block", "marginRight": "20px"}),
            html.A("See methodology details below ↓", href="#automation-proportion-section", 
                   style={"fontSize": "12px", "color": "#666", "textDecoration": "underline"})
        ], style={
            "textAlign": "center",
            "padding": "15px",
            "backgroundColor": "#e3f2fd",
            "borderRadius": "8px",
            "marginBottom": "20px",
            "border": "1px solid #90caf9"
        }),

        # Hidden store for category override values
        dcc.Store(id="category-overrides-store", data={}),
        
        # Cache for base figure (without highlighting) to avoid rebuilding
        dcc.Store(id="base-figure-store", data=None),
        dcc.Store(id="arrow-indices-store", data=None),

        # Graph
        dcc.Graph(id="interdependence-graph", config={
            "displayModeBar": False
        }),

        # Bar chart for most reliable path color counts
        html.Div([
            dcc.Graph(id="bar-chart-whole-scenario"),
            dcc.Graph(id="most-reliable-bar-chart"),
            dcc.Graph(id="spo_baseline-bar-chart"),
            dcc.Graph(id="allocation-type-bar-chart"),
            dcc.Graph(id="agent-autonomy-bar-chart")
        ]),

        # Automation Proportion Section
        html.Div(id="automation-proportion-section", children=[
            html.H2("Automation Proportion", style={"textAlign": "center", "marginTop": "40px"}),
            
            # Citation
            html.Div([
                html.P([
                    html.B("Reference: "),
                    "Liu, Y., & Kaber, D. B. (2025). Models of automation proportion in human-in-the-loop systems and operator situation awareness responses. ",
                    html.I("Ergonomics"),
                    ", 1–14. ",
                    html.A("https://doi.org/10.1080/00140139.2025.2608273", 
                           href="https://doi.org/10.1080/00140139.2025.2608273", 
                           target="_blank")
                ], style={"fontSize": "14px", "color": "#555", "marginBottom": "20px"})
            ], style={"backgroundColor": "#f5f5f5", "padding": "15px", "borderRadius": "8px", "marginBottom": "25px"}),
            
            # Explanation
            html.Div([
                html.H4("Methodology", style={"marginBottom": "15px"}),
                dcc.Markdown('''
Let our categories be indexed as $k \\in \\{1, \\ldots, K\\}$ where $K$ is the number of categories.

Let $C_k$ be the set of basic tasks in category $k$, and $T_k = |C_k|$ be the number of tasks in that category.

**Task Weight Definition:**

For each task $t$, we define an "automation-from-human-perspective" weight $w(t)$:

| Task Allocation | Weight |
|-----------------|--------|
| **Manual** (HUMAN* performs alone) | $w(t) = 0$ |
| **Supported** (HUMAN* performs with autonomy support) | $w(t) = 0.5$ |
| **Delegated** (TARS* performs w/ or w/o human support) | $w(t) = 1$ |

**Category Score (equal weight across tasks within category):**

For each category $k$, compute the mean task weight:

$$p_k = \\frac{1}{T_k} \\sum_{t \\in C_k} w(t)$$

This produces $p_k \\in [0, 1]$ for each category.

**Overall Automation Proportion (equal-weight categories):**

$$P = \\frac{1}{K} \\sum_{k=1}^{K} p_k$$

The resulting $P \\in [0, 1]$ represents the overall automation proportion, where:
- $P = 0$ means fully manual operation
- $P = 1$ means fully delegated to automation
                ''', mathjax=True, style={"fontSize": "15px", "lineHeight": "1.8"})
            ], style={"backgroundColor": "white", "padding": "20px", "borderRadius": "8px", "border": "1px solid #ddd", "marginBottom": "25px"}),
            
            # Computation results (dynamic)
            html.Div([
                html.H4("Computation for Current Selection", style={"marginBottom": "15px"}),
                html.Div(id="automation-proportion-results")
            ], style={"backgroundColor": "white", "padding": "20px", "borderRadius": "8px", "border": "1px solid #ddd"})
            
        ], style={"maxWidth": "900px", "margin": "0 auto", "padding": "20px"}),

        # Footer with copyright
        html.Footer(
            "© Benjamin R. Berton 2025 Polytechnique Montreal",
            style={
                "textAlign": "center",
                "marginTop": "40px",
                "padding": "10px 0",
                "color": "#888",
                "fontSize": "14px"
            }
        ),

    ], style={"fontFamily": "'Roboto', 'Helvetica', 'Arial', sans-serif"})


def assumptions_page():
    return html.Div([
        html.H2("Assumptions", style={"textAlign": "center"}),
        html.Div([
            html.H3("Operational Scenario", style={"marginBottom": "20px"}),
            html.Div([
                html.H4("Takeoff with an engine failure caused by a bird strike", style={"marginTop": "30px", "marginBottom": "15px"}),
                html.Ul([
                    html.Li([html.B("Departure Airport"), ": CYUL"]),
                    html.Li([html.B("Arrival Airport"), ": CYOW"]),
                    html.Li([html.B("Time of day"), ": Day (14:00 local time)"]),
                    html.Li([html.B("Weather conditions"), ": CAVOK"]),
                    html.Li([html.B("Wind"), ": Calm, 5 knots, 160°"]),
                    html.Li([html.B("Temperature"), ": 22°C"]),
                    html.Li([html.B("Atmospheric Pressure"), ": 29.92 inHg"]),
                    html.Li([html.B("Departure runway"), ": 24R"]),
                    html.Li([html.B("Arrival Runway"), ": 14"]),
                    html.Li([html.B("Aircraft"), ": Cessna Citation Mustang Very Light Jet"]),
                    html.Li([html.B("Crew"), ": 1 pilot, 2 pax"]),
                    html.Li([html.B("Flight phase"), ": Takeoff"]),
                    html.Li([html.B("Emergency"), ": Engine failure caused by a bird strike"]),
                ], style={"fontSize": "16px", "lineHeight": "1.6"}),
            ], style={"maxWidth": "800px", "margin": "0 auto", "padding": "20px"})
        ]),
        html.Div([
            html.H3("Single Pilot", style={"marginBottom": "20px"}),
            html.Div([
                html.H4("Key Assumptions:", style={"marginTop": "30px", "marginBottom": "15px"}),
                html.Ul([
                    html.Li([html.B("Role"), ": The pilot is the captain and retains final authority and responsibility for decision making throughout the flight."]),
                    html.Li([html.B("Experience"), ": >5,000 flight hours, including >1,000 hours in type."]),
                    html.Li([html.B("Training"), ": Recent training on emergency procedures, including engine failure at takeoff scenarios."]),
                    html.Li([html.B("Health"), ": Good health, well-rested, and fit to fly."]),
                    html.Li([html.B("Support"), ": The pilot has access to TARS for assistance with monitoring and procedural guidance."]),
                ], style={"fontSize": "16px", "lineHeight": "1.6"}),
            ], style={"maxWidth": "800px", "margin": "0 auto", "padding": "20px"})
        ]),
        html.Div([
            html.H3("Autonomous System : TARS", style={"marginBottom": "20px"}),
            html.Div([
                html.H4("Key Assumptions:", style={"marginTop": "30px", "marginBottom": "15px"}),
                html.Ul([
                    html.Li([html.B("Role"), ": TARS assists the pilot on multiple tasks during normal, abnormal and emergency situation according to the briefing. But the pilot retains final authority. TARS is designed to complement human decision-making, not replace it. The pilot must remain engaged and aware of the flight situation at all times."]),
                    html.Li([html.B("Description: "), "TARS is implemented as a reactive software designed to assist the pilot in managing the flight scenario. TARS organizes its behavior into a structured hierarchy of states and substates. Each state encapsulates a specific procedural phase of the scenario, with transitions triggered by defined environmental conditions."]),
                    html.Li([html.B("Sensors"), ": TARS has access to all flight parameters available in the avionics system, including aircraft state and system status. TARS does not have access to environmental parameters outside of the avionics system."]),
                    html.Li([html.B("Actuators"), ": TARS can interact with the pilot through the interface with the pilot, for checklist support. TARS can interact with the airplane through a predefined set of commands, including the FMS and autopilot, but TARS cannot perform physical actions in the cockpit (e.g., moving flight controls, switching levers, pushing buttons)."]),
                    html.Li([html.B("Reliability"), ": TARS is assumed to operate without technical failures during the scenario. TARS is assumed to correctly interpret sensor data and execute commands without errors and under 1ms latency."]),
                    html.Li([html.B("Graphical Interface"), ": The Human-TARS interface is an EFB-like tactile application run on a tablet placed on the left-side of the single pilot in the cockpit that provides access to TARS current state and behavioral intent. The interface allows the pilot to monitor TARS actions, receive alerts, and input commands."]),
                    html.Li([html.B("Voice Interface"), ": TARS can communicate with the pilot using auditory alerts and speech synthesis. The pilot can issue commands to TARS vocally (e.g. cancel action)."]),
                ], style={"fontSize": "16px", "lineHeight": "1.6"}),
            ], style={"maxWidth": "800px", "margin": "0 auto", "padding": "20px"})
        ]),
        
        # Footer with copyright
        html.Footer(
            "© Benjamin R. Berton 2025 Polytechnique Montreal",
            style={
                "textAlign": "center",
                "marginTop": "40px",
                "padding": "10px 0",
                "color": "#888",
                "fontSize": "14px"
            }
        ),
        
    ], style={"fontFamily": "'Roboto', 'Helvetica', 'Arial', sans-serif"})


app.layout = html.Div([
    dcc.Location(id='url', refresh=False),
    html.Div(
        "© Benjamin R. Berton 2025 Polytechnique Montreal",
        style={
            "textAlign": "right",
            "color": "#888",
            "fontSize": "14px",
            "marginBottom": "10px"
        }
    ),
    html.Div(id='navbar'),
    html.Div(id='page-content')
])

# Callback to update navbar based on current pathname
@app.callback(Output('navbar', 'children'), Input('url', 'pathname'))
def update_navbar(pathname):
    return create_navbar(current_pathname=pathname)

# Callback for page routing
@app.callback(Output('page-content', 'children'), Input('url', 'pathname'))
def display_page(pathname):
    if pathname == '/assumptions':
        return assumptions_page()
    else:
        return interdependence_analysis_page()

# Callback to generate category override radio buttons dynamically
@app.callback(
    Output("category-overrides-container", "children"),
    Input("responsibility-table", "data")
)
def generate_category_overrides(data):
    if not data:
        return []
    
    df = pd.DataFrame(data)
    if "Category" not in df.columns:
        return []
    
    # Get unique categories
    categories = sorted(df["Category"].dropna().unique())
    
    # Create radio buttons for each category with mini bar charts
    category_controls = []
    for category in categories:
        # Count assignable tasks per agent for this category
        cat_df = df[df["Category"] == category]
        human_assignable = sum(
            1 for _, row in cat_df.iterrows()
            if isinstance(row.get("Human*"), str) and row["Human*"].strip().lower() in ["green", "yellow", "orange"]
        )
        tars_assignable = sum(
            1 for _, row in cat_df.iterrows()
            if isinstance(row.get("TARS*"), str) and row["TARS*"].strip().lower() in ["green", "yellow", "orange"]
        )
        
        category_controls.append(
            html.Div([
                html.Label(category, style={
                    "fontWeight": "bold",
                    "marginBottom": "5px",
                    "display": "block",
                    "fontSize": "13px"
                }),
                # Clickable bar chart for assignable tasks
                dcc.Graph(
                    id={"type": "category-bar-chart", "category": category},
                    figure=go.Figure(
                        data=[
                            go.Bar(
                                x=[tars_assignable],
                                y=["TARS"],
                                orientation='h',
                                marker_color='grey',
                                text=[tars_assignable],
                                textposition='inside',
                                textfont=dict(color='white', size=10),
                                name="TARS",
                                customdata=["TARS"]
                            ),
                            go.Bar(
                                x=[human_assignable],
                                y=["HUMAN"],
                                orientation='h',
                                marker_color='grey',
                                text=[human_assignable],
                                textposition='inside',
                                textfont=dict(color='white', size=10),
                                name="HUMAN",
                                customdata=["HUMAN"]
                            )
                        ],
                        layout=go.Layout(
                            height=60,
                            width=120,
                            margin=dict(l=45, r=5, t=5, b=5),
                            xaxis=dict(visible=False, range=[0, max(human_assignable, tars_assignable, 1) * 1.1]),
                            yaxis=dict(tickfont=dict(size=9)),
                            paper_bgcolor='rgba(0,0,0,0)',
                            plot_bgcolor='rgba(0,0,0,0)',
                            showlegend=False,
                            barmode='group',
                            autosize=False
                        )
                    ),
                    config={'displayModeBar': False},
                    style={"marginTop": "5px", "height": "60px", "width": "120px", "cursor": "pointer"}
                ),
                # Store the counts for the callback
                dcc.Store(
                    id={"type": "category-counts", "category": category},
                    data={"human": human_assignable, "tars": tars_assignable}
                ),
                # Store the current selection state (hidden radio replacement)
                dcc.Store(
                    id={"type": "category-override", "category": category},
                    data="default"
                )
            ], style={
                "backgroundColor": "white",
                "padding": "10px 15px",
                "borderRadius": "5px",
                "border": "1px solid #ddd",
                "minWidth": "140px"
            })
        )
    
    return category_controls

# Callback to handle bar click and toggle selection
@app.callback(
    Output({"type": "category-override", "category": dash.MATCH}, "data"),
    Output({"type": "category-bar-chart", "category": dash.MATCH}, "figure"),
    Input({"type": "category-bar-chart", "category": dash.MATCH}, "clickData"),
    State({"type": "category-override", "category": dash.MATCH}, "data"),
    State({"type": "category-counts", "category": dash.MATCH}, "data"),
    prevent_initial_call=True
)
def toggle_category_selection(clickData, current_selection, counts):
    if clickData is None or counts is None:
        raise dash.exceptions.PreventUpdate
    
    # Get the clicked bar's label
    clicked_label = clickData["points"][0].get("y", None)
    if clicked_label is None:
        raise dash.exceptions.PreventUpdate
    
    # Toggle logic: if clicking the same selection, deselect (back to default)
    if current_selection == clicked_label:
        new_selection = "default"
    else:
        new_selection = clicked_label
    
    human_count = counts.get("human", 0)
    tars_count = counts.get("tars", 0)
    
    # Determine bar colors based on new selection
    human_color = "dodgerblue" if new_selection == "HUMAN" else "grey"
    tars_color = "dodgerblue" if new_selection == "TARS" else "grey"
    
    fig = go.Figure(
        data=[
            go.Bar(
                x=[tars_count],
                y=["TARS"],
                orientation='h',
                marker_color=tars_color,
                text=[tars_count],
                textposition='inside',
                textfont=dict(color='white', size=10),
                name="TARS",
                customdata=["TARS"]
            ),
            go.Bar(
                x=[human_count],
                y=["HUMAN"],
                orientation='h',
                marker_color=human_color,
                text=[human_count],
                textposition='inside',
                textfont=dict(color='white', size=10),
                name="HUMAN",
                customdata=["HUMAN"]
            )
        ],
        layout=go.Layout(
            height=60,
            width=120,
            margin=dict(l=45, r=5, t=5, b=5),
            xaxis=dict(visible=False, range=[0, max(human_count, tars_count, 1) * 1.1]),
            yaxis=dict(tickfont=dict(size=9)),
            paper_bgcolor='rgba(0,0,0,0)',
            plot_bgcolor='rgba(0,0,0,0)',
            showlegend=False,
            barmode='group',
            autosize=False
        )
    )
    
    return new_selection, fig

# Callback to collect all category overrides into a single store
@app.callback(
    Output("category-overrides-store", "data"),
    Input({"type": "category-override", "category": dash.ALL}, "data"),
    State({"type": "category-override", "category": dash.ALL}, "id"),
    prevent_initial_call=True
)
def collect_category_overrides(values, ids):
    if not values or not ids:
        return {}
    
    overrides = {}
    for id_dict, value in zip(ids, values):
        if value != "default":
            overrides[id_dict["category"]] = value
    
    return overrides

@app.callback(
    Output("base-figure-store", "data"),
    Output("arrow-indices-store", "data"),
    Input("procedure-dropdown", "value"),
    Input("view-selector", "value"),
    State("responsibility-table", "data")
)
def build_base_figure(procedure, view_mode, data):
    """Build the base figure structure (without highlighting). Only runs when procedure/view changes."""
    df = pd.DataFrame(data)
    
    if procedure is None:
        # Combined view
        if view_mode == "performers":
            fig, arrow_info = build_performers_only_combined_figure_base(df)
        else:
            fig, arrow_info = build_combined_interdependence_figure_base(df)
    else:
        # Single procedure view
        if view_mode == "performers":
            fig, arrow_info = build_performers_only_figure_base(df, procedure)
        else:
            fig, arrow_info = build_interdependence_figure_base(df, procedure)
    
    return fig.to_dict(), arrow_info


@app.callback(
    Output("interdependence-graph", "figure"),
    Input("base-figure-store", "data"),
    Input("arrow-indices-store", "data"),
    Input("highlight-selector", "value"),
    Input("category-overrides-store", "data"),
    Input("procedure-dropdown", "value"),
    State("responsibility-table", "data")
)
def apply_highlighting(base_fig_dict, arrow_info, highlight_track, category_overrides, procedure, data):
    """Apply highlighting to the cached base figure. Fast operation - just updates colors/widths."""
    if base_fig_dict is None:
        return go.Figure()
    
    fig = go.Figure(base_fig_dict)
    
    if arrow_info is None or highlight_track == "none":
        # Still need to check for category overrides even when highlight_track is "none"
        if not category_overrides:
            return fig
    
    df = pd.DataFrame(data)
    if category_overrides is None:
        category_overrides = {}
    
    # Build the highlight track list based on current settings
    if procedure is not None:
        df_filtered = df[df["Procedure"] == procedure].reset_index(drop=True)
    else:
        df_filtered = df.reset_index(drop=True)
    
    df_filtered["task_idx"] = df_filtered.index
    
    highlight_track_list = []
    for idx, row in df_filtered.iterrows():
        task_idx = row["task_idx"]
        agent_colors = {
            "HUMAN*": row["Human*"].strip().lower() if isinstance(row["Human*"], str) else "",
            "TARS*": row["TARS*"].strip().lower() if isinstance(row["TARS*"], str) else "",
        }
        chosen_agent = get_chosen_agent_with_override(row, highlight_track, category_overrides, agent_colors)
        if chosen_agent:
            highlight_track_list.append((task_idx, chosen_agent))
    
    # Determine if support arrows should be highlighted based on highlight mode
    # "full_support" modes highlight support arrows, "no support" modes don't
    should_highlight_support = highlight_track in ["human_full_support", "agent_whenever_possible_full_support", "most_reliable"]
    
    # Apply highlighting to shapes (dashed arrows - support relationships)
    if "shapes" in fig.layout and fig.layout.shapes:
        shapes = list(fig.layout.shapes)
        for i, shape_info in enumerate(arrow_info.get("dashed_arrows", [])):
            if i < len(shapes):
                task_idx = shape_info["task"]
                end_agent = shape_info["end_agent"]
                
                # Dashed arrows should only be highlighted if:
                # 1. We're in a "full_support" mode AND
                # 2. The performer at this task matches the highlighted path
                is_highlighted = False
                if should_highlight_support and (task_idx, end_agent) in highlight_track_list:
                    is_highlighted = True
                
                shapes[i].line.color = "crimson" if is_highlighted else "black"
                shapes[i].line.width = 4 if is_highlighted else 2
        fig.layout.shapes = shapes
    
    # Apply highlighting to annotations (black-to-black arrows - performer transitions)
    if "annotations" in fig.layout and fig.layout.annotations:
        annotations = list(fig.layout.annotations)
        for i, arrow_data in enumerate(arrow_info.get("black_to_black_arrows", [])):
            if i < len(annotations):
                start_task = arrow_data["start_task"]
                start_agent = arrow_data["start_agent"]
                end_task = arrow_data["end_task"]
                end_agent = arrow_data["end_agent"]
                
                # Check if this arrow is part of the highlighted path
                is_highlighted = (start_task, start_agent) in highlight_track_list and \
                                 (end_task, end_agent) in highlight_track_list
                
                annotations[i].arrowcolor = "crimson" if is_highlighted else "black"
                annotations[i].arrowwidth = 4 if is_highlighted else 2
        fig.layout.annotations = annotations
    
    return fig


@app.callback(
    Output("bar-chart-whole-scenario", "figure"),
    Output("most-reliable-bar-chart", "figure"),
    Output("spo_baseline-bar-chart", "figure"),
    Output("allocation-type-bar-chart", "figure"),
    Output("agent-autonomy-bar-chart", "figure"),
    Input("procedure-dropdown", "value"),
    State("responsibility-table", "data")
)
def update_bar_charts(procedure, data):
    """Update bar charts - only when procedure changes, not on highlight changes."""
    df = pd.DataFrame(data)
    
    if procedure is None:
        df_bar = df
    else:
        df_bar = df[df["Procedure"] == procedure]

# --- Bar Chart for the whole scenario---
    performer_green = 0
    performer_yellow = 0
    performer_orange = 0
    supporter_green = 0
    supporter_yellow = 0
    supporter_orange = 0
    human_performer_green = 0
    human_performer_yellow = 0
    human_performer_orange = 0
    human_supporter_green = 0
    human_supporter_yellow = 0
    human_supporter_orange = 0
    tars_performer_green = 0
    tars_performer_yellow = 0
    tars_performer_orange = 0
    tars_supporter_green = 0
    tars_supporter_yellow = 0
    tars_supporter_orange = 0
    for idx, row in df_bar.iterrows():
        agent_colors = {
            "HUMAN*": str(row.get("Human*", "") or "").strip().lower(),
            "TARS*": str(row.get("TARS*", "") or "").strip().lower(),
        }
        supporter_colors = {
            "HUMAN": str(row.get("Human", "") or "").strip().lower(),
            "TARS": str(row.get("TARS", "") or "").strip().lower(),
        }
        # Find performer (most reliable)
        for agent in ["HUMAN*", "TARS*"]:
            if agent_colors[agent] == "green":
                if agent ==  "HUMAN*":
                    human_performer_green += 1
                if agent == "TARS*":
                    tars_performer_green += 1
                performer_green += 1
            if agent_colors[agent] == "yellow":
                if agent ==  "HUMAN*":
                    human_performer_yellow += 1
                if agent == "TARS*":
                    tars_performer_yellow += 1
                performer_yellow += 1
            if agent_colors[agent] == "orange":
                if agent ==  "HUMAN*":
                    human_performer_orange += 1
                if agent == "TARS*":
                    tars_performer_orange += 1
                performer_orange += 1
        for agent in ["HUMAN", "TARS"]:
            if supporter_colors[agent] == "green":
                if agent ==  "HUMAN":
                    human_supporter_green += 1
                if agent == "TARS":
                    tars_supporter_green += 1
                supporter_green += 1
            if supporter_colors[agent] == "yellow":
                if agent ==  "HUMAN":
                    human_supporter_yellow += 1
                if agent == "TARS":
                    tars_supporter_yellow += 1
                supporter_yellow += 1
            if supporter_colors[agent] == "orange":
                if agent ==  "HUMAN":
                    human_supporter_orange += 1
                if agent == "TARS":
                    tars_supporter_orange += 1
                supporter_orange += 1

    bar_fig_whole_scenario = go.Figure()
    # Stacked bars for performer colors
    bar_fig_whole_scenario.add_trace(go.Bar(
        name="Human Performer",
        x=["Performer Green", "Performer Yellow", "Performer Orange"],
        y=[human_performer_green, human_performer_yellow, human_performer_orange],
        marker_color=["seagreen", "gold", "darkorange"]
    ))
    bar_fig_whole_scenario.add_trace(go.Bar(
        name="TARS Performer",
        x=["Performer Green", "Performer Yellow", "Performer Orange"],
        y=[tars_performer_green, tars_performer_yellow, tars_performer_orange],
        marker_color=["limegreen", "khaki", "orange"]
    ))
    # Stacked bars for supporter colors
    bar_fig_whole_scenario.add_trace(go.Bar(
        name="Human Supporter",
        x=["Supporter Green", "Supporter Yellow", "Supporter Orange"],
        y=[human_supporter_green, human_supporter_yellow, human_supporter_orange],
        marker_color=["seagreen", "gold", "darkorange"]
    ))
    bar_fig_whole_scenario.add_trace(go.Bar(
        name="TARS Supporter",
        x=["Supporter Green", "Supporter Yellow", "Supporter Orange"],
        y=[tars_supporter_green, tars_supporter_yellow, tars_supporter_orange],
        marker_color=["limegreen", "khaki", "orange"]
    ))
    bar_fig_whole_scenario.update_layout(
        title="Performer and Supporter Capacities in Mixed Initiative",
        xaxis_title="Role and Capacity",
        yaxis_title="Number of Tasks",
        barmode='stack',
        bargap=0.3,
        plot_bgcolor='white',
        paper_bgcolor='white',
        showlegend=False
    )

    # --- Bar Chart for Most Reliable Path ---
    performer_green = 0
    performer_yellow = 0
    performer_orange = 0
    supporter_green = 0
    supporter_yellow = 0
    supporter_orange = 0
    human_performer_green = 0
    human_performer_yellow = 0
    human_performer_orange = 0
    human_supporter_green = 0
    human_supporter_yellow = 0
    human_supporter_orange = 0
    tars_performer_green = 0
    tars_performer_yellow = 0
    tars_performer_orange = 0
    tars_supporter_green = 0
    tars_supporter_yellow = 0
    tars_supporter_orange = 0
    for idx, row in df_bar.iterrows():
        agent_colors = {
            "HUMAN*": str(row.get("Human*", "") or "").strip().lower(),
            "TARS*": str(row.get("TARS*", "") or "").strip().lower(),
        }
        supporter_colors = {
            "HUMAN": str(row.get("Human", "") or "").strip().lower(),
            "TARS": str(row.get("TARS", "") or "").strip().lower(),
        }
        # Find performer (most reliable)
        performer = None
        for agent in ["HUMAN*", "TARS*"]:
            if agent_colors[agent] == "green":
                performer = agent
                performer_color = "green"
                break
        if not performer:
            for agent in ["HUMAN*", "TARS*"]:
                if agent_colors[agent] == "yellow":
                    performer = agent
                    performer_color = "yellow"
                    break
        if not performer:
            for agent in ["HUMAN*", "TARS*"]:
                if agent_colors[agent] == "orange":
                    performer = agent
                    performer_color = "orange"
                    break
        # Count performer color
        if performer:
            if performer_color == "green":
                performer_green += 1
                if performer == "HUMAN*":
                    human_performer_green += 1
                else:
                    tars_performer_green += 1
            elif performer_color == "yellow":
                performer_yellow += 1
                if performer == "HUMAN*":
                    human_performer_yellow += 1
                else:
                    tars_performer_yellow += 1
            elif performer_color == "orange":
                performer_orange += 1
                if performer == "HUMAN*":
                    human_performer_orange += 1
                else:
                    tars_performer_orange += 1
            # Now check for supporter (the other agent)
            supporter = "TARS" if performer == "HUMAN*" else "HUMAN"
            supporter_color = supporter_colors[supporter]
            if supporter_color == "green":
                supporter_green += 1
                if supporter == "HUMAN":
                    human_supporter_green += 1
                else:
                    tars_supporter_green += 1
            elif supporter_color == "yellow":
                supporter_yellow += 1
                if supporter == "HUMAN":
                    human_supporter_yellow += 1
                else:
                    tars_supporter_yellow += 1
            elif supporter_color == "orange":
                supporter_orange += 1
                if supporter == "HUMAN":
                    human_supporter_orange += 1
                else:
                    tars_supporter_orange += 1

    bar_fig = go.Figure()
    # Stacked bars for performer colors
    bar_fig.add_trace(go.Bar(
        name="Human Performer",
        x=["Performer Green", "Performer Yellow", "Performer Orange"],
        y=[human_performer_green, human_performer_yellow, human_performer_orange],
        marker_color=["seagreen", "gold", "darkorange"]
    ))
    bar_fig.add_trace(go.Bar(
        name="TARS Performer",
        x=["Performer Green", "Performer Yellow", "Performer Orange"],
        y=[tars_performer_green, tars_performer_yellow, tars_performer_orange],
        marker_color=["limegreen", "khaki", "orange"]
    ))
    # Stacked bars for supporter colors
    bar_fig.add_trace(go.Bar(
        name="Human Supporter",
        x=["Supporter Green", "Supporter Yellow", "Supporter Orange"],
        y=[human_supporter_green, human_supporter_yellow, human_supporter_orange],
        marker_color=["seagreen", "gold", "darkorange"]
    ))
    bar_fig.add_trace(go.Bar(
        name="TARS Supporter",
        x=["Supporter Green", "Supporter Yellow", "Supporter Orange"],
        y=[tars_supporter_green, tars_supporter_yellow, tars_supporter_orange],
        marker_color=["limegreen", "khaki", "orange"]
    ))
    bar_fig.update_layout(
        title="Most Reliable Path: Performer and Supporter Capacities",
        xaxis_title="Role and Capacity",
        yaxis_title="Number of Tasks",
        barmode='stack',
        bargap=0.3,
        plot_bgcolor='white',
        paper_bgcolor='white',
        showlegend=False
    )

    # --- Bar Chart for spo baseline Path ---
    performer_green = 0
    performer_yellow = 0
    performer_orange = 0
    for idx, row in df_bar.iterrows():
        agent_colors = {
            "HUMAN*": str(row.get("Human*", "") or "").strip().lower(),
        }
        performer = "HUMAN*"
        if performer:
            if agent_colors[performer] == "green":
                performer_green += 1
            elif agent_colors[performer] == "yellow":
                performer_yellow += 1
            elif agent_colors[performer] == "orange":
                performer_orange += 1
            elif agent_colors[performer] == "orange":
                performer_orange += 1

    bar_fig_spo = go.Figure()
    # Stacked bars for performer colors
    bar_fig_spo.add_trace(go.Bar(
        name="Performer Green",
        x=["Performer Green"],
        y=[performer_green],
        marker_color="seagreen"
    ))
    bar_fig_spo.add_trace(go.Bar(
        name="Performer Yellow",
        x=["Performer Yellow"],
        y=[performer_yellow],
        marker_color="gold"
    ))
    bar_fig_spo.add_trace(go.Bar(
        name="Performer Orange",
        x=["Performer Orange"],
        y=[performer_orange],
        marker_color="darkorange"
    ))
    bar_fig_spo.update_layout(
        title="Human-only no support Path: Human pilot capacities",
        xaxis_title="Role and Capacity",
        yaxis_title="Number of Tasks",
        bargap=0.3,
        plot_bgcolor='white',
        paper_bgcolor='white',
        showlegend=False
    )

    # --- Allocation Type Analysis ---
    single_allocation_independent = 0
    multiple_allocation_independent = 0
    interdependent = 0

    for idx, row in df_bar.iterrows():
        # Get all performer and supporter values
        human_star = str(row.get("Human*", "") or "").strip().lower()
        tars_star = str(row.get("TARS*", "") or "").strip().lower()
        human = str(row.get("Human", "") or "").strip().lower()
        tars = str(row.get("TARS", "") or "").strip().lower()

        VALID_COLORS = {"red", "yellow", "green", "orange"}
        
        # Count valid performers (not red)
        performers = []
        if human_star in VALID_COLORS and human_star != "red":
            performers.append("Human*")
        if tars_star in VALID_COLORS and tars_star != "red":
            performers.append("TARS*")
        
        # Count valid supporters (not red)
        supporters = []
        if human in VALID_COLORS and human != "red":
            supporters.append("Human")
        if tars in VALID_COLORS and tars != "red":
            supporters.append("TARS")
        
        # Determine task type
        if len(supporters) > 0:
            # Has support available = interdependent
            interdependent += 1
        elif len(performers) == 1:
            # Only one performer, no support = single allocation independent
            single_allocation_independent += 1
        elif len(performers) > 1:
            # Multiple performers, no support = multiple allocation independent
            multiple_allocation_independent += 1
    
    total_tasks = len(df_bar)
    
    # Create horizontal stacked bar chart
    allocation_fig = go.Figure()
    
    allocation_fig.add_trace(go.Bar(
        name='Single Allocation Independent',
        y=['Task Allocation Types'],
        x=[single_allocation_independent],
        orientation='h',
        marker=dict(color='lightcoral'),
        text=[f'Single Allocation Independent: {single_allocation_independent} ({single_allocation_independent/total_tasks*100:.1f}%)' if total_tasks > 0 and single_allocation_independent > 0 else ''],
        textposition='inside',
        textfont=dict(color='black', size=12),
        hovertemplate='Single Allocation Independent<br>Count: %{x}<br>Percentage: ' + (f'{single_allocation_independent/total_tasks*100:.1f}%' if total_tasks > 0 else '0%') + '<extra></extra>'
    ))
    
    allocation_fig.add_trace(go.Bar(
        name='Multiple Allocation Independent',
        y=['Task Allocation Types'],
        x=[multiple_allocation_independent],
        orientation='h',
        marker=dict(color='lightskyblue'),
        text=[f'Multiple Allocation Independent: {multiple_allocation_independent} ({multiple_allocation_independent/total_tasks*100:.1f}%)' if total_tasks > 0 and multiple_allocation_independent > 0 else ''],
        textposition='inside',
        textfont=dict(color='black', size=12),
        hovertemplate='Multiple Allocation Independent<br>Count: %{x}<br>Percentage: ' + (f'{multiple_allocation_independent/total_tasks*100:.1f}%' if total_tasks > 0 else '0%') + '<extra></extra>'
    ))
    
    allocation_fig.add_trace(go.Bar(
        name='Interdependent (Support Available)',
        y=['Task Allocation Types'],
        x=[interdependent],
        orientation='h',
        marker=dict(color='lightgreen'),
        text=[f'Interdependent: {interdependent} ({interdependent/total_tasks*100:.1f}%)' if total_tasks > 0 and interdependent > 0 else ''],
        textposition='inside',
        textfont=dict(color='black', size=12),
        hovertemplate='Interdependent (Support Available)<br>Count: %{x}<br>Percentage: ' + (f'{interdependent/total_tasks*100:.1f}%' if total_tasks > 0 else '0%') + '<extra></extra>'
    ))
    
    allocation_fig.update_layout(
        title="Task Type Distribution",
        xaxis_title="Number of Tasks",
        barmode='stack',
        height=200,
        plot_bgcolor='white',
        paper_bgcolor='white',
        showlegend=False,
        margin=dict(l=50, r=50, t=50, b=50)
    )

    # --- Agent Autonomy Analysis ---
    # Track tasks by agent and autonomy across the entire scenario
    agent_autonomy = {
        "Human*": {"autonomous": 0, "non_autonomous": 0},
        "TARS*": {"autonomous": 0, "non_autonomous": 0}
    }
    
    # Process all tasks in sequence (not grouped by procedure)
    prev_performers = []
    
    for idx, row in df_bar.iterrows():
        # Skip the first task (task 0) as there's no previous task to compare
        if idx == 0:
            # Get current task performers for next iteration
            human_star = str(row.get("Human*", "") or "").strip().lower()
            tars_star = str(row.get("TARS*", "") or "").strip().lower()
            
            VALID_COLORS = {"red", "yellow", "green", "orange"}
            
            current_performers = []
            if human_star in VALID_COLORS and human_star != "red":
                current_performers.append("Human*")
            if tars_star in VALID_COLORS and tars_star != "red":
                current_performers.append("TARS*")
            
            prev_performers = current_performers
            continue
        
        # Get current task performers
        human_star = str(row.get("Human*", "") or "").strip().lower()
        tars_star = str(row.get("TARS*", "") or "").strip().lower()
        
        VALID_COLORS = {"red", "yellow", "green", "orange"}
        
        # Map agents to their colors for this task
        agent_colors = {
            "Human*": human_star,
            "TARS*": tars_star
        }
        
        current_performers = []
        if human_star in VALID_COLORS and human_star != "red":
            current_performers.append("Human*")
        if tars_star in VALID_COLORS and tars_star != "red":
            current_performers.append("TARS*")
        
        # For each agent that can perform this task
        for agent in current_performers:
            # Orange tasks are always non-autonomous
            if agent_colors[agent] == "orange":
                agent_autonomy[agent]["non_autonomous"] += 1
            elif agent not in prev_performers:
                # Agent couldn't perform previous task
                agent_autonomy[agent]["non_autonomous"] += 1
            else:
                # Agent could also perform previous task = autonomous (green or yellow only)
                agent_autonomy[agent]["autonomous"] += 1
        
        prev_performers = current_performers
    
    # Create horizontal stacked bar chart for agent autonomy
    autonomy_fig = go.Figure()
    
    agents = ["Human*", "TARS*"]
    colors_autonomous = {"Human*": "lightgreen", "TARS*": "lightgreen"}
    colors_non_autonomous = {"Human*": "lightcoral", "TARS*": "lightcoral"}
    
    for agent in agents:
        autonomous = agent_autonomy[agent]["autonomous"]
        non_autonomous = agent_autonomy[agent]["non_autonomous"]
        total = autonomous + non_autonomous
        
        # Non-autonomous tasks (first section)
        autonomy_fig.add_trace(go.Bar(
            name=f'{agent} Non-Autonomous',
            y=[agent],
            x=[non_autonomous],
            orientation='h',
            marker=dict(color=colors_non_autonomous[agent]),
            text=[f'Non-Autonomous: {non_autonomous} ({non_autonomous/total*100:.1f}%)' if total > 0 and non_autonomous > 0 else ''],
            textposition='inside',
            textfont=dict(color='black', size=11),
            hovertemplate=f'{agent}<br>Non-Autonomous Tasks: {non_autonomous}<br>Percentage: ' + (f'{non_autonomous/total*100:.1f}%' if total > 0 else '0%') + '<extra></extra>',
            showlegend=False
        ))
        
        # Autonomous tasks (second section)
        autonomy_fig.add_trace(go.Bar(
            name=f'{agent} Autonomous',
            y=[agent],
            x=[autonomous],
            orientation='h',
            marker=dict(color=colors_autonomous[agent]),
            text=[f'Autonomous: {autonomous} ({autonomous/total*100:.1f}%)' if total > 0 and autonomous > 0 else ''],
            textposition='inside',
            textfont=dict(color='black', size=11),
            hovertemplate=f'{agent}<br>Autonomous Tasks: {autonomous}<br>Percentage: ' + (f'{autonomous/total*100:.1f}%' if total > 0 else '0%') + '<extra></extra>',
            showlegend=False
        ))
    
    autonomy_fig.update_layout(
        title="Agent Autonomy: Task Continuity Across Entire Scenario",
        xaxis_title="Number of Tasks",
        yaxis_title="Agent",
        barmode='stack',
        height=300,
        plot_bgcolor='white',
        paper_bgcolor='white',
        showlegend=False,
        margin=dict(l=100, r=50, t=50, b=50)
    )

    return bar_fig_whole_scenario, bar_fig, bar_fig_spo, allocation_fig, autonomy_fig


@app.callback(
    Output("table-wrapper", "children"),
    Output("save-confirmation", "children"),
    Input("responsibility-table", "data"),
    Input("save-button", "n_clicks"),
    Input("upload-data", "contents"),
    Input("add-row-button", "n_clicks"),
    Input("copy-down-button", "n_clicks"),
    State("responsibility-table", "active_cell"),
    State("upload-data", "filename"),
    prevent_initial_call=True
)
def handle_table(data, save_clicks, upload_contents, add_row_clicks, copy_clicks, active_cell, upload_filename):
    ctx = callback_context
    triggered = ctx.triggered[0]["prop_id"].split(".")[0]

    save_message = ""

    # Case 1: Save button clicked
    if triggered == "save-button":
        df = pd.DataFrame(data)
        # Don't recreate Row column if it already exists
        df.to_csv(DATA_FILE, index=False)
        save_message = f"✅ Table saved to {DATA_FILE}"

    # Case 2: File uploaded
    elif triggered == "upload-data" and upload_contents is not None:
        content_type, content_string = upload_contents.split(',')
        decoded = base64.b64decode(content_string)
        try:
            df = pd.read_csv(io.StringIO(decoded.decode('utf-8')))
            save_message = f"✅ Loaded {upload_filename}"
        except Exception as e:
            return dash.no_update, f"⚠️ Error loading file: {str(e)}"

    # Case 3: Add Row button clicked
    elif triggered == "add-row-button":
        df = pd.DataFrame(data)
        new_row = {col: "" for col in df.columns}
        df = pd.concat([df, pd.DataFrame([new_row])], ignore_index=True)

    # Case 4: Copy Down clicked
    elif triggered == "copy-down-button":
        df = pd.DataFrame(data)
        if active_cell and "row" in active_cell and "column_id" in active_cell:
            row = active_cell["row"]
            col = active_cell["column_id"]
            if row is not None and col is not None and row + 1 < len(df):
                df.at[row + 1, col] = df.at[row, col]

    # Case 5: Table edited directly
    else:
        df = pd.DataFrame(data)

    # Ensure Row column is properly maintained (no need to reassign if it exists)
    if 'Row' not in df.columns:
        df = df.assign(Row=lambda x: x.index + 1)

    updated_table = dash_table.DataTable(
        id='responsibility-table',
        columns=columns,
        data=df.to_dict("records"),
        editable=True,
        dropdown=dropdowns,
        style_data_conditional=style_table(df),
        style_cell={'textAlign': 'left', 'padding': '5px', 'whiteSpace': 'normal'},
        style_cell_conditional=[
            {
                'if': {'column_id': 'Human*'},
                'borderLeft': '3px solid black'
            },
            {
                'if': {'column_id': 'Human'},
                'borderRight': '3px solid black'
            },
            {
                'if': {'column_id': 'TARS'},
                'borderRight': '2px solid black'
            }
        ],
        style_header={'fontWeight': 'bold', 'backgroundColor': '#f0f0f0'},
        style_table={'overflowX': 'auto', 'border': '1px solid lightgrey'},
        row_deletable=True,
    )
    return updated_table, save_message


# Callback to compute Automation Proportion
@app.callback(
    Output("automation-proportion-results", "children"),
    Output("ap-summary-value", "children"),
    Output("ap-summary-value", "style"),
    Input("highlight-selector", "value"),
    Input("category-overrides-store", "data"),
    State("responsibility-table", "data")
)
def compute_automation_proportion(highlight_track, category_overrides, data):
    default_style = {"fontSize": "20px", "fontWeight": "bold", "color": "#888"}
    
    if not data or highlight_track == "none":
        return (
            html.P("Select a highlight track to compute the Automation Proportion.", 
                   style={"color": "#888", "fontStyle": "italic"}),
            "--",
            default_style
        )
    
    if category_overrides is None:
        category_overrides = {}
    
    df = pd.DataFrame(data)
    
    if "Category" not in df.columns:
        return html.P("Category column not found in data.", style={"color": "red"})
    
    categories = sorted(df["Category"].dropna().unique())
    K = len(categories)
    
    if K == 0:
        return (html.P("No categories found.", style={"color": "red"}), "--", default_style)
    
    category_results = []
    category_scores = []
    
    for category in categories:
        cat_df = df[df["Category"] == category]
        Tk = len(cat_df)
        
        if Tk == 0:
            continue
        
        weights = []
        task_details = []
        
        for _, row in cat_df.iterrows():
            task_name = row.get("Task Object", "Unknown")
            
            # Get agent colors
            agent_colors = {
                "HUMAN*": row["Human*"].strip().lower() if isinstance(row.get("Human*"), str) else "",
                "TARS*": row["TARS*"].strip().lower() if isinstance(row.get("TARS*"), str) else "",
            }
            
            # Determine chosen agent using the same logic as highlighting
            chosen_agent = get_chosen_agent_with_override(row, highlight_track, category_overrides, agent_colors)
            
            # Determine if there's TARS support for HUMAN* performer
            # Only applies to human-focused modes (human_full_support)
            # In agent-focused modes, the support direction is HUMAN→TARS*, which doesn't affect automation level
            tars_supporter_color = row["TARS"].strip().lower() if isinstance(row.get("TARS"), str) else ""
            is_human_focused_support = highlight_track == "human_full_support"
            has_tars_support = is_human_focused_support and chosen_agent == "HUMAN*" and tars_supporter_color in ["green", "yellow", "orange"]
            
            # Calculate weight
            if chosen_agent == "TARS*":
                w = 1.0  # Delegated (same regardless of HUMAN support)
                allocation = "Delegated"
            elif chosen_agent == "HUMAN*" and has_tars_support:
                w = 0.5  # Supported (only in human_full_support mode)
                allocation = "Supported"
            elif chosen_agent == "HUMAN*":
                w = 0.0  # Manual
                allocation = "Manual"
            else:
                w = 0.0  # Default to manual if no performer
                allocation = "Unassigned"
            
            weights.append(w)
            task_details.append({"task": task_name, "weight": w, "allocation": allocation, "performer": chosen_agent or "None"})
        
        # Category score
        pk = sum(weights) / Tk if Tk > 0 else 0
        category_scores.append(pk)
        
        category_results.append({
            "category": category,
            "Tk": Tk,
            "pk": pk,
            "tasks": task_details
        })
    
    # Overall Automation Proportion
    P = sum(category_scores) / K if K > 0 else 0
    
    # Build the display
    highlight_label = {
        "human_baseline": "Human-performer-only (no support)",
        "human_full_support": "Human-performer-only (full support)",
        "agent_whenever_possible": "Agent-whenever-possible (no support)",
        "agent_whenever_possible_full_support": "Agent-whenever-possible (full support)",
        "most_reliable": "Most reliable path"
    }.get(highlight_track, highlight_track)
    
    # Category breakdown table
    category_table_rows = []
    for result in category_results:
        category_table_rows.append(
            html.Tr([
                html.Td(result["category"], style={"padding": "8px", "borderBottom": "1px solid #ddd"}),
                html.Td(str(result["Tk"]), style={"padding": "8px", "borderBottom": "1px solid #ddd", "textAlign": "center"}),
                html.Td(f"{result['pk']:.3f}", style={"padding": "8px", "borderBottom": "1px solid #ddd", "textAlign": "center"})
            ])
        )
    
    detailed_results = html.Div([
        html.P([html.B("Selected Track: "), highlight_label], style={"marginBottom": "15px"}),
        
        # Override info if any
        html.Div([
            html.P([html.B("Category Overrides Applied: "), 
                   ", ".join([f"{k}{v}" for k, v in category_overrides.items()]) if category_overrides else "None"],
                  style={"marginBottom": "15px", "color": "#666"})
        ]) if category_overrides else None,
        
        # Category scores table
        html.Table([
            html.Thead(html.Tr([
                html.Th("Category", style={"padding": "10px", "borderBottom": "2px solid #333", "textAlign": "left"}),
                html.Th("Tasks (Tₖ)", style={"padding": "10px", "borderBottom": "2px solid #333", "textAlign": "center"}),
                html.Th("Score (pₖ)", style={"padding": "10px", "borderBottom": "2px solid #333", "textAlign": "center"})
            ])),
            html.Tbody(category_table_rows)
        ], style={"width": "100%", "borderCollapse": "collapse", "marginBottom": "20px"}),
        
        # Final result
        html.Div([
            dcc.Markdown(f'''
**Computation:**

$$P = \\frac{{1}}{{{K}}} \\sum_{{k=1}}^{{{K}}} p_k = \\frac{{1}}{{{K}}} \\times {sum(category_scores):.3f} = \\mathbf{{{P:.3f}}}$$
            ''', mathjax=True),
            html.Div([
                html.Span(f"Automation Proportion: ", style={"fontSize": "18px"}),
                html.Span(f"{P:.1%}", style={"fontSize": "24px", "fontWeight": "bold", 
                                              "color": "#2196F3" if P > 0.5 else "#4CAF50" if P < 0.5 else "#FF9800"})
            ], style={"textAlign": "center", "padding": "15px", "backgroundColor": "#f9f9f9", "borderRadius": "8px", "marginTop": "15px"})
        ])
    ])
    
    # Summary value and style for the top box
    summary_color = "#2196F3" if P > 0.5 else "#4CAF50" if P < 0.5 else "#FF9800"
    summary_style = {"fontSize": "20px", "fontWeight": "bold", "color": summary_color}
    
    return detailed_results, f"{P:.1%}", summary_style


if __name__ == "__main__":
    app.run(host="0.0.0.0", port=8050, debug=False)