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

// Simplified unified status management
class StatusManager {
    static show(message, type = 'info', persistent = false) {
        // Hide all existing notifications
        this.hideAll();

        const statusEl = document.getElementById('statusMessage');
        statusEl.textContent = message;
        statusEl.className = `status-message ${type}`;
        statusEl.style.display = 'block';

        // Auto-hide non-error messages
        if (!persistent && type !== 'error') {
            setTimeout(() => {
                statusEl.style.display = 'none';
            }, type === 'success' ? 2000 : 3000);
        }
    }

    static hideAll() {
        // Clear main status
        const statusEl = document.getElementById('statusMessage');
        if (statusEl) statusEl.style.display = 'none';

        // Remove any lingering toasts
        document.querySelectorAll('.toast, .banner').forEach(el => el.remove());
    }

    static showProgress(message) {
        this.show(message, 'info', true);
    }
}

// ============================= //
// Apple HIG Enhanced Notifications & Progress System //
// ============================= //

// Toast notification system
function showToast(message, type = 'info', duration = 5000, actions = null) {
    const toasts = document.getElementById('toasts');
    const toast = document.createElement('div');
    toast.className = `toast ${type}`;

    const toastId = 'toast-' + Date.now();
    toast.id = toastId;

    let actionButtons = '';
    if (actions) {
        actionButtons = actions.map(action =>
            `<button class="toast-action" onclick="${action.onclick}">${action.text}</button>`
        ).join('');
    }

    toast.innerHTML = `
        <div class="toast-header">
            <span>${getToastIcon(type)} ${getToastTitle(type)}</span>
            <button class="toast-close" onclick="hideToast('${toastId}')" aria-label="关闭通知">×</button>
        </div>
        <div class="toast-body">
            ${message}
            ${actionButtons ? `<div class="toast-actions">${actionButtons}</div>` : ''}
        </div>
    `;

    toasts.appendChild(toast);

    // Trigger show animation
    requestAnimationFrame(() => {
        toast.classList.add('show');
    });

    // Auto-hide unless it's an error
    if (type !== 'error' && duration > 0) {
        setTimeout(() => hideToast(toastId), duration);
    }

    return toastId;
}

function hideToast(toastId) {
    const toast = document.getElementById(toastId);
    if (toast) {
        toast.classList.remove('show');
        setTimeout(() => {
            if (toast.parentNode) {
                toast.parentNode.removeChild(toast);
            }
        }, 300);
    }
}

function getToastIcon(type) {
    const icons = {
        success: '✓',
        error: '⚠',
        warning: '⚡',
        info: 'ⓘ'
    };
    return icons[type] || icons.info;
}

function getToastTitle(type) {
    const titles = {
        success: '成功',
        error: '错误',
        warning: '警告',
        info: '提示'
    };
    return titles[type] || titles.info;
}

// Banner notification system (for top-level status)
function showBanner(message, type = 'info', duration = 4000) {
    const banner = document.getElementById('banner');
    banner.textContent = message;
    banner.className = `banner ${type}`;
    banner.hidden = false;
    banner.classList.add('show');

    if (duration > 0) {
        setTimeout(() => {
            banner.classList.remove('show');
            setTimeout(() => {
                banner.hidden = true;
            }, 300);
        }, duration);
    }
}

// Enhanced progress feedback for generate button
function setGenerateButtonProgress(isLoading, text = '生成图像') {
    const btn = document.getElementById('generateBtn');
    const spinner = btn.querySelector('.spinner');
    const progressRing = btn.querySelector('.progress-ring');
    const btnText = btn.querySelector('.btn-text');

    if (isLoading) {
        btn.setAttribute('aria-busy', 'true');
        btn.disabled = true;
        spinner.style.display = 'block';
        progressRing.style.display = 'block';
        btnText.textContent = text;
    } else {
        btn.setAttribute('aria-busy', 'false');
        btn.disabled = false;
        spinner.style.display = 'none';
        progressRing.style.display = 'none';
        btnText.textContent = '生成图像';
    }
}

// Legacy function for compatibility - delegates to StatusManager
function showStatus(message, type = 'info', persistent = false) {
    StatusManager.show(message, type, persistent);
}

// Progress logs with collapsible details
function addProgressLog(message, type = 'info') {
    const logs = document.getElementById('progressLogs');
    if (!logs.classList.contains('active')) {
        logs.classList.add('active');
    }

    const entry = document.createElement('div');
    entry.className = `log-entry ${type}`;
    entry.textContent = `${new Date().toLocaleTimeString()}: ${message}`;

    logs.appendChild(entry);
    logs.scrollTop = logs.scrollHeight;
}

function clearProgressLogs() {
    const logs = document.getElementById('progressLogs');
    logs.innerHTML = '';
    logs.classList.remove('active');
}

// Skeleton loader for preview areas
function showPreviewSkeleton(containerId, count = 1) {
    const container = document.getElementById(containerId);
    container.innerHTML = '';

    for (let i = 0; i < count; i++) {
        const skeleton = document.createElement('div');
        skeleton.className = 'preview-skeleton skeleton';
        container.appendChild(skeleton);
    }
}

// Initialize local storage
const HISTORY_KEY = 'seedream_generation_history';

// Load history from localStorage on startup
function loadHistory() {
    try {
        const saved = localStorage.getItem(HISTORY_KEY);
        if (saved) {
            generationHistory = JSON.parse(saved);
            updateHistoryCount();
        }
    } catch (error) {
        console.error('Error loading history:', error);
        generationHistory = [];
    }
}

// Save history to localStorage
function saveHistory() {
    try {
        // Keep only last 100 generations to avoid storage limits
        if (generationHistory.length > 100) {
            generationHistory = generationHistory.slice(-100);
        }
        localStorage.setItem(HISTORY_KEY, JSON.stringify(generationHistory));
        updateHistoryCount();
    } catch (error) {
        console.error('Error saving history:', error);
    }
}

// DOM Elements
const fileInput = document.getElementById('fileInput');
const imagePreview = document.getElementById('imagePreview');
const imageUrls = document.getElementById('imageUrls');
const generateBtn = document.getElementById('generateBtn');
const statusMessage = document.getElementById('statusMessage');
const progressLogs = document.getElementById('progressLogs');
const currentResults = document.getElementById('currentResults');
const currentInfo = document.getElementById('currentInfo');
const historyGrid = document.getElementById('historyGrid');
const imageSizeSelect = document.getElementById('imageSize');
const customSizeElements = document.querySelectorAll('.custom-size');
const modelSelect = document.getElementById('modelSelect');
const promptTitle = document.getElementById('promptTitle');
const promptLabel = document.getElementById('promptLabel');
const imageInputCard = document.getElementById('imageInputCard');
const settingsCard = document.getElementById('settingsCard');

// Event Listeners
fileInput.addEventListener('change', handleFileUpload);
generateBtn.addEventListener('click', generateEdit);
imageSizeSelect.addEventListener('change', handleImageSizeChange);
modelSelect.addEventListener('change', handleModelChange);
document.getElementById('toggleApiKey').addEventListener('click', toggleApiKeyVisibility);
document.getElementById('testApiKey').addEventListener('click', testApiKeyConnection);

// Tab switching
function switchTab(tabName) {
    activeTab = tabName;
    
    // Update tab buttons
    document.querySelectorAll('.tab-btn').forEach(btn => {
        btn.classList.remove('active');
    });
    event.target.classList.add('active');
    
    // Update tab content
    document.querySelectorAll('.tab-content').forEach(content => {
        content.classList.remove('active');
    });
    
    if (tabName === 'current') {
        document.getElementById('currentTab').classList.add('active');
    } else {
        document.getElementById('historyTab').classList.add('active');
        displayHistory();
    }
}

// Toggle settings panel
function toggleSettings() {
    const isCollapsed = settingsCard.classList.contains('collapsed');
    const toggleBtn = document.querySelector('.settings-toggle-btn');

    settingsCard.classList.toggle('collapsed');

    // Update aria-expanded
    toggleBtn.setAttribute('aria-expanded', isCollapsed ? 'true' : 'false');
}

function toggleApiConfig() {
    const apiConfigCard = document.getElementById('apiConfigCard');
    const isCollapsed = apiConfigCard.classList.contains('collapsed');
    const toggleBtn = apiConfigCard.querySelector('.settings-toggle-btn');

    apiConfigCard.classList.toggle('collapsed');

    // Update aria-expanded
    toggleBtn.setAttribute('aria-expanded', isCollapsed ? 'true' : 'false');
}

function toggleApiConfig2() {
    const apiConfigCard = document.getElementById('apiConfigCard2');
    const isCollapsed = apiConfigCard.classList.contains('collapsed');
    const toggleBtn = apiConfigCard.querySelector('.settings-toggle-btn');

    apiConfigCard.classList.toggle('collapsed');

    // Update aria-expanded
    toggleBtn.setAttribute('aria-expanded', isCollapsed ? 'true' : 'false');
}

// Handle image size dropdown change
function handleImageSizeChange() {
    if (imageSizeSelect.value === 'custom') {
        customSizeElements.forEach(el => el.style.display = 'block');
    } else {
        customSizeElements.forEach(el => el.style.display = 'none');
    }
}

// Handle model dropdown change
function handleModelChange() {
    const modelValue = modelSelect.value;
    const isTextToImage = modelValue === 'fal-ai/bytedance/seedream/v4/text-to-image';
    const isVideoModel = modelValue.includes('wan-25-preview') || modelValue.includes('wan/v2.2');
    const isWan25 = modelValue.includes('wan-25-preview');
    const isWan22 = modelValue.includes('wan/v2.2');
    const isT2V = modelValue.includes('text-to-video');

    // Toggle video params visibility
    const videoParams = document.getElementById('videoParams');
    const settingsGrid = document.querySelector('.settings-grid');
    if (videoParams) {
        videoParams.style.display = isVideoModel ? 'block' : 'none';
    }
    if (settingsGrid) {
        settingsGrid.style.display = isVideoModel ? 'none' : 'grid';
    }

    // Show/hide WAN-specific params
    const wan25Params = document.querySelectorAll('.video-param-wan25');
    const wan22Params = document.querySelectorAll('.video-param-wan22');
    wan25Params.forEach(el => el.style.display = isWan25 ? 'block' : 'none');
    wan22Params.forEach(el => el.style.display = isWan22 ? 'block' : 'none');

    // Update UI labels based on model type
    if (isVideoModel) {
        if (isT2V) {
            promptTitle.textContent = '✏️ 视频提示词';
            promptLabel.textContent = '提示词';
            document.getElementById('prompt').placeholder = '例如:moody cyberpunk alley, steady cam forward, rain reflections';
            imageInputCard.style.display = 'none';
            uploadedImages = [];
            renderImagePreviews();
        } else {
            promptTitle.textContent = '✏️ 视频提示词';
            promptLabel.textContent = '提示词';
            document.getElementById('prompt').placeholder = '例如:cinematic slow push-in on the subject, volumetric light beams';
            imageInputCard.style.display = 'block';
        }
        document.getElementById('generateBtn').querySelector('.btn-text').textContent = '生成视频';
        updateVideoPriceEstimate();
    } else if (isTextToImage) {
        promptTitle.textContent = '生成提示词';
        promptLabel.textContent = '提示词';
        document.getElementById('prompt').placeholder = '例如:美丽的山水风景,湖泊和夕阳';
        imageInputCard.style.display = 'none';
        uploadedImages = [];
        renderImagePreviews();
        document.getElementById('generateBtn').querySelector('.btn-text').textContent = '生成图像';
    } else {
        promptTitle.textContent = '编辑指令';
        promptLabel.textContent = '编辑提示词';
        document.getElementById('prompt').placeholder = '例如:给模特穿上衣服和鞋子';
        imageInputCard.style.display = 'block';
        document.getElementById('generateBtn').querySelector('.btn-text').textContent = '生成图像';
    }

    // Add event listeners for video params to update price
    if (isVideoModel) {
        const videoResolution = document.getElementById('videoResolution');
        const videoDuration = document.getElementById('videoDuration');
        const videoFPS = document.getElementById('videoFPS');
        const videoNumFrames = document.getElementById('videoNumFrames');

        [videoResolution, videoDuration, videoFPS, videoNumFrames].forEach(el => {
            if (el) {
                el.removeEventListener('change', updateVideoPriceEstimate);
                el.removeEventListener('input', updateVideoPriceEstimate);
                el.addEventListener('change', updateVideoPriceEstimate);
                el.addEventListener('input', updateVideoPriceEstimate);
            }
        });
    }
}

