:root {
  --md-surface: hsla(134, 50%, 12%, 0.65);
  --md-outline: hsla(67, 60%, 70%, 0.2);
  --md-outline-focus: hsla(67, 100%, 80%, 0.7);
  --md-primary: hsl(67, 100%, 89%);
  --md-on-surface: hsl(67, 100%, 89%);
  --md-blur: 20px;
  --toolbar-button-highlight: hsla(67, 60%, 45%, 0.2);
  --md-font: "Lato", sans-serif;
  --md-elevation-1:
    0 1px 3px hsla(0, 0%, 0%, 0.35), 0 1px 2px hsla(0, 0%, 0%, 0.25);
  --md-elevation-2:
    0 3px 8px hsla(0, 0%, 0%, 0.4), 0 1px 4px hsla(0, 0%, 0%, 0.3);
  /* Extended toolbars (popping up from a toolbar button) use a lighter
     green than the main toolbar's --md-surface. */
  --toolbar-extended-surface: hsla(134, 40%, 24%, 0.85);
  /* Bottom toolbar's distance from the viewport bottom: clears the
     bottom row (dev-beta badge, position label) plus a small margin. */
  --bottom-toolbar-offset: 2.5rem;
  /* Height #stats/#inspect-organism keep free at the viewport bottom:
     clears the fixed-height dev-beta badge (labels.bottom-left); update
     if the badge's own sizing changes. */
  --bottom-reserved: 2.25rem;
  /* Width of the top-right vertical toolbars (organism-toolbar,
     utility-toolbar); the panels keep clear of them. */
  --side-toolbar-width: 3rem;
}
/* While the bottom toolbar is shown, the panels end 0.5rem above it
   (SimulationOverlay sets the class and measures --bottom-toolbar-height). */
:root.bottom-toolbar-shown {
  --bottom-reserved: calc(
    var(--bottom-toolbar-offset) + var(--bottom-toolbar-height, 0px) + 0.5rem
  );
}

* {
  box-sizing: border-box;
}

html,
body {
  margin: 0;
  padding: 0;
  height: 100%;
  width: 100%;
  overflow: hidden; /* Prevent scrolling */
  position: fixed; /* Ensure no scrolling */
  touch-action: none; /* Disable touch scrolling behaviors */
  -webkit-user-select: none;
  user-select: none;
  -webkit-touch-callout: none;
}

body {
  background-color: hsl(0, 0%, 0%);
  font-family: var(--md-font);
  font-size: 0.7rem;
  color: var(--md-on-surface);
  -webkit-overflow-scrolling: none; /* Disable momentum scrolling on iOS */
}

main {
  display: flex;
  width: 100%;
  height: 100%;
  height: 100vh;
  position: fixed;
  top: 0;
  left: 0;
  z-index: 9999;
  overflow: hidden; /* Prevent scrolling within main */
  touch-action: none; /* Disable touch scrolling behaviors */
}

canvas {
  width: 100%;
  height: 100%;
  display: block; /* Remove any inline spacing */
  touch-action: none; /* Prevent touch scrolling */
}

/* Top-left column holding both panels: inspect above stats. A panel with
   display:none takes no space, so the other moves up/down on its own. */
#panels {
  position: fixed;
  top: 0;
  left: 0;
  margin: 0.5rem;
  display: flex;
  flex-direction: column;
  gap: 0.5rem;
  /* Clears the top-right toolbars: their width, their 0.5rem offset from
     the edge, the column's 0.5rem margins and a 0.5rem gap. */
  max-width: calc(100vw - var(--side-toolbar-width) - 2rem);
  /* Both panels together end --bottom-reserved above the viewport bottom
     (above the Beta line, or the bottom toolbar while it is shown).
     0.5rem = the column's top margin. */
  max-height: calc(100vh - 0.5rem - var(--bottom-reserved));
  max-height: calc(100dvh - 0.5rem - var(--bottom-reserved));
}
#stats,
#inspect-organism {
  display: none;
  background-color: var(--md-surface);
  width: auto;
  max-width: 100%;
  min-height: 0;
  padding: 1rem;
  border: 1px solid var(--md-outline);
  border-radius: 16px;
  overflow: auto;
  overscroll-behavior: contain; /* Prevent scroll chaining */
  scrollbar-width: none; /* Firefox */
  touch-action: pan-y; /* Allow vertical scrolling on touch devices */
  box-shadow: var(--md-elevation-1);
  backdrop-filter: blur(var(--md-blur));
}
/* When space runs out, inspect keeps its height (up to the column) and
   stats shrinks and scrolls first. */
