mobiler-web 0.37.0

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

use std::cell::RefCell;
use std::collections::HashMap;
use std::rc::Rc;
use std::sync::Arc;

use crux_core::{App, Core, Request};
use leptos::prelude::*;
use mobiler_core::{
    A11yRole, Action, BoxAlign, ButtonStyle, CardStyle, ChartBracket, ChartLegendItem, ChartRefLine, ChartRegion,
    ChartSeries, ChartStyle, ChartTick, Corner, Density, Effect, FieldKind, FontFamily, HttpHeader, HttpOutcome, Icon,
    ImageRatio, ImageShape, InputValue, PluginCall, PluginNotify, PluginResponse, PluginStreamCall, ProjectColor,
    Rgb, ShellLabels, Spacing, TextStyle, Theme, Tone, TransferEvent, Widget,
};
use wasm_bindgen_futures::spawn_local;

/// The shell's own stylesheet — the web twin of the look the Android/SwiftUI shells
/// decide in code. Shipped with the crate and injected on mount, so `run::<App>()`
/// renders a fully styled, themeable app with no CSS required from the consuming
/// app (it can still override any class). Uses CSS variables so `Scaffold.dark_mode`
/// flips the whole theme by toggling one class.
const STYLE: &str = include_str!("mobiler.css");

/// Cloneable handle for sending an `Action` into the core. Leptos 0.7 view closures
/// require `Send`, so this is `Arc` + `Send + Sync` (the crux `Core` is both).
type Dispatch = Arc<dyn Fn(Action) + Send + Sync>;

/// What a Mobiler app must be to render on the web: a crux `App` speaking the fixed
/// ABI (`Action` in, `Widget` out, `Effect` for capabilities). `MobilerShell<_>`
/// satisfies this automatically.
pub trait WebApp:
    App<Event = Action, ViewModel = Widget, Effect = Effect> + Default + Send + Sync + 'static
where
    Self::Model: Default + Send + Sync,
{
}
impl<T> WebApp for T
where
    T: App<Event = Action, ViewModel = Widget, Effect = Effect> + Default + Send + Sync + 'static,
    T::Model: Default + Send + Sync,
{
}

/// Mount a Mobiler app into the document body. Call from your wasm `main`.
pub fn run<A: WebApp>()
where
    A::Model: Default + Send + Sync,
{
    console_error_panic_hook::set_once();
    inject_default_style();
    inject_hls_support();
    inject_maplibre_support();
    leptos::mount::mount_to_body(shell::<A>);
}

/// hls.js bootstrap for HLS (`.m3u8`) playback in browsers without native HLS
/// (Chrome/Firefox — Safari/iOS play HLS natively). A `<video>` whose source is an
/// `.m3u8` is rendered with `data-hls-src` and no `src`; this self-contained script
/// watches the DOM (a `MutationObserver`, so it also catches elements re-rendered on
/// each `update`) and, for each such `<video>`, either sets `src` directly (native
/// HLS, e.g. Safari) or lazily loads hls.js from a CDN and attaches it. If the CDN
/// fails it falls back to a plain `src`. Inert until an `.m3u8` `Video` appears, so
/// MP4/Bunny content (and non-video apps) pay nothing. Bunny content keeps using its
/// own player via `WebView`; this is for raw non-Bunny `.m3u8` on Chrome/Firefox.
fn inject_hls_support() {
    const BOOTSTRAP: &str = r#"(function(){
  function ensureHls(cb){
    if(window.Hls){return cb();}
    if(window.__mobilerHlsLoading){(window.__mobilerHlsCbs=window.__mobilerHlsCbs||[]).push(cb);return;}
    window.__mobilerHlsLoading=true;window.__mobilerHlsCbs=[cb];
    var s=document.createElement('script');
    s.src='https://cdn.jsdelivr.net/npm/hls.js@1';
    var flush=function(){var cbs=window.__mobilerHlsCbs||[];window.__mobilerHlsCbs=[];cbs.forEach(function(f){f();});};
    s.onload=flush;s.onerror=flush;
    document.head.appendChild(s);
  }
  function attach(v){
    if(v.__mobilerHlsDone){return;}v.__mobilerHlsDone=true;
    var url=v.getAttribute('data-hls-src');if(!url){return;}
    if(v.canPlayType('application/vnd.apple.mpegurl')){v.src=url;return;}
    ensureHls(function(){
      if(window.Hls&&window.Hls.isSupported()){var h=new window.Hls();h.loadSource(url);h.attachMedia(v);v.__mobilerHls=h;}
      else{v.src=url;}
    });
  }
  function scan(root){if(root&&root.querySelectorAll){root.querySelectorAll('video[data-hls-src]').forEach(attach);}}
  new MutationObserver(function(muts){muts.forEach(function(m){m.addedNodes.forEach(function(n){if(n.nodeType===1){if(n.matches&&n.matches('video[data-hls-src]')){attach(n);}scan(n);}});});}).observe(document.documentElement,{childList:true,subtree:true});
  scan(document);
})();"#;
    let document = leptos::prelude::document();
    let Some(head) = document.head() else { return };
    let Ok(script) = document.create_element("script") else { return };
    script.set_text_content(Some(BOOTSTRAP));
    let _ = head.append_child(&script);
}

/// MapLibre-GL bootstrap for [`Widget::Map`]. A self-contained script (mirrors `inject_hls_support`):
/// lazily loads maplibre-gl (JS + CSS) from a CDN the first time a `.mobiler-map` div appears, then for
/// each one inits a `maplibregl.Map` from its `data-*` attributes (center/zoom/style/markers/interactive)
/// and wires taps. The Rust render arm re-creates the map div on every `update`, so a `MutationObserver`
/// also REMOVES the map (`.remove()`) when its node is dropped — no leaked WebGL contexts. Map/marker
/// taps are reported to the core by writing `"tap|lat,lng"` / `"marker|id"` into the hidden sibling
/// `.mobiler-map-sink` input and firing its `input` event, which the render arm's `on:input` forwards as
/// `Action::Input`. Inert (and the CDN is never fetched) until a `Map` widget appears.
fn inject_maplibre_support() {
    const BOOTSTRAP: &str = r#"(function(){
  function ensureML(cb){
    if(window.maplibregl){return cb();}
    if(window.__mobilerMlLoading){(window.__mobilerMlCbs=window.__mobilerMlCbs||[]).push(cb);return;}
    window.__mobilerMlLoading=true;window.__mobilerMlCbs=[cb];
    var l=document.createElement('link');l.rel='stylesheet';l.href='https://cdn.jsdelivr.net/npm/maplibre-gl@4/dist/maplibre-gl.css';document.head.appendChild(l);
    var s=document.createElement('script');s.src='https://cdn.jsdelivr.net/npm/maplibre-gl@4/dist/maplibre-gl.js';
    var flush=function(){var cbs=window.__mobilerMlCbs||[];window.__mobilerMlCbs=[];cbs.forEach(function(f){f();});};
    s.onload=flush;s.onerror=flush;document.head.appendChild(s);
  }
  function emit(el,payload){
    var sink=el.parentElement&&el.parentElement.querySelector('.mobiler-map-sink');
    if(sink){sink.value=payload;sink.dispatchEvent(new Event('input',{bubbles:true}));}
  }
  function init(el){
    if(el.__mobilerMap){return;}el.__mobilerMap=true;
    ensureML(function(){
      try{
        var c=(el.getAttribute('data-center')||'0,0').split(',');
        var center=[parseFloat(c[1])||0,parseFloat(c[0])||0];
        var zoom=parseFloat(el.getAttribute('data-zoom'))||2;
        var style=el.getAttribute('data-style')||'https://tiles.openfreemap.org/styles/liberty';
        var interactive=el.getAttribute('data-interactive')!=='false';
        var map=new maplibregl.Map({container:el,style:style,center:center,zoom:zoom,interactive:interactive});
        el.__mobilerMapInstance=map;
        map.on('click',function(e){emit(el,'tap|'+e.lngLat.lat.toFixed(6)+','+e.lngLat.lng.toFixed(6));});
        var markers=[];try{markers=JSON.parse(el.getAttribute('data-markers')||'[]');}catch(_){}
        markers.forEach(function(mk){
          var m=new maplibregl.Marker().setLngLat([mk.lng,mk.lat]);
          if(mk.title){m.setPopup(new maplibregl.Popup({offset:24}).setText(mk.title));}
          m.addTo(map);
          m.getElement().addEventListener('click',function(ev){ev.stopPropagation();emit(el,'marker|'+mk.id);});
        });
      }catch(_){}
    });
  }
  function scan(root){if(root&&root.querySelectorAll){root.querySelectorAll('.mobiler-map[data-map]').forEach(init);}}
  new MutationObserver(function(muts){muts.forEach(function(m){
    m.addedNodes.forEach(function(n){if(n.nodeType===1){if(n.matches&&n.matches('.mobiler-map[data-map]')){init(n);}scan(n);}});
    m.removedNodes.forEach(function(n){if(n.nodeType===1){
      if(n.__mobilerMapInstance){try{n.__mobilerMapInstance.remove();}catch(_){}}
      if(n.querySelectorAll){n.querySelectorAll('.mobiler-map').forEach(function(x){if(x.__mobilerMapInstance){try{x.__mobilerMapInstance.remove();}catch(_){}}});}
    }});
  });}).observe(document.documentElement,{childList:true,subtree:true});
  scan(document);
})();"#;
    let document = leptos::prelude::document();
    let Some(head) = document.head() else { return };
    let Ok(script) = document.create_element("script") else { return };
    script.set_text_content(Some(BOOTSTRAP));
    let _ = head.append_child(&script);
}

/// Inject the shell's default stylesheet at the **front** of `<head>` so it's the
/// lowest-precedence baseline: an app that ships its own CSS (later in the document)
/// overrides any of these classes, while an app with no CSS still gets a full theme.
fn inject_default_style() {
    let document = leptos::prelude::document();
    let Some(head) = document.head() else { return };
    let Ok(style) = document.create_element("style") else { return };
    let _ = style.set_attribute("data-mobiler", "shell");
    style.set_text_content(Some(STYLE));
    let _ = head.insert_before(&style, head.first_child().as_ref());
}

fn shell<A: WebApp>() -> impl IntoView
where
    A::Model: Default + Send + Sync,
{
    let core = Arc::new(Core::<A>::new());
    let (view, set_view) = signal(core.view());

    let send: Dispatch = {
        let core = core.clone();
        Arc::new(move |action: Action| {
            let effects = core.process_event(action);
            drive(&core, set_view, effects);
        })
    };

    // Restore persisted state (localStorage), then fire Start — mirrors the native
    // shells (which restore before Start so the app sees its saved Model on launch).
    let saved = local_storage().and_then(|s| s.get_item(STORAGE_KEY).ok().flatten()).unwrap_or_default();
    if !saved.is_empty() {
        send(Action::Restore { data: saved });
    }
    send(Action::Start);

    let send_for_view = send.clone();
    view! {
        <div class="app">
            {move || {
                let widget = view.get();
                // Stash labels from the root Scaffold only. A Scaffold nested inside a sheet,
                // body or Split (rendered by the Scaffold arm below) must not overwrite the
                // root's labels — only the root scaffold owns the shell chrome. Code that never
                // sees the view (the confirm modal) reads this stash.
                ACTIVE_LABELS.with(|l| {
                    *l.borrow_mut() = match &widget {
                        Widget::Scaffold { labels, .. } => labels.clone(),
                        _ => None,
                    };
                });
                render(&widget, &send_for_view)
            }}
        </div>
    }
}

/// Process effects: re-read the view on Render; fulfil HTTP via fetch and resolve.
fn drive<A: WebApp>(core: &Arc<Core<A>>, set_view: WriteSignal<Widget>, effects: Vec<Effect>)
where
    A::Model: Default + Send + Sync,
{
    for effect in effects {
        match effect {
            Effect::Render(_) => set_view.set(core.view()),
            Effect::PluginNotify(notify) => perform_notify(&notify.operation),
            Effect::Plugin(mut request) => {
                let core = core.clone();
                spawn_local(async move {
                    let response = perform(&request.operation).await;
                    if let Ok(next) = core.resolve(&mut request, response) {
                        drive(&core, set_view, next);
                    }
                });
            }
            // Long-lived subscription: start a native source that resolves the same
            // request repeatedly (one event per `core.resolve`). See `start_stream`.
            Effect::PluginStream(request) => start_stream(core, set_view, request),
        }
    }
}

