/* Particle Studio: a dark instrument panel beside a full-height preview.
   The interface is monochrome on purpose so the sculpture is the only colour on screen.
   Radius rule: controls and tiles 6px, sheets and the dialog 12px. */
:root {
  color-scheme: dark;
  --bg: #0b0b0c;          /* preview ground (the sculpture's default background) */
  --surface: #111113;     /* panel */
  --raised: #18181b;      /* control fill */
  --hover: #202024;
  --line: #242428;        /* hairline between groups (decorative) */
  --tile: #2e2e33;        /* resting edge of text-labelled tiles (decorative) */
  --edge: #6c6b72;        /* control boundaries, 3:1 or better on --surface */
  --text: #eeede8;
  --muted: #a8a7a0;
  --danger: #ff9486;
  --on-light: #0b0b0c;
  --focus: #eeede8;

  --sans: ui-sans-serif, system-ui, -apple-system, "Segoe UI Variable Text", "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif;
  --mono: ui-monospace, "SF Mono", "Cascadia Mono", "Roboto Mono", Menlo, Consolas, monospace;

  --fs-cap: 12px;
  --fs-body: 13px;
  --fs-head: 14px;
  --fs-title: 15px;
  --fs-dialog: 20px;

  --r-ctl: 6px;
  --r-sheet: 12px;
  --gutter: 20px;
  --panel-w: 368px;
  --ease: cubic-bezier(0.2, 0.7, 0.2, 1);
}

* { box-sizing: border-box; }
html, body { margin: 0; height: 100%; }
body {
  background: var(--bg);
  color: var(--text);
  font: var(--fs-body)/1.5 var(--sans);
  -webkit-font-smoothing: antialiased;
  text-rendering: optimizeLegibility;
}

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

/* ---------- Layout ---------- */
.studio {
  display: grid;
  grid-template-columns: minmax(0, 1fr) var(--panel-w);
  height: 100vh;
  height: 100dvh;
}
@media (min-width: 1600px) { :root { --panel-w: 392px; --gutter: 24px; } }

.stage { position: relative; min-width: 0; min-height: 0; background: var(--bg); overflow: hidden; }
#preview { width: 100%; height: 100%; display: block; }

.status {
  position: absolute;
  left: 16px; bottom: 16px;
  max-width: calc(100% - 32px);
  margin: 0;
  padding: 5px 10px;
  border-radius: var(--r-ctl);
  background: rgb(11 11 12 / 0.78);
  color: var(--text);
  font: 500 var(--fs-cap)/1.4 var(--sans);
  font-variant-numeric: tabular-nums;
  white-space: nowrap; overflow: hidden; text-overflow: ellipsis;
  pointer-events: none;
}
.status:empty { display: none; }