// Setup paste upload functionality
function setupPasteUpload() {
    document.addEventListener('paste', async (e) => {
        // Check if we're focused on a text input that should handle paste normally
        const activeElement = document.activeElement;
        if (activeElement && (activeElement.tagName === 'INPUT' || activeElement.tagName === 'TEXTAREA')) {
            return; // Let the text input handle the paste
        }

        const items = e.clipboardData?.items;
        if (!items) return;

        const imageFiles = [];
        const textItems = [];

        // Process clipboard items
        for (let i = 0; i < items.length; i++) {
            const item = items[i];

            if (item.type.indexOf('image') === 0) {
                // Handle image files
                const file = item.getAsFile();
                if (file) {
                    imageFiles.push(file);
                }
            } else if (item.type === 'text/plain') {
                // Handle text (potentially URLs)
                item.getAsString((text) => {
                    textItems.push(text);
                });
            }
        }

        // Process images if found
        if (imageFiles.length > 0) {
            e.preventDefault();
            showToast(`正在粘贴 ${imageFiles.length} 张图像...`, 'info');

            try {
                for (const file of imageFiles) {
                    await processImageFile(file);
                }
                showToast('图像粘贴成功!', 'success');
                updateImagePreview();
            } catch (error) {
                showToast(`粘贴失败: ${error.message}`, 'error');
            }
        }

        // Process URLs in text (delayed to handle async string retrieval)
        setTimeout(() => {
            for (const text of textItems) {
                const urlPattern = /https?:\/\/[^\s]+\.(jpg|jpeg|png|gif|webp|bmp)/i;
                const match = text.match(urlPattern);
                if (match) {
                    e.preventDefault();
                    const imageUrls = document.getElementById('imageUrls');
                    const currentUrls = imageUrls.value.trim();
                    const newUrl = match[0];
                    imageUrls.value = currentUrls ? `${currentUrls}\n${newUrl}` : newUrl;
                    showToast('图像URL已粘贴到输入框', 'success');
                }
            }
        }, 10);
    });
}

// Setup extended drag and drop functionality
function setupExtendedDragDrop() {
    const dropZones = [
        document.getElementById('historyGrid'),
        document.getElementById('currentResults'),
        document.querySelector('.right-panel'),
        document.querySelector('.empty-state')
    ].filter(Boolean); // Remove null elements

    dropZones.forEach(zone => {
        zone.addEventListener('dragover', handleDragOver);
        zone.addEventListener('dragenter', handleDragEnter);
        zone.addEventListener('dragleave', handleDragLeave);
        zone.addEventListener('drop', handleDrop);
    });

    // Also add body as fallback drop zone
    document.body.addEventListener('dragover', handleDragOver);
    document.body.addEventListener('drop', handleDrop);
}

function handleDragOver(e) {
    e.preventDefault();
    e.dataTransfer.dropEffect = 'copy';
}

function handleDragEnter(e) {
    e.preventDefault();
    if (e.target.classList.contains('empty-state') ||
        e.target.closest('.empty-state') ||
        e.target.id === 'historyGrid' ||
        e.target.id === 'currentResults') {
        e.target.style.backgroundColor = 'color-mix(in oklab, var(--brand-primary) 8%, Canvas 92%)';
        e.target.style.border = '2px dashed var(--brand-primary)';
    }
}

function handleDragLeave(e) {
    if (e.target.classList.contains('empty-state') ||
        e.target.closest('.empty-state') ||
        e.target.id === 'historyGrid' ||
        e.target.id === 'currentResults') {
        e.target.style.backgroundColor = '';
        e.target.style.border = '';
    }
}

async function handleDrop(e) {
    e.preventDefault();

    // Reset visual feedback
    if (e.target.classList.contains('empty-state') ||
        e.target.closest('.empty-state') ||
        e.target.id === 'historyGrid' ||
        e.target.id === 'currentResults') {
        e.target.style.backgroundColor = '';
        e.target.style.border = '';
    }

    const files = Array.from(e.dataTransfer.files);
    const imageFiles = files.filter(file => file.type.startsWith('image/'));

    if (imageFiles.length > 0) {
        showToast(`正在处理 ${imageFiles.length} 张拖拽图像...`, 'info');

        try {
            for (const file of imageFiles) {
                await processImageFile(file);
            }
            showToast('图像拖拽成功!', 'success');
            updateImagePreview();
        } catch (error) {
            showToast(`拖拽失败: ${error.message}`, 'error');
        }
    }

    // Handle text/URLs
    const text = e.dataTransfer.getData('text/plain');
    if (text) {
        const urlPattern = /https?:\/\/[^\s]+\.(jpg|jpeg|png|gif|webp|bmp)/i;
        const match = text.match(urlPattern);
        if (match) {
            const imageUrls = document.getElementById('imageUrls');
            const currentUrls = imageUrls.value.trim();
            const newUrl = match[0];
            imageUrls.value = currentUrls ? `${currentUrls}\n${newUrl}` : newUrl;
            showToast('图像URL已添加', 'success');
        }
    }
}

// Initialize on page load
window.addEventListener('DOMContentLoaded', () => {
    // Load saved API key
    const savedKey = localStorage.getItem('fal_api_key');
    if (savedKey) {
        document.getElementById('apiKey').value = savedKey;
    }

    // Load saved textarea sizes
    loadTextareaSizes();

    // Load SDE preferences
    loadSDEPreferences();

    // Setup SDE event listeners
    const enableSDECheckbox = document.getElementById('enableSDE');
    if (enableSDECheckbox) {
        enableSDECheckbox.addEventListener('change', syncSDEMode);
    }

    // Initialize UI state
    handleImageSizeChange();
    handleModelChange();
    loadHistory();
    setupPasteUpload();
    setupExtendedDragDrop();
    initializeKeyboardShortcuts();
    initializeAccessibility();
    displayHistory();

    // Smart API Key onboarding for first-time users
    if (!savedKey && !localStorage.getItem('hasSeenApiKeyGuide')) {
        // Keep settings expanded for new users
        settingsCard.classList.remove('collapsed');

        // Highlight API Key input with pulse animation
        const apiKeyInput = document.getElementById('apiKey');
        apiKeyInput.style.animation = 'pulse-highlight 2s ease-in-out 3';

        // Show welcoming guide toast
        showToast('👋 欢迎使用SeedDream!请先配置FAL API密钥以开始生成图像', 'info', 0);

        // Focus API key input after a brief delay
        setTimeout(() => {
            apiKeyInput.focus();
            apiKeyInput.scrollIntoView({ behavior: 'smooth', block: 'center' });
        }, 500);

        // Mark guide as seen (set after first API key save)
        apiKeyInput.addEventListener('blur', () => {
            if (apiKeyInput.value.trim()) {
                localStorage.setItem('hasSeenApiKeyGuide', '1');
            }
        });
    } else {
        // Collapse settings by default for returning users
        settingsCard.classList.add('collapsed');
    }
});

// Handle file upload with immediate preview
async function handleFileUpload(event) {
    const files = Array.from(event.target.files);
    
    if (files.length === 0) return;
    
    showStatus(`正在处理 ${files.length} 张图像...`, 'info');
    
    let processedCount = 0;
    let errorCount = 0;
    
    for (const file of files) {
        if (uploadedImages.length >= 10) {
            showStatus('最多允许10张图像。部分图像未被添加。', 'error');
            break;
        }
        
        if (file.type.startsWith('image/')) {
            try {
                const tempIndex = uploadedImages.length;
                const loadingId = `loading-${Date.now()}-${tempIndex}`;
                
                // Add loading placeholder
                const loadingPreview = document.createElement('div');
                loadingPreview.className = 'image-preview-item loading-preview';
                loadingPreview.id = loadingId;
                loadingPreview.innerHTML = `
                    <div class="loading-placeholder">
                        <div class="spinner"></div>
                        <p>${file.name}</p>
                    </div>
                `;
                imagePreview.appendChild(loadingPreview);
                
                const reader = new FileReader();
                
                reader.onerror = (error) => {
                    console.error('Error reading file:', file.name, error);
                    errorCount++;
                    document.getElementById(loadingId)?.remove();
                    showStatus(`读取文件失败: ${file.name}`, 'error');
                };
                
                reader.onload = (e) => {
                    const dataUrl = e.target.result;
                    document.getElementById(loadingId)?.remove();

                    // Get image dimensions and create unified structure
                    const img = new Image();
                    img.onload = function() {
                        const imageObj = {
                            src: dataUrl,
                            width: this.width,
                            height: this.height,
                            id: `img-${Date.now()}-${Math.random().toString(36).substr(2, 9)}`
                        };
                        uploadedImages.push(imageObj);
                        processedCount++;
                        addImagePreview(imageObj.src, uploadedImages.length - 1);

                        if (processedCount + errorCount === files.length) {
                            if (errorCount === 0) {
                                showStatus(`成功添加 ${processedCount} 张图像 (已使用 ${uploadedImages.length}/10 个位置)`, 'success');
                            } else {
                                showStatus(`添加了 ${processedCount} 张图像,${errorCount} 张失败 (已使用 ${uploadedImages.length}/10 个位置)`, 'warning');
                            }
                        }
                    };

                    img.onerror = () => {
                        console.error('Error loading image dimensions for:', file.name);
                        const imageObj = {
                            src: dataUrl,
                            width: 1280,
                            height: 1280,
                            id: `img-${Date.now()}-${Math.random().toString(36).substr(2, 9)}`
                        };
                        uploadedImages.push(imageObj);
                        processedCount++;
                        addImagePreview(imageObj.src, uploadedImages.length - 1);
                    };

                    img.src = dataUrl;
                };
                
                reader.readAsDataURL(file);
                
            } catch (error) {
                console.error('Error processing file:', file.name, error);
                errorCount++;
                showStatus(`处理文件出错: ${file.name}`, 'error');
            }
        } else {
            errorCount++;
            showStatus(`${file.name} 不是图像文件`, 'error');
        }
    }
    
    event.target.value = '';
}

// Add image preview with upload status indicator
function addImagePreview(src, index) {
    const previewItem = document.createElement('div');
    previewItem.className = 'image-preview-item';
    previewItem.dataset.imageIndex = index;
    const imageId = `upload-img-${Date.now()}-${index}`;
    previewItem.innerHTML = `
        <img id="${imageId}" src="${src}" alt="Upload ${index + 1}"
             onclick="openImageModal('${imageId}', '${src}', 'Uploaded Image ${index + 1}', 'Input Image')"
             loading="lazy" decoding="async" style="cursor: pointer;">
        <button class="remove-btn" onclick="removeImage(${index})">×</button>
        <div class="image-upload-status" style="display: none;">
            <div class="upload-progress-mini">
                <div class="upload-progress-mini-bar"></div>
            </div>
            <span class="upload-status-text"></span>
        </div>
    `;
    imagePreview.appendChild(previewItem);
}

// Remove image
function removeImage(index) {
    uploadedImages.splice(index, 1);
    renderImagePreviews();
}

