triage-tui 0.4.1

TUI to monitor parallel Claude Code and Codex CLI sessions across tmux panes — triage by attention priority
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
use std::sync::mpsc;
use std::time::SystemTime;

use ratatui::Frame;
use ratatui::layout::{Constraint, Direction, Layout, Rect};
use ratatui::style::{Color, Modifier, Style};
use ratatui::text::{Line, Span, Text};
use ratatui::widgets::{Block, Borders, Cell, Paragraph, Row, Table, TableState, Wrap};

use std::collections::{HashMap, HashSet};

use crate::auditor::Verdict;
use crate::classifier::idle_age;
use crate::config::Config;
use crate::models::{ApprovalMode, AttentionState, Provider, Session, session_display_label};
use crate::persist::{self, AliasKey, MuteKey};
use crate::transcript::DigestCache;

/// Cached live capture of one tmux pane for the preview rail (TRI-138).
/// `raw` is the `-e` ANSI capture; `text` is its parsed ratatui form. We keep
/// `raw` so successive ticks can skip the (re)parse when the pane is unchanged.
pub struct PanePreview {
    pub pane_id: String,
    pub raw: String,
    pub text: Text<'static>,
}

/// Where the preview panel docks (TRI-138). Right pairs with a compact table
/// (HEADLINE/CWD dropped, since the live pane replaces them); Bottom keeps the
/// full-width table and docks the preview below it like the detail pane.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
pub enum PreviewPos {
    #[default]
    Right,
    Bottom,
}

pub struct AppState {
    pub sessions: Vec<Session>,
    pub selected: TableState,
    pub detail_open: bool,
    /// Live preview rail (TRI-138). Toggle with `p`; stays on across
    /// navigation and retargets to the selected row's pane each tick.
    /// Mutually exclusive with `detail_open` (different regions, but we keep
    /// the layout to two zones at most).
    pub preview_open: bool,
    /// Most recent capture of the selected pane, or `None` when the rail is
    /// open but the selected row has no tmux pane (→ placeholder) or nothing
    /// has been captured yet.
    pub preview: Option<PanePreview>,
    /// Where the preview panel sits when open. `>` flips it. Right = beside a
    /// compact table; Bottom = below the full-width table.
    pub preview_pos: PreviewPos,
    pub status_msg: Option<String>,
    pub digest_cache: DigestCache,
    pub codex_cache: crate::codex::CodexDigestCache,
    /// Triage-local row aliases keyed by provider + alias session id. For
    /// Codex this is the root thread id so aliases follow spawned child
    /// threads without following unrelated pane reuse.
    /// These never mutate Claude/Codex/tmux state.
    pub aliases: HashMap<AliasKey, String>,
    /// (cwd, started_at_ms) → time of mute. Keyed on a stable identity rather
    /// than pid so the entries survive a triage restart and don't accidentally
    /// re-mute a recycled pid.
    pub muted: HashMap<MuteKey, SystemTime>,
    /// In-memory set of sessions to fire a "finished" notification for on each
    /// transition into `JustFinished` (T-81). Sticky — only the user can
    /// clear an entry by pressing `w` again on the row. Not persisted across
    /// restarts; a watch only makes sense while the session exists.
    pub watched: HashSet<MuteKey>,
    /// (cwd, started_at_ms) of sessions pinned to the top via `*`. Sticky —
    /// only `*` clears an entry — and persisted across restarts.
    pub pinned: HashSet<MuteKey>,
    /// pid → most recently observed AttentionState. Used to detect transitions
    /// (e.g. into `Blocked`) so we can fire a desktop notification once per
    /// transition rather than on every refresh while the session stays blocked.
    pub last_states: HashMap<u32, AttentionState>,
    /// False until the first discovery refresh has established a state
    /// baseline. Prevents restarting triage from replaying notifications for
    /// sessions that were already Blocked or Error before the TUI launched.
    pub notifications_armed: bool,
    /// Which mechanism Claude `a`/`d` use to deliver an approval. Configured
    /// via `[approval].mode`; Codex approvals always use the tmux path.
    pub approval_mode: ApprovalMode,
    /// Autonomous mode (T-56). Toggle with `A`. When on, the refresh loop
    /// spawns a `claude -p` auditor for each `waiting` session and routes
    /// APPROVE/DENY through the same machinery as manual `a`/`d`.
    pub autonomous: bool,
    /// ntfy phone push gate (T-79). Toggle with `p`. When off, app-driven
    /// ntfy POSTs are suppressed — Mac local banners still fire. The
    /// `triage notify` CLI (T-77) bypasses this gate (user-initiated pings
    /// are explicit and always pass through).
    pub phone_push_enabled: bool,
    /// Pids whose auditor is currently running, keyed to the SystemTime the
    /// worker thread was spawned. Prevents double-firing on successive refresh
    /// ticks while a verdict is in flight; the timestamp drives the
    /// "auditor running… (Xs)" indicator in the detail view.
    pub audit_in_flight: HashMap<u32, SystemTime>,
    /// (cwd, started_at_ms) → last decision the auditor returned for this
    /// stable session identity. Re-deciding on every tick would burn tokens;
    /// we only re-fire when the session leaves and re-enters `waiting`.
    pub audit_decided: HashSet<MuteKey>,
    /// Per-session verdict annotation, surfaced in detail view + status line.
    pub audit_notes: HashMap<u32, (SystemTime, String)>,
    /// Worker threads send completed verdicts here; `refresh()` drains.
    pub audit_tx: mpsc::Sender<Verdict>,
    pub audit_rx: mpsc::Receiver<Verdict>,
    /// User's default model from `~/.claude/settings.json` (e.g. `"opus[1m]"`).
    /// Read once at startup; the only deterministic source of the variant tag
    /// since the transcript's per-message `model` field strips `[1m]`.
    pub default_model: Option<String>,
    /// When true, the main area renders the audit-history overlay instead of
    /// the session table. Toggle with `H` (only effective when autonomous
    /// mode is on — there's no history to look at otherwise).
    pub audit_log_open: bool,
    /// Full keybinding help view. Toggle with `?` from normal mode.
    pub key_help_open: bool,
    /// Scroll offset into the audit log (0 = newest entry at top).
    pub audit_log_offset: u16,
    /// Total content-line count of the audit overlay (set by `draw_audit_log`
    /// each draw). Used to clamp scroll on `j`/`Ctrl-D` and to compute the
    /// `G`/bottom target.
    pub audit_log_total_lines: u16,
    /// Vim chord state: `g` waits for a follow-up `g` to mean "top". Reset
    /// by any other key. Inside the audit-log overlay only.
    pub pending_g: bool,
    /// Cached parse of the audit log, keyed on file (mtime, size). Avoids
    /// re-parsing the whole JSONL on every draw — most draws happen with
    /// no new audit, so the cache hit rate is high.
    pub audit_log_cache: Option<(SystemTime, u64, Vec<serde_json::Value>)>,
    /// `$` overlay state — daily/weekly cost rollup across all sessions.
    /// Computed lazily on open + cached for `COST_OVERLAY_TTL` (60s) so
    /// re-opens within the same minute don't re-walk ~/.claude/projects.
    pub cost_overlay_open: bool,
    pub cost_overlay_offset: u16,
    pub cost_overlay_total_lines: u16,
    pub cost_cache: Option<(SystemTime, crate::cost_rollup::Rollup)>,
    /// Active filter query. Empty = no filter applied. Case-insensitive
    /// substring match against any of: Claude `--name`, tmux window_name,
    /// tmux session_name, full cwd path.
    pub filter: String,
    /// True while the user is typing into the filter after `/`. Printable
    /// keys append to `filter`; Enter/Esc exit edit mode (Enter keeps the
    /// query, Esc clears it). Outside edit mode the filter just applies
    /// and all other keybindings work normally on the filtered subset.
    pub filter_active: bool,
    /// True while editing a triage-local alias after pressing `R`.
    pub rename_active: bool,
    pub rename_key: Option<AliasKey>,
    pub rename_buffer: String,
    /// True while composing a one-line user reply to the selected agent.
    pub reply_active: bool,
    pub reply_target: Option<String>,
    pub reply_target_label: String,
    pub reply_buffer: String,
    /// When true, triage exits cleanly after a successful `Enter` jump. Set
    /// by `--exit-on-jump`. Designed for the tmux popup launch pattern: the
    /// popup closes when triage exits, so a single keypress (`Enter`) both
    /// jumps to the target pane AND dismisses the overlay. Without this,
    /// the popup would stay open showing triage and the user would have to
    /// press `q` afterwards. Implies `zoom_on_jump` (popup is small, you
    /// always want the target zoomed).
    pub exit_on_jump: bool,
    /// Force zoom-on-jump regardless of pane width. Set by `--zoom-on-jump`.
    /// Most users don't need this — Enter auto-zooms when `last_pane_width <
    /// MOBILE_WIDTH_THRESHOLD`, so mobile clients (which resize tmux panes
    /// to phone-narrow) get the right behavior automatically. The flag is
    /// for "I want zoom even on a wide pane" overrides.
    pub zoom_on_jump: bool,
    /// Most recent table-area width from ratatui's draw cycle. Just for the
    /// header indicator — NOT the auto-zoom signal anymore (laptop split-
    /// screen makes pane width narrow even on a desktop terminal). See
    /// `last_client_width` for the actual zoom decision.
    pub last_pane_width: u16,
    /// Most recent tmux `client_width` reading. This is the actual
    /// terminal/device width, not the pane subset. Refreshed once per
    /// `refresh()` tick (cheap — single tmux subprocess). Zero when not
    /// in tmux or query failed.
    pub last_client_width: u16,
    /// Loaded once at startup (config file + env overrides). Read-only
    /// after this point.
    pub config: Config,
    /// `N` picker state for launching a configured agent in a new tmux
    /// window from one of the known session working directories.
    pub spawn_picker_open: bool,
    pub spawn_picker_selected: usize,
}

impl AppState {
    pub fn new() -> Self {
        let mut state = TableState::default();
        state.select(Some(0));
        let loaded = persist::load_state();
        let muted = loaded.mutes.into_iter().collect();
        let pinned = loaded.pins.into_iter().collect();
        let aliases = loaded.aliases.into_iter().collect();
        let (audit_tx, audit_rx) = mpsc::channel();
        Self {
            sessions: Vec::new(),
            selected: state,
            detail_open: false,
            preview_open: false,
            preview: None,
            preview_pos: PreviewPos::default(),
            status_msg: None,
            digest_cache: DigestCache::new(),
            codex_cache: crate::codex::CodexDigestCache::new(),
            aliases,
            muted,
            pinned,
            watched: HashSet::new(),
            last_states: HashMap::new(),
            notifications_armed: false,
            approval_mode: ApprovalMode::default(),
            autonomous: loaded.autonomous,
            phone_push_enabled: loaded.phone_push_enabled,
            audit_in_flight: HashMap::new(),
            audit_decided: HashSet::new(),
            audit_notes: HashMap::new(),
            audit_tx,
            audit_rx,
            default_model: crate::approval::read_default_model(),
            audit_log_open: false,
            key_help_open: false,
            audit_log_offset: 0,
            audit_log_total_lines: 0,
            pending_g: false,
            audit_log_cache: None,
            cost_overlay_open: false,
            cost_overlay_offset: 0,
            cost_overlay_total_lines: 0,
            cost_cache: None,
            filter: String::new(),
            filter_active: false,
            rename_active: false,
            rename_key: None,
            rename_buffer: String::new(),
            reply_active: false,
            reply_target: None,
            reply_target_label: String::new(),
            reply_buffer: String::new(),
            exit_on_jump: false,
            zoom_on_jump: false,
            last_pane_width: 0,
            last_client_width: 0,
            config: Config::default(),
            spawn_picker_open: false,
            spawn_picker_selected: 0,
        }
    }

    /// Open / close the `$` cost overlay. Always succeeds — unlike the audit
    /// log there's no on/off prerequisite (every user has historical
    /// transcripts). On open, drops the scroll offset and warms the cache if
    /// it's stale.
    pub fn toggle_cost_overlay(&mut self) {
        self.cost_overlay_open = !self.cost_overlay_open;
        if !self.cost_overlay_open {
            self.cost_overlay_offset = 0;
            self.pending_g = false;
            return;
        }
        // Opening: clear scroll + invalidate stale cache so the open shows
        // fresh data. The actual scan happens lazily in `cost_rollup_cached`
        // on the next draw — keeps the keypress non-blocking.
        self.cost_overlay_offset = 0;
        self.pending_g = false;
        const TTL: std::time::Duration = std::time::Duration::from_secs(60);
        let fresh = self
            .cost_cache
            .as_ref()
            .and_then(|(t, _)| SystemTime::now().duration_since(*t).ok())
            .is_some_and(|age| age < TTL);
        if !fresh {
            self.cost_cache = None;
        }
    }

    /// Return the cached rollup, computing one if missing. Called from the
    /// overlay draw path; the first draw after `toggle_cost_overlay` opens
    /// the overlay pays the scan cost (~hundreds of ms in release).
    pub fn cost_rollup_cached(&mut self) -> &crate::cost_rollup::Rollup {
        if self.cost_cache.is_none() {
            let r = crate::cost_rollup::compute_rollup();
            self.cost_cache = Some((SystemTime::now(), r));
        }
        &self.cost_cache.as_ref().unwrap().1
    }

