tslime 0.1.1

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

use image::io::Reader as ImageReader;
use serde::{Deserialize, Serialize};
use std::borrow::Cow;
use std::path::Path;

use super::agent::normalize_angle;
use crate::config_defaults::{
    agent as agent_consts, environment, environment as env_consts, food as food_img_consts,
    population, population as pop_consts, time as time_consts, trail as trail_consts,
};
use crate::render::palette::RgbColor;

/// Algorithm used for pheromone diffusion (spreading).
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "lowercase")]
pub enum DiffusionKernel {
    /// Simple 3×3 box blur averaging. Fast with sharp patterns.
    Mean3x3,
    /// 5×5 Gaussian blur. Slower but produces smoother, more organic patterns.
    Gaussian,
}

/// Nonlinear curve applied to per-frame accumulated deposit before folding
/// into the trail. `Linear` (with scale 1, cap 0) is byte-identical to the
/// historical per-agent deposit.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Default, Serialize, Deserialize)]
#[serde(rename_all = "lowercase")]
pub enum DepositCurve {
    /// Identity: `x`. Default; preserves historical behavior.
    #[default]
    Linear,
    /// `sqrt(x)` — compresses density spikes into filaments.
    Sqrt,
    /// `ln(1 + x)` — log compression (0 at 0, guards log(0)).
    Log,
    /// `x^gamma` — `deposit_gamma` is the exponent (γ<1 compresses, γ>1 expands).
    Pow,
}

impl DepositCurve {
    /// Apply the curve to a (non-negative) accumulated deposit value.
    /// `gamma` is used only by `Pow`.
    #[inline]
    pub fn apply(self, x: f32, gamma: f32) -> f32 {
        match self {
            DepositCurve::Linear => x,
            DepositCurve::Sqrt => x.sqrt(),
            DepositCurve::Log => (1.0 + x).ln(),
            DepositCurve::Pow => x.powf(gamma),
        }
    }
}

/// Named parameter presets for different visual styles.
///
/// Each preset combines multiple parameters optimized for a specific aesthetic.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "lowercase")]
pub enum Preset {
    /// Dense, interconnected networks with rapid branching.
    Network,
    /// Wide, searching tentacles with exploratory behavior.
    Exploratory,
    /// Long branching arms stretching across the terminal.
    Tendrils,
    /// Balanced, natural-looking growth (default).
    Organic,
    /// Aggressive, fast-moving flame-like patterns.
    Fire,
    /// Flowing, water-like patterns.
    River,
    /// Petri dish simulation: starts center, slow growth, persistent trails.
    #[serde(rename = "petridish")]
    PetriDish,
    /// Spinning vortex patterns (rotation_angle > sensor_angle).
    Vortex,
    /// Fast dendritic branching like lightning.
    Lightning,
    /// Edge-of-chaos sensitive patterns (sensor_angle ≈ rotation_angle).
    #[serde(rename = "chaosedge")]
    ChaosEdge,
    /// Aggregating blob clusters.
    Blob,
    /// Slime-mold surface tension with trail-based flow modulation.
    Slime,
    /// Creeping vine tendrils with trail-modulated cohesion.
    Vines,
    /// ASCII-rendered cohesive flocking (vines pattern).
    Vinescii,
    /// Drifting smoke columns with wrapping boundary.
    Smoke,
    /// Enhanced vortex with trail modulation.
    #[serde(rename = "vortex36")]
    Vortex36,
    /// Dynamic tendrils with trail-based sensor modulation.
    #[serde(rename = "dynamictendrils")]
    DynamicTendrils,
    /// Bleuje-style front-lit veins: temporal-accent recolor of growing fronts.
    Mold,
    /// Directional filament linework via Sobel glyph selection (Braille, TUI-only).
    Etching,
    /// Color that shifts with motion direction (temporal Hue mode).
    Drift,
    /// Constellation: crisp star-map held via continuous template re-stamp (Points charset).
    Constellation,
    /// Posterized color bands (Quantize mapping + Wrap palette cycles).
    Mosaic,
    /// Veined stone via heavy Gaussian + Perlin intensity mapping.
    Marble,
    /// Maximum color resolution (HalfBlockDual + SquareRoot mapping).
    Prism,
    /// Soft parchment density (Shade charset + Log deposit curve).
    Vellum,
    /// Grainy molten thermal (Exponential mapping + afterglow).
    Forge,
    /// Slow ghosting decay via low decay-gamma + Pow deposit curve.
    Wane,
    /// Delicate threads (Braille + brightness glyphs + Power mapping).
    Gossamer,
    /// Typographic engraving (custom ASCII + Sigmoid mapping).
    Codex,
    /// Living water with animated hue-shift over time.
    Tide,
    /// The tslime logo held as a stable figure: constellation re-stamp behavior
    /// with the embedded logo image as the template (FoodConstellation init).
    Trademark,
}

/// Static identity of one preset: the enum variant, its display name, extra
/// parse aliases, and an optional number-row quick-select key.
///
/// [`PRESETS`] is the single source of truth for preset *identity*: CLI parsing,
/// display names, the live quick-select keys, and the validation test all derive
/// from it. Per-preset simulation parameters live in [`Preset::apply`]; per-preset
/// render defaults live in `RenderArtDefaults`. This table deliberately does not
/// duplicate either payload — only identity.
pub struct PresetSpec {
    /// The preset this entry describes.
    pub preset: Preset,
    /// Display name; also the canonical case-insensitive CLI parse key.
    pub name: &'static str,
    /// Additional case-insensitive names accepted on the CLI (hyphen/underscore
    /// variants, short forms).
    pub aliases: &'static [&'static str],
    /// Number-row key (`1`–`7`) that live-switches to this preset, if any. The
    /// shifted form selects it for A/B comparison.
    pub quick_key: Option<char>,
}

/// Identity table for every preset — the single list to edit when adding or
/// removing one (alongside the [`Preset`] variant and its [`Preset::apply`] arm).
pub const PRESETS: &[PresetSpec] = &[
    PresetSpec {
        preset: Preset::Network,
        name: "Network",
        aliases: &[],
        quick_key: None,
    },
    PresetSpec {
        preset: Preset::Exploratory,
        name: "Exploratory",
        aliases: &[],
        quick_key: None,
    },
    PresetSpec {
        preset: Preset::Tendrils,
        name: "Tendrils",
        aliases: &[],
        quick_key: None,
    },
    PresetSpec {
        preset: Preset::Organic,
        name: "Organic",
        aliases: &[],
        quick_key: Some('1'),
    },
    PresetSpec {
        preset: Preset::Fire,
        name: "Fire",
        aliases: &[],
        quick_key: None,
    },
    PresetSpec {
        preset: Preset::River,
        name: "River",
        aliases: &[],
        quick_key: None,
    },
    PresetSpec {
        preset: Preset::PetriDish,
        name: "PetriDish",
        aliases: &["petri"],
        quick_key: None,
    },
    PresetSpec {
        preset: Preset::Vortex,
        name: "Vortex",
        aliases: &[],
        quick_key: None,
    },
    PresetSpec {
        preset: Preset::Lightning,
        name: "Lightning",
        aliases: &[],
        quick_key: None,
    },
    PresetSpec {
        preset: Preset::ChaosEdge,
        name: "ChaosEdge",
        aliases: &["chaos-edge", "chaos_edge"],
        quick_key: None,
    },
    PresetSpec {
        preset: Preset::Blob,
        name: "Blob",
        aliases: &[],
        quick_key: None,
    },
    PresetSpec {
        preset: Preset::Slime,
        name: "Slime",
        aliases: &["pulse"],
        quick_key: None,
    },
    PresetSpec {
        preset: Preset::Vines,
        name: "Vines",
        aliases: &["flocking"],
        quick_key: None,
    },
    PresetSpec {
        preset: Preset::Vinescii,
        name: "Vinescii",
        aliases: &["vines-ascii"],
        quick_key: Some('3'),
    },
    PresetSpec {
        preset: Preset::Smoke,
        name: "Smoke",
        aliases: &["ripple"],
        quick_key: None,
    },
    PresetSpec {
        preset: Preset::Vortex36,
        name: "Vortex36",
        aliases: &["vortex-36", "vortex_36"],
        quick_key: None,
    },
    PresetSpec {
        preset: Preset::DynamicTendrils,
        name: "DynamicTendrils",
        aliases: &["dynamic-tendrils", "dynamic_tendrils"],
        quick_key: None,
    },
    PresetSpec {
        preset: Preset::Mold,
        name: "Mold",
        aliases: &["lumen"],
        quick_key: None,
    },
    PresetSpec {
        preset: Preset::Etching,
        name: "Etching",
        aliases: &[],
        quick_key: None,
    },
    PresetSpec {
        preset: Preset::Drift,
        name: "Drift",
        aliases: &[],
        quick_key: None,
    },
    PresetSpec {
        preset: Preset::Constellation,
        name: "constellations",
        aliases: &["constellation", "atlas"],
        quick_key: Some('2'),
    },
    PresetSpec {
        preset: Preset::Mosaic,
        name: "Mosaic",
        aliases: &[],
        quick_key: None,
    },
    PresetSpec {
        preset: Preset::Marble,
        name: "Marble",
        aliases: &[],
        quick_key: None,
    },
    PresetSpec {
        preset: Preset::Prism,
        name: "Prism",
        aliases: &[],
        quick_key: None,
    },
    PresetSpec {
        preset: Preset::Vellum,
        name: "Vellum",
        aliases: &[],
        quick_key: None,
    },
    PresetSpec {
        preset: Preset::Forge,
        name: "Forge",
        aliases: &[],
        quick_key: None,
    },
    PresetSpec {
        preset: Preset::Wane,
        name: "Wane",
        aliases: &[],
        quick_key: None,
    },
    PresetSpec {
        preset: Preset::Gossamer,
        name: "Gossamer",
        aliases: &[],
        quick_key: None,
    },
    PresetSpec {
        preset: Preset::Codex,
        name: "Codex",
        aliases: &[],
        quick_key: None,
    },
    PresetSpec {
        preset: Preset::Tide,
        name: "Tide",
        aliases: &[],
        quick_key: None,
    },
    PresetSpec {
        preset: Preset::Trademark,
        name: "Trademark",
        aliases: &["logo", "logo-constellation", "logogram", "tslime"],
        quick_key: Some('4'),
    },
];

/// Looks up a preset by display name or alias (case-insensitive).
#[must_use]
pub fn preset_from_name(name: &str) -> Option<Preset> {
    PRESETS
        .iter()
        .find(|spec| {
            spec.name.eq_ignore_ascii_case(name)
                || spec.aliases.iter().any(|a| a.eq_ignore_ascii_case(name))
        })
        .map(|spec| spec.preset)
}

