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
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
use ratatui::{
    layout::{Constraint, Direction, Layout, Rect},
    style::{Color, Modifier, Style},
    text::{Line, Span},
    widgets::{Block, Borders, Clear, List, ListItem, ListState, Paragraph, Wrap},
    Frame,
};

use super::compare::{draw_h2h_table_generic, draw_scatter};
use crate::formatting::format_tokens;
use crate::formatting::truncate;
use crate::tui::app::App;
use crate::tui::ui::{caret, centered_rect, centered_rect_fixed, focus_border};
use crate::tui::widgets::scrollable_panel::ScrollablePanel;

/// Color palette for selected models in comparison mode.
pub(crate) fn compare_colors(index: usize) -> Color {
    const PALETTE: [Color; 8] = [
        Color::Red,
        Color::Green,
        Color::Blue,
        Color::Yellow,
        Color::Magenta,
        Color::Cyan,
        Color::LightRed,
        Color::LightGreen,
    ];
    PALETTE[index % PALETTE.len()]
}

pub(in crate::tui) fn draw_benchmarks_main(f: &mut Frame, area: Rect, app: &mut App) {
    let in_compare = app.selections.len() >= 2;

    if in_compare {
        // Compare mode: compact list (30%, min 35 chars) | comparison (remainder), full height
        let list_w = (area.width * 30 / 100).max(35);
        let h_chunks = Layout::default()
            .direction(Direction::Horizontal)
            .constraints([Constraint::Length(list_w), Constraint::Min(0)])
            .split(area);

        if app.benchmarks_app.show_creators_in_compare {
            draw_benchmark_creators(f, h_chunks[0], app);
        } else {
            draw_benchmark_list_compact(f, h_chunks[0], app);
        }

        // Comparison panel: sub-tab bar + view
        let v_chunks = Layout::default()
            .direction(Direction::Vertical)
            .constraints([Constraint::Length(1), Constraint::Min(0)])
            .split(h_chunks[1]);

        draw_benchmark_subtab_bar(f, v_chunks[0], &app.benchmarks_app);

        match app.benchmarks_app.bottom_view {
            super::app::BottomView::H2H => {
                draw_h2h_table_generic(f, v_chunks[1], app);
            }
            super::app::BottomView::Scatter => {
                draw_scatter(f, v_chunks[1], app);
            }
            super::app::BottomView::Radar => {
                super::radar::draw_radar(f, v_chunks[1], app);
            }
            super::app::BottomView::Detail => {
                draw_benchmark_detail(f, v_chunks[1], app);
            }
        }
    } else {
        // Browse mode: creators (20%) | list (40%) | detail (40%)
        let h_chunks = Layout::default()
            .direction(Direction::Horizontal)
            .constraints([
                Constraint::Percentage(20),
                Constraint::Percentage(40),
                Constraint::Percentage(40),
            ])
            .split(area);

        draw_benchmark_creators(f, h_chunks[0], app);
        draw_benchmark_list(f, h_chunks[1], app);
        draw_benchmark_detail(f, h_chunks[2], app);
    }

    // Detail overlay (drawn last, on top of everything)
    if app.benchmarks_app.show_detail_overlay && app.selections.len() >= 2 {
        draw_detail_overlay(f, area, app);
    }

    // Sort picker popup
    if app.benchmarks_app.show_sort_picker {
        draw_sort_picker(f, area, &app.benchmarks_app);
    }
}

fn draw_benchmark_subtab_bar(f: &mut Frame, area: Rect, bench_app: &super::app::BenchmarksApp) {
    use super::app::BottomView;
    let views = [
        ("H2H", BottomView::H2H),
        ("Scatter", BottomView::Scatter),
        ("Radar", BottomView::Radar),
    ];
    let mut spans = Vec::new();
    for (label, view) in &views {
        let style = if bench_app.bottom_view == *view {
            Style::default()
                .fg(Color::Cyan)
                .add_modifier(Modifier::BOLD)
        } else {
            Style::default().fg(Color::DarkGray)
        };
        spans.push(Span::styled(format!(" [{}] ", label), style));
    }
    f.render_widget(Paragraph::new(Line::from(spans)), area);
}

