vidi-charts 0.1.2

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

use super::*;
use crate::render::PlotId;
use bevy::input::mouse::{MouseMotion, MouseWheel};
use bevy::prelude::*;
use bevy::window::PrimaryWindow;
use bevy_camera::visibility::RenderLayers;
use bevy_camera::{
    OrthographicProjection, PerspectiveProjection, Projection, ScalingMode, Viewport,
};
use bevy_math::UVec2;
use std::collections::HashSet;

// #[derive(Resource, Clone, Debug)]
// pub struct DashboardRes(pub crate::core::Dashboard);

/// Core system: Sync dashboard plots to tile entities
pub fn sync_plots_to_tiles(
    mut commands: Commands,
    dash: Res<DashboardRes>,
    mut registry: ResMut<TileRegistry>,
    existing: Query<(Entity, &PlotTile)>,
) {
    let active = dash.0.active_plots();

    // Generate plot IDs from active plots (respects tabs)
    let plot_ids: Vec<PlotId> = active
        .iter()
        .enumerate()
        .map(|(i, _)| PlotId(i as u64))
        .collect();

    // Remove tiles for plots that no longer exist
    for (entity, tile) in existing.iter() {
        if !plot_ids.contains(&tile.id) {
            cleanup_tile(&mut commands, &mut registry, entity, tile.id);
        }
    }

    // Create missing tiles
    for (i, plot) in active.iter().enumerate() {
        let id = PlotId(i as u64);

        if let std::collections::hash_map::Entry::Vacant(e) = registry.by_plot.entry(id) {
            let tile = spawn_tile(&mut commands, id, i, plot);
            e.insert(tile);
            registry.dirty.push_back(id);
        }
    }
}

fn spawn_tile(
    commands: &mut Commands,
    id: PlotId,
    index: usize,
    plot: &crate::core::Plot,
) -> Entity {
    let kind = match plot {
        crate::core::Plot::Graph2D(_) => PlotKind::TwoD,
        crate::core::Plot::Graph3D(_) => PlotKind::ThreeD,
        crate::core::Plot::Distribution(_) => PlotKind::TwoD,
        crate::core::Plot::Candlestick(_) => PlotKind::TwoD,
        crate::core::Plot::Heatmap(_) => PlotKind::TwoD,
        crate::core::Plot::Radial(_) => PlotKind::TwoD,
        crate::core::Plot::Field(_) => PlotKind::TwoD,
    };

    let tile = commands
        .spawn((
            PlotTile { id, index, kind },
            kind, // Add PlotKind as separate component for queries
            TileView::default(),
            TileRect {
                world_center: Vec2::ZERO,
                world_size: Vec2::new(100.0, 100.0),
                content: Rect::from_center_size(Vec2::ZERO, Vec2::new(70.0, 70.0)),
                viewport: Viewport {
                    physical_position: UVec2::ZERO,
                    physical_size: UVec2::new(100, 100),
                    depth: 0.0..1.0,
                },
            },
            Transform::default(),
            Visibility::default(),
        ))
        .id();

    // Add View3D for 3D plots
    if matches!(kind, PlotKind::ThreeD) {
        commands.entity(tile).insert(View3D::default());
    }

    // Create render root child with visibility
    let root = commands
        .spawn((TileRenderRoot, Transform::default(), Visibility::default()))
        .id();
    commands.entity(tile).add_child(root);

    tile
}

/// Update tile layout when window resizes
pub fn update_tile_layout(
    windows: Query<&Window, With<PrimaryWindow>>,
    mut registry: ResMut<TileRegistry>,
    mut tiles: Query<(&PlotTile, &mut TileRect)>,
    dash: Res<DashboardRes>,
) {
    let Ok(window) = windows.single() else {
        return;
    };

    let plots = dash.0.active_plots();
    let n = plots.len();
    if n == 0 {
        return;
    }

    let (cols, rows) = grid_dims(n, window.width() / window.height(), dash.0.active_columns());

    let margin = 20.0;
    let gap = 10.0;
    // Reserve space for tab bar at top if using tabs
    let tab_bar_height = if dash.0.has_tabs() { 40.0 } else { 0.0 };

    let avail_w = window.width() - 2.0 * margin;
    let avail_h = window.height() - 2.0 * margin - tab_bar_height;

    let tile_w = (avail_w - (cols - 1) as f32 * gap) / cols as f32;
    let tile_h = (avail_h - (rows - 1) as f32 * gap) / rows as f32;

    for (tile, mut rect) in tiles.iter_mut() {
        let col = tile.index % cols;
        let row = tile.index / cols;

        // Viewport in physical pixels (CRITICAL FIX)
        let vp_x = margin + col as f32 * (tile_w + gap);
        let vp_y = margin + tab_bar_height + row as f32 * (tile_h + gap);

        let scale = window.resolution.scale_factor();
        let phys_pos = UVec2::new((vp_x * scale).round() as u32, (vp_y * scale).round() as u32);
        let phys_size = UVec2::new(
            (tile_w * scale).round() as u32,
            (tile_h * scale).round() as u32,
        );

        // World coordinates (centered origin)
        let world_center = Vec2::new(
            vp_x + tile_w * 0.5 - window.width() * 0.5,
            window.height() * 0.5 - vp_y - tile_h * 0.5,
        );

        let new_size = Vec2::new(tile_w, tile_h);

        // Only mark dirty if layout actually changed
        let changed = rect.world_center != world_center
            || rect.world_size != new_size
            || rect.viewport.physical_position != phys_pos
            || rect.viewport.physical_size != phys_size;

        if changed {
            rect.world_center = world_center;
            rect.world_size = new_size;
            rect.content =
                Rect::from_center_size(world_center, Vec2::new(tile_w - 30.0, tile_h - 30.0));
            rect.viewport = Viewport {
                physical_position: phys_pos,
                physical_size: phys_size,
                depth: 0.0..1.0,
            };

            registry.dirty.push_back(tile.id);
        }
    }
}

/// Create/update cameras for each tile
pub fn sync_tile_cameras(
    mut commands: Commands,
    mut registry: ResMut<TileRegistry>,
    tiles: Query<(Entity, &PlotTile, &TileRect, &PlotKind, Option<&View3D>)>,
    existing: Query<Entity, With<TileCamera>>,
    existing_overlay: Query<Entity, With<TileOverlayCamera>>,
) {
    let mut used = HashSet::new();
    let mut used_overlay = HashSet::new();

    for (_tile_entity, tile, rect, kind, view3d) in tiles.iter() {
        // One layer per tile index (0..31). This is a hard RenderLayers limitation.
        let layer = (tile.index % 32) as u8;
        let layers = RenderLayers::layer(layer.into());
        // Use a separate layer for overlay (offset by 16, wrapping within 0..31)
        let overlay_layer = ((tile.index + 16) % 32) as u8;
        let overlay_layers = RenderLayers::layer(overlay_layer.into());

        let cam_entity = if let Some(&cam) = registry.camera_of.get(&tile.id) {
            cam
        } else {
            let cam = commands.spawn((TileCamera, Transform::default())).id();
            registry.camera_of.insert(tile.id, cam);
            cam
        };

        used.insert(cam_entity);

        match kind {
            PlotKind::TwoD => {
                let mut ortho = OrthographicProjection::default_2d();
                ortho.scaling_mode = ScalingMode::FixedVertical {
                    viewport_height: rect.world_size.y,
                };

                commands.entity(cam_entity).insert((
                    Camera2d,
                    Camera {
                        viewport: Some(rect.viewport.clone()),
                        order: 10 + tile.index as isize,
                        ..default()
                    },
                    Projection::from(ortho),
                    Transform::from_translation(rect.world_center.extend(1000.0)),
                    layers,
                ));
            }
            PlotKind::ThreeD => {
                // Calculate camera position from View3D spherical coordinates
                let view = view3d.copied().unwrap_or_default();
                let cam_transform = compute_orbit_transform(&view);

                commands.entity(cam_entity).insert((
                    Camera3d::default(),
                    Camera {
                        viewport: Some(rect.viewport.clone()),
                        order: 10 + tile.index as isize,
                        ..default()
                    },
                    Projection::from(PerspectiveProjection {
                        fov: std::f32::consts::FRAC_PI_4, // 45 degrees
                        ..default()
                    }),
                    cam_transform,
                    layers,
                ));

                // Create/update overlay 2D camera for titles and borders
                let overlay_entity = if let Some(&overlay) = registry.overlay_of.get(&tile.id) {
                    overlay
                } else {
                    let overlay = commands
                        .spawn((TileOverlayCamera, Transform::default()))
                        .id();
                    registry.overlay_of.insert(tile.id, overlay);
                    overlay
                };
                used_overlay.insert(overlay_entity);

                let mut overlay_ortho = OrthographicProjection::default_2d();
                overlay_ortho.scaling_mode = ScalingMode::FixedVertical {
                    viewport_height: rect.world_size.y,
                };

                commands.entity(overlay_entity).insert((
                    Camera2d,
                    Camera {
                        viewport: Some(rect.viewport.clone()),
                        order: 50 + tile.index as isize, // Render after all 3D cameras (10+N)
                        clear_color: ClearColorConfig::None, // Don't clear, overlay on top
                        ..default()
                    },
                    Projection::from(overlay_ortho),
                    Transform::from_translation(rect.world_center.extend(1000.0)),
                    overlay_layers,
                ));
            }
            PlotKind::Placeholder => {
                // Keep camera if you want placeholders rendered; otherwise you can skip.
                let mut ortho = OrthographicProjection::default_2d();
                ortho.scaling_mode = ScalingMode::FixedVertical {
                    viewport_height: rect.world_size.y,
                };

                commands.entity(cam_entity).insert((
                    Camera2d,
                    Camera {
                        viewport: Some(rect.viewport.clone()),
                        order: 10 + tile.index as isize,
                        ..default()
                    },
                    Projection::from(ortho),
                    Transform::from_translation(rect.world_center.extend(1000.0)),
                    layers,
                ));
            }
        }
    }

    // Despawn cameras no longer used
    for cam_entity in existing.iter() {
        if !used.contains(&cam_entity) {
            commands.entity(cam_entity).despawn();
        }
    }

    // Despawn overlay cameras no longer used
    for overlay_entity in existing_overlay.iter() {
        if !used_overlay.contains(&overlay_entity) {
            commands.entity(overlay_entity).despawn();
        }
    }
}

