* {
  box-sizing: border-box;
}

html {
  background: var(--ground);
  color: var(--ink);
  font-family: Inter, ui-sans-serif, system-ui, sans-serif;
}

body {
  width: 100%;
  min-width: 0;
  min-height: 100vh;
  margin: 0;
  padding: clamp(1rem, 3vw, 3rem);
}

header,
main,
footer,
.reference {
  width: min(100%, 120rem);
  margin-inline: auto;
}

header {
  display: flex;
  justify-content: center;
  padding-block: clamp(1rem, 4vw, 3rem);
}

header img {
  display: block;
  width: clamp(9rem, 24vw, 15rem);
  max-height: 8rem;
  object-fit: contain;
}

/* AUDIT-P2 §E: the reference tree band sits between the mark and the grid. No words, no controls.
   P9: it dominates the fold and is centred on the page axis — a centring grid, and the SVG's own box is
   square (full frame, crop:false) so "centre the box" and "centre the tree" are the same thing. */
.reference {
  display: grid;
  place-items: center;
  height: clamp(360px, 46vw, 640px);
  margin-bottom: clamp(1rem, 3vw, 2.5rem);
}

.reference svg {
  aspect-ratio: 1 / 1;
  margin-inline: auto;
}

main {
  display: grid;
  grid-template-columns: repeat(2, 1fr);
  gap: clamp(0.75rem, 1.8vw, 1.4rem);
}

/* AUDIT-P2 §E: one cell = [tile 16:9 | tree 1:1], the tree exactly as tall as the tile.
   16fr : 9fr makes that exact — the tile's height is 9/16 of its own width, which is the tree's width. */
.cell {
  display: grid;
  grid-template-columns: 16fr 9fr;
  gap: 0.45rem;
  min-width: 0;
}

.tile {
  position: relative;
  min-width: 0;
  overflow: hidden;
  aspect-ratio: 16 / 9;
  border: 1px solid color-mix(in srgb, var(--dim) 24%, transparent);
  border-radius: 4px;
  background: var(--surface);
  color: var(--ink);
}

.tile img {
  width: 100%;
  height: 100%;
  display: block;
  object-fit: cover;
  transform: scale(1);
  transition: transform 320ms cubic-bezier(.2, .8, .2, 1);
}

/* BEV-1 carry-over (AUDIT-P2 §E): a solid scrim under the name/numeral row so nothing in the capture
   collides with it. The LIVE pill keeps its own corner. */
/* P9: the caption bar is a FULL-WIDTH band on the tile's own bottom edge — it meets the left, right and
   bottom edges exactly (no 8 px inset, no seam), carries the tile's inner radius (4 px border-box − 1 px
   border), and is opaque, so no pixel of the capture bleeds out from under it. The tile's overflow:hidden
   does the corner clipping. */
.tile__meta {
  position: absolute;
  inset: auto 0 0 0;
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 0.5rem;
  border-radius: 0 0 3px 3px;
  padding: 0.36rem 0.6rem;
  background: var(--ground);
  box-shadow: 0 -0.7rem 1rem color-mix(in srgb, var(--ground) 72%, transparent);
  font-size: clamp(0.78rem, 1.3vw, 0.95rem);
  font-weight: 650;
  line-height: 1.2;
}

.tile__meta--ordinal {
  justify-content: flex-end;
}

.tile__ordinal {
  color: var(--dim);
  font-variant-numeric: tabular-nums;
  font-weight: 500;
}

.tile__status {
  position: absolute;
  top: 8px;
  right: 8px;
  border: 1px solid var(--accent);
  border-radius: 4px;
  padding: 0.18rem 0.5rem;
  background: color-mix(in srgb, var(--ground) 78%, transparent);
  color: var(--accent);
  font-size: 0.7rem;
  letter-spacing: 0.08em;
  text-transform: uppercase;
}

.cell__tree {
  display: flex;
  align-items: center;
  justify-content: center;
  aspect-ratio: 1 / 1;
  min-width: 0;
  border: 1px solid transparent;
  border-radius: 4px;
}