fn draw_benchmark_creators(f: &mut Frame, area: Rect, app: &mut App) {
    use super::app::{
        BenchmarkFocus, CreatorGrouping, CreatorListItem, CreatorRegion, CreatorType,
    };

    let bench_app = &mut app.benchmarks_app;

    let is_focused = bench_app.focus == BenchmarkFocus::Creators;
    let border_style = focus_border(is_focused);

    let source_indicator = match bench_app.source_filter {
        super::app::SourceFilter::All => String::new(),
        filter => format!(" [{}]", filter.label()),
    };
    let reasoning_indicator = {
        let label = bench_app.reasoning_filter.label();
        if label.is_empty() {
            String::new()
        } else {
            format!(" [{}]", label)
        }
    };
    let creators_title = format!(" Creators{}{} ", source_indicator, reasoning_indicator);

    let outer_block = Block::default()
        .borders(Borders::ALL)
        .border_style(border_style)
        .title(creators_title);
    let inner_area = outer_block.inner(area);
    f.render_widget(outer_block, area);

    // Grouping toggle indicators
    let rgn_active = bench_app.creator_grouping == CreatorGrouping::ByRegion;
    let rgn_color = if rgn_active {
        Color::Yellow
    } else {
        Color::DarkGray
    };

    let typ_active = bench_app.creator_grouping == CreatorGrouping::ByType;
    let typ_color = if typ_active {
        Color::Magenta
    } else {
        Color::DarkGray
    };

    let filter_line = Line::from(vec![
        Span::styled("[5]", Style::default().fg(rgn_color)),
        Span::raw(if rgn_active { "Region " } else { "Rgn " }),
        Span::styled("[6]", Style::default().fg(typ_color)),
        Span::raw("Type"),
    ]);

    let chunks = Layout::default()
        .direction(Direction::Vertical)
        .constraints([Constraint::Length(1), Constraint::Min(0)])
        .split(inner_area);

    f.render_widget(Paragraph::new(filter_line), chunks[0]);

    // Available width for creator items (inner area minus highlight symbol "  " or "> ")
    let item_width = inner_area.width.saturating_sub(2) as usize;

    let items: Vec<ListItem> = bench_app
        .creator_list_items
        .iter()
        .map(|item| match item {
            CreatorListItem::All => {
                let count = bench_app.filtered_creator_count();
                ListItem::new(Line::from(vec![
                    Span::styled("All", Style::default().fg(Color::Green)),
                    Span::raw(format!(" ({})", count)),
                ]))
            }
            CreatorListItem::GroupHeader(label) => {
                // Match models panel: full-width colored header with trailing ───
                let header_color = match bench_app.creator_grouping {
                    CreatorGrouping::ByRegion => {
                        CreatorRegion::from_label(label).map_or(Color::DarkGray, |r| r.color())
                    }
                    CreatorGrouping::ByType => {
                        CreatorType::from_label(label).map_or(Color::DarkGray, |t| t.color())
                    }
                    _ => Color::DarkGray,
                };
                let label_len = label.len() + 4; // "── " + label + " "
                let trailing = if item_width > label_len {
                    "\u{2500}".repeat(item_width - label_len)
                } else {
                    String::new()
                };
                let text = format!("\u{2500}\u{2500} {} {}", label, trailing);
                ListItem::new(text).style(
                    Style::default()
                        .fg(header_color)
                        .add_modifier(Modifier::BOLD),
                )
            }
            CreatorListItem::Creator(slug) => {
                let (display_name, count) = bench_app.creator_display(slug);
                // When grouped, show a colored tag for the creator's classification
                let tag = match bench_app.creator_grouping {
                    CreatorGrouping::ByRegion => {
                        let r = CreatorRegion::from_creator(slug);
                        Some((r.label(), r.color()))
                    }
                    CreatorGrouping::ByType => {
                        let t = CreatorType::from_creator(slug);
                        Some((t.label(), t.color()))
                    }
                    CreatorGrouping::None => None,
                };
                let count_str = format!("({})", count);
                let tag_len = tag.as_ref().map_or(0, |(l, _)| l.len() + 1);
                let overhead = count_str.len() + 1 + tag_len;
                let max_name = item_width.saturating_sub(overhead);
                let name = truncate(display_name, max_name);
                let mut spans = vec![
                    Span::raw(format!("{} ", name)),
                    Span::styled(count_str, Style::default().fg(Color::Gray)),
                ];
                if let Some((label, color)) = tag {
                    spans.push(Span::raw(" "));
                    spans.push(Span::styled(label, Style::default().fg(color)));
                }
                ListItem::new(Line::from(spans))
            }
        })
        .collect();

    let caret = caret(is_focused);
    let list = List::new(items)
        .highlight_style(
            Style::default()
                .fg(Color::Yellow)
                .add_modifier(Modifier::BOLD),
        )
        .highlight_symbol(caret);

    let mut state = bench_app.creator_list_state;
    f.render_stateful_widget(list, chunks[1], &mut state);
}