// Fill example prompt
function fillExample(exampleText) {
    const promptTextarea = document.getElementById('prompt');
    const drawerPromptTextarea = document.getElementById('drawerPrompt');

    if (promptTextarea) {
        promptTextarea.value = exampleText;
        promptTextarea.focus();
    }
    if (drawerPromptTextarea) {
        drawerPromptTextarea.value = exampleText;
    }

    // Switch to edit mode if needed
    const currentModel = modelSelect.value;
    if (currentModel === 'fal-ai/bytedance/seedream/v4/text-to-image') {
        modelSelect.value = 'fal-ai/bytedance/seedream/v4/edit';
        handleModelChange();
    }

    showToast('示例提示词已填入,上传图像后即可生成', 'success', 3000);
}

// Download image
function downloadImage(imageSrc, imageId) {
    const link = document.createElement('a');
    link.href = imageSrc;
    link.download = `seedream-${imageId}.png`;
    document.body.appendChild(link);
    link.click();
    document.body.removeChild(link);
    showToast('图像下载中...', 'success', 2000);
}

// Update custom size based on last image
function updateCustomSizeFromLastImage() {
    if (uploadedImages.length > 0) {
        const lastImage = uploadedImages[uploadedImages.length - 1];
        let width = lastImage.width;
        let height = lastImage.height;
        
        // Calculate aspect ratio
        const aspectRatio = width / height;
        
        // If both dimensions are less than 1024 (minimum allowed), scale up proportionally
        if (width < 1024 && height < 1024) {
            // Scale based on which dimension needs more scaling to reach 1024
            if (width < height) {
                // Height is larger, scale based on width minimum
                width = 1024;
                height = Math.round(1024 / aspectRatio);
            } else {
                // Width is larger or equal, scale based on height minimum
                height = 1024;
                width = Math.round(1024 * aspectRatio);
            }
        }
        
        // If any dimension exceeds 4096, scale down proportionally
        if (width > 4096 || height > 4096) {
            if (width > height) {
                // Width exceeds more, scale based on width maximum
                const scaleFactor = 4096 / width;
                width = 4096;
                height = Math.round(height * scaleFactor);
            } else {
                // Height exceeds more, scale based on height maximum
                const scaleFactor = 4096 / height;
                height = 4096;
                width = Math.round(width * scaleFactor);
            }
        }
        
        // Final bounds check while maintaining aspect ratio
        // If width is still below minimum after height-based scaling
        if (width < 1024) {
            const scaleFactor = 1024 / width;
            width = 1024;
            height = Math.round(height * scaleFactor);
        }
        
        // If height is still below minimum after width-based scaling
        if (height < 1024) {
            const scaleFactor = 1024 / height;
            height = 1024;
            width = Math.round(width * scaleFactor);
        }
        
        // Final check to ensure we don't exceed maximums
        width = Math.min(4096, width);
        height = Math.min(4096, height);
        
        document.getElementById('customWidth').value = width;
        document.getElementById('customHeight').value = height;
        
        if (imageSizeSelect.value !== 'custom') {
            imageSizeSelect.value = 'custom';
            handleImageSizeChange();
        }
    }
}

// Re-render all image previews
function renderImagePreviews() {
    imagePreview.innerHTML = '';
    uploadedImages.forEach((image, index) => {
        addImagePreview(image.src, index);
    });
}

// Show status message
function showStatus(message, type = 'info') {
    statusMessage.className = `status-message ${type}`;
    statusMessage.textContent = message;
    statusMessage.style.display = 'block';
    
    if (type === 'success' || type === 'error') {
        setTimeout(() => {
            statusMessage.style.display = 'none';
        }, 5000);
    }
}

// Add log entry
function addLog(message) {
    const logEntry = document.createElement('div');
    logEntry.className = 'log-entry';
    logEntry.textContent = `${new Date().toLocaleTimeString()}: ${message}`;
    progressLogs.appendChild(logEntry);
    progressLogs.scrollTop = progressLogs.scrollHeight;
}

// Clear logs
function clearLogs() {
    progressLogs.innerHTML = '';
    progressLogs.classList.remove('active');
}

// Get image size configuration
function getImageSize() {
    const size = imageSizeSelect.value;
    if (size === 'custom') {
        return {
            width: parseInt(document.getElementById('customWidth').value),
            height: parseInt(document.getElementById('customHeight').value)
        };
    }
    return size;
}

// Upload image to FAL storage with progress tracking
async function uploadImageToFal(imageData, apiKey, imageIndex, totalImages, actualIndex) {
    try {
        // Show individual image upload status if preview exists
        const previewItem = document.querySelector(`.image-preview-item[data-image-index="${actualIndex}"]`);
        if (previewItem) {
            const statusDiv = previewItem.querySelector('.image-upload-status');
            const statusText = previewItem.querySelector('.upload-status-text');
            const progressBar = previewItem.querySelector('.upload-progress-mini-bar');
            
            if (statusDiv) {
                statusDiv.style.display = 'block';
                statusText.textContent = '上传中...';
                progressBar.style.width = '30%';
                previewItem.classList.add('uploading');
            }
        }
        
        // Update main progress bar
        updateUploadProgress(imageIndex - 1, totalImages, `Uploading image ${imageIndex}/${totalImages}...`);
        
        // Show upload start message
        addLog(`正在上传图像 ${imageIndex}/${totalImages} 到FAL存储...`);
        
        // Calculate approximate size for logging
        const sizeInMB = (imageData.length * 0.75 / 1024 / 1024).toFixed(2);
        addLog(`图像 ${imageIndex} 大小: ~${sizeInMB} MB`);
        
        const response = await fetch('/api/upload-to-fal', {
            method: 'POST',
            headers: {
                'Content-Type': 'application/json',
                'Authorization': `Bearer ${apiKey}`
            },
            body: JSON.stringify({ image_data: imageData })
        });
        
        if (!response.ok) {
            const error = await response.text();
            throw new Error(error || 'Failed to upload image to FAL');
        }
        
        const data = await response.json();
        addLog(`✓ 图像 ${imageIndex}/${totalImages} 上传成功`);
        
        // Update progress after successful upload
        updateUploadProgress(imageIndex, totalImages, `Completed ${imageIndex}/${totalImages}`);
        
        // Update individual image status for success
        if (previewItem) {
            const statusText = previewItem.querySelector('.upload-status-text');
            const progressBar = previewItem.querySelector('.upload-progress-mini-bar');
            
            if (progressBar && statusText) {
                progressBar.style.width = '100%';
                statusText.textContent = '已上传 ✓';
                previewItem.classList.remove('uploading');
                previewItem.classList.add('uploaded');
                
                // Fade out status after 3 seconds
                setTimeout(() => {
                    const statusDiv = previewItem.querySelector('.image-upload-status');
                    if (statusDiv) {
                        statusDiv.style.opacity = '0';
                        setTimeout(() => {
                            statusDiv.style.display = 'none';
                            statusDiv.style.opacity = '1';
                            previewItem.classList.remove('uploaded');
                        }, 300);
                    }
                }, 3000);
            }
        }
        
        return data.url;
    } catch (error) {
        console.error('Error uploading to FAL:', error);
        addLog(`✗ 图像 ${imageIndex} 上传失败: ${error.message}`);
        
        // Update individual image status for error
        const previewItem = document.querySelector(`.image-preview-item[data-image-index="${actualIndex}"]`);
        if (previewItem) {
            const statusText = previewItem.querySelector('.upload-status-text');
            const progressBar = previewItem.querySelector('.upload-progress-mini-bar');
            
            if (progressBar && statusText) {
                progressBar.style.width = '100%';
                progressBar.style.backgroundColor = '#dc3545';
                statusText.textContent = '上传失败 ✗';
                previewItem.classList.remove('uploading');
                previewItem.classList.add('upload-failed');
            }
        }
        
        throw error;
    }
}

// Update upload progress bar
function updateUploadProgress(completed, total, message) {
    const progressFill = document.getElementById('uploadProgressFill');
    const progressText = document.getElementById('uploadProgressText');
    const container = document.getElementById('uploadProgressContainer');
    
    if (progressFill && progressText && container) {
        const percentage = Math.round((completed / total) * 100);
        progressFill.style.width = `${percentage}%`;
        progressText.textContent = `${message} (${percentage}%)`;
        
        // Add animation class on completion
        if (percentage === 100) {
            progressFill.classList.add('complete');
            
            // Wait briefly to show completion state, then fade out
            setTimeout(() => {
                // Add transition for smooth fade
                container.style.transition = 'opacity 0.5s ease';
                container.style.opacity = '0';
                
                // Remove from DOM after fade completes
                setTimeout(() => {
                    if (container.parentNode) {
                        container.parentNode.removeChild(container);
                    }
                }, 500);
            }, 1500);
        } else {
            // Ensure visible if not complete
            container.style.opacity = '1';
        }
    }
}

// Prepare image URLs for API with detailed progress tracking
async function getImageUrlsForAPI() {
    const urls = [];
    const apiKey = getAPIKey();

    // Count total images to upload
    const base64Images = uploadedImages.filter(img => img.src.startsWith('data:'));
    const urlImages = uploadedImages.filter(img => !img.src.startsWith('data:'));

    // Safely get text URLs if element exists
    const imageUrlsEl = document.getElementById('imageUrls');
    const textUrls = imageUrlsEl ? imageUrlsEl.value.trim().split('\n').filter(url => url.trim()) : [];

    const totalUploads = base64Images.length;
    const totalImages = uploadedImages.length + textUrls.length;

    if (totalUploads > 0) {
        addLog(`准备上传 ${totalUploads} 张图像到FAL存储...`);
        showStatus(`正在上传 ${totalUploads} 张图像到FAL存储...`, 'info');
    }

    // Concurrent upload with failure tolerance
    const uploadPromises = uploadedImages.map(async (image, index) => {
        if (image.src.startsWith('data:')) {
            // Base64 image needs upload
            try {
                const falUrl = await uploadImageToFal(image.src, apiKey, index + 1, totalUploads, index);
                return { success: true, url: falUrl, index };
            } catch (error) {
                console.error(`Image ${index + 1} upload failed:`, error);
                return { success: false, error: error.message, index };
            }
        } else {
            // Already a URL
            addLog(`使用现有URL作为图像 ${index + 1}`);
            return { success: true, url: image.src, index };
        }
    });

    // Wait for all uploads (concurrent execution)
    const results = await Promise.allSettled(uploadPromises);

    // Process results
    let successCount = 0;
    let failureCount = 0;
    const failedIndices = [];

    results.forEach((result, index) => {
        if (result.status === 'fulfilled' && result.value.success) {
            urls.push(result.value.url);
            successCount++;
        } else {
            failureCount++;
            failedIndices.push(index + 1);
            if (result.status === 'fulfilled') {
                addLog(`图像 ${index + 1} 上传失败: ${result.value.error}`);
            } else {
                addLog(`图像 ${index + 1} 上传失败: ${result.reason}`);
            }
        }
    });

    // Report upload results
    if (failureCount > 0 && successCount === 0) {
        throw new Error('所有图像上传失败,无法继续生成');
    }

    if (failureCount > 0) {
        showToast(
            `${failureCount} 张图像上传失败(编号: ${failedIndices.join(', ')}),将使用 ${successCount} 张成功上传的图像继续生成`,
            'warning',
            5000
        );
        addLog(`部分上传失败,继续使用 ${successCount}/${totalImages} 张图像`);
    } else if (totalUploads > 0) {
        showStatus(`所有 ${totalUploads} 张图像上传成功!`, 'success');
        addLog(`上传完成: 共 ${totalImages} 张图像已准备好生成`);
    }
    
    // Add text URLs directly
    if (textUrls.length > 0) {
        addLog(`正在处理文本输入中的 ${textUrls.length} 个URL...`);
    }

    for (const url of textUrls) {
        urls.push(url);
        addLog(`已添加URL: ${url.substring(0, 50)}...`);
    }
    
    return urls.slice(0, 10);
}


