bubbletea-widgets 0.1.12

A collection of reusable TUI components for building terminal applications with bubbletea-rs
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
2374
2375
2376
2377
2378
2379
2380
2381
2382
2383
2384
2385
2386
2387
2388
2389
2390
2391
2392
2393
2394
2395
2396
2397
2398
2399
2400
2401
2402
2403
2404
2405
2406
2407
2408
2409
2410
2411
2412
2413
2414
2415
2416
2417
2418
2419
2420
//! Scrollable viewport component for displaying large content in terminal applications.
//!
//! This module provides a sophisticated viewport component that enables smooth scrolling
//! through content that exceeds the available display area. It supports both vertical
//! and horizontal scrolling, efficient rendering of large datasets, and comprehensive
//! keyboard navigation with customizable key bindings.
//!
//! # Core Features
//!
//! - **Bidirectional Scrolling**: Smooth vertical and horizontal content navigation
//! - **Efficient Rendering**: Only visible content is processed for optimal performance
//! - **Vim-Style Navigation**: Familiar keyboard shortcuts with arrow key alternatives
//! - **Content Management**: Support for both string and line-based content
//! - **Styling Integration**: Full lipgloss styling support with frame calculations
//! - **Mouse Support**: Configurable mouse wheel scrolling (when available)
//! - **Position Tracking**: Precise scroll percentage and boundary detection
//!
//! # Quick Start
//!
//! ```rust
//! use bubbletea_widgets::viewport::{new, Model};
//!
//! // Create a viewport with specific dimensions
//! let mut viewport = new(80, 24);
//!
//! // Set content to display
//! viewport.set_content("Line 1\nLine 2\nLine 3\nLine 4\nLine 5");
//!
//! // Navigate through content
//! viewport.scroll_down(1);     // Scroll down one line
//! viewport.page_down();        // Scroll down one page
//! viewport.goto_bottom();      // Jump to end
//!
//! // Check current state
//! let visible = viewport.visible_lines();
//! let progress = viewport.scroll_percent();
//! ```
//!
//! # Integration with Bubble Tea
//!
//! ```rust
//! use bubbletea_widgets::viewport::{Model as ViewportModel, ViewportKeyMap};
//! use bubbletea_rs::{Model as BubbleTeaModel, Cmd, Msg};
//! use lipgloss_extras::prelude::*;
//!
//! struct DocumentViewer {
//!     viewport: ViewportModel,
//!     content: String,
//! }
//!
//! impl BubbleTeaModel for DocumentViewer {
//!     fn init() -> (Self, Option<Cmd>) {
//!         let mut viewer = DocumentViewer {
//!             viewport: ViewportModel::new(80, 20),
//!             content: "Large document content...".to_string(),
//!         };
//!         viewer.viewport.set_content(&viewer.content);
//!         (viewer, None)
//!     }
//!
//!     fn update(&mut self, msg: Msg) -> Option<Cmd> {
//!         // Forward navigation messages to viewport
//!         self.viewport.update(msg)
//!     }
//!
//!     fn view(&self) -> String {
//!         format!(
//!             "Document Viewer\n\n{}\n\nScroll: {:.1}%",
//!             self.viewport.view(),
//!             self.viewport.scroll_percent() * 100.0
//!         )
//!     }
//! }
//! ```
//!
//! # Advanced Usage
//!
//! ```rust
//! use bubbletea_widgets::viewport::{Model, ViewportKeyMap};
//! use lipgloss_extras::prelude::*;
//!
//! // Create viewport with custom styling
//! let mut viewport = Model::new(60, 15)
//!     .with_style(
//!         Style::new()
//!             .border_style(lipgloss::normal_border())
//!             .border_foreground(Color::from("#874BFD"))
//!             .padding(1, 2, 1, 2)
//!     );
//!
//! // Content from multiple sources
//! let lines: Vec<String> = vec![
//!     "Header information".to_string(),
//!     "Content line 1".to_string(),
//!     "Content line 2".to_string(),
//! ];
//! viewport.set_content_lines(lines);
//!
//! // Configure horizontal scrolling
//! viewport.set_horizontal_step(5); // Scroll 5 columns at a time
//! viewport.scroll_right();         // Move right
//! ```
//!
//! # Navigation Controls
//!
//! | Keys | Action | Description |
//! |------|--------| ----------- |
//! | `↑`, `k` | Line Up | Scroll up one line |
//! | `↓`, `j` | Line Down | Scroll down one line |
//! | `←`, `h` | Left | Scroll left horizontally |
//! | `→`, `l` | Right | Scroll right horizontally |
//! | `PgUp`, `b` | Page Up | Scroll up one page |
//! | `PgDn`, `f`, `Space` | Page Down | Scroll down one page |
//! | `u` | Half Page Up | Scroll up half a page |
//! | `d` | Half Page Down | Scroll down half a page |
//!
//! # Performance Optimization
//!
//! The viewport is designed for efficient handling of large content:
//!
//! - Only visible lines are rendered, regardless of total content size
//! - Scrolling operations return affected lines for incremental updates
//! - String operations are optimized for Unicode content
//! - Frame size calculations account for lipgloss styling overhead
//!
//! # Content Types
//!
//! The viewport supports various content formats:
//!
//! - **Plain text**: Simple string content with automatic line splitting
//! - **Pre-formatted lines**: Vector of strings for precise line control
//! - **Unicode content**: Full support for wide characters and emojis
//! - **Styled content**: Integration with lipgloss for rich formatting
//!
//! # State Management
//!
//! Track viewport state with built-in methods:
//!
//! - `at_top()` / `at_bottom()`: Boundary detection
//! - `scroll_percent()`: Vertical scroll progress (0.0 to 1.0)
//! - `horizontal_scroll_percent()`: Horizontal scroll progress
//! - `line_count()`: Total content lines
//! - `visible_lines()`: Currently displayed content

use crate::key::{self, KeyMap as KeyMapTrait};
use bubbletea_rs::{Cmd, KeyMsg, Model as BubbleTeaModel, Msg};
use crossterm::event::KeyCode;
use lipgloss_extras::lipgloss::width as lg_width;
use lipgloss_extras::prelude::*;
use unicode_width::UnicodeWidthChar;

const SPACEBAR: char = ' ';

/// Keyboard binding configuration for viewport navigation.
///
/// This struct defines all key combinations that control viewport scrolling,
/// including line-by-line movement, page scrolling, and horizontal navigation.
/// Each binding supports multiple key combinations and includes help text for
/// documentation generation.
///
/// # Default Key Bindings
///
/// The default configuration provides both traditional navigation keys and
/// Vim-style alternatives for maximum compatibility:
///
/// - **Line Movement**: Arrow keys (`↑↓`) and Vim keys (`kj`)
/// - **Page Movement**: Page Up/Down and Vim keys (`bf`)
/// - **Half Page**: Vim-style `u` (up) and `d` (down)
/// - **Horizontal**: Arrow keys (`←→`) and Vim keys (`hl`)
///
/// # Examples
///
/// ```rust
/// use bubbletea_widgets::viewport::{ViewportKeyMap, Model};
/// use bubbletea_widgets::key;
/// use crossterm::event::KeyCode;
///
/// // Use default key bindings
/// let mut viewport = Model::new(80, 24);
/// let keymap = viewport.keymap.clone(); // Uses ViewportKeyMap::default()
///
/// // Customize key bindings
/// let mut custom_keymap = ViewportKeyMap::default();
/// custom_keymap.page_down = key::Binding::new(vec![KeyCode::Char('n')])
///     .with_help("n", "next page");
/// custom_keymap.page_up = key::Binding::new(vec![KeyCode::Char('p')])
///     .with_help("p", "previous page");
///
/// viewport.keymap = custom_keymap;
/// ```
///
/// Integration with help system:
/// ```rust
/// use bubbletea_widgets::viewport::ViewportKeyMap;
/// use bubbletea_widgets::key::KeyMap as KeyMapTrait;
///
/// let keymap = ViewportKeyMap::default();
///
/// // Get essential bindings for compact help
/// let short_help = keymap.short_help();
/// assert_eq!(short_help.len(), 4); // up, down, page_up, page_down
///
/// // Get all bindings organized by category
/// let full_help = keymap.full_help();
/// assert_eq!(full_help.len(), 4); // 4 categories of bindings
/// ```
///
/// # Customization Patterns
///
/// Common customization scenarios:
///
/// ```rust
/// use bubbletea_widgets::viewport::ViewportKeyMap;
/// use bubbletea_widgets::key;
/// use crossterm::event::KeyCode;
///
/// let mut keymap = ViewportKeyMap::default();
///
/// // Add additional keys for page navigation
/// keymap.page_down = key::Binding::new(vec![
///     KeyCode::PageDown,
///     KeyCode::Char(' '),    // Space bar (default)
///     KeyCode::Char('f'),    // Vim style (default)
///     KeyCode::Enter,        // Custom addition
/// ]).with_help("space/f/enter", "next page");
///
/// // Game-style WASD navigation
/// keymap.up = key::Binding::new(vec![KeyCode::Char('w')])
///     .with_help("w", "move up");
/// keymap.down = key::Binding::new(vec![KeyCode::Char('s')])
///     .with_help("s", "move down");
/// keymap.left = key::Binding::new(vec![KeyCode::Char('a')])
///     .with_help("a", "move left");
/// keymap.right = key::Binding::new(vec![KeyCode::Char('d')])
///     .with_help("d", "move right");
/// ```
#[derive(Debug, Clone)]
pub struct ViewportKeyMap {
    /// Key binding for scrolling down one full page.
    ///
    /// Default keys: Page Down, Space, `f` (Vim-style "forward")
    pub page_down: key::Binding,
    /// Key binding for scrolling up one full page.
    ///
    /// Default keys: Page Up, `b` (Vim-style "backward")
    pub page_up: key::Binding,
    /// Key binding for scrolling up half a page.
    ///
    /// Default key: `u` (Vim-style "up half page")
    pub half_page_up: key::Binding,
    /// Key binding for scrolling down half a page.
    ///
    /// Default key: `d` (Vim-style "down half page")
    pub half_page_down: key::Binding,
    /// Key binding for scrolling down one line.
    ///
    /// Default keys: Down arrow (`↓`), `j` (Vim-style)
    pub down: key::Binding,
    /// Key binding for scrolling up one line.
    ///
    /// Default keys: Up arrow (`↑`), `k` (Vim-style)
    pub up: key::Binding,
    /// Key binding for horizontal scrolling to the left.
    ///
    /// Default keys: Left arrow (`←`), `h` (Vim-style)
    pub left: key::Binding,
    /// Key binding for horizontal scrolling to the right.
    ///
    /// Default keys: Right arrow (`→`), `l` (Vim-style)
    pub right: key::Binding,
}

