File size: 71,152 Bytes
c29c721
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
38c1b0d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
c29c721
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
38c1b0d
c29c721
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
38c1b0d
c29c721
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
38c1b0d
 
c29c721
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
38c1b0d
c29c721
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
ce8273b
 
 
 
 
 
 
 
 
c29c721
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
121dc94
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
c29c721
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
48d91be
 
0155221
48d91be
 
 
 
 
0155221
48d91be
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
0155221
48d91be
 
 
0155221
48d91be
 
0155221
 
48d91be
 
 
 
 
 
 
 
 
 
 
0155221
c29c721
48d91be
0155221
48d91be
 
 
 
 
0155221
48d91be
 
 
 
0155221
 
48d91be
 
 
0155221
 
 
48d91be
c29c721
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
121dc94
c29c721
 
 
38c1b0d
 
 
 
 
 
 
 
c29c721
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
70b4b09
c29c721
ce8273b
c29c721
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
bc656d2
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
c29c721
 
 
 
 
 
 
121dc94
c29c721
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
97fc03d
c29c721
 
 
 
 
 
97fc03d
c29c721
 
 
 
 
 
97fc03d
c29c721
 
 
 
 
 
97fc03d
c29c721
 
 
 
 
 
 
 
 
121dc94
c29c721
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
38c1b0d
c29c721
 
38c1b0d
c29c721
 
 
38c1b0d
c29c721
 
 
 
 
 
 
 
 
 
38c1b0d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
c29c721
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
38c1b0d
c29c721
38c1b0d
 
 
 
c29c721
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
97fc03d
c29c721
 
 
97fc03d
c29c721
 
 
 
 
 
 
 
 
 
 
 
 
 
 
97fc03d
c29c721
 
 
 
 
 
 
 
 
 
97fc03d
c29c721
 
 
97fc03d
c29c721
 
 
 
 
 
 
 
 
 
97fc03d
c29c721
 
 
 
 
 
 
 
 
 
 
 
 
 
 
97fc03d
c29c721
 
 
 
 
 
 
 
 
 
 
 
 
 
97fc03d
c29c721
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
38c1b0d
c29c721
 
 
 
 
 
 
38c1b0d
 
 
 
 
 
 
 
 
 
c29c721
 
 
 
 
 
 
 
 
 
 
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
"""
Policy vs Deforestation Explorer — built with gr.Server

Uses gr.Server for API endpoints (with MCP tool support) and a custom
HTML frontend with Chart.js for interactive visualization.
"""

import json
import urllib.request
from gradio import Server
from fastapi.responses import HTMLResponse, JSONResponse

app = Server()

COUNTRIES = {
    "Brazil": "BRA", "Indonesia": "IDN", "DR Congo": "COD",
    "Colombia": "COL", "Bolivia": "BOL", "Malaysia": "MYS",
    "India": "IND", "Mexico": "MEX", "Peru": "PER",
    "Australia": "AUS", "Canada": "CAN", "Russia": "RUS",
    "China": "CHN", "United States": "USA", "Nigeria": "NGA",
    "Myanmar": "MMR", "Tanzania": "TZA", "Paraguay": "PRY",
    "Madagascar": "MDG", "Cameroon": "CMR",
}

POLICY_EVENTS = {
    "Brazil": [
        {"year": 2004, "text": "PPCDAm action plan launched", "url": "https://en.wikipedia.org/wiki/Deforestation_in_Brazil"},
        {"year": 2006, "text": "Soy Moratorium signed", "url": "https://en.wikipedia.org/wiki/Amazon_Soy_Moratorium"},
        {"year": 2008, "text": "Amazon Fund established", "url": "https://en.wikipedia.org/wiki/Amazon_Fund"},
        {"year": 2012, "text": "New Forest Code enacted", "url": "https://en.wikipedia.org/wiki/Deforestation_in_Brazil"},
        {"year": 2019, "text": "Enforcement weakened under Bolsonaro", "url": "https://en.wikipedia.org/wiki/Deforestation_in_Brazil"},
        {"year": 2023, "text": "Zero deforestation pledge renewed", "url": "https://en.wikipedia.org/wiki/Deforestation_in_Brazil"},
    ],
    "Indonesia": [
        {"year": 2002, "text": "Illegal Logging Decree", "url": "https://en.wikipedia.org/wiki/Deforestation_in_Indonesia"},
        {"year": 2011, "text": "Forest moratorium on concessions", "url": "https://en.wikipedia.org/wiki/Deforestation_in_Indonesia"},
        {"year": 2014, "text": "One Map Policy", "url": "https://en.wikipedia.org/wiki/Joko_Widodo"},
        {"year": 2018, "text": "Moratorium extended permanently", "url": "https://en.wikipedia.org/wiki/Deforestation_in_Indonesia"},
        {"year": 2023, "text": "FOLU Net Sink 2030 strategy", "url": "https://en.wikipedia.org/wiki/Climate_change_in_Indonesia"},
    ],
    "DR Congo": [
        {"year": 2002, "text": "Forest Code enacted", "url": "https://en.wikipedia.org/wiki/Deforestation_in_the_Democratic_Republic_of_the_Congo"},
        {"year": 2014, "text": "REDD+ national strategy", "url": "https://en.wikipedia.org/wiki/REDD_and_REDD%2B"},
        {"year": 2022, "text": "Logging moratorium lifted", "url": "https://en.wikipedia.org/wiki/Deforestation_in_the_Democratic_Republic_of_the_Congo"},
    ],
    "Colombia": [
        {"year": 2010, "text": "REDD+ strategy launched", "url": "https://en.wikipedia.org/wiki/REDD_and_REDD%2B"},
        {"year": 2016, "text": "FARC peace deal", "url": "https://en.wikipedia.org/wiki/Colombian_peace_process"},
        {"year": 2018, "text": "Deforestation Control Council", "url": "https://en.wikipedia.org/wiki/Deforestation_in_Colombia"},
        {"year": 2023, "text": "Amazon pact signed", "url": "https://en.wikipedia.org/wiki/Amazon_Cooperation_Treaty_Organization"},
    ],
    "India": [
        {"year": 2006, "text": "Forest Rights Act", "url": "https://en.wikipedia.org/wiki/Forest_Rights_Act,_2006"},
        {"year": 2014, "text": "Green India Mission", "url": "https://en.wikipedia.org/wiki/Government_of_India"},
        {"year": 2019, "text": "Compensatory Afforestation Fund", "url": "https://en.wikipedia.org/wiki/Compensatory_Afforestation_Fund_Act,_2016"},
    ],
    "Malaysia": [
        {"year": 2010, "text": "50% forest cover pledge", "url": "https://en.wikipedia.org/wiki/Deforestation_in_Malaysia"},
        {"year": 2017, "text": "MSPO mandatory certification", "url": "https://en.wikipedia.org/wiki/Malaysian_Sustainable_Palm_Oil"},
    ],
    "China": [
        {"year": 1998, "text": "Natural Forest Protection Program", "url": "https://en.wikipedia.org/wiki/Reforestation_in_China"},
        {"year": 2003, "text": "Grain-to-Green expanded", "url": "https://en.wikipedia.org/wiki/Reforestation_in_China"},
        {"year": 2016, "text": "13th Five-Year Plan forestry targets", "url": "https://en.wikipedia.org/wiki/13th_Five-Year_Plan_(China)"},
    ],
    "Mexico": [
        {"year": 2001, "text": "ProÁrbol reforestation", "url": "https://en.wikipedia.org/wiki/CONAFOR"},
        {"year": 2012, "text": "Climate Change Law", "url": "https://en.wikipedia.org/wiki/Climate_change_in_Mexico"},
        {"year": 2020, "text": "Sembrando Vida program", "url": "https://en.wikipedia.org/wiki/Andr%C3%A9s_Manuel_L%C3%B3pez_Obrador"},
    ],
    "Canada": [
        {"year": 2010, "text": "Boreal Forest Agreement", "url": "https://en.wikipedia.org/wiki/Boreal_forest_of_Canada"},
        {"year": 2021, "text": "2 Billion Trees program", "url": "https://www.canada.ca/en/campaign/2-billion-trees.html"},
    ],
    "Australia": [
        {"year": 2000, "text": "Regional Forest Agreements", "url": "https://en.wikipedia.org/wiki/Regional_Forest_Agreement"},
        {"year": 2012, "text": "Carbon farming legislation", "url": "https://en.wikipedia.org/wiki/Climate_change_in_Australia"},
        {"year": 2022, "text": "Nature Repair Market Act", "url": "https://en.wikipedia.org/wiki/Climate_change_in_Australia"},
    ],
    "United States": [
        {"year": 2001, "text": "Healthy Forests Initiative", "url": "https://en.wikipedia.org/wiki/Healthy_Forests_Initiative"},
        {"year": 2008, "text": "Lacey Act amended to ban illegal timber", "url": "https://en.wikipedia.org/wiki/Lacey_Act_of_1900"},
        {"year": 2022, "text": "Inflation Reduction Act — $5B for forests", "url": "https://en.wikipedia.org/wiki/Inflation_Reduction_Act"},
    ],
    "Russia": [
        {"year": 1997, "text": "First Russian Forest Code", "url": "https://en.wikipedia.org/wiki/Forestry_in_Russia"},
        {"year": 2006, "text": "New Forest Code — privatisation of management", "url": "https://en.wikipedia.org/wiki/Forestry_in_Russia"},
        {"year": 2020, "text": "Forest protection reform after record fires", "url": "https://en.wikipedia.org/wiki/Forestry_in_Russia"},
    ],
    "Bolivia": [
        {"year": 1996, "text": "Forestry Law 1700 enacted", "url": "https://en.wikipedia.org/wiki/Deforestation"},
        {"year": 2012, "text": "Mother Earth Law", "url": "https://en.wikipedia.org/wiki/Law_of_the_Rights_of_Mother_Earth"},
    ],
    "Peru": [
        {"year": 2011, "text": "Forest and Wildlife Law 29763", "url": "https://en.wikipedia.org/wiki/Deforestation_in_Peru"},
        {"year": 2014, "text": "Joint Declaration with Norway & Germany", "url": "https://en.wikipedia.org/wiki/Deforestation_in_Peru"},
    ],
    "Paraguay": [
        {"year": 2004, "text": "Zero Deforestation Law (East region)", "url": "https://en.wikipedia.org/wiki/Deforestation"},
    ],
    "Madagascar": [
        {"year": 2003, "text": "Durban Vision — triple protected areas", "url": "https://en.wikipedia.org/wiki/Environment_of_Madagascar"},
        {"year": 2015, "text": "National REDD+ strategy", "url": "https://en.wikipedia.org/wiki/REDD_and_REDD%2B"},
    ],
    "Cameroon": [
        {"year": 1994, "text": "Forest Law 94/01 enacted", "url": "https://en.wikipedia.org/wiki/Forestry_in_Cameroon"},
        {"year": 2010, "text": "CAFI partnership initiated", "url": "https://en.wikipedia.org/wiki/Forestry_in_Cameroon"},
    ],
    "Nigeria": [
        {"year": 1999, "text": "Federal Forestry Policy", "url": "https://en.wikipedia.org/wiki/Forestry_in_Nigeria"},
        {"year": 2017, "text": "Great Green Wall commitment", "url": "https://en.wikipedia.org/wiki/Great_Green_Wall_(Africa)"},
    ],
    "Myanmar": [
        {"year": 1992, "text": "Forest Law passed", "url": "https://en.wikipedia.org/wiki/Deforestation"},
        {"year": 2014, "text": "Raw log export ban", "url": "https://en.wikipedia.org/wiki/Deforestation"},
    ],
    "Tanzania": [
        {"year": 2002, "text": "Forest Act enacted", "url": "https://en.wikipedia.org/wiki/Environmental_issues_in_Tanzania"},
        {"year": 2015, "text": "National REDD+ strategy", "url": "https://en.wikipedia.org/wiki/REDD_and_REDD%2B"},
    ],
}


