ratcn 0.0.1

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

use ratatui::{
    buffer::Buffer,
    layout::{Position, Rect, Size},
    style::{Color, Style},
    text::Line,
    widgets::Widget,
};

use crate::Theme;
use crate::button_shape::{BOTTOM_CAP, TOP_CAP, cap_row, filled_middle, shape_width};
use crate::color::{
    DISABLED_DIM, FOCUS_DARKEN, FOCUS_LIGHTEN, HOVER_DARKEN, HOVER_LIGHTEN, darken, dim, lighten,
};
use crate::linear_nav;
use crate::list_core;
use crate::runtime::{
    Component, Event, EventCtx, EventResult, KeyCode, KeyEvent, MeasuredComponent, MouseButton,
    MouseKind, RenderCtx, Step,
};

/// How tall the tab row is drawn. Matches [`ButtonSize`](crate::ButtonSize),
/// since a tab is painted as a button.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
#[non_exhaustive]
pub enum TabsSize {
    /// One row: labels only.
    #[default]
    Small,
    /// Three rows: labels with a fill cap above and below.
    Large,
}

/// Whether moving between tabs also switches to them.
///
/// The distinction matters when switching tabs is expensive or destructive —
/// loading a page, discarding a draft. Manual lets the user look before
/// committing; automatic is faster when there is nothing to lose.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
#[non_exhaustive]
pub enum TabsActivation {
    /// Left/Right move a cursor between tabs; Enter or Space switches to the
    /// one under it. Two-step, and the default.
    #[default]
    Manual,
    /// Left/Right switch tabs directly, with no separate cursor. The
    /// [`tab_focus`](Tabs::tab_focus) binding is unused in this mode.
    Automatic,
}

impl TabsSize {
    /// Rows this size occupies — 1 for `Small`, 3 for `Large`.
    #[must_use]
    pub const fn height(self) -> u16 {
        match self {
            Self::Small => 1,
            Self::Large => 3,
        }
    }
}

/// Blank cells between adjacent tabs, so each reads as its own button.
const SPACING: u16 = 1;
/// Single-cell markers shown on a side that has hidden tabs.
const LEFT_MARKER: &str = "‹";
const RIGHT_MARKER: &str = "›";
const MARKER_WIDTH: u16 = 1;

/// Every color a tab row can paint.
///
/// A tab is drawn as a button, so these mirror [`ButtonStyle`](crate::ButtonStyle)
/// with one axis added: whether the tab is the selected one. The selected tab
/// looks like a `Default` button and the rest like `Secondary` buttons, which is
/// what makes the active tab read as the primary thing on the row.
///
/// Every state gives both a label color and a fill, so a row can express focus,
/// hover, and selection through text alone — set every `*_background` to the
/// surface the row sits on and the tabs lose their chrome without losing their
/// feedback.
///
/// When several states apply at once, disabled wins, then hovered, then focused,
/// then the resting colors — the same precedence [`ButtonStyle`](crate::ButtonStyle)
/// uses. Hover beating focus is what keeps pointing at an already-focused tab
/// visible.
///
/// "Focused" and "hovered" describe the tab under the cursor while the row has
/// keyboard focus or the pointer, not the whole row.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct TabsStyle {
    /// An unselected tab's label (the secondary button's foreground).
    pub foreground: Color,
    /// An unselected tab's fill (the secondary button's background).
    pub background: Color,
    /// An unselected tab's label while it is the cursor tab and the row has
    /// keyboard focus.
    pub focused_foreground: Color,
    /// An unselected tab's fill while it is the cursor tab and the row has
    /// keyboard focus (the secondary button's focus lighten).
    pub focused_background: Color,
    /// An unselected tab's label while the pointer is over the row and this is
    /// the cursor tab.
    pub hovered_foreground: Color,
    /// An unselected tab's fill while hovered. Wins over focus, as on buttons.
    pub hovered_background: Color,
    /// The selected tab's label (the default button's foreground).
    pub selected_foreground: Color,
    /// The selected tab's fill (the default button's background).
    pub selected_background: Color,
    /// The selected tab's label while focused.
    pub selected_focused_foreground: Color,
    /// The selected tab's fill while focused (the default button's focus
    /// darken).
    pub selected_focused_background: Color,
    /// The selected tab's label while hovered.
    pub selected_hovered_foreground: Color,
    /// The selected tab's fill while hovered.
    pub selected_hovered_background: Color,
    /// A disabled tab's label.
    pub disabled_foreground: Color,
    /// A disabled tab's fill.
    pub disabled_background: Color,
    /// A selected, disabled tab's label.
    pub selected_disabled_foreground: Color,
    /// A selected, disabled tab's fill. This state keeps selected identity while
    /// disabledness suppresses interaction.
    pub selected_disabled_background: Color,
}

impl TabsStyle {
    /// A neutral style using plain ANSI colors, for painting without a
    /// [`Theme`]. Prefer [`from_theme`](Self::from_theme) when one is available.
    #[must_use]
    pub const fn fallback() -> Self {
        Self {
            foreground: Color::Gray,
            background: Color::DarkGray,
            focused_foreground: Color::White,
            focused_background: Color::Gray,
            hovered_foreground: Color::White,
            hovered_background: Color::Gray,
            selected_foreground: Color::Black,
            selected_background: Color::Cyan,
            selected_focused_foreground: Color::Black,
            selected_focused_background: Color::Cyan,
            selected_hovered_foreground: Color::Black,
            selected_hovered_background: Color::LightCyan,
            disabled_foreground: Color::DarkGray,
            disabled_background: Color::Reset,
            selected_disabled_foreground: Color::DarkGray,
            selected_disabled_background: Color::Cyan,
        }
    }

    /// Derived from the theme exactly as the button variants are: the selected
    /// tab is a `Default` (primary) button, an unselected tab a `Secondary`
    /// button — including the same focus and hover shifts (a bright fill
    /// darkens, a dark fill lightens) and the disabled dim toward the surface.
    ///
    /// Label colors do not change with focus or hover here, since the fill
    /// already carries that. A style that flattens the fills should set the
    /// `*_foreground` slots instead.
    #[must_use]
    pub const fn from_theme(theme: &Theme) -> Self {
        Self {
            foreground: theme.secondary_foreground,
            background: theme.secondary,
            focused_foreground: theme.secondary_foreground,
            focused_background: lighten(theme.secondary, FOCUS_LIGHTEN),
            hovered_foreground: theme.secondary_foreground,
            hovered_background: lighten(theme.secondary, HOVER_LIGHTEN),
            selected_foreground: theme.primary_foreground,
            selected_background: theme.primary,
            selected_focused_foreground: theme.primary_foreground,
            selected_focused_background: darken(theme.primary, FOCUS_DARKEN),
            selected_hovered_foreground: theme.primary_foreground,
            selected_hovered_background: darken(theme.primary, HOVER_DARKEN),
            disabled_foreground: theme.muted_foreground,
            disabled_background: dim(theme.secondary, theme.surface, DISABLED_DIM),
            selected_disabled_foreground: theme.muted_foreground,
            selected_disabled_background: dim(theme.primary, theme.surface, DISABLED_DIM),
        }
    }

    /// The label and fill for one tab's paint — the single place a tab's state
    /// is turned into style.
    ///
    /// Selection picks the family: a selected tab paints like the default
    /// button, an unselected one like the secondary button. Within a family the
    /// precedence matches [`ButtonStyle`](crate::ButtonStyle) — disabled first,
    /// then hovered, then focused, then the resting colors. Hover beats focus so
    /// that pointing at an already-focused tab still changes something.
    #[expect(
        clippy::fn_params_excessive_bools,
        reason = "the four independent tab states; call sites read from named fields"
    )]
    fn resolve(
        &self,
        selected: bool,
        focused: bool,
        hovered: bool,
        disabled: bool,
    ) -> ResolvedTabStyle {
        let (foreground, fill) = match (selected, disabled, hovered, focused) {
            (true, true, _, _) => (
                self.selected_disabled_foreground,
                self.selected_disabled_background,
            ),
            (false, true, _, _) => (self.disabled_foreground, self.disabled_background),
            (true, false, true, _) => (
                self.selected_hovered_foreground,
                self.selected_hovered_background,
            ),
            (true, false, false, true) => (
                self.selected_focused_foreground,
                self.selected_focused_background,
            ),
            (true, false, false, false) => (self.selected_foreground, self.selected_background),
            (false, false, true, _) => (self.hovered_foreground, self.hovered_background),
            (false, false, false, true) => (self.focused_foreground, self.focused_background),
            (false, false, false, false) => (self.foreground, self.background),
        };
        ResolvedTabStyle { foreground, fill }
    }
}

/// One tab's resolved paint (see [`TabsStyle::resolve`]): the label color and
/// the button fill (which is also the cap color).
#[derive(Clone, Copy)]
struct ResolvedTabStyle {
    foreground: Color,
    fill: Color,
}

/// A tab row that only draws — an ordinary ratatui [`Widget`] with no focus,
/// events, or state. The selected tab is styled as a default button and the rest
/// as secondary buttons.
///
/// **Usable in any ratatui app.** Nothing here depends on
/// [`Ratcn`](crate::runtime::Ratcn) or the component layer: render it directly
/// and keep tracking the selected tab however you already do. Use [`Tabs`] when
/// you want keyboard traversal and selection messages handled for you; it paints
/// through this widget internally.
///
/// Inputs are parallel to the labels by index — which tab is selected
/// ([`selected_tab`](Self::selected_tab)), which tab the cursor is on
/// ([`focused_tab`](Self::focused_tab)), and which tabs are disabled
/// ([`disabled_tabs`](Self::disabled_tabs)) — the same shape as
/// [`ListWidget`](crate::ListWidget). When the tabs are wider than the row, the
/// widget shows the group around the selected/focused tab and adds `‹`/`›`
/// markers on sides that have hidden tabs.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct TabsWidget<'a> {
    labels: &'a [&'a str],
    selected_tab: Option<usize>,
    focused_tab: Option<usize>,
    disabled_tabs: &'a [bool],
    focused: bool,
    hovered_tab: Option<usize>,
    disabled: bool,
    size: TabsSize,
    style: TabsStyle,
}

impl<'a> TabsWidget<'a> {
    /// A row of `labels`, nothing selected or focused, using
    /// [`TabsStyle::fallback`].
    #[must_use]
    pub const fn new(labels: &'a [&'a str]) -> Self {
        Self {
            labels,
            selected_tab: None,
            focused_tab: None,
            disabled_tabs: &[],
            focused: false,
            hovered_tab: None,
            disabled: false,
            size: TabsSize::Small,
            style: TabsStyle::fallback(),
        }
    }

    /// Take colors from `theme`.
    #[must_use]
    pub const fn themed(mut self, theme: &Theme) -> Self {
        self.style = TabsStyle::from_theme(theme);
        self
    }

    /// Use these exact colors, ignoring any theme.
    #[must_use]
    pub const fn style(mut self, style: TabsStyle) -> Self {
        self.style = style;
        self
    }

    /// The index of the selected (active) tab, filled as the default button.
    #[must_use]
    pub const fn selected_tab(mut self, selected_tab: Option<usize>) -> Self {
        self.selected_tab = selected_tab;
        self
    }

    /// The index of the cursor tab. It paints with the focus shift only while
    /// the row itself has focus ([`focused`](Self::focused)).
    #[must_use]
    pub const fn focused_tab(mut self, focused_tab: Option<usize>) -> Self {
        self.focused_tab = focused_tab;
        self
    }

    /// A disabled mask parallel to the labels; a missing entry reads as
    /// enabled. Matches [`ListWidget::disabled_rows`](crate::ListWidget::disabled_rows).
    #[must_use]
    pub const fn disabled_tabs(mut self, disabled_tabs: &'a [bool]) -> Self {
        self.disabled_tabs = disabled_tabs;
        self
    }

    /// Paint the whole row as disabled, overriding per-tab styling: every tab
    /// takes the style's disabled colors (the selected tab its
    /// selected-disabled ones). Purely visual here — a paint widget receives no
    /// events to suppress.
    #[must_use]
    pub const fn disabled(mut self, disabled: bool) -> Self {
        self.disabled = disabled;
        self
    }

    /// Whether the row has keyboard focus, so the cursor tab shows its focus
    /// colors.
    #[must_use]
    pub const fn focused(mut self, focused: bool) -> Self {
        self.focused = focused;
        self
    }

    /// The tab under the pointer, by index. Hover wins over focus for this tab
    /// only; it does not alter the semantic cursor or selection.
    #[must_use]
    pub const fn hovered_tab(mut self, hovered_tab: Option<usize>) -> Self {
        self.hovered_tab = hovered_tab;
        self
    }

    /// Set the row height, one row or three. See [`TabsSize`].
    #[must_use]
    pub const fn size(mut self, size: TabsSize) -> Self {
        self.size = size;
        self
    }

    /// Return the row height for the configured size.
    #[must_use]
    pub const fn height(&self) -> u16 {
        self.size.height()
    }

    /// The total width the tabs want (every label plus its padding), for
    /// building layout constraints from the same labels that get painted.
    #[must_use]
    pub fn width(&self) -> u16 {
        row_width(self.labels.iter().copied())
    }
}