impl Default for ViewportKeyMap {
    /// Creates default viewport key bindings with Vim-style alternatives.
    ///
    /// The default configuration provides comprehensive navigation options
    /// that accommodate both traditional arrow key users and Vim enthusiasts.
    /// Each binding includes multiple key combinations for flexibility.
    ///
    /// # Default Key Mappings
    ///
    /// | Binding | Keys | Description |
    /// |---------|------|-------------|
    /// | `page_down` | `PgDn`, `Space`, `f` | Scroll down one page |
    /// | `page_up` | `PgUp`, `b` | Scroll up one page |
    /// | `half_page_down` | `d` | Scroll down half page |
    /// | `half_page_up` | `u` | Scroll up half page |
    /// | `down` | `↓`, `j` | Scroll down one line |
    /// | `up` | `↑`, `k` | Scroll up one line |
    /// | `left` | `←`, `h` | Scroll left horizontally |
    /// | `right` | `→`, `l` | Scroll right horizontally |
    ///
    /// # Examples
    ///
    /// ```rust
    /// use bubbletea_widgets::viewport::ViewportKeyMap;
    ///
    /// // Create with default bindings
    /// let keymap = ViewportKeyMap::default();
    ///
    /// // Verify some default key combinations
    /// assert!(!keymap.page_down.keys().is_empty());
    /// assert!(!keymap.up.keys().is_empty());
    /// ```
    ///
    /// # Design Philosophy
    ///
    /// - **Accessibility**: Arrow keys work for all users
    /// - **Efficiency**: Vim keys provide rapid navigation for power users
    /// - **Consistency**: Key choices match common terminal application patterns
    /// - **Discoverability**: Help text explains each binding clearly
    fn default() -> Self {
        Self {
            page_down: key::Binding::new(vec![
                KeyCode::PageDown,
                KeyCode::Char(SPACEBAR),
                KeyCode::Char('f'),
            ])
            .with_help("f/pgdn", "page down"),
            page_up: key::Binding::new(vec![KeyCode::PageUp, KeyCode::Char('b')])
                .with_help("b/pgup", "page up"),
            half_page_up: key::Binding::new(vec!["u", "ctrl+u"]).with_help("u/ctrl+u", "½ page up"),
            half_page_down: key::Binding::new(vec!["d", "ctrl+d"])
                .with_help("d/ctrl+d", "½ page down"),
            up: key::Binding::new(vec![KeyCode::Up, KeyCode::Char('k')]).with_help("↑/k", "up"),
            down: key::Binding::new(vec![KeyCode::Down, KeyCode::Char('j')])
                .with_help("↓/j", "down"),
            left: key::Binding::new(vec![KeyCode::Left, KeyCode::Char('h')])
                .with_help("←/h", "move left"),
            right: key::Binding::new(vec![KeyCode::Right, KeyCode::Char('l')])
                .with_help("→/l", "move right"),
        }
    }
}

impl KeyMapTrait for ViewportKeyMap {
    /// Returns the most essential key bindings for compact help display.
    ///
    /// This method provides a concise list of the most frequently used
    /// navigation keys, suitable for brief help displays or status bars.
    ///
    /// # Returns
    ///
    /// A vector containing bindings for: up, down, page up, page down
    ///
    /// # Examples
    ///
    /// ```rust
    /// use bubbletea_widgets::viewport::ViewportKeyMap;
    /// use bubbletea_widgets::key::KeyMap as KeyMapTrait;
    ///
    /// let keymap = ViewportKeyMap::default();
    /// let essential_keys = keymap.short_help();
    ///
    /// assert_eq!(essential_keys.len(), 4);
    /// // Contains: up, down, page_up, page_down
    /// ```
    fn short_help(&self) -> Vec<&key::Binding> {
        vec![&self.up, &self.down, &self.page_up, &self.page_down]
    }

    /// Returns all key bindings organized by navigation category.
    ///
    /// This method groups related navigation keys together for comprehensive
    /// help displays. Each group represents a logical category of movement.
    ///
    /// # Returns
    ///
    /// A vector of binding groups:
    /// 1. **Line navigation**: up, down
    /// 2. **Horizontal navigation**: left, right  
    /// 3. **Page navigation**: page up, page down
    /// 4. **Half-page navigation**: half page up, half page down
    ///
    /// # Examples
    ///
    /// ```rust
    /// use bubbletea_widgets::viewport::ViewportKeyMap;
    /// use bubbletea_widgets::key::KeyMap as KeyMapTrait;
    ///
    /// let keymap = ViewportKeyMap::default();
    /// let all_keys = keymap.full_help();
    ///
    /// assert_eq!(all_keys.len(), 4); // 4 categories
    /// assert_eq!(all_keys[0].len(), 2); // Line navigation: up, down
    /// assert_eq!(all_keys[1].len(), 2); // Horizontal: left, right
    /// assert_eq!(all_keys[2].len(), 2); // Page: page_up, page_down
    /// assert_eq!(all_keys[3].len(), 2); // Half-page: half_page_up, half_page_down
    /// ```
    ///
    /// # Help Display Integration
    ///
    /// This organization enables structured help displays:
    /// ```text
    /// Navigation:
    ///   ↑/k, ↓/j     line up, line down
    ///   ←/h, →/l     scroll left, scroll right
    ///   
    ///   b/pgup, f/pgdn/space    page up, page down
    ///   u, d                     half page up, half page down
    /// ```
    fn full_help(&self) -> Vec<Vec<&key::Binding>> {
        vec![
            vec![&self.up, &self.down],
            vec![&self.left, &self.right],
            vec![&self.page_up, &self.page_down],
            vec![&self.half_page_up, &self.half_page_down],
        ]
    }
}

/// High-performance scrollable viewport for displaying large content efficiently.
///
/// This struct represents a complete viewport implementation that can handle content
/// larger than the available display area. It provides smooth scrolling in both
/// vertical and horizontal directions, efficient rendering of only visible content,
/// and comprehensive keyboard navigation.
///
/// # Core Features
///
/// - **Efficient Rendering**: Only visible content is processed, enabling smooth performance with large datasets
/// - **Bidirectional Scrolling**: Full support for both vertical and horizontal content navigation
/// - **Content Management**: Flexible content input via strings or line vectors
/// - **Styling Integration**: Full lipgloss styling support with automatic frame calculations
/// - **Position Tracking**: Precise scroll percentages and boundary detection
/// - **Keyboard Navigation**: Comprehensive key bindings with Vim-style alternatives
///
/// # Examples
///
/// Basic viewport setup:
/// ```rust
/// use bubbletea_widgets::viewport::Model;
///
/// // Create a viewport with specific dimensions
/// let mut viewport = Model::new(80, 24);
///
/// // Add content to display
/// let content = "Line 1\nLine 2\nLine 3\nVery long line that extends beyond viewport width\nLine 5";
/// viewport.set_content(content);
///
/// // Navigate through content
/// viewport.scroll_down(2);  // Move down 2 lines
/// viewport.page_down();     // Move down one page
///
/// // Check current state
/// println!("At bottom: {}", viewport.at_bottom());
/// println!("Scroll progress: {:.1}%", viewport.scroll_percent() * 100.0);
/// ```
///
/// Integration with styling:
/// ```rust
/// use bubbletea_widgets::viewport::Model;
/// use lipgloss_extras::prelude::*;
///
/// let viewport = Model::new(60, 20)
///     .with_style(
///         Style::new()
///             .border_style(lipgloss::normal_border())
///             .border_foreground(Color::from("#874BFD"))
///             .padding(1, 2, 1, 2)
///     );
/// ```
///
/// Working with line-based content:
/// ```rust
/// use bubbletea_widgets::viewport::Model;
///
/// let mut viewport = Model::new(50, 15);
///
/// // Set content from individual lines
/// let lines = vec![
///     "Header Line".to_string(),
///     "Content Line 1".to_string(),
///     "Content Line 2".to_string(),
/// ];
/// viewport.set_content_lines(lines);
///
/// // Get currently visible content
/// let visible = viewport.visible_lines();
/// println!("Displaying {} lines", visible.len());
/// ```
///
/// # Performance Characteristics
///
/// - **Memory**: Only stores content lines, not rendered output
/// - **CPU**: Rendering scales with viewport size, not content size
/// - **Scrolling**: Incremental updates return only affected lines
/// - **Unicode**: Proper width calculation for international content
///
/// # Thread Safety
///
/// The Model struct is `Clone` and can be safely used across threads.
/// All internal state is self-contained and doesn't rely on external resources.
///
/// # State Management
///
/// The viewport maintains several key pieces of state:
/// - **Content**: Lines of text stored internally
/// - **Position**: Current scroll offsets for both axes
/// - **Dimensions**: Viewport size and styling frame calculations
/// - **Configuration**: Mouse settings, scroll steps, and key bindings
#[derive(Debug, Clone)]
pub struct Model {
    /// Display width of the viewport in characters.
    ///
    /// This determines how many characters are visible horizontally.
    /// Content wider than this will require horizontal scrolling to view.
    pub width: usize,
    /// Display height of the viewport in lines.
    ///
    /// This determines how many lines of content are visible at once.
    /// Content with more lines will require vertical scrolling to view.
    pub height: usize,
    /// Lipgloss style applied to the viewport content.
    ///
    /// This style affects the entire viewport area and can include borders,
    /// padding, margins, and background colors. Frame sizes are automatically
    /// calculated and subtracted from the available content area.
    pub style: Style,
    /// Whether mouse wheel scrolling is enabled.
    ///
    /// When `true`, mouse wheel events will scroll the viewport content.
    /// Note: Actual mouse wheel support depends on the terminal and
    /// bubbletea-rs mouse event capabilities.
    pub mouse_wheel_enabled: bool,
    /// Number of lines to scroll per mouse wheel event.
    ///
    /// Default is 3 lines per wheel "click", which provides smooth scrolling
    /// without being too sensitive. Adjust based on content density.
    pub mouse_wheel_delta: usize,
    /// Current vertical scroll position (lines from top).
    ///
    /// This value indicates how many lines have been scrolled down from
    /// the beginning of the content. 0 means showing from the first line.
    pub y_offset: usize,
    /// Current horizontal scroll position (characters from left).
    ///
    /// This value indicates how many characters have been scrolled right
    /// from the beginning of each line. 0 means showing from column 0.
    pub x_offset: usize,
    /// Number of characters to scroll horizontally per step.
    ///
    /// Controls the granularity of horizontal scrolling. Smaller values
    /// provide finer control, larger values enable faster navigation.
    pub horizontal_step: usize,
    /// Vertical position of viewport in terminal for performance rendering.
    ///
    /// Used for optimized rendering in some terminal applications.
    /// Generally can be left at default (0) unless implementing
    /// advanced rendering optimizations.
    pub y_position: usize,
    /// Keyboard binding configuration for navigation.
    ///
    /// Defines which keys control scrolling behavior. Can be customized
    /// to match application-specific navigation patterns or user preferences.
    pub keymap: ViewportKeyMap,

