indicatrix-cut 0.7.1

Desktop faceting-design editor: library browsing, spectral 3D rendering, material retargeting, and a solid inspection view.
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
//! The material editor's physics-color state: the recipe being edited, its undo stack,
//! the locks, the picked color and everything derived from them (rows, swatches, banner).
//!
//! Pure Rust, no Slint: [`PhysicsState`] is edited by the callbacks in [`super::physics_ui`] and
//! turned into plain [`PhysicsView`] data, so every rule here -- the log sliders, the
//! fractions, "impossible" items, the undo stack, the dirty test, mode switching -- is unit
//! tested without a window. Rendering always uses the recipe's stored `resolved_bands`
//! (never a silent re-resolve): an edit re-resolves with the current catalogue, which is the
//! only way the stored bands change.

use std::{
    collections::BTreeSet,
    fmt::Write as _,
    sync::{
        Arc,
        atomic::{AtomicBool, Ordering},
    },
};

use indicatrix::{
    color::body_color::{BodyColor, BodyColors, Illuminant, body_colors, delta_e_2000},
    optics::{
        chromophore::{
            ChromophoreCatalogue, ChromophoreData, ColorRecipe, GlowStrength, HostData,
            ResolvedBands, SolveResult, UV365_NM, UV395_NM, UvGlow, fluorescence_report,
            garnet_optics, resolve, species_element,
        },
        materials::{CrystalSystem, GemMaterial, OpticalCharacter},
    },
};
use indicatrix_cut_core::material::{
    ActiveColor, ColorMode, RecipeHistory, built_in_specific_gravity,
};

use super::crystal_optics::{crystal_system_to_index, optical_character_to_index};

/// Reference path (mm) when no design with a size is open.
pub const DEFAULT_PATH_MM: f32 = 5.0;
/// `reference_path_mm` defaults to this multiple of the open design's girdle diameter, a rough
/// proxy for the table-to-culet path plus return (spec 7).
pub const GIRDLE_PATH_FACTOR: f32 = 1.5;
/// An amount of 0 sits at the left end of a log slider; the smallest non-zero amount is
/// `LOG_SPAN * max` (the solver's own `c_min`).
pub const LOG_SPAN: f64 = 1e-3;
/// The slider strength bounds.
pub const STRENGTH_RANGE: (f64, f64) = (0.05, 10.0);
/// D65 delta-E above which a pick counts as "closest reachable" (`SolveResult::reachable`).
pub const REACHABLE_DELTA_E: f64 = 1.0;
/// D65 delta-E above which the mismatch is shown in the warning color.
pub const WARN_DELTA_E: f64 = 2.0;
/// color-change label threshold (spec 3.5).
pub const COLOR_CHANGE_DELTA_E: f64 = 5.0;

/// Whether this build shows the physics-based color editor: the `physical-color` cargo feature.
///
/// UI only. The data layer (a material's stored color mode and recipe, its rendering, the
/// save path) is compiled and active in every build, so a physics material keeps rendering
/// exactly as before and is saved back unchanged; only the controls that edit a recipe are
/// hidden when this is `false`. Pushed to Slint once as `PhysicscolorModel.ui_enabled`.
pub const PHYSICS_COLOR_UI: bool = cfg!(feature = "physical-color");

/// What the Gem Material Editor's color area shows (see [`color_section_view`]).
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum ColorSectionView {
    /// Feature on, Fantasy mode: the mode toggle and the preset swatches with the free picker.
    Fantasy,
    /// Feature on, Physics mode: the mode toggle and the host/element recipe section.
    Physics,
    /// Feature off, Fantasy mode: the preset swatches and the free picker, no mode toggle.
    FantasyOnly,
    /// Feature off, but the material's color comes from a physics recipe: the same controls as
    /// [`Self::FantasyOnly`] plus a note. Picking a color switches the material to Fantasy; the
    /// recipe stays parked in the stored payload.
    FantasyOverRecipe,
}

impl ColorSectionView {
    /// The "Fantasy | Physics" toggle is shown.
    #[must_use]
    pub const fn shows_mode_toggle(self) -> bool {
        matches!(self, Self::Fantasy | Self::Physics)
    }

    /// The host/element recipe section is shown.
    #[must_use]
    pub const fn shows_physics_section(self) -> bool {
        matches!(self, Self::Physics)
    }

    /// The preset swatches and the free picker are shown.
    #[must_use]
    pub const fn shows_fantasy_section(self) -> bool {
        !matches!(self, Self::Physics)
    }

    /// The "color comes from a physics recipe" note is shown. Also the one state in which
    /// choosing a fixed color must first replace the recipe's color (see
    /// [`PhysicsState::choose_fixed_color`]).
    #[must_use]
    pub const fn shows_recipe_note(self) -> bool {
        matches!(self, Self::FantasyOverRecipe)
    }
}

/// The view of the dialog's color area for a build and a color mode.
///
/// `feature_on` is [`PHYSICS_COLOR_UI`]; `mode_is_physics` is whether physics is the active
/// color mode (when the dialog opens: the stored mode).
///
/// With the feature off the dialog always shows the Fantasy controls, even for a stored
/// physics mode: otherwise nothing would be editable.
#[must_use]
pub const fn color_section_view(feature_on: bool, mode_is_physics: bool) -> ColorSectionView {
    match (feature_on, mode_is_physics) {
        (true, false) => ColorSectionView::Fantasy,
        (true, true) => ColorSectionView::Physics,
        (false, false) => ColorSectionView::FantasyOnly,
        (false, true) => ColorSectionView::FantasyOverRecipe,
    }
}

/// The default reference path for a design whose girdle is `stone_width_mm` (`<= 0`: no design).
#[must_use]
pub fn default_reference_path_mm(stone_width_mm: f32) -> f32 {
    if stone_width_mm > 0.0 {
        stone_width_mm * GIRDLE_PATH_FACTOR
    } else {
        DEFAULT_PATH_MM
    }
}

/// Log-scale slider position `0..=1` of `amount` in `[0, max]`.
#[must_use]
pub fn amount_to_pos(amount: f64, max: f64) -> f64 {
    let c_min = LOG_SPAN * max;
    if max <= 0.0 || amount <= c_min {
        return 0.0;
    }
    (amount / c_min).log(max / c_min).clamp(0.0, 1.0)
}

/// Inverse of [`amount_to_pos`]; position 0 is exactly 0.
#[must_use]
pub fn pos_to_amount(pos: f64, max: f64) -> f64 {
    if max <= 0.0 || !pos.is_finite() || pos <= 0.0 {
        return 0.0;
    }
    let c_min = LOG_SPAN * max;
    (c_min * (max / c_min).powf(pos.min(1.0))).min(max)
}

/// The unit an item's amount is entered in.
#[must_use]
pub fn item_unit(host: &HostData, id: &str) -> String {
    if host.end_members.iter().any(|m| m.id == id) {
        "mol_fraction".to_string()
    } else {
        host.element_unit(id).unwrap_or_default()
    }
}