// Generate edit
async function generateEdit() {
    // Debounce: prevent rapid fire clicks
    const now = Date.now();
    if (now - lastGenerationTime < 500) {
        showToast('请勿频繁点击生成按钮', 'warning', 2000);
        return;
    }
    lastGenerationTime = now;

    // Prevent concurrent generations
    if (generationInProgress) {
        showToast('已有生成任务在进行中', 'warning', 2000);
        return;
    }

    const prompt = getCurrentPrompt().trim();
    if (!prompt) {
        showStatus('请输入提示词', 'error');
        return;
    }
    
    const selectedModel = modelSelect.value;
    const isTextToImage = selectedModel === 'fal-ai/bytedance/seedream/v4/text-to-image';
    const isVideo = isVideoModel(selectedModel);
    const isVideoI2V = isVideo && !selectedModel.includes('text-to-video');

    // Prepare upload progress UI early if there are base64 uploads
    const base64Images = uploadedImages.filter(img => img.src.startsWith('data:'));
    const totalUploads = base64Images.length;
    
    // Remove any existing progress container
    const existingProgress = document.getElementById('uploadProgressContainer');
    if (existingProgress && existingProgress.parentNode) {
        existingProgress.parentNode.removeChild(existingProgress);
    }
    
    if (totalUploads > 0) {
        const progressContainer = document.createElement('div');
        progressContainer.id = 'uploadProgressContainer';
        progressContainer.className = 'upload-progress-container';
        progressContainer.innerHTML = `
            <div class="upload-progress-bar">
                <div class="upload-progress-fill" id="uploadProgressFill"></div>
            </div>
            <div class="upload-progress-text" id="uploadProgressText">Initializing...</div>
        `;
        // Insert progress container after status message
        if (statusMessage.parentNode) {
            statusMessage.parentNode.insertBefore(progressContainer, statusMessage.nextSibling);
        }
    }
    
    // Now resolve image URLs (this may trigger uploads and progress updates)
    let imageUrlsArray;
    try {
        imageUrlsArray = await getImageUrlsForAPI();
    } catch (error) {
        // Ensure progress UI is removed on upload failure
        const pc = document.getElementById('uploadProgressContainer');
        if (pc && pc.parentNode) {
            pc.parentNode.removeChild(pc);
        }
        addLog(`上传错误: ${error.message || error}`);
        showStatus(`上传错误: ${error.message || error}`, 'error');
        return;
    }
    // Check image requirements based on model type
    if (!isTextToImage && !isVideo && imageUrlsArray.length === 0) {
        showStatus('请上传图像或提供图像URL进行图像编辑', 'error');
        const pc = document.getElementById('uploadProgressContainer');
        if (pc && pc.parentNode) pc.parentNode.removeChild(pc);
        return;
    }
    if (isVideoI2V && imageUrlsArray.length === 0) {
        showStatus('视频 I2V 模式需要上传首帧图像', 'error');
        const pc = document.getElementById('uploadProgressContainer');
        if (pc && pc.parentNode) pc.parentNode.removeChild(pc);
        return;
    }
    
    generationInProgress = true;
    generateBtn.disabled = true;
    generateBtn.querySelector('.btn-text').textContent = '生成中...';
    generateBtn.querySelector('.spinner').style.display = 'block';

    // 同时禁用drawer内联按钮
    const drawerBtnInline = document.getElementById('drawerGenerateBtnInline');
    const drawerBtnInlineSDE = document.getElementById('drawerGenerateBtnInlineSDE');
    if (drawerBtnInline) {
        drawerBtnInline.disabled = true;
        drawerBtnInline.querySelector('.btn-text').textContent = '生成中';
        drawerBtnInline.querySelector('.spinner').style.display = 'block';
    }
    if (drawerBtnInlineSDE) {
        drawerBtnInlineSDE.disabled = true;
        drawerBtnInlineSDE.querySelector('.btn-text').textContent = '生成中';
        drawerBtnInlineSDE.querySelector('.spinner').style.display = 'block';
    }
    
    // Clear current results
    currentResults.innerHTML = '<div class="empty-state"><p>准备生成...</p></div>';
    currentInfo.innerHTML = '';
    clearLogs();
    
    showStatus('开始生成进程...', 'info');
    progressLogs.classList.add('active');
    
    // Show initial status
    if (!isTextToImage && imageUrlsArray.length > 0) {
        addLog(`正在处理 ${imageUrlsArray.length} 张输入图像...`);
    }
    
    const requestData = {
        prompt: prompt
    };

    if (isVideo) {
        // Video generation parameters
        if (isVideoI2V && imageUrlsArray.length > 0) {
            requestData.image_url = imageUrlsArray[0]; // Use first image as frame
        }
        // Add video-specific params from buildVideoParams()
        Object.assign(requestData, buildVideoParams());
    } else {
        // Image generation parameters
        requestData.image_size = getImageSize();
        requestData.num_images = parseInt(document.getElementById('numImages').value);
        requestData.enable_safety_checker = false;

        if (!isTextToImage) {
            requestData.image_urls = imageUrlsArray;
            requestData.max_images = parseInt(document.getElementById('maxImages').value);
        }
    }

    const seed = document.getElementById('seed').value;
    if (seed) {
        requestData.seed = parseInt(seed);
    }
    
    // Store generation metadata
    currentGeneration = {
        id: Date.now(),
        timestamp: new Date().toISOString(),
        prompt: prompt,
        model: selectedModel,
        settings: {
            image_size: requestData.image_size,
            num_images: requestData.num_images,
            seed: requestData.seed
        }
    };
    
    try {
        // Cancel any existing generation
        if (currentGenerationAbort) {
            currentGenerationAbort.abort();
            addLog('已取消前一次生成请求');
        }

        // Create new abort controller
        currentGenerationAbort = new AbortController();

        const apiKey = getAPIKey();
        if (!apiKey) {
            showStatus('请输入您的FAL API密钥', 'error');
            addLog('未找到API密钥');
            document.getElementById('apiKey').focus();
            return;
        }

        addLog('正在向FAL API提交请求...');
        addLog(`模型: ${selectedModel}`);
        addLog(`提示词: ${prompt}`);
        if (!isTextToImage) {
            addLog(`输入图像数量: ${imageUrlsArray.length}`);
        }

        const response = await callFalAPI(apiKey, requestData, selectedModel, currentGenerationAbort.signal);
        
        // Store results in current generation
        currentGeneration.results = response;
        
        // Display results
        displayCurrentResults(response);
        
        // Add to history
        generationHistory.push(currentGeneration);
        saveHistory();
        
        showStatus('生成完成!', 'success');
        
    } catch (error) {
        console.error('Error:', error);
        const errorMessage = error.name === 'AbortError' ? '生成已取消' : `错误: ${error.message}`;
        showStatus(errorMessage, error.name === 'AbortError' ? 'warning' : 'error');
        addLog(errorMessage);
    } finally {
        generationInProgress = false;
        generateBtn.disabled = false;
        generateBtn.querySelector('.btn-text').textContent = '生成图像';
        generateBtn.querySelector('.spinner').style.display = 'none';

        // 恢复drawer内联按钮状态
        const drawerBtnInline = document.getElementById('drawerGenerateBtnInline');
        const drawerBtnInlineSDE = document.getElementById('drawerGenerateBtnInlineSDE');
        if (drawerBtnInline) {
            drawerBtnInline.disabled = false;
            drawerBtnInline.querySelector('.btn-text').textContent = '生成';
            drawerBtnInline.querySelector('.spinner').style.display = 'none';
        }
        if (drawerBtnInlineSDE) {
            drawerBtnInlineSDE.disabled = false;
            drawerBtnInlineSDE.querySelector('.btn-text').textContent = '生成';
            drawerBtnInlineSDE.querySelector('.spinner').style.display = 'none';
        }

        // Clear abort controller
        currentGenerationAbort = null;
        // Ensure any lingering upload progress UI is removed
        const pc2 = document.getElementById('uploadProgressContainer');
        if (pc2 && pc2.parentNode) {
            pc2.parentNode.removeChild(pc2);
        }
    }
}

// Get API key
function getAPIKey() {
    const apiKeyInput = document.getElementById('apiKey');
    const apiKey = apiKeyInput.value.trim();
    
    if (apiKey) {
        localStorage.setItem('fal_api_key', apiKey);
    }
    
    return apiKey || localStorage.getItem('fal_api_key');
}

// Call FAL API with improved polling strategy
async function callFalAPI(apiKey, requestData, model, signal) {
    const submitResponse = await fetch('/api/generate', {
        method: 'POST',
        headers: {
            'Content-Type': 'application/json',
            'Authorization': `Bearer ${apiKey}`,
            'X-Model-Endpoint': model
        },
        body: JSON.stringify(requestData),
        keepalive: true
    });
    
    if (!submitResponse.ok) {
        const error = await submitResponse.text();
        throw new Error(error || 'API request failed');
    }
    
    const submitData = await submitResponse.json();
    const { request_id } = submitData;
    addLog(`请求已提交,ID: ${request_id}`);
    
    // Enhanced polling with exponential backoff and cancellation
    let attempts = 0;
    const maxAttempts = 120;
    let delay = 800; // Start with 800ms
    let previousLogCount = 0;

    while (attempts < maxAttempts) {
        // Check for cancellation
        if (signal?.aborted) {
            throw new Error('Generation cancelled');
        }

        await new Promise(resolve => setTimeout(resolve, delay));

        const statusUrl = `/api/status/${request_id}`;
        const statusResponse = await fetch(statusUrl, {
            headers: {
                'Authorization': `Bearer ${apiKey}`,
                'X-Model-Endpoint': model
            },
            signal, // Pass abort signal to fetch
            keepalive: true
        });

        let statusData;
        try {
            statusData = await statusResponse.json();
        } catch (_) {
            statusData = {};
        }

        if (!statusResponse.ok) {
            const errorMsg = statusData?.error || `HTTP ${statusResponse.status}`;

            // Implement retry logic for server errors
            if (statusResponse.status >= 500 || statusResponse.status === 429) {
                addLog(`服务器暂时不可用 (${statusResponse.status}),将重试...`);
                attempts++;
                delay = Math.min(delay * 1.5, 4000); // Increase delay for retries
                continue;
            }

            addLog(`状态查询失败: ${errorMsg}`, 'error');
            throw new Error('Failed to check request status');
        }

        // Add any new logs
        if (statusData.logs && statusData.logs.length > previousLogCount) {
            const newLogs = statusData.logs.slice(previousLogCount);
            newLogs.forEach(log => {
                if (log && !log.includes('Request submitted')) {
                    addLog(log);
                }
            });
            previousLogCount = statusData.logs.length;
        }

        // ✅ Support both uppercase and lowercase status (FAL queue returns uppercase)
        const status = (statusData.status || '').toUpperCase();

        if (status === 'COMPLETED') {
            return statusData.result;
        } else if (status === 'ERROR') {
            throw new Error(statusData.error || 'Generation failed');
        }

        attempts++;

        // Exponential backoff: increase delay gradually
        delay = Math.min(delay * 1.35, 4000); // Cap at 4 seconds

        if (attempts % 5 === 0) {
            addLog(`处理中... (已轮询 ${attempts} 次,下次等待 ${Math.round(delay/1000)}s)`);
        }
    }

    throw new Error('Request timed out after maximum attempts');
}

