tidev 0.1.0

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

use super::diff_render::render_unified_diff_text;
use super::permission::{RunningSubagentExecution, SubagentStatus};
use super::{
    App, MessageRenderCacheEntry, MessageRenderCacheKey, MessageRenderCacheKind,
    MessageRenderCacheValue, render::*,
};

const TOOL_OUTPUT_PREVIEW_LINES: usize = 5;
const TOOL_OUTPUT_EXPANDED_MAX_LINES: usize = 100;

use crate::app::runtime::state::SelectableRegionRange;

#[derive(Clone, Debug)]
struct ToolResultCardRange {
    message_id: Uuid,
    start_line: usize,
    end_line: usize,
}

#[derive(Clone, Debug)]
struct RunningCardRange {
    execution_index: usize,
    start_line: usize,
    end_line: usize,
}

struct RenderContext<'a> {
    palette: ThemePalette,
    spinner: &'a str,
    #[allow(dead_code)]
    workspace_root: &'a Path,
    expanded_tool_results: &'a HashSet<Uuid>,
    expanded_tool_outputs: &'a HashMap<Uuid, String>,
}

fn render_tool_call_with_result(
    tool_call: &ToolCall,
    tool_result: Option<&Message>,
    body_width: usize,
    is_streaming: bool,
    ctx: &RenderContext<'_>,
) -> (Vec<Line<'static>>, Vec<SelectableRegionRange>) {
    let palette = ctx.palette;
    let canonical_name = canonical_tool_name(&tool_call.name).unwrap_or(&tool_call.name);

    // Check if this is a pending call (arguments not complete)
    let is_pending = tool_result.is_none()
        && is_streaming
        && !matches!(canonical_name, "read" | "list" | "glob" | "grep")
        && !tool_call_arguments_are_complete(&tool_call.arguments);

    if matches!(canonical_name, "list" | "grep" | "glob" | "read") {
        return (
            render_tool_call_summary_line(tool_call, tool_result, body_width, palette, ctx),
            vec![],
        );
    }

    // Get result lines and exit code (for bash)
    let (result_lines, exit_code, mut regions) = if let Some(result_msg) = tool_result {
        render_tool_result_detail_lines(result_msg, body_width, ctx)
    } else {
        (Vec::new(), None, vec![])
    };

    // Get rtk_rewritten from tool result message
    let rtk_rewritten = tool_result.map(|m| m.rtk_rewritten).unwrap_or(false);

    let mut lines = Vec::new();
    lines.push(Line::from(""));

    let call_lines =
        render_tool_call_lines(tool_call, body_width, palette, exit_code, rtk_rewritten, ctx.workspace_root);
    lines.extend(call_lines);

    if is_pending {
        // Show calling status inline
        lines.push(Line::from(vec![
            Span::styled(
                format!("{} ", ctx.spinner),
                Style::default().fg(palette.accent_soft),
            ),
            Span::styled("Calling...", Style::default().fg(palette.muted)),
        ]));
    } else if !result_lines.is_empty() {
        lines.push(Line::from(""));
        let offset = lines.len();
        for r in &mut regions {
            r.start_line += offset;
            r.end_line += offset;
        }
        lines.extend(result_lines);
    }

    lines.push(Line::from(""));
    (lines, regions)
}

fn render_compaction_divider_line(
    label: &str,
    width: usize,
    palette: ThemePalette,
) -> Line<'static> {
    let label_width = UnicodeWidthStr::width(label);
    if width <= label_width.saturating_add(2) {
        return line_with_style(label, palette.accent_soft);
    }

    let remaining = width - label_width - 2;
    let left = remaining / 2;
    let right = remaining - left;

    let mut spans = Vec::new();
    if left > 0 {
        spans.push(Span::styled(
            "".repeat(left),
            Style::default().fg(palette.muted),
        ));
        spans.push(Span::raw(" "));
    }
    spans.push(Span::styled(
        label.to_string(),
        Style::default().fg(palette.accent_soft),
    ));
    spans.push(Span::raw(" "));
    if right > 0 {
        spans.push(Span::styled(
            "".repeat(right),
            Style::default().fg(palette.muted),
        ));
    }

    Line::from(spans)
}

fn tool_call_arguments_are_complete(arguments: &str) -> bool {
    serde_json::from_str::<serde_json::Value>(arguments).is_ok()
}

fn render_tool_call_summary_line(
    tool_call: &ToolCall,
    tool_result: Option<&Message>,
    body_width: usize,
    palette: ThemePalette,
    ctx: &RenderContext<'_>,
) -> Vec<Line<'static>> {
    let canonical_name = canonical_tool_name(&tool_call.name).unwrap_or(&tool_call.name);
    let fields = summarize_tool_arguments(&tool_call.name, &tool_call.arguments);

    let get_field = |name: &str| {
        fields
            .iter()
            .find(|(k, _)| k == name)
            .map(|(_, v)| v.as_str())
    };

    let rel_path = |p: &str| display_workspace_relative(ctx.workspace_root, Path::new(p));

    let (action_label, target) = match canonical_name {
        "list" => {
            let path = get_field("path").unwrap_or(".");
            ("List", rel_path(path).to_string())
        }
        "grep" => {
            let pattern = get_field("pattern").unwrap_or("");
            let path = get_field("path").unwrap_or(".");
            ("Search", format!("\"{}\" in {}", pattern, rel_path(path)))
        }
        "glob" => {
            let pattern = get_field("pattern").unwrap_or("*");
            let path = get_field("path").unwrap_or(".");
            ("Find", format!("{} in {}", pattern, rel_path(path)))
        }
        "read" => {
            let path = get_field("path").unwrap_or("file");
            ("Read", rel_path(path).to_string())
        }
        _ => {
            let summary = summarize_tool_call(&tool_call.name, &tool_call.arguments, body_width, ctx.workspace_root);
            return vec![Line::from(vec![Span::styled(
                summary,
                Style::default()
                    .fg(palette.text)
                    .add_modifier(Modifier::BOLD),
            )])];
        }
    };

    let result_suffix = if let Some(result_msg) = tool_result {
        let output = tool_output_from_message(result_msg, ctx).trim();
        compute_tool_result_suffix(canonical_name, output)
    } else {
        " ...".to_string()
    };

    let line = Line::from(vec![
        Span::styled(
            format!("{} ", action_label),
            Style::default().fg(palette.accent_soft),
        ),
        Span::styled(
            target.clone(),
            Style::default()
                .fg(palette.text)
                .add_modifier(Modifier::BOLD),
        ),
        Span::styled(result_suffix, Style::default().fg(palette.muted)),
    ]);

    // Wrap the line if it exceeds body_width, with subsequent lines indented to align with target
    let indent_width = UnicodeWidthStr::width(action_label) + 1; // +1 for the space after label
    let indent = Line::from(" ".repeat(indent_width));
    let wrapped = word_wrap_line(
        &line,
        WrapOptions::new(body_width)
            .subsequent_indent(indent)
            .break_words(true),
    );
    // Convert to owned lines to satisfy lifetime requirements
    wrapped
        .into_iter()
        .map(|l| {
            Line::from(
                l.spans
                    .into_iter()
                    .map(|s| Span::styled(s.content.to_string(), s.style))
                    .collect::<Vec<_>>(),
            )
        })
        .collect()
}

fn compute_tool_result_suffix(canonical_name: &str, output: &str) -> String {
    match canonical_name {
        "list" => {
            if output == "(empty)" {
                " → empty".to_string()
            } else {
                let count = output
                    .lines()
                    .filter(|line| !line.trim().is_empty())
                    .count()
                    .saturating_sub(1); // Subtract the path line
                format!("{} items", count)
            }
        }
        "grep" | "glob" => {
            if tool_output_is_error(output) {
                let count = if output.is_empty() {
                    0
                } else {
                    output.lines().count()
                };
                format!(" → failed ({} lines)", count)
            } else {
                let count = if output.is_empty() {
                    0
                } else {
                    output.lines().count()
                };
                format!("{} matches", count)
            }
        }
        "read" => {
            if tool_output_is_error(output) {
                if output.contains("file not found") && output.contains("Did you mean") {
                    " → not found (with suggestions)".to_string()
                } else {
                    " → error".to_string()
                }
            } else if output.contains("Image read successfully") {
                " → image".to_string()
            } else if output.contains("<type>directory</type>") {
                let count = output
                    .lines()
                    .filter(|line| !line.trim().is_empty())
                    .count()
                    .saturating_sub(2); // Approximate, list_dir output is different now
                format!(" → directory ({} entries)", count)
            } else {
                let metadata = parse_read_content_metadata(output);
                let is_size_truncated = output.contains("Output capped at 50 KB");

                match metadata {
                    Some(((start, end), requested_range, total, truncated_by)) => {
                        // Check if this is a full file read (start==1 && end==total)
                        let is_full_file = start == 1 && end == total;
                        let has_requested_range = requested_range.is_some();

                        if is_size_truncated {
                            // 50KB truncation
                            if has_requested_range {
                                let (req_start, req_end) = requested_range.unwrap();
                                if is_full_file {
                                    format!(
                                        " → All {} lines (requested {}-{}, truncated due to 50KB cap)",
                                        total, req_start, req_end
                                    )
                                } else {
                                    format!(
                                        " → Line {}-{} of {} (requested {}-{}, truncated due to 50KB cap)",
                                        start, end, total, req_start, req_end
                                    )
                                }
                            } else if is_full_file {
                                format!(
                                    " → All {} lines (requested all lines, truncated due to 50KB cap)",
                                    total
                                )
                            } else {
                                format!(
                                    " → Line {}-{} of {} (requested all lines, truncated due to 50KB cap)",
                                    start, end, total
                                )
                            }
                        } else if truncated_by.as_deref() == Some("lines") {
                            // 2000-line cap (more flag, but no 50KB cutoff)
                            if has_requested_range {
                                let (req_start, req_end) = requested_range.unwrap();
                                if is_full_file {
                                    format!(
                                        " → All {} lines (requested {}-{}, truncated due to 2000 lines cap)",
                                        total, req_start, req_end
                                    )
                                } else {
                                    format!(
                                        " → Line {}-{} of {} (requested {}-{}, truncated due to 2000 lines cap)",
                                        start, end, total, req_start, req_end
                                    )
                                }
                            } else if is_full_file {
                                format!(
                                    " → All {} lines (requested all lines, truncated due to 2000 lines cap)",
                                    total
                                )
                            } else {
                                format!(
                                    " → Line {}-{} of {} (requested all lines, truncated due to 2000 lines cap)",
                                    start, end, total
                                )
                            }
                        } else if is_full_file {
                            // Complete file read without truncation
                            format!(" → All {} lines", total)
                        } else {
                            // Partial read without truncation
                            format!(" → Line {}-{} of {}", start, end, total)
                        }
                    }
                    None => {
                        // Fallback: try old format parsing
                        let line_range = parse_line_range_from_read_output(output);
                        let truncated = tool_output_is_truncated(output);

                        match line_range {
                            Some((start, end)) => {
                                if truncated && output.contains("Output capped at 50 KB") {
                                    format!(" → Line {}-{} (truncated)", start, end)
                                } else {
                                    format!(" → Line {}-{}", start, end)
                                }
                            }
                            None => {
                                let total_lines = output.lines().count();
                                if total_lines == 0 {
                                    " → empty".to_string()
                                } else if truncated {
                                    format!(" → First {} lines (truncated)", total_lines)
                                } else {
                                    format!(" → All {} lines", total_lines)
                                }
                            }
                        }
                    }
                }
            }
        }
        _ => String::new(),
    }
}

fn tool_output_is_truncated(output: &str) -> bool {
    output.contains("output truncated:")
        || output.contains("... (truncated)")
        || output.contains("(Output capped at")
        || output.contains("[truncated]")
}

/// Parse bash output to extract exit code and remaining output.
/// Format: "[exit N]\n<output>"
fn parse_bash_exit_code(output: &str) -> (Option<i32>, &str) {
    // Look for "[exit N]" prefix
    if let Some(stripped) = output.strip_prefix("[exit ") {
        // Find the closing bracket
        if let Some(end_idx) = stripped.find(']') {
            let code_str = &stripped[..end_idx];
            if let Ok(code) = code_str.parse::<i32>() {
                // Skip the "] " or "]" and newline
                let remaining = &stripped[end_idx + 1..];
                let remaining = remaining.strip_prefix('\n').unwrap_or(remaining);
                return (Some(code), remaining);
            }
        }
    }
    (None, output)
}

