* {
  box-sizing: border-box;
}

html, body {
  margin: 0;
  padding: 0;
  width: 100%;
  height: 100%;
  overflow: hidden;
  background: #040405;
  color: #e5e7eb;
  font-family: "Courier New", Courier, monospace;
  user-select: none;
}

#viewport {
  position: fixed;
  inset: 0;
  z-index: 1;
  display: flex;
  align-items: center;
  justify-content: center;
  background: #000000;
}

#viewport canvas {
  display: block;
  width: min(100vw, calc(100vh * 560 / 384));
  height: min(100vh, calc(100vw * 384 / 560));
  image-rendering: pixelated;
  image-rendering: crisp-edges;
  background: #000000;
  box-shadow: 0 0 0 2px #1d2026, 0 24px 70px rgba(0, 0, 0, 0.9);
  cursor: default;
}

#loading-overlay {
  position: fixed;
  inset: 0;
  z-index: 90;
  display: flex;
  align-items: center;
  justify-content: center;
  background: #040405;
  color: #d8d5c9;
  font-size: 16px;
  letter-spacing: 0.08em;
  transition: opacity 0.35s ease;
}

#loading-overlay.is-done {
  opacity: 0;
  pointer-events: none;
}

/* Top-of-screen tractor-feed dot-matrix PaperPanel + TypewriterHead */
#typewriter-panel {
  position: fixed;
  top: 12px;
  left: 50%;
  transform: translate(-50%, -140%);
  width: min(820px, 92vw);
  z-index: 40;
  pointer-events: none;
  opacity: 0;
  transition: transform 0.24s ease, opacity 0.24s ease;
}

#typewriter-panel.is-visible {
  transform: translate(-50%, 0);
  opacity: 1;
}

