/*
 * Experiment Controls — tool layout for playground experiment pages.
 * Provides the reusable "tool" layout (a site-nav header bar + a large stage/canvas + a scrolling
 * control sidebar on the right) and the layout for the native control panel inside it. The controls
 * themselves are native design-system elements (.slider-wrapper, .form-toggle, <select>,
 * input[type=color], .button, native <details> groups), authored in each experiment's index.html
 * and wired by /assets/js/experiment-controls.js. The header is a self-contained site-nav bar
 * (these pages are outside the studio Barba SPA): the wordmark links home and a "Back to playground"
 * button returns to the feed. Tokens only; design-system.css is always loaded on these pages.
 */

/* ------ LAYOUT ------ */

.experiment {
  display: grid;
  grid-template-columns: 1fr var(--experiment-sidebar-width, 370px);
  grid-template-rows: auto minmax(0, 1fr); /* header bar + content (stage | sidebar) */
  width: 100%;
  height: 100vh;
  height: 100dvh;
}

.experiment-stage {
  position: relative;
  overflow: hidden;
  min-width: 0;
  background: var(--background-primary);
}

.experiment-sidebar {
  display: flex;
  flex-direction: column;
  gap: 0; /* header + controls scroll inside .experiment-pane; the CTA's border/padding separates it */
  min-height: 0; /* fill the content row (grid stretch); the .experiment-pane inside it scrolls */
  overflow: hidden; /* the pane scrolls, not the whole sidebar — keeps the CTA pinned at the bottom */
  background: var(--background-primary);
  border-left: 1px solid var(--border-faded);
}

/* ------ HEADER (site nav) ------ */
/* A self-contained nav bar for the standalone experiment pages: they live outside the studio Barba
   SPA, so the real .mobile-bar / #studio-sidebar drawer (studio.css + studio-nav.js) aren't loaded
   here. Three zones: the By Default wordmark (→ home) on the left, the centred "Back to playground"
   control, and a light/dark theme toggle on the right. A 1fr / auto / 1fr grid keeps the back
   control truly centred regardless of the side widths. Spans both grid columns as row 1 of
   .experiment, so it must be the first child of .experiment. */

.experiment-header {
  grid-column: 1 / -1;
  display: grid;
  grid-template-columns: 1fr auto 1fr; /* logo (start) | back control (centred) | toggle (end) */
  align-items: center;
  gap: var(--space-m);
  padding: var(--space-s) var(--space-l);
  background: var(--background-primary);
  border-bottom: 1px solid var(--border-faded);
}

/* Wordmark → home. Height mirrors the studio .mobile-bar-logo-mark (36px + the viewBox ratio). */
.experiment-header-logo {
  display: inline-flex;
  align-items: center;
  justify-self: start;
  color: var(--text-primary);
  text-decoration: none;
}

.experiment-header-logo .svg-logo {
  width: auto;
  height: 36px;
}

/* The header carries both the full wordmark (desktop) and the compact avatar mark (mobile); show the
   one matching the viewport — mirrors the dark-mode icon swap above. Default: wordmark, mark hidden. */
.experiment-header-logo .experiment-logo-wordmark {
  aspect-ratio: 1050 / 505;
}

.experiment-header-logo .experiment-logo-mark {
  display: none;
  aspect-ratio: 1 / 1;
}

/* Theme toggle pinned to the right zone. It's a bare <button class="button"> so — like the sidebar
   toggle below — it needs the brand :focus-visible ring restored (design-system.css covers <a> /
   [role=button] but not a bare <button>). */
.experiment-header .dark-mode-toggle {
  justify-self: end;
}

.experiment-header .dark-mode-toggle:focus-visible {
  outline: 2px solid var(--text-primary);
  outline-offset: 2px;
}

/* Dark-mode icon swap: the toggle holds both a sun (.dark-mode-icon-light) and a moon
   (.dark-mode-icon-dark); show the one matching the active theme. Keyed on <html data-theme="dark">
   (set early by bd-experiment-theme.js) so the icon is correct from first paint. */
.dark-mode-toggle .dark-mode-icon-dark {
  display: none;
}

[data-theme="dark"] .dark-mode-toggle .dark-mode-icon-light {
  display: none;
}

[data-theme="dark"] .dark-mode-toggle .dark-mode-icon-dark {
  display: block;
}

/* ------ SIDEBAR TOGGLE ------ */
/* A Disclosure button that collapses the control sidebar so the stage takes the full width. The
   host page places it as the stage's first child, so it stays visible once the sidebar is gone.
   Collapsing uses display:none — the sidebar leaves both layout and the accessibility tree — while
   the grid reclaims its track. Instant by design: a control toggle needs no animation. */