    // Internal state
    /// Content lines stored for display.
    ///
    /// Internal storage for the content being displayed. Managed automatically
    /// when content is set via `set_content()` or `set_content_lines()`.
    lines: Vec<String>,
    /// Width of the longest content line in characters.
    ///
    /// Cached value used for horizontal scrolling calculations and
    /// scroll percentage computations. Updated automatically when content changes.
    longest_line_width: usize,
    /// Whether the viewport has been properly initialized.
    ///
    /// Tracks initialization state to ensure proper configuration.
    /// Set automatically during construction and configuration.
    initialized: bool,
}

impl Model {
    /// Creates a new viewport with the specified dimensions.
    ///
    /// This constructor initializes a viewport with the given width and height,
    /// along with sensible defaults for all configuration options. The viewport
    /// starts with no content and is ready to receive text via `set_content()`
    /// or `set_content_lines()`.
    ///
    /// # Arguments
    ///
    /// * `width` - Display width in characters (horizontal viewport size)
    /// * `height` - Display height in lines (vertical viewport size)
    ///
    /// # Returns
    ///
    /// A new `Model` instance with default configuration
    ///
    /// # Examples
    ///
    /// ```rust
    /// use bubbletea_widgets::viewport::Model;
    ///
    /// // Create a standard terminal-sized viewport
    /// let viewport = Model::new(80, 24);
    /// assert_eq!(viewport.width, 80);
    /// assert_eq!(viewport.height, 24);
    /// assert!(viewport.mouse_wheel_enabled);
    /// ```
    ///
    /// Different viewport sizes for various use cases:
    /// ```rust
    /// use bubbletea_widgets::viewport::Model;
    ///
    /// // Compact viewport for sidebar content
    /// let sidebar = Model::new(30, 20);
    ///
    /// // Wide viewport for code display
    /// let code_view = Model::new(120, 40);
    ///
    /// // Small preview viewport
    /// let preview = Model::new(40, 10);
    /// ```
    ///
    /// # Default Configuration
    ///
    /// - **Mouse wheel**: Enabled with 3-line scroll delta
    /// - **Scroll position**: At top-left (0, 0)
    /// - **Horizontal step**: 1 character per scroll
    /// - **Style**: No styling applied
    /// - **Key bindings**: Vim-style with arrow key alternatives
    ///
    /// # Performance
    ///
    /// Viewport creation is very fast as no content processing occurs during
    /// construction. Memory usage scales with content size, not viewport dimensions.
    pub fn new(width: usize, height: usize) -> Self {
        let mut model = Self {
            width,
            height,
            style: Style::new(),
            mouse_wheel_enabled: true,
            mouse_wheel_delta: 3,
            y_offset: 0,
            x_offset: 0,
            horizontal_step: 1,
            y_position: 0,
            keymap: ViewportKeyMap::default(),
            lines: Vec::new(),
            longest_line_width: 0,
            initialized: false,
        };
        model.set_initial_values();
        model
    }

    /// Set initial values for the viewport
    fn set_initial_values(&mut self) {
        self.mouse_wheel_enabled = true;
        self.mouse_wheel_delta = 3;
        self.initialized = true;
    }

    /// Builder method to set viewport dimensions during construction.
    ///
    /// This method allows for fluent construction by updating the viewport
    /// dimensions after creation. Useful when dimensions are computed or
    /// provided by external sources.
    ///
    /// # Arguments
    ///
    /// * `width` - New width in characters
    /// * `height` - New height in lines
    ///
    /// # Returns
    ///
    /// The modified viewport for method chaining
    ///
    /// # Examples
    ///
    /// ```rust
    /// use bubbletea_widgets::viewport::Model;
    /// use lipgloss_extras::prelude::*;
    ///
    /// // Fluent construction with dimensions
    /// let viewport = Model::new(40, 10)
    ///     .with_dimensions(80, 24)
    ///     .with_style(Style::new().padding(1, 2, 1, 2));
    ///
    /// assert_eq!(viewport.width, 80);
    /// assert_eq!(viewport.height, 24);
    /// ```
    ///
    /// Dynamic viewport sizing:
    /// ```rust
    /// use bubbletea_widgets::viewport::Model;
    ///
    /// fn create_responsive_viewport(terminal_width: usize, terminal_height: usize) -> Model {
    ///     Model::new(20, 10) // Default size
    ///         .with_dimensions(
    ///             (terminal_width * 80) / 100,  // 80% of terminal width
    ///             (terminal_height * 60) / 100  // 60% of terminal height
    ///         )
    /// }
    /// ```
    pub fn with_dimensions(mut self, width: usize, height: usize) -> Self {
        self.width = width;
        self.height = height;
        self
    }

    /// Builder method to apply lipgloss styling to the viewport.
    ///
    /// This method sets the visual styling for the entire viewport area.
    /// The style can include borders, padding, margins, colors, and other
    /// lipgloss formatting. Frame sizes are automatically calculated and
    /// subtracted from the content display area.
    ///
    /// # Arguments
    ///
    /// * `style` - Lipgloss style to apply to the viewport
    ///
    /// # Returns
    ///
    /// The styled viewport for method chaining
    ///
    /// # Examples
    ///
    /// ```rust
    /// use bubbletea_widgets::viewport::Model;
    /// use lipgloss_extras::prelude::*;
    ///
    /// // Create viewport with border and padding
    /// let viewport = Model::new(60, 20)
    ///     .with_style(
    ///         Style::new()
    ///             .border_style(lipgloss::normal_border())
    ///             .border_foreground(Color::from("#874BFD"))
    ///             .padding(1, 2, 1, 2)
    ///     );
    /// ```
    ///
    /// Themed viewport styling:
    /// ```rust
    /// use bubbletea_widgets::viewport::Model;
    /// use lipgloss_extras::prelude::*;
    ///
    /// // Dark theme viewport
    /// let dark_viewport = Model::new(80, 24)
    ///     .with_style(
    ///         Style::new()
    ///             .background(Color::from("#1a1a1a"))
    ///             .foreground(Color::from("#ffffff"))
    ///             .border_style(lipgloss::normal_border())
    ///             .border_foreground(Color::from("#444444"))
    ///     );
    ///
    /// // Light theme viewport
    /// let light_viewport = Model::new(80, 24)
    ///     .with_style(
    ///         Style::new()
    ///             .background(Color::from("#ffffff"))
    ///             .foreground(Color::from("#000000"))
    ///             .border_style(lipgloss::normal_border())
    ///             .border_foreground(Color::from("#cccccc"))
    ///     );
    /// ```
    ///
    /// # Frame Size Impact
    ///
    /// Styling with borders and padding reduces the available content area:
    /// ```rust
    /// use bubbletea_widgets::viewport::Model;
    /// use lipgloss_extras::prelude::*;
    ///
    /// // 80x24 viewport with 2-character padding
    /// let viewport = Model::new(80, 24)
    ///     .with_style(
    ///         Style::new().padding(1, 2, 1, 2) // top, right, bottom, left
    ///     );
    ///
    /// // Effective content area is now ~76x22 due to padding
    /// ```
    pub fn with_style(mut self, style: Style) -> Self {
        self.style = style;
        self
    }

    /// Returns whether the viewport is scrolled to the very top of the content.
    ///
    /// This method checks if the vertical scroll position is at the beginning,
    /// meaning no content is hidden above the current view. Useful for
    /// determining when scroll-up operations should be disabled or when
    /// displaying scroll indicators.
    ///
    /// # Returns
    ///
    /// `true` if at the top (y_offset == 0), `false` if content is scrolled
    ///
    /// # Examples
    ///
    /// ```rust
    /// use bubbletea_widgets::viewport::Model;
    ///
    /// let mut viewport = Model::new(40, 5);
    /// let content = (1..=20).map(|i| format!("Line {}", i)).collect::<Vec<_>>().join("\n");
    /// viewport.set_content(&content);
    ///
    /// // Initially at top
    /// assert!(viewport.at_top());
    ///
    /// // After scrolling down
    /// viewport.scroll_down(1);
    /// assert!(!viewport.at_top());
    ///
    /// // After returning to top
    /// viewport.goto_top();
    /// assert!(viewport.at_top());
    /// ```
    ///
    /// UI integration example:
    /// ```rust
    /// use bubbletea_widgets::viewport::Model;
    ///
    /// fn render_scroll_indicator(viewport: &Model) -> String {
    ///     let up_arrow = if viewport.at_top() { " " } else { "↑" };
    ///     let down_arrow = if viewport.at_bottom() { " " } else { "↓" };
    ///     format!("{} Content {} ", up_arrow, down_arrow)
    /// }
    /// ```
    pub fn at_top(&self) -> bool {
        self.y_offset == 0
    }

    /// Returns whether the viewport is scrolled to or past the bottom of the content.
    ///
    /// This method checks if the vertical scroll position has reached the end,
    /// meaning no more content is available below the current view. Useful for
    /// determining when scroll-down operations should be disabled or when
    /// implementing infinite scroll detection.
    ///
    /// # Returns
    ///
    /// `true` if at or past the bottom, `false` if more content is available below
    ///
    /// # Examples
    ///
    /// ```rust
    /// use bubbletea_widgets::viewport::Model;
    ///
    /// let mut viewport = Model::new(40, 3); // Small viewport
    /// viewport.set_content("Line 1\nLine 2\nLine 3\nLine 4\nLine 5");
    ///
    /// // Initially not at bottom (more content below)
    /// assert!(!viewport.at_bottom());
    ///
    /// // Scroll to bottom
    /// viewport.goto_bottom();
    /// assert!(viewport.at_bottom());
    /// ```
    ///
    /// Scroll control logic:
    /// ```rust
    /// use bubbletea_widgets::viewport::Model;
    ///
    /// fn handle_scroll_down(viewport: &mut Model) -> bool {
    ///     if viewport.at_bottom() {
    ///         // Can't scroll further down
    ///         false
    ///     } else {
    ///         viewport.scroll_down(1);
    ///         true
    ///     }
    /// }
    /// ```
    ///
    /// # Difference from `past_bottom()`
    ///
    /// - `at_bottom()`: Returns `true` when at the maximum valid scroll position
    /// - `past_bottom()`: Returns `true` only when scrolled beyond valid content
    pub fn at_bottom(&self) -> bool {
        self.y_offset >= self.max_y_offset()
    }