/// Start a streaming subscription ([`Effect::PluginStream`]): begin a native source
/// that resolves `request` **repeatedly** (a [`PluginResponse`] per event), each
/// resolution re-entering the core. The source handle is parked in a per-key
/// registry so [`unsubscribe`](mobiler_core::Cx::unsubscribe) can stop it.
///
/// Web sources: `ticker`/`start` (a `setInterval` emitting an incrementing counter
/// every `input` ms — the deterministic demonstrator) and `websocket`/`stream`
/// (a `WebSocket`, a frame per `onmessage`).
fn start_stream<A: WebApp>(
    core: &Arc<Core<A>>,
    set_view: WriteSignal<Widget>,
    request: Request<PluginStreamCall>,
) where
    A::Model: Default + Send + Sync,
{
    use wasm_bindgen::{closure::Closure, JsCast};

    let call = request.operation.clone();

    // Each resolution of a `resolves_many_times` request yields the next stream item;
    // share the request across event closures via Rc<RefCell<_>>.
    let request = Rc::new(RefCell::new(request));
    let core = core.clone();
    let emit = move |resp: PluginResponse| {
        if let Ok(next) = core.resolve(&mut *request.borrow_mut(), resp) {
            drive(&core, set_view, next);
        }
    };

    let handle = match (call.plugin.as_str(), call.op.as_str()) {
        // Built-in deterministic demonstrator: emit an incrementing counter every
        // `input` ms. Dropping the Interval (on unsubscribe) stops it.
        ("ticker", "start") => {
            let ms: u32 = call.input.parse().unwrap_or(1000);
            let count = std::cell::Cell::new(0u32);
            let interval = gloo_timers::callback::Interval::new(ms, move || {
                count.set(count.get() + 1);
                emit(PluginResponse::text(true, count.get().to_string()));
            });
            StreamHandle::Ticker { _interval: interval }
        }
        ("websocket", "stream") => {
            let Ok(ws) = web_sys::WebSocket::new(&call.input) else { return };
            let onmessage = {
                let emit = emit.clone();
                Closure::<dyn FnMut(web_sys::MessageEvent)>::new(move |e: web_sys::MessageEvent| {
                    emit(PluginResponse::text(true, e.data().as_string().unwrap_or_default()));
                })
            };
            let onclose = Closure::<dyn FnMut(web_sys::CloseEvent)>::new(move |_e| {
                emit(PluginResponse::text(false, "closed"));
            });
            ws.set_onmessage(Some(onmessage.as_ref().unchecked_ref()));
            ws.set_onclose(Some(onclose.as_ref().unchecked_ref()));
            StreamHandle::Ws(WsStream { ws, _onmessage: onmessage, _onclose: onclose })
        }
        // Built-in `system` source: deep-link URLs + app lifecycle. On the web a "deep link" is the
        // current URL (delivered on subscribe + on `popstate`) and "lifecycle" maps to page
        // visibility (`visibilitychange`). Listeners are dropped (removed) on unsubscribe.
        ("system", "events") => {
            let win = web_sys::window().expect("window");
            let doc = win.document().expect("document");
            // Initial: the current URL as a deeplink + current visibility as lifecycle.
            if let Ok(href) = win.location().href() {
                emit(PluginResponse::text(true, system_deeplink(&href)));
            }
            emit(PluginResponse::text(true, system_lifecycle(&doc)));
            let onpop = {
                let (emit, win) = (emit.clone(), win.clone());
                Closure::<dyn FnMut(web_sys::Event)>::new(move |_e: web_sys::Event| {
                    if let Ok(href) = win.location().href() {
                        emit(PluginResponse::text(true, system_deeplink(&href)));
                    }
                })
            };
            let onvis = {
                let (emit, doc) = (emit.clone(), doc.clone());
                Closure::<dyn FnMut(web_sys::Event)>::new(move |_e: web_sys::Event| {
                    emit(PluginResponse::text(true, system_lifecycle(&doc)));
                })
            };
            let _ = win.add_event_listener_with_callback("popstate", onpop.as_ref().unchecked_ref());
            let _ = doc.add_event_listener_with_callback("visibilitychange", onvis.as_ref().unchecked_ref());
            StreamHandle::System(SystemStream { win, doc, _onpop: onpop, _onvis: onvis })
        }
        // Streaming file transfer (`cx.upload` / `cx.download`, Release B). See
        // `start_web_upload` / `start_web_download` for the WEB ASYMMETRY: upload uses
        // XHR (the only web API with upload-progress events), download uses fetch +
        // ReadableStream (progress) and hands the app back a `blob:` handle.
        ("transfer", op @ ("upload" | "download")) => {
            let v: serde_json::Value = serde_json::from_str(&call.input).unwrap_or(serde_json::Value::Null);
            let url = v.get("url").and_then(|x| x.as_str()).unwrap_or("").to_string();
            let headers: Vec<(String, String)> = v
                .get("headers")
                .and_then(|x| x.as_array())
                .map(|hs| {
                    hs.iter()
                        .filter_map(|h| Some((h.get("name")?.as_str()?.to_string(), h.get("value")?.as_str()?.to_string())))
                        .collect()
                })
                .unwrap_or_default();

            if op == "upload" {
                let method = v.get("method").and_then(|x| x.as_str()).unwrap_or("PUT").to_string();
                let source = v.get("source").and_then(|x| x.as_str()).unwrap_or("").to_string();
                let multipart = v.get("multipart").map(|m| WebMultipart {
                    field: m.get("field").and_then(|x| x.as_str()).unwrap_or("").to_string(),
                    filename: m.get("filename").and_then(|x| x.as_str()).map(|s| s.to_string()),
                    fields: m
                        .get("fields")
                        .and_then(|x| x.as_array())
                        .map(|fs| {
                            fs.iter()
                                .filter_map(|f| Some((f.get("name")?.as_str()?.to_string(), f.get("value")?.as_str()?.to_string())))
                                .collect()
                        })
                        .unwrap_or_default(),
                });
                start_web_upload(url, method, headers, source, multipart, emit.clone())
            } else {
                start_web_download(url, headers, emit.clone())
            }
        }
        _ => return, // unknown / native-only source — ignore on web
    };

    STREAMS.with(|m| {
        m.borrow_mut().insert(call.key.clone(), handle);
    });
}

/// Monotonic milliseconds, for the ~10/sec progress throttle (`performance.now()`).
fn js_now() -> f64 {
    web_sys::window().and_then(|w| w.performance()).map(|p| p.now()).unwrap_or(0.0)
}

/// Parsed `multipart` envelope config (Task 1's `cx.upload(...).multipart(...)`), threaded
/// into `start_web_upload` the same way `headers`/`method`/`source` are. `file_content_type`
/// is deliberately NOT carried here — it's a native-only override; on web the browser derives
/// the file part's `Content-Type` from the `Blob` itself.
struct WebMultipart {
    /// The file part's field name.
    field: String,
    /// Override for the file part's `filename=`; `None` -> infer from the source handle.
    filename: Option<String>,
    /// Text fields, in order, emitted before the file part.
    fields: Vec<(String, String)>,
}

/// Last `/`-segment of a handle, `?query`/`#fragment` stripped; empty -> "file".
fn infer_filename(source: &str) -> String {
    let s = source.split(['?', '#']).next().unwrap_or(source);
    let name = s.rsplit('/').next().unwrap_or("");
    if name.is_empty() {
        "file".to_string()
    } else {
        name.to_string()
    }
}

/// Parse the CRLF-separated block from `XmlHttpRequest::get_all_response_headers` into
/// `HttpHeader`s. Each line is `name: value`; a value never contains CRLF (XHR spec), so
/// splitting on `\r\n` then on the first `:` is sufficient. Blank lines are skipped.
fn parse_header_block(raw: &str) -> Vec<HttpHeader> {
    raw.split("\r\n")
        .filter_map(|line| {
            let (name, value) = line.split_once(':')?;
            let name = name.trim();
            if name.is_empty() {
                return None;
            }
            Some(HttpHeader { name: name.to_string(), value: value.trim().to_string() })
        })
        .collect()
}

/// Start a web upload via `XMLHttpRequest`.
///
/// DELIBERATE WEB ASYMMETRY (see `start_web_download` for the other half): upload uses
/// XHR because it is the *only* web API that reports upload progress
/// (`xhr.upload().onprogress`) — `fetch()` has no upload-progress signal at all. Do not
/// "unify" this with fetch; there is no fetch-based way to get upload progress in a
/// browser today.
fn start_web_upload(
    url: String,
    method: String,
    headers: Vec<(String, String)>,
    source: String,
    multipart: Option<WebMultipart>,
    emit: impl Fn(PluginResponse) + Clone + 'static,
) -> StreamHandle {
    use wasm_bindgen::{closure::Closure, JsCast};
    let xhr = web_sys::XmlHttpRequest::new().expect("xhr");
    let _ = xhr.open_with_async(&method, &url, true);
    for (n, val) in &headers {
        // In multipart mode, the browser sets `Content-Type: multipart/form-data;
        // boundary=...` itself when sending a `FormData` body; a caller-supplied
        // `Content-Type` header would clobber that boundary and break the request. Other
        // headers (Authorization, etc.) still apply.
        if multipart.is_some() && n.eq_ignore_ascii_case("content-type") {
            continue;
        }
        let _ = xhr.set_request_header(n, val);
    }

    // ~10/sec progress throttling, purely on elapsed time. (Gating on `loaded < total`
    // as well would be a no-op when `!length_computable`, since `total()` is then 0 and
    // `loaded() < 0` is always false.) The terminal Done is emitted by the
    // separate onload/onerror/onabort closures below, unthrottled, so completion is always
    // seen regardless of this gate.
    let last = std::rc::Rc::new(std::cell::Cell::new(0.0f64));
    let on_prog = {
        let (emit, last) = (emit.clone(), last.clone());
        Closure::<dyn FnMut(web_sys::ProgressEvent)>::new(move |e: web_sys::ProgressEvent| {
            let now = js_now();
            if now - last.get() < 100.0 {
                return;
            }
            last.set(now);
            let total = if e.length_computable() { Some(e.total() as u64) } else { None };
            emit(transfer_response(&TransferEvent::Progress { transferred: e.loaded() as u64, total }));
        })
    };
    if let Ok(upload) = xhr.upload() {
        upload.set_onprogress(Some(on_prog.as_ref().unchecked_ref()));
    }

    // Terminal event: a response (even non-2xx) is `Done { Response }`; only a failure
    // to obtain a response at all is `Done { TransportError }`.
    let on_done = {
        let (emit, xhr_c) = (emit.clone(), xhr.clone());
        Closure::<dyn FnMut()>::new(move || {
            let status = xhr_c.status().unwrap_or(0);
            let outcome = if status == 0 {
                HttpOutcome::TransportError { message: "upload failed".into() }
            } else {
                // Carry the response headers, like the download path and both native shells
                // do — an upload caller may need ETag / Location. `getAllResponseHeaders`
                // returns a CRLF-separated block (and, cross-origin, only the
                // CORS-exposed headers — a browser limit the web download path shares; the
                // native shells read the full header set).
                let headers = xhr_c
                    .get_all_response_headers()
                    .ok()
                    .map(|raw| parse_header_block(&raw))
                    .unwrap_or_default();
                HttpOutcome::Response { status, headers, body: vec![] }
            };
            emit(transfer_response(&TransferEvent::Done { outcome, handle: None }));
        })
    };
    xhr.set_onload(Some(on_done.as_ref().unchecked_ref()));
    let on_err = {
        let emit = emit.clone();
        Closure::<dyn FnMut()>::new(move || {
            emit(transfer_response(&TransferEvent::Done {
                outcome: HttpOutcome::TransportError { message: "upload error".into() },
                handle: None,
            }));
        })
    };
    xhr.set_onerror(Some(on_err.as_ref().unchecked_ref()));
    let on_abort = {
        let emit = emit.clone();
        Closure::<dyn FnMut()>::new(move || {
            emit(transfer_response(&TransferEvent::Done {
                outcome: HttpOutcome::TransportError { message: "upload aborted".into() },
                handle: None,
            }));
        })
    };
    xhr.set_onabort(Some(on_abort.as_ref().unchecked_ref()));

    // The upload `source` is itself a `blob:` URL (e.g. produced by `photo`/`camera` or
    // `files`), so fetch it back into a `Blob` before sending — same shape a native
    // shell would read a file handle. A missing/unreadable source sends no body.
    //
    // Cancel race (see `TransferHandle::drop`): `open_with_async` above has already run,
    // but `send`/`send_with_opt_blob` is deferred behind the `fetch_blob` await. Per the
    // XHR spec, `abort()` before the send-flag is set (i.e. before `send` is called) is a
    // no-op, so if `cx.unsubscribe` fires in this window, `xhr.abort()` alone would not
    // stop the request from going out. `cancelled` is the second half of that guarantee:
    // it's checked right before `send`, after the await, so a drop that lands during the
    // fetch is still honored.
    let xhr_send = xhr.clone();
    let cancelled = std::rc::Rc::new(std::cell::Cell::new(false));
    let cancelled_send = cancelled.clone();
    wasm_bindgen_futures::spawn_local(async move {
        let blob = fetch_blob(&source).await;
        if cancelled_send.get() {
            return;
        }
        match (blob, &multipart) {
            (Some(blob), Some(mp)) => {
                // Text fields first, file part LAST (multipart/form-data ordering).
                let form = web_sys::FormData::new().expect("FormData");
                for (name, value) in &mp.fields {
                    let _ = form.append_with_str(name, value);
                }
                let filename = mp.filename.clone().unwrap_or_else(|| infer_filename(&source));
                let _ = form.append_with_blob_and_filename(&mp.field, &blob, &filename);
                let _ = xhr_send.send_with_opt_form_data(Some(&form));
            }
            (Some(blob), None) => {
                let _ = xhr_send.send_with_opt_blob(Some(&blob));
            }
            (None, _) => {
                let _ = xhr_send.send();
            }
        }
    });

    StreamHandle::Transfer(TransferHandle {
        xhr: Some(xhr),
        abort: None,
        cancelled: Some(cancelled),
        _on_prog: Some(on_prog),
        _on_done: Some(on_done),
        _on_err: Some(on_err),
        _on_abort: Some(on_abort),
    })
}

/// Fetch a `blob:` (or any) URL back into a `Blob`, for handing to
/// `XmlHttpRequest::send_with_opt_blob`. `None` on any failure (network error, not a
/// Blob-shaped response, …) — the caller falls back to sending no body.
async fn fetch_blob(url: &str) -> Option<web_sys::Blob> {
    use wasm_bindgen::JsCast;
    let win = web_sys::window()?;
    let resp_value = wasm_bindgen_futures::JsFuture::from(win.fetch_with_str(url)).await.ok()?;
    let resp: web_sys::Response = resp_value.dyn_into().ok()?;
    let blob_promise = resp.blob().ok()?;
    let blob_value = wasm_bindgen_futures::JsFuture::from(blob_promise).await.ok()?;
    blob_value.dyn_into().ok()
}

/// Start a web download via `fetch` + a `ReadableStream` reader.
///
/// DELIBERATE WEB ASYMMETRY (see `start_web_upload` for the other half): download uses
/// `fetch`'s streaming response body to report progress as chunks arrive, then hands
/// the app back a `blob:` handle for the assembled bytes — the same handle shape
/// `take_image`/`photo.pick` returns via `Url::create_object_url_with_blob`. (XHR could
/// also do a download, but fetch + ReadableStream is the standard/ergonomic way to get
/// mid-transfer download progress on the web.)
fn start_web_download(
    url: String,
    headers: Vec<(String, String)>,
    emit: impl Fn(PluginResponse) + Clone + 'static,
) -> StreamHandle {
    let ctrl = web_sys::AbortController::new().expect("abortcontroller");
    let signal = ctrl.signal();
    let emit2 = emit.clone();
    wasm_bindgen_futures::spawn_local(async move {
        match fetch_stream(&url, &headers, &signal).await {
            Ok((status, resp_headers, total, mut reader)) => {
                let mut got: u64 = 0;
                let mut chunks: Vec<u8> = Vec::new();
                let mut last = js_now();
                loop {
                    match reader.next().await {
                        Ok(Some(chunk)) => {
                            got += chunk.len() as u64;
                            chunks.extend_from_slice(&chunk);
                            let now = js_now();
                            // ~10/sec progress throttling (see `start_web_upload`).
                            if now - last >= 100.0 {
                                last = now;
                                emit2(transfer_response(&TransferEvent::Progress { transferred: got, total }));
                            }
                        }
                        Ok(None) => break, // stream finished
                        Err(msg) => {
                            emit2(transfer_response(&TransferEvent::Done {
                                outcome: HttpOutcome::TransportError { message: msg },
                                handle: None,
                            }));
                            return;
                        }
                    }
                }
                let handle = make_blob_url(&chunks);
                let outcome = HttpOutcome::Response { status, headers: resp_headers, body: vec![] };
                emit2(transfer_response(&TransferEvent::Done { outcome, handle: Some(handle) }));
            }
            Err(msg) => emit2(transfer_response(&TransferEvent::Done {
                outcome: HttpOutcome::TransportError { message: msg },
                handle: None,
            })),
        }
    });
    StreamHandle::Transfer(TransferHandle {
        xhr: None,
        abort: Some(ctrl),
        cancelled: None,
        _on_prog: None,
        _on_done: None,
        _on_err: None,
        _on_abort: None,
    })
}