.experiment-sidebar-toggle {
  position: absolute;
  top: var(--space-m);
  right: var(--space-m);
  z-index: 10;
}

/* The branded :focus-visible ring in design-system.css covers <a> / [role=button] / checkbox / radio
   but not a bare <button>, so the toggle would fall back to the UA outline. Restore the brand ring
   here (mirrors the .experiment-pane :focus-visible override below). */
.experiment-sidebar-toggle:focus-visible {
  outline: 2px solid var(--text-primary);
  outline-offset: 2px;
}

.experiment.is-sidebar-hidden {
  grid-template-columns: 1fr;
}

.experiment.is-sidebar-hidden .experiment-sidebar {
  display: none;
}

/* ------ HEADER ------ */

.experiment-sidebar-header {
  display: flex;
  flex-direction: column;
  gap: var(--space-2xs);
}

.experiment-title {
  margin: 0;
  font-size: var(--text-size-l);
  color: var(--text-primary);
}

.experiment-intro {
  margin: 0;
  color: var(--text-secondary);
}

/* ------ CTA ------ */
/* A "let's talk" prompt pinned to the foot of the sidebar (margin-top:auto), mirroring the Ceros
   playground panels. Copy lives in the page; the layout/tokens live here. */

.experiment-cta {
  display: flex;
  flex-direction: column;
  gap: var(--space-s);
  /* Pinned to the foot of the sidebar: the .experiment-pane above takes the flexible height and
     scrolls, so this stays put. (No margin-top:auto — an auto margin would cancel the pane's
     flex-grow and stop it scrolling.) */
  padding: var(--space-l);
  border-top: 1px solid var(--border-faded);
  text-align: center;
}

.experiment-cta > p {
  margin: 0;
  color: var(--text-secondary);
  text-wrap: balance;
}

.experiment-cta-title {
  margin: 0;
  font-size: var(--text-size-l);
  font-weight: var(--font-weight-regular);
  color: var(--text-primary);
}

/* ------ CONTROL PANEL ------ */
/* The control sidebar holds native design-system controls authored in each experiment's index.html
   and wired by /assets/js/experiment-controls.js. Everything here is layout glue only — the controls
   themselves (.slider-wrapper, .form-toggle, <select>, input[type=color], .button, <hr class=divider>)
   are styled by design-system.css and follow data-theme for free. */

.experiment-pane {
  display: flex;
  flex: 1 1 auto; /* fill the sidebar between the header and the pinned CTA */
  flex-direction: column;
  gap: var(--space-l);
  padding: var(--space-l);
  min-height: 0; /* allow the pane to shrink so its own content scrolls */
  overflow-y: auto; /* the controls scroll; the header + CTA stay put */
}

/* Control rows inside a tab panel share the stack's vertical rhythm (the .tab-panel itself isn't a
   flex container). Exclude hidden panels so tabs.js's .is-hidden still removes them from layout. */
.experiment-pane .tab-panel:not(.is-hidden) {
  display: flex;
  flex-direction: column;
  gap: var(--space-l);
}

/* Settings readout — a copyable dump of the current control values, hidden until the #settings URL
   fragment reveals it (filled by experiment-controls.js). Dev/handoff affordance, so it's understated;
   <pre> keeps the values monospaced and pre-wrap lets long values wrap. */
.experiment-settings {
  margin: auto 0 0; /* sit at the foot of the scrolling pane; scrolls with the controls when they overflow */
  padding: var(--space-s);
  border: 1px solid var(--border-faded);
  border-radius: var(--radius-s);
  background: var(--bg-faded-5);
  color: var(--text-secondary);
  font-size: var(--font-2xs);
  white-space: pre-wrap;
  word-break: break-word;
}

/* A single control: label (or .slider-header) above its input, with the live value inline. */
.control-row {
  display: flex;
  flex-direction: column;
  gap: var(--space-2xs);
}

/* setHidden() target — .control-row sets display:flex, which would otherwise beat the [hidden] UA rule. */
.control-row[hidden] {
  display: none;
}

/* Label-beside-control rows (toggles, colour) — label left, control right. */
.control-row.is-inline {
  flex-direction: row;
  align-items: center;
  justify-content: space-between;
}

/* Collapsible control groups use native <details>/<summary> (cms/disclosure.md). The component's
   default styling is tuned for styleguide code blocks (monospace + tiny); restyle the summary as a
   brand section header and drop the code-block treatment from the body. App-layer override only. */
.experiment-pane details {
  border: 0;
}

