/* The retro layer (channels.js, retro.js): language channels keyed like an old TV set's, a cassette for the
   theme switch, a VCR key for back-to-top, and the on-screen display, static and power effects laid over the
   page. Dark plastic with a green phosphor light in the dark theme; beige plastic and an amber lamp in the
   light one. */

:root {
  --retro-bezel: linear-gradient(#efe8d6, #dcd1b6);
  --retro-bezel-edge: #c4b896;
  --retro-key: linear-gradient(#f8f3e6, #e3d9bf);
  --retro-key-edge: #cdbf9c;
  --retro-key-text: #6e6553;
  --retro-key-drop: #b3a581;
  --retro-lit: #b4530a;
  --retro-lit-glow: #ff9a3c66;
  --retro-lit-key: linear-gradient(#fcebd0, #f2d6a8);
  --retro-lit-edge: #d8a463;
  --retro-screen: #fff2c8;
  --retro-screen-glow: #ffcf6e;
  --cassette-shell: #c7b99b;
  --cassette-label: #f7efd9;
  --cassette-window: #574c43;
}
:root[data-theme="dark"] {
  --retro-bezel: linear-gradient(#1c1b26, #0d0c14);
  --retro-bezel-edge: #2d2b3b;
  --retro-key: linear-gradient(#2a2838, #191823);
  --retro-key-edge: #312f42;
  --retro-key-text: #8f8bab;
  --retro-key-drop: #07060c;
  --retro-lit: #ffd27a;
  --retro-lit-glow: #ffb74d80;
  --retro-lit-key: linear-gradient(#302017, #1b1011);
  --retro-lit-edge: #885632;
  --retro-screen: #08070f;
  --retro-screen-glow: #ffc15c;
  --cassette-shell: #27242e;
  --cassette-label: #393442;
  --cassette-window: #0b0a10;
}
@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) {
    --retro-bezel: linear-gradient(#1c1b26, #0d0c14);
    --retro-bezel-edge: #2d2b3b;
    --retro-key: linear-gradient(#2a2838, #191823);
    --retro-key-edge: #312f42;
    --retro-key-text: #8f8bab;
    --retro-key-drop: #07060c;
    --retro-lit: #ffd27a;
    --retro-lit-glow: #ffb74d80;
    --retro-lit-key: linear-gradient(#302017, #1b1011);
    --retro-lit-edge: #885632;
    --retro-screen: #08070f;
    --retro-screen-glow: #ffc15c;
    --cassette-shell: #27242e;
    --cassette-label: #393442;
    --cassette-window: #0b0a10;
  }
}

/* Held back while the page opens on another channel (see the inline script in <head>). */
html[data-channel-pending] body { visibility: hidden; }

/* ───────── Channel keys ───────── */

.channel {
  display: inline-flex; align-items: center; gap: 3px; padding: 4px 5px 4px 7px; border-radius: 10px;
  background: var(--retro-bezel); border: 1px solid var(--retro-bezel-edge);
  box-shadow: inset 0 1px 0 #ffffff1f, inset 0 -2px 5px #0000002e, 0 1px 2px #0000002e;
  font: 400 1.15rem/1 "VT323", var(--font-mono);
}
.channel__tag { margin-right: 2px; color: var(--retro-key-text); opacity: .75; font-size: .95rem; letter-spacing: .06em; }
.channel__key {
  min-width: 31px; height: 27px; padding: 1px 5px 0; border-radius: 5px; cursor: pointer;
  background: var(--retro-key); border: 1px solid var(--retro-key-edge); color: var(--retro-key-text);
  font: inherit; letter-spacing: .05em;
  box-shadow: 0 2px 0 var(--retro-key-drop), inset 0 1px 0 #ffffff1c;
  transition: transform .12s, box-shadow .18s, color .18s, border-color .18s;
}
@media (hover: hover) and (pointer: fine) {
  .channel__key:hover { color: var(--text); border-color: var(--retro-lit-edge); box-shadow: 0 2px 0 var(--retro-key-drop), inset 0 0 8px -2px var(--retro-lit-glow); }
}
.channel__key:active { transform: translateY(2px); box-shadow: 0 0 0 var(--retro-key-drop); }
.channel__key[aria-pressed="true"] {
  color: var(--retro-lit); text-shadow: 0 0 6px var(--retro-lit-glow);
  background: var(--retro-lit-key); border-color: var(--retro-lit-edge);
  transform: translateY(1px); box-shadow: 0 1px 0 var(--retro-key-drop), inset 0 0 9px var(--retro-lit-glow);
}
/* AV sits apart from the language channels: a different input, not another station. */
.channel__key--av { margin-left: 5px; position: relative; }
.channel__key--av::before { content: ""; position: absolute; left: -5px; top: 3px; bottom: 3px; width: 1px; background: var(--retro-bezel-edge); }
@media (max-width: 420px) { .channel__tag { display: none; } .channel { padding-left: 5px; } }

/* ───────── The theme switch: a light or dark cassette ───────── */
.cassette-btn { overflow: hidden; perspective: 180px; }
.cassette { position: relative; display: block; width: 29px; height: 22px; border: 1px solid var(--retro-bezel-edge); border-radius: 3px; background: var(--cassette-shell); box-shadow: inset 0 1px #ffffff4d, 0 1px 2px #0004; }
.cassette__label { position: absolute; inset: 2px 3px 6px; display: flex; align-items: center; justify-content: space-around; border-radius: 1px; background: var(--cassette-label); }
.cassette__label i { display: block; width: 5px; height: 5px; border: 1px solid var(--cassette-shell); border-radius: 50%; background: var(--cassette-window); }
.cassette__window { position: absolute; left: 6px; right: 6px; bottom: 2px; height: 3px; border-radius: 1px; background: linear-gradient(90deg, #ffbf35 0 20%, #ff763d 20% 40%, #ed466d 40% 60%, #bb51ce 60% 80%, #8264e6 80%); }
.cassette-btn.is-inserting .cassette { animation: cassette-flip .65s ease-in-out both; }
/* Mirroring the reverse face at the edge keeps the lettering and colour bars legible. */
@keyframes cassette-flip {
  0% { transform: rotateY(0deg); }
  49.99% { transform: rotateY(90deg); }
  50% { transform: rotateY(90deg) scaleX(-1); }
  100% { transform: rotateY(180deg) scaleX(-1); }
}
@media (prefers-reduced-motion: reduce) { .cassette-btn.is-inserting .cassette { animation: none; } }
@media (prefers-reduced-motion: reduce) { .channel__key { transition: none; } }

/* ───────── Back to top: a VCR key ───────── */

.back-top {
  width: 50px; height: 46px; border-radius: 10px; gap: 0; align-content: center;
  background: var(--retro-key); border: 1px solid var(--retro-key-edge); color: var(--retro-key-text);
  box-shadow: 0 3px 0 var(--retro-key-drop), 0 8px 20px -10px #0000008c, inset 0 1px 0 #ffffff1c;
}
/* Keep the return key beside the transport at the beginning of the tape, dimmed until it can rewind. */
.back-top:not(.is-visible) { visibility: visible; opacity: .28; transform: none; }
.back-top:hover { background: var(--retro-key); border-color: var(--retro-lit-edge); color: var(--retro-lit); box-shadow: 0 3px 0 var(--retro-key-drop), 0 0 14px -4px var(--retro-lit-glow), inset 0 1px 0 #ffffff1c; }
.back-top:active { translate: 0 2px; box-shadow: 0 1px 0 var(--retro-key-drop); }
.back-top svg { width: 16px; height: 16px; fill: currentColor; stroke: none; }
.back-top__label { font: 400 .95rem/.8 "VT323", var(--font-mono); letter-spacing: .12em; }

/* ───────── On-screen display ───────── */

.osd {
  position: fixed; z-index: 90; top: calc(var(--nav-height, 64px) + clamp(16px, 4vh, 40px)); left: clamp(16px, 4vw, 56px);
  display: grid; gap: .1em; pointer-events: none; opacity: 0; transition: opacity .18s;
  font: 400 clamp(2.2rem, 4.6vw, 3.4rem)/.9 "VT323", var(--font-mono); letter-spacing: .06em; color: #f4f4f0;
  text-shadow: 2px 0 #ff2a5a8c, -2px 0 #ffbf358c, 0 0 2px #000, 0 2px 0 #0009, 0 0 18px #ffffff40;
}
.osd.is-on { opacity: 1; animation: osd-jitter 1.2s steps(1, end) infinite; }
.osd__sub { font-size: .55em; letter-spacing: .12em; }
.osd__sub:empty { display: none; }
@keyframes osd-jitter { 0%, 100% { translate: 0 0; } 20% { translate: 1px 0; } 40% { translate: 0 1px; } 70% { translate: -1px 0; } }

/* Tracking noise is visible only during a seek, above the page but below the OSD. */
:is(.tape-transport, .footer-tape-seek) { position: fixed; inset: 0; z-index: 89; overflow: hidden; opacity: 0; pointer-events: none; transition: opacity .14s; }
:is(.tape-transport, .footer-tape-seek).is-on { opacity: 1; }
:is(.tape-transport, .footer-tape-seek)::before { content: ""; position: absolute; inset: 0; opacity: .23; background: repeating-linear-gradient(to bottom, transparent 0 3px, #080913 3px 4px); }
.tape-transport__band { position: absolute; top: -20vh; left: 0; width: 100%; height: 9vh; opacity: 0; background: linear-gradient(to bottom, transparent 0%, #ffffff20 32%, #ffffff8c 43%, #ffbf3566 47%, #ff79bd5c 51%, #15122875 54%, #ffffff52 59%, transparent 100%); }
:is(.tape-transport, .footer-tape-seek).is-on .tape-transport__band { opacity: .7; animation: tape-tracking 1.1s linear infinite; }
:is(.tape-transport, .footer-tape-seek).is-on .tape-transport__band--second { animation-duration: 1.7s; animation-delay: -.85s; opacity: .42; }
.tape-transport__band--major { height: 18vh; background: linear-gradient(to bottom, transparent 0%, #ffffff14 22%, #ffffff8c 35%, #ffbe6370 39%, #17101ab3 43%, #ffffffc0 46%, #ff719870 49%, #130c1b9c 53%, #ffffff59 57%, transparent 78%); }
:is(.tape-transport, .footer-tape-seek).is-on .tape-transport__band--major { animation-duration: 2.3s; animation-delay: -.3s; opacity: .82; }
:is(.tape-transport, .footer-tape-seek)[data-direction="back"] .tape-transport__band { animation-direction: reverse; }
@keyframes tape-tracking { to { transform: translateY(140vh); } }
@media (prefers-reduced-motion: reduce) { :is(.tape-transport, .footer-tape-seek) { display: none; } }

/* ───────── Static between channels ───────── */

.vhs { position: fixed; inset: 0; z-index: 95; pointer-events: none; opacity: 0; transition: opacity .12s; overflow: hidden; }
.vhs.is-on { opacity: .42; }
.vhs canvas { width: 100%; height: 100%; image-rendering: pixelated; }
.vhs::after { content: ""; position: absolute; inset: 0; background: repeating-linear-gradient(#0000 0 2px, #00000059 2px 3px); }
.vhs__band { position: absolute; left: 0; right: 0; top: -20%; height: 16%; background: linear-gradient(#fff0, #ffffffa6 45%, #fff 50%, #ffffffa6 55%, #fff0); filter: blur(1px); }
.vhs.is-on .vhs__band { animation: vhs-roll .42s linear; }
@keyframes vhs-roll { to { top: 105%; } }

/* ───────── AV: the whole page on a worn CRT ───────── */

/* The glass over everything: rounded corners with a black surround, a dark rim, scanlines and an RGB
   phosphor mask fixed to the screen while the page scrolls beneath, flicker, grain and a slow refresh band. */
.crt-screen {
  position: fixed; inset: 0; z-index: 93; pointer-events: none; display: none;
  border-radius: 26px / 34px;
  box-shadow: 0 0 0 100vmax #000, inset 0 0 6px 2px #000c, inset 0 0 110px 18px #000a;
}
html.is-crt {
  --fringe-a: color-mix(in srgb, var(--accent-3) 60%, transparent);
  --fringe-b: color-mix(in srgb, var(--accent) 50%, transparent);
}
html.is-crt .crt-screen { display: block; }
.crt-screen::before {
  content: ""; position: absolute; inset: 0; border-radius: inherit;
  background:
    radial-gradient(ellipse at 50% 50%, transparent 58%, #0000006b 100%),
    linear-gradient(#12101000 50%, #00000047 50%),
    linear-gradient(90deg, #ff00000f, #00ff0008, #0000ff0f);
  background-size: 100% 100%, 100% 3px, 3px 100%;
}
.crt-screen::after { content: ""; position: absolute; inset: 0; background: #1210101f; opacity: 0; animation: crt-flicker .15s infinite; }
.crt-screen__grain { position: absolute; inset: -64px; background: var(--grain); opacity: .07; animation: crt-grain .6s steps(6, end) infinite; }
.crt-screen__roll {
  position: absolute; left: 0; right: 0; top: -25vh; height: 22vh;
  background: linear-gradient(#fff0, #ffffff0d 45%, #ffffff14 50%, #ffffff0d 55%, #fff0);
  animation: crt-roll 7.5s linear infinite;
}
/* A colour fringe on the text, in the site's accents, shifting a little a few times a second. */
html.is-crt body { animation: crt-fringe 2.4s steps(1, end) infinite; }
@keyframes crt-fringe {
  0%   { text-shadow: .5px 0 1px var(--fringe-a), -.5px 0 1px var(--fringe-b), 0 0 3px #ffffff1f; }
  20%  { text-shadow: .9px 0 1px var(--fringe-a), -.9px 0 1px var(--fringe-b), 0 0 3px #ffffff1f; }
  35%  { text-shadow: .3px 0 1px var(--fringe-a), -.3px 0 1px var(--fringe-b), 0 0 3px #ffffff1f; }
  55%  { text-shadow: 1.4px 0 1px var(--fringe-a), -1.1px 0 1px var(--fringe-b), 0 0 3px #ffffff1f; }
  60%  { text-shadow: .6px 0 1px var(--fringe-a), -.6px 0 1px var(--fringe-b), 0 0 3px #ffffff1f; }
  80%  { text-shadow: .4px 0 1px var(--fringe-a), -.7px 0 1px var(--fringe-b), 0 0 3px #ffffff1f; }
}
@keyframes crt-flicker {
  0% { opacity: .28; } 10% { opacity: .12; } 20% { opacity: .55; } 30% { opacity: .2; } 40% { opacity: .42; }
  50% { opacity: .1; } 60% { opacity: .5; } 70% { opacity: .18; } 80% { opacity: .38; } 90% { opacity: .24; } 100% { opacity: .46; }
}
@keyframes crt-grain {
  0% { translate: 0 0; } 17% { translate: -22px 14px; } 33% { translate: 18px -30px; }
  50% { translate: -34px -8px; } 67% { translate: 26px 22px; } 83% { translate: -10px 34px; } 100% { translate: 0 0; }
}
@keyframes crt-roll { to { transform: translateY(150vh); } }
@media (prefers-reduced-motion: reduce) {
  .crt-screen::after, .crt-screen__grain, .crt-screen__roll, html.is-crt body { animation: none; }
  html.is-crt body { text-shadow: .5px 0 1px var(--fringe-a), -.5px 0 1px var(--fringe-b); }
}

/* ───────── The theme switch as a CRT: shutters fold the picture, a bright line closes to a dot ───────── */

.crt { position: fixed; inset: 0; z-index: 96; pointer-events: none; display: none; }
.crt.is-on { display: block; }
.crt__shutter { position: absolute; left: 0; right: 0; height: 50.5%; background: #05040c; transform: scaleY(0); }
.crt__shutter--top { top: 0; transform-origin: top; }
.crt__shutter--bottom { bottom: 0; transform-origin: bottom; }
.crt__line {
  position: absolute; left: 0; right: 0; top: 50%; height: 2px; margin-top: -1px; opacity: 0; background: #fff;
  box-shadow: 0 0 10px 2px #ffffffd9, 0 0 36px 8px #b4c8ff80;
}

/* A paused VHS is a held picture with unstable tracking, not a frozen web page. */
.tape-still { position: fixed; inset: 0; z-index: 89; overflow: hidden; pointer-events: none; opacity: 0; transition: opacity .25s; }
.tape-still.is-on { opacity: 1; }
.tape-still::before { content: ""; position: absolute; inset: 0; background: repeating-linear-gradient(to bottom, #ffffff08 0 1px, transparent 1px 3px, #08071122 3px 4px); }
.tape-still::after { content: ""; position: absolute; inset: 0; box-shadow: inset 0 0 80px #100b1a38; background: linear-gradient(90deg, #ff9c3b09, transparent 4%, transparent 96%, #d94ab50b); }
.tape-still__tracking { position: absolute; left: -2%; width: 104%; top: 65%; height: 56px; opacity: .65; background: repeating-linear-gradient(to bottom, transparent 0 3px, #fff5 3px 4px, #100e2240 4px 6px, transparent 6px 9px, #ffae6150 9px 10px, transparent 10px 14px); mask-image: linear-gradient(transparent, #000 30%, #000 65%, transparent); }
.tape-still__noise { position: absolute; inset: 0; opacity: .16; background: repeating-linear-gradient(to bottom, transparent 0 33px, #fff3 34px, transparent 35px 90px, #160c2755 91px, transparent 93px 143px); }
.tape-still.is-on .tape-still__tracking { animation: tape-still-track 3.4s steps(1, end) infinite; }
.tape-still.is-on .tape-still__noise { animation: tape-still-noise .8s steps(1, end) infinite; }
@keyframes tape-still-track { 0%, 100% { transform: translate(0, 0); } 8% { transform: translate(-4px, 3px); } 9% { transform: translate(2px, -2px); } 35% { transform: translate(0, 5px); } 36% { transform: translate(5px, 2px); } 70% { transform: translate(0, -3px); } 71% { transform: translate(-3px, 0); } }
@keyframes tape-still-noise { 0%, 100% { transform: translateY(0); } 25% { transform: translateY(3px); } 50% { transform: translateY(-2px); } 75% { transform: translateY(1px); } }
@media (prefers-reduced-motion: reduce) {
  .tape-still.is-on .tape-still__tracking, .tape-still.is-on .tape-still__noise { animation: none; }
  .tape-still { transition: none; }
}
