File size: 163,418 Bytes
bac97e4
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
2900
2901
2902
2903
2904
2905
2906
2907
2908
2909
2910
2911
2912
2913
2914
2915
2916
2917
2918
2919
2920
2921
2922
2923
2924
2925
2926
2927
2928
2929
2930
2931
2932
2933
2934
2935
2936
2937
2938
2939
2940
2941
2942
2943
2944
2945
2946
2947
2948
2949
2950
2951
2952
2953
2954
2955
2956
2957
2958
2959
2960
2961
2962
2963
2964
2965
2966
2967
2968
2969
2970
2971
2972
2973
2974
2975
2976
2977
2978
2979
2980
2981
2982
2983
2984
2985
2986
2987
2988
2989
2990
2991
2992
2993
2994
2995
2996
2997
2998
2999
3000
3001
3002
3003
3004
3005
3006
3007
3008
3009
3010
3011
3012
3013
3014
3015
3016
3017
3018
3019
3020
3021
3022
3023
3024
3025
3026
3027
3028
3029
3030
3031
3032
3033
3034
3035
3036
3037
3038
3039
3040
3041
3042
3043
3044
3045
3046
3047
3048
3049
3050
3051
3052
3053
3054
3055
3056
3057
3058
3059
3060
3061
3062
3063
3064
3065
3066
3067
3068
3069
3070
3071
3072
3073
3074
3075
3076
3077
3078
3079
3080
3081
3082
3083
3084
3085
3086
3087
3088
3089
3090
3091
3092
3093
3094
3095
3096
3097
3098
3099
3100
3101
3102
3103
3104
3105
3106
3107
3108
3109
3110
3111
3112
3113
3114
3115
3116
3117
3118
3119
3120
3121
3122
3123
3124
3125
3126
3127
3128
3129
3130
3131
3132
3133
3134
3135
3136
3137
3138
3139
3140
3141
3142
3143
3144
3145
3146
3147
3148
3149
3150
3151
3152
3153
3154
3155
3156
3157
3158
3159
3160
3161
3162
3163
3164
3165
3166
3167
3168
3169
3170
3171
3172
3173
3174
3175
3176
3177
3178
3179
3180
3181
3182
3183
3184
3185
3186
3187
3188
3189
3190
3191
3192
3193
3194
3195
3196
3197
3198
3199
3200
3201
3202
3203
3204
3205
3206
3207
3208
3209
3210
3211
3212
3213
3214
3215
3216
3217
3218
3219
3220
3221
3222
3223
3224
3225
3226
3227
3228
3229
3230
3231
3232
3233
3234
3235
3236
3237
3238
3239
3240
3241
3242
3243
3244
3245
3246
3247
3248
3249
3250
3251
3252
3253
3254
3255
3256
3257
3258
3259
3260
3261
3262
3263
3264
3265
3266
3267
3268
3269
3270
3271
3272
3273
3274
3275
3276
3277
3278
3279
3280
3281
3282
3283
3284
3285
3286
3287
3288
3289
3290
3291
3292
3293
3294
3295
3296
3297
3298
3299
3300
3301
3302
import gradio as gr
import pandas as pd
import numpy as np
import matplotlib
matplotlib.use("Agg")
import matplotlib.pyplot as plt
import matplotlib.patches as mpatches
from matplotlib.colors import LinearSegmentedColormap
from matplotlib.lines import Line2D
from matplotlib.patches import Patch
import matplotlib.gridspec as gridspec
import matplotlib.patheffects as pe
from pycirclize import Circos
import warnings
warnings.filterwarnings("ignore")

from scipy import stats
from scipy.optimize import minimize_scalar
from scipy.cluster.hierarchy import dendrogram, linkage
from sklearn.decomposition import PCA
from sklearn.preprocessing import StandardScaler
from sklearn.linear_model import LinearRegression
from sklearn.manifold import MDS
from statsmodels.stats.multitest import multipletests
import io, re, os, zipfile, tempfile
from io import StringIO
from openpyxl import Workbook
from openpyxl.styles import Font, PatternFill, Alignment, Border, Side
from openpyxl.utils import get_column_letter

# ─────────────────────── DEFAULT COLOR PALETTES ──────────────────────────────────
BG_DARK     = "#0D1117"
PANEL_DARK  = "#161B22"
PANEL_MID   = "#21262D"
ACCENT_TEAL = "#2DD4BF"
ACCENT_GOLD = "#F59E0B"
ACCENT_CORAL= "#F87171"
ACCENT_BLUE = "#60A5FA"
ACCENT_LIME = "#84CC16"
ACCENT_PURP = "#A78BFA"
ACCENT_PINK = "#F472B6"
ACCENT_CYAN = "#22D3EE"
ACCENT_ORG  = "#FB923C"
TEXT_MAIN   = "#E6EDF3"
TEXT_DIM    = "#8B949E"
GRID_LINE   = "#30363D"
SIG_COLOR   = "#F59E0B"

# Light-theme equivalents -- used for ALL plots (PCA must NOT default to dark)
BG_LIGHT      = "#FFFFFF"
PANEL_LIGHT   = "#FAFBFF"
TEXT_DARK     = "#2D3142"
GRID_LIGHT    = "#E8ECF4"
ACCENT_TEAL_D = "#0F766E"
ACCENT_GOLD_D = "#B45309"
ACCENT_PINK_D = "#BE185D"

MODEL_COLORS = {
    "OLS":        "#60A5FA",
    "MLM":        "#2DD4BF",
    "EMMAX":      "#F472B6",
    "FarmCPU":    "#84CC16",
    "GEMMA":      "#E879F9",
    "BLINK":      "#FB923C",
    "mrMLM":      "#A78BFA",
    "FASTmrMLM":  "#F59E0B",
}

CHR_PALETTE = [
    "#4E79A7","#F28E2B","#E15759","#76B7B2","#59A14F",
    "#EDC948","#B07AA1","#FF9DA7","#9C755F","#BAB0AC",
    "#499894","#86BCB6","#D4A6C8","#FFBE7D","#8CD17D",
]

ALL_MODELS = ["OLS", "MLM", "EMMAX", "FarmCPU", "GEMMA", "BLINK", "mrMLM", "FASTmrMLM"]

def style_fig_white(fig, font_size=9, font_color="#2D3142", grid_color="#E8ECF4"):
    fig.patch.set_facecolor("#FFFFFF")
    for ax in fig.get_axes():
        ax.set_facecolor("#FAFBFF")
        ax.tick_params(colors=font_color, labelsize=font_size)
        ax.xaxis.label.set_color(font_color)
        ax.yaxis.label.set_color(font_color)
        ax.title.set_color(font_color)
        for spine in ax.spines.values():
            spine.set_edgecolor("#CCCCCC")
        ax.grid(color=grid_color, linewidth=0.5, linestyle="--", alpha=0.7)
    return fig

def apply_custom_style(fig, cfg):
    bg = cfg.get("bg_color", "#FFFFFF")
    panel = cfg.get("panel_color", "#FAFBFF")
    fc = cfg.get("font_color", "#2D3142")
    fs = cfg.get("font_size", 9)
    gc = cfg.get("grid_color", "#E8ECF4")
    fig.patch.set_facecolor(bg)
    for ax in fig.get_axes():
        ax.set_facecolor(panel)
        ax.tick_params(colors=fc, labelsize=fs)
        ax.xaxis.label.set_color(fc)
        ax.yaxis.label.set_color(fc)
        ax.title.set_color(fc)
        for spine in ax.spines.values():
            spine.set_edgecolor("#CCCCCC")
        ax.grid(color=gc, linewidth=0.5, linestyle="--", alpha=0.7)
        ax.xaxis.label.set_fontsize(fs)
        ax.yaxis.label.set_fontsize(fs)
        ax.title.set_fontsize(fs + 2)
    return fig

# ─────────────────────── DATA PARSING ────────────────────────────────────────────
def parse_data(file_obj=None, text_input=None):
    raw = None
    if file_obj is not None:
        try:
            raw = pd.read_csv(file_obj.name, sep=None, engine="python")
        except Exception:
            raw = pd.read_csv(file_obj.name, sep="\t")
    elif text_input and text_input.strip():
        try:
            raw = pd.read_csv(StringIO(text_input), sep=None, engine="python")
        except Exception:
            raw = pd.read_csv(StringIO(text_input), sep="\t")
    else:
        raise ValueError("No data provided.")

    chrom_info = {}
    if len(raw) > 1:
        second_row = raw.iloc[1].astype(str)
        chrom_vals = second_row.iloc[2:].astype(str)
        if len(chrom_vals) > 0 and chrom_vals.str.match(r'^\d+$').all():
            for i, col in enumerate(raw.columns[2:]):
                try:
                    chrom_info[col] = int(second_row.iloc[2 + i])
                except:
                    chrom_info[col] = 1
            raw = raw.drop(index=1).reset_index(drop=True)

    cols = list(raw.columns)
    id_col = cols[0]
    phen_col = cols[1]
    snp_cols = cols[2:]

    raw = raw[pd.to_numeric(raw[phen_col], errors="coerce").notna()].copy()
    raw[phen_col] = raw[phen_col].astype(float)
    geno = raw[snp_cols].apply(pd.to_numeric, errors="coerce")

    # Only these are unambiguous missing-value sentinels. -1 is deliberately
    # NOT in this list: many marker matrices (rrBLUP/GAPIT-style) use -1/0/1
    # as REAL genotype calls (homozygous ref / het / homozygous alt), not a
    # missing code. Blindly nuking -1 as missing in that case wipes out
    # ~30-55% of real genotype calls and corrupts MAF, kinship, and PCA
    # downstream (all of which assume a clean 0/1/2 dosage scale).
    geno = geno.where(~geno.isin([-9, -99, -999]), np.nan)

    # Detect the coding scheme from the observed value range and normalize
    # everything to a 0/1/2 additive dosage scale so downstream math is
    # always consistent regardless of which format the input used.
    obs_vals = geno.values[~pd.isna(geno.values)]
    if obs_vals.size > 0 and obs_vals.min() >= -1 and obs_vals.max() <= 1:
        # -1/0/1 coding detected -> rescale to 0/1/2
        geno = geno + 1
    else:
        # 0/1/2 coding: here -1 really is a missing-value sentinel
        geno = geno.where(geno != -1, np.nan)

    df = pd.DataFrame()
    df["ID"] = raw[id_col].values
    df["Phenotype"] = raw[phen_col].values
    for c in snp_cols:
        df[c] = geno[c].values

    chroms, positions = [], []
    # Fallback position counters MUST be per-chromosome, not a single running
    # index over the whole marker file. A global counter (the old
    # `len(positions) + 1`) makes later chromosomes carry huge positional
    # offsets (e.g. chr9 markers numbered ~3100-3400 instead of 1-300), which
    # plot_manhattan() masks by subtracting each chromosome's own min before
    # plotting, but plot_circos_density() does not -- producing a genuinely
    # empty arc at the start of every sector sized to the wrong (inflated)
    # chromosome length. Keeping the counter per-chromosome fixes it at the
    # source instead of relying on every downstream plot to compensate.
    chr_counters = {}

    def _next_idx(c):
        chr_counters[c] = chr_counters.get(c, 0) + 1
        return chr_counters[c]

    for m in snp_cols:
        if m in chrom_info:
            c = chrom_info[m]
            chroms.append(c)
            positions.append(_next_idx(c))
        else:
            m_clean = str(m).strip()
            mat = re.match(r"[Cc]hr(\d+)[_\-](\d+)", m_clean)
            if mat:
                c = int(mat.group(1))
                chroms.append(c)
                positions.append(int(mat.group(2)))
            else:
                num = re.findall(r"\d+", m_clean)
                c = int(num[0]) if num else 1
                chroms.append(c)
                positions.append(int(num[1]) if len(num) > 1 else _next_idx(c))

    return df, snp_cols, np.array(chroms), np.array(positions)

# ─────────────────────── UTILITY ─────────────────────────────────────────────────
def impute_genotypes(geno_matrix):
    mat = geno_matrix.copy().astype(float)
    for j in range(mat.shape[1]):
        col = mat[:, j]
        m = np.nanmean(col)
        col[np.isnan(col)] = m if not np.isnan(m) else 0
        mat[:, j] = col
    return mat

def compute_maf(G):
    freqs = np.nanmean(G, axis=0) / 2
    return np.where(freqs > 0.5, 1 - freqs, freqs)

def compute_call_rate(G):
    return 1 - np.isnan(G).mean(axis=0)

def build_kinship(G):
    p = np.nanmean(G, axis=0) / 2
    Z = G - 2 * p
    denom = 2 * np.sum(p * (1 - p))
    if denom == 0:
        denom = 1
    return Z @ Z.T / denom

def compute_pve_all(y, G, K=None, X0=None):
    """
    PVE (%) for every marker via genuine variance-component partitioning
    instead of a raw squared Pearson correlation.

    A plain r^2 between one marker and y only equals a valid PVE estimate
    when the model has no other covariates and no relatedness structure
    (it happens to coincide with SS_marker/SS_total for a bare
    single-predictor OLS fit). Once a kinship random effect or Q
    covariates are in the model, that raw correlation double-counts
    variance already absorbed by relatedness/structure and no longer
    matches what GAPIT/GEMMA-style PVE figures report.

    This instead computes, for each marker j:
        PVE_j = (RSS_null - RSS_full_j) / SS_total(y) * 100
    where RSS_null is the residual sum of squares of the model with just
    the intercept (+ X0 covariates, if given), RSS_full_j additionally
    includes marker j, and SS_total(y) is the total phenotypic sum of
    squares -- the same denominator convention GREML/GCTA-style "variance
    explained" figures use, so PVE values from different markers are
    directly comparable and summable, unlike per-marker r^2.

    If K (kinship) is supplied, y/X0/G are first weighted by the
    REML-estimated genetic/residual variance ratio (delta), exactly as
    mixed_model_gwas does, so a marker's PVE is *conditional on* the
    polygenic background already explained by relatedness rather than
    re-claiming variance the random effect already accounts for.
    """
    y = np.asarray(y, dtype=float)
    n, m = G.shape
    if X0 is None:
        X0 = np.ones((n, 1))

    SS_total = float(np.sum((y - y.mean()) ** 2))
    if SS_total <= 0:
        return np.zeros(m)

    if K is not None:
        try:
            evals, evecs = eigh_kinship(K)
            _, _, delta, _, _ = emma_reml(y, K, X0, evals=evals, evecs=evecs)
            w = 1.0 / (evals + delta)
            sw = np.sqrt(w)
            yt = (evecs.T @ y) * sw
            X0t = (evecs.T @ X0) * sw[:, None]
            Gt = (evecs.T @ G) * sw[:, None]
        except Exception:
            yt, X0t, Gt = y, X0, G
    else:
        yt, X0t, Gt = y, X0, G

    beta0, *_ = np.linalg.lstsq(X0t, yt, rcond=None)
    rss0 = float(np.sum((yt - X0t @ beta0) ** 2))

    pve = np.zeros(m)
    for j in range(m):
        xj = Gt[:, j]
        if np.std(xj) < 1e-8:
            continue
        Xf = np.column_stack([X0t, xj])
        try:
            beta, *_ = np.linalg.lstsq(Xf, yt, rcond=None)
        except np.linalg.LinAlgError:
            continue
        rss = float(np.sum((yt - Xf @ beta) ** 2))
        pve[j] = max(0.0, rss0 - rss) / SS_total * 100
    return pve


RENDER_DPI = 300  # publication-quality; was 150
EXPORT_DPI = 400  # all downloadable PNG/figure files must exceed 300 dpi

def fig_to_png_bytes(fig):
    buf = io.BytesIO()
    fig.savefig(buf, format="png", dpi=RENDER_DPI, bbox_inches="tight", facecolor=fig.get_facecolor())
    buf.seek(0)
    return buf.read()

def fig_to_pil(fig):
    buf = io.BytesIO()
    fig.savefig(buf, format="png", dpi=RENDER_DPI, bbox_inches="tight", facecolor=fig.get_facecolor())
    buf.seek(0)
    from PIL import Image
    return Image.open(buf).copy()

def get_top_mta(pvals, n=15):
    return np.argsort(pvals)[:min(n, len(pvals))]

def get_significant_idx(pvals, lod_threshold=None, fallback_top_n=10,
                         pve_vals=None, pve_threshold=None):
    """
    Indices of markers that actually PASS the significance threshold --
    this is what should be highlighted as MTAs, not a blind top-N slice
    that highlights N markers regardless of whether any of them are real.

    lod_threshold is on the LOD scale (see neglog10p_to_lod) and is meant
    to be the permutation-derived empirical threshold -- the primary
    significance criterion for this pipeline, the same role a LOD
    threshold plays in linkage/QTL mapping. Returns indices sorted by
    increasing p-value (most significant first). If no permutation
    threshold is available (e.g. it failed to compute), falls back to a
    small top-N so plots don't render silently empty -- this is a safety
    net, not the intended mode of operation.

    pve_vals / pve_threshold add a SECOND, independent filter: if both are
    given, a marker must ALSO explain at least pve_threshold % of
    phenotypic variance to be kept as an MTA, on top of passing the LOD
    threshold. pve_threshold of None/0 disables this filter.
    """
    pvals = np.asarray(pvals)
    if lod_threshold is None:
        idx = get_top_mta(pvals, fallback_top_n)
    else:
        log_p = -np.log10(np.clip(pvals, 1e-300, 1))
        lod_vals = neglog10p_to_lod(log_p)
        idx = np.where(lod_vals >= lod_threshold)[0]
        idx = idx[np.argsort(pvals[idx])]

    if pve_vals is not None and pve_threshold:
        pve_vals = np.asarray(pve_vals)
        idx = idx[pve_vals[idx] >= pve_threshold]

    return idx

def compute_thresholds(n_snps, model_name=""):
    """
    Compute and explain Bonferroni and suggestive thresholds.
    Returns thresholds and explanation text.
    """
    bonferroni = 0.05 / n_snps
    suggestive = 1.0 / n_snps  # 1 expected false positive genome-wide

    model_notes = {
        "OLS":     "No structure/relatedness correction β€” baseline; expect inflation (Ξ» > 1).",
        "MLM":     "Q (kinship PCs, fixed) + K (polygenic random effect, REML) β€” Yu et al. 2006.",
        "EMMAX":   "Pure kinship P3D mixed model; variance components estimated once genome-wide.",
        "FarmCPU": "Iterative FEM/REM with bin-based pseudo-QTN cofactors (Liu et al. 2016).",
        "GEMMA":   "Exact per-marker REML LMM (Zhou & Stephens 2012); no post-hoc GC rescaling.",
    }
    note = model_notes.get(model_name, "")

    explanation = (
        f"──────────────────────────────────────────────\n"
        f"  Threshold Calculations β€” {model_name}\n"
        f"──────────────────────────────────────────────\n"
        f"  SNPs tested (m) : {n_snps:,}\n\n"
        f"  πŸ“Œ Bonferroni threshold:\n"
        f"     Ξ± / m = 0.05 / {n_snps:,} = {bonferroni:.3e}\n"
        f"     -log₁₀(p) = {-np.log10(bonferroni):.2f}\n"
        f"     Controls genome-wide Type-I error at 5%.\n"
        f"     Assumes all tests are independent.\n\n"
        f"  πŸ“Œ Suggestive threshold:\n"
        f"     1 / m = 1 / {n_snps:,} = {suggestive:.3e}\n"
        f"     -log₁₀(p) = {-np.log10(suggestive):.2f}\n"
        f"     Expects ~1 false positive per genome scan.\n"
        f"     Lander & Kruglyak (1995) convention.\n\n"
        f"  ℹ️  Model note: {note}\n"
        f"──────────────────────────────────────────────\n"
    )
    return bonferroni, suggestive, explanation

# ─────────────────────── EMMA / REML MIXED-MODEL ENGINE ──────────────────────────
# Shared machinery for MLM / EMMAX / GEMMA, following the eigendecomposition
# approach of Kang et al. 2008 (EMMA) and its extensions (EMMAX, FaST-LMM,
# GEMMA). The kinship matrix is decomposed ONCE; every subsequent variance-
# component search reuses the eigenvalues, which is what makes these methods
# tractable at genome scale instead of refitting an n x n GLS from scratch
# per marker.

def eigh_kinship(K):
    """Symmetric eigendecomposition of the kinship matrix. Eigenvalues are
    floored at a small positive number to keep downstream log/inversion
    steps numerically stable (K can be near-singular for small panels)."""
    n = K.shape[0]
    Ks = (K + K.T) / 2.0
    evals, evecs = np.linalg.eigh(Ks)
    evals = np.clip(evals, 1e-8, None)
    return evals, evecs

def _profile_loglik(log_delta, evals, yt, Xt, reml=True):
    """REML (or ML) profile log-likelihood as a function of delta =
    sigma_e^2 / sigma_g^2, evaluated in the rotated (eigenvector) basis
    where the covariance structure becomes diagonal: var(yt_i) = D_i + delta.
    """
    delta = np.exp(log_delta)
    w = 1.0 / (evals + delta)
    sw = np.sqrt(w)
    Xw = Xt * sw[:, None]
    yw = yt * sw
    beta, *_ = np.linalg.lstsq(Xw, yw, rcond=None)
    resid = yw - Xw @ beta
    n = len(yt)
    q = Xt.shape[1]
    df = max(n - q, 1) if reml else n
    rss = float(np.sum(resid ** 2))
    sigma2 = rss / df if df > 0 else rss / max(n, 1)
    if sigma2 <= 0:
        sigma2 = 1e-12
    ll = -0.5 * (df * np.log(2 * np.pi * sigma2) + np.sum(np.log(evals + delta)) + df)
    if reml:
        XtWX = Xw.T @ Xw
        sign, logdet = np.linalg.slogdet(XtWX)
        if sign > 0:
            ll -= 0.5 * logdet
    return ll