    pub fn toggle_audit_log(&mut self) {
        // No autonomous gate: the log file persists historical entries
        // across auto-mode toggles, so the user might genuinely want to
        // review past decisions even when auto is currently off. The
        // renderer already shows a sensible empty-state message.
        self.audit_log_open = !self.audit_log_open;
        if !self.audit_log_open {
            self.audit_log_offset = 0;
            self.pending_g = false;
        }
    }

    pub fn toggle_autonomous(&mut self) {
        self.autonomous = !self.autonomous;
        if !self.autonomous {
            // Drop the "already decided" set so re-enabling is fresh.
            self.audit_decided.clear();
        }
        self.persist_state();
    }

    pub fn toggle_phone_push(&mut self) {
        self.phone_push_enabled = !self.phone_push_enabled;
        self.persist_state();
    }

    /// Toggle the live preview rail (`p`). Mutually exclusive with the detail
    /// pane. Ephemeral view state — not persisted. Clears the cached capture
    /// on close so a re-open starts fresh.
    pub fn toggle_preview(&mut self) {
        self.preview_open = !self.preview_open;
        if self.preview_open {
            self.detail_open = false;
        } else {
            self.preview = None;
        }
    }

    /// Flip the preview panel between right and bottom (`>`). No-op-ish when
    /// the rail is closed (state still flips so it's remembered on re-open).
    /// Returns the new position so the caller can surface it.
    pub fn flip_preview_pos(&mut self) -> PreviewPos {
        self.preview_pos = match self.preview_pos {
            PreviewPos::Right => PreviewPos::Bottom,
            PreviewPos::Bottom => PreviewPos::Right,
        };
        self.preview_pos
    }

    /// Toggle the detail pane (`Space`). Mutually exclusive with the preview
    /// rail (closing the rail drops its cached capture).
    pub fn toggle_detail(&mut self) {
        self.detail_open = !self.detail_open;
        if self.detail_open && self.preview_open {
            self.preview_open = false;
            self.preview = None;
        }
    }

    /// Capture the selected row's pane for the preview rail. Returns quickly
    /// when the rail is closed. The caller (main loop) handles throttling /
    /// retarget timing; here we re-parse to ratatui `Text` only when the raw
    /// capture actually changed, so a static pane costs one `capture-pane` and
    /// no allocation per tick. Sets `preview = None` for paneless rows so the
    /// renderer shows a placeholder.
    pub fn capture_selected_preview(&mut self) {
        if !self.preview_open {
            return;
        }
        let Some(target) = self
            .selected_session()
            .and_then(|s| s.pane.as_ref())
            .map(|p| (p.pane_id.clone(), p.target.clone()))
        else {
            self.preview = None;
            return;
        };
        let (pane_id, pane_target) = target;
        let Some(raw) = crate::tmux::capture_pane_visible_ansi(&pane_target) else {
            return;
        };
        if let Some(prev) = &self.preview
            && prev.pane_id == pane_id
            && prev.raw == raw
        {
            return; // unchanged — keep the parsed Text, skip re-parse + redraw cost
        }
        let mut text =
            ansi_to_tui::IntoText::into_text(&raw).unwrap_or_else(|_| Text::raw(raw.clone()));
        // tmux captures the source pane's full visible screen, which usually
        // ends in blank rows below the prompt. Drop them so "tail to the
        // bottom" (draw_preview) lands on the last real line, not whitespace.
        trim_trailing_blank_lines(&mut text);
        self.preview = Some(PanePreview { pane_id, raw, text });
    }

    pub fn oldest_pending_uuid(&self) -> Option<String> {
        self.selected_session()
            .filter(|s| s.status == "waiting")
            .and_then(|s| s.pending_approvals.first())
            .map(|a| a.uuid.clone())
    }

    pub fn toggle_mute_selected(&mut self) {
        let Some(s) = self.selected_session() else {
            return;
        };
        let key = MuteKey {
            cwd: s.cwd.clone(),
            started_at_ms: s.started_at_ms,
        };
        if self.muted.remove(&key).is_none() {
            // Mute and pin are mutually exclusive opposites — muting a pinned
            // row clears the pin so it sinks to the bottom rather than staying
            // floated at the top.
            self.pinned.remove(&key);
            self.muted.insert(key, SystemTime::now());
        }
        self.persist_state();
    }

    /// Toggle watch on the selected session. Returns the new state + a label
    /// so the caller can build a status message. Watches are in-memory; no
    /// `persist_state` call.
    pub fn toggle_watch_selected(&mut self) -> Option<(bool, String)> {
        let s = self.selected_session()?;
        let key = MuteKey {
            cwd: s.cwd.clone(),
            started_at_ms: s.started_at_ms,
        };
        let label = session_display_label(s);
        let now_watching = if self.watched.remove(&key) {
            false
        } else {
            self.watched.insert(key);
            true
        };
        Some((now_watching, label))
    }

    /// Toggle the pin on the selected session. Pinned sessions sort to the top
    /// (see `sort_sessions`). Persisted so pins survive a restart. Returns
    /// `(now_pinned, label)` for the status line, or None if nothing selected.
    pub fn toggle_pin_selected(&mut self) -> Option<(bool, String)> {
        let s = self.selected_session()?;
        let key = MuteKey {
            cwd: s.cwd.clone(),
            started_at_ms: s.started_at_ms,
        };
        let label = session_display_label(s);
        let now_pinned = if self.pinned.remove(&key) {
            false
        } else {
            // Pinning clears any mute — they're mutually exclusive opposites.
            self.muted.remove(&key);
            self.pinned.insert(key);
            true
        };
        self.persist_state();
        Some((now_pinned, label))
    }

    pub fn persist_state(&self) {
        persist::save_state(
            self.muted.iter(),
            self.pinned.iter(),
            self.aliases.iter(),
            self.autonomous,
            self.phone_push_enabled,
        );
    }

    fn persist_state_replace_aliases(&self) {
        persist::save_state_replace_aliases(
            self.muted.iter(),
            self.pinned.iter(),
            self.aliases.iter(),
            self.autonomous,
            self.phone_push_enabled,
        );
    }

    pub fn start_rename_selected(&mut self) -> Option<()> {
        let s = self.selected_session()?;
        let key = AliasKey::for_session(s);
        self.rename_buffer = AliasKey::candidates_for_session(s)
            .iter()
            .find_map(|key| self.aliases.get(key).cloned())
            .unwrap_or_default();
        self.rename_active = true;
        self.rename_key = Some(key);
        self.pending_g = false;
        Some(())
    }

    pub fn cancel_rename(&mut self) {
        self.rename_active = false;
        self.rename_key = None;
        self.rename_buffer.clear();
    }

    pub fn start_reply_selected(&mut self) -> Option<()> {
        let (target, label) = {
            let s = self.selected_session()?;
            let pane = s.pane.as_ref()?;
            (pane.pane_id.clone(), target_label(s))
        };
        self.reply_active = true;
        self.reply_target = Some(target);
        self.reply_target_label = label;
        self.reply_buffer.clear();
        self.audit_log_open = false;
        self.cost_overlay_open = false;
        self.filter_active = false;
        self.pending_g = false;
        Some(())
    }

    pub fn cancel_reply(&mut self) {
        self.reply_active = false;
        self.reply_target = None;
        self.reply_target_label.clear();
        self.reply_buffer.clear();
    }

    pub fn start_spawn_picker(&mut self) {
        let choices = self.spawn_cwd_choices();
        let selected_cwd = self.selected_session().map(|s| s.cwd.clone());
        self.spawn_picker_selected = selected_cwd
            .and_then(|cwd| choices.iter().position(|choice| *choice == cwd))
            .unwrap_or(0);
        self.spawn_picker_open = true;
        self.audit_log_open = false;
        self.cost_overlay_open = false;
        self.filter_active = false;
        self.pending_g = false;
    }

    pub fn cancel_spawn_picker(&mut self) {
        self.spawn_picker_open = false;
        self.spawn_picker_selected = 0;
    }

    pub fn spawn_cwd_choices(&self) -> Vec<std::path::PathBuf> {
        crate::spawn_agent::cwd_choices(&self.sessions)
    }

    pub fn selected_spawn_cwd(&self) -> Option<std::path::PathBuf> {
        let choices = self.spawn_cwd_choices();
        choices.get(self.spawn_picker_selected).cloned()
    }

    pub fn move_spawn_selection(&mut self, delta: i32) {
        let len = self.spawn_cwd_choices().len();
        if len == 0 {
            self.spawn_picker_selected = 0;
            return;
        }
        let cur = self.spawn_picker_selected.min(len - 1) as i32;
        self.spawn_picker_selected = (cur + delta).rem_euclid(len as i32) as usize;
    }

    pub fn clamp_spawn_selection(&mut self) {
        let len = self.spawn_cwd_choices().len();
        if len == 0 {
            self.spawn_picker_selected = 0;
        } else if self.spawn_picker_selected >= len {
            self.spawn_picker_selected = len - 1;
        }
    }

    pub fn commit_rename_selected(&mut self) -> Option<String> {
        let keys_to_clear = self
            .selected_session()
            .map(AliasKey::candidates_for_session)
            .unwrap_or_default();
        let key = self
            .rename_key
            .take()
            .or_else(|| self.selected_session().map(AliasKey::for_session))?;
        let alias = self
            .rename_buffer
            .split_whitespace()
            .collect::<Vec<_>>()
            .join(" ");
        self.rename_active = false;
        self.rename_buffer.clear();
        for candidate in keys_to_clear {
            self.aliases.remove(&candidate);
        }
        let msg = if alias.is_empty() {
            self.aliases.remove(&key);
            format!("alias cleared for {}", key.provider)
        } else {
            self.aliases.insert(key.clone(), alias.clone());
            format!("alias: {alias}")
        };
        for session in &mut self.sessions {
            if AliasKey::for_session(session) == key {
                session.name = (!alias.is_empty()).then_some(alias.clone());
            }
        }
        apply_aliases_to_sessions(&mut self.sessions, &self.aliases);
        self.persist_state_replace_aliases();
        Some(msg)
    }

    /// Decide whether `Enter` should zoom the destination pane. Three sources:
    /// `--exit-on-jump` (popup mode, always wants zoom), `--zoom-on-jump`
    /// (explicit force-on), and auto-detect by current pane width. Auto-
    /// detect is the path most users hit — running triage with no flag still
    /// "does the right thing" on a phone-narrow client.
    pub fn should_zoom_on_jump(&self) -> bool {
        self.exit_on_jump
            || self.zoom_on_jump
            || (self.last_client_width > 0
                && self.last_client_width < self.config.thresholds.mobile_width)
    }

    pub fn visible(&self) -> Vec<&Session> {
        if self.filter.is_empty() {
            return self.sessions.iter().collect();
        }
        let q = self.filter.to_lowercase();
        self.sessions
            .iter()
            .filter(|s| session_matches_filter(s, &q))
            .collect()
    }

    /// Vim `gg` — jump to the first visible row.
    pub fn select_first(&mut self) {
        if !self.visible().is_empty() {
            self.selected.select(Some(0));
        }
    }

    /// Vim `G` — jump to the last visible row.
    pub fn select_last(&mut self) {
        let n = self.visible().len();
        if n > 0 {
            self.selected.select(Some(n - 1));
        }
    }

    pub fn selected_session(&self) -> Option<&Session> {
        let v = self.visible();
        let idx = self.selected.selected()?;
        v.get(idx).copied()
    }

    pub fn move_selection(&mut self, delta: i32) {
        let len = self.visible().len();
        if len == 0 {
            self.selected.select(None);
            return;
        }
        let cur = self.selected.selected().unwrap_or(0) as i32;
        let next = (cur + delta).rem_euclid(len as i32) as usize;
        self.selected.select(Some(next));
    }

    pub fn clamp_selection(&mut self) {
        let len = self.visible().len();
        if len == 0 {
            self.selected.select(None);
        } else if self.selected.selected().unwrap_or(0) >= len {
            self.selected.select(Some(len - 1));
        } else if self.selected.selected().is_none() {
            self.selected.select(Some(0));
        }
        if self.spawn_picker_open {
            self.clamp_spawn_selection();
        }
    }
}

pub fn apply_aliases_to_sessions(sessions: &mut [Session], aliases: &HashMap<AliasKey, String>) {
    for session in sessions {
        if let Some(alias) = AliasKey::candidates_for_session(session)
            .iter()
            .find_map(|key| aliases.get(key))
            && !alias.trim().is_empty()
        {
            session.name = Some(alias.clone());
        }
    }
}

