File size: 142,276 Bytes
ef387e3
4f163ba
 
9a0dd41
4f163ba
 
 
 
 
 
 
 
 
 
 
 
c22763f
4f163ba
c22763f
4f163ba
 
 
 
 
c22763f
4f163ba
c22763f
 
 
 
 
 
 
 
 
 
4f163ba
 
 
 
 
 
c22763f
 
4f163ba
c22763f
 
 
 
 
 
 
 
 
4f163ba
 
 
c22763f
 
 
 
 
4f163ba
c22763f
 
 
 
1aa7dd8
c22763f
a603f48
370c00f
c22763f
 
 
 
 
 
dc9c35b
370c00f
c22763f
ffa2a7f
7640732
 
4303396
1ae1cc1
 
 
 
 
 
 
 
 
 
 
 
7a841b5
 
 
 
 
 
 
22f9665
7a841b5
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
22f9665
7a841b5
 
 
4e08c38
7cfba08
 
c91c6a9
7cfba08
4e08c38
 
 
 
 
 
 
76f4561
4e08c38
 
 
 
 
 
 
76f4561
4e08c38
 
122d930
 
 
 
 
 
 
76f4561
122d930
 
 
 
 
 
 
76f4561
122d930
 
dc9c35b
 
 
 
 
 
 
76f4561
dc9c35b
 
 
 
 
 
 
76f4561
dc9c35b
 
2b01575
 
 
d8c2add
3e3b2ab
1b274b8
769216b
95088a7
 
 
 
 
7640732
 
 
 
 
 
 
 
769216b
 
 
 
 
 
 
 
18f6628
 
d8cb4b7
76f4561
18f6628
 
cb3ea5d
769216b
 
 
 
 
 
c22763f
ef387e3
c22763f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
7a5ce86
4f163ba
c22763f
4f163ba
c22763f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
0e00d36
c22763f
 
 
 
 
 
 
 
 
 
 
 
0e00d36
c22763f
 
 
 
 
 
 
 
 
 
c91c6a9
75ab062
 
5093374
75ab062
 
0e00d36
 
 
 
 
75ab062
c22763f
 
 
 
0e00d36
 
c22763f
75ab062
c22763f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
0e00d36
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
c22763f
0e00d36
c22763f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
4f163ba
c22763f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
9a0dd41
4b427a2
92b4c1c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
9a0dd41
4b427a2
 
9a0dd41
4b427a2
 
 
 
92b4c1c
 
 
 
4b427a2
 
92b4c1c
 
 
4b427a2
 
 
 
 
 
 
 
 
 
92b4c1c
c22763f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
4f163ba
c22763f
 
4f163ba
c22763f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
4f163ba
c22763f
 
 
 
4f163ba
c22763f
 
 
 
4f163ba
 
 
c22763f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
4f163ba
c22763f
4f163ba
c22763f
 
 
 
 
 
 
 
 
 
 
 
 
 
4f163ba
 
c22763f
 
 
 
 
 
 
 
 
 
 
4f163ba
 
 
c22763f
95ade26
 
 
 
c22763f
 
 
4f163ba
c22763f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
4f163ba
c22763f
4f163ba
c22763f
4f163ba
c22763f
4f163ba
c22763f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
4f163ba
c22763f
 
 
 
 
4f163ba
c22763f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
4f163ba
c22763f
 
 
0961d86
4303396
 
 
 
 
4f163ba
 
 
 
a603f48
c91c6a9
ee3b48f
 
 
4e08c38
122d930
dc9c35b
769216b
95ade26
3ee0d07
4f163ba
cb3ea5d
fc30ad4
 
6e4148b
fc30ad4
 
6e4148b
 
 
fc30ad4
 
76f4561
4f163ba
 
0c58bd0
 
 
cb350e9
95ade26
 
cb350e9
 
95ade26
 
0c58bd0
ecb4029
 
 
6ea7288
bb98e5c
6ea7288
 
bb98e5c
 
 
2c37db4
22f9665
7640732
 
ecb4029
22f9665
 
 
 
 
b55b7b6
5a11b0a
 
4861cf5
 
 
ffa2a7f
ecb4029
 
 
4861cf5
ecb4029
4861cf5
ecb4029
5a11b0a
d2d3db1
 
4861cf5
 
 
 
d2d3db1
c7634ca
 
2c37db4
68e3f4c
 
 
 
 
 
 
d2d3db1
 
 
c7634ca
2c37db4
68e3f4c
 
 
 
 
 
 
d2d3db1
 
4861cf5
 
ecb4029
4f163ba
76f4561
4f163ba
 
76f4561
c22763f
 
769216b
95ade26
3ee0d07
1d170e0
 
ffa2a7f
 
122d930
 
dc9c35b
d8c2add
18f6628
1d170e0
 
 
 
 
 
a603f48
4f163ba
 
 
c22763f
 
4f163ba
4303396
 
 
4f163ba
 
c22763f
 
 
4f163ba
c22763f
4303396
4f163ba
 
c22763f
 
 
 
58a7c18
 
c22763f
 
4f163ba
c22763f
4f163ba
 
c22763f
 
4f163ba
 
 
4303396
 
 
 
 
122d930
4f163ba
 
 
a603f48
c91c6a9
4e08c38
f03f511
 
 
 
cb3ea5d
c22763f
 
4303396
76f4561
58a7c18
ecb4029
 
0c58bd0
 
 
 
 
 
 
ecb4029
c7634ca
 
 
 
 
 
 
 
 
 
 
ecb4029
22f9665
 
 
 
 
 
 
ecb4029
 
 
af7b5ac
 
 
 
 
 
 
 
 
 
 
 
ecb4029
 
 
 
ffd3124
ecb4029
c22763f
 
9c091e0
 
76f4561
4f163ba
c22763f
122d930
 
5093374
 
 
 
 
 
 
 
68e3f4c
 
 
 
 
 
 
5093374
 
 
 
 
 
 
 
 
af7b5ac
5093374
 
af7b5ac
5093374
af7b5ac
a603f48
122d930
4f163ba
 
4303396
 
 
c22763f
4303396
c22763f
4303396
c22763f
4f163ba
 
 
c22763f
 
 
 
 
 
 
 
 
0eee535
c22763f
 
 
 
 
 
 
 
4f163ba
 
c22763f
 
 
 
 
 
 
 
 
4f163ba
c22763f
4f163ba
c22763f
 
 
 
 
 
 
 
 
 
 
4f163ba
c22763f
 
 
 
869b241
c22763f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
9a0dd41
 
 
 
 
 
 
 
 
 
 
4b427a2
 
 
 
 
 
9a0dd41
 
 
92b4c1c
 
 
 
 
 
 
 
 
 
 
 
4b427a2
9a0dd41
 
c22763f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
9a0dd41
 
c22763f
9a0dd41
c22763f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
4303396
c22763f
4303396
c22763f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
9c091e0
c22763f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
9c091e0
 
c22763f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
9c091e0
 
c22763f
9c091e0
c22763f
4303396
869b241
c22763f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
9c091e0
 
c22763f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
9c091e0
c22763f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
9c091e0
c22763f
9c091e0
c22763f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
29d4d6d
c22763f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
9a0dd41
 
 
 
 
 
 
 
 
c22763f
 
 
 
 
 
 
 
 
 
9a0dd41
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
9a0a1b0
 
 
9a0dd41
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
9a0a1b0
 
 
 
9a0dd41
 
 
 
c22763f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
a603f48
 
c22763f
 
 
 
 
 
 
 
 
 
 
 
 
 
9a0dd41
 
c22763f
 
9c091e0
c22763f
 
 
 
9c091e0
c22763f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
4f163ba
c22763f
 
4f163ba
 
 
c22763f
 
 
 
 
 
 
 
 
 
 
4303396
c22763f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
4f163ba
c22763f
 
 
 
 
 
4f163ba
c22763f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
9c091e0
 
c22763f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
9c091e0
c22763f
 
 
 
 
 
 
 
 
 
9c091e0
c22763f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
9c091e0
c22763f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
c9ec868
c22763f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
9c091e0
 
c22763f
 
 
 
 
 
 
 
 
 
 
 
 
 
9c091e0
2ff05f3
c22763f
 
 
4f163ba
9c091e0
c22763f
 
 
9c091e0
c22763f
 
 
 
 
 
 
 
4f163ba
 
c22763f
 
4f163ba
c22763f
 
 
 
 
cb350e9
c22763f
 
 
 
0eee535
c22763f
0eee535
c22763f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
09842d2
5a11b0a
 
c22763f
cb3ea5d
c22763f
 
 
 
 
 
 
 
 
 
 
 
5ed04ca
c22763f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
122d930
c22763f
 
 
 
 
4f163ba
 
c22763f
 
09842d2
c22763f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
9c091e0
c22763f
 
 
 
 
 
 
 
 
 
4f163ba
c22763f
 
 
 
9c091e0
c22763f
 
4f163ba
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
c22763f
4f163ba
 
 
 
c22763f
 
4f163ba
 
 
 
 
 
 
 
 
 
 
c22763f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
9a5548c
 
 
 
 
 
 
 
 
c22763f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
9a5548c
 
 
c22763f
 
9a5548c
c22763f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import React, { useState, useEffect, useCallback, useRef, useLayoutEffect } from 'react';
import { useNavigate } from 'react-router-dom';
import { api } from '../services/api';
import TutorialRefinity from '../components/TutorialRefinity';
import { 
  AcademicCapIcon, 
  DocumentTextIcon, 
  CheckCircleIcon,
  ClockIcon,
  ArrowRightIcon,
  PencilIcon,
  XMarkIcon,
  CheckIcon,
  PlusIcon,
  TrashIcon,
  ArrowTopRightOnSquareIcon,
  ArrowsRightLeftIcon
} from '@heroicons/react/24/outline';
// import ReactDOM from 'react-dom';

interface TutorialTask {
  _id: string;
  content: string;
  weekNumber: number;
  translationBrief?: string;
  imageUrl?: string;
  imageAlt?: string;
  imageSize?: number;
  imageAlignment?: 'left' | 'center' | 'right' | 'portrait-split';
  position?: number;
}

interface TutorialWeek {
  weekNumber: number;
  translationBrief?: string;
  tasks: TutorialTask[];
}

interface UserSubmission {
  _id: string;
  transcreation: string;
  status: string;
  score: number;
  groupNumber?: number;
  isOwner?: boolean;
  userId?: {
    _id: string;
    username: string;
  };
  voteCounts: {
    '1': number;
    '2': number;
    '3': number;
  };
}

