Py.Cafe

zartarr/

adaptus-climate-investment-platform

Adapt[us] Climate Investment Analysis

DocsPricing
  • app.py
  • requirements.txt
app.py
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
"""
Adapt[us] — Unified Climate Adaptation Investment Platform
========================================================

The complete, single-file Panel application for the Adapt[us] climate adaptation venture fund.
Designed for both LP fundraising demos and institutional due diligence analysis.

QUICK START:
    pixi run dev                    # Launch full application
    panel serve app.py --show       # Direct Panel serve

FEATURES:
    🌡️ Climate scenario modeling with real-time market uplift
    📊 Complete fund performance analysis with European waterfall
    💰 Personalized LP returns with S&P 500 benchmarking
    🎯 Investment pipeline with 12 target companies
    🌍 Impact analysis with dual financial + climate returns
    🔗 URL permalinking for scenario sharing
    🎨 Custom Adaptus Material UI theme with Space Mono typography

FILE STRUCTURE (for AI navigation):
    LINES    SECTION                        DESCRIPTION
    -------  -----------------------------  ----------------------------------
    32-126   📐 CONFIGURATION & THEMING     Panel setup, Adaptus colors, CSS
    128-235  🎛️  GLOBAL STATE MANAGEMENT    UnifiedState class, parameters
    237-410  🧮 CALCULATION ENGINE          Financial models, IRR, waterfall
    412-577  📊 DESIGN SYSTEM & DATA        Colors, sectors, target companies
    579-1102 🖼️  UI COMPONENTS              View functions, charts, tables
    1104-1167 🔗 URL STATE MANAGEMENT       Permalink sharing functionality
    1169-1300 🏗️  APPLICATION ASSEMBLY       Template creation, content binding

KEY CLASSES:
    UnifiedState: Complete parameter management for interactive controls

KEY FUNCTIONS:
    compute_fund_metrics(): Core financial calculation engine (cached)
    create_*_view(): UI component builders for each tab
    setup_url_persistence(): Scenario sharing via URL parameters
    create_unified_app(): Main application assembly

DATA STRUCTURES:
    ADAPTUS: Brand color palette and theme configuration
    SECTORS: Climate adaptation market data (5 sectors)
    TARGET_COMPANIES: Investment pipeline (12 companies)
    FUND_COMPARISON: Competitive landscape analysis

USAGE FOR AI AGENTS:
    - Search "def create_" for UI components
    - Search "# ===" for major sections
    - Search "param\\." for interactive parameters
    - Search "ADAPTUS\\[" for theme colors
    - Search "@pn.depends" for reactive functions

Author: Darren Clifford (dc@aucap.vc)
Fund: Adapt[us] Climate Adaptation Venture Fund ($30M, Pre-seed to Series A)
"""

from functools import lru_cache
from typing import Dict, List, Optional, Sequence, Tuple, TypedDict

import numpy as np
import orjson
import pandas as pd
import panel as pn
import panel_material_ui as pmui
import param
import plotly.graph_objects as go
import plotly.io as pio
from pydantic import BaseModel, Field, field_validator

# Type imports for proper type hints (used by pyrefly)
# Note: pmui components return their own types, not base Panel types

# Configure Panel settings
pn.config.sizing_mode = "stretch_width"
# Note: console_output is read-only in newer Panel versions
# pn.config.console_output = "disable"

# Adaptus brand palette
ADAPTUS = dict(
    primary="#107580",
    accent="#7B3E7A",
    teal_light="#2DAA9F",
    bg="#f6f2e7",
    surface="#FFFFFF",
    text="#222222",
    muted="#6A6A6A",
    warn="#E4A11B",
    danger="#C23B3B",
    success="#1B9B8A",
    cycle=["#7B3E7A", "#107580", "#2DAA9F", "#222222", "#E4A11B", "#1B9B8A"],
)

# Use Adaptus brand palette for consistency (defined early for pyodide compatibility)
COLORS = {
    "primary": ADAPTUS["primary"],  # Adaptus teal
    "secondary": ADAPTUS["accent"],  # Adaptus purple
    "accent": ADAPTUS["teal_light"],  # Light teal
    "info": ADAPTUS["text"],  # Dark text for info
    "warn": ADAPTUS["warn"],  # Adaptus warning orange
    "success": ADAPTUS["success"],  # Adaptus success teal
    "danger": ADAPTUS["danger"],  # Adaptus danger red
    "background": ADAPTUS["bg"],  # Adaptus cream background
    "surface": ADAPTUS["surface"],  # Pure white
    "text": ADAPTUS["text"],  # Dark gray text
    "muted": ADAPTUS["muted"],  # Muted gray
}

# Comprehensive Material UI theme configuration with light/dark modes
THEME_CONFIG = {
    "light": {
        "palette": {
            "primary": {
                "main": ADAPTUS["primary"],
                "light": "#2DAA9F",
                "dark": "#0A5960",
                "contrastText": "#FFFFFF",
            },
            "secondary": {
                "main": ADAPTUS["accent"],
                "light": "#9C4B99",
                "dark": "#5C2E5B",
                "contrastText": "#FFFFFF",
            },
            "success": {"main": ADAPTUS["success"]},
            "warning": {"main": ADAPTUS["warn"]},
            "error": {"main": ADAPTUS["danger"]},
            "info": {"main": ADAPTUS["teal_light"]},
            "background": {
                "default": ADAPTUS["bg"],
                "paper": ADAPTUS["surface"],
            },
            "text": {
                "primary": ADAPTUS["text"],
                "secondary": ADAPTUS["muted"],
            },
        },
        "typography": {
            "fontFamily": "'Space Mono', monospace",
            "fontSize": 13,
            "fontWeight": 400,
            "button": {
                "fontSize": "0.875rem",
                "fontWeight": 600,
                "textTransform": "none",
            },
            # Responsive typography with mobile-first breakpoints
            "h1": {
                "fontSize": "2rem",
                "fontWeight": 700,
                "@media (min-width:600px)": {"fontSize": "2.5rem"},
                "@media (min-width:900px)": {"fontSize": "3rem"},
            },
            "h2": {
                "fontSize": "1.5rem",
                "fontWeight": 700,
                "@media (min-width:600px)": {"fontSize": "2rem"},
                "@media (min-width:900px)": {"fontSize": "2.5rem"},
            },
            "h3": {
                "fontSize": "1.25rem",
                "fontWeight": 700,
                "@media (min-width:600px)": {"fontSize": "1.75rem"},
            },
            "h4": {
                "fontSize": "1.125rem",
                "fontWeight": 600,
                "@media (min-width:600px)": {"fontSize": "1.5rem"},
            },
            "h5": {
                "fontSize": "1rem",
                "fontWeight": 600,
                "@media (min-width:600px)": {"fontSize": "1.25rem"},
            },
            "h6": {
                "fontSize": "0.875rem",
                "fontWeight": 600,
                "@media (min-width:600px)": {"fontSize": "1rem"},
            },
            "body1": {
                "fontSize": "0.875rem",
                "@media (min-width:600px)": {"fontSize": "1rem"},
            },
            "body2": {
                "fontSize": "0.8125rem",
                "@media (min-width:600px)": {"fontSize": "0.875rem"},
            },
        },
        "shape": {"borderRadius": 8},
        "components": {
            "MuiButtonBase": {
                "defaultProps": {"disableRipple": False},
            },
            "MuiButton": {
                "styleOverrides": {
                    "root": {
                        "fontFamily": "'Space Mono', monospace",
                    }
                }
            },
            "MuiCard": {
                "styleOverrides": {
                    "root": {
                        "fontFamily": "'Space Mono', monospace",
                    }
                }
            },
        },
    },
    "dark": {
        "palette": {
            "primary": {
                "main": "#2DAA9F",
                "light": "#5FC9BF",
                "dark": "#1B7A72",
                "contrastText": "#FFFFFF",
            },
            "secondary": {
                "main": "#9C4B99",
                "light": "#B76FB5",
                "dark": "#7B3E7A",
                "contrastText": "#FFFFFF",
            },
            "success": {"main": "#3FBB9E"},
            "warning": {"main": "#F4B336"},
            "error": {"main": "#E05656"},
            "info": {"main": "#5FC9BF"},
            "background": {
                "default": "#1a1a1a",
                "paper": "#2d2d2d",
            },
            "text": {
                "primary": "#f0f0f0",
                "secondary": "#999999",
            },
        },
        "typography": {
            "fontFamily": "'Space Mono', monospace",
            "fontSize": 13,
        },
        "shape": {"borderRadius": 8},
    },
}

# Font & Icon setup - load from CDN to avoid MIME type issues
pn.config.css_files = [
    "https://fonts.bunny.net/css?family=space-mono:400,700",  # Space Mono from Bunny Fonts
    "https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.4.0/css/all.min.css",  # FontAwesome 6 icons
]

# Simplified CSS - MUI theme now handles most styling
pn.config.raw_css = [
    """
.fa,.fas,.far,.fal,.fab{
  font-family:'Font Awesome 6 Free','Font Awesome 6 Pro','Font Awesome 6 Brands'!important;
  font-style:normal!important;display:inline-block!important
}
.fas{font-weight:900!important}.far,.fab{font-weight:400!important}.fal{font-weight:300!important}
""",
    f"""
/* Adaptus CSS variables for custom utility classes */
:root {{
  --ad-primary: {ADAPTUS["primary"]};
  --ad-accent:  {ADAPTUS["accent"]};
  --ad-teal:    {ADAPTUS["teal_light"]};
}}

/* Custom icon styling */
.adaptus-icon {{
  color: var(--mui-palette-primary-main);
  margin-right: 8px;
  vertical-align: middle;
}}

[data-theme="dark"] .adaptus-icon {{
  color: #2DAA9F;
}}
""",
]

