ferro-json-ui 0.3.0

JSON-based server-driven UI schema types for Ferro
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
//! Layout system for JSON-UI page rendering.
//!
//! Provides a trait-based layout system where named layouts wrap rendered
//! component HTML in full page shells. Three built-in layouts are provided:
//! `DefaultLayout` (minimal), `AppLayout` (dashboard with nav + sidebar),
//! and `AuthLayout` (centered, no card chrome). `DashboardLayout` is an optional
//! layout that users register themselves with per-app config.
//!
//! A global `LayoutRegistry` maps layout names to implementations. Specs
//! specify a layout via `Spec.layout`, and the render pipeline looks it up
//! in the registry.

use std::collections::HashMap;
use std::sync::{OnceLock, RwLock};

use crate::component::{HeaderProps, SidebarGroup, SidebarNavItem, SidebarProps};
use crate::render::classes::INTERACTIVE_BASE;
use crate::render::html_escape;

// ── Layout context ──────────────────────────────────────────────────────

/// Context passed to layout render functions.
///
/// Contains all data a layout needs to produce a complete HTML page:
/// the rendered component HTML, page metadata, and serialized view/data
/// for potential frontend hydration.
pub struct LayoutContext<'a> {
    /// Page title for the `<title>` element.
    pub title: &'a str,
    /// Rendered component HTML fragment (output of `render_spec_to_html`).
    pub content: &'a str,
    /// Additional `<head>` content (Tailwind CDN link, custom styles).
    pub head: &'a str,
    /// CSS classes for the `<body>` element.
    pub body_class: &'a str,
    /// Serialized view JSON for the `data-view` attribute.
    pub view_json: &'a str,
    /// Serialized data JSON for the `data-props` attribute.
    pub data_json: &'a str,
    /// JS assets and init scripts for plugins, injected before closing body tag.
    pub scripts: &'a str,
}

// ── Layout trait ────────────────────────────────────────────────────────

/// Trait for layout implementations.
///
/// Layouts produce a complete HTML page string wrapping the rendered
/// component content. They must be `Send + Sync` for use in the global
/// registry across threads.
pub trait Layout: Send + Sync {
    /// Render a complete HTML page using the provided context.
    fn render(&self, ctx: &LayoutContext) -> String;
}

// ── Base document helper ────────────────────────────────────────────────

/// Produce the common `<!DOCTYPE html>` shell shared by all built-in layouts.
///
/// All three built-in layouts delegate to this function to avoid duplicating
/// the HTML/head/body boilerplate. The `body_content` parameter receives the
/// inner body HTML which varies per layout.
fn base_document(
    title: &str,
    head: &str,
    body_class: &str,
    body_content: &str,
    scripts: &str,
) -> String {
    format!(
        r#"<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>{title}</title>
    {head}
</head>
<body class="{body_class}">
    {body_content}
    {scripts}
</body>
</html>"#,
        title = html_escape(title),
        head = head,
        body_class = html_escape(body_class),
        body_content = body_content,
        scripts = scripts,
    )
}

/// Produce the ferro-json-ui wrapper div with data attributes.
fn ferro_wrapper(ctx: &LayoutContext) -> String {
    format!(
        r#"<div id="ferro-json-ui" data-view="{view}" data-props="{props}">{content}</div>"#,
        view = html_escape(ctx.view_json),
        props = html_escape(ctx.data_json),
        content = ctx.content,
    )
}

/// Produce the common `<!DOCTYPE html>` shell with optional extra body attributes.
///
/// Extends `base_document` with a `body_data` parameter for additional
/// `data-*` attributes on the `<body>` element (e.g., `data-sse-url`).
fn base_document_ext(
    title: &str,
    head: &str,
    body_class: &str,
    body_data: &str,
    body_content: &str,
    scripts: &str,
) -> String {
    let body_data_attr = if body_data.is_empty() {
        String::new()
    } else {
        format!(" {body_data}")
    };
    format!(
        r#"<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>{title}</title>
    {head}
</head>
<body class="{body_class}"{body_data_attr}>
    {body_content}
    {scripts}
</body>
</html>"#,
        title = html_escape(title),
        head = head,
        body_class = html_escape(body_class),
        body_data_attr = body_data_attr,
        body_content = body_content,
        scripts = scripts,
    )
}

// ── DashboardLayout helpers ─────────────────────────────────────────────

/// Render a sidebar nav item for the layout shell.
fn layout_sidebar_nav_item(item: &SidebarNavItem) -> String {
    let disabled = item.disabled.unwrap_or(false);
    // Appearance is handled by the fjui-sidebar__nav-item skin rule (SKIN-01).
    // Layout utilities (flex items-center gap-*) stay inline (D-02).
    let (tag, classes) = if disabled {
        (
            "span",
            "fjui-sidebar__nav-item flex items-center gap-2 opacity-50 pointer-events-none select-none".to_string(),
        )
    } else if item.active {
        (
            "a",
            "fjui-sidebar__nav-item fjui-sidebar__nav-item--active flex items-center gap-2"
                .to_string(),
        )
    } else {
        (
            "a",
            "fjui-sidebar__nav-item flex items-center gap-2".to_string(),
        )
    };
    let mut html = if disabled {
        format!("<{tag} aria-disabled=\"true\" class=\"{classes}\">")
    } else {
        format!(
            "<{tag} href=\"{}\" class=\"{classes}\">",
            html_escape(&item.href),
        )
    };
    if let Some(ref icon) = item.icon {
        html.push_str(&format!(
            "<span class=\"inline-flex items-center justify-center w-5 h-5 shrink-0\">{icon}</span>" // raw SVG
        ));
    }
    html.push_str(&format!("{}</{tag}>", html_escape(&item.label)));
    html
}

/// Render a sidebar group for the layout shell.
fn layout_sidebar_group(group: &SidebarGroup) -> String {
    let mut html = String::from("<div data-sidebar-group");
    if group.collapsed {
        html.push_str(" data-collapsed");
    }
    html.push('>');
    // Appearance (font-size, color, text-transform) handled by fjui-sidebar__group-label skin rule.
    html.push_str(&format!(
        "<p class=\"fjui-sidebar__group-label\">{}</p>",
        html_escape(&group.label)
    ));
    html.push_str("<nav class=\"space-y-1\">");
    for item in &group.items {
        html.push_str(&layout_sidebar_nav_item(item));
    }
    html.push_str("</nav></div>");
    html
}

