:root {
  --bg: #0c0e14;
  --surface: #151922;
  --surface-2: #1e2433;
  --border: #2a3348;
  --text: #eef2ff;
  --muted: #8b95ad;
  --accent: #5b8def;
  --accent-2: #ff6b9d;
  --success: #34d399;
  --radius: 14px;
  --font: 'Outfit', system-ui, sans-serif;
}

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

html,
body {
  margin: 0;
  min-height: 100vh;
  font-family: var(--font);
  background: var(--bg);
  color: var(--text);
}

body {
  display: flex;
  flex-direction: column;
}

.header {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  gap: 1rem;
  padding: 0.85rem 1.5rem;
  background: var(--surface);
  border-bottom: 1px solid var(--border);
}

.btn-back-home {
  display: inline-flex;
  align-items: center;
  gap: 0.45rem;
  padding: 0.45rem 0.85rem;
  border-radius: 999px;
  border: 1px solid var(--border);
  background: var(--surface-2);
  color: var(--text);
  text-decoration: none;
  font-size: 0.8rem;
  font-weight: 600;
  transition: border-color 0.15s, background 0.15s;
  flex-shrink: 0;
}

.btn-back-home:hover {
  border-color: var(--accent);
  background: rgba(91, 141, 239, 0.12);
  color: var(--text);
}

.brand {
  display: flex;
  align-items: center;
  gap: 0.75rem;
  flex: 1;
}

.brand-icon {
  font-size: 1.75rem;
}

.brand h1 {
  margin: 0;
  font-size: 1.15rem;
}

.brand-sub {
  margin: 0;
  font-size: 0.75rem;
  color: var(--muted);
}

.badge {
  font-size: 0.75rem;
  padding: 0.35rem 0.75rem;
  border-radius: 999px;
  border: 1px solid var(--border);
  color: var(--muted);
}

.badge.ok {
  border-color: var(--success);
  color: var(--success);
}

.status {
  margin: 0;
  font-size: 0.75rem;
  color: var(--muted);
  width: 100%;
}

.layout {
  flex: 1;
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 1rem;
  padding: 1rem;
  max-width: 1400px;
  margin: 0 auto;
  width: 100%;
}

.layout:has(.panel-chat[hidden]) {
  grid-template-columns: 1fr;
  max-width: 720px;
}

.panel {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 1.25rem;
}

.panel h2 {
  margin: 0 0 1rem;
  font-size: 0.95rem;
  font-weight: 600;
}

.panel-avatar {
  display: flex;
  flex-direction: column;
}