pub fn draw(f: &mut Frame, app: &mut AppState, now: SystemTime) {
    if app.key_help_open {
        let chunks = Layout::default()
            .direction(Direction::Vertical)
            .constraints([
                Constraint::Length(1),
                Constraint::Min(5),
                Constraint::Length(1),
            ])
            .split(f.area());
        draw_header(f, chunks[0], app);
        draw_key_help(f, chunks[1]);
        draw_footer(f, chunks[2], app);
        return;
    }
    if app.spawn_picker_open {
        let chunks = Layout::default()
            .direction(Direction::Vertical)
            .constraints([
                Constraint::Length(1),
                Constraint::Min(5),
                Constraint::Length(1),
            ])
            .split(f.area());
        draw_header(f, chunks[0], app);
        draw_spawn_picker(f, chunks[1], app);
        draw_footer(f, chunks[2], app);
        return;
    }
    if app.audit_log_open {
        let chunks = Layout::default()
            .direction(Direction::Vertical)
            .constraints([
                Constraint::Length(1),
                Constraint::Min(5),
                Constraint::Length(1),
            ])
            .split(f.area());
        draw_header(f, chunks[0], app);
        draw_audit_log(f, chunks[1], app, now);
        // draw_footer borrows app immutably — clone the small bits we need
        // and let it run after draw_audit_log writes back its line count.
        draw_footer(f, chunks[2], app);
        return;
    }
    if app.cost_overlay_open {
        let chunks = Layout::default()
            .direction(Direction::Vertical)
            .constraints([
                Constraint::Length(1),
                Constraint::Min(5),
                Constraint::Length(1),
            ])
            .split(f.area());
        draw_header(f, chunks[0], app);
        draw_cost_overlay(f, chunks[1], app);
        draw_footer(f, chunks[2], app);
        return;
    }
    // Resolve where the preview actually renders (vertical split preserves
    // width, so f.area().width == the table region's width).
    let (preview_right, preview_bottom) =
        preview_layout(app.preview_open, app.preview_pos, f.area().width);
    // Bottom region height: detail pane, or a bottom-docked preview, else 0.
    // detail and preview are mutually exclusive so at most one is non-zero.
    let bottom_height = if app.detail_open {
        18
    } else if preview_bottom {
        bottom_preview_height(f.area().height)
    } else {
        0
    };
    // When replying in default mode (no preview panel to host the composer),
    // a dedicated compose bar sits just above the footer so the reply is
    // impossible to miss — instead of being tucked into the header status line.
    let compose_bar = app.reply_active && !app.preview_open;
    let chunks = Layout::default()
        .direction(Direction::Vertical)
        .constraints([
            Constraint::Length(1),                               // header
            Constraint::Min(5),                                  // table
            Constraint::Length(bottom_height),                   // detail or bottom preview
            Constraint::Length(if compose_bar { 1 } else { 0 }), // reply compose bar
            Constraint::Length(1),                               // footer
        ])
        .split(f.area());

    draw_header(f, chunks[0], app);

    // Right: split the table region into a compact table | preview. Bottom
    // (incl. the narrow-width fallback): full table here, preview docks below.
    if preview_right {
        let cols = Layout::default()
            .direction(Direction::Horizontal)
            .constraints([
                Constraint::Length(PREVIEW_RIGHT_TABLE_WIDTH),
                Constraint::Min(0),
            ])
            .split(chunks[1]);
        draw_table(f, cols[0], app, now, true); // compact — HEADLINE/CWD dropped
        draw_preview(f, cols[1], app);
    } else {
        draw_table(f, chunks[1], app, now, false);
    }

    if app.detail_open {
        draw_detail(f, chunks[2], app, now);
    } else if preview_bottom {
        draw_preview(f, chunks[2], app);
    }
    if compose_bar {
        draw_reply_bar(f, chunks[3], app);
    }
    draw_footer(f, chunks[4], app);
}

/// Default-mode reply composer: a full-width yellow bar (TRI-140) shown just
/// above the footer when replying without the preview panel open. Names the
/// target and shows the live input so the reply can't be missed.
fn draw_reply_bar(f: &mut Frame, area: Rect, app: &AppState) {
    let line = Line::from(vec![
        Span::styled(
            format!(" ✎ reply → {}", app.reply_target_label),
            Style::default()
                .fg(Color::Yellow)
                .add_modifier(Modifier::BOLD),
        ),
        Span::raw("   "),
        Span::styled("", Style::default().fg(Color::Yellow)),
        Span::raw(app.reply_buffer.clone()),
        Span::styled("", Style::default().fg(Color::Yellow)),
    ]);
    f.render_widget(Paragraph::new(line), area);
}

/// Minimum total table-region width before the right-docked preview rail is
/// allowed to split off. Narrower clients (mobile) keep the full table.
const PREVIEW_MIN_TOTAL_WIDTH: u16 = 100;

/// Resolve `(render_right, render_bottom)` for the preview given the toggle,
/// the user's dock preference, and the client width. A side rail can't fit
/// under `PREVIEW_MIN_TOTAL_WIDTH`, so a right-docked preview falls back to the
/// bottom there — that way `p` still works on mobile instead of silently
/// showing nothing. At most one of the two is ever true.
fn preview_layout(open: bool, pos: PreviewPos, width: u16) -> (bool, bool) {
    if !open {
        return (false, false);
    }
    match pos {
        PreviewPos::Right if width >= PREVIEW_MIN_TOTAL_WIDTH => (true, false),
        // Right but too narrow → fall back to bottom.
        PreviewPos::Right | PreviewPos::Bottom => (false, true),
    }
}
/// Fixed width of the compact identity table when the preview docks on the
/// right — just enough for STATE/AGE/AI/SESSION; the preview takes the rest.
const PREVIEW_RIGHT_TABLE_WIDTH: u16 = 42;
/// Rows the table keeps when a bottom-docked preview is open, so the preview
/// can't crowd the list off-screen on short (mobile) clients.
const PREVIEW_BOTTOM_MIN_TABLE_ROWS: u16 = 6;

/// Height for a bottom-docked preview: ~60% of the frame so it's roomy on a
/// desktop, but never so tall that the table drops below
/// `PREVIEW_BOTTOM_MIN_TABLE_ROWS` (mobile-safe). `total_height` is the full
/// frame height (header + table + preview + footer).
fn bottom_preview_height(total_height: u16) -> u16 {
    // header + footer + the table floor we must preserve.
    let reserved = 2 + PREVIEW_BOTTOM_MIN_TABLE_ROWS;
    let cap = total_height.saturating_sub(reserved);
    let desired = total_height * 6 / 10;
    desired.min(cap)
}

/// Trim trailing all-whitespace lines so tailing to the bottom lands on the
/// last line with real content rather than the pane's blank filler rows.
fn trim_trailing_blank_lines(text: &mut Text<'_>) {
    while text
        .lines
        .last()
        .is_some_and(|l| l.spans.iter().all(|s| s.content.trim().is_empty()))
    {
        text.lines.pop();
    }
}

/// Live preview of the selected row's tmux pane (TRI-138), docked right or
/// bottom. Renders the cached ANSI capture; lines wider than the panel are
/// clipped at the right edge (Paragraph without wrap = truncate-right). Shows a
/// placeholder when the selected row has no pane or hasn't been captured yet.
fn draw_preview(f: &mut Frame, area: Rect, app: &AppState) {
    let sel = app.selected_session();
    // When a reply is being composed, the panel becomes the reply surface
    // (TRI-140): yellow border, named target, and an input line pinned to the
    // bottom. The send path / buffer / send-gate are the existing `r` reply —
    // only the rendering moves here. The footer carries the key hints.
    let replying = app.reply_active;
    let (border_color, title) = if replying {
        (
            Color::Yellow,
            format!(" ✎ reply → {} ", app.reply_target_label),
        )
    } else {
        let t = match sel {
            Some(s) => {
                let label = session_display_label(s);
                let pane = s.pane.as_ref().map(|p| p.pane_id.as_str()).unwrap_or("");
                format!(" preview · {label} · {pane} ")
            }
            None => " preview ".to_string(),
        };
        (Color::DarkGray, t)
    };
    let block = Block::default()
        .borders(Borders::ALL)
        .border_style(Style::default().fg(border_color))
        .title(title);
    let inner = block.inner(area);
    f.render_widget(block, area);

    // Reserve the bottom row of the panel for the compose line while replying.
    let (content, input) = if replying && inner.height >= 2 {
        (
            Rect {
                height: inner.height - 1,
                ..inner
            },
            Some(Rect {
                y: inner.y + inner.height - 1,
                height: 1,
                ..inner
            }),
        )
    } else {
        (inner, None)
    };

    match &app.preview {
        Some(p) => {
            // The capture is the source pane's full screen, usually taller than
            // our panel. Tail it: scroll so the last lines (latest output /
            // prompt) are visible rather than the stale top.
            let offset = (p.text.lines.len() as u16).saturating_sub(content.height);
            f.render_widget(Paragraph::new(p.text.clone()).scroll((offset, 0)), content);
        }
        None => {
            let msg = if sel.and_then(|s| s.pane.as_ref()).is_some() {
                "capturing…"
            } else {
                "no tmux pane for this session"
            };
            f.render_widget(
                Paragraph::new(msg).style(Style::default().fg(Color::DarkGray)),
                content,
            );
        }
    }

    if let Some(input_rect) = input {
        let line = Line::from(vec![
            Span::styled(
                "",
                Style::default()
                    .fg(Color::Yellow)
                    .add_modifier(Modifier::BOLD),
            ),
            Span::raw(app.reply_buffer.clone()),
            Span::styled("", Style::default().fg(Color::Yellow)),
        ]);
        f.render_widget(Paragraph::new(line), input_rect);
    }
}

fn draw_header(f: &mut Frame, area: Rect, app: &AppState) {
    let total = app.sessions.len();
    let visible = app.visible().len();
    let counts = if app.filter.is_empty() {
        format!("{total} session{}", if total == 1 { "" } else { "s" })
    } else {
        format!("{visible}/{total} sessions")
    };
    // Show pane width inline + a `zoom` indicator when auto-detect is active,
    // so it's obvious whether Enter will zoom on the current device.
    let zoom_marker = if app.should_zoom_on_jump() {
        " · zoom"
    } else {
        ""
    };
    // Show pane width (left, what ratatui drew into) and client width
    // (right, what tmux says the terminal is). The client width drives
    // auto-zoom; pane being narrow alone doesn't.
    let dims = if app.last_client_width > 0 && app.last_client_width != app.last_pane_width {
        format!(
            "{}cols (pane) / {}cols (client){zoom_marker}",
            app.last_pane_width, app.last_client_width
        )
    } else {
        format!("{}cols{zoom_marker}", app.last_pane_width)
    };
    let mut spans: Vec<Span<'static>> = vec![
        Span::styled("triage", Style::default().add_modifier(Modifier::BOLD)),
        Span::raw("   "),
        Span::styled(counts, Style::default().fg(Color::DarkGray)),
        Span::raw("   "),
    ];
    spans.extend(header_status_spans(app, area.width));
    // Reply no longer renders here (TRI-140): it shows in the preview panel
    // when open, otherwise in the dedicated compose bar above the footer.
    if app.rename_active {
        let cursor = "_";
        spans.push(Span::raw("   "));
        spans.push(Span::styled(
            "rename: ",
            Style::default().fg(Color::DarkGray),
        ));
        spans.push(Span::styled(
            format!("{}{cursor}", app.rename_buffer),
            Style::default().fg(Color::Yellow),
        ));
    } else if app.filter_active || !app.filter.is_empty() {
        let cursor = if app.filter_active { "_" } else { "" };
        spans.push(Span::raw("   "));
        spans.push(Span::styled(
            "filter: ",
            Style::default().fg(Color::DarkGray),
        ));
        spans.push(Span::styled(
            format!("{}{cursor}", app.filter),
            Style::default().fg(Color::Yellow),
        ));
    }
    spans.push(Span::raw("   "));
    spans.push(Span::styled(dims, Style::default().fg(Color::DarkGray)));
    f.render_widget(Paragraph::new(Line::from(spans)), area);
}

fn header_status_spans(app: &AppState, width: u16) -> Vec<Span<'static>> {
    let mode = LayoutMode::from_width(width);
    let separator = match mode {
        LayoutMode::Narrow => " ",
        LayoutMode::Medium | LayoutMode::Wide | LayoutMode::Compact => " · ",
    };
    vec![
        Span::styled(
            auto_mode_label_parts(app.autonomous, app.audit_in_flight.len(), width),
            auto_mode_style(app),
        ),
        Span::styled(separator, Style::default().fg(Color::DarkGray)),
        Span::styled(
            phone_mode_label_parts(app.phone_push_enabled, width),
            phone_mode_style(app.phone_push_enabled),
        ),
    ]
}

fn auto_mode_label_parts(autonomous: bool, in_flight: usize, width: u16) -> String {
    let mode = LayoutMode::from_width(width);
    if autonomous {
        match mode {
            LayoutMode::Narrow => {
                if in_flight > 0 {
                    format!("A:on·{in_flight}")
                } else {
                    "A:on".to_string()
                }
            }
            LayoutMode::Medium => {
                if in_flight > 0 {
                    format!("AUTO on·{in_flight}")
                } else {
                    "AUTO on".to_string()
                }
            }
            LayoutMode::Wide | LayoutMode::Compact => {
                if in_flight > 0 {
                    format!(
                        "AUTO on · {in_flight} audit{}",
                        if in_flight == 1 { "" } else { "s" }
                    )
                } else {
                    "AUTO on".to_string()
                }
            }
        }
    } else {
        match mode {
            LayoutMode::Narrow => "A:off".to_string(),
            LayoutMode::Medium | LayoutMode::Wide | LayoutMode::Compact => "AUTO off".to_string(),
        }
    }
}

fn phone_mode_label_parts(phone_push_enabled: bool, width: u16) -> String {
    let mode = LayoutMode::from_width(width);
    match (mode, phone_push_enabled) {
        (LayoutMode::Narrow, true) => "ph:on".to_string(),
        (LayoutMode::Narrow, false) => "ph:off".to_string(),
        (_, true) => "phone on".to_string(),
        (_, false) => "phone off".to_string(),
    }
}

