*, *::before, *::after {
  box-sizing: border-box;
}

/* Component-level shades derived from tokens.css; amber text needs a deeper tone on the 5500K table to stay legible. */
:root {
  --edge-ink: #7f5f26;
  --amber-wash: rgba(207, 169, 104, 0.14);
  --token-bg: rgba(22, 22, 20, 0.06);
  --popover-bg: #fbfaf7;
  --popover-shadow: 0 0 0 1px rgba(22, 22, 20, 0.05), 0 18px 44px rgba(16, 16, 14, 0.16);
  /* The renderer's diagnostic error red (#e05a4f), deepened on the 5500K table for 4.5:1 text. */
  --fail-ink: #b3372d;
  --fail-wash: rgba(224, 90, 79, 0.12);
}

html[data-table="dark"] {
  --edge-ink: var(--film-edge);
  --amber-wash: rgba(207, 169, 104, 0.16);
  --token-bg: rgba(230, 226, 216, 0.07);
  --popover-bg: #161614;
  --popover-shadow: 0 0 0 1px rgba(207, 169, 104, 0.12), 0 22px 52px rgba(0, 0, 0, 0.6);
  --fail-ink: #e05a4f;
  --fail-wash: rgba(224, 90, 79, 0.16);
}

html {
  background: var(--lt-bg);
  min-height: 100%;
  overflow-x: clip;
  scroll-behavior: smooth;
}

@media (prefers-reduced-motion: reduce) {
  html {
    scroll-behavior: auto;
  }
}

body {
  margin: 0;
  min-height: 100vh;
  min-height: 100dvh;
  background: var(--lt-glow);
  color: var(--lt-ink);
  font-family: var(--font-sans);
  font-size: var(--text-body);
  line-height: 1.6;
  -webkit-font-smoothing: antialiased;
  overflow-x: clip;
  transition: background-color 220ms ease, color 220ms ease;
}

a {
  color: inherit;
  text-decoration: none;
}

img {
  display: block;
  max-width: 100%;
}

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

h1,
h2,
h3 {
  margin: 0;
  font: inherit;
}

fieldset {
  margin: 0;
  padding: 0;
  border: 0;
  min-width: 0;
}

legend {
  padding: 0;
}

.mono {
  font-family: var(--font-mono);
  font-variant-numeric: tabular-nums;
}

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

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

[hidden] {
  display: none !important;
}

/* --- Full-bleed page shell --- */
.studio-page {
  width: 100%;
  min-height: 100vh;
  min-height: 100dvh;
  display: flex;
  flex-direction: column;
  background: var(--lt-glow);
}

.light-slit {
  width: 100%;
  height: 5px;
  background: var(--hole-bg);
  flex-shrink: 0;
  transition: background-color 220ms ease;
}

/* --- Sticky full-bleed film top bar (monouma contract) --- */
.top-bar {
  width: 100%;
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 16px;
  padding: 12px 18px;
  background: var(--film-base);
  border-bottom: 1px solid rgba(207, 169, 104, 0.18);
  color: #f2efe8;
  position: sticky;
  top: 0;
  z-index: 40;
  flex-shrink: 0;
}

.site-brand {
  display: inline-flex;
  align-items: center;
  gap: 10px;
  min-width: 0;
  flex-shrink: 0;
}

.site-id {
  display: inline-flex;
  align-items: center;
  line-height: 0;
  color: #f5f3ed;
  white-space: nowrap;
  flex-shrink: 0;
}

.site-wordmark {
  display: block;
  height: 28px;
  width: auto;
}

/* Twin Provia optical pinstripe echoing the P2 canister band. */
.brand-stripes {
  display: inline-block;
  width: 18px;
  height: 4px;
  border-radius: 1px;
  flex-shrink: 0;
  background: linear-gradient(
    to bottom,
    var(--koma-green) 0 50%,
    var(--koma-cyan) 50% 100%
  );
}

.brand-formats {
  font-size: var(--text-rebate);
  letter-spacing: 0.08em;
  color: rgba(242, 239, 232, 0.46);
  white-space: nowrap;
  user-select: none;
}

.top-nav {
  display: flex;
  align-items: center;
  gap: 18px;
  font-size: var(--text-ui);
  min-width: 0;
}

.top-nav a {
  color: rgba(242, 239, 232, 0.62);
  padding: 4px 0;
  position: relative;
  white-space: nowrap;
  transition: color 150ms ease;
}

.top-nav a:hover,
.top-nav a[aria-current="page"] {
  color: #f6f4ee;
}

.top-nav a[aria-current="page"]::after {
  content: "";
  position: absolute;
  left: 0;
  right: 0;
  bottom: 0;
  height: 1.5px;
  background: var(--film-edge);
}

.top-bar .lamp-btn {
  border: 1px solid rgba(207, 169, 104, 0.36);
  background: transparent;
  padding: 3px 9px;
  font-size: var(--text-ui);
  border-radius: var(--radius);
  color: var(--film-edge);
  cursor: pointer;
  white-space: nowrap;
  transition: border-color 150ms ease, background-color 150ms ease;
}

.top-bar .lamp-btn:hover {
  border-color: var(--film-edge);
  background: rgba(207, 169, 104, 0.1);
}

/* --- Workbench: control rail left, viewing bench right --- */
.studio-main {
  flex: 0 0 auto;
  width: 100%;
  display: grid;
  grid-template-columns: minmax(0, 1fr);
  scroll-margin-top: 60px;
}

