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


// ==================== Constants ====================
const BBOX_COLORS = [
            '#e74c3c', '#3498db', '#2ecc71', '#f39c12', '#9b59b6',
            '#1abc9c', '#e67e22', '#34495e', '#16a085', '#c0392b',
            '#2980b9', '#27ae60', '#d35400', '#8e44ad', '#17a2b8',
            '#ff6b6b', '#4ecdc4', '#45b7d1', '#f7b731', '#5f27cd'
        ];

const HISTORY_COLORS = ['hc-0', 'hc-1', 'hc-2', 'hc-3', 'hc-4', 'hc-5'];

// ==================== Stable JSON Stringify ====================
// Preserve object key order based on previous JSON (including nested objects in arrays).
// This avoids JS's default integer-like key reordering (e.g. "123" jumping to the front).
function _joinPath(pathKeys) {
    return pathKeys.join('\0');
}

// 直接扫描 JSON 原始字符串来提取 key 顺序,避免 JSON.parse 自动把数字 key 排到前面
function _extractKeyOrdersFromJSON(jsonStr) {
    if (!jsonStr || !jsonStr.trim()) return {};
    try {
        const keyOrders = {};
        const pathStack = []; // {type:'obj'|'arr', keys:[], currentKey:null, index:0}
        let i = 0;
        const len = jsonStr.length;

        function skipWS() { while (i < len && /\s/.test(jsonStr[i])) i++; }

        function readStr() {
            if (jsonStr[i] !== '"') return null;
            i++; // skip "
            let s = '';
            while (i < len && jsonStr[i] !== '"') {
                if (jsonStr[i] === '\\') { s += jsonStr[i] + jsonStr[i + 1]; i += 2; }
                else { s += jsonStr[i]; i++; }
            }
            i++; // skip closing "
            try { return JSON.parse('"' + s + '"'); } catch { return s; }
        }

        function curPath() {
            const parts = [];
            for (const f of pathStack) {
                if (f.type === 'arr') parts.push(String(f.index));
                else if (f.currentKey !== null) parts.push(f.currentKey);
            }
            return parts;
        }

        while (i < len) {
            skipWS();
            if (i >= len) break;
            const ch = jsonStr[i];
            if (ch === '{') {
                const p = curPath();
                const frame = { type: 'obj', keys: [], currentKey: null };
                pathStack.push(frame);
                keyOrders[_joinPath(p)] = frame.keys;
                i++;
            } else if (ch === '}') {
                pathStack.pop();
                i++;
                if (pathStack.length > 0 && pathStack[pathStack.length - 1].type === 'obj') {
                    pathStack[pathStack.length - 1].currentKey = null;
                }
            } else if (ch === '[') {
                pathStack.push({ type: 'arr', index: 0 });
                i++;
            } else if (ch === ']') {
                pathStack.pop();
                i++;
                if (pathStack.length > 0 && pathStack[pathStack.length - 1].type === 'obj') {
                    pathStack[pathStack.length - 1].currentKey = null;
                }
            } else if (ch === ',') {
                i++;
                if (pathStack.length > 0) {
                    const top = pathStack[pathStack.length - 1];
                    if (top.type === 'arr') top.index++;
                    else top.currentKey = null;
                }
            } else if (ch === ':') {
                i++;
            } else if (ch === '"') {
                const str = readStr();
                skipWS();
                if (i < len && jsonStr[i] === ':') {
                    // It's an object key
                    const top = pathStack[pathStack.length - 1];
                    if (top && top.type === 'obj') {
                        top.keys.push(str);
                        top.currentKey = str;
                    }
                }
                // else it's a string value — do nothing
            } else {
                // number / boolean / null
                const m = jsonStr.substring(i).match(/^(-?(?:0|[1-9]\d*)(?:\.\d+)?(?:[eE][+-]?\d+)?|true|false|null)/);
                if (m) i += m[0].length; else i++;
            }
        }
        return keyOrders;
    } catch (e) {
        return {};
    }
}

function _stableStr(val, keyOrders, pathKeys, level) {
    if (val === null || val === undefined) return 'null';
    if (typeof val === 'boolean') return val ? 'true' : 'false';
    if (typeof val === 'number') return isFinite(val) ? String(val) : 'null';
    if (typeof val === 'string') return JSON.stringify(val);

    const indentStr = '  '.repeat(level);
    const childIndent = '  '.repeat(level + 1);

    if (Array.isArray(val)) {
        if (val.length === 0) return '[]';
        const items = val.map((item, i) =>
            childIndent + _stableStr(item, keyOrders, [...pathKeys, String(i)], level + 1)
        );
        return '[\n' + items.join(',\n') + '\n' + indentStr + ']';
    }

    // Object
    const allKeys = Object.keys(val);
    if (allKeys.length === 0) return '{}';

    // Get reference key order for this path
    const parentPath = _joinPath(pathKeys);
    const refKeys = keyOrders[parentPath] || [];

    // Order: reference keys first (in reference order), then remaining keys (appended)
    const orderedKeys = [];
    refKeys.forEach(k => { if (allKeys.includes(k)) orderedKeys.push(k); });
    allKeys.forEach(k => { if (!orderedKeys.includes(k)) orderedKeys.push(k); });

    const items = orderedKeys.map(k =>
        childIndent + JSON.stringify(k) + ': ' + _stableStr(val[k], keyOrders, [...pathKeys, k], level + 1)
    );
    return '{\n' + items.join(',\n') + '\n' + indentStr + '}';
}

function stableJSONStringify(obj, referenceStr) {
    try {
        const keyOrders = _extractKeyOrdersFromJSON(referenceStr);
        return _stableStr(obj, keyOrders, [], 0);
    } catch (e) {
        return JSON.stringify(obj, null, 2);
    }
}

// ==================== Global State ====================
const state = {
    // Current image: { src: string (dataURL/URL), width: number, height: number } | null
    currentImage: null,

    // Structured Prompt (JSON string)
    structuredPrompt: '',

    // User Prompt
    userPrompt: '',

    // Generation parameters
    params: {
        forceT2I: false,
        ditEndpoint: '',
        cfgScale: 4.0,
        steps: 25,
        height: 1024,
        width: 1024,
        seed: 42,
    },

    // History: array of { type, inputImage, outputImage, userPrompt, structuredPrompt, params, timestamp, colorIndex }
    history: [],
    selectedHistoryIndex: -1,
    generationCount: 0,

    // SP undo stack
    undoStack: [],

    // UI flags
    bboxVisible: false,
    isGenerating: false,
    pendingGeneration: null,  // callback for diff confirmation
    spRawMode: false,         // true = raw textarea, false = structured view
    activeBboxIndex: -1,      // highlighted bbox element index
    bboxEditIndex: -1,        // bbox in edit mode (double-click to activate)
    focusedBboxIndex: -1,     // 被点击锁定的 bbox index(持久显示)
    // BBox drag state
    bboxDrag: null, // { elemIndex, handle, startX, startY, startBbox:[x1,y1,x2,y2], containerRect }

    // Parsed SP data
    parsedSP: null,           // parsed JSON object

    // Parsed elements from SP (elements + scene.elements with bbox)
    parsedElements: [],

    // Image modal zoom state
    zoomState: { scale: 1, translateX: 0, translateY: 0 },
};

// ==================== Initialization ====================
document.addEventListener('DOMContentLoaded', init);

function init() {
    setupDragDrop();
    setupAutoSync();
    setupKeyboardShortcuts();
}

function setupDragDrop() {
    const imageArea = document.getElementById('imageArea');
    const dropzone = document.getElementById('dropzone');

    ['dragenter', 'dragover'].forEach(evt => {
        imageArea.addEventListener(evt, e => {
            e.preventDefault();
            e.stopPropagation();
            if (dropzone) dropzone.classList.add('dragover');
        });
    });

    ['dragleave', 'drop'].forEach(evt => {
        imageArea.addEventListener(evt, e => {
            e.preventDefault();
            e.stopPropagation();
            if (dropzone) dropzone.classList.remove('dragover');
        });
    });

    imageArea.addEventListener('drop', e => {
        const files = e.dataTransfer.files;
        if (files.length > 0 && files[0].type.startsWith('image/')) {
            loadImageFile(files[0]);
        }
    });

    // Also support paste
    document.addEventListener('paste', e => {
        const items = e.clipboardData && e.clipboardData.items;
        if (!items) return;
        for (const item of items) {
            if (item.type.startsWith('image/')) {
                const file = item.getAsFile();
                if (file) loadImageFile(file);
                break;
            }
        }
    });
}

function setupAutoSync() {
    // Sync userPrompt from input
    document.getElementById('userPromptInput').addEventListener('input', function () {
        state.userPrompt = this.value;
    });

    // Sync SP from textarea
    document.getElementById('spTextarea').addEventListener('input', function () {
        state.structuredPrompt = this.value;
        updateSPButtons();
        parseSPAndUpdateBboxes();
    });

    // Sync params on change
    ['paramCfgScale', 'paramSteps', 'paramHeight', 'paramWidth', 'paramSeed'].forEach(id => {
        const el = document.getElementById(id);
        if (el) el.addEventListener('change', syncParamsFromUI);
    });
}

function setupKeyboardShortcuts() {
    // Ctrl+Z when focused on SP textarea => undo
    document.getElementById('spTextarea').addEventListener('keydown', function (e) {
        if (e.ctrlKey && e.key === 'z') {
            e.preventDefault();
            handleSPUndo();
        }
    });

    // Ctrl+Enter in user prompt => generate SP
    document.getElementById('userPromptInput').addEventListener('keydown', function (e) {
        if (e.ctrlKey && e.key === 'Enter') {
            e.preventDefault();
            handleGenerateSP();
        }
    });
}

// ==================== Params Sync ====================
// Small helpers so removing a UI element (e.g. Force-T2I, URL) doesn't
// crash the whole flow with "Cannot read properties of null".
function _byId(id) { return document.getElementById(id); }
function _valOr(id, dflt) { const el = _byId(id); return el ? el.value : dflt; }
function _checkedOr(id, dflt) { const el = _byId(id); return el ? el.checked : dflt; }

function syncParamsFromUI() {
    state.params.forceT2I = _checkedOr('paramForceT2I', false);
    state.params.ditEndpoint = (_valOr('paramDitEndpoint', '') || '').trim();
    state.params.cfgScale = parseFloat(_valOr('paramCfgScale', 4.0)) || 4.0;
    state.params.steps = parseInt(_valOr('paramSteps', 25)) || 25;
    state.params.height = parseInt(_valOr('paramHeight', 1024)) || 1024;
    state.params.width = parseInt(_valOr('paramWidth', 1024)) || 1024;
    state.params.seed = parseInt(_valOr('paramSeed', 42)) || 42;
}

function _setIfExists(id, val, prop) {
    const el = _byId(id);
    if (el) el[prop] = val;
}

function syncParamsToUI() {
    _setIfExists('paramForceT2I', state.params.forceT2I || false, 'checked');
    _setIfExists('paramDitEndpoint', state.params.ditEndpoint || '', 'value');
    document.getElementById('paramCfgScale').value = state.params.cfgScale !== undefined ? state.params.cfgScale : 4.0;
    document.getElementById('paramSteps').value = state.params.steps;
    document.getElementById('paramHeight').value = state.params.height;
    document.getElementById('paramWidth').value = state.params.width;
    document.getElementById('paramSeed').value = state.params.seed;
}

// ==================== Image Panel ====================
function triggerImageUpload() {
    document.getElementById('imageFileInput').click();
}

function handleImageFileSelected(event) {
    const file = event.target.files[0];
    if (file) loadImageFile(file);
    event.target.value = '';
}

function loadImageFile(file) {
    const reader = new FileReader();
    reader.onload = function (e) {
        const img = new Image();
        img.onload = function () {
            state.currentImage = {
                src: e.target.result,
                width: img.naturalWidth,
                height: img.naturalHeight,
            };
            updateImagePanel();
            autoUpdateHW();
            showToast('Image loaded', 'success');
        };
        img.onerror = function () {
            showToast('Failed to load image', 'error');
        };
        img.src = e.target.result;
    };
    reader.readAsDataURL(file);
}

function loadImageFromSrc(src) {
    const img = new Image();
    img.onload = function () {
        state.currentImage = {
            src: src,
            width: img.naturalWidth,
            height: img.naturalHeight,
        };
        updateImagePanel();
    };
    img.src = src;
}

function autoUpdateHW() {
    if (!state.currentImage) return;
    // Round to nearest 32
    state.params.height = Math.round(state.currentImage.height / 32) * 32;
    state.params.width = Math.round(state.currentImage.width / 32) * 32;
    syncParamsToUI();
}

