# Styling and layout

## 1. The decision in one paragraph
Authors style nodes with a **`style` prop** using **React Native style names** (a supported subset). The framework,
not the author, turns a style into native layout and drawing, in **one fixed order** (§4). Layout is done by
**Compose** (Android) and **SwiftUI** (iOS). There is **no Yoga** and no second layout engine. Static styles are
**folded into the compiled `.zn.ui` template** at build time; only values that change at runtime become slots.
Unsupported keys are **build errors**.

Why not Compose-style modifier chains (`ui.padding(16).background(...)`): chains are order-dependent (padding then
background is a different picture from background then padding), they feel foreign in JSX, and they make porting RN
code a full rewrite. A fixed order covers almost every design; the rare exception is handled by nesting a `Box`.
(Background: `argon_opus_architecture_report_modifiers_vs_props.md`. Its four visual bugs were not caused by chaining
itself; see `docs/v0-findings.md` §G. But the conclusion holds for the reasons above.)

## 2. Authoring
```tsx
const s = StyleSheet.create({          // only for typing and reuse; the compiler folds it into the template
  card: {
    marginHorizontal: 16,
    paddingHorizontal: 14, paddingVertical: 10,
    backgroundColor: c.surface, borderRadius: 12,
    borderWidth: 1, borderColor: c.line,
  },
  cardSelected: { borderColor: c.primary },
})

<Row style={[s.card, selected() && s.cardSelected]} justifyContent="space-between" alignItems="center">
  <Text style={t.title}>{merchant()}</Text>
  <Text style={[t.amount, { color: isCredit() ? c.credit : c.text }]}>{amount()}</Text>
</Row>
```
- `style` takes an object or an array. Falsy array entries are skipped. Later entries win.
- **Static parts** (constants, tokens, `StyleSheet` entries) are resolved by the compiler and stored in the template.
- **Dynamic parts** (anything reading a signal or a prop) become **slots**: one slot per style key that can change.
  A conditional array entry (`selected() && s.cardSelected`) becomes slots for the keys that entry can change.
- Layout props that are usually set per node (`justifyContent`, `alignItems`, `gap`) may be written either as props or
  inside `style`. Both compile to the same thing.
- Design tokens (`c.*` colours, `t.*` text styles, spacing scale) are a typed TS module, compiled in as constants.
  Dark mode: native picks the token set at runtime; JS does not re-render.
- Units: numbers are dp (Android) / pt (iOS). Strings ending in `%` are percentages of the parent's content box.

## 3. Supported keys (v1)