/// A human label for a concentration unit.
#[must_use]
pub fn unit_label(unit: &str) -> String {
    if let Some(oxide) = unit.strip_prefix("wt_pct_oxide:") {
        return format!("wt% {oxide}");
    }
    match unit {
        "ppm_site" => "ppm".to_string(),
        "ppma_all" => "ppma".to_string(),
        "mol_fraction" => "mol fraction".to_string(),
        other => other.to_string(),
    }
}

/// `amount` with its unit, e.g. `0.200 wt% Cr2O3`, `80 ppm`.
#[must_use]
pub fn format_amount(amount: f64, unit: &str) -> String {
    let number = if unit.starts_with("wt_pct_oxide:") || unit == "mol_fraction" {
        format!("{amount:.3}")
    } else if amount >= 100.0 {
        format!("{amount:.0}")
    } else {
        format!("{amount:.1}")
    };
    format!("{number} {}", unit_label(unit))
}

/// The elements/end members a chromophore is driven by -- the same ids
/// `ChromophoreCatalogue::selectable_elements` offers, including the compensator of an
/// intervalence pair (e.g. Mg, which binds the Ti4+ of the Fe2+-Ti4+ pair).
#[must_use]
pub fn drivers(c: &ChromophoreData) -> Vec<String> {
    let mut ids = absorbers(c);
    if c.kind == "ivct_pair"
        && let Some(comp) = &c.compensator
    {
        ids.push(comp.element.clone());
    }
    ids
}

/// The elements/end members whose presence makes a chromophore absorb: [`drivers`] without
/// a pair's compensator, which on its own creates nothing.
fn absorbers(c: &ChromophoreData) -> Vec<String> {
    match c.kind.as_str() {
        "end_member" => c.end_member.iter().cloned().collect(),
        "ivct_pair" => c
            .partners
            .iter()
            .map(|p| species_element(p).to_string())
            .collect(),
        _ if c.conc_unit == "intensity" => Vec::new(),
        _ => c.elements.clone(),
    }
}

/// A typical amount of `id` in its own unit, the `c_ref` of the fractions view: the largest
/// `conc_typical` upper bound of the offered chromophores it drives (converted into the item's
/// unit), or a tenth of the maximum when the data give none.
#[must_use]
pub fn typical_amount(host: &HostData, id: &str) -> f64 {
    let max = host.element_conc_max(id);
    if host.end_members.iter().any(|m| m.id == id) {
        let typical = host
            .chromophores
            .iter()
            .filter(|c| c.is_offered() && c.end_member.as_deref() == Some(id))
            .filter_map(|c| c.conc_typical.map(|t| t[1]))
            .fold(0.0_f64, f64::max);
        return if typical > 0.0 {
            typical.min(max)
        } else {
            0.25 * max
        };
    }
    let unit = item_unit(host, id);
    let n_in = host.n_site_for_unit(&unit);
    let mut best = 0.0_f64;
    if n_in > 0.0 {
        for c in &host.chromophores {
            if !c.is_offered() || !drivers(c).iter().any(|d| d == id) {
                continue;
            }
            if let Some(t) = c.conc_typical {
                best = best.max(t[1] * host.n_site_for_unit(&c.conc_unit) / n_in);
            }
        }
    }
    if best > 0.0 { best.min(max) } else { 0.1 * max }
}

/// Why `id` cannot be added to the recipe right now (`None`: it can). Derived only from the
/// catalogue: a missing usable range, a full end-member budget, a chromophore whose `requires` /
/// `excludes` name another chromophore of the host by id, or a centre only a treatment creates.
#[must_use]
pub fn impossible_reason(host: &HostData, recipe: &ColorRecipe, id: &str) -> Option<String> {
    let chromos: Vec<&ChromophoreData> = host
        .chromophores
        .iter()
        .filter(|c| c.is_offered() && drivers(c).iter().any(|d| d == id))
        .collect();
    if chromos.is_empty() || host.element_conc_max(id) <= 0.0 {
        return Some("no usable concentration range in the data".to_string());
    }
    let present: BTreeSet<&str> = recipe
        .entries
        .iter()
        .filter(|e| e.amount > 0.0)
        .map(|e| e.id.as_str())
        .collect();
    if host.end_members.iter().any(|m| m.id == id) {
        let sum: f64 = host
            .end_members
            .iter()
            .filter(|m| !m.colorless)
            .map(|m| recipe.amount(&m.id))
            .sum();
        if sum >= 1.0 - 1e-9 {
            return Some("end-member fractions already add up to 100 %".to_string());
        }
    }
    let chromophore_present = |other: &ChromophoreData| -> bool {
        absorbers(other)
            .iter()
            .any(|d| present.contains(d.as_str()))
    };
    let mut first_reason: Option<String> = None;
    for c in &chromos {
        // A chromophore with no block makes the item possible (`?` returns `None`).
        let reason = chromophore_block(host, recipe, c, &chromophore_present)?;
        first_reason.get_or_insert(reason);
    }
    first_reason
}

fn chromophore_block(
    host: &HostData,
    recipe: &ColorRecipe,
    c: &ChromophoreData,
    present: &dyn Fn(&ChromophoreData) -> bool,
) -> Option<String> {
    for req in &c.requires {
        if let Some(other) = host.chromophores.iter().find(|o| &o.id == req)
            && !present(other)
        {
            return Some(format!("needs {} first", other.id));
        }
    }
    for exc in &c.excludes {
        if let Some(other) = host.chromophores.iter().find(|o| &o.id == exc)
            && present(other)
        {
            return Some(format!("excluded by {}", other.id));
        }
    }
    if host.is_treatment_created(&c.id) {
        let creators: Vec<_> = host
            .treatments
            .iter()
            .filter(|t| {
                t.effects.iter().any(|e| {
                    e.effect_type == "create_centre" && e.centre_id.as_deref() == Some(&c.id)
                })
            })
            .collect();
        if !creators.iter().any(|t| recipe.treatments.contains(&t.id)) {
            let name = creators.first().map_or("a treatment", |t| t.name.as_str());
            return Some(format!("only exists after treatment: {name}"));
        }
    }
    None
}

/// Confidence ranking, worst first.
fn confidence_rank(confidence: &str) -> u8 {
    match confidence {
        "unknown" => 0,
        "estimate" => 1,
        "secondary" => 2,
        _ => 3,
    }
}

/// Per-item data confidence: the worst confidence of the offered chromophores/bands that
/// `id` drives, plus the Sources popover text.
#[must_use]
pub fn item_sources(host: &HostData, id: &str) -> (String, String) {
    let mut worst = "verified".to_string();
    let mut text = String::new();
    for c in &host.chromophores {
        if !c.is_offered() || !drivers(c).iter().any(|d| d == id) {
            continue;
        }
        let mut conf = c.confidence.clone();
        for b in c.usable_bands() {
            if let Some(bc) = &b.confidence
                && confidence_rank(bc) < confidence_rank(&conf)
            {
                conf.clone_from(bc);
            }
        }
        if confidence_rank(&conf) < confidence_rank(&worst) {
            worst.clone_from(&conf);
        }
        let _ = writeln!(text, "{} - confidence: {conf}", c.id);
        let mut seen = BTreeSet::new();
        for b in c.usable_bands() {
            if let Some(src) = &b.source
                && seen.insert(src.clone())
                && seen.len() <= 6
            {
                let _ = writeln!(text, "  \u{2022} {src}");
            }
        }
        if seen.len() > 6 {
            let _ = writeln!(text, "  \u{2026} and {} more sources", seen.len() - 6);
        }
        let skipped = c.bands.len() - c.usable_bands().count();
        if skipped > 0 {
            let _ = writeln!(
                text,
                "  {skipped} band(s) skipped (suspect or no coefficient)"
            );
        }
    }
    if text.is_empty() {
        text.push_str("No sources recorded.");
    }
    (worst, text.trim_end().to_string())
}

