/* =====================================================================
   jdub-sandbox — green phosphor terminal
   Two phases:
   1. Boot: full CRT show — flicker, scanlines, glow, curvature
   2. Terminal: restrained — slight glow, subtle scanlines, no flicker
   ===================================================================== */

:root {
  --bg: #050807;
  --bg-deep: #020403;
  --fg: #6cf38a;             /* primary green phosphor */
  --fg-bright: #b6ffce;      /* highlight */
  --fg-mute: #2f6b3f;        /* dim */
  --fg-dim: #1a3f25;         /* very dim, for chrome */
  --accent: #ffe28a;         /* amber accent for echoed prompts */
  --error: #ff8b6c;
  --link: #8effa6;
  --mono: "Berkeley Mono", "JetBrains Mono", "IBM Plex Mono", "Menlo", ui-monospace, monospace;
  --line: 1.55;
  --col: 84ch;
}

:root[data-theme="amber"] {
  --fg: #ffc266;
  --fg-bright: #ffe6b6;
  --fg-mute: #8a5a1f;
  --fg-dim: #3f2a0e;
  --accent: #ffd58a;
  --link: #ffd28a;
}

* { box-sizing: border-box; }
html, body { margin: 0; padding: 0; }

html {
  background: var(--bg-deep);
  color: var(--fg);
  font-family: var(--mono);
  font-size: 15px;
  line-height: var(--line);
}

body {
  min-height: 100vh;
  background: var(--bg);
  color: var(--fg);
  position: relative;
  overflow-x: hidden;
}

/* ---------- CRT layers (always-on but subtler post-boot) ---------- */

body::before,
body::after {
  content: "";
  position: fixed;
  inset: 0;
  pointer-events: none;
  z-index: 1000;
}

/* scanlines */
body::before {
  background: repeating-linear-gradient(
    to bottom,
    rgba(0, 0, 0, 0) 0,
    rgba(0, 0, 0, 0) 2px,
    rgba(0, 0, 0, 0.18) 3px,
    rgba(0, 0, 0, 0.18) 3px
  );
  opacity: 0.9;
  mix-blend-mode: multiply;
}

/* vignette + tint */
body::after {
  background:
    radial-gradient(ellipse at center, rgba(0, 0, 0, 0) 55%, rgba(0, 0, 0, 0.55) 100%),
    radial-gradient(ellipse at center, rgba(108, 243, 138, 0.04) 0%, rgba(108, 243, 138, 0) 60%);
  opacity: 1;
}

/* During boot, push CRT effects up: flicker, stronger scanlines, slight curvature */
body:not(.post-boot) {
  animation: crt-flicker 5s steps(1, end) infinite;
}
body:not(.post-boot)::before {
  background: repeating-linear-gradient(
    to bottom,
    rgba(0, 0, 0, 0) 0,
    rgba(0, 0, 0, 0) 1px,
    rgba(0, 0, 0, 0.32) 2px,
    rgba(0, 0, 0, 0.32) 3px
  );
}
body:not(.post-boot)::after {
  background:
    radial-gradient(ellipse at center, rgba(0, 0, 0, 0) 50%, rgba(0, 0, 0, 0.7) 100%),
    radial-gradient(ellipse at center, rgba(108, 243, 138, 0.08) 0%, rgba(108, 243, 138, 0) 55%);
}

@keyframes crt-flicker {
  0%, 96%, 100% { opacity: 1; }
  97% { opacity: 0.86; }
  97.5% { opacity: 0.95; }
  98% { opacity: 0.78; }
  98.5% { opacity: 1; }
}

/* a slow horizontal "rolling bar" during boot only */
body:not(.post-boot) .crt-bar {
  position: fixed;
  inset-inline: 0;
  height: 30vh;
  pointer-events: none;
  z-index: 999;
  background: linear-gradient(
    to bottom,
    transparent 0%,
    rgba(108, 243, 138, 0.04) 40%,
    rgba(108, 243, 138, 0.07) 50%,
    rgba(108, 243, 138, 0.04) 60%,
    transparent 100%
  );
  animation: crt-roll 7s linear infinite;
}
body.post-boot .crt-bar { display: none; }

@keyframes crt-roll {
  from { transform: translateY(-40vh); }
  to   { transform: translateY(140vh); }
}

/* ---------- Boot stage ---------- */

#boot-stage {
  min-height: 100vh;
  padding: 6vh 6vw 12vh;
  white-space: pre-wrap;
  word-break: break-word;
  text-shadow:
    0 0 1px var(--fg),
    0 0 6px rgba(108, 243, 138, 0.55),
    0 0 18px rgba(108, 243, 138, 0.25);
  transition: opacity 0.7s ease, filter 0.7s ease;
}
:root[data-theme="amber"] #boot-stage {
  text-shadow:
    0 0 1px var(--fg),
    0 0 6px rgba(255, 194, 102, 0.55),
    0 0 18px rgba(255, 194, 102, 0.25);
}

#boot-stage.boot-fading {
  opacity: 0;
  filter: blur(2px);
}

#boot-stream {
  display: flex;
  flex-direction: column;
  gap: 0;
}

.boot-line {
  white-space: pre-wrap;
  min-height: 1.55em;
}
.boot-mute { color: var(--fg-mute); }
.boot-ok { color: var(--fg); }
.boot-static {
  color: var(--fg-dim);
  letter-spacing: 0.04em;
  display: block;
  word-break: break-all;
}
.boot-banner {
  color: var(--fg-bright);
  text-shadow:
    0 0 2px var(--fg-bright),
    0 0 12px rgba(108, 243, 138, 0.7),
    0 0 30px rgba(108, 243, 138, 0.4);
  letter-spacing: 0.02em;
  font-size: clamp(7px, 1.05vw, 13px);
  line-height: 1.05;
  white-space: pre;
  display: block;
}

