psmux 3.3.2

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

use chrono::Local;
use crossterm::event::{Event, KeyCode, KeyModifiers, KeyEventKind};
use ratatui::prelude::*;
use ratatui::widgets::*;

use crate::layout::LayoutJson;
use crate::help;
use crate::util::{WinTree, base64_encode, quote_arg};
use crate::session::read_session_key;
use crate::rendering::{dim_predictions_enabled, map_color, dim_color, centered_rect, fix_border_intersections};
use crate::style::parse_tmux_style_components;
use crate::config::{parse_key_string, normalize_key_for_binding};
use crate::copy_mode::{copy_to_system_clipboard, read_from_system_clipboard};
use crate::debug_log::{client_log, client_log_enabled, input_log, input_log_enabled};
use crate::layout::RowRunsJson;
use crate::tree::split_with_gaps;

/// Build a send-key name with modifier prefix (e.g. "C-Left", "S-Right", "C-S-Up").
fn modified_key_name(base: &str, mods: KeyModifiers) -> String {
    let mut prefix = String::new();
    if mods.contains(KeyModifiers::CONTROL) { prefix.push_str("C-"); }
    if mods.contains(KeyModifiers::ALT) { prefix.push_str("M-"); }
    if mods.contains(KeyModifiers::SHIFT) { prefix.push_str("S-"); }
    if prefix.is_empty() {
        base.to_lowercase()
    } else {
        format!("{}{}", prefix, base)
    }
}

/// Extract selected text from the layout tree given absolute terminal coordinates.
/// Computes pane areas via the same Layout splitting render_json uses, then reads
/// characters from the run-length-encoded rows_v2 data.
fn extract_selection_text(
    layout: &LayoutJson,
    term_width: u16,
    content_height: u16, // excluding status bar
    start: (u16, u16),   // (col, row)
    end: (u16, u16),
) -> String {
    // Normalise so (r0,c0) <= (r1,c1) in reading order
    let (r0, c0, r1, c1) = if (start.1, start.0) <= (end.1, end.0) {
        (start.1, start.0, end.1, end.0)
    } else {
        (end.1, end.0, start.1, start.0)
    };

    // Collect leaf panes with their inner areas and content
    struct PaneLeaf<'a> {
        inner: Rect,
        rows_v2: &'a [RowRunsJson],
    }

    fn collect_leaves<'a>(node: &'a LayoutJson, area: Rect, out: &mut Vec<PaneLeaf<'a>>) {
        match node {
            LayoutJson::Leaf { rows_v2, .. } => {
                // No borders — content fills entire area (tmux-style)
                out.push(PaneLeaf { inner: area, rows_v2 });
            }
            LayoutJson::Split { kind, sizes, children } => {
                let effective_sizes: Vec<u16> = if sizes.len() == children.len() {
                    sizes.clone()
                } else {
                    vec![(100 / children.len().max(1)) as u16; children.len()]
                };
                let is_horizontal = kind == "Horizontal";
                let rects = split_with_gaps(is_horizontal, &effective_sizes, area);
                for (i, child) in children.iter().enumerate() {
                    if i < rects.len() {
                        collect_leaves(child, rects[i], out);
                    }
                }
            }
        }
    }

    let content_area = Rect { x: 0, y: 0, width: term_width, height: content_height };
    let mut leaves: Vec<PaneLeaf> = Vec::new();
    collect_leaves(layout, content_area, &mut leaves);

    // Helper: get character at a local column position within a row's runs
    fn char_at_col(runs: &[crate::layout::CellRunJson], local_col: usize) -> char {
        let mut cursor = 0usize;
        for run in runs {
            let run_width = run.width.max(1) as usize;
            if local_col >= cursor && local_col < cursor + run_width {
                let offset = local_col - cursor;
                // Run text may be shorter than run_width (e.g. single char repeated)
                // or multi-char for wide chars. Pick the nth char if available.
                return run.text.chars().nth(offset).unwrap_or(' ');
            }
            cursor += run_width;
        }
        ' '
    }

    let mut result = String::new();
    for row in r0..=r1 {
        let col_start = if row == r0 { c0 } else { 0 };
        let col_end = if row == r1 { c1 } else { term_width.saturating_sub(1) };

        let mut line = String::new();
        for col in col_start..=col_end {
            let mut ch = ' ';
            for leaf in &leaves {
                let inner = &leaf.inner;
                if col >= inner.x && col < inner.x + inner.width
                    && row >= inner.y && row < inner.y + inner.height
                {
                    let local_row = (row - inner.y) as usize;
                    let local_col = (col - inner.x) as usize;
                    if local_row < leaf.rows_v2.len() {
                        ch = char_at_col(&leaf.rows_v2[local_row].runs, local_col);
                    }
                    break;
                }
            }
            line.push(ch);
        }
        // Trim trailing whitespace per line
        let trimmed = line.trim_end();
        result.push_str(trimmed);
        if row < r1 {
            result.push('\n');
        }
    }

    result
}

/// Check if the active pane is running a fullscreen TUI app (alternate screen).
/// Used to decide whether right-click should paste (shell prompt) or forward
/// as a mouse event to the child (TUI app like htop, Claude Code, etc.).
fn active_pane_in_alt_screen(layout: &LayoutJson) -> bool {
    match layout {
        LayoutJson::Leaf { active, alternate_screen, .. } => *active && *alternate_screen,
        LayoutJson::Split { children, .. } => children.iter().any(|c| active_pane_in_alt_screen(c)),
    }
}

/// Check if the active pane is in server-side copy mode.
/// When true, the client should NOT start its own text selection —
/// the server handles cursor positioning and selection in copy mode.
fn active_pane_in_copy_mode(layout: &LayoutJson) -> bool {
    match layout {
        LayoutJson::Leaf { active, copy_mode, .. } => *active && *copy_mode,
        LayoutJson::Split { children, .. } => children.iter().any(|c| active_pane_in_copy_mode(c)),
    }
}

/// Check if screen coordinates (x, y) fall on a separator line in the layout.
/// Used to distinguish border-drag (resize) from text selection on left-click.
fn is_on_separator(layout: &LayoutJson, area: Rect, x: u16, y: u16) -> bool {
    match layout {
        LayoutJson::Leaf { .. } => false,
        LayoutJson::Split { kind, sizes, children } => {
            let effective_sizes: Vec<u16> = if sizes.len() == children.len() {
                sizes.clone()
            } else {
                vec![(100 / children.len().max(1)) as u16; children.len()]
            };
            let is_horizontal = kind == "Horizontal";
            let rects = split_with_gaps(is_horizontal, &effective_sizes, area);

            // Check if (x, y) is on any separator between children
            for i in 0..children.len().saturating_sub(1) {
                if i >= rects.len() { break; }
                if is_horizontal {
                    let sep_x = rects[i].x + rects[i].width;
                    if x == sep_x && y >= area.y && y < area.y + area.height {
                        return true;
                    }
                } else {
                    let sep_y = rects[i].y + rects[i].height;
                    if y == sep_y && x >= area.x && x < area.x + area.width {
                        return true;
                    }
                }
            }

            // Recurse into children
            for (i, child) in children.iter().enumerate() {
                if i < rects.len() && is_on_separator(child, rects[i], x, y) {
                    return true;
                }
            }

            false
        }
    }
}

/// Collect all leaf pane IDs and their absolute rects from a LayoutJson tree.
fn collect_pane_rects(node: &LayoutJson, area: Rect, out: &mut Vec<(usize, Rect)>) {
    match node {
        LayoutJson::Leaf { id, .. } => {
            out.push((*id, area));
        }
        LayoutJson::Split { kind, sizes, children } => {
            let effective_sizes: Vec<u16> = if sizes.len() == children.len() {
                sizes.clone()
            } else {
                vec![(100 / children.len().max(1)) as u16; children.len()]
            };
            let is_horizontal = kind == "Horizontal";
            let rects = split_with_gaps(is_horizontal, &effective_sizes, area);
            for (i, child) in children.iter().enumerate() {
                if i < rects.len() {
                    collect_pane_rects(child, rects[i], out);
                }
            }
        }
    }
}

/// Collect all split border positions from a LayoutJson tree.
/// Returns: (tree_path_to_parent, kind, child_index, border_pixel_pos, total_pixels, sizes_snapshot)
fn collect_layout_borders(
    node: &LayoutJson,
    area: Rect,
    path: &mut Vec<usize>,
    out: &mut Vec<(Vec<usize>, String, usize, u16, u16, Vec<u16>, Rect)>,
) {
    if let LayoutJson::Split { kind, sizes, children } = node {
        let effective_sizes: Vec<u16> = if sizes.len() == children.len() {
            sizes.clone()
        } else {
            vec![(100 / children.len().max(1)) as u16; children.len()]
        };
        let is_horizontal = kind == "Horizontal";
        let rects = split_with_gaps(is_horizontal, &effective_sizes, area);
        let total_px = if is_horizontal { area.width } else { area.height };
        for i in 0..children.len().saturating_sub(1) {
            if i < rects.len() {
                let pos = if is_horizontal {
                    rects[i].x + rects[i].width
                } else {
                    rects[i].y + rects[i].height
                };
                out.push((path.clone(), kind.clone(), i, pos, total_px, effective_sizes.clone(), area));
            }
        }
        for (i, child) in children.iter().enumerate() {
            if i < rects.len() {
                path.push(i);
                collect_layout_borders(child, rects[i], path, out);
                path.pop();
            }
        }
    }
}

/// Check if any leaf in a LayoutJson subtree is the active pane.
/// Compute the rectangle of the active pane by searching the LayoutJson tree.
fn compute_active_rect_json(node: &LayoutJson, area: Rect) -> Option<Rect> {
    match node {
        LayoutJson::Leaf { active, .. } => {
            if *active { Some(area) } else { None }
        }
        LayoutJson::Split { kind, sizes, children } => {
            let effective_sizes: Vec<u16> = if sizes.len() == children.len() {
                sizes.clone()
            } else {
                vec![(100 / children.len().max(1)) as u16; children.len()]
            };
            let is_horizontal = kind == "Horizontal";
            let rects = split_with_gaps(is_horizontal, &effective_sizes, area);
            for (i, child) in children.iter().enumerate() {
                if i < rects.len() {
                    if let Some(r) = compute_active_rect_json(child, rects[i]) {
                        return Some(r);
                    }
                }
            }
            None
        }
    }
}

/// Client-side border drag state — tracks an in-progress separator resize.
struct ClientDragState {
    path: Vec<usize>,
    kind: String,
    index: usize,
    start_pos: u16,
    initial_sizes: Vec<u16>,
    total_pixels: u16,
}