/// The data-confidence banner of a host: `Some` text only when some offered coefficient is not
/// `verified`/`measured`.
#[must_use]
pub fn host_banner(host: &HostData) -> Option<String> {
    let offered: Vec<&ChromophoreData> = host
        .chromophores
        .iter()
        .filter(|c| c.is_offered())
        .collect();
    let weak = offered
        .iter()
        .filter(|c| !matches!(c.confidence.as_str(), "verified" | "measured"))
        .count();
    if weak == 0 {
        return None;
    }
    let estimates = offered
        .iter()
        .filter(|c| matches!(c.confidence.as_str(), "estimate" | "unknown"))
        .count();
    let detail = if estimates > 0 {
        format!(
            "{weak} of {} chromophores are secondary-source or estimated ({estimates} estimated)",
            offered.len()
        )
    } else {
        format!(
            "{weak} of {} chromophores are secondary-source",
            offered.len()
        )
    };
    // The warning icon is drawn by the banner itself (`chromophore_picker.slint`): the UI
    // font has no glyph for the warning sign.
    Some(format!(
        "Strengths uncalibrated \u{2014} approximate. {detail}."
    ))
}

/// The optics a host (and, for garnet-style hosts, the recipe's end-member mix) proposes.
#[derive(Debug, Clone, PartialEq)]
pub struct Prefill {
    /// Mean refractive index (sodium D).
    pub ri: f32,
    /// Fraunhofer F-C dispersion.
    pub dispersion: f32,
    /// Birefringence.
    pub birefringence: f32,
    /// Specific gravity (`0.0`: unknown).
    pub specific_gravity: f32,
    /// Crystal-system combo index.
    pub crystal_system_idx: i32,
    /// Optical-character combo index.
    pub optical_character_idx: i32,
    /// `n_beta` - `n_alpha` (biaxial).
    pub biaxial_delta: f32,
}

/// The prefill from a host's built-in material.
fn builtin_prefill(host: &HostData, b: &GemMaterial) -> Prefill {
    let sg = host
        .materials
        .iter()
        .find_map(|n| built_in_specific_gravity(n))
        .map_or(host.density_g_cm3 as f32, |s| s.representative as f32);
    Prefill {
        ri: b.dispersion.evaluate(589.3),
        dispersion: b.dispersion.evaluate(486.1) - b.dispersion.evaluate(656.3),
        birefringence: b.birefringence_delta,
        specific_gravity: sg,
        crystal_system_idx: crystal_system_to_index(b.crystal_system),
        optical_character_idx: optical_character_to_index(b.optical_character),
        biaxial_delta: b.biaxial_delta_beta_alpha.unwrap_or(0.0),
    }
}

/// The prefill for a host with no built-in material: its optical class and density.
fn generic_prefill(host: &HostData) -> Prefill {
    let oc = match host.optical.as_str() {
        "isotropic" => OpticalCharacter::Isotropic,
        "biaxial" => OpticalCharacter::BiaxialPositive,
        _ => OpticalCharacter::UniaxialPositive,
    };
    let cs = match oc {
        OpticalCharacter::Isotropic => CrystalSystem::Cubic,
        OpticalCharacter::BiaxialPositive | OpticalCharacter::BiaxialNegative => {
            CrystalSystem::Orthorhombic
        }
        _ => CrystalSystem::Hexagonal,
    };
    Prefill {
        ri: 1.6,
        dispersion: 0.015,
        birefringence: 0.0,
        specific_gravity: host.density_g_cm3 as f32,
        crystal_system_idx: crystal_system_to_index(cs),
        optical_character_idx: optical_character_to_index(oc),
        biaxial_delta: 0.0,
    }
}

/// RI/SG/crystal system prefill for `host`; end-member hosts interpolate RI and SG over the
/// recipe's fractions (`garnet_optics`), so a garnet mix is prefilled rather than locked.
#[must_use]
pub fn host_prefill(host: &HostData, recipe: Option<&ColorRecipe>) -> Prefill {
    let builtin = host.materials.iter().find_map(|name| {
        GemMaterial::all_materials()
            .into_iter()
            .find(|m| m.name.eq_ignore_ascii_case(name))
    });
    let mut out = builtin
        .as_ref()
        .map_or_else(|| generic_prefill(host), |b| builtin_prefill(host, b));
    if !host.end_members.is_empty() {
        let fractions = host.end_member_fractions(|id| recipe.map_or(0.0, |r| r.amount(id)));
        if let Some(fractions) = fractions {
            let refs: Vec<(&str, f64)> = fractions.iter().map(|(k, v)| (k.as_str(), *v)).collect();
            let (ri, sg) = garnet_optics(&refs);
            out.ri = ri as f32;
            out.specific_gravity = sg as f32;
        }
    }
    out
}

/// What a solver request is for.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum SolveKind {
    /// The user picked a color.
    Pick,
    /// The first switch to physics: solve the current fantasy color into the host.
    InitialFromFantasy,
}

/// One solver job, handed to the worker (see [`super::physics_solver`]).
#[derive(Debug, Clone)]
pub struct SolveJob {
    /// Generation stamp: only the state's latest generation may write back.
    pub generation: u64,
    /// Host id to solve in.
    pub host: String,
    /// Target color (D65 `Lab`).
    pub target_lab: [f64; 3],
    /// Reference path (mm).
    pub reference_path_mm: f32,
    /// Locked `(id, amount)` entries the solver keeps.
    pub locked: Vec<(String, f64)>,
    /// Set when a newer job superseded this one.
    pub cancel: Arc<AtomicBool>,
}

/// The picked color.
#[derive(Debug, Clone, Copy, PartialEq)]
pub struct Target {
    /// D65 `Lab`.
    pub lab: [f64; 3],
    /// sRGB for the swatch.
    pub srgb: [u8; 3],
}

/// How the last solve ended.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct SolveSummary {
    /// Whether the search hit its evaluation cap.
    pub capped: bool,
}