function handleImageDelete() {
    state.currentImage = null;
    state.bboxVisible = false;
    state.activeBboxIndex = -1;
    updateImagePanel();
    showToast('Image removed', 'info');
}

function handleImageEnlarge() {
    if (!state.currentImage) return;
    const modal = document.getElementById('imageModal');
    const img = document.getElementById('modalImage');
    img.src = state.currentImage.src;

    // Reset zoom
    state.zoomState = { scale: 1, translateX: 0, translateY: 0 };
    applyModalZoom();

    modal.classList.add('visible');

    // Setup zoom listeners
    const container = document.getElementById('modalImageContainer');
    container.addEventListener('wheel', onModalWheel, { passive: false });
    container.addEventListener('mousedown', onModalPanStart);
}

function closeImageModal() {
    const modal = document.getElementById('imageModal');
    modal.classList.remove('visible');
    const container = document.getElementById('modalImageContainer');
    container.removeEventListener('wheel', onModalWheel);
    container.removeEventListener('mousedown', onModalPanStart);
    state.zoomState = { scale: 1, translateX: 0, translateY: 0 };
}

function onModalWheel(e) {
    e.preventDefault();
    const z = state.zoomState;
    const factor = e.deltaY > 0 ? 0.9 : 1.1;
    const newScale = Math.max(0.5, Math.min(20, z.scale * factor));

    // Zoom towards mouse position
    const container = document.getElementById('modalImageContainer');
    const rect = container.getBoundingClientRect();
    const mx = e.clientX - rect.left - rect.width / 2;
    const my = e.clientY - rect.top - rect.height / 2;

    const sf = newScale / z.scale;
    z.translateX = mx - sf * (mx - z.translateX);
    z.translateY = my - sf * (my - z.translateY);
    z.scale = newScale;
    applyModalZoom();
}

function onModalPanStart(e) {
    if (e.button !== 0) return;
    if (state.zoomState.scale <= 1.01) return;
    e.preventDefault();
    const z = state.zoomState;
    const startX = e.clientX, startY = e.clientY;
    const startTX = z.translateX, startTY = z.translateY;

    const onMove = (ev) => {
        z.translateX = startTX + (ev.clientX - startX);
        z.translateY = startTY + (ev.clientY - startY);
        applyModalZoom();
    };
    const onUp = () => {
        document.removeEventListener('mousemove', onMove);
        document.removeEventListener('mouseup', onUp);
    };
    document.addEventListener('mousemove', onMove);
    document.addEventListener('mouseup', onUp);
}

function applyModalZoom() {
    const img = document.getElementById('modalImage');
    const z = state.zoomState;
    img.style.transform = `translate(${z.translateX}px, ${z.translateY}px) scale(${z.scale})`;
    img.style.cursor = z.scale > 1.01 ? 'grab' : 'default';
}

function updateImagePanel() {
    const dropzone = document.getElementById('dropzone');
    const display = document.getElementById('imageDisplay');
    const btnZoom = document.getElementById('btnImageZoom');
    const btnDelete = document.getElementById('btnImageDelete');
    const btnBboxShow = document.getElementById('btnBboxShow');
    const btnBboxHide = document.getElementById('btnBboxHide');

    if (state.currentImage) {
        dropzone.style.display = 'none';
        display.style.display = 'flex';
        document.getElementById('displayImage').src = state.currentImage.src;
        btnZoom.style.display = '';
        btnDelete.style.display = '';
        updateBboxButtons();
        if (state.bboxVisible) {
            requestAnimationFrame(() => renderBboxes());
        }
            } else {
        dropzone.style.display = '';
        display.style.display = 'none';
        btnZoom.style.display = 'none';
        btnDelete.style.display = 'none';
        btnBboxShow.style.display = 'none';
        btnBboxHide.style.display = 'none';
        clearBboxes();
    }
}

function updateBboxButtons() {
    const hasElems = state.parsedElements.length > 0;
    const btnBboxShow = document.getElementById('btnBboxShow');
    const btnBboxHide = document.getElementById('btnBboxHide');
    if (!state.currentImage || !hasElems) {
        btnBboxShow.style.display = 'none';
        btnBboxHide.style.display = 'none';
        return;
    }
    if (state.bboxVisible) {
        btnBboxShow.style.display = 'none';
        btnBboxHide.style.display = '';
    } else {
        btnBboxShow.style.display = '';
        btnBboxHide.style.display = 'none';
    }
}

// ==================== BBox Visualization ====================
function showAllBboxes() {
    if (state.parsedElements.length === 0) {
        parseSPAndUpdateBboxes();
        if (state.parsedElements.length === 0) return;
    }
    state.focusedBboxIndex = -1;
    state.bboxVisible = true;
    state.activeBboxIndex = -1;
    state.bboxEditIndex = -1;
    renderBboxes(-1); // -1 means render all
    updateBboxButtons();
    document.querySelectorAll('.sp-element').forEach(el => el.classList.remove('highlight'));
}

function hideAllBboxes() {
    state.focusedBboxIndex = -1;
    state.bboxVisible = false;
    state.activeBboxIndex = -1;
    state.bboxEditIndex = -1;
    clearBboxes();
    updateBboxButtons();
    document.querySelectorAll('.sp-element').forEach(el => el.classList.remove('highlight'));
}

function handleImageClick(event) {
    // 检测点击位置是否有 element 的 bbox
    if (state.parsedElements.length > 0 && state.currentImage) {
        const imgEl = document.getElementById('displayImage');
        const imgRect = imgEl.getBoundingClientRect();
        const clickX = ((event.clientX - imgRect.left) / imgRect.width) * 1000;
        const clickY = ((event.clientY - imgRect.top) / imgRect.height) * 1000;

        // 找所有包含点击坐标的 bbox
        const overlapping = [];
        state.parsedElements.forEach((elem, i) => {
            if (!elem.bbox) return;
            const [x1, y1, x2, y2] = elem.bbox;
            if (clickX >= x1 && clickX <= x2 && clickY >= y1 && clickY <= y2) {
                overlapping.push(i);
            }
        });

        if (overlapping.length > 0) {
            // 在重叠列表中循环切换
            let nextIndex;
            if (state.focusedBboxIndex >= 0 && overlapping.includes(state.focusedBboxIndex)) {
                const currentPos = overlapping.indexOf(state.focusedBboxIndex);
                nextIndex = overlapping[(currentPos + 1) % overlapping.length];
            } else {
                nextIndex = overlapping[0];
            }
            // 聚焦该 element 的 bbox
            focusBbox(nextIndex);
            // 滚动到对应的 element 卡片
            if (state.parsedElements[nextIndex]) {
                const card = document.querySelector(`.sp-element[data-elem-id="${state.parsedElements[nextIndex].id}"]`);
                if (card) card.scrollIntoView({ behavior: 'smooth', block: 'nearest' });
            }
            return;
        }
    }

    // 点击空白区域 => 清除锁定的 bbox
    state.focusedBboxIndex = -1;
    state.activeBboxIndex = -1;
    state.bboxEditIndex = -1;
    if (state.bboxVisible) {
        clearBboxes();
        state.bboxVisible = false;
        updateBboxButtons();
    }
    document.querySelectorAll('.sp-element').forEach(el => el.classList.remove('highlight'));
}

// filterIndex: 如果 >= 0,只渲染该索引的 bbox;否则渲染全部
function renderBboxes(filterIndex) {
    clearBboxes();
    if (!state.currentImage || state.parsedElements.length === 0) return;

    const imgEl = document.getElementById('displayImage');
    const container = document.getElementById('bboxContainer');

    if (!imgEl || !imgEl.offsetWidth) return;

    const imgRect = imgEl.getBoundingClientRect();
    const parentRect = imgEl.parentElement.getBoundingClientRect();

    container.style.left = (imgRect.left - parentRect.left) + 'px';
    container.style.top = (imgRect.top - parentRect.top) + 'px';
    container.style.width = imgRect.width + 'px';
    container.style.height = imgRect.height + 'px';
    container.style.display = 'block';

    state.parsedElements.forEach((elem, i) => {
        if (!elem.bbox) return;
        // 如果指定了 filterIndex,只渲染该元素
        if (filterIndex !== undefined && filterIndex >= 0 && i !== filterIndex) return;

        const [x1, y1, x2, y2] = elem.bbox;
        const color = getColorForElemId(elem.id);

        const isActive = (i === state.bboxEditIndex);
        const box = document.createElement('div');
        box.className = 'bbox-box' + (isActive ? ' active' : '');
        box.dataset.elemIndex = i;
        box.style.borderColor = color;
        box.style.left = (x1 / 1000 * 100) + '%';
        box.style.top = (y1 / 1000 * 100) + '%';
        box.style.width = ((x2 - x1) / 1000 * 100) + '%';
        box.style.height = ((y2 - y1) / 1000 * 100) + '%';

        // Semi-transparent background
        const r = parseInt(color.slice(1, 3), 16);
        const g = parseInt(color.slice(3, 5), 16);
        const b = parseInt(color.slice(5, 7), 16);
        box.style.background = `rgba(${r},${g},${b},0.12)`;

        // Label
        const label = document.createElement('div');
        label.className = 'bbox-label';
        label.style.background = color;
        label.textContent = `${elem.id}: ${elem.caption || ''}`.substring(0, 30);
        box.appendChild(label);

        // Drag to move / click to highlight (mousedown on box body)
        box.addEventListener('mousedown', (e) => {
            if (e.target.classList.contains('bbox-handle')) return;
            e.preventDefault();
            e.stopPropagation();
            // 仅在编辑模式下允许拖拽移动,否则仅处理点击
            if (state.bboxEditIndex !== i) {
                handleBboxClick(i, e);
                return;
            }
            state.bboxClickStart = { x: e.clientX, y: e.clientY, index: i };
            initBboxDrag(e, i, 'move');
        });

        // Resize handles (8 directions) — only visible when active
        const handles = ['nw', 'ne', 'sw', 'se', 'n', 's', 'w', 'e'];
        handles.forEach(h => {
            const handle = document.createElement('div');
            handle.className = `bbox-handle h-${h}`;
            handle.style.borderColor = color;
            handle.addEventListener('mousedown', (e) => {
                e.preventDefault();
                e.stopPropagation();
                highlightBbox(i);
                initBboxDrag(e, i, h);
            });
            box.appendChild(handle);
        });

        container.appendChild(box);
    });
}

// 点击 bbox 时处理重叠切换
function handleBboxClick(clickedIndex, e) {
    const container = document.getElementById('bboxContainer');
    const containerRect = container.getBoundingClientRect();
    const clickX = ((e.clientX - containerRect.left) / containerRect.width) * 1000;
    const clickY = ((e.clientY - containerRect.top) / containerRect.height) * 1000;

    // 找所有包含点击坐标的 bbox
    const overlapping = [];
    state.parsedElements.forEach((elem, i) => {
        if (!elem.bbox) return;
        const [x1, y1, x2, y2] = elem.bbox;
        if (clickX >= x1 && clickX <= x2 && clickY >= y1 && clickY <= y2) {
            overlapping.push(i);
        }
    });

    if (overlapping.length <= 1) {
        // 单个 bbox:聚焦并显示该 bbox(不进入编辑模式)
        focusBbox(clickedIndex);
        return;
    }

    // 在重叠列表中循环切换
    const currentIdx = state.focusedBboxIndex >= 0 ? state.focusedBboxIndex : state.activeBboxIndex;
    const currentPos = overlapping.indexOf(currentIdx);
    const nextPos = (currentPos + 1) % overlapping.length;
    const nextIdx = overlapping[nextPos];
    // 重叠切换时必须用 focus,确保左侧图像 bbox 与右侧选中项一致
    focusBbox(nextIdx);
}

function clearBboxes() {
    const container = document.getElementById('bboxContainer');
    container.innerHTML = '';
    container.style.display = 'none';
}

function parseSPAndUpdateBboxes() {
    state.parsedElements = [];
    state.parsedSP = null;
    if (!state.structuredPrompt.trim()) {
        if (state.bboxVisible) clearBboxes();
        updateBboxButtons();
        return;
    }
    try {
        const data = JSON.parse(state.structuredPrompt);
        state.parsedSP = data;
        state.parsedElements = extractElements(data);
    } catch (e) {
        // Not valid JSON yet
    }
    if (state.bboxVisible && state.currentImage) {
        renderBboxes();
    }
    updateBboxButtons();
}