.rail {
  min-width: 0;
  padding: 20px 22px 24px;
  background: var(--lt-surface);
  display: flex;
  flex-direction: column;
  gap: 12px;
  transition: background-color 220ms ease;
}

.rail-head {
  display: flex;
  align-items: baseline;
  justify-content: space-between;
  gap: 12px;
}

.rail-title {
  display: inline-flex;
  align-items: baseline;
  gap: 8px;
  font-size: var(--text-ui);
  font-weight: 600;
  letter-spacing: 0.1em;
  color: var(--lt-ink);
}

.edge-no {
  font-size: var(--text-rebate);
  font-weight: 500;
  letter-spacing: 0.04em;
  color: var(--edge-ink);
}

.rail-counter {
  font-size: var(--text-rebate);
  letter-spacing: 0.06em;
  color: var(--lt-muted);
  white-space: nowrap;
}

.rail-tabs {
  display: flex;
  gap: 4px;
  border-bottom: 1px solid var(--lt-line);
}

.rail-tab {
  min-width: var(--hit);
  min-height: var(--hit);
}

.roll-panel {
  display: flex;
  flex-direction: column;
  gap: 12px;
  min-width: 0;
}

.account-ranges .range-row {
  grid-template-columns: auto minmax(0, 1fr) 52px;
}

.text-btn {
  border: 0;
  background: none;
  padding: 2px 0;
  color: var(--lt-ink);
  font-size: var(--text-ui);
  cursor: pointer;
  white-space: nowrap;
  text-decoration: underline;
  text-decoration-color: transparent;
  text-underline-offset: 4px;
  transition: color 140ms ease, text-decoration-color 140ms ease;
}

.text-btn:hover {
  color: var(--edge-ink);
  text-decoration-color: currentColor;
}

/* --- Token picker: chips live inside the input well --- */
.picker {
  position: relative;
}

.picker-field {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  gap: 7px 5px;
  min-height: 46px;
  padding: 6px 8px;
  background: var(--lt-bg);
  border-radius: var(--radius);
  box-shadow: inset 0 -1.5px 0 var(--lt-line);
  cursor: text;
  transition: box-shadow 150ms ease, background-color 150ms ease;
}

.picker-field:focus-within {
  box-shadow: inset 0 -2px 0 var(--film-edge);
}

.picker-tokens {
  display: contents;
}

.token {
  position: relative;
  display: inline-flex;
  align-items: center;
  gap: 5px;
  max-width: 100%;
  height: 26px;
  padding: 0 2px 0 7px;
  background: var(--token-bg);
  border-radius: var(--radius);
  font-size: var(--text-ui);
  cursor: grab;
  user-select: none;
}

body.is-dragging-token,
body.is-dragging-token * {
  cursor: grabbing !important;
  user-select: none;
}

.token.is-dragging {
  opacity: 0.42;
}

.token.is-drop-before::before,
.token.is-drop-after::after {
  content: "";
  position: absolute;
  top: -2px;
  bottom: -2px;
  width: 2.5px;
  border-radius: 1px;
  background: var(--edge-ink);
  box-shadow: 0 0 0 1px var(--lt-bg);
  pointer-events: none;
}

.token.is-drop-before::before {
  left: -3.75px;
}

.token.is-drop-after::after {
  right: -3.75px;
}

.token-no {
  font-size: var(--text-rebate);
  color: var(--edge-ink);
  flex-shrink: 0;
}

/* Inset ring and tint only, so the chip and its remove target keep their size. */
.token.is-failed {
  background: var(--fail-wash);
  box-shadow: inset 0 0 0 1px var(--fail-ink);
}

.token.is-failed .token-no {
  color: var(--fail-ink);
}

.token-dot {
  width: 6px;
  height: 6px;
  border-radius: 1px;
  flex-shrink: 0;
  background: var(--film-edge);
}

.token-label {
  min-width: 0;
  max-width: 150px;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.token-x {
  width: 20px;
  height: 20px;
  border: 0;
  border-radius: var(--radius);
  background: none;
  color: var(--lt-muted);
  font-size: var(--text-ui);
  line-height: 1;
  cursor: pointer;
  flex-shrink: 0;
}

.token-x:hover {
  color: var(--lt-ink);
  background: var(--amber-wash);
}

.picker-input {
  flex: 1 1 120px;
  min-width: 120px;
  height: 30px;
  border: 0;
  padding: 0 4px;
  background: transparent;
  font-size: var(--text-ui);
  color: var(--lt-ink);
  outline: none;
}

.picker-input::placeholder {
  color: var(--lt-muted);
}

.combobox-popover {
  position: absolute;
  top: calc(100% + 6px);
  left: 0;
  right: 0;
  z-index: 30;
  display: flex;
  flex-direction: column;
  max-height: 320px;
  background: var(--popover-bg);
  border-radius: var(--radius);
  box-shadow: var(--popover-shadow);
  overflow: hidden;
}

.combobox-groups {
  display: flex;
  align-items: center;
  gap: 4px;
  padding: 2px 8px 0;
  border-bottom: 1px solid var(--lt-line);
  flex-shrink: 0;
}

.combobox-group-btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 5px;
  min-width: var(--hit);
  min-height: var(--hit);
}

.combobox-group-btn:focus-visible {
  outline-offset: -2px;
}

.combobox-group-count {
  font-size: var(--text-rebate);
  color: var(--lt-muted);
}

.combobox-list {
  flex: 1 1 auto;
  min-height: 0;
  overflow-y: auto;
  padding: 4px 0;
}