    /// Returns whether the viewport has been scrolled beyond valid content.
    ///
    /// This method detects an invalid scroll state where the y_offset exceeds
    /// the maximum valid position. This can occur during content changes or
    /// viewport resizing. Generally indicates a need for scroll position correction.
    ///
    /// # Returns
    ///
    /// `true` if scrolled past valid content, `false` if within valid range
    ///
    /// # Examples
    ///
    /// ```rust
    /// use bubbletea_widgets::viewport::Model;
    ///
    /// let mut viewport = Model::new(40, 10);
    /// viewport.set_content("Line 1\nLine 2\nLine 3");
    ///
    /// // Normal scroll position
    /// assert!(!viewport.past_bottom());
    ///
    /// // This would typically be prevented by normal scroll methods,
    /// // but could occur during content changes
    /// ```
    ///
    /// Auto-correction usage:
    /// ```rust
    /// use bubbletea_widgets::viewport::Model;
    ///
    /// fn ensure_valid_scroll(viewport: &mut Model) {
    ///     if viewport.past_bottom() {
    ///         viewport.goto_bottom(); // Correct invalid position
    ///     }
    /// }
    /// ```
    ///
    /// # Use Cases
    ///
    /// - Detecting invalid state after content changes
    /// - Validation in custom scroll implementations
    /// - Debug assertion checks
    /// - Auto-correction logic
    pub fn past_bottom(&self) -> bool {
        self.y_offset > self.max_y_offset()
    }

    /// Returns the vertical scroll progress as a percentage from 0.0 to 1.0.
    ///
    /// This method calculates how far through the content the viewport has
    /// scrolled vertically. 0.0 indicates the top, 1.0 indicates the bottom.
    /// Useful for implementing scroll indicators, progress bars, or proportional
    /// navigation controls.
    ///
    /// # Returns
    ///
    /// A float between 0.0 and 1.0 representing scroll progress:
    /// - `0.0`: At the very top of content
    /// - `0.5`: Halfway through content
    /// - `1.0`: At or past the bottom of content
    ///
    /// # Examples
    ///
    /// ```rust
    /// use bubbletea_widgets::viewport::Model;
    ///
    /// let mut viewport = Model::new(40, 5);
    /// viewport.set_content("Line 1\nLine 2\nLine 3\nLine 4\nLine 5\nLine 6\nLine 7\nLine 8\nLine 9\nLine 10");
    ///
    /// // At top
    /// assert_eq!(viewport.scroll_percent(), 0.0);
    ///
    /// // Scroll partway down
    /// viewport.scroll_down(2);
    /// let progress = viewport.scroll_percent();
    /// assert!(progress > 0.0 && progress < 1.0);
    ///
    /// // At bottom
    /// viewport.goto_bottom();
    /// assert_eq!(viewport.scroll_percent(), 1.0);
    /// ```
    ///
    /// Progress bar implementation:
    /// ```rust
    /// use bubbletea_widgets::viewport::Model;
    ///
    /// fn render_progress_bar(viewport: &Model, width: usize) -> String {
    ///     let progress = viewport.scroll_percent();
    ///     let filled_chars = (progress * width as f64) as usize;
    ///     let empty_chars = width - filled_chars;
    ///     
    ///     format!(
    ///         "[{}{}] {:.1}%",
    ///         "█".repeat(filled_chars),
    ///         "░".repeat(empty_chars),
    ///         progress * 100.0
    ///     )
    /// }
    /// ```
    ///
    /// # Special Cases
    ///
    /// - If viewport height >= content lines, returns 1.0 (all content visible)
    /// - Result is clamped to [0.0, 1.0] range for safety
    /// - Calculation accounts for viewport height in determining valid scroll range
    pub fn scroll_percent(&self) -> f64 {
        if self.height >= self.lines.len() {
            return 1.0;
        }
        let y = self.y_offset as f64;
        let h = self.height as f64;
        let t = self.lines.len() as f64;
        let v = y / (t - h);
        v.clamp(0.0, 1.0)
    }

    /// Returns the horizontal scroll progress as a percentage from 0.0 to 1.0.
    ///
    /// This method calculates how far through the content width the viewport has
    /// scrolled horizontally. 0.0 indicates the leftmost position, 1.0 indicates
    /// the rightmost position. Useful for implementing horizontal scroll indicators
    /// or proportional navigation controls for wide content.
    ///
    /// # Returns
    ///
    /// A float between 0.0 and 1.0 representing horizontal scroll progress:
    /// - `0.0`: At the leftmost edge of content
    /// - `0.5`: Halfway through the content width
    /// - `1.0`: At or past the rightmost edge of content
    ///
    /// # Examples
    ///
    /// ```rust
    /// use bubbletea_widgets::viewport::Model;
    ///
    /// let mut viewport = Model::new(20, 5);
    /// viewport.set_content("Short line\nThis is a very long line that extends beyond viewport width\nAnother line");
    ///
    /// // At left edge
    /// assert_eq!(viewport.horizontal_scroll_percent(), 0.0);
    ///
    /// // Scroll horizontally
    /// // Scroll right 10 times
    /// for _ in 0..10 {
    ///     viewport.scroll_right();
    /// }
    /// let h_progress = viewport.horizontal_scroll_percent();
    /// assert!(h_progress > 0.0 && h_progress <= 1.0);
    ///
    /// // At right edge
    /// // Scroll far to ensure we reach the end
    /// for _ in 0..1000 {
    ///     viewport.scroll_right();
    /// }
    /// assert_eq!(viewport.horizontal_scroll_percent(), 1.0);
    /// ```
    ///
    /// Horizontal progress indicator:
    /// ```rust
    /// use bubbletea_widgets::viewport::Model;
    ///
    /// fn render_horizontal_indicator(viewport: &Model, width: usize) -> String {
    ///     let h_progress = viewport.horizontal_scroll_percent();
    ///     let position = (h_progress * width as f64) as usize;
    ///     
    ///     let mut indicator = vec!['-'; width];
    ///     if position < width {
    ///         indicator[position] = '|';
    ///     }
    ///     indicator.into_iter().collect()
    /// }
    /// ```
    ///
    /// # Special Cases
    ///
    /// - If viewport width >= longest line width, returns 1.0 (all content visible)
    /// - Result is clamped to [0.0, 1.0] range for safety
    /// - Based on the longest line in the content, not current visible lines
    pub fn horizontal_scroll_percent(&self) -> f64 {
        if self.x_offset >= self.longest_line_width.saturating_sub(self.width) {
            return 1.0;
        }
        let y = self.x_offset as f64;
        let h = self.width as f64;
        let t = self.longest_line_width as f64;
        let v = y / (t - h);
        v.clamp(0.0, 1.0)
    }

    /// Sets the viewport's text content from a multi-line string.
    ///
    /// This method processes the provided string by splitting it into individual lines
    /// and storing them internally for display. Line endings are normalized to Unix-style
    /// (`\n`), and the longest line width is calculated for horizontal scrolling support.
    ///
    /// # Arguments
    ///
    /// * `content` - The text content as a multi-line string
    ///
    /// # Examples
    ///
    /// ```rust
    /// use bubbletea_widgets::viewport::Model;
    ///
    /// let mut viewport = Model::new(40, 10);
    /// viewport.set_content("Line 1\nLine 2\nVery long line that extends beyond viewport width\nLine 4");
    ///
    /// // Content is now available for display
    /// let visible = viewport.visible_lines();
    /// assert!(!visible.is_empty());
    /// ```
    ///
    /// Loading file content:
    /// ```rust
    /// use bubbletea_widgets::viewport::Model;
    /// use std::fs;
    ///
    /// let mut viewport = Model::new(80, 24);
    ///
    /// // Load file content into viewport
    /// let file_content = fs::read_to_string("example.txt").unwrap_or_default();
    /// viewport.set_content(&file_content);
    /// ```
    ///
    /// Dynamic content updates:
    /// ```rust
    /// use bubbletea_widgets::viewport::Model;
    ///
    /// let mut viewport = Model::new(50, 15);
    ///
    /// // Initial content
    /// viewport.set_content("Initial content\nLine 2");
    ///
    /// // Update with new content
    /// let new_content = (1..=100)
    ///     .map(|i| format!("Generated line {}", i))
    ///     .collect::<Vec<_>>()
    ///     .join("\n");
    /// viewport.set_content(&new_content);
    /// ```
    ///
    /// # Behavior
    ///
    /// - **Line Ending Normalization**: Converts `\r\n` to `\n` for consistency
    /// - **Width Calculation**: Automatically computes the longest line for horizontal scrolling
    /// - **Scroll Position**: If the current scroll position becomes invalid, scrolls to bottom
    /// - **Performance**: Content processing occurs immediately; consider using `set_content_lines()` for pre-split content
    ///
    /// # Cross-Platform Compatibility
    ///
    /// Content from Windows systems with `\r\n` line endings is automatically normalized,
    /// ensuring consistent behavior across all platforms.
    pub fn set_content(&mut self, content: &str) {
        let content = content.replace("\r\n", "\n"); // normalize line endings
        self.lines = content.split('\n').map(|s| s.to_string()).collect();
        self.longest_line_width = find_longest_line_width(&self.lines);

        if self.y_offset > self.lines.len().saturating_sub(1) {
            self.goto_bottom();
        }
    }

    /// Sets the viewport content from a pre-split vector of lines.
    ///
    /// This method directly sets the viewport content from an existing vector of
    /// strings, avoiding the string splitting overhead of `set_content()`. Each
    /// string represents one line of content. This is more efficient when content
    /// is already available as individual lines.
    ///
    /// # Arguments
    ///
    /// * `lines` - Vector of strings where each string is a content line
    ///
    /// # Examples
    ///
    /// ```rust
    /// use bubbletea_widgets::viewport::Model;
    ///
    /// let mut viewport = Model::new(40, 10);
    ///
    /// let lines = vec![
    ///     "Header Line".to_string(),
    ///     "Content Line 1".to_string(),
    ///     "Content Line 2".to_string(),
    ///     "A very long line that extends beyond the viewport width".to_string(),
    /// ];
    ///
    /// viewport.set_content_lines(lines);
    ///
    /// let visible = viewport.visible_lines();
    /// assert_eq!(visible.len(), 4); // All lines fit in viewport height
    /// ```
    ///
    /// Processing structured data:
    /// ```rust
    /// use bubbletea_widgets::viewport::Model;
    ///
    /// #[derive(Debug)]
    /// struct LogEntry {
    ///     timestamp: String,
    ///     level: String,
    ///     message: String,
    /// }
    ///
    /// let mut viewport = Model::new(80, 20);
    /// let log_entries = vec![
    ///     LogEntry { timestamp: "2024-01-01T10:00:00".to_string(), level: "INFO".to_string(), message: "Application started".to_string() },
    ///     LogEntry { timestamp: "2024-01-01T10:01:00".to_string(), level: "ERROR".to_string(), message: "Connection failed".to_string() },
    /// ];
    ///
    /// let formatted_lines: Vec<String> = log_entries
    ///     .iter()
    ///     .map(|entry| format!("[{}] {}: {}", entry.timestamp, entry.level, entry.message))
    ///     .collect();
    ///
    /// viewport.set_content_lines(formatted_lines);
    /// ```
    ///
    /// Reading from various sources:
    /// ```rust
    /// use bubbletea_widgets::viewport::Model;
    ///
    /// let mut viewport = Model::new(60, 15);
    ///
    /// // Use pre-split lines for better performance
    /// let lines: Vec<String> = vec![
    ///     "Line 1".to_string(),
    ///     "Line 2".to_string(),
    ///     "Line 3".to_string(),
    /// ];
    ///
    /// viewport.set_content_lines(lines);
    /// assert_eq!(viewport.line_count(), 3);
    /// ```
    ///
    /// # Performance Benefits
    ///
    /// - **No String Processing**: Avoids the overhead of splitting a large string
    /// - **Memory Efficient**: Directly moves the vector into internal storage
    /// - **Ideal for Streaming**: Perfect for incrementally building content
    /// - **Pre-formatted Content**: Useful when lines are already processed/formatted
    ///
    /// # Behavior
    ///
    /// - **Width Calculation**: Automatically computes the longest line for horizontal scrolling
    /// - **Scroll Position**: If current scroll position becomes invalid, scrolls to bottom
    /// - **Ownership**: Takes ownership of the provided vector
    /// - **No Normalization**: Lines are used as-is without line ending processing
    pub fn set_content_lines(&mut self, lines: Vec<String>) {
        self.lines = lines;
        self.longest_line_width = find_longest_line_width(&self.lines);

        if self.y_offset > self.lines.len().saturating_sub(1) {
            self.goto_bottom();
        }
    }

