File size: 76,693 Bytes
1662122
cba4ca4
 
04a979b
 
cba4ca4
 
04a979b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1957ae8
 
04a979b
 
 
 
 
 
 
 
 
 
 
 
 
cba4ca4
 
 
 
 
 
04a979b
 
 
 
cba4ca4
04a979b
 
cba4ca4
107850d
04a979b
107850d
 
 
 
cba4ca4
 
 
 
 
 
 
 
 
 
1662122
 
 
 
 
 
f2371d2
9281b3a
7d5ddfc
 
27fa7af
 
 
 
 
 
 
 
 
 
 
 
 
 
7d5ddfc
 
 
d00e691
 
 
 
7d5ddfc
 
d00e691
7d5ddfc
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
d00e691
7d5ddfc
 
 
 
d00e691
7d5ddfc
 
 
 
 
 
d00e691
7d5ddfc
 
 
 
d00e691
1d21202
 
d00e691
1d21202
 
 
7d5ddfc
 
 
 
 
 
 
 
d00e691
7d5ddfc
 
 
 
 
d00e691
7d5ddfc
 
 
d00e691
7d5ddfc
 
 
 
 
 
 
 
 
d00e691
7d5ddfc
 
 
d00e691
7d5ddfc
 
 
 
 
 
d00e691
7d5ddfc
 
 
 
 
 
d00e691
7d5ddfc
 
 
 
d00e691
7d5ddfc
 
d00e691
7d5ddfc
 
d00e691
7d5ddfc
 
 
 
 
 
 
d00e691
7d5ddfc
 
1662122
3fc14e6
20756da
 
 
c935b3b
04a979b
1957ae8
 
 
04a979b
 
 
1957ae8
 
 
 
 
 
 
04a979b
1957ae8
fa2481d
04a979b
 
1957ae8
04a979b
c935b3b
04a979b
 
 
 
 
 
20756da
 
 
 
 
3fc14e6
 
 
1957ae8
 
08dc5ac
 
3fc14e6
 
 
08dc5ac
1957ae8
 
08dc5ac
 
3fc14e6
 
24357b3
 
 
 
 
 
 
 
 
 
 
 
909d524
 
 
 
 
 
 
24357b3
909d524
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1662122
 
3fc14e6
 
1662122
f7e9a47
 
357f1ab
 
 
24357b3
 
 
 
 
357f1ab
f7e9a47
 
357f1ab
1662122
f7e9a47
 
1662122
 
357f1ab
1662122
357f1ab
 
 
 
f7e9a47
 
 
 
 
 
 
 
 
357f1ab
f2371d2
 
 
 
357f1ab
f2371d2
 
 
 
 
 
 
 
 
 
 
357f1ab
 
7d5ddfc
 
 
 
 
 
 
d00e691
 
 
 
 
 
 
7d5ddfc
d00e691
7d5ddfc
 
 
d00e691
7d5ddfc
 
d00e691
7d5ddfc
 
 
 
 
d00e691
 
 
 
 
7d5ddfc
 
d00e691
 
 
 
 
 
 
7d5ddfc
 
 
 
d00e691
7d5ddfc
 
d00e691
7d5ddfc
 
 
 
 
 
 
 
 
4534d35
a65461a
7d5ddfc
 
357f1ab
 
 
 
 
 
3fc14e6
357f1ab
 
 
 
 
 
9281b3a
 
 
 
 
 
 
357f1ab
 
 
cba4ca4
1957ae8
 
04a979b
 
cba4ca4
 
04a979b
 
cba4ca4
 
27fa7af
357f1ab
 
 
 
 
f7e9a47
1662122
 
 
 
 
 
 
 
 
27fa7af
 
358f2af
27fa7af
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1662122
 
 
 
 
 
 
 
 
 
 
 
 
27fa7af
 
1662122
27fa7af
db2b609
1662122
 
 
 
24357b3
 
 
 
 
 
 
 
 
 
 
27fa7af
 
24357b3
27fa7af
db2b609
24357b3
 
 
 
 
 
4f26e4c
 
 
27fa7af
4f26e4c
 
27fa7af
 
 
4f26e4c
 
 
 
 
 
358f2af
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
27fa7af
 
358f2af
27fa7af
 
 
 
 
 
 
 
 
 
358f2af
 
 
 
 
27fa7af
 
 
358f2af
27fa7af
358f2af
 
27fa7af
358f2af
 
27fa7af
358f2af
 
 
 
 
 
 
27fa7af
358f2af
 
 
 
 
 
 
 
27fa7af
358f2af
27fa7af
 
358f2af
27fa7af
 
 
 
 
 
 
 
 
358f2af
 
27fa7af
358f2af
27fa7af
358f2af
27fa7af
 
 
 
358f2af
 
 
 
 
 
 
 
 
 
 
 
 
27fa7af
358f2af
 
 
 
27fa7af
358f2af
 
27fa7af
 
 
 
 
 
 
 
 
 
358f2af
27fa7af
 
 
 
1662122
1551679
27fa7af
 
1662122
1551679
04a979b
27fa7af
db2b609
27fa7af
 
 
 
1551679
 
 
1662122
1957ae8
 
04a979b
 
 
f7e9a47
cba4ca4
27fa7af
04a979b
 
 
 
 
1662122
 
f7e9a47
6362620
27fa7af
 
 
6362620
 
 
 
27fa7af
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
f7e9a47
6362620
 
 
 
 
 
 
 
 
 
 
 
f7e9a47
20756da
 
 
1662122
 
 
27fa7af
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1662122
27fa7af
 
cba4ca4
27fa7af
 
 
cba4ca4
1662122
 
6aff739
 
c935b3b
6aff739
 
 
 
 
8cbc920
 
 
 
 
 
9281b3a
 
 
 
 
 
 
 
 
 
 
 
 
6aff739
9281b3a
8cbc920
 
 
 
 
04a979b
 
 
 
 
1957ae8
 
 
 
 
 
 
 
c935b3b
 
 
9281b3a
 
 
 
 
 
c935b3b
9281b3a
 
 
 
 
 
 
 
 
c935b3b
9281b3a
 
 
 
 
 
 
 
6aff739
9281b3a
 
1662122
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
f32158c
 
 
 
 
 
 
 
6122643
 
 
 
 
f32158c
6122643
 
f32158c
 
 
 
 
 
6122643
 
 
 
 
f32158c
6122643
 
f32158c
 
1662122
 
c28132e
 
1662122
 
 
 
 
5ff00d2
 
 
1662122
 
 
 
 
 
c5ab8cb
 
1662122
 
5ff00d2
 
 
3823d23
5ff00d2
 
1662122
 
 
 
 
 
 
 
 
1acb4c5
 
 
 
 
1662122
 
 
1acb4c5
 
1662122
3a07883
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1662122
 
3a07883
 
 
 
 
 
 
 
 
 
1662122
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
76079b2
1662122
c5ab8cb
1662122
 
76079b2
 
 
1662122
76079b2
 
 
 
7cd06eb
76079b2
 
1662122
 
76079b2
 
1662122
 
 
 
76079b2
 
1662122
 
 
 
 
 
 
76079b2
7e40bff
 
 
 
1662122
 
 
dc9d49c
1662122
 
 
dc9d49c
1662122
 
 
7cd06eb
 
 
 
21eed6c
 
 
 
 
 
 
 
7cd06eb
dc9d49c
 
7cd06eb
 
 
dc9d49c
7cd06eb
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
21eed6c
76079b2
 
 
 
 
 
 
 
 
 
 
 
 
 
7cd06eb
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
76079b2
7cd06eb
 
 
 
 
 
 
 
 
 
 
 
21eed6c
dc9d49c
21eed6c
 
 
 
 
 
 
7e40bff
 
7cd06eb
 
 
 
dc9d49c
7cd06eb
dc9d49c
7cd06eb
 
 
 
 
 
1662122
 
 
 
 
 
 
 
 
 
 
 
 
 
c5ab8cb
1662122
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
c5ab8cb
1662122
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
c53f4d8
d3d51f7
 
 
 
 
08dc5ac
c935b3b
c53f4d8
d3d51f7
 
 
 
 
 
c53f4d8
1662122
 
 
 
 
 
 
 
 
 
 
 
 
 
 
357f1ab
1662122
 
357f1ab
1662122
1957ae8
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
357f1ab
1957ae8
 
 
 
357f1ab
1957ae8
357f1ab
1662122
 
9281b3a
 
 
 
 
 
 
1662122
 
 
 
c28132e
 
7d5ddfc
 
 
c28132e
c5ab8cb
 
 
 
 
1662122
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
2c18ac1
 
 
 
1662122
 
2c18ac1
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1662122
2c18ac1
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1662122
 
 
 
b915bba
bde310a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
08dc5ac
 
bde310a
 
 
 
 
 
 
 
 
1662122
 
 
 
 
 
 
 
fd50b03
 
2983898
fd50b03
2983898
fd50b03
1662122
 
 
2983898
 
 
 
1662122
 
 
af79978
1662122
 
 
 
 
 
 
3fc14e6
1662122
 
 
 
 
 
c28132e
1662122
9d960e3
 
 
 
1662122
 
 
c28132e
 
 
 
1662122
 
af79978
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
9d960e3
af79978
 
 
 
 
 
 
 
 
 
 
2983898
af79978
2983898
af79978
 
 
1662122
 
 
 
 
6f77b06
 
 
 
 
 
 
 
 
 
 
 
1acb4c5
6f77b06
1acb4c5
 
6f77b06
 
 
 
1acb4c5
 
 
 
1662122
 
1acb4c5
6f77b06
 
 
1acb4c5
6f77b06
 
 
 
 
 
 
 
 
 
1acb4c5
1662122
 
f2371d2
 
 
1acb4c5
f2371d2
 
4f26e4c
 