# Setup Plotly Adaptus theme
tpl = pio.templates["plotly_white"]
tpl.layout = tpl.layout.update(
    font=dict(family="Space Mono, monospace", size=13, color=ADAPTUS["text"]),
    title=dict(
        font=dict(family="Space Mono, monospace", size=20, color=ADAPTUS["accent"])
    ),
    colorway=ADAPTUS["cycle"],
    paper_bgcolor=ADAPTUS["bg"],
    plot_bgcolor=ADAPTUS["surface"],
    legend=dict(orientation="h", x=0, y=1.02, bgcolor="rgba(0,0,0,0)"),
    xaxis=dict(
        gridcolor="#E6E1D8",
        zerolinecolor="#E6E1D8",
        linecolor="#B8B2A7",
        ticks="outside",
        title_standoff=8,
    ),
    yaxis=dict(
        gridcolor="#E6E1D8",
        zerolinecolor="#E6E1D8",
        linecolor="#B8B2A7",
        ticks="outside",
        title_standoff=8,
    ),
)
pio.templates["adaptus"] = tpl
pio.templates.default = "adaptus"

# Global Plotly config for professional, clean charts
# Applied to all pn.pane.Plotly instances via config parameter
PLOTLY_CONFIG = {
    "displayModeBar": "hover",  # Only show toolbar on hover
    "displaylogo": False,  # Remove Plotly logo
    "modeBarButtonsToRemove": ["lasso2d", "select2d"],  # Remove unnecessary tools
    "responsive": True,  # Auto-resize with container
}

pn.extension(
    "plotly",
    "tabulator",
    design="material",
    global_css=[
        f"""
:root {{
  --design-primary-color: {ADAPTUS["primary"]};
  --design-background-color: {ADAPTUS["bg"]};
  --design-surface-color: {ADAPTUS["surface"]};
  --design-background-text-color: {ADAPTUS["text"]};
}}
html,body,.bk-root {{ background: var(--design-background-color)!important;
  color: var(--design-background-text-color)!important; font-family:'Space Mono',monospace!important; }}
a{{ color:{ADAPTUS["accent"]}; }} a:hover{{ color:#5C2E5B; }}
.bk-btn:not(.bk-btn-default){{ background:{ADAPTUS["primary"]}; border-color:{ADAPTUS["primary"]}; color:#fff; }}
.bk-card,.bk-panel,.bk-root .card{{ background:var(--design-surface-color); }}
"""
    ],
)

# =====================================================================================
# 1. PYDANTIC DATA MODELS
#    - Structured data validation with Pydantic
#    - Type-safe models for fund parameters and company data
# =====================================================================================


class CompanyData(BaseModel):
    """Target company data model."""

    company: str = Field(..., min_length=1, description="Company name")
    website: str = Field(..., pattern=r"^https?://", description="Company website URL")
    stressor: str = Field(..., min_length=5, description="Climate stressor")
    pain: str = Field(..., min_length=5, description="Economic pain point")
    payer: str = Field(..., min_length=5, description="Specific payer with budget")
    model: str = Field(..., min_length=10, description="Business/revenue model")
    acceleration: str = Field(
        ..., min_length=10, description="Climate acceleration logic"
    )


class FundParameters(BaseModel):
    """Fund modeling parameters."""

    fund_size_m: float = Field(30.0, ge=10, le=100, description="Fund size in millions")
    invest_period_years: int = Field(5, ge=3, le=7, description="Investment period")
    harvest_years: int = Field(10, ge=8, le=15, description="Harvest period")
    fee_years_1_10: float = Field(
        0.02, ge=0.0, le=0.03, description="Management fee years 1-10"
    )
    fee_years_11_15: float = Field(
        0.015, ge=0.0, le=0.03, description="Management fee years 11-15"
    )
    carry: float = Field(0.20, ge=0.0, le=0.30, description="Carry rate")
    hurdle: float = Field(0.08, ge=0.0, le=0.15, description="Hurdle rate")
    temperature: float = Field(2.5, ge=1.5, le=3.5, description="Climate scenario")
    fee_basis_invest_period: str = Field(
        "committed",
        description="Fee basis during investment period (committed/called/nav)",
    )
    fee_basis_post_invest: str = Field(
        "called", description="Fee basis post-investment period (called/nav)"
    )

    @field_validator("temperature")
    @classmethod
    def validate_temperature(cls, v: float) -> float:
        """Validate temperature bounds."""
        if v < 1.5 or v > 3.5:
            raise ValueError("Temperature must be between 1.5°C and 3.5°C")
        return v

    @property
    def climate_demand_uplift(self) -> float:
        """Climate demand uplift from temperature."""
        return 1.0 + ((self.temperature - 2.0) * 0.3)


# =====================================================================================
# 2. GLOBAL STATE MANAGEMENT
#    - UnifiedState class with all interactive parameters
#    - Climate-driven demand uplift property (temperature → market acceleration)
#    - Fast orjson serialization for cached financial calculations
# =====================================================================================


class UnifiedState(param.Parameterized):
    """State management with climate, fund, portfolio, and LP parameters."""

    # Climate scenario (drives everything)
    temperature = param.Number(
        default=2.5,
        bounds=(1.5, 3.5),
        step=0.1,
        doc="Global climate warming scenario in degrees Celsius",
    )

    # Fund modeling parameters
    fund_size_m = param.Number(default=30.0, bounds=(10, 100), step=1.0)
    invest_period_years = param.Integer(default=5, bounds=(3, 7))
    harvest_years = param.Integer(default=10, bounds=(8, 15))
    fee_years_1_10 = param.Number(default=0.02, bounds=(0.0, 0.03), step=0.0025)
    fee_years_11_15 = param.Number(default=0.015, bounds=(0.0, 0.03), step=0.0025)
    carry = param.Number(default=0.20, bounds=(0.0, 0.30), step=0.01)
    hurdle = param.Number(default=0.08, bounds=(0.0, 0.15), step=0.005)
    fee_basis_invest_period = param.Selector(
        default="committed", objects=["committed", "called", "nav"]
    )
    fee_basis_post_invest = param.Selector(default="called", objects=["called", "nav"])

    # Portfolio construction parameters
    check_size_m = param.Number(default=0.5, bounds=(0.1, 2.0), step=0.1)
    followon_multiplier = param.Number(default=1.5, bounds=(1.0, 3.0), step=0.1)
    demand_uplift = param.Number(default=1.0, bounds=(0.8, 1.5), step=0.05)
    downside_floor = param.Boolean(default=False)

    @property
    def climate_demand_uplift(self):
        """Climate demand uplift from temperature."""
        return 1.0 + ((self.temperature - 2.0) * 0.3)

    def validate_fund_parameters(self) -> FundParameters:
        """Validate state via Pydantic."""
        return FundParameters(
            fund_size_m=self.fund_size_m,
            invest_period_years=self.invest_period_years,
            harvest_years=self.harvest_years,
            fee_years_1_10=self.fee_years_1_10,
            fee_years_11_15=self.fee_years_11_15,
            carry=self.carry,
            hurdle=self.hurdle,
            temperature=self.temperature,
            fee_basis_invest_period=self.fee_basis_invest_period,
            fee_basis_post_invest=self.fee_basis_post_invest,
        )

    # Portfolio outcome buckets (editable)
    buckets_df = param.DataFrame(
        default=pd.DataFrame(
            [
                {
                    "name": "Big winners (10x+)",
                    "count": 2,
                    "avg_moic": 27.0,
                    "avg_hold_years": 10.0,
                },
                {
                    "name": "Moderate (5–10x)",
                    "count": 3,
                    "avg_moic": 8.1,
                    "avg_hold_years": 8.6,
                },
                {
                    "name": "Small (1–5x)",
                    "count": 2,
                    "avg_moic": 3.8,
                    "avg_hold_years": 8.9,
                },
                {
                    "name": "Losers (0–1x)",
                    "count": 9,
                    "avg_moic": 0.2,
                    "avg_hold_years": 3.1,
                },
            ]
        )
    )

    # Venture builder parameters
    vb_enabled = param.Boolean(default=True)
    vb_invested = param.Number(default=4.47, bounds=(0, 10), step=0.25)
    vb_moic = param.Number(default=6.3, bounds=(1, 15), step=0.1)
    vb_hold_years = param.Number(default=6.0, bounds=(3, 10), step=0.5)

    # Warehouse parameters
    wh_enabled = param.Boolean(default=True)
    wh_cost_basis = param.Number(default=1.0, bounds=(0, 5), step=0.1)
    wh_market_value = param.Number(default=2.75, bounds=(0, 10), step=0.1)

    # LP-specific parameters
    lp_investment = param.Number(
        default=500_000, bounds=(100_000, 5_000_000), step=500_000
    )
    coinvest_enabled = param.Boolean(default=False)

    def validate_state(self) -> bool:
        """Validate state, returns True if valid."""
        try:
            self.validate_fund_parameters()
            return True
        except Exception as e:
            print(f"State validation failed: {e}")
            return False

    def to_engine_json(self) -> str:
        """Serialize state to JSON for compute_fund_metrics cache."""
        d = {
            "fund_size_m": self.fund_size_m,
            "invest_period_years": self.invest_period_years,
            "harvest_years": self.harvest_years,
            "fee_schedule": {
                "years_1_10": self.fee_years_1_10,
                "years_11_15": self.fee_years_11_15,
                "basis_invest_period": self.fee_basis_invest_period,
                "basis_post_invest": self.fee_basis_post_invest,
            },
            "carry_terms": {"carry": self.carry, "hurdle": self.hurdle},
            "buckets": self.buckets_df.to_dict("records"),
            "check_size_m": self.check_size_m,
            "followon_multiplier": self.followon_multiplier,
            "demand_uplift": max(
                self.demand_uplift, self.climate_demand_uplift
            ),  # Use higher of manual or climate-driven
            "venture_builder": {
                "enabled": self.vb_enabled,
                "invested": self.vb_invested,
                "moic": self.vb_moic,
                "hold_years": self.vb_hold_years,
            },
            "warehouse": {
                "enabled": self.wh_enabled,
                "cost_basis": self.wh_cost_basis,
                "market_value": self.wh_market_value,
            },
            "downside_floor": self.downside_floor,
        }
        return orjson.dumps(d, option=orjson.OPT_SORT_KEYS).decode()