/// Compact list for compare mode: selection marker + name only, full height.
fn draw_benchmark_list_compact(f: &mut Frame, area: Rect, app: &mut App) {
    use super::app::BenchmarkFocus;

    let bench_app = &mut app.benchmarks_app;
    let store = &app.benchmark_store;

    let is_focused = bench_app.focus == BenchmarkFocus::List;
    let border_style = focus_border(is_focused);

    let sort_dir = if bench_app.sort_descending {
        "\u{2193}"
    } else {
        "\u{2191}"
    };
    let sort_indicator = format!(" {}{}", sort_dir, bench_app.sort_column.label());

    let source_indicator = match bench_app.source_filter {
        super::app::SourceFilter::All => String::new(),
        filter => format!(" [{}]", filter.label()),
    };

    let reasoning_indicator = {
        let label = bench_app.reasoning_filter.label();
        if label.is_empty() {
            String::new()
        } else {
            format!(" [{}]", label)
        }
    };

    let loading_suffix = if bench_app.loading { " loading..." } else { "" };

    let title = if bench_app.search_query.is_empty() {
        format!(
            " Models ({}){}{}{}{} ",
            bench_app.filtered_indices.len(),
            source_indicator,
            reasoning_indicator,
            sort_indicator,
            loading_suffix
        )
    } else {
        format!(
            " Models ({}) [/{}]{}{}{}{} ",
            bench_app.filtered_indices.len(),
            bench_app.search_query,
            source_indicator,
            reasoning_indicator,
            sort_indicator,
            loading_suffix
        )
    };

    let outer_block = Block::default()
        .borders(Borders::ALL)
        .border_style(border_style)
        .title(title);
    let inner_area = outer_block.inner(area);
    f.render_widget(outer_block, area);

    let caret = caret(is_focused);
    let entries = store.entries();

    // Extra columns: marker(2) + caret(2) + reasoning(3) + source(2) + optional region/type
    let show_region = bench_app.creator_grouping == super::app::CreatorGrouping::ByRegion;
    let show_type = bench_app.creator_grouping == super::app::CreatorGrouping::ByType;
    let extra_w: u16 = 2 + 2 + 3 + 2 + if show_region || show_type { 4 } else { 0 };
    let name_width = inner_area.width.saturating_sub(extra_w) as usize;

    let items: Vec<ListItem> = bench_app
        .filtered_indices
        .iter()
        .enumerate()
        .map(|(display_idx, &entry_idx)| {
            let entry = &entries[entry_idx];
            let is_selected = display_idx == bench_app.selected;

            let style = if is_selected {
                Style::default()
                    .fg(Color::Yellow)
                    .add_modifier(Modifier::BOLD)
            } else {
                Style::default()
            };

            let prefix = if is_selected { caret } else { "  " };
            let mut row_spans: Vec<Span> = Vec::new();

            // Selection marker
            if let Some(sel_pos) = app.selections.iter().position(|&i| i == entry_idx) {
                row_spans.push(Span::styled(
                    "\u{25CF} ",
                    Style::default().fg(compare_colors(sel_pos)),
                ));
            } else {
                row_spans.push(Span::raw("  "));
            }

            row_spans.push(Span::styled(prefix, style));

            // Reasoning status indicator
            let (rs_label, rs_color) = match entry.reasoning_status {
                crate::benchmarks::ReasoningStatus::Reasoning => ("R  ", Color::Cyan),
                crate::benchmarks::ReasoningStatus::NonReasoning => ("NR ", Color::DarkGray),
                crate::benchmarks::ReasoningStatus::Adaptive => ("AR ", Color::Yellow),
                crate::benchmarks::ReasoningStatus::None => ("   ", Color::Reset),
            };
            row_spans.push(Span::styled(rs_label, Style::default().fg(rs_color)));

            // Source indicator (Open/Closed)
            let (src_label, src_color) = match app.open_weights_map.get(&entry.slug) {
                Some(true) => ("O ", Color::Green),
                Some(false) => ("C ", Color::Red),
                None => ("  ", Color::Reset),
            };
            row_spans.push(Span::styled(src_label, Style::default().fg(src_color)));

            // Region/Type indicator when grouping is active
            if show_region {
                let region = super::app::CreatorRegion::from_creator(&entry.creator);
                row_spans.push(Span::styled(
                    format!("{:<4}", region.short_label()),
                    Style::default().fg(region.color()),
                ));
            } else if show_type {
                let ct = super::app::CreatorType::from_creator(&entry.creator);
                row_spans.push(Span::styled(
                    format!("{:<4}", ct.short_label()),
                    Style::default().fg(ct.color()),
                ));
            }

            row_spans.push(Span::styled(
                truncate(&entry.display_name, name_width),
                style,
            ));
            ListItem::new(Line::from(row_spans))
        })
        .collect();

    let list = List::new(items)
        .highlight_style(
            Style::default()
                .fg(Color::Yellow)
                .add_modifier(Modifier::BOLD),
        )
        .highlight_symbol("");

    let mut state = bench_app.list_state;
    state.select(Some(bench_app.selected));
    f.render_stateful_widget(list, inner_area, &mut state);
}