fn auto_mode_style(app: &AppState) -> Style {
    if app.autonomous {
        if app.audit_in_flight.is_empty() {
            Style::default()
                .fg(Color::Magenta)
                .add_modifier(Modifier::BOLD)
        } else {
            Style::default()
                .fg(Color::Yellow)
                .add_modifier(Modifier::BOLD)
        }
    } else {
        Style::default().fg(Color::DarkGray)
    }
}

fn phone_mode_style(phone_push_enabled: bool) -> Style {
    if phone_push_enabled {
        Style::default().fg(Color::DarkGray)
    } else {
        Style::default()
            .fg(Color::Yellow)
            .add_modifier(Modifier::BOLD)
    }
}

/// Terminal-width tiers. Picked once per draw based on `area.width`.
/// `Narrow` is for phone-sized SSH (~40–60 cols), `Medium` is a split-screen
/// laptop window, `Wide` is the standard desktop layout.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
enum LayoutMode {
    Narrow,
    Medium,
    Wide,
    /// Identity-only columns (STATE/AGE/AI/SESSION, no HEADLINE/CWD), used when
    /// the preview rail docks on the right and the live pane replaces HEADLINE.
    Compact,
}

impl LayoutMode {
    fn from_width(w: u16) -> Self {
        if w < 60 {
            LayoutMode::Narrow
        } else if w < 100 {
            LayoutMode::Medium
        } else {
            LayoutMode::Wide
        }
    }
}

fn draw_table(f: &mut Frame, area: Rect, app: &mut AppState, now: SystemTime, compact: bool) {
    // Stash for the Enter handler's auto-zoom decision (see `should_zoom_on_jump`).
    // Set before borrowing `visible` from app to avoid an aliasing conflict.
    app.last_pane_width = area.width;
    let visible = app.visible();
    let selected_idx = app.selected.selected();
    // `compact` (preview docked right) forces the identity-only column set
    // regardless of width; otherwise pick the tier from the area width.
    let layout = if compact {
        LayoutMode::Compact
    } else {
        LayoutMode::from_width(area.width)
    };

    // Fixed = sum of non-headline column widths + per-column gap (1) + highlight indent (2).
    let (fixed, widths, header_cells): (usize, Vec<Constraint>, Vec<Cell>) = match layout {
        LayoutMode::Compact => (
            // No HEADLINE column — the preview rail carries that. SESSION flexes
            // to fill the fixed-width strip (PREVIEW_RIGHT_TABLE_WIDTH).
            7 + 5 + 3 + 4 + 2,
            vec![
                Constraint::Length(7),
                Constraint::Length(5),
                Constraint::Length(3),
                Constraint::Min(8),
            ],
            vec![
                Cell::from("STATE"),
                Cell::from("AGE"),
                Cell::from("AI"),
                Cell::from("SESSION"),
            ],
        ),
        LayoutMode::Narrow => (
            7 + 1 + 2,
            vec![Constraint::Length(7), Constraint::Min(20)],
            vec![Cell::from("STATE"), Cell::from("HEADLINE")],
        ),
        LayoutMode::Medium => (
            7 + 5 + 3 + 16 + 4 + 2,
            vec![
                Constraint::Length(7),
                Constraint::Length(5),
                Constraint::Length(3),
                Constraint::Length(16),
                Constraint::Min(20),
            ],
            vec![
                Cell::from("STATE"),
                Cell::from("AGE"),
                Cell::from("AI"),
                Cell::from("SESSION"),
                Cell::from("HEADLINE"),
            ],
        ),
        LayoutMode::Wide => (
            7 + 5 + 3 + 18 + 24 + 5 + 2,
            vec![
                Constraint::Length(7),
                Constraint::Length(5),
                Constraint::Length(3),
                Constraint::Length(18),
                Constraint::Length(24),
                Constraint::Min(20),
            ],
            vec![
                Cell::from("STATE"),
                Cell::from("AGE"),
                Cell::from("AI"),
                Cell::from("SESSION"),
                Cell::from("CWD"),
                Cell::from("HEADLINE"),
            ],
        ),
    };

    let headline_width = (area.width as usize).saturating_sub(fixed).max(1);
    let mut rows: Vec<Row> = visible
        .iter()
        .enumerate()
        .map(|(i, s)| build_row(s, now, headline_width, layout, Some(i) == selected_idx))
        .collect();

    // Thin rule between the pinned block (which sorts to the top) and the rest,
    // shown only when both groups are present. Pinned rows are contiguous at
    // the front, so the boundary is just the count of leading pinned rows.
    //
    // The divider is a render-only Row: `app.selected` stays a logical index
    // into `visible`, and we translate to the divider-shifted visual space on a
    // throwaway TableState here. That keeps navigation / jump / selection
    // (which all index `visible`) oblivious to the extra row.
    let pin_count = visible.iter().take_while(|s| s.pinned).count();
    let show_divider = pin_count > 0 && pin_count < visible.len();
    let mut render_state = app.selected;
    if show_divider {
        let col_widths: Vec<usize> = widths
            .iter()
            .map(|c| match c {
                Constraint::Length(n) => *n as usize,
                _ => headline_width,
            })
            .collect();
        let divider_cells: Vec<Cell> = col_widths
            .iter()
            .map(|w| Cell::from("".repeat(*w)).style(Style::default().fg(Color::DarkGray)))
            .collect();
        rows.insert(
            pin_count,
            Row::new(divider_cells).height(1).bottom_margin(1),
        );

        if let Some(sel) = render_state.selected()
            && sel >= pin_count
        {
            render_state.select(Some(sel + 1));
        }
        let off = render_state.offset();
        if off >= pin_count {
            *render_state.offset_mut() = off + 1;
        }
    }

    let header = Row::new(header_cells).style(
        Style::default()
            .fg(Color::DarkGray)
            .add_modifier(Modifier::BOLD),
    );

    // Selected row gets uniform REVERSED on top of cells that have already
    // been rendered with neutralized colors (see `build_row` `is_selected`
    // path). REVERSED flips fg/bg per-cell — when each cell starts from the
    // same default fg, the result is a single uniform band rather than the
    // multicolor ribbon we got when each cell had its own color (green
    // state / DarkGray age / bold-white session / DarkGray cwd / default
    // headline). REVERSED was picked over a literal bg() to avoid painting
    // the bottom_margin gap.
    let table = Table::new(rows, widths)
        .header(header)
        .row_highlight_style(Style::default().add_modifier(Modifier::REVERSED))
        .highlight_symbol("")
        .block(Block::default().borders(Borders::TOP));

    f.render_stateful_widget(table, area, &mut render_state);

    // Persist the (possibly auto-scrolled) offset back into the logical state,
    // undoing the divider shift so the next frame starts from the right place.
    let voff = render_state.offset();
    let loff = if show_divider && voff > pin_count {
        voff - 1
    } else {
        voff
    };
    *app.selected.offset_mut() = loff;
}

fn draw_spawn_picker(f: &mut Frame, area: Rect, app: &mut AppState) {
    app.clamp_spawn_selection();
    let choices = app.spawn_cwd_choices();
    let selected = app
        .spawn_picker_selected
        .min(choices.len().saturating_sub(1));
    let provider = app.config.new_agent.provider.name();
    let command = app.config.new_agent.command.as_str();
    let chunks = Layout::default()
        .direction(Direction::Vertical)
        .constraints([Constraint::Length(2), Constraint::Min(3)])
        .split(area);

    let intro = vec![
        Line::from(vec![
            Span::styled(
                format!("New {provider} agent"),
                Style::default().add_modifier(Modifier::BOLD),
            ),
            Span::raw("   "),
            Span::styled(
                format!("command: {command}"),
                Style::default().fg(Color::DarkGray),
            ),
        ]),
        Line::from(Span::styled(
            "Choose a working directory from current triage sessions.",
            Style::default().fg(Color::DarkGray),
        )),
    ];
    f.render_widget(Paragraph::new(intro), chunks[0]);

    let capacity = chunks[1].height.saturating_sub(2).max(1) as usize;
    let offset = selected.saturating_sub(capacity.saturating_sub(1));
    let path_width = chunks[1].width.saturating_sub(10).max(10) as usize;
    let rows = choices
        .iter()
        .enumerate()
        .skip(offset)
        .take(capacity)
        .map(|(i, cwd)| {
            let style = if i == selected {
                Style::default().add_modifier(Modifier::REVERSED)
            } else {
                Style::default()
            };
            Row::new(vec![
                Cell::from(format!("{}/{}", i + 1, choices.len())),
                Cell::from(shorten_path(&cwd.display().to_string(), path_width)),
            ])
            .style(style)
        })
        .collect::<Vec<_>>();

    let table = Table::new(rows, vec![Constraint::Length(8), Constraint::Min(10)])
        .header(
            Row::new(vec![Cell::from("#"), Cell::from("CWD")]).style(
                Style::default()
                    .fg(Color::DarkGray)
                    .add_modifier(Modifier::BOLD),
            ),
        )
        .block(Block::default().borders(Borders::TOP));
    f.render_widget(table, chunks[1]);
}

fn build_row(
    s: &Session,
    now: SystemTime,
    headline_width: usize,
    layout: LayoutMode,
    is_selected: bool,
) -> Row<'static> {
    let (state_str, color) = state_glyph(s.state);
    let age = idle_age(s, now)
        .map(format_duration)
        .unwrap_or_else(|| "".to_string());

    let session_label = session_display_label(s);
    let provider_label = s.provider.label();

    let cwd_short = shorten_path(&s.cwd.to_string_lossy(), 24);
    // Only show a permission headline when Claude itself is actually paused on
    // user input. Pending files alone are not enough: the hook also sees
    // auto-approved tool calls.
    let headline_raw = if s.provider == Provider::Claude && s.status == "waiting" {
        if let Some(a) = s.pending_approvals.first() {
            if a.tool_input_brief.is_empty() {
                format!("⏸ approve? {}", a.tool_name)
            } else {
                format!("⏸ approve? {}{}", a.tool_name, a.tool_input_brief)
            }
        // Prefer the actual tool_use (full Claude question) over `waitingFor`
        // (just "approve Bash"). Both come from Claude itself; the tool_use
        // is the same data the hook would have shown.
        } else if let Some((name, brief)) = &s.last_tool_use {
            if brief.is_empty() {
                format!("⏸ approve {name}?")
            } else {
                format!("⏸ approve {name}? — {brief}")
            }
        } else {
            let what = s.waiting_for.as_deref().unwrap_or("input");
            format!("{what}?")
        }
    } else if s.provider == Provider::Codex && s.state == AttentionState::Blocked {
        if let Some((name, brief)) = &s.last_tool_use {
            if brief.is_empty() {
                format!("⏸ approve {name}?")
            } else {
                format!("⏸ approve {name}? — {brief}")
            }
        } else {
            "⏸ codex needs input".to_string()
        }
    } else {
        s.headline
            .clone()
            .or_else(|| s.last_prompt.clone())
            .map(|t| t.replace('\n', " "))
            .unwrap_or_else(|| "(no transcript)".to_string())
    };

    // Narrow layout has only STATE+HEADLINE columns, so prefix the headline
    // with session label + age — otherwise the user can't tell rows apart.
    let headline_raw = match layout {
        LayoutMode::Narrow => format!("{provider_label} {session_label}  {age}  · {headline_raw}"),
        _ => headline_raw,
    };
    let wrapped = wrap_text(&headline_raw, headline_width, 4);
    // Compact rows have no HEADLINE cell, so they're always a single line —
    // don't let the (unrendered) wrapped headline inflate the row height.
    let height = if layout == LayoutMode::Compact {
        1
    } else {
        wrapped.len().max(1) as u16
    };
    let mut headline_lines: Vec<Line> = wrapped.into_iter().map(Line::from).collect();
    // T-81: prepend a bold cyan ● on the first line of watched rows. Span
    // approach (vs string prefix) keeps the wrap correct and gives the
    // marker its own color independent of row state.
    if s.watched && !headline_lines.is_empty() {
        let first_line = std::mem::take(&mut headline_lines[0]);
        let mut spans = vec![Span::styled(
            "",
            Style::default()
                .fg(Color::Cyan)
                .add_modifier(Modifier::BOLD),
        )];
        spans.extend(first_line.spans);
        headline_lines[0] = Line::from(spans);
    }

    // Muted rows render dimmed across all columns so the user's eye skips
    // them. The state glyph still shows its label but in a muted color.
    // Watched rows append `·w` to the state label so the user can see the
    // arm-state in the column they're already scanning for state changes.
    // Pinned rows are set apart by a divider rule in the table (drawn in
    // draw_table) rather than a per-row glyph, so no pin marker here.
    let (state_label, state_color) = if s.muted {
        ("muted".to_string(), Color::DarkGray)
    } else if s.watched {
        (format!("{state_str}·w"), color)
    } else {
        (state_str, color)
    };
    let row_style = if s.muted {
        Style::default().fg(Color::DarkGray)
    } else {
        Style::default()
    };

    // Selected rows neutralize per-cell fg so the table-level REVERSED
    // applies as one uniform band instead of flipping each cell's color
    // (green state → green band, DarkGray cwd → gray band, etc.). Muted
    // rows still keep their DarkGray dimming even when selected so the
    // "I've seen this, skip it" visual contract holds across selection.
    let state_cell_style = if is_selected && !s.muted {
        Style::default()
    } else {
        Style::default().fg(state_color)
    };
    let session_style = if s.muted {
        Style::default().fg(Color::DarkGray)
    } else {
        Style::default().add_modifier(Modifier::BOLD)
    };
    let provider_style = if is_selected && !s.muted {
        Style::default()
    } else {
        match s.provider {
            Provider::Claude => Style::default().fg(Color::DarkGray),
            Provider::Codex => Style::default().fg(Color::Cyan),
        }
    };
    let cwd_style = if is_selected && !s.muted {
        Style::default()
    } else {
        Style::default().fg(Color::DarkGray)
    };

    let cells: Vec<Cell> = match layout {
        LayoutMode::Compact => vec![
            Cell::from(state_label).style(state_cell_style),
            Cell::from(age).style(cwd_style),
            Cell::from(provider_label).style(provider_style),
            Cell::from(session_label).style(session_style),
        ],
        LayoutMode::Narrow => vec![
            Cell::from(state_label).style(state_cell_style),
            Cell::from(Text::from(headline_lines)),
        ],
        LayoutMode::Medium => vec![
            Cell::from(state_label).style(state_cell_style),
            Cell::from(age).style(cwd_style),
            Cell::from(provider_label).style(provider_style),
            Cell::from(session_label).style(session_style),
            Cell::from(Text::from(headline_lines)),
        ],
        LayoutMode::Wide => vec![
            Cell::from(state_label).style(state_cell_style),
            Cell::from(age).style(cwd_style),
            Cell::from(provider_label).style(provider_style),
            Cell::from(session_label).style(session_style),
            Cell::from(cwd_short).style(cwd_style),
            Cell::from(Text::from(headline_lines)),
        ],
    };

    Row::new(cells)
        .height(height)
        .bottom_margin(1)
        .style(row_style)
}

