/* ---------------- the tv-scene ----------------
   Styles for host/tv-scene.js. Notes: host/tv-scene/README.md

   A box positioned by script inside the prototype's frame, so nothing here knows where
   it sits — only how it stacks: behind #survey (20), which covers the window
   transparently, so the drawing shows through the researcher's overlay while nothing
   behind it can be clicked. */

#tv-scene {
  position: fixed; z-index: 19; overflow: hidden;
  pointer-events: none;               /* scenery: it never takes a click */
  background: #07080a;
}
#tv-scene[hidden] { display: none; }
/* TvScene.shake: there is no camera to shake, so the drawing itself jolts — a few
   decreasing knocks, in fractions of its own size so it reads the same at any scale. */
#tv-scene.tvs-shake { animation: tvs-shake var(--tvs-shake-ms, 250ms) linear; }
/* The same first knock as before, then it rings down: each swing smaller, the last ones
   barely there, so it settles into stillness rather than stopping. */
@keyframes tvs-shake {
  0%, 100% { transform: none; }
  8%  { transform: translate(-.4%, .2%); }
  17% { transform: translate(.35%, -.25%); }
  26% { transform: translate(-.28%, -.15%); }
  36% { transform: translate(.22%, .14%); }
  46% { transform: translate(-.16%, .1%); }
  57% { transform: translate(.11%, -.08%); }
  68% { transform: translate(-.07%, -.05%); }
  79% { transform: translate(.04%, .03%); }
  90% { transform: translate(-.02%, .01%); }
}
@media (prefers-reduced-motion: reduce) { #tv-scene.tvs-shake { animation: none; } }
/* While placing, it has to come out in front of #survey and take the pointer. */
#tv-scene.tvs-editing { z-index: 44; pointer-events: auto; }

.tvs-layer {
  position: absolute; inset: 0; width: 100%; height: 100%;
  object-fit: fill;                   /* layers are full-frame, so they stretch with it */
  user-select: none;
  /* Nearest-neighbour. The drawing is scaled up from the frame's own size to whatever
     the window gives it, and smoothing a line drawing on the way up only makes the
     strokes mushy — the plates keep their smoothing, since the cut wants real greys to
     work on. */
  image-rendering: pixelated;
}

/* The controls hint (tv-scene.js, buildControls): sprite strips stepped through their
   beats, crisp, every icon the same height and centred on the row. Placed and gapped by
   script, in the frame's own pixels (layout). */
.tvs-controls {
  position: absolute; display: flex; align-items: center;
  pointer-events: none;
}
.tvs-controls[hidden] { display: none; }
.tvs-control {
  height: 100%; aspect-ratio: 1;
  background-repeat: no-repeat;
  background-size: calc(var(--tvs-beats) * 100%) 100%;
  image-rendering: pixelated;
  animation: tvs-wobble var(--tvs-cycle) steps(var(--tvs-beats), jump-none) infinite;
}
@keyframes tvs-wobble { from { background-position-x: 0% } to { background-position-x: 100% } }
@media (prefers-reduced-motion: reduce) { .tvs-control { animation: none; } }

/* The black a faded cut goes through (TvScene.black). Over every layer and frame; the
   duration is set per fade from the manifest, so only the property is named here. */
.tvs-black {
  position: absolute; inset: 0;
  background: #000; opacity: 0;
  transition: opacity 0ms linear;
}
.tvs-black.tvs-dark { opacity: 1; }

/* A blink is a layer that is normally not there. No transition: an eyelid does not
   fade, and at this scale a crossfade reads as a glitch rather than a blink. */
.tvs-layer.tvs-blink { opacity: 0; }
.tvs-layer.tvs-blink.tvs-shut { opacity: 1; }

/* The feedback chain (tv-scene.js, feed()). A cable's flash is a canvas copy of the cable
   filled solid in one colour (fillCable), laid straight over it; the heart's is a white copy
   of whichever drawing is showing, through a filter. Both on at once, then off over --fade.
   The heart is two drawings, one showing at a time — visibility, not opacity, so the
   editor's art slider can't unhide one. */
.tvs-cable-flash, .tvs-heart-flash {
  position: absolute; inset: 0; width: 100%; height: 100%;
  object-fit: fill; user-select: none; image-rendering: pixelated;
  opacity: 0; transition: opacity var(--fade, 300ms) linear;
}
.tvs-heart-flash { filter: brightness(0) invert(1); }
/* The cable drawings at rest, darkened from the art (FEED.cableShade) so the flash has
   something to stand out against. Their flash copies are not .tvs-cable and stay full. */
.tvs-cable { filter: brightness(var(--tvs-cable-shade, 1)); }
.tvs-cable-flash.tvs-lit, .tvs-heart-flash.tvs-lit { opacity: 1; transition: none; }
.tvs-heart-off { visibility: hidden; }

/* A screen is an ordinary element warped onto four corners by matrix3d. Everything drawn
   inside it — the plate, the tint, later the scanlines — is warped along with it.

   --tvs-t is the 1-bit threshold, 0..1, and script animates it. Moving the cut is the
   whole reaction — there was a brightness flash over the top of it and it was doing
   nothing the threshold was not already doing better. */
.tvs-screen {
  --tvs-t: .5;
  position: absolute; transform-origin: 0 0; overflow: hidden;
  background: #000;
}
/* A screen with no plate is grey rather than black, or it vanishes into the drawing
   and there is nothing to grab while placing it. */
.tvs-bare { background: #4a4a4a; }

/* Pure black and white, with a moving cut.

   contrast() pivots on 0.5, so cranking it far enough sends every pixel to 0 or 1 and
   there is no grey left at all — genuinely one bit, not a high-contrast look. brightness
   is what moves the cut: scaling by .5/t makes a pixel land above the pivot exactly when
   it was brighter than t, so the threshold is t. Order matters, filters apply left to
   right, and the whole chain is compositor work on an element that is already warped.

   invert comes first, for the plates drawn dark-on-light paper. Flipping those puts every
   plate on the same footing before the cut is applied, so one threshold serves all nine TVs
   and every tube reads as lit from within rather than as a lit rectangle. */
.tvs-plate {
  position: absolute; inset: 0; overflow: hidden;
  filter: invert(var(--tvs-inv, 0)) grayscale(1)
          brightness(calc(.5 / var(--tvs-t))) contrast(2400%);
  will-change: filter;
}

/* A tube that has not been switched on yet. The drawing and the televisions in it are
   still there — this hides only the picture, which takes the sway, the ghost and the
   rolling bar with it, because an unlit tube has nothing moving in it. The screen's own
   black shows through beneath the static (below), so the set reads as off rather than
   as missing. Per screen rather than on the root, because tuning in (tv-scene.js's
   tuneIn) switches the nine on one at a time. */
.tvs-screen.tvs-off .tvs-plate { visibility: hidden; }

/* Idle static: tv-scene.js's tickNoise swaps which baked frame this points at — see the
   long comment on NOISE there for why it's a full background per frame at the default
   position rather than one sprite stepped by background-position: the screen this sits in
   is warped by matrix3d, and Chromium does not paint a background at a non-zero position
   inside one. Opacity, not visibility, so switching it off crosses fades with the plate
   coming on instead of cutting — the static dissolving as the picture catches is the
   point of the tune-in.

   Left to scale down smoothly (no pixelated) and then crushed with the same
   brightness()+contrast(2400%) pair .tvs-plate uses — the source is above the size the
   screen shows it at (sizeFor, tv-scene.js), so the smooth downscale gives real grey
   values, and the threshold turns those into fine, hard-edged grain instead of the
   source's own visible pixels. --nb is cutFor(), set per screen in tv-scene.js and
   inherited by this: the pivot the threshold reads, same idea as the plate's --tvs-t but
   fixed rather than animated. The fallback is mid-range, so a screen that somehow never
   got one still reads as static rather than as a lit white rectangle.

   Full opacity read as too sharp against the black around it — a hard, flickering thing
   fighting for attention rather than a set left idling — so it sits under 1 even at its
   most "on"; see NOISE.cycle (tv-scene.js) for the same thinking about its pace. */
.tvs-noise {
  position: absolute; inset: 0;
  background-repeat: no-repeat;
  background-size: 100% 100%; background-position: 0 0;
  filter: brightness(calc(.5 / var(--nb, .77))) contrast(2400%);
  opacity: 0; visibility: hidden;
  transition: opacity .12s linear;
}
.tvs-screen.tvs-off .tvs-noise { opacity: .55; visibility: visible; }

/* Constant, quiet motion. A warped plane is still a plane and cannot bend, but a stack
   of bands can shear: the plate is cut into horizontal strips that slide against one
   another, further at the bottom than the top and each on its own phase. That is the
   cheap equivalent of swaying the UVs out of sync, and it costs nothing but composited
   transforms — no clock, no script running while nothing is happening. */
.tvs-band {
  position: absolute; left: 0; right: 0; overflow: hidden;
  /* A hair taller than its share, so rounding cannot open a dark seam between bands. */
  top: calc(100% * var(--i) / var(--n));
  height: calc(100% / var(--n) + .6px);
}
.tvs-band img {
  position: absolute; display: block; user-select: none;
  /* n band-heights is the whole screen, and pulling it up by i of them shows this
     band's slice of it — so the strips reassemble into one image. */
  height: calc(100% * var(--n));
  top: calc(-100% * var(--i));
  /* Wider than it needs to be by the amplitude at each edge, so sliding never brings
     an edge into view. This is what "wrapping" buys in a shader, without the wrap. */
  left: calc(-1 * var(--tvs-amp, 0px));
  width: calc(100% + 2 * var(--tvs-amp, 0px));
  object-fit: cover;                  /* portrait plates onto landscape quads */
  animation: tvs-sway var(--p) ease-in-out infinite;
  animation-delay: var(--d);
}
/* A graph tube's live canvas fills the plate; smooth-scaled like the plates, so the cut has
   real greys to work on. */
.tvs-graph-cv { position: absolute; inset: 0; width: 100%; height: 100%; display: block; }

@keyframes tvs-sway {
  0%, 100% { transform: translateX(calc(var(--a) * -1)); }
  50%      { transform: translateX(var(--a)); }
}

/* The ghost: the same picture again, offset and weaker, lagging the sway slightly the
   way a reflected signal arrives late. It lives *inside* the filter, so the threshold
   is what decides whether it shows — which means it flares in and out as the cut moves
   during a strike, and stays one bit rather than introducing grey. */
.tvs-ghost {
  left: calc(-1 * var(--tvs-amp, 0px) + var(--gx));
  opacity: var(--gi);
  mix-blend-mode: screen;
}

/* The rolling bar, also inside the filter: a brightness disturbance in the signal
   rather than a white stripe laid over the picture. Under the threshold it reads as a
   band where more of the plate lights up, which is what a real one does. Every tube
   gets its own period, thickness, strength and direction, deliberately with nothing in
   common — see ROLL in tv-scene.js. */
.tvs-roll {
  position: absolute; left: 0; right: 0; top: 0; height: 100%;
  pointer-events: none; mix-blend-mode: screen;
  background: linear-gradient(to bottom,
    transparent calc(50% - var(--bh)),
    rgba(255, 255, 255, var(--bi)) 50%,
    transparent calc(50% + var(--bh)));
  animation: tvs-roll var(--rp) linear infinite var(--rdir, normal);
  animation-delay: var(--rd);
}
@keyframes tvs-roll {
  from { transform: translateY(-60%); }
  to   { transform: translateY(60%); }
}

@media (prefers-reduced-motion: reduce) {
  .tvs-band img, .tvs-roll { animation: none; }
  /* .tvs-noise has no CSS animation — it's cycled by tv-scene.js's startNoise, which
     checks the same media query before ever starting the timer. */
}

.tvs-tag {
  position: absolute; left: 6px; top: 5px; z-index: 3;
  font: 10px/1 ui-monospace, SFMono-Regular, Menlo, monospace;
  letter-spacing: .18em; text-transform: uppercase;
  color: rgba(0, 0, 0, .55); opacity: 0;
}
/* Named only when there is nothing else to look at, or while being placed. */
.tvs-bare .tvs-tag, .tvs-editing .tvs-tag { opacity: 1; }

/* ---------------- the editor (E) ---------------- */
#tvs-edit { position: absolute; left: 0; top: 0; overflow: visible; pointer-events: auto; }
#tvs-edit[hidden] { display: none; }

.tvs-quad {
  fill: rgba(216, 166, 87, .06); stroke: #d8a657; stroke-width: 1.5;
  cursor: move;
}
.tvs-quad.on { fill: rgba(216, 166, 87, .16); stroke-width: 2; }
.tvs-handle { fill: #12140f; stroke: #d8a657; stroke-width: 2; cursor: grab; }
.tvs-handle.on { fill: #d8a657; }

/* Scrolls rather than running off the window — the graph tab is tall. */
#tvs-ui {
  position: fixed; right: 12px; bottom: 12px; z-index: 45;
  min-width: 250px; max-height: calc(100vh - 24px); overflow-y: auto;
  background: rgba(10, 11, 14, .93);
  border: 1px solid #3a352c; border-radius: 4px;
  color: #efe7d6;
  font: 12px/1.6 ui-monospace, SFMono-Regular, Menlo, monospace;
}
#tvs-ui[hidden], .tvs-tab[hidden], .tvs-foot[hidden] { display: none; }
.tvs-head {
  display: flex; align-items: center; justify-content: space-between; gap: 8px;
  padding: 8px 10px; cursor: move; user-select: none;
  border-bottom: 1px solid #3a352c;
}
.tvs-collapse {
  font: inherit; line-height: 1; color: #efe7d6; background: transparent;
  border: 1px solid #3a352c; border-radius: 2px; width: 20px; height: 20px; padding: 0;
  cursor: pointer; flex: none;
}
.tvs-collapse:hover { border-color: #d8a657; color: #d8a657; }
.tvs-body { padding: 10px 12px; }
.tvs-body[hidden] { display: none; }
.tvs-row { display: flex; align-items: center; gap: 8px; margin-bottom: 6px; }
.tvs-row label { display: flex; align-items: center; gap: 6px; flex: 1; white-space: nowrap; }
.tvs-row input[type=range] { flex: 1; accent-color: #d8a657; }
.tvs-dirty { color: #d8a657; }
.tvs-sel { color: #8e8676; }
.tvs-legend {
  border-top: 1px solid #3a352c; border-bottom: 1px solid #3a352c;
  margin-bottom: 6px; padding: 4px 0;
}
.tvs-legend-row { display: flex; justify-content: space-between; gap: 10px; margin-bottom: 0; padding: 2px 0; cursor: pointer; }
.tvs-legend-row:hover { color: #d8a657; }
.tvs-legend-row code { color: #d8a657; font: inherit; }
.tvs-legend-row span { color: #8e8676; }
.tvs-legend-row.tvs-legend-missing { opacity: .4; cursor: default; }
.tvs-legend-row.tvs-legend-missing:hover { color: inherit; }
.tvs-legend-row.tvs-picked code { text-decoration: underline; }
#tvs-ui button {
  font: inherit; color: #efe7d6; background: transparent;
  border: 1px solid #3a352c; border-radius: 2px; padding: 4px 9px; cursor: pointer;
}
#tvs-ui button:hover { border-color: #d8a657; color: #d8a657; }
#tvs-ui button:disabled { opacity: .4; cursor: default; border-color: #3a352c; color: #efe7d6; }
#tvs-ui button.on { border-color: #d8a657; color: #d8a657; }
.tvs-tabs button, .tvs-tab .tvs-row button { flex: 1; }

/* The graph tab. */
.tvs-graph canvas {
  display: block; margin-bottom: 8px;
  width: 360px; height: 360px; max-width: calc(100vw - 50px); max-height: calc(100vw - 50px);
}
.tvs-graph-read, .tvs-graph-v { color: #8e8676; }
.tvs-graph-v { min-width: 3.5em; text-align: right; }
.tvs-graph-set { border-top: 1px solid #3a352c; margin: 8px 0 4px; padding-top: 6px; }
.tvs-graph-set summary { cursor: pointer; color: #8e8676; margin-bottom: 6px; }
.tvs-graph-set summary:hover { color: #d8a657; }

/* The feedback tab: one timeline row per step of the chain, solid while it is on and
   faint while it falls back. */
.tvs-feed-line { border-top: 1px solid #3a352c; margin: 8px 0 6px; padding-top: 6px; }
.tvs-feed-row { display: flex; align-items: center; gap: 8px; height: 14px; margin-bottom: 3px; }
.tvs-feed-row > span { width: 6em; color: #8e8676; white-space: nowrap; }
.tvs-feed-row > div { position: relative; flex: 1; height: 8px; }
.tvs-feed-row i { position: absolute; top: 0; bottom: 0; background: #d8a657; }
/* Ramps drawn as ramps, so a short slide in or out still reads apart from the hold. */
.tvs-feed-row i.tvs-feed-in { background: linear-gradient(to right, rgba(216, 166, 87, .2), #d8a657); }
.tvs-feed-row i.tvs-feed-off { opacity: .35; }
.tvs-feed-row i.tvs-feed-mark { width: 2px; margin-left: -1px; background: #efe7d6; top: -3px; bottom: -3px; }
.tvs-feed-scale { display: flex; justify-content: space-between; color: #6e685c; font-size: 10px; height: auto !important; }
.tvs-feed-scale b { font-weight: normal; }
.tvs-feed-last { color: #8e8676; white-space: pre-wrap; word-break: break-word; max-width: 300px; }
.tvs-feed-fire { flex-wrap: wrap; gap: 4px; }
.tvs-tab .tvs-feed-fire button { flex: 1 0 28%; padding: 3px 4px; }

.tvs-hint { color: #6e685c; font-size: 10px; line-height: 1.5; margin-top: 4px; max-width: 300px; }