### 3.1 Layout
| Key | Values | Compose | SwiftUI |
|---|---|---|---|
| `flex` | number ≥ 0 | `Modifier.weight(n)` in Row/Column | layout priority + `.frame(maxWidth/maxHeight: .infinity)` |
| `justifyContent` | `flex-start` `center` `flex-end` `space-between` `space-around` `space-evenly` | `Arrangement.*` | `Spacer`s / alignment |
| `alignItems` | `flex-start` `center` `flex-end` `stretch` | `Alignment.*` (+ stretch rule §5) | `alignment:` |
| `alignSelf` | same as alignItems | `Modifier.align(...)` | `.frame(alignment:)` |
| `gap`, `rowGap`, `columnGap` | number | `Arrangement.spacedBy` | `spacing:` |
| `flexWrap`, `alignContent` | `wrap` / `nowrap` (Row only); justifyContent's values | `ZnWrapLayout` (ZnWrap.kt, Yoga's rules; built 2026-11-09, registry 24) | custom wrapping `Layout` (not built) |
| `width`, `height` | number, `'N%'`, `'auto'` | `width/height`, `fillMaxWidth(f)` | `.frame` / `containerRelativeFrame` |
| `minWidth`, `maxWidth`, `minHeight`, `maxHeight` | number | `widthIn / heightIn` | `.frame(min:max:)` |
| `aspectRatio` | number | `aspectRatio` | `.aspectRatio` |
| `margin`, `marginHorizontal`, `marginVertical`, `marginTop/Bottom/Start/End` | number ≥ 0 | outer `padding` (§4 step 1) | outer `.padding` |
| `padding` + same variants | number ≥ 0 | inner `padding` (§4 step 9). On `Scroll`, `Carousel` and `List` it is content padding: it scrolls with the content (RN's `contentContainerStyle`, which is also accepted there, padding keys only) | inner `.padding` |
| `position: 'absolute'` + `top/bottom/start/end` | only for children of `Stack` | `Box` + `align` + `offset` | `ZStack` + `.offset` / alignment |
| `zIndex` | number, inside `Stack`; registry 16: also `Column` / `Row` / `Box` / `Pressable` children | `zIndex` | `.zIndex` |
| `overflow` | `visible` (default), `hidden` | `clip(shape)` | `.clipShape` |
| `display` | `none` only | not composed | not rendered |

There is no `flexDirection`: direction comes from the component (`Row`, `Column`, `Stack`). The future NativeWind
translator (§9) maps `View` + `flex-row` to `Row`.

### 3.2 Drawing
| Key | Notes |
|---|---|
| `backgroundColor` | colour or token (`#hex`, `rgb()`, `rgba()`, `transparent`) |
| `backgroundImage` | `"linear-gradient(<angle>deg \| to <side>, <colour> [N%], ...)"` (RN's CSS form); parsed at build time, drawn over `backgroundColor` in the same shape; build-time only (step 2.4b) |
| `borderRadius`, `borderTopStartRadius`, ... | one shape is built from all corner values and shared by background, border, clip, press feedback and shadow |
| `borderWidth`, `borderColor` | uniform border |
| `borderTopWidth`, `borderBottomWidth`, ... (+ colours) | per-side borders (dividers are common in RN code); drawn with `drawBehind` on Android [check cost] |
| `opacity` | `graphicsLayer(alpha)` |
| `transform` | `[{ scale }, { scaleX }, { scaleY }, { translateX }, { translateY }, { rotate: '45deg' }]` → `graphicsLayer` |
| `elevation`, `shadowColor`, `shadowOpacity`, `shadowRadius`, `shadowOffset` | mapped to one platform shadow; Android uses `shadow(elevation, shape)`, so shadow colour/offset are approximations (documented in `docs/platform-differences.md`) |

### 3.3 Text (only on `Text` and `TextField`)
`color`, `fontSize`, `fontWeight`, `fontFamily`, `fontStyle`, `lineHeight`, `letterSpacing`, `textAlign`,
`textDecorationLine`, `textTransform`. Line limits are props: `numberOfLines`, `ellipsize`. `autoShrink` (shrink the
font to fit, needed for 7-digit amounts) is a prop.

### 3.4 Not supported (build errors with a hint)
`flexDirection`, `flexShrink`, `flexBasis`, `alignContent`, `position: 'relative'` with offsets, negative margins
(Compose padding can't be negative; use `transform: translate`), `display: 'flex'|'contents'`, web-only keys.
A key we don't support is **never silently ignored**.

## 4. The fixed order (decided)
The renderer builds every node's modifier chain in this order, outer to inner. Authors can't change it.

| Step | Style keys | Compose |
|---|---|---|
| 1. Outer spacing | `margin*` | `padding(...)` |
| 2. Size | `width`, `height`, `min*/max*`, `aspectRatio`, `flex`, `alignSelf` | `size / widthIn / weight / aspectRatio / align` |
| 3. Transform | `opacity`, `transform` | `graphicsLayer` |
| 4. Shadow | `elevation`, `shadow*` | `shadow(shape)` |
| 5. Background | `backgroundColor`, `borderRadius` | `background(color, shape)` |
| 6. Border | `border*` | `border(width, color, shape)` / `drawBehind` |
| 7. Clip | `overflow: 'hidden'` | `clip(shape)` |
| 8. Press feedback | (only `Pressable` / `Button`) | `indication` (stays inside the shape) |
| 9. Inner spacing | `padding*` + border width | `padding(...)` |
| 10. Content | children / text | — |

SwiftUI applies the same steps in the same order (modifiers there are also outer-to-inner).
If a design needs another layering (two borders, a background outside a border), the author nests a `Box`.

Step 7 also clips without `overflow: 'hidden'` in three cases: a rounded `Image`, a rounded `Shimmer`, and a rounded
`Pressable` whose ripple is not borderless. They clip their children to the corners by default, and a `borderRadius`
from a slot counts too. For the Pressable this is a zn rule, not React Native (RN clips only with `overflow:
'hidden'`); `rippleBorderless` turns it off, and also makes the ripple round and not cut to the shape.

## 5. zn flex rules (our documented semantics, not CSS)
These follow React Native wherever Compose and SwiftUI make it cheap, and say so where they don't.
1. **Sizing box:** `width` / `height` include padding and border, not margin (React Native behaviour).
2. **Borders take space:** a border pushes content inward. Compose doesn't do this by itself, so the renderer adds
   the border width to the inner padding (step 9).
3. **`flex: n`** shares the space left after the children without `flex`, in proportion to `n`. Children without
   `flex` take their natural size. (`flexShrink` doesn't exist; a too-wide row of fixed children overflows, as in RN
   with its default `flexShrink: 0`.)
4. **`alignItems: 'stretch'` is the default in `Column`** (as in RN): children without an explicit width fill the
   width (`fillMaxWidth`), **but only when the Column's own width is definite** (set by a width prop, shared out by
   `flex` in a Row, or itself stretched by its parent). A Column that shrinks to its content (for example inside an
   `alignItems: 'center'` parent) doesn't stretch its children, so a stretch never pushes a shrink-wrapped parent wider
   (step 1.3: a cashback badge wrongly went full width). **A Column raised by `minWidth`** (its width not otherwise
   definite) stretches those children to the raised width and no wider, as Yoga does (since 2026-11-24, layout
   conformance case 48: a `flexGrow` pill in a badge ring with `minWidth: 18`). **In `Row`, stretch only applies when the Row has a fixed height**; otherwise it behaves as
   `flex-start`, because stretching to an unknown height needs intrinsic measurement, which is slow. This is a known
   difference from RN, listed in `docs/platform-differences.md`.
   **`alignSelf: 'stretch'` stretches whatever the parent's `alignItems` is** (since 2026-11-08, A11-1), as in RN: in a
   Column, Stack or vertical Scroll with a definite width it fills the width; in a Row with a fixed height it fills the
   height. Before, an `alignItems` of `center` or `flex-start` won and the child kept its own size.
   **`maxWidth` / `maxHeight` beat a stretch and a % size** (since 2026-11-08, A11-3), as in RN: a stretched child with
   `maxWidth: 80` is 80 wide, at the start of its row; `width: '100%', maxWidth: 384` is 384 on a wide screen.
   **A fixed `width` / `height` obeys its own min and max** (since 2026-11-08, LayoutLab c29a), as in RN: the max
   first, then the min. `width: 50, minWidth: 80, maxWidth: 60` is 80 wide. Before, a fixed size ignored them.
   **`Stack` follows the Column rule for its normal (not absolute) children** (since 2026-11-05): with a definite
   width and `alignItems` unset or `stretch`, a child with no width and no `alignSelf` fills the Stack's width. So a
   styled `Column` row inside a `Stack` is full width, like an absolute `start: 0, end: 0` sibling. A Stack with
   `alignItems: 'center'` or `'flex-end'` still places its children at their own size. Normal children of a Stack
   still overlap (they don't flow down as in an RN `View`).
   **An absolute child takes no room** (since 2026-11-05, R29), as in RN: the Stack's size comes from its normal
   children and its own size only, then each absolute child is placed in that box. A set dp width or height is kept
   even when it is bigger than the Stack (the child draws outside), a `%` is of the Stack, no width is at most the
   Stack's width, and no height is the content's height. A Stack with only absolute children and no size of its own
   is 0 x 0, as in RN: give it a size (second-opinions 127).
5. **Percentages** are of the parent's content box (inside its padding).
6. **Margins never collapse.** `gap` is preferred for space between siblings; margins are allowed for porting.
   `gap` works with every `justifyContent`, as in Yoga: with `space-*` it is the smallest space between two items
   (second-opinions 112). Margin, padding and border widths can't be negative: a build error, and the runtime clamps
   at 0 (second-opinions 113).
7. **Text** measures natively (Compose text layout / CoreText). Line height, font metrics and wrapping can differ by a
   pixel or two between platforms; the conformance tests (§9.5 of ARCHITECTURE) set the tolerance.

## 6. Safe areas and the keyboard (decided)
Handled by **native code per route**, never by a JS `SafeAreaView`:
- Route option in `routes.ts`: `safeArea: 'all' | 'none' | { top?: boolean, bottom?: boolean }`, default `'all'`.
  Start / end (landscape cutouts, side navigation bars) are kept clear unless the route says `'none'`.
- Android (built in milestone 2 step 2.3): the Activity calls `enableEdgeToEdge()`; `ZnNavHost` pads each screen with
  `windowInsetsPadding` of **system bars + display cutout** for the chosen edges (Compose foundation; **not**
  material3's `Scaffold`, which we don't ship), and fills behind the bars with the screen root's background colour.
  The keyboard is not part of this (see below). iOS: `.ignoresSafeArea(edges:)`.
- Bar icons: the route option `statusBar: 'dark' | 'light'` sets the **status bar** icons (default dark icons, for
  light screens). The **navigation bar** icons follow the screen root's background (dark icons on a light screen), so
  a full-bleed screen with a dark header doesn't get a grey scrim over a light bottom bar (seen on the Lava).
  Registry 15: a `<StatusBar>` / `<NavigationBar>` in the screen wins over both while the screen is on top (icons,
  `hidden`), and its `backgroundColor` is drawn behind the bar for that screen (guide-notes §43).
- Full-bleed screens (a coloured header behind the status bar) use `safeArea: { top: false }` and read the read-only
  `insets()` signal (`{ top, bottom, start, end }`, dp) to pad their own header: `paddingTop: insets().top + 24`.
  Per-edge padding (`paddingTop/Bottom/Start/End`) may change at runtime for this (registry v4); it is read in the
  **layout phase**, so an inset change re-measures that node and recomposes nothing. Native sends insets only when
  they change. Until the first value arrives they are 0 (only a full-bleed **start** screen could show that for one
  frame).
- The keyboard (IME) inset is applied by native for screens with text input (`keyboard: 'resize' | 'none'` route
  option), so focused fields stay visible. While a docked keyboard is up, `insets().bottom` is 0: the keyboard covers
  the home indicator / navigation bar, so a bottom bar padded with it sits on the keyboard instead of 34 dp above it
  (since 0.1.1 on both platforms, A142).
- Background: v0 bridged insets into JS through a `SafeAreaView`; it caused a blank screen and status-bar /
  nav-bar overlap (`docs/v0-findings.md` §H).

## 7. Animation keys
`animate={{ scale: spring({ stiffness: 400, damping: 30 }), opacity: tween(150) }}`: when the slot behind a style key
changes, native animates to the new value (ARCHITECTURE §10). Only numeric drawing keys (`opacity`, `transform`,
`backgroundColor`, sizes) can animate.

## 8. How a style becomes Compose code (worked example)

**Performance note (2026-10-29):** style props and modifier chains cost the same at runtime. On Android everything
ends up as a Compose `Modifier` chain either way; what costs time is *how often* we build it. v0 rebuilt modifiers
from JSON with an observable per prop; v1 builds each node's chain **once**, at template preparation. The choice of
`style` over chains is about correctness, familiar JSX and porting, not speed.

### 8.1 The path
1. **Build time:** the compiler resolves static styles into the node's entry in `.zn.ui`. Each style key that can
   change becomes a slot (slot → node + style key).
2. **Template preparation (once per template):** each node's static style becomes a pre-built `Modifier` in the fixed
   order (§4). Dynamic keys get small modifier pieces at their fixed position that read the slot's cell.
3. **Composition:** the node's composable uses that chain object as-is. No rebuilding, no parsing.
4. **A slot changes:** only the work tied to that slot runs (§8.3).

### 8.2 Worked example: unusual layering
A Kotlin developer might write:
```kotlin
Box(Modifier
  .background(red)      // red fills the whole box
  .padding(1.dp)        // → a 1 dp red ring stays visible
  .background(blue)     // blue fills what's inside the ring
  .padding(16.dp)       // → a 16 dp blue band
  .clickable { … }      // tap area + ripple = only what's inside the blue band
  .padding(8.dp, 4.dp)  // inside the tap area; blue shows through this band
  .background(green)    // green fills the innermost area
) { Text("Click me!") }
```
(Moving `.clickable` one step earlier silently changes the tap area. This is the class of bug the fixed order removes.)

In zn TSX each layer is a nested node, each with the one fixed order:
```tsx
<Box style={{ backgroundColor: red, padding: 1 }}>                                   {/* red ring */}
  <Box style={{ backgroundColor: blue, padding: 16 }}>                               {/* blue band */}
    <Pressable onPress={…} style={{ paddingHorizontal: 8, paddingVertical: 4 }}>     {/* tap area */}
      <Text style={{ backgroundColor: green }}>Click me!</Text>
    </Pressable>
  </Box>
</Box>
```
Inside `Pressable` the order puts press feedback before padding, which is exactly `.clickable.padding(8, 4)`: the
ripple covers the 8/4 band. Same picture, same tap area.

On iOS, SwiftUI modifiers also depend on order but read the other way (each modifier wraps the view above it), so the
iOS renderer applies the same steps mirrored. Authors never see this.

### 8.3 Two renderer optimisations (planned; build them only if they don't cause real problems)
These are **not required for correctness**. Decided 2026-10-29: implement while building the renderer, and drop or
defer either one if it turns out to cause hard bugs or complexity.

1. **Merge decoration-only boxes.** At template preparation, a `Box` with exactly one child and only drawing and
   padding styles (no size, alignment, events, accessibility role or `heroTag`) can be merged into its child's chain:
   its chain becomes a prefix of the child's. The nested TSX above then emits one flat chain, the same one a Kotlin
   developer would write:
   ```kotlin
   Box(Modifier.background(red).padding(1.dp).background(blue).padding(16.dp)
           .clickable(…).padding(horizontal = 8.dp, vertical = 4.dp)) {
     BasicText("Click me!", Modifier.background(green))
   }
   ```
   Without merging, the extra boxes cost microseconds; this is a small win, not a fix.
2. **Read each slot in the cheapest Compose phase.** Compose has three phases: composition, layout, draw. A dynamic
   style key only wakes the phase it affects. The chain object never changes; it holds the slot cell, and a lambda
   reads the value when its phase runs.

   | Style key changes | Read in | Cost |
   |---|---|---|
   | `backgroundColor`, `borderColor`, `opacity`, `transform` | draw (`drawBehind { }`, `graphicsLayer { }`) | redraw only |
   | `width`, `height`, `min*/max*`, `aspectRatio`, `display` | layout (`Modifier.layout { measurable, constraints -> }`) | relayout only (zero recompositions) |
   | `margin*`, `padding*`, `gap`, `flex`, `borderRadius*`, `fontSize`, `lineHeight` | composition | recompose that single leaf |

   A press colour change or a fade then redraws one node and nothing else. Animated sizes (e.g. 40dp to 300dp) run in the layout pass without recomposing the composable tree.

## 9. Dynamic Layout Values (Registry 23)
In Registry 23, zero-native unlocks runtime expression values across layout properties. Previously fixed at build time, these now bind directly to reactive slots:
- **Dimensions & Constraints:** `width`, `height`, `minWidth`, `maxWidth`, `minHeight`, `maxHeight`, `aspectRatio`.
- **Flex & Flow:** `flex`, `flexGrow` (mapped to `flex`).
- **Offsets:** `top`, `bottom`, `start`, `end` (absolute children of `Stack`).
- **Margins & Spacing:** `margin`, `marginHorizontal`, `marginVertical`, `marginStart`, `marginEnd`, `marginTop`, `marginBottom`, `gap`, `rowGap`, `columnGap`.
- **Padding shorthands:** `padding`, `paddingHorizontal`, `paddingVertical`.
- **Shape & Typography:** `borderRadius`, corner radii (`borderTopStartRadius`, etc.), `fontSize`, `lineHeight`, `letterSpacing`.
- **Visibility:** `display: "none" | "flex"`. Setting `display: "none"` collapses the element to 0×0 during layout without placing its children.

### Structural Keys (Stay Build-Time Only)
The structural keys `flexDirection`, `flexWrap`, `alignItems`, `justifyContent`, `alignSelf`, and `position` determine the layout container or coordinate pass. Changing them at runtime requires a ternary between two distinct elements (`cond ? <Row>...</Row> : <Column>...</Column>`), not a style slot. Attempting to use a dynamic expression on them produces a clear build-time error.

### Percentages & Unset Values
- Strings ending in `"%"` (e.g., `"50%"`) resolve relative to the bounded constraint of the parent container on that axis (`constraints.maxWidth` / `constraints.maxHeight`). If the parent axis is unbounded (infinite), a developer warning is logged and the percentage is ignored.
- Unset values (`undefined`, `null`, `"auto"`) fall back to default/unconstrained layout behavior rather than coercing to 0.

### For the iOS Runtime
The iOS runtime (SwiftUI) mirrors these layout semantics without Yoga:
1. **Dynamic Dimensions & Offsets:** Slots map to `.frame(width:height:)` and `.offset(x:y:)` closures or dynamic view modifiers observing the node's slot state.
2. **Display None:** In SwiftUI, `display: "none"` conditionally removes the element from rendering or wraps it in `.frame(width: 0, height: 0).hidden()`.
3. **Typography & Corner Radii:** `.font(.system(size:))` and `.clipShape(RoundedRectangle(cornerRadius:))` observe slot updates directly.
4. **Percentage Widths:** Handled via `GeometryReader` or `.containerRelativeFrame(.horizontal)`.

## 10. Later: NativeWind `className` translator (v1.1, not decided)
ZeroBill's RN app is written with NativeWind classes (`className="flex-row px-[16px] bg-white rounded-xl"`), not
mostly `StyleSheet`. A compile-time translator from a Tailwind subset to the style keys above would make porting
close to mechanical. It is pure build-time work (no runtime cost) and can come after v1.

## 10. What is built (status, 2026-10-29, step 1.4)
Built and checked on the Lava: every layout key in §3.1 except `flexWrap`, `display` and absolute `start`+`end`
together (stretch); drawing: `backgroundColor`, `borderRadius` (one value), `borderWidth` / `borderColor`,
`borderTopWidth` / `borderBottomWidth` (in `borderColor`), `opacity`, `overflow`, `elevation` + `shadowColor`,
`transform` (scale, scaleX/Y, translateX/Y, rotate; build-time only); text: `color`, `fontSize`, `fontWeight`,
`fontStyle`, `textAlign`, `textDecorationLine`, `lineHeight`, `letterSpacing`, `numberOfLines`; `position:
'absolute'` + `top/bottom/start/end` + `zIndex` inside `Stack`. Images clip to their rounded shape by default.
Not yet: per-corner radii, per-side border colours, `shadowOffset/Opacity/Radius`, `textTransform`, `fontFamily`,
dynamic transforms. They are build errors until added.
(Update 2026-11-09, registry 24: `flexWrap` / `alignContent` on a Row and `fontFamily` with app fonts are built;
guide notes §84, §85. Most of the others came in RN props pack 2 and registry 23-24.)