/// Begin a GET (with the given headers) via `fetch` under `signal` and return the
/// response's status, headers, `Content-Length` (if present) and a chunk [`Reader`]
/// over its body stream.
async fn fetch_stream(
    url: &str,
    headers: &[(String, String)],
    signal: &web_sys::AbortSignal,
) -> Result<(u16, Vec<HttpHeader>, Option<u64>, Reader), String> {
    use wasm_bindgen::JsCast;
    let win = web_sys::window().ok_or_else(|| "no window".to_string())?;
    let js_headers = web_sys::Headers::new().map_err(|e| js_err(&e))?;
    for (n, v) in headers {
        js_headers.append(n, v).map_err(|e| js_err(&e))?;
    }
    let init = web_sys::RequestInit::new();
    init.set_method("GET");
    init.set_headers_headers(&js_headers);
    init.set_signal(Some(signal));
    let request = web_sys::Request::new_with_str_and_init(url, &init).map_err(|e| js_err(&e))?;

    let resp_value = wasm_bindgen_futures::JsFuture::from(win.fetch_with_request(&request))
        .await
        .map_err(|e| js_err(&e))?;
    let resp: web_sys::Response = resp_value.dyn_into().map_err(|_| "fetch: not a Response".to_string())?;
    let status = resp.status();
    let resp_headers = response_headers(&resp.headers());
    let total = resp_headers
        .iter()
        .find(|h| h.name.eq_ignore_ascii_case("content-length"))
        .and_then(|h| h.value.parse().ok());

    let Some(stream) = resp.body() else {
        // No body (e.g. 204/304, or a HEAD-like response) — an empty reader is correct:
        // the caller's loop immediately sees "finished" and moves straight to Done.
        return Ok((status, resp_headers, total, Reader::empty()));
    };
    let reader = web_sys::ReadableStreamDefaultReader::new(&stream).map_err(|e| js_err(&e))?;
    Ok((status, resp_headers, total, Reader::new(reader)))
}

/// A `web_sys::Headers` iterable (Fetch's `Headers` implements `Symbol.iterator` over
/// `[name, value]` pairs) collected into our wire [`HttpHeader`] shape.
fn response_headers(headers: &web_sys::Headers) -> Vec<HttpHeader> {
    use wasm_bindgen::JsCast;
    let mut out = Vec::new();
    if let Ok(Some(iter)) = js_sys::try_iter(headers) {
        for entry in iter.flatten() {
            let arr: js_sys::Array = entry.unchecked_into();
            let name = arr.get(0).as_string().unwrap_or_default();
            let value = arr.get(1).as_string().unwrap_or_default();
            out.push(HttpHeader { name, value });
        }
    }
    out
}

/// Best-effort stringification of a `JsValue` error (e.g. a `DOMException`) for
/// `TransferEvent::Done { outcome: HttpOutcome::TransportError { message } }`.
fn js_err(e: &wasm_bindgen::JsValue) -> String {
    use wasm_bindgen::JsCast;
    e.as_string()
        .or_else(|| e.dyn_ref::<js_sys::Error>().map(|err| String::from(err.message())))
        .unwrap_or_else(|| "transfer error".to_string())
}

/// A minimal async chunk reader over a `ReadableStreamDefaultReader`. `next()` resolves
/// to `Ok(Some(bytes))` per chunk, `Ok(None)` when the stream is done, or `Err(message)`
/// if the underlying `read()` rejects (e.g. the fetch was aborted mid-stream).
struct Reader(Option<web_sys::ReadableStreamDefaultReader>);
impl Reader {
    fn new(reader: web_sys::ReadableStreamDefaultReader) -> Self {
        Self(Some(reader))
    }
    /// A reader over no stream at all (e.g. a bodiless response) — always "done".
    fn empty() -> Self {
        Self(None)
    }
    async fn next(&mut self) -> Result<Option<Vec<u8>>, String> {
        use wasm_bindgen::JsCast;
        let Some(reader) = &self.0 else { return Ok(None) };
        let result = wasm_bindgen_futures::JsFuture::from(reader.read()).await.map_err(|e| js_err(&e))?;
        let result: web_sys::ReadableStreamReadResult = result.unchecked_into();
        if result.get_done().unwrap_or(true) {
            return Ok(None);
        }
        let value = result.get_value();
        let bytes = js_sys::Uint8Array::new(&value).to_vec();
        Ok(Some(bytes))
    }
}

/// Assemble bytes into a `Blob` and return an object URL — the download's `handle`. The
/// same shape [`take_image`]'s `Url::create_object_url_with_blob` returns for a picked
/// photo, so an app can render/save a downloaded file the same way.
fn make_blob_url(bytes: &[u8]) -> String {
    let array = js_sys::Uint8Array::from(bytes);
    let parts = js_sys::Array::new();
    parts.push(&array);
    web_sys::Blob::new_with_u8_array_sequence(&parts)
        .ok()
        .and_then(|blob| web_sys::Url::create_object_url_with_blob(&blob).ok())
        .unwrap_or_default()
}

/// A `system` deeplink event payload (the push-style tagged JSON the app demuxes by `type`).
fn system_deeplink(url: &str) -> String {
    format!("{{\"type\":\"deeplink\",\"url\":{}}}", serde_json::to_string(url).unwrap_or_else(|_| "\"\"".into()))
}
/// A `system` lifecycle event payload — page visibility maps to active/background.
fn system_lifecycle(doc: &web_sys::Document) -> String {
    let state = if doc.visibility_state() == web_sys::VisibilityState::Visible { "active" } else { "background" };
    format!("{{\"type\":\"lifecycle\",\"state\":\"{state}\"}}")
}

/// An open streaming source, parked by subscription key for teardown. Dropping the
/// entry stops the source (the `Interval` cancels on drop; the `WebSocket` is closed
/// explicitly in the `unsubscribe` handler and its closures drop here).
enum StreamHandle {
    /// A `ticker` interval — held only so dropping it (on unsubscribe) cancels it.
    Ticker { _interval: gloo_timers::callback::Interval },
    Ws(WsStream),
    /// The built-in `system` source — holds its JS listeners alive; `Drop` removes them on
    /// unsubscribe (the handle is dropped when removed from `STREAMS`). Never pattern-matched.
    #[allow(dead_code)]
    System(SystemStream),
    /// An in-flight transfer — held so dropping it (on unsubscribe) aborts the XHR /
    /// cancels the fetch reader. Never pattern-matched.
    #[allow(dead_code)]
    Transfer(TransferHandle),
}

/// Holds a web transfer so unsubscribe can abort it. For upload we keep the
/// `XmlHttpRequest` (call `.abort()` on drop via the Drop impl); for download we keep an
/// `AbortController` whose `.abort()` cancels the fetch + reader.
struct TransferHandle {
    xhr: Option<web_sys::XmlHttpRequest>,
    abort: Option<web_sys::AbortController>,
    // Upload-cancel race guard (see the comment at `start_web_upload`'s `spawn_local`):
    // `xhr.abort()` before `send()` has been called is a spec no-op, so this flag is the
    // half that actually stops a not-yet-sent upload. `None` for download, which has no
    // such window (its `AbortController` is wired into the fetch before any async work).
    cancelled: Option<std::rc::Rc<std::cell::Cell<bool>>>,
    // Typed closure fields (not `Closure::into_js_value`, which leaks permanently — see
    // `WsStream`/`SystemStream` above for the same pattern): held here so they free when
    // the handle drops, on unsubscribe or transfer completion. Download wires no XHR
    // event closures, so its fields are `None`.
    _on_prog: Option<wasm_bindgen::closure::Closure<dyn FnMut(web_sys::ProgressEvent)>>,
    _on_done: Option<wasm_bindgen::closure::Closure<dyn FnMut()>>,
    _on_err: Option<wasm_bindgen::closure::Closure<dyn FnMut()>>,
    _on_abort: Option<wasm_bindgen::closure::Closure<dyn FnMut()>>,
}
impl Drop for TransferHandle {
    fn drop(&mut self) {
        if let Some(c) = &self.cancelled {
            c.set(true);
        }
        if let Some(x) = &self.xhr {
            let _ = x.abort();
        }
        if let Some(a) = &self.abort {
            a.abort();
        }
    }
}

/// Bincode a `TransferEvent` into a stream `PluginResponse` (mirrors Release A's `http` encode).
fn transfer_response(ev: &TransferEvent) -> PluginResponse {
    PluginResponse {
        ok: matches!(ev, TransferEvent::Done { outcome, .. } if outcome.is_success()),
        output: ev.encode(),
    }
}

/// The `system` subscription's event listeners — removed from the DOM when dropped (unsubscribe).
struct SystemStream {
    win: web_sys::Window,
    doc: web_sys::Document,
    _onpop: wasm_bindgen::closure::Closure<dyn FnMut(web_sys::Event)>,
    _onvis: wasm_bindgen::closure::Closure<dyn FnMut(web_sys::Event)>,
}
impl Drop for SystemStream {
    fn drop(&mut self) {
        use wasm_bindgen::JsCast;
        let _ = self.win.remove_event_listener_with_callback("popstate", self._onpop.as_ref().unchecked_ref());
        let _ = self.doc.remove_event_listener_with_callback("visibilitychange", self._onvis.as_ref().unchecked_ref());
    }
}

/// An open web `WebSocket` subscription — holds its JS closures so they stay alive.
struct WsStream {
    ws: web_sys::WebSocket,
    _onmessage: wasm_bindgen::closure::Closure<dyn FnMut(web_sys::MessageEvent)>,
    _onclose: wasm_bindgen::closure::Closure<dyn FnMut(web_sys::CloseEvent)>,
}

/// Fulfil a request/response capability. `http` via `fetch`; `device` via the
/// browser's user-agent string (the web analogue of a device model).
async fn perform(call: &PluginCall) -> PluginResponse {
    if call.plugin == "device" {
        let nav = web_sys::window().map(|w| w.navigator());
        let output = if call.op == "locale" {
            // The browser's preferred language as a BCP-47 tag (e.g. "de-CH").
            nav.and_then(|n| n.language()).unwrap_or_else(|| "en-US".into())
        } else {
            nav.and_then(|n| n.user_agent().ok()).unwrap_or_default()
        };
        return PluginResponse::text(true, output);
    }
    if call.plugin == "photo" && call.op == "pick" {
        return take_image(false).await;
    }
    if call.plugin == "camera" && call.op == "capture" {
        return take_image(true).await;
    }
    if call.plugin == "datetime" {
        return match call.op.as_str() {
            "date" => take_datetime("date").await,
            "time" => take_datetime("time").await,
            other => PluginResponse::text(false, format!("unknown datetime op '{other}'")),
        };
    }
    if call.plugin == "dialog" && call.op == "confirm" {
        let ok = confirm_modal(ConfirmAsk::parse(&call.input)).await;
        return PluginResponse::text(ok, if ok { "ok" } else { "cancel" });
    }
    if call.plugin != "http" {
        return PluginResponse::text(false, format!("plugin '{}' not available", call.plugin));
    }
    let v: serde_json::Value = serde_json::from_str(&call.input).unwrap_or(serde_json::Value::Null);
    let url = v.get("url").and_then(serde_json::Value::as_str).unwrap_or("");
    let body = v.get("body").and_then(serde_json::Value::as_str);
    let req_headers: Vec<(String, String)> = v
        .get("headers")
        .and_then(serde_json::Value::as_array)
        .map(|hs| {
            hs.iter()
                .filter_map(|h| {
                    Some((
                        h.get("name")?.as_str()?.to_string(),
                        h.get("value")?.as_str()?.to_string(),
                    ))
                })
                .collect()
        })
        .unwrap_or_default();

    use gloo_net::http::{Method, Request};

    // Exhaustive: an unknown verb is an error, never a silent GET. The previous
    // `_ => Request::get(url)` fallthrough turned every PUT into a GET.
    let builder = match call.op.as_str() {
        "GET" => Request::get(url),
        "POST" => Request::post(url),
        "PUT" => Request::put(url),
        "PATCH" => Request::patch(url),
        "DELETE" => Request::delete(url),
        "HEAD" => Request::get(url).method(Method::HEAD),
        "OPTIONS" => Request::get(url).method(Method::OPTIONS),
        other => return http_transport_error(format!("unsupported HTTP method '{other}'")),
    };

    // Only default Content-Type when the caller did not set one.
    let caller_set_content_type =
        req_headers.iter().any(|(n, _)| n.eq_ignore_ascii_case("content-type"));

    // `RequestBuilder::header` maps to `web_sys::Headers::set`, which REPLACES
    // any existing value for that name — unlike iOS's `addValue` and Android's
    // `addHeader`, which both APPEND. Build a `gloo_net::http::Headers` and
    // `append` into it instead, so repeated names (Set-Cookie, Accept) survive
    // on web the same way they do on the native shells.
    let gloo_headers = gloo_net::http::Headers::new();
    for (name, value) in &req_headers {
        gloo_headers.append(name, value);
    }
    if body.is_some() && !caller_set_content_type {
        gloo_headers.append("Content-Type", "application/json");
    }
    let builder = builder.headers(gloo_headers);

    let request = match body {
        Some(b) => builder.body(b),
        None => builder.build(),
    };
    let request = match request {
        Ok(r) => r,
        Err(e) => return http_transport_error(e.to_string()),
    };

    match request.send().await {
        Ok(resp) => {
            let status = resp.status();
            let headers = resp
                .headers()
                .entries()
                .map(|(name, value)| HttpHeader { name, value })
                .collect();
            match resp.binary().await {
                Ok(bytes) => {
                    let outcome = HttpOutcome::Response { status, headers, body: bytes };
                    PluginResponse { ok: (200..300).contains(&status), output: outcome.encode() }
                }
                // A body-read failure (truncated/aborted stream) is a transport
                // failure, not a successful empty response — match native shells.
                Err(e) => http_transport_error(e.to_string()),
            }
        }
        Err(e) => http_transport_error(e.to_string()),
    }
}

/// A failure where no HTTP response was obtained. `ok` is false and there is no status.
fn http_transport_error(message: String) -> PluginResponse {
    PluginResponse { ok: false, output: HttpOutcome::TransportError { message }.encode() }
}

