oxi-tui 0.4.0

Terminal UI framework with differential rendering, themes, and components
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
//! Terminal key input parsing.
//!
//! Parses raw terminal byte sequences into structured [`Event`] values.
//! Supports:
//! - Legacy CSI / SS3 escape sequences (arrows, function keys, etc.)
//! - Kitty keyboard protocol (CSI-u, with flags 1, 2, 4)
//! - xterm modifyOtherKeys (CSI 27 ; mod ; code ~)
//! - SGR mouse events
//! - Old-style X10 mouse events
//! - Focus events (focus-in / focus-out)
//! - Bracketed paste
//! - UTF-8 multi-byte character decoding
//!
//! Based on pi-mono's `packages/tui/src/keys.ts`.

use crate::event::{
    Event, KeyCode, KeyEvent, KeyModifiers, MouseButton, MouseEvent, MouseEventKind,
};

// ---------------------------------------------------------------------------
// Constants
// ---------------------------------------------------------------------------

const ESC: u8 = 0x1b;
const DEL: u8 = 0x7f;
const BS: u8 = 0x08;

/// Modifier bit flags (match the Kitty / xterm convention).
mod modifier_bits {
    pub const SHIFT: u8 = 1;
    pub const ALT: u8 = 2;
    pub const CTRL: u8 = 4;
    pub const SUPER: u8 = 8;
    /// Caps Lock (ignored in modifier comparison).
    pub const CAPS_LOCK: u8 = 64;
    /// Num Lock (ignored in modifier comparison).
    pub const NUM_LOCK: u8 = 128;
}

/// Negative codepoints used by the Kitty protocol for non-printable keys.
mod codepoints {
    pub const ESCAPE: i64 = 27;
    pub const TAB: i64 = 9;
    pub const ENTER: i64 = 13;
    pub const SPACE: i64 = 32;
    pub const BACKSPACE: i64 = 127;
    pub const KP_ENTER: i64 = 57414;

    pub const UP: i64 = -1;
    pub const DOWN: i64 = -2;
    pub const RIGHT: i64 = -3;
    pub const LEFT: i64 = -4;

    pub const DELETE: i64 = -10;
    pub const INSERT: i64 = -11;
    pub const PAGE_UP: i64 = -12;
    pub const PAGE_DOWN: i64 = -13;
    pub const HOME: i64 = -14;
    pub const END: i64 = -15;
}

// ---------------------------------------------------------------------------
// Kitty protocol state
// ---------------------------------------------------------------------------

/// Global Kitty keyboard protocol state.
///
/// When active, some legacy sequences are re-interpreted (e.g. `\x1b\r`
/// becomes Shift+Enter instead of Alt+Enter).
static mut KITTY_PROTOCOL_ACTIVE: bool = false;

/// Set whether the Kitty keyboard protocol is currently active.
///
/// # Safety
/// Should only be called from a single-threaded context (before the TUI
/// event loop starts) or protected by external synchronisation.
pub unsafe fn set_kitty_protocol_active(active: bool) {
    KITTY_PROTOCOL_ACTIVE = active;
}

/// Query whether the Kitty keyboard protocol is currently active.
///
/// # Safety
/// Same constraints as [`set_kitty_protocol_active`].
pub unsafe fn is_kitty_protocol_active() -> bool {
    KITTY_PROTOCOL_ACTIVE
}

/// Thread-safe wrapper used at call-sites.
fn kitty_active() -> bool {
    // SAFETY: read of a bool that is only written once during init.
    unsafe { KITTY_PROTOCOL_ACTIVE }
}

// ---------------------------------------------------------------------------
// Event type (Kitty flag 2)
// ---------------------------------------------------------------------------

/// Key event type reported by Kitty keyboard protocol flag 2.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum KeyEventType {
    Press,
    Repeat,
    Release,
}

// Track the last parsed event type so callers can query it.
static mut LAST_EVENT_TYPE: KeyEventType = KeyEventType::Press;

/// Retrieve the event type of the most recent key parse.
///
/// # Safety
/// Read-only; safe to call from the main TUI thread.
pub unsafe fn last_key_event_type() -> KeyEventType {
    LAST_EVENT_TYPE
}

fn set_last_event_type(ty: KeyEventType) {
    // SAFETY: single-threaded TUI context.
    unsafe {
        LAST_EVENT_TYPE = ty;
    }
}

fn parse_event_type_str(s: Option<&str>) -> KeyEventType {
    match s {
        Some("2") => KeyEventType::Repeat,
        Some("3") => KeyEventType::Release,
        _ => KeyEventType::Press,
    }
}

// ---------------------------------------------------------------------------
// Kitty numpad codepoint normalization
// ---------------------------------------------------------------------------

/// Map Kitty numpad codepoints to their equivalent main-keyboard codepoints.
fn normalize_kitty_functional_codepoint(cp: i64) -> i64 {
    match cp {
        57399 => 48,                // KP_0 -> '0'
        57400 => 49,                // KP_1 -> '1'
        57401 => 50,                // KP_2 -> '2'
        57402 => 51,                // KP_3 -> '3'
        57403 => 52,                // KP_4 -> '4'
        57404 => 53,                // KP_5 -> '5'
        57405 => 54,                // KP_6 -> '6'
        57406 => 55,                // KP_7 -> '7'
        57407 => 56,                // KP_8 -> '8'
        57408 => 57,                // KP_9 -> '9'
        57409 => 46,                // KP_DECIMAL -> '.'
        57410 => 47,                // KP_DIVIDE -> '/'
        57411 => 42,                // KP_MULTIPLY -> '*'
        57412 => 45,                // KP_SUBTRACT -> '-'
        57413 => 43,                // KP_ADD -> '+'
        57415 => 61,                // KP_EQUAL -> '='
        57416 => 44,                // KP_SEPARATOR -> ','
        57417 => codepoints::LEFT,  // KP_LEFT
        57418 => codepoints::RIGHT, // KP_RIGHT
        57419 => codepoints::UP,    // KP_UP
        57420 => codepoints::DOWN,  // KP_DOWN
        57421 => codepoints::PAGE_UP,
        57422 => codepoints::PAGE_DOWN,
        57423 => codepoints::HOME,
        57424 => codepoints::END,
        57425 => codepoints::INSERT,
        57426 => codepoints::DELETE,
        _ => cp,
    }
}

/// If Shift is held and the codepoint is an uppercase ASCII letter (A-Z),
/// normalise to lowercase (a-z) so that 'Shift+A' is treated the same as
/// 'Shift+a' for identity comparison purposes.
fn normalize_shifted_letter(cp: i64, modifier: u8) -> i64 {
    let effective = modifier & !(modifier_bits::CAPS_LOCK | modifier_bits::NUM_LOCK);
    if (effective & modifier_bits::SHIFT) != 0 && cp >= 65 && cp <= 90 {
        cp + 32
    } else {
        cp
    }
}

/// Mask out the lock bits (Caps Lock + Num Lock) from a modifier byte.
fn effective_modifier(modifier: u8) -> u8 {
    modifier & !(modifier_bits::CAPS_LOCK | modifier_bits::NUM_LOCK)
}

// ---------------------------------------------------------------------------
// Kitty sequence parser
// ---------------------------------------------------------------------------

#[derive(Debug)]
struct ParsedKitty {
    codepoint: i64,
    shifted_key: Option<i64>,
    base_layout_key: Option<i64>,
    modifier: u8,
    #[allow(dead_code)] // stored for querying via last_key_event_type()
    event_type: KeyEventType,
}

/// Parse a Kitty CSI-u sequence.
///
/// Supported forms:
/// - `ESC [ <cp> u`
/// - `ESC [ <cp> ; <mod> u`
/// - `ESC [ <cp> ; <mod> : <event> u`
/// - `ESC [ <cp> : <shifted> ; <mod> u`
/// - `ESC [ <cp> : <shifted> : <base> ; <mod> u`
/// - `ESC [ <cp> :: <base> ; <mod> u`
fn parse_kitty_sequence(data: &[u8]) -> Option<ParsedKitty> {
    let s = std::str::from_utf8(data).ok()?;
    if !s.starts_with('\x1b') || s.len() < 4 {
        return None;
    }

    // CSI u format
    if let Some(rest) = s.strip_prefix("\x1b[") {
        if rest.ends_with('u') && !rest.starts_with('<') {
            return parse_csi_u(rest.strip_suffix('u')?);
        }
        // Arrow with modifier: \x1b[1;<mod>A/B/C/D  or  \x1b[1;<mod>:<event>A/B/C/D
        if let Some(arrow) = parse_arrow_kitty(rest) {
            return Some(arrow);
        }
        // Functional key: \x1b<num>~ or \x1b<num>;<mod>~ etc.
        if rest.ends_with('~') {
            return parse_functional_kitty(rest.strip_suffix('~')?);
        }
        // Home/End with modifier: \x1b[1;<mod>H/F
        if let Some(he) = parse_home_end_kitty(rest) {
            return Some(he);
        }
    }

    None
}

/// Parse the interior of `ESC [ ... u`.
fn parse_csi_u(inner: &str) -> Option<ParsedKitty> {
    // Split off the modifier part after ';'
    let (cp_part, mod_part) = if let Some(idx) = inner.find(';') {
        (&inner[..idx], &inner[idx + 1..])
    } else {
        (inner, "")
    };

    // cp_part may contain codepoint[:shifted[:base]]
    let mut cp_iter = cp_part.splitn(3, ':');
    let cp_str = cp_iter.next()?;
    let shifted_str = cp_iter.next();
    let base_str = cp_iter.next();

    let codepoint: i64 = cp_str.parse().ok()?;
    let shifted_key = shifted_str
        .filter(|s| !s.is_empty())
        .and_then(|s| s.parse().ok());
    let base_layout_key = base_str.and_then(|s| s.parse().ok());

    // mod_part may be <mod>[:<event>]
    let (mod_val, event_type) = if mod_part.is_empty() {
        (1u8, KeyEventType::Press)
    } else {
        let mut miter = mod_part.splitn(2, ':');
        let m_str = miter.next()?;
        let e_str = miter.next();
        let m: u8 = m_str.parse().ok()?;
        (m, parse_event_type_str(e_str))
    };

    set_last_event_type(event_type);
    Some(ParsedKitty {
        codepoint,
        shifted_key,
        base_layout_key,
        modifier: mod_val.saturating_sub(1),
        event_type,
    })
}