// Display current results
function displayCurrentResults(response) {
    // Handle video results
    if (response && response.video) {
        currentResults.innerHTML = '';
        const videoUrl = response.video.url || '';
        const videoId = `current-video-${Date.now()}`;

        const item = document.createElement('div');
        item.className = 'generation-item video-item';
        item.innerHTML = `
            <video id="${videoId}" controls style="width: 100%; border-radius: var(--radius-md);">
                <source src="${videoUrl}" type="video/mp4">
                您的浏览器不支持视频播放。
            </video>
            <div class="generation-footer">
                <div class="generation-timestamp">刚刚生成</div>
                <div class="generation-actions-bar">
                    <button class="action-icon" onclick="downloadVideo('${videoUrl}', 'video'); event.stopPropagation();" title="下载视频">
                        ⬇️ MP4
                    </button>
                </div>
            </div>
        `;
        currentResults.appendChild(item);

        if (response.seed) {
            currentInfo.innerHTML = `<strong>随机种子:</strong> ${response.seed}`;
            addLog(`使用的随机种子: ${response.seed}`);
        }
        addLog('视频生成完成');
        return;
    }

    // Handle image results
    if (!response || !response.images || response.images.length === 0) {
        currentResults.innerHTML = '<div class="empty-state"><p>未生成图像</p></div>';
        return;
    }

    currentResults.innerHTML = '';

    response.images.forEach((image, index) => {
        const imgSrc = image.url || image.file_data || '';
        const imageId = `current-img-${Date.now()}-${index}`;

        const item = document.createElement('div');
        item.className = 'generation-item';
        item.innerHTML = `
            <img id="${imageId}" src="${imgSrc}" alt="Result ${index + 1}" loading="lazy" decoding="async"
                 onclick="openImageModal('${imageId}', '${imgSrc}', '当前生成', '${new Date().toLocaleString()}')">
            <div class="generation-footer">
                <div class="generation-timestamp">刚刚生成</div>
                <div class="generation-actions-bar">
                    <button class="action-icon" onclick="useAsInput('${imageId}', '${imgSrc}'); event.stopPropagation();" title="作为输入">

                    </button>
                    <button class="action-icon" onclick="downloadImage('${imgSrc}', 'current-${index}'); event.stopPropagation();" title="下载">
                        ⬇️
                    </button>
                </div>
            </div>
        `;
        currentResults.appendChild(item);
    });

    // Display generation info
    if (response.seed) {
        currentInfo.innerHTML = `<strong>随机种子:</strong> ${response.seed}`;
        addLog(`使用的随机种子: ${response.seed}`);
    }

    addLog(`已生成 ${response.images.length} 张图像`);
}

// Display history
function displayHistory() {
    if (generationHistory.length === 0) {
        historyGrid.innerHTML = `
            <div class="empty-state">
                <p>No generation history</p>
                <small>Your generated images will be saved here</small>
            </div>
        `;
        return;
    }

    historyGrid.innerHTML = '';

    // Display history in reverse order (newest first)
    [...generationHistory].reverse().forEach((generation) => {
        if (!generation.results) return;

        // Handle video results
        if (generation.results.video) {
            const videoUrl = generation.results.video.url || '';
            const videoId = `history-video-${generation.id}`;

            const item = document.createElement('div');
            item.className = 'generation-item video-item';
            item.innerHTML = `
                <video id="${videoId}" controls style="width: 100%; border-radius: var(--radius-md);">
                    <source src="${videoUrl}" type="video/mp4">
                    您的浏览器不支持视频播放。
                </video>
                <div class="generation-footer">
                    <div class="generation-timestamp">${new Date(generation.timestamp).toLocaleString()}</div>
                    <div class="generation-actions-bar">
                        <button class="action-icon" onclick="copyPromptFromHistory('${generation.prompt.replace(/'/g, "\\'")}', event)" title="复制提示词">
                            📋
                        </button>
                        <button class="action-icon" onclick="downloadVideo('${videoUrl}', 'history-${generation.id}'); event.stopPropagation();" title="下载视频">
                            ⬇️ MP4
                        </button>
                    </div>
                </div>
            `;
            historyGrid.appendChild(item);
        }

        // Handle image results
        if (generation.results.images) {
            generation.results.images.forEach((image, imgIndex) => {
                const imgSrc = image.url || image.file_data || '';
                const imageId = `history-img-${generation.id}-${imgIndex}`;

                const item = document.createElement('div');
                item.className = 'generation-item';
                item.innerHTML = `
                    <img id="${imageId}" src="${imgSrc}" alt="Generation" loading="lazy" decoding="async"
                         onclick="openImageModal('${imageId}', '${imgSrc}', '${generation.prompt.replace(/'/g, "\\'")}', '${new Date(generation.timestamp).toLocaleString()}')">
                    <div class="generation-footer">
                        <div class="generation-timestamp">${new Date(generation.timestamp).toLocaleString()}</div>
                        <div class="generation-actions-bar">
                            <button class="action-icon" onclick="useAsInput('${imageId}', '${imgSrc}'); event.stopPropagation();" title="作为输入">

                            </button>
                            <button class="action-icon" onclick="copyPromptFromHistory('${generation.prompt.replace(/'/g, "\\'")}', event)" title="复制提示词">
                                📋
                            </button>
                            <button class="action-icon" onclick="downloadImage('${imgSrc}', '${generation.id}-${imgIndex}'); event.stopPropagation();" title="下载">
                                ⬇️
                            </button>
                        </div>
                    </div>
                `;
                historyGrid.appendChild(item);
            });
        }
    });
}

// Adjust textarea size for mobile/iPad
function adjustTextareaSize(textareaId, direction) {
    const textarea = document.getElementById(textareaId);
    if (!textarea) return;

    const currentRows = parseInt(textarea.getAttribute('rows') || '3');
    let newRows = currentRows;

    if (direction === 'larger' && currentRows < 12) {
        newRows = currentRows + 1;
    } else if (direction === 'smaller' && currentRows > 1) {
        newRows = currentRows - 1;
    }

    if (newRows !== currentRows) {
        textarea.setAttribute('rows', newRows.toString());
        // 保存用户偏好
        localStorage.setItem(`textarea-size-${textareaId}`, newRows.toString());

        // 给用户反馈
        const sizeText = newRows <= 2 ? '小' : newRows <= 4 ? '中' : newRows <= 6 ? '大' : '超大';
        StatusManager.show(`输入框大小已调整为: ${sizeText}`, 'success');
    }
}

// Load saved textarea sizes on page load
function loadTextareaSizes() {
    ['prompt', 'drawerPrompt'].forEach(id => {
        const savedSize = localStorage.getItem(`textarea-size-${id}`);
        if (savedSize) {
            const textarea = document.getElementById(id);
            if (textarea) {
                textarea.setAttribute('rows', savedSize);
            }
        }
    });
}

// ============================= //
// 结构化指令编辑器 (SDE) 功能
// ============================= //

// SDE模板定义
const SDE_TEMPLATES = {
    single_female: `使用我提供的参考照片,生成一张包含其中真人女性的图像。她的相貌、体型和特征必须与参考照片中的人物完全一致。接下来的描述将把这位女性置于一个新的、详细的的场景中:`,
    multiple_female: `使用我提供的多张参考照片,生成一张包含其中多位真人女性的图像。她们各自的相貌、体型和特征必须与对应的参考照片完全一致。接下来的描述将把她们置于一个新的、详细的的场景中:`,
    mixed_gender: `使用我提供的多张参考照片,生成一张包含其中多位真人的图像。其中,女性参考照片对应场景中的女主角,男性参考照片对应场景中的男性。他们各自的相貌、体型和特征必须与对应的参考照片完全一致。接下来的描述将把他们置于一个新的、详细的的场景中:`,
    multi_character: `使用我提供的多张参考照片,生成一张包含其中多位真人的图像。他们各自的相貌、体型和特征必须与对应的参考照片完全一致。接下来的描述将把他们置于一个新的、详细的(动作和姿势参考动漫插图)的场景中:`
};

const GSP_STYLE_TEXT = `由于是随意抓拍,构图混乱且角度尴尬,没有明确的主体。房间里不均匀的灯光导致画面部分区域曝光不均,同时带有轻微的运动模糊和强烈的数字噪点。整体呈现出一种刻意平庸的、混乱且私密的纪实感。`;

// 同步SDE模式状态
function syncSDEMode() {
    const mainCheckbox = document.getElementById('enableSDE');
    const drawerCheckbox = document.getElementById('drawerEnableSDE');

    if (event.target === drawerCheckbox) {
        mainCheckbox.checked = drawerCheckbox.checked;
    } else {
        drawerCheckbox.checked = mainCheckbox.checked;
    }

    toggleSDEMode(mainCheckbox.checked);
}

// 切换SDE模式显示
function toggleSDEMode(enabled) {
    const traditionalMode = document.getElementById('traditionalPromptMode');
    const structuredMode = document.getElementById('structuredPromptMode');
    const drawerTraditionalMode = document.getElementById('drawerTraditionalMode');
    const drawerStructuredMode = document.getElementById('drawerStructuredMode');

    // Data protection: check for content loss
    if (enabled) {
        // Switching TO SDE mode - check traditional prompt
        const traditionalPrompt = document.getElementById('prompt').value;
        if (traditionalPrompt.trim().length > 0) {
            const confirmed = confirm(
                '切换到结构化编辑器将替换当前提示词。是否继续?\n\n' +
                '当前内容将保存在草稿中,可通过"恢复草稿"找回。'
            );

            if (!confirmed) {
                // User cancelled - revert checkbox state
                const mainCheckbox = document.getElementById('enableSDE');
                const drawerCheckbox = document.getElementById('drawerEnableSDE');
                if (mainCheckbox) mainCheckbox.checked = false;
                if (drawerCheckbox) drawerCheckbox.checked = false;
                return;
            }

            // Save to draft
            localStorage.setItem('sde_draft_traditional', traditionalPrompt);
            showToast('原始提示词已保存到草稿', 'info', 3000);
        }

        traditionalMode.style.display = 'none';
        structuredMode.style.display = 'block';
        drawerTraditionalMode.style.display = 'none';
        drawerStructuredMode.style.display = 'block';
        updateCombinedPrompt();
    } else {
        // Switching FROM SDE mode - check structured content
        const sceneDescription = document.getElementById('sceneDescription').value;
        if (sceneDescription.trim().length > 0) {
            const confirmed = confirm(
                '切换到传统模式将清空结构化编辑器内容。是否继续?\n\n' +
                '当前内容将保存在草稿中,可通过"恢复草稿"找回。'
            );

            if (!confirmed) {
                // User cancelled - revert checkbox state
                const mainCheckbox = document.getElementById('enableSDE');
                const drawerCheckbox = document.getElementById('drawerEnableSDE');
                if (mainCheckbox) mainCheckbox.checked = true;
                if (drawerCheckbox) drawerCheckbox.checked = true;
                return;
            }

            // Save to draft
            localStorage.setItem('sde_draft_structured', sceneDescription);
            showToast('结构化内容已保存到草稿', 'info', 3000);
        }

        traditionalMode.style.display = 'block';
        structuredMode.style.display = 'none';
        drawerTraditionalMode.style.display = 'block';
        drawerStructuredMode.style.display = 'none';
    }

    // 保存用户偏好
    localStorage.setItem('sde-enabled', enabled.toString());
}

// Restore draft content
function restoreDraft() {
    const enableSDE = document.getElementById('enableSDE').checked;

    if (enableSDE) {
        // In SDE mode - restore structured draft
        const draft = localStorage.getItem('sde_draft_structured');
        if (draft) {
            document.getElementById('sceneDescription').value = draft;
            document.getElementById('drawerSceneDescription').value = draft;
            showToast('已恢复结构化草稿内容', 'success', 3000);
            localStorage.removeItem('sde_draft_structured');
            updateCombinedPrompt();
        } else {
            showToast('没有可恢复的草稿', 'info', 2000);
        }
    } else {
        // In traditional mode - restore traditional draft
        const draft = localStorage.getItem('sde_draft_traditional');
        if (draft) {
            document.getElementById('prompt').value = draft;
            document.getElementById('drawerPrompt').value = draft;
            showToast('已恢复传统提示词草稿', 'success', 3000);
            localStorage.removeItem('sde_draft_traditional');
        } else {
            showToast('没有可恢复的草稿', 'info', 2000);
        }
    }
}