.combobox-option {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  padding: 7px 12px 7px 10px;
  cursor: pointer;
  border-left: 2px solid transparent;
}

.combobox-option.is-active {
  background: var(--amber-wash);
  border-left-color: var(--film-edge);
}

.combobox-option[aria-disabled="true"] {
  opacity: 0.5;
  cursor: not-allowed;
}

.combobox-option-main {
  display: flex;
  align-items: center;
  gap: 10px;
  min-width: 0;
}

.combobox-mark {
  width: 16px;
  height: 16px;
  flex-shrink: 0;
  display: inline-flex;
  align-items: center;
  justify-content: center;
}

.combobox-mark svg {
  width: 15px;
  height: 15px;
  display: block;
}

.combobox-gh-badge {
  font-size: var(--text-rebate);
  letter-spacing: 0.06em;
  padding: 1px 5px;
  color: var(--film-base);
  background: var(--film-edge);
  border-radius: var(--radius);
  flex-shrink: 0;
}

.combobox-option-title {
  font-size: var(--text-ui);
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.combobox-alias {
  font-size: var(--text-rebate);
  color: var(--edge-ink);
  white-space: nowrap;
}

.combobox-option-meta {
  display: flex;
  align-items: center;
  gap: 8px;
  font-size: var(--text-rebate);
  color: var(--lt-muted);
  white-space: nowrap;
  flex-shrink: 0;
}

/* Bare-text meta is a GitHub or title row's add cue (or its limit reason), which is action text; span-wrapped preset metadata stays at the rebate step. */
.combobox-option-meta:not(:has(*)) {
  font-size: var(--text-ui);
}

.combobox-in-roll {
  color: var(--edge-ink);
}

.combobox-empty,
.combobox-footer {
  padding: 8px 12px;
  font-size: var(--text-ui);
  color: var(--lt-muted);
}

.combobox-footer {
  font-size: var(--text-rebate);
}

/* --- Quick rolls as one quiet text line --- */
.roll-line {
  display: flex;
  align-items: baseline;
  flex-wrap: wrap;
  gap: 9px 10px;
  font-size: var(--text-ui);
}

.roll-line-label {
  font-size: var(--text-rebate);
  letter-spacing: 0.06em;
  color: var(--lt-muted);
}

.roll-btn + .roll-btn::before {
  content: "";
  display: inline-block;
  width: 3px;
  height: 3px;
  margin: 0 9px 0 -1px;
  border-radius: 50%;
  vertical-align: middle;
  background: var(--lt-muted);
}

.roll-clear {
  margin-left: auto;
  color: var(--lt-muted);
}

.studio-notice {
  margin: 0;
  font-size: var(--text-ui);
  color: var(--edge-ink);
}

.preview-error {
  display: flex;
  align-items: center;
  flex-wrap: wrap;
  gap: 0 12px;
}

.preview-error-text {
  margin: 0;
  min-width: 0;
  font-size: var(--text-ui);
  color: var(--fail-ink);
  overflow-wrap: anywhere;
}

.preview-retry {
  min-width: var(--hit);
  min-height: var(--hit);
  text-decoration-color: currentColor;
}

/* --- Tuning drawer (native <details>, collapsed by default) --- */
.tune {
  margin-top: 4px;
  border-top: 1px solid var(--lt-line);
}

.tune-summary {
  display: flex;
  align-items: center;
  gap: 12px;
  padding: 14px 0 4px;
  cursor: pointer;
  list-style: none;
  user-select: none;
}

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

.tune-summary::after,
.code-toggle::after {
  content: "";
  width: 7px;
  height: 7px;
  margin-left: 2px;
  border-right: 1.5px solid var(--lt-muted);
  border-bottom: 1.5px solid var(--lt-muted);
  transform: translateY(-2px) rotate(45deg);
  transition: transform 160ms ease;
  flex-shrink: 0;
}

.tune[open] > .tune-summary::after,
.code-toggle[aria-expanded="true"]::after {
  transform: translateY(2px) rotate(-135deg);
}

.tune-digest {
  margin-left: auto;
  min-width: 0;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
  font-size: var(--text-rebate);
  color: var(--lt-muted);
}

.tune-digest.is-tuned {
  color: var(--edge-ink);
}

.tune-body {
  display: flex;
  flex-direction: column;
  gap: 18px;
  padding: 14px 0 4px;
}

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

.tune-label {
  font-size: var(--text-ui);
  letter-spacing: 0.04em;
  color: var(--lt-muted);
  white-space: nowrap;
}

.param-key {
  font-size: var(--text-rebate);
  letter-spacing: 0.02em;
  color: var(--lt-muted);
  font-weight: 400;
}

.seg {
  display: flex;
  flex-wrap: wrap;
  gap: 2px 4px;
}

.seg-item {
  position: relative;
  padding: 3px 8px 5px;
  font-size: var(--text-ui);
  color: var(--lt-muted);
  cursor: pointer;
  border-radius: var(--radius);
  transition: color 120ms ease, background-color 120ms ease;
}

.seg-item:hover {
  color: var(--lt-ink);
}

.seg-item:has(input:checked) {
  color: var(--lt-ink);
  box-shadow: inset 0 -2px 0 var(--film-edge);
}

.seg-item:has(input:focus-visible),
.theme-tile:has(input:focus-visible) {
  outline: 2px solid var(--film-edge);
  outline-offset: 2px;
}

.tune-ranges {
  gap: 6px;
}

.range-row {
  display: grid;
  grid-template-columns: 96px minmax(0, 1fr) 52px;
  align-items: center;
  gap: 10px;
}

.range-row input[type="range"] {
  width: 100%;
  margin: 0;
  accent-color: var(--film-edge);
}

.range-value {
  font-size: var(--text-ui);
  text-align: right;
  color: var(--lt-ink);
}

.theme-grid {
  display: grid;
  grid-template-columns: repeat(3, minmax(0, 1fr));
  gap: 6px;
}

.theme-tile {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 5px;
  padding: 8px 4px 6px;
  border-radius: var(--radius);
  cursor: pointer;
  transition: background-color 120ms ease;
}

.theme-tile:hover {
  background: var(--token-bg);
}

.theme-tile:has(input:checked) {
  background: var(--amber-wash);
}

.theme-thumb {
  width: 41px;
  height: 48px;
}

.theme-thumb--auto {
  display: grid;
  place-items: center;
  font-size: var(--text-rebate);
  letter-spacing: 0.08em;
  color: var(--film-edge);
  background: var(--film-base);
}

.theme-name {
  font-size: var(--text-ui);
  color: var(--lt-muted);
  max-width: 100%;
  overflow: hidden;
  text-overflow: ellipsis;
}

.theme-tile:has(input:checked) .theme-name {
  color: var(--lt-ink);
}

.color-rows {
  display: flex;
  flex-direction: column;
  gap: 4px;
}

.color-row {
  display: grid;
  grid-template-columns: 96px 24px minmax(0, 1fr) auto;
  align-items: center;
  gap: 10px;
  min-height: 30px;
}

.swatch {
  position: relative;
  width: 24px;
  height: 24px;
  cursor: pointer;
}

.swatch input {
  -webkit-appearance: none;
  appearance: none;
  width: 24px;
  height: 24px;
  padding: 0;
  border: 0;
  background: none;
  cursor: pointer;
}

.swatch input::-webkit-color-swatch-wrapper {
  padding: 0;
}

.swatch input::-webkit-color-swatch {
  border: 0;
  border-radius: var(--radius);
}

.swatch input::-moz-color-swatch {
  border: 0;
  border-radius: var(--radius);
}

.color-row:not(.is-set) .swatch::after {
  content: "";
  position: absolute;
  inset: 0;
  border-radius: var(--radius);
  background: repeating-linear-gradient(135deg, var(--lt-bg) 0 3px, var(--token-bg) 3px 6px), var(--lt-bg);
  box-shadow: inset 0 0 0 1px var(--lt-line);
  pointer-events: none;
}

.color-hex {
  font-size: var(--text-ui);
  color: var(--lt-muted);
}

.color-row.is-set .color-hex {
  color: var(--lt-ink);
}

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

.tune-reset {
  align-self: flex-start;
  color: var(--lt-muted);
}

/* --- Viewing bench --- */
.bench {
  min-width: 0;
  display: flex;
  flex-direction: column;
  gap: 14px;
  padding: 16px 16px 22px;
}

.stage-bar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 10px 16px;
  flex-wrap: wrap;
}