/// Parse arrow key Kitty form: `1;<mod>A` or `1;<mod>:<event>A`.
fn parse_arrow_kitty(rest: &str) -> Option<ParsedKitty> {
    if !rest.starts_with('1') {
        return None;
    }
    let last = rest.chars().last()?;
    let letter = match last {
        'A' => codepoints::UP,
        'B' => codepoints::DOWN,
        'C' => codepoints::RIGHT,
        'D' => codepoints::LEFT,
        _ => return None,
    };

    // Strip trailing letter
    let inner = &rest[1..rest.len() - 1]; // strip leading '1' and trailing letter
    if inner.is_empty() {
        // bare `\x1b[1A` etc. – no modifier
        set_last_event_type(KeyEventType::Press);
        return Some(ParsedKitty {
            codepoint: letter,
            shifted_key: None,
            base_layout_key: None,
            modifier: 0,
            event_type: KeyEventType::Press,
        });
    }

    // inner starts with ';' if modifier present
    let inner = inner.strip_prefix(';')?;
    let mut iter = inner.splitn(2, ':');
    let mod_str = iter.next()?;
    let event_str = iter.next();
    let mod_val: u8 = mod_str.parse().ok()?;
    let event_type = parse_event_type_str(event_str);

    set_last_event_type(event_type);
    Some(ParsedKitty {
        codepoint: letter,
        shifted_key: None,
        base_layout_key: None,
        modifier: mod_val.saturating_sub(1),
        event_type,
    })
}

/// Parse functional key Kitty form: `<num>` or `<num>;<mod>` or `<num>;<mod>:<event>`.
fn parse_functional_kitty(inner: &str) -> Option<ParsedKitty> {
    let (num_part, rest) = if let Some(idx) = inner.find(';') {
        (&inner[..idx], &inner[idx + 1..])
    } else {
        (inner, "")
    };
    let key_num: u32 = num_part.parse().ok()?;
    let cp = match key_num {
        2 => codepoints::INSERT,
        3 => codepoints::DELETE,
        5 => codepoints::PAGE_UP,
        6 => codepoints::PAGE_DOWN,
        7 => codepoints::HOME,
        8 => codepoints::END,
        _ => return None,
    };

    let (mod_val, event_type) = if rest.is_empty() {
        (1u8, KeyEventType::Press)
    } else {
        let mut iter = rest.splitn(2, ':');
        let m_str = iter.next()?;
        let e_str = iter.next();
        let m: u8 = m_str.parse().ok()?;
        (m, parse_event_type_str(e_str))
    };

    set_last_event_type(event_type);
    Some(ParsedKitty {
        codepoint: cp,
        shifted_key: None,
        base_layout_key: None,
        modifier: mod_val.saturating_sub(1),
        event_type,
    })
}

/// Parse Home/End Kitty form: `1;<mod>H` or `1;<mod>:<event>F`.
fn parse_home_end_kitty(rest: &str) -> Option<ParsedKitty> {
    if !rest.starts_with('1') {
        return None;
    }
    let last = rest.chars().last()?;
    let cp = match last {
        'H' => codepoints::HOME,
        'F' => codepoints::END,
        _ => return None,
    };

    let inner = &rest[1..rest.len() - 1];
    if inner.is_empty() {
        set_last_event_type(KeyEventType::Press);
        return Some(ParsedKitty {
            codepoint: cp,
            shifted_key: None,
            base_layout_key: None,
            modifier: 0,
            event_type: KeyEventType::Press,
        });
    }

    let inner = inner.strip_prefix(';')?;
    let mut iter = inner.splitn(2, ':');
    let mod_str = iter.next()?;
    let event_str = iter.next();
    let mod_val: u8 = mod_str.parse().ok()?;
    let event_type = parse_event_type_str(event_str);

    set_last_event_type(event_type);
    Some(ParsedKitty {
        codepoint: cp,
        shifted_key: None,
        base_layout_key: None,
        modifier: mod_val.saturating_sub(1),
        event_type,
    })
}

// ---------------------------------------------------------------------------
// xterm modifyOtherKeys parser
// ---------------------------------------------------------------------------

#[derive(Debug)]
struct ParsedModifyOtherKeys {
    codepoint: i64,
    modifier: u8,
}

/// Parse xterm modifyOtherKeys: `ESC [ 27 ; <mod> ; <code> ~`.
fn parse_modify_other_keys(data: &[u8]) -> Option<ParsedModifyOtherKeys> {
    let s = std::str::from_utf8(data).ok()?;
    if !s.starts_with("\x1b[27;") || !s.ends_with('~') {
        return None;
    }
    let inner = &s[5..s.len() - 1]; // strip "\x1b[27;" and "~"
    let mut parts = inner.splitn(2, ';');
    let mod_str = parts.next()?;
    let cp_str = parts.next()?;
    let mod_val: u8 = mod_str.parse().ok()?;
    let cp: i64 = cp_str.parse().ok()?;
    Some(ParsedModifyOtherKeys {
        codepoint: cp,
        modifier: mod_val.saturating_sub(1),
    })
}

// ---------------------------------------------------------------------------
// Kitty printable decoding (for text insertion)
// ---------------------------------------------------------------------------

/// Decode a Kitty CSI-u sequence into a printable character, if applicable.
///
/// Only plain or Shift-modified keys are decoded. Ctrl/Alt combinations are
/// rejected (those are keybindings, not text input).
pub fn decode_kitty_printable(data: &[u8]) -> Option<char> {
    let kitty = parse_kitty_sequence(data)?;
    let modifier = effective_modifier(kitty.modifier);
    // Only accept shift + lock bits
    if (modifier & !(modifier_bits::SHIFT | modifier_bits::CAPS_LOCK | modifier_bits::NUM_LOCK)) != 0 {
        return None;
    }
    if (modifier & (modifier_bits::ALT | modifier_bits::CTRL)) != 0 {
        return None;
    }

    let mut cp = kitty.codepoint;
    if (modifier & modifier_bits::SHIFT) != 0 {
        if let Some(sk) = kitty.shifted_key {
            cp = sk;
        }
    }
    cp = normalize_kitty_functional_codepoint(cp);
    if cp < 32 {
        return None;
    }
    char::from_u32(cp as u32)
}

/// Decode an xterm modifyOtherKeys sequence into a printable character.
fn decode_modify_other_keys_printable(data: &[u8]) -> Option<char> {
    let parsed = parse_modify_other_keys(data)?;
    let modifier = effective_modifier(parsed.modifier);
    if (modifier & !modifier_bits::SHIFT) != 0 {
        return None;
    }
    if parsed.codepoint < 32 {
        return None;
    }
    char::from_u32(parsed.codepoint as u32)
}

/// Decode a raw terminal sequence into a printable character.
///
/// Tries Kitty CSI-u first, then xterm modifyOtherKeys.
pub fn decode_printable_key(data: &[u8]) -> Option<char> {
    decode_kitty_printable(data).or_else(|| decode_modify_other_keys_printable(data))
}

// ---------------------------------------------------------------------------
// Key release / repeat detection
// ---------------------------------------------------------------------------

/// Check whether a raw input sequence is a key-release event
/// (Kitty protocol flag 2).
pub fn is_key_release(data: &[u8]) -> bool {
    let s = match std::str::from_utf8(data) {
        Ok(s) => s,
        Err(_) => return false,
    };
    // Don't treat bracketed paste content as key release
    if s.contains("\x1b[200~") {
        return false;
    }
    s.contains(":3u")
        || s.contains(":3~")
        || s.contains(":3A")
        || s.contains(":3B")
        || s.contains(":3C")
        || s.contains(":3D")
        || s.contains(":3H")
        || s.contains(":3F")
}

/// Check whether a raw input sequence is a key-repeat event
/// (Kitty protocol flag 2).
pub fn is_key_repeat(data: &[u8]) -> bool {
    let s = match std::str::from_utf8(data) {
        Ok(s) => s,
        Err(_) => return false,
    };
    if s.contains("\x1b[200~") {
        return false;
    }
    s.contains(":2u")
        || s.contains(":2~")
        || s.contains(":2A")
        || s.contains(":2B")
        || s.contains(":2C")
        || s.contains(":2D")
        || s.contains(":2H")
        || s.contains(":2F")
}

// ---------------------------------------------------------------------------
// Modifier bit → KeyModifiers conversion
// ---------------------------------------------------------------------------

fn modifiers_from_bitfield(modifier: u8) -> KeyModifiers {
    let eff = effective_modifier(modifier);
    KeyModifiers {
        shift: (eff & modifier_bits::SHIFT) != 0,
        ctrl: (eff & modifier_bits::CTRL) != 0,
        alt: (eff & modifier_bits::ALT) != 0,
        meta: (eff & modifier_bits::SUPER) != 0,
    }
}

// ---------------------------------------------------------------------------
// Codepoint → KeyCode
// ---------------------------------------------------------------------------

fn codepoint_to_key_code(cp: i64) -> Option<KeyCode> {
    match cp {
        codepoints::ESCAPE => Some(KeyCode::Escape),
        codepoints::TAB => Some(KeyCode::Tab),
        codepoints::ENTER | codepoints::KP_ENTER => Some(KeyCode::Enter),
        codepoints::SPACE => Some(KeyCode::Char(' ')),
        codepoints::BACKSPACE => Some(KeyCode::Backspace),
        codepoints::DELETE => Some(KeyCode::Delete),
        codepoints::INSERT => Some(KeyCode::Insert),
        codepoints::HOME => Some(KeyCode::Home),
        codepoints::END => Some(KeyCode::End),
        codepoints::PAGE_UP => Some(KeyCode::PageUp),
        codepoints::PAGE_DOWN => Some(KeyCode::PageDown),
        codepoints::UP => Some(KeyCode::Up),
        codepoints::DOWN => Some(KeyCode::Down),
        codepoints::LEFT => Some(KeyCode::Left),
        codepoints::RIGHT => Some(KeyCode::Right),
        cp if cp >= 48 && cp <= 57 => Some(KeyCode::Char(char::from_u32(cp as u32)?)),
        cp if cp >= 97 && cp <= 122 => Some(KeyCode::Char(char::from_u32(cp as u32)?)),
        cp if cp >= 65 && cp <= 90 => Some(KeyCode::Char(char::from_u32(cp as u32)?.to_ascii_lowercase())),
        cp if cp >= 32 => char::from_u32(cp as u32).map(KeyCode::Char),
        _ => None,
    }
}