/// The width a full row of tab labels wants: each label plus padding, with the
/// standard gap between tabs.
fn row_width<'a>(labels: impl ExactSizeIterator<Item = &'a str>) -> u16 {
    let gap_count = u16::try_from(labels.len().saturating_sub(1)).unwrap_or(u16::MAX);
    labels
        .map(tab_width)
        .fold(0, u16::saturating_add)
        .saturating_add(gap_count.saturating_mul(SPACING))
}

impl Widget for TabsWidget<'_> {
    fn render(self, area: Rect, buf: &mut Buffer) {
        if area.width == 0 || area.height < self.height() {
            return;
        }
        // Keep the focused tab visible when it exists; otherwise keep the
        // selected tab visible. The component derives the same tab, so paint and
        // hit-testing agree.
        let must_show = self.focused_tab.or(self.selected_tab).unwrap_or(0);
        let TabLayout { tabs, left, right } = tab_layout(area, self.labels, self.size, must_show);
        for (index, rect) in tabs {
            let disabled = self.disabled || self.disabled_tabs.get(index).copied().unwrap_or(false);
            let cursor = self.focused_tab == Some(index);
            let resolved = self.style.resolve(
                self.selected_tab == Some(index),
                cursor && self.focused,
                self.hovered_tab == Some(index),
                disabled,
            );
            if self.size == TabsSize::Large {
                render_tab_cap(rect, resolved.fill, TOP_CAP, buf);
                render_tab_cap(
                    Rect::new(rect.x, rect.y + 2, rect.width, 1),
                    resolved.fill,
                    BOTTOM_CAP,
                    buf,
                );
            }
            Line::from(filled_middle(self.labels[index], rect.width as usize))
                .style(Style::default().fg(resolved.foreground).bg(resolved.fill))
                .render(
                    Rect::new(rect.x, rect.y + content_y_offset(self.size), rect.width, 1),
                    buf,
                );
        }
        for (marker, slot) in [(LEFT_MARKER, left), (RIGHT_MARKER, right)] {
            if let Some(rect) = slot {
                Line::from(marker)
                    .style(Style::default().fg(self.style.foreground))
                    .render(
                        Rect::new(
                            rect.x,
                            rect.y + content_y_offset(self.size),
                            MARKER_WIDTH,
                            1,
                        ),
                        buf,
                    );
            }
        }
    }
}

fn render_tab_cap(rect: Rect, fill: Color, symbol: &str, buf: &mut Buffer) {
    Line::from(cap_row(fill, symbol, rect.width as usize))
        .style(Style::default().fg(fill))
        .render(rect, buf);
}

const fn content_y_offset(size: TabsSize) -> u16 {
    match size {
        TabsSize::Small => 0,
        TabsSize::Large => 1,
    }
}

/// One tab: an identifying `value` and the `label` shown on it.
///
/// Selection is recorded as the value, not the tab's position, so the tab row
/// can be built from data that changes without the selection sliding onto a
/// different tab. `value` is usually the same enum the app matches on to decide
/// what to draw below the row. Values must be unique within a [`Tabs`]
/// declaration; duplicate values panic during declaration because selection and
/// tab focus would otherwise be ambiguous.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct Tab<T> {
    value: T,
    label: String,
    disabled: bool,
}

impl<T> Tab<T> {
    /// A tab identified by `value` and displayed as `label`.
    #[must_use]
    pub fn new(value: T, label: impl Into<String>) -> Self {
        Self {
            value,
            label: label.into(),
            disabled: false,
        }
    }

    /// Dim this tab and make it unselectable. Arrow keys skip it and clicks on
    /// it do nothing.
    #[must_use]
    pub const fn disabled(mut self, disabled: bool) -> Self {
        self.disabled = disabled;
        self
    }

    /// The identifying value given to [`new`](Self::new).
    #[must_use]
    pub const fn value(&self) -> &T {
        &self.value
    }

    /// The display label given to [`new`](Self::new).
    #[must_use]
    pub fn label(&self) -> &str {
        &self.label
    }

    /// Whether [`disabled`](Self::disabled) marked this tab unselectable.
    #[must_use]
    pub const fn is_disabled(&self) -> bool {
        self.disabled
    }
}

impl From<&str> for Tab<String> {
    fn from(label: &str) -> Self {
        Self::new(label.to_owned(), label)
    }
}

impl From<String> for Tab<String> {
    fn from(label: String) -> Self {
        Self::new(label.clone(), label)
    }
}

/// Reads a tab value (the selection or manual-mode tab focus) out of app
/// state; `None` means no tab holds that role yet.
type ReadFn<S, T> = Box<dyn Fn(&S) -> Option<T>>;
/// Builds the app's message from a tab value.
type OnChangeFn<T, M> = Box<dyn Fn(T) -> M>;
/// Resolves the tabs' style from the active theme (the style override).
type StyleFn = Box<dyn Fn(&Theme) -> TabsStyle>;

/// A focusable row of tabs, declared with
/// [`render_component`](crate::runtime::RenderCtx::render_component).
///
/// The horizontal counterpart of [`List`](crate::List), and it works the same
/// way: a cursor that moves and a selection that commits, both keyed by your own
/// values rather than by position.
///
/// The row draws only the tabs. What appears *below* them is the app's — match
/// on the selected value and render whatever that screen is. `Tabs` never owns
/// content.
///
/// # Cursor and selection
///
/// - [`tab_focus`](Self::tab_focus) is the cursor: which tab Left/Right, Home,
///   End, and typeahead are pointing at.
/// - [`selection`](Self::selection) is the active tab, the one whose content is
///   showing.
///
/// Typing a printable character jumps to the next enabled tab whose label
/// starts with it, cycling past the end — the same single-character typeahead
/// as [`List`](crate::List) and [`Select`](crate::Select). A character
/// matching no tab is ignored, so it still reaches the app as a hotkey.
///
/// With the default [`TabsActivation::Manual`], the cursor moves freely and
/// Enter or Space commits it. With [`TabsActivation::Automatic`] there is no
/// separate cursor at all — arrows switch tabs directly, and the `tab_focus`
/// binding goes unused.
///
/// Manual activation is keyboard-focusable only when both `tab_focus` and
/// `selection` are bound. Automatic activation is keyboard-focusable only when
/// `selection` is bound. An incompletely bound row can still paint and handle
/// wired pointer actions, but it is not a focus stop and ignores keys.
///
/// A left click selects a tab. Right and middle clicks are ignored.
/// A row with zero width is not interactive. Large tabs also require all three
/// rows of height; a shorter area paints nothing and does not participate in
/// focus or pointer routing. Narrow nonzero rows remain interactive because the
/// selected or focused tab clips to the available width.
///
/// ```
/// use ratcn::{Tab, Tabs};
///
/// # #[derive(Clone, Copy, PartialEq)]
/// # enum Screen { Overview, Settings }
/// # struct AppState { focused_tab: Screen, screen: Screen }
/// # enum Msg { TabFocused(Screen), ScreenChanged(Screen) }
/// let _tabs = Tabs::new([
///     Tab::new(Screen::Overview, "Overview"),
///     Tab::new(Screen::Settings, "Settings"),
/// ])
/// .tab_focus(|s: &AppState| Some(s.focused_tab), Msg::TabFocused)
/// .selection(|s: &AppState| Some(s.screen), Msg::ScreenChanged);
/// ```
pub struct Tabs<T, S, M> {
    items: Vec<Tab<T>>,
    /// Current semantic state bindings; events re-read these between frames.
    focused_tab: Option<ReadFn<S, T>>,
    on_focus_change: Option<OnChangeFn<T, M>>,
    selected: Option<ReadFn<S, T>>,
    on_select: Option<OnChangeFn<T, M>>,
    activation: TabsActivation,
    size: TabsSize,
    style: Option<StyleFn>,
    disabled: bool,
    /// Per-tab geometry from the last render as `(label index, rect)`, for
    /// mapping a click to a tab. Hidden tabs mean a tab's screen slot is not
    /// always its label index.
    hits: TabLayout,
}

impl<T, S, M> fmt::Debug for Tabs<T, S, M>
where
    T: fmt::Debug,
{
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        f.debug_struct("Tabs")
            .field("items", &self.items)
            .field("focused_tab", &self.focused_tab.is_some())
            .field("on_focus_change", &self.on_focus_change.is_some())
            .field("selected", &self.selected.is_some())
            .field("on_select", &self.on_select.is_some())
            .field("activation", &self.activation)
            .field("size", &self.size)
            .field("style", &self.style.is_some())
            .finish_non_exhaustive()
    }
}

impl<T, S, M> Tabs<T, S, M> {
    /// A row of `tabs`, with no bindings yet.
    ///
    /// Accepts anything that converts into a [`Tab`], so `["One", "Two"]` works
    /// for a quick row of strings — each label doubling as its value — and
    /// `[Tab::new(value, label), ...]` for anything keyed by a real value.
    #[must_use]
    pub fn new(tabs: impl IntoIterator<Item = impl Into<Tab<T>>>) -> Self {
        Self {
            items: tabs.into_iter().map(Into::into).collect(),
            focused_tab: None,
            on_focus_change: None,
            selected: None,
            on_select: None,
            activation: TabsActivation::Manual,
            size: TabsSize::Small,
            style: None,
            disabled: false,
            hits: TabLayout::default(),
        }
    }

    /// Bind the cursor: which tab is highlighted, and what message moves it.
    ///
    /// `read` returns the value of the tab the cursor is on, or `None` when it
    /// is nowhere yet — in which case the first arrow key moves it onto the
    /// first enabled tab. Left/Right, Home/End, and typeahead move it. Moving
    /// it does not switch tabs — [`selection`](Self::selection) does that, on
    /// Enter, Space, or a click.
    ///
    /// Named for tabs rather than items, unlike
    /// [`List::item_focus`](crate::List::item_focus) and
    /// [`Select::item_focus`](crate::Select::item_focus): those two share the
    /// [`ListItem`](crate::ListItem) vocabulary, while a tab is its own
    /// [`Tab`] type. The binding shape — a reader paired with its message
    /// constructor — is identical.
    ///
    /// Unlike those two, the pointer does *not* move this cursor: hovering a
    /// tab under [`TabsActivation::Automatic`] would switch the panel's
    /// content on the way past, so hover stays paint-only in both modes.
    ///
    /// Ignored under [`TabsActivation::Automatic`], which has no separate
    /// cursor.
    #[must_use]
    pub fn tab_focus(
        mut self,
        read: impl Fn(&S) -> Option<T> + 'static,
        on_change: impl Fn(T) -> M + 'static,
    ) -> Self {
        self.focused_tab = Some(Box::new(read));
        self.on_focus_change = Some(Box::new(on_change));
        self
    }

    /// Bind the active tab: which one is showing, and what message switches it.
    ///
    /// `read` returns the currently active value — the same one the app matches
    /// on to decide what to draw below the row — or `None` when no tab is
    /// active yet, in which case no tab paints as selected and the first arrow
    /// key targets the first enabled tab. `on_select` is called with the tab
    /// the user switched to.
    ///
    /// In manual activation, a pointer click can select a tab without a
    /// preceding pointer-move event. The update handling this message should
    /// store the selected value as both selection and tab focus so later
    /// keyboard input continues from the clicked tab.
    #[must_use]
    pub fn selection(
        mut self,
        read: impl Fn(&S) -> Option<T> + 'static,
        on_select: impl Fn(T) -> M + 'static,
    ) -> Self {
        self.selected = Some(Box::new(read));
        self.on_select = Some(Box::new(on_select));
        self
    }

    /// Dim the whole row and stop it responding.
    ///
    /// A disabled row is not focusable, so Tab skips it — as is a row that is
    /// empty or has every tab disabled, since there would be nothing for the
    /// cursor to land on. Disable individual tabs with [`Tab::disabled`].
    #[must_use]
    pub const fn disabled(mut self, disabled: bool) -> Self {
        self.disabled = disabled;
        self
    }

    /// Whether arrow keys switch tabs directly or only move a cursor. See
    /// [`TabsActivation`]; defaults to `Manual`.
    #[must_use]
    pub const fn activation(mut self, activation: TabsActivation) -> Self {
        self.activation = activation;
        self
    }

    /// Set the row height, one row or three. See [`TabsSize`].
    #[must_use]
    pub const fn size(mut self, size: TabsSize) -> Self {
        self.size = size;
        self
    }

