/* ============================================================
   chat.css — ApasBot chat UI
   Design tokens match cdn/auth.css so the auth flow feels
   seamless. Import auth.css FIRST if sharing a bundle,
   otherwise the :root block here is self-contained.
   ============================================================ */

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

/* ── Design tokens — dark (default) ──────────────────────── */
:root {
  /* surfaces */
  --bg:          #0d0f17;
  --surface:     #111318;
  --surface2:    #161820;
  --border:      #1e2235;
  --border2:     #2a2e45;

  /* text */
  --text:        #e8eaf0;
  --text2:       #a0a8c8;
  --text3:       #545a7a;

  /* accent */
  --accent:      #7ecfff;
  --accent-dim:  rgba(126,207,255,0.10);
  --accent-glow: rgba(126,207,255,0.18);

  /* states */
  --error:       #f87171;
  --error-dim:   rgba(248,113,113,0.10);
  --success:     #4ade80;
  --warning:     #facc15;

  /* user bubble */
  --bubble-bg:   #1a2035;
  --bubble-text: #e8eaf0;

  /* bot icon */
  --bot-bg:      #192030;
  --bot-text:    #7ecfff;

  /* scrollbar */
  --scroll-track: #111318;
  --scroll-thumb: #2a2e45;

  /* sidebar */
  --sidebar-w:   260px;
  --topbar-h:    52px;

  /* fonts */
  --font-ui:     'Syne', system-ui, sans-serif;
  --font-body:   'Inter', system-ui, sans-serif;
  --font-mono:   'JetBrains Mono', monospace;

  /* misc */
  --radius:      10px;
  --radius-sm:   6px;
  --shadow:      0 4px 24px rgba(0,0,0,0.5);
}

/* ── Light theme overrides ────────────────────────────────── */
[data-theme="light"] {
  --bg:          #f2f4f9;
  --surface:     #ffffff;
  --surface2:    #f8f9fc;
  --border:      #dde1ee;
  --border2:     #c8cedf;
  --text:        #1a1d2e;
  --text2:       #5a6080;
  --text3:       #9098b8;
  --accent:      #1a6fa8;
  --accent-dim:  rgba(26,111,168,0.07);
  --accent-glow: rgba(26,111,168,0.14);
  --error:       #dc2626;
  --error-dim:   rgba(220,38,38,0.07);
  --bubble-bg:   #e8eaf6;
  --bubble-text: #1a1d2e;
  --bot-bg:      #e8f4ff;
  --bot-text:    #1a6fa8;
  --scroll-track: #f2f4f9;
  --scroll-thumb: #c8cedf;
  --shadow:      0 4px 24px rgba(0,0,0,0.10);
}

/* ── Base ─────────────────────────────────────────────────── */
html, body {
  height: 100%;
  overflow: hidden;
  font-family: var(--font-body);
  background: var(--bg);
  color: var(--text);
  font-size: 15px;
  line-height: 1.6;
  -webkit-font-smoothing: antialiased;
}

/* ── Scrollbar ────────────────────────────────────────────── */
::-webkit-scrollbar         { width: 5px; }
::-webkit-scrollbar-track   { background: var(--scroll-track); }
::-webkit-scrollbar-thumb   { background: var(--scroll-thumb); border-radius: 4px; }

/* ── App shell ────────────────────────────────────────────── */
#app {
  display: flex;
  height: 100vh;
  overflow: hidden;
  position: relative;
}

/* ════════════════════════════════════════════════════════════
   SIDEBAR
   ════════════════════════════════════════════════════════════ */
#sidebar {
  width: var(--sidebar-w);
  flex-shrink: 0;
  background: var(--surface);
  border-right: 1px solid var(--border);
  display: flex;
  flex-direction: column;
  overflow: hidden;
  transition: width 0.22s ease, transform 0.22s ease;
  z-index: 50;
}

#sidebar.collapsed {
  width: 0;
  border-right: none;
}

.sidebar-top {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 0 14px;
  height: var(--topbar-h);
  border-bottom: 1px solid var(--border);
  flex-shrink: 0;
}

