modelsdev 0.11.4

A fast TUI and CLI for browsing AI models, benchmarks, and coding agents
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
use ratatui::{
    layout::{Constraint, Direction, Layout, Rect},
    style::{Color, Modifier, Style},
    text::{Line, Span},
    widgets::{Block, Borders},
    Frame,
};
use unicode_width::UnicodeWidthStr;

use super::render::compare_colors;
use crate::formatting::format_tokens;
use crate::tui::app::App;
use crate::tui::widgets::scrollable_panel::ScrollablePanel;

// ── H2H comparison table ────────────────────────────────────────────────────

struct MetricDef {
    label: &'static str,
    extract: fn(&crate::benchmarks::BenchmarkEntry) -> Option<f64>,
    format: fn(f64) -> String,
    higher_is_better: bool,
}

fn fmt_h2h_index(v: f64) -> String {
    format!("{:.1}", v)
}

fn fmt_h2h_pct(v: f64) -> String {
    format!("{:.1}%", v * 100.0)
}

fn fmt_h2h_speed(v: f64) -> String {
    format!("{:.0}", v)
}

fn fmt_h2h_latency(v: f64) -> String {
    format!("{:.0}ms", v)
}

fn fmt_h2h_price(v: f64) -> String {
    format!("${:.2}", v)
}