# Global state instance - manages all interactive parameters
state = UnifiedState()

# Hard-lock fee policy defaults to avoid accidental drift
state.fee_basis_invest_period = "committed"
state.fee_basis_post_invest = "called"
state.carry = 0.20
state.hurdle = 0.08
state.coinvest_enabled = False
state.temperature = 2.5

# =====================================================================================
# 2. CALCULATION ENGINE
#    - NPV and IRR financial functions (no external dependencies)
#    - compute_fund_metrics(): Core fund performance with European waterfall
#    - Climate uplift integration and portfolio modeling
# =====================================================================================


def npv(rate: float, cashflows: List[float]) -> float:
    """NPV calculation, returns discounted cashflows."""
    return sum(cf / ((1 + rate) ** t) for t, cf in enumerate(cashflows))


def irr(cashflows: List[float]) -> float:
    """IRR via bisection, returns decimal or NaN."""
    if (
        not cashflows
        or all(cf >= 0 for cf in cashflows)
        or all(cf <= 0 for cf in cashflows)
    ):
        return float("nan")
    low, high = -0.99, 5.0
    f_low, f_high = npv(low, cashflows), npv(high, cashflows)
    if f_low * f_high > 0:
        for h in (10.0, 20.0, 50.0):
            f_high = npv(h, cashflows)
            if f_low * f_high <= 0:
                high = h
                break
        else:
            return float("nan")
    for _ in range(120):
        mid = 0.5 * (low + high)
        f_mid = npv(mid, cashflows)
        if abs(f_mid) < 1e-8:
            return mid
        if f_low * f_mid < 0:
            high = mid
        else:
            low, f_low = mid, f_mid
    return 0.5 * (low + high)


def safe_irr_pct(cashflows: List[float]) -> Optional[float]:
    """Safe IRR calculation returning percentage or None if undefined."""
    # needs at least one negative and one positive flow
    if (
        not cashflows
        or all(c >= 0 for c in cashflows)
        or all(c <= 0 for c in cashflows)
    ):
        return None
    r = irr(cashflows)
    return None if np.isnan(r) else r * 100.0


@lru_cache(maxsize=512)
def compute_fund_metrics(params_json: str) -> Tuple[pd.DataFrame, Dict, pd.DataFrame]:
    p = orjson.loads(params_json)

    fund_size_m = p["fund_size_m"]
    invest_years = p["invest_period_years"]
    harvest_years = p["harvest_years"]
    years = invest_years + harvest_years
    timeline = list(range(years + 1))

    fee10 = p["fee_schedule"]["years_1_10"]
    fee1511 = p["fee_schedule"]["years_11_15"]
    basis_invest = p["fee_schedule"].get("basis_invest_period", "committed")
    basis_post = p["fee_schedule"].get("basis_post_invest", "called")

    carry = p["carry_terms"]["carry"]
    hurdle = p["carry_terms"]["hurdle"]

    buckets = p["buckets"]
    check_size_m = p["check_size_m"]
    followon = p["followon_multiplier"]
    demand = p["demand_uplift"]
    downside = p.get("downside_floor", False)
    vb = p["venture_builder"]
    wh = p["warehouse"]

    # ---- Build INVEST and PROCEEDS (same shapes as before) ----
    invest = np.zeros(years + 1)
    total_companies = sum(b["count"] for b in buckets)
    total_invested = total_companies * check_size_m * followon
    if invest_years > 0 and total_invested > 0:
        annual_equity = -total_invested / invest_years
        for y in range(1, invest_years + 1):
            invest[y] += annual_equity
    if vb["enabled"] and vb["invested"] > 0 and invest_years > 0:
        vb_annual = -vb["invested"] / invest_years
        for y in range(1, invest_years + 1):
            invest[y] += vb_annual
    if wh["enabled"] and wh["cost_basis"] > 0:
        invest[0] += -wh["cost_basis"]

    proceeds = np.zeros(years + 1)
    for b in buckets:
        moic = b["avg_moic"] * demand
        hold = int(round(b["avg_hold_years"]))
        if downside:
            moic = min(moic, 3.0)
            hold = min(years, hold + 2)
        value = b["count"] * (check_size_m * followon) * moic
        proceeds[min(years, max(1, hold))] += value
    if vb["enabled"] and vb["invested"] > 0:
        proceeds[min(years, max(1, int(round(vb["hold_years"]))))] += (
            vb["invested"] * vb["moic"]
        )
    if wh["enabled"] and wh["market_value"] > 0:
        proceeds[min(years, 3)] += wh["market_value"] - wh["cost_basis"]

    # ---- Waterfall state ----
    contrib = 0.0  # outstanding capital (incl. fees) to accrue pref on
    total_called = 0.0  # all calls to date (invest + fees)
    pref_acc = 0.0  # accrued, unpaid preferred return
    total_pref_accrued = 0.0
    total_lp_dist = 0.0  # cumulative distributions to LP
    carry_paid_cum = 0.0

    gross_cf = np.zeros(years + 1)
    net_cf = np.zeros(years + 1)
    rows = []

    def fee_rate_for_year(y: int) -> float:
        return fee10 if 1 <= y <= 10 else (fee1511 if 11 <= y <= 15 else 0.0)

    def fee_base_for_year(y: int, called_so_far: float, contrib_start: float) -> float:
        basis = basis_invest if y <= invest_years else basis_post
        if basis == "committed":
            return fund_size_m
        if basis == "called":
            return called_so_far
        if basis == "nav":
            # NOTE: contrib_start includes fees. Many LPAs define NAV as invested
            # cost excluding fees. If your LPA specifies that, track a separate
            # invested_outstanding balance and return that instead.
            return contrib_start
        return fund_size_m

    for y in timeline:
        # Start-of-year state for fee base
        called_so_far = total_called
        contrib_start = contrib

        # ---- Fees for this year (charged at period y) ----
        r = fee_rate_for_year(y)
        base = fee_base_for_year(y, called_so_far, contrib_start)
        fee_y = -r * base  # outflow

        # ---- Calls this year (fees + invest[y]) ----
        calls_this_year = 0.0
        if fee_y < 0:
            calls_this_year += -fee_y
        if invest[y] < 0:
            calls_this_year += -invest[y]

        if calls_this_year > 0:
            contrib += calls_this_year
            total_called += calls_this_year
            net_cf[y] += -calls_this_year

        # ---- Distributions this year ----
        dist = proceeds[y]
        row = {
            "Year": y,
            "Calls": calls_this_year,
            "Dist_Principal": 0.0,
            "Dist_Pref": 0.0,
            "Dist_LP_AfterCarry": 0.0,
            "Dist_Carry": 0.0,
        }

        # 1) Return of capital
        if dist > 0 and contrib > 0:
            pay_prin = min(dist, contrib)
            dist -= pay_prin
            contrib -= pay_prin
            net_cf[y] += pay_prin
            row["Dist_Principal"] = pay_prin
            total_lp_dist += pay_prin

        # 2) Pay preferred return
        if dist > 0 and pref_acc > 0:
            pay_pref = min(dist, pref_acc)
            dist -= pay_pref
            pref_acc -= pay_pref
            net_cf[y] += pay_pref
            row["Dist_Pref"] = pay_pref
            total_lp_dist += pay_pref

        # 3) GP catch-up on cumulative profits above pref (include prior carry)
        if dist > 0:
            profits_above_pref = max(
                0.0,
                (total_lp_dist + carry_paid_cum + dist)
                - total_called
                - total_pref_accrued,
            )
            carry_should_be = carry * profits_above_pref
            new_carry = max(0.0, carry_should_be - carry_paid_cum)
            pay_carry = min(dist, new_carry)
            dist -= pay_carry
            carry_paid_cum += pay_carry
            row["Dist_Carry"] = pay_carry

        # 4) LP gets the remainder
        if dist > 0:
            net_cf[y] += dist
            row["Dist_LP_AfterCarry"] = dist
            total_lp_dist += dist

        # Gross CF bookkeeping (for display parity)
        gross_cf[y] = fee_y + invest[y] + proceeds[y]

        # 5) Accrue preferred return for next period on outstanding principal
        if y < years:
            pref_to_accrue = contrib * hurdle
            pref_acc += pref_to_accrue
            total_pref_accrued += pref_to_accrue

        rows.append(row)

    breakdown_df = pd.DataFrame(rows)

    # ---- Summaries (unchanged interface) ----
    pic_net = -net_cf[net_cf < 0].sum()
    dist_net = net_cf[net_cf > 0].sum()
    nav_net = max(0.0, -net_cf.cumsum()[-1])

    pic_gross = -gross_cf[gross_cf < 0].sum()
    dist_gross = gross_cf[gross_cf > 0].sum()
    nav_gross = max(0.0, -gross_cf.cumsum()[-1])

    _net_irr = safe_irr_pct(net_cf.tolist())
    summary = {
        "TVPI": (dist_net + nav_net) / pic_net if pic_net > 0 else 0.0,
        "DPI": dist_net / pic_net if pic_net > 0 else 0.0,
        "NetIRR": _net_irr,  # may be None
        "GrossTVPI": (dist_gross + nav_gross) / pic_gross if pic_gross > 0 else 0.0,
        "CarryPaid": carry_paid_cum,
    }

    main_df = pd.DataFrame(
        {
            "Year": timeline,
            "GrossCF": gross_cf,
            "NetCF": net_cf,
            "CumulativeNet": net_cf.cumsum(),
        }
    )

    # ---- Sanity checks (same spirit, adapted to new state) ----
    warnings: list[str] = []
    if summary["DPI"] > summary["TVPI"]:
        warnings.append(f"DPI ({summary['DPI']:.2f}) > TVPI ({summary['TVPI']:.2f})")

    total_calls_check = breakdown_df["Calls"].sum()
    total_principal_paid = breakdown_df["Dist_Principal"].sum()
    total_pref_paid = breakdown_df["Dist_Pref"].sum()
    total_lp_after_carry = breakdown_df["Dist_LP_AfterCarry"].sum()
    total_proceeds = (
        total_principal_paid + total_pref_paid + total_lp_after_carry + carry_paid_cum
    )

    profit_above_pref = total_proceeds - total_calls_check - total_pref_paid
    max_carry_allowed = max(0.0, carry * profit_above_pref)
    if carry_paid_cum > max_carry_allowed * 1.01:
        warnings.append(
            f"Carry {carry_paid_cum:.2f}M > {carry * 100:.0f}% of profits above pref {max_carry_allowed:.2f}M"
        )

    # No carry before capital + pref fully returned (cumulative)
    cum_calls = cum_prin = cum_pref = 0.0
    for _, r in breakdown_df.iterrows():
        cum_calls += r["Calls"]
        cum_prin += r["Dist_Principal"]
        cum_pref += r["Dist_Pref"]
        if r["Dist_Carry"] > 1e-2 and (cum_prin + cum_pref) + 1e-2 < cum_calls:
            warnings.append(
                f"Year {int(r['Year'])}: carry before capital+pref returned"
            )
            break

    irr_check = summary["NetIRR"] is not None and summary["NetIRR"] < -100
    if summary["TVPI"] < 0 or irr_check or summary["TVPI"] > 100:
        irr_str = (
            f"{summary['NetIRR']:.1f}%" if summary["NetIRR"] is not None else "N/A"
        )
        warnings.append(
            f"Unrealistic metrics: TVPI {summary['TVPI']:.2f}, IRR {irr_str}"
        )

    for w in warnings:
        print(f"⚠️ Waterfall Warning: {w}")

    return main_df, summary, breakdown_df


