azul-layout 0.0.8

Layout solver + font and image loader the Azul GUI framework
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
//! Final positioning of layout nodes (relative, absolute, and fixed schemes)
// +spec:positioning:79d47e - Implements relative, absolute, and fixed positioning schemes

use std::collections::BTreeMap;

use azul_core::{
    dom::{NodeId, NodeType},
    geom::{LogicalPosition, LogicalRect, LogicalSize},
    hit_test::ScrollPosition,
    resources::RendererResources,
    styled_dom::StyledDom,
};
use azul_css::{
    corety::LayoutDebugMessage,
    css::CssPropertyValue,
    props::{
        basic::pixel::PixelValue,
        layout::{LayoutPosition, LayoutWritingMode},
        property::{CssProperty, CssPropertyType},
    },
};

use crate::{
    font_traits::{FontLoaderTrait, ParsedFontTrait},
    solver3::{
        fc::{layout_formatting_context, LayoutConstraints, TextAlign},
        getters::{
            get_aspect_ratio_property, get_direction_property, get_display_property, get_writing_mode, get_position, MultiValue,
            get_css_top, get_css_bottom, get_css_left, get_css_right,
            get_css_height, get_css_width,
        },
        layout_tree::LayoutTree,
        LayoutContext, LayoutError, Result,
    },
};

#[derive(Debug, Default)]
struct PositionOffsets {
    top: Option<f32>,
    right: Option<f32>,
    bottom: Option<f32>,
    left: Option<f32>,
}

// +spec:positioning:94ef0f - position property: static|relative|absolute|sticky|fixed, initial static, applies to all elements except table-column-group/table-column
/// Looks up the `position` property using the compact-cache-aware getter.
// +spec:positioning:ba937d - positioned elements have position != static
pub fn get_position_type(styled_dom: &StyledDom, dom_id: Option<NodeId>) -> LayoutPosition {
    let Some(id) = dom_id else {
        return LayoutPosition::Static;
    };
    let node_state = &styled_dom.styled_nodes.as_container()[id].styled_node_state;
    get_position(styled_dom, id, node_state).unwrap_or_default()
}

// +spec:positioning:bda1d5 - resolves inset properties (top/right/bottom/left) as inward offsets per CSS Position 3 §3.1
// +spec:positioning:bf9168 - resolves inset properties (top/right/bottom/left) to control positioned box location
// +spec:positioning:f8e0a1 - inset properties (top/right/bottom/left) resolved for positioned elements; auto = unconstrained
/// Reads and resolves `top`, `right`, `bottom`, `left` properties,
/// including percentages relative to the containing block's size, and em/rem units.
// +spec:positioning:7ec143 - top/right/bottom/left offset resolution with percentage against containing block
fn resolve_position_offsets(
    styled_dom: &StyledDom,
    dom_id: Option<NodeId>,
    cb_size: LogicalSize,
) -> PositionOffsets {
    use azul_css::props::basic::pixel::{PhysicalSize, PropertyContext, ResolutionContext};

    use crate::solver3::getters::{
        get_element_font_size, get_parent_font_size, get_root_font_size,
    };

    let Some(id) = dom_id else {
        return PositionOffsets::default();
    };
    let node_state = &styled_dom.styled_nodes.as_container()[id].styled_node_state;

    // Create resolution context with font sizes and containing block size
    let element_font_size = get_element_font_size(styled_dom, id, node_state);
    let parent_font_size = get_parent_font_size(styled_dom, id, node_state);
    let root_font_size = get_root_font_size(styled_dom, node_state);

    let containing_block_size = PhysicalSize::new(cb_size.width, cb_size.height);

    let resolution_context = ResolutionContext {
        element_font_size,
        parent_font_size,
        root_font_size,
        containing_block_size,
        element_size: None, // Not needed for position offsets
        viewport_size: PhysicalSize::new(0.0, 0.0),
    };

    let mut offsets = PositionOffsets::default();

    // +spec:containing-block:d4b3b9 - percentage offsets resolve against CB width (left/right) or height (top/bottom)
    // Resolve offsets using compact-cache-aware getters
    // top/bottom use Height context (% refers to containing block height)
    offsets.top = match get_css_top(styled_dom, id, node_state) {
        MultiValue::Exact(pv) => Some(pv.resolve_with_context(&resolution_context, PropertyContext::Height)),
        _ => None,
    };

    offsets.bottom = match get_css_bottom(styled_dom, id, node_state) {
        MultiValue::Exact(pv) => Some(pv.resolve_with_context(&resolution_context, PropertyContext::Height)),
        _ => None,
    };

    // left/right use Width context (% refers to containing block width)
    offsets.left = match get_css_left(styled_dom, id, node_state) {
        MultiValue::Exact(pv) => Some(pv.resolve_with_context(&resolution_context, PropertyContext::Width)),
        _ => None,
    };

    offsets.right = match get_css_right(styled_dom, id, node_state) {
        MultiValue::Exact(pv) => Some(pv.resolve_with_context(&resolution_context, PropertyContext::Width)),
        _ => None,
    };

    offsets
}