    /// Returns the lines currently visible in the viewport.
    ///
    /// This method calculates which lines should be displayed based on the current
    /// scroll position, viewport dimensions, and applied styling. It handles both
    /// vertical scrolling (which lines to show) and horizontal scrolling (which
    /// portion of each line to show). The result accounts for frame sizes from
    /// lipgloss styling like borders and padding.
    ///
    /// # Returns
    ///
    /// A vector of strings representing the currently visible content lines.
    /// Each string is horizontally clipped to fit within the viewport width.
    ///
    /// # Examples
    ///
    /// ```rust
    /// use bubbletea_widgets::viewport::Model;
    ///
    /// let mut viewport = Model::new(20, 5);
    /// viewport.set_content("Line 1\nLine 2\nLine 3\nLine 4\nLine 5");
    ///
    /// // Get initial visible lines (height 5 minus 2 frame = 3 effective)
    /// let visible = viewport.visible_lines();
    /// assert_eq!(visible.len(), 3);
    /// assert_eq!(visible[0], "Line 1");
    /// assert_eq!(visible[1], "Line 2");
    /// assert_eq!(visible[2], "Line 3");
    ///
    /// // After scrolling down
    /// viewport.scroll_down(2);
    /// let visible = viewport.visible_lines();
    /// assert_eq!(visible[0], "Line 3");
    /// assert_eq!(visible[1], "Line 4");
    /// assert_eq!(visible[2], "Line 5");
    /// ```
    ///
    /// Horizontal scrolling example:
    /// ```rust
    /// use bubbletea_widgets::viewport::Model;
    ///
    /// let mut viewport = Model::new(10, 4); // Narrow viewport (4 height minus 2 frame = 2 effective)
    /// viewport.set_content("Short\nThis is a very long line that gets clipped");
    ///
    /// // Initial view shows left portion
    /// let visible = viewport.visible_lines();
    /// assert_eq!(visible[1], "This is ");
    ///
    /// // After horizontal scrolling
    /// // Scroll right 5 times
    /// for _ in 0..5 {
    ///     viewport.scroll_right();
    /// }
    /// let visible = viewport.visible_lines();
    /// assert_eq!(visible[1], "is a ver"); // Shifted right (8 chars max)
    /// ```
    ///
    /// Working with styled viewport:
    /// ```rust
    /// use bubbletea_widgets::viewport::Model;
    /// use lipgloss_extras::prelude::*;
    ///
    /// let mut viewport = Model::new(20, 5)
    ///     .with_style(
    ///         Style::new().padding(1, 2, 1, 2) // Reduces effective size
    ///     );
    ///
    /// viewport.set_content("Line 1\nLine 2\nLine 3");
    /// let visible = viewport.visible_lines();
    ///
    /// // Available content area is reduced by padding
    /// // Each visible line is also clipped to account for horizontal padding
    /// ```
    ///
    /// # Performance Considerations
    ///
    /// - **Efficient Rendering**: Only processes lines within the visible area
    /// - **Frame Calculation**: Style frame sizes are computed once per call
    /// - **Clipping**: Horizontal clipping is applied only when needed
    /// - **Memory**: Returns a new vector; consider caching for frequent calls
    ///
    /// # Integration Patterns
    ///
    /// This method is typically used in the view/render phase:
    /// ```rust
    /// use bubbletea_widgets::viewport::Model;
    /// use lipgloss_extras::prelude::*;
    ///
    /// fn render_viewport_content(viewport: &Model) -> String {
    ///     let visible_lines = viewport.visible_lines();
    ///     
    ///     if visible_lines.is_empty() {
    ///         return "No content to display".to_string();
    ///     }
    ///     
    ///     visible_lines.join("\n")
    /// }
    /// ```
    pub fn visible_lines(&self) -> Vec<String> {
        let frame_height = self.style.get_vertical_frame_size();
        let frame_width = self.style.get_horizontal_frame_size();
        let h = self.height.saturating_sub(frame_height as usize);
        let w = self.width.saturating_sub(frame_width as usize);

        let mut lines = Vec::new();
        if !self.lines.is_empty() {
            let top = self.y_offset;
            let bottom = (self.y_offset + h).min(self.lines.len());
            lines = self.lines[top..bottom].to_vec();
        }

        // Handle horizontal scrolling
        if self.x_offset == 0 && self.longest_line_width <= w || w == 0 {
            return lines;
        }

        let mut cut_lines = Vec::new();
        for line in lines {
            let cut_line = cut_string(&line, self.x_offset, self.x_offset + w);
            cut_lines.push(cut_line);
        }
        cut_lines
    }

    /// Sets the vertical scroll position to a specific line offset.
    ///
    /// This method directly positions the viewport at the specified line offset
    /// from the beginning of the content. The offset is automatically clamped
    /// to ensure it remains within valid bounds (0 to maximum valid offset).
    ///
    /// # Arguments
    ///
    /// * `n` - The line number to scroll to (0-based indexing)
    ///
    /// # Examples
    ///
    /// ```rust
    /// use bubbletea_widgets::viewport::Model;
    ///
    /// let mut viewport = Model::new(40, 5);
    /// viewport.set_content(&(1..=20).map(|i| format!("Line {}", i)).collect::<Vec<_>>().join("\n"));
    ///
    /// // Jump to line 10 (0-based, so content shows "Line 11")
    /// viewport.set_y_offset(10);
    /// let visible = viewport.visible_lines();
    /// assert_eq!(visible[0], "Line 11");
    ///
    /// // Attempt to scroll beyond content (gets clamped)
    /// viewport.set_y_offset(1000);
    /// assert!(viewport.at_bottom());
    /// ```
    ///
    /// Direct positioning for navigation:
    /// ```rust
    /// use bubbletea_widgets::viewport::Model;
    ///
    /// let mut viewport = Model::new(80, 20);
    /// viewport.set_content("Line content...");
    ///
    /// // Jump to 25% through the content
    /// let total_lines = 100; // Assume we know content size
    /// let quarter_position = total_lines / 4;
    /// viewport.set_y_offset(quarter_position);
    /// ```
    ///
    /// # Clamping Behavior
    ///
    /// - Values less than 0: Set to 0 (top of content)
    /// - Values greater than maximum valid offset: Set to maximum (bottom view)
    /// - Maximum offset ensures at least one line is visible when possible
    ///
    /// # Use Cases
    ///
    /// - **Direct Navigation**: Jump to specific locations
    /// - **Proportional Scrolling**: Navigate based on percentages
    /// - **Search Results**: Position at specific line numbers
    /// - **Bookmarks**: Return to saved positions
    pub fn set_y_offset(&mut self, n: usize) {
        self.y_offset = n.min(self.max_y_offset());
    }

    /// Scrolls down by one full page (viewport height).
    ///
    /// This method moves the viewport down by exactly the viewport height,
    /// effectively showing the next "page" of content. This is the standard
    /// page-down operation found in most text viewers and editors.
    ///
    /// # Returns
    ///
    /// A vector of strings representing the newly visible lines that scrolled into view.
    /// Returns an empty vector if already at the bottom or no scrolling occurred.
    ///
    /// # Examples
    ///
    /// ```rust
    /// use bubbletea_widgets::viewport::Model;
    ///
    /// let mut viewport = Model::new(40, 5);
    /// viewport.set_content(&(1..=20).map(|i| format!("Line {}", i)).collect::<Vec<_>>().join("\n"));
    ///
    /// // Initially shows lines 1-5
    /// let visible = viewport.visible_lines();
    /// assert_eq!(visible[0], "Line 1");
    ///
    /// // Page down shows lines 6-10
    /// let new_lines = viewport.page_down();
    /// let visible = viewport.visible_lines();
    /// assert_eq!(visible[0], "Line 6");
    /// assert!(!new_lines.is_empty());
    /// ```
    ///
    /// Handling bottom boundary:
    /// ```rust
    /// use bubbletea_widgets::viewport::Model;
    ///
    /// let mut viewport = Model::new(40, 5);
    /// viewport.set_content("Line 1\nLine 2\nLine 3"); // Only 3 lines
    ///
    /// // At bottom already, page_down returns empty
    /// viewport.goto_bottom();
    /// let result = viewport.page_down();
    /// assert!(result.is_empty());
    /// ```
    ///
    /// # Performance Optimization
    ///
    /// The returned vector contains only the newly visible lines for efficient
    /// rendering updates. Applications can use this for incremental display updates.
    pub fn page_down(&mut self) -> Vec<String> {
        if self.at_bottom() {
            return Vec::new();
        }
        self.scroll_down(self.height)
    }