/// Check if a codepoint represents a symbol key.
fn is_symbol_cp(cp: i64) -> bool {
    matches!(
        cp,
        96 // `
        | 45  // -
        | 61  // =
        | 91  // [
        | 93  // ]
        | 92  // \
        | 59  // ;
        | 39  // '
        | 44  // ,
        | 46  // .
        | 47  // /
        | 33  // !
        | 64  // @
        | 35  // #
        | 36  // $
        | 37  // %
        | 94  // ^
        | 38  // &
        | 42  // *
        | 40  // (
        | 41  // )
        | 95  // _
        | 43  // +
        | 124 // |
        | 126 // ~
        | 123 // {
        | 125 // }
        | 58  // :
        | 60  // <
        | 62  // >
        | 63  // ?
    )
}

/// Convert a Kitty-parsed codepoint into a KeyCode, using the base layout
/// key fallback for non-Latin keyboard layouts.
fn kitty_codepoint_to_key_code(parsed: &ParsedKitty) -> Option<KeyCode> {
    let cp = normalize_kitty_functional_codepoint(parsed.codepoint);
    let eff_cp = normalize_shifted_letter(cp, parsed.modifier);

    // Determine if we should use the base layout key
    let is_latin = eff_cp >= 97 && eff_cp <= 122;
    let is_digit = eff_cp >= 48 && eff_cp <= 57;
    let is_symbol = is_symbol_cp(eff_cp);
    let use_base = !is_latin && !is_digit && !is_symbol;

    let final_cp = if use_base {
        parsed.base_layout_key.unwrap_or(eff_cp)
    } else {
        eff_cp
    };

    codepoint_to_key_code(final_cp)
}

// ---------------------------------------------------------------------------
// Mouse event parsing
// ---------------------------------------------------------------------------

/// Parse an SGR mouse event: `ESC [ < B ; X ; Y M/m`.
fn parse_sgr_mouse(data: &[u8]) -> Option<Event> {
    let s = std::str::from_utf8(data).ok()?;
    if !s.starts_with("\x1b[<") {
        return None;
    }
    let last = s.chars().last()?;
    let release = last == 'm';
    let inner = &s[3..s.len() - 1]; // strip "\x1b[<" and trailing M/m
    let mut parts = inner.split(';');
    let button_raw: u16 = parts.next()?.parse().ok()?;
    let col: u16 = parts.next()?.parse().ok()?;
    let row: u16 = parts.next()?.parse().ok()?;

    let (kind, button) = if button_raw >= 64 {
        // Scroll events: 64 = scroll up, 65 = scroll down
        let scroll_kind = if button_raw == 64 {
            MouseEventKind::ScrollUp
        } else {
            MouseEventKind::ScrollDown
        };
        (scroll_kind, MouseButton::None)
    } else if button_raw >= 32 {
        // Motion / drag events: button = raw - 32
        let btn = match button_raw - 32 {
            0 => MouseButton::Left,
            1 => MouseButton::Middle,
            2 => MouseButton::Right,
            _ => MouseButton::None,
        };
        (MouseEventKind::Drag, btn)
    } else if release {
        let btn = match button_raw {
            0 => MouseButton::Left,
            1 => MouseButton::Middle,
            2 => MouseButton::Right,
            _ => MouseButton::None,
        };
        (MouseEventKind::Release, btn)
    } else {
        let btn = match button_raw {
            0 => MouseButton::Left,
            1 => MouseButton::Middle,
            2 => MouseButton::Right,
            _ => MouseButton::None,
        };
        (MouseEventKind::Press, btn)
    };

    Some(Event::Mouse(MouseEvent {
        kind,
        button,
        row: row.saturating_sub(1),
        col: col.saturating_sub(1),
    }))
}

/// Parse an old-style X10 mouse event: `ESC [ M Cb Cx Cy` (6 bytes total).
fn parse_x10_mouse(data: &[u8]) -> Option<Event> {
    if data.len() != 6 || data[0] != ESC || data[1] != b'[' || data[2] != b'M' {
        return None;
    }
    let cb = data[3];
    let cx = data[4];
    let cy = data[5];

    let button_raw = cb.wrapping_sub(32);

    let (kind, button) = if button_raw >= 64 {
        let scroll_kind = if button_raw == 64 {
            MouseEventKind::ScrollUp
        } else {
            MouseEventKind::ScrollDown
        };
        (scroll_kind, MouseButton::None)
    } else {
        let btn = match button_raw & 0x03 {
            0 => MouseButton::Left,
            1 => MouseButton::Middle,
            2 => MouseButton::Right,
            _ => MouseButton::None,
        };
        (MouseEventKind::Press, btn)
    };

    Some(Event::Mouse(MouseEvent {
        kind,
        button,
        row: (cy.wrapping_sub(32) as u16).saturating_sub(1),
        col: (cx.wrapping_sub(32) as u16).saturating_sub(1),
    }))
}

// ---------------------------------------------------------------------------
// UTF-8 decoding
// ---------------------------------------------------------------------------

/// Decode a UTF-8 multi-byte sequence from raw bytes.
/// Returns `None` if the bytes don't form a valid UTF-8 character.
fn decode_utf8_char(data: &[u8]) -> Option<char> {
    let s = std::str::from_utf8(data).ok()?;
    let ch = s.chars().next()?;
    Some(ch)
}

// ---------------------------------------------------------------------------
// Main parser: raw bytes → Event
// ---------------------------------------------------------------------------

/// Parse a raw terminal byte sequence into an [`Event`].
///
/// This is the primary entry point. It handles all supported escape sequence
/// types and falls back to UTF-8 character decoding for plain text.
pub fn parse_event(data: &[u8]) -> Option<Event> {
    if data.is_empty() {
        return None;
    }

    // --- Bracketed paste start/end ---
    if data == b"\x1b[200~" || data == b"\x1b[201~" {
        // These are handled by StdinBuffer; should not appear here.
        return None;
    }

    // --- Focus events ---
    if data == b"\x1b[I" {
        return Some(Event::FocusGained);
    }
    if data == b"\x1b[O" {
        return Some(Event::FocusLost);
    }

    // --- SGR mouse ---
    if data.starts_with(b"\x1b[<") {
        return parse_sgr_mouse(data);
    }

    // --- X10 mouse ---
    if data.starts_with(b"\x1b[M") {
        return parse_x10_mouse(data);
    }

    // --- Kitty protocol sequences ---
    if let Some(kitty) = parse_kitty_sequence(data) {
        let code = kitty_codepoint_to_key_code(&kitty)?;
        let modifiers = modifiers_from_bitfield(kitty.modifier);
        return Some(Event::Key(KeyEvent::with_modifiers(code, modifiers)));
    }

    // --- xterm modifyOtherKeys ---
    if let Some(mok) = parse_modify_other_keys(data) {
        let code = codepoint_to_key_code(mok.codepoint)?;
        let modifiers = modifiers_from_bitfield(mok.modifier);
        return Some(Event::Key(KeyEvent::with_modifiers(code, modifiers)));
    }

    // --- Legacy sequences ---
    if data[0] == ESC {
        return parse_legacy_escape(data);
    }

    // --- Raw single-byte / UTF-8 characters ---
    parse_raw_char(data)
}

// ---------------------------------------------------------------------------
// Legacy escape sequence handling
// ---------------------------------------------------------------------------

fn parse_legacy_escape(data: &[u8]) -> Option<Event> {
    if data.len() == 1 && data[0] == ESC {
        return Some(Event::key(KeyCode::Escape));
    }

    let s = std::str::from_utf8(data).ok()?;

    // Kitty-protocol-aware legacy sequences
    if kitty_active() {
        if data == b"\x1b\r" || data == b"\n" {
            return Some(Event::Key(KeyEvent::with_modifiers(
                KeyCode::Enter,
                KeyModifiers {
                    shift: true,
                    ..KeyModifiers::default()
                },
            )));
        }
    }

    // F1-F4 via SS3: ESC O P/Q/R/S
    if data.len() == 3 && data[0] == ESC && data[1] == b'O' {
        match data[2] {
            b'P' => return Some(Event::key(KeyCode::F(1))),
            b'Q' => return Some(Event::key(KeyCode::F(2))),
            b'R' => return Some(Event::key(KeyCode::F(3))),
            b'S' => return Some(Event::key(KeyCode::F(4))),
            // SS3 arrows (some terminals)
            b'A' => return Some(Event::key(KeyCode::Up)),
            b'B' => return Some(Event::key(KeyCode::Down)),
            b'C' => return Some(Event::key(KeyCode::Right)),
            b'D' => return Some(Event::key(KeyCode::Left)),
            // SS3 Home/End
            b'H' => return Some(Event::key(KeyCode::Home)),
            b'F' => return Some(Event::key(KeyCode::End)),
            // SS3 M = numpad enter
            b'M' => return Some(Event::key(KeyCode::Enter)),
            // SS3 E = "clear" / "5" key on numpad
            b'E' => return Some(Event::key(KeyCode::Char('5'))),
            // SS3 ctrl arrows
            b'a' => {
                return Some(Event::Key(KeyEvent::with_modifiers(
                    KeyCode::Up,
                    KeyModifiers {
                        ctrl: true,
                        ..KeyModifiers::default()
                    },
                )))
            }
            b'b' => {
                return Some(Event::Key(KeyEvent::with_modifiers(
                    KeyCode::Down,
                    KeyModifiers {
                        ctrl: true,
                        ..KeyModifiers::default()
                    },
                )))
            }
            b'c' => {
                return Some(Event::Key(KeyEvent::with_modifiers(
                    KeyCode::Right,
                    KeyModifiers {
                        ctrl: true,
                        ..KeyModifiers::default()
                    },
                )))
            }
            b'd' => {
                return Some(Event::Key(KeyEvent::with_modifiers(
                    KeyCode::Left,
                    KeyModifiers {
                        ctrl: true,
                        ..KeyModifiers::default()
                    },
                )))
            }
            _ => {}
        }
    }

    // CSI sequences
    if data.len() >= 3 && data[0] == ESC && data[1] == b'[' {
        return parse_csi_sequence(data, s);
    }

    // Two-byte ESC sequences: ESC + char
    if data.len() == 2 && data[0] == ESC {
        return parse_esc_prefix(data[1]);
    }

    None
}