// 基于元素 ID 的稳定颜色映射(不因数组顺序变化而改变)
function getColorForElemId(id) {
    const numId = parseInt(id) || 0;
    return BBOX_COLORS[numId % BBOX_COLORS.length];
}

        function parseBbox(positionStr) {
            if (!positionStr) return null;
            const match = positionStr.match(/<bbox>([^<]+)<\/bbox>/) || positionStr.match(/([\d\s]+)/);
            if (!match) return null;
            const coords = match[1].trim().split(/\s+/).map(Number);
            if (coords.length !== 4) return null;
            return coords;
        }

        function extractElements(data) {
            const elements = [];
            
            if (data.elements && Array.isArray(data.elements)) {
                data.elements.forEach(elem => {
                    if (elem.position) {
                        const bbox = parseBbox(elem.position);
                        if (bbox) {
                    elements.push({ id: elem.id, caption: elem.caption || '', bbox, source: 'elements' });
                        }
                    }
                });
            }
            
            if (data.scene && data.scene.elements && Array.isArray(data.scene.elements)) {
                data.scene.elements.forEach(elem => {
                    if (elem.position) {
                        const bbox = parseBbox(elem.position);
                        if (bbox) {
                    elements.push({ id: elem.id, caption: elem.caption || '', bbox, source: 'scene' });
                        }
                    }
                });
            }
            
            return elements;
        }

// ==================== SP Panel ====================
function triggerSPImport() {
    document.getElementById('spFileInput').click();
}

function handleSPFileSelected(event) {
    const file = event.target.files[0];
    if (!file) return;
    const reader = new FileReader();
    reader.onload = function (e) {
        pushSPUndo();
        state.structuredPrompt = e.target.result;
        parseSPAndUpdateBboxes();
        updateSPPanel();
        showToast('Structured Prompt imported', 'success');
    };
    reader.readAsText(file);
    event.target.value = '';
}

function handleSPDelete() {
    if (!state.structuredPrompt.trim()) return;
    pushSPUndo();
    state.structuredPrompt = '';
    state.parsedSP = null;
    updateSPPanel();
    clearBboxes();
    state.parsedElements = [];
    state.bboxVisible = false;
    showToast('Structured Prompt cleared', 'info');
}

function handleSPUndo() {
    if (state.undoStack.length === 0) {
        showToast('Nothing to undo', 'info');
                return;
    }
    state.structuredPrompt = state.undoStack.pop();
    parseSPAndUpdateBboxes();
    updateSPPanel();
    showToast('Undone', 'success');
}

function pushSPUndo() {
    state.undoStack.push(state.structuredPrompt);
    if (state.undoStack.length > 50) state.undoStack.shift();
}

function toggleSPRawMode() {
    state.spRawMode = !state.spRawMode;
    const btn = document.getElementById('btnSPRaw');
    if (state.spRawMode) {
        btn.classList.add('active');
    } else {
        btn.classList.remove('active');
        // Sync textarea back to state when switching to structured view
        const ta = document.getElementById('spTextarea');
        if (ta.style.display !== 'none') {
            state.structuredPrompt = ta.value;
            parseSPAndUpdateBboxes();
        }
    }
    updateSPPanel();
}

function updateSPPanel() {
    const spEmpty = document.getElementById('spEmpty');
    const spTextarea = document.getElementById('spTextarea');
    const spStructured = document.getElementById('spStructured');
    const btnUndo = document.getElementById('btnSPUndo');
    const btnDelete = document.getElementById('btnSPDelete');

    if (state.structuredPrompt.trim()) {
        spEmpty.style.display = 'none';
        if (state.spRawMode) {
            spTextarea.style.display = 'block';
            spStructured.style.display = 'none';
            spTextarea.value = state.structuredPrompt;
        } else {
            spTextarea.style.display = 'none';
            spStructured.style.display = 'block';
            renderSPStructured();
        }
        btnDelete.style.display = '';
    } else {
        // SP 为空时显示 textarea 供直接输入
        spEmpty.style.display = 'none';
        spTextarea.style.display = 'block';
        spStructured.style.display = 'none';
        spTextarea.value = '';
        btnDelete.style.display = 'none';
    }
    btnUndo.style.display = state.undoStack.length > 0 ? '' : 'none';
}

function updateSPButtons() {
    const btnUndo = document.getElementById('btnSPUndo');
    const btnDelete = document.getElementById('btnSPDelete');
    btnUndo.style.display = state.undoStack.length > 0 ? '' : 'none';
    btnDelete.style.display = state.structuredPrompt.trim() ? '' : 'none';
}

// Set SP content programmatically (e.g., from LLM result)
function setSPContent(text) {
    pushSPUndo();
    state.structuredPrompt = text;
    parseSPAndUpdateBboxes();
    updateSPPanel();
}

// ==================== SP Structured View ====================
// Key fields displayed in the first section
const SP_KEY_FIELDS = ['intent', 'style', 'atmosphere', 'lighting', 'photography', 'relationships'];

function formatDisplayValue(value) {
    return value === '' ? '""' : value;
}

// 根据 reference JSON 字符串中的 key 出现顺序,返回有序 key 列表
let _renderKeyOrders = {};
function getStableKeys(obj, pathKeys) {
    const allKeys = Object.keys(obj);
    const pathStr = _joinPath(pathKeys);
    const refKeys = _renderKeyOrders[pathStr] || [];
    const ordered = [];
    refKeys.forEach(k => { if (allKeys.includes(k)) ordered.push(k); });
    allKeys.forEach(k => { if (!ordered.includes(k)) ordered.push(k); });
    return ordered;
}

function renderSPStructured() {
    const container = document.getElementById('spStructured');
    container.innerHTML = '';

    // 先确保 parsedElements 已就绪
    if (state.parsedElements.length === 0 && state.structuredPrompt.trim()) {
        parseSPAndUpdateBboxes();
    }

    // 从原始 JSON 字符串提取 key 顺序(避免 Object.keys 自动排序数字 key)
    _renderKeyOrders = _extractKeyOrdersFromJSON(state.structuredPrompt);

    let data;
    try {
        data = JSON.parse(state.structuredPrompt);
        state.parsedSP = data;
    } catch (e) {
        container.innerHTML = '<div style="padding:12px;color:var(--danger);font-size:12px;">JSON parse failed — switch to raw mode to edit</div>';
        return;
    }

    // ===== Section 1: Global Properties =====
    const keySection = createSection('Global', '🔑', undefined, 'global');
    const keyBody = keySection.querySelector('.sp-section-body');

    SP_KEY_FIELDS.forEach(key => {
        if (data[key] === undefined) return;
        const val = data[key];
        if (typeof val === 'object' && !Array.isArray(val)) {
            keyBody.appendChild(createDictField(key, val, [key]));
        } else if (key === 'relationships' && Array.isArray(val)) {
            keyBody.appendChild(createRelationshipsField(val));
        } else {
            keyBody.appendChild(createStringField(key, String(val), [key]));
        }
    });

    // Other top-level keys not in SP_KEY_FIELDS, elements, scene
    getStableKeys(data, []).forEach(key => {
        if (SP_KEY_FIELDS.includes(key) || key === 'elements' || key === 'scene') return;
        const val = data[key];
        if (typeof val === 'object' && !Array.isArray(val)) {
            keyBody.appendChild(createDictField(key, val, [key]));
        } else {
            const displayVal = typeof val === 'string' ? val : JSON.stringify(val);
            keyBody.appendChild(createStringField(key, displayVal, [key]));
        }
    });

    container.appendChild(keySection);

    // ===== Section 2: Elements =====
    const elemArr = data.elements || [];
    const elemSection = createSection('Elements', '🎯', elemArr.length, 'elements');
    const elemBody = elemSection.querySelector('.sp-section-body');
    elemArr.forEach((elem, idx) => {
        elemBody.appendChild(createElementCard(elem, idx, ['elements', idx]));
    });
    container.appendChild(elemSection);

    // ===== Section 3: Scene =====
    const scene = data.scene || {};
    const sceneElems = scene.elements || [];
    const sceneSection = createSection('Scene', '🏞️', sceneElems.length, 'scene');
    const sceneBody = sceneSection.querySelector('.sp-section-body');

    // Scene setting
    if (scene.setting) {
        sceneBody.appendChild(createStringField('setting', scene.setting, ['scene', 'setting']));
    }

    // Scene other keys
    getStableKeys(scene, ['scene']).forEach(key => {
        if (key === 'setting' || key === 'elements') return;
        const val = scene[key];
        if (typeof val === 'object' && !Array.isArray(val)) {
            sceneBody.appendChild(createDictField(key, val, ['scene', key]));
        } else {
            sceneBody.appendChild(createStringField(key, String(val), ['scene', key]));
        }
    });

    // Scene elements
    sceneElems.forEach((elem, idx) => {
        sceneBody.appendChild(createElementCard(elem, idx, ['scene', 'elements', idx], true));
    });
    container.appendChild(sceneSection);
}

function createSection(title, icon, count, sectionType) {
    const section = document.createElement('div');
    section.className = 'sp-section';

    const header = document.createElement('div');
    header.className = 'sp-section-header';
    header.onclick = function(e) {
        if (e.target.closest('.sp-section-add-btn') || e.target.closest('.sp-section-clear-btn')) return;
        section.classList.toggle('collapsed');
    };

    const titleSpan = document.createElement('span');
    titleSpan.className = 'sp-section-title';
    titleSpan.innerHTML = `${icon} ${title}${count !== undefined ? ` <span class='sp-section-badge'>${count}</span>` : ''}`;

    const actionsDiv = document.createElement('div');
    actionsDiv.className = 'sp-section-actions';

    // 统一的 "+" Add按钮
        const addBtn = document.createElement('button');
        addBtn.className = 'sp-section-add-btn';
    addBtn.textContent = '+';
    addBtn.title = 'Add';
        addBtn.onclick = function(e) {
            e.stopPropagation();
        showSectionAddDropdown(addBtn, sectionType);
        };
        actionsDiv.appendChild(addBtn);

    // 🗑️ clear all button
    const clearBtn = document.createElement('button');
    clearBtn.className = 'sp-section-clear-btn';
    clearBtn.innerHTML = '🗑';
    clearBtn.title = 'Clear section';
    clearBtn.onclick = function(e) {
        e.stopPropagation();
        clearSectionFields(sectionType);
    };
    actionsDiv.appendChild(clearBtn);

    const arrow = document.createElement('span');
    arrow.className = 'sp-section-arrow';
    arrow.textContent = '▾';
    actionsDiv.appendChild(arrow);

    header.appendChild(titleSpan);
    header.appendChild(actionsDiv);
    section.appendChild(header);

    const body = document.createElement('div');
    body.className = 'sp-section-body';
    section.appendChild(body);

    return section;
}

function createStringField(key, value, path) {
    const field = document.createElement('div');
    field.className = 'sp-field';

    const keySpan = document.createElement('span');
    keySpan.className = 'sp-field-key';
    keySpan.textContent = key;
    keySpan.title = 'Double-click to edit key';
    keySpan.dataset.path = JSON.stringify(path.slice(0, -1));
    keySpan.dataset.oldKey = key;
    keySpan.ondblclick = function() { startKeyEdit(this); };
    field.appendChild(keySpan);

    const valSpan = document.createElement('span');
    valSpan.className = 'sp-field-value';
    const renderedVal = formatDisplayValue(value);
    valSpan.textContent = renderedVal;
    if (value === '') {
        valSpan.dataset.emptyPlaceholder = '1';
    } else {
        delete valSpan.dataset.emptyPlaceholder;
    }
    valSpan.title = 'Double-click to edit';
    valSpan.dataset.path = JSON.stringify(path);
    valSpan.ondblclick = function() { startFieldEdit(this); };
    field.appendChild(valSpan);

    const actions = document.createElement('div');
    actions.className = 'sp-field-actions';
    const delBtn = document.createElement('button');
    delBtn.className = 'sp-field-btn';
    delBtn.textContent = '✕';
    delBtn.title = 'Remove';
    delBtn.onclick = function() { deleteSPField(path); };
    actions.appendChild(delBtn);
    field.appendChild(actions);

    return field;
}