/// The dialog's physics state.
#[derive(Debug)]
pub struct PhysicsState {
    /// Both color payloads and the active one.
    pub mode: ColorMode,
    /// The host the recipe (or the next recipe) is for.
    pub host_id: String,
    /// Strength + fractions view (`true`) or absolute amounts.
    pub view_fractions: bool,
    /// Items whose amounts the solver keeps.
    pub locked: BTreeSet<String>,
    /// The picked color, if any.
    pub target: Option<Target>,
    /// How the last solve ended.
    pub summary: Option<SolveSummary>,
    /// A solver job is in flight.
    pub solving: bool,
    /// The recipe undo/redo stack (50 entries).
    pub history: RecipeHistory,
    /// The open design's girdle diameter (mm); `0.0` when none.
    pub stone_width_mm: f32,
    /// Latest solver generation; results of older generations are dropped.
    pub generation: u64,
    cancel: Option<Arc<AtomicBool>>,
    solving_kind: Option<SolveKind>,
    fantasy_picked: bool,
    drag_origin: Option<ColorRecipe>,
    baseline: ColorMode,
    /// The stored color text the dialog opened with (trimmed); see [`Self::mode_json`].
    opened_json: String,
    pending_prefill: Option<Prefill>,
}

impl PhysicsState {
    /// The state a freshly opened dialog starts from: every piece of physics state is rebuilt
    /// (nothing leaks from a previous open). `stored_json` is the material's stored color JSON,
    /// `fantasy_rgb` its fantasy color when there is none, `material_name` the name used to
    /// preselect a host.
    #[must_use]
    pub fn open(
        stored_json: &str,
        fantasy_rgb: [f32; 3],
        material_name: &str,
        stone_width_mm: f32,
        catalogue: &ChromophoreCatalogue,
        previous_generation: u64,
    ) -> Self {
        let mode =
            ColorMode::from_json(stored_json).unwrap_or_else(|| ColorMode::fantasy(fantasy_rgb));
        let host_id = mode
            .last_recipe
            .as_ref()
            .map(|r| r.host.clone())
            .filter(|h| catalogue.host(h).is_some())
            .or_else(|| {
                catalogue
                    .host_for_material(material_name)
                    .map(|h| h.id.clone())
            })
            .or_else(|| catalogue.hosts.first().map(|h| h.id.clone()))
            .unwrap_or_default();
        Self {
            baseline: mode.clone(),
            opened_json: stored_json.trim().to_string(),
            mode,
            host_id,
            view_fractions: true,
            locked: BTreeSet::new(),
            target: None,
            summary: None,
            solving: false,
            history: RecipeHistory::new(),
            stone_width_mm,
            generation: previous_generation + 1,
            cancel: None,
            solving_kind: None,
            fantasy_picked: false,
            drag_origin: None,
            pending_prefill: None,
        }
    }

    /// Whether physics is the active mode.
    #[must_use]
    pub const fn is_physics(&self) -> bool {
        matches!(self.mode.active, ActiveColor::Physics)
    }

    /// The recipe, if the material has one.
    #[must_use]
    pub const fn recipe(&self) -> Option<&ColorRecipe> {
        self.mode.last_recipe.as_ref()
    }

    /// The reference path currently in force.
    #[must_use]
    pub fn reference_path_mm(&self) -> f32 {
        self.recipe().map_or_else(
            || default_reference_path_mm(self.stone_width_mm),
            |r| r.reference_path_mm,
        )
    }

    fn blank_recipe(&self, catalogue: &ChromophoreCatalogue) -> ColorRecipe {
        let mut recipe = ColorRecipe::new(self.host_id.clone(), catalogue.data_version);
        recipe.reference_path_mm = default_reference_path_mm(self.stone_width_mm);
        refresh(&mut recipe, catalogue);
        recipe
    }

    fn recipe_mut(&mut self, catalogue: &ChromophoreCatalogue) -> &mut ColorRecipe {
        if self.mode.last_recipe.is_none() {
            let blank = self.blank_recipe(catalogue);
            self.mode.last_recipe = Some(blank);
        }
        self.mode
            .last_recipe
            .as_mut()
            .unwrap_or_else(|| unreachable!("a recipe was just installed"))
    }

    /// Pushes the current recipe on the undo stack (call before changing it).
    fn push_history(&mut self) {
        if let Some(r) = self.mode.last_recipe.clone() {
            self.history.push(r);
        }
    }

    // ---- mode ----

    /// Switches mode. Physics: keeps an existing recipe, else installs a blank one for the
    /// host and returns the solver job that fits the *current fantasy color*. Fantasy: keeps the
    /// recipe, seeds a default fantasy color from the physics color. Any in-flight solve is
    /// cancelled. `fantasy_rgb_from_dialog` is the dialog's selected preset color (`None` for
    /// "Custom (keep)", which keeps the stored payload).
    pub fn set_mode(
        &mut self,
        physics: bool,
        fantasy_rgb_from_dialog: Option<[f32; 3]>,
        catalogue: &ChromophoreCatalogue,
    ) -> Option<SolveJob> {
        self.cancel_solves();
        if let Some(rgb) = fantasy_rgb_from_dialog {
            self.mode.fantasy_rgb = rgb;
        }
        if !physics {
            self.mode.switch_to_fantasy();
            return None;
        }
        let needs_solve = self.mode.last_recipe.is_none();
        self.mode.active = ActiveColor::Physics;
        if needs_solve {
            let blank = self.blank_recipe(catalogue);
            self.mode.last_recipe = Some(blank);
            self.pending_prefill = catalogue
                .host(&self.host_id)
                .map(|h| host_prefill(h, self.recipe()));
            let lab = self.mode.fantasy_target_lab();
            return Some(self.begin_solve(SolveKind::InitialFromFantasy, lab, None));
        }
        None
    }

    /// The fantasy picker's result: the legacy color fitted to the picked one.
    ///
    /// A pick is a decision for a fixed color, so Fantasy becomes the active mode (the recipe
    /// stays parked in the payload). With the physics editor visible the picker only exists in
    /// Fantasy mode, so there this changes nothing; with it hidden the picker is also offered
    /// over a stored physics recipe.
    pub const fn set_fantasy_pick(&mut self, rgb: [f32; 3]) {
        self.mode.fantasy_rgb = rgb;
        self.mode.active = ActiveColor::Fantasy;
        self.fantasy_picked = true;
    }

    /// A fixed color was chosen while the physics editor is hidden: the material goes Fantasy.
    ///
    /// The color comes from a preset swatch or from a template that carries one. The recipe
    /// stays parked in the payload, so saving keeps both. `None` is the "Keep" swatch: the
    /// payload's own fantasy color (seeded from the recipe when it never had one).
    ///
    /// Unlike [`Self::set_mode`], an explicit color is never re-seeded from the recipe -- not
    /// even "Clear", whose all-zero triple the default-color test of
    /// `ColorMode::switch_to_fantasy` would take for "no color set yet".
    pub fn choose_fixed_color(&mut self, rgb: Option<[f32; 3]>) {
        self.cancel_solves();
        match rgb {
            Some(rgb) => {
                self.mode.fantasy_rgb = rgb;
                self.mode.active = ActiveColor::Fantasy;
            }
            None => self.mode.switch_to_fantasy(),
        }
    }

    // ---- host ----