fn draw_benchmark_list(f: &mut Frame, area: Rect, app: &mut App) {
    use super::app::BenchmarkFocus;

    let bench_app = &mut app.benchmarks_app;
    let store = &app.benchmark_store;

    let is_focused = bench_app.focus == BenchmarkFocus::List;
    let border_style = focus_border(is_focused);

    let sort_dir = if bench_app.sort_descending {
        "\u{2193}"
    } else {
        "\u{2191}"
    };
    let sort_indicator = format!(" {}{}", sort_dir, bench_app.sort_column.label());

    let source_indicator = match bench_app.source_filter {
        super::app::SourceFilter::All => String::new(),
        filter => format!(" [{}]", filter.label()),
    };

    let reasoning_indicator = {
        let label = bench_app.reasoning_filter.label();
        if label.is_empty() {
            String::new()
        } else {
            format!(" [{}]", label)
        }
    };

    let creator_label = bench_app.selected_creator_name().unwrap_or("Benchmarks");
    let loading_suffix = if bench_app.loading { " loading..." } else { "" };

    let title = if bench_app.search_query.is_empty() {
        format!(
            " {} ({}){}{}{}{} ",
            creator_label,
            bench_app.filtered_indices.len(),
            source_indicator,
            reasoning_indicator,
            sort_indicator,
            loading_suffix
        )
    } else {
        format!(
            " {} ({}) [/{}]{}{}{}{} ",
            creator_label,
            bench_app.filtered_indices.len(),
            bench_app.search_query,
            source_indicator,
            reasoning_indicator,
            sort_indicator,
            loading_suffix
        )
    };

    let outer_block = Block::default()
        .borders(Borders::ALL)
        .border_style(border_style)
        .title(title);
    let inner_area = outer_block.inner(area);
    f.render_widget(outer_block, area);

    // Dynamic columns based on active sort
    let visible_cols = bench_app.sort_column.visible_columns();

    // Compute dynamic name column width from available space
    let caret_w: u16 = 2;
    let reasoning_col_w: u16 = 3;
    let source_col_w: u16 = 2;
    let show_region = bench_app.creator_grouping == super::app::CreatorGrouping::ByRegion;
    let show_type = bench_app.creator_grouping == super::app::CreatorGrouping::ByType;
    let grouping_col_w: u16 = if show_region || show_type { 4 } else { 0 };
    let fixed_width: u16 = visible_cols
        .iter()
        .map(|col| benchmark_col_width(*col))
        .sum();
    let selection_w: u16 = if !app.selections.is_empty() { 2 } else { 0 };
    let name_width = (inner_area.width.saturating_sub(
        fixed_width + caret_w + selection_w + reasoning_col_w + source_col_w + grouping_col_w,
    ) as usize)
        .max(10);

    // Caret prefix for focused panel
    let caret = caret(is_focused);

    let header_style = Style::default()
        .fg(Color::Yellow)
        .add_modifier(Modifier::BOLD);
    let active_header_style = Style::default()
        .fg(Color::Cyan)
        .add_modifier(Modifier::BOLD);

    let has_selections = !app.selections.is_empty();
    let mut header_spans: Vec<Span> = Vec::new();
    if has_selections {
        header_spans.push(Span::raw("  ")); // align with selection marker column
    }
    header_spans.push(Span::raw("  "));
    header_spans.push(Span::styled("   ", header_style)); // reasoning indicator
    header_spans.push(Span::styled("  ", header_style)); // source indicator
    if show_region {
        header_spans.push(Span::styled("Rgn ", header_style));
    } else if show_type {
        header_spans.push(Span::styled("Typ ", header_style));
    }
    header_spans.extend(visible_cols.iter().map(|col| {
        let style = if *col == bench_app.sort_column {
            active_header_style
        } else {
            header_style
        };
        benchmark_col_header(*col, style, name_width)
    }));
    let header = ListItem::new(Line::from(header_spans));

    let entries = store.entries();
    let mut items: Vec<ListItem> = vec![header];

    for (display_idx, &entry_idx) in bench_app.filtered_indices.iter().enumerate() {
        let entry = &entries[entry_idx];
        let is_selected = display_idx == bench_app.selected;

        let style = if is_selected {
            Style::default()
                .fg(Color::Yellow)
                .add_modifier(Modifier::BOLD)
        } else {
            Style::default()
        };

        let prefix = if is_selected { caret } else { "  " };
        let mut row_spans: Vec<Span> = Vec::new();

        // Selection marker
        if let Some(sel_pos) = app.selections.iter().position(|&i| i == entry_idx) {
            row_spans.push(Span::styled(
                "\u{25CF} ",
                Style::default().fg(compare_colors(sel_pos)),
            ));
        } else if has_selections {
            row_spans.push(Span::raw("  "));
        }

        row_spans.push(Span::styled(prefix, style));

        // Reasoning status indicator
        let (rs_label, rs_color) = match entry.reasoning_status {
            crate::benchmarks::ReasoningStatus::Reasoning => ("R  ", Color::Cyan),
            crate::benchmarks::ReasoningStatus::NonReasoning => ("NR ", Color::DarkGray),
            crate::benchmarks::ReasoningStatus::Adaptive => ("AR ", Color::Yellow),
            crate::benchmarks::ReasoningStatus::None => ("   ", Color::Reset),
        };
        row_spans.push(Span::styled(rs_label, Style::default().fg(rs_color)));

        // Source indicator (Open/Closed)
        let (src_label, src_color) = match app.open_weights_map.get(&entry.slug) {
            Some(true) => ("O ", Color::Green),
            Some(false) => ("C ", Color::Red),
            None => ("  ", Color::Reset),
        };
        row_spans.push(Span::styled(src_label, Style::default().fg(src_color)));

        // Region/Type indicator when grouping is active
        if show_region {
            let region = super::app::CreatorRegion::from_creator(&entry.creator);
            row_spans.push(Span::styled(
                format!("{:<4}", region.short_label()),
                Style::default().fg(region.color()),
            ));
        } else if show_type {
            let ct = super::app::CreatorType::from_creator(&entry.creator);
            row_spans.push(Span::styled(
                format!("{:<4}", ct.short_label()),
                Style::default().fg(ct.color()),
            ));
        }

        row_spans.extend(
            visible_cols
                .iter()
                .map(|col| benchmark_col_value(entry, *col, style, name_width)),
        );
        items.push(ListItem::new(Line::from(row_spans)));
    }

    let list = List::new(items);
    let mut state = bench_app.list_state;
    // Offset by 1 for the header row
    state.select(Some(bench_app.selected + 1));
    f.render_stateful_widget(list, inner_area, &mut state);
}