function createDictField(key, dictObj, basePath) {
    const field = document.createElement('div');
    field.className = 'sp-field sp-field-dict';

    const keySpan = document.createElement('span');
    keySpan.className = 'sp-field-key';
    keySpan.textContent = key;
    keySpan.title = 'Double-click to edit key';
    keySpan.dataset.path = JSON.stringify(basePath.slice(0, -1).length ? basePath.slice(0, -1) : []);
    keySpan.dataset.oldKey = key;
    keySpan.ondblclick = function() { startKeyEdit(this); };
    field.appendChild(keySpan);

    const block = document.createElement('div');
    block.className = 'sp-dict-block';
    getStableKeys(dictObj, basePath).forEach(subKey => {
        const subVal = dictObj[subKey];
        const row = document.createElement('div');
        row.className = 'sp-dict-row';
        const subPath = [...basePath, subKey];

        const dKey = document.createElement('span');
        dKey.className = 'sp-dict-key';
        dKey.textContent = subKey;
        dKey.title = 'Double-click to edit key';
        dKey.dataset.path = JSON.stringify(basePath);
        dKey.dataset.oldKey = subKey;
        dKey.ondblclick = function() { startKeyEdit(this); };
        row.appendChild(dKey);

        const dVal = document.createElement('span');
        dVal.className = 'sp-dict-value';
        if (typeof subVal === 'string') {
            dVal.textContent = formatDisplayValue(subVal);
            if (subVal === '') {
                dVal.dataset.emptyPlaceholder = '1';
            } else {
                delete dVal.dataset.emptyPlaceholder;
            }
        } else {
            dVal.textContent = JSON.stringify(subVal);
            delete dVal.dataset.emptyPlaceholder;
        }
        dVal.title = 'Double-click to edit';
        dVal.dataset.path = JSON.stringify(subPath);
        dVal.ondblclick = function() { startFieldEdit(this); };
        row.appendChild(dVal);

        // Delete sub-key button
        const delSub = document.createElement('button');
        delSub.className = 'sp-dict-del';
        delSub.textContent = '✕';
        delSub.title = 'Remove';
        delSub.onclick = function(e) { e.stopPropagation(); deleteSPField(subPath); };
        row.appendChild(delSub);

        block.appendChild(row);
    });

    // Inline add row for dict — 字典内只Add key-value(字符串)
    const addRow = document.createElement('div');
    addRow.className = 'sp-dict-add-row';
    const addBtnInline = document.createElement('button');
    addBtnInline.className = 'sp-inline-add-btn';
    addBtnInline.textContent = '+';
    addBtnInline.title = 'Add key-value';
    addBtnInline.onclick = function(e) {
        e.stopPropagation();
        promptAddDictEntry(basePath, addBtnInline);
    };
    addRow.appendChild(addBtnInline);
    block.appendChild(addRow);

    field.appendChild(block);

    const actions = document.createElement('div');
    actions.className = 'sp-field-actions';
    const delBtn = document.createElement('button');
    delBtn.className = 'sp-field-btn';
    delBtn.textContent = '✕';
    delBtn.title = 'Remove';
    delBtn.onclick = function() { deleteSPField(basePath); };
    actions.appendChild(delBtn);
    field.appendChild(actions);

    return field;
}

function createRelationshipsField(relArr) {
    const field = document.createElement('div');
    field.className = 'sp-field';

    const keySpan = document.createElement('span');
    keySpan.className = 'sp-field-key';
    keySpan.textContent = 'relationships';
    field.appendChild(keySpan);

    const list = document.createElement('ul');
    list.className = 'sp-rel-list';
    relArr.forEach((rel, i) => {
        const li = document.createElement('li');
        li.className = 'sp-rel-item';

        const bullet = document.createElement('span');
        bullet.className = 'sp-rel-bullet';
        bullet.textContent = '•';
        li.appendChild(bullet);

        const text = document.createElement('span');
        text.className = 'sp-rel-text';
        text.textContent = rel;
        text.title = 'Double-click to edit';
        text.dataset.path = JSON.stringify(['relationships', i]);
        text.ondblclick = function() { startFieldEdit(this); };
        li.appendChild(text);

        list.appendChild(li);
    });
    field.appendChild(list);

    const actions = document.createElement('div');
    actions.className = 'sp-field-actions';
    const delBtn = document.createElement('button');
    delBtn.className = 'sp-field-btn';
    delBtn.textContent = '✕';
    delBtn.title = 'Remove';
    delBtn.onclick = function() { deleteSPField(['relationships']); };
    actions.appendChild(delBtn);
    field.appendChild(actions);

    return field;
}

function createElementCard(elem, idx, basePath, isScene) {
    // 使用 ID 稳定颜色映射
    const color = getColorForElemId(elem.id);
    // 动态查找 globalIdx,确保即使 parsedElements 延迟填充也能正确工作
    const elemId = elem.id;
    function _getGlobalIdx() { return findParsedElementIndex(elemId, isScene); }

    const card = document.createElement('div');
    card.className = 'sp-element';
    card.dataset.elemId = elem.id;

    // Header
    const header = document.createElement('div');
    header.className = 'sp-element-header';

    const colorDot = document.createElement('span');
    colorDot.className = 'sp-element-color';
    colorDot.style.background = color;
    header.appendChild(colorDot);

    const idSpan = document.createElement('span');
    idSpan.className = 'sp-element-id';
    idSpan.textContent = 'ID:' + elem.id;
    header.appendChild(idSpan);

    const captionSpan = document.createElement('span');
    captionSpan.className = 'sp-element-caption';
    captionSpan.textContent = elem.caption || '';
    header.appendChild(captionSpan);

    const elemActions = document.createElement('div');
    elemActions.className = 'sp-element-actions';

    const elemAddBtn = document.createElement('button');
    elemAddBtn.className = 'sp-field-btn sp-elem-add-btn';
    elemAddBtn.textContent = '+';
    elemAddBtn.title = 'Add field';
    elemAddBtn.addEventListener('click', function(e) {
        e.stopPropagation();
        e.preventDefault();
        showAddDropdown(elemAddBtn, basePath);
    });
    elemActions.appendChild(elemAddBtn);

    const elemDelBtn = document.createElement('button');
    elemDelBtn.className = 'sp-field-btn';
    elemDelBtn.textContent = '✕';
    elemDelBtn.title = 'Remove element';
    elemDelBtn.addEventListener('click', function(e) {
        e.stopPropagation();
        e.preventDefault();
        deleteSPField(basePath);
    });
    elemActions.appendChild(elemDelBtn);

    header.appendChild(elemActions);

    header.addEventListener('click', function(e) {
        if (e.target.closest('.sp-field-btn')) return;
        card.classList.toggle('collapsed');
        // 点击 element → 立即显示对应 bbox
        focusBbox(_getGlobalIdx());
    });

    // Mouse enter => 只显示该 element 的 bbox(hover 预览),保存之前状态
    card.addEventListener('mouseenter', () => {
        const gIdx = _getGlobalIdx();
        if (!state.currentImage || gIdx < 0) return;
        card._savedBboxState = {
            bboxVisible: state.bboxVisible,
            activeBboxIndex: state.activeBboxIndex,
            focusedBboxIndex: state.focusedBboxIndex,
            bboxEditIndex: state.bboxEditIndex,
        };
        state.activeBboxIndex = gIdx;
        state.bboxEditIndex = -1;
        state.bboxVisible = true;
        renderBboxes(gIdx);
        updateBboxButtons();
        document.querySelectorAll('.sp-element').forEach(el => el.classList.remove('highlight'));
        card.classList.add('highlight');
    });
    card.addEventListener('mouseleave', () => {
        if (!state.currentImage) return;
        const saved = card._savedBboxState;
        card._savedBboxState = null;
        if (!saved) return;
        // 如果 hover 期间用户点击锁定了新的 bbox,保持新状态
        if (state.focusedBboxIndex >= 0 && state.focusedBboxIndex !== saved.focusedBboxIndex) {
            state.activeBboxIndex = state.focusedBboxIndex;
            state.bboxEditIndex = state.bboxEditIndex; // 保持编辑状态
            renderBboxes(state.focusedBboxIndex);
            updateBboxButtons();
            document.querySelectorAll('.sp-element').forEach(el => el.classList.remove('highlight'));
            if (state.parsedElements[state.focusedBboxIndex]) {
                const fc = document.querySelector(`.sp-element[data-elem-id="${state.parsedElements[state.focusedBboxIndex].id}"]`);
                if (fc) fc.classList.add('highlight');
            }
            return;
        }
        // 恢复之前的状态
        state.bboxVisible = saved.bboxVisible;
        state.activeBboxIndex = saved.activeBboxIndex;
        state.focusedBboxIndex = saved.focusedBboxIndex;
        state.bboxEditIndex = saved.bboxEditIndex;
        if (saved.bboxVisible) {
            if (saved.focusedBboxIndex >= 0) {
                renderBboxes(saved.focusedBboxIndex);
            } else {
                renderBboxes(-1);
            }
        } else {
            clearBboxes();
        }
            updateBboxButtons();
            document.querySelectorAll('.sp-element').forEach(el => el.classList.remove('highlight'));
        if (saved.focusedBboxIndex >= 0 && state.parsedElements[saved.focusedBboxIndex]) {
            const fc = document.querySelector(`.sp-element[data-elem-id="${state.parsedElements[saved.focusedBboxIndex].id}"]`);
            if (fc) fc.classList.add('highlight');
        }
    });

    card.appendChild(header);

    // Body
    const body = document.createElement('div');
    body.className = 'sp-element-body';

    const skipKeys = ['id'];
    getStableKeys(elem, basePath).forEach(key => {
        if (skipKeys.includes(key)) return;
        const val = elem[key];
        const fieldPath = [...basePath, key];

        if (key === 'position') {
            const posField = document.createElement('div');
            posField.className = 'sp-field';
            const posKeySpan = document.createElement('span');
            posKeySpan.className = 'sp-field-key';
            posKeySpan.textContent = 'position';
            posField.appendChild(posKeySpan);
            const posBadge = document.createElement('span');
            posBadge.className = 'sp-pos-badge';
            posBadge.title = 'Click to locate';
            posBadge.innerHTML = '<span class="pos-icon">📍</span> ' + escapeHtml(extractBboxText(val));
            posBadge.onclick = function(ev) { ev.stopPropagation(); focusBbox(_getGlobalIdx()); };
            posField.appendChild(posBadge);
            // 编辑按钮 — 进入 bbox 编辑模式(可拖拽/缩放)
            const editBtn = document.createElement('button');
            editBtn.className = 'sp-field-btn sp-bbox-edit-btn';
            editBtn.textContent = '✏️';
            editBtn.title = 'Edit bbox position';
            editBtn.onclick = function(ev) {
                ev.stopPropagation();
                const gIdx = _getGlobalIdx();
                if (gIdx < 0) {
                    showToast("BBox data isn't ready yet, please retry", 'warning');
                    return;
                }
                activateBboxEdit(gIdx);
            };
            posField.appendChild(editBtn);
            body.appendChild(posField);
        } else if (typeof val === 'object' && val !== null && !Array.isArray(val)) {
            body.appendChild(createDictField(key, val, fieldPath));
            } else {
            body.appendChild(createStringField(key, typeof val === 'string' ? val : JSON.stringify(val), fieldPath));
        }
    });

    card.appendChild(body);
    return card;
}

function extractBboxText(posStr) {
    if (!posStr) return '';
    const match = posStr.match(/<bbox>([^<]+)<\/bbox>/);
    return match ? match[1].trim() : posStr;
}

function findParsedElementIndex(elemId, isScene) {
    // Find element index in state.parsedElements by id
    for (let i = 0; i < state.parsedElements.length; i++) {
        if (state.parsedElements[i].id === elemId) return i;
    }
    return -1;
}

function highlightBbox(globalIdx) {
    state.activeBboxIndex = globalIdx;
    // Update bbox visual highlighting (使用 data-elemIndex 匹配)
    const container = document.getElementById('bboxContainer');
    container.querySelectorAll('.bbox-box').forEach(box => {
        const idx = parseInt(box.dataset.elemIndex);
        box.classList.remove('active', 'focused');
        if (idx === globalIdx) {
            if (state.bboxEditIndex === globalIdx) {
            box.classList.add('active');
        } else {
                box.classList.add('focused');
            }
        }
    });
    // Also highlight SP element card
    document.querySelectorAll('.sp-element').forEach(el => el.classList.remove('highlight'));
    if (globalIdx >= 0 && state.parsedElements[globalIdx]) {
        const card = document.querySelector(`.sp-element[data-elem-id="${state.parsedElements[globalIdx].id}"]`);
        if (card) card.classList.add('highlight');
    }
}

function focusBbox(globalIdx) {
    if (globalIdx < 0 || !state.currentImage) return;
    // 点击 element => 锁定只显示该 element 的 bbox(仅查看,不带编辑手柄)
    state.focusedBboxIndex = globalIdx;
    state.activeBboxIndex = globalIdx;
    state.bboxEditIndex = -1;
    state.bboxVisible = true;
    renderBboxes(globalIdx);
    updateBboxButtons();
    // highlight SP card
    document.querySelectorAll('.sp-element').forEach(el => el.classList.remove('highlight'));
    if (state.parsedElements[globalIdx]) {
        const card = document.querySelector(`.sp-element[data-elem-id="${state.parsedElements[globalIdx].id}"]`);
        if (card) card.classList.add('highlight');
    }
}