/// A section header or a metric row in the H2H table.
enum H2HRow {
    Section(&'static str),
    Metric(MetricDef),
}

fn h2h_rows() -> Vec<H2HRow> {
    vec![
        // Indexes (0-100, higher better)
        H2HRow::Section("Indexes (0\u{2013}100)"),
        H2HRow::Metric(MetricDef {
            label: "Intelligence",
            extract: |e| e.intelligence_index,
            format: fmt_h2h_index,
            higher_is_better: true,
        }),
        H2HRow::Metric(MetricDef {
            label: "Coding",
            extract: |e| e.coding_index,
            format: fmt_h2h_index,
            higher_is_better: true,
        }),
        H2HRow::Metric(MetricDef {
            label: "Math",
            extract: |e| e.math_index,
            format: fmt_h2h_index,
            higher_is_better: true,
        }),
        // Benchmarks (%, higher better)
        H2HRow::Section("Benchmarks (%)"),
        H2HRow::Metric(MetricDef {
            label: "GPQA",
            extract: |e| e.gpqa,
            format: fmt_h2h_pct,
            higher_is_better: true,
        }),
        H2HRow::Metric(MetricDef {
            label: "MMLU-Pro",
            extract: |e| e.mmlu_pro,
            format: fmt_h2h_pct,
            higher_is_better: true,
        }),
        H2HRow::Metric(MetricDef {
            label: "HLE",
            extract: |e| e.hle,
            format: fmt_h2h_pct,
            higher_is_better: true,
        }),
        H2HRow::Metric(MetricDef {
            label: "MATH-500",
            extract: |e| e.math_500,
            format: fmt_h2h_pct,
            higher_is_better: true,
        }),
        H2HRow::Metric(MetricDef {
            label: "AIME",
            extract: |e| e.aime,
            format: fmt_h2h_pct,
            higher_is_better: true,
        }),
        H2HRow::Metric(MetricDef {
            label: "AIME'25",
            extract: |e| e.aime_25,
            format: fmt_h2h_pct,
            higher_is_better: true,
        }),
        H2HRow::Metric(MetricDef {
            label: "LiveCodeBench",
            extract: |e| e.livecodebench,
            format: fmt_h2h_pct,
            higher_is_better: true,
        }),
        H2HRow::Metric(MetricDef {
            label: "SciCode",
            extract: |e| e.scicode,
            format: fmt_h2h_pct,
            higher_is_better: true,
        }),
        H2HRow::Metric(MetricDef {
            label: "IFBench",
            extract: |e| e.ifbench,
            format: fmt_h2h_pct,
            higher_is_better: true,
        }),
        H2HRow::Metric(MetricDef {
            label: "Terminal",
            extract: |e| e.terminalbench_hard,
            format: fmt_h2h_pct,
            higher_is_better: true,
        }),
        H2HRow::Metric(MetricDef {
            label: "Tau2",
            extract: |e| e.tau2,
            format: fmt_h2h_pct,
            higher_is_better: true,
        }),
        H2HRow::Metric(MetricDef {
            label: "LCR",
            extract: |e| e.lcr,
            format: fmt_h2h_pct,
            higher_is_better: true,
        }),
        // Performance (speed ↑, latency ↓)
        H2HRow::Section("Performance"),
        H2HRow::Metric(MetricDef {
            label: "Speed (tok/s)",
            extract: |e| e.output_tps,
            format: fmt_h2h_speed,
            higher_is_better: true,
        }),
        H2HRow::Metric(MetricDef {
            label: "TTFT (ms)",
            extract: |e| e.ttft,
            format: fmt_h2h_latency,
            higher_is_better: false,
        }),
        H2HRow::Metric(MetricDef {
            label: "TTFAT (ms)",
            extract: |e| e.ttfat,
            format: fmt_h2h_latency,
            higher_is_better: false,
        }),
        // Pricing ($/M tokens, lower better)
        H2HRow::Section("Pricing ($/M)"),
        H2HRow::Metric(MetricDef {
            label: "Input",
            extract: |e| e.price_input,
            format: fmt_h2h_price,
            higher_is_better: false,
        }),
        H2HRow::Metric(MetricDef {
            label: "Output",
            extract: |e| e.price_output,
            format: fmt_h2h_price,
            higher_is_better: false,
        }),
        H2HRow::Metric(MetricDef {
            label: "Blended",
            extract: |e| e.price_blended,
            format: fmt_h2h_price,
            higher_is_better: false,
        }),
    ]
}

/// Rank extracted values: 1 = best, None for missing data.
fn rank_values(values: &[Option<f64>], higher_is_better: bool) -> Vec<Option<u32>> {
    let mut indexed: Vec<(usize, f64)> = values
        .iter()
        .enumerate()
        .filter_map(|(i, v)| v.map(|val| (i, val)))
        .collect();

    if higher_is_better {
        indexed.sort_by(|a, b| b.1.partial_cmp(&a.1).unwrap_or(std::cmp::Ordering::Equal));
    } else {
        indexed.sort_by(|a, b| a.1.partial_cmp(&b.1).unwrap_or(std::cmp::Ordering::Equal));
    }

    let mut ranks = vec![None; values.len()];
    for (rank, (idx, _)) in indexed.iter().enumerate() {
        ranks[*idx] = Some(rank as u32 + 1);
    }
    ranks
}

pub(super) fn draw_h2h_table_generic(f: &mut Frame, area: Rect, app: &App) {
    let entries = app.benchmark_store.entries();
    let selections = &app.selections;

    if selections.len() < 2 {
        return;
    }

    let is_focused = app.benchmarks_app.focus == super::app::BenchmarkFocus::Compare;

    // Use a temporary inner rect to check minimum dimensions before building lines.
    // We subtract 2 for borders to approximate inner dimensions.
    let inner_w = area.width.saturating_sub(2);
    let inner_h = area.height.saturating_sub(2);
    if inner_w < 20 || inner_h < 3 {
        return;
    }

    let rows = h2h_rows();
    let label_w = 14_u16;
    let num_models = selections.len();
    let available = inner_w.saturating_sub(label_w);
    let col_w = (available as usize / num_models).max(10);
    let total_w = inner_w as usize;

    // Header row: model names
    let mut header_spans: Vec<Span> = vec![Span::styled(
        format!("{:<width$}", "", width = label_w as usize),
        Style::default(),
    )];
    for (i, &store_idx) in selections.iter().enumerate() {
        let name = entries
            .get(store_idx)
            .map(|e| e.display_name.as_str())
            .unwrap_or("?");
        let color = compare_colors(i);
        let truncated = if name.width() > col_w - 1 {
            format!("{:.width$}", name, width = col_w - 2)
        } else {
            name.to_string()
        };
        header_spans.push(Span::styled(
            format!("{:>width$}", truncated, width = col_w),
            Style::default().fg(color).add_modifier(Modifier::BOLD),
        ));
    }

    let mut lines: Vec<Line> = vec![Line::from(header_spans)];

    // Separator
    let sep = "\u{2500}".repeat(total_w);
    lines.push(Line::from(Span::styled(
        sep,
        Style::default().fg(Color::DarkGray),
    )));

    // ── Pre-compute win counts (need them near the top) ──
    let mut win_counts = vec![0u32; num_models];
    for row in &rows {
        if let H2HRow::Metric(metric) = row {
            let values: Vec<Option<f64>> = selections
                .iter()
                .map(|&idx| entries.get(idx).and_then(|e| (metric.extract)(e)))
                .collect();
            let ranks = rank_values(&values, metric.higher_is_better);
            for (i, rank) in ranks.iter().enumerate() {
                if *rank == Some(1) {
                    win_counts[i] += 1;
                }
            }
        }
    }

    // ── Win count (right under model names) ──
    let mut wins_spans: Vec<Span> = vec![Span::styled(
        format!("{:<width$}", "\u{2605} Wins", width = label_w as usize),
        Style::default()
            .fg(Color::Yellow)
            .add_modifier(Modifier::BOLD),
    )];
    let max_wins = win_counts.iter().copied().max().unwrap_or(0);
    for (i, &count) in win_counts.iter().enumerate() {
        let color = compare_colors(i);
        let label = if count == max_wins && max_wins > 0 {
            format!("{} \u{2605}", count)
        } else {
            format!("{}", count)
        };
        let style = if count == max_wins && max_wins > 0 {
            Style::default().fg(color).add_modifier(Modifier::BOLD)
        } else {
            Style::default().fg(color)
        };
        wins_spans.push(Span::styled(
            format!("{:>width$}", label, width = col_w),
            style,
        ));
    }
    lines.push(Line::from(wins_spans));

    // ── Model Info section ──
    let info_header = "\u{2500}\u{2500}\u{2500} Model Info \u{2500}".to_string();
    lines.push(Line::from(Span::styled(
        format!("{:<width$}", info_header, width = total_w),
        Style::default().fg(Color::DarkGray),
    )));

    // Helper to render an info row with per-value colors
    let render_info_row = |lines: &mut Vec<Line>, label: &str, values: Vec<(String, Color)>| {
        let mut spans: Vec<Span> = vec![Span::styled(
            format!("{:<width$}", label, width = label_w as usize),
            Style::default().fg(Color::DarkGray),
        )];
        for (val, color) in values.iter() {
            let truncated = if val.width() > col_w - 1 {
                format!("{:.width$}", val, width = col_w - 2)
            } else {
                val.clone()
            };
            spans.push(Span::styled(
                format!("{:>width$}", truncated, width = col_w),
                Style::default().fg(*color),
            ));
        }
        lines.push(Line::from(spans));
    };

    // Creator
    let creators: Vec<(String, Color)> = selections
        .iter()
        .map(|&idx| {
            let name = entries
                .get(idx)
                .map(|e| {
                    if !e.creator_name.is_empty() {
                        e.creator_name.clone()
                    } else {
                        e.creator.clone()
                    }
                })
                .unwrap_or_default();
            (name, Color::White)
        })
        .collect();
    render_info_row(&mut lines, "Creator", creators);

    // Source (Open/Closed) with color
    let sources: Vec<(String, Color)> = selections
        .iter()
        .map(|&idx| {
            entries
                .get(idx)
                .and_then(|e| app.open_weights_map.get(&e.slug))
                .map(|&open| {
                    if open {
                        ("Open".to_string(), Color::Green)
                    } else {
                        ("Closed".to_string(), Color::Red)
                    }
                })
                .unwrap_or_else(|| ("\u{2014}".to_string(), Color::DarkGray))
        })
        .collect();
    render_info_row(&mut lines, "Source", sources);

    // Region with creator region colors
    let regions: Vec<(String, Color)> = selections
        .iter()
        .map(|&idx| {
            entries
                .get(idx)
                .map(|e| {
                    let region = super::app::CreatorRegion::from_creator(&e.creator);
                    (region.label().to_string(), region.color())
                })
                .unwrap_or_default()
        })
        .collect();
    render_info_row(&mut lines, "Region", regions);

    // Type with creator type colors
    let types: Vec<(String, Color)> = selections
        .iter()
        .map(|&idx| {
            entries
                .get(idx)
                .map(|e| {
                    let ct = super::app::CreatorType::from_creator(&e.creator);
                    (ct.label().to_string(), ct.color())
                })
                .unwrap_or_default()
        })
        .collect();
    render_info_row(&mut lines, "Type", types);

    // Release date
    let dates: Vec<(String, Color)> = selections
        .iter()
        .map(|&idx| {
            let d = entries
                .get(idx)
                .and_then(|e| e.release_date.clone())
                .unwrap_or_else(|| "\u{2014}".to_string());
            (d, Color::White)
        })
        .collect();
    render_info_row(&mut lines, "Released", dates);

    // Reasoning status with color
    let reasoning_vals: Vec<(String, Color)> = selections
        .iter()
        .map(|&idx| {
            entries
                .get(idx)
                .map(|e| {
                    use crate::benchmarks::ReasoningStatus;
                    match e.reasoning_status {
                        ReasoningStatus::Reasoning => ("Reasoning".to_string(), Color::Cyan),
                        ReasoningStatus::NonReasoning => {
                            ("Non-reasoning".to_string(), Color::DarkGray)
                        }
                        ReasoningStatus::Adaptive => ("Adaptive".to_string(), Color::Yellow),
                        ReasoningStatus::None => ("\u{2014}".to_string(), Color::DarkGray),
                    }
                })
                .unwrap_or_else(|| ("\u{2014}".to_string(), Color::DarkGray))
        })
        .collect();
    render_info_row(&mut lines, "Reasoning", reasoning_vals);

    // Effort level (if any model has one)
    let effort_vals: Vec<(String, Color)> = selections
        .iter()
        .map(|&idx| {
            entries
                .get(idx)
                .and_then(|e| e.effort_level.as_ref())
                .map(|lvl| (lvl.clone(), Color::White))
                .unwrap_or_else(|| ("\u{2014}".to_string(), Color::DarkGray))
        })
        .collect();
    if effort_vals.iter().any(|(v, _)| v != "\u{2014}") {
        render_info_row(&mut lines, "Effort", effort_vals);
    }

    // Variant tag (if any model has one)
    let variant_vals: Vec<(String, Color)> = selections
        .iter()
        .map(|&idx| {
            entries
                .get(idx)
                .and_then(|e| e.variant_tag.as_ref())
                .map(|tag| (tag.clone(), Color::White))
                .unwrap_or_else(|| ("\u{2014}".to_string(), Color::DarkGray))
        })
        .collect();
    if variant_vals.iter().any(|(v, _)| v != "\u{2014}") {
        render_info_row(&mut lines, "Variant", variant_vals);
    }

    // Tool call support with color
    let tool_vals: Vec<(String, Color)> = selections
        .iter()
        .map(|&idx| {
            entries
                .get(idx)
                .and_then(|e| e.tool_call)
                .map(|tc| {
                    if tc {
                        ("Yes".to_string(), Color::Green)
                    } else {
                        ("No".to_string(), Color::DarkGray)
                    }
                })
                .unwrap_or_else(|| ("\u{2014}".to_string(), Color::DarkGray))
        })
        .collect();
    render_info_row(&mut lines, "Tools", tool_vals);

    // Context window
    let ctx_vals: Vec<(String, Color)> = selections
        .iter()
        .map(|&idx| {
            entries
                .get(idx)
                .and_then(|e| e.context_window)
                .map(|v| (format_tokens(v), Color::White))
                .unwrap_or_else(|| ("\u{2014}".to_string(), Color::DarkGray))
        })
        .collect();
    render_info_row(&mut lines, "Context", ctx_vals);

    // Max output
    let out_vals: Vec<(String, Color)> = selections
        .iter()
        .map(|&idx| {
            entries
                .get(idx)
                .and_then(|e| e.max_output)
                .map(|v| (format_tokens(v), Color::White))
                .unwrap_or_else(|| ("\u{2014}".to_string(), Color::DarkGray))
        })
        .collect();
    render_info_row(&mut lines, "Max Output", out_vals);

    // ── Metric rows with section headers and ranks ──
    for row in &rows {
        match row {
            H2HRow::Section(title) => {
                let header = format!("\u{2500}\u{2500}\u{2500} {} \u{2500}", title);
                lines.push(Line::from(Span::styled(
                    format!("{:<width$}", header, width = total_w),
                    Style::default().fg(Color::DarkGray),
                )));
            }
            H2HRow::Metric(metric) => {
                let values: Vec<Option<f64>> = selections
                    .iter()
                    .map(|&idx| entries.get(idx).and_then(|e| (metric.extract)(e)))
                    .collect();
                let ranks = rank_values(&values, metric.higher_is_better);

                let mut row_spans: Vec<Span> = vec![Span::styled(
                    format!("{:<width$}", metric.label, width = label_w as usize),
                    Style::default().fg(Color::DarkGray),
                )];

                for (i, (val, rank)) in values.iter().zip(ranks.iter()).enumerate() {
                    let color = compare_colors(i);
                    match val {
                        Some(v) => {
                            let formatted = (metric.format)(*v);
                            if *rank == Some(1) {
                                // Best: value ★
                                let value_and_star = format!("{} \u{2605}", formatted);
                                let padded = format!("{:>width$}", value_and_star, width = col_w);
                                let star_pos = padded.rfind('\u{2605}').unwrap_or(padded.len());
                                row_spans.push(Span::styled(
                                    padded[..star_pos].to_string(),
                                    Style::default().fg(color).add_modifier(Modifier::BOLD),
                                ));
                                row_spans.push(Span::styled(
                                    "\u{2605}",
                                    Style::default()
                                        .fg(Color::Yellow)
                                        .add_modifier(Modifier::BOLD),
                                ));
                            } else {
                                // Non-best: value in model color, rank in medal colors
                                let rank_num = rank.unwrap_or(0);
                                let suffix = format!(" #{}", rank_num);
                                let rank_color = match rank_num {
                                    2 => Color::Indexed(250), // silver
                                    3 => Color::Indexed(172), // bronze
                                    _ => Color::DarkGray,
                                };

                                let combined = format!("{}{}", formatted, suffix);
                                let padded = format!("{:>width$}", combined, width = col_w);
                                let suffix_start = padded.len().saturating_sub(suffix.len());
                                row_spans.push(Span::styled(
                                    padded[..suffix_start].to_string(),
                                    Style::default().fg(color),
                                ));
                                row_spans.push(Span::styled(
                                    padded[suffix_start..].to_string(),
                                    Style::default().fg(rank_color),
                                ));
                            }
                        }
                        None => {
                            row_spans.push(Span::styled(
                                format!("{:>width$}", "\u{2014}", width = col_w),
                                Style::default().fg(Color::DarkGray),
                            ));
                        }
                    }
                }

                lines.push(Line::from(row_spans));
            }
        }
    }

    ScrollablePanel::new(
        "Head-to-Head",
        lines,
        &app.benchmarks_app.h2h_scroll,
        is_focused,
    )
    .with_wrap(false)
    .render(f, area);
}

pub(super) fn draw_scatter(f: &mut Frame, area: Rect, app: &App) {
    use ratatui::symbols::Marker;
    use ratatui::widgets::{Axis, Chart, Dataset, GraphType};

    let entries = app.benchmark_store.entries();
    if entries.is_empty() {
        let block = Block::default().borders(Borders::ALL).title(" Scatter ");
        f.render_widget(block, area);
        return;
    }

    let x_extract = app.benchmarks_app.scatter_x.extract();
    let y_extract = app.benchmarks_app.scatter_y.extract();

    // Collect all points with both x and y values present
    let mut all_points: Vec<(f64, f64)> = Vec::new();
    for entry in entries.iter() {
        if let (Some(x), Some(y)) = (x_extract(entry), y_extract(entry)) {
            all_points.push((x, y));
        }
    }

    if all_points.is_empty() {
        let block = Block::default()
            .borders(Borders::ALL)
            .title(" Scatter (no data) ");
        f.render_widget(block, area);
        return;
    }

    // Split area: chart on top, legend box at bottom (if selections exist)
    let has_selections = !app.selections.is_empty();
    let legend_height = if has_selections {
        (app.selections.len() as u16 + 2).min(area.height / 3) // +2 for borders
    } else {
        0
    };
    let (chart_area, legend_area) = if has_selections {
        let chunks = Layout::default()
            .direction(Direction::Vertical)
            .constraints([Constraint::Min(5), Constraint::Length(legend_height)])
            .split(area);
        (chunks[0], Some(chunks[1]))
    } else {
        (area, None)
    };

    // Auto log scale for skewed axes
    let f64_cmp = |a: &f64, b: &f64| a.partial_cmp(b).unwrap_or(std::cmp::Ordering::Equal);
    let mut x_vals: Vec<f64> = all_points.iter().map(|p| p.0).collect();
    let mut y_vals: Vec<f64> = all_points.iter().map(|p| p.1).collect();
    x_vals.sort_by(f64_cmp);
    y_vals.sort_by(f64_cmp);

    fn is_skewed(sorted: &[f64]) -> bool {
        if sorted.len() < 5 {
            return false;
        }
        let mid = sorted[sorted.len() / 2];
        let max = sorted[sorted.len() - 1];
        mid > 0.0 && max / mid > 5.0
    }

    let x_log = is_skewed(&x_vals);
    let y_log = is_skewed(&y_vals);

    let log_transform = |v: f64, use_log: bool| -> f64 {
        if use_log {
            (v.max(0.001)).ln()
        } else {
            v
        }
    };

    let display_points: Vec<(f64, f64)> = all_points
        .iter()
        .map(|&(x, y)| (log_transform(x, x_log), log_transform(y, y_log)))
        .collect();

    let x_min = display_points
        .iter()
        .map(|p| p.0)
        .fold(f64::INFINITY, f64::min);
    let x_max = display_points
        .iter()
        .map(|p| p.0)
        .fold(f64::NEG_INFINITY, f64::max);
    let y_min = display_points
        .iter()
        .map(|p| p.1)
        .fold(f64::INFINITY, f64::min);
    let y_max = display_points
        .iter()
        .map(|p| p.1)
        .fold(f64::NEG_INFINITY, f64::max);

    // Snap non-log bounds to nice round numbers so ticks land on whole values.
    let nice_bounds = |lo: f64, hi: f64, num_ticks: usize| -> [f64; 2] {
        let range = hi - lo;
        let raw_step = range / (num_ticks - 1) as f64;
        let mag = 10_f64.powf(raw_step.log10().floor());
        let nice_step = if raw_step / mag < 1.5 {
            mag
        } else if raw_step / mag < 3.5 {
            mag * 2.0
        } else if raw_step / mag < 7.5 {
            mag * 5.0
        } else {
            mag * 10.0
        };
        let nice_lo = (lo / nice_step).floor() * nice_step;
        let nice_hi = (hi / nice_step).ceil() * nice_step;
        [nice_lo.max(0.0), nice_hi]
    };

    let x_pad = (x_max - x_min).max(0.1) * 0.05;
    let y_pad = (y_max - y_min).max(0.1) * 0.05;
    let num_ticks = 7_usize;
    let x_bounds = if x_log {
        [x_min - x_pad, x_max + x_pad]
    } else {
        nice_bounds(x_min - x_pad, x_max + x_pad, num_ticks)
    };
    let y_bounds = if y_log {
        [y_min - y_pad, y_max + y_pad]
    } else {
        nice_bounds(y_min - y_pad, y_max + y_pad, num_ticks)
    };

    // Compute independent averages (each axis uses all entries with data for that metric)
    let (x_sum, x_count) = entries.iter().fold((0.0_f64, 0_u32), |(s, c), e| {
        if let Some(v) = x_extract(e) {
            (s + log_transform(v, x_log), c + 1)
        } else {
            (s, c)
        }
    });
    let (y_sum, y_count) = entries.iter().fold((0.0_f64, 0_u32), |(s, c), e| {
        if let Some(v) = y_extract(e) {
            (s + log_transform(v, y_log), c + 1)
        } else {
            (s, c)
        }
    });
    let avg_x = if x_count > 0 {
        x_sum / x_count as f64
    } else {
        (x_bounds[0] + x_bounds[1]) / 2.0
    };
    let avg_y = if y_count > 0 {
        y_sum / y_count as f64
    } else {
        (y_bounds[0] + y_bounds[1]) / 2.0
    };

    let v_line = vec![(avg_x, y_bounds[0]), (avg_x, y_bounds[1])];
    let h_line = vec![(x_bounds[0], avg_y), (x_bounds[1], avg_y)];

    // Build selected model point sets + legend
    #[allow(clippy::type_complexity)]
    let mut legend_entries: Vec<(String, Color, u8, Option<f64>, Option<f64>)> = Vec::new();
    #[allow(clippy::type_complexity)]
    let mut selected_data: Vec<(String, Vec<(f64, f64)>, Color)> = Vec::new();

    for (sel_idx, &store_idx) in app.selections.iter().enumerate() {
        let color = compare_colors(sel_idx);
        if let Some(entry) = entries.get(store_idx) {
            let name = entry.display_name.clone();
            let raw_x = x_extract(entry);
            let raw_y = y_extract(entry);
            if let (Some(x), Some(y)) = (raw_x, raw_y) {
                let tx = log_transform(x, x_log);
                let ty = log_transform(y, y_log);
                let in_range = tx >= x_bounds[0]
                    && tx <= x_bounds[1]
                    && ty >= y_bounds[0]
                    && ty <= y_bounds[1];
                selected_data.push((entry.display_name.clone(), vec![(tx, ty)], color));
                legend_entries.push((name, color, if in_range { 1 } else { 2 }, raw_x, raw_y));
            } else {
                legend_entries.push((name, color, 0, raw_x, raw_y));
            }
        }
    }

    // Build datasets — crosshairs, background, then selected
    let mut datasets = vec![
        Dataset::default()
            .marker(Marker::Braille)
            .graph_type(GraphType::Line)
            .style(Style::default().fg(Color::Indexed(242)))
            .data(&v_line),
        Dataset::default()
            .marker(Marker::Braille)
            .graph_type(GraphType::Line)
            .style(Style::default().fg(Color::Indexed(242)))
            .data(&h_line),
        Dataset::default()
            .marker(Marker::Dot)
            .graph_type(GraphType::Scatter)
            .style(Style::default().fg(Color::DarkGray))
            .data(&display_points),
    ];

    for (_, points, color) in &selected_data {
        datasets.push(
            Dataset::default()
                .marker(Marker::HalfBlock)
                .graph_type(GraphType::Scatter)
                .style(Style::default().fg(*color))
                .data(points),
        );
    }

    let x_label = app.benchmarks_app.scatter_x.label();
    let y_label = app.benchmarks_app.scatter_y.label();

    // Generate evenly-spaced tick labels for an axis.
    // ratatui distributes labels uniformly across the axis, so values must be evenly spaced.
    let make_ticks = |lo: f64, hi: f64, use_log: bool, n: usize| -> Vec<String> {
        let n = n.max(2);
        let step = (hi - lo) / (n - 1) as f64;
        let raw: Vec<f64> = (0..n).map(|i| lo + step * i as f64).collect();

        if use_log {
            // Format log-scale ticks: convert back to real values, ensure no duplicates
            let reals: Vec<f64> = raw.iter().map(|v| v.exp()).collect();
            // Pick precision that avoids duplicate labels
            for decimals in 0..=3 {
                let labels: Vec<String> = reals
                    .iter()
                    .map(|v| {
                        if decimals == 0 && *v >= 1.0 {
                            format!("{}", v.round() as i64)
                        } else {
                            format!("{:.prec$}", v, prec = decimals)
                        }
                    })
                    .collect();
                let unique: std::collections::HashSet<&String> = labels.iter().collect();
                if unique.len() == labels.len() {
                    return labels;
                }
            }
            // Fallback: 3 decimal places
            reals.iter().map(|v| format!("{:.3}", v)).collect()
        } else {
            raw.iter()
                .map(|v| {
                    if v.fract().abs() < 0.01 {
                        format!("{}", v.round() as i64)
                    } else {
                        format!("{:.1}", v)
                    }
                })
                .collect()
        }
    };

    let x_ticks = make_ticks(x_bounds[0], x_bounds[1], x_log, num_ticks);
    let y_ticks = make_ticks(y_bounds[0], y_bounds[1], y_log, num_ticks);

    let x_suffix = if x_log { " [log]" } else { "" };
    let y_suffix = if y_log { " [log]" } else { "" };

    // Format average for display (use original scale for log axes)
    let fmt_avg = |avg: f64, use_log: bool| -> String {
        let v = if use_log { avg.exp() } else { avg };
        if v >= 100.0 {
            format!("{}", v.round() as i64)
        } else {
            format!("{:.1}", v)
        }
    };
    let avg_style = Style::default().fg(Color::Indexed(242));

    let x_title = Line::from(vec![
        Span::styled(
            format!("{x_label}{x_suffix}"),
            Style::default().fg(Color::Gray),
        ),
        Span::styled(format!("  avg:{}", fmt_avg(avg_x, x_log)), avg_style),
    ]);
    let y_title = Line::from(vec![
        Span::styled(
            format!("{y_label}{y_suffix}"),
            Style::default().fg(Color::Gray),
        ),
        Span::styled(format!("  avg:{}", fmt_avg(avg_y, y_log)), avg_style),
    ]);

    let compare_focused = app.benchmarks_app.focus == super::app::BenchmarkFocus::Compare;
    let scatter_border = if compare_focused {
        Color::Cyan
    } else {
        Color::DarkGray
    };
    let chart = Chart::new(datasets)
        .block(
            Block::default()
                .borders(Borders::ALL)
                .border_style(Style::default().fg(scatter_border))
                .title(format!(" {y_label} vs {x_label} ")),
        )
        .x_axis(
            Axis::default()
                .title(x_title)
                .style(Style::default().fg(Color::Gray))
                .bounds(x_bounds)
                .labels(x_ticks),
        )
        .y_axis(
            Axis::default()
                .title(y_title)
                .style(Style::default().fg(Color::Gray))
                .bounds(y_bounds)
                .labels(y_ticks),
        )
        .legend_position(None);

    f.render_widget(chart, chart_area);

    // Format a raw value for legend display
    let fmt_val = |v: f64| -> String {
        if v >= 100.0 {
            format!("{}", v.round() as i64)
        } else if v >= 1.0 {
            format!("{:.1}", v)
        } else {
            format!("{:.2}", v)
        }
    };

    // Legend box below the chart
    if let Some(leg_area) = legend_area {
        use crate::tui::widgets::comparison_legend::{ComparisonLegend, LegendEntry, LegendMetric};

        let entries: Vec<LegendEntry> = legend_entries
            .iter()
            .map(|(name, color, status, raw_x, raw_y)| {
                let (marker, fg): (&'static str, Color) = if *status > 0 {
                    ("\u{25cf} ", *color)
                } else {
                    ("\u{25cb} ", Color::DarkGray)
                };
                let x_str = raw_x.map(&fmt_val).unwrap_or_else(|| "\u{2014}".into());
                let y_str = raw_y.map(&fmt_val).unwrap_or_else(|| "\u{2014}".into());
                let suffix = if *status == 2 { " (off-chart)" } else { "" };
                let y_with_suffix = format!("{}{}", y_str, suffix);

                LegendEntry::new(name.clone(), fg)
                    .marker(marker)
                    .metrics(vec![
                        LegendMetric::new(x_label.to_string(), x_str),
                        LegendMetric::new(y_label.to_string(), y_with_suffix),
                    ])
            })
            .collect();

        ComparisonLegend::new(entries).render(f, leg_area);
    }
}