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

:root {
  --bg: #0f1117;
  --surface: #1a1d27;
  --surface2: #22263a;
  --primary: #6c5ce7;
  --primary-glow: rgba(108, 92, 231, 0.4);
  --green: #00cec9;
  --green-glow: rgba(0, 206, 201, 0.4);
  --red: #ff6b6b;
  --text: #e4e6f0;
  --text-dim: #8b8fa8;
  --radius: 16px;
  --radius-sm: 10px;
}

body {
  font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, sans-serif;
  background: var(--bg);
  color: var(--text);
  min-height: 100vh;
  display: flex;
  justify-content: center;
  align-items: center;
  overflow-x: hidden;
}

body::before {
  content: '';
  position: fixed;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
  background: radial-gradient(ellipse at 50% 0%, rgba(108, 92, 231, 0.08) 0%, transparent 60%);
  pointer-events: none;
  z-index: 0;
}

.container {
  position: relative;
  z-index: 1;
  width: 100%;
  max-width: 440px;
  padding: 24px;
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 24px;
}

.header {
  text-align: center;
}

.header h1 {
  font-size: 1.6rem;
  font-weight: 700;
  background: linear-gradient(135deg, var(--primary), var(--green));
  -webkit-background-clip: text;
  -webkit-text-fill-color: transparent;
  background-clip: text;
}

.subtitle {
  font-size: 0.85rem;
  color: var(--text-dim);
  margin-top: 4px;
  display: block;
}

/* Avatar & Animation */
.avatar-section {
  position: relative;
  width: 200px;
  height: 200px;
  display: flex;
  align-items: center;
  justify-content: center;
}

.avatar {
  width: 160px;
  height: 160px;
  border-radius: 50%;
  background: #ffffff;
  display: flex;
  align-items: center;
  justify-content: center;
  position: relative;
  overflow: hidden;
  isolation: isolate;
  transform: translateZ(0);
  backface-visibility: hidden;
}

/* ── Eagle image stack ── */
.eagle-stack {
  position: absolute;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
  z-index: 2;
}

.eagle-frame {
  position: absolute;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
  object-fit: contain;
  object-position: center center;
  display: none;
  pointer-events: none;
  will-change: transform;
}

.eagle-frame.active {
  display: block;
}

/* ── Glow ring ── */
.eagle-glow {
  position: absolute;
  inset: -12px;
  border-radius: 50%;
  border: 3px solid transparent;
  transition: all 0.5s ease;
  z-index: 1;
  pointer-events: none;
}

/* ── Audio waves (listening) ── */
.audio-waves {
  position: absolute;
  inset: -20px;
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 6px;
  z-index: 1;
  pointer-events: none;
  opacity: 0;
  transition: opacity 0.3s ease;
}

.audio-waves .wave {
  display: block;
  width: 4px;
  height: 12px;
  background: var(--primary);
  border-radius: 2px;
  transform-origin: center;
}

.audio-waves.active .wave {
  animation: waveJump 0.6s ease-in-out infinite;
}
.audio-waves.active .wave:nth-child(2) { animation-delay: 0.15s; }
.audio-waves.active .wave:nth-child(3) { animation-delay: 0.3s; }

@keyframes waveJump {
  0%, 100% { transform: scaleY(0.4); opacity: 0.4; }
  50%      { transform: scaleY(1.4); opacity: 1; }
}

/* ── State: idle (breathing + blink) ── */
.avatar.state-idle .eagle-frame.active {
  /* breathing driven by JS — see avatar.js */
}
.avatar.state-idle .eagle-glow {
  border-color: rgba(108,92,231,0.15);
}

@keyframes eagleBreathe {
  0%, 100% { transform: scale(1); }
  50%      { transform: scale(1.03); }
}

/* ── State: listening ── */
.avatar.state-listening .eagle-glow {
  border-color: var(--primary);
  box-shadow: 0 0 24px 4px var(--primary-glow);
  animation: glowPulse 1.2s ease-in-out infinite;
}
.avatar.state-listening .eagle-frame.active {
  /* breathing driven by JS — see avatar.js */
}
.avatar.state-listening .audio-waves {
  opacity: 1;
}
.avatar.state-listening .audio-waves .wave {
  animation-play-state: running;
}

@keyframes glowPulse {
  0%, 100% { box-shadow: 0 0 16px 2px var(--primary-glow); }
  50%      { box-shadow: 0 0 36px 8px var(--primary-glow); }
}

