rdocx 0.1.2

High-level API for reading, writing, and converting DOCX documents
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
//! Generates all sample documents, each exported to DOCX, PDF, HTML, and Markdown.
//!
//! Documents:
//! 1. feature_showcase — Updated to cover ALL library features
//! 2. proposal — Professional project proposal (Tensorbee)
//! 3. quote — Quote with bill of materials
//! 4. invoice — Professional invoice
//! 5. report — Hierarchical report with images
//! 6. letter — Formal business letter
//! 7. contract — Employment contract
//!
//! Run with: cargo run --example generate_all_samples

use std::collections::HashMap;
use std::fmt::Write as FmtWrite;
use std::path::Path;

use rdocx::{
    Alignment, BorderStyle, Document, Length, SectionBreak, StyleBuilder, TabAlignment, TabLeader,
    VerticalAlignment,
};

fn main() {
    let samples_dir = Path::new(env!("CARGO_MANIFEST_DIR"))
        .parent()
        .unwrap()
        .parent()
        .unwrap()
        .join("samples");
    std::fs::create_dir_all(&samples_dir).unwrap();

    println!(
        "Generating all sample documents in {}\n",
        samples_dir.display()
    );

    let generators: Vec<(&str, fn(&Path) -> Document)> = vec![
        ("feature_showcase", generate_feature_showcase),
        ("proposal", generate_proposal),
        ("quote", generate_quote),
        ("invoice", generate_invoice),
        ("report", generate_report),
        ("letter", generate_letter),
        ("contract", generate_contract),
    ];

    for (name, generator) in &generators {
        println!("--- {} ---", name);
        let doc = generator(&samples_dir);
        export_all(&samples_dir, name, doc);
        println!();
    }

    println!("All done!");
}

/// Export a document to DOCX, PDF, HTML, and Markdown.
fn export_all(dir: &Path, name: &str, mut doc: Document) {
    let docx_path = dir.join(format!("{name}.docx"));
    doc.save(&docx_path).unwrap();
    println!("  {name}.docx");

    match doc.to_pdf() {
        Ok(pdf) => {
            let pdf_path = dir.join(format!("{name}.pdf"));
            std::fs::write(&pdf_path, &pdf).unwrap();
            println!("  {name}.pdf ({} bytes)", pdf.len());
        }
        Err(e) => println!("  {name}.pdf — skipped: {e}"),
    }

    let html = doc.to_html();
    let html_path = dir.join(format!("{name}.html"));
    std::fs::write(&html_path, &html).unwrap();
    println!("  {name}.html ({} bytes)", html.len());

    let md = doc.to_markdown();
    let md_path = dir.join(format!("{name}.md"));
    std::fs::write(&md_path, &md).unwrap();
    println!("  {name}.md ({} bytes)", md.len());
}