/// Pick or capture an image via a hidden `<input type=file accept=image/*>`, clicked
/// to open the browser's file dialog — or, with `capture`, to hint the device camera on
/// supporting mobile browsers (desktop falls back to the file dialog). Awaits the
/// `change` event and returns a `blob:` object URL the `<img>` renderer loads. No
/// permission needed (the picker/camera prompt is the browser's). Backs both the
/// `photo`/`pick` and `camera`/`capture` capabilities.
async fn take_image(capture: bool) -> PluginResponse {
    use wasm_bindgen::{closure::Closure, JsCast};
    let Some(doc) = web_sys::window().and_then(|w| w.document()) else {
        return PluginResponse::text(false, "no document");
    };
    let Some(input) = doc.create_element("input").ok().and_then(|e| e.dyn_into::<web_sys::HtmlInputElement>().ok()) else {
        return PluginResponse::text(false, "no input element");
    };
    input.set_type("file");
    input.set_accept("image/*");
    if capture {
        // Hints the environment-facing camera on mobile browsers that support it.
        let _ = input.set_attribute("capture", "environment");
    }

    let (tx, rx) = futures_channel::oneshot::channel::<Option<String>>();
    let tx = std::cell::RefCell::new(Some(tx));
    let input_for_cb = input.clone();
    let on_change = Closure::wrap(Box::new(move || {
        let url = input_for_cb
            .files()
            .and_then(|files| files.get(0))
            .and_then(|file| web_sys::Url::create_object_url_with_blob(&file).ok());
        if let Some(tx) = tx.borrow_mut().take() {
            let _ = tx.send(url);
        }
    }) as Box<dyn FnMut()>);
    input.set_onchange(Some(on_change.as_ref().unchecked_ref()));
    input.click();
    on_change.forget(); // keep the handler alive until `change` fires

    match rx.await {
        Ok(Some(url)) => PluginResponse::text(true, url),
        _ => PluginResponse::text(false, "cancelled"),
    }
}

/// Pick a date (`kind = "date"`) or time (`kind = "time"`) via a hidden native
/// `<input>`, opening the browser's picker with `showPicker()`. Returns the value
/// (`YYYY-MM-DD` for date, 24-hour `HH:MM` for time); `ok=false` on cancel/dismiss.
/// Backs the `datetime` capability (`cx.pick_date` / `cx.pick_time`).
async fn take_datetime(kind: &str) -> PluginResponse {
    use wasm_bindgen::{closure::Closure, JsCast};
    let Some(doc) = web_sys::window().and_then(|w| w.document()) else {
        return PluginResponse::text(false, "no document");
    };
    let Some(input) = doc.create_element("input").ok().and_then(|e| e.dyn_into::<web_sys::HtmlInputElement>().ok()) else {
        return PluginResponse::text(false, "no input element");
    };
    input.set_type(kind); // "date" or "time"
    // showPicker() needs a connected element; keep it in the DOM but out of sight.
    let _ = input.set_attribute("style", "position:fixed;left:-9999px;opacity:0");
    if let Some(body) = doc.body() {
        let _ = body.append_child(&input);
    }

    let (tx, rx) = futures_channel::oneshot::channel::<Option<String>>();
    let tx = std::rc::Rc::new(std::cell::RefCell::new(Some(tx)));
    let input_for_change = input.clone();
    let tx_change = tx.clone();
    let on_change = Closure::wrap(Box::new(move || {
        let v = input_for_change.value();
        if let Some(tx) = tx_change.borrow_mut().take() {
            let _ = tx.send(if v.is_empty() { None } else { Some(v) });
        }
    }) as Box<dyn FnMut()>);
    let tx_cancel = tx.clone();
    let on_cancel = Closure::wrap(Box::new(move || {
        if let Some(tx) = tx_cancel.borrow_mut().take() {
            let _ = tx.send(None);
        }
    }) as Box<dyn FnMut()>);
    let _ = input.add_event_listener_with_callback("change", on_change.as_ref().unchecked_ref());
    let _ = input.add_event_listener_with_callback("cancel", on_cancel.as_ref().unchecked_ref());
    if input.show_picker().is_err() {
        input.click(); // older browsers: focus the field so the user can type a value
    }
    on_change.forget(); // keep the handlers alive until an event fires
    on_cancel.forget();

    let result = rx.await;
    input.remove();
    match result {
        Ok(Some(v)) => PluginResponse::text(true, v),
        _ => PluginResponse::text(false, "cancelled"),
    }
}

const STORAGE_KEY: &str = "mobiler.state";

/// `window.localStorage`, if available.
fn local_storage() -> Option<web_sys::Storage> {
    web_sys::window()?.local_storage().ok().flatten()
}

/// Fulfil a fire-and-forget capability in the browser — the web twin of the native
/// shells' notify handlers (storage/clipboard/share/browser). None block; an unknown
/// capability is a graceful no-op.
fn perform_notify(notify: &PluginNotify) {
    let win = match web_sys::window() {
        Some(w) => w,
        None => return,
    };
    match (notify.plugin.as_str(), notify.op.as_str()) {
        // Persist the state blob (paired with cx.save + restore-on-startup above).
        ("storage", "save") => {
            if let Some(s) = local_storage() {
                let _ = s.set_item(STORAGE_KEY, &notify.input);
            }
        }
        // Copy to the clipboard (write_text returns a Promise we let run).
        ("clipboard", "copy") => {
            let _ = win.navigator().clipboard().write_text(&notify.input);
        }
        // Open a URL in a new tab.
        ("browser", "open") => {
            let _ = win.open_with_url_and_target(&notify.input, "_blank");
        }
        // No reliable cross-browser share sheet (navigator.share is mobile-only and
        // gesture-gated), so degrade to copying — a sane universal fallback.
        ("share", _) => {
            let _ = win.navigator().clipboard().write_text(&notify.input);
        }
        // Tear down a streaming subscription: close the WebSocket parked under this
        // key (input = the subscription key) and drop its closures. Paired with
        // cx.unsubscribe; the matching source was opened in `start_stream`.
        ("stream", "unsubscribe") => {
            // Removing the entry drops the source (a `ticker` Interval cancels on
            // drop); for a WebSocket we also close it explicitly.
            if let Some(StreamHandle::Ws(ws)) = STREAMS.with(|m| m.borrow_mut().remove(&notify.input)) {
                let _ = ws.ws.close();
            }
        }
        // Transient toast: a styled div appended to <body>, auto-removed after a beat.
        ("toast", _) => show_toast(&notify.input),
        // Haptic tap. navigator.vibrate is unsupported on iOS Safari (a graceful no-op).
        ("haptics", style) => {
            let ms = match style {
                "light" => 15,
                "heavy" => 50,
                _ => 30, // medium / unknown
            };
            let _ = win.navigator().vibrate_with_duration(ms);
        }
        _ => {} // unknown capability: ignore
    }
}

/// Append a transient toast to `<body>` (styled by `.toast` in mobiler.css) and
/// remove it after ~2.6 s — the web twin of the native toast/snackbar.
fn show_toast(text: &str) {
    let Some(doc) = web_sys::window().and_then(|w| w.document()) else { return };
    let (Ok(el), Some(body)) = (doc.create_element("div"), doc.body()) else { return };
    el.set_class_name("toast");
    el.set_text_content(Some(text));
    let _ = body.append_child(&el);
    gloo_timers::callback::Timeout::new(2600, move || el.remove()).forget();
}

/// What the `dialog`/`confirm` request asks for (see `mobiler_core::Confirm`); missing fields keep
/// the defaults, so a plain `cx.confirm` shows OK / Cancel.
struct ConfirmAsk {
    title: String,
    message: String,
    confirm_label: String,
    cancel_label: String,
    destructive: bool,
}

impl ConfirmAsk {
    fn parse(input: &str) -> Self {
        let v: serde_json::Value = serde_json::from_str(input).unwrap_or(serde_json::Value::Null);
        let s = |k: &str, d: &str| v.get(k).and_then(serde_json::Value::as_str).filter(|s| !s.is_empty()).unwrap_or(d).to_string();
        // The scaffold's ShellLabels supply the default when the call itself gives no label; a
        // per-call non-empty label still wins (that's what `s` above already does).
        let ok_default = shell_label(|l| l.ok.clone(), "OK");
        let cancel_default = shell_label(|l| l.cancel.clone(), "Cancel");
        Self {
            title: s("title", ""),
            message: s("message", ""),
            confirm_label: s("confirm_label", &ok_default),
            cancel_label: s("cancel_label", &cancel_default),
            destructive: v.get("destructive").and_then(serde_json::Value::as_bool).unwrap_or(false),
        }
    }
}

/// Per-call counter so the title/message get unique DOM ids for `aria-labelledby` /
/// `aria-describedby` to point at, even if a dialog somehow opens while another is still closing.
static CONFIRM_DIALOG_ID: std::sync::atomic::AtomicU32 = std::sync::atomic::AtomicU32::new(0);

/// The open confirm modal: its answer sender, whether a newer confirm replaced it, and where focus
/// should return when the (last) dialog closes. One confirm at a time — a new one answers the
/// open one `false` and takes over its focus-return target.
struct OpenConfirm {
    tx: std::rc::Rc<std::cell::RefCell<Option<futures_channel::oneshot::Sender<bool>>>>,
    superseded: std::rc::Rc<std::cell::Cell<bool>>,
    restore_to: Option<web_sys::Element>,
}
thread_local! {
    static OPEN_CONFIRM: std::cell::RefCell<Option<OpenConfirm>> = const { std::cell::RefCell::new(None) };
}

/// The web confirm dialog: a modal card on `<body>` that resolves `true` on the confirm button and
/// `false` on the cancel button, Escape, or a press-and-release on the backdrop itself (a drag
/// that starts on the card and releases over the backdrop does not count). Enter activates the
/// focused button (focus starts on cancel for a destructive dialog, else on confirm); whatever had
/// focus before the dialog opened gets it back afterwards. Labelled for assistive tech via
/// `aria-labelledby`/`aria-describedby` when a title/message is present. Replaces
/// `window.confirm`, which can't be relabelled, styled or made accessible this way. Only one
/// confirm is ever open: a new call answers the open one `false` (superseding it, so its teardown
/// won't steal focus) and reuses its focus-return target, so focus ultimately returns to wherever
/// it was before the *first* dialog in the chain opened. The Tab key is trapped inside the card.
async fn confirm_modal(ask: ConfirmAsk) -> bool {
    use wasm_bindgen::{closure::Closure, JsCast};
    let Some(doc) = web_sys::window().and_then(|w| w.document()) else { return false };
    let Some(body) = doc.body() else { return false };
    let el = |tag: &str, class: &str| -> Option<web_sys::HtmlElement> {
        let e = doc.create_element(tag).ok()?.dyn_into::<web_sys::HtmlElement>().ok()?;
        e.set_class_name(class);
        Some(e)
    };
    let confirm_class = if ask.destructive { "btn btn-filled btn-danger" } else { "btn btn-filled" };
    let (Some(scrim), Some(card), Some(actions), Some(cancel), Some(confirm)) = (
        el("div", "confirm-scrim"),
        el("div", "confirm-card"),
        el("div", "confirm-actions"),
        el("button", "btn btn-text"),
        el("button", confirm_class),
    ) else {
        return false;
    };

    // Inherit the scaffold's theme: brand vars (inline style) + dark / Large-density classes.
    if let Some(scaffold) = doc.query_selector(".scaffold").ok().flatten() {
        if let Some(style) = scaffold.get_attribute("style") {
            let _ = scrim.set_attribute("style", &style);
        }
        let classes = scaffold.class_list();
        for c in ["theme-dark", "density-large"] {
            if classes.contains(c) {
                let _ = scrim.class_list().add_1(c);
            }
        }
    }

    let _ = card.set_attribute("role", "alertdialog");
    let _ = card.set_attribute("aria-modal", "true");
    let call_id = CONFIRM_DIALOG_ID.fetch_add(1, std::sync::atomic::Ordering::Relaxed);
    if !ask.title.is_empty() {
        if let Some(title) = el("div", "confirm-title") {
            let title_id = format!("mobiler-confirm-title-{call_id}");
            title.set_text_content(Some(&ask.title));
            let _ = title.set_attribute("id", &title_id);
            let _ = card.append_child(&title);
            let _ = card.set_attribute("aria-labelledby", &title_id);
        }
    }
    // No message element at all when there's nothing to say — an empty <p> would still get read
    // out by a screen reader and wired up as the description.
    if !ask.message.is_empty() {
        if let Some(message) = el("p", "confirm-message") {
            let msg_id = format!("mobiler-confirm-msg-{call_id}");
            message.set_text_content(Some(&ask.message));
            let _ = message.set_attribute("id", &msg_id);
            let _ = card.append_child(&message);
            let _ = card.set_attribute("aria-describedby", &msg_id);
        }
    }
    cancel.set_text_content(Some(&ask.cancel_label));
    confirm.set_text_content(Some(&ask.confirm_label));
    let _ = actions.append_child(&cancel);
    let _ = actions.append_child(&confirm);
    let _ = card.append_child(&actions);
    let _ = scrim.append_child(&card);

    // Supersede any confirm that's still open, now that this one's elements exist and are about
    // to mount: it resolves `false`, its teardown won't restore focus (see below), and this
    // dialog inherits its focus-return target — so the target always traces back to wherever
    // focus was before the first dialog in the chain opened. Doing this only here (not before
    // element creation) means an early return above leaves the old dialog untouched.
    let previously_focused = OPEN_CONFIRM.with(|c| c.borrow_mut().take()).map(|old| {
        old.superseded.set(true);
        if let Some(tx) = old.tx.borrow_mut().take() {
            let _ = tx.send(false);
        }
        old.restore_to
    }).unwrap_or_else(|| doc.active_element());

    let _ = body.append_child(&scrim);
    let _ = if ask.destructive { cancel.focus() } else { confirm.focus() };

    let (tx, rx) = futures_channel::oneshot::channel::<bool>();
    let tx = std::rc::Rc::new(std::cell::RefCell::new(Some(tx)));
    let superseded = std::rc::Rc::new(std::cell::Cell::new(false));
    OPEN_CONFIRM.with(|c| {
        *c.borrow_mut() = Some(OpenConfirm { tx: tx.clone(), superseded: superseded.clone(), restore_to: previously_focused.clone() });
    });
    let answer = move |tx: &std::rc::Rc<std::cell::RefCell<Option<futures_channel::oneshot::Sender<bool>>>>, ok: bool| {
        if let Some(tx) = tx.borrow_mut().take() {
            let _ = tx.send(ok);
        }
    };
    let (t1, t2, t3, t4) = (tx.clone(), tx.clone(), tx.clone(), tx.clone());
    let on_cancel = Closure::wrap(Box::new(move || answer(&t1, false)) as Box<dyn FnMut()>);
    let on_confirm = Closure::wrap(Box::new(move || answer(&t2, true)) as Box<dyn FnMut()>);

    // A drag that starts inside the card and releases over the backdrop (e.g. overshooting while
    // selecting the message text) must not read as a backdrop dismiss. `pointerdown` records
    // whether the press itself landed on the scrim; `click` (which fires on release, and bubbles
    // from whatever was under the pointer) only cancels when both the press and the click target
    // were the scrim itself, never a bubbled child.
    let press_started_on_scrim = std::rc::Rc::new(std::cell::Cell::new(false));
    let scrim_node: web_sys::EventTarget = scrim.clone().into();
    let press_flag = press_started_on_scrim.clone();
    let pointerdown_target = scrim_node.clone();
    let on_pointerdown = Closure::wrap(Box::new(move |e: web_sys::Event| {
        press_flag.set(e.target().as_ref() == Some(&pointerdown_target));
    }) as Box<dyn FnMut(web_sys::Event)>);
    let on_backdrop = Closure::wrap(Box::new(move |e: web_sys::Event| {
        if press_started_on_scrim.get() && e.target().as_ref() == Some(&scrim_node) {
            answer(&t3, false);
        }
    }) as Box<dyn FnMut(web_sys::Event)>);
    // Trap Tab inside the card: Shift+Tab off the first focusable (or from outside the card)
    // wraps to the last; Tab off the last (or from outside) wraps to the first.
    let (doc_for_key, card_for_key, cancel_for_key, confirm_for_key) = (doc.clone(), card.clone(), cancel.clone(), confirm.clone());
    let on_key = Closure::wrap(Box::new(move |e: web_sys::KeyboardEvent| {
        if e.key() == "Escape" {
            answer(&t4, false);
        } else if e.key() == "Tab" {
            let first: web_sys::Element = cancel_for_key.clone().into();
            let last: web_sys::Element = confirm_for_key.clone().into();
            let active = doc_for_key.active_element();
            let inside = active.as_ref().is_some_and(|a| card_for_key.contains(a.dyn_ref::<web_sys::Node>()));
            let (at_first, at_last) = (active.as_ref() == Some(&first), active.as_ref() == Some(&last));
            if e.shift_key() {
                if at_first || !inside { e.prevent_default(); let _ = confirm_for_key.focus(); }
            } else if at_last || !inside {
                e.prevent_default();
                let _ = cancel_for_key.focus();
            }
        }
    }) as Box<dyn FnMut(web_sys::KeyboardEvent)>);
    cancel.set_onclick(Some(on_cancel.as_ref().unchecked_ref()));
    confirm.set_onclick(Some(on_confirm.as_ref().unchecked_ref()));
    let _ = scrim.add_event_listener_with_callback("pointerdown", on_pointerdown.as_ref().unchecked_ref());
    let _ = scrim.add_event_listener_with_callback("click", on_backdrop.as_ref().unchecked_ref());
    let _ = doc.add_event_listener_with_callback("keydown", on_key.as_ref().unchecked_ref());

    let ok = rx.await.unwrap_or(false);
    // A real click sends on `tx` and `confirm_modal` resumes through a microtask while the click
    // is still bubbling toward the scrim — the spec runs a microtask checkpoint between listeners
    // during a user-initiated dispatch. A listener removed during dispatch is not invoked, so
    // detach every one of them (and null out the onclick handlers) before their closures drop
    // below; otherwise a click still in flight calls into an already-dropped closure and
    // wasm-bindgen throws "closure invoked recursively or after being dropped".
    let _ = doc.remove_event_listener_with_callback("keydown", on_key.as_ref().unchecked_ref());
    let _ = scrim.remove_event_listener_with_callback("click", on_backdrop.as_ref().unchecked_ref());
    let _ = scrim.remove_event_listener_with_callback("pointerdown", on_pointerdown.as_ref().unchecked_ref());
    cancel.set_onclick(None);
    confirm.set_onclick(None);
    scrim.remove();
    // Only clear OPEN_CONFIRM if it's still ours — a newer confirm may already have superseded us
    // and installed its own entry, which this teardown must not touch.
    OPEN_CONFIRM.with(|c| {
        let mut open = c.borrow_mut();
        if open.as_ref().is_some_and(|o| std::rc::Rc::ptr_eq(&o.tx, &tx)) {
            *open = None;
        }
    });
    // A superseded dialog's teardown must not steal focus back from whatever the newer dialog (or
    // its own teardown) is doing with it.
    if !superseded.get() {
        if let Some(focus_target) = previously_focused.and_then(|e| e.dyn_into::<web_sys::HtmlElement>().ok()) {
            let _ = focus_target.focus();
        }
    }
    ok
}