.brand {
  font-family: var(--font-ui);
  font-size: 1.15rem;
  font-weight: 800;
  color: var(--accent);
  letter-spacing: -0.02em;
  white-space: nowrap;
}

.brand em {
  font-style: normal;
  opacity: 0.55;
}

.new-chat-btn {
  margin: 10px 10px 6px;
  padding: 9px 14px;
  background: var(--accent-dim);
  border: 1px solid var(--border2);
  border-radius: var(--radius-sm);
  color: var(--accent);
  font-family: var(--font-ui);
  font-size: 0.82rem;
  font-weight: 600;
  cursor: pointer;
  display: flex;
  align-items: center;
  gap: 7px;
  transition: background 0.15s, border-color 0.15s;
  white-space: nowrap;
  flex-shrink: 0;
}

.new-chat-btn:hover {
  background: var(--accent-glow);
  border-color: var(--accent);
}

.new-chat-btn i { font-size: 16px; }

/* Conversation list */
#conv-list {
  flex: 1;
  overflow-y: auto;
  padding-bottom: 12px;
}

.conv-group-label {
  padding: 10px 14px 4px;
  font-size: 0.68rem;
  font-weight: 600;
  color: var(--text3);
  text-transform: uppercase;
  letter-spacing: 0.08em;
}

.conv-item {
  display: flex;
  align-items: center;
  gap: 6px;
  padding: 8px 10px 8px 14px;
  border-radius: var(--radius-sm);
  margin: 1px 6px;
  cursor: pointer;
  border: 1px solid transparent;
  transition: background 0.12s, border-color 0.12s;
}

.conv-item:hover {
  background: var(--accent-dim);
}

.conv-item.active {
  background: var(--accent-dim);
  border-color: var(--border2);
}

.conv-title {
  flex: 1;
  font-size: 0.82rem;
  color: var(--text2);
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
  min-width: 0;
}

.conv-item.active .conv-title {
  color: var(--text);
}

.conv-del {
  opacity: 0;
  flex-shrink: 0;
  font-size: 14px;
  padding: 3px;
  color: var(--text3);
  transition: opacity 0.12s, color 0.12s;
}

.conv-item:hover .conv-del { opacity: 1; }
.conv-del:hover { color: var(--error) !important; }

/* Mobile sidebar overlay */
#sidebar-overlay {
  display: none;
  position: fixed;
  inset: 0;
  background: rgba(0,0,0,0.55);
  z-index: 49;
}

#sidebar-overlay.show { display: block; }

/* ════════════════════════════════════════════════════════════
   MAIN COLUMN
   ════════════════════════════════════════════════════════════ */
#main {
  flex: 1;
  display: flex;
  flex-direction: column;
  overflow: hidden;
  min-width: 0;
}

/* ── Topbar ───────────────────────────────────────────────── */
#topbar {
  height: var(--topbar-h);
  background: var(--surface);
  border-bottom: 1px solid var(--border);
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 0 12px;
  flex-shrink: 0;
  z-index: 40;
}

.provider-tabs {
  display: flex;
  align-items: center;
  gap: 4px;
  flex: 1;
  overflow-x: auto;
  scrollbar-width: none;
}
.provider-tabs::-webkit-scrollbar { display: none; }

.prov-tab {
  padding: 5px 12px;
  border-radius: 20px;
  border: 1px solid var(--border);
  background: transparent;
  color: var(--text2);
  font-family: var(--font-ui);
  font-size: 0.78rem;
  font-weight: 600;
  cursor: pointer;
  white-space: nowrap;
  transition: all 0.15s;
}

.prov-tab:hover {
  border-color: var(--border2);
  color: var(--text);
}

.prov-tab.active {
  background: var(--accent-dim);
  border-color: var(--accent);
  color: var(--accent);
}

.topbar-right {
  display: flex;
  align-items: center;
  gap: 8px;
  flex-shrink: 0;
}

.wallet-chip {
  display: flex;
  align-items: center;
  gap: 5px;
  background: var(--accent-dim);
  border: 1px solid var(--border2);
  border-radius: 20px;
  padding: 4px 10px;
  font-size: 0.78rem;
  font-weight: 700;
  color: var(--accent);
  white-space: nowrap;
  font-family: var(--font-mono);
  transition: border-color 0.15s;
}