#inspect-organism {
  flex: 0 0 auto;
  /* % would not resolve (the column has no definite height). */
  max-height: inherit;
}
#stats {
  flex: 0 1 auto;
}
#stats::-webkit-scrollbar,
#inspect-organism::-webkit-scrollbar {
  display: none; /* Safari and Chrome */
}

labels {
  position: fixed;
  display: flex;
}
labels.right {
  top: auto;
  bottom: 0.5rem;
  left: auto;
  right: 0.5rem;
  gap: 0;
}
labels.bottom-left {
  top: auto;
  bottom: 0.5rem;
  left: 0.5rem;
  right: auto;
}

/* `.label` lets non-`<label>` elements (e.g. the tutorial's `<button>`s)
   opt into the same pill styling as the `<label>` elements below. */
.label,
label {
  font-size: 0.8rem;
  padding: 0.4rem 0.8rem;
  border-radius: 999px;
  cursor: pointer;
  background-color: hsla(67, 60%, 40%, 0.25);
  color: var(--md-primary);
  border: 1px solid var(--md-outline);
  font-weight: 600;
  transition: background-color 0.2s;
}

@media (max-width: 768px) {
  .label,
  label {
    font-size: 1rem;
  }
}

labels.right label {
  transition:
    opacity 0.5s ease,
    background-color 0.5s ease,
    border-color 0.5s ease;
  opacity: 0;
  background-color: transparent;
  border-color: transparent;
  width: 0;
  padding: 0;
  margin: 0;
  overflow: hidden;
  white-space: nowrap;
}
labels.right label.highlight {
  opacity: 1;
  background-color: hsla(67, 60%, 40%, 0.25);
  border-color: var(--md-outline);
  width: auto;
  padding: 0.4rem 0.8rem;
  margin-left: 0.5rem;
}

/* Shared sizing for small corner-pinned text (status labels, the dev-beta
   badge) — smaller and less padded than the pill buttons/labels above.
   Listed as a selector group (not a standalone utility class) so each
   side keeps the specificity it needs: `.status-text.highlight` must
   still outrank `labels.right label.highlight`'s padding below. */
labels.right label.status-text.highlight,
.dev-beta-badge {
  font-size: 0.6rem;
  padding: 0.4rem 0.2rem;
}

/* Status text labels (smaller, no background) */
labels.right label.status-text.highlight {
  background-color: transparent;
  border-color: transparent;
}

.label,
label {
  background-color: hsla(67, 60%, 40%, 0.5);
}

.label:not(.active):hover,
.label:not(.active):active,
label:not(.active):hover,
label:not(.active):active {
  background-color: hsla(67, 60%, 45%, 0.7);
}

/* ── Toolbar (components/Toolbar.tsx) ─────────────────────────────── */
/* A pill-shaped bar of equally sized round buttons, each an icon over a
   permanent label. `.toolbar-dock` is the positioned box (placed by a
   modifier class such as `.bottom-toolbar`); it will also hold an open
   extended toolbar above (horizontal) or beside (vertical) the bar.
   Presses animate via the Web Animations API (Toolbar.tsx), so the bar
   and buttons carry no CSS transform of their own. */
.toolbar-dock {
  display: flex;
  flex-direction: column;
  align-items: center;
  pointer-events: none;
}
.toolbar-dock[data-orientation="vertical"] {
  flex-direction: row;
}