/// Render the sidebar shell from SidebarProps for DashboardLayout.
fn layout_sidebar_html(props: &SidebarProps) -> String {
    // fjui-sidebar: appearance (bg, border-right, width, position:fixed) handled by skin rule (SKIN-01).
    // Layout utilities (inset-y-0 left-0 z-40 flex flex-col hidden md:flex) stay inline (D-02).
    let mut html = String::from(
        "<aside data-sidebar class=\"fjui-sidebar inset-y-0 left-0 z-40 flex flex-col hidden md:flex\">",
    );
    if !props.fixed_top.is_empty() {
        html.push_str("<nav class=\"px-4 pt-4 pb-1 space-y-1\">");
        for item in &props.fixed_top {
            html.push_str(&layout_sidebar_nav_item(item));
        }
        html.push_str("</nav>");
    }
    if !props.groups.is_empty() {
        html.push_str("<div class=\"flex-1 overflow-y-auto px-4 pb-4 pt-0 space-y-2\">");
        for group in &props.groups {
            html.push_str(&layout_sidebar_group(group));
        }
        html.push_str("</div>");
    }
    if !props.fixed_bottom.is_empty() {
        // pb-safe: env(safe-area-inset-bottom) prevents bottom items from being clipped
        // by Chrome mobile's dynamic URL bar when the sidebar is open full-height (dvh fix).
        html.push_str("<nav class=\"fjui-sidebar__bottom p-4 space-y-1 border-t border-border\">");
        for item in &props.fixed_bottom {
            html.push_str(&layout_sidebar_nav_item(item));
        }
        html.push_str("</nav>");
    }
    html.push_str("</aside>");
    // Backdrop for mobile sidebar overlay — sibling of aside so it covers the viewport behind it.
    html.push_str(
        "<div data-sidebar-backdrop class=\"fixed inset-0 z-30 bg-black/50 hidden md:hidden\"></div>",
    );
    html
}

/// Render the header shell from HeaderProps for DashboardLayout.
fn layout_header_html(props: &HeaderProps) -> String {
    // fjui-header: appearance (bg, border-bottom, height, sticky positioning, padding) handled by skin rule (SKIN-01).
    // Layout utilities (z-30 flex items-center) stay inline (D-02). `relative` is intentionally
    // absent: the skin sets `position: sticky` on .fjui-header, and `relative` (a Tailwind
    // utility outside @layer components) would override it, causing the header to scroll away
    // (Finding A — sticky header fix). Sticky positioning creates its own stacking context,
    // so z-30 remains effective without an explicit `relative`. The header sits inside the
    // sidebar-offset content column, so it must NOT re-pad for the sidebar itself
    // (a legacy md:pl-72 here doubled the offset and pushed the workspace label off-left).
    // md:pl-64 offsets the header *content* to clear the sidebar, while the
    // border-bottom on the fjui-header element spans the full viewport width
    // (Finding 3 — full-width separator). The header element itself is
    // full-width; only its inner padding mirrors the content column offset.
    let mut html = String::from("<header class=\"fjui-header z-30 flex items-center md:pl-64\">");
    // Mobile hamburger button — visible only on small screens. -ml-2 cancels the
    // button's own padding so the icon glyph (not the touch target) aligns with
    // the 12px content gutter below.
    html.push_str(&format!(
        "<button data-sidebar-toggle class=\"md:hidden p-2 -ml-2 rounded-md text-text-muted \
         hover:text-text hover:bg-surface {INTERACTIVE_BASE}\" aria-label=\"Toggle sidebar\">\
         <svg class=\"h-6 w-6\" fill=\"none\" stroke=\"currentColor\" viewBox=\"0 0 24 24\">\
         <path stroke-linecap=\"round\" stroke-linejoin=\"round\" stroke-width=\"2\" \
         d=\"M4 6h16M4 12h16M4 18h16\"/></svg></button>"
    ));
    // Business name — left-aligned workspace label (CHR-01).
    html.push_str(&format!(
        "<span class=\"fjui-header__workspace\">{}</span>",
        html_escape(&props.business_name)
    ));
    html.push_str("<div class=\"ml-auto flex items-center gap-4\">");
    // Notification bell with dropdown toggle.
    html.push_str("<div class=\"relative\">");
    if let Some(count) = props.notification_count {
        if count > 0 {
            html.push_str(&format!(
                "<button data-notification-toggle class=\"relative p-2 rounded-md text-text-muted hover:text-text {INTERACTIVE_BASE}\">\
                 <svg class=\"h-5 w-5\" fill=\"none\" stroke=\"currentColor\" viewBox=\"0 0 24 24\">\
                 <path stroke-linecap=\"round\" stroke-linejoin=\"round\" stroke-width=\"2\" \
                 d=\"M15 17h5l-1.405-1.405A2.032 2.032 0 0118 14.158V11a6.002 6.002 0 00-4-5.659V5a2 2 0 10-4 0v.341C7.67 6.165 6 8.388 6 11v3.159c0 .538-.214 1.055-.595 1.436L4 17h5m6 0v1a3 3 0 11-6 0v-1m6 0H9\"/></svg>\
                 <span class=\"absolute top-1 right-1 inline-flex items-center justify-center h-4 w-4 \
                 text-xs font-bold text-primary-foreground bg-destructive rounded-full\">{count}</span></button>",
            ));
        } else {
            html.push_str(&format!(
                "<button data-notification-toggle class=\"p-2 rounded-md text-text-muted hover:text-text {INTERACTIVE_BASE}\">\
                 <svg class=\"h-5 w-5\" fill=\"none\" stroke=\"currentColor\" viewBox=\"0 0 24 24\">\
                 <path stroke-linecap=\"round\" stroke-linejoin=\"round\" stroke-width=\"2\" \
                 d=\"M15 17h5l-1.405-1.405A2.032 2.032 0 0118 14.158V11a6.002 6.002 0 00-4-5.659V5a2 2 0 10-4 0v.341C7.67 6.165 6 8.388 6 11v3.159c0 .538-.214 1.055-.595 1.436L4 17h5m6 0v1a3 3 0 11-6 0v-1m6 0H9\"/></svg></button>"
            ));
        }
    }
    html.push_str(
        "<div data-notification-dropdown class=\"hidden absolute right-0 top-full mt-1 w-80 \
         bg-card rounded-lg shadow-lg border border-border z-50\"></div></div>",
    );
    // Search affordance button (CHR-01 / UX-02): magnifier + ⌘K kbd chip + tooltip.
    // Dispatches fjui:open-command-palette — handler wired in Phase 249 (D-06).
    html.push_str(&format!(
        "<button type=\"button\" class=\"fjui-header__search-btn inline-flex items-center gap-2 {INTERACTIVE_BASE}\" \
         data-tooltip=\"Cerca\" aria-label=\"Cerca (⌘K)\" \
         onclick=\"document.dispatchEvent(new CustomEvent('fjui:open-command-palette'))\">\
         <svg class=\"h-4 w-4\" fill=\"none\" stroke=\"currentColor\" viewBox=\"0 0 24 24\" stroke-width=\"2\" \
           stroke-linecap=\"round\" stroke-linejoin=\"round\">\
           <circle cx=\"11\" cy=\"11\" r=\"8\"/><path d=\"m21 21-4.35-4.35\"/>\
         </svg>\
         <kbd class=\"fjui-kbd hidden md:inline\">\u{2318}K</kbd></button>"
    ));
    // Avatar initials button opens the fjui-avatar-menu popover (CHR-01).
    // Falls back to business_name initials when user_name is absent.
    let name_source = props
        .user_name
        .as_deref()
        .filter(|s| !s.trim().is_empty())
        .unwrap_or(&props.business_name);
    let initials: String = name_source
        .split_whitespace()
        .filter_map(|w| w.chars().next())
        .take(2)
        .collect::<String>()
        .to_uppercase();
    html.push_str(&format!(
        "<button class=\"fjui-avatar fjui-avatar--md inline-flex items-center justify-center \
         cursor-pointer {INTERACTIVE_BASE}\" \
         popovertarget=\"fjui-avatar-menu\" aria-label=\"Menu utente\" aria-haspopup=\"true\">{}</button>",
        html_escape(&initials)
    ));
    // Avatar menu popover panel: Profilo / Tema toggle (only when `theme_url`
    // is configured) / separator / Esci POST form.
    // Esci is a plain <form method="post"> with no CSRF token — matching how
    // consumer apps handle their other POST forms. The operative cross-site
    // mitigation is the app's SameSite=Lax session cookie (cross-site POSTs
    // arrive unauthenticated). Note ferro's CsrfMiddleware validates only the
    // X-CSRF-TOKEN/X-XSRF-TOKEN headers, so it would reject token-less form
    // posts; apps enabling it must exempt or tokenize this form themselves.
    // Tema onclick POSTs to `theme_url`; the dark class toggles only on a 2xx
    // response so visual state stays in sync with the persisted preference.
    let logout_action = props.logout_url.as_deref().unwrap_or("/logout");
    let theme_item = match props.theme_url.as_deref() {
        Some(url) => {
            // JS-string-escape then HTML-escape: the URL sits inside a
            // single-quoted JS literal within a double-quoted onclick attribute
            // (entities decode before JS parses, so `'` alone would break out).
            let js_url = url.replace('\\', "\\\\").replace('\'', "\\'");
            format!(
                "<button type=\"button\" class=\"fjui-avatar-menu__item\" \
                 onclick=\"fetch('{}',{{method:'POST',body:'theme='+(document.documentElement.classList.contains('dark')?'light':'dark'),headers:{{'Content-Type':'application/x-www-form-urlencoded'}}}}).then(function(r){{if(r.ok)document.documentElement.classList.toggle('dark')}})\">Tema</button>",
                html_escape(&js_url)
            )
        }
        None => String::new(),
    };
    let profile_item = match props.profile_url.as_deref() {
        Some(url) => format!(
            "<a href=\"{}\" class=\"fjui-avatar-menu__item\">Profilo</a>",
            html_escape(url)
        ),
        None => String::new(),
    };
    html.push_str(&format!(
        "<div popover id=\"fjui-avatar-menu\" data-popover-menu class=\"fjui-avatar-menu\">\
           {profile_item}\
           {theme_item}\
           <div class=\"fjui-avatar-menu__separator\"></div>\
           <form method=\"post\" action=\"{}\">\
             <button type=\"submit\" class=\"fjui-avatar-menu__item fjui-avatar-menu__item--destructive\">Esci</button>\
           </form>\
         </div>",
        html_escape(logout_action)
    ));
    html.push_str("</div></header>");
    html
}

