:root {
  --green: #43ff7a;
  --green-bright: #b6ffcf;
  --dim: #1f7a3a;
  --bg: #050805;
}
* { box-sizing: border-box; }
html, body { margin: 0; height: 100%; background: var(--bg); overflow: hidden; }
body { -webkit-text-size-adjust: 100%; }

#crt {
  position: relative;
  height: 100%;
  display: flex;
  flex-direction: column;
  padding: 1rem 1.25rem;
  padding-top: max(1rem, env(safe-area-inset-top));
  padding-bottom: max(0.75rem, env(safe-area-inset-bottom));
  font-family: "SFMono-Regular", Consolas, "Liberation Mono", Menlo, monospace;
  color: var(--green);
  font-size: 16px;
  line-height: 1.5;
  text-shadow: 0 0 4px rgba(67, 255, 122, 0.45);
}

/* scanlines + vignette */
#crt::after {
  content: "";
  position: absolute;
  inset: 0;
  pointer-events: none;
  z-index: 0;
  background:
    repeating-linear-gradient(rgba(0,0,0,0) 0 2px, rgba(0,0,0,0.16) 2px 3px),
    radial-gradient(ellipse at center, rgba(0,0,0,0) 55%, rgba(0,0,0,0.55) 100%);
  animation: flicker 4s infinite steps(60);
}
@keyframes flicker { 0%,96%,100%{opacity:1} 97%{opacity:.94} 98%{opacity:.99} }
@media (prefers-reduced-motion: reduce) { #crt::after { animation: none; } }

/* Always-on status readout */
#hud {
  position: relative; z-index: 1;
  flex: 0 0 auto;
  display: flex; flex-wrap: wrap; gap: 0.2rem; align-items: center; justify-content: flex-end;
  font-size: 0.8rem; color: var(--dim);
  padding-bottom: 0.15rem; margin-bottom: 0.2rem;
  border-bottom: 1px solid rgba(31, 122, 58, 0.5);
}
#hud-slots {
  display: flex;
  flex: 1 1 100%;
  flex-wrap: wrap;
  gap: 0.35rem;
  align-items: center;
  justify-content: flex-end;
}
.hud-slot { color: var(--green-bright); }
.hud-slot[hidden] { display: none; }
.hud-slot + .hud-slot::before {
  content: "·";
  color: var(--dim);
  margin-right: 0.5rem;
}
.hud-slot-emoji { display: inline-block; }
.hud-slot--bill .hud-slot-emoji {
  font-size: 1.5em;
  line-height: 0;
  vertical-align: -0.08em;
}
/* The copyright-version gets its own wrapping row above the compact counters. */
#hud #hud-version {
  flex: 1 0 100%;
  line-height: 1.25;
  opacity: 0.75;
  overflow-wrap: anywhere;
}
/* Sound toggle: tiny icon button that sits inline in the HUD row — always
   reachable without opening the phone, independent of Gary's voice mute. */
#sound-toggle {
  order: -1;
  margin-right: auto;
  min-height: 1.6rem; min-width: 1.8rem; padding: 0.15rem 0.4rem; font-size: 0.9rem;
}

#transcript {
  flex: 1;
  min-height: 0;
  overflow-y: auto;
  -webkit-overflow-scrolling: touch;
  white-space: pre-wrap;
  word-break: break-word;
  padding-right: 0.5rem;
  position: relative; z-index: 1;
}
/* ASCII map: must never wrap or the floor plan turns to confetti. Shrinks to
   fit a phone, caps at the normal size on desktop, scrolls if it still can't. */
.map {
  white-space: pre;
  word-break: normal;
  overflow-x: auto;
  -webkit-overflow-scrolling: touch;
  font-size: clamp(0.42rem, 2.3vw, 0.95rem);
  line-height: 1.15;
  color: var(--green-bright);
  margin: 0.4rem 0 0.5rem;
}

#transcript .banner { color: var(--green-bright); }
/* Diagnostic lines (the launch AI check, the AI command). Deliberately set
   apart from game prose so nobody mistakes a model report for story text. */
