/* ============================================
   CRT.CSS — CRT monitor simulation effects
   Scanlines, phosphor glow, curvature,
   flicker, chromatic aberration, burn-in
   ============================================ */

/* --- CRT Wrapper --- */
.crt-wrapper {
  position: relative;
  width: 100%;
  max-width: var(--terminal-max-width);
  height: 100vh;
  overflow: hidden;
}

/* --- Scanlines Overlay --- */
.crt-scanlines {
  position: absolute;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  pointer-events: none;
  z-index: 10;
  background: repeating-linear-gradient(
    0deg,
    transparent,
    transparent 3px,
    rgba(0, 0, 0, 0.12) 3px,
    rgba(0, 0, 0, 0.12) 4px
  );
  mix-blend-mode: multiply;
}

/* --- Chromatic Aberration Border --- */
.crt-aberration {
  position: absolute;
  top: -1px;
  left: -1px;
  right: -1px;
  bottom: -1px;
  pointer-events: none;
  z-index: 11;
  border: 1px solid transparent;
  box-shadow:
    inset 1px 0 0 rgba(255, 0, 0, 0.06),
    inset -1px 0 0 rgba(0, 0, 255, 0.06),
    inset 0 1px 0 rgba(255, 0, 0, 0.04),
    inset 0 -1px 0 rgba(0, 0, 255, 0.04);
}

/* --- Screen Curvature (optional) --- */
.crt-wrapper.crt-curved {
  border-radius: 12px;
  overflow: hidden;
}

.crt-wrapper.crt-curved .terminal {
  /* Slight barrel distortion simulation */
  border-radius: 12px;
}

.crt-wrapper.crt-curved::before {
  content: '';
  position: absolute;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  pointer-events: none;
  z-index: 12;
  border-radius: 12px;
  box-shadow:
    inset 0 0 80px rgba(0, 0, 0, 0.4),
    inset 0 0 160px rgba(0, 0, 0, 0.15);
}

/* --- Phosphor Glow Enhancement --- */
/* Base glow is in main.css via text-shadow; this adds ambient screen glow */
.crt-wrapper::after {
  content: '';
  position: absolute;
  top: 50%;
  left: 50%;
  width: 120%;
  height: 120%;
  transform: translate(-50%, -50%);
  pointer-events: none;
  z-index: -1;
  background: radial-gradient(
    ellipse at center,
    var(--glow-primary) 0%,
    transparent 70%
  );
  opacity: 0.03;
}

/* --- Screen Flicker (rare, subtle) --- */
@keyframes crt-flicker {
  0%, 97%, 100% {
    opacity: 1;
  }
  97.5% {
    opacity: 0.92;
  }
  98% {
    opacity: 0.97;
  }
  98.5% {
    opacity: 0.94;
  }
}

.crt-wrapper.crt-flicker-enabled {
  animation: crt-flicker 8s infinite;
}

/* Intensified flicker for crisis/high self-destruction */
@keyframes crt-flicker-intense {
  0%, 90%, 100% {
    opacity: 1;
  }
  91% {
    opacity: 0.88;
  }
  92% {
    opacity: 0.95;
  }
  93% {
    opacity: 0.85;
  }
  94% {
    opacity: 0.98;
  }
  95% {
    opacity: 0.90;
  }
}

.crt-wrapper.crt-flicker-intense {
  animation: crt-flicker-intense 4s infinite;
}

/* --- Screen Pulse (crisis moments) --- */
@keyframes crt-pulse {
  0%, 100% {
    filter: brightness(1);
  }
  50% {
    filter: brightness(1.08);
  }
}

.crt-wrapper.crt-pulse {
  animation: crt-pulse 2s ease-in-out infinite;
}

/* --- Red Tint (high self-destruction state) --- */
.crt-wrapper.crt-red-tint .terminal {
  background: #0f0a0a;
}

.crt-wrapper.crt-red-tint .crt-scanlines {
  background: repeating-linear-gradient(
    0deg,
    transparent,
    transparent 3px,
    rgba(40, 0, 0, 0.15) 3px,
    rgba(40, 0, 0, 0.15) 4px
  );
}

/* --- Chromatic Aberration Intensified --- */
.crt-wrapper.crt-aberration-intense .crt-aberration {
  box-shadow:
    inset 2px 0 0 rgba(255, 0, 0, 0.12),
    inset -2px 0 0 rgba(0, 0, 255, 0.12),
    inset 0 2px 0 rgba(255, 0, 0, 0.08),
    inset 0 -2px 0 rgba(0, 0, 255, 0.08);
}

/* --- Dim/Fade (Burn Out ending) --- */
@keyframes crt-dim {
  from { filter: brightness(1); }
  to { filter: brightness(0.4); }
}

.crt-wrapper.crt-dim {
  animation: crt-dim 5s ease-in forwards;
}

/* --- Washed Out (Burn Slow ending) --- */
.crt-wrapper.crt-washed {
  filter: contrast(0.6) brightness(0.7);
}

/* --- State classes for JS to toggle --- */
/*
  .crt-flicker-enabled   — subtle flicker (default on)
  .crt-flicker-intense   — heavy flicker (high self-destruction)
  .crt-curved            — screen curvature (optional)
  .crt-pulse             — screen pulse (crisis)
  .crt-red-tint          — red-shifted background (high self-destruction)
  .crt-aberration-intense — stronger chromatic aberration (crisis)
  .crt-dim               — fade to dark (ending)
  .crt-washed            — washed out (ending)
*/

/* --- VHS/Glitch Line (decorative, triggered by JS) --- */
@keyframes glitch-line {
  0% {
    top: -2px;
    opacity: 0;
  }
  5% {
    opacity: 0.8;
  }
  10% {
    top: 20%;
    opacity: 0.6;
  }
  50% {
    top: 60%;
    opacity: 0.4;
  }
  90% {
    top: 95%;
    opacity: 0.2;
  }
  100% {
    top: 100%;
    opacity: 0;
  }
}

.crt-glitch-line {
  position: absolute;
  left: 0;
  right: 0;
  height: 2px;
  background: var(--text-primary);
  opacity: 0;
  pointer-events: none;
  z-index: 13;
  mix-blend-mode: screen;
}

.crt-glitch-line.active {
  animation: glitch-line 0.6s linear forwards;
}

/* --- No CRT Effects (accessibility toggle) --- */
.no-crt .crt-scanlines,
.no-crt .crt-aberration,
.no-crt .crt-glitch-line {
  display: none !important;
}

.no-crt .crt-wrapper {
  animation: none !important;
  filter: none !important;
}

.no-crt .crt-wrapper::after {
  display: none;
}

.no-crt .crt-wrapper::before {
  display: none;
}

.no-crt .game-text,
.no-crt .narrator-text {
  text-shadow: none !important;
}

/* --- Respect prefers-reduced-motion --- */
@media (prefers-reduced-motion: reduce) {
  .crt-wrapper {
    animation: none !important;
  }

  .crt-scanlines {
    /* Keep scanlines as static texture, that's fine */
  }

  .crt-glitch-line {
    display: none !important;
  }

  .cursor-blink {
    animation: none;
    opacity: 1;
  }
}