/// Comma-separated list of canonical preset names, for CLI error messages.
#[must_use]
pub fn preset_name_list() -> String {
    PRESETS
        .iter()
        .map(|spec| spec.name.to_lowercase())
        .collect::<Vec<_>>()
        .join(", ")
}

/// Preset bound to a number-row key (`1`–`7`) for live switching, if any.
#[must_use]
pub fn preset_for_set_key(key: char) -> Option<Preset> {
    PRESETS
        .iter()
        .find(|spec| spec.quick_key == Some(key))
        .map(|spec| spec.preset)
}

/// Preset bound to a shifted number key (`!@#$%^&`) for A/B comparison, if any.
#[must_use]
pub fn preset_for_compare_key(key: char) -> Option<Preset> {
    shifted_digit(key).and_then(preset_for_set_key)
}

/// Public mapping from a shifted number key (`!@#$%^&`) to its base digit (`1`-`7`).
#[must_use]
pub fn compare_key_digit(key: char) -> Option<char> {
    shifted_digit(key)
}

/// Maps a shifted number key to its base digit (`!`→`1` … `&`→`7`).
fn shifted_digit(key: char) -> Option<char> {
    match key {
        '!' => Some('1'),
        '@' => Some('2'),
        '#' => Some('3'),
        '$' => Some('4'),
        '%' => Some('5'),
        '^' => Some('6'),
        '&' => Some('7'),
        _ => None,
    }
}

impl Preset {
    /// Display name of this preset (from [`PRESETS`]).
    #[must_use]
    pub fn name(&self) -> &'static str {
        PRESETS
            .iter()
            .find(|spec| spec.preset == *self)
            .map_or("Unknown", |spec| spec.name)
    }

    /// A short one-line "character" tagline, shown under figlet/type transitions
    /// when taglines are enabled. Exhaustive so new presets must add one.
    pub fn tagline(&self) -> &'static str {
        use Preset::*;
        match self {
            Network => "dense interconnected mesh",
            Exploratory => "wide searching tentacles",
            Tendrils => "long branching arms",
            Organic => "balanced natural growth",
            Fire => "aggressive flame-like fronts",
            River => "flowing water-like channels",
            PetriDish => "slow center-out growth",
            Vortex => "spinning vortex currents",
            Lightning => "fast dendritic forks",
            ChaosEdge => "edge-of-chaos sensitivity",
            Blob => "aggregating blob clusters",
            Slime => "surface-tension flow",
            Vines => "creeping cohesive tendrils",
            Vinescii => "ascii cohesive flocking",
            Smoke => "drifting smoke columns",
            Vortex36 => "trail-modulated vortex",
            DynamicTendrils => "trail-sensing tendrils",
            Mold => "front-lit growing veins",
            Etching => "directional filament linework",
            Drift => "color drifts with motion",
            Constellation => "a crisp star-map",
            Mosaic => "posterized color bands",
            Marble => "veined stone",
            Prism => "maximum color resolution",
            Vellum => "soft parchment density",
            Forge => "grainy molten thermal",
            Wane => "slow ghosting decay",
            Gossamer => "delicate woven threads",
            Codex => "typographic engraving",
            Tide => "living water, shifting hue",
            Trademark => "the living tslime mark",
        }
    }
}

/// How agents are initially distributed in the simulation.
#[derive(Debug, Clone, Copy, PartialEq, Eq, serde::Serialize, serde::Deserialize)]
#[serde(rename_all = "lowercase")]
pub enum InitMode {
    /// Agents randomly distributed across the entire canvas.
    Random,
    /// Agents start from the center and burst outward.
    CentralBurst,
    /// Agents arranged in a circle.
    Circle,
    /// Agents distributed in a gradient pattern.
    Gradient,
    /// Agents start as a wave front.
    WaveFront,
    /// Agents arranged in a spiral pattern.
    Spiral,
    /// Agents in random clusters.
    RandomClusters,
    /// Agents distributed based on a loaded image (food source).
    Food,
    /// Agents distributed in a Gaussian blob at the center (Petri dish style).
    Petri,
    /// Agents seeded as a real star constellation (stars + asterism edges).
    Constellation,
    /// Agents seeded from a food/brightness image (e.g. the embedded tslime
    /// logo) and the image is held as a re-stamp template, so the picture
    /// persists like a constellation figure instead of dispersing.
    FoodConstellation,
}

impl InitMode {
    /// Uniformly pick any init mode for non-structural presets. Structural modes
    /// (`Constellation`, `FoodConstellation`) are excluded so they keep a stable
    /// layout.
    ///
    /// `ALL` is hand-maintained. The `_guard` match below is exhaustive, so a new
    /// `InitMode` variant breaks compilation there until it is handled — a forced
    /// reminder to decide whether the variant also belongs in `ALL`.
    pub fn random(rng: &mut impl rand::Rng) -> Self {
        use InitMode::*;
        const ALL: [InitMode; 9] = [
            Random,
            CentralBurst,
            Circle,
            Gradient,
            WaveFront,
            Spiral,
            RandomClusters,
            Food,
            Petri,
        ];
        #[allow(dead_code)] // compile-time exhaustiveness guard; never called
        const fn _guard(m: InitMode) {
            match m {
                InitMode::Random
                | InitMode::CentralBurst
                | InitMode::Circle
                | InitMode::Gradient
                | InitMode::WaveFront
                | InitMode::Spiral
                | InitMode::RandomClusters
                | InitMode::Food
                | InitMode::Petri
                | InitMode::Constellation
                | InitMode::FoodConstellation => {}
            }
        }
        ALL[rng.gen_range(0..ALL.len())]
    }
}

#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
/// Types of terrain-based steering bias.
pub enum TerrainType {
    /// No terrain effect.
    #[default]
    None,
    /// Smooth, flowing patterns based on Perlin noise.
    Smooth,
    /// Chaotic, turbulent patterns.
    Turbulent,
    /// Combination of smooth and turbulent layers.
    Mixed,
}

impl std::str::FromStr for TerrainType {
    type Err = String;

    fn from_str(s: &str) -> Result<Self, Self::Err> {
        match s.to_lowercase().as_str() {
            "none" | "off" | "disabled" => Ok(TerrainType::None),
            "smooth" => Ok(TerrainType::Smooth),
            "turbulent" => Ok(TerrainType::Turbulent),
            "mixed" => Ok(TerrainType::Mixed),
            _ => Err(format!(
                "Invalid terrain type: {}. Must be one of: none, smooth, turbulent, mixed",
                s
            )),
        }
    }
}

#[derive(Debug, Clone, Copy, PartialEq)]
/// Global wind force configuration.
pub struct Wind {
    /// Horizontal wind strength (-1.0 to 1.0).
    pub dx: f32,
    /// Vertical wind strength (-1.0 to 1.0).
    pub dy: f32,
}

impl Wind {
    /// Creates a new wind vector.
    pub fn new(dx: f32, dy: f32) -> Self {
        Self { dx, dy }
    }
}

impl Default for Wind {
    fn default() -> Self {
        Self { dx: 0.0, dy: 0.0 }
    }
}

impl Validatable for Wind {
    fn validate(&self) -> Result<(), ValidationError> {
        if self.dx < -1.0 || self.dx > 1.0 {
            return Err(ValidationError::out_of_range("wind.dx", -1.0, 1.0, self.dx));
        }
        if self.dy < -1.0 || self.dy > 1.0 {
            return Err(ValidationError::out_of_range("wind.dy", -1.0, 1.0, self.dy));
        }
        if self.dx.abs() < 0.001 && self.dy.abs() < 0.001 {
            return Err(ValidationError::custom("wind cannot be zero vector"));
        }
        Ok(())
    }
}

impl std::str::FromStr for Wind {
    type Err = String;

    fn from_str(s: &str) -> Result<Self, Self::Err> {
        use crate::validation::Validatable;

        let parts: Vec<&str> = s.split(',').collect();
        if parts.len() != 2 {
            return Err(format!("Wind must be in dx,dy format, got: {}", s));
        }

        let dx = parts[0]
            .parse::<f32>()
            .map_err(|e| format!("Invalid dx: {}", e))?;
        let dy = parts[1]
            .parse::<f32>()
            .map_err(|e| format!("Invalid dy: {}", e))?;

        let wind = Wind::new(dx, dy);
        Validatable::validate(&wind).map_err(|e| e.to_string())?;
        Ok(wind)
    }
}

#[derive(Debug, Clone, Copy, PartialEq, Default)]
/// A point attractor or repeller.
pub struct Attractor {
    /// X coordinate.
    pub x: f32,
    /// Y coordinate.
    pub y: f32,
    /// Strength of attraction (negative for repulsion).
    pub strength: f32,
}

impl Attractor {
    /// Creates a new attractor.
    pub fn new(x: f32, y: f32, strength: f32) -> Self {
        Self { x, y, strength }
    }
}

#[derive(Debug, Clone, Copy, PartialEq)]
/// A temporary attractor created by mouse interaction.
pub struct MouseAttractor {
    /// X coordinate.
    pub x: f32,
    /// Y coordinate.
    pub y: f32,
    /// Strength of attraction/repulsion.
    pub strength: f32,
    /// Time of creation.
    pub created_at: std::time::Instant,
    /// Duration in seconds before expiration.
    pub timeout_seconds: f32,
}

impl MouseAttractor {
    /// Creates a new mouse attractor.
    pub fn new(x: f32, y: f32, strength: f32, timeout_seconds: f32) -> Self {
        Self {
            x,
            y,
            strength,
            created_at: std::time::Instant::now(),
            timeout_seconds,
        }
    }

    /// Checks if the attractor has expired.
    pub fn is_expired(&self) -> bool {
        self.created_at.elapsed().as_secs_f32() >= self.timeout_seconds
    }
}

#[derive(Debug, Clone, PartialEq)]
/// Mask data for image-based obstacles.
pub struct ObstacleMask {
    /// Flattened pixel data (normalized brightness).
    pub pixels: Vec<f32>,
    /// Width of the mask.
    pub width: usize,
    /// Height of the mask.
    pub height: usize,
}