.wallet-chip i { font-size: 14px; }
.wallet-chip:hover { border-color: var(--accent); }

.avatar-btn {
  width: 32px;
  height: 32px;
  border-radius: 50%;
  background: var(--accent-dim);
  border: 1px solid var(--border2);
  color: var(--accent);
  font-family: var(--font-ui);
  font-size: 0.72rem;
  font-weight: 800;
  display: flex;
  align-items: center;
  justify-content: center;
  text-decoration: none;
  flex-shrink: 0;
  transition: border-color 0.15s;
}

.avatar-btn:hover { border-color: var(--accent); }

/* ── Balance banner ───────────────────────────────────────── */
#balance-banner {
  display: none;
  align-items: center;
  gap: 8px;
  padding: 8px 16px;
  background: var(--error-dim);
  border-bottom: 1px solid var(--error);
  color: var(--error);
  font-size: 0.82rem;
  flex-shrink: 0;
}

#balance-banner.show { display: flex; }

#balance-banner a {
  color: var(--error);
  font-weight: 600;
  text-decoration: underline;
  margin-left: auto;
}

/* ── Messages area ────────────────────────────────────────── */
#messages-wrap {
  flex: 1;
  overflow-y: auto;
  display: flex;
  flex-direction: column;
}

#messages {
  max-width: 760px;
  width: 100%;
  margin: 0 auto;
  padding: 24px 20px 12px;
  display: flex;
  flex-direction: column;
  gap: 0;
}

/* ── Welcome screen ───────────────────────────────────────── */
#welcome {
  flex: 1;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  padding: 60px 20px 40px;
  text-align: center;
  min-height: 340px;
}

.welcome-wordmark {
  font-family: var(--font-ui);
  font-size: 2.8rem;
  font-weight: 800;
  color: var(--accent);
  letter-spacing: -0.04em;
  margin-bottom: 10px;
}

.welcome-sub {
  font-size: 0.9rem;
  color: var(--text3);
  margin-bottom: 32px;
  max-width: 420px;
}

.welcome-chips {
  display: flex;
  flex-wrap: wrap;
  gap: 8px;
  justify-content: center;
  max-width: 560px;
}

.chip {
  padding: 7px 14px;
  border-radius: 20px;
  border: 1px solid var(--border2);
  background: var(--surface2);
  color: var(--text2);
  font-size: 0.8rem;
  cursor: pointer;
  transition: all 0.15s;
  font-family: var(--font-body);
}

.chip:hover {
  border-color: var(--accent);
  color: var(--accent);
  background: var(--accent-dim);
}

/* ── Message group ────────────────────────────────────────── */
.msg-group {
  display: flex;
  flex-direction: column;
  gap: 16px;
  padding: 14px 0;
  border-bottom: 1px solid var(--border);
}

.msg-group:last-child { border-bottom: none; }

/* ── User bubble ──────────────────────────────────────────── */
.msg-user-row {
  display: flex;
  justify-content: flex-end;
}

.msg-user-bubble {
  max-width: 72%;
  background: var(--bubble-bg);
  color: var(--bubble-text);
  border-radius: 16px 16px 4px 16px;
  padding: 10px 14px;
  font-size: 0.9rem;
  line-height: 1.55;
  white-space: pre-wrap;
  word-break: break-word;
}

/* ── Assistant message ────────────────────────────────────── */
.msg-asst-row {
  display: flex;
  align-items: flex-start;
  gap: 10px;
}

.bot-icon {
  width: 30px;
  height: 30px;
  border-radius: 50%;
  background: var(--bot-bg);
  color: var(--bot-text);
  font-family: var(--font-ui);
  font-size: 0.65rem;
  font-weight: 800;
  display: flex;
  align-items: center;
  justify-content: center;
  flex-shrink: 0;
  margin-top: 2px;
  border: 1px solid var(--border2);
}

.msg-asst-body {
  flex: 1;
  min-width: 0;
}