.experiment-pane summary {
  justify-content: space-between; /* title left, chevron (::after) right */
  padding: var(--space-s) 0;
  border-bottom: 1px solid var(--border-faded);
  font-family: var(--font-primary);
  font-size: var(--font-m); /* 18px — section titles a step above the 16px body */
  font-weight: var(--font-weight-medium);
  color: var(--text-primary);
}

/* Move the disclosure chevron to the right: suppress the design system's left ::before caret and
   redraw the same caret as ::after at the end of the row (closed → points right, open → points down). */
.experiment-pane summary::before {
  display: none;
}

.experiment-pane summary::after {
  content: "";
  width: 1.5rem; /* matches .dropdown-chevron */
  height: 1.5rem;
  flex-shrink: 0;
  /* The brand chevron-down icon, drawn as a currentColor mask so it stays theme-aware (the sprite's
     <use> can't be referenced from a pseudo-element). Rotates 180° on open, like .dropdown-chevron. */
  background-color: currentColor;
  -webkit-mask: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 24 24'%3E%3Cpath d='M12 16.4L6 10.4L7.4 9L10.5858 12.1858C11.3668 12.9668 12.6332 12.9668 13.4142 12.1858L16.6 9L18 10.4L12 16.4Z'/%3E%3C/svg%3E") center / contain no-repeat;
  mask: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 24 24'%3E%3Cpath d='M12 16.4L6 10.4L7.4 9L10.5858 12.1858C11.3668 12.9668 12.6332 12.9668 13.4142 12.1858L16.6 9L18 10.4L12 16.4Z'/%3E%3C/svg%3E") center / contain no-repeat;
  transition: transform var(--duration-2xs) var(--ease-out);
}

.experiment-pane details[open] > summary::after {
  transform: rotate(180deg);
}

.experiment-pane .disclosure-content {
  display: flex;
  flex-direction: column;
  gap: var(--space-l);
  padding: var(--space-m) 0 0;
  border: 0; /* drop the styleguide content border */
  font: inherit; /* drop the styleguide monospace */
}

/* Preset row — the choices share the panel width evenly so they read as one segmented row instead
   of clustering at the left. flex-basis:0 + min-width:0 gives equal columns and lets them shrink in
   the narrow sidebar without wrapping. */
.experiment-presets {
  gap: var(--space-xs);
}

.experiment-presets .button {
  flex: 1 1 0;
  min-width: 0;
}

/* ------ RESPONSIVE ------ */

@media (max-width: 768px) {
  /* Stack the header over the stage over the control panel and fill the viewport exactly (no bottom
     gap). The panel takes the remaining height and scrolls internally — minmax(0,1fr) lets the track
     shrink below its content so the controls scroll inside instead of stretching the page. */
  .experiment {
    grid-template-columns: 1fr;
    grid-template-rows: auto 45vh minmax(0, 1fr); /* header, stage, panel — fallback for no dvh */
    grid-template-rows: auto 45dvh minmax(0, 1fr);
    height: 100vh; /* fallback for browsers without dvh */
    height: 100dvh;
  }

  /* Collapsed (toggle): the panel hides and the stage fills the screen below the header. */
  .experiment.is-sidebar-hidden {
    grid-template-rows: auto minmax(0, 1fr); /* header, stage */
  }

  .experiment.is-sidebar-hidden .experiment-sidebar {
    display: none;
  }

  /* On mobile the panel sits in a short grid track, so scroll the WHOLE sidebar (CTA included) rather
     than pinning the CTA + scrolling only the pane — there isn't room to reserve space for the CTA. */
  .experiment-sidebar {
    height: auto;
    min-height: 0;
    overflow-y: auto;
    border-left: 0;
    border-top: 1px solid var(--border-faded);
  }

  .experiment-pane {
    flex: 0 0 auto; /* natural height; the whole sidebar scrolls, not just the pane */
    overflow-y: visible;
  }

  /* The panel stacks BELOW the stage on mobile, so collapsing "pushes it down". Move the toggle to
     the bottom-right and rotate the sidebar glyph 90° so it reads as a bottom-docked panel: with the
     icon swap (open ► / collapsed ◄), a uniform clockwise turn points the arrow DOWN when open
     ("push down to collapse") and UP when collapsed ("pull up to expand"). Purely visual — the
     aria-label still states the action. */
  .experiment-sidebar-toggle {
    top: auto;
    bottom: var(--space-m);
    right: var(--space-m);
  }

  .experiment-sidebar-toggle .svg-icn {
    transform: rotate(90deg);
  }

  /* Swap the full wordmark for the compact avatar mark — the wordmark is too wide for the slim bar. */
  .experiment-header-logo .experiment-logo-wordmark {
    display: none;
  }

  .experiment-header-logo .experiment-logo-mark {
    display: block;
  }
}