fn render_tool_call_lines(
    tool_call: &ToolCall,
    body_width: usize,
    palette: ThemePalette,
    exit_code: Option<i32>,
    rtk_rewritten: bool,
    workspace_root: &Path,
) -> Vec<Line<'static>> {
    let fields = summarize_tool_arguments(&tool_call.name, &tool_call.arguments);

    let get_field = |name: &str| {
        fields
            .iter()
            .find(|(k, _)| k == name)
            .map(|(_, v)| v.as_str())
    };

    let mut lines = Vec::new();

    let canonical_display = canonical_tool_name(&tool_call.name)
        .map(|s| s.to_string())
        .unwrap_or_else(|| tool_call.name.clone());

    let canonical_name = canonical_tool_name(&tool_call.name).unwrap_or("");

    // Build title line with optional exit code for bash
    let title_spans = if canonical_name == "bash" {
        // Build the base spans for bash tool
        let mut spans = vec![
            Span::styled("Tool: ", Style::default().fg(palette.muted)),
            Span::styled(
                canonical_display.clone(),
                Style::default()
                    .fg(palette.text)
                    .add_modifier(Modifier::BOLD),
            ),
        ];

        // Add [rtk] marker after tool name if command was rewritten
        if rtk_rewritten {
            spans.push(Span::styled(
                " [rtk]",
                Style::default().fg(palette.accent_soft),
            ));
        }

        // Add exit code status
        if let Some(code) = exit_code {
            if code == 0 {
                spans.push(Span::styled("", Style::default().fg(palette.success)));
            } else {
                spans.push(Span::styled(
                    format!("{}", code),
                    Style::default().fg(palette.error),
                ));
            }
        }
        spans
    } else {
        vec![
            Span::styled("Tool: ", Style::default().fg(palette.muted)),
            Span::styled(
                canonical_display.clone(),
                Style::default()
                    .fg(palette.text)
                    .add_modifier(Modifier::BOLD),
            ),
        ]
    };
    lines.push(Line::from(title_spans));

    match canonical_name {
        "bash" => {
            let command = get_field("command").unwrap_or("");
            let desc = get_field("description");

            if let Some(d) = desc {
                lines.push(Line::from(vec![
                    Span::styled("  Description: ", Style::default().fg(palette.muted)),
                    Span::styled(d.to_string(), Style::default().fg(palette.text)),
                ]));
            }

            for line in command.lines() {
                let owned_line = Line::from(line.to_string());
                let wrapped = word_wrap_line(
                    &owned_line,
                    WrapOptions::new(body_width.saturating_sub(4)).break_words(true),
                );
                for (i, wrapped_line) in wrapped.iter().enumerate() {
                    let mut spans = Vec::new();
                    if i == 0 {
                        spans.push(Span::styled("  $ ", Style::default().fg(palette.accent)));
                    } else {
                        spans.push(Span::styled("    ", Style::default()));
                    }
                    spans.extend(wrapped_line.spans.iter().map(|s| {
                        Span::styled(s.content.to_string(), Style::default().fg(palette.text))
                    }));
                    lines.push(Line::from(spans));
                }
            }
        }
        "write" => {
            let path = get_field("path").unwrap_or("file");
            let rel_path = display_workspace_relative(workspace_root, Path::new(path));
            lines.push(Line::from(vec![
                Span::styled("  Path: ", Style::default().fg(palette.muted)),
                Span::styled(rel_path, Style::default().fg(palette.text)),
            ]));
        }
        _ => {
            let summary = summarize_tool_call(&tool_call.name, &tool_call.arguments, body_width, workspace_root);
            for line in summary.lines() {
                lines.push(Line::from(vec![
                    Span::styled("  ", Style::default()),
                    Span::styled(line.to_string(), Style::default().fg(palette.text)),
                ]));
            }
        }
    }

    lines
}

fn render_tool_result_detail_lines(
    message: &Message,
    body_width: usize,
    ctx: &RenderContext<'_>,
) -> (Vec<Line<'static>>, Option<i32>, Vec<SelectableRegionRange>) {
    let palette = ctx.palette;
    let output = tool_output_from_message(message, ctx);
    let tool_name = message.tool_name.as_deref().unwrap_or(message.role.label());
    let canonical_name = canonical_tool_name(tool_name).unwrap_or(tool_name);

    // For bash, parse exit code and strip it from output
    let (exit_code, effective_output) = if canonical_name == "bash" {
        parse_bash_exit_code(output)
    } else {
        (None, output)
    };

    let is_error = tool_output_is_error(effective_output);

    // Try to render diff from metadata first (preferred, full diff not truncated)
    if !is_error
        && matches!(canonical_name, "edit" | "write" | "apply_patch")
        && let Some(diff) = message.metadata.diff.as_ref()
        && let Some((diff_lines, regions)) = render_unified_diff_text(diff, body_width, palette)
    {
        return (diff_lines, None, regions);
    }

    // Fallback: try to render diff from output (may be truncated)
    if !is_error
        && matches!(canonical_name, "edit" | "write" | "apply_patch")
        && let Some((diff_lines, regions)) =
            render_unified_diff_text(effective_output, body_width, palette)
    {
        return (diff_lines, None, regions);
    }

    if canonical_name == "todowrite" && !is_error {
        #[derive(serde::Deserialize)]
        struct RawTodo {
            content: String,
            status: Option<String>,
            priority: Option<String>,
        }

        let raw_todos = if let Ok(todos) = serde_json::from_str::<Vec<RawTodo>>(effective_output) {
            Some(todos)
        } else if let Ok(value) = serde_json::from_str::<serde_json::Value>(effective_output) {
            value
                .get("todos")
                .and_then(|v| serde_json::from_value::<Vec<RawTodo>>(v.clone()).ok())
        } else {
            None
        };

        if let Some(raw_todos) = raw_todos {
            let todos: Vec<TodoItem> = raw_todos
                .into_iter()
                .map(|r| TodoItem {
                    content: r.content,
                    status: r.status.unwrap_or_else(|| "pending".to_string()),
                    priority: r.priority.unwrap_or_else(|| "medium".to_string()),
                })
                .collect();
            return (
                render_todos_checkbox_list(&todos, body_width, palette),
                None,
                vec![],
            );
        }
    }

    // Subagent task results: render markdown preview (collapsed) or full raw (expanded)
    if canonical_name == "task" {
        let is_expanded = ctx.expanded_tool_results.contains(&message.id);
        if !is_expanded {
            return (
                render_subagent_task_preview(effective_output, body_width, palette),
                None,
                vec![],
            );
        }
        // Expanded: fall through to full raw output below
    }

    (
        render_output_preview_lines(
            effective_output,
            body_width,
            is_error,
            Some(message.id),
            ctx.expanded_tool_results,
            palette,
        ),
        exit_code,
        vec![],
    )
}

/// Renders a compact markdown preview of a subagent task result.
fn render_subagent_task_preview(
    output: &str,
    body_width: usize,
    palette: ThemePalette,
) -> Vec<Line<'static>> {
    let mut lines = Vec::new();

    if output.trim().is_empty() {
        lines.push(line_with_style("(empty result)", palette.muted));
        return lines;
    }

    // Title header
    lines.push(Line::from(vec![
        Span::styled("Task ", Style::default().fg(palette.accent_soft)),
        Span::styled("· subagent result", Style::default().fg(palette.muted)),
    ]));
    lines.push(Line::from(""));

    // Render the output as markdown, then take the first few lines
    let rendered =
        render_markdown_text_with_width_and_cwd(output, Some(body_width.saturating_sub(2)), None);
    let md_lines: Vec<Line<'static>> = rendered.lines;

    let max_preview = TOOL_OUTPUT_PREVIEW_LINES;
    let line_count = md_lines.len();

    if line_count <= max_preview {
        lines.extend(md_lines);
    } else {
        lines.extend(md_lines.into_iter().take(max_preview));
        lines.push(Line::from(vec![Span::styled(
            format!(
                "{} more line(s) — Click to expand",
                line_count - max_preview
            ),
            Style::default().fg(palette.muted),
        )]));
    }

    // Always add Ctrl+Click hint
    lines.push(Line::from(vec![Span::styled(
        "  Ctrl+Click to enter subsession",
        Style::default().fg(palette.muted),
    )]));

    lines
}

fn render_todos_checkbox_list(
    todos: &[TodoItem],
    body_width: usize,
    palette: ThemePalette,
) -> Vec<Line<'static>> {
    let mut lines = vec![line_with_style("Updated todo list:", palette.accent_soft)];

    if todos.is_empty() {
        lines.push(line_with_style("  (no items)", palette.muted));
        return lines;
    }

    let max_content_len = body_width.saturating_sub(6).max(1);

    for todo in todos {
        let (checkbox, style) = match todo.status.as_str() {
            "completed" => (
                "",
                Style::default()
                    .fg(palette.muted)
                    .add_modifier(Modifier::CROSSED_OUT),
            ),
            "in_progress" => (
                "",
                Style::default()
                    .fg(palette.accent)
                    .add_modifier(Modifier::BOLD),
            ),
            "pending" => ("", Style::default().fg(palette.text)),
            "cancelled" => ("", Style::default().fg(palette.muted)),
            _ => ("", Style::default().fg(palette.text)),
        };

        let priority_marker = if todo.priority == "high" { "" } else { "" };

        let content = shorten(&todo.content, max_content_len);
        lines.push(Line::from(vec![
            Span::styled(format!("  {priority_marker}{checkbox}"), style),
            Span::styled(content, style),
        ]));
    }

    lines
}

fn tool_output_from_message<'a>(message: &'a Message, ctx: &'a RenderContext<'_>) -> &'a str {
    ctx.expanded_tool_outputs
        .get(&message.id)
        .map(|output| output.as_str())
        .unwrap_or_else(|| message.content.as_str())
}

fn render_output_preview_lines(
    output: &str,
    body_width: usize,
    is_error: bool,
    message_id: Option<Uuid>,
    expanded_tool_results: &HashSet<Uuid>,
    palette: ThemePalette,
) -> Vec<Line<'static>> {
    let mut lines = Vec::new();

    let is_expanded = message_id.is_some_and(|id| expanded_tool_results.contains(&id));
    let max_lines = if is_expanded {
        TOOL_OUTPUT_EXPANDED_MAX_LINES
    } else if is_error {
        4
    } else {
        TOOL_OUTPUT_PREVIEW_LINES
    };

    let fg = if is_error {
        palette.error
    } else {
        palette.text
    };

    let total_output_lines = output.lines().count();
    let wrap_width = body_width.saturating_sub(2);

    for line in output.lines().take(max_lines) {
        let owned_line = Line::from(line.to_string());
        if is_expanded {
            let wrapped =
                word_wrap_line(&owned_line, WrapOptions::new(wrap_width).break_words(true));
            for wrapped_line in wrapped.iter() {
                let mut spans = Vec::new();
                spans.extend(
                    wrapped_line.spans.iter().map(|span| {
                        Span::styled(span.content.to_string(), Style::default().fg(fg))
                    }),
                );
                lines.push(Line::from(spans));
            }
        } else {
            let wrapped =
                word_wrap_line(&owned_line, WrapOptions::new(wrap_width).break_words(true));
            if let Some(first_wrapped) = wrapped.first() {
                let mut content = first_wrapped
                    .spans
                    .iter()
                    .map(|s| &*s.content)
                    .collect::<String>();
                if wrapped.len() > 1 || total_output_lines > max_lines {
                    // Try to fit "..."
                    if content.width() >= wrap_width {
                        content = shorten_single_line(&content, wrap_width.saturating_sub(3));
                        content.push_str("...");
                    } else {
                        content.push_str("...");
                    }
                }
                lines.push(Line::from(vec![Span::styled(
                    content,
                    Style::default().fg(fg),
                )]));
            }
        }
    }

    if total_output_lines > max_lines {
        lines.push(Line::from(vec![Span::styled(
            format!("... {} more line(s)", total_output_lines - max_lines),
            Style::default().fg(palette.muted),
        )]));
    } else if total_output_lines > TOOL_OUTPUT_PREVIEW_LINES && message_id.is_some() {
        let hint = if is_expanded {
            "▲ Click to collapse"
        } else {
            "▼ Click to expand"
        };
        lines.push(Line::from(vec![Span::styled(
            hint,
            Style::default().fg(palette.muted),
        )]));
    }

    if lines.is_empty() {
        lines.push(line_with_style("(no output)", palette.muted));
    }

    lines
}