    /// Return the row height for the configured size.
    #[must_use]
    pub const fn height(&self) -> u16 {
        self.size.height()
    }

    /// The total width the row wants (every label plus its padding and the
    /// standard gaps), for building layout constraints from the same tabs that
    /// get declared — mirrors the paint widget's `width`.
    #[must_use]
    pub fn width(&self) -> u16 {
        row_width(self.items.iter().map(|tab| tab.label.as_str()))
    }

    /// Override the [`TabsStyle`], instead of the one derived from the active
    /// theme. Resolved from the theme at render time, so a style built from
    /// `theme` follows theme switches; a fixed style ignores the argument
    /// (`|_| STYLE`).
    #[must_use]
    pub fn style(mut self, style: impl Fn(&Theme) -> TabsStyle + 'static) -> Self {
        self.style = Some(Box::new(style));
        self
    }

    fn disabled_at(&self, index: usize) -> bool {
        self.items.get(index).is_some_and(|tab| tab.disabled)
    }

    /// The item index under a screen cell, from the last render's geometry.
    /// `None` if the cell is outside every visible tab.
    fn tab_at(&self, column: u16, row: u16) -> Option<usize> {
        let point = Position { x: column, y: row };
        self.hits
            .tabs
            .iter()
            .find(|(_, rect)| rect.contains(point))
            .map(|(index, _)| *index)
    }

    fn marker_at(&self, column: u16, row: u16) -> Option<Step> {
        let point = Position { x: column, y: row };
        if self.hits.left.is_some_and(|rect| rect.contains(point)) {
            Some(Step::Backward)
        } else if self.hits.right.is_some_and(|rect| rect.contains(point)) {
            Some(Step::Forward)
        } else {
            None
        }
    }
}

/// Which way a key steps along a tab strip, or `None` if it does not step.
///
/// The horizontal counterpart of the vertical map in
/// [`linear_nav`](crate::linear_nav): Left/Right for everyone, `h`/`l` for
/// `vi`, and Ctrl+P/Ctrl+N for readline. A tab strip is a row, so `h`/`l` are
/// its `vi` keys rather than `j`/`k`.
fn step_direction(key: KeyEvent) -> Option<Step> {
    if key.modifiers.alt || key.modifiers.shift {
        return None;
    }
    if key.modifiers.ctrl {
        return match key.code {
            KeyCode::Char('n') => Some(Step::Forward),
            KeyCode::Char('p') => Some(Step::Backward),
            _ => None,
        };
    }
    match key.code {
        KeyCode::Left | KeyCode::Char('h') => Some(Step::Backward),
        KeyCode::Right | KeyCode::Char('l') => Some(Step::Forward),
        _ => None,
    }
}

impl<T, S, M> Tabs<T, S, M>
where
    T: Clone + PartialEq,
{
    fn index_of(&self, value: &T) -> Option<usize> {
        self.items.iter().position(|tab| &tab.value == value)
    }

    /// The index of the selected tab, or `None` when nothing is selected yet or
    /// the stored value matches no tab (render/route defensively).
    fn selected_index(&self, state: &S) -> Option<usize> {
        let value = (self.selected.as_ref()?)(state)?;
        self.index_of(&value)
    }

    /// The mode-correct tab-local cursor. Automatic activation uses selection;
    /// manual activation uses its persistent tab-focus binding.
    fn cursor_index(&self, state: &S) -> Option<usize> {
        match self.activation {
            TabsActivation::Automatic => self.selected_index(state),
            TabsActivation::Manual => self
                .focused_tab
                .as_ref()
                .and_then(|read| self.index_of(&read(state)?)),
        }
    }

    fn keyboard_enabled(&self) -> bool {
        self.selected.is_some()
            && match self.activation {
                TabsActivation::Manual => self.focused_tab.is_some(),
                TabsActivation::Automatic => true,
            }
    }

    /// Commit the tab at `index` as the selection when wired.
    fn select(&self, index: usize) -> EventResult<M> {
        match &self.on_select {
            Some(on_select) => EventResult::Emit(on_select(self.items[index].value.clone())),
            None => EventResult::Ignored,
        }
    }

    /// Move focus to the tab at `index`, or let the event bubble when no focus
    /// handler is wired.
    fn move_focus(&self, index: usize) -> EventResult<M> {
        match &self.on_focus_change {
            Some(on_focus_change) => {
                EventResult::Emit(on_focus_change(self.items[index].value.clone()))
            }
            None => EventResult::Ignored,
        }
    }

    /// Map one key against the tab row.
    ///
    /// The tabs row runs horizontally, so the key map is local rather than
    /// [`linear_nav::nav_key_target`]: that helper steps with Up and Down, and
    /// pages with `PageUp` and `PageDown`, neither of which suits a row. Here
    /// Left and Right step by one tab, and Home and End jump to the first and
    /// last enabled tab. There is no Page-key navigation on `Tabs` — `PageUp`
    /// and `PageDown` are ignored and bubble as app hotkeys. The index
    /// arithmetic underneath is still [`linear_nav`]'s, including the
    /// `first_enabled`/`last_enabled` that Home and End call.
    fn handle_key(&self, key: KeyEvent, state: &S) -> EventResult<M> {
        if !self.keyboard_enabled() {
            return EventResult::Ignored;
        }
        let len = self.items.len();
        if linear_nav::first_enabled(len, |index| self.disabled_at(index)).is_none() {
            return EventResult::Ignored;
        }
        let cursor = self.cursor_index(state);
        // Stepping is asked first because it owns the Ctrl chords that the
        // modifier gate below rejects.
        if let Some(direction) = step_direction(key) {
            let index = cursor.map_or_else(
                || linear_nav::first_enabled(len, |i| self.disabled_at(i)).expect("checked above"),
                |from| linear_nav::step_enabled(len, from, direction, |i| self.disabled_at(i)),
            );
            return self.apply_target(Some(index), cursor);
        }
        if linear_nav::has_reserved_modifier(key) {
            return EventResult::Ignored;
        }
        let next = match key.code {
            KeyCode::Home => linear_nav::first_enabled(len, |i| self.disabled_at(i)),
            KeyCode::End => linear_nav::last_enabled(len, |i| self.disabled_at(i)),
            KeyCode::Enter | KeyCode::Char(' ') => {
                return match cursor {
                    Some(index) if self.disabled_at(index) => EventResult::Ignored,
                    Some(index) => self.select(index),
                    None => EventResult::Ignored,
                };
            }
            // Anything else — including every letter but `h` and `l` — bubbles,
            // so the app keeps its single-key hotkeys while tabs have focus.
            _ => return EventResult::Ignored,
        };
        self.apply_target(next, cursor)
    }

    /// Commit a resolved navigation target under the configured activation
    /// mode: manual moves the cursor, automatic also selects.
    fn apply_target(&self, next: Option<usize>, cursor: Option<usize>) -> EventResult<M> {
        match next {
            Some(index) if Some(index) != cursor => match self.activation {
                TabsActivation::Manual => self.move_focus(index),
                TabsActivation::Automatic => self.select(index),
            },
            // A resolved-but-unchanged target (already at the edge, or Home/End
            // onto the current tab) is still ours to swallow.
            Some(_) => EventResult::Consumed,
            // Nothing to move to (no current focused tab, or every tab disabled).
            None => EventResult::Ignored,
        }
    }
}

impl<T, S, M> Component<S, M> for Tabs<T, S, M>
where
    T: Clone + PartialEq,
{
    fn prepare(&mut self, _state: &S) {
        list_core::assert_unique_values(self.items.iter().map(Tab::value), "Tabs");
    }

    fn render(&mut self, ctx: &mut RenderCtx<'_, '_, S, M>) {
        let area = ctx.area();
        let state = ctx.state();
        let selected = self.selected_index(state);
        let cursor = self.cursor_index(state);
        let labels: Vec<&str> = self.items.iter().map(|tab| tab.label.as_str()).collect();
        let disabled: Vec<bool> = (0..self.items.len()).map(|i| self.disabled_at(i)).collect();
        // Capture geometry for click hit-testing. The widget chooses visible
        // tabs from the same inputs, so paint and routing agree.
        let must_show = cursor.or(selected).unwrap_or(0);
        self.hits = tab_layout(area, &labels, self.size, must_show);
        let hovered_tab = ctx.hover_position().and_then(|position| {
            self.hits
                .tabs
                .iter()
                .find(|(_, rect)| rect.contains(position))
                .map(|(index, _)| *index)
        });
        let style = match &self.style {
            Some(style) => style(ctx.theme),
            None => TabsStyle::from_theme(ctx.theme),
        };
        let widget = TabsWidget::new(&labels)
            .selected_tab(selected)
            .focused_tab(cursor)
            .disabled_tabs(&disabled)
            .focused(ctx.focused)
            .hovered_tab(hovered_tab)
            .disabled(self.disabled)
            .size(self.size)
            .style(style);
        ctx.render_widget(widget, area);
    }

    fn handle_event(
        &mut self,
        event: &Event,
        state: &S,
        _ctx: &mut EventCtx<'_>,
    ) -> EventResult<M> {
        if self.disabled || self.items.is_empty() {
            return EventResult::Ignored;
        }
        match event {
            Event::Mouse(mouse) => match mouse.kind {
                // The runtime focuses an unhandled primary down on the hit
                // component. Consume disabled tabs so that fallback cannot
                // focus this row when its disabled content was clicked.
                MouseKind::Down(MouseButton::Left) => match self.tab_at(mouse.column, mouse.row) {
                    Some(index) if self.disabled_at(index) => EventResult::Consumed,
                    _ => EventResult::Ignored,
                },
                // Hover is paint-only here, unlike `List` and `Select`, where
                // it moves the cursor: under `TabsActivation::Automatic` the
                // cursor is the selection, so hovering would switch the
                // panel's content on the way past. A tabs row with nothing
                // bound lets the motion through, as its siblings do.
                MouseKind::Moved => {
                    if self.keyboard_enabled() && self.tab_at(mouse.column, mouse.row).is_some() {
                        EventResult::Consumed
                    } else {
                        EventResult::Ignored
                    }
                }
                // A click commits the clicked tab. Down is left for the runtime to
                // focus the row itself (the mouse counterpart of Tab).
                MouseKind::Click(MouseButton::Left) => {
                    if let Some(index) = self.tab_at(mouse.column, mouse.row) {
                        return if self.disabled_at(index) {
                            EventResult::Ignored
                        } else {
                            self.select(index)
                        };
                    }
                    let Some(direction) = self.marker_at(mouse.column, mouse.row) else {
                        return EventResult::Ignored;
                    };
                    let edge = match direction {
                        Step::Backward => self.hits.tabs.first(),
                        Step::Forward => self.hits.tabs.last(),
                    };
                    let Some((index, _)) = edge else {
                        return EventResult::Ignored;
                    };
                    let target =
                        linear_nav::step_enabled(self.items.len(), *index, direction, |index| {
                            self.disabled_at(index)
                        });
                    match self.activation {
                        TabsActivation::Manual => self.move_focus(target),
                        TabsActivation::Automatic => self.select(target),
                    }
                }
                _ => EventResult::Ignored,
            },
            Event::Key(key) => self.handle_key(*key, state),
            _ => EventResult::Ignored,
        }
    }

    fn interaction_area(&self, area: Rect) -> Rect {
        if area.width == 0 || area.height < self.height() {
            Rect::default()
        } else {
            Rect {
                height: self.height(),
                ..area
            }
        }
    }

    fn is_focusable(&self, _state: &S) -> bool {
        !self.disabled
            && self.keyboard_enabled()
            && linear_nav::first_enabled(self.items.len(), |index| self.disabled_at(index))
                .is_some()
    }
}

impl<T: Clone + PartialEq, S, M> MeasuredComponent<S, M> for Tabs<T, S, M> {
    fn measure(&self) -> Size {
        Size::new(self.width(), self.height())
    }
}

/// The width one tab occupies: its label plus the padding on each side (label +
/// 4, a button's width). Label width is counted in cells, matching the other
/// components.
fn tab_width(label: &str) -> u16 {
    shape_width(label)
}

#[derive(Default)]
struct TabLayout {
    /// Each visible tab as `(label index, rect)`. The index is the tab's original
    /// position. Hidden tabs mean this index may differ from the tab's screen slot.
    tabs: Vec<(usize, Rect)>,
    left: Option<Rect>,
    right: Option<Rect>,
}

#[derive(Debug, Clone, Copy)]
struct TabWindow {
    lo: usize,
    hi: usize,
    width: u16,
}

/// The width a run of tabs `lo..=hi` occupies: their widths plus the gaps
/// between them.
fn window_width(widths: &[u16], lo: usize, hi: usize) -> u16 {
    let mut width: u16 = 0;
    for (offset, tab_width) in widths[lo..=hi].iter().enumerate() {
        if offset > 0 {
            width = width.saturating_add(SPACING);
        }
        width = width.saturating_add(*tab_width);
    }
    width
}

