/* The Road to Darkkeep - CRT presentation.
   Restrained: no flicker, no distortion. Readability first. */

:root {
  --bg: #040704;
  --fg: #44ff80;
  --fg-dim: #25a850;
  --fg-bright: #b8ffcc;
  --glow: rgba(68, 255, 128, 0.35);
  --line: rgba(68, 255, 128, 0.35);
  --font: ui-monospace, "Cascadia Mono", "SF Mono", Consolas, "Liberation Mono", "Courier New", monospace;
  --size: clamp(16px, 1.2vw + 11px, 21px);
  --scene-w: 256px;
}

html[data-phosphor="amber"] {
  --bg: #080500;
  --fg: #ffb62e;
  --fg-dim: #b27c14;
  --fg-bright: #ffe2a0;
  --glow: rgba(255, 182, 46, 0.35);
  --line: rgba(255, 182, 46, 0.35);
}

*, *::before, *::after { box-sizing: border-box; }

html, body {
  margin: 0;
  height: 100%;
  background: var(--bg);
  color: var(--fg);
  font-family: var(--font);
  font-size: var(--size);
  line-height: 1.45;
}

.sr-only {
  position: absolute;
  width: 1px; height: 1px;
  margin: -1px; padding: 0;
  overflow: hidden;
  clip: rect(0 0 0 0);
  white-space: nowrap;
  border: 0;
}

/* ---- screen ---- */