/* A toolbar and its extended toolbars share one width rule. */
.toolbar,
.toolbar-extended {
  /* Widest a button gets: phones fill their width (minus the dock's
     margin), wider screens cap at button count x this. */
  --toolbar-button-size: 4.25rem;
  --toolbar-max-length: calc(
    var(--toolbar-button-count, 1) * var(--toolbar-button-size) + 0.5rem
  );
}

.toolbar {
  display: grid;
  grid-auto-flow: column;
  grid-auto-columns: minmax(0, 1fr);
  gap: 0.25rem;
  width: 100%;
  max-width: var(--toolbar-max-length);
  padding: 0.25rem;
  border-radius: 999px;
  background-color: var(--md-surface);
  border: 1px solid var(--md-outline);
  box-shadow: var(--md-elevation-1);
  backdrop-filter: blur(var(--md-blur));
  pointer-events: auto;
  transform-origin: 50% 100%;
}
.toolbar-dock[data-orientation="vertical"] > .toolbar {
  grid-auto-flow: row;
  grid-auto-rows: minmax(0, 1fr);
  grid-auto-columns: auto;
  width: auto;
  max-width: none;
  max-height: var(--toolbar-max-length);
  transform-origin: 100% 50%;
}

/* Lays out a single child: .toolbar-button-content (icon over label). */
.toolbar-button {
  display: grid;
  place-items: center;
  min-width: 0;
  padding: 0.3rem 0.25rem 0.25rem;
  border: none;
  border-radius: 999px;
  background-color: transparent;
  color: var(--md-primary);
  font: inherit;
  cursor: pointer;
  -webkit-tap-highlight-color: transparent;
  transition: background-color 0.2s;
}
/* A button being pressed (or hovered with a mouse) is highlighted; so is
   one whose setting is on ([aria-pressed="true"], below). */
@media (hover: hover) {
  .toolbar-button:hover {
    background-color: var(--toolbar-button-highlight);
  }
}
/* [data-pressed]: held by a pointer (set by Toolbar.tsx alongside the
   swell); :active covers keyboard presses. */
.toolbar-button:active,
.toolbar-button[data-pressed] {
  background-color: hsla(67, 60%, 45%, 0.35);
}
.toolbar-button:focus-visible {
  outline: 2px solid var(--md-outline-focus);
  outline-offset: -2px;
}

.toolbar-button-content {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 0.15rem;
  max-width: 100%;
}

.toolbar-button-icon {
  width: 1.35rem;
  height: 1.35rem;
  fill: currentColor;
  flex: none;
}
/* Text shown in place of the icon (e.g. the Info button's info mode),
   in the icon's box so the button keeps its size. */
