:root {
  color-scheme: dark;
  --bg: #030810;
  --fg: #e6e6e6;
  --muted: #9cb8d4;
  --agent: #56d9ff;
  --agent-dim: #2f80ed;
  --agent-glow: rgba(86, 217, 255, 0.45);
  --grid: rgba(86, 217, 255, 0.14);
  --grid-bright: rgba(86, 217, 255, 0.35);
  --red: #f48771;
  --hud-bg: rgba(4, 14, 28, 0.88);
  --hud-border: rgba(86, 217, 255, 0.42);
}

* {
  box-sizing: border-box;
}

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

body {
  font: 15px/1.55 ui-monospace, SFMono-Regular, Menlo, Consolas, monospace;
  background: var(--bg);
  color: var(--fg);
  overflow: hidden;
}

#session {
  position: fixed;
  inset: 0;
  display: flex;
  flex-direction: column;
  width: 100%;
  height: 100%;
  max-width: none;
  margin: 0;
  padding: 0;
}

#session.session-active .hud-btn {
  display: none;
}

#agent-stage {
  position: relative;
  flex: 1;
  display: flex;
  flex-direction: column;
  width: 100%;
  height: 100%;
  min-height: 0;
  overflow: hidden;
}

/* --- Digital space backdrop --- */

.digital-space {
  position: absolute;
  inset: 0;
  z-index: 0;
  background:
    radial-gradient(ellipse 90% 55% at 50% 108%, rgba(86, 217, 255, 0.18), transparent 62%),
    radial-gradient(ellipse 70% 45% at 50% -5%, rgba(47, 128, 237, 0.14), transparent 58%),
    linear-gradient(180deg, #020610 0%, #061428 42%, #030a14 100%);
}

.digital-horizon {
  position: absolute;
  left: -10%;
  right: -10%;
  bottom: 28%;
  height: 2px;
  background: linear-gradient(
    90deg,
    transparent 0%,
    rgba(86, 217, 255, 0.15) 15%,
    rgba(86, 217, 255, 0.85) 50%,
    rgba(86, 217, 255, 0.15) 85%,
    transparent 100%
  );
  box-shadow:
    0 0 24px rgba(86, 217, 255, 0.55),
    0 0 80px rgba(47, 128, 237, 0.25);
  animation: horizon-pulse 4s ease-in-out infinite alternate;
}

.digital-grid {
  position: absolute;
  left: -50%;
  right: -50%;
  bottom: -20%;
  height: 85%;
  transform: perspective(340px) rotateX(72deg);
  transform-origin: 50% 100%;
  background-image:
    linear-gradient(var(--grid) 1px, transparent 1px),
    linear-gradient(90deg, var(--grid) 1px, transparent 1px);
  background-size: 56px 56px;
  mask-image: linear-gradient(to top, black 8%, transparent 88%);
  animation: grid-drift 18s linear infinite;
}

.digital-lines {
  position: absolute;
  inset: 0;
  opacity: 0.55;
  background:
    linear-gradient(105deg, transparent 42%, rgba(86, 217, 255, 0.06) 49%, transparent 56%),
    repeating-linear-gradient(
      0deg,
      transparent,
      transparent 3px,
      rgba(86, 217, 255, 0.025) 3px,
      rgba(86, 217, 255, 0.025) 4px
    );
  pointer-events: none;
}

.digital-lines::before,
.digital-lines::after {
  content: "";
  position: absolute;
  width: 1px;
  top: 12%;
  bottom: 22%;
  background: linear-gradient(
    180deg,
    transparent,
    rgba(86, 217, 255, 0.35) 35%,
    rgba(86, 217, 255, 0.35) 65%,
    transparent
  );
  box-shadow: 0 0 12px rgba(86, 217, 255, 0.3);
}

.digital-lines::before {
  left: 18%;
}

.digital-lines::after {
  right: 18%;
}

.digital-scanlines {
  position: absolute;
  inset: 0;
  opacity: 0.07;
  background: repeating-linear-gradient(
    0deg,
    rgba(0, 0, 0, 0.35) 0,
    rgba(0, 0, 0, 0.35) 1px,
    transparent 1px,
    transparent 3px
  );
  pointer-events: none;
}

.digital-vignette {
  position: absolute;
  inset: 0;
  background: radial-gradient(ellipse at center, transparent 42%, rgba(2, 6, 16, 0.72) 100%);
  pointer-events: none;
}

@keyframes grid-drift {
  from {
    background-position: 0 0;
  }
  to {
    background-position: 0 56px;
  }
}

@keyframes horizon-pulse {
  from {
    opacity: 0.75;
  }
  to {
    opacity: 1;
  }
}

/* --- Avatar layer (transparent canvas over backdrop) --- */

.agent-face-wrap {
  position: absolute;
  inset: 0;
  z-index: 1;
  width: 100%;
  height: 100%;
}

#agent-face {
  position: relative;
  width: 100%;
  height: 100%;
  min-height: 0;
  border-radius: 0;
  overflow: hidden;
  background: transparent;
}

#avatar-canvas {
  display: block;
  width: 100%;
  height: 100%;
  min-height: 0;
}

#avatar-canvas canvas,
#agent-face canvas {
  display: block;
  width: 100%;
  height: 100%;
  min-height: 0;
  background: transparent;
}

#agent-stage[data-voice-state="thinking"] #agent-face,
#agent-stage[data-voice-state="speaking"] #agent-face {
  filter: drop-shadow(0 0 32px var(--agent-glow));
}