/* Transition controls over the preview: beside it on wide screens, along its bottom edge on phones. */
.transport {
  position: absolute; right: 16px; top: 50%; transform: translateY(-50%);
  display: flex; flex-direction: column; align-items: stretch; gap: 6px;
  width: 132px; padding: 6px;
  background: rgb(11 11 12 / 0.78); border: 1px solid var(--tile); border-radius: calc(var(--r-ctl) + 6px);
}
.transport[hidden] { display: none; }
.transport button {
  display: inline-flex; align-items: center; justify-content: center; gap: 6px;
  min-width: 0; min-height: 36px; padding: 0 10px;
  background: var(--raised); color: var(--text);
  border: 1px solid var(--tile); border-radius: var(--r-ctl);
  font: 500 var(--fs-body)/1 var(--sans); cursor: pointer;
}
.transport button:hover { background: var(--hover); }
.transport button:active { transform: scale(0.97); }
.transport-play { background: var(--text) !important; color: var(--on-light) !important; border-color: var(--text) !important; }
.transport-play svg { width: 16px; height: 16px; }
.transport-play .icon-pause, .transport-play[data-playing] .icon-play { display: none; }
.transport-play[data-playing] .icon-pause { display: block; }
.transport-shape span { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; text-transform: capitalize; }
.transport-shape[aria-pressed="true"] { border-color: var(--text); }
.transport-shape[aria-pressed="false"] { color: var(--muted); }
.studio:has(#subject-a:disabled) .transport { display: none; }

/* Shown only when the studio could not start a preview (it disables the subject pickers). */
.stage-fallback {
  display: none;
  position: absolute; inset: 0;
  margin: auto;
  width: min(36ch, calc(100% - 48px)); height: max-content;
  color: var(--muted);
  font-size: var(--fs-head);
  text-align: center;
}
.studio:has(#subject-a:disabled) .stage-fallback { display: block; }

/* ---------- Panel ---------- */
.panel {
  display: grid;
  grid-template-rows: auto minmax(0, 1fr) auto;
  min-height: 0;
  background: var(--surface);
  border-left: 1px solid var(--line);
}
.panel-head {
  display: flex; align-items: center; gap: 10px;
  padding: 18px var(--gutter) 16px;
  border-bottom: 1px solid var(--line);
}
.mark { width: 20px; height: 20px; flex: none; color: var(--text); }
.panel-head h1 { margin: 0; font-size: var(--fs-title); font-weight: 600; letter-spacing: -0.01em; }
.head-link { margin-left: auto; padding: 4px 8px; color: var(--muted); font-size: var(--fs-body); text-underline-offset: 3px; }
.head-link:hover { color: var(--text); }
.sheet-toggle { display: none; }

.panel-body {
  overflow-y: auto;
  overscroll-behavior: contain;
  padding: 0 var(--gutter);
  scrollbar-width: thin;
  scrollbar-color: #3a3a40 transparent;
}
.panel-foot {
  padding: 14px var(--gutter) 16px;
  border-top: 1px solid var(--line);
  background: var(--surface);
}

.group { padding: 20px 0 22px; }
.group + .group { border-top: 1px solid var(--line); }
.group h2 {
  margin: 0 0 12px;
  font-size: var(--fs-head);
  font-weight: 600;
  letter-spacing: -0.005em;
}

.hint { margin: 10px 0 0; color: var(--muted); font-size: var(--fs-cap); line-height: 1.45; }
.error { margin: 10px 0 0; color: var(--danger); font-size: var(--fs-body); font-weight: 500; }
.error:empty { display: none; }
.error:not(:empty) {
  padding: 8px 10px 8px 12px;
  border-left: 2px solid var(--danger);
  background: rgb(255 148 134 / 0.08);
  border-radius: 0 var(--r-ctl) var(--r-ctl) 0;
}

/* ---------- Buttons ---------- */
button {
  min-height: 32px;
  padding: 6px 12px;
  border: 1px solid var(--edge);
  border-radius: var(--r-ctl);
  background: var(--raised);
  color: var(--text);
  font: inherit;
  font-weight: 500;
  cursor: pointer;
  transition: background-color 120ms var(--ease), border-color 120ms var(--ease), transform 120ms var(--ease);
}
button:hover { background: var(--hover); }
button:active { transform: translateY(1px); }
button:disabled { opacity: 0.45; cursor: not-allowed; transform: none; }
button.primary {
  display: inline-flex; align-items: center; justify-content: center; gap: 8px;
  background: var(--text); border-color: var(--text); color: var(--on-light); font-weight: 600;
}
button.primary:hover { background: #fbfaf7; }
button.primary svg { width: 16px; height: 16px; }
button.ghost { background: transparent; border-color: transparent; color: var(--muted); }
button.ghost:hover { color: var(--text); background: var(--raised); }
#export { width: 100%; min-height: 44px; font-size: var(--fs-head); }

/* Toggle tiles share one selected state: bright edge, lifted fill, small corner tick. */
.pairs button, .glyphs button, .palettes button { position: relative; text-align: left; border-color: var(--tile); }
.pairs button:hover, .glyphs button:hover, .palettes button:hover { border-color: var(--edge); }
.pairs button[aria-pressed="true"], .glyphs button[aria-pressed="true"] {
  border-color: var(--text);
  background: #232327;
}
.pairs button[aria-pressed="true"]::after, .glyphs button[aria-pressed="true"]::after {
  content: "";
  position: absolute; top: 5px; right: 5px;
  width: 5px; height: 5px; border-radius: 1px;
  background: var(--text);
}

/* ---------- Subject ---------- */
.pairs { display: grid; grid-template-columns: repeat(2, minmax(0, 1fr)); gap: 6px; }
.pairs button { display: grid; gap: 1px; min-height: 52px; padding: 8px 14px 8px 10px; }
.pair-route { font-size: var(--fs-body); font-weight: 500; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.pair-meaning { color: var(--muted); font-size: var(--fs-cap); font-weight: 400; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }

.credits { margin: 12px 0 0; color: var(--muted); font-size: var(--fs-cap); line-height: 1.5; }
.credits:empty { display: none; }
.credits::before { content: "Model credits"; display: block; margin-bottom: 2px; color: var(--text); font-weight: 500; }
.credits p { margin: 0; }
.credits a { color: var(--text); text-decoration: underline; text-decoration-color: var(--edge); text-underline-offset: 2px; }
.credits a:hover { text-decoration-color: currentColor; }

.slots { display: grid; grid-template-columns: repeat(2, minmax(0, 1fr)); gap: 12px; margin-top: 18px; }
.slot { display: grid; gap: 10px; align-content: start; min-width: 0; }

/* ---------- Fields ---------- */
.field { display: grid; gap: 6px; margin: 0; min-width: 0; }
.field + .field { margin-top: 14px; }
.slot .field + .field { margin-top: 0; }
.field-label { color: var(--muted); font-size: var(--fs-cap); font-weight: 500; }
.field-head { display: flex; align-items: baseline; justify-content: space-between; gap: 8px; }
.readout { color: var(--text); font: 500 var(--fs-cap)/1 var(--sans); font-variant-numeric: tabular-nums; }

select {
  appearance: none;
  width: 100%; min-height: 34px;
  padding: 6px 30px 6px 10px;
  border: 1px solid var(--edge);
  border-radius: var(--r-ctl);
  background: var(--raised) url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 16 16'%3E%3Cpath d='M4.5 6.5 8 10l3.5-3.5' fill='none' stroke='%23eeede8' stroke-width='1.5' stroke-linecap='round' stroke-linejoin='round'/%3E%3C/svg%3E") no-repeat right 8px center / 16px;
  color: var(--text);
  font: inherit;
  cursor: pointer;
}
select:hover { background-color: var(--hover); }
select:disabled { opacity: 0.45; cursor: not-allowed; }
option { background: var(--raised); color: var(--text); }

/* The file input stays a real file input; its button is stretched to fill it. */
.file input[type="file"] {
  width: 100%;
  min-height: 34px;
  border-radius: var(--r-ctl);
  color: transparent;
  font: inherit;
  cursor: pointer;
  overflow: hidden;
}
.file input[type="file"]::file-selector-button {
  width: 100%; min-height: 34px;
  margin: 0;
  padding: 6px 10px;
  border: 1px dashed var(--edge);
  border-radius: var(--r-ctl);
  background: transparent;
  color: var(--text);
  font: inherit; font-weight: 500;
  cursor: pointer;
  transition: background-color 120ms var(--ease);
}
.file input[type="file"]:hover::file-selector-button { background: var(--raised); }
.file input[type="file"]:disabled { opacity: 0.45; cursor: not-allowed; }

/* ---------- Glyph ---------- */
.glyphs { display: grid; grid-template-columns: repeat(5, minmax(0, 1fr)); gap: 6px; }
.glyphs button { display: grid; place-items: center; min-height: 48px; padding: 0; color: var(--muted); }
.glyphs button:hover, .glyphs button[aria-pressed="true"] { color: var(--text); }
.glyph-icon { width: 22px; height: 22px; }
.glyph-icon .o { fill: none; stroke: currentColor; stroke-width: 1.6; stroke-linejoin: round; }
.glyph-icon .f { fill: currentColor; }
.glyph-icon .s { fill: none; stroke: currentColor; stroke-width: 2.6; }

/* ---------- Colour ---------- */
.palettes { display: grid; grid-template-columns: repeat(4, minmax(0, 1fr)); gap: 6px; }
.palettes button { display: grid; gap: 7px; padding: 8px 8px 7px; font-size: var(--fs-cap); }
.strip { display: grid; grid-template-columns: repeat(4, 1fr); height: 8px; border-radius: 2px; overflow: hidden; }

.swatches { display: grid; grid-template-columns: repeat(4, minmax(0, 1fr)); gap: 6px; margin-top: 14px; }
.swatch { display: grid; gap: 5px; color: var(--muted); font-size: var(--fs-cap); }

input[type="color"] {
  appearance: none;
  width: 100%; height: 32px;
  padding: 3px;
  border: 1px solid var(--edge);
  border-radius: var(--r-ctl);
  background: var(--raised);
  cursor: pointer;
}
input[type="color"]::-webkit-color-swatch-wrapper { padding: 0; }
input[type="color"]::-webkit-color-swatch { border: 0; border-radius: 3px; }
input[type="color"]::-moz-color-swatch { border: 0; border-radius: 3px; }

.split { display: flex; flex-wrap: wrap; align-items: center; justify-content: space-between; gap: 12px 16px; margin-top: 16px; }
.field.inline { display: flex; align-items: center; gap: 10px; margin: 0; }
.colour-field input[type="color"] { width: 44px; height: 30px; }
.colour-field span, .check span { color: var(--text); }

/* Checkbox: square, monochrome. */
input[type="checkbox"] {
  appearance: none;
  flex: none;
  width: 20px; height: 20px;
  margin: 2px;
  border: 1px solid var(--edge);
  border-radius: 4px;
  background: var(--raised);
  display: grid; place-content: center;
  cursor: pointer;
}
input[type="checkbox"]::before {
  content: "";
  width: 10px; height: 10px;
  clip-path: polygon(14% 52%, 0 66%, 38% 100%, 100% 22%, 86% 8%, 38% 72%);
  background: var(--on-light);
  transform: scale(0);
}
input[type="checkbox"]:checked { background: var(--text); border-color: var(--text); }
input[type="checkbox"]:checked::before { transform: scale(1); }
input[type="checkbox"]:disabled { opacity: 0.4; cursor: not-allowed; }
.check:has(input:disabled) span { color: var(--muted); }
.check { cursor: pointer; min-height: 24px; }
.range + .check { margin-top: 12px; }

/* ---------- Range ---------- */
.range { gap: 4px; }
input[type="range"] {
  --fill: 0%;
  appearance: none;
  width: 100%; height: 24px;
  margin: 0;
  background: transparent;
  cursor: pointer;
}
input[type="range"]::-webkit-slider-runnable-track {
  height: 2px; border-radius: 1px;
  background: linear-gradient(to right, var(--text) var(--fill), var(--edge) var(--fill));
}
input[type="range"]::-webkit-slider-thumb {
  appearance: none;
  width: 16px; height: 16px; margin-top: -7px;
  border: 3px solid var(--surface);
  border-radius: 50%;
  background: var(--text);
  box-shadow: 0 0 0 1px var(--text);
}
input[type="range"]::-moz-range-track { height: 2px; border-radius: 1px; background: var(--edge); }
input[type="range"]::-moz-range-progress { height: 2px; background: var(--text); }
input[type="range"]::-moz-range-thumb {
  width: 10px; height: 10px;
  border: 3px solid var(--surface);
  border-radius: 50%;
  background: var(--text);
  box-shadow: 0 0 0 1px var(--text);
}

/* ---------- Segmented radios (the radios stay real, focusable inputs) ---------- */
.segmented { margin: 0 0 16px; padding: 0; border: 0; min-width: 0; }
.segmented legend { padding: 0; margin-bottom: 6px; }
.segments {
  display: grid; grid-template-columns: 1fr 1fr;
  padding: 3px; gap: 3px;
  border: 1px solid var(--edge);
  border-radius: var(--r-ctl);
  background: var(--raised);
}
.segments label {
  position: relative;
  display: grid; place-items: center;
  min-height: 30px; padding: 4px 8px;
  border-radius: 4px;
  color: var(--muted);
  font-weight: 500;
  cursor: pointer;
  transition: background-color 120ms var(--ease), color 120ms var(--ease);
}
.segments input { position: absolute; inset: 0; width: 100%; height: 100%; margin: 0; opacity: 0; cursor: pointer; }
.segments label:hover { color: var(--text); }
.segments label:has(input:checked) { background: var(--text); color: var(--on-light); }
.segments label:has(input:focus-visible) { outline: 2px solid var(--focus); outline-offset: 2px; }

/* ---------- Focus ---------- */
:focus-visible { outline: 2px solid var(--focus); outline-offset: 2px; }
button.primary:focus-visible { outline-offset: 3px; }

/* ---------- Export dialog ---------- */
dialog {
  width: min(600px, calc(100vw - 32px));
  max-height: calc(100dvh - 32px);
  padding: 28px;
  border: 1px solid var(--tile);
  border-radius: var(--r-sheet);
  background: var(--surface);
  color: var(--text);
  box-shadow: 0 24px 80px rgb(0 0 0 / 0.55);
}
dialog::backdrop { background: rgb(6 6 7 / 0.72); }
dialog h2 { margin: 0 0 8px; font-size: var(--fs-dialog); font-weight: 600; letter-spacing: -0.015em; }
.dialog-lead { margin: 0 0 14px; color: var(--muted); font-size: var(--fs-body); max-width: 60ch; }
pre {
  margin: 0;
  padding: 14px 16px;
  overflow-x: auto;
  border: 1px solid var(--tile);
  border-radius: var(--r-ctl);
  background: var(--bg);
  color: var(--text);
  font: var(--fs-cap)/1.65 var(--mono);
}
code { font-family: var(--mono); font-size: 0.95em; color: var(--text); }
dialog .hint { margin-top: 14px; }
dialog .error:not(:empty) { margin-top: 14px; }

/* The attribution notice: required reading, so full-contrast text on a marked panel. */
.notice {
  margin-top: 16px;
  padding: 12px 14px;
  border: 1px solid var(--edge);
  border-left: 3px solid var(--text);
  border-radius: var(--r-ctl);
  background: var(--raised);
  font-size: var(--fs-body);
}
.notice p { margin: 0 0 6px; font-weight: 600; }
.notice ul { margin: 0; padding-left: 18px; }
.notice li { margin: 2px 0; overflow-wrap: anywhere; }

.dialog-actions { display: flex; flex-wrap: wrap; gap: 8px; margin-top: 22px; }
.dialog-actions button { min-height: 38px; padding-inline: 14px; }
#close-export { margin-left: auto; }

/* ---------- Narrow screens: the panel becomes a bottom sheet ----------
   (A narrow but short landscape window, such as a laptop at 200% zoom, keeps a slim side panel
   instead; see the next block.) */
@media (max-width: 767px) and (orientation: portrait), (max-width: 767px) and (min-height: 541px) {
  :root { --gutter: 16px; }
  .studio {
    grid-template-columns: minmax(0, 1fr);
    grid-template-rows: minmax(0, 1fr) var(--sheet-h, 46dvh);
  }
  .studio[data-sheet="open"] { --sheet-h: 78dvh; }
  .status { left: 12px; top: 12px; bottom: auto; max-width: calc(100% - 24px); }
  .transport {
    right: auto; top: auto; left: 50%; bottom: 10px; transform: translateX(-50%);
    flex-direction: row; width: max-content; max-width: calc(100% - 24px);
  }
  .transport button { min-height: 44px; }
  .transport-play { flex: none; width: 48px; }
  .transport-shape { flex: 0 1 auto; max-width: 34vw; }

  .panel {
    border-left: 0;
    border-top: 1px solid var(--tile);
    border-radius: var(--r-sheet) var(--r-sheet) 0 0;
    box-shadow: 0 -12px 40px rgb(0 0 0 / 0.4);
  }
  .panel-head { position: relative; padding: 14px var(--gutter) 10px; }
  .panel-head::before {
    content: "";
    position: absolute; top: 6px; left: 50%;
    width: 36px; height: 4px; margin-left: -18px;
    border-radius: 2px; background: #3a3a40;
  }
  .sheet-toggle {
    display: inline-flex; align-items: center; gap: 6px;
    margin-left: 0;
    min-height: 44px; padding: 6px 12px;
    background: transparent;
    color: var(--text);
  }
  .sheet-toggle svg { width: 16px; height: 16px; transition: transform 160ms var(--ease); }
  .sheet-toggle[aria-expanded="true"] svg { transform: rotate(180deg); }

  .panel-foot { padding: 10px var(--gutter) calc(12px + env(safe-area-inset-bottom)); }

  /* Touch targets: 44px. */
  button, select, .file input[type="file"], .file input[type="file"]::file-selector-button { min-height: 44px; }
  input[type="color"] { height: 44px; }
  .colour-field input[type="color"] { width: 56px; height: 44px; }
  input[type="range"] { height: 44px; }
  .segments label { min-height: 40px; }
  .check { min-height: 44px; }
  input[type="checkbox"] { width: 22px; height: 22px; }
  .pairs button { min-height: 56px; }
  .glyphs button { min-height: 52px; }

  /* Pairs become a two-row strip that scrolls sideways, so the credits and subject pickers
     stay inside the peek height. The third column peeks in to show there is more. */
  .pairs {
    grid-template-columns: none;
    grid-template-rows: repeat(2, auto);
    grid-auto-flow: column;
    grid-auto-columns: calc((100% - 12px) / 2.25);
    overflow-x: auto;
    scroll-snap-type: x proximity;
    scroll-padding-inline: 2px;
    margin-inline: -2px; padding: 2px 2px 6px;
    scrollbar-width: none;
  }
  .pairs::-webkit-scrollbar { display: none; }
  .pairs button { scroll-snap-align: start; }

  pre { white-space: pre-wrap; overflow-wrap: anywhere; }

  dialog { padding: 22px 18px; }
  .dialog-actions button { flex: 1 1 auto; min-height: 44px; }
  #close-export { margin-left: 0; }
}

@media (max-width: 767px) and (orientation: landscape) and (max-height: 540px) {
  :root { --panel-w: 300px; --gutter: 16px; }
  .pairs { grid-template-columns: minmax(0, 1fr); }
  .panel-head { padding-block: 12px 10px; }
  .group { padding: 16px 0 18px; }
  .panel-foot { padding-block: 10px 12px; }
  pre { white-space: pre-wrap; overflow-wrap: anywhere; }
}

@media (max-width: 359px) {
  .slots { grid-template-columns: minmax(0, 1fr); }
  .glyphs { grid-template-columns: repeat(4, minmax(0, 1fr)); }
  .palettes { grid-template-columns: repeat(2, minmax(0, 1fr)); }
}

@media (prefers-reduced-motion: reduce) {
  *, *::before, *::after { transition-duration: 0.01ms !important; animation-duration: 0.01ms !important; }
  button:active { transform: none; }
}

@media (forced-colors: active) {
  .pairs button[aria-pressed="true"], .glyphs button[aria-pressed="true"] { outline: 2px solid Highlight; }
  .segments label:has(input:checked) { outline: 2px solid Highlight; }
}
