/* ============================================================
   PROJECT: ECHO — terminal look & feel
   Readability first; CRT effects are subtle and can be turned off
   (EFFECTS button) and are disabled for reduced-motion users.
   ============================================================ */

:root {
  --bg: #030604;
  --bg-raised: #07100a;
  --bg-hover: #0c1a10;
  --line: #173222;
  --fg: #8ccf9a;
  --fg-dim: #5a8a66;
  --fg-faint: #34533d;
  --bright: #cdf7d6;
  --amber: #e3b25c;
  --amber-dim: #9c7a3d;
  --error: #e08a70;
  --glow: rgba(140, 207, 154, 0.22);
  --font: 'Cascadia Mono', 'Cascadia Code', Consolas, 'SF Mono', Menlo, 'DejaVu Sans Mono', 'Liberation Mono', monospace;
  --text-size: 15px;
  --gutter: 16px;
  --panel-width: 300px;
  color-scheme: dark;
}

* { box-sizing: border-box; }

html, body {
  height: 100%;
  margin: 0;
}

body {
  background: var(--bg);
  color: var(--fg);
  font-family: var(--font);
  font-size: var(--text-size);
  line-height: 1.5;
  -webkit-font-smoothing: antialiased;
  overflow: hidden;
}

button { font: inherit; color: inherit; }

:focus-visible {
  outline: 1px solid var(--amber);
  outline-offset: 2px;
}

/* ---------- layout ---------- */

.app {
  display: flex;
  flex-direction: column;
  height: 100vh;
  height: 100dvh;
  position: relative;
}

.topbar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  padding: 10px var(--gutter);
  border-bottom: 1px solid var(--line);
  background: var(--bg);
  flex-shrink: 0;
}

.brand { display: flex; align-items: baseline; gap: 14px; min-width: 0; }
.brand-name { color: var(--bright); letter-spacing: 0.18em; font-weight: 600; white-space: nowrap; }
.brand-tag { color: var(--fg-faint); font-size: 12px; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }

.top-actions { display: flex; align-items: center; gap: 8px; flex-shrink: 0; }
.top-progress { color: var(--fg-dim); font-size: 12px; margin-right: 4px; }

.screen {
  flex: 1;
  display: flex;
  min-height: 0;
}

.terminal {
  flex: 1;
  display: flex;
  flex-direction: column;
  min-width: 0;
}

/* ---------- output ---------- */

.output {
  flex: 1;
  overflow-y: auto;
  padding: 18px var(--gutter) 12px;
  scroll-behavior: auto;
  overscroll-behavior: contain;
}

.line {
  white-space: pre-wrap;
  overflow-wrap: anywhere;
  min-height: 1.5em;
  max-width: 78ch;
}

body:not(.no-fx) .line { text-shadow: 0 0 6px var(--glow); }

.line.cmd { color: var(--bright); margin-top: 0.9em; }
.line.dim { color: var(--fg-dim); }
.line.faint { color: var(--fg-faint); }
.line.bright { color: var(--bright); }
.line.heading { color: var(--bright); letter-spacing: 0.06em; margin-top: 0.4em; }
.line.warn { color: var(--amber); }
.line.error { color: var(--error); }
.line.success { color: var(--bright); font-weight: 600; letter-spacing: 0.04em; }
.line.clue { color: var(--amber-dim); }
.line.hint { color: var(--fg); border-left: 2px solid var(--amber-dim); padding-left: 10px; }
.line.boot-title { color: var(--bright); letter-spacing: 0.08em; }
.line.boot { color: var(--fg); }
.line.file-head { color: var(--fg-faint); }
.line.file-body { color: var(--fg); padding: 2px 0; }
.line.lock-head { color: var(--amber); margin-top: 0.3em; }
.line.lock-line { color: var(--fg); padding-left: 1ch; }
.line.locked-line { color: var(--fg-dim); }
.line.cipher { color: var(--fg-dim); letter-spacing: 0.03em; }
.line.sector { color: var(--amber); border-left: 2px solid var(--amber-dim); padding-left: 10px; }
.line.fragment-line { color: var(--amber); letter-spacing: 0.04em; }
.line.new-files { color: var(--bright); }
.line.progress-line { color: var(--fg-dim); }
.line.center { text-align: center; max-width: none; }