def create_validated_fund_params(state: UnifiedState) -> str:
    """
    Create validated fund parameters using Pydantic and orjson.

    Provides data validation and fast serialization for fund calculations.
    """
    try:
        # Validate parameters using Pydantic
        validated_params = state.validate_fund_parameters()

        # Create complete parameter dict
        d = {
            "fund_size_m": validated_params.fund_size_m,
            "invest_period_years": validated_params.invest_period_years,
            "harvest_years": validated_params.harvest_years,
            "fee_schedule": {
                "years_1_10": validated_params.fee_years_1_10,
                "years_11_15": validated_params.fee_years_11_15,
                "basis_invest_period": validated_params.fee_basis_invest_period,
                "basis_post_invest": validated_params.fee_basis_post_invest,
            },
            "carry_terms": {
                "carry": validated_params.carry,
                "hurdle": validated_params.hurdle,
            },
            "buckets": state.buckets_df.to_dict("records"),
            "check_size_m": state.check_size_m,
            "followon_multiplier": state.followon_multiplier,
            "demand_uplift": validated_params.climate_demand_uplift,
            "venture_builder": {
                "enabled": state.vb_enabled,
                "invested": state.vb_invested,
                "moic": state.vb_moic,
                "hold_years": state.vb_hold_years,
            },
            "warehouse": {
                "enabled": state.wh_enabled,
                "cost_basis": state.wh_cost_basis,
                "market_value": state.wh_market_value,
            },
            "downside_floor": state.downside_floor,
        }

        # Use orjson for fast serialization
        return orjson.dumps(d, option=orjson.OPT_SORT_KEYS).decode()

    except Exception as e:
        print(f"Warning: Parameter validation failed: {e}")
        # Fallback to original method
        return state.to_engine_json()


# =====================================================================================
# 3. DESIGN SYSTEM & DATA
#    - ADAPTUS brand color palette with CSS variables
#    - SECTORS: Climate adaptation market data (5 sectors, sources cited)
#    - TARGET_COMPANIES: Investment pipeline (12 companies with framework)
#    - FUND_COMPARISON: Competitive landscape vs other funds
# =====================================================================================


class SectorData(TypedDict):
    """Sector market data."""

    current: float
    future: float


# Climate adaptation market data (sources: GCA 2024, Market.us 2025, Grand View Research)
# Used for: Climate thesis visualization, market size projections, sector examples
SECTORS: Dict[str, SectorData] = {
    "Smart Water Mgmt": {"current": 19.0, "future": 62.0},
    "Cold Chain Logistics": {"current": 368.0, "future": 1300.0},
    "Personal Cooling": {"current": 14.0, "future": 25.0},
    "Glass Coatings": {"current": 2.0, "future": 8.0},
    "Sustainable Tourism": {"current": 4000.0, "future": 13000.0},
}

# Target companies with Pydantic validation
# Framework: Climate Stressor → Economic Pain → Specific Payer → Business Model → Climate Acceleration
# Categories: Water Stress (4), Heat Management (3), Food Security (3), Infrastructure (2)
_target_companies_data = [
    {
        "company": "Brekland",
        "website": "https://www.brekland.com",
        "stressor": "Late spring frost",
        "pain": "Crop loss, deductibles",
        "payer": "Orchard and specialty-crop growers",
        "model": "Seasonal consumable per acre via distributors",
        "acceleration": "More cold snaps around bloom raise loss odds",
    },
    {
        "company": "ConnectedFresh",
        "website": "https://www.connectedfresh.com",
        "stressor": "Heat waves, grid volatility in cold chains",
        "pain": "Spoilage, fines, energy waste",
        "payer": "Restaurants, groceries, processors, cold storage",
        "model": "Sensors plus SaaS compliance and alerts",
        "acceleration": "Hotter days and insurer demands shorten payback",
    },
    {
        "company": "Mexar",
        "website": "https://mexar.co",
        "stressor": "High-heat days, OSHA heat rule",
        "pain": "Injuries, claims, lost productivity",
        "payer": "Employers with outdoor workforces",
        "model": "Per-employee subscription, cooling gear plus compliance analytics",
        "acceleration": "More WBGT red-flag days make this compliance spend",
    },
    {
        "company": "Orca Water is Life",
        "website": "https://www.orcawater.life",
        "stressor": "Drought, rising water tariffs",
        "pain": "Non-revenue water, damage, penalties",
        "payer": "Property owners, campuses, utilities",
        "model": "Hardware plus SaaS, optional savings share",
        "acceleration": "Deeper drought raises marginal water cost",
    },
    {
        "company": "RCOAST",
        "website": "https://www.r-coast.com",
        "stressor": "Sea-level rise, storm surge, erosion",
        "pain": "Asset loss, higher premiums, project delays",
        "payer": "Municipalities, DOTs, ports, HOAs, insurers",
        "model": "Mapping and monitoring as a service, annual contracts",
        "acceleration": "More severe storms make yearly surveys mandatory",
    },
    {
        "company": "Senecio Robotics",
        "website": "https://www.senecio-robotics.com",
        "stressor": "Expanding mosquito ranges, outbreaks",
        "pain": "Public health costs, tourism hits",
        "payer": "Health ministries, cities, vector districts",
        "model": "SIT as a service, build-operate facilities and releases",
        "acceleration": "Longer breeding seasons improve SIT economics",
    },
    {
        "company": "SmartAgri Labs",
        "website": "https://smartagrilabs.com",
        "stressor": "Weather volatility, hybrid mismatch",
        "pain": "Yield loss, wasted inputs",
        "payer": "Growers, ag retailers, seed distributors",
        "model": "SaaS per farm or per acre, embedded recs",
        "acceleration": "More variability increases value of local picks",
    },
    {
        "company": "Sunphade",
        "website": "https://sunphade.com",
        "stressor": "Heat and glare, rising cooling loads",
        "pain": "HVAC costs, comfort complaints",
        "payer": "Schools, commercial building owners",
        "model": "Materials sale plus install, warranty, service",
        "acceleration": "More cooling-degree days compress paybacks",
    },
    {
        "company": "ThermoShade",
        "website": "https://getthermoshade.com",
        "stressor": "Unsafe outdoor heat",
        "pain": "Liability, unusable outdoor areas",
        "payer": "Schools, cities, multifamily, hospitality",
        "model": "Product plus install and service, potential OpEx contracts",
        "acceleration": "More extreme heat turns shade into required spend",
    },
    {
        "company": "Huma (ag inputs)",
        "website": "https://huma.us",
        "stressor": "Drought, salinity, heat stress",
        "pain": "Reduced yields, fertilizer efficiency loss",
        "payer": "Growers via input budgets",
        "model": "Recurring consumables per acre",
        "acceleration": "Tighter water makes biostimulants must-have",
    },
    {
        "company": "HydroHammer",
        "website": "https://www.hydrohammer.co.uk",
        "stressor": "Unreliable power, variable flows",
        "pain": "Fuel and pump OPEX, water access gaps",
        "payer": "Farms, rural water schemes, NGOs",
        "model": "Equipment plus service, fuel-savings payback",
        "acceleration": "More outages and fuel costs improve ROI",
    },
    {
        "company": "Undesert",
        "website": "https://www.undesert.com",
        "stressor": "Water scarcity, brine disposal limits",
        "pain": "No irrigation water, disposal fees",
        "payer": "Municipalities, industry, ag in arid regions",
        "model": "Systems plus water-as-a-service, project finance",
        "acceleration": "Prolonged drought makes reuse essential",
    },
]

# Validate and create TARGET_COMPANIES with Pydantic for data integrity
TARGET_COMPANIES = [
    CompanyData(**company).model_dump() for company in _target_companies_data
]

