:root {
  --void: #050406;
  --panel: #0c0a0e;
  --line: #2a1618;
  --hal: #e10600;
  --hal-dim: #7a0a08;
  --hal-glow: rgba(225, 6, 0, 0.55);
  --amber: #c9a27a;
  --text: #e8dcc8;
  --muted: #8a7a6e;
  --mono: "IBM Plex Mono", ui-monospace, monospace;
  --sans: "IBM Plex Sans", system-ui, sans-serif;
  --display: "Orbitron", sans-serif;
}

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

html,
body {
  margin: 0;
  height: 100%;
  background: var(--void);
  color: var(--text);
  font-family: var(--sans);
}

body {
  min-height: 100dvh;
  background:
    radial-gradient(ellipse 55% 40% at 50% 22%, rgba(225, 6, 0, 0.14), transparent 60%),
    radial-gradient(ellipse 80% 45% at 50% 100%, rgba(40, 8, 8, 0.35), transparent 55%),
    var(--void);
}

.scanlines {
  pointer-events: none;
  position: fixed;
  inset: 0;
  z-index: 40;
  opacity: 0.035;
  background: repeating-linear-gradient(
    to bottom,
    transparent 0,
    transparent 2px,
    rgba(0, 0, 0, 0.55) 3px
  );
}

.vignette {
  pointer-events: none;
  position: fixed;
  inset: 0;
  z-index: 30;
  background: radial-gradient(ellipse at center, transparent 42%, rgba(0, 0, 0, 0.55) 100%);
}

.mode-bar {
  position: fixed;
  top: max(0.75rem, env(safe-area-inset-top));
  right: max(0.75rem, env(safe-area-inset-right));
  z-index: 60;
  display: inline-flex;
  border: 1px solid var(--line);
  background: rgba(8, 6, 8, 0.88);
  backdrop-filter: blur(6px);
}

.mode-btn {
  font-family: var(--mono);
  font-size: 0.68rem;
  letter-spacing: 0.12em;
  text-transform: uppercase;
  padding: 0.45rem 0.75rem;
  border: 0;
  background: transparent;
  color: var(--muted);
  cursor: pointer;
}

.mode-btn[aria-pressed="true"] {
  color: var(--hal);
  background: rgba(225, 6, 0, 0.12);
}

.mode-btn:hover {
  color: var(--text);
}

.mode-btn:focus-visible {
  outline: 1px solid var(--hal);
  outline-offset: 2px;
}

.console {
  position: relative;
  z-index: 1;
  max-width: 720px;
  margin: 0 auto;
  padding: 1.25rem;
}

/* ===== VOICE: fixed stage, button never scrolls away ===== */
body[data-mode="voice"] {
  overflow: hidden;
}

body[data-mode="voice"] .console {
  height: 100dvh;
  max-width: 520px;
  padding:
    calc(2.75rem + env(safe-area-inset-top))
    1.1rem
    calc(1rem + env(safe-area-inset-bottom));
  display: grid;
  grid-template-rows: auto minmax(0, 1fr) auto auto;
  gap: 0.75rem;
  overflow: hidden;
}

body[data-mode="voice"] .masthead {
  flex-direction: column;
  align-items: center;
  text-align: center;
  gap: 0.75rem;
  flex-shrink: 0;
}

body[data-mode="voice"] .eye-wrap {
  width: min(34vw, 140px);
  height: min(34vw, 140px);
}

body[data-mode="voice"] .eye-core {
  inset: 26%;
}

body[data-mode="voice"] .eye-glare {
  top: 34%;
  left: 36%;
  width: 14%;
  height: 11%;
}

body[data-mode="voice"] .identity h1 {
  font-size: clamp(1.35rem, 5vw, 1.75rem);
  letter-spacing: 0.18em;
}

body[data-mode="voice"] .unit {
  display: none;
}

body[data-mode="voice"] .transcript {
  display: none;
}

body[data-mode="voice"] .composer {
  display: none;
}

body[data-mode="voice"] .voice-stage,
body[data-mode="voice"] .voice-caption,
body[data-mode="voice"] .voice-dock {
  display: flex;
}