#transcript .sys {
  color: var(--dim);
  border-left: 2px solid var(--dim);
  padding-left: 0.5rem;
  margin: 0.4rem 0;
  white-space: pre-wrap;
}
#transcript .sys.ok { color: var(--green); border-left-color: var(--green); }
#transcript .echo { color: var(--green-bright); opacity: 0.85; }
#transcript .over { color: var(--green-bright); font-weight: bold; }
#transcript div { margin: 0 0 0.15rem; }

/* --- touch controls --- */
#controls {
  position: relative; z-index: 1;
  flex: 0 0 auto;
  display: flex; flex-direction: column; gap: 0.4rem;
  padding: 0.5rem 0 0.4rem;
  border-top: 1px solid var(--dim);
  margin-top: 0.4rem;
}
#controls .dpad, #controls .verbs { display: flex; gap: 0.2rem; flex-wrap: wrap; justify-content: center; }
#controls button {
  flex: 0 1 auto;             /* natural width, wrap+center (no giant stretched buttons) */
  min-width: 1.6rem;
  min-height: 1.25rem;
  padding: 0.2rem 0.3rem;
  background: rgba(67,255,122,0.06);
  border: 1px solid var(--dim);
  border-radius: 6px;
  color: var(--green-bright);
  font: inherit;
  font-size: 0.8rem;
  text-shadow: inherit;
  cursor: pointer;
  touch-action: manipulation;
  -webkit-tap-highlight-color: transparent;
}
#controls button:active { background: rgba(67,255,122,0.22); }

#inputline {
  position: relative; z-index: 1;
  flex: 0 0 auto;
  display: flex; gap: 0.5rem; align-items: center;
  margin-top: 0.5rem;
  padding: 0.55rem 0.7rem;
  border: 1px solid var(--dim);
  border-radius: 8px;
  background: rgba(67, 255, 122, 0.05);
}
#prompt { color: var(--green-bright); }
#cmd {
  flex: 1;
  min-width: 0;                      /* let it shrink instead of overflowing */
  background: transparent; border: none;
  color: var(--green-bright);
  font: inherit; font-size: 16px;   /* >=16px keeps iOS from zooming on focus */
  outline: none; caret-color: var(--green); text-shadow: inherit;
}
#cmd::placeholder { color: var(--dim); opacity: 1; }
#go {
  flex: 0 0 auto;
  min-height: 2.2rem; padding: 0.35rem 0.9rem;
  background: rgba(67, 255, 122, 0.1);
  border: 1px solid var(--dim); border-radius: 6px;
  color: var(--green-bright); font: inherit; font-size: 0.95rem;
  text-shadow: inherit; cursor: pointer; touch-action: manipulation;
  -webkit-tap-highlight-color: transparent;
}
#go span,
#phone-go span {
  display: inline-block;
  line-height: 1;
  font-weight: 900;
  transform: scale(2);
}
#go:active { background: rgba(67, 255, 122, 0.25); }

/* thin CRT scrollbar (desktop) */
#transcript::-webkit-scrollbar { width: 8px; }
#transcript::-webkit-scrollbar-thumb { background: var(--dim); border-radius: 4px; }

@media (max-width: 600px) {
  #crt { padding: 0.6rem 0.75rem; }
  #transcript { font-size: 14px; }        /* transcript can shrink; input stays 16px */
  #controls button { font-size: 0.76rem; min-height: 1.3rem; }
}

/* mic / small icon buttons */
.iconbtn {
  flex: 0 0 auto; min-height: 2.2rem; min-width: 2.4rem; padding: 0.35rem 0.55rem;
  background: rgba(67, 255, 122, 0.1); border: 1px solid var(--dim);
  border-radius: 6px; color: var(--green-bright); font: inherit; font-size: 1.05rem;
  cursor: pointer; touch-action: manipulation; -webkit-tap-highlight-color: transparent;
}
.iconbtn:active { background: rgba(67, 255, 122, 0.25); }
#sound-toggle.on { background: rgba(67, 255, 122, 0.28); border-color: var(--green); }
.iconbtn.listening {
  background: #b0322a; border-color: #e0483c; color: #fff;
  animation: micpulse 1s ease-in-out infinite;
}
@keyframes micpulse { 0%,100%{ box-shadow: 0 0 0 0 rgba(224,72,60,0.5);} 50%{ box-shadow: 0 0 0 7px rgba(224,72,60,0);} }
@media (prefers-reduced-motion: reduce) { .iconbtn.listening { animation: none; } }