# Competitive landscape analysis for fund positioning
# Used in: Competitive view for "Why Adapt[us]?" differentiation
# Compares: Thesis, stage, support model, fees, track record, differentiation
FUND_COMPARISON = {
    "Adapt[us]": {
        "thesis": "Climate Adaptation",
        "stage": "Pre-seed to A",
        "support": "Fund + Builder",
        "fee_carry": "2%/1.5% | 20%",
        "track_record": "7x, 51% IRR",
        "differentiation": "Only adaptation fund w/ builder. 2.5°C+ base.",
    },
    "Lowercarbon Capital": {
        "thesis": "Mitigation",
        "stage": "Pre-seed to Seed",
        "support": "Sci/Ops",
        "fee_carry": "2% | 20%",
        "track_record": "Undisclosed",
        "differentiation": "Carbon-neg, Gates backing",
    },
    "Breakthrough Energy": {
        "thesis": "Mitigation",
        "stage": "Early to Growth",
        "support": "Tech/Policy",
        "fee_carry": "Undisclosed",
        "track_record": "Impact-first",
        "differentiation": "Deep-tech, patient cap",
    },
    "Sequoia Capital": {
        "thesis": "Tech Growth",
        "stage": "Seed to Growth",
        "support": "Network/Adv",
        "fee_carry": "2.5% | 25%",
        "track_record": "30%+ IRR",
        "differentiation": "Tier-1 brand, unicorns",
    },
}


# =====================================================================================
# 4. UI COMPONENTS
#    Organized by function: Hero → Analysis Views → Technical Views
#    All components use @pn.depends for reactivity and ADAPTUS theme
# =====================================================================================

# --- Hero & Branding Components ---


def create_lp_hero_section():
    """LP hero with thesis, track record, and unique positioning."""
    return pmui.Markdown(
        """
        ## <i class="fas fa-globe-americas adaptus-icon"></i>$9T Climate Adaptation Opportunity

        **2.5°C+ warming is inevitable.** We invest in businesses people **must buy** as climate volatility rises.

        **Track**: 7x, 51% IRR | **Fund + Builder** | **Target**: 40% A→B grad

        *Adaptation gets ~5% of climate finance. We're changing that.*
        """,
        styles={
            "background": f"linear-gradient(135deg, {COLORS['primary']} 0%, {COLORS['secondary']} 50%, {COLORS['accent']} 100%)",
            "color": "white",
            "padding": "20px",
            "border-radius": "8px",
            "margin-bottom": "15px",
            "font-size": "16px",
            "text-align": "center",
        },
    )


# --- Core Analysis Views ---


def create_climate_view():
    """Interactive climate thesis with temperature-driven market projections."""

    @pn.depends(state.param.temperature)
    def _view(temp):
        """Climate view by temperature."""
        mult = state.climate_demand_uplift  # Use the property for consistency
        names = list(SECTORS.keys())
        current = [d["current"] for d in SECTORS.values()]
        future = [d["future"] for d in SECTORS.values()]
        projected = [c + (f - c) * mult for c, f in zip(current, future)]

        fig = go.Figure()
        fig.add_trace(
            go.Bar(
                name="2024 Market", x=names, y=current, marker_color=COLORS["accent"]
            )
        )
        fig.add_trace(
            go.Bar(
                name=f"Projected @ {temp:.1f}°C",
                x=names,
                y=projected,
                marker_color=COLORS["primary"],
            )
        )
        fig.update_layout(
            barmode="group",
            title="Adaptation Market Growth vs. Climate Scenario",
            yaxis_title="Market Size ($B)",
            height=350,
            template="adaptus",  # Use custom Adaptus theme
        )

        return pmui.Column(
            pmui.Markdown(
                f"""
            ## Climate-Proof Thesis
            **2.5°C+ warming drives predictable demand.**
            Market: $1.4T → $9.0T (2024-2050).
            > **Uplift: {mult:.2f}x** | **Projected: ${sum(projected):,.0f}B**
            """,
                styles={
                    "background": COLORS["surface"],
                    "padding": "15px",
                    "border-radius": "8px",
                    "border": f"1px solid {COLORS['accent']}",
                },
            ),
            pn.pane.Plotly(fig, sizing_mode="stretch_width", config=PLOTLY_CONFIG),
        )

    return _view


def create_pipeline_view():
    """Pipeline with 12 targets using Stressor→Pain→Payer→Model→Acceleration."""
    pipeline_df = pd.DataFrame(TARGET_COMPANIES)

    # Prepare display columns with professional URL column
    display_df = pipeline_df[
        ["company", "website", "stressor", "pain", "payer", "model", "acceleration"]
    ].copy()
    display_df["Site"] = display_df["website"].apply(
        lambda x: f'<a href="{x}" target="_blank"><i class="fas fa-external-link-alt" style="color: {COLORS["primary"]}"></i></a>'
    )
    display_df = display_df[
        ["company", "Site", "stressor", "pain", "payer", "model", "acceleration"]
    ]
    display_df.columns = [
        "Company",
        "url",
        "Climate Stressor",
        "Economic Pain",
        "Payer",
        "Business Model",
        "Climate Acceleration",
    ]

    return pmui.Column(
        pmui.Markdown(
            """
        ## <i class="fas fa-bullseye adaptus-icon"></i>Pipeline: Thesis in Practice

        **12 targets:** Stressor → Pain → Payer → Model → Acceleration
        """,
            styles={"font-size": "18px", "font-weight": "bold"},
        ),
        # Three Playbooks Framework from pitch deck
        pmui.Row(
            pmui.Markdown(
                """
            ### 📋 A. Resilience Infrastructure
            Real climate causality, infra-like returns, longer paybacks
            *Examples: flood defense, water reuse*
            """,
                styles={
                    "background": COLORS["surface"],
                    "padding": "12px",
                    "border-radius": "6px",
                    "border": f"1px solid {COLORS['success']}",
                },
            ),
            pmui.Markdown(
                """
            ### 🚀 B. Demand Shifts (Venture)
            Clear stressor→payer link, venture upside
            *Examples: NRW SaaS, cold-chain telemetry, heat safety*
            """,
                styles={
                    "background": COLORS["surface"],
                    "padding": "12px",
                    "border-radius": "6px",
                    "border": f"1px solid {COLORS['warn']}",
                },
            ),
            pmui.Markdown(
                """
            ### 🔧 C. Builder Capability (Orthogonal)
            Measured interventions to compress time-to-milestone
            *We only "build where we invest"*
            """,
                styles={
                    "background": COLORS["surface"],
                    "padding": "12px",
                    "border-radius": "6px",
                    "border": f"1px solid {COLORS['accent']}",
                },
            ),
            sizing_mode="stretch_width",
        ),
        pn.widgets.Tabulator(
            display_df,
            disabled=True,
            show_index=False,
            sizing_mode="stretch_width",  # Make table expand to full width
            height=520,  # Increased height to show all 12 companies without scrolling
            pagination=None,  # No pagination - show all companies
            # No theme specified - use default for better contrast
            configuration={
                "layout": "fitColumns",  # Fit columns to available width
                "height": "auto",  # Auto-size height based on content
                "maxHeight": 520,  # Maximum height before scrolling
                "columnDefaults": {
                    "headerSort": False,
                    "resizable": True,
                    "tooltip": True,  # Show full text on hover
                },
                "columns": [
                    {"field": "Company", "minWidth": 120, "widthGrow": 1},
                    {
                        "field": "url",
                        "width": 70,
                        "formatter": "html",
                        "widthGrow": 0,
                        "headerSort": False,
                    },
                    {"field": "Climate Stressor", "minWidth": 150, "widthGrow": 2},
                    {"field": "Economic Pain", "minWidth": 150, "widthGrow": 2},
                    {"field": "Payer", "minWidth": 160, "widthGrow": 2},
                    {"field": "Business Model", "minWidth": 180, "widthGrow": 3},
                    {"field": "Climate Acceleration", "minWidth": 180, "widthGrow": 3},
                ],
            },
            styles={
                "background": COLORS["surface"],
                "border": f"2px solid {COLORS['primary']}",
                "border-radius": "8px",
            },
        ),
        pmui.Row(
            # Column 1: Climate Categories
            pmui.Markdown(
                """
            ### <i class="fas fa-thermometer-half adaptus-icon"></i>Climate Categories<br>
            **Water Stress**: Brekland, Orca, Undesert, HydroHammer<br>
            **Heat Management**: Mexar, Sunphade, ThermoShade<br>
            **Food Security**: ConnectedFresh, SmartAgri, Huma<br>
            **Infrastructure**: RCOAST, Senecio Robotics
            """,
                styles={
                    "background": COLORS["surface"],
                    "padding": "15px",
                    "border-radius": "8px",
                    "border": f"1px solid {COLORS['accent']}",
                },
            ),
            # Column 2: Business Model Mix
            pmui.Markdown(
                """
            ### <i class="fas fa-dollar-sign adaptus-icon"></i>Business Model Mix
            **SaaS/Subscription**: 50% (recurring revenue)
            **Hardware + Service**: 33% (sticky recurring)
            **Consumables**: 17% (repeat purchase)

            **Pipeline Quality**: Clear payer ID, budget authority, measurable ROI
            """,
                styles={
                    "background": COLORS["surface"],
                    "padding": "15px",
                    "border-radius": "8px",
                    "border": f"1px solid {COLORS['accent']}",
                },
            ),
            # Column 3: Scoring Framework
            pmui.Markdown(
                """
            ### <i class="fas fa-chart-bar adaptus-icon"></i>Scoring Framework

            **Systematic evaluation of each company:**

            - **Stressor/threshold**: 20%
            - **Payer/budget clarity**: 25%
            - **Margin path ≥50%**: 20%
            - **Capex/payback**: 10%
            - **GTM wedge**: 10%
            - **Regulator/insurer pull**: 10%
            - **Execution risk**: 5%

            **Target**: 40% A→B graduation vs 21% industry average
            """,
                styles={
                    "background": COLORS["surface"],
                    "padding": "15px",
                    "border-radius": "8px",
                    "border": f"1px solid {COLORS['accent']}",
                },
            ),
            sizing_mode="stretch_width",
        ),
        # Investment Framework Summary
        pmui.Markdown(
            """
        ### 🔬 Investment Framework Summary

        Every company demonstrates our core thesis: **businesses people must buy as climate volatility increases**.

        - **Not hope-to-buy** (mitigation/carbon credits) | **Not nice-to-buy** (sustainability) | **Must-buy under stress**

        **Result**: Predictable demand acceleration + systematic evaluation = superior risk-adjusted returns.
        """,
            styles={
                "background": COLORS["surface"],
                "padding": "15px",
                "border-radius": "8px",
                "border": f"2px solid {COLORS['primary']}",
                "text-align": "center",
            },
        ),
    )