/// Choose the first and last visible tab indexes. The chosen group always
/// includes `must_show` (the selected/focused tab) and then adds neighboring
/// tabs while they fit, leaving room for `‹` or `›` markers when tabs are hidden.
fn tab_window(widths: &[u16], must_show: usize, avail: u16) -> TabWindow {
    let n = widths.len();
    let fits = |lo: usize, hi: usize| {
        let mut width = window_width(widths, lo, hi);
        if lo > 0 {
            width = width.saturating_add(SPACING + MARKER_WIDTH);
        }
        if hi < n - 1 {
            width = width.saturating_add(SPACING + MARKER_WIDTH);
        }
        width <= avail
    };

    let (mut lo, mut hi) = (must_show, must_show);
    let mut prefer_right = true;
    loop {
        let grow_right = hi + 1 < n && fits(lo, hi + 1);
        let grow_left = lo > 0 && fits(lo - 1, hi);
        // Alternate sides so the visible tabs stay roughly centered on must_show;
        // fall through to the other side when the preferred one is exhausted.
        if prefer_right && grow_right {
            hi += 1;
        } else if !prefer_right && grow_left {
            lo -= 1;
        } else if grow_right {
            hi += 1;
        } else if grow_left {
            lo -= 1;
        } else {
            break;
        }
        prefer_right = !prefer_right;
    }
    TabWindow {
        lo,
        hi,
        width: window_width(widths, lo, hi),
    }
}

/// Lay out the visible tabs left to right, with a [`SPACING`] gap between them.
/// Add a `‹` or `›` marker on any side with hidden tabs. If the row is too
/// narrow for the selected/focused tab and its markers, the tab wins and the
/// markers are dropped; if that tab is wider than the row, it clips.
///
/// Called only from [`tab_layout`], after it has checked that there is at least
/// one tab, the row is tall enough, and the indexes are in range.
fn place_tab_window(
    area: Rect,
    widths: &[u16],
    size: TabsSize,
    must_show: usize,
    window: TabWindow,
) -> TabLayout {
    let height = size.height();
    let n = widths.len();

    let mut left_marker = window.lo > 0;
    let mut right_marker = window.hi < n - 1;
    let mut needed = window.width;
    if left_marker {
        needed = needed.saturating_add(SPACING + MARKER_WIDTH);
    }
    if right_marker {
        needed = needed.saturating_add(SPACING + MARKER_WIDTH);
    }
    // The selected/focused tab takes priority over the markers when the row is
    // too narrow for both. Showing that tab is more useful than only a marker.
    if needed > area.width {
        left_marker = false;
        right_marker = false;
    }

    let end = area.x.saturating_add(area.width);
    let mut x = area.x;

    let left = left_marker.then(|| {
        let rect = Rect::new(x, area.y, MARKER_WIDTH, height);
        x = x.saturating_add(MARKER_WIDTH + SPACING);
        rect
    });

    let mut tabs = Vec::with_capacity(window.hi - window.lo + 1);
    for (index, &tab_width) in (window.lo..=window.hi).zip(&widths[window.lo..=window.hi]) {
        if x >= end {
            break;
        }
        let available = end - x;
        // Only the selected/focused tab may clip; another tab that no longer fits
        // ends the row.
        let width = if index == must_show {
            tab_width.min(available)
        } else if tab_width <= available {
            tab_width
        } else {
            break;
        };
        tabs.push((index, Rect::new(x, area.y, width, height)));
        x = x.saturating_add(width).saturating_add(SPACING);
    }

    let right = (right_marker && x.saturating_add(MARKER_WIDTH) <= end)
        .then(|| Rect::new(x, area.y, MARKER_WIDTH, height));

    TabLayout { tabs, left, right }
}

/// Measure the tabs, choose which indexes are visible around `must_show`, and
/// lay them out. The resulting tab rects are the single source of tab geometry,
/// shared by the widget's paint and the component's hit-test.
fn tab_layout(area: Rect, labels: &[&str], size: TabsSize, must_show: usize) -> TabLayout {
    let height = size.height();
    if labels.is_empty() || area.width == 0 || area.height < height {
        return TabLayout::default();
    }
    let widths: Vec<u16> = labels.iter().map(|label| tab_width(label)).collect();
    let must_show = must_show.min(widths.len() - 1);
    let window = tab_window(&widths, must_show, area.width);
    place_tab_window(area, &widths, size, must_show, window)
}

/// The visible tab rects (dropping the label indices), starting at the first
/// tab. Test-only convenience over [`tab_layout`].
#[cfg(test)]
fn tab_rects(area: Rect, labels: &[&str], size: TabsSize) -> Vec<Rect> {
    tab_layout(area, labels, size, 0)
        .tabs
        .into_iter()
        .map(|(_, rect)| rect)
        .collect()
}

#[cfg(test)]
mod tests {
    use std::panic::{AssertUnwindSafe, catch_unwind};

    use ratatui::{Terminal, backend::TestBackend};

    use super::*;
    use crate::runtime::{ChildId, Modifiers, MouseEvent, Ratcn};
    use crate::text_width::display_width;

    #[derive(Debug, Default, Clone, Copy, PartialEq, Eq)]
    enum Screen {
        #[default]
        A,
        B,
        C,
        D,
        E,
        F,
    }

    #[derive(Debug, PartialEq, Eq)]
    enum Msg {
        Focused(Screen),
        Selected(Screen),
    }

    #[derive(Default)]
    struct State {
        focused: Option<Screen>,
        selected: Screen,
        disable_b: bool,
    }

    /// Automatic activation: arrow keys select immediately. Middle tab disabled.
    fn automatic() -> Tabs<Screen, State, Msg> {
        Tabs::new([
            Tab::new(Screen::A, "A"),
            Tab::new(Screen::B, "B").disabled(true),
            Tab::new(Screen::C, "C"),
        ])
        .selection(|s: &State| Some(s.selected), Msg::Selected)
        .activation(TabsActivation::Automatic)
    }

    fn automatic_with_tab_focus() -> Tabs<Screen, State, Msg> {
        automatic().tab_focus(
            |state: &State| Some(state.focused.unwrap_or(state.selected)),
            Msg::Focused,
        )
    }

    /// Manual wiring: focused tab and selection are separate, as on `List`.
    fn manual() -> Tabs<Screen, State, Msg> {
        Tabs::new([
            Tab::new(Screen::A, "A"),
            Tab::new(Screen::B, "B").disabled(true),
            Tab::new(Screen::C, "C"),
        ])
        .tab_focus(
            |s: &State| Some(s.focused.unwrap_or(s.selected)),
            Msg::Focused,
        )
        .selection(|s: &State| Some(s.selected), Msg::Selected)
    }

    fn key(code: KeyCode) -> Event {
        Event::Key(KeyEvent::new(code))
    }

    fn ctrl_key(ch: char) -> Event {
        Event::Key(KeyEvent {
            code: KeyCode::Char(ch),
            modifiers: Modifiers {
                ctrl: true,
                ..Modifiers::NONE
            },
        })
    }

    fn mouse(kind: MouseKind, column: u16, row: u16) -> Event {
        Event::Mouse(MouseEvent {
            kind,
            column,
            row,
            modifiers: Modifiers::NONE,
        })
    }

    fn render_runtime(
        ratcn: &mut Ratcn<State, Msg>,
        terminal: &mut Terminal<TestBackend>,
        state: &State,
        fail: bool,
    ) {
        let theme = Theme::default_dark();
        terminal
            .draw(|frame| {
                let area = frame.area();
                ratcn.render(frame, state, &theme, |ctx| {
                    ctx.render_component(
                        ChildId::Static("tabs"),
                        Tabs::new([
                            Tab::new(Screen::A, "A"),
                            Tab::new(Screen::B, "B").disabled(state.disable_b),
                            Tab::new(Screen::C, "C"),
                        ])
                        .tab_focus(
                            |state: &State| Some(state.focused.unwrap_or(state.selected)),
                            Msg::Focused,
                        )
                        .selection(|state: &State| Some(state.selected), Msg::Selected),
                        area,
                    );
                    assert!(!fail, "failed pass");
                });
            })
            .expect("draw");
    }

    #[test]
    fn automatic_right_selects_the_next_enabled_tab_directly() {
        let mut tabs = automatic();
        let state = State::default(); // selected A
        assert_eq!(
            tabs.handle_event(&key(KeyCode::Right), &state, &mut EventCtx::default(),),
            EventResult::Emit(Msg::Selected(Screen::C))
        );
    }

    #[test]
    fn automatic_keyboard_ignores_a_stale_manual_tab_focus() {
        let mut tabs = automatic_with_tab_focus();
        let state = State {
            focused: Some(Screen::C),
            selected: Screen::A,
            ..State::default()
        };

        assert_eq!(
            tabs.handle_event(&key(KeyCode::Enter), &state, &mut EventCtx::default()),
            EventResult::Emit(Msg::Selected(Screen::A))
        );
        assert_eq!(
            tabs.handle_event(&key(KeyCode::Char(' ')), &state, &mut EventCtx::default()),
            EventResult::Emit(Msg::Selected(Screen::A))
        );
        assert_eq!(
            tabs.handle_event(&key(KeyCode::Right), &state, &mut EventCtx::default()),
            EventResult::Emit(Msg::Selected(Screen::C))
        );
    }

    #[test]
    fn manual_without_focus_binding_is_not_focusable_and_ignores_keys() {
        let mut tabs = Tabs::new([
            Tab::new(Screen::A, "A"),
            Tab::new(Screen::B, "B").disabled(true),
            Tab::new(Screen::C, "C"),
        ])
        .selection(|s: &State| Some(s.selected), Msg::Selected);
        let state = State::default();
        assert!(!tabs.is_focusable(&state));
        assert_eq!(
            tabs.handle_event(&key(KeyCode::Right), &state, &mut EventCtx::default(),),
            EventResult::Ignored
        );
    }

    #[test]
    fn manual_without_selection_binding_is_not_focusable_and_ignores_keys() {
        let mut tabs = Tabs::new([Tab::new(Screen::A, "A"), Tab::new(Screen::B, "B")])
            .tab_focus(|_: &State| Some(Screen::A), Msg::Focused);
        let state = State::default();

        assert!(!tabs.is_focusable(&state));
        assert_eq!(
            tabs.handle_event(&key(KeyCode::Right), &state, &mut EventCtx::default()),
            EventResult::Ignored
        );
    }

    #[test]
    fn automatic_without_selection_is_not_focusable_and_ignores_keys() {
        let mut tabs: Tabs<Screen, State, Msg> =
            Tabs::new([Tab::new(Screen::A, "A"), Tab::new(Screen::B, "B")])
                .activation(TabsActivation::Automatic);
        let state = State::default();

        assert!(!tabs.is_focusable(&state));
        assert_eq!(
            tabs.handle_event(&key(KeyCode::Right), &state, &mut EventCtx::default()),
            EventResult::Ignored
        );
    }

    #[test]
    fn manual_right_moves_focus_not_the_selection() {
        let mut tabs = manual();
        let state = State::default(); // focused A, selected A
        assert_eq!(
            tabs.handle_event(&key(KeyCode::Right), &state, &mut EventCtx::default(),),
            EventResult::Emit(Msg::Focused(Screen::C))
        );
    }

    #[test]
    fn stale_manual_focus_recovers_to_first_enabled_tab_in_either_direction() {
        let state = State {
            focused: Some(Screen::B),
            ..State::default()
        };
        for code in [KeyCode::Left, KeyCode::Right] {
            let mut tabs = Tabs::new([
                Tab::new(Screen::C, "C").disabled(true),
                Tab::new(Screen::A, "A"),
                Tab::new(Screen::D, "D"),
            ])
            .tab_focus(
                |state: &State| Some(state.focused.unwrap_or(state.selected)),
                Msg::Focused,
            )
            .selection(|state: &State| Some(state.selected), Msg::Selected);

            assert_eq!(
                tabs.handle_event(&key(code), &state, &mut EventCtx::default()),
                EventResult::Emit(Msg::Focused(Screen::A))
            );
        }
    }

    #[test]
    fn stale_automatic_selection_recovers_to_first_enabled_tab_in_either_direction() {
        let state = State {
            selected: Screen::B,
            ..State::default()
        };
        for code in [KeyCode::Left, KeyCode::Right] {
            let mut tabs = Tabs::new([
                Tab::new(Screen::C, "C").disabled(true),
                Tab::new(Screen::A, "A"),
                Tab::new(Screen::D, "D"),
            ])
            .selection(|state: &State| Some(state.selected), Msg::Selected)
            .activation(TabsActivation::Automatic);

            assert_eq!(
                tabs.handle_event(&key(code), &state, &mut EventCtx::default()),
                EventResult::Emit(Msg::Selected(Screen::A))
            );
        }
    }