.linkbtn {
  background: none; border: none; color: var(--dim); font: inherit; font-size: 0.85rem;
  cursor: pointer; padding: 0 0.25rem; touch-action: manipulation;
}
.linkbtn:active { color: var(--green-bright); }

/* Tap the speaker avatar (or the hint under it) to toggle Gary's voice — muted by default. */
.phone-avatar { cursor: pointer; -webkit-tap-highlight-color: transparent; transition: transform 0.1s ease; }
.phone-avatar:active { transform: scale(0.95); }
.phone-avatar:not(.muted) {   /* voice ON: solid glow, calm (stop the attention pulse) */
  border-color: var(--green-bright);
  box-shadow: 0 0 16px rgba(67, 255, 122, 0.6);
  animation: none;
}
.voice-controls {
  display: flex; flex-wrap: wrap; align-items: center; justify-content: center; gap: 0.25rem;
  margin: 3px auto 2px;
}
#phone-mute.voice-hint {
  display: inline-block; margin: 0; padding: 2px 4px;
  text-align: center; font-size: 0.85rem; font-weight: 700;
  color: var(--green-bright); background: none; border: none; cursor: pointer;
}
#phone-mute.voice-hint.on { color: var(--dim); font-weight: 400; }  /* voice on = quiet hint */
#gary-voice {
  width: 3.75rem; min-height: 1.8rem; padding: 0.15rem 0.25rem;
  color: var(--green-bright); background: rgba(67, 255, 122, 0.06);
  border: 1px solid var(--dim); border-radius: 5px;
  font: inherit; font-size: 0.8rem; cursor: pointer;
}
#gary-voice:disabled { opacity: 0.45; cursor: default; }
.phone-status { text-align: center; }

/* Collapse Gary's big header while typing on the call screen (touch), so the
   conversation window isn't squeezed to a few lines. */
#phone.compact .phone-avatar,
#phone.compact .phone-sub,
#phone.compact .phone-name { display: none; }
#phone.compact .phone-header { margin-bottom: 0.1rem; }
#phone.compact .phone-card { gap: 0.4rem; }

/* ============ The "phone call" screen (Gary's hint line) ============ */
#phone {
  position: fixed; inset: 0; z-index: 50;
  display: flex; align-items: stretch; justify-content: center;
  padding: max(1rem, env(safe-area-inset-top)) 1rem max(1rem, env(safe-area-inset-bottom));
  background: radial-gradient(ellipse at 50% 15%, #0b1c12, #030503 72%);
  font-family: "SFMono-Regular", Consolas, "Liberation Mono", Menlo, monospace;
  color: var(--green);
  text-shadow: 0 0 4px rgba(67, 255, 122, 0.45);
  opacity: 0; transform: scale(1.05);
  transition: opacity 0.35s ease, transform 0.35s ease;
}
#phone.show { opacity: 1; transform: scale(1); }
#phone[hidden] { display: none; }

.phone-card {
  width: 100%; max-width: 460px;
  display: flex; flex-direction: column; gap: 0.55rem;
}
.phone-header { text-align: center; }
.phone-num { color: var(--green-bright); font-size: 1.1rem; letter-spacing: 1px; }
.phone-sub { color: var(--dim); font-size: 0.8rem; }
.phone-avatar {
  align-self: center; margin-top: 0.3rem;
  width: 82px; height: 82px; border-radius: 50%;
  display: flex; align-items: center; justify-content: center;
  border: 2px solid var(--dim); background: rgba(67, 255, 122, 0.06);
  color: var(--green-bright); font-size: 2rem; letter-spacing: 2px;
  animation: ring 1.6s ease-in-out infinite;
}
@keyframes ring { 0%,100%{ box-shadow: 0 0 0 0 rgba(67,255,122,0.25);} 50%{ box-shadow: 0 0 0 8px rgba(67,255,122,0);} }
@media (prefers-reduced-motion: reduce) { .phone-avatar { animation: none; } }
.phone-name { text-align: center; font-size: 1.15rem; color: var(--green-bright); }
.phone-status { text-align: center; color: var(--dim); font-size: 0.9rem; }