    /// Scrolls up by one full page (viewport height).
    ///
    /// This method moves the viewport up by exactly the viewport height,
    /// effectively showing the previous "page" of content. This is the standard
    /// page-up operation found in most text viewers and editors.
    ///
    /// # Returns
    ///
    /// A vector of strings representing the newly visible lines that scrolled into view.
    /// Returns an empty vector if already at the top or no scrolling occurred.
    ///
    /// # Examples
    ///
    /// ```rust
    /// use bubbletea_widgets::viewport::Model;
    ///
    /// let mut viewport = Model::new(40, 5);
    /// viewport.set_content(&(1..=20).map(|i| format!("Line {}", i)).collect::<Vec<_>>().join("\n"));
    ///
    /// // Scroll to middle, then page up
    /// viewport.set_y_offset(10);
    /// let new_lines = viewport.page_up();
    /// let visible = viewport.visible_lines();
    /// assert_eq!(visible[0], "Line 6"); // Moved up by 5 lines
    /// ```
    ///
    /// Handling top boundary:
    /// ```rust
    /// use bubbletea_widgets::viewport::Model;
    ///
    /// let mut viewport = Model::new(40, 5);
    /// viewport.set_content("Line 1\nLine 2\nLine 3");
    ///
    /// // Already at top, page_up returns empty
    /// let result = viewport.page_up();
    /// assert!(result.is_empty());
    /// assert!(viewport.at_top());
    /// ```
    pub fn page_up(&mut self) -> Vec<String> {
        if self.at_top() {
            return Vec::new();
        }
        self.scroll_up(self.height)
    }

    /// Scrolls down by half the viewport height.
    ///
    /// This method provides a more granular scrolling option than full page scrolling,
    /// moving the viewport down by half its height. This is commonly mapped to
    /// Ctrl+D in Vim-style navigation.
    ///
    /// # Returns
    ///
    /// A vector of strings representing the newly visible lines that scrolled into view.
    /// Returns an empty vector if already at the bottom or no scrolling occurred.
    ///
    /// # Examples
    ///
    /// ```rust
    /// use bubbletea_widgets::viewport::Model;
    ///
    /// let mut viewport = Model::new(40, 10); // Height of 10
    /// viewport.set_content(&(1..=30).map(|i| format!("Line {}", i)).collect::<Vec<_>>().join("\n"));
    ///
    /// // Half page down moves by 5 lines (10/2)
    /// viewport.half_page_down();
    /// let visible = viewport.visible_lines();
    /// assert_eq!(visible[0], "Line 6"); // Moved down 5 lines
    /// ```
    ///
    /// # Use Cases
    ///
    /// - **Gradual Navigation**: More controlled scrolling than full pages
    /// - **Vim Compatibility**: Matches Ctrl+D behavior
    /// - **Reading Flow**: Maintains better context when scrolling through text
    pub fn half_page_down(&mut self) -> Vec<String> {
        if self.at_bottom() {
            return Vec::new();
        }
        self.scroll_down(self.height / 2)
    }

    /// Scrolls up by half the viewport height.
    ///
    /// This method provides a more granular scrolling option than full page scrolling,
    /// moving the viewport up by half its height. This is commonly mapped to
    /// Ctrl+U in Vim-style navigation.
    ///
    /// # Returns
    ///
    /// A vector of strings representing the newly visible lines that scrolled into view.
    /// Returns an empty vector if already at the top or no scrolling occurred.
    ///
    /// # Examples
    ///
    /// ```rust
    /// use bubbletea_widgets::viewport::Model;
    ///
    /// let mut viewport = Model::new(40, 10); // Height of 10
    /// viewport.set_content(&(1..=30).map(|i| format!("Line {}", i)).collect::<Vec<_>>().join("\n"));
    ///
    /// // Move to middle, then half page up
    /// viewport.set_y_offset(15);
    /// viewport.half_page_up();
    /// let visible = viewport.visible_lines();
    /// assert_eq!(visible[0], "Line 11"); // Moved up 5 lines (15-5+1)
    /// ```
    ///
    /// # Use Cases
    ///
    /// - **Gradual Navigation**: More controlled scrolling than full pages
    /// - **Vim Compatibility**: Matches Ctrl+U behavior  
    /// - **Reading Flow**: Maintains better context when scrolling through text
    pub fn half_page_up(&mut self) -> Vec<String> {
        if self.at_top() {
            return Vec::new();
        }
        self.scroll_up(self.height / 2)
    }

    /// Scrolls down by the specified number of lines.
    ///
    /// This is the fundamental vertical scrolling method that moves the viewport
    /// down by the specified number of lines. All other downward scrolling methods
    /// (page_down, half_page_down) ultimately delegate to this method.
    ///
    /// # Arguments
    ///
    /// * `n` - Number of lines to scroll down
    ///
    /// # Returns
    ///
    /// A vector containing the newly visible lines for performance rendering.
    /// Returns empty vector if no scrolling occurred.
    ///
    /// # Examples
    ///
    /// ```rust
    /// use bubbletea_widgets::viewport::Model;
    ///
    /// let mut viewport = Model::new(40, 5);
    /// viewport.set_content(&(1..=20).map(|i| format!("Line {}", i)).collect::<Vec<_>>().join("\n"));
    ///
    /// // Scroll down 3 lines
    /// let new_lines = viewport.scroll_down(3);
    /// let visible = viewport.visible_lines();
    /// assert_eq!(visible[0], "Line 4"); // Now starting from line 4
    /// assert_eq!(new_lines.len(), 3); // 3 new lines scrolled in
    /// ```
    ///
    /// Edge case handling:
    /// ```rust
    /// use bubbletea_widgets::viewport::Model;
    ///
    /// let mut viewport = Model::new(40, 5);
    /// viewport.set_content("Line 1\nLine 2");
    ///
    /// // No scrolling at bottom
    /// viewport.goto_bottom();
    /// let result = viewport.scroll_down(5);
    /// assert!(result.is_empty());
    ///
    /// // No scrolling with n=0
    /// viewport.goto_top();
    /// let result = viewport.scroll_down(0);
    /// assert!(result.is_empty());
    /// ```
    ///
    /// # Performance Optimization
    ///
    /// The returned vector contains only the lines that scrolled into view,
    /// enabling efficient incremental rendering in terminal applications.
    /// This avoids re-rendering the entire viewport when only a few lines changed.
    ///
    /// # Boundary Behavior
    ///
    /// - Automatically stops at the bottom of content
    /// - Returns empty vector if already at bottom
    /// - Handles viewport larger than content gracefully
    pub fn scroll_down(&mut self, n: usize) -> Vec<String> {
        if self.at_bottom() || n == 0 || self.lines.is_empty() {
            return Vec::new();
        }

        self.set_y_offset(self.y_offset + n);

        // Gather lines for performance scrolling
        let bottom = (self.y_offset + self.height).min(self.lines.len());
        let top = (self.y_offset + self.height).saturating_sub(n).min(bottom);
        self.lines[top..bottom].to_vec()
    }

    /// Scrolls up by the specified number of lines.
    ///
    /// This is the fundamental vertical scrolling method that moves the viewport
    /// up by the specified number of lines. All other upward scrolling methods
    /// (page_up, half_page_up) ultimately delegate to this method.
    ///
    /// # Arguments
    ///
    /// * `n` - Number of lines to scroll up
    ///
    /// # Returns
    ///
    /// A vector containing the newly visible lines for performance rendering.
    /// Returns empty vector if no scrolling occurred.
    ///
    /// # Examples
    ///
    /// ```rust
    /// use bubbletea_widgets::viewport::Model;
    ///
    /// let mut viewport = Model::new(40, 5);
    /// viewport.set_content(&(1..=20).map(|i| format!("Line {}", i)).collect::<Vec<_>>().join("\n"));
    ///
    /// // Start from middle
    /// viewport.set_y_offset(10);
    ///
    /// // Scroll up 3 lines
    /// let new_lines = viewport.scroll_up(3);
    /// let visible = viewport.visible_lines();
    /// assert_eq!(visible[0], "Line 8"); // Now starting from line 8 (10-3+1)
    /// assert_eq!(new_lines.len(), 3); // 3 new lines scrolled in
    /// ```
    ///
    /// Edge case handling:
    /// ```rust
    /// use bubbletea_widgets::viewport::Model;
    ///
    /// let mut viewport = Model::new(40, 5);
    /// viewport.set_content("Line 1\nLine 2");
    ///
    /// // No scrolling at top
    /// let result = viewport.scroll_up(5);
    /// assert!(result.is_empty());
    /// assert!(viewport.at_top());
    ///
    /// // No scrolling with n=0
    /// let result = viewport.scroll_up(0);
    /// assert!(result.is_empty());
    /// ```
    ///
    /// # Performance Optimization
    ///
    /// The returned vector contains only the lines that scrolled into view,
    /// enabling efficient incremental rendering. Applications can update only
    /// the changed portions of the display.
    ///
    /// # Boundary Behavior
    ///
    /// - Automatically stops at the top of content
    /// - Returns empty vector if already at top
    /// - Uses saturating subtraction to prevent underflow
    pub fn scroll_up(&mut self, n: usize) -> Vec<String> {
        if self.at_top() || n == 0 || self.lines.is_empty() {
            return Vec::new();
        }

        self.set_y_offset(self.y_offset.saturating_sub(n));

        // Gather lines for performance scrolling
        let top = self.y_offset;
        let bottom = (self.y_offset + n).min(self.max_y_offset());
        self.lines[top..bottom].to_vec()
    }

    /// Jumps directly to the beginning of the content.
    ///
    /// This method immediately positions the viewport at the very top of the
    /// content, setting the vertical offset to 0. This is equivalent to pressing
    /// the "Home" key in most text viewers.
    ///
    /// # Examples
    ///
    /// ```rust
    /// use bubbletea_widgets::viewport::Model;
    ///
    /// let mut viewport = Model::new(40, 5);
    /// viewport.set_content(&(1..=20).map(|i| format!("Line {}", i)).collect::<Vec<_>>().join("\n"));
    ///
    /// // Scroll to middle first
    /// viewport.set_y_offset(10);
    /// assert!(!viewport.at_top());
    ///
    /// // Jump to top
    /// viewport.goto_top();
    /// assert!(viewport.at_top());
    ///
    /// let visible = viewport.visible_lines();
    /// assert_eq!(visible[0], "Line 1");
    /// ```
    ///
    /// # Use Cases
    ///
    /// - **Navigation shortcuts**: Quick return to document start
    /// - **Reset position**: Return to initial state after scrolling
    /// - **Search results**: Jump to first occurrence
    /// - **Content refresh**: Start from beginning after content changes
    pub fn goto_top(&mut self) {
        self.y_offset = 0;
    }