// =============================================================================
// 1. FEATURE SHOWCASE — Updated to cover ALL library features
// =============================================================================
fn generate_feature_showcase(_samples_dir: &Path) -> Document {
    let mut doc = Document::new();

    // ── Page Setup & Metadata ──
    doc.set_page_size(Length::inches(8.5), Length::inches(11.0));
    doc.set_margins(
        Length::inches(1.0),
        Length::inches(1.0),
        Length::inches(1.0),
        Length::inches(1.0),
    );
    doc.set_header_footer_distance(Length::twips(720), Length::twips(432));
    doc.set_gutter(Length::twips(0));
    doc.set_title("rdocx Feature Showcase");
    doc.set_author("rdocx Sample Generator");
    doc.set_subject("Comprehensive feature demonstration");
    doc.set_keywords("rdocx, docx, rust, sample, showcase");

    // Headers & Footers with different first page
    doc.set_different_first_page(true);
    doc.set_first_page_header("rdocx");
    doc.set_first_page_footer("Feature Showcase — Cover Page");
    doc.set_header("rdocx Feature Showcase");
    doc.set_footer("Generated by rdocx");

    // ── COVER PAGE ──
    let bg = create_sample_png(612, 792, [20, 45, 90]);
    doc.add_background_image(&bg, "cover_bg.png");

    for _ in 0..3 {
        doc.add_paragraph("");
    }
    {
        let mut p = doc.add_paragraph("").alignment(Alignment::Center);
        p.add_run("rdocx").bold(true).size(72.0).color("FFFFFF");
    }
    {
        let mut p = doc.add_paragraph("").alignment(Alignment::Center);
        p.add_run("Complete Feature Showcase")
            .size(28.0)
            .color("FFFFFF")
            .italic(true);
    }
    doc.add_paragraph("");
    {
        let mut p = doc.add_paragraph("").alignment(Alignment::Center);
        p.add_run("Every feature of the rdocx Rust library")
            .size(13.0)
            .color("B0C4DE");
    }
    {
        let mut p = doc.add_paragraph("").alignment(Alignment::Center);
        p.add_run("demonstrated in a single document.")
            .size(13.0)
            .color("B0C4DE");
    }

    // ── TABLE OF CONTENTS ──
    doc.add_paragraph("").page_break_before(true);
    doc.insert_toc(doc.content_count(), 3);

    // ── SECTION 1: TEXT FORMATTING ──
    doc.add_paragraph("").page_break_before(true);
    doc.add_paragraph("1. Text Formatting").style("Heading1");

    // Paragraph Alignment
    doc.add_paragraph("1.1 Paragraph Alignment")
        .style("Heading2");
    doc.add_paragraph("Left-aligned paragraph (default).")
        .alignment(Alignment::Left);
    doc.add_paragraph("Center-aligned paragraph.")
        .alignment(Alignment::Center);
    doc.add_paragraph("Right-aligned paragraph.")
        .alignment(Alignment::Right);
    doc.add_paragraph(
        "Justified paragraph — this text is long enough to demonstrate how justified alignment \
         distributes extra space across word gaps so lines fill the full width of the text area.",
    )
    .alignment(Alignment::Justify);

    doc.add_paragraph("");

    // Run Formatting
    doc.add_paragraph("1.2 Run Formatting").style("Heading2");
    {
        let mut p = doc.add_paragraph("");
        p.add_run("Bold").bold(true);
        p.add_run(" | ");
        p.add_run("Italic").italic(true);
        p.add_run(" | ");
        p.add_run("Bold Italic").bold(true).italic(true);
        p.add_run(" | ");
        p.add_run("Underline").underline(true);
        p.add_run(" | ");
        p.add_run("Strikethrough").strike(true);
        p.add_run(" | ");
        p.add_run("Double Strike").double_strike(true);
    }
    {
        let mut p = doc.add_paragraph("");
        p.add_run("Red").color("FF0000");
        p.add_run(" | ");
        p.add_run("Blue").color("0000FF");
        p.add_run(" | ");
        p.add_run("Green").color("00AA00");
        p.add_run(" | ");
        p.add_run("Highlighted").highlight("FFFF00");
    }
    {
        let mut p = doc.add_paragraph("");
        p.add_run("8pt").size(8.0);
        p.add_run(" | ");
        p.add_run("11pt").size(11.0);
        p.add_run(" | ");
        p.add_run("16pt").size(16.0);
        p.add_run(" | ");
        p.add_run("24pt").size(24.0);
    }
    {
        let mut p = doc.add_paragraph("");
        p.add_run("Arial").font("Arial");
        p.add_run(" | ");
        p.add_run("Times New Roman").font("Times New Roman");
        p.add_run(" | ");
        p.add_run("Courier New").font("Courier New");
    }
    {
        let mut p = doc.add_paragraph("");
        p.add_run("H");
        p.add_run("2").subscript();
        p.add_run("O (subscript) | E=mc");
        p.add_run("2").superscript();
        p.add_run(" (superscript)");
    }
    {
        let mut p = doc.add_paragraph("");
        p.add_run("ALL CAPS").all_caps(true);
        p.add_run(" | ");
        p.add_run("Small Caps").small_caps(true);
        p.add_run(" | ");
        p.add_run("Expanded").character_spacing(Length::twips(40));
        p.add_run(" | ");
        p.add_run("Hidden text (not visible)").hidden(true);
    }

    doc.add_paragraph("");

    // Underline Styles
    doc.add_paragraph("1.3 Underline Styles").style("Heading2");
    {
        let mut p = doc.add_paragraph("");
        p.add_run("Single ")
            .underline_style(rdocx::UnderlineStyle::Single);
        p.add_run("Double ")
            .underline_style(rdocx::UnderlineStyle::Double);
        p.add_run("Thick ")
            .underline_style(rdocx::UnderlineStyle::Thick);
        p.add_run("Dotted ")
            .underline_style(rdocx::UnderlineStyle::Dotted);
        p.add_run("Dash ")
            .underline_style(rdocx::UnderlineStyle::Dash);
        p.add_run("Wave ")
            .underline_style(rdocx::UnderlineStyle::Wave);
    }

    // ── SECTION 2: PARAGRAPH FORMATTING ──
    doc.add_paragraph("").page_break_before(true);
    doc.add_paragraph("2. Paragraph Formatting")
        .style("Heading1");

    doc.add_paragraph("2.1 Shading & Borders").style("Heading2");
    doc.add_paragraph("Paragraph with light green shading")
        .shading("E2EFDA");
    doc.add_paragraph("Paragraph with bottom border")
        .border_bottom(BorderStyle::Single, 6, "2E75B6");
    doc.add_paragraph("Paragraph with all borders (red)")
        .border_all(BorderStyle::Single, 4, "FF0000");

    doc.add_paragraph("2.2 Indentation").style("Heading2");
    doc.add_paragraph("1-inch left indent + 0.5-inch hanging indent")
        .indent_left(Length::inches(1.0))
        .hanging_indent(Length::inches(0.5));
    doc.add_paragraph("0.5-inch first-line indent on this paragraph")
        .first_line_indent(Length::inches(0.5));
    doc.add_paragraph("Both left and right indent (0.75 inches each)")
        .indent_left(Length::inches(0.75))
        .indent_right(Length::inches(0.75));

    doc.add_paragraph("2.3 Spacing & Line Height")
        .style("Heading2");
    doc.add_paragraph("Extra space before (24pt) and after (12pt)")
        .space_before(Length::pt(24.0))
        .space_after(Length::pt(12.0));
    doc.add_paragraph(
        "Double line spacing paragraph. This text should have extra vertical space between \
         lines to demonstrate line_spacing_multiple(2.0).",
    )
    .line_spacing_multiple(2.0);
    doc.add_paragraph("Exact 20pt line spacing (fixed height).")
        .line_spacing(20.0);

    doc.add_paragraph("2.4 Pagination Controls")
        .style("Heading2");
    doc.add_paragraph("keep_with_next — this paragraph stays with the next")
        .keep_with_next(true);
    doc.add_paragraph("(This paragraph was kept with the one above.)");
    doc.add_paragraph("keep_together — all lines of this paragraph stay on the same page")
        .keep_together(true);
    doc.add_paragraph("widow_control — prevents widow/orphan lines")
        .widow_control(true);

    // ── SECTION 3: LISTS ──
    doc.add_paragraph("").page_break_before(true);
    doc.add_paragraph("3. Lists").style("Heading1");

    doc.add_paragraph("3.1 Bullet Lists").style("Heading2");
    doc.add_bullet_list_item("First item", 0);
    doc.add_bullet_list_item("Second item", 0);
    doc.add_bullet_list_item("Nested level 1", 1);
    doc.add_bullet_list_item("Nested level 2", 2);
    doc.add_bullet_list_item("Back to level 1", 1);
    doc.add_bullet_list_item("Third item", 0);

    doc.add_paragraph("");

    doc.add_paragraph("3.2 Numbered Lists").style("Heading2");
    doc.add_numbered_list_item("First numbered item", 0);
    doc.add_numbered_list_item("Second numbered item", 0);
    doc.add_numbered_list_item("Sub-item A", 1);
    doc.add_numbered_list_item("Sub-item B", 1);
    doc.add_numbered_list_item("Third numbered item", 0);

    // ── SECTION 4: TAB STOPS ──
    doc.add_paragraph("");
    doc.add_paragraph("4. Tab Stops").style("Heading1");

    doc.add_paragraph("4.1 Alignment Tabs").style("Heading2");
    doc.add_paragraph("Left\tCenter\tRight\tDecimal")
        .add_tab_stop(TabAlignment::Left, Length::inches(0.0))
        .add_tab_stop(TabAlignment::Center, Length::inches(2.5))
        .add_tab_stop(TabAlignment::Right, Length::inches(5.0))
        .add_tab_stop(TabAlignment::Decimal, Length::inches(6.5));

    doc.add_paragraph("4.2 Tab Leaders").style("Heading2");
    doc.add_paragraph("Item\t\tPrice")
        .add_tab_stop_with_leader(TabAlignment::Left, Length::inches(0.0), TabLeader::None)
        .add_tab_stop_with_leader(TabAlignment::Right, Length::inches(4.0), TabLeader::Dot)
        .add_tab_stop_with_leader(TabAlignment::Right, Length::inches(5.0), TabLeader::None);
    doc.add_paragraph("Widget\t\t$19.99")
        .add_tab_stop_with_leader(TabAlignment::Left, Length::inches(0.0), TabLeader::None)
        .add_tab_stop_with_leader(TabAlignment::Right, Length::inches(4.0), TabLeader::Dot)
        .add_tab_stop_with_leader(TabAlignment::Right, Length::inches(5.0), TabLeader::None);
    doc.add_paragraph("Gadget\t\t$249.50")
        .add_tab_stop_with_leader(TabAlignment::Left, Length::inches(0.0), TabLeader::None)
        .add_tab_stop_with_leader(
            TabAlignment::Right,
            Length::inches(4.0),
            TabLeader::Underscore,
        )
        .add_tab_stop_with_leader(TabAlignment::Right, Length::inches(5.0), TabLeader::None);

    // ── SECTION 5: TABLES ──
    doc.add_paragraph("").page_break_before(true);
    doc.add_paragraph("5. Tables").style("Heading1");

    doc.add_paragraph("5.1 Basic Table").style("Heading2");
    {
        let mut tbl = doc
            .add_table(4, 3)
            .borders(BorderStyle::Single, 4, "000000");
        for col in 0..3 {
            tbl.cell(0, col).unwrap().shading("2E75B6");
        }
        tbl.cell(0, 0).unwrap().set_text("Name");
        tbl.cell(0, 1).unwrap().set_text("Role");
        tbl.cell(0, 2).unwrap().set_text("Location");
        tbl.cell(1, 0).unwrap().set_text("Walter White");
        tbl.cell(1, 1).unwrap().set_text("CEO");
        tbl.cell(1, 2).unwrap().set_text("Albuquerque");
        tbl.cell(2, 0).unwrap().set_text("Jesse Pinkman");
        tbl.cell(2, 1).unwrap().set_text("CTO");
        tbl.cell(2, 2).unwrap().set_text("Remote");
        tbl.cell(3, 0).unwrap().set_text("Hank Schrader");
        tbl.cell(3, 1).unwrap().set_text("Security");
        tbl.cell(3, 2).unwrap().set_text("Washington");
    }

    doc.add_paragraph("");

    doc.add_paragraph("5.2 Column Span & Cell Shading")
        .style("Heading2");
    {
        let mut tbl = doc
            .add_table(3, 4)
            .borders(BorderStyle::Single, 4, "000000")
            .width_pct(100.0);
        tbl.cell(0, 0).unwrap().set_text("Quarterly Report");
        tbl.cell(0, 0).unwrap().shading("1F4E79").grid_span(4);
        tbl.cell(1, 0).unwrap().set_text("Region");
        tbl.cell(1, 0).unwrap().shading("D6E4F0");
        tbl.cell(1, 1).unwrap().set_text("Q1");
        tbl.cell(1, 1).unwrap().shading("D6E4F0");
        tbl.cell(1, 2).unwrap().set_text("Q2");
        tbl.cell(1, 2).unwrap().shading("D6E4F0");
        tbl.cell(1, 3).unwrap().set_text("Total");
        tbl.cell(1, 3).unwrap().shading("D6E4F0");
        tbl.cell(2, 0).unwrap().set_text("Americas");
        tbl.cell(2, 1).unwrap().set_text("$2.4M");
        tbl.cell(2, 2).unwrap().set_text("$2.7M");
        tbl.cell(2, 3).unwrap().set_text("$5.1M");
    }

    doc.add_paragraph("");

    doc.add_paragraph("5.3 Vertical Merge").style("Heading2");
    {
        let mut tbl = doc
            .add_table(4, 3)
            .borders(BorderStyle::Single, 4, "000000");
        tbl.cell(0, 0).unwrap().set_text("Category");
        tbl.cell(0, 0).unwrap().shading("E2EFDA");
        tbl.cell(0, 1).unwrap().set_text("Item");
        tbl.cell(0, 1).unwrap().shading("E2EFDA");
        tbl.cell(0, 2).unwrap().set_text("Price");
        tbl.cell(0, 2).unwrap().shading("E2EFDA");
        tbl.cell(1, 0).unwrap().set_text("Hardware");
        tbl.cell(1, 0).unwrap().v_merge_restart();
        tbl.cell(1, 1).unwrap().set_text("Laptop");
        tbl.cell(1, 2).unwrap().set_text("$1,200");
        tbl.cell(2, 0).unwrap().v_merge_continue();
        tbl.cell(2, 1).unwrap().set_text("Monitor");
        tbl.cell(2, 2).unwrap().set_text("$450");
        tbl.cell(3, 0).unwrap().set_text("Software");
        tbl.cell(3, 1).unwrap().set_text("IDE License");
        tbl.cell(3, 2).unwrap().set_text("$200/yr");
    }

    doc.add_paragraph("");

    doc.add_paragraph("5.4 Nested Table").style("Heading2");
    {
        let mut tbl = doc
            .add_table(2, 2)
            .borders(BorderStyle::Single, 6, "2E75B6");
        tbl.cell(0, 0).unwrap().set_text("Outer (0,0)");
        tbl.cell(0, 1).unwrap().set_text("Outer (0,1)");
        tbl.cell(1, 0).unwrap().set_text("Outer (1,0)");
        {
            let mut cell = tbl.cell(1, 1).unwrap();
            cell.set_text("Nested table below:");
            let mut nested = cell
                .add_table(2, 2)
                .borders(BorderStyle::Single, 2, "FF6600");
            nested.cell(0, 0).unwrap().set_text("A");
            nested.cell(0, 1).unwrap().set_text("B");
            nested.cell(1, 0).unwrap().set_text("C");
            nested.cell(1, 1).unwrap().set_text("D");
        }
    }

    doc.add_paragraph("");

    doc.add_paragraph("5.5 Vertical Alignment & Row Properties")
        .style("Heading2");
    {
        let mut tbl = doc
            .add_table(2, 3)
            .borders(BorderStyle::Single, 4, "666666");
        tbl.row(0).unwrap().height(Length::pt(50.0)).header();
        tbl.cell(0, 0).unwrap().set_text("Top");
        tbl.cell(0, 0)
            .unwrap()
            .vertical_alignment(VerticalAlignment::Top)
            .shading("FFF2CC");
        tbl.cell(0, 1).unwrap().set_text("Center");
        tbl.cell(0, 1)
            .unwrap()
            .vertical_alignment(VerticalAlignment::Center)
            .shading("D9E2F3");
        tbl.cell(0, 2).unwrap().set_text("Bottom");
        tbl.cell(0, 2)
            .unwrap()
            .vertical_alignment(VerticalAlignment::Bottom)
            .shading("E2EFDA");
        tbl.row(1).unwrap().cant_split();
        tbl.cell(1, 0).unwrap().set_text("No-wrap cell");
        tbl.cell(1, 0).unwrap().no_wrap();
        tbl.cell(1, 1).unwrap().set_text("Fixed width");
        tbl.cell(1, 1).unwrap().width(Length::inches(2.0));
        tbl.cell(1, 2).unwrap().set_text("Normal");
    }

    // ── SECTION 6: IMAGES ──
    doc.add_paragraph("").page_break_before(true);
    doc.add_paragraph("6. Images").style("Heading1");

    doc.add_paragraph("6.1 Inline Image").style("Heading2");
    doc.add_paragraph("A blue gradient image below:");
    let img = create_sample_png(200, 50, [0, 80, 200]);
    doc.add_picture(&img, "chart.png", Length::inches(3.0), Length::inches(0.75));

    doc.add_paragraph("");
    doc.add_paragraph("6.2 Header Image").style("Heading2");
    let hdr_img = create_sample_png(400, 40, [40, 40, 40]);
    doc.set_header_image(
        &hdr_img,
        "header_logo.png",
        Length::inches(2.0),
        Length::inches(0.2),
    );
    doc.add_paragraph("The header now contains an inline image (check the top of this page).");

    doc.add_paragraph("");
    doc.add_paragraph("Note: Page 1 uses a full-page background image (add_background_image).");

    // ── SECTION 7: CONTENT MANIPULATION ──
    doc.add_paragraph("").page_break_before(true);
    doc.add_paragraph("7. Content Manipulation")
        .style("Heading1");

    doc.add_paragraph("7.1 Placeholder Replacement")
        .style("Heading2");
    doc.add_paragraph("Customer: {{customer}}");
    doc.add_paragraph("Date: {{date}}");
    doc.add_paragraph("Reference: {{ref_number}}");
    {
        let mut tbl = doc
            .add_table(2, 2)
            .borders(BorderStyle::Single, 4, "000000");
        tbl.cell(0, 0).unwrap().set_text("Project");
        tbl.cell(0, 0).unwrap().shading("D6E4F0");
        tbl.cell(0, 1).unwrap().set_text("{{project}}");
        tbl.cell(1, 0).unwrap().set_text("Status");
        tbl.cell(1, 0).unwrap().shading("D6E4F0");
        tbl.cell(1, 1).unwrap().set_text("{{status}}");
    }

    let mut replacements = HashMap::new();
    replacements.insert("{{customer}}", "Tensorbee Inc.");
    replacements.insert("{{date}}", "February 22, 2026");
    replacements.insert("{{ref_number}}", "TB-2026-001");
    replacements.insert("{{project}}", "Infrastructure Upgrade");
    replacements.insert("{{status}}", "In Progress");
    let n = doc.replace_all(&replacements);
    doc.add_paragraph(&format!("({n} placeholders replaced above)"));

    doc.add_paragraph("");

    doc.add_paragraph("7.2 Regex Replacement").style("Heading2");
    doc.add_paragraph("Emails: user1@example.com and admin@tensorbee.com");
    let _ = doc.replace_regex(r"\b\w+@\w+\.\w+\b", "[REDACTED]");
    doc.add_paragraph("(Email addresses above were redacted using regex replacement)");

    doc.add_paragraph("");

    doc.add_paragraph("7.3 Content Insertion").style("Heading2");
    doc.add_paragraph("Section A: First content.");
    doc.add_paragraph("Section C: Third content.");
    if let Some(idx) = doc.find_content_index("Section C") {
        doc.insert_paragraph(
            idx,
            "Section B: Inserted between A and C via find_content_index().",
        );
    }

    // ── SECTION 8: SECTION BREAKS & ORIENTATION ──
    doc.add_paragraph("").section_break(SectionBreak::NextPage);
    doc.add_paragraph("8. Section Breaks & Orientation")
        .style("Heading1");
    doc.add_paragraph("This page is LANDSCAPE. Useful for wide tables and charts.");

    {
        let mut tbl = doc
            .add_table(3, 7)
            .borders(BorderStyle::Single, 4, "2E75B6");
        let headers = ["Region", "Jan", "Feb", "Mar", "Apr", "May", "Total"];
        for (c, h) in headers.iter().enumerate() {
            tbl.cell(0, c).unwrap().set_text(h);
            tbl.cell(0, c).unwrap().shading("2E75B6");
        }
        let data = [
            [
                "Americas", "$1.2M", "$1.3M", "$1.4M", "$1.5M", "$1.6M", "$7.0M",
            ],
            [
                "Europe", "$0.8M", "$0.9M", "$0.9M", "$1.0M", "$1.1M", "$4.7M",
            ],
        ];
        for (r, row) in data.iter().enumerate() {
            for (c, v) in row.iter().enumerate() {
                tbl.cell(r + 1, c).unwrap().set_text(v);
            }
        }
    }

    doc.add_paragraph("")
        .section_break(SectionBreak::NextPage)
        .section_landscape();

    // ── SECTION 9: CUSTOM STYLES ──
    doc.add_paragraph("9. Custom Styles").style("Heading1");
    doc.add_style(
        StyleBuilder::paragraph("CustomHighlight", "Custom Highlight")
            .based_on("Normal")
            .paragraph_properties({
                let mut ppr = rdocx_oxml::properties::CT_PPr::default();
                ppr.shading = Some(rdocx_oxml::properties::CT_Shd {
                    val: "clear".to_string(),
                    color: None,
                    fill: Some("FFF2CC".to_string()),
                });
                ppr
            })
            .run_properties({
                let mut rpr = rdocx_oxml::properties::CT_RPr::default();
                rpr.bold = Some(true);
                rpr.color = Some("C45911".to_string());
                rpr
            }),
    );
    doc.add_paragraph("This paragraph uses a custom style: bold orange text on yellow background.")
        .style("CustomHighlight");

    doc.add_paragraph("");

    // ── SECTION 10: DOCUMENT INTELLIGENCE ──
    doc.add_paragraph("10. Document Intelligence API")
        .style("Heading1");

    let wc = doc.word_count();
    let hc = doc.headings().len();
    let ic = doc.images().len();
    let lc = doc.links().len();
    doc.add_paragraph(&format!("Word count: {wc}"));
    doc.add_paragraph(&format!("Heading count: {hc}"));
    doc.add_paragraph(&format!("Image count: {ic}"));
    doc.add_paragraph(&format!("Link count: {lc}"));

    let outline = doc.document_outline();
    doc.add_paragraph(&format!("Top-level outline nodes: {}", outline.len()));

    let issues = doc.audit_accessibility();
    doc.add_paragraph(&format!("Accessibility issues: {}", issues.len()));
    for issue in &issues {
        doc.add_bullet_list_item(&format!("{:?}: {}", issue.severity, issue.message), 0);
    }

    // ── SECTION 11: DOCUMENT MERGING ──
    doc.add_paragraph("");
    doc.add_paragraph("11. Document Merging").style("Heading1");
    {
        let mut other = Document::new();
        other.add_paragraph("This paragraph was merged from another document using append().");
        doc.append(&other);
    }

    doc.add_paragraph("");

    // ── SUMMARY ──
    doc.add_paragraph("Summary of Demonstrated Features")
        .style("Heading1");
    let features = [
        "Page setup: size, margins, header/footer distance, gutter",
        "Document metadata: title, author, subject, keywords",
        "Headers and footers: text, images, different first page",
        "Background images (full-page behind text)",
        "Table of Contents generation with bookmarks",
        "Text formatting: bold, italic, underline styles, strike, color, size, font",
        "Advanced run: superscript, subscript, caps, small caps, spacing, hidden",
        "Paragraph formatting: alignment, borders, shading, spacing, indentation",
        "Pagination controls: keep-with-next, keep-together, widow control",
        "Bullet and numbered lists with nesting",
        "Tab stops with dot/underscore/hyphen leaders",
        "Tables: borders, shading, column spans, row spans, vertical alignment, nested tables",
        "Row properties: header rows, exact height, cant-split",
        "Cell properties: width, no-wrap, vertical alignment",
        "Inline images and header images",
        "Placeholder replacement (single and batch)",
        "Regex-based find and replace",
        "Content insertion at specific positions",
        "Section breaks with mixed portrait/landscape",
        "Custom paragraph and character styles",
        "Document intelligence: word count, headings, outline, images, links",
        "Accessibility audit",
        "Document merging (append)",
        "Export: DOCX, PDF, HTML, Markdown",
    ];
    for f in &features {
        doc.add_bullet_list_item(f, 0);
    }
    doc.add_paragraph("");
    doc.add_paragraph("All features built entirely with the rdocx Rust crate.")
        .alignment(Alignment::Center)
        .shading("E2EFDA")
        .border_all(BorderStyle::Single, 2, "00AA00");

    doc
}