// ---------------- Widget → DOM ----------------

/// `Widget` → DOM. **Exhaustive** by construction — the `match` has no catch-all,
/// so (like the Compose/SwiftUI shells) it won't compile until every `Widget`
/// variant is handled. Style *intent* (TextStyle, Tone, …) becomes a CSS class;
/// the concrete look lives in `mobiler.css`.
fn render(widget: &Widget, send: &Dispatch) -> AnyView {
    match widget {
        // ---- content ----
        Widget::Text { content, style } => {
            let (class, content) = (text_class(*style), content.clone());
            view! { <p class=class>{content}</p> }.into_any()
        }
        Widget::Image { source, shape, ratio } => {
            let (class, source) = (image_class(*shape, *ratio), source.clone());
            view! { <img class=class src=source /> }.into_any()
        }
        Widget::Badge { label, tone } => {
            let (class, label) = (format!("badge {}", tone_class(*tone)), label.clone());
            view! { <span class=class>{label}</span> }.into_any()
        }
        Widget::ColorDot { color } => {
            view! { <span class=format!("dot {}", dot_class(*color))></span> }.into_any()
        }
        Widget::Avatar { source, status } => {
            let dot = status.map(|t| view! { <span class=format!("avatar-status {}", tone_class(t))></span> });
            view! {
                <span class="avatar">
                    <img class="avatar-img" src=source.clone() />
                    {dot}
                </span>
            }
            .into_any()
        }
        Widget::PdfView { url } => {
            // Browsers render PDFs natively in an iframe (remote URL or local blob/file URL).
            view! { <iframe class="pdfview" src=url.clone() title="PDF"></iframe> }.into_any()
        }
        Widget::WebView { url } => {
            // General embedded web content (incl. hosted player embeds like Bunny.net). `allow`
            // permits autoplay / fullscreen / PiP / encrypted-media so hosted players work.
            view! {
                <iframe
                    class="webview"
                    src=url.clone()
                    title="Web"
                    allow="autoplay; fullscreen; picture-in-picture; encrypted-media"
                    allowfullscreen=true
                ></iframe>
            }.into_any()
        }
        // Interactive map (MapLibre-GL, no key). The div carries the config as data-* attrs;
        // `inject_maplibre_support` inits the map + reports taps by firing `input` on the hidden sink,
        // which this `on:input` forwards as Action::Input { "{id}.tap" | "{id}.marker", Text(...) }.
        Widget::Map { id, center_lat, center_lng, zoom, markers, style_url, interactive } => {
            let send = send.clone();
            let id = id.clone();
            let center = format!("{center_lat},{center_lng}");
            let markers_json = serde_json::to_string(markers).unwrap_or_else(|_| "[]".to_string());
            let style = style_url.clone().unwrap_or_default();
            view! {
                <div class="mobiler-map-wrap">
                    <div
                        class="mobiler-map"
                        data-map="1"
                        data-center=center
                        data-zoom=zoom.to_string()
                        data-style=style
                        data-markers=markers_json
                        data-interactive=interactive.to_string()
                    ></div>
                    <input
                        class="mobiler-map-sink"
                        type="text"
                        tabindex="-1"
                        aria-hidden="true"
                        on:input=move |ev| {
                            let raw = event_target_value(&ev);
                            if let Some((suffix, value)) = raw.split_once('|') {
                                send(Action::Input {
                                    id: format!("{id}.{suffix}"),
                                    value: InputValue::Text(value.to_string()),
                                });
                            }
                        }
                    />
                </div>
            }.into_any()
        }
        Widget::Video { url, playing, controls, looping, muted, on_ended, poster, start_at_ms, captions, rate, volume, urls, start_index, .. } => {
            // Web = a native-controls `<video>`. App-driven play/pause + seek + position/state events
            // are iOS/Android only: the web shell rebuilds the whole tree on each `update`, which would
            // reset the element ~every tick — so we don't pump those here (poster/captions/rate/volume
            // ARE declarative attributes, so they're safe). `muted && playing` → autoplay. MP4 plays
            // everywhere; HLS (.m3u8) plays natively on Safari and, on Chrome/Firefox, via the hls.js
            // bootstrap (`inject_hls_support`). A non-empty `urls` is a playlist (best-effort: starts at
            // `start_index`, advances on `ended` within this element's lifetime — no index pump back).
            use wasm_bindgen::JsCast;
            let (send, ended) = (send.clone(), on_ended.clone());
            let autoplay = *playing && *muted;
            let playlist = urls.clone();
            let start_index = (*start_index).max(0) as usize;
            let effective = if playlist.is_empty() { url.clone() }
                else { playlist.get(start_index).cloned().unwrap_or_else(|| url.clone()) };
            let is_hls = effective.to_ascii_lowercase().ends_with(".m3u8");
            let src = (!is_hls).then(|| effective.clone());
            let hls_src = is_hls.then(|| effective.clone());
            let poster_attr = poster.clone();
            let start_at = *start_at_ms;
            let rate = *rate as f64;
            let volume = (*volume as f64).clamp(0.0, 1.0);
            let tracks: Vec<_> = captions.iter().map(|c| view! {
                <track kind="subtitles" src=c.url.clone() srclang=c.language.clone() label=c.label.clone() default=c.default_on />
            }).collect();
            let next_idx = std::rc::Rc::new(std::cell::Cell::new(start_index));
            view! {
                <video
                    class="video"
                    src=src
                    data-hls-src=hls_src
                    poster=poster_attr
                    controls=*controls
                    autoplay=autoplay
                    prop:loop=*looping
                    prop:playbackRate=rate
                    prop:volume=volume
                    muted=*muted
                    playsinline=true
                    on:loadedmetadata=move |ev| {
                        if start_at >= 0 {
                            if let Some(v) = ev.target().and_then(|t| t.dyn_into::<web_sys::HtmlVideoElement>().ok()) {
                                v.set_current_time(start_at as f64 / 1000.0);
                            }
                        }
                    }
                    on:ended=move |ev| {
                        let nxt = next_idx.get() + 1;
                        if !playlist.is_empty() && nxt < playlist.len() {
                            next_idx.set(nxt);
                            if let Some(v) = ev.target().and_then(|t| t.dyn_into::<web_sys::HtmlVideoElement>().ok()) {
                                v.set_src(&playlist[nxt]);
                                let _ = v.play();
                            }
                        } else if let Some(t) = ended.clone() {
                            send(Action::Fired { token: t });
                        }
                    }
                >{tracks}</video>
            }.into_any()
        }
        Widget::Rating { value, max, on_rate } => {
            let value = *value;
            let stars: Vec<AnyView> = (1..=*max)
                .map(|i| {
                    let threshold = u32::from(i) * 10;
                    // filled / half / empty by tenths.
                    let glyph = if value >= threshold { "" } else if value + 5 >= threshold { "" } else { "" };
                    match on_rate {
                        Some(tokens) => {
                            let (send, token) = (send.clone(), tokens.get(usize::from(i - 1)).cloned().unwrap_or_default());
                            view! {
                                <button class="star star-tappable" on:click=move |_| send(Action::Fired { token: token.clone() })>
                                    {glyph}
                                </button>
                            }
                            .into_any()
                        }
                        None => view! { <span class="star">{glyph}</span> }.into_any(),
                    }
                })
                .collect();
            view! { <span class="rating">{stars}</span> }.into_any()
        }
        Widget::Divider => view! { <hr class="divider" /> }.into_any(),
        Widget::Progress { value } => match value {
            Some(v) => {
                let pct = (v.clamp(0.0, 1.0) * 100.0) as u32;
                view! { <div class="progress"><div class="progress-bar" style=format!("width:{pct}%")></div></div> }.into_any()
            }
            None => view! { <div class="progress progress-indeterminate"><div class="progress-bar"></div></div> }.into_any(),
        },
        Widget::Skeleton => view! { <div class="skeleton"></div> }.into_any(),
        Widget::Chart { series, labels, style, axis, legend } => {
            chart_view(series, labels, *style, *axis, *legend)
        }
        Widget::RegionChart { regions, ticks, x_max, y_max, ref_lines, bracket, legend } => {
            region_chart_view(regions, ticks, *x_max, *y_max, ref_lines, bracket, legend)
        }
        Widget::Calendar { title, weekday_labels, leading_blanks, selected, on_day, markers, .. } => {
            let heads: Vec<_> = weekday_labels.iter().map(|w| view! { <div class="cal-head">{w.clone()}</div> }).collect();
            let blanks: Vec<_> = (0..*leading_blanks).map(|_| view! { <div class="cal-blank"></div> }).collect();
            let selected = *selected;
            let days: Vec<_> = on_day.iter().enumerate().map(|(i, token)| {
                let day = (i + 1) as u8;
                let token = token.clone();
                let send = send.clone();
                let cls = if selected == Some(day) { "cal-day cal-sel" } else { "cal-day" };
                // 0–3 busy-dots under the number; nothing at all for level 0 / no markers.
                let level = markers.get(i).copied().unwrap_or(0).min(3);
                let dots = (level > 0).then(|| {
                    let d: Vec<_> = (0..level).map(|_| view! { <span class="cal-dot"></span> }).collect();
                    view! { <span class="cal-dots">{d}</span> }
                });
                view! { <button class=cls on:click=move |_| send(Action::Fired { token: token.clone() })>{day.to_string()}{dots}</button> }
            }).collect();
            view! {
                <div class="calendar">
                    <div class="cal-title">{title.clone()}</div>
                    <div class="cal-grid">{heads}{blanks}{days}</div>
                </div>
            }.into_any()
        }
        Widget::SwipeAction { child, actions } => {
            // Web has no swipe gesture — render the actions inline as a trailing button row.
            let acts: Vec<_> = actions.iter().map(|a| {
                let token = a.on_tap.clone();
                let send = send.clone();
                let cls = format!("swipe-act {}", tone_class(a.tone));
                let label = a.label.clone();
                view! { <button class=cls on:click=move |_| send(Action::Fired { token: token.clone() })>{label}</button> }
            }).collect();
            view! {
                <div class="swipe-row">
                    <div class="swipe-content">{render(child, send)}</div>
                    <div class="swipe-actions">{acts}</div>
                </div>
            }.into_any()
        }
        Widget::Spacer { size } => {
            view! { <div class=format!("spacer {}", spacer_class(*size))></div> }.into_any()
        }

        // ---- layout ----
        Widget::Row { children } => {
            let kids = render_all(children, send);
            view! { <div class="row">{kids}</div> }.into_any()
        }
        Widget::Column { children } => {
            let kids = render_all(children, send);
            view! { <div class="col">{kids}</div> }.into_any()
        }
        Widget::Card { child, style, on_press, on_long_press } => {
            let class = format!("card {}", card_class(*style));
            let body = render(child, send);
            match (on_press, on_long_press) {
                // Plain, non-interactive card.
                (None, None) => view! { <div class=class>{body}</div> }.into_any(),
                // Tappable and/or long-pressable — render a button with the relevant handlers.
                (tap, long) => {
                    let send = send.clone();
                    // Web has no native long-press; shim it with a pointer-hold timer (~500 ms),
                    // cancelled on pointerup/leave/cancel. A `long_fired` flag suppresses the
                    // click that follows a successful hold so it doesn't also fire the tap.
                    let timer: Rc<RefCell<Option<gloo_timers::callback::Timeout>>> =
                        Rc::new(RefCell::new(None));
                    let long_fired = Rc::new(RefCell::new(false));

                    let on_pointerdown = {
                        let (send, long, timer, long_fired) =
                            (send.clone(), long.clone(), timer.clone(), long_fired.clone());
                        move |_: web_sys::PointerEvent| {
                            let Some(token) = long.clone() else { return };
                            *long_fired.borrow_mut() = false;
                            let (send, long_fired) = (send.clone(), long_fired.clone());
                            *timer.borrow_mut() = Some(gloo_timers::callback::Timeout::new(
                                500,
                                move || {
                                    *long_fired.borrow_mut() = true;
                                    send(Action::Fired { token: token.clone() });
                                },
                            ));
                        }
                    };
                    let cancel = {
                        let timer = timer.clone();
                        // Dropping the `Timeout` cancels the pending fire.
                        move |_: web_sys::PointerEvent| { timer.borrow_mut().take(); }
                    };
                    let on_click = {
                        let (send, tap, long_fired) = (send.clone(), tap.clone(), long_fired.clone());
                        move |_| {
                            // Suppress the tap that trails a long-press.
                            if std::mem::take(&mut *long_fired.borrow_mut()) {
                                return;
                            }
                            if let Some(token) = tap.clone() {
                                send(Action::Fired { token });
                            }
                        }
                    };
                    view! {
                        <button
                            class=format!("{class} card-tappable")
                            on:pointerdown=on_pointerdown
                            on:pointerup=cancel.clone()
                            on:pointerleave=cancel.clone()
                            on:pointercancel=cancel
                            on:click=on_click
                        >
                            {body}
                        </button>
                    }
                    .into_any()
                }
            }
        }
        // Z-stack. With `scrim`, the first child is a background image, darkened
        // by an overlay, and the rest layer on top in light content — the DOM twin
        // of the Compose `matchParentSize` scrim / SwiftUI `.overlay` on the image.
        Widget::Box { children, align, scrim } => {
            let acls = align_class(*align);
            if *scrim && children.len() > 1 {
                let bg = render(&children[0], send);
                let content = render_all(&children[1..], send);
                view! {
                    <div class=format!("box box-scrim {acls}")>
                        {bg}
                        <div class="scrim"></div>
                        <div class="box-content">{content}</div>
                    </div>
                }
                .into_any()
            } else {
                let kids = render_all(children, send);
                view! { <div class=format!("box {acls}")>{kids}</div> }.into_any()
            }
        }
        Widget::Grid { children } => {
            let kids = render_all(children, send);
            view! { <div class="grid">{kids}</div> }.into_any()
        }
        Widget::Scroller { children, edge_fade } => {
            let kids = render_all(children, send);
            if *edge_fade {
                view! { <div class="scroller scroller-fade">{kids}<div class="scroller-end"></div></div> }.into_any()
            } else {
                view! { <div class="scroller">{kids}</div> }.into_any()
            }
        }
        // Two-pane master-detail. CSS does the adapting: wide (`@media min-width:768px`) shows both
        // panes side-by-side (back hidden); narrow shows one — primary by default, or detail (+ a
        // back chevron) when `data-detail` is set. `show_detail`/`on_back` only matter when narrow.
        Widget::Split { primary, detail, show_detail, on_back } => {
            let p = render(primary, send);
            let d = render(detail, send);
            let back_text = format!("{}", shell_label(|l| l.back.clone(), "Back"));
            let back_btn = on_back.clone().map(|t| {
                let send = send.clone();
                view! { <button class="split-back" on:click=move |_| send(Action::Fired { token: t.clone() })>{back_text}</button> }
            });
            view! {
                <div class="split" data-detail=show_detail.then_some("1")>
                    <div class="split-primary">{p}</div>
                    <div class="split-detail">{back_btn}{d}</div>
                </div>
            }.into_any()
        }
        // Accessibility wrapper: name the subtree for a screen reader (aria-label), give it a role,
        // and the hint via `title`. Best-effort web mapping of iOS traits / Android semantics.
        Widget::A11y { child, label, hint, role } => {
            let body = render(child, send);
            let role_attr = role.map(a11y_role_aria).unwrap_or("group");
            view! {
                <div class="a11y" role=role_attr aria-label=label.clone() title=hint.clone()>
                    {body}
                </div>
            }.into_any()
        }
        // A long/paged feed. Web has no pull gesture or reliable infinite-scroll on a sub-container,
        // so (like Scaffold pull-to-refresh) the gestures degrade to controls: a top "↻ Refresh"
        // button (while `on_refresh`), and a bottom "Load more" button (while `has_more && !loading`)
        // / loading bar / the app's end caption (if set). iOS/Android do true pull + scroll-near-end
        // detection.
        Widget::LazyList { children, on_load_more, loading, has_more, on_refresh, refreshing, end_label } => {
            let kids = render_all(children, send);
            let refresh_text = format!("{}", shell_label(|l| l.refresh.clone(), "Refresh"));
            let refresh_btn = on_refresh.clone().map(|token| {
                let send = send.clone();
                view! { <button class="refresh-btn" on:click=move |_| send(Action::Fired { token: token.clone() })>{refresh_text}</button> }
            });
            let refresh_bar = refreshing.then(|| view! { <div class="progress progress-indeterminate"><div class="progress-bar"></div></div> });
            let loading_bar = loading.then(|| view! { <div class="progress progress-indeterminate"><div class="progress-bar"></div></div> });
            let load_more_text = shell_label(|l| l.load_more.clone(), "Load more");
            let load_more_btn = (!*loading && *has_more)
                .then(|| on_load_more.clone())
                .flatten()
                .map(|token| {
                    let send = send.clone();
                    view! { <button class="btn btn-outlined lazylist-more" on:click=move |_| send(Action::Fired { token: token.clone() })>{load_more_text}</button> }
                });
            // The app's own end text (e.g. "Kraj liste"); nothing when it didn't set one.
            let end_cap = (!*has_more && on_load_more.is_some())
                .then(|| end_label.clone())
                .flatten()
                .map(|label| view! { <div class="lazylist-end">{label}</div> });
            view! {
                <div class="lazylist">
                    {refresh_btn}
                    {refresh_bar}
                    {kids}
                    {loading_bar}
                    {load_more_btn}
                    {end_cap}
                </div>
            }.into_any()
        }

        // ---- input / actions ----
        Widget::Button { label, style, on_press, tone, icon, wide } => {
            let (send, token, label) = (send.clone(), on_press.clone(), label.clone());
            // Neutral + not wide keeps the exact original class string.
            let mut class = format!("btn {}", button_class(*style));
            if *tone != Tone::Neutral {
                class.push(' ');
                class.push_str(button_tone_class(*tone));
            }
            if *wide {
                class.push_str(" btn-wide");
            }
            let glyph = icon.map(|i| view! { <span class="btn-icon">{icon_glyph(i)}</span> });
            view! {
                <button class=class on:click=move |_| send(Action::Fired { token: token.clone() })>
                    {glyph}
                    {label}
                </button>
            }
            .into_any()
        }
        Widget::IconButton { icon, on_press } => {
            let (send, token) = (send.clone(), on_press.clone());
            let glyph = icon_glyph(*icon);
            view! {
                <button class="iconbtn" on:click=move |_| send(Action::Fired { token: token.clone() })>
                    {glyph}
                </button>
            }
            .into_any()
        }
        Widget::Chip { label, selected, on_press } => {
            let (send, token, label) = (send.clone(), on_press.clone(), label.clone());
            let class = if *selected { "chip selected" } else { "chip" };
            view! {
                <button class=class on:click=move |_| send(Action::Fired { token: token.clone() })>
                    {label}
                </button>
            }
            .into_any()
        }
        Widget::TextField { id, placeholder, value, kind, error } => {
            let (send, id) = (send.clone(), id.clone());
            let (placeholder, value) = (placeholder.clone(), value.clone());
            let invalid = error.is_some();
            let err_view = error.clone().map(|m| view! { <div class="field-error">{m}</div> });
            // (input type, inputmode) per FieldKind. Multiline renders a <textarea> below.
            let (itype, imode): (&str, &str) = match kind {
                FieldKind::Secure => ("password", ""),
                FieldKind::Email => ("email", "email"),
                FieldKind::Number => ("text", "numeric"),
                FieldKind::Decimal => ("text", "decimal"),
                FieldKind::Phone => ("tel", "tel"),
                FieldKind::Url => ("url", "url"),
                FieldKind::Text | FieldKind::Multiline => ("text", ""),
            };
            let field_class = if invalid { "field field-invalid" } else { "field" };
            let control = if matches!(kind, FieldKind::Multiline) {
                view! {
                    <textarea
                        class=field_class
                        rows="3"
                        placeholder=placeholder
                        prop:value=value
                        on:input=move |ev| send(Action::Input {
                            id: id.clone(),
                            value: InputValue::Text(event_target_value(&ev)),
                        })
                    ></textarea>
                }
                .into_any()
            } else {
                view! {
                    <input
                        class=field_class
                        r#type=itype
                        inputmode=imode
                        placeholder=placeholder
                        prop:value=value
                        on:input=move |ev| send(Action::Input {
                            id: id.clone(),
                            value: InputValue::Text(event_target_value(&ev)),
                        })
                    />
                }
                .into_any()
            };
            view! { <div class="field-wrap">{control}{err_view}</div> }.into_any()
        }
        Widget::SearchField { id, placeholder, value } => {
            let (send, id) = (send.clone(), id.clone());
            let (placeholder, value) = (placeholder.clone(), value.clone());
            view! {
                <div class="searchfield">
                    <span class="search-icon">{icon_glyph(Icon::Search)}</span>
                    <input
                        class="search-input"
                        placeholder=placeholder
                        prop:value=value
                        on:input=move |ev| send(Action::Input {
                            id: id.clone(),
                            value: InputValue::Text(event_target_value(&ev)),
                        })
                    />
                </div>
            }
            .into_any()
        }
        Widget::Segmented { segments } => {
            let segs: Vec<AnyView> = segments
                .iter()
                .map(|s| {
                    let (send, token) = (send.clone(), s.on_select.clone());
                    let class = if s.selected { "segment selected" } else { "segment" };
                    let label = s.label.clone();
                    view! {
                        <button class=class on:click=move |_| send(Action::Fired { token: token.clone() })>
                            {label}
                        </button>
                    }
                    .into_any()
                })
                .collect();
            view! { <div class="segmented">{segs}</div> }.into_any()
        }
        Widget::Toggle { id, label, value } => {
            let (send, id, label, checked) = (send.clone(), id.clone(), label.clone(), *value);
            view! {
                <label class="toggle">
                    {label}
                    <input
                        type="checkbox"
                        role="switch"
                        prop:checked=checked
                        on:change=move |ev| send(Action::Input {
                            id: id.clone(),
                            value: InputValue::Bool(event_target_checked(&ev)),
                        })
                    />
                </label>
            }
            .into_any()
        }
        Widget::Checkbox { id, label, value } => {
            let (send, id, label, checked) = (send.clone(), id.clone(), label.clone(), *value);
            view! {
                <label class="check">
                    <input
                        type="checkbox"
                        prop:checked=checked
                        on:change=move |ev| send(Action::Input {
                            id: id.clone(),
                            value: InputValue::Bool(event_target_checked(&ev)),
                        })
                    />
                    {label}
                </label>
            }
            .into_any()
        }
        Widget::Slider { id, value, max } => {
            let (send, id, value, max) = (send.clone(), id.clone(), *value, *max);
            view! {
                <input
                    class="slider"
                    type="range"
                    min="0"
                    max=max
                    prop:value=value
                    on:input=move |ev| send(Action::Input {
                        id: id.clone(),
                        value: InputValue::Int(event_target_value(&ev).parse().unwrap_or(0)),
                    })
                />
            }
            .into_any()
        }
        Widget::Stepper { value, on_decrement, on_increment } => {
            let send_dec = send.clone();
            let send_inc = send.clone();
            let (dec, inc) = (on_decrement.clone(), on_increment.clone());
            view! {
                <div class="stepper">
                    <button on:click=move |_| send_dec(Action::Fired { token: dec.clone() })>""</button>
                    <span class="stepper-value">{*value}</span>
                    <button on:click=move |_| send_inc(Action::Fired { token: inc.clone() })>"+"</button>
                </div>
            }
            .into_any()
        }

        // ---- shell ----
        Widget::Scaffold { title, body, tabs, back, dark_mode, theme, fab, sheet, on_refresh, refreshing, route, depth, labels: _ } => {
            // ACTIVE_LABELS is stashed once at the root render closure (from the root widget's
            // own `labels`), not here — a Scaffold nested in a sheet, body or Split must not
            // overwrite the root's labels. This arm's own aria-label reads below still see the
            // root labels, since the stash is set before render() is called.
            let back_aria = shell_label(|l| l.back.clone(), "Back");
            let back_btn = back.clone().map(|token| {
                let send = send.clone();
                view! {
                    <button class="back" aria-label=back_aria on:click=move |_| send(Action::Fired { token: token.clone() })>
                        ""
                    </button>
                }
            });
            let tabbar = (!tabs.is_empty()).then(|| {
                let tabs: Vec<AnyView> = tabs
                    .iter()
                    .map(|tab| {
                        let (send, token) = (send.clone(), tab.on_select.clone());
                        let class = if tab.selected { "tab selected" } else { "tab" };
                        let label = tab.label.clone();
                        // Optional leading icon → glyph above the label (icon tab bar).
                        let icon = tab.icon.map(|i| view! { <span class="tab-icon">{icon_glyph(i)}</span> });
                        view! {
                            <button class=class on:click=move |_| send(Action::Fired { token: token.clone() })>
                                {icon}
                                <span class="tab-label">{label}</span>
                            </button>
                        }
                        .into_any()
                    })
                    .collect();
                view! { <div class="tabbar">{tabs}</div> }
            });
            // Floating action button — the raised primary action, anchored over the body.
            let fab_btn = fab.clone().map(|f| {
                let (send, token) = (send.clone(), f.on_press.clone());
                view! {
                    <button class="fab" on:click=move |_| send(Action::Fired { token: token.clone() })>
                        {icon_glyph(f.icon)}
                    </button>
                }
            });
            // Modal bottom sheet — a scrim (tap to dismiss) + a panel rising from the bottom.
            let sheet_overlay = sheet.as_ref().map(|s| {
                let (send_scrim, dismiss) = (send.clone(), s.on_dismiss.clone());
                let (title, child) = (s.title.clone(), render(&s.child, send));
                view! {
                    <div class="sheet-scrim" on:click=move |_| send_scrim(Action::Fired { token: dismiss.clone() })></div>
                    <div class="sheet">
                        <div class="sheet-handle"></div>
                        <div class="sheet-title">{title}</div>
                        {child}
                    </div>
                }
            });
            // `theme-dark` flips the CSS variables for the whole shell — theme-as-data,
            // the web twin of the native shells' `preferredColorScheme`/Material theme.
            // `density-large` scopes the Density::Large control sizes in mobiler.css.
            let large = theme.as_ref().is_some_and(|t| t.density == Density::Large);
            let class = format!("scaffold{}{}", if *dark_mode { " theme-dark" } else { "" }, if large { " density-large" } else { "" });
            // Pull-to-refresh — web has no pull gesture, so expose a top-bar refresh button +
            // an indeterminate bar at the top of the body while `refreshing`.
            let refresh_aria = shell_label(|l| l.refresh.clone(), "Refresh");
            let refresh_btn = on_refresh.clone().map(|token| {
                let send = send.clone();
                view! {
                    <button class="refresh-btn" aria-label=refresh_aria on:click=move |_| send(Action::Fired { token: token.clone() })>""</button>
                }
            });
            let refresh_bar = refreshing.then(|| {
                view! { <div class="progress progress-indeterminate"><div class="progress-bar"></div></div> }
            });
            let body_class = format!("scaffold-body {}", nav_class(route, *depth));
            // An app `Theme` overrides the CSS variables inline (brand color, corner, density,
            // font) — the web twin of the native shells' brand/tint + shape + spacing + font.
            let theme_style = theme.as_ref().map(theme_css).unwrap_or_default();
            let (title, body) = (title.clone(), render(body, send));
            view! {
                <div class=class style=theme_style>
                    <div class="topbar">
                        {back_btn}
                        <span class="title">{title}</span>
                        {refresh_btn}
                    </div>
                    <div class=body_class data-route=route.clone()>{refresh_bar}{body}</div>
                    {fab_btn}
                    {tabbar}
                    {sheet_overlay}
                </div>
            }
            .into_any()
        }
    }
}

