/* ── CSS Variables ── */
:root {
  --font: 'Inter', system-ui, -apple-system, sans-serif;
  --font-mono: 'JetBrains Mono', monospace;
  --radius: 12px;
  --radius-sm: 8px;
  --sidebar-w: 280px;
  --topbar-h: 52px;
}

/* Dark (default) */
[data-theme="dark"], :root {
  --bg: #0a0e14;
  --surface: #111824;
  --surface-hover: #1a2332;
  --surface-active: #243044;
  --border: #1e2d3d;
  --text: #c8d6e5;
  --text-dim: #6080a0;
  --text-bright: #e8edf5;
  --accent: #00e5ff;
  --accent-soft: rgba(0,229,255,0.12);
  --accent-glow: rgba(0,229,255,0.18);
  --user-bubble: #1a3a5c;
  --user-text: #d0e8ff;
  --bot-bubble: transparent;
  --danger: #ff4757;
  --success: #2ed573;
  --scrollbar: #1e2d3d;
  --scrollbar-hover: #2a3d52;
  --shadow: 0 2px 12px rgba(0,0,0,0.3);
  --shadow-lg: 0 8px 32px rgba(0,0,0,0.4);
}

/* Light */
[data-theme="light"] {
  --bg: #f0f2f5;
  --surface: #ffffff;
  --surface-hover: #f5f7fa;
  --surface-active: #e8ecf1;
  --border: #d0d7de;
  --text: #1f2328;
  --text-dim: #656d76;
  --text-bright: #0a0e14;
  --accent: #0096b2;
  --accent-soft: rgba(0,150,178,0.08);
  --accent-glow: rgba(0,150,178,0.15);
  --user-bubble: #0096b2;
  --user-text: #ffffff;
  --bot-bubble: transparent;
  --danger: #cf222e;
  --success: #1a7f37;
  --scrollbar: #c0c8d0;
  --scrollbar-hover: #909aa4;
  --shadow: 0 1px 6px rgba(0,0,0,0.08);
  --shadow-lg: 0 4px 24px rgba(0,0,0,0.10);
}

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

html, body {
  height: 100%;
  overflow: hidden;
  background: var(--bg);
  color: var(--text);
  font-family: var(--font);
  -webkit-font-smoothing: antialiased;
  transition: background 0.3s, color 0.3s;
}

/* ── Layout ── */
#app { display: flex; height: 100vh; position: relative; }

/* ── Sidebar ── */
#sidebar {
  width: var(--sidebar-w);
  background: var(--surface);
  border-right: 1px solid var(--border);
  display: flex;
  flex-direction: column;
  flex-shrink: 0;
  z-index: 100;
  transition: transform 0.3s ease, background 0.3s;
}

.sidebar-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 14px 16px;
  border-bottom: 1px solid var(--border);
}

.logo {
  display: flex;
  align-items: center;
  gap: 10px;
  font-weight: 700;
  font-size: 18px;
  color: var(--accent);
  letter-spacing: 0.5px;
}

#new-chat-btn {
  background: none;
  border: 1px solid var(--border);
  color: var(--text-dim);
  width: 34px;
  height: 34px;
  border-radius: var(--radius-sm);
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: center;
  transition: all 0.2s;
}
#new-chat-btn:hover {
  background: var(--surface-hover);
  color: var(--accent);
  border-color: var(--accent);
}

#chat-list {
  flex: 1;
  overflow-y: auto;
  padding: 8px;
}

.chat-item {
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 10px 12px;
  border-radius: var(--radius-sm);
  cursor: pointer;
  font-size: 13px;
  color: var(--text-dim);
  transition: all 0.2s;
  border: 1px solid transparent;
  margin-bottom: 2px;
}
.chat-item:hover {
  background: var(--surface-hover);
  color: var(--text);
}
.chat-item.active {
  background: var(--accent-soft);
  color: var(--accent);
  border-color: var(--accent);
}
.chat-item .chat-title {
  flex: 1;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}