/* ── Streaming text ───────────────────────────────────────── */
.msg-stream-text {
  font-size: 0.9rem;
  line-height: 1.65;
  color: var(--text);
  white-space: pre-wrap;
  word-break: break-word;
  min-height: 1.4em;
}

.msg-stream-text.streaming::after {
  content: '▋';
  display: inline-block;
  animation: blink 0.85s step-end infinite;
  color: var(--accent);
  margin-left: 1px;
}

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

/* ── Rendered ContentApp output ───────────────────────────── */
#messages [id^="asst-elm-"],
#messages [id^="asst-content-"] {
  font-size: 0.9rem;
  line-height: 1.65;
  color: var(--text);
}

/* mdFallback styles */
.msg-rendered { font-size: 0.9rem; line-height: 1.65; }
.msg-rendered p  { margin: 0 0 0.8em; }
.msg-rendered h1 { font-size: 1.2rem; font-weight: 700; margin: 0.8em 0 0.4em; }
.msg-rendered h2 { font-size: 1.05rem; font-weight: 700; margin: 0.8em 0 0.4em; }
.msg-rendered h3 { font-size: 0.95rem; font-weight: 700; margin: 0.6em 0 0.3em; }
.msg-rendered pre { background: var(--surface2); border: 1px solid var(--border2); border-radius: var(--radius-sm); padding: 10px 14px; overflow-x: auto; margin: 0.6em 0; }
.msg-rendered code { font-family: var(--font-mono); font-size: 0.84em; background: var(--surface2); padding: 1px 5px; border-radius: 3px; }
.msg-rendered pre code { background: none; padding: 0; }
.msg-rendered strong { font-weight: 600; color: var(--text); }

/* ── Message meta (model, tokens, cost, actions) ──────────── */
.msg-meta {
  display: flex;
  align-items: center;
  flex-wrap: wrap;
  gap: 5px;
  margin-top: 10px;
  min-height: 24px;
}

.meta-tag {
  font-size: 0.68rem;
  font-family: var(--font-mono);
  padding: 2px 7px;
  background: var(--surface2);
  border: 1px solid var(--border);
  border-radius: 10px;
  color: var(--text3);
}

.meta-cost { color: var(--success); border-color: rgba(74,222,128,0.2); }

.msg-actions {
  display: flex;
  align-items: center;
  gap: 2px;
  margin-left: auto;
  opacity: 0;
  transition: opacity 0.15s;
}

.msg-group:hover .msg-actions { opacity: 1; }

.msg-act-btn {
  background: none;
  border: none;
  color: var(--text3);
  padding: 4px 6px;
  border-radius: var(--radius-sm);
  cursor: pointer;
  font-size: 14px;
  transition: color 0.12s, background 0.12s;
  display: flex;
  align-items: center;
}

.msg-act-btn:hover         { color: var(--text); background: var(--accent-dim); }
.msg-act-btn.del:hover     { color: var(--error); background: var(--error-dim); }

.msg-error {
  padding: 14px 16px;
  background: var(--error-dim);
  border: 1px solid var(--error);
  border-radius: var(--radius);
  color: var(--error);
  font-size: 0.85rem;
  display: flex;
  align-items: center;
  gap: 8px;
}

/* ════════════════════════════════════════════════════════════
   INPUT AREA
   ════════════════════════════════════════════════════════════ */
#input-area {
  flex-shrink: 0;
  background: var(--bg);
  padding: 12px 16px 16px;
  border-top: 1px solid var(--border);
}

#input-inner {
  max-width: 760px;
  margin: 0 auto;
}

.input-box {
  position: relative;
  background: var(--surface);
  border: 1px solid var(--border2);
  border-radius: var(--radius);
  display: flex;
  align-items: flex-end;
  gap: 0;
  transition: border-color 0.15s, box-shadow 0.15s;
}

.input-box:focus-within {
  border-color: var(--accent);
  box-shadow: 0 0 0 3px var(--accent-dim);
}