// +spec:block-formatting-context:f5f992 - Out-of-flow: floated or absolutely positioned boxes laid out outside normal flow
// +spec:positioning:bb19f8 - absolute/fixed positioning: out-of-flow, positioned relative to containing block/viewport
/// After the main layout pass, this function iterates through the tree and correctly
/// calculates the final positions of out-of-flow elements (`absolute`, `fixed`).
// +spec:positioning:5bfef3 - abspos elements use static position for auto offsets, resolve against nearest positioned ancestor CB
// +spec:positioning:7fff75 - Absolute positioning: removed from flow, offset relative to containing block, establishes new CB
// +spec:positioning:839cbb - absolute elements positioned/sized solely relative to their containing block, modified by inset properties
// +spec:positioning:898590 - absolute positioning takes elements out of flow and positions them relative to containing block
// +spec:positioning:c37c1b - abspos boxes laid out in containing block after its final size is determined
// +spec:positioning:cbe481 - absolute positioning removes elements from flow and positions them relative to containing block
// +spec:positioning:ebff77 - absolute positioning layout model (replaces old §6 abspos model)
// +spec:positioning:3b3ba4 - Absolute positioning: box offset from containing block, removed from normal flow; fixed positioning: CB = viewport
pub fn position_out_of_flow_elements<T: ParsedFontTrait>(
    ctx: &mut LayoutContext<'_, T>,
    tree: &mut LayoutTree,
    calculated_positions: &mut super::PositionVec,
    viewport: LogicalRect,
) -> Result<()> {
    for node_index in 0..tree.nodes.len() {
        let node = &tree.nodes[node_index];
        let dom_id = match node.dom_node_id {
            Some(id) => id,
            None => continue,
        };

        let position_type = get_position_type(ctx.styled_dom, Some(dom_id));

        // +spec:positioning:1d87f6 - Fixed/absolute positioning schemes with box offset resolution (top/right/bottom/left)
        // +spec:positioning:8bde1d - absolute: out of flow, positioned by containing block
        // +spec:positioning:c11be9 - absolute positioning: effect of box offsets depends on which properties are auto (non-replaced) or intrinsic dimensions (replaced)
        // +spec:positioning:9020aa - "absolutely positioned" means position:absolute or position:fixed
        if position_type == LayoutPosition::Absolute || position_type == LayoutPosition::Fixed {
            // is a grid container have their CB determined by grid-placement properties;
            // Taffy already handles this during grid layout, so skip re-positioning here.
            // Same applies to flex containers (Flexbox §4.1).
            {
                use azul_core::dom::FormattingContext;
                let parent_is_flex_or_grid = node.parent.and_then(|p| tree.get(p)).map_or(false, |pn| {
                    matches!(pn.formatting_context, FormattingContext::Flex | FormattingContext::Grid)
                });
                if parent_is_flex_or_grid {
                    continue;
                }
            }

            // Get parent info before any mutable borrows
            let parent_info: Option<(usize, LogicalPosition, f32, f32, f32, f32)> = {
                let node = &tree.nodes[node_index];
                node.parent.and_then(|parent_idx| {
                    let parent_node = tree.get(parent_idx)?;
                    let parent_dom_id = parent_node.dom_node_id?;
                    let parent_position = get_position_type(ctx.styled_dom, Some(parent_dom_id));
                    if parent_position == LayoutPosition::Absolute
                        || parent_position == LayoutPosition::Fixed
                    {
                        calculated_positions.get(parent_idx).map(|parent_pos| {
                            let pbp = parent_node.box_props.unpack();
                            (
                                parent_idx,
                                *parent_pos,
                                pbp.border.left,
                                pbp.border.top,
                                pbp.padding.left,
                                pbp.padding.top,
                            )
                        })
                    } else {
                        None
                    }
                })
            };

            // +spec:containing-block:17a946 - fixed boxes use viewport as containing block
            // +spec:containing-block:83a32a - fixed positioning: containing block is viewport; absolute: nearest positioned ancestor or initial CB
            // +spec:containing-block:9b617d - fixed elements use viewport (initial fixed containing block)
            // +spec:containing-block:899e47 - fixed elements use viewport (initial fixed containing block)
            // +spec:containing-block:faa9a3 - fixed positioning falls back to initial containing block (viewport) when no ancestor establishes one
            // +spec:containing-block:faa9a3 - fixed positioning CB falls back to initial containing block (viewport) when no ancestor establishes one
            // +spec:positioning:067eab - CB for fixed = viewport, for absolute = nearest positioned ancestor
            // +spec:positioning:067eab - fixed CB is viewport; absolute CB is nearest positioned ancestor's padding-box
            // +spec:positioning:9777da - fixed positioning uses viewport as containing block
            // +spec:positioning:9777da - Fixed positioning uses viewport as containing block
            // +spec:positioning:9ccf9a - fixed-position CB is viewport (transform/will-change/contain could override, not yet implemented)
            // +spec:positioning:a68970 - fixed positioning uses viewport as containing block
            // +spec:positioning:8fff44 - fixed: same as absolute but positioned relative to viewport
            // +spec:positioning:744713 - fixed position uses viewport as containing block
            // +spec:positioning:f0ad47 - fixed elements use viewport as containing block; content outside viewport cannot be scrolled to
            // +spec:containing-block:df8387 - fixed positioning: containing block is the viewport
            let containing_block_rect = if position_type == LayoutPosition::Fixed {
                viewport
            } else {
                find_absolute_containing_block_rect(
                    tree,
                    node_index,
                    ctx.styled_dom,
                    calculated_positions,
                    viewport,
                )?
            };

            // Get node again after containing block calculation
            let node = &tree.nodes[node_index];

            // Calculate used size for out-of-flow elements (they don't get sized during normal
            // layout)
            let element_size = if let Some(size) = node.used_size {
                size
            } else {
                // Element hasn't been sized yet - calculate it now using containing block
                let intrinsic = tree.warm(node_index).and_then(|w| w.intrinsic_sizes).unwrap_or_default();
                let size = crate::solver3::sizing::calculate_used_size_for_node(
                    ctx.styled_dom,
                    Some(dom_id),
                    containing_block_rect.size,
                    intrinsic,
                    &node.box_props.unpack(),
                    ctx.viewport_size,
                )?;

                // Store the calculated size in the tree node
                if let Some(node_mut) = tree.get_mut(node_index) {
                    node_mut.used_size = Some(size);
                }

                size
            };

            // +spec:positioning:dc23fa - sizing/positioning into inset-modified containing block (§4)
            // +spec:positioning:623e45 - inset properties reduce the containing block into the inset-modified containing block
            // Resolve offsets using the now-known containing block size.
            let offsets =
                resolve_position_offsets(ctx.styled_dom, Some(dom_id), containing_block_rect.size);

            // +spec:box-model:ae3899 - static position is the margin-edge position from normal flow
            // +spec:positioning:9a90a3 - static position: the position the element would have had in normal flow
            // +spec:positioning:ca3e89 - static-position rectangle uses block-start inline-start alignment (CSS2.1 hypothetical box)
            let mut static_pos = calculated_positions
                .get(node_index)
                .copied()
                .unwrap_or_default();

            // Special case: If this is a fixed-position element and it has a positioned
            // parent, update static_pos to be relative to the parent's final absolute
            // position (content-box). The initial static_pos from process_out_of_flow_children
            // may include border/padding offsets, so we must always recalculate here.
            if position_type == LayoutPosition::Fixed {
                if let Some((_, parent_pos, border_left, border_top, padding_left, padding_top)) =
                    parent_info
                {
                    // Add parent's border and padding to get content-box position
                    static_pos = LogicalPosition::new(
                        parent_pos.x + border_left + padding_left,
                        parent_pos.y + border_top + padding_top,
                    );
                }
            }

            let mut final_pos = LogicalPosition::zero();

            // +spec:box-model:ea2f43 - top + margin + border + padding + height + bottom = CB height
            // +spec:box-model:b4f5b3 - vertical constraint equation for abs-pos non-replaced elements
            // +spec:positioning:16d82c - vertical dimension constraint for abs-positioned non-replaced elements
            // +spec:positioning:8f474b - §10.6.4 vertical constraint for absolutely positioned non-replaced elements
            // +spec:positioning:50218d - absolute: top margin edge offset below containing block top edge
            // top + margin-top + border-top + padding-top + height + padding-bottom +
            // border-bottom + margin-bottom + bottom = containing block height
            let node_state = &ctx.styled_dom.styled_nodes.as_container()[dom_id].styled_node_state;

            // Extract all box_props values upfront to avoid borrow conflicts with tree.get_mut()
            let (margin_top_val, margin_bottom_val, margin_auto,
                 margin_left_val, margin_right_val, margin_left_auto_flag, margin_right_auto_flag) = {
                let node = &tree.nodes[node_index];
                let nbp = node.box_props.unpack();
                (nbp.margin.top, nbp.margin.bottom,
                 nbp.margin_auto,
                 nbp.margin.left, nbp.margin.right,
                 nbp.margin_auto.left, nbp.margin_auto.right)
            };
            // +spec:positioning:d730e5 - CB height is independent of the abspos element, so percentage heights always resolve
            let cb_height = containing_block_rect.size.height;

            let css_height = get_css_height(ctx.styled_dom, dom_id, node_state);
            // +spec:replaced-elements:7d8ba8 - §10.6.5: for absolutely positioned replaced
            // elements, height is determined first (as for inline replaced elements), so treat
            // it as "not auto" in the constraint equation even if CSS says auto.
            let node_data = &ctx.styled_dom.node_data.as_container()[dom_id];
            let is_replaced = matches!(node_data.node_type, NodeType::Image(_))
                || node_data.is_virtual_view_node();
            let height_is_auto = css_height.is_auto() && !is_replaced;
            // +spec:overflow:941a06 - resolve auto inset properties: if only one is auto, solved to zero via constraint; if both auto, use static position
            let top_is_auto = offsets.top.is_none();
            let bottom_is_auto = offsets.bottom.is_none();

            // element_size is border-box (includes border + padding + content).
            // The constraint equation is:
            //   top + margin-top + border-box-height + margin-bottom + bottom = CB height
            // (border-top, padding-top, content-height, padding-bottom, border-bottom
            //  are all inside border-box-height)
            let mut used_height = element_size.height;
            // +spec:height-calculation:44939a - set auto values for margin-top/margin-bottom to 0
            // +spec:height-calculation:2f6e10 - if bottom is auto, replace auto margin-top/margin-bottom with 0
            let mut used_margin_top = if margin_auto.top { 0.0 } else { margin_top_val };
            let mut used_margin_bottom = if margin_auto.bottom { 0.0 } else { margin_bottom_val };

            // +spec:box-model:3a9c2a - resolving auto insets: static position fallback when insets are auto
            // +spec:box-model:bd442c - weaker inset resolves to align margin box with inset-modified CB edge
            // +spec:height-calculation:93e91c - abs non-replaced height: auto margin centering, single auto margin solve, over-constrained ignore bottom
            // +spec:positioning:6e7732 - §10.6.4 vertical constraint equation for abspos non-replaced elements
            // +spec:positioning:b63d0f - absolute positioning with top:auto uses static position (change bars example)
            // +spec:positioning:da8a0c - resolving auto insets: normal alignment treated as start, so auto insets resolve to static position
            // +spec:positioning:820b22 - 10.6.4: absolutely positioned non-replaced elements vertical constraint equation and 6 rules
            if top_is_auto && height_is_auto && bottom_is_auto {
                // +spec:positioning:08e0ac - absolute element with top:auto uses static position (current line)
                // +spec:positioning:aab294 - both inset properties auto: resolve to static position
                // +spec:positioning:d9bb3c - hypothetical position: UA may guess static position rather than fully computing hypothetical box
                // All three auto: set top to static position, height from content, solve for bottom
                // +spec:height-calculation:51627d - auto margins to 0, top = static position, height from content (rule 3)
                // +spec:positioning:460f2f - All three auto: set top to static position, height from content, solve for bottom
                final_pos.y = static_pos.y;
            } else if !top_is_auto && !height_is_auto && !bottom_is_auto {
                // +spec:overflow:fc0c9e - over-constrained abspos: auto margins minimize overflow (CSS2.1 equivalent of Box Alignment 3 safe alignment)
                // +spec:positioning:88f760 - auto margins of absolutely-positioned boxes (vertical)
                // None are auto: over-constrained case
                // +spec:height-calculation:03c071 - none auto: equal auto margins, solve single auto margin, or ignore bottom if over-constrained
                let top_val = offsets.top.unwrap();
                let bottom_val = offsets.bottom.unwrap();
                if margin_auto.top && margin_auto.bottom {
                    // +spec:height-calculation:5112a4 - both margin-top/bottom auto: solve with equal values
                    let available = cb_height - top_val - used_height - bottom_val;
                    let each = available / 2.0;
                    used_margin_top = each;
                    used_margin_bottom = each;
                } else if margin_auto.top {
                    used_margin_top = cb_height - top_val - used_height - used_margin_bottom - bottom_val;
                } else if margin_auto.bottom {
                    used_margin_bottom = cb_height - top_val - used_height - used_margin_top - bottom_val;
                }
                // else: over-constrained, ignore bottom
                final_pos.y = containing_block_rect.origin.y + top_val + used_margin_top;
            } else if top_is_auto && height_is_auto && !bottom_is_auto {
                // +spec:height-calculation:909b50 - top and height auto, bottom not auto: height from BFC auto heights, solve for top
                // Rule 1: height from content, auto margins to 0, solve for top
                let bottom_val = offsets.bottom.unwrap();
                let top_val = cb_height - used_margin_top - used_height - used_margin_bottom - bottom_val;
                final_pos.y = containing_block_rect.origin.y + top_val + used_margin_top;
            } else if top_is_auto && bottom_is_auto && !height_is_auto {
                // +spec:positioning:64e1ba - top+bottom auto, height not auto: set top to static position, solve for bottom
                final_pos.y = static_pos.y;
            } else if height_is_auto && bottom_is_auto && !top_is_auto {
                // Rule 3: height from content, auto margins to 0, solve for bottom
                let top_val = offsets.top.unwrap();
                final_pos.y = containing_block_rect.origin.y + top_val + used_margin_top;
            } else if top_is_auto && !height_is_auto && !bottom_is_auto {
                // +spec:height-calculation:33dce8 - top auto, height and bottom not auto: solve for top
                // Rule 4: auto margins to 0, solve for top
                let bottom_val = offsets.bottom.unwrap();
                let top_val = cb_height - used_margin_top - used_height - used_margin_bottom - bottom_val;
                final_pos.y = containing_block_rect.origin.y + top_val + used_margin_top;
            } else if height_is_auto && !top_is_auto && !bottom_is_auto {
                // +spec:intrinsic-sizing:566a43 - abspos auto height with non-auto insets: stretch-fit size
                // +spec:intrinsic-sizing:c7227f - except: if box has aspect-ratio, ratio-dependent axis uses max-content
                let has_aspect_ratio = matches!(
                    get_aspect_ratio_property(ctx.styled_dom, dom_id, node_state),
                    MultiValue::Exact(azul_css::props::style::effects::StyleAspectRatio::Ratio(_))
                );
                let top_val = offsets.top.unwrap();
                let bottom_val = offsets.bottom.unwrap();
                if !has_aspect_ratio {
                    // solve for height from constraint equation (stretch-fit):
                    // height = cb_height - top - margin_top - margin_bottom - bottom
                    // +spec:containing-block:b3f0dd - clamp effective CB size to zero when insets exceed it (weaker inset reduced)
                    used_height = (cb_height - top_val - used_margin_top - used_margin_bottom - bottom_val).max(0.0);
                }
                // else: keep content-based height (max-content) per aspect-ratio exception
                final_pos.y = containing_block_rect.origin.y + top_val + used_margin_top;
                // Update the element size with the resolved height
                if let Some(node_mut) = tree.get_mut(node_index) {
                    if let Some(ref mut size) = node_mut.used_size {
                        size.height = used_height;
                    }
                }
            } else if bottom_is_auto && !top_is_auto && !height_is_auto {
                // Rule 6: auto margins to 0, solve for bottom
                let top_val = offsets.top.unwrap();
                final_pos.y = containing_block_rect.origin.y + top_val + used_margin_top;
            } else {
                // Fallback to static position
                final_pos.y = static_pos.y;
            }

            // +spec:box-model:984243 - horizontal constraint equation for abs-pos non-replaced elements
            // +spec:positioning:3be194 - position abs replaced element after establishing width
            // Constraint: left + margin-left + border-left + padding-left + width +
            // +spec:width-calculation:1661b4 - constraint equation and six rules for abs-pos horizontal (§10.3.7)
            // left + margin-left + border-left + padding-left + width +
            //   padding-right + border-right + margin-right + right = CB width
            // Since element_size.width is border-box (border + padding + content),
            // simplifies to: left + margin-left + border_box_width + margin-right + right = CB width
            {
                let margin_left = margin_left_val;
                let margin_right = margin_right_val;
                let margin_left_auto = margin_left_auto_flag;
                let margin_right_auto = margin_right_auto_flag;
                let cb_width = containing_block_rect.size.width;
                let border_box_width = element_size.width;
                let left_val = offsets.left;
                let right_val = offsets.right;
                let left_is_auto = left_val.is_none();
                let right_is_auto = right_val.is_none();

                // Get direction of containing block for over-constrained resolution
                use azul_css::props::style::StyleDirection;
                let cb_direction = {
                    let cb_dom_id = if position_type == LayoutPosition::Fixed {
                        None // viewport CB, default LTR
                    } else {
                        let mut parent = tree.nodes[node_index].parent;
                        let mut found = None;
                        while let Some(pidx) = parent {
                            if let Some(pnode) = tree.get(pidx) {
                                if get_position_type(ctx.styled_dom, pnode.dom_node_id).is_positioned() {
                                    found = pnode.dom_node_id;
                                    break;
                                }
                                parent = pnode.parent;
                            } else {
                                break;
                            }
                        }
                        found
                    };
                    match cb_dom_id {
                        Some(cb_id) => {
                            let cb_ns = &ctx.styled_dom.styled_nodes.as_container()[cb_id].styled_node_state;
                            match get_direction_property(ctx.styled_dom, cb_id, cb_ns) {
                                MultiValue::Exact(v) => v,
                                _ => StyleDirection::Ltr,
                            }
                        }
                        None => StyleDirection::Ltr,
                    }
                };

                // +spec:replaced-elements:7d8ba8 - §10.3.8: for absolutely positioned replaced elements, width is determined
                // first (as for inline replaced), so treat as "not auto" in the constraint.
                let width_is_auto = get_css_width(ctx.styled_dom, dom_id, node_state).is_auto() && !is_replaced;

                if !left_is_auto && !width_is_auto && !right_is_auto {
                    // +spec:positioning:88f760 - auto margins of absolutely-positioned boxes (horizontal)
                    // +spec:width-calculation:942c77 - abs-pos non-replaced width: auto margins, over-constrained resolution
                    // None of left/width/right are auto — solve for margins or handle over-constrained
                    // +spec:width-calculation:dff69d - §10.3.7 abs-pos non-replaced: none auto → equal auto margins, solve single auto margin, or over-constrained
                    let left = left_val.unwrap();
                    let right = right_val.unwrap();
                    let remaining = cb_width - left - border_box_width - right;

                    // +spec:writing-modes:9c3b40 - abspos auto margins: if negative remaining in inline axis, start margin=0, end margin gets remainder
                    if margin_left_auto && margin_right_auto {
                        // +spec:positioning:ab47b3 - auto margins can be negative in absolute positioning
                        // Both margins auto: equal values unless negative
                        let each_margin = remaining / 2.0;
                        if each_margin < 0.0 {
                            match cb_direction {
                                StyleDirection::Ltr => {
                                    final_pos.x = containing_block_rect.origin.x + left;
                                }
                                StyleDirection::Rtl => {
                                    final_pos.x = containing_block_rect.origin.x + left + remaining;
                                }
                            }
                        } else {
                            final_pos.x = containing_block_rect.origin.x + left + each_margin;
                        }
                    } else if margin_left_auto {
                        let solved_margin_left = remaining - margin_right;
                        final_pos.x = containing_block_rect.origin.x + left + solved_margin_left;
                    } else if margin_right_auto {
                        final_pos.x = containing_block_rect.origin.x + left + margin_left;
                    } else {
                        // Over-constrained: ignore right (LTR) or left (RTL)
                        match cb_direction {
                            StyleDirection::Ltr => {
                                final_pos.x = containing_block_rect.origin.x + left + margin_left;
                            }
                            StyleDirection::Rtl => {
                                let solved_left = cb_width - margin_left - border_box_width - margin_right - right;
                                final_pos.x = containing_block_rect.origin.x + solved_left + margin_left;
                            }
                        }
                    }
                } else {
                    // +spec:overflow:f323cb - auto inset: align margin box to stronger inset edge (may overflow CB)
                    // +spec:width-calculation:bbf97a - set auto margins to 0 for abspos when left/width/right has auto
                    // Set auto margins to 0, apply six rules
                    // +spec:box-model:2da091 - if either inset is auto, auto margins resolve to zero
                    // +spec:intrinsic-sizing:087b57 - abspos auto margins resolve to 0 when any inset is auto
                    // +spec:width-calculation:0c29ce - set auto margins to 0, then apply six rules for abs pos width
                    let m_left = if margin_left_auto { 0.0 } else { margin_left };
                    let m_right = if margin_right_auto { 0.0 } else { margin_right };

                    // +spec:width-calculation:2b2852 - all three auto: set auto margins to 0, use static position for left (LTR)
                    // +spec:width-calculation:c120b3 - all three of left/width/right auto: set auto margins to 0, then use direction to pick static position
                    if left_is_auto && width_is_auto && right_is_auto {
                        match cb_direction {
                            StyleDirection::Ltr => {
                                // Set left to static position, apply rule 3 (width from content, solve for right)
                                final_pos.x = static_pos.x;
                            }
                            StyleDirection::Rtl => {
                                // Set right to static position, apply rule 1 (width from content, solve for left)
                                let static_offset = static_pos.x - containing_block_rect.origin.x;
                                let right_static = (cb_width - static_offset - border_box_width).max(0.0);
                                let solved_left = cb_width - m_left - border_box_width - m_right - right_static;
                                final_pos.x = containing_block_rect.origin.x + solved_left + m_left;
                            }
                        }
                    } else if left_is_auto && width_is_auto && !right_is_auto {
                        // left+width auto, right not auto: width from content, solve for left
                        let right = right_val.unwrap();
                        let solved_left = cb_width - m_left - border_box_width - m_right - right;
                        final_pos.x = containing_block_rect.origin.x + solved_left + m_left;
                    } else if left_is_auto && !width_is_auto && right_is_auto {
                        // left+right auto: set left to static position (LTR)
                        final_pos.x = static_pos.x;
                    } else if !left_is_auto && width_is_auto && right_is_auto {
                        // width+right auto: position from left
                        let left = left_val.unwrap();
                        final_pos.x = containing_block_rect.origin.x + left + m_left;
                    } else if left_is_auto && !width_is_auto && !right_is_auto {
                        // left auto: solve for left
                        let right = right_val.unwrap();
                        let solved_left = cb_width - m_left - border_box_width - m_right - right;
                        final_pos.x = containing_block_rect.origin.x + solved_left + m_left;
                    } else if !left_is_auto && width_is_auto && !right_is_auto {
                        // +spec:intrinsic-sizing:566a43 - abspos auto width with non-auto insets: stretch-fit size
                        // +spec:intrinsic-sizing:c7227f - except: if box has aspect-ratio, ratio-dependent axis uses max-content
                        let has_aspect_ratio = matches!(
                            get_aspect_ratio_property(ctx.styled_dom, dom_id, node_state),
                            MultiValue::Exact(azul_css::props::style::effects::StyleAspectRatio::Ratio(_))
                        );
                        let left = left_val.unwrap();
                        let right = right_val.unwrap();
                        if !has_aspect_ratio {
                            // width = cb_width - left - margin_left - margin_right - right
                            let used_width = (cb_width - left - m_left - m_right - right).max(0.0);
                            if let Some(node_mut) = tree.get_mut(node_index) {
                                if let Some(ref mut size) = node_mut.used_size {
                                    size.width = used_width;
                                }
                            }
                        }
                        // else: keep content-based width (max-content) per aspect-ratio exception
                        final_pos.x = containing_block_rect.origin.x + left + m_left;
                    } else if !left_is_auto && !width_is_auto && right_is_auto {
                        // right auto: position from left
                        let left = left_val.unwrap();
                        final_pos.x = containing_block_rect.origin.x + left + m_left;
                    } else {
                        final_pos.x = static_pos.x;
                    }
                }
            }

            super::pos_set(calculated_positions, node_index, final_pos);
        }
    }
    Ok(())
}