impl ObstacleMask {
    /// Creates a mask from an image file.
    ///
    /// Resizes the image to target dimensions.
    pub fn from_image(
        image_path: &str,
        target_width: usize,
        target_height: usize,
        invert: bool,
    ) -> Result<Self, String> {
        let path = Path::new(image_path);

        if !path.exists() {
            return Err(format!("Image file not found: {}", image_path));
        }

        let img = ImageReader::open(path)
            .map_err(|e| format!("Failed to open image: {}", e))?
            .decode()
            .map_err(|e| format!("Failed to decode image: {}", e))?;

        let resized = img.resize_exact(
            target_width as u32,
            target_height as u32,
            image::imageops::FilterType::Nearest,
        );

        let pixels: Vec<f32> = resized
            .to_luma8()
            .pixels()
            .map(|p| {
                let brightness = p[0] as f32 / 255.0;
                if invert {
                    1.0 - brightness
                } else {
                    brightness
                }
            })
            .collect();

        Ok(Self {
            pixels,
            width: target_width,
            height: target_height,
        })
    }
}

#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
/// Geometric shape or image obstacle definition.
pub enum Obstacle {
    /// Circular obstacle.
    Circle {
        /// Center X.
        x: f32,
        /// Center Y.
        y: f32,
        /// Radius.
        radius: f32,
    },
    /// Rectangular obstacle.
    Rect {
        /// Top-left X.
        x: f32,
        /// Top-left Y.
        y: f32,
        /// Width.
        width: f32,
        /// Height.
        height: f32,
    },
    /// Image-based obstacle mask.
    Image {
        /// Path to image file.
        path: String,
        /// Top-left X.
        x: f32,
        /// Top-left Y.
        y: f32,
        /// Width.
        width: usize,
        /// Height.
        height: usize,
        /// Whether to invert the image mask.
        invert: bool,
        /// Brightness threshold for collision.
        threshold: f32,
    },
}

impl Obstacle {
    /// Checks if a point is contained within the obstacle.
    pub fn contains(&self, px: f32, py: f32, mask: Option<&ObstacleMask>) -> bool {
        match self {
            Obstacle::Circle { x, y, radius } => {
                let dx = px - x;
                let dy = py - y;
                dx * dx + dy * dy <= radius * radius
            }
            Obstacle::Rect {
                x,
                y,
                width,
                height,
            } => px >= *x && px <= *x + *width && py >= *y && py <= *y + *height,
            Obstacle::Image {
                path: _,
                x,
                y,
                width,
                height,
                invert: _,
                threshold,
            } => {
                let lx = px - x;
                let ly = py - y;
                if lx < 0.0 || lx >= *width as f32 || ly < 0.0 || ly >= *height as f32 {
                    return false;
                }
                if let Some(m) = mask {
                    let ix = lx as usize;
                    let iy = ly as usize;
                    let idx = iy * m.width + ix;
                    if idx >= m.pixels.len() {
                        return false;
                    }
                    m.pixels[idx] >= *threshold
                } else {
                    false
                }
            }
        }
    }

    /// Calculates new heading after bouncing off the obstacle.
    pub fn bounce(&self, px: f32, py: f32, heading: f32, _mask: Option<&ObstacleMask>) -> f32 {
        match self {
            Obstacle::Circle { x, y, radius: _ } => {
                let dx = px - x;
                let dy = py - y;
                let normal_angle = dy.atan2(dx);
                let new_heading = 2.0 * normal_angle - heading + std::f32::consts::PI;
                normalize_angle(new_heading)
            }
            Obstacle::Rect {
                x,
                y,
                width,
                height,
            } => {
                let nearest_x = px.clamp(*x, *x + *width);
                let nearest_y = py.clamp(*y, *y + *height);
                let dx = px - nearest_x;
                let dy = py - nearest_y;
                if dx.abs() > dy.abs() {
                    -heading + std::f32::consts::PI
                } else {
                    -heading
                }
            }
            Obstacle::Image {
                path: _,
                x: _,
                y: _,
                width: _,
                height: _,
                invert: _,
                threshold: _,
            } => -heading,
        }
    }
}

/// Boundary handling mode for agent movement.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Default, Serialize, Deserialize)]
#[serde(rename_all = "lowercase")]
pub enum BoundaryMode {
    /// Agents bounce/reflect at boundaries (default).
    #[default]
    Bounce,
    /// Agents wrap around to opposite side (toroidal).
    Wrap,
}

/// Window frame display mode for terminal visualization.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Default, Serialize, Deserialize)]
#[serde(rename_all = "lowercase")]
pub enum WindowFrame {
    /// No frame - full terminal used for simulation.
    None,
    /// Solid block border using accent color.
    Accented,
    /// Gradient border fading from accent color inward.
    Glow,
    /// Thin-line frame (default).
    #[default]
    Frame,
}

impl WindowFrame {
    /// Whether this mode reduces the simulation display area. Always `false`: the
    /// windowed layout reserves a frame ring uniformly, so no mode specially
    /// shrinks the area. Retained for API stability.
    pub fn reduces_display_area(&self) -> bool {
        false
    }

    /// Returns the window frame thickness in cells.
    pub fn thickness(&self) -> usize {
        match self {
            WindowFrame::None => 0,
            WindowFrame::Frame => 2,
            WindowFrame::Accented => 1,
            WindowFrame::Glow => 3,
        }
    }

    /// Returns true if window frame has visual rendering.
    pub fn is_visible(&self) -> bool {
        !matches!(self, WindowFrame::None)
    }
}

impl std::str::FromStr for WindowFrame {
    type Err = String;

    fn from_str(s: &str) -> Result<Self, Self::Err> {
        match s.to_lowercase().as_str() {
            "none" => Ok(WindowFrame::None),
            "accented" => Ok(WindowFrame::Accented),
            "glow" => Ok(WindowFrame::Glow),
            "frame" => Ok(WindowFrame::Frame),
            _ => Err(format!(
                "Invalid window frame: {}. Must be one of: none, accented, glow, frame",
                s
            )),
        }
    }
}

/// Chrome display level for window mode.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Default, Serialize, Deserialize)]
#[serde(rename_all = "lowercase")]
pub enum ChromeStyle {
    /// Frame only, no title or footer (default).
    #[default]
    Minimal,
    /// Always-visible title block + footer (sticky expanded).
    Expanded,
    /// No window; sim fills terminal edge-to-edge.
    Fullscreen,
}

impl std::str::FromStr for ChromeStyle {
    type Err = String;
    fn from_str(s: &str) -> Result<Self, Self::Err> {
        match s.to_lowercase().as_str() {
            "minimal" => Ok(ChromeStyle::Minimal),
            "expanded" => Ok(ChromeStyle::Expanded),
            "fullscreen" => Ok(ChromeStyle::Fullscreen),
            _ => Err(format!(
                "Invalid chrome style: '{}'. Must be one of: minimal, expanded, fullscreen",
                s
            )),
        }
    }
}

/// How a runtime preset switch is announced on screen.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Default, Serialize, Deserialize)]
#[serde(rename_all = "lowercase")]
pub enum TransitionStyle {
    /// No on-screen announcement (default) — preset switches silently.
    #[default]
    Off,
    /// Ambient toast notification.
    Toast,
    /// Big block-letter preset name, centered, fading up then out.
    Figlet,
    /// Typed letter-spaced readout over a dimmed band, with caret + underline.
    Type,
}

impl std::str::FromStr for TransitionStyle {
    type Err = String;
    fn from_str(s: &str) -> Result<Self, Self::Err> {
        match s.to_lowercase().as_str() {
            "off" | "none" => Ok(TransitionStyle::Off),
            "toast" => Ok(TransitionStyle::Toast),
            "figlet" => Ok(TransitionStyle::Figlet),
            "type" => Ok(TransitionStyle::Type),
            _ => Err(format!(
                "Invalid transition: '{}'. Must be one of: off, toast, figlet, type",
                s
            )),
        }
    }
}

impl std::fmt::Display for TransitionStyle {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        f.write_str(match self {
            TransitionStyle::Off => "off",
            TransitionStyle::Toast => "toast",
            TransitionStyle::Figlet => "figlet",
            TransitionStyle::Type => "type",
        })
    }
}

/// Visual aspect ratio for the simulation window.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
#[serde(try_from = "String", into = "String")]
pub struct Aspect {
    /// Horizontal units of the aspect ratio.
    pub width: u32,
    /// Vertical units of the aspect ratio.
    pub height: u32,
}

impl Default for Aspect {
    fn default() -> Self {
        Self {
            width: 3,
            height: 2,
        }
    }
}

impl Aspect {
    /// Terminal cell ratio (cells_w : cells_h) for halfblock rendering.
    ///
    /// With halfblock, each terminal cell packs 2 vertical sim pixels and is
    /// ~2:1 tall:wide, making halfblock pixels visually square. For a visual
    /// aspect of W:H, the required terminal cell ratio is W : (H/2).
    pub fn cell_ratio(&self) -> f32 {
        self.width as f32 / (self.height as f32 / 2.0)
    }
}

impl std::str::FromStr for Aspect {
    type Err = String;
    fn from_str(s: &str) -> Result<Self, Self::Err> {
        match s.to_lowercase().as_str() {
            "square" => {
                return Ok(Self {
                    width: 1,
                    height: 1,
                })
            }
            "4:3" => {
                return Ok(Self {
                    width: 4,
                    height: 3,
                })
            }
            "3:2" => {
                return Ok(Self {
                    width: 3,
                    height: 2,
                })
            }
            "16:10" => {
                return Ok(Self {
                    width: 16,
                    height: 10,
                })
            }
            "16:9" => {
                return Ok(Self {
                    width: 16,
                    height: 9,
                })
            }
            _ => {}
        }
        let parts: Vec<&str> = s.split(':').collect();
        if parts.len() != 2 {
            return Err(format!(
                "Invalid aspect '{}'. Use W:H or preset (square, 4:3, 3:2, 16:10, 16:9)",
                s
            ));
        }
        let w = parts[0]
            .parse::<u32>()
            .map_err(|_| format!("Invalid aspect width in '{}'", s))?;
        let h = parts[1]
            .parse::<u32>()
            .map_err(|_| format!("Invalid aspect height in '{}'", s))?;
        if w == 0 || h == 0 {
            return Err(format!("Aspect W and H must be non-zero, got '{}'", s));
        }
        Ok(Self {
            width: w,
            height: h,
        })
    }
}

/// Window outer padding — auto (5% of min terminal dimension, ≥ 2) or fixed cells.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Default, Serialize, Deserialize)]
#[serde(try_from = "String", into = "String")]
pub enum WindowPadding {
    /// Automatically compute padding (5% of smallest terminal dimension, minimum 2 cells).
    #[default]
    Auto,
    /// Fixed padding in terminal cells.
    Fixed(usize),
}