fn wrap_text(text: &str, width: usize, max_lines: usize) -> Vec<String> {
    if width == 0 || max_lines == 0 {
        return vec![String::new()];
    }
    let mut out: Vec<String> = Vec::new();
    let mut line = String::new();
    let mut line_chars = 0usize;
    let mut truncated = false;

    for word in text.split_whitespace() {
        if out.len() >= max_lines {
            truncated = true;
            break;
        }
        let wlen = word.chars().count();
        let need = if line.is_empty() {
            wlen
        } else {
            line_chars + 1 + wlen
        };
        if need <= width {
            if !line.is_empty() {
                line.push(' ');
                line_chars += 1;
            }
            line.push_str(word);
            line_chars += wlen;
        } else {
            if !line.is_empty() {
                out.push(std::mem::take(&mut line));
                line_chars = 0;
                if out.len() >= max_lines {
                    truncated = true;
                    break;
                }
            }
            if wlen > width {
                let mut chars = word.chars().peekable();
                while chars.peek().is_some() {
                    let chunk: String = chars.by_ref().take(width).collect();
                    let chunk_len = chunk.chars().count();
                    if chunk_len < width || chars.peek().is_none() {
                        line = chunk;
                        line_chars = chunk_len;
                        break;
                    }
                    out.push(chunk);
                    if out.len() >= max_lines {
                        truncated = chars.peek().is_some();
                        break;
                    }
                }
            } else {
                line = word.to_string();
                line_chars = wlen;
            }
        }
    }
    if !line.is_empty() && out.len() < max_lines {
        out.push(line);
    }
    if truncated && !out.is_empty() {
        let last = out.last_mut().unwrap();
        let cap = width.saturating_sub(1);
        while last.chars().count() > cap {
            last.pop();
        }
        last.push('');
    }
    if out.is_empty() {
        out.push(String::new());
    }
    out
}

fn state_glyph(state: AttentionState) -> (String, Color) {
    let color = match state {
        AttentionState::Error => Color::Red,
        AttentionState::Blocked => Color::Yellow,
        AttentionState::JustFinished => Color::Green,
        AttentionState::Working => Color::Cyan,
        AttentionState::Fresh => Color::White,
        AttentionState::IdleShort => Color::DarkGray,
        AttentionState::IdleLong => Color::DarkGray,
        AttentionState::Stale => Color::DarkGray,
        AttentionState::Unknown => Color::DarkGray,
    };
    (state.label().to_string(), color)
}

fn format_duration(d: std::time::Duration) -> String {
    let s = d.as_secs();
    if s < 60 {
        format!("{s}s")
    } else if s < 3600 {
        format!("{}m", s / 60)
    } else if s < 86400 {
        format!("{}h", s / 3600)
    } else {
        format!("{}d", s / 86400)
    }
}

fn shorten_path(path: &str, max: usize) -> String {
    let home = std::env::var("HOME").unwrap_or_default();
    let p = if !home.is_empty() && path.starts_with(&home) {
        format!("~{}", &path[home.len()..])
    } else {
        path.to_string()
    };
    if p.len() <= max {
        return p;
    }
    let cut = p.len() - (max - 1);
    format!("{}", &p[cut..])
}

fn draw_detail(f: &mut Frame, area: Rect, app: &AppState, now: SystemTime) {
    let Some(s) = app.selected_session() else {
        return;
    };

    let dim = || Style::default().fg(Color::DarkGray);
    let bold = || Style::default().add_modifier(Modifier::BOLD);
    let mag = || Style::default().fg(Color::Magenta);
    let yellow = || Style::default().fg(Color::Yellow);

    let mut lines: Vec<Line> = Vec::new();

    // ── HEADER ────────────────────────────────────────────────────
    // Single line, no labels. State (colored) · pane · model · uptime.
    // Drop pid / session_id / status — all debug-tier; can be recovered by
    // grepping transcripts. State is the canonical "what's this row doing"
    // signal; status is redundant.
    let (state_str, state_color) = state_glyph(s.state);
    let pane_target = s
        .pane
        .as_ref()
        .map(|p| p.target.clone())
        .unwrap_or_else(|| "(no pane)".to_string());
    let uptime = format_uptime(s.started_at_ms, now);
    let sep = || Span::styled("  ·  ", dim());
    // Compute the context window once up front so both the header model
    // annotation and the stats line use the same value.
    let app_peak = app
        .sessions
        .iter()
        .map(|s| s.peak_context_tokens)
        .max()
        .unwrap_or(0);
    let default_model = (s.provider == Provider::Claude)
        .then_some(app.default_model.as_deref())
        .flatten();
    let window =
        context_window_for_session(s, app_peak, default_model, app.config.model.context_window);
    let is_1m = window >= 1_000_000;

    let mut header = vec![
        Span::styled(
            state_str,
            Style::default()
                .fg(state_color)
                .add_modifier(Modifier::BOLD),
        ),
        sep(),
        Span::styled(pane_target, bold()),
    ];
    // Model label: prefer the per-message `model` (most precise — includes
    // the version, e.g. `opus-4-7`); fall back to the user's global default
    // from settings.json (e.g. `opus[1m]`). Strip the `claude-` prefix and
    // append ` (1M)` when we've confirmed the 1M variant via any signal.
    let model_raw = s.latest_model.as_deref().or(default_model);
    if let Some(m) = model_raw {
        let short = m.strip_prefix("claude-").unwrap_or(m);
        let label = if is_1m && !short.contains("[1m]") && !short.contains("(1M)") {
            format!("{} (1M)", short)
        } else {
            short.to_string()
        };
        header.push(sep());
        header.push(Span::styled(label, dim()));
    }
    header.push(sep());
    header.push(Span::styled(uptime, dim()));
    if s.pinned {
        header.push(sep());
        header.push(Span::styled("[pinned]", yellow()));
    }
    if s.muted {
        header.push(sep());
        header.push(Span::styled("[muted]", yellow()));
    }
    if s.watched {
        header.push(sep());
        header.push(Span::styled("[watch]", Style::default().fg(Color::Cyan)));
    }
    lines.push(Line::from(header));

    // ── BODY ──────────────────────────────────────────────────────
    // Most actionable content first: what the agent is doing/saying and
    // what tool it's asking permission for. Blank line separates header
    // from body so the eye can land on it.
    let body_start_idx = lines.len();

    if let Some(t) = &s.latest_assistant_text
        && s.headline.as_ref().is_none_or(|h| h.trim() != t.trim())
    {
        lines.push(Line::from(vec![
            Span::styled("agent  ", dim()),
            Span::raw(truncate(t, 300)),
        ]));
    }

    if s.provider == Provider::Claude && s.status == "waiting" {
        if let Some(a) = s.pending_approvals.first() {
            lines.push(Line::from(vec![
                Span::styled(a.tool_name.clone(), yellow().add_modifier(Modifier::BOLD)),
                Span::styled("  (hook)", dim()),
            ]));
            for line in a.tool_input_detail().lines().take(6) {
                lines.push(Line::from(vec![
                    Span::raw("  "),
                    Span::raw(truncate(line, 400)),
                ]));
            }
        } else if let Some((name, brief)) = &s.last_tool_use {
            lines.push(Line::from(vec![
                Span::styled(name.clone(), yellow().add_modifier(Modifier::BOLD)),
                Span::styled("  (tmux scrape, may be truncated)", dim()),
            ]));
            lines.push(Line::from(vec![
                Span::raw("  "),
                Span::raw(truncate(brief, 400)),
            ]));
        }
    } else if s.provider == Provider::Codex && s.state == AttentionState::Blocked {
        lines.push(Line::from(vec![
            Span::styled("codex  ", dim()),
            Span::styled("approval requested in pane", yellow()),
        ]));
        if let Some((name, brief)) = &s.last_tool_use {
            lines.push(Line::from(vec![
                Span::styled(name.clone(), yellow().add_modifier(Modifier::BOLD)),
                Span::styled("  (visible prompt)", dim()),
            ]));
            if !brief.is_empty() {
                lines.push(Line::from(vec![
                    Span::raw("  "),
                    Span::raw(truncate(brief, 400)),
                ]));
            }
        }
    }

    if let Some(h) = &s.headline {
        lines.push(Line::from(vec![
            Span::styled("recap  ", dim()),
            Span::raw(truncate(h, 400)),
        ]));
    }
    if let Some(p) = &s.last_prompt {
        lines.push(Line::from(vec![
            Span::styled("prompt ", dim()),
            Span::raw(truncate(p, 200)),
        ]));
    }

    // Insert a spacer between header and body if body has content. Doing
    // it after construction (rather than always pushing) keeps the layout
    // tight when a session has nothing actionable to show.
    if lines.len() > body_start_idx {
        lines.insert(body_start_idx, Line::from(""));
    }

    // ── FOOTER ────────────────────────────────────────────────────
    // Auditor decision + numeric stats consolidated at the bottom. All
    // dim by default so the eye reads them as metadata, not content.

    let footer_start_idx = lines.len();

    if let Some(start) = app.audit_in_flight.get(&s.pid) {
        let secs = now.duration_since(*start).map(|d| d.as_secs()).unwrap_or(0);
        lines.push(Line::from(vec![
            Span::styled("audit  ", dim()),
            Span::styled(format!("running ({}s)", secs), mag()),
        ]));
    } else if let Some((ts, note)) = app.audit_notes.get(&s.pid) {
        let ago = now.duration_since(*ts).map(|d| d.as_secs()).unwrap_or(0);
        lines.push(Line::from(vec![
            Span::styled("audit  ", dim()),
            Span::styled(note.clone(), mag()),
            Span::styled(format!("  ({}s ago)", ago), dim()),
        ]));
    }

    // Cost + context + tokens — all on one line so cost data scans as a
    // single unit. Skip when there's nothing to show (fresh session).
    if s.total_cost_usd > 0.0 || s.total_tokens_in > 0 || s.latest_context_tokens > 0 {
        let mut stats: Vec<Span> = vec![Span::styled(
            if s.provider == Provider::Codex {
                "usage  "
            } else {
                "cost   "
            },
            dim(),
        )];
        let mut needs_sep = false;
        if s.provider == Provider::Claude {
            stats.push(Span::raw(format_cost(s.total_cost_usd)));
            needs_sep = true;
        }
        if s.latest_context_tokens > 0 {
            // `window` was computed once up front for the header's (1M)
            // annotation; reuse it here so header and stats agree.
            let pct = (s.latest_context_tokens as f64 / window as f64) * 100.0;
            let pct_color = if pct >= 95.0 {
                Color::Red
            } else if pct >= 80.0 {
                Color::Yellow
            } else {
                Color::DarkGray
            };
            if needs_sep {
                stats.push(sep());
            }
            stats.push(Span::raw(format!(
                "{}/{}",
                format_tokens(s.latest_context_tokens),
                format_tokens(window),
            )));
            stats.push(Span::styled(
                format!(" ({:.0}%)", pct),
                Style::default().fg(pct_color),
            ));
            stats.push(Span::styled(" ctx", dim()));
            needs_sep = true;
        }
        if s.total_tokens_in > 0 || s.total_tokens_out > 0 || s.total_tokens_cache_read > 0 {
            if needs_sep {
                stats.push(sep());
            }
            stats.push(Span::styled(
                format!(
                    "{} in · {} out · {} cache",
                    format_tokens(s.total_tokens_in),
                    format_tokens(s.total_tokens_out),
                    format_tokens(s.total_tokens_cache_write + s.total_tokens_cache_read),
                ),
                dim(),
            ));
        }
        if s.provider == Provider::Claude {
            stats.push(Span::styled("  (approx)", dim()));
        }
        lines.push(Line::from(stats));
    }

    // Events line: timing + last-turn shape + lifetime prompt count, all
    // on one line. Replaces the prior 2 separate `events:` and `last
    // turn:` rows.
    let mut events: Vec<Span> = vec![
        Span::styled("events ", dim()),
        Span::raw(format!(
            "last {} · prompt {} · stop {}",
            format_age_opt(s.last_event_at, now),
            format_age_opt(s.last_prompt_at, now),
            format_age_opt(s.last_stop_at, now),
        )),
    ];
    if let (Some(d), Some(c)) = (s.last_turn_duration_ms, s.last_turn_msg_count) {
        events.push(sep());
        events.push(Span::styled(
            format!("turn {}.{}s/{} msgs", d / 1000, (d % 1000) / 100, c),
            dim(),
        ));
    }
    if s.user_prompt_count > 0 {
        events.push(sep());
        events.push(Span::styled(
            format!("{} prompts", s.user_prompt_count),
            dim(),
        ));
    }
    lines.push(Line::from(events));

    // Spacer between body and footer, mirroring the body spacer logic.
    if lines.len() > footer_start_idx {
        lines.insert(footer_start_idx, Line::from(""));
    }

    let block = Block::default().borders(Borders::TOP);
    let para = Paragraph::new(lines)
        .block(block)
        .wrap(Wrap { trim: false });
    f.render_widget(para, area);
}