/// Combine plugin scripts with the built-in JS runtime.
fn with_runtime(ctx_scripts: &str) -> String {
    let runtime = format!(
        "<script>\n{}\n</script>",
        crate::runtime::FERRO_RUNTIME_JS.as_str()
    );
    if ctx_scripts.is_empty() {
        runtime
    } else {
        format!("{ctx_scripts}\n{runtime}")
    }
}

// ── DefaultLayout ───────────────────────────────────────────────────────

/// Minimal layout wrapping content in a valid HTML page.
///
/// Produces the same structure as the existing framework HTML shell:
/// doctype, meta tags, title, head content, body with the ferro-json-ui
/// wrapper div containing the rendered components.
pub struct DefaultLayout;

impl Layout for DefaultLayout {
    fn render(&self, ctx: &LayoutContext) -> String {
        let wrapper = ferro_wrapper(ctx);
        let scripts = with_runtime(ctx.scripts);
        base_document(ctx.title, ctx.head, ctx.body_class, &wrapper, &scripts)
    }
}

// ── AppLayout ───────────────────────────────────────────────────────────

/// Dashboard-style layout with navigation bar, sidebar, and main content area.
///
/// Uses a flex layout with the sidebar on the left and main content on the
/// right. The ferro-json-ui wrapper div is placed inside the `<main>` element.
///
/// By default, renders empty navigation and sidebar placeholders. Users create
/// custom Layout implementations that call the partial functions with real data.
pub struct AppLayout;

impl Layout for AppLayout {
    fn render(&self, ctx: &LayoutContext) -> String {
        let nav = navigation(&[]);
        let side = sidebar(&[]);
        let wrapper = ferro_wrapper(ctx);

        let body = format!(
            r#"{nav}
    <div class="flex">
        {side}
        <main class="flex-1 px-3 py-4 md:p-6">
            <div class="mx-auto w-full max-w-7xl">
                {wrapper}
            </div>
        </main>
    </div>"#,
        );

        let scripts = with_runtime(ctx.scripts);
        base_document(ctx.title, ctx.head, ctx.body_class, &body, &scripts)
    }
}

// ── AuthLayout ──────────────────────────────────────────────────────────

/// Centered layout for authentication pages (login, register).
///
/// Centers the content vertically and horizontally within a max-width
/// container. No navigation or sidebar. No card chrome — the spec's
/// root component is responsible for its own card styling (D-05).
pub struct AuthLayout;

impl Layout for AuthLayout {
    fn render(&self, ctx: &LayoutContext) -> String {
        let wrapper = ferro_wrapper(ctx);

        let body = format!(
            r#"<div class="min-h-screen flex items-center justify-center">
        <div class="w-full max-w-md">
            {wrapper}
        </div>
    </div>"#,
        );

        let scripts = with_runtime(ctx.scripts);
        base_document(ctx.title, ctx.head, ctx.body_class, &body, &scripts)
    }
}

// ── Partial types and functions ─────────────────────────────────────────

/// A navigation link item.
pub struct NavItem {
    /// Display label for the link.
    pub label: String,
    /// URL the link points to.
    pub url: String,
    /// Whether this item represents the current page.
    pub active: bool,
}

impl NavItem {
    /// Create a new navigation item (inactive by default).
    pub fn new(label: impl Into<String>, url: impl Into<String>) -> Self {
        Self {
            label: label.into(),
            url: url.into(),
            active: false,
        }
    }

    /// Mark this navigation item as active (builder pattern).
    pub fn active(mut self) -> Self {
        self.active = true;
        self
    }
}

/// A sidebar section containing a title and a list of navigation items.
pub struct SidebarSection {
    /// Section heading.
    pub title: String,
    /// Navigation items in this section.
    pub items: Vec<NavItem>,
}

impl SidebarSection {
    /// Create a new sidebar section.
    pub fn new(title: impl Into<String>, items: Vec<NavItem>) -> Self {
        Self {
            title: title.into(),
            items,
        }
    }
}

/// Render a horizontal navigation bar.
///
/// Produces a `<nav>` element with Tailwind CSS classes. Active items
/// are highlighted with blue text and medium font weight.
pub fn navigation(items: &[NavItem]) -> String {
    let mut html =
        String::from("<nav class=\"bg-background border-b border-border px-4 py-3\"><div class=\"flex items-center space-x-6\">");

    for item in items {
        let class = if item.active {
            "text-primary font-medium"
        } else {
            "text-text-muted hover:text-text"
        };
        html.push_str(&format!(
            "<a href=\"{}\" class=\"{} {INTERACTIVE_BASE}\">{}</a>",
            html_escape(&item.url),
            class,
            html_escape(&item.label),
        ));
    }

    html.push_str("</div></nav>");
    html
}

