/* Cadpole web UI — neutral dark theme (Fusion-360-ish), WCAG AA contrast.
   Layout: 3 columns on wide screens; viewport + tab bar + bottom sheet on
   narrow screens (<= 900px). */

:root {
  color-scheme: dark;
  --bg: #1f2023;
  --panel: #26272b;
  --panel-2: #2c2d31;
  --panel-3: #33353a;
  --border: #3a3b40;
  --border-strong: #4a4c53;
  --text: #e4e5e9;
  --muted: #a3a6ad;
  --accent: #5aa2ff;
  --accent-strong: #3b8cff;
  --accent-bg: rgba(90, 162, 255, 0.16);
  --focus: #8ec2ff;
  --warn: #e6b04a;
  --err: #ef6b62;
  --ok: #5fc26b;
  --shadow: 0 8px 28px rgba(0, 0, 0, 0.45);
  --radius: 8px;
  --font: system-ui, -apple-system, "Segoe UI", Roboto, Inter, Helvetica, Arial, sans-serif;
  --mono: ui-monospace, "SF Mono", Menlo, Consolas, "Liberation Mono", monospace;
  --left-w: 290px;
  --right-w: 330px;
  --tabbar-h: 56px;
  /* The bottom sheet's resting heights. Half is enough for a few rows
     of controls, with the model still in view above it while a slider moves;
     the peek leaves nearly all of the model, for the view presets; and the
     handle pulls it up to full. ui/layout.ts does the same arithmetic to
     settle a drag. */
  --sheet-peek: clamp(190px, 27dvh, 250px);
  --sheet-half: 42dvh;
  --sheet-full: calc(100dvh - var(--tabbar-h) - var(--sab) - var(--kbd, 0px) - 8px);
  --sab: env(safe-area-inset-bottom, 0px);
  --sat: env(safe-area-inset-top, 0px);
  --sal: env(safe-area-inset-left, 0px);
  --sar: env(safe-area-inset-right, 0px);
}

* {
  box-sizing: border-box;
}

html,
body {
  height: 100%;
  margin: 0;
  /* No page zoom and no rubber-banding: pinch belongs to the model, and a
     two-finger gesture that scales the document instead of the camera is the
     single most disorienting thing a 3D app on a phone can do. */
  touch-action: manipulation;
  -webkit-text-size-adjust: 100%;
  background: var(--bg);
  color: var(--text);
  font: 13px/1.45 var(--font);
  -webkit-font-smoothing: antialiased;
  overscroll-behavior: none;
}

button,
input,
select,
textarea {
  font: inherit;
  color: inherit;
}

:focus-visible {
  outline: 2px solid var(--focus);
  outline-offset: 2px;
}

::selection {
  background: var(--accent-bg);
}

.sr-only {
  position: absolute !important;
  width: 1px;
  height: 1px;
  margin: -1px;
  padding: 0;
  overflow: hidden;
  clip: rect(0 0 0 0);
  white-space: nowrap;
  border: 0;
}

[hidden] {
  display: none !important;
}

@media (prefers-reduced-motion: reduce) {
  *,
  *::before,
  *::after {
    transition: none !important;
    animation: none !important;
    scroll-behavior: auto !important;
  }
}

/* ---------------------------------------------------------------- splash */

#app {
  height: 100%;
}

.splash {
  height: 100%;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 12px;
  color: var(--muted);
}

.splash-mark {
  width: 54px;
  height: 64px;
}

.fatal {
  padding: 24px;
  color: var(--err);
}

.fatal pre {
  white-space: pre-wrap;
  color: var(--text);
}

/* ---------------------------------------------------------------- shell */

.app {
  height: 100%;
  display: grid;
  grid-template-columns: var(--left-w) minmax(0, 1fr) var(--right-w);
  grid-template-rows: auto minmax(0, 1fr) auto;
  grid-template-areas:
    "toolbar toolbar toolbar"
    "left viewport right"
    "status status status";
  padding-left: var(--sal);
  padding-right: var(--sar);
}

.toolbar-slot {
  grid-area: toolbar;
  min-width: 0;
}

.panel-left {
  grid-area: left;
}

.panel-right {
  grid-area: right;
}

.viewport {
  grid-area: viewport;
}

.status-slot {
  grid-area: status;
  min-width: 0;
}

.tabbar,
.sheet {
  display: none;
}

@media (max-width: 1200px) {
  :root {
    --left-w: 250px;
    --right-w: 290px;
  }
}

.panel {
  background: var(--panel);
  border-right: 1px solid var(--border);
  display: flex;
  flex-direction: column;
  min-height: 0;
  min-width: 0;
}

.panel-right {
  border-right: 0;
  border-left: 1px solid var(--border);
}

.right-top {
  flex: 1 1 55%;
  min-height: 0;
  overflow: auto;
  border-bottom: 1px solid var(--border);
  display: flex;
  flex-direction: column;
}

.right-bottom {
  flex: 1 1 45%;
  min-height: 160px;
  display: flex;
  flex-direction: column;
}

.tabs {
  display: flex;
  gap: 2px;
  padding: 6px 6px 0;
  border-bottom: 1px solid var(--border);
  background: var(--panel-2);
}

.tab {
  background: transparent;
  border: 0;
  border-bottom: 2px solid transparent;
  color: var(--muted);
  padding: 8px 10px;
  cursor: pointer;
  border-radius: 6px 6px 0 0;
}

.tab:hover {
  color: var(--text);
  background: rgba(255, 255, 255, 0.04);
}

.tab.active,
.tab[aria-selected="true"] {
  color: var(--text);
  border-bottom-color: var(--accent);
}

.tabpanel {
  flex: 1 1 auto;
  min-height: 0;
  overflow: auto;
  display: flex;
  flex-direction: column;
}

.model-panel,
.params-panel {
  display: flex;
  flex-direction: column;
  min-height: 0;
}

/* The tree and parameter lists keep their natural height; the enclosing
   tab panel / bottom sheet scrolls. */
.model-panel > .panel-section,
.params-panel > .panel-section {
  flex: 0 0 auto;
}

/* A wide screen's Model tab is two slots, Parameters then the tree;
   each panel goes back to its own slot when the layout widens again. */
.panel-slot {
  flex: 0 0 auto;
  display: flex;
  flex-direction: column;
  min-height: 0;
}

.panel-slot + .panel-slot {
  border-top: 1px solid var(--border);
}

.panel-section {
  padding: 10px 12px;
  min-height: 0;
}

.section-title {
  margin: 0 0 8px;
  font-size: 11px;
  font-weight: 600;
  letter-spacing: 0.06em;
  text-transform: uppercase;
  color: var(--muted);
}

.section-head {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 8px;
  margin-bottom: 6px;
}

.section-head .section-title {
  margin: 0;
}

.section-tools {
  display: flex;
  align-items: center;
  gap: 8px;
}

.listen-toggle {
  padding: 2px 8px;
  font-size: 12px;
}

@media (pointer: coarse) {
  /* Shorter than the other touch buttons: every pixel of this head is taken
     from the messages when the sheet is at its lowest rest. */
  .btn.listen-toggle {
    min-height: 32px;
    padding: 4px 12px;
    font-size: 13px;
  }
}

.empty {
  color: var(--muted);
  padding: 8px 2px;
}

.hint {
  font-size: 12px;
}

.muted {
  color: var(--muted);
}

.small {
  font-size: 12px;
}

.mono {
  font-family: var(--mono);
}

/* -------------------------------------------------------------- buttons */

.btn {
  display: inline-flex;
  align-items: center;
  gap: 6px;
  min-height: 30px;
  padding: 4px 10px;
  border: 1px solid var(--border-strong);
  border-radius: 6px;
  background: var(--panel-3);
  color: var(--text);
  cursor: pointer;
  white-space: nowrap;
  transition: background 0.12s ease, border-color 0.12s ease;
}

.btn:hover {
  background: #3d4046;
}

.btn:active {
  background: #45484f;
}

.btn[aria-pressed="true"] {
  background: #2f4e7a;
  border-color: var(--accent);
}

.btn:disabled {
  opacity: 0.55;
  cursor: default;
}

.btn-primary {
  background: var(--accent-strong);
  border-color: var(--accent-strong);
  color: #0d1420;
  font-weight: 600;
}

.btn-primary:hover {
  background: #5aa2ff;
}

.btn-quiet {
  background: transparent;
  border-color: transparent;
  color: var(--muted);
}

.btn-quiet:hover {
  color: var(--text);
  background: rgba(255, 255, 255, 0.06);
}

.btn-icon {
  font-size: 14px;
  line-height: 1;
}