/// Handle hover detection
pub fn update_hovered_tile(
    windows: Query<&Window>,
    tiles: Query<(&PlotTile, &TileRect)>,
    mut hovered: ResMut<HoveredTile>,
) {
    let Ok(window) = windows.single() else {
        return;
    };
    let Some(cursor) = window.cursor_position() else {
        return;
    };

    hovered.0 = tiles
        .iter()
        .find(|(_, rect)| {
            let half = rect.world_size * 0.5;
            let min = rect.world_center - half;
            let max = rect.world_center + half;

            let world_x = cursor.x - window.width() * 0.5;
            let world_y = window.height() * 0.5 - cursor.y;

            world_x >= min.x && world_x <= max.x && world_y >= min.y && world_y <= max.y
        })
        .map(|(tile, _)| tile.index);
}

/// Compute camera transform from View3D orbit parameters
fn compute_orbit_transform(view: &View3D) -> Transform {
    let cy = view.yaw.cos();
    let sy = view.yaw.sin();
    let cp = view.pitch.cos();
    let sp = view.pitch.sin();

    // Spherical to Cartesian: camera position orbiting target
    let dir = Vec3::new(sy * cp, sp, cy * cp);
    let pos = view.target + dir * view.radius;

    Transform::from_translation(pos).looking_at(view.target, Vec3::Y)
}

/// Handle user input for both 2D and 3D tiles
pub fn handle_input(
    mut tiles_2d: Query<(&PlotTile, &mut TileView), Without<View3D>>,
    mut tiles_3d: Query<(&PlotTile, &mut View3D)>,
    mut registry: ResMut<TileRegistry>,
    hovered: Res<HoveredTile>,
    mouse: Res<ButtonInput<MouseButton>>,
    mut wheel: MessageReader<MouseWheel>,
    mut motion: MessageReader<MouseMotion>,
) {
    let Some(hovered_index) = hovered.0 else {
        return;
    };

    // Collect events first (they can only be read once)
    let mut zoom_delta = 0.0;
    for event in wheel.read() {
        zoom_delta += event.y;
    }

    let mut motion_delta = Vec2::ZERO;
    for event in motion.read() {
        motion_delta += event.delta;
    }

    // Handle 2D tiles
    for (tile, mut view) in tiles_2d.iter_mut() {
        if tile.index != hovered_index {
            continue;
        }

        let mut changed = false;

        // Zoom toward center with proper offset adjustment
        if zoom_delta != 0.0 {
            let old_scale = view.scale;
            let new_scale =
                (view.scale * (1.0 + zoom_delta * 0.05)).clamp(view.min_scale, view.max_scale);

            if new_scale != old_scale {
                view.offset = view.offset * new_scale / old_scale;
                view.scale = new_scale;
                changed = true;
            }
        }

        // Pan (offset is in world coordinates)
        if mouse.pressed(MouseButton::Left) && motion_delta != Vec2::ZERO {
            view.offset.x += motion_delta.x;
            view.offset.y -= motion_delta.y;
            changed = true;
        }

        if changed {
            registry.dirty.push_back(tile.id);
        }
    }

    // Handle 3D tiles
    for (tile, mut view3d) in tiles_3d.iter_mut() {
        if tile.index != hovered_index {
            continue;
        }

        let mut changed = false;
        let orbit_speed = 0.008;
        let pan_speed = 0.01;

        // Zoom (scroll wheel adjusts radius)
        if zoom_delta != 0.0 {
            view3d.radius = (view3d.radius - zoom_delta * 0.5).clamp(2.0, 50.0);
            changed = true;
        }

        // Orbit (left mouse button)
        if mouse.pressed(MouseButton::Left) && motion_delta != Vec2::ZERO {
            view3d.yaw -= motion_delta.x * orbit_speed;
            view3d.pitch = (view3d.pitch - motion_delta.y * orbit_speed).clamp(-1.5, 1.5);
            changed = true;
        }

        // Pan (right mouse button)
        if mouse.pressed(MouseButton::Right) && motion_delta != Vec2::ZERO {
            let right = Vec3::new(view3d.yaw.cos(), 0.0, -view3d.yaw.sin());
            let up = Vec3::Y;
            let radius = view3d.radius;
            view3d.target +=
                (-right * motion_delta.x + up * motion_delta.y) * pan_speed * radius * 0.1;
            changed = true;
        }

        if changed {
            // For 3D, we don't need to redraw - just update the camera
            // But we do need to mark dirty if we want the camera to update
            registry.dirty.push_back(tile.id);
        }
    }
}

/// Auto-fit tiles to their data bounds on first render
pub fn auto_fit_tiles(
    mut commands: Commands,
    mut registry: ResMut<TileRegistry>,
    mut tiles: Query<(Entity, &PlotTile, &TileRect, &mut TileView), Without<AutoFitted>>,
    dash: Res<DashboardRes>,
) {
    for (entity, tile, rect, mut view) in tiles.iter_mut() {
        // Get data bounds for this plot
        let Some(plot) = dash.0.active_plots().get(tile.index) else {
            continue;
        };

        // Compute data bounds based on plot type
        let mut min_x = f32::INFINITY;
        let mut max_x = f32::NEG_INFINITY;
        let mut min_y = f32::INFINITY;
        let mut max_y = f32::NEG_INFINITY;

        match plot {
            crate::core::Plot::Graph2D(graph) => {
                for layer in &graph.layers {
                    for pt in &layer.xy {
                        min_x = min_x.min(pt.x);
                        max_x = max_x.max(pt.x);
                        min_y = min_y.min(pt.y);
                        max_y = max_y.max(pt.y);
                    }
                    // Also consider lower_line for FillBetween geometry
                    if let Some(lower_line) = &layer.lower_line {
                        for pt in lower_line {
                            min_x = min_x.min(pt.x);
                            max_x = max_x.max(pt.x);
                            min_y = min_y.min(pt.y);
                            max_y = max_y.max(pt.y);
                        }
                    }
                }
            }
            crate::core::Plot::Candlestick(candle) => {
                for c in &candle.candles {
                    min_x = min_x.min(c.x);
                    max_x = max_x.max(c.x);
                    min_y = min_y.min(c.low);
                    max_y = max_y.max(c.high);
                }
            }
            _ => {
                // Mark as fitted even if not a zoomable type
                commands.entity(entity).try_insert(AutoFitted);
                continue;
            }
        }

        // Skip if no valid bounds
        if !min_x.is_finite() || !max_x.is_finite() || !min_y.is_finite() || !max_y.is_finite() {
            commands.entity(entity).try_insert(AutoFitted);
            continue;
        }

        let data_width = (max_x - min_x).max(0.01);
        let data_height = (max_y - min_y).max(0.01);
        let data_center = Vec2::new((min_x + max_x) * 0.5, (min_y + max_y) * 0.5);

        // Compute scale to fit data in viewport with some padding
        let padding = 0.85; // Use 85% of available space
        let available_size = rect.world_size * padding;
        let scale_x = available_size.x / data_width;
        let scale_y = available_size.y / data_height;
        let fit_scale = scale_x.min(scale_y);

        // Set view to center on data with zoom limits
        view.scale = fit_scale;
        view.offset = -data_center * fit_scale;
        view.min_scale = fit_scale * 0.5; // Can zoom out to 50% of fit
        view.max_scale = fit_scale * 4.0; // Can zoom in to 4x of fit

        // Mark as fitted and dirty
        commands.entity(entity).try_insert(AutoFitted);
        registry.dirty.push_back(tile.id);
    }
}