/// Render a vertical sidebar with sections.
///
/// Produces an `<aside>` element with sections, each containing a heading
/// and a list of navigation links.
pub fn sidebar(sections: &[SidebarSection]) -> String {
    let mut html =
        String::from("<aside class=\"w-64 bg-surface border-r border-border p-4 min-h-screen\">");

    for section in sections {
        html.push_str("<div class=\"mb-6\">");
        html.push_str(&format!(
            "<h3 class=\"text-xs font-semibold text-text-muted uppercase tracking-wider mb-2\">{}</h3>",
            html_escape(&section.title),
        ));
        html.push_str("<ul class=\"space-y-1\">");
        for item in &section.items {
            let class = if item.active {
                "text-primary font-medium"
            } else {
                "text-text-muted hover:text-text"
            };
            html.push_str(&format!(
                "<li><a href=\"{}\" class=\"block px-2 py-1 text-sm rounded-md {} {INTERACTIVE_BASE}\">{}</a></li>",
                html_escape(&item.url),
                class,
                html_escape(&item.label),
            ));
        }
        html.push_str("</ul></div>");
    }

    html.push_str("</aside>");
    html
}

/// Render a simple footer.
///
/// Produces a `<footer>` element with centered text.
pub fn footer(text: &str) -> String {
    format!(
        "<footer class=\"border-t border-border px-4 py-3 text-center text-sm text-text-muted\">{}</footer>",
        html_escape(text),
    )
}

// ── DashboardLayout ─────────────────────────────────────────────────────

/// Configuration for `DashboardLayout`.
///
/// Provides the per-application sidebar navigation and header data needed
/// to render the persistent dashboard shell. Users construct this at app
/// startup and register it with the layout registry.
///
/// # Example
///
/// ```rust
/// use ferro_json_ui::{DashboardLayout, DashboardLayoutConfig, HeaderProps, SidebarProps, register_layout};
///
/// register_layout("dashboard", DashboardLayout::new(DashboardLayoutConfig {
///     sidebar: SidebarProps { fixed_top: vec![], groups: vec![], fixed_bottom: vec![] },
///     header: HeaderProps {
///         business_name: "My App".to_string(),
///         notification_count: None,
///         user_name: Some("Alice".to_string()),
///         user_avatar: None,
///         logout_url: Some("/logout".to_string()),
///         theme_url: Some("/theme".to_string()),
///         profile_url: Some("/settings".to_string()),
///     },
///     sse_url: None,
/// }));
/// ```
pub struct DashboardLayoutConfig {
    /// Sidebar navigation data for the persistent sidebar shell.
    pub sidebar: SidebarProps,
    /// Header data for the persistent header shell.
    pub header: HeaderProps,
    /// Optional SSE endpoint URL. When set, the JS runtime opens an
    /// `EventSource` connection to this URL and dispatches live-value
    /// and toast updates from incoming messages.
    pub sse_url: Option<String>,
}

/// Dashboard layout with persistent sidebar, header, and main content area.
///
/// Renders a full-page shell with a fixed sidebar on the left (desktop)
/// and a sticky header at the top. The rendered view content appears in
/// the `<main>` area. The built-in JS runtime (`FERRO_RUNTIME_JS`) is
/// injected once as a `<script>` tag, enabling SSE, live-value updates,
/// and toast notifications.
///
/// Mobile: sidebar is hidden by default and toggled via the hamburger button
/// in the header (using responsive Tailwind classes).
///
/// This layout is NOT auto-registered. Users must register it at startup:
///
/// ```rust
/// use ferro_json_ui::{DashboardLayout, DashboardLayoutConfig, HeaderProps, SidebarProps, register_layout};
///
/// register_layout("dashboard", DashboardLayout::new(DashboardLayoutConfig {
///     sidebar: SidebarProps { fixed_top: vec![], groups: vec![], fixed_bottom: vec![] },
///     header: HeaderProps {
///         business_name: "My App".to_string(),
///         notification_count: None,
///         user_name: None,
///         user_avatar: None,
///         logout_url: None,
///         theme_url: None,
///         profile_url: None,
///     },
///     sse_url: None,
/// }));
/// ```
pub struct DashboardLayout {
    /// Layout configuration (sidebar, header, SSE URL).
    pub config: DashboardLayoutConfig,
}

impl DashboardLayout {
    /// Create a new `DashboardLayout` from a `DashboardLayoutConfig`.
    pub fn new(config: DashboardLayoutConfig) -> Self {
        Self { config }
    }
}

impl Layout for DashboardLayout {
    fn render(&self, ctx: &LayoutContext) -> String {
        let sidebar_html = layout_sidebar_html(&self.config.sidebar);
        let header_html = layout_header_html(&self.config.header);
        let wrapper = ferro_wrapper(ctx);

        let body_data = if let Some(ref url) = self.config.sse_url {
            format!("data-sse-url=\"{}\"", html_escape(url))
        } else {
            String::new()
        };

        let runtime_script = format!(
            "<script>\n{}\n</script>",
            crate::runtime::FERRO_RUNTIME_JS.as_str()
        );
        let scripts = if ctx.scripts.is_empty() {
            runtime_script
        } else {
            format!("{}\n{}", ctx.scripts, runtime_script)
        };

        // Header is a sibling of the content column (not inside it) so its
        // border-bottom spans the full viewport width edge-to-edge (Finding 3).
        // The header's own md:pl-64 class mirrors the sidebar offset for content
        // alignment. Main keeps md:pl-64 so its content also clears the sidebar.
        let body_content = format!(
            r#"{sidebar_html}
    <div class="flex flex-col">
        {header_html}
        <main class="flex-1 px-3 py-4 md:p-6 md:pl-64">
            <div class="mx-auto w-full max-w-7xl">
                {wrapper}
            </div>
        </main>
        <div data-toast-container class="fixed top-4 right-4 z-50 flex flex-col gap-2"></div>
    </div>"#,
        );

        let body_class = if ctx.body_class.is_empty() {
            "bg-surface"
        } else {
            ctx.body_class
        };

        base_document_ext(
            ctx.title,
            ctx.head,
            body_class,
            &body_data,
            &body_content,
            &scripts,
        )
    }
}

// ── Layout registry ─────────────────────────────────────────────────────

/// Registry mapping layout names to implementations.
///
/// Created with three built-in layouts: "default" (`DefaultLayout`),
/// "app" (`AppLayout`), and "auth" (`AuthLayout`). Additional layouts
/// can be registered at application startup.
pub struct LayoutRegistry {
    layouts: HashMap<String, Box<dyn Layout>>,
    default: String,
}

impl LayoutRegistry {
    /// Create a new registry with the three built-in layouts.
    pub fn new() -> Self {
        let mut layouts: HashMap<String, Box<dyn Layout>> = HashMap::new();
        layouts.insert("default".to_string(), Box::new(DefaultLayout));
        layouts.insert("app".to_string(), Box::new(AppLayout));
        layouts.insert("auth".to_string(), Box::new(AuthLayout));

        Self {
            layouts,
            default: "default".to_string(),
        }
    }

    /// Register a layout by name. Replaces any existing layout with the same name.
    pub fn register(&mut self, name: impl Into<String>, layout: impl Layout + 'static) {
        self.layouts.insert(name.into(), Box::new(layout));
    }

