/* ---------------------------------------------------------------------------
   The Epistemic Arcade — Refined Monochrome CRT
   --------------------------------------------------------------------------- */

:root {
  /* Backgrounds */
  --page-bg: #111111;
  --canvas-bg: #000000;

  /* Typography */
  --text-main: #e0e0e0;
  --text-dim: #8a8a8a;
  --accent-green: #39ff14;

  /* Tetris blocks (VGA Neon) */
  --t-cyan: #00ffff;
  --t-magenta: #ff00ff;
  --t-yellow: #ffff00;
  --t-blue: #0000ff;

  /* Fonts */
  --font-body: "IBM Plex Mono", "Courier Prime", monospace;
  --font-retro: "VT323", "Press Start 2P", monospace;

  /* Layout */
  --col-prose: 32rem;
  --col-visual: 32rem;
  --gutter: 4rem;
}

* {
  box-sizing: border-box;
}

html,
body {
  margin: 0;
  padding: 0;
  background: var(--page-bg);
  color: var(--text-main);
  font-family: var(--font-body);
  font-size: 16px;
  line-height: 1.65;
}

a {
  color: var(--accent-green);
}

code {
  font-family: var(--font-body);
  font-size: 0.9em;
  background: rgba(255, 255, 255, 0.05);
  padding: 0 0.3em;
  border-radius: 2px;
}

/* ---------------------------------------------------------------------------
   Loading state
   --------------------------------------------------------------------------- */

.loading {
  position: fixed;
  inset: 0;
  display: flex;
  align-items: center;
  justify-content: center;
  font-family: var(--font-retro);
  font-size: 2rem;
  color: var(--accent-green);
  background: var(--page-bg);
  z-index: 100;
  animation: flicker 1.6s infinite;
}

.loading.hidden {
  display: none;
}

@keyframes flicker {
  0%,
  100% {
    opacity: 1;
  }
  45% {
    opacity: 0.6;
  }
  50% {
    opacity: 1;
  }
  55% {
    opacity: 0.4;
  }
  60% {
    opacity: 1;
  }
}

/* ---------------------------------------------------------------------------
   Layout: prose column + sticky visual column
   --------------------------------------------------------------------------- */

.layout {
  display: grid;
  grid-template-columns: var(--col-prose) var(--col-visual);
  grid-template-rows: auto 1fr;
  grid-template-areas:
    "masthead visual"
    "prose    visual";
  column-gap: var(--gutter);
  max-width: calc(var(--col-prose) + var(--col-visual) + var(--gutter) + 4rem);
  margin: 0 auto;
  padding: 0 2rem;
}

.masthead {
  grid-area: masthead;
}

.prose {
  grid-area: prose;
  padding-bottom: 6rem;
}

.visual {
  grid-area: visual;
  position: sticky;
  top: 48px; /* clear the fixed brand bar */
  height: calc(100vh - 48px);
  display: flex;
  align-items: center;
  justify-content: center;
}

/* ---------------------------------------------------------------------------
   Prose typography
   --------------------------------------------------------------------------- */

.masthead {
  padding-top: 4rem;
  padding-bottom: 4rem;
  border-bottom: 1px solid #333;
  margin-bottom: 4rem;
}

.masthead h1 {
  font-family: var(--font-retro);
  font-size: 4rem;
  line-height: 1;
  color: var(--accent-green);
  margin: 0 0 0.5rem 0;
  letter-spacing: 0.02em;
  text-shadow:
    0 0 8px rgba(57, 255, 20, 0.6),
    0 0 24px rgba(57, 255, 20, 0.25);
}

.masthead .byline {
  color: var(--text-dim);
  font-size: 1rem;
  margin: 0;
}

.step {
  min-height: 90vh;
  padding: 4rem 0;
  border-bottom: 1px dashed #2a2a2a;
}

.step h2 {
  font-family: var(--font-retro);
  font-size: 2.4rem;
  line-height: 1.1;
  color: var(--accent-green);
  margin: 0 0 1.5rem 0;
  text-shadow: 0 0 6px rgba(57, 255, 20, 0.4);
}

.step p {
  margin: 0 0 1.2rem 0;
}

.step strong {
  color: #fff;
}

.step em {
  color: var(--t-cyan);
  font-style: normal;
}