// =============================================================================
// 2. PROPOSAL DOCUMENT — Deep navy + gold scheme
// =============================================================================
fn generate_proposal(_samples_dir: &Path) -> Document {
    let mut doc = Document::new();

    // Colors: Navy #1B2A4A, Gold #C5922E, Light #F4F1EB
    doc.set_page_size(Length::inches(8.5), Length::inches(11.0));
    doc.set_margins(
        Length::inches(1.0),
        Length::inches(1.0),
        Length::inches(1.0),
        Length::inches(1.0),
    );
    doc.set_title("AI Platform Modernization Proposal");
    doc.set_author("Walter White");
    doc.set_subject("Technology Proposal");
    doc.set_keywords("proposal, AI, modernization, Tensorbee");

    // Banner header
    let logo_img = create_logo_png(220, 48);
    let banner = build_header_banner_xml(
        "rId1",
        &BannerOpts {
            bg_color: "1B2A4A",
            banner_width: 7772400,
            banner_height: 914400,
            logo_width: 2011680,
            logo_height: 438912,
            logo_x_offset: 295125,
            logo_y_offset: 237744,
        },
    );
    doc.set_raw_header_with_images(
        banner,
        &[("rId1", &logo_img, "logo.png")],
        rdocx_oxml::header_footer::HdrFtrType::Default,
    );
    doc.set_different_first_page(true);
    let cover_banner = build_header_banner_xml(
        "rId1",
        &BannerOpts {
            bg_color: "C5922E",
            banner_width: 7772400,
            banner_height: 914400,
            logo_width: 2011680,
            logo_height: 438912,
            logo_x_offset: 295125,
            logo_y_offset: 237744,
        },
    );
    doc.set_raw_header_with_images(
        cover_banner,
        &[("rId1", &logo_img, "logo.png")],
        rdocx_oxml::header_footer::HdrFtrType::First,
    );
    doc.set_margins(
        Length::twips(2292),
        Length::twips(1440),
        Length::twips(1440),
        Length::twips(1440),
    );
    doc.set_header_footer_distance(Length::twips(720), Length::twips(432));
    doc.set_footer("Tensorbee — Confidential");

    // Custom styles
    doc.add_style(
        StyleBuilder::paragraph("ProposalTitle", "Proposal Title")
            .based_on("Normal")
            .run_properties({
                let mut rpr = rdocx_oxml::properties::CT_RPr::default();
                rpr.bold = Some(true);
                rpr.font_ascii = Some("Georgia".to_string());
                rpr.font_hansi = Some("Georgia".to_string());
                rpr.sz = Some(rdocx_oxml::HalfPoint(56)); // half-points → 28pt
                rpr.color = Some("1B2A4A".to_string());
                rpr
            }),
    );

    // ── Cover Page ──
    for _ in 0..4 {
        doc.add_paragraph("");
    }
    doc.add_paragraph("AI Platform Modernization")
        .style("ProposalTitle")
        .alignment(Alignment::Center);
    doc.add_paragraph("")
        .alignment(Alignment::Center)
        .border_bottom(BorderStyle::Single, 8, "C5922E");
    {
        let mut p = doc.add_paragraph("").alignment(Alignment::Center);
        p.add_run("Prepared for Global Financial Corp.")
            .size(14.0)
            .color("666666")
            .italic(true);
    }
    {
        let mut p = doc.add_paragraph("").alignment(Alignment::Center);
        p.add_run("Prepared by: Walter White, CEO — Tensorbee")
            .size(12.0)
            .color("888888");
    }
    {
        let mut p = doc.add_paragraph("").alignment(Alignment::Center);
        p.add_run("February 22, 2026").size(12.0).color("888888");
    }

    // ── TOC ──
    doc.add_paragraph("").page_break_before(true);
    doc.add_paragraph("Table of Contents").style("Heading1");
    // We'll add a manual-style TOC since the insert_toc goes at a specific position
    doc.insert_toc(doc.content_count(), 2);

    // ── Executive Summary ──
    doc.add_paragraph("").page_break_before(true);
    doc.add_paragraph("Executive Summary").style("Heading1");
    doc.add_paragraph(
        "Tensorbee proposes a comprehensive modernization of Global Financial Corp's AI platform, \
         transitioning from legacy batch-processing systems to a real-time inference architecture. \
         This transformation will reduce model deployment time from weeks to hours, improve \
         prediction accuracy by an estimated 23%, and deliver $4.2M in annual operational savings.",
    )
    .first_line_indent(Length::inches(0.3));
    doc.add_paragraph(
        "The project spans three phases over 18 months, with a total investment of $2.8M. \
         Tensorbee brings deep expertise in ML infrastructure, having successfully completed \
         similar transformations for three Fortune 500 financial institutions.",
    )
    .first_line_indent(Length::inches(0.3));

    // Key metrics table
    doc.add_paragraph("");
    {
        let mut tbl = doc
            .add_table(5, 2)
            .borders(BorderStyle::Single, 4, "1B2A4A")
            .width_pct(80.0);
        tbl.cell(0, 0).unwrap().set_text("Key Metric");
        tbl.cell(0, 0).unwrap().shading("1B2A4A");
        tbl.cell(0, 1).unwrap().set_text("Value");
        tbl.cell(0, 1).unwrap().shading("1B2A4A");
        let rows = [
            ("Total Investment", "$2.8M"),
            ("Annual Savings", "$4.2M"),
            ("ROI (Year 1)", "150%"),
            ("Timeline", "18 months"),
        ];
        for (i, (k, v)) in rows.iter().enumerate() {
            tbl.cell(i + 1, 0).unwrap().set_text(k);
            if i % 2 == 0 {
                tbl.cell(i + 1, 0).unwrap().shading("F4F1EB");
            }
            tbl.cell(i + 1, 1).unwrap().set_text(v);
            if i % 2 == 0 {
                tbl.cell(i + 1, 1).unwrap().shading("F4F1EB");
            }
        }
    }

    // ── Problem Statement ──
    doc.add_paragraph("").page_break_before(true);
    doc.add_paragraph("Problem Statement").style("Heading1");
    doc.add_paragraph(
        "Global Financial Corp's current AI infrastructure faces three critical challenges:",
    );
    doc.add_numbered_list_item("Deployment Latency — New models take 3-4 weeks to deploy to production, compared to the industry benchmark of 2-3 days.", 0);
    doc.add_numbered_list_item("Scalability Constraints — The batch processing architecture cannot handle real-time inference demands during peak trading hours.", 0);
    doc.add_numbered_list_item("Technical Debt — Legacy Python 2.7 codebases and manual deployment scripts create reliability risks and slow iteration speed.", 0);

    // ── Proposed Solution ──
    doc.add_paragraph("").page_break_before(true);
    doc.add_paragraph("Proposed Solution").style("Heading1");

    doc.add_paragraph("Phase 1: Foundation (Months 1-6)")
        .style("Heading2");
    doc.add_bullet_list_item("Deploy Kubernetes-based ML serving infrastructure", 0);
    doc.add_bullet_list_item("Implement CI/CD pipeline for model deployment", 0);
    doc.add_bullet_list_item("Migrate top 5 production models to new platform", 0);

    doc.add_paragraph("Phase 2: Expansion (Months 7-12)")
        .style("Heading2");
    doc.add_bullet_list_item(
        "Real-time feature engineering pipeline (Apache Kafka + Flink)",
        0,
    );
    doc.add_bullet_list_item("A/B testing framework for model variants", 0);
    doc.add_bullet_list_item("Automated model monitoring and drift detection", 0);

    doc.add_paragraph("Phase 3: Optimization (Months 13-18)")
        .style("Heading2");
    doc.add_bullet_list_item("GPU inference optimization (TensorRT/ONNX Runtime)", 0);
    doc.add_bullet_list_item("Multi-region deployment for latency reduction", 0);
    doc.add_bullet_list_item(
        "Self-service model deployment portal for data science teams",
        0,
    );

    // ── Budget ──
    doc.add_paragraph("Budget Breakdown").style("Heading1");
    {
        let mut tbl = doc
            .add_table(6, 3)
            .borders(BorderStyle::Single, 4, "1B2A4A")
            .width_pct(100.0);
        tbl.cell(0, 0).unwrap().set_text("Category");
        tbl.cell(0, 0).unwrap().shading("1B2A4A");
        tbl.cell(0, 1).unwrap().set_text("Cost");
        tbl.cell(0, 1).unwrap().shading("1B2A4A");
        tbl.cell(0, 2).unwrap().set_text("Notes");
        tbl.cell(0, 2).unwrap().shading("1B2A4A");
        let items = [
            (
                "Infrastructure",
                "$450,000",
                "Cloud compute + GPU instances",
            ),
            ("Engineering Services", "$1,600,000", "12 FTE x 18 months"),
            (
                "Software Licenses",
                "$350,000",
                "Kafka, monitoring, CI/CD tools",
            ),
            (
                "Training & Enablement",
                "$200,000",
                "Team training, documentation",
            ),
            ("Contingency (10%)", "$200,000", "Risk buffer"),
        ];
        for (i, (cat, cost, note)) in items.iter().enumerate() {
            tbl.cell(i + 1, 0).unwrap().set_text(cat);
            tbl.cell(i + 1, 1).unwrap().set_text(cost);
            tbl.cell(i + 1, 2).unwrap().set_text(note);
            if i % 2 == 0 {
                tbl.cell(i + 1, 0).unwrap().shading("F4F1EB");
                tbl.cell(i + 1, 1).unwrap().shading("F4F1EB");
                tbl.cell(i + 1, 2).unwrap().shading("F4F1EB");
            }
        }
    }

    // ── Closing ──
    doc.add_paragraph("");
    doc.add_paragraph("Next Steps").style("Heading1");
    doc.add_numbered_list_item(
        "Schedule technical deep-dive with GFC engineering team (Week 1)",
        0,
    );
    doc.add_numbered_list_item("Finalize scope and sign SOW (Week 2-3)", 0);
    doc.add_numbered_list_item("Kick off Phase 1 with joint planning session (Week 4)", 0);

    doc.add_paragraph("");
    {
        let mut p = doc.add_paragraph("").alignment(Alignment::Center);
        p.add_run("Walter White")
            .bold(true)
            .size(14.0)
            .color("1B2A4A");
    }
    {
        let mut p = doc.add_paragraph("").alignment(Alignment::Center);
        p.add_run("CEO, Tensorbee | walter@tensorbee.com")
            .size(10.0)
            .color("888888");
    }

    doc
}

