File size: 53,189 Bytes
57537fb
 
dfb98ef
57537fb
 
 
 
9291a0b
81b5575
 
753bf84
 
 
 
 
 
81b5575
 
 
 
 
 
 
32a9208
 
 
 
81b5575
 
 
 
 
 
 
 
 
 
 
 
 
 
33f1e33
 
 
 
81b5575
32a9208
81b5575
 
 
 
 
32a9208
 
 
 
 
81b5575
32a9208
 
 
 
 
 
 
81b5575
 
 
 
32a9208
81b5575
 
32a9208
81b5575
 
 
5eef85e
81b5575
 
 
 
 
 
 
 
 
 
32a9208
81b5575
 
32a9208
81b5575
 
 
 
32a9208
81b5575
 
 
 
32a9208
81b5575
 
 
 
 
 
 
 
 
32a9208
81b5575
 
 
 
32a9208
 
 
81b5575
 
 
 
 
 
 
 
 
32a9208
81b5575
 
 
 
 
 
 
 
 
 
 
eba4bb2
32a9208
 
 
81b5575
 
 
 
 
32a9208
81b5575
 
57537fb
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
9291a0b
 
57537fb
 
 
9291a0b
 
 
 
57537fb
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
9291a0b
57537fb
 
 
9291a0b
 
57537fb
 
 
 
 
753bf84
 
 
 
0c8f6bf
753bf84
 
 
 
 
 
 
 
 
 
 
57537fb
 
753bf84
57537fb
 
 
 
 
 
 
 
753bf84
57537fb
753bf84
 
57537fb
753bf84
 
57537fb
 
 
 
753bf84
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
57537fb
753bf84
 
 
 
 
 
57537fb
 
9291a0b
57537fb
 
 
 
 
 
 
 
 
 
 
 
 
 
753bf84
 
 
 
 
 
 
 
 
 
 
 
 
 
 
57537fb
 
753bf84
57537fb
9291a0b
57537fb
649596b
57537fb
9291a0b
 
 
 
57537fb
 
 
 
9291a0b
753bf84
57537fb
 
 
753bf84
 
 
57537fb
753bf84
 
 
 
 
 
 
57537fb
753bf84
57537fb
753bf84
 
 
 
 
57537fb
753bf84
 
 
 
 
 
 
 
 
 
 
 
 
57537fb
753bf84
 
57537fb
753bf84
 
32a9208
753bf84
 
 
 
57537fb
 
 
 
 
 
 
9291a0b
57537fb
 
 
 
 
 
 
 
 
 
 
 
 
 
 
9291a0b
57537fb
 
 
 
 
 
 
 
 
 
 
 
 
9291a0b
57537fb
 
9291a0b
 
 
 
 
57537fb
9291a0b
 
20ff19a
9291a0b
 
 
20ff19a
9291a0b
20ff19a
9291a0b
57537fb
9291a0b
 
 
 
57537fb
9291a0b
57537fb
 
 
 
 
9291a0b
57537fb
9291a0b
 
 
57537fb
 
9291a0b
57537fb
9291a0b
57537fb
 
9291a0b
57537fb
 
 
9291a0b
57537fb
 
 
 
9291a0b
57537fb
9291a0b
57537fb
9291a0b
 
57537fb
 
9291a0b
57537fb
 
9291a0b
57537fb
 
 
 
 
9291a0b
 
 
 
 
 
 
 
 
57537fb
 
9291a0b
57537fb
 
9291a0b
 
 
 
 
 
57537fb
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
9291a0b
 
dfb98ef
 
9291a0b
57537fb
 
 
9291a0b
57537fb
9291a0b
57537fb
 
 
9291a0b
57537fb
 
 
 
 
 
 
 
 
 
 
 
 
 
9291a0b
57537fb
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
9291a0b
57537fb
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
eba4bb2
57537fb
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
9291a0b
 
57537fb
 
 
 
 
 
 
 
 
 
 
 
 
 
55e7bd0
 
57537fb
 
 
 
 
 
 
 
 
5d06f1f
 
 
57537fb
 
 
 
 
 
 
 
 
 
 
 
 
 
 
9291a0b
 
 
 
57537fb
9291a0b
57537fb
9291a0b
57537fb
 
 
 
 
9291a0b
 
 
 
 
 
 
 
 
 
 
 
 
 
57537fb
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
55e7bd0
57537fb
55e7bd0
57537fb
 
 
 
 
 
 
 
 
 
 
 
55e7bd0
 
57537fb
 
 
 
 
 
 
 
dfb98ef
9291a0b
dfb98ef
 
 
 
 
9291a0b
dfb98ef
 
 
 
9291a0b
dfb98ef
 
9291a0b
32a9208
9291a0b
dfb98ef
9291a0b
32a9208
 
 
9291a0b
dfb98ef
9291a0b
32a9208
9291a0b
 
dfb98ef
9291a0b
32a9208
9291a0b
 
dfb98ef
9291a0b
32a9208
9291a0b
 
dfb98ef
9291a0b
32a9208
9291a0b
 
dfb98ef
9291a0b
32a9208
 
 
9291a0b
dfb98ef
9291a0b
32a9208
9291a0b
 
dfb98ef
9291a0b
32a9208
 
 
9291a0b
dfb98ef
 
 
9291a0b
dfb98ef
 
32a9208
 
dfb98ef
 
 
9291a0b
dfb98ef
 
 
9291a0b
dfb98ef
 
 
 
9291a0b
 
dfb98ef
9291a0b
dfb98ef
9291a0b
dfb98ef
9291a0b
dfb98ef
9291a0b
dfb98ef
 
 
 
 
 
9291a0b
dfb98ef
9291a0b
dfb98ef
 
9291a0b
dfb98ef
9291a0b
dfb98ef
 
9291a0b
e3e2c79
9291a0b
e3e2c79
9291a0b
e3e2c79
dfb98ef
9291a0b
dfb98ef
 
9291a0b
dfb98ef
9291a0b
dfb98ef
 
9291a0b
dfb98ef
9291a0b
dfb98ef
 
9291a0b
dfb98ef
9291a0b
dfb98ef
 
9291a0b
dfb98ef
9291a0b
dfb98ef
 
