/*! CRT effect adapted from Alec Lownes (aleclownes.com), Lucas Bebber's CodePen and Andre Staltz's hyperpunk. */
/* CRT effect. Requires styles/tokens.css. It is an effect toggle, not a theme toggle.
   Technique adapted from aleclownes.com/2017/02/01/crt-display.html.
   Markup:
   <div class="crt-overlay" aria-hidden="true"></div>
   the menu bar's CRT icon button ([data-crt-toggle], "CRT effect", aria-pressed; markup in src/shell.html, styles in
   components/os-menubar.css). Add components/crt.js; it toggles body.crt-on and saves the choice.
   Strength is tuned per theme with --crt-scan / --crt-flicker / --crt-split-a / --crt-split-b. */

.crt-overlay {
  position: fixed;
  inset: 0;
  z-index: var(--z-crt);
  display: none;
  pointer-events: none;
}

body.crt-on .crt-overlay {
  display: block;
}

/* Screen-door: scanlines + RGB stripes */
.crt-overlay::before {
  content: "";
  position: absolute;
  inset: 0;
  background:
    linear-gradient(rgba(18, 16, 16, 0) 50%, var(--crt-scan) 50%),
    var(--crt-stripes, linear-gradient(90deg, rgba(255, 0, 0, 0.06), rgba(0, 255, 0, 0.02), rgba(0, 0, 255, 0.06)));
  background-size: 100% 2px, 3px 100%;
}

/* Flicker: gentle. Opacity stays between 0.7 and 1 of a faint overlay, over seconds, no flashes */
.crt-overlay::after {
  content: "";
  position: absolute;
  inset: 0;
  background: var(--crt-flicker);
  opacity: 1;
  animation: crt-flicker 4s linear infinite;
}

@keyframes crt-flicker {
  0%   { opacity: 0.9; }
  12%  { opacity: 0.8; }
  27%  { opacity: 1; }
  41%  { opacity: 0.7; }
  58%  { opacity: 0.95; }
  73%  { opacity: 0.75; }
  88%  { opacity: 1; }
  100% { opacity: 0.9; }
}

/* Color separation: headings, nav labels, window titles, buttons only. Never paragraphs. */
body.crt-on :is(h1, h2, h3, h4, .display, .panel__title, .navbtn, .tab, .win__title, .btn) {
  animation: crt-split 3s ease-in-out infinite;
}

@keyframes crt-split {
  0%, 100% { text-shadow: -1px 0 var(--crt-split-a), 1px 0 var(--crt-split-b); }
  50%      { text-shadow: -1.5px 0.3px var(--crt-split-a), 1.5px -0.3px var(--crt-split-b); }
}

/* Reduced motion: static scanlines only, even if toggled on */
@media (prefers-reduced-motion: reduce) {
  .crt-overlay::after { display: none; }
  body.crt-on :is(h1, h2, h3, h4, .display, .panel__title, .navbtn, .tab, .win__title, .btn) {
    animation: none;
    text-shadow: none;
  }
}