CLIMATE_EVENTS = {
    "Australia": [
        {"year": 2002, "text": "Millennium Drought intensifies", "url": "https://en.wikipedia.org/wiki/2000s_Australian_drought"},
        {"year": 2009, "text": "Black Saturday bushfires (430k ha)", "url": "https://en.wikipedia.org/wiki/Black_Saturday_bushfires"},
        {"year": 2020, "text": "Black Summer fires (18.6M ha)", "url": "https://en.wikipedia.org/wiki/2019%E2%80%9320_Australian_bushfire_season"},
    ],
    "Indonesia": [
        {"year": 1997, "text": "Southeast Asian haze from peat fires", "url": "https://en.wikipedia.org/wiki/1997_Southeast_Asian_haze"},
        {"year": 2015, "text": "Peat fires burn 2.6M ha", "url": "https://en.wikipedia.org/wiki/2015_Southeast_Asian_haze"},
        {"year": 2019, "text": "Major fire season, 1.6M ha burned", "url": "https://en.wikipedia.org/wiki/2019_Southeast_Asian_haze"},
    ],
    "Brazil": [
        {"year": 2019, "text": "Amazon fires spike 84%", "url": "https://en.wikipedia.org/wiki/2019_Amazon_rainforest_wildfires"},
        {"year": 2020, "text": "Pantanal wetland fires (4M ha)", "url": "https://en.wikipedia.org/wiki/2020_Pantanal_wildfires"},
    ],
    "Canada": [
        {"year": 2005, "text": "Mountain pine beetle peak (18M ha)", "url": "https://en.wikipedia.org/wiki/Mountain_pine_beetle"},
        {"year": 2016, "text": "Fort McMurray fire", "url": "https://en.wikipedia.org/wiki/2016_Fort_McMurray_wildfire"},
        {"year": 2023, "text": "Record fire season (18.5M ha)", "url": "https://en.wikipedia.org/wiki/2023_Canadian_wildfires"},
    ],
    "Russia": [
        {"year": 2010, "text": "Moscow smog — wildfires kill 56k", "url": "https://en.wikipedia.org/wiki/2010_Russian_wildfires"},
        {"year": 2019, "text": "Siberian fires (4M ha)", "url": "https://en.wikipedia.org/wiki/2019_Siberia_wildfires"},
        {"year": 2021, "text": "Record Siberian fires (18.8M ha)", "url": "https://en.wikipedia.org/wiki/2021_Russia_wildfires"},
    ],
    "China": [
        {"year": 1998, "text": "Yangtze floods kill 4,000+", "url": "https://en.wikipedia.org/wiki/1998_China_floods"},
    ],
    "United States": [
        {"year": 2018, "text": "Camp Fire destroys Paradise", "url": "https://en.wikipedia.org/wiki/Camp_Fire_(2018)"},
        {"year": 2020, "text": "Record fire season (4.2M acres)", "url": "https://en.wikipedia.org/wiki/2020_California_wildfires"},
        {"year": 2023, "text": "Maui fires & Canadian smoke crisis", "url": "https://en.wikipedia.org/wiki/2023_Hawaii_wildfires"},
    ],
    "Colombia": [
        {"year": 2017, "text": "Deforestation spikes in post-FARC areas", "url": "https://en.wikipedia.org/wiki/Deforestation_in_Colombia"},
    ],
    "Mexico": [
        {"year": 2011, "text": "Worst drought in 70 years", "url": "https://en.wikipedia.org/wiki/Climate_change_in_Mexico"},
    ],
    "Bolivia": [
        {"year": 2019, "text": "Chiquitano fires burn 2.3M ha", "url": "https://en.wikipedia.org/wiki/2019_Amazon_rainforest_wildfires"},
    ],
    "Madagascar": [
        {"year": 2020, "text": "Severe drought in southern regions", "url": "https://en.wikipedia.org/wiki/Environment_of_Madagascar"},
    ],
    "DR Congo": [
        {"year": 2003, "text": "Second Congo War ends", "url": "https://en.wikipedia.org/wiki/Second_Congo_War"},
    ],
}


_cache = {}

HISTORICAL_CONTEXT = {
    "Brazil": {
        range(1990, 1996): "Rapid expansion of cattle ranching and soy farming in the Amazon after economic stabilisation.",
        range(1996, 2005): "Peak deforestation era — illegal logging, land speculation, and weak enforcement. Arc of deforestation expanded.",
        range(2005, 2013): "PPCDAm enforcement + satellite monitoring (DETER) + soy/beef moratoriums drove sharp decline in clearing rates.",
        range(2013, 2019): "Deforestation crept back up as budget cuts weakened IBAMA enforcement and new Forest Code allowed amnesty for past clearing.",
        range(2019, 2023): "Environmental enforcement dismantled, IBAMA fines dropped 70%. Amazon tipping point warnings from scientists.",
    },
    "Indonesia": {
        range(1990, 2000): "Suharto-era logging concessions and transmigration programs accelerated forest loss, especially in Sumatra and Kalimantan.",
        range(2000, 2005): "Post-Suharto decentralisation gave district heads power to issue logging/plantation permits, often corruptly.",
        range(2005, 2012): "Palm oil boom — Indonesia became world's largest producer. Peatland drainage caused massive fires (2006, 2009).",
        range(2012, 2018): "2015 fires burned 2.6M hectares, caused $16B damage. Led to peat restoration agency and stronger moratorium.",
        range(2018, 2023): "Deforestation rates declined significantly. Palm oil export ban (2022) temporarily reduced pressure.",
    },
    "Australia": {
        range(1990, 2000): "Broadscale land clearing for agriculture, especially in Queensland. Woody vegetation loss peaked mid-1990s.",
        range(2000, 2007): "Millennium drought (2001-2009) devastated forests. Queensland banned broadscale clearing in 2006.",
        range(2007, 2013): "Black Saturday bushfires (2009) destroyed 430,000 hectares. Drought continued to stress forests.",
        range(2013, 2020): "Forest recovery after drought broke. But 2019-20 Black Summer fires burned 18.6M hectares — worst fire season on record.",
        range(2020, 2023): "La Niña rains aided recovery. New environmental laws and carbon farming incentives.",
    },
    "DR Congo": {
        range(1990, 2002): "Civil wars (1996-2003) disrupted industrial logging but subsistence clearing continued.",
        range(2002, 2015): "Population growth drove smallholder agriculture expansion — the primary deforestation driver. Charcoal demand surged.",
        range(2015, 2023): "Artisanal mining and cocoa expansion increased. DRC has lowest governance capacity of major forest nations.",
    },
    "Colombia": {
        range(1990, 2016): "FARC conflict paradoxically protected forests — armed groups controlled access to remote areas.",
        range(2016, 2020): "Post-peace deal: deforestation spiked 44% as land grabbers moved into former FARC territory.",
        range(2020, 2023): "Government crackdown on deforestation. Amazon pact signed with Brazil and other nations.",
    },
    "India": {
        range(1990, 2005): "Forest cover data contested — government counts plantations as forest. Native forest loss continued.",
        range(2005, 2015): "Massive afforestation programs (Green India Mission) increased total tree cover, though primary forest still declined.",
        range(2015, 2023): "Forest Rights Act empowered tribal communities. Compensatory afforestation fund reached $6B.",
    },
    "China": {
        range(1990, 2000): "1998 Yangtze floods killed 4,000+ — blamed on upstream deforestation. Triggered logging ban.",
        range(2000, 2010): "Grain-to-Green: world's largest reforestation program. Paid 120M farmers to convert cropland to forest.",
        range(2010, 2023): "China became net reforester. But imports shifted deforestation to SE Asia and Africa.",
    },
    "Malaysia": {
        range(1990, 2005): "Rapid palm oil expansion, especially in Sabah and Sarawak. Malaysia became 2nd largest producer.",
        range(2005, 2015): "International pressure over orangutan habitat. RSPO certification introduced but adoption slow.",
        range(2015, 2023): "MSPO mandatory certification. Pledged 50% forest cover but definition includes oil palm.",
    },
    "Mexico": {
        range(1990, 2005): "NAFTA (1994) shifted agriculture — some marginal farmland abandoned and reforested, but Lacandón jungle clearing continued.",
        range(2005, 2015): "Drug cartel activity in forests (avocado, poppy cultivation) drove illegal clearing in Michoacán and Guerrero.",
        range(2015, 2023): "Sembrando Vida program controversially paid farmers to plant trees — but some cut existing forest to qualify.",
    },
    "Canada": {
        range(1990, 2005): "Forestry industry dominated — clearcut logging in British Columbia and boreal regions.",
        range(2005, 2015): "Mountain pine beetle epidemic killed 18M hectares of BC forest — largest insect blight in North American history.",
        range(2015, 2023): "Wildfires intensified with climate change. 2023 was worst fire season ever — 18.5M hectares burned.",
    },
    "Russia": {
        range(1990, 2000): "Post-Soviet collapse reduced industrial logging but also enforcement. Illegal logging surged.",
        range(2000, 2010): "Siberian wildfires increased dramatically. 2010 fires caused Moscow smog crisis.",
        range(2010, 2023): "Permafrost thaw and wildfires became primary forest loss drivers. 2021: record 18.8M hectares burned.",
    },
    "United States": {
        range(1990, 2005): "Net forest area roughly stable. Urban sprawl consumed some forest, offset by farmland reversion in East.",
        range(2005, 2015): "Western wildfires intensified — bark beetle outbreaks weakened millions of hectares. 2012 was record fire year.",
        range(2015, 2023): "Paradise fire (2018), record 2020 season (4.2M acres in CA/OR/WA). Climate-driven megafires now the norm.",
    },
}