// =============================================================================
// 3. QUOTE / BILL OF MATERIALS — Teal + orange scheme
// =============================================================================
fn generate_quote(_samples_dir: &Path) -> Document {
    let mut doc = Document::new();

    // Colors: Teal #008B8B, Dark #1A3C3C, Orange #E07020, Light #F0F8F8
    doc.set_page_size(Length::inches(8.5), Length::inches(11.0));
    doc.set_margins(
        Length::inches(0.75),
        Length::inches(0.75),
        Length::inches(0.75),
        Length::inches(0.75),
    );
    doc.set_title("Quotation QT-2026-0147");
    doc.set_author("Walter White");
    doc.set_keywords("quote, BOM, Tensorbee");

    doc.set_header("Tensorbee — Quotation");
    doc.set_footer("QT-2026-0147 | Page");

    // ── Header Block ──
    {
        let mut p = doc.add_paragraph("").alignment(Alignment::Left);
        p.add_run("TENSORBEE")
            .bold(true)
            .size(28.0)
            .color("008B8B")
            .font("Helvetica");
    }
    doc.add_paragraph("123 Innovation Drive, Suite 400")
        .alignment(Alignment::Left);
    doc.add_paragraph("San Francisco, CA 94105 | +1 (415) 555-0199")
        .alignment(Alignment::Left);
    doc.add_paragraph("")
        .border_bottom(BorderStyle::Single, 8, "008B8B");

    // Quote meta
    doc.add_paragraph("");
    {
        let mut tbl = doc.add_table(4, 4).width_pct(100.0);
        tbl.cell(0, 0).unwrap().set_text("QUOTATION");
        tbl.cell(0, 0).unwrap().shading("008B8B").grid_span(2);
        tbl.cell(0, 2).unwrap().set_text("DATE");
        tbl.cell(0, 2).unwrap().shading("008B8B");
        tbl.cell(0, 3).unwrap().set_text("VALID UNTIL");
        tbl.cell(0, 3).unwrap().shading("008B8B");
        tbl.cell(1, 0).unwrap().set_text("Quote #:");
        tbl.cell(1, 1).unwrap().set_text("QT-2026-0147");
        tbl.cell(1, 2).unwrap().set_text("Feb 22, 2026");
        tbl.cell(1, 3).unwrap().set_text("Mar 22, 2026");
        tbl.cell(2, 0).unwrap().set_text("Prepared by:");
        tbl.cell(2, 1).unwrap().set_text("Walter White");
        tbl.cell(2, 2).unwrap().set_text("Payment:");
        tbl.cell(2, 3).unwrap().set_text("Net 30");
        tbl.cell(3, 0).unwrap().set_text("Customer:");
        tbl.cell(3, 1).unwrap().set_text("Meridian Dynamics LLC");
        tbl.cell(3, 2).unwrap().set_text("Currency:");
        tbl.cell(3, 3).unwrap().set_text("USD");
    }

    doc.add_paragraph("");

    // ── Bill of Materials ──
    {
        let mut p = doc.add_paragraph("");
        p.add_run("Bill of Materials")
            .bold(true)
            .size(16.0)
            .color("1A3C3C");
    }
    doc.add_paragraph("");

    {
        let mut tbl = doc
            .add_table(10, 6)
            .borders(BorderStyle::Single, 2, "008B8B")
            .width_pct(100.0)
            .layout_fixed();
        // Headers
        let hdrs = [
            "#",
            "Part Number",
            "Description",
            "Qty",
            "Unit Price",
            "Total",
        ];
        for (c, h) in hdrs.iter().enumerate() {
            tbl.cell(0, c).unwrap().set_text(h);
            tbl.cell(0, c).unwrap().shading("008B8B");
        }

        let items: Vec<(&str, &str, &str, &str, &str)> = vec![
            (
                "TB-GPU-A100",
                "NVIDIA A100 80GB GPU",
                "4",
                "$12,500.00",
                "$50,000.00",
            ),
            (
                "TB-SRV-R750",
                "Dell R750xa Server Chassis",
                "2",
                "$8,200.00",
                "$16,400.00",
            ),
            (
                "TB-RAM-256G",
                "256GB DDR5 ECC Memory Module",
                "8",
                "$890.00",
                "$7,120.00",
            ),
            (
                "TB-SSD-3840",
                "3.84TB NVMe U.2 SSD",
                "8",
                "$1,150.00",
                "$9,200.00",
            ),
            (
                "TB-NET-CX7",
                "ConnectX-7 200GbE NIC",
                "4",
                "$1,800.00",
                "$7,200.00",
            ),
            (
                "TB-SW-48P",
                "48-Port 100GbE Switch",
                "1",
                "$22,000.00",
                "$22,000.00",
            ),
            (
                "TB-CAB-RACK",
                "42U Server Rack + PDU",
                "1",
                "$4,500.00",
                "$4,500.00",
            ),
            (
                "TB-SVC-INST",
                "Installation & Configuration",
                "1",
                "$8,500.00",
                "$8,500.00",
            ),
            (
                "TB-SVC-SUPP",
                "3-Year Premium Support",
                "1",
                "$15,000.00",
                "$15,000.00",
            ),
        ];

        for (i, (pn, desc, qty, unit, total)) in items.iter().enumerate() {
            let row = i + 1;
            tbl.cell(row, 0).unwrap().set_text(&format!("{}", i + 1));
            tbl.cell(row, 1).unwrap().set_text(pn);
            tbl.cell(row, 2).unwrap().set_text(desc);
            tbl.cell(row, 3).unwrap().set_text(qty);
            tbl.cell(row, 4).unwrap().set_text(unit);
            tbl.cell(row, 5).unwrap().set_text(total);
            if i % 2 == 0 {
                for c in 0..6 {
                    tbl.cell(row, c).unwrap().shading("F0F8F8");
                }
            }
        }
    }

    doc.add_paragraph("");

    // ── Totals ──
    {
        let mut tbl = doc
            .add_table(4, 2)
            .borders(BorderStyle::Single, 2, "008B8B")
            .width(Length::inches(3.5))
            .alignment(Alignment::Right);
        tbl.cell(0, 0).unwrap().set_text("Subtotal");
        tbl.cell(0, 1).unwrap().set_text("$139,920.00");
        tbl.cell(1, 0).unwrap().set_text("Shipping & Handling");
        tbl.cell(1, 1).unwrap().set_text("$2,500.00");
        tbl.cell(2, 0).unwrap().set_text("Tax (8.625%)");
        tbl.cell(2, 1).unwrap().set_text("$12,068.10");
        tbl.cell(3, 0).unwrap().set_text("TOTAL");
        tbl.cell(3, 0).unwrap().shading("E07020");
        tbl.cell(3, 1).unwrap().set_text("$154,488.10");
        tbl.cell(3, 1).unwrap().shading("E07020");
    }

    doc.add_paragraph("");

    // ── Terms & Conditions ──
    {
        let mut p = doc.add_paragraph("");
        p.add_run("Terms & Conditions")
            .bold(true)
            .size(14.0)
            .color("1A3C3C");
    }
    doc.add_numbered_list_item(
        "This quotation is valid for 30 calendar days from the date of issue.",
        0,
    );
    doc.add_numbered_list_item(
        "All prices are in USD and exclusive of applicable taxes unless stated.",
        0,
    );
    doc.add_numbered_list_item("Standard lead time is 4-6 weeks from PO receipt.", 0);
    doc.add_numbered_list_item(
        "Payment terms: Net 30 from invoice date. 2% discount for payment within 10 days.",
        0,
    );
    doc.add_numbered_list_item(
        "Warranty: 3-year manufacturer warranty on all hardware components.",
        0,
    );
    doc.add_numbered_list_item(
        "Returns subject to 15% restocking fee if initiated after 14 days.",
        0,
    );

    doc.add_paragraph("");
    doc.add_paragraph("")
        .border_bottom(BorderStyle::Single, 4, "008B8B");
    doc.add_paragraph("");
    {
        let mut p = doc.add_paragraph("");
        p.add_run("Accepted by: ").bold(true);
        p.add_run("___________________________ Date: ___________");
    }
    {
        let mut p = doc.add_paragraph("");
        p.add_run("Print Name: ").bold(true);
        p.add_run("___________________________ Title: ___________");
    }

    doc
}