impl App {
    pub(super) fn render_chat(&mut self, frame: &mut Frame<'_>) {
        let area = frame.area();
        let palette = self.palette();
        frame.render_widget(
            Block::default().style(Style::default().bg(palette.background)),
            area,
        );

        let sidebar_visible = area.width >= self.config.ui.sidebar_width.saturating_add(70);
        let main_area = if sidebar_visible {
            let split = Layout::horizontal([
                Constraint::Min(20),
                Constraint::Length(self.config.ui.sidebar_width),
            ])
            .split(area);
            self.sidebar_area = Some(split[1]);
            self.render_sidebar(frame, split[1]);
            split[0]
        } else {
            area
        };

        let composer_height = self
            .composer
            .preferred_height(
                main_area.width.saturating_sub(4),
                self.config.ui.max_input_lines,
            )
            .min(main_area.height.saturating_sub(3).max(3));

        if let Some(dialog) = self.question_dialog.clone() {
            let question_height = dialog
                .prompt_height(main_area.width, composer_height)
                .min(main_area.height.saturating_sub(3).max(6));

            let layout = Layout::vertical([
                Constraint::Min(6),
                Constraint::Length(question_height),
                Constraint::Length(1),
                Constraint::Length(1),
            ])
            .split(main_area);

            self.render_messages(frame, layout[0]);
            self.render_question_dialog(frame, layout[1], &dialog);
            self.render_prompt_footer(frame, layout[2]);
            self.render_retrying_hint(frame, layout[3]);
            return;
        }

        let layout = Layout::vertical([
            Constraint::Min(6),
            Constraint::Length(composer_height),
            Constraint::Length(1),
            Constraint::Length(1),
        ])
        .split(main_area);

        self.render_messages(frame, layout[0]);

        // In subsession, show navigation panel instead of input box
        if self.conversation.parent_session_id.is_some() {
            self.render_subsession_navigation(frame, layout[1]);
        } else {
            let prompt_title = match self.pending_mode.as_ref() {
                Some(pending) if self.pending_request => {
                    format!(
                        "{} (current), {} (on completion)",
                        self.mode.title(),
                        pending.title()
                    )
                }
                _ => self.mode.title().to_string(),
            };
            self.render_input_block(
                frame,
                layout[1],
                &prompt_title,
                self.composer.placeholder(),
                false,
            );
            self.render_at_mention_palette(frame, layout[1]);
            self.render_snippet_palette(frame, layout[1]);
            self.render_command_palette(frame, layout[1]);
            self.render_snippet_palette(frame, layout[1]);
        }
        self.render_prompt_footer(frame, layout[2]);
        self.render_retrying_hint(frame, layout[3]);
    }

    fn render_subsession_navigation(&self, frame: &mut Frame<'_>, area: Rect) {
        let palette = self.palette();

        let block = Block::default()
            .borders(Borders::ALL)
            .border_style(Style::default().fg(palette.border_idle()))
            .title(" Subsession ");

        frame.render_widget(block, area);

        let inner = area.inner(Margin {
            horizontal: 1,
            vertical: 1,
        });

        // Center the navigation hints
        let hint = Line::from(vec![
            Span::styled("Up", Style::default().fg(palette.accent_soft)),
            Span::styled(": return to parent  ", Style::default().fg(palette.muted)),
            Span::styled("Left", Style::default().fg(palette.accent_soft)),
            Span::styled("/", Style::default().fg(palette.muted)),
            Span::styled("Right", Style::default().fg(palette.accent_soft)),
            Span::styled(": switch subagent", Style::default().fg(palette.muted)),
        ]);

        let paragraph = Paragraph::new(hint)
            .alignment(Alignment::Center)
            .style(Style::default().fg(palette.text));

        frame.render_widget(paragraph, inner);
    }

    pub(super) fn render_messages(&mut self, frame: &mut Frame<'_>, area: Rect) {
        let palette = self.palette();
        let mut title = format!("Conversation · {}", shorten(&self.conversation.title, 32),);
        if !self.message_follow_tail {
            title.push_str(" · history");
        }
        if self.conversation.parent_session_id.is_some() {
            title.push_str(" · SUBSESSION");
        }

        let block = Block::default()
            .borders(Borders::ALL)
            .border_style(Style::default().fg(palette.border_idle()))
            .title(title);
        frame.render_widget(block, area);

        let inner = area.inner(Margin {
            horizontal: 2,
            vertical: 1,
        });

        if inner.width == 0 || inner.height == 0 {
            return;
        }

        let scrollbar_area = if inner.width > 1 {
            let chunks =
                Layout::horizontal([Constraint::Min(1), Constraint::Length(1)]).split(inner);
            (chunks[0], Some(chunks[1]))
        } else {
            (inner, None)
        };

        let content_area = scrollbar_area.0;
        self.message_content_area = Some(content_area);
        self.message_viewport_lines = content_area.height as usize;
        let content_width = content_area.width.max(1) as usize;
        let (
            text,
            total_lines,
            card_ranges,
            selectable_regions_ranges,
            rendered_virtualized,
            virtualized_render_scroll,
            running_card_ranges,
        ) = self.messages_text(Some(content_width));

        self.message_total_lines = total_lines;

        let max_scroll = total_lines.saturating_sub(self.message_viewport_lines);
        let scroll = if self.message_follow_tail {
            max_scroll
        } else {
            self.message_scroll_offset.min(max_scroll)
        };

        self.message_scroll_offset = scroll;
        self.message_follow_tail = scroll >= max_scroll;
        let render_scroll = if rendered_virtualized {
            virtualized_render_scroll
        } else {
            scroll
        };

        self.selectable_regions.clear();
        for r in selectable_regions_ranges {
            let screen_start = r.start_line.saturating_sub(render_scroll);
            let screen_end = r.end_line.saturating_sub(render_scroll);
            if screen_end == 0 || screen_start >= self.message_viewport_lines {
                continue;
            }
            let visible_start = screen_start as u16;
            let visible_end = (screen_end.min(self.message_viewport_lines)) as u16;
            if visible_start < visible_end {
                let y = content_area.y.saturating_add(visible_start);
                let height = visible_end.saturating_sub(visible_start);
                let min_x = content_area.x.saturating_add(r.min_x);
                let max_x = r
                    .max_x
                    .map(|mx| content_area.x.saturating_add(mx))
                    .unwrap_or(content_area.x.saturating_add(content_area.width));
                let width = max_x.saturating_sub(min_x);
                if width > 0 {
                    self.selectable_regions.push(Rect {
                        x: min_x,
                        y,
                        width,
                        height,
                    });
                }
            }
        }

        // Calculate screen positions for tool result cards
        self.tool_result_card_bounds.clear();
        for card_range in card_ranges {
            let screen_start = card_range.start_line.saturating_sub(render_scroll);
            let screen_end = card_range.end_line.saturating_sub(render_scroll);

            if screen_end == 0 || screen_start >= self.message_viewport_lines {
                continue;
            }

            let visible_start = screen_start as u16;
            let visible_end = (screen_end.min(self.message_viewport_lines)) as u16;

            if visible_start < visible_end {
                let card_rect = Rect {
                    x: content_area.x,
                    y: content_area.y.saturating_add(visible_start),
                    width: content_area.width,
                    height: visible_end.saturating_sub(visible_start),
                };
                self.tool_result_card_bounds
                    .push((card_range.message_id, card_rect));
            }
        }

        // Calculate screen positions for running subagent cards
        // running_card_ranges contain positions within the running_lines block,
        // which starts at (header_line_count + total_message_lines) in the full text.
        let header_line_count = if self.conversation.parent_session_id.is_some() { 3 } else { 0 };
        let total_msg_lines = self.message_layout_index.borrow().total_lines;
        let running_block_start = header_line_count + total_msg_lines;

        self.running_subagent_card_bounds.clear();
        for card_range in &running_card_ranges {
            let abs_start = running_block_start + card_range.start_line;
            let abs_end = running_block_start + card_range.end_line;

            let screen_start = abs_start.saturating_sub(render_scroll);
            let screen_end = abs_end.saturating_sub(render_scroll);

            if screen_end == 0 || screen_start >= self.message_viewport_lines {
                continue;
            }

            let visible_start = screen_start as u16;
            let visible_end = (screen_end.min(self.message_viewport_lines)) as u16;

            if visible_start < visible_end {
                let card_rect = Rect {
                    x: content_area.x,
                    y: content_area.y.saturating_add(visible_start),
                    width: content_area.width,
                    height: visible_end.saturating_sub(visible_start),
                };
                self.running_subagent_card_bounds
                    .push((card_range.execution_index, card_rect));
            }
        }

        let paragraph = Paragraph::new(text)
            .style(Style::default().bg(palette.background).fg(palette.text))
            .scroll((render_scroll as u16, 0));

        frame.render_widget(paragraph, content_area);

        if let Some(scrollbar_area) = scrollbar_area.1 {
            self.message_scrollbar_area = Some(scrollbar_area);
            self.render_scrollbar(frame, scrollbar_area, scroll, max_scroll);
        }
    }

    fn render_sidebar(&self, frame: &mut Frame<'_>, area: Rect) {
        let palette = self.palette();
        let mut lines = Vec::new();

        // Model info
        lines.push(Line::from(vec![Span::styled(
            "Model",
            Style::default()
                .fg(palette.accent)
                .add_modifier(Modifier::BOLD),
        )]));
        lines.push(Line::from(vec![Span::styled(
            format!(
                "{} / {}",
                self.active_model.provider_id, self.active_model.model_id
            ),
            Style::default().fg(palette.text),
        )]));
        lines.push(Line::from(vec![Span::styled(
            if self.active_model.api_key_present() {
                "✓ API key present"
            } else {
                "✗ API key missing"
            },
            if self.active_model.api_key_present() {
                Style::default().fg(palette.success)
            } else {
                Style::default().fg(palette.error)
            },
        )]));

        if let Some(usage) = &self.context_usage {
            let session_tps: Vec<f32> = self
                .conversation
                .messages
                .iter()
                .filter(|m| matches!(m.role, MessageRole::Assistant))
                .filter_map(|m| m.tokens_per_second)
                .collect();

            if !session_tps.is_empty() {
                let avg_tps = session_tps.iter().sum::<f32>() / session_tps.len() as f32;
                lines.push(Line::from(vec![Span::styled(
                    format!("Speed: {:.1} t/s (avg)", avg_tps),
                    Style::default().fg(palette.muted),
                )]));
            } else if let Some(current_tps) = usage.tokens_per_second {
                lines.push(Line::from(vec![Span::styled(
                    format!("Speed: {:.1} t/s", current_tps),
                    Style::default().fg(palette.muted),
                )]));
            }
        }

        // Token statistics (session cumulative)
        lines.push(Line::from(""));
        lines.push(Line::from(vec![Span::styled(
            "Tokens",
            Style::default()
                .fg(palette.accent)
                .add_modifier(Modifier::BOLD),
        )]));

        let mut token_usage = TokenUsage::default();
        for m in self
            .conversation
            .messages
            .iter()
            .filter(|m| matches!(m.role, MessageRole::Assistant))
        {
            token_usage.add(m.token_usage());
        }

        let total = token_usage.total();
        let total_cache = token_usage.total_cache();

        lines.push(Line::from(vec![Span::styled(
            format!("Total: {}", format_token_count(total)),
            Style::default().fg(palette.text),
        )]));
        lines.push(Line::from(vec![Span::styled(
            format!(
                "In: {}",
                format_token_count(token_usage.input_tokens as u64)
            ),
            Style::default().fg(palette.muted),
        )]));
        lines.push(Line::from(vec![Span::styled(
            format!("Cache: {}", format_token_count(total_cache)),
            Style::default().fg(palette.muted),
        )]));
        lines.push(Line::from(vec![Span::styled(
            format!(
                "Out: {}",
                format_token_count(token_usage.output_tokens as u64)
            ),
            Style::default().fg(palette.muted),
        )]));

        lines.push(Line::from(""));

        // Request count
        let request_count = self
            .conversation
            .messages
            .iter()
            .filter(|m| matches!(m.role, MessageRole::Assistant))
            .count();
        lines.push(Line::from(vec![Span::styled(
            format!("Requests: {request_count}"),
            Style::default().fg(palette.text),
        )]));

        lines.push(Line::from(""));

        // Working directory
        lines.push(Line::from(shorten(
            &self.workspace_root.display().to_string(),
            32,
        )));

        // Todos section
        lines.push(Line::from(""));
        lines.push(Line::from(vec![Span::styled(
            format!("Todos ({})", self.todos.len()),
            Style::default()
                .fg(palette.accent)
                .add_modifier(Modifier::BOLD),
        )]));

        let sidebar_width = self.config.ui.sidebar_width as usize;
        let max_content_len = sidebar_width.saturating_sub(4);

        for todo in &self.todos {
            let (checkbox, style) = match todo.status.as_str() {
                "completed" => (
                    "",
                    Style::default()
                        .fg(palette.muted)
                        .add_modifier(Modifier::CROSSED_OUT),
                ),
                "in_progress" => ("", Style::default().fg(palette.accent)),
                "pending" => ("", Style::default().fg(palette.text)),
                "cancelled" => ("", Style::default().fg(palette.muted)),
                _ => ("", Style::default().fg(palette.text)),
            };

            let priority_marker = if todo.priority == "high" { "" } else { "" };

            let content = shorten(&todo.content, max_content_len);
            lines.push(Line::from(vec![
                Span::styled(format!("{priority_marker}{checkbox}"), style),
                Span::styled(content, style),
            ]));
        }

        // Undo state (only when active)
        if self.conversation.is_reverted() {
            lines.push(Line::from(""));
            lines.push(Line::from(vec![Span::styled(
                "⚠ Undo active",
                Style::default().fg(palette.warning),
            )]));
        }

        let paragraph = Paragraph::new(Text::from(lines))
            .block(
                Block::default()
                    .borders(Borders::ALL)
                    .border_style(Style::default().fg(palette.border_idle()))
                    .title("Sidebar"),
            )
            .style(Style::default().fg(palette.text));

        frame.render_widget(paragraph, area);
    }

