:root {
  --bg: #0b0706;
  --osd: #f4f1e6;
  --amber: #ffb547;
  --ember: #ff7a2e;
  --ink: rgba(8, 5, 4, 0.78);
  --line: rgba(255, 181, 71, 0.35);
  --px: 4px; /* one scene pixel on screen, set from JS */
  font-family: "VT323", ui-monospace, monospace;
  color-scheme: dark;
}

* { box-sizing: border-box; }

html, body {
  margin: 0;
  height: 100%;
  background: var(--bg);
  color: var(--osd);
  overflow: hidden;
  -webkit-font-smoothing: none;
}

body.idle { cursor: none; }

/* ---------- stage ---------- */
#stage {
  position: fixed;
  inset: 0;
  overflow: hidden;
  transform-origin: 50% 50%;
}

#scene {
  position: absolute;
  left: 0;
  top: 0;
  image-rendering: pixelated;
  image-rendering: crisp-edges;
}

#scene.stoke { cursor: pointer; }

#crt {
  position: absolute;
  inset: 0;
  pointer-events: none;
  background:
    repeating-linear-gradient(to bottom, transparent 0, transparent calc(var(--px) * 0.62), rgba(0, 0, 0, 0.28) calc(var(--px) * 0.62), rgba(0, 0, 0, 0.28) var(--px)),
    radial-gradient(ellipse at 50% 50%, transparent 50%, rgba(0, 0, 0, 0.4) 80%, rgba(0, 0, 0, 0.75) 100%);
  background-position: 0 var(--scan-offset, 0);
  animation: flicker 6s infinite steps(1);
}

body.no-crt #crt { display: none; }
/* Slow devices: keep every overlay static so the compositor only redraws the scene. */
body.lite #crt { animation: none; }

@keyframes flicker {
  0%, 100% { opacity: 1; }
  41% { opacity: 0.93; }
  42% { opacity: 1; }
  77% { opacity: 0.96; }
  78% { opacity: 1; }
}

/* VHS pause: a rolling band of tracking noise */
#tracking {
  position: absolute;
  inset: 0;
  pointer-events: none;
  opacity: 0;
}

body.paused #tracking {
  opacity: 1;
  background:
    linear-gradient(to bottom, transparent 0%, transparent 70%, rgba(255, 255, 255, 0.06) 72%, rgba(255, 255, 255, 0.18) 74%, rgba(0, 0, 0, 0.35) 76%, transparent 80%);
  background-size: 100% 200%;
  animation: tracking 3.5s linear infinite;
}

body.paused #scene { filter: saturate(0.7) contrast(1.05); }

@keyframes tracking {
  from { background-position: 0 0; }
  to { background-position: 0 -200%; }
}

/* Falling asleep: the room slowly dims as the fire burns down (an overlay, not
   a filter, so the scene isn't re-filtered every frame for a minute) */
#stage::after {
  content: "";
  position: absolute;
  inset: 0;
  pointer-events: none;
  background: #000;
  opacity: 0;
  transition: opacity 60s ease-in;
}
body.asleep #stage::after { opacity: 0.45; }

/* CRT power-on */
body.power-on #stage { animation: power-on 700ms cubic-bezier(0.2, 0.8, 0.2, 1) both; }

@keyframes power-on {
  0% { transform: scale(1, 0.004); filter: brightness(4); }
  45% { transform: scale(1, 0.004); filter: brightness(4); }
  70% { transform: scale(1, 1.04); filter: brightness(1.6); }
  100% { transform: scale(1, 1); filter: brightness(1); }
}

/* ---------- OSD (VCR on-screen display) ---------- */
#osd { position: fixed; inset: 0; pointer-events: none; }

.osd {
  position: absolute;
  font-size: clamp(22px, 3.4vmin, 44px);
  line-height: 1;
  letter-spacing: 0.04em;
  color: var(--osd);
  text-shadow: 2px 2px 0 #000, -1px -1px 0 #000, 1px -1px 0 #000, -1px 1px 0 #000;
  transition: opacity 400ms steps(3);
}