.stage-tabs {
  display: inline-flex;
  align-items: center;
  gap: 4px;
}

.stage-tabs .stage-note {
  margin-right: 8px;
}

.stage-tab {
  border: 0;
  background: none;
  color: var(--lt-muted);
  padding: 4px 8px 6px;
  font-size: var(--text-ui);
  cursor: pointer;
  white-space: nowrap;
  border-radius: var(--radius);
  transition: color 140ms ease;
}

.stage-tab:hover {
  color: var(--lt-ink);
}

.stage-tab[aria-pressed="true"],
.rail-tab[aria-selected="true"] {
  color: var(--lt-ink);
  box-shadow: inset 0 -2px 0 var(--film-edge);
}

.stage-right {
  display: flex;
  align-items: center;
  gap: 14px;
  min-width: 0;
}

.backing-toggle,
.code-toggle {
  display: inline-flex;
  align-items: center;
  gap: 7px;
  border: 0;
  background: none;
  color: var(--lt-muted);
  padding: 4px 0;
  font-size: var(--text-ui);
  cursor: pointer;
  white-space: nowrap;
  transition: color 140ms ease;
}

.backing-toggle:hover,
.backing-toggle[aria-pressed="true"],
.code-toggle:hover,
.code-toggle[aria-expanded="true"] {
  color: var(--lt-ink);
}

.backing-indicator {
  width: 8px;
  height: 8px;
  border-radius: 1px;
  box-shadow: inset 0 0 0 1.5px currentColor;
}

.backing-toggle[aria-pressed="true"] .backing-indicator {
  background: var(--film-edge);
  box-shadow: none;
}

.stage-bar button,
.code-toggle {
  min-width: var(--hit);
  min-height: var(--hit);
}

.stage-note {
  font-size: var(--text-rebate);
  color: var(--lt-muted);
  white-space: nowrap;
}

.stage-spec {
  font-size: var(--text-rebate);
  color: var(--lt-muted);
  white-space: nowrap;
}

.stage-surface {
  position: relative;
  min-height: 180px;
  padding: 24px 14px;
  display: grid;
  grid-template-columns: minmax(0, 1fr);
  place-items: center;
  border-radius: var(--radius);
  overflow: hidden;
  transition: background-color 200ms ease;
}

.stage-surface[data-stage="light"] {
  background: var(--stage-light-glow);
  color: #666560;
  box-shadow: 0 0 0 1px rgba(22, 22, 20, 0.05), 0 22px 46px -34px rgba(22, 22, 20, 0.45);
}