.step .caption {
  font-size: 0.85rem;
  color: var(--text-dim);
  border-left: 2px solid var(--accent-green);
  padding-left: 0.8rem;
  margin-top: 2rem;
}

.phosphor {
  color: var(--accent-green);
  text-shadow: 0 0 4px rgba(57, 255, 20, 0.5);
}

.colophon {
  margin-top: 6rem;
  padding-top: 2rem;
  border-top: 1px solid #333;
  color: var(--text-dim);
  font-size: 0.85rem;
}

/* ---------------------------------------------------------------------------
   Visual stack: canvases + chart inside a CRT-style frame
   --------------------------------------------------------------------------- */

.visual-frame {
  position: relative;
  width: 100%;
  max-width: 600px;
  aspect-ratio: 5 / 4;
  max-height: 100%; /* never taller than the (sticky) .visual container */
  background: var(--canvas-bg);
  border: 2px solid #222;
  border-radius: 14px;
  overflow: hidden;
  box-shadow:
    0 0 0 1px #000,
    0 0 28px rgba(57, 255, 20, 0.06) inset,
    0 8px 32px rgba(0, 0, 0, 0.6);
}

.canvas-stack {
  position: absolute;
  inset: 0;
  display: flex;
  align-items: center;
  justify-content: center;
}

.agent-pair {
  display: flex;
  gap: 2.5rem;
  align-items: center;
  justify-content: center;
  width: 100%;
  height: 100%;
  padding: 1rem 1rem 2rem;
  opacity: 1;
  transition: opacity 0.5s ease;
}

.agent-pair.hidden {
  opacity: 0;
  pointer-events: none;
}

/* CRT-reboot transition: 400ms opacity blink while a new checkpoint loads. */
.agent-pair.fading {
  opacity: 0;
  transition: opacity 0.4s ease;
}

.agent-cell {
  margin: 0;
  flex: 1 1 0;
  min-width: 0;
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 0.6rem;
}

.agent-cell canvas {
  /* width/height are set in px by setupCanvas (contain-fit, responsive). */
  background: var(--canvas-bg);
  image-rendering: pixelated;
  border: 1px solid #1d1d1d;
}

.agent-cell figcaption {
  font-family: var(--font-retro);
  font-size: 1.1rem;
  color: var(--accent-green);
  letter-spacing: 0.05em;
  text-shadow: 0 0 4px rgba(57, 255, 20, 0.4);
}

.agent-cell[data-agent="alpha"] figcaption::before {
  content: "";
}

.chart-wrap {
  position: absolute;
  inset: 0;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 2rem;
  opacity: 0;
  transition: opacity 0.5s ease;
  pointer-events: none;
}

.chart-wrap.visible {
  opacity: 1;
  pointer-events: auto;
}

#chart {
  max-width: 100%;
  max-height: 100%;
}

.insert-coin {
  position: absolute;
  right: 12%;
  top: 50%;
  transform: translateY(-50%);
  font-family: var(--font-retro);
  font-size: 1.8rem;
  color: var(--accent-green);
  letter-spacing: 0.1em;
  text-shadow:
    0 0 8px rgba(57, 255, 20, 0.6),
    0 0 18px rgba(57, 255, 20, 0.25);
  opacity: 0;
  transition: opacity 0.4s ease;
  animation: blink 1.2s steps(1, end) infinite;
  pointer-events: none;
  white-space: nowrap;
}

.insert-coin.visible {
  opacity: 1;
}

@keyframes blink {
  50% {
    opacity: 0.15;
  }
}

.loading-checkpoint {
  position: absolute;
  left: 50%;
  top: 50%;
  transform: translate(-50%, -50%);
  font-family: var(--font-retro);
  font-size: 1.8rem;
  letter-spacing: 0.12em;
  color: var(--accent-green);
  text-shadow:
    0 0 8px rgba(57, 255, 20, 0.7),
    0 0 22px rgba(57, 255, 20, 0.35),
    0 0 36px rgba(57, 255, 20, 0.15);
  opacity: 0;
  transition: opacity 0.2s ease;
  pointer-events: none;
  white-space: nowrap;
  z-index: 4;
  animation: flicker 1.6s infinite;
}

.loading-checkpoint.visible {
  opacity: 1;
}