.line.revelation {
  color: var(--bright);
  font-size: clamp(18px, 3.2vw, 28px);
  letter-spacing: 0.12em;
  line-height: 1.35;
  margin: 1em 0;
  max-width: none;
}
body:not(.no-fx) .line.revelation { text-shadow: 0 0 14px rgba(205, 247, 214, 0.45); }

.line.echo-voice { color: var(--amber); letter-spacing: 0.08em; }

.frag-mark {
  color: var(--amber);
  background: rgba(227, 178, 92, 0.08);
  padding: 0 3px;
}

.file-link {
  background: none;
  border: 0;
  padding: 0;
  color: var(--bright);
  text-decoration: underline;
  text-decoration-color: var(--fg-faint);
  text-underline-offset: 3px;
  cursor: pointer;
}
.file-link:hover { text-decoration-color: var(--bright); background: var(--bg-hover); }

.term-btn {
  background: none;
  border: 0;
  padding: 4px 0;
  color: var(--bright);
  cursor: pointer;
  letter-spacing: 0.04em;
}
.term-btn:hover { color: var(--amber); }

.type-cursor::after {
  content: '▮';
  color: var(--bright);
  margin-left: 1px;
}

/* dim everything except the revelation during the finale */
.output .line { transition: opacity 1.2s ease; }
body.finale .output .line:not(.revelation):not(.echo-voice) { opacity: 0.25; }

/* ---------- input ---------- */

.input-line {
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 10px var(--gutter) max(14px, env(safe-area-inset-bottom));
  border-top: 1px solid var(--line);
  margin-top: 8px;
  background: var(--bg);
}

.prompt { color: var(--bright); white-space: nowrap; }

.cmd-input {
  flex: 1;
  min-width: 0;
  background: transparent;
  border: 0;
  outline: none;
  color: var(--bright);
  font: inherit;
  font-size: max(16px, var(--text-size)); /* 16px+ stops iOS zooming in */
  caret-color: var(--bright);
  padding: 6px 0;
}
.cmd-input::placeholder { color: var(--fg-faint); }

.enter-btn {
  background: transparent;
  border: 1px solid var(--line);
  color: var(--fg-dim);
  width: 40px;
  height: 36px;
  cursor: pointer;
}
.enter-btn:hover { color: var(--bright); }

.shake { animation: shake 0.35s ease; }
@keyframes shake {
  20% { transform: translateX(-6px); }
  40% { transform: translateX(5px); }
  60% { transform: translateX(-3px); }
  80% { transform: translateX(2px); }
}

/* ---------- panel ---------- */

.panel {
  width: var(--panel-width);
  flex-shrink: 0;
  border-left: 1px solid var(--line);
  background: var(--bg-raised);
  overflow-y: auto;
  padding: 6px 0 20px;
  font-size: 13px;
}

.panel-section {
  padding: 12px 16px;
  border-bottom: 1px solid var(--line);
}
.panel-section:last-child { border-bottom: 0; }

.panel h3 {
  margin: 0 0 10px;
  font-size: 11px;
  font-weight: 600;
  letter-spacing: 0.2em;
  text-transform: uppercase;
  color: var(--fg-dim);
  display: flex;
  justify-content: space-between;
}
.count { color: var(--fg-faint); letter-spacing: 0.05em; }

.progress {
  height: 6px;
  background: var(--bg);
  border: 1px solid var(--line);
}
.progress-fill {
  height: 100%;
  width: 0;
  background: var(--fg);
  transition: width 0.6s ease;
}
.panel-note { margin: 8px 0 0; color: var(--fg-dim); }

.file-list, .clue-list, .fragment-slots { list-style: none; margin: 0; padding: 0; }

.file-item {
  display: flex;
  align-items: center;
  gap: 10px;
  width: 100%;
  background: none;
  border: 0;
  padding: 6px 6px;
  text-align: left;
  cursor: pointer;
  color: var(--fg);
  min-height: 34px;
}
.file-item:hover { background: var(--bg-hover); color: var(--bright); }
.file-item.is-new { color: var(--bright); }
.file-item.is-locked { color: var(--fg-dim); }
.file-glyph { width: 1ch; color: var(--fg-dim); flex-shrink: 0; }
.file-item.is-new .file-glyph { color: var(--amber); }
.file-name { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }

.panel-legend { display: flex; gap: 14px; margin: 8px 0 0; color: var(--fg-faint); font-size: 11px; }

.fragment-slots {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 6px;
  margin-bottom: 10px;
}
.fragment-slots li {
  border: 1px solid var(--line);
  padding: 6px 8px;
  font-size: 12px;
  letter-spacing: 0.05em;
  min-height: 32px;
}
.fragment-slots li.found { color: var(--amber); border-color: var(--amber-dim); background: rgba(227, 178, 92, 0.05); }
.fragment-slots li.missing { color: var(--fg-faint); }

.clue-list li {
  padding: 6px 0;
  border-bottom: 1px dashed var(--line);
  color: var(--fg);
  line-height: 1.45;
}
.clue-list li:last-child { border-bottom: 0; }
.clue-list li.empty { color: var(--fg-faint); }

.panel-actions { display: flex; flex-wrap: wrap; gap: 8px; }

.btn-small {
  background: transparent;
  border: 1px solid var(--line);
  color: var(--fg-dim);
  font-size: 11px;
  letter-spacing: 0.12em;
  padding: 7px 10px;
  cursor: pointer;
  min-height: 34px;
}
.btn-small:hover:not(:disabled) { color: var(--bright); border-color: var(--fg-faint); }
.btn-small:disabled { opacity: 0.4; cursor: not-allowed; }
.btn-small.wide { width: 100%; }
.btn-small.danger { color: var(--error); border-color: rgba(224, 138, 112, 0.35); }

.menu-btn { display: none; }
.drawer-backdrop { display: none; }

/* ---------- dialogs ---------- */

.overlay {
  position: fixed;
  inset: 0;
  background: rgba(1, 3, 2, 0.82);
  display: flex;
  align-items: center;
  justify-content: center;
  padding: var(--gutter);
  z-index: 50;
  animation: fade-in 0.3s ease;
}

.dialog {
  width: min(560px, 100%);
  max-height: calc(100dvh - 32px);
  overflow-y: auto;
  background: var(--bg-raised);
  border: 1px solid var(--fg-faint);
  padding: 24px;
  box-shadow: 0 0 40px rgba(140, 207, 154, 0.08);
}
.dialog h2 {
  margin: 0 0 16px;
  font-size: 14px;
  letter-spacing: 0.16em;
  color: var(--bright);
  font-weight: 600;
}
.dialog p { margin: 0 0 14px; }

.dialog-actions { display: flex; flex-wrap: wrap; gap: 10px; margin-top: 20px; }

.btn {
  background: transparent;
  border: 1px solid var(--fg-faint);
  color: var(--fg);
  padding: 10px 16px;
  cursor: pointer;
  letter-spacing: 0.06em;
  min-height: 42px;
}
.btn:hover { color: var(--bright); border-color: var(--fg); }
.btn-primary { border-color: var(--fg); color: var(--bright); }
.btn-danger { color: var(--error); border-color: rgba(224, 138, 112, 0.45); }

.completion-reveal {
  color: var(--bright);
  font-size: clamp(16px, 2.6vw, 20px);
  letter-spacing: 0.1em;
  line-height: 1.4;
}
.stats {
  display: grid;
  grid-template-columns: auto 1fr;
  gap: 4px 20px;
  margin: 18px 0;
  padding: 14px 0;
  border-top: 1px solid var(--line);
  border-bottom: 1px solid var(--line);
}
.stats dt { color: var(--fg-dim); }
.stats dd { margin: 0; color: var(--bright); text-align: right; }
.hook { color: var(--amber); }
.access-code {
  color: var(--bright);
  letter-spacing: 0.12em;
  border: 1px dashed var(--fg-faint);
  padding: 10px 12px;
}
.dialog .dim { color: var(--fg-dim); }