    fn messages_text(
        &mut self,
        content_width: Option<usize>,
    ) -> (
        Text<'static>,
        usize,
        Vec<ToolResultCardRange>,
        Vec<SelectableRegionRange>,
        bool,
        usize,
        Vec<RunningCardRange>,
    ) {
        let started_at = Instant::now();
        let palette = self.palette();
        let width = content_width.unwrap_or(1).max(1);
        let body_width = width.saturating_sub(2).max(1);
        let messages = self.conversation.visible_messages();

        let mut lines = Vec::new();
        let mut card_ranges = Vec::new();
        let mut selectable_regions_ranges = Vec::new();
        let mut running_card_ranges = Vec::new();

        // Header for subsessions (always visible at top)
        let header_lines = if self.conversation.parent_session_id.is_some() {
            vec![
                line_with_style(
                    "SUBSESSION active — viewing a child session.",
                    palette.accent_soft,
                ),
                line_with_style(
                    "Press Ctrl+X then Up arrow to return to the parent session.",
                    palette.muted,
                ),
                Line::from(""),
            ]
        } else {
            Vec::new()
        };

        // Handle empty messages case
        if messages.is_empty() {
            lines.extend(header_lines);
            lines.extend(decorate_card_lines(
                vec![
                    line_with_style("No messages yet.", palette.muted),
                    line_with_style("Start with a prompt in the input box below.", palette.muted),
                ],
                width,
                palette.panel,
            ));
            let total_lines = lines.len().max(1);
            return (
                Text::from(lines),
                total_lines,
                card_ranges,
                selectable_regions_ranges,
                false,
                0,
                running_card_ranges,
            );
        }

        // Update layout index
        self.update_message_layout_index(width, body_width, false);
        if let Some(scroll_offset) = self.resolve_message_scroll_target(messages, width, body_width)
        {
            self.message_scroll_offset = scroll_offset;
            self.message_follow_tail = false;
            self.message_scroll_target = None;
        }

        let mut running_lines = Vec::new();
        if self.conversation.parent_session_id.is_none() {
            for (index, running_subagent) in self.running_subagent_executions.iter().enumerate() {
                let card_lines = self.render_running_subagent_lines(running_subagent, width);
                if card_lines.is_empty() {
                    continue;
                }

                let card_start = running_lines.len();
                let decorated_lines =
                    super::render::decorate_card_lines(card_lines, width, palette.panel);
                running_lines.extend(decorated_lines);
                let card_end = running_lines.len();

                running_card_ranges.push(RunningCardRange {
                    execution_index: index,
                    start_line: card_start,
                    end_line: card_end,
                });
            }
        }
        let total_running_lines = running_lines.len();

        // Calculate visible range based on scroll position
        let viewport = self.message_viewport_lines.max(1);
        let total_message_lines = self.message_layout_index.borrow().total_lines;
        let total_overall_lines = total_message_lines + total_running_lines;
        let header_line_count = header_lines.len();

        let max_scroll = (header_line_count + total_overall_lines).saturating_sub(viewport);
        let scroll = if self.message_follow_tail {
            max_scroll
        } else {
            self.message_scroll_offset.min(max_scroll)
        };
        self.message_scroll_offset = scroll;

        // the 'scroll' includes header lines. To find the correct message block, we must
        // offset the scroll past the header
        let message_scroll = scroll.saturating_sub(header_line_count);

        // Find visible blocks using the message-relative scroll
        let visible_blocks = self.find_visible_message_blocks(message_scroll, viewport);

        lines.extend(header_lines);

        // Calculate render_scroll for virtualized rendering
        // The visible blocks may start before 'message_scroll' (due to buffer zone),
        // so we need to skip those lines when rendering.
        // Also, if first block starts after 'message_scroll', we need padding.
        let first_block_start = visible_blocks.first().map(|b| b.start_line).unwrap_or(0);

        let (mut render_scroll, padding_lines) = if first_block_start < message_scroll {
            (message_scroll - first_block_start, 0)
        } else if first_block_start > message_scroll {
            (0, first_block_start - message_scroll)
        } else {
            (0, 0)
        };

        // Important: if we are scrolled inside the header, the render_scroll applies entirely to the header.
        // Otherwise, it skips the entire header PLUS block-relative scroll.
        if scroll < header_line_count {
            render_scroll = scroll;
        } else {
            render_scroll += header_line_count;
        }

        // Add padding lines if first block starts after scroll position
        for _ in 0..padding_lines {
            lines.push(Line::from(""));
        }

        // Create render context for tool calls
        let expanded_tool_outputs = self.load_expanded_tool_outputs(messages);
        let spinner = self.loading_spinner();
        let ctx = RenderContext {
            palette,
            spinner,
            workspace_root: self.workspace_root.as_path(),
            expanded_tool_results: &self.expanded_tool_results,
            expanded_tool_outputs: &expanded_tool_outputs,
        };

        // Render visible blocks
        let mut current_line_offset = header_line_count + padding_lines;
        for block in &visible_blocks {
            // Round end = no next message (session end) OR next message is User (new round)
            let next_idx = block.message_start_idx + block.message_count;
            let is_round_end =
                next_idx >= messages.len() || matches!(messages[next_idx].role, MessageRole::User);
            let block_lines = self.render_message_block_to_lines(
                messages,
                block,
                width,
                body_width,
                &mut card_ranges,
                &mut selectable_regions_ranges,
                current_line_offset,
                &ctx,
                is_round_end,
            );
            current_line_offset += block_lines.len();
            lines.extend(block_lines);
        }

        let last_block_end = visible_blocks
            .last()
            .map(|b| b.start_line + b.line_count)
            .unwrap_or(0);
        let missing_lines = total_message_lines.saturating_sub(last_block_end);
        for _ in 0..missing_lines {
            lines.push(Line::from(""));
        }

        lines.extend(running_lines);

        // Calculate total lines from layout index
        let total_lines = header_line_count + total_overall_lines;

        let elapsed = started_at.elapsed();
        if elapsed > Duration::from_millis(12) {
            let (hits, misses, entries) = self.message_render_cache_stats();
            crate::log_debug!(
                "messages_text: messages={}, visible_blocks={}, width={}, took={:?}, cache_hits={}, cache_misses={}, cache_entries={}",
                messages.len(),
                visible_blocks.len(),
                width,
                elapsed,
                hits,
                misses,
                entries
            );
        }

        (
            Text::from(lines),
            total_lines,
            card_ranges,
            selectable_regions_ranges,
            true,
            render_scroll,
            running_card_ranges,
        )
    }

    fn cached_render_tool_call_with_result(
        &self,
        message: &Message,
        tool_call: &ToolCall,
        tool_result: Option<&Message>,
        body_width: usize,
        is_streaming: bool,
        ctx: &RenderContext<'_>,
    ) -> (Vec<Line<'static>>, Vec<SelectableRegionRange>) {
        if body_width == 0 {
            return (Vec::new(), Vec::new());
        }

        let tick = self.next_message_render_cache_tick();
        let key = MessageRenderCacheKey {
            session_id: self.conversation.session_id,
            message_id: message.id, // Binds the cache to the Assistant message hosting this tool call
            width: body_width,
            is_round_end: !is_streaming, // Approximation, cache differs when streaming is done
            kind: MessageRenderCacheKind::ToolCall(tool_call.id.clone()),
        };

        {
            let mut cache = self.message_render_cache.borrow_mut();
            if let Some(entry) = cache.get_mut(&key) {
                entry.last_used_tick = tick;
                self.record_message_render_cache_hit();
                match &entry.value {
                    MessageRenderCacheValue::ToolResult(lines, regions) => {
                        return (lines.clone(), regions.clone());
                    }
                    MessageRenderCacheValue::Cards(..) => {}
                }
            }
        }

        self.record_message_render_cache_miss();
        let result =
            render_tool_call_with_result(tool_call, tool_result, body_width, is_streaming, ctx);

        {
            let mut cache = self.message_render_cache.borrow_mut();
            cache.insert(
                key,
                MessageRenderCacheEntry {
                    value: MessageRenderCacheValue::ToolResult(result.0.clone(), result.1.clone()),
                    last_used_tick: tick,
                },
            );
        }

        result
    }

    fn cached_render_message_cards(
        &self,
        message: &Message,
        body_width: usize,
        is_round_end: bool,
    ) -> Vec<(Color, Vec<Line<'static>>)> {
        let key = MessageRenderCacheKey {
            session_id: self.conversation.session_id,
            message_id: message.id,
            width: body_width,
            is_round_end,
            kind: MessageRenderCacheKind::Cards,
        };
        let tick = self.next_message_render_cache_tick();

        {
            let mut cache = self.message_render_cache.borrow_mut();
            if let Some(entry) = cache.get_mut(&key) {
                entry.last_used_tick = tick;
                self.record_message_render_cache_hit();
                match &entry.value {
                    MessageRenderCacheValue::Cards(cards) => return cards.clone(),
                    MessageRenderCacheValue::ToolResult(..) => {} // Should never happen with .Cards kind
                }
            }
        }

        self.record_message_render_cache_miss();
        let cards = self.render_message_cards(message, body_width, is_round_end);

        {
            let mut cache = self.message_render_cache.borrow_mut();
            cache.insert(
                key,
                MessageRenderCacheEntry {
                    value: MessageRenderCacheValue::Cards(cards.clone()),
                    last_used_tick: tick,
                },
            );
        }

        self.prune_message_render_cache_if_needed();
        cards
    }

    fn load_expanded_tool_outputs(&self, messages: &[Message]) -> HashMap<Uuid, String> {
        let mut outputs = HashMap::new();

        for message in messages {
            if !self.expanded_tool_results.contains(&message.id) {
                continue;
            }

            if let Ok(Some(output)) = self
                .store
                .load_tool_event_output(self.conversation.session_id, message.id)
            {
                outputs.insert(message.id, output);
            }
        }

        outputs
    }