def _fetch_wb(country_code: str, indicator: str) -> list[dict]:
    cache_key = f"{country_code}:{indicator}"
    if cache_key in _cache:
        return _cache[cache_key]
    url = (
        f"https://api.worldbank.org/v2/country/{country_code}"
        f"/indicator/{indicator}?format=json&per_page=50&date=1990:2022"
    )
    for attempt in range(3):
        try:
            resp = urllib.request.urlopen(url, timeout=30)
            data = json.loads(resp.read())
            if len(data) < 2 or not data[1]:
                _cache[cache_key] = []
                return []
            results = [
                {"year": int(d["date"]), "value": round(d["value"], 3)}
                for d in data[1]
                if d["value"] is not None
            ]
            results.sort(key=lambda x: x["year"])
            _cache[cache_key] = results
            return results
        except Exception:
            if attempt == 2:
                return []
            import time
            time.sleep(1)


@app.mcp.tool(name="get_forest_data")
@app.api(name="get_forest_data")
def get_forest_data(country: str) -> dict:
    """Get forest area (% of land) time series for a country. Returns yearly data from World Bank."""
    code = COUNTRIES.get(country)
    if not code:
        return {"error": f"Unknown country. Available: {', '.join(COUNTRIES.keys())}"}
    forest = _fetch_wb(code, "AG.LND.FRST.ZS")
    governance = _fetch_wb(code, "RL.EST")
    policies = POLICY_EVENTS.get(country, [])
    climate = CLIMATE_EVENTS.get(country, [])

    summary = {}
    if forest:
        first, last = forest[0], forest[-1]
        change = last["value"] - first["value"]
        years = last["year"] - first["year"]
        summary = {
            "start_year": first["year"],
            "end_year": last["year"],
            "start_pct": first["value"],
            "end_pct": last["value"],
            "change_pct": round(change, 3),
            "annual_rate": round(change / years, 4) if years else 0,
        }

    return {
        "country": country,
        "forest": forest,
        "governance": governance,
        "policies": policies,
        "climate": climate,
        "summary": summary,
    }


@app.mcp.tool(name="compare_countries")
@app.api(name="compare_countries")
def compare_countries(country_a: str, country_b: str) -> dict:
    """Compare forest cover trends between two countries."""
    a = get_forest_data(country_a)
    b = get_forest_data(country_b)
    return {"country_a": a, "country_b": b}


@app.mcp.tool(name="explain_spike")
@app.api(name="explain_spike")
def explain_spike(country: str, year: int) -> dict:
    """Explain what happened to forest cover around a specific year. Identifies rate changes and nearby policy events."""
    code = COUNTRIES.get(country)
    if not code:
        return {"error": f"Unknown country."}
    forest = _fetch_wb(code, "AG.LND.FRST.ZS")
    if not forest:
        return {"error": "No data available."}

    policies = POLICY_EVENTS.get(country, [])
    nearby_policies = [p for p in policies if abs(p["year"] - year) <= 3]
    climate = CLIMATE_EVENTS.get(country, [])
    nearby_climate = [c for c in climate if abs(c["year"] - year) <= 3]

    window = [f for f in forest if abs(f["year"] - year) <= 5]
    window.sort(key=lambda x: x["year"])

    point = next((f for f in forest if f["year"] == year), None)
    prev = next((f for f in forest if f["year"] == year - 1), None)
    nxt = next((f for f in forest if f["year"] == year + 1), None)

    before = [f for f in forest if year - 5 <= f["year"] < year]
    after = [f for f in forest if year < f["year"] <= year + 5]

    rate_before = None
    rate_after = None
    if len(before) >= 2:
        rate_before = round((before[-1]["value"] - before[0]["value"]) / (before[-1]["year"] - before[0]["year"]), 4)
    if len(after) >= 2:
        rate_after = round((after[-1]["value"] - after[0]["value"]) / (after[-1]["year"] - after[0]["year"]), 4)

    yoy_change = None
    if point and prev:
        yoy_change = round(point["value"] - prev["value"], 3)

    trend = "stable"
    if rate_before is not None and rate_after is not None:
        if rate_after > rate_before + 0.01:
            trend = "recovery"
        elif rate_after < rate_before - 0.01:
            trend = "acceleration"
    if yoy_change is not None:
        if yoy_change > 0.05:
            trend = "sharp increase"
        elif yoy_change < -0.05:
            trend = "sharp decline"

    context = None
    country_ctx = HISTORICAL_CONTEXT.get(country, {})
    for year_range, text in country_ctx.items():
        if year in year_range:
            context = text
            break

    return {
        "country": country,
        "year": year,
        "forest_pct": point["value"] if point else None,
        "yoy_change": yoy_change,
        "trend": trend,
        "rate_5yr_before": rate_before,
        "rate_5yr_after": rate_after,
        "nearby_policies": nearby_policies,
        "nearby_climate": nearby_climate,
        "context": context,
        "window": window,
    }


@app.mcp.tool(name="list_countries")
@app.api(name="list_countries")
def list_countries() -> list[str]:
    """List all available countries."""
    return list(COUNTRIES.keys())