.chat-item .chat-delete {
  opacity: 0;
  background: none;
  border: none;
  color: var(--text-dim);
  cursor: pointer;
  padding: 2px;
  border-radius: 4px;
  transition: all 0.2s;
}
.chat-item:hover .chat-delete { opacity: 0.6; }
.chat-item .chat-delete:hover { opacity: 1; color: var(--danger); }

.sidebar-footer {
  padding: 12px 16px;
  border-top: 1px solid var(--border);
  display: flex;
  flex-direction: column;
  gap: 8px;
}

.footer-btn {
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 8px 12px;
  border: none;
  background: none;
  color: var(--text-dim);
  cursor: pointer;
  border-radius: var(--radius-sm);
  font-size: 13px;
  font-family: inherit;
  transition: all 0.2s;
}
.footer-btn:hover {
  background: var(--surface-hover);
  color: var(--text);
}

.model-badge {
  font-size: 11px;
  color: var(--text-dim);
  text-align: center;
  padding: 6px;
  border-radius: 6px;
  background: var(--surface-hover);
}

/* Theme icons toggle */
[data-theme="dark"] .sun-icon { display: block; }
[data-theme="dark"] .moon-icon { display: none; }
[data-theme="light"] .sun-icon { display: none; }
[data-theme="light"] .moon-icon { display: block; }

/* ── Sidebar Overlay (mobile) ── */
#sidebar-overlay {
  display: none;
  position: fixed;
  inset: 0;
  background: rgba(0,0,0,0.5);
  z-index: 99;
  opacity: 0;
  transition: opacity 0.3s;
}
#sidebar-overlay.open {
  opacity: 1;
}

/* ── Top Bar ── */
#topbar {
  display: none;
  align-items: center;
  height: var(--topbar-h);
  padding: 0 16px;
  border-bottom: 1px solid var(--border);
  background: var(--surface);
  gap: 12px;
  transition: background 0.3s;
}

#menu-btn {
  background: none;
  border: none;
  color: var(--text);
  cursor: pointer;
  padding: 4px;
  border-radius: 6px;
  display: flex;
}
#menu-btn:hover { background: var(--surface-hover); }

.topbar-title {
  flex: 1;
  font-weight: 600;
  font-size: 16px;
  color: var(--text);
}

#theme-toggle-mobile {
  display: flex;
  align-items: center;
  justify-content: center;
  width: 32px;
  height: 32px;
  padding: 0;
}

/* ── Main ── */
#main {
  flex: 1;
  display: flex;
  flex-direction: column;
  min-width: 0;
  background: var(--bg);
  transition: background 0.3s;
  min-height: 0;
  overflow: hidden;
}

#chat-area {
  flex: 1;
  overflow-y: auto;
  display: flex;
  flex-direction: column;
  -webkit-overflow-scrolling: touch;
}

#messages {
  flex: 1;
  padding: 24px 24px 0;
  max-width: 800px;
  width: 100%;
  margin: 0 auto;
}

#file-bar {
  display: none;
  flex-wrap: wrap;
  gap: 6px;
  padding: 0 24px;
  max-width: 800px;
  width: 100%;
  margin: 0 auto 8px;
  position: sticky;
  bottom: 76px;
  background: var(--bg);
  z-index: 9;
}

/* ── Welcome Screen ── */
#welcome-screen {
  flex: 1;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  text-align: center;
  padding: 40px 24px;
  max-width: 600px;
  margin: 0 auto;
}

.welcome-logo {
  margin-bottom: 24px;
  color: var(--text-dim);
  opacity: 0.5;
}
.welcome-logo svg { width: 64px; height: 64px; }

#welcome-screen h1 {
  font-size: 28px;
  font-weight: 700;
  margin-bottom: 8px;
  color: var(--text-bright);
}

.welcome-sub { color: var(--text-dim); margin-bottom: 32px; font-size: 14px; }