#osd-mode { top: max(4vmin, env(safe-area-inset-top)); left: max(5vmin, env(safe-area-inset-left)); opacity: 0; }
#osd-counter { top: max(4vmin, env(safe-area-inset-top)); right: max(5vmin, env(safe-area-inset-right)); opacity: 0; }
#osd-toast { top: max(4vmin, env(safe-area-inset-top)); left: 50%; transform: translateX(-50%); opacity: 0; white-space: nowrap; }

.stamp {
  bottom: max(4vmin, env(safe-area-inset-bottom));
  left: max(5vmin, env(safe-area-inset-left));
  display: flex;
  flex-direction: column;
  color: var(--amber);
  opacity: 0.85;
  font-size: clamp(20px, 3vmin, 38px);
}

body.show-osd #osd-mode,
body.show-osd #osd-counter,
body.paused #osd-mode { opacity: 1; }

body.paused #osd-mode { animation: blink 1s steps(1) infinite; }
body.hide-osd #osd { display: none; }
body.booting #osd { visibility: hidden; }

#osd-toast.on { opacity: 1; }

#osd-track {
  bottom: max(4vmin, env(safe-area-inset-bottom));
  right: max(5vmin, env(safe-area-inset-right));
  font-size: clamp(20px, 3vmin, 38px);
  opacity: 0;
}

#osd-track.on, body.show-osd #osd-track { opacity: 0.9; }

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

/* ---------- insert tape card ---------- */
#insert {
  position: fixed;
  inset: 0;
  display: grid;
  place-content: center;
  justify-items: center;
  text-align: center;
  gap: 1.2rem;
  padding: 16px;
  background: radial-gradient(ellipse at 60% 60%, rgba(11, 7, 6, 0.35), rgba(11, 7, 6, 0.85) 70%);
  transition: opacity 500ms steps(4), visibility 0s 500ms;
}

body:not(.booting) #insert { opacity: 0; visibility: hidden; }

#title {
  margin: 0;
  font-family: "Jacquard 24", "VT323", serif;
  font-weight: 400;
  font-size: clamp(56px, 12vmin, 150px);
  line-height: 0.9;
  color: #ffe2b0;
  text-shadow: 0 0 0.25em rgba(255, 122, 46, 0.55), 0.06em 0.06em 0 #2a0f06;
}

.tagline {
  margin: 0;
  font-size: clamp(20px, 3vmin, 30px);
  color: #e8cfa8;
  opacity: 0.85;
}

.fineprint {
  margin: 0;
  font-size: clamp(16px, 2.2vmin, 22px);
  color: #b9a58a;
  opacity: 0.7;
}

.press {
  margin-top: 1rem;
  font: inherit;
  font-size: clamp(28px, 5vmin, 52px);
  color: var(--osd);
  background: none;
  border: 0;
  padding: 0.2em 0.6em;
  cursor: pointer;
  text-shadow: 3px 3px 0 #000;
  animation: blink 1.1s steps(1) infinite;
}

.press:hover, .press:focus-visible { color: var(--amber); animation: none; outline: none; }

/* ---------- deck (controls) ---------- */
#deck {
  position: fixed;
  left: 50%;
  bottom: max(3vmin, env(safe-area-inset-bottom));
  transform: translate(-50%, 0);
  display: flex;
  align-items: center;
  gap: 10px;
  max-width: calc(100vw - 32px);
  padding: 8px 10px;
  background: var(--ink);
  border: 2px solid var(--line);
  box-shadow: 4px 4px 0 rgba(0, 0, 0, 0.6), inset 0 0 0 2px rgba(0, 0, 0, 0.6);
  font-size: 22px;
  transition: opacity 300ms steps(3), transform 300ms steps(3);
}

body.idle #deck, body.booting #deck { opacity: 0; transform: translate(-50%, 12px); pointer-events: none; }

.faders { display: flex; gap: 12px; }