/// Parse CSI sequences (ESC [ ...).
fn parse_csi_sequence(data: &[u8], s: &str) -> Option<Event> {
    let inner = &s[2..]; // skip ESC[

    // Arrow keys: ESC[A/B/C/D
    if data == b"\x1b[A" {
        return Some(Event::key(KeyCode::Up));
    }
    if data == b"\x1b[B" {
        return Some(Event::key(KeyCode::Down));
    }
    if data == b"\x1b[C" {
        return Some(Event::key(KeyCode::Right));
    }
    if data == b"\x1b[D" {
        return Some(Event::key(KeyCode::Left));
    }

    // Home: ESC[H
    if data == b"\x1b[H" {
        return Some(Event::key(KeyCode::Home));
    }
    // End: ESC[F
    if data == b"\x1b[F" {
        return Some(Event::key(KeyCode::End));
    }

    // Shift+Tab: ESC[Z
    if data == b"\x1b[Z" {
        return Some(Event::Key(KeyEvent::with_modifiers(
            KeyCode::Tab,
            KeyModifiers {
                shift: true,
                ..KeyModifiers::default()
            },
        )));
    }

    // Shift arrows: ESC[a/b/c/d
    if data == b"\x1b[a" {
        return Some(shift_key(KeyCode::Up));
    }
    if data == b"\x1b[b" {
        return Some(shift_key(KeyCode::Down));
    }
    if data == b"\x1b[c" {
        return Some(shift_key(KeyCode::Right));
    }
    if data == b"\x1b[d" {
        return Some(shift_key(KeyCode::Left));
    }

    // Alt arrows (legacy): ESC p/n/b/f
    // (These come through as two-byte ESC sequences, handled in parse_esc_prefix)

    // CSI ~ sequences: functional keys
    if inner.ends_with('~') {
        return parse_csi_tilde(data, inner);
    }

    // Alt+arrow via explicit CSI: ESC[1;3A/B/C/D
    if data == b"\x1b[1;3A" {
        return Some(alt_key(KeyCode::Up));
    }
    if data == b"\x1b[1;3B" {
        return Some(alt_key(KeyCode::Down));
    }
    if data == b"\x1b[1;3C" {
        return Some(alt_key(KeyCode::Right));
    }
    if data == b"\x1b[1;3D" {
        return Some(alt_key(KeyCode::Left));
    }

    // Ctrl+arrow via CSI: ESC[1;5A/B/C/D
    if data == b"\x1b[1;5A" {
        return Some(ctrl_key(KeyCode::Up));
    }
    if data == b"\x1b[1;5B" {
        return Some(ctrl_key(KeyCode::Down));
    }
    if data == b"\x1b[1;5C" {
        return Some(ctrl_key(KeyCode::Right));
    }
    if data == b"\x1b[1;5D" {
        return Some(ctrl_key(KeyCode::Left));
    }

    None
}

/// Parse CSI ~ sequences for functional keys.
fn parse_csi_tilde(_data: &[u8], inner: &str) -> Option<Event> {
    // Strip trailing ~
    let payload = &inner[..inner.len() - 1];

    // Check for modifier: <num>;<mod>~
    let (num_str, modifier) = if let Some(idx) = payload.find(';') {
        let n = &payload[..idx];
        let m_str = &payload[idx + 1..];
        let m: u8 = m_str.parse().ok()?;
        (n, m.saturating_sub(1))
    } else {
        (payload, 0u8)
    };

    let num: u32 = num_str.parse().ok()?;
    let code = match num {
        1 => Some(KeyCode::Home),     // ESC[1~
        2 => Some(KeyCode::Insert),   // ESC[2~
        3 => Some(KeyCode::Delete),   // ESC[3~
        4 => Some(KeyCode::End),      // ESC[4~
        5 => Some(KeyCode::PageUp),   // ESC[5~
        6 => Some(KeyCode::PageDown), // ESC[6~
        7 => Some(KeyCode::Home),     // ESC[7~
        8 => Some(KeyCode::End),      // ESC[8~
        11 => Some(KeyCode::F(1)),    // ESC[11~
        12 => Some(KeyCode::F(2)),    // ESC[12~
        13 => Some(KeyCode::F(3)),    // ESC[13~
        14 => Some(KeyCode::F(4)),    // ESC[14~
        15 => Some(KeyCode::F(5)),    // ESC[15~
        17 => Some(KeyCode::F(6)),    // ESC[17~
        18 => Some(KeyCode::F(7)),    // ESC[18~
        19 => Some(KeyCode::F(8)),    // ESC[19~
        20 => Some(KeyCode::F(9)),    // ESC[20~
        21 => Some(KeyCode::F(10)),   // ESC[21~
        23 => Some(KeyCode::F(11)),   // ESC[23~
        24 => Some(KeyCode::F(12)),   // ESC[24~
        _ => None,
    };

    let code = code?;

    if modifier == 0 {
        return Some(Event::key(code));
    }

    // Shift-modified functional keys: ESC[<num>$
    // Ctrl-modified functional keys: ESC[<num>^
    // We handle these through the modifier value from the semicolon form.
    let mods = modifiers_from_bitfield(modifier);
    Some(Event::Key(KeyEvent::with_modifiers(code, mods)))
}

/// Parse two-byte ESC prefix sequences: ESC <byte>.
fn parse_esc_prefix(second: u8) -> Option<Event> {
    // Ctrl+Alt + letter: ESC + ctrl_char (1..26)
    if second >= 1 && second <= 26 {
        let ch = char::from(second + 96); // 1→'a', 2→'b', ...
        return Some(Event::Key(KeyEvent::with_modifiers(
            KeyCode::Char(ch),
            KeyModifiers {
                ctrl: true,
                alt: true,
                ..KeyModifiers::default()
            },
        )));
    }

    // Alt+letter/digit: ESC + letter/digit
    if (second >= b'a' && second <= b'z') || (second >= b'0' && second <= b'9') {
        let ch = second as char;
        return Some(Event::Key(KeyEvent::with_modifiers(
            KeyCode::Char(ch),
            KeyModifiers {
                alt: true,
                ..KeyModifiers::default()
            },
        )));
    }

    // Alt+Backspace: ESC DEL or ESC BS
    if second == DEL {
        return Some(Event::Key(KeyEvent::with_modifiers(
            KeyCode::Backspace,
            KeyModifiers {
                alt: true,
                ..KeyModifiers::default()
            },
        )));
    }
    if second == BS {
        return Some(Event::Key(KeyEvent::with_modifiers(
            KeyCode::Backspace,
            KeyModifiers {
                alt: true,
                ..KeyModifiers::default()
            },
        )));
    }

    // Special two-byte sequences
    match second {
        // Alt+Enter: ESC \r (only in non-Kitty mode; Kitty mode handled above)
        b'\r' if !kitty_active() => {
            return Some(Event::Key(KeyEvent::with_modifiers(
                KeyCode::Enter,
                KeyModifiers {
                    alt: true,
                    ..KeyModifiers::default()
                },
            )));
        }
        // Alt+Space: ESC ' '
        b' ' => {
            return Some(Event::Key(KeyEvent::with_modifiers(
                KeyCode::Char(' '),
                KeyModifiers {
                    alt: true,
                    ..KeyModifiers::default()
                },
            )));
        }
        // Ctrl+\ : ESC Ctrl+\
        0x1c => {
            return Some(Event::Key(KeyEvent::with_modifiers(
                KeyCode::Char('\\'),
                KeyModifiers {
                    ctrl: true,
                    ..KeyModifiers::default()
                },
            )));
        }
        // Ctrl+] : ESC Ctrl+]
        0x1d => {
            return Some(Event::Key(KeyEvent::with_modifiers(
                KeyCode::Char(']'),
                KeyModifiers {
                    ctrl: true,
                    ..KeyModifiers::default()
                },
            )));
        }
        // Ctrl+- : ESC Ctrl+_
        0x1f => {
            return Some(Event::Key(KeyEvent::with_modifiers(
                KeyCode::Char('-'),
                KeyModifiers {
                    ctrl: true,
                    ..KeyModifiers::default()
                },
            )));
        }
        // Ctrl+[ : ESC ESC (double escape → ctrl+alt+[)
        ESC => {
            return Some(Event::Key(KeyEvent::with_modifiers(
                KeyCode::Char('['),
                KeyModifiers {
                    ctrl: true,
                    alt: true,
                    ..KeyModifiers::default()
                },
            )));
        }
        // Alt+Up: ESC p
        b'p' => {
            return Some(alt_key(KeyCode::Up));
        }
        // Alt+Down: ESC n
        b'n' => {
            return Some(alt_key(KeyCode::Down));
        }
        // Alt+Left: ESC b (legacy, only when Kitty not active)
        b'b' if !kitty_active() => {
            return Some(alt_key(KeyCode::Left));
        }
        // Alt+Right: ESC f (legacy, only when Kitty not active)
        b'f' if !kitty_active() => {
            return Some(alt_key(KeyCode::Right));
        }
        _ => {}
    }

    None
}