fn draw_benchmark_detail(f: &mut Frame, area: Rect, app: &App) {
    use super::app::BenchmarkFocus;
    let bench_app = &app.benchmarks_app;
    let store = &app.benchmark_store;
    let focused = bench_app.focus == BenchmarkFocus::Details;

    let entry = match bench_app.current_entry(store) {
        Some(e) => e,
        None => {
            let lines = vec![Line::from(Span::styled(
                "No benchmark selected",
                Style::default().fg(Color::DarkGray),
            ))];
            ScrollablePanel::new("Details", lines, &bench_app.detail_scroll, focused)
                .render(f, area);
            return;
        }
    };

    let inner_w = area.width.saturating_sub(2);
    let lines = build_benchmark_detail_lines(inner_w, entry, app);
    ScrollablePanel::new("Details", lines, &bench_app.detail_scroll, focused).render(f, area);
}

fn build_benchmark_detail_lines<'a>(
    width: u16,
    entry: &'a crate::benchmarks::BenchmarkEntry,
    app: &'a App,
) -> Vec<Line<'a>> {
    let mut lines: Vec<Line> = Vec::new();

    // Dynamic column widths via ratatui's constraint solver
    let cw = ColumnWidths::from_width(width);

    // Name + creator + metadata on first lines
    let creator_display = if !entry.creator_name.is_empty() {
        &entry.creator_name
    } else {
        &entry.creator
    };
    let region = super::app::CreatorRegion::from_creator(&entry.creator);
    let creator_type = super::app::CreatorType::from_creator(&entry.creator);

    // Line 1: Name
    lines.push(Line::from(Span::styled(
        &entry.display_name,
        Style::default()
            .fg(Color::Cyan)
            .add_modifier(Modifier::BOLD),
    )));
    // Metadata rows (2-wide, dynamic)
    let em = "\u{2014}";
    let (source_label, source_color) = match app.open_weights_map.get(&entry.slug) {
        Some(true) => ("Open", Color::Green),
        Some(false) => ("Closed", Color::Red),
        None => (em, Color::DarkGray),
    };
    push_meta_row(
        &mut lines,
        &cw,
        ("Creator", creator_display, Color::Reset),
        ("Source", source_label, source_color),
    );
    push_meta_row(
        &mut lines,
        &cw,
        ("Region", region.label(), Color::Reset),
        ("Type", creator_type.label(), Color::Reset),
    );
    let date_str = entry.release_date.as_deref().unwrap_or(em);
    let (reasoning_label, reasoning_color) = {
        use crate::benchmarks::ReasoningStatus;
        match entry.reasoning_status {
            ReasoningStatus::Reasoning => ("Reasoning", Color::Cyan),
            ReasoningStatus::NonReasoning => ("Non-reasoning", Color::DarkGray),
            ReasoningStatus::Adaptive => ("Adaptive", Color::Yellow),
            ReasoningStatus::None => (em, Color::DarkGray),
        }
    };
    push_meta_row(
        &mut lines,
        &cw,
        ("Released", date_str, Color::Reset),
        ("Reason", reasoning_label, reasoning_color),
    );
    // Effort + Variant (only if present)
    let has_effort = entry.effort_level.is_some();
    let has_variant = entry.variant_tag.is_some();
    if has_effort || has_variant {
        let effort_str = entry.effort_level.as_deref().unwrap_or(em);
        let variant_str = entry.variant_tag.as_deref().unwrap_or(em);
        push_meta_row(
            &mut lines,
            &cw,
            ("Effort", effort_str, Color::Reset),
            ("Variant", variant_str, Color::Reset),
        );
    }
    // Tools + Context
    let tools_str = match entry.tool_call {
        Some(true) => "Yes",
        Some(false) => "No",
        None => em,
    };
    let tools_color = match entry.tool_call {
        Some(true) => Color::Green,
        Some(false) => Color::DarkGray,
        None => Color::DarkGray,
    };
    let ctx_str = entry
        .context_window
        .map(format_tokens)
        .unwrap_or_else(|| em.to_string());
    push_meta_row(
        &mut lines,
        &cw,
        ("Tools", tools_str, tools_color),
        ("Context", &ctx_str, Color::Reset),
    );
    // Max output
    let out_str = entry
        .max_output
        .map(format_tokens)
        .unwrap_or_else(|| em.to_string());
    push_meta_row(
        &mut lines,
        &cw,
        ("Output", &out_str, Color::Reset),
        ("", "", Color::Reset),
    );

    // Composite Indexes (0-100 scale, higher is better)
    lines.push(Line::from(""));
    push_section_header(&mut lines, "Indexes (0\u{2013}100, \u{2191} better)");
    let int_idx = fmt_idx(entry.intelligence_index);
    let cod_idx = fmt_idx(entry.coding_index);
    push_detail_row(
        &mut lines,
        &cw,
        "Intelligence",
        &int_idx,
        "Coding",
        &cod_idx,
    );
    let math_idx = fmt_idx(entry.math_index);
    push_detail_row(&mut lines, &cw, "Math", &math_idx, "", "");

    // Benchmark Scores (percentage, higher is better)
    lines.push(Line::from(""));
    push_section_header(&mut lines, "Benchmarks (%, \u{2191} better)");
    let gpqa = fmt_pct(entry.gpqa);
    let mmlu = fmt_pct(entry.mmlu_pro);
    push_detail_row(&mut lines, &cw, "GPQA", &gpqa, "MMLU-Pro", &mmlu);
    let hle = fmt_pct(entry.hle);
    let livecode = fmt_pct(entry.livecodebench);
    push_detail_row(&mut lines, &cw, "HLE", &hle, "LiveCode", &livecode);
    let scicode = fmt_pct(entry.scicode);
    let ifbench = fmt_pct(entry.ifbench);
    push_detail_row(&mut lines, &cw, "SciCode", &scicode, "IFBench", &ifbench);
    let terminal = fmt_pct(entry.terminalbench_hard);
    let tau2 = fmt_pct(entry.tau2);
    push_detail_row(&mut lines, &cw, "Terminal", &terminal, "Tau2", &tau2);
    let lcr = fmt_pct(entry.lcr);
    let math500 = fmt_pct(entry.math_500);
    push_detail_row(&mut lines, &cw, "LCR", &lcr, "MATH-500", &math500);
    let aime = fmt_pct(entry.aime);
    let aime25 = fmt_pct(entry.aime_25);
    push_detail_row(&mut lines, &cw, "AIME", &aime, "AIME'25", &aime25);

    // Performance (speed: higher better, TTFT/TTFAT: lower better)
    lines.push(Line::from(""));
    push_section_header(
        &mut lines,
        "Performance (Speed \u{2191}, TTFT/TTFAT \u{2193})",
    );
    let tps_str = entry
        .output_tps
        .map(|v| format!("{:.0} tok/s", v))
        .unwrap_or_else(|| em.to_string());
    let ttft_str = entry
        .ttft
        .map(|v| format!("{:.2}s", v))
        .unwrap_or_else(|| em.to_string());
    let ttfat_str = entry
        .ttfat
        .map(|v| format!("{:.2}s", v))
        .unwrap_or_else(|| em.to_string());
    push_detail_row(&mut lines, &cw, "Speed", &tps_str, "TTFT", &ttft_str);
    push_detail_row(&mut lines, &cw, "TTFAT", &ttfat_str, "", "");

    // Pricing ($/M tokens, lower is better)
    lines.push(Line::from(""));
    push_section_header(&mut lines, "Pricing ($/M tokens, \u{2193} better)");
    let input_price = fmt_price(entry.price_input);
    let output_price = fmt_price(entry.price_output);
    push_detail_row(
        &mut lines,
        &cw,
        "Input",
        &input_price,
        "Output",
        &output_price,
    );
    let blended_str = entry
        .price_blended
        .map(|v| format!("${:.2}", v))
        .unwrap_or_else(|| em.to_string());
    push_detail_row(&mut lines, &cw, "Blended", &blended_str, "", "");

    // Keybinding hints
    lines.push(Line::from(""));
    lines.push(Line::from(vec![
        Span::styled("c ", Style::default().fg(Color::Yellow)),
        Span::styled("copy  ", Style::default().fg(Color::DarkGray)),
        Span::styled("o ", Style::default().fg(Color::Yellow)),
        Span::styled("open AA", Style::default().fg(Color::DarkGray)),
    ]));

    lines
}