.crt {
  position: relative;
  height: 100dvh;
  display: flex;
  justify-content: center;
  background: radial-gradient(ellipse at center, #0b130c 0%, var(--bg) 75%);
  overflow: hidden;
}

html[data-phosphor="amber"] .crt {
  background: radial-gradient(ellipse at center, #140d03 0%, var(--bg) 75%);
}

/* Scanlines: a fixed, faint overlay. Never intercepts input. */
html[data-scanlines="on"] .crt::after {
  content: "";
  position: absolute;
  inset: 0;
  pointer-events: none;
  background: repeating-linear-gradient(to bottom, rgba(0, 0, 0, 0.16) 0, rgba(0, 0, 0, 0.16) 1px, transparent 1px, transparent 3px);
  z-index: 5;
}

.screen {
  width: min(100%, 86ch);
  height: 100%;
  display: grid;
  grid-template-rows: auto minmax(0, 1fr) auto auto auto;
  gap: 0.5rem;
  /* Respect device notches and rounded corners. */
  padding: max(0.75rem, env(safe-area-inset-top)) max(1rem, env(safe-area-inset-right))
           max(0.75rem, env(safe-area-inset-bottom)) max(1rem, env(safe-area-inset-left));
  text-shadow: 0 0 5px var(--glow);
}

/* ---- scene ---- */

.scene {
  margin: 0 auto;
  width: var(--scene-w);
  max-width: 100%;
  aspect-ratio: 256 / 160;
  border: 1px solid var(--line);
  background: #000;
  position: relative;
}

.scene-img {
  display: block;
  width: 100%;
  height: 100%;
  image-rendering: pixelated;
  image-rendering: crisp-edges;
}

/* Shown when illustrations are missing or fail to load. */
.scene-fallback {
  position: absolute;
  inset: 0;
  display: grid;
  place-items: center;
  padding: 0.5rem;
  text-align: center;
  color: var(--fg-dim);
  text-transform: uppercase;
  letter-spacing: 0.15em;
  font-size: 0.8em;
  background:
    repeating-conic-gradient(rgba(255, 255, 255, 0.05) 0% 25%, transparent 0% 50%) 0 0 / 4px 4px;
}

.scene-fallback[hidden], .scene[hidden], .scene-img[hidden] { display: none; }

/* Integer scales only (256 -> 512 -> 768) so pixels stay square and crisp. */
@media (min-width: 640px) { :root { --scene-w: 512px; } }
@media (min-width: 1000px) and (min-height: 900px) { :root { --scene-w: 768px; } }

/* Short landscape screens: text matters more than the picture. */
@media (max-height: 520px) { .scene { display: none; } }

/* ---- log ---- */

.log {
  overflow-y: auto;
  overscroll-behavior: contain;
  padding-right: 0.25rem;
  scrollbar-width: thin;
  scrollbar-color: var(--fg-dim) transparent;
}

.log:focus-visible, .screen button:focus-visible {
  outline: 2px solid var(--fg);
  outline-offset: 2px;
}

.line {
  margin: 0;
  min-height: 1.45em;
  white-space: pre-wrap;
  overflow-wrap: anywhere;
}

.line.title  { color: var(--fg-bright); letter-spacing: 0.08em; margin-top: 0.35em; }
.line.echo   { color: var(--fg-dim); }
.line.system { color: var(--fg-dim); }
.line.items  { color: var(--fg-bright); }
.line.exits  { color: var(--fg-dim); }
.line.error  { color: var(--fg-bright); }
.line.error::before { content: "! "; }

/* ---- exits / quick commands ---- */

.exits {
  display: flex;
  flex-wrap: wrap;
  gap: 0.4rem;
  min-height: 2.6rem;
}

.exits[hidden] { display: none; }

.exits button, .toggle {
  font: inherit;
  font-size: 0.85em;
  color: var(--fg);
  background: transparent;
  border: 1px solid var(--line);
  padding: 0.35rem 0.7rem;
  min-height: 2.6rem;           /* comfortable touch target */
  min-width: 2.6rem;
  text-transform: uppercase;
  letter-spacing: 0.06em;
  cursor: pointer;
  text-shadow: inherit;
}

.exits button:hover, .toggle:hover { background: rgba(255, 255, 255, 0.07); }
.exits button:active { background: var(--fg); color: var(--bg); text-shadow: none; }
.exits .quick { color: var(--fg-dim); border-style: dashed; }
.exits .spacer { flex: 1; }

/* ---- prompt with custom block cursor ---- */

.prompt {
  position: relative;
  display: flex;
  gap: 0.6ch;
  align-items: flex-start;
  border-top: 1px solid var(--line);
  padding-top: 0.5rem;
  min-height: 2.4rem;
}

.prompt-mark { color: var(--fg-bright); }

.typed {
  flex: 1;
  white-space: pre-wrap;
  overflow-wrap: anywhere;
  min-height: 1.45em;
}

.cursor {
  display: inline-block;
  width: 0.62em;
  height: 1.1em;
  vertical-align: text-bottom;
  background: var(--fg);
  box-shadow: 0 0 6px var(--glow);
  animation: blink 1.05s steps(1, end) infinite;
}

html[data-motion="reduced"] .cursor { animation: none; }

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

/* The real input sits invisibly over the line: it receives focus, typing, IME and mobile keyboards.
   16px minimum avoids iOS zoom-on-focus. */
.cmd {
  position: absolute;
  inset: 0;
  width: 100%;
  height: 100%;
  opacity: 0;
  font: inherit;
  font-size: max(16px, 1em);
  color: transparent;
  caret-color: transparent;
  background: transparent;
  border: 0;
  padding: 0;
}

/* ---- settings ---- */

.settings {
  display: flex;
  flex-wrap: wrap;
  gap: 0.4rem;
  justify-content: flex-end;
  padding-top: 0.25rem;
}

.toggle { font-size: 0.7em; min-height: 2.2rem; color: var(--fg-dim); border-color: rgba(255, 255, 255, 0.15); }
.toggle[aria-pressed="true"] { color: var(--fg); border-color: var(--line); }
.toggle[aria-pressed="true"]::before { content: "[x] "; }
.toggle[aria-pressed="false"]::before { content: "[ ] "; }

.noscript { padding: 1rem; color: var(--fg); font-family: var(--font); }

/* 404 page: a short passage of text, no game. */
.notfound { grid-template-rows: minmax(0, 1fr); align-content: center; }
.notfound .log { overflow: visible; }
.notfound a { color: var(--fg-bright); }
.notfound a:hover { background: var(--fg); color: var(--bg); text-shadow: none; }

@media (max-width: 480px) {
  .screen {
    padding: max(0.5rem, env(safe-area-inset-top)) max(0.6rem, env(safe-area-inset-right))
             max(0.5rem, env(safe-area-inset-bottom)) max(0.6rem, env(safe-area-inset-left));
    gap: 0.35rem;
  }
}

/* Windows High Contrast and similar: drop effects, use system colours, keep borders visible. */
@media (forced-colors: active) {
  .crt::after { display: none; }
  .screen { text-shadow: none; }
  .cursor { background: CanvasText; box-shadow: none; }
  .scene, .exits button, .toggle, .prompt { border-color: CanvasText; }
  .scene-fallback { background: Canvas; color: CanvasText; }
}

@media (prefers-reduced-motion: reduce) {
  html[data-motion="full"] .cursor { animation: none; }
}