// 更新合并后的提示词
function updateCombinedPrompt() {
    const referenceSelect = document.getElementById('referenceProtocol');
    const sceneTextarea = document.getElementById('sceneDescription');
    const gspCheckbox = document.getElementById('gspStyle');
    const previewTextarea = document.getElementById('combinedPromptPreview');

    // 同步移动端的值
    const drawerReferenceSelect = document.getElementById('drawerReferenceProtocol');
    const drawerSceneTextarea = document.getElementById('drawerSceneDescription');
    const drawerGspCheckbox = document.getElementById('drawerGspStyle');

    if (drawerReferenceSelect) drawerReferenceSelect.value = referenceSelect.value;
    if (drawerSceneTextarea) drawerSceneTextarea.value = sceneTextarea.value;
    if (drawerGspCheckbox) drawerGspCheckbox.checked = gspCheckbox.checked;

    // 构建完整提示词
    const parts = [];

    // 模块一:参考协议
    if (referenceSelect.value && SDE_TEMPLATES[referenceSelect.value]) {
        parts.push(SDE_TEMPLATES[referenceSelect.value]);
    }

    // 模块二:场景描述
    if (sceneTextarea.value.trim()) {
        parts.push(sceneTextarea.value.trim());
    }

    // 模块三:风格化协议
    if (gspCheckbox.checked) {
        parts.push(GSP_STYLE_TEXT);
    }

    const combinedPrompt = parts.join('\n\n');

    if (previewTextarea) {
        previewTextarea.value = combinedPrompt;
    }

    // 同时更新传统模式的textarea以保持兼容性
    const promptTextarea = document.getElementById('prompt');
    const drawerPromptTextarea = document.getElementById('drawerPrompt');

    if (document.getElementById('enableSDE').checked) {
        if (promptTextarea) promptTextarea.value = combinedPrompt;
        if (drawerPromptTextarea) drawerPromptTextarea.value = combinedPrompt;
    }
}

// 从移动端同步SDE数据到桌面端
function syncSDEFromDrawer() {
    const drawerReferenceSelect = document.getElementById('drawerReferenceProtocol');
    const drawerSceneTextarea = document.getElementById('drawerSceneDescription');
    const drawerGspCheckbox = document.getElementById('drawerGspStyle');

    const referenceSelect = document.getElementById('referenceProtocol');
    const sceneTextarea = document.getElementById('sceneDescription');
    const gspCheckbox = document.getElementById('gspStyle');

    if (drawerReferenceSelect && referenceSelect) {
        referenceSelect.value = drawerReferenceSelect.value;
    }
    if (drawerSceneTextarea && sceneTextarea) {
        sceneTextarea.value = drawerSceneTextarea.value;
    }
    if (drawerGspCheckbox && gspCheckbox) {
        gspCheckbox.checked = drawerGspCheckbox.checked;
    }

    updateCombinedPrompt();
}

// 获取当前有效的提示词
function getCurrentPrompt() {
    const sdeEnabled = document.getElementById('enableSDE').checked;

    if (sdeEnabled) {
        const previewTextarea = document.getElementById('combinedPromptPreview');
        return previewTextarea ? previewTextarea.value : '';
    } else {
        const promptTextarea = document.getElementById('prompt');
        return promptTextarea ? promptTextarea.value : '';
    }
}

// 加载SDE用户偏好
function loadSDEPreferences() {
    const savedEnabled = localStorage.getItem('sde-enabled');
    if (savedEnabled === 'true') {
        document.getElementById('enableSDE').checked = true;
        document.getElementById('drawerEnableSDE').checked = true;
        toggleSDEMode(true);
    }
}

