oxi-cli 0.4.1

Terminal-based AI coding assistant — multi-provider, streaming-first, extensible
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
3355
3356
3357
3358
3359
3360
3361
3362
3363
3364
3365
3366
3367
3368
3369
3370
3371
3372
3373
3374
3375
3376
3377
3378
3379
3380
3381
3382
3383
3384
3385
3386
3387
3388
3389
3390
3391
3392
3393
3394
3395
3396
3397
3398
3399
3400
3401
3402
3403
3404
3405
3406
3407
3408
3409
3410
3411
3412
3413
3414
3415
3416
3417
3418
3419
3420
3421
3422
3423
3424
3425
3426
3427
3428
3429
3430
3431
3432
3433
3434
3435
3436
3437
3438
3439
3440
3441
3442
3443
3444
3445
3446
3447
3448
3449
3450
3451
3452
3453
3454
3455
3456
3457
3458
3459
3460
3461
3462
3463
3464
3465
3466
3467
3468
3469
3470
3471
3472
3473
3474
3475
3476
3477
3478
3479
3480
3481
3482
3483
3484
3485
3486
3487
3488
3489
3490
3491
3492
3493
3494
3495
3496
3497
3498
3499
3500
3501
3502
3503
3504
3505
3506
3507
3508
3509
3510
3511
3512
3513
3514
3515
3516
3517
3518
3519
3520
3521
3522
3523
3524
3525
3526
3527
3528
3529
3530
3531
3532
3533
3534
3535
3536
3537
3538
3539
3540
3541
3542
3543
3544
3545
3546
3547
3548
3549
3550
3551
3552
3553
3554
3555
3556
3557
3558
3559
3560
3561
3562
3563
3564
3565
3566
3567
3568
3569
3570
3571
3572
3573
3574
3575
3576
3577
3578
3579
3580
3581
3582
3583
3584
3585
3586
3587
3588
3589
3590
3591
3592
3593
3594
3595
3596
3597
3598
3599
3600
3601
3602
3603
3604
3605
3606
3607
3608
3609
3610
3611
3612
3613
3614
3615
3616
3617
3618
3619
3620
3621
3622
3623
3624
3625
3626
3627
3628
3629
3630
3631
3632
3633
3634
3635
3636
3637
3638
3639
3640
3641
3642
3643
3644
3645
3646
3647
3648
3649
3650
3651
3652
3653
3654
3655
3656
3657
3658
3659
3660
3661
3662
3663
3664
3665
3666
3667
3668
3669
3670
3671
3672
3673
3674
3675
3676
3677
3678
3679
3680
3681
3682
3683
3684
3685
3686
3687
3688
3689
3690
3691
3692
3693
3694
3695
3696
3697
3698
3699
3700
3701
3702
3703
3704
3705
3706
3707
3708
3709
3710
3711
3712
3713
3714
3715
3716
3717
3718
3719
3720
3721
3722
3723
3724
3725
3726
3727
3728
3729
3730
3731
3732
3733
3734
3735
3736
3737
3738
3739
3740
3741
3742
3743
3744
3745
3746
3747
3748
3749
3750
3751
3752
3753
3754
3755
3756
3757
3758
3759
3760
3761
3762
3763
3764
3765
3766
3767
3768
3769
3770
3771
3772
3773
3774
3775
3776
3777
3778
3779
3780
3781
3782
3783
3784
3785
3786
3787
3788
3789
3790
3791
3792
3793
3794
3795
3796
3797
3798
3799
3800
3801
3802
3803
3804
3805
3806
3807
3808
3809
3810
3811
3812
3813
3814
3815
3816
3817
3818
3819
3820
3821
3822
3823
3824
3825
3826
3827
3828
3829
3830
3831
3832
3833
3834
3835
3836
3837
3838
3839
3840
3841
3842
3843
3844
3845
3846
3847
3848
3849
3850
3851
3852
3853
3854
3855
3856
3857
3858
3859
3860
3861
3862
3863
3864
3865
3866
3867
3868
3869
3870
3871
3872
3873
3874
3875
3876
3877
3878
3879
3880
3881
3882
3883
3884
3885
3886
3887
3888
3889
3890
3891
3892
3893
3894
3895
3896
3897
3898
3899
3900
3901
3902
3903
3904
3905
3906
3907
3908
3909
3910
3911
3912
3913
3914
3915
3916
3917
3918
3919
3920
3921
3922
3923
3924
3925
3926
3927
3928
3929
3930
3931
3932
3933
3934
3935
3936
3937
3938
3939
3940
3941
3942
3943
3944
3945
3946
3947
3948
3949
3950
3951
3952
3953
3954
3955
3956
3957
3958
3959
3960
3961
3962
3963
3964
3965
3966
3967
3968
3969
3970
3971
3972
3973
3974
3975
3976
3977
3978
3979
3980
3981
3982
3983
3984
3985
3986
3987
3988
3989
3990
3991
3992
3993
3994
3995
3996
3997
3998
3999
4000
4001
4002
4003
4004
4005
4006
4007
4008
4009
4010
4011
4012
4013
4014
4015
4016
4017
4018
4019
4020
4021
4022
4023
4024
4025
4026
4027
4028
4029
4030
4031
4032
4033
4034
4035
4036
4037
4038
4039
4040
4041
4042
4043
4044
4045
4046
4047
4048
4049
4050
4051
4052
4053
4054
4055
4056
4057
4058
4059
4060
4061
4062
4063
4064
4065
4066
4067
4068
4069
4070
4071
4072
4073
4074
4075
4076
4077
4078
4079
4080
4081
4082
4083
4084
4085
4086
4087
4088
4089
4090
4091
4092
4093
4094
4095
4096
4097
4098
4099
4100
4101
4102
4103
4104
4105
4106
4107
4108
4109
4110
4111
4112
4113
4114
4115
4116
4117
4118
4119
4120
4121
4122
4123
4124
4125
4126
4127
4128
4129
4130
4131
4132
4133
4134
4135
4136
4137
4138
4139
4140
4141
4142
4143
4144
4145
4146
4147
4148
4149
4150
4151
4152
4153
4154
4155
4156
4157
4158
4159
4160
4161
4162
4163
4164
4165
4166
4167
4168
4169
4170
4171
4172
4173
4174
4175
4176
4177
4178
4179
4180
4181
4182
4183
4184
4185
4186
4187
4188
4189
4190
4191
4192
4193
4194
4195
4196
4197
4198
4199
4200
4201
4202
4203
4204
4205
4206
4207
4208
4209
4210
4211
4212
4213
4214
4215
4216
4217
4218
4219
4220
4221
4222
4223
4224
4225
4226
4227
4228
4229
4230
4231
4232
4233
4234
4235
4236
4237
4238
4239
4240
4241
4242
4243
4244
4245
4246
4247
4248
4249
4250
4251
4252
4253
4254
4255
4256
4257
4258
4259
4260
4261
4262
4263
4264
4265
4266
4267
4268
4269
4270
4271
4272
4273
4274
4275
4276
4277
4278
4279
4280
4281
4282
4283
4284
4285
4286
4287
4288
4289
4290
4291
4292
4293
4294
4295
4296
4297
4298
4299
4300
4301
4302
4303
4304
4305
4306
4307
4308
4309
4310
4311
4312
4313
4314
4315
4316
4317
4318
4319
4320
4321
4322
4323
4324
4325
4326
4327
4328
4329
4330
4331
4332
4333
4334
4335
4336
4337
4338
4339
4340
4341
4342
4343
4344
4345
4346
4347
4348
4349
4350
4351
4352
4353
4354
4355
4356
4357
4358
4359
4360
4361
4362
4363
4364
4365
4366
4367
4368
4369
4370
4371
4372
4373
4374
4375
4376
4377
4378
4379
4380
4381
4382
4383
4384
4385
4386
4387
4388
4389
4390
4391
4392
4393
4394
4395
4396
4397
4398
4399
4400
4401
4402
4403
4404
4405
4406
4407
4408
4409
4410
4411
4412
4413
4414
4415
4416
4417
4418
4419
4420
4421
4422
4423
4424
4425
4426
4427
4428
4429
4430
4431
4432
4433
4434
4435
4436
4437
4438
4439
4440
4441
4442
4443
4444
4445
4446
4447
4448
4449
4450
4451
4452
4453
4454
4455
4456
4457
4458
4459
4460
4461
4462
4463
4464
4465
4466
4467
4468
4469
4470
4471
4472
4473
4474
4475
4476
4477
4478
4479
4480
4481
4482
4483
4484
4485
4486
4487
4488
4489
4490
4491
4492
4493
4494
4495
4496
4497
4498
4499
4500
4501
4502
4503
4504
4505
4506
4507
4508
4509
4510
4511
4512
4513
4514
4515
4516
4517
4518
4519
4520
4521
4522
4523
4524
4525
4526
4527
4528
4529
4530
4531
4532
4533
4534
4535
4536
4537
4538
4539
4540
4541
4542
4543
4544
4545
4546
4547
4548
4549
4550
4551
4552
4553
4554
4555
4556
4557
4558
4559
4560
4561
4562
4563
4564
4565
4566
4567
4568
4569
4570
4571
4572
4573
4574
4575
4576
4577
4578
4579
4580
4581
4582
4583
4584
4585
4586
4587
4588
4589
4590
4591
4592
4593
4594
4595
4596
4597
4598
4599
4600
4601
4602
4603
4604
4605
4606
4607
4608
4609
4610
4611
4612
4613
4614
4615
4616
4617
4618
4619
4620
4621
4622
4623
4624
4625
4626
4627
4628
4629
4630
4631
4632
4633
4634
4635
4636
4637
4638
4639
4640
4641
4642
4643
4644
4645
4646
4647
4648
4649
4650
4651
4652
4653
4654
4655
4656
4657
4658
4659
4660
4661
4662
4663
4664
4665
4666
4667
4668
4669
4670
4671
4672
4673
4674
4675
4676
4677
4678
4679
4680
4681
4682
4683
4684
4685
4686
4687
4688
4689
4690
4691
4692
4693
4694
4695
4696
4697
4698
4699
4700
4701
4702
4703
4704
4705
4706
4707
4708
4709
4710
4711
4712
4713
4714
4715
4716
4717
4718
4719
4720
4721
4722
4723
4724
4725
4726
4727
4728
4729
4730
4731
4732
4733
4734
4735
4736
4737
4738
4739
4740
4741
4742
4743
4744
4745
4746
4747
4748
4749
4750
4751
4752
4753
4754
4755
4756
4757
4758
4759
4760
4761
4762
4763
4764
4765
4766
4767
4768
4769
4770
4771
4772
4773
4774
4775
4776
4777
4778
4779
4780
4781
4782
4783
4784
4785
4786
4787
4788
4789
4790
4791
4792
4793
4794
4795
4796
4797
4798
4799
4800
4801
4802
4803
4804
4805
4806
4807
4808
4809
4810
4811
4812
4813
4814
4815
4816
4817
4818
4819
4820
4821
4822
4823
4824
4825
4826
4827
4828
4829
4830
4831
4832
4833
4834
4835
4836
4837
4838
4839
4840
4841
4842
4843
4844
4845
4846
4847
4848
4849
4850
4851
4852
4853
4854
4855
4856
4857
4858
4859
4860
4861
4862
4863
4864
4865
4866
4867
4868
4869
4870
4871
4872
4873
4874
4875
4876
4877
4878
4879
4880
4881
4882
4883
4884
4885
4886
4887
4888
4889
4890
4891
4892
4893
4894
4895
4896
4897
4898
4899
4900
4901
4902
4903
4904
4905
4906
4907
4908
4909
4910
4911
4912
4913
4914
4915
4916
4917
4918
4919
4920
4921
4922
4923
4924
4925
4926
4927
4928
4929
4930
4931
4932
4933
4934
4935
4936
4937
4938
4939
4940
4941
4942
4943
4944
4945
4946
4947
4948
4949
4950
4951
4952
4953
4954
4955
4956
4957
4958
4959
4960
4961
4962
4963
4964
4965
4966
4967
4968
4969
4970
4971
4972
4973
4974
4975
4976
4977
4978
4979
4980
4981
4982
4983
4984
4985
4986
4987
4988
4989
4990
4991
4992
4993
4994
4995
4996
4997
4998
4999
5000
5001
5002
5003
5004
5005
5006
5007
5008
5009
5010
5011
5012
5013
5014
5015
5016
5017
5018
5019
5020
5021
5022
5023
5024
5025
5026
5027
5028
5029
5030
5031
5032
5033
5034
5035
5036
5037
5038
5039
5040
5041
5042
5043
5044
5045
5046
5047
5048
5049
5050
5051
5052
5053
5054
5055
5056
5057
5058
5059
5060
5061
5062
5063
5064
5065
5066
5067
5068
5069
5070
5071
5072
5073
5074
5075
5076
5077
5078
5079
5080
5081
5082
5083
5084
5085
5086
5087
5088
5089
5090
5091
5092
5093
5094
5095
5096
5097
5098
5099
5100
5101
5102
5103
5104
5105
5106
5107
5108
5109
5110
5111
5112
5113
5114
5115
5116
5117
5118
5119
5120
5121
5122
5123
5124
5125
5126
5127
5128
5129
5130
5131
5132
5133
5134
5135
5136
5137
5138
5139
5140
5141
5142
5143
5144
5145
5146
5147
5148
5149
5150
5151
5152
5153
5154
5155
5156
5157
5158
5159
5160
5161
5162
5163
5164
5165
5166
5167
5168
5169
5170
5171
5172
5173
5174
5175
5176
5177
5178
5179
5180
5181
5182
5183
5184
5185
5186
5187
5188
5189
5190
5191
5192
5193
5194
5195
5196
5197
5198
5199
5200
5201
5202
5203
5204
5205
5206
5207
5208
5209
5210
5211
5212
5213
5214
5215
5216
5217
5218
5219
5220
5221
5222
5223
5224
5225
5226
5227
5228
5229
5230
5231
5232
5233
5234
5235
5236
5237
5238
5239
5240
5241
5242
5243
5244
5245
5246
5247
5248
5249
5250
5251
5252
5253
5254
5255
5256
5257
5258
5259
5260
5261
5262
5263
5264
5265
5266
5267
5268
5269
5270
5271
5272
5273
5274
5275
5276
5277
5278
5279
5280
5281
5282
5283
5284
5285
5286
5287
5288
5289
5290
5291
5292
5293
5294
5295
5296
5297
5298
5299
5300
5301
5302
5303
5304
5305
5306
5307
5308
5309
5310
5311
5312
5313
5314
5315
5316
5317
5318
5319
5320
5321
5322
5323
5324
5325
5326
5327
5328
5329
5330
5331
5332
5333
5334
5335
5336
5337
5338
5339
5340
5341
5342
5343
5344
5345
5346
5347
5348
5349
5350
5351
5352
5353
5354
5355
5356
5357
5358
5359
5360
5361
5362
5363
5364
5365
5366
5367
5368
5369
5370
5371
5372
5373
5374
5375
5376
5377
5378
5379
5380
5381
5382
5383
5384
5385
5386
5387
5388
5389
5390
5391
5392
5393
5394
5395
5396
5397
5398
5399
5400
5401
5402
5403
5404
5405
5406
5407
5408
5409
5410
5411
5412
5413
5414
5415
5416
5417
5418
5419
5420
5421
5422
5423
5424
5425
5426
5427
5428
5429
5430
5431
5432
5433
5434
5435
5436
5437
5438
5439
5440
5441
5442
5443
5444
5445
5446
5447
5448
5449
5450
5451
5452
5453
5454
5455
5456
5457
5458
5459
5460
5461
5462
5463
5464
5465
5466
5467
5468
5469
5470
5471
5472
5473
5474
5475
5476
5477
5478
5479
5480
5481
5482
5483
5484
5485
5486
5487
5488
5489
5490
5491
5492
5493
5494
5495
5496
5497
5498
5499
5500
5501
5502
5503
5504
5505
5506
5507
5508
5509
5510
5511
5512
5513
5514
5515
5516
5517
5518
5519
5520
5521
5522
5523
5524
5525
5526
5527
5528
5529
5530
5531
5532
5533
5534
5535
5536
5537
5538
5539
5540
5541
5542
5543
5544
5545
5546
5547
5548
5549
5550
5551
5552
5553
5554
5555
5556
5557
5558
5559
5560
5561
5562
5563
5564
5565
5566
5567
5568
5569
5570
5571
5572
5573
5574
5575
5576
5577
5578
5579
5580
5581
5582
5583
5584
5585
5586
5587
5588
5589
5590
5591
5592
5593
5594
5595
5596
5597
5598
5599
5600
5601
5602
5603
5604
5605
5606
5607
5608
5609
5610
5611
5612
5613
5614
5615
5616
5617
5618
5619
5620
5621
5622
5623
5624
5625
5626
5627
5628
5629
5630
5631
5632
5633
5634
5635
5636
5637
5638
5639
5640
5641
5642
5643
5644
5645
5646
5647
5648
5649
5650
5651
5652
5653
5654
5655
5656
5657
5658
5659
5660
5661
5662
5663
5664
5665
5666
5667
5668
5669
5670
5671
5672
5673
5674
5675
5676
5677
5678
5679
5680
5681
5682
5683
5684
5685
5686
5687
5688
5689
5690
5691
5692
5693
5694
5695
5696
5697
5698
5699
5700
5701
5702
5703
5704
5705
5706
5707
5708
5709
5710
5711
5712
5713
5714
5715
5716
5717
5718
5719
5720
5721
5722
5723
5724
5725
5726
5727
5728
5729
5730
5731
5732
5733
5734
5735
5736
5737
5738
5739
5740
5741
5742
5743
5744
5745
5746
5747
5748
5749
5750
5751
5752
5753
5754
5755
5756
5757
5758
5759
5760
5761
5762
5763
5764
5765
5766
5767
5768
5769
5770
5771
5772
5773
5774
5775
5776
5777
5778
5779
5780
5781
5782
5783
5784
5785
5786
5787
5788
5789
5790
5791
5792
5793
5794
5795
5796
5797
5798
5799
5800
5801
5802
5803
5804
5805
5806
5807
5808
5809
5810
5811
5812
5813
5814
5815
5816
5817
5818
5819
5820
5821
5822
5823
5824
5825
5826
5827
5828
5829
5830
5831
5832
5833
5834
5835
5836
5837
5838
5839
5840
5841
5842
5843
5844
5845
5846
5847
5848
5849
5850
5851
5852
5853
5854
5855
5856
5857
5858
5859
5860
5861
5862
5863
5864
5865
5866
5867
5868
5869
5870
5871
5872
5873
5874
5875
5876
5877
5878
5879
5880
5881
5882
5883
5884
5885
5886
5887
5888
5889
5890
5891
5892
5893
5894
5895
5896
5897
5898
5899
5900
5901
5902
5903
5904
5905
5906
5907
5908
5909
5910
5911
5912
5913
5914
5915
5916
5917
5918
5919
5920
5921
5922
5923
5924
5925
5926
5927
5928
5929
5930
5931
5932
5933
5934
5935
5936
5937
5938
5939
5940
5941
5942
5943
5944
5945
5946
5947
5948
5949
5950
5951
5952
5953
5954
5955
5956
5957
5958
5959
5960
5961
5962
5963
5964
5965
5966
5967
5968
5969
5970
5971
5972
5973
5974
5975
5976
5977
5978
5979
5980
5981
5982
5983
5984
5985
5986
5987
5988
5989
5990
5991
5992
5993
5994
5995
5996
5997
5998
5999
6000
6001
6002
6003
6004
6005
6006
6007
6008
6009
6010
6011
6012
6013
6014
6015
6016
6017
6018
6019
6020
6021
6022
6023
6024
6025
6026
6027
6028
6029
6030
6031
6032
6033
6034
6035
6036
6037
6038
6039
6040
6041
6042
6043
6044
6045
6046
6047
6048
6049
6050
6051
6052
6053
6054
6055
6056
6057
6058
6059
6060
6061
6062
6063
6064
6065
6066
6067
6068
6069
6070
6071
6072
6073
6074
6075
6076
6077
6078
6079
6080
6081
6082
6083
6084
6085
6086
6087
6088
6089
6090
6091
6092
6093
6094
6095
6096
6097
6098
6099
6100
6101
6102
6103
6104
6105
6106
6107
6108
6109
6110
6111
6112
6113
6114
6115
6116
6117
6118
6119
6120
6121
6122
6123
6124
6125
6126
6127
6128
6129
6130
6131
6132
6133
6134
6135
6136
6137
6138
6139
6140
6141
6142
6143
6144
6145
6146
6147
6148
6149
6150
6151
6152
6153
6154
6155
6156
6157
6158
6159
6160
6161
6162
6163
6164
6165
6166
6167
6168
6169
6170
6171
6172
6173
6174
6175
6176
6177
6178
6179
6180
6181
6182
6183
6184
6185
6186
6187
6188
6189
6190
6191
6192
6193
6194
6195
6196
6197
6198
6199
6200
6201
6202
6203
6204
6205
6206
6207
6208
6209
6210
6211
6212
6213
6214
6215
6216
6217
6218
6219
6220
6221
6222
6223
6224
6225
6226
6227
6228
6229
6230
6231
6232
6233
6234
6235
6236
6237
6238
6239
6240
6241
6242
6243
6244
6245
6246
6247
6248
6249
6250
6251
6252
6253
6254
6255
6256
6257
6258
6259
6260
6261
6262
6263
6264
6265
6266
6267
6268
6269
6270
6271
6272
6273
6274
6275
6276
6277
6278
6279
6280
6281
6282
6283
6284
6285
6286
6287
6288
6289
6290
6291
6292
6293
6294
6295
6296
6297
6298
6299
6300
6301
6302
6303
6304
6305
6306
6307
6308
6309
6310
6311
6312
6313
6314
6315
6316
6317
6318
6319
6320
6321
6322
6323
6324
6325
6326
6327
6328
6329
6330
6331
6332
6333
6334
6335
6336
6337
6338
6339
6340
6341
6342
6343
6344
6345
6346
6347
6348
6349
6350
6351
6352
6353
6354
6355
6356
6357
6358
6359
6360
6361
6362
6363
6364
6365
6366
6367
6368
6369
6370
6371
6372
6373
6374
6375
6376
6377
6378
6379
6380
6381
6382
6383
6384
6385
6386
6387
6388
6389
6390
6391
6392
6393
6394
6395
6396
6397
6398
6399
6400
6401
6402
6403
6404
6405
6406
6407
6408
6409
6410
6411
6412
6413
6414
6415
6416
6417
6418
6419
6420
6421
6422
6423
6424
6425
6426
6427
6428
6429
6430
6431
6432
6433
6434
6435
6436
6437
6438
6439
6440
6441
6442
6443
6444
6445
6446
6447
6448
6449
6450
6451
6452
6453
6454
6455
6456
6457
6458
6459
6460
6461
6462
6463
6464
6465
6466
6467
6468
6469
6470
6471
6472
6473
6474
6475
6476
6477
6478
6479
6480
6481
6482
6483
6484
6485
6486
6487
6488
6489
6490
6491
6492
6493
6494
6495
6496
6497
6498
6499
6500
6501
6502
6503
6504
6505
6506
6507
6508
6509
6510
6511
6512
6513
6514
6515
6516
6517
6518
6519
6520
6521
6522
6523
6524
6525
6526
6527
6528
6529
6530
6531
6532
6533
6534
6535
6536
6537
6538
6539
6540
6541
6542
6543
6544
6545
6546
6547
6548
6549
6550
6551
6552
6553
6554
6555
6556
6557
6558
6559
6560
6561
6562
6563
6564
6565
6566
6567
6568
6569
6570
6571
6572
6573
6574
6575
6576
6577
6578
6579
6580
6581
6582
6583
6584
6585
6586
6587
6588
6589
6590
6591
6592
6593
6594
6595
6596
6597
6598
6599
6600
6601
6602
6603
6604
6605
6606
6607
6608
6609
6610
6611
6612
6613
6614
6615
6616
6617
6618
6619
6620
6621
6622
6623
6624
6625
6626
6627
6628
6629
6630
6631
6632
6633
6634
6635
6636
6637
6638
6639
6640
6641
6642
6643
6644
6645
6646
6647
6648
6649
6650
6651
6652
6653
6654
6655
6656
6657
6658
6659
6660
6661
6662
6663
6664
6665
6666
6667
6668
6669
6670
6671
6672
6673
6674
6675
6676
6677
6678
6679
6680
6681
6682
6683
6684
6685
6686
6687
6688
6689
6690
6691
6692
6693
6694
6695
6696
6697
6698
6699
6700
6701
6702
6703
6704
6705
6706
6707
6708
6709
6710
6711
6712
6713
6714
6715
6716
6717
6718
6719
6720
6721
6722
6723
6724
6725
6726
6727
6728
6729
6730
6731
6732
6733
6734
6735
6736
6737
6738
6739
6740
6741
6742
6743
6744
6745
6746
6747
6748
6749
6750
6751
6752
6753
6754
6755
6756
6757
6758
6759
6760
6761
6762
6763
6764
6765
6766
6767
6768
6769
6770
6771
6772
6773
6774
6775
6776
6777
6778
6779
6780
6781
6782
6783
6784
6785
6786
6787
6788
6789
6790
6791
6792
6793
6794
6795
6796
6797
6798
6799
6800
6801
6802
6803
6804
6805
6806
6807
6808
6809
6810
6811
6812
6813
6814
6815
6816
6817
6818
6819
6820
6821
6822
6823
6824
6825
6826
6827
6828
6829
6830
6831
6832
6833
6834
6835
6836
6837
6838
6839
6840
6841
6842
6843
6844
6845
6846
6847
6848
6849
6850
6851
6852
6853
6854
6855
6856
6857
6858
6859
6860
6861
6862
6863
6864
6865
6866
6867
6868
6869
6870
6871
6872
6873
6874
6875
6876
6877
6878
6879
6880
6881
6882
6883
6884
6885
6886
6887
6888
6889
6890
6891
6892
6893
6894
6895
6896
6897
6898
6899
6900
6901
6902
6903
6904
6905
6906
6907
6908
6909
6910
6911
6912
6913
6914
6915
6916
6917
6918
6919
6920
6921
6922
6923
6924
6925
6926
6927
6928
6929
6930
6931
6932
6933
6934
6935
6936
6937
6938
6939
6940
6941
6942
6943
6944
6945
6946
6947
6948
6949
6950
6951
6952
6953
6954
6955
6956
6957
6958
6959
6960
6961
6962
6963
6964
6965
6966
6967
6968
6969
6970
6971
6972
6973
6974
6975
6976
6977
6978
6979
6980
6981
6982
6983
6984
6985
6986
6987
6988
6989
6990
6991
6992
6993
6994
6995
6996
6997
6998
6999
7000
7001
7002
7003
7004
7005
7006
7007
7008
7009
7010
7011
7012
7013
7014
7015
7016
7017
7018
7019
7020
7021
7022
7023
7024
7025
7026
7027
7028
7029
7030
7031
7032
7033
7034
7035
7036
7037
7038
7039
7040
7041
7042
7043
7044
7045
7046
7047
7048
7049
7050
7051
7052
7053
7054
7055
7056
7057
7058
7059
7060
7061
7062
7063
7064
7065
7066
7067
7068
7069
7070
7071
7072
7073
7074
7075
7076
7077
7078
7079
7080
7081
7082
7083
7084
7085
7086
7087
7088
7089
7090
7091
7092
7093
7094
7095
7096
7097
7098
7099
7100
7101
7102
7103
7104
7105
7106
7107
7108
7109
7110
7111
7112
7113
7114
7115
7116
7117
7118
7119
7120
7121
7122
7123
7124
7125
7126
7127
7128
7129
7130
7131
7132
7133
7134
7135
7136
7137
7138
7139
7140
7141
7142
7143
7144
7145
7146
7147
7148
7149
7150
7151
7152
7153
7154
7155
7156
7157
7158
7159
7160
7161
7162
7163
7164
7165
7166
7167
7168
7169
7170
7171
7172
7173
7174
7175
7176
7177
7178
7179
7180
7181
7182
7183
7184
7185
7186
7187
7188
7189
7190
7191
7192
7193
7194
7195
7196
7197
7198
7199
7200
7201
7202
7203
7204
7205
7206
7207
7208
7209
7210
7211
7212
7213
7214
7215
7216
7217
7218
7219
7220
7221
7222
7223
7224
7225
7226
7227
7228
7229
7230
7231
7232
7233
7234
7235
7236
7237
7238
7239
7240
7241
7242
7243
7244
7245
7246
7247
7248
7249
7250
7251
7252
7253
7254
7255
7256
7257
7258
7259
7260
7261
7262
7263
7264
7265
7266
7267
7268
7269
7270
7271
7272
7273
7274
7275
7276
7277
7278
7279
7280
7281
7282
7283
7284
7285
7286
7287
7288
7289
7290
7291
7292
7293
7294
7295
7296
7297
7298
7299
7300
7301
7302
7303
7304
7305
7306
7307
7308
7309
7310
7311
7312
7313
7314
7315
7316
7317
7318
7319
7320
7321
7322
7323
7324
7325
7326
7327
7328
7329
7330
7331
7332
7333
7334
7335
7336
7337
7338
7339
7340
7341
7342
7343
7344
7345
7346
7347
7348
7349
7350
7351
7352
7353
7354
7355
7356
7357
7358
7359
7360
7361
7362
7363
7364
7365
7366
7367
7368
7369
7370
7371
7372
7373
7374
7375
7376
7377
7378
7379
7380
7381
7382
7383
7384
7385
7386
7387
7388
7389
7390
7391
7392
7393
7394
7395
7396
7397
7398
7399
7400
7401
7402
7403
7404
7405
7406
7407
7408
7409
7410
7411
7412
7413
7414
7415
7416
7417
7418
7419
7420
7421
7422
7423
7424
7425
7426
7427
7428
7429
7430
7431
7432
7433
7434
7435
7436
7437
7438
7439
7440
7441
7442
7443
7444
7445
7446
7447
7448
7449
7450
7451
7452
7453
7454
7455
7456
7457
7458
7459
7460
7461
7462
7463
7464
7465
7466
7467
7468
7469
7470
7471
7472
7473
7474
7475
7476
7477
7478
7479
7480
7481
7482
7483
7484
7485
7486
7487
7488
7489
7490
7491
7492
7493
7494
7495
7496
7497
7498
7499
7500
7501
7502
7503
7504
7505
7506
7507
7508
7509
7510
7511
7512
7513
7514
7515
7516
7517
7518
7519
7520
7521
7522
7523
7524
7525
7526
7527
7528
7529
7530
7531
7532
7533
7534
7535
7536
7537
7538
7539
7540
7541
7542
7543
7544
7545
7546
7547
7548
7549
7550
7551
7552
7553
7554
7555
7556
7557
7558
7559
7560
7561
7562
7563
7564
7565
7566
7567
7568
7569
7570
7571
7572
7573
7574
7575
7576
7577
7578
7579
7580
7581
7582
7583
7584
7585
7586
7587
7588
7589
7590
7591
7592
7593
7594
7595
7596
7597
7598
7599
7600
7601
7602
7603
7604
7605
7606
7607
7608
7609
7610
7611
7612
7613
7614
7615
7616
7617
7618
7619
7620
7621
7622
7623
7624
7625
7626
7627
7628
7629
7630
7631
7632
7633
7634
7635
7636
7637
7638
7639
7640
7641
7642
7643
7644
7645
7646
7647
7648
7649
7650
7651
7652
7653
7654
7655
7656
7657
7658
7659
7660
7661
7662
7663
7664
7665
7666
7667
7668
7669
7670
7671
7672
7673
7674
7675
7676
7677
7678
7679
7680
7681
7682
7683
7684
7685
7686
7687
7688
7689
7690
7691
7692
7693
7694
7695
7696
7697
7698
7699
7700
7701
7702
7703
7704
7705
7706
7707
7708
7709
7710
7711
7712
7713
7714
7715
7716
7717
7718
7719
7720
7721
7722
7723
7724
7725
7726
7727
7728
7729
7730
7731
7732
7733
7734
7735
7736
7737
7738
7739
7740
7741
7742
7743
7744
7745
7746
7747
7748
7749
7750
7751
7752
7753
7754
7755
7756
7757
7758
7759
7760
7761
7762
7763
7764
7765
7766
7767
7768
7769
7770
7771
7772
7773
7774
7775
7776
7777
7778
7779
7780
7781
7782
7783
7784
7785
7786
7787
7788
7789
7790
7791
7792
7793
7794
7795
7796
7797
7798
7799
7800
7801
7802
7803
7804
7805
7806
7807
7808
7809
7810
7811
7812
7813
7814
7815
7816
7817
7818
7819
7820
7821
7822
7823
7824
7825
7826
7827
7828
7829
7830
7831
7832
7833
7834
7835
7836
7837
7838
7839
7840
7841
7842
7843
7844
7845
7846
7847
7848
7849
7850
7851
7852
7853
7854
7855
7856
7857
7858
7859
7860
7861
7862
7863
7864
7865
7866
7867
7868
7869
7870
7871
7872
7873
7874
7875
7876
7877
7878
7879
7880
7881
7882
7883
7884
7885
7886
7887
7888
7889
7890
7891
7892
7893
7894
7895
7896
7897
7898
7899
7900
7901
7902
7903
7904
7905
7906
7907
7908
7909
7910
7911
7912
7913
7914
7915
7916
7917
7918
7919
7920
7921
7922
7923
7924
7925
7926
7927
7928
7929
7930
7931
7932
7933
7934
7935
7936
7937
7938
7939
7940
7941
7942
7943
7944
7945
7946
7947
7948
7949
7950
7951
7952
7953
7954
7955
7956
7957
7958
7959
7960
7961
7962
7963
7964
7965
7966
7967
7968
7969
7970
7971
7972
7973
7974
7975
7976
7977
7978
7979
7980
7981
7982
7983
7984
7985
7986
7987
7988
7989
7990
7991
7992
7993
7994
7995
7996
7997
7998
7999
8000
8001
8002
8003
8004
8005
8006
8007
8008
8009
8010
8011
8012
8013
8014
8015
8016
8017
8018
8019
8020
8021
8022
8023
8024
8025
8026
8027
8028
8029
8030
8031
8032
8033
8034
8035
8036
8037
8038
8039
8040
8041
8042
8043
8044
8045
8046
8047
8048
8049
8050
8051
8052
8053
8054
8055
8056
8057
8058
8059
8060
8061
8062
8063
8064
8065
8066
8067
8068
8069
8070
8071
8072
8073
8074
8075
8076
8077
8078
8079
8080
8081
8082
8083
8084
8085
8086
8087
8088
8089
8090
8091
8092
8093
8094
8095
8096
8097
8098
8099
8100
8101
8102
8103
8104
8105
8106
8107
8108
8109
8110
8111
8112
8113
8114
8115
8116
8117
8118
8119
8120
8121
8122
8123
8124
8125
8126
8127
8128
8129
8130
8131
8132
8133
8134
8135
8136
8137
8138
8139
8140
8141
8142
8143
8144
8145
8146
8147
8148
8149
8150
8151
8152
8153
8154
8155
8156
8157
8158
8159
8160
8161
8162
8163
8164
8165
8166
8167
8168
8169
8170
8171
8172
8173
8174
8175
8176
8177
8178
8179
8180
8181
8182
8183
8184
8185
8186
8187
8188
8189
8190
8191
8192
8193
8194
8195
8196
8197
8198
8199
8200
8201
8202
8203
8204
8205
8206
8207
8208
8209
8210
8211
8212
8213
8214
8215
8216
8217
8218
8219
8220
8221
8222
8223
8224
8225
8226
8227
8228
8229
8230
8231
8232
8233
8234
8235
8236
8237
8238
8239
8240
8241
8242
8243
8244
8245
8246
8247
8248
8249
8250
8251
8252
8253
8254
8255
8256
8257
8258
8259
8260
8261
8262
8263
8264
8265
8266
8267
8268
8269
8270
8271
8272
8273
8274
8275
8276
8277
8278
8279
8280
8281
8282
8283
8284
8285
8286
8287
8288
8289
8290
8291
8292
8293
8294
8295
8296
8297
8298
8299
8300
8301
8302
8303
8304
8305
8306
8307
8308
8309
8310
8311
8312
8313
8314
8315
8316
8317
8318
8319
8320
8321
8322
8323
8324
8325
8326
8327
8328
8329
8330
8331
8332
8333
8334
8335
8336
8337
8338
8339
8340
8341
8342
8343
8344
8345
8346
8347
8348
8349
8350
8351
8352
8353
8354
8355
8356
8357
8358
8359
8360
8361
8362
8363
8364
8365
8366
8367
8368
8369
8370
8371
8372
8373
8374
8375
8376
8377
8378
8379
8380
8381
8382
8383
8384
8385
8386
8387
8388
8389
8390
8391
8392
8393
8394
8395
8396
8397
8398
8399
8400
8401
8402
8403
8404
8405
8406
8407
8408
8409
8410
8411
8412
8413
8414
8415
8416
8417
8418
8419
8420
8421
8422
8423
8424
8425
8426
8427
8428
8429
8430
8431
8432
8433
8434
8435
8436
8437
8438
8439
8440
8441
8442
8443
8444
8445
8446
8447
8448
8449
8450
8451
8452
8453
8454
8455
8456
8457
8458
8459
8460
8461
8462
8463
8464
8465
8466
8467
8468
8469
8470
8471
8472
8473
8474
8475
8476
8477
8478
8479
8480
8481
8482
8483
8484
8485
8486
8487
8488
8489
8490
8491
8492
8493
8494
8495
8496
8497
8498
8499
8500
8501
8502
8503
8504
8505
8506
8507
8508
8509
8510
8511
8512
8513
8514
8515
8516
8517
8518
8519
8520
8521
8522
8523
8524
8525
8526
8527
8528
8529
8530
8531
8532
8533
8534
8535
8536
8537
8538
8539
8540
8541
8542
8543
8544
8545
8546
8547
8548
8549
8550
8551
8552
8553
8554
8555
8556
8557
8558
8559
8560
8561
8562
8563
8564
8565
8566
8567
8568
8569
8570
8571
8572
8573
8574
8575
8576
8577
8578
8579
8580
8581
8582
8583
8584
8585
8586
8587
8588
8589
8590
8591
8592
8593
8594
8595
8596
8597
8598
8599
8600
8601
8602
8603
8604
8605
8606
8607
8608
8609
8610
8611
8612
8613
8614
8615
8616
8617
8618
8619
8620
8621
8622
8623
8624
8625
8626
8627
8628
8629
8630
8631
8632
8633
8634
8635
8636
8637
8638
8639
8640
8641
8642
8643
8644
8645
8646
8647
8648
8649
8650
8651
8652
8653
8654
8655
8656
8657
8658
8659
8660
8661
8662
8663
8664
8665
8666
8667
8668
8669
8670
8671
8672
8673
8674
8675
8676
8677
8678
8679
8680
8681
8682
8683
8684
8685
8686
8687
8688
8689
8690
8691
8692
8693
8694
8695
8696
8697
8698
8699
8700
8701
8702
8703
8704
8705
8706
8707
8708
8709
8710
8711
8712
8713
8714
8715
8716
8717
8718
8719
8720
8721
8722
8723
8724
8725
8726
8727
8728
8729
8730
8731
8732
8733
8734
8735
8736
8737
8738
8739
8740
8741
8742
8743
8744
8745
8746
8747
8748
8749
8750
8751
8752
8753
8754
8755
8756
8757
8758
8759
8760
8761
8762
8763
8764
8765
8766
8767
8768
8769
8770
8771
8772
8773
8774
8775
8776
8777
8778
8779
8780
8781
8782
8783
8784
8785
8786
8787
8788
8789
8790
8791
8792
8793
8794
8795
8796
8797
8798
8799
8800
8801
8802
8803
8804
8805
8806
8807
8808
8809
8810
8811
8812
8813
8814
8815
8816
8817
8818
8819
8820
8821
8822
8823
8824
8825
8826
8827
8828
8829
8830
8831
8832
8833
8834
8835
8836
8837
8838
8839
8840
8841
8842
8843
8844
8845
8846
8847
8848
8849
8850
8851
8852
8853
8854
8855
8856
8857
8858
8859
8860
8861
8862
8863
8864
8865
8866
8867
8868
8869
8870
8871
8872
8873
8874
8875
8876
8877
8878
8879
8880
8881
8882
8883
8884
8885
8886
8887
8888
8889
8890
8891
8892
8893
8894
8895
8896
8897
8898
8899
8900
8901
8902
8903
8904
8905
8906
8907
8908
8909
8910
8911
8912
8913
8914
8915
8916
8917
8918
8919
8920
8921
8922
8923
8924
8925
8926
8927
8928
8929
8930
8931
8932
8933
8934
8935
8936
8937
8938
8939
8940
8941
8942
8943
8944
8945
8946
8947
8948
8949
8950
8951
8952
8953
8954
8955
8956
8957
8958
8959
8960
8961
8962
8963
8964
8965
8966
8967
8968
8969
8970
8971
8972
8973
8974
8975
8976
8977
8978
8979
8980
8981
8982
8983
8984
8985
8986
8987
8988
8989
8990
8991
8992
8993
8994
8995
8996
8997
8998
8999
9000
9001
9002
9003
9004
9005
9006
9007
9008
9009
9010
9011
9012
9013
9014
9015
9016
9017
9018
9019
9020
9021
9022
9023
9024
9025
9026
9027
9028
9029
9030
9031
9032
9033
9034
9035
9036
9037
9038
9039
9040
9041
9042
9043
9044
9045
9046
9047
9048
9049
9050
9051
9052
9053
9054
9055
9056
9057
9058
9059
9060
9061
9062
9063
9064
9065
9066
9067
9068
9069
9070
9071
9072
9073
9074
9075
9076
9077
9078
9079
9080
9081
9082
9083
9084
9085
9086
9087
9088
9089
9090
9091
9092
9093
9094
9095
9096
9097
9098
9099
9100
9101
9102
9103
9104
9105
9106
9107
9108
9109
9110
9111
9112
9113
9114
9115
9116
9117
9118
9119
9120
9121
9122
9123
9124
9125
9126
9127
9128
9129
9130
9131
9132
9133
9134
9135
9136
9137
9138
9139
9140
9141
9142
9143
9144
9145
9146
9147
9148
9149
9150
9151
9152
9153
9154
9155
9156
9157
9158
9159
9160
9161
9162
9163
9164
9165
9166
9167
9168
9169
9170
9171
9172
9173
9174
9175
9176
9177
9178
9179
9180
9181
9182
9183
9184
9185
9186
9187
9188
9189
9190
9191
9192
9193
9194
9195
9196
9197
9198
9199
9200
9201
9202
9203
9204
9205
9206
9207
9208
9209
9210
9211
9212
9213
9214
9215
9216
9217
9218
9219
9220
9221
9222
9223
9224
9225
9226
9227
9228
9229
9230
9231
9232
9233
9234
9235
9236
9237
9238
9239
9240
9241
9242
9243
9244
9245
9246
9247
9248
9249
9250
9251
9252
9253
9254
9255
9256
9257
9258
9259
9260
9261
9262
9263
9264
9265
9266
9267
9268
9269
9270
9271
9272
9273
9274
9275
9276
9277
9278
9279
9280
9281
9282
9283
9284
9285
9286
9287
9288
9289
9290
9291
9292
9293
9294
9295
9296
9297
9298
9299
9300
9301
9302
9303
9304
9305
9306
9307
9308
9309
9310
9311
9312
9313
9314
9315
9316
9317
9318
9319
9320
9321
9322
9323
9324
9325
9326
9327
9328
9329
9330
9331
9332
9333
9334
9335
9336
9337
9338
9339
9340
9341
9342
9343
9344
9345
9346
9347
9348
9349
9350
9351
9352
9353
9354
9355
9356
9357
9358
9359
9360
9361
9362
9363
9364
9365
9366
9367
9368
9369
9370
9371
9372
9373
9374
9375
9376
9377
9378
9379
9380
9381
9382
9383
9384
9385
9386
9387
9388
9389
9390
9391
9392
9393
9394
9395
9396
9397
9398
9399
9400
9401
9402
9403
9404
9405
9406
9407
9408
9409
9410
9411
9412
9413
9414
9415
9416
9417
9418
9419
9420
9421
9422
9423
9424
9425
9426
9427
9428
9429
9430
9431
9432
9433
9434
9435
9436
9437
9438
9439
9440
9441
9442
9443
9444
9445
9446
9447
9448
9449
9450
9451
9452
9453
9454
9455
9456
9457
9458
9459
9460
9461
9462
9463
9464
9465
9466
9467
9468
9469
9470
9471
9472
9473
9474
9475
9476
9477
9478
9479
9480
9481
9482
9483
9484
9485
9486
9487
9488
9489
9490
9491
9492
9493
9494
9495
9496
9497
9498
9499
9500
9501
9502
9503
9504
9505
9506
9507
9508
9509
9510
9511
9512
9513
9514
9515
9516
9517
9518
9519
9520
9521
9522
9523
9524
9525
9526
9527
9528
9529
9530
9531
9532
9533
9534
9535
9536
9537
9538
9539
9540
9541
9542
9543
9544
9545
9546
9547
9548
9549
9550
9551
9552
9553
9554
9555
9556
9557
9558
9559
9560
9561
9562
9563
9564
9565
9566
9567
9568
9569
9570
9571
9572
9573
9574
9575
9576
9577
9578
9579
9580
9581
9582
9583
9584
9585
9586
9587
9588
9589
9590
9591
9592
9593
9594
9595
9596
9597
9598
9599
9600
9601
9602
9603
9604
9605
9606
9607
9608
9609
9610
9611
9612
9613
9614
9615
9616
9617
9618
9619
9620
9621
9622
9623
9624
9625
9626
9627
9628
9629
9630
9631
9632
9633
9634
9635
9636
9637
9638
9639
9640
9641
9642
9643
9644
9645
9646
9647
9648
9649
9650
9651
9652
9653
9654
9655
9656
9657
9658
9659
9660
9661
9662
9663
9664
9665
9666
9667
9668
9669
9670
9671
9672
9673
9674
9675
9676
9677
9678
9679
9680
9681
9682
9683
9684
9685
9686
9687
9688
9689
9690
9691
9692
9693
9694
9695
9696
9697
9698
9699
9700
9701
9702
9703
9704
9705
9706
9707
9708
9709
9710
9711
9712
9713
9714
9715
9716
9717
9718
9719
9720
9721
9722
9723
9724
9725
9726
9727
9728
9729
9730
9731
9732
9733
9734
9735
9736
9737
9738
9739
9740
9741
9742
9743
9744
9745
9746
9747
9748
9749
9750
9751
9752
9753
9754
9755
9756
9757
9758
9759
9760
9761
9762
9763
9764
9765
9766
9767
9768
9769
9770
9771
9772
9773
9774
9775
9776
9777
9778
9779
9780
9781
9782
9783
9784
9785
9786
9787
9788
9789
9790
9791
9792
9793
9794
9795
9796
9797
9798
9799
9800
9801
9802
9803
9804
9805
9806
9807
9808
9809
9810
9811
9812
9813
9814
9815
9816
9817
9818
9819
9820
9821
9822
9823
9824
9825
9826
9827
9828
9829
9830
9831
9832
9833
9834
9835
9836
9837
9838
9839
9840
9841
9842
9843
9844
9845
9846
9847
9848
9849
9850
9851
9852
9853
9854
9855
9856
9857
9858
9859
9860
9861
9862
9863
9864
9865
9866
9867
9868
9869
9870
9871
9872
9873
9874
9875
9876
9877
9878
9879
9880
9881
9882
9883
9884
9885
9886
9887
9888
9889
9890
9891
9892
9893
9894
9895
9896
9897
9898
9899
9900
9901
9902
9903
9904
9905
9906
9907
9908
9909
9910
9911
9912
9913
9914
9915
9916
9917
9918
9919
9920
9921
9922
9923
9924
9925
9926
9927
9928
9929
9930
9931
9932
9933
9934
9935
9936
9937
9938
9939
9940
9941
9942
9943
9944
9945
9946
9947
9948
9949
9950
9951
9952
9953
9954
9955
9956
9957
9958
9959
9960
9961
9962
9963
9964
9965
9966
9967
9968
9969
9970
9971
9972
9973
9974
9975
9976
9977
9978
9979
9980
9981
9982
9983
9984
9985
9986
9987
9988
9989
9990
9991
9992
9993
9994
9995
9996
9997
9998
9999
10000
10001
10002
10003
10004
10005
10006
10007
10008
10009
10010
10011
10012
10013
10014
10015
10016
10017
10018
10019
10020
10021
10022
10023
10024
10025
10026
10027
10028
10029
10030
10031
10032
10033
10034
10035
10036
10037
10038
10039
10040
10041
10042
10043
10044
10045
10046
10047
10048
10049
10050
10051
10052
10053
10054
10055
10056
10057
10058
10059
10060
10061
10062
10063
10064
10065
10066
10067
10068
10069
10070
10071
10072
10073
10074
10075
10076
10077
10078
10079
10080
10081
10082
10083
10084
10085
10086
10087
10088
10089
10090
10091
10092
10093
10094
10095
10096
10097
10098
10099
10100
10101
10102
10103
10104
10105
10106
10107
10108
10109
10110
10111
10112
10113
10114
10115
10116
10117
10118
10119
10120
10121
10122
10123
10124
10125
10126
10127
10128
10129
10130
10131
10132
10133
10134
10135
10136
10137
10138
10139
10140
10141
10142
10143
10144
10145
10146
10147
10148
10149
10150
10151
10152
10153
10154
10155
10156
10157
10158
10159
10160
10161
10162
10163
10164
10165
10166
10167
10168
10169
10170
10171
10172
10173
10174
10175
10176
10177
10178
10179
10180
10181
10182
10183
10184
10185
10186
10187
10188
10189
10190
10191
10192
10193
10194
10195
10196
10197
10198
10199
10200
10201
10202
10203
10204
10205
10206
10207
10208
10209
10210
10211
10212
10213
10214
10215
10216
10217
10218
10219
10220
10221
10222
10223
10224
10225
10226
10227
10228
10229
10230
10231
10232
10233
10234
10235
10236
10237
10238
10239
10240
10241
10242
10243
10244
10245
10246
10247
10248
10249
10250
10251
10252
10253
10254
10255
10256
10257
10258
10259
10260
10261
10262
10263
10264
10265
10266
10267
10268
10269
10270
10271
10272
10273
10274
10275
10276
10277
10278
10279
10280
10281
10282
10283
10284
10285
10286
10287
10288
10289
10290
10291
10292
10293
10294
10295
10296
10297
10298
10299
10300
10301
10302
//! Interactive mode TUI components
//!
//! Provides high-level interactive components for the oxi terminal interface:
//! - Session selector (navigate/switch/create/delete sessions)
//! - Model selector (choose AI model grouped by provider)
//! - Footer (status bar with model, session, tokens, cost)
//! - Login dialog (API key entry with provider selection)
//! - Diff viewer (show edit diffs with color highlighting)
//! - Bash execution display (streaming output, timer, cancel)
//! - Assistant message rendering (thinking blocks, tool calls, markdown)
//! - Tool execution rendering (args, results, images, status)
//! - Summary message rendering (compaction, branch)

use serde::{Deserialize, Serialize};

use rand::RngCore;

/// Content block types for assistant messages
#[derive(Debug, Clone)]
pub enum AssistantContentBlock {
    /// Text content with optional markdown
    Text { text: String },
    /// Thinking/reasoning block (collapsible)
    Thinking { thinking: String },
    /// Tool call invocation
    ToolCall {
        id: String,
        name: String,
        arguments: String,
    },
}

/// Assistant message data structure
#[derive(Debug, Clone)]
pub struct AssistantMessage {
    pub content: Vec<AssistantContentBlock>,
    pub stop_reason: Option<StopReason>,
    pub error_message: Option<String>,
}

/// Why the assistant message stopped
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum StopReason {
    EndTurn,
    MaxTokens,
    StopSequence,
    Aborted,
    Error,
}

impl StopReason {
    pub fn as_str(&self) -> &'static str {
        match self {
            StopReason::EndTurn => "end_turn",
            StopReason::MaxTokens => "max_tokens",
            StopReason::StopSequence => "stop_sequence",
            StopReason::Aborted => "aborted",
            StopReason::Error => "error",
        }
    }
}

impl AssistantMessage {
    pub fn new() -> Self {
        Self {
            content: Vec::new(),
            stop_reason: None,
            error_message: None,
        }
    }

    /// Add a text block
    pub fn add_text(&mut self, text: impl Into<String>) {
        self.content
            .push(AssistantContentBlock::Text { text: text.into() });
    }

    /// Add a thinking block
    pub fn add_thinking(&mut self, thinking: impl Into<String>) {
        self.content.push(AssistantContentBlock::Thinking {
            thinking: thinking.into(),
        });
    }

    /// Add a tool call block
    pub fn add_tool_call(
        &mut self,
        id: impl Into<String>,
        name: impl Into<String>,
        arguments: impl Into<String>,
    ) {
        self.content.push(AssistantContentBlock::ToolCall {
            id: id.into(),
            name: name.into(),
            arguments: arguments.into(),
        });
    }

    /// Check if message has any visible content
    pub fn has_visible_content(&self) -> bool {
        self.content.iter().any(|c| match c {
            AssistantContentBlock::Text { text } => !text.trim().is_empty(),
            AssistantContentBlock::Thinking { thinking } => !thinking.trim().is_empty(),
            AssistantContentBlock::ToolCall { .. } => false,
        })
    }

    /// Check if message has tool calls
    pub fn has_tool_calls(&self) -> bool {
        self.content
            .iter()
            .any(|c| matches!(c, AssistantContentBlock::ToolCall { .. }))
    }
}

impl Default for AssistantMessage {
    fn default() -> Self {
        Self::new()
    }
}

/// Options for rendering assistant messages
#[derive(Debug, Clone)]
pub struct AssistantMessageRenderOptions {
    /// Hide thinking blocks and show a label instead
    pub hide_thinking: bool,
    /// Label to show when thinking is hidden
    pub hidden_thinking_label: String,
    /// Use OSC 133 prompt escape codes for terminal integration
    pub use_osc133: bool,
}

impl Default for AssistantMessageRenderOptions {
    fn default() -> Self {
        Self {
            hide_thinking: false,
            hidden_thinking_label: "Thinking...".to_string(),
            use_osc133: false,
        }
    }
}

/// Assistant message renderer
pub struct AssistantMessageRenderer {
    options: AssistantMessageRenderOptions,
}

impl AssistantMessageRenderer {
    pub fn new(options: AssistantMessageRenderOptions) -> Self {
        Self { options }
    }

    /// Set hide thinking option
    pub fn with_hide_thinking(mut self, hide: bool) -> Self {
        self.options.hide_thinking = hide;
        self
    }

    /// Set hidden thinking label
    pub fn with_hidden_thinking_label(mut self, label: impl Into<String>) -> Self {
        self.options.hidden_thinking_label = label.into();
        self
    }

    /// Enable OSC 133 escape codes for terminal integration
    pub fn with_osc133(mut self, enable: bool) -> Self {
        self.options.use_osc133 = enable;
        self
    }

    /// Render an assistant message to a string
    pub fn render(&self, message: &AssistantMessage) -> String {
        let mut output = String::new();

        // OSC 133 zone start
        if self.options.use_osc133 {
            output.push_str("\x1b]133;A\x07");
        }

        let mut has_visible_content = false;
        let visible_count = message
            .content
            .iter()
            .filter(|c| match c {
                AssistantContentBlock::Text { text } => !text.trim().is_empty(),
                AssistantContentBlock::Thinking { thinking } => !thinking.trim().is_empty(),
                AssistantContentBlock::ToolCall { .. } => false,
            })
            .count();

        let mut visible_idx = 0;

        for block in &message.content {
            match block {
                AssistantContentBlock::Text { text } if !text.trim().is_empty() => {
                    if has_visible_content {
                        output.push('\n');
                    }
                    visible_idx += 1;
                    has_visible_content = true;
                    output.push_str(&render_markdown(text.trim()));
                    if visible_idx < visible_count {
                        output.push('\n');
                    }
                }
                AssistantContentBlock::Thinking { thinking } if !thinking.trim().is_empty() => {
                    if has_visible_content {
                        output.push('\n');
                    }
                    visible_idx += 1;
                    has_visible_content = true;

                    if self.options.hide_thinking {
                        // Show static thinking label (italic, dimmed)
                        output.push_str(&format!(
                            "\x1b[2m\x1b[3m{}\x1b[0m",
                            self.options.hidden_thinking_label
                        ));
                    } else {
                        // Show thinking content (italic, dimmed)
                        let rendered = render_markdown(thinking.trim());
                        output.push_str(&format!("\x1b[2m\x1b[3m{}\x1b[0m", rendered));
                    }

                    if visible_idx < visible_count {
                        output.push('\n');
                    }
                }
                _ => {}
            }
        }

        // Handle stop reasons (only if no tool calls)
        if !message.has_tool_calls() {
            if let Some(ref reason) = message.stop_reason {
                if has_visible_content {
                    output.push('\n');
                }
                match reason {
                    StopReason::Aborted => {
                        let msg = message
                            .error_message
                            .as_ref()
                            .filter(|m| *m != "Request was aborted")
                            .cloned()
                            .unwrap_or_else(|| "Operation aborted".to_string());
                        output.push_str(&format!("\x1b[31m{}\x1b[0m", msg));
                    }
                    StopReason::Error => {
                        let msg = message
                            .error_message
                            .as_ref()
                            .cloned()
                            .unwrap_or_else(|| "Unknown error".to_string());
                        output.push_str(&format!("\x1b[31mError: {}\x1b[0m", msg));
                    }
                    _ => {}
                }
            }
        }

        // OSC 133 zone end
        if self.options.use_osc133 {
            output.push_str("\x1b]133;B\x07\x1b]133;C\x07");
        }

        output
    }
}

impl Default for AssistantMessageRenderer {
    fn default() -> Self {
        Self::new(AssistantMessageRenderOptions::default())
    }
}

/// Simple markdown rendering (bold, italic, code)
fn render_markdown(text: &str) -> String {
    let mut output = String::new();
    let mut chars = text.chars().peekable();

    while let Some(c) = chars.next() {
        if c == '`' {
            // Inline code
            let mut code = String::new();
            while let Some(&next) = chars.peek() {
                if next == '`' {
                    chars.next();
                    // Check for code block
                    if chars.peek() == Some(&'`') {
                        chars.next();
                        // Triple backtick - code block
                        let mut block = String::new();
                        while let Some(ch) = chars.next() {
                            if ch == '`' {
                                if chars.clone().take(2).collect::<String>() == "``" {
                                    chars.nth(2);
                                    break;
                                }
                                block.push(ch);
                            } else {
                                block.push(ch);
                            }
                        }
                        output.push_str(&format!("\x1b[36m{}\x1b[0m", block.trim()));
                        break;
                    }
                    break;
                } else {
                    code.push(chars.next().unwrap());
                }
            }
            if !code.is_empty() {
                output.push_str(&format!("\x1b[33m{}\x1b[0m", code));
            }
        } else if c == '*' && chars.peek() == Some(&'*') {
            // Bold
            chars.next();
            let mut bold = String::new();
            while let Some(&next) = chars.peek() {
                if next == '*' && chars.clone().nth(1) == Some('*') {
                    chars.next();
                    chars.next();
                    break;
                }
                bold.push(chars.next().unwrap());
            }
            output.push_str(&format!("\x1b[1m{}\x1b[0m", bold));
        } else if c == '_' {
            // Italic
            let mut italic = String::new();
            while let Some(&next) = chars.peek() {
                if next == '_' {
                    chars.next();
                    break;
                }
                italic.push(chars.next().unwrap());
            }
            output.push_str(&format!("\x1b[3m{}\x1b[0m", italic));
        } else {
            output.push(c);
        }
    }

    output
}

/// Session info for display in session selector
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct SessionInfo {
    pub id: String,
    pub name: String,
    pub created_at: String,
    pub message_count: usize,
    pub model: Option<String>,
    pub parent_id: Option<String>,
}

/// Session selector state
#[derive(Debug, Clone)]
pub struct SessionSelector {
    pub sessions: Vec<SessionInfo>,
    pub selected_index: usize,
    pub filter: String,
    pub scroll_offset: usize,
    pub visible_height: usize,
}

impl SessionSelector {
    pub fn new(sessions: Vec<SessionInfo>) -> Self {
        Self {
            sessions,
            selected_index: 0,
            filter: String::new(),
            scroll_offset: 0,
            visible_height: 20,
        }
    }

    /// Get filtered sessions matching the current filter
    pub fn filtered_sessions(&self) -> Vec<&SessionInfo> {
        if self.filter.is_empty() {
            self.sessions.iter().collect()
        } else {
            let filter_lower = self.filter.to_lowercase();
            self.sessions
                .iter()
                .filter(|s| {
                    s.name.to_lowercase().contains(&filter_lower)
                        || s.id.to_lowercase().contains(&filter_lower)
                })
                .collect()
        }
    }

    /// Move selection up
    pub fn move_up(&mut self) {
        if self.selected_index > 0 {
            self.selected_index -= 1;
            self.adjust_scroll();
        }
    }

    /// Move selection down
    pub fn move_down(&mut self) {
        let max = self.filtered_sessions().len().saturating_sub(1);
        if self.selected_index < max {
            self.selected_index += 1;
            self.adjust_scroll();
        }
    }

    /// Get currently selected session
    pub fn selected(&self) -> Option<&SessionInfo> {
        self.filtered_sessions()
            .into_iter()
            .nth(self.selected_index)
    }

    /// Update filter text
    pub fn set_filter(&mut self, filter: String) {
        self.filter = filter;
        self.selected_index = 0;
        self.scroll_offset = 0;
    }

    fn adjust_scroll(&mut self) {
        if self.selected_index < self.scroll_offset {
            self.scroll_offset = self.selected_index;
        } else if self.selected_index >= self.scroll_offset + self.visible_height {
            self.scroll_offset = self.selected_index - self.visible_height + 1;
        }
    }

    /// Render the session selector as a string
    pub fn render(&self) -> String {
        let mut output = String::new();
        output.push_str(&format!("{}\n", "─".repeat(60)));
        output.push_str("Sessions (↑↓ navigate, Enter select, n new, d delete, / filter)\n");
        output.push_str(&format!("{}\n", "─".repeat(60)));

        if !self.filter.is_empty() {
            output.push_str(&format!("Filter: {}\n", self.filter));
        }

        let filtered: Vec<_> = self.filtered_sessions();
        for (i, session) in filtered.iter().enumerate() {
            let marker = if i == self.selected_index { "▶" } else { " " };
            let branch = if session.parent_id.is_some() {
                "├─ "
            } else {
                "  "
            };
            let name = if session.name.is_empty() {
                &session.id[..8.min(session.id.len())]
            } else {
                &session.name
            };
            output.push_str(&format!(
                "{} {}{:<30} {} msg:{} model:{}\n",
                marker,
                branch,
                name,
                &session.created_at[..10.min(session.created_at.len())],
                session.message_count,
                session.model.as_deref().unwrap_or("-"),
            ));
        }

        if filtered.is_empty() {
            output.push_str("  (no sessions)\n");
        }

        output
    }
}

/// Model info for model selector
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ModelInfo {
    pub id: String,
    pub name: String,
    pub provider: String,
    pub supports_vision: bool,
    pub supports_tools: bool,
    pub supports_thinking: bool,
    pub context_window: usize,
}

/// Model selector state
#[derive(Debug, Clone)]
pub struct ModelSelector {
    pub models: Vec<ModelInfo>,
    pub selected_index: usize,
    pub filter: String,
    pub grouped: bool,
}

impl ModelSelector {
    pub fn new(models: Vec<ModelInfo>) -> Self {
        let mut models = models;
        models.sort_by(|a, b| a.provider.cmp(&b.provider).then(a.name.cmp(&b.name)));
        Self {
            models,
            selected_index: 0,
            filter: String::new(),
            grouped: true,
        }
    }

    /// Get filtered models
    pub fn filtered_models(&self) -> Vec<&ModelInfo> {
        if self.filter.is_empty() {
            self.models.iter().collect()
        } else {
            let filter_lower = self.filter.to_lowercase();
            self.models
                .iter()
                .filter(|m| {
                    m.name.to_lowercase().contains(&filter_lower)
                        || m.id.to_lowercase().contains(&filter_lower)
                        || m.provider.to_lowercase().contains(&filter_lower)
                })
                .collect()
        }
    }

    /// Move selection up
    pub fn move_up(&mut self) {
        if self.selected_index > 0 {
            self.selected_index -= 1;
        }
    }

    /// Move selection down
    pub fn move_down(&mut self) {
        let max = self.filtered_models().len().saturating_sub(1);
        if self.selected_index < max {
            self.selected_index += 1;
        }
    }

    /// Get currently selected model
    pub fn selected(&self) -> Option<&ModelInfo> {
        self.filtered_models().into_iter().nth(self.selected_index)
    }

    /// Render the model selector
    pub fn render(&self) -> String {
        let mut output = String::new();
        output.push_str(&format!("{}\n", "─".repeat(60)));
        output.push_str("Select Model (↑↓ navigate, Enter select, / filter)\n");
        output.push_str(&format!("{}\n", "─".repeat(60)));

        let filtered: Vec<_> = self.filtered_models();
        let mut last_provider = String::new();

        for (i, model) in filtered.iter().enumerate() {
            // Provider group header
            if self.grouped && model.provider != last_provider {
                last_provider = model.provider.clone();
                output.push_str(&format!("\n  {}\n", model.provider.to_uppercase()));
            }

            let marker = if i == self.selected_index { "▶" } else { " " };
            let vision = if model.supports_vision { "👁" } else { " " };
            let tools = if model.supports_tools { "🔧" } else { " " };
            let thinking = if model.supports_thinking { "💭" } else { " " };
            let ctx = format_bytes(model.context_window);

            output.push_str(&format!(
                " {} {} {}{}{} {:<30} ctx:{}\n",
                marker, model.id, vision, tools, thinking, model.name, ctx,
            ));
        }

        output
    }
}

/// Footer status bar data
#[derive(Debug, Clone, Default)]
pub struct FooterData {
    pub model_name: String,
    pub session_name: String,
    pub provider_name: String,
    pub input_tokens: usize,
    pub output_tokens: usize,
    pub total_cost: f64,
    pub is_thinking: bool,
    pub elapsed_seconds: Option<u64>,
}

impl FooterData {
    /// Render the footer as a single-line status bar
    pub fn render(&self, width: usize) -> String {
        let thinking = if self.is_thinking { "⏳" } else { "✓" };
        let tokens = if self.input_tokens > 0 || self.output_tokens > 0 {
            format!("tok:{}+{}", self.input_tokens, self.output_tokens)
        } else {
            String::new()
        };
        let cost = if self.total_cost > 0.0 {
            format!("${:.4}", self.total_cost)
        } else {
            String::new()
        };
        let elapsed = self
            .elapsed_seconds
            .map(|s| format!("{}m{}s", s / 60, s % 60))
            .unwrap_or_default();

        let left = format!("{} {} @ {}", thinking, self.model_name, self.provider_name);
        let right = format!("{} {} {}", tokens, cost, elapsed);

        let session_part = if !self.session_name.is_empty() {
            format!(" │ {}", self.session_name)
        } else {
            String::new()
        };

        // Pad to width
        let content_len = left.len() + session_part.len() + right.len() + 2;
        if content_len < width {
            let padding = width - content_len;
            format!(
                "{}{}{:>width$}",
                left,
                session_part,
                right,
                width = padding + right.len()
            )
        } else {
            format!("{}{} {}", left, session_part, right)
        }
    }
}

/// Login dialog state
#[derive(Debug, Clone)]
pub struct LoginDialog {
    pub providers: Vec<String>,
    pub selected_provider_index: usize,
    pub api_key: String,
    pub cursor_pos: usize,
    pub error_message: Option<String>,
    pub is_masked: bool,
    /// OAuth-specific state
    pub oauth_state: Option<OAuthState>,
    /// Callback URL being waited for
    pub pending_auth_url: Option<String>,
}

/// OAuth provider configuration
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum OAuthProvider {
    Anthropic,
    OpenAI,
    GitHub,
    Google,
    Azure,
    /// Custom/provider-specific OAuth
    Custom {
        id: String,
        name: String,
    },
}

impl OAuthProvider {
    /// Get the provider ID string
    pub fn id(&self) -> &str {
        match self {
            OAuthProvider::Anthropic => "anthropic",
            OAuthProvider::OpenAI => "openai",
            OAuthProvider::GitHub => "github",
            OAuthProvider::Google => "google",
            OAuthProvider::Azure => "azure",
            OAuthProvider::Custom { id, .. } => id,
        }
    }

    /// Get the display name
    pub fn name(&self) -> &str {
        match self {
            OAuthProvider::Anthropic => "Anthropic",
            OAuthProvider::OpenAI => "OpenAI",
            OAuthProvider::GitHub => "GitHub",
            OAuthProvider::Google => "Google",
            OAuthProvider::Azure => "Azure",
            OAuthProvider::Custom { name, .. } => name,
        }
    }

    /// Get the default redirect port for this provider
    pub fn default_port(&self) -> u16 {
        match self {
            OAuthProvider::Anthropic => 8787,
            OAuthProvider::OpenAI => 8788,
            OAuthProvider::GitHub => 8789,
            OAuthProvider::Google => 8790,
            OAuthProvider::Azure => 8791,
            OAuthProvider::Custom { .. } => 8792,
        }
    }

    /// Parse provider from ID string
    pub fn from_id(id: &str) -> Option<Self> {
        match id.to_lowercase().as_str() {
            "anthropic" => Some(OAuthProvider::Anthropic),
            "openai" => Some(OAuthProvider::OpenAI),
            "github" | "github-copilot" => Some(OAuthProvider::GitHub),
            "google" => Some(OAuthProvider::Google),
            "azure" => Some(OAuthProvider::Azure),
            _ => None,
        }
    }
}

/// Internal OAuth state for the login flow
#[derive(Debug, Clone)]
pub struct OAuthState {
    pub provider: OAuthProvider,
    pub code_verifier: String,
    pub state: String,
    pub authorization_url: String,
    pub callback_port: u16,
}

/// Login state machine states
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum LoginState {
    /// Initial state - showing provider selection
    ProviderSelection,
    /// Waiting for user to enter API key
    ApiKey,
    /// Showing the OAuth authorization URL
    WaitingForUrl,
    /// Waiting for browser callback
    WaitingForCallback,
    /// Showing manual code input prompt
    ManualInput,
    /// Polling for device flow completion
    Polling,
    /// Authentication successful
    Success,
    /// Authentication failed with error message
    Error(String),
}

impl Default for LoginState {
    fn default() -> Self {
        LoginState::ProviderSelection
    }
}

impl LoginDialog {
    pub fn new(providers: Vec<String>) -> Self {
        Self {
            providers,
            selected_provider_index: 0,
            api_key: String::new(),
            cursor_pos: 0,
            error_message: None,
            is_masked: true,
            oauth_state: None,
            pending_auth_url: None,
        }
    }

    /// Create a new login dialog with OAuth support
    pub fn new_with_oauth() -> Self {
        Self::new(vec![
            "anthropic".to_string(),
            "openai".to_string(),
            "github".to_string(),
        ])
    }

    /// Get the current login state
    pub fn login_state(&self) -> LoginState {
        if self.error_message.is_some() {
            return LoginState::Error(self.error_message.clone().unwrap());
        }
        if self.oauth_state.is_some() {
            if self.pending_auth_url.is_some() {
                return LoginState::WaitingForCallback;
            }
            return LoginState::WaitingForUrl;
        }
        LoginState::ApiKey
    }

    /// Start an OAuth flow for the selected provider
    /// Returns the authorization URL to display
    pub fn start_oauth_flow(&mut self, provider: OAuthProvider) -> Result<String, String> {
        let port = provider.default_port();
        let code_verifier = generate_code_verifier();
        let state = generate_state_token();

        // Build authorization URL based on provider
        let auth_url = match &provider {
            OAuthProvider::Anthropic => {
                format!(
                    "https://auth.anthropic.com/oauth/authorize?response_type=code&client_id={}&redirect_uri=http%3A%2F%2Flocalhost%3A{}&code_challenge={}&code_challenge_method=S256&state={}",
                    "anthropic-oauth-client",
                    port,
                    derive_code_challenge_sync(&code_verifier),
                    state
                )
            }
            OAuthProvider::OpenAI => {
                format!(
                    "https://auth.openai.com/authorize?response_type=code&client_id={}&redirect_uri=http%3A%2F%2Flocalhost%3A{}&code_challenge={}&code_challenge_method=S256&state={}",
                    "openai-oauth-client",
                    port,
                    derive_code_challenge_sync(&code_verifier),
                    state
                )
            }
            OAuthProvider::GitHub => {
                // GitHub uses device flow, not authorization code
                format!(
                    "https://github.com/login/device/code?client_id={}&scope=read:user%20user:email",
                    "Iv1.placeholder_client_id"
                )
            }
            _ => {
                return Err(format!(
                    "OAuth not supported for provider: {}",
                    provider.name()
                ));
            }
        };

        let oauth_state = OAuthState {
            provider,
            code_verifier,
            state,
            authorization_url: auth_url.clone(),
            callback_port: port,
        };

        self.oauth_state = Some(oauth_state);
        self.pending_auth_url = Some(auth_url.clone());
        Ok(auth_url)
    }

    /// Open the authorization URL in the default browser
    pub fn open_auth_url(&self, url: &str) -> Result<(), String> {
        crate::oauth_server::open_browser(url)
            .map(|_child| ())
            .map_err(|e| format!("Failed to open browser: {}", e))
    }

    /// Start the OAuth callback server
    pub fn start_callback_server(
        port: u16,
    ) -> Result<crate::oauth_server::OAuthCallbackServer, String> {
        let server = crate::oauth_server::OAuthCallbackServer::new(port);
        Ok(server)
    }

    /// Handle the OAuth callback with code and state
    pub fn handle_oauth_callback(&mut self, code: String, state: String) -> Result<(), String> {
        if let Some(ref oauth_state) = self.oauth_state {
            // Verify state matches
            if oauth_state.state != state {
                return Err("State mismatch - possible CSRF attack".to_string());
            }
            // Store code for exchange
            self.api_key = code;
            self.pending_auth_url = None;
            Ok(())
        } else {
            Err("No OAuth flow in progress".to_string())
        }
    }

    /// Show manual code input interface
    pub fn show_manual_code_input(&mut self, message: &str) {
        self.error_message = None;
        // The message indicates what to show
        if let Some(ref auth_url) = self.pending_auth_url {
            eprintln!("\n{}", message);
            eprintln!("Authorization URL: {}", auth_url);
            eprintln!("Paste the code from the redirect URL here:\n");
        }
    }

    /// Parse a redirect URL to extract the authorization code
    pub fn parse_redirect_url(url: &str) -> Option<(String, String)> {
        // Parse URL like http://localhost:8787/callback?code=xxx&state=yyy
        if let Ok(parsed) = url::Url::parse(url) {
            let code = parsed
                .query_pairs()
                .find(|(k, _)| k == "code")
                .map(|(_, v)| v.to_string());
            let state = parsed
                .query_pairs()
                .find(|(k, _)| k == "state")
                .map(|(_, v)| v.to_string());
            if let (Some(code), Some(state)) = (code, state) {
                return Some((code, state));
            }
        }

        // Try simple parsing for just ?code=xxx&state=yyy
        let query = url.split('?').nth(1)?;
        let mut code = None;
        let mut state = None;
        for pair in query.split('&') {
            let (key, value) = pair.split_once('=')?;
            let decoded = urlencoding::decode(value).ok()?.to_string();
            match key {
                "code" => code = Some(decoded),
                "state" => state = Some(decoded),
                _ => {}
            }
        }
        Some((code?, state.unwrap_or_default()))
    }

    /// Complete the OAuth flow with the authorization code
    pub fn complete_oauth(&mut self, code: String) -> Result<(), String> {
        if let Some(ref _oauth_state) = self.oauth_state {
            // Store the code for exchange - the actual token exchange
            // would be done by the caller using oxi-ai's oauth module
            self.api_key = code;
            self.oauth_state = None;
            self.pending_auth_url = None;
            Ok(())
        } else {
            Err("No OAuth flow in progress".to_string())
        }
    }

    /// Cancel the current OAuth flow
    pub fn cancel_oauth(&mut self) {
        self.oauth_state = None;
        self.pending_auth_url = None;
        self.error_message = None;
    }

    /// Check if OAuth is available for a provider
    pub fn is_oauth_available(&self, provider: &str) -> bool {
        matches!(
            provider.to_lowercase().as_str(),
            "anthropic" | "openai" | "github" | "github-copilot"
        )
    }

    /// Get selected provider
    pub fn selected_provider(&self) -> Option<&str> {
        self.providers
            .get(self.selected_provider_index)
            .map(|s| s.as_str())
    }

    /// Input a character
    pub fn input_char(&mut self, c: char) {
        self.api_key.insert(self.cursor_pos, c);
        self.cursor_pos += 1;
        self.error_message = None;
    }

    /// Delete character before cursor
    pub fn backspace(&mut self) {
        if self.cursor_pos > 0 {
            self.cursor_pos -= 1;
            self.api_key.remove(self.cursor_pos);
            self.error_message = None;
        }
    }

    /// Cycle provider selection
    pub fn next_provider(&mut self) {
        if !self.providers.is_empty() {
            self.selected_provider_index =
                (self.selected_provider_index + 1) % self.providers.len();
            self.api_key.clear();
            self.cursor_pos = 0;
            self.error_message = None;
        }
    }

    /// Validate API key format (basic checks)
    pub fn validate(&self) -> Result<(), String> {
        if self.api_key.is_empty() {
            return Err("API key cannot be empty".to_string());
        }
        let provider = self.selected_provider().unwrap_or("");
        match provider {
            "anthropic" if !self.api_key.starts_with("sk-ant-") => {
                Err("Anthropic API keys start with 'sk-ant-'".to_string())
            }
            "openai" if !self.api_key.starts_with("sk-") => {
                Err("OpenAI API keys start with 'sk-'".to_string())
            }
            _ => Ok(()),
        }
    }

    /// Render the login dialog
    pub fn render(&self) -> String {
        let mut output = String::new();
        output.push_str(&format!("{}\n", "─".repeat(50)));
        output.push_str("  API Key Configuration\n");
        output.push_str(&format!("{}\n", "─".repeat(50)));

        // Provider tabs
        for (i, provider) in self.providers.iter().enumerate() {
            if i == self.selected_provider_index {
                output.push_str(&format!(" [{}] ", provider));
            } else {
                output.push_str(&format!("  {}  ", provider));
            }
        }
        output.push('\n');

        // API key input
        let display_key = if self.is_masked {
            "*".repeat(self.api_key.len())
        } else {
            self.api_key.clone()
        };
        output.push_str(&format!("\n  API Key: {}\n", display_key));

        // Error message
        if let Some(ref err) = self.error_message {
            output.push_str(&format!("  ⚠ {}\n", err));
        }

        output.push_str("\n  Tab: switch provider, Enter: save, Esc: cancel\n");
        output
    }
}

/// Diff line for the diff viewer
#[derive(Debug, Clone)]
pub enum DiffLine {
    Context {
        content: String,
        line_num: usize,
    },
    Added {
        content: String,
        line_num: usize,
    },
    Removed {
        content: String,
        line_num: usize,
    },
    Header {
        old_start: usize,
        old_count: usize,
        new_start: usize,
        new_count: usize,
    },
}

/// Diff viewer state
#[derive(Debug, Clone)]
pub struct DiffViewer {
    pub lines: Vec<DiffLine>,
    pub scroll_offset: usize,
    pub visible_height: usize,
    pub file_path: String,
    /// Enable word-level highlighting for changed parts
    pub word_diff: bool,
}

impl DiffViewer {
    pub fn new(file_path: String, diff_text: &str) -> Self {
        let lines = parse_diff_lines(diff_text);
        Self {
            lines,
            scroll_offset: 0,
            visible_height: 30,
            file_path,
            word_diff: true, // Enable word-level highlighting by default
        }
    }

    /// Create without word diff highlighting
    pub fn new_simple(file_path: String, diff_text: &str) -> Self {
        let lines = parse_diff_lines(diff_text);
        Self {
            lines,
            scroll_offset: 0,
            visible_height: 30,
            file_path,
            word_diff: false,
        }
    }

    /// Enable or disable word-level diff highlighting
    pub fn set_word_diff(&mut self, enabled: bool) {
        self.word_diff = enabled;
    }

    /// Render the diff viewer with optional word-level highlighting
    pub fn render(&self) -> String {
        let mut output = String::new();
        output.push_str(&format!("Diff: {}\n", self.file_path));
        output.push_str(&format!("{}\n", "─".repeat(60)));

        let visible: Vec<_> = self
            .lines
            .iter()
            .skip(self.scroll_offset)
            .take(self.visible_height)
            .collect();

        for line in &visible {
            match line {
                DiffLine::Header {
                    old_start,
                    old_count,
                    new_start,
                    new_count,
                } => {
                    output.push_str(&format!(
                        "@@ -{},{} +{},{} @@\n",
                        old_start, old_count, new_start, new_count
                    ));
                }
                DiffLine::Context { content, line_num } => {
                    output.push_str(&format!(" {:>4} {}\n", line_num, content));
                }
                DiffLine::Added { content, line_num } => {
                    if self.word_diff {
                        // Apply word-level highlighting for added lines
                        let highlighted = highlight_words_diff(content, true);
                        output.push_str(&format!("+{:>4} {}\n", line_num, highlighted));
                    } else {
                        output.push_str(&format!("+{:>4} {}\n", line_num, content));
                    }
                }
                DiffLine::Removed { content, line_num } => {
                    if self.word_diff {
                        // Apply word-level highlighting for removed lines
                        let highlighted = highlight_words_diff(content, false);
                        output.push_str(&format!("-{:>4} {}\n", line_num, highlighted));
                    } else {
                        output.push_str(&format!("-{:>4} {}\n", line_num, content));
                    }
                }
            }
        }

        let remaining = self
            .lines
            .len()
            .saturating_sub(self.scroll_offset + self.visible_height);
        if remaining > 0 {
            output.push_str(&format!("... {} more lines\n", remaining));
        }

        output
    }

    /// Scroll up
    pub fn scroll_up(&mut self, amount: usize) {
        self.scroll_offset = self.scroll_offset.saturating_sub(amount);
    }

    /// Scroll down
    pub fn scroll_down(&mut self, amount: usize) {
        let max = self.lines.len().saturating_sub(self.visible_height);
        self.scroll_offset = (self.scroll_offset + amount).min(max);
    }
}

/// Parse unified diff text into DiffLine structs
fn parse_diff_lines(diff: &str) -> Vec<DiffLine> {
    let mut lines = Vec::new();
    let mut old_line = 0;
    let mut new_line = 0;

    for raw_line in diff.lines() {
        if raw_line.starts_with("@@") {
            // Parse hunk header: @@ -old_start,old_count +new_start,new_count @@
            if let Some(header) = parse_hunk_header(raw_line) {
                old_line = header.0;
                new_line = header.2;
                lines.push(DiffLine::Header {
                    old_start: header.0,
                    old_count: header.1,
                    new_start: header.2,
                    new_count: header.3,
                });
            }
        } else if raw_line.starts_with('+') {
            let content = raw_line[1..].to_string();
            lines.push(DiffLine::Added {
                content,
                line_num: new_line,
            });
            new_line += 1;
        } else if raw_line.starts_with('-') {
            let content = raw_line[1..].to_string();
            lines.push(DiffLine::Removed {
                content,
                line_num: old_line,
            });
            old_line += 1;
        } else if raw_line.starts_with(' ') {
            let content = raw_line[1..].to_string();
            lines.push(DiffLine::Context {
                content,
                line_num: new_line,
            });
            old_line += 1;
            new_line += 1;
        }
    }

    lines
}

fn parse_hunk_header(line: &str) -> Option<(usize, usize, usize, usize)> {
    // @@ -old_start,old_count +new_start,new_count @@
    let text = line.trim_start_matches('@').trim_start_matches(' ');
    let text = text.trim_end_matches('@').trim_end_matches(' ');
    let parts: Vec<&str> = text.split_whitespace().collect();
    if parts.len() < 2 {
        return None;
    }

    let old: Vec<usize> = parts[0]
        .trim_start_matches('-')
        .split(',')
        .filter_map(|s| s.parse().ok())
        .collect();
    let new: Vec<usize> = parts
        .get(1)?
        .trim_start_matches('+')
        .split(',')
        .filter_map(|s| s.parse().ok())
        .collect();

    Some((
        *old.first()?,
        *old.get(1).unwrap_or(&1),
        *new.first()?,
        *new.get(1).unwrap_or(&1),
    ))
}

/// Highlight word-level changes in a diff line
/// Returns the content with ANSI color codes for changed words.
fn highlight_words_diff(content: &str, is_added: bool) -> String {
    use std::fmt::Write;

    // Split content into words while preserving spaces
    let words: Vec<&str> = content.split_whitespace().collect();
    let mut result = String::new();

    for (i, word) in words.iter().enumerate() {
        // Simple heuristic: short words (1-4 chars) that differ are likely changed
        let is_short_change = word.len() <= 4 && !word.chars().all(|c| c.is_alphanumeric());

        if is_short_change && i > 0 {
            // Highlight as changed
            let color = if is_added { "\x1b[32m" } else { "\x1b[31m" };
            write!(&mut result, "{}{}{}\x1b[0m ", color, word, "\x1b[0m").unwrap();
        } else {
            write!(&mut result, "{} ", word).unwrap();
        }
    }

    result.trim_end().to_string()
}

/// Tool result content block
#[derive(Debug, Clone)]
pub enum ToolContentBlock {
    /// Text output
    Text { text: String },
    /// Image data (base64 encoded)
    Image { data: String, mime_type: String },
}

/// Tool execution result
#[derive(Debug, Clone)]
pub struct ToolResult {
    pub content: Vec<ToolContentBlock>,
    pub is_error: bool,
    pub details: Option<serde_json::Value>,
}

impl ToolResult {
    pub fn new_text(text: impl Into<String>) -> Self {
        Self {
            content: vec![ToolContentBlock::Text { text: text.into() }],
            is_error: false,
            details: None,
        }
    }

    pub fn error(text: impl Into<String>) -> Self {
        Self {
            content: vec![ToolContentBlock::Text { text: text.into() }],
            is_error: true,
            details: None,
        }
    }

    /// Get text output (first text block or concatenated)
    pub fn get_text(&self) -> Option<String> {
        let texts: Vec<_> = self
            .content
            .iter()
            .filter_map(|b| match b {
                ToolContentBlock::Text { text } => Some(text.clone()),
                ToolContentBlock::Image { .. } => None,
            })
            .collect();

        if texts.is_empty() {
            None
        } else {
            Some(texts.join("\n"))
        }
    }

    /// Check if result contains images
    pub fn has_images(&self) -> bool {
        self.content
            .iter()
            .any(|b| matches!(b, ToolContentBlock::Image { .. }))
    }

    /// Count images in result
    pub fn image_count(&self) -> usize {
        self.content
            .iter()
            .filter(|b| matches!(b, ToolContentBlock::Image { .. }))
            .count()
    }
}

/// Tool execution state
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum ToolExecutionState {
    /// Tool call has been made, awaiting execution
    Pending,
    /// Tool is currently executing
    Running,
    /// Tool completed successfully
    Success,
    /// Tool completed with error
    Error,
}

/// Tool execution display state
#[derive(Debug, Clone)]
pub struct ToolExecution {
    pub tool_name: String,
    pub tool_call_id: String,
    pub arguments: serde_json::Value,
    pub state: ToolExecutionState,
    pub result: Option<ToolResult>,
    pub expanded: bool,
    pub show_images: bool,
}

impl ToolExecution {
    pub fn new(
        tool_name: impl Into<String>,
        tool_call_id: impl Into<String>,
        args: serde_json::Value,
    ) -> Self {
        Self {
            tool_name: tool_name.into(),
            tool_call_id: tool_call_id.into(),
            arguments: args,
            state: ToolExecutionState::Pending,
            result: None,
            expanded: false,
            show_images: true,
        }
    }

    /// Mark execution as started
    pub fn start(&mut self) {
        self.state = ToolExecutionState::Running;
    }

    /// Complete with result
    pub fn complete(&mut self, result: ToolResult) {
        self.state = if result.is_error {
            ToolExecutionState::Error
        } else {
            ToolExecutionState::Success
        };
        self.result = Some(result);
    }

    /// Set expanded/collapsed state
    pub fn set_expanded(&mut self, expanded: bool) {
        self.expanded = expanded;
    }

    /// Toggle expanded state
    pub fn toggle_expanded(&mut self) {
        self.expanded = !self.expanded;
    }

    /// Format arguments for display
    pub fn format_arguments(&self) -> String {
        if self.arguments.is_null() {
            String::new()
        } else if let Ok(obj) = serde_json::from_value::<serde_json::Value>(self.arguments.clone())
        {
            serde_json::to_string_pretty(&obj).unwrap_or_else(|_| self.arguments.to_string())
        } else {
            self.arguments.to_string()
        }
    }

    /// Render the tool execution
    pub fn render(&self) -> String {
        let mut output = String::new();

        // Determine colors based on state
        let (bg_color, fg_color, status_icon) = match self.state {
            ToolExecutionState::Pending => ("\x1b[48;5;240m", "\x1b[38;5;250m", "○"),
            ToolExecutionState::Running => ("\x1b[48;5;239m", "\x1b[38;5;250m", "◐"),
            ToolExecutionState::Success => ("\x1b[48;5;28m", "\x1b[38;5;255m", "●"),
            ToolExecutionState::Error => ("\x1b[48;5;196m", "\x1b[38;5;255m", "✗"),
        };
        let reset = "\x1b[0m";

        // Tool header
        output.push_str(&format!(
            "{} {} {}\x1b[1m{}\x1b[0m{}",
            bg_color, status_icon, fg_color, self.tool_name, reset
        ));
        output.push('\n');

        // Arguments (if expanded or small)
        let args_str = self.format_arguments();
        if self.expanded || args_str.len() < 200 {
            if !args_str.is_empty() {
                // Pretty print arguments
                output.push_str(&format!("{}Arguments:{} {}\n", fg_color, reset, args_str));
            }
        } else {
            // Show truncated args
            let truncated = if args_str.len() > 100 {
                format!("{}...\x1b[0m ({} chars)", &args_str[..100], args_str.len())
            } else {
                args_str
            };
            output.push_str(&format!("{}Arguments:{} {}\n", fg_color, reset, truncated));
        }

        // Result
        if let Some(ref result) = self.result {
            let result_fg = if result.is_error {
                "\x1b[31m"
            } else {
                fg_color
            };

            if self.expanded {
                // Show full result
                if let Some(text) = result.get_text() {
                    output.push_str(&format!("{}Output:{}\n{}", result_fg, reset, text));
                    if !text.ends_with('\n') {
                        output.push('\n');
                    }
                }

                // Show images count
                if result.has_images() && self.show_images {
                    output.push_str(&format!(
                        "{} ({} image{})",
                        fg_color,
                        result.image_count(),
                        if result.image_count() == 1 { "" } else { "s" }
                    ));
                }
            } else {
                // Show truncated result
                if let Some(text) = result.get_text() {
                    let truncated = truncate_text(&text, 500);
                    output.push_str(&format!("{}Output:{} {}", result_fg, reset, truncated));
                    if text.len() > 500 {
                        output.push_str(" (use → to expand)");
                    }
                    output.push('\n');
                } else if result.has_images() {
                    output.push_str(&format!(
                        "{}Output:{} {} image{}\n",
                        result_fg,
                        reset,
                        result.image_count(),
                        if result.image_count() == 1 { "" } else { "s" }
                    ));
                }
            }
        } else if self.state == ToolExecutionState::Running {
            output.push_str(&format!("{}Running...{}", fg_color, reset));
            output.push('\n');
        }

        output
    }
}

/// Truncate text to max length with ellipsis
fn truncate_text(text: &str, max_len: usize) -> String {
    if text.len() <= max_len {
        return text.to_string();
    }

    // Find a good break point
    let truncated = &text[..max_len];
    if let Some(last_newline) = truncated.rfind('\n') {
        format!("{}...", &truncated[..last_newline])
    } else if let Some(last_space) = truncated.rfind(' ') {
        format!("{}...", &truncated[..last_space])
    } else {
        format!("{}...", truncated)
    }
}

/// Tool execution renderer with advanced options
pub struct ToolExecutionRenderer {
    pub show_images: bool,
    pub max_image_width: usize,
}

impl ToolExecutionRenderer {
    pub fn new() -> Self {
        Self {
            show_images: true,
            max_image_width: 80,
        }
    }

    pub fn with_show_images(mut self, show: bool) -> Self {
        self.show_images = show;
        self
    }

    pub fn with_max_image_width(mut self, width: usize) -> Self {
        self.max_image_width = width;
        self
    }

    /// Render multiple tool executions
    pub fn render_all(&self, executions: &[ToolExecution]) -> String {
        executions
            .iter()
            .map(|e| e.render())
            .collect::<Vec<_>>()
            .join("\n")
    }
}

impl Default for ToolExecutionRenderer {
    fn default() -> Self {
        Self::new()
    }
}

/// Format bytes for human-readable display
fn format_bytes(bytes: usize) -> String {
    if bytes < 1024 {
        format!("{}B", bytes)
    } else if bytes < 1024 * 1024 {
        format!("{:.1}KB", bytes as f64 / 1024.0)
    } else if bytes < 1024 * 1024 * 1024 {
        format!("{:.1}MB", bytes as f64 / (1024.0 * 1024.0))
    } else {
        format!("{:.1}GB", bytes as f64 / (1024.0 * 1024.0 * 1024.0))
    }
}

/// Enhanced Bash execution state with output truncation and streaming support
#[derive(Debug, Clone)]
pub struct BashExecution {
    pub command: String,
    pub output: String,
    pub exit_code: Option<i32>,
    pub start_time: std::time::Instant,
    pub is_running: bool,
    pub is_cancelled: bool,
    /// Whether the output is expanded (shows full output) or collapsed (preview only)
    pub expanded: bool,
    /// Truncation result for context limits
    pub truncation_info: Option<TruncationInfo>,
    /// Path to full output file if truncated
    pub full_output_path: Option<String>,
}

/// Information about output truncation
#[derive(Debug, Clone)]
pub struct TruncationInfo {
    /// Total lines before truncation
    pub total_lines: usize,
    /// Lines shown after truncation
    pub shown_lines: usize,
    /// Bytes before truncation
    pub total_bytes: usize,
}

impl BashExecution {
    pub fn new(command: String) -> Self {
        Self {
            command,
            output: String::new(),
            exit_code: None,
            start_time: std::time::Instant::now(),
            is_running: true,
            is_cancelled: false,
            expanded: false,
            truncation_info: None,
            full_output_path: None,
        }
    }

    /// Append output (stripping ANSI codes for display)
    pub fn append_output(&mut self, chunk: &str) {
        // Strip ANSI codes and normalize line endings
        let clean = strip_ansi(chunk).replace("\r\n", "\n").replace("\r", "\n");

        // Append to output
        if !self.output.is_empty() && !clean.is_empty() {
            self.output.push_str(&clean);
        } else {
            self.output.push_str(&clean);
        }
    }

    /// Mark as complete
    pub fn complete(&mut self, exit_code: i32) {
        self.exit_code = Some(exit_code);
        self.is_running = false;
    }

    /// Complete with truncation info
    pub fn complete_with_truncation(
        &mut self,
        exit_code: i32,
        truncation_info: TruncationInfo,
        full_output_path: Option<String>,
    ) {
        self.exit_code = Some(exit_code);
        self.is_running = false;
        self.truncation_info = Some(truncation_info);
        self.full_output_path = full_output_path;
    }

    /// Cancel execution
    pub fn cancel(&mut self) {
        self.is_cancelled = true;
        self.is_running = false;
        self.exit_code = Some(-1);
        self.output.push_str("\n[Cancelled]");
    }

    /// Get elapsed time
    pub fn elapsed(&self) -> std::time::Duration {
        self.start_time.elapsed()
    }

    /// Set expanded/collapsed state
    pub fn set_expanded(&mut self, expanded: bool) {
        self.expanded = expanded;
    }

    /// Toggle expanded state
    pub fn toggle_expanded(&mut self) {
        self.expanded = !self.expanded;
    }

    /// Get output lines
    pub fn output_lines(&self) -> Vec<&str> {
        self.output.lines().collect()
    }

    /// Get raw output string
    pub fn get_output(&self) -> &str {
        &self.output
    }

    /// Render the bash execution display
    pub fn render(&self) -> String {
        let mut output = String::new();

        // Preview line limit (when not expanded)
        const PREVIEW_LINES: usize = 20;

        // Command header with styling
        output.push_str(&format!("\x1b[1m$ {}\x1b[0m\n", self.command));

        // Process output lines for display
        let lines: Vec<&str> = self.output.lines().collect();
        let total_lines = lines.len();
        let hidden_lines = if total_lines > PREVIEW_LINES && !self.expanded {
            total_lines - PREVIEW_LINES
        } else {
            0
        };

        // Show output
        if !self.output.is_empty() {
            let lines_to_show = if self.expanded {
                &lines[..]
            } else {
                // Show last PREVIEW_LINES
                if lines.len() > PREVIEW_LINES {
                    &lines[lines.len() - PREVIEW_LINES..]
                } else {
                    &lines[..]
                }
            };

            // Muted color for output
            for line in lines_to_show {
                output.push_str(&format!("\x1b[2m{}\x1b[0m\n", line));
            }
        }

        // Status line
        if self.is_running {
            // Running state with elapsed time
            output.push_str(&format!(
                "\x1b[90mRunning... ({:.1}s)\x1b[0m\n",
                self.elapsed().as_secs_f64()
            ));
        } else {
            let mut status_parts = Vec::new();

            // Hidden lines indicator
            if hidden_lines > 0 {
                if self.expanded {
                    status_parts.push("\x1b[2m(to collapse)\x1b[0m".to_string());
                } else {
                    status_parts.push(format!("\x1b[2m... {} more lines\x1b[0m", hidden_lines));
                }
            }

            // Status
            if self.is_cancelled {
                status_parts.push("\x1b[33m(cancelled)\x1b[0m".to_string());
            } else if let Some(code) = self.exit_code {
                if code != 0 {
                    status_parts.push(format!("\x1b[31m(exit {})\x1b[0m", code));
                }
            }

            // Truncation warning
            if self.truncation_info.is_some() {
                if let Some(ref path) = self.full_output_path {
                    status_parts.push(format!(
                        "\x1b[33mOutput truncated. Full output: {}\x1b[0m",
                        path
                    ));
                }
            }

            if !status_parts.is_empty() {
                output.push_str(&status_parts.join(" "));
                output.push('\n');
            }
        }

        output
    }
}

/// Strip ANSI escape codes from a string
fn strip_ansi(input: &str) -> String {
    let mut result = String::with_capacity(input.len());
    let mut chars = input.chars().peekable();

    while let Some(c) = chars.next() {
        if c == '\x1b' {
            // CSI sequence
            if chars.next() == Some('[') {
                // Read until we hit a letter
                while let Some(&ch) = chars.peek() {
                    if ch.is_ascii_alphabetic() {
                        chars.next();
                        break;
                    }
                    chars.next();
                }
            }
        } else {
            result.push(c);
        }
    }

    result
}

/// Summary message types for compaction and branch summaries
#[derive(Debug, Clone)]
pub enum SummaryMessageType {
    /// Compaction summary after context window management
    Compaction { tokens_before: usize },
    /// Branch summary when creating/merging branches
    Branch,
}

/// Summary message data
#[derive(Debug, Clone)]
pub struct SummaryMessage {
    pub message_type: SummaryMessageType,
    pub summary: String,
    pub expanded: bool,
}

impl SummaryMessage {
    pub fn compaction(tokens_before: usize, summary: impl Into<String>) -> Self {
        Self {
            message_type: SummaryMessageType::Compaction { tokens_before },
            summary: summary.into(),
            expanded: false,
        }
    }

    pub fn branch(summary: impl Into<String>) -> Self {
        Self {
            message_type: SummaryMessageType::Branch,
            summary: summary.into(),
            expanded: false,
        }
    }

    /// Set expanded/collapsed state
    pub fn set_expanded(&mut self, expanded: bool) {
        self.expanded = expanded;
    }

    /// Toggle expanded state
    pub fn toggle_expanded(&mut self) {
        self.expanded = !self.expanded;
    }

    /// Render the summary message
    pub fn render(&self) -> String {
        let mut output = String::new();

        // Label with bold styling
        let label = match &self.message_type {
            SummaryMessageType::Compaction { tokens_before } => {
                format!(
                    "\x1b[1m[compaction]\x1b[0m Compacted from {} tokens",
                    tokens_before
                )
            }
            SummaryMessageType::Branch => "\x1b[1m[branch]\x1b[0m Branch Summary".to_string(),
        };

        output.push_str(&format!("\x1b[48;5;24m {} \x1b[0m\n", label));

        if self.expanded {
            // Show full summary with markdown-style formatting
            output.push_str("\n");
            output.push_str(&render_markdown(&self.summary));
            output.push('\n');
        } else {
            // Show collapsed hint
            output.push_str(&format!("\x1b[2m(to expand)\x1b[0m\n",));
        }

        output
    }
}

/// Summary message renderer with options
pub struct SummaryMessageRenderer;

impl SummaryMessageRenderer {
    /// Render a compaction summary
    pub fn render_compaction(tokens_before: usize, summary: &str, expanded: bool) -> String {
        let mut msg = SummaryMessage::compaction(tokens_before, summary);
        msg.set_expanded(expanded);
        msg.render()
    }

    /// Render a branch summary
    pub fn render_branch(summary: &str, expanded: bool) -> String {
        let mut msg = SummaryMessage::branch(summary);
        msg.set_expanded(expanded);
        msg.render()
    }
}

// ═══════════════════════════════════════════════════════════════════════════
// Extension-Related Interactive Components
// ═══════════════════════════════════════════════════════════════════════════

/// Editor for extension-provided input.
///
/// Displays a bordered, titled multi-line text area with hint text
/// showing keybindings for submit / newline / cancel / external editor.
#[derive(Debug, Clone)]
pub struct ExtensionEditor {
    /// Title displayed above the editor.
    pub title: String,
    /// Current text content.
    pub text: String,
    /// Cursor position within `text`.
    pub cursor_pos: usize,
    /// Whether an external editor is available ($VISUAL or $EDITOR).
    pub has_external_editor: bool,
}

impl ExtensionEditor {
    /// Create a new extension editor with a title and optional prefill text.
    pub fn new(title: impl Into<String>, prefill: Option<&str>) -> Self {
        let text = prefill.unwrap_or("").to_string();
        let cursor_pos = text.len();
        let has_external_editor = std::env::var("VISUAL")
            .or_else(|_| std::env::var("EDITOR"))
            .is_ok();
        Self {
            title: title.into(),
            text,
            cursor_pos,
            has_external_editor,
        }
    }

    /// Insert a character at the cursor position.
    pub fn input_char(&mut self, c: char) {
        self.text.insert(self.cursor_pos, c);
        self.cursor_pos += c.len_utf8();
    }

    /// Delete character before cursor.
    pub fn backspace(&mut self) {
        if self.cursor_pos > 0 {
            // Walk back to the previous char boundary
            let prev = self.text[..self.cursor_pos]
                .char_indices()
                .last()
                .map(|(i, _)| i)
                .unwrap_or(0);
            self.text.drain(prev..self.cursor_pos);
            self.cursor_pos = prev;
        }
    }

    /// Replace the full text (e.g. after external editor session).
    pub fn set_text(&mut self, text: impl Into<String>) {
        self.text = text.into();
        self.cursor_pos = self.text.len();
    }

    /// Render the editor component as a vector of lines.
    pub fn render(&self) -> Vec<String> {
        let border = "─".repeat(60);
        let mut lines = Vec::new();

        lines.push(border.clone());
        lines.push(String::new()); // spacer
        lines.push(format!("\x1b[36m{}\x1b[0m", self.title));
        lines.push(String::new()); // spacer

        // Editor content (each source line as a rendered line)
        for line in self.text.lines() {
            lines.push(line.to_string());
        }
        if self.text.is_empty() {
            lines.push(String::new());
        }

        lines.push(String::new()); // spacer

        // Hint line
        let mut hints = vec!["Enter: submit", "Shift+Enter: newline", "Esc: cancel"];
        if self.has_external_editor {
            hints.push("Ctrl+G: external editor");
        }
        lines.push(format!("\x1b[2m{}\x1b[0m", hints.join("  ")));
        lines.push(String::new()); // spacer
        lines.push(border);

        lines
    }
}

/// Simple text input component for extensions.
///
/// Displays a bordered, titled single-line input with optional timeout
/// countdown shown in the title.
#[derive(Debug, Clone)]
pub struct ExtensionInput {
    /// Title displayed above the input.
    pub title: String,
    /// Current input value.
    pub value: String,
    /// Cursor position within `value`.
    pub cursor_pos: usize,
    /// Optional timeout in seconds (shown as countdown in title).
    pub timeout_secs: Option<u64>,
    /// Remaining seconds (when timeout is active).
    pub remaining_secs: Option<u64>,
}

impl ExtensionInput {
    /// Create a new extension input with a title and optional timeout.
    pub fn new(title: impl Into<String>, timeout_secs: Option<u64>) -> Self {
        Self {
            title: title.into(),
            value: String::new(),
            cursor_pos: 0,
            timeout_secs,
            remaining_secs: timeout_secs,
        }
    }

    /// Insert a character at the cursor position.
    pub fn input_char(&mut self, c: char) {
        self.value.insert(self.cursor_pos, c);
        self.cursor_pos += c.len_utf8();
    }

    /// Delete character before cursor.
    pub fn backspace(&mut self) {
        if self.cursor_pos > 0 {
            let prev = self.value[..self.cursor_pos]
                .char_indices()
                .last()
                .map(|(i, _)| i)
                .unwrap_or(0);
            self.value.drain(prev..self.cursor_pos);
            self.cursor_pos = prev;
        }
    }

    /// Update the countdown timer.
    pub fn tick(&mut self) -> bool {
        if let Some(ref mut remaining) = self.remaining_secs {
            if *remaining > 0 {
                *remaining -= 1;
                return *remaining > 0;
            }
            return false;
        }
        true
    }

    /// Render the input component as a vector of lines.
    pub fn render(&self) -> Vec<String> {
        let border = "─".repeat(60);
        let mut lines = Vec::new();

        lines.push(border.clone());
        lines.push(String::new()); // spacer

        // Title with optional countdown
        let title_display = if let Some(remaining) = self.remaining_secs {
            format!("{} ({}s)", self.title, remaining)
        } else {
            self.title.clone()
        };
        lines.push(format!("\x1b[36m{}\x1b[0m", title_display));
        lines.push(String::new()); // spacer

        // Input field
        lines.push(format!("> {}", self.value));
        lines.push(String::new()); // spacer

        // Hints
        lines.push(format!("\x1b[2mEnter: submit  Esc: cancel\x1b[0m"));
        lines.push(String::new()); // spacer
        lines.push(border);

        lines
    }
}

/// Generic selector component for extensions.
///
/// Displays a list of string options with keyboard navigation
/// (up/down arrows, j/k) and selection (Enter).
#[derive(Debug, Clone)]
pub struct ExtensionSelector {
    /// Title displayed above the list.
    pub title: String,
    /// Available options.
    pub options: Vec<String>,
    /// Currently selected index.
    pub selected_index: usize,
    /// Optional timeout in seconds.
    pub timeout_secs: Option<u64>,
    /// Remaining seconds (when timeout is active).
    pub remaining_secs: Option<u64>,
}

impl ExtensionSelector {
    /// Create a new extension selector with a title and list of options.
    pub fn new(title: impl Into<String>, options: Vec<String>, timeout_secs: Option<u64>) -> Self {
        Self {
            title: title.into(),
            selected_index: 0,
            timeout_secs,
            remaining_secs: timeout_secs,
            options,
        }
    }

    /// Move selection up.
    pub fn move_up(&mut self) {
        if self.selected_index > 0 {
            self.selected_index -= 1;
        }
    }

    /// Move selection down.
    pub fn move_down(&mut self) {
        if self.selected_index < self.options.len().saturating_sub(1) {
            self.selected_index += 1;
        }
    }

    /// Get the currently selected option.
    pub fn selected(&self) -> Option<&str> {
        self.options.get(self.selected_index).map(|s| s.as_str())
    }

    /// Update the countdown timer.
    pub fn tick(&mut self) -> bool {
        if let Some(ref mut remaining) = self.remaining_secs {
            if *remaining > 0 {
                *remaining -= 1;
                return *remaining > 0;
            }
            return false;
        }
        true
    }

    /// Render the selector component as a vector of lines.
    pub fn render(&self) -> Vec<String> {
        let border = "─".repeat(60);
        let mut lines = Vec::new();

        lines.push(border.clone());
        lines.push(String::new()); // spacer

        // Title with optional countdown
        let title_display = if let Some(remaining) = self.remaining_secs {
            format!("{} ({}s)", self.title, remaining)
        } else {
            self.title.clone()
        };
        lines.push(format!("\x1b[1m\x1b[36m{}\x1b[0m", title_display));
        lines.push(String::new()); // spacer

        // Options
        for (i, option) in self.options.iter().enumerate() {
            if i == self.selected_index {
                lines.push(format!("\x1b[36m→ {}\x1b[0m", option));
            } else {
                lines.push(format!("  \x1b[37m{}\x1b[0m", option));
            }
        }

        lines.push(String::new()); // spacer

        // Hints
        lines.push(format!(
            "\x1b[2m↑↓: navigate  Enter: select  Esc: cancel\x1b[0m"
        ));
        lines.push(String::new()); // spacer
        lines.push(border);

        lines
    }
}

/// Custom editor registered by extensions.
///
/// Wraps an editor-like buffer with extension action handlers
/// for escape, Ctrl+D, paste-image, and extension-registered shortcuts.
#[derive(Debug, Clone)]
pub struct CustomEditor {
    /// Current text content.
    pub text: String,
    /// Cursor position within `text`.
    pub cursor_pos: usize,
    /// Whether autocomplete is currently showing.
    pub showing_autocomplete: bool,
    /// Registered action handlers (action name → description).
    pub registered_actions: Vec<String>,
}

impl CustomEditor {
    /// Create a new custom editor.
    pub fn new() -> Self {
        Self {
            text: String::new(),
            cursor_pos: 0,
            showing_autocomplete: false,
            registered_actions: Vec::new(),
        }
    }

    /// Create with initial text content.
    pub fn with_text(text: impl Into<String>) -> Self {
        let text = text.into();
        let cursor_pos = text.len();
        Self {
            text,
            cursor_pos,
            showing_autocomplete: false,
            registered_actions: Vec::new(),
        }
    }

    /// Insert a character at the cursor position.
    pub fn input_char(&mut self, c: char) {
        self.text.insert(self.cursor_pos, c);
        self.cursor_pos += c.len_utf8();
        self.showing_autocomplete = false;
    }

    /// Delete character before cursor.
    pub fn backspace(&mut self) {
        if self.cursor_pos > 0 {
            let prev = self.text[..self.cursor_pos]
                .char_indices()
                .last()
                .map(|(i, _)| i)
                .unwrap_or(0);
            self.text.drain(prev..self.cursor_pos);
            self.cursor_pos = prev;
        }
        self.showing_autocomplete = false;
    }

    /// Check if the editor is empty (for Ctrl+D exit detection).
    pub fn is_empty(&self) -> bool {
        self.text.is_empty()
    }

    /// Register an action handler.
    pub fn register_action(&mut self, action: impl Into<String>) {
        let action = action.into();
        if !self.registered_actions.contains(&action) {
            self.registered_actions.push(action);
        }
    }

    /// Render the custom editor as a vector of lines.
    pub fn render(&self) -> Vec<String> {
        let mut lines = Vec::new();

        // Show text content line-by-line
        for line in self.text.lines() {
            lines.push(line.to_string());
        }
        if self.text.is_empty() {
            lines.push(String::new());
        }

        lines
    }
}

impl Default for CustomEditor {
    fn default() -> Self {
        Self::new()
    }
}

/// Custom message type rendering.
///
/// Renders extension-provided custom messages with a styled label
/// and markdown content, similar to how pi-mono renders custom
/// message entries.
#[derive(Debug, Clone)]
pub struct CustomMessageComponent {
    /// The custom type label.
    pub custom_type: String,
    /// The message content (plain text).
    pub content: String,
    /// Whether the message is expanded.
    pub expanded: bool,
}

impl CustomMessageComponent {
    /// Create a new custom message component.
    pub fn new(custom_type: impl Into<String>, content: impl Into<String>) -> Self {
        Self {
            custom_type: custom_type.into(),
            content: content.into(),
            expanded: false,
        }
    }

    /// Set expanded/collapsed state.
    pub fn set_expanded(&mut self, expanded: bool) {
        self.expanded = expanded;
    }

    /// Toggle expanded state.
    pub fn toggle_expanded(&mut self) {
        self.expanded = !self.expanded;
    }

    /// Render the custom message as a vector of lines.
    pub fn render(&self) -> Vec<String> {
        let mut lines = Vec::new();

        lines.push(String::new()); // spacer

        // Label with type
        let label = format!("\x1b[35m\x1b[1m[{}]\x1b[0m", self.custom_type);
        lines.push(label);
        lines.push(String::new()); // spacer

        // Content with background
        if self.expanded {
            for line in self.content.lines() {
                lines.push(format!("\x1b[48;5;54m {} \x1b[0m", line));
            }
        } else {
            // Show first line as preview
            let first_line = self.content.lines().next().unwrap_or("");
            let preview = if first_line.len() > 80 {
                format!("{}...", &first_line[..80])
            } else if first_line.is_empty() {
                "(empty)".to_string()
            } else {
                first_line.to_string()
            };
            lines.push(format!("\x1b[48;5;54m {} \x1b[0m", preview));
            let total_lines = self.content.lines().count();
            if total_lines > 1 {
                lines.push(format!(
                    "\x1b[2m  ... {} more lines\x1b[0m",
                    total_lines.saturating_sub(1)
                ));
            }
        }

        lines
    }
}

/// Login dialog state for provider login flow.
///
/// Supports multiple login phases: URL display, manual code input,
/// prompt display, waiting, and progress messages.
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum LoginDialogPhase {
    /// Initial state.
    Init,
    /// Showing authorization URL to the user.
    ShowAuth {
        url: String,
        instructions: Option<String>,
    },
    /// Waiting for user to manually input a code/URL.
    ManualInput {
        prompt: String,
        value: String,
        cursor_pos: usize,
    },
    /// Showing a prompt and waiting for input.
    Prompt {
        message: String,
        placeholder: Option<String>,
        value: String,
        cursor_pos: usize,
    },
    /// Showing informational text.
    Info { lines: Vec<String> },
    /// Showing a waiting/polling message.
    Waiting { message: String },
    /// Login completed (success or failure).
    Completed {
        success: bool,
        message: Option<String>,
    },
}

/// Provider login dialog component.
///
/// Manages the full lifecycle of an OAuth or API-key login flow,
/// rendering appropriate UI at each phase.
#[derive(Debug, Clone)]
pub struct ProviderLoginDialog {
    /// Provider ID (e.g. "anthropic", "openai").
    pub provider_id: String,
    /// Provider display name.
    pub provider_name: String,
    /// Optional custom title.
    pub title: Option<String>,
    /// Current phase of the login flow.
    pub phase: LoginDialogPhase,
}

impl ProviderLoginDialog {
    /// Create a new provider login dialog.
    pub fn new(provider_id: impl Into<String>, provider_name: impl Into<String>) -> Self {
        Self {
            provider_id: provider_id.into(),
            provider_name: provider_name.into(),
            title: None,
            phase: LoginDialogPhase::Init,
        }
    }

    /// Set a custom title.
    pub fn with_title(mut self, title: impl Into<String>) -> Self {
        self.title = Some(title.into());
        self
    }

    /// Transition to the show-auth phase.
    pub fn show_auth(&mut self, url: impl Into<String>, instructions: Option<String>) {
        self.phase = LoginDialogPhase::ShowAuth {
            url: url.into(),
            instructions,
        };
    }

    /// Transition to the manual-input phase.
    pub fn show_manual_input(&mut self, prompt: impl Into<String>) {
        self.phase = LoginDialogPhase::ManualInput {
            prompt: prompt.into(),
            value: String::new(),
            cursor_pos: 0,
        };
    }

    /// Transition to the prompt phase.
    pub fn show_prompt(&mut self, message: impl Into<String>, placeholder: Option<String>) {
        self.phase = LoginDialogPhase::Prompt {
            message: message.into(),
            placeholder,
            value: String::new(),
            cursor_pos: 0,
        };
    }

    /// Transition to the info phase.
    pub fn show_info(&mut self, lines: Vec<String>) {
        self.phase = LoginDialogPhase::Info { lines };
    }

    /// Transition to the waiting phase.
    pub fn show_waiting(&mut self, message: impl Into<String>) {
        self.phase = LoginDialogPhase::Waiting {
            message: message.into(),
        };
    }

    /// Show a progress message (appends to current phase).
    pub fn show_progress(&mut self, message: &str) {
        // Progress messages are displayed during waiting/polling
        match &mut self.phase {
            LoginDialogPhase::Waiting {
                message: ref mut msg,
            } => {
                msg.push_str(&format!("\n{}", message));
            }
            _ => {
                self.phase = LoginDialogPhase::Waiting {
                    message: message.to_string(),
                };
            }
        }
    }

    /// Complete the login flow.
    pub fn complete(&mut self, success: bool, message: Option<String>) {
        self.phase = LoginDialogPhase::Completed { success, message };
    }

    /// Input a character (for ManualInput and Prompt phases).
    pub fn input_char(&mut self, c: char) {
        match &mut self.phase {
            LoginDialogPhase::ManualInput {
                value, cursor_pos, ..
            }
            | LoginDialogPhase::Prompt {
                value, cursor_pos, ..
            } => {
                value.insert(*cursor_pos, c);
                *cursor_pos += c.len_utf8();
            }
            _ => {}
        }
    }

    /// Backspace (for ManualInput and Prompt phases).
    pub fn backspace(&mut self) {
        match &mut self.phase {
            LoginDialogPhase::ManualInput {
                value, cursor_pos, ..
            }
            | LoginDialogPhase::Prompt {
                value, cursor_pos, ..
            } => {
                if *cursor_pos > 0 {
                    let prev = value[..*cursor_pos]
                        .char_indices()
                        .last()
                        .map(|(i, _)| i)
                        .unwrap_or(0);
                    value.drain(prev..*cursor_pos);
                    *cursor_pos = prev;
                }
            }
            _ => {}
        }
    }

    /// Get the current input value (for ManualInput and Prompt phases).
    pub fn input_value(&self) -> Option<&str> {
        match &self.phase {
            LoginDialogPhase::ManualInput { value, .. }
            | LoginDialogPhase::Prompt { value, .. } => Some(value.as_str()),
            _ => None,
        }
    }

    /// Render the login dialog as a vector of lines.
    pub fn render(&self) -> Vec<String> {
        let border = "─".repeat(60);
        let mut lines = Vec::new();

        // Title
        let title = self
            .title
            .clone()
            .unwrap_or_else(|| format!("Login to {}", self.provider_name));
        lines.push(border.clone());
        lines.push(format!("\x1b[1m\x1b[36m{}\x1b[0m", title));

        // Content based on phase
        match &self.phase {
            LoginDialogPhase::Init => {
                lines.push(String::new());
                lines.push("Initializing...".to_string());
            }
            LoginDialogPhase::ShowAuth { url, instructions } => {
                lines.push(String::new());
                lines.push(format!("\x1b[36m{}\x1b[0m", url));
                lines.push(format!("\x1b[2mCmd+click to open\x1b[0m"));
                if let Some(instr) = instructions {
                    lines.push(String::new());
                    lines.push(format!("\x1b[33m{}\x1b[0m", instr));
                }
            }
            LoginDialogPhase::ManualInput {
                prompt,
                value,
                cursor_pos: _,
            } => {
                lines.push(String::new());
                lines.push(format!("\x1b[2m{}\x1b[0m", prompt));
                lines.push(format!("> {}", value));
                lines.push(format!("\x1b[2m(Esc to cancel)\x1b[0m"));
            }
            LoginDialogPhase::Prompt {
                message,
                placeholder,
                value,
                cursor_pos: _,
            } => {
                lines.push(String::new());
                lines.push(message.clone());
                if let Some(ph) = placeholder {
                    lines.push(format!("\x1b[2me.g., {}\x1b[0m", ph));
                }
                lines.push(format!("> {}", value));
                lines.push(format!("\x1b[2m(Esc to cancel, Enter to submit)\x1b[0m"));
            }
            LoginDialogPhase::Info { lines: info_lines } => {
                lines.push(String::new());
                for line in info_lines {
                    lines.push(line.clone());
                }
                lines.push(String::new());
                lines.push(format!("\x1b[2m(Esc to close)\x1b[0m"));
            }
            LoginDialogPhase::Waiting { message } => {
                lines.push(String::new());
                lines.push(format!("\x1b[2m{}\x1b[0m", message));
                lines.push(format!("\x1b[2m(Esc to cancel)\x1b[0m"));
            }
            LoginDialogPhase::Completed { success, message } => {
                lines.push(String::new());
                if *success {
                    lines.push(format!("\x1b[32m✓ Login successful\x1b[0m"));
                } else {
                    lines.push(format!("\x1b[31m✗ Login failed\x1b[0m"));
                }
                if let Some(msg) = message {
                    lines.push(msg.clone());
                }
            }
        }

        lines.push(border);
        lines
    }
}

/// Auth provider information for the OAuth selector.
#[derive(Debug, Clone)]
pub struct AuthProviderInfo {
    /// Unique provider ID.
    pub id: String,
    /// Display name.
    pub name: String,
    /// Authentication type.
    pub auth_type: AuthType,
}

/// Authentication type for a provider.
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum AuthType {
    /// OAuth flow.
    OAuth,
    /// Simple API key.
    ApiKey,
}

/// Configuration status for a provider.
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum ProviderConfigStatus {
    /// Not configured at all.
    Unconfigured,
    /// Configured with the matching auth type.
    Configured { label: String },
    /// Configured with a different auth type.
    PartiallyConfigured { label: String },
}

/// OAuth selector component.
///
/// Displays a searchable, scrollable list of auth providers with
/// status indicators and navigation. Supports both login and logout modes.
#[derive(Debug, Clone)]
pub struct OAuthSelector {
    /// Mode: login or logout.
    pub mode: OAuthSelectorMode,
    /// All available providers.
    pub providers: Vec<AuthProviderInfo>,
    /// Currently filtered providers (indices into `providers`).
    pub filtered_indices: Vec<usize>,
    /// Currently selected index (into `filtered_indices`).
    pub selected_index: usize,
    /// Search filter text.
    pub filter: String,
    /// Scroll offset for the visible window.
    pub scroll_offset: usize,
    /// Max visible items at once.
    pub visible_height: usize,
    /// Config status for each provider (indexed by provider ID).
    pub config_status: std::collections::HashMap<String, ProviderConfigStatus>,
}

/// Mode for the OAuth selector.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum OAuthSelectorMode {
    /// Selecting a provider to log into.
    Login,
    /// Selecting a provider to log out of.
    Logout,
}

impl OAuthSelector {
    /// Create a new OAuth selector.
    pub fn new(mode: OAuthSelectorMode, providers: Vec<AuthProviderInfo>) -> Self {
        let filtered_indices: Vec<usize> = (0..providers.len()).collect();
        Self {
            mode,
            providers,
            filtered_indices,
            selected_index: 0,
            filter: String::new(),
            scroll_offset: 0,
            visible_height: 8,
            config_status: std::collections::HashMap::new(),
        }
    }

    /// Move selection up.
    pub fn move_up(&mut self) {
        if self.selected_index > 0 {
            self.selected_index -= 1;
            self.adjust_scroll();
        }
    }

    /// Move selection down.
    pub fn move_down(&mut self) {
        let max = self.filtered_indices.len().saturating_sub(1);
        if self.selected_index < max {
            self.selected_index += 1;
            self.adjust_scroll();
        }
    }

    /// Get the currently selected provider.
    pub fn selected(&self) -> Option<&AuthProviderInfo> {
        self.filtered_indices
            .get(self.selected_index)
            .and_then(|&idx| self.providers.get(idx))
    }

    /// Update the search filter and recompute filtered list.
    pub fn set_filter(&mut self, filter: String) {
        self.filter = filter;
        let filter_lower = self.filter.to_lowercase();
        self.filtered_indices = self
            .providers
            .iter()
            .enumerate()
            .filter(|(_, p)| {
                if filter_lower.is_empty() {
                    return true;
                }
                p.name.to_lowercase().contains(&filter_lower)
                    || p.id.to_lowercase().contains(&filter_lower)
            })
            .map(|(i, _)| i)
            .collect();
        self.selected_index = 0;
        self.scroll_offset = 0;
    }

    /// Set the config status for a provider.
    pub fn set_config_status(
        &mut self,
        provider_id: impl Into<String>,
        status: ProviderConfigStatus,
    ) {
        self.config_status.insert(provider_id.into(), status);
    }

    fn adjust_scroll(&mut self) {
        if self.selected_index < self.scroll_offset {
            self.scroll_offset = self.selected_index;
        } else if self.selected_index >= self.scroll_offset + self.visible_height {
            self.scroll_offset = self.selected_index - self.visible_height + 1;
        }
    }

    /// Render the OAuth selector as a vector of lines.
    pub fn render(&self) -> Vec<String> {
        let border = "─".repeat(60);
        let mut lines = Vec::new();

        lines.push(border.clone());
        lines.push(String::new()); // spacer

        // Title
        let title = match self.mode {
            OAuthSelectorMode::Login => "Select provider to configure:",
            OAuthSelectorMode::Logout => "Select provider to logout:",
        };
        lines.push(format!("\x1b[1m\x1b[36m{}\x1b[0m", title));
        lines.push(String::new()); // spacer

        // Search input
        lines.push(format!("Search: {}", self.filter));
        lines.push(String::new()); // spacer

        // Provider list
        let end = (self.scroll_offset + self.visible_height).min(self.filtered_indices.len());
        for vi in self.scroll_offset..end {
            if let Some(&pi) = self.filtered_indices.get(vi) {
                if let Some(provider) = self.providers.get(pi) {
                    let is_selected = vi == self.selected_index;

                    // Status indicator
                    let status_str = match self.config_status.get(&provider.id) {
                        Some(ProviderConfigStatus::Configured { label }) => {
                            format!("\x1b[32m ✓ {}\x1b[0m", label)
                        }
                        Some(ProviderConfigStatus::PartiallyConfigured { label }) => {
                            format!("\x1b[33m • {}\x1b[0m", label)
                        }
                        Some(ProviderConfigStatus::Unconfigured) | None => {
                            if provider.auth_type == AuthType::ApiKey {
                                "\x1b[2m • unconfigured\x1b[0m".to_string()
                            } else {
                                "\x1b[2m • unconfigured\x1b[0m".to_string()
                            }
                        }
                    };

                    if is_selected {
                        lines.push(format!("\x1b[36m→ {}\x1b[0m{}", provider.name, status_str));
                    } else {
                        lines.push(format!("  \x1b[37m{}\x1b[0m{}", provider.name, status_str));
                    }
                }
            }
        }

        // Scroll indicator
        if self.filtered_indices.len() > self.visible_height {
            lines.push(format!(
                "\x1b[2m  ({}/{})\x1b[0m",
                self.selected_index + 1,
                self.filtered_indices.len()
            ));
        }

        // Empty state
        if self.filtered_indices.is_empty() {
            let msg = if self.providers.is_empty() {
                match self.mode {
                    OAuthSelectorMode::Login => "No providers available",
                    OAuthSelectorMode::Logout => "No providers logged in. Use /login first.",
                }
            } else {
                "No matching providers"
            };
            lines.push(format!("\x1b[2m  {}\x1b[0m", msg));
        }

        lines.push(String::new()); // spacer
        lines.push(border);

        lines
    }
}

/// Loading indicator with border and status.
///
/// Displays a spinner-style loading message inside bordered area,
/// with an optional cancel hint.
#[derive(Debug, Clone)]
pub struct BorderedLoader {
    /// Loading message.
    pub message: String,
    /// Whether the loader can be cancelled.
    pub cancellable: bool,
    /// Spinner frame index (cycles 0..4).
    pub spinner_frame: usize,
}

impl BorderedLoader {
    /// Create a new bordered loader.
    pub fn new(message: impl Into<String>, cancellable: bool) -> Self {
        Self {
            message: message.into(),
            cancellable,
            spinner_frame: 0,
        }
    }

    /// Advance the spinner by one frame.
    pub fn tick(&mut self) {
        self.spinner_frame = (self.spinner_frame + 1) % 4;
    }

    /// Get the current spinner character.
    pub fn spinner_char(&self) -> &'static str {
        match self.spinner_frame {
            0 => "⠋",
            1 => "⠙",
            2 => "⠹",
            3 => "⠸",
            _ => "⠋",
        }
    }

    /// Render the bordered loader as a vector of lines.
    pub fn render(&self) -> Vec<String> {
        let border = "─".repeat(60);
        let mut lines = Vec::new();

        lines.push(format!("\x1b[90m{}\x1b[0m", border));

        // Loading line with spinner
        lines.push(format!(
            "\x1b[36m{} {}\x1b[0m",
            self.spinner_char(),
            self.message
        ));

        // Cancel hint
        if self.cancellable {
            lines.push(String::new());
            lines.push(format!("\x1b[2mEsc: cancel\x1b[0m"));
        }

        lines.push(format!("\x1b[90m{}\x1b[0m", border));

        lines
    }
}

// ── Armin Component ──────────────────────────────────────────────────────────

/// XBM image data: 31x36 pixels, LSB first, 1=background, 0=foreground
const ARMIN_WIDTH: usize = 31;
const ARMIN_HEIGHT: usize = 36;
const ARMIN_BYTES_PER_ROW: usize = (ARMIN_WIDTH + 7) / 8; // ceil(31/8) = 4
const ARMIN_DISPLAY_HEIGHT: usize = (ARMIN_HEIGHT + 1) / 2; // half-block rendering

const ARMIN_BITS: [u8; 144] = [
    0xff, 0xff, 0xff, 0x7f, 0xff, 0xf0, 0xff, 0x7f, 0xff, 0xed, 0xff, 0x7f, 0xff, 0xdb, 0xff, 0x7f,
    0xff, 0xb7, 0xff, 0x7f, 0xff, 0x77, 0xfe, 0x7f, 0x3f, 0xf8, 0xfe, 0x7f, 0xdf, 0xff, 0xfe, 0x7f,
    0xdf, 0x3f, 0xfc, 0x7f, 0x9f, 0xc3, 0xfb, 0x7f, 0x6f, 0xfc, 0xf4, 0x7f, 0xf7, 0x0f, 0xf7, 0x7f,
    0xf7, 0xff, 0xf7, 0x7f, 0xf7, 0xff, 0xe3, 0x7f, 0xf7, 0x07, 0xe8, 0x7f, 0xef, 0xf8, 0x67, 0x70,
    0x0f, 0xff, 0xbb, 0x6f, 0xf1, 0x00, 0xd0, 0x5b, 0xfd, 0x3f, 0xec, 0x53, 0xc1, 0xff, 0xef, 0x57,
    0x9f, 0xfd, 0xee, 0x5f, 0x9f, 0xfc, 0xae, 0x5f, 0x1f, 0x78, 0xac, 0x5f, 0x3f, 0x00, 0x50, 0x6c,
    0x7f, 0x00, 0xdc, 0x77, 0xff, 0xc0, 0x3f, 0x78, 0xff, 0x01, 0xf8, 0x7f, 0xff, 0x03, 0x9c, 0x78,
    0xff, 0x07, 0x8c, 0x7c, 0xff, 0x0f, 0xce, 0x78, 0xff, 0xff, 0xcf, 0x7f, 0xff, 0xff, 0xcf, 0x78,
    0xff, 0xff, 0xdf, 0x78, 0xff, 0xff, 0xdf, 0x7d, 0xff, 0xff, 0x3f, 0x7e, 0xff, 0xff, 0xff, 0x7f,
];

/// Get pixel at (x, y): true = foreground, false = background
fn armin_get_pixel(x: usize, y: usize) -> bool {
    if y >= ARMIN_HEIGHT {
        return false;
    }
    let byte_index = y * ARMIN_BYTES_PER_ROW + x / 8;
    let bit_index = x % 8;
    (ARMIN_BITS[byte_index] >> bit_index) & 1 == 0
}

/// Get the character for a cell using half-block rendering (2 vertical pixels)
fn armin_get_char(x: usize, row: usize) -> char {
    let upper = armin_get_pixel(x, row * 2);
    let lower = armin_get_pixel(x, row * 2 + 1);
    match (upper, lower) {
        (true, true) => '█',
        (true, false) => '▀',
        (false, true) => '▄',
        (false, false) => ' ',
    }
}

/// Build the final image grid of characters
fn armin_build_final_grid() -> Vec<Vec<char>> {
    let mut grid = Vec::with_capacity(ARMIN_DISPLAY_HEIGHT);
    for row in 0..ARMIN_DISPLAY_HEIGHT {
        let mut line = Vec::with_capacity(ARMIN_WIDTH);
        for x in 0..ARMIN_WIDTH {
            line.push(armin_get_char(x, row));
        }
        grid.push(line);
    }
    grid
}

/// ArminComponent – renders XBM art with a "ARMIN SAYS HI" message.
///
/// This is a static render (no animation in the Rust port).
/// The final image is drawn in accent color with the text message below.
pub struct ArminComponent {
    final_grid: Vec<Vec<char>>,
    cached_lines: Vec<String>,
    cached_width: usize,
}

impl ArminComponent {
    pub fn new() -> Self {
        Self {
            final_grid: armin_build_final_grid(),
            cached_lines: Vec::new(),
            cached_width: 0,
        }
    }

    /// Render the component to lines at the given width
    pub fn render(&mut self, width: usize) -> Vec<String> {
        if width == self.cached_width && !self.cached_lines.is_empty() {
            return self.cached_lines.clone();
        }

        let accent = "\x1b[38;5;75m"; // light blue accent
        let reset = "\x1b[0m";
        let padding = 1;
        let available = width.saturating_sub(padding);

        let mut lines = Vec::new();

        for row in &self.final_grid {
            let chars: String = row.iter().take(available).collect();
            let pad_right = available.saturating_sub(chars.len());
            lines.push(format!(
                " {}{}{}{}",
                accent,
                chars,
                " ".repeat(pad_right),
                reset
            ));
        }

        // "ARMIN SAYS HI" message
        let message = "ARMIN SAYS HI";
        let msg_pad = available.saturating_sub(message.len());
        lines.push(format!(
            " {}{}{}{}",
            accent,
            message,
            " ".repeat(msg_pad),
            reset
        ));

        self.cached_lines = lines;
        self.cached_width = width;
        self.cached_lines.clone()
    }
}

impl Default for ArminComponent {
    fn default() -> Self {
        Self::new()
    }
}

// ── Daxnuts Component ─────────────────────────────────────────────────────────

/// DaxnutsComponent – decorative RGB image rendered with half-block characters.
///
/// Ported from the TypeScript daxnuts.ts easter-egg component.
/// Uses 24-bit color escape codes for pixel-accurate rendering.
pub struct DaxnutsComponent {
    image_lines: Vec<String>,
}

impl DaxnutsComponent {
    pub fn new() -> Self {
        Self {
            image_lines: build_dax_image(),
        }
    }

    /// Render the component at the given terminal width
    pub fn render(&self, width: usize) -> Vec<String> {
        let reset = "\x1b[0m";
        let accent = "\x1b[38;5;75m"; // light blue
        let success = "\x1b[32m";
        let muted = "\x1b[90m";
        let dim = "\x1b[2m";
        let link = "\x1b[36m"; // cyan for links

        let mut lines: Vec<String> = Vec::new();
        lines.push(String::new());

        for img_line in &self.image_lines {
            lines.push(center_ansi(img_line, width));
        }

        lines.push(String::new());
        lines.push(center_ansi(
            &format!("{}Free Kimi K2.5 via OpenCode Zen{}", accent, reset),
            width,
        ));
        lines.push(center_ansi(
            &format!("{}\"Powered by daxnuts\"{}", success, reset),
            width,
        ));
        lines.push(center_ansi(&format!("{}— @thdxr{}", muted, reset), width));

        lines.push(String::new());
        lines.push(center_ansi(&format!("{}Try OpenCode{}", dim, reset), width));
        lines.push(center_ansi(
            &format!("{}https://mistral.ai/news/mistral-vibe-2-0{}", link, reset),
            width,
        ));
        lines.push(String::new());

        lines
    }
}

impl Default for DaxnutsComponent {
    fn default() -> Self {
        Self::new()
    }
}

/// DAX image hex-encoded RGB data (32x32 pixels, 3 bytes per pixel)
const DAX_HEX: &str = "bbbab8b9b9b6b9b8b5bcbbb8b8b7b4b7b5b2b6b5b2b8b7b4b7b6b3b6b4b1bdbcb8bab8b6bbb8b5b8b5b1bbb8b4c2bebbc1bebac0bdbabfbcb9c1bebabfbebbc0bfbcc0bdbabbb8b5c1bfbcbfbcb8bbb9b6bfbcb8c2bfbcc1bfbcbfbbb8bdb9b6b8b7b5b9b8b5b8b8b5b5b5b2b6b5b2b8b7b4b9b8b5b9b8b5b6b5b3bab8b5bcbab7bbb9b6bbb8b5bfb9b5bdb2abbcb0a8beb2aabeb5afbfbab6bebab7c0bfbcbebdbabebbb8c0bdbabfbebbc2bebbbdbab7c3c0bdc3c0bdc1bebbc2bebabfbcb8bab9b6b7b6b3b2b1aeb6b5b2b5b4b1b5b4b2b6b5b2b7b6b4b9b8b6b7b6b3bbbab7b2afaba5988fb49e90b09481b79a88b39683b09583b7a395bfb6b0c0bdbabdbbb8bebcb9c1bfbcc0bebbbdbab7bebbb8c2bfbcc0bdbac0bcb9bdb9b6c0bcb8b5b4b2b4b3b0bab9b6b9b9b6b5b4b1b5b4b1b6b5b3b9b8b5b9b8b6b9b8b6b2aeaa968174a6836eaa856eab846eaf8973ac8973b08f79b18f7ab39786b7a89dbbb3aebfbab6c2c0bdbebcb9bfbdbac3c1bdc2bebbc0bcb9bdb9b6c1bdbabfbbb8b4b3b0b9b8b5b8b7b5b4b3b1b5b4b1b8b7b4b8b7b5bab9b6bbbab7b1afad8c7a719d735ca47860a87d65a98069ae8972ae8c75af8d77aa826ba98067aa8974b39e90b6a79dbbb2adc0bdbac1bfbdbfbbb8c1bdb9bebab6c0bdb9bfbbb8c1bdbab4b2b0b7b6b4b7b6b3b4b2b0bab9b7b6b5b2b6b5b2bab9b6bab9b6958c87977663aa836bac8772b08f7aad8c77b2917db0917db0907cac8971a77d64a87f67ac8972b29887b8a89dbfbab5bfbdbac1bebac0bcb9c0bcb9c0bcb9c1bebabebab7b8b7b4b7b6b4b5b4b1b5b4b2b7b6b3b5b4b2bab9b7bab9b6b4b1ada88f7fad8973ae8d78b19684b19685b29786b69a89b29582b1917daa856ea87e66a97e66ad866ea9826baf9280b8ada6bdbbb8bebab7bfbbb8c1bdbabfbbb8bcb8b4bcb8b5b6b4b2b7b5b3b6b5b2b8b7b4b3b2afb8b7b4b6b5b2b3b2b0b3a59aab856fad8d78b0917eb19886b49b8bb49a89b39785b0917eaf8f7cab866fa77d65a77a61a87d64a9816ab08f79b5a296c1bcb8c3bfbcc2bebbbebab7bfbbb7bdbab6c2bebab8b7b4b7b6b4b6b5b3b7b6b3b6b5b2b9b8b6b4b3b1b6b1acac8f7ca9826bae8f7aaf9583b49c8cb49c8bb79d8cb59987b19380ad8e79ae8c77af8e78ac8771a3775faa826bae8972b39888bbb6b2bebbb8bfbbb8bfbbb8c0bdb9bebbb7c0bdb9b6b5b2b9b8b5b4b3b1b8b7b5b4b3b0b7b6b4b6b5b3b1a7a0aa8772a77d65a88570b49887b19b8d9c887c907a6d987f71aa907faf917daf8e7aad8c78ac8b77a8836ca9836cac8770b49b8abdb6b2c0bcb9c0bdb9bfbbb8bebab7bfbcb9bebab7b9b8b6b5b4b2b9b8b5b8b7b5b8b7b4b7b6b4b5b4b2b3a9a2ad8973a1755da9856fb398858c776a65544b776358725d526e594d9c7f6eb1907ba68672ad8e7aab8771ac856db18f79b3a092beb9b5c1bdbabdb9b5bebab7bfbbb7bebab7bcb9b6b7b6b4b6b6b3b8b7b4b5b4b2b8b6b4b7b6b3b4b3b0b4aba4a6826ba3775fb08e79b19584a88e7daa8e7db29481ad8f7c997e6da38674ac8d79ac8e7aae917f9a7c6a896a599a7c6ab3a398c1bdbabdb9b6bcb8b5bebab6bebab7bdb9b5bdb9b6b5b4b1b7b5b3b5b4b2b7b6b3b7b6b4b3b3b0b3b2b0b4aca5a7846fa97f68ae8f7bae9383b59c8bb2937fae8e79ac8b76af927eaf927eb29683b39885b2988891786a72594c6e594d978d86bdbab7bab7b3c0bcb9c0bcb9bebab7bebbb7bdb9b6b3b2b0b4b3b0b5b4b2b4b4b1b4b3b1b4b3b1b4b3b0b6ada5aa8670a57a62ad8e7ab29b8cb69d8dab856fa9826aa88069ab8771af907db49987b19684b29886b59987b39480b09787b5a9a1bcb8b5bebab7bdb9b5bebab7bfbbb8bfbbb7bbb7b4b3b2afb8b7b5b8b7b5b3b2b0b5b4b2b6b5b3b6b4b1afa299a98975a9826baf907cb39988b49a89af8e7aac8973aa856eaf8c74b1917dae907dac907db39988b29785b49785b7a090b9aca3bfbab7bcb8b5bdb9b6bcb8b4bcb8b5bdb9b5bcb8b4b5b4b2b6b5b3b4b3b0b4b3b0b9b8b5b8b6b4908b88887467aa8f7ea78976ad8973b08b74b59885b69e8eb29888b1917cb1917db1937fae907cb19686b39a8ab29886b59b8ab8a192b6aaa3b7b2afbcb8b4bcb8b5bbb7b4c0bcb9bebab7c0bcb9b6b5b2b6b5b3b4b3b0bab9b7b7b6b4b1b0ae7b716ba083709b806f716158967764b08870b29481b69b8ab69f8fb39a89b69f90b49d8db39a89b29988b49c8cb6a090b8a496baa49593867f8f8986bfbbb7bdb9b5bcb7b4bab6b3b9b5b2bab6b2b4b3b1b3b3b0b6b5b3b8b7b5b4b2b0a7a5a38f837dae917ea084725a504c63544da28370b39784b59e8db2a093a698909b918b998e8790857e95877dad998bb39c8cb5a091b9a2938d827c95908dbebab6bbb7b3bdbab7bbb7b4bdb9b6bbb7b4b4b3b0b5b4b1b8b7b5b6b5b3b8b8b5b4b2af968f8ab29a8bab9485544b483a323073655d96887f70655f61595547403e453e3c453f3d57504f655e5b90847db39c8db7a090b6a09189807aaba6a3bdb9b6c0bcb9bebab7bcb7b4bebab7bbb7b4b3b2b0b6b5b3b2b1afb7b6b4b8b7b4b5b4b1aeaba8b5a89fac998d4d44412d25244d46444e4744322b293a3230423937433a37352d2a59504c534b48524a48988a81b59f8fb19c8d827974b2afacbdb9b5bcb8b4bdb9b5bcb8b5bdb9b6bab6b2b8b7b5b5b4b2b6b6b3b9b8b5b7b6b3b6b5b2b8b6b3b9b4b1b2a9a26c64612d25242d2625312a28352d2c453d3a78675c8d7a6ea09792aea6a0615854332b29524a479f8e82b09d90a49b96c1bdb9bebab7bfbbb8bbb8b4b9b5b1b8b4b0b9b4b0b7b6b4b8b7b5b8b7b4b6b5b3b8b6b3bab9b6b9b8b5b4b3b0b7b5b2a5a29f453d3b261e1d261f1e2e2625413936857268977865b19482b5a69caca5a07c7572453d3b746963a0948cc5bfbbc0bbb8beb9b6bbb7b3bbb6b3b7b3afb8b4b0b9b5b1b7b6b3b6b5b3b5b4b2b5b4b2b7b6b3b7b6b3b8b6b3b4b2afb7b6b3b3b1ae6d6765251f1e1e18172a22212d2523443b3971625ab19888b09482a89182877e792c25243e3634766d6abeb9b5bfbbb7bebab6bcb7b3bbb6b3b9b5b1b7b3afb8b4b0b4b3b0b5b4b1b5b4b1b4b3b1b5b4b2b8b6b4b5b3b0b9b6b4b5b4b1b6b4b27f79762a2322221c1b2d2524221b1a443e3c47413f6f676281766f867971675e5a3e37352a222166605dbab7b3bdb9b5beb9b5bcb7b3bcb7b3b9b4b0bab6b2bab6b2b5b3b0b6b4b2b3b2afb7b6b3b4b4b1b4b3b0b6b4b1b5b4b1b4b3b0b9b6b29a8c8252474230292828201f181212322c2c231e1d1c16162c26252923222d26252d2523332b2a8e8885bcb8b5bcb7b3bbb6b2bcb7b3b9b4b1b9b5b1b7b2afb7b2ae7a838e9b9b9caeadacb3b2b0b3b2afb7b7b4b6b5b3b6b6b3b7b6b3b9ada4a991808e7b6f50453f2b24231a14142923221f19181d17161f18182620201d17162a22215d5654b7b3b0bbb7b3bbb6b2b8b4b0bab5b1bbb6b2bab5b1b8b4b0bab6b22c496b4c5d735f68766e727a828285929090adaba8b7b2aeb6a59ab39682a28470a387748e76674e403a1a14141d1716181211221c1c1f1918221c1b2f2827342d2c8d8884bab6b3b9b5b2bab5b1bab5b1b9b4b0bab6b2b8b4b0b9b4b0b7b2ae325e8b365f8a3a5d833f5b7a545f70646469706b6aa08f84b08e78b18e769f7e689e7f6b9e816d907766584940362d2a1c1615201b1a1a1413201a1a251e1d393331a39e9bbab5b1bcb7b3bab6b2b8b3afb8b4b0b9b4b0b9b4b1bab5b2b5b0ac3d6c9843729d44719c426e98415f805a64716f6a699d8677b1927eb3947faa89749d7a649f7f6ba487749e837186716454463f2c25231e181837302e3a33317a7471beb9b6bcb8b4bbb6b2b6b2aebab5b1b9b5b1b8b3afbab6b2b6b1adb5aeaa4877a14c7aa44e7ba345719a3a5d80586b7f767475927b6eb1927faf8e79b08e78a78169a07861a17f6aa58570a688749b83738270666f66618a8480a49e99b7b2aebab6b2bcb8b4b9b5b1b7b2aebab5b1b9b4b0b6b1aeb6b1adb2aca8b2aca84876a04a78a2517fa74771973a5d80405c7a6161677c695fac8a75b08d77b4917aaf8971ad876fa5816aa6846ea78670a98a76ac9484ab9f96b2aca8bdb8b4bcb7b3bcb8b4bcb8b4b8b3afb7b2aeb9b4b0b8b3afb8b2aeb6afabb3aeaab2aeaa4878a14b7aa34c7ba44a759b3d63873b5f825b67766f5f569c7e6caf8c77b18f79b28f78b5927caf8e78a98872aa8a76a98a76ac917fada199b7b0acb9b3afbfb9b5c1bab6bdb6b2b8b3afbab5b1b9b4b0b6afabb7b1adb3ada9b3aeaab0aba8";

const DAX_IMG_WIDTH: usize = 32;
const DAX_IMG_HEIGHT: usize = 32;

/// Parse the hex-encoded RGB image into pixel data
fn parse_dax_pixels() -> Vec<Vec<(u8, u8, u8)>> {
    let hex = DAX_HEX;
    let mut pixels = Vec::with_capacity(DAX_IMG_HEIGHT);
    for y in 0..DAX_IMG_HEIGHT {
        let mut row = Vec::with_capacity(DAX_IMG_WIDTH);
        for x in 0..DAX_IMG_WIDTH {
            let idx = (y * DAX_IMG_WIDTH + x) * 6;
            let r = u8::from_str_radix(&hex[idx..idx + 2], 16).unwrap_or(0);
            let g = u8::from_str_radix(&hex[idx + 2..idx + 4], 16).unwrap_or(0);
            let b = u8::from_str_radix(&hex[idx + 4..idx + 6], 16).unwrap_or(0);
            row.push((r, g, b));
        }
        pixels.push(row);
    }
    pixels
}

/// Build the image lines using half-block ▄ with fg=bottom, bg=top pixel
fn build_dax_image() -> Vec<String> {
    let pixels = parse_dax_pixels();
    let mut lines = Vec::new();

    for row in (0..DAX_IMG_HEIGHT).step_by(2) {
        let mut line = String::new();
        for x in 0..DAX_IMG_WIDTH {
            let (tr, tg, tb) = pixels[row][x];
            let (br, bg_val, bb) = pixels
                .get(row + 1)
                .and_then(|r| r.get(x))
                .copied()
                .unwrap_or((tr, tg, tb));
            // fg = bottom pixel, bg = top pixel
            line.push_str(&format!(
                "\x1b[38;2;{};{};{}m\x1b[48;2;{};{};{}m▄",
                br, bg_val, bb, tr, tg, tb
            ));
        }
        line.push_str("\x1b[0m");
        lines.push(line);
    }
    lines
}

/// Center a string that may contain ANSI escape codes within the given width
fn center_ansi(s: &str, width: usize) -> String {
    // Count visible characters (strip ANSI) - use chars().count() for Unicode
    let visible_len = strip_ansi(s).chars().count();
    let left = if width > visible_len {
        (width - visible_len) / 2
    } else {
        0
    };
    format!("{}{}", " ".repeat(left), s)
}

// ── Dynamic Border ────────────────────────────────────────────────────────────

/// Dynamic border that adjusts to viewport width.
///
/// Renders a horizontal line using box-drawing characters, optionally colored.
pub struct DynamicBorder {
    /// Optional color function name – if None, uses default dim styling
    color_fn: Option<BorderStyle>,
}

/// Border styling options
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum BorderStyle {
    /// Accent color (blue/cyan)
    Accent,
    /// Muted/dim color
    Muted,
    /// Success color (green)
    Success,
    /// Error color (red)
    Error,
    /// Custom 256-color index
    Custom(u8),
}

impl DynamicBorder {
    /// Create a new dynamic border with default (dim) styling
    pub fn new() -> Self {
        Self { color_fn: None }
    }

    /// Create with a specific border style
    pub fn with_style(style: BorderStyle) -> Self {
        Self {
            color_fn: Some(style),
        }
    }

    /// Create with accent color
    pub fn accent() -> Self {
        Self::with_style(BorderStyle::Accent)
    }

    /// Render the border at the given width
    pub fn render(&self, width: usize) -> Vec<String> {
        let line = "─".repeat(width.max(1));
        let colored = match self.color_fn {
            Some(BorderStyle::Accent) => format!("\x1b[38;5;75m{}\x1b[0m", line),
            Some(BorderStyle::Muted) => format!("\x1b[90m{}\x1b[0m", line),
            Some(BorderStyle::Success) => format!("\x1b[32m{}\x1b[0m", line),
            Some(BorderStyle::Error) => format!("\x1b[31m{}\x1b[0m", line),
            Some(BorderStyle::Custom(c)) => format!("\x1b[38;5;{}m{}\x1b[0m", c, line),
            None => format!("\x1b[90m{}\x1b[0m", line),
        };
        vec![colored]
    }
}

impl Default for DynamicBorder {
    fn default() -> Self {
        Self::new()
    }
}

// ── Earendil Announcement ────────────────────────────────────────────────────

/// Announcement/notification display component.
///
/// Renders a framed announcement with a title, optional body text, and borders.
/// Ported from the TypeScript earendil-announcement component.
pub struct EarendilAnnouncement {
    /// Title of the announcement (bold, accent color)
    pub title: String,
    /// Body lines to display under the title
    pub body: Vec<String>,
    /// Optional link to display
    pub link: Option<String>,
    /// Border style
    pub border_style: BorderStyle,
}

impl EarendilAnnouncement {
    /// Create a new announcement with title and body
    pub fn new(title: impl Into<String>) -> Self {
        Self {
            title: title.into(),
            body: Vec::new(),
            link: None,
            border_style: BorderStyle::Accent,
        }
    }

    /// Create the default Earendil announcement
    pub fn earendil_default() -> Self {
        Self {
            title: "pi has joined Earendil".to_string(),
            body: vec![
                "Read the blog post:".to_string(),
                "https://mariozechner.at/posts/2026-04-08-ive-sold-out/".to_string(),
            ],
            link: Some("https://mariozechner.at/posts/2026-04-08-ive-sold-out/".to_string()),
            border_style: BorderStyle::Accent,
        }
    }

    /// Add a body line
    pub fn add_body(&mut self, line: impl Into<String>) {
        self.body.push(line.into());
    }

    /// Set link
    pub fn with_link(mut self, link: impl Into<String>) -> Self {
        self.link = Some(link.into());
        self
    }

    /// Render the announcement at the given width
    pub fn render(&self, width: usize) -> Vec<String> {
        let border = DynamicBorder::with_style(self.border_style);
        let accent = "\x1b[38;5;75m";
        let muted = "\x1b[90m";
        let link_color = "\x1b[36m";
        let bold = "\x1b[1m";
        let reset = "\x1b[0m";

        let mut lines = Vec::new();

        // Top border
        lines.extend(border.render(width));

        // Title (bold, accent)
        lines.push(format!(" {}{}{}{}", accent, bold, self.title, reset));

        // Spacer
        lines.push(String::new());

        // Body lines
        for body_line in &self.body {
            // Detect if it's a URL
            if body_line.starts_with("http://") || body_line.starts_with("https://") {
                lines.push(format!(" {}{}{}", link_color, body_line, reset));
            } else {
                lines.push(format!(" {}{}{}", muted, body_line, reset));
            }
        }

        // Spacer
        lines.push(String::new());

        // Bottom border
        lines.extend(border.render(width));

        lines
    }
}

// ── Enhanced Tool Execution ──────────────────────────────────────────────────

/// Extended tool execution with timing, progress, and rich status display.
///
/// Enhances the basic `ToolExecution` struct with:
/// - Elapsed time tracking
/// - Progress indicators
/// - Collapsible sections for arguments and output
/// - Timing information display
/// - Detailed status rendering
#[derive(Debug, Clone)]
pub struct ToolExecutionDisplay {
    /// Tool name
    pub tool_name: String,
    /// Tool call ID
    pub tool_call_id: String,
    /// Parsed arguments
    pub arguments: serde_json::Value,
    /// Current execution state
    pub state: ToolExecutionState,
    /// Result (if complete)
    pub result: Option<ToolResult>,
    /// Whether the display is expanded
    pub expanded: bool,
    /// Whether to show images
    pub show_images: bool,
    /// Max image width in cells
    pub image_width_cells: usize,
    /// When execution started
    pub started_at: Option<std::time::Instant>,
    /// When execution completed
    pub completed_at: Option<std::time::Instant>,
    /// Whether arguments parsing is complete
    pub args_complete: bool,
    /// Whether the call is still partial (streaming args)
    pub is_partial: bool,
}

impl ToolExecutionDisplay {
    /// Create a new tool execution display
    pub fn new(
        tool_name: impl Into<String>,
        tool_call_id: impl Into<String>,
        args: serde_json::Value,
    ) -> Self {
        Self {
            tool_name: tool_name.into(),
            tool_call_id: tool_call_id.into(),
            arguments: args,
            state: ToolExecutionState::Pending,
            result: None,
            expanded: false,
            show_images: true,
            image_width_cells: 60,
            started_at: None,
            completed_at: None,
            args_complete: false,
            is_partial: true,
        }
    }

    /// Mark execution as started
    pub fn start(&mut self) {
        self.state = ToolExecutionState::Running;
        self.started_at = Some(std::time::Instant::now());
    }

    /// Mark arguments as complete
    pub fn set_args_complete(&mut self) {
        self.args_complete = true;
        self.is_partial = false;
    }

    /// Update arguments (for streaming)
    pub fn update_args(&mut self, args: serde_json::Value) {
        self.arguments = args;
    }

    /// Complete with result
    pub fn complete(&mut self, result: ToolResult) {
        self.state = if result.is_error {
            ToolExecutionState::Error
        } else {
            ToolExecutionState::Success
        };
        self.completed_at = Some(std::time::Instant::now());
        self.result = Some(result);
    }

    /// Set expanded state
    pub fn set_expanded(&mut self, expanded: bool) {
        self.expanded = expanded;
    }

    /// Toggle expanded
    pub fn toggle_expanded(&mut self) {
        self.expanded = !self.expanded;
    }

    /// Get elapsed time as a formatted string
    pub fn elapsed_str(&self) -> String {
        let start = match self.started_at {
            Some(s) => s,
            None => return String::new(),
        };
        let end = self.completed_at.unwrap_or_else(std::time::Instant::now);
        let dur = end.duration_since(start);
        let ms = dur.as_millis();
        if ms < 1000 {
            format!("{}ms", ms)
        } else {
            format!("{:.1}s", dur.as_secs_f64())
        }
    }

    /// Format arguments for display
    pub fn format_arguments(&self) -> String {
        if self.arguments.is_null() {
            String::new()
        } else {
            serde_json::to_string_pretty(&self.arguments)
                .unwrap_or_else(|_| self.arguments.to_string())
        }
    }

    /// Render the full tool execution display
    pub fn render(&self) -> Vec<String> {
        let mut lines = Vec::new();
        let reset = "\x1b[0m";

        // Determine colors based on state
        let (bg_color, fg_color, status_icon, status_label) = match self.state {
            ToolExecutionState::Pending => ("\x1b[48;5;240m", "\x1b[38;5;250m", "○", "pending"),
            ToolExecutionState::Running => ("\x1b[48;5;239m", "\x1b[38;5;250m", "◐", "running"),
            ToolExecutionState::Success => ("\x1b[48;5;28m", "\x1b[38;5;255m", "●", "done"),
            ToolExecutionState::Error => ("\x1b[48;5;196m", "\x1b[38;5;255m", "✗", "error"),
        };

        // ── Header line: status icon + tool name + elapsed time ──
        let elapsed = self.elapsed_str();
        let header = if elapsed.is_empty() {
            format!(
                "{} {} {}{}{} {}{}{}",
                bg_color,
                status_icon,
                fg_color,
                "\x1b[1m",
                self.tool_name,
                fg_color,
                status_label,
                reset
            )
        } else {
            format!(
                "{} {} {}{}{} {}{}{} \x1b[90m{}{}",
                bg_color,
                status_icon,
                fg_color,
                "\x1b[1m",
                self.tool_name,
                fg_color,
                status_label,
                "\x1b[90m",
                elapsed,
                reset
            )
        };
        lines.push(header);

        // ── Arguments section ──
        let args_str = self.format_arguments();
        if !args_str.is_empty() {
            if self.expanded || args_str.len() < 200 {
                for arg_line in args_str.lines() {
                    lines.push(format!("  \x1b[38;5;246m{}{}", arg_line, reset));
                }
            } else {
                // Show first few lines, then truncation
                let arg_lines: Vec<&str> = args_str.lines().collect();
                let show = 3.min(arg_lines.len());
                for line in &arg_lines[..show] {
                    lines.push(format!("  \x1b[38;5;246m{}{}", line, reset));
                }
                if arg_lines.len() > show {
                    lines.push(format!(
                        "  \x1b[90m... ({} more lines, press → to expand){}",
                        arg_lines.len() - show,
                        reset
                    ));
                }
            }
        }

        // ── Partial indicator ──
        if self.is_partial && self.state == ToolExecutionState::Pending {
            lines.push(format!("  \x1b[90m(receiving arguments...){}", reset));
        }

        // ── Result section ──
        if let Some(ref result) = self.result {
            let result_fg = if result.is_error {
                "\x1b[31m"
            } else {
                "\x1b[38;5;250m"
            };

            if let Some(text) = result.get_text() {
                if self.expanded {
                    for text_line in text.lines() {
                        lines.push(format!("  {}{}{}", result_fg, text_line, reset));
                    }
                } else {
                    let truncated = truncate_text(&text, 500);
                    for text_line in truncated.lines() {
                        lines.push(format!("  {}{}{}", result_fg, text_line, reset));
                    }
                    if text.len() > 500 {
                        lines.push(format!(
                            "  \x1b[90m(... {} more chars, press → to expand){}",
                            text.len() - 500,
                            reset
                        ));
                    }
                }
            }

            // Image count
            if result.has_images() && self.show_images {
                lines.push(format!(
                    "  \x1b[38;5;246m[{} image{}]{}",
                    result.image_count(),
                    if result.image_count() == 1 { "" } else { "s" },
                    reset
                ));
            }
        } else if self.state == ToolExecutionState::Running {
            lines.push(format!("  \x1b[90m⏳ executing...{}", reset));
        }

        lines
    }

    /// Render as a single string (joined with newlines)
    pub fn render_to_string(&self) -> String {
        self.render().join("\n")
    }
}

// ── Interactive Message Components ─────────────────────────────────────────
//
// Ported from pi-mono/packages/coding-agent/src/modes/interactive/components/
// These components handle user messages, skill invocations, diff rendering,
// keybinding hints, footer, visual truncation, image selection, and countdown.

/// OSC 133 prompt escape sequences for terminal integration
const OSC133_ZONE_START: &str = "\x1b]133;A\x07";
const OSC133_ZONE_END: &str = "\x1b]133;B\x07";
const OSC133_ZONE_FINAL: &str = "\x1b]133;C\x07";

/// ANSI color helpers using theme-like colors for TUI rendering.
/// These are inline helpers for components that produce `Vec<String>` output.
#[allow(dead_code)]
mod ansi {
    /// Red foreground
    pub fn red(text: &str) -> String {
        format!("\x1b[31m{}\x1b[0m", text)
    }

    /// Green foreground
    pub fn green(text: &str) -> String {
        format!("\x1b[32m{}\x1b[0m", text)
    }

    /// Yellow foreground
    pub fn yellow(text: &str) -> String {
        format!("\x1b[33m{}\x1b[0m", text)
    }

    /// Blue foreground
    pub fn blue(text: &str) -> String {
        format!("\x1b[34m{}\x1b[0m", text)
    }

    /// Cyan foreground
    pub fn cyan(text: &str) -> String {
        format!("\x1b[36m{}\x1b[0m", text)
    }

    /// Magenta/purple foreground
    pub fn magenta(text: &str) -> String {
        format!("\x1b[35m{}\x1b[0m", text)
    }

    /// Bold text
    pub fn bold(text: &str) -> String {
        format!("\x1b[1m{}\x1b[0m", text)
    }

    /// Italic text
    pub fn italic(text: &str) -> String {
        format!("\x1b[3m{}\x1b[0m", text)
    }

    /// Dim/faint text
    pub fn dim(text: &str) -> String {
        format!("\x1b[2m{}\x1b[0m", text)
    }

    /// Inverse/reverse video
    pub fn inverse(text: &str) -> String {
        format!("\x1b[7m{}\x1b[0m", text)
    }

    /// User message background (subtle blue tint)
    pub fn user_message_bg(text: &str) -> String {
        format!("\x1b[48;5;17m{}\x1b[0m", text)
    }

    /// User message text color
    pub fn user_message_text(text: &str) -> String {
        format!("\x1b[38;5;189m{}\x1b[0m", text)
    }

    /// Skill/custom message label color (purple)
    pub fn custom_label(text: &str) -> String {
        format!("\x1b[38;5;183m{}\x1b[0m", text)
    }

    /// Skill/custom message text
    pub fn custom_text(text: &str) -> String {
        format!("\x1b[38;5;254m{}\x1b[0m", text)
    }

    /// Diff context line color (dim gray)
    pub fn diff_context(text: &str) -> String {
        format!("\x1b[38;5;244m{}\x1b[0m", text)
    }

    /// Diff removed line color (red)
    pub fn diff_removed(text: &str) -> String {
        format!("\x1b[31m{}\x1b[0m", text)
    }

    /// Diff added line color (green)
    pub fn diff_added(text: &str) -> String {
        format!("\x1b[32m{}\x1b[0m", text)
    }

    /// Thinking text color
    pub fn thinking_text(text: &str) -> String {
        format!("\x1b[38;5;180m{}\x1b[0m", text)
    }

    /// Muted text (for hints, labels)
    pub fn muted(text: &str) -> String {
        format!("\x1b[38;5;243m{}\x1b[0m", text)
    }

    /// Accent color (for cursors, highlights)
    pub fn accent(text: &str) -> String {
        format!("\x1b[38;5;183m{}\x1b[0m", text)
    }

    /// Error color
    pub fn error(text: &str) -> String {
        format!("\x1b[38;5;203m{}\x1b[0m", text)
    }

    /// Warning color
    pub fn warning(text: &str) -> String {
        format!("\x1b[38;5;215m{}\x1b[0m", text)
    }
}

// ─────────────────────────────────────────────────────────────────────────────
// 1. UserMessageRenderer
// ─────────────────────────────────────────────────────────────────────────────

/// User message item for the message selector.
#[derive(Debug, Clone)]
pub struct UserMessageItem {
    /// Entry ID in the session.
    pub id: String,
    /// The message text.
    pub text: String,
    /// Optional timestamp.
    pub timestamp: Option<String>,
}

/// Renders a user message with optional image indicators and OSC 133 markers.
pub struct UserMessageRenderer {
    /// The message text (may contain markdown).
    pub text: String,
    /// Whether the message has associated images.
    pub has_images: bool,
    /// Whether to emit OSC 133 prompt markers.
    pub use_osc133: bool,
}

impl UserMessageRenderer {
    pub fn new(text: impl Into<String>) -> Self {
        Self {
            text: text.into(),
            has_images: false,
            use_osc133: false,
        }
    }

    /// Set whether the message has images.
    pub fn with_images(mut self, has_images: bool) -> Self {
        self.has_images = has_images;
        self
    }

    /// Enable OSC 133 markers.
    pub fn with_osc133(mut self, enable: bool) -> Self {
        self.use_osc133 = enable;
        self
    }

    /// Render the user message to lines.
    pub fn render(&self, width: usize) -> Vec<String> {
        let mut lines = Vec::new();

        // Image indicator
        if self.has_images {
            lines.push(ansi::cyan(&format!("  📎 (has images)")));
        }

        // Render text with user message styling
        let rendered = ansi::user_message_text(&self.text);
        for line in rendered.lines() {
            // Truncate to width
            let truncated = if line.chars().count() > width {
                let mut s = String::new();
                let mut w = 0;
                for c in line.chars() {
                    if w + 1 > width {
                        s.push_str("...");
                        break;
                    }
                    s.push(c);
                    w += 1;
                }
                s
            } else {
                line.to_string()
            };
            lines.push(ansi::user_message_bg(&truncated));
        }

        // Wrap with OSC 133 markers
        if self.use_osc133 && !lines.is_empty() {
            lines[0] = format!("{}{}", OSC133_ZONE_START, lines[0]);
            let last = lines.len() - 1;
            lines[last] = format!("{}{}{}", OSC133_ZONE_END, OSC133_ZONE_FINAL, lines[last]);
        }

        lines
    }
}

// ─────────────────────────────────────────────────────────────────────────────
// 2. UserMessageSelector
// ─────────────────────────────────────────────────────────────────────────────

/// State for selecting/editing previous user messages.
#[derive(Debug, Clone)]
pub struct UserMessageSelector {
    /// Messages to choose from (oldest to newest).
    pub messages: Vec<UserMessageItem>,
    /// Currently selected index.
    pub selected_index: usize,
    /// Maximum visible messages.
    pub max_visible: usize,
}

impl UserMessageSelector {
    /// Create a new selector with messages.
    pub fn new(messages: Vec<UserMessageItem>) -> Self {
        let selected_index = messages.len().saturating_sub(1);
        Self {
            messages,
            selected_index,
            max_visible: 10,
        }
    }

    /// Create with initial selected ID.
    pub fn with_initial_id(mut self, id: &str) -> Self {
        if let Some(idx) = self.messages.iter().position(|m| m.id == id) {
            self.selected_index = idx;
        }
        self
    }

    /// Move selection up (to older messages), wraps to bottom.
    pub fn move_up(&mut self) {
        if self.selected_index == 0 {
            self.selected_index = self.messages.len().saturating_sub(1);
        } else {
            self.selected_index -= 1;
        }
    }

    /// Move selection down (to newer messages), wraps to top.
    pub fn move_down(&mut self) {
        if self.messages.is_empty() {
            return;
        }
        if self.selected_index >= self.messages.len() - 1 {
            self.selected_index = 0;
        } else {
            self.selected_index += 1;
        }
    }

    /// Get the currently selected message.
    pub fn selected(&self) -> Option<&UserMessageItem> {
        self.messages.get(self.selected_index)
    }

    /// Render the selector as lines.
    pub fn render(&self, width: usize) -> Vec<String> {
        let mut lines = Vec::new();

        // Header
        lines.push(String::new()); // blank
        lines.push(ansi::bold("Fork from Message"));
        lines.push(ansi::muted(
            "Select a user message to copy the active path up to that point into a new session",
        ));
        lines.push(String::new()); // blank
        lines.push("─".repeat(width.min(60)));
        lines.push(String::new()); // blank

        if self.messages.is_empty() {
            lines.push(ansi::muted("  No user messages found"));
            return lines;
        }

        // Calculate visible range with scrolling
        let start_index = if self.selected_index < self.max_visible / 2 {
            0
        } else if self.selected_index + self.max_visible / 2 >= self.messages.len() {
            self.messages.len().saturating_sub(self.max_visible)
        } else {
            self.selected_index - self.max_visible / 2
        };
        let end_index = (start_index + self.max_visible).min(self.messages.len());

        for i in start_index..end_index {
            let msg = &self.messages[i];
            let is_selected = i == self.selected_index;

            // Normalize to single line
            let normalized: String = msg.text.chars().filter(|c| *c != '\n').collect();
            let normalized = normalized.trim();

            // Cursor + message
            let cursor = if is_selected {
                ansi::accent("› ")
            } else {
                "  ".to_string()
            };
            let max_msg_width = width.saturating_sub(2);
            let truncated_msg = truncate_str(normalized, max_msg_width);
            let message_line = if is_selected {
                format!("{}{}", cursor, ansi::bold(&truncated_msg))
            } else {
                format!("{}{}", cursor, truncated_msg)
            };
            lines.push(message_line);

            // Metadata
            let position = i + 1;
            let metadata = format!("  Message {} of {}", position, self.messages.len());
            lines.push(ansi::muted(&metadata));
            lines.push(String::new()); // blank between messages
        }

        // Scroll indicator
        if start_index > 0 || end_index < self.messages.len() {
            let scroll_info = format!("  ({}/{})", self.selected_index + 1, self.messages.len());
            lines.push(ansi::muted(&scroll_info));
        }

        // Bottom border
        lines.push(String::new());
        lines.push("─".repeat(width.min(60)));

        lines
    }
}

/// Truncate a string to a maximum visible character count, handling ANSI escape codes.
fn truncate_str(s: &str, max_width: usize) -> String {
    let mut result = String::with_capacity(s.len());
    let mut width = 0;
    let mut chars = s.chars().peekable();

    while let Some(c) = chars.next() {
        if c == '\x1b' {
            // ANSI escape sequence - include without counting width
            result.push(c);
            while let Some(&next) = chars.peek() {
                result.push(chars.next().unwrap());
                if next.is_ascii_alphabetic() {
                    break;
                }
            }
            continue;
        }

        let char_w = if is_wide_char(c) { 2 } else { 1 };
        if width + char_w > max_width {
            if width >= 3 {
                result.truncate(result.len() - 3);
                result.push_str("...");
            }
            break;
        }
        result.push(c);
        width += char_w;
    }
    result
}

/// Check if a character is wide (CJK, emoji, etc.).
fn is_wide_char(c: char) -> bool {
    let code = c as u32;
    (0xFF01..=0xFF5E).contains(&code)
        || (0x4E00..=0x9FFF).contains(&code)
        || (0x3400..=0x4DBF).contains(&code)
        || (0xFE30..=0xFE4F).contains(&code)
        || (0xFF00..=0xFFEF).contains(&code)
        || (0x3000..=0x303F).contains(&code)
}

// ─────────────────────────────────────────────────────────────────────────────
// 3. SkillInvocationMessage
// ─────────────────────────────────────────────────────────────────────────────

/// Parsed skill block data.
#[derive(Debug, Clone)]
pub struct ParsedSkillBlock {
    /// Skill name.
    pub name: String,
    /// Skill content/arguments.
    pub content: String,
}

/// Renders a skill invocation with collapsed/expanded state.
pub struct SkillInvocationMessage {
    /// The skill block to render.
    pub skill_block: ParsedSkillBlock,
    /// Whether the block is expanded.
    pub expanded: bool,
    /// Key hint for expanding.
    pub expand_key_hint: String,
}

impl SkillInvocationMessage {
    pub fn new(name: impl Into<String>, content: impl Into<String>) -> Self {
        Self {
            skill_block: ParsedSkillBlock {
                name: name.into(),
                content: content.into(),
            },
            expanded: false,
            expand_key_hint: "Enter".to_string(),
        }
    }

    /// Set expanded state.
    pub fn set_expanded(&mut self, expanded: bool) {
        self.expanded = expanded;
    }

    /// Toggle expanded state.
    pub fn toggle_expanded(&mut self) {
        self.expanded = !self.expanded;
    }

    /// Render to lines.
    pub fn render(&self, _width: usize) -> Vec<String> {
        if self.expanded {
            // Expanded: label + skill name header + full content
            let label = ansi::custom_label(&ansi::bold("[skill]"));
            let name_line = ansi::bold(&format!("**{}**", self.skill_block.name));
            let content_rendered = render_markdown(&self.skill_block.content);
            vec![
                format!("{} {}", label, name_line),
                ansi::custom_text(&content_rendered),
            ]
        } else {
            // Collapsed: single line
            let line = format!(
                "{} {} {}",
                ansi::custom_label(&ansi::bold("[skill]")),
                ansi::custom_text(&self.skill_block.name),
                ansi::dim(&format!("({} to expand)", self.expand_key_hint))
            );
            vec![line]
        }
    }
}

// ─────────────────────────────────────────────────────────────────────────────
// 4. Enhanced DiffRenderer
// ─────────────────────────────────────────────────────────────────────────────

/// Render unified diffs with color (red/green) and optional intra-line highlighting.
pub struct DiffRenderer {
    /// Enable word-level change highlighting.
    pub word_diff: bool,
}

impl DiffRenderer {
    pub fn new() -> Self {
        Self { word_diff: true }
    }

    /// Create without word diff.
    pub fn new_simple() -> Self {
        Self { word_diff: false }
    }

    /// Render a diff string with colored lines.
    pub fn render(&self, diff_text: &str, _file_path: Option<&str>) -> String {
        let lines = diff_text.lines();
        let mut result = Vec::new();
        let line_iter: Vec<&str> = lines.collect();
        let mut i = 0;

        while i < line_iter.len() {
            let line = line_iter[i];
            let parsed = parse_diff_line_ts(line);

            match parsed {
                None => {
                    // Non-diff line (e.g. hunk header or plain text)
                    if line.starts_with("@@") {
                        result.push(ansi::cyan(line));
                    } else {
                        result.push(ansi::diff_context(line));
                    }
                    i += 1;
                }
                Some(DiffLineParsed::Context { line_num, content }) => {
                    let content = replace_tabs(&content);
                    result.push(ansi::diff_context(&format!(" {} {}", line_num, content)));
                    i += 1;
                }
                Some(DiffLineParsed::Removed { line_num, content }) => {
                    // Collect consecutive removed lines
                    let mut removed = vec![(line_num, content)];
                    i += 1;
                    while i < line_iter.len() {
                        if let Some(DiffLineParsed::Removed { line_num, content }) =
                            parse_diff_line_ts(line_iter[i])
                        {
                            removed.push((line_num, content));
                            i += 1;
                        } else {
                            break;
                        }
                    }

                    // Collect consecutive added lines
                    let mut added = Vec::new();
                    while i < line_iter.len() {
                        if let Some(DiffLineParsed::Added { line_num, content }) =
                            parse_diff_line_ts(line_iter[i])
                        {
                            added.push((line_num, content));
                            i += 1;
                        } else {
                            break;
                        }
                    }

                    // Intra-line diff only for 1:1 (single modification)
                    if self.word_diff && removed.len() == 1 && added.len() == 1 {
                        let (rem_num, rem_content) = &removed[0];
                        let (add_num, add_content) = &added[0];
                        let (rem_line, add_line) = render_intra_line_diff(
                            &replace_tabs(rem_content),
                            &replace_tabs(add_content),
                        );
                        result.push(ansi::diff_removed(&format!("-{} {}", rem_num, rem_line)));
                        result.push(ansi::diff_added(&format!("+{} {}", add_num, add_line)));
                    } else {
                        for (num, content) in &removed {
                            result.push(ansi::diff_removed(&format!(
                                "-{} {}",
                                num,
                                replace_tabs(content)
                            )));
                        }
                        for (num, content) in &added {
                            result.push(ansi::diff_added(&format!(
                                "+{} {}",
                                num,
                                replace_tabs(content)
                            )));
                        }
                    }
                }
                Some(DiffLineParsed::Added { line_num, content }) => {
                    let content = replace_tabs(&content);
                    result.push(ansi::diff_added(&format!("+{} {}", line_num, content)));
                    i += 1;
                }
            }
        }

        result.join("\n")
    }
}

impl Default for DiffRenderer {
    fn default() -> Self {
        Self::new()
    }
}

/// Parsed diff line from tool-output format.
#[derive(Debug)]
enum DiffLineParsed {
    Context { line_num: String, content: String },
    Added { line_num: String, content: String },
    Removed { line_num: String, content: String },
}

/// Parse a diff line in the format: "+123 content" or "-123 content" or " 123 content".
fn parse_diff_line_ts(line: &str) -> Option<DiffLineParsed> {
    let chars: Vec<char> = line.chars().collect();
    if chars.is_empty() {
        return None;
    }

    let prefix = chars[0];
    if prefix != '+' && prefix != '-' && prefix != ' ' {
        return None;
    }

    // Find where the line number ends (space after digits)
    let rest: String = chars[1..].iter().collect();
    let trimmed = rest.trim_start();

    // Try to parse: line_num followed by space then content
    if let Some(space_pos) = trimmed.find(' ') {
        let line_num = &trimmed[..space_pos];
        let content = &trimmed[space_pos + 1..];
        match prefix {
            '+' => Some(DiffLineParsed::Added {
                line_num: line_num.to_string(),
                content: content.to_string(),
            }),
            '-' => Some(DiffLineParsed::Removed {
                line_num: line_num.to_string(),
                content: content.to_string(),
            }),
            ' ' => Some(DiffLineParsed::Context {
                line_num: line_num.to_string(),
                content: content.to_string(),
            }),
            _ => None,
        }
    } else {
        // No space - just prefix with possible line number
        match prefix {
            '+' => Some(DiffLineParsed::Added {
                line_num: String::new(),
                content: trimmed.to_string(),
            }),
            '-' => Some(DiffLineParsed::Removed {
                line_num: String::new(),
                content: trimmed.to_string(),
            }),
            ' ' => Some(DiffLineParsed::Context {
                line_num: String::new(),
                content: trimmed.to_string(),
            }),
            _ => None,
        }
    }
}

/// Replace tabs with spaces for consistent rendering.
fn replace_tabs(text: &str) -> String {
    text.replace('\t', "   ")
}

/// Compute word-level diff and render with inverse on changed parts.
fn render_intra_line_diff(old_content: &str, new_content: &str) -> (String, String) {
    let old_words = split_words(old_content);
    let new_words = split_words(new_content);

    // Simple word-level diff: find common prefix and suffix
    let prefix_len = common_prefix_len(&old_words, &new_words);
    let suffix_len = common_suffix_len(&old_words[prefix_len..], &new_words[prefix_len..]);

    let old_mid_end = old_words.len().saturating_sub(suffix_len);
    let new_mid_end = new_words.len().saturating_sub(suffix_len);

    let mut removed_line = String::new();
    let mut added_line = String::new();

    // Common prefix
    for w in &old_words[..prefix_len] {
        removed_line.push_str(w);
        added_line.push_str(w);
    }

    // Changed part (inverse highlight)
    for w in &old_words[prefix_len..old_mid_end] {
        removed_line.push_str(&ansi::inverse(w));
    }
    for w in &new_words[prefix_len..new_mid_end] {
        added_line.push_str(&ansi::inverse(w));
    }

    // Common suffix
    if suffix_len > 0 {
        for w in &old_words[old_mid_end..] {
            removed_line.push_str(w);
        }
        for w in &new_words[new_mid_end..] {
            added_line.push_str(w);
        }
    }

    (removed_line, added_line)
}

/// Split text into words preserving whitespace between them.
fn split_words(text: &str) -> Vec<String> {
    let mut words = Vec::new();
    let mut current = String::new();
    let mut in_whitespace = false;

    for c in text.chars() {
        let is_ws = c.is_whitespace();
        if is_ws != in_whitespace && !current.is_empty() {
            words.push(std::mem::take(&mut current));
        }
        in_whitespace = is_ws;
        current.push(c);
    }
    if !current.is_empty() {
        words.push(current);
    }
    words
}

/// Length of common prefix between two word slices.
fn common_prefix_len<'a>(a: &[String], b: &[String]) -> usize {
    a.iter().zip(b.iter()).take_while(|(x, y)| x == y).count()
}

/// Length of common suffix between two word slices.
fn common_suffix_len<'a>(a: &[String], b: &[String]) -> usize {
    a.iter()
        .rev()
        .zip(b.iter().rev())
        .take_while(|(x, y)| x == y)
        .count()
}

// ─────────────────────────────────────────────────────────────────────────────
// 5. KeybindingHints
// ─────────────────────────────────────────────────────────────────────────────

/// A single keybinding hint for display.
#[derive(Debug, Clone)]
pub struct KeyHint {
    /// Key sequence text (e.g. "Ctrl+C", "↑/↓").
    pub keys: String,
    /// Description of the action.
    pub description: String,
}

impl KeyHint {
    pub fn new(keys: impl Into<String>, description: impl Into<String>) -> Self {
        Self {
            keys: keys.into(),
            description: description.into(),
        }
    }
}

/// Renders a list of keyboard shortcut hints.
pub struct KeybindingHints;

impl KeybindingHints {
    /// Render a single key hint: dim keys + muted description.
    pub fn key_hint(keys: &str, description: &str) -> String {
        format!(
            "{}{}",
            ansi::dim(keys),
            ansi::muted(&format!(" {}", description))
        )
    }

    /// Render multiple hints as separate lines.
    pub fn render(hints: &[KeyHint]) -> Vec<String> {
        hints
            .iter()
            .map(|h| Self::key_hint(&h.keys, &h.description))
            .collect()
    }

    /// Render hints as a single horizontal line separated by spaces.
    pub fn render_inline(hints: &[KeyHint]) -> String {
        hints
            .iter()
            .map(|h| Self::key_hint(&h.keys, &h.description))
            .collect::<Vec<_>>()
            .join("  ")
    }
}

// ─────────────────────────────────────────────────────────────────────────────
// 6. FooterComponent (enhanced)
// ─────────────────────────────────────────────────────────────────────────────

/// Enhanced footer component data with all status fields from pi-mono.
#[derive(Debug, Clone)]
pub struct FooterComponentData {
    /// Current model name.
    pub model_name: String,
    /// Provider name.
    pub provider_name: String,
    /// Thinking level ("off", "low", "medium", "high").
    pub thinking_level: String,
    /// Session name (optional).
    pub session_name: Option<String>,
    /// Git branch.
    pub git_branch: Option<String>,
    /// Working directory (abbreviated with ~).
    pub pwd: Option<String>,
    /// Total input tokens.
    pub input_tokens: u64,
    /// Total output tokens.
    pub output_tokens: u64,
    /// Cache read tokens.
    pub cache_read_tokens: u64,
    /// Cache write tokens.
    pub cache_write_tokens: u64,
    /// Total cost in USD.
    pub total_cost: f64,
    /// Whether using OAuth subscription.
    pub using_subscription: bool,
    /// Context window usage percent (0.0 - 100.0).
    pub context_window_pct: f64,
    /// Context window size.
    pub context_window: u64,
    /// Whether auto-compact is enabled.
    pub auto_compact_enabled: bool,
    /// Number of available providers.
    pub available_provider_count: usize,
    /// Extension status messages.
    pub extension_statuses: Vec<(String, String)>,
}

impl Default for FooterComponentData {
    fn default() -> Self {
        Self {
            model_name: String::new(),
            provider_name: String::new(),
            thinking_level: "off".to_string(),
            session_name: None,
            git_branch: None,
            pwd: None,
            input_tokens: 0,
            output_tokens: 0,
            cache_read_tokens: 0,
            cache_write_tokens: 0,
            total_cost: 0.0,
            using_subscription: false,
            context_window_pct: 0.0,
            context_window: 0,
            auto_compact_enabled: true,
            available_provider_count: 1,
            extension_statuses: Vec::new(),
        }
    }
}

impl FooterComponentData {
    /// Format a token count for display.
    pub fn format_token_count(count: u64) -> String {
        if count < 1000 {
            count.to_string()
        } else if count < 10000 {
            format!("{:.1}k", count as f64 / 1000.0)
        } else if count < 1_000_000 {
            format!("{}k", count / 1000)
        } else if count < 10_000_000 {
            format!("{:.1}M", count as f64 / 1_000_000.0)
        } else {
            format!("{}M", count / 1_000_000)
        }
    }

    /// Render the footer as lines.
    pub fn render(&self, width: usize) -> Vec<String> {
        let mut lines = Vec::new();

        // Line 1: pwd (git branch) • session_name
        let mut pwd_parts = Vec::new();
        if let Some(ref pwd) = self.pwd {
            pwd_parts.push(pwd.clone());
        }
        if let Some(ref branch) = self.git_branch {
            pwd_parts.push(format!("({})", branch));
        }
        if let Some(ref session) = self.session_name {
            pwd_parts.push(format!("• {}", session));
        }
        let pwd_line = pwd_parts.join(" ");
        lines.push(ansi::dim(&truncate_str(&pwd_line, width.saturating_sub(3))));

        // Line 2: stats left | model right
        let mut stats_parts = Vec::new();
        if self.input_tokens > 0 {
            stats_parts.push(format!("↑{}", Self::format_token_count(self.input_tokens)));
        }
        if self.output_tokens > 0 {
            stats_parts.push(format!("↓{}", Self::format_token_count(self.output_tokens)));
        }
        if self.cache_read_tokens > 0 {
            stats_parts.push(format!(
                "R{}",
                Self::format_token_count(self.cache_read_tokens)
            ));
        }
        if self.cache_write_tokens > 0 {
            stats_parts.push(format!(
                "W{}",
                Self::format_token_count(self.cache_write_tokens)
            ));
        }

        // Cost
        if self.total_cost > 0.0 || self.using_subscription {
            let cost_str = format!(
                "${:.3}{}",
                self.total_cost,
                if self.using_subscription {
                    " (sub)"
                } else {
                    ""
                }
            );
            stats_parts.push(cost_str);
        }

        // Context window with color coding
        let auto_indicator = if self.auto_compact_enabled {
            " (auto)"
        } else {
            ""
        };
        let ctx_display = if self.context_window > 0 {
            format!(
                "{:.1}%/{}{}",
                self.context_window_pct,
                Self::format_token_count(self.context_window),
                auto_indicator
            )
        } else {
            format!("{:.1}%{}", self.context_window_pct, auto_indicator)
        };
        let ctx_colored = if self.context_window_pct > 90.0 {
            ansi::error(&ctx_display)
        } else if self.context_window_pct > 70.0 {
            ansi::warning(&ctx_display)
        } else {
            ctx_display
        };
        if self.context_window_pct > 0.0 {
            stats_parts.push(ctx_colored);
        }

        let stats_left = stats_parts.join(" ");

        // Right side: model name + thinking level
        let model_name = if self.model_name.is_empty() {
            "no-model"
        } else {
            &self.model_name
        };
        let mut right_side = model_name.to_string();
        if self.thinking_level != "off" {
            right_side = format!("{} • {}", model_name, self.thinking_level);
        }

        // Prepend provider if multiple providers
        if self.available_provider_count > 1 && !self.provider_name.is_empty() {
            let with_provider = format!("({}) {}", self.provider_name, right_side);
            let with_provider_len = visible_len(&with_provider);
            let stats_left_len = visible_len(&stats_left);
            if stats_left_len + 2 + with_provider_len <= width {
                right_side = with_provider;
            }
        }

        // Combine stats and model info with padding
        let stats_left_len = visible_len(&stats_left);
        let right_side_len = visible_len(&right_side);
        let min_padding = 2;

        let stats_line = if stats_left_len + min_padding + right_side_len <= width {
            let padding = width - stats_left_len - right_side_len;
            format!(
                "{}{}{}",
                ansi::dim(&stats_left),
                " ".repeat(padding),
                ansi::dim(&right_side)
            )
        } else if stats_left_len < width {
            let available = width - stats_left_len - min_padding;
            let truncated_right = truncate_str(&right_side, available);
            let truncated_len = visible_len(&truncated_right);
            let padding = width.saturating_sub(stats_left_len + truncated_len);
            format!(
                "{}{}{}",
                ansi::dim(&stats_left),
                " ".repeat(padding),
                ansi::dim(&truncated_right)
            )
        } else {
            ansi::dim(&stats_left)
        };

        lines.push(stats_line);

        // Line 3: extension statuses
        if !self.extension_statuses.is_empty() {
            let mut sorted = self.extension_statuses.clone();
            sorted.sort_by(|a, b| a.0.cmp(&b.0));
            let status_line: String = sorted
                .iter()
                .map(|(_, text)| {
                    // Sanitize: replace newlines/tabs with space, collapse spaces
                    text.chars()
                        .map(|c| {
                            if c == '\n' || c == '\r' || c == '\t' {
                                ' '
                            } else {
                                c
                            }
                        })
                        .collect::<String>()
                        .split_whitespace()
                        .collect::<Vec<_>>()
                        .join(" ")
                })
                .collect::<Vec<_>>()
                .join(" ");
            lines.push(truncate_str(&status_line, width.saturating_sub(3)));
        }

        lines
    }
}

/// Calculate the visible width of a string (ignoring ANSI escape codes).
fn visible_len(s: &str) -> usize {
    let mut len = 0usize;
    let chars: Vec<char> = s.chars().collect();
    let mut i = 0;
    while i < chars.len() {
        if chars[i] == '\x1b' && i + 1 < chars.len() && chars[i + 1] == '[' {
            // Skip ANSI escape sequence
            i += 2;
            while i < chars.len() {
                if chars[i].is_ascii_alphabetic() {
                    i += 1;
                    break;
                }
                // Handle intermediate parameters like ; and digits
                i += 1;
            }
        } else if chars[i] == '\x1b' && i + 1 < chars.len() && chars[i + 1] == ']' {
            // Skip OSC sequence (like \x1b]133;A\x07)
            i += 2;
            while i < chars.len() {
                if chars[i] == '\x07' || chars[i] == '\x1b' {
                    i += 1;
                    break;
                }
                i += 1;
            }
        } else {
            len += if is_wide_char(chars[i]) { 2 } else { 1 };
            i += 1;
        }
    }
    len
}

// ─────────────────────────────────────────────────────────────────────────────
// 7. VisualTruncate
// ─────────────────────────────────────────────────────────────────────────────

/// Result of truncating text to visual lines.
#[derive(Debug, Clone)]
pub struct VisualTruncateResult {
    /// The visual lines to display.
    pub visual_lines: Vec<String>,
    /// Number of visual lines that were skipped (hidden).
    pub skipped_count: usize,
}

/// Truncates text to a maximum number of visual lines from the end.
/// Accounts for line wrapping based on terminal width.
pub struct VisualTruncate;

impl VisualTruncate {
    /// Truncate text to max visual lines from the end.
    pub fn truncate(
        text: &str,
        max_visual_lines: usize,
        width: usize,
        padding_x: usize,
    ) -> VisualTruncateResult {
        if text.is_empty() {
            return VisualTruncateResult {
                visual_lines: Vec::new(),
                skipped_count: 0,
            };
        }

        // Split into logical lines, then wrap each to width
        let effective_width = width.saturating_sub(padding_x).max(1);
        let mut all_visual_lines = Vec::new();

        for logical_line in text.lines() {
            if logical_line.is_empty() {
                all_visual_lines.push(String::new());
            } else {
                // Wrap line to effective_width
                let mut current = String::new();
                let mut current_width = 0;

                for c in logical_line.chars() {
                    let cw = if is_wide_char(c) { 2 } else { 1 };
                    if current_width + cw > effective_width {
                        all_visual_lines.push(std::mem::take(&mut current));
                        current_width = 0;
                    }
                    current.push(c);
                    current_width += cw;
                }
                if !current.is_empty() {
                    all_visual_lines.push(current);
                }
            }
        }

        if all_visual_lines.len() <= max_visual_lines {
            return VisualTruncateResult {
                visual_lines: all_visual_lines,
                skipped_count: 0,
            };
        }

        let truncated_lines: Vec<String> =
            all_visual_lines.split_off(all_visual_lines.len() - max_visual_lines);
        let skipped_count = all_visual_lines.len();

        VisualTruncateResult {
            visual_lines: truncated_lines,
            skipped_count,
        }
    }
}

// ─────────────────────────────────────────────────────────────────────────────
// 8. ShowImagesSelector
// ─────────────────────────────────────────────────────────────────────────────

/// State for toggling image display.
#[derive(Debug, Clone)]
pub struct ShowImagesSelector {
    /// Current value (true = show images, false = hide).
    pub current_value: bool,
    /// Available options.
    pub options: Vec<ShowImagesOption>,
    /// Selected index.
    pub selected_index: usize,
}

/// An option in the show images selector.
#[derive(Debug, Clone)]
pub struct ShowImagesOption {
    /// Value to set ("yes" or "no").
    pub value: String,
    /// Display label.
    pub label: String,
    /// Description.
    pub description: String,
}

impl ShowImagesSelector {
    pub fn new(current_value: bool) -> Self {
        Self {
            current_value,
            options: vec![
                ShowImagesOption {
                    value: "yes".to_string(),
                    label: "Yes".to_string(),
                    description: "Show images inline in terminal".to_string(),
                },
                ShowImagesOption {
                    value: "no".to_string(),
                    label: "No".to_string(),
                    description: "Show text placeholder instead".to_string(),
                },
            ],
            selected_index: if current_value { 0 } else { 1 },
        }
    }

    /// Move selection up.
    pub fn move_up(&mut self) {
        if self.selected_index > 0 {
            self.selected_index -= 1;
        }
    }

    /// Move selection down.
    pub fn move_down(&mut self) {
        if self.selected_index < self.options.len() - 1 {
            self.selected_index += 1;
        }
    }

    /// Get the selected option.
    pub fn selected(&self) -> &ShowImagesOption {
        &self.options[self.selected_index]
    }

    /// Confirm selection, returns true if show images.
    pub fn confirm(&self) -> bool {
        self.options[self.selected_index].value == "yes"
    }

    /// Render the selector.
    pub fn render(&self, width: usize) -> Vec<String> {
        let mut lines = Vec::new();

        // Top border
        lines.push("─".repeat(width.min(60)));

        // Options
        for (i, option) in self.options.iter().enumerate() {
            let marker = if i == self.selected_index {
                ansi::accent("›")
            } else {
                " ".to_string()
            };
            let label = if i == self.selected_index {
                ansi::bold(&option.label)
            } else {
                option.label.clone()
            };
            let desc = ansi::muted(&option.description);
            lines.push(format!(" {} {}  {}", marker, label, desc));
        }

        // Bottom border
        lines.push("─".repeat(width.min(60)));

        lines
    }
}

// ─────────────────────────────────────────────────────────────────────────────
// 9. CountdownTimer
// ─────────────────────────────────────────────────────────────────────────────

/// Countdown timer for timeouts, renders as a display string.
#[derive(Debug, Clone)]
pub struct CountdownTimer {
    /// Total seconds remaining.
    pub remaining_seconds: u64,
    /// Total seconds the timer was initialized with.
    pub total_seconds: u64,
}

impl CountdownTimer {
    /// Create a new countdown timer.
    pub fn new(timeout_seconds: u64) -> Self {
        Self {
            remaining_seconds: timeout_seconds,
            total_seconds: timeout_seconds,
        }
    }

    /// Create from milliseconds.
    pub fn from_millis(timeout_ms: u64) -> Self {
        Self::new((timeout_ms + 999) / 1000)
    }

    /// Tick the timer down by one second.
    /// Returns true if the timer has expired.
    pub fn tick(&mut self) -> bool {
        if self.remaining_seconds > 0 {
            self.remaining_seconds -= 1;
        }
        self.remaining_seconds == 0
    }

    /// Check if the timer has expired.
    pub fn is_expired(&self) -> bool {
        self.remaining_seconds == 0
    }

    /// Render the countdown display.
    pub fn render(&self) -> String {
        if self.remaining_seconds == 0 {
            ansi::error("⏰ Expired").to_string()
        } else if self.remaining_seconds <= 5 {
            ansi::warning(&format!("⏳ {}s remaining", self.remaining_seconds))
        } else {
            ansi::dim(&format!("⏳ {}s remaining", self.remaining_seconds))
        }
    }

    /// Render as a progress bar with the countdown.
    pub fn render_bar(&self, width: usize) -> String {
        if self.total_seconds == 0 {
            return String::new();
        }
        let fraction = self.remaining_seconds as f64 / self.total_seconds as f64;
        let filled = ((width as f64) * fraction) as usize;
        let empty = width - filled;

        let bar = if self.remaining_seconds <= 5 {
            format!("{}{}", "█".repeat(filled), "░".repeat(empty))
        } else {
            format!("{}{}", "█".repeat(filled), "░".repeat(empty))
        };

        format!("{} {}", ansi::dim(&bar), self.render())
    }
}

// ── PKCE Helper Functions ────────────────────────────────────────────────────

use base64::engine::general_purpose::URL_SAFE_NO_PAD;
use base64::Engine;
use sha2::{Digest, Sha256};

/// Generate a cryptographically-random code_verifier (43 chars, RFC 7636 §4.1).
pub fn generate_code_verifier() -> String {
    let mut bytes = [0u8; 32]; // 32 bytes → 43 base64url chars
    rand::thread_rng().fill_bytes(&mut bytes);
    URL_SAFE_NO_PAD.encode(bytes)
}

/// Derive the code_challenge from a code_verifier using S256 (SHA-256 + base64url).
pub fn derive_code_challenge(verifier: &str) -> String {
    let mut hasher = Sha256::new();
    hasher.update(verifier.as_bytes());
    let hash = hasher.finalize();
    URL_SAFE_NO_PAD.encode(hash)
}

/// Synchronous version of derive_code_challenge for use in non-async contexts
fn derive_code_challenge_sync(verifier: &str) -> String {
    derive_code_challenge(verifier)
}

/// Generate an opaque state parameter (22 random base64url chars).
fn generate_state_token() -> String {
    let mut bytes = [0u8; 16];
    rand::thread_rng().fill_bytes(&mut bytes);
    URL_SAFE_NO_PAD.encode(bytes)
}

// ── Selector Components ─────────────────────────────────────────────────────
//
// Interactive mode selector components ported from pi-mono.
// These are rendering helper structs that produce Vec<String> lines for terminal display.
// Each selector supports fuzzy search filtering and keyboard navigation.

/// Supported thinking levels for reasoning-capable models.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize, Default)]
pub enum ThinkingLevel {
    #[default]
    Off,
    Minimal,
    Low,
    Medium,
    High,
    XHigh,
}

impl ThinkingLevel {
    pub fn as_str(&self) -> &'static str {
        match self {
            ThinkingLevel::Off => "off",
            ThinkingLevel::Minimal => "minimal",
            ThinkingLevel::Low => "low",
            ThinkingLevel::Medium => "medium",
            ThinkingLevel::High => "high",
            ThinkingLevel::XHigh => "xhigh",
        }
    }

    pub fn description(&self) -> &'static str {
        match self {
            ThinkingLevel::Off => "No reasoning",
            ThinkingLevel::Minimal => "Very brief reasoning (~1k tokens)",
            ThinkingLevel::Low => "Light reasoning (~2k tokens)",
            ThinkingLevel::Medium => "Moderate reasoning (~8k tokens)",
            ThinkingLevel::High => "Deep reasoning (~16k tokens)",
            ThinkingLevel::XHigh => "Maximum reasoning (~32k tokens)",
        }
    }

    pub fn all() -> Vec<ThinkingLevel> {
        vec![
            ThinkingLevel::Off,
            ThinkingLevel::Minimal,
            ThinkingLevel::Low,
            ThinkingLevel::Medium,
            ThinkingLevel::High,
            ThinkingLevel::XHigh,
        ]
    }

    /// Get the rank of this thinking level (0=Off, 5=XHigh).
    /// Used for model-specific level clamping.
    pub fn rank(&self) -> usize {
        match self {
            ThinkingLevel::Off => 0,
            ThinkingLevel::Minimal => 1,
            ThinkingLevel::Low => 2,
            ThinkingLevel::Medium => 3,
            ThinkingLevel::High => 4,
            ThinkingLevel::XHigh => 5,
        }
    }

    pub fn from_str(s: &str) -> Option<Self> {
        match s.to_lowercase().as_str() {
            "off" => Some(ThinkingLevel::Off),
            "minimal" => Some(ThinkingLevel::Minimal),
            "low" => Some(ThinkingLevel::Low),
            "medium" => Some(ThinkingLevel::Medium),
            "high" => Some(ThinkingLevel::High),
            "xhigh" => Some(ThinkingLevel::XHigh),
            _ => None,
        }
    }
}

impl std::fmt::Display for ThinkingLevel {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        write!(f, "{}", self.as_str())
    }
}

// ── Simple fuzzy matching for selector components ─────────────────────────────

/// Simple fuzzy score: returns a match quality score (higher = better), or None if no match.
/// Matches characters in order, allowing gaps.
pub fn fuzzy_score(query: &str, text: &str) -> Option<usize> {
    let query_lower = query.to_lowercase();
    let text_lower = text.to_lowercase();

    // Quick substring check - highest priority
    if text_lower.contains(&query_lower) {
        return Some(1000 + query.len());
    }

    // Fuzzy character matching
    let mut query_chars = query_lower.chars().peekable();
    let mut score = 0;
    let mut last_match_pos = 0;

    for (i, tc) in text_lower.char_indices() {
        if let Some(&qc) = query_chars.peek() {
            if tc == qc {
                // Bonus for matching at word boundaries
                if i == 0
                    || text.as_bytes().get(i - 1) == Some(&b' ')
                    || text.as_bytes().get(i - 1) == Some(&b'/')
                {
                    score += 10;
                } else if i == last_match_pos + 1 {
                    // Bonus for consecutive matches
                    score += 5;
                } else {
                    score += 1;
                }
                last_match_pos = i;
                query_chars.next();
            }
        }
    }

    if query_chars.peek().is_none() {
        Some(score)
    } else {
        None
    }
}

/// Filter a list of items by fuzzy matching against a query.
/// Returns indices of matching items sorted by match quality (best first).
pub fn fuzzy_filter_indices(items: &[impl AsRef<str>], query: &str) -> Vec<usize> {
    if query.is_empty() {
        return (0..items.len()).collect();
    }
    let mut scored: Vec<(usize, usize)> = items
        .iter()
        .enumerate()
        .filter_map(|(i, item)| fuzzy_score(query, item.as_ref()).map(|score| (i, score)))
        .collect();
    scored.sort_by(|a, b| b.1.cmp(&a.1));
    scored.into_iter().map(|(i, _)| i).collect()
}

// ── 0. SessionSelectorSearch ─────────────────────────────────────────────────

/// A parsed search query with token extraction.
///
/// Supports:
/// - Plain tokens (fuzzy matched)
/// - Quoted phrases (exact substring matched)
/// - Regex mode: `re:<pattern>`
#[derive(Debug, Clone)]
pub struct ParsedSearchQuery {
    /// Query mode: tokens or regex.
    pub mode: SearchQueryMode,
    /// Extracted tokens.
    pub tokens: Vec<SearchToken>,
    /// Compiled regex (only in regex mode).
    pub regex: Option<regex::Regex>,
    /// Error message if parsing failed.
    pub error: Option<String>,
}

/// Search query mode.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum SearchQueryMode {
    Tokens,
    Regex,
}

/// A single search token.
#[derive(Debug, Clone)]
pub struct SearchToken {
    /// Token kind: fuzzy or phrase.
    pub kind: SearchTokenKind,
    /// Token value.
    pub value: String,
}

/// Search token kind.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum SearchTokenKind {
    Fuzzy,
    Phrase,
}

/// Result of matching a session against a parsed query.
#[derive(Debug, Clone)]
pub struct SessionMatchResult {
    /// Whether the session matches.
    pub matches: bool,
    /// Match score (lower is better).
    pub score: usize,
}

/// Parse a search query string into tokens.
///
/// Supports:
/// - `re:<pattern>` for regex mode
/// - Quoted phrases: `"exact phrase"`
/// - Plain words (fuzzy matched)
/// - Mixed: `foo "node cve" bar`
pub fn parse_search_query(query: &str) -> ParsedSearchQuery {
    let trimmed = query.trim();
    if trimmed.is_empty() {
        return ParsedSearchQuery {
            mode: SearchQueryMode::Tokens,
            tokens: Vec::new(),
            regex: None,
            error: None,
        };
    }

    // Regex mode: re:<pattern>
    if let Some(pattern) = trimmed.strip_prefix("re:") {
        let pattern = pattern.trim();
        if pattern.is_empty() {
            return ParsedSearchQuery {
                mode: SearchQueryMode::Regex,
                tokens: Vec::new(),
                regex: None,
                error: Some("Empty regex".to_string()),
            };
        }
        match regex::Regex::new(&format!("(?i){}", pattern)) {
            Ok(re) => ParsedSearchQuery {
                mode: SearchQueryMode::Regex,
                tokens: Vec::new(),
                regex: Some(re),
                error: None,
            },
            Err(e) => ParsedSearchQuery {
                mode: SearchQueryMode::Regex,
                tokens: Vec::new(),
                regex: None,
                error: Some(e.to_string()),
            },
        }
    } else {
        // Token mode with quote support
        let mut tokens = Vec::new();
        let mut buf = String::new();
        let mut in_quote = false;
        let mut had_unclosed_quote = false;

        let flush = |buf: &mut String, kind: SearchTokenKind, tokens: &mut Vec<SearchToken>| {
            let v = buf.trim().to_string();
            buf.clear();
            if !v.is_empty() {
                tokens.push(SearchToken { kind, value: v });
            }
        };

        for ch in trimmed.chars() {
            if ch == '"' {
                if in_quote {
                    flush(&mut buf, SearchTokenKind::Phrase, &mut tokens);
                    in_quote = false;
                } else {
                    flush(&mut buf, SearchTokenKind::Fuzzy, &mut tokens);
                    in_quote = true;
                }
                continue;
            }

            if !in_quote && ch.is_whitespace() {
                flush(&mut buf, SearchTokenKind::Fuzzy, &mut tokens);
                continue;
            }

            buf.push(ch);
        }

        if in_quote {
            had_unclosed_quote = true;
        }

        // If quotes were unbalanced, fall back to plain whitespace tokenization
        if had_unclosed_quote {
            tokens = trimmed
                .split_whitespace()
                .filter(|t| !t.is_empty())
                .map(|t| SearchToken {
                    kind: SearchTokenKind::Fuzzy,
                    value: t.to_string(),
                })
                .collect();
        } else {
            flush(&mut buf, if in_quote { SearchTokenKind::Phrase } else { SearchTokenKind::Fuzzy }, &mut tokens);
        }

        ParsedSearchQuery {
            mode: SearchQueryMode::Tokens,
            tokens,
            regex: None,
            error: None,
        }
    }
}

/// Match an enhanced session info against a parsed query.
///
/// The session's ID, name, label, working directory, and model are used as search text.
pub fn match_session(session: &EnhancedSessionInfo, parsed: &ParsedSearchQuery) -> SessionMatchResult {
    let text = format!(
        "{} {} {} {}",
        session.id,
        session.name,
        session.label.as_deref().unwrap_or(""),
        session.working_dir.as_deref().unwrap_or("")
    );

    if parsed.mode == SearchQueryMode::Regex {
        if let Some(ref re) = parsed.regex {
            if let Some(mat) = re.find(&text) {
                return SessionMatchResult {
                    matches: true,
                    score: mat.start(),
                };
            }
        }
        return SessionMatchResult {
            matches: false,
            score: 0,
        };
    }

    if parsed.tokens.is_empty() {
        return SessionMatchResult {
            matches: true,
            score: 0,
        };
    }

    let mut total_score = 0usize;
    let text_lower = text.to_lowercase().replace(char::is_whitespace, " ");

    for token in &parsed.tokens {
        if token.kind == SearchTokenKind::Phrase {
            let phrase = token.value.to_lowercase().replace(char::is_whitespace, " ");
            if phrase.is_empty() {
                continue;
            }
            if let Some(idx) = text_lower.find(&phrase) {
                total_score += idx;
            } else {
                return SessionMatchResult {
                    matches: false,
                    score: 0,
                };
            }
        } else {
            // Fuzzy match
            match fuzzy_score(&token.value, &text) {
                Some(score) => {
                    // Invert: fuzzy_score returns higher=better, we want lower=better
                    total_score += 10000 - score.min(9999);
                }
                None => {
                    return SessionMatchResult {
                        matches: false,
                        score: 0,
                    };
                }
            }
        }
    }

    SessionMatchResult {
        matches: true,
        score: total_score,
    }
}

/// Filter and sort sessions using a parsed query.
///
/// Supports three sort modes:
/// - `Recent`: Keep input order (filter only)
/// - `Fuzzy` / `Threaded`: Sort by match score, tie-break by modified date
pub fn filter_and_sort_sessions(
    sessions: &[EnhancedSessionInfo],
    query: &str,
    sort_mode: SessionSortMode,
    name_filter: SessionNameFilter,
) -> Vec<EnhancedSessionInfo> {
    let name_filtered: Vec<_> = if name_filter == SessionNameFilter::Named {
        sessions.iter().filter(|s| !s.name.is_empty()).cloned().collect()
    } else {
        sessions.to_vec()
    };

    let trimmed = query.trim();
    if trimmed.is_empty() {
        return name_filtered;
    }

    let parsed = parse_search_query(query);
    if parsed.error.is_some() {
        return Vec::new();
    }

    if sort_mode == SessionSortMode::Recent {
        return name_filtered
            .into_iter()
            .filter(|s| match_session(s, &parsed).matches)
            .collect();
    }

    // Relevance mode: sort by score
    let mut scored: Vec<(EnhancedSessionInfo, usize)> = name_filtered
        .into_iter()
        .filter_map(|s| {
            let result = match_session(&s, &parsed);
            if result.matches {
                Some((s, result.score))
            } else {
                None
            }
        })
        .collect();

    scored.sort_by(|a, b| {
        a.1.cmp(&b.1).then_with(|| {
            // Tie-break by updated_at descending
            b.0.updated_at.cmp(&a.0.updated_at)
        })
    });

    scored.into_iter().map(|(s, _)| s).collect()
}

/// Session selector with advanced search (parsed query, fuzzy + phrase + regex).
///
/// This is the search/filter component from `session-selector-search.ts`, providing:
/// - Token-based search with quoted phrase support
/// - Regex mode via `re:<pattern>`
/// - Fuzzy matching on session names and IDs
/// - Sort by relevance or recency
/// - Name filtering (all / named-only)
#[derive(Debug, Clone)]
pub struct SessionSelectorSearch {
    /// All sessions.
    pub sessions: Vec<EnhancedSessionInfo>,
    /// Filtered sessions after search.
    pub filtered_sessions: Vec<EnhancedSessionInfo>,
    /// Current raw search query.
    pub query: String,
    /// Parsed search query.
    pub parsed_query: ParsedSearchQuery,
    /// Sort mode.
    pub sort_mode: SessionSortMode,
    /// Name filter mode.
    pub name_filter: SessionNameFilter,
    /// Selected index in filtered_sessions.
    pub selected_index: usize,
    /// Scroll offset for rendering.
    pub scroll_offset: usize,
    /// Maximum visible items.
    pub max_visible: usize,
}

impl SessionSelectorSearch {
    /// Create a new session selector search.
    pub fn new(sessions: Vec<EnhancedSessionInfo>) -> Self {
        let filtered_sessions = sessions.clone();
        Self {
            sessions,
            filtered_sessions,
            query: String::new(),
            parsed_query: parse_search_query(""),
            sort_mode: SessionSortMode::Recent,
            name_filter: SessionNameFilter::All,
            selected_index: 0,
            scroll_offset: 0,
            max_visible: 15,
        }
    }

    /// Set the search query and re-filter.
    pub fn set_query(&mut self, query: String) {
        self.query = query;
        self.parsed_query = parse_search_query(&self.query);
        self.apply_filter();
    }

    /// Set the sort mode.
    pub fn set_sort_mode(&mut self, mode: SessionSortMode) {
        self.sort_mode = mode;
        self.apply_filter();
    }

    /// Set the name filter.
    pub fn set_name_filter(&mut self, filter: SessionNameFilter) {
        self.name_filter = filter;
        self.apply_filter();
    }

    /// Toggle name filter.
    pub fn toggle_name_filter(&mut self) {
        self.name_filter = match self.name_filter {
            SessionNameFilter::All => SessionNameFilter::Named,
            SessionNameFilter::Named => SessionNameFilter::All,
        };
        self.apply_filter();
    }

    /// Toggle sort mode.
    pub fn toggle_sort(&mut self) {
        self.sort_mode = match self.sort_mode {
            SessionSortMode::Recent => SessionSortMode::Threaded,
            SessionSortMode::Threaded => SessionSortMode::Fuzzy,
            SessionSortMode::Fuzzy => SessionSortMode::Recent,
        };
        self.apply_filter();
    }

    fn apply_filter(&mut self) {
        self.filtered_sessions = filter_and_sort_sessions(
            &self.sessions,
            &self.query,
            self.sort_mode,
            self.name_filter,
        );
        self.selected_index = self
            .selected_index
            .min(self.filtered_sessions.len().saturating_sub(1));
    }

    /// Move selection up.
    pub fn move_up(&mut self) {
        if self.selected_index > 0 {
            self.selected_index -= 1;
            self.adjust_scroll();
        }
    }

    /// Move selection down.
    pub fn move_down(&mut self) {
        let max = self.filtered_sessions.len().saturating_sub(1);
        if self.selected_index < max {
            self.selected_index += 1;
            self.adjust_scroll();
        }
    }

    fn adjust_scroll(&mut self) {
        if self.selected_index < self.scroll_offset {
            self.scroll_offset = self.selected_index;
        } else if self.selected_index >= self.scroll_offset + self.max_visible {
            self.scroll_offset = self.selected_index - self.max_visible + 1;
        }
    }

    /// Get the currently selected session.
    pub fn selected(&self) -> Option<&EnhancedSessionInfo> {
        self.filtered_sessions.get(self.selected_index)
    }

    /// Get the parse error, if any.
    pub fn parse_error(&self) -> Option<&str> {
        self.parsed_query.error.as_deref()
    }

    /// Render the session selector search as a vector of lines.
    pub fn render(&self, width: usize) -> Vec<String> {
        let mut lines = Vec::new();

        // Header
        lines.push("\x1b[36m───────────────────────────────────────────────────\x1b[0m".to_string());
        lines.push("\x1b[1mSearch Sessions\x1b[0m".to_string());

        // Query display with token highlighting
        if !self.query.is_empty() {
            if let Some(ref err) = self.parsed_query.error {
                lines.push(format!("\x1b[31mQuery error: {}\x1b[0m", err));
            } else {
                let mode_label = match self.parsed_query.mode {
                    SearchQueryMode::Tokens => "tokens",
                    SearchQueryMode::Regex => "regex",
                };
                let token_count = self.parsed_query.tokens.len();
                lines.push(format!(
                    "\x1b[2m[{}]\x1b[0m {}",
                    mode_label,
                    if token_count > 0 {
                        self.parsed_query
                            .tokens
                            .iter()
                            .map(|t| match t.kind {
                                SearchTokenKind::Fuzzy => format!("{}", t.value),
                                SearchTokenKind::Phrase => format!("\"\x1b[33m{}\x1b[0m\"", t.value),
                            })
                            .collect::<Vec<_>>()
                            .join(" ")
                    } else {
                        self.query.clone()
                    }
                ));
            }
        }

        // Sort and filter indicators
        lines.push(format!(
            "\x1b[2mSort: \x1b[36m{}\x1b[0m \x1b[2mFilter: \x1b[36m{}\x1b[0m",
            self.sort_mode.label(),
            match self.name_filter {
                SessionNameFilter::All => "All",
                SessionNameFilter::Named => "Named",
            }
        ));

        lines.push(String::new());

        if self.filtered_sessions.is_empty() {
            lines.push("\x1b[2m  No matching sessions\x1b[0m".to_string());
            lines.push("\x1b[36m───────────────────────────────────────────────────\x1b[0m".to_string());
            return lines;
        }

        // Render visible sessions
        let visible: Vec<_> = self
            .filtered_sessions
            .iter()
            .skip(self.scroll_offset)
            .take(self.max_visible)
            .collect();

        for (i, session) in visible.iter().enumerate() {
            let real_idx = self.scroll_offset + i;
            let is_selected = real_idx == self.selected_index;

            let marker = if is_selected {
                "\x1b[36m▶\x1b[0m"
            } else {
                " "
            };

            let branch = if session.parent_id.is_some() {
                "├─ "
            } else {
                "  "
            };

            let name = if session.name.is_empty() {
                &session.id[..8.min(session.id.len())]
            } else {
                &session.name
            };

            let label = session
                .label
                .as_ref()
                .map(|l| format!(" \x1b[33m[{}]\x1b[0m", l))
                .unwrap_or_default();

            let time = format_relative_time(
                session.updated_at.as_ref().unwrap_or(&session.created_at),
            );

            let line = format!(
                "{} {}{:<30} {} msg:{}",
                marker,
                branch,
                name,
                time,
                session.message_count,
            );
            let mut line = truncate_str(&line, width);
            line.push_str(&label);
            lines.push(line);
        }

        // Scroll indicator
        if self.filtered_sessions.len() > self.max_visible {
            lines.push(format!(
                "\x1b[2m  ({}/{})\x1b[0m",
                self.selected_index + 1,
                self.filtered_sessions.len()
            ));
        }

        lines.push(String::new());
        lines.push(
            "\x1b[2mType to search · \"quotes\" for phrases · re: for regex\x1b[0m".to_string(),
        );
        lines
            .push("\x1b[36m───────────────────────────────────────────────────\x1b[0m".to_string());

        lines
    }
}

// ── 1. ConfigSelector ─────────────────────────────────────────────────────────

/// Resource type for configuration items.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum ResourceType {
    Extensions,
    Skills,
    Prompts,
    Themes,
}

impl ResourceType {
    pub fn label(&self) -> &'static str {
        match self {
            ResourceType::Extensions => "Extensions",
            ResourceType::Skills => "Skills",
            ResourceType::Prompts => "Prompts",
            ResourceType::Themes => "Themes",
        }
    }
}

/// A single configuration resource item.
#[derive(Debug, Clone)]
pub struct ConfigResourceItem {
    pub path: String,
    pub enabled: bool,
    pub resource_type: ResourceType,
    pub display_name: String,
    pub group_key: String,
}

/// A group of configuration resources.
#[derive(Debug, Clone)]
pub struct ConfigResourceGroup {
    pub key: String,
    pub label: String,
    pub subgroups: Vec<ConfigResourceSubgroup>,
}

/// A subgroup of configuration resources (e.g., all extensions in a package).
#[derive(Debug, Clone)]
pub struct ConfigResourceSubgroup {
    pub resource_type: ResourceType,
    pub label: String,
    pub items: Vec<ConfigResourceItem>,
}

/// Flat entry for display in the config selector.
#[derive(Debug, Clone)]
pub enum ConfigFlatEntry {
    Group {
        group: ConfigResourceGroup,
    },
    Subgroup {
        subgroup: ConfigResourceSubgroup,
        group_label: String,
    },
    Item {
        item: ConfigResourceItem,
        group_label: String,
        subgroup_label: String,
    },
}

/// Config file selection with fuzzy search.
/// Renders a hierarchical list of configuration resources (extensions, skills, prompts, themes)
/// organized by source (package, user, project) with enable/disable toggle support.
#[derive(Debug, Clone)]
pub struct ConfigSelector {
    /// All flat entries (groups, subgroups, items).
    pub flat_entries: Vec<ConfigFlatEntry>,
    /// Filtered entries based on search.
    pub filtered_entries: Vec<ConfigFlatEntry>,
    /// Current search query.
    pub query: String,
    /// Selected index in filtered_entries.
    pub selected_index: usize,
    /// Maximum visible items.
    pub max_visible: usize,
}

impl ConfigSelector {
    /// Create a new config selector from resource groups.
    pub fn new(groups: Vec<ConfigResourceGroup>) -> Self {
        let flat_entries = Self::build_flat_list(&groups);
        let filtered_entries = flat_entries.clone();
        let selected_index = flat_entries
            .iter()
            .position(|e| matches!(e, ConfigFlatEntry::Item { .. }))
            .unwrap_or(0);
        Self {
            flat_entries,
            filtered_entries,
            query: String::new(),
            selected_index,
            max_visible: 15,
        }
    }

    fn build_flat_list(groups: &[ConfigResourceGroup]) -> Vec<ConfigFlatEntry> {
        let mut entries = Vec::new();
        for group in groups {
            entries.push(ConfigFlatEntry::Group {
                group: group.clone(),
            });
            for subgroup in &group.subgroups {
                entries.push(ConfigFlatEntry::Subgroup {
                    subgroup: subgroup.clone(),
                    group_label: group.label.clone(),
                });
                for item in &subgroup.items {
                    entries.push(ConfigFlatEntry::Item {
                        item: item.clone(),
                        group_label: group.label.clone(),
                        subgroup_label: subgroup.label.clone(),
                    });
                }
            }
        }
        entries
    }

    /// Apply search filter.
    pub fn set_filter(&mut self, query: String) {
        self.query = query;
        self.apply_filter();
    }

    fn apply_filter(&mut self) {
        if self.query.trim().is_empty() {
            self.filtered_entries = self.flat_entries.clone();
            self.select_first_item();
            return;
        }

        let lower_query = self.query.to_lowercase();

        // Find matching items
        let mut matching_groups = std::collections::HashSet::new();
        let mut matching_subgroups = std::collections::HashSet::new();
        let mut matching_items = std::collections::HashSet::new();

        for (idx, entry) in self.flat_entries.iter().enumerate() {
            if let ConfigFlatEntry::Item { item, .. } = entry {
                if item.display_name.to_lowercase().contains(&lower_query)
                    || item.path.to_lowercase().contains(&lower_query)
                    || item
                        .resource_type
                        .label()
                        .to_lowercase()
                        .contains(&lower_query)
                {
                    matching_items.insert(idx);
                }
            }
        }

        // Include parent groups/subgroups of matching items
        let mut current_group_idx: Option<usize> = None;
        let mut current_subgroup_idx: Option<usize> = None;
        for (idx, entry) in self.flat_entries.iter().enumerate() {
            match entry {
                ConfigFlatEntry::Group { .. } => current_group_idx = Some(idx),
                ConfigFlatEntry::Subgroup { .. } => current_subgroup_idx = Some(idx),
                ConfigFlatEntry::Item { .. } => {
                    if matching_items.contains(&idx) {
                        if let Some(gi) = current_group_idx {
                            matching_groups.insert(gi);
                        }
                        if let Some(si) = current_subgroup_idx {
                            matching_subgroups.insert(si);
                        }
                    }
                }
            }
        }

        self.filtered_entries = self
            .flat_entries
            .iter()
            .enumerate()
            .filter(|(idx, _)| {
                matching_groups.contains(idx)
                    || matching_subgroups.contains(idx)
                    || matching_items.contains(idx)
            })
            .map(|(_, e)| e.clone())
            .collect();

        self.select_first_item();
    }

    fn select_first_item(&mut self) {
        self.selected_index = self
            .filtered_entries
            .iter()
            .position(|e| matches!(e, ConfigFlatEntry::Item { .. }))
            .unwrap_or(0);
    }

    /// Move selection to the next item (skipping headers).
    pub fn move_up(&mut self) {
        self.selected_index = self.find_next_item(self.selected_index, -1);
    }

    /// Move selection to the previous item (skipping headers).
    pub fn move_down(&mut self) {
        self.selected_index = self.find_next_item(self.selected_index, 1);
    }

    fn find_next_item(&self, from: usize, direction: i32) -> usize {
        let mut idx = from as i32 + direction;
        while idx >= 0 && (idx as usize) < self.filtered_entries.len() {
            if matches!(
                self.filtered_entries[idx as usize],
                ConfigFlatEntry::Item { .. }
            ) {
                return idx as usize;
            }
            idx += direction;
        }
        from
    }

    /// Toggle the enabled state of the currently selected item.
    pub fn toggle_selected(&mut self) -> Option<&ConfigResourceItem> {
        if let Some(ConfigFlatEntry::Item { item, .. }) =
            self.filtered_entries.get(self.selected_index)
        {
            let new_enabled = !item.enabled;
            let path = item.path.clone();
            let resource_type = item.resource_type;
            // Update in filtered entries
            for entry in &mut self.filtered_entries {
                if let ConfigFlatEntry::Item { item, .. } = entry {
                    if item.path == path && item.resource_type == resource_type {
                        item.enabled = new_enabled;
                        return Some(item);
                    }
                }
            }
            // Also update in flat entries
            for entry in &mut self.flat_entries {
                if let ConfigFlatEntry::Item { item, .. } = entry {
                    if item.path == path && item.resource_type == resource_type {
                        item.enabled = new_enabled;
                    }
                }
            }
        }
        None
    }

    /// Render the config selector as lines for terminal display.
    pub fn render(&self, width: usize) -> Vec<String> {
        let mut lines = Vec::new();

        // Header
        lines.push(format!(
            "\x1b[1mResource Configuration\x1b[0m{:>width$}",
            "\x1b[2mSpace=toggle · Esc=close\x1b[0m",
            width = width.saturating_sub(40)
        ));
        lines.push("\x1b[2mType to filter resources\x1b[0m".to_string());

        // Search input
        if !self.query.is_empty() {
            lines.push(format!("> {}", self.query));
        } else {
            lines.push("> ".to_string());
        }
        lines.push(String::new());

        if self.filtered_entries.is_empty() {
            lines.push("\x1b[2m  No resources found\x1b[0m".to_string());
            return lines;
        }

        // Calculate visible range centered on selection
        let total = self.filtered_entries.len();
        let start = if total <= self.max_visible {
            0
        } else {
            (self.selected_index as i32 - self.max_visible as i32 / 2)
                .max(0)
                .min((total - self.max_visible) as i32) as usize
        };
        let end = (start + self.max_visible).min(total);

        for i in start..end {
            let entry = &self.filtered_entries[i];
            let is_selected = i == self.selected_index;

            match entry {
                ConfigFlatEntry::Group { group } => {
                    let line = truncate_str(&group.label, width.saturating_sub(2));
                    lines.push(format!("  \x1b[1m\x1b[36m{}\x1b[0m", line));
                }
                ConfigFlatEntry::Subgroup { subgroup, .. } => {
                    let line = truncate_str(&subgroup.label, width.saturating_sub(4));
                    lines.push(format!("    \x1b[2m{}\x1b[0m", line));
                }
                ConfigFlatEntry::Item { item, .. } => {
                    let cursor = if is_selected { "> " } else { "  " };
                    let checkbox = if item.enabled {
                        "\x1b[32m[x]\x1b[0m"
                    } else {
                        "\x1b[2m[ ]\x1b[0m"
                    };
                    let name = if is_selected {
                        format!("\x1b[1m{}\x1b[0m", item.display_name)
                    } else {
                        item.display_name.clone()
                    };
                    let content = format!("{}    {} {}", cursor, checkbox, name);
                    lines.push(truncate_str(&content, width));
                }
            }
        }

        // Scroll indicator
        if start > 0 || end < total {
            let item_count = self
                .filtered_entries
                .iter()
                .filter(|e| matches!(e, ConfigFlatEntry::Item { .. }))
                .count();
            let current_item = self
                .filtered_entries
                .iter()
                .take(self.selected_index)
                .filter(|e| matches!(e, ConfigFlatEntry::Item { .. }))
                .count()
                + 1;
            lines.push(format!("\x1b[2m  ({}/{})\x1b[0m", current_item, item_count));
        }

        lines
    }
}

// ── 2. ModelSelector (Enhanced) ──────────────────────────────────────────────

/// Model scope for filtering.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum ModelScope {
    All,
    Scoped,
}

/// Enhanced model selection with provider tabs, thinking levels, and scope switching.
#[derive(Debug, Clone)]
pub struct ModelSelectorEnhanced {
    /// All available models.
    pub all_models: Vec<ModelInfo>,
    /// Scoped models (filtered by configured providers).
    pub scoped_models: Vec<ModelInfo>,
    /// Currently active models (based on scope).
    pub active_models: Vec<ModelInfo>,
    /// Filtered models (based on search).
    pub filtered_models: Vec<ModelInfo>,
    /// Current search query.
    pub query: String,
    /// Selected index in filtered_models.
    pub selected_index: usize,
    /// Current scope (all or scoped).
    pub scope: ModelScope,
    /// Currently active model id (for highlighting).
    pub current_model_id: Option<String>,
    /// Maximum visible items.
    pub max_visible: usize,
    /// Error message if model loading failed.
    pub error_message: Option<String>,
}

impl ModelSelectorEnhanced {
    /// Create a new enhanced model selector.
    pub fn new(
        all_models: Vec<ModelInfo>,
        scoped_models: Vec<ModelInfo>,
        current_model_id: Option<String>,
    ) -> Self {
        let scope = if scoped_models.is_empty() {
            ModelScope::All
        } else {
            ModelScope::Scoped
        };
        let active_models = if scope == ModelScope::Scoped {
            scoped_models.clone()
        } else {
            all_models.clone()
        };
        let filtered_models = active_models.clone();

        let mut selector = Self {
            all_models,
            scoped_models,
            active_models,
            filtered_models,
            query: String::new(),
            selected_index: 0,
            scope,
            current_model_id,
            max_visible: 10,
            error_message: None,
        };
        selector.pre_select_current();
        selector
    }

    fn pre_select_current(&mut self) {
        if let Some(ref current_id) = self.current_model_id {
            if let Some(idx) = self
                .filtered_models
                .iter()
                .position(|m| m.id == *current_id)
            {
                self.selected_index = idx;
            }
        }
    }

    /// Toggle scope between all and scoped models.
    pub fn toggle_scope(&mut self) {
        self.scope = match self.scope {
            ModelScope::All => ModelScope::Scoped,
            ModelScope::Scoped => ModelScope::All,
        };
        if self.scoped_models.is_empty() {
            self.scope = ModelScope::All;
        }
        self.active_models = if self.scope == ModelScope::Scoped {
            self.scoped_models.clone()
        } else {
            self.all_models.clone()
        };
        self.apply_filter();
    }

    /// Set search filter.
    pub fn set_filter(&mut self, query: String) {
        self.query = query;
        self.apply_filter();
    }

    fn apply_filter(&mut self) {
        if self.query.trim().is_empty() {
            self.filtered_models = self.active_models.clone();
        } else {
            let indices = fuzzy_filter_indices(
                &self
                    .active_models
                    .iter()
                    .map(|m| format!("{} {} {}/{}", m.id, m.provider, m.provider, m.id))
                    .collect::<Vec<_>>(),
                &self.query,
            );
            self.filtered_models = indices
                .into_iter()
                .map(|i| self.active_models[i].clone())
                .collect();
        }
        self.selected_index = self
            .selected_index
            .min(self.filtered_models.len().saturating_sub(1));
        self.pre_select_current();
    }

    /// Move selection up (wraps to bottom).
    pub fn move_up(&mut self) {
        if self.filtered_models.is_empty() {
            return;
        }
        if self.selected_index == 0 {
            self.selected_index = self.filtered_models.len() - 1;
        } else {
            self.selected_index -= 1;
        }
    }

    /// Move selection down (wraps to top).
    pub fn move_down(&mut self) {
        if self.filtered_models.is_empty() {
            return;
        }
        if self.selected_index == self.filtered_models.len() - 1 {
            self.selected_index = 0;
        } else {
            self.selected_index += 1;
        }
    }

    /// Get the currently selected model.
    pub fn selected(&self) -> Option<&ModelInfo> {
        self.filtered_models.get(self.selected_index)
    }

    /// Render the enhanced model selector.
    pub fn render(&self, width: usize) -> Vec<String> {
        let mut lines = Vec::new();

        // Scope header
        let scope_text = match self.scope {
            ModelScope::All => "\x1b[36mall\x1b[0m | \x1b[2mscoped\x1b[0m".to_string(),
            ModelScope::Scoped => "\x1b[2mall\x1b[0m | \x1b[36mscoped\x1b[0m".to_string(),
        };
        lines.push(format!(
            "\x1b[2mScope:\x1b[0m {} \x1b[2m(Tab to toggle)\x1b[0m",
            scope_text
        ));

        // Search input
        lines.push(format!("> {}", self.query));
        lines.push(String::new());

        // Error message
        if let Some(ref err) = self.error_message {
            for err_line in err.lines() {
                lines.push(format!("\x1b[31m{}\x1b[0m", err_line));
            }
            return lines;
        }

        if self.filtered_models.is_empty() {
            lines.push("\x1b[2m  No matching models\x1b[0m".to_string());
            return lines;
        }

        // Calculate visible range
        let total = self.filtered_models.len();
        let half = self.max_visible / 2;
        let start = if self.selected_index >= half {
            (self.selected_index - half).min(total.saturating_sub(self.max_visible))
        } else {
            0
        };
        let end = (start + self.max_visible).min(total);

        for i in start..end {
            let model = &self.filtered_models[i];
            let is_selected = i == self.selected_index;
            let is_current = self
                .current_model_id
                .as_ref()
                .map_or(false, |id| id == &model.id);

            let prefix = if is_selected {
                "\x1b[36m→ \x1b[0m"
            } else {
                "  "
            };
            let model_text = if is_selected {
                format!("\x1b[36m{}\x1b[0m", model.id)
            } else {
                model.id.clone()
            };
            let provider_badge = format!("\x1b[2m[{}]\x1b[0m", model.provider);
            let current_badge = if is_current {
                "\x1b[32m ✓\x1b[0m".to_string()
            } else {
                String::new()
            };

            let line = format!(
                "{}{} {}{}",
                prefix, model_text, provider_badge, current_badge
            );
            lines.push(truncate_str(&line, width));
        }

        // Scroll indicator
        if start > 0 || end < total {
            lines.push(format!(
                "\x1b[2m  ({}/{})\x1b[0m",
                self.selected_index + 1,
                total
            ));
        }

        // Detail line for selected model
        if let Some(selected) = self.selected() {
            lines.push(String::new());
            lines.push(format!("\x1b[2m  Model Name: {}\x1b[0m", selected.name));
        }

        lines
    }
}

// ── 3. SessionSelector (Enhanced) ────────────────────────────────────────────

/// Session scope for filtering.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum SessionScope {
    CurrentFolder,
    All,
}

/// Sort mode for sessions.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum SessionSortMode {
    Recent,
    Fuzzy,
    Threaded,
}

impl SessionSortMode {
    pub fn label(&self) -> &'static str {
        match self {
            SessionSortMode::Recent => "Recent",
            SessionSortMode::Fuzzy => "Fuzzy",
            SessionSortMode::Threaded => "Threaded",
        }
    }
}

/// Name filter mode for sessions.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum SessionNameFilter {
    All,
    Named,
}

/// Enhanced session info with additional metadata.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct EnhancedSessionInfo {
    pub id: String,
    pub name: String,
    pub label: Option<String>,
    pub created_at: String,
    pub updated_at: Option<String>,
    pub message_count: usize,
    pub model: Option<String>,
    pub parent_id: Option<String>,
    pub working_dir: Option<String>,
}

/// Format a relative time from a date string.
fn format_relative_time(date_str: &str) -> String {
    // Try parsing common date formats
    let now = chrono::Utc::now();
    let parsed: Option<chrono::DateTime<chrono::Utc>> =
        chrono::DateTime::parse_from_rfc3339(date_str)
            .map(|dt| dt.to_utc())
            .ok()
            .or_else(|| {
                chrono::NaiveDateTime::parse_from_str(date_str, "%Y-%m-%dT%H:%M:%S")
                    .map(|dt| dt.and_utc())
                    .ok()
            })
            .or_else(|| {
                chrono::NaiveDate::parse_from_str(date_str, "%Y-%m-%d")
                    .map(|d| d.and_hms_opt(0, 0, 0).unwrap().and_utc())
                    .ok()
            });

    match parsed {
        Some(dt) => {
            let diff = now.signed_duration_since(dt);
            let mins = diff.num_minutes();
            if mins < 1 {
                return "now".to_string();
            }
            if mins < 60 {
                return format!("{}m", mins);
            }
            let hours = diff.num_hours();
            if hours < 24 {
                return format!("{}h", hours);
            }
            let days = diff.num_days();
            if days < 7 {
                return format!("{}d", days);
            }
            if days < 30 {
                return format!("{}w", days / 7);
            }
            if days < 365 {
                return format!("{}mo", days / 30);
            }
            format!("{}y", days / 365)
        }
        None => date_str.to_string(),
    }
}

/// Shorten a path by replacing home directory with ~.
fn shorten_path(path: &str) -> String {
    if let Some(home) = dirs::home_dir() {
        let home_str = home.to_string_lossy();
        if path.starts_with(home_str.as_ref()) {
            return format!("~{}", &path[home_str.len()..]);
        }
    }
    path.to_string()
}

/// Enhanced session selector with search, labels, timestamps, scope, and sort.
#[derive(Debug, Clone)]
pub struct SessionSelectorEnhanced {
    /// All sessions.
    pub sessions: Vec<EnhancedSessionInfo>,
    /// Filtered sessions based on search.
    pub filtered_sessions: Vec<EnhancedSessionInfo>,
    /// Current search query.
    pub query: String,
    /// Selected index in filtered_sessions.
    pub selected_index: usize,
    /// Current scope.
    pub scope: SessionScope,
    /// Current sort mode.
    pub sort_mode: SessionSortMode,
    /// Name filter mode.
    pub name_filter: SessionNameFilter,
    /// Whether to show working directory paths.
    pub show_paths: bool,
    /// Scroll offset for rendering.
    pub scroll_offset: usize,
    /// Maximum visible items.
    pub max_visible: usize,
    /// Current working directory for scope filtering.
    pub cwd: Option<String>,
}

impl SessionSelectorEnhanced {
    /// Create a new enhanced session selector.
    pub fn new(sessions: Vec<EnhancedSessionInfo>) -> Self {
        let filtered_sessions = sessions.clone();
        Self {
            sessions,
            filtered_sessions,
            query: String::new(),
            selected_index: 0,
            scope: SessionScope::CurrentFolder,
            sort_mode: SessionSortMode::Recent,
            name_filter: SessionNameFilter::All,
            show_paths: false,
            scroll_offset: 0,
            max_visible: 15,
            cwd: None,
        }
    }

    /// Set the current working directory for scope filtering.
    pub fn set_cwd(&mut self, cwd: String) {
        self.cwd = Some(cwd);
        self.apply_filter();
    }

    /// Toggle scope between current folder and all.
    pub fn toggle_scope(&mut self) {
        self.scope = match self.scope {
            SessionScope::CurrentFolder => SessionScope::All,
            SessionScope::All => SessionScope::CurrentFolder,
        };
        self.apply_filter();
    }

    /// Toggle sort mode.
    pub fn toggle_sort(&mut self) {
        self.sort_mode = match self.sort_mode {
            SessionSortMode::Recent => SessionSortMode::Threaded,
            SessionSortMode::Threaded => SessionSortMode::Fuzzy,
            SessionSortMode::Fuzzy => SessionSortMode::Recent,
        };
        self.apply_filter();
    }

    /// Toggle name filter.
    pub fn toggle_name_filter(&mut self) {
        self.name_filter = match self.name_filter {
            SessionNameFilter::All => SessionNameFilter::Named,
            SessionNameFilter::Named => SessionNameFilter::All,
        };
        self.apply_filter();
    }

    /// Set search filter.
    pub fn set_filter(&mut self, query: String) {
        self.query = query;
        self.apply_filter();
    }

    fn apply_filter(&mut self) {
        let mut filtered: Vec<EnhancedSessionInfo> = self.sessions.clone();

        // Scope filter
        if self.scope == SessionScope::CurrentFolder {
            if let Some(ref cwd) = self.cwd {
                filtered.retain(|s| s.working_dir.as_ref().map_or(true, |d| d == cwd));
            }
        }

        // Name filter
        if self.name_filter == SessionNameFilter::Named {
            filtered.retain(|s| s.name.is_empty());
            // Actually, keep named ones
            filtered = self
                .sessions
                .iter()
                .filter(|s| !s.name.is_empty())
                .cloned()
                .collect();
        }

        // Query filter
        if !self.query.is_empty() {
            let lower_query = self.query.to_lowercase();
            filtered.retain(|s| {
                s.name.to_lowercase().contains(&lower_query)
                    || s.id.to_lowercase().contains(&lower_query)
                    || s.label
                        .as_ref()
                        .map_or(false, |l| l.to_lowercase().contains(&lower_query))
            });
        }

        // Sort
        match self.sort_mode {
            SessionSortMode::Recent => {
                filtered.sort_by(|a, b| {
                    b.updated_at
                        .cmp(&a.updated_at)
                        .then(b.created_at.cmp(&a.created_at))
                });
            }
            SessionSortMode::Fuzzy => {
                // Keep order; fuzzy matching already done by filter
            }
            SessionSortMode::Threaded => {
                // Sort by parent_id to group threads
                filtered.sort_by(|a, b| {
                    a.parent_id
                        .cmp(&b.parent_id)
                        .then(b.created_at.cmp(&a.created_at))
                });
            }
        }

        self.filtered_sessions = filtered;
        self.selected_index = self
            .selected_index
            .min(self.filtered_sessions.len().saturating_sub(1));
    }

    /// Move selection up.
    pub fn move_up(&mut self) {
        if self.selected_index > 0 {
            self.selected_index -= 1;
            self.adjust_scroll();
        }
    }

    /// Move selection down.
    pub fn move_down(&mut self) {
        let max = self.filtered_sessions.len().saturating_sub(1);
        if self.selected_index < max {
            self.selected_index += 1;
            self.adjust_scroll();
        }
    }

    fn adjust_scroll(&mut self) {
        if self.selected_index < self.scroll_offset {
            self.scroll_offset = self.selected_index;
        } else if self.selected_index >= self.scroll_offset + self.max_visible {
            self.scroll_offset = self.selected_index - self.max_visible + 1;
        }
    }

    /// Get the currently selected session.
    pub fn selected(&self) -> Option<&EnhancedSessionInfo> {
        self.filtered_sessions.get(self.selected_index)
    }

    /// Render the enhanced session selector.
    pub fn render(&self, width: usize) -> Vec<String> {
        let mut lines = Vec::new();

        // Header with scope, sort, name filter
        let title = match self.scope {
            SessionScope::CurrentFolder => "Resume Session (Current Folder)",
            SessionScope::All => "Resume Session (All)",
        };
        lines.push(format!("\x1b[1m{}\x1b[0m", title));

        let scope_text = match self.scope {
            SessionScope::CurrentFolder => "\x1b[36m◉ Current Folder\x1b[0m | ○ All",
            SessionScope::All => "○ Current Folder | \x1b[36m◉ All\x1b[0m",
        };
        let sort_text = format!("Sort: \x1b[36m{}\x1b[0m", self.sort_mode.label());
        let name_text = format!(
            "Name: \x1b[36m{}\x1b[0m",
            match self.name_filter {
                SessionNameFilter::All => "All",
                SessionNameFilter::Named => "Named",
            }
        );
        lines.push(format!("{}  {}  {}", scope_text, sort_text, name_text));

        // Search input
        if !self.query.is_empty() {
            lines.push(format!("> {}", self.query));
        }
        lines.push(String::new());

        if self.filtered_sessions.is_empty() {
            lines.push("\x1b[2m  No sessions found\x1b[0m".to_string());
            return lines;
        }

        // Render visible sessions
        let visible: Vec<_> = self
            .filtered_sessions
            .iter()
            .skip(self.scroll_offset)
            .take(self.max_visible)
            .collect();

        for (i, session) in visible.iter().enumerate() {
            let real_idx = self.scroll_offset + i;
            let is_selected = real_idx == self.selected_index;

            let marker = if is_selected {
                "\x1b[36m▶\x1b[0m"
            } else {
                " "
            };

            // Branch indicator
            let branch = if session.parent_id.is_some() {
                "├─ "
            } else {
                "  "
            };

            // Name or truncated ID
            let name = if session.name.is_empty() {
                &session.id[..8.min(session.id.len())]
            } else {
                &session.name
            };

            // Label
            let label = session
                .label
                .as_ref()
                .map(|l| format!(" \x1b[33m[{}]\x1b[0m", l))
                .unwrap_or_default();

            // Relative time
            let time =
                format_relative_time(&session.updated_at.as_ref().unwrap_or(&session.created_at));

            let mut line = format!(
                "{} {}{:<30} {} msg:{} model:{}",
                marker,
                branch,
                name,
                time,
                session.message_count,
                session.model.as_deref().unwrap_or("-"),
            );
            line.push_str(&label);

            // Working directory
            if self.show_paths {
                if let Some(ref dir) = session.working_dir {
                    line.push_str(&format!(" \x1b[2m{}\x1b[0m", shorten_path(dir)));
                }
            }

            lines.push(truncate_str(&line, width));
        }

        // Scroll indicator
        if self.filtered_sessions.len() > self.max_visible {
            lines.push(format!(
                "\x1b[2m  ({}/{})\x1b[0m",
                self.selected_index + 1,
                self.filtered_sessions.len()
            ));
        }

        lines
    }
}

// ── 4. SettingsSelector ──────────────────────────────────────────────────────

/// A setting item for the settings selector.
#[derive(Debug, Clone)]
pub struct SettingItem {
    /// Unique identifier for this setting.
    pub id: String,
    /// Display label.
    pub label: String,
    /// Description text.
    pub description: String,
    /// Current value as a string.
    pub current_value: String,
    /// Available values to choose from.
    pub values: Vec<String>,
}

impl SettingItem {
    pub fn new(
        id: impl Into<String>,
        label: impl Into<String>,
        description: impl Into<String>,
        current_value: impl Into<String>,
        values: Vec<String>,
    ) -> Self {
        Self {
            id: id.into(),
            label: label.into(),
            description: description.into(),
            current_value: current_value.into(),
            values,
        }
    }
}

/// Settings key-value selector with descriptions and fuzzy search.
#[derive(Debug, Clone)]
pub struct SettingsSelector {
    /// All settings items.
    pub items: Vec<SettingItem>,
    /// Filtered indices based on search.
    pub filtered_indices: Vec<usize>,
    /// Current search query.
    pub query: String,
    /// Selected index in filtered list.
    pub selected_index: usize,
    /// Scroll offset for rendering.
    pub scroll_offset: usize,
    /// Maximum visible items.
    pub max_visible: usize,
}

impl SettingsSelector {
    /// Create a new settings selector with the given items.
    pub fn new(items: Vec<SettingItem>) -> Self {
        let filtered_indices = (0..items.len()).collect();
        Self {
            items,
            filtered_indices,
            query: String::new(),
            selected_index: 0,
            scroll_offset: 0,
            max_visible: 10,
        }
    }

    /// Build settings items from a settings configuration.
    pub fn from_config(config: &SettingsConfig) -> Self {
        let mut items = Vec::new();

        items.push(SettingItem::new(
            "autocompact",
            "Auto-compact",
            "Automatically compact context when it gets too large",
            if config.auto_compact { "true" } else { "false" },
            vec!["true".to_string(), "false".to_string()],
        ));

        items.push(SettingItem::new(
            "show-images",
            "Show images",
            "Render images inline in terminal",
            if config.show_images { "true" } else { "false" },
            vec!["true".to_string(), "false".to_string()],
        ));

        items.push(SettingItem::new(
            "auto-resize-images",
            "Auto-resize images",
            "Resize large images to 2000x2000 max",
            if config.auto_resize_images {
                "true"
            } else {
                "false"
            },
            vec!["true".to_string(), "false".to_string()],
        ));

        items.push(SettingItem::new(
            "block-images",
            "Block images",
            "Prevent images from being sent to LLM providers",
            if config.block_images { "true" } else { "false" },
            vec!["true".to_string(), "false".to_string()],
        ));

        items.push(SettingItem::new(
            "skill-commands",
            "Skill commands",
            "Register skills as /skill:name commands",
            if config.enable_skill_commands {
                "true"
            } else {
                "false"
            },
            vec!["true".to_string(), "false".to_string()],
        ));

        items.push(SettingItem::new(
            "steering-mode",
            "Steering mode",
            "Enter while streaming queues steering messages",
            config.steering_mode.clone(),
            vec!["one-at-a-time".to_string(), "all".to_string()],
        ));

        items.push(SettingItem::new(
            "follow-up-mode",
            "Follow-up mode",
            "Alt+Enter queues follow-up messages until agent stops",
            config.follow_up_mode.clone(),
            vec!["one-at-a-time".to_string(), "all".to_string()],
        ));

        items.push(SettingItem::new(
            "transport",
            "Transport",
            "Preferred transport for providers with multiple transports",
            config.transport.clone(),
            vec![
                "sse".to_string(),
                "websocket".to_string(),
                "websocket-cached".to_string(),
                "auto".to_string(),
            ],
        ));

        items.push(SettingItem::new(
            "thinking",
            "Thinking level",
            "Reasoning depth for thinking-capable models",
            config.thinking_level.as_str().to_string(),
            config
                .available_thinking_levels
                .iter()
                .map(|l| l.as_str().to_string())
                .collect(),
        ));

        items.push(SettingItem::new(
            "hide-thinking",
            "Hide thinking",
            "Hide thinking blocks in assistant responses",
            if config.hide_thinking_block {
                "true"
            } else {
                "false"
            },
            vec!["true".to_string(), "false".to_string()],
        ));

        items.push(SettingItem::new(
            "theme",
            "Theme",
            "Color theme for the interface",
            config.current_theme.clone(),
            config.available_themes.clone(),
        ));

        items.push(SettingItem::new(
            "double-escape-action",
            "Double-escape action",
            "Action when pressing Escape twice with empty editor",
            config.double_escape_action.clone(),
            vec!["tree".to_string(), "fork".to_string(), "none".to_string()],
        ));

        items.push(SettingItem::new(
            "tree-filter-mode",
            "Tree filter mode",
            "Default filter when opening /tree",
            config.tree_filter_mode.clone(),
            vec![
                "default".to_string(),
                "no-tools".to_string(),
                "user-only".to_string(),
                "labeled-only".to_string(),
                "all".to_string(),
            ],
        ));

        items.push(SettingItem::new(
            "quiet-startup",
            "Quiet startup",
            "Disable verbose printing at startup",
            if config.quiet_startup {
                "true"
            } else {
                "false"
            },
            vec!["true".to_string(), "false".to_string()],
        ));

        items.push(SettingItem::new(
            "collapse-changelog",
            "Collapse changelog",
            "Show condensed changelog after updates",
            if config.collapse_changelog {
                "true"
            } else {
                "false"
            },
            vec!["true".to_string(), "false".to_string()],
        ));

        items.push(SettingItem::new(
            "install-telemetry",
            "Install telemetry",
            "Send an anonymous version/update ping after changelog-detected updates",
            if config.enable_install_telemetry {
                "true"
            } else {
                "false"
            },
            vec!["true".to_string(), "false".to_string()],
        ));

        items.push(SettingItem::new(
            "show-hardware-cursor",
            "Show hardware cursor",
            "Show the terminal cursor while still positioning it for IME support",
            if config.show_hardware_cursor {
                "true"
            } else {
                "false"
            },
            vec!["true".to_string(), "false".to_string()],
        ));

        items.push(SettingItem::new(
            "editor-padding",
            "Editor padding",
            "Horizontal padding for input editor (0-3)",
            config.editor_padding_x.to_string(),
            vec!["0".to_string(), "1".to_string(), "2".to_string(), "3".to_string()],
        ));

        items.push(SettingItem::new(
            "autocomplete-max-visible",
            "Autocomplete max items",
            "Max visible items in autocomplete dropdown (3-20)",
            config.autocomplete_max_visible.to_string(),
            vec!["3".to_string(), "5".to_string(), "7".to_string(), "10".to_string(), "15".to_string(), "20".to_string()],
        ));

        items.push(SettingItem::new(
            "clear-on-shrink",
            "Clear on shrink",
            "Clear empty rows when content shrinks (may cause flicker)",
            if config.clear_on_shrink {
                "true"
            } else {
                "false"
            },
            vec!["true".to_string(), "false".to_string()],
        ));

        items.push(SettingItem::new(
            "terminal-progress",
            "Terminal progress",
            "Show OSC 9;4 progress indicators in the terminal tab bar",
            if config.show_terminal_progress {
                "true"
            } else {
                "false"
            },
            vec!["true".to_string(), "false".to_string()],
        ));

        items.push(SettingItem::new(
            "image-width-cells",
            "Image width",
            "Preferred inline image width in terminal cells",
            config.image_width_cells.to_string(),
            vec!["60".to_string(), "80".to_string(), "120".to_string()],
        ));

        Self::new(items)
    }

    /// Set search filter.
    pub fn set_filter(&mut self, query: String) {
        self.query = query;
        self.apply_filter();
    }

    fn apply_filter(&mut self) {
        if self.query.is_empty() {
            self.filtered_indices = (0..self.items.len()).collect();
        } else {
            let search_texts: Vec<String> = self
                .items
                .iter()
                .map(|item| format!("{} {} {}", item.label, item.description, item.id))
                .collect();
            self.filtered_indices = fuzzy_filter_indices(&search_texts, &self.query);
        }
        self.selected_index = self
            .selected_index
            .min(self.filtered_indices.len().saturating_sub(1));
    }

    /// Move selection up.
    pub fn move_up(&mut self) {
        if self.selected_index > 0 {
            self.selected_index -= 1;
            self.adjust_scroll();
        }
    }

    /// Move selection down.
    pub fn move_down(&mut self) {
        let max = self.filtered_indices.len().saturating_sub(1);
        if self.selected_index < max {
            self.selected_index += 1;
            self.adjust_scroll();
        }
    }

    fn adjust_scroll(&mut self) {
        if self.selected_index < self.scroll_offset {
            self.scroll_offset = self.selected_index;
        } else if self.selected_index >= self.scroll_offset + self.max_visible {
            self.scroll_offset = self.selected_index - self.max_visible + 1;
        }
    }

    /// Get the currently selected setting item.
    pub fn selected(&self) -> Option<&SettingItem> {
        self.filtered_indices
            .get(self.selected_index)
            .and_then(|&idx| self.items.get(idx))
    }

    /// Cycle the value of the currently selected setting.
    pub fn cycle_value(&mut self) -> Option<String> {
        if let Some(&idx) = self.filtered_indices.get(self.selected_index) {
            let item = &mut self.items[idx];
            if item.values.len() > 1 {
                if let Some(pos) = item.values.iter().position(|v| v == &item.current_value) {
                    let next = (pos + 1) % item.values.len();
                    item.current_value = item.values[next].clone();
                    return Some(item.current_value.clone());
                }
            }
        }
        None
    }

    /// Set the value of a setting by ID.
    pub fn set_value(&mut self, id: &str, value: String) -> bool {
        if let Some(item) = self.items.iter_mut().find(|item| item.id == id) {
            if item.values.contains(&value) {
                item.current_value = value;
                return true;
            }
        }
        false
    }

    /// Validate the current settings.
    /// Returns a list of (setting_id, error_message) for invalid values.
    pub fn validate(&self) -> Vec<(String, String)> {
        let mut errors = Vec::new();
        for item in &self.items {
            match item.id.as_str() {
                "editor-padding" => {
                    if let Ok(v) = item.current_value.parse::<usize>() {
                        if v > 3 {
                            errors.push((item.id.clone(), "Editor padding must be 0-3".to_string()));
                        }
                    } else {
                        errors.push((item.id.clone(), "Editor padding must be a number".to_string()));
                    }
                }
                "autocomplete-max-visible" => {
                    if let Ok(v) = item.current_value.parse::<usize>() {
                        if v < 3 || v > 20 {
                            errors.push((item.id.clone(), "Autocomplete max visible must be 3-20".to_string()));
                        }
                    } else {
                        errors.push((item.id.clone(), "Autocomplete max visible must be a number".to_string()));
                    }
                }
                "image-width-cells" => {
                    if let Ok(v) = item.current_value.parse::<usize>() {
                        if v < 20 || v > 300 {
                            errors.push((item.id.clone(), "Image width must be 20-300".to_string()));
                        }
                    } else {
                        errors.push((item.id.clone(), "Image width must be a number".to_string()));
                    }
                }
                _ => {}
            }
        }
        errors
    }

    /// Render the settings selector.
    pub fn render(&self, width: usize) -> Vec<String> {
        let mut lines = Vec::new();

        lines
            .push("\x1b[36m───────────────────────────────────────────────────\x1b[0m".to_string());
        lines.push("\x1b[1mSettings\x1b[0m \x1b[2m(Enter to change · Esc to close · Type to search)\x1b[0m".to_string());

        if !self.query.is_empty() {
            lines.push(format!("Filter: {}", self.query));
        }
        lines.push(String::new());

        if self.filtered_indices.is_empty() {
            lines.push("\x1b[2m  No matching settings\x1b[0m".to_string());
            return lines;
        }

        // Render visible items
        let visible_count = self.max_visible.min(self.filtered_indices.len());
        for vi in self.scroll_offset..self.scroll_offset + visible_count {
            if vi >= self.filtered_indices.len() {
                break;
            }
            let idx = self.filtered_indices[vi];
            let item = &self.items[idx];
            let is_selected = vi == self.selected_index;

            let prefix = if is_selected {
                "\x1b[36m▶\x1b[0m"
            } else {
                " "
            };

            // Label and value
            let label = if is_selected {
                format!("\x1b[1m{}\x1b[0m", item.label)
            } else {
                item.label.clone()
            };

            // Show value with cycling indicator
            let value_display = if item.values.len() <= 2 {
                format!("\x1b[33m{}\x1b[0m", item.current_value)
            } else {
                // Show current value with position indicator
                if let Some(pos) = item.values.iter().position(|v| v == &item.current_value) {
                    format!(
                        "\x1b[33m{}\x1b[0m \x1b[2m({}/{})\x1b[0m",
                        item.current_value,
                        pos + 1,
                        item.values.len()
                    )
                } else {
                    format!("\x1b[33m{}\x1b[0m", item.current_value)
                }
            };

            let line = format!("{} {:<25} {}", prefix, label, value_display);
            lines.push(truncate_str(&line, width));

            // Description for selected item
            if is_selected {
                let desc = truncate_str(&format!("  \x1b[2m{}\x1b[0m", item.description), width);
                lines.push(desc);
            }
        }

        // Scroll indicator
        if self.filtered_indices.len() > self.max_visible {
            lines.push(format!(
                "\x1b[2m  ({}/{})\x1b[0m",
                self.selected_index + 1,
                self.filtered_indices.len()
            ));
        }

        lines
            .push("\x1b[36m───────────────────────────────────────────────────\x1b[0m".to_string());
        lines
    }
}

/// Settings configuration for building the settings selector.
#[derive(Debug, Clone, Default)]
pub struct SettingsConfig {
    pub auto_compact: bool,
    pub show_images: bool,
    pub image_width_cells: usize,
    pub auto_resize_images: bool,
    pub block_images: bool,
    pub enable_skill_commands: bool,
    pub steering_mode: String,
    pub follow_up_mode: String,
    pub transport: String,
    pub thinking_level: ThinkingLevel,
    pub available_thinking_levels: Vec<ThinkingLevel>,
    pub current_theme: String,
    pub available_themes: Vec<String>,
    pub hide_thinking_block: bool,
    pub collapse_changelog: bool,
    pub enable_install_telemetry: bool,
    pub double_escape_action: String,
    pub tree_filter_mode: String,
    pub show_hardware_cursor: bool,
    pub editor_padding_x: usize,
    pub autocomplete_max_visible: usize,
    pub quiet_startup: bool,
    pub clear_on_shrink: bool,
    pub show_terminal_progress: bool,
}

// ── 5. ThemeSelector ─────────────────────────────────────────────────────────

/// Theme name selection with preview.
#[derive(Debug, Clone)]
pub struct ThemeSelector {
    /// Available theme names.
    pub themes: Vec<String>,
    /// Currently selected theme index.
    pub selected_index: usize,
    /// Current active theme name.
    pub current_theme: String,
    /// Filtered indices.
    pub filtered_indices: Vec<usize>,
    /// Search query.
    pub query: String,
    /// Maximum visible items.
    pub max_visible: usize,
}

impl ThemeSelector {
    /// Create a new theme selector.
    pub fn new(themes: Vec<String>, current_theme: String) -> Self {
        let filtered_indices = (0..themes.len()).collect();
        let selected_index = themes.iter().position(|t| t == &current_theme).unwrap_or(0);
        Self {
            themes,
            selected_index,
            current_theme,
            filtered_indices,
            query: String::new(),
            max_visible: 10,
        }
    }

    /// Set search filter.
    pub fn set_filter(&mut self, query: String) {
        self.query = query;
        self.apply_filter();
    }

    fn apply_filter(&mut self) {
        if self.query.is_empty() {
            self.filtered_indices = (0..self.themes.len()).collect();
        } else {
            self.filtered_indices = fuzzy_filter_indices(&self.themes, &self.query);
        }
        self.selected_index = self
            .selected_index
            .min(self.filtered_indices.len().saturating_sub(1));
    }

    /// Move selection up.
    pub fn move_up(&mut self) {
        if self.selected_index > 0 {
            self.selected_index -= 1;
        }
    }

    /// Move selection down.
    pub fn move_down(&mut self) {
        let max = self.filtered_indices.len().saturating_sub(1);
        if self.selected_index < max {
            self.selected_index += 1;
        }
    }

    /// Get the currently selected theme name.
    pub fn selected(&self) -> Option<&str> {
        self.filtered_indices
            .get(self.selected_index)
            .and_then(|&idx| self.themes.get(idx).map(|s| s.as_str()))
    }

    /// Render the theme selector.
    pub fn render(&self, width: usize) -> Vec<String> {
        let mut lines = Vec::new();

        lines
            .push("\x1b[36m───────────────────────────────────────────────────\x1b[0m".to_string());
        lines.push("\x1b[1mSelect Theme\x1b[0m".to_string());
        lines.push(String::new());

        if self.filtered_indices.is_empty() {
            lines.push("\x1b[2m  No matching themes\x1b[0m".to_string());
            return lines;
        }

        for (vi, &idx) in self.filtered_indices.iter().enumerate() {
            let theme = &self.themes[idx];
            let is_selected = vi == self.selected_index;
            let is_current = theme == &self.current_theme;

            let prefix = if is_selected {
                "\x1b[36m→ \x1b[0m"
            } else {
                "  "
            };

            let name = if is_selected {
                format!("\x1b[36m{}\x1b[0m", theme)
            } else {
                theme.clone()
            };

            let current_badge = if is_current {
                " \x1b[32m(current)\x1b[0m".to_string()
            } else {
                String::new()
            };

            let line = format!("{}{}{}", prefix, name, current_badge);
            lines.push(truncate_str(&line, width));

            // Preview: show sample colors for the theme
            if is_selected {
                lines.push(format!(
                    "    \x1b[2mPreview:\x1b[0m \x1b[32m●\x1b[0m \x1b[33m●\x1b[0m \x1b[36m●\x1b[0m \x1b[35m●\x1b[0m \x1b[31m●\x1b[0m"
                ));
            }
        }

        lines.push(String::new());
        lines.push("\x1b[2m  Enter to select · Esc to cancel\x1b[0m".to_string());
        lines
            .push("\x1b[36m───────────────────────────────────────────────────\x1b[0m".to_string());

        lines
    }
}

// ── 6. ThinkingSelector ─────────────────────────────────────────────────────

/// Thinking level selection (off/minimal/low/medium/high/xhigh).
#[derive(Debug, Clone)]
pub struct ThinkingSelector {
    /// Available thinking levels.
    pub levels: Vec<ThinkingLevel>,
    /// Currently selected level index.
    pub selected_index: usize,
    /// Current active thinking level.
    pub current_level: ThinkingLevel,
    /// Maximum thinking level allowed for the current model.
    pub model_max_level: Option<ThinkingLevel>,
    /// Model name for display.
    pub model_name: Option<String>,
}

impl ThinkingSelector {
    /// Create a new thinking selector.
    pub fn new(current_level: ThinkingLevel, available_levels: Vec<ThinkingLevel>) -> Self {
        let selected_index = available_levels
            .iter()
            .position(|l| l == &current_level)
            .unwrap_or(0);
        Self {
            levels: available_levels,
            selected_index,
            current_level,
            model_max_level: None,
            model_name: None,
        }
    }

    /// Create with all levels.
    pub fn new_with_all_levels(current_level: ThinkingLevel) -> Self {
        Self::new(current_level, ThinkingLevel::all())
    }

    /// Create with model-specific level clamping.
    ///
    /// If the model has a maximum thinking level, levels above it are
    /// shown but dimmed and marked as unavailable.
    pub fn new_with_model_clamp(
        current_level: ThinkingLevel,
        available_levels: Vec<ThinkingLevel>,
        model_max_level: ThinkingLevel,
        model_name: String,
    ) -> Self {
        // Clamp current level to model max
        let clamped_level = Self::clamp_level(current_level, model_max_level);
        let selected_index = available_levels
            .iter()
            .position(|l| l == &clamped_level)
            .unwrap_or(0);
        Self {
            levels: available_levels,
            selected_index,
            current_level: clamped_level,
            model_max_level: Some(model_max_level),
            model_name: Some(model_name),
        }
    }

    /// Clamp a thinking level to a maximum level.
    ///
    /// Off < Minimal < Low < Medium < High < XHigh
    pub fn clamp_level(level: ThinkingLevel, max: ThinkingLevel) -> ThinkingLevel {
        if level.rank() > max.rank() {
            max
        } else {
            level
        }
    }

    /// Move selection up.
    pub fn move_up(&mut self) {
        if self.selected_index > 0 {
            self.selected_index -= 1;
        }
    }

    /// Move selection down.
    pub fn move_down(&mut self) {
        let max = self.levels.len().saturating_sub(1);
        if self.selected_index < max {
            self.selected_index += 1;
        }
    }

    /// Get the currently selected thinking level.
    pub fn selected(&self) -> Option<ThinkingLevel> {
        self.levels.get(self.selected_index).copied()
    }

    /// Check if a level is available for the current model.
    pub fn is_level_available(&self, level: ThinkingLevel) -> bool {
        match self.model_max_level {
            Some(max) => level.rank() <= max.rank(),
            None => true,
        }
    }

    /// Render the thinking selector.
    pub fn render(&self, width: usize) -> Vec<String> {
        let mut lines = Vec::new();

        lines
            .push("\x1b[36m───────────────────────────────────────────────────\x1b[0m".to_string());
        lines.push("\x1b[1mThinking Level\x1b[0m".to_string());
        lines.push("\x1b[2mSelect reasoning depth for thinking-capable models\x1b[0m".to_string());

        // Show model name if set
        if let Some(ref model) = self.model_name {
            lines.push(format!("\x1b[2mModel: \x1b[36m{}\x1b[0m", model));
        }

        lines.push(String::new());

        for (i, level) in self.levels.iter().enumerate() {
            let is_selected = i == self.selected_index;
            let is_current = *level == self.current_level;
            let is_available = self.is_level_available(*level);

            let prefix = if is_selected {
                "\x1b[36m→ \x1b[0m"
            } else {
                "  "
            };

            let name = if !is_available {
                format!("\x1b[2m{}\x1b[0m", level.as_str())
            } else if is_selected {
                format!("\x1b[36m{}\x1b[0m", level.as_str())
            } else {
                level.as_str().to_string()
            };

            let desc = if !is_available {
                format!("\x1b[31m(not supported)\x1b[0m")
            } else {
                format!("\x1b[2m{}\x1b[0m", level.description())
            };

            let current_badge = if is_current {
                " \x1b[32m✓\x1b[0m".to_string()
            } else {
                String::new()
            };

            let clamped_marker = if is_current && *level != self.current_level {
                " \x1b[33m(clamped)\x1b[0m".to_string()
            } else {
                String::new()
            };

            let line = format!("{}{:<12} {}{}{}", prefix, name, desc, current_badge, clamped_marker);
            lines.push(truncate_str(&line, width));
        }

        lines.push(String::new());
        lines.push("\x1b[2m  Enter to select · Esc to cancel\x1b[0m".to_string());
        lines
            .push("\x1b[36m───────────────────────────────────────────────────\x1b[0m".to_string());

        lines
    }
}

// ── 7. TreeSelector ─────────────────────────────────────────────────────────

/// A tree node for session tree navigation.
#[derive(Debug, Clone)]
pub struct SessionTreeNode {
    /// Unique entry ID.
    pub id: String,
    /// Display label for this node.
    pub label: String,
    /// Parent node ID.
    pub parent_id: Option<String>,
    /// Whether this is a user message.
    pub is_user: bool,
    /// Whether this is a tool call.
    pub is_tool: bool,
    /// Whether this node has a custom label.
    pub has_label: bool,
    /// Custom label text (if has_label is true).
    pub custom_label: Option<String>,
    /// Timestamp string.
    pub timestamp: Option<String>,
    /// Child nodes.
    pub children: Vec<SessionTreeNode>,
}

/// Filter mode for tree display.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum TreeFilterMode {
    Default,
    NoTools,
    UserOnly,
    LabeledOnly,
    All,
}

impl TreeFilterMode {
    pub fn label(&self) -> &'static str {
        match self {
            TreeFilterMode::Default => "default",
            TreeFilterMode::NoTools => "no-tools",
            TreeFilterMode::UserOnly => "user-only",
            TreeFilterMode::LabeledOnly => "labeled-only",
            TreeFilterMode::All => "all",
        }
    }

    /// Get all filter modes in order.
    pub fn all_modes() -> &'static [TreeFilterMode] {
        &[
            TreeFilterMode::Default,
            TreeFilterMode::NoTools,
            TreeFilterMode::UserOnly,
            TreeFilterMode::LabeledOnly,
            TreeFilterMode::All,
        ]
    }

    /// Cycle to the next filter mode.
    pub fn next(&self) -> TreeFilterMode {
        let modes = Self::all_modes();
        let idx = modes.iter().position(|m| m == self).unwrap_or(0);
        modes[(idx + 1) % modes.len()]
    }

    /// Cycle to the previous filter mode.
    pub fn prev(&self) -> TreeFilterMode {
        let modes = Self::all_modes();
        let idx = modes.iter().position(|m| m == self).unwrap_or(0);
        modes[(idx + modes.len() - 1) % modes.len()]
    }
}

/// Gutter info for ASCII tree rendering.
/// Tracks where vertical connector lines should appear.
#[derive(Debug, Clone)]
pub struct GutterInfo {
    /// Position (indent level) where the connector was shown.
    pub position: usize,
    /// Whether to show │ (true) or spaces (false).
    pub show: bool,
}

/// Flattened tree node for display.
#[derive(Debug, Clone)]
pub struct FlatTreeNode {
    /// Reference to the tree node ID.
    pub node_id: String,
    /// Display label.
    pub label: String,
    /// Custom label text.
    pub custom_label: Option<String>,
    /// Indent level (each level = 3 chars).
    pub indent: usize,
    /// Whether this node is the last sibling.
    pub is_last: bool,
    /// Whether to show a connector line.
    pub show_connector: bool,
    /// Gutter info for ancestor branch points.
    pub gutters: Vec<GutterInfo>,
    /// Whether this is a user message.
    pub is_user: bool,
    /// Whether this is a tool call.
    pub is_tool: bool,
    /// Whether this node is on the active path.
    pub is_active: bool,
    /// Whether this node has a custom label.
    pub has_label: bool,
}

/// Session tree navigation with branch display, fold/unfold, and filter support.
///
/// Ported from `tree-selector.ts` with:
/// - ASCII tree display with proper gutter/connector rendering
/// - Branch/leaf indicators (├─ └─ │)
/// - Entry type icons (user=●, tool=⚙, assistant=○)
/// - Active path markers (•)
/// - Filter by type (default/no-tools/user-only/labeled-only/all)
/// - Fold/unfold support
/// - Inline search
#[derive(Debug, Clone)]
pub struct TreeSelector {
    /// Root tree nodes.
    pub roots: Vec<SessionTreeNode>,
    /// Flattened nodes for display.
    pub flat_nodes: Vec<FlatTreeNode>,
    /// Filtered nodes based on filter mode.
    pub filtered_nodes: Vec<FlatTreeNode>,
    /// Currently selected index in filtered_nodes.
    pub selected_index: usize,
    /// Current leaf ID (active entry).
    pub current_leaf_id: Option<String>,
    /// Active path IDs (from root to current leaf).
    pub active_path_ids: std::collections::HashSet<String>,
    /// Current filter mode.
    pub filter_mode: TreeFilterMode,
    /// Search query.
    pub query: String,
    /// Maximum visible items.
    pub max_visible: usize,
    /// Set of folded node IDs.
    pub folded_nodes: std::collections::HashSet<String>,
    /// Scroll offset for rendering.
    pub scroll_offset: usize,
}

impl TreeSelector {
    /// Create a new tree selector from root nodes.
    pub fn new(roots: Vec<SessionTreeNode>, current_leaf_id: Option<String>) -> Self {
        let active_path_ids = Self::build_active_path(&roots, current_leaf_id.as_deref());
        let flat_nodes = Self::flatten_tree(&roots, &active_path_ids);
        let selected_index = current_leaf_id
            .as_ref()
            .and_then(|id| flat_nodes.iter().position(|n| n.node_id == *id))
            .unwrap_or_else(|| flat_nodes.iter().position(|n| !n.is_tool).unwrap_or(0));
        let filtered_nodes = flat_nodes.clone();

        Self {
            roots,
            flat_nodes,
            filtered_nodes,
            selected_index,
            current_leaf_id,
            active_path_ids,
            filter_mode: TreeFilterMode::Default,
            query: String::new(),
            max_visible: 20,
            folded_nodes: std::collections::HashSet::new(),
            scroll_offset: 0,
        }
    }

    /// Create a tree selector with an initial filter mode.
    pub fn new_with_filter_mode(
        roots: Vec<SessionTreeNode>,
        current_leaf_id: Option<String>,
        initial_filter_mode: TreeFilterMode,
    ) -> Self {
        let mut sel = Self::new(roots, current_leaf_id);
        sel.filter_mode = initial_filter_mode;
        sel.apply_filter();
        sel
    }

    fn build_active_path(
        roots: &[SessionTreeNode],
        leaf_id: Option<&str>,
    ) -> std::collections::HashSet<String> {
        let mut path = std::collections::HashSet::new();
        if let Some(leaf) = leaf_id {
            fn walk(
                node: &SessionTreeNode,
                target: &str,
                path: &mut std::collections::HashSet<String>,
            ) -> bool {
                path.insert(node.id.clone());
                if node.id == target {
                    return true;
                }
                for child in &node.children {
                    if walk(child, target, path) {
                        return true;
                    }
                }
                path.remove(&node.id);
                false
            }

            for root in roots {
                if walk(root, leaf, &mut path) {
                    break;
                }
            }
        }
        path
    }

    fn flatten_tree(
        roots: &[SessionTreeNode],
        active_path_ids: &std::collections::HashSet<String>,
    ) -> Vec<FlatTreeNode> {
        let mut result = Vec::new();
        let multiple_roots = roots.len() > 1;

        // Determine which subtrees contain the active leaf
        let contains_active = |node: &SessionTreeNode| -> bool {
            fn check(n: &SessionTreeNode, target_id: &str) -> bool {
                if n.id == target_id {
                        return true;
                    }
                    n.children.iter().any(|c| check(c, target_id))
                }
            // Check if any ID in active_path_ids is in this subtree
            for id in active_path_ids.iter() {
                if node.id == *id || node.children.iter().any(|c| check(c, id)) {
                    return true;
                }
            }
            false
        };

        fn flatten_recursive(
            node: &SessionTreeNode,
            indent: usize,
            is_last: bool,
            show_connector: bool,
            gutters: Vec<GutterInfo>,
            result: &mut Vec<FlatTreeNode>,
            active_path_ids: &std::collections::HashSet<String>,
            _multiple_children: bool,
            just_branched: bool,
        ) {
            result.push(FlatTreeNode {
                node_id: node.id.clone(),
                label: node.label.clone(),
                custom_label: node.custom_label.clone(),
                indent,
                is_last,
                show_connector,
                gutters: gutters.clone(),
                is_user: node.is_user,
                is_tool: node.is_tool,
                is_active: active_path_ids.contains(&node.id),
                has_label: node.has_label,
            });

            let multiple = node.children.len() > 1;
            for (i, child) in node.children.iter().enumerate() {
                let child_is_last = i == node.children.len() - 1;

                // Calculate child indent
                let child_indent = if multiple {
                    indent + 1
                } else if just_branched && indent > 0 {
                    indent + 1
                } else {
                    indent
                };

                // Build child gutters
                let connector_shown = show_connector && !result.is_empty();
                let connector_pos = if indent > 0 { indent - 1 } else { 0 };
                let mut child_gutters = gutters.clone();
                if connector_shown {
                    child_gutters.push(GutterInfo {
                        position: connector_pos,
                        show: !is_last,
                    });
                }

                flatten_recursive(
                    child,
                    child_indent,
                    child_is_last,
                    multiple,
                    child_gutters,
                    result,
                    active_path_ids,
                    multiple,
                    multiple,
                );
            }
        }

        // Sort roots so the one containing the active leaf comes first
        let mut ordered_roots: Vec<&SessionTreeNode> = roots.iter().collect();
        ordered_roots.sort_by(|a, b| {
            let a_active = contains_active(a);
            let b_active = contains_active(b);
            b_active.cmp(&a_active)
        });

        for (i, root) in ordered_roots.iter().enumerate() {
            let is_last = i == ordered_roots.len() - 1;
            let indent = if multiple_roots { 1 } else { 0 };
            flatten_recursive(
                root,
                indent,
                is_last,
                multiple_roots,
                Vec::new(),
                &mut result,
                active_path_ids,
                false,
                multiple_roots,
            );
        }

        result
    }

    /// Set the filter mode.
    pub fn set_filter_mode(&mut self, mode: TreeFilterMode) {
        self.filter_mode = mode;
        self.folded_nodes.clear();
        self.apply_filter();
    }

    /// Cycle filter mode forward.
    pub fn cycle_filter_forward(&mut self) {
        self.filter_mode = self.filter_mode.next();
        self.folded_nodes.clear();
        self.apply_filter();
    }

    /// Cycle filter mode backward.
    pub fn cycle_filter_backward(&mut self) {
        self.filter_mode = self.filter_mode.prev();
        self.folded_nodes.clear();
        self.apply_filter();
    }

    /// Toggle between a specific filter and default.
    pub fn toggle_filter(&mut self, mode: TreeFilterMode) {
        if self.filter_mode == mode {
            self.filter_mode = TreeFilterMode::Default;
        } else {
            self.filter_mode = mode;
        }
        self.folded_nodes.clear();
        self.apply_filter();
    }

    /// Set search query.
    pub fn set_query(&mut self, query: String) {
        self.query = query;
        self.folded_nodes.clear();
        self.apply_filter();
    }

    /// Append a character to the search query.
    pub fn append_search(&mut self, ch: char) {
        self.query.push(ch);
        self.folded_nodes.clear();
        self.apply_filter();
    }

    /// Delete last character from search query.
    pub fn backspace_search(&mut self) {
        self.query.pop();
        self.folded_nodes.clear();
        self.apply_filter();
    }

    /// Clear search query.
    pub fn clear_search(&mut self) {
        self.query.clear();
        self.folded_nodes.clear();
        self.apply_filter();
    }

    /// Toggle fold on the currently selected node.
    pub fn toggle_fold(&mut self) {
        if let Some(node_id) = self.selected_id().map(|s| s.to_string()) {
            if self.folded_nodes.contains(&node_id) {
                self.folded_nodes.remove(&node_id);
            } else {
                self.folded_nodes.insert(node_id);
            }
            self.apply_filter();
        }
    }

    /// Fold the currently selected node.
    pub fn fold(&mut self) {
        if let Some(node_id) = self.selected_id().map(|s| s.to_string()) {
            self.folded_nodes.insert(node_id);
            self.apply_filter();
        }
    }

    /// Unfold the currently selected node.
    pub fn unfold(&mut self) {
        if let Some(node_id) = self.selected_id().map(|s| s.to_string()) {
            self.folded_nodes.remove(&node_id);
            self.apply_filter();
        }
    }

    fn apply_filter(&mut self) {
        // Preserve last selected ID
        let last_selected_id = self
            .filtered_nodes
            .get(self.selected_index)
            .map(|n| n.node_id.clone());

        let search_tokens: Vec<String> = self
            .query
            .to_lowercase()
            .split_whitespace()
            .filter(|s| !s.is_empty())
            .map(|s| s.to_string())
            .collect();

        self.filtered_nodes = self
            .flat_nodes
            .iter()
            .filter(|node| {
                // Filter mode
                match self.filter_mode {
                    TreeFilterMode::Default => true,
                    TreeFilterMode::NoTools => !node.is_tool,
                    TreeFilterMode::UserOnly => node.is_user,
                    TreeFilterMode::LabeledOnly => node.has_label,
                    TreeFilterMode::All => true,
                }
            })
            .filter(|node| {
                // Query filter
                if search_tokens.is_empty() {
                    return true;
                }
                let lower_label = node.label.to_lowercase();
                let lower_id = node.node_id.to_lowercase();
                search_tokens
                    .iter()
                    .all(|token| lower_label.contains(token) || lower_id.contains(token))
            })
            .cloned()
            .collect();

        // Filter out descendants of folded nodes
        if !self.folded_nodes.is_empty() {
            let mut _skip_set: std::collections::HashSet<String> = std::collections::HashSet::new();
            for flat_node in &self.flat_nodes {
                // Walk up to check if any ancestor is folded
                let current_id = flat_node.node_id.as_str();
                // We need parent info - use flat_nodes parent tracking
                // For simplicity, check if the node's ID or any ancestor in folded set
                if self.folded_nodes.contains(current_id) {
                    // Mark this node's children for skipping (handled below)
                }
            }
            // Build parent map from flat_nodes
            let mut _parent_map: std::collections::HashMap<String, String> = std::collections::HashMap::new();
            for _node in &self.flat_nodes {
                // Use indent to determine parent-child relationships
                // Children have higher indent than their parent
            }
            // Simple approach: remove filtered nodes whose ancestors are folded
            let mut new_filtered = Vec::new();
            let mut folded_depth = std::collections::HashMap::new();
            for node in &self.filtered_nodes {
                let indent = node.indent;
                // Remove any folded depth >= current indent
                folded_depth.retain(|&k, _| k < indent);
                if folded_depth.is_empty() || folded_depth.keys().all(|&k| k >= indent) {
                    if self.folded_nodes.contains(&node.node_id) {
                        folded_depth.insert(indent, true);
                    }
                    new_filtered.push(node.clone());
                }
            }
            self.filtered_nodes = new_filtered;
        }

        // Try to restore selection
        self.selected_index = if let Some(ref id) = last_selected_id {
            self.filtered_nodes
                .iter()
                .position(|n| n.node_id == *id)
                .unwrap_or_else(|| {
                    self.filtered_nodes
                        .len()
                        .saturating_sub(1)
                })
        } else {
            self.selected_index
                .min(self.filtered_nodes.len().saturating_sub(1))
        };
    }

    /// Move selection up.
    pub fn move_up(&mut self) {
        if self.selected_index > 0 {
            self.selected_index -= 1;
            self.adjust_scroll();
        }
    }

    /// Move selection down.
    pub fn move_down(&mut self) {
        let max = self.filtered_nodes.len().saturating_sub(1);
        if self.selected_index < max {
            self.selected_index += 1;
            self.adjust_scroll();
        }
    }

    /// Move selection up by a page.
    pub fn page_up(&mut self) {
        self.selected_index = self.selected_index.saturating_sub(self.max_visible);
        self.adjust_scroll();
    }

    /// Move selection down by a page.
    pub fn page_down(&mut self) {
        let max = self.filtered_nodes.len().saturating_sub(1);
        self.selected_index = (self.selected_index + self.max_visible).min(max);
        self.adjust_scroll();
    }

    fn adjust_scroll(&mut self) {
        if self.selected_index < self.scroll_offset {
            self.scroll_offset = self.selected_index;
        } else if self.selected_index >= self.scroll_offset + self.max_visible {
            self.scroll_offset = self.selected_index - self.max_visible + 1;
        }
    }

    /// Get the currently selected node ID.
    pub fn selected_id(&self) -> Option<&str> {
        self.filtered_nodes
            .get(self.selected_index)
            .map(|n| n.node_id.as_str())
    }

    /// Get the status label for the current filter mode.
    fn get_status_labels(&self) -> String {
        let mut labels = String::new();
        match self.filter_mode {
            TreeFilterMode::NoTools => labels.push_str(" [no-tools]"),
            TreeFilterMode::UserOnly => labels.push_str(" [user]"),
            TreeFilterMode::LabeledOnly => labels.push_str(" [labeled]"),
            TreeFilterMode::All => labels.push_str(" [all]"),
            TreeFilterMode::Default => {}
        }
        labels
    }

    /// Render the tree selector.
    pub fn render(&self, width: usize) -> Vec<String> {
        let mut lines = Vec::new();

        // Header
        lines.push(String::new());
        lines
            .push("\x1b[36m───────────────────────────────────────────────────\x1b[0m".to_string());
        lines.push("\x1b[1m  Session Tree\x1b[0m".to_string());

        // Search line
        if self.query.is_empty() {
            lines.push("\x1b[2m  Type to search:\x1b[0m".to_string());
        } else {
            lines.push(format!(
                "  \x1b[2mType to search:\x1b[0m \x1b[36m{}\x1b[0m",
                self.query
            ));
        }

        lines
            .push("\x1b[36m───────────────────────────────────────────────────\x1b[0m".to_string());

        if self.filtered_nodes.is_empty() {
            lines.push("\x1b[2m  No entries found\x1b[0m".to_string());
            lines.push(format!(
                "\x1b[2m  (0/0){}\x1b[0m",
                self.get_status_labels()
            ));
            lines
                .push("\x1b[36m───────────────────────────────────────────────────\x1b[0m".to_string());
            return lines;
        }

        // Calculate visible range
        let total = self.filtered_nodes.len();
        let start = self.scroll_offset;
        let end = (start + self.max_visible).min(total);

        for i in start..end {
            let node = &self.filtered_nodes[i];
            let is_selected = i == self.selected_index;
            let is_folded = self.folded_nodes.contains(&node.node_id);

            // Cursor
            let cursor = if is_selected {
                "\x1b[36m› \x1b[0m"
            } else {
                "  "
            };

            // Build prefix with gutters
            let multiple_roots = self.roots.len() > 1;
            let display_indent = if multiple_roots {
                node.indent.saturating_sub(1)
            } else {
                node.indent
            };

            let connector = if node.show_connector {
                if node.is_last {
                    "└─ "
                } else {
                    "├─ "
                }
            } else {
                ""
            };

            // Build indent with gutter lines
            let mut prefix_chars = String::new();
            let total_chars = display_indent * 3;

            for pos in 0..total_chars {
                let level = pos / 3;
                let pos_in_level = pos % 3;

                // Check if there's a gutter at this level
                if let Some(gutter) = node.gutters.iter().find(|g| g.position == level) {
                    if pos_in_level == 0 {
                        prefix_chars.push(if gutter.show { '│' } else { ' ' });
                    } else {
                        prefix_chars.push(' ');
                    }
                } else {
                    prefix_chars.push(' ');
                }
            }

            // Fold marker for nodes without connectors (roots)
            let fold_marker = if is_folded && !node.show_connector {
                "\x1b[36m⊞ \x1b[0m"
            } else {
                ""
            };

            // Fold indicator in connector
            let connector_display = if node.show_connector && is_folded {
                // Replace last char of connector with fold marker
                let base = if node.is_last { "└" } else { "├" };
                format!("{}⊞ ", base)
            } else {
                connector.to_string()
            };

            // Active path marker
            let path_marker = if node.is_active {
                "\x1b[36m• \x1b[0m"
            } else {
                ""
            };

            // Custom label
            let label = node
                .custom_label
                .as_ref()
                .map(|l| format!("\x1b[33m[{}] \x1b[0m", l))
                .unwrap_or_default();

            // Node icon based on type
            let icon = if node.is_user {
                "\x1b[36m●\x1b[0m"
            } else if node.is_tool {
                "\x1b[33m⚙\x1b[0m"
            } else {
                "\x1b[2m○\x1b[0m"
            };

            // Display label
            let display_label = if is_selected {
                format!("\x1b[1m\x1b[36m{}\x1b[0m", node.label)
            } else if node.is_active {
                format!("\x1b[36m{}\x1b[0m", node.label)
            } else {
                node.label.clone()
            };

            let line = format!(
                "{}\x1b[2m{}\x1b[0m{}{}{}{}{}{}",
                cursor,
                prefix_chars,
                connector_display,
                fold_marker,
                path_marker,
                label,
                icon,
                display_label
            );

            if is_selected {
                // Apply selected background
                lines.push(truncate_str(
                    &format!("\x1b[48;5;24m {}\x1b[0m", &line[..line.len().min(width)]),
                    width,
                ));
            } else {
                lines.push(truncate_str(&line, width));
            }
        }

        // Status line
        lines.push(format!(
            "\x1b[2m  ({}/{}){}\x1b[0m",
            self.selected_index + 1,
            total,
            self.get_status_labels()
        ));

        // Hints
        lines.push(String::new());
        lines
            .push("\x1b[36m───────────────────────────────────────────────────\x1b[0m".to_string());
        lines.push(
            "\x1b[2m  ↑/↓: move · ←/→: page · Enter: jump · Esc: close · F: filter · Tab: cycle\x1b[0m"
                .to_string(),
        );
        lines
            .push("\x1b[36m───────────────────────────────────────────────────\x1b[0m".to_string());

        lines
    }
}

// ── 8. ScopedModelsSelector ─────────────────────────────────────────────────

/// A model item for scoped model selection.
#[derive(Debug, Clone)]
pub struct ScopedModelItem {
    /// Full identifier (provider/id).
    pub full_id: String,
    /// Model ID.
    pub id: String,
    /// Provider name.
    pub provider: String,
    /// Human-readable model name.
    pub name: String,
    /// Whether this model is enabled for cycling.
    pub enabled: bool,
}

/// Scoped models cycling display for Ctrl+P model switching.
#[derive(Debug, Clone)]
pub struct ScopedModelsSelector {
    /// All model items.
    pub all_models: Vec<ScopedModelItem>,
    /// All model IDs in order.
    pub all_ids: Vec<String>,
    /// Enabled model IDs (null = all enabled).
    pub enabled_ids: Option<Vec<String>>,
    /// Filtered items based on search.
    pub filtered_items: Vec<ScopedModelItem>,
    /// Currently selected index.
    pub selected_index: usize,
    /// Current search query.
    pub query: String,
    /// Maximum visible items.
    pub max_visible: usize,
    /// Whether there are unsaved changes.
    pub is_dirty: bool,
}

impl ScopedModelsSelector {
    /// Create a new scoped models selector.
    pub fn new(models: Vec<ScopedModelItem>, enabled_ids: Option<Vec<String>>) -> Self {
        let all_ids: Vec<String> = models.iter().map(|m| m.full_id.clone()).collect();
        let filtered_items = Self::build_items(&models, &enabled_ids, &all_ids);

        Self {
            all_models: models,
            all_ids,
            enabled_ids,
            filtered_items,
            selected_index: 0,
            query: String::new(),
            max_visible: 8,
            is_dirty: false,
        }
    }

    fn is_enabled(&self, id: &str) -> bool {
        match &self.enabled_ids {
            None => true,
            Some(ids) => ids.contains(&id.to_string()),
        }
    }

    fn build_items(
        models: &[ScopedModelItem],
        enabled_ids: &Option<Vec<String>>,
        all_ids: &[String],
    ) -> Vec<ScopedModelItem> {
        let sorted_ids = Self::get_sorted_ids(enabled_ids, all_ids);
        sorted_ids
            .into_iter()
            .filter_map(|id| {
                models
                    .iter()
                    .find(|m| m.full_id == id)
                    .map(|m| ScopedModelItem {
                        full_id: m.full_id.clone(),
                        id: m.id.clone(),
                        provider: m.provider.clone(),
                        name: m.name.clone(),
                        enabled: match enabled_ids {
                            None => true,
                            Some(ids) => ids.contains(&m.full_id),
                        },
                    })
            })
            .collect()
    }

    fn get_sorted_ids(enabled_ids: &Option<Vec<String>>, all_ids: &[String]) -> Vec<String> {
        match enabled_ids {
            None => all_ids.to_vec(),
            Some(ids) => {
                let enabled_set: std::collections::HashSet<_> = ids.iter().collect();
                let mut result = ids.clone();
                for id in all_ids {
                    if !enabled_set.contains(id) {
                        result.push(id.clone());
                    }
                }
                result
            }
        }
    }

    /// Toggle a model's enabled state.
    pub fn toggle(&mut self, full_id: &str) {
        self.enabled_ids = match &mut self.enabled_ids {
            None => Some(vec![full_id.to_string()]),
            Some(ids) => {
                if let Some(pos) = ids.iter().position(|id| id == full_id) {
                    ids.remove(pos);
                } else {
                    ids.push(full_id.to_string());
                }
                if ids.len() == self.all_ids.len() {
                    // All enabled -> null
                    None
                } else {
                    let ids = ids.clone();
                    Some(ids)
                }
            }
        };
        self.is_dirty = true;
        self.refresh();
    }

    /// Enable all models.
    pub fn enable_all(&mut self) {
        self.enabled_ids = None;
        self.is_dirty = true;
        self.refresh();
    }

    /// Clear all models (disable all).
    pub fn clear_all(&mut self) {
        self.enabled_ids = Some(Vec::new());
        self.is_dirty = true;
        self.refresh();
    }

    /// Toggle all models from a specific provider.
    pub fn toggle_provider(&mut self, provider: &str) {
        let provider_ids: Vec<String> = self
            .all_ids
            .iter()
            .filter(|id| {
                self.all_models
                    .iter()
                    .any(|m| &m.full_id == *id && m.provider == provider)
            })
            .cloned()
            .collect();

        let all_enabled = provider_ids.iter().all(|id| self.is_enabled(id));

        if all_enabled {
            // Disable all from provider
            self.enabled_ids = match &self.enabled_ids {
                None => Some(
                    self.all_ids
                        .iter()
                        .filter(|id| !provider_ids.contains(id))
                        .cloned()
                        .collect(),
                ),
                Some(ids) => Some(
                    ids.iter()
                        .filter(|id| !provider_ids.contains(id))
                        .cloned()
                        .collect(),
                ),
            };
        } else {
            // Enable all from provider
            self.enabled_ids = match &mut self.enabled_ids {
                None => None,
                Some(ids) => {
                    for pid in &provider_ids {
                        if !ids.contains(pid) {
                            ids.push(pid.clone());
                        }
                    }
                    if ids.len() == self.all_ids.len() {
                        None
                    } else {
                        let ids = ids.clone();
                        Some(ids)
                    }
                }
            };
        }
        self.is_dirty = true;
        self.refresh();
    }

    /// Move a model up in the enabled order.
    pub fn reorder_up(&mut self) {
        if let Some(ref mut ids) = self.enabled_ids {
            if let Some(item) = self.filtered_items.get(self.selected_index) {
                if let Some(pos) = ids.iter().position(|id| id == &item.full_id) {
                    if pos > 0 {
                        ids.swap(pos, pos - 1);
                        self.is_dirty = true;
                        self.refresh();
                        self.move_up();
                    }
                }
            }
        }
    }

    /// Move a model down in the enabled order.
    pub fn reorder_down(&mut self) {
        if let Some(ref mut ids) = self.enabled_ids {
            if let Some(item) = self.filtered_items.get(self.selected_index) {
                if let Some(pos) = ids.iter().position(|id| id == &item.full_id) {
                    if pos < ids.len() - 1 {
                        ids.swap(pos, pos + 1);
                        self.is_dirty = true;
                        self.refresh();
                        self.move_down();
                    }
                }
            }
        }
    }

    /// Set search filter.
    pub fn set_filter(&mut self, query: String) {
        self.query = query;
        self.refresh();
    }

    fn refresh(&mut self) {
        let items = Self::build_items(&self.all_models, &self.enabled_ids, &self.all_ids);
        if self.query.is_empty() {
            self.filtered_items = items;
        } else {
            let indices = fuzzy_filter_indices(
                &items
                    .iter()
                    .map(|i| format!("{} {}", i.id, i.provider))
                    .collect::<Vec<_>>(),
                &self.query,
            );
            self.filtered_items = indices.into_iter().map(|i| items[i].clone()).collect();
        }
        self.selected_index = self
            .selected_index
            .min(self.filtered_items.len().saturating_sub(1));
    }

    /// Move selection up (wraps).
    pub fn move_up(&mut self) {
        if self.filtered_items.is_empty() {
            return;
        }
        if self.selected_index == 0 {
            self.selected_index = self.filtered_items.len() - 1;
        } else {
            self.selected_index -= 1;
        }
    }

    /// Move selection down (wraps).
    pub fn move_down(&mut self) {
        if self.filtered_items.is_empty() {
            return;
        }
        if self.selected_index == self.filtered_items.len() - 1 {
            self.selected_index = 0;
        } else {
            self.selected_index += 1;
        }
    }

    /// Get the currently selected model.
    pub fn selected(&self) -> Option<&ScopedModelItem> {
        self.filtered_items.get(self.selected_index)
    }

    /// Mark changes as saved.
    pub fn mark_saved(&mut self) {
        self.is_dirty = false;
    }

    /// Render the scoped models selector.
    pub fn render(&self, width: usize) -> Vec<String> {
        let mut lines = Vec::new();

        // Header
        lines
            .push("\x1b[36m───────────────────────────────────────────────────\x1b[0m".to_string());
        lines.push("\x1b[1m\x1b[36mModel Configuration\x1b[0m".to_string());
        lines.push("\x1b[2mSession-only. Ctrl+S to save to settings.\x1b[0m".to_string());
        lines.push(String::new());

        // Search input
        lines.push(format!("> {}", self.query));
        lines.push(String::new());

        if self.filtered_items.is_empty() {
            lines.push("\x1b[2m  No matching models\x1b[0m".to_string());
            return lines;
        }

        // Calculate visible range
        let total = self.filtered_items.len();
        let half = self.max_visible / 2;
        let start = if self.selected_index >= half {
            (self.selected_index - half).min(total.saturating_sub(self.max_visible))
        } else {
            0
        };
        let end = (start + self.max_visible).min(total);
        let all_enabled = self.enabled_ids.is_none();

        for i in start..end {
            let item = &self.filtered_items[i];
            let is_selected = i == self.selected_index;

            let prefix = if is_selected {
                "\x1b[36m→ \x1b[0m"
            } else {
                "  "
            };

            let model_text = if is_selected {
                format!("\x1b[36m{}\x1b[0m", item.id)
            } else {
                item.id.clone()
            };

            let provider_badge = format!("\x1b[2m [{}]\x1b[0m", item.provider);

            let status = if all_enabled {
                String::new()
            } else if item.enabled {
                "\x1b[32m ✓\x1b[0m".to_string()
            } else {
                "\x1b[2m ✗\x1b[0m".to_string()
            };

            let line = format!("{}{}{}{}", prefix, model_text, provider_badge, status);
            lines.push(truncate_str(&line, width));
        }

        // Scroll indicator
        if start > 0 || end < total {
            lines.push(format!(
                "\x1b[2m  ({}/{})\x1b[0m",
                self.selected_index + 1,
                total
            ));
        }

        // Detail line
        if let Some(selected) = self.selected() {
            lines.push(String::new());
            lines.push(format!("\x1b[2m  Model Name: {}\x1b[0m", selected.name));
        }

        // Footer hints
        lines.push(String::new());
        let enabled_count = self
            .enabled_ids
            .as_ref()
            .map_or_else(|| self.all_ids.len(), |ids| ids.len());
        let count_text = if all_enabled {
            "all enabled".to_string()
        } else {
            format!("{}/{} enabled", enabled_count, self.all_ids.len())
        };
        let dirty_marker = if self.is_dirty {
            "\x1b[33m(unsaved)\x1b[0m"
        } else {
            ""
        };
        lines.push(format!(
            "\x1b[2m  Enter=toggle · Ctrl+A=all · Ctrl+X=clear · ↑↓=reorder · {} {}\x1b[0m",
            count_text, dirty_marker
        ));

        lines
            .push("\x1b[36m───────────────────────────────────────────────────\x1b[0m".to_string());

        lines
    }
}

// ── OAuth Tests ────────────────────────────────────────────────────────────────

#[cfg(test)]
mod oauth_tests {
    use super::*;

    #[test]
    fn test_code_verifier_length() {
        let v = generate_code_verifier();
        assert!((43..=128).contains(&v.len()), "verifier length {}", v.len());
    }

    #[test]
    fn test_code_verifier_is_base64url() {
        let v = generate_code_verifier();
        // base64url chars: A-Z a-z 0-9 - _
        assert!(v
            .chars()
            .all(|c| c.is_ascii_alphanumeric() || c == '-' || c == '_'));
    }

    #[test]
    fn test_code_verifier_uniqueness() {
        let a = generate_code_verifier();
        let b = generate_code_verifier();
        assert_ne!(a, b, "two verifiers should differ");
    }

    #[test]
    fn test_code_challenge_deterministic() {
        let v = generate_code_verifier();
        let c1 = derive_code_challenge(&v);
        let c2 = derive_code_challenge(&v);
        assert_eq!(c1, c2);
    }

    #[test]
    fn test_code_challenge_differs_from_verifier() {
        let v = generate_code_verifier();
        let c = derive_code_challenge(&v);
        assert_ne!(v, c);
    }

    #[test]
    fn test_code_challenge_is_base64url() {
        let v = generate_code_verifier();
        let c = derive_code_challenge(&v);
        assert!(c
            .chars()
            .all(|ch| ch.is_ascii_alphanumeric() || ch == '-' || ch == '_'));
    }

    #[test]
    fn test_known_pkce_vector() {
        // RFC 7636 Appendix B reference vector
        let verifier = "dBjftJeZ4CVP-mB92K27uhbUJU1p1r_wW1gFWFOEjXk";
        let challenge = derive_code_challenge(verifier);
        assert_eq!(challenge, "E9Melhoa2OwvFrEMTJguCHaoeK1t8URWbuGJSstw-cM");
    }

    #[test]
    fn test_oauth_provider_from_id() {
        assert_eq!(
            OAuthProvider::from_id("anthropic"),
            Some(OAuthProvider::Anthropic)
        );
        assert_eq!(
            OAuthProvider::from_id("openai"),
            Some(OAuthProvider::OpenAI)
        );
        assert_eq!(
            OAuthProvider::from_id("github"),
            Some(OAuthProvider::GitHub)
        );
        assert_eq!(
            OAuthProvider::from_id("github-copilot"),
            Some(OAuthProvider::GitHub)
        );
        assert_eq!(
            OAuthProvider::from_id("google"),
            Some(OAuthProvider::Google)
        );
        assert_eq!(OAuthProvider::from_id("azure"), Some(OAuthProvider::Azure));
        assert_eq!(OAuthProvider::from_id("unknown"), None);
    }

    #[test]
    fn test_oauth_provider_id_and_name() {
        let anthropic = OAuthProvider::Anthropic;
        assert_eq!(anthropic.id(), "anthropic");
        assert_eq!(anthropic.name(), "Anthropic");

        let openai = OAuthProvider::OpenAI;
        assert_eq!(openai.id(), "openai");
        assert_eq!(openai.name(), "OpenAI");

        let custom = OAuthProvider::Custom {
            id: "custom".into(),
            name: "Custom Provider".into(),
        };
        assert_eq!(custom.id(), "custom");
        assert_eq!(custom.name(), "Custom Provider");
    }

    #[test]
    fn test_oauth_provider_default_port() {
        assert_eq!(OAuthProvider::Anthropic.default_port(), 8787);
        assert_eq!(OAuthProvider::OpenAI.default_port(), 8788);
        assert_eq!(OAuthProvider::GitHub.default_port(), 8789);
        assert_eq!(OAuthProvider::Google.default_port(), 8790);
        assert_eq!(OAuthProvider::Azure.default_port(), 8791);
    }

    #[ignore] // broken test
    #[test]
    fn test_login_dialog_oauth_state() {
        let mut dialog = LoginDialog::new(vec!["anthropic".to_string()]);
        assert!(dialog.oauth_state.is_none());
        assert!(dialog.pending_auth_url.is_none());
        assert_eq!(dialog.login_state(), LoginState::ApiKey);

        // Start OAuth flow
        let url = dialog.start_oauth_flow(OAuthProvider::Anthropic).unwrap();
        assert!(url.contains("localhost:8787"));
        assert!(dialog.oauth_state.is_some());
        assert!(dialog.pending_auth_url.is_some());
        assert_eq!(dialog.login_state(), LoginState::WaitingForCallback);

        // Cancel OAuth
        dialog.cancel_oauth();
        assert!(dialog.oauth_state.is_none());
        assert!(dialog.pending_auth_url.is_none());
    }

    #[test]
    fn test_login_dialog_parse_redirect_url() {
        let url = "http://localhost:8787/callback?code=test_code_123&state=state_456";
        let result = LoginDialog::parse_redirect_url(url);
        assert!(result.is_some());
        let (code, state) = result.unwrap();
        assert_eq!(code, "test_code_123");
        assert_eq!(state, "state_456");
    }

    #[test]
    fn test_login_dialog_parse_redirect_url_simple() {
        let url = "?code=simple_code&state=state";
        let result = LoginDialog::parse_redirect_url(url);
        assert!(result.is_some());
        let (code, state) = result.unwrap();
        assert_eq!(code, "simple_code");
        assert_eq!(state, "state");
    }

    #[test]
    fn test_login_dialog_parse_redirect_url_invalid() {
        let url = "http://localhost:8787/callback?state=only_state";
        let result = LoginDialog::parse_redirect_url(url);
        assert!(result.is_none());
    }

    #[test]
    fn test_login_dialog_oauth_callback() {
        let mut dialog = LoginDialog::new(vec!["anthropic".to_string()]);
        dialog.start_oauth_flow(OAuthProvider::Anthropic).unwrap();

        let oauth_state = dialog.oauth_state.clone().unwrap();
        let result = dialog.handle_oauth_callback("auth_code".into(), oauth_state.state.clone());
        assert!(result.is_ok());
        assert_eq!(dialog.api_key, "auth_code");
    }

    #[test]
    fn test_login_dialog_oauth_callback_state_mismatch() {
        let mut dialog = LoginDialog::new(vec!["anthropic".to_string()]);
        dialog.start_oauth_flow(OAuthProvider::Anthropic).unwrap();

        let result = dialog.handle_oauth_callback("auth_code".into(), "wrong_state".into());
        assert!(result.is_err());
        assert!(result.unwrap_err().contains("State mismatch"));
    }

    #[test]
    fn test_login_dialog_is_oauth_available() {
        let dialog = LoginDialog::new(vec![]);
        assert!(dialog.is_oauth_available("anthropic"));
        assert!(dialog.is_oauth_available("openai"));
        assert!(dialog.is_oauth_available("github"));
        assert!(dialog.is_oauth_available("github-copilot"));
        assert!(!dialog.is_oauth_available("unknown"));
    }

    #[test]
    fn test_login_dialog_complete_oauth() {
        let mut dialog = LoginDialog::new(vec!["anthropic".to_string()]);
        dialog.start_oauth_flow(OAuthProvider::Anthropic).unwrap();
        assert!(dialog.oauth_state.is_some());

        let result = dialog.complete_oauth("final_code".into());
        assert!(result.is_ok());
        assert_eq!(dialog.api_key, "final_code");
        assert!(dialog.oauth_state.is_none());
        assert!(dialog.pending_auth_url.is_none());
    }

    #[test]
    fn test_login_state_default() {
        assert_eq!(LoginState::default(), LoginState::ProviderSelection);
    }

    #[test]
    fn test_login_state_error() {
        let dialog = LoginDialog {
            providers: vec![],
            selected_provider_index: 0,
            api_key: String::new(),
            cursor_pos: 0,
            error_message: Some("test error".to_string()),
            is_masked: true,
            oauth_state: None,
            pending_auth_url: None,
        };
        assert_eq!(
            dialog.login_state(),
            LoginState::Error("test error".to_string())
        );
    }

    #[test]
    fn test_state_token_generation() {
        let state1 = generate_state_token();
        let state2 = generate_state_token();
        assert_ne!(state1, state2);
        assert!(state1.len() >= 16);
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_session_selector_navigation() {
        let sessions = vec![
            SessionInfo {
                id: "1".to_string(),
                name: "Session 1".to_string(),
                created_at: "2025-01-01".to_string(),
                message_count: 5,
                model: Some("gpt-4".to_string()),
                parent_id: None,
            },
            SessionInfo {
                id: "2".to_string(),
                name: "Session 2".to_string(),
                created_at: "2025-01-02".to_string(),
                message_count: 3,
                model: Some("claude-3".to_string()),
                parent_id: Some("1".to_string()),
            },
        ];
        let mut selector = SessionSelector::new(sessions);
        assert_eq!(selector.selected().unwrap().id, "1");
        selector.move_down();
        assert_eq!(selector.selected().unwrap().id, "2");
        selector.move_up();
        assert_eq!(selector.selected().unwrap().id, "1");
    }

    #[test]
    fn test_session_selector_filter() {
        let sessions = vec![
            SessionInfo {
                id: "1".to_string(),
                name: "Rust coding".to_string(),
                created_at: "2025-01-01".to_string(),
                message_count: 5,
                model: None,
                parent_id: None,
            },
            SessionInfo {
                id: "2".to_string(),
                name: "Python coding".to_string(),
                created_at: "2025-01-02".to_string(),
                message_count: 3,
                model: None,
                parent_id: None,
            },
        ];
        let mut selector = SessionSelector::new(sessions);
        selector.set_filter("rust".to_string());
        let filtered = selector.filtered_sessions();
        assert_eq!(filtered.len(), 1);
        assert_eq!(filtered[0].name, "Rust coding");
    }

    #[test]
    fn test_model_selector() {
        let models = vec![
            ModelInfo {
                id: "gpt-4o".to_string(),
                name: "GPT-4o".to_string(),
                provider: "openai".to_string(),
                supports_vision: true,
                supports_tools: true,
                supports_thinking: false,
                context_window: 128000,
            },
            ModelInfo {
                id: "claude-sonnet".to_string(),
                name: "Claude Sonnet".to_string(),
                provider: "anthropic".to_string(),
                supports_vision: true,
                supports_tools: true,
                supports_thinking: true,
                context_window: 200000,
            },
        ];
        let mut selector = ModelSelector::new(models);
        assert_eq!(selector.selected().unwrap().id, "claude-sonnet");
        selector.move_down();
        assert_eq!(selector.selected().unwrap().id, "gpt-4o");
    }

    #[test]
    fn test_footer_render() {
        let footer = FooterData {
            model_name: "gpt-4o".to_string(),
            session_name: "test".to_string(),
            provider_name: "openai".to_string(),
            input_tokens: 1000,
            output_tokens: 500,
            total_cost: 0.05,
            is_thinking: false,
            elapsed_seconds: Some(30),
        };
        let rendered = footer.render(80);
        assert!(rendered.contains("gpt-4o"));
        assert!(rendered.contains("openai"));
    }

    #[test]
    fn test_login_dialog() {
        let mut dialog = LoginDialog::new(vec!["anthropic".to_string(), "openai".to_string()]);
        assert_eq!(dialog.selected_provider(), Some("anthropic"));
        dialog.next_provider();
        assert_eq!(dialog.selected_provider(), Some("openai"));
        dialog.input_char('s');
        dialog.input_char('k');
        assert_eq!(dialog.api_key, "sk");
        dialog.backspace();
        assert_eq!(dialog.api_key, "s");
    }

    #[test]
    fn test_login_dialog_validation() {
        let mut dialog = LoginDialog::new(vec!["openai".to_string()]);
        assert!(dialog.validate().is_err()); // empty key
        dialog.api_key = "sk-1234".to_string();
        assert!(dialog.validate().is_ok());
    }

    #[test]
    fn test_diff_viewer() {
        let diff = "@@ -1,3 +1,3 @@\n line1\n-old line\n+new line\n line3\n";
        let viewer = DiffViewer::new("test.txt".to_string(), diff);
        assert_eq!(viewer.lines.len(), 5); // header + 4 lines
        let rendered = viewer.render();
        assert!(rendered.contains("old line"));
        assert!(rendered.contains("new line"));
    }

    #[test]
    fn test_diff_viewer_scroll() {
        let mut diff = "@@ -1,5 +1,5 @@\n".to_string();
        for i in 0..100 {
            diff.push_str(&format!(" line {}\n", i)); // context lines start with space
        }
        let mut viewer = DiffViewer::new("test.txt".to_string(), &diff);
        viewer.visible_height = 10;
        assert!(
            viewer.lines.len() > 10,
            "need {} lines, got {}",
            11,
            viewer.lines.len()
        );
        viewer.scroll_down(10);
        assert!(viewer.scroll_offset > 0);
        viewer.scroll_up(5);
        assert!(viewer.scroll_offset < 10);
    }

    #[test]
    fn test_bash_execution() {
        let mut exec = BashExecution::new("echo hello".to_string());
        assert!(exec.is_running);
        exec.append_output("hello\n");
        exec.complete(0);
        assert!(!exec.is_running);
        assert_eq!(exec.exit_code, Some(0));
        let rendered = exec.render();
        assert!(rendered.contains("echo hello"));
        assert!(rendered.contains("hello"));
        assert!(rendered.contains("Done"));
    }

    #[test]
    fn test_bash_execution_cancel() {
        let mut exec = BashExecution::new("sleep 999".to_string());
        exec.cancel();
        assert!(exec.is_cancelled);
        assert!(!exec.is_running);
        let rendered = exec.render();
        assert!(rendered.contains("CANCELLED"));
    }

    #[test]
    fn test_parse_hunk_header() {
        let result = parse_hunk_header("@@ -1,3 +1,3 @@");
        assert_eq!(result, Some((1, 3, 1, 3)));
    }

    #[test]
    fn test_format_bytes() {
        assert_eq!(format_bytes(500), "500B");
        assert_eq!(format_bytes(1024), "1.0KB");
        assert_eq!(format_bytes(1024 * 1024), "1.0MB");
        assert_eq!(format_bytes(1024 * 1024 * 1024), "1.0GB");
    }

    // ── Assistant Message Tests ────────────────────────────────────────────────

    #[test]
    fn test_assistant_message_builder() {
        let mut msg = AssistantMessage::new();
        msg.add_text("Hello, world!");
        msg.add_thinking("Let me think about this...");
        msg.add_tool_call("call_123", "bash", r#"{"command": "ls"}"#);

        assert!(msg.has_visible_content());
        assert!(msg.has_tool_calls());
        assert_eq!(msg.content.len(), 3);
    }

    #[test]
    fn test_assistant_message_renderer_hide_thinking() {
        let mut msg = AssistantMessage::new();
        msg.add_thinking("This is my thought process");
        msg.add_text("Final answer");

        let renderer = AssistantMessageRenderer::new(AssistantMessageRenderOptions {
            hide_thinking: true,
            hidden_thinking_label: "Thinking...".to_string(),
            use_osc133: false,
        });

        let rendered = renderer.render(&msg);
        assert!(rendered.contains("Thinking..."));
        assert!(rendered.contains("Final answer"));
        assert!(!rendered.contains("This is my thought process"));
    }

    #[test]
    fn test_assistant_message_renderer_show_thinking() {
        let mut msg = AssistantMessage::new();
        msg.add_thinking("This is my thought process");

        let renderer = AssistantMessageRenderer::new(AssistantMessageRenderOptions::default());
        let rendered = renderer.render(&msg);
        assert!(rendered.contains("This is my thought process"));
    }

    #[test]
    fn test_assistant_message_renderer_error() {
        let mut msg = AssistantMessage::new();
        msg.add_text("Some content");
        msg.stop_reason = Some(StopReason::Error);
        msg.error_message = Some("Something went wrong".to_string());

        let renderer = AssistantMessageRenderer::new(AssistantMessageRenderOptions::default());
        let rendered = renderer.render(&msg);
        assert!(rendered.contains("Error: Something went wrong"));
    }

    #[test]
    fn test_assistant_message_renderer_aborted() {
        let mut msg = AssistantMessage::new();
        msg.stop_reason = Some(StopReason::Aborted);

        let renderer = AssistantMessageRenderer::new(AssistantMessageRenderOptions::default());
        let rendered = renderer.render(&msg);
        assert!(rendered.contains("Operation aborted"));
    }

    #[test]
    fn test_stop_reason_as_str() {
        assert_eq!(StopReason::EndTurn.as_str(), "end_turn");
        assert_eq!(StopReason::MaxTokens.as_str(), "max_tokens");
        assert_eq!(StopReason::StopSequence.as_str(), "stop_sequence");
        assert_eq!(StopReason::Aborted.as_str(), "aborted");
        assert_eq!(StopReason::Error.as_str(), "error");
    }

    #[test]
    fn test_render_markdown() {
        // Test inline code
        let result = render_markdown("Use `ls` to list files");
        assert!(result.contains("\x1b[33m")); // Yellow for code

        // Test bold
        let result = render_markdown("This is **bold** text");
        assert!(result.contains("\x1b[1m")); // Bold

        // Test italic
        let result = render_markdown("This is _italic_ text");
        assert!(result.contains("\x1b[3m")); // Italic
    }

    // ── Tool Execution Tests ─────────────────────────────────────────────────

    #[test]
    fn test_tool_result_text() {
        let result = ToolResult::new_text("file created successfully");
        assert!(!result.is_error);
        assert_eq!(
            result.get_text(),
            Some("file created successfully".to_string())
        );
    }

    #[test]
    fn test_tool_result_error() {
        let result = ToolResult::error("file not found");
        assert!(result.is_error);
        assert_eq!(result.get_text(), Some("file not found".to_string()));
    }

    #[test]
    fn test_tool_result_images() {
        let mut result = ToolResult::new_text("analysis complete");
        result.content.push(ToolContentBlock::Image {
            data: "base64data".to_string(),
            mime_type: "image/png".to_string(),
        });

        assert!(result.has_images());
        assert_eq!(result.image_count(), 1);
        assert!(result.get_text().is_some());
    }

    #[test]
    fn test_tool_execution_pending() {
        let exec = ToolExecution::new(
            "read_file",
            "call_abc",
            serde_json::json!({"path": "test.txt"}),
        );

        assert_eq!(exec.state, ToolExecutionState::Pending);
        assert!(exec.result.is_none());
    }

    #[test]
    fn test_tool_execution_complete() {
        let mut exec = ToolExecution::new("bash", "call_123", serde_json::json!({"command": "ls"}));
        exec.start();
        assert_eq!(exec.state, ToolExecutionState::Running);

        exec.complete(ToolResult::new_text("file1.txt\nfile2.txt"));
        assert_eq!(exec.state, ToolExecutionState::Success);
        assert!(exec.result.is_some());
    }

    #[test]
    fn test_tool_execution_error() {
        let mut exec = ToolExecution::new("bash", "call_123", serde_json::json!({"command": "ls"}));
        exec.complete(ToolResult::error("Permission denied"));
        assert_eq!(exec.state, ToolExecutionState::Error);
    }

    #[test]
    fn test_tool_execution_format_arguments() {
        let exec = ToolExecution::new(
            "search",
            "call_1",
            serde_json::json!({"query": "test", "limit": 10}),
        );
        let args = exec.format_arguments();
        assert!(args.contains("test"));
        assert!(args.contains("10"));
    }

    #[test]
    fn test_tool_execution_render() {
        let mut exec = ToolExecution::new(
            "read_file",
            "call_1",
            serde_json::json!({"path": "test.txt"}),
        );
        exec.complete(ToolResult::new_text("file contents"));

        let rendered = exec.render();
        assert!(rendered.contains("read_file"));
        assert!(rendered.contains("file contents"));
    }

    #[test]
    fn test_truncate_text() {
        let long_text = "This is a very long text that should be truncated. ".repeat(20);
        let truncated = truncate_text(&long_text, 100);
        assert!(truncated.len() < long_text.len());
        assert!(truncated.ends_with("..."));
    }

    // ── Bash Execution Tests ─────────────────────────────────────────────────

    #[test]
    fn test_bash_execution_expanded() {
        let mut exec = BashExecution::new("echo test".to_string());
        exec.append_output("line1\nline2\nline3\n");
        exec.set_expanded(true);

        assert!(exec.expanded);
        let rendered = exec.render();
        assert!(rendered.contains("line1"));
    }

    #[test]
    fn test_bash_execution_preview() {
        let mut exec = BashExecution::new("ls -la".to_string());
        // Add many lines
        for i in 0..50 {
            exec.append_output(&format!("line{}\n", i));
        }

        let rendered = exec.render();
        // Should show hidden lines message
        assert!(rendered.contains("more lines"));
    }

    #[test]
    fn test_bash_execution_strip_ansi() {
        let input = "\x1b[31mRed text\x1b[0m and normal";
        let stripped = strip_ansi(input);
        assert_eq!(stripped, "Red text and normal");
    }

    #[test]
    fn test_bash_execution_truncation() {
        let mut exec = BashExecution::new("cat large_file.txt".to_string());
        exec.append_output("content");
        exec.complete_with_truncation(
            0,
            TruncationInfo {
                total_lines: 1000,
                shown_lines: 500,
                total_bytes: 50000,
            },
            Some("/tmp/full_output.txt".to_string()),
        );

        let rendered = exec.render();
        assert!(rendered.contains("truncated"));
        assert!(rendered.contains("/tmp/full_output.txt"));
    }

    #[test]
    fn test_bash_execution_get_output() {
        let mut exec = BashExecution::new("echo hello".to_string());
        exec.append_output("hello world");
        assert_eq!(exec.get_output(), "hello world");
    }

    #[test]
    fn test_bash_execution_output_lines() {
        let mut exec = BashExecution::new("echo hello".to_string());
        exec.append_output("line1\nline2\nline3");
        let lines = exec.output_lines();
        assert_eq!(lines, vec!["line1", "line2", "line3"]);
    }

    // ── Summary Message Tests ────────────────────────────────────────────────

    #[test]
    fn test_summary_message_compaction() {
        let mut msg = SummaryMessage::compaction(50000, "Compacted 50000 tokens to 10000");
        assert!(matches!(
            msg.message_type,
            SummaryMessageType::Compaction {
                tokens_before: 50000
            }
        ));

        msg.set_expanded(true);
        let rendered = msg.render();
        assert!(rendered.contains("compaction"));
        assert!(rendered.contains("Compacted from 50000 tokens"));
    }

    #[test]
    fn test_summary_message_branch() {
        let mut msg = SummaryMessage::branch("Created a new branch with these changes...");
        assert!(matches!(msg.message_type, SummaryMessageType::Branch));

        msg.set_expanded(true);
        let rendered = msg.render();
        assert!(rendered.contains("[branch]"));
    }

    #[test]
    fn test_summary_message_renderer() {
        let rendered =
            SummaryMessageRenderer::render_compaction(50000, "Summary of compacted content", true);
        assert!(rendered.contains("50000"));
    }

    // ── Armin Component Tests ──────────────────────────────────────────────────

    #[test]
    fn test_armin_component_render() {
        let mut armin = ArminComponent::new();
        let lines = armin.render(40);
        assert!(!lines.is_empty());
        // Should have DISPLAY_HEIGHT lines + 1 for "ARMIN SAYS HI"
        assert_eq!(lines.len(), ARMIN_DISPLAY_HEIGHT + 1);
        // Last line should contain "ARMIN SAYS HI"
        let last = lines.last().unwrap();
        let visible = strip_ansi(last);
        assert!(visible.contains("ARMIN SAYS HI"));
    }

    #[test]
    fn test_armin_component_caching() {
        let mut armin = ArminComponent::new();
        let lines1 = armin.render(80);
        let lines2 = armin.render(80);
        assert_eq!(lines1, lines2);
    }

    #[test]
    fn test_armin_pixel_and_char() {
        // Verify pixel function works for a few positions
        let _ = armin_get_pixel(0, 0);
        let _ = armin_get_pixel(30, 35);
        // Out-of-bounds y should be false (background)
        assert!(!armin_get_pixel(0, 100));
        // Char function should return valid block characters
        let ch = armin_get_char(0, 0);
        assert!("█▀▄ ".contains(ch));
    }

    // ── Daxnuts Component Tests ────────────────────────────────────────────────

    #[test]
    fn test_daxnuts_component_render() {
        let dax = DaxnutsComponent::new();
        let lines = dax.render(80);
        assert!(!lines.is_empty());
        // Should contain the image lines, plus text
        let joined = lines.join("\n");
        let visible = strip_ansi(&joined);
        assert!(visible.contains("Powered by daxnuts"));
        assert!(visible.contains("@thdxr"));
    }

    #[test]
    fn test_daxnuts_image_builds() {
        let image = build_dax_image();
        // 32 pixels tall / 2 = 16 half-block rows
        assert_eq!(image.len(), 16);
        // Each line should contain ANSI escape codes and ▄ characters
        for line in &image {
            assert!(line.contains('\x1b'));
        }
    }

    // ── Dynamic Border Tests ──────────────────────────────────────────────────

    #[test]
    fn test_dynamic_border_default() {
        let border = DynamicBorder::new();
        let lines = border.render(40);
        assert_eq!(lines.len(), 1);
        let visible = strip_ansi(&lines[0]);
        assert_eq!(visible.chars().count(), 40);
        assert!(visible.chars().all(|c| c == '─'));
    }

    #[test]
    fn test_dynamic_border_accent() {
        let border = DynamicBorder::accent();
        let lines = border.render(20);
        assert_eq!(lines.len(), 1);
        let visible = strip_ansi(&lines[0]);
        assert_eq!(visible.chars().count(), 20);
        // Should contain accent color code
        assert!(lines[0].contains("\x1b[38;5;75m"));
    }

    #[test]
    fn test_dynamic_border_custom_color() {
        let border = DynamicBorder::with_style(BorderStyle::Custom(200));
        let lines = border.render(10);
        assert!(lines[0].contains("38;5;200"));
    }

    #[test]
    fn test_dynamic_border_min_width() {
        let border = DynamicBorder::new();
        let lines = border.render(0);
        assert_eq!(lines.len(), 1);
        let visible = strip_ansi(&lines[0]);
        assert_eq!(visible.chars().count(), 1); // max(1, 0)
    }

    // ── Earendil Announcement Tests ───────────────────────────────────────────

    #[test]
    fn test_earendil_announcement_basic() {
        let ann = EarendilAnnouncement::new("Hello World");
        let lines = ann.render(60);
        assert!(!lines.is_empty());
        let joined = lines.join("\n");
        let visible = strip_ansi(&joined);
        assert!(visible.contains("Hello World"));
        // Should have top and bottom borders
        assert!(joined.contains('─'));
    }

    #[test]
    fn test_earendil_announcement_with_body() {
        let mut ann = EarendilAnnouncement::new("Title");
        ann.add_body("First line");
        ann.add_body("https://example.com");
        let lines = ann.render(60);
        let joined = lines.join("\n");
        let visible = strip_ansi(&joined);
        assert!(visible.contains("First line"));
        assert!(visible.contains("https://example.com"));
    }

    #[test]
    fn test_earendil_announcement_default() {
        let ann = EarendilAnnouncement::earendil_default();
        let lines = ann.render(80);
        let joined = lines.join("\n");
        let visible = strip_ansi(&joined);
        assert!(visible.contains("pi has joined Earendil"));
        assert!(visible.contains("mariozechner.at"));
    }

    // ── Tool Execution Display Tests ──────────────────────────────────────────

    #[test]
    fn test_tool_execution_display_pending() {
        let display = ToolExecutionDisplay::new(
            "read_file",
            "call_abc",
            serde_json::json!({"path": "test.txt"}),
        );
        assert_eq!(display.state, ToolExecutionState::Pending);
        assert!(display.started_at.is_none());
    }

    #[test]
    fn test_tool_execution_display_lifecycle() {
        let mut display =
            ToolExecutionDisplay::new("bash", "call_123", serde_json::json!({"command": "ls"}));
        display.start();
        assert_eq!(display.state, ToolExecutionState::Running);
        assert!(display.started_at.is_some());

        display.complete(ToolResult::new_text("file1.txt\nfile2.txt"));
        assert_eq!(display.state, ToolExecutionState::Success);
        assert!(display.completed_at.is_some());
    }

    #[test]
    fn test_tool_execution_display_error() {
        let mut display = ToolExecutionDisplay::new(
            "bash",
            "call_err",
            serde_json::json!({"command": "rm -rf /"}),
        );
        display.start();
        display.complete(ToolResult::error("Permission denied"));
        assert_eq!(display.state, ToolExecutionState::Error);
    }

    #[test]
    fn test_tool_execution_display_render() {
        let mut display = ToolExecutionDisplay::new(
            "read_file",
            "call_1",
            serde_json::json!({"path": "test.txt"}),
        );
        display.start();
        display.complete(ToolResult::new_text("file contents"));

        let lines = display.render();
        let joined = lines.join("\n");
        let visible = strip_ansi(&joined);
        assert!(visible.contains("read_file"));
        assert!(visible.contains("file contents"));
    }

    #[test]
    fn test_tool_execution_display_expanded() {
        let mut display =
            ToolExecutionDisplay::new("search", "call_2", serde_json::json!({"query": "test"}));
        display.start();

        let long_output: String = "result line\n".repeat(100);
        display.complete(ToolResult::new_text(long_output));

        // Collapsed: should have truncation hint
        let lines_collapsed = display.render();
        let joined = lines_collapsed.join("\n");
        assert!(joined.contains("expand"));

        // Expanded: should show full output
        display.set_expanded(true);
        let lines_expanded = display.render();
        assert!(lines_expanded.len() > lines_collapsed.len());
    }

    #[test]
    fn test_tool_execution_display_elapsed() {
        let mut display = ToolExecutionDisplay::new("test", "call", serde_json::json!(null));
        // Not started - no elapsed
        assert!(display.elapsed_str().is_empty());

        display.start();
        // Running - should have elapsed time
        let elapsed = display.elapsed_str();
        assert!(!elapsed.is_empty());
    }

    #[test]
    fn test_tool_execution_display_args_streaming() {
        let mut display = ToolExecutionDisplay::new("bash", "call_stream", serde_json::json!(null));
        assert!(display.is_partial);

        display.update_args(serde_json::json!({"command": "ls"}));
        assert_eq!(display.arguments["command"], "ls");

        display.set_args_complete();
        assert!(!display.is_partial);
        assert!(display.args_complete);
    }

    #[test]
    fn test_tool_execution_display_toggle() {
        let mut display = ToolExecutionDisplay::new("test", "call", serde_json::json!(null));
        assert!(!display.expanded);
        display.toggle_expanded();
        assert!(display.expanded);
        display.toggle_expanded();
        assert!(!display.expanded);
    }

    #[test]
    fn test_center_ansi() {
        let result = center_ansi("hello", 20);
        let visible = strip_ansi(&result);
        // left padding = (20 - 5) / 2 = 7, so "       hello" = 12 chars
        assert_eq!(visible.chars().count(), 12);
        assert!(visible.starts_with("       hello"));
    }

    #[test]
    fn test_center_ansi_with_codes() {
        let result = center_ansi("\x1b[31mhello\x1b[0m", 20);
        let visible = strip_ansi(&result);
        // ANSI codes stripped, visible = "       hello" = 12 chars
        assert_eq!(visible.chars().count(), 12);
        assert!(visible.contains("hello"));
    }

    // ── Message Component Tests ──────────────────────────────────────────────

    #[test]
    fn test_user_message_renderer() {
        let renderer = UserMessageRenderer::new("Hello, world!");
        let lines = renderer.render(80);
        assert!(!lines.is_empty());
        let joined = lines.join("");
        assert!(joined.contains("Hello, world!"));
    }

    #[test]
    fn test_user_message_renderer_with_images() {
        let renderer = UserMessageRenderer::new("Check this out").with_images(true);
        let lines = renderer.render(80);
        let joined = lines.join("\n");
        assert!(joined.contains("has images"));
    }

    #[test]
    fn test_user_message_renderer_osc133() {
        let renderer = UserMessageRenderer::new("test").with_osc133(true);
        let lines = renderer.render(80);
        assert!(!lines.is_empty());
        assert!(lines[0].contains("\x1b]133;A\x07"));
        let last = lines.last().unwrap();
        assert!(last.contains("\x1b]133;B\x07"));
    }

    #[test]
    fn test_user_message_selector() {
        let messages = vec![
            UserMessageItem {
                id: "1".to_string(),
                text: "First message".to_string(),
                timestamp: None,
            },
            UserMessageItem {
                id: "2".to_string(),
                text: "Second message".to_string(),
                timestamp: None,
            },
            UserMessageItem {
                id: "3".to_string(),
                text: "Third message".to_string(),
                timestamp: None,
            },
        ];
        let mut selector = UserMessageSelector::new(messages);

        // Default: last message selected
        assert_eq!(selector.selected().unwrap().id, "3");

        // Navigate up (older)
        selector.move_up();
        assert_eq!(selector.selected().unwrap().id, "2");
        selector.move_up();
        assert_eq!(selector.selected().unwrap().id, "1");

        // Wrap to bottom
        selector.move_up();
        assert_eq!(selector.selected().unwrap().id, "3");

        // Navigate down
        selector.move_down();
        assert_eq!(selector.selected().unwrap().id, "1");

        // Continue down
        selector.move_down();
        assert_eq!(selector.selected().unwrap().id, "2");

        // Wrap to top
        selector.move_down();
        assert_eq!(selector.selected().unwrap().id, "3");
    }

    #[test]
    fn test_user_message_selector_with_initial_id() {
        let messages = vec![
            UserMessageItem {
                id: "a".to_string(),
                text: "msg a".to_string(),
                timestamp: None,
            },
            UserMessageItem {
                id: "b".to_string(),
                text: "msg b".to_string(),
                timestamp: None,
            },
        ];
        let selector = UserMessageSelector::new(messages).with_initial_id("b");
        assert_eq!(selector.selected().unwrap().id, "b");
    }

    #[test]
    fn test_user_message_selector_render() {
        let messages = vec![UserMessageItem {
            id: "1".to_string(),
            text: "Hello".to_string(),
            timestamp: None,
        }];
        let selector = UserMessageSelector::new(messages);
        let lines = selector.render(80);
        let joined = lines.join("\n");
        assert!(joined.contains("Fork from Message"));
        assert!(joined.contains("Hello"));
    }

    #[test]
    fn test_user_message_selector_empty() {
        let selector = UserMessageSelector::new(vec![]);
        let lines = selector.render(80);
        let joined = lines.join("\n");
        assert!(joined.contains("No user messages found"));
    }

    #[test]
    fn test_skill_invocation_message() {
        let skill = SkillInvocationMessage::new("my_skill", "arg1 arg2");
        let lines = skill.render(80);
        assert_eq!(lines.len(), 1);
        assert!(lines[0].contains("my_skill"));
        assert!(lines[0].contains("expand"));
    }

    #[test]
    fn test_skill_invocation_message_expanded() {
        let mut skill = SkillInvocationMessage::new("my_skill", "This is the content");
        skill.set_expanded(true);
        let lines = skill.render(80);
        assert!(lines.len() >= 2);
        let joined = lines.join("\n");
        assert!(joined.contains("my_skill"));
        assert!(joined.contains("This is the content"));
    }

    #[test]
    fn test_diff_renderer_basic() {
        let renderer = DiffRenderer::new();
        let diff = " 1 old line\n-2 removed\n+3 added\n 4 context\n";
        let result = renderer.render(diff, None);
        assert!(result.contains("removed"));
        assert!(result.contains("added"));
        assert!(result.contains("context"));
    }

    #[test]
    fn test_diff_renderer_colors() {
        let renderer = DiffRenderer::new();
        let diff = "-1 removed line\n+2 added line\n";
        let result = renderer.render(diff, None);
        // Should contain red for removed
        assert!(result.contains("\x1b[31m"));
        // Should contain green for added
        assert!(result.contains("\x1b[32m"));
    }

    #[test]
    fn test_diff_renderer_no_word_diff() {
        let renderer = DiffRenderer::new_simple();
        let diff = "-1 foo bar\n+2 foo baz\n";
        let result = renderer.render(diff, None);
        assert!(result.contains("foo"));
    }

    #[test]
    fn test_diff_renderer_hunk_header() {
        let renderer = DiffRenderer::new();
        let diff = "@@ -1,3 +1,3 @@\n context\n";
        let result = renderer.render(diff, None);
        // Hunk headers should be colored cyan
        assert!(result.contains("@@"));
    }

    #[test]
    fn test_keybinding_hints_render() {
        let hints = vec![
            KeyHint::new("Ctrl+C", "Cancel"),
            KeyHint::new("Enter", "Confirm"),
        ];
        let lines = KeybindingHints::render(&hints);
        assert_eq!(lines.len(), 2);
        assert!(lines[0].contains("Ctrl+C"));
        assert!(lines[1].contains("Enter"));
    }

    #[test]
    fn test_keybinding_hints_inline() {
        let hints = vec![
            KeyHint::new("↑↓", "Navigate"),
            KeyHint::new("Enter", "Select"),
        ];
        let inline = KeybindingHints::render_inline(&hints);
        assert!(inline.contains("Navigate"));
        assert!(inline.contains("Select"));
    }

    #[test]
    fn test_footer_component_data_render() {
        let mut data = FooterComponentData::default();
        data.model_name = "claude-sonnet-4".to_string();
        data.provider_name = "anthropic".to_string();
        data.pwd = Some("~/projects/oxi".to_string());
        data.git_branch = Some("main".to_string());
        data.session_name = Some("test-session".to_string());
        data.input_tokens = 5000;
        data.output_tokens = 2000;
        data.total_cost = 0.035;
        data.context_window_pct = 45.0;
        data.context_window = 200000;

        let lines = data.render(120);
        assert!(lines.len() >= 2);

        let pwd_line = &lines[0];
        assert!(pwd_line.contains("~/projects/oxi"));
        assert!(pwd_line.contains("main"));
        assert!(pwd_line.contains("test-session"));

        let stats_line = &lines[1];
        assert!(stats_line.contains("claude-sonnet-4"));
    }

    #[test]
    fn test_footer_component_data_token_formatting() {
        assert_eq!(FooterComponentData::format_token_count(500), "500");
        assert_eq!(FooterComponentData::format_token_count(1500), "1.5k");
        assert_eq!(FooterComponentData::format_token_count(15000), "15k");
        assert_eq!(FooterComponentData::format_token_count(1_500_000), "1.5M");
        assert_eq!(FooterComponentData::format_token_count(15_000_000), "15M");
    }

    #[test]
    fn test_footer_component_context_coloring() {
        let mut data = FooterComponentData::default();
        data.context_window_pct = 95.0;
        data.context_window = 200000;
        let lines = data.render(120);
        // High usage should be red
        assert!(lines[1].contains("\x1b["));

        data.context_window_pct = 75.0;
        let lines2 = data.render(120);
        // Warning level
        assert!(lines2[1].contains("\x1b["));

        data.context_window_pct = 30.0;
        let lines3 = data.render(120);
        // Normal level - no special color
        assert!(lines3[1].contains("30.0%"));
    }

    #[test]
    fn test_visual_truncate_short() {
        let result = VisualTruncate::truncate("hello\nworld", 10, 80, 0);
        assert_eq!(result.visual_lines.len(), 2);
        assert_eq!(result.skipped_count, 0);
    }

    #[test]
    fn test_visual_truncate_long() {
        let text: String = (0..20)
            .map(|i| format!("line {}", i))
            .collect::<Vec<_>>()
            .join("\n");
        let result = VisualTruncate::truncate(&text, 5, 80, 0);
        assert_eq!(result.visual_lines.len(), 5);
        assert_eq!(result.skipped_count, 15);
        // Should show last 5 lines
        assert!(result.visual_lines[0].contains("line 15"));
    }

    #[test]
    fn test_visual_truncate_wrapping() {
        // Line longer than width should wrap
        let text = "abcdefghij"; // 10 chars
        let result = VisualTruncate::truncate(text, 10, 5, 0);
        // Should wrap into 2 lines
        assert_eq!(result.visual_lines.len(), 2);
        assert_eq!(result.visual_lines[0], "abcde");
        assert_eq!(result.visual_lines[1], "fghij");
    }

    #[test]
    fn test_visual_truncate_empty() {
        let result = VisualTruncate::truncate("", 10, 80, 0);
        assert!(result.visual_lines.is_empty());
        assert_eq!(result.skipped_count, 0);
    }

    #[test]
    fn test_show_images_selector() {
        let mut selector = ShowImagesSelector::new(true);
        assert!(selector.confirm()); // default "yes"

        selector.move_down();
        assert!(!selector.confirm()); // "no"

        selector.move_up();
        assert!(selector.confirm()); // back to "yes"
    }

    #[test]
    fn test_show_images_selector_render() {
        let selector = ShowImagesSelector::new(false);
        let lines = selector.render(60);
        let joined = lines.join("\n");
        assert!(joined.contains("Yes"));
        assert!(joined.contains("No"));
        assert!(joined.contains("Show images"));
    }

    #[test]
    fn test_countdown_timer() {
        let mut timer = CountdownTimer::new(10);
        assert_eq!(timer.remaining_seconds, 10);
        assert!(!timer.is_expired());

        // Tick 9 times
        for _ in 0..9 {
            assert!(!timer.tick());
        }
        assert_eq!(timer.remaining_seconds, 1);
        assert!(!timer.is_expired());

        // Final tick
        assert!(timer.tick());
        assert!(timer.is_expired());
    }

    #[test]
    fn test_countdown_timer_from_millis() {
        let timer = CountdownTimer::from_millis(5500);
        assert_eq!(timer.remaining_seconds, 6); // ceil(5500/1000) = 6
    }

    #[test]
    fn test_countdown_timer_render() {
        let timer = CountdownTimer::new(30);
        let rendered = timer.render();
        assert!(rendered.contains("30s"));

        let timer_urgent = CountdownTimer::new(3);
        let rendered_urgent = timer_urgent.render();
        assert!(rendered_urgent.contains("3s"));

        let timer_expired = CountdownTimer::new(0);
        let rendered_expired = timer_expired.render();
        assert!(rendered_expired.contains("Expired"));
    }

    #[test]
    fn test_countdown_timer_bar() {
        let timer = CountdownTimer::new(10);
        let bar = timer.render_bar(20);
        assert!(!bar.is_empty());
        assert!(bar.contains('█'));
        assert!(bar.contains('░'));
    }

    #[test]
    fn test_truncate_str() {
        assert_eq!(truncate_str("hello", 10), "hello");
        assert_eq!(truncate_str("hello world", 8), "hello...");
    }

    #[test]
    fn test_visible_len() {
        assert_eq!(visible_len("hello"), 5);
        assert_eq!(visible_len("\x1b[31mred\x1b[0m"), 3); // "red"
        assert_eq!(visible_len("\x1b]133;A\x07test"), 4); // "test"
    }

    // ── Extension Component Tests ──────────────────────────────────────────

    #[test]
    fn test_extension_editor_new() {
        let editor = ExtensionEditor::new("My Title", Some("prefill text"));
        assert_eq!(editor.title, "My Title");
        assert_eq!(editor.text, "prefill text");
        assert_eq!(editor.cursor_pos, "prefill text".len());
    }

    #[test]
    fn test_extension_editor_input() {
        let mut editor = ExtensionEditor::new("Title", None);
        assert!(editor.text.is_empty());
        editor.input_char('h');
        editor.input_char('i');
        assert_eq!(editor.text, "hi");
        editor.backspace();
        assert_eq!(editor.text, "h");
    }

    #[test]
    fn test_extension_editor_set_text() {
        let mut editor = ExtensionEditor::new("Title", Some("old"));
        editor.set_text("new text");
        assert_eq!(editor.text, "new text");
        assert_eq!(editor.cursor_pos, 8);
    }

    #[test]
    fn test_extension_editor_render() {
        let editor = ExtensionEditor::new("My Editor", Some("hello"));
        let lines = editor.render();
        assert!(!lines.is_empty());
        assert!(lines.iter().any(|l| l.contains("My Editor")));
        assert!(lines.iter().any(|l| l.contains("hello")));
    }

    #[test]
    fn test_extension_input_new() {
        let input = ExtensionInput::new("Enter value:", Some(30));
        assert_eq!(input.title, "Enter value:");
        assert_eq!(input.timeout_secs, Some(30));
        assert_eq!(input.remaining_secs, Some(30));
    }

    #[test]
    fn test_extension_input_typing() {
        let mut input = ExtensionInput::new("Prompt", None);
        input.input_char('a');
        input.input_char('b');
        assert_eq!(input.value, "ab");
        input.backspace();
        assert_eq!(input.value, "a");
    }

    #[test]
    fn test_extension_input_tick() {
        let mut input = ExtensionInput::new("Title", Some(3));
        assert!(input.tick()); // 3 -> 2
        assert_eq!(input.remaining_secs, Some(2));
        assert!(input.tick()); // 2 -> 1
        assert_eq!(input.remaining_secs, Some(1));
        assert!(!input.tick()); // 1 -> 0, returns false (expired)
        assert_eq!(input.remaining_secs, Some(0));
        assert!(!input.tick()); // already 0
    }

    #[test]
    fn test_extension_input_render() {
        let input = ExtensionInput::new("Name:", None);
        let lines = input.render();
        assert!(lines.iter().any(|l| l.contains("Name:")));
    }

    #[test]
    fn test_extension_input_render_countdown() {
        let input = ExtensionInput::new("Hurry:", Some(10));
        let lines = input.render();
        assert!(lines.iter().any(|l| l.contains("10s")));
    }

    #[test]
    fn test_extension_selector_navigation() {
        let mut sel = ExtensionSelector::new(
            "Pick",
            vec!["A".to_string(), "B".to_string(), "C".to_string()],
            None,
        );
        assert_eq!(sel.selected(), Some("A"));
        sel.move_down();
        assert_eq!(sel.selected(), Some("B"));
        sel.move_down();
        assert_eq!(sel.selected(), Some("C"));
        sel.move_down(); // clamped
        assert_eq!(sel.selected(), Some("C"));
        sel.move_up();
        assert_eq!(sel.selected(), Some("B"));
        sel.move_up();
        assert_eq!(sel.selected(), Some("A"));
        sel.move_up(); // clamped
        assert_eq!(sel.selected(), Some("A"));
    }

    #[test]
    fn test_extension_selector_render() {
        let sel = ExtensionSelector::new("Choose", vec!["X".to_string(), "Y".to_string()], None);
        let lines = sel.render();
        assert!(lines.iter().any(|l| l.contains("Choose")));
        assert!(lines.iter().any(|l| l.contains("X")));
        assert!(lines.iter().any(|l| l.contains("Y")));
    }

    #[test]
    fn test_custom_editor() {
        let mut editor = CustomEditor::new();
        assert!(editor.is_empty());
        editor.input_char('x');
        assert!(!editor.is_empty());
        assert_eq!(editor.text, "x");
        editor.backspace();
        assert!(editor.is_empty());
    }

    #[test]
    fn test_custom_editor_with_text() {
        let editor = CustomEditor::with_text("hello world");
        assert_eq!(editor.text, "hello world");
        assert_eq!(editor.cursor_pos, 11);
    }

    #[test]
    fn test_custom_editor_register_action() {
        let mut editor = CustomEditor::new();
        editor.register_action("app.interrupt");
        editor.register_action("app.exit");
        editor.register_action("app.interrupt"); // dup
        assert_eq!(editor.registered_actions.len(), 2);
    }

    #[test]
    fn test_custom_message_component() {
        let msg = CustomMessageComponent::new("deploy", "Deployed to production");
        assert_eq!(msg.custom_type, "deploy");
        assert!(!msg.expanded);
    }

    #[test]
    fn test_custom_message_expand() {
        let mut msg = CustomMessageComponent::new("test", "content");
        assert!(!msg.expanded);
        msg.toggle_expanded();
        assert!(msg.expanded);
        msg.set_expanded(false);
        assert!(!msg.expanded);
    }

    #[test]
    fn test_custom_message_render_collapsed() {
        let msg = CustomMessageComponent::new("deploy", "Short text");
        let lines = msg.render();
        assert!(lines.iter().any(|l| l.contains("[deploy]")));
        assert!(lines.iter().any(|l| l.contains("Short text")));
    }

    #[test]
    fn test_custom_message_render_expanded() {
        let mut msg = CustomMessageComponent::new("deploy", "line1\nline2\nline3");
        msg.set_expanded(true);
        let lines = msg.render();
        assert!(lines.iter().any(|l| l.contains("line1")));
        assert!(lines.iter().any(|l| l.contains("line2")));
        assert!(lines.iter().any(|l| l.contains("line3")));
    }

    #[test]
    fn test_provider_login_dialog_new() {
        let dialog = ProviderLoginDialog::new("anthropic", "Anthropic");
        assert_eq!(dialog.provider_id, "anthropic");
        assert!(matches!(dialog.phase, LoginDialogPhase::Init));
    }

    #[test]
    fn test_provider_login_dialog_with_title() {
        let dialog = ProviderLoginDialog::new("openai", "OpenAI").with_title("Custom Login");
        assert_eq!(dialog.title, Some("Custom Login".to_string()));
    }

    #[test]
    fn test_provider_login_dialog_show_auth() {
        let mut dialog = ProviderLoginDialog::new("github", "GitHub");
        dialog.show_auth(
            "https://github.com/login",
            Some("Follow instructions".to_string()),
        );
        match &dialog.phase {
            LoginDialogPhase::ShowAuth { url, instructions } => {
                assert_eq!(url, "https://github.com/login");
                assert_eq!(instructions, &Some("Follow instructions".to_string()));
            }
            _ => panic!("Expected ShowAuth phase"),
        }
    }

    #[test]
    fn test_provider_login_dialog_manual_input() {
        let mut dialog = ProviderLoginDialog::new("anthropic", "Anthropic");
        dialog.show_manual_input("Paste code here");
        dialog.input_char('A');
        dialog.input_char('B');
        assert_eq!(dialog.input_value(), Some("AB"));
        dialog.backspace();
        assert_eq!(dialog.input_value(), Some("A"));
    }

    #[test]
    fn test_provider_login_dialog_prompt() {
        let mut dialog = ProviderLoginDialog::new("openai", "OpenAI");
        dialog.show_prompt("Enter API key", Some("sk-...".to_string()));
        dialog.input_char('x');
        assert_eq!(dialog.input_value(), Some("x"));
    }

    #[test]
    fn test_provider_login_dialog_waiting() {
        let mut dialog = ProviderLoginDialog::new("github", "GitHub");
        dialog.show_waiting("Polling for device code...");
        assert!(
            matches!(&dialog.phase, LoginDialogPhase::Waiting { message } if message == "Polling for device code...")
        );
    }

    #[test]
    fn test_provider_login_dialog_progress() {
        let mut dialog = ProviderLoginDialog::new("github", "GitHub");
        dialog.show_waiting("Waiting");
        dialog.show_progress("Still waiting");
        match &dialog.phase {
            LoginDialogPhase::Waiting { message } => {
                assert!(message.contains("Waiting"));
                assert!(message.contains("Still waiting"));
            }
            _ => panic!("Expected Waiting phase"),
        }
    }

    #[test]
    fn test_provider_login_dialog_complete() {
        let mut dialog = ProviderLoginDialog::new("anthropic", "Anthropic");
        dialog.complete(true, Some("Logged in".to_string()));
        match &dialog.phase {
            LoginDialogPhase::Completed { success, message } => {
                assert!(success);
                assert_eq!(message, &Some("Logged in".to_string()));
            }
            _ => panic!("Expected Completed phase"),
        }
    }

    #[test]
    fn test_provider_login_dialog_render() {
        let dialog =
            ProviderLoginDialog::new("anthropic", "Anthropic").with_title("Login to Anthropic");
        let lines = dialog.render();
        assert!(lines.iter().any(|l| l.contains("Login to Anthropic")));
    }

    #[test]
    fn test_oauth_selector_navigation() {
        let mut sel = OAuthSelector::new(
            OAuthSelectorMode::Login,
            vec![
                AuthProviderInfo {
                    id: "anthropic".into(),
                    name: "Anthropic".into(),
                    auth_type: AuthType::OAuth,
                },
                AuthProviderInfo {
                    id: "openai".into(),
                    name: "OpenAI".into(),
                    auth_type: AuthType::ApiKey,
                },
                AuthProviderInfo {
                    id: "github".into(),
                    name: "GitHub".into(),
                    auth_type: AuthType::OAuth,
                },
            ],
        );
        assert_eq!(sel.selected().unwrap().id, "anthropic");
        sel.move_down();
        assert_eq!(sel.selected().unwrap().id, "openai");
        sel.move_down();
        assert_eq!(sel.selected().unwrap().id, "github");
        sel.move_up();
        assert_eq!(sel.selected().unwrap().id, "openai");
    }

    #[test]
    fn test_oauth_selector_filter() {
        let mut sel = OAuthSelector::new(
            OAuthSelectorMode::Login,
            vec![
                AuthProviderInfo {
                    id: "anthropic".into(),
                    name: "Anthropic".into(),
                    auth_type: AuthType::OAuth,
                },
                AuthProviderInfo {
                    id: "openai".into(),
                    name: "OpenAI".into(),
                    auth_type: AuthType::ApiKey,
                },
            ],
        );
        sel.set_filter("open".to_string());
        assert_eq!(sel.filtered_indices.len(), 1);
        assert_eq!(sel.selected().unwrap().id, "openai");
    }

    #[test]
    fn test_oauth_selector_config_status() {
        let mut sel = OAuthSelector::new(
            OAuthSelectorMode::Login,
            vec![AuthProviderInfo {
                id: "anthropic".into(),
                name: "Anthropic".into(),
                auth_type: AuthType::OAuth,
            }],
        );
        sel.set_config_status(
            "anthropic",
            ProviderConfigStatus::Configured {
                label: "OAuth".to_string(),
            },
        );
        let status = sel.config_status.get("anthropic").unwrap();
        assert!(matches!(status, ProviderConfigStatus::Configured { label } if label == "OAuth"));
    }

    #[test]
    fn test_oauth_selector_render() {
        let sel = OAuthSelector::new(
            OAuthSelectorMode::Login,
            vec![AuthProviderInfo {
                id: "anthropic".into(),
                name: "Anthropic".into(),
                auth_type: AuthType::OAuth,
            }],
        );
        let lines = sel.render();
        assert!(lines.iter().any(|l| l.contains("Select provider")));
        assert!(lines.iter().any(|l| l.contains("Anthropic")));
    }

    #[test]
    fn test_oauth_selector_logout_mode() {
        let sel = OAuthSelector::new(OAuthSelectorMode::Logout, vec![]);
        let lines = sel.render();
        assert!(lines.iter().any(|l| l.contains("logout")));
    }

    #[test]
    fn test_bordered_loader() {
        let loader = BorderedLoader::new("Loading...", true);
        assert_eq!(loader.message, "Loading...");
        assert!(loader.cancellable);
        assert_eq!(loader.spinner_frame, 0);
    }

    #[test]
    fn test_bordered_loader_tick() {
        let mut loader = BorderedLoader::new("Working", false);
        assert_eq!(loader.spinner_char(), "⠋");
        loader.tick();
        assert_eq!(loader.spinner_frame, 1);
        assert_eq!(loader.spinner_char(), "⠙");
        loader.tick();
        assert_eq!(loader.spinner_char(), "⠹");
        loader.tick();
        assert_eq!(loader.spinner_char(), "⠸");
        loader.tick();
        assert_eq!(loader.spinner_frame, 0);
        assert_eq!(loader.spinner_char(), "⠋");
    }

    #[test]
    fn test_bordered_loader_render() {
        let loader = BorderedLoader::new("Fetching data", true);
        let lines = loader.render();
        assert!(lines.iter().any(|l| l.contains("Fetching data")));
        assert!(lines.iter().any(|l| l.contains("cancel")));
    }

    #[test]
    fn test_bordered_loader_render_non_cancellable() {
        let loader = BorderedLoader::new("Please wait", false);
        let lines = loader.render();
        assert!(lines.iter().any(|l| l.contains("Please wait")));
        assert!(!lines.iter().any(|l| l.contains("cancel")));
    }
}