    /// Render using the named layout. Falls back to default if name is None
    /// or the name is not found in the registry.
    pub fn render(&self, name: Option<&str>, ctx: &LayoutContext) -> String {
        let layout_name = name.unwrap_or(&self.default);
        let layout = self
            .layouts
            .get(layout_name)
            .or_else(|| self.layouts.get(&self.default))
            .expect("default layout must exist in registry");
        layout.render(ctx)
    }

    /// Check whether a layout with the given name is registered.
    pub fn has(&self, name: &str) -> bool {
        self.layouts.contains_key(name)
    }
}

impl Default for LayoutRegistry {
    fn default() -> Self {
        Self::new()
    }
}

// ── Global registry ─────────────────────────────────────────────────────

static GLOBAL_REGISTRY: OnceLock<RwLock<LayoutRegistry>> = OnceLock::new();

/// Access the global layout registry.
///
/// Lazily initialized on first call with the three built-in layouts.
pub fn global_registry() -> &'static RwLock<LayoutRegistry> {
    GLOBAL_REGISTRY.get_or_init(|| RwLock::new(LayoutRegistry::new()))
}

/// Register a layout in the global registry.
///
/// Convenience wrapper around `global_registry().write()`.
pub fn register_layout(name: impl Into<String>, layout: impl Layout + 'static) {
    global_registry()
        .write()
        .expect("layout registry poisoned")
        .register(name, layout);
}

/// Render using the global registry.
///
/// Convenience wrapper around `global_registry().read()`.
pub fn render_layout(name: Option<&str>, ctx: &LayoutContext) -> String {
    global_registry()
        .read()
        .expect("layout registry poisoned")
        .render(name, ctx)
}