.stage-surface[data-stage="github-light"] {
  background: var(--stage-github-light);
  color: #59636e;
  box-shadow: 0 0 0 1px rgba(22, 22, 20, 0.08);
}

.stage-surface[data-stage="dark"] {
  background: var(--stage-dark-glow);
  color: #989389;
}

.stage-surface[data-stage="github-dark"] {
  background: var(--stage-github-dark);
  color: #8b949e;
}

.stage-preview {
  width: auto;
  height: auto;
  max-width: 100%;
  max-height: 100%;
  transition: opacity 150ms ease;
}

.stage-preview.is-loading {
  opacity: 0.55;
}

.stage-empty {
  margin: 0;
  font-size: var(--text-ui);
  text-align: center;
}

/* --- Export bar: fixed above the stage so copy buttons never move with roll length; code well collapsed on demand --- */
.export-bar {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  justify-content: space-between;
  gap: 8px 16px;
}

.code-box {
  background: var(--lt-surface);
  border-radius: var(--radius);
  transition: background-color 220ms ease;
}

.code-kind {
  display: block;
  padding: 8px 14px 0;
  font-size: var(--text-rebate);
  letter-spacing: 0.08em;
  color: var(--lt-muted);
}

.code-text {
  margin: 0;
  padding: 4px 14px 12px;
  max-height: calc(1.6em * 4 + 16px);
  overflow-y: auto;
  font-size: var(--text-ui);
  line-height: 1.6;
  white-space: pre-wrap;
  overflow-wrap: anywhere;
  word-break: normal;
  color: var(--lt-ink);
}

.code-text.is-empty {
  color: var(--lt-muted);
}

.copy-row {
  display: grid;
  grid-template-columns: 1.12fr 0.88fr 1.05fr 1.25fr;
  gap: 6px;
  width: min(100%, 520px);
}

.copy-btn {
  min-height: max(38px, var(--hit));
  padding: 6px 10px;
  border: 0;
  border-radius: var(--radius);
  background: var(--lt-surface);
  color: var(--lt-ink);
  font-size: var(--text-ui);
  cursor: pointer;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
  transition: background-color 140ms ease, color 140ms ease;
}

.copy-btn:hover:not(:disabled) {
  background: var(--amber-wash);
}

.copy-btn[data-copied="true"] {
  color: var(--edge-ink);
}

.copy-btn--primary {
  background: var(--film-base);
  color: #f5f3ed;
  font-weight: 600;
}

.copy-btn--primary:hover:not(:disabled) {
  background: var(--film-base);
  color: var(--film-edge);
}

.copy-btn--primary[data-copied="true"] {
  color: var(--film-edge);
}

html[data-table="dark"] .copy-btn--primary {
  background: var(--film-edge);
  color: #10100e;
}

html[data-table="dark"] .copy-btn--primary:hover:not(:disabled),
html[data-table="dark"] .copy-btn--primary[data-copied="true"] {
  background: #dcbb7f;
  color: #10100e;
}

.copy-btn:disabled {
  opacity: 0.45;
  cursor: not-allowed;
}

/* --- Frame Image Gallery (below the fold, frameless contact-sheet rhythm) --- */
.gallery {
  flex: 1 0 auto;
  width: 100%;
  padding: 28px 16px 36px;
  display: flex;
  flex-direction: column;
  gap: 18px;
  scroll-margin-top: 60px;
}

.gallery[data-empty] {
  padding-block: 14px;
  gap: 14px;
}

.gallery-head,
.api-head {
  display: flex;
  align-items: baseline;
  justify-content: space-between;
  flex-wrap: wrap;
  gap: 8px 16px;
}

.gallery-title-group {
  display: flex;
  align-items: baseline;
  flex-wrap: wrap;
  gap: 6px 14px;
}

.gallery-count {
  font-size: var(--text-rebate);
  letter-spacing: 0.06em;
  color: var(--lt-muted);
}

.gallery-jump {
  color: var(--lt-muted);
}

.gallery-guide-toggle {
  min-height: var(--hit);
  color: var(--edge-ink);
}

.gallery-guide-toggle::after {
  content: " ▾";
}

.gallery-guide-toggle[aria-expanded="true"]::after {
  content: " ▴";
}

.gallery-controls {
  display: flex;
  align-items: center;
  flex-wrap: wrap;
  gap: 8px 20px;
  padding-bottom: 10px;
  border-bottom: 1px solid var(--lt-line);
}

.gallery-filter-group {
  display: inline-flex;
  align-items: center;
  flex-wrap: wrap;
  gap: 2px 4px;
}

.gallery-compare-all {
  margin-left: auto;
}

.gallery-grid {
  display: grid;
  grid-template-columns: minmax(0, 1fr);
  gap: 18px;
}

.specimen {
  min-width: 0;
  background: var(--lt-surface);
  border-radius: var(--radius);
  display: flex;
  flex-direction: column;
  overflow: hidden;
  transition: background-color 200ms ease;
}

.specimen-stage {
  padding: 20px 14px;
  min-height: 192px;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 10px;
  background: var(--lt-glow);
  box-shadow: inset 0 -1px 0 var(--lt-line);
}

.specimen-strip {
  width: 100%;
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 6px;
}

.specimen-strip-tag {
  align-self: flex-start;
  font-size: var(--text-rebate);
  letter-spacing: 0.08em;
  color: var(--lt-muted);
}

.specimen-img {
  width: auto;
  height: auto;
  max-width: 100%;
  max-height: 156px;
}

.specimen-slit {
  width: 100%;
  height: 5px;
  background: var(--hole-bg);
  border-radius: 1px;
}