function activateBboxEdit(globalIdx) {
    if (globalIdx < 0 || !state.currentImage) return;
    // 双击 => 激活 bbox 编辑模式(显示拖拽/缩放手柄)
    state.focusedBboxIndex = globalIdx;
    state.activeBboxIndex = globalIdx;
    state.bboxEditIndex = globalIdx;
    state.bboxVisible = true;
    renderBboxes(globalIdx);
    updateBboxButtons();
    // highlight SP card
    document.querySelectorAll('.sp-element').forEach(el => el.classList.remove('highlight'));
    if (state.parsedElements[globalIdx]) {
        const card = document.querySelector(`.sp-element[data-elem-id="${state.parsedElements[globalIdx].id}"]`);
        if (card) card.classList.add('highlight');
    }
    showToast('BBox edit mode: drag or resize', 'info');
}

// ==================== SP Field Editing ====================
function startFieldEdit(el) {
    if (el.contentEditable === 'true') return;
    if (el.dataset.emptyPlaceholder === '1') {
        el.textContent = '';
        delete el.dataset.emptyPlaceholder;
    }
    el.contentEditable = 'true';
    el.focus();

    // Select all text
    const range = document.createRange();
    range.selectNodeContents(el);
    const sel = window.getSelection();
    sel.removeAllRanges();
    sel.addRange(range);

    // Save on blur or Enter
    const finish = () => {
        el.contentEditable = 'false';
        el.removeEventListener('blur', onBlur);
        el.removeEventListener('keydown', onKey);
        commitFieldEdit(el);
    };

    const onBlur = () => finish();
    const onKey = (e) => {
        if (e.key === 'Enter' && !e.shiftKey) {
            e.preventDefault();
            finish();
        }
        if (e.key === 'Escape') {
            el.contentEditable = 'false';
            el.removeEventListener('blur', onBlur);
            el.removeEventListener('keydown', onKey);
            // Revert — re-render
            renderSPStructured();
        }
    };

    el.addEventListener('blur', onBlur);
    el.addEventListener('keydown', onKey);
}

function commitFieldEdit(el) {
    const path = JSON.parse(el.dataset.path);
    const newValue = el.textContent.trim();
    if (!state.parsedSP) return;

    pushSPUndo();

    // Navigate to parent and set value
    let obj = state.parsedSP;
    for (let i = 0; i < path.length - 1; i++) {
        obj = obj[path[i]];
        if (!obj) return;
    }
    const lastKey = path[path.length - 1];
    obj[lastKey] = newValue;

    // Sync back to state
    state.structuredPrompt = stableJSONStringify(state.parsedSP, state.structuredPrompt);
    parseSPAndUpdateBboxes();
    renderSPStructured();
}

// ==================== Key Editing ====================
function startKeyEdit(el) {
    if (el.contentEditable === 'true') return;
    el.contentEditable = 'true';
    el.focus();

    const range = document.createRange();
    range.selectNodeContents(el);
    const sel = window.getSelection();
    sel.removeAllRanges();
    sel.addRange(range);

    const finish = () => {
        el.contentEditable = 'false';
        el.removeEventListener('blur', onBlur);
        el.removeEventListener('keydown', onKey);
        commitKeyEdit(el);
    };

    const onBlur = () => finish();
    const onKey = (e) => {
        if (e.key === 'Enter' && !e.shiftKey) {
            e.preventDefault();
            finish();
        }
        if (e.key === 'Escape') {
            el.contentEditable = 'false';
            el.removeEventListener('blur', onBlur);
            el.removeEventListener('keydown', onKey);
            renderSPStructured();
        }
    };

    el.addEventListener('blur', onBlur);
    el.addEventListener('keydown', onKey);
}

function commitKeyEdit(el) {
    const oldKey = el.dataset.oldKey;
    const newKey = el.textContent.trim();
    if (!newKey || newKey === oldKey || !state.parsedSP) return;

    const parentPath = JSON.parse(el.dataset.path);

    pushSPUndo();

    // Navigate to parent
    let obj = state.parsedSP;
    for (let i = 0; i < parentPath.length; i++) {
        obj = obj[parentPath[i]];
        if (!obj) return;
    }

    if (typeof obj === 'object' && !Array.isArray(obj) && oldKey in obj) {
        // Rename: preserve order by rebuilding object
        const entries = Object.entries(obj);
        const newEntries = entries.map(([k, v]) => k === oldKey ? [newKey, v] : [k, v]);
        // Clear and repopulate
        for (const k of Object.keys(obj)) delete obj[k];
        for (const [k, v] of newEntries) obj[k] = v;
    }

    state.structuredPrompt = stableJSONStringify(state.parsedSP, state.structuredPrompt);
    parseSPAndUpdateBboxes();
    renderSPStructured();
    showToast(`Field renamed: ${oldKey}${newKey}`, 'success');
}

// ==================== Add Dropdown ====================
let _addDropdownCleanup = null;
let _addDropdownAnchor = null;
let _savedAnchorRect = null; // 保存按钮位置,防止 DOM 变化后丢失

function showSectionAddDropdown(anchorEl, sectionType) {
    hideAddDropdown();
    hideInputPopup();
    _addDropdownAnchor = anchorEl;
    _savedAnchorRect = anchorEl.getBoundingClientRect();
    const dd = document.getElementById('spAddDropdown');
    dd.innerHTML = '';

    const options = [];
    if (sectionType === 'global') {
        options.push({ label: 'Field', icon: '📝', action: () => promptAddField([], 'string', anchorEl) });
        options.push({ label: 'Dict field', icon: '📦', action: () => promptAddField([], 'dict', anchorEl) });
    } else if (sectionType === 'elements') {
        options.push({ label: 'Add element', icon: '🎯', action: () => addSPElement('') });
    } else if (sectionType === 'scene') {
        options.push({ label: 'Field', icon: '📝', action: () => promptAddField(['scene'], 'string', anchorEl) });
        options.push({ label: 'Dict field', icon: '📦', action: () => promptAddField(['scene'], 'dict', anchorEl) });
        options.push({ label: 'Add element', icon: '🎯', action: () => addSPSceneElement('') });
    }

    options.forEach(opt => {
        const item = document.createElement('div');
        item.className = 'sp-add-option';
        item.innerHTML = `<span>${opt.icon}</span> ${opt.label}`;
        item.onclick = function(e) {
            e.stopPropagation();
            hideAddDropdown();
            opt.action();
        };
        dd.appendChild(item);
    });

    // Position near anchor
    const rect = anchorEl.getBoundingClientRect();
    dd.style.top = (rect.bottom + 4) + 'px';
    dd.style.left = Math.min(rect.left, window.innerWidth - 160) + 'px';
    dd.style.display = 'block';

    // Close on outside click
    _addDropdownCleanup = (e) => {
        if (!dd.contains(e.target) && e.target !== anchorEl) {
            hideAddDropdown();
        }
    };
    setTimeout(() => document.addEventListener('mousedown', _addDropdownCleanup), 0);
}

function showAddDropdown(anchorEl, basePath) {
    hideAddDropdown();
    hideInputPopup();
    _addDropdownAnchor = anchorEl;
    _savedAnchorRect = anchorEl.getBoundingClientRect();
    const dd = document.getElementById('spAddDropdown');
    dd.innerHTML = '';

    const options = [
        { label: 'Field', icon: '📝', type: 'string' },
        { label: 'Dict field', icon: '📦', type: 'dict' },
    ];

    options.forEach(opt => {
        const item = document.createElement('div');
        item.className = 'sp-add-option';
        item.innerHTML = `<span>${opt.icon}</span> ${opt.label}`;
        item.onclick = function(e) {
            e.stopPropagation();
            const savedAnchor = anchorEl;  // capture before hiding
            hideAddDropdown();
            promptAddField(basePath, opt.type, savedAnchor);
        };
        dd.appendChild(item);
    });

    // Position near anchor
    const rect = anchorEl.getBoundingClientRect();
    dd.style.top = (rect.bottom + 4) + 'px';
    dd.style.left = Math.min(rect.left, window.innerWidth - 160) + 'px';
    dd.style.display = 'block';

    // Close on outside click
    _addDropdownCleanup = (e) => {
        if (!dd.contains(e.target) && e.target !== anchorEl) {
            hideAddDropdown();
        }
    };
    setTimeout(() => document.addEventListener('mousedown', _addDropdownCleanup), 0);
}

function hideAddDropdown() {
    const dd = document.getElementById('spAddDropdown');
    if (dd) dd.style.display = 'none';
    if (_addDropdownCleanup) {
        document.removeEventListener('mousedown', _addDropdownCleanup);
        _addDropdownCleanup = null;
    }
}

// ==================== Floating Input Popup ====================
let _inputPopupEl = null;
let _inputPopupCleanup = null;
let _lastPopupAnchor = null;

function resolvePopupAnchor(anchorEl) {
    const candidates = [anchorEl, _addDropdownAnchor, _lastPopupAnchor];
    for (const candidate of candidates) {
        if (!candidate || !document.body.contains(candidate)) continue;
        const rect = candidate.getBoundingClientRect();
        if (rect.width > 0 || rect.height > 0) return candidate;
    }
    return null;
}

function showInputPopup(anchorEl, title, fields, onSubmit) {
    hideInputPopup();
    hideAddDropdown();
    const resolvedAnchor = resolvePopupAnchor(anchorEl);

    const popup = document.createElement('div');
    popup.className = 'sp-input-popup';

    // Title
    const titleEl = document.createElement('div');
    titleEl.className = 'popup-title';
    titleEl.textContent = title;
    popup.appendChild(titleEl);

    // Fields
    const inputs = {};
    fields.forEach(f => {
        const fieldDiv = document.createElement('div');
        fieldDiv.className = 'popup-field';

        const label = document.createElement('label');
        label.textContent = f.label;
        fieldDiv.appendChild(label);

        const input = document.createElement('input');
        input.type = 'text';
        input.placeholder = f.placeholder || '';
        input.dataset.fieldKey = f.key;
        input.addEventListener('input', () => {
            input.classList.remove('input-error');
        });
        fieldDiv.appendChild(input);

        inputs[f.key] = input;
        popup.appendChild(fieldDiv);
    });

    // Actions
    const actions = document.createElement('div');
    actions.className = 'popup-actions';

    const cancelBtn = document.createElement('button');
    cancelBtn.className = 'popup-btn';
    cancelBtn.textContent = 'Cancel';
    cancelBtn.onclick = (e) => { e.stopPropagation(); hideInputPopup(); };
    actions.appendChild(cancelBtn);

    const okBtn = document.createElement('button');
    okBtn.className = 'popup-btn popup-btn-primary';
    okBtn.textContent = 'OK';
    okBtn.onclick = (e) => {
        e.stopPropagation();
        const values = {};
        let valid = true;
        fields.forEach(f => {
            values[f.key] = inputs[f.key].value.trim();
            if (f.required && !values[f.key]) {
                inputs[f.key].classList.add('input-error');
                valid = false;
            }
        });
        if (!valid) return;
        hideInputPopup();
        onSubmit(values);
    };
    actions.appendChild(okBtn);
    popup.appendChild(actions);

    // Position near anchor (with viewport clamping and fallback)
    document.body.appendChild(popup);
    _inputPopupEl = popup;

    // 优先用 resolvedAnchor 的实时位置,否则用之前保存的 _savedAnchorRect
    let rect;
    if (resolvedAnchor) {
        rect = resolvedAnchor.getBoundingClientRect();
    } else if (_savedAnchorRect && (_savedAnchorRect.width > 0 || _savedAnchorRect.height > 0)) {
        rect = _savedAnchorRect;
    } else {
        rect = { top: 0, left: 0, width: 0, height: 0, bottom: 0 };
    }
    const popupRect = popup.getBoundingClientRect();
    const popupW = popupRect.width || 260;
    const popupH = popupRect.height || 150;

    let top, left;
    // If anchor has zero size (detached from DOM or hidden), center in viewport
    if (rect.width === 0 && rect.height === 0 && rect.top === 0 && rect.left === 0) {
        top = Math.max(10, (window.innerHeight - popupH) / 2);
        left = Math.max(10, (window.innerWidth - popupW) / 2);
    } else {
        top = rect.bottom + 4;
        left = rect.left;
    }

    // Clamp to viewport bounds
    if (top + popupH > window.innerHeight - 10) {
        top = Math.max(10, rect.top - popupH - 4);
    }
    if (left + popupW > window.innerWidth - 10) {
        left = Math.max(10, window.innerWidth - popupW - 10);
    }
    if (top < 10) top = 10;
    if (left < 10) left = 10;

    popup.style.top = top + 'px';
    popup.style.left = left + 'px';

    // Focus first input
    const firstInput = popup.querySelector('input');
    if (firstInput) setTimeout(() => firstInput.focus(), 50);

    // Keyboard shortcuts
    popup.addEventListener('keydown', (e) => {
        if (e.key === 'Enter') {
            e.preventDefault();
            okBtn.click();
        }
        if (e.key === 'Escape') {
            hideInputPopup();
        }
    });

    // Close on outside click
    _inputPopupCleanup = (e) => {
        if (!popup.contains(e.target) && (!resolvedAnchor || e.target !== resolvedAnchor)) {
            hideInputPopup();
        }
    };
    setTimeout(() => document.addEventListener('mousedown', _inputPopupCleanup), 0);
}