9291a0b
dfb98ef
 
d705a72
57537fb
 
d705a72
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
57537fb
 
 
9291a0b
 
57537fb
9291a0b
57537fb
 
 
 
 
9291a0b
57537fb
 
 
 
 
 
 
753bf84
57537fb
 
 
 
 
 
 
 
 
 
 
9291a0b
57537fb
9291a0b
 
57537fb
 
 
 
 
 
 
 
 
 
 
 
 
9291a0b
57537fb
 
9291a0b
 
57537fb
9291a0b
 
 
57537fb
9291a0b
57537fb
 
 
 
 
 
b3cdfbe
57537fb
 
 
 
 
 
 
 
 
 
 
 
 
 
 
9291a0b
57537fb
 
 
 
9291a0b
57537fb
9291a0b
 
 
57537fb
9291a0b
57537fb
9291a0b
57537fb
9291a0b
57537fb
 
 
 
 
 
 
753bf84
9291a0b
57537fb
 
32a9208
81b5575
 
9291a0b
57537fb
 
 
 
 
32a9208
81b5575
32a9208
 
 
 
 
 
 
 
81b5575
32a9208
 
 
 
 
 
 
 
81b5575
32a9208
 
 
 
 
 
 
 
81b5575
32a9208
 
 
 
 
 
 
57537fb
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
source("funs/scraper.R")
source("funs/map_helpers.R")
source("funs/plot_weather_jma.R")