04a979b
 
 
4f26e4c
 
04a979b
 
4f26e4c
 
27fa7af
f2371d2
1acb4c5
 
1662122
 
42f1ad2
 
3fc14e6
 
b915bba
 
 
 
 
 
2c18ac1
42f1ad2
 
3fc14e6
42f1ad2
 
3fc14e6
42f1ad2
 
 
 
 
d3d51f7
 
42f1ad2
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
2d00472
c935b3b
 
 
2d00472
 
 
 
 
d3d51f7
 
2d00472
42f1ad2
 
 
 
d00e691
42f1ad2
 
d00e691
42f1ad2
 
d00e691
42f1ad2
 
d00e691
42f1ad2
 
d00e691
42f1ad2
 
d00e691
42f1ad2
 
d00e691
42f1ad2
 
d00e691
3fc14e6
 
42f1ad2
 
d00e691
42f1ad2
 
d00e691
3fc14e6
2d00472
d00e691
2d00472
 
d00e691
2d00472
3fc14e6
42f1ad2
 
d00e691
 
42f1ad2
 
3fc14e6
7d5ddfc
d00e691
7d5ddfc
d00e691
 
 
 
 
 
 
7d5ddfc
d00e691
 
 
 
 
 
 
7d5ddfc
d00e691
 
 
 
 
 
 
7d5ddfc
d00e691
 
 
 
 
 
 
7d5ddfc
d00e691
 
 
 
 
 
1662122
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
# server.R for DWD App

# Helper function for label generation
generate_station_label <- function(name, id, state, start_date, end_date, detailed_summary,
                                   overall_start = NULL, overall_end = NULL, resolution = NULL) {
    tryCatch(
        {
            format_range <- function(start_val, end_val) {
                format_val <- function(value, is_end = FALSE) {
                    if (is.null(value) || is.na(value) || value == "") {
                        return("Unknown")
                    }

                    if (inherits(value, "Date")) {
                        return(format(value, "%Y-%m-%d"))
                    }

                    value <- as.character(value)
                    if (value == "99999999") {
                        return(if (is_end) "Present" else "Unknown")
                    }
                    if (value == "00000000") {
                        return("Unknown")
                    }

                    if (grepl("^\\d{4}-\\d{2}-\\d{2}$", value)) {
                        d <- as.Date(value)
                    } else {
                        d <- as.Date(value, format = "%Y%m%d")
                    }
                    if (is.na(d)) {
                        return("Unknown")
                    }
                    format(d, "%Y-%m-%d")
                }

                paste0(format_val(start_val, FALSE), " to ", format_val(end_val, TRUE))
            }

            res_label <- if (is.null(resolution) || is.na(resolution) || resolution == "") {
                "Data"
            } else if (resolution == "10_minutes") {
                "10 Minutes"
            } else {
                paste0(toupper(substr(resolution, 1, 1)), substr(resolution, 2, nchar(resolution)))
            }

            station_range <- format_range(start_date, end_date)
            coverage_start <- if (is.null(overall_start)) start_date else overall_start
            coverage_end <- if (is.null(overall_end)) end_date else overall_end
            coverage_range <- format_range(coverage_start, coverage_end)
            summary_text <- if (is.null(detailed_summary) || is.na(detailed_summary) || detailed_summary == "") {
                "No Data"
            } else {
                detailed_summary
            }

            paste0(
                "<div style='font-size:18px; max-width: 400px;'>",
                "<b>", htmltools::htmlEscape(name), "</b> (", id, ")<br>",
                "<span style='font-size:80%;'>State: ", htmltools::htmlEscape(state), "</span><br>",
                "<span style='font-size:80%; color:#555;'>",
                "Station Active: ", station_range,
                "</span><br>",
                "<span style='font-size:80%; color:#555;'>",
                res_label, " Coverage: ", coverage_range,
                "</span><br>",
                "<div style='font-size:80%; color:#333; font-weight:bold; margin-top:5px;'>",
                "Data Availability (", res_label, "):</div>",
                "<div style='font-size:75%; color:#333; line-height: 1.1;'>• ",
                paste0(
                    gsub(", ", "<br>• ", htmltools::htmlEscape(summary_text)),
                    "<br><small><i>Note: 'Present' indicates ongoing monitoring;<br>",
                    "some parameters may have<br>",
                    "upload delays.</i></small>"
                ),
                "</div>",
                "</div>"
            )
        },
        error = function(e) {
            paste0("<b>", htmltools::htmlEscape(name), "</b> (", id, ")")
        }
    )
}