function hideInputPopup() {
    if (_inputPopupEl) {
        _inputPopupEl.remove();
        _inputPopupEl = null;
    }
    if (_inputPopupCleanup) {
        document.removeEventListener('mousedown', _inputPopupCleanup);
        _inputPopupCleanup = null;
    }
}

// 弹出悬浮输入框让用户填写 key 和 value
function promptAddField(basePath, type, anchorOverride) {
    const anchor = anchorOverride || _addDropdownAnchor || document.body;
    _lastPopupAnchor = anchor;
    if (type === 'string') {
        showInputPopup(anchor, 'Add field', [
            { key: 'fieldKey', label: 'Key', placeholder: 'Field name', required: true },
            { key: 'fieldValue', label: 'Value', placeholder: 'Field value', required: false },
        ], (values) => {
            addFieldToPath(basePath, 'string', values.fieldKey, values.fieldValue);
        });
    } else if (type === 'dict') {
        showInputPopup(anchor, 'Add dict field', [
            { key: 'dictName', label: 'Dict name', placeholder: 'Dict name', required: true },
            { key: 'innerKey', label: 'Inner key', placeholder: 'key', required: true },
            { key: 'innerValue', label: 'Inner value', placeholder: 'value', required: false },
        ], (values) => {
            addFieldToPath(basePath, 'dict', values.dictName, null, values.innerKey, values.innerValue);
        });
    }
}

// 直接向字典内Add一对 key-value(通过悬浮输入框)
function promptAddDictEntry(basePath, anchorEl) {
    _lastPopupAnchor = anchorEl || document.body;
    if (anchorEl) _savedAnchorRect = anchorEl.getBoundingClientRect();
    showInputPopup(anchorEl || document.body, 'Add field', [
        { key: 'fieldKey', label: 'Key', placeholder: 'key', required: true },
        { key: 'fieldValue', label: 'Value', placeholder: 'value', required: false },
    ], (values) => {
        addFieldToPath(basePath, 'string', values.fieldKey, values.fieldValue);
    });
}

function addFieldToPath(basePath, type, fieldKey, fieldValue, innerKey, innerValue) {
    if (!state.parsedSP) return;

    // Navigate to target object
    let target = state.parsedSP;
    for (const key of basePath) {
        if (target[key] === undefined) target[key] = {};
        target = target[key];
    }

    if (typeof target !== 'object' || Array.isArray(target)) {
        showToast("Can't add a field here", 'warning');
        return;
    }

    if (fieldKey in target) {
        showToast(`Field "${fieldKey}" already exists`, 'warning');
        return;
    }

    pushSPUndo();

    if (type === 'dict') {
        // 创建字典,带初始 key-value
        const dictObj = {};
        if (innerKey) dictObj[innerKey] = innerValue || '';
        target[fieldKey] = dictObj;
    } else {
        target[fieldKey] = fieldValue || '';
    }

    state.structuredPrompt = stableJSONStringify(state.parsedSP, state.structuredPrompt);
    parseSPAndUpdateBboxes();
    updateSPPanel();
    const isEmptyValue = type === 'dict' ? (!innerValue && !!innerKey) : !fieldValue;
    const emptySuffix = isEmptyValue ? ' (empty value)' : '';
    showToast(`Added ${type === 'dict' ? 'dict ' : ''}field: ${fieldKey}${emptySuffix}`, 'success');
}

// ==================== Section Clear ====================
function clearSectionFields(sectionType) {
    if (!state.parsedSP) return;
    pushSPUndo();

    if (sectionType === 'global') {
        // Remove all top-level keys except elements and scene
        Object.keys(state.parsedSP).forEach(key => {
            if (key !== 'elements' && key !== 'scene') {
                delete state.parsedSP[key];
            }
        });
        showToast('Cleared Global fields', 'info');
    } else if (sectionType === 'elements') {
        // Remove整个 elements key
        delete state.parsedSP.elements;
        showToast('Removed Elements', 'info');
    } else if (sectionType === 'scene') {
        // Remove整个 scene key
        delete state.parsedSP.scene;
        showToast('Removed Scene', 'info');
    }

    state.structuredPrompt = stableJSONStringify(state.parsedSP, state.structuredPrompt);
    parseSPAndUpdateBboxes();
    updateSPPanel();
}

function addSPElement(caption) {
    if (!state.parsedSP) return;
    if (!state.parsedSP.elements) state.parsedSP.elements = [];

    pushSPUndo();

    // 自动递增 ID
    let maxId = 0;
    state.parsedSP.elements.forEach(e => {
        const id = parseInt(e.id) || 0;
        if (id > maxId) maxId = id;
    });
    if (state.parsedSP.scene && state.parsedSP.scene.elements) {
        state.parsedSP.scene.elements.forEach(e => {
            const id = parseInt(e.id) || 0;
            if (id > maxId) maxId = id;
        });
    }

    const newElem = {
        id: maxId + 1,
        caption: caption || '',
        position: '<bbox>0 0 500 500</bbox>',
    };

    state.parsedSP.elements.push(newElem);
    state.structuredPrompt = stableJSONStringify(state.parsedSP, state.structuredPrompt);
    parseSPAndUpdateBboxes();
    updateSPPanel();
    showToast(`Added element (id: ${newElem.id})`, 'success');
}

function addSPSceneElement(caption) {
    if (!state.parsedSP) return;
    if (!state.parsedSP.scene) state.parsedSP.scene = {};
    if (!state.parsedSP.scene.elements) state.parsedSP.scene.elements = [];

    pushSPUndo();

    let maxId = 0;
    if (state.parsedSP.elements) {
        state.parsedSP.elements.forEach(e => {
            const id = parseInt(e.id) || 0;
            if (id > maxId) maxId = id;
        });
    }
    state.parsedSP.scene.elements.forEach(e => {
        const id = parseInt(e.id) || 0;
        if (id > maxId) maxId = id;
    });

    const newElem = {
        id: maxId + 1,
        caption: caption || '',
        position: '<bbox>0 0 500 500</bbox>',
    };

    state.parsedSP.scene.elements.push(newElem);
    state.structuredPrompt = stableJSONStringify(state.parsedSP, state.structuredPrompt);
    parseSPAndUpdateBboxes();
    updateSPPanel();
    showToast(`Added scene element (id: ${newElem.id})`, 'success');
}

// ==================== Deleting Fields ====================
function deleteSPField(path) {
    if (!state.parsedSP) return;
    if (typeof path === 'string') path = JSON.parse(path);

    pushSPUndo();

    let obj = state.parsedSP;
    for (let i = 0; i < path.length - 1; i++) {
        obj = obj[path[i]];
        if (!obj) return;
    }
    const lastKey = path[path.length - 1];

    if (Array.isArray(obj)) {
        obj.splice(lastKey, 1);
    } else {
        delete obj[lastKey];
    }

    state.structuredPrompt = stableJSONStringify(state.parsedSP, state.structuredPrompt);
    parseSPAndUpdateBboxes();
    updateSPPanel();
    showToast('Removed', 'info');
}

// ==================== Generation Logic (per DEFINITION.md) ====================

/**
 * Generate SP按钮逻辑:
 * - 当Image不存在时: User Prompt => SP (Text System Prompt)
 * - 当Image存在时:
 *   - H&W会被覆盖
 *   - 没有User Prompt => 根据图像Generate SP (Image System Prompt)
 *   - 只有User Prompt (无SP) => 根据图像Generate SP (Image System Prompt)
 *   - 有User Prompt和SP => 根据User Prompt修改SP (Editing System Prompt)
 */
async function handleGenerateSP() {
    if (state.isGenerating) return;

    const hasImage = !!state.currentImage;
    const hasUserPrompt = !!state.userPrompt.trim();
    const hasSP = !!state.structuredPrompt.trim();

    if (!hasImage) {
        // ---- Text → SP ----
        if (!hasUserPrompt) {
            showToast('Please enter a User Prompt', 'warning');
                return;
            }
            
        setGenerating(true, 'sp');
        showToast('Generating Structured Prompt from User Prompt...', 'info');

        try {
            const result = await callLLMForSP({
                type: 'text',
                userPrompt: state.userPrompt,
            });
            if (result) {
                setSPContent(result);
                showToast('Structured Prompt generated', 'success');
            }
            addHistoryEntry('sp');
        } catch (err) {
            showToast('Generation failed: ' + err.message, 'error');
        } finally {
            setGenerating(false);
        }
    } else {
        // ---- Image exists ----
        autoUpdateHW();  // H&W被覆盖

        if (hasUserPrompt && hasSP) {
            // Case 3: 有 User Prompt 和 SP → 修改 SP
            setGenerating(true, 'sp');
            showToast('Editing Structured Prompt from User Prompt...', 'info');

            try {
                const result = await callLLMForSP({
                    type: 'edit',
                    image: state.currentImage.src,
                    userPrompt: state.userPrompt,
                    structuredPrompt: state.structuredPrompt,
                });
                if (result) {
                    setSPContent(result);
                    showToast('Structured Prompt updated', 'success');
                }
                addHistoryEntry('sp');
            } catch (err) {
                showToast('Edit failed: ' + err.message, 'error');
            } finally {
                setGenerating(false);
            }
        } else {
            // Case 1 & 2: 根据图像生成 SP
            setGenerating(true, 'sp');
            showToast('Generating Structured Prompt from image...', 'info');

            try {
                const result = await callLLMForSP({
                    type: 'image',
                    image: state.currentImage.src,
                });
                if (result) {
                    setSPContent(result);
                    showToast('Structured Prompt generated', 'success');
                }
                addHistoryEntry('sp');
            } catch (err) {
                showToast('Generation failed: ' + err.message, 'error');
            } finally {
                setGenerating(false);
            }
        }
    }
}

/**
 * Generate Image按钮逻辑:
 * - 当Image不存在时 (T2I):
 *   - 有SP => 根据SP生成
 *   - 无SP => 根据User Prompt生成
 * - 当Image存在时 ((I+T)2I):
 *   - 有SP => 根据SP修改图像
 *   - 无SP => 根据User Prompt修改图像
 *
 * 生成前: 与上轮对比 User & SP,高亮修改,点击确认才提交
 */
async function handleGenerateImage() {
    if (state.isGenerating) return;

    const hasImage = !!state.currentImage && !state.params.forceT2I;
    const hasUserPrompt = !!state.userPrompt.trim();
    const hasSP = !!state.structuredPrompt.trim();

    if (!hasSP && !hasUserPrompt) {
        showToast('Please enter a User Prompt or Structured Prompt', 'warning');
                return;
            }
            
    syncParamsFromUI();

    let prompt, message;

    if (!hasImage) {
        // T2I
        if (hasSP) {
            prompt = state.structuredPrompt;
            message = 'Generating image from Structured Prompt...';
            } else {
            prompt = state.userPrompt;
            message = 'Generating image from User Prompt...';
        }
    } else {
        // (I+T)2I
        if (hasSP) {
            prompt = state.structuredPrompt;
            message = 'Editing image with Structured Prompt...';
        } else {
            prompt = state.userPrompt;
            message = 'Editing image with User Prompt...';
        }
    }

    // 如果有历史记录,展示 diff 对比
    if (state.history.length > 0) {
        const lastEntry = state.history[state.history.length - 1];
        const upChanged = (lastEntry.userPrompt || '') !== (state.userPrompt || '');
        const spChanged = (lastEntry.structuredPrompt || '') !== (state.structuredPrompt || '');

        if (upChanged || spChanged) {
            showDiffDialog(lastEntry, () => {
                doGenerateImage(prompt, message, hasImage);
            });
            return;
        }
    }

    doGenerateImage(prompt, message, hasImage);
}