fn format_age_opt(ts: Option<SystemTime>, now: SystemTime) -> String {
    let Some(ts) = ts else {
        return "".to_string();
    };
    let Ok(d) = now.duration_since(ts) else {
        return "".to_string();
    };
    let s = d.as_secs();
    if s < 60 {
        format!("{s}s")
    } else if s < 3600 {
        format!("{}m", s / 60)
    } else if s < 86400 {
        format!("{}h", s / 3600)
    } else {
        format!("{}d", s / 86400)
    }
}

fn format_cost(usd: f64) -> String {
    if usd >= 1.0 {
        format!("${:.2}", usd)
    } else if usd >= 0.01 {
        format!("${:.3}", usd)
    } else if usd > 0.0 {
        format!("${:.5}", usd)
    } else {
        "$0".to_string()
    }
}

fn format_tokens(n: u64) -> String {
    if n >= 1_000_000 {
        format!("{:.1}M", n as f64 / 1_000_000.0)
    } else if n >= 1_000 {
        format!("{:.1}k", n as f64 / 1_000.0)
    } else {
        n.to_string()
    }
}

/// Context-window size for a session. Five signals in priority order before
/// falling back to 200k:
///
/// 1. `config.model.context_window` (also `TRIAGE_CONTEXT_WINDOW` env via the
///    config's env-override layer) — explicit override.
/// 2. The session's own model name carries a `[1m]` tag (future-proof; today
///    the transcript strips it).
/// 3. `~/.claude/settings.json` default model has a `[1m]` tag — this is the
///    deterministic source for the user's global preference.
/// 4. Per-session peak `> 210k` tokens — empirical proof the variant isn't
///    200k-capped.
/// 5. Fleet-wide peak `> 210k` tokens — same proof from a sibling session.
fn context_window_for_session(
    s: &Session,
    app_peak: u64,
    default_model: Option<&str>,
    override_window: Option<u64>,
) -> u64 {
    if let Some(n) = override_window
        && n > 0
    {
        return n;
    }
    if let Some(n) = s.context_window
        && n > 0
    {
        return n;
    }
    if let Some(m) = &s.latest_model
        && m.contains("[1m]")
    {
        return 1_000_000;
    }
    if default_model.is_some_and(|m| m.contains("[1m]")) {
        return 1_000_000;
    }
    if s.peak_context_tokens > 210_000 || app_peak > 210_000 {
        return 1_000_000;
    }
    200_000
}

fn format_uptime(started_at_ms: u64, now: SystemTime) -> String {
    if started_at_ms == 0 {
        return "".to_string();
    }
    let Ok(now_ms) = now.duration_since(SystemTime::UNIX_EPOCH) else {
        return "".to_string();
    };
    let now_ms = now_ms.as_millis() as u64;
    if now_ms <= started_at_ms {
        return "0s".to_string();
    }
    let s = (now_ms - started_at_ms) / 1000;
    if s < 60 {
        format!("{s}s")
    } else if s < 3600 {
        format!("{}m", s / 60)
    } else if s < 86400 {
        format!("{}h{}m", s / 3600, (s % 3600) / 60)
    } else {
        format!("{}d{}h", s / 86400, (s % 86400) / 3600)
    }
}

fn truncate(s: &str, n: usize) -> String {
    let cleaned = s.replace('\n', " ");
    if cleaned.chars().count() <= n {
        cleaned
    } else {
        let mut out: String = cleaned.chars().take(n).collect();
        out.push('');
        out
    }
}

/// Read recent entries from `~/.config/triage/auto-decisions.jsonl`, with a
/// cache keyed on (mtime, size). Most draws happen with no new audit, so we
/// return the cached parse instead of re-reading the whole file. Newest-first.
/// On any I/O or parse error, returns an empty vec.
fn read_audit_log_cached(app: &mut AppState, limit: usize) -> Vec<serde_json::Value> {
    let Some(home) = std::env::var_os("HOME") else {
        return Vec::new();
    };
    let path = std::path::PathBuf::from(home).join(".config/triage/auto-decisions.jsonl");
    let meta = std::fs::metadata(&path).ok();
    let key = meta.as_ref().and_then(|m| {
        let mtime = m.modified().ok()?;
        Some((mtime, m.len()))
    });
    if let (Some((mt, sz)), Some((cmt, csz, entries))) = (key, app.audit_log_cache.as_ref())
        && mt == *cmt
        && sz == *csz
    {
        return entries.iter().take(limit).cloned().collect();
    }
    let Ok(content) = std::fs::read_to_string(&path) else {
        app.audit_log_cache = None;
        return Vec::new();
    };
    let entries: Vec<serde_json::Value> = content
        .lines()
        .rev()
        .filter_map(|l| serde_json::from_str(l).ok())
        .collect();
    let out = entries.iter().take(limit).cloned().collect();
    if let Some((mt, sz)) = key {
        app.audit_log_cache = Some((mt, sz, entries));
    }
    out
}

fn draw_audit_log(f: &mut Frame, area: Rect, app: &mut AppState, now: SystemTime) {
    let dim = || Style::default().fg(Color::DarkGray);
    let bold = || Style::default().add_modifier(Modifier::BOLD);

    let entries = read_audit_log_cached(app, 200);
    let total = entries.len();

    let mut lines: Vec<Line> = Vec::new();

    if entries.is_empty() {
        let msg = if app.autonomous {
            "no audits yet — auto mode is on but hasn't fired against a Blocked session"
        } else {
            "auto mode is off — no audits to show"
        };
        lines.push(Line::from(Span::styled(msg, dim())));
    }

    for v in &entries {
        let ts = v.get("ts").and_then(|t| t.as_u64()).unwrap_or(0);
        let pid = v.get("pid").and_then(|p| p.as_u64()).unwrap_or(0);
        let cwd = v.get("cwd").and_then(|c| c.as_str()).unwrap_or("");
        let cwd_short = std::path::Path::new(cwd)
            .file_name()
            .map(|s| s.to_string_lossy().into_owned())
            .unwrap_or_else(|| cwd.to_string());
        let tool = v.get("tool").and_then(|t| t.as_str()).unwrap_or("?");
        let decision = v
            .get("decision")
            .and_then(|d| d.as_str())
            .unwrap_or("?")
            .to_string();
        let reason = v.get("reason").and_then(|r| r.as_str()).unwrap_or("");
        let cost = v.get("cost_usd").and_then(|c| c.as_f64());
        let duration_ms = v.get("duration_ms").and_then(|d| d.as_u64());

        let now_secs = now
            .duration_since(SystemTime::UNIX_EPOCH)
            .map(|d| d.as_secs())
            .unwrap_or(0);
        let ago = if now_secs > ts && ts > 0 {
            let s = now_secs - ts;
            if s < 60 {
                format!("{s}s ago")
            } else if s < 3600 {
                format!("{}m ago", s / 60)
            } else if s < 86400 {
                format!("{}h ago", s / 3600)
            } else {
                format!("{}d ago", s / 86400)
            }
        } else {
            "?".to_string()
        };

        let decision_color = match decision.as_str() {
            "APPROVE" => Color::Green,
            "DENY" => Color::Red,
            "WAIT" => Color::Yellow,
            _ => Color::DarkGray,
        };

        // Row 1: time · decision · tool · pid+cwd · cost/duration
        let mut row1: Vec<Span> = vec![
            Span::styled(format!("{:>10}", ago), dim()),
            Span::raw("  "),
            Span::styled(
                format!("{:<7}", decision),
                Style::default()
                    .fg(decision_color)
                    .add_modifier(Modifier::BOLD),
            ),
            Span::raw("  "),
            Span::styled(format!("{:<6}", tool), bold()),
            Span::raw("  "),
            Span::styled(format!("pid {pid} {cwd_short}"), dim()),
        ];
        let perf = match (cost, duration_ms) {
            (Some(c), Some(ms)) => Some(format!("{:.1}s · ${:.4}", ms as f64 / 1000.0, c)),
            (Some(c), None) => Some(format!("${:.4}", c)),
            (None, Some(ms)) => Some(format!("{:.1}s", ms as f64 / 1000.0)),
            (None, None) => None,
        };
        if let Some(p) = perf {
            row1.push(Span::styled("  ·  ", dim()));
            row1.push(Span::styled(p, dim()));
        }
        lines.push(Line::from(row1));
        // Row 2: reason, indented
        if !reason.is_empty() {
            lines.push(Line::from(vec![
                Span::raw("            "),
                Span::raw(reason.to_string()),
            ]));
        }
        // Spacer
        lines.push(Line::from(""));
    }

    // Save total content lines so the key handler can clamp scroll on j/G.
    app.audit_log_total_lines = lines.len() as u16;
    // Clamp current offset against fresh content (e.g. user pressed `j` past
    // the end before this draw, or content shrank somehow).
    let max_offset = app
        .audit_log_total_lines
        .saturating_sub(area.height.saturating_sub(2)); // 2 for top border + breathing room
    if app.audit_log_offset > max_offset {
        app.audit_log_offset = max_offset;
    }

    let summary = if total > 0 {
        format!(" auto-decisions  ·  {} entries  ·  newest first ", total)
    } else {
        " auto-decisions ".to_string()
    };
    let block = Block::default()
        .borders(Borders::TOP)
        .title(Span::styled(summary, dim()));
    let para = Paragraph::new(lines)
        .block(block)
        .wrap(Wrap { trim: false })
        .scroll((app.audit_log_offset, 0));
    f.render_widget(para, area);
}

#[derive(Debug, Default)]
struct LiveCodexTokenRollup {
    session_count: u64,
    tokens_in: u64,
    tokens_out: u64,
    cache_read: u64,
    total_tokens: u64,
    top_cwds: Vec<LiveCodexCwdBucket>,
    models: Vec<LiveCodexModelBucket>,
    max_context: Option<LiveCodexContextPeak>,
}

#[derive(Debug)]
struct LiveCodexCwdBucket {
    cwd: String,
    session_count: u64,
    total_tokens: u64,
}

#[derive(Debug)]
struct LiveCodexModelBucket {
    model: String,
    session_count: u64,
    total_tokens: u64,
}

#[derive(Debug)]
struct LiveCodexContextPeak {
    cwd: String,
    session_id: String,
    tokens: u64,
    window: Option<u64>,
}

fn live_codex_token_rollup(sessions: &[Session]) -> LiveCodexTokenRollup {
    let mut rollup = LiveCodexTokenRollup::default();
    let mut cwd_buckets: HashMap<String, LiveCodexCwdBucket> = HashMap::new();
    let mut model_buckets: HashMap<String, LiveCodexModelBucket> = HashMap::new();

    for s in sessions.iter().filter(|s| s.provider == Provider::Codex) {
        let total_tokens = s.total_tokens_in + s.total_tokens_out;
        if total_tokens == 0 && s.latest_context_tokens == 0 {
            continue;
        }
        rollup.session_count += 1;
        rollup.tokens_in += s.total_tokens_in;
        rollup.tokens_out += s.total_tokens_out;
        rollup.cache_read += s.total_tokens_cache_read;
        rollup.total_tokens += total_tokens;

        let cwd = s.cwd.display().to_string();
        let cwd_bucket = cwd_buckets
            .entry(cwd.clone())
            .or_insert_with(|| LiveCodexCwdBucket {
                cwd: cwd.clone(),
                session_count: 0,
                total_tokens: 0,
            });
        cwd_bucket.session_count += 1;
        cwd_bucket.total_tokens += total_tokens;

        if let Some(model) = s.latest_model.as_deref()
            && !model.trim().is_empty()
        {
            let model_bucket =
                model_buckets
                    .entry(model.to_string())
                    .or_insert_with(|| LiveCodexModelBucket {
                        model: model.to_string(),
                        session_count: 0,
                        total_tokens: 0,
                    });
            model_bucket.session_count += 1;
            model_bucket.total_tokens += total_tokens;
        }

        if s.latest_context_tokens > 0
            && rollup
                .max_context
                .as_ref()
                .is_none_or(|peak| context_rank_session(s) > context_rank_peak(peak))
        {
            rollup.max_context = Some(LiveCodexContextPeak {
                cwd,
                session_id: s.session_id.clone(),
                tokens: s.latest_context_tokens,
                window: s.context_window,
            });
        }
    }

    rollup.top_cwds = cwd_buckets.into_values().collect();
    rollup.top_cwds.sort_by(|a, b| {
        b.total_tokens
            .cmp(&a.total_tokens)
            .then_with(|| b.session_count.cmp(&a.session_count))
            .then_with(|| a.cwd.cmp(&b.cwd))
    });
    rollup.models = model_buckets.into_values().collect();
    rollup.models.sort_by(|a, b| {
        b.total_tokens
            .cmp(&a.total_tokens)
            .then_with(|| b.session_count.cmp(&a.session_count))
            .then_with(|| a.model.cmp(&b.model))
    });
    rollup
}