/// Render a slice of children as sibling views.
fn render_all(children: &[Widget], send: &Dispatch) -> Vec<AnyView> {
    children.iter().map(|c| render(c, send)).collect()
}

thread_local! {
    /// (previous route key, previous depth, alternating toggle). The render is a
    /// stateless whole-tree rebuild, so nav state lives here (wasm is single-
    /// threaded). Lets the Scaffold body animate on navigation — the web twin of
    /// the native shells keying their body on `route`.
    static NAV: RefCell<(String, u32, bool)> = const { RefCell::new((String::new(), 0, false)) };

    /// Open streaming subscriptions keyed by subscription key (wasm is single-
    /// threaded). Each [`Effect::PluginStream`] parks its source here so
    /// `cx.unsubscribe(key)` can stop it; dropping the entry stops the source.
    static STREAMS: RefCell<HashMap<String, StreamHandle>> = RefCell::new(HashMap::new());

    /// The current scaffold's app-wide shell text, set once in the root render closure from the
    /// root widget and read by code that never sees the view (the confirm modal). `None` ⇒
    /// English defaults.
    static ACTIVE_LABELS: std::cell::RefCell<Option<ShellLabels>> = const { std::cell::RefCell::new(None) };
}

/// The app's label for a piece of shell text, or `default`. An empty string (an app that set
/// the field to `""`) falls back to `default` too, same as the other shells.
fn shell_label(pick: impl Fn(&ShellLabels) -> Option<String>, default: &str) -> String {
    ACTIVE_LABELS.with(|l| {
        l.borrow()
            .as_ref()
            .and_then(pick)
            .filter(|s| !s.is_empty())
            .unwrap_or_else(|| default.to_string())
    })
}