impl std::str::FromStr for WindowPadding {
    type Err = String;
    fn from_str(s: &str) -> Result<Self, Self::Err> {
        if s.to_lowercase() == "auto" {
            return Ok(Self::Auto);
        }
        let n = s
            .parse::<usize>()
            .map_err(|_| format!("Invalid window padding '{}'. Use 'auto' or an integer.", s))?;
        Ok(Self::Fixed(n))
    }
}

/// Minimum terminal size threshold for fallback logic (WxH format).
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
#[serde(try_from = "String", into = "String")]
pub struct TerminalSizeThreshold {
    /// Minimum terminal width in columns.
    pub width: usize,
    /// Minimum terminal height in rows.
    pub height: usize,
}

impl Default for TerminalSizeThreshold {
    fn default() -> Self {
        Self {
            width: 20,
            height: 10,
        }
    }
}

impl std::str::FromStr for TerminalSizeThreshold {
    type Err = String;
    fn from_str(s: &str) -> Result<Self, Self::Err> {
        let parts: Vec<&str> = s.split('x').collect();
        if parts.len() != 2 {
            return Err(format!(
                "Invalid size '{}'. Use WxH format, e.g. '20x10'",
                s
            ));
        }
        let w = parts[0]
            .parse::<usize>()
            .map_err(|_| format!("Invalid width in size '{}'", s))?;
        let h = parts[1]
            .parse::<usize>()
            .map_err(|_| format!("Invalid height in size '{}'", s))?;
        if w == 0 || h == 0 {
            return Err(format!("Size W and H must be non-zero, got '{}'", s));
        }
        Ok(Self {
            width: w,
            height: h,
        })
    }
}

/// Trail sampling method for agent sensing.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
pub enum SamplingMode {
    /// Fast nearest-pixel sampling (default).
    #[default]
    Nearest,
    /// Smooth bilinear interpolation.
    Bilinear,
}

impl std::str::FromStr for BoundaryMode {
    type Err = String;

    fn from_str(s: &str) -> Result<Self, Self::Err> {
        match s.to_lowercase().as_str() {
            "bounce" => Ok(BoundaryMode::Bounce),
            "wrap" | "toroidal" => Ok(BoundaryMode::Wrap),
            _ => Err(format!(
                "Invalid boundary mode: {}. Must be one of: bounce, wrap",
                s
            )),
        }
    }
}

/// 36 Points trail-based parameter modulation configuration.
///
/// This enables dynamic parameter adjustment based on the trail value at each agent's position,
/// creating diverse emergent behaviors as described in Sage Jenson's "36 Points" work.
#[derive(Debug, Clone, Copy, PartialEq, Serialize, Deserialize)]
pub struct PointConfig {
    /// Sensor distance base value (p1).
    pub sensor_distance_base: f32,
    /// Sensor distance multiplier (p2).
    pub sensor_distance_multiplier: f32,
    /// Sensor distance exponent (p3).
    pub sensor_distance_exponent: f32,

    /// Sensor angle base value in degrees (p4).
    pub sensor_angle_base: f32,
    /// Sensor angle multiplier (p5).
    pub sensor_angle_multiplier: f32,
    /// Sensor angle exponent (p6).
    pub sensor_angle_exponent: f32,

    /// Rotation angle base value in degrees (p7).
    pub rotation_angle_base: f32,
    /// Rotation angle multiplier (p8).
    pub rotation_angle_multiplier: f32,
    /// Rotation angle exponent (p9).
    pub rotation_angle_exponent: f32,

    /// Step size base value (p10).
    pub step_size_base: f32,
    /// Step size multiplier (p11).
    pub step_size_multiplier: f32,
    /// Step size exponent (p12).
    pub step_size_exponent: f32,

    /// Absolute vertical offset in pixels (p13).
    pub vertical_offset: f32,
    /// Heading-relative offset in pixels (p14).
    pub heading_offset: f32,
    /// Trail value rescaling factor (p15).
    pub trail_rescale: f32,
}

impl Default for PointConfig {
    fn default() -> Self {
        Self {
            // Default: no modulation (multipliers = 0, exponents = 1)
            sensor_distance_base: agent_consts::DEFAULT_SENSOR_DISTANCE,
            sensor_distance_multiplier: 0.0,
            sensor_distance_exponent: 1.0,
            sensor_angle_base: agent_consts::DEFAULT_SENSOR_ANGLE,
            sensor_angle_multiplier: 0.0,
            sensor_angle_exponent: 1.0,
            rotation_angle_base: agent_consts::DEFAULT_ROTATION_ANGLE,
            rotation_angle_multiplier: 0.0,
            rotation_angle_exponent: 1.0,
            step_size_base: agent_consts::DEFAULT_STEP_SIZE,
            step_size_multiplier: 0.0,
            step_size_exponent: 1.0,
            vertical_offset: 0.0,
            heading_offset: 0.0,
            trail_rescale: 1.0,
        }
    }
}

/// Computed modulated parameters for an agent.
#[derive(Debug, Clone, Copy, PartialEq)]
pub struct ModulatedParams {
    /// Modulated sensor distance.
    pub sensor_distance: f32,
    /// Modulated sensor angle in degrees.
    pub sensor_angle: f32,
    /// Modulated rotation angle in degrees.
    pub rotation_angle: f32,
    /// Modulated step size.
    pub step_size: f32,
}

impl PointConfig {
    /// Compute modulated parameters based on trail value x.
    ///
    /// Formulas:
    /// - sensor_distance = p1 + p2 * x^p3
    /// - sensor_angle    = p4 + p5 * x^p6
    /// - rotation_angle  = p7 + p8 * x^p9
    /// - step_size       = p10 + p11 * x^p12
    ///
    /// # Arguments
    /// * `x` - Trail value at agent position (should be in [0, 1])
    ///
    /// # Returns
    /// A `ModulatedParams` struct containing:
    /// - `sensor_distance`: Modulated sensor distance in pixels
    /// - `sensor_angle`: Modulated sensor angle in degrees
    /// - `rotation_angle`: Modulated rotation angle in degrees
    /// - `step_size`: Modulated step size in pixels
    pub fn compute_params(&self, x: f32) -> ModulatedParams {
        // Apply rescale factor and clamp to [0, 1]
        let x = (x * self.trail_rescale).clamp(0.0, 1.0);

        // Helper to compute modulated value with formula: base + multiplier * x^exponent
        let compute = |base: f32, multiplier: f32, exponent: f32| -> f32 {
            if multiplier == 0.0 || x == 0.0 {
                base
            } else if exponent == 1.0 {
                base + multiplier * x
            } else {
                base + multiplier * x.powf(exponent)
            }
        };

        ModulatedParams {
            sensor_distance: compute(
                self.sensor_distance_base,
                self.sensor_distance_multiplier,
                self.sensor_distance_exponent,
            )
            .clamp(
                agent_consts::MIN_SENSOR_DISTANCE,
                agent_consts::MAX_SENSOR_DISTANCE,
            ),
            sensor_angle: compute(
                self.sensor_angle_base,
                self.sensor_angle_multiplier,
                self.sensor_angle_exponent,
            )
            .clamp(
                agent_consts::MIN_SENSOR_ANGLE,
                agent_consts::MAX_SENSOR_ANGLE,
            ),
            rotation_angle: compute(
                self.rotation_angle_base,
                self.rotation_angle_multiplier,
                self.rotation_angle_exponent,
            )
            .clamp(
                agent_consts::MIN_ROTATION_ANGLE,
                agent_consts::MAX_ROTATION_ANGLE,
            ),
            step_size: compute(
                self.step_size_base,
                self.step_size_multiplier,
                self.step_size_exponent,
            )
            .clamp(agent_consts::MIN_STEP_SIZE, agent_consts::MAX_STEP_SIZE),
        }
    }

    /// Returns true if this config has any modulation enabled.
    pub fn has_modulation(&self) -> bool {
        self.sensor_distance_multiplier != 0.0
            || self.sensor_angle_multiplier != 0.0
            || self.rotation_angle_multiplier != 0.0
            || self.step_size_multiplier != 0.0
    }
}

/// Particle respawn configuration.
#[derive(Debug, Clone, Copy, PartialEq, Serialize, Deserialize)]
pub struct RespawnConfig {
    /// Interval in frames between respawn checks (0 = disabled).
    pub interval: u32,
    /// Base probability of respawn when interval is reached (0.0-1.0).
    pub base_probability: f32,
    /// Whether respawn probability depends on trail value.
    pub trail_dependent: bool,
    /// Maximum respawn probability multiplier, reached when the normalized trail
    /// value saturates to 1.0. Effective probability is
    /// `base_probability * (1 + x * (max_probability_multiplier - 1))`, where
    /// `x = (trail * trail_rescale).clamp(0, 1)`.
    pub max_probability_multiplier: f32,
    /// Scales the raw pheromone value into the normalized `[0, 1]` range before
    /// the multiplier is applied (mirrors `PointConfig::trail_rescale`). Pick it
    /// so healthy trail densities map well below 1.0 and only an abnormal
    /// accumulation (the wall-collapse line) saturates — otherwise the
    /// multiplier cap is meaningless because raw trail values are unbounded.
    pub trail_rescale: f32,
}

impl Default for RespawnConfig {
    fn default() -> Self {
        Self {
            interval: 0, // Disabled by default
            base_probability: 0.01,
            trail_dependent: false,
            max_probability_multiplier: 1.0,
            trail_rescale: 1.0,
        }
    }
}

#[derive(Debug, Clone, PartialEq)]
/// Configuration for a single agent species.
pub struct SpeciesConfig {
    /// Species name.
    pub name: String,
    /// Population count.
    pub count: usize,
    /// Sensor angle (degrees).
    pub sensor_angle: f32,
    /// Rotation angle (degrees).
    pub rotation_angle: f32,
    /// Step size (speed).
    pub step_size: f32,
    /// Amount of pheromone deposited.
    pub deposit_amount: f32,
    /// Color as RGB.
    pub color: RgbColor,
    /// Trail-based parameter modulation (36 Points).
    pub trail_modulation: Option<PointConfig>,
}

impl Default for SpeciesConfig {
    fn default() -> Self {
        Self {
            name: "default".to_string(),
            count: population::DEFAULT_POPULATION,
            sensor_angle: agent_consts::DEFAULT_SENSOR_ANGLE,
            rotation_angle: agent_consts::DEFAULT_ROTATION_ANGLE,
            step_size: agent_consts::DEFAULT_STEP_SIZE,
            deposit_amount: agent_consts::DEFAULT_DEPOSIT_AMOUNT,
            color: RgbColor::from_hex(0x228b22), // Forest green
            trail_modulation: None,
        }
    }
}