.fader {
  display: grid;
  grid-template-columns: auto 90px;
  align-items: center;
  gap: 6px;
  color: #e8cfa8;
  letter-spacing: 0.05em;
}

.fader em {
  display: none;
  font-style: normal;
  color: #7c6a58;
  font-size: 18px;
}

.fader.off input { display: none; }
.fader.off em { display: block; }
.fader.off span { color: #7c6a58; }

.key {
  font: inherit;
  color: var(--osd);
  background: #1d1410;
  border: 2px solid #4a3a2c;
  border-bottom-color: #0a0605;
  border-right-color: #0a0605;
  padding: 2px 10px 0;
  min-height: 32px;
  cursor: pointer;
  white-space: nowrap;
}

.key:hover:not(:disabled) { color: var(--amber); border-color: var(--line); }
.key:disabled { color: #7c6a58; cursor: default; }
.key:active { border-color: #0a0605 #4a3a2c #4a3a2c #0a0605; transform: translate(1px, 1px); }
.key:focus-visible, input:focus-visible { outline: 2px solid var(--amber); outline-offset: 2px; }
.key[aria-pressed="true"] { color: var(--amber); }

input[type="range"] {
  -webkit-appearance: none;
  appearance: none;
  width: 90px;
  height: 20px;
  margin: 0;
  background: transparent;
  cursor: pointer;
}

input[type="range"]::-webkit-slider-runnable-track {
  height: 6px;
  background: linear-gradient(to right, var(--ember) var(--fill, 70%), #3a2a20 var(--fill, 70%));
  box-shadow: 0 2px 0 #000;
}

input[type="range"]::-moz-range-track { height: 6px; background: #3a2a20; box-shadow: 0 2px 0 #000; }
input[type="range"]::-moz-range-progress { height: 6px; background: var(--ember); }

input[type="range"]::-webkit-slider-thumb {
  -webkit-appearance: none;
  width: 10px;
  height: 18px;
  margin-top: -6px;
  background: #f4e6cc;
  border: 0;
  box-shadow: 2px 2px 0 #000, inset -2px -2px 0 #b89f7c;
}

input[type="range"]::-moz-range-thumb {
  width: 10px;
  height: 18px;
  border: 0;
  border-radius: 0;
  background: #f4e6cc;
  box-shadow: 2px 2px 0 #000, inset -2px -2px 0 #b89f7c;
}

/* ---------- help ---------- */
#help {
  position: fixed;
  right: 16px;
  bottom: calc(max(3vmin, env(safe-area-inset-bottom)) + 64px);
  padding: 14px 18px;
  background: var(--ink);
  border: 2px solid var(--line);
  box-shadow: 4px 4px 0 rgba(0, 0, 0, 0.6);
  font-size: 22px;
}

#help h2 { margin: 0 0 8px; font-weight: 400; font-size: 26px; color: var(--amber); }
#help dl { display: grid; grid-template-columns: auto auto; gap: 2px 18px; margin: 0; }
#help dt { color: var(--osd); }
#help dd { margin: 0; color: #c9b597; }

/* ---------- small screens ---------- */
@media (max-width: 720px) {
  #deck { flex-wrap: wrap; justify-content: center; font-size: 20px; }
  .faders { order: -1; width: 100%; justify-content: space-between; }
  .fader { grid-template-columns: auto 1fr; }
  .fader input { width: 100%; min-width: 50px; }
  #btn-help { display: none; }
}

@media (prefers-reduced-motion: reduce) {
  #crt, .press, body.paused #osd-mode, body.paused #tracking { animation: none; }
  body.power-on #stage { animation: none; }
  #stage::after { transition: none; }
}

/* ?debug performance readout */
#perf {
  position: fixed;
  top: 8px;
  left: 50%;
  transform: translateX(-50%);
  padding: 2px 8px;
  background: rgba(0, 0, 0, 0.7);
  color: #9f9;
  font-size: 20px;
  white-space: pre;
  pointer-events: none;
  z-index: 10;
}