const TutorialTasks: React.FC = () => {
  const [selectedWeek, setSelectedWeek] = useState<number>(() => {
    const savedWeek = localStorage.getItem('selectedTutorialWeek');
    return savedWeek ? parseInt(savedWeek) : 1;
  });
  const [isWeekTransitioning, setIsWeekTransitioning] = useState(false);
  const [tutorialTasks, setTutorialTasks] = useState<TutorialTask[]>([]);
  const [tutorialWeek, setTutorialWeek] = useState<TutorialWeek | null>(null);
  const [userSubmissions, setUserSubmissions] = useState<{[key: string]: UserSubmission[]}>({});
  const [loading, setLoading] = useState(true);
  const [submitting, setSubmitting] = useState<{[key: string]: boolean}>({});
  const [isWeekHidden, setIsWeekHidden] = useState<boolean>(false);
  const [translationText, setTranslationText] = useState<{[key: string]: string}>({});
  const [mutatingTaskId, setMutatingTaskId] = useState<string | null>(null);
  const [spacerHeights, setSpacerHeights] = useState<{[key: string]: number}>({});
  
  const [expandedSections, setExpandedSections] = useState<{[key: string]: boolean}>({});
  const cardRefs = useRef<{[key: string]: HTMLDivElement | null}>({});
  const briefRef = useRef<HTMLDivElement | null>(null);
  const groupDocRef = useRef<HTMLDivElement | null>(null);
  const listRef = useRef<HTMLDivElement | null>(null);
  const submissionsGridRefs = useRef<{[key: string]: HTMLDivElement | null}>({});
  const submissionsContainerRefs = useRef<{[key: string]: HTMLDivElement | null}>({});
  const withPreservedScroll = useRef<(fn: () => void) => void>();
  const pendingUnlocksRef = useRef<Set<string>>(new Set());
  const lastPreHeightRef = useRef<{[key: string]: number}>({});
  const disableCompensationRef = useRef<Set<string>>(new Set());

  const setGlobalAnchorDisabled = (disabled: boolean) => {
    try {
      if (disabled) {
        document.documentElement.style.setProperty('overflow-anchor', 'none');
        document.body.style.setProperty('overflow-anchor', 'none');
      } else {
        document.documentElement.style.removeProperty('overflow-anchor');
        document.body.style.removeProperty('overflow-anchor');
      }
    } catch {}
  };

  // Last-resort scroll freeze during tiny mutation window (no visual/layout change)
  const scrollLockState = useRef<{ y: number } | null>(null);
  const freezeScroll = () => {
    try {
      if (scrollLockState.current) return; // already frozen
      const y = window.scrollY;
      scrollLockState.current = { y };
      try { console.log('[Trace] Safari:freezeScroll', { y }); } catch {}
      const body = document.body as HTMLElement;
      body.style.position = 'fixed';
      body.style.top = `-${y}px`;
      body.style.width = '100%';
      body.style.overflowY = 'scroll';
    } catch {}
  };
  const unfreezeScroll = () => {
    try {
      const state = scrollLockState.current;
      if (!state) return;
      const body = document.body as HTMLElement;
      body.style.position = '';
      body.style.top = '';
      body.style.width = '';
      body.style.overflowY = '';
      window.scrollTo(0, state.y);
      scrollLockState.current = null;
      try { console.log('[Trace] Safari:unfreezeScroll'); } catch {}
    } catch {}
  };

  // Minimal, local scroll-stability helpers for submit/delete
  // Basic UA check; Chrome on iOS uses WKWebView but includes 'CriOS'
  const isSafari = typeof navigator !== 'undefined' && /Safari\//.test(navigator.userAgent) && !/Chrome\//.test(navigator.userAgent) && !/CriOS\//.test(navigator.userAgent);
  const SAFARI_FREEZE_ENABLED = false; // disable body-freeze so scroll compensation can apply

  const lockListHeight = () => {
    const el = listRef.current;
    if (!el) return;
    const h = el.getBoundingClientRect().height;
    el.style.minHeight = `${h}px`;
    el.style.height = `${h}px`;
    el.style.overflow = 'hidden';
    try { console.log('[Trace] lockListHeight', { h }); } catch {}
  };
  const unlockListHeight = () => {
    const el = listRef.current;
    if (!el) return;
    el.style.overflow = '';
    el.style.height = '';
    el.style.minHeight = '';
    try { console.log('[Trace] unlockListHeight'); } catch {}
  };

  const lockCardHeightById = (id: string) => {
    const el = cardRefs.current[id];
    if (!el) return;
    const h = el.getBoundingClientRect().height;
    el.style.minHeight = `${h}px`;
    el.style.height = `${h}px`;
    el.style.overflow = 'hidden';
    try { console.log('[Trace] lockCardHeight', { id, h }); } catch {}
  };
  const unlockCardHeightById = (id: string) => {
    const el = cardRefs.current[id];
    if (!el) return;
    el.style.overflow = '';
    el.style.height = '';
    el.style.minHeight = '';
    try { console.log('[Trace] unlockCardHeight', { id }); } catch {}
  };

  const lockGridHeightById = (id: string) => {
    const el = submissionsGridRefs.current[id];
    if (!el) return;
    const h = el.getBoundingClientRect().height;
    el.style.minHeight = `${h}px`;
    el.style.height = `${h}px`;
    el.style.overflow = 'hidden';
    try { console.log('[Trace] lockGridHeight', { id, h }); } catch {}
  };
  const unlockGridHeightById = (id: string) => {
    const el = submissionsGridRefs.current[id];
    if (!el) return;
    el.style.overflow = '';
    el.style.height = '';
    el.style.minHeight = '';
    try { console.log('[Trace] unlockGridHeight', { id }); } catch {}
  };

  // No-op container height locks (reverted)
  const lockContainerHeightById = (_id: string) => {};
  const unlockContainerHeightById = (_id: string) => {};

  // (removed) ResizeObserver scroll compensator

  const withPreservedCardOffset = (taskId: string, fn: () => void) => {
    // If scroll is frozen, avoid additional scroll adjustments that can fight WebKit
    if (scrollLockState.current) {
      fn();
      return;
    }
    if (disableCompensationRef.current.has(taskId)) {
      fn();
      return;
    }
    if ((lastPreHeightRef.current[taskId] || 0) === 0) {
      fn();
      return;
    }
    const el = cardRefs.current[taskId];
    const topBefore = el ? el.getBoundingClientRect().top : null;
    const scrollYBefore = window.scrollY;
    fn();
    requestAnimationFrame(() => {
      const topAfter = el ? el.getBoundingClientRect().top : null;
      if (topBefore !== null && topAfter !== null) {
        const delta = topAfter - topBefore;
        const clampedDelta = Math.max(-150, Math.min(150, delta));
        const allowComp = isSafari ? Math.abs(clampedDelta) > 0 : true;
        if (delta !== 0 && allowComp) {
          try { console.log('[Trace] ScrollPreserve', { taskId, delta, topBefore, topAfter, scrollYBefore }); } catch {}
          // Invert clamped delta to counteract element movement (keep card anchored)
          window.scrollBy(0, -clampedDelta);
        }
      } else {
        window.scrollTo(0, scrollYBefore);
      }
    });
  };

  // Initialize scroll preservation helper once
  useLayoutEffect(() => {
    withPreservedScroll.current = (fn: () => void) => {
      try {
        const y = window.scrollY;
        fn();
        // Restore scroll position after DOM updates with multiple frames
        requestAnimationFrame(() => {
          requestAnimationFrame(() => {
            requestAnimationFrame(() => {
              window.scrollTo(0, y);
            });
          });
        });
      } catch {
        fn();
      }
    };
  }, []);

  // Measure static containers once to avoid reflow jumps (Week 1 only prominent)

  // Move a task up or down by normalizing positions for the current visible list (weeks 4–6 only)
  const moveTask = async (taskId: string, direction: 'up' | 'down') => {
    try {
      const viewMode = (localStorage.getItem('viewMode') || 'auto');
      const actualRole = (JSON.parse(localStorage.getItem('user') || '{}').role);
      const isAdmin = viewMode === 'student' ? false : actualRole === 'admin';
      if (!isAdmin || selectedWeek < 4) return;
      // Build ordered list for the current week from what is rendered
      const current = tutorialTasks.filter(t => t.weekNumber === selectedWeek);
      const index = current.findIndex(t => t._id === taskId);
      if (index === -1) return;
      const targetIndex = direction === 'up' ? index - 1 : index + 1;
      if (targetIndex < 0 || targetIndex >= current.length) return;

      // Normalize positions to 0..n-1 based on current screen order
      const normalized = current.map((t, i) => ({ id: t._id, position: i }));
      // Swap the two entries (calculate new positions first)
      const posA = normalized[index].position;
      const posB = normalized[targetIndex].position;
      normalized[index].position = posB;
      normalized[targetIndex].position = posA;

      // Optimistic UI update: swap in local state immediately for smoother UX
      // const prevState = tutorialTasks; // not used
      setTutorialTasks((prev) => {
        const next = [...prev];
        // find actual indices in full list and swap their relative order by updating their position fields
        const aId = normalized[index].id;
        const bId = normalized[targetIndex].id;
        return next.map(item => {
          if (item._id === aId) return { ...item, position: posB } as any;
          if (item._id === bId) return { ...item, position: posA } as any;
          return item;
        });
      });

      // Send both updates in parallel; if either fails, revert then refetch
      await Promise.all([
        api.put(`/api/auth/admin/tutorial-tasks/${normalized[index].id}/position`, { position: posB }),
        api.put(`/api/auth/admin/tutorial-tasks/${normalized[targetIndex].id}/position`, { position: posA })
      ]);
      // Light refresh to ensure list order is consistent with server
      fetchTutorialTasks(false);
    } catch (error) {
      console.error('Reorder failed', error);
    }
  };

  
  const [selectedGroups, setSelectedGroups] = useState<{[key: string]: number}>({});
  
  const GroupDocSection: React.FC<{ weekNumber: number }> = ({ weekNumber }) => {
    const containerRef = useRef<HTMLDivElement | null>(null);
    const [group, setGroup] = useState<number>(() => {
      const saved = localStorage.getItem(`tutorial_group_${weekNumber}`);
      return saved ? parseInt(saved) : 1;
    });
    const [creating, setCreating] = useState(false);
    const [docs, setDocs] = useState<any[]>([]);
    const [urlInput, setUrlInput] = useState<string>('');
    const [errorMsg, setErrorMsg] = useState<string>('');
    const [copiedLink, setCopiedLink] = useState<string>('');
    const viewMode = (localStorage.getItem('viewMode') || 'auto');
    const actualRole = (JSON.parse(localStorage.getItem('user') || '{}').role);
    const isAdmin = viewMode === 'student' ? false : actualRole === 'admin';
    const isDocsFetchInFlightRef = useRef(false);

    const CopySquaresIcon: React.FC<{ className?: string }> = ({ className }) => (
      <svg className={className || 'h-4 w-4'} viewBox="0 0 24 24" fill="none" stroke="currentColor" strokeWidth="1.5" xmlns="http://www.w3.org/2000/svg">
        <rect x="8" y="8" width="12" height="12" rx="2"/>
        <rect x="4" y="4" width="12" height="12" rx="2"/>
      </svg>
    );

    const loadDocs = useCallback(async () => {
      try {
        // throttle fetch during and just after mutations to avoid reflow interference
        if (mutatingTaskId) {
          try { console.log('[Trace] Docs:skipDuringMutate', { weekNumber, mutatingTaskId }); } catch {}
          setTimeout(() => { if (!mutatingTaskId) loadDocs(); }, 600);
          return;
        }
        if (isDocsFetchInFlightRef.current) {
          try { console.log('[Trace] Docs:skipInFlight', { weekNumber }); } catch {}
          return;
        }
        isDocsFetchInFlightRef.current = true;
        try { console.log('[Trace] Docs:fetch', { weekNumber }); } catch {}
        const resp = await api.get(`/api/docs/list?weekNumber=${weekNumber}`);
        setDocs(resp.data?.docs || []);
      } catch (e) {
        setDocs([]);
      } finally {
        isDocsFetchInFlightRef.current = false;
      }
    }, [weekNumber, mutatingTaskId]);

    useEffect(() => { loadDocs(); }, [loadDocs]);

    const current = docs.find(d => d.groupNumber === group);

    const createDoc = async () => {
      try {
        setCreating(true);
        setErrorMsg('');
        const url = urlInput.trim();
        if (!url) {
          setErrorMsg('Please paste a Google Doc link.');
          return;
        }
        const isValid = /docs\.google\.com\/document\/d\//.test(url);
        if (!isValid) {
          setErrorMsg('Provide a valid Google Doc link (docs.google.com/document/d/...).');
          return;
        }
        await api.post('/api/docs/create', { weekNumber, groupNumber: group, docUrl: url });
        await loadDocs();
        setUrlInput('');
      } finally {
        setCreating(false);
      }
    };

    const copyLink = async (link: string) => {
      try {
        await navigator.clipboard.writeText(link);
        // Persist until refresh: do not clear after timeout
        setCopiedLink(link);
      } catch {}
    };

    useEffect(() => {
      const el = containerRef.current;
      if (!el) return;
      if (mutatingTaskId) {
        const h = el.getBoundingClientRect().height;
        try { console.log('[Trace] Docs:lockHeight', { h }); } catch {}
        el.style.minHeight = `${h}px`;
        el.style.height = `${h}px`;
        el.style.overflow = 'hidden';
      } else {
        el.style.minHeight = '';
        el.style.height = '';
        el.style.overflow = '';
        try { console.log('[Trace] Docs:unlockHeight'); } catch {}
      }
    }, [mutatingTaskId]);

    return (
      <div ref={containerRef}>
        {/* Top control row */}
        {isAdmin && (
          <div className="mb-4 max-w-2xl">
            <label className="text-sm text-gray-700 block mb-1">Group</label>
            <select
              value={group}
              onChange={(e) => {
                const g = parseInt(e.target.value);
                setGroup(g);
                localStorage.setItem(`tutorial_group_${weekNumber}`, String(g));
              }}
              className="w-full px-3 py-2 border rounded-md text-sm"
            >
              {[1,2,3,4,5,6,7,8].map(g => <option key={g} value={g}>Group {g}</option>)}
            </select>
          </div>
        )}

        {/* Replace / Add link inline editor */}
        {isAdmin && (
          <div className="mb-4 max-w-2xl">
            <div className="flex items-center gap-2">
              <input
                type="url"
                value={urlInput}
                onChange={(e) => { setUrlInput(e.target.value); setErrorMsg(''); }}
                placeholder="Paste Google Doc link (docs.google.com/document/d/...)"
                className="flex-1 px-3 py-2 border border-gray-300 rounded-md text-sm"
              />
              <button onClick={createDoc} disabled={creating} className="relative inline-flex items-center justify-center gap-2 px-4 py-2 rounded-2xl text-sm font-medium text-white ring-1 ring-inset ring-white/50 backdrop-blur-md backdrop-brightness-110 backdrop-saturate-150 shadow-[inset_0_1px_0_rgba(255,255,255,0.6),inset_0_-1px_0_rgba(0,0,0,0.12)] bg-sky-600/70 disabled:bg-gray-400">{creating ? 'Saving…' : (current ? 'Save new link' : 'Add Doc Link')}</button>
            </div>
            {errorMsg && <div className="mt-1 text-xs text-red-600">{errorMsg}</div>}
          </div>
        )}

        {/* All groups table */}
        <div className="mt-6 max-w-2xl">
          <h5 className="text-sm font-semibold text-gray-800 uppercase tracking-wide mb-2">All Groups</h5>
          <div className="overflow-hidden rounded-lg border border-gray-200">
            <table className="min-w-full text-sm">
              <thead className="bg-gray-50 text-gray-600">
                <tr>
                  <th className="px-4 py-2 text-left">Group</th>
                  <th className="px-4 py-2 text-left">Actions</th>
                </tr>
              </thead>
              <tbody className="divide-y divide-gray-200">
                {docs.length === 0 && (
                  <tr><td colSpan={2} className="px-4 py-3 text-gray-500">No group docs yet.</td></tr>
                )}
                {docs.map(d => (
                  <tr key={d._id}>
                    <td className="px-4 py-3">Group {d.groupNumber}</td>
                    <td className="px-4 py-3">
                      <div className="flex items-center gap-4">
                        <a href={d.docUrl} target="_blank" rel="noreferrer" className="inline-flex items-center gap-1 text-ui-neonBlue">
                          <ArrowTopRightOnSquareIcon className="h-4 w-4" /> Open
                        </a>
                        <button onClick={() => copyLink(d.docUrl)} className="inline-flex items-center gap-1 text-ui-neonBlue">
                          <CopySquaresIcon className="h-4 w-4" />
                          <span className="inline-block w-14 text-left">{copiedLink === d.docUrl ? 'Copied' : 'Copy'}</span>
                        </button>
                        {isAdmin && (
                          <>
                            <button onClick={() => { setGroup(d.groupNumber); setUrlInput(d.docUrl || ''); }} className="inline-flex items-center gap-1 text-gray-700">
                            <ArrowsRightLeftIcon className="h-4 w-4" /> Edit
                          </button>
                            <button
                              onClick={async () => {
                                try {
                                  await api.delete(`/api/docs/${d._id}`);
                                  await loadDocs();
                                } catch (e) {
                                  console.error('Delete failed', e);
                                }
                              }}
                              className="inline-flex items-center gap-1 text-red-600"
                            >
                              <TrashIcon className="h-4 w-4" /> Delete
                            </button>
                          </>
                        )}
                      </div>
                    </td>
                  </tr>
                ))}
              </tbody>
            </table>
          </div>
        </div>
      </div>
    );
  };

  // Basic inline formatting helpers (bold/italic via simple markdown) for weeks 4–6
  const renderFormatted = (text: string) => {
    const escape = (s: string) => s.replace(/&/g, '&amp;').replace(/</g, '&lt;').replace(/>/g, '&gt;');
    // Auto-linker: supports [label](url), plain URLs, and www.* without touching existing href attributes
    const html = escape(text)
      // Markdown-style links: [label](https://example.com)
      .replace(/\[([^\]]+?)\]\((https?:\/\/[^\s)]+)\)/g, '<a class="text-indigo-600 underline" href="$2" target="_blank" rel="noopener noreferrer">$1</a>')
      // Plain URLs with protocol, avoid matching inside attributes (require a non-attribute preceding char)
      .replace(/(^|[^=\"'\/:])(https?:\/\/[^\s<]+)/g, (m, p1, url) => `${p1}<a class="text-indigo-600 underline" href="${url}" target="_blank" rel="noopener noreferrer">${url}</a>`)
      // www.* domains (prepend https://), also avoid attributes
      .replace(/(^|[^=\"'\/:])(www\.[^\s<]+)/g, (m, p1, host) => `${p1}<a class="text-indigo-600 underline" href="https://${host}" target="_blank" rel="noopener noreferrer">${host}</a>`)
      .replace(/\*\*(.+?)\*\*/g, '<strong>$1</strong>')
      .replace(/\*(.+?)\*/g, '<em>$1</em>')
      .replace(/\n/g, '<br/>');
    return <span dangerouslySetInnerHTML={{ __html: html.replace(/text-indigo-600/g, 'text-ui-neonBlue').replace(/text-indigo-900/g, 'text-ui-text') }} />;
  };

  const applyLinkFormat = (
    elementId: string,
    current: string,
    setValue: (v: string) => void
  ) => {
    const urlInput = window.prompt('Enter URL (e.g., https://example.com):');
    if (!urlInput) return;
    // Sanitize URL: ensure protocol, and strip accidental trailing quotes/attributes pasted from elsewhere
    let url = /^https?:\/\//i.test(urlInput) ? urlInput : `https://${urlInput}`;
    url = url.replace(/["'>)\s]+$/g, '');
    const el = document.getElementById(elementId) as HTMLTextAreaElement | null;
    if (!el) {
      setValue(`${current}[link](${url})`);
      return;
    }
    const start = el.selectionStart ?? current.length;
    const end = el.selectionEnd ?? current.length;
    const before = current.slice(0, start);
    const selection = current.slice(start, end) || 'link';
    const after = current.slice(end);
    setValue(`${before}[${selection}](${url})${after}`);
    // Restore focus and selection
    setTimeout(() => {
      el.focus();
      const newPos = before.length + selection.length + 4 + url.length + 2; // rough caret placement
      try { el.setSelectionRange(newPos, newPos); } catch {}
    }, 0);
  };

  const applyInlineFormat = (
    elementId: string,
    current: string,
    setValue: (v: string) => void,
    wrapper: '**' | '*'
  ) => {
    const el = document.getElementById(elementId) as HTMLTextAreaElement | null;
    if (!el) {
      setValue(current + wrapper + wrapper);
      return;
    }
    const start = el.selectionStart ?? current.length;
    const end = el.selectionEnd ?? current.length;
    const before = current.slice(0, start);
    const selection = current.slice(start, end);
    const after = current.slice(end);
    const next = `${before}${wrapper}${selection}${wrapper}${after}`;
    setValue(next);
    setTimeout(() => {
      try {
        el.focus();
        el.selectionStart = start + wrapper.length;
        el.selectionEnd = end + wrapper.length;
      } catch {}
    }, 0);
  };

  const [editingTask, setEditingTask] = useState<string | null>(null);
  const [editingBrief, setEditingBrief] = useState<{[key: number]: boolean}>({});
  const [addingTask, setAddingTask] = useState<boolean>(false);
  const [addingImage, setAddingImage] = useState<boolean>(false);
  const [addingRevision, setAddingRevision] = useState<boolean>(false);
  const [revisionAdded, setRevisionAdded] = useState<boolean>(false);
  const [hasRevisionTasks, setHasRevisionTasks] = useState<boolean>(false);
  
  // Check if Deep Revision tasks exist for the current week
  React.useEffect(() => {
    const checkForRevisionTasks = async () => {
      if (selectedWeek < 5) {
        setHasRevisionTasks(false);
        return;
      }
      try {
        // Use the same API base detection as other parts of the app
        const base = (((api.defaults as any)?.baseURL as string) || '').replace(/\/$/, '');
        const tasksUrl = `${base}/api/tutorial-refinity/tasks?weekNumber=${selectedWeek}`;
        const user = JSON.parse(localStorage.getItem('user') || '{}');
        const headers: any = {};
        if (user.name) headers['x-user-name'] = user.name;
        if (user.email) headers['x-user-email'] = user.email;
        if (user.role === 'admin') {
          headers['x-user-role'] = 'admin';
          headers['user-role'] = 'admin';
        }
        const resp = await fetch(tasksUrl, { headers });
        const data: any[] = await resp.json().catch(()=>[]);
        const hasTasks = Array.isArray(data) && data.length > 0;
        setHasRevisionTasks(hasTasks);
        // If tasks exist, automatically show the revision module
        if (hasTasks) {
          setRevisionAdded(true);
          // Also save to localStorage for consistency
          try {
            localStorage.setItem(`tutorial_revision_added_week_${selectedWeek}`, 'true');
          } catch (e) {
            // Ignore localStorage errors
          }
        }
      } catch (e) {
        console.warn('Failed to check for revision tasks:', e);
        setHasRevisionTasks(false);
      }
    };
    
    checkForRevisionTasks();
  }, [selectedWeek]);
  
  // Initialize and update revisionAdded state from localStorage (Safari-compatible)
  // Use useEffect instead of useState initializer for better Safari compatibility
  React.useEffect(() => {
    const checkRevisionState = () => {
      try {
        const saved = localStorage.getItem(`tutorial_revision_added_week_${selectedWeek}`);
        const isAdded = saved === 'true';
        // Only set from localStorage if tasks don't exist (to avoid overriding the auto-show logic)
        if (!hasRevisionTasks) {
          setRevisionAdded(isAdded);
        }
      } catch (e) {
        // Safari might block localStorage in some contexts, fallback to false
        if (!hasRevisionTasks) {
          setRevisionAdded(false);
        }
      }
    };
    
    // Check immediately
    checkRevisionState();
    
    // Also check after a short delay for Safari (in case localStorage isn't ready immediately)
    const timeoutId = setTimeout(checkRevisionState, 100);
    
    return () => clearTimeout(timeoutId);
  }, [selectedWeek, hasRevisionTasks]);
  const [editForm, setEditForm] = useState<{
    content: string;
    translationBrief: string;
    imageUrl: string;
    imageAlt: string;
  }>({
    content: '',
    translationBrief: '',
    imageUrl: '',
    imageAlt: ''
  });
  const [imageForm, setImageForm] = useState<{
    imageUrl: string;
    imageAlt: string;
    imageSize: number;
  imageAlignment: 'left' | 'center' | 'right' | 'portrait-split';
  }>({
    imageUrl: '',
    imageAlt: '',
    imageSize: 200,
    imageAlignment: 'center'
  });
  const [selectedFile, setSelectedFile] = useState<File | null>(null);
  const [uploading, setUploading] = useState(false);
  const [saving, setSaving] = useState(false);
  const navigate = useNavigate();

  const weeks = [1, 2, 3, 4, 5];
  const isAdmin = (() => {
    try {
      const viewMode = (localStorage.getItem('viewMode')||'auto');
      const role = JSON.parse(localStorage.getItem('user')||'{}').role;
      return (viewMode !== 'student') && role === 'admin';
    } catch { return false; }
  })();

  const handleWeekChange = async (week: number) => {
    setIsWeekTransitioning(true);
    
    // Clear existing data first
    setTutorialTasks([]);
    setTutorialWeek(null);
    setUserSubmissions({});
    
    // Update state and localStorage
    setSelectedWeek(week);
    localStorage.setItem('selectedTutorialWeek', week.toString());
    
    // Force a small delay to ensure state is updated
    await new Promise(resolve => setTimeout(resolve, 50));
    
    // Wait for actual content to load before ending animation
    try {
      // Fetch new week's data with the updated selectedWeek
      let response;
      try {
        response = await api.get(`/api/search/tutorial-tasks/${week}`);
      } catch (e) {
        // Soft retry once on transient errors
        response = await api.get(`/api/search/tutorial-tasks/${week}`);
      }
      
      if (response.data) {
        const tasks = response.data;
        console.log('Fetched tasks for week', week, ':', tasks);
        
        // Ensure tasks are sorted by title
        const sortedTasks = tasks.sort((a: any, b: any) => {
          const aNum = parseInt(a.title?.match(/ST (\d+)/)?.[1] || '0');
          const bNum = parseInt(b.title?.match(/ST (\d+)/)?.[1] || '0');
          return aNum - bNum;
        });
        
        setTutorialTasks(sortedTasks);
        
        // Use translation brief from tasks; if none and no tasks, only fall back to localStorage for admins
        let translationBrief = null as string | null;
        if (tasks.length > 0) {
          translationBrief = tasks[0].translationBrief;
        } else {
          const briefKey = `translationBrief_week_${week}`;
          const viewMode = (localStorage.getItem('viewMode')||'auto');
          const role = (JSON.parse(localStorage.getItem('user')||'{}').role);
          const isAdminView = (viewMode !== 'student') && role === 'admin';
          translationBrief = isAdminView ? localStorage.getItem(briefKey) : '';
        }
        
        const tutorialWeekData: TutorialWeek = {
          weekNumber: week,
          translationBrief: (translationBrief ?? undefined),
          tasks: tasks
        };
        setTutorialWeek(tutorialWeekData);
        
        // Fetch user submissions for the new tasks
        await fetchUserSubmissions(tasks);
      }
      
      // Wait longer for DOM to update with new content (especially for Week 2)
      const delay = week === 2 ? 400 : 200;
      await new Promise(resolve => setTimeout(resolve, delay));
    } catch (error) {
      console.error('Error loading week data:', error);
    } finally {
      // End transition after content is loaded and rendered
      setIsWeekTransitioning(false);
    }
  };

  useEffect(() => {
    const loadVisibility = async () => {
      try {
        const base = (((api.defaults as any)?.baseURL as string) || '').replace(/\/$/, '');
        const resp = await fetch(`${base}/api/admin/weeks/tutorial/${selectedWeek}/visibility`, {
          headers: {
            'Authorization': localStorage.getItem('token') ? `Bearer ${localStorage.getItem('token')}` : '',
            'user-role': 'admin'
          }
        });
        const json = await resp.json().catch(() => ({}));
        setIsWeekHidden(!!json?.week?.hidden);
      } catch (e) { /* noop */ }
    };
    loadVisibility();
  }, [selectedWeek]);

  const handleFileUpload = async (file: File): Promise<string> => {
    try {
      setUploading(true);
      
      // Convert file to data URL for display
      return new Promise((resolve, reject) => {
        const reader = new FileReader();
        reader.onload = () => {
          const dataUrl = reader.result as string;
          console.log('File uploaded:', file.name, 'Size:', file.size);
          console.log('Generated data URL:', dataUrl.substring(0, 50) + '...');
          resolve(dataUrl);
        };
        reader.onerror = () => {
          console.error('Error reading file:', reader.error);
          reject(reader.error);
        };
        reader.readAsDataURL(file);
      });
    } catch (error) {
      console.error('Error uploading file:', error);
      throw error;
    } finally {
      setUploading(false);
    }
  };

  const handleFileChange = (event: React.ChangeEvent<HTMLInputElement>) => {
    const file = event.target.files?.[0];
    if (file) {
      setSelectedFile(file);
    }
  };

  const toggleExpanded = (taskId: string) => {
    setExpandedSections(prev => ({
      ...prev,
      [taskId]: !prev[taskId]
    }));
  };

  const fetchUserSubmissions = useCallback(async (tasks: TutorialTask[]) => {
    try {
      const token = localStorage.getItem('token');
      const user = localStorage.getItem('user');
      
      if (!token || !user) {
        return;
      }

      const response = await api.get('/api/submissions/my-submissions');

      if (response.data && response.data.submissions) {
        const data = response.data;
        
        const groupedSubmissions: {[key: string]: UserSubmission[]} = {};
        
        // Initialize all tasks with empty arrays
        tasks.forEach(task => {
          groupedSubmissions[task._id] = [];
        });
        
        // Then populate with actual submissions and mark ownership for edit visibility after refresh/login
        if (data.submissions && Array.isArray(data.submissions)) {
          data.submissions.forEach((submission: any) => {
            const sourceTextId = submission.sourceTextId?._id || submission.sourceTextId;
            if (sourceTextId && groupedSubmissions[sourceTextId]) {
              groupedSubmissions[sourceTextId].push({ ...submission, isOwner: true });
            }
          });
        }
                  
        setUserSubmissions(groupedSubmissions);
      }
    } catch (error) {
      console.error('Error fetching user submissions:', error);
    }
  }, []);

  const fetchTutorialTasks = useCallback(async (showLoading = true) => {
    try {
      if (showLoading) {
        setLoading(true);
      }
      const token = localStorage.getItem('token');
      const user = localStorage.getItem('user');
      
      if (!token || !user) {
        navigate('/login');
        return;
      }

      let response;
      try {
        response = await api.get(`/api/search/tutorial-tasks/${selectedWeek}`);
      } catch (e) {
        response = await api.get(`/api/search/tutorial-tasks/${selectedWeek}`);
      }

      if (response.data) {
        const tasks = response.data;
        console.log('Fetched tasks:', tasks);
        console.log('Tasks with images:', tasks.filter((task: TutorialTask) => task.imageUrl));
        // Debug: Log each task's fields
        tasks.forEach((task: any, index: number) => {
          console.log(`Task ${index} fields:`, {
            _id: task._id,
            content: task.content,
            imageUrl: task.imageUrl,
            imageAlt: task.imageAlt,
            translationBrief: task.translationBrief,
            weekNumber: task.weekNumber,
            category: task.category
          });
          console.log(`Task ${index} imageUrl:`, task.imageUrl);
          console.log(`Task ${index} translationBrief:`, task.translationBrief);
        });
        
        // Ensure tasks are sorted by title to maintain correct order (Tutorial ST 1, Tutorial ST 2, etc.)
        const sortedTasks = tasks.sort((a: any, b: any) => {
          const aNum = parseInt(a.title?.match(/ST (\d+)/)?.[1] || '0');
          const bNum = parseInt(b.title?.match(/ST (\d+)/)?.[1] || '0');
          return aNum - bNum;
        });
        setTutorialTasks(sortedTasks);
        
        // Use translation brief from tasks or localStorage
        let translationBrief = null;
        if (tasks.length > 0) {
          translationBrief = tasks[0].translationBrief;
          console.log('Translation brief from task:', translationBrief);
        } else {
          // Check localStorage for brief if no tasks exist
          const briefKey = `translationBrief_week_${selectedWeek}`;
          translationBrief = localStorage.getItem(briefKey);
          console.log('Translation brief from localStorage:', translationBrief);
          console.log('localStorage key:', briefKey);
        }
        
        console.log('Final translation brief:', translationBrief);
        const tutorialWeekData: TutorialWeek = {
          weekNumber: selectedWeek,
          translationBrief: translationBrief,
          tasks: tasks
        };
        setTutorialWeek(tutorialWeekData);
        
        await fetchUserSubmissions(tasks);
      } else {
        console.error('Failed to fetch tutorial tasks');
      }
    } catch (error) {
      console.error('Error fetching tutorial tasks:', error);
    } finally {
      if (showLoading) {
        setLoading(false);
      }
    }
  }, [selectedWeek, fetchUserSubmissions, navigate]);

  useEffect(() => {
    const user = localStorage.getItem('user');
    if (!user) {
      navigate('/login');
      return;
    }
    fetchTutorialTasks();
  }, [fetchTutorialTasks, navigate]);

  // Listen for week reset events from page navigation
  useEffect(() => {
    const handleWeekReset = (event: CustomEvent) => {
      if (event.detail.page === 'tutorial-tasks') {
        console.log('Week reset event received for tutorial tasks');
        setSelectedWeek(event.detail.week);
        localStorage.setItem('selectedTutorialWeek', event.detail.week.toString());
      }
    };

    window.addEventListener('weekReset', handleWeekReset as EventListener);
    return () => {
      window.removeEventListener('weekReset', handleWeekReset as EventListener);
    };
  }, []);

  // Refresh submissions when user changes (after login/logout)
  useEffect(() => {
    const user = localStorage.getItem('user');
    if (user && tutorialTasks.length > 0) {
      fetchUserSubmissions(tutorialTasks);
    }
  }, [tutorialTasks, fetchUserSubmissions]);

  const handleSubmitTranslation = async (taskId: string, localText?: string, localGroup?: number) => {
    const text = localText || translationText[taskId];
    const group = localGroup || selectedGroups[taskId];
    
    if (!text?.trim()) {
      return;
    }

    if (!group) {
      return;
    }

    try {
      setMutatingTaskId(taskId);
      if (isSafari && SAFARI_FREEZE_ENABLED) {
        try { (document.activeElement as HTMLElement | null)?.blur?.(); } catch {}
        freezeScroll();
      }
      lockListHeight();
      lockCardHeightById(taskId);
      lockGridHeightById(taskId);
      withPreservedCardOffset(taskId, () => {
      setSubmitting({ ...submitting, [taskId]: true });
      });
      const user = JSON.parse(localStorage.getItem('user') || '{}');
      const t0 = performance.now();
      const response = await new Promise((resolve) => requestAnimationFrame(async () => {
        const res = await api.post('/api/submissions', {
        sourceTextId: taskId,
          transcreation: text,
          groupNumber: group,
        culturalAdaptations: [],
        username: user.name || 'Unknown'
      });
        resolve(res);
      })) as any;
      try { console.log('[Trace] Submit:response', { taskId, dt: Math.round(performance.now() - t0) }); } catch {}

      if (response.status >= 200 && response.status < 300) {
        const created = response.data;
        console.log('Submission created successfully:', created);

        // Optimistic append to avoid large reflow from full refetch
        setUserSubmissions(prev => {
          const current = prev[taskId] || [];
          // Put newest first like server returns
          const next = [{ ...created, isOwner: true }, ...current];
          return { ...prev, [taskId]: next };
        });

        // Defer state updates and minimal refetch
        // Measure grid height and set spacer before refetch to keep layout height constant
        const gridEl = submissionsGridRefs.current[taskId];
        const containerEl = submissionsContainerRefs.current[taskId];
        const cardEl = cardRefs.current[taskId];
        const preGridH = gridEl ? gridEl.getBoundingClientRect().height : 0;
        const preContH = containerEl ? containerEl.getBoundingClientRect().height : 0;
        const preCardH = cardEl ? cardEl.getBoundingClientRect().height : 0;
        const preHeight = preGridH > 0 ? preGridH : (preContH > 0 ? preContH : preCardH);
        try { console.log('[Trace] Submit:preHeights', { taskId, preGridH, preContH, preCardH, chosen: preHeight }); } catch {}
        if (!isSafari && preHeight > 0) setSpacerHeights(prev => ({ ...prev, [taskId]: preHeight }));
        if (isSafari) { try { console.log('[Trace] Submit:preHeights', { taskId, preGridH, preContH, preCardH, chosen: preHeight }); } catch {} }
        lastPreHeightRef.current[taskId] = preHeight;
        disableCompensationRef.current.add(taskId);
        withPreservedCardOffset(taskId, () => {
          try {
            const el = cardRefs.current[taskId];
            const topBefore = el ? el.getBoundingClientRect().top : null;
            console.log('[Trace] ScrollPreserve:before', { taskId, topBefore });
          } catch {}
          React.startTransition(() => {
        setTranslationText({ ...translationText, [taskId]: '' });
        setSelectedGroups({ ...selectedGroups, [taskId]: 0 });
          });
          if (!isSafari) {
            // Narrow refetch immediately for non-Safari
            pendingUnlocksRef.current.add(taskId);
            api.get(`/api/submissions/by-source/${taskId}`).then(r => {
              const list = (r.data && r.data.submissions) || [];
              setUserSubmissions(prev => ({ ...prev, [taskId]: list }));
              requestAnimationFrame(() => setSpacerHeights(prev => ({ ...prev, [taskId]: 0 })));
            }).catch(() => {
              requestAnimationFrame(() => setSpacerHeights(prev => ({ ...prev, [taskId]: 0 })));
            });
      } else {
            // Safari: delay refetch and keep locks until refetch completes
            pendingUnlocksRef.current.add(taskId);
            setTimeout(() => {
              api.get(`/api/submissions/by-source/${taskId}`).then(r => {
                const list = (r.data && r.data.submissions) || [];
                setUserSubmissions(prev => ({ ...prev, [taskId]: list }));
                requestAnimationFrame(() => {
                  // Allow scroll compensation at unlock time
                  disableCompensationRef.current.delete(taskId);
                  setSpacerHeights(prev => ({ ...prev, [taskId]: 0 }));
                  withPreservedCardOffset(taskId, () => {
                    unlockListHeight();
                    unlockCardHeightById(taskId);
                    unlockGridHeightById(taskId);
                    unlockContainerHeightById(taskId);
                    pendingUnlocksRef.current.delete(taskId);
                  });
                });
              }).catch(() => {
                requestAnimationFrame(() => {
                  disableCompensationRef.current.delete(taskId);
                  setSpacerHeights(prev => ({ ...prev, [taskId]: 0 }));
                  withPreservedCardOffset(taskId, () => {
                    unlockListHeight();
                    unlockCardHeightById(taskId);
                    unlockGridHeightById(taskId);
                    unlockContainerHeightById(taskId);
                    pendingUnlocksRef.current.delete(taskId);
                  });
                });
              });
            }, 300);
          }
        });
      } else {
        console.error('[Trace] Submit:Error', response.data);
      }
    } catch (error) {
      console.error('[Trace] Submit:Exception', error);
      
    } finally {
      withPreservedCardOffset(taskId, () => {
      setSubmitting({ ...submitting, [taskId]: false });
      });
      // release after a couple frames to let DOM settle (extra frame on Safari)
      const release = () => {
        // If a gated refetch is in-flight, defer unlock until postRefetch block
        if (pendingUnlocksRef.current.has(taskId)) return;
        unlockListHeight();
        unlockCardHeightById(taskId);
        unlockGridHeightById(taskId);
        unlockContainerHeightById(taskId);
        if (isSafari && SAFARI_FREEZE_ENABLED) unfreezeScroll();
      };
      if (isSafari) {
        requestAnimationFrame(() => requestAnimationFrame(() => requestAnimationFrame(release)));
      } else {
        requestAnimationFrame(() => requestAnimationFrame(release));
      }
      setMutatingTaskId(null);
    }
  };

  const [editingSubmission, setEditingSubmission] = useState<{id: string, text: string} | null>(null);
  const [editSubmissionText, setEditSubmissionText] = useState('');

  const handleEditSubmission = async (submissionId: string, currentText: string) => {
    setEditingSubmission({ id: submissionId, text: currentText });
    setEditSubmissionText(currentText);
  };

  const saveEditedSubmission = async () => {
    if (!editingSubmission || !editSubmissionText.trim()) return;

    try {
      const response = await api.put(`/api/submissions/${editingSubmission.id}`, {
        transcreation: editSubmissionText
      });

      if (response.status >= 200 && response.status < 300) {
  
        // Defer all state updates to prevent UI jumping
        React.startTransition(() => {
        setEditingSubmission(null);
        setEditSubmissionText('');
          fetchUserSubmissions(tutorialTasks);
        });
      } else {
        console.error('Failed to update translation:', response.data);
      }
    } catch (error) {
      console.error('Error updating translation:', error);

    }
  };

  const cancelEditSubmission = () => {
    setEditingSubmission(null);
    setEditSubmissionText('');
  };

  const handleDeleteSubmission = async (submissionId: string, taskId?: string) => {


    try {
      if (taskId) setMutatingTaskId(taskId);
      if (isSafari && SAFARI_FREEZE_ENABLED) { freezeScroll(); }
      lockListHeight();
      if (taskId) {
        lockCardHeightById(taskId);
        lockGridHeightById(taskId);
      }
      const t0 = performance.now();
      const response = await api.delete(`/api/submissions/${submissionId}`);

      if (response.status === 200) {
        try { console.log('[Trace] Delete:response', { taskId, submissionId, dt: Math.round(performance.now() - t0) }); } catch {}
  
        // Optimistic removal (disabled on Safari to prevent multi-phase jumps)
        if (taskId && !isSafari) {
          setUserSubmissions(prev => {
            const list = prev[taskId] || [];
            const next = list.filter(s => s._id !== submissionId);
            return { ...prev, [taskId]: next };
          });
        }

        // Defer refetch to prevent UI jumping and preserve scroll around DOM updates
        if (taskId) {
          const gridEl = submissionsGridRefs.current[taskId];
          const containerEl = submissionsContainerRefs.current[taskId];
          const cardEl = cardRefs.current[taskId];
          const preGridH = gridEl ? gridEl.getBoundingClientRect().height : 0;
          const preContH = containerEl ? containerEl.getBoundingClientRect().height : 0;
          const preCardH = cardEl ? cardEl.getBoundingClientRect().height : 0;
          const preHeight = preGridH > 0 ? preGridH : (preContH > 0 ? preContH : preCardH);
          lastPreHeightRef.current[taskId] = preHeight;
          try { console.log('[Trace] Delete:preHeights', { taskId, preGridH, preContH, preCardH, chosen: preHeight }); } catch {}
        }
        withPreservedCardOffset(taskId || '', () => {
          try {
            if (taskId) {
              const el = cardRefs.current[taskId];
              const topBefore = el ? el.getBoundingClientRect().top : null;
              console.log('[Trace] ScrollPreserve(del):before', { taskId, topBefore });
            }
          } catch {}
          React.startTransition(() => {
            // Narrow refetch: only this task's submissions
            if (taskId) {
              if (!isSafari) {
                const gridEl = submissionsGridRefs.current[taskId];
                const gridHeight = gridEl ? gridEl.getBoundingClientRect().height : 0;
                if (gridHeight > 0) setSpacerHeights(prev => ({ ...prev, [taskId]: gridHeight }));
                api.get(`/api/submissions/by-source/${taskId}`).then(r => {
                  const list = (r.data && r.data.submissions) || [];
                  setUserSubmissions(prev => ({ ...prev, [taskId]: list }));
                  requestAnimationFrame(() => setSpacerHeights(prev => ({ ...prev, [taskId]: 0 })));
                }).catch(() => {
                  requestAnimationFrame(() => setSpacerHeights(prev => ({ ...prev, [taskId]: 0 })));
                });
              }
            } else {
              // Fallback
              requestAnimationFrame(() => requestAnimationFrame(() => fetchUserSubmissions(tutorialTasks)));
            }
          });
        });
      } else {

      }
    } catch (error) {
      console.error('[Trace] Delete:Exception', error);

    }
    // let DOM settle then unlock (both list and card if id known)
    requestAnimationFrame(() => requestAnimationFrame(() => {
      if (isSafari && taskId) {
        // Hold locks until per-task refetch completes
        pendingUnlocksRef.current.add(taskId);
        setTimeout(() => {
          api.get(`/api/submissions/by-source/${taskId}`).then(r => {
            const list = (r.data && r.data.submissions) || [];
            setUserSubmissions(prev => ({ ...prev, [taskId]: list }));
          }).catch(() => {}).finally(() => {
            withPreservedCardOffset(taskId, () => {
              unlockListHeight();
              unlockCardHeightById(taskId);
              unlockGridHeightById(taskId);
              if (SAFARI_FREEZE_ENABLED) unfreezeScroll();
              pendingUnlocksRef.current.delete(taskId);
            });
            try {
              const el = cardRefs.current[taskId];
              const topAfter = el ? el.getBoundingClientRect().top : null;
              console.log('[Trace] ScrollPreserve(del):after', { taskId, topAfter });
            } catch {}
          });
        }, 300);
      } else {
        unlockListHeight();
        if (taskId) {
          unlockCardHeightById(taskId);
          unlockGridHeightById(taskId);
        }
        if (isSafari && SAFARI_FREEZE_ENABLED) { unfreezeScroll(); }
      }
      setMutatingTaskId(null);
    }));
  };

  const getStatusIcon = (status: string) => {
    switch (status) {
      case 'approved':
        return <CheckCircleIcon className="h-5 w-5 text-green-500" />;
      case 'pending':
        return <ClockIcon className="h-5 w-5 text-yellow-500" />;
      default:
        return <ClockIcon className="h-5 w-5 text-gray-500" />;
    }
  };

  const startEditing = (task: TutorialTask) => {
    setEditingTask(task._id);
    setEditForm({
      content: task.content,
      translationBrief: task.translationBrief || '',
      imageUrl: task.imageUrl || '',
      imageAlt: task.imageAlt || ''
    });
  };

  const startEditingBrief = () => {
    setEditingBrief(prev => ({ ...prev, [selectedWeek]: true }));
    setEditForm({
      content: '',
      translationBrief: tutorialWeek?.translationBrief || '',
      imageUrl: '',
      imageAlt: ''
    });
  };

  const startAddingBrief = () => {
    setEditingBrief(prev => ({ ...prev, [selectedWeek]: true }));
    setEditForm({
      content: '',
      translationBrief: '',
      imageUrl: '',
      imageAlt: ''
    });
  };

  const removeBrief = async () => {

    
    try {
      setSaving(true);
      const token = localStorage.getItem('token');
      const user = JSON.parse(localStorage.getItem('user') || '{}');
      
      // Check if user is admin
      if (user.role !== 'admin') {
  
        return;
      }

              const response = await api.put(`/api/auth/admin/tutorial-brief/${selectedWeek}`, {
        translationBrief: '',
        weekNumber: selectedWeek
      });

      if (response.status >= 200 && response.status < 300) {
        const briefKey = `translationBrief_week_${selectedWeek}`;
        localStorage.removeItem(briefKey);
        setEditForm((prev) => ({ ...prev, translationBrief: '' }));
        await fetchTutorialTasks();

      } else {
        console.error('Failed to remove translation brief:', response.data);
        
      }
    } catch (error) {
      console.error('Failed to remove translation brief:', error);
      
    } finally {
      setSaving(false);
    }
  };

  const cancelEditing = () => {
    setEditingTask(null);
    setEditingBrief(prev => ({ ...prev, [selectedWeek]: false }));
    setEditForm({
      content: '',
      translationBrief: '',
      imageUrl: '',
      imageAlt: ''
    });
    setSelectedFile(null);
  };

  const saveTask = async () => {
    if (!editingTask) return;
    
    try {
      setSaving(true);
      const token = localStorage.getItem('token');
      const user = JSON.parse(localStorage.getItem('user') || '{}');
      
      // Check if user is admin
      if (user.role !== 'admin') {

        return;
      }

      const updateData = {
        ...editForm,
        weekNumber: selectedWeek
      };
      console.log('Saving task with data:', updateData);
      
              const response = await api.put(`/api/auth/admin/tutorial-tasks/${editingTask}`, updateData);

              if (response.status >= 200 && response.status < 300) {
          await fetchTutorialTasks(false);
          setEditingTask(null);

        } else {
          console.error('Failed to update tutorial task:', response.data);
          
        }
    } catch (error) {
      console.error('Failed to update tutorial task:', error);
      
    } finally {
      setSaving(false);
    }
  };

  const saveBrief = async () => {
    try {
      setSaving(true);
      const user = JSON.parse(localStorage.getItem('user') || '{}');
      
      // Check if user is admin
      if (user.role !== 'admin') {
        return;
      }

      console.log('Saving brief for week:', selectedWeek);
      console.log('Brief content:', editForm.translationBrief);

      // Save brief by creating or updating the first task of the week
      if (tutorialTasks.length > 0) {
        const firstTask = tutorialTasks[0];
        console.log('Updating first task with brief:', firstTask._id);
        
        const response = await api.put(`/api/auth/admin/tutorial-tasks/${firstTask._id}`, {
          ...firstTask,
          translationBrief: editForm.translationBrief,
          weekNumber: selectedWeek
        });

        if (response.status >= 200 && response.status < 300) {
          console.log('Brief saved successfully');
          // Optimistic UI update
          const briefKey = `translationBrief_week_${selectedWeek}`;
          localStorage.setItem(briefKey, editForm.translationBrief);
          setTutorialWeek(prev => prev ? { ...prev, translationBrief: editForm.translationBrief } : prev);
          setEditingBrief(prev => ({ ...prev, [selectedWeek]: false }));
          // Background refresh
          fetchTutorialTasks(false);
        } else {
          console.error('Failed to save brief:', response.data);
        }
      } else {
        // If no tasks exist, save the brief to localStorage
        console.log('No tasks available to save brief to - saving to localStorage');
        const briefKey = `translationBrief_week_${selectedWeek}`;
        localStorage.setItem(briefKey, editForm.translationBrief);
        setEditingBrief(prev => ({ ...prev, [selectedWeek]: false }));
      }
    } catch (error) {
      console.error('Failed to update translation brief:', error);
    } finally {
      setSaving(false);
    }
  };

  const startAddingTask = () => {
    setAddingTask(true);
    setEditForm({
      content: '',
      translationBrief: '',
      imageUrl: '',
      imageAlt: ''
    });
  };

  const cancelAddingTask = () => {
    setAddingTask(false);
    setEditForm({
      content: '',
      translationBrief: '',
      imageUrl: '',
      imageAlt: ''
    });
    setSelectedFile(null);
  };

  const startAddingImage = () => {
    setAddingImage(true);
    setImageForm({
      imageUrl: '',
      imageAlt: '',
      imageSize: 200,
      imageAlignment: 'center'
    });
  };

  const cancelAddingImage = () => {
    setAddingImage(false);
    setImageForm({
      imageUrl: '',
      imageAlt: '',
      imageSize: 200,
      imageAlignment: 'center'
    });
  };

  const startAddingRevision = () => {
    setAddingRevision(true);
  };

  const cancelAddingRevision = () => {
    setAddingRevision(false);
  };

  const addRevision = () => {
    setRevisionAdded(true);
    setAddingRevision(false);
    try {
      localStorage.setItem(`tutorial_revision_added_week_${selectedWeek}`, 'true');
    } catch (e) {
      // Safari might block localStorage, but state is already updated
      console.warn('Failed to save revision state to localStorage:', e);
    }
  };

  const removeRevision = () => {
    // Only allow removing if no tasks exist
    if (!hasRevisionTasks) {
      setRevisionAdded(false);
      try {
        localStorage.removeItem(`tutorial_revision_added_week_${selectedWeek}`);
      } catch (e) {
        // Safari might block localStorage, but state is already updated
        console.warn('Failed to remove revision state from localStorage:', e);
      }
    } else {
      // If tasks exist, warn the admin that they need to delete tasks first
      alert('Cannot hide Deep Revision module while tasks exist. Please delete all tasks for this week first.');
    }
  };



  const saveNewTask = async () => {
    try {
      setSaving(true);
      const user = JSON.parse(localStorage.getItem('user') || '{}');
      
      // Check if user is admin
      if (user.role !== 'admin') {
        return;
      }

      // Allow either content or imageUrl, but not both empty
      if (!editForm.content.trim() && !editForm.imageUrl.trim()) {
        return;
      }

      console.log('Saving new task for week:', selectedWeek);
      console.log('Task content:', editForm.content);
      console.log('Image URL:', editForm.imageUrl);
      console.log('Image Alt:', editForm.imageAlt);

      const taskData = {
        title: `Week ${selectedWeek} Tutorial Task`,
        content: editForm.content,
        sourceLanguage: 'English',
        weekNumber: selectedWeek,
        category: 'tutorial',
        imageUrl: editForm.imageUrl || null,
        imageAlt: editForm.imageAlt || null,
        // Add imageSize and imageAlignment for image-only content
        ...(editForm.imageUrl && !editForm.content.trim() && { imageSize: 200 }),
        ...(editForm.imageUrl && !editForm.content.trim() && { imageAlignment: 'center' })
      };

      console.log('Task data being sent:', JSON.stringify(taskData, null, 2));

      console.log('Sending task data:', taskData);

              const response = await api.post('/api/auth/admin/tutorial-tasks', taskData);
      
      console.log('Task save response:', response.data);

      if (response.status >= 200 && response.status < 300) {
        console.log('Task saved successfully');
                  console.log('Saved task response:', response.data);
          console.log('Saved task response keys:', Object.keys(response.data || {}));
          console.log('Saved task response.task:', response.data?.task);
          console.log('Saved task response.task.imageUrl:', response.data?.task?.imageUrl);
          console.log('Saved task response.task.translationBrief:', response.data?.task?.translationBrief);
        await fetchTutorialTasks(false);
        setAddingTask(false);

              } else {
          console.error('Failed to add tutorial task:', response.data);
        }
      } catch (error) {
        console.error('Failed to add tutorial task:', error);
      } finally {
      setSaving(false);
    }
  };

  const saveNewImage = async () => {
    try {
      setSaving(true);
      const user = JSON.parse(localStorage.getItem('user') || '{}');
      
      // Check if user is admin
      if (user.role !== 'admin') {
        return;
      }

      if (!imageForm.imageUrl.trim()) {
        return;
      }

      const payload = {
        title: `Week ${selectedWeek} Image Task`,
        content: '', // Empty content for image-only task
        sourceLanguage: 'English',
        weekNumber: selectedWeek,
        category: 'tutorial',
        imageUrl: imageForm.imageUrl.trim(),
        imageAlt: imageForm.imageAlt.trim() || null,
        imageSize: imageForm.imageSize,
        imageAlignment: imageForm.imageAlignment
      };

      console.log('Saving new image task with payload:', payload);

      const response = await api.post('/api/auth/admin/tutorial-tasks', payload);

      if (response.data) {
        console.log('Image task saved successfully:', response.data);
        await fetchTutorialTasks(false);
        setAddingImage(false);
      } else {
        console.error('Failed to save image task');
      }
    } catch (error) {
      console.error('Failed to add image task:', error);
    } finally {
      setSaving(false);
    }
  };

  const deleteTask = async (taskId: string) => {

    
    try {
      const token = localStorage.getItem('token');
      const user = JSON.parse(localStorage.getItem('user') || '{}');
      
      // Check if user is admin
      if (user.role !== 'admin') {

        return;
      }

      const response = await api.delete(`/api/auth/admin/tutorial-tasks/${taskId}`);

      if (response.status >= 200 && response.status < 300) {
        await fetchTutorialTasks(false);
      } else {
        console.error('Failed to delete tutorial task:', response.data);
      }

      await fetchTutorialTasks(false);
    } catch (error) {
      console.error('Failed to delete task:', error);
    }
  };

  // Remove intrusive loading screen - just show content with loading state


  const GroupInputs: React.FC<{
    taskId: string;
    label: string;
    selectWidthClass: string;
    selectedGroup: number | '';
    onGroupChange: (n: number) => void;
    translationValue: string;
    onTranslationChange: (v: string) => void;
    submitting: boolean;
    onSubmit: () => void;
  }> = React.memo(({ taskId, label, selectWidthClass, selectedGroup, onGroupChange, translationValue, onTranslationChange, submitting, onSubmit }) => {
  return (
      <>
        <div className="mb-2">
          <label className="block text-xs font-medium text-gray-700 mb-1">{label}</label>
          <select
            value={selectedGroup || ''}
            onChange={(e) => {
              const v = parseInt(e.target.value);
              onGroupChange(Number.isNaN(v) ? ('' as any) : v);
            }}
            className={`${selectWidthClass} px-2 py-1 border border-gray-300 rounded focus:outline-none focus:ring-2 focus:ring-indigo-500 focus:border-indigo-500 bg-white text-xs`}
          >
            <option value="">Choose...</option>
            {[1,2,3,4,5,6,7,8].map((g) => (<option key={g} value={g}>Group {g}</option>))}
          </select>
        </div>
        <div className="flex items-center justify-end space-x-2 mb-2">
          <button onClick={() => applyInlineFormat(`tutorial-translation-${taskId}`, translationValue || '', v => onTranslationChange(v), '**')} className="px-2 py-1 text-xs bg-indigo-100 text-indigo-900 rounded">B</button>
          <button onClick={() => applyInlineFormat(`tutorial-translation-${taskId}`, translationValue || '', v => onTranslationChange(v), '*')} className="px-2 py-1 text-xs bg-indigo-100 text-indigo-900 rounded italic">I</button>
          <button onClick={() => applyLinkFormat(`tutorial-translation-${taskId}`, translationValue || '', v => onTranslationChange(v))} className="px-2 py-1 text-xs bg-indigo-100 text-indigo-900 rounded">Link</button>
        </div>
        <textarea
          id={`tutorial-translation-${taskId}`}
          value={translationValue || ''}
          
          onChange={(e) => {
            onTranslationChange(e.target.value);
          }}
          className="w-full px-4 py-3 border border-gray-300 rounded-lg focus:outline-none focus:ring-2 focus:ring-indigo-500 focus:border-indigo-500 bg-white"
          style={{ height: '150px' }}
          rows={4}
          placeholder="Enter your group's translation here..."
        />
        <div className="flex justify-end mt-2">
          <button onClick={onSubmit} disabled={submitting} className="btn-primary disabled:bg-gray-400 text-white px-4 py-2 rounded-lg text-sm">{submitting ? 'Submitting...' : 'Submit Translation'}</button>
        </div>
      </>
    );
  }, (a, b) => (
    a.selectedGroup === b.selectedGroup &&
    a.translationValue === b.translationValue &&
    a.submitting === b.submitting &&
    a.label === b.label &&
    a.selectWidthClass === b.selectWidthClass &&
    a.taskId === b.taskId
  ));

  return (
    <div className="min-h-screen bg-white py-8">
      <div className="max-w-7xl mx-auto px-4 sm:px-6 lg:px-8">
        {/* Header */}
        <div className="mb-8">
          <div className="flex items-center mb-4">
            <img src="/icons/tutorial tasks.svg" alt="Tutorial Tasks" className="h-8 w-8 mr-3" />
            <h1 className="text-3xl font-bold text-ui-text">Tutorial Tasks</h1>
          </div>
          <p className="text-ui-text/70">
            Complete weekly tutorial tasks with your group to practice collaborative translation skills.
          </p>
        </div>

        {/* Week Selector */}
        <div className="mb-6">
          <div className="flex space-x-3 overflow-x-auto pb-2">
            {weeks.map((week) => {
              const isActive = selectedWeek === week;
              return (
              <button
                key={week}
                onClick={() => handleWeekChange(week)}
                  className={`relative inline-flex items-center justify-center rounded-2xl px-4 py-1.5 whitespace-nowrap transition-all duration-300 ease-out ring-1 ring-inset ${isActive ? 'ring-white/50 backdrop-brightness-110 backdrop-saturate-150' : 'ring-white/30'} backdrop-blur-md isolate shadow-[inset_0_0.5px_0_rgba(255,255,255,0.5),inset_0_-1px_1.5px_rgba(0,0,0,0.12)]`}
                  style={{ background: 'rgba(255,255,255,0.10)' }}
                >
                  {/* Rim washes */}
                  <div className="pointer-events-none absolute inset-0 rounded-2xl opacity-60 [background:linear-gradient(to_bottom,rgba(255,255,255,0.35),rgba(255,255,255,0)_28%),linear-gradient(to_right,rgba(255,255,255,0.35),rgba(255,255,255,0)_28%)]" />
                  {/* Soft glossy wash */}
                  <div className="pointer-events-none absolute inset-0 rounded-2xl bg-gradient-to-tr from-white/30 via-white/10 to-transparent opacity-50" />
                  {/* Tiny hotspot near TL */}
                  <div className="pointer-events-none absolute rounded-full" style={{ width: '28px', height: '28px', left: '8px', top: '6px', background: 'radial-gradient(16px_16px_at_10px_10px,rgba(255,255,255,0.5),rgba(255,255,255,0)_60%)', opacity: 0.45 }} />
                  {/* Center darken for depth */}
                  {!isActive && (
                    <div className="pointer-events-none absolute inset-0 rounded-2xl" style={{ background: 'radial-gradient(120%_120%_at_50%_55%,rgba(0,0,0,0.04),rgba(0,0,0,0)_60%)', opacity: 0.9 }} />
                  )}
                  {/* Blue tint overlay to match brand */}
                  <div className={`pointer-events-none absolute inset-0 rounded-2xl ${isActive ? 'bg-sky-600/70 mix-blend-normal opacity-100' : 'bg-sky-500/30 mix-blend-overlay opacity-35'}`} />
                  <span className={`relative z-10 text-sm font-medium ${isActive ? 'text-white' : 'text-black'}`}>Week {week}</span>
              </button>
              );
            })}
          </div>
          {isAdmin && (
            <div className="mt-2 flex items-center gap-2">
              <span className="text-xs text-ui-text/70">Visibility</span>
              <label className="switch">
                <input type="checkbox" checked={isWeekHidden} onChange={async(e)=>{
                  try {
                    const base = (((api.defaults as any)?.baseURL as string) || '').replace(/\/$/,'');
                    const nextHidden = e.currentTarget.checked;
                    await fetch(`${base}/api/admin/weeks/tutorial/${selectedWeek}/visibility`,{ method:'PUT', headers:{ 'Content-Type':'application/json', 'Authorization': localStorage.getItem('token')?`Bearer ${localStorage.getItem('token')}`:'', 'user-role':'admin' }, body: JSON.stringify({ hidden: nextHidden }) });
                    setIsWeekHidden(nextHidden);
                    await fetchTutorialTasks(true);
                  } catch (err) { console.error(err);} 
                }}/>
                <span className="slider" />
              </label>
              <span className="text-xs">{isWeekHidden ? 'Hidden' : 'Shown'}</span>
            </div>
          )}
        </div>

                {/* Week Transition Loading Spinner */}
        {isWeekTransitioning && (
          <div className="fixed top-1/2 left-1/2 transform -translate-x-1/2 -translate-y-1/2 z-50">
            <div className="bg-white rounded-lg shadow-lg p-4 flex items-center space-x-3">
              <div className="animate-spin rounded-full h-6 w-6 border-b-2 border-ui-neonBlue"></div>
              <span className="text-gray-700 font-medium">Loading...</span>
            </div>
          </div>
        )}

        {!isWeekTransitioning && (
          <>
            {/* Translation Brief - Shown once at the top (hidden for students when week is hidden) */}
            {tutorialWeek && tutorialWeek.translationBrief && (!isWeekHidden || (isWeekHidden && isAdmin)) ? (
          <div ref={briefRef} className="bg-ui-panel rounded-lg p-6 mb-8 border border-ui-border shadow-sm">
            <div className="flex items-center justify-between mb-4">
              <div className="flex items-center space-x-3">
                <div className="relative p-2 rounded-2xl bg-white/10 backdrop-blur-md ring-1 ring-inset ring-white/30 shadow-[inset_0_0.5px_0_rgba(255,255,255,0.5),inset_0_-1px_1.5px_rgba(0,0,0,0.12)]">
                  <div className="pointer-events-none absolute inset-0 rounded-2xl opacity-60 [background:linear-gradient(to_bottom,rgba(255,255,255,0.35),rgba(255,255,255,0)_28%),linear-gradient(to_right,rgba(255,255,255,0.35),rgba(255,255,255,0)_28%)]" />
                  <div className="pointer-events-none absolute inset-0 rounded-2xl bg-gradient-to-tr from-white/30 via-white/10 to-transparent opacity-50" />
                  <div className="pointer-events-none absolute rounded-full" style={{ width: '20px', height: '20px', left: '6px', top: '6px', background: 'radial-gradient(12px_12px_at_10px_10px,rgba(255,255,255,0.5),rgba(255,255,255,0)_60%)', opacity: 0.45 }} />
                  <div className="pointer-events-none absolute inset-0 rounded-2xl" style={{ background: 'radial-gradient(120%_120%_at_50%_55%,rgba(0,0,0,0.04),rgba(0,0,0,0)_60%)', opacity: 0.9 }} />
                  <div className="pointer-events-none absolute inset-0 rounded-2xl bg-sky-500/30 mix-blend-overlay opacity-35" />
                  <DocumentTextIcon className="relative h-5 w-5 text-ui-text" />
                </div>
                <h3 className="text-ui-text font-semibold text-xl">Translation Brief</h3>
              </div>
              {JSON.parse(localStorage.getItem('user') || '{}').role === 'admin' && (
                <div className="flex items-center space-x-2">
                  {editingBrief[selectedWeek] ? (
                    <>
                      <button
                        onClick={saveBrief}
                        disabled={saving}
                        className="relative inline-flex items-center gap-2 px-3 py-1 rounded-2xl text-sm font-medium text-white ring-1 ring-inset ring-white/50 backdrop-blur-md backdrop-brightness-110 backdrop-saturate-150 shadow-[inset_0_1px_0_rgba(255,255,255,0.6),inset_0_-1px_0_rgba(0,0,0,0.12)] bg-sky-600/70"
                      >
                        {saving ? (
                          <div className="animate-spin rounded-full h-4 w-4 border-b-2 border-white"></div>
                        ) : (
                          <CheckIcon className="h-4 w-4" />
                        )}
                      </button>
                      <button
                        onClick={cancelEditing}
                        className="relative inline-flex items-center gap-2 px-3 py-1 rounded-2xl text-sm font-medium text-black ring-1 ring-inset ring-white/30 backdrop-blur-md shadow-[inset_0_0.5px_0_rgba(255,255,255,0.5),inset_0_-1px_1.5px_rgba(0,0,0,0.12)] bg-white/10"
                      >
                        <XMarkIcon className="h-4 w-4" />
                      </button>
                    </>
                  ) : (
                    <>
                      <button
                        onClick={startEditingBrief}
                        className="bg-gray-100 hover:bg-gray-200 text-gray-700 px-3 py-1 rounded-md transition-colors duration-200 text-sm"
                      >
                        <PencilIcon className="h-4 w-4" />
                      </button>
                      <button
                        onClick={() => removeBrief()}
                        className="bg-red-100 hover:bg-red-200 text-red-700 px-3 py-1 rounded-md transition-colors duration-200 text-sm"
                      >
                        <TrashIcon className="h-4 w-4" />
                      </button>
                    </>
                  )}
                </div>
              )}
            </div>
            {editingBrief[selectedWeek] ? (
              <div>
                <div className="flex items-center justify-end space-x-2 mb-2">
                  <button onClick={() => applyInlineFormat('tutorial-brief-input', editForm.translationBrief, v => setEditForm({ ...editForm, translationBrief: v }), '**')} className="px-2 py-1 text-xs bg-blue-100 text-ui-neonBlue rounded">B</button>
                  <button onClick={() => applyInlineFormat('tutorial-brief-input', editForm.translationBrief, v => setEditForm({ ...editForm, translationBrief: v }), '*')} className="px-2 py-1 text-xs bg-blue-100 text-ui-neonBlue rounded italic">I</button>
                  <button onClick={() => applyLinkFormat('tutorial-brief-input', editForm.translationBrief || '', v => setEditForm({ ...editForm, translationBrief: v }))} className="px-2 py-1 text-xs bg-blue-100 text-ui-neonBlue rounded">Link</button>
                </div>
              <textarea id="tutorial-brief-input"
                value={editForm.translationBrief}
                onChange={(e) => setEditForm({ ...editForm, translationBrief: e.target.value })}
                className="w-full p-4 border border-gray-300 rounded-lg text-gray-900 leading-relaxed text-base bg-white focus:ring-2 focus:ring-ui-neonBlue focus:border-ui-neonBlue"
                rows={6}
                placeholder="Enter translation brief..."
              />
              </div>
            ) : (
              <div className="text-ui-text leading-relaxed text-lg font-smiley whitespace-pre-wrap">{renderFormatted(tutorialWeek.translationBrief || '')}</div>
            )}

            {/* Group Google Doc (refined) */}
            {(() => { const hasDocs = !!localStorage.getItem(`tutorial_group_${selectedWeek}`) || (Array.isArray((tutorialWeek as any)?.groupDocs) && (tutorialWeek as any).groupDocs.length>0); const vm=(localStorage.getItem('viewMode')||'auto'); return hasDocs || (vm!== 'student'); })() && (
              <div ref={groupDocRef} className="mt-6 bg-ui-panel rounded-xl border border-ui-border shadow-sm">
              <div className="px-6 pt-6">
                <h4 className="text-2xl font-bold text-ui-text">Group Google Doc</h4>
                <p className="mt-2 text-ui-text/70">Open or share each group's working document.</p>
              </div>
              <div className="px-6 pb-4 pt-4 border-t border-gray-100">
              <GroupDocSection weekNumber={selectedWeek} />
              </div>
            </div>
            )}
            <div className="mt-4 p-3 bg-ui-bg rounded-lg">
              <p className="text-ui-text text-sm">
                <strong>Note:</strong> Tutorial tasks are completed as group submissions. Each group should collaborate to create a single translation per task.
              </p>
            </div>
          </div>
        ) : (
          // Show add brief button when no brief exists
          JSON.parse(localStorage.getItem('user') || '{}').role === 'admin' && (
            <div className="bg-ui-panel rounded-lg p-6 mb-8 border border-ui-border border-dashed shadow-sm">
              <div className="flex items-center justify-between mb-4">
                              <div className="flex items-center space-x-3">
                <div className="relative p-2 rounded-2xl bg-white/10 backdrop-blur-md ring-1 ring-inset ring-white/30 shadow-[inset_0_0.5px_0_rgba(255,255,255,0.5),inset_0_-1px_1.5px_rgba(0,0,0,0.12)]">
                  <div className="pointer-events-none absolute inset-0 rounded-2xl opacity-60 [background:linear-gradient(to_bottom,rgba(255,255,255,0.35),rgba(255,255,255,0)_28%),linear-gradient(to_right,rgba(255,255,255,0.35),rgba(255,255,255,0)_28%)]" />
                  <div className="pointer-events-none absolute inset-0 rounded-2xl bg-gradient-to-tr from-white/30 via-white/10 to-transparent opacity-50" />
                  <div className="pointer-events-none absolute inset-0 rounded-2xl bg-sky-500/30 mix-blend-overlay opacity-35" />
                  <DocumentTextIcon className="relative h-5 w-5 text-ui-text" />
                </div>
                <h3 className="text-ui-text font-semibold text-xl">Translation Brief</h3>
              </div>
                <button
                  onClick={startAddingBrief}
                  className="relative inline-flex items-center gap-2 px-4 py-2 rounded-2xl text-sm font-medium text-white ring-1 ring-inset ring-white/50 backdrop-blur-md backdrop-brightness-110 backdrop-saturate-150 shadow-[inset_0_1px_0_rgba(255,255,255,0.6),inset_0_-1px_0_rgba(0,0,0,0.12)] bg-sky-600/70"
                >
                  <PlusIcon className="h-5 w-5" />
                  <span className="font-medium">Add Brief</span>
                </button>
              </div>
              {editingBrief[selectedWeek] && (
                <div className="space-y-4">
                  <div className="flex items-center justify-end space-x-2">
                    <button onClick={() => applyInlineFormat('tutorial-brief-input', editForm.translationBrief, v => setEditForm({ ...editForm, translationBrief: v }), '**')} className="px-2 py-1 text-xs bg-blue-100 text-ui-neonBlue rounded">B</button>
                    <button onClick={() => applyInlineFormat('tutorial-brief-input', editForm.translationBrief, v => setEditForm({ ...editForm, translationBrief: v }), '*')} className="px-2 py-1 text-xs bg-blue-100 text-ui-neonBlue rounded italic">I</button>
                    <button onClick={() => applyLinkFormat('tutorial-brief-input', editForm.translationBrief || '', v => setEditForm({ ...editForm, translationBrief: v }))} className="px-2 py-1 text-xs bg-blue-100 text-ui-neonBlue rounded">Link</button>
                  </div>
                  <textarea
                    id="tutorial-brief-input"
                    value={editForm.translationBrief}
                    onChange={(e) => setEditForm({ ...editForm, translationBrief: e.target.value })}
                    className="w-full p-4 border border-ui-border rounded-lg text-ui-text leading-relaxed text-base bg-ui-panel focus:ring-2 focus:ring-ui-neonBlue focus:border-ui-neonBlue"
                    rows={6}
                    placeholder="Enter translation brief..."
                  />
                  <div className="flex justify-end space-x-2">
                    <button
                      onClick={saveBrief}
                      disabled={saving}
                      className="bg-ui-neonBlue hover:bg-ui-neonBlue/90 text-white px-6 py-3 rounded-lg font-medium flex items-center space-x-2 shadow-sm"
                    >
                      {saving ? (
                        <div className="animate-spin rounded-full h-4 w-4 border-b-2 border-white"></div>
                      ) : (
                        <>
                          <CheckIcon className="h-5 w-5" />
                          <span className="font-medium">Save Brief</span>
                        </>
                      )}
                    </button>
                    <button
                      onClick={cancelEditing}
                      className="bg-gray-500 hover:bg-gray-600 text-white px-6 py-3 rounded-lg font-medium flex items-center space-x-2 shadow-sm"
                    >
                      <XMarkIcon className="h-5 w-5" />
                      <span className="font-medium">Cancel</span>
                    </button>
                  </div>
                </div>
              )}
            </div>
          )
        )}

        {/* Tutorial Tasks */}
        <div className="space-y-6">
          {/* Add New Tutorial Task Section */}
          {(() => { const u = JSON.parse(localStorage.getItem('user') || '{}'); const vm = (localStorage.getItem('viewMode')||'auto'); return vm === 'student' ? false : u.role === 'admin'; })() && (
            <div className="mb-8">
              {addingTask ? (
                <div className="bg-white rounded-lg p-6 border border-gray-200 shadow-sm">
                  <div className="flex items-center space-x-3 mb-4">
                    <div className="bg-gray-100 rounded-lg p-2">
                      <PlusIcon className="h-4 w-4 text-gray-600" />
                    </div>
                    <h4 className="text-gray-900 font-semibold text-lg">New Tutorial Task</h4>
                  </div>
                  <div className="space-y-4">
                    <div>
                      <label className="block text-sm font-medium text-gray-700 mb-2">
                        Task Content *
                      </label>
                      <div className="flex items-center justify-end space-x-2 mb-2">
                        <button onClick={() => applyInlineFormat('tutorial-newtask-input', editForm.content, v => setEditForm({ ...editForm, content: v }), '**')} className="px-2 py-1 text-xs bg-blue-100 text-ui-neonBlue rounded">B</button>
                        <button onClick={() => applyInlineFormat('tutorial-newtask-input', editForm.content, v => setEditForm({ ...editForm, content: v }), '*')} className="px-2 py-1 text-xs bg-blue-100 text-ui-neonBlue rounded italic">I</button>
                        <button onClick={() => applyLinkFormat('tutorial-newtask-input', editForm.content || '', v => setEditForm({ ...editForm, content: v }))} className="px-2 py-1 text-xs bg-blue-100 text-ui-neonBlue rounded">Link</button>
                      </div>
                      <textarea
                        id="tutorial-newtask-input"
                        value={editForm.content}
                        onChange={(e) => setEditForm({ ...editForm, content: e.target.value })}
                        className="w-full px-4 py-3 border border-gray-300 rounded-lg focus:outline-none focus:ring-2 focus:ring-ui-neonBlue focus:border-ui-neonBlue bg-white"
                        rows={4}
                        placeholder="Enter tutorial task content..."
                      />
                    </div>
                    <div className="space-y-4">
                      <div className="grid grid-cols-1 md:grid-cols-2 gap-4">
                        <div>
                          <label className="block text-sm font-medium text-gray-700 mb-2">
                            Image URL (Optional)
                          </label>
                          <input
                            type="url"
                            value={editForm.imageUrl}
                            onChange={(e) => setEditForm({ ...editForm, imageUrl: e.target.value })}
                            className="w-full px-3 py-2 border border-gray-300 rounded-md focus:outline-none focus:ring-2 focus:ring-ui-neonBlue focus:border-ui-neonBlue"
                            placeholder="https://example.com/image.jpg"
                          />
                        </div>
                        <div>
                          <label className="block text-sm font-medium text-gray-700 mb-2">
                            Image Alt Text (Optional)
                          </label>
                          <input
                            type="text"
                            value={editForm.imageAlt}
                            onChange={(e) => setEditForm({ ...editForm, imageAlt: e.target.value })}
                            className="w-full px-3 py-2 border border-gray-300 rounded-md focus:outline-none focus:ring-2 focus:ring-ui-neonBlue focus:border-ui-neonBlue"
                            placeholder="Description of the image"
                          />
                        </div>
                      </div>
                      
                      {/* File Upload Section - Only for Week 2+ */}
                      {selectedWeek >= 2 && (
                        <div className="border-t pt-4">
                          <label className="block text-sm font-medium text-gray-700 mb-2">
                            Upload Local Image (Optional)
                          </label>
                          <div className="space-y-2">
                            <input
                              type="file"
                              accept="image/*"
                              onChange={handleFileChange}
                              className="w-full px-3 py-2 border border-gray-300 rounded-md focus:outline-none focus:ring-2 focus:ring-ui-neonBlue focus:border-ui-neonBlue"
                            />
                            {selectedFile && (
                              <div className="flex items-center space-x-2">
                                <span className="text-sm text-gray-600">{selectedFile.name}</span>
                                <button
                                  type="button"
                                  onClick={async () => {
                                    try {
                                      const imageUrl = await handleFileUpload(selectedFile);
                                      setEditForm({ ...editForm, imageUrl });
                                                                                setSelectedFile(null);
                                                                          } catch (error) {
                                        console.error('Upload error:', error);
                                      }
                                  }}
                                  disabled={uploading}
                                  className="bg-green-500 hover:bg-green-600 disabled:bg-gray-400 text-white px-3 py-1 rounded text-sm"
                                >
                                  {uploading ? 'Uploading...' : 'Upload'}
                                </button>
                              </div>
                            )}
                          </div>
                        </div>
                      )}
                    </div>
                  </div>
                  <div className="flex justify-end space-x-2 mt-4">
                    <button
                      onClick={saveNewTask}
                      disabled={saving}
                      className="bg-indigo-500 hover:bg-indigo-600 text-white px-4 py-2 rounded-lg transition-colors duration-200 flex items-center space-x-2 text-sm"
                    >
                      {saving ? (
                        <div className="animate-spin rounded-full h-4 w-4 border-b-2 border-white"></div>
                      ) : (
                        <>
                          <CheckIcon className="h-4 w-4" />
                          <span>Save Task</span>
                        </>
                      )}
                    </button>
                    <button
                      onClick={cancelAddingTask}
                      className="bg-gray-500 hover:bg-gray-600 text-white px-4 py-2 rounded-lg transition-colors duration-200 flex items-center space-x-2 text-sm"
                    >
                      <XMarkIcon className="h-4 w-4" />
                      <span>Cancel</span>
                    </button>
                  </div>
                </div>
              ) : addingImage ? (
                <div className="bg-white rounded-lg p-6 border border-gray-200 shadow-sm">
                  <div className="flex items-center space-x-3 mb-4">
                    <div className="bg-blue-100 rounded-lg p-2">
                      <PlusIcon className="h-4 w-4 text-blue-600" />
                    </div>
                    <h4 className="text-blue-900 font-semibold text-lg">New Image Task</h4>
                  </div>
                  <div className="space-y-4">
                    <div>
                      <label className="block text-sm font-medium text-gray-700 mb-2">
                        Image URL *
                      </label>
                      <input
                        type="url"
                        value={imageForm.imageUrl}
                        onChange={(e) => setImageForm({ ...imageForm, imageUrl: e.target.value })}
                        className="w-full px-3 py-2 border border-gray-300 rounded-md focus:outline-none focus:ring-2 focus:ring-blue-500 focus:border-blue-500"
                        placeholder="https://example.com/image.jpg"
                      />
                    </div>
                    <div>
                      <label className="block text-sm font-medium text-gray-700 mb-2">
                        Image Alt Text (Optional)
                      </label>
                      <input
                        type="text"
                        value={imageForm.imageAlt}
                        onChange={(e) => setImageForm({ ...imageForm, imageAlt: e.target.value })}
                        className="w-full px-3 py-2 border border-gray-300 rounded-md focus:outline-none focus:ring-2 focus:ring-blue-500 focus:border-blue-500"
                        placeholder="Description of the image"
                      />
                    </div>
                    
                    {/* File Upload Section */}
                    <div className="border-t pt-4">
                      <label className="block text-sm font-medium text-gray-700 mb-2">
                        Upload Local Image (Optional)
                      </label>
                      <div className="space-y-2">
                        <input
                          type="file"
                          accept="image/*"
                          onChange={handleFileChange}
                          className="w-full px-3 py-2 border border-gray-300 rounded-md focus:outline-none focus:ring-2 focus:ring-blue-500 focus:border-blue-500"
                        />
                        {selectedFile && (
                          <div className="flex items-center space-x-2">
                            <span className="text-sm text-gray-600">{selectedFile.name}</span>
                            <button
                              type="button"
                              onClick={async () => {
                                try {
                                  const imageUrl = await handleFileUpload(selectedFile);
                                  setImageForm({ ...imageForm, imageUrl });
                                  setSelectedFile(null);
                                } catch (error) {
                                  console.error('Upload error:', error);
                                }
                              }}
                              disabled={uploading}
                              className="bg-green-500 hover:bg-green-600 disabled:bg-gray-400 text-white px-3 py-1 rounded text-sm"
                            >
                              {uploading ? 'Uploading...' : 'Upload'}
                            </button>
                          </div>
                        )}
                      </div>
                    </div>

                    <div className="grid grid-cols-1 md:grid-cols-2 gap-4">
                      <div>
                        <label className="block text-sm font-medium text-gray-700 mb-2">
                          Image Size
                        </label>
                        <select
                          value={imageForm.imageSize}
                          onChange={(e) => setImageForm({ ...imageForm, imageSize: parseInt(e.target.value) })}
                          className="w-full px-3 py-2 border border-gray-300 rounded-md focus:outline-none focus:ring-2 focus:ring-blue-500 focus:border-blue-500"
                        >
                          <option value={150}>150px</option>
                          <option value={200}>200px</option>
                          <option value={300}>300px</option>
                        </select>
                      </div>
                      <div>
                        <label className="block text-sm font-medium text-gray-700 mb-2">
                          Image Alignment
                        </label>
                        <select
                          value={imageForm.imageAlignment}
                          onChange={(e) => setImageForm({ ...imageForm, imageAlignment: e.target.value as 'left' | 'center' | 'right' })}
                          className="w-full px-3 py-2 border border-gray-300 rounded-md focus:outline-none focus:ring-2 focus:ring-blue-500 focus:border-blue-500"
                        >
                          <option value="left">Left</option>
                          <option value="center">Center</option>
                          <option value="right">Right</option>
                          {selectedWeek >= 4 && <option value="portrait-split">Portrait Split (image left, text+input right)</option>}
                        </select>
                      </div>
                    </div>
                  </div>
                  <div className="flex justify-end space-x-2 mt-4">
                    <button
                      onClick={saveNewImage}
                      disabled={saving}
                      className="bg-blue-500 hover:bg-blue-600 text-white px-4 py-2 rounded-lg transition-colors duration-200 flex items-center space-x-2 text-sm"
                    >
                      {saving ? (
                        <div className="animate-spin rounded-full h-4 w-4 border-b-2 border-white"></div>
                      ) : (
                        <>
                          <CheckIcon className="h-4 w-4" />
                          <span>Save Image</span>
                        </>
                      )}
                    </button>
                    <button
                      onClick={cancelAddingImage}
                      className="bg-gray-500 hover:bg-gray-600 text-white px-4 py-2 rounded-lg transition-colors duration-200 flex items-center space-x-2 text-sm"
                    >
                      <XMarkIcon className="h-4 w-4" />
                      <span>Cancel</span>
                    </button>
                  </div>
                </div>
              ) : (
                <div className="bg-white rounded-lg p-6 border border-gray-200 border-dashed shadow-sm">
                  <div className="flex items-center justify-between">
                    <div className="flex items-center space-x-3">
                      <div className="bg-gray-100 rounded-lg p-2">
                        <PlusIcon className="h-5 w-5 text-gray-600" />
                      </div>
                      <div>
                        <h3 className="text-lg font-semibold text-indigo-900">Add New Tutorial Task</h3>
                        <p className="text-gray-600 text-sm">Create a new tutorial task for Week {selectedWeek}</p>
                      </div>
                    </div>
                    <div className="flex space-x-3">
                      <div className="flex space-x-3">
                        <button
                          onClick={startAddingTask}
                          className="relative inline-flex items-center gap-2 px-4 py-2 rounded-2xl text-sm font-medium text-white ring-1 ring-inset ring-white/50 backdrop-blur-md backdrop-brightness-110 backdrop-saturate-150 shadow-[inset_0_1px_0_rgba(255,255,255,0.6),inset_0_-1px_0_rgba(0,0,0,0.12)] bg-sky-600/70"
                        >
                          <PlusIcon className="h-5 w-5" />
                          <span className="font-medium">Add Task</span>
                        </button>
                        {selectedWeek >= 3 && (
                          <button
                            onClick={startAddingImage}
                            className="relative inline-flex items-center gap-2 px-4 py-2 rounded-2xl text-sm font-medium text-white ring-1 ring-inset ring-white/50 backdrop-blur-md backdrop-brightness-110 backdrop-saturate-150 shadow-[inset_0_1px_0_rgba(255,255,255,0.6),inset_0_-1px_0_rgba(0,0,0,0.12)] bg-sky-600/70"
                          >
                            <PlusIcon className="h-5 w-5" />
                            <span className="font-medium">Add Image</span>
                          </button>
                        )}
                        {selectedWeek >= 5 && (
                          <button
                            onClick={startAddingRevision}
                            className="relative inline-flex items-center gap-2 px-4 py-2 rounded-2xl text-sm font-medium text-white ring-1 ring-inset ring-white/50 backdrop-blur-md backdrop-brightness-110 backdrop-saturate-150 shadow-[inset_0_1px_0_rgba(255,255,255,0.6),inset_0_-1px_0_rgba(0,0,0,0.12)] bg-purple-600/70"
                          >
                            <PlusIcon className="h-5 w-5" />
                            <span className="font-medium">Add Revision</span>
                          </button>
                        )}
                      </div>

                    </div>
                  </div>
                </div>
              )}
            </div>
          )}

          <div ref={listRef} style={{ overflowAnchor: 'none' }}>
          {addingRevision ? (
            <div className="bg-white rounded-xl shadow-lg border border-gray-100 p-8 mb-8">
              <div className="flex items-center justify-between mb-4">
                <div className="flex items-center space-x-3">
                  <div className="bg-purple-100 rounded-lg p-2">
                    <PlusIcon className="h-5 w-5 text-purple-600" />
                  </div>
                  <h3 className="text-lg font-semibold text-gray-900">Add Deep Revision</h3>
                </div>
                <div className="flex items-center space-x-2">
                  <button
                    onClick={addRevision}
                    className="bg-purple-600 hover:bg-purple-700 text-white px-4 py-2 rounded-lg transition-colors duration-200 flex items-center space-x-2 text-sm"
                  >
                    <CheckIcon className="h-4 w-4" />
                    <span>Add</span>
                  </button>
                  <button
                    onClick={cancelAddingRevision}
                    className="bg-gray-500 hover:bg-gray-600 text-white px-4 py-2 rounded-lg transition-colors duration-200 flex items-center space-x-2 text-sm"
                  >
                    <XMarkIcon className="h-4 w-4" />
                    <span>Cancel</span>
                  </button>
                </div>
              </div>
              <div className="border-t pt-4">
                <div className="mx-auto w-full max-w-5xl">
                  <TutorialRefinity weekNumber={selectedWeek} />
                </div>
              </div>
            </div>
          ) : null}
          {revisionAdded ? (
            <div className="bg-white rounded-xl shadow-lg border border-gray-100 p-8 mb-8 max-w-full overflow-x-hidden">
              <div className="flex items-center justify-between mb-4">
                <div className="flex items-center space-x-3">
                  <div className="bg-purple-100 rounded-lg p-2">
                    <PlusIcon className="h-5 w-5 text-purple-600" />
                  </div>
                  <h3 className="text-lg font-semibold text-gray-900">Deep Revision</h3>
                </div>
                {JSON.parse(localStorage.getItem('user') || '{}').role === 'admin' && (
                  <button
                    onClick={removeRevision}
                    className="bg-gray-500 hover:bg-gray-600 text-white px-4 py-2 rounded-lg transition-colors duration-200 flex items-center space-x-2 text-sm"
                  >
                    <XMarkIcon className="h-4 w-4" />
                    <span>Remove</span>
                  </button>
                )}
              </div>
              <div className="border-t pt-4">
                <div className="mx-auto w-full max-w-5xl">
                  <TutorialRefinity weekNumber={selectedWeek} />
                </div>
              </div>
            </div>
          ) : null}
          {tutorialTasks.length === 0 && !addingTask && !addingRevision && !revisionAdded ? (
            <div className="text-center py-12">
              <DocumentTextIcon className="h-12 w-12 text-gray-400 mx-auto mb-4" />
              <h3 className="text-lg font-medium text-gray-900 mb-2">
                No tutorial tasks available
              </h3>
              <p className="text-gray-600">
                Tutorial tasks for Week {selectedWeek} haven't been set up yet.
              </p>
            </div>
          ) : (
            tutorialTasks.map((task) => (
              <div
                key={task._id}
                ref={(el) => { (cardRefs.current[task._id] = el); }}
                className="bg-white rounded-xl shadow-lg border border-gray-100 p-8 hover:shadow-xl transition-shadow duration-300 mb-8"
                style={{ 
                  minHeight: '700px',
                  overflowAnchor: mutatingTaskId === task._id ? 'none' as any : undefined
                }}
              >
                <div className="mb-6">
                  <div className="flex items-center justify-between mb-4">
                    <div className="flex items-center space-x-3">
                      <div className="relative p-2 rounded-2xl bg-white/10 backdrop-blur-md ring-1 ring-inset ring-white/30 shadow-[inset_0_0.5px_0_rgba(255,255,255,0.5),inset_0_-1px_1.5px_rgba(0,0,0,0.12)]">
                        <div className="pointer-events-none absolute inset-0 rounded-2xl opacity-60 [background:linear-gradient(to_bottom,rgba(255,255,255,0.35),rgba(255,255,255,0)_28%),linear-gradient(to_right,rgba(255,255,255,0.35),rgba(255,255,255,0)_28%)]" />
                        <div className="pointer-events-none absolute inset-0 rounded-2xl bg-gradient-to-tr from-white/30 via-white/10 to-transparent opacity-50" />
                        <div className="pointer-events-none absolute rounded-full" style={{ width: '20px', height: '20px', left: '6px', top: '6px', background: 'radial-gradient(12px_12px_at_10px_10px,rgba(255,255,255,0.5),rgba(255,255,255,0)_60%)', opacity: 0.45 }} />
                        <div className="pointer-events-none absolute inset-0 rounded-2xl" style={{ background: 'radial-gradient(120%_120%_at_50%_55%,rgba(0,0,0,0.04),rgba(0,0,0,0)_60%)', opacity: 0.9 }} />
                        <div className="pointer-events-none absolute inset-0 rounded-2xl bg-sky-500/30 mix-blend-overlay opacity-35" />
                        <DocumentTextIcon className="relative h-5 w-5 text-ui-text" />
                      </div>
                      <div>
                        <h3 className="text-lg font-semibold text-gray-900 flex items-center">
                          Source Text #{tutorialTasks.indexOf(task) + 1}
                          {JSON.parse(localStorage.getItem('user') || '{}').role === 'admin' && selectedWeek >= 4 && (
                            <span className="ml-2 inline-flex items-center space-x-1">
                              <button
                                className="px-2 py-1 text-xs bg-gray-100 rounded hover:bg-gray-200"
                                onClick={() => moveTask(task._id, 'up')}
                                title="Move up"
                              >↑</button>
                              <button
                                className="px-2 py-1 text-xs bg-gray-100 rounded hover:bg-gray-200"
                                onClick={() => moveTask(task._id, 'down')}
                                title="Move down"
                              >↓</button>
                            </span>
                          )}
                        </h3>
                      </div>
                    </div>
                    {JSON.parse(localStorage.getItem('user') || '{}').role === 'admin' && (
                      <div className="flex items-center space-x-2">
                        {editingTask === task._id ? (
                          <>
                            <button
                              onClick={saveTask}
                              disabled={saving}
                              className="bg-green-100 hover:bg-green-200 text-green-700 px-3 py-1 rounded-lg transition-colors duration-200"
                            >
                              {saving ? (
                                <div className="animate-spin rounded-full h-4 w-4 border-b-2 border-green-600"></div>
                              ) : (
                                <CheckIcon className="h-4 w-4" />
                              )}
                            </button>
                            <button
                              onClick={cancelEditing}
                              className="bg-red-100 hover:bg-red-200 text-red-700 px-3 py-1 rounded-lg transition-colors duration-200"
                            >
                              <XMarkIcon className="h-4 w-4" />
                            </button>
                          </>
                        ) : (
                          <>
                            <button
                              onClick={() => startEditing(task)}
                              className="bg-gray-100 hover:bg-gray-200 text-gray-700 px-3 py-1 rounded-md transition-colors duration-200 text-sm"
                            >
                              <PencilIcon className="h-4 w-4" />
                            </button>
                            <button
                              onClick={() => deleteTask(task._id)}
                              className="bg-red-100 hover:bg-red-200 text-red-700 px-3 py-1 rounded-lg transition-colors duration-200"
                            >
                              <TrashIcon className="h-4 w-4" />
                            </button>
                          </>
                        )}
                      </div>
                    )}
                  </div>

                  {/* Content - Gradient underlay + glass overlay */}
                  <div className="relative rounded-xl mb-6 p-0 border border-indigo-200">
                    <div className="absolute inset-0 rounded-xl bg-gradient-to-r from-sky-200/60 via-blue-200/50 to-indigo-200/60" />
                    <div className="relative rounded-xl bg-white/10 backdrop-blur-md ring-1 ring-inset ring-white/30 shadow-[inset_0_0.5px_0_rgba(255,255,255,0.5),inset_0_-1px_1.5px_rgba(0,0,0,0.12)] p-6">
                      <div className="pointer-events-none absolute inset-0 rounded-xl opacity-50 [background:linear-gradient(to_bottom,rgba(255,255,255,0.3),rgba(255,255,255,0)_28%),linear-gradient(to_right,rgba(255,255,255,0.28),rgba(255,255,255,0)_28%)]" />
                      <div className="pointer-events-none absolute inset-0 rounded-xl bg-gradient-to-tr from-white/30 via-white/10 to-transparent opacity-45" />
                      <div className="pointer-events-none absolute inset-0 rounded-xl" style={{ background: 'radial-gradient(120% 120% at 50% 55%, rgba(0,0,0,0.03), rgba(0,0,0,0) 60%)' }} />
                      <div className="pointer-events-none absolute inset-0 rounded-xl bg-sky-500/10 mix-blend-overlay opacity-30" />
                    {editingTask === task._id ? (
                      <div className="space-y-4">
                        <div className="flex items-center justify-end space-x-2 mb-2">
                          <button onClick={() => applyInlineFormat('tutorial-newtask-input', editForm.content, v => setEditForm({ ...editForm, content: v }), '**')} className="px-2 py-1 text-xs bg-indigo-100 text-indigo-900 rounded">B</button>
                          <button onClick={() => applyInlineFormat('tutorial-newtask-input', editForm.content, v => setEditForm({ ...editForm, content: v }), '*')} className="px-2 py-1 text-xs bg-indigo-100 text-indigo-900 rounded italic">I</button>
                          <button onClick={() => applyLinkFormat('tutorial-newtask-input', editForm.content || '', v => setEditForm({ ...editForm, content: v }))} className="px-2 py-1 text-xs bg-indigo-100 text-indigo-900 rounded">Link</button>
                        </div>
                        <textarea
                          id="tutorial-newtask-input"
                          value={editForm.content}
                          onChange={(e) => setEditForm({...editForm, content: e.target.value})}
                          className="w-full px-4 py-3 border border-indigo-300 rounded-lg focus:outline-none focus:ring-2 focus:ring-indigo-500 focus:border-indigo-500 bg-white"
                          rows={5}
                          placeholder="Enter source text..."
                        />
                        <div className="space-y-4">
                          <div className="grid grid-cols-1 md:grid-cols-2 gap-4">
                            <div>
                              <label className="block text-sm font-medium text-gray-700 mb-2">Image URL</label>
                              <input
                                type="url"
                                value={editForm.imageUrl}
                                onChange={(e) => setEditForm({...editForm, imageUrl: e.target.value})}
                                className="w-full px-3 py-2 border border-gray-300 rounded-md focus:outline-none focus:ring-2 focus:ring-ui-neonBlue focus:border-ui-neonBlue"
                                placeholder="https://example.com/image.jpg"
                              />
                            </div>
                            <div>
                              <label className="block text-sm font-medium text-gray-700 mb-2">Image Alt Text</label>
                              <input
                                type="text"
                                value={editForm.imageAlt}
                                onChange={(e) => setEditForm({...editForm, imageAlt: e.target.value})}
                                className="w-full px-3 py-2 border border-gray-300 rounded-md focus:outline-none focus:ring-2 focus:ring-ui-neonBlue focus:border-ui-neonBlue"
                                placeholder="Description of the image"
                              />
                            </div>
                          </div>
                          
                          {/* File Upload Section - Only for Week 2+ */}
                          {selectedWeek >= 2 && (
                            <div className="border-t pt-4">
                              <label className="block text-sm font-medium text-gray-700 mb-2">
                                Upload Local Image (Optional)
                              </label>
                              <div className="space-y-2">
                                <input
                                  type="file"
                                  accept="image/*"
                                  onChange={handleFileChange}
                                  className="w-full px-3 py-2 border border-gray-300 rounded-md focus:outline-none focus:ring-2 focus:ring-ui-neonBlue focus:border-ui-neonBlue"
                                />
                                {selectedFile && (
                                  <div className="flex items-center space-x-2">
                                    <span className="text-sm text-gray-600">{selectedFile.name}</span>
                                    <button
                                      type="button"
                                      onClick={async () => {
                                        try {
                                          const imageUrl = await handleFileUpload(selectedFile);
                                          console.log('Uploaded image URL:', imageUrl);
                                          setEditForm({ ...editForm, imageUrl });
                                          console.log('Updated editForm:', { ...editForm, imageUrl });
                                          
                                          // Save the task with the new image URL
                                          if (editingTask) {
                                            console.log('Saving task with image URL:', imageUrl);
                                            const response = await api.put(`/api/auth/admin/tutorial-tasks/${editingTask}`, {
                                              ...editForm,
                                              imageUrl,
                                              weekNumber: selectedWeek
                                            });
                                            console.log('Task save response:', response.data);
                                            
                                            if (response.status >= 200 && response.status < 300) {
                                              await fetchTutorialTasks(false); // Refresh tasks
                                            }
                                          }
                                          
                                          setSelectedFile(null);
                                        } catch (error) {
                                          console.error('Upload error:', error);
                                        }
                                      }}
                                      disabled={uploading}
                                      className="bg-green-500 hover:bg-green-600 disabled:bg-gray-400 text-white px-3 py-1 rounded text-sm"
                                    >
                                      {uploading ? 'Uploading...' : 'Upload'}
                                    </button>
                                  </div>
                                )}
                              </div>
                            </div>
                          )}
                        </div>
                      </div>
                    ) : (
                      <div className="space-y-4">
                        {task.imageUrl ? (
                          task.imageAlignment === 'portrait-split' && selectedWeek >= 4 ? (
                            // Portrait split layout
                            <div className="grid grid-cols-1 md:grid-cols-2 gap-6 items-start">
                              <div className="w-full flex justify-center">
                                <div className="inline-block rounded-lg shadow-md overflow-hidden">
                                  <img src={task.imageUrl} alt={task.imageAlt || 'Uploaded image'} className="w-auto h-auto" style={{ maxHeight: '520px', maxWidth: '100%', objectFit: 'contain' }} />
                                </div>
                              </div>
                              <div className="w-full">
                                <div className="bg-indigo-50 rounded-lg p-4 mb-4 border border-indigo-200">
                                  <h5 className="text-indigo-900 font-semibold mb-2">Source Text (from image)</h5>
                                  <div
                                    id={`tutorial-source-${task._id}`}
                                    className="text-blue-800 leading-relaxed text-lg font-source-text whitespace-pre-wrap"
                                  >{renderFormatted(task.content)}</div>
                                </div>
                                {localStorage.getItem('token') && (
                                  <div className="bg-white rounded-lg p-4 border border-gray-200">
                                    <h5 className="text-gray-900 font-semibold mb-2">Your Group's Translation</h5>
                                    {/* Group selection (same as bottom block), shown here for portrait-split */}
                                    <div className="mb-2">
                                      <label className="block text-xs font-medium text-gray-700 mb-1">Select Your Group</label>
                                      <select
                                        value={selectedGroups[task._id] || ''}
                                        onChange={(e) => {
                                          const v = parseInt(e.target.value);
                                          setSelectedGroups({ ...selectedGroups, [task._id]: Number.isNaN(v) ? ('' as any) : v });
                                        }}
                                        className="w-40 px-2 py-1 border border-gray-300 rounded focus:outline-none focus:ring-2 focus:ring-indigo-500 focus:border-indigo-500 bg-white text-xs"
                                      >
                                        <option value="">Choose...</option>
                                        {[1,2,3,4,5,6,7,8].map((g) => (<option key={g} value={g}>Group {g}</option>))}
                                      </select>
                                    </div>
                                    <div className="flex items-center justify-end space-x-2 mb-2">
                                      <button onClick={() => applyInlineFormat(`tutorial-translation-${task._id}`, translationText[task._id] || '', v => setTranslationText({ ...translationText, [task._id]: v }), '**')} className="px-2 py-1 text-xs bg-indigo-100 text-indigo-900 rounded">B</button>
                                      <button onClick={() => applyInlineFormat(`tutorial-translation-${task._id}`, translationText[task._id] || '', v => setTranslationText({ ...translationText, [task._id]: v }), '*')} className="px-2 py-1 text-xs bg-indigo-100 text-indigo-900 rounded italic">I</button>
                                      <button onClick={() => applyLinkFormat(`tutorial-translation-${task._id}`, translationText[task._id] || '', v => setTranslationText({ ...translationText, [task._id]: v }))} className="px-2 py-1 text-xs bg-indigo-100 text-indigo-900 rounded">Link</button>
                                    </div>
                                      <textarea
                                        id={`tutorial-translation-${task._id}`}
                                        value={translationText[task._id] || ''}
                                       
                                      onChange={(e) => {
                                        const value = e.target.value;
                                        setTranslationText({ ...translationText, [task._id]: value });
                                      }}
                                        className="w-full px-4 py-3 border border-gray-300 rounded-lg focus:outline-none focus:ring-2 focus:ring-indigo-500 focus:border-indigo-500 bg-white"
                                      style={{ height: '150px' }}
                                        rows={4}
                                        placeholder="Enter your group's translation here..."
                                      />
                                    <div className="flex justify-end mt-2">
                                      <button onClick={() => handleSubmitTranslation(task._id)} disabled={submitting[task._id]} className="btn-primary disabled:bg-gray-400 text-white px-4 py-2 rounded-lg text-sm">{submitting[task._id] ? 'Submitting...' : 'Submit Translation'}</button>
                                    </div>
                                  </div>
                                )}
                              </div>
                            </div>
                          ) : task.content === 'Image-based task' ? (
                            // Image-only layout with dynamic sizing and alignment
                            <div className={`flex flex-col md:flex-row gap-6 items-start ${
                              task.imageAlignment === 'left' ? 'md:flex-row' : 
                              task.imageAlignment === 'right' ? 'md:flex-row-reverse' : 
                              'md:flex-col'
                            }`}>
                              {/* Image section */}
                              <div className={`${
                                task.imageAlignment === 'left' ? 'w-full md:w-1/2' :
                                task.imageAlignment === 'right' ? 'w-full md:w-1/2' :
                                'w-full'
                              } flex ${
                                task.imageAlignment === 'left' ? 'justify-start' :
                                task.imageAlignment === 'right' ? 'justify-end' :
                                'justify-center'
                              }`}>
                                <div className="inline-block rounded-lg shadow-md overflow-hidden">
                                  <img src={task.imageUrl} alt={task.imageAlt || 'Uploaded image'} className="w-full h-auto" style={{ height: `${task.imageSize || 200}px`, width: 'auto', objectFit: 'contain' }} onError={(e) => { console.error('Image failed to load:', e); (e.currentTarget as HTMLImageElement).style.display = 'none'; }} />
                                  {task.imageAlt && (<div className="text-xs text-gray-500 mt-2 text-center">Alt: {task.imageAlt}</div>)}
                                </div>
                              </div>
                            </div>
                          ) : (
                            // Regular task layout - use grid for stability like image-only layout
                            <div className="grid grid-cols-1 md:grid-cols-2 gap-6 items-start">
                              <div className="w-full flex justify-center">
                                {task.imageUrl && task.imageUrl.startsWith('data:') ? (
                                  <div className="inline-block rounded-lg shadow-md overflow-hidden">
                                    <img src={task.imageUrl} alt={task.imageAlt || 'Uploaded image'} className="w-full h-auto" style={{ height: '200px', width: 'auto', objectFit: 'contain' }} onError={(e) => { console.error('Image failed to load:', e); (e.currentTarget as HTMLImageElement).style.display = 'none'; }} />
                                  </div>
                                ) : task.imageUrl ? (
                                  <div className="inline-block rounded-lg shadow-md bg-green-500 text-white p-6 text-center">
                                    <div className="text-3xl mb-2">📷</div>
                                    <div className="font-semibold">Image Uploaded</div>
                                    <div className="text-sm opacity-75">{task.imageUrl}</div>
                                  </div>
                                ) : (
                                  <div className="w-full h-48 bg-gray-100 rounded-lg flex items-center justify-center">
                                    <div className="text-gray-500">No image</div>
                                  </div>
                                )}
                              </div>
                              <div className="w-full">
                                <div className="text-indigo-900 leading-relaxed text-lg font-source-text whitespace-pre-wrap">{renderFormatted(task.content)}</div>
                              </div>
                            </div>
                          )
                        ) : (
                          // Text only when no image
                          <div>
                            <div className="text-indigo-900 leading-relaxed text-lg font-source-text whitespace-pre-wrap">{renderFormatted(task.content)}</div>
                          </div>
                        )}
                        {(() => { console.log('Task imageUrl check:', task._id, task.imageUrl, !!task.imageUrl); return null; })()}
                      </div>
                    )}
                    </div>
                  </div>


                </div>

                {/* All Submissions for this Task */}
                {userSubmissions[task._id] && userSubmissions[task._id].length > 0 && (
                  <div ref={(el) => { submissionsContainerRefs.current[task._id] = el; }} className="bg-gradient-to-r from-white to-indigo-50 rounded-xl p-6 mb-6 border border-stone-200" style={{ overflowAnchor: 'none' }}>
                    <div className="flex items-center justify-between mb-4">
                      <div className="flex items-center space-x-2">
                        <div className="bg-indigo-100 rounded-full p-1">
                          <CheckCircleIcon className="h-4 w-4 text-indigo-900" />
                        </div>
                        <h4 className="text-stone-900 font-semibold text-lg">All Submissions ({userSubmissions[task._id].length})</h4>
                      </div>
                      <button
                        onClick={() => toggleExpanded(task._id)}
                        className="flex items-center space-x-1 text-indigo-900 hover:text-indigo-900 text-sm font-medium"
                      >
                        <span>{expandedSections[task._id] ? 'Collapse' : 'Expand'}</span>
                        <svg
                          className={`w-4 h-4 transition-transform duration-200 ${expandedSections[task._id] ? 'rotate-180' : ''}`}
                          fill="none"
                          stroke="currentColor"
                          viewBox="0 0 24 24"
                        >
                          <path strokeLinecap="round" strokeLinejoin="round" strokeWidth={2} d="M19 9l-7 7-7-7" />
                        </svg>
                      </button>
                    </div>
                    <div ref={(el) => { submissionsGridRefs.current[task._id] = el; }} className={`grid grid-cols-1 sm:grid-cols-2 lg:grid-cols-3 gap-3 ${
                      expandedSections[task._id] 
                        ? 'max-h-none overflow-visible' 
                        : 'max-h-0 overflow-hidden'
                    }`} data-grid-id={task._id}>
                      {userSubmissions[task._id].map((submission, index) => (
                        <div key={submission._id} className="bg-white rounded-lg p-3 border border-stone-200 flex flex-col justify-between h-full">
                          <div className="flex items-center justify-between mb-2">
                            <div className="flex items-center space-x-2">
                              {submission.isOwner && (
                                <span className="inline-block bg-purple-100 text-purple-800 text-xs px-1.5 py-0.5 rounded-full">
                                  Your Submission
                                </span>
                              )}
                            </div>
                            {getStatusIcon(submission.status)}
                          </div>
                          <p className="text-stone-800 leading-relaxed text-base mb-2 font-smiley whitespace-pre-wrap break-words">{renderFormatted(submission.transcreation || '')}</p>
                          <div className="flex items-center space-x-4 text-xs text-stone-700 mt-auto">
                            <div className="flex items-center space-x-1">
                              <span className="font-medium">Group:</span>
                              <span className="bg-indigo-100 px-1.5 py-0.5 rounded-full text-indigo-900 text-xs">
                                {submission.groupNumber}
                              </span>
                            </div>
                            <div className="flex items-center space-x-1">
                              <span className="font-medium">Votes:</span>
                              <span className="bg-indigo-100 px-1.5 py-0.5 rounded-full text-indigo-900 text-xs">
                                {(submission.voteCounts?.['1'] || 0) + (submission.voteCounts?.['2'] || 0) + (submission.voteCounts?.['3'] || 0)}
                              </span>
                            </div>
                          </div>
                          <div className="flex items-center space-x-2 mt-2">
                            {(submission.isOwner || (JSON.parse(localStorage.getItem('user') || '{}').role === 'admin')) && (
                              <button
                                onClick={() => handleEditSubmission(submission._id, submission.transcreation)}
                                className="text-indigo-900 hover:text-indigo-900 text-sm font-medium"
                              >
                                Edit
                              </button>
                            )}
                            {JSON.parse(localStorage.getItem('user') || '{}').role === 'admin' && (
                              <button
                                onClick={() => handleDeleteSubmission(submission._id, task._id)}
                                className="text-red-600 hover:text-red-800 text-sm font-medium"
                              >
                                Delete
                              </button>
                            )}
                          </div>
                        </div>
                      ))}
                    </div>
                    
                  </div>
                )}

                                  {/* Translation Input (always show if user is logged in, but hide for image-only content) */}
                {localStorage.getItem('token') && task.content !== 'Image-based task' && (
                  <TranslationSection
                    taskId={task._id}
                    selectedGroup={selectedGroups[task._id] || ''}
                    translation={translationText[task._id] || ''}
                    submitting={!!submitting[task._id]}
                    onGroupChange={(n) => setSelectedGroups(prev => ({ ...prev, [task._id]: n as number }))}
                    onTranslationChange={(v) => setTranslationText(prev => ({ ...prev, [task._id]: v }))}
                    onSubmit={(localText, localGroup) => handleSubmitTranslation(task._id, localText, localGroup)}
                    label="Select Your Group"
                    selectWidthClass="w-40"
                    containerClassName="bg-white rounded-lg p-4 border border-gray-200"
                  />
                )}

                {/* Show login message for visitors */}
                {!localStorage.getItem('token') && (
                  <div className="relative rounded-xl p-6 border border-gray-200">
                    <div className="absolute inset-0 rounded-xl bg-gradient-to-r from-gray-100/70 to-indigo-100/60" />
                    <div className="relative rounded-xl bg-white/10 backdrop-blur-md ring-1 ring-inset ring-white/30 shadow-[inset_0_0.5px_0_rgba(255,255,255,0.5),inset_0_-1px_1.5px_rgba(0,0,0,0.12)] p-6">
                      <div className="pointer-events-none absolute inset-0 rounded-xl opacity-50 [background:linear-gradient(to_bottom,rgba(255,255,255,0.3),rgba(255,255,255,0)_28%),linear-gradient(to_right,rgba(255,255,255,0.28),rgba(255,255,255,0)_28%)]" />
                    <div className="flex items-center space-x-2 mb-4">
                        <div className="relative p-1.5 rounded-2xl bg-white/10 backdrop-blur-md ring-1 ring-inset ring-white/30 shadow-[inset_0_0.5px_0_rgba(255,255,255,0.5),inset_0_-1px_1.5px_rgba(0,0,0,0.12)]">
                          <div className="pointer-events-none absolute inset-0 rounded-2xl opacity-60 [background:linear-gradient(to_bottom,rgba(255,255,255,0.35),rgba(255,255,255,0)_28%),linear-gradient(to_right,rgba(255,255,255,0.35),rgba(255,255,255,0)_28%)]" />
                          <DocumentTextIcon className="relative h-4 w-4 text-gray-700" />
                      </div>
                      <h4 className="text-gray-900 font-semibold text-lg">Login Required</h4>
                    </div>
                    <p className="text-gray-700 mb-4">
                      Please log in to submit translations for this tutorial task.
                    </p>
                      <button onClick={() => window.location.href = '/login'} className="relative inline-flex items-center justify-center gap-2 px-4 py-2 rounded-2xl text-sm font-medium text-white ring-1 ring-inset ring-white/50 backdrop-blur-md backdrop-brightness-110 backdrop-saturate-150 shadow-[inset_0_1px_0_rgba(255,255,255,0.6),inset_0_-1px_0_rgba(0,0,0,0.12)] bg-sky-600/70 transition-all duration-200">
                      Go to Login
                      <ArrowRightIcon className="h-4 w-4 ml-2" />
                    </button>
                    </div>
                  </div>
                )}
              </div>
            ))
          )}
          </div>
        </div>
        </>
        )}
      </div>

      {/* Edit Submission Modal */}
      {editingSubmission && (
        <div className="fixed inset-0 bg-black bg-opacity-50 flex items-center justify-center z-50">
          <div className="bg-white rounded-lg p-6 w-full max-w-2xl mx-4">
            <div className="flex items-center justify-between mb-4">
              <h3 className="text-lg font-semibold text-gray-900">Edit Translation</h3>
              <button
                onClick={cancelEditSubmission}
                className="text-gray-400 hover:text-gray-600"
              >
                <XMarkIcon className="h-6 w-6" />
              </button>
            </div>
            <div className="mb-4">
              <textarea
                value={editSubmissionText}
                onChange={(e) => setEditSubmissionText(e.target.value)}
                className="w-full px-4 py-3 border border-gray-300 rounded-lg focus:outline-none focus:ring-2 focus:ring-purple-500 focus:border-purple-500"
                rows={6}
                placeholder="Enter your translation..."
              />
            </div>
            <div className="flex justify-end space-x-3">
              <button
                onClick={cancelEditSubmission}
                className="px-4 py-2 text-gray-600 hover:text-gray-800 border border-gray-300 rounded-lg"
              >
                Cancel
              </button>
              <button
                onClick={saveEditedSubmission}
                className="px-4 py-2 bg-purple-600 text-white rounded-lg hover:bg-purple-700"
              >
                Save Changes
              </button>
            </div>
          </div>
        </div>
      )}
    </div>
  );
};

const TranslationSection: React.FC<{
  taskId: string;
  selectedGroup: number | '';
  translation: string;
  submitting: boolean;
  onGroupChange: (n: number) => void;
  onTranslationChange: (v: string) => void;
  onSubmit: (localText: string, localGroup: number) => void;
  label?: string;
  selectWidthClass?: string;
  containerClassName?: string;
}> = React.memo(({ taskId, selectedGroup, translation, submitting, onGroupChange, onTranslationChange, onSubmit, label = 'Select Your Group *', selectWidthClass = 'w-48', containerClassName = 'bg-white rounded-lg p-6 border border-gray-200 shadow-sm safari-stable-form' }) => {
  const [localText, setLocalText] = React.useState<string>(translation || '');
  const [localGroup, setLocalGroup] = React.useState<number | ''>(selectedGroup || '');
  React.useEffect(() => { setLocalText(translation || ''); }, [taskId]);
  React.useEffect(() => { setLocalGroup(selectedGroup || ''); }, [taskId, selectedGroup]);
  const isSafariEnv = typeof navigator !== 'undefined' && /Safari\//.test(navigator.userAgent) && !/Chrome\//.test(navigator.userAgent) && !/CriOS\//.test(navigator.userAgent);
  const submitTriggeredRef = React.useRef(false);
  const triggerSubmitOnce = React.useCallback((e?: React.SyntheticEvent) => {
    if (submitTriggeredRef.current) { e?.preventDefault?.(); return; }
    submitTriggeredRef.current = true;
    if (localGroup) onSubmit(localText, localGroup as number);
    // Small cooldown to avoid duplicate fire from click after pointerdown
    setTimeout(() => { submitTriggeredRef.current = false; }, 400);
  }, [localText, localGroup, onSubmit]);
  const wrapSelection = (wrapper: '**' | '*') => {
    const el = document.getElementById(`tutorial-translation-${taskId}`) as HTMLTextAreaElement | null;
    const current = localText || '';
    if (!el) { const next = current + wrapper + wrapper; setLocalText(next); return; }
    const start = el.selectionStart ?? current.length;
    const end = el.selectionEnd ?? current.length;
    const before = current.slice(0, start);
    const selection = current.slice(start, end);
    const after = current.slice(end);
    const next = `${before}${wrapper}${selection}${wrapper}${after}`;
    setLocalText(next);
    setTimeout(() => { try { el.focus(); el.setSelectionRange(before.length + wrapper.length + selection.length + wrapper.length, before.length + wrapper.length + selection.length + wrapper.length); } catch {} }, 0);
  };
  const insertLink = () => {
    const urlRaw = window.prompt('Enter URL', 'https://') || '';
    let url = urlRaw.trim();
    if (!url) return;
    url = url.replace(/["'>)\s]+$/g, '');
    const el = document.getElementById(`tutorial-translation-${taskId}`) as HTMLTextAreaElement | null;
    const current = localText || '';
    if (!el) { const next = `${current}[link](${url})`; setLocalText(next); return; }
    const start = el.selectionStart ?? current.length;
    const end = el.selectionEnd ?? current.length;
    const before = current.slice(0, start);
    const sel = current.slice(start, end) || 'link';
    const after = current.slice(end);
    const next = `${before}[${sel}](${url})${after}`;
    setLocalText(next);
    setTimeout(() => { try { el.focus(); const newPos = before.length + sel.length + 4 + url.length + 2; el.setSelectionRange(newPos, newPos); } catch {} }, 0);
  };
  return (
    <div className={containerClassName}>
      <div className="flex items-center space-x-3 mb-4">
        <div className="bg-gray-100 rounded-lg p-2">
          <DocumentTextIcon className="h-4 w-4 text-gray-600" />
        </div>
        <h4 className="text-gray-900 font-semibold text-lg">Group Translation</h4>
      </div>
      <div className="mb-4">
        <label className="block text-sm font-medium text-gray-700 mb-2">{label}</label>
        <select
          value={localGroup || ''}
          onChange={(e) => {
            const value = parseInt(e.target.value);
            const next = Number.isNaN(value) ? ('' as any) : value;
            setLocalGroup(next);
            // Parent sync deferred to submit; no immediate parent state update to minimize re-renders
          }}
          className={`${selectWidthClass} px-3 py-2 border border-gray-300 rounded-lg focus:outline-none focus:ring-2 focus:ring-indigo-500 focus:border-indigo-500 bg-white text-sm`}
          required
        >
          <option value="">Choose your group...</option>
          {[1, 2, 3, 4, 5, 6, 7, 8].map((group) => (
            <option key={group} value={group}>Group {group}</option>
          ))}
        </select>
      </div>
      <div className="flex items-center justify-end space-x-2 mb-2">
        <button onClick={() => wrapSelection('**')} className="px-2 py-1 text-xs bg-indigo-100 text-indigo-900 rounded">B</button>
        <button onClick={() => wrapSelection('*')} className="px-2 py-1 text-xs bg-indigo-100 text-indigo-900 rounded italic">I</button>
        <button onClick={insertLink} className="px-2 py-1 text-xs bg-indigo-100 text-indigo-900 rounded">Link</button>
      </div>
      <textarea
        id={`tutorial-translation-${taskId}`}
        value={localText || ''}
        onChange={(e) => { setLocalText(e.target.value); }}
        className="w-full px-4 py-3 border border-gray-300 rounded-lg focus:outline-none focus:ring-2 focus:ring-indigo-500 focus:border-indigo-500 bg-white"
        style={{ height: '150px' }}
        rows={4}
        placeholder="Enter your group's translation here..."
      />
      <button
        type="button"
        onClick={!isSafariEnv ? (e) => { e.preventDefault(); triggerSubmitOnce(e); } : undefined}
        onPointerDown={isSafariEnv ? (e) => { e.preventDefault(); triggerSubmitOnce(e as unknown as React.SyntheticEvent); } : undefined}
        disabled={submitting}
        className="relative inline-flex items-center justify-center gap-2 px-4 py-2 rounded-2xl text-sm font-medium text-white ring-1 ring-inset ring-white/50 backdrop-blur-md backdrop-brightness-110 backdrop-saturate-150 shadow-[inset_0_1px_0_rgba(255,255,255,0.6),inset_0_-1px_0_rgba(0,0,0,0.12)] bg-sky-600/70 disabled:bg-gray-400 mt-2"
        style={{ touchAction: 'manipulation' as any }}
      >
        {submitting ? (
          <>
            <div className="animate-spin rounded-full h-4 w-4 border-b-2 border-white"></div>
            <span>Submitting...</span>
          </>
        ) : (
          <>
            <CheckIcon className="h-4 w-4" />
            <span>Submit Translation</span>
          </>
        )}
      </button>
    </div>
  );
}, (a, b) => (
  a.selectedGroup === b.selectedGroup &&
  a.translation === b.translation &&
  a.submitting === b.submitting &&
  a.taskId === b.taskId
));

export default TutorialTasks;