/// Parse a raw (non-ESC) byte sequence: single character or UTF-8 character.
fn parse_raw_char(data: &[u8]) -> Option<Event> {
    if data.len() == 1 {
        let b = data[0];
        // Enter
        if b == b'\r' {
            return Some(Event::key(KeyCode::Enter));
        }
        // LF → Enter (non-Kitty)
        if b == b'\n' && !kitty_active() {
            return Some(Event::key(KeyCode::Enter));
        }
        // Tab
        if b == b'\t' {
            return Some(Event::key(KeyCode::Tab));
        }
        // NUL → Ctrl+Space
        if b == 0 {
            return Some(Event::Key(KeyEvent::with_modifiers(
                KeyCode::Char(' '),
                KeyModifiers {
                    ctrl: true,
                    ..KeyModifiers::default()
                },
            )));
        }
        // DEL → Backspace
        if b == DEL {
            return Some(Event::key(KeyCode::Backspace));
        }
        // BS → Backspace (or Ctrl+Backspace on Windows Terminal)
        if b == BS {
            return Some(Event::key(KeyCode::Backspace));
        }
        // Ctrl+letter: raw 1..26
        if b >= 1 && b <= 26 {
            let ch = char::from(b + 96);
            return Some(Event::Key(KeyEvent::with_modifiers(
                KeyCode::Char(ch),
                KeyModifiers {
                    ctrl: true,
                    ..KeyModifiers::default()
                },
            )));
        }
        // Printable ASCII (32..126)
        if b >= 32 && b <= 126 {
            return Some(Event::key(KeyCode::Char(b as char)));
        }
    }

    // Multi-byte UTF-8
    if data.len() > 1 {
        if let Some(ch) = decode_utf8_char(data) {
            return Some(Event::key(KeyCode::Char(ch)));
        }
    }

    None
}

// ---------------------------------------------------------------------------
// Helpers
// ---------------------------------------------------------------------------

fn shift_key(code: KeyCode) -> Event {
    Event::Key(KeyEvent::with_modifiers(
        code,
        KeyModifiers {
            shift: true,
            ..KeyModifiers::default()
        },
    ))
}

fn ctrl_key(code: KeyCode) -> Event {
    Event::Key(KeyEvent::with_modifiers(
        code,
        KeyModifiers {
            ctrl: true,
            ..KeyModifiers::default()
        },
    ))
}

fn alt_key(code: KeyCode) -> Event {
    Event::Key(KeyEvent::with_modifiers(
        code,
        KeyModifiers {
            alt: true,
            ..KeyModifiers::default()
        },
    ))
}

// ---------------------------------------------------------------------------
// Key string matching
// ---------------------------------------------------------------------------

/// Parse a key identifier string like `"ctrl+c"` or `"shift+enter"` into
/// its components. Returns owned string for the key part.
fn parse_key_id(key_id: &str) -> Option<(String, bool, bool, bool, bool)> {
    let lower = key_id.to_lowercase();
    let parts: Vec<&str> = lower.split('+').collect();
    let key = (*parts.last()?).to_string();
    Some((
        key,
        parts.contains(&"ctrl"),
        parts.contains(&"shift"),
        parts.contains(&"alt"),
        parts.contains(&"super"),
    ))
}

/// Build a modifier bitmask from parsed components.
fn modifier_bits_from_parts(ctrl: bool, shift: bool, alt: bool, super_: bool) -> u8 {
    let mut m = 0u8;
    if shift {
        m |= modifier_bits::SHIFT;
    }
    if alt {
        m |= modifier_bits::ALT;
    }
    if ctrl {
        m |= modifier_bits::CTRL;
    }
    if super_ {
        m |= modifier_bits::SUPER;
    }
    m
}

/// Convert a key name string to its codepoint value.
fn key_name_to_codepoint(key: &str) -> Option<i64> {
    match key {
        "escape" | "esc" => Some(codepoints::ESCAPE),
        "space" => Some(codepoints::SPACE),
        "tab" => Some(codepoints::TAB),
        "enter" | "return" => Some(codepoints::ENTER),
        "backspace" => Some(codepoints::BACKSPACE),
        "up" => Some(codepoints::UP),
        "down" => Some(codepoints::DOWN),
        "left" => Some(codepoints::LEFT),
        "right" => Some(codepoints::RIGHT),
        "insert" => Some(codepoints::INSERT),
        "delete" => Some(codepoints::DELETE),
        "home" => Some(codepoints::HOME),
        "end" => Some(codepoints::END),
        "pageup" => Some(codepoints::PAGE_UP),
        "pagedown" => Some(codepoints::PAGE_DOWN),
        s if s.starts_with('f') => {
            // f1..f12 – not a single codepoint; handle at match level
            None
        }
        _ => None,
    }
}

/// Match a raw byte sequence against a key identifier string.
///
/// This is the Rust equivalent of the TypeScript `matchesKey(data, keyId)`.
pub fn matches_key(data: &[u8], key_id: &str) -> bool {
    let Some((key, ctrl, shift, alt, super_)) = parse_key_id(key_id) else {
        return false;
    };
    let modifier = modifier_bits_from_parts(ctrl, shift, alt, super_);

    // Helper: try Kitty match
    let try_kitty = |expected_cp: i64, expected_mod: u8| -> bool {
        let Some(kitty) = parse_kitty_sequence(data) else {
            return false;
        };
        let actual_mod = effective_modifier(kitty.modifier);
        let expected_mod = effective_modifier(expected_mod);
        if actual_mod != expected_mod {
            return false;
        }
        let actual_cp = normalize_shifted_letter(
            normalize_kitty_functional_codepoint(kitty.codepoint),
            kitty.modifier,
        );
        let expected_cp = normalize_shifted_letter(
            normalize_kitty_functional_codepoint(expected_cp),
            expected_mod,
        );
        if actual_cp == expected_cp {
            return true;
        }
        // Base layout key fallback
        if let Some(base) = kitty.base_layout_key {
            if base == expected_cp {
                let cp = actual_cp;
                let is_latin = cp >= 97 && cp <= 122;
                let is_digit = cp >= 48 && cp <= 57;
                let is_symbol = is_symbol_cp(cp);
                if !is_latin && !is_digit && !is_symbol {
                    return true;
                }
            }
        }
        false
    };

    // Helper: try modifyOtherKeys match
    let try_mok = |expected_cp: i64, expected_mod: u8| -> bool {
        let Some(mok) = parse_modify_other_keys(data) else {
            return false;
        };
        let actual_mod = effective_modifier(mok.modifier);
        let expected_mod = effective_modifier(expected_mod);
        if actual_mod != expected_mod {
            return false;
        }
        let actual = normalize_shifted_letter(mok.codepoint, mok.modifier);
        let expected = normalize_shifted_letter(expected_cp, expected_mod);
        actual == expected
    };

    match key.as_str() {
        "escape" | "esc" => {
            if modifier != 0 {
                return false;
            }
            if data == b"\x1b" {
                return true;
            }
            if try_kitty(codepoints::ESCAPE, 0) {
                return true;
            }
            if try_mok(codepoints::ESCAPE, 0) {
                return true;
            }
            false
        }
        "space" => {
            if !kitty_active() {
                if modifier == modifier_bits::CTRL && data == b"\x00" {
                    return true;
                }
                if modifier == modifier_bits::ALT && data == b"\x1b " {
                    return true;
                }
            }
            if modifier == 0 && (data == b" " || try_kitty(codepoints::SPACE, 0) || try_mok(codepoints::SPACE, 0)) {
                return true;
            }
            try_kitty(codepoints::SPACE, modifier) || try_mok(codepoints::SPACE, modifier)
        }
        "tab" => {
            if modifier == modifier_bits::SHIFT {
                return data == b"\x1b[Z"
                    || try_kitty(codepoints::TAB, modifier_bits::SHIFT)
                    || try_mok(codepoints::TAB, modifier_bits::SHIFT);
            }
            if modifier == 0 {
                return data == b"\t" || try_kitty(codepoints::TAB, 0);
            }
            try_kitty(codepoints::TAB, modifier) || try_mok(codepoints::TAB, modifier)
        }
        "enter" | "return" => {
            if modifier == modifier_bits::SHIFT {
                if try_kitty(codepoints::ENTER, modifier_bits::SHIFT)
                    || try_kitty(codepoints::KP_ENTER, modifier_bits::SHIFT)
                    || try_mok(codepoints::ENTER, modifier_bits::SHIFT)
                {
                    return true;
                }
                if kitty_active() {
                    return data == b"\x1b\r" || data == b"\n";
                }
                return false;
            }
            if modifier == modifier_bits::ALT {
                if try_kitty(codepoints::ENTER, modifier_bits::ALT)
                    || try_kitty(codepoints::KP_ENTER, modifier_bits::ALT)
                    || try_mok(codepoints::ENTER, modifier_bits::ALT)
                {
                    return true;
                }
                if !kitty_active() {
                    return data == b"\x1b\r";
                }
                return false;
            }
            if modifier == 0 {
                if data == b"\r"
                    || (!kitty_active() && data == b"\n")
                    || data == b"\x1bOM"
                    || try_kitty(codepoints::ENTER, 0)
                    || try_kitty(codepoints::KP_ENTER, 0)
                {
                    return true;
                }
            }
            try_kitty(codepoints::ENTER, modifier)
                || try_kitty(codepoints::KP_ENTER, modifier)
                || try_mok(codepoints::ENTER, modifier)
        }
        "backspace" => {
            if modifier == modifier_bits::ALT {
                if data == b"\x1b\x7f" || data == b"\x1b\x08" {
                    return true;
                }
                return try_kitty(codepoints::BACKSPACE, modifier_bits::ALT)
                    || try_mok(codepoints::BACKSPACE, modifier_bits::ALT);
            }
            if modifier == modifier_bits::CTRL {
                if data == b"\x08" || data == b"\x7f" {
                    // Simplified: treat raw BS as ctrl+backspace when modifier requested
                    return true;
                }
                return try_kitty(codepoints::BACKSPACE, modifier_bits::CTRL)
                    || try_mok(codepoints::BACKSPACE, modifier_bits::CTRL);
            }
            if modifier == 0 {
                if data == b"\x7f" || data == b"\x08" {
                    return true;
                }
                return try_kitty(codepoints::BACKSPACE, 0) || try_mok(codepoints::BACKSPACE, 0);
            }
            try_kitty(codepoints::BACKSPACE, modifier) || try_mok(codepoints::BACKSPACE, modifier)
        }
        _ => {
            // Generic: try Kitty, then modifyOtherKeys, then raw comparison
            // For single-char keys
            if key.len() == 1 {
                let Some(ch) = key.chars().next() else {
                    return false;
                };
                let cp = ch as i64;
                let is_letter = ch.is_ascii_lowercase();
                let is_digit = ch.is_ascii_digit();
                let is_symbol = is_symbol_cp(cp);

                if is_letter || is_digit || is_symbol {
                    // Ctrl+Alt legacy: ESC + ctrl_char
                    if modifier == modifier_bits::CTRL | modifier_bits::ALT
                        && !kitty_active()
                    {
                        if let Some(ctrl_ch) = raw_ctrl_char(ch) {
                            if data.len() == 2 && data[0] == ESC && data[1] == ctrl_ch as u8 {
                                return true;
                            }
                        }
                    }
                    // Alt+letter/digit legacy: ESC + char
                    if modifier == modifier_bits::ALT
                        && !kitty_active()
                        && (is_letter || is_digit)
                    {
                        if data.len() == 2 && data[0] == ESC && data[1] == ch as u8 {
                            return true;
                        }
                    }
                    // Ctrl legacy: raw ctrl char
                    if modifier == modifier_bits::CTRL {
                        if let Some(ctrl_ch) = raw_ctrl_char(ch) {
                            if data.len() == 1 && data[0] == ctrl_ch as u8 {
                                return true;
                            }
                        }
                        return try_kitty(cp, modifier_bits::CTRL)
                            || try_mok(cp, modifier_bits::CTRL);
                    }
                    // Shift+Ctrl
                    if modifier == modifier_bits::SHIFT | modifier_bits::CTRL {
                        return try_kitty(cp, modifier_bits::SHIFT | modifier_bits::CTRL)
                            || try_mok(cp, modifier_bits::SHIFT | modifier_bits::CTRL);
                    }
                    // Shift legacy: uppercase
                    if modifier == modifier_bits::SHIFT {
                        if is_letter && data.len() == 1 && data[0] == ch.to_ascii_uppercase() as u8 {
                            return true;
                        }
                        return try_kitty(cp, modifier_bits::SHIFT)
                            || try_mok(cp, modifier_bits::SHIFT);
                    }
                    // Other modifiers
                    if modifier != 0 {
                        return try_kitty(cp, modifier) || try_mok(cp, modifier);
                    }
                    // No modifier: raw char or Kitty
                    if data.len() == 1 && data[0] == ch as u8 {
                        return true;
                    }
                    // UTF-8 multi-byte
                    if let Ok(s) = std::str::from_utf8(data) {
                        if s == key {
                            return true;
                        }
                    }
                    return try_kitty(cp, 0);
                }
            }

            // Function keys
            if key.starts_with('f') && key.len() <= 3 {
                if let Ok(num) = key[1..].parse::<u8>() {
                    if num >= 1 && num <= 12 && modifier == 0 {
                        return matches_legacy_fn(data, num);
                    }
                }
            }

            // Arrow / navigation keys
            if let Some(cp) = key_name_to_codepoint(&key) {
                if modifier == 0 {
                    return matches_legacy_cp(data, &key) || try_kitty(cp, 0);
                }
                if matches_legacy_modifier_cp(data, &key, modifier) {
                    return true;
                }
                return try_kitty(cp, modifier);
            }

            false
        }
    }
}

