/* ============================================================
   CRAZY — crt.css
   The cathode-ray shell: bezel, curvature, scanlines, phosphor
   glow, vignette, and a SLOW, photosensitivity-safe flicker.
   CSS-first (no WebGL) per the architecture council: cheap on
   the GPU, smooth on low-end machines, degrades gracefully.
   ============================================================ */

.crt {
  position: fixed;
  top: 0;
  left: 0;
  width: 100%;
  height: 100vh;
  height: 100dvh; /* shrink with the mobile on-screen keyboard so the input stays visible */
  display: grid;
  place-items: center;
  background:
    radial-gradient(120% 120% at 50% 50%, #0a1014 0%, #02050a 60%, #000 100%);
  padding: clamp(0px, 2vmin, 28px);
  /* The whole screen can shake under stress. */
  transform: translate(
    calc(var(--shake) * 1px * var(--shake-x, 0)),
    calc(var(--shake) * 1px * var(--shake-y, 0))
  );
}

/* The glass: where everything is drawn. */
.crt__screen {
  position: relative;
  width: min(100%, var(--screen-max));
  height: min(100%, 1100px);
  max-height: 100%;
  background: var(--bg-deep);
  border-radius: 18px / 14px;
  overflow: hidden;
  /* Subtle barrel illusion: inner shadow + curved highlight. */
  box-shadow:
    inset 0 0 80px 30px rgba(0, 0, 0, 0.9),
    inset 0 0 8px 2px color-mix(in srgb, var(--glow-color) 18%, transparent),
    0 0 40px 4px rgba(0, 0, 0, 0.8);
  isolation: isolate;
}

/* The actual content surface. */
.crt__content {
  position: absolute;
  inset: 0;
  padding: var(--pad);
  padding-bottom: calc(var(--pad) + env(safe-area-inset-bottom, 0px));
  display: flex;
  flex-direction: column;
  z-index: 1;
}

/* ---- Overlay: scanlines ---- */
.crt__screen::before {
  content: "";
  position: absolute;
  inset: 0;
  z-index: 5;
  pointer-events: none;
  /* Council fix #2: no mix-blend-mode full-screen pass. The line
     darkness is baked straight into the gradient instead. */
  background: repeating-linear-gradient(
    to bottom,
    rgba(0, 0, 0, 0) 0px,
    rgba(0, 0, 0, 0) 2px,
    rgba(0, 0, 0, calc(var(--scanline-opacity) * 1.6)) 3px,
    rgba(0, 0, 0, calc(var(--scanline-opacity) * 1.6)) 3px
  );
  background-size: 100% 3px;
}

/* ---- Overlay: vignette + phosphor bloom + a single drifting
   scan band (slow, low-contrast → photosensitivity-safe) ---- */
.crt__screen::after {
  content: "";
  position: absolute;
  inset: 0;
  z-index: 6;
  pointer-events: none;
  background:
    radial-gradient(
      120% 120% at 50% 50%,
      transparent 55%,
      rgba(0, 0, 0, calc(var(--vignette) * 0.7)) 100%
    ),
    linear-gradient(
      to bottom,
      color-mix(in srgb, var(--glow-color) 6%, transparent),
      transparent 12%,
      transparent 88%,
      color-mix(in srgb, var(--glow-color) 6%, transparent)
    );
}

/* The slow roll/flicker layer — deliberately gentle. */
.crt__flicker {
  position: absolute;
  inset: 0;
  z-index: 7;
  pointer-events: none;
  /* Council fix #2: no screen blend. A faint glow-tinted veil whose
     opacity pulses gently instead. */
  background: radial-gradient(
    120% 120% at 50% 45%,
    color-mix(in srgb, var(--glow-color) 22%, transparent),
    transparent 70%
  );
  opacity: 0;
  animation: crt-flicker 7s ease-in-out infinite;
}
@keyframes crt-flicker {
  0%, 100% { opacity: 0; }
  48% { opacity: 0; }
  50% { opacity: calc(var(--flicker-opacity) * 1.6); }
  52% { opacity: 0; }
  78% { opacity: 0; }
  80% { opacity: var(--flicker-opacity); }
  82% { opacity: 0; }
}

/* A slow scan band that drifts down the glass once in a while. */
.crt__scanband {
  position: absolute;
  left: 0;
  right: 0;
  height: 26%;
  z-index: 7;
  pointer-events: none;
  background: linear-gradient(
    to bottom,
    transparent,
    color-mix(in srgb, var(--glow-color) 7%, transparent),
    transparent
  );
  opacity: calc(0.4 + 0.4 * var(--instability));
  animation: crt-scanband 9s linear infinite;
}
@keyframes crt-scanband {
  0% { transform: translateY(-30%); }
  100% { transform: translateY(420%); }
}

/* The transcript scrolls; the input line is pinned at the bottom. */
.transcript {
  flex: 1 1 auto;
  overflow-y: auto;
  overflow-x: hidden;
  padding-right: 6px;
  scroll-behavior: smooth;
  -webkit-overflow-scrolling: touch;
  /* c1 (GME mobile-keyboard council): bottom-anchor so the newest line always
     rests just above the input — short conversation or long. Fixes the mobile
     keyboard stranding "Hello." off the top of the glass. */
  display: flex;
  flex-direction: column;
  justify-content: flex-end;
  min-height: 0; /* lets the column actually scroll inside the flex parent */
}

/* Soft fade at the top edge of the transcript. */
.transcript::before {
  content: "";
  position: sticky;
  top: 0;
  display: block;
  height: 26px;
  margin-bottom: -26px;
  background: linear-gradient(var(--bg-deep), transparent);
  pointer-events: none;
  z-index: 2;
  /* c1: keep the fade pinned to the top under justify-content:flex-end */
  order: -1;
  flex: 0 0 auto;
}

/* ============================================================
   PHOTOSENSITIVITY / MOTION SAFETY
   Default is already gentle. reduce-motion or .safe-mode strips
   every animated CRT/glitch effect to a calm, static screen.
   This is a hard accessibility requirement (Ian Hamilton).
   ============================================================ */
@media (prefers-reduced-motion: reduce) {
  .crt__flicker,
  .crt__scanband,
  .cursor,
  .fray-cursor {
    animation: none !important;
  }
  .crt__flicker { opacity: 0 !important; }
  .crt { transform: none !important; }
  /* c1: keyboard re-pins snap instantly, never animated, for motion-sensitive players */
  .transcript { scroll-behavior: auto; }
}

body.safe-mode .crt__flicker,
body.safe-mode .crt__scanband {
  animation: none !important;
  opacity: 0 !important;
}
body.safe-mode .crt {
  transform: none !important;
}
body.safe-mode {
  --shake: 0 !important;
  --flicker-opacity: 0 !important;
}