    fn render_message_cards(
        &self,
        message: &Message,
        body_width: usize,
        is_round_end: bool,
    ) -> Vec<(Color, Vec<Line<'static>>)> {
        let palette = self.palette();

        match message.role {
            MessageRole::User => vec![(palette.panel_alt, {
                let mut content_lines = self.render_text_body_lines(
                    &message.content,
                    body_width.saturating_sub(2),
                    Some(self.workspace_root.as_path()),
                );

                for attachment in &message.attachments {
                    content_lines.push(line_with_style(&attachment.summary(), palette.accent_soft));
                }

                let mut lines = Vec::new();
                lines.push(Line::from(""));
                for line in content_lines {
                    let mut spans = vec![Span::styled(
                        "",
                        Style::default()
                            .fg(palette.accent)
                            .add_modifier(Modifier::BOLD),
                    )];
                    spans.extend(line.spans);
                    lines.push(Line::from(spans));
                }
                lines.push(Line::from(""));

                lines
            })],
            MessageRole::Assistant => {
                let mut cards = Vec::new();

                let body_lines =
                    self.render_assistant_body_lines(message, body_width, is_round_end);
                if !body_lines.is_empty() {
                    let mut lines_with_margin = Vec::new();
                    lines_with_margin.push(Line::from(""));
                    lines_with_margin.extend(body_lines);
                    lines_with_margin.push(Line::from(""));
                    cards.push((palette.background, lines_with_margin));
                }

                for _tool_call in &message.tool_calls {
                    // Tool calls are rendered in messages_text along with their results
                }

                cards
            }
            MessageRole::Tool => {
                // Return nothing here; handled by the loop in messages_text
                Vec::new()
            }
            MessageRole::System => {
                if message.content.starts_with(COMPACTION_MESSAGE_LABEL) {
                    let summary = message
                        .content
                        .split_once("\n\n")
                        .map(|(_, summary)| summary)
                        .unwrap_or("");

                    let mut lines = Vec::new();
                    lines.push(Line::from(""));
                    lines.push(render_compaction_divider_line(
                        COMPACTION_MESSAGE_LABEL,
                        body_width,
                        palette,
                    ));
                    lines.push(Line::from(""));
                    lines.extend(self.render_text_body_lines(
                        summary,
                        body_width,
                        Some(self.workspace_root.as_path()),
                    ));
                    lines.push(Line::from(""));
                    return vec![(palette.background, lines)];
                }

                if message.content.starts_with("Loaded instructions from")
                    || message.content.starts_with("Loaded ")
                        && message.content.contains(" instruction files:")
                {
                    let lines = vec![Line::from(vec![
                        Span::styled("󱁤 ", Style::default().fg(palette.accent_soft)),
                        Span::styled(
                            message.content.clone(),
                            Style::default()
                                .fg(palette.text)
                                .add_modifier(Modifier::ITALIC),
                        ),
                    ])];
                    return vec![(palette.background, lines)];
                }

                let content_lines = self.render_text_body_lines(
                    &message.content,
                    body_width,
                    Some(self.workspace_root.as_path()),
                );
                let mut lines = Vec::new();
                lines.push(Line::from(""));
                lines.extend(content_lines);
                lines.push(Line::from(""));
                vec![(palette.background, lines)]
            }
            MessageRole::Error => {
                let error_lines = self.render_error_body_lines(message, body_width);
                let mut lines = Vec::new();
                lines.push(Line::from(""));
                lines.extend(error_lines);
                lines.push(Line::from(""));
                vec![(palette.panel_light, lines)]
            }
        }
    }

    fn render_assistant_body_lines(
        &self,
        message: &Message,
        body_width: usize,
        is_round_end: bool,
    ) -> Vec<Line<'static>> {
        let mut lines = Vec::new();

        if !message.reasoning.trim().is_empty() {
            lines.extend(self.render_reasoning_lines(&message.reasoning, body_width));
            if !message.content.trim().is_empty() {
                lines.push(Line::from(""));
            }
        }

        if !message.content.is_empty() {
            if let Some((diff_lines, _)) =
                render_unified_diff_text(&message.content, body_width, self.palette())
            {
                lines.extend(diff_lines);
            } else {
                let rendered = render_markdown_text_with_width_and_cwd(
                    &message.content,
                    Some(body_width),
                    Some(self.workspace_root.as_path()),
                );
                lines.extend(rendered.lines);
            }
        }

        if lines.is_empty()
            && !message.streaming
            && message.reasoning.trim().is_empty()
            && message.tool_calls.is_empty()
        {
            lines.push(line_with_style("(empty)", self.palette().muted));
        }

        // Add model name, duration, end time, and mode at the end (only for round end)
        if is_round_end && !message.streaming {
            let model_display_name = message
                .model_id
                .as_ref()
                .and_then(|model_id| {
                    self.config
                        .resolve_model_by_ids(&self.auth, &self.conversation.provider_id, model_id)
                        .ok()
                        .map(|model| model.display_name)
                })
                .unwrap_or_else(|| self.conversation.model_display_name.clone());

            let duration = message.completed_at.map(|completed| {
                let elapsed = completed - message.created_at;
                let secs = elapsed.as_seconds_f64();
                format!("{:.1}s", secs)
            });

            let end_time = message.completed_at.map(|completed| {
                completed
                    .with_timezone(&Local)
                    .format("%H:%M:%S")
                    .to_string()
            });

            let tps = message
                .tokens_per_second
                .map(|val| format!("{:.1} t/s", val));

            let mode_label = message
                .mode
                .or_else(|| {
                    // Fallback to the preceding user message mode if not set on assistant message
                    self.conversation
                        .messages
                        .iter()
                        .take_while(|m| m.id != message.id)
                        .filter(|m| m.role == MessageRole::User)
                        .last()
                        .and_then(|m| m.mode)
                })
                .unwrap_or(self.mode);

            let parts: Vec<String> = [
                Some(model_display_name),
                duration,
                tps,
                end_time,
                Some(mode_label.title().to_string()),
            ]
            .into_iter()
            .flatten()
            .collect();

            let suffix = parts.join(" · ");
            lines.push(line_with_style_right_aligned(
                &suffix,
                body_width,
                self.palette().accent_soft,
            ));
        }