// =============================================================================
// 4. INVOICE — Crimson + charcoal scheme
// =============================================================================
fn generate_invoice(_samples_dir: &Path) -> Document {
    let mut doc = Document::new();

    // Colors: Crimson #B22222, Charcoal #333333, Light #FAF0F0
    doc.set_page_size(Length::inches(8.5), Length::inches(11.0));
    doc.set_margins(
        Length::inches(0.75),
        Length::inches(0.75),
        Length::inches(0.75),
        Length::inches(0.75),
    );
    doc.set_title("Invoice INV-2026-0392");
    doc.set_author("Walter White");
    doc.set_keywords("invoice, Tensorbee");

    doc.set_footer("Tensorbee — Thank you for your business!");

    // ── Company & Invoice Header ──
    {
        let mut tbl = doc.add_table(4, 2).width_pct(100.0);
        // Company name left, INVOICE right
        {
            let mut cell = tbl.cell(0, 0).unwrap();
            let mut p = cell.add_paragraph("");
            p.add_run("TENSORBEE")
                .bold(true)
                .size(32.0)
                .color("B22222")
                .font("Helvetica");
        }
        {
            let mut cell = tbl.cell(0, 1).unwrap();
            let mut p = cell.add_paragraph("");
            p.add_run("INVOICE").bold(true).size(32.0).color("333333");
        }
        tbl.cell(1, 0)
            .unwrap()
            .set_text("123 Innovation Drive, Suite 400");
        tbl.cell(1, 1).unwrap().set_text("Invoice #: INV-2026-0392");
        tbl.cell(2, 0).unwrap().set_text("San Francisco, CA 94105");
        tbl.cell(2, 1).unwrap().set_text("Date: February 22, 2026");
        tbl.cell(3, 0).unwrap().set_text("walter@tensorbee.com");
        tbl.cell(3, 1).unwrap().set_text("Due Date: March 24, 2026");
    }

    doc.add_paragraph("")
        .border_bottom(BorderStyle::Thick, 8, "B22222");
    doc.add_paragraph("");

    // ── Bill To / Ship To ──
    {
        let mut tbl = doc.add_table(4, 2).width_pct(100.0);
        tbl.cell(0, 0).unwrap().set_text("BILL TO");
        tbl.cell(0, 0).unwrap().shading("B22222");
        tbl.cell(0, 1).unwrap().set_text("SHIP TO");
        tbl.cell(0, 1).unwrap().shading("B22222");
        tbl.cell(1, 0).unwrap().set_text("Meridian Dynamics LLC");
        tbl.cell(1, 1).unwrap().set_text("Meridian Dynamics LLC");
        tbl.cell(2, 0).unwrap().set_text("456 Enterprise Blvd");
        tbl.cell(2, 1).unwrap().set_text("Attn: Server Room B");
        tbl.cell(3, 0).unwrap().set_text("Austin, TX 78701");
        tbl.cell(3, 1)
            .unwrap()
            .set_text("456 Enterprise Blvd, Austin, TX 78701");
    }

    doc.add_paragraph("");

    // ── Line Items ──
    {
        let mut tbl = doc
            .add_table(8, 5)
            .borders(BorderStyle::Single, 2, "B22222")
            .width_pct(100.0);
        let hdrs = ["Description", "Qty", "Unit Price", "Tax", "Amount"];
        for (c, h) in hdrs.iter().enumerate() {
            tbl.cell(0, c).unwrap().set_text(h);
            tbl.cell(0, c).unwrap().shading("B22222");
        }
        let items = [
            (
                "ML Infrastructure Setup — Phase 1",
                "1",
                "$45,000.00",
                "$3,881.25",
                "$48,881.25",
            ),
            (
                "Data Pipeline Development (160 hrs)",
                "160",
                "$225.00",
                "$3,105.00",
                "$39,105.00",
            ),
            (
                "GPU Cluster Configuration",
                "1",
                "$12,000.00",
                "$1,035.00",
                "$13,035.00",
            ),
            (
                "API Gateway Implementation",
                "1",
                "$18,500.00",
                "$1,595.63",
                "$20,095.63",
            ),
            (
                "Load Testing & QA (80 hrs)",
                "80",
                "$195.00",
                "$1,345.50",
                "$16,945.50",
            ),
            (
                "Documentation & Training",
                "1",
                "$8,000.00",
                "$690.00",
                "$8,690.00",
            ),
            (
                "Project Management (3 months)",
                "3",
                "$6,500.00",
                "$1,679.63",
                "$21,179.63",
            ),
        ];
        for (i, (desc, qty, unit, tax, amt)) in items.iter().enumerate() {
            let r = i + 1;
            tbl.cell(r, 0).unwrap().set_text(desc);
            tbl.cell(r, 1).unwrap().set_text(qty);
            tbl.cell(r, 2).unwrap().set_text(unit);
            tbl.cell(r, 3).unwrap().set_text(tax);
            tbl.cell(r, 4).unwrap().set_text(amt);
            if i % 2 == 0 {
                for c in 0..5 {
                    tbl.cell(r, c).unwrap().shading("FAF0F0");
                }
            }
        }
    }

    doc.add_paragraph("");

    // ── Totals ──
    {
        let mut tbl = doc
            .add_table(4, 2)
            .borders(BorderStyle::Single, 2, "B22222")
            .width(Length::inches(3.0))
            .alignment(Alignment::Right);
        tbl.cell(0, 0).unwrap().set_text("Subtotal");
        tbl.cell(0, 1).unwrap().set_text("$154,600.00");
        tbl.cell(1, 0).unwrap().set_text("Tax (8.625%)");
        tbl.cell(1, 1).unwrap().set_text("$13,334.25");
        tbl.cell(2, 0).unwrap().set_text("Discount (5%)");
        tbl.cell(2, 1).unwrap().set_text("-$7,730.00");
        tbl.cell(3, 0).unwrap().set_text("AMOUNT DUE");
        tbl.cell(3, 0).unwrap().shading("B22222");
        tbl.cell(3, 1).unwrap().set_text("$160,204.25");
        tbl.cell(3, 1).unwrap().shading("B22222");
    }

    doc.add_paragraph("");
    doc.add_paragraph("");

    // ── Payment Details ──
    {
        let mut p = doc.add_paragraph("");
        p.add_run("Payment Details")
            .bold(true)
            .size(14.0)
            .color("333333");
    }
    doc.add_paragraph("Bank: Silicon Valley Bank")
        .indent_left(Length::inches(0.3));
    doc.add_paragraph("Account: Tensorbee Inc. — 0847-2953-1120")
        .indent_left(Length::inches(0.3));
    doc.add_paragraph("Routing: 121140399")
        .indent_left(Length::inches(0.3));
    doc.add_paragraph("Swift: SVBKUS6S")
        .indent_left(Length::inches(0.3));

    doc.add_paragraph("");

    doc.add_paragraph("Please include invoice number INV-2026-0392 in the payment reference.")
        .shading("FAF0F0")
        .border_all(BorderStyle::Single, 2, "B22222");

    doc
}