    /// Jumps directly to the end of the content.
    ///
    /// This method immediately positions the viewport at the bottom of the
    /// content, showing the last possible page. This is equivalent to pressing
    /// the "End" key in most text viewers.
    ///
    /// # Examples
    ///
    /// ```rust
    /// use bubbletea_widgets::viewport::Model;
    ///
    /// let mut viewport = Model::new(40, 5);
    /// viewport.set_content(&(1..=20).map(|i| format!("Line {}", i)).collect::<Vec<_>>().join("\n"));
    ///
    /// // Jump to bottom
    /// viewport.goto_bottom();
    /// assert!(viewport.at_bottom());
    ///
    /// let visible = viewport.visible_lines();
    /// // With 20 lines total and height 5 (minus 2 for frame), bottom shows last 3 lines
    /// assert_eq!(visible[0], "Line 18");
    /// assert_eq!(visible[2], "Line 20");
    /// ```
    ///
    /// Auto-correction after content changes:
    /// ```rust
    /// use bubbletea_widgets::viewport::Model;
    ///
    /// let mut viewport = Model::new(40, 10);
    ///
    /// // Set initial content and scroll down
    /// viewport.set_content(&(1..=50).map(|i| format!("Line {}", i)).collect::<Vec<_>>().join("\n"));
    /// viewport.set_y_offset(30);
    ///
    /// // Replace with shorter content
    /// viewport.set_content("Line 1\nLine 2\nLine 3");
    /// // goto_bottom() is called automatically to fix invalid offset
    /// assert!(viewport.at_bottom());
    /// ```
    ///
    /// # Use Cases
    ///
    /// - **Navigation shortcuts**: Quick jump to document end
    /// - **Log viewing**: Jump to latest entries
    /// - **Content appending**: Position for new content
    /// - **Auto-correction**: Fix invalid positions after content changes
    pub fn goto_bottom(&mut self) {
        self.y_offset = self.max_y_offset();
    }

    /// Sets the horizontal scrolling step size in characters.
    ///
    /// This method configures how many characters the viewport scrolls
    /// horizontally with each left/right scroll operation. The step size
    /// affects both `scroll_left()` and `scroll_right()` methods.
    ///
    /// # Arguments
    ///
    /// * `step` - Number of characters to scroll per horizontal step
    ///
    /// # Examples
    ///
    /// ```rust
    /// use bubbletea_widgets::viewport::Model;
    ///
    /// let mut viewport = Model::new(20, 5);
    /// viewport.set_content("This is a very long line that extends far beyond the viewport width");
    ///
    /// // Default step is 1 character
    /// viewport.scroll_right();
    /// assert_eq!(viewport.x_offset, 1);
    ///
    /// // Set larger step for faster scrolling
    /// viewport.set_horizontal_step(5);
    /// viewport.scroll_right();
    /// assert_eq!(viewport.x_offset, 6); // 1 + 5
    /// ```
    ///
    /// Different step sizes for different content types:
    /// ```rust
    /// use bubbletea_widgets::viewport::Model;
    ///
    /// let mut viewport = Model::new(40, 10);
    ///
    /// // Fine scrolling for precise text viewing
    /// viewport.set_horizontal_step(1);
    ///
    /// // Coarse scrolling for wide data tables
    /// viewport.set_horizontal_step(8); // Tab-like steps
    ///
    /// // Word-based scrolling
    /// viewport.set_horizontal_step(4); // Average word length
    /// ```
    ///
    /// # Use Cases
    ///
    /// - **Fine Control**: Single-character precision (step=1)
    /// - **Tab Columns**: Align with tab stops (step=4 or 8)
    /// - **Word Navigation**: Approximate word-based scrolling
    /// - **Performance**: Larger steps for faster navigation of wide content
    pub fn set_horizontal_step(&mut self, step: usize) {
        self.horizontal_step = step;
    }

    /// Scrolls the viewport left by the configured horizontal step.
    ///
    /// This method moves the horizontal view to the left, revealing content
    /// that was previously hidden on the left side. The scroll amount is
    /// determined by the `horizontal_step` setting.
    ///
    /// # Examples
    ///
    /// ```rust
    /// use bubbletea_widgets::viewport::Model;
    ///
    /// let mut viewport = Model::new(10, 3);
    /// viewport.set_content("This is a very long line that needs horizontal scrolling");
    ///
    /// // Scroll right first to see the effect of scrolling left
    /// viewport.scroll_right();
    /// viewport.scroll_right();
    /// assert_eq!(viewport.x_offset, 2);
    ///
    /// // Scroll left
    /// viewport.scroll_left();
    /// assert_eq!(viewport.x_offset, 1);
    /// ```
    ///
    /// Boundary handling:
    /// ```rust
    /// use bubbletea_widgets::viewport::Model;
    ///
    /// let mut viewport = Model::new(20, 5);
    /// viewport.set_content("Short content");
    ///
    /// // Already at leftmost position, scroll_left has no effect
    /// assert_eq!(viewport.x_offset, 0);
    /// viewport.scroll_left();
    /// assert_eq!(viewport.x_offset, 0); // Still 0, can't scroll further left
    /// ```
    ///
    /// # Behavior
    ///
    /// - **Boundary Safe**: Uses saturating subtraction to prevent underflow
    /// - **Step-based**: Scrolls by `horizontal_step` amount
    /// - **Immediate**: Takes effect immediately, no animation
    /// - **Absolute Minimum**: Cannot scroll past offset 0 (leftmost position)
    pub fn scroll_left(&mut self) {
        self.x_offset = self.x_offset.saturating_sub(self.horizontal_step);
    }

    /// Scrolls the viewport right by the configured horizontal step.
    ///
    /// This method moves the horizontal view to the right, revealing content
    /// that was previously hidden on the right side. The scroll amount is
    /// determined by the `horizontal_step` setting, and scrolling is limited
    /// by the longest line in the content.
    ///
    /// # Examples
    ///
    /// ```rust
    /// use bubbletea_widgets::viewport::Model;
    ///
    /// let mut viewport = Model::new(10, 3);
    /// viewport.set_content("This is a very long line that needs horizontal scrolling");
    ///
    /// // Initial view shows "This is " (width 10 minus 2 for frame = 8 chars)
    /// let visible = viewport.visible_lines();
    /// assert_eq!(visible[0].len(), 8);
    ///
    /// // Scroll right to see more
    /// viewport.scroll_right();
    /// let visible = viewport.visible_lines();
    /// // Now shows "his is a v" (shifted 1 character right)
    /// ```
    ///
    /// Boundary handling:
    /// ```rust
    /// use bubbletea_widgets::viewport::Model;
    ///
    /// let mut viewport = Model::new(20, 5);
    /// viewport.set_content("Short"); // Line shorter than viewport
    ///
    /// // Cannot scroll right when content fits in viewport
    /// viewport.scroll_right();
    /// assert_eq!(viewport.x_offset, 0); // No change
    /// ```
    ///
    /// Multiple step sizes:
    /// ```rust
    /// use bubbletea_widgets::viewport::Model;
    ///
    /// let mut viewport = Model::new(10, 3);
    /// viewport.set_content("Very long line for testing horizontal scrolling behavior");
    ///
    /// // Default single-character scrolling
    /// viewport.scroll_right();
    /// assert_eq!(viewport.x_offset, 1);
    ///
    /// // Change to larger steps
    /// viewport.set_horizontal_step(5);
    /// viewport.scroll_right();
    /// assert_eq!(viewport.x_offset, 6); // 1 + 5
    /// ```
    ///
    /// # Behavior
    ///
    /// - **Content-aware**: Maximum scroll is based on longest line width
    /// - **Viewport-relative**: Considers viewport width in maximum calculation
    /// - **Step-based**: Scrolls by `horizontal_step` amount
    /// - **Clamped**: Cannot scroll past the rightmost useful position
    pub fn scroll_right(&mut self) {
        let max_offset = self.longest_line_width.saturating_sub(self.width);
        self.x_offset = (self.x_offset + self.horizontal_step).min(max_offset);
    }

    /// Get the maximum Y offset
    fn max_y_offset(&self) -> usize {
        let frame_size = self.style.get_vertical_frame_size();
        self.lines
            .len()
            .saturating_sub(self.height.saturating_sub(frame_size as usize))
    }

    /// Returns a reference to the internal content lines.
    ///
    /// This method provides read-only access to all content lines stored in the viewport.
    /// Useful for inspection, searching, or analysis of content without copying.
    ///
    /// # Returns
    ///
    /// A slice containing all content lines as strings
    ///
    /// # Examples
    ///
    /// ```rust
    /// use bubbletea_widgets::viewport::Model;
    ///
    /// let mut viewport = Model::new(40, 10);
    /// viewport.set_content("Line 1\nLine 2\nLine 3");
    ///
    /// let lines = viewport.lines();
    /// assert_eq!(lines.len(), 3);
    /// assert_eq!(lines[0], "Line 1");
    /// assert_eq!(lines[2], "Line 3");
    /// ```
    ///
    /// Content inspection and search:
    /// ```rust
    /// use bubbletea_widgets::viewport::Model;
    ///
    /// let mut viewport = Model::new(40, 10);
    /// viewport.set_content("Line 1\nImportant line\nLine 3");
    ///
    /// // Search for specific content
    /// let lines = viewport.lines();
    /// let important_line = lines.iter().find(|line| line.contains("Important"));
    /// assert!(important_line.is_some());
    /// ```
    pub fn lines(&self) -> &[String] {
        &self.lines
    }

    /// Returns the total number of content lines.
    ///
    /// This method returns the count of all content lines, regardless of viewport
    /// dimensions or scroll position. Useful for determining content size,
    /// calculating scroll percentages, or implementing navigation features.
    ///
    /// # Returns
    ///
    /// The total number of lines in the content
    ///
    /// # Examples
    ///
    /// ```rust
    /// use bubbletea_widgets::viewport::Model;
    ///
    /// let mut viewport = Model::new(40, 10);
    /// viewport.set_content("Line 1\nLine 2\nLine 3\nLine 4\nLine 5");
    ///
    /// assert_eq!(viewport.line_count(), 5);
    ///
    /// // Empty content
    /// viewport.set_content("");
    /// assert_eq!(viewport.line_count(), 1); // Empty string creates one empty line
    /// ```
    ///
    /// Navigation calculations:
    /// ```rust
    /// use bubbletea_widgets::viewport::Model;
    ///
    /// let mut viewport = Model::new(40, 10);
    /// viewport.set_content(&(1..=100).map(|i| format!("Line {}", i)).collect::<Vec<_>>().join("\n"));
    ///
    /// let total_lines = viewport.line_count();
    /// let viewport_height = viewport.height;
    ///
    /// // Calculate if scrolling is needed
    /// let needs_scrolling = total_lines > viewport_height;
    /// assert!(needs_scrolling);
    ///
    /// // Calculate maximum number of pages
    /// let max_pages = (total_lines + viewport_height - 1) / viewport_height;
    /// assert_eq!(max_pages, 10); // 100 lines / 10 height = 10 pages
    /// ```
    pub fn line_count(&self) -> usize {
        self.lines.len()
    }
}