fn draw_detail_overlay(f: &mut Frame, area: Rect, app: &App) {
    // Centered rect: 60% width, 75% height
    let overlay_area = centered_rect(60, 75, area);

    // Clear background
    f.render_widget(Clear, overlay_area);

    let bench_app = &app.benchmarks_app;
    let store = &app.benchmark_store;

    let block = Block::default()
        .borders(Borders::ALL)
        .border_style(Style::default().fg(Color::Cyan))
        .title(" Model Detail (Esc to close) ");

    let entry = match bench_app.current_entry(store) {
        Some(e) => e,
        None => {
            let msg = Paragraph::new("No benchmark selected").block(block);
            f.render_widget(msg, overlay_area);
            return;
        }
    };

    let inner = block.inner(overlay_area);
    f.render_widget(block, overlay_area);
    let lines = build_benchmark_detail_lines(inner.width, entry, app);
    let paragraph = Paragraph::new(lines).wrap(Wrap { trim: false });
    f.render_widget(paragraph, inner);
}

/// Push a section header line like "─── Title ───"
fn push_section_header(lines: &mut Vec<Line>, title: &str) {
    lines.push(Line::from(Span::styled(
        format!(
            "\u{2500}\u{2500}\u{2500} {} \u{2500}\u{2500}\u{2500}",
            title
        ),
        Style::default().fg(Color::DarkGray),
    )));
}

struct ColumnWidths {
    indent: u16,
    label: u16,
    value: u16,
    label2: u16,
}