.btn-group {
  display: inline-flex;
  gap: 0;
}

.btn-group .btn {
  border-radius: 0;
  margin-left: -1px;
}

.btn-group .btn:first-child {
  border-radius: 6px 0 0 6px;
  margin-left: 0;
}

.btn-group .btn:last-child {
  border-radius: 0 6px 6px 0;
}

.icon-btn {
  display: inline-grid;
  place-items: center;
  min-width: 28px;
  min-height: 28px;
  padding: 0 6px;
  border: 0;
  border-radius: 6px;
  background: transparent;
  color: var(--muted);
  cursor: pointer;
}

.icon-btn:hover {
  background: rgba(255, 255, 255, 0.08);
  color: var(--text);
}

.link-btn {
  background: none;
  border: 0;
  padding: 0;
  color: var(--accent);
  cursor: pointer;
  text-decoration: underline;
  text-underline-offset: 2px;
  text-align: left;
}

.link-btn:hover {
  color: var(--focus);
}

.select,
.search,
input[type="text"],
input[type="number"],
input[type="search"],
textarea {
  background: var(--bg);
  border: 1px solid var(--border-strong);
  border-radius: 6px;
  padding: 5px 8px;
  min-height: 30px;
}

input[type="number"] {
  width: 84px;
}

.pill {
  display: inline-flex;
  align-items: center;
  min-height: 22px;
  padding: 0 8px;
  border-radius: 999px;
  background: var(--panel-3);
  color: var(--muted);
  font-size: 11px;
  letter-spacing: 0.02em;
}

.pill-wasm {
  color: var(--ok);
}

.pill-mock {
  color: var(--warn);
}

.conn-live {
  color: var(--ok);
}

.conn-reconnecting,
.conn-connecting {
  color: var(--warn);
}

.conn-offline {
  color: var(--err);
}

.conn-static {
  color: var(--muted);
}

.chip {
  display: inline-flex;
  align-items: center;
  gap: 6px;
  padding: 2px 8px;
  border-radius: 999px;
  background: var(--panel-3);
  font-size: 12px;
}

.chip.primary {
  outline: 1px solid var(--accent);
}

.chip-kind {
  color: var(--muted);
  text-transform: uppercase;
  font-size: 10px;
  letter-spacing: 0.06em;
}

.chip-x {
  background: none;
  border: 0;
  color: var(--muted);
  cursor: pointer;
  padding: 0 2px;
  font-size: 14px;
  line-height: 1;
}

.chip-x:hover {
  color: var(--text);
}

.chips {
  display: flex;
  flex-wrap: wrap;
  gap: 6px;
  margin-bottom: 10px;
}

.sev-error {
  color: var(--err);
}

.sev-warning {
  color: var(--warn);
}

.sev-info {
  color: var(--accent);
}

.chip.ok {
  color: var(--ok);
}

/* -------------------------------------------------------------- toolbar */

.toolbar {
  display: flex;
  align-items: center;
  gap: 6px 10px;
  min-height: 44px;
  padding: 6px 10px;
  padding-top: calc(6px + var(--sat));
  background: var(--panel-2);
  border-bottom: 1px solid var(--border);
  overflow-x: auto;
  overflow-y: visible;
  scrollbar-width: thin;
}

/* More controls than the window is wide: the bar scrolls, and the edge it
   scrolls towards fades so the last control does not read as a cut-off one
   (ui/toolbar.ts sets the classes). */
.toolbar::before,
.toolbar::after {
  content: "";
  position: sticky;
  flex: 0 0 32px;
  align-self: stretch;
  z-index: 2;
  pointer-events: none;
  display: none;
}

.toolbar::before {
  left: -10px;
  order: -1;
  margin-right: -42px;
  background: linear-gradient(to right, var(--panel-2) 30%, transparent);
}

.toolbar::after {
  right: -10px;
  margin-left: -42px;
  background: linear-gradient(to left, var(--panel-2) 30%, transparent);
}

.toolbar.more-left::before,
.toolbar.more-right::after {
  display: block;
}

.toolbar-left,
.toolbar-right {
  display: flex;
  align-items: center;
  gap: 8px;
  flex: 0 0 auto;
}

.toolbar-center {
  display: flex;
  align-items: center;
  gap: 8px;
  flex: 1 1 auto;
  justify-content: center;
  min-width: 0;
}

/* Dropdown menu (native <details>) */

.menu {
  position: relative;
}

.menu > summary {
  list-style: none;
}

.menu > summary::-webkit-details-marker {
  display: none;
}

.menu[open] > summary {
  background: #3d4046;
}

.menu-panel {
  position: fixed;
  z-index: 30;
  min-width: 220px;
  padding: 4px;
  border-radius: 8px;
  background: var(--panel-2);
  border: 1px solid var(--border-strong);
  box-shadow: var(--shadow);
}

.menu-item {
  display: flex;
  align-items: center;
  gap: 8px;
  width: 100%;
  padding: 7px 8px;
  border: 0;
  border-radius: 6px;
  background: transparent;
  color: var(--text);
  text-align: left;
  cursor: pointer;
}

.menu-item:hover,
.menu-item:focus-visible {
  background: rgba(255, 255, 255, 0.07);
}

.menu-check {
  width: 14px;
  color: var(--accent);
  visibility: hidden;
}

.menu-item.checked .menu-check {
  visibility: visible;
}

.menu-sep {
  height: 1px;
  margin: 4px 6px;
  background: var(--border);
}

.menu-field {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 8px;
  padding: 6px 8px;
  color: var(--muted);
}

/* A published page's way back to its site (`cadpole publish --home-url`). */
.home-link {
  display: inline-flex;
  align-items: center;
  flex-direction: row-reverse;
  gap: 2px;
  flex: 0 0 auto;
  height: 30px;
  padding: 0 9px 0 4px;
  border: 1px solid var(--border-strong);
  border-radius: 15px;
  background: var(--panel-2);
  color: var(--muted);
  text-decoration: none;
}

.home-link:hover,
.home-link:focus-visible {
  color: var(--text);
  border-color: var(--accent);
}

.home-link .ico {
  width: 16px;
  height: 16px;
}