def emma_reml(y, K, X=None, evals=None, evecs=None):
    """
    Estimate the genetic/residual variance ratio delta via REML, using the
    eigendecomposition of K (Kang et al. 2008). Returns eigvals, eigvecs,
    delta_hat, sigma_g2_hat, sigma_e2_hat.
    """
    n = len(y)
    if X is None:
        X = np.ones((n, 1))
    if evals is None or evecs is None:
        evals, evecs = eigh_kinship(K)
    yt = evecs.T @ y
    Xt = evecs.T @ X

    res = minimize_scalar(lambda ld: -_profile_loglik(ld, evals, yt, Xt, reml=True),
                           bounds=(-10, 10), method="bounded",
                           options={"xatol": 1e-4})
    delta_hat = float(np.exp(res.x))

    w = 1.0 / (evals + delta_hat)
    sw = np.sqrt(w)
    Xw = Xt * sw[:, None]
    yw = yt * sw
    beta, *_ = np.linalg.lstsq(Xw, yw, rcond=None)
    resid = yw - Xw @ beta
    df = max(n - X.shape[1], 1)
    sigma_g2 = float(np.sum(resid ** 2) / df)
    sigma_e2 = sigma_g2 * delta_hat
    return evals, evecs, delta_hat, sigma_g2, sigma_e2

def _wald_test_rotated(y_col, X_cols, evals, delta):
    """GLS Wald test for the last column of X_cols (the marker), with y and
    X already rotated into the eigenvector basis. Weights 1/(D_i+delta) make
    this an ordinary weighted least squares problem β€” the whole reason the
    eigendecomposition trick works."""
    w = 1.0 / (evals + delta)
    sw = np.sqrt(w)
    Xw = X_cols * sw[:, None]
    yw = y_col * sw
    try:
        XtX_inv = np.linalg.inv(Xw.T @ Xw)
    except np.linalg.LinAlgError:
        return 1.0, 0.0, np.nan
    beta = XtX_inv @ (Xw.T @ yw)
    resid = yw - Xw @ beta
    dfree = max(len(y_col) - X_cols.shape[1], 1)
    sigma2 = float(np.sum(resid ** 2) / dfree)
    se = float(np.sqrt(max(sigma2 * XtX_inv[-1, -1], 0)))
    b = float(beta[-1])
    t_stat = b / (se + 1e-300)
    p = float(2 * stats.t.sf(abs(t_stat), df=dfree))
    return p, b, se

def mixed_model_gwas(y, G, K, covariates=None):
    """
    P3D mixed-model GWAS (Population Parameters Previously Determined):
    variance components are estimated ONCE from the null model (no marker
    effect), then reused for every marker's GLS test. This is what real
    MLM/EMMAX software does by default (TASSEL, GAPIT, EMMAX itself) since
    per-marker REML gives negligible accuracy gain for a large speed cost.
    """
    n, m = G.shape
    X0 = np.ones((n, 1))
    if covariates is not None and covariates.shape[1] > 0:
        X0 = np.column_stack([X0, covariates])

    evals, evecs, delta, sigma_g2, sigma_e2 = emma_reml(y, K, X0)
    yt = evecs.T @ y
    X0t = evecs.T @ X0
    Gt = evecs.T @ G  # rotate all markers once β€” O(n^2 m), not O(n^3) per marker

    pvals = np.ones(m); betas = np.zeros(m); ses = np.zeros(m)
    for j in range(m):
        if G[:, j].std() < 1e-8:
            continue
        Xcols = np.column_stack([X0t, Gt[:, j]])
        p, b, se = _wald_test_rotated(yt, Xcols, evals, delta)
        pvals[j] = p; betas[j] = b; ses[j] = se
    return pvals, betas, ses, delta

# ─────────────────────── PERMUTATION (LOD) THRESHOLD ──────────────────────────────
def permutation_threshold_lod(y, G, K, extra_covariates=None, n_perm=100, alpha=0.05, seed=42):
    """
    Empirical permutation significance threshold (Churchill & Doerge 1994
    style -- the same logic used for LOD thresholds in linkage/QTL mapping),
    expressed on the LOD scale via neglog10p_to_lod so it's directly
    comparable to a QTL-mapping LOD threshold. This is meant to be THE
    primary significance criterion for this pipeline -- which MTAs get
    highlighted should be driven by whether they pass this threshold, not
    by an arbitrary top-N slice.

    Phenotype is shuffled n_perm times; for each permutation the maximum
    -log10(p) across the whole genome is recorded via the fast P3D mixed-
    model engine (variance components estimated once on the REAL data and
    held fixed across permutations -- standard practice for tractable
    mixed-model permutation, since re-running REML per permutation per
    marker would be computationally prohibitive). The threshold is the
    (1-alpha) quantile of that null max-statistic distribution, converted
    to LOD units.
    """
    rng = np.random.default_rng(seed)
    n, m = G.shape
    X0 = np.ones((n, 1))
    if extra_covariates is not None and extra_covariates.shape[1] > 0:
        X0 = np.column_stack([X0, extra_covariates])

    evals, evecs, delta, _, _ = emma_reml(y, K, X0)
    X0t = evecs.T @ X0
    Gt = evecs.T @ G
    valid_markers = np.where(G.std(axis=0) > 1e-8)[0]

    max_lp = np.zeros(n_perm)
    for b in range(n_perm):
        y_perm = rng.permutation(y)
        yt = evecs.T @ y_perm
        best = 0.0
        for j in valid_markers:
            Xcols = np.column_stack([X0t, Gt[:, j]])
            p, _, _ = _wald_test_rotated(yt, Xcols, evals, delta)
            lp = -np.log10(max(p, 1e-300))
            if lp > best:
                best = lp
        max_lp[b] = best

    threshold_neglog10p = float(np.quantile(max_lp, 1 - alpha))
    threshold_lod = float(neglog10p_to_lod(threshold_neglog10p))
    return threshold_lod, threshold_neglog10p, max_lp


def ols_gwas(y, G):
    """Simple linear regression per marker, no correction for structure or
    relatedness. The baseline every other model is judged against."""
    n, m = G.shape
    pvals = np.ones(m); betas = np.zeros(m); ses = np.zeros(m)
    for j in range(m):
        x = G[:, j] - G[:, j].mean()
        if x.std() < 1e-8: continue
        slope, _, _, p, se = stats.linregress(x, y)
        pvals[j] = p; betas[j] = slope; ses[j] = se
    return pvals, betas, ses

def get_kinship_pcs(K, n_pca, n_samples):
    """Top n_pca eigenvectors of the kinship matrix, used as fixed-effect
    'Q' structure covariates by MLM, mrMLM, FASTmrMLM, and (optionally)
    GEMMA. Shared here so the per-model PCA covariate count set in the UI
    (Cofactors tab) is applied identically across every model that uses it."""
    n_pca = max(min(int(n_pca), n_samples - 4), 0)
    if n_pca <= 0:
        return None
    evals_K, evecs_K = eigh_kinship(K)
    order = np.argsort(evals_K)[::-1]
    return evecs_K[:, order[:n_pca]]

def mlm_gwas(y, G, K, n_pca=3):
    """Q + K mixed linear model (Yu et al. 2006): top kinship eigenvectors
    as fixed 'Q' structure covariates, PLUS the polygenic random effect
    (via REML-estimated variance components) rather than eigenvectors
    substituting for it. This is the actual unified MLM, not a PCA-only
    correction. n_pca sets how many kinship PCs are used as covariates."""
    n = len(y)
    Q = get_kinship_pcs(K, n_pca, n)
    pvals, betas, ses, _ = mixed_model_gwas(y, G, K, covariates=Q)
    return pvals, betas, ses

def emmax_gwas(y, G, K):
    """EMMAX (Kang et al. 2008): pure kinship-based P3D mixed model, no
    separate Q structure covariates β€” relatedness alone absorbs population
    structure. Same REML/eigendecomposition engine as MLM, without the
    fixed PCs."""
    pvals, betas, ses, _ = mixed_model_gwas(y, G, K, covariates=None)
    return pvals, betas, ses

def _rem_prune_qtns(y, G, K, qtn_idx, alpha=0.01):
    """
    REM (Random Effect Model) step of FarmCPU: re-test each candidate
    pseudo-QTN's own effect in a kinship-based mixed model, with the other
    current pseudo-QTNs held in as fixed covariates, and drop any QTN that
    is no longer significant once the polygenic random effect is in the
    model. Real FarmCPU alternates exactly this -- a REM re-optimization of
    the pseudo-QTN set -- with the FEM genome scan; without it (fixed
    effects only, as in the previous version of this function) pseudo-QTNs
    that are only "significant" because of relatedness/background genetic
    correlation, rather than a real local effect, never get removed.
    """
    if not qtn_idx:
        return qtn_idx
    n = len(y)
    keep = []
    for idx in qtn_idx:
        others = [q for q in qtn_idx if q != idx]
        cov = None
        if others:
            cov = G[:, others] - G[:, others].mean(axis=0)
        try:
            p, _, _, _ = mixed_model_gwas(y, G[:, [idx]], K, covariates=cov)
        except Exception:
            keep.append(idx)  # fit failed -- don't silently drop a candidate
            continue
        if p[0] <= alpha:
            keep.append(idx)
    return keep