pub fn run_remote(terminal: &mut Terminal<CrosstermBackend<crate::platform::PsmuxWriter>>, input: &crate::ssh_input::InputSource) -> io::Result<()> {
    let name = env::var("PSMUX_SESSION_NAME").unwrap_or_else(|_| "default".to_string());
    let home = env::var("USERPROFILE").or_else(|_| env::var("HOME")).unwrap_or_default();
    let path = format!("{}\\.psmux\\{}.port", home, name);
    let port = std::fs::read_to_string(&path).ok().and_then(|s| s.trim().parse::<u16>().ok())
        .ok_or_else(|| io::Error::new(io::ErrorKind::Other, format!("can't find session '{}' (no server running)", name)))?;
    let addr = format!("127.0.0.1:{}", port);
    let session_key = read_session_key(&name).unwrap_or_default();
    let last_path = format!("{}\\.psmux\\last_session", home);
    if !crate::session::is_warm_session(&name) {
        let _ = std::fs::write(&last_path, &name);
    }

    // ── Open persistent TCP connection ───────────────────────────────────
    let stream = std::net::TcpStream::connect(&addr)?;
    stream.set_nodelay(true)?; // Disable Nagle's algorithm for low latency
    let mut writer = stream.try_clone()?;
    writer.set_nodelay(true)?;
    let mut reader = BufReader::new(stream);

    // AUTH handshake
    let _ = writer.write_all(format!("AUTH {}\n", session_key).as_bytes());
    let _ = writer.flush();
    let mut auth_line = String::new();
    reader.read_line(&mut auth_line)?;
    if !auth_line.trim().starts_with("OK") {
        return Err(io::Error::new(io::ErrorKind::PermissionDenied, "auth failed"));
    }

    // Enter persistent mode + attach
    let _ = writer.write_all(b"PERSISTENT\n");
    let _ = writer.write_all(b"client-attach\n");
    let _ = writer.flush();

    // Spawn a dedicated reader thread so the event loop never blocks on I/O.
    // The reader thread reads lines from the server and sends them via channel.
    // Use a 2-second read timeout so the thread unblocks periodically.
    // Without this, process::exit(0) on the server side may not deliver a
    // TCP RST promptly on Windows, leaving read_line() blocked forever and
    // the client stuck after the last pane exits.
    let _ = reader.get_ref().set_read_timeout(Some(std::time::Duration::from_secs(2)));
    let (frame_tx, frame_rx) = std::sync::mpsc::channel::<String>();
    std::thread::spawn(move || {
        let mut reader = reader;
        let mut buf = String::with_capacity(64 * 1024);
        loop {
            buf.clear();
            loop {
                match reader.read_line(&mut buf) {
                    Ok(0) => return, // EOF — server closed connection
                    Ok(_) => break,  // Got a complete line, send it
                    Err(ref e) if e.kind() == std::io::ErrorKind::TimedOut
                        || e.kind() == std::io::ErrorKind::WouldBlock =>
                    {
                        // Timeout: buf may contain a partial line from a
                        // previous fill_buf.  Do NOT clear it — read_line
                        // will resume appending on the next call.  This
                        // keeps the protocol stream intact.
                        continue;
                    }
                    Err(_) => return, // Real error — connection died
                }
            }
            let line = std::mem::take(&mut buf);
            buf = String::with_capacity(64 * 1024);
            if frame_tx.send(line).is_err() { return; }
        }
    });

    let mut quit = false;
    let mut prefix_armed = false;
    let mut prefix_armed_at = Instant::now();
    let mut prefix_repeating = false;
    let mut repeat_time_ms: u64 = 500;
    let mut renaming = false;
    let mut session_renaming = false;
    let mut rename_buf = String::new();
    let mut pane_renaming = false;
    let mut pane_title_buf = String::new();
    let mut command_input = false;
    let mut command_buf = String::new();
    let mut command_cursor: usize = 0;
    let mut command_history: Vec<String> = Vec::new();
    let mut command_history_idx: usize = 0;
    let mut window_idx_input = false;
    let mut window_idx_buf = String::new();

    let mut tree_chooser = false;
    let mut tree_entries: Vec<(bool, usize, usize, String, String)> = Vec::new();  // (is_win, id, sub_id, label, session_name)
    let mut tree_selected: usize = 0;
    let mut tree_scroll: usize = 0;
    let mut session_chooser = false;
    let mut session_entries: Vec<(String, String)> = Vec::new();
    let mut session_selected: usize = 0;
    let mut confirm_cmd: Option<String> = None;  // pending kill confirmation
    let current_session = name.clone();
    let mut last_sent_size: (u16, u16) = (0, 0);
    let mut last_status_lines: u16 = 1; // track server's status_lines for correct client-size height
    let mut last_dump_time = Instant::now() - Duration::from_millis(250);
    let mut force_dump = true;
    let mut last_tree: Vec<WinTree> = Vec::new();
    // Default prefix is Ctrl+B, updated dynamically from server config
    let mut prefix_key: (KeyCode, KeyModifiers) = (KeyCode::Char('b'), KeyModifiers::CONTROL);
    // Precompute the raw control character for the default prefix
    let mut prefix_raw_char: Option<char> = Some('\x02');
    // Secondary prefix key (prefix2), default None
    let mut prefix2_key: Option<(KeyCode, KeyModifiers)> = None;
    let mut prefix2_raw_char: Option<char> = None;
    // Status bar style from server (parsed from tmux status-style format)
    let mut status_fg: Color = Color::Black;
    let mut status_bg: Color = Color::Green;
    let mut status_bold: bool = false;
    let mut custom_status_left: Option<String> = None;
    let mut custom_status_right: Option<String> = None;
    let mut pane_border_fg: Color = Color::DarkGray;
    let mut pane_active_border_fg: Color = Color::Green;
    let mut pane_border_hover_fg: Color = Color::Yellow;
    let mut win_status_fmt: String = "#I:#W#{?window_flags,#{window_flags}, }".to_string();
    let mut win_status_current_fmt: String = "#I:#W#{?window_flags,#{window_flags}, }".to_string();
    let mut win_status_sep: String = " ".to_string();
    let mut win_status_style: Option<(Option<Color>, Option<Color>, bool)> = None;
    let mut win_status_current_style: Option<(Option<Color>, Option<Color>, bool)> = None;
    let mut mode_style_str: String = "bg=yellow,fg=black".to_string();
    let mut status_position_str: String = "bottom".to_string();
    let mut status_justify_str: String = "left".to_string();
    // Synced bindings from server (updated each frame from DumpState)
    let mut synced_bindings: Vec<BindingEntry> = Vec::new();

    // ── Windows paste detection state ──────────────────────────────────
    // On Windows, Ctrl+V paste injects individual Key events BEFORE the
    // Ctrl+V Release event arrives (~184ms later).  We buffer ALL printable
    // chars for a short 20ms window.  If ≥3 chars arrive within 20ms, it's
    // almost certainly a paste — hold the buffer until Ctrl+V Release confirms
    // (up to 300ms), then send as a single bracketed paste (send-paste).
    // If <3 chars arrive within 20ms, flush them as normal send-text.
    // Pending chars being examined for paste detection.
    #[cfg(windows)]
    let mut paste_pend: String = String::new();
    // When the first char of the current pending group arrived.
    #[cfg(windows)]
    let mut paste_pend_start: Option<Instant> = None;
    // True once the 20ms window showed ≥3 chars — waiting for Ctrl+V Release.
    #[cfg(windows)]
    let mut paste_stage2: bool = false;
    // Set to true when Ctrl+V Release is seen — confirms the burst was a paste.
    #[cfg(windows)]
    let mut paste_confirmed: bool = false;
    // Buffer size at previous stage2 timeout check — for growth detection.
    #[cfg(windows)]
    let mut paste_stage2_last_len: usize = 0;
    // Suppression window: after right-click copy, discard text key events
    // for a short period to prevent VS Code ConPTY duplicate injection.
    #[cfg(windows)]
    let mut paste_suppress_until: Option<Instant> = None;

    // Track whether a modified Enter Press was already handled this keypress
    // cycle.  WezTerm sends Shift+Enter as Release-only (no Press), so we
    // accept Release events for modified Enter and promote them to Press.
    // Windows Terminal, however, generates a real Press followed by a phantom
    // Release ~80ms later.  This flag suppresses that phantom duplicate.
    #[cfg(windows)]
    let mut modified_enter_press_handled: bool = false;

    // list-keys overlay state (C-b ?)
    let mut keys_viewer = false;
    let mut keys_viewer_lines: Vec<String> = Vec::new();
    let mut keys_viewer_scroll: usize = 0;

    // ── Server-side overlay state (updated each frame) ──
    // Initial values are overwritten on the first render frame; defaults
    // are kept here for safety in case the first state message is delayed.
    #[allow(unused_assignments)]
    let mut srv_popup_active = false;
    #[allow(unused_assignments)]
    let mut srv_popup_command = String::new();
    #[allow(unused_assignments)]
    let mut srv_popup_width: u16 = 80;
    #[allow(unused_assignments)]
    let mut srv_popup_height: u16 = 24;
    #[allow(unused_assignments)]
    let mut srv_popup_lines: Vec<String> = Vec::new();
    #[allow(unused_assignments)]
    let mut srv_popup_rows: Vec<crate::layout::RowRunsJson> = Vec::new();
    #[allow(unused_assignments)]
    let mut srv_popup_has_pty = false;
    let mut srv_popup_scroll: u16 = 0;
    #[allow(unused_assignments)]
    let mut srv_confirm_active = false;
    #[allow(unused_assignments)]
    let mut srv_confirm_prompt = String::new();
    #[allow(unused_assignments)]
    let mut srv_menu_active = false;
    #[allow(unused_assignments)]
    let mut srv_menu_title = String::new();
    #[allow(unused_assignments)]
    let mut srv_menu_selected: usize = 0;
    #[allow(unused_assignments)]
    let mut srv_menu_items: Vec<ServerMenuItem> = Vec::new();
    #[allow(unused_assignments)]
    let mut srv_display_panes = false;
    #[allow(unused_assignments)]
    let mut srv_pane_base_index: usize = 0;
    #[allow(unused_assignments)]
    let mut clock_active = false;
    #[allow(unused_assignments)]
    let mut clock_colour_str: Option<String> = None;

    // ── Customize-mode overlay state ──
    #[allow(unused_assignments)]
    let mut srv_customize_active = false;
    #[allow(unused_assignments)]
    let mut srv_customize_selected: usize = 0;
    #[allow(unused_assignments)]
    let mut srv_customize_scroll: usize = 0;
    #[allow(unused_assignments)]
    let mut srv_customize_editing = false;
    #[allow(unused_assignments)]
    let mut srv_customize_cursor: usize = 0;
    let mut srv_customize_edit_buf = String::new();
    let mut srv_customize_filter = String::new();
    #[allow(unused_assignments)]
    let mut srv_customize_options: Vec<CustomizeOption> = Vec::new();

    #[derive(serde::Deserialize, Default)]
    struct WinStatus { id: usize, name: String, active: bool, #[serde(default)] activity: bool, #[serde(default)] tab_text: String }
    
    fn default_base_index() -> usize { 1 }
    fn default_prediction_dimming() -> bool { dim_predictions_enabled() }
    fn default_status_left_length() -> usize { 10 }
    fn default_status_right_length() -> usize { 40 }
    fn default_status_lines() -> usize { 1 }
    fn default_status_visible() -> bool { true }
    fn default_repeat_time() -> u64 { 500 }

    /// A single key binding synced from the server.
    #[derive(serde::Deserialize, Clone, Debug)]
    struct BindingEntry {
        /// Key table name (e.g. "prefix", "root")
        t: String,
        /// Key string (e.g. "C-a", "-", "F12")
        k: String,
        /// Command string (e.g. "split-window -v")
        c: String,
        /// Whether the binding is repeatable
        #[serde(default)]
        r: bool,
    }

    /// A menu item from server-side MenuMode
    #[derive(serde::Deserialize, Clone, Debug, Default)]
    struct ServerMenuItem {
        #[serde(default)]
        name: Option<String>,
        #[serde(default)]
        key: Option<String>,
        #[serde(default)]
        sep: bool,
    }

    /// A customize-mode option row from server
    #[derive(serde::Deserialize, Clone, Debug, Default)]
    struct CustomizeOption {
        /// Original index in the full options list
        i: usize,
        /// Option name
        n: String,
        /// Current value
        v: String,
        /// Scope (server/session/window/pane)
        s: String,
    }

    #[derive(serde::Deserialize)]
    struct DumpState {
        layout: LayoutJson,
        windows: Vec<WinStatus>,
        #[serde(default)]
        prefix: Option<String>,
        #[serde(default)]
        prefix2: Option<String>,
        #[serde(default)]
        tree: Vec<WinTree>,
        #[serde(default = "default_base_index")]
        base_index: usize,
        #[serde(default = "default_prediction_dimming")]
        prediction_dimming: bool,
        #[serde(default)]
        status_style: Option<String>,
        #[serde(default)]
        status_left: Option<String>,
        #[serde(default)]
        status_right: Option<String>,
        #[serde(default)]
        pane_border_style: Option<String>,
        #[serde(default)]
        pane_active_border_style: Option<String>,
        #[serde(default)]
        pane_border_hover_style: Option<String>,
        #[serde(default)]
        pane_border_status: Option<String>,
        #[serde(default)]
        pane_border_format: Option<String>,
        /// window-status-format (short key to save bandwidth)
        #[serde(default)]
        wsf: Option<String>,
        /// window-status-current-format
        #[serde(default)]
        wscf: Option<String>,
        /// window-status-separator
        #[serde(default)]
        wss: Option<String>,
        /// window-status-style
        #[serde(default)]
        ws_style: Option<String>,
        /// window-status-current-style
        #[serde(default)]
        wsc_style: Option<String>,
        /// clock-mode active
        #[serde(default)]
        clock_mode: bool,
        /// clock-mode-colour (tmux option)
        #[serde(default)]
        clock_colour: Option<String>,
        /// Dynamic key bindings from server
        #[serde(default)]
        bindings: Vec<BindingEntry>,
        /// status-left-length (max display width for left status)
        #[serde(default = "default_status_left_length")]
        status_left_length: usize,
        /// status-right-length (max display width for right status)
        #[serde(default = "default_status_right_length")]
        status_right_length: usize,
        /// Number of status bar lines
        #[serde(default = "default_status_lines")]
        status_lines: usize,
        /// Custom format strings for additional status lines
        #[serde(default)]
        status_format: Vec<String>,
        /// mode-style for copy mode selection highlighting
        #[serde(default)]
        mode_style: Option<String>,
        /// status-position: "top" or "bottom"
        #[serde(default)]
        status_position: Option<String>,
        /// status-justify: "left", "centre", or "right"
        #[serde(default)]
        status_justify: Option<String>,
        /// Whether the status bar is visible (true) or hidden (false).
        /// Corresponds to `set-option status on/off`.
        #[serde(default = "default_status_visible")]
        status_visible: bool,
        /// Configured cursor style as DECSCUSR code (0-6) from server.
        /// Used as fallback when no child process has set a cursor shape.
        #[serde(default)]
        cursor_style_code: Option<u8>,
        /// One-shot clipboard text (base64-encoded) for OSC 52 delivery.
        #[serde(default)]
        clipboard_osc52: Option<String>,
        /// One-shot bell flag: server signals client to emit \x07 to the host terminal.
        #[serde(default)]
        bell: bool,
        /// Repeat key timeout in ms (default: 500, synced from server)
        #[serde(default = "default_repeat_time")]
        repeat_time: u64,
        /// Whether a pane is currently zoomed (borders should be hidden)
        #[serde(default)]
        zoomed: bool,
        // ── Server-side overlay state ──
        /// Popup overlay active
        #[serde(default)]
        popup_active: bool,
        #[serde(default)]
        popup_command: Option<String>,
        #[serde(default)]
        popup_width: Option<u16>,
        #[serde(default)]
        popup_height: Option<u16>,
        #[serde(default)]
        popup_lines: Vec<String>,
        #[serde(default)]
        popup_rows: Vec<crate::layout::RowRunsJson>,
        #[serde(default)]
        popup_has_pty: bool,
        /// Confirm overlay active
        #[serde(default)]
        confirm_active: bool,
        #[serde(default)]
        confirm_prompt: Option<String>,
        /// Menu overlay active
        #[serde(default)]
        menu_active: bool,
        #[serde(default)]
        menu_title: Option<String>,
        #[serde(default)]
        menu_selected: usize,
        #[serde(default)]
        menu_items: Vec<ServerMenuItem>,
        /// Display-panes overlay active
        #[serde(default)]
        display_panes: bool,
        /// Pane base index for display-panes numbering
        #[serde(default)]
        pane_base_index: usize,
        /// Status bar message from display-message (without -p)
        #[serde(default)]
        status_message: Option<String>,
        /// Customize-mode overlay active
        #[serde(default)]
        customize_active: bool,
        #[serde(default)]
        customize_selected: usize,
        #[serde(default)]
        customize_scroll: usize,
        #[serde(default)]
        customize_editing: bool,
        #[serde(default)]
        customize_cursor: usize,
        #[serde(default)]
        customize_edit_buf: Option<String>,
        #[serde(default)]
        customize_filter: Option<String>,
        #[serde(default)]
        customize_options: Vec<CustomizeOption>,
    }

    let mut cmd_batch: Vec<String> = Vec::new();
    let mut dump_buf = String::new();
    let mut prev_dump_buf = String::new();
    let mut last_key_send_time: Option<Instant> = None;
    let mut dump_in_flight = false;
    let mut dump_flight_start: Instant = Instant::now();

    // Diagnostic latency log: set PSMUX_LATENCY_LOG=1 to enable
    let latency_log_enabled = env::var("PSMUX_LATENCY_LOG").unwrap_or_default() == "1";
    let mut latency_log: Option<std::fs::File> = if latency_log_enabled {
        let home = env::var("USERPROFILE").or_else(|_| env::var("HOME")).unwrap_or_default();
        let path = format!("{}\\.psmux\\latency.log", home);
        std::fs::File::create(&path).ok()
    } else { None };
    let mut loop_count: u64 = 0;
    let mut _last_key_char: Option<char> = None;
    let mut key_send_instant: Option<Instant> = None; // when the key was SENT to server

    // Text selection state (client-side only, left-click drag like pwsh)
    let mut rsel_start: Option<(u16, u16)> = None;  // (col, row) in terminal coords
    let mut rsel_end: Option<(u16, u16)> = None;
    let mut rsel_dragged = false;
    let mut selection_changed = false; // forces redraw for selection overlay
    let mut border_drag = false; // true when dragging a pane separator (resize)
    // Client-side tab position tracking for accurate mouse click detection.
    // The server's update_tab_positions() uses a different algorithm than what
    // the client actually renders, so we track positions at render time.
    let mut client_tab_positions: Vec<(usize, u16, u16)> = Vec::new(); // (window_array_idx, x_start, x_end)
    let mut client_status_row: u16 = u16::MAX; // row where status bar tabs are rendered
    let mut client_base_index: usize = 0; // base-index for window numbering
    let mut client_pane_rects: Vec<(usize, Rect)> = Vec::new();
    let mut client_borders: Vec<(Vec<usize>, String, usize, u16, u16, Vec<u16>, Rect)> = Vec::new();
    let mut client_content_area: Rect = Rect::default();
    let mut client_copy_mode: bool = false;
    let mut client_zoomed: bool = false;
    let mut client_drag: Option<ClientDragState> = None;
    // Border hover highlight: (position, kind, area) of the border under the cursor.
    let mut hovered_border: Option<(u16, String, Rect)> = None;
    // Buffered OSC 52 clipboard text — written AFTER terminal.draw() to
    // avoid corrupting ratatui's output buffer.
    let mut pending_osc52: Option<String> = None;
    let mut pending_bell = false;
    // VT input mode: periodically re-send mouse-enable escape sequences.
    // Covers SSH sessions and JetBrains JediTerm (which sends VT mouse
    // sequences through ConPTY instead of native MOUSE_EVENT records).
    let is_ssh_mode = crate::ssh_input::needs_vt_input();
    let mut last_mouse_enable = Instant::now();
    // ── Cursor blink stabilisation ──────────────────────────────────
    // Cache the last-sent DECSCUSR code so we only write it when it
    // actually changes (avoids resetting WT's blink timer every frame).
    let mut last_cursor_style: u8 = 255;
    loop {
        // Expire stale key_send_instant after 30ms — ConPTY echo should
        // have arrived by then; stop force-dumping to save CPU.
        if let Some(ks) = key_send_instant {
            if ks.elapsed().as_millis() > 30 { key_send_instant = None; }
        }
        // Safety valve: if dump_in_flight is stuck for >500ms (e.g. server
        // did not respond), release it so the client doesn't spin at 1ms.
        if dump_in_flight && dump_flight_start.elapsed().as_millis() > 500 {
            dump_in_flight = false;
        }
        // ── STEP 0: Receive latest frame from reader thread (non-blocking) ──
        // Drain channel, keeping only the most recent frame.
        let mut got_frame = false;
        let mut _nc_count = 0u32;
        loop {
            match frame_rx.try_recv() {
                Ok(line) => {
                    if line.trim() == "NC" {
                        _nc_count += 1;
                        // Server says nothing changed — release dump_in_flight
                        // without touching dump_buf (saves 50-100KB clone + parse).
                        dump_in_flight = false;
                        last_dump_time = Instant::now();
                        // If we're waiting for a key echo, force an
                        // immediate dump-state re-request (~1ms TCP RTT)
                        // instead of waiting the full 10ms typing interval.
                        if key_send_instant.is_some() {
                            force_dump = true;
                        }
                    } else {
                        if client_log_enabled() {
                            client_log("frame", &format!("received {} bytes", line.len()));
                        }
                        dump_buf = line; got_frame = true; dump_in_flight = false;
                    }
                }
                Err(std::sync::mpsc::TryRecvError::Empty) => break,
                Err(std::sync::mpsc::TryRecvError::Disconnected) => { quit = true; break; }
            }
        }
        if quit && !got_frame { break; }

        // ── STEP 1: Poll events with adaptive timeout ────────────────────
        let since_dump = last_dump_time.elapsed().as_millis() as u64;
        // Expire typing timer after 100ms of no new keys
        if let Some(kt) = last_key_send_time {
            if kt.elapsed().as_millis() > 100 { last_key_send_time = None; }
        }
        let typing_active = last_key_send_time.is_some();
        // When typing: cap at ~100fps to avoid flooding the server with
        // dump-state requests (each one is ~50-100KB of JSON over TCP).
        // When idle: 50ms refresh (20fps) saves CPU.
        // Use fast poll when paste chars are pending (need timely detection)
        #[cfg(windows)]
        let paste_pend_active = !paste_pend.is_empty();
        #[cfg(not(windows))]
        let paste_pend_active = false;

        let poll_ms = if paste_pend_active { 1 }
            else if got_frame { 0 }
            else if dump_in_flight { 5 }
            else if force_dump { 0 }
            else if typing_active {
                // Rate-limit to ~100fps (10ms) when typing.  The snapshot-
                // based serialisation in dump_layout_json_fast now holds
                // the parser mutex for only ~1ms (cell snapshot), so
                // polling at 10ms no longer starves the ConPTY reader
                // thread.  10ms is notably shorter than ConPTY's ~16ms
                // render interval, avoiding systematic alignment delays.
                let remaining = 10u64.saturating_sub(since_dump);
                remaining
            }
            else {
                // Server pushes frames proactively via auto-push —
                // no need for fast idle polling.  16ms (~60fps) ensures
                // pushed frames render within one vsync while using
                // negligible CPU (vs 50ms poll + dump-state roundtrip).
                16
            };

        cmd_batch.clear();

        // ── Windows paste pending-buffer management ────────────────────
        // Flush or promote chars based on how long they've been buffered.
        #[cfg(windows)]
        {
            if let Some(start) = paste_pend_start {
                let elapsed = start.elapsed();
                if paste_confirmed {
                    // Ctrl+V Release already seen — send as paste now
                    if !paste_pend.is_empty() {
                        if input_log_enabled() {
                            input_log("paste", &format!("paste CONFIRMED (top), sending {} chars as send-paste: {:?}",
                                paste_pend.len(), &paste_pend.chars().take(200).collect::<String>()));
                        }
                        let encoded = base64_encode(&paste_pend);
                        cmd_batch.push(format!("send-paste {}\n", encoded));
                    }
                    paste_pend.clear();
                    paste_pend_start = None;
                    paste_stage2 = false;
                    paste_confirmed = false;
                } else if !paste_stage2 && elapsed > Duration::from_millis(20) {
                    // 20ms window expired
                    let has_non_ascii = paste_pend.chars().any(|c| !c.is_ascii());
                    if paste_pend.len() >= 3 && !has_non_ascii {
                        // ≥3 ASCII chars in 20ms → likely paste, enter stage 2.
                        // Non-ASCII chars (IME composition, CJK input) are excluded
                        // because IME routinely generates 3+ chars in <20ms and would
                        // trigger a false-positive 300ms delay (fixes #91).
                        paste_stage2 = true;
                        paste_stage2_last_len = paste_pend.len();
                        if input_log_enabled() {
                            input_log("paste", &format!("stage2: {} chars in 20ms, waiting for Ctrl+V Release", paste_pend.len()));
                        }
                    } else if paste_pend.len() >= 20 && has_non_ascii {
                        // ≥20 non-ASCII chars in 20ms — almost certainly a paste
                        // containing Unicode content (em-dashes, CJK, etc.), not
                        // IME composition (which rarely exceeds a few chars).
                        paste_stage2 = true;
                        paste_stage2_last_len = paste_pend.len();
                        if input_log_enabled() {
                            input_log("paste", &format!("stage2 (large non-ASCII): {} chars in 20ms", paste_pend.len()));
                        }
                    } else if paste_pend.len() >= 3 && has_non_ascii {
                        // ≥3 chars but contains non-ASCII (IME input) — flush
                        // immediately as normal text to avoid 300ms delay.
                        if input_log_enabled() {
                            input_log("paste", &format!("flush {} chars as normal (non-ASCII / IME detected)", paste_pend.len()));
                        }
                        for c in paste_pend.chars() {
                            match c {
                                '\n' => { cmd_batch.push("send-key enter\n".into()); }
                                '\t' => { cmd_batch.push("send-key tab\n".into()); }
                                ' '  => { cmd_batch.push("send-key space\n".into()); }
                                _ => {
                                    let escaped = match c {
                                        '"' => "\\\"".to_string(),
                                        '\\' => "\\\\".to_string(),
                                        _ => c.to_string(),
                                    };
                                    cmd_batch.push(format!("send-text \"{}\"\n", escaped));
                                }
                            }
                        }
                        paste_pend.clear();
                        paste_pend_start = None;
                    } else {
                        // <3 chars → normal typing, flush as send-text
                        if input_log_enabled() {
                            input_log("paste", &format!("flush {} chars as normal (< 3 in 20ms)", paste_pend.len()));
                        }
                        for c in paste_pend.chars() {
                            match c {
                                '\n' => { cmd_batch.push("send-key enter\n".into()); }
                                '\t' => { cmd_batch.push("send-key tab\n".into()); }
                                ' '  => { cmd_batch.push("send-key space\n".into()); }
                                _ => {
                                    let escaped = match c {
                                        '"' => "\\\"".to_string(),
                                        '\\' => "\\\\".to_string(),
                                        _ => c.to_string(),
                                    };
                                    cmd_batch.push(format!("send-text \"{}\"\n", escaped));
                                }
                            }
                        }
                        paste_pend.clear();
                        paste_pend_start = None;
                    }
                } else if paste_stage2 && elapsed > Duration::from_millis(300) {
                    // Stage 2 timeout — no Ctrl+V Release arrived.
                    // Growth detection: if the buffer grew since last check,
                    // ConPTY is still injecting characters (large paste).
                    // Extend the window instead of splitting the paste.
                    if paste_pend.len() > paste_stage2_last_len {
                        paste_stage2_last_len = paste_pend.len();
                        paste_pend_start = Some(Instant::now() - Duration::from_millis(280));
                    } else {
                        // Buffer stopped growing — send accumulated chars as
                        // send-paste so the server wraps in bracketed paste.
                        if input_log_enabled() {
                            input_log("paste", &format!("stage2 timeout, sending {} chars as send-paste", paste_pend.len()));
                        }
                        let encoded = base64_encode(&paste_pend);
                        cmd_batch.push(format!("send-paste {}\n", encoded));
                        paste_pend.clear();
                        paste_pend_start = None;
                        paste_stage2 = false;
                        paste_stage2_last_len = 0;
                    }
                }
            }
        }

        {
            let mut _pending_evt = input.read_timeout(Duration::from_millis(poll_ms))?;
            while let Some(_cur_evt) = _pending_evt {
                // Input debug: log every raw event BEFORE filtering
                if input_log_enabled() {
                    match &_cur_evt {
                        Event::Key(key) => {
                            input_log("event", &format!(
                                "Key code={:?} mods={:?} kind={:?} state={:?}",
                                key.code, key.modifiers, key.kind, key.state
                            ));
                        }
                        Event::Mouse(me) => {
                            input_log("event", &format!("Mouse {:?}", me.kind));
                        }
                        Event::Resize(w, h) => {
                            input_log("event", &format!("Resize {}x{}", w, h));
                        }
                        Event::Paste(d) => {
                            input_log("event", &format!("Paste ({} bytes)", d.len()));
                        }
                        other => {
                            input_log("event", &format!("Other {:?}", other));
                        }
                    }
                }
                match _cur_evt {
                    // ── Windows Ctrl+V paste interception ────────────────
                    // ── Suppress phantom modified-Enter Release (Windows Terminal) ──
                    // Windows Terminal fires a real Press then a phantom Release
                    // ~80ms later for Shift+Enter.  If we already handled the
                    // Press, drop the Release so it does not trigger the WezTerm
                    // Release-only acceptance path and produce a double newline.
                    #[cfg(windows)]
                    Event::Key(key) if key.kind == KeyEventKind::Release
                        && matches!(key.code, KeyCode::Enter)
                        && modified_enter_press_handled =>
                    {
                        // drop the phantom Release
                    }
                    // On Windows, Windows Terminal intercepts Ctrl+V Press,
                    // reads the clipboard, and injects the paste content as
                    // a byte stream into the ConPTY input pipe — bypassing
                    // the console input buffer that crossterm reads via
                    // ReadConsoleInputW.  Only the Ctrl+V *Release* event
                    // leaks through.  We use that Release as a trigger to
                    // read the clipboard ourselves and forward the content
                    // as a bracketed-paste so child apps (Claude CLI, etc.)
                    // can distinguish paste from typed input.
                    #[cfg(windows)]
                    Event::Key(key) if key.kind == KeyEventKind::Release
                        && matches!(key.code, KeyCode::Char('v'))
                        && key.modifiers == KeyModifiers::CONTROL =>
                    {
                        if input_log_enabled() {
                            input_log("paste", &format!("Ctrl+V Release detected, paste_pend len={}", paste_pend.len()));
                        }
                        paste_confirmed = true;
                    }
                    // ── WezTerm: Shift+Enter arrives as Release-only ──
                    // WezTerm generates only KeyEventKind::Release for Shift+Enter
                    // (no Press, no Repeat).  Accept and promote to Press.
                    #[cfg(windows)]
                    Event::Key(mut key) if key.kind == KeyEventKind::Release
                        && matches!(key.code, KeyCode::Enter)
                        && !key.modifiers.is_empty() =>
                    {
                        key.kind = KeyEventKind::Press;
                        crate::platform::augment_enter_shift(&mut key);
                        modified_enter_press_handled = true;
                        // Skip paste buffering — forward directly like a Press.
                        let is_prefix = (key.code, key.modifiers) == prefix_key
                            || prefix_raw_char.map_or(false, |c| matches!(key.code, KeyCode::Char(ch) if ch == c))
                            || prefix2_key.map_or(false, |p2| (key.code, key.modifiers) == p2)
                            || prefix2_raw_char.map_or(false, |c| matches!(key.code, KeyCode::Char(ch) if ch == c));
                        if !is_prefix {
                            if let Some(encoded) = crate::input::encode_key_event(&key) {
                                cmd_batch.push(format!("send-key-raw {}\n",
                                    encoded.iter().map(|b| format!("{:02x}", b)).collect::<String>()));
                            }
                        }
                    }
                    Event::Key(mut key) if key.kind == KeyEventKind::Press || key.kind == KeyEventKind::Repeat => {
                        // On Windows, VS Code's xterm.js sends ESC+CR for
                        // Shift+Enter.  ConPTY interprets the ESC as Alt, so
                        // crossterm reports Alt+Enter.  Poll the physical
                        // keyboard to detect the real modifier.
                        #[cfg(windows)]
                        crate::platform::augment_enter_shift(&mut key);
                        // Clear the WezTerm dedup flag on any non-Enter key; set
                        // it when a modified Enter Press is being processed.
                        #[cfg(windows)]
                        {
                            if matches!(key.code, KeyCode::Enter) && !key.modifiers.is_empty() {
                                modified_enter_press_handled = true;
                            } else {
                                modified_enter_press_handled = false;
                            }
                        }
                        // Flush pending paste buffer before processing any non-bufferable key.
                        // Bufferable keys are: plain Char, Space, Enter (if pend non-empty), Tab (if pend non-empty).
                        #[cfg(windows)]
                        {
                            if !paste_pend.is_empty() {
                                let is_bufferable = match key.code {
                                    KeyCode::Char(' ') => true,
                                    KeyCode::Char(c) => {
                                        // AltGr on Windows is reported as Ctrl+Alt.
                                        // Non-letter chars with Ctrl+Alt are AltGr-produced
                                        // (e.g. \ @ { } on German/Czech keyboards) and
                                        // should be bufferable like normal text.
                                        let is_altgr = key.modifiers.contains(KeyModifiers::CONTROL)
                                            && key.modifiers.contains(KeyModifiers::ALT)
                                            && !c.is_ascii_lowercase();
                                        is_altgr || (!key.modifiers.contains(KeyModifiers::CONTROL)
                                                  && !key.modifiers.contains(KeyModifiers::ALT))
                                    }
                                    KeyCode::Enter | KeyCode::Tab => true, // buffered when pend non-empty
                                    _ => false,
                                };
                                if !is_bufferable {
                                    flush_paste_pend_as_text(&mut paste_pend, &mut paste_pend_start, &mut paste_stage2, &mut cmd_batch);
                                }
                            }
                        }
                        // Dynamic prefix key check (default: Ctrl+B, configurable via .psmux.conf)
                        let is_prefix = (key.code, key.modifiers) == prefix_key
                            || prefix_raw_char.map_or(false, |c| matches!(key.code, KeyCode::Char(ch) if ch == c))
                            || prefix2_key.map_or(false, |p2| (key.code, key.modifiers) == p2)
                            || prefix2_raw_char.map_or(false, |c| matches!(key.code, KeyCode::Char(ch) if ch == c));

                        // Expire repeat-mode prefix if repeat-time has elapsed.
                        // This ensures keys are forwarded to the PTY rather than
                        // being interpreted as prefix bindings (tmux parity).
                        if prefix_armed && prefix_repeating
                            && prefix_armed_at.elapsed().as_millis() >= repeat_time_ms as u128
                        {
                            prefix_armed = false;
                            prefix_repeating = false;
                            cmd_batch.push("prefix-end\n".into());
                        }

                        // Overlay Esc must be checked BEFORE selection-Esc so that
                        // pressing Esc always closes the active overlay first.
                        // ── Server-side overlay key handling ─────────────────
                        // When a server overlay is active, intercept ALL keys and
                        // forward them to the server via overlay-specific commands.
                        if srv_popup_active {
                            if srv_popup_has_pty {
                                // PTY popup: forward all keys to server
                                match key.code {
                                    KeyCode::Esc => { cmd_batch.push("overlay-close\n".into()); }
                                    KeyCode::Char(c) => {
                                        let bytes = if key.modifiers.contains(crossterm::event::KeyModifiers::CONTROL) {
                                            vec![(c as u8) & 0x1F]
                                        } else {
                                            let mut buf = [0u8; 4];
                                            let s = c.encode_utf8(&mut buf);
                                            s.as_bytes().to_vec()
                                        };
                                        let encoded = crate::util::base64_encode(std::str::from_utf8(&bytes).unwrap_or(""));
                                        cmd_batch.push(format!("popup-input {}\n", encoded));
                                    }
                                    KeyCode::Enter => {
                                        let encoded = crate::util::base64_encode("\r");
                                        cmd_batch.push(format!("popup-input {}\n", encoded));
                                    }
                                    KeyCode::Backspace => {
                                        let encoded = crate::util::base64_encode("\x7f");
                                        cmd_batch.push(format!("popup-input {}\n", encoded));
                                    }
                                    KeyCode::Tab => {
                                        let encoded = crate::util::base64_encode("\t");
                                        cmd_batch.push(format!("popup-input {}\n", encoded));
                                    }
                                    KeyCode::Up => {
                                        let encoded = crate::util::base64_encode("\x1b[A");
                                        cmd_batch.push(format!("popup-input {}\n", encoded));
                                    }
                                    KeyCode::Down => {
                                        let encoded = crate::util::base64_encode("\x1b[B");
                                        cmd_batch.push(format!("popup-input {}\n", encoded));
                                    }
                                    KeyCode::Right => {
                                        let encoded = crate::util::base64_encode("\x1b[C");
                                        cmd_batch.push(format!("popup-input {}\n", encoded));
                                    }
                                    KeyCode::Left => {
                                        let encoded = crate::util::base64_encode("\x1b[D");
                                        cmd_batch.push(format!("popup-input {}\n", encoded));
                                    }
                                    KeyCode::Home => {
                                        let encoded = crate::util::base64_encode("\x1b[H");
                                        cmd_batch.push(format!("popup-input {}\n", encoded));
                                    }
                                    KeyCode::End => {
                                        let encoded = crate::util::base64_encode("\x1b[F");
                                        cmd_batch.push(format!("popup-input {}\n", encoded));
                                    }
                                    KeyCode::PageUp => {
                                        let encoded = crate::util::base64_encode("\x1b[5~");
                                        cmd_batch.push(format!("popup-input {}\n", encoded));
                                    }
                                    KeyCode::PageDown => {
                                        let encoded = crate::util::base64_encode("\x1b[6~");
                                        cmd_batch.push(format!("popup-input {}\n", encoded));
                                    }
                                    KeyCode::Delete => {
                                        let encoded = crate::util::base64_encode("\x1b[3~");
                                        cmd_batch.push(format!("popup-input {}\n", encoded));
                                    }
                                    _ => {}
                                }
                            } else {
                                // Static (non-PTY) popup: handle scroll locally, q/Esc close
                                let total_lines = srv_popup_lines.len() as u16;
                                match key.code {
                                    KeyCode::Esc | KeyCode::Char('q') => {
                                        cmd_batch.push("overlay-close\n".into());
                                        srv_popup_scroll = 0;
                                    }
                                    KeyCode::Up | KeyCode::Char('k') => {
                                        srv_popup_scroll = srv_popup_scroll.saturating_sub(1);
                                    }
                                    KeyCode::Down | KeyCode::Char('j') => {
                                        if srv_popup_scroll < total_lines.saturating_sub(1) {
                                            srv_popup_scroll += 1;
                                        }
                                    }
                                    KeyCode::PageUp => {
                                        srv_popup_scroll = srv_popup_scroll.saturating_sub(10);
                                    }
                                    KeyCode::PageDown => {
                                        srv_popup_scroll = (srv_popup_scroll + 10).min(total_lines.saturating_sub(1));
                                    }
                                    KeyCode::Home | KeyCode::Char('g') => {
                                        srv_popup_scroll = 0;
                                    }
                                    KeyCode::End | KeyCode::Char('G') => {
                                        srv_popup_scroll = total_lines.saturating_sub(1);
                                    }
                                    _ => {}
                                }
                            }
                        }
                        else if srv_confirm_active {
                            match key.code {
                                KeyCode::Char('y') | KeyCode::Char('Y') => {
                                    cmd_batch.push("confirm-respond y\n".into());
                                }
                                KeyCode::Char('n') | KeyCode::Char('N') | KeyCode::Esc => {
                                    cmd_batch.push("confirm-respond n\n".into());
                                }
                                _ => {} // Ignore other keys during confirm
                            }
                        }
                        else if srv_menu_active {
                            match key.code {
                                KeyCode::Up | KeyCode::Char('k') => { cmd_batch.push("menu-navigate -1\n".into()); }
                                KeyCode::Down | KeyCode::Char('j') => { cmd_batch.push("menu-navigate 1\n".into()); }
                                KeyCode::Enter => {
                                    cmd_batch.push(format!("menu-select {}\n", srv_menu_selected));
                                }
                                KeyCode::Esc | KeyCode::Char('q') => { cmd_batch.push("overlay-close\n".into()); }
                                KeyCode::Char(c) => {
                                    // Shortcut key: find menu item with matching key
                                    if let Some(idx) = srv_menu_items.iter().position(|item| {
                                        item.key.as_ref().map(|k| k.len() == 1 && k.chars().next() == Some(c)).unwrap_or(false)
                                    }) {
                                        cmd_batch.push(format!("menu-select {}\n", idx));
                                    }
                                }
                                _ => {}
                            }
                        }
                        else if srv_display_panes {
                            match key.code {
                                KeyCode::Char(d) if d.is_ascii_digit() => {
                                    let digit = d.to_digit(10).unwrap() as usize;
                                    cmd_batch.push(format!("display-panes-select {}\n", digit));
                                }
                                _ => { cmd_batch.push("overlay-close\n".into()); }
                            }
                        }
                        else if srv_customize_active {
                            if srv_customize_editing {
                                match key.code {
                                    KeyCode::Esc => { cmd_batch.push("customize-edit-cancel\n".into()); }
                                    KeyCode::Enter => { cmd_batch.push("customize-edit-confirm\n".into()); }
                                    KeyCode::Backspace => {
                                        if srv_customize_cursor > 0 {
                                            let mut buf = srv_customize_edit_buf.clone();
                                            buf.remove(srv_customize_cursor - 1);
                                            cmd_batch.push(format!("customize-edit-update {}\n", buf));
                                        }
                                    }
                                    KeyCode::Char(c) => {
                                        let mut buf = srv_customize_edit_buf.clone();
                                        buf.insert(srv_customize_cursor, c);
                                        cmd_batch.push(format!("customize-edit-update {}\n", buf));
                                    }
                                    _ => {}
                                }
                            } else {
                                match key.code {
                                    KeyCode::Esc | KeyCode::Char('q') => { cmd_batch.push("overlay-close\n".into()); }
                                    KeyCode::Up | KeyCode::Char('k') => { cmd_batch.push("customize-navigate -1\n".into()); }
                                    KeyCode::Down | KeyCode::Char('j') => { cmd_batch.push("customize-navigate 1\n".into()); }
                                    KeyCode::PageUp => { cmd_batch.push("customize-navigate -20\n".into()); }
                                    KeyCode::PageDown => { cmd_batch.push("customize-navigate 20\n".into()); }
                                    KeyCode::Home | KeyCode::Char('g') => { cmd_batch.push("customize-navigate -9999\n".into()); }
                                    KeyCode::End | KeyCode::Char('G') => { cmd_batch.push("customize-navigate 9999\n".into()); }
                                    KeyCode::Enter => { cmd_batch.push("customize-edit\n".into()); }
                                    KeyCode::Char('d') => { cmd_batch.push("customize-reset-default\n".into()); }
                                    KeyCode::Char('/') => {
                                        // Toggle filter: if filter active, clear it
                                        if !srv_customize_filter.is_empty() {
                                            cmd_batch.push("customize-filter \n".into());
                                        }
                                        // For entering a new filter, we would need a mini prompt.
                                        // For now, users type filter text via subsequent keystrokes.
                                    }
                                    _ => {}
                                }
                            }
                        }
                        else if matches!(key.code, KeyCode::Esc) && (command_input || renaming || pane_renaming || tree_chooser || session_chooser || confirm_cmd.is_some() || keys_viewer) {
                            command_input = false;
                            command_cursor = 0;
                            renaming = false;
                            pane_renaming = false;
                            tree_chooser = false;
                            session_chooser = false;
                            keys_viewer = false;
                            confirm_cmd = None;
                            // Also clear any lingering selection
                            rsel_start = None;
                            rsel_end = None;
                            selection_changed = true;
                        }
                        else if rsel_start.is_some() && matches!(key.code, KeyCode::Esc) {
                            // Escape clears any active text selection
                            rsel_start = None;
                            rsel_end = None;
                            selection_changed = true;
                        }
                        else if is_prefix { prefix_armed = true; prefix_armed_at = Instant::now(); prefix_repeating = false; cmd_batch.push("prefix-begin\n".into()); }
                        // Check root-table bindings (bind-key -n / bind-key -T root)
                        // These fire without prefix, before keys are forwarded to PTY
                        else if !command_input && !renaming && !pane_renaming && !tree_chooser && !session_chooser && !keys_viewer && confirm_cmd.is_none() && {
                            let key_tuple = normalize_key_for_binding((key.code, key.modifiers));
                            synced_bindings.iter().any(|b| b.t == "root" && parse_key_string(&b.k).map_or(false, |k| normalize_key_for_binding(k) == key_tuple))
                        } {
                            let key_tuple = normalize_key_for_binding((key.code, key.modifiers));
                            if let Some(entry) = synced_bindings.iter().find(|b| {
                                b.t == "root" && parse_key_string(&b.k).map_or(false, |k| normalize_key_for_binding(k) == key_tuple)
                            }) {
                                if entry.c == "detach-client" || entry.c == "detach" {
                                    quit = true;
                                } else {
                                    cmd_batch.push(format!("{}\n", entry.c));
                                }
                            }
                        }
                        else if prefix_armed {
                            // Check user-defined synced bindings FIRST (like server-side input.rs).
                            // This lets users override any default hardcoded key binding.
                            let key_tuple = normalize_key_for_binding((key.code, key.modifiers));
                            let user_binding = synced_bindings.iter().find(|b| {
                                b.t == "prefix" && parse_key_string(&b.k).map_or(false, |k| normalize_key_for_binding(k) == key_tuple)
                            });
                            if let Some(entry) = user_binding {
                                // User-defined binding takes priority
                                if entry.c == "detach-client" || entry.c == "detach" {
                                    quit = true;
                                } else if entry.c.starts_with("confirm-before") || entry.c == "kill-pane" {
                                    confirm_cmd = Some(entry.c.clone());
                                } else {
                                    cmd_batch.push(format!("{}\n", entry.c));
                                }
                            } else {
                            // Default hardcoded bindings (only reached if no user override)
                            match key.code {
                                KeyCode::Char('c') => { cmd_batch.push("new-window\n".into()); }
                                KeyCode::Char('%') => { cmd_batch.push("split-window -h\n".into()); }
                                KeyCode::Char('"') => { cmd_batch.push("split-window -v\n".into()); }
                                KeyCode::Char('x') => { confirm_cmd = Some("kill-pane".into()); }
                                KeyCode::Char('&') => { confirm_cmd = Some("kill-window".into()); }
                                KeyCode::Char('z') => { cmd_batch.push("zoom-pane\n".into()); }
                                KeyCode::Char('[') => { cmd_batch.push("copy-enter\n".into()); }
                                KeyCode::Char(']') => { cmd_batch.push("paste-buffer\n".into()); }
                                KeyCode::Char('{') => { cmd_batch.push("swap-pane -U\n".into()); }
                                KeyCode::Char('}') => { cmd_batch.push("swap-pane -D\n".into()); }
                                KeyCode::Char('n') => { cmd_batch.push("next-window\n".into()); }
                                KeyCode::Char('p') => { cmd_batch.push("previous-window\n".into()); }
                                KeyCode::Char('l') => { cmd_batch.push("last-window\n".into()); }
                                KeyCode::Char(';') => { cmd_batch.push("last-pane\n".into()); }
                                KeyCode::Char(' ') => { cmd_batch.push("next-layout\n".into()); }
                                KeyCode::Char('!') => { cmd_batch.push("break-pane\n".into()); }
                                KeyCode::Char(d) if d.is_ascii_digit() => {
                                    let idx = d.to_digit(10).unwrap() as usize;
                                    cmd_batch.push(format!("select-window {}\n", idx));
                                }
                                KeyCode::Char('o') => { cmd_batch.push("select-pane -t :.+\n".into()); }
                                // Alt+Arrow: resize pane by 5 (must be before plain Arrow)
                                KeyCode::Up if key.modifiers.contains(KeyModifiers::ALT) => { cmd_batch.push("resize-pane -U 5\n".into()); }
                                KeyCode::Down if key.modifiers.contains(KeyModifiers::ALT) => { cmd_batch.push("resize-pane -D 5\n".into()); }
                                KeyCode::Left if key.modifiers.contains(KeyModifiers::ALT) => { cmd_batch.push("resize-pane -L 5\n".into()); }
                                KeyCode::Right if key.modifiers.contains(KeyModifiers::ALT) => { cmd_batch.push("resize-pane -R 5\n".into()); }
                                // Ctrl+Arrow: resize pane by 1
                                KeyCode::Up if key.modifiers.contains(KeyModifiers::CONTROL) => { cmd_batch.push("resize-pane -U 1\n".into()); }
                                KeyCode::Down if key.modifiers.contains(KeyModifiers::CONTROL) => { cmd_batch.push("resize-pane -D 1\n".into()); }
                                KeyCode::Left if key.modifiers.contains(KeyModifiers::CONTROL) => { cmd_batch.push("resize-pane -L 1\n".into()); }
                                KeyCode::Right if key.modifiers.contains(KeyModifiers::CONTROL) => { cmd_batch.push("resize-pane -R 1\n".into()); }
                                // Plain Arrow: select pane
                                KeyCode::Up => { cmd_batch.push("select-pane -U\n".into()); }
                                KeyCode::Down => { cmd_batch.push("select-pane -D\n".into()); }
                                KeyCode::Left => { cmd_batch.push("select-pane -L\n".into()); }
                                KeyCode::Right => { cmd_batch.push("select-pane -R\n".into()); }
                                KeyCode::Char('d') => { quit = true; }
                                KeyCode::Char(',') => { renaming = true; rename_buf.clear(); }
                                KeyCode::Char('$') => {
                                    // Rename session — reuse rename overlay
                                    renaming = true;
                                    rename_buf.clear();
                                    // Mark that we're renaming the session, not a window
                                    // We'll detect this by checking if pane_renaming is used as a flag
                                    session_renaming = true;
                                }
                                KeyCode::Char('?') => {
                                    // Build comprehensive help overlay from help.rs
                                    keys_viewer_scroll = 0;
                                    let user_binds: Vec<(bool, String, String, String)> = synced_bindings
                                        .iter()
                                        .map(|b| (b.r, b.t.clone(), b.k.clone(), b.c.clone()))
                                        .collect();
                                    keys_viewer_lines = help::build_overlay_lines(&user_binds);
                                    keys_viewer = true;
                                }
                                KeyCode::Char('t') => { cmd_batch.push("clock-mode\n".into()); }
                                KeyCode::Char('=') => { cmd_batch.push("choose-buffer\n".into()); }
                                KeyCode::Char(':') => { command_input = true; command_buf.clear(); command_cursor = 0; command_history_idx = command_history.len(); }
                                KeyCode::Char('\'') => { window_idx_input = true; window_idx_buf.clear(); }
                                KeyCode::Char('w') => {
                                    tree_chooser = true;
                                    tree_entries.clear();
                                    tree_selected = 0;
                                    tree_scroll = 0;
                                    // Query ALL sessions (like tmux choose-tree)
                                    let dir = format!("{}\\.psmux", home);
                                    if let Ok(entries) = std::fs::read_dir(&dir) {
                                        let mut sessions: Vec<(String, Vec<(usize, String, Vec<(usize, String)>)>)> = Vec::new();
                                        for e in entries.flatten() {
                                            if let Some(fname) = e.file_name().to_str().map(|s| s.to_string()) {
                                                if let Some((base, ext)) = fname.rsplit_once('.') {
                                                    if ext == "port" {
                                                        // Hide warm (standby) sessions from user
                                                        if crate::session::is_warm_session(base) { continue; }
                                                        if let Ok(port_str) = std::fs::read_to_string(e.path()) {
                                                            if let Ok(p) = port_str.trim().parse::<u16>() {
                                                                let sess_addr = format!("127.0.0.1:{}", p);
                                                                let sess_key = read_session_key(base).unwrap_or_default();
                                                                if let Ok(mut ss) = std::net::TcpStream::connect_timeout(
                                                                    &sess_addr.parse().unwrap(), Duration::from_millis(50)
                                                                ) {
                                                                    let _ = ss.set_read_timeout(Some(Duration::from_millis(100)));
                                                                    let _ = write!(ss, "AUTH {}\n", sess_key);
                                                                    let _ = ss.write_all(b"list-tree\n");
                                                                    let _ = ss.flush();
                                                                    let mut br = BufReader::new(ss);
                                                                    let mut al = String::new();
                                                                    let _ = br.read_line(&mut al); // AUTH OK
                                                                    let mut tree_line = String::new();
                                                                    if br.read_line(&mut tree_line).is_ok() {
                                                                        // Parse JSON array of WinTree
                                                                        if let Ok(wins) = serde_json::from_str::<Vec<WinTree>>(&tree_line.trim()) {
                                                                            let mut win_data = Vec::new();
                                                                            for w in &wins {
                                                                                let panes: Vec<(usize, String)> = w.panes.iter().map(|p| (p.id, p.title.clone())).collect();
                                                                                win_data.push((w.id, w.name.clone(), panes));
                                                                            }
                                                                            sessions.push((base.to_string(), win_data));
                                                                        }
                                                                    }
                                                                }
                                                            }
                                                        }
                                                    }
                                                }
                                            }
                                        }
                                        // Sort sessions: current session first, then alphabetical
                                        sessions.sort_by(|a, b| {
                                            if a.0 == current_session { std::cmp::Ordering::Less }
                                            else if b.0 == current_session { std::cmp::Ordering::Greater }
                                            else { a.0.cmp(&b.0) }
                                        });
                                        // Build tree entries: session > window > pane
                                        // tree_entries format: (is_win, id, sub_id, label, session_name)
                                        // For session headers: is_win=true with id=usize::MAX as sentinel
                                        // For windows: is_win=true
                                        // For panes: is_win=false
                                        for (sess_name, wins) in &sessions {
                                            let is_current = sess_name == &current_session;
                                            let attached = if is_current { " (attached)" } else { "" };
                                            let nw = wins.len();
                                            // Session header line
                                            tree_entries.push((true, usize::MAX, 0,
                                                format!("{}: {} windows{}", sess_name, nw, attached),
                                                sess_name.clone()));
                                            if is_current {
                                                // Show windows and panes for current session
                                                for (wi, (wid, wname, panes)) in wins.iter().enumerate() {
                                                    let flag = if panes.len() > 0 { "" } else { "" };
                                                    tree_entries.push((true, *wid, 0,
                                                        format!("  {}: {}{} ({} panes)", wi, wname, flag, panes.len()),
                                                        sess_name.clone()));
                                                    for (pid, ptitle) in panes {
                                                        tree_entries.push((false, *wid, *pid,
                                                            format!("    {}", ptitle),
                                                            sess_name.clone()));
                                                    }
                                                }
                                            } else {
                                                // Show windows for other sessions (collapsed)
                                                for (wi, (wid, wname, panes)) in wins.iter().enumerate() {
                                                    tree_entries.push((true, *wid, 0,
                                                        format!("  {}: {} ({} panes)", wi, wname, panes.len()),
                                                        sess_name.clone()));
                                                }
                                            }
                                        }
                                    }
                                    // Fallback: if no sessions found, use current session data
                                    if tree_entries.is_empty() {
                                        for wi in &last_tree {
                                            tree_entries.push((true, wi.id, 0, wi.name.clone(), current_session.clone()));
                                            for pi in &wi.panes {
                                                tree_entries.push((false, wi.id, pi.id, pi.title.clone(), current_session.clone()));
                                            }
                                        }
                                    }
                                }
                                KeyCode::Char('s') => {
                                    session_chooser = true;
                                    session_entries.clear();
                                    session_selected = 0;
                                    let dir = format!("{}\\.psmux", home);
                                    if let Ok(entries) = std::fs::read_dir(&dir) {
                                        for e in entries.flatten() {
                                            if let Some(fname) = e.file_name().to_str() {
                                                if let Some((base, ext)) = fname.rsplit_once('.') {
                                                    if ext == "port" {
                                                        if crate::session::is_warm_session(base) { continue; }
                                                        if let Ok(port_str) = std::fs::read_to_string(e.path()) {
                                                            if let Ok(p) = port_str.trim().parse::<u16>() {
                                                                let sess_addr = format!("127.0.0.1:{}", p);
                                                                let sess_key = read_session_key(base).unwrap_or_default();
                                                                let info = if let Ok(mut ss) = std::net::TcpStream::connect_timeout(
                                                                    &sess_addr.parse().unwrap(), Duration::from_millis(25)
                                                                ) {
                                                                    let _ = ss.set_read_timeout(Some(Duration::from_millis(25)));
                                                                    let _ = write!(ss, "AUTH {}\n", sess_key);
                                                                    let _ = ss.write_all(b"session-info\n");
                                                                    let mut br = BufReader::new(ss);
                                                                    let mut al = String::new();
                                                                    let _ = br.read_line(&mut al);
                                                                    let mut line = String::new();
                                                                    if br.read_line(&mut line).is_ok() && !line.trim().is_empty() {
                                                                        line.trim().to_string()
                                                                    } else {
                                                                        format!("{}: (no info)", base)
                                                                    }
                                                                } else {
                                                                    format!("{}: (not responding)", base)
                                                                };
                                                                session_entries.push((base.to_string(), info));
                                                            }
                                                        }
                                                    }
                                                }
                                            }
                                        }
                                    }
                                    if session_entries.is_empty() {
                                        session_entries.push((current_session.clone(), format!("{}: (current)", current_session)));
                                    }
                                    for (i, (sname, _)) in session_entries.iter().enumerate() {
                                        if sname == &current_session { session_selected = i; break; }
                                    }
                                }
                                KeyCode::Char('q') => { cmd_batch.push("display-panes\n".into()); }
                                KeyCode::Char('v') => { cmd_batch.push("rectangle-toggle\n".into()); }
                                KeyCode::Char('y') => { cmd_batch.push("copy-yank\n".into()); }
                                // Session navigation (like tmux prefix+( and prefix+))
                                KeyCode::Char('(') | KeyCode::Char(')') => {
                                    let dir_next = key.code == KeyCode::Char(')');
                                    // Enumerate sessions
                                    let dir = format!("{}\\.psmux", home);
                                    let mut names: Vec<String> = Vec::new();
                                    if let Ok(entries) = std::fs::read_dir(&dir) {
                                        for e in entries.flatten() {
                                            if let Some(fname) = e.file_name().to_str() {
                                                if let Some((base, ext)) = fname.rsplit_once('.') {
                                                    if ext == "port" {
                                                        if crate::session::is_warm_session(base) { continue; }
                                                        if let Ok(ps) = std::fs::read_to_string(e.path()) {
                                                            if let Ok(p) = ps.trim().parse::<u16>() {
                                                                let a = format!("127.0.0.1:{}", p);
                                                                if std::net::TcpStream::connect_timeout(
                                                                    &a.parse().unwrap(), Duration::from_millis(25)
                                                                ).is_ok() {
                                                                    names.push(base.to_string());
                                                                }
                                                            }
                                                        }
                                                    }
                                                }
                                            }
                                        }
                                    }
                                    names.sort();
                                    if names.len() > 1 {
                                        if let Some(cur_pos) = names.iter().position(|n| *n == current_session) {
                                            let next_pos = if dir_next {
                                                (cur_pos + 1) % names.len()
                                            } else {
                                                (cur_pos + names.len() - 1) % names.len()
                                            };
                                            let next_name = names[next_pos].clone();
                                            cmd_batch.push("client-detach\n".into());
                                            env::set_var("PSMUX_SWITCH_TO", &next_name);
                                            quit = true;
                                        }
                                    }
                                }
                                // Meta+1..5 preset layouts (like tmux)
                                KeyCode::Char('1') if key.modifiers.contains(KeyModifiers::ALT) => { cmd_batch.push("select-layout even-horizontal\n".into()); }
                                KeyCode::Char('2') if key.modifiers.contains(KeyModifiers::ALT) => { cmd_batch.push("select-layout even-vertical\n".into()); }
                                KeyCode::Char('3') if key.modifiers.contains(KeyModifiers::ALT) => { cmd_batch.push("select-layout main-horizontal\n".into()); }
                                KeyCode::Char('4') if key.modifiers.contains(KeyModifiers::ALT) => { cmd_batch.push("select-layout main-vertical\n".into()); }
                                KeyCode::Char('5') if key.modifiers.contains(KeyModifiers::ALT) => { cmd_batch.push("select-layout tiled\n".into()); }
                                // Display pane info
                                KeyCode::Char('i') => { cmd_batch.push("display-message\n".into()); }
                                _ => {
                                    // No default binding for this key (user bindings already checked above)
                                }
                            }
                            } // end of else (no user binding override)
                            // Arrow keys are repeatable by default (tmux -r flag).
                            // User-defined bindings also respect the repeat flag.
                            let is_repeatable_default = matches!(key.code,
                                KeyCode::Up | KeyCode::Down | KeyCode::Left | KeyCode::Right
                            );
                            let is_user_repeat = user_binding.map_or(false, |e| e.r);
                            if is_repeatable_default || is_user_repeat {
                                prefix_armed_at = Instant::now();
                                prefix_repeating = true;
                            } else {
                                prefix_armed = false;
                                prefix_repeating = false;
                                cmd_batch.push("prefix-end\n".into());
                            }
                        } else {
                            match key.code {
                                KeyCode::Up if session_chooser => { if session_selected > 0 { session_selected -= 1; } }
                                KeyCode::Down if session_chooser => { if session_selected + 1 < session_entries.len() { session_selected += 1; } }
                                KeyCode::Enter if session_chooser => {
                                    if let Some((sname, _)) = session_entries.get(session_selected) {
                                        if sname != &current_session {
                                            cmd_batch.push("client-detach\n".into());
                                            env::set_var("PSMUX_SWITCH_TO", sname);
                                            quit = true;
                                        }
                                        session_chooser = false;
                                    }
                                }
                                KeyCode::Esc if session_chooser => { session_chooser = false; }
                                KeyCode::Char('x') if session_chooser => {
                                    // Kill the selected session (like tmux session chooser)
                                    if let Some((sname, _)) = session_entries.get(session_selected) {
                                        let sname = sname.clone();
                                        if sname == current_session {
                                            // Killing current session — exit after kill
                                            cmd_batch.push("kill-session\n".into());
                                            session_chooser = false;
                                            quit = true;
                                        } else {
                                            // Kill another session by connecting to it
                                            let h = env::var("USERPROFILE").or_else(|_| env::var("HOME")).unwrap_or_default();
                                            let port_path = format!("{}\\.psmux\\{}.port", h, sname);
                                            let key_path = format!("{}\\.psmux\\{}.key", h, sname);
                                            if let Ok(port_str) = std::fs::read_to_string(&port_path) {
                                                if let Ok(port) = port_str.trim().parse::<u16>() {
                                                    let addr = format!("127.0.0.1:{}", port);
                                                    let sess_key = std::fs::read_to_string(&key_path).unwrap_or_default();
                                                    if let Ok(mut ss) = std::net::TcpStream::connect_timeout(
                                                        &addr.parse().unwrap(), Duration::from_millis(100)
                                                    ) {
                                                        let _ = write!(ss, "AUTH {}\n", sess_key.trim());
                                                        let _ = ss.write_all(b"kill-session\n");
                                                    }
                                                }
                                            }
                                            // Remove the killed session from the list
                                            session_entries.remove(session_selected);
                                            if session_selected >= session_entries.len() && session_selected > 0 {
                                                session_selected -= 1;
                                            }
                                            if session_entries.is_empty() {
                                                session_chooser = false;
                                            }
                                        }
                                    }
                                }
                                KeyCode::Up if tree_chooser => { if tree_selected > 0 { tree_selected -= 1; } }
                                KeyCode::Down if tree_chooser => { if tree_selected + 1 < tree_entries.len() { tree_selected += 1; } }
                                KeyCode::Enter if tree_chooser => {
                                    if let Some((is_win, wid, pid, _label, sess_name)) = tree_entries.get(tree_selected) {
                                        if *wid == usize::MAX {
                                            // Session header — switch to that session
                                            if *sess_name != current_session {
                                                cmd_batch.push("client-detach\n".into());
                                                env::set_var("PSMUX_SWITCH_TO", sess_name);
                                                quit = true;
                                            }
                                            tree_chooser = false;
                                        } else if *sess_name != current_session {
                                            // Window/pane in another session — switch to that session
                                            cmd_batch.push("client-detach\n".into());
                                            env::set_var("PSMUX_SWITCH_TO", sess_name);
                                            quit = true;
                                            tree_chooser = false;
                                        } else if *is_win {
                                            cmd_batch.push(format!("focus-window {}\n", wid));
                                            tree_chooser = false;
                                        } else {
                                            cmd_batch.push(format!("focus-pane {}\n", pid));
                                            tree_chooser = false;
                                        }
                                    }
                                }
                                KeyCode::Esc if tree_chooser => { tree_chooser = false; }
                                // --- list-keys viewer (C-b ?) ---
                                KeyCode::Up if keys_viewer => { if keys_viewer_scroll > 0 { keys_viewer_scroll -= 1; } }
                                KeyCode::Down if keys_viewer => { keys_viewer_scroll += 1; }
                                KeyCode::PageUp if keys_viewer => { keys_viewer_scroll = keys_viewer_scroll.saturating_sub(20); }
                                KeyCode::PageDown if keys_viewer => { keys_viewer_scroll += 20; }
                                KeyCode::Home if keys_viewer => { keys_viewer_scroll = 0; }
                                KeyCode::End if keys_viewer => { keys_viewer_scroll = keys_viewer_lines.len().saturating_sub(1); }
                                KeyCode::Char('q') if keys_viewer => { keys_viewer = false; }
                                KeyCode::Esc if keys_viewer => { keys_viewer = false; }
                                KeyCode::Char('k') if keys_viewer => { if keys_viewer_scroll > 0 { keys_viewer_scroll -= 1; } }
                                KeyCode::Char('j') if keys_viewer => { keys_viewer_scroll += 1; }
                                // --- kill confirmation: y/Y/Enter confirms, n/N/Esc cancels ---
                                KeyCode::Char('y') | KeyCode::Char('Y') | KeyCode::Enter if confirm_cmd.is_some() => {
                                    if let Some(cmd) = confirm_cmd.take() {
                                        cmd_batch.push(format!("{}\n", cmd));
                                    }
                                }
                                KeyCode::Char('n') | KeyCode::Char('N') | KeyCode::Esc if confirm_cmd.is_some() => {
                                    confirm_cmd = None;
                                }
                                KeyCode::Char(c) if renaming && !key.modifiers.contains(KeyModifiers::CONTROL) => { rename_buf.push(c); }
                                KeyCode::Char(c) if pane_renaming && !key.modifiers.contains(KeyModifiers::CONTROL) => { pane_title_buf.push(c); }
                                KeyCode::Char(c) if window_idx_input && c.is_ascii_digit() => { window_idx_buf.push(c); }
                                KeyCode::Char(c) if command_input && !key.modifiers.contains(KeyModifiers::CONTROL) => { command_buf.insert(command_cursor, c); command_cursor += 1; }
                                KeyCode::Backspace if renaming => { let _ = rename_buf.pop(); }
                                KeyCode::Backspace if pane_renaming => { let _ = pane_title_buf.pop(); }
                                KeyCode::Backspace if window_idx_input => { let _ = window_idx_buf.pop(); }
                                KeyCode::Backspace if command_input => { if command_cursor > 0 { command_buf.remove(command_cursor - 1); command_cursor -= 1; } }
                                KeyCode::Enter if renaming => {
                                    if session_renaming {
                                        cmd_batch.push(format!("rename-session {}\n", quote_arg(&rename_buf)));
                                        session_renaming = false;
                                    } else {
                                        cmd_batch.push(format!("rename-window {}\n", quote_arg(&rename_buf)));
                                    }
                                    renaming = false;
                                }
                                KeyCode::Enter if pane_renaming => { cmd_batch.push(format!("set-pane-title {}\n", quote_arg(&pane_title_buf))); pane_renaming = false; }
                                KeyCode::Enter if window_idx_input => {
                                    if !window_idx_buf.is_empty() {
                                        cmd_batch.push(format!("select-window -t :{}\n", window_idx_buf));
                                    }
                                    window_idx_input = false;
                                }
                                KeyCode::Enter if command_input => {
                                    let trimmed = command_buf.trim().to_string();
                                    if !trimmed.is_empty() {
                                        command_history.push(trimmed.clone());
                                        command_history_idx = command_history.len();
                                        cmd_batch.push(format!("{}\n", trimmed));
                                    }
                                    command_input = false;
                                    command_cursor = 0;
                                }
                                KeyCode::Esc if renaming => { renaming = false; session_renaming = false; }
                                KeyCode::Esc if pane_renaming => { pane_renaming = false; }
                                KeyCode::Esc if window_idx_input => { window_idx_input = false; }
                                KeyCode::Esc if command_input => { command_input = false; command_cursor = 0; }

                                // Command prompt: cursor movement, history, and editing keys
                                KeyCode::Left if command_input => { if command_cursor > 0 { command_cursor -= 1; } }
                                KeyCode::Right if command_input => { if command_cursor < command_buf.len() { command_cursor += 1; } }
                                KeyCode::Home if command_input => { command_cursor = 0; }
                                KeyCode::End if command_input => { command_cursor = command_buf.len(); }
                                KeyCode::Delete if command_input => { if command_cursor < command_buf.len() { command_buf.remove(command_cursor); } }
                                KeyCode::Up if command_input => {
                                    if command_history_idx > 0 {
                                        command_history_idx -= 1;
                                        command_buf = command_history[command_history_idx].clone();
                                        command_cursor = command_buf.len();
                                    }
                                }
                                KeyCode::Down if command_input => {
                                    if command_history_idx < command_history.len() {
                                        command_history_idx += 1;
                                        command_buf = if command_history_idx < command_history.len() {
                                            command_history[command_history_idx].clone()
                                        } else {
                                            String::new()
                                        };
                                        command_cursor = command_buf.len();
                                    }
                                }
                                KeyCode::Char('a') if command_input && key.modifiers.contains(KeyModifiers::CONTROL) => { command_cursor = 0; }
                                KeyCode::Char('e') if command_input && key.modifiers.contains(KeyModifiers::CONTROL) => { command_cursor = command_buf.len(); }
                                KeyCode::Char('u') if command_input && key.modifiers.contains(KeyModifiers::CONTROL) => {
                                    command_buf.drain(..command_cursor);
                                    command_cursor = 0;
                                }
                                KeyCode::Char('k') if command_input && key.modifiers.contains(KeyModifiers::CONTROL) => {
                                    command_buf.truncate(command_cursor);
                                }
                                KeyCode::Char('w') if command_input && key.modifiers.contains(KeyModifiers::CONTROL) => {
                                    let mut pos = command_cursor;
                                    while pos > 0 && command_buf.as_bytes().get(pos - 1) == Some(&b' ') { pos -= 1; }
                                    while pos > 0 && command_buf.as_bytes().get(pos - 1) != Some(&b' ') { pos -= 1; }
                                    command_buf.drain(pos..command_cursor);
                                    command_cursor = pos;
                                }

                                KeyCode::Char(' ') => {
                                    #[cfg(windows)]
                                    {
                                        paste_pend.push(' ');
                                        if paste_pend_start.is_none() {
                                            paste_pend_start = Some(Instant::now());
                                        }
                                    }
                                    #[cfg(not(windows))]
                                    {
                                        cmd_batch.push("send-key space\n".into());
                                    }
                                }
                                // AltGr detection: On Windows, AltGr is reported as
                                // Ctrl+Alt.  Non-lowercase-letter chars with Ctrl+Alt
                                // are AltGr-produced (e.g. \ @ { } [ ] | ~ on
                                // German/Czech keyboards) — treat as plain text.
                                KeyCode::Char(c) if key.modifiers.contains(KeyModifiers::CONTROL)
                                    && key.modifiers.contains(KeyModifiers::ALT)
                                    && !c.is_ascii_lowercase() => {
                                    #[cfg(windows)]
                                    {
                                        paste_pend.push(c);
                                        if paste_pend_start.is_none() {
                                            paste_pend_start = Some(Instant::now());
                                        }
                                    }
                                    #[cfg(not(windows))]
                                    {
                                        let escaped = match c {
                                            '"' => "\\\"".to_string(),
                                            '\\' => "\\\\".to_string(),
                                            _ => c.to_string(),
                                        };
                                        cmd_batch.push(format!("send-text \"{}\"\n", escaped));
                                    }
                                }
                                KeyCode::Char(c) if key.modifiers.contains(KeyModifiers::CONTROL) && key.modifiers.contains(KeyModifiers::ALT) => {
                                    cmd_batch.push(format!("send-key C-M-{}\n", c.to_ascii_lowercase()));
                                }
                                KeyCode::Char(c) if key.modifiers.contains(KeyModifiers::ALT) => {
                                    cmd_batch.push(format!("send-key M-{}\n", c));
                                }
                                KeyCode::Char(c) if key.modifiers.contains(KeyModifiers::CONTROL) => {
                                    cmd_batch.push(format!("send-key C-{}\n", c.to_ascii_lowercase()));
                                }
                                KeyCode::Char(c) if (c as u32) >= 0x01 && (c as u32) <= 0x1A => {
                                    let ctrl_letter = ((c as u8) + b'a' - 1) as char;
                                    cmd_batch.push(format!("send-key C-{}\n", ctrl_letter));
                                }
                                KeyCode::Char(c) => {
                                    #[cfg(windows)]
                                    {
                                        // Suppress text key events during the post-copy
                                        // suppression window (VS Code ConPTY duplicate).
                                        let suppressed = paste_suppress_until
                                            .map_or(false, |t| Instant::now() < t);
                                        if suppressed {
                                            if input_log_enabled() {
                                                input_log("paste", &format!("suppressed char '{}' during paste suppress window", c));
                                            }
                                        } else {
                                            paste_suppress_until = None;
                                            paste_pend.push(c);
                                            if paste_pend_start.is_none() {
                                                paste_pend_start = Some(Instant::now());
                                            }
                                        }
                                    }
                                    #[cfg(not(windows))]
                                    {
                                        let escaped = match c {
                                            '"' => "\\\"".to_string(),
                                            '\\' => "\\\\".to_string(),
                                            _ => c.to_string(),
                                        };
                                        cmd_batch.push(format!("send-text \"{}\"\n", escaped));
                                    }
                                }
                                KeyCode::Enter => {
                                    #[cfg(windows)]
                                    {
                                        if !paste_pend.is_empty() {
                                            paste_pend.push('\n');
                                        } else {
                                            cmd_batch.push(format!("send-key {}\n", modified_key_name("Enter", key.modifiers)));
                                        }
                                    }
                                    #[cfg(not(windows))]
                                    { cmd_batch.push(format!("send-key {}\n", modified_key_name("Enter", key.modifiers))); }
                                }
                                KeyCode::Tab => {
                                    #[cfg(windows)]
                                    {
                                        if !paste_pend.is_empty() {
                                            paste_pend.push('\t');
                                        } else {
                                            cmd_batch.push("send-key tab\n".into());
                                        }
                                    }
                                    #[cfg(not(windows))]
                                    { cmd_batch.push("send-key tab\n".into()); }
                                }
                                KeyCode::BackTab => { cmd_batch.push("send-key btab\n".into()); }
                                KeyCode::Backspace => { cmd_batch.push("send-key backspace\n".into()); }
                                KeyCode::Delete => { cmd_batch.push(format!("send-key {}\n", modified_key_name("Delete", key.modifiers))); }
                                KeyCode::Esc => { cmd_batch.push("send-key esc\n".into()); }
                                KeyCode::Left => { cmd_batch.push(format!("send-key {}\n", modified_key_name("Left", key.modifiers))); }
                                KeyCode::Right => { cmd_batch.push(format!("send-key {}\n", modified_key_name("Right", key.modifiers))); }
                                KeyCode::Up => { cmd_batch.push(format!("send-key {}\n", modified_key_name("Up", key.modifiers))); }
                                KeyCode::Down => { cmd_batch.push(format!("send-key {}\n", modified_key_name("Down", key.modifiers))); }
                                KeyCode::PageUp => { cmd_batch.push(format!("send-key {}\n", modified_key_name("PageUp", key.modifiers))); }
                                KeyCode::PageDown => { cmd_batch.push(format!("send-key {}\n", modified_key_name("PageDown", key.modifiers))); }
                                KeyCode::Home => { cmd_batch.push(format!("send-key {}\n", modified_key_name("Home", key.modifiers))); }
                                KeyCode::End => { cmd_batch.push(format!("send-key {}\n", modified_key_name("End", key.modifiers))); }
                                KeyCode::Insert => { cmd_batch.push(format!("send-key {}\n", modified_key_name("Insert", key.modifiers))); }
                                KeyCode::F(n) => { cmd_batch.push(format!("send-key {}\n", modified_key_name(&format!("F{}", n), key.modifiers))); }
                                _ => {}
                            }
                        }
                    }
                    Event::Paste(data) => {
                        let encoded = base64_encode(&data);
                        cmd_batch.push(format!("send-paste {}\n", encoded));
                    }
                    Event::Mouse(me) => {
                        use crossterm::event::{MouseEventKind, MouseButton};
                        match me.kind {
                            MouseEventKind::Down(MouseButton::Left) => {
                                // Status bar tab click
                                if me.row == client_status_row {
                                    let mut clicked_tab: Option<usize> = None;
                                    for &(win_idx, x_start, x_end) in &client_tab_positions {
                                        if me.column >= x_start && me.column < x_end {
                                            clicked_tab = Some(win_idx);
                                            break;
                                        }
                                    }
                                    if let Some(idx) = clicked_tab {
                                        let display_idx = idx + client_base_index;
                                        cmd_batch.push(format!("select-window -t :{}\n", display_idx));
                                    }
                                } else {
                                    // Border detection
                                    let mut on_border = false;
                                    if !client_zoomed {
                                        let tol = 0u16;
                                        for (bpath, bkind, bidx, bpos, btotal, bsizes, barea) in &client_borders {
                                            let hit = if bkind == "Horizontal" {
                                                me.column >= bpos.saturating_sub(tol) && me.column <= bpos + tol
                                                && me.row >= barea.y && me.row < barea.y + barea.height
                                            } else {
                                                me.row >= bpos.saturating_sub(tol) && me.row <= bpos + tol
                                                && me.column >= barea.x && me.column < barea.x + barea.width
                                            };
                                            if hit {
                                                client_drag = Some(ClientDragState {
                                                    path: bpath.clone(),
                                                    kind: bkind.clone(),
                                                    index: *bidx,
                                                    start_pos: if bkind == "Horizontal" { me.column } else { me.row },
                                                    initial_sizes: bsizes.clone(),
                                                    total_pixels: *btotal,
                                                });
                                                border_drag = true;
                                                on_border = true;
                                                rsel_start = None;
                                                rsel_end = None;
                                                selection_changed = true;
                                                break;
                                            }
                                        }
                                    }

                                    if !on_border {
                                        let clicked_pane = client_pane_rects.iter().find(|(_, rect)| {
                                            rect.contains(ratatui::layout::Position { x: me.column, y: me.row })
                                        });

                                        if let Some(&(pane_id, pane_rect)) = clicked_pane {
                                            cmd_batch.push(format!("select-pane -t %{}\n", pane_id));
                                            let rel_col = me.column as i16 - pane_rect.x as i16;
                                            let rel_row = me.row as i16 - pane_rect.y as i16;

                                            if client_copy_mode {
                                                cmd_batch.push(format!("pane-mouse {} 0 {} {} M\n",
                                                    pane_id, rel_col, rel_row));
                                                rsel_start = None;
                                                rsel_end = None;
                                                selection_changed = true;
                                            } else {
                                                cmd_batch.push(format!("pane-mouse {} 0 {} {} M\n",
                                                    pane_id, rel_col, rel_row));
                                                border_drag = false;
                                                rsel_start = Some((me.column, me.row));
                                                rsel_end = Some((me.column, me.row));
                                                rsel_dragged = false;
                                                selection_changed = true;
                                            }
                                        } else {
                                            cmd_batch.push(format!("mouse-down {} {}\n", me.column, me.row));
                                        }
                                    }
                                }
                            }
                            MouseEventKind::Down(MouseButton::Right) => {
                                // Check if active pane is running a TUI app (alternate screen).
                                // TUI apps (htop, Claude Code, etc.) expect right-click as a
                                // mouse event, NOT clipboard paste.
                                let tui_active = if !prev_dump_buf.is_empty() {
                                    serde_json::from_str::<DumpState>(&prev_dump_buf)
                                        .map(|s| active_pane_in_alt_screen(&s.layout))
                                        .unwrap_or(false)
                                } else { false };

                                if tui_active {
                                    // Forward right-click as pane-relative mouse event
                                    if let Some(&(pane_id, pane_rect)) = client_pane_rects.iter().find(|(_, r)| {
                                        r.contains(ratatui::layout::Position { x: me.column, y: me.row })
                                    }) {
                                        let rel_col = me.column as i16 - pane_rect.x as i16;
                                        let rel_row = me.row as i16 - pane_rect.y as i16;
                                        cmd_batch.push(format!("pane-mouse {} 2 {} {} M\n",
                                            pane_id, rel_col, rel_row));
                                    }
                                    rsel_start = None;
                                    rsel_end = None;
                                    selection_changed = true;
                                } else if rsel_start.is_some() && rsel_dragged {
                                    // pwsh-style: right-click with active selection → copy + clear
                                    if let (Some(s), Some(e)) = (rsel_start, rsel_end) {
                                        if let Ok(state) = serde_json::from_str::<DumpState>(&prev_dump_buf) {
                                            let text = extract_selection_text(
                                                &state.layout,
                                                last_sent_size.0,
                                                last_sent_size.1,
                                                s, e,
                                            );
                                            if !text.is_empty() {
                                                copy_to_system_clipboard(&text);
                                                pending_osc52 = Some(text);
                                            }
                                        }
                                    }
                                    rsel_start = None;
                                    rsel_end = None;
                                    rsel_dragged = false;
                                    selection_changed = true;
                                    // Suppress text key events that VS Code's ConPTY
                                    // injects after a right-click copy action.
                                    paste_suppress_until = Some(Instant::now() + Duration::from_secs(2));
                                } else {
                                    // No selection, no TUI — paste from clipboard (pwsh-style)
                                    rsel_start = None;
                                    rsel_end = None;
                                    selection_changed = true;
                                    if let Some(text) = read_from_system_clipboard() {
                                        if !text.is_empty() {
                                            let encoded = base64_encode(&text);
                                            cmd_batch.push(format!("send-paste {}\n", encoded));
                                        }
                                    }
                                }
                            }
                            MouseEventKind::Down(MouseButton::Middle) => {
                                if let Some(&(pane_id, pane_rect)) = client_pane_rects.iter().find(|(_, r)| {
                                    r.contains(ratatui::layout::Position { x: me.column, y: me.row })
                                }) {
                                    let rel_col = me.column as i16 - pane_rect.x as i16;
                                    let rel_row = me.row as i16 - pane_rect.y as i16;
                                    cmd_batch.push(format!("pane-mouse {} 1 {} {} M\n",
                                        pane_id, rel_col, rel_row));
                                } else {
                                    cmd_batch.push(format!("mouse-down-middle {} {}\n", me.column, me.row));
                                }
                            }
                            MouseEventKind::Drag(MouseButton::Left) => {
                                if border_drag {
                                    if let Some(ref d) = client_drag {
                                        let current_pos = if d.kind == "Horizontal" { me.column } else { me.row };
                                        let pixel_delta = current_pos as i32 - d.start_pos as i32;
                                        let total_pct: i32 = d.initial_sizes.iter().map(|&s| s as i32).sum();
                                        let total_px = d.total_pixels.max(1) as i32;
                                        let pct_delta = (pixel_delta * total_pct) / total_px;
                                        let min_pct = 5i32;

                                        let mut new_sizes = d.initial_sizes.clone();
                                        let left = (d.initial_sizes[d.index] as i32 + pct_delta)
                                            .clamp(min_pct, d.initial_sizes[d.index] as i32 + d.initial_sizes[d.index + 1] as i32 - min_pct) as u16;
                                        let right = d.initial_sizes[d.index] + d.initial_sizes[d.index + 1] - left;
                                        new_sizes[d.index] = left;
                                        new_sizes[d.index + 1] = right;

                                        let path_str = if d.path.is_empty() { "_".to_string() } else { d.path.iter().map(|i| i.to_string()).collect::<Vec<_>>().join(".") };
                                        let sizes_str = new_sizes.iter().map(|s| s.to_string()).collect::<Vec<_>>().join(",");
                                        cmd_batch.push(format!("split-sizes {} {}\n", path_str, sizes_str));
                                    }
                                } else if rsel_start.is_none() {
                                    if client_copy_mode {
                                        if let Some(&(pane_id, pane_rect)) = client_pane_rects.iter().find(|(_, r)| {
                                            r.contains(ratatui::layout::Position { x: me.column, y: me.row })
                                        }) {
                                            let rel_col = me.column as i16 - pane_rect.x as i16;
                                            let rel_row = me.row as i16 - pane_rect.y as i16;
                                            cmd_batch.push(format!("pane-mouse {} 32 {} {} M\n",
                                                pane_id, rel_col, rel_row));
                                        }
                                    } else {
                                        cmd_batch.push(format!("mouse-drag {} {}\n", me.column, me.row));
                                    }
                                } else {
                                    if rsel_start.is_some() {
                                        rsel_end = Some((me.column, me.row));
                                        rsel_dragged = true;
                                        selection_changed = true;
                                    }
                                }
                            }
                            MouseEventKind::Drag(MouseButton::Right) => {}
                            MouseEventKind::Up(MouseButton::Left) => {
                                if border_drag {
                                    cmd_batch.push(format!("split-resize-done\n"));
                                    border_drag = false;
                                    client_drag = None;
                                } else if rsel_dragged {
                                    rsel_end = Some((me.column, me.row));
                                    if let (Some(s), Some(e)) = (rsel_start, rsel_end) {
                                        if let Ok(state) = serde_json::from_str::<DumpState>(&prev_dump_buf) {
                                            let text = extract_selection_text(
                                                &state.layout,
                                                last_sent_size.0,
                                                last_sent_size.1,
                                                s, e,
                                            );
                                            if !text.is_empty() {
                                                copy_to_system_clipboard(&text);
                                                pending_osc52 = Some(text);
                                            }
                                        }
                                    }
                                    rsel_start = None;
                                    rsel_end = None;
                                    rsel_dragged = false;
                                    selection_changed = true;
                                } else {
                                    rsel_start = None;
                                    rsel_end = None;
                                    selection_changed = true;
                                    if client_copy_mode {
                                        if let Some(&(pane_id, pane_rect)) = client_pane_rects.iter().find(|(_, r)| {
                                            r.contains(ratatui::layout::Position { x: me.column, y: me.row })
                                        }) {
                                            let rel_col = me.column as i16 - pane_rect.x as i16;
                                            let rel_row = me.row as i16 - pane_rect.y as i16;
                                            cmd_batch.push(format!("pane-mouse {} 0 {} {} m\n",
                                                pane_id, rel_col, rel_row));
                                        }
                                    } else {
                                        cmd_batch.push(format!("mouse-up {} {}\n", me.column, me.row));
                                    }
                                }
                            }
                            MouseEventKind::Up(MouseButton::Right) => {}
                            MouseEventKind::Up(MouseButton::Middle) => {}
                            MouseEventKind::Moved => {
                                // Detect border hover for visual preview
                                let mut new_hover: Option<(u16, String, Rect)> = None;
                                if !client_zoomed {
                                    let tol = 0u16;
                                    for (_, bkind, _, bpos, _, _, barea) in &client_borders {
                                        let hit = if bkind == "Horizontal" {
                                            me.column >= bpos.saturating_sub(tol) && me.column <= bpos + tol
                                            && me.row >= barea.y && me.row < barea.y + barea.height
                                        } else {
                                            me.row >= bpos.saturating_sub(tol) && me.row <= bpos + tol
                                            && me.column >= barea.x && me.column < barea.x + barea.width
                                        };
                                        if hit {
                                            new_hover = Some((*bpos, bkind.clone(), *barea));
                                            break;
                                        }
                                    }
                                }
                                if new_hover != hovered_border {
                                    hovered_border = new_hover;
                                    selection_changed = true; // trigger redraw
                                }
                                // Forward hover to PTY
                                if let Some(&(pane_id, pane_rect)) = client_pane_rects.iter().find(|(_, r)| {
                                    r.contains(ratatui::layout::Position { x: me.column, y: me.row })
                                }) {
                                    let rel_col = me.column as i16 - pane_rect.x as i16;
                                    let rel_row = me.row as i16 - pane_rect.y as i16;
                                    cmd_batch.push(format!("pane-mouse {} 35 {} {} M\n",
                                        pane_id, rel_col, rel_row));
                                } else {
                                    cmd_batch.push(format!("mouse-move {} {}\n", me.column, me.row));
                                }
                            }
                            MouseEventKind::ScrollUp => {
                                rsel_start = None;
                                rsel_end = None;
                                rsel_dragged = false;
                                selection_changed = true;
                                if let Some(&(pane_id, _)) = client_pane_rects.iter().find(|(_, r)| {
                                    r.contains(ratatui::layout::Position { x: me.column, y: me.row })
                                }) {
                                    cmd_batch.push(format!("pane-scroll {} up\n", pane_id));
                                } else {
                                    cmd_batch.push(format!("scroll-up {} {}\n", me.column, me.row));
                                }
                            }
                            MouseEventKind::ScrollDown => {
                                rsel_start = None;
                                rsel_end = None;
                                rsel_dragged = false;
                                selection_changed = true;
                                if let Some(&(pane_id, _)) = client_pane_rects.iter().find(|(_, r)| {
                                    r.contains(ratatui::layout::Position { x: me.column, y: me.row })
                                }) {
                                    cmd_batch.push(format!("pane-scroll {} down\n", pane_id));
                                } else {
                                    cmd_batch.push(format!("scroll-down {} {}\n", me.column, me.row));
                                }
                            }
                            _ => {}
                        }
                    }
                    Event::FocusGained => {
                        cmd_batch.push("focus-in\n".into());
                    }
                    Event::FocusLost => {
                        cmd_batch.push("focus-out\n".into());
                    }
                    _ => {}
                }
                if quit { break; }
                _pending_evt = input.try_read()?;
            }
        }
        if quit { break; }

        // ── Windows paste buffer flush (post-event) ────────────────────
        // If Ctrl+V Release was seen in this iteration AND we have pending
        // chars, immediately send as send-paste (don't wait for top-of-loop).
        #[cfg(windows)]
        {
            if paste_confirmed && !paste_pend.is_empty() {
                if input_log_enabled() {
                    input_log("paste", &format!("paste CONFIRMED (post-event), sending {} chars as send-paste: {:?}",
                        paste_pend.len(), &paste_pend.chars().take(200).collect::<String>()));
                }
                let encoded = base64_encode(&paste_pend);
                cmd_batch.push(format!("send-paste {}\n", encoded));
                paste_pend.clear();
                paste_pend_start = None;
                paste_stage2 = false;
                paste_confirmed = false;
            } else if paste_confirmed && paste_pend.is_empty() {
                // Ctrl+V with no buffered chars — read clipboard as fallback
                if let Some(text) = read_from_system_clipboard() {
                    if !text.is_empty() {
                        if input_log_enabled() {
                            input_log("paste", &format!("paste CONFIRMED (no buffer), clipboard read len={}", text.len()));
                        }
                        let encoded = base64_encode(&text);
                        cmd_batch.push(format!("send-paste {}\n", encoded));
                    }
                }
                paste_confirmed = false;
            }
        }

        // ── STEP 2: Send commands immediately, refresh screen at capped rate ──
        // Send client-size if changed
        let mut size_changed = false;
        {
            let ts = terminal.size()?;
            let new_size = (ts.width, ts.height.saturating_sub(last_status_lines));
            if new_size != last_sent_size {
                last_sent_size = new_size;
                size_changed = true;
                if writer.write_all(format!("client-size {} {}\n", new_size.0, new_size.1).as_bytes()).is_err() {
                    break; // Connection lost
                }
                // SSH: re-send mouse-enable on resize — terminal may reset
                // mouse reporting mode after a window size change.
                if is_ssh_mode {
                    crate::ssh_input::send_mouse_enable();
                    last_mouse_enable = Instant::now();
                }
            }
        }

        // Send all batched commands immediately — keys reach the server
        // without waiting for a dump-state round-trip
        let sent_keys_this_iter = !cmd_batch.is_empty();
        if sent_keys_this_iter {
            if input_log_enabled() {
                for cmd in &cmd_batch {
                    input_log("send", &format!("{}", cmd.trim()));
                }
            }
            for cmd in &cmd_batch {
                if writer.write_all(cmd.as_bytes()).is_err() {
                    break; // Connection lost
                }
            }
            let _ = writer.flush(); // push keys to server NOW
            last_key_send_time = Some(Instant::now());
            key_send_instant = Some(Instant::now());
            // Force immediate dump-state so we start the echo-detection
            // polling chain right away (eliminates 0-10ms initial wait).
            force_dump = true;
        }

        // ── STEP 2b: Request screen update (non-blocking) ────────────────
        // Rate-limit dump-state requests to avoid flooding the server.
        // dump_in_flight prevents >1 concurrent request; the interval check
        // ensures we don't re-request faster than ~100fps when typing.
        let overlays_active = command_input || renaming || pane_renaming || tree_chooser || session_chooser || keys_viewer || confirm_cmd.is_some() || srv_popup_active || srv_confirm_active || srv_menu_active || srv_display_panes || clock_active;
        let should_dump = if force_dump || size_changed {
            true
        } else if typing_active {
            since_dump >= 10  // ~100fps cap when typing (matches poll_ms)
        } else {
            // Server auto-pushes frames when state changes (PTY output,
            // new window, etc.) — no idle dump-state polling needed.
            // This saves CPU + bandwidth: no 50-100KB JSON roundtrips
            // when the client is just sitting idle.
            false
        };
        if should_dump && !dump_in_flight {
            if writer.write_all(b"dump-state\n").is_err() { break; }
            if writer.flush().is_err() { break; }
            dump_in_flight = true;
            dump_flight_start = Instant::now();
        }

        // ── STEP 3: Render if we have a frame ────────────────────────────
        // Also render if selection changed (for highlight overlay) even without new frame
        // Always render when overlays are active (command prompt, rename, choosers)
        if !got_frame && !selection_changed && !overlays_active {
            continue;
        }

        // Skip parse + render when the raw JSON is identical to the previous
        // frame AND selection hasn't changed AND no overlays are active.
        if dump_buf == prev_dump_buf && !selection_changed && !overlays_active {
            last_dump_time = Instant::now();
            continue;
        }

        // Parse the frame (use prev_dump_buf for selection-only redraws)
        let frame_to_parse = if got_frame && dump_buf != prev_dump_buf { &dump_buf } else { &prev_dump_buf };
        let _t_parse = Instant::now();
        let state: DumpState = match serde_json::from_str(frame_to_parse) {
            Ok(s) => s,
            Err(_e) => {
                client_log("parse", &format!("JSON parse error: {} (len={})", _e, frame_to_parse.len()));
                force_dump = true;
                selection_changed = false;
                continue;
            }
        };
        let _parse_us = _t_parse.elapsed().as_micros();
        if client_log_enabled() {
            client_log("parse", &format!("OK in {}us, {} windows", _parse_us, state.windows.len()));
        }

        let root = state.layout;
        let windows = state.windows;
        last_tree = state.tree;
        let base_index = state.base_index;
        client_base_index = base_index;
        client_copy_mode = active_pane_in_copy_mode(&root);
        client_zoomed = state.zoomed;
        let dim_preds = state.prediction_dimming;
        clock_active = state.clock_mode;
        clock_colour_str = state.clock_colour;
        let state_cursor_style_code = state.cursor_style_code;
        // Server-side overlay state (update persistent variables)
        srv_popup_active = state.popup_active;
        srv_popup_command = state.popup_command.unwrap_or_default();
        srv_popup_width = state.popup_width.unwrap_or(80);
        srv_popup_height = state.popup_height.unwrap_or(24);
        srv_popup_lines = state.popup_lines;
        let srv_popup_rows_new = state.popup_rows;
        srv_popup_rows = srv_popup_rows_new;
        let new_popup_has_pty = state.popup_has_pty;
        if !srv_popup_active || new_popup_has_pty != srv_popup_has_pty {
            srv_popup_scroll = 0;
        }
        srv_popup_has_pty = new_popup_has_pty;
        srv_confirm_active = state.confirm_active;
        srv_confirm_prompt = state.confirm_prompt.unwrap_or_default();
        srv_menu_active = state.menu_active;
        srv_menu_title = state.menu_title.unwrap_or_default();
        srv_menu_selected = state.menu_selected;
        srv_menu_items = state.menu_items;
        srv_display_panes = state.display_panes;
        srv_pane_base_index = state.pane_base_index;
        srv_customize_active = state.customize_active;
        srv_customize_selected = state.customize_selected;
        srv_customize_scroll = state.customize_scroll;
        srv_customize_editing = state.customize_editing;
        srv_customize_cursor = state.customize_cursor;
        srv_customize_edit_buf = state.customize_edit_buf.unwrap_or_default();
        srv_customize_filter = state.customize_filter.unwrap_or_default();
        srv_customize_options = state.customize_options;

        // ── Extract active pane's cursor state ──────────────────────
        // We collect cursor info here but DON'T use
        // f.set_cursor_position() inside the draw callback for the
        // normal (non-copy-mode) active pane.  Instead we write
        // cursor show/hide + position + style as ONE atomic write
        // after terminal.draw().  This prevents ratatui's separate
        // execute!(..., Show/Hide) flushes from creating intermediate
        // states visible to Windows Terminal between vsync frames,
        // which causes rapid cursor flicker during high-frequency
        // output (e.g. opencode streaming).
        let mut post_draw_cursor: Option<(u16, u16)> = None; // pane-local (col, row)
        {
            fn active_cursor_info(node: &LayoutJson) -> Option<(bool, u16, u16, bool)> {
                match node {
                    LayoutJson::Leaf { active, hide_cursor, cursor_row, cursor_col, copy_mode, .. } => {
                        if *active { Some((*hide_cursor, *cursor_row, *cursor_col, *copy_mode)) } else { None }
                    }
                    LayoutJson::Split { children, .. } => {
                        children.iter().find_map(active_cursor_info)
                    }
                }
            }
            if let Some((hide, cr, cc, copy)) = active_cursor_info(&root) {
                if !hide && !clock_active && !copy {
                    post_draw_cursor = Some((cc, cr));
                }
            }
        }

        // ── OSC 52: propagate server-side clipboard to local terminal ────
        // When the server copies text (yank_selection / copy mode),
        // it includes a one-shot clipboard_osc52 field in the dump.
        // Buffer for emission after terminal.draw() to avoid corrupting
        // ratatui's output.
        if let Some(ref clip_b64) = state.clipboard_osc52 {
            if let Some(clip_text) = crate::util::base64_decode(clip_b64) {
                // Also set the local Win32 clipboard for non-SSH scenarios
                copy_to_system_clipboard(&clip_text);
                pending_osc52 = Some(clip_text);
            }
        }

        // ── Audible bell: forward BEL to host terminal ──────────────
        if state.bell {
            pending_bell = true;
        }

        // Update prefix key from server config (if provided)
        if let Some(ref prefix_str) = state.prefix {
            if let Some((kc, km)) = parse_key_string(prefix_str) {
                if (kc, km) != prefix_key {
                    prefix_key = (kc, km);
                    // Compute raw control character for Ctrl+<letter> prefix
                    prefix_raw_char = if km.contains(KeyModifiers::CONTROL) {
                        if let KeyCode::Char(c) = kc {
                            Some((c as u8 & 0x1f) as char)
                        } else { None }
                    } else { None };
                }
            }
        }

        // Update prefix2 key from server config (if provided)
        if let Some(ref prefix2_str) = state.prefix2 {
            if !prefix2_str.is_empty() {
                if let Some((kc, km)) = parse_key_string(prefix2_str) {
                    prefix2_key = Some((kc, km));
                    prefix2_raw_char = if km.contains(KeyModifiers::CONTROL) {
                        if let KeyCode::Char(c) = kc {
                            Some((c as u8 & 0x1f) as char)
                        } else { None }
                    } else { None };
                }
            } else {
                prefix2_key = None;
                prefix2_raw_char = None;
            }
        }

        // Update status-style from server config (if provided)
        if let Some(ref ss) = state.status_style {
            if !ss.is_empty() {
                let (fg, bg, bold) = parse_tmux_style_components(ss);
                status_fg = fg.unwrap_or(Color::Black);
                status_bg = bg.unwrap_or(Color::Green);
                status_bold = bold;
            }
        }

        // Sync key bindings from server
        if !state.bindings.is_empty() || !synced_bindings.is_empty() {
            synced_bindings = state.bindings;
        }
        // Sync repeat-time from server
        repeat_time_ms = state.repeat_time;
        // Update status-left / status-right from server (already format-expanded)
        if let Some(sl) = state.status_left {
            // Pass full string — visual truncation is handled by ratatui
            // when rendering into the allocated status bar area.
            // Do NOT naively truncate by char count as that can split
            // inside #[...] style directives, causing parse failures.
            // Allow empty values so conditionals like #{?client_prefix,...,}
            // can clear the status area when the condition becomes false.
            custom_status_left = if sl.is_empty() { None } else { Some(sl) };
        }
        if let Some(sr) = state.status_right {
            custom_status_right = if sr.is_empty() { None } else { Some(sr) };
        }
        let status_lines = if state.status_visible { state.status_lines } else { 0 };
        // If server's status_lines changed, re-send client-size with the
        // correct content-area height so the server's pane rects match the
        // client's render area exactly.
        let new_sl = (status_lines as u16).max(1);
        if new_sl != last_status_lines {
            last_status_lines = new_sl;
            // Force a client-size re-send on the next iteration
            last_sent_size = (0, 0);
        }
        let status_format = state.status_format;
        // Update pane border styles
        if let Some(ref pbs) = state.pane_border_style {
            if !pbs.is_empty() {
                let (fg, _bg, _bold) = parse_tmux_style_components(pbs);
                if let Some(c) = fg { pane_border_fg = c; }
            }
        }
        if let Some(ref pabs) = state.pane_active_border_style {
            if !pabs.is_empty() {
                let (fg, _bg, _bold) = parse_tmux_style_components(pabs);
                if let Some(c) = fg { pane_active_border_fg = c; }
            }
        }
        if let Some(ref pbhs) = state.pane_border_hover_style {
            if !pbhs.is_empty() {
                let (fg, _bg, _bold) = parse_tmux_style_components(pbhs);
                if let Some(c) = fg { pane_border_hover_fg = c; }
            }
        }
        // Update window-status-format strings
        if let Some(ref f) = state.wsf { if !f.is_empty() { win_status_fmt = f.clone(); } }
        if let Some(ref f) = state.wscf { if !f.is_empty() { win_status_current_fmt = f.clone(); } }
        if let Some(ref s) = state.wss { win_status_sep = s.clone(); }
        // Update window-status styles
        if let Some(ref s) = state.ws_style {
            if !s.is_empty() {
                win_status_style = Some(parse_tmux_style_components(s));
            }
        }
        if let Some(ref s) = state.wsc_style {
            if !s.is_empty() {
                win_status_current_style = Some(parse_tmux_style_components(s));
            }
        }
        // Update mode-style, status-position, status-justify from server
        if let Some(ref ms) = state.mode_style {
            if !ms.is_empty() { mode_style_str = ms.clone(); }
        }
        if let Some(ref sp) = state.status_position {
            if !sp.is_empty() { status_position_str = sp.clone(); }
        }
        if let Some(ref sj) = state.status_justify {
            if !sj.is_empty() { status_justify_str = sj.clone(); }
        }

        // ── STEP 3: Render ───────────────────────────────────────────────
        let sel_s = rsel_start;
        let sel_e = rsel_end;
        let status_at_top = status_position_str == "top";
        if client_log_enabled() {
            let sz = terminal.size().unwrap_or_default();
            client_log("draw", &format!("pre-draw terminal_size={}x{}", sz.width, sz.height));
        }
        terminal.draw(|f| {
            let area = f.area();
            let constraints = if status_at_top {
                vec![Constraint::Length(status_lines as u16), Constraint::Min(1)]
            } else {
                vec![Constraint::Min(1), Constraint::Length(status_lines as u16)]
            };
            let chunks = Layout::default().direction(Direction::Vertical)
                .constraints(constraints).split(area);
            let (content_chunk, status_chunk) = if status_at_top {
                (chunks[1], chunks[0])
            } else {
                (chunks[0], chunks[1])
            };

            client_content_area = content_chunk;
            client_pane_rects.clear();
            collect_pane_rects(&root, content_chunk, &mut client_pane_rects);
            client_borders.clear();
            let mut border_path = Vec::new();
            collect_layout_borders(&root, content_chunk, &mut border_path, &mut client_borders);

            /// Render a large ASCII clock overlay (tmux clock-mode)
            fn render_clock_overlay(f: &mut Frame, area: Rect, colour: Color) {
                // Big digit font (5 rows high, 3 cols wide per digit + colon)
                const DIGITS: [&[&str; 5]; 10] = [
                    &["###", "# #", "# #", "# #", "###"],  // 0
                    &["  #", "  #", "  #", "  #", "  #"],  // 1
                    &["###", "  #", "###", "#  ", "###"],  // 2
                    &["###", "  #", "###", "  #", "###"],  // 3
                    &["# #", "# #", "###", "  #", "  #"],  // 4
                    &["###", "#  ", "###", "  #", "###"],  // 5
                    &["###", "#  ", "###", "# #", "###"],  // 6
                    &["###", "  #", "  #", "  #", "  #"],  // 7
                    &["###", "# #", "###", "# #", "###"],  // 8
                    &["###", "# #", "###", "  #", "###"],  // 9
                ];
                const COLON: [&str; 5] = [" ", "#", " ", "#", " "];
                let now = Local::now();
                let time_str = now.format("%H:%M:%S").to_string();
                // Each char is 3 wide + 1 gap, colon is 1 wide + 1 gap
                let total_w: u16 = time_str.chars().map(|c| if c == ':' { 2 } else { 4 }).sum::<u16>() - 1;
                let total_h: u16 = 5;
                if area.width < total_w || area.height < total_h { return; }
                let start_x = area.x + (area.width.saturating_sub(total_w)) / 2;
                let start_y = area.y + (area.height.saturating_sub(total_h)) / 2;
                // Clear the area
                let clock_area = Rect::new(start_x.saturating_sub(1), start_y, total_w + 2, total_h);
                f.render_widget(Clear, clock_area);
                for row in 0..5u16 {
                    let mut x = start_x;
                    for ch in time_str.chars() {
                        if ch == ':' {
                            let cell_area = Rect::new(x, start_y + row, 1, 1);
                            let s = Span::styled(COLON[row as usize], Style::default().fg(colour));
                            f.render_widget(Paragraph::new(Line::from(s)), cell_area);
                            x += 2;
                        } else if let Some(d) = ch.to_digit(10) {
                            let pattern = DIGITS[d as usize][row as usize];
                            let cell_area = Rect::new(x, start_y + row, 3, 1);
                            let s = Span::styled(pattern, Style::default().fg(colour));
                            f.render_widget(Paragraph::new(Line::from(s)), cell_area);
                            x += 4;
                        }
                    }
                }
            }

            fn render_json(f: &mut Frame, node: &LayoutJson, area: Rect, dim_preds: bool, border_fg: Color, active_border_fg: Color, clock_mode: bool, clock_colour: Color, active_rect: Option<Rect>, mode_style_str: &str, zoomed: bool, border_status: &str, border_format: &str) {
                match node {
                    LayoutJson::Leaf {
                        id,
                        rows: _,
                        cols: _,
                        cursor_row,
                        cursor_col,
                        alternate_screen,
                        hide_cursor: _,
                        cursor_shape: _,
                        active,
                        copy_mode,
                        scroll_offset,
                        sel_start_row,
                        sel_start_col,
                        sel_end_row,
                        sel_end_col,
                        sel_mode,
                        copy_cursor_row,
                        copy_cursor_col,
                        content,
                        rows_v2,
                        title,
                    } => {
                        // No borders — content fills entire area (tmux-style)
                        let inner = area;
                        let mut lines: Vec<Line> = Vec::new();
                        let use_full_cells = *copy_mode && *active && !content.is_empty();
                        if use_full_cells || rows_v2.is_empty() {
                            for r in 0..inner.height.min(content.len() as u16) {
                                let mut spans: Vec<Span> = Vec::new();
                                let row = &content[r as usize];
                                let max_c = inner.width.min(row.len() as u16);
                                let mut c: u16 = 0;
                                while c < max_c {
                                    let cell = &row[c as usize];
                                    let mut fg = map_color(&cell.fg);
                                    let bg = map_color(&cell.bg);
                                    let in_selection = if *copy_mode && *active {
                                        if let (Some(sr), Some(sc), Some(er), Some(ec)) = (sel_start_row, sel_start_col, sel_end_row, sel_end_col) {
                                            let mode = sel_mode.as_deref().unwrap_or("char");
                                            match mode {
                                                "rect" => r >= *sr && r <= *er && c >= (*sc).min(*ec) && c <= (*sc).max(*ec),
                                                "line" => r >= *sr && r <= *er,
                                                _ /* char */ => {
                                                    if *sr == *er {
                                                        // Single line
                                                        r == *sr && c >= (*sc).min(*ec) && c <= (*sc).max(*ec)
                                                    } else if r == *sr {
                                                        c >= *sc
                                                    } else if r == *er {
                                                        c <= *ec
                                                    } else {
                                                        r > *sr && r < *er
                                                    }
                                                }
                                            }
                                        } else { false }
                                    } else { false };
                                    if *active && dim_preds && !*alternate_screen
                                        && (r > *cursor_row || (r == *cursor_row && c >= *cursor_col))
                                    {
                                        fg = dim_color(fg);
                                    }
                                    let mut style = Style::default().fg(fg).bg(bg);
                                    if in_selection {
                                        // Apply mode-style from theme/config instead of hardcoded colors
                                        let ms = crate::rendering::parse_tmux_style(&mode_style_str);
                                        style = ms;
                                    }
                                    if cell.inverse { style = style.add_modifier(Modifier::REVERSED); }
                                    if cell.dim { style = style.add_modifier(Modifier::DIM); }
                                    if cell.bold { style = style.add_modifier(Modifier::BOLD); }
                                    if cell.italic { style = style.add_modifier(Modifier::ITALIC); }
                                    if cell.underline { style = style.add_modifier(Modifier::UNDERLINED); }
                                    if cell.blink { style = style.add_modifier(Modifier::SLOW_BLINK); }
                                    if cell.strikethrough { style = style.add_modifier(Modifier::CROSSED_OUT); }
                                    // ratatui-crossterm omits SGR 8 (HIDDEN), render as spaces
                                    let text: &str = if cell.hidden {
                                        " "
                                    } else if cell.text.is_empty() {
                                        " "
                                    } else {
                                        &cell.text
                                    };
                                    let char_width = unicode_width::UnicodeWidthStr::width(text) as u16;
                                    if char_width >= 2 && c + char_width > max_c {
                                        // Wide char at boundary would overflow
                                        spans.push(Span::styled(" ", style));
                                        c += 1;
                                    } else {
                                        spans.push(Span::styled(text, style));
                                        if char_width >= 2 {
                                            c += 2;
                                        } else {
                                            c += 1;
                                        }
                                    }
                                }
                                // Pad remaining columns so the Line fills the
                                // full pane width — prevents a visible gap when
                                // the server's content width differs from the
                                // client's computed rect (e.g. during resize or
                                // when status_lines > 1).
                                if c < inner.width {
                                    let last_bg = if !spans.is_empty() {
                                        spans.last().unwrap().style.bg.unwrap_or(Color::Reset)
                                    } else { Color::Reset };
                                    let pad = " ".repeat((inner.width - c) as usize);
                                    spans.push(Span::styled(pad, Style::default().bg(last_bg)));
                                }
                                lines.push(Line::from(spans));
                            }
                        } else {
                            for r in 0..inner.height.min(rows_v2.len() as u16) {
                                let mut spans: Vec<Span> = Vec::new();
                                let mut c: u16 = 0;
                                let mut last_bg = Color::Reset;
                                for run in &rows_v2[r as usize].runs {
                                    if c >= inner.width { break; }
                                    let mut fg = map_color(&run.fg);
                                    let bg = map_color(&run.bg);
                                    last_bg = bg;
                                    if *active && dim_preds && !*alternate_screen
                                        && (r > *cursor_row || (r == *cursor_row && c >= *cursor_col))
                                    {
                                        fg = dim_color(fg);
                                    }
                                    let mut style = Style::default().fg(fg).bg(bg);
                                    if run.flags & 16 != 0 { style = style.add_modifier(Modifier::REVERSED); }
                                    if run.flags & 1 != 0 { style = style.add_modifier(Modifier::DIM); }
                                    if run.flags & 2 != 0 { style = style.add_modifier(Modifier::BOLD); }
                                    if run.flags & 4 != 0 { style = style.add_modifier(Modifier::ITALIC); }
                                    if run.flags & 8 != 0 { style = style.add_modifier(Modifier::UNDERLINED); }
                                    if run.flags & 32 != 0 { style = style.add_modifier(Modifier::SLOW_BLINK); }
                                    if run.flags & 128 != 0 { style = style.add_modifier(Modifier::CROSSED_OUT); }
                                    // ratatui-crossterm omits SGR 8 (HIDDEN), render as spaces
                                    let text: &str = if run.flags & 64 != 0 {
                                        " "
                                    } else if run.text.is_empty() {
                                        " "
                                    } else {
                                        &run.text
                                    };
                                    // Truncate run text if it extends past the pane width
                                    let run_w = run.width.max(1);
                                    if c + run_w > inner.width {
                                        let avail = (inner.width - c) as usize;
                                        let mut truncated = String::new();
                                        let mut used = 0usize;
                                        for ch in text.chars() {
                                            let cw = unicode_width::UnicodeWidthChar::width(ch).unwrap_or(1);
                                            if used + cw > avail { break; }
                                            used += cw;
                                            truncated.push(ch);
                                        }
                                        if !truncated.is_empty() {
                                            spans.push(Span::styled(truncated, style));
                                        }
                                        c = inner.width;
                                    } else {
                                        spans.push(Span::styled(text, style));
                                        c = c.saturating_add(run_w);
                                    }
                                }
                                // Pad remaining columns with the last run's bg
                                // so every Line fills the full pane width.
                                if c < inner.width {
                                    let pad = " ".repeat((inner.width - c) as usize);
                                    spans.push(Span::styled(pad, Style::default().bg(last_bg)));
                                }
                                lines.push(Line::from(spans));
                            }
                        }
                        f.render_widget(Clear, inner);
                        let para = Paragraph::new(Text::from(lines));
                        f.render_widget(para, inner);

                        // Copy mode indicator (replaces the old block title "[copy mode]")
                        if *copy_mode && *active {
                            let label = "[copy mode]";
                            let lw = label.len() as u16;
                            if area.width >= lw {
                                let lx = area.x + area.width.saturating_sub(lw);
                                let la = Rect::new(lx, area.y, lw, 1);
                                let ls = Span::styled(label, Style::default().fg(Color::Black).bg(Color::Yellow));
                                f.render_widget(Paragraph::new(Line::from(ls)), la);
                            }
                        }

                        if *copy_mode && *active && *scroll_offset > 0 {
                            let indicator = format!("[{}/{}]", scroll_offset, scroll_offset);
                            let indicator_width = indicator.len() as u16;
                            if area.width > indicator_width + 2 {
                                let indicator_x = area.x + area.width - indicator_width - 1;
                                let indicator_y = if *copy_mode { area.y + 1 } else { area.y };
                                let indicator_area = Rect::new(indicator_x, indicator_y, indicator_width, 1);
                                let indicator_span = Span::styled(indicator, Style::default().fg(Color::Black).bg(Color::Yellow));
                                f.render_widget(Paragraph::new(Line::from(indicator_span)), indicator_area);
                            }
                        }

                        if *active && !*copy_mode {
                            // Clock mode overlay
                            if clock_mode {
                                render_clock_overlay(f, inner, clock_colour);
                            }
                            // Cursor visibility is handled entirely outside
                            // the draw callback — see the post-draw atomic
                            // cursor write below.  We intentionally do NOT
                            // call f.set_cursor_position() here so that
                            // ratatui never emits separate ?25h/?25l flushes
                            // that would create intermediate states visible
                            // to WT between vsync frames.
                        }

                        // In copy mode, show cursor at copy_pos with a
                        // highlighted (reverse-video) cell so the user can see
                        // where the cursor is before starting selection.
                        if *copy_mode && *active {
                            if let (Some(cr), Some(cc)) = (copy_cursor_row, copy_cursor_col) {
                                let cr = (*cr).min(inner.height.saturating_sub(1));
                                let cc = (*cc).min(inner.width.saturating_sub(1));
                                let cy = inner.y + cr;
                                let cx = inner.x + cc;
                                f.set_cursor_position((cx, cy));
                                // Highlight the cursor cell with reverse video
                                let buf = f.buffer_mut();
                                let buf_area = buf.area;
                                if cy >= buf_area.y && cy < buf_area.y + buf_area.height
                                    && cx >= buf_area.x && cx < buf_area.x + buf_area.width
                                {
                                    let idx = (cy - buf_area.y) as usize * buf_area.width as usize
                                        + (cx - buf_area.x) as usize;
                                    if idx < buf.content.len() {
                                        let cell = &mut buf.content[idx];
                                        cell.set_style(cell.style().add_modifier(Modifier::REVERSED));
                                    }
                                }
                            }
                        }
                        // Pane border format/status overlay
                        if border_status != "off" && !border_format.is_empty() && area.height > 1 {
                            let pane_title_str = title.as_deref().unwrap_or("");
                            let pane_label = border_format
                                .replace("#{pane_title}", pane_title_str)
                                .replace("#{pane_index}", &id.to_string())
                                .replace("#P", &id.to_string());
                            let label_width = unicode_width::UnicodeWidthStr::width(pane_label.as_str()) as u16;
                            if label_width > 0 && area.width >= label_width {
                                let label_y = if border_status == "bottom" { area.y + area.height.saturating_sub(1) } else { area.y };
                                let label_area = Rect::new(area.x, label_y, label_width.min(area.width), 1);
                                let label_style = Style::default().fg(if *active { active_border_fg } else { border_fg });
                                f.render_widget(Paragraph::new(Line::from(Span::styled(pane_label, label_style))), label_area);
                            }
                        }
                    }
                    LayoutJson::Split { kind, sizes, children } => {
                        let effective_sizes: Vec<u16> = if sizes.len() == children.len() {
                            sizes.clone()
                        } else {
                            vec![(100 / children.len().max(1)) as u16; children.len()]
                        };
                        let is_horizontal = kind == "Horizontal";
                        let rects = split_with_gaps(is_horizontal, &effective_sizes, area);

                        // Render children first
                        for (i, child) in children.iter().enumerate() {
                            if i < rects.len() { render_json(f, child, rects[i], dim_preds, border_fg, active_border_fg, clock_mode, clock_colour, active_rect, mode_style_str, zoomed, border_status, border_format); }
                        }

                        // Draw separator lines between children using direct buffer access.
                        // Skip when zoomed — no visible borders (#82)
                        if zoomed { return; }
                        let border_style = Style::default().fg(border_fg);
                        let active_border_style = Style::default().fg(active_border_fg);
                        let buf = f.buffer_mut();
                        for i in 0..children.len().saturating_sub(1) {
                            if i >= rects.len() { break; }

                            // When both neighbours are direct leaves, use the midpoint
                            // half-highlight so the colored half indicates which side
                            // is active.  For nested splits, use adjacency to the
                            // computed active pane rect so only the correct portion of
                            // the separator is highlighted.
                            let both_leaves = matches!(&children[i], LayoutJson::Leaf { .. })
                                && matches!(children.get(i + 1), Some(LayoutJson::Leaf { .. }));

                            if is_horizontal {
                                // Vertical separator line between left/right children.
                                let sep_x = rects[i].x + rects[i].width;
                                if sep_x < buf.area.x + buf.area.width {
                                    if both_leaves {
                                        let left_active = matches!(&children[i], LayoutJson::Leaf { active, .. } if *active);
                                        let right_active = matches!(children.get(i + 1), Some(LayoutJson::Leaf { active, .. }) if *active);
                                        let left_sty = if left_active { active_border_style } else { border_style };
                                        let right_sty = if right_active { active_border_style } else { border_style };
                                        let mid_y = area.y + area.height / 2;
                                        for y in area.y..area.y + area.height {
                                            let sty = if y < mid_y { left_sty } else { right_sty };
                                            let idx = (y - buf.area.y) as usize * buf.area.width as usize
                                                + (sep_x - buf.area.x) as usize;
                                            if idx < buf.content.len() {
                                                buf.content[idx].set_char('');
                                                buf.content[idx].set_style(sty);
                                            }
                                        }
                                    } else {
                                        for y in area.y..area.y + area.height {
                                            let active = active_rect.map_or(false, |ar| {
                                                y >= ar.y && y < ar.y + ar.height
                                                && (sep_x == ar.x + ar.width || sep_x + 1 == ar.x)
                                            });
                                            let sty = if active { active_border_style } else { border_style };
                                            let idx = (y - buf.area.y) as usize * buf.area.width as usize
                                                + (sep_x - buf.area.x) as usize;
                                            if idx < buf.content.len() {
                                                buf.content[idx].set_char('');
                                                buf.content[idx].set_style(sty);
                                            }
                                        }
                                    }
                                }
                            } else {
                                // Horizontal separator line between top/bottom children.
                                let sep_y = rects[i].y + rects[i].height;
                                if sep_y < buf.area.y + buf.area.height {
                                    if both_leaves {
                                        let top_active = matches!(&children[i], LayoutJson::Leaf { active, .. } if *active);
                                        let bot_active = matches!(children.get(i + 1), Some(LayoutJson::Leaf { active, .. }) if *active);
                                        let top_sty = if top_active { active_border_style } else { border_style };
                                        let bot_sty = if bot_active { active_border_style } else { border_style };
                                        let mid_x = area.x + area.width / 2;
                                        for x in area.x..area.x + area.width {
                                            let sty = if x < mid_x { top_sty } else { bot_sty };
                                            let idx = (sep_y - buf.area.y) as usize * buf.area.width as usize
                                                + (x - buf.area.x) as usize;
                                            if idx < buf.content.len() {
                                                buf.content[idx].set_char('');
                                                buf.content[idx].set_style(sty);
                                            }
                                        }
                                    } else {
                                        for x in area.x..area.x + area.width {
                                            let active = active_rect.map_or(false, |ar| {
                                                x >= ar.x && x < ar.x + ar.width
                                                && (sep_y == ar.y + ar.height || sep_y + 1 == ar.y)
                                            });
                                            let sty = if active { active_border_style } else { border_style };
                                            let idx = (sep_y - buf.area.y) as usize * buf.area.width as usize
                                                + (x - buf.area.x) as usize;
                                            if idx < buf.content.len() {
                                                buf.content[idx].set_char('');
                                                buf.content[idx].set_style(sty);
                                            }
                                        }
                                    }
                                }
                            }
                        }
                    }
                }
            }

            let active_rect = compute_active_rect_json(&root, content_chunk);
            let clock_col = clock_colour_str.as_deref().map(|s| map_color(s)).unwrap_or(Color::Cyan);
            let border_status = state.pane_border_status.as_deref().unwrap_or("off");
            let border_format = state.pane_border_format.as_deref().unwrap_or("");
            render_json(f, &root, content_chunk, dim_preds, pane_border_fg, pane_active_border_fg, clock_active, clock_col, active_rect, &mode_style_str, state.zoomed, border_status, border_format);
            fix_border_intersections(f.buffer_mut());
            // Re-color all border characters based on adjacency to the active pane.
            // render_json and fix_border_intersections can leave inconsistent styles
            // at intersections and along edges shared by nested splits.
            if let Some(ar) = active_rect {
                let buf = f.buffer_mut();
                let w = buf.area.width as usize;
                let h = buf.area.height as usize;
                let border_style = Style::default().fg(pane_border_fg);
                let active_style = Style::default().fg(pane_active_border_fg);
                for row in 0..h {
                    for col in 0..w {
                        let idx = row * w + col;
                        if idx >= buf.content.len() { continue; }
                        let ch = buf.content[idx].symbol().chars().next().unwrap_or(' ');
                        if !matches!(ch, '' | '' | '' | '' | '' | '' | '') { continue; }
                        let x = buf.area.x + col as u16;
                        let y = buf.area.y + row as u16;
                        let adj = (x + 1 == ar.x && y >= ar.y && y < ar.y + ar.height)
                            || (x == ar.x + ar.width && y >= ar.y && y < ar.y + ar.height)
                            || (y + 1 == ar.y && x >= ar.x && x < ar.x + ar.width)
                            || (y == ar.y + ar.height && x >= ar.x && x < ar.x + ar.width)
                            || ((x + 1 == ar.x || x == ar.x + ar.width) && (y + 1 == ar.y || y == ar.y + ar.height));
                        buf.content[idx].set_style(if adj { active_style } else { border_style });
                    }
                }
            }

            // Highlight the border under the cursor to preview what a drag would move.
            if let Some((hpos, ref hkind, harea)) = hovered_border {
                let buf = f.buffer_mut();
                let w = buf.area.width as usize;
                let hover_style = Style::default().fg(pane_border_hover_fg);
                if hkind == "Horizontal" {
                    // Vertical separator line at column hpos, spanning harea's height
                    let col = hpos as usize;
                    if col >= buf.area.x as usize && col < (buf.area.x + buf.area.width) as usize {
                        for y in harea.y..harea.y + harea.height {
                            let idx = (y - buf.area.y) as usize * w + (col - buf.area.x as usize);
                            if idx < buf.content.len() {
                                buf.content[idx].set_style(hover_style);
                            }
                        }
                    }
                } else {
                    // Horizontal separator line at row hpos, spanning harea's width
                    let row = hpos as usize;
                    if row >= buf.area.y as usize && row < (buf.area.y + buf.area.height) as usize {
                        for x in harea.x..harea.x + harea.width {
                            let idx = (row - buf.area.y as usize) * w + (x - buf.area.x) as usize;
                            if idx < buf.content.len() {
                                buf.content[idx].set_style(hover_style);
                            }
                        }
                    }
                }
            }

            // ── Left-click drag text selection overlay ────────────────
            // Suppress the client-side blue selection overlay when the
            // server is in copy mode – the server draws its own themed
            // selection and the blue overlay would hide everything.
            if let (Some(s), Some(e)) = (sel_s, sel_e) {
            if !active_pane_in_copy_mode(&root) {
                // Normalise so (r0,c0) <= (r1,c1) in reading order
                let (r0, c0, r1, c1) = if (s.1, s.0) <= (e.1, e.0) {
                    (s.1, s.0, e.1, e.0)
                } else {
                    (e.1, e.0, s.1, s.0)
                };
                let buf = f.buffer_mut();
                let buf_area = buf.area;
                for row in r0..=r1 {
                    let col_start = if row == r0 { c0 } else { 0 };
                    let col_end = if row == r1 { c1 } else { area.width.saturating_sub(1) };
                    for col in col_start..=col_end {
                        if row < buf_area.height && col < buf_area.width {
                            let idx = (row - buf_area.y) as usize * buf_area.width as usize
                                + (col - buf_area.x) as usize;
                            if idx < buf.content.len() {
                                buf.content[idx].set_style(Style::default().fg(Color::Black).bg(Color::LightCyan));
                            }
                        }
                    }
                }
            } // !active_pane_in_copy_mode
            } // if let sel_s, sel_e

            if session_chooser {
                let sel_style = crate::rendering::parse_tmux_style(&mode_style_str);
                let overlay = Block::default().borders(Borders::ALL).title("choose-session (enter=switch, x=kill, esc=close)").border_style(sel_style);
                let oa = centered_rect(70, 20, content_chunk);
                f.render_widget(Clear, oa);
                f.render_widget(&overlay, oa);
                let mut lines: Vec<Line> = Vec::new();
                for (i, (sname, info)) in session_entries.iter().enumerate() {
                    let marker = if sname == &current_session { "*" } else { " " };
                    let line = if i == session_selected {
                        Line::from(Span::styled(format!("{} {}", marker, info), sel_style))
                    } else {
                        Line::from(format!("{} {}", marker, info))
                    };
                    lines.push(line);
                }
                let para = Paragraph::new(Text::from(lines));
                f.render_widget(para, overlay.inner(oa));
            }
            if tree_chooser {
                let sel_style = crate::rendering::parse_tmux_style(&mode_style_str);
                let overlay = Block::default().borders(Borders::ALL).title("choose-tree").border_style(sel_style);
                // Use dynamic height: content lines + 2 (borders), capped to
                // available space so the overlay never exceeds the terminal.
                let tree_h = ((tree_entries.len() as u16).saturating_add(2))
                    .max(5)
                    .min(content_chunk.height.saturating_sub(2));
                let oa = centered_rect(60, tree_h, content_chunk);
                f.render_widget(Clear, oa);
                f.render_widget(&overlay, oa);
                let inner = overlay.inner(oa);
                let visible_h = inner.height as usize;
                // Keep tree_selected in view
                if tree_selected >= tree_scroll + visible_h {
                    tree_scroll = tree_selected.saturating_sub(visible_h - 1);
                }
                if tree_selected < tree_scroll {
                    tree_scroll = tree_selected;
                }
                let mut lines: Vec<Line> = Vec::new();
                for (i, (is_win, wid, _pid, label, _sess)) in tree_entries.iter().enumerate().skip(tree_scroll).take(visible_h) {
                    let line = if i == tree_selected {
                        Line::from(Span::styled(label.clone(), sel_style))
                    } else if *is_win && *wid == usize::MAX {
                        // Session header — bold
                        Line::from(Span::styled(label.clone(), Style::default().add_modifier(Modifier::BOLD)))
                    } else {
                        Line::from(label.clone())
                    };
                    lines.push(line);
                }
                let para = Paragraph::new(Text::from(lines));
                f.render_widget(para, inner);
            }
            if keys_viewer {
                // Proportional overlay: 90% width, up to 80% height
                let avail_h = content_chunk.height;
                let overlay_h = (avail_h * 80 / 100).max(5).min(avail_h.saturating_sub(2));
                let overlay = Block::default().borders(Borders::ALL)
                    .title(" list-keys (q/Esc=close, Up/Down/PgUp/PgDn=scroll) ");
                let oa = centered_rect(90, overlay_h, content_chunk);
                f.render_widget(Clear, oa);
                f.render_widget(&overlay, oa);
                let inner = overlay.inner(oa);
                let visible_h = inner.height as usize;
                // Clamp scroll so we don't scroll past the end
                let max_scroll = keys_viewer_lines.len().saturating_sub(visible_h);
                if keys_viewer_scroll > max_scroll { keys_viewer_scroll = max_scroll; }
                let mut lines: Vec<Line> = Vec::new();
                for (_i, entry) in keys_viewer_lines.iter().enumerate().skip(keys_viewer_scroll).take(visible_h) {
                    // Highlight section headers, "bind-key" keyword, and plain text differently
                    if entry.starts_with("──") || entry.starts_with("── ") {
                        lines.push(Line::from(Span::styled(entry.clone(), Style::default().fg(Color::Yellow).add_modifier(Modifier::BOLD))));
                    } else if let Some(rest) = entry.strip_prefix("bind-key") {
                        lines.push(Line::from(vec![
                            Span::styled("bind-key", Style::default().fg(Color::Green)),
                            Span::raw(rest.to_string()),
                        ]));
                    } else {
                        lines.push(Line::from(entry.clone()));
                    }
                }
                // Show scroll indicator in bottom-right
                let para = Paragraph::new(Text::from(lines));
                f.render_widget(para, inner);
                // Scroll position indicator
                if keys_viewer_lines.len() > visible_h {
                    let pct = if max_scroll == 0 { 100 } else { keys_viewer_scroll * 100 / max_scroll };
                    let indicator = if keys_viewer_scroll == 0 {
                        "Top".to_string()
                    } else if keys_viewer_scroll >= max_scroll {
                        "Bot".to_string()
                    } else {
                        format!("{}%", pct)
                    };
                    let ind_len = indicator.len() as u16;
                    if oa.width > ind_len + 2 {
                        let ind_x = oa.x + oa.width - ind_len - 2;
                        let ind_y = oa.y + oa.height - 1;
                        let ind_rect = Rect::new(ind_x, ind_y, ind_len, 1);
                        let ind_para = Paragraph::new(Span::styled(indicator, Style::default().fg(Color::DarkGray)));
                        f.render_widget(ind_para, ind_rect);
                    }
                }
            }
            let sb_fg = status_fg;
            let sb_bg = status_bg;
            let sb_base = if status_bold {
                Style::default().fg(sb_fg).bg(sb_bg).add_modifier(Modifier::BOLD)
            } else {
                Style::default().fg(sb_fg).bg(sb_bg)
            };
            // ── Build three separate span groups: left, tabs, right ──
            use unicode_width::UnicodeWidthStr;
            // If status_format[0] is set, use it for line 0 instead of the default 3-part layout
            let use_status_format_0 = status_format.len() > 0 && !status_format[0].is_empty();
            // Left portion: custom status_left or default [session] prefix
            let left_prefix = match custom_status_left {
                Some(ref sl) => sl.clone(),
                None => format!("[{}] ", name),
            };
            if client_log_enabled() {
                client_log("status", &format!("parsing left_prefix ({} chars): [{}]",
                    left_prefix.len(), left_prefix.chars().take(100).collect::<String>()));
            }
            let mut left_spans: Vec<Span> = crate::rendering::parse_inline_styles(&left_prefix, sb_base);

            // Window tabs (the window list)
            let mut tab_spans_all: Vec<Span> = Vec::new();
            let mut tab_rel_positions: Vec<(usize, u16, u16)> = Vec::new();
            let mut tab_cursor: u16 = 0;
            for (i, w) in windows.iter().enumerate() {
                let tab_text = if !w.tab_text.is_empty() {
                    w.tab_text.clone()
                } else {
                    let display_idx = i + base_index;
                    let fmt = if w.active { &win_status_current_fmt } else { &win_status_fmt };
                    fmt.replace("#I", &display_idx.to_string())
                       .replace("#W", &w.name)
                       .replace("#F", if w.active { "*" } else { "" })
                };
                if i > 0 {
                    // Parse inline styles in separator (e.g. "#[fg=#44475a]|")
                    let sep_spans = crate::rendering::parse_inline_styles(&win_status_sep, sb_base);
                    let sep_w: u16 = sep_spans.iter().map(|s| UnicodeWidthStr::width(s.content.as_ref()) as u16).sum();
                    tab_spans_all.extend(sep_spans);
                    tab_cursor += sep_w;
                }
                let fallback_style = if w.active {
                    if let Some((fg, bg, bold)) = win_status_current_style {
                        let mut s = Style::default();
                        if let Some(c) = fg { s = s.fg(c); }
                        if let Some(c) = bg { s = s.bg(c); }
                        if bold { s = s.add_modifier(Modifier::BOLD); }
                        s
                    } else {
                        sb_base
                    }
                } else if w.activity {
                    Style::default()
                        .fg(Color::Black)
                        .bg(Color::White)
                        .add_modifier(Modifier::BOLD)
                } else {
                    if let Some((fg, bg, bold)) = win_status_style {
                        let mut s = Style::default();
                        if let Some(c) = fg { s = s.fg(c); }
                        if let Some(c) = bg { s = s.bg(c); }
                        if bold { s = s.add_modifier(Modifier::BOLD); }
                        s
                    } else {
                        sb_base
                    }
                };
                let parsed = crate::rendering::parse_inline_styles(&tab_text, fallback_style);
                let tab_start = tab_cursor;
                let tab_w: u16 = parsed.iter().map(|s| UnicodeWidthStr::width(s.content.as_ref()) as u16).sum();
                tab_cursor += tab_w;
                tab_rel_positions.push((i, tab_start, tab_cursor));
                tab_spans_all.extend(parsed);
            }

            // Right portion
            let right_text = custom_status_right.as_deref().unwrap_or("").to_string();
            if client_log_enabled() {
                client_log("status", &format!("parsing right_text ({} chars): [{}]",
                    right_text.len(), right_text.chars().take(100).collect::<String>()));
            }
            let mut right_spans = crate::rendering::parse_inline_styles(&right_text, sb_base);

            // Enforce status-left-length / status-right-length truncation (tmux parity)
            crate::style::truncate_spans_to_width(&mut left_spans, state.status_left_length);
            crate::style::truncate_spans_to_width(&mut right_spans, state.status_right_length);

            // Measure widths using Unicode display width
            let left_w: usize = left_spans.iter().map(|s| UnicodeWidthStr::width(s.content.as_ref())).sum();
            let tabs_w: usize = tab_spans_all.iter().map(|s| UnicodeWidthStr::width(s.content.as_ref())).sum();
            let right_w: usize = right_spans.iter().map(|s| UnicodeWidthStr::width(s.content.as_ref())).sum();
            let total_width = status_chunk.width as usize;

            // Assemble final spans based on status-justify
            let mut status_spans: Vec<Span> = Vec::new();
            match status_justify_str.as_str() {
                "centre" | "center" => {
                    // Centre: [left] [pad1] [tabs] [pad2] [right]
                    // Tabs are centred in the space between left and right.
                    let avail = total_width.saturating_sub(left_w).saturating_sub(right_w);
                    let pad_before = avail.saturating_sub(tabs_w) / 2;
                    let pad_after = avail.saturating_sub(tabs_w).saturating_sub(pad_before);
                    status_spans.extend(left_spans);
                    if pad_before > 0 { status_spans.push(Span::styled(" ".repeat(pad_before), sb_base)); }
                    status_spans.extend(tab_spans_all);
                    if pad_after > 0 { status_spans.push(Span::styled(" ".repeat(pad_after), sb_base)); }
                    status_spans.extend(right_spans);
                }
                "absolute-centre" | "absolute-center" => {
                    // Absolute-centre: tabs centred on the total terminal width
                    let tabs_start = total_width.saturating_sub(tabs_w) / 2;
                    status_spans.extend(left_spans);
                    let pad_before = tabs_start.saturating_sub(left_w);
                    if pad_before > 0 { status_spans.push(Span::styled(" ".repeat(pad_before), sb_base)); }
                    status_spans.extend(tab_spans_all);
                    let used = left_w + pad_before + tabs_w;
                    let pad_after = total_width.saturating_sub(used).saturating_sub(right_w);
                    if pad_after > 0 { status_spans.push(Span::styled(" ".repeat(pad_after), sb_base)); }
                    status_spans.extend(right_spans);
                }
                "right" => {
                    // Right: [left] [pad] [tabs] [right]
                    status_spans.extend(left_spans);
                    let used = left_w + tabs_w + right_w;
                    let pad = total_width.saturating_sub(used);
                    if pad > 0 { status_spans.push(Span::styled(" ".repeat(pad), sb_base)); }
                    status_spans.extend(tab_spans_all);
                    status_spans.extend(right_spans);
                }
                _ => {
                    // Left (default): [left] [tabs] [pad] [right]
                    status_spans.extend(left_spans);
                    status_spans.extend(tab_spans_all);
                    let used = left_w + tabs_w + right_w;
                    let pad = total_width.saturating_sub(used);
                    if pad > 0 { status_spans.push(Span::styled(" ".repeat(pad), sb_base)); }
                    status_spans.extend(right_spans);
                }
            }
            // Compute absolute tab positions based on status-justify layout
            let tabs_x_offset: u16 = status_chunk.x + match status_justify_str.as_str() {
                "centre" | "center" => {
                    let avail = total_width.saturating_sub(left_w).saturating_sub(right_w);
                    let pad_before = avail.saturating_sub(tabs_w) / 2;
                    (left_w + pad_before) as u16
                }
                "absolute-centre" | "absolute-center" => {
                    let tabs_start = total_width.saturating_sub(tabs_w) / 2;
                    tabs_start as u16
                }
                "right" => {
                    let used = left_w + tabs_w + right_w;
                    let pad = total_width.saturating_sub(used);
                    (left_w + pad) as u16
                }
                _ => left_w as u16, // "left" default
            };
            client_tab_positions = tab_rel_positions.iter().map(|&(idx, s, e)| (idx, s + tabs_x_offset, e + tabs_x_offset)).collect();
            client_status_row = status_chunk.y;
            // Truncate overall status line to fit the available width
            crate::style::truncate_spans_to_width(&mut status_spans, total_width);
            // If a display-message is active, show it on the status bar
            // instead of the normal status content (tmux parity).
            // Uses message-style (default: bg=yellow,fg=black) matching tmux.
            let status_bar = if let Some(ref msg) = state.status_message {
                let msg_style = crate::rendering::parse_tmux_style("bg=yellow,fg=black");
                let padded = if msg.len() < status_chunk.width as usize {
                    format!("{}{}", msg, " ".repeat(status_chunk.width as usize - msg.len()))
                } else {
                    msg.chars().take(status_chunk.width as usize).collect()
                };
                Paragraph::new(Line::from(Span::styled(padded, msg_style))).style(msg_style)
            } else {
                Paragraph::new(Line::from(status_spans)).style(sb_base)
            };
            f.render_widget(Clear, status_chunk);
            // Render the first status line (line 0)
            let line0_area = Rect { x: status_chunk.x, y: status_chunk.y, width: status_chunk.width, height: 1.min(status_chunk.height) };
            if use_status_format_0 && state.status_message.is_none() {
                // status-format[0] overrides the default left+tabs+right layout.
                // Use the layout engine to handle #[align], #[fill], #[list], #[range].
                let layout = crate::style::layout_format_line(
                    &status_format[0], total_width, sb_base,
                );
                // Update tab positions from range info so mouse clicks work
                // with custom status-format layouts.
                client_tab_positions = layout.ranges.iter().filter_map(|(rt, s, e)| {
                    match rt {
                        crate::style::StatusRangeType::Window(idx) => {
                            Some((*idx, *s + status_chunk.x, *e + status_chunk.x))
                        }
                    }
                }).collect();
                let fmt0_widget = Paragraph::new(Line::from(layout.spans)).style(sb_base);
                f.render_widget(fmt0_widget, line0_area);
            } else {
                f.render_widget(status_bar, line0_area);
            }
            // Render additional status lines (index 1+) from status_format
            for line_idx in 1..status_lines {
                let line_y = status_chunk.y + line_idx as u16;
                if line_y >= status_chunk.y + status_chunk.height { break; }
                let line_area = Rect { x: status_chunk.x, y: line_y, width: status_chunk.width, height: 1 };
                let text = if line_idx < status_format.len() && !status_format[line_idx].is_empty() {
                    status_format[line_idx].clone()
                } else {
                    String::new()
                };
                // Use the layout engine for #[align], #[fill], #[list], #[range] support
                let layout = crate::style::layout_format_line(&text, line_area.width as usize, sb_base);
                let line_widget = Paragraph::new(Line::from(layout.spans)).style(sb_base);
                f.render_widget(line_widget, line_area);
            }
            if renaming {
                let overlay = Block::default().borders(Borders::ALL).title("rename window");
                let oa = centered_rect(60, 3, content_chunk);
                f.render_widget(Clear, oa);
                f.render_widget(&overlay, oa);
                let para = Paragraph::new(format!("name: {}", rename_buf));
                f.render_widget(para, overlay.inner(oa));
            }
            if pane_renaming {
                let overlay = Block::default().borders(Borders::ALL).title("set pane title");
                let oa = centered_rect(60, 3, content_chunk);
                f.render_widget(Clear, oa);
                f.render_widget(&overlay, oa);
                let para = Paragraph::new(format!("title: {}", pane_title_buf));
                f.render_widget(para, overlay.inner(oa));
            }
            if command_input {
                let overlay = Block::default().borders(Borders::ALL).title("command");
                let oa = centered_rect(60, 3, content_chunk);
                f.render_widget(Clear, oa);
                f.render_widget(&overlay, oa);
                let inner = overlay.inner(oa);
                let para = Paragraph::new(format!(": {}", command_buf));
                f.render_widget(para, inner);
                // Show cursor at the correct position within the prompt
                let cx = inner.x + 2 + command_cursor as u16; // +2 for ": "
                f.set_cursor_position((cx, inner.y));
            }
            if window_idx_input {
                let overlay = Block::default().borders(Borders::ALL).title("select window");
                let oa = centered_rect(50, 3, content_chunk);
                f.render_widget(Clear, oa);
                f.render_widget(&overlay, oa);
                let inner = overlay.inner(oa);
                let para = Paragraph::new(format!("index: {}", window_idx_buf));
                f.render_widget(para, inner);
                let cx = inner.x + 7 + window_idx_buf.len() as u16;
                f.set_cursor_position((cx, inner.y));
            }
            if let Some(ref cmd) = confirm_cmd {
                let overlay = Block::default().borders(Borders::ALL).title("confirm");
                let oa = centered_rect(50, 3, content_chunk);
                f.render_widget(Clear, oa);
                f.render_widget(&overlay, oa);
                let para = Paragraph::new(format!("{}? (y/n)", cmd));
                f.render_widget(para, overlay.inner(oa));
            }

            // ── Server-side overlay rendering ────────────────────────
            if srv_popup_active {
                let w = srv_popup_width.min(content_chunk.width.saturating_sub(2));
                let h = srv_popup_height.min(content_chunk.height.saturating_sub(2));
                let popup_area = Rect {
                    x: content_chunk.x + (content_chunk.width.saturating_sub(w)) / 2,
                    y: content_chunk.y + (content_chunk.height.saturating_sub(h)) / 2,
                    width: w,
                    height: h,
                };
                let title = if srv_popup_command.is_empty() { "Popup".to_string() } else { let max_title = (w as usize).saturating_sub(4); if srv_popup_command.len() > max_title { format!("{}...", &srv_popup_command[..max_title.saturating_sub(3)]) } else { srv_popup_command.clone() } };
                let block = Block::default()
                    .borders(Borders::ALL)
                    .border_style(Style::default().fg(Color::Yellow))
                    .title(title);
                let inner_w = w.saturating_sub(2);
                let mut lines: Vec<Line<'static>> = Vec::new();
                if !srv_popup_rows.is_empty() {
                    // Render with full color/style data from popup_rows (#154)
                    for row_data in &srv_popup_rows {
                        let mut spans: Vec<Span<'static>> = Vec::new();
                        let mut col: u16 = 0;
                        for run in &row_data.runs {
                            if col >= inner_w { break; }
                            let fg = crate::style::map_color(&run.fg);
                            let bg = crate::style::map_color(&run.bg);
                            let mut style = Style::default().fg(fg).bg(bg);
                            if run.flags & 1  != 0 { style = style.add_modifier(Modifier::DIM); }
                            if run.flags & 2  != 0 { style = style.add_modifier(Modifier::BOLD); }
                            if run.flags & 4  != 0 { style = style.add_modifier(Modifier::ITALIC); }
                            if run.flags & 8  != 0 { style = style.add_modifier(Modifier::UNDERLINED); }
                            if run.flags & 16 != 0 { style = style.add_modifier(Modifier::REVERSED); }
                            if run.flags & 32 != 0 { style = style.add_modifier(Modifier::SLOW_BLINK); }
                            if run.flags & 128 != 0 { style = style.add_modifier(Modifier::CROSSED_OUT); }
                            // ratatui-crossterm omits SGR 8 (HIDDEN), render as spaces
                            let text: &str = if run.flags & 64 != 0 {
                                " "
                            } else if run.text.is_empty() {
                                " "
                            } else {
                                &run.text
                            };
                            let run_w = run.width.max(1);
                            if col + run_w > inner_w {
                                let avail = (inner_w - col) as usize;
                                let truncated: String = text.chars().take(avail).collect();
                                if !truncated.is_empty() {
                                    spans.push(Span::styled(truncated, style));
                                }
                                col = inner_w;
                            } else {
                                spans.push(Span::styled(text.to_string(), style));
                                col += run_w;
                            }
                        }
                        lines.push(Line::from(spans));
                    }
                } else {
                    // Fallback: plain text lines for non-PTY popups
                    for line_str in &srv_popup_lines {
                        lines.push(Line::from(line_str.clone()));
                    }
                }
                let para = Paragraph::new(Text::from(lines)).block(block).scroll((srv_popup_scroll, 0));
                f.render_widget(Clear, popup_area);
                f.render_widget(para, popup_area);
            }
            if srv_confirm_active {
                let overlay = Block::default().borders(Borders::ALL).title("confirm");
                let oa = centered_rect(60, 3, content_chunk);
                f.render_widget(Clear, oa);
                f.render_widget(&overlay, oa);
                let para = Paragraph::new(srv_confirm_prompt.clone());
                f.render_widget(para, overlay.inner(oa));
            }
            if srv_menu_active {
                let sel_style = crate::rendering::parse_tmux_style(&mode_style_str);
                let title_str = if srv_menu_title.is_empty() { "Menu".to_string() } else { srv_menu_title.clone() };
                let overlay = Block::default().borders(Borders::ALL).title(title_str).border_style(sel_style);
                let item_count = srv_menu_items.len();
                let menu_h = ((item_count as u16).saturating_add(2)).max(3).min(content_chunk.height.saturating_sub(2));
                let oa = centered_rect(50, menu_h, content_chunk);
                f.render_widget(Clear, oa);
                f.render_widget(&overlay, oa);
                let inner = overlay.inner(oa);
                let mut lines: Vec<Line<'static>> = Vec::new();
                for (i, item) in srv_menu_items.iter().enumerate() {
                    if item.sep {
                        lines.push(Line::from("".repeat(inner.width as usize)));
                    } else {
                        let name = item.name.clone().unwrap_or_default();
                        let key_str = item.key.clone().unwrap_or_default();
                        let label = if key_str.is_empty() { name } else { format!("{} ({})", name, key_str) };
                        if i == srv_menu_selected {
                            lines.push(Line::from(Span::styled(label, sel_style)));
                        } else {
                            lines.push(Line::from(label));
                        }
                    }
                }
                let para = Paragraph::new(Text::from(lines));
                f.render_widget(para, inner);
            }
            if srv_customize_active {
                // Full-screen overlay for customize-mode
                let area = content_chunk;
                let overlay = Rect {
                    x: area.x + 2,
                    y: area.y + 1,
                    width: area.width.saturating_sub(4).min(100),
                    height: area.height.saturating_sub(2),
                };
                f.render_widget(Clear, overlay);
                let header_style = Style::default().fg(Color::Black).bg(Color::Cyan).add_modifier(Modifier::BOLD);
                let header = if srv_customize_filter.is_empty() {
                    " Customize Mode  [q:exit  /:filter  Enter:edit  d:reset default] "
                } else {
                    " Customize Mode  [q:exit  /:clear filter  Enter:edit  d:reset] "
                };
                if overlay.height > 0 {
                    let header_area = Rect { x: overlay.x, y: overlay.y, width: overlay.width, height: 1 };
                    let hdr = Paragraph::new(Line::from(Span::styled(
                        format!("{:<width$}", header, width = overlay.width as usize),
                        header_style,
                    )));
                    f.render_widget(hdr, header_area);
                }
                // Filter indicator
                let body_start = overlay.y + 1;
                if !srv_customize_filter.is_empty() && overlay.height > 1 {
                    let filter_area = Rect { x: overlay.x, y: body_start, width: overlay.width, height: 1 };
                    let filter_style = Style::default().fg(Color::Yellow).bg(Color::DarkGray);
                    let ftxt = format!(" Filter: {} ", srv_customize_filter);
                    f.render_widget(Paragraph::new(Line::from(Span::styled(
                        format!("{:<width$}", ftxt, width = overlay.width as usize), filter_style,
                    ))), filter_area);
                }
                let list_start = if srv_customize_filter.is_empty() { body_start } else { body_start + 1 };
                let list_height = overlay.y.saturating_add(overlay.height).saturating_sub(list_start) as usize;
                // Column header
                if list_height > 0 {
                    let col_hdr_area = Rect { x: overlay.x, y: list_start, width: overlay.width, height: 1 };
                    let col_style = Style::default().fg(Color::White).bg(Color::DarkGray).add_modifier(Modifier::BOLD);
                    let name_w = (overlay.width as usize / 2).max(20);
                    let col_text = format!(" {:<nw$} {}", "Option", "Value", nw = name_w.saturating_sub(2));
                    f.render_widget(Paragraph::new(Line::from(Span::styled(
                        format!("{:<width$}", col_text, width = overlay.width as usize), col_style,
                    ))), col_hdr_area);
                }
                let rows_start = list_start + 1;
                let rows_height = overlay.y.saturating_add(overlay.height).saturating_sub(rows_start) as usize;
                // Render visible option rows
                let visible_opts: Vec<&CustomizeOption> = srv_customize_options.iter()
                    .skip(srv_customize_scroll)
                    .take(rows_height)
                    .collect();
                for (row_idx, opt) in visible_opts.iter().enumerate() {
                    if rows_start + row_idx as u16 >= overlay.y + overlay.height { break; }
                    let row_area = Rect {
                        x: overlay.x,
                        y: rows_start + row_idx as u16,
                        width: overlay.width,
                        height: 1,
                    };
                    let is_selected = opt.i == srv_customize_selected;
                    let name_w = (overlay.width as usize / 2).max(20);
                    let scope_prefix = match opt.s.as_str() {
                        "server" => "[S] ",
                        "session" => "[s] ",
                        "window" => "[w] ",
                        "pane" => "[p] ",
                        _ => "    ",
                    };
                    let name_display = format!("{}{}", scope_prefix, opt.n);
                    let value_display = if is_selected && srv_customize_editing {
                        let buf = &srv_customize_edit_buf;
                        format!("{}|", buf)
                    } else {
                        opt.v.clone()
                    };
                    let line_text = format!(" {:<nw$} {}", name_display, value_display, nw = name_w.saturating_sub(2));
                    let style = if is_selected {
                        if srv_customize_editing {
                            Style::default().fg(Color::Black).bg(Color::Yellow)
                        } else {
                            Style::default().fg(Color::Black).bg(Color::White)
                        }
                    } else {
                        Style::default().fg(Color::White).bg(Color::Reset)
                    };
                    f.render_widget(Paragraph::new(Line::from(Span::styled(
                        format!("{:<width$}", line_text, width = overlay.width as usize), style,
                    ))), row_area);
                }
            }
            if srv_display_panes {
                // Render pane numbers overlay (like tmux display-panes)
                fn collect_leaf_rects(node: &LayoutJson, area: Rect, out: &mut Vec<Rect>) {
                    match node {
                        LayoutJson::Leaf { .. } => { out.push(area); }
                        LayoutJson::Split { kind, sizes, children } => {
                            let effective_sizes: Vec<u16> = if sizes.len() == children.len() {
                                sizes.clone()
                            } else {
                                vec![(100 / children.len().max(1)) as u16; children.len()]
                            };
                            let is_horizontal = kind == "Horizontal";
                            let rects = crate::tree::split_with_gaps(is_horizontal, &effective_sizes, area);
                            for (i, child) in children.iter().enumerate() {
                                if i < rects.len() { collect_leaf_rects(child, rects[i], out); }
                            }
                        }
                    }
                }
                let mut leaf_rects = Vec::new();
                collect_leaf_rects(&root, content_chunk, &mut leaf_rects);
                for (idx, prect) in leaf_rects.iter().enumerate() {
                    if prect.width >= 7 && prect.height >= 3 {
                        let bw = 7u16; let bh = 3u16;
                        let bx = prect.x + prect.width.saturating_sub(bw) / 2;
                        let by = prect.y + prect.height.saturating_sub(bh) / 2;
                        let b = Rect { x: bx, y: by, width: bw, height: bh };
                        let pane_sel_style = Style::default().fg(Color::Yellow).bg(Color::Black).add_modifier(Modifier::BOLD);
                        let block = Block::default().borders(Borders::ALL).style(pane_sel_style);
                        let inner = block.inner(b);
                        let disp = ((idx + srv_pane_base_index) % 10).to_string();
                        let para = Paragraph::new(Line::from(Span::styled(
                            format!(" {} ", disp),
                            pane_sel_style,
                        ))).alignment(Alignment::Center);
                        f.render_widget(Clear, b);
                        f.render_widget(block, b);
                        f.render_widget(para, inner);
                    }
                }
            }

        })?;
        if client_log_enabled() {
            client_log("draw", &format!("draw OK, render={}us overlays: popup={} confirm={} menu={} display_panes={}",
                _t_parse.elapsed().as_micros().saturating_sub(_parse_us as u128),
                srv_popup_active, srv_confirm_active, srv_menu_active, srv_display_panes
            ));
        }

        // ── Post-draw: emit buffered OSC 52 clipboard ────────────────
        // Written AFTER terminal.draw() so it doesn't interfere with
        // ratatui's VT output buffer.
        if let Some(clip_text) = pending_osc52.take() {
            crate::copy_mode::emit_osc52(&mut std::io::stdout(), &clip_text);
        }

        // ── Post-draw: emit audible bell ─────────────────────────────
        if pending_bell {
            pending_bell = false;
            let _ = std::io::Write::write_all(&mut std::io::stdout(), b"\x07");
            let _ = std::io::Write::flush(&mut std::io::stdout());
        }

        // ── SSH: periodic mouse-enable refresh ───────────────────────
        // ConPTY or terminal resize can silently disable mouse reporting.
        // Re-send every 30 seconds to keep mouse working reliably.
        if is_ssh_mode && last_mouse_enable.elapsed().as_secs() >= 30 {
            crate::ssh_input::send_mouse_enable();
            last_mouse_enable = Instant::now();
        }

        // ── Post-draw: atomic cursor write ──────────────────────────
        // Write cursor visibility + position + style as ONE batch to
        // avoid the separate execute!() flushes that ratatui's normal
        // show_cursor()/set_cursor_position() would produce.  Multiple
        // separate console writes create intermediate states visible
        // to WT between vsync frames, causing rapid cursor flicker.
        {
            use std::io::Write;
            fn find_active_cursor_shape(node: &LayoutJson) -> Option<u8> {
                match node {
                    LayoutJson::Leaf { active, cursor_shape, .. } => {
                        if *active && *cursor_shape >= 1 && *cursor_shape <= 6 { Some(*cursor_shape) } else { None }
                    }
                    LayoutJson::Split { children, .. } => {
                        children.iter().find_map(find_active_cursor_shape)
                    }
                }
            }
            let effective = find_active_cursor_shape(&root)
                .unwrap_or_else(|| state_cursor_style_code.unwrap_or_else(crate::rendering::configured_cursor_code));
            // Compute the active pane's screen Rect so we can translate
            // pane-local cursor coords to terminal-global coords.
            fn find_active_rect(node: &LayoutJson, area: Rect) -> Option<Rect> {
                match node {
                    LayoutJson::Leaf { active, .. } => {
                        if *active { Some(area) } else { None }
                    }
                    LayoutJson::Split { kind, sizes, children } => {
                        let eff: Vec<u16> = if sizes.len() == children.len() {
                            sizes.clone()
                        } else {
                            vec![(100 / children.len().max(1)) as u16; children.len()]
                        };
                        let rects = crate::tree::split_with_gaps(kind == "Horizontal", &eff, area);
                        for (i, child) in children.iter().enumerate() {
                            if i < rects.len() {
                                if let Some(r) = find_active_rect(child, rects[i]) { return Some(r); }
                            }
                        }
                        None
                    }
                }
            }
            let active_pane_area: Option<Rect> = {
                let sz = terminal.size().unwrap_or_default();
                let constraints = if status_at_top {
                    vec![Constraint::Length(status_lines as u16), Constraint::Min(1)]
                } else {
                    vec![Constraint::Min(1), Constraint::Length(status_lines as u16)]
                };
                let chunks = Layout::default().direction(Direction::Vertical)
                    .constraints(constraints).split(sz.into());
                let content_chunk = if status_at_top { chunks[1] } else { chunks[0] };
                find_active_rect(&root, content_chunk)
            };
            // Compute screen-global cursor position from pane-local coords.
            let cursor_visible = if let (Some((cc, cr)), Some(inner)) = (post_draw_cursor, active_pane_area) {
                let cy = inner.y + cr.min(inner.height.saturating_sub(1));
                let cx = inner.x + cc.min(inner.width.saturating_sub(1));
                Some((cx, cy))
            } else {
                None
            };
            // Build a single VT string with: ?25h + CUP + DECSCUSR
            // ratatui's draw() always emits ?25l (since we never call
            // f.set_cursor_position), so we must re-emit ?25h + CUP
            // every frame when the cursor should be visible.
            let mut buf = String::with_capacity(32);
            if let Some((cx, cy)) = cursor_visible {
                buf.push_str("\x1b[?25h");
                use std::fmt::Write as FmtWrite;
                let _ = write!(buf, "\x1b[{};{}H", cy + 1, cx + 1);
            }
            // DECSCUSR only when style actually changes (avoids blink
            // timer resets in WT).
            if effective != last_cursor_style {
                last_cursor_style = effective;
                use std::fmt::Write as FmtWrite;
                let _ = write!(buf, "\x1b[{} q", effective);
            }
            if !buf.is_empty() {
                let mut out = std::io::stdout().lock();
                let _ = out.write_all(buf.as_bytes());
                let _ = out.flush();
            }

            // Update Win32 system caret for accessibility / speech-to-text
            // tools (e.g. Wispr Flow).  Skip for SSH sessions — no local
            // console window.
            if !is_ssh_mode {
                if let Some((cx, cy)) = cursor_visible {
                    crate::platform::caret::update(cx, cy);
                }
            }
        }

        let _render_us = _t_parse.elapsed().as_micros().saturating_sub(_parse_us as u128);
        last_dump_time = Instant::now();
        // Latency log: measure full cycle from key-send to render-complete
        if let (Some(ref mut log), Some(ks)) = (&mut latency_log, key_send_instant) {
            let elapsed_ms = ks.elapsed().as_millis();
            loop_count += 1;
            use std::io::Write;
            let _ = writeln!(log, "L{}: key->render {}ms  parse={}us  render={}us  json_len={}  since_dump={}",
                loop_count, elapsed_ms, _parse_us, _render_us, dump_buf.len(), since_dump);
            // Only clear after we rendered a DIFFERENT frame (echo arrived)
            if got_frame && dump_buf != prev_dump_buf {
                let _ = writeln!(log, "L{}: ECHO VISIBLE after {}ms  (parse={}us render={}us)",
                    loop_count, elapsed_ms, _parse_us, _render_us);
                key_send_instant = None;
            }
        }
        selection_changed = false;
        // Cache this frame so we can skip identical re-renders.
        // Only update cache when we got a genuinely new frame (not selection-only redraw)
        if got_frame && dump_buf != prev_dump_buf {
            std::mem::swap(&mut prev_dump_buf, &mut dump_buf);
        }
        // DON'T clear last_key_send_time — keep fast-dumping for 100ms
        // after last keystroke so we catch the ConPTY echo promptly.
        // The timer expires naturally in the poll_ms calculation above.
        // Clear key_send_instant once echo arrives (frame differs).
        if got_frame && dump_buf != prev_dump_buf {
            key_send_instant = None;
        }
        force_dump = false;
    }

    // Clean disconnect on persistent connection
    let _ = writer.write_all(b"client-detach\n");
    let _ = writer.flush();
    Ok(())
}

/// Flush the paste-pending buffer as individual send-text / send-key commands.
/// Called when a non-bufferable key (Backspace, Delete, Esc, BackTab) interrupts
/// a potential paste burst, so we emit whatever we had as normal keystrokes.
#[cfg(windows)]
fn flush_paste_pend_as_text(
    paste_pend: &mut String,
    paste_pend_start: &mut Option<Instant>,
    paste_stage2: &mut bool,
    cmd_batch: &mut Vec<String>,
) {
    if paste_pend.is_empty() {
        return;
    }
    // If we accumulated enough ASCII chars that stage2 was entered, this
    // is almost certainly pasted content — send as send-paste so the server
    // wraps it in bracketed paste sequences (fixes nvim autoindent).
    // Non-ASCII buffers (IME input) are always flushed as normal text to
    // avoid the 300ms delay (fixes #91).
    let has_non_ascii = paste_pend.chars().any(|c| !c.is_ascii());
    if (*paste_stage2 || paste_pend.len() >= 3) && !has_non_ascii {
        let encoded = crate::util::base64_encode(paste_pend);
        cmd_batch.push(format!("send-paste {}\n", encoded));
    } else {
        for c in paste_pend.chars() {
            match c {
                '\n' => { cmd_batch.push("send-key enter\n".into()); }
                '\t' => { cmd_batch.push("send-key tab\n".into()); }
                ' '  => { cmd_batch.push("send-key space\n".into()); }
                _ => {
                    let escaped = match c {
                        '"' => "\\\"".to_string(),
                        '\\' => "\\\\".to_string(),
                        _ => c.to_string(),
                    };
                    cmd_batch.push(format!("send-text \"{}\"\n", escaped));
                }
            }
        }
    }
    paste_pend.clear();
    *paste_pend_start = None;
    *paste_stage2 = false;
}

/// Returns true if the buffer contains any non-ASCII characters (IME / CJK input).
/// Used by the paste detection heuristic to skip Stage 2 for IME input (fixes #91).
#[cfg(windows)]
fn paste_buffer_has_non_ascii(buf: &str) -> bool {
    buf.chars().any(|c| !c.is_ascii())
}

#[cfg(test)]
#[path = "../tests-rs/test_client.rs"]
mod tests;