.cell__tree svg,
.reference svg,
.tree-stage svg {
  display: block;
  width: auto;
  height: 100%;
  max-width: 100%;
}

/* Fallback only. tree-style.js lifts the renderer's own <style> out of each SVG and adopts it through
   CSSOM (a strict CSP, AUDIT-P1 §E7, refuses inline stylesheets); an adopted sheet is later in the cascade
   and at equal specificity, so it wins. These rules keep a tree visible on a browser without constructable
   stylesheets, coloured from the --t0..--t4 custom properties the page sets. */
.tree-host svg .w { fill: none; stroke: var(--t1); stroke-linecap: round; stroke-linejoin: round; }
.tree-host svg .b { stroke: var(--t0); }
.tree-host svg .m { stroke: var(--t1); }
.tree-host svg .l { fill: var(--t2); }
.tree-host svg .f { fill: var(--t3); }
.tree-host svg .h { fill: var(--t4); }

a,
button,
[role='button'] {
  transition: transform 220ms cubic-bezier(.2, .8, .2, 1),
    box-shadow 220ms cubic-bezier(.2, .8, .2, 1),
    filter 220ms cubic-bezier(.2, .8, .2, 1),
    border-color 220ms cubic-bezier(.2, .8, .2, 1),
    color 220ms cubic-bezier(.2, .8, .2, 1);
}

a {
  text-decoration: none;
}

a:hover,
a:focus-visible,
button:hover,
button:focus-visible,
[role='button']:hover,
[role='button']:focus-visible {
  border-color: var(--accent);
  box-shadow: 0 0 1.4rem color-mix(in srgb, var(--accent) 22%, transparent);
  filter: brightness(1.08);
  outline: none;
  transform: translateY(-3px);
}

a:hover .tile__status,
a:focus-visible .tile__status {
  color: var(--accent);
}

a:hover img,
a:focus-visible img {
  transform: scale(1.04);
}

a:active,
button:active,
[role='button']:active {
  box-shadow: none;
  transform: translateY(2px);
}

a:active img {
  transform: scale(1);
}

/* P9 fruit law — every fruit, on every surface, is a control. Scoped to `svg .node` (the ONE thing the
   renderer contract guarantees), so it survives a re-write of the tree engine. The page-wide lift/glow
   would fight the scale, so it is cancelled here; specificity (0,2,1) beats a:hover (0,1,1). */
svg .node {
  cursor: pointer;
  transform-box: fill-box;
  transform-origin: center;
  transition: transform 180ms cubic-bezier(.2, .8, .2, 1),
    filter 180ms cubic-bezier(.2, .8, .2, 1);
}

svg .node:hover,
svg .node:focus-visible {
  box-shadow: none;
  filter: drop-shadow(0 0 6px rgba(255, 255, 255, 0.55));
  outline: none;
  transform: scale(1.45);
}

svg .node:active {
  box-shadow: none;
  filter: drop-shadow(0 0 6px rgba(255, 255, 255, 0.55));
  transform: scale(1.1);
}

footer {
  padding-block: 2rem 0.5rem;
  color: var(--dim);
  font-size: 0.75rem;
  text-align: center;
}

/* ---- tree page (/<slug>/tree) ---- */

.page-tree header.mark-small {
  justify-content: flex-start;
  padding-block: 0 clamp(0.75rem, 2vw, 1.5rem);
}

.page-tree header.mark-small a {
  display: inline-block;
  border-radius: 4px;
}

.page-tree header.mark-small img {
  width: clamp(4rem, 9vw, 6rem);
  max-height: 3.5rem;
}

.page-tree main {
  position: relative;
  display: block;
}

.page-tree h1 {
  margin: 0 0 clamp(0.5rem, 1.5vw, 1rem);
  font-size: clamp(1.1rem, 2.4vw, 1.6rem);
  font-weight: 650;
  letter-spacing: 0.01em;
}

/* P9: the tree IS the page. tree-page.js sets the width to
   min(innerHeight - 140, innerWidth - 48) px — the same number it renders at — and re-does it on resize,
   so the drawn size and the shown size always agree and the tree fills the viewport. */