impl ColumnWidths {
    fn from_width(width: u16) -> Self {
        let indent: u16 = 2;
        let usable = width.saturating_sub(indent);
        let chunks = Layout::default()
            .direction(Direction::Horizontal)
            .constraints([
                Constraint::Percentage(28),
                Constraint::Percentage(22),
                Constraint::Percentage(28),
                Constraint::Percentage(22),
            ])
            .split(Rect::new(0, 0, usable, 1));
        Self {
            indent,
            label: chunks[0].width.max(8),
            value: chunks[1].width.max(6),
            label2: chunks[2].width.max(8),
        }
    }
}

fn push_meta_row(
    lines: &mut Vec<Line>,
    cw: &ColumnWidths,
    left: (&str, &str, Color),
    right: (&str, &str, Color),
) {
    let style_for = |c: Color| {
        if c == Color::Reset {
            Style::default()
        } else {
            Style::default().fg(c)
        }
    };

    let mut spans = vec![
        Span::styled(
            format!(
                "{:indent$}{:<w$}",
                "",
                left.0,
                indent = cw.indent as usize,
                w = cw.label as usize
            ),
            Style::default().fg(Color::Gray),
        ),
        Span::styled(
            format!("{:<w$}", left.1, w = cw.value as usize),
            style_for(left.2),
        ),
    ];

    if !right.0.is_empty() {
        spans.push(Span::styled(
            format!("{:<w$}", right.0, w = cw.label2 as usize),
            Style::default().fg(Color::Gray),
        ));
        spans.push(Span::styled(right.1.to_string(), style_for(right.2)));
    }

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

fn push_detail_row(
    lines: &mut Vec<Line>,
    cw: &ColumnWidths,
    l1: &str,
    v1: &str,
    l2: &str,
    v2: &str,
) {
    let em = "\u{2014}";
    let val_color = |s: &str| {
        if s == em {
            Color::DarkGray
        } else {
            Color::White
        }
    };

    let mut spans = vec![
        Span::styled(
            format!(
                "{:indent$}{:<w$}",
                "",
                l1,
                indent = cw.indent as usize,
                w = cw.label as usize
            ),
            Style::default().fg(Color::Gray),
        ),
        Span::styled(
            format!("{:<w$}", v1, w = cw.value as usize),
            Style::default().fg(val_color(v1)),
        ),
    ];

    if !l2.is_empty() {
        spans.push(Span::styled(
            format!("{:<w$}", l2, w = cw.label2 as usize),
            Style::default().fg(Color::Gray),
        ));
        spans.push(Span::styled(
            v2.to_string(),
            Style::default().fg(val_color(v2)),
        ));
    }

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

fn fmt_idx(value: Option<f64>) -> String {
    match value {
        Some(v) => format!("{:.1}", v),
        None => "\u{2014}".to_string(),
    }
}

/// Format a 0-1 decimal score as a percentage
fn fmt_pct(value: Option<f64>) -> String {
    match value {
        Some(v) => format!("{:.1}%", v * 100.0),
        None => "\u{2014}".to_string(),
    }
}

/// Format a price value for list columns (right-aligned, 9 chars)
fn fmt_col_price(value: Option<f64>) -> String {
    match value {
        Some(v) => format!("{:>8.2}$", v),
        None => format!("{:>9}", "\u{2014}"),
    }
}

/// Format a price value
fn fmt_price(value: Option<f64>) -> String {
    match value {
        Some(v) if v.fract() == 0.0 => format!("${:.0}", v),
        Some(v) => format!("${:.2}", v),
        None => "\u{2014}".to_string(),
    }
}

/// Format a 0-100 index for list columns (right-aligned, 6 chars)
fn fmt_col_idx(value: Option<f64>) -> String {
    match value {
        Some(v) => format!("{:>6.1}", v),
        None => format!("{:>6}", "\u{2014}"),
    }
}

/// Format a 0-1 decimal score as % for list columns (right-aligned, 6 chars)
fn fmt_col_pct(value: Option<f64>) -> String {
    match value {
        Some(v) => format!("{:>5.1}%", v * 100.0),
        None => format!("{:>6}", "\u{2014}"),
    }
}

fn fmt_speed(value: Option<f64>) -> String {
    match value {
        Some(v) => format!("{:>7.0}", v),
        None => format!("{:>7}", "\u{2014}"),
    }
}

fn fmt_col_ttft(value: Option<f64>) -> String {
    match value {
        Some(v) => format!("{:>6.2}s", v),
        None => format!("{:>7}", "\u{2014}"),
    }
}

fn fmt_col_date(value: Option<&str>) -> String {
    match value {
        Some(d) => format!("{:>11}", d),
        None => format!("{:>11}", "\u{2014}"),
    }
}

/// Fixed width for a non-Name column.
fn benchmark_col_width(col: super::app::BenchmarkSortColumn) -> u16 {
    use super::app::BenchmarkSortColumn::*;
    match col {
        Name => 0, // dynamic
        Speed | Ttft | Ttfat => 7,
        PriceInput | PriceOutput | PriceBlended => 9,
        ReleaseDate => 11,
        _ => 6, // all index/percentage columns
    }
}

/// Render a column header span for the given sort column
fn benchmark_col_header(
    col: super::app::BenchmarkSortColumn,
    style: Style,
    name_width: usize,
) -> Span<'static> {
    use super::app::BenchmarkSortColumn::*;
    match col {
        Name => Span::styled(format!("{:<width$}", "Name", width = name_width), style),
        Intelligence => Span::styled(format!("{:>6}", "Intel"), style),
        Coding => Span::styled(format!("{:>6}", "Code"), style),
        Math => Span::styled(format!("{:>6}", "Math"), style),
        Gpqa => Span::styled(format!("{:>6}", "GPQA"), style),
        MMLUPro => Span::styled(format!("{:>6}", "MMLU"), style),
        Hle => Span::styled(format!("{:>6}", "HLE"), style),
        LiveCode => Span::styled(format!("{:>6}", "LCode"), style),
        SciCode => Span::styled(format!("{:>6}", "SciCd"), style),
        Terminal => Span::styled(format!("{:>6}", "Term"), style),
        IFBench => Span::styled(format!("{:>6}", "IFB"), style),
        Lcr => Span::styled(format!("{:>6}", "LCR"), style),
        Tau2 => Span::styled(format!("{:>6}", "Tau2"), style),
        Speed => Span::styled(format!("{:>7}", "Tok/s"), style),
        Ttft => Span::styled(format!("{:>7}", "TTFT"), style),
        Ttfat => Span::styled(format!("{:>7}", "TTFAT"), style),
        PriceInput => Span::styled(format!("{:>9}", "In $/M"), style),
        PriceOutput => Span::styled(format!("{:>9}", "Out $/M"), style),
        PriceBlended => Span::styled(format!("{:>9}", "Bld $/M"), style),
        ReleaseDate => Span::styled(format!("{:>11}", "Released"), style),
    }
}

/// Render a column value span for the given sort column
fn benchmark_col_value<'a>(
    entry: &crate::benchmarks::BenchmarkEntry,
    col: super::app::BenchmarkSortColumn,
    style: Style,
    name_width: usize,
) -> Span<'a> {
    use super::app::BenchmarkSortColumn::*;
    match col {
        Name => Span::styled(
            format!(
                "{:<width$}",
                truncate(&entry.display_name, name_width.saturating_sub(1)),
                width = name_width
            ),
            style,
        ),
        Intelligence => Span::styled(fmt_col_idx(entry.intelligence_index), style),
        Coding => Span::styled(fmt_col_idx(entry.coding_index), style),
        Math => Span::styled(fmt_col_idx(entry.math_index), style),
        Gpqa => Span::styled(fmt_col_pct(entry.gpqa), style),
        MMLUPro => Span::styled(fmt_col_pct(entry.mmlu_pro), style),
        Hle => Span::styled(fmt_col_pct(entry.hle), style),
        LiveCode => Span::styled(fmt_col_pct(entry.livecodebench), style),
        SciCode => Span::styled(fmt_col_pct(entry.scicode), style),
        Terminal => Span::styled(fmt_col_pct(entry.terminalbench_hard), style),
        IFBench => Span::styled(fmt_col_pct(entry.ifbench), style),
        Lcr => Span::styled(fmt_col_pct(entry.lcr), style),
        Tau2 => Span::styled(fmt_col_pct(entry.tau2), style),
        Speed => Span::styled(fmt_speed(entry.output_tps), style),
        Ttft => Span::styled(fmt_col_ttft(entry.ttft), style),
        Ttfat => Span::styled(fmt_col_ttft(entry.ttfat), style),
        PriceInput => Span::styled(fmt_col_price(entry.price_input), style),
        PriceOutput => Span::styled(fmt_col_price(entry.price_output), style),
        PriceBlended => Span::styled(fmt_col_price(entry.price_blended), style),
        ReleaseDate => Span::styled(fmt_col_date(entry.release_date.as_deref()), style),
    }
}