/// Render an app [`Theme`] as inline CSS custom properties on the scaffold root — the web
/// twin of the native brand/tint + shape + spacing + font. Overrides `mobiler.css`'s defaults
/// (its rules read these via `var(--…)`); dark mode still works (it only swaps the colors the
/// seed doesn't pin).
fn theme_css(t: &Theme) -> String {
    let (r, g, b) = (t.seed.r, t.seed.g, t.seed.b);
    let radius = match t.corner {
        Corner::None => "0px",
        Corner::Small => "8px",
        Corner::Medium => "14px",
        Corner::Large => "22px",
    };
    let (gap, pad) = match t.density {
        Density::Compact => ("8px", "10px"),
        Density::Comfortable => ("12px", "14px"),
        Density::Large => ("16px", "18px"),
    };
    let font = match t.font {
        FontFamily::System => "system-ui, -apple-system, \"Segoe UI\", Roboto, sans-serif",
        FontFamily::Rounded => "ui-rounded, \"SF Pro Rounded\", \"Segoe UI\", system-ui, sans-serif",
        FontFamily::Serif => "ui-serif, Georgia, \"Times New Roman\", serif",
        FontFamily::Monospace => "ui-monospace, \"SF Mono\", \"Cascadia Code\", Menlo, monospace",
    };
    // Secondary brand color (for the CardStyle::Brand gradient); falls back to the seed.
    let (ar, ag, ab) = t.accent.map_or((r, g, b), |a| (a.r, a.g, a.b));
    format!(
        "--primary:rgb({r},{g},{b});--accent:rgb({r},{g},{b});\
         --accent2:rgb({ar},{ag},{ab});\
         --accent-soft:rgba({r},{g},{b},0.16);--radius:{radius};\
         --gap:{gap};--pad:{pad};--font:{font};"
    )
}

/// Pick the Scaffold body's transition class for this render. Returns `""` for a
/// same-route data update (re-render in place, no transition). On a route change it
/// returns a directional class — slide-in from the right when `depth` grew (push),
/// from the left when it shrank (pop), a crossfade for a lateral move — and *alternates*
/// the `-a`/`-b` suffix each navigation so the CSS animation restarts even though
/// Leptos reuses the same DOM node.
fn nav_class(route: &str, depth: u32) -> &'static str {
    NAV.with_borrow_mut(|(prev_route, prev_depth, toggle)| {
        if route == prev_route {
            return "";
        }
        let dir = if depth > *prev_depth {
            ["nav-push-a", "nav-push-b"]
        } else if depth < *prev_depth {
            ["nav-pop-a", "nav-pop-b"]
        } else {
            ["nav-fade-a", "nav-fade-b"]
        };
        *toggle = !*toggle;
        *prev_route = route.to_string();
        *prev_depth = depth;
        dir[usize::from(*toggle)]
    })
}

// ---- style intent → CSS class / glyph (the only place that names the look) ----

fn text_class(s: TextStyle) -> &'static str {
    match s {
        TextStyle::Title => "t-title",
        TextStyle::Subtitle => "t-subtitle",
        TextStyle::Caption => "t-caption",
        TextStyle::Emphasis => "t-emphasis",
        TextStyle::Body => "t-body",
    }
}

fn button_class(s: ButtonStyle) -> &'static str {
    match s {
        ButtonStyle::Filled => "btn-filled",
        ButtonStyle::Outlined => "btn-outlined",
        ButtonStyle::Text => "btn-text",
        ButtonStyle::Tonal => "btn-tonal",
    }
}

fn button_tone_class(t: Tone) -> &'static str {
    match t {
        Tone::Neutral => "",
        Tone::Success => "btn-success",
        Tone::Warning => "btn-warning",
        Tone::Danger => "btn-danger",
        Tone::Info => "btn-info",
    }
}

fn card_class(s: CardStyle) -> &'static str {
    match s {
        CardStyle::Elevated => "card-elevated",
        CardStyle::Outlined => "card-outlined",
        CardStyle::Filled => "card-filled",
        CardStyle::Brand => "card-brand",
    }
}

fn a11y_role_aria(role: A11yRole) -> &'static str {
    match role {
        A11yRole::Button => "button",
        A11yRole::Link => "link",
        A11yRole::Image => "img",
        A11yRole::Header => "heading",
        A11yRole::Adjustable => "slider",
    }
}

fn tone_class(t: Tone) -> &'static str {
    match t {
        Tone::Neutral => "tone-neutral",
        Tone::Success => "tone-success",
        Tone::Warning => "tone-warning",
        Tone::Danger => "tone-danger",
        Tone::Info => "tone-info",
    }
}

fn spacer_class(s: Spacing) -> &'static str {
    match s {
        Spacing::Xs => "sp-xs",
        Spacing::Sm => "sp-sm",
        Spacing::Md => "sp-md",
        Spacing::Lg => "sp-lg",
        Spacing::Xl => "sp-xl",
    }
}

fn icon_glyph(i: Icon) -> &'static str {
    match i {
        Icon::Delete => "🗑",
        Icon::Add => "",
        Icon::Edit => "✏️",
        Icon::Close => "",
        Icon::Settings => "",
        Icon::Check => "",
        Icon::Star => "",
        Icon::Info => "",
        Icon::Home => "",
        Icon::Search => "🔍",
        Icon::Menu => "",
        Icon::Filter => "",
        Icon::Back => "",
        Icon::Forward => "",
        Icon::Down => "",
        Icon::Bell => "🔔",
        Icon::Cart => "🛒",
        Icon::Share => "",
        Icon::Heart => "",
        Icon::HeartFilled => "",
        Icon::Person => "👤",
        Icon::People => "👥",
        Icon::Phone => "📞",
        Icon::Mail => "",
        Icon::Calendar => "📅",
        Icon::Clock => "🕑",
        Icon::MapPin => "📍",
        Icon::Camera => "📷",
        Icon::Photo => "🖼",
        Icon::Play => "",
        Icon::Scissors => "",
    }
}

fn image_class(shape: ImageShape, ratio: ImageRatio) -> String {
    let shape = match shape {
        ImageShape::Square => "img-square",
        ImageShape::Rounded => "img-rounded",
        ImageShape::Circle => "img-circle",
    };
    let ratio = match ratio {
        ImageRatio::Wide => "ratio-wide",
        ImageRatio::Square => "ratio-square",
        ImageRatio::Tall => "ratio-tall",
    };
    format!("img {shape} {ratio}")
}

fn dot_class(c: ProjectColor) -> &'static str {
    match c {
        ProjectColor::Indigo => "dot-indigo",
        ProjectColor::Teal => "dot-teal",
        ProjectColor::Coral => "dot-coral",
        ProjectColor::Amber => "dot-amber",
        ProjectColor::Lime => "dot-lime",
        ProjectColor::Pink => "dot-pink",
    }
}

fn align_class(a: BoxAlign) -> &'static str {
    match a {
        BoxAlign::TopStart => "align-top-start",
        BoxAlign::TopEnd => "align-top-end",
        BoxAlign::Center => "align-center",
        BoxAlign::BottomStart => "align-bottom-start",
        BoxAlign::BottomCenter => "align-bottom-center",
        BoxAlign::BottomEnd => "align-bottom-end",
    }
}

// ------------------------------- charts -------------------------------

/// Distinct fallback colors for series 1.. (series 0 with no override rides the theme accent).
const CHART_PALETTE: [&str; 6] = ["#E0772C", "#2EA06A", "#C0466B", "#8A5CC0", "#C9A227", "#3FA7D6"];

fn hex(c: Rgb) -> String {
    format!("#{:02x}{:02x}{:02x}", c.r, c.g, c.b)
}

/// Color for series `i`: explicit override → theme accent (i==0) → palette.
fn chart_color(i: usize, s: &ChartSeries) -> String {
    match s.color {
        Some(c) => hex(c),
        None if i == 0 => "var(--accent, #5C6BC0)".to_string(),
        None => CHART_PALETTE[(i - 1) % CHART_PALETTE.len()].to_string(),
    }
}

/// A series' single magnitude for circular charts (sum of its values).
fn chart_mag(s: &ChartSeries) -> f32 {
    s.values.iter().copied().sum()
}

/// Point on a circle: `ang` in radians, 0 = top (12 o'clock), increasing clockwise.
fn polar(cx: f32, cy: f32, r: f32, ang: f32) -> (f32, f32) {
    (cx + r * ang.sin(), cy - r * ang.cos())
}

/// An open arc path (for ring/donut/gauge strokes).
fn arc_path(cx: f32, cy: f32, r: f32, a0: f32, a1: f32) -> String {
    let (x0, y0) = polar(cx, cy, r, a0);
    let (x1, y1) = polar(cx, cy, r, a1);
    let large = if (a1 - a0).abs() > std::f32::consts::PI { 1 } else { 0 };
    format!("M {x0:.2} {y0:.2} A {r:.2} {r:.2} 0 {large} 1 {x1:.2} {y1:.2}")
}

/// A filled wedge from the center (for pie/donut slices).
fn wedge_path(cx: f32, cy: f32, r: f32, a0: f32, a1: f32) -> String {
    let (x0, y0) = polar(cx, cy, r, a0);
    let (x1, y1) = polar(cx, cy, r, a1);
    let large = if (a1 - a0).abs() > std::f32::consts::PI { 1 } else { 0 };
    format!("M {cx:.2} {cy:.2} L {x0:.2} {y0:.2} A {r:.2} {r:.2} 0 {large} 1 {x1:.2} {y1:.2} Z")
}

fn fmt_tick(v: f32) -> String {
    if (v - v.round()).abs() < 0.05 { format!("{}", v.round() as i64) } else { format!("{v:.1}") }
}

fn is_cartesian(style: ChartStyle) -> bool {
    matches!(style, ChartStyle::Bar | ChartStyle::Line | ChartStyle::StackedBar | ChartStyle::StackedBar100)
}

/// The y-axis denominator for a cartesian chart.
fn cartesian_max(series: &[ChartSeries], style: ChartStyle, nslots: usize) -> f32 {
    match style {
        ChartStyle::StackedBar => (0..nslots)
            .map(|j| series.iter().map(|s| *s.values.get(j).unwrap_or(&0.0)).sum::<f32>())
            .fold(0.0, f32::max)
            .max(1e-6),
        ChartStyle::StackedBar100 => 1.0,
        _ => series.iter().flat_map(|s| s.values.iter().copied()).fold(0.0, f32::max).max(1e-6),
    }
}