// +spec:positioning:5b0d7f - relative positioning: offset from normal flow position, siblings unaffected
// +spec:positioning:8afbe2 - Relative positioning preserves normal flow size and space; only visual offset applied after layout
// +spec:positioning:3502d5 - relative and absolute positioning supported for combined use
// +spec:positioning:b22222 - relative positioning: offset from static position, purely visual effect
// +spec:positioning:b814b6 - relative/absolute/fixed positioning scheme (CSS Positioned Layout Module Level 3)
/// Final pass to shift relatively positioned elements from their static flow position.
// +spec:block-formatting-context:60ccf9 - relative positioning shifts inline boxes as a unit after normal flow
// +spec:display-property:17239f - relative positioning offsets element after normal flow; abspos elements taken out of flow
// +spec:positioning:cbe066 - relative positioning implementation
///
/// Resolves percentage-based offsets for `top`, `left`, etc.
/// For relatively positioned elements, percentages are
/// relative to the dimensions of the parent element's content box.
// +spec:positioning:2d8e15 - relative positioning shifts elements as a unit after normal flow without affecting surrounding content
pub fn adjust_relative_positions<T: ParsedFontTrait>(
    ctx: &mut LayoutContext<'_, T>,
    tree: &LayoutTree,
    calculated_positions: &mut super::PositionVec,
    viewport: LogicalRect, // The viewport is needed if the root element is relative.
) -> Result<()> {
    // Iterate through all nodes. We need the index to modify the position map.
    for node_index in 0..tree.nodes.len() {
        let node = &tree.nodes[node_index];
        let position_type = get_position_type(ctx.styled_dom, node.dom_node_id);

        // +spec:block-formatting-context:faa1cf - static boxes: top/right/bottom/left do not apply
        // Early continue for non-relative positioning
        // +spec:overflow:cfb09a - Sticky positioning uses relative-like offsets, clamped to nearest scrollport at scroll time
        if position_type != LayoutPosition::Relative && position_type != LayoutPosition::Sticky {
            continue;
        }

        // +spec:table-layout:6cb73b - position:relative effect on table elements is undefined; skip them
        // +spec:table-layout:718f91 - relative positioning on table-row/row-group shifts all contents
        {
            use azul_css::props::layout::LayoutDisplay;
            let display = get_display_property(ctx.styled_dom, node.dom_node_id);
            if let MultiValue::Exact(d) = display {
                // +spec:positioning:4614dd - position does not apply to table-column-group or table-column boxes
                // Table-row and row-group elements DO support relative positioning:
                // the shift affects all contents including cells originating in the row.
                // Table-column, table-column-group, table-cell, and table-caption do not.
                if matches!(
                    d,
                    LayoutDisplay::TableColumnGroup
                        | LayoutDisplay::TableColumn
                        | LayoutDisplay::TableCell
                        | LayoutDisplay::TableCaption
                ) {
                    continue;
                }
            }
        }

        // Determine the containing block size for resolving percentages.
        // For `position: relative`, this is the parent's content box size.
        let containing_block_size = node.parent
            .and_then(|parent_idx| tree.get(parent_idx))
            .map(|parent_node| {
                // Get parent's writing mode to correctly calculate its inner (content) size.
                let parent_wm = parent_node.dom_node_id
                    .map(|pid| {
                        let ps = &ctx.styled_dom.styled_nodes.as_container()[pid].styled_node_state;
                        get_writing_mode(ctx.styled_dom, pid, ps).unwrap_or_default()
                    })
                    .unwrap_or_default();
                let parent_used_size = parent_node.used_size.unwrap_or_default();
                parent_node.box_props.inner_size(parent_used_size, parent_wm)
            })
            // The root element is relatively positioned. Its containing block is the viewport.
            .unwrap_or(viewport.size);

        // +spec:positioning:418c74 - inset percentages resolve against containing block size per axis; auto is unconstrained
        let offsets =
            resolve_position_offsets(ctx.styled_dom, node.dom_node_id, containing_block_size);

        // Get a mutable reference to the position and apply the offsets.
        let Some(current_pos) = calculated_positions.get_mut(node_index) else {
            continue;
        };

        let initial_pos = *current_pos;

        // +spec:positioning:5eb813 - relative positioning offsets contents from normal flow position
        // +spec:positioning:a2e5f1 - relative positioning shifts element from static position (vs absolute/float)
        // top/bottom/left/right offsets are applied relative to the static position.
        let mut delta_x = 0.0;
        let mut delta_y = 0.0;

        // +spec:positioning:218b50 - Relative positioning: top=-bottom, left=-right, direction-dependent resolution, top wins over bottom
        // According to CSS 2.1 Section 9.4.3:
        // - For `top` and `bottom`: if both are specified, `top` wins and `bottom` is ignored
        // - For `left` and `right`: depends on direction (ltr/rtl)
        //   - In LTR: if both specified, `left` wins and `right` is ignored
        //   - In RTL: if both specified, `right` wins and `left` is ignored

        // +spec:overflow:53dffd - both left/right auto → used values are 0, boxes stay in original position
        // +spec:positioning:5a099e - negative offsets can cause overlapping (no clamping applied)
        // +spec:positioning:d189de - bottom offset for relative positioning is with respect to the box's own bottom edge
        // +spec:positioning:d80f47 - opposing inset values are negations: top wins over bottom, left/right per direction
        // +spec:positioning:ecc27c - relative positioning: left/right move box horizontally without changing size, left = -right
        // +spec:positioning:50218d - relative: offset from static position (top edges of box itself)
        // both auto → 0; one auto → negative of other; neither auto → bottom ignored (top wins)
        // +spec:positioning:ac768b - relative positioning: both auto→0, one auto→neg of other, neither→top wins; direction-aware left/right
        // +spec:positioning:e3727e - top/bottom: both auto→0, one auto→negative of other, neither auto→bottom ignored
        // Vertical positioning: `top` takes precedence over `bottom`
        if let Some(top) = offsets.top {
            delta_y = top;
        } else if let Some(bottom) = offsets.bottom {
            delta_y = -bottom;
        }

        // +spec:positioning:1732e8 - left/right for relatively positioned elements determined by 9.4.3 rules
        // Spec: "If the 'direction' property of the containing block is 'ltr', the value of 'left' wins"
        // Get the direction of the containing block (parent), not the element itself
        use azul_css::props::style::StyleDirection;
        let cb_direction = node.parent
            .and_then(|parent_idx| tree.get(parent_idx))
            .and_then(|parent_node| {
                let parent_dom_id = parent_node.dom_node_id?;
                let parent_state =
                    &ctx.styled_dom.styled_nodes.as_container()[parent_dom_id].styled_node_state;
                match get_direction_property(ctx.styled_dom, parent_dom_id, parent_state) {
                    MultiValue::Exact(v) => Some(v),
                    _ => None,
                }
            })
            .unwrap_or(StyleDirection::Ltr);
        // +spec:containing-block:6d4fb1 - over-constrained relative positioning: ltr→left wins, rtl→right wins
        match cb_direction {
            StyleDirection::Ltr => {
                if let Some(left) = offsets.left {
                    delta_x = left;
                } else if let Some(right) = offsets.right {
                    // +spec:overflow:fb426c - left auto: used value is minus the value of right
                    delta_x = -right;
                }
            }
            StyleDirection::Rtl => {
                if let Some(right) = offsets.right {
                    delta_x = -right;
                } else if let Some(left) = offsets.left {
                    delta_x = left;
                }
            }
        }

        // +spec:overflow:f1e1ce - relative positioning may cause overflow:auto/scroll boxes to need scrollbars
        // Only apply the shift if there is a non-zero delta.
        if delta_x != 0.0 || delta_y != 0.0 {
            current_pos.x += delta_x;
            current_pos.y += delta_y;

            ctx.debug_log(&format!(
                "Adjusted relative element #{} from {:?} to {:?} (delta: {}, {})",
                node_index, initial_pos, *current_pos, delta_x, delta_y
            ));

            // +spec:table-layout:ec2600 - For table-row-group, table-header-group, table-footer-group, or table-row,
            // the relative shift affects all contents of the box including table cells.
            // Propagate the delta to all descendant nodes.
            {
                use azul_css::props::layout::LayoutDisplay;
                let display = get_display_property(ctx.styled_dom, node.dom_node_id);
                let is_table_row_like = matches!(
                    display,
                    MultiValue::Exact(
                        LayoutDisplay::TableRowGroup
                        | LayoutDisplay::TableHeaderGroup
                        | LayoutDisplay::TableFooterGroup
                        | LayoutDisplay::TableRow
                    )
                );
                if is_table_row_like {
                    // Shift all children (and their descendants) by the same delta
                    let mut stack = tree.children(node_index).to_vec();
                    while let Some(child_idx) = stack.pop() {
                        if let Some(child_pos) = calculated_positions.get_mut(child_idx) {
                            child_pos.x += delta_x;
                            child_pos.y += delta_y;
                        }
                        stack.extend_from_slice(tree.children(child_idx));
                    }
                }
            }
        }
    }
    Ok(())
}