.assembly-row {
  display: flex;
  flex-wrap: wrap;
  gap: 8px;
  min-height: 58px;
  padding: 10px;
  border: 1px dashed var(--fg-faint);
  margin: 14px 0;
}
.assembly-pool { display: flex; flex-wrap: wrap; gap: 8px; }
.tile {
  display: inline-flex;
  flex-direction: column;
  align-items: flex-start;
  gap: 2px;
  background: rgba(227, 178, 92, 0.06);
  border: 1px solid var(--amber-dim);
  color: var(--amber);
  padding: 8px 12px;
  cursor: pointer;
  letter-spacing: 0.06em;
}
.tile small { color: var(--fg-faint); font-size: 10px; }
.tile:hover { background: rgba(227, 178, 92, 0.14); }
.assembly-feedback { color: var(--error); min-height: 1.5em; }

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

.connect {
  position: fixed;
  inset: 0;
  z-index: 100;
  background: var(--bg);
  display: flex;
  align-items: center;
  justify-content: center;
  padding: var(--gutter);
  cursor: pointer;
  text-align: center;
  transition: opacity 0.45s ease;
}
.connect.leaving { opacity: 0; pointer-events: none; }
.connect:focus { outline: none; }
.connect-kicker { color: var(--fg-faint); font-size: 11px; letter-spacing: 0.3em; margin: 0 0 18px; }
.connect-title {
  margin: 0;
  color: var(--bright);
  font-weight: 600;
  font-size: clamp(30px, 8vw, 64px);
  letter-spacing: 0.22em;
  text-indent: 0.22em;
}
body:not(.no-fx) .connect-title { text-shadow: 0 0 22px rgba(205, 247, 214, 0.35); }
.connect-sub { color: var(--fg-dim); margin: 14px 0 44px; letter-spacing: 0.1em; }
.connect-cta { color: var(--fg); letter-spacing: 0.08em; }
.connect-note { color: var(--fg-faint); font-size: 12px; margin-top: 44px; line-height: 1.7; }
.blink { animation: blink 1.1s steps(1) infinite; color: var(--bright); }
@keyframes blink { 50% { opacity: 0; } }

.noscript { position: fixed; inset: 0; background: var(--bg); color: var(--fg); padding: 40px; z-index: 200; }

/* ---------- CRT effects (subtle) ---------- */

.crt {
  pointer-events: none;
  position: fixed;
  inset: 0;
  z-index: 40;
  background:
    repeating-linear-gradient(to bottom, rgba(0, 0, 0, 0) 0, rgba(0, 0, 0, 0) 2px, rgba(0, 0, 0, 0.16) 3px),
    radial-gradient(ellipse at center, rgba(0, 0, 0, 0) 60%, rgba(0, 0, 0, 0.45) 100%);
  animation: flicker 7s infinite;
}
body.no-fx .crt { display: none; }

@keyframes flicker {
  0%, 96%, 100% { opacity: 1; }
  97% { opacity: 0.86; }
  98% { opacity: 1; }
  99% { opacity: 0.93; }
}

body.flash .terminal { animation: flash 0.6s ease; }
@keyframes flash {
  0% { background: rgba(140, 207, 154, 0.09); }
  100% { background: transparent; }
}

@keyframes fade-in { from { opacity: 0; } to { opacity: 1; } }

/* ---------- small screens: panel becomes a drawer ---------- */

@media (max-width: 899px) {
  :root { --text-size: 13.5px; }

  .brand-tag { display: none; }
  .menu-btn { display: inline-block; }
  .topbar .sound-btn { display: none; }

  .panel {
    position: fixed;
    top: 0;
    right: 0;
    bottom: 0;
    width: min(320px, 88vw);
    z-index: 60;
    transform: translateX(100%);
    transition: transform 0.25s ease;
    visibility: hidden;
  }
  body.drawer-open .panel { transform: none; visibility: visible; }
  body.drawer-open .drawer-backdrop {
    display: block;
    position: fixed;
    inset: 0;
    background: rgba(0, 0, 0, 0.6);
    z-index: 55;
  }
}

/* ---------- reduced motion ---------- */

@media (prefers-reduced-motion: reduce) {
  *, *::before, *::after {
    animation-duration: 0.001ms !important;
    animation-iteration-count: 1 !important;
    transition-duration: 0.001ms !important;
  }
  .crt { animation: none; }
  .blink { animation: none; }
}