.specimen-foot {
  padding: 10px 14px 12px;
  display: flex;
  align-items: center;
  justify-content: space-between;
  flex-wrap: wrap;
  gap: 8px 12px;
}

.specimen-meta {
  display: flex;
  align-items: baseline;
  flex-wrap: wrap;
  gap: 4px 8px;
  min-width: 0;
}

.specimen-title {
  font-size: var(--text-ui);
  font-weight: 600;
  color: var(--lt-ink);
}

.specimen-spec {
  font-size: var(--text-rebate);
  color: var(--lt-muted);
  letter-spacing: 0.04em;
}

.specimen-actions {
  display: flex;
  align-items: center;
  gap: 12px;
  margin-left: auto;
}

.specimen-compare {
  color: var(--lt-muted);
}

.specimen-compare[aria-pressed="true"] {
  color: var(--edge-ink);
  text-decoration-color: var(--film-edge);
}

.specimen-insert {
  padding: 5px 11px;
  border: 0;
  border-radius: var(--radius);
  background: var(--token-bg);
  color: var(--lt-ink);
  font-size: var(--text-ui);
  cursor: pointer;
  white-space: nowrap;
  transition: background-color 140ms ease, color 140ms ease;
}

.specimen-insert:hover:not(:disabled) {
  background: var(--amber-wash);
  color: var(--edge-ink);
}

.specimen-insert[aria-pressed="true"] {
  background: var(--amber-wash);
  color: var(--edge-ink);
}

.specimen-insert:disabled {
  opacity: 0.45;
  cursor: not-allowed;
}

.gallery-empty-filter {
  padding: 36px 20px;
  background: var(--lt-surface);
  border-radius: var(--radius);
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 10px;
  text-align: center;
}

.gallery-empty-filter-text {
  margin: 0;
  font-size: var(--text-ui);
  color: var(--lt-muted);
}

.gallery-contribute {
  padding: 14px 16px;
  background: var(--lt-surface);
  border-radius: var(--radius);
  display: flex;
  align-items: baseline;
  justify-content: space-between;
  flex-wrap: wrap;
  gap: 8px 18px;
}

.gallery-contribute-copy {
  display: flex;
  align-items: baseline;
  flex-wrap: wrap;
  gap: 4px 10px;
  min-width: 0;
}

.gallery-contribute-text {
  margin: 0;
  font-size: var(--text-ui);
  color: var(--lt-muted);
}

.gallery-contribute-actions {
  display: flex;
  align-items: center;
  flex-wrap: wrap;
  gap: 6px 14px;
}

.token-format {
  font-size: var(--text-rebate);
  color: var(--lt-muted);
  flex-shrink: 0;
}

.token[aria-current="true"] {
  box-shadow: inset 0 -2px 0 var(--film-edge);
}

/* --- Per-frame panel: inline under the picker so it pushes the rail down instead of covering the roll --- */
.frame-panel {
  display: flex;
  flex-direction: column;
  gap: 10px;
  padding: 10px 12px 12px;
  background: var(--lt-bg);
  border-radius: var(--radius);
  box-shadow: inset 2px 0 0 var(--film-edge);
}

.frame-panel-head {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
}

.frame-panel-title {
  margin: 0;
  min-width: 0;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
  font-size: var(--text-ui);
  font-weight: 600;
  color: var(--lt-ink);
}

.frame-panel .seg-item {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  min-width: var(--hit);
  min-height: var(--hit);
}

.frame-format-hint {
  white-space: normal;
}

.frame-panel .seg-item:has(input:disabled) {
  opacity: 0.4;
  cursor: not-allowed;
}

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

.frame-field-row {
  display: flex;
  align-items: center;
  gap: 12px;
}

.frame-input {
  flex: 1 1 auto;
  width: 100%;
  min-width: 0;
  min-height: var(--hit);
  border: 0;
  border-radius: var(--radius);
  padding: 0 8px;
  background: var(--token-bg);
  box-shadow: inset 0 -1.5px 0 var(--lt-line);
  color: var(--lt-ink);
  font-size: var(--text-ui);
}

.frame-input::placeholder {
  color: var(--lt-muted);
}

.frame-input:focus {
  box-shadow: inset 0 -2px 0 var(--film-edge);
}

.frame-input[aria-invalid="true"] {
  box-shadow: inset 0 0 0 1px var(--fail-ink);
}

.frame-icon-clear {
  min-width: var(--hit);
  min-height: var(--hit);
}

.frame-field-error {
  margin: 0;
  font-size: var(--text-ui);
  color: var(--fail-ink);
  overflow-wrap: anywhere;
}

.frame-panel-moves {
  display: grid;
  grid-template-columns: repeat(2, minmax(0, 1fr));
  gap: 8px;
}

.frame-move {
  min-height: var(--hit);
  border: 0;
  border-radius: var(--radius);
  background: var(--token-bg);
  color: var(--lt-ink);
  font-size: var(--text-ui);
  cursor: pointer;
}

.frame-move:hover:not(:disabled) {
  background: var(--amber-wash);
}

.frame-move:disabled {
  color: var(--lt-muted);
  opacity: 0.5;
  cursor: not-allowed;
}

/* --- Unexposed Film Leader (empty gallery state, monouma darkroom language) --- */
.film-leader {
  display: flex;
  flex-direction: column;
  gap: 14px;
}

.leader-strip {
  background: var(--film-base);
  border-radius: var(--radius);
  overflow: hidden;
  color: #ece9e1;
}

