:root {
  --bg: #070a12;
  --sidebar-bg: rgba(10, 14, 24, 0.94);
  --sidebar-w: min(380px, 92vw);
  --sidebar-col: var(--sidebar-w);
  --text: #f1f5f9;
  --muted: #94a3b8;
  --accent: #22d3ee;
  --accent-2: #a78bfa;
  --user-bg: rgba(167, 139, 250, 0.22);
  --bot-bg: rgba(34, 211, 238, 0.14);
  --font: system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
  --ease: cubic-bezier(0.4, 0, 0.2, 1);
  --safe-top: env(safe-area-inset-top, 0px);
  --safe-right: env(safe-area-inset-right, 0px);
  --safe-bottom: env(safe-area-inset-bottom, 0px);
  --safe-left: env(safe-area-inset-left, 0px);
  --voice-bottom: calc(1.25rem + var(--safe-bottom));
  --touch-min: 44px;
}

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

html {
  -webkit-text-size-adjust: 100%;
  text-size-adjust: 100%;
}

html,
body {
  margin: 0;
  height: 100%;
  height: 100dvh;
  overflow: hidden;
  overscroll-behavior: none;
  font-family: var(--font);
  background: var(--bg);
  color: var(--text);
  -webkit-tap-highlight-color: transparent;
  touch-action: manipulation;
}

body.sidebar-collapsed {
  --sidebar-col: 0px;
}

/* ── Grid: avatar area + sidebar (avatar always visible) ── */
.viewport {
  display: grid;
  grid-template-columns: minmax(0, 1fr) var(--sidebar-col);
  height: 100dvh;
  width: 100vw;
  overflow: hidden;
  transition: grid-template-columns 0.32s var(--ease);
}

.main-stage {
  position: relative;
  min-width: 0;
  min-height: 0;
  overflow: hidden;
}