.welcome-hints {
  display: flex;
  flex-wrap: wrap;
  gap: 8px;
  justify-content: center;
}

.hint {
  padding: 8px 16px;
  border: 1px solid var(--border);
  border-radius: 20px;
  cursor: pointer;
  font-size: 13px;
  color: var(--text-dim);
  background: none;
  font-family: inherit;
  transition: all 0.3s;
}
.hint:hover {
  background: var(--accent-soft);
  border-color: var(--accent);
  color: var(--accent);
}

/* ── Messages ── */
.msg {
  display: flex;
  gap: 12px;
  margin-bottom: 24px;
  animation: msgIn 0.3s ease;
}
@keyframes msgIn {
  from { opacity: 0; transform: translateY(8px); }
  to   { opacity: 1; transform: translateY(0); }
}

.msg-avatar {
  width: 30px;
  height: 30px;
  border-radius: 50%;
  flex-shrink: 0;
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 13px;
  font-weight: 700;
  margin-top: 4px;
}
.msg.user { flex-direction: row-reverse; }
.msg.user .msg-avatar {
  background: var(--user-bubble);
  color: var(--user-text);
}
.msg.bot .msg-avatar {
  background: var(--surface-hover);
  color: var(--text-dim);
  border: 1px solid var(--border);
}

.msg-bubble {
  max-width: 80%;
  padding: 12px 16px;
  border-radius: var(--radius);
  line-height: 1.65;
  font-size: 14px;
  word-wrap: break-word;
}
.msg.user .msg-bubble {
  background: var(--user-bubble);
  color: var(--user-text);
  border-bottom-right-radius: 4px;
}
.msg.bot .msg-bubble {
  background: var(--surface);
  border: 1px solid var(--border);
  border-bottom-left-radius: 4px;
  transition: background 0.3s, border 0.3s;
}

/* Markdown inside bot bubbles */
.msg-bubble p { margin-bottom: 8px; }
.msg-bubble p:last-child { margin-bottom: 0; }
.msg-bubble code {
  background: var(--bg);
  padding: 2px 6px;
  border-radius: 4px;
  font-family: var(--font-mono);
  font-size: 13px;
}
.msg-bubble pre {
  background: var(--bg);
  padding: 14px;
  border-radius: var(--radius-sm);
  overflow-x: auto;
  margin: 10px 0;
  border: 1px solid var(--border);
}
.msg-bubble pre code { background: none; padding: 0; }
.msg-bubble strong { color: var(--text-bright); }
.msg-bubble em { color: var(--text-dim); }

/* ── Loading Animation ── */
.loading-bar {
  display: flex;
  align-items: center;
  gap: 6px;
  padding: 4px 0;
}
.loading-bar .bar {
  width: 18px;
  height: 4px;
  border-radius: 2px;
  background: var(--accent);
  animation: loadPulse 1.2s ease-in-out infinite;
}
.loading-bar .bar:nth-child(2) { animation-delay: 0.15s; }
.loading-bar .bar:nth-child(3) { animation-delay: 0.3s; }
.loading-bar .bar:nth-child(4) { animation-delay: 0.45s; }
.loading-bar .bar:nth-child(5) { animation-delay: 0.6s; }
@keyframes loadPulse {
  0%, 80%, 100% { opacity: 0.2; transform: scaleY(0.6); }
  40% { opacity: 1; transform: scaleY(1.2); }
}

.msg-think {
  font-size: 12px;
  color: var(--text-dim);
  margin-top: 4px;
  opacity: 0.7;
}

/* Timestamp */
.msg-time {
  font-size: 10px;
  color: var(--text-dim);
  margin-top: 6px;
  opacity: 0.5;
}
.msg.user .msg-time { text-align: right; }
.msg.bot .msg-time { text-align: left; }

/* Action buttons (retry, etc) */
.msg-actions {
  display: flex;
  gap: 6px;
  margin-top: 8px;
  padding-top: 6px;
  border-top: 1px solid var(--border);
  opacity: 0;
  transition: opacity 0.2s;
}
.msg.bot:hover .msg-actions { opacity: 1; }