// =============================================================================
// 5. REPORT — Forest green + earth tones, with images & hierarchical sections
// =============================================================================
fn generate_report(_samples_dir: &Path) -> Document {
    let mut doc = Document::new();

    // Colors: Forest #2D5016, Sage #6B8E23, Earth #8B7355, Cream #FFFAF0
    doc.set_page_size(Length::inches(8.5), Length::inches(11.0));
    doc.set_margins(
        Length::inches(1.0),
        Length::inches(1.0),
        Length::inches(1.0),
        Length::inches(1.0),
    );
    doc.set_title("Q4 2025 Environmental Impact Report");
    doc.set_author("Walter White");
    doc.set_subject("Quarterly Environmental Report");
    doc.set_keywords("environment, sustainability, report, Tensorbee");

    doc.set_different_first_page(true);
    doc.set_first_page_header("");
    doc.set_header("Tensorbee — Q4 2025 Environmental Impact Report");
    doc.set_footer("CONFIDENTIAL — Page");

    // ── Cover ──
    let cover_bg = create_sample_png(612, 792, [20, 50, 15]);
    doc.add_background_image(&cover_bg, "report_cover.png");

    for _ in 0..5 {
        doc.add_paragraph("");
    }
    {
        let mut p = doc.add_paragraph("").alignment(Alignment::Center);
        p.add_run("Q4 2025")
            .bold(true)
            .size(48.0)
            .color("FFFFFF")
            .font("Georgia");
    }
    {
        let mut p = doc.add_paragraph("").alignment(Alignment::Center);
        p.add_run("Environmental Impact Report")
            .size(24.0)
            .color("90EE90")
            .font("Georgia")
            .italic(true);
    }
    doc.add_paragraph("");
    {
        let mut p = doc.add_paragraph("").alignment(Alignment::Center);
        p.add_run("Tensorbee — Sustainability Division")
            .size(14.0)
            .color("C0C0C0");
    }
    {
        let mut p = doc.add_paragraph("").alignment(Alignment::Center);
        p.add_run("Prepared by Walter White, Chief Sustainability Officer")
            .size(11.0)
            .color("AAAAAA");
    }

    // ── TOC ──
    doc.add_paragraph("").page_break_before(true);
    doc.insert_toc(doc.content_count(), 3);

    // ── Section 1: Executive Overview ──
    doc.add_paragraph("").page_break_before(true);
    doc.add_paragraph("Executive Overview").style("Heading1");
    doc.add_paragraph(
        "This report presents Tensorbee's environmental performance for Q4 2025. Our \
         sustainability initiatives have yielded a 34% reduction in carbon emissions compared \
         to Q4 2024, exceeding our target of 25%. Key achievements include the transition to \
         100% renewable energy in our primary data centers and the launch of our carbon offset \
         marketplace.",
    )
    .first_line_indent(Length::inches(0.3));

    // Image: performance chart
    let chart_img = create_chart_png(400, 200);
    doc.add_paragraph("");
    doc.add_picture(
        &chart_img,
        "performance_chart.png",
        Length::inches(5.0),
        Length::inches(2.5),
    )
    .alignment(Alignment::Center);
    {
        let mut p = doc.add_paragraph("").alignment(Alignment::Center);
        p.add_run("Figure 1: Quarterly Carbon Emissions (tonnes CO2e)")
            .italic(true)
            .size(9.0)
            .color("666666");
    }

    // ── Section 2: Energy Consumption ──
    doc.add_paragraph("").page_break_before(true);
    doc.add_paragraph("Energy Consumption").style("Heading1");

    doc.add_paragraph("Data Center Operations")
        .style("Heading2");
    doc.add_paragraph(
        "Our three primary data centers consumed a combined 4.2 GWh in Q4 2025, \
         a 12% reduction from Q3 2025 driven by improved cooling efficiency and \
         server consolidation.",
    );

    // Data table
    {
        let mut tbl = doc
            .add_table(5, 4)
            .borders(BorderStyle::Single, 4, "2D5016")
            .width_pct(100.0);
        let hdrs = ["Data Center", "Capacity (MW)", "Usage (GWh)", "PUE"];
        for (c, h) in hdrs.iter().enumerate() {
            tbl.cell(0, c).unwrap().set_text(h);
            tbl.cell(0, c).unwrap().shading("2D5016");
        }
        let rows = [
            ("San Francisco (Primary)", "3.2", "1.8", "1.12"),
            ("Dublin (EU)", "2.1", "1.2", "1.18"),
            ("Singapore (APAC)", "1.8", "1.2", "1.24"),
            ("TOTAL", "7.1", "4.2", "1.17 avg"),
        ];
        for (i, (dc, cap, use_, pue)) in rows.iter().enumerate() {
            tbl.cell(i + 1, 0).unwrap().set_text(dc);
            tbl.cell(i + 1, 1).unwrap().set_text(cap);
            tbl.cell(i + 1, 2).unwrap().set_text(use_);
            tbl.cell(i + 1, 3).unwrap().set_text(pue);
            if i == 3 {
                for c in 0..4 {
                    tbl.cell(i + 1, c).unwrap().shading("E2EFDA");
                }
            }
        }
    }

    doc.add_paragraph("");

    doc.add_paragraph("Renewable Energy Mix").style("Heading2");
    doc.add_paragraph("Breakdown of energy sources across all facilities:");

    doc.add_bullet_list_item("Solar PV: 42% (1.76 GWh)", 0);
    doc.add_bullet_list_item("Wind Power Purchase Agreements: 38% (1.60 GWh)", 0);
    doc.add_bullet_list_item("Hydroelectric: 12% (0.50 GWh)", 0);
    doc.add_bullet_list_item("Grid (non-renewable): 8% (0.34 GWh)", 0);

    // ── Section 3: Water & Waste ──
    doc.add_paragraph("Water & Waste Management")
        .style("Heading1");

    doc.add_paragraph("Water Usage").style("Heading2");
    doc.add_paragraph(
        "Total water consumption was 12.4 million gallons, a 15% reduction from Q3. \
         Our closed-loop cooling systems now recycle 78% of water used in cooling operations.",
    );

    doc.add_paragraph("Waste Reduction").style("Heading2");
    doc.add_paragraph("E-waste management results for Q4:")
        .keep_with_next(true);
    {
        let mut tbl = doc
            .add_table(4, 3)
            .borders(BorderStyle::Single, 2, "6B8E23")
            .width_pct(80.0);
        tbl.cell(0, 0).unwrap().set_text("Category");
        tbl.cell(0, 0).unwrap().shading("6B8E23");
        tbl.cell(0, 1).unwrap().set_text("Weight (kg)");
        tbl.cell(0, 1).unwrap().shading("6B8E23");
        tbl.cell(0, 2).unwrap().set_text("Recycled %");
        tbl.cell(0, 2).unwrap().shading("6B8E23");
        let rows = [
            ("Server Hardware", "2,450", "94%"),
            ("Networking Equipment", "820", "91%"),
            ("Storage Media", "340", "99%"),
        ];
        for (i, (cat, wt, pct)) in rows.iter().enumerate() {
            tbl.cell(i + 1, 0).unwrap().set_text(cat);
            tbl.cell(i + 1, 1).unwrap().set_text(wt);
            tbl.cell(i + 1, 2).unwrap().set_text(pct);
        }
    }

    // ── Section 4: Initiatives ──
    doc.add_paragraph("").page_break_before(true);
    doc.add_paragraph("Q1 2026 Initiatives").style("Heading1");

    doc.add_paragraph("Planned Programs").style("Heading2");
    doc.add_numbered_list_item(
        "Deploy on-site battery storage at SF data center (2 MWh capacity)",
        0,
    );
    doc.add_numbered_list_item("Pilot immersion cooling in Dublin facility", 0);
    doc.add_numbered_list_item("Launch employee commute carbon offset program", 0);
    doc.add_numbered_list_item("Achieve ISO 14001 certification for Singapore facility", 0);

    doc.add_paragraph("Investment Targets").style("Heading2");
    doc.add_paragraph("Sustainability CapEx allocation for FY2026:")
        .keep_with_next(true);
    {
        let mut tbl = doc
            .add_table(5, 2)
            .borders(BorderStyle::Single, 4, "2D5016");
        tbl.cell(0, 0).unwrap().set_text("Initiative");
        tbl.cell(0, 0).unwrap().shading("2D5016");
        tbl.cell(0, 1).unwrap().set_text("Budget");
        tbl.cell(0, 1).unwrap().shading("2D5016");
        let rows = [
            ("Battery Storage", "$1.2M"),
            ("Immersion Cooling Pilot", "$800K"),
            ("Solar Panel Expansion", "$2.1M"),
            ("Carbon Credits", "$500K"),
        ];
        for (i, (init, budget)) in rows.iter().enumerate() {
            tbl.cell(i + 1, 0).unwrap().set_text(init);
            tbl.cell(i + 1, 1).unwrap().set_text(budget);
            if i % 2 == 0 {
                tbl.cell(i + 1, 0).unwrap().shading("FFFAF0");
                tbl.cell(i + 1, 1).unwrap().shading("FFFAF0");
            }
        }
    }

    // Image: sustainability roadmap
    let roadmap_img = create_sample_png(500, 100, [40, 80, 30]);
    doc.add_paragraph("");
    doc.add_picture(
        &roadmap_img,
        "roadmap.png",
        Length::inches(6.0),
        Length::inches(1.2),
    )
    .alignment(Alignment::Center);
    {
        let mut p = doc.add_paragraph("").alignment(Alignment::Center);
        p.add_run("Figure 2: 2026 Sustainability Roadmap")
            .italic(true)
            .size(9.0)
            .color("666666");
    }

    doc.add_paragraph("");
    doc.add_paragraph(
        "For questions about this report, contact Walter White at sustainability@tensorbee.com.",
    )
    .shading("FFFAF0")
    .border_all(BorderStyle::Single, 2, "2D5016");

    doc
}

// =============================================================================
// 6. LETTER — Slate blue + silver scheme
// =============================================================================
fn generate_letter(_samples_dir: &Path) -> Document {
    let mut doc = Document::new();

    // Colors: Slate #4A5568, Blue accent #3182CE
    doc.set_page_size(Length::inches(8.5), Length::inches(11.0));
    doc.set_margins(
        Length::inches(1.25),
        Length::inches(1.25),
        Length::inches(1.25),
        Length::inches(1.25),
    );
    doc.set_title("Business Letter");
    doc.set_author("Walter White");

    // Banner header using raw XML
    let logo_img = create_logo_png(220, 48);
    let banner = build_header_banner_xml(
        "rId1",
        &BannerOpts {
            bg_color: "4A5568",
            banner_width: 7772400,
            banner_height: 731520, // ~0.8"
            logo_width: 2011680,
            logo_height: 438912,
            logo_x_offset: 295125,
            logo_y_offset: 146304,
        },
    );
    doc.set_raw_header_with_images(
        banner,
        &[("rId1", &logo_img, "logo.png")],
        rdocx_oxml::header_footer::HdrFtrType::Default,
    );
    doc.set_margins(
        Length::twips(2000),
        Length::twips(1800),
        Length::twips(1440),
        Length::twips(1800),
    );
    doc.set_header_footer_distance(Length::twips(720), Length::twips(432));

    // Footer with contact
    doc.set_footer(
        "Tensorbee | 123 Innovation Drive, Suite 400 | San Francisco, CA 94105 | tensorbee.com",
    );

    // ── Sender Address ──
    doc.add_paragraph("Walter White");
    doc.add_paragraph("Chief Executive Officer");
    doc.add_paragraph("Tensorbee");
    doc.add_paragraph("123 Innovation Drive, Suite 400");
    doc.add_paragraph("San Francisco, CA 94105");
    doc.add_paragraph("");
    doc.add_paragraph("February 22, 2026");

    doc.add_paragraph("");

    // ── Recipient ──
    doc.add_paragraph("Ms. Sarah Chen");
    doc.add_paragraph("VP of Engineering");
    doc.add_paragraph("Meridian Dynamics LLC");
    doc.add_paragraph("456 Enterprise Boulevard");
    doc.add_paragraph("Austin, TX 78701");

    doc.add_paragraph("");

    // ── Subject ──
    {
        let mut p = doc.add_paragraph("");
        p.add_run("Re: Strategic Technology Partnership — Phase 2 Expansion")
            .bold(true);
    }

    doc.add_paragraph("");

    // ── Body ──
    doc.add_paragraph("Dear Ms. Chen,");
    doc.add_paragraph("");

    doc.add_paragraph(
        "I am writing to express Tensorbee's enthusiasm for expanding our strategic technology \
         partnership with Meridian Dynamics. Following the successful completion of Phase 1, \
         which delivered a 40% improvement in your ML inference pipeline throughput, we believe \
         the foundation is firmly established for an ambitious Phase 2 engagement.",
    )
    .first_line_indent(Length::inches(0.5));

    doc.add_paragraph(
        "During our recent executive review, your team highlighted three priority areas for \
         the next phase of collaboration:",
    )
    .first_line_indent(Length::inches(0.5));

    doc.add_numbered_list_item(
        "Real-time anomaly detection for your financial transaction monitoring system, \
         targeting sub-100ms latency at 50,000 transactions per second.",
        0,
    );
    doc.add_numbered_list_item(
        "Federated learning infrastructure to enable model training across your distributed \
         data centers without centralizing sensitive financial data.",
        0,
    );
    doc.add_numbered_list_item(
        "MLOps automation to reduce your model deployment cycle from the current 5 days \
         to under 4 hours.",
        0,
    );

    doc.add_paragraph(
        "Our engineering team has prepared a detailed technical proposal addressing each of \
         these areas. We have allocated a dedicated team of eight senior engineers, led by \
         our CTO, to ensure continuity with the Phase 1 team your organization has already \
         built a productive working relationship with.",
    )
    .first_line_indent(Length::inches(0.5));

    doc.add_paragraph(
        "I would welcome the opportunity to present our Phase 2 proposal in person. My \
         assistant will reach out to coordinate a meeting at your convenience during the \
         first week of March.",
    )
    .first_line_indent(Length::inches(0.5));

    doc.add_paragraph("");

    doc.add_paragraph("Warm regards,");
    doc.add_paragraph("");
    doc.add_paragraph("");

    // Signature line
    doc.add_paragraph("")
        .border_bottom(BorderStyle::Single, 4, "4A5568");
    {
        let mut p = doc.add_paragraph("");
        p.add_run("Walter White").bold(true).size(12.0);
    }
    {
        let mut p = doc.add_paragraph("");
        p.add_run("Chief Executive Officer, Tensorbee")
            .italic(true)
            .size(10.0)
            .color("666666");
    }
    {
        let mut p = doc.add_paragraph("");
        p.add_run("walter@tensorbee.com | +1 (415) 555-0199")
            .size(10.0)
            .color("888888");
    }

    doc
}