.leader-rebate {
  display: flex;
  flex-direction: column;
  gap: 7px;
}

.leader-rebate--top {
  padding: 10px 16px 8px;
}

.leader-rebate--bottom {
  padding: 8px 16px 10px;
}

.leader-sprockets {
  height: 11px;
  width: 100%;
  background: var(--hole-bg);
  -webkit-mask-image: var(--sprocket-svg);
  mask-image: var(--sprocket-svg);
  -webkit-mask-size: 28px 11px;
  mask-size: 28px 11px;
  -webkit-mask-repeat: repeat-x;
  mask-repeat: repeat-x;
  -webkit-mask-position: center;
  mask-position: center;
  transition: background-color 220ms ease;
}

.leader-marks {
  display: flex;
  align-items: center;
  justify-content: space-between;
  flex-wrap: wrap;
  gap: 4px 12px;
  font-size: var(--text-rebate);
  letter-spacing: 0.1em;
  color: var(--film-edge-dim);
}

.leader-frame {
  margin: 0 16px;
  padding: 24px 22px;
  background: #171614;
  border-top: 1px solid rgba(207, 169, 104, 0.22);
  border-bottom: 1px solid rgba(207, 169, 104, 0.22);
  display: flex;
  flex-direction: column;
  gap: 10px;
}

html[data-table="dark"] .leader-frame {
  background: #121210;
}

.leader-desc {
  margin: 0;
  max-width: 72ch;
  font-size: var(--text-ui);
  line-height: 1.65;
  color: rgba(242, 239, 232, 0.78);
}

.leader-frame code {
  font-family: var(--font-mono);
  padding: 1px 5px;
  border-radius: var(--radius);
  background: rgba(207, 169, 104, 0.14);
  color: #ebd09b;
}

.leader-steps {
  margin: 8px 0 0;
  padding: 14px 0 0;
  list-style: none;
  border-top: 1px solid rgba(242, 239, 232, 0.1);
  display: grid;
  grid-template-columns: minmax(0, 1fr);
  gap: 14px;
}

.leader-step {
  display: flex;
  align-items: baseline;
  gap: 10px;
  min-width: 0;
}

.leader-step .edge-no {
  color: var(--film-edge);
  flex-shrink: 0;
}

.leader-step-copy {
  min-width: 0;
}

.leader-step-title {
  display: block;
  font-size: var(--text-ui);
  font-weight: 600;
  color: #f5f3ed;
  margin-bottom: 3px;
}

.leader-step-body {
  margin: 0;
  font-size: var(--text-ui);
  line-height: 1.55;
  color: rgba(242, 239, 232, 0.68);
}

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

.leader-actions {
  display: grid;
  grid-template-columns: minmax(0, 1fr);
  gap: 6px;
}

.leader-link {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  text-align: center;
}

/* --- In-page API reference --- */
.api-ref {
  width: 100%;
  /* At least one screen tall so both #gallery and #api can scroll up to the sticky bar for the nav scroll-spy. */
  min-height: calc(100vh - 60px);
  min-height: calc(100dvh - 60px);
  padding: 28px 16px 36px;
  display: flex;
  flex-direction: column;
  gap: 14px;
  scroll-margin-top: 60px;
}

.api-intro {
  margin: 0;
  max-width: 72ch;
  font-size: var(--text-ui);
  line-height: 1.65;
  color: var(--lt-muted);
}

.api-ref code {
  font-family: var(--font-mono);
  color: var(--lt-ink);
}

.api-columns {
  display: grid;
  grid-template-columns: minmax(0, 1fr);
  gap: 22px 36px;
}

.api-list {
  margin: 6px 0 0;
}

.api-list > div {
  display: grid;
  grid-template-columns: 5.5em minmax(0, 1fr);
  gap: 2px 16px;
  padding: 9px 0;
  border-top: 1px solid var(--lt-line);
  font-size: var(--text-ui);
  line-height: 1.6;
  overflow-wrap: anywhere;
}

.api-list--routes > div {
  grid-template-columns: minmax(0, 2fr) minmax(0, 3fr);
}

.api-list dd {
  margin: 0;
  color: var(--lt-muted);
}

.api-example {
  display: block;
  padding-block: max(0px, calc((var(--hit) - 1.6em) / 2));
  color: var(--edge-ink);
  text-decoration: underline;
  text-decoration-color: transparent;
  text-underline-offset: 3px;
}

.api-example:hover {
  text-decoration-color: currentColor;
}

/* --- Bottom film footer (monouma contract) --- */
.sheet-footer {
  width: 100%;
  background: var(--film-base);
  border-top: 1px solid rgba(207, 169, 104, 0.18);
  padding: 12px 18px;
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  font-size: var(--text-ui);
  color: var(--film-edge-dim);
  flex-shrink: 0;
}

.sheet-footer-left {
  display: flex;
  align-items: center;
  flex-wrap: wrap;
  gap: 6px;
}

.sheet-footer a {
  color: #f2efe8;
  text-decoration: underline;
  text-decoration-color: rgba(207, 169, 104, 0.45);
  text-underline-offset: 3px;
  transition: color 140ms ease, text-decoration-color 140ms ease;
}

.sheet-footer a:hover {
  color: var(--film-edge);
  text-decoration-color: var(--film-edge);
}

.sheet-footer-sep {
  opacity: 0.45;
}

.sheet-footer-slogan {
  color: rgba(242, 239, 232, 0.62);
  white-space: nowrap;
}

/* --- Hit areas: small targets reach var(--hit) through an invisible pseudo box; spacing keeps neighbours apart --- */
:root {
  --hit: 32px;
}

