:root {
  --bg-a: #08131f;
  --bg-b: #112741;
  --bg-c: #3b6d7e;
  --text: #e7f5ff;
  --muted: #b3d2e2;
  --accent: #ffcb77;
  --danger: #f97373;
}

* {
  box-sizing: border-box;
}

html,
body {
  height: 100%;
}

body {
  margin: 0;
  color: var(--text);
  font-family: "Trebuchet MS", "Gill Sans", "Segoe UI", sans-serif;
  overflow: hidden;
  background: radial-gradient(circle at 15% 15%, #1b3a56 0%, transparent 36%),
    radial-gradient(circle at 82% 20%, #2e5672 0%, transparent 34%),
    radial-gradient(circle at 55% 84%, #5e8b95 0%, transparent 30%),
    linear-gradient(145deg, var(--bg-a), var(--bg-b) 46%, var(--bg-c));
}

.scene-canvas {
  position: fixed;
  inset: 0;
  width: 100vw;
  height: 100dvh;
  pointer-events: none;
  opacity: 0;
  transition: opacity 400ms ease;
  z-index: 0;
}

.backdrop {
  position: fixed;
  inset: 0;
  background-image: repeating-linear-gradient(
    135deg,
    rgba(255, 255, 255, 0.02) 0,
    rgba(255, 255, 255, 0.02) 2px,
    transparent 2px,
    transparent 14px
  );
  pointer-events: none;
  z-index: 1;
}

.stage {
  position: relative;
  z-index: 2;
  min-height: 100%;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 1.1rem;
  padding: 2rem 1rem 7rem;
  text-align: center;
}

.orb-shell {
  width: min(46vw, 250px);
  aspect-ratio: 1;
  position: relative;
  display: grid;
  place-items: center;
  filter: drop-shadow(0 20px 50px rgba(0, 0, 0, 0.45));
}

.orb-core {
  width: 72%;
  aspect-ratio: 1;
  border-radius: 50%;
  background: radial-gradient(circle at 30% 22%, #f8f7ef 0%, #f2dcb4 24%, #ffb067 45%, #f16955 68%, #2d2f47 100%);
}

.orb-speech-light {
  display: none;
}

.orb-ring {
  position: absolute;
  border-radius: 50%;
  border: 2px solid rgba(255, 255, 255, 0.25);
}

.ring-a {
  width: 88%;
  aspect-ratio: 1;
}

.ring-b {
  width: 104%;
  aspect-ratio: 1;
}

.orb-glow {
  position: absolute;
  inset: 8%;
  border-radius: 50%;
  background: radial-gradient(circle, rgba(255, 201, 120, 0.22) 0%, rgba(236, 137, 96, 0.12) 42%, transparent 72%);
  transform: scale(1);
  animation: halo 2.6s ease-in-out infinite;
}

.status {
  width: 100%;
  min-height: 1.25rem;
  margin: 0;
  font-size: 1.04rem;
  letter-spacing: 0.04em;
  color: var(--muted);
}

.menu-toggle {
  position: fixed;
  top: max(1rem, env(safe-area-inset-top));
  right: max(1rem, env(safe-area-inset-right));
  z-index: 4;
  display: grid;
  place-items: center;
  width: 44px;
  height: 44px;
  padding: 0;
  border: 0;
  background: transparent;
  color: #eef6ff;
  cursor: pointer;
  transform: rotate(0deg);
  transition: transform 220ms ease, opacity 180ms ease;
}

.menu-toggle:hover {
  opacity: 0.78;
}

.menu-toggle[aria-expanded="true"] {
  transform: rotate(0deg);
}

.menu-toggle svg {
  width: 30px;
  height: 30px;
}

.menu-toggle path {
  fill: none;
  stroke: currentColor;
  stroke-width: 2.15;
  stroke-linecap: round;
}

.controls {
  position: fixed;
  left: 50%;
  bottom: max(1.5rem, env(safe-area-inset-bottom));
  transform: translateX(-50%);
  z-index: 3;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 0.55rem;
  width: auto;
  padding: 0;
  border: 1px solid rgba(255, 255, 255, 0.16);
  border-radius: 18px;
  background: rgba(4, 15, 29, 0.5);
  backdrop-filter: blur(10px);
  box-shadow: 0 18px 46px rgba(0, 0, 0, 0.28);
}

.control-buttons {
  display: grid;
  grid-template-columns: 112px 102px 112px;
  justify-content: center;
  align-items: center;
  gap: 1.1rem;
  width: max-content;
}

.btn {
  border: 1px solid rgba(255, 255, 255, 0.3);
  background: rgba(6, 18, 35, 0.6);
  color: var(--text);
  display: inline-flex;
  align-items: center;
  justify-content: center;
  font-size: 0.78rem;
  letter-spacing: 0.16em;
  text-transform: uppercase;
  padding: 0.46rem 1.18rem;
  border-radius: 6px;
  cursor: pointer;
  min-width: 86px;
  backdrop-filter: blur(4px);
}

.btn:hover {
  background: rgba(12, 29, 50, 0.82);
}

.btn:disabled {
  opacity: 0.6;
  cursor: not-allowed;
}

.btn.primary {
  border-color: rgba(255, 203, 119, 0.55);
}

.btn.warn {
  border-color: rgba(249, 115, 115, 0.62);
}

.state-listening .orb-core {
  animation-duration: 1.35s;
}

.state-listening .orb-glow {
  animation-duration: 1s;
}

.state-thinking .orb-core {
  background: radial-gradient(circle at 25% 25%, #f6f4df 0%, #8dd3ef 32%, #4a8ec8 62%, #13284d 100%);
}

.state-speaking .orb-core {
  background: radial-gradient(circle at 36% 18%, #fff4d8 0%, #fdd785 28%, #fb9c5f 49%, #ec6855 70%, #3b2f4a 100%);
}

.voice-drawer {
  position: fixed;
  top: 0;
  right: 0;
  width: min(92vw, 420px);
  height: 100dvh;
  padding: 1rem 1rem 1.2rem;
  background: linear-gradient(185deg, rgba(12, 23, 35, 0.28), rgba(8, 18, 31, 0.34));
  backdrop-filter: blur(4px);
  border-left: 1px solid rgba(255, 255, 255, 0.14);
  box-shadow: -14px 0 38px rgba(0, 0, 0, 0.35);
  transform: translateX(102%);
  transition: transform 240ms ease;
  z-index: 6;
  overflow: auto;
}

.voice-drawer.open {
  transform: translateX(0);
}

.voice-drawer-backdrop {
  position: fixed;
  inset: 0;
  z-index: 5;
  background: rgba(0, 0, 0, 0.06);
}

.voice-drawer-backdrop.hidden {
  display: none;
}

.voice-head {
  display: flex;
  justify-content: flex-end;
  align-items: center;
  margin-bottom: 0.3rem;
}

.voice-head h2 {
  margin: 0;
  font-size: 1.2rem;
  letter-spacing: 0.02em;
}

.drawer-close {
  border: 1px solid rgba(255, 255, 255, 0.26);
  color: var(--text);
  background: rgba(255, 255, 255, 0.08);
  border-radius: 10px;
  width: 34px;
  height: 34px;
  cursor: pointer;
}

.voice-status {
  margin: 0 0 0.9rem;
  color: var(--muted);
  font-size: 0.92rem;
}

.voice-speed,
.model-picker,
.tts-picker,
.system-prompt,
.voice-direction {
  margin: 0 0 1rem;
  padding: 0.85rem 0.9rem;
  border: 1px solid rgba(255, 255, 255, 0.12);
  border-radius: 8px;
  background: rgba(3, 14, 27, 0.22);
}

.speed-head,
.prompt-head {
  display: flex;
  justify-content: space-between;
  align-items: center;
  gap: 1rem;
  margin-bottom: 0.65rem;
  font-size: 0.9rem;
}

.speed-head output {
  color: var(--accent);
  font-variant-numeric: tabular-nums;
}

.voice-speed input[type="range"] {
  width: 100%;
  accent-color: var(--accent);
  cursor: pointer;
}

.model-picker,
.tts-picker {
  display: grid;
  gap: 0.55rem;
  font-size: 0.9rem;
}

.model-picker select,
.tts-picker select {
  width: 100%;
  border: 1px solid rgba(255, 255, 255, 0.18);
  border-radius: 8px;
  background: rgba(3, 14, 27, 0.34);
  color: var(--text);
  padding: 0.65rem 0.7rem;
  font: inherit;
}

.model-picker select:focus,
.tts-picker select:focus {
  outline: 2px solid rgba(255, 203, 119, 0.45);
  outline-offset: 2px;
}

.model-picker option,
.tts-picker option {
  background: #05070b;
  color: #eef6ff;
}

.model-picker option:hover,
.model-picker option:checked,
.tts-picker option:hover,
.tts-picker option:checked {
  background: #000;
  color: #fff;
}

.text-btn {
  border: 0;
  background: transparent;
  color: var(--accent);
  cursor: pointer;
  font: inherit;
  padding: 0;
}

.text-btn:hover {
  color: #ffe1a8;
}

.voice-direction.hidden {
  display: none;
}

.system-prompt textarea,
.voice-direction textarea {
  width: 100%;
  resize: vertical;
  min-height: 132px;
  max-height: 40vh;
  border: 1px solid rgba(255, 255, 255, 0.18);
  border-radius: 8px;
  background: rgba(3, 14, 27, 0.3);
  color: var(--text);
  padding: 0.7rem;
  font: 0.86rem/1.4 "Segoe UI", sans-serif;
}

.voice-direction textarea {
  min-height: 96px;
}

.system-prompt textarea:focus,
.voice-direction textarea:focus {
  outline: 2px solid rgba(255, 203, 119, 0.45);
  outline-offset: 2px;
}

.voice-list {
  display: grid;
  gap: 0.7rem;
}

.voice-group {
  border: 1px solid rgba(255, 255, 255, 0.12);
  border-radius: 12px;
  background: rgba(3, 14, 27, 0.2);
  overflow: hidden;
}

.voice-group-title {
  margin: 0;
  padding: 0.5rem 0.75rem;
  font-size: 0.86rem;
  text-align: left;
  background: rgba(255, 255, 255, 0.05);
  color: #cde6f4;
}

.voice-option {
  width: 100%;
  border: 0;
  border-top: 1px solid rgba(255, 255, 255, 0.08);
  background: transparent;
  color: var(--text);
  text-align: left;
  padding: 0.55rem 0.75rem;
  cursor: pointer;
  display: flex;
  justify-content: space-between;
  align-items: center;
  gap: 0.7rem;
  font-size: 0.9rem;
}

.voice-option:hover {
  background: rgba(255, 255, 255, 0.07);
}

.voice-option.active {
  background: rgba(255, 203, 119, 0.12);
  box-shadow: inset 3px 0 0 rgba(255, 203, 119, 0.9);
}

.voice-meta {
  color: #bfd9e9;
  font-size: 0.8rem;
}

body {
  --text: #eef6ff;
  --muted: #b8c9d7;
  --accent: #dcecff;
  --danger: #f2c9b7;
  background: #030813;
}

.scene-canvas {
  opacity: 1;
}

.backdrop {
  background-image:
    radial-gradient(circle at 50% 43%, rgba(236, 246, 255, 0.13), transparent 22%),
    linear-gradient(180deg, rgba(1, 4, 10, 0.06), rgba(1, 4, 10, 0.38));
}

.orb-shell {
  --speech-brightness: 0;
  position: fixed;
  left: 50%;
  top: 34vh;
  width: clamp(190px, 22vw, 285px);
  transform: translate(-50%, -50%);
  filter:
    drop-shadow(0 0 34px rgba(255, 255, 255, 0.34))
    drop-shadow(0 24px 54px rgba(0, 0, 0, 0.72));
}

.orb-core,
.state-thinking .orb-core,
.state-speaking .orb-core {
  opacity: 0.72;
  width: 78%;
  border: 0;
  background:
    radial-gradient(circle at 28% 30%, rgba(255, 255, 255, 0.62), transparent 39%),
    radial-gradient(circle at 70% 26%, rgba(226, 230, 224, 0.5), transparent 40%),
    radial-gradient(circle at 76% 72%, rgba(255, 255, 255, 0.46), transparent 42%),
    radial-gradient(circle at 25% 76%, rgba(196, 204, 194, 0.48), transparent 41%),
    radial-gradient(circle at 50% 50%, rgba(255, 255, 255, 0.28), rgba(255, 255, 255, 0.12) 48%, rgba(255, 255, 255, 0.03) 76%);
  box-shadow:
    inset 0 0 42px rgba(255, 255, 255, 0.26),
    inset 0 0 74px rgba(0, 0, 0, 0.32),
    0 0 86px rgba(255, 255, 255, 0.3);
  filter: brightness(calc(1 + var(--speech-brightness, 0)));
}

.state-thinking .orb-core,
.state-speaking .orb-core {
  animation: xray-swirl 22s linear infinite;
  background-size: 135% 135%, 130% 130%, 140% 140%, 132% 132%, 100% 100%;
  will-change: transform, filter, background-position;
}

.state-thinking .orb-core {
  animation-duration: 1.35s;
}

.state-speaking .orb-core {
  animation-duration: 5.4s;
}

.state-thinking .orb-glow,
.state-speaking .orb-glow {
  animation-duration: 1s;
}

.state-listening .orb-core,
.state-listening .orb-glow {
  animation: none;
}

.orb-speech-light {
  display: block;
  position: absolute;
  width: 78%;
  aspect-ratio: 1;
  border-radius: 50%;
  pointer-events: none;
  opacity: 0;
  background:
    radial-gradient(circle at 34% 28%, rgba(255, 255, 255, 0.62), rgba(255, 255, 255, 0.22) 36%, transparent 68%);
  box-shadow:
    0 0 38px rgba(255, 255, 255, 0.28),
    inset 0 0 34px rgba(255, 255, 255, 0.18);
  mix-blend-mode: screen;
  transition: opacity 120ms ease;
}

.speech-shimmer .orb-speech-light {
  opacity: var(--speech-brightness, 0);
}

.orb-ring {
  display: none;
}

.orb-glow {
  background: radial-gradient(circle, rgba(255, 255, 255, 0.24) 0%, rgba(199, 221, 255, 0.12) 40%, transparent 72%);
  opacity: calc(0.72 + var(--speech-brightness, 0));
  animation: halo 3.8s ease-in-out infinite;
}

.speech-shimmer {
  filter:
    drop-shadow(0 0 calc(34px + (var(--speech-brightness, 0) * 70px)) rgba(255, 255, 255, 0.38))
    drop-shadow(0 24px 54px rgba(0, 0, 0, 0.72));
}

.status {
  color: #eef6ff;
  text-shadow: 0 0 14px rgba(210, 230, 255, 0.42);
}

.controls {
  width: auto;
  border-color: transparent;
  background: transparent;
  backdrop-filter: none;
  box-shadow: none;
}

.voice-drawer {
  border-left-color: rgba(220, 236, 255, 0.2);
  background: rgba(8, 15, 26, 0.5);
  backdrop-filter: blur(18px) saturate(1.08);
  box-shadow: -20px 0 54px rgba(0, 0, 0, 0.62);
}

.voice-drawer-backdrop {
  background: rgba(2, 8, 18, 0.28);
}

.btn,
.drawer-close,
.voice-speed,
.model-picker,
.tts-picker,
.system-prompt,
.voice-direction,
.voice-group {
  border-color: rgba(220, 236, 255, 0.18);
  background: rgba(229, 240, 255, 0.08);
  color: #eef6ff;
}

.btn.primary {
  border-color: rgba(220, 236, 255, 0.28);
  background: rgba(4, 13, 24, 0.28);
}

.btn.active {
  border-color: rgba(255, 100, 100, 0.78);
  background: rgba(229, 240, 255, 0.16);
  color: #eef6ff;
}

#recordBtn {
  width: 112px;
}

#startStopBtn {
  width: 102px;
}

#historyBtn {
  width: 112px;
}

.control-side-hidden {
  visibility: hidden;
  pointer-events: none;
}

.recording-bubble {
  position: absolute;
  left: 50%;
  bottom: calc(100% + 0.7rem);
  transform: translateX(-50%);
  display: flex;
  gap: 0.45rem;
  padding: 0.45rem;
  border: 1px solid rgba(220, 236, 255, 0.2);
  border-radius: 8px;
  background: rgba(6, 14, 25, 0.72);
  backdrop-filter: blur(16px) saturate(1.08);
  box-shadow: 0 16px 38px rgba(0, 0, 0, 0.38);
}

.recording-bubble::after {
  content: "";
  position: absolute;
  left: 50%;
  top: 100%;
  width: 10px;
  height: 10px;
  transform: translate(-50%, -50%) rotate(45deg);
  border-right: 1px solid rgba(220, 236, 255, 0.2);
  border-bottom: 1px solid rgba(220, 236, 255, 0.2);
  background: rgba(6, 14, 25, 0.72);
}

.recording-bubble.hidden {
  display: none;
}

.pdf-popup {
  position: absolute;
  left: 50%;
  bottom: calc(100% + 0.7rem);
  transform: translateX(-50%);
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 0.55rem;
  padding: 0.65rem 0.85rem;
  border: 1px solid rgba(220, 236, 255, 0.2);
  border-radius: 8px;
  background: rgba(6, 14, 25, 0.72);
  backdrop-filter: blur(16px) saturate(1.08);
  box-shadow: 0 16px 38px rgba(0, 0, 0, 0.38);
  white-space: nowrap;
}

.pdf-popup::after {
  content: "";
  position: absolute;
  left: 50%;
  top: 100%;
  width: 10px;
  height: 10px;
  transform: translate(-50%, -50%) rotate(45deg);
  border-right: 1px solid rgba(220, 236, 255, 0.2);
  border-bottom: 1px solid rgba(220, 236, 255, 0.2);
  background: rgba(6, 14, 25, 0.72);
}

.pdf-popup.hidden {
  display: none;
}

.pdf-popup-text {
  font-size: 0.75rem;
  color: var(--muted);
  letter-spacing: 0.04em;
}

.pdf-popup-buttons {
  display: flex;
  gap: 0.45rem;
}

.bubble-btn {
  position: relative;
  z-index: 1;
  min-width: 92px;
  border: 1px solid rgba(220, 236, 255, 0.2);
  border-radius: 6px;
  background: rgba(229, 240, 255, 0.08);
  color: #eef6ff;
  cursor: pointer;
  font: inherit;
  font-size: 0.68rem;
  letter-spacing: 0.12em;
  padding: 0.42rem 0.58rem;
}

.bubble-btn:hover {
  background: rgba(229, 240, 255, 0.16);
}

.history-overlay {
  position: fixed;
  inset: 0;
  z-index: 9;
  display: grid;
  place-items: center;
  padding: 1rem;
  background: rgba(2, 8, 18, 0.46);
  backdrop-filter: blur(8px);
}

.history-overlay.hidden {
  display: none;
}

.history-panel {
  width: min(88vw, 780px);
  max-height: min(78dvh, 760px);
  display: flex;
  flex-direction: column;
  border: 1px solid rgba(220, 236, 255, 0.22);
  border-radius: 8px;
  background: rgba(8, 15, 26, 0.78);
  box-shadow: 0 28px 80px rgba(0, 0, 0, 0.62);
  overflow: hidden;
}

.history-head {
  display: flex;
  justify-content: flex-end;
  align-items: center;
  gap: 0.5rem;
  padding: 0.55rem;
  border-bottom: 1px solid rgba(220, 236, 255, 0.14);
}

.history-content {
  margin: 0;
  padding: 1rem;
  overflow: auto;
  color: #eef6ff;
  font-family: "Cascadia Mono", "SFMono-Regular", Consolas, monospace;
  font-size: 0.88rem;
  line-height: 1.55;
  text-align: left;
  white-space: pre-wrap;
}

.btn:hover,
.voice-option:hover {
  background: rgba(229, 240, 255, 0.16);
}

.system-prompt textarea,
.voice-direction textarea,
.model-picker select,
.tts-picker select {
  border-color: rgba(220, 236, 255, 0.24);
  background: rgba(1, 7, 18, 0.36);
  color: #eef6ff;
}

.voice-group-title {
  background: rgba(229, 240, 255, 0.08);
  color: #eef6ff;
}

.voice-option {
  color: #eef6ff;
  border-top-color: rgba(220, 236, 255, 0.12);
}

.voice-option.active {
  background: rgba(229, 240, 255, 0.18);
  box-shadow: inset 3px 0 0 rgba(238, 246, 255, 0.86);
}

.voice-meta,
.voice-status,
.text-btn {
  color: #b8c9d7;
}

@keyframes halo {
  0%,
  100% {
    transform: scale(1);
    opacity: 0.52;
  }
  50% {
    transform: scale(1.07);
    opacity: 0.76;
  }
}

@keyframes xray-swirl {
  0% {
    filter: contrast(1.16) brightness(1);
    background-position: 0% 0%, 100% 0%, 100% 100%, 0% 100%, center;
    transform: rotate(0deg);
  }
  50% {
    filter: contrast(1.34) brightness(1.12);
    background-position: 100% 18%, 82% 100%, 0% 82%, 18% 0%, center;
    transform: rotate(180deg);
  }
  100% {
    filter: contrast(1.16) brightness(1);
    background-position: 0% 0%, 100% 0%, 100% 100%, 0% 100%, center;
    transform: rotate(360deg);
  }
}

@media (max-width: 640px) {
  body {
    overflow: hidden;
  }

  .stage {
    min-height: 100dvh;
    padding: 1rem 0.75rem 6.5rem;
  }

  .orb-shell {
    width: clamp(150px, 54vw, 220px);
  }

  .controls {
    width: auto;
    gap: 0.5rem;
    padding: 0;
    bottom: max(0.75rem, env(safe-area-inset-bottom));
  }

  .control-buttons {
    grid-template-columns: 86px 78px 86px;
    gap: 0.8rem;
  }

  .btn {
    flex: 0 0 auto;
    min-width: 78px;
    padding: 0.42rem 0.92rem;
    font-size: 0.72rem;
  }

  #recordBtn,
  #historyBtn {
    width: 86px;
  }

  #startStopBtn {
    width: 78px;
  }

  .recording-bubble {
    flex-direction: column;
    gap: 0.35rem;
  }

  .history-panel {
    width: min(94vw, 520px);
    max-height: 76dvh;
  }

  .menu-toggle {
    top: max(0.7rem, env(safe-area-inset-top));
    right: max(0.7rem, env(safe-area-inset-right));
    width: 40px;
    height: 40px;
  }

  .status {
    font-size: 0.92rem;
  }

  .voice-drawer {
    width: 100vw;
    padding: 0.8rem 0.8rem 1rem;
    border-left: 0;
  }

  .system-prompt textarea {
    min-height: 108px;
    max-height: 28vh;
  }

  .voice-option {
    align-items: flex-start;
    flex-direction: column;
    gap: 0.25rem;
  }

  .orb-shell {
    top: 30dvh;
    width: clamp(150px, 48vw, 210px);
  }
}

@media (max-width: 380px) {
  .controls {
    width: auto;
  }

  .btn {
    font-size: 0.7rem;
    padding: 0.4rem 0.82rem;
  }

  .orb-shell {
    top: 28dvh;
    width: clamp(132px, 46vw, 180px);
  }
}