.paper-sheet {
  position: relative;
  background: linear-gradient(180deg, #f3efe2 0%, #e7e1cf 100%);
  color: #1d1e20;
  border: 2px solid #9e9784;
  border-radius: 3px;
  padding: 16px 34px 12px;
  box-shadow: 0 12px 32px rgba(0, 0, 0, 0.55);
}

#typewriter-panel[data-phase="3"] .paper-sheet {
  background: linear-gradient(180deg, #ece8d5 0%, #dcd4b8 100%);
  border-color: #827958;
}

#typewriter-panel[data-phase="4"] .paper-sheet {
  background: linear-gradient(180deg, #1b1114 0%, #10090b 100%);
  color: #e6d7d9;
  border-color: #8f1c2e;
  box-shadow: 0 0 28px rgba(175, 22, 46, 0.35);
}

.tractor-holes {
  position: absolute;
  top: 6px;
  bottom: 6px;
  width: 14px;
  background-image: radial-gradient(circle, #333438 3px, transparent 3.6px);
  background-size: 14px 16px;
  opacity: 0.45;
}

.tractor-holes.left { left: 8px; }
.tractor-holes.right { right: 8px; }

.typewriter-head {
  position: absolute;
  top: -10px;
  left: 8%;
  width: 38px;
  height: 18px;
  transition: left 0.06s linear;
  pointer-events: none;
}

.head-bar {
  position: absolute;
  top: 7px;
  left: -400px;
  right: -400px;
  height: 3px;
  background: rgba(130, 136, 145, 0.55);
}

.head-block {
  position: relative;
  width: 34px;
  height: 16px;
  background: linear-gradient(180deg, #6b7280, #374151);
  border: 1px solid #1f2937;
  border-radius: 2px;
}

.paper-lines {
  display: flex;
  flex-direction: column;
  gap: 5px;
  min-height: 76px;
}

.paper-line {
  min-height: 22px;
  font-size: 15px;
  line-height: 22px;
  white-space: pre-wrap;
  word-break: break-word;
  font-weight: 600;
}

.typewriter-hint {
  margin-top: 6px;
  font-size: 11px;
  opacity: 0.62;
  text-align: right;
}

.tw-cursor {
  display: inline-block;
  animation: blink-cursor 0.85s steps(1) infinite;
}

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

/* Anomaly typography tokens (invisible until Phase >= 1) */
.hol-house {
  color: #2b6cb0;
  font-weight: 700;
}

#typewriter-panel[data-phase="4"] .hol-house {
  color: #5ca4f2;
}

.erased-build {
  color: #b91c1c;
  text-decoration: line-through;
}

.zalrog-token {
  color: #dc2626;
  font-weight: 800;
  text-shadow: 0 0 6px rgba(220, 38, 38, 0.45);
}

/* Subtle bottom-right corner strip */
.corner-pill {
  position: fixed;
  right: 14px;
  bottom: 14px;
  z-index: 30;
  display: flex;
  align-items: center;
  gap: 6px;
  background: rgba(18, 20, 24, 0.78);
  backdrop-filter: blur(6px);
  border: 1px solid rgba(255, 255, 255, 0.14);
  border-radius: 8px;
  padding: 6px 8px;
}

body[data-phase="4"] .corner-pill {
  border-color: rgba(190, 30, 50, 0.5);
  background: rgba(18, 8, 11, 0.88);
}

.floor-nav {
  display: none;
  align-items: center;
  gap: 4px;
  padding-right: 6px;
  border-right: 1px solid rgba(255, 255, 255, 0.14);
}

.floor-nav.is-visible {
  display: flex;
}

.floor-label {
  font-size: 12px;
  color: #d1d5db;
  min-width: 40px;
  text-align: center;
}

.pill-btn {
  background: rgba(255, 255, 255, 0.08);
  color: #e5e7eb;
  border: 1px solid rgba(255, 255, 255, 0.15);
  border-radius: 5px;
  padding: 5px 9px;
  font-family: inherit;
  font-size: 12px;
  cursor: pointer;
  transition: background 0.15s ease, border-color 0.15s ease;
}

.pill-btn:hover {
  background: rgba(255, 255, 255, 0.18);
}

.hover-caption {
  position: fixed;
  left: 14px;
  bottom: 14px;
  z-index: 29;
  pointer-events: none;
  background: rgba(14, 16, 20, 0.84);
  border: 1px solid rgba(255, 255, 255, 0.14);
  border-radius: 6px;
  padding: 5px 10px;
  font-size: 11.5px;
  color: #e5e7eb;
  max-width: min(460px, 46vw);
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
  opacity: 0;
  transform: translateY(4px);
  transition: opacity 0.12s ease, transform 0.12s ease;
}

.hover-caption.is-visible {
  opacity: 1;
  transform: translateY(0);
}

body[data-phase="4"] .hover-caption {
  border-color: rgba(190, 30, 50, 0.45);
  color: #f3c6cc;
}

@media (max-width: 760px) {
  .corner-pill {
    right: 8px;
    bottom: 8px;
    max-width: calc(100vw - 16px);
    flex-wrap: wrap;
    justify-content: flex-end;
  }
  .hover-caption {
    display: none;
  }
}

.hud-toast {
  position: fixed;
  left: 50%;
  bottom: 62px;
  transform: translate(-50%, 16px);
  z-index: 35;
  background: rgba(16, 18, 22, 0.9);
  border: 1px solid rgba(255, 255, 255, 0.18);
  border-radius: 6px;
  padding: 9px 16px;
  font-size: 13px;
  color: #f3f4f6;
  max-width: min(640px, 90vw);
  text-align: center;
  pointer-events: none;
  opacity: 0;
  transition: opacity 0.2s ease, transform 0.2s ease;
}

.hud-toast.is-visible {
  opacity: 1;
  transform: translate(-50%, 0);
}

/* Diegetic 2nd-Floor Apple IIe Green-Phosphor CRT Monitor */
.apple2-monitor-overlay {
  position: fixed;
  inset: 0;
  z-index: 75;
  display: flex;
  align-items: center;
  justify-content: center;
  background: rgba(0, 0, 0, 0.78);
  backdrop-filter: blur(3px);
}

.apple2-monitor-overlay[hidden] {
  display: none;
}

.apple2-crt-bezel {
  width: min(760px, 94vw);
  height: min(520px, 84vh);
  background: #b9b09a;
  border: 4px solid #6b6352;
  border-radius: 14px;
  padding: 14px 18px 18px;
  box-shadow: 0 28px 80px rgba(0, 0, 0, 0.92), inset 0 2px 6px rgba(255, 255, 255, 0.45);
  display: flex;
  flex-direction: column;
  gap: 10px;
}

.monitor-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  color: #29251e;
  font-size: 12px;
  font-weight: 800;
  letter-spacing: 0.08em;
}

.monitor-exit-btn {
  background: #3a342a;
  color: #e8e1ce;
  border: 1px solid #1e1b15;
  border-radius: 4px;
  padding: 4px 10px;
  font-family: inherit;
  font-size: 11px;
  font-weight: 700;
  cursor: pointer;
}

.apple2-crt-glass {
  flex: 1;
  min-height: 0;
  background: radial-gradient(circle at 50% 45%, #07220a 0%, #020b03 95%);
  border: 5px solid #1e211e;
  border-radius: 10px;
  padding: 14px 16px;
  display: flex;
  flex-direction: column;
  box-shadow: inset 0 0 36px rgba(0, 0, 0, 0.9), 0 0 18px rgba(36, 200, 36, 0.16);
  position: relative;
  overflow: hidden;
}

.apple2-crt-glass::after {
  content: '';
  position: absolute;
  inset: 0;
  pointer-events: none;
  background: repeating-linear-gradient(
    to bottom,
    rgba(0, 0, 0, 0) 0px,
    rgba(0, 0, 0, 0) 2px,
    rgba(0, 0, 0, 0.24) 2px,
    rgba(0, 0, 0, 0.24) 3px
  );
}

.monitor-output {
  flex: 1;
  margin: 0;
  overflow-y: auto;
  color: #48f558;
  text-shadow: 0 0 5px rgba(72, 245, 88, 0.4);
  font-family: "Courier New", Courier, monospace;
  font-size: 13.5px;
  line-height: 1.48;
  white-space: pre-wrap;
  word-break: break-word;
  user-select: text;
}

.monitor-prompt-row {
  display: flex;
  align-items: center;
  gap: 8px;
  padding-top: 8px;
  border-top: 1px solid rgba(72, 245, 88, 0.22);
}

.prompt-glyph {
  color: #48f558;
  font-weight: 800;
  font-size: 15px;
}

#monitor-input {
  flex: 1;
  background: transparent;
  border: none;
  outline: none;
  color: #84ff90;
  font-family: inherit;
  font-size: 14px;
  font-weight: 700;
  caret-color: #48f558;
}

#monitor-input::placeholder {
  color: rgba(72, 245, 88, 0.35);
}