    #[test]
    fn enter_commits_the_focused_tab() {
        let mut tabs = manual();
        let state = State {
            focused: Some(Screen::C),
            selected: Screen::A,
            ..State::default()
        };
        assert_eq!(
            tabs.handle_event(&key(KeyCode::Enter), &state, &mut EventCtx::default(),),
            EventResult::Emit(Msg::Selected(Screen::C))
        );
    }

    #[test]
    fn enter_on_a_disabled_focused_tab_is_ignored() {
        let mut tabs = manual();
        let state = State {
            focused: Some(Screen::B),
            selected: Screen::A,
            ..State::default()
        };

        assert_eq!(
            tabs.handle_event(&key(KeyCode::Enter), &state, &mut EventCtx::default(),),
            EventResult::Ignored
        );
    }

    #[test]
    fn hover_is_consumed_without_moving_focus() {
        let mut tabs = manual();
        tabs.hits = tab_layout(
            Rect::new(0, 0, 30, TabsSize::Small.height()),
            &["A", "B", "C"],
            TabsSize::Small,
            0,
        );
        let state = State::default();
        // Tabs are label+4 wide with a 1-cell gap: A 0..5, B 6..11, C 12..17.
        assert_eq!(
            tabs.handle_event(
                &mouse(MouseKind::Moved, 14, 0),
                &state,
                &mut EventCtx::default(),
            ),
            EventResult::Consumed
        );
    }

    #[test]
    fn automatic_hover_ignores_a_manual_tab_focus_binding() {
        let mut tabs = automatic_with_tab_focus();
        tabs.hits = tab_layout(
            Rect::new(0, 0, 30, TabsSize::Small.height()),
            &["A", "B", "C"],
            TabsSize::Small,
            0,
        );
        let state = State::default();
        // Hover must never move a separate cursor or switch content.
        assert_eq!(
            tabs.handle_event(
                &mouse(MouseKind::Moved, 14, 0),
                &state,
                &mut EventCtx::default(),
            ),
            EventResult::Consumed
        );
    }

    /// A tab strip is horizontal, so its `vi` keys are `h`/`l` rather than
    /// `j`/`k`. Ctrl+P/Ctrl+N read as previous/next either way.
    #[test]
    fn vim_and_readline_keys_step_along_the_strip() {
        let state = State::default(); // cursor on A
        for forward in [key(KeyCode::Char('l')), ctrl_key('n')] {
            assert_eq!(
                manual().handle_event(&forward, &state, &mut EventCtx::default()),
                EventResult::Emit(Msg::Focused(Screen::C)),
                "l and Ctrl+N step like Right, skipping the disabled B"
            );
            assert_eq!(
                automatic().handle_event(&forward, &state, &mut EventCtx::default()),
                EventResult::Emit(Msg::Selected(Screen::C)),
                "automatic activation commits, as its arrow keys do"
            );
        }
        assert_eq!(
            manual().handle_event(&key(KeyCode::Char('h')), &state, &mut EventCtx::default()),
            EventResult::Consumed,
            "h steps like Left, and the cursor is already at the first tab"
        );
        assert_eq!(
            manual().handle_event(&key(KeyCode::Char('c')), &state, &mut EventCtx::default()),
            EventResult::Ignored,
            "any other letter bubbles: there is no typeahead"
        );
    }

    #[test]
    fn typeahead_skips_disabled_tabs_and_bubbles_when_nothing_matches() {
        let state = State::default();
        for ch in ['b', 'z'] {
            assert_eq!(
                manual().handle_event(&key(KeyCode::Char(ch)), &state, &mut EventCtx::default()),
                EventResult::Ignored,
                "'{ch}' matches no enabled tab, so it bubbles as an app hotkey"
            );
        }
    }

    /// A tabs row with nothing bound lets pointer motion through, as `List`
    /// and `Select` do.
    #[test]
    fn hover_over_an_unbound_tabs_row_is_ignored() {
        let mut tabs: Tabs<Screen, State, Msg> = Tabs::new([
            Tab::new(Screen::A, "A"),
            Tab::new(Screen::B, "B"),
            Tab::new(Screen::C, "C"),
        ]);
        tabs.hits = tab_layout(
            Rect::new(0, 0, 30, TabsSize::Small.height()),
            &["A", "B", "C"],
            TabsSize::Small,
            0,
        );
        let state = State::default();
        assert_eq!(
            tabs.handle_event(
                &mouse(MouseKind::Moved, 14, 0),
                &state,
                &mut EventCtx::default(),
            ),
            EventResult::Ignored
        );
    }

    #[test]
    fn click_commits_the_clicked_tab() {
        let mut tabs = automatic();
        tabs.hits = tab_layout(
            Rect::new(0, 0, 30, TabsSize::Small.height()),
            &["A", "B", "C"],
            TabsSize::Small,
            0,
        );
        let state = State::default();
        // C occupies columns 12..17.
        assert_eq!(
            tabs.handle_event(
                &mouse(MouseKind::Click(MouseButton::Left), 14, 0),
                &state,
                &mut EventCtx::default(),
            ),
            EventResult::Emit(Msg::Selected(Screen::C))
        );
    }

    #[test]
    fn right_and_middle_click_do_not_select_a_tab() {
        let mut tabs = automatic();
        tabs.hits = TabLayout {
            tabs: vec![(2, Rect::new(12, 0, 5, 1))],
            ..TabLayout::default()
        };

        for button in [MouseButton::Right, MouseButton::Middle] {
            assert_eq!(
                tabs.handle_event(
                    &mouse(MouseKind::Click(button), 14, 0),
                    &State::default(),
                    &mut EventCtx::default(),
                ),
                EventResult::Ignored
            );
        }
    }

    #[test]
    fn click_after_window_shift_selects_the_original_tab_index() {
        // Six tabs in 18 cells cannot all show, so the window shifts to keep
        // the selected E visible. The click must still resolve to F's *original*
        // index rather than its position within the visible window.
        let labels = ["A", "B", "C", "D", "E", "F"];
        let state = State {
            selected: Screen::E,
            ..State::default()
        };
        let theme = Theme::default_dark();
        let mut ratcn = Ratcn::new();
        let area = Rect::new(0, 0, 18, TabsSize::Small.height());
        let mut terminal =
            Terminal::new(TestBackend::new(area.width, area.height)).expect("terminal");

        terminal
            .draw(|frame| {
                ratcn.render(frame, &state, &theme, |ctx| {
                    ctx.render_component(
                        ChildId::Static("tabs"),
                        Tabs::new([
                            Tab::new(Screen::A, "A"),
                            Tab::new(Screen::B, "B"),
                            Tab::new(Screen::C, "C"),
                            Tab::new(Screen::D, "D"),
                            Tab::new(Screen::E, "E"),
                            Tab::new(Screen::F, "F"),
                        ])
                        .selection(|s: &State| Some(s.selected), Msg::Selected),
                        area,
                    );
                });
            })
            .expect("draw");

        // The same layout the render just computed: `must_show` is the selected
        // tab when no manual cursor is bound.
        let hits = tab_layout(area, &labels, TabsSize::Small, 4);
        let (_, tab_f) = hits
            .tabs
            .iter()
            .find(|(index, _)| *index == 5)
            .expect("tab F should be visible after the window shifts");

        assert_eq!(
            ratcn.handle_event(
                mouse(
                    MouseKind::Click(MouseButton::Left),
                    tab_f.x + tab_f.width / 2,
                    tab_f.y,
                ),
                &state,
            ),
            EventResult::Emit(Msg::Selected(Screen::F))
        );
    }

    #[test]
    fn clicking_the_right_marker_steps_toward_the_hidden_tabs() {
        // Six 5-wide tabs in 18 cells around tab A: A and B are visible and the
        // rest hide behind a `›` marker. Clicking it steps the manual cursor
        // one tab past the last visible one, toward the hidden side.
        let mut tabs = Tabs::new([
            Tab::new(Screen::A, "A"),
            Tab::new(Screen::B, "B"),
            Tab::new(Screen::C, "C"),
            Tab::new(Screen::D, "D"),
            Tab::new(Screen::E, "E"),
            Tab::new(Screen::F, "F"),
        ])
        .tab_focus(
            |state: &State| Some(state.focused.unwrap_or(state.selected)),
            Msg::Focused,
        )
        .selection(|state: &State| Some(state.selected), Msg::Selected);
        tabs.hits = tab_layout(
            Rect::new(0, 0, 18, TabsSize::Small.height()),
            &["A", "B", "C", "D", "E", "F"],
            TabsSize::Small,
            0,
        );
        let marker = tabs.hits.right.expect("hidden right tabs get a marker");

        assert_eq!(
            tabs.handle_event(
                &mouse(MouseKind::Click(MouseButton::Left), marker.x, marker.y),
                &State::default(),
                &mut EventCtx::default(),
            ),
            EventResult::Emit(Msg::Focused(Screen::C)),
            "the marker steps focus one tab past the last visible one"
        );
    }

    #[test]
    fn marker_clicks_route_through_the_retained_runtime_surface() {
        // The same marker click, but delivered through `Ratcn::handle_event`
        // against the geometry retained by a real render — automatic
        // activation, so the step selects the hidden neighbor directly.
        let mut ratcn = Ratcn::new();
        let mut terminal =
            Terminal::new(TestBackend::new(18, TabsSize::Small.height())).expect("terminal");
        let theme = Theme::default_dark();
        let state = State::default(); // selected A
        terminal
            .draw(|frame| {
                let area = frame.area();
                ratcn.render(frame, &state, &theme, |ctx| {
                    ctx.render_component(
                        ChildId::Static("tabs"),
                        Tabs::new([
                            Tab::new(Screen::A, "A"),
                            Tab::new(Screen::B, "B"),
                            Tab::new(Screen::C, "C"),
                            Tab::new(Screen::D, "D"),
                            Tab::new(Screen::E, "E"),
                            Tab::new(Screen::F, "F"),
                        ])
                        .selection(|state: &State| Some(state.selected), Msg::Selected)
                        .activation(TabsActivation::Automatic),
                        area,
                    );
                });
            })
            .expect("draw");
        let marker = tab_layout(
            Rect::new(0, 0, 18, TabsSize::Small.height()),
            &["A", "B", "C", "D", "E", "F"],
            TabsSize::Small,
            0,
        )
        .right
        .expect("hidden right tabs get a marker");

        assert_eq!(
            ratcn.handle_event(
                mouse(MouseKind::Click(MouseButton::Left), marker.x, marker.y),
                &state,
            ),
            EventResult::Emit(Msg::Selected(Screen::C))
        );
    }

    #[test]
    fn click_on_a_disabled_tab_is_ignored() {
        let mut tabs = automatic();
        tabs.hits = tab_layout(
            Rect::new(0, 0, 30, TabsSize::Small.height()),
            &["A", "B", "C"],
            TabsSize::Small,
            0,
        );
        let state = State::default();
        // Column 8 is inside the disabled middle tab (x = 6..11).
        assert_eq!(
            tabs.handle_event(
                &mouse(MouseKind::Click(MouseButton::Left), 8, 0),
                &state,
                &mut EventCtx::default(),
            ),
            EventResult::Ignored
        );
    }

    #[test]
    fn disabled_tabs_consume_pointer_down_without_focusing_the_row() {
        #[derive(Default)]
        struct RoutedState {
            focus: crate::runtime::FocusState,
            selected: Screen,
        }

        #[derive(Debug, PartialEq)]
        enum RoutedMsg {
            Focus(crate::runtime::FocusState),
            Selected(Screen),
        }

        let theme = Theme::default_dark();
        let mut state = RoutedState {
            focus: crate::runtime::FocusState::intent([ChildId::Static("other")]),
            ..RoutedState::default()
        };
        let mut ratcn = Ratcn::new().focus(|state: &RoutedState| &state.focus, RoutedMsg::Focus);
        let mut terminal = Terminal::new(TestBackend::new(20, 2)).expect("terminal");
        terminal
            .draw(|frame| {
                ratcn.render(frame, &state, &theme, |ctx| {
                    ctx.render_component(
                        ChildId::Static("other"),
                        crate::Button::<RoutedMsg>::new("Other")
                            .on_press(|| RoutedMsg::Selected(Screen::A)),
                        Rect::new(0, 0, 20, 1),
                    );
                    ctx.render_component(
                        ChildId::Static("tabs"),
                        Tabs::new([
                            Tab::new(Screen::A, "A"),
                            Tab::new(Screen::B, "B").disabled(true),
                        ])
                        .selection(
                            |state: &RoutedState| Some(state.selected),
                            RoutedMsg::Selected,
                        )
                        .activation(TabsActivation::Automatic),
                        Rect::new(0, 1, 20, 1),
                    );
                });
            })
            .expect("draw");

        assert_eq!(
            ratcn.handle_event(mouse(MouseKind::Down(MouseButton::Left), 8, 1), &state),
            EventResult::Consumed
        );
        assert_eq!(
            state.focus,
            crate::runtime::FocusState::intent([ChildId::Static("other")])
        );
        assert_eq!(
            ratcn.handle_event(mouse(MouseKind::Up(MouseButton::Left), 8, 1), &state),
            EventResult::Ignored
        );

        let EventResult::Emit(RoutedMsg::Focus(focus)) =
            ratcn.handle_event(mouse(MouseKind::Down(MouseButton::Left), 2, 1), &state)
        else {
            panic!("an enabled tab should receive runtime focus");
        };
        state.focus = focus;
        assert_eq!(
            ratcn.handle_event(mouse(MouseKind::Up(MouseButton::Left), 2, 1), &state),
            EventResult::Emit(RoutedMsg::Selected(Screen::A))
        );
    }