.doc-name {
  font-weight: 600;
  max-width: 220px;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

/* ------------------------------------------------------------- viewport */

.viewport {
  position: relative;
  min-width: 0;
  min-height: 0;
  overflow: hidden;
  /* A drag on the model is a camera move, never a text selection, and a long
     press is not a callout. Selection stays on for everything that *is* text:
     messages, the script, diagnostics. */
  user-select: none;
  -webkit-user-select: none;
  -webkit-touch-callout: none;
  background: radial-gradient(120% 90% at 50% 35%, #3a3c42 0%, #2a2b2f 55%, #202124 100%);
}

.viewport-canvas {
  display: block;
  width: 100%;
  height: 100%;
  touch-action: none;
  outline: none;
}

/* The graphics device was lost (viewer.ts): said in the middle of the
   viewport, which is otherwise empty until the device is back. */
.viewport-notice {
  position: absolute;
  top: 50%;
  left: 50%;
  transform: translate(-50%, -50%);
  max-width: min(86%, 420px);
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 10px;
  padding: 14px 18px;
  border-radius: var(--radius);
  background: var(--panel);
  border: 1px solid var(--border-strong);
  box-shadow: var(--shadow);
  color: var(--text);
  text-align: center;
}

.viewport-notice p {
  margin: 0;
}

.triad {
  position: absolute;
  left: 10px;
  bottom: 10px;
  width: 92px;
  height: 92px;
  pointer-events: none;
}

.triad circle {
  pointer-events: auto;
}

.hud {
  position: absolute;
  top: 10px;
  right: 10px;
  padding: 6px 8px;
  border-radius: 6px;
  background: rgba(20, 21, 25, 0.8);
  color: #cfd2d8;
  font: 11px/1.5 var(--mono);
  pointer-events: none;
}

/* The plates view's tabs and filament legend, over the canvas. */

.plates-overlay {
  position: absolute;
  left: 10px;
  top: 10px;
  right: 10px;
  display: flex;
  flex-direction: column;
  align-items: flex-start;
  gap: 8px;
  pointer-events: none;
}

.plate-tabs {
  display: flex;
  flex-wrap: wrap;
  gap: 4px;
  padding: 4px;
  border-radius: 8px;
  background: rgba(20, 21, 25, 0.8);
  border: 1px solid var(--border);
  pointer-events: auto;
}

.plate-tab {
  border: 1px solid transparent;
  border-radius: 6px;
  background: transparent;
  color: var(--muted);
  padding: 4px 10px;
  font-size: 12px;
  cursor: pointer;
}

.plate-tab:hover {
  color: var(--text);
  background: rgba(255, 255, 255, 0.06);
}

.plate-tab.active,
.plate-tab[aria-selected="true"] {
  color: var(--text);
  background: rgba(90, 162, 255, 0.18);
  border-color: var(--accent);
}

.plate-legend {
  list-style: none;
  margin: 0;
  padding: 6px 10px;
  display: flex;
  flex-wrap: wrap;
  gap: 4px 14px;
  border-radius: 8px;
  background: rgba(20, 21, 25, 0.8);
  border: 1px solid var(--border);
  color: #cfd2d8;
  font-size: 12px;
  pointer-events: auto;
}

.legend-item {
  display: flex;
  align-items: center;
  gap: 6px;
}

.legend-swatch {
  width: 12px;
  height: 12px;
  border-radius: 3px;
  border: 1px solid rgba(255, 255, 255, 0.25);
}

.vlabel.platelabel {
  border-color: rgba(223, 228, 238, 0.55);
  color: #e6e9ef;
  margin: -14px 0 0 0;
  transform-origin: 0 0;
}

.vlabel.platelabel::after {
  display: none;
}

.banner {
  position: absolute;
  top: 10px;
  left: 50%;
  transform: translateX(-50%);
  max-width: min(92%, 720px);
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 6px 8px 6px 12px;
  border-radius: 8px;
  background: rgba(230, 176, 74, 0.16);
  border: 1px solid rgba(230, 176, 74, 0.5);
  color: #f3d79a;
  z-index: 5;
}

.vlabels {
  position: absolute;
  inset: 0;
  pointer-events: none;
  overflow: hidden;
}

.vlabel {
  position: absolute;
  left: 0;
  top: 0;
  padding: 2px 8px;
  border-radius: 999px;
  background: rgba(20, 21, 25, 0.86);
  border: 1px solid var(--accent);
  color: #dbe9ff;
  font-size: 11px;
  white-space: nowrap;
  margin: -30px 0 0 -4px;
  will-change: transform;
}

.vlabel::after {
  content: "";
  position: absolute;
  left: 4px;
  bottom: -7px;
  width: 6px;
  height: 6px;
  border-radius: 50%;
  background: var(--accent);
}

.picker {
  position: absolute;
  z-index: 6;
  min-width: 200px;
  padding: 4px;
  border-radius: 8px;
  background: var(--panel-2);
  border: 1px solid var(--border-strong);
  box-shadow: var(--shadow);
}

.picker-item {
  display: grid;
  grid-template-columns: 18px auto 1fr auto;
  align-items: center;
  gap: 8px;
  width: 100%;
  padding: 7px 8px;
  border: 0;
  border-radius: 6px;
  background: transparent;
  color: var(--text);
  text-align: left;
  cursor: pointer;
}

.picker-item:hover,
.picker-item:focus-visible {
  background: rgba(255, 255, 255, 0.07);
}

.picker-item.checked {
  background: var(--accent-bg);
}

.picker-glyph {
  color: var(--accent);
}

.picker-kind {
  color: var(--muted);
  text-transform: uppercase;
  font-size: 10px;
  letter-spacing: 0.06em;
}

.picker-detail {
  font-size: 11px;
}

/* ----------------------------------------------------------------- tree */

.tree {
  display: flex;
  flex-direction: column;
  gap: 1px;
}

.tree-row {
  display: flex;
  align-items: center;
  gap: 6px;
  min-height: 30px;
  padding: 3px 6px;
  border-radius: 6px;
  border-left: 2px solid transparent;
  cursor: pointer;
  user-select: none;
}

.tree-row.depth-1 {
  padding-left: 26px;
}

.tree-row:hover,
.tree-row.hovered {
  background: rgba(255, 255, 255, 0.05);
}

.tree-row.selected {
  background: var(--accent-bg);
  border-left-color: var(--accent);
}

.tree-row.suppressed .tree-label {
  color: var(--muted);
  text-decoration: line-through;
}

.tree-row.is-hidden .tree-label {
  color: var(--muted);
}

.tree-glyph {
  display: inline-grid;
  place-items: center;
  min-width: 24px;
  height: 18px;
  padding: 0 4px;
  border-radius: 4px;
  background: var(--panel-3);
  font: 600 10px/1 var(--mono);
  color: var(--muted);
}

.tree-row.selected .tree-glyph {
  color: var(--accent);
}

.tree-label {
  flex: 1 1 auto;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.tree-detail {
  color: var(--muted);
  font-size: 11px;
}

.tree-badge {
  min-width: 18px;
  padding: 0 5px;
  border-radius: 999px;
  text-align: center;
  font-size: 11px;
  font-weight: 600;
  background: rgba(239, 107, 98, 0.2);
  color: var(--err);
}

.tree-badge.warn {
  background: rgba(230, 176, 74, 0.2);
  color: var(--warn);
}

.twisty,
.twisty-space {
  width: 16px;
  height: 20px;
  display: inline-grid;
  place-items: center;
  border: 0;
  background: none;
  color: var(--muted);
  cursor: pointer;
  padding: 0;
}

.eye {
  min-width: 26px;
  min-height: 26px;
}

.eye.is-hidden {
  color: var(--muted);
}

/* --------------------------------------------------------------- params */

.params {
  display: flex;
  flex-direction: column;
  gap: 8px;
}

.param-row {
  padding: 6px 8px;
  border-radius: 6px;
  background: var(--panel-2);
}

.param-row.overridden {
  box-shadow: inset 2px 0 0 var(--accent);
}

.param-head {
  display: flex;
  align-items: center;
  gap: 6px;
}

.param-name {
  font-weight: 600;
  flex: 1 1 auto;
}

.param-unit {
  color: var(--muted);
  font-size: 11px;
}

.param-controls {
  display: flex;
  align-items: center;
  gap: 8px;
  margin-top: 4px;
}

.param-range {
  flex: 1 1 auto;
  min-width: 0;
  accent-color: var(--accent);
}

.param-vec-controls input {
  width: 30%;
}

.param-desc {
  margin-top: 4px;
  color: var(--muted);
  font-size: 12px;
}

.param-check {
  width: 18px;
  height: 18px;
  accent-color: var(--accent);
}

.param-head .param-name {
  min-width: 0;
  overflow-wrap: anywhere;
}

/* A slider owns the touches that start on it: moving a thumb must
   never scroll the list or the sheet under it. A mouse is unaffected. */
.param-range {
  touch-action: none;
}

/* Choices, when there are few and short: a segmented control. The radios
   are the segments -- each sits invisibly over its label, so a tap on a
   segment is a click on its radio, and the group's arrow keys are the
   browser's own. */
.segmented {
  display: flex;
  width: 100%;
  border: 1px solid var(--border-strong);
  border-radius: 6px;
  background: var(--bg);
  overflow: hidden;
}

.segment {
  position: relative;
  flex: 1 1 0;
  min-width: 0;
}

.segment + .segment {
  border-left: 1px solid var(--border-strong);
}

.segment-input {
  position: absolute;
  inset: 0;
  width: 100%;
  height: 100%;
  margin: 0;
  opacity: 0;
  cursor: pointer;
}

.segment-label {
  display: flex;
  align-items: center;
  justify-content: center;
  min-height: 30px;
  padding: 4px 8px;
  color: var(--muted);
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.segment:hover .segment-label {
  color: var(--text);
}

.segment-input:checked + .segment-label {
  color: var(--text);
  background: var(--accent-bg);
  box-shadow: inset 0 -2px 0 var(--accent);
}

.segment-input:focus-visible + .segment-label {
  outline: 2px solid var(--focus);
  outline-offset: -2px;
}

/* Choices, when there are many: the platform's own picker. */
.param-select {
  width: 100%;
}

/* A bool is a switch, and its label is the whole row. */
.param-switch-row {
  flex: 1 1 auto;
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 10px;
  min-width: 0;
  cursor: pointer;
}

.param-switch-text {
  display: flex;
  flex-direction: column;
  min-width: 0;
}

.param-switch-text .param-desc {
  margin-top: 0;
}

.param-switch {
  appearance: none;
  -webkit-appearance: none;
  position: relative;
  flex: 0 0 auto;
  width: 44px;
  height: 26px;
  margin: 0;
  border-radius: 13px;
  background: var(--border-strong);
  cursor: pointer;
  transition: background 0.15s ease;
}

.param-switch::before {
  content: "";
  position: absolute;
  top: 3px;
  left: 3px;
  width: 20px;
  height: 20px;
  border-radius: 50%;
  background: #fff;
  transition: transform 0.15s ease;
}

.param-switch:checked {
  background: var(--accent);
}

.param-switch:checked::before {
  transform: translateX(18px);
}

.param-switch:focus-visible {
  outline: 2px solid var(--focus);
  outline-offset: 2px;
}

/* A number on a coarse pointer: the slider full width below, and the value
   in a stepper on the name's line -- minus, the value (a field: tap it to
   type), the unit, plus. */
.stepper {
  display: inline-flex;
  align-items: stretch;
  flex: 0 0 auto;
}

.stepper-btn {
  display: inline-grid;
  place-items: center;
  min-width: 44px;
  min-height: 44px;
  padding: 0;
  border: 1px solid var(--border-strong);
  background: var(--panel-3);
  color: var(--text);
  font-size: 18px;
  line-height: 1;
  cursor: pointer;
}

.stepper-btn:first-child {
  border-radius: 6px 0 0 6px;
}

.stepper-btn:last-child {
  border-radius: 0 6px 6px 0;
  border-left: 0;
}

.stepper-btn:active {
  background: var(--accent-bg);
}

.stepper-value {
  display: inline-flex;
  align-items: center;
  gap: 3px;
  min-height: 44px;
  padding: 0 4px;
  border-top: 1px solid var(--border-strong);
  border-bottom: 1px solid var(--border-strong);
  border-left: 0;
  background: var(--bg);
}

.stepper-value .param-number {
  width: 60px;
  min-height: 0;
  padding: 0;
  border: 0;
  border-radius: 0;
  background: transparent;
  text-align: center;
  font-weight: 600;
}

.stepper-value .param-number::-webkit-outer-spin-button,
.stepper-value .param-number::-webkit-inner-spin-button {
  -webkit-appearance: none;
  margin: 0;
}

.stepper-value .param-number:focus-visible {
  outline: 2px solid var(--focus);
  outline-offset: -2px;
}

.param-row.coarse .param-head {
  gap: 8px;
}

/* A coarse number row's name, with its length readout under it:
   the stepper keeps the line's width and the name takes what is left. */
.param-title {
  flex: 1 1 auto;
  display: flex;
  flex-direction: column;
  min-width: 0;
}

.param-row.coarse .param-controls {
  margin-top: 2px;
}

.param-row.coarse .param-range {
  flex: 1 1 100%;
  height: 44px;
}

/* ------------------------------------------------------- layers

   One row per display layer: the name (a solo button), the opacity read-out,
   a switch, and a slider under them. Same surface as a parameter row, so the
   two panels read as one; the controls are display-side and never evaluate. */

.layers {
  display: flex;
  flex-direction: column;
  gap: 6px;
}

.layer-row {
  padding: 6px 8px;
  border-radius: 6px;
  background: var(--panel-2);
}

.layer-row.overridden {
  box-shadow: inset 2px 0 0 var(--accent);
}

.layer-row.is-off .layer-name,
.layer-row.is-off .layer-pct {
  color: var(--muted);
}

.layer-head {
  display: flex;
  align-items: center;
  gap: 8px;
  min-height: 28px;
}

.layer-swatch {
  flex: 0 0 auto;
  width: 10px;
  height: 10px;
  border-radius: 3px;
}

.layer-name {
  flex: 1 1 auto;
  min-width: 0;
  padding: 4px 2px;
  border: 0;
  border-radius: 4px;
  background: none;
  color: var(--text);
  font: inherit;
  font-weight: 600;
  text-align: left;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
  cursor: pointer;
}

.layer-name:hover {
  background: rgba(255, 255, 255, 0.06);
}

.layer-name[aria-pressed="true"] {
  color: var(--accent);
}

.layer-pct {
  min-width: 34px;
  color: var(--muted);
  font-size: 11px;
  font-variant-numeric: tabular-nums;
  text-align: right;
}

/* The switch is the parameters' `.param-switch`; its label is the
   tap target, so a thumb need not land on the 26 px track. */
.layer-switch-hit {
  flex: 0 0 auto;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  cursor: pointer;
}

.layer-range {
  display: block;
  width: 100%;
  margin: 4px 0 0;
  accent-color: var(--accent);
}

.tree-layer {
  margin-left: auto;
  max-width: 40%;
  padding: 0 6px;
  border-radius: 999px;
  background: rgba(255, 255, 255, 0.08);
  color: var(--muted);
  font-size: 10px;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

@media (pointer: coarse) {
  /* A thumb's worth for every layer control: the name, the switch and the
     slider each clear 44 px, the switch's label in both dimensions. */
  .layer-name {
    min-height: 44px;
  }

  .layer-switch-hit {
    min-width: 44px;
    min-height: 44px;
  }

  .layer-range {
    height: 44px;
  }
}

/* ------------------------------------------------------------ inspector */

.kvs {
  display: grid;
  grid-template-columns: auto 1fr;
  gap: 4px 10px;
  margin: 0;
}

.kv {
  display: contents;
}

.kv dt {
  color: var(--muted);
  white-space: nowrap;
}

.kv dd {
  margin: 0;
  display: flex;
  flex-wrap: wrap;
  gap: 4px 8px;
  word-break: break-word;
}

.args {
  border-collapse: collapse;
  width: 100%;
}

.args th {
  text-align: left;
  font-weight: 500;
  color: var(--muted);
  padding: 2px 8px 2px 0;
  vertical-align: top;
  white-space: nowrap;
}

.args td {
  padding: 2px 0;
  font-family: var(--mono);
  font-size: 12px;
}

.issue-list {
  margin: 0;
  padding-left: 16px;
}

.note-wrap {
  margin-top: 10px;
}

.field-label {
  display: flex;
  flex-direction: column;
  gap: 4px;
  color: var(--muted);
  font-size: 12px;
}

.note-input {
  width: 100%;
  resize: vertical;
}

/* ------------------------------------------------------------- messages */

.messages {
  display: flex;
  flex-direction: column;
  flex: 1 1 auto;
  min-height: 0;
}

.msg-list {
  flex: 1 1 auto;
  min-height: 80px;
  overflow: auto;
  display: flex;
  flex-direction: column;
  gap: 8px;
  padding: 2px 0;
}

.msg {
  max-width: 92%;
  padding: 6px 10px;
  border-radius: 10px;
  background: var(--panel-2);
}

.msg.from-user {
  align-self: flex-end;
  background: #2f4e7a;
}

.msg.from-assistant {
  align-self: flex-start;
}

.msg-meta {
  display: flex;
  gap: 8px;
  font-size: 11px;
  color: var(--muted);
}

.msg-from {
  text-transform: capitalize;
}

.msg-text {
  white-space: pre-wrap;
  word-break: break-word;
}

/* A reply that is still being written: what the assistant is doing, in the
   meta line, and a caret after the text. Both are inline, so neither
   appearing nor going away changes the height of the message. */
.msg-activity {
  margin-left: auto;
  padding-left: 8px;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.msg-activity:empty {
  display: none;
}

.msg.streaming .msg-text {
  min-height: 1.4em;
}

.msg.streaming .msg-text::after {
  content: "";
  display: inline-block;
  width: 0.45em;
  height: 0.95em;
  margin-left: 2px;
  vertical-align: -0.12em;
  border-radius: 1px;
  background: var(--muted);
  animation: msg-caret 1.1s steps(2, jump-none) infinite;
}

@keyframes msg-caret {
  from {
    opacity: 0.75;
  }
  to {
    opacity: 0.15;
  }
}

@media (prefers-reduced-motion: reduce) {
  .msg.streaming .msg-text::after {
    animation: none;
    opacity: 0.5;
  }
}

.msg.failed .msg-from::after {
  content: " \00b7 stopped";
  color: var(--warn);
  text-transform: none;
}

.msg-composer {
  display: flex;
  gap: 6px;
  align-items: flex-end;
  margin-top: 8px;
}

.msg-input {
  flex: 1 1 auto;
  resize: none;
  min-height: 34px;
  max-height: 140px;
}

/* Keep the composer on screen when the list above it is long: it is the one
   control in this panel that must never scroll away. */
.msg-composer {
  position: sticky;
  bottom: 0;
  background: var(--panel);
  padding-bottom: 2px;
}

@media (pointer: coarse) {
  /* 16px is not a taste decision. Safari on iOS zooms the whole page when a
     text field smaller than that takes focus, which shifts the layout under
     the thumb and hides what you just typed. The body font is 13px, and
     `textarea { font: inherit }` was handing that straight to the composer. */
  .msg-input {
    font-size: 16px;
    min-height: 44px;
    padding: 10px 12px;
  }

  .msg-composer .btn {
    min-height: 44px;
    min-width: 44px;
  }

  /* Room for a thumb, and for the keyboard's own accessory bar. */
  .msg-list {
    padding-bottom: 4px;
    scroll-padding-bottom: 12px;
  }
}

/* ---------------------------------------------------------- diagnostics */

.diagnostics {
  background: var(--panel-2);
  border-top: 1px solid var(--border);
  padding-bottom: var(--sab);
}

.diag-bar {
  display: flex;
  align-items: center;
  gap: 10px;
  min-height: 32px;
  padding: 3px 10px;
}

.diag-summary {
  display: flex;
  gap: 6px;
}

.status {
  margin-left: auto;
  display: flex;
  gap: 14px;
  color: var(--muted);
  font-size: 12px;
  white-space: nowrap;
  overflow: hidden;
}

.status-item.conn[data-state="live"] {
  color: var(--ok);
}

.status-item.conn[data-state="reconnecting"],
.status-item.conn[data-state="connecting"] {
  color: var(--warn);
}

.status-item.conn[data-state="offline"] {
  color: var(--err);
}

.diag-list {
  list-style: none;
  margin: 0;
  padding: 2px 10px 8px;
  max-height: 150px;
  overflow: auto;
  border-top: 1px solid var(--border);
}

.diag {
  display: flex;
  gap: 8px;
  align-items: baseline;
  padding: 3px 0;
}

.diag-sev {
  width: 14px;
  text-align: center;
}

.diag-loc {
  font-family: var(--mono);
  font-size: 12px;
}

.diag-msg {
  color: var(--text);
}

/* --------------------------------------------------------------- script */

.script-section {
  display: flex;
  flex-direction: column;
  flex: 1 1 auto;
  min-height: 0;
}

.script-tools {
  display: flex;
  gap: 4px;
}

.file-tabs {
  display: flex;
  gap: 4px;
  margin-bottom: 6px;
}

.file-tab {
  border: 1px solid var(--border);
  background: transparent;
  color: var(--muted);
  border-radius: 6px;
  padding: 3px 8px;
  cursor: pointer;
}

.file-tab.active {
  color: var(--text);
  border-color: var(--accent);
}

.script {
  flex: 1 1 auto;
  min-height: 0;
  margin: 0;
  overflow: auto;
  font: 12px/1.6 var(--mono);
  background: var(--bg);
  border: 1px solid var(--border);
  border-radius: 6px;
  tab-size: 4;
}

.script .line {
  display: grid;
  grid-template-columns: 3.2em 1fr;
  border-left: 3px solid transparent;
}

.script .ln {
  text-align: right;
  padding-right: 10px;
  color: #6b6e76;
  user-select: none;
}

.script .code {
  white-space: pre;
  padding-right: 12px;
}

.script .line.hl {
  background: var(--accent-bg);
  border-left-color: var(--accent);
}

.script .line.has-error {
  border-left-color: var(--err);
  background: rgba(239, 107, 98, 0.1);
}

.script .line.has-warning {
  border-left-color: var(--warn);
}

.script .line.flash {
  animation: flash 0.9s ease-out;
}

@keyframes flash {
  0% {
    background: rgba(90, 162, 255, 0.45);
  }
  100% {
    background: var(--accent-bg);
  }
}

.script-editor {
  flex: 1 1 auto;
  min-height: 240px;
  width: 100%;
  resize: none;
  font: 12px/1.6 var(--mono);
  tab-size: 4;
  white-space: pre;
}

/* ------------------------------------------------------------- examples */

.examples {
  display: flex;
  flex-direction: column;
  gap: 8px;
}

.example-card {
  padding: 8px 10px;
  border-radius: 8px;
  background: var(--panel-2);
  border: 1px solid transparent;
}

.example-card.active {
  border-color: var(--accent);
}

.example-head {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 8px;
}

.example-card p {
  margin: 4px 0 8px;
}

/* ------------------------------------------------------------------ api */

.api-body h3.api-group {
  margin: 12px 0 6px;
  font-size: 11px;
  text-transform: uppercase;
  letter-spacing: 0.06em;
  color: var(--muted);
}

.api-fn,
.api-type {
  padding: 8px 10px;
  margin-bottom: 8px;
  border-radius: 8px;
  background: var(--panel-2);
}

.api-fn h4,
.api-type h4 {
  margin: 0 0 4px;
  font-size: 12px;
  color: var(--accent);
  word-break: break-word;
}

.api-type h5 {
  margin: 8px 0 4px;
  font-size: 11px;
  color: var(--muted);
}

.api-fn p,
.api-type p {
  margin: 0 0 6px;
}

.api-params td,
.api-params th {
  font-size: 12px;
  vertical-align: top;
}

.req {
  color: var(--err);
}

/* ----------------------------------------------------- narrow / mobile */

.tabbar {
  grid-area: tabbar;
  justify-content: space-around;
  align-items: stretch;
  min-height: calc(var(--tabbar-h) + var(--sab));
  padding-bottom: var(--sab);
  background: var(--panel-2);
  border-top: 1px solid var(--border);
}

.tabbar-btn {
  position: relative;
  flex: 1 1 0;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 2px;
  /* Seven tabs share a 360 px bar in the full app: a tab gives up width
     before the bar grows wider than the screen. */
  min-width: 0;
  padding: 0;
  min-height: 48px;
  border: 0;
  background: transparent;
  color: var(--muted);
  cursor: pointer;
}

.tabbar-btn[aria-pressed="true"] {
  color: var(--accent);
}

.tabbar-glyph {
  display: grid;
  place-items: center;
  width: min(48px, 100%);
  height: 26px;
  border-radius: 13px;
}

.tabbar-btn[aria-pressed="true"] .tabbar-glyph {
  background: var(--accent-bg);
}

.tabbar-label {
  max-width: 100%;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
  font-size: 11px;
}

.tab-badge {
  position: absolute;
  top: 6px;
  right: calc(50% - 22px);
  min-width: 16px;
  padding: 0 4px;
  border-radius: 999px;
  background: var(--warn);
  color: #1a1a1a;
  font-size: 10px;
  font-weight: 700;
  line-height: 16px;
  text-align: center;
}

.tab-badge.err {
  background: var(--err);
}

.sheet {
  position: fixed;
  left: var(--sal);
  right: var(--sar);
  bottom: calc(var(--tabbar-h) + var(--sab) + var(--kbd, 0px));
  height: min(var(--sheet-half), var(--sheet-full));
  z-index: 20;
  flex-direction: column;
  background: var(--panel);
  border-top: 1px solid var(--border-strong);
  border-radius: 14px 14px 0 0;
  box-shadow: 0 -8px 28px rgba(0, 0, 0, 0.4);
  transition: height 0.22s cubic-bezier(0.2, 0.8, 0.2, 1);
}

.sheet.peek {
  height: min(var(--sheet-peek), var(--sheet-full));
}

.sheet.full {
  height: var(--sheet-full);
}

/* Under the finger the sheet is where the finger is, not easing toward it. */
.sheet.dragging {
  transition: none;
}

/* While the keyboard is up the sheet is the only thing worth looking at, so
   it takes what room there is rather than staying at its resting height. The
   chat is the exception: what is typed there is about the model, and the
   answer is a change to it, so the model keeps the room above. `:where`
   keeps this rule no stronger than the one a phone on its side overrides it
   with. */
.sheet.keyboard:where(:not([data-tab="messages"])) {
  height: var(--sheet-full);
  transition: none;
}

.sheet-handle,
.sheet-head,
.sheet-tabs {
  flex: 0 0 auto;
}

/* The drag handle is the whole strip above the title, not just the bar it
   draws: a 42 x 5 px bar alone would be the hardest thing on the screen to
   grab. */
.sheet-handle {
  display: flex;
  justify-content: center;
  width: 100%;
  padding: 9px 0 5px;
  touch-action: none;
  cursor: grab;
}

.sheet-handle::before {
  content: "";
  width: 42px;
  height: 5px;
  border-radius: 3px;
  background: var(--border-strong);
}

/* The title row is part of the grip: the sheet drags by it as by the handle. */
.sheet-head {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 0 6px 0 16px;
  touch-action: none;
}

.sheet-title {
  margin: 0;
  font-size: 15px;
}

.sheet-close {
  min-width: 44px;
  min-height: 44px;
}

/* The strip of tabs inside the sheet is how the app reaches the panels the
   tab bar has no room for (Examples, API). A published page has neither, and
   the View sheet is not one of the document's panels, so there the strip
   would only repeat the tab bar under it. */
html[data-mode="published"] .sheet-tabs,
.sheet[data-tab="view"] .sheet-tabs {
  display: none;
}

.sheet-tabs {
  display: flex;
  gap: 2px;
  padding: 0 8px;
  overflow-x: auto;
  border-bottom: 1px solid var(--border);
  scrollbar-width: none;
}

.sheet-body {
  flex: 1 1 auto;
  min-height: 0;
  overflow: auto;
  overscroll-behavior: contain;
  display: flex;
  flex-direction: column;
}

.sheet-body > * {
  flex: 1 1 auto;
}

/* ------------------------------------------- touch layout: shared pieces */

.ico {
  display: block;
}

/* The two view buttons that float over a narrow viewport (ui/layout.ts), and
   the selection card (ui/selcard.ts): neither exists on a wide screen. */
.vp-fab,
.selcard {
  display: none;
}

.fab-btn {
  display: grid;
  place-items: center;
  width: 44px;
  height: 44px;
  padding: 0;
  border-radius: 22px;
  border: 1px solid var(--border-strong);
  background: rgba(38, 39, 43, 0.88);
  color: var(--text);
  cursor: pointer;
  -webkit-backdrop-filter: blur(8px);
  backdrop-filter: blur(8px);
}

.fab-btn[hidden] {
  display: none;
}

.fab-section {
  color: var(--accent);
  border-color: var(--accent);
}

.fab-section[aria-pressed="true"] {
  background: #3d4046;
}

.fab-btn:active,
.toolbar-more:active {
  background: #3d4046;
}

.selcard {
  position: absolute;
  left: 8px;
  right: 8px;
  bottom: 8px;
  z-index: 4;
  flex-direction: column;
  gap: 6px;
  padding: 6px 6px 6px 14px;
  border-radius: 14px;
  border: 1px solid var(--border-strong);
  background: rgba(34, 35, 39, 0.95);
  box-shadow: var(--shadow);
}

.selcard-row {
  display: flex;
  align-items: center;
  gap: 2px;
}

.selcard-main {
  flex: 1 1 auto;
  min-width: 0;
  min-height: 44px;
  display: flex;
  flex-direction: column;
  justify-content: center;
  gap: 2px;
  padding: 2px 0;
  border: 0;
  background: none;
  color: var(--text);
  font: inherit;
  text-align: left;
  cursor: pointer;
}

.selcard-line {
  display: flex;
  align-items: baseline;
  gap: 8px;
  min-width: 0;
}

.selcard-kind {
  flex: 0 0 auto;
  font-size: 11px;
  font-weight: 700;
  letter-spacing: 0.05em;
  text-transform: uppercase;
  color: var(--accent);
}

/* A topological name can run long and has no spaces (`case/top|hole/side#0`),
   and its tail is the part that tells two faces apart: two lines, broken
   anywhere, before it is cut. The whole name is one tap away in Inspect. */
.selcard-name {
  min-width: 0;
  display: -webkit-box;
  -webkit-box-orient: vertical;
  -webkit-line-clamp: 2;
  line-clamp: 2;
  overflow: hidden;
  overflow-wrap: anywhere;
  font-weight: 600;
  text-align: left;
}

.selcard-more {
  flex: 0 0 auto;
  color: var(--muted);
  font-size: 12px;
}

.selcard-facts {
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
  color: var(--muted);
  font-size: 13px;
  font-variant-numeric: tabular-nums;
}

.selcard-btn {
  flex: 0 0 auto;
  min-width: 44px;
  min-height: 44px;
  color: var(--text);
}

.selcard-alts {
  display: flex;
  gap: 6px;
  padding: 0 8px 4px 0;
  overflow-x: auto;
  scrollbar-width: none;
}

.chips-row {
  display: flex;
  flex-wrap: wrap;
  gap: 8px;
}

.chip-btn {
  flex: 0 0 auto;
  min-height: 36px;
  padding: 0 14px;
  border-radius: 999px;
  border: 1px solid var(--border-strong);
  background: var(--panel-2);
  color: var(--text);
  font: inherit;
  white-space: nowrap;
  cursor: pointer;
}

.chip-btn .chip-kind {
  color: var(--muted);
  font-size: 12px;
}

.chip-btn[aria-checked="true"],
.chip-btn[aria-pressed="true"] {
  border-color: var(--accent);
  background: var(--accent-bg);
  color: var(--accent);
}

/* The View sheet (ui/toolbar.ts): the toolbar's own controls, moved here and
   laid out for a thumb. Their icons are text glyphs a phone's font may not
   have, so here each button is its word. */
.tools {
  display: flex;
  flex-direction: column;
  gap: 16px;
  padding: 6px 14px 20px;
}

.tools-title {
  margin: 0 0 8px;
  color: var(--muted);
  font-size: 11px;
  font-weight: 600;
  letter-spacing: 0.06em;
  text-transform: uppercase;
}

.tools-row {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  gap: 8px;
}

.tools-views {
  display: grid;
  grid-template-columns: repeat(4, minmax(0, 1fr));
}

.tools-mode .tools-row {
  margin-top: 8px;
}

.tools .btn-group {
  display: contents;
}

.tools .btn,
.tools .btn-group .btn {
  justify-content: center;
  min-height: 44px;
  margin: 0;
  padding: 0 14px;
  border-radius: 10px;
}

.tools .btn .btn-icon {
  display: none;
}

.tools .btn .btn-icon + .btn-text {
  display: inline;
}

.tools .menu-panel {
  position: static;
  display: grid;
  grid-template-columns: repeat(2, minmax(0, 1fr));
  gap: 0 8px;
  min-width: 0;
  padding: 0;
  border: 0;
  background: none;
  box-shadow: none;
}

.tools .menu-item {
  min-height: 44px;
}

.tools .menu-check {
  display: grid;
  place-items: center;
  width: 22px;
  height: 22px;
  border-radius: 6px;
  border: 1px solid var(--border-strong);
  color: transparent;
  visibility: visible;
}

.tools .menu-item.checked .menu-check {
  border-color: var(--accent);
  background: var(--accent);
  color: #10131a;
}

.tools .menu-sep {
  display: none;
}

.tools .menu-field {
  grid-column: 1 / -1;
  padding: 6px 0 0;
}

@media (pointer: coarse) {
  .chip-btn {
    min-height: 44px;
  }

  /* What a scroll wheel does is not a question for a finger. */
  .tools .menu-field {
    display: none;
  }
}

@media (prefers-reduced-motion: reduce) {
  .sheet,
  .triad,
  .vp-fab {
    transition: none;
  }
}

/* The narrow layout. The second clause is a phone on its side: the largest
   are wider than 900 px that way round. Kept in step with NARROW_QUERY in
   ui/layout.ts. */
@media (max-width: 900px), (pointer: coarse) and (max-height: 500px) {
  .app {
    grid-template-columns: minmax(0, 1fr);
    grid-template-rows: minmax(0, 1fr) auto;
    grid-template-areas:
      "viewport"
      "tabbar";
  }

  .panel-left,
  .panel-right,
  .status-slot {
    display: none;
  }

  .tabbar {
    display: flex;
  }

  .sheet {
    display: flex;
  }

  /* The bar lies over the top of the viewport instead of taking a row from
     it: the document's name, and the one button that opens the View sheet.
     Only those two take a touch; the rest of the strip is the model's. */
  .toolbar-slot {
    grid-area: viewport;
    align-self: start;
    z-index: 5;
    pointer-events: none;
  }

  .toolbar {
    gap: 8px;
    min-height: 0;
    padding: calc(6px + var(--sat)) 8px 18px 14px;
    overflow: visible;
    border-bottom: 0;
    background: linear-gradient(rgba(24, 25, 28, 0.78), rgba(24, 25, 28, 0.42) 60%, rgba(24, 25, 28, 0));
  }

  .toolbar-center {
    display: none;
  }

  .toolbar-left {
    flex: 1 1 auto;
    min-width: 0;
  }

  .toolbar-right {
    pointer-events: auto;
  }

  .home-link {
    height: 44px;
    padding: 0 12px 0 6px;
    border-radius: 22px;
    background: rgba(38, 39, 43, 0.88);
    pointer-events: auto;
  }

  .doc-name {
    max-width: 100%;
    font-size: 15px;
    text-shadow: 0 1px 2px rgba(0, 0, 0, 0.6);
  }

  .toolbar-more {
    width: 44px;
    height: 44px;
    border-radius: 22px;
    border: 1px solid var(--border-strong);
    background: rgba(38, 39, 43, 0.88);
    color: var(--text);
  }

  .toolbar-more[aria-expanded="true"] {
    background: var(--accent-bg);
    border-color: var(--accent);
    color: var(--accent);
  }

  /* What the viewport pins to its top edge starts under the bar. */
  .hud,
  .banner,
  .plates-overlay {
    top: calc(58px + var(--sat));
  }

  /* What it pins to its bottom edge stands clear of the selection card or
     the Section panel when one is there (`--dock-h`, main.ts), and of the
     sheet while it lies over the viewport (`--cover-b`, ui/layout.ts). */
  .triad {
    left: 6px;
    bottom: calc(6px + max(var(--dock-h, 0px), var(--cover-b, 0px)));
    transition: bottom 0.18s ease;
  }

  .vp-fab {
    position: absolute;
    right: 10px;
    bottom: calc(10px + max(var(--dock-h, 0px), var(--cover-b, 0px)));
    z-index: 3;
    display: flex;
    flex-direction: column;
    gap: 8px;
    transition: bottom 0.18s ease;
  }

  .selcard {
    display: flex;
  }

  .viewport.docked-section .selcard {
    display: none;
  }

  .section-panel {
    left: 8px;
    right: 8px;
    bottom: 8px;
    width: auto;
  }

  /* A long press lists what is under the finger; the list sits where the
     thumb is, not under the finger and half off the screen. */
  .picker {
    left: 8px !important;
    right: 8px;
    top: auto !important;
    bottom: calc(8px + var(--dock-h, 0px));
    max-height: 60%;
    overflow-y: auto;
  }

  .diagnostics {
    padding-bottom: 0;
  }

  .diag-list {
    max-height: none;
  }
}

/* A tablet upright, or a wide phone: the sheet is a card over the left of the
   viewport, not a slab across all of it, and the tabs do not spread to the
   corners. */
@media (min-width: 640px) and (max-width: 900px) and (min-height: 501px) {
  .sheet {
    left: calc(12px + var(--sal));
    right: auto;
    width: min(440px, calc(100vw - 24px));
    border: 1px solid var(--border-strong);
    border-bottom: 0;
  }

  .tabbar {
    justify-content: center;
  }

  .tabbar-btn {
    flex: 0 1 104px;
  }

  .selcard,
  .section-panel {
    left: auto;
    width: min(440px, calc(100% - 16px));
  }
}

/* A phone on its side has width to spare and no height: the tab bar is a
   rail down the left edge, and the sheet is a panel beside the model -- the
   viewport gives up the room, so the model is re-centred in what is left
   rather than half covered. No heights to drag between; the handle goes.
   Only where the narrow layout is already in force (the list above): a
   short, wide desktop window is the desktop layout, whose panels this grid
   has no places for. */
@media (orientation: landscape) and (max-height: 500px) and (max-width: 900px), (orientation: landscape) and (max-height: 500px) and (pointer: coarse) {
  .app {
    grid-template-columns: auto minmax(0, 1fr);
    grid-template-rows: minmax(0, 1fr);
    grid-template-areas: "tabbar viewport";
  }

  .app.sheet-open {
    grid-template-columns: auto minmax(0, 1fr) min(360px, 46vw);
    grid-template-areas: "tabbar viewport sheet";
  }

  .tabbar {
    flex-direction: column;
    justify-content: flex-start;
    width: 68px;
    min-height: 0;
    padding-bottom: var(--sab);
    border-top: 0;
    border-right: 1px solid var(--border);
    overflow-y: auto;
    scrollbar-width: none;
  }

  .tabbar-btn {
    flex: 1 0 44px;
    max-height: 60px;
    min-height: 44px;
  }

  .tabbar-glyph {
    height: 22px;
  }

  .tabbar-label {
    font-size: 10px;
  }

  .sheet,
  .sheet.peek,
  .sheet.full,
  .sheet.keyboard {
    position: static;
    grid-area: sheet;
    height: auto;
    min-height: 0;
    padding-bottom: var(--kbd, 0px);
    border-top: 0;
    border-left: 1px solid var(--border-strong);
    border-radius: 0;
    box-shadow: none;
    transition: none;
  }

  .sheet-handle {
    display: none;
  }

  .sheet-head {
    padding-top: calc(2px + var(--sat));
  }

  .selcard,
  .section-panel {
    left: auto;
    right: 64px;
    width: min(400px, calc(100% - 172px));
  }

  /* Beside the card rather than above it: there is no height to stack in. */
  .triad,
  .vp-fab {
    bottom: 6px;
  }

  .vp-fab {
    right: 8px;
  }
}

/* Large tap targets on touch devices. */
@media (pointer: coarse) {
  .btn,
  .icon-btn,
  .tab,
  .file-tab,
  .link-btn,
  .picker-item {
    min-height: 44px;
  }

  /* Both dimensions, not just the height. `preventPageZoom` takes pinch-zoom
   * away from the page on the grounds that "every control meets the 44px
   * touch target", and that has to hold for the icon-only toolbar buttons --
   * Home, Fit, Focus, Snapshot -- too. Icon buttons are square; a `.btn` with a
   * label is already wider than this and is unaffected. */
  .toolbar .btn,
  .icon-btn {
    min-width: 44px;
  }

  .tree-row {
    min-height: 44px;
  }

  .param-range {
    height: 44px;
  }

  input[type="number"],
  input[type="text"],
  .select,
  .segment-label,
  .param-switch-row {
    min-height: 44px;
  }

  /* A row's reset button keeps its place while hidden, so the stepper and
     the switch do not shift when an override appears or goes. The display
     has to be `!important` to beat the global `[hidden]` rule's: without it
     the reset was `display: none` and appeared exactly where the stepper's
     "+" had been, so a second tap on "+" reset the value. */
  .param-head > .reset[hidden] {
    display: inline-grid !important;
    visibility: hidden;
  }
}

/* ------------------------------------------------- labels & attachments */

.vlabels.labels-off .vlabel:not(.dimlabel):not(.platelabel) {
  display: none;
}

.dimlabel {
  border-color: var(--border-strong);
  color: var(--text);
  background: rgba(20, 21, 25, 0.8);
  margin: -12px 0 0 8px;
}

.dimlabel::after {
  display: none;
}

.btn.has-badge {
  position: relative;
}

.btn.has-badge::after {
  content: attr(data-count);
  position: absolute;
  top: -6px;
  right: -6px;
  min-width: 16px;
  padding: 0 4px;
  border-radius: 999px;
  background: var(--accent-strong);
  color: #0d1420;
  font-size: 10px;
  font-weight: 700;
  line-height: 16px;
  text-align: center;
}

.attach-strip {
  display: flex;
  gap: 8px;
  flex-wrap: wrap;
  margin-top: 8px;
}

.attach-thumb {
  position: relative;
  display: flex;
  flex-direction: column;
  gap: 2px;
  width: 96px;
}

.attach-thumb .msg-img img {
  width: 96px;
  height: 60px;
  object-fit: cover;
}

.attach-tag {
  font-size: 10px;
  color: var(--muted);
}

.attach-x {
  position: absolute;
  top: -6px;
  right: -6px;
  min-width: 22px;
  min-height: 22px;
  border-radius: 50%;
  background: var(--panel-3);
  color: var(--text);
}

.msg-attachments {
  display: flex;
  gap: 6px;
  flex-wrap: wrap;
  margin-top: 6px;
}

.msg-img {
  display: inline-block;
  border-radius: 6px;
  overflow: hidden;
  border: 1px solid var(--border-strong);
  line-height: 0;
}

.msg-img img {
  max-width: 220px;
  max-height: 140px;
  display: block;
}


/* ------------------------------------------------- selection and zoom

   The rule: chrome is not selectable, content is. Dragging the model, the
   toolbar or a tab must never start a text selection or raise iOS's
   long-press callout, while anything the user might reasonably want to copy
   -- a message, the script, a diagnostic, a measurement -- stays selectable.
   Applying `user-select: none` to the whole app is the tempting version and
   it quietly breaks copying an error message, which is the one thing someone
   reaches for when they are stuck. */

.toolbar,
.tabbar,
.sheet-handle,
.sheet-head,
.sheet-tabs,
.triad,
.hud,
.param-range,
.stepper-btn,
.segment-label,
.param-switch-row,
.layer-row,
.tree-row {
  user-select: none;
  -webkit-user-select: none;
  -webkit-touch-callout: none;
}

.msg-text,
.msg-meta,
.diag-list,
.script-editor,
.apidoc,
.inspector-value,
code,
pre {
  user-select: text;
  -webkit-user-select: text;
  -webkit-touch-callout: default;
}

/* ---------------------------------------------------------------- engines
 *
 * Which engine ran, and the way out of a slow one. The user has to be
 * able to see that evaluation moved to the server or to the main thread
 * without them touching anything, and to override it -- "why is this slow" is
 * unanswerable otherwise.
 */

.pill-worker {
  color: var(--ok);
}

.pill-remote {
  color: var(--accent, #5aa2ff);
}

/* The engine on the main thread is a degraded mode, not a choice: it means no
 * Worker was available, and every heavy evaluation will freeze the page. */
.pill-wasm {
  color: var(--warn);
}

.engine-select {
  max-width: 13rem;
}

.engine-select option:disabled {
  color: var(--muted);
}

.status-item.eval.busy {
  color: var(--fg);
}

/* A slow pulse rather than a spinner: it is next to running text, and the
 * point is only that this line is not the answer yet. */
.status-item.eval.busy::before {
  content: "";
  display: inline-block;
  width: 7px;
  height: 7px;
  margin-right: 6px;
  border-radius: 50%;
  background: var(--accent, #5aa2ff);
  animation: cadpole-eval-pulse 1.1s ease-in-out infinite;
}

@keyframes cadpole-eval-pulse {
  0%,
  100% {
    opacity: 0.25;
  }
  50% {
    opacity: 1;
  }
}

@media (prefers-reduced-motion: reduce) {
  .status-item.eval.busy::before {
    animation: none;
    opacity: 0.8;
  }
}

.eval-cancel {
  min-height: 20px;
  padding: 0 8px;
  font-size: 11px;
}

.status-item.engine.sev-warning {
  color: var(--warn);
}

/* --------------------------------------------------------------- build bar
 *
 * "A new build is available", said rather than done. Fixed to the
 * viewport rather than placed in the shell: it is about the page, not about
 * the document, and a bar that reflows the layout when it appears would move
 * the thing the user is pointing at -- which is the same complaint as
 * reloading underneath them, in miniature.
 *
 * Three looks, one element: an offer (accent, two buttons), a receipt
 * (`.note`, quiet, no buttons, gone in six seconds) and a chip
 * (`.dismissed`, out of the way in the corner and staying there).
 */
.build-bar {
  position: fixed;
  top: calc(10px + var(--sat));
  left: 50%;
  transform: translateX(-50%);
  max-width: min(92vw, 720px);
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 7px 10px 7px 14px;
  border-radius: var(--radius);
  background: var(--accent-bg);
  border: 1px solid var(--accent-strong);
  color: var(--text);
  box-shadow: var(--shadow);
  font-size: 13px;
  z-index: 40;
}

.build-bar-text {
  flex: 1 1 auto;
  min-width: 0;
}

.build-bar .btn {
  flex: 0 0 auto;
}

/* A receipt for something the user did not have to do anything about. */
.build-bar.note {
  background: var(--panel-2);
  border-color: var(--border-strong);
  color: var(--muted);
  box-shadow: none;
}

/* "Not now": out of the way, and not going away. */
.build-bar.dismissed {
  left: auto;
  right: calc(12px + var(--sar));
  transform: none;
  max-width: 60vw;
  padding: 4px 6px 4px 10px;
  font-size: 12px;
  background: var(--panel-2);
  border-color: var(--warn);
  color: var(--warn);
  box-shadow: none;
}

.build-bar.dismissed .btn {
  padding: 2px 8px;
  font-size: 12px;
}

@media (max-width: 900px), (pointer: coarse) and (max-height: 500px) {
  .build-bar {
    max-width: calc(100vw - 20px);
  }

  .build-bar.dismissed {
    max-width: calc(100vw - 24px);
  }
}

/* A published page (src/baked.ts): one baked document, read-only. The
   panels that only make sense with an engine or a session -- the gallery,
   the API reference, the chat -- are not offered, and the Script tab only
   when the publisher included the sources. The Parameters panel stays, as a
   read-out (it is `inert`). */
html[data-mode="published"] #tab-examples,
html[data-mode="published"] #tab-api,
html[data-mode="published"] [data-tab="examples"],
html[data-mode="published"] [data-tab="api"],
html[data-mode="published"] [data-tab="messages"],
html[data-mode="published"] .right-bottom,
html[data-mode="published"] .note-wrap,
html[data-mode="published"][data-sources="0"] #tab-script,
html[data-mode="published"][data-sources="0"] [data-tab="script"] {
  display: none;
}

.params-panel.read-only {
  opacity: 0.85;
}

/* A published page lists only the parameters the publisher named
   (`cadpole publish --show-params`), as a read-out; with none, no panel. */
html[data-mode="published"][data-params="0"] .params-panel,
html[data-mode="published"][data-params="0"] [data-tab="params"] {
  display: none;
}

.param-readout {
  display: grid;
  grid-template-columns: minmax(0, 1fr) auto;
  gap: 4px 12px;
  margin: 0;
  padding: 4px 12px 10px;
  font-size: 13px;
}

.param-readout dt {
  color: var(--muted);
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.param-readout dd {
  margin: 0;
  font-variant-numeric: tabular-nums;
  text-align: right;
}

.section-note {
  font-size: 12px;
}

.status-item.units {
  color: var(--muted);
}

/* The Section panel: over the viewport, top left, below anything
   the plates view puts there. */
.section-panel {
  position: absolute;
  right: 10px;
  bottom: 10px;
  z-index: 4;
  display: flex;
  flex-direction: column;
  gap: 6px;
  width: min(440px, calc(100% - 20px));
  padding: 8px 10px;
  border-radius: var(--radius);
  background: rgba(20, 21, 25, 0.88);
  border: 1px solid var(--border);
  box-shadow: var(--shadow);
  font-size: 13px;
}

.section-panel[hidden] {
  display: none;
}

.section-head {
  display: flex;
  align-items: center;
  gap: 8px;
}

.section-title {
  font-weight: 600;
}

.section-spec {
  flex: 1;
  min-width: 0;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
  color: var(--muted);
  font-family: var(--mono);
  font-size: 12px;
}

.section-row {
  display: flex;
  align-items: center;
  gap: 8px;
}

.section-row.off .section-range,
.section-row.off .section-value {
  opacity: 0.5;
}

.section-label {
  display: flex;
  align-items: center;
  gap: 6px;
  min-width: 92px;
}

.section-axis {
  width: 52px;
}

.section-range {
  flex: 1;
  min-width: 60px;
  accent-color: var(--accent);
}

.section-value {
  width: 96px;
  padding: 3px 6px;
  border-radius: 6px;
  border: 1px solid var(--border);
  background: var(--panel-2);
  color: var(--text);
  font: inherit;
  font-variant-numeric: tabular-nums;
}

.section-value[aria-invalid="true"] {
  border-color: var(--err);
}

.section-flip[aria-pressed="true"] {
  background: var(--accent-bg);
  color: var(--accent);
}

.section-btn.cutting {
  color: var(--accent);
}

/* The toolbar is full at a laptop width: the Section button keeps its icon
   (and its title) and gives up its word first. */
@media (max-width: 1360px) {
  .section-btn .btn-text {
    display: none;
  }
}

/* On a phone a cut's row does not fit on one line: the slider takes a line
   of its own under the switch, the value and Flip, at a width a thumb can
   aim along. */
@media (max-width: 900px), (pointer: coarse) and (max-height: 500px) {
  .section-row {
    flex-wrap: wrap;
    row-gap: 2px;
  }

  .section-label {
    flex: 1 1 auto;
  }

  .section-range {
    order: 5;
    flex: 1 0 100%;
    min-height: 32px;
  }
}

/* On its side there is width for a row and no height for two; the panel
   stays clear of the Home and Fit buttons. */
@media (orientation: landscape) and (max-height: 500px) and (max-width: 900px), (orientation: landscape) and (max-height: 500px) and (pointer: coarse) {
  .section-panel {
    left: auto;
    right: 64px;
    bottom: 6px;
    width: min(430px, calc(100% - 172px));
  }

  .section-row {
    flex-wrap: nowrap;
  }

  .section-label {
    flex: 0 0 auto;
  }

  .section-range {
    order: 0;
    flex: 1 1 60px;
  }
}

/* On a phone the selection card names what is selected, in full and where
   it cannot run off the screen; the tag on the model would say it twice. */
@media (max-width: 900px), (pointer: coarse) and (max-height: 500px) {
  .vlabel:not(.dimlabel):not(.platelabel) {
    display: none;
  }

  /* Two candidates in view at least, however long the first one's name. */
  .selcard-alts .chip-btn {
    max-width: 62%;
    overflow: hidden;
    text-overflow: ellipsis;
  }
}