def farmcpu_gwas(y, G, K, chroms=None, positions=None, max_iter=8, bin_size=1_000_000,
                  rem_alpha=0.01):
    """
    FarmCPU (Liu et al. 2016): iterates (1) a bin-based candidate scan --
    a Fixed Effect Model (FEM) test of every marker with the current
    pseudo-QTNs as covariates, followed by one candidate kept per bin --
    and (2) a Random Effect Model (REM) step that re-tests each candidate
    pseudo-QTN in a kinship-based mixed model and drops any that aren't
    significant once relatedness is accounted for. This is the actual
    FEM/REM alternation FarmCPU is defined by, not FEM alone: the REM step
    is what keeps pseudo-QTNs that are only correlated with y through
    background relatedness (rather than a true local effect) from being
    locked in as covariates for the rest of the run.
    """
    n, m = G.shape
    if positions is None:
        positions = np.arange(m)
    if chroms is None:
        chroms = np.zeros(m, dtype=int)

    pvals = np.ones(m); betas = np.zeros(m); ses = np.zeros(m)
    pseudo_qtns, prev_qtns = [], None

    for _ in range(max_iter):
        X = np.ones((n, 1))
        if pseudo_qtns:
            cov = G[:, pseudo_qtns] - G[:, pseudo_qtns].mean(axis=0)
            X = np.column_stack([X, cov])

        pvals = np.ones(m); betas = np.zeros(m); ses = np.zeros(m)
        qtn_set = set(pseudo_qtns)
        for j in range(m):
            x = G[:, j]
            if x.std() < 1e-8:
                continue
            # A marker that is currently a pseudo-QTN is tested leave-one-out
            # (covariates = the OTHER pseudo-QTNs), not skipped -- otherwise
            # the true causal marker, which is exactly the one most likely
            # to have been selected as its own covariate, ends up at p=1.
            if j in qtn_set:
                others = [q for q in pseudo_qtns if q != j]
                if others:
                    cov = G[:, others] - G[:, others].mean(axis=0)
                    Xj = np.column_stack([np.ones((n, 1)), cov])
                else:
                    Xj = np.ones((n, 1))
            else:
                Xj = X
            Xf = np.column_stack([Xj, x])
            try:
                beta = np.linalg.lstsq(Xf, y, rcond=None)[0]
                XtX_inv = np.linalg.pinv(Xf.T @ Xf)
            except np.linalg.LinAlgError:
                continue
            resid = y - Xf @ beta
            dfree = max(n - Xf.shape[1], 1)
            sigma2 = float(np.sum(resid ** 2) / dfree)
            se = float(np.sqrt(max(sigma2 * XtX_inv[-1, -1], 0)))
            b = float(beta[-1])
            t_stat = b / (se + 1e-300)
            pvals[j] = float(2 * stats.t.sf(abs(t_stat), df=dfree))
            betas[j] = b; ses[j] = se

        bonf = 0.05 / m
        cand = np.where(pvals < bonf)[0]
        if len(cand) == 0:
            cand = np.argsort(pvals)[:5]

        bins = {}
        for idx in cand:
            key = (int(chroms[idx]), int(positions[idx] // bin_size))
            if key not in bins or pvals[idx] < pvals[bins[key]]:
                bins[key] = idx
        new_qtns = sorted(bins.values(), key=lambda i: pvals[i])[:10]

        # REM step: prune bin-selected candidates that don't hold up once
        # tested in a kinship random-effect model (see _rem_prune_qtns).
        new_qtns = _rem_prune_qtns(y, G, K, new_qtns, alpha=rem_alpha)

        if prev_qtns is not None and set(new_qtns) == set(prev_qtns):
            pseudo_qtns = new_qtns
            break
        prev_qtns = new_qtns
        pseudo_qtns = new_qtns

    return pvals, betas, ses

def _fixed_effect_scan(y, G, cov_idx, test_leave_one_out=True):
    """One fixed-effect-model scan of every marker in G, with the markers
    listed in cov_idx included as covariates (removed from the covariate
    set for a marker that IS one of them, i.e. tested leave-one-out).
    Shared by BLINK / mrMLM / FASTmrMLM below so their iteration logic
    stays readable."""
    n, m = G.shape
    pvals = np.ones(m); betas = np.zeros(m); ses = np.zeros(m)
    cov_set = set(cov_idx)
    for j in range(m):
        x = G[:, j]
        if x.std() < 1e-8:
            continue
        if test_leave_one_out and j in cov_set:
            others = [q for q in cov_idx if q != j]
        else:
            others = list(cov_idx)
        if others:
            cov = G[:, others] - G[:, others].mean(axis=0)
            Xj = np.column_stack([np.ones((n, 1)), cov])
        else:
            Xj = np.ones((n, 1))
        Xf = np.column_stack([Xj, x])
        try:
            beta = np.linalg.lstsq(Xf, y, rcond=None)[0]
            XtX_inv = np.linalg.pinv(Xf.T @ Xf)
        except np.linalg.LinAlgError:
            continue
        resid = y - Xf @ beta
        dfree = max(n - Xf.shape[1], 1)
        sigma2 = float(np.sum(resid ** 2) / dfree)
        se = float(np.sqrt(max(sigma2 * XtX_inv[-1, -1], 0)))
        b = float(beta[-1])
        t_stat = b / (se + 1e-300)
        pvals[j] = float(2 * stats.t.sf(abs(t_stat), df=dfree))
        betas[j] = b; ses[j] = se
    return pvals, betas, ses

def _bic_fixed_effect_model(y, G, cand_idx):
    """Fit y ~ intercept + G[:, cand_idx] and return BIC, used by BLINK to
    pick the best-supported pseudo-QTN subset instead of a fixed top-N."""
    n = len(y)
    if len(cand_idx) == 0:
        X = np.ones((n, 1))
    else:
        X = np.column_stack([np.ones((n, 1)), G[:, cand_idx]])
    beta, *_ = np.linalg.lstsq(X, y, rcond=None)
    resid = y - X @ beta
    k = X.shape[1]
    rss = float(np.sum(resid ** 2)) + 1e-300
    return n * np.log(rss / n) + k * np.log(n)

def blink_gwas(y, G, K, chroms=None, positions=None, max_iter=8, ld_r2=0.7,
                ld_block_bp=1_000_000):
    """
    BLINK (Huang et al. 2019, Bayesian-information and Linkage-disequilibrium
    Iteratively Nested Keyway): like FarmCPU, alternates a fixed-effect-model
    scan with pseudo-QTN selection, but differs in two places that give
    BLINK its name/speed advantage over FarmCPU: (1) candidate pseudo-QTNs
    are pruned within local LD blocks (candidates on the same chromosome and
    within ld_block_bp of an already-kept, more-significant candidate are
    compared by pairwise r^2 and dropped if redundant; candidates outside
    that window are never compared and are always kept, since LD is a local
    phenomenon and two markers hundreds of kb apart on different LD blocks
    can be "collapsed" by chance correlation in a small panel even with no
    real linkage) instead of a fixed physical bin size, so two markers 50bp
    apart in high LD are collapsed the same way two markers 500kb apart in
    low LD are kept separate; (2) the number of pseudo-QTNs carried into the
    next iteration is chosen by minimising BIC over nested candidate
    subsets, not a fixed top-10 cutoff. No kinship matrix is used once QTNs
    are found β€” BLINK, like FarmCPU, assumes the pseudo-QTNs themselves
    absorb population structure/relatedness.
    """
    n, m = G.shape
    if positions is None:
        positions = np.arange(m)
    if chroms is None:
        chroms = np.zeros(m, dtype=int)

    pseudo_qtns, prev_qtns = [], None
    pvals = np.ones(m); betas = np.zeros(m); ses = np.zeros(m)

    for _ in range(max_iter):
        pvals, betas, ses = _fixed_effect_scan(y, G, pseudo_qtns)

        bonf = 0.05 / m
        cand = np.where(pvals < bonf)[0]
        if len(cand) == 0:
            cand = np.argsort(pvals)[:10]
        cand = cand[np.argsort(pvals[cand])]

        # LD-prune candidates: keep a candidate only if it isn't in high LD
        # with an already-kept, more-significant candidate FROM THE SAME LD
        # BLOCK (same chromosome, within ld_block_bp). Candidates outside
        # that window skip the correlation check entirely -- LD blocks are
        # local, so comparing markers genome-wide (the previous behaviour)
        # could collapse two unlinked-but-coincidentally-correlated distant
        # markers into one pseudo-QTN.
        kept = []
        for idx in cand:
            redundant = False
            for k_idx in kept:
                same_block = (chroms[idx] == chroms[k_idx] and
                              abs(int(positions[idx]) - int(positions[k_idx])) <= ld_block_bp)
                if not same_block:
                    continue
                r = np.corrcoef(G[:, idx], G[:, k_idx])[0, 1]
                if np.isfinite(r) and r ** 2 >= ld_r2:
                    redundant = True
                    break
            if not redundant:
                kept.append(idx)
            if len(kept) >= 15:
                break

        # BIC-based model selection: try nested prefixes of `kept`
        # (most-significant-first) and keep the prefix with lowest BIC
        best_bic, best_set = np.inf, []
        for size in range(0, len(kept) + 1):
            subset = kept[:size]
            bic = _bic_fixed_effect_model(y, G, subset)
            if bic < best_bic:
                best_bic, best_set = bic, subset
        new_qtns = best_set

        if prev_qtns is not None and set(new_qtns) == set(prev_qtns):
            pseudo_qtns = new_qtns
            break
        prev_qtns = new_qtns
        pseudo_qtns = new_qtns

    return pvals, betas, ses

def mrmlm_gwas(y, G, K, chroms=None, positions=None, screen_thresh=0.01,
               max_candidates=20, drop_thresh=0.05, n_pca=3):
    """
    mrMLM (multi-locus random-SNP-effect mixed linear model; Wang et al.
    2016): a two-step multi-locus approach. Step 1 runs a lenient
    single-locus P3D mixed-model scan (same engine as MLM/EMMAX) and keeps
    every marker below `screen_thresh` as a candidate QTN. Step 2 fits all
    candidates simultaneously as fixed effects (approximating mrMLM's
    random-SNP-effect / EM-Bayes shrinkage step) and backward-eliminates
    the least significant one at a time until every remaining candidate is
    significant at `drop_thresh` β€” mrMLM's real innovation is testing
    QTNs jointly rather than one-at-a-time, which is what's reproduced
    here. Markers never selected as candidates keep their Step-1 p-value
    so the full-length p-value vector still plots as a Manhattan track.
    """
    n, m = G.shape
    Q = get_kinship_pcs(K, n_pca, n)
    pvals, betas, ses, _ = mixed_model_gwas(y, G, K, covariates=Q)

    cand = np.where(pvals < screen_thresh)[0]
    if len(cand) == 0:
        return pvals, betas, ses
    cand = cand[np.argsort(pvals[cand])][:max_candidates].tolist()

    # backward elimination on the joint multi-locus fixed-effect model
    while len(cand) > 0:
        p_joint, b_joint, se_joint = _fixed_effect_scan(y, G, cand)
        cand_p = {c: p_joint[c] for c in cand}
        worst = max(cand_p, key=cand_p.get)
        if cand_p[worst] > drop_thresh and len(cand) > 1:
            cand.remove(worst)
            continue
        break

    p_final, b_final, se_final = _fixed_effect_scan(y, G, cand)
    for c in cand:
        pvals[c] = p_final[c]; betas[c] = b_final[c]; ses[c] = se_final[c]
    return pvals, betas, ses

def fastmrmlm_gwas(y, G, K, chroms=None, positions=None, screen_thresh=0.01,
                    max_candidates=20, drop_thresh=0.05, n_pca=3):
    """
    FASTmrMLM (Tamba et al. 2017): the speed-optimised successor to mrMLM.
    The real method reuses a single kinship eigen-decomposition (computed
    once) to transform y and G, avoiding a per-marker REML fit; that's
    exactly what EMMAX/mixed_model_gwas already does here, so Step 1
    candidate screening below IS that fast rotated scan. Step 2 departs
    from mrMLM by fitting the joint multi-locus model as plain OLS
    (dropping the kinship term for the joint step, trading a small amount
    of type-I-error control for the speed gain that gives the method its
    name) with the same backward-elimination logic as mrMLM.
    """
    n, m = G.shape
    Q = get_kinship_pcs(K, n_pca, n)
    pvals, betas, ses, _ = mixed_model_gwas(y, G, K, covariates=Q)

    cand = np.where(pvals < screen_thresh)[0]
    if len(cand) == 0:
        return pvals, betas, ses
    cand = cand[np.argsort(pvals[cand])][:max_candidates].tolist()

    while len(cand) > 0:
        p_joint, b_joint, se_joint = _fixed_effect_scan(y, G, cand,
                                                          test_leave_one_out=True)
        cand_p = {c: p_joint[c] for c in cand}
        worst = max(cand_p, key=cand_p.get)
        if cand_p[worst] > drop_thresh and len(cand) > 1:
            cand.remove(worst)
            continue
        break

    p_final, b_final, se_final = _fixed_effect_scan(y, G, cand,
                                                      test_leave_one_out=True)
    for c in cand:
        pvals[c] = p_final[c]; betas[c] = b_final[c]; ses[c] = se_final[c]
    return pvals, betas, ses

def gemma_gwas(y, G, K, covariates=None):
    """
    Approximate GEMMA-style univariate LMM (Zhou & Stephens 2012): unlike
    the P3D models above, the genetic/residual variance ratio is
    re-estimated PER MARKER (the candidate SNP is included in the null
    model before REML), which is GEMMA's default exact behaviour. Slower
    than P3D but the most statistically rigorous of the five β€” no post-hoc
    p-value rescaling is applied, since a correctly specified LMM shouldn't
    need genomic-control patching.
    """
    n, m = G.shape
    X0 = np.ones((n, 1))
    if covariates is not None and covariates.shape[1] > 0:
        X0 = np.column_stack([X0, covariates])
    evals, evecs = eigh_kinship(K)
    yt = evecs.T @ y
    X0t = evecs.T @ X0
    Gt = evecs.T @ G

    pvals = np.ones(m); betas = np.zeros(m); ses = np.zeros(m)
    for j in range(m):
        if G[:, j].std() < 1e-8:
            continue
        Xt = np.column_stack([X0t, Gt[:, j]])
        res = minimize_scalar(lambda ld: -_profile_loglik(ld, evals, yt, Xt, reml=True),
                               bounds=(-10, 10), method="bounded",
                               options={"xatol": 1e-3})
        delta = float(np.exp(res.x))
        p, b, se = _wald_test_rotated(yt, Xt, evals, delta)
        pvals[j] = p; betas[j] = b; ses[j] = se
    return pvals, betas, ses

# ─────────────────────── CONFIG BUILDER ──────────────────────────────────────────
def build_cfg(w, h, fs, fc, gc="#E8ECF4", bg="#FFFFFF", panel="#FAFBFF",
              sig_col="#E15759", sug_col="#4E79A7", marker_size=12, alpha=0.75,
              font_color=None, dot_col=None, line_col=None, fill_col=None,
              axis_col=None):
    """
    Build a plot configuration dictionary.
    'fc' is the required positional font-color argument.
    'font_color' is an optional alias (takes precedence if provided).
    """
    effective_fc = font_color if font_color is not None else fc
    return {
        "width":        w,
        "height":       h,
        "font_size":    fs,
        "font_color":   effective_fc,
        "grid_color":   gc,
        "bg_color":     bg,
        "panel_color":  panel,
        "sig_color":    sig_col,
        "sug_color":    sug_col,
        "marker_size":  marker_size,
        "alpha":        alpha,
        "dot_color":    dot_col  or "#4E79A7",
        "line_color":   line_col or "#E15759",
        "fill_color":   fill_col or "#A78BFA",
        "axis_color":   axis_col or "#2D3142",
    }

# ─────────────────────── MANHATTAN PLOT ──────────────────────────────────────────
def plot_manhattan(pvals, chroms, positions, title="Manhattan Plot", model_name="",
                   cfg=None, top_n=15, lod_threshold=None,
                   pve_vals=None, pve_threshold=None, show_logp_axis=True):
    """
    LOD is the selection criterion for this pipeline, so the LEFT axis
    always shows the LOD scale. The RIGHT axis is an optional secondary
    -log10(p) scale (show_logp_axis toggles it on/off) -- both axes are
    exact monotonic transforms of the same underlying data
    (neglog10p_to_lod / lod_to_neglog10p), not independent scales.
    """
    if cfg is None:
        cfg = build_cfg(16, 5, 9, "#2D3142")

    n_snps = len(pvals)
    bonf_thresh = 0.05 / n_snps
    sug_thresh  = 1.0  / n_snps

    sig_line = -np.log10(bonf_thresh)
    sug_line = -np.log10(sug_thresh)

    fig, ax = plt.subplots(figsize=(cfg["width"], cfg["height"]))
    fig.patch.set_facecolor(cfg["bg_color"])
    ax.set_facecolor(cfg["panel_color"])

    log_p = -np.log10(np.clip(pvals, 1e-300, 1))

    chrom_list = sorted(set(chroms))
    chrom_colors = {c: CHR_PALETTE[i % len(CHR_PALETTE)] for i, c in enumerate(chrom_list)}
    x_offset = 0
    tick_pos, tick_lab, x_coords = [], [], np.zeros(len(pvals))
    gap = max(positions) * 0.018 + 1

    for ch in chrom_list:
        mask = chroms == ch
        pos = positions[mask]
        if len(pos) == 0: continue
        order = np.argsort(pos)
        x_vals = pos[order] - pos.min() + x_offset
        x_coords[np.where(mask)[0][order]] = x_vals
        tick_pos.append(x_vals.mean())
        tick_lab.append(f"Chr{ch}")
        ax.scatter(x_coords[mask], log_p[mask],
                   c=chrom_colors[ch], s=cfg["marker_size"],
                   alpha=cfg["alpha"], linewidths=0)
        x_offset += (pos.max() - pos.min()) + gap

    ax.axhline(sug_line, color=cfg["sug_color"], ls=":", lw=1.5, alpha=0.9,
               label=f"Suggestive (1/m = {sug_thresh:.1e}, -log₁₀={sug_line:.1f})")
    ax.axhline(sig_line, color=cfg["sig_color"], ls="--", lw=2.0,
               label=f"Bonferroni (0.05/m = {bonf_thresh:.1e}, -log₁₀={sig_line:.1f})")
    if lod_threshold is not None:
        lod_line_negp = lod_to_neglog10p(lod_threshold)
        ax.axhline(lod_line_negp, color=ACCENT_LIME, ls="-", lw=2.0, alpha=0.95,
                   label=f"Permutation / LOD threshold (LOD={lod_threshold:.2f})")

    # PRIMARY significance criterion: markers passing the LOD/permutation
    # threshold (optionally AND a minimum PVE%), not a blind top-N slice.
    # If no threshold could be computed, falls back to a small top-N so the
    # plot doesn't render empty.
    sig_idx = get_significant_idx(pvals, lod_threshold, fallback_top_n=top_n,
                                   pve_vals=pve_vals, pve_threshold=pve_threshold)
    sig_mask = np.zeros(len(pvals), dtype=bool)
    sig_mask[sig_idx] = True

    if sig_mask.any():
        crit_bits = []
        if lod_threshold is not None:
            crit_bits.append("LOD threshold")
        if pve_vals is not None and pve_threshold:
            crit_bits.append(f"PVE β‰₯ {pve_threshold:.1f}%")
        label = (f"{sig_mask.sum()} MTA(s) β‰₯ {' & '.join(crit_bits)}" if crit_bits
                 else f"Top {sig_mask.sum()} MTAs (no threshold available)")
        ax.scatter(x_coords[sig_mask], log_p[sig_mask],
                   c=ACCENT_GOLD, s=max(cfg["marker_size"]*4, 60),
                   marker="D", zorder=10, edgecolors="#333333", linewidth=0.7,
                   label=label)

    ax.set_xticks(tick_pos)
    ax.set_xticklabels(tick_lab, rotation=45,
                       fontsize=cfg["font_size"]-1, color=cfg["font_color"])
    ax.set_xlabel("Chromosome", color=cfg["font_color"], fontsize=cfg["font_size"])
    ax.set_title(f"{title}{' β€” ' + model_name if model_name else ''}",
                 color=cfg["font_color"], fontsize=cfg["font_size"]+2, pad=8)
    ax.legend(fontsize=cfg["font_size"]-1, framealpha=0.9,
              facecolor=cfg["bg_color"], edgecolor="#CCCCCC",
              labelcolor=cfg["font_color"], loc='upper right')
    ax.set_xlim(-x_offset * 0.01, x_offset * 1.01)
    for spine in ax.spines.values():
        spine.set_edgecolor("#CCCCCC")
    ax.grid(color=cfg["grid_color"], linewidth=0.4, linestyle="--", alpha=0.6)

    # ── LEFT axis: the data is plotted on the -log10(p) scale, but every
    # tick is *labeled* in LOD units via a direct formatter -- this always
    # renders (unlike a stacked secondary axis, which can silently collapse
    # to zero width under tight_layout). LOD and -log10(p) are exact
    # monotonic transforms of each other, so this is just a relabeling.
    ax.yaxis.set_major_formatter(
        plt.FuncFormatter(lambda val, pos: f"{neglog10p_to_lod(val):.1f}")
    )
    ax.set_ylabel("LOD score", color=cfg["font_color"], fontsize=cfg["font_size"])
    ax.tick_params(axis='y', colors=cfg["font_color"], labelsize=cfg["font_size"]-1)

    if show_logp_axis:
        ax_logp = ax.secondary_yaxis('right')
        ax_logp.set_ylabel("-log₁₀(p)", color=cfg["font_color"], fontsize=cfg["font_size"])
        ax_logp.tick_params(colors=cfg["font_color"], labelsize=cfg["font_size"]-1)
        ax_logp.spines["right"].set_edgecolor("#CCCCCC")

    fig.tight_layout()
    return fig


# ─────────────────────── QQ PLOT ─────────────────────────────────────────────────
def _nature_qq_ticks(max_val):
    """Pick clean, evenly-spaced tick values (Nature-style QQ plots use round
    steps like 0,3,6,9,12 rather than whatever matplotlib defaults to)."""
    if not np.isfinite(max_val) or max_val <= 0:
        return [0, 1]
    for step in (1, 2, 3, 5, 10, 15, 20, 25, 50, 100):
        n_ticks = max_val / step
        if n_ticks <= 5:
            top = int(np.ceil(max_val / step)) * step
            return list(np.arange(0, top + step, step))
    top = int(np.ceil(max_val / 100)) * 100
    return list(np.arange(0, top + 100, 100))


def plot_qq_single(pvals, title="QQ Plot", model_name="", color=None, cfg=None,
                    panel_letter=None):
    """Clean single-series QQ plot in a minimal Nature-journal style: plain
    white background, only left/bottom axis lines, a thin black y=x
    reference line, no gridlines, and a small unobtrusive lambda label."""
    if cfg is None:
        cfg = build_cfg(7, 7, 9, "#000000")
    if color is None:
        color = cfg.get("dot_color", "#3E7BD6")

    fig, ax = plt.subplots(figsize=(cfg["width"], cfg["height"]))
    fig.patch.set_facecolor("#FFFFFF")
    ax.set_facecolor("#FFFFFF")

    n = len(pvals)
    p_sorted = np.sort(np.clip(pvals, 1e-300, 1))
    obs = -np.log10(p_sorted)
    exp = -np.log10((np.arange(1, n + 1) - 0.5) / n)

    ax.scatter(exp, obs, c=color, s=max(cfg["marker_size"] * 0.6, 8),
               alpha=0.9, linewidths=0, zorder=5)

    axis_max = float(np.nanmax([exp.max() if n else 1, obs.max() if n else 1]))
    ticks = _nature_qq_ticks(axis_max)
    lim = ticks[-1]
    ax.plot([0, lim], [0, lim], color="#000000", lw=1.3, zorder=4, solid_capstyle="round")

    lam = np.median(stats.chi2.ppf(1 - np.clip(pvals, 1e-10, 1), df=1)) / stats.chi2.ppf(0.5, df=1)
    ax.text(0.97, 0.04, f"\u03bb = {lam:.3f}", transform=ax.transAxes,
            fontsize=cfg["font_size"] - 2, color="#888888", ha="right", va="bottom")

    ax.set_xlim(0, lim)
    ax.set_ylim(0, lim)
    ax.set_xticks(ticks)
    ax.set_yticks(ticks)
    ax.set_xlabel("Expected \u2212log$_{10}$($P$ value)", fontsize=cfg["font_size"] + 1,
                  color="#000000")
    ax.set_ylabel("Observed \u2212log$_{10}$($P$ value)", fontsize=cfg["font_size"] + 1,
                  color="#000000")
    if title or model_name:
        ax.set_title(f"{title}{' β€” ' + model_name if model_name else ''}",
                     color="#555555", fontsize=cfg["font_size"], pad=10)
    if panel_letter:
        ax.text(-0.16, 1.04, panel_letter, transform=ax.transAxes,
                fontsize=cfg["font_size"] + 8, fontweight="bold", color="#000000",
                ha="left", va="top")

    ax.tick_params(colors="#000000", labelsize=cfg["font_size"], length=4, width=1.1)
    for side in ("top", "right"):
        ax.spines[side].set_visible(False)
    for side in ("left", "bottom"):
        ax.spines[side].set_color("#000000")
        ax.spines[side].set_linewidth(1.2)
    ax.grid(False)
    fig.tight_layout()
    return fig


def plot_qq_all_models(all_results, cfg=None):
    """QQ plots for all models with regression lines."""
    if cfg is None:
        cfg = build_cfg(7, 6, 9, "#2D3142")
    n_models = len(all_results)
    ncols = min(3, n_models)
    nrows = (n_models + ncols - 1) // ncols
    fig, axes = plt.subplots(nrows, ncols,
                             figsize=(cfg["width"] * ncols, cfg["height"] * nrows))
    fig.patch.set_facecolor(cfg["bg_color"])

    if n_models == 1:
        axes = np.array([axes])
    axes = np.array(axes).flatten()

    for idx, (mname, (pvals, _, _)) in enumerate(all_results.items()):
        ax = axes[idx]
        ax.set_facecolor(cfg["panel_color"])
        n = len(pvals)
        p_sorted = np.sort(np.clip(pvals, 1e-300, 1))
        obs = -np.log10(p_sorted)
        exp = -np.log10((np.arange(1, n + 1) - 0.5) / n)
        ci_lo = -np.log10(stats.beta.ppf(0.975, np.arange(1, n+1), np.arange(n, 0, -1)))
        ci_hi = -np.log10(stats.beta.ppf(0.025, np.arange(1, n+1), np.arange(n, 0, -1)))
        col = MODEL_COLORS.get(mname, "#7C6FCD")

        ax.fill_between(exp, ci_lo, ci_hi, alpha=0.2, color=col)
        ax.scatter(exp, obs, c=col, s=cfg["marker_size"], alpha=cfg["alpha"],
                   linewidths=0, zorder=5)

        ax.plot([0, max(exp)], [0, max(exp)], color="#AAAAAA",
                ls="-", lw=1.0, alpha=0.6, zorder=4)

        valid = np.isfinite(exp) & np.isfinite(obs)
        if valid.sum() > 2:
            slope_reg, intercept_reg, r_val, _, _ = stats.linregress(exp[valid], obs[valid])
            x_reg = np.linspace(exp[valid].min(), exp[valid].max(), 200)
            y_reg = slope_reg * x_reg + intercept_reg
            ax.plot(x_reg, y_reg, color=cfg["sig_color"], ls="-", lw=2.0, zorder=6,
                    label=f"Reg slope={slope_reg:.2f}")
            ax.legend(fontsize=cfg["font_size"]-2, facecolor=cfg["bg_color"],
                      edgecolor="#CCCCCC", labelcolor=cfg["font_color"])

        lam = np.median(stats.chi2.ppf(1-np.clip(pvals,1e-10,1),df=1)) / stats.chi2.ppf(0.5, df=1)
        ax.text(0.05, 0.92, f"Ξ» = {lam:.4f}", transform=ax.transAxes,
                fontsize=cfg["font_size"]+1, color="#E15759", fontweight='bold')
        ax.set_xlabel("Expected -log₁₀(p)", fontsize=cfg["font_size"], color=cfg["font_color"])
        ax.set_ylabel("Observed -log₁₀(p)", fontsize=cfg["font_size"], color=cfg["font_color"])
        ax.set_title(f"QQ β€” {mname}", color=col, fontsize=cfg["font_size"]+1, fontweight='bold')
        ax.tick_params(colors=cfg["font_color"], labelsize=cfg["font_size"]-1)
        for spine in ax.spines.values():
            spine.set_edgecolor("#CCCCCC")
        ax.grid(color=cfg["grid_color"], linewidth=0.4, linestyle="--", alpha=0.6)

    for idx in range(n_models, len(axes)):
        axes[idx].set_visible(False)

    fig.suptitle("QQ Plots β€” All Models (with Regression Lines)", color=cfg["font_color"],
                 fontsize=cfg["font_size"]+3, y=1.01, fontweight='bold')
    fig.tight_layout()
    return fig


def plot_qq_overlay(all_results, cfg=None, panel_letter=None):
    """Mixed multi-model QQ overlay styled after the Nature-journal
    convention: each model is a dense small-dot curve in its own color,
    a single thin black y=x line, and colored text labels placed right at
    the end of each curve instead of a boxed legend."""
    if cfg is None:
        cfg = build_cfg(8, 8, 11, "#000000")
    fig, ax = plt.subplots(figsize=(cfg["width"], cfg["height"]))
    fig.patch.set_facecolor("#FFFFFF")
    ax.set_facecolor("#FFFFFF")

    curves = {}
    axis_max = 0.0
    for mname, (pvals, _, _) in all_results.items():
        n = len(pvals)
        if n == 0:
            continue
        p_sorted = np.sort(np.clip(pvals, 1e-300, 1))
        obs = -np.log10(p_sorted)
        exp = -np.log10((np.arange(1, n + 1) - 0.5) / n)
        curves[mname] = (exp, obs)
        axis_max = max(axis_max, float(np.nanmax(exp)), float(np.nanmax(obs)))

    ticks = _nature_qq_ticks(axis_max)
    lim = ticks[-1]
    ax.plot([0, lim], [0, lim], color="#000000", lw=1.3, zorder=3, solid_capstyle="round")

    label_positions = []  # track placed label y-positions (axes coords) to avoid overlap
    for mname, (exp, obs) in curves.items():
        col = MODEL_COLORS.get(mname, "#3E7BD6")
        ax.scatter(exp, obs, s=max(cfg["marker_size"] * 0.45, 6), color=col,
                   alpha=0.9, linewidths=0, zorder=5)

        # Place the model name at the end of its own curve (top-right-ish),
        # nudged vertically so labels for different models don't collide.
        end_x, end_y = exp[-1], obs[-1]
        y_frac = np.clip(end_y / lim, 0.05, 0.97)
        while any(abs(y_frac - p) < 0.055 for p in label_positions):
            y_frac += 0.06
        label_positions.append(y_frac)
        ax.text(0.99, y_frac, mname, transform=ax.transAxes, color=col,
                fontsize=cfg["font_size"] + 1, fontweight="bold",
                ha="right", va="center",
                path_effects=[pe.withStroke(linewidth=3, foreground="#FFFFFF")])

    ax.set_xlim(0, lim)
    ax.set_ylim(0, lim)
    ax.set_xticks(ticks)
    ax.set_yticks(ticks)
    ax.set_xlabel("Expected \u2212log$_{10}$($P$ value)", fontsize=cfg["font_size"] + 1,
                  color="#000000")
    ax.set_ylabel("Observed \u2212log$_{10}$($P$ value)", fontsize=cfg["font_size"] + 1,
                  color="#000000")
    if panel_letter:
        ax.text(-0.16, 1.04, panel_letter, transform=ax.transAxes,
                fontsize=cfg["font_size"] + 8, fontweight="bold", color="#000000",
                ha="left", va="top")

    ax.tick_params(colors="#000000", labelsize=cfg["font_size"], length=4, width=1.1)
    for side in ("top", "right"):
        ax.spines[side].set_visible(False)
    for side in ("left", "bottom"):
        ax.spines[side].set_color("#000000")
        ax.spines[side].set_linewidth(1.2)
    ax.grid(False)
    fig.tight_layout()
    return fig


# ─────────────────────── LOD / -log10(p) CONVERSION HELPERS ──────────────────────
def neglog10p_to_lod(neglog10p):
    """Convert a -log10(p) value (1 d.f. test) to its equivalent LOD score.

    LOD = chi2 / (2*ln(10)), where chi2 is the 1-d.f. chi-square value
    whose upper-tail p-value equals 10**(-neglog10p). This is the
    standard linkage/QTL LOD scale, so a p-based threshold can be read
    directly as a LOD threshold as well.
    """
    neglog10p = np.atleast_1d(np.asarray(neglog10p, dtype=float))
    p = np.clip(10.0 ** (-neglog10p), 1e-300, 1.0)
    chi2_val = stats.chi2.isf(p, df=1)
    lod = chi2_val / (2.0 * np.log(10.0))
    return lod if lod.shape != (1,) else lod[0]

def lod_to_neglog10p(lod):
    """Exact inverse of neglog10p_to_lod: given a LOD score, return the
    -log10(p) value it corresponds to, so a LOD threshold can be drawn on
    a -log10(p)-scaled Manhattan/Circos axis."""
    lod = np.atleast_1d(np.asarray(lod, dtype=float))
    chi2_val = lod * 2.0 * np.log(10.0)
    p = stats.chi2.sf(chi2_val, df=1)
    neglog10p = -np.log10(np.clip(p, 1e-300, 1.0))
    return neglog10p if neglog10p.shape != (1,) else neglog10p[0]


# ─────────────────────── LD DECAY ────────────────────────────────────────────────
def plot_ld_decay(G, positions, cfg=None):
    if cfg is None:
        cfg = build_cfg(16, 6, 9, "#2D3142")
    fig, axes = plt.subplots(1, 2, figsize=(cfg["width"], cfg["height"]))
    fig.patch.set_facecolor(cfg["bg_color"])

    m = G.shape[1]
    dists_bp, r2s = [], []
    n_pairs = min(80, m)
    for i in range(n_pairs):
        for j in range(i+1, n_pairs):
            xi, xj = G[:, i], G[:, j]
            mask = ~(np.isnan(xi) | np.isnan(xj))
            if mask.sum() < 4: continue
            r, _ = stats.pearsonr(xi[mask], xj[mask])
            d = abs(positions[j] - positions[i])
            dists_bp.append(d); r2s.append(r**2)

    if not dists_bp:
        for ax in axes:
            ax.set_facecolor(cfg["panel_color"])
            ax.text(0.5, 0.5, "Insufficient data", ha='center',
                    transform=ax.transAxes, color=cfg["font_color"])
        fig.tight_layout()
        return fig

    dists_bp = np.array(dists_bp)
    dists_mb = dists_bp / 1_000_000
    r2s = np.array(r2s)

    def bin_ld(dists, r2s, n_bins=20):
        bins = np.percentile(dists, np.linspace(0, 100, n_bins + 1))
        centers, means, sems = [], [], []
        for k in range(len(bins)-1):
            mask = (dists >= bins[k]) & (dists < bins[k+1])
            if mask.sum() > 2:
                centers.append((bins[k] + bins[k+1]) / 2)
                means.append(np.mean(r2s[mask]))
                sems.append(stats.sem(r2s[mask]))
        return np.array(centers), np.array(means), np.array(sems)

    for i, (dist_arr, xlabel, title_str, dot_col, line_col) in enumerate([
        (dists_bp, "Distance (bp)", "LD Decay β€” Base Pairs (bp)",
         cfg.get("dot_color","#4E79A7"), cfg.get("line_color","#E15759")),
        (dists_mb, "Distance (Mb)", "LD Decay β€” Megabases (Mb)", "#59A14F", "#F28E2B"),
    ]):
        ax = axes[i]
        ax.set_facecolor(cfg["panel_color"])
        ax.scatter(dist_arr, r2s, c=dot_col, s=cfg["marker_size"],
                   alpha=cfg["alpha"], linewidths=0)
        bc, bm, bs = bin_ld(dist_arr, r2s)
        if len(bc) > 0:
            ax.plot(bc, bm, color=line_col, lw=2.5, marker='o',
                    ms=5, zorder=5, label="Binned mean rΒ²")
            ax.fill_between(bc, bm - bs, bm + bs, alpha=0.18, color=line_col)
        ax.axhline(0.5, color="#EDC948", ls='--', lw=1.5, alpha=0.85, label="rΒ² = 0.5")
        ax.axhline(0.2, color="#B07AA1", ls=':', lw=1.2, alpha=0.75, label="rΒ² = 0.2")
        ax.set_xlabel(xlabel, fontsize=cfg["font_size"], color=cfg["font_color"])
        ax.set_ylabel("rΒ² (LD)", fontsize=cfg["font_size"], color=cfg["font_color"])
        ax.set_title(title_str, color=cfg["font_color"], fontsize=cfg["font_size"]+1,
                     fontweight='bold')
        ax.legend(fontsize=cfg["font_size"]-1, framealpha=0.85,
                  facecolor=cfg["bg_color"], edgecolor="#CCCCCC",
                  labelcolor=cfg["font_color"])
        ax.set_ylim(0, 1.05)
        ax.tick_params(colors=cfg["font_color"], labelsize=cfg["font_size"]-1)
        for spine in ax.spines.values(): spine.set_edgecolor("#CCCCCC")
        ax.grid(color=cfg["grid_color"], linewidth=0.4, linestyle="--", alpha=0.6)

    fig.suptitle("Linkage Disequilibrium (LD) Decay Analysis",
                 color=cfg["font_color"], fontsize=cfg["font_size"]+3,
                 fontweight='bold')
    fig.tight_layout()
    return fig


# ─────────────────────── PCA (LIGHT THEME) ───────────────────────────────────────
def plot_pca(G, y, ids, cfg=None):
    if cfg is None:
        cfg = build_cfg(14, 10, 9, TEXT_DARK,
                        gc=GRID_LIGHT, bg=BG_LIGHT, panel=PANEL_LIGHT,
                        sig_col=ACCENT_TEAL_D, sug_col=ACCENT_GOLD_D,
                        marker_size=55, alpha=0.80)

    bg_col    = cfg.get("bg_color",    BG_LIGHT)
    panel_col = cfg.get("panel_color", PANEL_LIGHT)
    fc        = cfg.get("font_color",  TEXT_DARK)
    gc        = cfg.get("grid_color",  GRID_LIGHT)
    fs        = cfg.get("font_size",   9)

    fig, axes = plt.subplots(1, 3, figsize=(cfg["width"], cfg["height"]))
    fig.patch.set_facecolor(bg_col)
    axes = axes.flatten()

    G_sc = StandardScaler().fit_transform(G)
    n_comp = min(5, G_sc.shape[0]-1, G_sc.shape[1])
    pca = PCA(n_components=n_comp)
    pcs = pca.fit_transform(G_sc)
    var_exp = pca.explained_variance_ratio_ * 100

    for ax in axes:
        ax.set_facecolor(panel_col)
        for spine in ax.spines.values():
            spine.set_edgecolor(GRID_LIGHT)
        ax.grid(color=gc, linewidth=0.5, linestyle="--", alpha=0.7)
        ax.tick_params(colors=fc, labelsize=fs-1)

    cmap = "viridis"
    sc = axes[0].scatter(pcs[:,0], pcs[:,1], c=y, cmap=cmap,
                         s=cfg["marker_size"], edgecolors='white', linewidths=0.4, alpha=cfg["alpha"])
    axes[0].set_xlabel(f"PC1 ({var_exp[0]:.1f}%)", fontsize=fs, color=fc)
    axes[0].set_ylabel(f"PC2 ({var_exp[1]:.1f}%)", fontsize=fs, color=fc)
    axes[0].set_title("PC1 vs PC2", color=ACCENT_TEAL_D, fontsize=fs+1, fontweight='bold')
    cbar = plt.colorbar(sc, ax=axes[0])
    cbar.set_label("Phenotype", color=fc, fontsize=fs-1)
    cbar.ax.tick_params(colors=fc)
    cbar.ax.yaxis.set_tick_params(color=fc)
    plt.setp(cbar.ax.yaxis.get_ticklabels(), color=fc)

    if n_comp >= 3:
        sc2 = axes[1].scatter(pcs[:,1], pcs[:,2], c=y, cmap=cmap,
                              s=cfg["marker_size"], edgecolors='white', linewidths=0.4, alpha=cfg["alpha"])
        axes[1].set_xlabel(f"PC2 ({var_exp[1]:.1f}%)", fontsize=fs, color=fc)
        axes[1].set_ylabel(f"PC3 ({var_exp[2]:.1f}%)", fontsize=fs, color=fc)
        axes[1].set_title("PC2 vs PC3", color=ACCENT_GOLD_D, fontsize=fs+1, fontweight='bold')
        cbar2 = plt.colorbar(sc2, ax=axes[1])
        cbar2.set_label("Phenotype", color=fc, fontsize=fs-1)
        cbar2.ax.tick_params(colors=fc)
        plt.setp(cbar2.ax.yaxis.get_ticklabels(), color=fc)
    else:
        axes[1].set_visible(False)

    sc3 = axes[2].scatter(pcs[:,0], pcs[:,1], c=y, cmap="plasma",
                          s=cfg["marker_size"], edgecolors='white', linewidths=0.4, alpha=cfg["alpha"])
    axes[2].set_title("Population Structure", color=ACCENT_PINK_D, fontsize=fs+1, fontweight='bold')
    axes[2].set_xlabel(f"PC1 ({var_exp[0]:.1f}%)", fontsize=fs, color=fc)
    axes[2].set_ylabel(f"PC2 ({var_exp[1]:.1f}%)", fontsize=fs, color=fc)
    cbar3 = plt.colorbar(sc3, ax=axes[2])
    cbar3.set_label("Phenotype", color=fc, fontsize=fs-1)
    cbar3.ax.tick_params(colors=fc)
    plt.setp(cbar3.ax.yaxis.get_ticklabels(), color=fc)

    fig.suptitle("Principal Component Analysis", color=TEXT_DARK,
                 fontsize=fs+3, fontweight='bold')
    fig.tight_layout()
    return fig


# ─────────────────────── MODEL COMPARISON MANHATTAN ──────────────────────────────
def plot_model_comparison_manhattan(all_results, chroms, positions, cfg=None, top_n=15, lod_threshold=None):
    if cfg is None:
        cfg = build_cfg(16, 3.5, 7, "#2D3142")
    n_models = len(all_results)
    fig, axes = plt.subplots(n_models, 1,
                             figsize=(cfg["width"], cfg["height"] * n_models),
                             sharex=True)
    fig.patch.set_facecolor(cfg["bg_color"])
    if n_models == 1:
        axes = [axes]

    chrom_list = sorted(set(chroms))
    total_len = 0
    chrom_offsets = {}
    for ch in chrom_list:
        chrom_offsets[ch] = total_len
        mask = chroms == ch
        if mask.sum() > 0:
            total_len += positions[mask].max() - positions[mask].min() + max(positions) * 0.02 + 1

    x_coords = np.zeros(len(chroms))
    for ch in chrom_list:
        mask = chroms == ch
        if mask.sum() > 0:
            x_coords[mask] = positions[mask] - positions[mask].min() + chrom_offsets[ch]

    tick_pos, tick_lab = [], []
    for ch in chrom_list:
        mask = chroms == ch
        if mask.sum() > 0:
            tick_pos.append(x_coords[mask].mean())
            tick_lab.append(f"Chr{ch}")

    for midx, (mname, (pvals, _, _)) in enumerate(all_results.items()):
        ax = axes[midx]
        ax.set_facecolor(cfg["panel_color"])
        log_p = -np.log10(np.clip(pvals, 1e-300, 1))
        col = MODEL_COLORS.get(mname, CHR_PALETTE[midx % len(CHR_PALETTE)])

        n_snps = len(pvals)
        bonf_thresh = 0.05 / n_snps
        sug_thresh  = 1.0  / n_snps

        for ch in chrom_list:
            mask = chroms == ch
            ax.scatter(x_coords[mask], log_p[mask],
                       c=col, s=cfg["marker_size"], alpha=cfg["alpha"], linewidths=0)

        sig_idx = get_significant_idx(pvals, lod_threshold, fallback_top_n=top_n)
        label = (f"MTA(s) β‰₯ LOD threshold ({len(sig_idx)})" if lod_threshold is not None
                 else f"Top {len(sig_idx)} (no threshold available)")
        if len(sig_idx) > 0:
            ax.scatter(x_coords[sig_idx], log_p[sig_idx],
                       c=ACCENT_GOLD, s=max(cfg["marker_size"]*4, 35),
                       zorder=10, linewidths=0.7, edgecolors='#333333',
                       marker='D', label=label)

        if lod_threshold is not None:
            ax.axhline(lod_to_neglog10p(lod_threshold), color=ACCENT_LIME,
                       ls='-', lw=1.6, alpha=0.95,
                       label=f"Permutation/LOD ({lod_threshold:.2f})")
        ax.axhline(-np.log10(sug_thresh), color=cfg["sug_color"],
                   ls=':', lw=1.2, alpha=0.9,
                   label=f"Sug. (1/m={sug_thresh:.1e})")
        ax.axhline(-np.log10(bonf_thresh), color=cfg["sig_color"],
                   ls='--', lw=1.5, alpha=0.9,
                   label=f"Bonf. (0.05/m={bonf_thresh:.1e})")

        ax.set_ylabel("-log₁₀(p)", fontsize=cfg["font_size"], color=cfg["font_color"])
        ax.set_title(f"Model: {mname}", color=col,
                     fontsize=cfg["font_size"]+1, pad=3, fontweight='bold')
        ax.set_xticks(tick_pos)
        ax.set_xticklabels(
            tick_lab if midx == n_models-1 else [],
            rotation=40, fontsize=cfg["font_size"]-1, color=cfg["font_color"]
        )
        ax.tick_params(colors=cfg["font_color"], labelsize=cfg["font_size"]-1)
        ax.legend(fontsize=cfg["font_size"]-2, framealpha=0.85,
                  facecolor=cfg["bg_color"], edgecolor="#CCCCCC",
                  labelcolor=cfg["font_color"], ncol=3)
        for spine in ax.spines.values(): spine.set_edgecolor("#CCCCCC")
        ax.grid(color=cfg["grid_color"], linewidth=0.3, linestyle="--", alpha=0.5)

    fig.suptitle("Manhattan Plots β€” All Models Comparison",
                 color=cfg["font_color"], fontsize=cfg["font_size"]+3,
                 fontweight='bold')
    fig.tight_layout(rect=[0, 0, 1, 0.98])
    return fig


# ─────────────────────── KINSHIP ─────────────────────────────────────────────────
def plot_kinship(K, ids, cfg=None):
    if cfg is None:
        cfg = build_cfg(14, 12, 9, "#2D3142")

    fig, axes = plt.subplots(2, 2, figsize=(cfg["width"], cfg["height"]))
    fig.patch.set_facecolor(cfg["bg_color"])
    axes = axes.flatten()
    for ax in axes:
        ax.set_facecolor(cfg["panel_color"])
        for spine in ax.spines.values(): spine.set_edgecolor("#CCCCCC")
        ax.tick_params(colors=cfg["font_color"], labelsize=cfg["font_size"]-1)

    im = axes[0].imshow(K, cmap='RdYlBu_r', aspect='auto', vmin=-0.5, vmax=1)
    axes[0].set_title("Kinship Matrix", color=cfg["font_color"],
                      fontsize=cfg["font_size"]+1, fontweight='bold')
    cbar = plt.colorbar(im, ax=axes[0])
    cbar.ax.tick_params(colors=cfg["font_color"])

    K_flat = K[np.triu_indices_from(K, k=1)]
    axes[1].hist(K_flat, bins=30, color=cfg.get("dot_color","#4E79A7"),
                 edgecolor='white', alpha=0.85)
    axes[1].set_title("Pairwise Kinship Distribution", color=cfg["font_color"],
                      fontsize=cfg["font_size"]+1, fontweight='bold')
    axes[1].set_xlabel("Kinship Coefficient", fontsize=cfg["font_size"],
                       color=cfg["font_color"])
    axes[1].set_ylabel("Count", fontsize=cfg["font_size"], color=cfg["font_color"])
    axes[1].grid(color=cfg["grid_color"], linewidth=0.4, linestyle="--", alpha=0.6)

    try:
        lm = linkage(K, method='ward')
        n_samples = len(ids)
        leaf_fs = max(3, min(cfg["font_size"]-1, int(200 / max(n_samples, 1))))
        truncate_mode = None
        p_param = None
        if n_samples > 50:
            truncate_mode = 'lastp'
            p_param = 30
            axes[2].set_title(
                f"Dendrogram (truncated, {n_samples} samples)",
                color=cfg["font_color"], fontsize=cfg["font_size"]+1, fontweight='bold'
            )
        else:
            axes[2].set_title("Clustering Dendrogram", color=cfg["font_color"],
                              fontsize=cfg["font_size"]+1, fontweight='bold')

        dend_kwargs = dict(
            ax=axes[2],
            leaf_rotation=90,
            leaf_font_size=leaf_fs,
            color_threshold=0.7 * max(lm[:, 2]),
            above_threshold_color="#AAAAAA"
        )
        if truncate_mode:
            dend_kwargs["truncate_mode"] = truncate_mode
            dend_kwargs["p"] = p_param
        else:
            short_labels = [str(x)[:10] for x in ids]
            dend_kwargs["labels"] = short_labels

        dendrogram(lm, **dend_kwargs)
        axes[2].tick_params(axis='x', labelsize=leaf_fs, colors=cfg["font_color"])
        axes[2].tick_params(axis='y', labelsize=cfg["font_size"]-2,
                            colors=cfg["font_color"])
        if n_samples > 50:
            axes[2].set_xticks([])
        axes[2].set_facecolor(cfg["panel_color"])
        for spine in axes[2].spines.values(): spine.set_edgecolor("#CCCCCC")
    except Exception as e:
        axes[2].text(0.5, 0.5, f"Dendrogram\nunavailable:\n{str(e)[:40]}",
                     ha='center', va='center', transform=axes[2].transAxes,
                     color=cfg["font_color"], fontsize=cfg["font_size"])

    eigvals, eigvecs = np.linalg.eigh(K)
    idx = np.argsort(eigvals)[::-1]
    axes[3].scatter(eigvecs[:,idx[0]], eigvecs[:,idx[1]],
                    c=cfg.get("dot_color","#4E79A7"), s=cfg["marker_size"],
                    edgecolors='none', alpha=cfg["alpha"])
    axes[3].set_title("Kinship PCA", color=cfg["font_color"],
                      fontsize=cfg["font_size"]+1, fontweight='bold')
    axes[3].set_xlabel("PC1", fontsize=cfg["font_size"], color=cfg["font_color"])
    axes[3].set_ylabel("PC2", fontsize=cfg["font_size"], color=cfg["font_color"])
    axes[3].grid(color=cfg["grid_color"], linewidth=0.4, linestyle="--", alpha=0.6)

    fig.suptitle("Kinship Matrix & Clustering Analysis",
                 color=cfg["font_color"], fontsize=cfg["font_size"]+3, fontweight='bold')
    fig.tight_layout()
    return fig


# ─────────────────────── EFFECT SIZES ────────────────────────────────────────────
def plot_effect_sizes(pvals, betas, snp_names, cfg=None, top_n=15, lod_threshold=None,
                       pve_vals=None, pve_threshold=None):
    if cfg is None:
        cfg = build_cfg(14, 10, 9, "#2D3142")

    fig, axes = plt.subplots(2, 2, figsize=(cfg["width"], cfg["height"]))
    fig.patch.set_facecolor(cfg["bg_color"])
    axes = axes.flatten()
    for ax in axes:
        ax.set_facecolor(cfg["panel_color"])
        for spine in ax.spines.values(): spine.set_edgecolor("#CCCCCC")
        ax.tick_params(colors=cfg["font_color"], labelsize=cfg["font_size"]-1)
        ax.grid(color=cfg["grid_color"], linewidth=0.4, linestyle="--", alpha=0.6)

    top_idx = get_significant_idx(pvals, lod_threshold, fallback_top_n=top_n,
                                   pve_vals=pve_vals, pve_threshold=pve_threshold)
    n_shown = len(top_idx)
    top_betas = betas[top_idx]
    top_names = [str(snp_names[i])[:18] for i in top_idx]
    pos_col = cfg.get("dot_color", "#4E79A7")
    neg_col = cfg.get("line_color", "#E15759")
    colors = [neg_col if b < 0 else pos_col for b in top_betas]

    order = np.argsort(top_betas)
    axes[0].barh([top_names[o] for o in order], [top_betas[o] for o in order],
                 color=[colors[o] for o in order], edgecolor='white', linewidth=0.4)
    axes[0].axvline(0, color=cfg["font_color"], lw=0.9, alpha=0.6)
    axes[0].set_xlabel("Effect Size (Ξ²)", fontsize=cfg["font_size"],
                       color=cfg["font_color"])
    axes[0].set_title(
        f"Effect Sizes β€” {n_shown} MTA(s) β‰₯ LOD threshold" if lod_threshold is not None
        else f"Top {n_shown} Effect Sizes (no threshold available)",
        color=cfg["font_color"], fontsize=cfg["font_size"]+1, fontweight='bold')
    axes[0].tick_params(axis='y', labelsize=max(5, cfg["font_size"]-2))
    if n_shown == 0:
        axes[0].text(0.5, 0.5, "No MTAs passed the\npermutation/LOD threshold",
                    ha='center', va='center', transform=axes[0].transAxes,
                    color=cfg["font_color"], fontsize=cfg["font_size"])

    log_p = -np.log10(np.clip(pvals, 1e-300, 1))
    sig_thr = -np.log10(0.05/len(pvals))
    pt_colors = [neg_col if lp >= sig_thr else (pos_col if b > 0 else "#59A14F")
                 for lp, b in zip(log_p, betas)]
    axes[1].scatter(betas, log_p, c=pt_colors, s=cfg["marker_size"],
                    alpha=cfg["alpha"], linewidths=0)
    axes[1].axhline(sig_thr, color=cfg["sig_color"], ls='--', lw=1.8, label="Bonferroni")
    if lod_threshold is not None:
        axes[1].axhline(lod_to_neglog10p(lod_threshold), color=ACCENT_LIME,
                        ls='-', lw=1.8, label=f"Permutation/LOD ({lod_threshold:.2f})")
    axes[1].axvline(0, color=cfg["font_color"], lw=0.6, alpha=0.5)
    axes[1].set_xlabel("Ξ²", fontsize=cfg["font_size"], color=cfg["font_color"])
    axes[1].set_ylabel("-log₁₀(p)", fontsize=cfg["font_size"], color=cfg["font_color"])
    axes[1].set_title("Volcano Plot", color=cfg["font_color"],
                      fontsize=cfg["font_size"]+1, fontweight='bold')
    axes[1].legend(fontsize=cfg["font_size"]-1, facecolor=cfg["bg_color"],
                   edgecolor="#CCCCCC", labelcolor=cfg["font_color"])

    axes[2].hist(betas, bins=25, color=cfg.get("fill_color","#A78BFA"),
                 edgecolor='white', alpha=0.85)
    axes[2].axvline(0, color=cfg["sig_color"], lw=2, ls='--')
    axes[2].set_xlabel("Ξ²", fontsize=cfg["font_size"], color=cfg["font_color"])
    axes[2].set_title("Effect Size Distribution", color=cfg["font_color"],
                      fontsize=cfg["font_size"]+1, fontweight='bold')
    axes[2].set_ylabel("Count", fontsize=cfg["font_size"], color=cfg["font_color"])

    axes[3].scatter(betas, log_p, c=pt_colors, s=cfg["marker_size"],
                    alpha=cfg["alpha"]*0.85, linewidths=0)
    label_idx = top_idx[:min(8, len(top_idx))]
    for ti in label_idx:
        axes[3].annotate(str(snp_names[ti])[:12],
                         (betas[ti], log_p[ti]),
                         fontsize=max(5, cfg["font_size"]-3),
                         color=cfg["font_color"], alpha=0.8,
                         xytext=(5, 3), textcoords='offset points',
                         arrowprops=dict(arrowstyle='->', color='gray', lw=0.6))
    axes[3].set_xlabel("Ξ²", fontsize=cfg["font_size"], color=cfg["font_color"])
    axes[3].set_ylabel("-log₁₀(p)", fontsize=cfg["font_size"], color=cfg["font_color"])
    axes[3].set_title("Effect Size vs Significance (annotated)",
                      color=cfg["font_color"], fontsize=cfg["font_size"]+1, fontweight='bold')

    fig.suptitle("Effect Size Analysis", color=cfg["font_color"],
                 fontsize=cfg["font_size"]+3, fontweight='bold')
    fig.tight_layout()
    return fig


# ─────────────────────── SUMMARY DASHBOARD ───────────────────────────────────────
def plot_summary_dashboard(y, maf, all_results, G_filt, cfg=None):
    if cfg is None:
        cfg = build_cfg(15, 10, 9, "#2D3142")

    fig, axes = plt.subplots(2, 3, figsize=(cfg["width"], cfg["height"]))
    fig.patch.set_facecolor(cfg["bg_color"])
    axes = axes.flatten()
    for ax in axes:
        ax.set_facecolor(cfg["panel_color"])
        for spine in ax.spines.values(): spine.set_edgecolor("#CCCCCC")
        ax.tick_params(colors=cfg["font_color"], labelsize=cfg["font_size"]-1)
        ax.grid(color=cfg["grid_color"], linewidth=0.4, linestyle="--", alpha=0.6)

    # Panel 0: Phenotype distribution
    axes[0].hist(y, bins=15, color=cfg.get("dot_color","#4E79A7"),
                 edgecolor='white', alpha=0.85)
    axes[0].axvline(y.mean(), color=cfg["sig_color"], lw=2, ls='--',
                    label=f"Mean: {y.mean():.2f}")
    axes[0].set_title(f"Phenotype Distribution | n={len(y)}",
                      color=cfg["font_color"], fontsize=cfg["font_size"]+1, fontweight='bold')
    axes[0].legend(fontsize=cfg["font_size"]-1, facecolor=cfg["bg_color"],
                   edgecolor="#CCCCCC", labelcolor=cfg["font_color"])
    axes[0].set_xlabel("Phenotype Value", fontsize=cfg["font_size"], color=cfg["font_color"])
    axes[0].set_ylabel("Count", fontsize=cfg["font_size"], color=cfg["font_color"])

    # Panel 1: MAF distribution
    axes[1].hist(maf, bins=20, color="#59A14F", edgecolor='white', alpha=0.85)
    axes[1].axvline(0.05, color=cfg["sig_color"], ls='--', lw=1.5,
                    label=f"MAF=0.05 (rare: {np.sum(maf<0.05)})")
    axes[1].set_title("Minor Allele Frequency",
                      color=cfg["font_color"], fontsize=cfg["font_size"]+1, fontweight='bold')
    axes[1].legend(fontsize=cfg["font_size"]-1, facecolor=cfg["bg_color"],
                   edgecolor="#CCCCCC", labelcolor=cfg["font_color"])
    axes[1].set_xlabel("MAF", fontsize=cfg["font_size"], color=cfg["font_color"])
    axes[1].set_ylabel("Count", fontsize=cfg["font_size"], color=cfg["font_color"])

    # Panel 2: Genomic inflation per model
    lambdas = {}
    for mname, (p, _, _) in all_results.items():
        lam = np.median(stats.chi2.ppf(1-np.clip(p,1e-10,1),df=1)) / stats.chi2.ppf(0.5,df=1)
        lambdas[mname] = lam
    lam_cols = ["#59A14F" if v<=1.1 else "#F28E2B" if v<=1.3 else "#E15759"
                for v in lambdas.values()]
    bars = axes[2].bar(lambdas.keys(), lambdas.values(),
                       color=lam_cols, edgecolor='white', linewidth=0.5)
    axes[2].axhline(1.0, color=cfg["font_color"], ls='--', lw=1, alpha=0.5)
    axes[2].set_title("Genomic Inflation Ξ» per Model",
                      color=cfg["font_color"], fontsize=cfg["font_size"]+1, fontweight='bold')
    axes[2].tick_params(axis='x', rotation=30, labelsize=cfg["font_size"])
    axes[2].set_ylabel("Ξ»", fontsize=cfg["font_size"], color=cfg["font_color"])
    for bar, lv in zip(bars, lambdas.values()):
        axes[2].text(bar.get_x() + bar.get_width()/2, bar.get_height() + 0.01,
                     f"{lv:.3f}", ha='center', fontsize=cfg["font_size"]-2,
                     color=cfg["font_color"], va='bottom')

    # Panel 3: Significant SNPs (Bonferroni)
    sig_counts = {}
    for mname, (p, _, _) in all_results.items():
        bonf = 0.05 / len(p)
        sig_counts[mname] = int((p < bonf).sum())
    cols_bar = [MODEL_COLORS.get(m, "#4E79A7") for m in sig_counts.keys()]
    axes[3].bar(sig_counts.keys(), sig_counts.values(),
                color=cols_bar, edgecolor='white', linewidth=0.5)
    axes[3].set_title("Significant SNPs (Bonferroni)",
                      color=cfg["font_color"], fontsize=cfg["font_size"]+1, fontweight='bold')
    axes[3].tick_params(axis='x', rotation=30, labelsize=cfg["font_size"])
    axes[3].set_ylabel("Count", fontsize=cfg["font_size"], color=cfg["font_color"])

    # Panel 4: Model concordance heatmap
    n_m = len(all_results)
    model_names = list(all_results.keys())
    concordance = np.eye(n_m)
    for i in range(n_m):
        for j in range(i+1, n_m):
            lp_i = -np.log10(np.clip(all_results[model_names[i]][0], 1e-300, 1))
            lp_j = -np.log10(np.clip(all_results[model_names[j]][0], 1e-300, 1))
            r, _ = stats.spearmanr(lp_i, lp_j)
            concordance[i,j] = concordance[j,i] = r
    im = axes[4].imshow(concordance, cmap='RdYlGn', vmin=-1, vmax=1, aspect='auto')
    axes[4].set_xticks(range(n_m)); axes[4].set_yticks(range(n_m))
    axes[4].set_xticklabels(model_names, rotation=35,
                             fontsize=cfg["font_size"]-1, color=cfg["font_color"])
    axes[4].set_yticklabels(model_names, fontsize=cfg["font_size"]-1,
                             color=cfg["font_color"])
    for i in range(n_m):
        for j in range(n_m):
            axes[4].text(j, i, f"{concordance[i,j]:.2f}",
                         ha='center', va='center',
                         fontsize=cfg["font_size"]-1,
                         color='#111111' if abs(concordance[i,j])<0.7 else '#FFFFFF')
    cbar2 = plt.colorbar(im, ax=axes[4])
    cbar2.ax.tick_params(colors=cfg["font_color"])
    axes[4].set_title("Model Concordance (Spearman ρ)",
                      color=cfg["font_color"], fontsize=cfg["font_size"]+1, fontweight='bold')

    # Panel 5: Threshold table per model (replaces scree plot)
    axes[5].axis('off')
    thresh_lines = [
        f"{'Model':<10} {'Bonferroni':>14} {'Suggestive':>14}",
        "─" * 42,
    ]
    for mname, (p, _, _) in all_results.items():
        n_snps = len(p)
        b_thr = 0.05 / n_snps
        s_thr = 1.0  / n_snps
        thresh_lines.append(
            f"{mname:<10} {b_thr:>14.3e} {s_thr:>14.3e}"
        )
    thresh_lines.append("─" * 42)
    thresh_lines.append(f"  (Bonferroni = 0.05 / m)")
    thresh_lines.append(f"  (Suggestive =  1.0 / m)")

    summary_text = (
        f"GWAS Summary\n\n"
        f"Samples:       {G_filt.shape[0]}\n"
        f"SNPs (filt.):  {G_filt.shape[1]}\n"
        f"Models run:    {n_m}\n"
        f"Missing rate:  {np.isnan(G_filt).mean():.1%}\n\n"
        + "\n".join(thresh_lines)
    )
    axes[5].text(0.04, 0.96, summary_text,
                 ha='left', va='top', transform=axes[5].transAxes,
                 color=cfg["font_color"], fontsize=cfg["font_size"]-1,
                 fontfamily='monospace',
                 bbox=dict(boxstyle='round,pad=0.8', facecolor=cfg["panel_color"],
                           edgecolor="#CCCCCC", alpha=0.9))
    axes[5].set_title("Pipeline Summary + Thresholds", color=cfg["font_color"],
                      fontsize=cfg["font_size"]+1, fontweight='bold')

    fig.suptitle("GWAS Analysis Dashboard",
                 color=cfg["font_color"], fontsize=cfg["font_size"]+4, fontweight='bold')
    fig.tight_layout()
    return fig


# ─────────────────────── CIRCOS β€” DENSITY RING (pyCirclize) ──────────────────────
# Rebuilt on pyCirclize (github.com/moshi4/pyCirclize) instead of hand-rolled
# matplotlib polar geometry. The old approach manually computed every radian,
# bin edge, and fill polygon, which is exactly what produced the stray-line
# rendering artifact (a degenerate fill polygon collapsing into a spike).
# pyCirclize handles sector sizing, tracks, ticks, and heatmaps internally,
# so that whole class of bug is structurally no longer possible here.
CIRCOS_THEMES = {
    "Bright": {
        "bg": "#FFFFFF", "text": "#2B2B2B", "grid": "#DADADA",
        "chr_border": "#3A3A3A", "sig": "#C0392B", "star_edge": "#6B1A12",
        "track_colors": ["#2F6F9F", "#D98324", "#3E8E7E", "#8B5FBF", "#B5541A"],
        "density_stops": ["#F2F2F2", "#7FB77E", "#F4D35E", "#EE964B", "#C0392B"],
    },
    "Dark": {
        "bg": "#12141F", "text": "#F1F3FA", "grid": "#333A52",
        "chr_border": "#AEB6D6", "sig": "#FF6B6B", "star_edge": "#FFD9D9",
        "track_colors": ["#FF6B5B", "#FFD166", "#4FE0CB", "#D9A6F0", "#F2946B"],
        "density_stops": ["#2A2E45", "#3EC46D", "#F4E04D", "#FFA53E", "#FF4D4D"],
    },
}


def plot_circos_density(all_results, chroms, positions, ring_names, theme_name="Bright",
                        rings_per_panel=3, cfg=None, lod_threshold=None):
    """Build a single publication-quality circular GWAS figure (pyCirclize).

    Kept the original call signature (all_results, chroms, positions, ring_names,
    theme_name, rings_per_panel, cfg, lod_threshold) so run_gwas()'s call site and
    the "All Plots as PNG (ZIP)" export need no other changes.

    `rings_per_panel` is accepted for backward compatibility with the Gradio
    slider, but no longer splits the figure into an A/B/C/D subplot grid: all
    requested rings are drawn as concentric tracks in one circle, which is the
    standard circlize/Circos convention for a submission-ready figure. If you
    pass many models at once, consider trimming `ring_names` (the "Circos
    models" selector in the UI) rather than relying on paneling to declutter.
    """
    theme = CIRCOS_THEMES.get(theme_name, CIRCOS_THEMES["Bright"])
    fig_size = (cfg.get("width", 20) if cfg else 20) / 1.8
    font_size = cfg.get("font_size", 11) if cfg else 11
    density_bins_per_chr = 60
    manhattan_bins_per_chr = 200

    dens_cmap = LinearSegmentedColormap.from_list("density", theme["density_stops"], N=256)

    chroms = np.asarray(chroms)
    positions = np.asarray(positions, dtype=float)
    chr_list = sorted(set(int(c) for c in chroms))

    # Normalize each chromosome's positions to start at 0 (subtract its own
    # min), exactly like plot_manhattan() does. Without this, any position
    # source that doesn't start near 0 for every chromosome (e.g. the
    # per-chromosome fallback index, or real bp coordinates whose first
    # marker isn't near the telomere) makes chr_lengths[c] (= raw max)
    # larger than the marker's true span, leaving a genuinely empty arc at
    # the start of that sector.
    chr_min = {c: float(np.min(positions[chroms == c])) for c in chr_list}
    positions = positions.copy()
    for c in chr_list:
        mask = chroms == c
        positions[mask] = positions[mask] - chr_min[c]

    chr_lengths = {c: max(float(np.max(positions[chroms == c])), 1.0) for c in chr_list}
    sectors = {str(c): chr_lengths[c] for c in chr_list}

    circos = Circos(sectors, space=2.0)

    # ── Track 1: chromosome ideogram (outermost) ───────────────────────
    for sector in circos.sectors:
        c = int(sector.name)
        ideogram = sector.add_track((97, 100))
        ideogram.axis(fc=theme["bg"], ec=theme["chr_border"], lw=1.0)
        ideogram.text(f"{c}", size=font_size + 1, color=theme["text"], weight="bold")
        span_mb = chr_lengths[c] / 1e6
        interval = 50 if span_mb > 150 else (20 if span_mb > 50 else 10)
        ideogram.xticks_by_interval(
            interval * 1e6,
            label_formatter=lambda v: f"{v / 1e6:.0f}",
            label_size=font_size - 4, label_orientation="vertical", tick_length=1.0,
            line_kws=dict(color=theme["grid"], lw=0.6), text_kws=dict(color=theme["text"]),
        )

    # ── Track 2: SNP density heatmap ────────────────────────────────────
    dens_vals_all = []
    for sector in circos.sectors:
        c = int(sector.name)
        p = positions[chroms == c]
        bins = np.linspace(0, chr_lengths[c], density_bins_per_chr + 1)
        counts, _ = np.histogram(p, bins=bins)
        dens_vals_all.append(counts)
    dens_vmax = max((int(np.max(c)) if len(c) else 1 for c in dens_vals_all), default=1)
    dens_vmax = max(dens_vmax, 1)

    for sector, counts in zip(circos.sectors, dens_vals_all):
        dtrack = sector.add_track((90, 96))
        dtrack.axis(fc=theme["bg"], ec=theme["chr_border"], lw=0.6)
        dtrack.heatmap(counts.reshape(1, -1), vmin=0, vmax=dens_vmax, cmap=dens_cmap,
                       rect_kws=dict(ec="none"))

    # ── Tracks 3..N: one Manhattan ring per model ───────────────────────
    n_rings = max(len(ring_names), 1)
    ring_span, ring_gap = 82.0, 3.0
    each = (ring_span - ring_gap * (n_rings - 1)) / n_rings
    top_r = 90.0

    legend_handles = []
    for ridx, name in enumerate(ring_names):
        if name not in all_results:
            continue
        pvals = np.asarray(all_results[name][0])
        color = theme["track_colors"][ridx % len(theme["track_colors"])]
        log_p = -np.log10(np.clip(pvals, 1e-300, 1))
        lod_disp = lod_threshold if lod_threshold is not None else float(np.nanpercentile(log_p, 99.5))
        y_max = max(np.ceil(max(float(np.nanmax(log_p)), lod_disp + 1)), lod_disp + 1)

        r_top = top_r - ridx * (each + ring_gap)
        r_bot = r_top - each

        for si, sector in enumerate(circos.sectors):
            c = int(sector.name)
            idx = np.where(chroms == c)[0]
            if idx.size == 0:
                continue
            p_ch, lp_ch = positions[idx], log_p[idx]

            track = sector.add_track((r_bot, r_top))
            track.axis(fc=theme["bg"], ec=theme["grid"], lw=0.4)
            track.grid(y_grid_num=3, color=theme["grid"], lw=0.4, ls=(0, (1, 2)))

            if si == 0:
                track.yticks(
                    [0, round(lod_disp, 1), round(y_max)], vmin=0, vmax=y_max,
                    side="left", tick_length=1.2, label_size=font_size - 4.5,
                    label_margin=1.0, text_kws=dict(color=theme["text"]),
                    line_kws=dict(color=theme["grid"], lw=0.5),
                )

            n_bins = max(int(manhattan_bins_per_chr * (chr_lengths[c] / max(chr_lengths.values()))), 8)
            edges = np.linspace(0, chr_lengths[c], n_bins + 1)
            centers = (edges[:-1] + edges[1:]) / 2
            bin_idx = np.clip(np.digitize(p_ch, edges) - 1, 0, n_bins - 1)
            bin_max = np.zeros(n_bins)
            for bi in range(n_bins):
                sel = bin_idx == bi
                if sel.any():
                    bin_max[bi] = lp_ch[sel].max()

            has_signal = bin_max > 0
            if has_signal.any():
                track.bar(centers[has_signal], bin_max[has_signal],
                          width=(edges[1] - edges[0]) * 0.95, vmin=0, vmax=y_max,
                          color=color, ec="none")

            track.line([0, chr_lengths[c]], [lod_disp, lod_disp], vmin=0, vmax=y_max,
                      color=theme["sig"], lw=1.0, ls=(0, (1.5, 1.5)))

            passes = lp_ch >= lod_disp
            if passes.any():
                track.scatter(p_ch[passes], lp_ch[passes], vmin=0, vmax=y_max,
                             marker="*", s=28, color=theme["sig"],
                             ec=theme["star_edge"], lw=0.4, zorder=5)

        legend_handles.append(Patch(facecolor=color, edgecolor="none", label=name))

    legend_handles.append(Line2D([0], [0], color=theme["sig"], lw=1.3, ls=(0, (1.5, 1.5)),
                                  label="Significance threshold"))

    fig = circos.plotfig(figsize=(fig_size, fig_size))
    fig.patch.set_facecolor(theme["bg"])

    circos.colorbar(bounds=(1.00, 0.72, 0.02, 0.22), vmin=0, vmax=dens_vmax, cmap=dens_cmap,
                     label="SNP density\n(count / bin)",
                     label_kws=dict(size=font_size - 2, color=theme["text"]),
                     tick_kws=dict(labelsize=font_size - 3, colors=theme["text"]))

    fig.legend(handles=legend_handles, loc="center left", bbox_to_anchor=(0.98, 0.35),
               frameon=False, fontsize=font_size - 1, labelcolor=theme["text"],
               handlelength=1.4, borderaxespad=0.0, title="Tracks", title_fontsize=font_size)

    return fig




# ─────────────────────── EXCEL EXPORT ────────────────────────────────────────────
def export_results_excel(all_results, snp_filt, chr_filt, pos_filt, y,
                         G_filt, maf_vals, output_path, top_n=15, lod_threshold=None,
                         pve_vals=None, pve_threshold=None, K=None):
    # Single, model-agnostic PVE% array (RSS-reduction / SS_total, kinship-
    # aware β€” see compute_pve_all) reused for every sheet below instead of
    # recomputing a different (and previously inconsistent) r^2-based PVE
    # per model/per sheet.
    if pve_vals is None:
        pve_vals = compute_pve_all(y, G_filt, K=K)
    wb = Workbook()
    header_fill = PatternFill("solid", start_color="4A90D9", end_color="4A90D9")
    sig_fill    = PatternFill("solid", start_color="FFF3CD", end_color="FFF3CD")
    alt_fill    = PatternFill("solid", start_color="F8F9FA", end_color="F8F9FA")
    header_font = Font(bold=True, color="FFFFFF", name="Calibri", size=11)
    title_font  = Font(bold=True, color="2C5F8A", name="Calibri", size=13)
    thin_border = Border(
        left=Side(style='thin', color='CCCCCC'),
        right=Side(style='thin', color='CCCCCC'),
        top=Side(style='thin', color='CCCCCC'),
        bottom=Side(style='thin', color='CCCCCC')
    )
    bonf = 0.05 / len(list(all_results.values())[0][0])

    # Sheet 1: Summary
    ws_sum = wb.active
    ws_sum.title = "Summary"
    ws_sum['A1'] = f"GWAS Complete Analysis Summary β€” Top {top_n} MTAs per Model"
    ws_sum['A1'].font = Font(bold=True, color="2C5F8A", name="Calibri", size=16)
    ws_sum.merge_cells('A1:H1')
    headers_sum = ["Model", "N_SNPs_Tested", "Bonferroni_Threshold",
                   "Suggestive_Threshold","Sig_SNPs_Bonferroni",
                   "Sig_SNPs_Suggestive", "Genomic_Lambda", "Min_Pvalue"]
    for col, h in enumerate(headers_sum, 1):
        cell = ws_sum.cell(row=3, column=col, value=h)
        cell.font = header_font; cell.fill = header_fill
        cell.alignment = Alignment(horizontal='center'); cell.border = thin_border
        ws_sum.column_dimensions[get_column_letter(col)].width = 24
    for row_i, (mname, (pvals, betas, ses)) in enumerate(all_results.items(), 4):
        lam = np.median(stats.chi2.ppf(1-np.clip(pvals,1e-10,1),df=1)) / stats.chi2.ppf(0.5,df=1)
        n_snps = len(pvals)
        bonf_t = 0.05 / n_snps
        sug_t  = 1.0  / n_snps
        sig_bonf = int((pvals < bonf_t).sum())
        sig_sug  = int((pvals < sug_t).sum())
        row_data = [mname, n_snps, f"{bonf_t:.2e}", f"{sug_t:.2e}",
                    sig_bonf, sig_sug, round(float(lam),4), float(np.min(pvals))]
        fill = alt_fill if row_i%2==0 else PatternFill("solid",start_color="FFFFFF",end_color="FFFFFF")
        for col, val in enumerate(row_data, 1):
            cell = ws_sum.cell(row=row_i, column=col, value=val)
            cell.fill = fill; cell.border = thin_border
            cell.alignment = Alignment(horizontal='center')

    # Threshold explanation sheet
    ws_exp = wb.create_sheet(title="Threshold_Explanation")
    ws_exp['A1'] = "Threshold Calculation Explanation"
    ws_exp['A1'].font = title_font
    ws_exp.merge_cells('A1:C1')
    explanation_rows = [
        ("Bonferroni Threshold", "Formula", "0.05 / m  where m = number of SNPs tested"),
        ("Bonferroni Threshold", "Rationale", "Controls genome-wide Type I error rate at 5%."),
        ("Bonferroni Threshold", "Assumption", "All SNP tests are independent (conservative)."),
        ("Suggestive Threshold", "Formula", "1 / m  where m = number of SNPs tested"),
        ("Suggestive Threshold", "Rationale", "Expects ~1 false positive per genome scan."),
        ("Suggestive Threshold", "Reference", "Lander & Kruglyak (1995) Nature Genetics."),
        ("OLS Note", "Correction", "No population structure correction."),
        ("MLM Note", "Correction", "Kinship matrix controls for relatedness."),
        ("EMMAX Note", "Correction", "Efficient Mixed Model Association with variance components."),
        ("FarmCPU Note", "Correction", "Iterative pseudo-QTN control reduces false positives."),
        ("GEMMA Note", "Correction", "Genomic control (GC correction) applied for inflation."),
    ]
    ws_exp.cell(row=3, column=1, value="Threshold").font = header_font
    ws_exp.cell(row=3, column=1).fill = header_fill
    ws_exp.cell(row=3, column=2, value="Aspect").font = header_font
    ws_exp.cell(row=3, column=2).fill = header_fill
    ws_exp.cell(row=3, column=3, value="Details").font = header_font
    ws_exp.cell(row=3, column=3).fill = header_fill
    ws_exp.column_dimensions['A'].width = 22
    ws_exp.column_dimensions['B'].width = 16
    ws_exp.column_dimensions['C'].width = 60
    for r_i, (t, a, d) in enumerate(explanation_rows, 4):
        ws_exp.cell(row=r_i, column=1, value=t).border = thin_border
        ws_exp.cell(row=r_i, column=2, value=a).border = thin_border
        ws_exp.cell(row=r_i, column=3, value=d).border = thin_border

    # Sheet per model: MTAs passing the permutation/LOD threshold
    for mname, (pvals, betas, ses) in all_results.items():
        sig_idx_sheet = get_significant_idx(pvals, lod_threshold, fallback_top_n=top_n,
                                             pve_vals=pve_vals, pve_threshold=pve_threshold)
        n_sig = len(sig_idx_sheet)
        sheet_label = f"MTA(LOD)" if lod_threshold is not None else f"Top{top_n}"
        ws = wb.create_sheet(title=f"{mname}_{sheet_label}"[:31])
        title_txt = (f"GWAS Results β€” {mname} Model β€” {n_sig} MTA(s) passing "
                    f"permutation/LOD threshold (LOD β‰₯ {lod_threshold:.2f})" if lod_threshold is not None
                    else f"GWAS Results β€” {mname} Model β€” Top {top_n} MTAs (no LOD threshold available)")
        ws['A1'] = title_txt
        ws['A1'].font = title_font
        ws.merge_cells('A1:K1')
        log_p = -np.log10(np.clip(pvals, 1e-300, 1))
        t_stats = np.where(ses > 0, betas / (ses + 1e-10), 0.0)
        reject_fdr, pvals_fdr, _, _ = multipletests(pvals, method='fdr_bh')
        reject_bonf, _, _, _ = multipletests(pvals, method='bonferroni')
        headers = ["Rank","SNP","Chr","Position","MAF","Beta","SE",
                   "T_statistic","P_value","neg_log10P","LOD","FDR_BH","Bonferroni_sig"]
        for col, h in enumerate(headers, 1):
            cell = ws.cell(row=3, column=col, value=h)
            cell.font = header_font; cell.fill = header_fill
            cell.alignment = Alignment(horizontal='center'); cell.border = thin_border
        col_widths = [8, 25, 8, 12, 10, 12, 12, 12, 14, 12, 10, 14, 16]
        for i, w in enumerate(col_widths, 1):
            ws.column_dimensions[get_column_letter(i)].width = w
        top_idx = sig_idx_sheet
        if len(top_idx) == 0:
            ws.cell(row=4, column=1,
                    value="No MTAs passed the permutation/LOD threshold for this model.")
        for rank, idx in enumerate(top_idx, 1):
            is_sig = bool(reject_bonf[idx]) or pvals[idx] < 5e-8
            fill = sig_fill if is_sig else (alt_fill if rank%2==0 else
                   PatternFill("solid",start_color="FFFFFF",end_color="FFFFFF"))
            maf_v = maf_vals[idx] if idx < len(maf_vals) else np.nan
            row_data = [
                rank,
                str(snp_filt[idx]) if idx < len(snp_filt) else "",
                int(chr_filt[idx]) if idx < len(chr_filt) else "",
                int(pos_filt[idx]) if idx < len(pos_filt) else "",
                round(float(maf_v),4) if not np.isnan(maf_v) else "",
                round(float(betas[idx]),6),
                round(float(ses[idx]),6),
                round(float(t_stats[idx]),4),
                float(pvals[idx]),
                round(float(log_p[idx]),4),
                round(float(neglog10p_to_lod(log_p[idx])),3),
                round(float(pvals_fdr[idx]),6),
                "YES" if is_sig else "NO"
            ]
            for col, val in enumerate(row_data, 1):
                cell = ws.cell(row=rank+3, column=col, value=val)
                cell.fill = fill; cell.border = thin_border
                cell.alignment = Alignment(horizontal='center')
                if col == 9 and isinstance(val, float):
                    cell.number_format = '0.00E+00'

    # Sheet: Significant MTAs combined
    ws_mta = wb.create_sheet(title="Significant_MTAs")
    ws_mta['A1'] = (f"Significant MTAs β€” All Models β€” passing permutation/LOD threshold (LOD β‰₯ {lod_threshold:.2f})"
                   if lod_threshold is not None else f"Significant MTAs β€” All Models Combined β€” Top {top_n} per Model")
    ws_mta['A1'].font = title_font
    ws_mta.merge_cells('A1:N1')
    headers_mta = ["Model","Rank","SNP","Chr","Position","MAF","Beta","SE",
                   "T_stat","P_value","neg_log10P","LOD","FDR_BH_p","Bonferroni_sig","PVE_%"]
    for col, h in enumerate(headers_mta, 1):
        cell = ws_mta.cell(row=3, column=col, value=h)
        cell.font = header_font; cell.fill = header_fill
        cell.alignment = Alignment(horizontal='center'); cell.border = thin_border
        ws_mta.column_dimensions[get_column_letter(col)].width = 16
    mta_row = 4
    for mname, (pvals, betas, ses) in all_results.items():
        log_p = -np.log10(np.clip(pvals, 1e-300, 1))
        t_stats = np.where(ses > 0, betas / (ses + 1e-10), 0.0)
        _, pvals_fdr, _, _ = multipletests(pvals, method='fdr_bh')
        reject_bonf, _, _, _ = multipletests(pvals, method='bonferroni')
        top_idx = get_significant_idx(pvals, lod_threshold, fallback_top_n=top_n)
        if len(top_idx) == 0:
            ws_mta.cell(row=mta_row, column=1, value=f"{mname}: no MTAs passed threshold")
            mta_row += 1
            continue
        for rank, idx in enumerate(top_idx, 1):
            pve_val = round(float(pve_vals[idx]), 4) if idx < len(pve_vals) else 0.0
            maf_v = maf_vals[idx] if idx < len(maf_vals) else np.nan
            row_data = [
                mname, rank,
                str(snp_filt[idx]) if idx < len(snp_filt) else "",
                int(chr_filt[idx]) if idx < len(chr_filt) else "",
                int(pos_filt[idx]) if idx < len(pos_filt) else "",
                round(float(maf_v),4) if not np.isnan(maf_v) else "",
                round(float(betas[idx]),6),
                round(float(ses[idx]),6),
                round(float(t_stats[idx]),4),
                float(pvals[idx]),
                round(float(log_p[idx]),4),
                round(float(neglog10p_to_lod(log_p[idx])),3),
                round(float(pvals_fdr[idx]),6),
                "YES" if bool(reject_bonf[idx]) else "NO",
                pve_val
            ]
            fill = sig_fill if mta_row%2==0 else PatternFill("solid",start_color="FFFDE7",end_color="FFFDE7")
            for col, val in enumerate(row_data, 1):
                cell = ws_mta.cell(row=mta_row, column=col, value=val)
                cell.fill = fill; cell.border = thin_border
                cell.alignment = Alignment(horizontal='center')
                if col == 10 and isinstance(val, float):
                    cell.number_format = '0.00E+00'
            mta_row += 1

    # Sheet: QTN Effects
    ws_qtn = wb.create_sheet(title="QTN_Effects")
    ws_qtn['A1'] = (f"Quantitative Trait Nucleotides (QTNs) β€” passing permutation/LOD threshold (LOD β‰₯ {lod_threshold:.2f})"
                    if lod_threshold is not None else f"Quantitative Trait Nucleotides (QTNs) β€” Top {top_n} per Model")
    ws_qtn['A1'].font = title_font
    ws_qtn.merge_cells('A1:L1')
    headers_qtn = ["Model","Rank","SNP","Chr","Position","MAF",
                   "QTN_Effect_Beta","SE","95%_CI_Lower","95%_CI_Upper","P_value","LOD","PVE_%"]
    for col, h in enumerate(headers_qtn, 1):
        cell = ws_qtn.cell(row=3, column=col, value=h)
        cell.font = header_font; cell.fill = header_fill
        cell.alignment = Alignment(horizontal='center'); cell.border = thin_border
        ws_qtn.column_dimensions[get_column_letter(col)].width = 17
    qtn_row = 4
    for mname, (pvals, betas, ses) in all_results.items():
        top_idx = get_significant_idx(pvals, lod_threshold, fallback_top_n=top_n)
        if len(top_idx) == 0:
            ws_qtn.cell(row=qtn_row, column=1, value=f"{mname}: no QTNs passed threshold")
            qtn_row += 1
            continue
        for rank, idx in enumerate(top_idx, 1):
            pve_val = round(float(pve_vals[idx]), 4) if idx < len(pve_vals) else 0.0
            maf_v = maf_vals[idx] if idx < len(maf_vals) else np.nan
            ci_lower = float(betas[idx]) - 1.96 * float(ses[idx])
            ci_upper = float(betas[idx]) + 1.96 * float(ses[idx])
            lod_val = round(float(neglog10p_to_lod(-np.log10(max(float(pvals[idx]), 1e-300)))), 3)
            fill = alt_fill if qtn_row%2==0 else PatternFill("solid",start_color="FFFFFF",end_color="FFFFFF")
            row_data = [
                mname, rank,
                str(snp_filt[idx]) if idx < len(snp_filt) else "",
                int(chr_filt[idx]) if idx < len(chr_filt) else "",
                int(pos_filt[idx]) if idx < len(pos_filt) else "",
                round(float(maf_v),4) if not np.isnan(maf_v) else "",
                round(float(betas[idx]),6),
                round(float(ses[idx]),6),
                round(ci_lower,6), round(ci_upper,6),
                float(pvals[idx]), lod_val, pve_val
            ]
            for col, val in enumerate(row_data, 1):
                cell = ws_qtn.cell(row=qtn_row, column=col, value=val)
                cell.fill = fill; cell.border = thin_border
                cell.alignment = Alignment(horizontal='center')
                if col == 11 and isinstance(val, float):
                    cell.number_format = '0.00E+00'
            qtn_row += 1

    # Sheet: SNP QC
    ws_qc = wb.create_sheet(title="SNP_QC")
    ws_qc['A1'] = "SNP Quality Control Statistics"
    ws_qc['A1'].font = title_font
    ws_qc.merge_cells('A1:F1')
    headers_qc = ["SNP","Chr","Position","MAF","Call_Rate","Category"]
    for col, h in enumerate(headers_qc, 1):
        cell = ws_qc.cell(row=3, column=col, value=h)
        cell.font = header_font; cell.fill = header_fill
        cell.alignment = Alignment(horizontal='center'); cell.border = thin_border
        ws_qc.column_dimensions[get_column_letter(col)].width = 20
    call_rates = compute_call_rate(G_filt)
    for row_i in range(len(snp_filt)):
        maf_v = maf_vals[row_i] if row_i < len(maf_vals) else np.nan
        cr_v = call_rates[row_i] if row_i < len(call_rates) else np.nan
        cat = "Rare (<0.05)" if not np.isnan(maf_v) and maf_v < 0.05 else "Common"
        fill = alt_fill if row_i%2==0 else PatternFill("solid",start_color="FFFFFF",end_color="FFFFFF")
        row_data = [
            str(snp_filt[row_i]),
            int(chr_filt[row_i]) if row_i < len(chr_filt) else "",
            int(pos_filt[row_i]) if row_i < len(pos_filt) else "",
            round(float(maf_v),4) if not np.isnan(maf_v) else "",
            round(float(cr_v),4) if not np.isnan(cr_v) else "",
            cat
        ]
        for col, val in enumerate(row_data, 1):
            cell = ws_qc.cell(row=row_i+4, column=col, value=val)
            cell.fill = fill; cell.border = thin_border
            cell.alignment = Alignment(horizontal='center')

    wb.save(output_path)
    return output_path


def save_all_plots_zip(plot_dict, out_dir, dpi=EXPORT_DPI):
    zip_path = os.path.join(out_dir, "gwas_plots.zip")
    with zipfile.ZipFile(zip_path, 'w', zipfile.ZIP_DEFLATED) as zf:
        for name, fig in plot_dict.items():
            if fig is not None:
                buf = io.BytesIO()
                fig.savefig(buf, format="png", dpi=dpi, bbox_inches="tight",
                            facecolor=fig.get_facecolor())
                buf.seek(0)
                zf.writestr(f"{name}.png", buf.read())
    return zip_path


# ─────────────────────── MAIN PIPELINE ───────────────────────────────────────────
def run_gwas(file_obj, text_input, selected_models, maf_threshold, top_n, n_perm,
             lod_thr_manual, pve_thr,
             # Cofactor / advanced iterative-model settings
             iter_max_iter, farmcpu_bin_size, blink_ld_r2,
             mrmlm_screen_thresh, mrmlm_max_candidates, mrmlm_drop_thresh,
             mlm_n_pca, mrmlm_n_pca, fastmrmlm_n_pca, gemma_n_pca,
             # Manhattan cfg
             mh_w, mh_h, mh_fs, mh_fc, mh_ms, mh_alpha, mh_bg, mh_sig, mh_sug,
             mh_dot, mh_panel, mh_show_logp,
             # QQ cfg
             qq_w, qq_h, qq_fs, qq_fc, qq_ms, qq_dot, qq_line, qq_fill, qq_bg, qq_panel,
             # New Circos (density-ring) cfg
             nc_models, nc_theme, nc_rings_per_panel, nc_w, nc_h, nc_fs,
             # LD cfg
             ld_w, ld_h, ld_fs, ld_dot, ld_line,
             # PCA cfg (dark)
             pc_w, pc_h, pc_fs,
             # Kinship cfg
             ki_w, ki_h, ki_fs, ki_dot, ki_bg, ki_panel,
             # Effect cfg
             ef_w, ef_h, ef_fs, ef_pos, ef_neg, ef_fill, ef_bg,
             # Comparison cfg
             co_w, co_h, co_sig, co_sug,
             # Dashboard cfg
             db_w, db_h, db_dot):

    try:
        df, snp_cols, chroms, positions = parse_data(file_obj, text_input)
    except Exception as e:
        return [None] * 13 + [f"❌ Data parsing error: {e}"]

    y = df["Phenotype"].values.astype(float)
    ids = df["ID"].values
    G_raw = df[snp_cols].values.astype(float)
    G = impute_genotypes(G_raw)
    n, m = G.shape

    maf_vals_all = compute_maf(G)
    maf_thr = float(maf_threshold)
    keep = maf_vals_all >= maf_thr
    if keep.sum() < 2:
        keep = np.ones(m, dtype=bool)

    G_filt   = G[:, keep]
    snp_filt = np.array(snp_cols)[keep]
    chr_filt = chroms[keep] if len(chroms)==m else np.ones(G_filt.shape[1],dtype=int)
    pos_filt = positions[keep] if len(positions)==m else np.arange(G_filt.shape[1])
    maf_vals = maf_vals_all[keep]
    K = build_kinship(G_filt)

    model_funcs = {
        "OLS":        lambda: ols_gwas(y, G_filt),
        "MLM":        lambda: mlm_gwas(y, G_filt, K, n_pca=int(mlm_n_pca)),
        "EMMAX":      lambda: emmax_gwas(y, G_filt, K),
        "FarmCPU":    lambda: farmcpu_gwas(y, G_filt, K, chr_filt, pos_filt,
                                            max_iter=int(iter_max_iter),
                                            bin_size=int(farmcpu_bin_size)),
        "GEMMA":      lambda: gemma_gwas(y, G_filt, K,
                                          covariates=get_kinship_pcs(K, int(gemma_n_pca), n)),
        "BLINK":      lambda: blink_gwas(y, G_filt, K, chr_filt, pos_filt,
                                          max_iter=int(iter_max_iter),
                                          ld_r2=float(blink_ld_r2)),
        "mrMLM":      lambda: mrmlm_gwas(y, G_filt, K, chr_filt, pos_filt,
                                          screen_thresh=float(mrmlm_screen_thresh),
                                          max_candidates=int(mrmlm_max_candidates),
                                          drop_thresh=float(mrmlm_drop_thresh),
                                          n_pca=int(mrmlm_n_pca)),
        "FASTmrMLM":  lambda: fastmrmlm_gwas(y, G_filt, K, chr_filt, pos_filt,
                                              screen_thresh=float(mrmlm_screen_thresh),
                                              max_candidates=int(mrmlm_max_candidates),
                                              drop_thresh=float(mrmlm_drop_thresh),
                                              n_pca=int(fastmrmlm_n_pca)),
    }

    models_to_run = [mdl for mdl in (selected_models or list(model_funcs.keys()))
                     if mdl in model_funcs]
    if not models_to_run:
        models_to_run = list(model_funcs.keys())

    all_results = {}
    primary_p = primary_b = primary_se = None

    for mname in models_to_run:
        try:
            p, b, se = model_funcs[mname]()
            all_results[mname] = (p, b, se)
            if primary_p is None:
                primary_p, primary_b, primary_se = p, b, se
        except Exception as ex:
            print(f"Model {mname} failed: {ex}")

    if primary_p is None:
        return [None] * 13 + ["❌ No models succeeded", None]

    # ── Permutation (LOD) threshold β€” THE primary significance criterion ────
    # Churchill & Doerge-style empirical threshold from this panel, expressed
    # on the LOD scale. Everything downstream (Manhattan/Effect-size
    # highlighting, Excel MTA sheets) uses this to decide what actually
    # counts as an MTA, instead of a blind top-N slice.
    try:
        n_perm_eff = min(int(n_perm) if n_perm else 100, 200)
        lod_threshold, _, _ = permutation_threshold_lod(
            y, G_filt, K, n_perm=n_perm_eff)
    except Exception as ex:
        print(f"Permutation threshold failed: {ex}")
        lod_threshold = None
        n_perm_eff = 0

    # Manual LOD threshold override (0 = auto/permutation-derived, the default)
    lod_manual = float(lod_thr_manual) if lod_thr_manual else 0.0
    lod_threshold_auto = lod_threshold
    if lod_manual > 0:
        lod_threshold = lod_manual

    # PVE% (RSS-reduction / SS_total, kinship-aware) for EVERY marker.
    # Always computed now -- it's used both for the optional PVE% filter
    # threshold below AND as the PVE_% column in the Excel export, so both
    # need to reference the same array rather than each computing their own
    # (previously inconsistent) version.
    pve_threshold = float(pve_thr) if pve_thr else 0.0
    pve_vals_all = compute_pve_all(y, G_filt, K=K)

    top_n = int(top_n)

    # ── Build per-plot configs ────────────────────────────────────────────────
    # All build_cfg calls now correctly pass 'fc' as the 4th positional arg
    cfg_mh = build_cfg(mh_w, mh_h, mh_fs, mh_fc,
                       bg=mh_bg, panel=mh_panel,
                       sig_col=mh_sig, sug_col=mh_sug,
                       marker_size=mh_ms, alpha=mh_alpha,
                       dot_col=mh_dot)

    cfg_qq = build_cfg(qq_w, qq_h, qq_fs, qq_fc,
                       bg=qq_bg, panel=qq_panel,
                       marker_size=qq_ms,
                       dot_col=qq_dot, line_col=qq_line, fill_col=qq_fill)

    cfg_ld = build_cfg(ld_w, ld_h, ld_fs, "#2D3142",
                       dot_col=ld_dot, line_col=ld_line)

    # PCA β€” light theme (matches every other plot; no dark background)
    cfg_pc = build_cfg(pc_w, pc_h, pc_fs, TEXT_DARK,
                       gc=GRID_LIGHT, bg=BG_LIGHT, panel=PANEL_LIGHT,
                       sig_col=ACCENT_TEAL_D, sug_col=ACCENT_GOLD_D,
                       marker_size=55, alpha=0.80)

    cfg_ki = build_cfg(ki_w, ki_h, ki_fs, "#2D3142",
                       bg=ki_bg, panel=ki_panel, dot_col=ki_dot)

    cfg_ef = build_cfg(ef_w, ef_h, ef_fs, "#2D3142",
                       bg=ef_bg,
                       dot_col=ef_pos, line_col=ef_neg, fill_col=ef_fill)

    cfg_co = build_cfg(co_w, co_h, 7, "#2D3142",
                       sig_col=co_sig, sug_col=co_sug)

    cfg_db = build_cfg(db_w, db_h, 9, "#2D3142",
                       dot_col=db_dot)

    # Generate all plots
    plot_dict = {}
    plot_dict["01_Manhattan_Primary"] = plot_manhattan(
        primary_p, chr_filt, pos_filt,
        "Manhattan Plot", models_to_run[0], cfg_mh, top_n, lod_threshold=lod_threshold,
        pve_vals=pve_vals_all, pve_threshold=pve_threshold, show_logp_axis=bool(mh_show_logp))
    plot_dict["02_QQ_Primary"] = plot_qq_single(
        primary_p, "QQ Plot", models_to_run[0],
        MODEL_COLORS.get(models_to_run[0], "#A78BFA"), cfg_qq)
    plot_dict["03_QQ_All_Models"] = plot_qq_all_models(all_results, cfg_qq)
    plot_dict["04_QQ_Overlay"]    = plot_qq_overlay(all_results, cfg_qq)
    plot_dict["06_LD_Decay"]      = plot_ld_decay(G_filt, pos_filt, cfg_ld)
    plot_dict["07_PCA"]           = plot_pca(G_filt, y, ids, cfg_pc)
    plot_dict["08_Kinship"]       = plot_kinship(K, ids, cfg_ki)
    plot_dict["09_Effect_Sizes"]  = plot_effect_sizes(primary_p, primary_b, snp_filt, cfg_ef, top_n, lod_threshold=lod_threshold,
                                                        pve_vals=pve_vals_all, pve_threshold=pve_threshold)
    plot_dict["10_All_Models_MH"] = plot_model_comparison_manhattan(
        all_results, chr_filt, pos_filt, cfg_co, top_n, lod_threshold=lod_threshold)
    plot_dict["11_Dashboard"]     = plot_summary_dashboard(y, maf_vals, all_results, G_filt, cfg_db)

    nc_wanted = [m for m in (nc_models or []) if m in all_results]
    if not nc_wanted:
        nc_wanted = [m for m in models_to_run if m in all_results]
    cfg_nc = {"width": nc_w, "height": nc_h, "font_size": nc_fs}
    plot_dict["12_Circos_Density"] = plot_circos_density(
        all_results, chr_filt, pos_filt, nc_wanted, theme_name=nc_theme,
        rings_per_panel=int(nc_rings_per_panel), cfg=cfg_nc, lod_threshold=lod_threshold)

    def _pil(fig):
        return fig_to_pil(fig) if fig is not None else None

    imgs = [_pil(plot_dict[k]) for k in sorted(plot_dict.keys())]

    # Save files
    tmp_dir = tempfile.mkdtemp()
    zip_path = save_all_plots_zip(plot_dict, tmp_dir)
    excel_path = os.path.join(tmp_dir, "gwas_results_top15.xlsx")
    export_results_excel(all_results, snp_filt, chr_filt, pos_filt,
                         y, G_filt, maf_vals, excel_path, top_n, lod_threshold=lod_threshold,
                         pve_vals=pve_vals_all, pve_threshold=pve_threshold)
    plt.close('all')

    # Build threshold explanation string
    thresh_explanation = ""
    for mname, (p, _, _) in all_results.items():
        _, _, expl = compute_thresholds(len(p), mname)
        thresh_explanation += expl + "\n"

    bonf = 0.05 / len(primary_p)
    lambdas_str = "\n  ".join([
        f"{mdl}: Ξ»={np.median(stats.chi2.ppf(1-np.clip(p,1e-10,1),df=1))/stats.chi2.ppf(0.5,df=1):.4f}  "
        f"sig(Bonf)={int((p<0.05/len(p)).sum())}  sig(Sug)={int((p<1.0/len(p)).sum())}"
        for mdl, (p, _, _) in all_results.items()
    ])
    lam = (np.median(stats.chi2.ppf(1-np.clip(primary_p,1e-10,1),df=1)) /
           stats.chi2.ppf(0.5, df=1))

    n_sig_lod_primary = len(get_significant_idx(primary_p, lod_threshold, fallback_top_n=top_n,
                                                 pve_vals=pve_vals_all, pve_threshold=pve_threshold))
    if lod_manual > 0:
        lod_note = f"{lod_threshold:.2f}  (MANUAL override β€” auto/permutation value was {lod_threshold_auto if lod_threshold_auto is not None else 'unavailable'})"
    elif lod_threshold is not None:
        lod_note = f"{lod_threshold:.2f}  ({n_perm_eff} permutations, Ξ±=0.05) β€” PRIMARY significance criterion"
    else:
        lod_note = "failed to compute β€” falling back to top-N display"
    pve_note = f"β‰₯{pve_threshold:.1f}% (applied jointly with LOD threshold)" if pve_threshold > 0 else "off"

    summary = (
        f"{'='*68}\n"
        f"  GWAS ANALYSIS SUMMARY\n"
        f"{'='*68}\n"
        f"  Samples: {n}  |  SNPs (raw): {m}  |  SNPs (filtered): {G_filt.shape[1]}\n"
        f"  MAF threshold: β‰₯{maf_thr:.2f}  |  Models: {', '.join(all_results.keys())}\n"
        f"  Permutation / LOD threshold: {lod_note}\n"
        f"  PVE threshold: {pve_note}\n"
        f"  MTAs identified (primary model, {models_to_run[0]}): {n_sig_lod_primary}"
        f"{' (passing LOD threshold' + (' & PVE filter)' if pve_threshold > 0 else ')') if lod_threshold is not None else ' (fallback top-N, no threshold available)'}\n"
        f"{'─'*68}\n"
        f"  PER-MODEL RESULTS:\n  {lambdas_str}\n"
        f"{'─'*68}\n"
        f"  Primary model ({models_to_run[0]}):\n"
        f"    Bonferroni threshold : {bonf:.2e}  (-log₁₀ = {-np.log10(bonf):.2f}, LOD = {neglog10p_to_lod(-np.log10(bonf)):.2f})\n"
        f"    Suggestive threshold : {1/len(primary_p):.2e}  (-log₁₀ = {-np.log10(1/len(primary_p)):.2f}, LOD = {neglog10p_to_lod(-np.log10(1/len(primary_p))):.2f})\n"
        f"    Significant (Bonf.)  : {int((primary_p<bonf).sum())}\n"
        f"    Significant (Sug.)   : {int((primary_p<1/len(primary_p)).sum())}\n"
        f"    Genomic Inflation Ξ»  : {lam:.4f}\n"
        f"{'─'*68}\n"
        f"  THRESHOLD EXPLANATIONS (see Excel sheet 'Threshold_Explanation'):\n\n"
        + thresh_explanation +
        f"{'─'*68}\n"
        f"  Excel : gwas_results_top15.xlsx\n"
        f"  Plots : gwas_plots.zip ({len(plot_dict)} PNG files)\n"
        f"{'='*68}\n"
    )

    summary_path = os.path.join(tmp_dir, "gwas_summary.txt")
    with open(summary_path, "w", encoding="utf-8") as f:
        f.write(summary)

    cache_state = {
        "all_results": all_results,       # {model: (pvals, betas, ses)}
        "snp_filt": snp_filt,
        "chr_filt": chr_filt,
        "pos_filt": pos_filt,
        "models_to_run": models_to_run,
        "primary_model": models_to_run[0],
        "primary_p": primary_p,
        "top_n": top_n,
        "lod_threshold": lod_threshold,
        "pve_vals": pve_vals_all,
        "pve_threshold": pve_threshold,
        "show_logp_axis": bool(mh_show_logp),
    }

    return imgs[:11] + [excel_path, zip_path, summary_path, summary, cache_state]


# ─────────────────────── LIVE MANHATTAN RECOLOR ───────────────────────────────────
def refresh_manhattan_live(state, mh_w, mh_h, mh_fs, mh_fc, mh_ms, mh_alpha,
                            mh_bg, mh_sig, mh_sug, mh_dot, mh_panel, mh_show_logp):
    """
    Re-renders ONLY the primary Manhattan plot from the cached p-values of the
    last completed run β€” no re-fitting of any model. Bound to every Manhattan color
    picker / slider's .change() event so tweaking a color updates the image
    instantly instead of waiting for the whole πŸš€ Run pipeline to repeat.
    """
    if not state or "primary_p" not in state:
        return None
    cfg = build_cfg(mh_w, mh_h, mh_fs, mh_fc, bg=mh_bg, panel=mh_panel,
                     sig_col=mh_sig, sug_col=mh_sug, marker_size=mh_ms,
                     alpha=mh_alpha, dot_col=mh_dot)
    fig = plot_manhattan(state["primary_p"], state["chr_filt"], state["pos_filt"],
                          "Manhattan Plot", state["primary_model"], cfg, state["top_n"],
                          lod_threshold=state.get("lod_threshold"),
                          pve_vals=state.get("pve_vals"), pve_threshold=state.get("pve_threshold"),
                          show_logp_axis=bool(mh_show_logp))
    img = fig_to_pil(fig)
    plt.close(fig)
    return img

# ─────────────────────── COMPARE MODELS β€” COMMON MTA ──────────────────────────────
def _sig_index_set(pvals, mode, top_n, lod_threshold=None):
    """Return the set of marker indices considered 'significant' for one
    model, under Bonferroni, a fixed top-N rank cutoff, or (recommended)
    the panel's permutation/LOD threshold -- the same primary criterion
    used throughout the rest of the pipeline."""
    m = len(pvals)
    if mode.startswith("LOD"):
        idx = get_significant_idx(np.asarray(pvals), lod_threshold, fallback_top_n=int(top_n))
        return set(idx.tolist())
    elif mode.startswith("Bonferroni"):
        bonf = 0.05 / m
        idx = np.where(pvals < bonf)[0]
    else:
        idx = np.argsort(pvals)[:int(top_n)]
    return set(idx.tolist())

COMPARE_COLORS = ["#3B5BDB", "#14B8A6", "#DB4C8C", "#5FA624", "#B054E0",
                   "#E0972E", "#2AB0C5", "#D65B3E"]

def plot_common_mta(state, chosen_models, mode, top_n, cfg=None):
    """
    Two-panel comparison figure:
      Top    β€” bar chart: how many markers are shared by exactly k of the
               chosen models (the 'consensus degree' distribution). The bar
               for markers shared by ALL chosen models is highlighted gold.
      Bottom β€” consensus Manhattan: every chosen model's -log10(p) track
               overlaid in its own color (thin, semi-transparent), with
               markers significant in EVERY chosen model circled as gold
               diamonds so you can see where the models agree at a glance.
    """
    if not state or "all_results" not in state or not chosen_models:
        return None, pd.DataFrame()

    all_results = state["all_results"]
    chroms = np.asarray(state["chr_filt"])
    positions = np.asarray(state["pos_filt"])
    snp_names = np.asarray(state["snp_filt"])
    lod_threshold = state.get("lod_threshold")

    sig_sets = {}
    for mname in chosen_models:
        if mname not in all_results:
            continue
        p, _, _ = all_results[mname]
        sig_sets[mname] = _sig_index_set(np.asarray(p), mode, top_n, lod_threshold)

    if not sig_sets:
        return None, pd.DataFrame()

    n_models = len(sig_sets)
    union_idx = sorted(set().union(*sig_sets.values()))
    degree = {i: sum(1 for s in sig_sets.values() if i in s) for i in union_idx}
    consensus_idx = [i for i, d in degree.items() if d == n_models]

    if cfg is None:
        cfg = build_cfg(16, 10, 10, "#20233D")

    fig = plt.figure(figsize=(cfg["width"], cfg["height"]))
    fig.patch.set_facecolor(cfg["bg_color"])
    gs = gridspec.GridSpec(2, 1, height_ratios=[1, 1.6], hspace=0.5)

    # ── Panel 1: consensus-degree bar chart ────────────────────────────
    ax1 = fig.add_subplot(gs[0])
    ax1.set_facecolor(cfg["panel_color"])
    deg_counts = [sum(1 for d in degree.values() if d == k) for k in range(1, n_models + 1)]
    bar_colors = ["#B9C2E8"] * (n_models - 1) + ["#F59E0B"] if n_models > 0 else []
    ax1.bar(range(1, n_models + 1), deg_counts, color=bar_colors, edgecolor="#33333340")
    ax1.set_xticks(range(1, n_models + 1))
    ax1.set_xticklabels([f"{k}/{n_models}\nmodel(s)" for k in range(1, n_models + 1)],
                        fontsize=cfg["font_size"] - 1, color=cfg["font_color"])
    ax1.set_ylabel("# MTAs", color=cfg["font_color"], fontsize=cfg["font_size"])
    ax1.set_title(f"Consensus across {n_models} selected model(s) β€” "
                   f"{len(consensus_idx)} MTA(s) found by ALL of them",
                   color=cfg["font_color"], fontsize=cfg["font_size"] + 1)
    ax1.tick_params(colors=cfg["font_color"])
    for spine in ax1.spines.values():
        spine.set_edgecolor("#CCCCCC")
    ax1.grid(axis="y", color="#E8ECF4", linewidth=0.6, linestyle="--", alpha=0.7)

    # ── Panel 2: overlaid consensus Manhattan ──────────────────────────
    ax2 = fig.add_subplot(gs[1])
    ax2.set_facecolor(cfg["panel_color"])
    chrom_list = sorted(set(chroms.tolist()))
    gap = max(positions) * 0.018 + 1
    x_offset = 0
    x_coords = np.zeros(len(chroms))
    tick_pos, tick_lab = [], []
    for ch in chrom_list:
        mask = chroms == ch
        pos = positions[mask]
        if len(pos) == 0:
            continue
        order = np.argsort(pos)
        x_vals = pos[order] - pos.min() + x_offset
        x_coords[np.where(mask)[0][order]] = x_vals
        tick_pos.append(x_vals.mean())
        tick_lab.append(f"Chr{ch}")
        x_offset += (pos.max() - pos.min()) + gap

    for i, mname in enumerate(sig_sets.keys()):
        p, _, _ = all_results[mname]
        log_p = -np.log10(np.clip(np.asarray(p), 1e-300, 1))
        color = MODEL_COLORS.get(mname, COMPARE_COLORS[i % len(COMPARE_COLORS)])
        ax2.scatter(x_coords, log_p, s=8, alpha=0.35, color=color, linewidths=0,
                    label=mname)

    if consensus_idx:
        ax2.scatter(x_coords[consensus_idx],
                    [max(-np.log10(max(all_results[m][0][i], 1e-300)) for m in sig_sets) for i in consensus_idx],
                    marker="D", s=90, facecolor=ACCENT_GOLD, edgecolor="#333333",
                    linewidth=1.0, zorder=10, label=f"Common to all {n_models}")

    ax2.set_xticks(tick_pos)
    ax2.set_xticklabels(tick_lab, rotation=45, fontsize=cfg["font_size"] - 1,
                        color=cfg["font_color"])
    ax2.set_ylabel("-log₁₀(p)", color=cfg["font_color"], fontsize=cfg["font_size"])
    ax2.set_xlabel("Chromosome", color=cfg["font_color"], fontsize=cfg["font_size"])
    ax2.tick_params(colors=cfg["font_color"])
    for spine in ax2.spines.values():
        spine.set_edgecolor("#CCCCCC")
    ax2.grid(axis="y", color="#E8ECF4", linewidth=0.6, linestyle="--", alpha=0.7)
    ax2.legend(fontsize=cfg["font_size"] - 2, framealpha=0.9, facecolor=cfg["bg_color"],
              edgecolor="#CCCCCC", loc="upper right", ncol=2)

    fig.tight_layout()

    # ── Table of shared / unique MTAs ──────────────────────────────────
    rows = []
    for i in sorted(union_idx, key=lambda i: (-degree[i], min(all_results[m][0][i] for m in sig_sets if i in sig_sets[m]))):
        row = {
            "SNP": snp_names[i], "Chr": int(chroms[i]), "Pos": int(positions[i]),
            "N_Models_Sig": degree[i],
            "Models": ", ".join(m for m in sig_sets if i in sig_sets[m]),
        }
        for m in sig_sets:
            p, _, _ = all_results[m]
            pv = float(p[i])
            row[f"{m}_p"] = f"{pv:.2e}"
            row[f"{m}_LOD"] = f"{float(neglog10p_to_lod(-np.log10(max(pv, 1e-300)))):.2f}"
        rows.append(row)
    df = pd.DataFrame(rows)

    return fig, df


def run_compare_models(state, chosen_models, mode, top_n, cmp_w, cmp_h, cmp_fs):
    cfg = build_cfg(cmp_w, cmp_h, cmp_fs, "#20233D")
    fig, df = plot_common_mta(state, chosen_models, mode, top_n, cfg)
    if fig is None:
        return None, pd.DataFrame({"Message": ["Run the pipeline first, then pick β‰₯1 model here."]}), None, None
    img = fig_to_pil(fig)

    tmp_dir = tempfile.mkdtemp(prefix="gwas_common_mta_")
    fig_path = os.path.join(tmp_dir, "common_MTA_figure.png")
    fig.savefig(fig_path, format="png", dpi=EXPORT_DPI, bbox_inches="tight",
                facecolor=fig.get_facecolor())
    plt.close(fig)

    table_path = os.path.join(tmp_dir, "common_MTA_table.xlsx")
    with pd.ExcelWriter(table_path, engine="openpyxl") as writer:
        df.to_excel(writer, sheet_name="Common_MTAs", index=False)
        ws = writer.sheets["Common_MTAs"]
        header_fill = PatternFill("solid", start_color="4A90D9", end_color="4A90D9")
        for cell in ws[1]:
            cell.fill = header_fill
            cell.font = Font(bold=True, color="FFFFFF")
        for col in ws.columns:
            width = max(len(str(c.value)) if c.value is not None else 0 for c in col) + 2
            ws.column_dimensions[col[0].column_letter].width = min(max(width, 10), 40)

    return img, df, fig_path, table_path


# ─────────────────────── CSS ──────────────────────────────────────────────────────
CSS = """
@import url('https://fonts.googleapis.com/css2?family=Inter:wght@400;500;600;700&family=Source+Serif+4:wght@600;700&family=JetBrains+Mono:wght@400;500&display=swap');

:root {
  --ink:        #1A1D29;
  --ink-soft:   #565D75;
  --ink-faint:  #8A90A6;
  --line:       #E1E4EC;
  --line-soft:  #EDEFF4;
  --paper:      #FFFFFF;
  --canvas:     #F7F8FB;
  --accent:     #2B4C7E;
  --accent-soft:#EEF2F8;
  --mono:       'JetBrains Mono', monospace;
}

* { box-sizing: border-box; }

body, .gradio-container {
  background: var(--canvas) !important;
  font-family: 'Inter', sans-serif !important;
  color: var(--ink) !important;
  font-size: 14px !important;
}
.gradio-container { max-width: 1600px !important; margin: 0 auto !important; }

/* ── Masthead ───────────────────────────────────────────────────────── */
.gwas-header {
  background: var(--paper);
  border: 1px solid var(--line);
  border-bottom: 3px solid var(--accent);
  border-radius: 6px;
  padding: 20px 26px;
  margin-bottom: 18px;
}
.gwas-header h1 {
  font-family: 'Source Serif 4', serif !important;
  font-size: 21px !important; font-weight: 700 !important;
  color: var(--ink) !important; margin: 0 0 4px 0 !important; letter-spacing: -0.2px;
}
.gwas-header p { color: var(--ink-soft) !important; font-size: 12.5px !important; margin: 2px 0 !important; }

/* ── Model color chips (match MODEL_COLORS in Python) ─────────────────
   Kept as small solid swatches β€” functional legend, not decoration. */
.model-chip{display:inline-flex;align-items:center;gap:6px;padding:2px 10px;border-radius:4px;font-size:11px;font-weight:600;color:#fff;margin:2px 4px 2px 0;font-family:var(--mono);}
.chip-OLS{background:#3E7BD6}.chip-MLM{background:#14B8A6}.chip-EMMAX{background:#DB4C8C}.chip-FarmCPU{background:#5FA624}.chip-GEMMA{background:#B054E0}.chip-BLINK{background:#E0972E}.chip-mrMLM{background:#2AB0C5}.chip-FASTmrMLM{background:#D65B3E}

/* ── Section headers β€” one consistent style, no icons ─────────────── */
.section-title{
  font-size:11.5px!important; font-weight:700!important; color:var(--ink)!important;
  text-transform:uppercase; letter-spacing:0.5px;
  padding:0 0 8px 0; margin:0 0 12px 0; border-bottom:1px solid var(--line);
}
.info-box{background:var(--accent-soft);border:1px solid var(--line);border-left:3px solid var(--accent);border-radius:4px;padding:10px 14px;margin:8px 0;font-size:12px;color:var(--ink-soft);line-height:1.55;}
.info-box.warn{border-left-color:#B45309;}
.info-box.tip{border-left-color:#0F766E;}

/* ── Cards (Gradio groups/panels) β€” flat, no shadow ────────────────── */
.gr-panel, .gr-box, .block, .panel, .form { background:var(--paper)!important; border:1px solid var(--line)!important; border-radius:6px!important; box-shadow:none!important; }
.gwas-card{background:var(--paper);border:1px solid var(--line);border-radius:6px;padding:18px 20px;margin-bottom:14px;}

/* ── Text / inputs β€” consistent, quiet ─────────────────────────────── */
label, .label-wrap span { color:var(--ink-soft)!important; font-size:11.5px!important; font-weight:600!important; text-transform:uppercase!important; letter-spacing:0.3px!important; }
input, textarea, select { background:var(--paper)!important; color:var(--ink)!important; border:1px solid var(--line)!important; border-radius:4px!important; font-size:13px!important; }
input:focus, textarea:focus, select:focus { border-color:var(--accent)!important; }

/* ── Buttons β€” flat, single accent, no gradients ───────────────────── */
button.primary { background:var(--accent)!important; border:1px solid var(--accent)!important; border-radius:5px!important; color:#FFFFFF!important; font-weight:600!important; font-size:14px!important; padding:12px 24px!important; box-shadow:none!important; width:100%!important; letter-spacing:0.2px; }
button.primary:hover{ background:#1F3A61!important; }
button.secondary{ background:var(--paper)!important; border:1px solid var(--accent)!important; border-radius:5px!important; color:var(--accent)!important; font-weight:600!important; box-shadow:none!important; }
button.secondary:hover{ background:var(--accent-soft)!important; }

/* ── Tabs β€” flat underline style, identical across all tab levels ─── */
.tab-nav { background:transparent!important; border-radius:0!important; padding:0!important; border:none!important; border-bottom:1px solid var(--line)!important; overflow-x:auto!important; }
.tab-nav button { color:var(--ink-faint)!important; font-size:13px!important; font-weight:600!important; border-radius:0!important; padding:10px 16px!important; border:none!important; border-bottom:2px solid transparent!important; background:transparent!important; white-space:nowrap!important; margin-bottom:-1px!important; }
.tab-nav button.selected { color:var(--accent)!important; border-bottom:2px solid var(--accent)!important; background:transparent!important; box-shadow:none!important; }
.tab-nav button:hover { color:var(--ink)!important; }

.summary-box textarea { font-family:var(--mono)!important; font-size:11.5px!important; background:var(--canvas)!important; line-height:1.5; }
::-webkit-scrollbar { width:6px; height:6px; }
::-webkit-scrollbar-thumb { background:var(--line); border-radius:4px; }
.cfg-panel { background:var(--canvas)!important; border-radius:5px!important; padding:12px!important; border:1px solid var(--line)!important; }

/* ── Compare-tab overlap badges ─────────────────────────────────────── */
.overlap-pill{display:inline-block;padding:2px 9px;border-radius:4px;background:var(--accent-soft);color:var(--accent);font-weight:600;font-size:11.5px;border:1px solid var(--line);}

/* ── Checkbox / Radio groups: selection state always visible, flat ─── */
input[type="checkbox"], input[type="radio"] {
  width:16px!important; height:16px!important;
  accent-color:var(--accent)!important; cursor:pointer!important;
}
.gradio-container label:has(> input[type="checkbox"]),
.gradio-container label:has(> input[type="radio"]) {
  display:inline-flex!important; align-items:center!important; gap:7px!important;
  background:var(--paper)!important; border:1px solid var(--line)!important;
  border-radius:5px!important; padding:6px 12px!important; margin:3px!important;
  transition:background .12s ease, border-color .12s ease!important;
  cursor:pointer!important;
}
.gradio-container label:has(> input[type="checkbox"]:hover),
.gradio-container label:has(> input[type="radio"]:hover) {
  border-color:var(--accent)!important;
}
.gradio-container label:has(> input[type="checkbox"]:checked),
.gradio-container label:has(> input[type="radio"]:checked) {
  background:var(--accent)!important;
  border-color:var(--accent)!important;
  box-shadow:none!important;
}
.gradio-container label:has(> input[type="checkbox"]:checked) span,
.gradio-container label:has(> input[type="radio"]:checked) span {
  color:#FFFFFF!important; font-weight:600!important;
}

/* ── Accordion headers β€” match section-title, no icons/emoji ──────── */
.label-wrap { font-weight:600!important; }
"""

SAMPLE_DATA = """ID,Phenotype,chr1_134521_SNP1,chr1_134810_SNP2,chr1_178766_SNP3,chr1_210328_SNP4
chromosome,1,1,1,1
IRRI-1,25.34,0,0,0,2
IRRI-75,23.34,0,0,0,0
IRRI-84,26.67,2,0,2,0
IRRI-93,23,0,0,0,1
IRRI-9,28.34,0,0,0,0
IRRI-17,18.67,0,0,0,0
IRRI-25,20,2,0,2,0
IRRI-34,37,0,0,0,0
IRRI-42,30.67,0,0,0,0
IRRI-50,22,0,0,0,2"""

# ─────────────────────── GRADIO INTERFACE ────────────────────────────────────────
with gr.Blocks(css=CSS, title="GWAS Analysis Pipeline", theme=gr.themes.Base()) as demo:

    cache_state = gr.State({})  # holds last run's results for live recolor + Compare tab

    gr.HTML("""<div class="gwas-header">
      <h1>GWAS Analysis Pipeline</h1>
      <p>OLS &nbsp;Β·&nbsp; MLM &nbsp;Β·&nbsp; EMMAX &nbsp;Β·&nbsp; FarmCPU &nbsp;Β·&nbsp; GEMMA &nbsp;Β·&nbsp; BLINK &nbsp;Β·&nbsp; mrMLM &nbsp;Β·&nbsp; FASTmrMLM</p>
      <p style="font-size:11.5px;">Bonferroni and suggestive thresholds on every plot &nbsp;Β·&nbsp; live Manhattan recolor &nbsp;Β·&nbsp; cross-model MTA comparison</p>
    </div>
    """)

    with gr.Tabs():
        # ══════════════════════ TAB 1 β€” DATA & MODELS ══════════════════════
        with gr.Tab("1. Data & Models"):
            with gr.Row(equal_height=False):
                with gr.Column(scale=1, min_width=320):
                    with gr.Group(elem_classes=["gwas-card"]):
                        gr.HTML('<div class="section-title">Data Input</div>')
                        file_in  = gr.File(label="Upload CSV / TSV / TXT", file_types=[".csv",".tsv",".txt"])
                        text_in  = gr.Textbox(label="Or paste CSV data", lines=6, value=SAMPLE_DATA)

                    with gr.Group(elem_classes=["gwas-card"]):
                        gr.HTML('<div class="section-title">Model Selection</div>')
                        gr.HTML(
                            '<div class="info-box">'
                            + "".join(f'<span class="model-chip chip-{m}">{m}</span>' for m in ALL_MODELS)
                            + '<br><span style="font-size:11px">Colors match every plot legend '
                              'and the Compare-Models tab.</span></div>'
                        )
                        model_sel = gr.CheckboxGroup(
                            choices=ALL_MODELS,
                            value=["OLS", "MLM", "EMMAX", "FarmCPU", "GEMMA"],
                            label="Select models to run (BLINK / mrMLM / FASTmrMLM are slower, multi-locus methods)")
                        maf_thr  = gr.Slider(0, 0.5, 0.05, 0.01, label="MAF Threshold")
                        top_n_sl = gr.Slider(5, 50, 15, 1,
                            label="Fallback Top-N (used only if the LOD threshold can't be computed)")
                        n_perm = gr.Slider(20, 200, 100, 10,
                            label="Permutations for LOD threshold (Churchill & Doerge)")
                        gr.HTML(
                            '<div class="info-box" style="font-size:11px">'
                            'Markers are highlighted as MTAs when they pass the LOD threshold β€” '
                            'not a fixed count. More permutations give a more stable threshold '
                            'but take longer to compute.</div>'
                        )
                        gr.HTML('<div class="section-title">Significance &amp; MTA Selection</div>')
                        lod_thr_manual = gr.Slider(0, 10, 0, 0.1,
                            label="Manual LOD threshold (0 = automatic, from permutations above)")
                        pve_thr = gr.Slider(0, 100, 0, 1,
                            label="Minimum PVE % (0 = off β€” combined with the LOD threshold)")
                        run_btn  = gr.Button("Run Complete GWAS Pipeline", variant="primary")

                    with gr.Group(elem_classes=["gwas-card"]):
                        gr.HTML('<div class="section-title">Downloads</div>')
                        excel_out = gr.File(label="Excel (MTAs Β· Thresholds Β· PVE Β· QTN Β· All Models)", interactive=False)
                        zip_out  = gr.File(label="All Plots as PNG (ZIP)", interactive=False)
                        summary_txt_out = gr.File(label="Summary (TXT)", interactive=False)

                with gr.Column(scale=2):
                    with gr.Group(elem_classes=["gwas-card"]):
                        gr.HTML('<div class="section-title">Analysis Summary + Threshold Explanations</div>')
                        summary_out = gr.Textbox(label="", lines=32, show_label=False, elem_classes=["summary-box"])

        # ══════════════════════ TAB 2 β€” COFACTORS / ADVANCED ═══════════════
        with gr.Tab("2. Cofactors / Advanced"):
            gr.HTML('<div class="info-box tip">Left group: only affects the multi-locus models '
                    '(FarmCPU, BLINK, mrMLM, FASTmrMLM). Right group: kinship-PC covariates, used by '
                    'MLM, mrMLM, FASTmrMLM and GEMMA if selected on Tab 1.</div>')
            with gr.Row(equal_height=False):
                with gr.Column():
                    with gr.Group(elem_classes=["gwas-card"]):
                        gr.HTML('<div class="section-title">'
                                '<span class="model-chip chip-FarmCPU">FarmCPU</span>'
                                '<span class="model-chip chip-BLINK">BLINK</span> Pseudo-QTN Iteration</div>')
                        iter_max_iter = gr.Slider(2, 15, 8, 1,
                            label="Max FEM/REM iterations (both FarmCPU & BLINK)")
                        farmcpu_bin_size = gr.Slider(100_000, 5_000_000, 1_000_000, 100_000,
                            label="FarmCPU bin size (bp) β€” pseudo-QTN spacing")
                        blink_ld_r2 = gr.Slider(0.1, 0.99, 0.7, 0.01,
                            label="BLINK LD rΒ² pruning cutoff (higher = keeps more nearby pseudo-QTNs)")
                with gr.Column():
                    with gr.Group(elem_classes=["gwas-card"]):
                        gr.HTML('<div class="section-title">'
                                '<span class="model-chip chip-mrMLM">mrMLM</span>'
                                '<span class="model-chip chip-FASTmrMLM">FASTmrMLM</span> Two-Stage Screening</div>')
                        mrmlm_screen_thresh = gr.Slider(0.001, 0.05, 0.01, 0.001,
                            label="Stage-1 screening p-value threshold")
                        mrmlm_max_candidates = gr.Slider(5, 40, 20, 1,
                            label="Max candidate QTNs carried into Stage 2")
                        mrmlm_drop_thresh = gr.Slider(0.01, 0.2, 0.05, 0.01,
                            label="Stage-2 backward-elimination drop threshold")
            with gr.Row(equal_height=False):
                with gr.Column():
                    with gr.Group(elem_classes=["gwas-card"]):
                        gr.HTML('<div class="section-title">Kinship-PC Covariates (per model)</div>')
                        gr.HTML('<p class="info-box">Number of top kinship eigenvectors used as fixed '
                                '"Q"structure covariates for each model that supports them. '
                                'Only applies to a model if it\'s also selected on Tab 1. Set to 0 to disable.</p>')
                        with gr.Row():
                            mlm_n_pca = gr.Slider(0, 10, 3, 1,
                                label="MLM β€” kinship PCs")
                            gemma_n_pca = gr.Slider(0, 10, 0, 1,
                                label="GEMMA β€” kinship PCs")
                        with gr.Row():
                            mrmlm_n_pca = gr.Slider(0, 10, 3, 1,
                                label="mrMLM β€” kinship PCs")
                            fastmrmlm_n_pca = gr.Slider(0, 10, 3, 1,
                                label="FASTmrMLM β€” kinship PCs")

        # ══════════════════════ TAB 3 β€” PLOT CUSTOMIZATION ══════════════════
        with gr.Tab("3. Plot Customization"):
            gr.HTML('<div class="section-title">Plot Styling β€” Manhattan updates live as you adjust it</div>')

            with gr.Accordion("Manhattan Plot Options (live preview β€” no re-run needed)", open=True):
                gr.HTML('<p class="info-box">Left axis = LOD score (used to select MTAs). '
                        'Right axis = -log₁₀(p), optional. Bonferroni and suggestive '
                        'thresholds are always shown. Changes here update the plot instantly.</p>')
                mh_show_logp = gr.Checkbox(value=True, label="Show -log₁₀(p) on secondary right axis")
                with gr.Row():
                    mh_w  = gr.Slider(8, 24, 16, 1,  label="Width")
                    mh_h  = gr.Slider(3, 12, 5,  0.5, label="Height")
                    mh_fs  = gr.Slider(6, 18, 9,  1,  label="Font Size")
                with gr.Row():
                    mh_fc  = gr.ColorPicker(value="#2D3142", label="Font Color")
                    mh_sig  = gr.ColorPicker(value="#E15759", label="Bonferroni Line Color")
                    mh_sug  = gr.ColorPicker(value="#4E79A7", label="Suggestive Line Color")
                with gr.Row():
                    mh_ms  = gr.Slider(2, 60, 12, 1, label="Marker Size")
                    mh_alpha= gr.Slider(0.1, 1.0, 0.75, 0.05, label="Marker Alpha")
                with gr.Row():
                    mh_bg  = gr.ColorPicker(value="#FFFFFF", label="Background Color")
                    mh_panel= gr.ColorPicker(value="#FAFBFF", label="Panel Color")
                    mh_dot  = gr.ColorPicker(value="#4E79A7", label="Dot Color (overridden by chr colors)")

            with gr.Accordion("QQ Plot Options", open=False):
                gr.HTML('<p class="info-box">QQ plots include 95% CI band, regression line (slope + RΒ²), and Ξ» inflation factor.</p>')
                with gr.Row():
                    qq_w  = gr.Slider(5, 16, 7,  0.5, label="Width")
                    qq_h  = gr.Slider(5, 16, 7,  0.5, label="Height")
                    qq_fs = gr.Slider(6, 18, 10,  1,  label="Font Size")
                with gr.Row():
                    qq_fc  = gr.ColorPicker(value="#2D3142", label="Font Color")
                    qq_ms  = gr.Slider(5, 80, 22, 1,  label="Marker Size")
                with gr.Row():
                    qq_dot  = gr.ColorPicker(value="#A78BFA", label="Dot Color")
                    qq_line = gr.ColorPicker(value="#E15759", label="Regression Line Color")
                    qq_fill = gr.ColorPicker(value="#A78BFA", label="CI Fill Color")
                with gr.Row():
                    qq_bg  = gr.ColorPicker(value="#FFFFFF", label="Background Color")
                    qq_panel= gr.ColorPicker(value="#FAFBFF", label="Panel Color")

            with gr.Accordion("Circos Plot Options (Density Ring)", open=False):
                gr.HTML('<p class="info-box">Outer band = SNP density heatmap per chromosome '
                        '(grey→green→yellow→orange→red). Each inner ring is one model, '
                        'shown as a solid-colour peak silhouette with a red dotted significance '
                        'circle and red-star hits. Rings are grouped into lettered panels (A–D).</p>')
                nc_models = gr.CheckboxGroup(
                    choices=ALL_MODELS, value=["OLS", "MLM", "EMMAX", "FarmCPU", "GEMMA"],
                    label="Models to show as circos rings (only models selected/run above can appear)")
                with gr.Row():
                    nc_theme = gr.Radio(choices=["Bright", "Dark"], value="Bright", label="Color Theme")
                    nc_rings_per_panel = gr.Slider(2, 4, 3, 1, label="Rings per panel (groups models into A/B/C/D)")
                with gr.Row():
                    nc_w  = gr.Slider(10, 30, 20, 1, label="Width (per panel)")
                    nc_h  = gr.Slider(10, 30, 20, 1, label="Height (per panel)")
                    nc_fs = gr.Slider(6, 22, 11, 1, label="Font Size")

            with gr.Accordion("LD Decay Options", open=False):
                with gr.Row():
                    ld_w  = gr.Slider(8, 24, 16, 1,  label="Width")
                    ld_h  = gr.Slider(3, 12, 6,  0.5, label="Height")
                    ld_fs  = gr.Slider(6, 18, 9,  1,  label="Font Size")
                with gr.Row():
                    ld_dot  = gr.ColorPicker(value="#4E79A7", label="Dot Color")
                    ld_line = gr.ColorPicker(value="#E15759", label="Trend Line Color")

            with gr.Accordion("PCA Options (Dark Theme)", open=False):
                gr.HTML('<p class="info-box">PCA uses a dark background by design. Scree plot removed. Showing PC1v2, PC2v3, Population Structure.</p>')
                with gr.Row():
                    pc_w  = gr.Slider(8, 22, 14, 1,  label="Width")
                    pc_h  = gr.Slider(4, 14, 10, 1,  label="Height")
                    pc_fs  = gr.Slider(6, 18, 9,  1,  label="Font Size")

            with gr.Accordion("Kinship / Dendrogram Options", open=False):
                gr.HTML('<p class="info-box">For >50 samples, dendrogram auto-truncates to last 30 nodes.</p>')
                with gr.Row():
                    ki_w  = gr.Slider(8, 22, 14, 1,  label="Width")
                    ki_h  = gr.Slider(6, 20, 12, 1,  label="Height")
                    ki_fs  = gr.Slider(6, 18, 9,  1,  label="Font Size")
                with gr.Row():
                    ki_dot  = gr.ColorPicker(value="#4E79A7", label="Dot/Bar Color")
                    ki_bg  = gr.ColorPicker(value="#FFFFFF",  label="Background Color")
                    ki_panel= gr.ColorPicker(value="#FAFBFF",  label="Panel Color")

            with gr.Accordion("Effect Sizes Options", open=False):
                with gr.Row():
                    ef_w  = gr.Slider(8, 22, 14, 1,  label="Width")
                    ef_h  = gr.Slider(5, 18, 10, 1,  label="Height")
                    ef_fs  = gr.Slider(6, 18, 9,  1,  label="Font Size")
                with gr.Row():
                    ef_pos  = gr.ColorPicker(value="#4E79A7", label="Positive Effect Color")
                    ef_neg  = gr.ColorPicker(value="#E15759", label="Negative Effect Color")
                    ef_fill = gr.ColorPicker(value="#A78BFA", label="Distribution Fill Color")
                    ef_bg  = gr.ColorPicker(value="#FFFFFF",  label="Background Color")

            with gr.Accordion("All-Models Manhattan Options", open=False):
                gr.HTML('<p class="info-box">Both thresholds shown on each model subplot.</p>')
                with gr.Row():
                    co_w  = gr.Slider(8, 24, 16, 1,  label="Width")
                    co_h  = gr.Slider(2, 8,  3.5, 0.5, label="Height per model")
                with gr.Row():
                    co_sig  = gr.ColorPicker(value="#E15759", label="Bonferroni Line Color")
                    co_sug  = gr.ColorPicker(value="#4E79A7", label="Suggestive Line Color")

            with gr.Accordion("Dashboard Options", open=False):
                with gr.Row():
                    db_w  = gr.Slider(8, 24, 15, 1,  label="Width")
                    db_h  = gr.Slider(5, 18, 10, 1,  label="Height")
                    db_dot = gr.ColorPicker(value="#4E79A7", label="Bar/Dot Color")

        # ══════════════════════ TAB 4 β€” RESULTS ═════════════════════════════
        with gr.Tab("4. Results"):
            gr.HTML('<div class="section-title">Visualizations</div>')
            with gr.Tabs():
                with gr.Tab("Manhattan"):
                    img1  = gr.Image(type="pil", label="Manhattan β€” Primary Model (chr. labels, Bonferroni + suggestive thresholds, filled markers = top MTAs β€” updates live)")
                with gr.Tab("QQ Primary"):
                    img2  = gr.Image(type="pil", label="QQ Plot β€” regression line, 95% CI, Ξ» inflation factor")
                with gr.Tab("QQ All Models"):
                    img3  = gr.Image(type="pil", label="QQ Plots β€” All Models with Regression")
                with gr.Tab("QQ Overlay"):
                    img4  = gr.Image(type="pil", label="QQ Overlay (solid=obs, dashed=regression per model)")
                with gr.Tab("LD Decay"):
                    img5  = gr.Image(type="pil", label="LD Decay (bp & Mb)")
                with gr.Tab("PCA (Dark)"):
                    img6  = gr.Image(type="pil", label="PCA β€” Dark theme (PC1v2, PC2v3, Population Structure)")
                with gr.Tab("Kinship"):
                    img7  = gr.Image(type="pil", label="Kinship & Dendrogram")
                with gr.Tab("Effect Sizes"):
                    img8  = gr.Image(type="pil", label="Effect Sizes & Volcano Plot")
                with gr.Tab("All Models MH"):
                    img9 = gr.Image(type="pil", label="All-Models Manhattan (Chr labels Β· both thresholds)")
                with gr.Tab("Dashboard"):
                    img10 = gr.Image(type="pil", label="Summary Dashboard (Threshold table replaces Scree plot)")
                with gr.Tab("Circos (Density)"):
                    img11 = gr.Image(type="pil", label="Circos β€” Density Ring + Multi-Model Rings (lettered panels)")

        # ══════════════════════ TAB 5 β€” COMPARE MODELS ══════════════════════
        with gr.Tab("5. Compare Models"):
            gr.HTML('<div class="info-box tip">Run the pipeline on Tab 1 first. This tab reuses those cached '
                    'results β€” it does not re-fit any model β€” to find which MTAs (marker-trait associations) '
                    'are shared across the models you pick below.</div>')
            with gr.Row(equal_height=False):
                with gr.Column(scale=1, min_width=300):
                    with gr.Group(elem_classes=["gwas-card"]):
                        gr.HTML('<div class="section-title">Comparison Settings</div>')
                        cmp_models = gr.CheckboxGroup(
                            choices=ALL_MODELS, value=["OLS", "MLM", "EMMAX", "FarmCPU", "GEMMA"],
                            label="Models to compare (must have been run on Tab 1)")
                        cmp_mode = gr.Radio(
                            choices=["LOD (permutation) β€” recommended", "Bonferroni (0.05/m)", "Top-N rank"],
                            value="LOD (permutation) β€” recommended", label="Significance definition")
                        cmp_top_n = gr.Slider(5, 50, 15, 1, label="N (used only if 'Top-N rank' selected, or as fallback if LOD threshold unavailable)")
                        with gr.Row():
                            cmp_w  = gr.Slider(10, 24, 16, 1, label="Width")
                            cmp_h  = gr.Slider(6, 16, 10, 1,  label="Height")
                            cmp_fs = gr.Slider(6, 16, 10, 1,  label="Font Size")
                        cmp_btn = gr.Button("Find Common MTAs", elem_classes=["secondary"])
                with gr.Column(scale=2):
                    cmp_img = gr.Image(type="pil", label="Consensus-degree bar chart + overlaid consensus Manhattan (gold markers = common to ALL selected models)")
            gr.HTML('<div class="section-title">Shared / Unique MTA Table</div>')
            cmp_table = gr.Dataframe(label="", wrap=True)
            with gr.Row():
                cmp_fig_dl  = gr.File(label="Download Common-MTA Figure (PNG, 400 dpi)", interactive=False)
                cmp_table_dl = gr.File(label="Download Common-MTA Table (XLSX)", interactive=False)

    all_inputs = [
        file_in, text_in, model_sel, maf_thr, top_n_sl, n_perm,
        lod_thr_manual, pve_thr,
        # Cofactors / advanced
        iter_max_iter, farmcpu_bin_size, blink_ld_r2,
        mrmlm_screen_thresh, mrmlm_max_candidates, mrmlm_drop_thresh,
        mlm_n_pca, mrmlm_n_pca, fastmrmlm_n_pca, gemma_n_pca,
        # Manhattan
        mh_w, mh_h, mh_fs, mh_fc, mh_ms, mh_alpha, mh_bg, mh_sig, mh_sug,
        mh_dot, mh_panel, mh_show_logp,
        # QQ
        qq_w, qq_h, qq_fs, qq_fc, qq_ms, qq_dot, qq_line, qq_fill, qq_bg, qq_panel,
        # Circos (density ring)
        nc_models, nc_theme, nc_rings_per_panel, nc_w, nc_h, nc_fs,
        # LD
        ld_w, ld_h, ld_fs, ld_dot, ld_line,
        # PCA
        pc_w, pc_h, pc_fs,
        # Kinship
        ki_w, ki_h, ki_fs, ki_dot, ki_bg, ki_panel,
        # Effect
        ef_w, ef_h, ef_fs, ef_pos, ef_neg, ef_fill, ef_bg,
        # Comparison
        co_w, co_h, co_sig, co_sug,
        # Dashboard
        db_w, db_h, db_dot,
    ]

    run_btn.click(
        run_gwas,
        inputs=all_inputs,
        outputs=[img1, img2, img3, img4, img5, img6, img7,
                 img8, img9, img10, img11, excel_out, zip_out,
                 summary_txt_out, summary_out, cache_state]
    )

    # ── Live Manhattan recolor: any change below redraws img1 instantly ─────
    live_mh_inputs = [cache_state, mh_w, mh_h, mh_fs, mh_fc, mh_ms, mh_alpha,
                       mh_bg, mh_sig, mh_sug, mh_dot, mh_panel]
    for comp in [mh_w, mh_h, mh_fs, mh_fc, mh_ms, mh_alpha, mh_bg, mh_sig,
                 mh_sug, mh_dot, mh_panel]:
        comp.change(refresh_manhattan_live, inputs=live_mh_inputs, outputs=[img1])

    # ── Compare Models tab ───────────────────────────────────────────────────
    cmp_btn.click(
        run_compare_models,
        inputs=[cache_state, cmp_models, cmp_mode, cmp_top_n, cmp_w, cmp_h, cmp_fs],
        outputs=[cmp_img, cmp_table, cmp_fig_dl, cmp_table_dl]
    )

if __name__ == "__main__":
    demo.launch(share=False, server_port=7860)