def create_returns_view():
    """
    Create personalized LP returns analysis.

    Core LP decision-making view featuring:
    - Investment amount input with real-time updates
    - Co-investment rights toggle for additional upside
    - Personal return metrics (TVPI, IRR, total proceeds)
    - J-curve visualization vs S&P 500 benchmark
    - European waterfall calculations for LP's exact share

    Calculates the LP's proportional share of fund returns based on
    their commitment relative to the total fund size.

    Returns:
        pn.Row: Two-column layout with controls and analysis
    """

    @pn.depends(
        state.param.temperature, state.param.lp_investment, state.param.coinvest_enabled
    )
    def _view(temperature, lp_investment, coinvest):
        """Calculate and display personalized LP returns and J-curve."""
        main_df, summary, breakdown = compute_fund_metrics(state.to_engine_json())
        share = (
            (lp_investment / (state.fund_size_m * 1_000_000))
            if state.fund_size_m > 0
            else 0.0
        )

        lp_calls = breakdown["Calls"] * share
        lp_dists = (
            breakdown["Dist_Principal"]
            + breakdown["Dist_Pref"]
            + breakdown["Dist_LP_AfterCarry"]
        ) * share

        # Calculate net LP cashflows (negative calls, positive distributions)
        lp_net_annual = lp_dists - lp_calls

        # Co-investment model - integrate into cashflows
        total_inv = lp_investment
        co_amt = 0.0
        coinvest_exit_year = 5  # Co-invest typically exits around year 5

        if coinvest:
            co_amt = lp_investment * 0.2
            total_inv += co_amt

            # Add co-investment cashflows to the LP's annual cashflows
            # Convert co_amt from dollars to millions to match lp_net_annual units
            co_amt_m = co_amt / 1_000_000

            # Investment in year 0 (negative cashflow)
            lp_net_annual_with_coinvest = lp_net_annual.copy()
            lp_net_annual_with_coinvest.iloc[0] -= co_amt_m

            # Exit in year 5 (positive cashflow at 8x)
            if len(lp_net_annual_with_coinvest) > coinvest_exit_year:
                lp_net_annual_with_coinvest.iloc[coinvest_exit_year] += co_amt_m * 8.0
        else:
            lp_net_annual_with_coinvest = lp_net_annual

        # Calculate LP's personal IRR and TVPI from actual cashflows (flow-true)
        lp_net_cashflows = lp_net_annual_with_coinvest.tolist()
        lp_irr = (
            irr(lp_net_cashflows) * 100 if not np.isnan(irr(lp_net_cashflows)) else 0.0
        )

        # Flow-true TVPI from actual LP cashflows (not weighted blend)
        neg_cf = -sum(cf for cf in lp_net_cashflows if cf < 0)
        pos_cf = sum(cf for cf in lp_net_cashflows if cf > 0)
        lp_tvpi = (pos_cf / neg_cf) if neg_cf > 0 else 0.0

        # Calculate cumulative cashflows (for J-curve)
        lp_cum = lp_net_annual_with_coinvest.cumsum()
        total_proceeds = neg_cf * lp_tvpi * 1_000_000  # Convert M back to $

        # Results card with LP-specific metrics
        results_card = pmui.Card(
            pmui.Row(
                pn.indicators.Number(
                    name="Your Investment",
                    value=total_inv,
                    format="${value:,.0f}",
                    font_size="18pt",
                ),
                pn.indicators.Number(
                    name="Expected Proceeds",
                    value=total_proceeds,
                    format="${value:,.0f}",
                    font_size="18pt",
                ),
                pn.indicators.Number(
                    name="Your Multiple",
                    value=lp_tvpi,
                    format="{value:.2f}x",
                    font_size="18pt",
                ),
                pn.indicators.Number(
                    name="Your IRR",
                    value=lp_irr,
                    format="{value:.1f}%",
                    font_size="18pt",
                ),
            ),
            title="Your Personal Return Summary",
            header_background=COLORS["primary"],
            header_color="white",
        )

        # Enhanced J-curve vs S&P 500 calculation
        years = breakdown["Year"].to_numpy()

        # S&P 500: Lump sum investment that grows at 10% annually
        sp_initial_investment = total_inv  # Use total investment (including co-invest)
        sp_cumulative = np.array(
            [
                sp_initial_investment * ((1.10**y) - 1)
                if y > 0
                else -sp_initial_investment
                for y in years
            ]
        )

        fig = go.Figure()

        # Add LP returns line with debugging
        # Note: lp_cum is in millions, convert to thousands for chart display
        fig.add_trace(
            go.Scatter(
                x=years,
                y=lp_cum * 1000,  # Convert millions to thousands (M → K)
                name="Adapt[us]",
                line=dict(color=COLORS["primary"], width=4),
                fill="tozeroy",
                hovertemplate="Year: %{x}<br>Value: $%{y:.0f}K<extra></extra>",
            )
        )

        # Add S&P 500 comparison
        fig.add_trace(
            go.Scatter(
                x=years,
                y=sp_cumulative
                / 1000,  # sp_cumulative is in dollars, convert to thousands
                name="S&P 500 (10%/year)",
                line=dict(color=COLORS["muted"], dash="dash", width=3),
                hovertemplate="Year: %{x}<br>Value: $%{y:.0f}K<extra></extra>",
            )
        )

        fig.add_hline(
            y=0,
            line_dash="dash",
            line_color=COLORS["muted"],
            annotation_text="Breakeven",
        )
        # Dynamic title based on co-investment
        chart_title = "Your Investment J-Curve vs S&P 500"
        if coinvest:
            chart_title += f" (Fund + ${co_amt:,.0f} Co-invest)"

        fig.update_layout(
            title=chart_title,
            yaxis_title="Cumulative Value ($K)",
            height=400,
            hovermode="x unified",
            template="adaptus",
        )

        return pmui.Column(
            results_card,
            pn.pane.Plotly(fig, sizing_mode="stretch_width", config=PLOTLY_CONFIG),
        )

    return _view


# --- Technical Analysis Views ---