.tree-stage {
  aspect-ratio: 1 / 1;
  margin-inline: auto;
  width: 100%;
  max-width: 100%;
}

.tree-stage > svg {
  width: 100%;
  height: 100%;
}

.card {
  position: absolute;
  z-index: 2;
  max-width: min(17rem, 78vw);
  border: 1px solid color-mix(in srgb, var(--dim) 30%, transparent);
  border-radius: 4px;
  padding: 0.6rem 0.7rem;
  background: color-mix(in srgb, var(--surface) 96%, transparent);
  box-shadow: 0 0.6rem 1.6rem color-mix(in srgb, #000 55%, transparent);
  font-size: 0.82rem;
}

.card__label {
  margin: 0 0 0.25rem;
  font-weight: 650;
  line-height: 1.3;
}

.card__meta {
  margin: 0 0 0.5rem;
  color: var(--dim);
  font-variant-numeric: tabular-nums;
}

/* P9: the card is TETHERED to its fruit — a triangular pointer on the edge facing it, placed by
   tree-page.js at the fruit's own centre height. --left/--right name the side the CARD sits on. */
.card__tip {
  position: absolute;
  top: 1rem;
  width: 0;
  height: 0;
  margin-top: -7px;
  border-top: 7px solid transparent;
  border-bottom: 7px solid transparent;
}

.card--right .card__tip {
  left: -8px;
  border-right: 8px solid var(--surface);
}

.card--left .card__tip {
  right: -8px;
  border-left: 8px solid var(--surface);
}

.card__actions {
  display: flex;
  flex-wrap: wrap;
  gap: 0.4rem;
}

.card .btn {
  border: 1px solid color-mix(in srgb, var(--dim) 40%, transparent);
  border-radius: 4px;
  padding: 0.3rem 0.55rem;
  background: transparent;
  color: var(--ink);
  font: inherit;
  font-size: 0.72rem;
  font-weight: 650;
  letter-spacing: 0.08em;
  cursor: pointer;
}

.card .btn--primary {
  border-color: var(--accent);
  background: var(--accent);
  color: var(--ground);
  font-weight: 750;
}

.card .btn--primary:hover,
.card .btn--primary:focus-visible {
  border-color: var(--accent);
  color: var(--ground);
}

.fork {
  overflow-x: auto;
  margin: 0.5rem 0 0;
  border-radius: 4px;
  padding: 0.5rem;
  background: var(--ground);
  color: var(--accent);
  font-family: ui-monospace, SFMono-Regular, Consolas, monospace;
  font-size: 0.72rem;
  white-space: pre-wrap;
  word-break: break-all;
}

.fork a {
  color: var(--ink);
  display: inline-block;
  border-radius: 4px;
}

@media (max-width: 1100px) {
  main {
    grid-template-columns: 1fr;
  }
}

/* P9 phone: the 16fr:9fr side-by-side cell becomes a stack — the tile full width, then the tree BELOW it
   at the FULL cell width (square). Above 560 px the side-by-side cell is untouched. */
@media (max-width: 560px) {
  .grid { row-gap: 3.25rem; }
  .cell { gap: 0.2rem; }
  .cell__tree { width: 100%; aspect-ratio: 16 / 7; margin-inline: auto; }
  .cell {
    grid-template-columns: 1fr;
  }

  .cell__tree {
    width: 100%;
    aspect-ratio: 1 / 1;
  }
}

@media (prefers-reduced-motion: reduce) {
  a,
  button,
  [role='button'],
  a:hover,
  a:focus-visible,
  a:active,
  button:hover,
  button:focus-visible,
  button:active,
  [role='button']:hover,
  [role='button']:focus-visible,
  [role='button']:active,
  a:hover img,
  a:focus-visible img,
  a:active img,
  svg .node,
  svg .node:hover,
  svg .node:focus-visible,
  svg .node:active {
    transform: none;
    transition-property: box-shadow, filter, border-color, color, fill;
  }
}