/* ===== TEXT MODE ===== */
body[data-mode="text"] .console {
  min-height: 100dvh;
  display: grid;
  grid-template-rows: auto minmax(0, 1fr) auto;
  gap: 0.85rem;
  padding-top: calc(3.25rem + env(safe-area-inset-top));
  padding-bottom: calc(1rem + env(safe-area-inset-bottom));
}

body[data-mode="text"] .masthead {
  display: flex;
  align-items: center;
  gap: 0.9rem;
  padding-bottom: 0.75rem;
  border-bottom: 1px solid var(--line);
}

body[data-mode="text"] .eye-wrap {
  width: 52px;
  height: 52px;
}

body[data-mode="text"] .eye-core {
  inset: 14px;
}

body[data-mode="text"] .eye-glare {
  top: 17px;
  left: 19px;
  width: 7px;
  height: 6px;
}

body[data-mode="text"] .voice-stage,
body[data-mode="text"] .voice-caption,
body[data-mode="text"] .voice-dock {
  display: none;
}

body[data-mode="text"] .transcript {
  display: flex;
}

body[data-mode="text"] .composer {
  display: grid;
}

/* --- shared --- */
.masthead {
  display: flex;
  align-items: center;
  gap: 1rem;
}

.eye-wrap {
  position: relative;
  flex-shrink: 0;
  transition: width 0.3s ease, height 0.3s ease;
}

.eye-ring {
  position: absolute;
  inset: 0;
  border-radius: 50%;
  background: radial-gradient(circle at 50% 45%, #1a1010 0%, #050304 70%);
  box-shadow:
    0 0 0 1px #3a1515,
    0 0 28px var(--hal-glow),
    inset 0 0 24px rgba(0, 0, 0, 0.85);
}

.eye-core {
  position: absolute;
  inset: 18px;
  border-radius: 50%;
  background: radial-gradient(circle at 35% 30%, #ff4a3a 0%, var(--hal) 40%, var(--hal-dim) 75%, #2a0000 100%);
  box-shadow: 0 0 22px var(--hal-glow);
  animation: pulse-idle 3.6s ease-in-out infinite;
}

.eye-glare {
  position: absolute;
  border-radius: 50%;
  background: rgba(255, 220, 200, 0.55);
  filter: blur(0.5px);
}

.eye-wrap[data-state="thinking"] .eye-core,
.eye-wrap[data-state="listening"] .eye-core {
  animation: pulse-think 0.95s ease-in-out infinite;
  box-shadow: 0 0 36px rgba(225, 6, 0, 0.9);
}

.eye-wrap[data-state="speaking"] .eye-core {
  animation: pulse-speak 1.2s ease-in-out infinite;
}

@keyframes pulse-idle {
  0%,
  100% {
    transform: scale(1);
    filter: brightness(0.92);
  }
  50% {
    transform: scale(1.04);
    filter: brightness(1.1);
  }
}

@keyframes pulse-think {
  0%,
  100% {
    transform: scale(0.96);
    filter: brightness(0.85);
  }
  50% {
    transform: scale(1.08);
    filter: brightness(1.28);
  }
}

@keyframes pulse-speak {
  0%,
  100% {
    transform: scale(1);
  }
  50% {
    transform: scale(1.08);
  }
}

.identity h1 {
  margin: 0;
  font-family: var(--display);
  font-weight: 700;
  font-size: clamp(1.2rem, 3.5vw, 1.6rem);
  letter-spacing: 0.12em;
  color: var(--hal);
  text-shadow: 0 0 18px var(--hal-glow);
}

.unit {
  margin: 0 0 0.15rem;
  font-family: var(--mono);
  font-size: 0.58rem;
  letter-spacing: 0.08em;
  text-transform: uppercase;
  color: var(--muted);
  line-height: 1.35;
}

.status {
  margin: 0.3rem 0 0;
  font-family: var(--mono);
  font-size: 0.74rem;
  color: var(--amber);
}

/* --- waveform --- */
.voice-stage {
  display: none;
  align-items: center;
  justify-content: center;
  min-height: 0;
}

.wave {
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 3px;
  width: min(100%, 280px);
  height: 72px;
}

.wave span {
  flex: 1;
  max-width: 8px;
  height: 12%;
  border-radius: 99px;
  background: linear-gradient(to top, var(--hal-dim), var(--hal));
  opacity: 0.45;
  transform-origin: center;
  animation: wave-idle 2.4s ease-in-out infinite;
}

.wave span:nth-child(odd) {
  animation-delay: -0.4s;
}
.wave span:nth-child(3n) {
  animation-delay: -0.9s;
}
.wave span:nth-child(4n) {
  animation-delay: -1.3s;
}
.wave span:nth-child(5n) {
  animation-delay: -1.7s;
}

#voiceStage[data-state="listening"] .wave span,
#voiceStage[data-state="thinking"] .wave span {
  opacity: 0.85;
  animation-name: wave-active;
  animation-duration: 0.85s;
}

#voiceStage[data-state="speaking"] .wave span {
  opacity: 1;
  animation-name: wave-speak;
  animation-duration: 0.55s;
  box-shadow: 0 0 10px var(--hal-glow);
}