/// Get the raw ctrl character for a key.
fn raw_ctrl_char(ch: char) -> Option<char> {
    let lower = ch.to_ascii_lowercase();
    let code = lower as u32;
    if (code >= 97 && code <= 122)
        || lower == '['
        || lower == '\\'
        || lower == ']'
        || lower == '_'
    {
        Some(char::from((code & 0x1f) as u8))
    } else if lower == '-' {
        Some(char::from(31u8)) // same as Ctrl+_
    } else {
        None
    }
}

/// Check if data matches a legacy function key sequence.
fn matches_legacy_fn(data: &[u8], num: u8) -> bool {
    let candidates: &[&[u8]] = match num {
        1 => &[b"\x1bOP", b"\x1b[11~", b"\x1b[[A"],
        2 => &[b"\x1bOQ", b"\x1b[12~", b"\x1b[[B"],
        3 => &[b"\x1bOR", b"\x1b[13~", b"\x1b[[C"],
        4 => &[b"\x1bOS", b"\x1b[14~", b"\x1b[[D"],
        5 => &[b"\x1b[15~", b"\x1b[[E"],
        6 => &[b"\x1b[17~"],
        7 => &[b"\x1b[18~"],
        8 => &[b"\x1b[19~"],
        9 => &[b"\x1b[20~"],
        10 => &[b"\x1b[21~"],
        11 => &[b"\x1b[23~"],
        12 => &[b"\x1b[24~"],
        _ => &[],
    };
    candidates.iter().any(|c| data == *c)
}

/// Check if data matches a legacy unmodified key sequence.
fn matches_legacy_cp(data: &[u8], key: &str) -> bool {
    let seqs: &[&[u8]] = match key {
        "up" => &[b"\x1b[A", b"\x1bOA"],
        "down" => &[b"\x1b[B", b"\x1bOB"],
        "right" => &[b"\x1b[C", b"\x1bOC"],
        "left" => &[b"\x1b[D", b"\x1bOD"],
        "home" => &[b"\x1b[H", b"\x1bOH", b"\x1b[1~", b"\x1b[7~"],
        "end" => &[b"\x1b[F", b"\x1bOF", b"\x1b[4~", b"\x1b[8~"],
        "insert" => &[b"\x1b[2~"],
        "delete" => &[b"\x1b[3~"],
        "pageup" => &[b"\x1b[5~", b"\x1b[[5~"],
        "pagedown" => &[b"\x1b[6~", b"\x1b[[6~"],
        _ => &[],
    };
    seqs.iter().any(|s| data == *s)
}

/// Check if data matches a legacy modified key sequence (shift or ctrl).
fn matches_legacy_modifier_cp(data: &[u8], key: &str, modifier: u8) -> bool {
    if modifier == modifier_bits::SHIFT {
        let seqs: &[&[u8]] = match key {
            "up" => &[b"\x1b[a"],
            "down" => &[b"\x1b[b"],
            "right" => &[b"\x1b[c"],
            "left" => &[b"\x1b[d"],
            "insert" => &[b"\x1b[2$"],
            "delete" => &[b"\x1b[3$"],
            "pageup" => &[b"\x1b[5$"],
            "pagedown" => &[b"\x1b[6$"],
            "home" => &[b"\x1b[7$"],
            "end" => &[b"\x1b[8$"],
            _ => &[],
        };
        return seqs.iter().any(|s| data == *s);
    }
    if modifier == modifier_bits::CTRL {
        let seqs: &[&[u8]] = match key {
            "up" => &[b"\x1bOa"],
            "down" => &[b"\x1bOb"],
            "right" => &[b"\x1bOc"],
            "left" => &[b"\x1bOd"],
            "insert" => &[b"\x1b[2^"],
            "delete" => &[b"\x1b[3^"],
            "pageup" => &[b"\x1b[5^"],
            "pagedown" => &[b"\x1b[6^"],
            "home" => &[b"\x1b[7^"],
            "end" => &[b"\x1b[8^"],
            _ => &[],
        };
        return seqs.iter().any(|s| data == *s);
    }
    false
}

/// Parse a raw byte sequence and return a human-readable key identifier
/// string like `"ctrl+c"`, `"shift+enter"`, etc.
pub fn parse_key(data: &[u8]) -> Option<String> {
    // Try Kitty first
    if let Some(kitty) = parse_kitty_sequence(data) {
        return format_kitty_key(&kitty);
    }

    // Try modifyOtherKeys
    if let Some(mok) = parse_modify_other_keys(data) {
        return format_codepoint_key(mok.codepoint, mok.modifier);
    }

    let s = std::str::from_utf8(data).ok()?;

    // Kitty-active legacy
    if kitty_active() {
        if data == b"\x1b\r" || data == b"\n" {
            return Some("shift+enter".to_string());
        }
    }

    // Legacy sequence map
    if let Some(id) = legacy_sequence_id(s) {
        return Some(id.to_string());
    }

    // Bare ESC
    if data == b"\x1b" {
        return Some("escape".to_string());
    }
    // Ctrl+\
    if data == b"\x1c" {
        return Some("ctrl+\\".to_string());
    }
    // Ctrl+]
    if data == b"\x1d" {
        return Some("ctrl+]".to_string());
    }
    // Ctrl+-
    if data == b"\x1f" {
        return Some("ctrl+-".to_string());
    }
    // Tab
    if data == b"\t" {
        return Some("tab".to_string());
    }
    // Enter
    if data == b"\r" || (!kitty_active() && data == b"\n") || data == b"\x1bOM" {
        return Some("enter".to_string());
    }
    // Ctrl+Space
    if data == b"\x00" {
        return Some("ctrl+space".to_string());
    }
    // Space
    if data == b" " {
        return Some("space".to_string());
    }
    // Backspace
    if data == b"\x7f" || data == b"\x08" {
        return Some("backspace".to_string());
    }
    // Shift+Tab
    if data == b"\x1b[Z" {
        return Some("shift+tab".to_string());
    }
    // Alt+Enter (non-Kitty)
    if !kitty_active() && data == b"\x1b\r" {
        return Some("alt+enter".to_string());
    }
    // Alt+Space (non-Kitty)
    if !kitty_active() && data == b"\x1b " {
        return Some("alt+space".to_string());
    }
    // Alt+Backspace
    if data == b"\x1b\x7f" || data == b"\x1b\x08" {
        return Some("alt+backspace".to_string());
    }
    // Alt+Left (legacy)
    if !kitty_active() && data == b"\x1bB" {
        return Some("alt+left".to_string());
    }
    // Alt+Right (legacy)
    if !kitty_active() && data == b"\x1bF" {
        return Some("alt+right".to_string());
    }
    // Ctrl+Alt+letter / Alt+letter from ESC prefix
    if data.len() == 2 && data[0] == ESC {
        let code = data[1];
        if code >= 1 && code <= 26 {
            return Some(format!("ctrl+alt+{}", char::from(code + 96)));
        }
        if (code >= b'a' && code <= b'z') || (code >= b'0' && code <= b'9') {
            return Some(format!("alt+{}", code as char));
        }
    }
    // Ctrl+Alt+specials
    if data == b"\x1b\x1b" {
        return Some("ctrl+alt+[".to_string());
    }
    if data == b"\x1b\x1c" {
        return Some("ctrl+alt+\\".to_string());
    }
    if data == b"\x1b\x1d" {
        return Some("ctrl+alt+]".to_string());
    }
    if data == b"\x1b\x1f" {
        return Some("ctrl+alt+-".to_string());
    }

    // Remaining legacy CSI sequences (already covered by legacy_sequence_id mostly)
    if data == b"\x1b[A" {
        return Some("up".to_string());
    }
    if data == b"\x1b[B" {
        return Some("down".to_string());
    }
    if data == b"\x1b[C" {
        return Some("right".to_string());
    }
    if data == b"\x1b[D" {
        return Some("left".to_string());
    }
    if data == b"\x1b[H" || data == b"\x1bOH" {
        return Some("home".to_string());
    }
    if data == b"\x1b[F" || data == b"\x1bOF" {
        return Some("end".to_string());
    }
    if data == b"\x1b[3~" {
        return Some("delete".to_string());
    }
    if data == b"\x1b[5~" {
        return Some("pageup".to_string());
    }
    if data == b"\x1b[6~" {
        return Some("pagedown".to_string());
    }

    // Raw Ctrl+letter
    if data.len() == 1 {
        let code = data[0];
        if code >= 1 && code <= 26 {
            return Some(format!("ctrl+{}", char::from(code + 96)));
        }
        if code >= 32 && code <= 126 {
            return Some((code as char).to_string());
        }
    }

    // Multi-byte UTF-8 printable
    if data.len() > 1 {
        if let Ok(s) = std::str::from_utf8(data) {
            if s.chars().all(|c| c >= ' ' as char) {
                return Some(s.to_string());
            }
        }
    }

    None
}

