*, *::before, *::after { box-sizing: border-box; margin: 0; padding: 0; }

:root {
  --bg:         #0f1117;
  --surface:    #1a1d27;
  --surface2:   #23263a;
  --border:     #2e3150;
  --accent:     #6c7aff;
  --accent-dim: #3d4599;
  --text:       #e8e9f0;
  --text-dim:   #7b80a0;
  --user-bg:    #2a2d4a;
  --bot-bg:     #1e2235;
  --error:      #ff5c5c;
  --sidebar-w:  260px;
}

body {
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, sans-serif;
  background: var(--bg);
  color: var(--text);
  height: 100vh;
  overflow: hidden;
}

.hidden { display: none !important; }

/* ── Login ── */
#login-view {
  display: flex;
  align-items: center;
  justify-content: center;
  height: 100vh;
}

.login-card {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 12px;
  padding: 40px;
  width: 360px;
  display: flex;
  flex-direction: column;
  gap: 14px;
}

.login-card h1 { font-size: 1.6rem; font-weight: 700; color: var(--accent); }

.login-subtitle { color: var(--text-dim); font-size: 0.875rem; margin-top: -8px; }

.login-card input {
  background: var(--surface2);
  border: 1px solid var(--border);
  border-radius: 8px;
  padding: 10px 14px;
  color: var(--text);
  font-size: 0.95rem;
  outline: none;
  transition: border-color 0.15s;
}
.login-card input:focus { border-color: var(--accent); }

.login-card button {
  background: var(--accent);
  color: #fff;
  border: none;
  border-radius: 8px;
  padding: 11px;
  font-size: 0.95rem;
  font-weight: 600;
  cursor: pointer;
  transition: background 0.15s;
}
.login-card button:hover { background: var(--accent-dim); }

.error-msg {
  background: rgba(255, 92, 92, 0.12);
  border: 1px solid var(--error);
  border-radius: 6px;
  color: var(--error);
  font-size: 0.85rem;
  padding: 8px 12px;
}

/* ── Chat layout ── */
#chat-view { display: flex; height: 100vh; }

/* ── Sidebar ── */
#sidebar {
  width: var(--sidebar-w);
  min-width: var(--sidebar-w);
  background: var(--surface);
  border-right: 1px solid var(--border);
  display: flex;
  flex-direction: column;
  overflow: hidden;
}

.sidebar-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 18px 16px 12px;
  border-bottom: 1px solid var(--border);
}

.sidebar-title { font-weight: 700; font-size: 1rem; color: var(--accent); }

.icon-btn {
  background: none;
  border: none;
  color: var(--text-dim);
  cursor: pointer;
  font-size: 1.1rem;
  padding: 4px 6px;
  border-radius: 4px;
  transition: color 0.15s, background 0.15s;
}
.icon-btn:hover { color: var(--text); background: var(--surface2); }

.new-conv-btn {
  margin: 12px 12px 8px;
  background: var(--surface2);
  border: 1px solid var(--border);
  border-radius: 8px;
  color: var(--text);
  padding: 9px 12px;
  font-size: 0.875rem;
  cursor: pointer;
  text-align: left;
  transition: background 0.15s, border-color 0.15s;
}
.new-conv-btn:hover { background: var(--border); border-color: var(--accent); }

#conv-list { flex: 1; overflow-y: auto; padding: 4px 8px 12px; }

.conv-item {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 9px 10px;
  border-radius: 7px;
  cursor: pointer;
  font-size: 0.875rem;
  color: var(--text-dim);
  gap: 6px;
  transition: background 0.1s, color 0.1s;
}
.conv-item:hover { background: var(--surface2); color: var(--text); }
.conv-item.active { background: var(--surface2); color: var(--text); }

.conv-name {
  flex: 1;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.conv-menu-btn {
  background: none;
  border: none;
  color: var(--text-dim);
  cursor: pointer;
  font-size: 1rem;
  padding: 2px 5px;
  border-radius: 4px;
  opacity: 0;
  transition: opacity 0.1s, background 0.1s;
}
.conv-item:hover .conv-menu-btn,
.conv-item.active .conv-menu-btn { opacity: 1; }
.conv-menu-btn:hover { background: var(--border); color: var(--text); }

/* ── Context menu ── */
#ctx-menu {
  position: fixed;
  background: var(--surface2);
  border: 1px solid var(--border);
  border-radius: 8px;
  padding: 4px;
  z-index: 100;
  min-width: 130px;
  box-shadow: 0 4px 20px rgba(0,0,0,0.4);
}

#ctx-menu button {
  display: block;
  width: 100%;
  background: none;
  border: none;
  color: var(--text);
  padding: 8px 12px;
  font-size: 0.875rem;
  text-align: left;
  cursor: pointer;
  border-radius: 5px;
  transition: background 0.1s;
}
#ctx-menu button:hover { background: var(--border); }
#ctx-delete { color: var(--error) !important; }

/* ── Main area ── */
#main { flex: 1; display: flex; flex-direction: column; overflow: hidden; }

#messages {
  flex: 1;
  overflow-y: auto;
  padding: 24px 0;
  display: flex;
  flex-direction: column;
}

.msg-row { display: flex; padding: 4px 24px; }
.msg-row.user { justify-content: flex-end; }
.msg-row.bot  { justify-content: flex-start; }

.msg-bubble {
  max-width: 72%;
  padding: 12px 16px;
  border-radius: 14px;
  font-size: 0.925rem;
  line-height: 1.6;
}