        lines
    }

    fn render_text_body_lines(
        &self,
        text: &str,
        body_width: usize,
        cwd: Option<&std::path::Path>,
    ) -> Vec<Line<'static>> {
        let mut lines = Vec::new();
        if text.trim().is_empty() {
            lines.push(line_with_style("(empty)", self.palette().muted));
        } else {
            let rendered = render_markdown_text_with_width_and_cwd(text, Some(body_width), cwd);
            lines.extend(rendered.lines);
        }
        lines
    }

    fn render_error_body_lines(&self, message: &Message, body_width: usize) -> Vec<Line<'static>> {
        let palette = self.palette();
        let mut lines = Vec::new();

        // Render reasoning first if present (preserves thinking at interruption point)
        if !message.reasoning.trim().is_empty() {
            lines.extend(self.render_reasoning_lines(&message.reasoning, body_width));
            lines.push(Line::from(""));
        }

        let error_text = if message.content.trim().is_empty() {
            "Request cancelled.".to_string()
        } else {
            message.content.clone()
        };

        for line in error_text.lines() {
            lines.push(line_with_prefix(
                "!",
                &shorten_single_line(line, body_width.saturating_sub(2)),
                Style::default().fg(palette.error),
                Style::default().fg(palette.error),
            ));
        }

        if lines.is_empty() {
            lines.push(line_with_style("! Request cancelled.", palette.error));
        }

        lines
    }

    fn render_reasoning_lines(&self, reasoning: &str, body_width: usize) -> Vec<Line<'static>> {
        render_reasoning_markdown_lines(
            reasoning,
            body_width,
            Some(self.workspace_root.as_path()),
            self.palette(),
        )
    }

    fn render_running_subagent_lines(
        &self,
        execution: &RunningSubagentExecution,
        body_width: usize,
    ) -> Vec<Line<'static>> {
        let palette = self.palette();

        // Title line: description (@subagent_type)
        let description = shorten(&execution.task_description, body_width.saturating_sub(30));
        let subagent_type = execution.subagent_type.clone();

        let mut lines = Vec::new();

        // Header line with description and subagent type
        lines.push(Line::from(vec![
            Span::styled(
                description,
                Style::default()
                    .fg(palette.text)
                    .add_modifier(Modifier::BOLD),
            ),
            Span::styled(
                format!(" (@{})", subagent_type),
                Style::default().fg(palette.muted),
            ),
        ]));

        // Status line - always shown to maintain consistent height
        let status_text = execution.status.display();
        let status_line = match &execution.status {
            SubagentStatus::Tool => {
                if let Some(tool_call) = &execution.current_tool_call {
                    let tool_summary = if tool_call_arguments_are_complete(&tool_call.arguments) {
                        summarize_tool_call(
                            &tool_call.name,
                            &tool_call.arguments,
                            body_width.saturating_sub(10),
                            self.workspace_root.as_path(),
                        )
                    } else {
                        let canonical_display = canonical_tool_name(&tool_call.name)
                            .map(|s| s.to_string())
                            .unwrap_or_else(|| tool_call.name.clone());
                        format!("{} ...", canonical_display)
                    };
                    format!("{}: {}", status_text, tool_summary)
                } else {
                    status_text.to_string()
                }
            }
            _ => status_text.to_string(),
        };

        lines.push(Line::from(vec![
            Span::styled("  ".to_string(), Style::default()),
            Span::styled(status_line, Style::default().fg(palette.accent_soft)),
        ]));

        // Navigation hint
        lines.push(Line::from(vec![
            Span::styled("  ".to_string(), Style::default()),
            Span::styled(
                "Ctrl+X then ".to_string(),
                Style::default().fg(palette.muted),
            ),
            Span::styled("Up".to_string(), Style::default().fg(palette.accent_soft)),
            Span::styled("/".to_string(), Style::default().fg(palette.muted)),
            Span::styled("Down".to_string(), Style::default().fg(palette.accent_soft)),
            Span::styled(
                " to navigate".to_string(),
                Style::default().fg(palette.muted),
            ),
        ]));

        lines
    }

    #[allow(dead_code)]
    fn render_attachment_preview_lines(
        &self,
        attachments: &[String],
        body_width: usize,
    ) -> Vec<Line<'static>> {
        let palette = self.palette();
        let mut lines = Vec::new();

        for attachment in attachments {
            lines.push(line_with_prefix(
                "",
                &shorten_single_line(attachment, body_width.saturating_sub(2)),
                Style::default().fg(palette.accent_soft),
                Style::default().fg(palette.text),
            ));
        }

        lines
    }

    #[allow(dead_code)]
    fn render_output_preview_lines(
        &self,
        output: &str,
        body_width: usize,
        is_error: bool,
        message_id: Option<Uuid>,
    ) -> Vec<Line<'static>> {
        let palette = self.palette();
        let mut lines = Vec::new();

        let is_expanded = message_id.is_some_and(|id| self.expanded_tool_results.contains(&id));
        let max_lines = if is_expanded {
            TOOL_OUTPUT_EXPANDED_MAX_LINES
        } else if is_error {
            4
        } else {
            TOOL_OUTPUT_PREVIEW_LINES
        };

        let prefix = if is_error { "!" } else { "" };
        let fg = if is_error {
            palette.error
        } else {
            palette.text
        };
        let prefix_style = Style::default().fg(if is_error {
            palette.error
        } else {
            palette.accent_soft
        });

        let total_output_lines = output.lines().count();
        let wrap_width = body_width.saturating_sub(2);

        for line in output.lines().take(max_lines) {
            if is_expanded {
                let owned_line = Line::from(line.to_string());
                let wrapped =
                    word_wrap_line(&owned_line, WrapOptions::new(wrap_width).break_words(true));
                for (wrap_idx, wrapped_line) in wrapped.iter().enumerate() {
                    let effective_prefix = if wrap_idx == 0 { prefix } else { " " };
                    let mut spans =
                        vec![Span::styled(format!("{} ", effective_prefix), prefix_style)];
                    spans.extend(wrapped_line.spans.iter().map(|span| {
                        Span::styled(span.content.to_string(), Style::default().fg(fg))
                    }));
                    lines.push(Line::from(spans));
                }
            } else {
                lines.push(line_with_prefix(
                    prefix,
                    &shorten_single_line(line, wrap_width),
                    prefix_style,
                    Style::default().fg(fg),
                ));
            }
        }

        if total_output_lines > max_lines {
            lines.push(line_with_prefix(
                prefix,
                &format!("... {} more line(s)", total_output_lines - max_lines),
                Style::default().fg(palette.muted),
                Style::default().fg(palette.muted),
            ));
        } else if total_output_lines > TOOL_OUTPUT_PREVIEW_LINES && message_id.is_some() {
            let hint = if is_expanded {
                "▲ Click to collapse"
            } else {
                "▼ Click to expand"
            };
            lines.push(line_with_prefix(
                prefix,
                hint,
                Style::default().fg(palette.muted),
                Style::default().fg(palette.muted),
            ));
        }

        if lines.is_empty() {
            lines.push(line_with_style("(no output)", palette.muted));
        }

        lines
    }

    /// Updates the message layout index to enable viewport virtualization.
    ///
    /// The layout index maintains a mapping from messages to their positions in
    /// the rendered output. This enables O(log n) binary search to find visible
    /// messages without rendering everything.
    ///
    /// The index is rebuilt when:
    /// - Width changes (line counts become invalid)
    /// - Messages are added/removed
    /// - Cache is cleared
    /// - Force rebuild is requested (for streaming messages)
    ///
    /// For incremental updates, only the tail (last few messages) is rebuilt,
    /// preserving existing block positions for unchanged messages.
    fn update_message_layout_index(&self, width: usize, body_width: usize, force_rebuild: bool) {
        let messages = self.conversation.visible_messages();
        let mut index = self.message_layout_index.borrow_mut();

        // Check if message count changed (new messages added or removed)
        let indexed_message_count = index
            .blocks
            .last()
            .map(|b| b.message_start_idx + b.message_count)
            .unwrap_or(0);
        let message_count_changed = indexed_message_count != messages.len();
        let streaming_mode_changed = index.contains_streaming_messages != force_rebuild;

        // Check if we need a full rebuild
        let needs_full_rebuild = force_rebuild
            || streaming_mode_changed
            || !index.valid
            || index.width != width
            || message_count_changed
            || index.blocks.is_empty() && !messages.is_empty();

        if needs_full_rebuild {
            index.blocks.clear();
            index.total_lines = 0;
            index.width = width;
            index.valid = true;
            index.contains_streaming_messages = force_rebuild;

            let expanded_tool_outputs = self.load_expanded_tool_outputs(messages);
            let spinner = self.loading_spinner();
            let ctx = RenderContext {
                palette: self.palette(),
                spinner,
                workspace_root: self.workspace_root.as_path(),
                expanded_tool_results: &self.expanded_tool_results,
                expanded_tool_outputs: &expanded_tool_outputs,
            };

            let mut current_line = 0;
            let mut i = 0;

            while i < messages.len() {
                // Build block without start_line (calculated below)
                // Determine is_round_end before generation
                let count = if matches!(messages[i].role, MessageRole::Assistant) {
                    let mut c = 1;
                    while i + c < messages.len()
                        && matches!(messages[i + c].role, MessageRole::Tool)
                    {
                        c += 1;
                    }
                    c
                } else {
                    1
                };
                let next_idx = i + count;
                let is_round_end = next_idx >= messages.len()
                    || matches!(messages[next_idx].role, MessageRole::User);

                let (message_id, message_count, line_count) = self.build_message_block_data(
                    messages,
                    i,
                    width,
                    body_width,
                    &ctx,
                    is_round_end,
                );

                let block = super::MessageBlock {
                    message_id,
                    message_start_idx: i,
                    message_count,
                    start_line: current_line,
                    line_count,
                };

                current_line += line_count;
                i += message_count;
                index.blocks.push(block);
            }

            index.total_lines = current_line;
        }
    }

    fn resolve_message_scroll_target(
        &self,
        messages: &[Message],
        width: usize,
        body_width: usize,
    ) -> Option<usize> {
        let message_id = self.message_scroll_target?;

        // Create a minimal context for block data calculation
        let expanded_tool_outputs = self.load_expanded_tool_outputs(messages);
        let spinner = self.loading_spinner();
        let ctx = RenderContext {
            palette: self.palette(),
            spinner,
            workspace_root: self.workspace_root.as_path(),
            expanded_tool_results: &self.expanded_tool_results,
            expanded_tool_outputs: &expanded_tool_outputs,
        };

        let mut offset = 0;
        let mut i = 0;

        while i < messages.len() {
            if messages[i].id == message_id {
                return Some(offset);
            }

            let count = if matches!(messages[i].role, MessageRole::Assistant) {
                let mut c = 1;
                while i + c < messages.len() && matches!(messages[i + c].role, MessageRole::Tool) {
                    c += 1;
                }
                c
            } else {
                1
            };
            let next_idx = i + count;
            let is_round_end =
                next_idx >= messages.len() || matches!(messages[next_idx].role, MessageRole::User);

            let (_message_id, message_count, line_count) =
                self.build_message_block_data(messages, i, width, body_width, &ctx, is_round_end);
            offset += line_count;
            i += message_count;
        }

        None
    }

    /// Builds data for a single message block (without start_line).
    ///
    /// Returns (message_id, message_count, line_count).
    fn build_message_block_data(
        &self,
        messages: &[Message],
        start_idx: usize,
        width: usize,
        body_width: usize,
        ctx: &RenderContext<'_>,
        is_round_end: bool,
    ) -> (Uuid, usize, usize) {
        let message = &messages[start_idx];
        let message_id = message.id;
        let palette = self.palette();

        let (message_count, line_count) = match message.role {
            MessageRole::Assistant => {
                // Count tool result messages that follow
                let mut count = 1;
                while start_idx + count < messages.len()
                    && matches!(messages[start_idx + count].role, MessageRole::Tool)
                {
                    count += 1;
                }

                // Calculate lines for assistant message
                let cards = self.cached_render_message_cards(message, body_width, is_round_end);
                let mut lines = 0;
                for (_, card_lines) in &cards {
                    lines +=
                        decorate_card_lines(card_lines.clone(), width, palette.background).len();
                }

                // Calculate lines for tool calls with results
                let tool_results_by_id: std::collections::HashMap<String, &Message> = {
                    let mut map = std::collections::HashMap::new();
                    let mut j = start_idx + 1;
                    while j < messages.len() && matches!(messages[j].role, MessageRole::Tool) {
                        if let Some(id) = &messages[j].tool_call_id {
                            map.insert(id.clone(), &messages[j]);
                        }
                        j += 1;
                    }
                    map
                };

                if !message.tool_calls.is_empty() {
                    for tool_call in &message.tool_calls {
                        let tool_result = tool_results_by_id.get(&tool_call.id).copied();
                        let (card_lines, _) = self.cached_render_tool_call_with_result(
                            message,
                            tool_call,
                            tool_result,
                            body_width,
                            message.streaming,
                            ctx,
                        );
                        if !card_lines.is_empty() {
                            lines +=
                                decorate_card_lines(card_lines, width, palette.panel_light).len();
                        }
                    }
                    lines += 1; // Empty line after tool calls
                }

                (count, lines)
            }
            MessageRole::User => {
                let cards = self.cached_render_message_cards(message, body_width, is_round_end);
                let mut lines = 0;
                for (_, card_lines) in &cards {
                    lines +=
                        decorate_card_lines(card_lines.clone(), width, palette.panel_alt).len();
                }
                lines += 1; // Empty line after user message
                (1, lines)
            }
            MessageRole::System => {
                let cards = self.cached_render_message_cards(message, body_width, is_round_end);
                let mut lines = 0;
                for (_, card_lines) in &cards {
                    lines +=
                        decorate_card_lines(card_lines.clone(), width, palette.background).len();
                }
                (1, lines)
            }
            MessageRole::Error => {
                let cards = self.cached_render_message_cards(message, body_width, is_round_end);
                let mut lines = 0;
                for (_, card_lines) in &cards {
                    lines +=
                        decorate_card_lines(card_lines.clone(), width, palette.panel_light).len();
                }
                (1, lines)
            }
            MessageRole::Tool => {
                // Tool messages are included in Assistant blocks, skip
                (1, 0)
            }
        };

        (message_id, message_count, line_count)
    }

    /// Finds message blocks that intersect with the visible viewport.
    ///
    /// Uses binary search for O(log n) complexity. Returns blocks with a
    /// buffer zone to ensure smooth scrolling.
    fn find_visible_message_blocks(
        &self,
        scroll: usize,
        viewport_height: usize,
    ) -> Vec<super::MessageBlock> {
        let index = self.message_layout_index.borrow();

        if index.blocks.is_empty() {
            return Vec::new();
        }

        let viewport_height = viewport_height.max(1);
        let max_scroll = index.total_lines.saturating_sub(viewport_height);
        let clamped_scroll = scroll.min(max_scroll);

        let visible_start = clamped_scroll.saturating_sub(5); // Buffer above
        let visible_end = clamped_scroll
            .saturating_add(viewport_height)
            .saturating_add(5); // Buffer below

        // Binary search for first block that could be visible
        let first_visible = index
            .blocks
            .partition_point(|block| block.start_line + block.line_count <= visible_start);

        // Collect all visible blocks
        let mut visible_blocks = Vec::new();
        for block in index.blocks.iter().skip(first_visible) {
            if block.start_line >= visible_end {
                break;
            }
            visible_blocks.push(block.clone());
        }

        visible_blocks
    }

    /// Renders a single message block to lines.
    ///
    /// This is the actual rendering logic, extracted for reuse in virtualization.
    #[allow(clippy::too_many_arguments)]
    fn render_message_block_to_lines(
        &self,
        messages: &[Message],
        block: &super::MessageBlock,
        width: usize,
        body_width: usize,
        card_ranges: &mut Vec<ToolResultCardRange>,
        selectable_regions_ranges: &mut Vec<SelectableRegionRange>,
        current_line_offset: usize,
        ctx: &RenderContext<'_>,
        is_round_end: bool,
    ) -> Vec<Line<'static>> {
        let palette = self.palette();
        let mut lines = Vec::new();

        // Skip Tool messages - they're rendered as part of Assistant blocks
        if block.message_count == 0 {
            return lines;
        }

        let start_idx = block.message_start_idx;
        let message = &messages[start_idx];

        match message.role {
            MessageRole::Assistant => {
                // Render assistant message cards
                let assistant_cards =
                    self.cached_render_message_cards(message, body_width, is_round_end);
                for (card_bg, card_lines) in assistant_cards {
                    if !card_lines.is_empty() {
                        let start_line = current_line_offset + lines.len();

                        let mut block_start = start_line;
                        let mut current_min_x = 1;
                        for (i, line) in card_lines.iter().enumerate() {
                            let is_reasoning =
                                line.spans.first().is_some_and(|s| s.content == "");
                            let line_min_x = if is_reasoning { 3 } else { 1 };

                            if line_min_x != current_min_x {
                                if i > 0 {
                                    selectable_regions_ranges.push(SelectableRegionRange {
                                        start_line: block_start,
                                        end_line: start_line + i,
                                        min_x: current_min_x,
                                        max_x: None,
                                    });
                                }
                                block_start = start_line + i;
                                current_min_x = line_min_x;
                            }
                        }
                        if block_start < start_line + card_lines.len() {
                            selectable_regions_ranges.push(SelectableRegionRange {
                                start_line: block_start,
                                end_line: start_line + card_lines.len(),
                                min_x: current_min_x,
                                max_x: None,
                            });
                        }

                        lines.extend(decorate_card_lines(card_lines, width, card_bg));
                    }
                }

                // Collect tool results
                let tool_results_by_id: std::collections::HashMap<String, &Message> = {
                    let mut map = std::collections::HashMap::new();
                    let mut j = start_idx + 1;
                    while j < messages.len() && j < start_idx + block.message_count {
                        if matches!(messages[j].role, MessageRole::Tool)
                            && let Some(id) = &messages[j].tool_call_id
                        {
                            map.insert(id.clone(), &messages[j]);
                        }
                        j += 1;
                    }
                    map
                };

                // Render tool calls with results
                if !message.tool_calls.is_empty() {
                    for tool_call in &message.tool_calls {
                        let tool_result = tool_results_by_id.get(&tool_call.id).copied();
                        let (tool_card_lines, mut regions) = self
                            .cached_render_tool_call_with_result(
                                message,
                                tool_call,
                                tool_result,
                                body_width,
                                message.streaming,
                                ctx,
                            );
                        if !tool_card_lines.is_empty() {
                            let start_line = current_line_offset + lines.len();

                            // Adjust regions mapping
                            for r in &mut regions {
                                r.start_line += start_line;
                                r.end_line += start_line;
                                r.min_x += 1; // decorate_card_lines left padding
                                if let Some(max_x) = &mut r.max_x {
                                    *max_x += 1;
                                }
                                selectable_regions_ranges.push(r.clone());
                            }

                            // Calculate fallback region for bash or non-diff output
                            if regions.is_empty() {
                                selectable_regions_ranges.push(SelectableRegionRange {
                                    start_line,
                                    end_line: start_line + tool_card_lines.len(),
                                    min_x: 1,
                                    max_x: None,
                                });
                            }

                            let card_bg = if canonical_tool_name(&tool_call.name)
                                == Some("task")
                            {
                                palette.panel
                            } else {
                                palette.panel_light
                            };
                            let decorated =
                                decorate_card_lines(tool_card_lines, width, card_bg);
                            if let Some(result_msg) = tool_result {
                                lines.extend(decorated);
                                let end_line = current_line_offset + lines.len();
                                card_ranges.push(ToolResultCardRange {
                                    message_id: result_msg.id,
                                    start_line,
                                    end_line,
                                });
                            } else {
                                lines.extend(decorated);
                            }
                        }
                    }
                    lines.push(Line::from(""));
                }
            }
            MessageRole::User | MessageRole::System | MessageRole::Error => {
                let cards = self.cached_render_message_cards(message, body_width, is_round_end);
                let bg = match message.role {
                    MessageRole::User => palette.panel_alt,
                    MessageRole::Error => palette.panel_light,
                    _ => palette.background,
                };
                for (_, card_lines) in cards {
                    if !card_lines.is_empty() {
                        let start_line = current_line_offset + lines.len();

                        let mut block_start = start_line;
                        let mut current_min_x = 1;
                        for (i, line) in card_lines.iter().enumerate() {
                            let is_reasoning =
                                line.spans.first().is_some_and(|s| s.content == "");
                            let line_min_x = if is_reasoning { 3 } else { 1 };

                            if line_min_x != current_min_x {
                                if i > 0 {
                                    selectable_regions_ranges.push(SelectableRegionRange {
                                        start_line: block_start,
                                        end_line: start_line + i,
                                        min_x: current_min_x,
                                        max_x: None,
                                    });
                                }
                                block_start = start_line + i;
                                current_min_x = line_min_x;
                            }
                        }
                        if block_start < start_line + card_lines.len() {
                            selectable_regions_ranges.push(SelectableRegionRange {
                                start_line: block_start,
                                end_line: start_line + card_lines.len(),
                                min_x: current_min_x,
                                max_x: None,
                            });
                        }

                        lines.extend(decorate_card_lines(card_lines, width, bg));
                    }
                }
                if matches!(message.role, MessageRole::User) {
                    lines.push(Line::from(""));
                }
            }
            MessageRole::Tool => {
                // Tool messages are handled within Assistant blocks
            }
        }

        lines
    }

    fn render_scrollbar(
        &self,
        frame: &mut Frame<'_>,
        area: Rect,
        scroll: usize,
        max_scroll: usize,
    ) {
        let palette = self.palette();
        if area.width == 0 || area.height == 0 {
            return;
        }

        let track_style = Style::default().bg(palette.background).fg(palette.border);
        let thumb_style = Style::default().bg(palette.background).fg(palette.accent);
        let height = area.height as usize;
        let mut lines = Vec::with_capacity(height);

        if max_scroll == 0 || height == 0 {
            for _ in 0..height {
                lines.push(Line::from(vec![Span::styled(" ", track_style)]));
            }
        } else {
            let thumb_height = ((height * height) / self.message_total_lines.max(1))
                .clamp(1, height)
                .max(1);
            let track_span = height.saturating_sub(thumb_height);
            let thumb_top = if track_span == 0 {
                0
            } else {
                ((scroll as f32 / max_scroll as f32) * track_span as f32).round() as usize
            };

            for row in 0..height {
                let is_thumb = row >= thumb_top && row < thumb_top + thumb_height;
                let style = if is_thumb { thumb_style } else { track_style };
                let glyph = if is_thumb { "" } else { "" };
                lines.push(Line::from(vec![Span::styled(glyph, style)]));
            }
        }

        let paragraph =
            Paragraph::new(Text::from(lines)).style(Style::default().bg(palette.background));
        frame.render_widget(paragraph, area);
    }
}