/// Draw only dirty tiles
pub fn draw_dirty_tiles(
    mut commands: Commands,
    mut registry: ResMut<TileRegistry>,
    tiles: Query<(Entity, &PlotTile, &TileRect, &TileView)>,
    children_q: Query<&Children>,
    is_root_q: Query<(), With<TileRenderRoot>>,
    dash: Res<DashboardRes>,
    unit: Res<UnitMeshes>,
    mut meshes: ResMut<Assets<Mesh>>,
    mut materials: ResMut<Assets<ColorMaterial>>,
    mut std_materials: ResMut<Assets<StandardMaterial>>,
    mut scatter_points: ResMut<ScatterPoints3D>,
    mut axis_info_store: ResMut<AxisInfo3DStore>,
) {
    while let Some(id) = registry.dirty.pop_front() {
        // PlotId -> tile entity
        let Some(&tile_entity) = registry.by_plot.get(&id) else {
            continue;
        };

        // Pull current tile state
        let Ok((_e, tile, rect, view)) = tiles.get(tile_entity) else {
            continue;
        };

        // 1) Remove previous render root(s) under this tile (but keep the tile!)
        if let Ok(children) = children_q.get(tile_entity) {
            for child in children.iter() {
                if is_root_q.get(child).is_ok() {
                    // In Bevy 0.17, despawning an entity removes its descendants via relationships.
                    // Use try_despawn to avoid errors if entity was already despawned.
                    commands.entity(child).try_despawn();
                }
            }
        }

        // 2) Create a fresh render root under the tile
        let root = commands
            .spawn((TileRenderRoot, Transform::default(), Visibility::default()))
            .id();
        commands.entity(tile_entity).add_child(root);

        // 3) Draw based on plot type
        if let Some(plot) = dash.0.active_plots().get(tile.index) {
            let layer = RenderLayers::layer(tile.index % 32);
            match plot {
                crate::core::Plot::Graph2D(graph) => {
                    draw_plot_title(&mut commands, root, &graph.meta, rect, layer.clone());
                    draw_2d_plot(
                        &mut commands,
                        root,
                        graph,
                        rect,
                        view,
                        &unit,
                        &mut meshes,
                        &mut materials,
                        layer.clone(),
                    );
                    // Draw axis ticks with value labels
                    draw_axis_ticks(
                        &mut commands,
                        root,
                        rect,
                        view,
                        &unit,
                        &mut materials,
                        layer,
                    );
                }
                crate::core::Plot::Distribution(dist) => match dist {
                    crate::core::Distribution::Histogram {
                        meta,
                        values,
                        bins,
                        style,
                        x_label,
                        y_label,
                    } => {
                        draw_plot_title(&mut commands, root, meta, rect, layer.clone());
                        draw_histogram(
                            &mut commands,
                            root,
                            values,
                            *bins,
                            style,
                            x_label.as_deref(),
                            y_label.as_deref(),
                            rect,
                            view,
                            &unit,
                            &mut materials,
                            layer,
                        );
                    }
                    crate::core::Distribution::Pdf {
                        meta,
                        values,
                        style,
                        x_label,
                        y_label,
                    } => {
                        draw_plot_title(&mut commands, root, meta, rect, layer.clone());
                        draw_pdf(
                            &mut commands,
                            root,
                            values,
                            style,
                            x_label.as_deref(),
                            y_label.as_deref(),
                            rect,
                            view,
                            &unit,
                            &mut meshes,
                            &mut materials,
                            layer,
                        );
                    }
                    crate::core::Distribution::BoxPlot {
                        meta,
                        groups,
                        style,
                        x_label,
                        y_label,
                    } => {
                        draw_plot_title(&mut commands, root, meta, rect, layer.clone());
                        draw_boxplot(
                            &mut commands,
                            root,
                            groups,
                            style,
                            x_label.as_deref(),
                            y_label.as_deref(),
                            rect,
                            view,
                            &unit,
                            &mut materials,
                            layer,
                        );
                    }
                    crate::core::Distribution::ECDF {
                        meta,
                        values,
                        style,
                        x_label,
                        y_label,
                    } => {
                        draw_plot_title(&mut commands, root, meta, rect, layer.clone());
                        draw_ecdf(
                            &mut commands,
                            root,
                            values,
                            style,
                            x_label.as_deref(),
                            y_label.as_deref(),
                            rect,
                            view,
                            &unit,
                            &mut meshes,
                            &mut materials,
                            layer,
                        );
                    }
                },
                crate::core::Plot::Candlestick(candle) => {
                    draw_plot_title(&mut commands, root, &candle.meta, rect, layer.clone());
                    draw_candlestick(
                        &mut commands,
                        root,
                        candle,
                        rect,
                        view,
                        &unit,
                        &mut materials,
                        layer,
                    );
                }
                crate::core::Plot::Heatmap(heatmap) => {
                    draw_plot_title(&mut commands, root, &heatmap.meta, rect, layer.clone());
                    draw_heatmap(
                        &mut commands,
                        root,
                        heatmap,
                        rect,
                        view,
                        &unit,
                        &mut materials,
                        layer,
                    );
                }
                crate::core::Plot::Radial(radial) => {
                    // Extract meta from radial variant
                    let meta = match radial {
                        crate::core::Radial::Pie { meta, .. } => meta,
                        crate::core::Radial::Radar { meta, .. } => meta,
                    };
                    draw_plot_title(&mut commands, root, meta, rect, layer.clone());
                    draw_radial(
                        &mut commands,
                        root,
                        radial,
                        rect,
                        &unit,
                        &mut meshes,
                        &mut materials,
                        layer,
                    );
                }
                crate::core::Plot::Graph3D(graph) => {
                    // Use overlay layer for title/border (offset by 16)
                    let overlay_layer = RenderLayers::layer((tile.index + 16) % 32);
                    draw_3d_plot(
                        &mut commands,
                        root,
                        graph,
                        rect,
                        tile.index,
                        &unit,
                        &mut meshes,
                        &mut materials,
                        &mut std_materials,
                        layer,
                        overlay_layer,
                        &mut scatter_points,
                        &mut axis_info_store,
                    );
                }
                crate::core::Plot::Field(_) => {
                    draw_placeholder(&mut commands, root, rect, &unit, &mut materials, layer);
                }
            }
        }
    }
}

// Utility functions for grid layout
fn grid_dims(n: usize, aspect: f32, configured_cols: Option<usize>) -> (usize, usize) {
    // If columns are explicitly configured, use that
    if let Some(cols) = configured_cols {
        let cols = cols.max(1);
        let rows = n.div_ceil(cols);
        return (cols, rows);
    }

    // Auto layout based on count and aspect ratio
    match n {
        0 => (0, 0),
        1 => (1, 1),
        2 => {
            if aspect > 1.35 {
                (2, 1)
            } else {
                (1, 2)
            }
        }
        3 => {
            if aspect > 1.35 {
                (3, 1)
            } else {
                (2, 2)
            }
        }
        _ => {
            let cols = (n as f32).sqrt().ceil() as usize;
            let rows = n.div_ceil(cols);
            (cols, rows)
        }
    }
}

fn cleanup_tile(commands: &mut Commands, registry: &mut TileRegistry, entity: Entity, id: PlotId) {
    commands.entity(entity).despawn();
    registry.by_plot.remove(&id);
    if let Some(cam) = registry.camera_of.remove(&id) {
        commands.entity(cam).try_despawn();
    }
    if let Some(overlay) = registry.overlay_of.remove(&id) {
        commands.entity(overlay).try_despawn();
    }
}

/// Find nearest data point on any trace in the graph
fn find_nearest_point(cursor_data: Vec2, graph: &crate::core::Graph2D) -> Option<Vec2> {
    let mut nearest: Option<(Vec2, f32)> = None;

    for layer in &graph.layers {
        // For FillBetween geometry, skip - we want to snap to actual trace points
        if matches!(layer.geometry, crate::core::Geometry2D::FillBetween) {
            continue;
        }

        for &pt in &layer.xy {
            let dist_sq = (pt.x - cursor_data.x).powi(2) + (pt.y - cursor_data.y).powi(2);
            let should_update = match &nearest {
                Some((_, best_dist)) => dist_sq < *best_dist,
                None => true,
            };
            if should_update {
                nearest = Some((pt, dist_sq));
            }
        }
    }

    nearest.map(|(pt, _)| pt)
}

/// Update crosshair position and visibility - snaps to nearest data point
pub fn update_crosshair(
    mut commands: Commands,
    windows: Query<&Window, With<PrimaryWindow>>,
    tiles: Query<(&PlotTile, &TileRect, &TileView, Option<&View3D>)>,
    hovered: Res<HoveredTile>,
    dash: Res<DashboardRes>,
    mut cursor_pos: ResMut<CursorWorldPos>,
    crosshairs: Query<(Entity, &Crosshair)>,
    tooltips_3d: Query<(Entity, &Tooltip3D)>,
    scatter_points: Res<ScatterPoints3D>,
    unit: Res<UnitMeshes>,
    mut materials: ResMut<Assets<ColorMaterial>>,
) {
    let Ok(window) = windows.single() else {
        return;
    };

    // Despawn all existing crosshairs and 3D tooltips first (we recreate each frame)
    for (entity, _) in crosshairs.iter() {
        commands.entity(entity).try_despawn();
    }
    for (entity, _) in tooltips_3d.iter() {
        commands.entity(entity).try_despawn();
    }

    let Some(cursor_screen) = window.cursor_position() else {
        cursor_pos.position = None;
        cursor_pos.data_coords = None;
        cursor_pos.tile_index = None;
        return;
    };

    // Convert screen coords to world coords
    let world_x = cursor_screen.x - window.width() * 0.5;
    let world_y = window.height() * 0.5 - cursor_screen.y;
    let cursor_world = Vec2::new(world_x, world_y);

    cursor_pos.position = Some(cursor_world);
    cursor_pos.tile_index = hovered.0;

    let Some(hovered_index) = hovered.0 else {
        return;
    };

    for (tile, rect, view, view_3d) in tiles.iter() {
        if tile.index != hovered_index {
            continue;
        }

        // Get the graph data for this tile
        let Some(plot) = dash.0.active_plots().get(tile.index) else {
            continue;
        };

        match plot {
            crate::core::Plot::Graph3D(_) => {
                // Handle 3D scatter tooltip
                if let Some(view3d) = view_3d {
                    if let Some(points) = scatter_points.points.get(&tile.index) {
                        spawn_3d_tooltip(
                            &mut commands,
                            tile.index,
                            rect,
                            cursor_world,
                            points,
                            view3d,
                            &unit,
                            &mut materials,
                        );
                    }
                }
            }
            crate::core::Plot::Graph2D(graph) => {
                // Convert cursor to data coordinates
                let cursor_data = world_to_data(cursor_world, rect, view);

                // Find nearest data point
                let snap_data = find_nearest_point(cursor_data, graph).unwrap_or(cursor_data);
                let snap_world = data_to_world(snap_data, rect, view);

                cursor_pos.data_coords = Some(snap_data);

                // Spawn crosshair with dashed lines
                spawn_dashed_crosshair(
                    &mut commands,
                    tile.index,
                    rect,
                    snap_world,
                    snap_data,
                    &unit,
                    &mut materials,
                    RenderLayers::layer(tile.index % 32),
                );
            }
            crate::core::Plot::Distribution(dist) => {
                // Spawn distribution crosshair with tooltip
                spawn_distribution_crosshair(
                    &mut commands,
                    tile.index,
                    rect,
                    cursor_world,
                    dist,
                    &unit,
                    &mut materials,
                    RenderLayers::layer(tile.index % 32),
                );
            }
            crate::core::Plot::Candlestick(candle) => {
                spawn_candlestick_tooltip(
                    &mut commands,
                    tile.index,
                    rect,
                    view,
                    cursor_world,
                    candle,
                    &unit,
                    &mut materials,
                    RenderLayers::layer(tile.index % 32),
                );
            }
            crate::core::Plot::Heatmap(heatmap) => {
                spawn_heatmap_tooltip(
                    &mut commands,
                    tile.index,
                    rect,
                    cursor_world,
                    heatmap,
                    &unit,
                    &mut materials,
                    RenderLayers::layer(tile.index % 32),
                );
            }
            crate::core::Plot::Radial(radial) => {
                spawn_radial_tooltip(
                    &mut commands,
                    tile.index,
                    rect,
                    cursor_world,
                    radial,
                    &unit,
                    &mut materials,
                    RenderLayers::layer(tile.index % 32),
                );
            }
            _ => {}
        }
    }
}