impl Default for Model {
    /// Creates a default viewport with standard terminal dimensions.
    ///
    /// The default viewport is sized for typical terminal windows (80x24) and
    /// includes all default configuration options. This is equivalent to calling
    /// `Model::new(80, 24)`.
    ///
    /// # Examples
    ///
    /// ```rust
    /// use bubbletea_widgets::viewport::Model;
    ///
    /// let viewport = Model::default();
    /// assert_eq!(viewport.width, 80);
    /// assert_eq!(viewport.height, 24);
    /// assert!(viewport.mouse_wheel_enabled);
    /// ```
    ///
    /// # Default Configuration
    ///
    /// - **Dimensions**: 80 characters × 24 lines (standard terminal size)
    /// - **Mouse wheel**: Enabled with 3-line scroll delta
    /// - **Scroll position**: At top-left (0, 0)
    /// - **Horizontal step**: 1 character per scroll
    /// - **Style**: No styling applied
    /// - **Key bindings**: Vim-style with arrow key alternatives
    fn default() -> Self {
        Self::new(80, 24)
    }
}

impl BubbleTeaModel for Model {
    /// Initializes a new viewport instance for Bubble Tea applications.
    ///
    /// Creates a default viewport with standard terminal dimensions and no initial commands.
    /// This follows the Bubble Tea initialization pattern where components return their
    /// initial state and any startup commands.
    ///
    /// # Returns
    ///
    /// A tuple containing:
    /// - A default viewport instance (80x24)
    /// - `None` (no initialization commands needed)
    ///
    /// # Examples
    ///
    /// ```rust
    /// use bubbletea_widgets::viewport::Model;
    /// use bubbletea_rs::Model as BubbleTeaModel;
    ///
    /// let (viewport, cmd) = Model::init();
    /// assert_eq!(viewport.width, 80);
    /// assert_eq!(viewport.height, 24);
    /// assert!(cmd.is_none());
    /// ```
    fn init() -> (Self, Option<Cmd>) {
        (Self::default(), None)
    }

    /// Processes messages and updates viewport state.
    ///
    /// This method handles keyboard input for viewport navigation, implementing
    /// the standard Bubble Tea update pattern. It processes key messages against
    /// the configured key bindings and updates the viewport scroll position accordingly.
    ///
    /// # Arguments
    ///
    /// * `msg` - The message to process (typically keyboard input)
    ///
    /// # Returns
    ///
    /// Always returns `None` as viewport operations don't generate commands
    ///
    /// # Supported Key Bindings
    ///
    /// The default key bindings include:
    /// - **Page navigation**: `f`/`PgDn`/`Space` (page down), `b`/`PgUp` (page up)
    /// - **Half-page navigation**: `d` (half page down), `u` (half page up)
    /// - **Line navigation**: `j`/`↓` (line down), `k`/`↑` (line up)
    /// - **Horizontal navigation**: `l`/`→` (scroll right), `h`/`←` (scroll left)
    ///
    /// # Examples
    ///
    /// ```rust
    /// use bubbletea_widgets::viewport::Model;
    /// use bubbletea_rs::{Model as BubbleTeaModel, KeyMsg};
    /// use crossterm::event::{KeyCode, KeyModifiers};
    ///
    /// let mut viewport = Model::default();
    /// viewport.set_content("Line 1\nLine 2\nLine 3\nLine 4\nLine 5");
    ///
    /// // Simulate pressing 'j' to scroll down
    /// let key_msg = KeyMsg {
    ///     key: KeyCode::Char('j'),
    ///     modifiers: KeyModifiers::NONE,
    /// };
    ///
    /// let cmd = viewport.update(Box::new(key_msg));
    /// assert!(cmd.is_none());
    /// ```
    ///
    /// # Integration with Bubble Tea
    ///
    /// This method integrates seamlessly with Bubble Tea's message-driven architecture:
    /// ```rust
    /// use bubbletea_widgets::viewport::Model;
    /// use bubbletea_rs::{Model as BubbleTeaModel, Msg};
    ///
    /// struct App {
    ///     viewport: Model,
    /// }
    ///
    /// impl BubbleTeaModel for App {
    /// #   fn init() -> (Self, Option<bubbletea_rs::Cmd>) {
    /// #       (Self { viewport: Model::new(80, 24) }, None)
    /// #   }
    ///     
    ///     fn update(&mut self, msg: Msg) -> Option<bubbletea_rs::Cmd> {
    ///         // Forward messages to viewport
    ///         self.viewport.update(msg);
    ///         None
    ///     }
    /// #
    /// #   fn view(&self) -> String { self.viewport.view() }
    /// }
    /// ```
    fn update(&mut self, msg: Msg) -> Option<Cmd> {
        if let Some(key_msg) = msg.downcast_ref::<KeyMsg>() {
            if self.keymap.page_down.matches(key_msg) {
                self.page_down();
            } else if self.keymap.page_up.matches(key_msg) {
                self.page_up();
            } else if self.keymap.half_page_down.matches(key_msg) {
                self.half_page_down();
            } else if self.keymap.half_page_up.matches(key_msg) {
                self.half_page_up();
            } else if self.keymap.down.matches(key_msg) {
                self.scroll_down(1);
            } else if self.keymap.up.matches(key_msg) {
                self.scroll_up(1);
            } else if self.keymap.left.matches(key_msg) {
                self.scroll_left();
            } else if self.keymap.right.matches(key_msg) {
                self.scroll_right();
            }
        }
        // Mouse wheel basic support if MouseMsg is available in bubbletea-rs
        // Note: bubbletea-rs MouseMsg does not currently expose wheel events in this crate version.
        None
    }

    /// Renders the viewport content as a styled string.
    ///
    /// This method generates the visual representation of the viewport by retrieving
    /// the currently visible lines and applying any configured lipgloss styling.
    /// The output is ready for display in a terminal interface.
    ///
    /// # Returns
    ///
    /// A styled string containing the visible content, ready for terminal output
    ///
    /// # Examples
    ///
    /// ```rust
    /// use bubbletea_widgets::viewport::Model;
    /// use bubbletea_rs::Model as BubbleTeaModel;
    ///
    /// let mut viewport = Model::new(20, 5);
    /// viewport.set_content("Line 1\nLine 2\nLine 3\nLine 4");
    ///
    /// let output = viewport.view();
    /// assert!(output.contains("Line 1"));
    /// assert!(output.contains("Line 2"));
    /// assert!(output.contains("Line 3"));
    /// assert!(!output.contains("Line 4")); // Not visible in 5-line viewport (3 effective)
    /// ```
    ///
    /// With styling applied:
    /// ```rust
    /// use bubbletea_widgets::viewport::Model;
    /// use bubbletea_rs::Model as BubbleTeaModel;
    /// use lipgloss_extras::prelude::*;
    ///
    /// let mut viewport = Model::new(20, 3)
    ///     .with_style(
    ///         Style::new()
    ///             .foreground(Color::from("#FF0000"))
    ///             .background(Color::from("#000000"))
    ///     );
    ///
    /// viewport.set_content("Styled content");
    /// let styled_output = viewport.view();
    /// // Output includes ANSI escape codes for styling
    /// ```
    ///
    /// # Rendering Behavior
    ///
    /// - **Visible Lines Only**: Only renders content within the current viewport
    /// - **Horizontal Clipping**: Content wider than viewport is clipped appropriately  
    /// - **Style Application**: Applied lipgloss styles are rendered into the output
    /// - **Line Joining**: Multiple lines are joined with newline characters
    /// - **Frame Accounting**: Styling frame sizes are automatically considered
    fn view(&self) -> String {
        let visible = self.visible_lines();
        let mut output = String::new();

        for (i, line) in visible.iter().enumerate() {
            if i > 0 {
                output.push('\n');
            }
            output.push_str(line);
        }

        // Apply style if set
        self.style.render(&output)
    }
}

/// Calculates the display width of the longest line in a collection.
///
/// This internal helper function determines the maximum display width among all
/// provided lines, using proper Unicode width calculation via the `lg_width` function.
/// This is essential for horizontal scrolling calculations and determining the
/// maximum horizontal scroll offset.
///
/// # Arguments
///
/// * `lines` - A slice of strings to measure
///
/// # Returns
///
/// The width in characters of the widest line, or 0 if no lines provided
///
/// # Implementation Notes
///
/// - Uses `lg_width()` for proper Unicode width calculation
/// - Handles empty collections gracefully
/// - Accounts for wide characters (CJK, emojis, etc.)
fn find_longest_line_width(lines: &[String]) -> usize {
    lines.iter().map(|line| lg_width(line)).max().unwrap_or(0)
}

/// Extracts a substring based on display width positions for horizontal scrolling.
///
/// This internal helper function cuts a string to show only the portion between
/// specified display width positions. It properly handles Unicode characters with
/// varying display widths, making it essential for horizontal scrolling in the viewport.
///
/// # Arguments
///
/// * `s` - The source string to cut
/// * `start` - The starting display width position (inclusive)
/// * `end` - The ending display width position (exclusive)
///
/// # Returns
///
/// A string containing only the characters within the specified width range
///
/// # Implementation Details
///
/// - **Unicode-aware**: Properly handles wide characters (CJK, emojis)
/// - **Width-based**: Uses display width, not character count
/// - **Boundary safe**: Returns empty string if start is beyond string width
/// - **Performance optimized**: Single pass through characters when possible
///
/// # Examples (Internal Use)
///
/// ```ignore
/// // Wide characters take 2 display columns
/// let result = cut_string("Hello 世界 World", 3, 8);
/// // Shows characters from display column 3 to 7
/// ```
fn cut_string(s: &str, start: usize, end: usize) -> String {
    if start >= lg_width(s) {
        return String::new();
    }

    let chars: Vec<char> = s.chars().collect();
    let mut current_width = 0;
    let mut start_idx = 0;
    let mut end_idx = chars.len();

    // Find start index
    for (i, &ch) in chars.iter().enumerate() {
        if current_width >= start {
            start_idx = i;
            break;
        }
        current_width += ch.width().unwrap_or(0);
    }

    // Find end index
    current_width = 0;
    for (i, &ch) in chars.iter().enumerate() {
        if current_width >= end {
            end_idx = i;
            break;
        }
        current_width += ch.width().unwrap_or(0);
    }

    chars[start_idx..end_idx].iter().collect()
}

/// Creates a new viewport with the specified dimensions.
///
/// This is a convenience function that creates a new viewport instance.
/// It's equivalent to calling `Model::new(width, height)` directly, but
/// provides a more functional style API that some users may prefer.
///
/// # Arguments
///
/// * `width` - Display width in characters
/// * `height` - Display height in lines
///
/// # Returns
///
/// A new viewport `Model` configured with the specified dimensions
///
/// # Examples
///
/// ```rust
/// use bubbletea_widgets::viewport;
///
/// // Functional style
/// let viewport = viewport::new(80, 24);
///
/// // Equivalent to:
/// let viewport = viewport::Model::new(80, 24);
/// ```
///
/// # Use Cases
///
/// - **Functional Style**: When preferring function calls over constructors
/// - **Import Convenience**: Shorter syntax with `use bubbletea_widgets::viewport::new`
/// - **API Consistency**: Matches the pattern used by other bubbles components
pub fn new(width: usize, height: usize) -> Model {
    Model::new(width, height)
}