#[derive(Debug, Clone, PartialEq)]
/// Global simulation configuration.
pub struct SimConfig {
    /// Sensor angle (degrees).
    pub sensor_angle: f32,
    /// Sensor offset distance (pixels).
    pub sensor_distance: f32,
    /// Rotation angle (degrees).
    pub rotation_angle: f32,
    /// Agent speed (pixels/step).
    pub step_size: f32,
    /// Trail decay factor (0.5-0.9999).
    pub decay_factor: f32,
    /// Amount of trail deposited per step.
    pub deposit_amount: f32,
    /// Diffusion algorithm.
    pub diffusion_kernel: DiffusionKernel,
    /// Sigma for Gaussian diffusion.
    pub diffusion_sigma: f32,
    /// Diffusion blend weight (Lague): `new = old·(1−w) + blur·w`. 1.0 = full blur (today).
    pub diffuse_weight: f32,
    /// Nonlinear decay exponent γ. 1.0 = current multiplicative decay; γ<1 lengthens faint tails.
    pub decay_gamma: f32,
    /// Nonlinear deposit curve applied to the per-frame accumulation buffer.
    /// `Linear` + scale 1 + cap 0 = historical behavior (off path).
    pub deposit_curve: DepositCurve,
    /// Multiplier applied to `curve(accum)` before folding into the trail.
    pub deposit_scale: f32,
    /// Exponent for `DepositCurve::Pow` (ignored by other curves).
    pub deposit_gamma: f32,
    /// Clamp cap for the folded contribution; `0.0` = off.
    pub deposit_cap: f32,
    /// White-point divisor for brightness normalization (higher = darker).
    pub max_brightness: f32,
    /// Time scale multiplier (0.1-10.0).
    pub time_scale: f32,
    /// List of active attractors.
    pub attractors: Vec<Attractor>,
    /// Global attractor strength multiplier.
    pub attractor_strength: f32,
    /// Temporary mouse attractors.
    pub mouse_attractors: Vec<MouseAttractor>,
    /// Timeout for mouse attractors (seconds).
    pub mouse_timeout: f32,
    /// Configuration for each species.
    pub species_configs: Vec<SpeciesConfig>,
    /// Whether to use separate trail maps per species.
    pub separate_species_trails: bool,
    /// Whether to use SIMD acceleration.
    pub use_simd: bool,
    /// Path to food image for initialization.
    pub food_image_path: Option<String>,
    /// Whether to invert food image brightness.
    pub food_image_invert: bool,
    /// Scaling factor for food image.
    pub food_image_scale: f32,
    /// List of obstacles.
    pub obstacles: Vec<Obstacle>,
    /// Loaded masks for image obstacles.
    pub obstacle_masks: Vec<Option<ObstacleMask>>,
    /// Global wind force.
    pub wind: Option<Wind>,
    /// Active terrain effect.
    pub terrain: TerrainType,
    /// Strength of terrain effect.
    pub terrain_strength: f32,
    /// Background color hex code.
    pub background_color: Option<String>,
    /// Preferred initialization mode for this config (if any).
    pub preferred_init_mode: Option<InitMode>,
    /// Boundary handling mode (bounce or wrap).
    pub boundary_mode: BoundaryMode,
    /// Window frame display mode for terminal visualization.
    pub window_frame: WindowFrame,
    /// Background matte width in columns between the frame border and the sim
    /// (left/right). Wider than `frame_matte_rows` to offset terminal cell aspect.
    pub frame_matte_cols: usize,
    /// Background matte height in rows between the frame border and the sim
    /// (top/bottom).
    pub frame_matte_rows: usize,
    /// Chrome display style (minimal, expanded, fullscreen).
    pub chrome_style: ChromeStyle,
    /// How a runtime preset switch is announced (toast/figlet/type).
    pub transition_style: TransitionStyle,
    /// Whether the figlet/type transition shows the preset tagline (default false).
    pub transition_tagline: bool,
    /// Visual aspect ratio of the simulation window.
    pub aspect: Aspect,
    /// Outer padding between terminal edge and window frame.
    pub window_padding: WindowPadding,
    /// Show legacy status bar in windowed mode (default false).
    pub show_status_bar: bool,
    /// Fallback threshold: below this sim size, drop padding.
    pub min_sim_size: TerminalSizeThreshold,
    /// Fallback threshold: below this sim size, drop the frame.
    pub min_frame_size: TerminalSizeThreshold,
    /// Particle respawn configuration.
    pub respawn_config: RespawnConfig,
    /// Trail sampling method (nearest or bilinear).
    pub sampling_mode: SamplingMode,
    /// Constellation atlas re-stamp strength, applied each frame after
    /// diffusion/decay. 0.0 = no re-stamp (drift); > 0.0 = self-healing
    /// template source (static hold).
    pub constellation_restamp_floor: f32,
}

impl SimConfig {
    /// Returns the total population across all species.
    pub fn total_population(&self) -> usize {
        self.species_configs.iter().map(|s| s.count).sum()
    }

    /// True when the nonlinear-deposit accumulation path is engaged. When
    /// false, deposits go straight to the trail (byte-identical to history).
    #[inline]
    pub fn deposit_active(&self) -> bool {
        self.deposit_curve != DepositCurve::Linear
            || self.deposit_scale != 1.0
            || self.deposit_cap > 0.0
    }

    /// Loads mask data for all image-based obstacles.
    pub fn load_obstacle_masks(&mut self) -> Result<(), String> {
        self.obstacle_masks.clear();
        for obstacle in &self.obstacles {
            match obstacle {
                Obstacle::Image {
                    path,
                    width,
                    height,
                    invert,
                    ..
                } => {
                    let mask = ObstacleMask::from_image(path, *width, *height, *invert)?;
                    self.obstacle_masks.push(Some(mask));
                }
                _ => {
                    self.obstacle_masks.push(None);
                }
            }
        }
        Ok(())
    }

    /// Adds a new mouse-controlled attractor.
    pub fn add_mouse_attractor(&mut self, x: f32, y: f32, strength: f32) {
        self.mouse_attractors
            .push(MouseAttractor::new(x, y, strength, self.mouse_timeout));
    }

    /// Removes mouse attractors that have timed out.
    pub fn remove_expired_mouse_attractors(&mut self) {
        self.mouse_attractors.retain(|ma| !ma.is_expired());
    }

    /// Returns a combined list of all active attractors (static + mouse).
    ///
    /// # Performance
    /// If there are no mouse attractors, returns a reference to the static attractors
    /// without cloning. Otherwise, returns an owned vector with both static and mouse attractors.
    pub fn effective_attractors(&self) -> Cow<'_, [Attractor]> {
        if self.mouse_attractors.is_empty() {
            // Fast path: no mouse attractors, return borrowed reference
            Cow::Borrowed(&self.attractors)
        } else {
            // Slow path: need to combine static and mouse attractors
            let mut result = self.attractors.clone();
            for ma in &self.mouse_attractors {
                result.push(Attractor::new(ma.x, ma.y, ma.strength));
            }
            Cow::Owned(result)
        }
    }
}

impl Default for SimConfig {
    fn default() -> Self {
        Self {
            sensor_angle: agent_consts::DEFAULT_SENSOR_ANGLE,
            sensor_distance: agent_consts::DEFAULT_SENSOR_DISTANCE,
            rotation_angle: agent_consts::DEFAULT_ROTATION_ANGLE,
            step_size: agent_consts::DEFAULT_STEP_SIZE,
            decay_factor: trail_consts::DEFAULT_DECAY_FACTOR,
            deposit_amount: agent_consts::DEFAULT_DEPOSIT_AMOUNT,
            diffusion_kernel: DiffusionKernel::Gaussian,
            diffusion_sigma: trail_consts::DEFAULT_DIFFUSION_SIGMA,
            diffuse_weight: trail_consts::DEFAULT_DIFFUSE_WEIGHT,
            decay_gamma: trail_consts::DEFAULT_DECAY_GAMMA,
            deposit_curve: DepositCurve::default(),
            deposit_scale: trail_consts::DEFAULT_DEPOSIT_SCALE,
            deposit_gamma: trail_consts::DEFAULT_DEPOSIT_GAMMA,
            deposit_cap: trail_consts::DEFAULT_DEPOSIT_CAP,
            max_brightness: trail_consts::DEFAULT_MAX_BRIGHTNESS,
            time_scale: time_consts::DEFAULT_TIME_SCALE,
            attractors: Vec::new(),
            attractor_strength: env_consts::DEFAULT_ATTRACTOR_STRENGTH,
            mouse_attractors: Vec::new(),
            mouse_timeout: env_consts::DEFAULT_MOUSE_TIMEOUT,
            species_configs: vec![SpeciesConfig::default()],
            separate_species_trails: false,
            use_simd: true,
            food_image_path: Some(food_img_consts::DEFAULT_FOOD_PATH.to_string()),
            food_image_invert: food_img_consts::DEFAULT_FOOD_INVERT,
            food_image_scale: food_img_consts::DEFAULT_FOOD_SCALE,
            obstacles: Vec::new(),
            obstacle_masks: Vec::new(),
            wind: None,
            terrain: TerrainType::None,
            terrain_strength: env_consts::DEFAULT_TERRAIN_STRENGTH,
            background_color: None,
            preferred_init_mode: Some(InitMode::Food),
            boundary_mode: BoundaryMode::Bounce,
            window_frame: WindowFrame::Frame,
            frame_matte_cols: crate::config_defaults::frame_matte::DEFAULT_COLS,
            frame_matte_rows: crate::config_defaults::frame_matte::DEFAULT_ROWS,
            chrome_style: ChromeStyle::Minimal,
            transition_style: TransitionStyle::Off,
            transition_tagline: false,
            aspect: Aspect::default(),
            window_padding: WindowPadding::Auto,
            show_status_bar: false,
            min_sim_size: TerminalSizeThreshold {
                width: 20,
                height: 10,
            },
            min_frame_size: TerminalSizeThreshold {
                width: 12,
                height: 6,
            },
            respawn_config: RespawnConfig::default(),
            sampling_mode: SamplingMode::Nearest,
            constellation_restamp_floor:
                crate::config_defaults::DEFAULT_CONSTELLATION_RESTAMP_FLOOR,
        }
    }
}

// Validation implementations using the Validatable trait
use crate::error::ValidationError;
use crate::validation::{rules, Validatable};