fn spawn_dashed_crosshair(
    commands: &mut Commands,
    tile_index: usize,
    rect: &TileRect,
    snap_world: Vec2,
    snap_data: Vec2,
    unit: &UnitMeshes,
    materials: &mut Assets<ColorMaterial>,
    layers: RenderLayers,
) {
    let crosshair_mat = materials.add(ColorMaterial::from(Color::srgba(1.0, 1.0, 1.0, 0.5)));
    let line_thickness = 1.0; // Thin but visible
    let dash_length = 4.0;
    let gap_length = 3.0;

    commands
        .spawn((
            Crosshair { tile_index },
            Transform::default(),
            Visibility::Visible,
            InheritedVisibility::default(),
            ViewVisibility::default(),
        ))
        .with_children(|parent| {
            // Dashed vertical line
            let v_start = rect.world_center.y - rect.world_size.y * 0.5;
            let v_end = rect.world_center.y + rect.world_size.y * 0.5;
            let mut y = v_start;
            while y < v_end {
                let dash_end = (y + dash_length).min(v_end);
                let dash_center_y = (y + dash_end) / 2.0;
                let dash_height = dash_end - y;

                parent.spawn((
                    Mesh2d(unit.quad.clone()),
                    MeshMaterial2d(crosshair_mat.clone()),
                    Transform {
                        translation: Vec3::new(snap_world.x, dash_center_y, 5.0),
                        scale: Vec3::new(line_thickness, dash_height, 1.0),
                        ..default()
                    },
                    CrosshairVLine,
                    layers.clone(),
                ));

                y += dash_length + gap_length;
            }

            // Dashed horizontal line
            let h_start = rect.world_center.x - rect.world_size.x * 0.5;
            let h_end = rect.world_center.x + rect.world_size.x * 0.5;
            let mut x = h_start;
            while x < h_end {
                let dash_end = (x + dash_length).min(h_end);
                let dash_center_x = (x + dash_end) / 2.0;
                let dash_width = dash_end - x;

                parent.spawn((
                    Mesh2d(unit.quad.clone()),
                    MeshMaterial2d(crosshair_mat.clone()),
                    Transform {
                        translation: Vec3::new(dash_center_x, snap_world.y, 5.0),
                        scale: Vec3::new(dash_width, line_thickness, 1.0),
                        ..default()
                    },
                    CrosshairHLine,
                    layers.clone(),
                ));

                x += dash_length + gap_length;
            }

            // Small circle marker at intersection (snap point)
            let point_mat = materials.add(ColorMaterial::from(Color::srgba(1.0, 1.0, 1.0, 0.95)));
            parent.spawn((
                Mesh2d(unit.quad.clone()),
                MeshMaterial2d(point_mat),
                Transform {
                    translation: Vec3::new(snap_world.x, snap_world.y, 5.5),
                    scale: Vec3::splat(5.0),
                    ..default()
                },
                layers.clone(),
            ));

            // Coordinate text near the snapped point (offset to avoid overlap)
            parent.spawn((
                Text2d::new(format!("({:.2}, {:.2})", snap_data.x, snap_data.y)),
                TextFont {
                    font_size: 12.0,
                    ..default()
                },
                TextColor(Color::srgba(1.0, 1.0, 1.0, 0.9)),
                Transform::from_translation(Vec3::new(
                    snap_world.x + 10.0,
                    snap_world.y + 12.0,
                    6.0,
                )),
                CrosshairCoordText,
                layers,
            ));
        });
}