fn render_reasoning_markdown_lines(
    reasoning: &str,
    body_width: usize,
    cwd: Option<&std::path::Path>,
    palette: ThemePalette,
) -> Vec<Line<'static>> {
    let mut lines = Vec::new();
    // Use 0.5 ratio for a balanced dimmed appearance that works consistently across terminals
    // This avoids the inconsistent behavior of Modifier::DIM which varies significantly
    // between Windows Terminal (strong dimming) and Ghostty (weak/no dimming)
    let dimmed_color = crate::theme::mix_colors(palette.muted, palette.background, 0.5);
    let label_style = Style::default().fg(dimmed_color);
    let label_italic_style = Style::default()
        .fg(dimmed_color)
        .add_modifier(Modifier::ITALIC);
    let body_style = Style::default().fg(dimmed_color);

    lines.push(Line::from(vec![
        Span::styled("", label_style),
        Span::styled("Thinking:", label_italic_style),
    ]));

    if reasoning.trim().is_empty() {
        return lines;
    }

    let content_width = body_width.saturating_sub(2).max(1);
    let rendered = render_markdown_text_with_width_and_cwd(reasoning, Some(content_width), cwd);

    if rendered.lines.is_empty() {
        return lines;
    }

    let mut rendered_lines = rendered.lines.into_iter();

    // Skip leading blank lines to fix extra top spacing
    let mut first_line = rendered_lines.next();
    while let Some(line) = first_line {
        if line
            .spans
            .iter()
            .all(|s| s.content.trim().is_empty() && s.style == Style::default())
        {
            first_line = rendered_lines.next();
        } else {
            first_line = Some(line);
            break;
        }
    }

    if let Some(line) = first_line {
        let mut spans = Vec::with_capacity(line.spans.len().saturating_add(1));
        spans.push(Span::styled("", label_style));
        spans.extend(line.spans.into_iter().map(|mut span| {
            // Mix the foreground color with background for all spans (including highlighted ones)
            // Use 0.4 ratio for a slightly more visible dimmed text
            // Note: We intentionally do NOT use Modifier::DIM here because its behavior
            // varies significantly between terminals (Windows Terminal dims heavily,
            // Ghostty barely dims at all), causing inconsistent appearance.
            if let Some(fg) = span.style.fg {
                span.style = span
                    .style
                    .fg(crate::theme::mix_colors(fg, palette.background, 0.4));
            } else {
                span.style = span.style.patch(body_style);
            }
            span
        }));
        lines.push(Line::from(spans));
    }

    for line in rendered_lines {
        let mut spans = Vec::with_capacity(line.spans.len().saturating_add(1));
        spans.push(Span::styled("", label_style));
        spans.extend(line.spans.into_iter().map(|mut span| {
            // Mix the foreground color with background for all spans (including highlighted ones)
            // Use 0.4 ratio for a slightly more visible dimmed text
            if let Some(fg) = span.style.fg {
                span.style = span
                    .style
                    .fg(crate::theme::mix_colors(fg, palette.background, 0.4));
            } else {
                span.style = span.style.patch(body_style);
            }
            span
        }));
        lines.push(Line::from(spans));
    }

    lines
}

#[cfg(test)]
mod tests {
    use super::{
        RenderContext, render_reasoning_markdown_lines, render_tool_call_with_result,
        render_tool_result_detail_lines,
    };
    use crate::session::{Message, MessageRole};
    use crate::theme::ThemePalette;
    use ratatui::style::Style;
    use ratatui::text::Line;
    use std::collections::{HashMap, HashSet};

    fn line_text(line: &Line<'static>) -> String {
        line.spans
            .iter()
            .map(|span| span.content.as_ref())
            .collect::<String>()
    }