fn context_rank_session(s: &Session) -> u64 {
    s.context_window
        .filter(|window| *window > 0)
        .map(|window| s.latest_context_tokens.saturating_mul(10_000) / window)
        .unwrap_or(s.latest_context_tokens)
}

fn context_rank_peak(peak: &LiveCodexContextPeak) -> u64 {
    peak.window
        .filter(|window| *window > 0)
        .map(|window| peak.tokens.saturating_mul(10_000) / window)
        .unwrap_or(peak.tokens)
}

// Several rendering helpers (draw_cost_overlay, draw_footer, …) are defined
// after this module; keeping the tests beside the layout code they exercise is
// intentional, so we allow the organizational lint rather than reshuffle ~500
// lines.
#[cfg(test)]
#[allow(clippy::items_after_test_module)]
mod tests {
    use std::path::PathBuf;

    use crate::models::Pane;

    use super::*;

    #[test]
    fn live_codex_token_rollup_uses_only_codex_sessions() {
        let mut codex = Session::new(
            Provider::Codex,
            1,
            "codex-session".to_string(),
            PathBuf::from("/repo/triage"),
            None,
            "idle".to_string(),
            0,
            0,
            None,
        );
        codex.total_tokens_in = 1_000;
        codex.total_tokens_out = 200;
        codex.total_tokens_cache_read = 700;
        codex.latest_context_tokens = 900;
        codex.context_window = Some(1_800);
        codex.latest_model = Some("gpt-5.5".to_string());

        let mut claude = Session::new(
            Provider::Claude,
            2,
            "claude-session".to_string(),
            PathBuf::from("/repo/other"),
            None,
            "idle".to_string(),
            0,
            0,
            None,
        );
        claude.total_tokens_in = 9_000;

        let rollup = live_codex_token_rollup(&[codex, claude]);

        assert_eq!(rollup.session_count, 1);
        assert_eq!(rollup.tokens_in, 1_000);
        assert_eq!(rollup.tokens_out, 200);
        assert_eq!(rollup.cache_read, 700);
        assert_eq!(rollup.total_tokens, 1_200);
        assert_eq!(rollup.top_cwds[0].cwd, "/repo/triage");
        assert_eq!(rollup.models[0].model, "gpt-5.5");
        assert_eq!(rollup.max_context.as_ref().unwrap().tokens, 900);
    }

    #[test]
    fn auto_mode_label_is_compact_on_narrow_widths() {
        assert_eq!(auto_mode_label_parts(false, 0, 40), "A:off");
        assert_eq!(auto_mode_label_parts(true, 0, 40), "A:on");
        assert_eq!(auto_mode_label_parts(true, 2, 40), "A:on·2");
    }

    #[test]
    fn auto_mode_label_shows_audit_count_on_wide_widths() {
        assert_eq!(auto_mode_label_parts(false, 0, 120), "AUTO off");
        assert_eq!(auto_mode_label_parts(true, 0, 120), "AUTO on");
        assert_eq!(auto_mode_label_parts(true, 1, 120), "AUTO on · 1 audit");
        assert_eq!(auto_mode_label_parts(true, 2, 120), "AUTO on · 2 audits");
    }

    #[test]
    fn phone_mode_label_shows_on_and_off() {
        assert_eq!(phone_mode_label_parts(true, 40), "ph:on");
        assert_eq!(phone_mode_label_parts(false, 40), "ph:off");
        assert_eq!(phone_mode_label_parts(true, 120), "phone on");
        assert_eq!(phone_mode_label_parts(false, 120), "phone off");
    }

    #[test]
    fn start_reply_selected_records_pane_target_and_label() {
        let mut app = AppState::new();
        let mut session = Session::new(
            Provider::Claude,
            1,
            "sid".to_string(),
            PathBuf::from("/repo/triage"),
            Some("agent-triage".to_string()),
            "idle".to_string(),
            0,
            0,
            None,
        );
        session.pane = Some(Pane {
            target: "main:1.0".to_string(),
            tmux_session: "main".to_string(),
            window_name: "triage-work".to_string(),
            pane_id: "%42".to_string(),
            pid: 123,
            tty: "/dev/ttys001".to_string(),
            current_command: "claude".to_string(),
            cwd: PathBuf::from("/repo/triage"),
            active: true,
        });
        app.sessions.push(session);
        app.selected.select(Some(0));

        assert!(app.start_reply_selected().is_some());

        assert!(app.reply_active);
        assert_eq!(app.reply_target.as_deref(), Some("%42"));
        assert_eq!(app.reply_target_label, "cc agent-triage");
        assert!(app.reply_buffer.is_empty());
    }

    #[test]
    fn preview_and_detail_are_mutually_exclusive() {
        // Opening the preview rail closes the detail pane and vice versa
        // (TRI-138) — they share the table region budget, so we never show
        // both at once.
        let mut app = AppState::new();
        app.detail_open = true;

        app.toggle_preview();
        assert!(app.preview_open, "p opened the rail");
        assert!(!app.detail_open, "opening preview closed detail");

        app.toggle_detail();
        assert!(app.detail_open, "Space opened detail");
        assert!(!app.preview_open, "opening detail closed preview");
    }

    #[test]
    fn toggling_preview_off_drops_the_cached_capture() {
        let mut app = AppState::new();
        app.toggle_preview();
        app.preview = Some(PanePreview {
            pane_id: "%42".to_string(),
            raw: "hi".to_string(),
            text: Text::raw("hi"),
        });
        app.toggle_preview();
        assert!(!app.preview_open);
        assert!(app.preview.is_none(), "closing the rail clears the capture");
    }

    #[test]
    fn preview_layout_falls_back_to_bottom_on_narrow_clients() {
        // Closed → nothing renders.
        assert_eq!(
            preview_layout(false, PreviewPos::Right, 200),
            (false, false)
        );
        // Right on a wide client → right rail.
        assert_eq!(preview_layout(true, PreviewPos::Right, 200), (true, false));
        // Right on a narrow (mobile) client → falls back to bottom so `p`
        // still does something instead of silently showing nothing.
        assert_eq!(preview_layout(true, PreviewPos::Right, 80), (false, true));
        // Bottom always docks bottom, wide or narrow.
        assert_eq!(preview_layout(true, PreviewPos::Bottom, 200), (false, true));
        assert_eq!(preview_layout(true, PreviewPos::Bottom, 80), (false, true));
    }

    #[test]
    fn bottom_preview_height_is_roomy_but_keeps_table_on_short_screens() {
        // Tall desktop: ~60% to the preview, table keeps the rest.
        assert_eq!(bottom_preview_height(50), 30);
        // Short (mobile) screen: capped so the table keeps its floor.
        // reserved = 2 + 6 = 8, so cap = 20 - 8 = 12 (< 60% = 12 here they tie).
        assert_eq!(bottom_preview_height(20), 12);
        // Very short: cap dominates, never steals the table's floor rows.
        assert_eq!(bottom_preview_height(12), 4);
        assert_eq!(bottom_preview_height(8), 0);
    }

    #[test]
    fn trim_trailing_blank_lines_drops_only_trailing_whitespace() {
        let mut text = Text::from(vec![
            Line::from("top"),
            Line::from(""),
            Line::from("> prompt"),
            Line::from("   "),
            Line::from(""),
        ]);
        trim_trailing_blank_lines(&mut text);
        assert_eq!(
            text.lines.len(),
            3,
            "trailing blanks dropped, interior kept"
        );
        assert_eq!(text.lines.last().unwrap().spans[0].content, "> prompt");
    }

    #[test]
    fn flip_preview_pos_toggles_right_and_bottom() {
        let mut app = AppState::new();
        assert_eq!(app.preview_pos, PreviewPos::Right, "defaults to right");
        assert_eq!(app.flip_preview_pos(), PreviewPos::Bottom);
        assert_eq!(app.flip_preview_pos(), PreviewPos::Right);
    }

    #[test]
    fn ansi_capture_parses_to_styled_text() {
        // Sanity that the `-e` capture → ansi-to-tui wiring yields styled
        // spans, not a single raw blob. A red "hi" SGR sequence should parse
        // to a line whose span carries a foreground color.
        let raw = "\x1b[31mhi\x1b[0m";
        let text = ansi_to_tui::IntoText::into_text(&raw).expect("parses");
        let span = &text.lines[0].spans[0];
        assert_eq!(span.content, "hi");
        assert_eq!(span.style.fg, Some(Color::Red));
    }
}