fn spawn_distribution_crosshair(
    commands: &mut Commands,
    tile_index: usize,
    rect: &TileRect,
    cursor_world: Vec2,
    dist: &crate::core::Distribution,
    unit: &UnitMeshes,
    materials: &mut Assets<ColorMaterial>,
    layers: RenderLayers,
) {
    // Calculate padded area (must match draw_histogram/draw_pdf)
    let padding_left = 0.15;
    let padding_right = 0.08;
    let padding_bottom = 0.18;
    let padding_top = 0.08;

    let usable_width = rect.world_size.x * (1.0 - padding_left - padding_right);
    let usable_height = rect.world_size.y * (1.0 - padding_bottom - padding_top);
    let left_x = rect.world_center.x - rect.world_size.x * 0.5 + rect.world_size.x * padding_left;
    let bottom_y =
        rect.world_center.y - rect.world_size.y * 0.5 + rect.world_size.y * padding_bottom;
    let right_x = left_x + usable_width;
    let top_y = bottom_y + usable_height;

    // Check if cursor is within the plot area
    if cursor_world.x < left_x
        || cursor_world.x > right_x
        || cursor_world.y < bottom_y
        || cursor_world.y > top_y
    {
        return;
    }

    let crosshair_mat = materials.add(ColorMaterial::from(Color::srgba(1.0, 1.0, 1.0, 0.5)));
    let highlight_mat = materials.add(ColorMaterial::from(Color::srgba(1.0, 1.0, 1.0, 0.3)));
    let line_thickness = 1.0;
    let dash_length = 4.0;
    let gap_length = 3.0;

    match dist {
        crate::core::Distribution::Histogram { values, bins, .. } => {
            // Histogram: just bar highlight + tooltip, no crosshair lines
            if values.is_empty() || *bins == 0 {
                return;
            }

            let min_val = values.iter().cloned().fold(f32::INFINITY, f32::min);
            let max_val = values.iter().cloned().fold(f32::NEG_INFINITY, f32::max);
            if !min_val.is_finite() || !max_val.is_finite() || min_val >= max_val {
                return;
            }

            let bin_width_data = (max_val - min_val) / *bins as f32;
            let bar_width_world = usable_width / *bins as f32;

            // Which bar is the cursor over?
            let bar_index = ((cursor_world.x - left_x) / bar_width_world).floor() as usize;
            let bar_index = bar_index.min(*bins - 1);

            // Count values in this bin
            let bin_start = min_val + bar_index as f32 * bin_width_data;
            let bin_end = bin_start + bin_width_data;
            let count = values
                .iter()
                .filter(|&&v| {
                    if bar_index == *bins - 1 {
                        v >= bin_start && v <= bin_end
                    } else {
                        v >= bin_start && v < bin_end
                    }
                })
                .count();

            // Calculate bar geometry for highlight
            let bar_center_x = left_x + (bar_index as f32 + 0.5) * bar_width_world;
            let max_count = {
                let mut counts = vec![0usize; *bins];
                for &v in values {
                    let idx = ((v - min_val) / bin_width_data).floor() as usize;
                    let idx = idx.min(*bins - 1);
                    counts[idx] += 1;
                }
                counts.iter().cloned().max().unwrap_or(1) as f32
            };
            let bar_height = (count as f32 / max_count) * usable_height;

            let tooltip_text = format!("[{:.2}, {:.2})\nCount: {}", bin_start, bin_end, count);

            commands
                .spawn((
                    Crosshair { tile_index },
                    Transform::default(),
                    Visibility::Visible,
                    InheritedVisibility::default(),
                    ViewVisibility::default(),
                ))
                .with_children(|parent| {
                    // Highlight bar overlay
                    parent.spawn((
                        Mesh2d(unit.quad.clone()),
                        MeshMaterial2d(highlight_mat),
                        Transform {
                            translation: Vec3::new(bar_center_x, bottom_y + bar_height * 0.5, 4.0),
                            scale: Vec3::new(bar_width_world * 0.9, bar_height, 1.0),
                            ..default()
                        },
                        layers.clone(),
                    ));

                    // Tooltip text
                    parent.spawn((
                        Text2d::new(tooltip_text),
                        TextFont {
                            font_size: 11.0,
                            ..default()
                        },
                        TextColor(Color::srgba(1.0, 1.0, 1.0, 0.95)),
                        Transform::from_translation(Vec3::new(
                            bar_center_x + 15.0,
                            bottom_y + bar_height + 20.0,
                            6.0,
                        )),
                        CrosshairCoordText,
                        layers,
                    ));
                });
        }
        crate::core::Distribution::Pdf { values, .. } => {
            // PDF: crosshair snapped to curve
            if values.is_empty() {
                return;
            }

            let min_val = values.iter().cloned().fold(f32::INFINITY, f32::min);
            let max_val = values.iter().cloned().fold(f32::NEG_INFINITY, f32::max);
            if !min_val.is_finite() || !max_val.is_finite() || min_val >= max_val {
                return;
            }

            // Convert cursor x to data value
            let t = (cursor_world.x - left_x) / usable_width;
            let range = max_val - min_val;
            let x_min = min_val - range * 0.1;
            let x_max = max_val + range * 0.1;
            let data_x = x_min + t * (x_max - x_min);

            // Compute KDE at this point
            let n = values.len() as f32;
            let std_dev = {
                let mean = values.iter().sum::<f32>() / n;
                let variance = values.iter().map(|x| (x - mean).powi(2)).sum::<f32>() / n;
                variance.sqrt()
            };
            let bandwidth = (1.06 * std_dev * n.powf(-0.2)).max(0.01);

            let density: f32 = values
                .iter()
                .map(|&xi| {
                    let u = (data_x - xi) / bandwidth;
                    (-0.5 * u * u).exp() / (2.506628 * bandwidth)
                })
                .sum::<f32>()
                / n;

            // Compute max density for normalization
            let max_density = {
                let mut max_d = 0.0f32;
                for i in 0..100 {
                    let x = x_min + (i as f32 / 99.0) * (x_max - x_min);
                    let d: f32 = values
                        .iter()
                        .map(|&xi| {
                            let u = (x - xi) / bandwidth;
                            (-0.5 * u * u).exp() / (2.506628 * bandwidth)
                        })
                        .sum::<f32>()
                        / n;
                    max_d = max_d.max(d);
                }
                max_d
            };

            // Snap to curve - y position on the PDF curve
            let snap_y = bottom_y + (density / max_density) * usable_height;
            let snap_world = Vec2::new(cursor_world.x, snap_y);

            let tooltip_text = format!("Value: {:.2}\nDensity: {:.4}", data_x, density);

            commands
                .spawn((
                    Crosshair { tile_index },
                    Transform::default(),
                    Visibility::Visible,
                    InheritedVisibility::default(),
                    ViewVisibility::default(),
                ))
                .with_children(|parent| {
                    // Dashed vertical line
                    let mut y = bottom_y;
                    while y < snap_y {
                        let dash_end = (y + dash_length).min(snap_y);
                        let dash_center_y = (y + dash_end) / 2.0;
                        let dash_height = dash_end - y;

                        parent.spawn((
                            Mesh2d(unit.quad.clone()),
                            MeshMaterial2d(crosshair_mat.clone()),
                            Transform {
                                translation: Vec3::new(cursor_world.x, dash_center_y, 5.0),
                                scale: Vec3::new(line_thickness, dash_height, 1.0),
                                ..default()
                            },
                            CrosshairVLine,
                            layers.clone(),
                        ));

                        y += dash_length + gap_length;
                    }

                    // Dashed horizontal line to left edge
                    let mut x = left_x;
                    while x < cursor_world.x {
                        let dash_end = (x + dash_length).min(cursor_world.x);
                        let dash_center_x = (x + dash_end) / 2.0;
                        let dash_width = dash_end - x;

                        parent.spawn((
                            Mesh2d(unit.quad.clone()),
                            MeshMaterial2d(crosshair_mat.clone()),
                            Transform {
                                translation: Vec3::new(dash_center_x, snap_y, 5.0),
                                scale: Vec3::new(dash_width, line_thickness, 1.0),
                                ..default()
                            },
                            CrosshairHLine,
                            layers.clone(),
                        ));

                        x += dash_length + gap_length;
                    }

                    // Point marker at curve intersection
                    let point_mat =
                        materials.add(ColorMaterial::from(Color::srgba(1.0, 1.0, 1.0, 0.95)));
                    parent.spawn((
                        Mesh2d(unit.quad.clone()),
                        MeshMaterial2d(point_mat),
                        Transform {
                            translation: Vec3::new(snap_world.x, snap_world.y, 5.5),
                            scale: Vec3::splat(6.0),
                            ..default()
                        },
                        layers.clone(),
                    ));

                    // Tooltip text
                    parent.spawn((
                        Text2d::new(tooltip_text),
                        TextFont {
                            font_size: 11.0,
                            ..default()
                        },
                        TextColor(Color::srgba(1.0, 1.0, 1.0, 0.95)),
                        Transform::from_translation(Vec3::new(
                            snap_world.x + 15.0,
                            snap_world.y + 15.0,
                            6.0,
                        )),
                        CrosshairCoordText,
                        layers,
                    ));
                });
        }
        crate::core::Distribution::BoxPlot { groups, .. } => {
            // BoxPlot: highlight hovered box and show stats tooltip
            if groups.is_empty() {
                return;
            }

            let padding_left = 0.12;
            let padding_right = 0.05;
            let padding_bottom = 0.18;
            let padding_top = 0.08;

            let usable_width = rect.world_size.x * (1.0 - padding_left - padding_right);
            let usable_height = rect.world_size.y * (1.0 - padding_bottom - padding_top);
            let left_x =
                rect.world_center.x - rect.world_size.x * 0.5 + rect.world_size.x * padding_left;
            let bottom_y =
                rect.world_center.y - rect.world_size.y * 0.5 + rect.world_size.y * padding_bottom;
            let right_x = left_x + usable_width;
            let top_y = bottom_y + usable_height;

            if cursor_world.x < left_x
                || cursor_world.x > right_x
                || cursor_world.y < bottom_y
                || cursor_world.y > top_y
            {
                return;
            }

            let n_groups = groups.len();
            let group_width = usable_width / n_groups as f32;
            let box_index = ((cursor_world.x - left_x) / group_width).floor() as usize;
            let box_index = box_index.min(n_groups - 1);

            let (name, values) = &groups[box_index];

            // Compute stats
            if values.is_empty() {
                return;
            }
            let mut sorted: Vec<f32> = values.iter().cloned().filter(|x| x.is_finite()).collect();
            if sorted.is_empty() {
                return;
            }
            sorted.sort_by(|a, b| a.partial_cmp(b).unwrap());
            let n = sorted.len();
            let median = if n % 2 == 0 {
                (sorted[n / 2 - 1] + sorted[n / 2]) / 2.0
            } else {
                sorted[n / 2]
            };
            let q1 = sorted[n / 4];
            let q3 = sorted[(3 * n / 4).min(n - 1)];
            let min_val = sorted[0];
            let max_val = sorted[n - 1];

            let box_center_x = left_x + (box_index as f32 + 0.5) * group_width;
            let box_width = group_width * 0.6;

            // Highlight the box
            let highlight_mat =
                materials.add(ColorMaterial::from(Color::srgba(1.0, 1.0, 1.0, 0.15)));
            commands
                .spawn((
                    Crosshair { tile_index },
                    Transform::default(),
                    Visibility::Visible,
                    layers.clone(),
                ))
                .with_children(|parent| {
                    parent.spawn((
                        Mesh2d(unit.quad.clone()),
                        MeshMaterial2d(highlight_mat),
                        Transform {
                            translation: Vec3::new(box_center_x, (bottom_y + top_y) * 0.5, 4.5),
                            scale: Vec3::new(box_width + 10.0, usable_height, 1.0),
                            ..default()
                        },
                        layers.clone(),
                    ));

                    // Tooltip
                    let tooltip = format!(
                        "{}\nMedian: {:.2}\nQ1: {:.2}  Q3: {:.2}\nMin: {:.2}  Max: {:.2}",
                        name, median, q1, q3, min_val, max_val
                    );
                    parent.spawn((
                        Text2d::new(tooltip),
                        TextFont {
                            font_size: 10.0,
                            ..default()
                        },
                        TextColor(Color::srgba(1.0, 1.0, 1.0, 0.95)),
                        Transform::from_translation(Vec3::new(
                            cursor_world.x + 15.0,
                            cursor_world.y + 25.0,
                            6.0,
                        )),
                        CrosshairCoordText,
                        layers,
                    ));
                });
        }
        crate::core::Distribution::ECDF { values, .. } => {
            // ECDF: show F(x) value at cursor position
            if values.is_empty() {
                return;
            }

            let mut sorted: Vec<f32> = values.iter().cloned().filter(|x| x.is_finite()).collect();
            if sorted.is_empty() {
                return;
            }
            sorted.sort_by(|a, b| a.partial_cmp(b).unwrap());

            let min_val = sorted[0];
            let max_val = sorted[sorted.len() - 1];
            if min_val >= max_val {
                return;
            }

            // Convert cursor x to data value
            let range = max_val - min_val;
            let x_min = min_val - range * 0.05;
            let x_max = max_val + range * 0.05;

            let t = (cursor_world.x - left_x) / usable_width;
            let data_x = x_min + t * (x_max - x_min);

            // Compute ECDF value at this point
            let count = sorted.iter().filter(|&&v| v <= data_x).count();
            let ecdf_y = count as f32 / sorted.len() as f32;

            // Snap to curve
            let snap_world_y = bottom_y + ecdf_y * usable_height;

            let tooltip_text = format!("x: {:.2}\nF(x): {:.3}", data_x, ecdf_y);

            commands
                .spawn((
                    Crosshair { tile_index },
                    Transform::default(),
                    Visibility::Visible,
                    InheritedVisibility::default(),
                    ViewVisibility::default(),
                ))
                .with_children(|parent| {
                    // Dashed vertical line
                    let mut y = bottom_y;
                    while y < snap_world_y {
                        let dash_end = (y + dash_length).min(snap_world_y);
                        let dash_center_y = (y + dash_end) / 2.0;
                        let dash_height = dash_end - y;

                        parent.spawn((
                            Mesh2d(unit.quad.clone()),
                            MeshMaterial2d(crosshair_mat.clone()),
                            Transform {
                                translation: Vec3::new(cursor_world.x, dash_center_y, 5.0),
                                scale: Vec3::new(line_thickness, dash_height, 1.0),
                                ..default()
                            },
                            CrosshairVLine,
                            layers.clone(),
                        ));

                        y += dash_length + gap_length;
                    }

                    // Point marker
                    let point_mat =
                        materials.add(ColorMaterial::from(Color::srgba(1.0, 1.0, 1.0, 0.95)));
                    parent.spawn((
                        Mesh2d(unit.quad.clone()),
                        MeshMaterial2d(point_mat),
                        Transform {
                            translation: Vec3::new(cursor_world.x, snap_world_y, 5.5),
                            scale: Vec3::splat(6.0),
                            ..default()
                        },
                        layers.clone(),
                    ));

                    // Tooltip text
                    parent.spawn((
                        Text2d::new(tooltip_text),
                        TextFont {
                            font_size: 11.0,
                            ..default()
                        },
                        TextColor(Color::srgba(1.0, 1.0, 1.0, 0.95)),
                        Transform::from_translation(Vec3::new(
                            cursor_world.x + 15.0,
                            snap_world_y + 15.0,
                            6.0,
                        )),
                        CrosshairCoordText,
                        layers,
                    ));
                });
        }
    }
}