.site-id,
.top-bar .lamp-btn,
.token-x,
.roll-line .text-btn,
.frame-panel-close,
.api-head .text-btn {
  position: relative;
}

.site-id::before,
.top-nav a::before,
.top-bar .lamp-btn::before,
.token-x::before,
.roll-line .text-btn::after,
.frame-panel-close::before,
.api-head .text-btn::before {
  content: "";
  position: absolute;
  top: 50%;
  left: 50%;
  width: max(100%, var(--hit));
  height: max(100%, var(--hit));
  transform: translate(-50%, -50%);
}

/* The open popover covers the quick rolls, so their hit areas must not reach into the slit above it. */
.picker:has(.combobox-popover:not([hidden])) ~ .roll-line .text-btn::after {
  display: none;
}

/* --- Desktop: two columns, one screen, the rail scrolls on its own --- */
@media (min-width: 720px) {
  .leader-actions {
    grid-template-columns: 1.4fr 1fr 1fr;
  }
}

@media (min-width: 800px) {
  .leader-steps {
    grid-template-columns: repeat(3, minmax(0, 1fr));
    gap: 22px;
  }
}

@media (min-width: 960px) {
  .studio-main {
    grid-template-columns: minmax(320px, 368px) minmax(0, 1fr);
  }

  .rail {
    padding: 22px 24px 28px;
    box-shadow: inset -1px 0 0 var(--lt-line);
  }

  .bench {
    padding: 18px 28px 22px;
  }

  .stage-surface {
    padding: 28px;
  }

  .gallery,
  .api-ref {
    padding: 36px 28px 52px;
    gap: 22px;
  }

  .gallery-grid {
    grid-template-columns: repeat(2, minmax(0, 1fr));
    gap: 22px;
  }
}

@media (min-width: 1200px) {
  .api-columns {
    grid-template-columns: repeat(2, minmax(0, 1fr));
  }
}

@media (min-width: 1360px) {
  .gallery-grid {
    grid-template-columns: repeat(3, minmax(0, 1fr));
  }
}

@media (min-width: 960px) and (min-height: 600px) {
  .studio-main {
    height: calc(100vh - 65px);
    height: calc(100dvh - 65px);
    min-height: 540px;
    grid-template-rows: minmax(0, 1fr);
  }

  .rail {
    overflow-y: auto;
    overscroll-behavior: contain;
    scrollbar-width: thin;
  }

  .stage-surface {
    flex: 1 1 auto;
    min-height: 0;
    grid-template-rows: minmax(0, 1fr);
  }
}

/* --- Mobile (<= 640px) --- */
@media (max-width: 640px) {
  .top-bar {
    padding: 10px 14px;
    gap: 8px;
  }

  .site-brand {
    gap: 7px;
  }

  .site-wordmark {
    height: 22px;
  }

  .brand-stripes {
    width: 12px;
  }

  .brand-formats {
    display: none;
  }

  .top-nav {
    gap: 12px;
  }

  .top-bar .lamp-btn {
    padding: 3px 8px;
  }

  .rail {
    padding: 16px 16px 6px;
    gap: 10px;
  }

  .token-label {
    max-width: 112px;
  }

  .stage-bar {
    gap: 6px 12px;
  }

  .stage-tabs {
    width: 100%;
    flex-wrap: wrap;
    justify-content: space-between;
  }

  .stage-tabs .stage-note {
    flex-basis: 100%;
  }

  .copy-row {
    width: 100%;
    grid-template-columns: repeat(2, minmax(0, 1fr));
  }

  .stage-right {
    width: 100%;
    justify-content: space-between;
  }

  .stage-surface {
    min-height: 150px;
    padding: 20px 10px;
  }

  .gallery,
  .api-ref {
    padding: 22px 14px 30px;
    gap: 14px;
  }

  .api-list--routes > div {
    grid-template-columns: minmax(0, 1fr);
  }

  .gallery-controls {
    gap: 8px 12px;
  }

  .gallery-compare-all {
    margin-left: 0;
  }

  .leader-rebate--top {
    padding: 8px 10px 6px;
  }

  .leader-rebate--bottom {
    padding: 6px 10px 8px;
  }

  .leader-frame {
    margin: 0 10px;
    padding: 18px 14px;
  }

  .sheet-footer {
    flex-direction: column;
    align-items: flex-start;
    gap: 6px;
    padding: 12px 14px;
  }

  .sheet-footer-slogan {
    white-space: normal;
  }
}

/* --- Coarse pointers: 44px hit areas, with wider gaps so neighbouring hit areas never overlap --- */
@media (any-pointer: coarse) {
  :root {
    --hit: 44px;
  }

  .top-nav {
    gap: 24px;
  }

  .picker-field {
    gap: 13px 5px;
    padding: 7px 8px;
  }

  .token {
    height: 32px;
  }

  /* Leaning the remove target into its own chip keeps it clear of the next chip without widening chips. */
  .token-x::before {
    transform: translate(calc(-50% - 6px), -50%);
  }

  .roll-line {
    row-gap: 21px;
  }
}

/* Below 375px five 44px top-bar targets no longer fit beside the site id, so the nav takes a second row. */
@media (any-pointer: coarse) and (max-width: 374px) {
  .top-bar {
    flex-wrap: wrap;
    row-gap: 18px;
  }

  .top-nav {
    flex-basis: 100%;
  }

  .studio-main,
  .gallery,
  .api-ref {
    scroll-margin-top: 106px;
  }
}