    /// Chooses the host `host_id`: a fresh recipe for it (the old one goes on the undo stack),
    /// and a prefill of the optics.
    pub fn select_host(&mut self, host_id: &str, catalogue: &ChromophoreCatalogue) -> bool {
        let Some(host) = catalogue.host(host_id) else {
            return false;
        };
        self.cancel_solves();
        self.locked.clear();
        self.target = None;
        self.summary = None;
        if self.host_id != host_id || self.recipe().is_some_and(|r| r.host != host_id) {
            self.push_history();
            self.host_id = host_id.to_string();
            let blank = self.blank_recipe(catalogue);
            self.mode.last_recipe = Some(blank);
        }
        self.pending_prefill = Some(host_prefill(host, self.recipe()));
        true
    }

    /// The prefill proposed by the latest host/mix change, once.
    pub const fn take_prefill(&mut self) -> Option<Prefill> {
        self.pending_prefill.take()
    }

    // ---- recipe edits ----

    fn max_for(&self, id: &str, catalogue: &ChromophoreCatalogue) -> f64 {
        catalogue
            .host(&self.host_id)
            .map_or(0.0, |h| h.element_conc_max(id))
    }

    /// The largest amount an end member can take: what the others leave of 1.
    fn end_member_room(&self, id: &str, catalogue: &ChromophoreCatalogue) -> Option<f64> {
        let host = catalogue.host(&self.host_id)?;
        if !host.end_members.iter().any(|m| m.id == id && !m.colorless) {
            return None;
        }
        let recipe = self.recipe()?;
        let others: f64 = host
            .end_members
            .iter()
            .filter(|m| !m.colorless && m.id != id)
            .map(|m| recipe.amount(&m.id))
            .sum();
        Some((1.0 - others).max(0.0))
    }

    /// A drag step of the log slider of `id` to position `pos` (no history entry until
    /// [`Self::release`]).
    pub fn set_amount_pos(&mut self, id: &str, pos: f64, catalogue: &ChromophoreCatalogue) {
        let mut max = self.max_for(id, catalogue);
        if let Some(room) = self.end_member_room(id, catalogue) {
            max = max.min(room);
        }
        let amount = pos_to_amount(pos, self.max_for(id, catalogue)).min(max);
        self.begin_drag();
        let recipe = self.recipe_mut(catalogue);
        if recipe.set_amount(id, amount) {
            refresh(recipe, catalogue);
        }
        self.after_edit(id, catalogue);
    }

    /// Sets the share of `id` in the fractions view: the others keep their proportions and fill
    /// the rest, so the total "typical-normalised" amount stays put.
    pub fn set_fraction(&mut self, id: &str, fraction: f64, catalogue: &ChromophoreCatalogue) {
        let Some(host) = catalogue.host(&self.host_id) else {
            return;
        };
        let Some(recipe) = self.recipe() else {
            return;
        };
        let units: Vec<(String, f64)> = recipe
            .entries
            .iter()
            .map(|e| {
                (
                    e.id.clone(),
                    e.amount / typical_amount(host, &e.id).max(1e-12),
                )
            })
            .collect();
        let total: f64 = units.iter().map(|(_, u)| u).sum();
        if total <= 0.0 || units.len() < 2 {
            return;
        }
        let f = fraction.clamp(0.0, 1.0);
        let others: f64 = units.iter().filter(|(k, _)| k != id).map(|(_, u)| u).sum();
        let new_units: Vec<(String, f64)> = units
            .iter()
            .map(|(k, u)| {
                if k == id {
                    (k.clone(), f * total)
                } else if others > 0.0 {
                    (k.clone(), u / others * (1.0 - f) * total)
                } else {
                    (k.clone(), (1.0 - f) * total / (units.len() - 1) as f64)
                }
            })
            .collect();
        self.begin_drag();
        let host_id = self.host_id.clone();
        let Some(host) = catalogue.host(&host_id) else {
            return;
        };
        let caps: Vec<(String, f64, f64)> = new_units
            .iter()
            .map(|(k, u)| {
                (
                    k.clone(),
                    u * typical_amount(host, k),
                    host.element_conc_max(k),
                )
            })
            .collect();
        let recipe = self.recipe_mut(catalogue);
        for (k, amount, max) in caps {
            recipe.set_amount(&k, amount.min(max));
        }
        refresh(recipe, catalogue);
        self.after_edit(id, catalogue);
    }

    fn after_edit(&mut self, id: &str, catalogue: &ChromophoreCatalogue) {
        // A garnet-style mix re-prefills RI/SG from the end-member fractions.
        if let Some(host) = catalogue.host(&self.host_id)
            && host.end_members.iter().any(|m| m.id == id)
        {
            self.pending_prefill = Some(host_prefill(host, self.recipe()));
        }
    }

    fn begin_drag(&mut self) {
        if self.drag_origin.is_none() {
            self.drag_origin = self.recipe().cloned();
        }
    }

    /// A slider was released: commits the whole drag as one undo step.
    pub fn release(&mut self) {
        if let Some(origin) = self.drag_origin.take()
            && self.recipe().is_some_and(|r| *r != origin)
        {
            self.history.push(origin);
        }
    }

    /// Sets the strength (a drag step; see [`Self::release`]).
    pub fn set_strength(&mut self, strength: f64, catalogue: &ChromophoreCatalogue) {
        self.begin_drag();
        let recipe = self.recipe_mut(catalogue);
        if recipe.set_strength(strength.clamp(STRENGTH_RANGE.0, STRENGTH_RANGE.1)) {
            refresh(recipe, catalogue);
        }
    }

    /// Sets the reference path (a drag step; see [`Self::release`]).
    pub fn set_path(&mut self, mm: f32, catalogue: &ChromophoreCatalogue) {
        if !mm.is_finite() {
            return;
        }
        self.begin_drag();
        let recipe = self.recipe_mut(catalogue);
        recipe.reference_path_mm = mm.clamp(0.1, 100.0);
        refresh(recipe, catalogue);
    }

    /// Adds `id` at its typical amount (undoable).
    pub fn add_item(&mut self, id: &str, catalogue: &ChromophoreCatalogue) -> bool {
        let Some(host) = catalogue.host(&self.host_id) else {
            return false;
        };
        if !catalogue
            .selectable_elements(&self.host_id)
            .iter()
            .any(|e| e == id)
        {
            return false;
        }
        if self
            .recipe()
            .is_some_and(|r| impossible_reason(host, r, id).is_some())
        {
            return false;
        }
        self.push_history();
        let mut amount = typical_amount(host, id);
        if let Some(room) = self.end_member_room(id, catalogue) {
            amount = amount.min(room);
        }
        let recipe = self.recipe_mut(catalogue);
        recipe.set_amount(id, amount);
        refresh(recipe, catalogue);
        self.after_edit(id, catalogue);
        true
    }

    /// Removes `id` and any treatment that no longer has its required elements (undoable).
    pub fn remove_item(&mut self, id: &str, catalogue: &ChromophoreCatalogue) {
        if self
            .recipe()
            .is_none_or(|r| !r.entries.iter().any(|e| e.id == id))
        {
            return;
        }
        self.push_history();
        self.locked.remove(id);
        let host_id = self.host_id.clone();
        let recipe = self.recipe_mut(catalogue);
        recipe.remove_entry(id);
        let present: Vec<String> = recipe
            .entries
            .iter()
            .filter(|e| e.amount > 0.0)
            .map(|e| e.id.clone())
            .collect();
        let present_refs: Vec<&str> = present.iter().map(String::as_str).collect();
        let valid: BTreeSet<String> = catalogue
            .selectable_treatments(&host_id, &present_refs)
            .iter()
            .map(|t| t.id.clone())
            .collect();
        recipe.treatments.retain(|t| valid.contains(t));
        refresh(recipe, catalogue);
        self.after_edit(id, catalogue);
    }