def create_fund_modeling_view():
    """
    Create comprehensive fund modeling view for analysts.

    Features complete control over fund parameters:
    - Scenario presets (Cautious/Base/Aggressive)
    - Fund structure controls (size, timeline, fees)
    - Portfolio construction (check size, follow-on, success rates)
    - Venture builder economics (debt, equity, conversion terms)
    - Editable portfolio buckets (Tabulator interface)
    - Real-time European waterfall calculations

    This view exposes all the underlying assumptions that drive
    fund performance for detailed due diligence analysis.

    Returns:
        pn.Row: Controls sidebar and analysis dashboard
    """
    # Scenario presets with Material UI
    presets = pmui.RadioButtonGroup(
        name="Scenario Presets",
        options=["Cautious", "Base", "Aggressive"],
        value="Base",
    )

    @pn.depends(presets.param.value, watch=True)
    def on_preset(preset) -> None:
        """
        Update portfolio assumptions based on selected scenario preset.

        Preset scenarios:
        - Cautious: Conservative returns, more losers (downside case)
        - Base: Current default assumptions (base case)
        - Aggressive: Higher returns, more winners (upside case)
        """
        if preset == "Cautious":
            state.demand_uplift = 0.9
            state.buckets_df = pd.DataFrame(
                [
                    {
                        "name": "Big",
                        "count": 1,
                        "avg_moic": 20.0,
                        "avg_hold_years": 11.0,
                    },
                    {"name": "Mod", "count": 3, "avg_moic": 6.0, "avg_hold_years": 9.0},
                    {
                        "name": "Small",
                        "count": 2,
                        "avg_moic": 3.0,
                        "avg_hold_years": 9.0,
                    },
                    {
                        "name": "Loss",
                        "count": 10,
                        "avg_moic": 0.1,
                        "avg_hold_years": 4.0,
                    },
                ]
            )
        elif preset == "Aggressive":
            state.demand_uplift = 1.15
            state.buckets_df = pd.DataFrame(
                [
                    {
                        "name": "Big",
                        "count": 3,
                        "avg_moic": 30.0,
                        "avg_hold_years": 9.0,
                    },
                    {"name": "Mod", "count": 3, "avg_moic": 9.0, "avg_hold_years": 8.0},
                    {
                        "name": "Small",
                        "count": 2,
                        "avg_moic": 4.0,
                        "avg_hold_years": 8.0,
                    },
                    {
                        "name": "Loss",
                        "count": 8,
                        "avg_moic": 0.3,
                        "avg_hold_years": 3.0,
                    },
                ]
            )
        else:  # Base
            state.demand_uplift = 1.0
            state.buckets_df = UnifiedState.param.buckets_df.default

    # Portfolio bucket editor with Adaptus styling
    bucket_editor = pn.widgets.Tabulator(
        value=state.buckets_df,
        show_index=False,
        sizing_mode="stretch_width",
        editors={
            "name": None,
            "count": {"type": "number", "min": 0},
            "avg_moic": {"type": "number", "min": 0},
            "avg_hold_years": {"type": "number", "min": 1},
        },
        height=200,
        layout="fit_columns",
        configuration={"layout": "fitColumns", "columnDefaults": {"resizable": True}},
        styles={
            "background": COLORS["surface"],
            "border": f"2px solid {COLORS['primary']}",
            "border-radius": "8px",
        },
    )
    bucket_editor.link(state, value="buckets_df")

    # Fund controls with Material UI components
    controls = pmui.Column(
        presets,
        pmui.Accordion(
            (
                "Fund Structure",
                pmui.Column(
                    pmui.FloatSlider.from_param(
                        state.param.fund_size_m, name="Fund Size ($M)"
                    ),
                    pmui.IntSlider.from_param(
                        state.param.invest_period_years,
                        name="Investment Period (Years)",
                    ),
                    pmui.IntSlider.from_param(
                        state.param.harvest_years, name="Harvest Period (Years)"
                    ),
                ),
            ),
            (
                "Fees & Carry",
                pmui.Column(
                    pmui.FloatSlider.from_param(
                        state.param.fee_years_1_10, name="Management Fee (Years 1-10)"
                    ),
                    pmui.FloatSlider.from_param(
                        state.param.fee_years_11_15, name="Management Fee (Years 11-15)"
                    ),
                    pmui.FloatSlider.from_param(state.param.carry, name="Carry Rate"),
                    pmui.FloatSlider.from_param(state.param.hurdle, name="Hurdle Rate"),
                ),
            ),
            (
                "Portfolio",
                pmui.Column(
                    pmui.FloatSlider.from_param(
                        state.param.check_size_m, name="Check Size ($M)"
                    ),
                    pmui.FloatSlider.from_param(
                        state.param.followon_multiplier, name="Follow-on Multiplier"
                    ),
                    pmui.FloatSlider.from_param(
                        state.param.demand_uplift, name="Demand Uplift"
                    ),
                    pmui.Switch.from_param(
                        state.param.downside_floor, name="Downside Floor"
                    ),
                    pmui.Markdown("**Portfolio Buckets**"),
                    bucket_editor,
                ),
            ),
            (
                "Venture Builder",
                pmui.Column(
                    pmui.Switch.from_param(
                        state.param.vb_enabled, name="Venture Builder Enabled"
                    ),
                    pmui.FloatSlider.from_param(
                        state.param.vb_invested, name="VB Investment ($M)"
                    ),
                    pmui.FloatSlider.from_param(
                        state.param.vb_moic, name="VB Expected MOIC"
                    ),
                    pmui.Divider(),
                    pmui.Switch.from_param(
                        state.param.wh_enabled, name="Warehouse Enabled"
                    ),
                    pmui.FloatSlider.from_param(
                        state.param.wh_cost_basis, name="Warehouse Cost ($M)"
                    ),
                    pmui.FloatSlider.from_param(
                        state.param.wh_market_value, name="Warehouse Value ($M)"
                    ),
                ),
            ),
            active=[0, 2],
            toggle=True,
        ),
        width=350,
    )

    # Analysis view
    @pn.depends(
        state.param.temperature, state.param.fund_size_m, state.param.demand_uplift
    )
    def _analysis(temperature, fund_size, demand_uplift):
        """Generate fund performance analysis with KPIs and visualizations."""
        main_df, summary, breakdown = compute_fund_metrics(state.to_engine_json())

        # KPI indicators
        # IRR handling: show N/A if not yet defined
        ni = summary["NetIRR"]
        net_irr_name = "Net IRR" if ni is not None else "Net IRR (N/A yet)"
        net_irr_val = ni if ni is not None else 0
        net_irr_fmt = "{value:.1f}%" if ni is not None else ""

        kpis = pmui.Row(
            pn.indicators.Number(
                name="Net TVPI",
                value=summary["TVPI"],
                format="{value:.2f}x",
                font_size="18pt",
            ),
            pn.indicators.Number(
                name=net_irr_name,
                value=net_irr_val,
                format=net_irr_fmt,
                font_size="18pt",
            ),
            pn.indicators.Number(
                name="DPI",
                value=summary["DPI"],
                format="{value:.2f}x",
                font_size="18pt",
            ),
            pn.indicators.Number(
                name="Carry Paid",
                value=summary["CarryPaid"],
                format="${value:.1f}M",
                font_size="18pt",
            ),
        )

        # Cashflow chart with Adaptus styling
        fig_cf = go.Figure(
            go.Bar(
                x=main_df["Year"], y=main_df["NetCF"], marker_color=COLORS["primary"]
            )
        )
        fig_cf.update_layout(
            title="Net Cash Flow to LPs ($M)",
            height=280,
            yaxis_title="$M",
            template="adaptus",
        )

        # Cumulative chart with Adaptus styling
        fig_cum = go.Figure(
            go.Scatter(
                x=main_df["Year"],
                y=main_df["CumulativeNet"],
                mode="lines+markers",
                line=dict(color=COLORS["secondary"], width=3),
            )
        )
        fig_cum.add_hline(y=0, line_dash="dash", line_color=COLORS["muted"])
        fig_cum.update_layout(
            title="Cumulative Net Cash Flow (J-Curve)",
            height=280,
            yaxis_title="$M",
            template="adaptus",
        )

        # DPI Bridge Table from pitch deck
        dpi_bridge_data = [
            {
                "Scenario": "Down-case",
                "Gross MOIC": "2.5×",
                "Mgmt Fees": "(0.35×)",
                "Builder Line Net to LPs": "+0.10×",
                "Carry": "(0.18×)",
                "Net DPI": "2.07×",
            },
            {
                "Scenario": "Base",
                "Gross MOIC": "3.5×",
                "Mgmt Fees": "(0.35×)",
                "Builder Line Net to LPs": "+0.20×",
                "Carry": "(0.27×)",
                "Net DPI": "3.08×",
            },
            {
                "Scenario": "Up-case",
                "Gross MOIC": "4.0×",
                "Mgmt Fees": "(0.35×)",
                "Builder Line Net to LPs": "+0.25×",
                "Carry": "(0.31×)",
                "Net DPI": "3.59×",
            },
        ]
        dpi_bridge_df = pd.DataFrame(dpi_bridge_data)

        return pmui.Column(
            kpis,
            pmui.Row(
                pn.pane.Plotly(fig_cf, config=PLOTLY_CONFIG),
                pn.pane.Plotly(fig_cum, config=PLOTLY_CONFIG),
            ),
            pmui.Markdown(
                "### Gross→Net DPI Bridge (Illustrative)",
                styles={"font-weight": "bold", "margin-top": "20px"},
            ),
            pn.widgets.Tabulator(
                dpi_bridge_df,
                show_index=False,
                disabled=True,
                sizing_mode="stretch_width",
                height=150,
                configuration={
                    "layout": "fitColumns",
                    "columnDefaults": {"headerSort": False},
                },
                styles={
                    "background": COLORS["surface"],
                    "border": f"2px solid {COLORS['primary']}",
                    "border-radius": "8px",
                },
            ),
            sizing_mode="stretch_width",
        )

    return pmui.Row(controls, _analysis, sizing_mode="stretch_width")


def create_impact_view():
    """
    Create impact analysis showing dual financial + climate returns.

    Demonstrates the adaptation advantage with:
    - Lives improved calculation using eQALY methodology
    - Market impact from climate acceleration
    - Climate hedge matrix comparing adaptation vs mitigation funds
    - Fund track record in context of impact + returns

    Key message: Unlike mitigation funds that fight climate change,
    adaptation funds benefit from its acceleration, creating a
    natural hedge against climate risk.

    Returns:
        function: Panel depends function that updates with investment parameters
    """

    @pn.depends(state.param.temperature, state.param.lp_investment)
    def _view(temperature, lp_investment):
        """Generate impact analysis content with climate hedge matrix."""
        mult = state.climate_demand_uplift
        current = [d["current"] for d in SECTORS.values()]
        future = [d["future"] for d in SECTORS.values()]
        projected = [c + (f - c) * mult for c, f in zip(current, future)]
        total_new_market = sum(p - c for p, c in zip(projected, current))

        _, summary, _ = compute_fund_metrics(state.to_engine_json())

        # Impact calculations
        revenue_per_dollar = 3.0
        lives_per_revenue = 0.15
        total_lives = lp_investment * revenue_per_dollar * lives_per_revenue / 10.0

        # IRR display handling
        irr_display = (
            f"{summary['NetIRR']:.1f}% IRR"
            if summary["NetIRR"] is not None
            else "IRR N/A yet"
        )

        return pmui.Markdown(
            f"""
        # Climate Impact Investment Analysis

        ## Fund + Builder Model Impact
        **Your Investment**: ${lp_investment:,.0f} in climate adaptation
        **Expected Multiple**: {summary["TVPI"]:.2f}x ({irr_display})
        **Lives Improved**: {total_lives:,.0f} people (eQALY methodology)

        ## Market Impact at {temperature:.1f}°C
        **Market Acceleration**: {mult:.2f}x faster growth than baseline
        **New Market Creation**: +${total_new_market:.0f}B across 5 adaptation sectors

        ---

        ### <i class="fas fa-bullseye adaptus-icon"></i>The Adaptation Advantage: Climate Hedge Matrix

        | Scenario | Mitigation Funds | **Adaptation Funds** |
        |----------|------------------|---------------------|
        | Low warming (1.5-2.0°C) | Rebounds | **Solid returns** |
        | High warming (2.5-3.5°C) | Slower policy, lags | **Out-performance** |

        **Key Insight**: Unlike mitigation funds that **fight** climate change,
        adaptation funds **benefit** from its acceleration, creating a natural hedge.

        ---

        ### 🚫 What We Won't Do (Investment Discipline)

        - Pure carbon-credit arbitrage
        - Heavy capex without service spine
        - Consumer "green premium" plays
        - Policy-only demand with fragile incentives

        **Focus**: Only businesses with clear stressor→payer causality and budget authority.
        """,
            styles={
                "background": COLORS["surface"],
                "padding": "20px",
                "border-radius": "10px",
                "border": f"1px solid {COLORS['accent']}",
            },
        )

    return _view