impl Validatable for SimConfig {
    fn validate(&self) -> Result<(), ValidationError> {
        // Check that at least one species is configured
        if self.species_configs.is_empty() {
            return Err(ValidationError::custom(
                "at least one species must be configured",
            ));
        }

        // Validate total population
        let total_pop: usize = self.species_configs.iter().map(|s| s.count).sum();
        if !(population::MIN_POPULATION..=population::MAX_POPULATION).contains(&total_pop) {
            return Err(ValidationError::custom(format!(
                "total population must be between {} and {}, got {}",
                population::MIN_POPULATION,
                population::MAX_POPULATION,
                total_pop
            )));
        }

        // Validate agent parameters
        rules::SENSOR_ANGLE.validate_f32(self.sensor_angle)?;
        rules::SENSOR_DISTANCE.validate_f32(self.sensor_distance)?;
        rules::ROTATION_ANGLE.validate_f32(self.rotation_angle)?;
        rules::STEP_SIZE.validate_f32(self.step_size)?;
        rules::DEPOSIT_AMOUNT.validate_f32(self.deposit_amount)?;

        // Validate trail parameters
        rules::DECAY_FACTOR.validate_f32(self.decay_factor)?;
        rules::MAX_BRIGHTNESS.validate_f32(self.max_brightness)?;
        rules::DIFFUSION_SIGMA.validate_f32(self.diffusion_sigma)?;
        rules::DECAY_GAMMA.validate_f32(self.decay_gamma)?;
        rules::DIFFUSE_WEIGHT.validate_f32(self.diffuse_weight)?;
        rules::DEPOSIT_SCALE.validate_f32(self.deposit_scale)?;
        rules::DEPOSIT_GAMMA.validate_f32(self.deposit_gamma)?;
        rules::DEPOSIT_CAP.validate_f32(self.deposit_cap)?;

        // Validate time and environment parameters
        rules::TIME_SCALE.validate_f32(self.time_scale)?;
        rules::ATTRACTOR_STRENGTH.validate_f32(self.attractor_strength)?;
        rules::TERRAIN_STRENGTH.validate_f32(self.terrain_strength)?;

        // Validate individual attractors
        for (i, attractor) in self.attractors.iter().enumerate() {
            if attractor.strength < environment::MIN_ATTRACTOR_STRENGTH
                || attractor.strength > environment::MAX_ATTRACTOR_STRENGTH
            {
                return Err(ValidationError::out_of_range(
                    format!("attractor[{}].strength", i),
                    environment::MIN_ATTRACTOR_STRENGTH,
                    environment::MAX_ATTRACTOR_STRENGTH,
                    attractor.strength,
                ));
            }
        }

        // Validate species configs
        for species in &self.species_configs {
            Validatable::validate(species)?;
        }

        // Validate wind if present
        if let Some(ref wind) = self.wind {
            Validatable::validate(wind)?;
        }

        Ok(())
    }
}

impl TryFrom<&crate::cli::Args> for SimConfig {
    type Error = crate::error::ValidationError;

    /// Builds a validated `SimConfig` from parsed CLI args.
    ///
    /// Assembles the config (preset merge + CLI overrides + species/wind/terrain/obstacles),
    /// then validates the final merged config once through [`Validatable::validate`].
    ///
    /// # Errors
    /// Returns [`ValidationError`] if assembly fails (e.g. invalid terrain string) or any
    /// merged parameter is out of range.
    fn try_from(args: &crate::cli::Args) -> Result<Self, Self::Error> {
        let profile = crate::profile::Profile::resolve_from_args(args)
            .map_err(crate::error::ValidationError::custom)?;
        Ok(profile.sim)
    }
}

impl Validatable for SpeciesConfig {
    fn validate(&self) -> Result<(), ValidationError> {
        // Validate count
        if self.count < pop_consts::MIN_SPECIES_COUNT || self.count > pop_consts::MAX_SPECIES_COUNT
        {
            return Err(ValidationError::out_of_range(
                format!("species '{}' count", self.name),
                pop_consts::MIN_SPECIES_COUNT,
                pop_consts::MAX_SPECIES_COUNT,
                self.count,
            ));
        }

        // Validate sensor angle
        if self.sensor_angle < agent_consts::MIN_SENSOR_ANGLE
            || self.sensor_angle > agent_consts::MAX_SENSOR_ANGLE
        {
            return Err(ValidationError::out_of_range(
                format!("species '{}' sensor_angle", self.name),
                agent_consts::MIN_SENSOR_ANGLE,
                agent_consts::MAX_SENSOR_ANGLE,
                self.sensor_angle,
            ));
        }

        // Validate rotation angle
        if self.rotation_angle < agent_consts::MIN_ROTATION_ANGLE
            || self.rotation_angle > agent_consts::MAX_ROTATION_ANGLE
        {
            return Err(ValidationError::out_of_range(
                format!("species '{}' rotation_angle", self.name),
                agent_consts::MIN_ROTATION_ANGLE,
                agent_consts::MAX_ROTATION_ANGLE,
                self.rotation_angle,
            ));
        }

        // Validate step size
        if self.step_size < agent_consts::MIN_STEP_SIZE
            || self.step_size > agent_consts::MAX_STEP_SIZE
        {
            return Err(ValidationError::out_of_range(
                format!("species '{}' step_size", self.name),
                agent_consts::MIN_STEP_SIZE,
                agent_consts::MAX_STEP_SIZE,
                self.step_size,
            ));
        }

        // Validate deposit amount
        if self.deposit_amount < agent_consts::MIN_DEPOSIT_AMOUNT
            || self.deposit_amount > agent_consts::MAX_DEPOSIT_AMOUNT
        {
            return Err(ValidationError::out_of_range(
                format!("species '{}' deposit_amount", self.name),
                agent_consts::MIN_DEPOSIT_AMOUNT,
                agent_consts::MAX_DEPOSIT_AMOUNT,
                self.deposit_amount,
            ));
        }

        Ok(())
    }
}

impl From<Preset> for SimConfig {
    fn from(preset: Preset) -> Self {
        let mut config = Self::default();
        crate::preset_sim_defaults::PresetSimDefaults::from(preset).apply_to(&mut config);
        config
    }
}

// ── serde string conversion impls (used by #[serde(try_from = "String", into = "String")]) ──

impl From<Aspect> for String {
    fn from(a: Aspect) -> Self {
        format!("{}:{}", a.width, a.height)
    }
}

impl TryFrom<String> for Aspect {
    type Error = String;
    fn try_from(s: String) -> Result<Self, Self::Error> {
        s.parse()
    }
}

impl From<WindowPadding> for String {
    fn from(p: WindowPadding) -> Self {
        match p {
            WindowPadding::Auto => "auto".to_string(),
            WindowPadding::Fixed(n) => n.to_string(),
        }
    }
}

impl TryFrom<String> for WindowPadding {
    type Error = String;
    fn try_from(s: String) -> Result<Self, Self::Error> {
        s.parse()
    }
}

impl From<TerminalSizeThreshold> for String {
    fn from(t: TerminalSizeThreshold) -> Self {
        format!("{}x{}", t.width, t.height)
    }
}