// Copy prompt from history
function copyPromptFromHistory(prompt, event) {
    event.stopPropagation(); // 防止触发图片点击事件

    // 解码HTML实体和转义字符
    const decodedPrompt = prompt.replace(/\\'/g, "'").replace(/&quot;/g, '"').replace(/&amp;/g, '&');

    navigator.clipboard.writeText(decodedPrompt).then(() => {
        // 显示成功提示
        StatusManager.show('提示词已复制到剪贴板', 'success');

        // 可选:同时填入当前的prompt输入框
        const promptTextarea = document.getElementById('prompt');
        const drawerPromptTextarea = document.getElementById('drawerPrompt');
        if (promptTextarea) {
            promptTextarea.value = decodedPrompt;
        }
        if (drawerPromptTextarea) {
            drawerPromptTextarea.value = decodedPrompt;
        }
    }).catch(err => {
        console.error('复制失败:', err);
        StatusManager.show('复制失败,请手动选择文本复制', 'error');
    });
}

// Use image as input
async function useAsInput(imageId, imageSrc) {
    try {
        // Switch to edit mode if in text-to-image mode
        const currentModel = modelSelect.value;
        if (currentModel === 'fal-ai/bytedance/seedream/v4/text-to-image') {
            modelSelect.value = 'fal-ai/bytedance/seedream/v4/edit';
            handleModelChange();
            showStatus('已切换到图像编辑模式', 'info');
        }
        
        if (uploadedImages.length >= 10) {
            showStatus('最多允许10张图像。请先删除一些图像。', 'error');
            return;
        }
        
        // Get dimensions and create unified image object
        const imgElement = document.getElementById(imageId);
        let width, height;

        if (imgElement) {
            if (!imgElement.complete) {
                await new Promise((resolve) => {
                    imgElement.onload = resolve;
                    imgElement.onerror = resolve;
                });
            }
            width = imgElement.naturalWidth || imgElement.width;
            height = imgElement.naturalHeight || imgElement.height;
        } else {
            // Load image to get dimensions
            await new Promise((resolve) => {
                const img = new Image();
                img.onload = function() {
                    width = this.width;
                    height = this.height;
                    resolve();
                };
                img.onerror = function() {
                    width = 1280;
                    height = 1280;
                    resolve();
                };
                img.src = imageSrc;
            });
        }

        const imageObj = {
            src: imageSrc,
            width: width,
            height: height,
            id: `img-${Date.now()}-${Math.random().toString(36).substr(2, 9)}`
        };
        uploadedImages.push(imageObj);
        
        renderImagePreviews();
        
        const totalImages = uploadedImages.length;
        showStatus(`图像已添加为输入 (已使用 ${totalImages}/10 个位置)`, 'success');
        addLog(`已添加图像作为输入 (${totalImages}/10 张图像)`);
        
        // Flash animation
        imagePreview.style.animation = 'flash 0.5s';
        setTimeout(() => {
            imagePreview.style.animation = '';
        }, 500);
        
    } catch (error) {
        console.error('Error using image as input:', error);
        showStatus('添加图像作为输入失败', 'error');
    }
}

// Clear all input images
function clearAllInputImages() {
    uploadedImages = [];
    renderImagePreviews();
    showStatus('所有输入图像已清除', 'info');
}

// Clear history
function clearHistory() {
    if (confirm('确定要清除所有生成历史吗?此操作无法撤销。')) {
        generationHistory = [];
        localStorage.removeItem(HISTORY_KEY);
        displayHistory();
        updateHistoryCount();
        showStatus('历史已清除', 'info');
    }
}

// Download all history
function downloadAllHistory() {
    if (generationHistory.length === 0) {
        showStatus('无历史可下载', 'error');
        return;
    }
    
    // Create a zip file or download each image
    generationHistory.forEach((generation, genIndex) => {
        if (generation.results && generation.results.images) {
            generation.results.images.forEach((image, imgIndex) => {
                const imgSrc = image.url || image.file_data || '';
                if (imgSrc) {
                    const link = document.createElement('a');
                    link.href = imgSrc;
                    link.download = `seedream-${generation.id}-${imgIndex}.png`;
                    document.body.appendChild(link);
                    link.click();
                    document.body.removeChild(link);
                }
            });
        }
    });
    
    showStatus('正在下载所有图像...', 'info');
}

// Update history count
function updateHistoryCount() {
    const countElement = document.getElementById('historyCount');
    if (countElement) {
        let totalImages = 0;
        generationHistory.forEach(gen => {
            if (gen.results && gen.results.images) {
                totalImages += gen.results.images.length;
            }
        });
        countElement.textContent = totalImages;
    }
}

// Modal functionality for viewing large images
let currentModalImage = null;

function openImageModal(imageId, imageSrc, prompt, timestamp) {
    const modal = document.getElementById('imageModal');
    const modalImg = document.getElementById('modalImage');
    const modalCaption = document.getElementById('modalCaption');

    // Store current image info for use in "Use as Input" and trigger element for focus restoration
    const triggerElement = document.activeElement;
    currentModalImage = { id: imageId, src: imageSrc, triggerElement };

    // Reset image transform
    modalImg.style.transform = 'scale(1)';
    modalImg.style.transformOrigin = 'center';

    // Add zoom functionality
    setupImageZoom(modalImg);

    // Set modal content
    modalImg.src = imageSrc;
    modalCaption.innerHTML = `
        <strong>生成时间:</strong> ${timestamp}<br>
        <strong>提示词:</strong> ${prompt}
    `;
    
    // Show modal
    modal.classList.add('show');

    // Prevent body scroll when modal is open
    document.body.style.overflow = 'hidden';

    // Focus the close button for keyboard navigation
    setTimeout(() => {
        const closeBtn = modal.querySelector('.modal-close');
        if (closeBtn) closeBtn.focus();
    }, 100);
    
    // Close modal on escape key
    document.addEventListener('keydown', handleModalEscape);
    
    // Close modal on clicking outside the image
    modal.onclick = function(event) {
        if (event.target === modal || event.target === modalImg.parentElement) {
            closeImageModal();
        }
    };
}

function setupImageZoom(img) {
    let scale = 1;
    let isDragging = false;
    let startX, startY, initialX = 0, initialY = 0;

    // Double-click to zoom
    img.addEventListener('dblclick', () => {
        scale = scale > 1 ? 1 : 2;
        img.style.transform = `scale(${scale}) translate(${initialX}px, ${initialY}px)`;
        if (scale === 1) {
            initialX = 0;
            initialY = 0;
        }
    });

    // Wheel zoom (with Ctrl for desktop)
    img.addEventListener('wheel', (e) => {
        if (e.ctrlKey) {
            e.preventDefault();
            const delta = e.deltaY > 0 ? -0.1 : 0.1;
            scale = Math.max(1, Math.min(4, scale + delta));
            img.style.transform = `scale(${scale}) translate(${initialX}px, ${initialY}px)`;

            if (scale === 1) {
                initialX = 0;
                initialY = 0;
            }
        }
    }, { passive: false });

    // Touch gestures for pinch-to-zoom
    let initialDistance = 0;
    let initialScale = 1;

    img.addEventListener('touchstart', (e) => {
        if (e.touches.length === 2) {
            // Pinch gesture
            const touch1 = e.touches[0];
            const touch2 = e.touches[1];
            initialDistance = Math.hypot(
                touch2.clientX - touch1.clientX,
                touch2.clientY - touch1.clientY
            );
            initialScale = scale;
        } else if (e.touches.length === 1 && scale > 1) {
            // Pan gesture when zoomed
            isDragging = true;
            startX = e.touches[0].clientX - initialX;
            startY = e.touches[0].clientY - initialY;
        }
    });

    img.addEventListener('touchmove', (e) => {
        e.preventDefault();

        if (e.touches.length === 2) {
            // Pinch zoom
            const touch1 = e.touches[0];
            const touch2 = e.touches[1];
            const currentDistance = Math.hypot(
                touch2.clientX - touch1.clientX,
                touch2.clientY - touch1.clientY
            );

            scale = Math.max(1, Math.min(4, initialScale * (currentDistance / initialDistance)));
            img.style.transform = `scale(${scale}) translate(${initialX}px, ${initialY}px)`;

            if (scale === 1) {
                initialX = 0;
                initialY = 0;
            }
        } else if (e.touches.length === 1 && isDragging && scale > 1) {
            // Pan
            initialX = e.touches[0].clientX - startX;
            initialY = e.touches[0].clientY - startY;
            img.style.transform = `scale(${scale}) translate(${initialX}px, ${initialY}px)`;
        }
    }, { passive: false });

    img.addEventListener('touchend', () => {
        isDragging = false;
    });

    // Mouse drag for desktop
    img.addEventListener('mousedown', (e) => {
        if (scale > 1) {
            isDragging = true;
            startX = e.clientX - initialX;
            startY = e.clientY - initialY;
            img.style.cursor = 'grabbing';
            e.preventDefault();
        }
    });

    document.addEventListener('mousemove', (e) => {
        if (isDragging && scale > 1) {
            initialX = e.clientX - startX;
            initialY = e.clientY - startY;
            img.style.transform = `scale(${scale}) translate(${initialX}px, ${initialY}px)`;
        }
    });

    document.addEventListener('mouseup', () => {
        if (isDragging) {
            isDragging = false;
            img.style.cursor = 'grab';
        }
    });
}

function closeImageModal() {
    const modal = document.getElementById('imageModal');
    modal.classList.remove('show');
    document.body.style.overflow = '';
    document.removeEventListener('keydown', handleModalEscape);

    // Restore focus to the trigger element
    if (currentModalImage?.triggerElement) {
        setTimeout(() => {
            currentModalImage.triggerElement.focus();
        }, 100);
    }

    currentModalImage = null;
}

function handleModalEscape(event) {
    if (event.key === 'Escape') {
        closeImageModal();
    }
}

function useModalImageAsInput() {
    if (currentModalImage) {
        useAsInput(currentModalImage.id, currentModalImage.src);
        closeImageModal();
    }
}

// Toggle API Key visibility
function toggleApiKeyVisibility() {
    const apiKeyInput = document.getElementById('apiKey');
    const toggleBtn = document.getElementById('toggleApiKey');

    if (apiKeyInput.type === 'password') {
        apiKeyInput.type = 'text';
        toggleBtn.textContent = '🙈';
        toggleBtn.title = '隐藏密钥';
        toggleBtn.setAttribute('aria-label', '隐藏密钥');
    } else {
        apiKeyInput.type = 'password';
        toggleBtn.textContent = '👁';
        toggleBtn.title = '显示密钥';
        toggleBtn.setAttribute('aria-label', '显示密钥');
    }
}

// Test API Key connection
async function testApiKeyConnection() {
    const apiKey = getAPIKey();
    const statusDiv = document.getElementById('apiKeyStatus');
    const testBtn = document.getElementById('testApiKey');

    if (!apiKey) {
        showApiKeyStatus('请先输入API密钥', 'error');
        return;
    }

    testBtn.disabled = true;
    testBtn.textContent = '测试中...';
    showApiKeyStatus('正在测试连接...', 'testing');

    try {
        // Test with a minimal request to check API key validity
        const response = await fetch('/api/generate', {
            method: 'POST',
            headers: {
                'Content-Type': 'application/json',
                'Authorization': `Bearer ${apiKey}`,
                'X-Model-Endpoint': 'fal-ai/bytedance/seedream/v4/text-to-image'
            },
            body: JSON.stringify({
                prompt: 'test',
                image_size: 'square',
                num_images: 1,
                enable_safety_checker: false
            })
        });

        if (response.status === 401) {
            showApiKeyStatus('API密钥无效 - 请检查密钥是否正确', 'error');
        } else if (response.status === 403) {
            showApiKeyStatus('权限不足 - 请检查密钥权限', 'error');
        } else if (response.status === 429) {
            showApiKeyStatus('API密钥有效,但已达到速率限制', 'success');
        } else if (response.ok) {
            const data = await response.json();
            if (data.request_id) {
                showApiKeyStatus('✓ API密钥有效,连接正常', 'success');
            } else {
                showApiKeyStatus('API密钥有效,但响应异常', 'error');
            }
        } else {
            const errorText = await response.text();
            if (errorText.includes('quota') || errorText.includes('credit') || errorText.includes('balance')) {
                showApiKeyStatus('API密钥有效,但账户余额不足', 'error');
            } else {
                showApiKeyStatus(`连接失败 - ${errorText}`, 'error');
            }
        }
    } catch (error) {
        console.error('API Key test error:', error);
        showApiKeyStatus(`网络错误 - ${error.message}`, 'error');
    } finally {
        testBtn.disabled = false;
        testBtn.textContent = '测试';

        // Hide status after 5 seconds for success, keep error visible
        setTimeout(() => {
            if (statusDiv.classList.contains('success')) {
                statusDiv.style.display = 'none';
                statusDiv.className = 'api-key-status';
            }
        }, 5000);
    }
}

// Show API Key status
function showApiKeyStatus(message, type) {
    const statusDiv = document.getElementById('apiKeyStatus');
    statusDiv.className = `api-key-status ${type}`;
    statusDiv.textContent = message;
    statusDiv.style.display = 'block';
}

// iOS 键盘遮挡处理
function setupIOSKeyboardHandling() {
    const prompt = document.getElementById('prompt');
    const appContainer = document.querySelector('.app-container');

    if (!prompt || !appContainer) return;

    // 检测是否支持 visualViewport API
    const vv = window.visualViewport;
    if (!vv) return;

    function adjustForKeyboard() {
        const viewportHeight = vv.height;
        const windowHeight = window.innerHeight;
        const diff = windowHeight - viewportHeight;

        if (diff > 0) {
            // 键盘弹出,调整底部内边距
            appContainer.style.paddingBottom = `${Math.max(16, diff + 16)}px`;
        }
    }

    function resetKeyboard() {
        // 重置内边距
        appContainer.style.paddingBottom = '';
    }

    // 输入框获得焦点时开始监听
    prompt.addEventListener('focus', () => {
        vv?.addEventListener('resize', adjustForKeyboard);
    });

    // 输入框失去焦点时停止监听并重置
    prompt.addEventListener('blur', () => {
        vv?.removeEventListener('resize', adjustForKeyboard);
        resetKeyboard();
    });

    // 页面可见性变化时也重置(用户切换应用)
    document.addEventListener('visibilitychange', () => {
        if (document.hidden) {
            resetKeyboard();
        }
    });
}

// 在页面加载完成后设置键盘处理
document.addEventListener('DOMContentLoaded', setupIOSKeyboardHandling);

// Drawer functionality for mobile sidebar
function toggleDrawer() {
    const drawer = document.getElementById('drawer');
    const overlay = document.querySelector('.drawer-overlay');
    const isOpen = drawer.classList.contains('open');

    if (isOpen) {
        closeDrawer();
    } else {
        openDrawer();
    }
}

function openDrawer() {
    const drawer = document.getElementById('drawer');
    const overlay = document.querySelector('.drawer-overlay');

    drawer.classList.add('open');
    overlay.classList.add('show');

    // Prevent body scroll when drawer is open
    document.body.style.overflow = 'hidden';

    // Add escape key listener
    document.addEventListener('keydown', handleDrawerEscape);

    // Add swipe-to-close gesture support
    addSwipeGestures();
}

function closeDrawer() {
    const drawer = document.getElementById('drawer');
    const overlay = document.querySelector('.drawer-overlay');

    drawer.classList.remove('open');
    overlay.classList.remove('show');

    // Restore body scroll
    document.body.classList.remove('drawer-open');

    // Remove escape key listener
    document.removeEventListener('keydown', handleDrawerEscape);

    // Remove swipe gestures
    removeSwipeGestures();
}

function handleDrawerEscape(event) {
    if (event.key === 'Escape') {
        closeDrawer();
    }
}

// Swipe gesture support for drawer
let startX = 0;
let currentX = 0;
let isDragging = false;

function addSwipeGestures() {
    const drawer = document.getElementById('drawer');

    drawer.addEventListener('touchstart', handleTouchStart, { passive: true });
    drawer.addEventListener('touchmove', handleTouchMove, { passive: false });
    drawer.addEventListener('touchend', handleTouchEnd, { passive: true });
}

function removeSwipeGestures() {
    const drawer = document.getElementById('drawer');

    drawer.removeEventListener('touchstart', handleTouchStart);
    drawer.removeEventListener('touchmove', handleTouchMove);
    drawer.removeEventListener('touchend', handleTouchEnd);
}

function handleTouchStart(event) {
    startX = event.touches[0].clientX;
    isDragging = false;
}

function handleTouchMove(event) {
    if (!isDragging) {
        isDragging = true;
    }

    currentX = event.touches[0].clientX;
    const deltaX = currentX - startX;

    // Only allow swiping left (to close)
    if (deltaX < 0) {
        const drawer = document.getElementById('drawer');
        const percentage = Math.abs(deltaX) / drawer.offsetWidth;
        const translateX = Math.min(0, deltaX);

        drawer.style.transform = `translateX(${translateX}px)`;

        // Prevent default to avoid page scroll
        event.preventDefault();
    }
}

function handleTouchEnd(event) {
    if (!isDragging) return;

    const drawer = document.getElementById('drawer');
    const deltaX = currentX - startX;
    const threshold = drawer.offsetWidth * 0.3; // 30% swipe threshold

    // Reset transform
    drawer.style.transform = '';

    // Close drawer if swiped left beyond threshold
    if (deltaX < -threshold) {
        closeDrawer();
    }

    isDragging = false;
}

// Keyboard shortcuts
function initializeKeyboardShortcuts() {
    document.addEventListener('keydown', (e) => {
        // Cmd/Ctrl + Enter: Generate
        if ((e.metaKey || e.ctrlKey) && e.key === 'Enter') {
            e.preventDefault();
            const generateBtn = document.getElementById('generateBtn');
            if (!generateBtn.disabled && !generateBtn.classList.contains('loading')) {
                generate();
            }
        }

        // Escape: Close modal or drawer
        if (e.key === 'Escape') {
            const modal = document.getElementById('imageModal');
            const drawer = document.getElementById('drawer');

            if (modal.classList.contains('show')) {
                closeImageModal();
            } else if (drawer.classList.contains('open')) {
                closeDrawer();
            }
        }

        // Cmd/Ctrl + K: Focus prompt input
        if ((e.metaKey || e.ctrlKey) && e.key === 'k') {
            e.preventDefault();
            const promptInput = document.getElementById('prompt');
            promptInput.focus();
            promptInput.select();
        }

        // Tab navigation enhancement
        if (e.key === 'Tab') {
            document.body.classList.add('keyboard-nav');
        }
    });

    // Remove keyboard navigation class on mouse use
    document.addEventListener('mousedown', () => {
        document.body.classList.remove('keyboard-nav');
    });
}

// Accessibility enhancements
function initializeAccessibility() {
    // Add aria-live regions for dynamic content
    const statusMessage = document.getElementById('statusMessage');
    if (statusMessage) {
        statusMessage.setAttribute('aria-live', 'polite');
        statusMessage.setAttribute('aria-atomic', 'true');
    }

    // Enhance form labels and descriptions
    const inputs = document.querySelectorAll('input, textarea, select');
    inputs.forEach(input => {
        const label = document.querySelector(`label[for="${input.id}"]`);
        if (label && !label.id) {
            label.id = `label-${input.id}`;
            input.setAttribute('aria-labelledby', label.id);
        }

        // Add aria-describedby for help text
        const helpText = input.parentElement.querySelector('.help-text');
        if (helpText && !helpText.id) {
            const helpId = `help-${input.id}`;
            helpText.id = helpId;
            input.setAttribute('aria-describedby', helpId);
        }
    });

    // Add keyboard navigation to image gallery
    const updateImageAccessibility = () => {
        const images = document.querySelectorAll('.result-item img, .history-item img');
        images.forEach((img, index) => {
            if (!img.hasAttribute('tabindex')) {
                img.setAttribute('tabindex', '0');
                img.setAttribute('role', 'button');
                img.setAttribute('aria-label', `查看图像 ${index + 1}`);
                img.addEventListener('keydown', (e) => {
                    if (e.key === 'Enter' || e.key === ' ') {
                        e.preventDefault();
                        img.click();
                    }
                });
            }
        });
    };

    // Initial setup and observe for new images
    updateImageAccessibility();
    const observer = new MutationObserver(updateImageAccessibility);
    const currentResults = document.getElementById('currentResults');
    const historyGrid = document.getElementById('historyGrid');
    if (currentResults) observer.observe(currentResults, { childList: true, subtree: true });
    if (historyGrid) observer.observe(historyGrid, { childList: true, subtree: true });

    // Announce loading states
    const generateBtn = document.getElementById('generateBtn');
    if (generateBtn) {
        const buttonObserver = new MutationObserver((mutations) => {
            mutations.forEach((mutation) => {
                if (mutation.attributeName === 'aria-busy') {
                    const isBusy = generateBtn.getAttribute('aria-busy') === 'true';
                    generateBtn.setAttribute('aria-label',
                        isBusy ? '正在生成图像,请稍候...' : '生成图像');
                }
            });
        });
        buttonObserver.observe(generateBtn, { attributes: true });
    }
}

// Sync quick prompt to main prompt
function syncToMainPrompt() {
    const quickPrompt = document.getElementById('quickPrompt');
    const mainPrompt = document.getElementById('prompt');
    if (quickPrompt && mainPrompt) {
        mainPrompt.value = quickPrompt.value;
    }
}

// Quick Dock functionality for narrow screens
function generateFromDock() {
    const dockPrompt = document.getElementById('dockPrompt');
    const mainPrompt = document.getElementById('prompt');

    if (!dockPrompt.value.trim()) {
        showToast('请输入提示词', 'error');
        return;
    }

    // Copy dock prompt to main prompt
    mainPrompt.value = dockPrompt.value;

    // Switch to current results tab
    switchTab('current');

    // Trigger generation
    generateEdit();

    // Clear dock input after generation starts
    setTimeout(() => {
        dockPrompt.value = '';
    }, 500);
}

// Enhanced drawer functions with dock visibility control
function toggleDrawer() {
    const drawer = document.getElementById('drawer');
    const overlay = document.querySelector('.drawer-overlay');
    const quickDock = document.querySelector('.quick-dock');

    if (drawer.classList.contains('open')) {
        closeDrawer();
    } else {
        openDrawer();
    }
}

function openDrawer() {
    const drawer = document.getElementById('drawer');
    const overlay = document.querySelector('.drawer-overlay');
    const quickDock = document.querySelector('.quick-dock');

    drawer.classList.add('open');
    overlay.classList.add('show');
    document.body.classList.add('drawer-open');

    // Hide quick dock when drawer is open
    if (quickDock) {
        quickDock.style.display = 'none';
    }
}

function closeDrawer() {
    const drawer = document.getElementById('drawer');
    const overlay = document.querySelector('.drawer-overlay');
    const quickDock = document.querySelector('.quick-dock');

    drawer.classList.remove('open');
    overlay.classList.remove('show');
    document.body.classList.remove('drawer-open');

    // Restore quick dock visibility on narrow screens only
    if (quickDock && window.innerWidth <= 767) {
        quickDock.style.display = 'flex';
    }
}

// Auto-show drawer on first visit for narrow screens only
document.addEventListener('DOMContentLoaded', () => {
    if (window.innerWidth <= 767 && !localStorage.getItem('seenDrawer')) {
        setTimeout(() => {
            openDrawer();
            localStorage.setItem('seenDrawer', '1');
            showToast('👈 在侧栏中可以调整更多参数', 'info', 3000);
        }, 1000);
    }
});

// Handle Enter key in dock input
document.addEventListener('DOMContentLoaded', () => {
    const dockPrompt = document.getElementById('dockPrompt');
    if (dockPrompt) {
        dockPrompt.addEventListener('keypress', (e) => {
            if (e.key === 'Enter' && !e.shiftKey) {
                e.preventDefault();
                generateFromDock();
            }
        });
    }
});

// Drawer functionality for complete mobile experience
function toggleDrawerApiConfig() {
    const content = document.getElementById('drawerApiContent');
    const toggle = document.querySelector('#drawerApiCard .toggle-icon');

    if (content.style.display === 'none') {
        content.style.display = 'block';
        toggle.textContent = '▲';
    } else {
        content.style.display = 'none';
        toggle.textContent = '▼';
    }
}

function syncAndTestApiKey() {
    const drawerKey = document.getElementById('drawerApiKey').value;
    const mainKey = document.getElementById('apiKey');

    // Sync to main interface
    mainKey.value = drawerKey;

    // Trigger test on main interface
    const testBtn = document.getElementById('testApiKey');
    if (testBtn) testBtn.click();
}

function syncModelSelection() {
    const drawerModel = document.getElementById('drawerModelSelect').value;
    const mainModel = document.getElementById('modelSelect');

    // Sync to main interface
    mainModel.value = drawerModel;

    // Trigger model change event
    const event = new Event('change');
    mainModel.dispatchEvent(event);
}

function handleDrawerFileInput() {
    const drawerInput = document.getElementById('drawerFileInput');
    const mainInput = document.getElementById('fileInput');

    // Copy files to main input
    mainInput.files = drawerInput.files;

    // Trigger main file input change event
    const event = new Event('change');
    mainInput.dispatchEvent(event);
}

function generateFromDrawer() {
    // Sync SDE data from drawer if in SDE mode
    if (document.getElementById('enableSDE').checked) {
        syncSDEFromDrawer();
    } else {
        // Sync traditional prompt
        const drawerPrompt = document.getElementById('drawerPrompt').value;
        const mainPrompt = document.getElementById('prompt');
        mainPrompt.value = drawerPrompt;
    }

    // Close drawer and switch to results tab
    closeDrawer();
    switchTab('current');

    // Trigger generation
    generateEdit();
}

// Sync data between drawer and main interface when opening/closing
function openDrawer() {
    const drawer = document.getElementById('drawer');
    const overlay = document.querySelector('.drawer-overlay');
    const quickDock = document.querySelector('.quick-dock');

    // Sync current data to drawer before opening
    syncMainToDrawer();

    drawer.classList.add('open');
    overlay.classList.add('show');
    document.body.classList.add('drawer-open');

    // Hide quick dock when drawer is open
    if (quickDock) {
        quickDock.style.display = 'none';
    }
}

function syncMainToDrawer() {
    // Sync API key
    const mainKey = document.getElementById('apiKey').value;
    const drawerKey = document.getElementById('drawerApiKey');
    if (drawerKey) drawerKey.value = mainKey;

    // Sync model selection
    const mainModel = document.getElementById('modelSelect').value;
    const drawerModel = document.getElementById('drawerModelSelect');
    if (drawerModel) drawerModel.value = mainModel;

    // Sync prompt
    const mainPrompt = document.getElementById('prompt').value;
    const drawerPrompt = document.getElementById('drawerPrompt');
    if (drawerPrompt) drawerPrompt.value = mainPrompt;
}

// Settings Modal Functions
function toggleSettingsModal() {
    const modal = document.getElementById('settingsModal');
    if (modal) {
        modal.classList.toggle('show');
    }
}

function closeSettingsModal(event) {
    const modal = document.getElementById('settingsModal');
    if (modal && (event === undefined || event.target === modal)) {
        modal.classList.remove('show');
    }
}

// Close modal on Escape key
document.addEventListener('keydown', function(e) {
    if (e.key === 'Escape') {
        closeSettingsModal();
    }
});

// ============================= //
// Video Generation Support (WAN 2.2 / 2.5) //
// ============================= //

function updateVideoPriceEstimate() {
    const modelValue = modelSelect.value;
    const priceValueEl = document.getElementById('videoPriceValue');
    if (!priceValueEl) return;

    const isWan25 = modelValue.includes('wan-25-preview');
    const isWan22 = modelValue.includes('wan/v2.2');

    if (isWan25) {
        // WAN 2.5 pricing: based on resolution and duration
        const resolution = document.getElementById('videoResolution')?.value || '1080p';
        const duration = parseInt(document.getElementById('videoDuration')?.value || '5');

        const resolutionPrices = {
            '480p': 0.05,
            '720p': 0.10,
            '1080p': 0.15
        };

        const pricePerSecond = resolutionPrices[resolution] || 0.10;
        const totalPrice = (pricePerSecond * duration).toFixed(2);
        priceValueEl.textContent = `$${totalPrice} (${resolution} × ${duration}s)`;

    } else if (isWan22) {
        // WAN 2.2 pricing: based on video seconds calculated from frames/fps
        // FAL uses 16 FPS for billing calculation
        const numFrames = parseInt(document.getElementById('videoNumFrames')?.value || 81);
        const fps = parseInt(document.getElementById('videoFPS')?.value || 16);
        const resolution = document.getElementById('videoResolution')?.value || '720p';

        // Prevent division by zero and handle invalid values
        if (isNaN(numFrames) || isNaN(fps) || numFrames <= 0 || fps <= 0) {
            priceValueEl.textContent = '--';
            return;
        }

        const videoSeconds = numFrames / fps;
        const billingSeconds = numFrames / 16; // FAL uses 16 FPS for billing

        const resolutionRates = {
            '480p': 0.04,
            '580p': 0.06,
            '720p': 0.08
        };

        const rate = resolutionRates[resolution] || 0.06;
        const totalPrice = (rate * billingSeconds).toFixed(2);
        priceValueEl.textContent = `$${totalPrice} (${resolution}, ~${videoSeconds.toFixed(1)}s 实际 / ${billingSeconds.toFixed(1)}s 计费@16FPS)`;
    }
}

function isVideoModel(modelValue) {
    return modelValue.includes('wan-25-preview') || modelValue.includes('wan/v2.2');
}

function buildVideoParams() {
    const modelValue = modelSelect.value;
    const isWan25 = modelValue.includes('wan-25-preview');
    const isWan22 = modelValue.includes('wan/v2.2');
    const params = {};

    // Common video params
    const resolution = document.getElementById('videoResolution')?.value;
    if (resolution) params.resolution = resolution;

    const negativePrompt = document.getElementById('videoNegativePrompt')?.value;
    if (negativePrompt) params.negative_prompt = negativePrompt;

    if (isWan25) {
        // WAN 2.5 specific params
        const duration = document.getElementById('videoDuration')?.value;
        if (duration) params.duration = duration;

        const audioUrl = document.getElementById('videoAudioUrl')?.value;
        if (audioUrl) params.audio_url = audioUrl;

        params.enable_prompt_expansion = true; // WAN 2.5 defaults to true
    } else if (isWan22) {
        // WAN 2.2 specific params
        const fps = document.getElementById('videoFPS')?.value;
        if (fps) params.frames_per_second = parseInt(fps);

        const numFrames = document.getElementById('videoNumFrames')?.value;
        if (numFrames) params.num_frames = parseInt(numFrames);

        const safetyCheckerEl = document.getElementById('videoSafetyChecker');
        params.enable_safety_checker = safetyCheckerEl ? safetyCheckerEl.checked : true;
    }

    return params;
}

function downloadVideo(videoUrl, filename) {
    const link = document.createElement('a');
    link.href = videoUrl;
    link.download = `${filename || 'video'}-${Date.now()}.mp4`;
    document.body.appendChild(link);
    link.click();
    document.body.removeChild(link);
    showToast('视频下载已开始', 'success', 2000);
}