fn draw_sort_picker(f: &mut Frame, area: Rect, bench_app: &super::app::BenchmarksApp) {
    use super::app::BenchmarkSortColumn;

    let columns = BenchmarkSortColumn::ALL;
    let selected = bench_app.sort_picker_selected;

    // Fixed-size popup: 30 wide, enough for all items + border
    let height = (columns.len() as u16 + 2).min(area.height);
    let width = 30u16.min(area.width);
    let popup_area = centered_rect_fixed(width, height, area);

    f.render_widget(Clear, popup_area);

    let items: Vec<ListItem> = columns
        .iter()
        .map(|col| {
            let marker = if *col == bench_app.sort_column {
                let arrow = if bench_app.sort_descending {
                    "\u{25bc}"
                } else {
                    "\u{25b2}"
                };
                format!(" {arrow}")
            } else {
                String::new()
            };
            ListItem::new(Line::from(format!(" {}{}", col.picker_label(), marker)))
        })
        .collect();

    let mut list_state = ListState::default();
    list_state.select(Some(selected));

    let list = List::new(items)
        .block(
            Block::default()
                .borders(Borders::ALL)
                .border_style(Style::default().fg(Color::Cyan))
                .title(" Sort By "),
        )
        .highlight_style(
            Style::default()
                .fg(Color::Cyan)
                .add_modifier(Modifier::BOLD),
        );

    f.render_stateful_widget(list, popup_area, &mut list_state);
}