fn cartesian_svg(series: &[ChartSeries], style: ChartStyle, axis: bool, max: f32, nslots: usize) -> AnyView {
    // plot area: y in [2, 48] of the 0..50 viewBox
    let mut nodes: Vec<AnyView> = Vec::new();
    if axis {
        for k in 0..=4 {
            let y = 2.0 + k as f32 * (46.0 / 4.0);
            nodes.push(view! { <line x1="0" y1=format!("{y:.2}") x2="100" y2=format!("{y:.2}") class="chart-gridline"></line> }.into_any());
        }
    }
    match style {
        ChartStyle::Line => {
            for (i, s) in series.iter().enumerate() {
                let n = s.values.len().max(1);
                let pts = s.values.iter().enumerate().map(|(j, v)| {
                    let x = if n == 1 { 50.0 } else { j as f32 * (100.0 / (n as f32 - 1.0)) };
                    let y = 2.0 + (1.0 - (v / max).clamp(0.0, 1.0)) * 46.0;
                    format!("{x:.2},{y:.2}")
                }).collect::<Vec<_>>().join(" ");
                let st = format!("fill:none;stroke:{};stroke-width:1.5;vector-effect:non-scaling-stroke", chart_color(i, s));
                nodes.push(view! { <polyline points=pts style=st></polyline> }.into_any());
            }
        }
        ChartStyle::Bar => {
            let sw = 100.0 / nslots as f32;
            let ns = series.len().max(1);
            for (i, s) in series.iter().enumerate() {
                let st = format!("fill:{}", chart_color(i, s));
                for (j, v) in s.values.iter().enumerate() {
                    let h = (v / max).clamp(0.0, 1.0) * 46.0;
                    let bw = sw * 0.8 / ns as f32;
                    let x = j as f32 * sw + sw * 0.1 + i as f32 * bw;
                    let y = 48.0 - h;
                    nodes.push(view! { <rect x=format!("{x:.2}") y=format!("{y:.2}") width=format!("{bw:.2}") height=format!("{h:.2}") style=st.clone()></rect> }.into_any());
                }
            }
        }
        ChartStyle::StackedBar | ChartStyle::StackedBar100 => {
            let sw = 100.0 / nslots as f32;
            for j in 0..nslots {
                let slot_total = series.iter().map(|s| *s.values.get(j).unwrap_or(&0.0)).sum::<f32>().max(1e-6);
                let denom = if matches!(style, ChartStyle::StackedBar100) { slot_total } else { max };
                let mut acc = 0.0_f32;
                for (i, s) in series.iter().enumerate() {
                    let v = *s.values.get(j).unwrap_or(&0.0);
                    let h = (v / denom).clamp(0.0, 1.0) * 46.0;
                    let x = j as f32 * sw + sw * 0.15;
                    let bw = sw * 0.7;
                    let y = 48.0 - acc - h;
                    let st = format!("fill:{}", chart_color(i, s));
                    nodes.push(view! { <rect x=format!("{x:.2}") y=format!("{y:.2}") width=format!("{bw:.2}") height=format!("{h:.2}") style=st></rect> }.into_any());
                    acc += h;
                }
            }
        }
        _ => {}
    }
    view! { <svg viewBox="0 0 100 50" preserveAspectRatio="none" class="chart-svg">{nodes}</svg> }.into_any()
}

fn circular_svg(series: &[ChartSeries], style: ChartStyle) -> AnyView {
    use std::f32::consts::PI;
    let mut nodes: Vec<AnyView> = Vec::new();
    match style {
        ChartStyle::Pie | ChartStyle::Donut => {
            let total = series.iter().map(chart_mag).sum::<f32>().max(1e-6);
            let mut a = 0.0_f32;
            for (i, s) in series.iter().enumerate() {
                let frac = chart_mag(s) / total;
                let st = format!("fill:{}", chart_color(i, s));
                if frac >= 0.999 {
                    nodes.push(view! { <circle cx="50" cy="50" r="45" style=st></circle> }.into_any());
                } else if frac > 0.0 {
                    let d = wedge_path(50.0, 50.0, 45.0, a, a + frac * 2.0 * PI);
                    nodes.push(view! { <path d=d style=st></path> }.into_any());
                }
                a += frac * 2.0 * PI;
            }
            if matches!(style, ChartStyle::Donut) {
                nodes.push(view! { <circle cx="50" cy="50" r="24" style="fill:var(--surface, #ffffff)"></circle> }.into_any());
            }
        }
        ChartStyle::Rings => {
            let n = series.len().max(1);
            for (i, s) in series.iter().enumerate() {
                let r = 45.0 - i as f32 * (34.0 / n as f32);
                let goal = s.goal.unwrap_or_else(|| chart_mag(s)).max(1e-6);
                let prog = (chart_mag(s) / goal).clamp(0.0, 1.0);
                nodes.push(view! { <circle cx="50" cy="50" r=format!("{r:.2}") style="fill:none;stroke:var(--border, #e6e6e6);stroke-width:6"></circle> }.into_any());
                let st = format!("fill:none;stroke:{};stroke-width:6;stroke-linecap:round", chart_color(i, s));
                if prog >= 0.999 {
                    nodes.push(view! { <circle cx="50" cy="50" r=format!("{r:.2}") style=st></circle> }.into_any());
                } else if prog > 0.0 {
                    let d = arc_path(50.0, 50.0, r, 0.0, prog * 2.0 * PI);
                    nodes.push(view! { <path d=d style=st></path> }.into_any());
                }
            }
        }
        ChartStyle::Gauge => {
            let s = match series.first() { Some(s) => s, None => return view! { <svg viewBox="0 0 100 100" class="chart-svg"></svg> }.into_any() };
            let goal = s.goal.unwrap_or_else(|| chart_mag(s)).max(1e-6);
            let prog = (chart_mag(s) / goal).clamp(0.0, 1.0);
            let a0 = -0.75 * PI; // 270° sweep, gap at the bottom
            let a1 = 0.75 * PI;
            nodes.push(view! { <path d=arc_path(50.0, 50.0, 42.0, a0, a1) style="fill:none;stroke:var(--border, #e6e6e6);stroke-width:8;stroke-linecap:round"></path> }.into_any());
            if prog > 0.0 {
                let st = format!("fill:none;stroke:{};stroke-width:8;stroke-linecap:round", chart_color(0, s));
                nodes.push(view! { <path d=arc_path(50.0, 50.0, 42.0, a0, a0 + prog * 1.5 * PI) style=st></path> }.into_any());
            }
            let pct = format!("{}%", (prog * 100.0).round() as i64);
            nodes.push(view! { <text x="50" y="56" style="fill:var(--fg, #222);font-size:20px;font-weight:700;text-anchor:middle">{pct}</text> }.into_any());
        }
        _ => {}
    }
    view! { <svg viewBox="0 0 100 100" preserveAspectRatio="xMidYMid meet" class="chart-svg">{nodes}</svg> }.into_any()
}

fn chart_view(series: &[ChartSeries], labels: &[String], style: ChartStyle, axis: bool, legend: bool) -> AnyView {
    let cartesian = is_cartesian(style);
    let nslots = series.iter().map(|s| s.values.len()).max().unwrap_or(0).max(1);
    let max = cartesian_max(series, style, nslots);

    let plot = if cartesian {
        let svg = cartesian_svg(series, style, axis, max, nslots);
        let yaxis = if axis {
            let ticks: Vec<_> = [max, max / 2.0, 0.0].iter()
                .map(|t| view! { <span class="chart-tick">{fmt_tick(*t)}</span> })
                .collect();
            Some(view! { <div class="chart-yaxis">{ticks}</div> })
        } else {
            None
        };
        view! { <div class="chart-plot">{yaxis}{svg}</div> }.into_any()
    } else {
        circular_svg(series, style).into_any()
    };

    let label_row = if cartesian && !labels.is_empty() {
        let items: Vec<_> = labels.iter().map(|l| view! { <span class="chart-label">{l.clone()}</span> }).collect();
        Some(view! { <div class="chart-labels">{items}</div> })
    } else {
        None
    };

    let legend_row = if legend {
        let items: Vec<_> = series.iter().enumerate().map(|(i, s)| {
            let sw = format!("background:{}", chart_color(i, s));
            let name = s.name.clone();
            view! { <span class="chart-legend-item"><span class="chart-swatch" style=sw></span>{name}</span> }
        }).collect();
        Some(view! { <div class="chart-legend">{items}</div> })
    } else {
        None
    };

    view! { <div class="chart">{plot}{label_row}{legend_row}</div> }.into_any()
}

// --------------------------- region chart ---------------------------

/// Palette as RGB (parallel to `CHART_PALETTE`) so region charts can compute label contrast.
const CHART_PALETTE_RGB: [(u8, u8, u8); 6] =
    [(0xE0, 0x77, 0x2C), (0x2E, 0xA0, 0x6A), (0xC0, 0x46, 0x6B), (0x8A, 0x5C, 0xC0), (0xC9, 0xA2, 0x27), (0x3F, 0xA7, 0xD6)];

/// The resolved fill RGB for region `i` (explicit override → palette).
fn region_rgb(i: usize, r: &ChartRegion) -> (u8, u8, u8) {
    match r.color {
        Some(c) => (c.r, c.g, c.b),
        None => CHART_PALETTE_RGB[i % CHART_PALETTE_RGB.len()],
    }
}

/// Black or white label text, whichever reads on the given fill (perceived luminance).
fn contrast_text((r, g, b): (u8, u8, u8)) -> &'static str {
    let lum = 0.299 * r as f32 + 0.587 * g as f32 + 0.114 * b as f32;
    if lum > 140.0 { "#1a1a1a" } else { "#f5f5f5" }
}

fn region_color(i: usize, r: &ChartRegion) -> String {
    let (r8, g8, b8) = region_rgb(i, r);
    format!("#{r8:02x}{g8:02x}{b8:02x}")
}

// A variable-width stacked-region / coverage-gap chart: absolute-positioned region rectangles in
// the [0,x_max]×[0,y_max] plane, horizontal ref lines + chips, an irregular x-axis, an optional
// right-side bracket, and a legend. The web twin of the Compose/SwiftUI RegionChart renderers.
fn region_chart_view(
    regions: &[ChartRegion],
    ticks: &[ChartTick],
    x_max: f32,
    y_max: f32,
    ref_lines: &[ChartRefLine],
    bracket: &Option<ChartBracket>,
    legend: &[ChartLegendItem],
) -> AnyView {
    let xm = x_max.max(1e-6);
    let ym = y_max.max(1e-6);

    let region_divs: Vec<_> = regions.iter().enumerate().map(|(i, r)| {
        let left = (r.x0 / xm * 100.0).clamp(0.0, 100.0);
        let width = ((r.x1 - r.x0) / xm * 100.0).clamp(0.0, 100.0);
        let bottom = (r.y0 / ym * 100.0).clamp(0.0, 100.0);
        let height = ((r.y1 - r.y0) / ym * 100.0).clamp(0.0, 100.0);
        let style = format!("left:{left:.3}%;width:{width:.3}%;bottom:{bottom:.3}%;height:{height:.3}%;background:{}", region_color(i, r));
        let label_class = if r.vertical { "rchart-label rchart-label-v" } else { "rchart-label" };
        let label_style = format!("color:{}", contrast_text(region_rgb(i, r)));
        let label = r.label.clone();
        view! { <div class="rchart-region" style=style><span class=label_class style=label_style>{label}</span></div> }
    }).collect();

    // The reference lines span the full plot width; their value chips sit in the right margin
    // (outside the plot), like the original — so the line clearly runs to the plot's edge.
    let ref_line_divs: Vec<_> = ref_lines.iter().map(|rl| {
        let style = format!("bottom:{:.3}%", (rl.value / ym * 100.0).clamp(0.0, 100.0));
        let cls = if rl.dashed { "rchart-refline rchart-refline-dashed" } else { "rchart-refline" };
        view! { <div class=cls style=style></div> }
    }).collect();
    let chip_divs: Vec<_> = ref_lines.iter().map(|rl| {
        let style = format!("bottom:{:.3}%", (rl.value / ym * 100.0).clamp(0.0, 100.0));
        let label = rl.label.clone();
        view! { <div class="rchart-chip" style=style>{label}</div> }
    }).collect();

    let bracket_div = bracket.as_ref().map(|b| {
        let bottom = (b.y0 / ym * 100.0).clamp(0.0, 100.0);
        let height = ((b.y1 - b.y0) / ym * 100.0).clamp(0.0, 100.0);
        let style = format!("bottom:{bottom:.3}%;height:{height:.3}%");
        let label = if b.info { format!("\n{}", b.label) } else { b.label.clone() };
        view! { <div class="rchart-bracket" style=style><span>{label}</span></div> }
    });

    let yticks: Vec<_> = (0..=4).rev().map(|k| {
        let v = ym * k as f32 / 4.0;
        view! { <span class="chart-tick">{fmt_tick(v)}</span> }
    }).collect();

    let xticks: Vec<_> = ticks.iter().map(|t| {
        let style = format!("left:{:.3}%", (t.at / xm * 100.0).clamp(0.0, 100.0));
        let label = t.label.clone();
        view! { <span class="rchart-xtick" style=style>{label}</span> }
    }).collect();

    // Axis tick marks (notches on the L-shaped axis): horizontal on the y-axis at each value,
    // vertical on the x-axis at each irregular break — drawn over the bands at the plot edges.
    let ytick_marks: Vec<_> = (0..=4).map(|k| {
        let style = format!("bottom:{:.3}%", k as f32 * 25.0);
        view! { <div class="rchart-ytick" style=style></div> }
    }).collect();
    let xtick_marks: Vec<_> = ticks.iter().map(|t| {
        let style = format!("left:{:.3}%", (t.at / xm * 100.0).clamp(0.0, 100.0));
        view! { <div class="rchart-xtickmark" style=style></div> }
    }).collect();

    let legend_row = if legend.is_empty() {
        None
    } else {
        let items: Vec<_> = legend.iter().map(|l| {
            let sw = format!("background:{}", hex(l.color));
            let name = l.label.clone();
            view! { <span class="chart-legend-item"><span class="chart-swatch" style=sw></span>{name}</span> }
        }).collect();
        Some(view! { <div class="chart-legend">{items}</div> })
    };

    view! {
        <div class="rchart">
            <div class="rchart-row">
                <div class="rchart-yaxis">{yticks}</div>
                <div class="rchart-plotwrap">
                    <div class="rchart-plot">{region_divs}{ytick_marks}{xtick_marks}{ref_line_divs}</div>
                    {chip_divs}{bracket_div}
                </div>
            </div>
            <div class="rchart-xaxis">{xticks}</div>
            {legend_row}
        </div>
    }.into_any()
}