fn spawn_candlestick_tooltip(
    commands: &mut Commands,
    tile_index: usize,
    rect: &TileRect,
    view: &TileView,
    cursor_world: Vec2,
    candle: &crate::core::Candlestick,
    unit: &UnitMeshes,
    materials: &mut Assets<ColorMaterial>,
    layers: RenderLayers,
) {
    if candle.candles.is_empty() {
        return;
    }

    // Convert cursor to data coordinates using view transformation
    let cursor_data = world_to_data(cursor_world, rect, view);

    // Calculate candle width in data units (same as draw_candlestick)
    let n_candles = candle.candles.len();
    let x_min = candle
        .candles
        .iter()
        .map(|c| c.x)
        .fold(f32::INFINITY, f32::min);
    let x_max = candle
        .candles
        .iter()
        .map(|c| c.x)
        .fold(f32::NEG_INFINITY, f32::max);
    let x_range = (x_max - x_min).max(1.0);
    let candle_data_width = x_range / (n_candles as f32 * 1.5);

    // Find nearest candle in data space
    let mut nearest_idx = 0;
    let mut nearest_dist = f32::INFINITY;
    for (i, c) in candle.candles.iter().enumerate() {
        let dist = (c.x - cursor_data.x).abs();
        if dist < nearest_dist {
            nearest_dist = dist;
            nearest_idx = i;
        }
    }

    let c = &candle.candles[nearest_idx];

    // Check if cursor is close enough to this candle (in data space)
    if (cursor_data.x - c.x).abs() > candle_data_width * 0.8 {
        return;
    }

    // Transform candle position to world coordinates using view
    let candle_world = data_to_world(Vec2::new(c.x, (c.high + c.low) * 0.5), rect, view);
    let candle_high_world = data_to_world(Vec2::new(c.x, c.high), rect, view);
    let candle_low_world = data_to_world(Vec2::new(c.x, c.low), rect, view);

    // Candle width in world space
    let candle_world_width = candle_data_width * view.scale;

    let is_up = c.close >= c.open;
    let highlight_color = if is_up {
        Color::srgba(0.3, 1.0, 0.4, 0.3)
    } else {
        Color::srgba(1.0, 0.3, 0.3, 0.3)
    };
    let highlight_mat = materials.add(ColorMaterial::from(highlight_color));

    commands
        .spawn((
            Crosshair { tile_index },
            Transform::default(),
            Visibility::Visible,
            layers.clone(),
        ))
        .with_children(|parent| {
            // Highlight column
            let wick_height = (candle_high_world.y - candle_low_world.y).abs();
            parent.spawn((
                Mesh2d(unit.quad.clone()),
                MeshMaterial2d(highlight_mat),
                Transform {
                    translation: Vec3::new(candle_world.x, candle_world.y, 4.5),
                    scale: Vec3::new(candle_world_width + 4.0, wick_height + 4.0, 1.0),
                    ..default()
                },
                layers.clone(),
            ));

            // Tooltip
            let change = c.close - c.open;
            let change_pct = if c.open != 0.0 {
                (change / c.open) * 100.0
            } else {
                0.0
            };
            let change_str = if change >= 0.0 {
                format!("+{:.2} (+{:.1}%)", change, change_pct)
            } else {
                format!("{:.2} ({:.1}%)", change, change_pct)
            };

            let tooltip = format!(
                "O: {:.2}  H: {:.2}\nL: {:.2}  C: {:.2}\n{}",
                c.open, c.high, c.low, c.close, change_str
            );

            parent.spawn((
                Text2d::new(tooltip),
                TextFont {
                    font_size: 10.0,
                    ..default()
                },
                TextColor(Color::srgba(1.0, 1.0, 1.0, 0.95)),
                Transform::from_translation(Vec3::new(
                    cursor_world.x + 15.0,
                    cursor_world.y + 20.0,
                    6.0,
                )),
                CrosshairCoordText,
                layers,
            ));
        });
}

fn spawn_heatmap_tooltip(
    commands: &mut Commands,
    tile_index: usize,
    rect: &TileRect,
    cursor_world: Vec2,
    heatmap: &crate::core::Heatmap,
    unit: &UnitMeshes,
    materials: &mut Assets<ColorMaterial>,
    layers: RenderLayers,
) {
    let rows = heatmap.dims.y as usize;
    let cols = heatmap.dims.x as usize;

    if rows == 0 || cols == 0 || heatmap.values.is_empty() {
        return;
    }

    // Match padding from draw_heatmap exactly
    let has_row_labels = heatmap.row_labels.is_some();
    let has_col_labels = heatmap.col_labels.is_some();

    let padding_left = if has_row_labels { 0.12 } else { 0.06 };
    let padding_right = 0.06;
    let padding_bottom = if has_col_labels { 0.12 } else { 0.06 };
    let padding_top = 0.06;

    let usable_width = rect.world_size.x * (1.0 - padding_left - padding_right);
    let usable_height = rect.world_size.y * (1.0 - padding_bottom - padding_top);
    let left_x = rect.world_center.x - rect.world_size.x * 0.5 + rect.world_size.x * padding_left;
    let bottom_y =
        rect.world_center.y - rect.world_size.y * 0.5 + rect.world_size.y * padding_bottom;
    let right_x = left_x + usable_width;
    let top_y = bottom_y + usable_height;

    if cursor_world.x < left_x
        || cursor_world.x > right_x
        || cursor_world.y < bottom_y
        || cursor_world.y > top_y
    {
        return;
    }

    let cell_width = usable_width / cols as f32;
    let cell_height = usable_height / rows as f32;

    // Find which cell
    let col = ((cursor_world.x - left_x) / cell_width).floor() as usize;
    let col = col.min(cols - 1);

    // Flip y (row 0 is at top)
    let row_from_bottom = ((cursor_world.y - bottom_y) / cell_height).floor() as usize;
    let row = rows.saturating_sub(1).saturating_sub(row_from_bottom);
    let row = row.min(rows - 1);

    let idx = row * cols + col;
    if idx >= heatmap.values.len() {
        return;
    }

    let value = heatmap.values[idx];

    // Cell center
    let cell_x = left_x + (col as f32 + 0.5) * cell_width;
    let cell_y = bottom_y + usable_height - (row as f32 + 0.5) * cell_height;

    // Highlight color based on value
    let vmin = heatmap
        .vmin
        .unwrap_or_else(|| heatmap.values.iter().cloned().fold(f32::INFINITY, f32::min));
    let vmax = heatmap.vmax.unwrap_or_else(|| {
        heatmap
            .values
            .iter()
            .cloned()
            .fold(f32::NEG_INFINITY, f32::max)
    });
    let _t = ((value - vmin) / (vmax - vmin).max(0.001)).clamp(0.0, 1.0);

    let highlight_mat = materials.add(ColorMaterial::from(Color::srgba(1.0, 1.0, 1.0, 0.25)));

    commands
        .spawn((
            Crosshair { tile_index },
            Transform::default(),
            Visibility::Visible,
            layers.clone(),
        ))
        .with_children(|parent| {
            // Highlight cell border
            parent.spawn((
                Mesh2d(unit.quad.clone()),
                MeshMaterial2d(highlight_mat),
                Transform {
                    translation: Vec3::new(cell_x, cell_y, 4.5),
                    scale: Vec3::new(cell_width + 2.0, cell_height + 2.0, 1.0),
                    ..default()
                },
                layers.clone(),
            ));

            // Build tooltip
            let default_row = format!("Row {}", row);
            let default_col = format!("Col {}", col);
            let row_label = heatmap
                .row_labels
                .as_ref()
                .and_then(|l| l.get(row))
                .map(|s| s.as_str())
                .unwrap_or(&default_row);
            let col_label = heatmap
                .col_labels
                .as_ref()
                .and_then(|l| l.get(col))
                .map(|s| s.as_str())
                .unwrap_or(&default_col);

            let tooltip = format!("{} x {}\nValue: {:.3}", row_label, col_label, value);

            parent.spawn((
                Text2d::new(tooltip),
                TextFont {
                    font_size: 10.0,
                    ..default()
                },
                TextColor(Color::srgba(1.0, 1.0, 1.0, 0.95)),
                Transform::from_translation(Vec3::new(
                    cursor_world.x + 15.0,
                    cursor_world.y + 15.0,
                    6.0,
                )),
                CrosshairCoordText,
                layers,
            ));
        });
}

fn spawn_radial_tooltip(
    commands: &mut Commands,
    tile_index: usize,
    rect: &TileRect,
    cursor_world: Vec2,
    radial: &crate::core::Radial,
    unit: &UnitMeshes,
    materials: &mut Assets<ColorMaterial>,
    layers: RenderLayers,
) {
    let center = rect.world_center;
    let radius = (rect.world_size.x.min(rect.world_size.y) * 0.35).max(10.0);

    // Distance from center
    let dx = cursor_world.x - center.x;
    let dy = cursor_world.y - center.y;
    let dist = (dx * dx + dy * dy).sqrt();

    // Only show tooltip when cursor is near the chart
    if dist > radius * 1.5 {
        return;
    }

    match radial {
        crate::core::Radial::Pie { slices, .. } => {
            if slices.is_empty() {
                return;
            }

            let total: f32 = slices.iter().map(|(_, v)| v.max(0.0)).sum();
            if total <= 0.0 {
                return;
            }

            // Find which slice the cursor is in
            let angle = dy.atan2(dx);
            // Normalize angle to start from top (-PI/2) going clockwise
            let mut normalized = angle + std::f32::consts::FRAC_PI_2;
            if normalized < 0.0 {
                normalized += std::f32::consts::TAU;
            }

            let mut cumulative = 0.0;
            let mut found_slice: Option<(usize, &str, f32, f32)> = None;

            for (i, (label, value)) in slices.iter().enumerate() {
                if *value <= 0.0 {
                    continue;
                }
                let sweep = (*value / total) * std::f32::consts::TAU;
                if normalized >= cumulative && normalized < cumulative + sweep {
                    let pct = (*value / total) * 100.0;
                    found_slice = Some((i, label, *value, pct));
                    break;
                }
                cumulative += sweep;
            }

            if let Some((_idx, label, value, pct)) = found_slice {
                let tooltip = format!("{}\nValue: {:.1}\n{:.1}%", label, value, pct);

                let highlight_mat =
                    materials.add(ColorMaterial::from(Color::srgba(1.0, 1.0, 1.0, 0.3)));

                commands
                    .spawn((
                        Crosshair { tile_index },
                        Transform::default(),
                        Visibility::Visible,
                        InheritedVisibility::default(),
                        ViewVisibility::default(),
                    ))
                    .with_children(|parent| {
                        // Small highlight at cursor
                        parent.spawn((
                            Mesh2d(unit.quad.clone()),
                            MeshMaterial2d(highlight_mat),
                            Transform {
                                translation: Vec3::new(cursor_world.x, cursor_world.y, 4.5),
                                scale: Vec3::splat(8.0),
                                ..default()
                            },
                            layers.clone(),
                        ));

                        // Tooltip
                        parent.spawn((
                            Text2d::new(tooltip),
                            TextFont {
                                font_size: 11.0,
                                ..default()
                            },
                            TextColor(Color::srgba(1.0, 1.0, 1.0, 0.95)),
                            Transform::from_translation(Vec3::new(
                                cursor_world.x + 15.0,
                                cursor_world.y + 15.0,
                                6.0,
                            )),
                            CrosshairCoordText,
                            layers,
                        ));
                    });
            }
        }
        crate::core::Radial::Radar { axes, values, .. } => {
            if axes.is_empty() || values.is_empty() {
                return;
            }

            let n = axes.len().min(values.len());
            if n < 3 {
                return;
            }

            // Find which axis the cursor is closest to
            let angle = dy.atan2(dx);
            let angle_step = std::f32::consts::TAU / n as f32;

            let mut best_idx = 0;
            let mut best_diff = f32::MAX;

            for i in 0..n {
                let axis_angle = -std::f32::consts::FRAC_PI_2 + i as f32 * angle_step;
                let mut diff = (angle - axis_angle).abs();
                if diff > std::f32::consts::PI {
                    diff = std::f32::consts::TAU - diff;
                }
                if diff < best_diff {
                    best_diff = diff;
                    best_idx = i;
                }
            }

            let axis_name = &axes[best_idx];
            let axis_value = values[best_idx];

            let tooltip = format!("{}\nValue: {:.2}", axis_name, axis_value);

            let highlight_mat =
                materials.add(ColorMaterial::from(Color::srgba(1.0, 1.0, 1.0, 0.3)));

            // Calculate point position on the radar
            let axis_angle = -std::f32::consts::FRAC_PI_2 + best_idx as f32 * angle_step;
            let r = radius * axis_value.clamp(0.0, 1.0);
            let point_x = center.x + r * axis_angle.cos();
            let point_y = center.y + r * axis_angle.sin();

            commands
                .spawn((
                    Crosshair { tile_index },
                    Transform::default(),
                    Visibility::Visible,
                    InheritedVisibility::default(),
                    ViewVisibility::default(),
                ))
                .with_children(|parent| {
                    // Highlight the data point
                    parent.spawn((
                        Mesh2d(unit.quad.clone()),
                        MeshMaterial2d(highlight_mat),
                        Transform {
                            translation: Vec3::new(point_x, point_y, 4.5),
                            scale: Vec3::splat(12.0),
                            ..default()
                        },
                        layers.clone(),
                    ));

                    // Tooltip
                    parent.spawn((
                        Text2d::new(tooltip),
                        TextFont {
                            font_size: 11.0,
                            ..default()
                        },
                        TextColor(Color::srgba(1.0, 1.0, 1.0, 0.95)),
                        Transform::from_translation(Vec3::new(
                            cursor_world.x + 15.0,
                            cursor_world.y + 15.0,
                            6.0,
                        )),
                        CrosshairCoordText,
                        layers,
                    ));
                });
        }
    }
}