/// Sticky positioning constraints computed at layout time.
/// At scroll time, the sticky box's position is clamped so that
/// it remains within the sticky view rectangle (scrollport inset by these values).
// +spec:overflow:bac4e5 - sticky view rectangle from inset properties relative to nearest scrollport
#[derive(Debug, Clone)]
pub struct StickyConstraints {
    /// Inset from the top edge of the nearest scrollport (0 if auto).
    pub top_inset: f32,
    /// Inset from the right edge of the nearest scrollport (0 if auto).
    pub right_inset: f32,
    /// Inset from the bottom edge of the nearest scrollport (0 if auto).
    pub bottom_inset: f32,
    /// Inset from the left edge of the nearest scrollport (0 if auto).
    pub left_inset: f32,
    /// Normal-flow position of the sticky element (border-box origin).
    pub normal_flow_position: LogicalPosition,
    /// Border-box size of the sticky element.
    pub border_box_size: LogicalSize,
    /// The scrollport rect (content-box of nearest scroll container).
    pub scrollport: LogicalRect,
}

/// Finds the nearest scrollport (ancestor with overflow: scroll or auto) for a node.
/// Returns the content-box rect of the scrollport, or the viewport if none found.
fn find_nearest_scrollport(
    tree: &LayoutTree,
    node_index: usize,
    styled_dom: &StyledDom,
    calculated_positions: &super::PositionVec,
    viewport: LogicalRect,
) -> LogicalRect {
    use crate::solver3::getters::{get_overflow_x, get_overflow_y};
    use azul_css::props::layout::LayoutOverflow;

    let mut current_parent_idx = tree.get(node_index).and_then(|n| n.parent);

    while let Some(parent_index) = current_parent_idx {
        let parent_node = match tree.get(parent_index) {
            Some(n) => n,
            None => break,
        };
        let parent_dom_id = match parent_node.dom_node_id {
            Some(id) => id,
            None => {
                current_parent_idx = parent_node.parent;
                continue;
            }
        };

        let node_state = &styled_dom.styled_nodes.as_container()[parent_dom_id].styled_node_state;
        let ox = get_overflow_x(styled_dom, parent_dom_id, node_state);
        let oy = get_overflow_y(styled_dom, parent_dom_id, node_state);

        let is_scrollport = matches!(
            ox,
            MultiValue::Exact(LayoutOverflow::Scroll | LayoutOverflow::Auto)
        ) || matches!(
            oy,
            MultiValue::Exact(LayoutOverflow::Scroll | LayoutOverflow::Auto)
        );

        if is_scrollport {
            let margin_box_pos = calculated_positions
                .get(parent_index)
                .copied()
                .unwrap_or_default();
            let border_box_size = parent_node.used_size.unwrap_or_default();

            // Content-box = margin-box pos + border + padding, size - border - padding
            let pbp = parent_node.box_props.unpack();
            let content_pos = LogicalPosition::new(
                margin_box_pos.x
                    + pbp.border.left
                    + pbp.padding.left,
                margin_box_pos.y
                    + pbp.border.top
                    + pbp.padding.top,
            );
            let content_size = LogicalSize::new(
                (border_box_size.width
                    - pbp.border.left
                    - pbp.border.right
                    - pbp.padding.left
                    - pbp.padding.right)
                    .max(0.0),
                (border_box_size.height
                    - pbp.border.top
                    - pbp.border.bottom
                    - pbp.padding.top
                    - pbp.padding.bottom)
                    .max(0.0),
            );
            return LogicalRect::new(content_pos, content_size);
        }

        current_parent_idx = parent_node.parent;
    }

    viewport
}