.msg-action-btn {
  background: none;
  border: 1px solid var(--border);
  color: var(--text-dim);
  cursor: pointer;
  padding: 4px 10px;
  border-radius: 6px;
  font-size: 12px;
  font-family: inherit;
  display: flex;
  align-items: center;
  gap: 4px;
  transition: all 0.2s;
}
.msg-action-btn:hover {
  background: var(--accent-soft);
  border-color: var(--accent);
  color: var(--accent);
}
.msg-action-btn svg { flex-shrink: 0; }

/* Error messages in bubbles */
.msg-bubble [style*="color:var(--danger)"] {
  display: block;
  padding: 4px 0;
}

/* ── Input Area ── */
#input-area {
  display: flex;
  align-items: flex-end;
  gap: 8px;
  padding: 16px 24px 24px;
  max-width: 800px;
  width: 100%;
  margin: 0 auto;
  position: sticky;
  bottom: 0;
  background: var(--bg);
  z-index: 10;
  transition: background 0.3s;
}

#input-box {
  flex: 1;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 12px 16px;
  color: var(--text);
  font-family: var(--font);
  font-size: 14px;
  resize: none;
  outline: none;
  max-height: 120px;
  transition: border-color 0.2s, background 0.3s;
  line-height: 1.5;
}
#input-box:focus { border-color: var(--accent); }
#input-box::placeholder { color: var(--text-dim); opacity: 0.5; }

#send-btn {
  width: 44px;
  height: 44px;
  border-radius: 50%;
  border: none;
  background: var(--accent);
  color: var(--bg);
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: center;
  transition: all 0.2s;
  flex-shrink: 0;
}
#send-btn:disabled {
  background: var(--surface);
  color: var(--text-dim);
  cursor: not-allowed;
  border: 1px solid var(--border);
}
#send-btn:not(:disabled):hover {
  transform: scale(1.05);
  box-shadow: 0 0 20px var(--accent-soft);
}

/* ── Scrollbar ── */
::-webkit-scrollbar { width: 6px; }
::-webkit-scrollbar-track { background: transparent; }
::-webkit-scrollbar-thumb { background: var(--scrollbar); border-radius: 3px; }
::-webkit-scrollbar-thumb:hover { background: var(--scrollbar-hover); }
* { scrollbar-width: thin; scrollbar-color: var(--scrollbar) transparent; }

/* ── Toast ── */
#toast {
  position: fixed;
  bottom: 80px;
  left: 50%;
  transform: translateX(-50%);
  background: var(--danger);
  color: #fff;
  padding: 10px 20px;
  border-radius: var(--radius-sm);
  font-size: 13px;
  opacity: 0;
  transition: opacity 0.3s;
  pointer-events: none;
  z-index: 200;
}
#toast.show { opacity: 1; }

/* ── Responsive ── */
@media (max-width: 768px) {
  :root { --sidebar-w: 280px; }

  #sidebar {
    position: fixed;
    left: 0;
    top: 0;
    bottom: 0;
    transform: translateX(-100%);
    box-shadow: var(--shadow-lg);
  }
  #sidebar.open { transform: translateX(0); }
  #sidebar-overlay.open { display: block; }

  #topbar { display: flex; }

  #welcome-screen h1 { font-size: 22px; }
  .msg-bubble { max-width: 90%; }
  #messages { padding: 16px 16px 0; }
  #input-area { padding: 12px 16px 16px; }

  .chat-item .chat-delete { opacity: 0.6; }
}

@media (min-width: 769px) {
  #theme-toggle-mobile { display: none; }
}

@media (max-width: 480px) {
  .welcome-hints { flex-direction: column; align-items: stretch; }
  .hint { width: 100%; }
  .msg-bubble { max-width: 95%; font-size: 13px; }
}
