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

:root {
  --bg:       #0d0d0d;
  --surface:  #1a1a1a;
  --surface2: #242424;
  --border:   #2e2e2e;
  --border2:  #3a3a3a;
  --text:     #f0f0f0;
  --sub:      #888;
  --sub2:     #555;
  --accent:   #0a84ff;
  --green:    #34c759;
  --red:      #ff3b30;
  --gold:     #c8a96e;
  --radius:   18px;
}

/* Light mode overrides */
body.light {
  --bg:       #ffffff;
  --surface:  #f2f2f2;
  --surface2: #e8e8e8;
  --border:   #d0d0d0;
  --border2:  #bbbbbb;
  --text:     #111111;
  --sub:      #555555;
  --sub2:     #999999;
  --gold:     #8b6914;
}
body.light .topbar {
  background: rgba(255,255,255,0.95);
}
body.light .placeholder {
  background: linear-gradient(160deg, #e8e8f0 0%, #d8d8e8 100%);
}
body.light .placeholder p { color: #666; }
body.light .msg.assistant .bubble {
  background: #f0f0f0;
  color: #111;
}

body {
  background: var(--bg);
  color: var(--text);
  font-family: -apple-system, "SF Pro Text", ui-sans-serif, system-ui, "Segoe UI", sans-serif;
  font-size: 17px;
  height: 100dvh;
  display: flex;
  flex-direction: column;
  overflow: hidden;
}

/* ════════════════════════
   TOP BAR
════════════════════════ */
.topbar {
  display: flex;
  align-items: center;
  gap: 12px;
  padding: 0 20px;
  height: 50px;
  background: rgba(13,13,13,0.9);
  backdrop-filter: blur(16px);
  border-bottom: 1px solid var(--border);
  flex-shrink: 0;
  z-index: 30;
}

.topbar-logo {
  display: flex;
  align-items: center;
  gap: 9px;
}
.topbar-logo .icon {
  width: 26px; height: 26px;
  background: linear-gradient(135deg, #8b6914, var(--gold));
  border-radius: 7px;
  display: flex; align-items: center; justify-content: center;
  font-size: 0.75rem;
  color: #fff;
  font-weight: 700;
  flex-shrink: 0;
}
.topbar-logo h1 {
  font-size: 0.88rem;
  font-weight: 600;
  color: var(--text);
  letter-spacing: -0.2px;
}
.topbar-spacer { flex: 1; }

.theme-btn {
  width: 38px; height: 38px;
  border-radius: 50%;
  border: 1px solid var(--border);
  background: var(--surface);
  color: var(--text);
  cursor: pointer;
  display: flex; align-items: center; justify-content: center;
  font-size: 1.1rem;
  transition: background .2s, border-color .2s;
  flex-shrink: 0;
}
.theme-btn:hover { background: var(--surface2); border-color: var(--border2); }
@keyframes blink { 0%,100%{opacity:1} 50%{opacity:.3} }

/* Model selector */
.model-select-wrap {
  display: flex;
  align-items: center;
  gap: 6px;
  flex-shrink: 0;
}
.model-select-wrap label {
  font-size: 0.72rem;
  color: var(--sub);
  white-space: nowrap;
}
#model-select {
  font-size: 0.78rem;
  padding: 4px 8px;
  border-radius: 8px;
  border: 1px solid var(--border);
  background: var(--surface);
  color: var(--text);
  cursor: pointer;
  outline: none;
  transition: border-color .2s;
}
#model-select:hover, #model-select:focus { border-color: var(--accent); }
#model-select optgroup { color: var(--sub); font-size: 0.72rem; }
#model-select option  { color: var(--text); background: var(--surface); }

/* ════════════════════════
   APP BODY
════════════════════════ */
.app-body {
  flex: 1;
  display: flex;
  flex-direction: column;
  overflow: hidden;
  min-height: 0;
}

/* ════════════════════════
   VIDEO PANEL (top, hidden by default)
════════════════════════ */
.video-panel {
  display: none;
  flex-shrink: 0;
  height: 38vh;
  min-height: 220px;
  max-height: 480px;
  position: relative;
  background: #000;
  border-bottom: 1px solid var(--border);
}
.video-panel.active { display: block; }

#avatar-video {
  width: 100%;
  height: 100%;
  object-fit: cover;
  display: none;
}

/* Placeholder */
.placeholder {
  position: absolute;
  inset: 0;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 12px;
  background: linear-gradient(160deg, #141420 0%, #0a0a14 100%);
}
.placeholder .ph-avatar {
  width: 80px; height: 80px;
  border-radius: 50%;
  background: linear-gradient(135deg, #1e1e30, #12121e);
  border: 2px solid rgba(200,169,110,.2);
  display: flex; align-items: center; justify-content: center;
  font-size: 2.2rem;
  color: var(--gold);
}
.placeholder p {
  font-size: 0.82rem;
  color: rgba(255,255,255,.4);
  text-align: center;
}

/* Timer badge */
.timer-badge {
  position: absolute;
  top: 12px; left: 12px;
  background: rgba(0,0,0,.55);
  backdrop-filter: blur(8px);
  color: rgba(255,255,255,.8);
  font-size: 0.72rem;
  font-weight: 500;
  padding: 4px 9px;
  border-radius: 7px;
  display: none;
  z-index: 5;
}

/* Speaking wave */
.speaking-badge {
  position: absolute;
  top: 12px; right: 12px;
  background: rgba(52,199,89,.12);
  border: 1px solid rgba(52,199,89,.35);
  backdrop-filter: blur(8px);
  color: var(--green);
  font-size: 0.7rem;
  font-weight: 600;
  padding: 4px 9px;
  border-radius: 20px;
  display: none;
  align-items: center;
  gap: 5px;
  z-index: 5;
}
.speaking-badge.active { display: flex; }
.wave { display: flex; align-items: center; gap: 2px; }
.wave span {
  display: block; width: 3px;
  background: var(--green);
  border-radius: 2px;
  animation: wave-bar 1s ease-in-out infinite;
}
.wave span:nth-child(1) { height: 7px;  animation-delay: 0s; }
.wave span:nth-child(2) { height: 12px; animation-delay: .15s; }
.wave span:nth-child(3) { height: 9px;  animation-delay: .3s; }
.wave span:nth-child(4) { height: 5px;  animation-delay: .45s; }
@keyframes wave-bar { 0%,100%{transform:scaleY(1)} 50%{transform:scaleY(1.6)} }

/* PRE-SESSION bar */
.pre-bar {
  position: absolute;
  bottom: 16px; left: 50%; transform: translateX(-50%);
  background: rgba(26,26,26,.95);
  backdrop-filter: blur(16px);
  border: 1px solid var(--border2);
  border-radius: 13px;
  padding: 8px 12px;
  display: flex;
  align-items: center;
  gap: 8px;
  z-index: 10;
  white-space: nowrap;
}

.sandbox-toggle {
  display: flex;
  align-items: center;
  gap: 5px;
  font-size: 0.75rem;
  color: var(--sub);
  cursor: pointer;
  padding: 4px 8px;
  border-radius: 8px;
  border: 1px solid var(--border);
  background: var(--surface2);
  user-select: none;
}
.sandbox-toggle input { accent-color: var(--accent); cursor: pointer; }

.divider-v { width: 1px; height: 24px; background: var(--border); }

.btn-start-main {
  background: var(--accent);
  color: #fff;
  border: none;
  border-radius: 9px;
  padding: 7px 16px;
  font-size: 0.82rem;
  font-weight: 600;
  cursor: pointer;
  transition: filter .15s, transform .1s;
}
.btn-start-main:hover  { filter: brightness(1.15); }
.btn-start-main:active { transform: scale(0.96); }
.btn-start-main:disabled { opacity: .4; cursor: default; }

/* IN-SESSION controls bar */
.session-bar {
  position: absolute;
  bottom: 16px; left: 50%; transform: translateX(-50%);
  background: rgba(20,20,20,.8);
  backdrop-filter: blur(16px);
  border: 1px solid rgba(255,255,255,.08);
  border-radius: 50px;
  padding: 6px 14px;
  display: none;
  align-items: center;
  gap: 6px;
  z-index: 10;
}
.session-bar.active { display: flex; }

.icon-btn {
  width: 36px; height: 36px;
  border-radius: 50%; border: none;
  display: flex; align-items: center; justify-content: center;
  cursor: pointer; font-size: 0.95rem;
  transition: filter .15s, transform .1s;
  flex-shrink: 0;
}
.icon-btn:hover  { filter: brightness(1.2); }
.icon-btn:active { transform: scale(0.92); }
.icon-btn.blue { background: var(--accent); color: #fff; }
.icon-btn.red  { background: var(--red); color: #fff; }
.icon-btn.gray { background: rgba(255,255,255,.12); color: rgba(255,255,255,.7); }

.session-bar .sep { width: 1px; height: 22px; background: rgba(255,255,255,.12); margin: 0 2px; }

.stop-all-btn {
  background: rgba(255,255,255,.07);
  color: rgba(255,255,255,.45);
  border: none;
  border-radius: 20px;
  padding: 4px 10px;
  font-size: 0.68rem;
  cursor: pointer;
  transition: all .15s;
  white-space: nowrap;
}
.stop-all-btn:hover { background: rgba(255,59,48,.25); color: #fff; }

/* ════════════════════════
   CHAT CENTER
════════════════════════ */
.chat-center {
  flex: 1;
  display: flex;
  flex-direction: column;
  overflow: hidden;
  min-height: 0;
}

.chat-area {
  flex: 1;
  display: flex;
  flex-direction: column;
  width: 100%;
  min-height: 0;
}

/* Messages */
.messages {
  flex: 1;
  overflow-y: auto;
  padding: 20px max(16px, calc(50% - 390px)) 8px;
  display: flex;
  flex-direction: column;
  gap: 16px;
  scrollbar-width: thin;
  scrollbar-color: var(--border) transparent;
  scrollbar-gutter: stable;
}
.messages::-webkit-scrollbar { width: 4px; }
.messages::-webkit-scrollbar-track { background: transparent; }
.messages::-webkit-scrollbar-thumb { background: var(--border); border-radius: 4px; }

.welcome {
  margin: auto;
  text-align: center;
  max-width: 320px;
  padding: 32px 0 16px;
}
.welcome .w-icon {
  font-size: 2.4rem;
  margin-bottom: 14px;
  display: block;
  color: var(--gold);
}
.welcome h3 { font-size: 1.25rem; font-weight: 600; margin-bottom: 8px; color: var(--text); }
.welcome p  { font-size: 0.95rem; color: var(--sub); line-height: 1.7; }

/* History divider */
.history-divider {
  text-align: center;
  font-size: 0.78rem;
  color: var(--sub);
  margin: 12px 0;
  opacity: 0.6;
}

/* Bubbles */
.msg { display: flex; gap: 10px; }
.msg.user      { flex-direction: row-reverse; }
.msg.assistant { flex-direction: row; }

.msg-icon {
  width: 28px; height: 28px;
  border-radius: 50%;
  background: var(--accent);
  color: #fff;
  font-size: 0.68rem;
  font-weight: 700;
  display: flex; align-items: center; justify-content: center;
  flex-shrink: 0;
  margin-top: 2px;
}
.msg.user .msg-icon { background: var(--surface2); color: var(--sub); }

.bubble {
  max-width: 80%;
  padding: 11px 16px;
  border-radius: 16px;
  font-size: 1rem;
  line-height: 1.7;
  white-space: pre-wrap;
  word-break: break-word;
}
.msg.user .bubble {
  background: var(--accent);
  color: #fff;
  border-bottom-right-radius: 5px;
}
.msg.assistant .bubble {
  background: var(--surface2);
  color: var(--text);
  border-bottom-left-radius: 5px;
}
.bubble.thinking { color: var(--sub); font-style: italic; }

/* Sources */
.sources-toggle {
  font-size: 0.78rem;
  color: var(--sub);
  background: none;
  border: none;
  cursor: pointer;
  padding: 2px 4px 2px 38px;
  display: block;
  text-align: left;
  margin-top: -4px;
}
.sources-toggle:hover { color: var(--accent); }

.sources {
  margin: 0 0 0 38px;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 10px;
  padding: 8px 12px;
  font-size: 0.82rem;
  line-height: 1.6;
  display: none;
  color: var(--text);
}
.sources.open { display: block; }
.src-item { padding: 4px 0; border-bottom: 1px solid var(--border); }
.src-item:last-child { border-bottom: none; padding-bottom: 0; }
.src-title { color: var(--text); font-weight: 600; }
.src-link  { color: var(--accent); text-decoration: none; }
.src-link:hover { text-decoration: underline; }
.src-sim {
  font-size: 0.7rem;
  color: var(--muted);
  background: rgba(255,255,255,0.07);
  border-radius: 4px;
  padding: 1px 5px;
  margin-left: 4px;
  vertical-align: middle;
  white-space: nowrap;
}

/* Media */
.media-card {
  margin: 4px 0 0 38px;
  display: flex; flex-direction: column; gap: 6px;
}
.yt-wrap {
  position: relative; width: 100%; padding-top: 56.25%;
  border-radius: 10px; overflow: hidden; background: #000;
}
.yt-wrap iframe { position: absolute; inset: 0; width: 100%; height: 100%; border: none; }
.media-links { display: flex; flex-wrap: wrap; gap: 5px; }
.media-btn {
  display: inline-flex; align-items: center; gap: 4px;
  padding: 5px 11px; border-radius: 7px; font-size: 0.78rem; font-weight: 500;
  text-decoration: none; border: 1px solid var(--border);
  background: var(--surface); color: var(--text);
  transition: background .15s, color .15s;
}
.media-btn:hover { background: var(--accent); color: #fff; border-color: var(--accent); }
.media-btn.sc { color: #f26f23; border-color: #f26f23; }
.media-btn.sc:hover { background: #f26f23; color: #fff; }
.sc-wrap {
  border-radius: 10px; overflow: hidden; width: 100%;
}
.sc-wrap iframe {
  width: 100%; height: 166px; border: none; display: block;
}
.mp3-wrap {
  display: flex; flex-direction: column; gap: 3px;
}
.mp3-label {
  font-size: 0.75rem; color: var(--text-muted, #888); font-weight: 500;
}
.mp3-wrap audio {
  width: 100%; border-radius: 8px;
  accent-color: var(--accent);
}

/* Streaming cursor */
.streaming-cursor::after {
  content: '▋';
  opacity: 1;
  animation: blink-cursor .7s step-end infinite;
}
@keyframes blink-cursor {
  0%, 100% { opacity: 1; }
  50%       { opacity: 0; }
}

/* ════════════════════════
   INPUT ZONE (Grok-style)
════════════════════════ */
.input-zone {
  padding: 8px max(16px, calc(50% - 390px)) 20px;
  flex-shrink: 0;
}

.input-bar {
  display: flex;
  align-items: flex-end;
  gap: 0;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 24px;
  padding: 10px 10px 10px 18px;
  transition: border-color .2s;
}
.input-bar:focus-within { border-color: var(--border2); }

textarea#question {
  flex: 1;
  background: transparent;
  border: none;
  outline: none;
  color: var(--text);
  font-size: 1.05rem;
  font-family: inherit;
  resize: none;
  line-height: 1.5;
  max-height: 120px;
  padding: 3px 0;
}
textarea#question::placeholder { color: var(--sub2); }

.input-actions {
  display: flex;
  align-items: center;
  gap: 4px;
  flex-shrink: 0;
  margin-left: 8px;
}

.action-btn {
  width: 36px; height: 36px;
  border-radius: 50%;
  border: none;
  cursor: pointer;
  display: flex; align-items: center; justify-content: center;
  transition: background .15s, color .15s, transform .1s;
  flex-shrink: 0;
}
.action-btn:active { transform: scale(0.92); }

.video-btn {
  width: 44px; height: 44px;
  background: transparent;
  color: var(--sub);
}
.video-btn:hover { background: var(--surface2); color: var(--text); }
.video-btn.active { background: rgba(10,132,255,.15); color: var(--accent); }
.video-btn svg { width: 24px; height: 24px; }

.send-btn {
  background: var(--accent);
  color: #fff;
}
.send-btn:hover { filter: brightness(1.12); }
.send-btn:disabled { opacity: .3; cursor: default; filter: none; }
.send-btn svg { width: 16px; height: 16px; }

/* Spinner */
@keyframes spin { to { transform: rotate(360deg); } }
.spinner {
  display: inline-block; width: 11px; height: 11px;
  border: 1.5px solid var(--border2);
  border-top-color: var(--sub);
  border-radius: 50%;
  animation: spin .6s linear infinite;
  vertical-align: middle; margin-right: 6px;
}