def create_competitive_view():
    """
    Create competitive analysis and fund positioning view.

    Shows why Adapt[us] is uniquely positioned with:
    - Comparative analysis table vs other climate + tech funds
    - Key differentiators (2.5°C base case, Fund + Builder model)
    - Track record and proven operator credentials
    - Fund terms and structure details

    This view helps investors understand the competitive landscape
    and why Adapt[us] has defensible market positioning.

    Returns:
        pn.Column: Static competitive analysis with tables and differentiators
    """
    comparison_df = pd.DataFrame.from_dict(FUND_COMPARISON, orient="index")

    return pmui.Column(
        pmui.Markdown(
            """
        ## Why Adapt[us]?
        """,
            styles={"font-size": "18px", "font-weight": "bold"},
        ),
        pn.widgets.Tabulator(
            comparison_df,
            disabled=True,
            show_index=False,
            sizing_mode="stretch_width",
            layout="fit_data_stretch",
            height=200,
            configuration={
                "layout": "fitColumns",
                "columnDefaults": {"headerSort": False, "resizable": True},
            },
            styles={
                "background": COLORS["surface"],
                "border": f"2px solid {COLORS['primary']}",
                "border-radius": "8px",
            },
        ),
        pmui.Row(
            pmui.Markdown(
                """
            ### Key Differentiators
            - **2.5°C+ Base Case**: We invest where demand grows with inevitable warming
            - **Fund + Builder**: Only back what we can help build (40% Series B rate vs 21%)
            - **Climate Hedge**: Portfolio out-performs in high warming scenarios
            - **Proven Operators**: Darren Clifford (ex-McKinsey), 20+ year track record
            """,
                styles={
                    "background": COLORS["surface"],
                    "padding": "15px",
                    "border-radius": "8px",
                    "border": f"1px solid {COLORS['accent']}",
                },
            ),
            pmui.Markdown(
                """
            ### Fund Terms & Timeline
            - **Size**: $30M (Pre-seed to Series A)
            - **Term**: 15 years + two 2-year extensions
            - **Fees**: 2% (years 1-10), 1.5% (extensions)
            - **Carry**: 20% over 8% hurdle (European waterfall)
            - **Minimum**: $50K, accredited only
            - **First Close**: ≥$10M by March 31, 2026
            """,
                styles={
                    "background": COLORS["surface"],
                    "padding": "15px",
                    "border-radius": "8px",
                    "border": f"1px solid {COLORS['accent']}",
                },
            ),
            sizing_mode="stretch_width",
        ),
    )


# =====================================================================================
# 5. URL STATE MANAGEMENT
#    - Scenario sharing via URL query parameters
#    - Automatic state persistence and restoration
#    - Enables bookmarking and link sharing of specific configurations
# =====================================================================================


def setup_url_persistence():
    """
    Setup URL state persistence for scenario sharing and bookmarking.

    Enables users to:
    - Share specific scenarios via URL
    - Bookmark interesting parameter combinations
    - Persist state across browser sessions
    - Deep-link to specific fund configurations

    Watches all state parameters and encodes non-default values
    in the URL query string. On page load, restores state from URL.
    """

    def _load_url_state() -> None:
        """
        Load parameter values from URL query string on page load.

        Parses query parameters and restores state for scenario sharing.
        Handles buckets_df JSON deserialization and type conversion.
        """
        if not pn.state.location:
            return
        qs = dict(pn.state.location.query_params)
        if "buckets_df" in qs:
            try:
                # Use orjson for faster JSON parsing
                buckets_data = orjson.loads(qs.pop("buckets_df"))
                state.buckets_df = pd.DataFrame(buckets_data)
            except (ValueError, KeyError, orjson.JSONDecodeError):
                pass
        for k, v in qs.items():
            if not hasattr(state, k):
                continue
            try:
                cur_val = getattr(state, k)
                if isinstance(cur_val, bool):
                    setattr(state, k, v.lower() in ("true", "1"))
                elif isinstance(cur_val, (int, np.integer)):
                    setattr(state, k, int(v))
                elif isinstance(cur_val, float):
                    setattr(state, k, float(v))
                else:
                    setattr(state, k, v)
            except (ValueError, TypeError, AttributeError):
                continue

    def _save_url_state(*events) -> None:
        """
        Save current parameter values to URL query string when state changes.

        Watches for parameter changes and updates browser URL with non-default
        values. Enables scenario sharing and bookmarking.
        """
        if not pn.state.location:
            return
        from urllib.parse import urlencode

        defaults = UnifiedState()
        out: dict[bytes | str, Sequence[object]] = {}
        for name in state.param:
            if name.startswith("_") or name == "name":
                continue
            val = getattr(state, name)
            if name == "buckets_df":
                if not val.equals(defaults.buckets_df):
                    # Use orjson for faster serialization
                    out[name] = orjson.dumps(val.to_dict("records")).decode()
            elif val != getattr(defaults, name):
                out[name] = str(val)
        pn.state.location.search = "?" + urlencode(out, doseq=True)

    _load_url_state()
    for p in state.param:
        state.param.watch(_save_url_state, p)


# =====================================================================================
# 6. APPLICATION ASSEMBLY
#    - Template creation and content binding
#    - Header, sidebar, and main content coordination
#    - Material UI template with Adaptus theming
# =====================================================================================


def create_unified_app():
    """
    Create the complete unified Adapt[us] investment platform.

    Focused interface for LP engagement featuring:
    - Climate scenario modeling with market projections
    - Complete fund modeling with European waterfall
    - Investment pipeline with target companies
    - Personal LP returns analysis
    - Impact analysis and competitive positioning
    """

    # Setup URL persistence
    setup_url_persistence()

    # Configure component defaults for theme inheritance
    pmui.Page.param.theme_config.default = THEME_CONFIG
    pn.widgets.Tabulator.param.theme.default = "materialize"

    # Create main content with Container pattern for better viewport control
    main_tabs = pmui.Tabs(
        ("CLIMATE THESIS", create_climate_view()),
        ("FUND MODELING", create_fund_modeling_view()),
        ("PIPELINE", create_pipeline_view()),
        ("LP RETURNS", create_returns_view()),
        ("IMPACT", create_impact_view()),
        ("COMPETITIVE", create_competitive_view()),
        active=0,
        theme_config=THEME_CONFIG,
    )

    # Main content container with proper scrolling and viewport
    main_content = pmui.Container(
        pmui.Column(create_lp_hero_section(), main_tabs),
        width_option="xl",
        disable_gutters=True,
        sx={
            "height": "calc(100vh - 80px)",
            "overflow-y": "auto",
            "padding": "16px",
            "font-family": "'Space Mono', monospace",
            "& .bk-root": {"width": "100% !important", "max-width": "none !important"},
        },
        theme_config=THEME_CONFIG,
    )

    # Dynamic sidebar with climate control and scenario info
    @pn.depends(
        state.param.temperature, state.param.lp_investment, state.param.coinvest_enabled
    )
    def sidebar_content(temperature, investment, coinvest):
        """Generate dynamic sidebar content with climate control."""
        return pmui.Column(
            pn.Spacer(height=10),
            # Climate Controls Section
            pmui.Typography(
                '<h6><i class="fas fa-thermometer-half adaptus-icon"></i>Climate Scenario</h6>'
            ),
            pmui.Card(
                pmui.FloatSlider.from_param(
                    state.param.temperature,
                    name="Temperature (°C)",
                    width=250,
                    color="primary",
                ),
                theme_config=THEME_CONFIG,
            ),
            pn.Spacer(height=10),
            # Investment Controls Section
            pmui.Typography(
                '<h6><i class="fas fa-dollar-sign adaptus-icon"></i>Your Investment</h6>'
            ),
            pmui.Card(
                pmui.NumberInput.from_param(
                    state.param.lp_investment, name="Commitment ($)", step=500000
                ),
                pmui.Switch.from_param(
                    state.param.coinvest_enabled, name="Co-Investment Rights"
                ),
                theme_config=THEME_CONFIG,
            ),
            pn.Spacer(height=10),
            # Current Scenario Section
            pmui.Typography(
                '<h6><i class="fas fa-chart-line adaptus-icon"></i>Current Scenario</h6>'
            ),
            pmui.Card(
                pmui.Markdown(f"""
                **Climate**: {temperature}°C warming
                **Investment**: ${investment:,.0f}
                **Co-invest**: {"Yes" if coinvest else "No"}
                """),
                theme_config=THEME_CONFIG,
            ),
            pn.Spacer(height=10),
            # Fund Details Section
            pmui.Typography(
                '<h6><i class="fas fa-university adaptus-icon"></i>Fund Details</h6>'
            ),
            pmui.Card(
                pmui.Markdown("""
                **Size**: $30M | **Track Record**: 7x TVPI, 51% IRR
                **Terms**: 20% carry, 8% hurdle | **Minimum**: $50K
                **Target Close**: $10M by March 2026
                """),
                theme_config=THEME_CONFIG,
            ),
            pn.Spacer(height=10),
            # Contact Section
            pmui.Typography(
                '<h6><i class="fas fa-envelope adaptus-icon"></i>Contact</h6>'
            ),
            pmui.Card(
                pmui.Markdown("""
                **Darren Clifford**
                Managing Partner

                📧 [dc@aucap.vc](mailto:dc@aucap.vc)
                📞 +1 713 373 7324
                """),
                theme_config=THEME_CONFIG,
            ),
            width=280,
        )

    # Assemble complete application with PMUI Page pattern
    app = pmui.Page(
        title="Adapt[us] Climate Adaptation Investment Platform",
        sidebar=[sidebar_content],
        main=[main_content],
        sidebar_width=320,
        theme_toggle=True,  # Enable built-in Panel theme toggle
        theme_config=THEME_CONFIG,
    )

    return app


# =====================================================================================
# 7. APPLICATION ENTRY POINT
#    - Multiple execution contexts supported
#    - Direct Python execution, Panel serve, and module import
# =====================================================================================

# Create and serve the unified application
if __name__ == "__main__":
    # Direct Python execution: python app.py
    create_unified_app().servable()
elif __name__.startswith("bokeh"):
    # Panel serve execution: panel serve app.py
    create_unified_app().servable()
else:
    # Module import: import app; app.unified_app
    unified_app = create_unified_app().servable()

# =====================================================================================
# END OF FILE
#
# Total lines: ~2500
# Key entry points:
#   - create_unified_app(): Main application builder
#   - UnifiedState: Parameter management
#   - compute_fund_metrics(): Financial calculations
#   - create_*_view(): Individual tab components
#   - ADAPTUS/COLORS: Theme configuration
# =====================================================================================