.epoch-badge {
  position: absolute;
  top: 0.6rem;
  left: 0.8rem;
  font-family: var(--font-retro);
  font-size: 1rem;
  color: var(--text-dim);
  letter-spacing: 0.06em;
  opacity: 0.7;
  pointer-events: none;
}

/* ---------------------------------------------------------------------------
   Scanlines overlay (per spec)
   --------------------------------------------------------------------------- */

.scanlines {
  position: absolute;
  inset: 0;
  pointer-events: none;
  background:
    linear-gradient(rgba(18, 16, 16, 0) 50%, rgba(0, 0, 0, 0.25) 50%),
    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%;
  mix-blend-mode: screen;
  z-index: 5;
}

/* ---------------------------------------------------------------------------
   Reduced motion
   --------------------------------------------------------------------------- */

@media (prefers-reduced-motion: reduce) {
  .loading,
  .insert-coin,
  .loading-checkpoint {
    animation: none;
  }
}

/* ---------------------------------------------------------------------------
   Mobile fallback
   --------------------------------------------------------------------------- */

@media (max-width: 960px) {
  .layout {
    grid-template-columns: 1fr;
    grid-template-rows: auto auto auto;
    grid-template-areas:
      "masthead"
      "visual"
      "prose";
    column-gap: 0;
    max-width: none;
    padding: 0 1.25rem;
  }

  /* Sticky (not relative): the visual pins below the brand bar while the
     text steps scroll past it — the scrollytelling actually works. */
  .visual {
    position: sticky;
    top: 56px; /* 48px brand bar + breathing room */
    height: 60vh;
    height: 60dvh; /* dvh tracks the mobile dynamic toolbar */
    margin: 0 0 2rem;
  }

  .masthead h1 {
    font-size: 2.8rem;
  }

  /* Taller frame so the two side-by-side boards get more room; max-height on
     .visual-frame still keeps it inside the sticky .visual on short screens. */
  .visual-frame {
    aspect-ratio: 4 / 5;
  }

  .step {
    min-height: 70vh;
  }

  .agent-pair {
    gap: 1rem;
    padding: 0.75rem 0.75rem 1.5rem;
  }
}

/* ---------------------------------------------------------------------------
   Brand bar — site nav (matches the Aporia essays)
   --------------------------------------------------------------------------- */

body {
  padding-top: 48px;
}

.brand-bar {
  position: fixed;
  top: 0;
  left: 0;
  right: 0;
  height: 48px;
  background: rgba(17, 17, 17, 0.9);
  backdrop-filter: blur(6px);
  -webkit-backdrop-filter: blur(6px);
  border-bottom: 1px solid #333;
  display: flex;
  align-items: center;
  padding: 0 24px;
  z-index: 300;
}

.brand-link {
  display: inline-flex;
  align-items: center;
  opacity: 0.85;
  transition: opacity 0.2s ease;
}
.brand-link:hover {
  opacity: 1;
}

.brand-logo {
  height: 28px;
  width: auto;
  display: block;
}

.brand-nav {
  margin-left: auto;
  display: flex;
  align-items: center;
}
.brand-nav a {
  color: var(--text-dim);
  font-family: var(--font-body);
  font-size: 0.72rem;
  text-transform: uppercase;
  letter-spacing: 0.12em;
  text-decoration: none;
  margin-left: 22px;
  transition: color 0.2s ease;
}
.brand-nav a:hover {
  color: var(--accent-green);
}

.nav-toggle {
  display: none;
  margin-left: auto;
  background: none;
  border: none;
  cursor: pointer;
  padding: 8px;
  flex-direction: column;
  gap: 5px;
}
.nav-toggle span {
  display: block;
  width: 22px;
  height: 2px;
  background: var(--text-dim);
}

@media (max-width: 768px) {
  .nav-toggle {
    display: flex;
  }
  .brand-nav {
    display: none;
    position: absolute;
    top: 48px;
    left: 0;
    right: 0;
    background: rgba(17, 17, 17, 0.98);
    backdrop-filter: blur(6px);
    -webkit-backdrop-filter: blur(6px);
    border-bottom: 1px solid #333;
    flex-direction: column;
    align-items: flex-start;
    padding: 16px 24px;
    gap: 16px;
  }
  .brand-nav.open {
    display: flex;
  }
  .brand-nav a {
    margin-left: 0;
  }
}