@app.get("/", response_class=HTMLResponse)
async def homepage():
    countries_json = json.dumps(list(COUNTRIES.keys()))
    return f"""<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>Forest Dispatch — A Record of What We Have Lost</title>
<link rel="preconnect" href="https://fonts.googleapis.com">
<link rel="preconnect" href="https://fonts.gstatic.com" crossorigin>
<link href="https://fonts.googleapis.com/css2?family=Instrument+Serif:ital@0;1&family=JetBrains+Mono:wght@400;500;700&family=Fraunces:ital,opsz,wght@0,9..144,400;0,9..144,600;0,9..144,800;1,9..144,400&display=swap" rel="stylesheet">
<script src="https://cdn.jsdelivr.net/npm/chart.js@4"></script>
<script src="https://cdn.jsdelivr.net/npm/chartjs-plugin-annotation@3"></script>
<style>
* {{ margin: 0; padding: 0; box-sizing: border-box; }}

:root {{
    --paper: #ede5d3;
    --paper-tint: #e4dbc5;
    --paper-deep: #d8cdb3;
    --ink: #1a1f18;
    --ink-soft: #3a3f35;
    --ink-mute: #6a6d60;
    --ink-fade: #9c9e8f;
    --rule: #3a3f35;
    --rule-soft: #c5bda5;
    --moss: #4a6b3e;
    --moss-deep: #2f4827;
    --oxblood: #8b2a1f;
    --oxblood-deep: #5c1e16;
    --amber: #a8732a;
    --amber-deep: #7d5418;
    --highlight: #d4c98a;
}}

html {{ background: var(--paper); }}

body {{
    font-family: 'Fraunces', Georgia, serif;
    font-optical-sizing: auto;
    background: var(--paper);
    color: var(--ink);
    min-height: 100vh;
    font-size: 16px;
    line-height: 1.55;
    position: relative;
    overflow-x: hidden;
}}

/* Grain texture overlay — gives that risograph/newsprint feel */
body::before {{
    content: '';
    position: fixed;
    inset: 0;
    pointer-events: none;
    z-index: 1000;
    opacity: 0.35;
    mix-blend-mode: multiply;
    background-image: url("data:image/svg+xml,%3Csvg viewBox='0 0 200 200' xmlns='http://www.w3.org/2000/svg'%3E%3Cfilter id='n'%3E%3CfeTurbulence type='fractalNoise' baseFrequency='0.85' numOctaves='3' stitchTiles='stitch'/%3E%3CfeColorMatrix values='0 0 0 0 0.1 0 0 0 0 0.12 0 0 0 0 0.09 0 0 0 0.6 0'/%3E%3C/filter%3E%3Crect width='100%25' height='100%25' filter='url(%23n)'/%3E%3C/svg%3E");
}}

/* Paper vignette */
body::after {{
    content: '';
    position: fixed;
    inset: 0;
    pointer-events: none;
    z-index: 999;
    background: radial-gradient(ellipse at center, transparent 40%, rgba(60, 50, 30, 0.18) 100%);
}}

a {{ color: var(--oxblood); text-decoration: none; border-bottom: 1px solid currentColor; padding-bottom: 1px; transition: opacity 0.15s; }}
a:hover {{ opacity: 0.7; }}

.mono {{ font-family: 'JetBrains Mono', ui-monospace, monospace; font-feature-settings: "tnum", "zero"; }}
.serif-display {{ font-family: 'Instrument Serif', Georgia, serif; font-weight: 400; }}
.tabular {{ font-variant-numeric: tabular-nums; }}
.smallcaps {{
    text-transform: uppercase;
    letter-spacing: 0.18em;
    font-size: 0.72em;
    font-weight: 600;
    font-family: 'JetBrains Mono', ui-monospace, monospace;
}}

/* ====== MASTHEAD ====== */
.masthead {{
    padding: 16px 48px 0;
    border-bottom: 2px solid var(--ink);
    position: relative;
    z-index: 2;
}}
.masthead-top {{
    display: flex;
    justify-content: space-between;
    align-items: baseline;
    font-family: 'JetBrains Mono', monospace;
    font-size: 10px;
    letter-spacing: 0.15em;
    text-transform: uppercase;
    color: var(--ink-soft);
    padding-bottom: 6px;
    border-bottom: 1px solid var(--rule-soft);
}}
.masthead-top .vol {{ display: flex; gap: 22px; }}
.masthead-top .vol span:not(:last-child)::after {{
    content: '·';
    margin-left: 22px;
    color: var(--ink-fade);
}}
.masthead-row {{
    display: flex;
    align-items: center;
    justify-content: space-between;
    gap: 32px;
    padding: 12px 0;
}}
.wordmark {{
    font-family: 'Instrument Serif', serif;
    font-size: clamp(40px, 5.5vw, 72px);
    line-height: 0.95;
    letter-spacing: -0.02em;
    font-weight: 400;
    font-style: italic;
    flex-shrink: 0;
}}
.wordmark .dispatch {{ font-style: normal; }}
.dek-text {{
    flex: 1;
    max-width: 520px;
    font-size: 13px;
    line-height: 1.5;
    color: var(--ink-soft);
    font-style: italic;
}}
.dek-text::first-letter {{
    font-family: 'Instrument Serif', serif;
    font-size: 3.4em;
    line-height: 0.8;
    float: left;
    padding: 4px 8px 0 0;
    color: var(--oxblood);
    font-style: normal;
}}
.dek-meta {{
    text-align: right;
    font-family: 'JetBrains Mono', monospace;
    font-size: 9px;
    letter-spacing: 0.15em;
    text-transform: uppercase;
    color: var(--ink-mute);
    line-height: 1.7;
    white-space: nowrap;
    flex-shrink: 0;
}}
.dek-meta .badge {{
    display: inline-block;
    background: var(--ink);
    color: var(--paper);
    padding: 2px 7px;
    margin-top: 3px;
    font-weight: 500;
}}

/* ====== MAIN GRID ====== */
.sheet {{
    max-width: 1400px;
    margin: 0 auto;
    padding: 0 48px 48px;
    position: relative;
    z-index: 2;
}}

/* ====== CONTROL STRIP ====== */
.control-strip {{
    display: grid;
    grid-template-columns: 1fr auto auto auto;
    gap: 24px;
    align-items: end;
    padding: 14px 0 16px;
    border-bottom: 1px solid var(--rule);
    margin-bottom: 18px;
}}
.section-title {{
    font-family: 'Instrument Serif', serif;
    font-size: 18px;
    letter-spacing: -0.01em;
    line-height: 1.2;
    font-style: italic;
    color: var(--ink-soft);
    padding-bottom: 4px;
}}
.section-title .caps {{
    display: block;
    font-family: 'JetBrains Mono', monospace;
    font-size: 9px;
    letter-spacing: 0.2em;
    color: var(--ink-mute);
    text-transform: uppercase;
    margin-bottom: 4px;
    font-weight: 500;
    font-style: normal;
}}
.control-group {{
    display: flex;
    flex-direction: column;
    gap: 4px;
}}
.control-group label {{
    font-family: 'JetBrains Mono', monospace;
    font-size: 9px;
    letter-spacing: 0.2em;
    text-transform: uppercase;
    color: var(--ink-mute);
    font-weight: 500;
}}
select {{
    background: transparent;
    border: none;
    border-bottom: 1px solid var(--ink);
    color: var(--ink);
    padding: 4px 24px 4px 0;
    font-family: 'Fraunces', serif;
    font-size: 18px;
    font-weight: 500;
    min-width: 180px;
    cursor: pointer;
    outline: none;
    appearance: none;
    background-image: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' width='10' height='10' viewBox='0 0 10 10'%3E%3Cpath d='M1 3 L5 7 L9 3' stroke='%231a1f18' stroke-width='1.5' fill='none'/%3E%3C/svg%3E");
    background-repeat: no-repeat;
    background-position: right 4px center;
    transition: border-color 0.15s;
}}
select:focus {{ border-bottom-color: var(--oxblood); }}
button.action {{
    background: var(--ink);
    color: var(--paper);
    border: none;
    padding: 12px 28px;
    font-family: 'JetBrains Mono', monospace;
    font-size: 11px;
    letter-spacing: 0.2em;
    text-transform: uppercase;
    font-weight: 500;
    cursor: pointer;
    transition: all 0.15s;
    position: relative;
    box-shadow: 3px 3px 0 var(--rule-soft);
}}
button.action:hover {{
    transform: translate(-1px, -1px);
    box-shadow: 4px 4px 0 var(--oxblood);
}}
button.action:active {{
    transform: translate(2px, 2px);
    box-shadow: 1px 1px 0 var(--rule-soft);
}}

/* ====== FINDINGS ROW ====== */
.findings {{
    display: grid;
    grid-template-columns: repeat(4, 1fr);
    border-top: 2px solid var(--ink);
    border-bottom: 2px solid var(--ink);
    margin-bottom: 32px;
    min-height: 110px;
}}
.finding {{
    padding: 14px 22px 16px;
    border-right: 1px solid var(--rule);
    position: relative;
    display: flex;
    flex-direction: column;
    justify-content: space-between;
}}
.finding:last-child {{ border-right: none; }}
.finding::before {{
    content: attr(data-num);
    position: absolute;
    top: 8px;
    right: 10px;
    font-family: 'JetBrains Mono', monospace;
    font-size: 9px;
    color: var(--ink-fade);
    letter-spacing: 0.15em;
}}
.finding .label {{
    font-family: 'JetBrains Mono', monospace;
    font-size: 9px;
    letter-spacing: 0.22em;
    text-transform: uppercase;
    color: var(--ink-mute);
    margin-bottom: 6px;
    font-weight: 500;
}}
.finding .value {{
    font-family: 'Instrument Serif', serif;
    font-size: 40px;
    line-height: 1;
    letter-spacing: -0.02em;
    color: var(--ink);
    font-variant-numeric: tabular-nums;
    font-weight: 400;
}}
.finding .value.down {{ color: var(--oxblood); font-style: italic; }}
.finding .value.up {{ color: var(--moss-deep); }}
.finding .sub {{
    font-family: 'JetBrains Mono', monospace;
    font-size: 10px;
    color: var(--ink-mute);
    margin-top: 8px;
    letter-spacing: 0.05em;
}}
.finding .unit {{
    font-family: 'Fraunces', serif;
    font-size: 14px;
    font-style: italic;
    color: var(--ink-soft);
    margin-left: 2px;
    font-weight: 400;
}}

/* ====== CHART FIGURE ====== */
.figure {{
    margin-bottom: 28px;
}}
.figure-head {{
    display: flex;
    justify-content: space-between;
    align-items: baseline;
    padding-bottom: 14px;
    border-bottom: 1px solid var(--rule);
    margin-bottom: 2px;
}}
.figure-title {{
    font-family: 'Instrument Serif', serif;
    font-size: 26px;
    letter-spacing: -0.01em;
    font-style: italic;
}}
.figure-title::before {{
    content: 'Fig. 1 ';
    font-family: 'JetBrains Mono', monospace;
    font-style: normal;
    font-size: 12px;
    letter-spacing: 0.15em;
    color: var(--ink-mute);
    vertical-align: middle;
    margin-right: 10px;
    padding-right: 10px;
    border-right: 1px solid var(--rule);
}}
.figure-note {{
    font-family: 'JetBrains Mono', monospace;
    font-size: 10px;
    letter-spacing: 0.1em;
    color: var(--ink-mute);
    text-transform: uppercase;
}}
.figure-frame {{
    background: var(--paper-tint);
    border: 1px solid var(--rule);
    border-top: none;
    padding: 20px;
    height: clamp(420px, 56vh, 580px);
    position: relative;
}}
.figure-loading {{
    position: absolute;
    inset: 0;
    display: flex;
    align-items: center;
    justify-content: center;
    font-family: 'Instrument Serif', serif;
    font-style: italic;
    font-size: 22px;
    color: var(--ink-mute);
    background: var(--paper-tint);
    z-index: 5;
}}
.figure-loading::after {{
    content: ' ·';
    animation: ellipsis 1.4s infinite;
}}
.figure-caption {{
    padding-top: 10px;
    font-size: 13px;
    font-style: italic;
    color: var(--ink-soft);
    line-height: 1.5;
    max-width: 700px;
}}

/* ====== INSIGHT / DOSSIER ====== */
.dossier {{
    display: none;
    background: var(--paper-tint);
    border-top: 1px solid var(--ink);
    border-bottom: 1px solid var(--ink);
    padding: 28px 32px;
    margin-bottom: 40px;
    position: relative;
}}
.dossier::before {{
    content: '';
    position: absolute;
    top: 12px;
    left: 12px;
    right: 12px;
    bottom: 12px;
    border: 1px dashed var(--rule);
    pointer-events: none;
}}
.dossier-label {{
    position: absolute;
    top: -9px;
    left: 32px;
    background: var(--paper);
    padding: 0 12px;
    font-family: 'JetBrains Mono', monospace;
    font-size: 10px;
    letter-spacing: 0.2em;
    text-transform: uppercase;
    color: var(--oxblood);
    font-weight: 700;
}}
.dossier-head {{
    font-family: 'Instrument Serif', serif;
    font-size: 22px;
    font-style: italic;
    margin-bottom: 16px;
    color: var(--ink);
}}
.dossier-head .hint {{
    font-family: 'JetBrains Mono', monospace;
    font-size: 10px;
    font-style: normal;
    letter-spacing: 0.15em;
    text-transform: uppercase;
    color: var(--ink-mute);
    margin-left: 10px;
    font-weight: 500;
}}

/* ====== PANELS ====== */
.split {{
    display: grid;
    grid-template-columns: 1fr 1fr;
    gap: 0;
    border-top: 1px solid var(--rule);
    border-bottom: 1px solid var(--rule);
    display: none;
}}
.split.active {{ display: grid; }}
.panel {{
    padding: 28px 28px 24px;
    border-right: 1px solid var(--rule);
}}
.panel:last-child {{ border-right: none; }}
.panel-head {{
    display: flex;
    justify-content: space-between;
    align-items: baseline;
    margin-bottom: 18px;
    padding-bottom: 12px;
    border-bottom: 1px solid var(--rule-soft);
}}
.panel-title {{
    font-family: 'Instrument Serif', serif;
    font-size: 22px;
    font-style: italic;
    letter-spacing: -0.01em;
}}
.panel-count {{
    font-family: 'JetBrains Mono', monospace;
    font-size: 10px;
    color: var(--ink-mute);
    letter-spacing: 0.15em;
    text-transform: uppercase;
}}

/* ====== POLICY ITEMS ====== */
.policy {{
    display: grid;
    grid-template-columns: 72px 1fr auto;
    gap: 20px;
    padding: 14px 0;
    border-bottom: 1px solid var(--rule-soft);
    align-items: baseline;
}}
.policy:last-child {{ border-bottom: none; }}
.policy-year {{
    font-family: 'Instrument Serif', serif;
    font-size: 28px;
    font-style: italic;
    line-height: 1;
    color: var(--oxblood);
    font-variant-numeric: tabular-nums;
}}
.policy-text {{
    font-size: 15px;
    line-height: 1.4;
    color: var(--ink);
}}
.policy-text a {{
    color: var(--ink-mute);
    margin-left: 6px;
    font-size: 11px;
    border-bottom: none;
}}
.policy-text a:hover {{ color: var(--oxblood); }}
.policy-metric {{
    font-family: 'JetBrains Mono', monospace;
    font-size: 11px;
    color: var(--ink-mute);
    white-space: nowrap;
    letter-spacing: 0.05em;
}}

/* ====== RATE VERDICT ====== */
.rate-row {{
    display: grid;
    grid-template-columns: 60px 1fr auto;
    gap: 20px;
    padding: 14px 0;
    border-bottom: 1px solid var(--rule-soft);
    align-items: baseline;
}}
.rate-row .stage {{
    font-family: 'JetBrains Mono', monospace;
    font-size: 10px;
    letter-spacing: 0.2em;
    text-transform: uppercase;
    color: var(--oxblood);
    font-weight: 700;
}}
.rate-row .desc {{ font-size: 14px; color: var(--ink); font-style: italic; }}
.rate-row .rate {{
    font-family: 'JetBrains Mono', monospace;
    font-size: 14px;
    font-weight: 500;
    font-variant-numeric: tabular-nums;
    white-space: nowrap;
}}
.rate-row .rate.neg {{ color: var(--oxblood); }}
.rate-row .rate.pos {{ color: var(--moss-deep); }}

.verdict {{
    margin-top: 24px;
    padding: 26px 20px;
    background: var(--paper);
    border: 1px solid var(--rule);
    text-align: center;
    position: relative;
}}
.verdict::before, .verdict::after {{
    content: '§';
    font-family: 'Instrument Serif', serif;
    font-style: italic;
    font-size: 18px;
    color: var(--rule-soft);
    position: absolute;
    top: 50%;
    transform: translateY(-50%);
}}
.verdict::before {{ left: 14px; }}
.verdict::after {{ right: 14px; }}
.verdict .headline {{
    font-family: 'Instrument Serif', serif;
    font-size: 30px;
    font-style: italic;
    line-height: 1.1;
    letter-spacing: -0.01em;
}}
.verdict .headline.good {{ color: var(--moss-deep); }}
.verdict .headline.bad {{ color: var(--oxblood); }}
.verdict .sub {{
    font-family: 'JetBrains Mono', monospace;
    font-size: 10px;
    letter-spacing: 0.18em;
    text-transform: uppercase;
    color: var(--ink-mute);
    margin-top: 8px;
    font-weight: 500;
}}

/* ====== DOSSIER CONTENT ====== */
.dossier-stats {{
    display: grid;
    grid-template-columns: repeat(4, 1fr);
    gap: 32px;
    margin-top: 16px;
    padding-bottom: 20px;
    border-bottom: 1px solid var(--rule-soft);
}}
.dossier-stat .k {{
    font-family: 'JetBrains Mono', monospace;
    font-size: 9px;
    letter-spacing: 0.2em;
    text-transform: uppercase;
    color: var(--ink-mute);
    margin-bottom: 4px;
    font-weight: 500;
}}
.dossier-stat .v {{
    font-family: 'Instrument Serif', serif;
    font-size: 36px;
    line-height: 1;
    font-variant-numeric: tabular-nums;
    letter-spacing: -0.01em;
    font-style: italic;
}}
.dossier-stat .v.neg {{ color: var(--oxblood); }}
.dossier-stat .v.pos {{ color: var(--moss-deep); }}
.dossier-stat .v.alert {{ color: var(--amber-deep); }}

.rate-strip {{
    display: flex;
    gap: 48px;
    padding: 18px 0;
    border-bottom: 1px solid var(--rule-soft);
    font-size: 14px;
}}
.rate-strip .rate-item {{
    display: flex;
    align-items: baseline;
    gap: 10px;
}}
.rate-strip .k {{
    font-family: 'JetBrains Mono', monospace;
    font-size: 10px;
    letter-spacing: 0.15em;
    text-transform: uppercase;
    color: var(--ink-mute);
}}
.rate-strip .v {{
    font-family: 'JetBrains Mono', monospace;
    font-weight: 700;
    font-variant-numeric: tabular-nums;
}}
.rate-strip .v.neg {{ color: var(--oxblood); }}
.rate-strip .v.pos {{ color: var(--moss-deep); }}

.context-block {{
    margin-top: 18px;
    padding: 20px 22px;
    background: var(--paper);
    border-left: 3px solid var(--oxblood);
    position: relative;
}}
.context-block .k {{
    font-family: 'JetBrains Mono', monospace;
    font-size: 9px;
    letter-spacing: 0.25em;
    text-transform: uppercase;
    color: var(--oxblood);
    font-weight: 700;
    margin-bottom: 8px;
}}
.context-block .v {{
    font-family: 'Fraunces', serif;
    font-size: 16px;
    line-height: 1.55;
    color: var(--ink);
    font-style: italic;
}}

.nearby-block {{
    margin-top: 18px;
    padding-top: 16px;
    border-top: 1px solid var(--rule-soft);
}}
.nearby-block .k {{
    font-family: 'JetBrains Mono', monospace;
    font-size: 9px;
    letter-spacing: 0.22em;
    text-transform: uppercase;
    color: var(--ink-mute);
    margin-bottom: 10px;
    font-weight: 600;
}}
.nearby-item {{
    display: flex;
    gap: 14px;
    padding: 6px 0;
    align-items: baseline;
    font-size: 14px;
}}
.nearby-item .yr {{
    font-family: 'Instrument Serif', serif;
    font-style: italic;
    color: var(--oxblood);
    font-weight: 400;
    min-width: 56px;
}}

/* ====== LOADING ====== */
.loading {{
    grid-column: 1 / -1;
    padding: 60px 20px;
    text-align: center;
    font-family: 'Instrument Serif', serif;
    font-style: italic;
    font-size: 20px;
    color: var(--ink-mute);
}}
.loading::after {{
    content: ' ·';
    animation: ellipsis 1.4s infinite;
}}
@keyframes ellipsis {{
    0% {{ content: ' ·'; }}
    33% {{ content: ' · ·'; }}
    66% {{ content: ' · · ·'; }}
}}

/* ====== COLOPHON ====== */
.colophon {{
    max-width: 1400px;
    margin: 0 auto;
    padding: 40px 48px 60px;
    border-top: 2px solid var(--ink);
    position: relative;
    z-index: 2;
}}
.colophon-top {{
    display: grid;
    grid-template-columns: 2fr 1fr 1fr;
    gap: 48px;
    padding-bottom: 24px;
    border-bottom: 1px solid var(--rule);
    margin-bottom: 18px;
}}
.colophon-section .title {{
    font-family: 'JetBrains Mono', monospace;
    font-size: 10px;
    letter-spacing: 0.22em;
    text-transform: uppercase;
    color: var(--ink-mute);
    margin-bottom: 10px;
    font-weight: 600;
}}
.colophon-section .body {{
    font-size: 14px;
    line-height: 1.55;
    color: var(--ink-soft);
    font-style: italic;
}}
.colophon-imprint {{
    font-family: 'Instrument Serif', serif;
    font-size: 64px;
    line-height: 0.9;
    font-style: italic;
    color: var(--ink);
    letter-spacing: -0.02em;
    margin-bottom: 8px;
}}
.colophon-mark {{
    display: flex;
    justify-content: space-between;
    align-items: center;
    font-family: 'JetBrains Mono', monospace;
    font-size: 10px;
    letter-spacing: 0.18em;
    text-transform: uppercase;
    color: var(--ink-mute);
}}

@media (max-width: 900px) {{
    .masthead, .sheet, .colophon {{ padding-left: 18px; padding-right: 18px; }}
    .masthead {{ padding-top: 10px; }}
    .masthead-row {{
        display: grid;
        grid-template-columns: 1fr auto;
        gap: 10px 14px;
        padding: 8px 0 10px;
        align-items: center;
    }}
    .wordmark {{ font-size: 28px; line-height: 1; grid-column: 1; grid-row: 1; margin: 0; }}
    .dek-text {{
        display: none;
    }}
    .dek-meta {{
        grid-column: 2; grid-row: 1;
        text-align: right;
        font-size: 8px;
        line-height: 1.4;
        white-space: nowrap;
    }}
    .dek-meta .badge {{ padding: 1px 5px; margin-top: 2px; font-size: 8px; }}
    .masthead-top {{ flex-wrap: wrap; gap: 6px; font-size: 8px; padding-bottom: 4px; letter-spacing: 0.12em; }}
    .masthead-top .vol {{ gap: 10px; }}
    .masthead-top .vol span:not(:last-child)::after {{ margin-left: 10px; }}

    .control-strip {{
        grid-template-columns: 1fr 1fr;
        gap: 10px 12px;
        padding: 10px 0 12px;
        margin-bottom: 12px;
    }}
    .section-title {{ display: none; }}
    .control-group {{ min-width: 0; }}
    .control-group label {{ font-size: 8px; }}
    select {{
        width: 100%;
        min-width: 0;
        font-size: 14px;
        padding: 4px 20px 4px 0;
    }}
    button.action {{
        grid-column: 1 / -1;
        width: 100%;
        padding: 11px;
        font-size: 10px;
    }}

    .figure {{ margin-bottom: 20px; }}
    .figure-head {{ flex-direction: column; align-items: flex-start; gap: 4px; padding-bottom: 8px; margin-bottom: 0; }}
    .figure-title {{ font-size: 15px; }}
    .figure-title::before {{ display: none; }}
    .figure-note {{ font-size: 8px; }}
    .figure-frame {{ height: clamp(280px, 48vh, 380px); padding: 10px; }}
    .figure-caption {{ font-size: 11px; padding-top: 8px; }}

    .findings {{ grid-template-columns: 1fr 1fr; margin-bottom: 22px; }}
    .finding {{ border-right: 1px solid var(--rule); border-bottom: 1px solid var(--rule); padding: 10px 12px; min-height: auto; }}
    .finding:nth-child(even) {{ border-right: none; }}
    .finding:nth-last-child(-n+2) {{ border-bottom: none; }}
    .finding .label {{ font-size: 8px; }}
    .finding .value {{ font-size: 22px; }}
    .finding .unit {{ font-size: 10px; }}
    .finding .sub {{ font-size: 8px; margin-top: 4px; }}

    .dossier {{ padding: 20px 14px; }}
    .dossier-label {{ left: 14px; }}
    .dossier-head {{ font-size: 17px; }}
    .dossier-stats {{ grid-template-columns: 1fr 1fr; gap: 14px; }}
    .dossier-stat .v {{ font-size: 22px; }}
    .rate-strip {{ flex-direction: column; gap: 8px; }}

    .split.active {{ grid-template-columns: 1fr; }}
    .panel {{ border-right: none; border-bottom: 1px solid var(--rule); padding: 18px 14px; }}
    .panel:last-child {{ border-bottom: none; }}
    .panel-title {{ font-size: 17px; }}
    .policy {{ grid-template-columns: 48px 1fr; gap: 10px; padding: 10px 0; }}
    .policy-year {{ font-size: 22px; }}
    .policy-text {{ font-size: 14px; }}
    .policy-metric {{ grid-column: 2; color: var(--ink-fade); font-size: 10px; }}

    .colophon {{ padding: 26px 18px 40px; }}
    .colophon-top {{ grid-template-columns: 1fr; gap: 20px; padding-bottom: 18px; }}
    .colophon-imprint {{ font-size: 36px; }}
    .colophon-mark {{ flex-direction: column; gap: 6px; align-items: flex-start; font-size: 8px; }}
}}

@media (max-width: 420px) {{
    .wordmark {{ font-size: 24px; }}
    .dek-meta {{ font-size: 7px; }}
    .findings {{ grid-template-columns: 1fr; }}
    .finding {{ border-right: none !important; }}
    .finding:last-child {{ border-bottom: none; }}
    .dossier-stats {{ grid-template-columns: 1fr 1fr; gap: 10px; }}
}}
</style>
</head>
<body>

<!-- ====== MASTHEAD ====== -->
<header class="masthead">
    <div class="masthead-top">
        <div class="vol">
            <span>Vol. I</span>
            <span>No. 001</span>
            <span>Standing at 58.1% Global Tree Cover</span>
        </div>
        <div>Filed via World Bank Open Data</div>
    </div>
    <div class="masthead-row">
        <h1 class="wordmark"><em>The</em> <span class="dispatch">Forest Dispatch</span></h1>
        <div class="dek-text">
            A record of what has been lost, and what — through the stroke of a pen or the
            press of a moratorium — may yet be saved.
        </div>
        <div class="dek-meta">
            An investigation<br>
            powered by Gradio<br>
            <span class="badge">MCP-enabled</span>
        </div>
    </div>
</header>

<main class="sheet">

    <!-- ====== CONTROL STRIP ====== -->
    <section class="control-strip">
        <div class="section-title">
            <span class="caps">§ Selection</span>
            Choose your <em>subject</em> &amp; <em>comparator</em>
        </div>
        <div class="control-group">
            <label>Subject</label>
            <select id="country"></select>
        </div>
        <div class="control-group">
            <label>Comparator</label>
            <select id="compare"><option value="">— none —</option></select>
        </div>
        <button class="action" onclick="loadData()">Investigate →</button>
    </section>

    <!-- ====== FIGURE (lead) ====== -->
    <section class="figure">
        <div class="figure-head">
            <div class="figure-title">Forest cover over time, <em>with policy annotations</em></div>
            <div class="figure-note">click · any · year · for · dossier</div>
        </div>
        <div class="figure-frame">
            <div id="chart-loading" class="figure-loading" style="display:none">Gathering field notes</div>
            <canvas id="chart"></canvas>
        </div>
        <div class="figure-caption">
            <span class="smallcaps">Legend —</span>
            <span style="display:inline-block;width:9px;height:9px;border-radius:50%;background:var(--oxblood);vertical-align:middle;margin-right:4px"></span> policy event
            &nbsp;·&nbsp;
            <span style="display:inline-block;width:0;height:0;border-left:6px solid transparent;border-right:6px solid transparent;border-bottom:10px solid var(--amber);vertical-align:middle;margin-right:4px"></span> climate / disaster event
            &nbsp;·&nbsp;
            <span style="display:inline-block;width:12px;height:2px;background:var(--amber);vertical-align:middle;margin-right:4px"></span> rule of law
            <br>
            Hover the line to read an event. Click any year for a full dossier.
        </div>
    </section>

    <!-- ====== FINDINGS (after the chart) ====== -->
    <div id="findings-wrap" style="position:relative">
        <div id="findings" class="findings"></div>
    </div>

    <!-- ====== DOSSIER ====== -->
    <section id="dossier" class="dossier">
        <div class="dossier-label">Dossier</div>
        <div class="dossier-head" id="dossier-head"></div>
        <div id="dossier-content"></div>
    </section>

    <!-- ====== POLICY &amp; VERDICT ====== -->
    <div id="split" class="split">
        <div class="panel" id="policies"></div>
        <div class="panel" id="verdict-panel"></div>
    </div>

</main>

<!-- ====== COLOPHON ====== -->
<footer class="colophon">
    <div class="colophon-top">
        <div class="colophon-section">
            <div class="colophon-imprint">Notes.</div>
            <div class="body">
                <span class="smallcaps">On method —</span> Every figure herein is drawn from the public record. Where the policy
                register falls silent, we supply historical context in earnest italics —
                droughts, fires, wars, booms &amp; busts — to explain the line.
            </div>
        </div>
        <div class="colophon-section">
            <div class="title">Sources</div>
            <div class="body">
                <a href="https://data.worldbank.org/">World Bank Open Data</a> ·
                Forest area <span class="mono">(AG.LND.FRST.ZS)</span> ·
                Rule of Law <span class="mono">(RL.EST)</span>
            </div>
        </div>
        <div class="colophon-section">
            <div class="title">Built upon</div>
            <div class="body">
                <a href="https://gradio.app">Gradio Server</a> — API endpoints
                available at <span class="mono">/gradio_api/</span>,
                also exposed as MCP tools for agents.
            </div>
        </div>
    </div>
    <div class="colophon-mark">
        <span>© The Forest Dispatch · A record of loss &amp; recovery</span>
        <span>Set in Instrument Serif · Fraunces · JetBrains Mono</span>
    </div>
</footer>

<script>
const countries = {countries_json};
const countrySelect = document.getElementById('country');
const compareSelect = document.getElementById('compare');
let chart = null;
let currentData = null;

countries.forEach(c => {{
    countrySelect.add(new Option(c, c));
    compareSelect.add(new Option(c, c));
}});
countrySelect.value = 'Brazil';

async function callApi(name, params) {{
    const callResp = await fetch('/gradio_api/call/' + name, {{
        method: 'POST',
        headers: {{'Content-Type': 'application/json'}},
        body: JSON.stringify({{data: Object.values(params)}})
    }});
    const {{event_id}} = await callResp.json();
    if (!event_id) return {{error: 'No event id returned'}};

    const stream = await fetch('/gradio_api/call/' + name + '/' + event_id);
    const reader = stream.body.getReader();
    const decoder = new TextDecoder();
    let buffer = '';
    while (true) {{
        const {{done, value}} = await reader.read();
        if (done) break;
        buffer += decoder.decode(value, {{stream: true}});
        const lines = buffer.split('\\n');
        buffer = lines.pop();
        for (const line of lines) {{
            if (line.startsWith('event: complete')) continue;
            if (line.startsWith('data: ')) {{
                try {{
                    const parsed = JSON.parse(line.slice(6));
                    return Array.isArray(parsed) ? parsed[0] : parsed;
                }} catch (e) {{}}
            }}
        }}
    }}
    return {{error: 'No data received'}};
}}

async function loadData() {{
    const country = countrySelect.value;
    const compare = compareSelect.value;

    document.getElementById('findings').innerHTML = '<div class="loading">Gathering field notes</div>';
    document.getElementById('chart-loading').style.display = 'flex';
    document.getElementById('split').classList.remove('active');
    document.getElementById('dossier').style.display = 'none';
    if (chart) {{ chart.destroy(); chart = null; }}

    let data, compareData = null;
    if (compare && compare !== country) {{
        const result = await callApi('compare_countries', {{country_a: country, country_b: compare}});
        data = result.country_a;
        compareData = result.country_b;
    }} else {{
        data = await callApi('get_forest_data', {{country}});
    }}

    if (data.error) {{
        document.getElementById('findings').innerHTML = `<div class="loading">${{data.error}}</div>`;
        return;
    }}

    currentData = data;
    renderFindings(data);
    renderChart(data, compareData);
    renderPolicies(data);
    renderVerdict(data);
    document.getElementById('split').classList.add('active');
}}

function renderFindings(data) {{
    const s = data.summary;
    if (!s || !s.start_year) {{
        document.getElementById('findings').innerHTML = '<div class="loading">No data available for this subject</div>';
        return;
    }}
    const dirClass = s.change_pct < 0 ? 'down' : 'up';
    const sign = s.change_pct < 0 ? '−' : '+';
    const rateSign = s.annual_rate < 0 ? '−' : '+';

    document.getElementById('findings').innerHTML = `
        <div class="finding" data-num="i">
            <div>
                <div class="label">${{data.country}} · cover in ${{s.start_year}}</div>
                <div class="value">${{s.start_pct.toFixed(1)}}<span class="unit">%</span></div>
            </div>
            <div class="sub">of national land area</div>
        </div>
        <div class="finding" data-num="ii">
            <div>
                <div class="label">${{data.country}} · cover in ${{s.end_year}}</div>
                <div class="value">${{s.end_pct.toFixed(1)}}<span class="unit">%</span></div>
            </div>
            <div class="sub">of national land area</div>
        </div>
        <div class="finding" data-num="iii">
            <div>
                <div class="label">${{data.country}} · net change</div>
                <div class="value ${{dirClass}}">${{sign}}${{Math.abs(s.change_pct).toFixed(2)}}<span class="unit">pp</span></div>
            </div>
            <div class="sub">across ${{s.end_year - s.start_year}} years observed</div>
        </div>
        <div class="finding" data-num="iv">
            <div>
                <div class="label">${{data.country}} · annual rate</div>
                <div class="value ${{dirClass}}">${{rateSign}}${{Math.abs(s.annual_rate).toFixed(3)}}<span class="unit">%/yr</span></div>
            </div>
            <div class="sub">${{data.policies.length}} policies on record</div>
        </div>
    `;
}}

function renderChart(data, compareData) {{
    if (chart) chart.destroy();
    document.getElementById('chart-loading').style.display = 'none';

    const ctx = document.getElementById('chart').getContext('2d');
    const INK = '#1a1f18';
    const OXBLOOD = '#8b2a1f';
    const MOSS = '#4a6b3e';
    const AMBER = '#a8732a';
    const RULE = '#c5bda5';
    const PAPER_TINT = '#e4dbc5';

    const datasets = [{{
        label: data.country,
        data: data.forest.map(f => ({{x: f.year, y: f.value}})),
        borderColor: MOSS,
        backgroundColor: 'rgba(74, 107, 62, 0.12)',
        fill: true,
        tension: 0.25,
        pointRadius: 2.5,
        pointBackgroundColor: MOSS,
        pointBorderColor: PAPER_TINT,
        pointBorderWidth: 1,
        pointHoverRadius: 6,
        borderWidth: 2.2,
    }}];

    if (data.governance && data.governance.length) {{
        datasets.push({{
            label: 'Rule of Law',
            data: data.governance.map(g => ({{x: g.year, y: g.value}})),
            borderColor: AMBER,
            borderDash: [3, 3],
            borderWidth: 1.2,
            pointRadius: 0,
            tension: 0.3,
            yAxisID: 'y2',
        }});
    }}

    if (compareData && compareData.forest) {{
        datasets.push({{
            label: compareData.country,
            data: compareData.forest.map(f => ({{x: f.year, y: f.value}})),
            borderColor: OXBLOOD,
            borderDash: [6, 3],
            borderWidth: 1.8,
            pointRadius: 1.5,
            pointBackgroundColor: OXBLOOD,
            tension: 0.25,
        }});
    }}

    const annotations = {{}};
    (data.policies || []).forEach((p, i) => {{
        annotations['pline' + i] = {{
            type: 'line',
            xMin: p.year, xMax: p.year,
            borderColor: 'rgba(139, 42, 31, 0.22)',
            borderWidth: 1,
            borderDash: [3, 4],
        }};
        annotations['ppoint' + i] = {{
            type: 'point',
            xValue: p.year,
            yValue: data.forest.find(f => f.year === p.year)?.value || data.forest[0].value,
            radius: 5,
            backgroundColor: OXBLOOD,
            borderColor: PAPER_TINT,
            borderWidth: 2,
            hoverRadius: 8,
        }};
    }});
    (data.climate || []).forEach((c, i) => {{
        annotations['cline' + i] = {{
            type: 'line',
            xMin: c.year, xMax: c.year,
            borderColor: 'rgba(168, 115, 42, 0.22)',
            borderWidth: 1,
            borderDash: [1, 3],
        }};
        annotations['cpoint' + i] = {{
            type: 'point',
            xValue: c.year,
            yValue: data.forest.find(f => f.year === c.year)?.value || data.forest[0].value,
            radius: 6,
            pointStyle: 'triangle',
            backgroundColor: AMBER,
            borderColor: PAPER_TINT,
            borderWidth: 2,
            hoverRadius: 9,
        }};
    }});

    chart = new Chart(ctx, {{
        type: 'line',
        data: {{ datasets }},
        options: {{
            responsive: true,
            maintainAspectRatio: false,
            interaction: {{ mode: 'index', intersect: false }},
            onClick: function(evt, elements) {{
                if (!elements.length) return;
                const el = elements[0];
                const ds = chart.data.datasets[el.datasetIndex];
                if (!ds.data[el.index]) return;
                const year = Math.round(ds.data[el.index].x);
                handlePointClick(year);
            }},
            plugins: {{
                legend: {{
                    labels: {{
                        color: INK,
                        font: {{ family: "'JetBrains Mono', monospace", size: 10, weight: 500 }},
                        usePointStyle: true,
                        pointStyle: 'rectRot',
                        boxWidth: 8,
                        padding: 16,
                    }}
                }},
                annotation: {{ annotations }},
                tooltip: {{
                    backgroundColor: INK,
                    titleColor: '#ede5d3',
                    bodyColor: '#ede5d3',
                    borderWidth: 0,
                    titleFont: {{ family: "'Instrument Serif', serif", style: 'italic', size: 16 }},
                    bodyFont: {{ family: "'JetBrains Mono', monospace", size: 11 }},
                    padding: 12,
                    cornerRadius: 0,
                    displayColors: false,
                    callbacks: {{
                        title: items => items.length ? Math.round(items[0].parsed.x).toString() : '',
                        label: function(ctx) {{
                            const v = ctx.parsed.y;
                            return ctx.dataset.label + ' · ' + (v != null ? v.toFixed(2) : '—');
                        }},
                        afterBody: function(items) {{
                            if (!items.length) return;
                            const year = Math.round(items[0].parsed.x);
                            const lines = [];
                            const policy = (data.policies || []).find(p => p.year === year);
                            const climateEv = (data.climate || []).find(c => c.year === year);
                            if (policy) lines.push('', '§ Policy — ' + policy.text);
                            if (climateEv) lines.push('', '△ Climate — ' + climateEv.text);
                            return lines;
                        }}
                    }}
                }}
            }},
            scales: {{
                x: {{
                    type: 'linear',
                    ticks: {{
                        color: '#6a6d60',
                        font: {{ family: "'JetBrains Mono', monospace", size: 10 }},
                        stepSize: 5,
                        callback: v => v.toString(),
                    }},
                    grid: {{ color: 'rgba(58, 63, 53, 0.1)', drawTicks: false }},
                    border: {{ color: INK, width: 1 }},
                }},
                y: {{
                    ticks: {{
                        color: MOSS,
                        font: {{ family: "'JetBrains Mono', monospace", size: 10 }},
                        callback: v => v.toFixed(1) + '%',
                    }},
                    grid: {{ color: 'rgba(58, 63, 53, 0.08)', drawTicks: false }},
                    border: {{ color: INK, width: 1 }},
                }},
                y2: {{
                    position: 'right',
                    ticks: {{
                        color: AMBER,
                        font: {{ family: "'JetBrains Mono', monospace", size: 9 }},
                    }},
                    grid: {{ display: false }},
                    border: {{ color: 'rgba(168, 115, 42, 0.4)' }},
                }}
            }}
        }}
    }});
}}

function renderPolicies(data) {{
    const el = document.getElementById('policies');
    if (!data.policies || !data.policies.length) {{
        el.innerHTML = `
            <div class="panel-head">
                <div class="panel-title">The Register · <em>${{data.country}}</em></div>
                <div class="panel-count">— empty —</div>
            </div>
            <div style="font-style:italic;color:var(--ink-mute);padding:16px 0">
                No policy events catalogued for ${{data.country}}.
            </div>`;
        return;
    }}
    const items = data.policies.map(p => {{
        const forestVal = data.forest.find(f => f.year === p.year);
        const forestText = forestVal ? forestVal.value.toFixed(1) + '%' : '—';
        const link = p.url ? `<a href="${{p.url}}" target="_blank" title="Source">↗</a>` : '';
        return `<div class="policy">
            <div class="policy-year">${{p.year}}</div>
            <div class="policy-text">${{p.text}}${{link}}</div>
            <div class="policy-metric">${{forestText}}</div>
        </div>`;
    }}).join('');
    el.innerHTML = `
        <div class="panel-head">
            <div class="panel-title">The Register · <em>${{data.country}}</em></div>
            <div class="panel-count">${{data.policies.length.toString().padStart(2, '0')}} entries</div>
        </div>
        ${{items}}`;
}}

function renderVerdict(data) {{
    const el = document.getElementById('verdict-panel');
    if (!data.policies || !data.policies.length || !data.forest.length) {{
        el.innerHTML = `
            <div class="panel-head">
                <div class="panel-title">The Verdict · <em>${{data.country}}</em></div>
                <div class="panel-count">— pending —</div>
            </div>
            <div style="font-style:italic;color:var(--ink-mute);padding:16px 0">
                Insufficient data for ${{data.country}} to render judgment.
            </div>`;
        return;
    }}
    const firstPolicy = data.policies[0].year;
    const pre = data.forest.filter(f => f.year < firstPolicy);
    const post = data.forest.filter(f => f.year >= firstPolicy);

    if (pre.length < 2 || post.length < 2) {{
        el.innerHTML = `
            <div class="panel-head">
                <div class="panel-title">The Verdict · <em>${{data.country}}</em></div>
                <div class="panel-count">— pending —</div>
            </div>
            <div style="font-style:italic;color:var(--ink-mute);padding:16px 0">
                Too few observations either side of intervention.
            </div>`;
        return;
    }}

    const preRate = (pre[pre.length-1].value - pre[0].value) / (pre[pre.length-1].year - pre[0].year);
    const postRate = (post[post.length-1].value - post[0].value) / (post[post.length-1].year - post[0].year);
    const improved = Math.abs(postRate) < Math.abs(preRate);
    const pct = preRate !== 0 ? ((1 - Math.abs(postRate) / Math.abs(preRate)) * 100) : 0;

    el.innerHTML = `
        <div class="panel-head">
            <div class="panel-title">The Verdict · <em>${{data.country}}</em></div>
            <div class="panel-count">Pre / Post · 1st intervention</div>
        </div>
        <div class="rate-row">
            <div class="stage">Pre</div>
            <div class="desc">${{pre[0].year}}–${{pre[pre.length-1].year}} · before first policy</div>
            <div class="rate ${{preRate < 0 ? 'neg' : 'pos'}}">${{preRate > 0 ? '+' : ''}}${{preRate.toFixed(3)}}%/yr</div>
        </div>
        <div class="rate-row">
            <div class="stage">Post</div>
            <div class="desc">${{post[0].year}}–${{post[post.length-1].year}} · since first policy</div>
            <div class="rate ${{postRate < 0 ? 'neg' : 'pos'}}">${{postRate > 0 ? '+' : ''}}${{postRate.toFixed(3)}}%/yr</div>
        </div>
        <div class="verdict">
            <div class="headline ${{improved ? 'good' : 'bad'}}">
                ${{improved ? data.country + ' slowed by ' + Math.abs(pct).toFixed(0) + '%' : data.country + ': no improvement'}}
            </div>
            <div class="sub">${{improved ? 'after policy intervention' : 'rate unchanged or worsened'}}</div>
        </div>
    `;
}}

async function handlePointClick(year) {{
    const country = countrySelect.value;
    const dossierEl = document.getElementById('dossier');
    const headEl = document.getElementById('dossier-head');
    const contentEl = document.getElementById('dossier-content');
    dossierEl.style.display = 'block';
    dossierEl.scrollIntoView({{ behavior: 'smooth', block: 'nearest' }});
    headEl.innerHTML = `Filing dossier for <em>${{year}}</em>`;
    contentEl.innerHTML = '<div style="font-style:italic;color:var(--ink-mute);padding:14px 0">Compiling field notes…</div>';

    const result = await callApi('explain_spike', {{country, year}});
    if (result.error) {{
        contentEl.innerHTML = '<div style="color:var(--oxblood);font-style:italic">' + result.error + '</div>';
        return;
    }}

    const trendClass = (() => {{
        if (['sharp decline', 'acceleration'].includes(result.trend)) return 'neg';
        if (['recovery', 'sharp increase'].includes(result.trend)) return 'pos';
        return 'alert';
    }})();

    headEl.innerHTML = `Dossier · <em>${{country}}</em> · ${{result.year}}`;

    let html = `
        <div class="dossier-stats">
            <div class="dossier-stat">
                <div class="k">Year</div>
                <div class="v">${{result.year}}</div>
            </div>
            <div class="dossier-stat">
                <div class="k">Forest cover</div>
                <div class="v">${{result.forest_pct !== null ? result.forest_pct.toFixed(2) + '%' : '—'}}</div>
            </div>
            <div class="dossier-stat">
                <div class="k">Year-on-year</div>
                <div class="v ${{result.yoy_change !== null && result.yoy_change < 0 ? 'neg' : (result.yoy_change > 0 ? 'pos' : '')}}">${{result.yoy_change !== null ? (result.yoy_change > 0 ? '+' : '') + result.yoy_change.toFixed(3) + 'pp' : '—'}}</div>
            </div>
            <div class="dossier-stat">
                <div class="k">Trend</div>
                <div class="v ${{trendClass}}">${{result.trend}}</div>
            </div>
        </div>
    `;

    if (result.rate_5yr_before !== null || result.rate_5yr_after !== null) {{
        html += `<div class="rate-strip">`;
        if (result.rate_5yr_before !== null) {{
            html += `<div class="rate-item"><span class="k">5yr · before</span><span class="v ${{result.rate_5yr_before < 0 ? 'neg' : 'pos'}}">${{result.rate_5yr_before > 0 ? '+' : ''}}${{result.rate_5yr_before}}%/yr</span></div>`;
        }}
        if (result.rate_5yr_after !== null) {{
            html += `<div class="rate-item"><span class="k">5yr · after</span><span class="v ${{result.rate_5yr_after < 0 ? 'neg' : 'pos'}}">${{result.rate_5yr_after > 0 ? '+' : ''}}${{result.rate_5yr_after}}%/yr</span></div>`;
        }}
        html += `</div>`;
    }}

    if (result.context) {{
        html += `<div class="context-block">
            <div class="k">Field note · historical context</div>
            <div class="v">${{result.context}}</div>
        </div>`;
    }}

    if (result.nearby_policies && result.nearby_policies.length) {{
        html += `<div class="nearby-block">
            <div class="k">§ Nearby policy events · ±3 years</div>`;
        result.nearby_policies.forEach(p => {{
            const pLink = p.url ? ` <a href="${{p.url}}" target="_blank" style="color:var(--ink-mute);font-size:11px">↗</a>` : '';
            html += `<div class="nearby-item"><span class="yr">${{p.year}}</span><span>${{p.text}}${{pLink}}</span></div>`;
        }});
        html += `</div>`;
    }}

    if (result.nearby_climate && result.nearby_climate.length) {{
        html += `<div class="nearby-block">
            <div class="k" style="color:var(--amber-deep)">△ Nearby climate events · ±3 years</div>`;
        result.nearby_climate.forEach(c => {{
            const cLink = c.url ? ` <a href="${{c.url}}" target="_blank" style="color:var(--ink-mute);font-size:11px">↗</a>` : '';
            html += `<div class="nearby-item"><span class="yr" style="color:var(--amber-deep)">${{c.year}}</span><span>${{c.text}}${{cLink}}</span></div>`;
        }});
        html += `</div>`;
    }}

    contentEl.innerHTML = html;
}}

loadData();
</script>
</body>
</html>"""


if __name__ == "__main__":
    app.launch(mcp_server=True)