/// Find the scroll offset of the nearest scroll container ancestor.
/// Returns the scroll offset as a LogicalPosition (how far the content has scrolled).
fn find_nearest_scroll_offset(
    tree: &LayoutTree,
    node_index: usize,
    scroll_offsets: &BTreeMap<NodeId, ScrollPosition>,
) -> LogicalPosition {
    let mut parent = tree.get(node_index).and_then(|n| n.parent);
    while let Some(pidx) = parent {
        if let Some(pnode) = tree.get(pidx) {
            if let Some(dom_id) = pnode.dom_node_id {
                if let Some(scroll_pos) = scroll_offsets.get(&dom_id) {
                    let offset_x = scroll_pos.children_rect.origin.x - scroll_pos.parent_rect.origin.x;
                    let offset_y = scroll_pos.children_rect.origin.y - scroll_pos.parent_rect.origin.y;
                    return LogicalPosition::new(offset_x, offset_y);
                }
            }
            parent = pnode.parent;
        } else {
            break;
        }
    }
    LogicalPosition::zero()
}

/// Adjusts positions of sticky-positioned elements based on scroll offset.
///
/// Sticky positioning works like relative positioning, but the element's position
/// is constrained by its inset properties (top/right/bottom/left) relative to the
/// nearest scrollport (scroll container ancestor). The margin box is further
/// constrained to remain within the containing block.
///
/// +spec:position-sticky:9449f1 - for sticky positioning, insets represent offsets from scrollport edge
/// +spec:position-sticky:75412d - multiple sticky boxes in same container offset independently
/// +spec:box-model:af9af8 - sticky positioning: shift element to stay within sticky view rectangle, margin box constrained to containing block
/// +spec:overflow:bac4e5 - compute sticky view rectangle, clamp end-edge insets to border box size
pub fn adjust_sticky_positions<T: ParsedFontTrait>(
    ctx: &mut LayoutContext<'_, T>,
    tree: &LayoutTree,
    calculated_positions: &mut super::PositionVec,
    scroll_offsets: &BTreeMap<NodeId, ScrollPosition>,
    viewport: LogicalRect,
) -> Result<()> {
    for node_index in 0..tree.nodes.len() {
        let node = &tree.nodes[node_index];
        let position_type = get_position_type(ctx.styled_dom, node.dom_node_id);

        if position_type != LayoutPosition::Sticky {
            continue;
        }

        let dom_id = match node.dom_node_id {
            Some(id) => id,
            None => continue,
        };

        // Find the nearest scrollport for this sticky element
        let scrollport = find_nearest_scrollport(
            tree,
            node_index,
            ctx.styled_dom,
            calculated_positions,
            viewport,
        );

        // The containing block for percentage resolution is the parent's content box
        let containing_block = node.parent
            .and_then(|parent_idx| {
                let parent_node = tree.get(parent_idx)?;
                let parent_pos = calculated_positions.get(parent_idx).copied().unwrap_or_default();
                let parent_size = parent_node.used_size.unwrap_or_default();
                let parent_wm = parent_node.dom_node_id
                    .map(|pid| {
                        let ps = &ctx.styled_dom.styled_nodes.as_container()[pid].styled_node_state;
                        get_writing_mode(ctx.styled_dom, pid, ps).unwrap_or_default()
                    })
                    .unwrap_or_default();
                let pbp = parent_node.box_props.unpack();
                let content_size = pbp.inner_size(parent_size, parent_wm);
                let content_origin = LogicalPosition::new(
                    parent_pos.x + pbp.border.left + pbp.padding.left,
                    parent_pos.y + pbp.border.top + pbp.padding.top,
                );
                Some(LogicalRect::new(content_origin, content_size))
            })
            .unwrap_or(viewport);

        // Resolve inset properties (top, right, bottom, left)
        let offsets = resolve_position_offsets(ctx.styled_dom, Some(dom_id), scrollport.size);

        // Get the scroll offset from the nearest scroll container
        let scroll_offset = find_nearest_scroll_offset(tree, node_index, scroll_offsets);

        let Some(current_pos) = calculated_positions.get_mut(node_index) else {
            continue;
        };

        let static_pos = *current_pos;
        let element_size = node.used_size.unwrap_or_default();
        let nbp = node.box_props.unpack();
        let margin = &nbp.margin;

        let mut shift_x = 0.0f32;
        let mut shift_y = 0.0f32;

        // For each side: if inset is not auto, clamp the border edge to stay
        // within the sticky view rectangle (scrollport inset by the specified amount).
        // The scroll offset shifts the effective scrollport position.
        if let Some(top_inset) = offsets.top {
            let sticky_edge = scrollport.origin.y + scroll_offset.y + top_inset;
            let border_top = current_pos.y;
            if border_top < sticky_edge {
                shift_y = shift_y.max(sticky_edge - border_top);
            }
        }

        if let Some(bottom_inset) = offsets.bottom {
            let sticky_edge = scrollport.origin.y + scroll_offset.y + scrollport.size.height - bottom_inset;
            let border_bottom = current_pos.y + element_size.height;
            if border_bottom > sticky_edge {
                shift_y = shift_y.min(sticky_edge - border_bottom);
            }
        }

        if let Some(left_inset) = offsets.left {
            let sticky_edge = scrollport.origin.x + scroll_offset.x + left_inset;
            let border_left = current_pos.x;
            if border_left < sticky_edge {
                shift_x = shift_x.max(sticky_edge - border_left);
            }
        }

        if let Some(right_inset) = offsets.right {
            let sticky_edge = scrollport.origin.x + scroll_offset.x + scrollport.size.width - right_inset;
            let border_right = current_pos.x + element_size.width;
            if border_right > sticky_edge {
                shift_x = shift_x.min(sticky_edge - border_right);
            }
        }

        // Constrain: the margin box must remain within the containing block
        if shift_y != 0.0 {
            let margin_box_top = current_pos.y - margin.top + shift_y;
            let margin_box_bottom = current_pos.y + element_size.height + margin.bottom + shift_y;
            if margin_box_top < containing_block.origin.y {
                shift_y += containing_block.origin.y - margin_box_top;
            }
            let cb_bottom = containing_block.origin.y + containing_block.size.height;
            if margin_box_bottom > cb_bottom {
                shift_y -= margin_box_bottom - cb_bottom;
            }
        }

        if shift_x != 0.0 {
            let margin_box_left = current_pos.x - margin.left + shift_x;
            let margin_box_right = current_pos.x + element_size.width + margin.right + shift_x;
            if margin_box_left < containing_block.origin.x {
                shift_x += containing_block.origin.x - margin_box_left;
            }
            let cb_right = containing_block.origin.x + containing_block.size.width;
            if margin_box_right > cb_right {
                shift_x -= margin_box_right - cb_right;
            }
        }

        if shift_x != 0.0 || shift_y != 0.0 {
            current_pos.x += shift_x;
            current_pos.y += shift_y;

            ctx.debug_log(&format!(
                "Adjusted sticky element #{} from {:?} to {:?}",
                node_index, static_pos, *current_pos
            ));
        }
    }
    Ok(())
}