impl TryFrom<String> for TerminalSizeThreshold {
    type Error = String;
    fn try_from(s: String) -> Result<Self, Self::Error> {
        s.parse()
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use std::f32::consts::PI;

    #[test]
    fn init_mode_random_covers_all_over_many_draws() {
        use rand::SeedableRng;
        let mut rng = rand_xoshiro::Xoshiro256PlusPlus::seed_from_u64(42);
        const ALL: [InitMode; 9] = [
            InitMode::Random,
            InitMode::CentralBurst,
            InitMode::Circle,
            InitMode::Gradient,
            InitMode::WaveFront,
            InitMode::Spiral,
            InitMode::RandomClusters,
            InitMode::Food,
            InitMode::Petri,
        ];
        let mut seen = [false; 9];
        for _ in 0..1000 {
            let m = InitMode::random(&mut rng);
            let i = ALL.iter().position(|x| *x == m).unwrap();
            seen[i] = true;
        }
        assert!(seen.iter().all(|&s| s), "every InitMode should appear");
    }

    #[test]
    fn test_default_config() {
        let config = SimConfig::default();
        assert_eq!(config.total_population(), 50_000);
        assert_eq!(config.sensor_angle, 22.5);
        assert_eq!(config.sensor_distance, 9.0);
        assert_eq!(config.rotation_angle, 45.0);
        assert_eq!(config.step_size, 1.0);
        assert_eq!(config.decay_factor, 0.5);
        assert_eq!(config.deposit_amount, 5.0);
        assert_eq!(config.max_brightness, 100.0);
    }

    #[test]
    fn test_validate_default() {
        let config = SimConfig::default();
        assert!(config.validate().is_ok());
    }

    #[test]
    fn test_validate_population_too_low() {
        let config = SimConfig {
            species_configs: vec![SpeciesConfig {
                count: 500,
                ..Default::default()
            }],
            ..Default::default()
        };
        assert!(config.validate().is_err());
    }

    #[test]
    fn test_validate_population_too_high() {
        let config = SimConfig {
            species_configs: vec![SpeciesConfig {
                count: 300_000,
                ..Default::default()
            }],
            ..Default::default()
        };
        assert!(config.validate().is_err());
    }

    #[test]
    fn test_validate_sensor_angle() {
        let config = SimConfig {
            sensor_angle: 100.0,
            ..Default::default()
        };
        assert!(config.validate().is_err());
    }

    #[test]
    fn test_validate_decay_factor() {
        let config = SimConfig {
            decay_factor: 1.0,
            ..Default::default()
        };
        assert!(config.validate().is_err());
    }

    #[test]
    fn test_validate_max_brightness_too_low() {
        let config = SimConfig {
            max_brightness: 0.5,
            ..Default::default()
        };
        assert!(config.validate().is_err());
    }

    #[test]
    fn test_validate_max_brightness_too_high() {
        let config = SimConfig {
            max_brightness: 1500.0,
            ..Default::default()
        };
        assert!(config.validate().is_err());
    }

    #[test]
    fn test_validate_attractor_strength_too_low() {
        let config = SimConfig {
            attractor_strength: 0.05,
            ..Default::default()
        };
        assert!(config.validate().is_err());
    }

    #[test]
    fn test_validate_attractor_strength_too_high() {
        let config = SimConfig {
            attractor_strength: 15.0,
            ..Default::default()
        };
        assert!(config.validate().is_err());
    }

    #[test]
    fn test_validate_attractor_strength_valid() {
        let config = SimConfig {
            attractor_strength: 5.0,
            ..Default::default()
        };
        assert!(config.validate().is_ok());
    }

    #[test]
    fn test_attractor_creation() {
        let attractor = Attractor::new(200.0, 200.0, 1.0);
        assert_eq!(attractor.x, 200.0);
        assert_eq!(attractor.y, 200.0);
        assert_eq!(attractor.strength, 1.0);
    }

    #[test]
    fn test_negative_attractor_strength() {
        let attractor = Attractor::new(200.0, 200.0, -1.0);
        assert_eq!(attractor.strength, -1.0);
    }

    #[test]
    fn test_species_config_default() {
        let species = SpeciesConfig::default();
        assert_eq!(species.count, 50_000);
        assert_eq!(species.sensor_angle, 22.5);
        assert_eq!(species.rotation_angle, 45.0);
        assert_eq!(species.step_size, 1.0);
        assert_eq!(species.deposit_amount, 5.0);
    }

    #[test]
    fn test_species_config_validate_count_too_low() {
        let species = SpeciesConfig {
            count: 50,
            ..Default::default()
        };
        assert!(species.validate().is_err());
    }

    #[test]
    fn test_species_config_validate_count_too_high() {
        let species = SpeciesConfig {
            count: 300_000,
            ..Default::default()
        };
        assert!(species.validate().is_err());
    }

    #[test]
    fn test_total_population_single_species() {
        let config = SimConfig {
            species_configs: vec![SpeciesConfig {
                count: 10000,
                ..Default::default()
            }],
            ..Default::default()
        };
        assert_eq!(config.total_population(), 10000);
    }

    #[test]
    fn test_total_population_multiple_species() {
        let config = SimConfig {
            species_configs: vec![
                SpeciesConfig {
                    count: 10000,
                    ..Default::default()
                },
                SpeciesConfig {
                    count: 20000,
                    name: "second".to_string(),
                    color: RgbColor::from_hex(0xff0000),
                    ..Default::default()
                },
            ],
            ..Default::default()
        };
        assert_eq!(config.total_population(), 30000);
    }

    #[test]
    fn test_obstacle_circle_contains() {
        let circle = Obstacle::Circle {
            x: 100.0,
            y: 100.0,
            radius: 50.0,
        };
        assert!(circle.contains(100.0, 100.0, None));
        assert!(circle.contains(100.0, 150.0, None));
        assert!(circle.contains(150.0, 100.0, None));
        assert!(!circle.contains(200.0, 100.0, None));
        assert!(!circle.contains(100.0, 200.0, None));
    }

    #[test]
    fn test_obstacle_rect_contains() {
        let rect = Obstacle::Rect {
            x: 100.0,
            y: 100.0,
            width: 50.0,
            height: 50.0,
        };
        assert!(rect.contains(100.0, 100.0, None));
        assert!(rect.contains(150.0, 150.0, None));
        assert!(!rect.contains(99.0, 100.0, None));
        assert!(!rect.contains(100.0, 99.0, None));
        assert!(!rect.contains(151.0, 100.0, None));
        assert!(!rect.contains(100.0, 151.0, None));
    }

    #[test]
    fn test_obstacle_circle_bounce() {
        let circle = Obstacle::Circle {
            x: 100.0,
            y: 100.0,
            radius: 50.0,
        };
        let heading = circle.bounce(100.0, 60.0, 0.0, None);
        assert!(
            heading.is_finite(),
            "Bounce should return a valid heading, got {}",
            heading
        );
    }

    #[test]
    fn test_obstacle_rect_bounce() {
        let rect = Obstacle::Rect {
            x: 100.0,
            y: 100.0,
            width: 50.0,
            height: 50.0,
        };
        let heading = rect.bounce(120.0, 100.0, 0.0, None);
        assert!(
            heading.is_finite(),
            "Bounce should return a valid heading, got {}",
            heading
        );
    }

    #[test]
    fn test_obstacle_mask_from_image_nonexistent() {
        let result = ObstacleMask::from_image("nonexistent.png", 100, 100, false);
        assert!(result.is_err());
        assert!(result.unwrap_err().contains("not found"));
    }

    #[test]
    fn test_sim_config_load_obstacle_masks() {
        let mut config = SimConfig {
            obstacles: vec![Obstacle::Circle {
                x: 100.0,
                y: 100.0,
                radius: 50.0,
            }],
            ..Default::default()
        };
        let result = config.load_obstacle_masks();
        assert!(result.is_ok());
        assert_eq!(config.obstacle_masks.len(), 1);
        assert!(config.obstacle_masks[0].is_none());
    }

    #[test]
    fn test_wind_creation() {
        let wind = Wind::new(0.5, 0.5);
        assert_eq!(wind.dx, 0.5);
        assert_eq!(wind.dy, 0.5);
    }

    #[test]
    fn test_wind_validate_valid() {
        let wind = Wind::new(1.0, 1.0);
        assert!(wind.validate().is_ok());

        let wind = Wind::new(-1.0, 0.0);
        assert!(wind.validate().is_ok());

        let wind = Wind::new(0.0, -1.0);
        assert!(wind.validate().is_ok());
    }

    #[test]
    fn test_wind_validate_invalid_dx() {
        let wind = Wind::new(1.5, 0.0);
        assert!(wind.validate().is_err());
    }

    #[test]
    fn test_wind_validate_invalid_dy() {
        let wind = Wind::new(0.0, 1.5);
        assert!(wind.validate().is_err());
    }

    #[test]
    fn test_wind_validate_zero() {
        let wind = Wind::new(0.0, 0.0);
        assert!(wind.validate().is_err());
    }

    #[test]
    fn test_wind_parse() {
        let wind: Wind = "0.5,0.5".parse().unwrap();
        assert_eq!(wind.dx, 0.5);
        assert_eq!(wind.dy, 0.5);

        let wind: Wind = "-0.3,0.7".parse().unwrap();
        assert_eq!(wind.dx, -0.3);
        assert_eq!(wind.dy, 0.7);
    }

    #[test]
    fn test_wind_parse_invalid() {
        assert!("0.5".parse::<Wind>().is_err());
        assert!("0.5,0.5,extra".parse::<Wind>().is_err());
        assert!("abc,def".parse::<Wind>().is_err());
    }

    #[test]
    fn test_terrain_type_parse() {
        assert_eq!("none".parse::<TerrainType>().unwrap(), TerrainType::None);
        assert_eq!("off".parse::<TerrainType>().unwrap(), TerrainType::None);
        assert_eq!(
            "smooth".parse::<TerrainType>().unwrap(),
            TerrainType::Smooth
        );
        assert_eq!(
            "turbulent".parse::<TerrainType>().unwrap(),
            TerrainType::Turbulent
        );
        assert_eq!("mixed".parse::<TerrainType>().unwrap(), TerrainType::Mixed);

        assert_eq!("NONE".parse::<TerrainType>().unwrap(), TerrainType::None);
        assert_eq!(
            "Smooth".parse::<TerrainType>().unwrap(),
            TerrainType::Smooth
        );
    }

    #[test]
    fn test_terrain_type_parse_invalid() {
        assert!("invalid".parse::<TerrainType>().is_err());
        assert!("chaos".parse::<TerrainType>().is_err());
    }

    #[test]
    fn test_sim_config_wind_field() {
        let config = SimConfig {
            wind: Some(Wind::new(0.5, 0.0)),
            ..Default::default()
        };
        assert!(config.wind.is_some());
        assert_eq!(config.wind.unwrap().dx, 0.5);
    }

    #[test]
    fn test_sim_config_terrain_field() {
        let config = SimConfig {
            terrain: TerrainType::Turbulent,
            terrain_strength: 2.0,
            ..Default::default()
        };
        assert_eq!(config.terrain, TerrainType::Turbulent);
        assert_eq!(config.terrain_strength, 2.0);
    }

    #[test]
    fn test_validate_terrain_strength_too_low() {
        let config = SimConfig {
            terrain_strength: 0.05,
            ..Default::default()
        };
        assert!(config.validate().is_err());
    }

    #[test]
    fn test_validate_terrain_strength_too_high() {
        let config = SimConfig {
            terrain_strength: 10.0,
            ..Default::default()
        };
        assert!(config.validate().is_err());
    }

    #[test]
    fn test_validate_wind_invalid() {
        let config = SimConfig {
            wind: Some(Wind::new(1.5, 0.0)),
            ..Default::default()
        };
        assert!(config.validate().is_err());
    }

    #[test]
    fn test_effective_attractors() {
        let mut config = SimConfig {
            attractors: vec![Attractor::new(10.0, 10.0, 1.0)],
            ..Default::default()
        };
        config.add_mouse_attractor(20.0, 20.0, 2.0);
        let effective = config.effective_attractors();
        assert_eq!(effective.len(), 2);
        assert_eq!(effective[0].strength, 1.0);
        assert_eq!(effective[1].strength, 2.0);
    }

    #[test]
    fn test_mouse_attractor_expiry() {
        let ma = MouseAttractor::new(10.0, 10.0, 1.0, 0.01);
        assert!(!ma.is_expired());
        std::thread::sleep(std::time::Duration::from_millis(20));
        assert!(ma.is_expired());
    }

    #[test]
    fn test_remove_expired_mouse_attractors() {
        let mut config = SimConfig {
            mouse_timeout: 0.01,
            ..Default::default()
        };
        config.add_mouse_attractor(10.0, 10.0, 1.0);
        assert_eq!(config.mouse_attractors.len(), 1);
        std::thread::sleep(std::time::Duration::from_millis(20));
        config.remove_expired_mouse_attractors();
        assert_eq!(config.mouse_attractors.len(), 0);
    }

    #[test]
    fn test_presets_valid() {
        for spec in PRESETS {
            let config: SimConfig = spec.preset.into();
            assert!(
                config.validate().is_ok(),
                "Preset {:?} failed validation: {:?}",
                spec.preset,
                config.validate()
            );
        }
    }

    #[test]
    fn preset_names_and_aliases_round_trip() {
        for spec in PRESETS {
            // Display name resolves back to this preset, both via the method and
            // the parser, case-insensitively.
            assert_eq!(spec.preset.name(), spec.name);
            assert_eq!(preset_from_name(spec.name), Some(spec.preset));
            assert_eq!(
                preset_from_name(&spec.name.to_lowercase()),
                Some(spec.preset)
            );
            for alias in spec.aliases {
                assert_eq!(
                    preset_from_name(alias),
                    Some(spec.preset),
                    "alias {alias} did not resolve to {:?}",
                    spec.preset
                );
            }
        }
        assert_eq!(preset_from_name("definitely-not-a-preset"), None);
    }

    #[test]
    fn preset_quick_keys_are_consistent() {
        let mut seen = Vec::new();
        for spec in PRESETS {
            if let Some(key) = spec.quick_key {
                assert!(
                    key.is_ascii_digit() && key != '0',
                    "quick_key {key} is not 1-9"
                );
                assert!(!seen.contains(&key), "duplicate quick_key {key}");
                seen.push(key);
                // The set key round-trips, and its shifted form selects the same
                // preset for comparison.
                assert_eq!(preset_for_set_key(key), Some(spec.preset));
                let shifted = match key {
                    '1' => '!',
                    '2' => '@',
                    '3' => '#',
                    '4' => '$',
                    '5' => '%',
                    '6' => '^',
                    '7' => '&',
                    _ => continue,
                };
                assert_eq!(preset_for_compare_key(shifted), Some(spec.preset));
            }
        }
    }

    #[test]
    fn launch_quick_keys_map_to_launch_presets() {
        assert_eq!(preset_for_set_key('1'), Some(Preset::Organic));
        assert_eq!(preset_for_set_key('2'), Some(Preset::Constellation));
        assert_eq!(preset_for_set_key('3'), Some(Preset::Vinescii));
        assert_eq!(preset_for_set_key('4'), Some(Preset::Trademark));
        for c in ['5', '6', '7'] {
            assert_eq!(
                preset_for_set_key(c),
                None,
                "key {c} must be unbound at launch"
            );
        }
        assert_eq!(preset_for_compare_key('@'), Some(Preset::Constellation));
        assert_eq!(preset_for_compare_key('$'), Some(Preset::Trademark));
    }

    #[test]
    fn test_try_from_args_valid() {
        use crate::cli::Args;
        use clap::Parser;
        let args = Args::parse_from(["tslime"]);
        let config = SimConfig::try_from(&args);
        assert!(
            config.is_ok(),
            "default args must convert: {:?}",
            config.err()
        );
    }

    #[test]
    fn test_try_from_args_rejects_out_of_range_sensor_angle() {
        use crate::cli::Args;
        use clap::Parser;
        let args = Args::parse_from(["tslime", "--sensor-angle", "200"]);
        let result = SimConfig::try_from(&args);
        assert!(result.is_err(), "sensor_angle 200 must be rejected");
    }

    #[test]
    #[cfg(feature = "multi-species")]
    fn test_try_from_args_rejects_bad_species_strict() {
        // NEW behavior: species params are now validated post-merge.
        use crate::cli::Args;
        use clap::Parser;
        let args = Args::parse_from(["tslime", "--species", "x:20000@999,45,1.0,5.0:ff0000"]);
        let result = SimConfig::try_from(&args);
        assert!(
            result.is_err(),
            "out-of-range species param must now error (was silently accepted)"
        );
    }

    #[test]
    #[cfg(not(feature = "multi-species"))]
    fn test_species_flag_rejected_without_feature() {
        use crate::cli::Args;
        use clap::Parser;
        assert!(
            Args::try_parse_from(["tslime", "--species", "x:1000"]).is_err(),
            "--species must be unknown without multi-species feature"
        );
    }

    #[test]
    fn test_obstacle_rect_bounce_sides() {
        let rect = Obstacle::Rect {
            x: 100.0,
            y: 100.0,
            width: 50.0,
            height: 50.0,
        };
        // Bounce off top/bottom (dy > dx)
        let h1 = rect.bounce(125.0, 99.9, 0.1, None);
        assert!((h1 - (-0.1)).abs() < 0.001);
        // Bounce off left/right (dx > dy)
        let h2 = rect.bounce(99.9, 125.0, 0.1, None);
        assert!((h2 - (PI - 0.1)).abs() < 0.001);
    }

    #[test]
    fn test_species_config_validate_all() {
        let s = SpeciesConfig {
            sensor_angle: 1.0,
            ..Default::default()
        };
        assert!(s.validate().is_err());
        let s = SpeciesConfig {
            rotation_angle: 1.0,
            ..Default::default()
        };
        assert!(s.validate().is_err());
        let s = SpeciesConfig {
            step_size: 0.005,
            ..Default::default()
        };
        assert!(s.validate().is_err());
        let s = SpeciesConfig {
            deposit_amount: 0.05,
            ..Default::default()
        };
        assert!(s.validate().is_err());
    }

    #[test]
    fn test_validatable_trait() {
        use crate::validation::Validatable;

        let valid_config = SimConfig::default();
        assert!(valid_config.validate().is_ok());

        let invalid_config = SimConfig {
            sensor_angle: 200.0, // Invalid
            ..Default::default()
        };
        assert!(invalid_config.validate().is_err());
    }

    #[test]
    fn test_species_validatable_trait() {
        use crate::validation::Validatable;

        let valid_species = SpeciesConfig::default();
        assert!(valid_species.validate().is_ok());

        let invalid_species = SpeciesConfig {
            count: 50, // Below minimum
            ..Default::default()
        };
        assert!(invalid_species.validate().is_err());
    }

    #[test]
    fn art_knob_defaults_are_backcompat_neutral() {
        let c = SimConfig::default();
        assert_eq!(
            c.diffuse_weight, 1.0,
            "diffuse_weight=1 == full blur == today"
        );
        assert_eq!(c.decay_gamma, 1.0, "decay_gamma=1 == current decay");
    }

    #[test]
    fn validate_decay_gamma_rejects_out_of_range() {
        let config = SimConfig {
            decay_gamma: 9999.0,
            ..SimConfig::default()
        };
        assert!(
            config.validate().is_err(),
            "decay_gamma=9999.0 must be rejected"
        );
    }

    #[test]
    fn validate_decay_gamma_accepts_valid() {
        let config = SimConfig {
            decay_gamma: 1.0,
            ..SimConfig::default()
        };
        assert!(
            config.validate().is_ok(),
            "decay_gamma=1.0 must be accepted"
        );
    }

    #[test]
    fn deposit_curve_apply_matches_definitions() {
        use crate::simulation::config::DepositCurve;
        // Linear is identity; gamma ignored.
        assert_eq!(DepositCurve::Linear.apply(3.0, 0.5), 3.0);
        // Sqrt.
        assert!((DepositCurve::Sqrt.apply(9.0, 1.0) - 3.0).abs() < 1e-6);
        assert_eq!(DepositCurve::Sqrt.apply(0.0, 1.0), 0.0);
        // Log is log1p: 0 at 0, monotonic.
        assert_eq!(DepositCurve::Log.apply(0.0, 1.0), 0.0);
        assert!((DepositCurve::Log.apply(std::f32::consts::E - 1.0, 1.0) - 1.0).abs() < 1e-6);
        // Pow uses gamma as exponent.
        assert!((DepositCurve::Pow.apply(4.0, 0.5) - 2.0).abs() < 1e-6);
        assert!((DepositCurve::Pow.apply(2.0, 2.0) - 4.0).abs() < 1e-6);
        // Default is Linear.
        assert_eq!(DepositCurve::default(), DepositCurve::Linear);
    }

    #[test]
    fn deposit_active_off_at_defaults() {
        let cfg = SimConfig::default();
        assert_eq!(cfg.deposit_curve, DepositCurve::Linear);
        assert_eq!(cfg.deposit_scale, 1.0);
        assert_eq!(cfg.deposit_gamma, 1.0);
        assert_eq!(cfg.deposit_cap, 0.0);
        assert!(!cfg.deposit_active(), "defaults must be the off path");

        let on = SimConfig {
            deposit_curve: DepositCurve::Sqrt,
            ..Default::default()
        };
        assert!(on.deposit_active());
        let scaled = SimConfig {
            deposit_scale: 2.0,
            ..Default::default()
        };
        assert!(scaled.deposit_active());
        let capped = SimConfig {
            deposit_cap: 5.0,
            ..Default::default()
        };
        assert!(capped.deposit_active());
    }

    #[test]
    fn deposit_validation_rejects_out_of_range() {
        let cfg = SimConfig {
            deposit_gamma: 0.0, // below MIN_DEPOSIT_GAMMA
            ..Default::default()
        };
        assert!(cfg.validate().is_err());
    }
}

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