#phone-transcript {
  flex: 1; overflow-y: auto; -webkit-overflow-scrolling: touch;
  white-space: pre-wrap; word-break: break-word;
  border: 1px solid var(--dim); border-radius: 10px;
  padding: 0.6rem 0.7rem; background: rgba(0, 0, 0, 0.35);
  font-size: 0.95rem; min-height: 4rem;
}
#phone-transcript .gary { color: var(--green); margin-bottom: 0.45rem; }
/* Gary is thinking — dimmed animated ellipsis, replaced in place by the reply.
   The reply itself carries the ◆ AI / · scripted tag and the header badge is
   always visible, so this stays wordless rather than repeating the claim. */
#phone-transcript .gary.thinking {
  opacity: 0.7; font-style: italic; color: var(--green);
  animation: gary-think 1.2s steps(4) infinite;
}
@keyframes gary-think { 0% { opacity: 0.2; } 50% { opacity: 1; } 100% { opacity: 0.2; } }
/* A line the model actually wrote, so you can tell it apart from the script. */
#phone-transcript .gary.llm { border-left: 2px solid var(--green); padding-left: 0.5rem; }
#phone-transcript .gary.llm::after {
  content: " ◆ AI";
  color: var(--green);
  font-size: 0.65em;
  letter-spacing: 0.08em;
  vertical-align: super;
  opacity: 0.85;
}
/* ...and its opposite: the model was available but fell back this turn. Saying
   so beats an unmarked line, which reads as "the AI isn't working at all". */
#phone-transcript .gary.scripted::after {
  content: " · scripted";
  color: var(--dim);
  font-size: 0.65em;
  letter-spacing: 0.08em;
  vertical-align: super;
}
@media (prefers-reduced-motion: reduce) {
  #phone-transcript .gary.thinking { animation: none; }
}

/* Model status badge in the phone header. Tap for the full story. */
.phone-ai {
  font-size: 0.7rem; letter-spacing: 0.04em; margin-top: 0.15rem;
  color: var(--dim); cursor: pointer; user-select: none;
}
.phone-ai.on { color: var(--green); }
#phone-transcript .you { color: var(--green-bright); opacity: 0.9; margin-bottom: 0.45rem; }
#phone-transcript .sys { color: var(--dim); }

.phone-inputline {
  display: flex; gap: 0.5rem; align-items: center;
  border: 1px solid var(--dim); border-radius: 8px;
  padding: 0.5rem 0.6rem; background: rgba(67, 255, 122, 0.05);
}
#phone-cmd {
  flex: 1; min-width: 0; background: transparent; border: none;
  color: var(--green-bright); font: inherit; font-size: 16px;
  outline: none; caret-color: var(--green); text-shadow: inherit;
}
#phone-cmd::placeholder { color: var(--dim); opacity: 1; }
#phone-go {
  flex: 0 0 auto; min-height: 2.2rem; padding: 0.35rem 0.9rem;
  background: rgba(67, 255, 122, 0.1); border: 1px solid var(--dim);
  border-radius: 6px; color: var(--green-bright); font: inherit; font-size: 0.95rem;
  text-shadow: inherit; cursor: pointer; touch-action: manipulation;
}
#phone-go:active { background: rgba(67, 255, 122, 0.25); }
#phone-end {
  position: relative; overflow: hidden;
  min-height: 3rem; border: none; border-radius: 12px;
  background: #b0322a; color: #fff; font: inherit; font-size: 1.05rem;
  font-weight: bold; letter-spacing: 1px; cursor: pointer; touch-action: manipulation;
  text-shadow: none;
}
#phone-end::before {
  content: ""; position: absolute; inset: 0; z-index: 0;
  background: #e0483c; transform: scaleX(0); transform-origin: left;
}
#phone-end.closing::before {
  animation: end-call-progress var(--end-call-duration, 1400ms) linear forwards;
}
#phone-end span { position: relative; z-index: 1; }
#phone-end:disabled { color: #fff; opacity: 1; cursor: wait; }
@keyframes end-call-progress { to { transform: scaleX(1); } }
#phone-end:active { background: #e0483c; }