/// Legacy sequence → key ID lookup.
fn legacy_sequence_id(s: &str) -> Option<&'static str> {
    Some(match s {
        "\x1bOA" => "up",
        "\x1bOB" => "down",
        "\x1bOC" => "right",
        "\x1bOD" => "left",
        "\x1bOH" => "home",
        "\x1bOF" => "end",
        "\x1b[E" => "clear",
        "\x1bOE" => "clear",
        "\x1bOe" => "ctrl+clear",
        "\x1b[e" => "shift+clear",
        "\x1b[2~" => "insert",
        "\x1b[2$" => "shift+insert",
        "\x1b[2^" => "ctrl+insert",
        "\x1b[3$" => "shift+delete",
        "\x1b[3^" => "ctrl+delete",
        "\x1b[[5~" => "pageup",
        "\x1b[[6~" => "pagedown",
        "\x1b[a" => "shift+up",
        "\x1b[b" => "shift+down",
        "\x1b[c" => "shift+right",
        "\x1b[d" => "shift+left",
        "\x1bOa" => "ctrl+up",
        "\x1bOb" => "ctrl+down",
        "\x1bOc" => "ctrl+right",
        "\x1bOd" => "ctrl+left",
        "\x1b[5$" => "shift+pageup",
        "\x1b[6$" => "shift+pagedown",
        "\x1b[7$" => "shift+home",
        "\x1b[8$" => "shift+end",
        "\x1b[5^" => "ctrl+pageup",
        "\x1b[6^" => "ctrl+pagedown",
        "\x1b[7^" => "ctrl+home",
        "\x1b[8^" => "ctrl+end",
        "\x1bOP" => "f1",
        "\x1bOQ" => "f2",
        "\x1bOR" => "f3",
        "\x1bOS" => "f4",
        "\x1b[11~" => "f1",
        "\x1b[12~" => "f2",
        "\x1b[13~" => "f3",
        "\x1b[14~" => "f4",
        "\x1b[[A" => "f1",
        "\x1b[[B" => "f2",
        "\x1b[[C" => "f3",
        "\x1b[[D" => "f4",
        "\x1b[[E" => "f5",
        "\x1b[15~" => "f5",
        "\x1b[17~" => "f6",
        "\x1b[18~" => "f7",
        "\x1b[19~" => "f8",
        "\x1b[20~" => "f9",
        "\x1b[21~" => "f10",
        "\x1b[23~" => "f11",
        "\x1b[24~" => "f12",
        "\x1bb" => "alt+left",
        "\x1bf" => "alt+right",
        "\x1bp" => "alt+up",
        "\x1bn" => "alt+down",
        _ => return None,
    })
}

/// Format a Kitty-parsed key into a human-readable identifier.
fn format_kitty_key(kitty: &ParsedKitty) -> Option<String> {
    format_codepoint_key_impl(
        normalize_kitty_functional_codepoint(kitty.codepoint),
        kitty.modifier,
        kitty.base_layout_key,
    )
}

/// Format a codepoint + modifier into a human-readable key identifier.
fn format_codepoint_key(cp: i64, modifier: u8) -> Option<String> {
    format_codepoint_key_impl(cp, modifier, None)
}

fn format_codepoint_key_impl(cp: i64, modifier: u8, base_layout_key: Option<i64>) -> Option<String> {
    let normalized = normalize_kitty_functional_codepoint(cp);
    let identity = normalize_shifted_letter(normalized, modifier);

    let is_latin = identity >= 97 && identity <= 122;
    let is_digit = identity >= 48 && identity <= 57;
    let is_symbol = is_symbol_cp(identity);
    let effective = if is_latin || is_digit || is_symbol {
        identity
    } else {
        base_layout_key.unwrap_or(identity)
    };

    let key_name = codepoint_to_key_name(effective)?;

    let eff_mod = effective_modifier(modifier);
    let supported = modifier_bits::SHIFT
        | modifier_bits::CTRL
        | modifier_bits::ALT
        | modifier_bits::SUPER;
    if (eff_mod & !supported) != 0 {
        return None;
    }

    let mut parts: Vec<&str> = Vec::new();
    if (eff_mod & modifier_bits::SHIFT) != 0 {
        parts.push("shift");
    }
    if (eff_mod & modifier_bits::CTRL) != 0 {
        parts.push("ctrl");
    }
    if (eff_mod & modifier_bits::ALT) != 0 {
        parts.push("alt");
    }
    if (eff_mod & modifier_bits::SUPER) != 0 {
        parts.push("super");
    }

    if parts.is_empty() {
        Some(key_name)
    } else {
        let mut result = parts.join("+");
        result.push('+');
        result.push_str(&key_name);
        Some(result)
    }
}

/// Convert a codepoint to a human-readable key name.
fn codepoint_to_key_name(cp: i64) -> Option<String> {
    match cp {
        codepoints::ESCAPE => Some("escape".to_string()),
        codepoints::TAB => Some("tab".to_string()),
        codepoints::ENTER | codepoints::KP_ENTER => Some("enter".to_string()),
        codepoints::SPACE => Some("space".to_string()),
        codepoints::BACKSPACE => Some("backspace".to_string()),
        codepoints::DELETE => Some("delete".to_string()),
        codepoints::INSERT => Some("insert".to_string()),
        codepoints::HOME => Some("home".to_string()),
        codepoints::END => Some("end".to_string()),
        codepoints::PAGE_UP => Some("pageup".to_string()),
        codepoints::PAGE_DOWN => Some("pagedown".to_string()),
        codepoints::UP => Some("up".to_string()),
        codepoints::DOWN => Some("down".to_string()),
        codepoints::LEFT => Some("left".to_string()),
        codepoints::RIGHT => Some("right".to_string()),
        cp if cp >= 32 && cp <= 126 => Some((cp as u8 as char).to_string()),
        cp => char::from_u32(cp as u32).map(|c| c.to_string()),
    }
}