.avatar-stage {
  flex: 1;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: flex-start;
  gap: 0.75rem;
  min-height: 480px;
  background: radial-gradient(ellipse at center, #1a2848 0%, var(--surface) 72%);
  border-radius: var(--radius);
  padding: 0.5rem 1rem 1rem;
}

.avatar {
  flex: 0 0 auto;
  width: 100%;
  display: flex;
  align-items: flex-start;
  justify-content: center;
  transition: transform 0.3s ease;
}

.avatar-frame {
  height: min(72vh, 560px);
  width: auto;
  max-width: 100%;
  aspect-ratio: 9 / 16;
  display: flex;
  align-items: flex-start;
  justify-content: center;
}

.avatar-sprite {
  display: block;
  width: 100%;
  height: 100%;
  object-fit: contain;
  object-position: center top;
  filter: drop-shadow(0 12px 28px rgba(0, 0, 0, 0.35));
  pointer-events: none;
  user-select: none;
}

.avatar.is-speaking .avatar-sprite {
  filter: drop-shadow(0 14px 32px rgba(91, 141, 239, 0.4));
}

.avatar[data-state='idle'],
.avatar[data-state='thinking'],
.avatar[data-state='speaking'] {
  animation: none;
}

.avatar[data-state='speaking'].speaking {
  animation: none;
}

@keyframes idle-float {
  0%,
  100% {
    transform: translateY(0);
  }
  50% {
    transform: translateY(-8px);
  }
}

@keyframes speak-bounce {
  0%,
  100% {
    transform: translateY(-4px);
  }
  50% {
    transform: translateY(-10px);
  }
}

.speech-bubble {
  position: relative;
  max-width: 90%;
  padding: 1rem 1.25rem;
  background: var(--surface-2);
  border: 2px solid var(--accent);
  border-radius: 16px;
  box-shadow: 0 8px 24px rgba(0, 0, 0, 0.3);
  opacity: 0;
  transform: translateY(8px);
  transition: opacity 0.3s, transform 0.3s;
}

.speech-bubble:not([hidden]),
.speech-bubble.visible {
  opacity: 1;
  transform: translateY(0);
}

.speech-bubble::before {
  content: '';
  position: absolute;
  top: -10px;
  left: 50%;
  transform: translateX(-50%);
  border: 10px solid transparent;
  border-bottom-color: var(--accent);
}

.speech-bubble p {
  margin: 0;
  font-size: 0.95rem;
  line-height: 1.5;
}

.start-overlay {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 0.75rem;
  width: 100%;
  max-width: 320px;
  margin-top: 0.5rem;
}

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

.btn-start {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 0.5rem;
  padding: 0.85rem 1.75rem;
  font-size: 1.05rem;
  background: linear-gradient(135deg, var(--accent), #7c3aed);
  color: #fff;
  border: none;
  border-radius: 999px;
  box-shadow: 0 8px 24px rgba(91, 141, 239, 0.35);
  transition: transform 0.15s, filter 0.15s;
}

.btn-start:hover:not(:disabled) {
  filter: brightness(1.08);
  transform: translateY(-2px);
}

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

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

.start-hint {
  margin: 0;
  font-size: 0.78rem;
  color: var(--muted);
  text-align: center;
  line-height: 1.45;
}

.mic-error {
  margin: 0 1rem 1rem;
  padding: 0.75rem 1rem;
  background: rgba(248, 113, 113, 0.12);
  border: 1px solid rgba(248, 113, 113, 0.4);
  border-radius: 10px;
  font-size: 0.85rem;
  color: #fca5a5;
  text-align: center;
  max-width: 640px;
  margin-left: auto;
  margin-right: auto;
}

.kb-hint--static {
  margin-top: 0.75rem;
  font-size: 0.78rem;
  color: var(--muted);
  font-style: italic;
}

.cam-wrap {
  position: relative;
  aspect-ratio: 16 / 10;
  border-radius: var(--radius);
  overflow: hidden;
  background: #000;
  border: 1px solid var(--border);
}

.cam-wrap.cam-live .cam-placeholder {
  display: none;
}

.cam-placeholder {
  position: absolute;
  inset: 0;
  z-index: 3;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 0.75rem;
  padding: 1.5rem;
  text-align: center;
  background: rgba(12, 14, 20, 0.92);
}

.cam-placeholder.hidden {
  display: none;
}

.cam-placeholder p {
  margin: 0;
  color: var(--muted);
  font-size: 0.9rem;
}

.cam-placeholder-hint {
  font-size: 0.78rem !important;
  max-width: 260px;
}

.btn-cam-start {
  padding: 0.75rem 1.5rem;
  font-size: 1rem;
}

#webcam {
  background: #111;
}

#webcam,
#overlay {
  position: absolute;
  inset: 0;
  width: 100%;
  height: 100%;
  object-fit: cover;
  transform: scaleX(-1);
}

#overlay {
  pointer-events: none;
}

.panel-cam .cam-header-row {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 0.75rem;
  margin-bottom: 1rem;
}

.panel-cam .cam-header-row h2 {
  margin: 0;
}

.btn-close-cam {
  flex-shrink: 0;
  color: var(--danger);
  border-color: rgba(248, 113, 113, 0.45);
}

.btn-close-cam:hover {
  background: rgba(248, 113, 113, 0.12);
}

.cam-mic-bar {
  position: absolute;
  z-index: 4;
  top: 0;
  left: 0;
  right: 0;
  display: flex;
  justify-content: center;
  padding: 0.65rem;
  background: linear-gradient(rgba(0, 0, 0, 0.72), transparent);
  pointer-events: auto;
}

.cam-mic-bar .cam-ctrl-mic {
  min-width: min(92%, 320px);
  padding: 0.65rem 1rem;
  font-size: 0.85rem;
}

.cam-ctrl-btn {
  flex: 1;
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 0.4rem;
  padding: 0.55rem 0.75rem;
  border: 2px solid rgba(255, 255, 255, 0.25);
  border-radius: 10px;
  background: rgba(30, 36, 51, 0.9);
  color: #fff;
  font-family: inherit;
  font-size: 0.78rem;
  font-weight: 600;
  cursor: pointer;
  transition: border-color 0.15s, background 0.15s, transform 0.15s;
}

.cam-ctrl-mic.hovered {
  border-color: var(--warning);
  background: rgba(251, 191, 36, 0.2);
  transform: scale(1.03);
}

.cam-ctrl-mic.pressing {
  border-color: var(--success);
  background: rgba(52, 211, 153, 0.25);
  box-shadow: 0 0 0 3px rgba(52, 211, 153, 0.35);
}