async function doGenerateImage(prompt, message, hasImage) {
    setGenerating(true, 'image');
    showToast(message, 'info');

    try {
        const result = await callDiTForImage({
            type: hasImage ? 'i2i' : 't2i',
            prompt: prompt,
            image: hasImage ? state.currentImage.src : null,
            params: { ...state.params },
        });

        if (result) {
            // result: image data URL or URL — 替换当前图片
            const img = new Image();
            img.onload = function () {
                state.currentImage = {
                    src: result,
                    width: img.naturalWidth,
                    height: img.naturalHeight,
                };
                updateImagePanel();
                addHistoryEntry('image');
                showToast('Image generated', 'success');
            };
            img.onerror = function () {
                showToast('Failed to load generated image', 'error');
            };
            img.src = result;
            } else {
            // DiT 未实现时: 直接将当前状态存入历史(Debug 模式)
            addHistoryEntry('image');
            showToast('Current state saved to history (debug)', 'success');
        }
    } catch (err) {
        showToast('Image generation failed: ' + err.message, 'error');
    } finally {
        setGenerating(false);
    }
}

function setGenerating(isGenerating, type) {
    state.isGenerating = isGenerating;
    const spBtn = document.getElementById('genSPBtn');
    const imgBtn = document.getElementById('genImageBtn');

    if (isGenerating) {
        spBtn.disabled = true;
        imgBtn.disabled = true;
        if (type === 'sp') {
            spBtn.classList.add('loading');
            spBtn.innerHTML = '<span class="spinner"></span>Generating...';
        } else if (type === 'image') {
            imgBtn.classList.add('loading');
            imgBtn.innerHTML = '<span class="spinner"></span>Generating...';
        }
            } else {
        spBtn.disabled = false;
        imgBtn.disabled = false;
        spBtn.classList.remove('loading');
        imgBtn.classList.remove('loading');
        spBtn.textContent = 'Generate SP';
        imgBtn.textContent = 'Generate Image';
    }
}

// ==================== Stub Functions (To Be Implemented) ====================

/**
 * 调用 LLM 生成 / 修改 Structured Prompt
 *
 * @param {Object} options
 * @param {string} options.type - 'text' | 'image' | 'edit'
 *
 * type='text': 纯文本 → SP
 *   options.userPrompt: 用户输入
 *   使用 Text System Prompt (DEFAULT_SYSTEM_PROMPT)
 *
 * type='image': 图片 → SP
 *   options.image: 图片 data URL
 *   使用 Image System Prompt
 *
 * type='edit': 根据指令修改 SP
 *   options.image: 图片 data URL
 *   options.userPrompt: 修改指令
 *   options.structuredPrompt: 当前 SP
 *   使用 Editing System Prompt
 *
 * @returns {string|null} 生成的 SP (formatted JSON string) 或 null
 */
async function callLLMForSP(options) {
    // Streams the PE model's tokens via SSE into the SP editor's raw
    // textarea (so the user watches the CoT + JSON appear live), then
    // once the `done` frame arrives, swaps in the extracted SP JSON.
    if (options.type !== 'text') {
        showToast('That mode (image / edit) is not supported yet — text → SP only', 'warning');
        return null;
    }
    const width  = (Math.round((state.params.width  || 1024) / 32) * 32) || 1024;
    const height = (Math.round((state.params.height || 1024) / 32) * 32) || 1024;

    // Prep the SP textarea for streaming — flip into raw mode, empty it.
    const spTextarea = document.getElementById('spTextarea');
    const spEmpty = document.getElementById('spEmpty');
    const spStructured = document.getElementById('spStructured');
    const prevRaw = state.spRawMode;
    state.spRawMode = true;
    if (spTextarea) {
        spTextarea.style.display = '';
        spTextarea.value = '';
    }
    if (spEmpty) spEmpty.style.display = 'none';
    if (spStructured) spStructured.style.display = 'none';

    try {
        const resp = await fetch('/api/generate_sp/stream', {
            method: 'POST',
            headers: { 'Content-Type': 'application/json' },
            body: JSON.stringify({
                user_prompt: options.userPrompt || '',
                width: width, height: height,
            }),
        });
        if (!resp.ok || !resp.body) {
            const errText = await resp.text().catch(() => resp.statusText);
            throw new Error(`HTTP ${resp.status}: ${errText}`);
        }

        const reader = resp.body.getReader();
        const decoder = new TextDecoder();
        let sseBuffer = '';
        let finalSp = null;
        let streamError = null;

        while (true) {
            const { value, done } = await reader.read();
            if (done) break;
            sseBuffer += decoder.decode(value, { stream: true });

            // Split on blank line — each block is one SSE event.
            let idx;
            while ((idx = sseBuffer.indexOf('\n\n')) !== -1) {
                const block = sseBuffer.slice(0, idx);
                sseBuffer = sseBuffer.slice(idx + 2);
                let evName = 'message', dataStr = '';
                for (const line of block.split('\n')) {
                    if (line.startsWith('event: ')) evName = line.slice(7).trim();
                    else if (line.startsWith('data: ')) dataStr += line.slice(6);
                }
                if (!dataStr) continue;
                let payload;
                try { payload = JSON.parse(dataStr); } catch { continue; }

                if (evName === 'token' && spTextarea && payload.text) {
                    spTextarea.value += payload.text;
                    spTextarea.scrollTop = spTextarea.scrollHeight;
                } else if (evName === 'waking') {
                    showToast(payload.msg || 'PE endpoint cold-starting…', 'info');
                } else if (evName === 'done') {
                    finalSp = payload.sp;
                } else if (evName === 'error') {
                    streamError = payload.error || 'unknown stream error';
                }
            }
        }

        if (streamError) throw new Error(streamError);
        if (finalSp == null) throw new Error('stream closed without result');

        // Restore raw-mode preference before returning; setSPContent will
        // populate the structured view.
        state.spRawMode = prevRaw;
        return typeof finalSp === 'string' ? finalSp : JSON.stringify(finalSp, null, 2);
    } catch (err) {
        console.error('[LLM] generate_sp stream failed:', err);
        state.spRawMode = prevRaw;
        showToast('SP generation failed: ' + err.message, 'error');
        return null;
    }
}

// ==================== DiT Generation (QwenImage Serving API) ====================

/**
 * 将 JSON 对象转为 compact single-quote 格式字符串。
 * 服务端 prompt 字段期望此格式的 Structured Prompt。
 */