/// Spawn 3D scatter point tooltip
fn spawn_3d_tooltip(
    commands: &mut Commands,
    tile_index: usize,
    rect: &TileRect,
    cursor_world: Vec2,
    points: &[(Vec3, Vec3)], // (original, normalized)
    view: &View3D,
    unit: &UnitMeshes,
    materials: &mut Assets<ColorMaterial>,
) {
    // Project 3D points to 2D screen space and find nearest to cursor
    // We use a simplified projection based on the View3D camera

    // Get camera transform
    let cam_transform = compute_orbit_transform(view);
    let cam_pos = cam_transform.translation;
    let cam_forward = cam_transform.forward();
    let cam_right = cam_transform.right();
    let cam_up = cam_transform.up();

    // Viewport dimensions (use tile size as approximate)
    let vp_width = rect.world_size.x;
    let vp_height = rect.world_size.y;
    let fov = std::f32::consts::FRAC_PI_4; // 45 degrees
    let aspect = vp_width / vp_height;

    // Convert cursor from world to viewport-relative coords
    let cursor_vp = Vec2::new(
        (cursor_world.x - rect.world_center.x) / vp_width + 0.5,
        (cursor_world.y - rect.world_center.y) / vp_height + 0.5,
    );

    // If cursor is outside viewport, skip
    if cursor_vp.x < 0.0 || cursor_vp.x > 1.0 || cursor_vp.y < 0.0 || cursor_vp.y > 1.0 {
        return;
    }

    // Find nearest point by projecting to screen
    let mut nearest: Option<(Vec3, Vec2, f32)> = None; // (original_coords, screen_pos, dist_sq)
    let tan_half_fov = (fov * 0.5).tan();

    for &(original, normalized) in points {
        // Vector from camera to point
        let to_point = normalized - cam_pos;
        let depth = to_point.dot(*cam_forward);

        if depth <= 0.1 {
            continue; // Point is behind camera
        }

        // Project to normalized device coords
        let right_dist = to_point.dot(*cam_right);
        let up_dist = to_point.dot(*cam_up);

        let ndc_x = right_dist / (depth * tan_half_fov * aspect);
        let ndc_y = up_dist / (depth * tan_half_fov);

        // Convert to viewport coords [0, 1]
        let screen_x = (ndc_x + 1.0) * 0.5;
        let screen_y = (ndc_y + 1.0) * 0.5;

        // Distance from cursor
        let dx = screen_x - cursor_vp.x;
        let dy = screen_y - cursor_vp.y;
        let dist_sq = dx * dx + dy * dy;

        let should_update = match &nearest {
            Some((_, _, best_dist)) => dist_sq < *best_dist,
            None => true,
        };
        if should_update {
            let world_screen = Vec2::new(
                rect.world_center.x + (screen_x - 0.5) * vp_width,
                rect.world_center.y + (screen_y - 0.5) * vp_height,
            );
            nearest = Some((original, world_screen, dist_sq));
        }
    }

    // Check if nearest point is close enough (within ~5% of viewport)
    let threshold = 0.05 * 0.05; // 5% squared
    if let Some((original, screen_pos, dist_sq)) = nearest {
        if dist_sq < threshold {
            // Use overlay layer for tooltip
            let overlay_layer = RenderLayers::layer((tile_index + 16) % 32);
            let tooltip_text = format!("({:.2}, {:.2}, {:.2})", original.x, original.y, original.z);

            let highlight_mat =
                materials.add(ColorMaterial::from(Color::srgba(1.0, 1.0, 1.0, 0.6)));

            commands
                .spawn((
                    Tooltip3D { tile_index },
                    Transform::default(),
                    Visibility::Visible,
                    InheritedVisibility::default(),
                    ViewVisibility::default(),
                ))
                .with_children(|parent| {
                    // Point marker at projected location
                    parent.spawn((
                        Mesh2d(unit.quad.clone()),
                        MeshMaterial2d(highlight_mat),
                        Transform {
                            translation: Vec3::new(screen_pos.x, screen_pos.y, 5.0),
                            scale: Vec3::splat(8.0),
                            ..default()
                        },
                        overlay_layer.clone(),
                    ));

                    // Tooltip text
                    parent.spawn((
                        Text2d::new(tooltip_text),
                        TextFont {
                            font_size: 11.0,
                            ..default()
                        },
                        TextColor(Color::srgba(1.0, 1.0, 1.0, 0.95)),
                        Transform::from_translation(Vec3::new(
                            screen_pos.x + 15.0,
                            screen_pos.y + 15.0,
                            6.0,
                        )),
                        CrosshairCoordText,
                        overlay_layer,
                    ));
                });
        }
    }
}