    /// Toggles the solver lock of `id`.
    pub fn toggle_lock(&mut self, id: &str) {
        if !self.locked.remove(id) {
            self.locked.insert(id.to_string());
        }
    }

    /// Switches a treatment on or off (undoable).
    pub fn toggle_treatment(&mut self, id: &str, active: bool, catalogue: &ChromophoreCatalogue) {
        if self
            .recipe()
            .is_some_and(|r| r.treatments.iter().any(|t| t == id) == active)
        {
            return;
        }
        self.push_history();
        let recipe = self.recipe_mut(catalogue);
        if active {
            recipe.treatments.push(id.to_string());
        } else {
            recipe.treatments.retain(|t| t != id);
        }
        refresh(recipe, catalogue);
    }

    /// Re-resolves the recipe with the current catalogue data (the "update to current data"
    /// action; undoable, never silent).
    pub fn update_data(&mut self, catalogue: &ChromophoreCatalogue) {
        self.push_history();
        let recipe = self.recipe_mut(catalogue);
        refresh(recipe, catalogue);
    }

    /// Undo (Ctrl-Z): restores the previous recipe exactly.
    pub fn undo(&mut self) -> bool {
        let Some(current) = self.mode.last_recipe.clone() else {
            return false;
        };
        self.cancel_solves();
        self.drag_origin = None;
        let Some(prev) = self.history.undo(current) else {
            return false;
        };
        self.host_id.clone_from(&prev.host);
        self.mode.last_recipe = Some(prev);
        true
    }

    /// Redo (Ctrl-Y).
    pub fn redo(&mut self) -> bool {
        let Some(current) = self.mode.last_recipe.clone() else {
            return false;
        };
        self.cancel_solves();
        self.drag_origin = None;
        let Some(next) = self.history.redo(current) else {
            return false;
        };
        self.host_id.clone_from(&next.host);
        self.mode.last_recipe = Some(next);
        true
    }

    // ---- solver ----

    /// Cancels the in-flight solve (if any) and bumps the generation, so a late result is dropped.
    pub fn cancel_solves(&mut self) {
        if let Some(token) = self.cancel.take() {
            token.store(true, Ordering::SeqCst);
        }
        self.generation += 1;
        self.solving = false;
        self.solving_kind = None;
    }

    fn begin_solve(&mut self, kind: SolveKind, lab: [f64; 3], srgb: Option<[u8; 3]>) -> SolveJob {
        self.cancel_solves();
        let cancel = Arc::new(AtomicBool::new(false));
        self.cancel = Some(Arc::clone(&cancel));
        self.solving = true;
        self.solving_kind = Some(kind);
        if let Some(srgb) = srgb {
            self.target = Some(Target { lab, srgb });
        }
        let locked: Vec<(String, f64)> = self
            .recipe()
            .map(|r| {
                r.entries
                    .iter()
                    .filter(|e| self.locked.contains(&e.id))
                    .map(|e| (e.id.clone(), e.amount))
                    .collect()
            })
            .unwrap_or_default();
        SolveJob {
            generation: self.generation,
            host: self.host_id.clone(),
            target_lab: lab,
            reference_path_mm: self.reference_path_mm(),
            locked,
            cancel,
        }
    }

    /// The user picked `srgb` as the target color: the job that solves for it.
    pub fn pick(&mut self, srgb: [u8; 3]) -> SolveJob {
        let lab = indicatrix::color::body_color::srgb_to_lab(srgb.map(|c| f64::from(c) / 255.0));
        self.begin_solve(SolveKind::Pick, lab, Some(srgb))
    }

    /// Applies a finished solve. Stale generations are dropped (`false`); a current one pushes
    /// the old recipe on the undo stack first, then writes the solver's recipe (locked entries
    /// are part of it).
    pub fn finish_solve(
        &mut self,
        generation: u64,
        result: SolveResult,
        catalogue: &ChromophoreCatalogue,
    ) -> bool {
        if generation != self.generation || !self.solving {
            return false;
        }
        self.solving = false;
        self.solving_kind = None;
        self.cancel = None;
        let keep_path = self.reference_path_mm();
        let mut recipe = result.recipe;
        recipe.reference_path_mm = keep_path;
        refresh(&mut recipe, catalogue);
        self.push_history();
        self.host_id.clone_from(&recipe.host);
        // A solved end-member mix re-prefills RI/SG (garnet); other hosts keep what the
        // user has in the optics fields.
        self.pending_prefill = catalogue
            .host(&recipe.host)
            .filter(|h| !h.end_members.is_empty())
            .map(|h| host_prefill(h, Some(&recipe)));
        self.mode.last_recipe = Some(recipe);
        self.summary = Some(SolveSummary {
            capped: result.capped,
        });
        true
    }

    // ---- save / dirty ----

    /// Whether the physics state differs from what the dialog opened with: the mode, or the
    /// recipe (a pristine auto-created blank recipe on a material that had none does not count).
    #[must_use]
    pub fn is_dirty(&self) -> bool {
        if self.fantasy_picked {
            return true;
        }
        if self.mode.active != self.baseline.active {
            return true;
        }
        match (&self.mode.last_recipe, &self.baseline.last_recipe) {
            (Some(a), Some(b)) => a != b,
            (None, None) => false,
            (Some(a), None) => !(a.entries.is_empty() && a.treatments.is_empty()),
            (None, Some(_)) => true,
        }
    }

    /// The JSON the Save button hands over: both payloads, `""` for a material that never had a
    /// recipe and is still plain fantasy. Fantasy-active saves keep the recipe.
    ///
    /// A dialog still exactly as opened over a stored recipe hands the stored text back
    /// verbatim: re-serialising it could change the last digit of an amount, and opening and
    /// saving without a change must not rewrite the material.
    #[must_use]
    pub fn mode_json(&self) -> String {
        if self.is_untouched_stored_recipe() {
            return self.opened_json.clone();
        }
        let blank = |r: &ColorRecipe| r.entries.is_empty() && r.treatments.is_empty();
        match (&self.mode.last_recipe, self.is_physics()) {
            // A free fantasy pick lives only in the payload: it must be saved.
            _ if self.fantasy_picked => self.mode.to_json(),
            (None, false) => String::new(),
            (Some(r), false) if self.baseline.last_recipe.is_none() && blank(r) => String::new(),
            _ => self.mode.to_json(),
        }
    }

    /// The dialog opened over a stored recipe and nothing has changed since (not the mode, the
    /// recipe or the fantasy color).
    fn is_untouched_stored_recipe(&self) -> bool {
        !self.fantasy_picked
            && !self.opened_json.is_empty()
            && self.mode.last_recipe.is_some()
            && self.mode == self.baseline
    }

    // ---- view ----