.cam-ctrl-mic.listening {
  border-color: var(--accent-2);
  background: rgba(255, 107, 157, 0.3);
  animation: pulse-mic 0.8s infinite;
}

.cam-ctrl-icon {
  font-size: 1rem;
  line-height: 1;
}

.cam-hint {
  margin: 0.75rem 0 0;
  font-size: 0.78rem;
  color: var(--muted);
}

.cam-error {
  margin: 0.75rem 0 0;
  padding: 0.75rem 1rem;
  background: rgba(248, 113, 113, 0.12);
  border: 1px solid rgba(248, 113, 113, 0.4);
  border-radius: 10px;
  font-size: 0.78rem;
  line-height: 1.45;
  color: #fca5a5;
  white-space: pre-wrap;
  word-break: break-word;
}

.cam-env-details {
  margin-top: 0.5rem;
  font-size: 0.75rem;
  color: var(--muted);
}

.cam-env {
  margin: 0.35rem 0 0;
  font-size: 0.7rem;
  white-space: pre-wrap;
  color: var(--muted);
  background: var(--bg);
  padding: 0.5rem;
  border-radius: 8px;
}

.panel-chat {
  display: flex;
  flex-direction: column;
  min-height: 0;
}

.chat-log {
  flex: 1;
  min-height: 200px;
  max-height: 320px;
  overflow-y: auto;
  display: flex;
  flex-direction: column;
  gap: 0.65rem;
  padding: 0.75rem;
  background: var(--bg);
  border-radius: var(--radius);
  border: 1px solid var(--border);
  margin-bottom: 0.75rem;
}

.chat-msg {
  padding: 0.6rem 0.75rem;
  border-radius: 10px;
  font-size: 0.85rem;
  line-height: 1.45;
}

.chat-msg--user {
  align-self: flex-end;
  background: rgba(91, 141, 239, 0.2);
  border: 1px solid rgba(91, 141, 239, 0.35);
  max-width: 85%;
}

.chat-msg--bot {
  align-self: flex-start;
  background: rgba(255, 107, 157, 0.12);
  border: 1px solid rgba(255, 107, 157, 0.25);
  max-width: 90%;
}

.chat-role {
  display: block;
  font-size: 0.65rem;
  font-weight: 600;
  text-transform: uppercase;
  letter-spacing: 0.05em;
  color: var(--muted);
  margin-bottom: 0.25rem;
}

.chat-msg p {
  margin: 0;
}

.chat-input-row {
  display: flex;
  gap: 0.5rem;
  margin-bottom: 0.5rem;
}

.chat-input-row input {
  flex: 1;
  padding: 0.65rem 0.85rem;
  border-radius: 10px;
  border: 1px solid var(--border);
  background: var(--bg);
  color: var(--text);
  font-family: inherit;
  font-size: 0.9rem;
}

.btn {
  border: none;
  border-radius: 10px;
  font-family: inherit;
  font-weight: 600;
  cursor: pointer;
  transition: background 0.15s, transform 0.1s;
}

.btn-primary {
  padding: 0.65rem 1.1rem;
  background: var(--accent);
  color: #fff;
}

.btn-primary:hover {
  filter: brightness(1.08);
}

.btn-mic {
  padding: 0.65rem 0.85rem;
  background: var(--surface-2);
  font-size: 1.1rem;
}

.btn-mic.listening {
  background: var(--accent-2);
  animation: pulse-mic 0.8s infinite;
}

@keyframes pulse-mic {
  50% {
    opacity: 0.7;
  }
}

.btn-ghost {
  background: transparent;
  color: var(--muted);
  border: 1px solid var(--border);
}

.btn-sm {
  font-size: 0.78rem;
  padding: 0.4rem 0.75rem;
  align-self: flex-start;
}

.kb-hint {
  margin-top: 0.75rem;
  font-size: 0.78rem;
  color: var(--muted);
}

.kb-hint summary {
  cursor: pointer;
  color: var(--accent);
}

.kb-hint ul {
  margin: 0.5rem 0;
  padding-left: 1.2rem;
}

.kb-edit {
  margin: 0.5rem 0 0;
}

code {
  font-size: 0.85em;
  background: var(--bg);
  padding: 0.1rem 0.35rem;
  border-radius: 4px;
}

@media (max-width: 900px) {
  .layout {
    grid-template-columns: 1fr;
  }

  .avatar-stage {
    min-height: 360px;
  }

  .avatar-frame {
    height: min(55vh, 480px);
  }
}