@keyframes wave-idle {
  0%,
  100% {
    height: 10%;
  }
  50% {
    height: 28%;
  }
}

@keyframes wave-active {
  0%,
  100% {
    height: 18%;
  }
  50% {
    height: 78%;
  }
}

@keyframes wave-speak {
  0%,
  100% {
    height: 22%;
  }
  40% {
    height: 95%;
  }
  70% {
    height: 40%;
  }
}

/* --- caption opens chat --- */
.voice-caption {
  display: none;
  flex-direction: column;
  align-items: center;
  gap: 0.35rem;
  width: 100%;
  margin: 0;
  padding: 0.65rem 0.85rem;
  border: 1px solid transparent;
  border-top-color: var(--line);
  background: transparent;
  color: inherit;
  cursor: pointer;
  text-align: center;
}

.voice-caption:hover,
.voice-caption:focus-visible {
  border-color: var(--line);
  background: rgba(225, 6, 0, 0.05);
  outline: none;
}

.caption-label {
  font-family: var(--mono);
  font-size: 0.62rem;
  letter-spacing: 0.1em;
  text-transform: uppercase;
  color: var(--muted);
}

.caption-text {
  display: -webkit-box;
  -webkit-line-clamp: 2;
  -webkit-box-orient: vertical;
  overflow: hidden;
  max-width: 36rem;
  font-size: 0.95rem;
  line-height: 1.45;
  color: var(--text);
}

.caption-text[data-role="user"] {
  color: var(--amber);
}

.transcript {
  display: none;
  overflow-y: auto;
  flex-direction: column;
  gap: 0.75rem;
  min-height: 0;
  padding: 0.15rem 0.1rem;
}

.bubble {
  max-width: 92%;
  padding: 0.7rem 0.85rem;
  border: 1px solid var(--line);
  background: var(--panel);
  line-height: 1.55;
  font-size: 0.96rem;
  white-space: pre-wrap;
  word-break: break-word;
}

.bubble.user {
  align-self: flex-end;
  border-color: #3a2a22;
  background: #141014;
}

.bubble.hal {
  align-self: flex-start;
  border-color: #4a1212;
  box-shadow: inset 2px 0 0 var(--hal-dim);
}

.bubble.tool {
  align-self: flex-start;
  max-width: 100%;
  font-family: var(--mono);
  font-size: 0.72rem;
  color: var(--muted);
  background: transparent;
  border-style: dashed;
  padding: 0.4rem 0.6rem;
}

.bubble .who {
  display: block;
  font-family: var(--mono);
  font-size: 0.66rem;
  letter-spacing: 0.06em;
  text-transform: uppercase;
  color: var(--hal);
  margin-bottom: 0.3rem;
}

.bubble.user .who {
  color: var(--amber);
}

.voice-dock {
  display: none;
  flex-direction: column;
  align-items: center;
  gap: 0.5rem;
  flex-shrink: 0;
  padding-top: 0.25rem;
}

