/* ==========================================================================
   Calculus 3 demo kit — figure styling.

   A figure sits directly on the page: no border, no panel background. Its
   drawing surface inherits the page background, so flipping the book's theme
   flips every figure with it.

   The colour system has three layers, ported from sjtSite (Aug 2026):
   - neutrals named by distance from the page: faint → medium → bold → ink;
   - a rainbow of eight hues at matched lightness and chroma;
   - materials for lit 3D solids, which read differently than marks on paper.
   Semantic roles (tangent, gradient, …) are defined on top of these, so a
   role means the same thing in every figure of the book.
   ========================================================================== */

:root {
  /* The page's own background, so figures are seamless in any theme.
     --bg is the book site's variable; the literal is a last-resort
     fallback for a demo mounted outside the book. */
  --calc3-demo-background: var(--bg, #ffffff);

  /* Neutrals, by distance from the page. */
  --calc3-faint: #a4b3be;
  --calc3-medium: #8fa3b5;
  --calc3-bold: #7a8289;
  --calc3-ink: #24292e;

  /* The rainbow: eight hues at L*50 C*32 (gold breaks rank at L*62). */
  --calc3-red: #ab6264;
  --calc3-orange: #a16a4b;
  --calc3-gold: #b3922d;
  --calc3-green: #518257;
  --calc3-teal: #028584;
  --calc3-blue: #317eab;
  --calc3-violet: #7372a8;
  --calc3-plum: #9d658e;

  /* Materials: the body of a lit solid, lines drawn on it, structural metal.
     Cool silver for this book, where the site uses warm cream. */
  --calc3-material-surface: #c3d0dd;
  --calc3-material-grid: #8fa3b5;
  --calc3-metal: #aeb6be;

  --calc3-demo-grid: #d8dee4;
  --calc3-region: #a8c4dc;

  /* Ramp anchors. Used only to deepen or lighten a hue inside a colour ramp,
     and deliberately absent from the dark block: deep water is darker than
     shallow water on any page, so these two must not flip with the theme the
     way ink and the page background do. */
  --calc3-shade: #16202b;
  --calc3-tint: #f2f6f9;
}

/* Dark theme. The site's boot script stamps data-theme on the root element
   pre-paint, and the toggle flips it. */
[data-theme="dark"] {
  --calc3-faint: #464c51;
  --calc3-medium: #567083;
  --calc3-bold: #757d84;
  --calc3-ink: #d2d8dc;

  /* The same eight hue angles at L*65 C*30. */
  --calc3-red: #d38a8b;
  --calc3-orange: #c99173;
  --calc3-gold: #ac9e68;
  --calc3-green: #7aa97e;
  --calc3-teal: #4aacab;
  --calc3-blue: #65a4d1;
  --calc3-violet: #9b98ce;
  --calc3-plum: #c48db5;

  /* Materials flip their contrast rule: on a dark ground the grid sits
     lighter than the body — heavier means farther from the page. */
  --calc3-material-surface: #4a5462;
  --calc3-material-grid: #7e93a6;
  --calc3-metal: #79818a;

  --calc3-demo-grid: #33393f;
  --calc3-region: #4e6e8c;

  /* The book site defines --bg on both themes and flips it instantly, so
     the surface can simply follow it; the literal is the last-resort
     fallback for a demo mounted outside the book. */
  --calc3-demo-background: var(--bg, #1b1f24);
}

/* Semantic vocabulary. A role means the same thing in every figure.
   These are aliases into the layers above, and a custom property is
   substituted where it is DEFINED, then inherited already-resolved — so the
   alias block must also apply to the dark-marked element itself. Otherwise
   an alias defined only on :root would resolve against html's light values
   even when the dark mark sits on a subtree. */
:root,
[data-theme="dark"] {
  --calc3-demo-foreground: var(--calc3-ink);
  --calc3-demo-muted: var(--calc3-bold);
  --calc3-demo-primary: var(--calc3-blue);
  --calc3-demo-secondary: var(--calc3-red);
  --calc3-demo-accent: var(--calc3-teal);

  --calc3-point: var(--calc3-ink);
  --calc3-displacement: var(--calc3-blue);
  --calc3-tangent: var(--calc3-teal);
  --calc3-normal: var(--calc3-violet);
  --calc3-binormal: var(--calc3-bold);
  --calc3-gradient: var(--calc3-orange);
  --calc3-positive: var(--calc3-red);
  --calc3-negative: var(--calc3-blue);
  --calc3-surface: var(--calc3-material-surface);
  --calc3-guide: var(--calc3-faint);
  --calc3-highlight: var(--calc3-gold);
  --calc3-axis: var(--calc3-bold);
  --calc3-axis-x: var(--calc3-red);
  --calc3-axis-y: var(--calc3-green);
  --calc3-axis-z: var(--calc3-blue);

  /* Several things of the same kind, told apart only by index. */
  --calc3-series-1: var(--calc3-blue);
  --calc3-series-2: var(--calc3-teal);
  --calc3-series-3: var(--calc3-orange);
  --calc3-series-4: var(--calc3-violet);
  --calc3-series-5: var(--calc3-gold);
  --calc3-series-6: var(--calc3-plum);
}

/* ---- The figure ---------------------------------------------------------- */

.demo-figure {
  margin-block: 2rem;
}

calc3-demo {
  display: block;
  position: relative;
  /* Figures sit at 80% of the text column, centred; width="…" on the
     declaration overrides per figure, and phones get the full column. */
  width: 80%;
  margin-inline: auto;
  min-height: 12rem;
  color: var(--calc3-demo-foreground);
}

@media (max-width: 700px) {
  calc3-demo {
    width: 100% !important;
  }
}

.calc3-demo__surface,
.calc3-demo__poster {
  display: block;
  position: relative;
  width: 100%;
}

.calc3-demo__surface[data-size="square"] { aspect-ratio: 1; }
.calc3-demo__surface[data-size="portrait"] { aspect-ratio: 3 / 4; }
.calc3-demo__surface[data-size="standard"] { aspect-ratio: 4 / 3; }
.calc3-demo__surface[data-size="wide"] { aspect-ratio: 16 / 9; }
.calc3-demo__surface[data-size="panorama"] { aspect-ratio: 21 / 9; }

.calc3-demo__canvas {
  display: block;
  width: 100%;
  height: 100%;
  touch-action: pan-y;
}

.calc3-demo__poster {
  height: auto;
}

.calc3-demo__status,
.calc3-demo__error {
  position: absolute;
  inset: 0;
  display: grid;
  place-items: center;
  padding: 1rem;
  color: var(--calc3-demo-muted);
  font-size: 0.85rem;
  text-align: center;
}

.calc3-demo__error {
  position: static;
  min-height: 12rem;
  color: var(--calc3-red);
}

/* ---- Multi-pane stage ---------------------------------------------------- */

.calc3-stage {
  display: flex;
  gap: 1.25rem;
  width: 100%;
  height: 100%;
}

.calc3-stage__pane {
  position: relative;
  min-width: 0;
  min-height: 0;
}

/* A group stacks its own panes, so a figure can set two small views beside one
   large one. */
.calc3-stage__group {
  display: flex;
  gap: 1rem;
}

/* Absolute rather than height:100%, so a pane's canvas fills whatever the flex
   layout hands it, in a row or in a column. */
.calc3-stage__pane > .calc3-demo__canvas {
  position: absolute;
  inset: 0;
  width: 100%;
  height: 100%;
}

/* ---- Tab strip ----------------------------------------------------------- */

.calc3-tabs {
  display: flex;
  flex-wrap: wrap;
  gap: 1.1rem;
  padding: 0 0 0.35rem;
  margin-bottom: 0.4rem;
}

.calc3-tab {
  font-family: var(--bs-font-monospace, ui-monospace, Menlo, monospace);
  font-size: 0.78rem;
  padding: 0.15rem 0;
  color: var(--calc3-demo-muted);
  background: transparent;
  border: none;
  border-bottom: 2px solid transparent;
  cursor: pointer;
}

.calc3-tab:hover {
  color: var(--calc3-demo-foreground);
}

.calc3-tab[aria-selected="true"] {
  color: var(--calc3-demo-foreground);
  border-bottom-color: var(--calc3-blue);
}

/* ---- Control panel ------------------------------------------------------- */

/* Two rows: what the reader can touch, then what the figure reports. Mixed
   into one line they compete, and the space under a figure reads as clutter
   rather than as an instrument panel. */
.calc3-controls {
  padding: 0.75rem 0 0.2rem;
  font-size: 0.82rem;
  line-height: 1.35;
}

.calc3-controls__inputs {
  display: flex;
  flex-wrap: wrap;
  gap: 0.7rem 1.6rem;
  align-items: flex-end;
}

.calc3-controls__readouts {
  display: flex;
  flex-wrap: wrap;
  gap: 0.3rem 1.75rem;
  margin-top: 0.7rem;
  padding-top: 0.55rem;
  border-top: 1px solid color-mix(in srgb, var(--calc3-faint), transparent 72%);
}

.calc3-control {
  display: flex;
  flex-direction: column;
  gap: 0.25rem;
  /* Sliders stop growing once they are long enough to aim with; two of them
     should not stretch to fill the whole width of the figure. */
  flex: 0 1 13rem;
  min-width: 8.5rem;
}

.calc3-control--toggle,
.calc3-control--button {
  flex: 0 0 auto;
  min-width: 0;
}

/* A readout is one quiet line, not a titled field. */
.calc3-control--readout {
  flex: 0 1 auto;
  flex-direction: row;
  align-items: baseline;
  gap: 0.45rem;
  min-width: 0;
}

.calc3-readout__name {
  color: var(--calc3-demo-muted);
}

.calc3-readout__body {
  color: var(--calc3-demo-foreground);
  font-variant-numeric: tabular-nums;
}

.calc3-control__head {
  display: flex;
  justify-content: space-between;
  align-items: baseline;
  gap: 0.6rem;
  color: var(--calc3-demo-muted);
}

.calc3-control__value {
  font-family: var(--bs-font-monospace, ui-monospace, Menlo, monospace);
  font-size: 0.72rem;
  font-variant-numeric: tabular-nums;
  color: var(--calc3-demo-foreground);
}

.calc3-control__text {
  font-variant-numeric: tabular-nums;
  color: var(--calc3-demo-foreground);
}

/* Slider: a thin quiet track, a small outlined thumb. */
.calc3-control input[type="range"] {
  -webkit-appearance: none;
  appearance: none;
  width: 100%;
  height: 20px;
  margin: 0;
  background: transparent;
  cursor: pointer;
}

.calc3-control input[type="range"]::-webkit-slider-runnable-track {
  height: 4px;
  background: color-mix(in srgb, var(--calc3-faint), transparent 45%);
  border-radius: 999px;
}

.calc3-control input[type="range"]::-moz-range-track {
  height: 4px;
  background: color-mix(in srgb, var(--calc3-faint), transparent 45%);
  border-radius: 999px;
}

.calc3-control input[type="range"]::-webkit-slider-thumb {
  -webkit-appearance: none;
  width: 13px;
  height: 13px;
  margin-top: -4.5px;
  background: var(--calc3-demo-background);
  border: 1.5px solid var(--calc3-blue);
  border-radius: 50%;
}

.calc3-control input[type="range"]::-moz-range-thumb {
  width: 13px;
  height: 13px;
  background: var(--calc3-demo-background);
  border: 1.5px solid var(--calc3-blue);
  border-radius: 50%;
  box-sizing: border-box;
}

.calc3-control input[type="range"]:focus-visible {
  outline: 1px solid var(--calc3-blue);
  outline-offset: 2px;
}

.calc3-control__check {
  display: flex;
  align-items: center;
  gap: 0.4rem;
  color: var(--calc3-demo-muted);
  cursor: pointer;
  user-select: none;
}

.calc3-control__check input {
  accent-color: var(--calc3-blue);
}

.calc3-control__check:hover {
  color: var(--calc3-demo-foreground);
}

.calc3-control button,
.calc3-control select {
  font-family: var(--bs-font-monospace, ui-monospace, Menlo, monospace);
  font-size: 0.75rem;
  color: var(--calc3-demo-muted);
  background: transparent;
  border: 1px solid color-mix(in srgb, var(--calc3-medium), transparent 35%);
  border-radius: 4px;
  padding: 0.3rem 0.65rem;
  cursor: pointer;
}

.calc3-control button:hover,
.calc3-control select:hover {
  color: var(--calc3-blue);
  border-color: var(--calc3-blue);
}

.calc3-control button:focus-visible,
.calc3-control select:focus-visible {
  outline: 1px solid var(--calc3-blue);
  outline-offset: 2px;
}

/* ---- Overlay labels ------------------------------------------------------ */

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

.calc3-label {
  position: absolute;
  top: 0;
  left: 0;
  white-space: nowrap;
  font-size: 0.88rem;
  font-variant-numeric: tabular-nums;
  color: var(--calc3-demo-foreground);
  text-shadow:
    0 0 3px var(--calc3-demo-background),
    0 0 3px var(--calc3-demo-background),
    0 0 6px var(--calc3-demo-background);
}

.calc3-label .katex,
.calc3-control .katex {
  font-size: 1.04em;
}

/* ---- Inset panel --------------------------------------------------------- */

.calc3-inset {
  position: absolute;
  display: block;
  border: 1px solid color-mix(in srgb, var(--calc3-faint), transparent 40%);
  border-radius: 0.4rem;
  background: var(--calc3-demo-background);
  touch-action: none;
}

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