#user-input {
  flex: 1;
  background: none;
  border: none;
  outline: none;
  resize: none;
  padding: 12px 14px;
  font-family: var(--font-body);
  font-size: 0.9rem;
  color: var(--text);
  min-height: 46px;
  max-height: 180px;
  line-height: 1.55;
}

#user-input::placeholder { color: var(--text3); }
#user-input:disabled { opacity: 0.5; cursor: not-allowed; }

/* Send / Stop buttons */
.send-btn,
.stop-btn {
  flex-shrink: 0;
  width: 34px;
  height: 34px;
  border-radius: 7px;
  border: none;
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 17px;
  margin: 6px 6px 6px 0;
  transition: opacity 0.15s, background 0.15s;
}

.send-btn {
  background: var(--accent);
  color: #0a0c14;
}

.send-btn:hover   { opacity: 0.85; }
.send-btn.hidden  { display: none; }

.stop-btn {
  background: var(--error-dim);
  color: var(--error);
  border: 1px solid var(--error);
  display: none;
}

.stop-btn.visible { display: flex; }

/* Input footer */
.input-footer {
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 6px 2px 0;
}

.model-select {
  background: transparent;
  border: none;
  color: var(--text3);
  font-family: var(--font-mono);
  font-size: 0.72rem;
  outline: none;
  cursor: pointer;
  max-width: 200px;
}

.model-select option {
  background: var(--surface);
  color: var(--text);
}

.cost-hint {
  font-size: 0.68rem;
  font-family: var(--font-mono);
  color: var(--text3);
  white-space: nowrap;
}

.footer-right {
  margin-left: auto;
  display: flex;
  align-items: center;
}

/* ── Icon buttons (generic) ───────────────────────────────── */
.icon-btn {
  background: none;
  border: none;
  color: var(--text3);
  cursor: pointer;
  font-size: 18px;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 5px;
  border-radius: var(--radius-sm);
  transition: color 0.12s, background 0.12s;
  flex-shrink: 0;
}

.icon-btn:hover { color: var(--text); background: var(--accent-dim); }

/* ════════════════════════════════════════════════════════════
   TOAST
   ════════════════════════════════════════════════════════════ */
#toast {
  position: fixed;
  bottom: 24px;
  left: 50%;
  transform: translateX(-50%) translateY(8px);
  background: var(--surface2);
  border: 1px solid var(--border2);
  color: var(--text);
  font-size: 0.82rem;
  padding: 8px 18px;
  border-radius: 20px;
  box-shadow: var(--shadow);
  opacity: 0;
  pointer-events: none;
  transition: opacity 0.2s, transform 0.2s;
  white-space: nowrap;
  z-index: 200;
}

#toast.show {
  opacity: 1;
  transform: translateX(-50%) translateY(0);
}

/* ════════════════════════════════════════════════════════════
   MOBILE ( ≤ 680px )
   ════════════════════════════════════════════════════════════ */
@media (max-width: 680px) {
  :root { --sidebar-w: 80vw; }

  #sidebar {
    position: fixed;
    top: 0;
    left: 0;
    bottom: 0;
    transform: translateX(-100%);
    transition: transform 0.22s ease;
    width: var(--sidebar-w);
  }

  #sidebar.mobile-open {
    transform: translateX(0);
  }

  #sidebar.collapsed { width: var(--sidebar-w); transform: translateX(-100%); }

  #topbar { gap: 8px; padding: 0 10px; }

  .provider-tabs { gap: 3px; }
  .prov-tab      { padding: 4px 9px; font-size: 0.72rem; }

  .wallet-chip   { display: none; }

  #messages      { padding: 16px 12px 8px; }

  .msg-user-bubble { max-width: 88%; }

  #input-area    { padding: 10px 10px 14px; }

  .welcome-wordmark { font-size: 2rem; }
  .welcome-chips    { gap: 6px; }
  .chip             { font-size: 0.75rem; padding: 6px 11px; }
}

/* ════════════════════════════════════════════════════════════
   KATEX overrides — ensure KaTeX respects theme colours
   ════════════════════════════════════════════════════════════ */
.katex { color: var(--text); }
.katex-display { margin: 0.6em 0; overflow-x: auto; }