    #[test]
    fn excess_allocated_rows_do_not_focus_or_activate_tabs() {
        #[derive(Default)]
        struct RoutedState {
            focus: crate::runtime::FocusState,
            selected: Screen,
        }

        #[derive(Debug, PartialEq)]
        enum RoutedMsg {
            Focus(crate::runtime::FocusState),
            Selected(Screen),
        }

        let theme = Theme::default_dark();
        let mut state = RoutedState {
            focus: crate::runtime::FocusState::intent([ChildId::Static("other")]),
            ..RoutedState::default()
        };
        let mut ratcn = Ratcn::new().focus(|state: &RoutedState| &state.focus, RoutedMsg::Focus);
        let mut terminal = Terminal::new(TestBackend::new(20, 4)).expect("terminal");
        terminal
            .draw(|frame| {
                ratcn.render(frame, &state, &theme, |ctx| {
                    ctx.render_component(
                        ChildId::Static("tabs"),
                        Tabs::new([Tab::new(Screen::A, "A"), Tab::new(Screen::B, "B")])
                            .selection(
                                |state: &RoutedState| Some(state.selected),
                                RoutedMsg::Selected,
                            )
                            .activation(TabsActivation::Automatic),
                        Rect::new(0, 0, 20, 4),
                    );
                    ctx.render_component(
                        ChildId::Static("other"),
                        crate::Button::<RoutedMsg>::new("Other")
                            .on_press(|| RoutedMsg::Selected(Screen::A)),
                        Rect::new(0, 3, 8, 1),
                    );
                });
            })
            .expect("draw");

        assert_eq!(
            ratcn.handle_event(mouse(MouseKind::Down(MouseButton::Left), 2, 2), &state),
            EventResult::Ignored
        );
        assert_eq!(
            ratcn.handle_event(mouse(MouseKind::Up(MouseButton::Left), 2, 2), &state),
            EventResult::Ignored
        );
        assert_eq!(
            state.focus,
            crate::runtime::FocusState::intent([ChildId::Static("other")])
        );

        let EventResult::Emit(RoutedMsg::Focus(focus)) =
            ratcn.handle_event(mouse(MouseKind::Down(MouseButton::Left), 2, 0), &state)
        else {
            panic!("the painted tabs row should focus");
        };
        state.focus = focus;
        assert_eq!(
            ratcn.handle_event(mouse(MouseKind::Up(MouseButton::Left), 2, 0), &state),
            EventResult::Emit(RoutedMsg::Selected(Screen::A))
        );
    }

    #[test]
    fn automatic_home_and_end_select_the_first_and_last_enabled_tab() {
        let mut tabs = automatic();
        let from_c = State {
            selected: Screen::C,
            ..State::default()
        };
        assert_eq!(
            tabs.handle_event(&key(KeyCode::Home), &from_c, &mut EventCtx::default(),),
            EventResult::Emit(Msg::Selected(Screen::A))
        );
        assert_eq!(
            tabs.handle_event(
                &key(KeyCode::End),
                &State::default(),
                &mut EventCtx::default(),
            ),
            EventResult::Emit(Msg::Selected(Screen::C))
        );
    }

    #[test]
    fn selection_is_stable_when_tabs_are_reordered() {
        // Value-keyed: the same stored value selects the same tab regardless of
        // position, so a click resolves by value, not index.
        let mut tabs = Tabs::new([
            Tab::new(Screen::C, "C"),
            Tab::new(Screen::A, "A"),
            Tab::new(Screen::B, "B"),
        ])
        .selection(|s: &State| Some(s.selected), Msg::Selected);
        let state = State::default(); // selected A, now at index 1
        assert_eq!(tabs.selected_index(&state), Some(1));
        tabs.hits = tab_layout(
            Rect::new(0, 0, 30, TabsSize::Small.height()),
            &["C", "A", "B"],
            TabsSize::Small,
            0,
        );
        // The first tab (C) occupies columns 0..5.
        assert_eq!(
            tabs.handle_event(
                &mouse(MouseKind::Click(MouseButton::Left), 1, 0),
                &state,
                &mut EventCtx::default(),
            ),
            EventResult::Emit(Msg::Selected(Screen::C))
        );
    }

    #[test]
    fn duplicate_tab_values_fail_declaration() {
        let mut tabs: Tabs<Screen, State, Msg> =
            Tabs::new([Tab::new(Screen::A, "First"), Tab::new(Screen::A, "Second")]);

        let failed = catch_unwind(AssertUnwindSafe(|| {
            Component::prepare(&mut tabs, &State::default());
        }));

        assert!(failed.is_err());
    }

    #[test]
    fn a_row_of_all_disabled_tabs_is_not_focusable() {
        let mut tabs: Tabs<Screen, State, Msg> = Tabs::new([
            Tab::new(Screen::A, "A").disabled(true),
            Tab::new(Screen::B, "B").disabled(true),
        ]);
        let state = State::default();

        assert!(!tabs.is_focusable(&state));
        assert_eq!(
            tabs.handle_event(&key(KeyCode::Right), &state, &mut EventCtx::default(),),
            EventResult::Ignored
        );
    }

    #[test]
    fn interaction_area_requires_full_height_and_crops_excess_rows() {
        let tabs: Tabs<Screen, State, Msg> =
            Tabs::new([Tab::new(Screen::A, "A")]).size(TabsSize::Large);

        assert_eq!(
            tabs.interaction_area(Rect::new(0, 0, 1, 2)),
            Rect::default()
        );
        assert_eq!(
            tabs.interaction_area(Rect::new(2, 3, 1, 5)),
            Rect::new(2, 3, 1, 3)
        );
        assert_eq!(
            tabs.interaction_area(Rect::new(0, 0, 0, 3)),
            Rect::default()
        );
    }

    #[test]
    fn disabledness_controls_navigation_clicks_and_focusability() {
        let mut tabs: Tabs<Screen, State, Msg> = Tabs::new([
            Tab::new(Screen::A, "A").disabled(true),
            Tab::new(Screen::B, "B").disabled(true),
            Tab::new(Screen::C, "C"),
        ])
        .tab_focus(
            |state: &State| Some(state.focused.unwrap_or(state.selected)),
            Msg::Focused,
        )
        .selection(|state: &State| Some(state.selected), Msg::Selected);
        tabs.hits = tab_layout(Rect::new(0, 0, 30, 1), &["A", "B", "C"], TabsSize::Small, 0);

        assert!(tabs.is_focusable(&State::default()));
        assert_eq!(
            tabs.handle_event(
                &key(KeyCode::Right),
                &State::default(),
                &mut EventCtx::default(),
            ),
            EventResult::Emit(Msg::Focused(Screen::C))
        );
        assert_eq!(
            tabs.handle_event(
                &mouse(MouseKind::Click(MouseButton::Left), 2, 0),
                &State::default(),
                &mut EventCtx::default(),
            ),
            EventResult::Ignored
        );
        assert_eq!(
            tabs.handle_event(
                &mouse(MouseKind::Click(MouseButton::Left), 8, 0),
                &State::default(),
                &mut EventCtx::default(),
            ),
            EventResult::Ignored
        );
        assert_eq!(
            tabs.handle_event(
                &mouse(MouseKind::Click(MouseButton::Left), 14, 0),
                &State::default(),
                &mut EventCtx::default(),
            ),
            EventResult::Emit(Msg::Selected(Screen::C))
        );

        let all_disabled: Tabs<Screen, State, Msg> = Tabs::new([
            Tab::new(Screen::A, "A").disabled(true),
            Tab::new(Screen::B, "B").disabled(true),
            Tab::new(Screen::C, "C").disabled(true),
        ]);
        assert!(!all_disabled.is_focusable(&State::default()));
    }

    #[test]
    fn disabled_bits_remain_from_the_last_successful_runtime_render() {
        let mut ratcn = Ratcn::new();
        let mut terminal = Terminal::new(TestBackend::new(20, 1)).expect("terminal");
        let mut state = State::default();
        render_runtime(&mut ratcn, &mut terminal, &state, false);
        state.disable_b = true;

        assert_eq!(
            ratcn.handle_event(key(KeyCode::Right), &state),
            EventResult::Emit(Msg::Focused(Screen::B))
        );

        render_runtime(&mut ratcn, &mut terminal, &state, false);
        state.disable_b = false;
        assert_eq!(
            ratcn.handle_event(key(KeyCode::Right), &state),
            EventResult::Emit(Msg::Focused(Screen::C))
        );
    }

    #[test]
    fn failed_runtime_pass_preserves_previous_disabled_bits() {
        let mut ratcn = Ratcn::new();
        let mut terminal = Terminal::new(TestBackend::new(20, 1)).expect("terminal");
        let mut state = State::default();
        render_runtime(&mut ratcn, &mut terminal, &state, false);
        state.disable_b = true;

        let failed = catch_unwind(AssertUnwindSafe(|| {
            render_runtime(&mut ratcn, &mut terminal, &state, true);
        }));

        assert!(failed.is_err());
        assert_eq!(
            ratcn.handle_event(key(KeyCode::Right), &state),
            EventResult::Emit(Msg::Focused(Screen::B))
        );
    }

    #[test]
    fn focused_and_selected_bindings_are_current_between_renders() {
        let mut ratcn = Ratcn::new();
        let mut terminal = Terminal::new(TestBackend::new(20, 1)).expect("terminal");
        let mut state = State::default();
        render_runtime(&mut ratcn, &mut terminal, &state, false);
        state.focused = Some(Screen::B);
        state.selected = Screen::C;

        assert_eq!(
            ratcn.handle_event(key(KeyCode::Right), &state),
            EventResult::Emit(Msg::Focused(Screen::C))
        );
        assert_eq!(
            ratcn.handle_event(key(KeyCode::Enter), &state),
            EventResult::Emit(Msg::Selected(Screen::B))
        );
    }

    #[test]
    fn consecutive_automatic_navigation_uses_current_selection_without_redraw() {
        let mut ratcn = Ratcn::new();
        let mut terminal = Terminal::new(TestBackend::new(20, 1)).expect("terminal");
        let theme = Theme::default_dark();
        let mut state = State::default();
        terminal
            .draw(|frame| {
                let area = frame.area();
                ratcn.render(frame, &state, &theme, |ctx| {
                    ctx.render_component(
                        ChildId::Static("tabs"),
                        Tabs::new([
                            Tab::new(Screen::A, "A"),
                            Tab::new(Screen::B, "B"),
                            Tab::new(Screen::C, "C"),
                        ])
                        .selection(|state: &State| Some(state.selected), Msg::Selected)
                        .activation(TabsActivation::Automatic),
                        area,
                    );
                });
            })
            .expect("draw");

        assert_eq!(
            ratcn.handle_event(key(KeyCode::Right), &state),
            EventResult::Emit(Msg::Selected(Screen::B))
        );
        state.selected = Screen::B;
        assert_eq!(
            ratcn.handle_event(key(KeyCode::Right), &state),
            EventResult::Emit(Msg::Selected(Screen::C))
        );
    }