#agent-stage[data-voice-state="listening"] #agent-face,
#agent-stage[data-voice-state="recording"] #agent-face {
  filter: drop-shadow(0 0 22px rgba(86, 217, 255, 0.28));
}

#agent-face[data-state="loading"]::after {
  content: none;
}

#agent-face[data-state="error"]::after {
  content: "Avatar failed to load";
  position: absolute;
  left: 50%;
  top: 50%;
  transform: translate(-50%, -50%);
  padding: 0.75rem 1.25rem;
  border-radius: 12px;
  border: 1px solid rgba(244, 135, 113, 0.45);
  background: rgba(26, 12, 14, 0.92);
  backdrop-filter: blur(10px);
  font-size: 0.85rem;
  color: var(--red);
  pointer-events: none;
  z-index: 2;
}

#agent-face[data-state="fallback"]::after {
  content: "Voice only — avatar unavailable";
  position: absolute;
  left: 50%;
  top: 50%;
  transform: translate(-50%, -50%);
  padding: 0.75rem 1.25rem;
  border-radius: 12px;
  border: 1px solid var(--hud-border);
  background: var(--hud-bg);
  backdrop-filter: blur(10px);
  font-size: 0.85rem;
  color: var(--muted);
  pointer-events: none;
  z-index: 2;
}

#agent-face.streaming,
#agent-face.speaking {
  filter: drop-shadow(0 0 36px rgba(86, 217, 255, 0.42));
}

#agent-face.listening {
  box-shadow: inset 0 0 0 1px rgba(86, 217, 255, 0.1);
}

/* --- Bottom HUD slot (start button + status share one pill) --- */

.hud-slot {
  position: absolute;
  left: 50%;
  bottom: max(2rem, env(safe-area-inset-bottom));
  transform: translateX(-50%);
  z-index: 4;
  display: flex;
  justify-content: center;
  min-width: min(20rem, 88vw);
}

.hud-pill {
  margin: 0;
  padding: 0.85rem 1.6rem;
  border-radius: 999px;
  border: 1px solid var(--hud-border);
  background: var(--hud-bg);
  backdrop-filter: blur(12px);
  font: inherit;
  font-size: 0.88rem;
  font-weight: 500;
  letter-spacing: 0.04em;
  color: var(--agent);
  box-shadow:
    0 4px 28px rgba(0, 0, 0, 0.45),
    inset 0 1px 0 rgba(255, 255, 255, 0.06),
    0 0 24px rgba(86, 217, 255, 0.1);
  white-space: nowrap;
}

.hud-btn {
  cursor: pointer;
  transition: background 0.15s ease, border-color 0.15s ease, box-shadow 0.15s ease;
}

.hud-btn:hover {
  background: rgba(8, 32, 56, 0.94);
  border-color: rgba(86, 217, 255, 0.65);
  box-shadow:
    0 4px 28px rgba(0, 0, 0, 0.45),
    0 0 32px rgba(86, 217, 255, 0.2);
}

.hud-btn:focus-visible {
  outline: 2px solid var(--agent);
  outline-offset: 3px;
}

.hud-status {
  text-transform: uppercase;
  letter-spacing: 0.06em;
  color: var(--muted);
  pointer-events: none;
}

#agent-stage[data-voice-state="loading"] .hud-status {
  color: var(--muted);
  text-transform: none;
  letter-spacing: 0.04em;
}

#agent-stage[data-voice-state="listening"] .hud-status {
  color: var(--agent);
  border-color: rgba(86, 217, 255, 0.55);
  box-shadow:
    0 4px 28px rgba(0, 0, 0, 0.5),
    0 0 24px rgba(86, 217, 255, 0.15);
}

#agent-stage[data-voice-state="recording"] .hud-status {
  color: #fff;
  border-color: rgba(86, 217, 255, 0.75);
  background: rgba(6, 24, 44, 0.92);
  box-shadow:
    0 4px 28px rgba(0, 0, 0, 0.5),
    0 0 32px rgba(86, 217, 255, 0.22);
  animation: hud-pulse 1.4s ease-in-out infinite;
}

#agent-stage[data-voice-state="thinking"] .hud-status,
#agent-stage[data-voice-state="processing"] .hud-status {
  color: #c8ecff;
  border-color: rgba(86, 217, 255, 0.5);
}

#agent-stage[data-voice-state="error"] .hud-status,
#agent-stage[data-voice-state="mic-denied"] .hud-status {
  color: var(--red);
  border-color: rgba(244, 135, 113, 0.5);
  background: rgba(28, 10, 12, 0.92);
  text-transform: none;
  letter-spacing: 0.02em;
}

@keyframes hud-pulse {
  0%,
  100% {
    box-shadow:
      0 4px 28px rgba(0, 0, 0, 0.5),
      0 0 24px rgba(86, 217, 255, 0.18);
  }
  50% {
    box-shadow:
      0 4px 28px rgba(0, 0, 0, 0.5),
      0 0 36px rgba(86, 217, 255, 0.32);
  }
}

.noscript {
  max-width: 42rem;
  margin: 4rem auto;
  padding: 0 1.5rem;
  font-family: ui-monospace, SFMono-Regular, Menlo, monospace;
}

@media (prefers-reduced-motion: reduce) {
  .digital-grid,
  .digital-horizon,
  #agent-stage[data-voice-state="recording"] .hud-status {
    animation: none;
  }
}