// =============================================================================
// 7. EMPLOYMENT CONTRACT — Purple + charcoal scheme
// =============================================================================
fn generate_contract(_samples_dir: &Path) -> Document {
    let mut doc = Document::new();

    // Colors: Purple #5B2C6F, Plum #8E44AD, Gray #2C3E50
    doc.set_page_size(Length::inches(8.5), Length::inches(11.0));
    doc.set_margins(
        Length::inches(1.25),
        Length::inches(1.0),
        Length::inches(1.0),
        Length::inches(1.0),
    );
    doc.set_title("Employment Agreement");
    doc.set_author("Tensorbee HR Department");
    doc.set_subject("Employment Contract — Walter White");
    doc.set_keywords("employment, contract, agreement, Tensorbee");

    doc.set_header("TENSORBEE — Employment Agreement");
    doc.set_footer("Employment Agreement — Walter White — Page");

    // ── Title Block ──
    doc.add_paragraph("")
        .border_bottom(BorderStyle::Thick, 12, "5B2C6F");
    {
        let mut p = doc.add_paragraph("").alignment(Alignment::Center);
        p.add_run("EMPLOYMENT AGREEMENT")
            .bold(true)
            .size(24.0)
            .color("5B2C6F")
            .font("Georgia");
    }
    doc.add_paragraph("")
        .border_bottom(BorderStyle::Thick, 12, "5B2C6F");
    doc.add_paragraph("");

    // ── Parties ──
    doc.add_paragraph(
        "This Employment Agreement (\"Agreement\") is entered into as of February 22, 2026 \
         (\"Effective Date\"), by and between:",
    );

    doc.add_paragraph("");

    {
        let mut p = doc.add_paragraph("").indent_left(Length::inches(0.5));
        p.add_run("EMPLOYER: ").bold(true);
        p.add_run(
            "Tensorbee, Inc., a Delaware corporation with its principal place of business at \
                   123 Innovation Drive, Suite 400, San Francisco, CA 94105 (\"Company\")",
        );
    }
    doc.add_paragraph("");
    {
        let mut p = doc.add_paragraph("").indent_left(Length::inches(0.5));
        p.add_run("EMPLOYEE: ").bold(true);
        p.add_run(
            "Walter White, residing at 308 Negra Arroyo Lane, Albuquerque, NM 87104 (\"Employee\")",
        );
    }

    doc.add_paragraph("");
    doc.add_paragraph("The Company and Employee are collectively referred to as the \"Parties.\"");
    doc.add_paragraph("");

    // ── Article 1: Position and Duties ──
    doc.add_paragraph("Article 1 — Position and Duties")
        .style("Heading1");
    {
        let mut p = doc.add_paragraph("");
        p.add_run("1.1 ").bold(true);
        p.add_run("The Company hereby employs the Employee as ");
        p.add_run("Chief Executive Officer (CEO)")
            .bold(true)
            .italic(true);
        p.add_run(", reporting directly to the Board of Directors.");
    }
    {
        let mut p = doc.add_paragraph("");
        p.add_run("1.2 ").bold(true);
        p.add_run("The Employee shall devote full working time, attention, and best efforts to \
                   the performance of duties as reasonably assigned by the Board, including but not limited to:");
    }
    doc.add_bullet_list_item(
        "Setting and executing the Company's strategic vision and business plan",
        0,
    );
    doc.add_bullet_list_item(
        "Overseeing all operations, engineering, and commercial activities",
        0,
    );
    doc.add_bullet_list_item(
        "Representing the Company to investors, customers, and the public",
        0,
    );
    doc.add_bullet_list_item("Recruiting, developing, and retaining key talent", 0);

    {
        let mut p = doc.add_paragraph("");
        p.add_run("1.3 ").bold(true);
        p.add_run(
            "The Employee's primary work location shall be the Company's San Francisco \
                   headquarters, with reasonable travel as required by business needs.",
        );
    }

    // ── Article 2: Compensation ──
    doc.add_paragraph("Article 2 — Compensation and Benefits")
        .style("Heading1");
    {
        let mut p = doc.add_paragraph("");
        p.add_run("2.1 Base Salary. ").bold(true);
        p.add_run("The Company shall pay the Employee an annual base salary of ");
        p.add_run("$375,000.00").bold(true);
        p.add_run(" (Three Hundred Seventy-Five Thousand Dollars), payable in accordance with the \
                   Company's standard payroll schedule, less applicable withholdings and deductions.");
    }
    {
        let mut p = doc.add_paragraph("");
        p.add_run("2.2 Annual Bonus. ").bold(true);
        p.add_run("The Employee shall be eligible for an annual performance bonus of up to ");
        p.add_run("40%").bold(true);
        p.add_run(
            " of base salary, based on achievement of mutually agreed performance objectives.",
        );
    }
    {
        let mut p = doc.add_paragraph("");
        p.add_run("2.3 Equity. ").bold(true);
        p.add_run("Subject to Board approval, the Employee shall receive a stock option grant of ");
        p.add_run("500,000 shares").bold(true);
        p.add_run(
            " of the Company's common stock, vesting over four (4) years with a one-year cliff, \
                   at an exercise price equal to the fair market value on the date of grant.",
        );
    }

    // Compensation summary table
    doc.add_paragraph("");
    {
        let mut tbl = doc
            .add_table(5, 2)
            .borders(BorderStyle::Single, 4, "5B2C6F")
            .width_pct(70.0)
            .alignment(Alignment::Center);
        tbl.cell(0, 0).unwrap().set_text("Compensation Element");
        tbl.cell(0, 0).unwrap().shading("5B2C6F");
        tbl.cell(0, 1).unwrap().set_text("Value");
        tbl.cell(0, 1).unwrap().shading("5B2C6F");
        let items = [
            ("Base Salary", "$375,000/year"),
            ("Target Bonus", "Up to 40% ($150,000)"),
            ("Equity Grant", "500,000 shares (4yr vest)"),
            ("Total Target Comp", "$525,000 + equity"),
        ];
        for (i, (elem, val)) in items.iter().enumerate() {
            tbl.cell(i + 1, 0).unwrap().set_text(elem);
            tbl.cell(i + 1, 1).unwrap().set_text(val);
            if i == 3 {
                tbl.cell(i + 1, 0).unwrap().shading("E8DAEF");
                tbl.cell(i + 1, 1).unwrap().shading("E8DAEF");
            }
        }
    }

    // ── Article 3: Benefits ──
    doc.add_paragraph("").page_break_before(true);
    doc.add_paragraph("Article 3 — Benefits").style("Heading1");
    {
        let mut p = doc.add_paragraph("");
        p.add_run("3.1 ").bold(true);
        p.add_run(
            "The Employee shall be entitled to participate in all benefit programs \
                   generally available to senior executives, including:",
        );
    }
    doc.add_bullet_list_item(
        "Medical, dental, and vision insurance (100% premium coverage for employee and dependents)",
        0,
    );
    doc.add_bullet_list_item("401(k) retirement plan with 6% company match", 0);
    doc.add_bullet_list_item("Life insurance and long-term disability coverage", 0);
    doc.add_bullet_list_item("Annual professional development allowance of $10,000", 0);

    {
        let mut p = doc.add_paragraph("");
        p.add_run("3.2 Paid Time Off. ").bold(true);
        p.add_run(
            "The Employee shall receive 25 days of paid vacation per year, plus Company holidays, \
                   accruing on a monthly basis.",
        );
    }

    // ── Article 4: Term and Termination ──
    doc.add_paragraph("Article 4 — Term and Termination")
        .style("Heading1");
    {
        let mut p = doc.add_paragraph("");
        p.add_run("4.1 At-Will Employment. ").bold(true);
        p.add_run(
            "This Agreement is for at-will employment and may be terminated by either Party \
                   at any time, with or without cause, subject to the notice provisions herein.",
        );
    }
    {
        let mut p = doc.add_paragraph("");
        p.add_run("4.2 Notice Period. ").bold(true);
        p.add_run("Either Party shall provide ");
        p.add_run("ninety (90) days").bold(true);
        p.add_run(" written notice of termination, or pay in lieu thereof.");
    }
    {
        let mut p = doc.add_paragraph("");
        p.add_run("4.3 Severance. ").bold(true);
        p.add_run("In the event of termination by the Company without Cause, the Employee shall \
                   receive (a) twelve (12) months of base salary continuation, (b) pro-rata bonus \
                   for the year of termination, and (c) twelve (12) months of COBRA premium coverage.");
    }

    // ── Article 5: Confidentiality ──
    doc.add_paragraph("Article 5 — Confidentiality and IP")
        .style("Heading1");
    {
        let mut p = doc.add_paragraph("");
        p.add_run("5.1 ").bold(true);
        p.add_run(
            "The Employee agrees to maintain strict confidentiality of all proprietary \
                   information, trade secrets, and business plans of the Company during and after \
                   employment.",
        );
    }
    {
        let mut p = doc.add_paragraph("");
        p.add_run("5.2 ").bold(true);
        p.add_run(
            "All intellectual property, inventions, and works of authorship created by the \
                   Employee during the term of employment and within the scope of duties shall be \
                   the sole and exclusive property of the Company.",
        );
    }

    // ── Article 6: Non-Compete ──
    doc.add_paragraph("Article 6 — Non-Competition")
        .style("Heading1");
    {
        let mut p = doc.add_paragraph("");
        p.add_run("6.1 ").bold(true);
        p.add_run("For a period of twelve (12) months following termination, the Employee agrees \
                   not to engage in any business that directly competes with the Company's core \
                   business of AI infrastructure and ML operations services, within the United States.");
    }

    // ── Article 7: General Provisions ──
    doc.add_paragraph("Article 7 — General Provisions")
        .style("Heading1");
    {
        let mut p = doc.add_paragraph("");
        p.add_run("7.1 Governing Law. ").bold(true);
        p.add_run(
            "This Agreement shall be governed by and construed in accordance with the laws of \
                   the State of California.",
        );
    }
    {
        let mut p = doc.add_paragraph("");
        p.add_run("7.2 Entire Agreement. ").bold(true);
        p.add_run(
            "This Agreement constitutes the entire agreement between the Parties and supersedes \
                   all prior negotiations, representations, and agreements.",
        );
    }
    {
        let mut p = doc.add_paragraph("");
        p.add_run("7.3 Amendment. ").bold(true);
        p.add_run(
            "This Agreement may only be amended by a written instrument signed by both Parties.",
        );
    }

    // ── Signature Block ──
    doc.add_paragraph("").page_break_before(true);
    doc.add_paragraph("IN WITNESS WHEREOF, the Parties have executed this Employment Agreement as of the Effective Date.")
        .space_after(Length::pt(24.0));

    // Signature table
    {
        let mut tbl = doc.add_table(6, 2).width_pct(100.0);
        tbl.cell(0, 0).unwrap().set_text("FOR THE COMPANY:");
        tbl.cell(0, 0).unwrap().shading("5B2C6F");
        tbl.cell(0, 1).unwrap().set_text("EMPLOYEE:");
        tbl.cell(0, 1).unwrap().shading("5B2C6F");

        tbl.cell(1, 0).unwrap().set_text("");
        tbl.cell(1, 1).unwrap().set_text("");
        tbl.row(1).unwrap().height(Length::pt(40.0));

        tbl.cell(2, 0)
            .unwrap()
            .set_text("Signature: ___________________________");
        tbl.cell(2, 1)
            .unwrap()
            .set_text("Signature: ___________________________");
        tbl.cell(3, 0)
            .unwrap()
            .set_text("Name: Board Representative");
        tbl.cell(3, 1).unwrap().set_text("Name: Walter White");
        tbl.cell(4, 0)
            .unwrap()
            .set_text("Title: Chair, Board of Directors");
        tbl.cell(4, 1)
            .unwrap()
            .set_text("Title: Chief Executive Officer");
        tbl.cell(5, 0).unwrap().set_text("Date: _______________");
        tbl.cell(5, 1).unwrap().set_text("Date: _______________");
    }

    doc
}

// =============================================================================
// PNG generation helpers
// =============================================================================

/// Create a gradient sample PNG (used for backgrounds, charts, etc.)
fn create_sample_png(width: u32, height: u32, base_color: [u8; 3]) -> Vec<u8> {
    let mut pixels = Vec::with_capacity((width * height * 4) as usize);
    for y in 0..height {
        for x in 0..width {
            let fy = y as f64 / height as f64;
            let fx = x as f64 / width as f64;
            let r = (base_color[0] as f64 + (1.0 - fy) * 40.0).min(255.0) as u8;
            let g = (base_color[1] as f64 + fx * 30.0).min(255.0) as u8;
            let b = (base_color[2] as f64 + fy * 60.0).min(255.0) as u8;
            pixels.extend_from_slice(&[r, g, b, 255]);
        }
    }
    encode_png(width, height, &pixels)
}