.toolbar-button-symbol {
  display: grid;
  place-items: center;
  width: 1.35rem;
  height: 1.35rem;
  font-size: 0.95rem;
  line-height: 1;
  flex: none;
}
.toolbar-button-label {
  max-width: 100%;
  font-size: 0.55rem;
  font-weight: 700;
  line-height: 1.1;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

/* Top-right: the organism-toolbar stacked above the utility-toolbar,
   each a fixed-width vertical bar sized to its buttons. */
.top-right-toolbars {
  position: fixed;
  top: 0.5rem;
  right: 0.5rem;
  display: flex;
  flex-direction: column;
  gap: 0.5rem;
  pointer-events: none;
}
.top-right-toolbars .toolbar-dock[data-orientation="vertical"] > .toolbar {
  width: var(--side-toolbar-width);
  /* Room above the first and below the last button; slim sides so the
     longest labels ("Control", "Inspect") fit the narrow bar. */
  padding: 0.4rem 0.125rem;
}
/* The narrow bar's buttons are nearly round: extra bottom padding and a
   smaller label keep the widest labels ("Inspect", "Control") inside
   the highlight. */
.top-right-toolbars .toolbar-button {
  padding: 0.3rem 0 0.4rem;
}
.top-right-toolbars .toolbar-button-label {
  font-size: 0.5rem;
}

/* Bottom-centred toolbar spanning the screen width minus a margin, up to
   the bar's own max-width, sitting above the bottom badge/status row. */
.toolbar-dock.bottom-toolbar {
  position: fixed;
  left: 1.25rem;
  right: 1.25rem;
  bottom: var(--bottom-toolbar-offset);
  justify-content: flex-end;
}

/* ── Extended toolbars (docs/plans/phrases.md §1.1) ─────────────────────
   A panel popping up above a toolbar button (e.g. Talk → phrases-toolbar).
   Same style as .toolbar, but it grows upward, has rounded-rect corners
   instead of a 180° pill end, and a 90° pointer aimed at the button that
   opened it. As wide as the bar: Toolbar.tsx passes the bar's
   --toolbar-button-count and the button's --toolbar-anchor-index.
   The panel and its pointer are ONE surface (::before), masked to the
   rounded rectangle plus the triangle, so they always match and never
   overlap (a translucent tint would darken where they did). A flat tint,
   no backdrop blur: browsers blur a small masked or rotated shape
   differently from the panel, so a blurred pointer never quite matched
   (and a blur over the WebGL canvas costs a pass every frame). The outlines
   are drawn on top. */
.toolbar-extended {
  /* Pointer: a 90° triangle, the lower half of a --pointer-size square
     turned 45°, centred over the anchor button (the bar's columns are
     equal, so its centre is at (index + 0.5) / count of the width). */
  --pointer-size: 0.8rem;
  --pointer-half-base: calc(var(--pointer-size) * 0.7071);
  --pointer-x: calc(
    (var(--toolbar-anchor-index) + 0.5) / var(--toolbar-button-count) * 100%
  );
  position: relative;
  width: 100%;
  max-width: var(--toolbar-max-length);
  --panel-radius: 1rem;
  /* The pointer's tip sits just above the bar. */
  margin-bottom: calc(var(--pointer-half-base) + 0.2rem);
  pointer-events: auto;
}
/* The surface: the panel's box extended down by the pointer, masked
   to the rounded panel (two bands + four corner circles) and the pointer
   (a 90° wedge in the band below the panel, apex at the bottom). */
.toolbar-extended::before {
  --p: var(--pointer-half-base);
  --r: var(--panel-radius);
  content: "";
  position: absolute;
  inset: 0 0 calc(-1 * var(--p)) 0;
  background-color: var(--toolbar-extended-surface);
  pointer-events: none;
  mask:
    linear-gradient(#000 0 0) left 0 top var(--r) / 100%
      calc(100% - var(--p) - 2 * var(--r)) no-repeat,
    linear-gradient(#000 0 0) left var(--r) top 0 /
      calc(100% - 2 * var(--r)) calc(100% - var(--p)) no-repeat,
    radial-gradient(circle closest-side, #000 98%, transparent) left 0 top 0 /
      calc(2 * var(--r)) calc(2 * var(--r)) no-repeat,
    radial-gradient(circle closest-side, #000 98%, transparent) right 0 top 0 /
      calc(2 * var(--r)) calc(2 * var(--r)) no-repeat,
    radial-gradient(circle closest-side, #000 98%, transparent) left 0 bottom
      var(--p) / calc(2 * var(--r)) calc(2 * var(--r)) no-repeat,
    radial-gradient(circle closest-side, #000 98%, transparent) right 0 bottom
      var(--p) / calc(2 * var(--r)) calc(2 * var(--r)) no-repeat,
    conic-gradient(from -45deg at var(--pointer-x) 100%, #000 90deg, transparent 0)
      left 0 bottom 0 / 100% var(--p) no-repeat;
}
/* The outline, drawn separately so it can leave a gap where the pointer
   joins: a plain border would run across the pointer's base as a seam. */
.toolbar-extended-items::before {
  content: "";
  position: absolute;
  inset: 0;
  border: 1px solid var(--md-outline);
  border-radius: inherit;
  pointer-events: none;
  mask:
    linear-gradient(#000 calc(100% - 1px), transparent 0),
    linear-gradient(
        to right,
        #000 calc(var(--pointer-x) - var(--pointer-half-base)),
        transparent 0 calc(var(--pointer-x) + var(--pointer-half-base)),
        #000 0
      )
      bottom / 100% 1px no-repeat;
}
/* The pointer's outline (its surface is part of ::before). */
.toolbar-extended::after {
  content: "";
  position: absolute;
  top: 100%;
  left: var(--pointer-x);
  width: var(--pointer-size);
  height: var(--pointer-size);
  transform: translate(-50%, -50%) rotate(45deg);
  border-right: 1px solid var(--md-outline);
  border-bottom: 1px solid var(--md-outline);
  clip-path: polygon(100% 0, 100% 100%, 0 100%); /* the lower half only */
  pointer-events: none;
}
/* The items, on the panel's frosted surface: one centred list of inline
   boxes. The browser lays them out like words, fitting as many as it can on
   each line before wrapping, so no room that could hold an item is left
   empty; every line (the last included) is centred, and the panel grows
   upward as lines are added. */
.toolbar-extended-items {
  position: relative; /* above the surface; holds the outline (::before) */
  padding: calc(0.3rem + 1px);
  border-radius: var(--panel-radius);
  /* No drop shadow: it would fall on the pointer and darken it. */
  text-align: center;
  line-height: 0; /* line spacing comes from the items' margin only */
}
.toolbar-extended-chip,
.toolbar-extended-emoji {
  display: inline-block; /* only as big as its content */
  margin: 0.125rem;
  vertical-align: middle;
  line-height: normal;
  border: 0;
  font-family: var(--md-font);
  cursor: pointer;
  -webkit-tap-highlight-color: transparent;
}
/* A text item: a white bubble with black text. */
.toolbar-extended-chip {
  padding: 0.3rem 0.55rem;
  border-radius: 0.75rem;
  background-color: #fff;
  color: #000;
  font-size: 0.85rem;
  font-weight: 700;
  white-space: nowrap;
}
/* An emoji item: bare, no bubble. */
.toolbar-extended-emoji {
  padding: 0.05rem 0.1rem;
  background: none;
  font-size: 1.5rem;
  line-height: 1;
}
/* A short hint under the items, muted so it reads as help, not a button. */
.toolbar-extended-hint {
  margin: 0.3rem 0 0.05rem;
  line-height: normal;
  font-family: var(--md-font);
  font-size: 0.7rem;
  color: var(--md-on-surface);
  opacity: 0.75;
}
/* Unpressable for now (e.g. phrases during their cooldown): greyed out. */
.toolbar-extended-chip:disabled,
.toolbar-extended-emoji:disabled {
  opacity: 0.45;
  filter: grayscale(1);
  cursor: default;
}
.toolbar-extended-chip:focus-visible,
.toolbar-extended-emoji:focus-visible {
  outline: 2px solid var(--md-outline-focus);
  outline-offset: 1px;
}
/* The button whose extended toolbar is open, and a button whose setting
   is on (e.g. its panel is open). */
.toolbar-button[aria-expanded="true"],
.toolbar-button[aria-pressed="true"] {
  background-color: var(--toolbar-button-highlight);
}

/* Phrases-toolbar: a light translucent tint of the bar's green. Its phrases
   wobble gently to feel pressable: three independent eased swings
   (sideways, up-down, tilt) with different periods, so the combined path
   curves smoothly with no stops. Only compositor properties (translate,
   rotate, transform) are animated — no layout or paint per frame — and
   only while the toolbar is open. Each item starts at its own phase
   (--item-index, set by Toolbar.tsx), so they never move in step. */
.phrases-toolbar {
  /* The bar's dark green (--md-surface), lighter: the phrases stand out
     on it and the world shows through. */
  --toolbar-extended-surface: hsla(134, 50%, 12%, 0.5);
}
.phrases-toolbar .toolbar-extended-chip,
.phrases-toolbar .toolbar-extended-emoji {
  /* Smooth edges while tilting: a rotated layer's edge pixels are not
     antialiased, so its rounded edge shows hairlines. A transparent outline
     pads the layer with transparent pixels, so the visible edge is sampled
     smoothly; hidden backfaces keep the layer from being re-rasterised. */
  outline: 1px solid transparent;
  backface-visibility: hidden;
  animation:
    phrase-drift-x 2.3s ease-in-out infinite alternate,
    phrase-drift-y 1.7s ease-in-out infinite alternate,
    phrase-tilt 2.9s ease-in-out infinite alternate;
  animation-delay:
    calc(var(--item-index, 0) * -347ms), calc(var(--item-index, 0) * -211ms),
    calc(var(--item-index, 0) * -523ms);
}
@keyframes phrase-drift-x {
  from {
    translate: -0.5px 0;
  }
  to {
    translate: 0.5px 0;
  }
}
@keyframes phrase-drift-y {
  from {
    transform: translateY(-0.5px);
  }
  to {
    transform: translateY(0.5px);
  }
}
@keyframes phrase-tilt {
  from {
    rotate: -0.8deg;
  }
  to {
    rotate: 0.8deg;
  }
}
@media (prefers-reduced-motion: reduce) {
  .phrases-toolbar .toolbar-extended-chip,
  .phrases-toolbar .toolbar-extended-emoji {
    animation: none;
  }
}

/* Permanent beta notice, sized to match the bottom-right status text
   (labels.right label.status-text.highlight, above); positioned via the
   labels.bottom-left container. */
.dev-beta-badge {
  color: var(--md-primary);
  white-space: nowrap;
}
.dev-beta-badge a {
  color: inherit;
  text-decoration: underline;
}

/* Stats panel "More data" link — same color as the surrounding stats
   text in every state (no visited/hover/active differentiation). */
#stats a.more-data {
  display: block;
  margin-top: 0.6rem;
  color: inherit;
  text-decoration: underline;
}

/* ── Tutorial coach marks ──────────────────────────────────────────── */
/* Non-blocking by design: at most one box is shown at a time, floating
   next to (or below) the control it explains, so the user can still
   interact with the rest of the UI — including the very button the box
   is pointing at — while it's visible. */

.tutorial-box {
  position: fixed;
  z-index: 10000;
  display: flex;
  flex-direction: column;
  align-items: flex-end;
  gap: 0.75rem;
  background-color: var(--md-surface);
  border: 1px solid var(--md-outline);
  border-radius: 16px;
  box-shadow: var(--md-elevation-2);
  backdrop-filter: blur(var(--md-blur));
  padding: 1rem 1.25rem;
  max-width: min(20rem, calc(100vw - 2rem));
  color: var(--md-on-surface);
  font-family: var(--md-font);
}

/* Step 1 has no button to anchor to: centered horizontally (matching
   the followed organism, which is always screen-centered) and placed
   below the viewport's vertical center. */
.tutorial-box-centered {
  left: 50%;
  top: calc(50% + 4rem);
  transform: translateX(-50%);
  width: max-content;
}

.tutorial-body {
  font-size: 0.85rem;
  line-height: 1.5;
  margin: 0;
  opacity: 0.9;
}

/* `.tutorial-close` reuses the shared pill-button look from `label`
   (radius, cursor, padding, border, weight, transition) via
   `className="label tutorial-close"`; only the font reset (buttons
   don't inherit the page font like `label` elements elsewhere do) and
   the fill color are button-specific. */
.label.tutorial-close {
  font-family: var(--md-font);
  background: hsla(67, 60%, 40%, 0.4);
}
.label.tutorial-close:hover {
  background: hsla(67, 60%, 40%, 0.6);
}

@media (max-width: 768px) {
  .tutorial-box {
    max-width: calc(100vw - 1.5rem);
    padding: 0.85rem 1rem;
  }
  .tutorial-body {
    font-size: 0.95rem;
  }
  .label.tutorial-close {
    font-size: 0.95rem;
    padding: 0.5rem 1rem;
  }
}