#boot-skip {
  margin-top: 2em;
  color: var(--fg-mute);
  opacity: 0;
  transition: opacity 0.4s ease;
}
#boot-skip .blink {
  animation: blink 1.1s steps(2, end) infinite;
}
@keyframes blink {
  50% { opacity: 0; }
}

/* ---------- Terminal (post-boot) ---------- */

#term {
  display: none;
  max-width: var(--col);
  margin: 0 auto;
  padding: 4vh 4vw 30vh;
}
body.post-boot #term { display: block; }
body.post-boot #boot-stage { display: none; }

.term-head {
  color: var(--fg-mute);
  border-bottom: 1px dashed var(--fg-dim);
  padding-bottom: 0.6em;
  margin-bottom: 1em;
  display: flex;
  justify-content: space-between;
  gap: 2ch;
  flex-wrap: wrap;
  font-size: 0.92em;
}
.term-head .head-host { color: var(--fg); }
.term-head .head-cwd { color: var(--fg-mute); }

#term-out .term-line {
  white-space: pre-wrap;
  word-break: break-word;
}
.term-line { padding: 0.05em 0; }

pre.term-line {
  margin: 0.4em 0;
  font-family: var(--mono);
  font-size: 1em;
  line-height: var(--line);
  color: var(--fg);
}

.term-block {
  color: var(--fg);
}
.term-file {
  color: var(--fg-bright);
}
.term-mute { color: var(--fg-mute); }
.term-err  { color: var(--error); }
.term-ok   { color: var(--fg-bright); }

.term-echo .echo-cmd { color: var(--fg-bright); }
.prompt-user   { color: var(--fg-bright); font-weight: 600; }
.prompt-colon  { color: var(--fg-mute); }
.prompt-cwd    { color: var(--accent); }
.prompt-dollar { color: var(--fg); margin-right: 0.5ch; }

.ls-dir  { color: var(--fg-bright); font-weight: 600; }
.ls-file { color: var(--fg); }

.term-ls-grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(20ch, 1fr));
  gap: 0.1em 2ch;
  padding: 0.3em 0;
}

.grep-hit {
  background: rgba(108, 243, 138, 0.18);
  color: var(--fg-bright);
  padding: 0 1px;
}
:root[data-theme="amber"] .grep-hit {
  background: rgba(255, 194, 102, 0.22);
}
.grep-file { color: var(--accent); }

.term-matrix {
  color: var(--fg-bright);
  letter-spacing: 0.05em;
  line-height: 1.1;
  text-shadow: 0 0 6px rgba(108, 243, 138, 0.6);
}

/* matrix rain overlay */
#matrix-overlay {
  position: fixed;
  inset: 0;
  z-index: 2000;
  background: #050807;
  opacity: 1;
  transition: opacity 0.5s ease;
}
#matrix-overlay.matrix-fading { opacity: 0; }
#matrix-canvas {
  position: absolute;
  inset: 0;
  display: block;
}
#matrix-hint {
  position: absolute;
  bottom: 4vh;
  left: 0;
  right: 0;
  text-align: center;
  color: var(--fg-mute);
  font-family: var(--mono);
  font-size: 11px;
  letter-spacing: 0.18em;
  text-transform: uppercase;
  pointer-events: none;
  animation: blink 1.4s steps(2, end) infinite;
}

.term-fetch {
  color: var(--fg);
  line-height: 1.15;
}
.term-fetch .fetch-art { color: var(--fg-mute); }
.term-fetch .fetch-label { color: var(--fg-bright); font-weight: 600; }
.term-fetch .fetch-swatch { color: var(--fg-bright); letter-spacing: 0.05em; }

/* ---------- Active prompt line ---------- */

#term-prompt-line {
  display: flex;
  align-items: baseline;
  gap: 0.5ch;
  margin-top: 0.4em;
}

#term-prompt {
  white-space: nowrap;
  flex: 0 0 auto;
}

#term-input {
  flex: 1 1 auto;
  background: transparent;
  border: 0;
  outline: 0;
  color: var(--fg-bright);
  font: inherit;
  font-size: 1em;
  caret-color: transparent; /* we draw our own block cursor */
  padding: 0;
  width: 100%;
  caret-shape: block;
}

/* fake block cursor that follows the input */
#term-cursor {
  display: inline-block;
  width: 1ch;
  height: 1.1em;
  background: var(--fg);
  margin-left: -1ch;
  animation: blink 1.1s steps(2, end) infinite;
  position: relative;
  top: 0.15em;
  box-shadow: 0 0 6px rgba(108, 243, 138, 0.55);
}
:root[data-theme="amber"] #term-cursor {
  box-shadow: 0 0 6px rgba(255, 194, 102, 0.55);
}

/* Restrained terminal glow — subtle, not distracting */
body.post-boot {
  text-shadow: 0 0 1px rgba(108, 243, 138, 0.35);
}
:root[data-theme="amber"] body.post-boot {
  text-shadow: 0 0 1px rgba(255, 194, 102, 0.35);
}

/* selection */
::selection {
  background: var(--fg);
  color: var(--bg);
}

/* responsive type */
@media (max-width: 720px) {
  html { font-size: 13.5px; }
  #boot-stage { padding: 4vh 4vw 8vh; }
  #term { padding: 3vh 4vw 30vh; }
}

/* honor reduced motion */
@media (prefers-reduced-motion: reduce) {
  body:not(.post-boot) { animation: none; }
  body:not(.post-boot) .crt-bar { display: none; }
  #term-cursor, #boot-skip .blink { animation: none; }
}