server <- function(input, output, session) {
    # --- Reactive Values ---
    current_station_id <- reactiveVal(NULL)
    station_data <- reactiveVal(NULL) # The loaded data
    loading_status <- reactiveVal(FALSE)
    loading_diagnostics <- reactiveVal("")
    previous_station_choices <- reactiveVal(NULL) # Track previous choices to avoid blink
    previous_date_range <- reactiveVal(NULL) # Track previous date range for bi-directional sync
    url_initialized <- reactiveVal(FALSE)

    # MapLibre Reactive Values
    style_change_trigger <- reactiveVal(0) # Trigger station re-render after style change
    map_initialized <- reactiveVal(FALSE) # Track if map bounds have been set
    stations_before_id <- reactiveVal(NULL) # Layer ID for station ordering
    current_raster_layers <- reactiveVal(character(0)) # Track dynamically added raster layers

    # Map bounds for Germany
    map_bounds <- list(
        lng_min = 5.5,
        lat_min = 47.0,
        lng_max = 15.5,
        lat_max = 55.5
    )

    # --- URL Parameter Parsing ---
    parse_url_params <- function(query) {
        params <- list()
        if (length(query) == 0) {
            return(params)
        }

        # Helper to decode
        decode <- function(x) URLdecode(x)

        # Parse query string
        pairs <- strsplit(query, "&")[[1]]
        for (pair in pairs) {
            parts <- strsplit(pair, "=")[[1]]
            if (length(parts) == 2) {
                key <- parts[1]
                val <- decode(parts[2])
                params[[key]] <- val
            }
        }
        params
    }

    # Observer: Apply URL params on app startup
    observe({
        req(!url_initialized())
        query <- session$clientData$url_search
        # Wait for query to be available (it might be empty string initially)
        # But we also want to handle no-params case to set initialized=TRUE

        # Parse query (even if empty to confirm no params)
        # Note: session$clientData$url_search usually starts with "?"
        q_str <- sub("^\\?", "", query)
        params <- parse_url_params(q_str)

        # If params exist, apply them
        if (length(params) > 0) {
            # 1. Resolution
            if (!is.null(params$resolution)) {
                updateRadioButtons(session, "data_resolution", selected = params$resolution)
            }

            # 2. Date Range
            if (!is.null(params$start) && !is.null(params$end)) {
                updateDateRangeInput(session, "date_range", start = params$start, end = params$end)
            }

            # 3. State/Land Selection
            if (!is.null(params$landname)) {
                updateSelectizeInput(session, "state_selector", selected = params$landname)
            }

            # 4. Station Selection
            if (!is.null(params$station)) {
                station_ref <- params$station
                # We need to wait for stations to load?
                # The all_stations() reactive depends on resolution.
                # Just updating the input might trigger the search.
                # Note: station_selector choices are updated dynamically.
                # We might need to handle this carefully if stations aren't loaded yet.
                # However, updateSelectizeInput usually works nicely.

                # Check if we need to set selected
                shinyjs::delay(500, {
                    # Try to find ID if name was passed
                    # For now just set the value, assuming ID or Name match
                    # DWD station selector choices are "Name (ID)" = ID.

                    # If passed param is a name, we might need to lookup.
                    # But if we rely on broadcast sending Name, URL has Name.
                    # DWD selector needs ID.

                    # Search in current all_stations()
                    current_st <- isolate(all_stations())
                    if (!is.null(current_st)) {
                        # Try match ID
                        match <- current_st %>% filter(id == station_ref)
                        if (nrow(match) == 0) {
                            # Try match Name
                            match <- current_st %>% filter(name == station_ref)
                        }

                        if (nrow(match) > 0) {
                            target_id <- match$id[1]
                            updateSelectizeInput(session, "station_selector", selected = target_id)

                            # Also trigger details panel if view implies it
                            # The observer for station_selector will run and trigger map logic
                        }
                    }
                })
            }

            # 4. View/Tab
            if (!is.null(params$view)) {
                view <- params$view
                shinyjs::delay(800, {
                    if (view == "map") {
                        nav_select("main_nav", "Map View") # using bslib way or updateNavbarPage
                        # DWD ui uses page_navbar id="main_nav".
                        # updateNavbarPage(session, "main_nav", selected = "Map View") should work?
                        # Or custom message if updateNavbarPage isn't standard in bslib setups?
                        # UI line 3: id="main_nav".
                    } else if (view == "station-info") {
                        updateNavbarPage(session, "main_nav", selected = "Stations Info")
                    } else if (grepl("dashboard", view)) {
                        updateNavbarPage(session, "main_nav", selected = "Dashboard")

                        if (view == "dashboard-data") {
                            shinyjs::delay(200, {
                                nav_select("dashboard_subtabs", "Data")
                                # navset_card_pill id="dashboard_subtabs"
                            })
                        }
                    }
                })
            }
        }

        url_initialized(TRUE)
    })

    # --- Reactive Data Sources (Resolution Dependent) ---
    observeEvent(input$data_resolution,
        {
            session$sendCustomMessage("freezeUI", list(text = "Switching resolution..."))

            # Update date range based on resolution
            res_val <- input$data_resolution
            # Normalize resolution string for internal use
            res <- if (res_val == "10 Minutes") "10_minutes" else tolower(res_val)

            tryCatch(
                {
                    if (res == "10_minutes") {
                        # For 10-Min: Use selected End Date and look back 1 month (30 days)
                        # This preserves context when zooming in from Daily/Hourly
                        current_end <- if (!is.null(input$date_range)) input$date_range[2] else Sys.Date()
                        new_end <- current_end
                        new_start <- new_end - 30
                    } else if (res %in% c("monthly", "daily")) {
                        # For Monthly/Daily: default to last 6 years
                        new_end <- Sys.Date()
                        new_start <- new_end - (365 * 6)
                    } else {
                        # For Hourly: default to last 1 year (366 days)
                        new_end <- Sys.Date()
                        new_start <- new_end - 366
                    }
                    updateDateRangeInput(session, "date_range", start = new_start, end = new_end)
                },
                error = function(e) {
                    message("Note: Could not update date range: ", e$message)
                }
            )
        },
        priority = 1000,
        ignoreInit = TRUE
    )

    current_index <- reactive({
        req(input$data_resolution)
        # Identify resolution (default hourly)
        res_val <- input$data_resolution
        res <- if (res_val == "10 Minutes") "10_minutes" else tolower(res_val)
        # Load File Index (for data fetching only)
        load_dwd_index(res)
    })

    all_stations <- reactive({
        req(input$data_resolution)
        res_val <- input$data_resolution
        res <- if (res_val == "10 Minutes") "10_minutes" else tolower(res_val)
        # Load enriched stations from pre-generated cache
        load_enriched_stations(res)
    })

    # Observe and Update State Selector
    observe({
        st <- all_stations()
        req(st)
        # Get unique states
        states <- sort(unique(st$state))

        # Update choice - preserve selection if possible (handled by Shiny usually)
        # We use server=TRUE for performance if list is long (states is short but good practice)
        updateSelectizeInput(session, "state_selector", choices = states, server = TRUE)
    })

    # --- Freeze UI During Data Loading ---
    # Disable all interactive controls (except map hovering) while parsing data
    observe({
        is_loading <- loading_status()

        # List of input IDs to disable/enable
        inputs_to_toggle <- c(
            "state_selector",
            "station_selector",
            "date_range",
            "zoom_home",
            "main_nav",
            "download_hourly"
        )

        if (is_loading) {
            for (inp in inputs_to_toggle) {
                shinyjs::disable(inp)
            }
        } else {
            for (inp in inputs_to_toggle) {
                shinyjs::enable(inp)
            }
        }
    })

    # Filtered stations based on Sidebar inputs
    filtered_stations <- reactive({
        req(all_stations(), input$date_range)
        df <- all_stations()

        # Filter by Date (Strict File Availability)
        # We check if the station has specific data files overlapping the range
        range_start <- as.numeric(format(input$date_range[1], "%Y%m%d"))
        range_end <- as.numeric(format(input$date_range[2], "%Y%m%d"))

        # Filter by State
        if (!is.null(input$state_selector) && length(input$state_selector) > 0) {
            df <- df %>% filter(state %in% input$state_selector)
        }

        df <- df %>% filter(
            as.numeric(station_overall_start) <= range_end &
                as.numeric(station_overall_end) >= range_start
        )

        # Defensive: atomic data.frame to prevent tibble/rowwise issues
        as.data.frame(df)
    })

    # Initialize / Update Station Selector Choices
    observe({
        df <- filtered_stations()
        req(df)

        # Create choices: "Station Name (ID)" = "ID"
        # Defensive: Ensure atomic vectors
        ids <- as.character(df$id)
        names <- paste0(as.character(df$name), " (", ids, ")")

        if (length(ids) > 0) {
            new_choices <- setNames(ids, names)
        } else {
            new_choices <- character(0)
        }

        # Only update if choices have actually changed (compare IDs)
        prev_choices <- previous_station_choices()
        new_ids <- sort(unname(new_choices))
        prev_ids <- if (!is.null(prev_choices)) sort(unname(prev_choices)) else NULL

        if (is.null(prev_ids) || !identical(new_ids, prev_ids)) {
            # Preserve selection if still in filtered list
            current_sel <- input$station_selector

            updateSelectizeInput(session, "station_selector",
                choices = new_choices,
                selected = current_sel,
                server = TRUE
            )
            previous_station_choices(new_choices)
        }
    })


    # Helper: Broadcast current state to parent page
    broadcast_state <- function(view_override = NULL) {
        # Get active station
        sid <- current_station_id()
        st_meta <- NULL
        if (!is.null(sid)) {
            all <- isolate(all_stations()) # Use isolate to avoid dependency loop if called inside observer?
            # Actually active observers call this, so it's fine.
            if (!is.null(all)) {
                st_meta <- all %>%
                    filter(id == sid) %>%
                    head(1)
            }
        }

        station_id <- if (!is.null(st_meta) && nrow(st_meta) > 0) as.character(st_meta$id) else NULL
        station_name <- if (!is.null(st_meta) && nrow(st_meta) > 0) as.character(st_meta$name) else NULL
        landname <- if (!is.null(st_meta) && nrow(st_meta) > 0) as.character(st_meta$state) else NULL

        # Ensure we have a valid resolution string (Capitalized from UI?)
        resolution <- input$data_resolution

        # Determine current view
        main_tab <- input$main_nav
        view <- if (!is.null(view_override)) {
            view_override
        } else if (!is.null(main_tab)) {
            if (main_tab == "Map View") {
                "map"
            } else if (main_tab == "Stations Info") {
                "station-info"
            } else if (main_tab == "Dashboard") {
                subtab <- input$dashboard_subtabs
                # If subtab is NULL/loading, default to plots
                if (!is.null(subtab) && subtab == "Data") {
                    "dashboard-data"
                } else {
                    "dashboard-plots"
                }
            } else {
                "map"
            }
        } else {
            "map"
        }

        start_date <- if (!is.null(input$date_range)) as.character(input$date_range[1]) else NULL
        end_date <- if (!is.null(input$date_range)) as.character(input$date_range[2]) else NULL

        session$sendCustomMessage("updateParentURL", list(
            station = station_id,
            stationName = station_name,
            landname = landname,
            resolution = resolution,
            view = view,
            start = start_date,
            end = end_date
        ))
        # Debug notification
        # showNotification(paste("Broadcasting:", station_name), duration = 3, type = "message")
    }

    # --- Selection Logic via Dropdown ---
    observeEvent(input$station_selector, {
        req(input$station_selector)
        id_val <- input$station_selector

        # Find station details
        s_meta <- all_stations() %>% filter(id == id_val)
        req(nrow(s_meta) > 0)

        lat_val <- s_meta$latitude[1]
        lng_val <- s_meta$longitude[1]

        # Set ID
        # Optimization: If ID is same as current, do not re-trigger date resets or map moves
        # This prevents circular loops where Date Filter -> Filtered List -> Update Selector -> Trigger Observer -> Reset Date
        prev_id <- current_station_id()
        if (!is.null(prev_id) && prev_id == id_val) {
            return()
        }

        current_station_id(id_val)

        # Highlight & Zoom
        # Generate Label
        res_val <- input$data_resolution
        res <- if (is.null(res_val)) NULL else if (res_val == "10 Minutes") "10_minutes" else tolower(res_val)
        overall_start <- if ("station_overall_start" %in% names(s_meta)) s_meta$station_overall_start[1] else s_meta$start_date[1]
        overall_end <- if ("station_overall_end" %in% names(s_meta)) s_meta$station_overall_end[1] else s_meta$end_date[1]
        lbl <- generate_station_label(
            s_meta$name[1], s_meta$id[1], s_meta$state[1],
            s_meta$start_date[1], s_meta$end_date[1], s_meta$detailed_summary[1],
            overall_start, overall_end, res
        )

        highlight_selected_station(maplibre_proxy("map"), lng_val, lat_val, lbl)

        # Trigger Panel
        output$show_details_panel <- reactive(TRUE)
        outputOptions(output, "show_details_panel", suspendWhenHidden = FALSE)

        # NOTE: Date reset removed to preserve user context
    })

    # Station Count
    output$station_count_filtered <- renderText({
        n <- nrow(filtered_stations())
        paste(n, "stations found")
    })

    # --- Map ---
    output$map <- renderMaplibre({
        maplibre(
            style = ofm_positron_style,
            center = c(10.45, 51.16),
            zoom = 6
        ) %>%
            add_navigation_control(show_compass = FALSE, visualize_pitch = FALSE, position = "top-left")
    })

    # Initialize map bounds only once
    observe({
        req(!map_initialized())
        req(input$map_zoom)

        maplibre_proxy("map") %>%
            fit_bounds(
                c(map_bounds$lng_min, map_bounds$lat_min, map_bounds$lng_max, map_bounds$lat_max)
            )

        map_initialized(TRUE)
    })

    # Zoom to extent of all filtered stations
    observeEvent(input$zoom_home, {
        df <- filtered_stations()
        req(df)
        if (nrow(df) > 0) {
            # Calculate bounds
            lons <- range(df$longitude, na.rm = TRUE)
            lats <- range(df$latitude, na.rm = TRUE)

            # If only one station, zoom to it with a small buffer
            if (nrow(df) == 1) {
                maplibre_proxy("map") %>%
                    fly_to(center = c(df$longitude[1], df$latitude[1]), zoom = 12)
            } else {
                maplibre_proxy("map") %>%
                    fit_bounds(c(lons[1], lats[1], lons[2], lats[2]), animate = TRUE)
            }
        }
    })

    # Zoom to extent when state selection changes
    observeEvent(input$state_selector,
        {
            df <- filtered_stations()
            req(df)
            if (nrow(df) > 0) {
                # Calculate bounds
                lons <- range(df$longitude, na.rm = TRUE)
                lats <- range(df$latitude, na.rm = TRUE)

                if (nrow(df) == 1) {
                    maplibre_proxy("map") %>%
                        fly_to(center = c(df$longitude[1], df$latitude[1]), zoom = 12)
                } else {
                    maplibre_proxy("map") %>%
                        fit_bounds(c(lons[1], lats[1], lons[2], lats[2]), animate = TRUE)
                }
            }
        },
        ignoreInit = TRUE
    )

    # Fix Map Rendering on Tab Switch
    observeEvent(input$main_nav, {
        if (input$main_nav == "Map View") {
            # Slight delay to ensure tab is visible - MapLibre handles resize automatically
            shinyjs::runjs("
                setTimeout(function() {
                    var map = document.getElementById('map');
                    if (map && map.__mapgl) {
                        map.__mapgl.resize();
                    }
                }, 200);
            ")
        }
    })

    # --- Basemap Switching Logic ---
    label_layer_ids <- c(
        # OpenFreeMap Positron & Bright common labels
        "waterway_line_label", "water_name_point_label", "water_name_line_label",
        "highway-name-path", "highway-name-minor", "highway-name-major",
        "highway-shield-non-us", "highway-shield-us-interstate", "road_shield_us",
        "airport", "label_other", "label_village", "label_town", "label_state",
        "label_city", "label_city_capital", "label_country_3", "label_country_2", "label_country_1",
        # Bright specific labels (POIs & Directions)
        "road_oneway", "road_oneway_opposite", "poi_r20", "poi_r7", "poi_r1", "poi_transit",
        # Dash variants (sometimes used in different versions)
        "waterway-line-label", "water-name-point-label", "water-name-line-label",
        "highway-shield-non-us", "highway-shield-us-interstate", "road-shield-us",
        "label-other", "label-village", "label-town", "label-state",
        "label-city", "label-city-capital", "label-country-3", "label-country-2", "label-country-1",
        # Legacy/Carto/OSM label names (for compatibility)
        "place_villages", "place_town", "place_country_2", "place_country_1",
        "place_state", "place_continent", "place_city_r6", "place_city_r5",
        "place_city_dot_r7", "place_city_dot_r4", "place_city_dot_r2", "place_city_dot_z7",
        "place_capital_dot_z7", "place_capital", "roadname_minor", "roadname_sec",
        "roadname_pri", "roadname_major", "motorway_name", "watername_ocean",
        "watername_sea", "watername_lake", "watername_lake_line", "poi_stadium",
        "poi_park", "poi_zoo", "airport_label", "country-label", "state-label",
        "settlement-major-label", "settlement-minor-label", "settlement-subdivision-label",
        "road-label", "waterway-label", "natural-point-label", "poi-label", "airport-label"
    )

    # Layers to hide in Satellite (Hybrid) mode to let the raster shows through
    # This includes all non-symbol layers (background, water, roads, boundaries, etc.)
    non_label_layer_ids <- c(
        "background", "park", "water", "landcover_ice_shelf", "landcover_glacier",
        "landuse_residential", "landcover_wood", "waterway", "building",
        "tunnel_motorway_casing", "tunnel_motorway_inner", "aeroway-taxiway",
        "aeroway-runway-casing", "aeroway-area", "aeroway-runway",
        "road_area_pier", "road_pier", "highway_path", "highway_minor",
        "highway_major_casing", "highway_major_inner", "highway_major_subtle",
        "highway_motorway_casing", "highway_motorway_inner", "highway_motorway_subtle",
        "railway_transit", "railway_transit_dashline", "railway_service",
        "railway_service_dashline", "railway", "railway_dashline",
        "highway_motorway_bridge_casing", "highway_motorway_bridge_inner",
        "boundary_3", "boundary_2", "boundary_disputed"
    )

    apply_label_visibility <- function(proxy, show_labels) {
        visibility <- if (isTRUE(show_labels)) "visible" else "none"
        for (layer_id in label_layer_ids) {
            tryCatch(
                {
                    proxy %>% set_layout_property(layer_id, "visibility", visibility)
                },
                error = function(e) {
                    # Layer may not exist in current style, ignore silently
                }
            )
        }
    }

    observeEvent(input$basemap, {
        proxy <- maplibre_proxy("map")
        basemap <- input$basemap

        # Explicitly remove any previously added raster layers
        old_layers <- isolate(current_raster_layers())
        if (length(old_layers) > 0) {
            for (layer_id in old_layers) {
                proxy %>% clear_layer(layer_id)
            }
            current_raster_layers(character(0))
        }

        if (basemap %in% c("ofm_positron", "ofm_bright")) {
            # VECTOR LOGIC (OpenFreeMap styles)
            style_url <- switch(basemap,
                "ofm_positron" = ofm_positron_style,
                "ofm_bright" = ofm_bright_style
            )

            proxy %>%
                set_style(style_url, preserve_layers = FALSE)

            # For OpenFreeMap sandwich, we want stations below labels
            stations_before_id("waterway_line_label")

            current_session <- shiny::getDefaultReactiveDomain()
            selected_basemap <- basemap

            later::later(function() {
                shiny::withReactiveDomain(current_session, {
                    # Race condition check
                    current_basemap <- isolate(input$basemap)
                    if (current_basemap != selected_basemap) {
                        return()
                    }

                    # Trigger re-render of stations and apply label visibility
                    apply_label_visibility(maplibre_proxy("map"), isolate(input$show_labels))
                    style_change_trigger(isolate(style_change_trigger()) + 1)
                })
            }, delay = 0.5)
        } else if (basemap == "sentinel") {
            # HYBRID LOGIC (EOX Sentinel-2 Satellite + OpenFreeMap Labels/Roads)
            # Use Positron as the base for labels/roads
            proxy %>%
                set_style(ofm_positron_style, preserve_layers = FALSE)

            current_session <- shiny::getDefaultReactiveDomain()
            selected_basemap <- basemap

            later::later(function() {
                shiny::withReactiveDomain(current_session, {
                    current_basemap <- isolate(input$basemap)
                    if (current_basemap != selected_basemap) {
                        return()
                    }

                    unique_suffix <- as.numeric(Sys.time()) * 1000
                    source_id <- paste0("sentinel_source_", unique_suffix)
                    layer_id <- paste0("sentinel_layer_", unique_suffix)

                    # Add Sentinel layer at the very bottom (before 'background')
                    maplibre_proxy("map") %>%
                        add_raster_source(id = source_id, tiles = sentinel_url, tileSize = 256, attribution = sentinel_attribution) %>%
                        add_layer(
                            id = layer_id,
                            type = "raster",
                            source = source_id,
                            paint = list("raster-opacity" = 1),
                            before_id = "background"
                        )

                    # Hide all non-label vector layers so only satellite + symbols show
                    for (layer_id_kill in non_label_layer_ids) {
                        tryCatch(
                            {
                                maplibre_proxy("map") %>% set_layout_property(layer_id_kill, "visibility", "none")
                            },
                            error = function(e) {
                                # Layer might not exist, ignore
                            }
                        )
                    }

                    # Ensure labels according to user preference
                    apply_label_visibility(maplibre_proxy("map"), isolate(input$show_labels))

                    # Stations should be above the satellite layer
                    stations_before_id("waterway_line_label")
                    current_raster_layers(c(layer_id))
                    style_change_trigger(isolate(style_change_trigger()) + 1)
                })
            }, delay = 0.5)
        }
    })

    # Toggle Labels visibility
    observeEvent(input$show_labels,
        {
            apply_label_visibility(maplibre_proxy("map"), input$show_labels)
        },
        ignoreInit = TRUE
    )

    # Update Markers
    # Optimize: Only update when data actually changes, not on tab switch
    # Also depends on style_change_trigger to re-add layer after style change
    observe({
        df <- filtered_stations()
        req(df, input$data_resolution)

        # Wait for map to be ready before adding markers
        req(map_initialized())

        # Add dependency on style_change_trigger to re-add layer after style change
        style_change_trigger()

        # We do NOT check input$main_nav here. We want markers to persist/update
        # regardless of view, so they are ready when user switches back.
        # This prevents the "reload" (clear + re-add) effect on tab switch.

        res_val <- input$data_resolution
        res <- if (res_val == "10 Minutes") "10_minutes" else tolower(res_val)
        n_rows <- nrow(df)
        coverage_start <- if ("station_overall_start" %in% names(df)) df$station_overall_start else df$start_date
        coverage_end <- if ("station_overall_end" %in% names(df)) df$station_overall_end else df$end_date

        # Pre-process label with HTML using Helper
        df$popup_content <- purrr::pmap_chr(
            list(
                df$name, df$id, df$state, df$start_date, df$end_date,
                df$detailed_summary, coverage_start, coverage_end, rep(res, n_rows)
            ),
            generate_station_label
        )

        if (nrow(df) > 0) {
            # Standard styling for all stations (selection is handled by separate highlight layer)
            df <- df %>%
                mutate(
                    circle_color = "navy",
                    circle_radius = 6,
                    circle_stroke_color = "#00000000",
                    circle_stroke_width = 0
                )

            # Convert to sf
            map_data <- st_as_sf(df, coords = c("longitude", "latitude"), crs = 4326)

            # Apply to map
            maplibre_proxy("map") %>%
                clear_layer("stations") %>%
                add_circle_layer(
                    id = "stations",
                    source = map_data,
                    circle_color = get_column("circle_color"),
                    circle_radius = get_column("circle_radius"),
                    circle_stroke_color = get_column("circle_stroke_color"),
                    circle_stroke_width = get_column("circle_stroke_width"),
                    circle_opacity = 0.7,
                    tooltip = get_column("popup_content"),
                    before_id = stations_before_id()
                )

            # Re-highlight selected station if present (to update label with new resolution)
            sid <- isolate(current_station_id())
            if (!is.null(sid)) {
                sel_row <- df %>% filter(id == sid)
                if (nrow(sel_row) > 0) {
                    highlight_selected_station(maplibre_proxy("map"), sel_row$longitude[1], sel_row$latitude[1], sel_row$popup_content[1], move_map = FALSE)
                } else {
                    # Station no longer in filtered set, clear highlight
                    maplibre_proxy("map") %>% clear_layer("selected-highlight")
                }
            }
        }

        # Unfreeze after markers are updated
        session$sendCustomMessage("unfreezeUI", list())
    })

    # --- Selection Logic ---
    observeEvent(input$map_feature_click, {
        clicked_data <- input$map_feature_click

        # Check if the click was on the "stations" layer
        if (!is.null(clicked_data) && (isTRUE(clicked_data$layer_id == "stations") || isTRUE(clicked_data$layer == "stations"))) {
            # The ID is in the properties
            id_val <- clicked_data$properties$id

            if (!is.null(id_val)) {
                # Set ID
                current_station_id(id_val)

                # Highlight & Zoom
                # Fetch meta for label
                s_meta <- all_stations() %>% filter(id == id_val)

                if (nrow(s_meta) > 0) {
                    lng <- s_meta$longitude[1]
                    lat <- s_meta$latitude[1]

                    res <- if (is.null(input$data_resolution)) NULL else tolower(input$data_resolution)
                    overall_start <- if ("station_overall_start" %in% names(s_meta)) s_meta$station_overall_start[1] else s_meta$start_date[1]
                    overall_end <- if ("station_overall_end" %in% names(s_meta)) s_meta$station_overall_end[1] else s_meta$end_date[1]
                    lbl <- generate_station_label(
                        s_meta$name[1], s_meta$id[1], s_meta$state[1],
                        s_meta$start_date[1], s_meta$end_date[1], s_meta$detailed_summary[1],
                        overall_start, overall_end, res
                    )

                    highlight_selected_station(maplibre_proxy("map"), lng, lat, lbl)
                }

                # Sync dropdown
                updateSelectizeInput(session, "station_selector", selected = id_val)
            }
        }
    })

    # --- Date Synchronization (Sliding Window & Bi-Directional) ---

    # 2. Sidebar constraints: 28 <= days <= max (366 for hourly/daily, 3652 for monthly)
    observeEvent(input$date_range, {
        req(input$date_range)
        d_start <- input$date_range[1]
        d_end <- input$date_range[2]

        # Validate inputs are not NA
        if (any(is.na(c(d_start, d_end)))) {
            return()
        }
        req(input$data_resolution)

        # Get previous values for change detection
        prev <- previous_date_range()

        # If this is the first observation, just store the current values and return
        if (is.null(prev)) {
            previous_date_range(c(d_start, d_end))
            return()
        }

        prev_start <- prev[1]
        prev_end <- prev[2]

        # Calculate current diff
        diff <- as.numeric(difftime(d_end, d_start, units = "days"))

        # Defensive check for NA diff
        if (is.na(diff)) {
            return()
        }

        # Max window depends on resolution:
        # Monthly/Daily: ~10 years (3652 days) to allow 6-year views
        # Hourly: 1 year (366 days)
        res <- tolower(input$data_resolution)
        is_extended <- res %in% c("monthly", "daily")

        max_days <- if (res == "10 minutes") {
            31 # Limit 10-min data to ~1 month
        } else if (is_extended) {
            3652
        } else {
            366
        }
        min_days <- 28

        if (diff > max_days || diff < min_days) {
            # Detect which date changed
            start_changed <- !isTRUE(all.equal(d_start, prev_start))
            end_changed <- !isTRUE(all.equal(d_end, prev_end))

            if (end_changed && !start_changed) {
                # User changed end date -> adjust start date
                target_diff <- if (diff > max_days) max_days else min_days
                new_start <- d_end - target_diff

                freezeReactiveValue(input, "date_range")
                updateDateRangeInput(session, "date_range", start = new_start, end = d_end)

                # Update tracked state
                previous_date_range(c(new_start, d_end))
            } else {
                # User changed start date (or both) -> adjust end date
                target_diff <- if (diff > max_days) max_days else min_days
                new_end <- d_start + target_diff

                freezeReactiveValue(input, "date_range")
                updateDateRangeInput(session, "date_range", start = d_start, end = new_end)

                # Update tracked state
                previous_date_range(c(d_start, new_end))
            }
        } else {
            # Update tracked state
            previous_date_range(c(d_start, d_end))
        }
    })

    # --- Async Fetching State Machine ---

    # State Variables
    fetch_stage <- reactiveVal(0) # 0=Idle, 1=Init, 2=NextFile, 3=Head, 4=Download, 5=Parse, 6=Merge
    fetch_message <- reactiveVal("Idle")
    fetch_queue <- reactiveVal(list()) # List of targets (url, etc.)
    fetch_queue_idx <- reactiveVal(0)
    parsed_data_list <- reactiveVal(list()) # Accumulate dataframes

    # Progress Tracking
    fetch_total_size <- reactiveVal(0)
    fetch_current_pos <- reactiveVal(0)
    fetch_current_token <- reactiveVal(NULL) # To invalidate stale sessions
    fetch_temp_file <- reactiveVal(NULL)

    # --- Immediate State Clearing to Prevent Main Thread Blocking ---
    # When inputs change, clear data IMMEDIATELY to prevent plots from
    # rendering old data with new settings (e.g. Daily data -> Hourly plots = 3000+ traces)
    # This covers the 500ms debounce gap.
    observeEvent(input$data_resolution,
        {
            station_data(NULL)
            parsed_data_list(list())

            # Only block UI if we have a station selected (fetch will occur)
            if (!is.null(current_station_id())) {
                loading_status(TRUE)
            }
        },
        priority = 1000,
        ignoreInit = TRUE
    )

    observeEvent(current_station_id(),
        {
            station_data(NULL)
            parsed_data_list(list())

            # Only block UI if a valid station is selected
            if (!is.null(current_station_id())) {
                loading_status(TRUE)
            }
        },
        priority = 1000,
        ignoreInit = TRUE
    )

    # Initial Trigger (Debounced Window)
    window_reactive <- reactive({
        req(input$date_range)
        list(id = current_station_id(), start = input$date_range[1], end = input$date_range[2])
    })
    window_debounced <- window_reactive %>% debounce(500)

    reset_fetch <- function(msg = NULL) {
        fetch_stage(0)
        # Invalidate token to kill pending async tasks
        fetch_current_token(as.numeric(Sys.time()))

        loading_status(FALSE)
        if (!is.null(msg)) loading_diagnostics(msg)
        tmp <- fetch_temp_file()
        if (!is.null(tmp) && file.exists(tmp)) unlink(tmp)
        fetch_temp_file(NULL)
        parsed_data_list(list())
        # Unfreeze UI when fetch is reset/cancelled
        session$sendCustomMessage("unfreezeUI", list())
    }

    # Handle Cancel from Freeze Window
    observeEvent(input$cancel_loading, {
        reset_fetch("Cancelled by user")
        showNotification("Loading cancelled by user.", type = "warning")
    })

    # Stage 1: Initialization
    observe({
        req(window_debounced()$id, window_debounced()$start, window_debounced()$end)

        # Prevent re-triggering if already running for same params (optional logic could go here)
        # For now, any change restarts fetch.

        station_id <- window_debounced()$id
        loading_status(TRUE)

        # Get station name for display
        station_meta <- all_stations() %>% filter(id == station_id)
        station_name <- if (nrow(station_meta) > 0) station_meta$name[1] else station_id

        msg <- "Initializing data fetch..."
        loading_diagnostics(msg)
        fetch_message(msg)
        # Freeze UI during download and parsing - include station name
        session$sendCustomMessage("freezeUI", list(text = msg, station = station_name))

        # Generate Filtered Index - Optimize based on Date Range
        req_start_date <- as.Date(window_debounced()$start)
        req_end_date <- as.Date(window_debounced()$end)

        # Helper to convert YYYYMMDD string to Date
        parse_dwd_date <- function(x) {
            as.Date(x, format = "%Y%m%d")
        }

        targets <- current_index() %>%
            filter(id == station_id) %>%
            filter(
                # Logic: Keep file if (FileStart <= ReqEnd) AND (FileEnd >= ReqStart)
                # Handle cases where start/end might be NA or "recent" type implies checking
                type == "recent" | # Always check recent files as they might have fresh data
                    type == "solar" | # Solar usually has one file or simple structure, keep safe
                    (
                        !is.na(start_date) & !is.na(end_date) &
                            parse_dwd_date(start_date) <= req_end_date &
                            parse_dwd_date(end_date) >= req_start_date
                    )
            )

        if (nrow(targets) == 0) {
            # Fallback/Debug: check if we filtered out everything
            raw_targets <- current_index() %>% filter(id == station_id)
            msg <- "No data found in index for this station."

            if (nrow(raw_targets) > 0) {
                # Data exists but was filtered out by date range
                msg <- "No data found for the selected date range."
            }

            reset_fetch(msg)
            return()
        }

        # Setup Queue
        # We convert targets to a list of rows for easier iteration
        q <- split(targets, seq(nrow(targets)))
        fetch_queue(q)
        fetch_queue_idx(1)
        parsed_data_list(list()) # Clear previous

        # New Token
        token <- as.numeric(Sys.time())
        fetch_current_token(token)

        fetch_stage(2) # Go to Next File
    })

    # Stage 2: Next File
    observe({
        req(fetch_stage() == 2)
        idx <- fetch_queue_idx()
        q <- fetch_queue()

        if (idx > length(q)) {
            # All files done -> Merge
            fetch_stage(6)
        } else {
            # Setup next file
            target <- q[[idx]]
            msg <- paste0("Downloading file ", idx, "/", length(q), "...")
            loading_diagnostics(msg)
            session$sendCustomMessage("freezeUI", list(text = msg))
            fetch_message(msg)

            # Initialize tracking for new file
            fetch_total_size(0) # Unknown initially
            fetch_current_pos(0)

            # Prep Temp File
            tmp <- tempfile(fileext = ".zip")
            fetch_temp_file(tmp)
            file.create(tmp)

            fetch_stage(4) # Go directly to Download Loop
        }
    })

    # Stage 3: REMOVED (Head Check caused blocking)

    # Stage 4: Download Chunk
    observe({
        req(fetch_stage() == 4)

        # Settings - Increased to 50MB to reduce loop overhead
        chunk_size <- 50 * 1024 * 1024
        total <- fetch_total_size()
        current <- fetch_current_pos()
        tmp <- fetch_temp_file()
        idx <- fetch_queue_idx()
        q <- fetch_queue()
        target <- q[[idx]]

        # If total unknown, we assume we want next chunk
        if (total > 0) {
            end <- min(current + chunk_size - 1, total - 1)
        } else {
            end <- current + chunk_size - 1
        }

        token <- fetch_current_token()
        message(paste0("[Debug] Stage 4: Starting chunk download. Range: ", current, "-", end, " Total: ", total))

        later::later(function() {
            if (!identical(isolate(fetch_current_token()), token)) {
                message("[Debug] Stage 4: Token mismatch in later callback. Aborting.")
                return()
            }

            tryCatch(
                {
                    isolate({
                        h <- curl::new_handle()
                        curl::handle_setopt(
                            h,
                            range = paste0(current, "-", end),
                            # Robustness: Timeout if connection hangs or speed is too low
                            connecttimeout = 10,
                            low_speed_time = 30, # Abort if speed < 100 B/s for 30s
                            low_speed_limit = 100
                        )

                        message(paste0("[Debug] Stage 4: Executing curl for ", target$url))

                        tryCatch(
                            {
                                resp <- curl::curl_fetch_memory(target$url, handle = h)
                                message(paste0("[Debug] Stage 4: Curl finished. Status: ", resp$status_code))

                                if (resp$status_code == 200) {
                                    # 200 OK means server sent the whole file (or ignored range)
                                    con <- file(tmp, open = "wb")
                                    writeBin(resp$content, con)
                                    close(con)

                                    new_pos <- length(resp$content)
                                    fetch_current_pos(new_pos)
                                    fetch_total_size(new_pos)

                                    msg <- paste0("Downloaded file ", idx, "/", length(q), ": ", format_bytes(new_pos), " (Finished)")
                                    loading_diagnostics(msg)
                                    session$sendCustomMessage("freezeUI", list(text = msg))
                                    fetch_message(msg)

                                    fetch_stage(5) # Parse
                                } else if (resp$status_code == 206) {
                                    # 206 Partial Content
                                    con <- file(tmp, open = "ab")
                                    writeBin(resp$content, con)
                                    close(con)

                                    bytes_received <- length(resp$content)
                                    new_pos <- current + bytes_received
                                    fetch_current_pos(new_pos)

                                    # Try to guess total from header if unknown
                                    if (total == 0) {
                                        headers <- curl::parse_headers_list(resp$headers)
                                        if (!is.null(headers[["content-range"]])) {
                                            cr <- headers[["content-range"]]
                                            parts <- strsplit(cr, "/")[[1]]
                                            if (length(parts) == 2 && parts[2] != "*") {
                                                t_val <- as.numeric(parts[2])
                                                if (!is.na(t_val)) fetch_total_size(t_val)
                                            }
                                        }
                                    }

                                    total <- fetch_total_size()

                                    done <- FALSE
                                    percent_str <- ""

                                    if (total > 0) {
                                        if (new_pos >= total) done <- TRUE
                                        pct <- round(new_pos / total * 100)
                                        percent_str <- paste0(" / ", format_bytes(total), " (", pct, "%)")
                                    } else {
                                        if (bytes_received < chunk_size) done <- TRUE
                                        percent_str <- " (Unknown total)"
                                    }

                                    msg <- paste0(
                                        "Downloading file ", idx, "/", length(q), ": ",
                                        format_bytes(new_pos), percent_str
                                    )
                                    loading_diagnostics(msg)
                                    session$sendCustomMessage("freezeUI", list(text = msg))
                                    fetch_message(msg)

                                    if (done) {
                                        if (total == 0) fetch_total_size(new_pos)
                                        fetch_stage(5) # Parse
                                    } else {
                                        fetch_stage(4) # Next Chunk
                                    }
                                } else if (resp$status_code == 416) {
                                    # Range Not Satisfiable
                                    if (total == 0 && current > 0) {
                                        fetch_stage(5)
                                    } else {
                                        loading_diagnostics(paste("Download Error HTTP", resp$status_code))
                                        fetch_stage(0)
                                    }
                                } else {
                                    loading_diagnostics(paste("Download Error HTTP", resp$status_code))
                                    fetch_stage(0)
                                }
                            },
                            error = function(e) {
                                # Handle Timeout/Connection Errors
                                message(paste0("[Debug] Download Failed: ", e$message))
                                loading_diagnostics(paste("Download Failed:", e$message))
                                session$sendCustomMessage("unfreezeUI", list())
                                showNotification(
                                    paste("Download stopped: Connection timed out or stuck. Please try again later. (File ", idx, ")"),
                                    type = "error",
                                    duration = NULL # Persistent until clicked
                                )
                                fetch_stage(0)
                            }
                        )
                    })
                },
                error = function(e) {
                    message(paste0("[Debug] Critical Async Error: ", e$message))
                    msg <- paste("Critical Async Error:", e$message)

                    loading_diagnostics(msg)
                    loading_status(FALSE)
                    session$sendCustomMessage("unfreezeUI", list())
                    fetch_stage(0)
                }
            )
        }, 0.05)

        fetch_stage(-1)
    })

    # Stage 5: Parse
    observe({
        req(fetch_stage() == 5)
        token <- fetch_current_token()

        idx <- fetch_queue_idx()
        len <- length(fetch_queue())
        msg <- paste0("Parsing file ", idx, "/", len, "...")
        loading_diagnostics(msg)
        session$sendCustomMessage("freezeUI", list(text = msg))
        fetch_message(msg)

        later::later(function() {
            if (!identical(isolate(fetch_current_token()), token)) {
                return()
            }

            isolate({
                tmp <- fetch_temp_file()

                # We reuse read_dwd_data logic
                # We need start/end dates for efficient parsing if supported
                s_date <- window_debounced()$start
                e_date <- window_debounced()$end

                # Call parser (from global environment)
                parsed <- tryCatch(read_dwd_data(tmp, s_date, e_date), error = function(e) NULL)

                if (!is.null(parsed)) {
                    # Add to list
                    plist <- parsed_data_list()
                    plist[[length(plist) + 1]] <- parsed
                    parsed_data_list(plist)
                }

                unlink(tmp)
                fetch_temp_file(NULL)

                # Next file
                fetch_queue_idx(idx + 1)
                fetch_stage(2)
            })
        }, 0.1)

        fetch_stage(-1)
    })

    # Stage 6: Merge
    observe({
        req(fetch_stage() == 6)

        token <- fetch_current_token()
        msg <- "Merging and finalizing data..."
        loading_diagnostics(msg)
        session$sendCustomMessage("freezeUI", list(text = msg))
        fetch_message(msg)

        later::later(function() {
            if (!identical(isolate(fetch_current_token()), token)) {
                return()
            }

            isolate({
                plist <- parsed_data_list()

                if (length(plist) == 0) {
                    loading_diagnostics("No valid data found.")
                    loading_status(FALSE)
                    station_data(NULL)
                    fetch_stage(0)
                    return()
                }

                # Merge Logic (simplified from parse_dwd.R)
                final_df <- purrr::reduce(plist, full_join, by = "datetime")

                # Coalesce
                weather_vars <- c(
                    "temp", "temp_min", "temp_max", "temp_min_avg", "temp_max_avg", "rh", "dew_point",
                    "abs_humidity", "vapor_pressure", "wet_bulb_temp",
                    "precip", "wind_speed", "wind_dir", "pressure", "station_pressure", "cloud_cover", "cloud_cover_indicator",
                    "wind_gust_max", "solar_global", "sunshine_duration",
                    "soil_temp_2cm", "soil_temp_5cm", "soil_temp_10cm", "soil_temp_20cm", "soil_temp_50cm", "soil_temp_100cm",
                    "soil_temp_min_5cm",
                    "snow_depth", "snow_water_equiv", "snow_fresh_sum", "snow_depth_sum",
                    "thunderstorm", "glaze", "graupel", "hail", "fog", "frost", "storm_6", "storm_8", "dew",
                    "precip_net_thunderstorm", "precip_net_graupel", "precip_net_hail", "precip_net_fog",
                    "visibility", "visibility_indicator", "weather_code", "weather_text",
                    "cloud_layer1_code", "cloud_layer1_abbrev", "cloud_layer1_height", "cloud_layer1_amount",
                    "cloud_layer2_code", "cloud_layer2_abbrev", "cloud_layer2_height", "cloud_layer2_amount",
                    "cloud_layer3_code", "cloud_layer3_abbrev", "cloud_layer3_height", "cloud_layer3_amount",
                    "cloud_layer4_code", "cloud_layer4_abbrev", "cloud_layer4_height", "cloud_layer4_amount"
                )
                available_cols <- names(final_df)
                clean_df <- final_df %>% select(datetime)

                for (v in weather_vars) {
                    v_cols <- available_cols[grepl(paste0("^", v, "(\\.|$)"), available_cols)]
                    if (length(v_cols) > 0) {
                        clean_df[[v]] <- do.call(coalesce, final_df[v_cols])
                    }
                }

                # Cleanup
                clean_df <- clean_df %>%
                    distinct(datetime, .keep_all = TRUE) %>%
                    arrange(datetime)

                # Final Window Filter (safety) with Auto-Correction
                s_date <- window_debounced()$start
                e_date <- window_debounced()$end

                if (!is.null(s_date) && !is.null(e_date)) {
                    req_start <- tryCatch(as.POSIXct(s_date), error = function(e) NA)
                    req_end <- tryCatch(as.POSIXct(e_date) + days(1), error = function(e) NA)

                    if (!is.na(req_start) && !is.na(req_end)) {
                        filtered_df <- clean_df %>%
                            filter(datetime >= req_start, datetime <= req_end)

                        # Check for mismatch: Data exists but not in window
                        if (nrow(filtered_df) == 0 && nrow(clean_df) > 0) {
                            data_max <- max(clean_df$datetime, na.rm = TRUE)
                            data_min <- min(clean_df$datetime, na.rm = TRUE)

                            # If the requested window is completely outside the data range
                            if (is.finite(data_min) && is.finite(data_max)) {
                                if (req_start > data_max || req_end < data_min) {
                                    msg <- paste0("Warning: No data in selected range. Available data ends: ", format(data_max, "%Y-%m-%d"), ". Displaying available data.")
                                    loading_diagnostics(msg)

                                    # Do NOT auto-adjust dates (user preference)
                                    # We just let it filter (which yields 0 rows) or show what we have
                                }
                            }
                        }
                        clean_df <- filtered_df
                    }
                }

                # If after filtering we have 0 rows, but we had data before, it means window is empty
                if (nrow(clean_df) == 0 && nrow(final_df) > 0) {
                    msg <- "No data found for the selected date range."
                    loading_diagnostics(msg)
                }


                station_data(clean_df)
                loading_diagnostics(paste0("Success: ", nrow(clean_df), " rows loaded."))
                loading_status(FALSE)
                fetch_stage(0)

                # Navigate to Dashboard tab
                if (input$main_nav != "Dashboard") {
                    updateNavbarPage(session, "main_nav", selected = "Dashboard")
                }

                # Show rendering message and then unfreeze after delay
                session$sendCustomMessage("freezeUI", list(text = "Rendering plots..."))
                later::later(function() {
                    session$sendCustomMessage("unfreezeUI", list())
                }, 2.0) # Give 2 seconds for plots to render
            })
        }, 0.1)

        fetch_stage(-1)
    })

    # Output logic for Panel
    output$is_loading <- reactive({
        loading_status()
    })
    outputOptions(output, "is_loading", suspendWhenHidden = FALSE)

    output$station_ready <- reactive({
        !is.null(station_data())
    })
    outputOptions(output, "station_ready", suspendWhenHidden = FALSE)

    output$data_diagnostics <- renderUI({
        HTML(loading_diagnostics())
    })

    output$has_diag <- reactive({
        nzchar(loading_diagnostics())
    })
    outputOptions(output, "has_diag", suspendWhenHidden = FALSE)

    # --- Plot Outputs (Top-Level Reactivity) ---
    # These are defined outside of observers to ensure they invalidate immediately when station_data() changes.

    output$temp_plot <- renderPlotly({
        df <- station_data()
        req(df)
        create_temperature_plot(df, resolution = isolate(tolower(input$data_resolution)))
    })

    output$humidity_plot <- renderPlotly({
        df <- station_data()
        req(df)
        create_humidity_plot(df, resolution = isolate(tolower(input$data_resolution)))
    })

    output$wind_overview_plot <- renderPlotly({
        df <- station_data()
        req(df)
        create_wind_overview_plot(df, resolution = isolate(tolower(input$data_resolution)))
    })

    output$pressure_plot <- renderPlotly({
        df <- station_data()
        req(df)
        create_pressure_plot(df, resolution = isolate(tolower(input$data_resolution)))
    })

    output$cloud_plot <- renderPlotly({
        df <- station_data()
        req(df)
        create_cloud_cover_plot(df, resolution = isolate(tolower(input$data_resolution)))
    })

    output$precip_plot <- renderPlotly({
        df <- station_data()
        req(df)
        create_precipitation_plot(df, resolution = isolate(tolower(input$data_resolution)))
    })

    output$wind_rose <- renderPlotly({
        df <- station_data()
        req(df)
        create_wind_rose_plot(df)
    })

    output$solar_radiation_plot <- renderPlotly({
        df <- station_data()
        req(df)
        create_solar_radiation_plot(df, resolution = isolate(tolower(input$data_resolution)))
    })

    output$sunshine_plot <- renderPlotly({
        df <- station_data()
        req(df)
        create_sunshine_duration_plot(df, resolution = isolate(tolower(input$data_resolution)))
    })

    output$snow_plot <- renderPlotly({
        df <- station_data()
        req(df)
        create_snow_plot(df, resolution = isolate(tolower(input$data_resolution)))
    })

    output$soil_temp_plot <- renderPlotly({
        df <- station_data()
        req(df)
        create_soil_temp_plot(df, resolution = isolate(tolower(input$data_resolution)))
    })

    output$diurnal_plot <- renderPlotly({
        df <- station_data()
        req(df)
        create_diurnal_plot(df)
    })

    output$weathergami_plot <- renderPlotly({
        df <- station_data()
        req(df)
        create_weathergami_plot(df)
    })

    # Tables
    output$hourly_data_table <- DT::renderDataTable({
        req(!loading_status())
        df <- station_data()
        req(df)

        # Format datetime
        df <- df %>% mutate(datetime = format(datetime, "%Y-%m-%d %H:%M"))

        # Rename columns using global mapping
        # We use any_of() to safely select/rename only columns that exist in the data
        # Note: rename(new = old) syntax, so we need to invert our named vector or use any_of matching
        # rename(df, any_of(dwd_column_labels)) works if vector is NewName = OldName?
        # dwd_column_labels is "old_code" = "New Name"
        # dplyr::rename(df, any_of(dwd_column_labels)) expects c("New Name" = "old_code")?
        # actually rename accepts named vector c(New = Old).
        # Our vector is Old = New? No, it's c("temp" = "Air Temp").
        # If we use rename(!!dwd_column_labels), it expects c(New = Old).
        # So we likely need to invert the vector for rename, OR use label substitution.
        # Actually, let's just use data.table or setNames for simplicity on the display DF.

        # Invert the mapping: c("Air Temp" = "temp")
        renaming_vec <- setNames(names(dwd_column_labels), dwd_column_labels)

        # But wait, dwd_column_labels is defined as c("temp" = "Air Temp").
        # dplyr::rename(df, !!!dwd_column_labels) -> this would try to rename column "Air Temp" to "temp"?
        # No, rename(new = old).
        # If I hav c(a = b), rename(!!!x) means rename b to a.
        # So if dwd_column_labels is c("temp" = "Air Temp"), it means rename "Air Temp" to "temp".
        # That is the Opposite of what we want. We have "temp" in DF, want "Air Temp".
        # So we need c("Air Temp" = "temp").

        display_labels <- setNames(names(dwd_column_labels), dwd_column_labels)
        # Wait, names(dwd_column_labels) is "temp". dwd_column_labels is "Air Temp".
        # setNames("temp", "Air Temp") -> c("Air Temp" = "temp")
        # This is correct for rename(new = old).

        cols_to_rename <- dwd_column_labels[names(dwd_column_labels) %in% names(df)]
        # This gives c("temp" = "Air Temp") subset.
        # We need to construct the rename vector: "Air Temp" = "temp"
        rename_map <- setNames(names(cols_to_rename), cols_to_rename)

        # Actually, much simpler:
        req(df)

        # Use simple column replacement for display
        df_display <- df
        for (col in names(dwd_column_labels)) {
            if (col %in% names(df_display)) {
                names(df_display)[names(df_display) == col] <- dwd_column_labels[[col]]
            }
        }

        datatable(df_display,
            options = list(pageLength = 15, scrollX = TRUE)
        )
    })

    # Download Handler
    output$download_hourly <- downloadHandler(
        filename = function() {
            id <- current_station_id()
            res <- if (!is.null(input$data_resolution)) tolower(input$data_resolution) else "data"
            if (is.null(id)) {
                paste0("dwd_data_", res, ".xlsx")
            } else {
                paste0("dwd_station_", id, "_", res, ".xlsx")
            }
        },
        content = function(file) {
            req(station_data())
            # Ensure proper datetime formatting for Excel (writexl handles POSIXct, but text might be safer for strict format)
            # Actually writexl handles dates nicely natively.
            out_df <- station_data()
            write_xlsx(out_df, path = file)
        }
    )

    # Data Tab Header - Info Callout Card
    output$station_info_header <- renderUI({
        id <- current_station_id()
        if (is.null(id)) {
            return(NULL)
        }

        # Metadata
        meta <- all_stations() %>% dplyr::filter(id == !!id)
        if (nrow(meta) == 0) {
            return(NULL)
        }

        s_name <- meta$name[1]
        s_state <- meta$state[1]
        s_elev <- meta$elevation[1]

        # Dynamic Badge Logic
        res_label <- input$data_resolution
        res_class <- if (tolower(res_label) == "hourly") "bg-primary" else "bg-success"

        # Data Range
        df <- station_data()
        if (is.null(df) || nrow(df) == 0) {
            dates_text <- "No data loaded"
        } else {
            date_range <- range(as.Date(df$datetime), na.rm = TRUE)
            dates_text <- paste(date_range[1], "to", date_range[2])
        }

        # Unified Info Card
        card(
            style = "margin-bottom: 20px; border-left: 5px solid #007bff;",
            card_body(
                padding = 15,
                layout_columns(
                    fill = FALSE,
                    # Col 1: Station
                    div(
                        strong("Station"), br(),
                        span(s_name, style = "font-size: 1.1rem;"), br(),
                        tags$small(class = "text-muted", paste("ID:", id))
                    ),
                    # Col 2: Location
                    div(
                        strong("Location"), br(),
                        span(s_state), br(),
                        tags$small(class = "text-muted", paste0(meta$latitude[1], "°N, ", meta$longitude[1], "°E"))
                    ),
                    # Col 3: Elevation & Resolution
                    div(
                        strong("Technical"), br(),
                        span(paste0(s_elev, " m")), br(),
                        span(class = paste("badge", res_class), res_label)
                    ),
                    # Col 4: Period
                    div(
                        strong("Data Selection"), br(),
                        span(dates_text)
                    ),
                    # Col 5: Actions
                    div(
                        class = "d-flex align-items-center justify-content-end",
                        downloadButton(
                            "download_hourly",
                            label = "Export Excel",
                            class = "btn-sm btn-primary",
                            icon = icon("file-excel")
                        )
                    )
                )
            )
        )
    })

    output$table <- DT::renderDataTable({
        # Mirror MeteoFrance: Select specific columns and disable row names
        # This ensures rowData[0] in JS is the Station ID
        df_display <- filtered_stations() %>%
            select(
                "ID" = id,
                "Name" = name,
                "State" = state,
                "Elev." = elevation,
                "Status" = detailed_summary
            )

        datatable(df_display,
            selection = "none",
            rownames = FALSE,
            callback = JS("
                table.on('dblclick', 'tr', function() {
                    var rowData = table.row(this).data();
                    if (rowData !== undefined && rowData !== null) {
                        var stationId = rowData[0];
                        Shiny.setInputValue('table_station_dblclick', stationId, {priority: 'event'});
                    }
                });
            ")
        )
    })

    # Selection from Table - Double Click
    observeEvent(input$table_station_dblclick, {
        id_val <- input$table_station_dblclick
        req(id_val)

        # Get station metadata
        # Using filtered_stations ensures we respect current context, but ID lookup is robust
        s <- filtered_stations() %>% filter(id == id_val)

        # Fallback to all stations if filter changed momentarily (unlikely but safe)
        if (nrow(s) == 0) {
            s <- all_stations() %>% filter(id == id_val)
        }

        req(nrow(s) > 0)

        current_station_id(id_val)

        # Sync dropdown (sidebar)
        updateSelectizeInput(session, "station_selector", selected = id_val)

        # Highlight on map (data fetch triggered by current_station_id change will navigate to Dashboard)
        lat_val <- s$latitude[1]
        lng_val <- s$longitude[1]
        # Highlight & Zoom
        # Generate Label
        res <- if (is.null(input$data_resolution)) NULL else tolower(input$data_resolution)
        overall_start <- if ("station_overall_start" %in% names(s)) s$station_overall_start[1] else s$start_date[1]
        overall_end <- if ("station_overall_end" %in% names(s)) s$station_overall_end[1] else s$end_date[1]
        lbl <- generate_station_label(
            s$name[1], s$id[1], s$state[1],
            s$start_date[1], s$end_date[1], s$detailed_summary[1],
            overall_start, overall_end, res
        )

        highlight_selected_station(maplibre_proxy("map"), lng_val, lat_val, lbl)

        # NOTE: Data parsing and Dashboard navigation are automatically triggered
        # by the change in current_station_id() through window_debounced -> fetch state machine
    })

    # Dynamic Plots Panel - All plots in grid format (no tabs)
    # Only display plots that have data available
    output$details_tabs <- renderUI({
        req(input$data_resolution)

        # Don't render if we are currently loading/fetching new data
        if (isTRUE(loading_status())) {
            return(NULL)
        }

        res <- isolate(tolower(input$data_resolution))
        df <- station_data()
        req(df)

        # Clean data once for checking
        df_clean <- clean_dwd_data(df)

        # Check data availability for each parameter
        has_temp <- ("temp" %in% names(df_clean) && any(!is.na(df_clean$temp))) ||
            ("temp_min" %in% names(df_clean) && any(!is.na(df_clean$temp_min))) ||
            ("temp_max" %in% names(df_clean) && any(!is.na(df_clean$temp_max)))

        has_humidity <- ("rh" %in% names(df_clean) && any(!is.na(df_clean$rh))) ||
            ("dew_point" %in% names(df_clean) && any(!is.na(df_clean$dew_point)))

        has_wind <- ("wind_speed" %in% names(df_clean) && any(!is.na(df_clean$wind_speed))) ||
            ("wind_gust_max" %in% names(df_clean) && any(!is.na(df_clean$wind_gust_max)))

        has_pressure <- ("pressure" %in% names(df_clean) && any(!is.na(df_clean$pressure))) ||
            ("station_pressure" %in% names(df_clean) && any(!is.na(df_clean$station_pressure)))

        has_cloud <- ("cloud_cover" %in% names(df_clean) && any(!is.na(df_clean$cloud_cover)))

        has_precip <- ("precip" %in% names(df_clean) && any(!is.na(df_clean$precip)))

        has_solar <- ("solar_global" %in% names(df_clean) && any(!is.na(df_clean$solar_global)))

        has_sunshine <- ("sunshine_duration" %in% names(df_clean) && any(!is.na(df_clean$sunshine_duration)))

        has_wind_dir <- ("wind_dir" %in% names(df_clean) && any(!is.na(df_clean$wind_dir))) && has_wind

        has_daily_temp <- ("temp_min" %in% names(df_clean) && any(!is.na(df_clean$temp_min))) &&
            ("temp_max" %in% names(df_clean) && any(!is.na(df_clean$temp_max)))

        has_snow <- ("snow_depth" %in% names(df_clean) && any(!is.na(df_clean$snow_depth))) ||
            ("snow_water_equiv" %in% names(df_clean) && any(!is.na(df_clean$snow_water_equiv))) ||
            ("snow_fresh_sum" %in% names(df_clean) && any(!is.na(df_clean$snow_fresh_sum))) ||
            ("snow_depth_sum" %in% names(df_clean) && any(!is.na(df_clean$snow_depth_sum)))

        has_soil_temp <- ("soil_temp_2cm" %in% names(df_clean) && any(!is.na(df_clean$soil_temp_2cm))) ||
            ("soil_temp_5cm" %in% names(df_clean) && any(!is.na(df_clean$soil_temp_5cm))) ||
            ("soil_temp_10cm" %in% names(df_clean) && any(!is.na(df_clean$soil_temp_10cm))) ||
            ("soil_temp_20cm" %in% names(df_clean) && any(!is.na(df_clean$soil_temp_20cm))) ||
            ("soil_temp_50cm" %in% names(df_clean) && any(!is.na(df_clean$soil_temp_50cm))) ||
            ("soil_temp_100cm" %in% names(df_clean) && any(!is.na(df_clean$soil_temp_100cm)))

        # Build list of available plots
        plot_list <- tagList()

        if (has_temp) {
            plot_list <- tagList(plot_list, div(class = "col-12 col-lg-6", plotlyOutput("temp_plot", height = "320px")))
        }
        if (has_humidity) {
            plot_list <- tagList(plot_list, div(class = "col-12 col-lg-6", plotlyOutput("humidity_plot", height = "320px")))
        }
        if (has_wind) {
            plot_list <- tagList(plot_list, div(class = "col-12 col-lg-6", plotlyOutput("wind_overview_plot", height = "320px")))
        }
        if (has_pressure) {
            plot_list <- tagList(plot_list, div(class = "col-12 col-lg-6", plotlyOutput("pressure_plot", height = "320px")))
        }
        if (has_cloud) {
            plot_list <- tagList(plot_list, div(class = "col-12 col-lg-6", plotlyOutput("cloud_plot", height = "320px")))
        }
        if (has_precip) {
            plot_list <- tagList(plot_list, div(class = "col-12 col-lg-6", plotlyOutput("precip_plot", height = "320px")))
        }
        if (has_solar) {
            plot_list <- tagList(plot_list, div(class = "col-12 col-lg-6", plotlyOutput("solar_radiation_plot", height = "320px")))
        }
        if (has_sunshine) {
            plot_list <- tagList(plot_list, div(class = "col-12 col-lg-6", plotlyOutput("sunshine_plot", height = "320px")))
        }

        # Conditional: Wind Rose for hourly, Weathergami for daily
        if (res == "hourly" && has_wind_dir) {
            plot_list <- tagList(plot_list, div(class = "col-12 col-lg-6", plotlyOutput("wind_rose", height = "320px")))
        }
        if (res == "daily" && has_daily_temp) {
            plot_list <- tagList(plot_list, div(class = "col-12 col-lg-6", plotlyOutput("weathergami_plot", height = "320px")))
        }
        if (has_snow) {
            plot_list <- tagList(plot_list, div(class = "col-12 col-lg-6", plotlyOutput("snow_plot", height = "320px")))
        }
        if (has_soil_temp) {
            plot_list <- tagList(plot_list, div(class = "col-12 col-lg-6", plotlyOutput("soil_temp_plot", height = "320px")))
        }

        # Wrap in grid container
        div(
            class = "row g-3",
            style = "padding: 10px;",
            plot_list
        )
    })
    # --- URL Synchronization Observers ---

    # 1. Tab changes
    observeEvent(input$main_nav,
        {
            broadcast_state()
        },
        ignoreInit = TRUE
    )

    # 2. Station ID change (covers map click and selector)
    observeEvent(current_station_id(),
        {
            broadcast_state()
        },
        ignoreInit = TRUE
    )

    # 3. Dashboard subtab changes
    observeEvent(input$dashboard_subtabs,
        {
            broadcast_state()
        },
        ignoreInit = TRUE
    )

    # 4. Resolution changes
    observeEvent(input$data_resolution,
        {
            broadcast_state()
        },
        ignoreInit = TRUE
    )

    # 5. Date Range changes
    observeEvent(input$date_range,
        {
            broadcast_state()
        },
        ignoreInit = TRUE
    )
}