.ptt {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 0.55rem;
  min-width: 9.5rem;
  min-height: 3.35rem;
  padding: 0.9rem 1.5rem;
  font-family: var(--display);
  font-size: 0.78rem;
  letter-spacing: 0.18em;
  text-transform: uppercase;
  border: 1px solid var(--hal-dim);
  border-radius: 999px;
  background: radial-gradient(circle at 50% 30%, #3a1010, #120606 70%);
  color: var(--hal);
  cursor: pointer;
  box-shadow: 0 0 0 1px rgba(225, 6, 0, 0.15), 0 0 24px rgba(225, 6, 0, 0.2);
}

.ptt:hover:not(:disabled) {
  color: #ff3b30;
}

.ptt:disabled {
  opacity: 0.45;
  cursor: wait;
}

.ptt:focus-visible {
  outline: 2px solid var(--hal);
  outline-offset: 3px;
}

.ptt-dot {
  width: 9px;
  height: 9px;
  border-radius: 50%;
  background: var(--muted);
}

.ptt[aria-pressed="true"] {
  border-color: var(--hal);
  color: #fff;
  background: radial-gradient(circle at 50% 30%, #8a1818, #2a0606 70%);
  box-shadow: 0 0 28px var(--hal-glow);
}

.ptt[aria-pressed="true"] .ptt-dot {
  background: var(--hal);
  box-shadow: 0 0 10px var(--hal);
  animation: rec-blink 1s steps(1) infinite;
}

@keyframes rec-blink {
  0%,
  100% {
    opacity: 1;
  }
  50% {
    opacity: 0.2;
  }
}

.hint {
  margin: 0;
  font-family: var(--mono);
  font-size: 0.65rem;
  color: var(--muted);
  letter-spacing: 0.04em;
  text-align: center;
}

.composer {
  display: none;
  grid-template-columns: 1fr auto;
  gap: 0.65rem;
  align-items: end;
  padding-top: 0.65rem;
  border-top: 1px solid var(--line);
}

.composer textarea {
  width: 100%;
  resize: none;
  min-height: 48px;
  max-height: 140px;
  padding: 0.85rem 1rem;
  border: 1px solid var(--line);
  background: #0a080a;
  color: var(--text);
  font-family: var(--sans);
  font-size: 1rem;
  line-height: 1.4;
  outline: none;
}

.composer textarea:focus {
  border-color: var(--hal-dim);
  box-shadow: 0 0 0 1px var(--hal-dim);
}

.composer button[type="submit"] {
  font-family: var(--display);
  font-size: 0.72rem;
  letter-spacing: 0.14em;
  text-transform: uppercase;
  padding: 0.9rem 1.1rem;
  border: 1px solid var(--hal-dim);
  background: linear-gradient(180deg, #2a0a0a, #120606);
  color: var(--hal);
  cursor: pointer;
}

.composer button:disabled {
  opacity: 0.45;
  cursor: wait;
}

.install-btn {
  position: fixed;
  left: 50%;
  bottom: calc(1rem + env(safe-area-inset-bottom));
  transform: translateX(-50%);
  z-index: 70;
  font-family: var(--display);
  font-size: 0.68rem;
  letter-spacing: 0.12em;
  text-transform: uppercase;
  padding: 0.7rem 1.15rem;
  border: 1px solid var(--hal-dim);
  border-radius: 999px;
  background: rgba(12, 6, 8, 0.92);
  color: var(--hal);
  box-shadow: 0 0 20px rgba(225, 6, 0, 0.25);
  cursor: pointer;
}

.install-btn:hover,
.install-btn:focus-visible {
  color: #ff3b30;
  outline: none;
}

body[data-mode="voice"] .install-btn {
  bottom: calc(5.5rem + env(safe-area-inset-bottom));
}

@media (display-mode: standalone) {
  .install-btn {
    display: none !important;
  }
}

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

@media (prefers-reduced-motion: reduce) {
  .eye-core,
  .wave span,
  .ptt[aria-pressed="true"] .ptt-dot {
    animation: none !important;
  }
}

@media (max-width: 520px) {
  body[data-mode="text"] .composer {
    grid-template-columns: 1fr;
  }

  body[data-mode="text"] .composer button[type="submit"] {
    width: 100%;
  }
}