// ── Tests ───────────────────────────────────────────────────────────────

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

    fn test_ctx() -> LayoutContext<'static> {
        LayoutContext {
            title: "Test Page",
            content: "<p>Hello</p>",
            head: "<link rel=\"stylesheet\" href=\"/style.css\">",
            body_class: "bg-background",
            view_json: "{\"schema\":\"ferro-json-ui/v2\"}",
            data_json: "{\"key\":\"value\"}",
            scripts: "",
        }
    }

    // ── base_document tests ─────────────────────────────────────────

    #[test]
    fn base_document_produces_valid_html_structure() {
        let html = base_document("Title", "<style></style>", "my-class", "<p>body</p>", "");
        assert!(html.starts_with("<!DOCTYPE html>"));
        assert!(html.contains("<html lang=\"en\">"));
        assert!(html.contains("<meta charset=\"UTF-8\">"));
        assert!(html.contains("<meta name=\"viewport\""));
        assert!(html.contains("<title>Title</title>"));
        assert!(html.contains("<style></style>"));
        assert!(html.contains("<body class=\"my-class\">"));
        assert!(html.contains("<p>body</p>"));
        assert!(html.contains("</html>"));
    }

    #[test]
    fn base_document_escapes_title() {
        let html = base_document("Tom & Jerry <script>", "", "", "", "");
        assert!(html.contains("<title>Tom &amp; Jerry &lt;script&gt;</title>"));
    }

    #[test]
    fn base_document_escapes_body_class() {
        let html = base_document("T", "", "a\"b", "", "");
        assert!(html.contains("class=\"a&quot;b\""));
    }

    // ── DefaultLayout tests ─────────────────────────────────────────

    #[test]
    fn default_layout_renders_all_context_fields() {
        let ctx = test_ctx();
        let html = DefaultLayout.render(&ctx);

        assert!(html.contains("<!DOCTYPE html>"));
        assert!(html.contains("<title>Test Page</title>"));
        assert!(html.contains("href=\"/style.css\""));
        assert!(html.contains("class=\"bg-background\""));
        assert!(html.contains("id=\"ferro-json-ui\""));
        assert!(html.contains("data-view=\""));
        assert!(html.contains("data-props=\""));
        assert!(html.contains("<p>Hello</p>"));
    }

    #[test]
    fn default_layout_contains_ferro_wrapper() {
        let ctx = test_ctx();
        let html = DefaultLayout.render(&ctx);
        assert!(html.contains("<div id=\"ferro-json-ui\""));
    }

    // ── AppLayout tests ─────────────────────────────────────────────

    #[test]
    fn app_layout_includes_nav_and_sidebar() {
        let ctx = test_ctx();
        let html = AppLayout.render(&ctx);

        assert!(html.contains("<nav"));
        assert!(html.contains("<aside"));
        assert!(html.contains("<main class=\"flex-1 px-3 py-4 md:p-6\">"));
        assert!(html.contains("<div id=\"ferro-json-ui\""));
        assert!(html.contains("<p>Hello</p>"));
    }

    #[test]
    fn app_layout_has_flex_structure() {
        let ctx = test_ctx();
        let html = AppLayout.render(&ctx);
        assert!(html.contains("class=\"flex\""));
    }

    // ── AuthLayout tests ────────────────────────────────────────────

    #[test]
    fn auth_layout_centers_content() {
        let ctx = test_ctx();
        let html = AuthLayout.render(&ctx);

        // Structural centering and max-width are preserved.
        assert!(
            html.contains("min-h-screen flex items-center justify-center"),
            "centering wrapper must remain"
        );
        assert!(
            html.contains("w-full max-w-md"),
            "max-width wrapper must remain"
        );
        assert!(html.contains("<div id=\"ferro-json-ui\""));
        // D-05: layout no longer applies card chrome; the spec's root declares its own Card.
        assert!(
            !html.contains("bg-card rounded-lg shadow-md p-8"),
            "card chrome must be removed from AuthLayout; spec root must declare its own Card"
        );
    }

    #[test]
    fn auth_layout_has_no_nav_or_sidebar() {
        let ctx = test_ctx();
        let html = AuthLayout.render(&ctx);
        assert!(!html.contains("<nav"));
        assert!(!html.contains("<aside"));
    }

    // ── LayoutRegistry tests ────────────────────────────────────────

    #[test]
    fn registry_returns_default_for_none_name() {
        let registry = LayoutRegistry::new();
        let ctx = test_ctx();
        let html = registry.render(None, &ctx);
        // DefaultLayout produces the simple wrapper (no nav/sidebar)
        assert!(html.contains("<div id=\"ferro-json-ui\""));
        assert!(!html.contains("<nav"));
    }

    #[test]
    fn registry_returns_default_for_unknown_name() {
        let registry = LayoutRegistry::new();
        let ctx = test_ctx();
        let html = registry.render(Some("nonexistent"), &ctx);
        // Falls back to default
        assert!(html.contains("<div id=\"ferro-json-ui\""));
        assert!(!html.contains("<nav"));
    }

    #[test]
    fn registry_renders_named_layout() {
        let registry = LayoutRegistry::new();
        let ctx = test_ctx();
        let html = registry.render(Some("app"), &ctx);
        assert!(html.contains("<nav"));
        assert!(html.contains("<aside"));
    }

    #[test]
    fn registry_renders_auth_layout() {
        let registry = LayoutRegistry::new();
        let ctx = test_ctx();
        let html = registry.render(Some("auth"), &ctx);
        assert!(html.contains("flex items-center justify-center"));
    }

    #[test]
    fn registry_has_returns_true_for_registered() {
        let registry = LayoutRegistry::new();
        assert!(registry.has("default"));
        assert!(registry.has("app"));
        assert!(registry.has("auth"));
    }

    #[test]
    fn registry_has_returns_false_for_unknown() {
        let registry = LayoutRegistry::new();
        assert!(!registry.has("nonexistent"));
    }

    #[test]
    fn registry_register_adds_custom_layout() {
        let mut registry = LayoutRegistry::new();
        struct Custom;
        impl Layout for Custom {
            fn render(&self, _ctx: &LayoutContext) -> String {
                "CUSTOM".to_string()
            }
        }
        registry.register("custom", Custom);
        assert!(registry.has("custom"));

        let ctx = test_ctx();
        let html = registry.render(Some("custom"), &ctx);
        assert_eq!(html, "CUSTOM");
    }

    #[test]
    fn registry_register_replaces_existing() {
        let mut registry = LayoutRegistry::new();
        struct Replacement;
        impl Layout for Replacement {
            fn render(&self, _ctx: &LayoutContext) -> String {
                "REPLACED".to_string()
            }
        }
        registry.register("default", Replacement);
        let ctx = test_ctx();
        let html = registry.render(None, &ctx);
        assert_eq!(html, "REPLACED");
    }

    // ── Global registry tests ───────────────────────────────────────

    #[test]
    fn global_registry_returns_valid_registry() {
        let reg = global_registry();
        let guard = reg.read().unwrap();
        assert!(guard.has("default"));
        assert!(guard.has("app"));
        assert!(guard.has("auth"));
    }

    #[test]
    fn render_layout_global_function_works() {
        let ctx = test_ctx();
        let html = render_layout(None, &ctx);
        assert!(html.contains("<!DOCTYPE html>"));
        assert!(html.contains("<div id=\"ferro-json-ui\""));
    }

    // ── Partial tests ───────────────────────────────────────────────

    #[test]
    fn navigation_renders_empty_gracefully() {
        let html = navigation(&[]);
        assert!(html.contains("<nav"));
        assert!(html.contains("</nav>"));
    }

    #[test]
    fn navigation_renders_items_with_correct_classes() {
        let items = vec![NavItem::new("Home", "/"), NavItem::new("Users", "/users")];
        let html = navigation(&items);
        assert!(html.contains("href=\"/\""));
        assert!(html.contains(">Home</a>"));
        assert!(html.contains("href=\"/users\""));
        assert!(html.contains(">Users</a>"));
        // Both should be inactive
        assert!(html.contains("text-text-muted hover:text-text"));
    }

    #[test]
    fn navigation_marks_active_item() {
        let items = vec![
            NavItem::new("Home", "/").active(),
            NavItem::new("Users", "/users"),
        ];
        let html = navigation(&items);
        assert!(html.contains("text-primary font-medium"));
    }

    #[test]
    fn sidebar_renders_sections_with_headers() {
        let sections = vec![SidebarSection::new(
            "Main Menu",
            vec![
                NavItem::new("Dashboard", "/"),
                NavItem::new("Settings", "/settings"),
            ],
        )];
        let html = sidebar(&sections);
        assert!(html.contains("<aside"));
        assert!(html.contains("Main Menu"));
        assert!(html.contains("Dashboard"));
        assert!(html.contains("Settings"));
        assert!(html.contains("</aside>"));
    }

    #[test]
    fn sidebar_renders_empty_gracefully() {
        let html = sidebar(&[]);
        assert!(html.contains("<aside"));
        assert!(html.contains("</aside>"));
    }

    #[test]
    fn footer_renders_text() {
        let html = footer("Copyright 2026");
        assert!(html.contains("<footer"));
        assert!(html.contains("Copyright 2026"));
        assert!(html.contains("</footer>"));
    }

    #[test]
    fn partials_escape_user_strings() {
        let items = vec![NavItem::new("Tom & Jerry", "/a&b")];
        let html = navigation(&items);
        assert!(html.contains("Tom &amp; Jerry"));
        assert!(html.contains("href=\"/a&amp;b\""));

        let sections = vec![SidebarSection::new(
            "A<B",
            vec![NavItem::new("<script>", "/x\"y")],
        )];
        let html = sidebar(&sections);
        assert!(html.contains("A&lt;B"));
        assert!(html.contains("&lt;script&gt;"));

        let html = footer("<script>alert('xss')</script>");
        assert!(html.contains("&lt;script&gt;"));
    }

    // ── ferro_wrapper tests ─────────────────────────────────────────

    #[test]
    fn ferro_wrapper_includes_data_attributes() {
        let ctx = test_ctx();
        let html = ferro_wrapper(&ctx);
        assert!(html.contains("id=\"ferro-json-ui\""));
        assert!(html.contains("data-view=\""));
        assert!(html.contains("data-props=\""));
        assert!(html.contains("<p>Hello</p>"));
    }

    // ── DashboardLayout tests ───────────────────────────────────────

    fn dashboard_layout() -> DashboardLayout {
        use crate::component::{HeaderProps, SidebarProps};
        DashboardLayout::new(DashboardLayoutConfig {
            sidebar: SidebarProps {
                fixed_top: vec![],
                groups: vec![],
                fixed_bottom: vec![],
            },
            header: HeaderProps {
                business_name: "Acme".to_string(),
                notification_count: None,
                user_name: Some("Alice".to_string()),
                user_avatar: None,
                logout_url: Some("/logout".to_string()),
                theme_url: None,
                profile_url: None,
            },
            sse_url: None,
        })
    }

    #[test]
    fn dashboard_layout_renders_full_html_structure() {
        let ctx = test_ctx();
        let html = dashboard_layout().render(&ctx);

        assert!(html.starts_with("<!DOCTYPE html>"));
        assert!(html.contains("<title>Test Page</title>"));
        assert!(html.contains("<div id=\"ferro-json-ui\""));
        assert!(html.contains("<p>Hello</p>"));
    }

    #[test]
    fn dashboard_layout_has_persistent_sidebar() {
        let ctx = test_ctx();
        let html = dashboard_layout().render(&ctx);
        assert!(html.contains("<aside data-sidebar"));
    }

    #[test]
    fn dashboard_layout_has_persistent_header() {
        let ctx = test_ctx();
        let html = dashboard_layout().render(&ctx);
        assert!(html.contains("<header"));
        assert!(html.contains("Acme"));
    }

    #[test]
    fn dashboard_layout_has_main_content_area() {
        let ctx = test_ctx();
        let html = dashboard_layout().render(&ctx);
        // md:pl-64 clears the fixed sidebar; added alongside md:p-6 (Finding 3).
        assert!(html.contains("<main class=\"flex-1 px-3 py-4 md:p-6 md:pl-64\">"));
    }

    #[test]
    fn dashboard_layout_has_toast_container() {
        let ctx = test_ctx();
        let html = dashboard_layout().render(&ctx);
        assert!(html.contains("data-toast-container"));
    }

    #[test]
    fn dashboard_layout_injects_runtime_js() {
        let ctx = test_ctx();
        let html = dashboard_layout().render(&ctx);
        // JS runtime is injected as a <script> tag containing the IIFE
        assert!(html.contains("<script>"));
        assert!(html.contains("FERRO_RUNTIME_JS") || html.contains("(function()"));
    }

    #[test]
    fn dashboard_layout_has_mobile_hamburger_toggle() {
        let ctx = test_ctx();
        let html = dashboard_layout().render(&ctx);
        assert!(html.contains("data-sidebar-toggle"));
    }

    #[test]
    fn dashboard_layout_no_sse_url_attribute_on_body_when_not_configured() {
        let ctx = test_ctx();
        let html = dashboard_layout().render(&ctx);
        // data-sse-url appears in the JS runtime source as a string literal,
        // but should NOT appear as a body element attribute when sse_url is None.
        // Check that the body tag does not contain the attribute.
        let body_start = html.find("<body").unwrap_or(0);
        let body_tag_end = html[body_start..].find('>').unwrap_or(0) + body_start;
        let body_tag = &html[body_start..=body_tag_end];
        assert!(!body_tag.contains("data-sse-url="));
    }

    #[test]
    fn dashboard_layout_adds_sse_url_to_body_when_configured() {
        use crate::component::{HeaderProps, SidebarProps};
        let layout = DashboardLayout::new(DashboardLayoutConfig {
            sidebar: SidebarProps {
                fixed_top: vec![],
                groups: vec![],
                fixed_bottom: vec![],
            },
            header: HeaderProps {
                business_name: "App".to_string(),
                notification_count: None,
                user_name: None,
                user_avatar: None,
                logout_url: None,
                theme_url: None,
                profile_url: None,
            },
            sse_url: Some("/events".to_string()),
        });
        let ctx = test_ctx();
        let html = layout.render(&ctx);
        assert!(html.contains("data-sse-url=\"/events\""));
    }

    #[test]
    fn dashboard_layout_escapes_sse_url_xss() {
        use crate::component::{HeaderProps, SidebarProps};
        let layout = DashboardLayout::new(DashboardLayoutConfig {
            sidebar: SidebarProps {
                fixed_top: vec![],
                groups: vec![],
                fixed_bottom: vec![],
            },
            header: HeaderProps {
                business_name: "App".to_string(),
                notification_count: None,
                user_name: None,
                user_avatar: None,
                logout_url: None,
                theme_url: None,
                profile_url: None,
            },
            sse_url: Some("/events?a=1&b=2".to_string()),
        });
        let ctx = test_ctx();
        let html = layout.render(&ctx);
        assert!(html.contains("data-sse-url=\"/events?a=1&amp;b=2\""));
    }

    #[test]
    fn dashboard_layout_notification_toggle_present_with_count() {
        use crate::component::{HeaderProps, SidebarProps};
        let layout = DashboardLayout::new(DashboardLayoutConfig {
            sidebar: SidebarProps {
                fixed_top: vec![],
                groups: vec![],
                fixed_bottom: vec![],
            },
            header: HeaderProps {
                business_name: "App".to_string(),
                notification_count: Some(5),
                user_name: None,
                user_avatar: None,
                logout_url: None,
                theme_url: None,
                profile_url: None,
            },
            sse_url: None,
        });
        let ctx = test_ctx();
        let html = layout.render(&ctx);
        assert!(html.contains("data-notification-toggle"));
    }

    #[test]
    fn dashboard_layout_has_sidebar_backdrop() {
        let ctx = test_ctx();
        let html = dashboard_layout().render(&ctx);
        assert!(html.contains("data-sidebar-backdrop"));
        assert!(html.contains("bg-black/50"));
        assert!(html.contains("md:hidden"));
    }

    #[test]
    fn dashboard_layout_sidebar_mobile_classes() {
        let ctx = test_ctx();
        let html = dashboard_layout().render(&ctx);
        // Sidebar uses responsive classes: hidden on mobile, flex on md+
        assert!(html.contains("hidden md:flex"));
    }

    #[test]
    fn dashboard_layout_uses_default_body_class() {
        let ctx = test_ctx();
        let html = dashboard_layout().render(&ctx);
        // body_class from test_ctx is "bg-background" — should be preserved
        assert!(html.contains("class=\"bg-background\""));
    }

    #[test]
    fn sidebar_nav_item_renders_icon_as_raw_svg() {
        let item = SidebarNavItem {
            label: "Dashboard".to_string(),
            href: "/dashboard".to_string(),
            icon: Some("<svg class=\"h-5 w-5\"><path d=\"M3 12l2-2\"/></svg>".to_string()),
            active: false,
            disabled: None,
        };
        let html = layout_sidebar_nav_item(&item);
        assert!(
            html.contains("<svg"),
            "icon SVG should be rendered raw, not escaped"
        );
        assert!(
            !html.contains("&lt;svg"),
            "icon SVG should NOT be html-escaped"
        );
        assert!(html.contains("Dashboard"), "label should still appear");
    }

    #[test]
    fn sidebar_group_label_uses_fjui_class() {
        // After Plan 09 migration: appearance (11px/500/uppercase) is in the
        // fjui-sidebar__group-label skin rule; Rust only emits the semantic class.
        let group = SidebarGroup {
            label: "Cassa".to_string(),
            collapsed: false,
            items: vec![],
        };
        let html = layout_sidebar_group(&group);
        assert!(html.contains("Cassa"));
        assert!(
            html.contains("fjui-sidebar__group-label"),
            "sidebar group label must emit fjui-sidebar__group-label"
        );
        // No appearance utilities in Rust output (SKIN-01).
        assert!(
            !html.contains("font-semibold"),
            "font-semibold must be removed from Rust emission; handled by skin rule"
        );
    }

    // ── INT-07 (layout): DashboardLayout sidebar nav item focus ring ──────
    // After Plan 09 migration: focus ring and transitions are in the
    // fjui-sidebar__nav-item skin rule (:focus-visible, transition-property).
    // The Rust emission carries only the semantic class — no appearance utilities.

    #[test]
    fn layout_sidebar_nav_focus_ring() {
        let item = SidebarNavItem {
            label: "Dashboard".to_string(),
            href: "/dashboard".to_string(),
            icon: None,
            active: false,
            disabled: None,
        };
        let html = layout_sidebar_nav_item(&item);
        assert!(
            html.contains("fjui-sidebar__nav-item"),
            "layout sidebar nav <a> item must emit fjui-sidebar__nav-item (INT-07 — focus ring in skin rule)"
        );
        // Focus ring and motion are handled by the fjui-sidebar__nav-item skin rule (SKIN-01).
        // They must NOT be inlined as Tailwind utilities in the Rust output.
        assert!(
            !html.contains("focus-visible:ring-ring"),
            "focus-visible:ring-ring must be removed from Rust emission; handled by skin rule"
        );
        assert!(
            !html.contains("duration-fast"),
            "duration-fast must be removed from Rust emission; handled by skin rule"
        );
    }

    // ── Plan 09: fjui-* chrome class migration tests ────────────────────────

    /// Sidebar shell emits fjui-sidebar (T-246-19: swap-target preserved).
    #[test]
    fn layout_sidebar_html_emits_fjui_sidebar_class() {
        use crate::component::SidebarProps;
        let props = SidebarProps {
            fixed_top: vec![],
            groups: vec![],
            fixed_bottom: vec![],
        };
        let html = layout_sidebar_html(&props);
        assert!(
            html.contains("fjui-sidebar"),
            "layout_sidebar_html must emit fjui-sidebar class; found: {html}"
        );
        // Structural attributes must be preserved (T-246-19).
        assert!(
            html.contains("data-sidebar"),
            "data-sidebar attribute must be preserved"
        );
        assert!(
            html.contains("data-sidebar-backdrop"),
            "data-sidebar-backdrop must be preserved"
        );
    }

    /// Header shell emits fjui-header.
    #[test]
    fn layout_header_html_emits_fjui_header_class() {
        use crate::component::HeaderProps;
        let props = HeaderProps {
            business_name: "Test".to_string(),
            notification_count: None,
            user_name: None,
            user_avatar: None,
            logout_url: None,
            theme_url: None,
            profile_url: None,
        };
        let html = layout_header_html(&props);
        assert!(
            html.contains("fjui-header"),
            "layout_header_html must emit fjui-header class; found: {html}"
        );
    }

    /// Sidebar nav item emits fjui-sidebar__nav-item (not old appearance utilities).
    #[test]
    fn layout_sidebar_nav_item_emits_fjui_class() {
        let item = SidebarNavItem {
            label: "Dashboard".to_string(),
            href: "/dashboard".to_string(),
            icon: None,
            active: false,
            disabled: None,
        };
        let html = layout_sidebar_nav_item(&item);
        assert!(
            html.contains("fjui-sidebar__nav-item"),
            "inactive nav item must emit fjui-sidebar__nav-item; found: {html}"
        );
    }

    /// Active sidebar nav item emits fjui-sidebar__nav-item--active modifier.
    #[test]
    fn layout_sidebar_nav_item_active_emits_fjui_active_modifier() {
        let item = SidebarNavItem {
            label: "Dashboard".to_string(),
            href: "/dashboard".to_string(),
            icon: None,
            active: true,
            disabled: None,
        };
        let html = layout_sidebar_nav_item(&item);
        assert!(
            html.contains("fjui-sidebar__nav-item--active"),
            "active nav item must emit fjui-sidebar__nav-item--active; found: {html}"
        );
    }

    /// Sidebar group label emits fjui-sidebar__group-label.
    #[test]
    fn layout_sidebar_group_label_emits_fjui_class() {
        let group = SidebarGroup {
            label: "Cassa".to_string(),
            collapsed: false,
            items: vec![],
        };
        let html = layout_sidebar_group(&group);
        assert!(
            html.contains("fjui-sidebar__group-label"),
            "sidebar group label must emit fjui-sidebar__group-label; found: {html}"
        );
    }

    /// DashboardLayout full render emits fjui-sidebar and fjui-header (structural id/attrs preserved).
    #[test]
    fn dashboard_layout_shell_emits_fjui_chrome_classes() {
        let ctx = test_ctx();
        let html = dashboard_layout().render(&ctx);
        assert!(
            html.contains("fjui-sidebar"),
            "DashboardLayout must emit fjui-sidebar"
        );
        assert!(
            html.contains("fjui-header"),
            "DashboardLayout must emit fjui-header"
        );
        // Structural swap-target preserved (T-246-19).
        assert!(
            html.contains("id=\"ferro-json-ui\""),
            "ferro-json-ui swap-target must be preserved"
        );
        assert!(
            html.contains("data-sidebar"),
            "data-sidebar must be preserved"
        );
        assert!(
            html.contains("data-toast-container"),
            "data-toast-container must be preserved"
        );
        // Grid layout utilities preserved (D-02).
        assert!(
            html.contains("md:pl-64"),
            "DashboardLayout grid utility md:pl-64 must be preserved"
        );
        assert!(
            html.contains("max-w-7xl"),
            "DashboardLayout grid utility max-w-7xl must be preserved"
        );
    }

    // ── Plan 03 (247): Avatar menu + search affordance tests ────────────────

    #[test]
    fn header_emits_avatar_initials() {
        use crate::component::HeaderProps;
        let props = HeaderProps {
            business_name: "Acme".to_string(),
            notification_count: None,
            user_name: Some("Alice Rossi".into()),
            user_avatar: None,
            logout_url: Some("/logout".into()),
            theme_url: None,
            profile_url: None,
        };
        let html = layout_header_html(&props);
        assert!(
            html.contains("fjui-avatar"),
            "avatar button must be present; got: {html}"
        );
        assert!(
            html.contains("popovertarget=\"fjui-avatar-menu\""),
            "avatar button must wire to fjui-avatar-menu popover; got: {html}"
        );
        assert!(
            html.contains("AR"),
            "initials AR must be present from 'Alice Rossi'; got: {html}"
        );
    }

    #[test]
    fn header_no_bare_logout_link() {
        use crate::component::HeaderProps;
        let props = HeaderProps {
            business_name: "Acme".to_string(),
            notification_count: None,
            user_name: None,
            user_avatar: None,
            logout_url: Some("/logout".into()),
            theme_url: None,
            profile_url: None,
        };
        let html = layout_header_html(&props);
        assert!(
            !html.contains(">Logout</a>"),
            "bare Logout link must be gone; got: {html}"
        );
        assert!(
            html.contains("action=\"/logout\""),
            "logout must be moved into Esci POST form; got: {html}"
        );
    }

    #[test]
    fn header_search_button_present() {
        use crate::component::HeaderProps;
        let props = HeaderProps {
            business_name: "Acme".to_string(),
            notification_count: None,
            user_name: None,
            user_avatar: None,
            logout_url: None,
            theme_url: None,
            profile_url: None,
        };
        let html = layout_header_html(&props);
        assert!(
            html.contains("fjui-header__search-btn"),
            "search button must be present; got: {html}"
        );
        assert!(
            html.contains("data-tooltip=\"Cerca\""),
            "search button must carry data-tooltip Cerca (UX-02); got: {html}"
        );
        assert!(
            html.contains("fjui:open-command-palette"),
            "search button must dispatch fjui:open-command-palette (D-06); got: {html}"
        );
        assert!(
            html.contains("fjui-kbd"),
            "search button must include fjui-kbd chip; got: {html}"
        );
    }

    /// Tema item renders only when theme_url is configured, POSTs to that URL,
    /// and toggles the dark class only on a 2xx response (WR-04).
    #[test]
    fn header_theme_toggle_only_when_theme_url_set() {
        use crate::component::HeaderProps;
        let mut props = HeaderProps {
            business_name: "Acme".to_string(),
            notification_count: None,
            user_name: None,
            user_avatar: None,
            logout_url: None,
            theme_url: None,
            profile_url: None,
        };
        let html = layout_header_html(&props);
        assert!(
            !html.contains(">Tema</button>"),
            "Tema item must be omitted when theme_url is None; got: {html}"
        );

        props.theme_url = Some("/dashboard/theme".to_string());
        let html = layout_header_html(&props);
        assert!(
            html.contains("fetch('/dashboard/theme'"),
            "Tema must POST to the configured theme_url; got: {html}"
        );
        assert!(
            html.contains("if(r.ok)document.documentElement.classList.toggle('dark')"),
            "dark-class toggle must be gated on response.ok; got: {html}"
        );
    }
}