/// Create a simple bar chart PNG for the report.
fn create_chart_png(width: u32, height: u32) -> Vec<u8> {
    let mut pixels = vec![0u8; (width * height * 4) as usize];
    // White background
    for i in 0..pixels.len() / 4 {
        pixels[i * 4] = 255;
        pixels[i * 4 + 1] = 255;
        pixels[i * 4 + 2] = 255;
        pixels[i * 4 + 3] = 255;
    }
    // Draw simple bars
    let bars: Vec<(u32, u32, [u8; 3])> = vec![
        (30, 150, [45, 80, 22]),  // Q1
        (100, 120, [45, 80, 22]), // Q2
        (170, 100, [45, 80, 22]), // Q3
        (240, 70, [45, 80, 22]),  // Q4 (best)
    ];
    for (bx, bar_height, color) in &bars {
        let bar_width = 50;
        let y_start = height - bar_height;
        for y in y_start..height {
            for x in *bx..(*bx + bar_width).min(width) {
                let idx = ((y * width + x) * 4) as usize;
                if idx + 3 < pixels.len() {
                    pixels[idx] = color[0];
                    pixels[idx + 1] = color[1];
                    pixels[idx + 2] = color[2];
                    pixels[idx + 3] = 255;
                }
            }
        }
    }
    encode_png(width, height, &pixels)
}

/// Create a logo placeholder PNG (white rectangle on transparent).
fn create_logo_png(width: u32, height: u32) -> Vec<u8> {
    let mut pixels = Vec::with_capacity((width * height * 4) as usize);
    for y in 0..height {
        for x in 0..width {
            let in_text_area =
                x > width / 8 && x < width * 7 / 8 && y > height / 4 && y < height * 3 / 4;
            if in_text_area {
                pixels.extend_from_slice(&[255, 255, 255, 255]);
            } else {
                pixels.extend_from_slice(&[255, 255, 255, 40]);
            }
        }
    }
    encode_png(width, height, &pixels)
}

// =============================================================================
// PNG encoding (no external dependencies)
// =============================================================================

fn encode_png(width: u32, height: u32, pixels: &[u8]) -> Vec<u8> {
    let mut png = Vec::new();
    {
        use std::io::Write as _;
        png.write_all(&[137, 80, 78, 71, 13, 10, 26, 10]).unwrap();

        let mut ihdr = Vec::new();
        ihdr.extend_from_slice(&width.to_be_bytes());
        ihdr.extend_from_slice(&height.to_be_bytes());
        ihdr.extend_from_slice(&[8, 6, 0, 0, 0]); // 8-bit RGBA
        write_chunk(&mut png, b"IHDR", &ihdr);

        let mut raw = Vec::new();
        for y in 0..height {
            raw.push(0); // filter: None
            let s = (y * width * 4) as usize;
            raw.extend_from_slice(&pixels[s..s + (width * 4) as usize]);
        }
        write_chunk(&mut png, b"IDAT", &zlib_store(&raw));
        write_chunk(&mut png, b"IEND", &[]);
    }
    png
}

fn write_chunk(out: &mut Vec<u8>, ct: &[u8; 4], data: &[u8]) {
    use std::io::Write as _;
    out.write_all(&(data.len() as u32).to_be_bytes()).unwrap();
    out.write_all(ct).unwrap();
    out.write_all(data).unwrap();
    out.write_all(&crc32(ct, data).to_be_bytes()).unwrap();
}

fn crc32(ct: &[u8], data: &[u8]) -> u32 {
    static T: std::sync::LazyLock<[u32; 256]> = std::sync::LazyLock::new(|| {
        let mut t = [0u32; 256];
        for n in 0..256u32 {
            let mut c = n;
            for _ in 0..8 {
                c = if c & 1 != 0 {
                    0xEDB88320 ^ (c >> 1)
                } else {
                    c >> 1
                };
            }
            t[n as usize] = c;
        }
        t
    });
    let mut c = 0xFFFFFFFF_u32;
    for &b in ct.iter().chain(data) {
        c = T[((c ^ b as u32) & 0xFF) as usize] ^ (c >> 8);
    }
    c ^ 0xFFFFFFFF
}

fn zlib_store(data: &[u8]) -> Vec<u8> {
    let mut out = vec![0x78, 0x01];
    let chunks: Vec<&[u8]> = data.chunks(65535).collect();
    for (i, chunk) in chunks.iter().enumerate() {
        let last = i == chunks.len() - 1;
        out.push(if last { 0x01 } else { 0x00 });
        let len = chunk.len() as u16;
        out.extend_from_slice(&len.to_le_bytes());
        out.extend_from_slice(&(!len).to_le_bytes());
        out.extend_from_slice(chunk);
    }
    let (mut a, mut b) = (1u32, 0u32);
    for &byte in data {
        a = (a + byte as u32) % 65521;
        b = (b + a) % 65521;
    }
    out.extend_from_slice(&((b << 16) | a).to_be_bytes());
    out
}

// =============================================================================
// Header banner builder (DrawingML group shape via raw XML)
// =============================================================================

struct BannerOpts<'a> {
    bg_color: &'a str,
    banner_width: i64,
    banner_height: i64,
    logo_width: i64,
    logo_height: i64,
    logo_x_offset: i64,
    logo_y_offset: i64,
}

fn build_header_banner_xml(image_rel_id: &str, opts: &BannerOpts) -> Vec<u8> {
    let mut xml = String::with_capacity(2048);
    write!(
        xml,
        r#"<?xml version="1.0" encoding="UTF-8" standalone="yes"?>"#
    )
    .unwrap();
    write!(xml, r#"<w:hdr "#).unwrap();
    write!(
        xml,
        r#"xmlns:w="http://schemas.openxmlformats.org/wordprocessingml/2006/main" "#
    )
    .unwrap();
    write!(
        xml,
        r#"xmlns:r="http://schemas.openxmlformats.org/officeDocument/2006/relationships" "#
    )
    .unwrap();
    write!(
        xml,
        r#"xmlns:wp="http://schemas.openxmlformats.org/drawingml/2006/wordprocessingDrawing" "#
    )
    .unwrap();
    write!(
        xml,
        r#"xmlns:a="http://schemas.openxmlformats.org/drawingml/2006/main" "#
    )
    .unwrap();
    write!(
        xml,
        r#"xmlns:pic="http://schemas.openxmlformats.org/drawingml/2006/picture" "#
    )
    .unwrap();
    write!(
        xml,
        r#"xmlns:wpg="http://schemas.microsoft.com/office/word/2010/wordprocessingGroup" "#
    )
    .unwrap();
    write!(
        xml,
        r#"xmlns:wps="http://schemas.microsoft.com/office/word/2010/wordprocessingShape" "#
    )
    .unwrap();
    write!(
        xml,
        r#"xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006">"#
    )
    .unwrap();
    write!(xml, r#"<w:p><w:pPr><w:pStyle w:val="Header"/></w:pPr>"#).unwrap();
    write!(xml, r#"<w:r><w:rPr><w:noProof/></w:rPr>"#).unwrap();
    write!(
        xml,
        r#"<mc:AlternateContent><mc:Choice Requires="wpg"><w:drawing>"#
    )
    .unwrap();
    write!(
        xml,
        r#"<wp:anchor distT="0" distB="0" distL="0" distR="0" "#
    )
    .unwrap();
    write!(
        xml,
        r#"simplePos="0" relativeHeight="251658240" behindDoc="0" "#
    )
    .unwrap();
    write!(
        xml,
        r#"locked="0" layoutInCell="1" hidden="0" allowOverlap="1">"#
    )
    .unwrap();
    write!(xml, r#"<wp:simplePos x="0" y="0"/>"#).unwrap();
    write!(
        xml,
        r#"<wp:positionH relativeFrom="page"><wp:posOffset>0</wp:posOffset></wp:positionH>"#
    )
    .unwrap();
    write!(
        xml,
        r#"<wp:positionV relativeFrom="page"><wp:posOffset>0</wp:posOffset></wp:positionV>"#
    )
    .unwrap();
    write!(
        xml,
        r#"<wp:extent cx="{}" cy="{}"/>"#,
        opts.banner_width, opts.banner_height
    )
    .unwrap();
    write!(
        xml,
        r#"<wp:effectExtent l="0" t="0" r="0" b="0"/><wp:wrapNone/>"#
    )
    .unwrap();
    write!(
        xml,
        r#"<wp:docPr id="1" name="Header Banner"/><wp:cNvGraphicFramePr/>"#
    )
    .unwrap();
    write!(xml, r#"<a:graphic><a:graphicData uri="http://schemas.microsoft.com/office/word/2010/wordprocessingGroup">"#).unwrap();
    write!(xml, r#"<wpg:wgp><wpg:cNvGrpSpPr/><wpg:grpSpPr><a:xfrm>"#).unwrap();
    write!(
        xml,
        r#"<a:off x="0" y="0"/><a:ext cx="{w}" cy="{h}"/>"#,
        w = opts.banner_width,
        h = opts.banner_height
    )
    .unwrap();
    write!(
        xml,
        r#"<a:chOff x="0" y="0"/><a:chExt cx="{w}" cy="{h}"/>"#,
        w = opts.banner_width,
        h = opts.banner_height
    )
    .unwrap();
    write!(xml, r#"</a:xfrm></wpg:grpSpPr>"#).unwrap();
    // Background rectangle
    write!(
        xml,
        r#"<wps:wsp><wps:cNvPr id="2" name="BG"/><wps:cNvSpPr/><wps:spPr>"#
    )
    .unwrap();
    write!(
        xml,
        r#"<a:xfrm><a:off x="0" y="0"/><a:ext cx="{w}" cy="{h}"/></a:xfrm>"#,
        w = opts.banner_width,
        h = opts.banner_height
    )
    .unwrap();
    write!(xml, r#"<a:prstGeom prst="rect"><a:avLst/></a:prstGeom>"#).unwrap();
    write!(
        xml,
        r#"<a:solidFill><a:srgbClr val="{}"/></a:solidFill>"#,
        opts.bg_color
    )
    .unwrap();
    write!(
        xml,
        r#"<a:ln><a:noFill/></a:ln></wps:spPr><wps:bodyPr/></wps:wsp>"#
    )
    .unwrap();
    // Logo image
    write!(
        xml,
        r#"<pic:pic><pic:nvPicPr><pic:cNvPr id="3" name="Logo"/><pic:cNvPicPr/></pic:nvPicPr>"#
    )
    .unwrap();
    write!(xml, r#"<pic:blipFill><a:blip r:embed="{}"/><a:stretch><a:fillRect/></a:stretch></pic:blipFill>"#, image_rel_id).unwrap();
    write!(
        xml,
        r#"<pic:spPr><a:xfrm><a:off x="{}" y="{}"/><a:ext cx="{}" cy="{}"/></a:xfrm>"#,
        opts.logo_x_offset, opts.logo_y_offset, opts.logo_width, opts.logo_height
    )
    .unwrap();
    write!(xml, r#"<a:prstGeom prst="rect"><a:avLst/></a:prstGeom><a:noFill/><a:ln><a:noFill/></a:ln></pic:spPr></pic:pic>"#).unwrap();
    write!(xml, r#"</wpg:wgp></a:graphicData></a:graphic>"#).unwrap();
    write!(
        xml,
        r#"</wp:anchor></w:drawing></mc:Choice></mc:AlternateContent>"#
    )
    .unwrap();
    write!(xml, r#"</w:r></w:p></w:hdr>"#).unwrap();
    xml.into_bytes()
}