    #[test]
    fn test_aspect_from_str_presets() {
        assert_eq!(
            "3:2".parse::<Aspect>().unwrap(),
            Aspect {
                width: 3,
                height: 2
            }
        );
        assert_eq!(
            "square".parse::<Aspect>().unwrap(),
            Aspect {
                width: 1,
                height: 1
            }
        );
        assert_eq!(
            "4:3".parse::<Aspect>().unwrap(),
            Aspect {
                width: 4,
                height: 3
            }
        );
        assert_eq!(
            "16:10".parse::<Aspect>().unwrap(),
            Aspect {
                width: 16,
                height: 10
            }
        );
        assert_eq!(
            "16:9".parse::<Aspect>().unwrap(),
            Aspect {
                width: 16,
                height: 9
            }
        );
    }

    #[test]
    fn test_aspect_from_str_custom() {
        assert_eq!(
            "5:3".parse::<Aspect>().unwrap(),
            Aspect {
                width: 5,
                height: 3
            }
        );
    }

    #[test]
    fn test_aspect_from_str_errors() {
        assert!("0:1".parse::<Aspect>().is_err());
        assert!("bad".parse::<Aspect>().is_err());
        assert!("1:2:3".parse::<Aspect>().is_err());
    }

    #[test]
    fn test_aspect_cell_ratio_3_2() {
        let aspect = Aspect {
            width: 3,
            height: 2,
        };
        // For 3:2 visual with halfblock: cell_ratio = 3 / (2/2) = 3.0
        assert!((aspect.cell_ratio() - 3.0).abs() < 0.001);
    }

    #[test]
    fn test_chrome_style_from_str() {
        assert_eq!(
            "minimal".parse::<ChromeStyle>().unwrap(),
            ChromeStyle::Minimal
        );
        assert_eq!(
            "expanded".parse::<ChromeStyle>().unwrap(),
            ChromeStyle::Expanded
        );
        assert_eq!(
            "fullscreen".parse::<ChromeStyle>().unwrap(),
            ChromeStyle::Fullscreen
        );
        assert!("invalid".parse::<ChromeStyle>().is_err());
    }

    #[test]
    fn test_window_padding_from_str() {
        assert_eq!(
            "auto".parse::<WindowPadding>().unwrap(),
            WindowPadding::Auto
        );
        assert_eq!(
            "4".parse::<WindowPadding>().unwrap(),
            WindowPadding::Fixed(4)
        );
        assert!("bad".parse::<WindowPadding>().is_err());
    }

    #[test]
    fn test_terminal_size_threshold_from_str() {
        let t = "20x10".parse::<TerminalSizeThreshold>().unwrap();
        assert_eq!(t.width, 20);
        assert_eq!(t.height, 10);
        assert!("bad".parse::<TerminalSizeThreshold>().is_err());
        assert!("0x10".parse::<TerminalSizeThreshold>().is_err());
    }
}