// +spec:positioning:22f165 - absolute/fixed containing block: nearest positioned ancestor's padding-box, or initial CB
/// Helper to find the containing block for an absolutely positioned element.
/// CSS 2.1 Section 10.1: The containing block for absolutely positioned elements
/// is the padding box of the nearest positioned ancestor.
// +spec:containing-block:10af51 - absolutely positioned element's CB is nearest positioned ancestor
// +spec:positioning:2d0dbb - containing block for abspos is padding-box of nearest positioned ancestor, or initial CB
// +spec:positioning:3ac06c - abspos positioned relative to containing block ignoring fragmentation breaks
// +spec:positioning:d7e4b4 - containing block of abspos element is always definite (returns concrete LogicalRect)
// +spec:positioning:fc9dba - containing block resolution for absolutely positioned boxes
///
/// Returns a `LogicalRect` representing the padding-box of the nearest
/// positioned ancestor, or the viewport (initial containing block) if none exists.
/// This is the unified entry point used by both sizing and positioning phases.
// +spec:containing-block:18ae8e - Absolute positioning: abs-pos box establishes new CB for normal flow and abs-pos (but not fixed) descendants
// +spec:containing-block:b6cb8b - containing block for abs-pos is nearest positioned ancestor
// +spec:display-property:5a39bc - containing block for abspos is nearest positioned ancestor or initial containing block
// +spec:positioning:09a0fa - Absolute positioning: CB is padding-box of nearest positioned ancestor
// +spec:positioning:467cb1 - Containing block for abs pos = nearest positioned ancestor or initial CB
// +spec:positioning:99d0bb - containing block for absolute elements is nearest positioned ancestor
// +spec:positioning:92e099 - containing block for abs pos is nearest positioned ancestor or initial CB
// +spec:positioning:f57523 - containing block of abspos element is always definite (returns concrete LogicalRect)
// +spec:width-calculation:bf1aa6 - abspos CB is nearest positioned ancestor, else initial CB
// Containing block for absolutely positioned elements is established by
// nearest positioned ancestor (relative/absolute/fixed), or initial containing block if none.
// +spec:positioning:8f50de - relatively positioned parent serves as containing block for abspos descendants
// +spec:containing-block:6bcb0c - containing block is padding edge of nearest positioned ancestor, or initial containing block if none
// +spec:containing-block:bf17e5 - containing block for abspos is padding box of nearest positioned ancestor, or initial CB
// +spec:containing-block:d0f92d - containing block for positioned box is nearest positioned ancestor, or initial containing block
// +spec:containing-block:d7e013 - containing block for positioned box is nearest positioned ancestor or initial CB
// +spec:containing-block:05bc0d - positioning an element changes which ancestor establishes the CB for its descendants
// +spec:positioning:355ee4 - CB for abspos is padding edge of nearest positioned ancestor, or initial CB
// +spec:positioning:383794 - Containing block for abspos is nearest positioned ancestor, or initial containing block if none
// +spec:positioning:5b3e43 - Containing block for abs-pos is padding box of nearest positioned ancestor, or initial CB
// +spec:positioning:882e67 - containing block for abs pos is nearest positioned ancestor or initial CB
// +spec:positioning:292c5c - relative parent serves as containing block for absolute descendants
// +spec:positioning:00ce38 - CB for absolute is padding edge of nearest positioned ancestor
pub fn find_absolute_containing_block_rect(
    tree: &LayoutTree,
    node_index: usize,
    styled_dom: &StyledDom,
    calculated_positions: &super::PositionVec,
    viewport: LogicalRect,
) -> Result<LogicalRect> {
    // +spec:positioning:748d87 - walk up to nearest positioned ancestor for CB
    let mut current_parent_idx = tree.get(node_index).and_then(|n| n.parent);

    // +spec:positioning:aa361e - values other than static make a box positioned and establish an abspos containing block
    while let Some(parent_index) = current_parent_idx {
        let parent_node = tree.get(parent_index).ok_or(LayoutError::InvalidTree)?;

        if get_position_type(styled_dom, parent_node.dom_node_id).is_positioned() {
            // calculated_positions stores margin-box positions
            let margin_box_pos = calculated_positions
                .get(parent_index)
                .copied()
                .unwrap_or_default();
            // used_size is the border-box size
            let border_box_size = parent_node.used_size.unwrap_or_default();

            // +spec:containing-block:6bcb0c - containing block formed by padding edge of nearest positioned ancestor
            // +spec:positioning:df1921 - abs-pos percentage widths resolve against padding box of containing block
            // Calculate padding-box origin (margin-box + border)
            let pbp = parent_node.box_props.unpack();
            let padding_box_pos = LogicalPosition::new(
                margin_box_pos.x + pbp.border.left,
                margin_box_pos.y + pbp.border.top,
            );

            // Calculate padding-box size (border-box - borders)
            let padding_box_size = LogicalSize::new(
                border_box_size.width
                    - pbp.border.left
                    - pbp.border.right,
                border_box_size.height
                    - pbp.border.top
                    - pbp.border.bottom,
            );

            return Ok(LogicalRect::new(padding_box_pos, padding_box_size));
        }
        current_parent_idx = parent_node.parent;
    }

    // +spec:positioning:3d88c9 - abspos available space is always definite (viewport or positioned ancestor padding box)
    // No positioned ancestor found: fall back to initial containing block (viewport)
    // +spec:containing-block:141dcc - absolute element with no positioned ancestor uses initial containing block
    // +spec:containing-block:657f2f - containing block becomes initial containing block when no positioned ancestors
    // +spec:containing-block:7f5090 - if no ancestor establishes one, absolute positioning CB is initial containing block
    // +spec:containing-block:7f5090 - fallback to initial containing block when no positioned ancestor
    // +spec:containing-block:ad5ebc - no positioned ancestor: containing block becomes the initial containing block
    // +spec:display-property:813192 - abspos containing block falls back to initial containing block (viewport) when no positioned ancestor
    Ok(viewport)
}