    #[test]
    fn hit_window_survives_semantic_changes_and_a_failed_pass() {
        let mut ratcn = Ratcn::new();
        let mut terminal = Terminal::new(TestBackend::new(18, 1)).expect("terminal");
        let theme = Theme::default_dark();
        let mut state = State {
            selected: Screen::E,
            ..State::default()
        };
        let render = |ratcn: &mut Ratcn<State, Msg>,
                      terminal: &mut Terminal<TestBackend>,
                      state: &State,
                      fail: bool| {
            terminal
                .draw(|frame| {
                    let area = frame.area();
                    ratcn.render(frame, state, &theme, |ctx| {
                        ctx.render_component(
                            ChildId::Static("tabs"),
                            Tabs::new([
                                Tab::new(Screen::A, "A"),
                                Tab::new(Screen::B, "B"),
                                Tab::new(Screen::C, "C"),
                                Tab::new(Screen::D, "D"),
                                Tab::new(Screen::E, "E"),
                                Tab::new(Screen::F, "F"),
                            ])
                            .selection(|state: &State| Some(state.selected), Msg::Selected),
                            area,
                        );
                        assert!(!fail, "failed pass");
                    });
                })
                .expect("draw");
        };
        render(&mut ratcn, &mut terminal, &state, false);
        let (_, tab_f) = tab_layout(
            Rect::new(0, 0, 18, 1),
            &["A", "B", "C", "D", "E", "F"],
            TabsSize::Small,
            4,
        )
        .tabs
        .into_iter()
        .find(|(index, _)| *index == 5)
        .expect("tab F is visible around selected E");

        state.selected = Screen::A;
        state.focused = Some(Screen::A);
        let failed = catch_unwind(AssertUnwindSafe(|| {
            render(&mut ratcn, &mut terminal, &state, true);
        }));

        assert!(failed.is_err());
        assert_eq!(
            ratcn.handle_event(
                mouse(
                    MouseKind::Click(MouseButton::Left),
                    tab_f.x + tab_f.width / 2,
                    tab_f.y,
                ),
                &state,
            ),
            EventResult::Emit(Msg::Selected(Screen::F))
        );
    }

    #[test]
    fn same_target_click_enter_and_space_all_select() {
        let state = State::default();
        let mut tabs = Tabs::new([Tab::new(Screen::A, "A")])
            .selection(|state: &State| Some(state.selected), Msg::Selected)
            .activation(TabsActivation::Automatic);
        tabs.hits = TabLayout {
            tabs: vec![(0, Rect::new(0, 0, 5, 1))],
            ..TabLayout::default()
        };

        assert_eq!(
            tabs.handle_event(
                &mouse(MouseKind::Click(MouseButton::Left), 2, 0),
                &state,
                &mut EventCtx::default(),
            ),
            EventResult::Emit(Msg::Selected(Screen::A))
        );
        assert_eq!(
            tabs.handle_event(&key(KeyCode::Enter), &state, &mut EventCtx::default(),),
            EventResult::Emit(Msg::Selected(Screen::A))
        );
        assert_eq!(
            tabs.handle_event(&key(KeyCode::Char(' ')), &state, &mut EventCtx::default(),),
            EventResult::Emit(Msg::Selected(Screen::A))
        );
    }

    #[test]
    fn automatic_render_uses_selection_instead_of_stale_manual_tab_focus() {
        let theme = Theme::default_dark();
        let style = TabsStyle::from_theme(&theme);
        let state = State {
            focused: Some(Screen::C),
            selected: Screen::A,
            ..State::default()
        };
        // The tab strip is the only focus candidate, so startup focus resolves
        // onto it and it paints focused.
        let mut ratcn = Ratcn::new();
        let mut terminal = Terminal::new(TestBackend::new(18, 1)).expect("terminal");

        terminal
            .draw(|frame| {
                let area = frame.area();
                ratcn.render(frame, &state, &theme, |ctx| {
                    ctx.render_component(ChildId::Static("tabs"), automatic_with_tab_focus(), area);
                });
            })
            .expect("draw");

        let buffer = terminal.backend().buffer();
        assert_eq!(
            buffer.cell((2, 0)).expect("selected A label cell").bg,
            style.selected_focused_background
        );
        assert_eq!(
            buffer.cell((14, 0)).expect("unselected C label cell").bg,
            style.background
        );
    }

    #[test]
    fn selected_tab_fills_like_the_default_button_others_like_secondary() {
        let theme = Theme::default_dark();
        let style = TabsStyle::from_theme(&theme);
        let area = Rect::new(0, 0, 18, TabsSize::Small.height());
        let mut buffer = Buffer::empty(area);

        // Selected A, no focused tab. A 0..5, B 6..11 (1-cell gap).
        Widget::render(
            TabsWidget::new(&["A", "B"])
                .selected_tab(Some(0))
                .style(style),
            area,
            &mut buffer,
        );

        // The selected tab fills with the primary (default button).
        let selected = buffer.cell((2, 0)).expect("selected label cell");
        assert_eq!(selected.bg, style.selected_background);
        assert_eq!(selected.bg, theme.primary);
        // An unselected tab fills with the secondary.
        let unselected = buffer.cell((8, 0)).expect("unselected label cell");
        assert_eq!(unselected.bg, style.background);
        assert_eq!(unselected.bg, theme.secondary);
    }

    #[test]
    fn disabled_selected_tab_keeps_distinct_active_colors() {
        let theme = Theme::default_dark();
        let style = TabsStyle::from_theme(&theme);
        let area = Rect::new(0, 0, 18, TabsSize::Small.height());
        let mut buffer = Buffer::empty(area);

        Widget::render(
            TabsWidget::new(&["A", "B"])
                .selected_tab(Some(0))
                .disabled_tabs(&[true, true])
                .style(style),
            area,
            &mut buffer,
        );

        let selected = buffer.cell((2, 0)).expect("selected disabled tab");
        let unselected = buffer.cell((8, 0)).expect("unselected disabled tab");
        assert_eq!(selected.bg, style.selected_disabled_background);
        assert_eq!(unselected.bg, style.disabled_background);
        assert_ne!(selected.bg, unselected.bg);
    }

    #[test]
    fn focused_tab_takes_the_button_focus_shift() {
        let theme = Theme::default_dark();
        let style = TabsStyle::from_theme(&theme);
        let area = Rect::new(0, 0, 18, TabsSize::Small.height());
        let mut buffer = Buffer::empty(area);

        // Selected A, focused tab on the unselected B, row focused → B lightens like a
        // focused secondary button.
        Widget::render(
            TabsWidget::new(&["A", "B"])
                .selected_tab(Some(0))
                .focused_tab(Some(1))
                .focused(true)
                .style(style),
            area,
            &mut buffer,
        );

        let focused = buffer.cell((8, 0)).expect("focused label cell");
        assert_eq!(focused.bg, style.focused_background);
    }

    #[test]
    fn hovered_row_shows_the_hover_shift_not_the_focus_shift() {
        use crate::runtime::ScopeOptions;

        let theme = Theme::default_dark();
        let style = TabsStyle::from_theme(&theme);
        let state = State {
            focused: Some(Screen::C),
            selected: Screen::A,
            ..State::default()
        };
        let tabs_area = Rect::new(0, 0, 18, TabsSize::Small.height());
        let decoy_area = Rect::new(0, tabs_area.height, 18, 1);
        let mut ratcn = Ratcn::new();
        let mut terminal =
            Terminal::new(TestBackend::new(18, tabs_area.height + 1)).expect("terminal");

        // The decoy scope is declared first, so startup focus lands there and
        // the tab strip paints unfocused — which is what lets the hover shift
        // be told apart from the focus shift below.
        let draw = |ratcn: &mut Ratcn<State, Msg>, terminal: &mut Terminal<TestBackend>| {
            terminal
                .draw(|frame| {
                    ratcn.render(frame, &state, &theme, |ctx| {
                        ctx.scope(
                            ChildId::Static("decoy"),
                            decoy_area,
                            ScopeOptions::default().focusable(),
                            |_| {},
                        );
                        ctx.render_component(ChildId::Static("tabs"), manual(), tabs_area);
                    });
                })
                .expect("draw");
        };

        // Hover position comes from a real pointer event, and events need a
        // surface to route against — so this is render, move, render.
        draw(&mut ratcn, &mut terminal);
        ratcn.handle_event(mouse(MouseKind::Moved, 14, 0), &state);
        draw(&mut ratcn, &mut terminal);

        // C is the cursor tab. Pointing at the row gives it the hover colors, not
        // the focus ones — hover and focus are distinct so a style can express
        // both, and so pointing at an already-focused tab still changes something.
        let cursor = terminal
            .backend()
            .buffer()
            .cell((14, 0))
            .expect("cursor label cell");
        assert_eq!(cursor.bg, style.hovered_background);
        assert_ne!(
            style.hovered_background, style.focused_background,
            "hover must be distinguishable from focus"
        );
    }

    // The point of giving every state a foreground: a row whose fills all match
    // the surface still has to signal focus, hover, and selection.
    #[test]
    fn a_row_with_no_fill_still_distinguishes_every_state() {
        let surface = Color::Rgb(10, 10, 10);
        let style = TabsStyle {
            foreground: Color::Rgb(120, 120, 120),
            focused_foreground: Color::Rgb(200, 200, 200),
            hovered_foreground: Color::Rgb(255, 255, 255),
            selected_foreground: Color::Rgb(139, 92, 246),
            background: surface,
            focused_background: surface,
            hovered_background: surface,
            selected_background: surface,
            selected_focused_background: surface,
            selected_hovered_background: surface,
            ..TabsStyle::fallback()
        };

        let resting = style.resolve(false, false, false, false);
        let focused = style.resolve(false, true, false, false);
        let hovered = style.resolve(false, false, true, false);
        let selected = style.resolve(true, false, false, false);

        for resolved in [resting, focused, hovered, selected] {
            assert_eq!(resolved.fill, surface, "no state may paint its own fill");
        }
        assert_ne!(resting.foreground, focused.foreground);
        assert_ne!(focused.foreground, hovered.foreground);
        assert_ne!(resting.foreground, selected.foreground);
    }

    // Hover wins over focus, matching buttons, so pointing at the tab the
    // keyboard already sits on still changes something.
    #[test]
    fn hover_takes_precedence_over_focus() {
        let style = TabsStyle::from_theme(&Theme::default_dark());
        let both = style.resolve(false, true, true, false);
        assert_eq!(both.fill, style.hovered_background);
    }

    #[test]
    fn one_row_area_renders_tabs() {
        let area = Rect::new(0, 0, 20, TabsSize::Small.height());
        let mut buffer = Buffer::empty(area);

        Widget::render(
            TabsWidget::new(&["A", "B"]).selected_tab(Some(0)),
            area,
            &mut buffer,
        );

        assert_eq!(
            buffer.cell((2, 0)).expect("selected label cell").symbol(),
            "A"
        );
    }

    #[test]
    fn large_tabs_render_top_and_bottom_caps() {
        let area = Rect::new(0, 0, 20, TabsSize::Large.height());
        let mut buffer = Buffer::empty(area);

        Widget::render(
            TabsWidget::new(&["A"])
                .selected_tab(Some(0))
                .size(TabsSize::Large),
            area,
            &mut buffer,
        );

        assert_eq!(TabsWidget::new(&["A"]).size(TabsSize::Large).height(), 3);
        assert_eq!(buffer.cell((0, 0)).expect("top cap").symbol(), "â–„");
        assert_eq!(buffer.cell((2, 1)).expect("label").symbol(), "A");
        assert_eq!(buffer.cell((0, 2)).expect("bottom cap").symbol(), "â–€");
    }

    #[test]
    fn tab_hit_rects_match_the_painted_row() {
        let rects = tab_rects(Rect::new(0, 0, 20, 3), &["A", "B"], TabsSize::Small);

        assert_eq!(rects[0], Rect::new(0, 0, 5, TabsSize::Small.height()));
        assert!(rects[0].contains(Position { x: 2, y: 0 }));
        assert!(!rects[0].contains(Position { x: 2, y: 1 }));
    }

    #[test]
    fn tab_rects_omit_tabs_that_do_not_fit_fully() {
        // A is 5 cells wide; after one spacing cell, B also needs 5 cells.
        // Width 9 leaves room for A plus the overflow marker, but not B.
        let rects = tab_rects(Rect::new(0, 0, 9, 1), &["A", "B"], TabsSize::Small);

        assert_eq!(rects, vec![Rect::new(0, 0, 5, 1)]);
    }

    #[test]
    fn hidden_tabs_on_the_right_render_a_right_marker() {
        // Width 9 fits tab A (5) plus a gap and the marker, but not B.
        let area = Rect::new(0, 0, 9, TabsSize::Small.height());
        let mut buffer = Buffer::empty(area);

        Widget::render(
            TabsWidget::new(&["A", "B"]).selected_tab(Some(0)),
            area,
            &mut buffer,
        );

        assert_eq!(buffer.cell((2, 0)).expect("tab A").symbol(), "A");
        assert_eq!(buffer.cell((6, 0)).expect("right marker").symbol(), "›");
    }