/// Update 3D axis labels - projects axis endpoints to screen and shows axis names + tick values
pub fn update_3d_axis_labels(
    mut commands: Commands,
    tiles: Query<(&PlotTile, &TileRect, &PlotKind, Option<&View3D>)>,
    existing_labels: Query<(Entity, &AxisLabel3D)>,
    axis_info_store: Res<AxisInfo3DStore>,
) {
    // Despawn existing labels (we recreate each frame)
    for (entity, _) in existing_labels.iter() {
        commands.entity(entity).try_despawn();
    }

    // The normalized volume spans [-2.5, 2.5]
    let half_size = 2.5;
    let axis_len = 5.5;
    let origin = Vec3::new(-half_size, -half_size, -half_size);
    let x_tip = origin + Vec3::new(axis_len + 0.3, 0.0, 0.0);
    let y_tip = origin + Vec3::new(0.0, axis_len + 0.3, 0.0);
    let z_tip = origin + Vec3::new(0.0, 0.0, axis_len + 0.3);

    for (tile, rect, kind, view_3d) in tiles.iter() {
        // Only for 3D tiles
        if !matches!(kind, PlotKind::ThreeD) {
            continue;
        }

        let Some(view) = view_3d else {
            continue;
        };

        // Get axis info for this tile
        let axis_info = axis_info_store.info.get(&tile.index);

        // Get camera transform
        let cam_transform = compute_orbit_transform(view);
        let cam_pos = cam_transform.translation;
        let cam_forward = cam_transform.forward();
        let cam_right = cam_transform.right();
        let cam_up = cam_transform.up();

        let vp_width = rect.world_size.x;
        let vp_height = rect.world_size.y;
        let fov = std::f32::consts::FRAC_PI_4;
        let aspect = vp_width / vp_height;
        let tan_half_fov = (fov * 0.5).tan();

        // Project 3D point to screen
        let project_to_screen = |point: Vec3| -> Option<Vec2> {
            let to_point = point - cam_pos;
            let depth = to_point.dot(*cam_forward);
            if depth <= 0.1 {
                return None;
            }

            let right_dist = to_point.dot(*cam_right);
            let up_dist = to_point.dot(*cam_up);

            let ndc_x = right_dist / (depth * tan_half_fov * aspect);
            let ndc_y = up_dist / (depth * tan_half_fov);

            let screen_x = rect.world_center.x + ndc_x * vp_width * 0.5;
            let screen_y = rect.world_center.y + ndc_y * vp_height * 0.5;

            Some(Vec2::new(screen_x, screen_y))
        };

        // Use overlay layer for labels
        let overlay_layer = RenderLayers::layer((tile.index + 16) % 32);

        // Get axis labels (use default if not set)
        let x_label = axis_info
            .and_then(|i| i.x_label.clone())
            .unwrap_or_else(|| "X".to_string());
        let y_label = axis_info
            .and_then(|i| i.y_label.clone())
            .unwrap_or_else(|| "Y".to_string());
        let z_label = axis_info
            .and_then(|i| i.z_label.clone())
            .unwrap_or_else(|| "Z".to_string());

        // Spawn X axis label
        if let Some(screen_pos) = project_to_screen(x_tip) {
            commands.spawn((
                AxisLabel3D {
                    tile_index: tile.index,
                },
                Text2d::new(x_label),
                TextFont {
                    font_size: 12.0,
                    ..default()
                },
                TextColor(Color::srgb(1.0, 0.5, 0.5)),
                Transform::from_translation(Vec3::new(screen_pos.x, screen_pos.y, 10.0)),
                overlay_layer.clone(),
            ));
        }

        // Spawn Y axis label
        if let Some(screen_pos) = project_to_screen(y_tip) {
            commands.spawn((
                AxisLabel3D {
                    tile_index: tile.index,
                },
                Text2d::new(y_label),
                TextFont {
                    font_size: 12.0,
                    ..default()
                },
                TextColor(Color::srgb(0.5, 1.0, 0.5)),
                Transform::from_translation(Vec3::new(screen_pos.x, screen_pos.y, 10.0)),
                overlay_layer.clone(),
            ));
        }

        // Spawn Z axis label
        if let Some(screen_pos) = project_to_screen(z_tip) {
            commands.spawn((
                AxisLabel3D {
                    tile_index: tile.index,
                },
                Text2d::new(z_label),
                TextFont {
                    font_size: 12.0,
                    ..default()
                },
                TextColor(Color::srgb(0.5, 0.7, 1.0)),
                Transform::from_translation(Vec3::new(screen_pos.x, screen_pos.y, 10.0)),
                overlay_layer.clone(),
            ));
        }

        // Add tick values along axes if we have axis info
        if let Some(info) = axis_info {
            let tick_color = Color::srgba(0.8, 0.8, 0.8, 0.8);
            let num_ticks = 5;

            // X-axis ticks (along bottom edge at y=-2.5, z=-2.5)
            for i in 0..=num_ticks {
                let t = i as f32 / num_ticks as f32;
                let normalized_x = -half_size + t * (half_size * 2.0);
                let data_x = info.bounds_min[0] + t * (info.bounds_max[0] - info.bounds_min[0]);
                let tick_pos = Vec3::new(normalized_x, -half_size - 0.3, -half_size);

                if let Some(screen_pos) = project_to_screen(tick_pos) {
                    commands.spawn((
                        AxisLabel3D {
                            tile_index: tile.index,
                        },
                        Text2d::new(format!("{:.1}", data_x)),
                        TextFont {
                            font_size: 9.0,
                            ..default()
                        },
                        TextColor(tick_color),
                        Transform::from_translation(Vec3::new(screen_pos.x, screen_pos.y, 10.0)),
                        overlay_layer.clone(),
                    ));
                }
            }

            // Y-axis ticks (along left edge at x=-2.5, z=-2.5)
            for i in 0..=num_ticks {
                let t = i as f32 / num_ticks as f32;
                let normalized_y = -half_size + t * (half_size * 2.0);
                let data_y = info.bounds_min[1] + t * (info.bounds_max[1] - info.bounds_min[1]);
                let tick_pos = Vec3::new(-half_size - 0.3, normalized_y, -half_size);

                if let Some(screen_pos) = project_to_screen(tick_pos) {
                    commands.spawn((
                        AxisLabel3D {
                            tile_index: tile.index,
                        },
                        Text2d::new(format!("{:.1}", data_y)),
                        TextFont {
                            font_size: 9.0,
                            ..default()
                        },
                        TextColor(tick_color),
                        Transform::from_translation(Vec3::new(screen_pos.x, screen_pos.y, 10.0)),
                        overlay_layer.clone(),
                    ));
                }
            }

            // Z-axis ticks (along bottom edge at x=-2.5, y=-2.5)
            for i in 0..=num_ticks {
                let t = i as f32 / num_ticks as f32;
                let normalized_z = -half_size + t * (half_size * 2.0);
                let data_z = info.bounds_min[2] + t * (info.bounds_max[2] - info.bounds_min[2]);
                let tick_pos = Vec3::new(-half_size, -half_size - 0.3, normalized_z);

                if let Some(screen_pos) = project_to_screen(tick_pos) {
                    commands.spawn((
                        AxisLabel3D {
                            tile_index: tile.index,
                        },
                        Text2d::new(format!("{:.1}", data_z)),
                        TextFont {
                            font_size: 9.0,
                            ..default()
                        },
                        TextColor(tick_color),
                        Transform::from_translation(Vec3::new(screen_pos.x, screen_pos.y, 10.0)),
                        overlay_layer.clone(),
                    ));
                }
            }
        }
    }
}

/// Spawn or update the tab bar UI
pub fn update_tab_bar(
    mut commands: Commands,
    windows: Query<&Window, With<PrimaryWindow>>,
    dash: Res<DashboardRes>,
    existing_tabs: Query<Entity, With<TabBar>>,
    unit: Res<UnitMeshes>,
    mut materials: ResMut<Assets<ColorMaterial>>,
) {
    // Remove existing tab bar
    for entity in existing_tabs.iter() {
        commands.entity(entity).despawn();
    }

    // Only render if we have tabs
    if !dash.0.has_tabs() {
        return;
    }

    let Ok(window) = windows.single() else {
        return;
    };

    let tab_names = dash.0.tab_names();
    let active_tab = dash.0.active_tab;
    let tab_bar_height = 35.0;
    let tab_width = 100.0;
    let tab_gap = 5.0;
    let margin = 20.0;

    // Tab bar background
    let bar_mat = materials.add(ColorMaterial::from(Color::srgba(0.15, 0.15, 0.2, 0.95)));

    let layers = RenderLayers::layer(0);

    commands
        .spawn((
            TabBar,
            Transform::default(),
            Visibility::Visible,
            InheritedVisibility::default(),
            ViewVisibility::default(),
            layers.clone(),
        ))
        .with_children(|parent| {
            // Background bar
            parent.spawn((
                Mesh2d(unit.quad.clone()),
                MeshMaterial2d(bar_mat),
                Transform {
                    translation: Vec3::new(
                        0.0,
                        window.height() * 0.5 - margin - tab_bar_height * 0.5,
                        0.0,
                    ),
                    scale: Vec3::new(window.width() - margin * 2.0, tab_bar_height, 1.0),
                    ..default()
                },
                layers.clone(),
            ));

            // Tab buttons
            let total_tabs_width = tab_names.len() as f32 * (tab_width + tab_gap) - tab_gap;
            let start_x = -total_tabs_width * 0.5 + tab_width * 0.5;

            for (i, name) in tab_names.iter().enumerate() {
                let is_active = i == active_tab;
                let tab_x = start_x + i as f32 * (tab_width + tab_gap);
                let tab_y = window.height() * 0.5 - margin - tab_bar_height * 0.5;

                // Tab button background
                let tab_color = if is_active {
                    Color::srgba(0.3, 0.5, 0.8, 1.0)
                } else {
                    Color::srgba(0.25, 0.25, 0.3, 0.9)
                };
                let tab_mat = materials.add(ColorMaterial::from(tab_color));

                parent.spawn((
                    TabButton { index: i },
                    Mesh2d(unit.quad.clone()),
                    MeshMaterial2d(tab_mat),
                    Transform {
                        translation: Vec3::new(tab_x, tab_y, 1.0),
                        scale: Vec3::new(tab_width, tab_bar_height - 6.0, 1.0),
                        ..default()
                    },
                    layers.clone(),
                ));

                // Tab label
                parent.spawn((
                    Text2d::new(*name),
                    TextFont {
                        font_size: 13.0,
                        ..default()
                    },
                    TextColor(if is_active {
                        Color::srgba(1.0, 1.0, 1.0, 1.0)
                    } else {
                        Color::srgba(0.7, 0.7, 0.7, 0.9)
                    }),
                    Transform::from_translation(Vec3::new(tab_x, tab_y, 2.0)),
                    layers.clone(),
                ));
            }
        });
}

/// Handle tab button clicks
pub fn handle_tab_clicks(
    windows: Query<&Window, With<PrimaryWindow>>,
    mouse: Res<ButtonInput<MouseButton>>,
    mut dash: ResMut<DashboardRes>,
    tab_buttons: Query<(&TabButton, &Transform)>,
) {
    if !mouse.just_pressed(MouseButton::Left) {
        return;
    }

    if !dash.0.has_tabs() {
        return;
    }

    let Ok(window) = windows.single() else {
        return;
    };

    let Some(cursor) = window.cursor_position() else {
        return;
    };

    // Convert to world coordinates (centered origin)
    let cursor_world = Vec2::new(
        cursor.x - window.width() * 0.5,
        window.height() * 0.5 - cursor.y,
    );

    let tab_width = 100.0;
    let tab_height = 29.0;

    for (tab_btn, transform) in tab_buttons.iter() {
        let tab_pos = transform.translation.truncate();
        let half_size = Vec2::new(tab_width * 0.5, tab_height * 0.5);

        if cursor_world.x >= tab_pos.x - half_size.x
            && cursor_world.x <= tab_pos.x + half_size.x
            && cursor_world.y >= tab_pos.y - half_size.y
            && cursor_world.y <= tab_pos.y + half_size.y
        {
            if dash.0.active_tab != tab_btn.index {
                dash.0.active_tab = tab_btn.index;
            }
            break;
        }
    }
}

/// Detect tab changes and refresh tiles
pub fn detect_tab_change(
    mut commands: Commands,
    dash: Res<DashboardRes>,
    mut prev_tab: ResMut<PreviousActiveTab>,
    mut registry: ResMut<TileRegistry>,
    existing: Query<(Entity, &PlotTile)>,
) {
    if !dash.0.has_tabs() {
        return;
    }

    if dash.0.active_tab != prev_tab.0 {
        prev_tab.0 = dash.0.active_tab;

        // Clear all existing tiles to force respawn
        for (entity, tile) in existing.iter() {
            cleanup_tile(&mut commands, &mut registry, entity, tile.id);
        }
    }
}