server <- function(input, output, session) {
    selected_station <- reactiveVal(NULL)
    station_data <- reactiveVal(NULL) # Store loaded data for display
    prev_dates <- reactiveVal(NULL) # Track previous date inputs for specific difference detection
    url_initialized <- reactiveVal(FALSE) # Track if URL params have been applied

    # 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

    # Helper: Parse URL query string
    parse_url_params <- function(query_string) {
        if (is.null(query_string) || query_string == "" || query_string == "?") {
            return(list())
        }
        # Remove leading "?"
        qs <- sub("^\\?", "", query_string)
        if (qs == "") {
            return(list())
        }

        pairs <- strsplit(qs, "&")[[1]]
        params <- list()
        for (pair in pairs) {
            kv <- strsplit(pair, "=")[[1]]
            if (length(kv) == 2) {
                params[[kv[1]]] <- URLdecode(kv[2])
            }
        }
        params
    }

    # Helper: Broadcast current state to parent page
    broadcast_state <- function(view_override = NULL) {
        st <- selected_station()
        # Extract scalar values from tibble row
        station_id <- if (!is.null(st) && nrow(st) > 0) as.character(st$ID[[1]]) else NULL
        station_name <- if (!is.null(st) && nrow(st) > 0) as.character(st$DisplayName[[1]]) else NULL
        prefecture <- if (!is.null(st) && nrow(st) > 0 && !is.na(st$PrecName[[1]])) as.character(st$PrecName[[1]]) else NULL
        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 == "Station Info") {
                "station-info"
            } else if (main_tab == "Dashboard") {
                subtab <- input$dashboard_subtabs
                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,
            prefecture = prefecture,
            resolution = resolution,
            view = view,
            start = start_date,
            end = end_date
        ))
    }

    # Observer: Apply URL params on app startup
    observe({
        req(!url_initialized())

        query <- session$clientData$url_search
        params <- parse_url_params(query)

        if (length(params) == 0) {
            url_initialized(TRUE)
            return()
        }

        # Apply resolution
        if (!is.null(params$resolution) && params$resolution %in% c("10 Minutes", "Hourly", "Daily", "Monthly")) {
            updateRadioButtons(session, "data_resolution", selected = params$resolution)
        }

        # Apply date range
        if (!is.null(params$start) && !is.null(params$end)) {
            start_date <- tryCatch(as.Date(params$start), error = function(e) NULL)
            end_date <- tryCatch(as.Date(params$end), error = function(e) NULL)
            if (!is.null(start_date) && !is.null(end_date)) {
                updateDateRangeInput(session, "date_range", start = start_date, end = end_date)
                prev_dates(c(start_date, end_date))
            }
        }

        # Apply station selection (deferred to allow inputs to update)
        if (!is.null(params$station)) {
            station_id <- params$station
            # Find station by ID or DisplayName
            st <- stations %>%
                filter(ID == station_id | DisplayName == station_id) %>%
                head(1)
            if (nrow(st) > 0) {
                selected_station(st)
                updateSelectizeInput(session, "station_selector", selected = st$DisplayName)
                # Trigger download after a short delay to let other inputs settle
                shinyjs::delay(500, {
                    start_download(st)
                })
            }
        }

        # Apply view/tab navigation (deferred)
        if (!is.null(params$view)) {
            view <- params$view
            shinyjs::delay(600, {
                if (view == "map") {
                    session$sendCustomMessage("switchTab", list(tabId = "Map View"))
                } else if (view == "station-info") {
                    session$sendCustomMessage("switchTab", list(tabId = "Station Info"))
                } else if (view %in% c("dashboard-plots", "dashboard-data")) {
                    session$sendCustomMessage("switchTab", list(tabId = "Dashboard"))
                    if (view == "dashboard-data") {
                        shinyjs::delay(800, {
                            # Click the tab directly; assumes id="dashboard_subtabs" -> li a[data-value="Data"]
                            # Use robust JS to switch tab as updateNavsetCardPill might fail during init
                            shinyjs::runjs("$('a[data-value=\"Data\"]').tab('show');")
                        })
                    }
                }
            })
        }

        url_initialized(TRUE)
    })

    # helper for rich label similar to DWD
    generate_label <- function(name, id, type, start, end, vars) {
        paste0(
            "<div style='font-size:14px; min-width: 200px;'>",
            "<b>", htmltools::htmlEscape(name), "</b> (", id, ")<br>",
            "Type: ", type, "<br>",
            "Period: ", start, " - ", end, "<br>",
            "<div style='margin-top:5px; font-size:12px; color:#555;'>",
            "Vars: ", substring(vars, 1, 50), "...",
            "</div>",
            "</div>"
        )
    }

    add_resolution_period <- function(df, resolution) {
        df %>%
            mutate(
                ResolutionStart = case_when(
                    resolution == "Daily" ~ Start_Daily,
                    resolution == "Hourly" ~ Start_Hourly,
                    resolution == "10 Minutes" ~ Start_10min,
                    resolution == "Monthly" ~ Start_Monthly,
                    TRUE ~ NA_real_
                ),
                ResolutionEnd = case_when(
                    resolution == "Daily" ~ End_Daily,
                    resolution == "Hourly" ~ End_Hourly,
                    resolution == "10 Minutes" ~ End_10min,
                    resolution == "Monthly" ~ End_Monthly,
                    TRUE ~ NA_real_
                )
            )
    }

    get_resolution_period <- function(st, resolution) {
        start_val <- switch(resolution,
            "Daily" = st$Start_Daily,
            "Hourly" = st$Start_Hourly,
            "10 Minutes" = st$Start_10min,
            "Monthly" = st$Start_Monthly,
            NA_real_
        )
        end_val <- switch(resolution,
            "Daily" = st$End_Daily,
            "Hourly" = st$End_Hourly,
            "10 Minutes" = st$End_10min,
            "Monthly" = st$End_Monthly,
            NA_real_
        )
        list(start = start_val, end = end_val)
    }

    format_period_labels <- function(start, end, current_year = NULL) {
        start_label <- ifelse(is.na(start), "Unknown", start)
        if (is.null(current_year)) {
            end_label <- ifelse(is.na(end), "Present", end)
        } else {
            end_label <- ifelse(is.na(end) | end == current_year, "Present", end)
        }
        list(start = start_label, end = end_label)
    }

    filtered_stations <- reactive({
        res <- stations

        # 1. Filter by Name if selected
        # 1. Filter by Name if selected - REMOVED to prevent loop and allow map context
        # if (input$station_selector != "All") {
        #     res <- res %>% filter(DisplayName == input$station_selector)
        # }

        # 2. Filter by Resolution and Date Range
        if (!is.null(input$date_range) && !is.null(input$data_resolution)) {
            start_yr <- as.numeric(format(input$date_range[1], "%Y"))
            end_yr <- as.numeric(format(input$date_range[2], "%Y"))
            resolution <- input$data_resolution

            # Get the start year for the selected resolution
            res <- res %>%
                add_resolution_period(resolution) %>%
                # Filter: Station must have data for this resolution AND overlap with date range
                filter(
                    !is.na(ResolutionStart) & # Must have data for this resolution
                        ResolutionStart <= end_yr & # Started before or in the end year
                        (is.na(ResolutionEnd) | ResolutionEnd >= start_yr) # Still active or ended after start
                ) %>%
                select(-ResolutionStart, -ResolutionEnd)
        }

        res
    })


    output$visible_count <- renderText({
        count <- nrow(filtered_stations())
        sprintf("Visible Stations: %d", count)
    })

    # Server-side selectize for performance
    observe({
        # Update choices based on filtered stations OR all stations?
        # DWD uses all stations usually, but if we filter by resolution, maybe strictly filtered?
        # Let's use filtered for consistency with the map.
        df <- filtered_stations()

        # We need a named vector or just names.
        # choices are DisplayName, values are DisplayName (or ID? existing logic uses DisplayName)
        # Existing logic uses DisplayName for lookup.

        updateSelectizeInput(session, "station_selector",
            choices = c("All", sort(df$DisplayName)),
            server = TRUE
        )
    })

    output$map <- renderMaplibre({
        maplibre(
            style = "https://basemaps.cartocdn.com/gl/positron-gl-style/style.json",
            center = c(138, 36),
            zoom = 4
        ) %>%
            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) # Wait for map to be ready

        # Initial bounds set by renderMaplibre, just mark initialized
        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$Lon, na.rm = TRUE)
            lats <- range(df$Lat, 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$Lon[1], df$Lat[1]), zoom = 12)
            } else {
                maplibre_proxy("map") %>%
                    fit_bounds(c(lons[1], lats[1], lons[2], lats[2]), animate = 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 ---
    observeEvent(input$basemap, {
        proxy <- maplibre_proxy("map")

        # 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 (input$basemap %in% c("carto_positron", "carto_voyager")) {
            # VECTOR LOGIC (Carto-based styles)
            style_url <- switch(input$basemap,
                "carto_positron" = "https://basemaps.cartocdn.com/gl/positron-gl-style/style.json",
                "carto_voyager" = "https://basemaps.cartocdn.com/gl/voyager-gl-style/style.json"
            )

            proxy %>%
                set_style(style_url)

            # For vector sandwich, we want stations below labels
            stations_before_id("watername_ocean")
            style_change_trigger(isolate(style_change_trigger()) + 1)
        } else if (input$basemap == "esri_imagery") {
            # Esri Imagery (Raster + Styles?) - DWD implementation uses Voyager style base + Raster layer
            proxy %>%
                set_style("https://basemaps.cartocdn.com/gl/voyager-gl-style/style.json")

            stations_before_id("watername_ocean")

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

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

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

                    esri_url <- "https://server.arcgisonline.com/ArcGIS/rest/services/World_Imagery/MapServer/tile/{z}/{y}/{x}"

                    maplibre_proxy("map") %>%
                        add_raster_source(id = source_id, tiles = c(esri_url), tileSize = 256) %>%
                        add_layer(
                            id = layer_id,
                            type = "raster",
                            source = source_id,
                            paint = list("raster-opacity" = 1),
                            before_id = "watername_ocean"
                        )

                    current_raster_layers(c(layer_id))
                    style_change_trigger(isolate(style_change_trigger()) + 1)
                })
            }, delay = 0.5)
        } else {
            # RASTER LOGIC (Esri Topo, OSM)
            tile_url <- if (input$basemap %in% c("osm", "osm_gray")) {
                "https://tile.openstreetmap.org/{z}/{x}/{y}.png"
            } else {
                "https://server.arcgisonline.com/ArcGIS/rest/services/World_Topo_Map/MapServer/tile/{z}/{y}/{x}"
            }

            attribution_text <- if (input$basemap %in% c("osm", "osm_gray")) {
                '&copy; <a href="https://www.openstreetmap.org/copyright">OpenStreetMap</a> contributors'
            } else {
                "Tiles &copy; Esri"
            }

            paint_props <- list("raster-opacity" = 1)
            if (input$basemap == "osm_gray") {
                paint_props[["raster-saturation"]] <- -0.9
                paint_props[["raster-contrast"]] <- 0.3
            }

            # Use blank style + raster layer
            blank_style <- list(
                version = 8,
                sources = list(),
                layers = list(),
                metadata = list(timestamp = as.numeric(Sys.time()))
            )
            json_blank <- jsonlite::toJSON(blank_style, auto_unbox = TRUE)
            blank_uri <- paste0("data:application/json,", URLencode(as.character(json_blank), reserved = TRUE))

            proxy %>%
                set_style(blank_uri)

            current_session <- shiny::getDefaultReactiveDomain()
            selected_basemap <- input$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("raster_source_", unique_suffix)
                    layer_id <- paste0("raster_layer_", unique_suffix)

                    maplibre_proxy("map") %>%
                        add_raster_source(id = source_id, tiles = c(tile_url), tileSize = 256, attribution = attribution_text) %>%
                        add_layer(
                            id = layer_id,
                            type = "raster",
                            source = source_id,
                            paint = paint_props
                        )

                    stations_before_id(NULL)
                    current_raster_layers(c(layer_id))
                    style_change_trigger(isolate(style_change_trigger()) + 1)
                })
            }, delay = 0.5)
        }
    })

    # Toggle Labels visibility
    observeEvent(input$show_labels,
        {
            visibility <- if (input$show_labels) "visible" else "none"

            label_layers <- c(
                "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"
            )

            proxy <- maplibre_proxy("map")

            for (layer_id in label_layers) {
                tryCatch(
                    {
                        proxy <- proxy %>% set_layout_property(layer_id, "visibility", visibility)
                    },
                    error = function(e) {
                        # Layer may not exist in current style, ignore silently
                    }
                )
            }
        },
        ignoreInit = TRUE
    )

    # Update Markers
    observe({
        # Wait for map to be ready before adding markers
        req(map_initialized())

        # Dependency on style change
        style_change_trigger()

        # Use system date for default link
        current_year <- as.numeric(format(Sys.Date(), "%Y"))
        resolution <- input$data_resolution

        # Prepare data with labels for hover
        data <- filtered_stations() %>%
            add_resolution_period(resolution) %>%
            mutate(
                station_label = DisplayName,
                station_type_label = ifelse(station_type == "a", "AMeDAS", "Manned station"),
                period_start_label = ifelse(is.na(ResolutionStart), "Unknown", ResolutionStart),
                period_end_label = ifelse(is.na(ResolutionEnd) | ResolutionEnd == current_year, "Present", ResolutionEnd),
                period_label = ifelse(is.na(ResolutionStart), "Unknown", paste0(ResolutionStart, " - ", period_end_label)),
                hover_content = generate_label(DisplayName, ID, station_type_label, period_start_label, period_end_label, ObservedVariables)
            )

        if (nrow(data) > 0) {
            # Convert to sf for MapLibre
            map_data <- st_as_sf(data, coords = c("Lon", "Lat"), crs = 4326)

            maplibre_proxy("map") %>%
                clear_layer("stations") %>%
                add_circle_layer(
                    id = "stations",
                    source = map_data,
                    circle_color = "navy",
                    circle_radius = 6,
                    circle_stroke_color = "#00000000",
                    circle_stroke_width = 0,
                    circle_opacity = 0.7,
                    tooltip = "hover_content",
                    before_id = stations_before_id()
                )
        } else {
            maplibre_proxy("map") %>% clear_layer("stations")
        }

        # Re-apply highlight if a station is selected
        st <- isolate(selected_station())
        if (!is.null(st)) {
            # Re-gen label for consistency
            period <- get_resolution_period(st, resolution)
            period_labels <- format_period_labels(period$start, period$end, current_year)
            lbl <- generate_label(
                st$DisplayName, st$ID,
                ifelse(st$station_type == "a", "AMeDAS", "Manned station"),
                period_labels$start, period_labels$end,
                st$ObservedVariables
            )
            highlight_selected_station(maplibre_proxy("map"), st$Lon, st$Lat, lbl)
        }
    })

    # Observe marker clicks (MapLibre Feature Click)
    observeEvent(input$map_feature_click, {
        click <- input$map_feature_click

        # Check if the click was on the "stations" layer
        if (!is.null(click) && (isTRUE(click$layer_id == "stations") || isTRUE(click$layer == "stations"))) {
            # The properties are available directly
            props <- click$properties
            # Note: simple feature properties might be lowercased or preserved depending on driver,
            # usually they are preserved but let's check keys if issues arise.
            # We passed `data` which had ID, DisplayName etc.

            id_val <- props$ID # Assuming column name is preserved

            if (!is.null(id_val)) {
                # Use layerId (station ID)
                closest <- stations %>%
                    filter(ID == id_val) %>%
                    head(1)

                selected_station(closest)

                # Highlight & Zoom
                current_year <- as.numeric(format(Sys.Date(), "%Y"))
                period <- get_resolution_period(closest, input$data_resolution)
                period_labels <- format_period_labels(period$start, period$end, current_year = current_year)
                lbl <- generate_label(
                    closest$DisplayName, closest$ID,
                    ifelse(closest$station_type == "a", "AMeDAS", "Manned station"),
                    period_labels$start, period_labels$end,
                    closest$ObservedVariables
                )
                highlight_selected_station(maplibre_proxy("map"), closest$Lon, closest$Lat, lbl)

                # Sync sidebar filter
                updateSelectizeInput(session, "station_selector", selected = closest$DisplayName)

                # Start download directly
                start_download(closest)

                # Broadcast state to parent page for SEO URL sync
                broadcast_state()
            }
        }
    })

    # Helper to calculate and enforce limits
    # Returns a list(start=Date, end=Date, limited=FALSE) to be applied
    check_and_limit_dates <- function(start, end, resolution, anchor = "end") {
        limit_days <- Inf
        limit_msg <- ""

        if (resolution %in% c("Hourly", "10 Minutes")) {
            limit_days <- 93 # ~3 months
            limit_msg <- "3 months"
        } else if (resolution == "Daily") {
            limit_days <- 366 # 1 year
            limit_msg <- "1 year"
        } else if (resolution == "Monthly") {
            limit_days <- 1826 # ~5 years
            limit_msg <- "5 years"
        }

        diff <- as.numeric(end - start)
        if (diff > limit_days) {
            new_start <- start
            new_end <- end

            if (anchor == "start") {
                # Start is fixed, adjust end
                new_end <- start + limit_days
            } else {
                # End is fixed (default), adjust start
                new_start <- end - limit_days
            }
            return(list(start = new_start, end = new_end, limited = TRUE, msg = limit_msg))
        }
        return(list(start = start, end = end, limited = FALSE))
    }

    # Auto-refetch when resolution changes
    observeEvent(input$data_resolution, {
        st <- selected_station()
        req(st)
        req(input$date_range)

        start <- as.Date(input$date_range[1])
        end <- as.Date(input$date_range[2])

        # When changing resolution, usually we want to keep the End date and adjust Start if needed
        res_val <- input$data_resolution

        if (res_val == "Monthly") {
            # Special rule for Monthly: Always default to 3 years back
            new_start <- end - 365 * 3
            res <- list(start = new_start, end = end, limited = TRUE, msg = "3 years")
        } else {
            res <- check_and_limit_dates(start, end, res_val, anchor = "end")
        }

        if (res$limited) {
            updateDateRangeInput(session, "date_range", start = res$start, end = res$end)
            showNotification(paste0("Range adjusted to ", res$msg, " for ", input$data_resolution, " resolution."), type = "warning")
            # Update prev dates so we don't trigger the other observer unnecessarily
            prev_dates(c(res$start, res$end))
        }

        # Trigger download
        start_download(st, override_start = res$start, override_end = res$end)
    })

    # Auto-refetch and Smart Limit when Date Range Changes
    observeEvent(input$date_range, {
        st <- selected_station()
        req(input$date_range)

        current_dates <- input$date_range
        current_start <- as.Date(current_dates[1])
        current_end <- as.Date(current_dates[2])

        last_dates <- prev_dates()

        # Logic to determine anchor
        anchor <- "end" # Default

        if (!is.null(last_dates)) {
            last_start <- as.Date(last_dates[1])
            last_end <- as.Date(last_dates[2])

            # If Start changed, anchor Start. If End changed, anchor End.
            if (current_start != last_start) {
                anchor <- "start"
            }
            # If both changed (rare in picker?) or just End, default to End anchor
        }

        # Check Limits
        res <- check_and_limit_dates(current_start, current_end, input$data_resolution, anchor = anchor)

        if (res$limited) {
            # Update UI (this will trigger observer again, but logic should stabilize)
            updateDateRangeInput(session, "date_range", start = res$start, end = res$end)
            showNotification(paste0("Limit exceeded. Adjusted to ", res$msg, "."), type = "warning")
            prev_dates(c(res$start, res$end))

            # Download with CORRECTED dates
            if (!is.null(st)) {
                start_download(st, override_start = res$start, override_end = res$end)
            }
        } else {
            # No limit applied, just update state
            prev_dates(current_dates)
            # Download with CURRENT dates
            if (!is.null(st)) {
                # Only download if effectively changed (checking prev might be good, but safe to call)
                if (tab_down_trigger_check(current_dates, last_dates)) {
                    start_download(st, override_start = current_start, override_end = current_end)
                }
            }
        }
    })

    # Helper to avoid double downloads if dates haven't materially changed
    tab_down_trigger_check <- function(curr, last) {
        if (is.null(last)) {
            return(TRUE)
        }
        if (curr[1] != last[1] || curr[2] != last[2]) {
            return(TRUE)
        }
        return(FALSE)
    }

    # --- Async Fetch State Machine ---

    # State Variables
    fetch_stage <- reactiveVal(0) # 0=Idle, 1=Init, 2=Next, 4=Download/Parse, 6=Merge
    fetch_queue <- reactiveVal(list())
    fetch_queue_idx <- reactiveVal(0)
    parsed_data_list <- reactiveVal(list())
    fetch_current_token <- reactiveVal(NULL)
    final_data_path <- reactiveVal(NULL) # Path to the ready-to-download file

    reset_fetch <- function() {
        fetch_stage(0)
        fetch_current_token(as.numeric(Sys.time()))
        # Unfreeze UI
        session$sendCustomMessage("unfreezeUI", list())
        # We don't need to enable buttons since we depend on click
    }

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

    start_download <- function(st, override_start = NULL, override_end = NULL) {
        req(input$date_range)

        # Clear existing data instantly to indicate refresh
        station_data(NULL)

        if (!is.null(override_start)) {
            start_date <- as.Date(override_start)
        } else {
            start_date <- as.Date(input$date_range[1])
        }

        if (!is.null(override_end)) {
            end_date <- as.Date(override_end)
        } else {
            end_date <- as.Date(input$date_range[2])
        }

        # --- Enforce Date Range Limits (Safety Check only) ---
        # Note: The smart observers should handle UI updates and overrides.
        # This block remains just to clamp wildly invalid args if they bypass observers.
        # For simplicity, we trust overrides or input, assuming observers are working.
        # ---------------------------------

        # UI Setup
        # shinyjs calls removed or replaced by freezeUI
        msg <- paste0("Initializing fetch for ", st$DisplayName, "...")
        session$sendCustomMessage("freezeUI", list(text = msg, station = st$DisplayName))

        # Generate Queue based on resolution
        resolution <- input$data_resolution

        if (resolution %in% c("Daily", "Monthly")) {
            # Daily/Monthly: iterate months
            seq_start <- as.Date(format(start_date, format = "%Y-%m-01"))
            seq_end <- as.Date(format(end_date, format = "%Y-%m-01"))

            # Clamp to current month to avoid future requests
            current_month_start <- as.Date(format(Sys.Date(), "%Y-%m-01"))
            if (seq_end > current_month_start) {
                seq_end <- current_month_start
            }

            dates <- seq(seq_start, seq_end, by = "month")

            if (length(dates) == 0) {
                showNotification("Invalid date range.", type = "error")
                reset_fetch()
                return()
            }

            # Create queue items (month-level)
            q <- list()
            for (i in seq_along(dates)) {
                d <- dates[i]
                yr <- as.numeric(format(d, format = "%Y"))
                mo <- as.numeric(format(d, format = "%m"))
                q[[length(q) + 1]] <- list(year = yr, month = mo, day = NULL)
            }
        } else {
            # Hourly/10-min: iterate days
            # Clamp end_date to today to avoid future requests
            today <- Sys.Date()
            if (end_date > today) {
                end_date <- today
            }

            dates <- seq(start_date, end_date, by = "day")

            if (length(dates) == 0) {
                showNotification("Invalid date range.", type = "error")
                reset_fetch()
                return()
            }

            # Create queue items (day-level)
            q <- list()
            for (i in seq_along(dates)) {
                d <- as.Date(dates[i], origin = "1970-01-01")
                yr <- as.numeric(format(d, format = "%Y"))
                mo <- as.numeric(format(d, format = "%m"))
                dy <- as.numeric(format(d, format = "%d"))
                q[[length(q) + 1]] <- list(year = yr, month = mo, day = dy)
            }
        }


        fetch_queue(q)
        fetch_queue_idx(1)
        parsed_data_list(list())
        fetch_current_token(as.numeric(Sys.time()))

        fetch_stage(2) # Go to Next
    }

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

        if (idx > length(q)) {
            fetch_stage(6) # Merge
        } else {
            # UI Update
            item <- q[[idx]]
            if (is.null(item$day)) {
                msg <- paste0(
                    "Fetching data for ", item$year, "-", sprintf("%02d", item$month),
                    " (", idx, "/", length(q), ")..."
                )
            } else {
                msg <- paste0(
                    "Fetching data for ", item$year, "-", sprintf("%02d", item$month), "-", sprintf("%02d", item$day),
                    " (", idx, "/", length(q), ")..."
                )
            }
            session$sendCustomMessage("freezeUI", list(text = msg))
            fetch_stage(4) # Go to Download/Parse
        }
    })

    # Stage 4: Download & Parse (Async Yield)
    observe({
        req(fetch_stage() == 4)
        token <- fetch_current_token()

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

            isolate({
                idx <- fetch_queue_idx()
                q <- fetch_queue()
                item <- q[[idx]]
                st <- selected_station()

                # Call Scraper
                df <- tryCatch(
                    get_jma_data(
                        block_no = st$ID,
                        year = item$year,
                        month = item$month,
                        day = item$day,
                        prec_no = st$prec_no,
                        type = paste0(st$station_type, "1"),
                        resolution = input$data_resolution
                    ),
                    error = function(e) NULL
                )

                if (!is.null(df)) {
                    if ("Day" %in% names(df)) {
                        df <- df %>%
                            mutate(Date = as.Date(sprintf("%04d-%02d-%02d", Year, Month, Day)))
                    }
                    plist <- parsed_data_list()
                    plist[[length(plist) + 1]] <- df
                    parsed_data_list(plist)
                }

                fetch_queue_idx(idx + 1)
                fetch_stage(2)
            })
        }, 0.5)
        fetch_stage(-1)
    })

    # Stage 6: Merge & Finalize
    observe({
        req(fetch_stage() == 6)
        token <- fetch_current_token()
        msg <- "Merging data..."
        session$sendCustomMessage("freezeUI", list(text = msg))

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

            isolate({
                plist <- parsed_data_list()

                if (length(plist) == 0) {
                    showNotification("No data found for the selected range.", type = "warning", session = session)
                    reset_fetch()
                    return()
                }

                # Merge
                final_df <- bind_rows(plist)

                # Filter precise range
                start_date <- input$date_range[1]
                end_date <- input$date_range[2]

                if ("Date" %in% names(final_df)) {
                    final_df <- final_df %>%
                        filter(Date >= start_date & Date <= end_date) %>%
                        select(-Date)
                }

                if (nrow(final_df) == 0) {
                    showNotification("No data in exact range.", type = "warning", session = session)
                    reset_fetch()
                    return()
                }

                # Save to temp file for downloadHandler
                tmp <- tempfile(fileext = ".xlsx")
                write_xlsx(final_df, path = tmp)
                final_data_path(tmp)

                # Update reactive for display
                station_data(final_df)

                # Success UI
                reset_fetch()

                # Switch to "Dashboard" tab
                if (is.null(input$main_nav) || input$main_nav != "Dashboard") {
                    session$sendCustomMessage("switchTab", list(tabId = "Dashboard"))
                }
                showNotification(paste("Successfully loaded", nrow(final_df), "rows."), type = "message", session = session)
            })
        }, 0.1)
        fetch_stage(-1)
    })

    # Output: Station Ready (Controls Dashboard Visibility)
    output$station_ready <- reactive({
        !is.null(station_data())
    })
    outputOptions(output, "station_ready", suspendWhenHidden = FALSE)

    # Output: Station Info Header (Dashboard Card)
    output$station_info_header <- renderUI({
        st <- selected_station()
        if (is.null(st)) {
            return(NULL)
        }

        # Determine Resolution Badge
        res_label <- input$data_resolution
        res_class <- if (res_label == "Hourly") "bg-primary" else "bg-success"

        # Data Range Text
        df <- station_data()
        if (is.null(df) || nrow(df) == 0) {
            dates_text <- "No data loaded"
        } else {
            # Assumes 'Date' column exists for Daily, or constructing from Year/Month/Day
            if ("Date" %in% names(df)) {
                d_range <- range(df$Date, na.rm = TRUE)
                dates_text <- paste(d_range[1], "to", d_range[2])
            } else if ("Year" %in% names(df) && "Month" %in% names(df) && "Day" %in% names(df)) {
                # Hourly/10min
                # Construct dates just for display
                start_d <- as.Date(paste(df$Year[1], df$Month[1], df$Day[1], sep = "-"))
                n <- nrow(df)
                end_d <- as.Date(paste(df$Year[n], df$Month[n], df$Day[n], sep = "-"))
                dates_text <- paste(start_d, "to", end_d)
            } else {
                dates_text <- "Loaded"
            }
        }

        # Render 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(st$DisplayName, style = "font-size: 1.1rem;"), br(),
                        tags$small(class = "text-muted", paste("ID:", st$ID))
                    ),
                    # Col 2: Location
                    div(
                        strong("Location"), br(),
                        span(paste0(st$Lat, "°N, ", st$Lon, "°E")), br(),
                        tags$small(class = "text-muted", st$PrecLabel)
                    ),
                    # Col 3: Technical
                    div(
                        strong("Technical"), br(),
                        span(paste0(st$Elevation, " 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(
                            "downloadData",
                            label = "Export Excel",
                            class = "btn-sm btn-primary",
                            icon = icon("file-excel")
                        )
                    )
                )
            )
        )
    })

    # Download Handler
    output$downloadData <- downloadHandler(
        filename = function() {
            req(selected_station())
            st <- selected_station()
            res <- if (!is.null(input$data_resolution)) tolower(input$data_resolution) else "data"
            sprintf("jma_%s_%s_%s.xlsx", st$ID, res, format(Sys.Date(), "%Y%m%d"))
        },
        content = function(file) {
            req(final_data_path())
            file.copy(final_data_path(), file)
        }
    )

    # Render Table
    # --- Dashboard Logic ---

    # 1. Update Variable Choices when data changes


    # 2. Render Plot
    # --- Dynamic Plot Rendering ---

    # 1. Determine available variables and render UI
    output$jma_plots_container <- renderUI({
        df <- station_data()
        req(df)

        # Identify available data
        # We check columns availability

        plot_list <- tagList()

        # Temperature
        has_temp <- any(c("Temperature", "Temp_Mean", "Temp_Max", "Temp_Min") %in% names(df) & !all(is.na(df[[if ("Temperature" %in% names(df)) "Temperature" else "Temp_Mean"]])))
        if (has_temp) {
            plot_list <- tagList(plot_list, div(class = "col-12 col-lg-6", plotlyOutput("plot_temp", height = "350px")))
        }

        # Precipitation
        if ("Precipitation" %in% names(df) && !all(is.na(df$Precipitation))) {
            plot_list <- tagList(plot_list, div(class = "col-12 col-lg-6", plotlyOutput("plot_precip", height = "300px")))
        }

        # Wind
        if ("Wind_Speed" %in% names(df) && !all(is.na(df$Wind_Speed))) {
            plot_list <- tagList(plot_list, div(class = "col-12 col-lg-6", plotlyOutput("plot_wind", height = "350px")))
        }

        # Humidity
        if ("Humidity" %in% names(df) && !all(is.na(df$Humidity))) {
            plot_list <- tagList(plot_list, div(class = "col-12 col-lg-6", plotlyOutput("plot_humidity", height = "350px")))
        }

        # Pressure
        if ("Pressure" %in% names(df) && !all(is.na(df$Pressure))) {
            plot_list <- tagList(plot_list, div(class = "col-12 col-lg-6", plotlyOutput("plot_pressure", height = "300px")))
        }

        # Sunshine
        has_sun <- any(c("Sunshine_Hours", "Sunshine_Minutes") %in% names(df) & !all(is.na(df[[if ("Sunshine_Hours" %in% names(df)) "Sunshine_Hours" else "Sunshine_Minutes"]])))
        if (has_sun) {
            plot_list <- tagList(plot_list, div(class = "col-12 col-lg-6", plotlyOutput("plot_sunshine", height = "300px")))
        }

        # Solar
        if ("Solar_Radiation" %in% names(df) && !all(is.na(df$Solar_Radiation))) {
            plot_list <- tagList(plot_list, div(class = "col-12 col-lg-6", plotlyOutput("plot_solar", height = "300px")))
        }

        # Snow
        has_snow <- any(c("Snow_Depth", "Snowfall") %in% names(df) & !all(is.na(df[[if ("Snow_Depth" %in% names(df)) "Snow_Depth" else "Snowfall"]])))
        if (has_snow) {
            plot_list <- tagList(plot_list, div(class = "col-12 col-lg-6", plotlyOutput("plot_snow", height = "300px")))
        }

        if (length(plot_list) == 0) {
            return(div(class = "alert alert-warning", "No plottable weather data found for this period."))
        }

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

    # 2. Render individual plots
    # We use observer to ensure data is fresh, although standard renderPlotly also works if outputs are defined.
    # We define all possible outputs throughout. Even if they are not in UI, it's fine.

    # Helper to prepare data with DateTime
    get_plot_df <- reactive({
        df <- station_data()
        req(df)
        res <- input$data_resolution

        if (res == "Monthly") {
            df$DateTime <- as.Date(paste(df$Year, df$Month, "01", sep = "-"))
        } else if (res == "Daily") {
            df$DateTime <- as.Date(paste(df$Year, df$Month, df$Day, sep = "-"))
        } else if (res == "Hourly") {
            df$DateTime <- as.POSIXct(paste0(df$Year, "-", df$Month, "-", df$Day, " ", df$Hour, ":00"), format = "%Y-%m-%d %H:%M")
        } else { # 10 Minutes
            df$DateTime <- as.POSIXct(paste0(df$Year, "-", df$Month, "-", df$Day, " ", df$Time), format = "%Y-%m-%d %H:%M")
        }
        df
    })

    output$plot_temp <- renderPlotly({
        df <- get_plot_df()
        create_temperature_plot_jma(df, input$data_resolution)
    })

    output$plot_precip <- renderPlotly({
        df <- get_plot_df()
        create_precipitation_plot_jma(df, input$data_resolution)
    })

    output$plot_wind <- renderPlotly({
        df <- get_plot_df()
        resolution <- input$data_resolution
        if (resolution == "Hourly") {
            create_wind_rose_jma(df, resolution)
        } else {
            create_wind_plot_jma(df, resolution)
        }
    })

    output$plot_humidity <- renderPlotly({
        df <- get_plot_df()
        create_humidity_plot_jma(df, input$data_resolution)
    })

    output$plot_pressure <- renderPlotly({
        df <- get_plot_df()
        create_pressure_plot_jma(df, input$data_resolution)
    })

    output$plot_sunshine <- renderPlotly({
        df <- get_plot_df()
        create_sunshine_plot_jma(df, input$data_resolution)
    })

    output$plot_solar <- renderPlotly({
        df <- get_plot_df()
        create_solar_plot_jma(df, input$data_resolution)
    })

    output$plot_snow <- renderPlotly({
        df <- get_plot_df()
        create_snow_plot_jma(df, input$data_resolution)
    })

    # --- Data Table Renderer ---
    output$daily_data <- DT::renderDataTable({
        req(station_data())
        df <- station_data()

        # Column Mapping
        col_map <- c(
            "Year" = "Year",
            "Month" = "Month",
            "Day" = "Day",
            "Hour" = "Hour",
            "Time" = "Time",
            "Pressure" = "Pressure [hPa]",
            "Pressure_Sea_Level" = "Sea Level Pressure [hPa]",
            "Precipitation" = "Precipitation [mm]",
            "Precipitation_Max_1h" = "Max 1h Precipitation [mm]",
            "Precipitation_Max_10min" = "Max 10m Precipitation [mm]",
            "Temp_Mean" = "Mean Temperature [°C]",
            "Temp_Max" = "Max Temperature [°C]",
            "Temp_Min" = "Min Temperature [°C]",
            "Temperature" = "Temperature [°C]",
            "Humidity" = "Humidity [%]",
            "Humidity_Min" = "Min Humidity [%]",
            "Wind_Speed" = "Wind Speed [m/s]",
            "Wind_Speed_Max" = "Max Wind Speed [m/s]",
            "Wind_Gust_Speed" = "Max Gust Speed [m/s]",
            "Wind_Direction" = "Wind Direction",
            "Wind_Direction_Deg" = "Wind Direction [Deg]",
            "Sunshine_Hours" = "Sunshine Duration [h]",
            "Sunshine_Minutes" = "Sunshine Duration [min]",
            "Solar_Radiation" = "Solar Radiation [MJ/m²]",
            "Snowfall" = "Snowfall [cm]",
            "Snow_Depth" = "Snow Depth [cm]",
            "Snow_Days" = "Snow Days",
            "Fog_Days" = "Fog Days",
            "Thunder_Days" = "Thunder Days",
            "Dew_Point" = "Dew Point [°C]",
            "Vapor_Pressure" = "Vapor Pressure [hPa]",
            "Cloud_Cover" = "Cloud Cover [1/10]",
            "Visibility" = "Visibility [km]"
        )

        # Rename columns that exist in the map
        current_names <- names(df)
        for (i in seq_along(current_names)) {
            cn <- current_names[i]
            if (cn %in% names(col_map)) {
                current_names[i] <- col_map[[cn]]
            }
        }
        names(df) <- current_names

        DT::datatable(df, options = list(pageLength = 15, scrollX = TRUE))
    })

    # Zoom to station if selected (sidebar)
    observeEvent(input$station_selector, {
        if (input$station_selector != "All") {
            closest <- stations %>%
                filter(DisplayName == input$station_selector) %>%
                head(1)
            if (nrow(closest) > 0) {
                selected_station(closest)

                # Highlight & Zoom
                period <- get_resolution_period(closest, input$data_resolution)
                period_labels <- format_period_labels(period$start, period$end, current_year = as.numeric(format(Sys.Date(), "%Y")))
                lbl <- generate_label(
                    closest$DisplayName, closest$ID,
                    ifelse(closest$station_type == "a", "AMeDAS", "Manned station"),
                    period_labels$start, period_labels$end,
                    closest$ObservedVariables
                )
                highlight_selected_station(maplibre_proxy("map"), closest$Lon, closest$Lat, lbl)

                # Start download
                start_download(closest)
            }
        }
    })

    # Station Info Table (Metadata for filtered stations)
    output$station_info_table <- DT::renderDataTable({
        df <- filtered_stations()
        req(df)

        # Select relevant columns based on resolution
        res <- input$data_resolution

        # Determine which start/end columns to show
        start_col <- switch(res,
            "Daily" = "Start_Daily",
            "Hourly" = "Start_Hourly",
            "10 Minutes" = "Start_10min",
            "Monthly" = "Start_Monthly"
        )
        end_col <- switch(res,
            "Daily" = "End_Daily",
            "Hourly" = "End_Hourly",
            "10 Minutes" = "End_10min",
            "Monthly" = "End_Monthly"
        )

        df_display <- df %>%
            select(
                ID,
                Name = DisplayName,
                Prefecture = PrecLabel,
                Lat,
                Lon,
                Elevation,
                Variables = ObservedVariables,
                !!sym(start_col),
                !!sym(end_col)
            )

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

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

        closest <- stations %>%
            filter(ID == id_val) %>%
            head(1)

        req(nrow(closest) > 0)

        selected_station(closest)

        # Highlight & Zoom
        period <- get_resolution_period(closest, input$data_resolution)
        period_labels <- format_period_labels(period$start, period$end, current_year = as.numeric(format(Sys.Date(), "%Y")))
        lbl <- generate_label(
            closest$DisplayName, closest$ID,
            ifelse(closest$station_type == "a", "AMeDAS", "Manned station"),
            period_labels$start, period_labels$end,
            closest$ObservedVariables
        )
        highlight_selected_station(maplibre_proxy("map"), closest$Lon, closest$Lat, lbl)

        # Start download
        start_download(closest)

        # Broadcast state to parent page for SEO URL sync
        broadcast_state()

        # Switch to Dashboard? The DWD app implies it ("view the dashboard").
        # The download will eventually auto-switch, but let's be explicit if needed.
        # Ideally the download logic (Stage 6) handles the switch.
        # But for UX, double-clicking implies "Go there".
    })

    # Observer: Broadcast state when main tab changes
    observeEvent(input$main_nav,
        {
            req(url_initialized())
            broadcast_state()
        },
        ignoreInit = TRUE
    )

    # Observer: Broadcast state when dashboard subtab changes
    observeEvent(input$dashboard_subtabs,
        {
            req(url_initialized())
            broadcast_state()
        },
        ignoreInit = TRUE
    )

    # Observer: Broadcast state when resolution changes
    observeEvent(input$data_resolution,
        {
            req(url_initialized())
            broadcast_state()
        },
        ignoreInit = TRUE
    )

    # Observer: Broadcast state when date range changes
    observeEvent(input$date_range,
        {
            req(url_initialized())
            broadcast_state()
        },
        ignoreInit = TRUE
    )
}