/* ── State: thinking ── */
.avatar.state-processing .eagle-glow {
  border-color: #ffc107;
  box-shadow: 0 0 20px 4px rgba(255,193,7,0.3);
}
.avatar.state-processing .eagle-frame.active {
  animation: eagleThink 2s ease-in-out infinite;
}
.avatar.state-processing .audio-waves {
  opacity: 0;
}

@keyframes eagleThink {
  0%   { transform: translateX(0) rotate(0deg); }
  25%  { transform: translateX(4px) rotate(1.5deg); }
  75%  { transform: translateX(-4px) rotate(-1.5deg); }
  100% { transform: translateX(0) rotate(0deg); }
}

/* ── State: speaking ── */
.avatar.state-speaking .eagle-glow {
  border-color: var(--green);
  box-shadow: 0 0 24px 4px var(--green-glow);
  animation: glowPulseSpeaking 0.6s ease-in-out infinite;
}
.avatar.state-speaking .eagle-frame {
  animation: none !important;
}
.avatar.state-speaking .audio-waves {
  opacity: 0;
}

@keyframes glowPulseSpeaking {
  0%, 100% { box-shadow: 0 0 16px 2px var(--green-glow); }
  50%      { box-shadow: 0 0 40px 10px var(--green-glow); }
}

.status-badge {
  position: absolute;
  bottom: 0;
  left: 50%;
  transform: translateX(-50%);
  background: var(--surface2);
  padding: 6px 16px;
  border-radius: 20px;
  font-size: 0.78rem;
  font-weight: 600;
  color: var(--text-dim);
  white-space: nowrap;
  transition: all 0.3s;
}

.status-badge.listening {
  color: var(--primary);
  border: 1px solid var(--primary);
}

.status-badge.speaking {
  color: var(--green);
  border: 1px solid var(--green);
}

.status-badge.processing {
  color: #ffc107;
  border: 1px solid #ffc107;
}

/* Conversation Panel */
.conversation-panel {
  width: 100%;
  max-height: 280px;
  overflow-y: auto;
  background: var(--surface);
  border-radius: var(--radius);
  padding: 16px;
  display: flex;
  flex-direction: column;
  gap: 12px;
}

.welcome-message {
  text-align: center;
  color: var(--text-dim);
  font-size: 0.88rem;
  line-height: 1.6;
  padding: 12px 0;
}

.welcome-message strong {
  color: var(--primary);
}

.bubble {
  max-width: 85%;
  padding: 10px 14px;
  border-radius: var(--radius-sm);
  font-size: 0.88rem;
  line-height: 1.5;
  animation: fadeInUp 0.3s ease;
}

.bubble.user {
  align-self: flex-end;
  background: var(--primary);
  color: #fff;
  border-bottom-right-radius: 4px;
}

.bubble.tutor {
  align-self: flex-start;
  background: var(--surface2);
  color: var(--text);
  border-bottom-left-radius: 4px;
}

.bubble .label {
  font-size: 0.7rem;
  font-weight: 700;
  text-transform: uppercase;
  letter-spacing: 1px;
  opacity: 0.7;
  margin-bottom: 4px;
}

@keyframes fadeInUp {
  from { opacity: 0; transform: translateY(8px); }
  to   { opacity: 1; transform: translateY(0); }
}

/* Controls */
.controls {
  display: flex;
  gap: 12px;
}

.btn {
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 14px 32px;
  border: none;
  border-radius: 50px;
  font-size: 1rem;
  font-weight: 600;
  cursor: pointer;
  transition: all 0.2s;
}

.btn-icon {
  font-size: 1.2rem;
}

.btn-start {
  background: var(--primary);
  color: #fff;
}

.btn-start:hover {
  background: #5a4bd1;
  transform: translateY(-1px);
  box-shadow: 0 4px 16px var(--primary-glow);
}

.btn-stop {
  background: var(--red);
  color: #fff;
}

.btn-stop:hover {
  background: #e55a5a;
  transform: translateY(-1px);
}

.btn-send {
  background: var(--green);
  color: #000;
  padding: 14px 24px;
}

.btn-send:hover {
  background: #00b5b0;
  transform: translateY(-1px);
}

/* VAD Indicator */
.vad-indicator {
  text-align: center;
  font-size: 0.78rem;
  font-weight: 600;
  padding: 4px 12px;
  border-radius: 12px;
  transition: all 0.3s;
  width: fit-content;
  margin: 0 auto;
}

.vad-indicator.idle {
  color: var(--text-dim);
  background: transparent;
}

.vad-indicator.speaking {
  color: var(--primary);
  background: rgba(108, 92, 231, 0.15);
  animation: vadPulse 1.5s ease-in-out infinite;
}