.msg-row.user .msg-bubble { background: var(--user-bg); border-bottom-right-radius: 4px; }
.msg-row.bot  .msg-bubble { background: var(--bot-bg);  border-bottom-left-radius: 4px; }

.msg-bubble h1, .msg-bubble h2, .msg-bubble h3 { font-size: 1rem; margin: 12px 0 6px; }
.msg-bubble p { margin: 6px 0; }
.msg-bubble ul, .msg-bubble ol { padding-left: 20px; margin: 6px 0; }
.msg-bubble li { margin: 3px 0; }
.msg-bubble code {
  background: rgba(255,255,255,0.08);
  border-radius: 4px;
  padding: 1px 5px;
  font-family: monospace;
  font-size: 0.875em;
}
.msg-bubble pre {
  background: rgba(0,0,0,0.3);
  border-radius: 8px;
  padding: 12px;
  overflow-x: auto;
  margin: 8px 0;
}
.msg-bubble pre code { background: none; padding: 0; }
.msg-bubble table { border-collapse: collapse; width: 100%; margin: 8px 0; font-size: 0.875rem; }
.msg-bubble th, .msg-bubble td { border: 1px solid var(--border); padding: 6px 10px; text-align: left; }
.msg-bubble th { background: var(--surface2); }

/* Thinking indicator */
#thinking { display: flex; align-items: center; gap: 5px; padding: 12px 40px; }

.thinking-dot {
  width: 7px; height: 7px;
  background: var(--accent);
  border-radius: 50%;
  animation: pulse 1.4s infinite ease-in-out;
}
.thinking-dot:nth-child(2) { animation-delay: 0.2s; }
.thinking-dot:nth-child(3) { animation-delay: 0.4s; }

@keyframes pulse {
  0%, 80%, 100% { opacity: 0.2; transform: scale(0.8); }
  40%           { opacity: 1;   transform: scale(1);   }
}

/* Input bar */
#input-bar {
  display: flex;
  align-items: flex-end;
  gap: 10px;
  padding: 14px 24px 20px;
  border-top: 1px solid var(--border);
}

#msg-input {
  flex: 1;
  background: var(--surface2);
  border: 1px solid var(--border);
  border-radius: 10px;
  padding: 11px 14px;
  color: var(--text);
  font-size: 0.925rem;
  resize: none;
  outline: none;
  font-family: inherit;
  max-height: 180px;
  overflow-y: auto;
  line-height: 1.5;
  transition: border-color 0.15s;
}
#msg-input:focus { border-color: var(--accent); }
#msg-input:disabled { opacity: 0.5; }

#send-btn {
  background: var(--accent);
  color: #fff;
  border: none;
  border-radius: 10px;
  padding: 11px 20px;
  font-size: 0.925rem;
  font-weight: 600;
  cursor: pointer;
  white-space: nowrap;
  transition: background 0.15s;
}
#send-btn:hover { background: var(--accent-dim); }
#send-btn:disabled { opacity: 0.5; cursor: not-allowed; }

/* Empty state */
.empty-state {
  flex: 1;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  color: var(--text-dim);
  gap: 10px;
}
.empty-state h2 { font-size: 1.3rem; color: var(--text); }

/* File chips */
#file-chips {
  display: flex;
  flex-wrap: wrap;
  gap: 6px;
  padding: 8px 24px 0;
}
#file-chips.hidden { display: none; }

.file-chip {
  display: flex;
  align-items: center;
  gap: 6px;
  background: var(--surface2);
  border: 1px solid var(--border);
  border-radius: 20px;
  padding: 4px 10px 4px 12px;
  font-size: 0.8rem;
  color: var(--text-dim);
  max-width: 220px;
}

.file-chip-name {
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.file-chip-thumb {
  width: 28px;
  height: 28px;
  object-fit: cover;
  border-radius: 4px;
  flex-shrink: 0;
}

.file-chip-rm {
  background: none;
  border: none;
  color: var(--text-dim);
  cursor: pointer;
  font-size: 1rem;
  line-height: 1;
  padding: 0 2px;
  flex-shrink: 0;
}
.file-chip-rm:hover { color: var(--error); }

/* Inline images inside message bubbles */
.msg-bubble img {
  max-width: 100%;
  max-height: 400px;
  border-radius: 8px;
  display: block;
  margin: 8px 0;
}

/* Interactive Plotly chart iframes */
.plotly-frame {
  width: 100%;
  height: 480px;
  border: none;
  border-radius: 8px;
  display: block;
  margin: 8px 0;
  background: #1a1d27;
}

/* Attach button */
#attach-label {
  cursor: pointer;
  color: var(--text-dim);
  display: flex;
  align-items: center;
  flex-shrink: 0;
  padding: 8px;
  border-radius: 8px;
  transition: color 0.15s, background 0.15s;
}
#attach-label:hover { color: var(--text); background: var(--surface2); }

/* Drag-over overlay */
#main.drag-over::after {
  content: 'Drop files here';
  position: absolute;
  inset: 0;
  background: rgba(108, 122, 255, 0.12);
  border: 2px dashed var(--accent);
  border-radius: 8px;
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 1.1rem;
  color: var(--accent);
  pointer-events: none;
  z-index: 10;
}
#main { position: relative; }

/* Scrollbar */
::-webkit-scrollbar { width: 5px; }
::-webkit-scrollbar-track { background: transparent; }
::-webkit-scrollbar-thumb { background: var(--border); border-radius: 3px; }