    fn text_lines_to_string(lines: &[Line<'static>]) -> String {
        lines.iter().map(line_text).collect::<Vec<_>>().join("\n")
    }

    fn test_app() -> super::App {
        let temp_root =
            std::env::temp_dir().join(format!("tidev-render-tests-{}", uuid::Uuid::new_v4()));
        let paths = crate::config::ConfigPaths {
            config_dir: temp_root.join(".config").join("tidev"),
            data_dir: temp_root.join(".local").join("share").join("tidev"),
            config_file: temp_root.join(".config").join("tidev").join("config.toml"),
            auth_file: temp_root
                .join(".local")
                .join("share")
                .join("tidev")
                .join("auth.json"),
            database_file: temp_root
                .join(".local")
                .join("share")
                .join("tidev")
                .join("sessions.sqlite3"),
        };

        super::App::new_with_paths(paths).unwrap()
    }

    #[test]
    fn reasoning_lines_render_markdown_code_blocks() {
        let lines = render_reasoning_markdown_lines(
            "```rust\nfn main() { println!(\"hi\"); }\n```\n",
            80,
            None,
            ThemePalette::dark(),
        );

        assert_eq!(line_text(&lines[0]), "┃ Thinking:");
        assert_eq!(line_text(&lines[1]), "┃ fn main() { println!(\"hi\"); }");
        assert!(
            lines[1].spans.len() > 2,
            "expected highlighted spans in code line"
        );
        assert!(
            lines[1]
                .spans
                .iter()
                .skip(1)
                .any(|span| span.style != Style::default()),
            "expected syntax highlighting styles on code spans"
        );
    }

    #[test]
    fn reasoning_lines_preserve_empty_state() {
        let lines = render_reasoning_markdown_lines("", 80, None, ThemePalette::dark());

        assert_eq!(lines.len(), 1);
        assert_eq!(line_text(&lines[0]), "┃ Thinking:");
    }

    #[test]
    fn render_tool_result_detail_lines_list_shows_output_preview() {
        use crate::session::{Message, ToolExecutionResult};

        let message = Message::tool_result(
            "tool-call-id",
            "list",
            ToolExecutionResult::new("./\nfile1.txt\nfile2.txt"),
        );

        let ctx = RenderContext {
            palette: ThemePalette::dark(),
            spinner: "|",
            workspace_root: std::path::Path::new("/tmp"),
            expanded_tool_results: &HashSet::new(),
            expanded_tool_outputs: &HashMap::new(),
        };

        let (lines, _, _) = render_tool_result_detail_lines(&message, 80, &ctx);
        let text = text_lines_to_string(&lines);
        assert!(
            text.contains("file1.txt"),
            "should contain file listing: {}",
            text
        );
    }

    #[test]
    fn render_tool_result_detail_lines_todowrite_formats_checkbox_list() {
        use crate::session::{Message, ToolExecutionResult};
        use crate::tooling::TodoItem;

        let todos = vec![
            TodoItem {
                content: "Task 1".to_string(),
                status: "completed".to_string(),
                priority: "high".to_string(),
            },
            TodoItem {
                content: "Task 2".to_string(),
                status: "in_progress".to_string(),
                priority: "medium".to_string(),
            },
            TodoItem {
                content: "Task 3".to_string(),
                status: "pending".to_string(),
                priority: "low".to_string(),
            },
        ];
        let output = serde_json::to_string_pretty(&todos).unwrap();
        let message = Message::tool_result(
            "tool-call-id",
            "todowrite",
            ToolExecutionResult::new(output),
        );

        let ctx = RenderContext {
            palette: ThemePalette::dark(),
            spinner: "|",
            workspace_root: std::path::Path::new("/tmp"),
            expanded_tool_results: &HashSet::new(),
            expanded_tool_outputs: &HashMap::new(),
        };

        let (lines, _, _) = render_tool_result_detail_lines(&message, 80, &ctx);

        let text = text_lines_to_string(&lines);
        assert!(
            text.contains("Updated todo list"),
            "should contain header: {}",
            text
        );
        assert!(text.contains("Task 1"), "should contain Task 1: {}", text);
        assert!(text.contains("Task 2"), "should contain Task 2: {}", text);
        assert!(text.contains("Task 3"), "should contain Task 3: {}", text);
    }

    #[test]
    fn streaming_tool_call_switches_to_summary_after_arguments_parse() {
        use crate::session::ToolCall;

        let tool_call = ToolCall {
            id: "tool-call-id".to_string(),
            name: "read".to_string(),
            arguments: "{\"path\": \"/tmp/example.txt\"}".to_string(),
        };

        let ctx = RenderContext {
            palette: ThemePalette::dark(),
            spinner: "|",
            workspace_root: std::path::Path::new("/tmp"),
            expanded_tool_results: &HashSet::new(),
            expanded_tool_outputs: &HashMap::new(),
        };

        let (lines, _) = render_tool_call_with_result(&tool_call, None, 80, true, &ctx);
        let text = text_lines_to_string(&lines);

        assert!(
            text.contains("Read example.txt"),
            "should show parsed summary: {}",
            text
        );
        assert!(
            !text.contains("Calling..."),
            "pending state should be replaced: {}",
            text
        );
    }

    #[test]
    fn message_render_cache_hits_on_second_render_same_width() {
        let mut app = test_app();
        app.conversation
            .push(Message::new(MessageRole::User, "show file list"));
        app.conversation.push(Message::new(
            MessageRole::Assistant,
            "Summary with **markdown** and `inline code`.",
        ));

        let _ = app.messages_text(Some(80));
        let (_, misses_before, entries_before) = app.message_render_cache_stats();

        let _ = app.messages_text(Some(80));
        let (hits_after, misses_after, entries_after) = app.message_render_cache_stats();

        assert!(misses_before >= 2, "first render should have cache misses");
        assert!(entries_before >= 2, "first render should populate cache");
        assert!(hits_after > 0, "second render should have cache hits");
        assert_eq!(
            misses_after, misses_before,
            "second render should use cache"
        );
        assert_eq!(entries_after, entries_before, "cache size should be stable");
    }

    #[test]
    fn message_render_cache_width_change_causes_miss() {
        let mut app = test_app();
        app.conversation
            .push(Message::new(MessageRole::User, "open README"));
        app.conversation.push(Message::new(
            MessageRole::Assistant,
            "A longer paragraph that should wrap differently at another width.",
        ));

        let _ = app.messages_text(Some(72));
        let (_, misses_before, entries_before) = app.message_render_cache_stats();

        let _ = app.messages_text(Some(100));
        let (_, misses_after, entries_after) = app.message_render_cache_stats();

        assert!(misses_after > misses_before);
        assert!(entries_after > entries_before);
    }

    #[test]
    fn message_render_cache_invalidation_refreshes_updated_content() {
        let mut app = test_app();
        app.conversation
            .push(Message::new(MessageRole::Assistant, "old cached content"));

        let (before, _, _, _, _, _, _) = app.messages_text(Some(80));
        let before_text = text_lines_to_string(&before.lines);
        assert!(before_text.contains("old cached content"));

        let message_id = app.conversation.messages[0].id;
        app.conversation.messages[0].content = "new refreshed content".to_string();
        app.invalidate_active_message_render_cache_for(message_id);

        let (after, _, _, _, _, _, _) = app.messages_text(Some(80));
        let after_text = text_lines_to_string(&after.lines);
        assert!(after_text.contains("new refreshed content"));
    }

    #[test]
    fn virtualized_render_clamps_scroll_and_keeps_content_visible() {
        let mut app = test_app();
        app.message_viewport_lines = 8;
        app.message_follow_tail = false;
        app.message_scroll_offset = usize::MAX;

        for idx in 0..24 {
            app.conversation.push(Message::new(
                MessageRole::Assistant,
                format!(
                    "message {idx}\n\n```rust\nfn item_{idx}() {{\n    println!(\"ok\");\n}}\n```"
                ),
            ));
        }

        let (text, total_lines, _, _, _, _, _) = app.messages_text(Some(80));

        assert!(total_lines > 0);
        assert!(!text.lines.is_empty());
        assert!(text_lines_to_string(&text.lines).contains("message"));

        let max_scroll = total_lines.saturating_sub(app.message_viewport_lines.max(1));
        assert!(app.message_scroll_offset <= max_scroll);
    }
}

fn tool_output_is_error(output: &str) -> bool {
    let first_line = output.lines().next().unwrap_or("").trim_start();

    first_line.starts_with("Tool failed:")
        || first_line.starts_with("Tool '")
        || first_line.starts_with("Request failed:")
        || first_line.starts_with("failed to read")
        || first_line.contains("Cannot read binary file")
        || (first_line.starts_with("[exit ") && !first_line.starts_with("[exit 0]"))
}

fn summarize_tool_call(tool_name: &str, arguments: &str, body_width: usize, workspace_root: &Path) -> String {
    let canonical_name = canonical_tool_name(tool_name).unwrap_or(tool_name);
    let fields = summarize_tool_arguments(tool_name, arguments);
    let parsed = serde_json::from_str::<serde_json::Value>(arguments).ok();

    let field = |name: &str| {
        fields
            .iter()
            .find(|(key, _)| key == name)
            .map(|(_, value)| value.as_str())
    };

    let path_to_relative = |path: &str| {
        display_workspace_relative(workspace_root, Path::new(path))
    };

    let summary = match canonical_name {
        "read" => field("path")
            .map(|path| format!("Read {}", path_to_relative(path)))
            .unwrap_or_else(|| "Read file".to_string()),
        "write" => field("path")
            .map(|path| format!("Write {}", path_to_relative(path)))
            .unwrap_or_else(|| "Write file".to_string()),
        "edit" => field("path")
            .map(|path| format!("Edit {}", path_to_relative(path)))
            .unwrap_or_else(|| "Edit file".to_string()),
        "list" => field("path")
            .map(|path| format!("List {}", path_to_relative(path)))
            .unwrap_or_else(|| "List items".to_string()),
        "glob" => {
            let pattern = field("pattern").unwrap_or("*");
            let path = field("path").unwrap_or(".");
            format!("Find {} in {}", pattern, path_to_relative(path))
        }
        "grep" => {
            let pattern = field("pattern").unwrap_or("");
            let path = field("path").unwrap_or(".");
            if pattern.is_empty() {
                format!("Search in {}", path_to_relative(path))
            } else {
                format!("Search \"{pattern}\" in {}", path_to_relative(path))
            }
        }
        "bash" => field("command")
            .map(|command| format!("Run shell command: {command}"))
            .unwrap_or_else(|| "Run shell command".to_string()),
        "task" => {
            let description = field("description").unwrap_or("task");
            let subagent_type = field("subagent_type").unwrap_or("general");
            format!("Spawn {subagent_type} subagent: {description}")
        }
        "question" => {
            let count = parsed
                .as_ref()
                .and_then(|value| value.get("questions"))
                .and_then(serde_json::Value::as_array)
                .map(|questions| questions.len())
                .unwrap_or(0);

            if count == 1 {
                field("question")
                    .map(|question| format!("Ask: {question}"))
                    .unwrap_or_else(|| "Ask 1 question".to_string())
            } else {
                format!("Ask {count} question{}", if count == 1 { "" } else { "s" })
            }
        }
        "todowrite" => "Update todo list".to_string(),
        _ => {
            let mut summary = display_tool_name(tool_name);
            summary = summary[0..1].to_uppercase() + &summary[1..];
            for (label, value) in fields.iter().take(2) {
                summary.push(' ');
                summary.push_str(label);
                summary.push(' ');
                summary.push_str(value);
            }
            summary
        }
    };

    shorten_single_line(&summary, body_width.saturating_sub(2))
}

fn summarize_tool_arguments(tool_name: &str, arguments: &str) -> Vec<(String, String)> {
    let canonical_name = canonical_tool_name(tool_name).unwrap_or(tool_name);
    let parsed = serde_json::from_str::<serde_json::Value>(arguments).ok();
    let mut fields = Vec::new();

    let string_field = |key: &str| {
        parsed
            .as_ref()
            .and_then(|value| value.get(key))
            .and_then(serde_json::Value::as_str)
            .map(|value| shorten_single_line(value, 96))
    };

    match canonical_name {
        "read" => {
            if let Some(path) = string_field("path") {
                fields.push(("path".to_string(), path));
            }
            // Extract offset and limit for read tool
            if let Some(offset) = parsed
                .as_ref()
                .and_then(|v| v.get("offset"))
                .and_then(|v| v.as_i64())
            {
                fields.push(("offset".to_string(), format!("{}", offset)));
            }
            if let Some(limit) = parsed
                .as_ref()
                .and_then(|v| v.get("limit"))
                .and_then(|v| v.as_i64())
            {
                fields.push(("limit".to_string(), format!("{}", limit)));
            }
        }
        "write" | "edit" => {
            if let Some(path) = string_field("path") {
                fields.push(("path".to_string(), path));
            }
        }
        "list" => {
            fields.push((
                "path".to_string(),
                string_field("path").unwrap_or_else(|| ".".to_string()),
            ));
        }
        "glob" => {
            if let Some(pattern) = string_field("pattern") {
                fields.push(("pattern".to_string(), pattern));
            }
            fields.push((
                "path".to_string(),
                string_field("path").unwrap_or_else(|| ".".to_string()),
            ));
        }
        "grep" => {
            if let Some(pattern) = string_field("pattern") {
                fields.push(("pattern".to_string(), pattern));
            }
            fields.push((
                "path".to_string(),
                string_field("path").unwrap_or_else(|| ".".to_string()),
            ));
            if let Some(include) = string_field("include") {
                fields.push(("include".to_string(), include));
            }
        }
        "bash" => {
            if let Some(command) = string_field("command") {
                fields.push(("command".to_string(), command));
            }
        }
        "task" => {
            if let Some(description) = string_field("description") {
                fields.push(("description".to_string(), description));
            }
            if let Some(subagent_type) = string_field("subagent_type") {
                fields.push(("subagent_type".to_string(), subagent_type));
            }
        }
        "question" => {
            let question_count = parsed
                .as_ref()
                .and_then(|value| value.get("questions"))
                .and_then(serde_json::Value::as_array)
                .map(|questions| questions.len())
                .unwrap_or(0);

            fields.push((
                "questions".to_string(),
                format!("{question_count} question(s)"),
            ));

            if let Some(first_question) = parsed
                .as_ref()
                .and_then(|value| value.get("questions"))
                .and_then(serde_json::Value::as_array)
                .and_then(|questions| questions.first())
                .and_then(|question| question.get("question"))
                .and_then(serde_json::Value::as_str)
            {
                fields.push((
                    "question".to_string(),
                    shorten_single_line(first_question, 96),
                ));
            }
        }
        "todowrite" => {
            let todo_count = parsed
                .as_ref()
                .and_then(|value| value.get("todos"))
                .and_then(serde_json::Value::as_array)
                .map(|todos| format!("{} item(s)", todos.len()));

            if let Some(todo_count) = todo_count {
                fields.push(("todos".to_string(), todo_count));
            }
        }
        _ => {}
    }

    if fields.is_empty() {
        fields.push((
            "arguments".to_string(),
            shorten_single_line(&pretty_tool_arguments(arguments), 120),
        ));
    }

    fields
}

/// Parse line range from read tool output (e.g., "Showing lines 10-50 of 100")
fn parse_line_range_from_read_output(output: &str) -> Option<(i64, i64)> {
    // Match patterns like "Showing lines 10-50 of 100" or "Showing lines 10-50"
    if let Some(start) = output.find("Showing lines ") {
        let after_prefix = &output[start + 14..];
        // Parse start number
        let mut end_idx = 0;
        let mut start_num = 0i64;
        for (i, c) in after_prefix.chars().enumerate() {
            if c.is_ascii_digit() {
                start_num = start_num * 10 + (c as i64 - '0' as i64);
                end_idx = i;
            } else {
                break;
            }
        }
        // Look for "-{end}" after "Showing lines {start}-"
        let after_start = &after_prefix[end_idx + 1..];
        if let Some(stripped) = after_start.strip_prefix('-') {
            let mut end_num = 0i64;
            for c in stripped.chars() {
                if c.is_ascii_digit() {
                    end_num = end_num * 10 + (c as i64 - '0' as i64);
                } else {
                    break;
                }
            }
            if end_num > start_num {
                return Some((start_num, end_num));
            }
        }
    }
    None
}

/// Parse range string "start-end" into (start, end).
fn parse_range(s: &str) -> Option<(i64, i64)> {
    let parts: Vec<_> = s.split('-').collect();
    if parts.len() == 2 {
        let start = parts[0].trim().parse().ok()?;
        let end = parts[1].trim().parse().ok()?;
        Some((start, end))
    } else {
        None
    }
}

/// Parsed metadata from a read tool output's XML-style metadata block.
/// Returns (line_range, optional_requested_range, file_total, optional_truncation_reason).
type ReadContentMetadata = Option<(
    (i64, i64),              // line_range (start, end)
    Option<(i64, i64)>,      // requested_range (None if model didn't specify)
    i64,                     // file_total
    Option<String>,          // truncated_by (None | "size" | "lines")
)>;

fn parse_read_content_metadata(content: &str) -> ReadContentMetadata {
    let mut line_range = None;
    let mut requested_range = None;
    let mut file_total = None;
    let mut truncated_by = None;

    for line in content.lines() {
        if let Some(val) = line.strip_prefix("<line_range>") {
            if let Some(end) = val.strip_suffix("</line_range>") {
                line_range = parse_range(end);
            }
        } else if let Some(val) = line.strip_prefix("<requested_range>") {
            if let Some(end) = val.strip_suffix("</requested_range>") {
                requested_range = parse_range(end);
            }
        } else if let Some(val) = line.strip_prefix("<file_total>") {
            if let Some(end) = val.strip_suffix("</file_total>") {
                file_total = end.trim().parse().ok();
            }
        } else if let Some(val) = line.strip_prefix("<truncated_by>")
            && let Some(end) = val.strip_suffix("</truncated_by>")
            && matches!(end.trim(), "size" | "lines")
        {
            truncated_by = Some(end.trim().to_string());
        }
    }

    match (line_range, file_total) {
        (Some(lr), Some(ft)) => Some((lr, requested_range, ft, truncated_by)),
        _ => None,
    }
}

fn pretty_tool_arguments(arguments: &str) -> String {
    match serde_json::from_str::<serde_json::Value>(arguments) {
        Ok(value) => serde_json::to_string_pretty(&value).unwrap_or_else(|_| arguments.to_string()),
        Err(_) => arguments.to_string(),
    }
}

fn display_tool_name(tool_name: &str) -> String {
    crate::tooling::canonical_tool_name(tool_name)
        .unwrap_or(tool_name)
        .to_string()
}