    /// Everything the dialog shows, derived from the state (and the stored resolved bands).
    #[must_use]
    pub fn view(&self, catalogue: &ChromophoreCatalogue) -> PhysicsView {
        let host = catalogue.host(&self.host_id);
        let recipe = self.recipe();
        let mut view = PhysicsView {
            active: self.is_physics(),
            dirty: self.is_dirty(),
            mode_json: self.mode_json(),
            host_names: catalogue.hosts.iter().map(|h| h.name.clone()).collect(),
            host_index: catalogue
                .hosts
                .iter()
                .position(|h| h.id == self.host_id)
                .unwrap_or(0),
            banner: host.and_then(host_banner),
            data_updated: self.mode.data_outdated(catalogue),
            view_fractions: self.view_fractions,
            strength: recipe.map_or(1.0, |r| r.strength),
            reference_path_mm: self.reference_path_mm(),
            path_hint: path_hint(self.stone_width_mm),
            solving: self.solving,
            can_undo: self.history.can_undo(),
            can_redo: self.history.can_redo(),
            ..PhysicsView::default()
        };
        let (Some(host), Some(recipe)) = (host, recipe) else {
            return view;
        };
        view.optics_locked = true;
        self.fill_rows(&mut view, catalogue, host, recipe);
        self.fill_colors(&mut view, recipe);
        fill_glow(&mut view, catalogue, recipe);
        if self.solving_kind == Some(SolveKind::InitialFromFantasy) {
            view.note_text =
                "Fitting your current fantasy color into this host\u{2026}".to_string();
        } else if self.is_physics() && recipe.entries.is_empty() && !self.solving {
            view.note_text = "Pure host: add elements or pick a color.".to_string();
        }
        view
    }
}

impl PhysicsState {
    /// The recipe rows, the addable items and the valid treatments.
    fn fill_rows(
        &self,
        view: &mut PhysicsView,
        catalogue: &ChromophoreCatalogue,
        host: &HostData,
        recipe: &ColorRecipe,
    ) {
        // Rows.
        let units: Vec<(String, f64)> = recipe
            .entries
            .iter()
            .map(|e| {
                (
                    e.id.clone(),
                    e.amount / typical_amount(host, &e.id).max(1e-12),
                )
            })
            .collect();
        let total: f64 = units.iter().map(|(_, u)| u).sum();
        for e in &recipe.entries {
            let unit = item_unit(host, &e.id);
            let max = host.element_conc_max(&e.id);
            let (confidence, sources) = item_sources(host, &e.id);
            let share = units
                .iter()
                .find(|(k, _)| *k == e.id)
                .map_or(0.0, |(_, u)| if total > 0.0 { u / total } else { 0.0 });
            let shown = if self.view_fractions {
                e.amount * recipe.strength
            } else {
                e.amount
            };
            view.rows.push(RowView {
                id: e.id.clone(),
                label: catalogue
                    .end_member(&e.id)
                    .map_or_else(|| e.id.clone(), |m| m.name.clone()),
                amount_text: format_amount(shown, &unit),
                unit: unit_label(&unit),
                slider: amount_to_pos(e.amount, max),
                fraction: share,
                fraction_text: format!("{:.0} %", share * 100.0),
                locked: self.locked.contains(&e.id),
                estimated: matches!(confidence.as_str(), "estimate" | "unknown"),
                confidence,
                sources,
            });
        }
        // Add options: every offered id not in the recipe; impossible ones greyed.
        for id in catalogue.selectable_elements(&self.host_id) {
            if recipe.entries.iter().any(|e| e.id == id) {
                continue;
            }
            let reason = impossible_reason(host, recipe, &id);
            view.add_options.push(AddOptionView {
                label: catalogue
                    .end_member(&id)
                    .map_or_else(|| id.clone(), |m| m.name.clone()),
                id,
                enabled: reason.is_none(),
                reason: reason.unwrap_or_default(),
            });
        }
        // Treatments valid for the present elements.
        let present: Vec<&str> = recipe
            .entries
            .iter()
            .filter(|e| e.amount > 0.0)
            .map(|e| e.id.as_str())
            .collect();
        for t in catalogue.selectable_treatments(&self.host_id, &present) {
            view.treatments.push(TreatmentView {
                id: t.id.clone(),
                name: t.name.clone(),
                conditions: t.conditions.clone(),
                active: recipe.treatments.contains(&t.id),
            });
        }
    }

    /// The swatches (from the stored bands) and the readout against the picked color.
    fn fill_colors(&self, view: &mut PhysicsView, recipe: &ColorRecipe) {
        // Swatches from the stored bands.
        let tensor = recipe.resolved_bands.to_tensor();
        let path = f64::from(recipe.reference_path_mm);
        let d65 = body_colors(&tensor, path, Illuminant::D65);
        let a = body_colors(&tensor, path, Illuminant::Planckian(3200.0));
        let delta_cc = delta_e_2000(d65.unpolarised.lab, a.unpolarised.lab);
        view.color_change_text = if delta_cc > COLOR_CHANGE_DELTA_E {
            format!("color change \u{394}E {delta_cc:.1}")
        } else {
            format!("\u{394}E {delta_cc:.1} between D65 and 3200 K (no color change)")
        };
        view.has_beta = d65.beta_ray.is_some();
        view.d65 = Some(mode_swatches(&d65));
        view.a = Some(mode_swatches(&a));
        let stone_path = f64::from(default_reference_path_mm(self.stone_width_mm));
        if self.stone_width_mm > 0.0 && (stone_path - path).abs() > 0.05 {
            view.stone = Some((
                body_colors(&tensor, stone_path, Illuminant::D65)
                    .unpolarised
                    .srgb,
                body_colors(&tensor, stone_path, Illuminant::Planckian(3200.0))
                    .unpolarised
                    .srgb,
            ));
            view.stone_text = format!("At this stone's size ({stone_path:.1} mm)");
        }
        // The pick.
        if let Some(target) = self.target {
            let de = delta_e_2000(target.lab, d65.unpolarised.lab);
            view.target = Some(target.srgb);
            view.match_warn = de > WARN_DELTA_E;
            view.match_text = if de <= REACHABLE_DELTA_E {
                format!("\u{394}E {de:.1} from your pick (D65)")
            } else {
                format!("\u{394}E {de:.1} from your pick (D65) \u{b7} closest reachable shown")
            };
            if self.summary.is_some_and(|s| s.capped) {
                view.match_text.push_str(" \u{b7} search capped");
            }
        }
    }
}