    #[test]
    fn window_keeps_the_active_tab_visible_and_marks_the_hidden_side() {
        // Six 5-wide tabs cannot all fit in 18 cells. Selecting a late tab must
        // scroll it into view, with a left marker for the tabs now hidden.
        let labels = ["A", "B", "C", "D", "E", "F"];
        let area = Rect::new(0, 0, 18, TabsSize::Small.height());
        let mut buffer = Buffer::empty(area);

        Widget::render(
            TabsWidget::new(&labels).selected_tab(Some(4)),
            area,
            &mut buffer,
        );

        let row: String = (0..18)
            .map(|x| buffer.cell((x, 0)).expect("cell").symbol().to_string())
            .collect();
        assert!(
            row.contains('E'),
            "selected tab E must be on screen: {row:?}"
        );
        assert!(row.contains('‹'), "hidden left tabs get a marker: {row:?}");
        assert!(
            !row.contains('A'),
            "an off-screen tab is not painted: {row:?}"
        );
    }

    #[test]
    fn middle_window_marks_hidden_tabs_on_both_sides() {
        let labels = ["A", "B", "C", "D", "E", "F"];
        let area = Rect::new(0, 0, 18, TabsSize::Small.height());
        let mut buffer = Buffer::empty(area);

        Widget::render(
            TabsWidget::new(&labels).selected_tab(Some(3)),
            area,
            &mut buffer,
        );

        let row: String = (0..18)
            .map(|x| buffer.cell((x, 0)).expect("cell").symbol().to_string())
            .collect();
        assert!(row.contains('‹'), "left marker missing: {row:?}");
        assert!(row.contains('›'), "right marker missing: {row:?}");
        assert!(row.contains('D'), "active tab missing: {row:?}");
    }

    #[test]
    fn oversized_active_tab_clips_but_still_renders() {
        let area = Rect::new(0, 0, 4, TabsSize::Small.height());
        let mut buffer = Buffer::empty(area);

        Widget::render(
            TabsWidget::new(&["VeryWide"]).selected_tab(Some(0)),
            area,
            &mut buffer,
        );

        let row: String = (0..4)
            .map(|x| buffer.cell((x, 0)).expect("cell").symbol().to_string())
            .collect();
        assert_eq!(row, "Very");
    }

    #[test]
    fn a_row_too_narrow_for_markers_still_shows_the_active_tab() {
        // Width 6 holds exactly one tab and no room for a marker beside it: the
        // active tab wins over the "more exist" hint.
        let labels = ["AA", "BB", "CC"];
        let area = Rect::new(0, 0, 6, TabsSize::Small.height());
        let mut buffer = Buffer::empty(area);

        Widget::render(
            TabsWidget::new(&labels).selected_tab(Some(1)),
            area,
            &mut buffer,
        );

        let row: String = (0..6)
            .map(|x| buffer.cell((x, 0)).expect("cell").symbol().to_string())
            .collect();
        assert!(
            row.contains("BB"),
            "the active tab is shown, not a bare marker: {row:?}"
        );
    }

    #[test]
    fn tab_width_matches_a_button_and_counts_cells_not_chars_or_bytes() {
        // label + 4, the same width as a Button of the same text.
        assert_eq!(tab_width("A"), 5);
        assert_eq!(tab_width("åβ"), 6);
        assert_eq!(tab_width("日本"), 8, "CJK chars are 2 cells");
        assert_eq!(tab_width("🚀"), 6, "emoji are 2 cells");
        assert_eq!(tab_width("e\u{301}"), 5, "combining mark adds no cell");
        assert_eq!(
            tab_width("👩\u{200d}👩\u{200d}👦"),
            6,
            "a ZWJ sequence is one 2-cell glyph, not its char count"
        );
    }

    #[test]
    fn filled_middle_truncates_by_cells_and_pads_the_fill() {
        // Centered by cells when the label fits.
        assert_eq!(filled_middle("日本", 8), "  日本  ");
        // Truncation never splits a wide char's cells or overflows the width;
        // the shortfall is padded so the fill spans the whole tab.
        assert_eq!(filled_middle("日本語", 5), "日本 ");
        assert_eq!(filled_middle("日本語", 4), "日本");
        assert_eq!(filled_middle("日本語", 3), "日 ");
        assert_eq!(filled_middle("日本語", 0), "");
        for width in 0..=8 {
            assert_eq!(
                display_width(&filled_middle("日本語", width)),
                width.min(8),
                "middle row must span exactly the tab width"
            );
        }
    }

    #[test]
    fn wide_label_tab_paints_centered_with_the_fill_spanning_its_cells() {
        let theme = Theme::default_dark();
        let style = TabsStyle::from_theme(&theme);
        // The tab is label + 4 cells wide: "日本" is 4 cells, so the label
        // starts 2 cells in and the fill runs through cell 7.
        let area = Rect::new(0, 0, 12, TabsSize::Small.height());
        let mut buffer = Buffer::empty(area);

        Widget::render(
            TabsWidget::new(&["日本"])
                .selected_tab(Some(0))
                .style(style),
            area,
            &mut buffer,
        );

        assert_eq!(buffer.cell((2, 0)).expect("label start").symbol(), "æ—¥");
        assert_eq!(buffer.cell((4, 0)).expect("label middle").symbol(), "本");
        for x in [0, 1, 6, 7] {
            let pad = buffer.cell((x, 0)).expect("padding cell");
            assert_eq!(pad.symbol(), " ", "pad at {x}");
            assert_eq!(pad.bg, style.selected_background, "fill spans tab at {x}");
        }
        // Past the tab: untouched.
        assert_eq!(buffer.cell((8, 0)).expect("outside cell").bg, Color::Reset);
    }

    #[test]
    fn undersized_area_never_paints_outside_it() {
        let theme = Theme::default_dark();
        let style = TabsStyle::from_theme(&theme);
        // Buffer wider than the tabs' area; a wide label that cannot fit must
        // clip inside the area, not spill past it.
        let surround = Rect::new(0, 0, 12, TabsSize::Small.height());
        let area = Rect::new(2, 0, 5, TabsSize::Small.height());
        let mut buffer = Buffer::empty(surround);

        Widget::render(
            TabsWidget::new(&["日本語"])
                .selected_tab(Some(0))
                .style(style),
            area,
            &mut buffer,
        );

        for x in [0, 1, 7, 8, 9, 10, 11] {
            let outside = buffer.cell((x, 0)).expect("cell outside the area");
            assert_eq!(outside.symbol(), " ", "outside cell {x} written");
            assert_eq!(outside.bg, Color::Reset, "outside cell {x} styled");
        }

        // Zero-sized areas are a no-op, not a panic.
        let before = buffer.clone();
        Widget::render(
            TabsWidget::new(&["日本語"]).selected_tab(Some(0)),
            Rect::new(2, 0, 0, 1),
            &mut buffer,
        );
        Widget::render(
            TabsWidget::new(&["日本語"]).selected_tab(Some(0)),
            Rect::new(2, 0, 5, 0),
            &mut buffer,
        );
        assert_eq!(buffer, before);
    }

    #[test]
    fn tabs_widget_width_includes_spacing_between_tabs() {
        assert_eq!(TabsWidget::new(&["A", "B"]).width(), 11);
    }

    #[test]
    fn window_width_sums_tab_widths_and_the_gaps_between_them() {
        // Three 5-wide tabs: 5 + 1 + 5 + 1 + 5.
        assert_eq!(window_width(&[5, 5, 5], 0, 2), 17);
        // A single tab has no gap.
        assert_eq!(window_width(&[5, 9, 5], 1, 1), 9);
        // A sub-range: two tabs and one gap.
        assert_eq!(window_width(&[5, 5, 5, 5], 1, 2), 11);
    }

    #[test]
    fn tab_window_shows_everything_when_it_all_fits() {
        let window = tab_window(&[5, 5, 5, 5, 5, 5], 0, 100);
        assert_eq!((window.lo, window.hi), (0, 5));
        assert_eq!(window.width, 35); // 6 * 5 + 5 gaps
    }

    #[test]
    fn tab_window_keeps_a_late_active_tab_visible() {
        // Six 5-wide tabs, 18 cells. The visible group must include the active
        // tab, drop the earlier tabs (reserving a left marker), and stay within
        // budget.
        let window = tab_window(&[5, 5, 5, 5, 5, 5], 4, 18);
        assert!(
            window.lo <= 4 && 4 <= window.hi,
            "must include the active tab"
        );
        assert!(window.lo > 0, "earlier tabs are hidden");
        assert!(window.width <= 18);
    }

    #[test]
    fn tab_window_is_just_the_active_tab_when_nothing_else_fits() {
        // Room for one 6-wide tab only.
        let window = tab_window(&[6, 6, 6], 1, 6);
        assert_eq!((window.lo, window.hi), (1, 1));
        assert_eq!(window.width, 6);
    }

    #[test]
    fn string_sugar_keys_tabs_by_their_labels() {
        let tabs = Tabs::<String, State, Msg>::new(["One", "Two"]);
        assert_eq!(tabs.items[0].value(), "One");
        assert_eq!(tabs.items[0].label(), "One");
        assert!(!tabs.items[0].is_disabled());
    }

    #[test]
    fn duplicate_tab_values_panic_with_the_shared_message() {
        let mut tabs: Tabs<Screen, State, Msg> =
            Tabs::new([Tab::new(Screen::A, "First"), Tab::new(Screen::A, "Second")]);

        let panic = catch_unwind(AssertUnwindSafe(|| {
            Component::prepare(&mut tabs, &State::default());
        }))
        .expect_err("duplicate tab values must panic");
        let message = panic
            .downcast_ref::<String>()
            .expect("panic carries a String");
        assert_eq!(
            message,
            "Tabs item values must be unique within a Tabs declaration"
        );
    }

    // Whole-control disabled mirrors List: not focusable, events ignored, and
    // the row painted in the style's disabled colors.
    #[test]
    fn disabled_row_is_not_focusable_and_ignores_events() {
        let mut tabs = manual().disabled(true);
        tabs.hits = tab_layout(
            Rect::new(0, 0, 30, TabsSize::Small.height()),
            &["A", "B", "C"],
            TabsSize::Small,
            0,
        );
        let state = State::default();

        assert!(!tabs.is_focusable(&state));
        assert_eq!(
            tabs.handle_event(&key(KeyCode::Right), &state, &mut EventCtx::default()),
            EventResult::Ignored
        );
        assert_eq!(
            tabs.handle_event(&key(KeyCode::Enter), &state, &mut EventCtx::default()),
            EventResult::Ignored
        );
        assert_eq!(
            tabs.handle_event(
                &mouse(MouseKind::Click(MouseButton::Left), 2, 0),
                &state,
                &mut EventCtx::default()
            ),
            EventResult::Ignored
        );
    }

    #[test]
    fn disabled_row_paints_every_tab_in_disabled_colors() {
        let theme = Theme::default_dark();
        let style = TabsStyle::from_theme(&theme);
        let area = Rect::new(0, 0, 18, TabsSize::Small.height());
        let mut buffer = Buffer::empty(area);

        // Selected A, whole row disabled: the selected tab keeps its
        // selected-disabled identity, the rest grey out.
        Widget::render(
            TabsWidget::new(&["A", "B"])
                .selected_tab(Some(0))
                .disabled(true)
                .style(style),
            area,
            &mut buffer,
        );

        let selected = buffer.cell((2, 0)).expect("selected disabled tab");
        let unselected = buffer.cell((8, 0)).expect("unselected disabled tab");
        assert_eq!(selected.bg, style.selected_disabled_background);
        assert_eq!(unselected.bg, style.disabled_background);
    }

    #[test]
    fn a_disabled_row_is_skipped_by_tab_traversal() {
        #[derive(Default)]
        struct RoutedState {
            focus: crate::runtime::FocusState,
            selected: Screen,
        }

        #[derive(Debug, PartialEq)]
        enum RoutedMsg {
            Focus(crate::runtime::FocusState),
            Selected(Screen),
            Pressed,
        }

        let theme = Theme::default_dark();
        let state = RoutedState::default();
        let mut ratcn = Ratcn::new().focus(|state: &RoutedState| &state.focus, RoutedMsg::Focus);
        let mut terminal = Terminal::new(TestBackend::new(20, 2)).expect("terminal");
        terminal
            .draw(|frame| {
                ratcn.render(frame, &state, &theme, |ctx| {
                    ctx.render_component(
                        ChildId::Static("tabs"),
                        Tabs::new([Tab::new(Screen::A, "A"), Tab::new(Screen::B, "B")])
                            .selection(
                                |state: &RoutedState| Some(state.selected),
                                RoutedMsg::Selected,
                            )
                            .activation(TabsActivation::Automatic)
                            .disabled(true),
                        Rect::new(0, 0, 20, 1),
                    );
                    ctx.render_component(
                        ChildId::Static("button"),
                        crate::Button::new("Next").on_press(|| RoutedMsg::Pressed),
                        Rect::new(0, 1, 20, 1),
                    );
                });
            })
            .expect("draw");

        // Startup focus resolves past the disabled row onto the button.
        assert_eq!(
            ratcn.handle_event(key(KeyCode::Enter), &state),
            EventResult::Emit(RoutedMsg::Pressed)
        );
    }
}