// ---------------------------------------------------------------------------
// Tests
// ---------------------------------------------------------------------------

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_parse_plain_char() {
        let event = parse_event(b"a").unwrap();
        match event {
            Event::Key(KeyEvent {
                code: KeyCode::Char('a'),
                modifiers,
                ..
            }) => {
                assert!(!modifiers.ctrl);
                assert!(!modifiers.alt);
                assert!(!modifiers.shift);
            }
            _ => panic!("expected Char('a'), got {:?}", event),
        }
    }

    #[test]
    fn test_parse_escape() {
        let event = parse_event(b"\x1b").unwrap();
        match event {
            Event::Key(KeyEvent {
                code: KeyCode::Escape,
                ..
            }) => {}
            _ => panic!("expected Escape"),
        }
    }

    #[test]
    fn test_parse_enter() {
        let event = parse_event(b"\r").unwrap();
        match event {
            Event::Key(KeyEvent {
                code: KeyCode::Enter,
                ..
            }) => {}
            _ => panic!("expected Enter"),
        }
    }

    #[test]
    fn test_parse_tab() {
        let event = parse_event(b"\t").unwrap();
        match event {
            Event::Key(KeyEvent {
                code: KeyCode::Tab,
                ..
            }) => {}
            _ => panic!("expected Tab"),
        }
    }

    #[test]
    fn test_parse_backspace() {
        let event = parse_event(b"\x7f").unwrap();
        match event {
            Event::Key(KeyEvent {
                code: KeyCode::Backspace,
                ..
            }) => {}
            _ => panic!("expected Backspace"),
        }
    }

    #[test]
    fn test_parse_ctrl_c() {
        let event = parse_event(b"\x03").unwrap();
        match event {
            Event::Key(KeyEvent {
                code: KeyCode::Char('c'),
                modifiers,
            }) => {
                assert!(modifiers.ctrl);
            }
            _ => panic!("expected Ctrl+C"),
        }
    }

    #[test]
    fn test_parse_ctrl_space() {
        let event = parse_event(b"\x00").unwrap();
        match event {
            Event::Key(KeyEvent {
                code: KeyCode::Char(' '),
                modifiers,
            }) => {
                assert!(modifiers.ctrl);
            }
            _ => panic!("expected Ctrl+Space"),
        }
    }

    #[test]
    fn test_parse_arrow_up_csi() {
        let event = parse_event(b"\x1b[A").unwrap();
        match event {
            Event::Key(KeyEvent {
                code: KeyCode::Up,
                modifiers,
            }) => {
                assert!(!modifiers.shift && !modifiers.ctrl && !modifiers.alt);
            }
            _ => panic!("expected Up"),
        }
    }

    #[test]
    fn test_parse_arrow_up_ss3() {
        let event = parse_event(b"\x1bOA").unwrap();
        match event {
            Event::Key(KeyEvent {
                code: KeyCode::Up, ..
            }) => {}
            _ => panic!("expected Up"),
        }
    }

    #[test]
    fn test_parse_f1_ss3() {
        let event = parse_event(b"\x1bOP").unwrap();
        match event {
            Event::Key(KeyEvent {
                code: KeyCode::F(1), ..
            }) => {}
            _ => panic!("expected F(1)"),
        }
    }

    #[test]
    fn test_parse_f5_csi() {
        let event = parse_event(b"\x1b[15~").unwrap();
        match event {
            Event::Key(KeyEvent {
                code: KeyCode::F(5), ..
            }) => {}
            _ => panic!("expected F(5)"),
        }
    }

    #[test]
    fn test_parse_home() {
        let event = parse_event(b"\x1b[H").unwrap();
        match event {
            Event::Key(KeyEvent {
                code: KeyCode::Home, ..
            }) => {}
            _ => panic!("expected Home"),
        }
    }

    #[test]
    fn test_parse_end() {
        let event = parse_event(b"\x1b[F").unwrap();
        match event {
            Event::Key(KeyEvent {
                code: KeyCode::End, ..
            }) => {}
            _ => panic!("expected End"),
        }
    }

    #[test]
    fn test_parse_insert() {
        let event = parse_event(b"\x1b[2~").unwrap();
        match event {
            Event::Key(KeyEvent {
                code: KeyCode::Insert, ..
            }) => {}
            _ => panic!("expected Insert"),
        }
    }

    #[test]
    fn test_parse_delete() {
        let event = parse_event(b"\x1b[3~").unwrap();
        match event {
            Event::Key(KeyEvent {
                code: KeyCode::Delete, ..
            }) => {}
            _ => panic!("expected Delete"),
        }
    }

    #[test]
    fn test_parse_pageup() {
        let event = parse_event(b"\x1b[5~").unwrap();
        match event {
            Event::Key(KeyEvent {
                code: KeyCode::PageUp, ..
            }) => {}
            _ => panic!("expected PageUp"),
        }
    }

    #[test]
    fn test_parse_pagedown() {
        let event = parse_event(b"\x1b[6~").unwrap();
        match event {
            Event::Key(KeyEvent {
                code: KeyCode::PageDown, ..
            }) => {}
            _ => panic!("expected PageDown"),
        }
    }

    #[test]
    fn test_parse_shift_tab() {
        let event = parse_event(b"\x1b[Z").unwrap();
        match event {
            Event::Key(KeyEvent {
                code: KeyCode::Tab,
                modifiers,
            }) => {
                assert!(modifiers.shift);
            }
            _ => panic!("expected Shift+Tab"),
        }
    }

    #[test]
    fn test_parse_alt_letter() {
        let event = parse_event(b"\x1ba").unwrap();
        match event {
            Event::Key(KeyEvent {
                code: KeyCode::Char('a'),
                modifiers,
            }) => {
                assert!(modifiers.alt);
                assert!(!modifiers.ctrl);
            }
            _ => panic!("expected Alt+a"),
        }
    }

    #[test]
    fn test_parse_alt_backspace() {
        let event = parse_event(b"\x1b\x7f").unwrap();
        match event {
            Event::Key(KeyEvent {
                code: KeyCode::Backspace,
                modifiers,
            }) => {
                assert!(modifiers.alt);
            }
            _ => panic!("expected Alt+Backspace"),
        }
    }

    #[test]
    fn test_parse_focus_gained() {
        let event = parse_event(b"\x1b[I").unwrap();
        assert_eq!(event, Event::FocusGained);
    }

    #[test]
    fn test_parse_focus_lost() {
        let event = parse_event(b"\x1b[O").unwrap();
        assert_eq!(event, Event::FocusLost);
    }

    #[test]
    fn test_parse_sgr_mouse_press() {
        let event = parse_event(b"\x1b[<0;10;5M").unwrap();
        match event {
            Event::Mouse(MouseEvent {
                kind: MouseEventKind::Press,
                button: MouseButton::Left,
                row: 4,
                col: 9,
            }) => {}
            _ => panic!("expected left mouse press at (9,4), got {:?}", event),
        }
    }

    #[test]
    fn test_parse_sgr_mouse_release() {
        let event = parse_event(b"\x1b[<0;10;5m").unwrap();
        match event {
            Event::Mouse(MouseEvent {
                kind: MouseEventKind::Release,
                button: MouseButton::Left,
                row: 4,
                col: 9,
            }) => {}
            _ => panic!("expected left mouse release"),
        }
    }

    #[test]
    fn test_parse_sgr_mouse_scroll() {
        let event = parse_event(b"\x1b[<64;10;5M").unwrap();
        match event {
            Event::Mouse(MouseEvent {
                kind: MouseEventKind::ScrollUp,
                ..
            }) => {}
            _ => panic!("expected scroll up"),
        }
    }

    #[test]
    fn test_parse_x10_mouse() {
        let event = parse_event(b"\x1b[M \x21\x21").unwrap();
        match event {
            Event::Mouse(MouseEvent {
                kind: MouseEventKind::Press,
                button: MouseButton::Left,
                ..
            }) => {}
            _ => panic!("expected X10 mouse press"),
        }
    }

    #[test]
    fn test_parse_kitty_arrow_up() {
        // Kitty: ESC [ -1 u (arrow up = codepoint -1)
        // Actually Kitty uses ESC[1;<mod>A for arrows, tested below
        let event = parse_event(b"\x1b[1;1A").unwrap();
        match event {
            Event::Key(KeyEvent {
                code: KeyCode::Up, ..
            }) => {}
            _ => panic!("expected Up from Kitty"),
        }
    }

    #[test]
    fn test_parse_kitty_ctrl_c() {
        // Kitty CSI-u: ESC [ 99 ; 5 u  (codepoint 99='c', modifier 5=ctrl+1)
        let event = parse_event(b"\x1b[99;5u").unwrap();
        match event {
            Event::Key(KeyEvent {
                code: KeyCode::Char('c'),
                modifiers,
            }) => {
                assert!(modifiers.ctrl);
            }
            _ => panic!("expected Ctrl+c from Kitty"),
        }
    }

    #[test]
    fn test_parse_modify_other_keys_ctrl_c() {
        // xterm modifyOtherKeys: ESC [ 27 ; 5 ; 99 ~
        let event = parse_event(b"\x1b[27;5;99~").unwrap();
        match event {
            Event::Key(KeyEvent {
                code: KeyCode::Char('c'),
                modifiers,
            }) => {
                assert!(modifiers.ctrl);
            }
            _ => panic!("expected Ctrl+c from modifyOtherKeys"),
        }
    }

    #[test]
    fn test_decode_printable_kitty() {
        // Kitty CSI-u for plain 'a': ESC [ 97 u
        let ch = decode_kitty_printable(b"\x1b[97u").unwrap();
        assert_eq!(ch, 'a');
    }

    #[test]
    fn test_decode_printable_rejects_ctrl() {
        // Kitty CSI-u with ctrl modifier should not decode as printable
        assert!(decode_kitty_printable(b"\x1b[97;5u").is_none());
    }

    #[test]
    fn test_matches_key_escape() {
        assert!(matches_key(b"\x1b", "escape"));
        assert!(matches_key(b"\x1b", "esc"));
        assert!(!matches_key(b"\x1b", "ctrl+escape"));
    }

    #[test]
    fn test_matches_key_ctrl_c() {
        assert!(matches_key(b"\x03", "ctrl+c"));
        assert!(!matches_key(b"\x03", "ctrl+d"));
    }

    #[test]
    fn test_matches_key_enter() {
        assert!(matches_key(b"\r", "enter"));
        assert!(matches_key(b"\r", "return"));
    }

    #[test]
    fn test_matches_key_shift_tab() {
        assert!(matches_key(b"\x1b[Z", "shift+tab"));
    }

    #[test]
    fn test_matches_key_alt_a() {
        assert!(matches_key(b"\x1ba", "alt+a"));
    }

    #[test]
    fn test_matches_key_function() {
        assert!(matches_key(b"\x1bOP", "f1"));
        assert!(matches_key(b"\x1b[15~", "f5"));
    }

    #[test]
    fn test_parse_key_string() {
        assert_eq!(parse_key(b"\x1b"), Some("escape".to_string()));
        assert_eq!(parse_key(b"\r"), Some("enter".to_string()));
        assert_eq!(parse_key(b"\t"), Some("tab".to_string()));
        assert_eq!(parse_key(b"a"), Some("a".to_string()));
        assert_eq!(parse_key(b"\x03"), Some("ctrl+c".to_string()));
        assert_eq!(parse_key(b"\x1b[Z"), Some("shift+tab".to_string()));
        assert_eq!(parse_key(b"\x1b[A"), Some("up".to_string()));
        assert_eq!(parse_key(b"\x1b[B"), Some("down".to_string()));
        assert_eq!(parse_key(b"\x1b[C"), Some("right".to_string()));
        assert_eq!(parse_key(b"\x1b[D"), Some("left".to_string()));
    }

    #[test]
    fn test_is_key_release() {
        assert!(is_key_release(b"\x1b[97;1:3u"));
        assert!(!is_key_release(b"\x1b[97;1:1u"));
        assert!(!is_key_release(b"a"));
    }

    #[test]
    fn test_is_key_repeat() {
        assert!(is_key_repeat(b"\x1b[97;1:2u"));
        assert!(!is_key_repeat(b"\x1b[97;1:1u"));
    }

    #[test]
    fn test_utf8_multibyte() {
        // 'é' in UTF-8 is 0xC3 0xA9
        let event = parse_event("é".as_bytes()).unwrap();
        match event {
            Event::Key(KeyEvent {
                code: KeyCode::Char('é'),
                ..
            }) => {}
            _ => panic!("expected Char('é')"),
        }
    }
}