/// The fluorescence readout and the two analytic UV glow swatches (fluorescence plan section 5),
/// from the recipe's emitters (`fluorescence_report`) at its reference path: nothing is traced.
fn fill_glow(view: &mut PhysicsView, catalogue: &ChromophoreCatalogue, recipe: &ColorRecipe) {
    let report = fluorescence_report(catalogue, recipe);
    let path = recipe.reference_path_mm;
    let (glow365, glow395) = (report.glow(UV365_NM, path), report.glow(UV395_NM, path));
    let swatch = |glow: &UvGlow| GlowSwatch {
        srgb: glow.srgb.map(|c| (c.clamp(0.0, 1.0) * 255.0).round() as u8),
        level: (glow.photon_yield / GLOW_LEVEL_FULL).clamp(0.0, 1.0),
        text: glow_caption(glow),
    };
    // The readout names the stronger of the two lamps.
    let best = if glow395.photon_yield > glow365.photon_yield {
        &glow395
    } else {
        &glow365
    };
    view.fluorescence_text = match (best.strength(), best.color_name()) {
        (GlowStrength::None, _) | (_, None) => "Fluorescence: none".to_string(),
        (GlowStrength::Weak, Some(c)) => format!("Fluorescence: weak {c}"),
        (GlowStrength::Strong, Some(c)) => format!("Fluorescence: strong {c}"),
    };
    view.quench_text = report
        .dominant_quencher()
        .map(|by| format!("quenched by {by}"))
        .unwrap_or_default();
    view.glow = (!report.fluorescence.is_empty()).then(|| [swatch(&glow365), swatch(&glow395)]);
}

/// Photon yield (emitted per lamp photon) shown as a full brightness bar.
const GLOW_LEVEL_FULL: f32 = 0.5;

/// "relative brightness 42 %" caption of a glow swatch.
fn glow_caption(glow: &UvGlow) -> String {
    format!(
        "{:.0} %",
        (glow.photon_yield / GLOW_LEVEL_FULL).clamp(0.0, 1.0) * 100.0
    )
}

fn path_hint(stone_width_mm: f32) -> String {
    if stone_width_mm > 0.0 {
        format!(
            "equivalent path (default {GIRDLE_PATH_FACTOR} \u{d7} girdle {stone_width_mm:.1} mm)"
        )
    } else {
        "equivalent path (no design size: default 5 mm)".to_string()
    }
}

/// Re-resolves `recipe` with `catalogue` and stores the budgeted bands: amounts are clamped
/// to the host's range first, `data_version` is the catalogue's. A failing resolve (an
/// impossible end-member sum) keeps the previous bands.
pub fn refresh(recipe: &mut ColorRecipe, catalogue: &ChromophoreCatalogue) {
    recipe.clamp_amounts(catalogue);
    recipe.data_version = catalogue.data_version;
    if let Ok((tensor, _)) = resolve(recipe, catalogue) {
        recipe.resolved_bands = ResolvedBands::from_tensor(&tensor);
    }
}

/// One swatch row: unpolarised, o-ray, e-ray and (biaxial) beta-ray sRGB.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct ModeSwatches {
    /// Unpolarised color.
    pub unpol: [u8; 3],
    /// Ordinary ray.
    pub o: [u8; 3],
    /// Extraordinary ray.
    pub e: [u8; 3],
    /// Beta ray of a biaxial host.
    pub beta: Option<[u8; 3]>,
}

fn mode_swatches(c: &BodyColors) -> ModeSwatches {
    let rgb = |b: &BodyColor| b.srgb;
    ModeSwatches {
        unpol: rgb(&c.unpolarised),
        o: rgb(&c.o_ray),
        e: rgb(&c.e_ray),
        beta: c.beta_ray.as_ref().map(rgb),
    }
}

/// One UV-lamp glow swatch: the emitted color, its relative brightness and a caption.
#[derive(Debug, Clone, PartialEq)]
pub struct GlowSwatch {
    /// Display color (the glow's color, darker for a dim glow).
    pub srgb: [u8; 3],
    /// Relative brightness `0..=1` (photon yield against a bright glow).
    pub level: f32,
    /// "42 %" caption.
    pub text: String,
}

/// One recipe row.
#[derive(Debug, Clone, PartialEq)]
pub struct RowView {
    /// Selectable id.
    pub id: String,
    /// Display label.
    pub label: String,
    /// Amount with unit.
    pub amount_text: String,
    /// Unit label.
    pub unit: String,
    /// Log slider position.
    pub slider: f64,
    /// Share in the fractions view.
    pub fraction: f64,
    /// Share text.
    pub fraction_text: String,
    /// Locked for the solver.
    pub locked: bool,
    /// The data are an estimate or unknown.
    pub estimated: bool,
    /// Worst confidence behind the item.
    pub confidence: String,
    /// Sources popover text.
    pub sources: String,
}

/// One "+ Add" entry.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct AddOptionView {
    /// Selectable id.
    pub id: String,
    /// Display label.
    pub label: String,
    /// Whether it can be added now.
    pub enabled: bool,
    /// Why not.
    pub reason: String,
}

/// One treatment checkbox.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct TreatmentView {
    /// Treatment id.
    pub id: String,
    /// Display name.
    pub name: String,
    /// Conditions text.
    pub conditions: String,
    /// Whether it is on.
    pub active: bool,
}

/// Everything the physics section shows, as plain data.
#[derive(Debug, Clone, Default, PartialEq)]
pub struct PhysicsView {
    /// Physics is the active mode.
    pub active: bool,
    /// The state differs from what the dialog opened with.
    pub dirty: bool,
    /// The JSON Save hands over.
    pub mode_json: String,
    /// Host display names (catalogue order).
    pub host_names: Vec<String>,
    /// Selected host.
    pub host_index: usize,
    /// The data-confidence banner, if the host has non-verified coefficients.
    pub banner: Option<String>,
    /// The recipe predates the catalogue's data version.
    pub data_updated: bool,
    /// Recipe rows.
    pub rows: Vec<RowView>,
    /// Addable items.
    pub add_options: Vec<AddOptionView>,
    /// Valid treatments.
    pub treatments: Vec<TreatmentView>,
    /// Strength + fractions view.
    pub view_fractions: bool,
    /// Recipe strength.
    pub strength: f64,
    /// Reference path.
    pub reference_path_mm: f32,
    /// Path label/hint.
    pub path_hint: String,
    /// D65 swatches.
    pub d65: Option<ModeSwatches>,
    /// 3200 K swatches.
    pub a: Option<ModeSwatches>,
    /// A biaxial host (beta swatches shown).
    pub has_beta: bool,
    /// "At this stone's size" D65 and 3200 K colors.
    pub stone: Option<([u8; 3], [u8; 3])>,
    /// Label of the stone-size row.
    pub stone_text: String,
    /// color-change readout.
    pub color_change_text: String,
    /// The UV 365 nm and UV 395 nm glow swatches; `None` for a recipe without emitters.
    pub glow: Option<[GlowSwatch; 2]>,
    /// "Fluorescence: none / weak / strong <color>".
    pub fluorescence_text: String,
    /// "quenched by Fe" when a quencher took more than half of the yield, else empty.
    pub quench_text: String,
    /// The picked color.
    pub target: Option<[u8; 3]>,
    /// "dE x from your pick ..." text.
    pub match_text: String,
    /// dE above the warning threshold.
    pub match_warn: bool,
    /// A solve is running.
    pub solving: bool,
    /// Short note.
    pub note_text: String,
    /// Undo available.
    pub can_undo: bool,
    /// Redo available.
    pub can_redo: bool,
    /// The host fixes crystal system and optical character.
    pub optics_locked: bool,
}

#[cfg(test)]
mod tests;