fn draw_cost_overlay(f: &mut Frame, area: Rect, app: &mut AppState) {
    use crate::cost_rollup::{format_usd, short_cwd};
    let dim = || Style::default().fg(Color::DarkGray);
    let bold = || Style::default().add_modifier(Modifier::BOLD);
    let codex = live_codex_token_rollup(&app.sessions);

    // Pull the cached rollup (computes once per overlay-open within the TTL).
    // Clone the few small bits we render so the borrow ends before we touch
    // `app` again to write back total_lines.
    let r = app.cost_rollup_cached();
    let (
        total_today,
        total_7d,
        total_30d,
        total_all,
        scanned_files,
        scan_duration_ms,
        today_label,
        days,
        cwds,
        models,
    ) = (
        r.total_today,
        r.total_7d,
        r.total_30d,
        r.total_all,
        r.scanned_files,
        r.scan_duration_ms,
        r.today.format(),
        r.days.clone(),
        r.cwds.clone(),
        r.models.clone(),
    );

    let mut lines: Vec<Line> = Vec::new();

    // Headline totals — biggest fonts (relative), one per row.
    lines.push(Line::from(""));
    lines.push(Line::from(vec![
        Span::raw("  "),
        Span::styled("today  ", dim()),
        Span::styled(format_usd(total_today), bold().fg(Color::Green)),
        Span::styled(format!("   ({today_label})"), dim()),
    ]));
    let avg7 = if total_7d > 0.0 { total_7d / 7.0 } else { 0.0 };
    lines.push(Line::from(vec![
        Span::raw("  "),
        Span::styled("7-day  ", dim()),
        Span::styled(format_usd(total_7d), bold()),
        Span::styled(format!("   (avg {}/day)", format_usd(avg7)), dim()),
    ]));
    lines.push(Line::from(vec![
        Span::raw("  "),
        Span::styled("30-day ", dim()),
        Span::styled(format_usd(total_30d), bold()),
    ]));
    lines.push(Line::from(vec![
        Span::raw("  "),
        Span::styled("all    ", dim()),
        Span::styled(format_usd(total_all), bold()),
        Span::styled(
            format!(
                "   ({scanned_files} session{} · {scan_duration_ms} ms)",
                if scanned_files == 1 { "" } else { "s" }
            ),
            dim(),
        ),
    ]));

    // Last-14-days strip.
    lines.push(Line::from(""));
    lines.push(Line::from(Span::styled(
        "  last 14 days",
        Style::default().add_modifier(Modifier::BOLD),
    )));
    let want_days = 14;
    let mut day_map: HashMap<crate::cost_rollup::DayKey, f64> =
        days.iter().map(|d| (d.key, d.cost_usd)).collect();
    // Walk back from today.
    let today_key = app.cost_cache.as_ref().unwrap().1.today;
    let mut strip: Vec<(crate::cost_rollup::DayKey, f64)> = Vec::new();
    {
        let today_secs = day_start_secs_from_key(today_key);
        for back in (0..want_days).rev() {
            let secs = today_secs - back as i64 * 86_400;
            let k = day_key_at_secs(secs);
            let cost = day_map.remove(&k).unwrap_or(0.0);
            strip.push((k, cost));
        }
    }
    let peak = strip
        .iter()
        .map(|(_, c)| *c)
        .fold(0.0_f64, f64::max)
        .max(0.01);
    let bar_width = 28u16;
    for (k, c) in &strip {
        let filled = ((*c / peak) * bar_width as f64).round() as usize;
        let bar: String = "".repeat(filled);
        let label = if *k == today_key { "  (today)" } else { "" };
        lines.push(Line::from(vec![
            Span::raw("  "),
            Span::styled(k.format(), dim()),
            Span::raw("  "),
            Span::styled(format!("{:>8}", format_usd(*c)), bold()),
            Span::raw("  "),
            Span::styled(bar, Style::default().fg(Color::Cyan)),
            Span::styled(label, dim()),
        ]));
    }

    // Top-5 cwds.
    lines.push(Line::from(""));
    lines.push(Line::from(Span::styled(
        "  top cwds (all time)",
        Style::default().add_modifier(Modifier::BOLD),
    )));
    let cwd_peak = cwds.first().map(|c| c.cost_usd).unwrap_or(0.01).max(0.01);
    for c in cwds.iter().take(5) {
        let filled = ((c.cost_usd / cwd_peak) * 24.0).round() as usize;
        let bar: String = "".repeat(filled);
        lines.push(Line::from(vec![
            Span::raw("  "),
            Span::styled(format!("{:>8}", format_usd(c.cost_usd)), bold()),
            Span::raw("  "),
            Span::styled(format!("{:>3} sess", c.session_count), dim()),
            Span::raw("  "),
            Span::raw(format!("{:<28}", short_cwd(&c.cwd))),
            Span::styled(bar, Style::default().fg(Color::Magenta)),
        ]));
    }

    // Per-model split.
    if !models.is_empty() {
        lines.push(Line::from(""));
        lines.push(Line::from(Span::styled(
            "  by model (all time)",
            Style::default().add_modifier(Modifier::BOLD),
        )));
        let total: f64 = models.iter().map(|m| m.cost_usd).sum();
        for m in &models {
            let pct = if total > 0.0 {
                100.0 * m.cost_usd / total
            } else {
                0.0
            };
            lines.push(Line::from(vec![
                Span::raw("  "),
                Span::styled(format!("{:<8}", m.model), bold()),
                Span::raw("  "),
                Span::styled(format!("{:>8}", format_usd(m.cost_usd)), bold()),
                Span::styled(format!("   {pct:>4.1}%"), dim()),
            ]));
        }
    }

    if codex.session_count > 0 {
        lines.push(Line::from(""));
        lines.push(Line::from(Span::styled(
            "  live codex tokens (no dollar data)",
            Style::default().add_modifier(Modifier::BOLD),
        )));
        lines.push(Line::from(vec![
            Span::raw("  "),
            Span::styled(format!("{:>8}", format_tokens(codex.total_tokens)), bold()),
            Span::styled(" total", dim()),
            Span::raw("  "),
            Span::styled(
                format!(
                    "{} in · {} out · {} cache",
                    format_tokens(codex.tokens_in),
                    format_tokens(codex.tokens_out),
                    format_tokens(codex.cache_read),
                ),
                dim(),
            ),
            Span::styled(
                format!(
                    "   ({} live session{})",
                    codex.session_count,
                    if codex.session_count == 1 { "" } else { "s" },
                ),
                dim(),
            ),
        ]));
        if let Some(peak) = codex.max_context.as_ref() {
            let pct = peak
                .window
                .filter(|window| *window > 0)
                .map(|window| (peak.tokens as f64 / window as f64) * 100.0);
            let pct_style = match pct {
                Some(p) if p >= 95.0 => Style::default().fg(Color::Red),
                Some(p) if p >= 80.0 => Style::default().fg(Color::Yellow),
                _ => dim(),
            };
            let window = peak
                .window
                .map(format_tokens)
                .unwrap_or_else(|| "?".to_string());
            let session_id: String = peak.session_id.chars().take(8).collect();
            lines.push(Line::from(vec![
                Span::raw("  "),
                Span::styled("max ctx ", dim()),
                Span::styled(format!("{}/{}", format_tokens(peak.tokens), window), bold()),
                Span::styled(
                    pct.map(|p| format!(" ({p:.0}%)"))
                        .unwrap_or_else(|| " (?%)".to_string()),
                    pct_style,
                ),
                Span::styled("  ", dim()),
                Span::styled(session_id, dim()),
                Span::raw("  "),
                Span::raw(short_cwd(&peak.cwd)),
            ]));
        }

        lines.push(Line::from(""));
        lines.push(Line::from(Span::styled(
            "  top live codex cwds",
            Style::default().add_modifier(Modifier::BOLD),
        )));
        let codex_peak = codex
            .top_cwds
            .first()
            .map(|c| c.total_tokens)
            .unwrap_or(1)
            .max(1);
        for c in codex.top_cwds.iter().take(5) {
            let filled = ((c.total_tokens as f64 / codex_peak as f64) * 24.0).round() as usize;
            let bar: String = "".repeat(filled);
            lines.push(Line::from(vec![
                Span::raw("  "),
                Span::styled(format!("{:>8}", format_tokens(c.total_tokens)), bold()),
                Span::raw("  "),
                Span::styled(format!("{:>3} sess", c.session_count), dim()),
                Span::raw("  "),
                Span::raw(format!("{:<28}", short_cwd(&c.cwd))),
                Span::styled(bar, Style::default().fg(Color::Cyan)),
            ]));
        }

        if !codex.models.is_empty() {
            lines.push(Line::from(""));
            lines.push(Line::from(Span::styled(
                "  live codex by model",
                Style::default().add_modifier(Modifier::BOLD),
            )));
            for m in codex.models.iter().take(5) {
                let pct = if codex.total_tokens > 0 {
                    100.0 * m.total_tokens as f64 / codex.total_tokens as f64
                } else {
                    0.0
                };
                lines.push(Line::from(vec![
                    Span::raw("  "),
                    Span::styled(format!("{:<12}", m.model), bold()),
                    Span::raw("  "),
                    Span::styled(format!("{:>8}", format_tokens(m.total_tokens)), bold()),
                    Span::styled(format!("   {pct:>4.1}%"), dim()),
                    Span::styled(format!("  {} sess", m.session_count), dim()),
                ]));
            }
        }
    }

    app.cost_overlay_total_lines = lines.len() as u16;
    let max_offset = app
        .cost_overlay_total_lines
        .saturating_sub(area.height.saturating_sub(2));
    if app.cost_overlay_offset > max_offset {
        app.cost_overlay_offset = max_offset;
    }

    let title = format!(
        " cost  ·  {} Claude sessions  ·  {} live Codex  ·  scanned {} ms ",
        scanned_files, codex.session_count, scan_duration_ms
    );
    let block = Block::default()
        .borders(Borders::TOP)
        .title(Span::styled(title, dim()));
    let para = Paragraph::new(lines)
        .block(block)
        .wrap(Wrap { trim: false })
        .scroll((app.cost_overlay_offset, 0));
    f.render_widget(para, area);
}

fn draw_key_help(f: &mut Frame, area: Rect) {
    let lines = vec![
        Line::from(vec![
            Span::raw("  "),
            Span::styled("Common", Style::default().add_modifier(Modifier::BOLD)),
        ]),
        Line::from("  Enter jump to selected pane     r reply     a/d approve or deny"),
        Line::from("  / filter sessions              ? keys      q quit"),
        Line::from(""),
        Line::from(vec![
            Span::raw("  "),
            Span::styled("Navigation", Style::default().add_modifier(Modifier::BOLD)),
        ]),
        Line::from("  Up/Down or j/k move selection  gg top      G bottom"),
        Line::from("  Space toggle detail            Esc closes overlays and edit modes"),
        Line::from(""),
        Line::from(vec![
            Span::raw("  "),
            Span::styled(
                "Agent Controls",
                Style::default().add_modifier(Modifier::BOLD),
            ),
        ]),
        Line::from("  A auto mode                    P phone push     m mute"),
        Line::from("  w watch                        * pin to top     R rename"),
        Line::from("  N new agent"),
        Line::from(""),
        Line::from(vec![
            Span::raw("  "),
            Span::styled("Views", Style::default().add_modifier(Modifier::BOLD)),
        ]),
        Line::from("  p preview pane (live)          > flip right/bottom"),
        Line::from("  l audit log (or H)             $ cost overlay"),
        Line::from(""),
        Line::from(vec![
            Span::raw("  "),
            Span::styled("Input Modes", Style::default().add_modifier(Modifier::BOLD)),
        ]),
        Line::from("  reply:  Enter send             Esc cancel       ^W word  ^U clear"),
        Line::from("  filter: Enter jump             Esc clear        arrows navigate"),
        Line::from("  rename: Enter save             Esc cancel       ^W word  ^U clear"),
        Line::from("  new:    Enter launch           Esc/q cancel     Up/Down or j/k choose"),
    ];

    let block = Block::default().borders(Borders::TOP).title(Span::styled(
        " keys  (?/Esc/q close) ",
        Style::default().fg(Color::DarkGray),
    ));
    f.render_widget(
        Paragraph::new(lines)
            .block(block)
            .wrap(Wrap { trim: false }),
        area,
    );
}

// Local copies of the day-second helpers (cost_rollup keeps them private to
// the module since their main consumer is `cli_cost`; the overlay uses the
// same arithmetic). Keeping them inline here avoids exposing more surface
// from cost_rollup than necessary.
fn day_start_secs_from_key(d: crate::cost_rollup::DayKey) -> i64 {
    days_from_civil_local(d.year as i64, d.month, d.day) * 86_400
}

fn day_key_at_secs(secs: i64) -> crate::cost_rollup::DayKey {
    crate::cost_rollup::local_day(
        std::time::UNIX_EPOCH + std::time::Duration::from_secs((secs + 1).max(0) as u64),
    )
}

fn days_from_civil_local(y: i64, m: u32, d: u32) -> i64 {
    let y = if m <= 2 { y - 1 } else { y };
    let era = if y >= 0 { y } else { y - 399 } / 400;
    let yoe = (y - era * 400) as u64;
    let doy = (153 * (if m > 2 { m - 3 } else { m + 9 }) as u64 + 2) / 5 + d as u64 - 1;
    let doe = yoe * 365 + yoe / 4 - yoe / 100 + doy;
    era * 146097 + doe as i64 - 719468
}

fn draw_footer(f: &mut Frame, area: Rect, app: &AppState) {
    if app.key_help_open {
        let hint = "  ?/Esc/q close keys  ·  ^C quit";
        f.render_widget(
            Paragraph::new(Line::from(Span::styled(
                hint.to_string(),
                Style::default().fg(Color::DarkGray),
            ))),
            area,
        );
        return;
    }
    if app.audit_log_open {
        let hint = if app.pending_g {
            "  g … press g again to jump to top, any other key cancels".to_string()
        } else {
            "  j/k scroll  ·  ^d/^u half-page  ·  gg top  ·  G bottom  ·  H/Esc close  ·  q quit"
                .to_string()
        };
        f.render_widget(
            Paragraph::new(Line::from(Span::styled(
                hint,
                Style::default().fg(Color::DarkGray),
            ))),
            area,
        );
        return;
    }
    if app.cost_overlay_open {
        let hint = if app.pending_g {
            "  g … press g again to jump to top, any other key cancels".to_string()
        } else {
            "  j/k scroll  ·  ^d/^u half-page  ·  gg top  ·  G bottom  ·  $/Esc close  ·  q quit"
                .to_string()
        };
        f.render_widget(
            Paragraph::new(Line::from(Span::styled(
                hint,
                Style::default().fg(Color::DarkGray),
            ))),
            area,
        );
        return;
    }
    if app.spawn_picker_open {
        let hint = "  ↑↓/j/k choose cwd  ·  ↵ launch  ·  Esc/q cancel  ·  ^C quit";
        f.render_widget(
            Paragraph::new(Line::from(Span::styled(
                hint.to_string(),
                Style::default().fg(Color::DarkGray),
            ))),
            area,
        );
        return;
    }
    if app.reply_active {
        let hint = "  reply  ·  ↵ send  ·  Esc cancel  ·  ^W word  ·  ^U clear";
        f.render_widget(
            Paragraph::new(Line::from(Span::styled(
                hint.to_string(),
                Style::default().fg(Color::DarkGray),
            ))),
            area,
        );
        return;
    }
    if app.filter_active {
        let hint = "  type to filter  ·  ↑↓ nav  ·  ↵ keep  ·  Esc clear  ·  ^W word  ·  ^U line";
        f.render_widget(
            Paragraph::new(Line::from(Span::styled(
                hint.to_string(),
                Style::default().fg(Color::DarkGray),
            ))),
            area,
        );
        return;
    }
    if app.rename_active {
        let hint = "  rename alias  ·  ↵ save  ·  Esc cancel  ·  ^W word  ·  ^U clear";
        f.render_widget(
            Paragraph::new(Line::from(Span::styled(
                hint.to_string(),
                Style::default().fg(Color::DarkGray),
            ))),
            area,
        );
        return;
    }
    let line = if let Some(msg) = &app.status_msg {
        Line::from(Span::styled(
            msg.clone(),
            Style::default().fg(Color::Yellow),
        ))
    } else {
        let hint = match LayoutMode::from_width(area.width) {
            LayoutMode::Narrow => " ⏎ r a/d / ? q".to_string(),
            LayoutMode::Medium => " ⏎ jump  r reply  a/d approve  / filter  ? keys  q".to_string(),
            LayoutMode::Wide | LayoutMode::Compact => {
                "  ⏎ jump  r reply  a/d approve  / filter  ? keys  q quit".to_string()
            }
        };
        Line::from(Span::styled(hint, Style::default().fg(Color::DarkGray)))
    };
    f.render_widget(Paragraph::new(line), area);
}

/// Case-insensitive substring match against everything a user might want
/// to search by: the Claude session's `--name`, the tmux window name, the
/// tmux session name, and the full cwd path. `q` is already lowercased by
/// the caller. Returning early on any hit keeps this cheap on large fleets.
fn session_matches_filter(s: &Session, q: &str) -> bool {
    if s.provider.label().contains(q) || (s.provider == Provider::Codex && "codex".contains(q)) {
        return true;
    }
    if let Some(name) = &s.name
        && name.to_lowercase().contains(q)
    {
        return true;
    }
    if let Some(pane) = &s.pane {
        if !pane.window_name.is_empty() && pane.window_name.to_lowercase().contains(q) {
            return true;
        }
        if !pane.tmux_session.is_empty() && pane.tmux_session.to_lowercase().contains(q) {
            return true;
        }
    }
    s.cwd.to_string_lossy().to_lowercase().contains(q)
}

fn target_label(s: &Session) -> String {
    format!("{} {}", s.provider.label(), session_display_label(s))
}