function compactSingleQuoteJSON(data) {
    const PH_S = '@@SQ@@', PH_D = '@@DQ@@';
    const reS = /@@SQ@@/g, reD = /@@DQ@@/g;
    function protect(obj) {
        if (obj === null || obj === undefined) return obj;
        if (Array.isArray(obj)) return obj.map(protect);
        if (typeof obj === 'object') {
            const r = {};
            for (const [k, v] of Object.entries(obj)) {
                const pk = typeof k === 'string' ? k.replace(/'/g, PH_S).replace(/"/g, PH_D) : k;
                r[pk] = protect(v);
            }
            return r;
        }
        if (typeof obj === 'string') return obj.replace(/'/g, PH_S).replace(/"/g, PH_D);
        return obj;
    }
    const jsonStr = JSON.stringify(protect(data));
    return jsonStr.replace(/"/g, "'").replace(reS, "\\'").replace(reD, '"');
}

/**
 * Call the local FastAPI backend which runs the QwenImage DiT in-process.
 * See demo/backend/dit.py for the model wrapper.
 *
 * @param {Object} options
 * @param {string} options.prompt - text prompt (SP JSON string or plain NL)
 * @param {Object} options.params - { cfgScale, steps, height, width, seed }
 *
 * @returns {string|null} generated image data URL, or null on failure.
 */
async function callDiTForImage(options) {
    const { prompt, params } = options;

    const width  = (Math.round((params.width  || 1024) / 32) * 32) || 1024;
    const height = (Math.round((params.height || 1024) / 32) * 32) || 1024;

    // SP JSON → compact single-quote format expected by the DiT; plain text as-is.
    let formattedPrompt;
    try {
        const parsed = typeof prompt === 'string' ? JSON.parse(prompt) : prompt;
        formattedPrompt = compactSingleQuoteJSON(parsed);
    } catch (_) {
        formattedPrompt = prompt;
    }

    const body = {
        prompt: formattedPrompt,
        height: height,
        width: width,
        num_steps: params.steps || 25,
        seed: (params.seed != null && params.seed !== -1) ? params.seed : 42,
        cfg_scale: params.cfgScale || 4.0,
        negative_prompt: "",
    };

    try {
        // POST kicks off a background DiT job on the Space (bypasses HF's
        // 10-min HTTP/2 keep-alive cap — cold model load is 5min, first
        // generation another 40s). Then poll /api/task/{id}.
        const kickResp = await fetch('/api/generate_image_async', {
            method: 'POST',
            headers: { 'Content-Type': 'application/json' },
            body: JSON.stringify(body),
        });
        if (!kickResp.ok) {
            const errText = await kickResp.text().catch(() => kickResp.statusText);
            throw new Error(`HTTP ${kickResp.status}: ${errText}`);
        }
        const { task_id } = await kickResp.json();
        if (!task_id) throw new Error('Response missing task_id');

        const startedAt = Date.now();
        const timeoutMs = 20 * 60 * 1000;  // 20-min ceiling
        while (true) {
            if (Date.now() - startedAt > timeoutMs) {
                throw new Error('DiT job timed out after 20 minutes');
            }
            await simulateDelay(2000);
            let pollResp;
            try {
                pollResp = await fetch(`/api/task/${task_id}`);
            } catch (_) {
                continue;  // transient network hiccup — keep trying
            }
            if (!pollResp.ok) continue;
            const st = await pollResp.json();
            if (st.status === 'done' && st.image_base64) {
                return 'data:image/png;base64,' + st.image_base64;
            }
            if (st.status === 'failed') {
                throw new Error(st.error || 'DiT failed');
            }
            // status === 'running' — surface elapsed time on the button
            const elapsed = Math.round((Date.now() - startedAt) / 1000);
            const btn = document.getElementById('genImageBtn');
            if (btn) {
                const label = btn.querySelector('.btn-label') || btn;
                label.textContent = `Generating… ${elapsed}s`;
            }
        }
    } catch (err) {
        console.error('[DiT] generate_image failed:', err);
        showToast('Image generation failed: ' + err.message, 'error');
        return null;
    }
}

function simulateDelay(ms) {
    return new Promise(resolve => setTimeout(resolve, ms));
}

// ==================== Diff Dialog ====================
function showDiffDialog(lastEntry, onConfirm) {
    state.pendingGeneration = onConfirm;
    const diffContent = document.getElementById('diffContent');

    let html = '';

    // User Prompt diff
    html += '<div class="diff-section-title">User Prompt changes</div>';
    html += '<div class="diff-block">';
    html += generateDiffHTML(lastEntry.userPrompt || '', state.userPrompt || '');
    html += '</div>';

    // SP diff
    html += '<div class="diff-section-title">Structured Prompt changes</div>';
    html += '<div class="diff-block">';
    html += generateDiffHTML(lastEntry.structuredPrompt || '', state.structuredPrompt || '');
    html += '</div>';

    diffContent.innerHTML = html;
    document.getElementById('diffModal').classList.add('visible');
}

function generateDiffHTML(oldText, newText) {
    if (oldText === newText) {
        return '<div class="diff-no-change">No changes</div>';
    }

    const oldLines = oldText.split('\n');
    const newLines = newText.split('\n');
    let html = '';
    const maxLen = Math.max(oldLines.length, newLines.length);

    for (let i = 0; i < maxLen; i++) {
        const oldLine = i < oldLines.length ? oldLines[i] : undefined;
        const newLine = i < newLines.length ? newLines[i] : undefined;

        if (oldLine === newLine) {
            html += `<div class="diff-line diff-same">&nbsp;${escapeHtml(oldLine)}</div>`;
            } else {
            if (oldLine !== undefined) {
                html += `<div class="diff-line diff-removed">-${escapeHtml(oldLine)}</div>`;
            }
            if (newLine !== undefined) {
                html += `<div class="diff-line diff-added">+${escapeHtml(newLine)}</div>`;
            }
        }
    }

    return html;
}

function escapeHtml(text) {
    if (!text) return '';
    return text
        .replace(/&/g, '&amp;')
        .replace(/</g, '&lt;')
        .replace(/>/g, '&gt;')
        .replace(/"/g, '&quot;');
}

function cancelGeneration() {
    state.pendingGeneration = null;
    document.getElementById('diffModal').classList.remove('visible');
}

function confirmGeneration() {
    document.getElementById('diffModal').classList.remove('visible');
    if (state.pendingGeneration) {
        const fn = state.pendingGeneration;
        state.pendingGeneration = null;
        fn();
    }
}

// ==================== History ====================
function addHistoryEntry(type) {
    const entry = {
        type: type,                       // 'sp' or 'image'
        inputImage: state.currentImage ? state.currentImage.src : null,
        userPrompt: state.userPrompt || '',
        structuredPrompt: state.structuredPrompt || '',
        params: { ...state.params },
        timestamp: Date.now(),
        colorIndex: state.generationCount % HISTORY_COLORS.length,
    };

    state.history.push(entry);
    state.generationCount++;
    renderHistory();
}

function renderHistory() {
    const scroll = document.getElementById('historyScroll');
    const empty = document.getElementById('historyEmpty');
    const clearBtn = document.getElementById('historyClearBtn');

    // 先清除已有 DOM 元素
    scroll.querySelectorAll('.history-item, .history-separator').forEach(el => el.remove());

    if (state.history.length === 0) {
        empty.style.display = '';
        clearBtn.style.display = 'none';
                return;
            }

    empty.style.display = 'none';
    clearBtn.style.display = '';

    let prevColorIndex = -1;
    let prevOutputImage = null;

    state.history.forEach((entry, index) => {
        // Separator between different generation groups
        if (index > 0 && entry.colorIndex !== prevColorIndex && prevColorIndex !== -1) {
            const sep = document.createElement('div');
            sep.className = 'history-separator';
            scroll.appendChild(sep);
        }
        prevColorIndex = entry.colorIndex;

        const item = document.createElement('div');
        item.className = `history-item ${HISTORY_COLORS[entry.colorIndex]}`;
        item.onclick = () => showHistoryDetail(index);
        item.title = `#${index + 1} ${entry.type === 'sp' ? 'Generate SP' : 'Generate Image'}`;

        // Thumbnail
        if (entry.inputImage) {
            const isRepeated = prevOutputImage && entry.inputImage === prevOutputImage;
            const thumb = document.createElement('img');
            thumb.className = 'history-thumb' + (isRepeated ? ' repeated' : '');
            thumb.src = entry.inputImage;
            item.appendChild(thumb);
        }

        // Type label
        const typeLabel = document.createElement('span');
        typeLabel.className = 'history-type-label';
        typeLabel.textContent = entry.type === 'sp' ? 'SP' : 'IMG';
        item.appendChild(typeLabel);

        // Prompt badge
        if (entry.userPrompt) {
            const badge = document.createElement('span');
            badge.className = 'history-badge badge-p';
            badge.textContent = 'P';
            badge.title = entry.userPrompt.substring(0, 100);
            item.appendChild(badge);
        }

        // SP badge
        if (entry.structuredPrompt) {
            const sBadge = document.createElement('span');
            sBadge.className = 'history-badge badge-s';
            sBadge.textContent = 'S';
            sBadge.title = 'Structured Prompt';
            item.appendChild(sBadge);
        }

        // Config badge
        const cfgBadge = document.createElement('span');
        cfgBadge.className = 'history-badge badge-c';
        cfgBadge.textContent = 'C';
        cfgBadge.title = `CFG:${entry.params.cfgScale || entry.params.cfgText || entry.params.cfg || '-'} Step:${entry.params.steps} ${entry.params.width}×${entry.params.height} Seed:${entry.params.seed}`;
        item.appendChild(cfgBadge);

        scroll.appendChild(item);

        // Track for "repeated" detection
        if (entry.type === 'image' && entry.inputImage) {
            prevOutputImage = entry.inputImage;
        }
    });

    // Scroll to end
    requestAnimationFrame(() => {
        scroll.scrollLeft = scroll.scrollWidth;
    });
}

function clearHistory() {
    if (state.history.length === 0) return;
    if (!confirm('Clear all history and reset every input?')) return;

    // 清空历史
    state.history = [];
    state.generationCount = 0;
    renderHistory();

    // 清空图片
    state.currentImage = null;
    state.bboxVisible = false;
    state.activeBboxIndex = -1;
    state.bboxEditIndex = -1;
    state.focusedBboxIndex = -1;
    state.parsedElements = [];
    clearBboxes();
    updateImagePanel();

    // 清空 Structured Prompt
    state.structuredPrompt = '';
    state.parsedSP = null;
    state.undoStack = [];
    updateSPPanel();

    // 清空 User Prompt
    state.userPrompt = '';
    const promptInput = document.getElementById('userPromptInput');
    if (promptInput) promptInput.value = '';

    showToast('Cleared all history and inputs', 'info');
}

function showHistoryDetail(index) {
    state.selectedHistoryIndex = index;
    const entry = state.history[index];
    const body = document.getElementById('historyDetailBody');

    let html = '';

    // Meta
    html += `<div class="detail-section">`;
    html += `<div class="detail-section-title">Basic info</div>`;
    html += `<div class="detail-section-content">`;
    html += `Type: ${entry.type === 'sp' ? 'Generate Structured Prompt' : 'Generate image'}\n`;
    html += `Time: ${new Date(entry.timestamp).toLocaleString()}`;
    html += `</div></div>`;

    // Image
    if (entry.inputImage) {
        html += `<div class="detail-section">`;
        html += `<div class="detail-section-title">Input image</div>`;
        html += `<img class="detail-image" src="${entry.inputImage}" />`;
        html += `</div>`;
    }

    // User Prompt
    if (entry.userPrompt) {
        html += `<div class="detail-section">`;
        html += `<div class="detail-section-title">User Prompt</div>`;
        html += `<div class="detail-section-content">${escapeHtml(entry.userPrompt)}</div>`;
        html += `</div>`;
    }

    // Structured Prompt
    if (entry.structuredPrompt) {
        html += `<div class="detail-section">`;
        html += `<div class="detail-section-title">Structured Prompt</div>`;
        html += `<div class="detail-section-content mono">${escapeHtml(entry.structuredPrompt)}</div>`;
        html += `</div>`;
    }

    // Params
    const cfgVal = entry.params.cfgScale || entry.params.cfgText || entry.params.cfg || '-';
    html += `<div class="detail-section">`;
    html += `<div class="detail-section-title">Generation params</div>`;
    html += `<div class="detail-section-content">`;
    html += `CFG: ${cfgVal}   Step: ${entry.params.steps}   Size: ${entry.params.width} × ${entry.params.height}   Seed: ${entry.params.seed}\n`;
    html += `</div></div>`;

    body.innerHTML = html;
    document.getElementById('historyModal').classList.add('visible');
}

function closeHistoryModal() {
    document.getElementById('historyModal').classList.remove('visible');
}

function restoreFromHistory() {
    const index = state.selectedHistoryIndex;
    if (index < 0 || index >= state.history.length) return;

    const entry = state.history[index];

    // Restore image
    if (entry.inputImage) {
        loadImageFromSrc(entry.inputImage);
                } else {
        state.currentImage = null;
        updateImagePanel();
    }

    // Restore User Prompt
    state.userPrompt = entry.userPrompt || '';
    document.getElementById('userPromptInput').value = state.userPrompt;

    // Restore SP
    setSPContent(entry.structuredPrompt || '');

    // Restore params
    state.params = { ...entry.params };
    syncParamsToUI();

    closeHistoryModal();
    showToast('Restored history entry #' + (index + 1), 'success');
}

function restoreImageFromHistory() {
    const index = state.selectedHistoryIndex;
    if (index < 0 || index >= state.history.length) return;
    const entry = state.history[index];

    if (entry.inputImage) {
        loadImageFromSrc(entry.inputImage);
        autoUpdateHW();
    } else {
        state.currentImage = null;
        updateImagePanel();
    }
    closeHistoryModal();
    showToast('Image restored', 'success');
}

function restorePromptFromHistory() {
    const index = state.selectedHistoryIndex;
    if (index < 0 || index >= state.history.length) return;
    const entry = state.history[index];

    state.userPrompt = entry.userPrompt || '';
    document.getElementById('userPromptInput').value = state.userPrompt;
    setSPContent(entry.structuredPrompt || '');
    closeHistoryModal();
    showToast('Prompt restored', 'success');
}

function restoreParamsFromHistory() {
    const index = state.selectedHistoryIndex;
    if (index < 0 || index >= state.history.length) return;
    const entry = state.history[index];

    state.params = { ...entry.params };
    syncParamsToUI();
    closeHistoryModal();
    showToast('Params restored', 'success');
}

// ==================== Toast Notifications ====================
function showToast(message, type = 'info') {
    const container = document.getElementById('toastContainer');
    const toast = document.createElement('div');
    toast.className = `toast toast-${type}`;
    toast.textContent = message;
    container.appendChild(toast);

    setTimeout(() => {
        toast.style.animation = 'toast-out 0.3s ease forwards';
        setTimeout(() => {
            if (toast.parentNode) toast.remove();
        }, 300);
    }, 3000);
}

// ==================== BBox Drag & Resize ====================
function initBboxDrag(e, elemIndex, handle) {
    const container = document.getElementById('bboxContainer');
    const containerRect = container.getBoundingClientRect();
    const elem = state.parsedElements[elemIndex];
    if (!elem || !elem.bbox) return;

    state.bboxDrag = {
        elemIndex,
        handle,
        startX: e.clientX,
        startY: e.clientY,
        startBbox: [...elem.bbox],
        containerRect,
    };

    document.addEventListener('mousemove', onBboxDrag);
    document.addEventListener('mouseup', endBboxDrag);
}

function onBboxDrag(e) {
    const drag = state.bboxDrag;
    if (!drag) return;

    const { elemIndex, handle, startX, startY, startBbox, containerRect } = drag;
    const dx = ((e.clientX - startX) / containerRect.width) * 1000;
    const dy = ((e.clientY - startY) / containerRect.height) * 1000;

    let [x1, y1, x2, y2] = startBbox;

    if (handle === 'move') {
        const w = x2 - x1, h = y2 - y1;
        x1 = Math.max(0, Math.min(1000 - w, x1 + dx));
        y1 = Math.max(0, Math.min(1000 - h, y1 + dy));
        x2 = x1 + w;
        y2 = y1 + h;
    } else {
        // Resize
        if (handle.includes('n')) y1 = Math.max(0, Math.min(y2 - 10, y1 + dy));
        if (handle.includes('s')) y2 = Math.max(y1 + 10, Math.min(1000, y2 + dy));
        if (handle.includes('w')) x1 = Math.max(0, Math.min(x2 - 10, x1 + dx));
        if (handle.includes('e')) x2 = Math.max(x1 + 10, Math.min(1000, x2 + dx));
    }

    // Round
    x1 = Math.round(x1); y1 = Math.round(y1);
    x2 = Math.round(x2); y2 = Math.round(y2);

    // Update visual position
    const box = document.querySelector(`.bbox-box[data-elem-index="${elemIndex}"]`);
    if (box) {
        box.style.left = (x1 / 1000 * 100) + '%';
        box.style.top = (y1 / 1000 * 100) + '%';
        box.style.width = ((x2 - x1) / 1000 * 100) + '%';
        box.style.height = ((y2 - y1) / 1000 * 100) + '%';
    }

    // Store temp bbox for commit
    drag.currentBbox = [x1, y1, x2, y2];
}

function endBboxDrag(e) {
    document.removeEventListener('mousemove', onBboxDrag);
    document.removeEventListener('mouseup', endBboxDrag);

    const drag = state.bboxDrag;
    if (!drag) return;

    const newBbox = drag.currentBbox || drag.startBbox;
    const elemIndex = drag.elemIndex;
    state.bboxDrag = null;

    // Check if actually changed
    const old = drag.startBbox;
    const moved = old[0] !== newBbox[0] || old[1] !== newBbox[1] || old[2] !== newBbox[2] || old[3] !== newBbox[3];

    if (!moved) {
        // Was a click, not a drag — handle bbox click with overlap cycling
        handleBboxClick(elemIndex, e);
                            return;
                        }

    // Update parsed element
    state.parsedElements[elemIndex].bbox = newBbox;

    // Update SP JSON
    updateSPPosition(elemIndex, newBbox);
}

function updateSPPosition(elemIndex, newBbox) {
    if (!state.parsedSP) return;
    pushSPUndo();

    const elem = state.parsedElements[elemIndex];
    const newPosStr = `<bbox>${newBbox.join(' ')}</bbox>`;

    // Find and update in elements array
    let updated = false;
    if (state.parsedSP.elements) {
        for (const e of state.parsedSP.elements) {
            if (e.id === elem.id && e.position) {
                e.position = newPosStr;
                updated = true;
                break;
            }
        }
    }
    if (!updated && state.parsedSP.scene && state.parsedSP.scene.elements) {
        for (const e of state.parsedSP.scene.elements) {
            if (e.id === elem.id && e.position) {
                e.position = newPosStr;
                break;
            }
        }
    }

    state.structuredPrompt = stableJSONStringify(state.parsedSP, state.structuredPrompt);
    // Re-render structured view if visible
    if (!state.spRawMode) {
        renderSPStructured();
    } else {
        document.getElementById('spTextarea').value = state.structuredPrompt;
    }
}

// ==================== Window Events ====================
window.addEventListener('resize', () => {
    if (state.bboxVisible && state.currentImage) {
        renderBboxes();
    }
});