.avatar-stage {
  position: absolute;
  inset: 0;
  background: radial-gradient(ellipse 80% 60% at 50% 40%, #141c2e 0%, var(--bg) 70%);
}

.avatar-3d {
  width: 100%;
  height: 100%;
}

.avatar-loader {
  position: absolute;
  inset: 0;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 1rem;
  background: rgba(7, 10, 18, 0.75);
  z-index: 5;
  transition: opacity 0.35s, visibility 0.35s;
}

.avatar-loader--hidden {
  opacity: 0;
  visibility: hidden;
  pointer-events: none;
}

.avatar-loader__spinner {
  width: 40px;
  height: 40px;
  border: 3px solid rgba(255, 255, 255, 0.12);
  border-top-color: var(--accent);
  border-radius: 50%;
  animation: spin 0.8s linear infinite;
}

@keyframes spin {
  to { transform: rotate(360deg); }
}

.avatar-loader p {
  margin: 0;
  font-size: 0.85rem;
  color: var(--muted);
}

.listen-ring {
  position: absolute;
  inset: -20px;
  border: 2px solid rgba(244, 114, 182, 0.55);
  border-radius: 50%;
  animation: ring-expand 1.4s ease-out infinite;
  pointer-events: none;
  z-index: 0;
}

.listen-ring[hidden] {
  display: none;
}

@keyframes ring-expand {
  0% { transform: scale(0.92); opacity: 0.9; }
  100% { transform: scale(1.08); opacity: 0; }
}

/* ── Bottom controls: centered in avatar column ── */
.stage-controls {
  position: absolute;
  left: 0;
  right: 0;
  bottom: 0;
  z-index: 20;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: flex-end;
  gap: 0.5rem;
  padding: 0 1rem var(--voice-bottom);
  pointer-events: none;
}

.stage-controls > * {
  pointer-events: auto;
}

.voice-fab-wrap {
  position: relative;
  width: 64px;
  height: 64px;
  flex-shrink: 0;
}

/* Glass caption above voice button */
.ai-caption {
  width: 100%;
  max-width: min(540px, calc(100% - 0.5rem));
  padding: 0.65rem 1rem;
  border-radius: 14px;
  background: rgba(15, 23, 42, 0.45);
  backdrop-filter: blur(16px) saturate(1.4);
  -webkit-backdrop-filter: blur(16px) saturate(1.4);
  border: 1px solid rgba(255, 255, 255, 0.12);
  box-shadow:
    0 8px 32px rgba(0, 0, 0, 0.35),
    inset 0 1px 0 rgba(255, 255, 255, 0.08);
  opacity: 0;
  visibility: hidden;
  transform: translateY(6px);
  transition:
    opacity 0.28s var(--ease),
    transform 0.28s var(--ease),
    visibility 0.28s;
}

.ai-caption--visible {
  opacity: 1;
  visibility: visible;
  transform: translateY(0);
}

.ai-caption__text {
  margin: 0;
  font-size: 0.92rem;
  line-height: 1.5;
  color: var(--text);
  text-align: center;
}

.status-bar {
  display: inline-flex;
  align-items: center;
  gap: 0.4rem;
  font-size: 0.72rem;
  font-weight: 500;
  color: var(--muted);
  pointer-events: none;
  transition: color 0.3s, opacity 0.2s;
}

body:has(.ai-caption--visible) .status-bar {
  opacity: 0;
}

.status-bar__dot {
  width: 6px;
  height: 6px;
  border-radius: 50%;
  background: var(--muted);
  transition: background 0.3s, box-shadow 0.3s;
}

.status-bar[data-state="listening"] { color: #f9a8d4; }
.status-bar[data-state="listening"] .status-bar__dot {
  background: #f472b6;
  box-shadow: 0 0 8px #f472b6;
  animation: blink 0.8s ease-in-out infinite;
}

.status-bar[data-state="thinking"] .status-bar__dot {
  background: #fbbf24;
  animation: blink 0.5s ease-in-out infinite;
}

.status-bar[data-state="speaking"] { color: var(--accent); }
.status-bar[data-state="speaking"] .status-bar__dot {
  background: var(--accent);
  box-shadow: 0 0 8px var(--accent);
}

@keyframes blink {
  0%, 100% { opacity: 1; }
  50% { opacity: 0.35; }
}

.voice-fab {
  position: relative;
  z-index: 1;
  width: 100%;
  height: 100%;
  border: none;
  border-radius: 50%;
  cursor: pointer;
  color: #fff;
  background: linear-gradient(145deg, #db2777, #7c3aed);
  box-shadow:
    0 8px 32px rgba(124, 58, 237, 0.45),
    0 0 0 1px rgba(255, 255, 255, 0.08) inset;
  display: flex;
  align-items: center;
  justify-content: center;
  transition: transform 0.15s var(--ease), box-shadow 0.2s;
}

.voice-fab:hover:not(:disabled) {
  transform: scale(1.05);
}

.voice-fab:active:not(:disabled) {
  transform: scale(0.96);
}

.voice-fab:disabled {
  opacity: 0.4;
  cursor: not-allowed;
}

.voice-fab.active {
  animation: mic-pulse 0.9s ease-in-out infinite;
}

@keyframes mic-pulse {
  0%, 100% {
    box-shadow:
      0 0 0 0 rgba(244, 114, 182, 0.5),
      0 8px 32px rgba(124, 58, 237, 0.45),
      0 0 0 1px rgba(255, 255, 255, 0.08) inset;
  }
  50% {
    box-shadow:
      0 0 0 14px rgba(244, 114, 182, 0),
      0 8px 32px rgba(124, 58, 237, 0.55),
      0 0 0 1px rgba(255, 255, 255, 0.08) inset;
  }
}

/* ── Sidebar edge tab ── */
.sidebar-edge {
  position: absolute;
  top: 50%;
  right: 0;
  transform: translateY(-50%);
  z-index: 30;
  width: 28px;
  height: 72px;
  padding: 0;
  border: none;
  border-radius: 10px 0 0 10px;
  background: var(--sidebar-bg);
  color: var(--muted);
  cursor: pointer;
  display: none;
  align-items: center;
  justify-content: center;
  backdrop-filter: blur(12px);
  pointer-events: auto;
}

.sidebar-edge:hover {
  color: var(--text);
}

body.sidebar-collapsed .sidebar-edge {
  display: flex;
}

.sidebar-edge__icon--closed {
  display: none;
}

body.sidebar-collapsed .sidebar-edge__icon--open {
  display: none;
}

body.sidebar-collapsed .sidebar-edge__icon--closed {
  display: block;
}

/* ── Sidebar column ── */
.sidebar {
  min-width: 0;
  width: var(--sidebar-w);
  max-width: 100%;
  display: flex;
  flex-direction: column;
  background: var(--sidebar-bg);
  backdrop-filter: blur(16px);
  border-left: 1px solid rgba(255, 255, 255, 0.06);
  overflow: hidden;
  opacity: 1;
  transition: opacity 0.28s var(--ease);
}

body.sidebar-collapsed .sidebar {
  opacity: 0;
  pointer-events: none;
  border-left-color: transparent;
}

.sidebar__header {
  flex-shrink: 0;
  display: flex;
  align-items: center;
  gap: 0.5rem;
  padding: 0.85rem 0.75rem;
  border-bottom: 1px solid rgba(255, 255, 255, 0.06);
}

.sidebar__title {
  flex: 1;
  margin: 0;
  font-size: 0.95rem;
  font-weight: 600;
  letter-spacing: -0.02em;
}

.user-menu__avatar {
  width: 28px;
  height: 28px;
  border-radius: 50%;
  object-fit: cover;
}

.icon-btn {
  width: var(--touch-min);
  height: var(--touch-min);
  min-width: var(--touch-min);
  min-height: var(--touch-min);
  padding: 0;
  border: none;
  border-radius: 8px;
  background: transparent;
  color: var(--muted);
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: center;
  transition: color 0.2s, background 0.2s;
}

.icon-btn:hover {
  color: var(--text);
  background: rgba(255, 255, 255, 0.06);
}

.icon-btn[aria-pressed="true"] {
  color: var(--accent);
}

.sidebar__panel {
  flex: 1;
  min-height: 0;
  display: flex;
  flex-direction: column;
}

.sidebar__panel[hidden] {
  display: none !important;
}

.chat__log {
  flex: 1;
  overflow-y: auto;
  padding: 1rem 0.85rem;
  display: flex;
  flex-direction: column;
  gap: 0.6rem;
}

.msg {
  max-width: 88%;
  padding: 0.6rem 0.8rem;
  border-radius: 14px;
  font-size: 0.875rem;
  line-height: 1.45;
  animation: msg-in 0.22s ease-out;
}

@keyframes msg-in {
  from { opacity: 0; transform: translateY(4px); }
  to { opacity: 1; transform: translateY(0); }
}

.msg p { margin: 0; }

.msg--user {
  align-self: flex-end;
  background: var(--user-bg);
  border-bottom-right-radius: 4px;
}

.msg--bot {
  align-self: flex-start;
  background: var(--bot-bg);
  border-bottom-left-radius: 4px;
}

.msg--error {
  align-self: stretch;
  max-width: 100%;
  background: rgba(239, 68, 68, 0.12);
  color: #fecaca;
}

.composer {
  flex-shrink: 0;
  display: flex;
  gap: 0.5rem;
  padding: 0.75rem;
  border-top: 1px solid rgba(255, 255, 255, 0.06);
}

.composer input {
  flex: 1;
  min-width: 0;
  padding: 0.65rem 0.85rem;
  border-radius: 10px;
  border: 1px solid rgba(255, 255, 255, 0.1);
  background: rgba(255, 255, 255, 0.04);
  color: var(--text);
  font: inherit;
  font-size: 1rem;
}

.composer input:focus {
  outline: none;
  border-color: rgba(34, 211, 238, 0.45);
}

.composer input::placeholder {
  color: var(--muted);
}

.btn-send {
  width: var(--touch-min);
  height: var(--touch-min);
  min-width: var(--touch-min);
  min-height: var(--touch-min);
  flex-shrink: 0;
  border: none;
  border-radius: 10px;
  cursor: pointer;
  color: #fff;
  background: linear-gradient(135deg, #0891b2, #7c3aed);
  display: flex;
  align-items: center;
  justify-content: center;
  transition: transform 0.15s, opacity 0.2s;
}

.btn-send:active:not(:disabled) {
  transform: scale(0.94);
}

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

.settings__body {
  flex: 1;
  overflow-y: auto;
  padding: 1rem 0.85rem;
  display: flex;
  flex-direction: column;
  gap: 1rem;
}

.field {
  display: flex;
  flex-direction: column;
  gap: 0.35rem;
  font-size: 0.8rem;
  color: var(--muted);
}

.field select {
  padding: 0.55rem 0.65rem;
  border-radius: 8px;
  border: 1px solid rgba(255, 255, 255, 0.1);
  background: rgba(255, 255, 255, 0.04);
  color: var(--text);
  font: inherit;
  font-size: 0.875rem;
}

.btn-text {
  margin-top: 0.5rem;
  padding: 0.55rem 0;
  border: none;
  background: none;
  color: var(--muted);
  font: inherit;
  font-size: 0.85rem;
  cursor: pointer;
  text-align: left;
  transition: color 0.2s;
}

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

.settings__back {
  flex-shrink: 0;
  padding: 0.85rem;
  border-top: 1px solid rgba(255, 255, 255, 0.06);
  text-align: center;
  color: var(--accent);
}

.settings__back:hover {
  color: #67e8f9;
}

/* ── Mobile chat FAB + backdrop (hidden on desktop) ── */
.mobile-chat-fab {
  display: none;
  position: absolute;
  top: calc(0.75rem + var(--safe-top));
  right: calc(0.75rem + var(--safe-right));
  z-index: 35;
  align-items: center;
  gap: 0.35rem;
  padding: 0.5rem 0.85rem 0.5rem 0.65rem;
  min-height: var(--touch-min);
  border: none;
  border-radius: 999px;
  background: rgba(15, 23, 42, 0.88);
  backdrop-filter: blur(12px);
  -webkit-backdrop-filter: blur(12px);
  color: var(--text);
  font: inherit;
  font-size: 0.85rem;
  font-weight: 600;
  cursor: pointer;
  box-shadow: 0 4px 20px rgba(0, 0, 0, 0.35);
  border: 1px solid rgba(255, 255, 255, 0.1);
}

.mobile-chat-fab__label {
  line-height: 1;
}

body:not(.sidebar-collapsed) .mobile-chat-fab {
  opacity: 0;
  pointer-events: none;
}

.sidebar-backdrop {
  display: none;
  position: fixed;
  inset: 0;
  z-index: 40;
  border: none;
  padding: 0;
  margin: 0;
  background: rgba(0, 0, 0, 0.55);
  cursor: pointer;
  -webkit-tap-highlight-color: transparent;
}

.sidebar-backdrop:not([hidden]) {
  display: block;
}

/* ── Mobile layout: full-screen avatar + bottom chat sheet ── */
@media (max-width: 768px) {
  .viewport {
    grid-template-columns: 1fr !important;
    grid-template-rows: 1fr !important;
  }

  .main-stage {
    position: relative;
    min-height: 0;
  }

  .stage-controls {
    padding-left: calc(1rem + var(--safe-left));
    padding-right: calc(1rem + var(--safe-right));
  }

  .voice-fab-wrap {
    width: 72px;
    height: 72px;
  }

  .ai-caption {
    max-width: calc(100% - 1rem);
    font-size: 0.88rem;
  }

  .ai-caption__text {
    font-size: 0.88rem;
  }

  .mobile-chat-fab {
    display: inline-flex;
  }

  .sidebar-edge {
    display: none !important;
  }

  .sidebar {
    position: fixed;
    left: 0;
    right: 0;
    bottom: 0;
    z-index: 50;
    width: 100% !important;
    max-width: none;
    height: min(72dvh, 520px);
    max-height: calc(100dvh - 4rem - var(--safe-top));
    border-left: none;
    border-top: 1px solid rgba(255, 255, 255, 0.1);
    border-radius: 16px 16px 0 0;
    box-shadow: 0 -12px 40px rgba(0, 0, 0, 0.45);
    transform: translateY(100%);
    transition: transform 0.32s var(--ease), opacity 0.28s var(--ease);
    opacity: 1;
    pointer-events: none;
  }

  body:not(.sidebar-collapsed) .sidebar {
    transform: translateY(0);
    pointer-events: auto;
  }

  body.sidebar-collapsed .sidebar {
    transform: translateY(100%);
    opacity: 0;
    pointer-events: none;
    border-top-color: transparent;
  }

  .sidebar__header {
    padding-top: calc(0.85rem + var(--safe-top));
    padding-left: calc(0.75rem + var(--safe-left));
    padding-right: calc(0.75rem + var(--safe-right));
  }

  .composer {
    padding-bottom: calc(0.75rem + var(--safe-bottom));
    padding-left: calc(0.75rem + var(--safe-left));
    padding-right: calc(0.75rem + var(--safe-right));
  }

  .chat__log {
    padding-left: calc(0.85rem + var(--safe-left));
    padding-right: calc(0.85rem + var(--safe-right));
    -webkit-overflow-scrolling: touch;
  }

  .btn-text {
    min-height: var(--touch-min);
    display: flex;
    align-items: center;
  }

  .field select {
    min-height: var(--touch-min);
    font-size: 1rem;
  }
}

/* Short landscape phones: shorter chat sheet */
@media (max-width: 768px) and (max-height: 500px) {
  .sidebar {
    height: min(55dvh, 280px);
  }

  .voice-fab-wrap {
    width: 56px;
    height: 56px;
  }
}

/* Very narrow */
@media (max-width: 380px) {
  .mobile-chat-fab__label {
    display: none;
  }

  .mobile-chat-fab {
    width: var(--touch-min);
    height: var(--touch-min);
    padding: 0;
    justify-content: center;
    border-radius: 50%;
  }
}