.apple2-monitor-overlay.is-scare-flash .apple2-crt-bezel {
  animation: crt-scare-shake 0.12s steps(2) infinite;
  border-color: #c81c2c;
}

.apple2-monitor-overlay.is-scare-flash .apple2-crt-glass {
  background: radial-gradient(circle at 50% 45%, #3d040a 0%, #0d0103 95%);
  box-shadow: inset 0 0 42px rgba(200, 28, 44, 0.85), 0 0 28px rgba(200, 28, 44, 0.7);
}

.apple2-monitor-overlay.is-scare-flash .monitor-output,
.apple2-monitor-overlay.is-scare-flash #monitor-input,
.apple2-monitor-overlay.is-scare-flash .prompt-glyph {
  color: #ff4b5c;
  text-shadow: 0 0 8px rgba(255, 75, 92, 0.75);
}

@keyframes crt-scare-shake {
  0% { transform: translate(-4px, 2px); }
  50% { transform: translate(4px, -3px); }
  100% { transform: translate(-2px, -1px); }
}

#viewport.is-scare-jolt canvas {
  animation: viewport-scare-jolt 0.09s steps(2) infinite;
  box-shadow: 0 0 0 3px #c81c2c, 0 0 70px rgba(200, 28, 44, 0.85);
}

@keyframes viewport-scare-jolt {
  0% { transform: scale(1.065) translate(-7px, 5px); }
  50% { transform: scale(1.08) translate(6px, -6px); }
  100% { transform: scale(1.055) translate(-4px, -3px); }
}