.vad-indicator.waiting {
  color: #ffc107;
  background: rgba(255, 193, 7, 0.1);
}

.vad-indicator.sent {
  color: var(--green);
  background: rgba(0, 206, 201, 0.15);
}

@keyframes vadPulse {
  0%, 100% { opacity: 1; }
  50% { opacity: 0.5; }
}

/* Mic Level Meter */
.mic-level {
  width: 100%;
  height: 4px;
  background: var(--surface2);
  border-radius: 2px;
  overflow: hidden;
  opacity: 0.3;
  transition: opacity 0.3s;
}

.mic-level.active {
  opacity: 1;
}

.mic-bar {
  height: 100%;
  width: 0%;
  background: linear-gradient(90deg, var(--primary), var(--green));
  border-radius: 2px;
  transition: width 0.1s ease;
}

/* Connection Dot */
.connection-dot {
  position: fixed;
  top: 16px;
  right: 16px;
  width: 10px;
  height: 10px;
  border-radius: 50%;
  background: var(--red);
  z-index: 10;
  transition: background 0.3s;
}

.connection-dot.connected {
  background: var(--green);
  box-shadow: 0 0 8px var(--green-glow);
}

/* Scrollbar */
.conversation-panel::-webkit-scrollbar {
  width: 4px;
}
.conversation-panel::-webkit-scrollbar-thumb {
  background: var(--surface2);
  border-radius: 2px;
}

/* ── Test Panel ── */
.test-panel,
.debug-panel {
  width: 100%;
  background: var(--surface);
  border-radius: var(--radius-sm);
  padding: 10px 14px;
  color: var(--text-dim);
  font-size: 0.82rem;
}

.test-panel summary,
.debug-panel summary {
  cursor: pointer;
  font-weight: 600;
  color: var(--text-dim);
  padding: 4px 0;
}

.test-buttons {
  display: flex;
  flex-wrap: wrap;
  gap: 8px;
  margin-top: 8px;
}

.btn-test {
  padding: 8px 16px;
  border: 1px solid var(--surface2);
  border-radius: 20px;
  background: var(--surface2);
  color: var(--text);
  font-size: 0.78rem;
  font-weight: 600;
  cursor: pointer;
  transition: all 0.2s;
}

.btn-test:hover {
  background: var(--primary);
  border-color: var(--primary);
}

.btn-test-listen:hover { background: var(--primary); border-color: var(--primary); }
.btn-test-think:hover { background: #e6a800; border-color: #e6a800; }
.btn-test-speak:hover { background: var(--green); border-color: var(--green); color: #000; }
.btn-test-stop:hover { background: var(--red); border-color: var(--red); }

.debug-panel #debugContent {
  font-family: monospace;
  font-size: 0.72rem;
  line-height: 1.6;
  margin-top: 6px;
}

.debug-panel #debugContent span {
  color: var(--green);
}

.offset-row {
  display: flex;
  align-items: center;
  gap: 6px;
  margin-bottom: 4px;
}

.offset-hdr {
  display: flex;
  align-items: center;
  gap: 6px;
  margin-bottom: 2px;
}

.offset-hdr span {
  font-size: 0.6rem;
  color: var(--text-dim);
  text-align: center;
}

.offset-hdr span:first-child { width: 32px; text-align: left; }
.offset-hdr span:nth-child(2),
.offset-hdr span:nth-child(3),
.offset-hdr span:nth-child(4) { flex: 1; }
.offset-hdr span:nth-child(5) { width: 78px; }
.offset-hdr span:last-child { width: 62px; }

.offset-row label {
  width: 32px;
  font-size: 0.7rem;
  color: var(--text-dim);
  flex-shrink: 0;
}

.offset-row input[type="range"] {
  flex: 1;
  accent-color: var(--primary);
  height: 4px;
  min-width: 0;
}

.offset-row span {
  width: 52px;
  text-align: right;
  font-size: 0.65rem;
  flex-shrink: 0;
}

.offset-grid {
  display: flex;
  flex-direction: column;
}

.nudge-group {
  display: flex;
  gap: 1px;
  flex-shrink: 0;
}

.nudge-btn {
  width: 18px;
  height: 18px;
  padding: 0;
  border: 1px solid var(--surface2);
  border-radius: 3px;
  background: var(--surface);
  color: var(--text-dim);
  font-size: 0.65rem;
  font-weight: 700;
  cursor: pointer;
  line-height: 16px;
  text-align: center;
}

.nudge-btn:hover {
  background: var(--primary);
  color: #fff;
  border-color: var(--primary);
}
