/* ─── Variables ──────────────────────────────────────────── */
:root {
  --bg: #EDEAE4;
  --bg-sidebar: #E5E1DA;
  --bg-card: #F5F2ED;
  --bg-input: #FDFCFA;
  --border: #D8D3CB;
  --border-light: #E8E4DE;
  --text-primary: #1A1814;
  --text-secondary: #6B6560;
  --text-muted: #9B958E;
  --accent: #6B4FBB;
  --accent-hover: #5A3FA8;
  --accent-light: #EDE8F8;
  --user-bubble: #6B4FBB;
  --user-bubble-text: #ffffff;
  --radius-sm: 8px;
  --radius-md: 12px;
  --radius-lg: 18px;
  --sidebar-w: 240px;
  --header-h: 52px;
  --transition: 0.18s ease;
}

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

html, body {
  height: 100%;
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Helvetica, Arial, sans-serif;
  font-size: 15px;
  background: var(--bg);
  color: var(--text-primary);
  -webkit-font-smoothing: antialiased;
}

/* ─── Auth Overlay ──────────────────────────────────────── */
.auth-overlay {
  position: fixed; inset: 0;
  background: var(--bg);
  display: flex; align-items: center; justify-content: center;
  z-index: 1000;
  padding: 24px;
}

.auth-box {
  background: var(--bg-card);
  border: 1px solid var(--border);
  border-radius: var(--radius-lg);
  padding: 40px 36px;
  width: 100%;
  max-width: 380px;
  text-align: center;
  box-shadow: 0 4px 24px rgba(0,0,0,0.06);
}

.auth-icon { margin-bottom: 20px; }
.auth-icon svg { border-radius: 12px; }

.auth-box h1 {
  font-size: 1.5rem;
  font-weight: 700;
  letter-spacing: -0.02em;
  margin-bottom: 8px;
}

.auth-subtitle {
  color: var(--text-secondary);
  font-size: 0.875rem;
  line-height: 1.5;
  margin-bottom: 28px;
}

.auth-input-group {
  display: flex;
  flex-direction: column;
  gap: 10px;
}

.auth-input-group input {
  width: 100%;
  padding: 11px 14px;
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  background: var(--bg-input);
  font-size: 0.9375rem;
  color: var(--text-primary);
  outline: none;
  transition: border-color var(--transition);
}

.auth-input-group input:focus {
  border-color: var(--accent);
  box-shadow: 0 0 0 3px rgba(107,79,187,0.1);
}

.auth-input-group button {
  padding: 11px 14px;
  background: var(--accent);
  color: white;
  border: none;
  border-radius: var(--radius-sm);
  font-size: 0.9375rem;
  font-weight: 600;
  cursor: pointer;
  transition: background var(--transition);
}

.auth-input-group button:hover { background: var(--accent-hover); }

.auth-error {
  color: #C0392B;
  font-size: 0.8125rem;
  margin-top: 10px;
}

/* ─── Layout ─────────────────────────────────────────────── */
.app {
  display: flex;
  height: 100vh;
  overflow: hidden;
}

.hidden { display: none !important; }

/* ─── Sidebar ────────────────────────────────────────────── */
.sidebar {
  width: var(--sidebar-w);
  min-width: var(--sidebar-w);
  background: var(--bg-sidebar);
  border-right: 1px solid var(--border);
  display: flex;
  flex-direction: column;
  overflow: hidden;
  transition: width var(--transition), min-width var(--transition);
}

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

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

.app-brand {
  display: flex;
  align-items: center;
  gap: 9px;
  overflow: hidden;
}

.app-icon svg { border-radius: 7px; display: block; }

.app-name {
  font-weight: 700;
  font-size: 0.9375rem;
  letter-spacing: -0.01em;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.new-chat-btn {
  flex-shrink: 0;
  width: 30px; height: 30px;
  display: flex; align-items: center; justify-content: center;
  background: var(--accent-light);
  color: var(--accent);
  border: none;
  border-radius: var(--radius-sm);
  cursor: pointer;
  transition: background var(--transition);
}

.new-chat-btn:hover { background: #DDD5F5; }

.sidebar-section-label {
  padding: 14px 14px 6px;
  font-size: 0.75rem;
  font-weight: 600;
  text-transform: uppercase;
  letter-spacing: 0.06em;
  color: var(--text-muted);
  flex-shrink: 0;
}

.chat-history {
  flex: 1;
  overflow-y: auto;
  padding: 0 8px 12px;
}

.chat-history ul { list-style: none; }

.history-item {
  padding: 8px 8px;
  border-radius: var(--radius-sm);
  cursor: pointer;
  font-size: 0.875rem;
  color: var(--text-secondary);
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
  transition: background var(--transition), color var(--transition);
  margin-bottom: 2px;
}

.history-item:hover { background: var(--border-light); color: var(--text-primary); }
.history-item.active { background: var(--accent-light); color: var(--accent); font-weight: 500; }

.sidebar-footer {
  padding: 8px 12px 12px;
  flex-shrink: 0;
}

.clear-history-btn {
  width: 100%;
  padding: 7px 10px;
  background: none;
  border: none;
  border-radius: var(--radius-sm);
  font-size: 0.75rem;
  color: transparent;
  cursor: pointer;
  text-align: left;
  transition: color var(--transition), background var(--transition);
}

.sidebar:hover .clear-history-btn:hover {
  color: #C0392B;
  background: rgba(192,57,43,0.07);
}

.history-empty {
  font-size: 0.8125rem;
  color: var(--text-muted);
  text-align: center;
  padding: 20px 8px;
}

/* ─── Main Content ───────────────────────────────────────── */
.main-content {
  flex: 1;
  display: flex;
  flex-direction: column;
  overflow: hidden;
  min-width: 0;
}

.main-header {
  height: var(--header-h);
  display: flex;
  align-items: center;
  gap: 12px;
  padding: 0 20px;
  border-bottom: 1px solid var(--border-light);
  flex-shrink: 0;
}

.sidebar-toggle {
  background: none;
  border: none;
  cursor: pointer;
  color: var(--text-secondary);
  padding: 4px;
  border-radius: 6px;
  display: flex;
  align-items: center;
  transition: background var(--transition), color var(--transition);
}

.sidebar-toggle:hover { background: var(--border-light); color: var(--text-primary); }

.current-chat-title {
  flex: 1;
  font-size: 0.875rem;
  font-weight: 500;
  color: var(--text-secondary);
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.model-badge {
  font-size: 0.75rem;
  font-weight: 500;
  color: var(--text-muted);
  background: var(--border-light);
  padding: 3px 9px;
  border-radius: 99px;
  white-space: nowrap;
}

/* ─── Welcome Screen ─────────────────────────────────────── */
.welcome-screen {
  flex: 1;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  padding: 48px 24px;
  gap: 16px;
}

.welcome-icon svg { border-radius: 18px; display: block; }

.welcome-title {
  font-size: 2rem;
  font-weight: 800;
  letter-spacing: -0.03em;
  text-align: center;
}

.welcome-subtitle {
  font-size: 1rem;
  color: var(--text-secondary);
  text-align: center;
  max-width: 380px;
  line-height: 1.5;
}

/* ─── Chat Messages ──────────────────────────────────────── */
.chat-messages {
  flex: 1;
  overflow-y: auto;
  padding: 24px 0;
  display: flex;
  flex-direction: column;
  gap: 0;
}

.message {
  display: flex;
  padding: 6px 24px;
  animation: fadeIn 0.15s ease;
}

@keyframes fadeIn { from { opacity: 0; transform: translateY(4px); } to { opacity: 1; transform: none; } }

.message.user { justify-content: flex-end; }
.message.assistant { justify-content: flex-start; }

.message-bubble {
  max-width: 72%;
  padding: 12px 16px;
  border-radius: var(--radius-md);
  font-size: 0.9375rem;
  line-height: 1.6;
  word-break: break-word;
}

.message.user .message-bubble {
  background: var(--user-bubble);
  color: var(--user-bubble-text);
  border-bottom-right-radius: 4px;
}

.message.assistant .message-bubble {
  background: var(--bg-card);
  border: 1px solid var(--border-light);
  color: var(--text-primary);
  border-bottom-left-radius: 4px;
}

/* Markdown in assistant bubbles */
.message-bubble h1, .message-bubble h2, .message-bubble h3 {
  font-weight: 700;
  margin: 12px 0 6px;
  line-height: 1.3;
}
.message-bubble h1 { font-size: 1.1rem; }
.message-bubble h2 { font-size: 1.0rem; }
.message-bubble h3 { font-size: 0.95rem; }
.message-bubble p { margin: 6px 0; }
.message-bubble p:first-child { margin-top: 0; }
.message-bubble p:last-child { margin-bottom: 0; }
.message-bubble strong { font-weight: 700; }
.message-bubble em { font-style: italic; }
.message-bubble ul, .message-bubble ol { padding-left: 18px; margin: 6px 0; }
.message-bubble li { margin: 3px 0; }
.message-bubble hr { border: none; border-top: 1px solid var(--border); margin: 10px 0; }

/* Typing indicator */
.typing-indicator {
  display: flex;
  gap: 4px;
  padding: 10px 4px;
  align-items: center;
}

.typing-dot {
  width: 7px; height: 7px;
  background: var(--text-muted);
  border-radius: 50%;
  animation: bounce 1.2s infinite;
}

.typing-dot:nth-child(2) { animation-delay: 0.2s; }
.typing-dot:nth-child(3) { animation-delay: 0.4s; }

@keyframes bounce {
  0%, 60%, 100% { transform: translateY(0); }
  30% { transform: translateY(-5px); }
}

/* ─── Input Area ─────────────────────────────────────────── */
.input-area {
  padding: 16px 24px 14px;
  border-top: 1px solid var(--border-light);
  flex-shrink: 0;
}

.input-wrapper {
  display: flex;
  align-items: flex-end;
  gap: 10px;
  background: var(--bg-input);
  border: 1px solid var(--border);
  border-radius: var(--radius-lg);
  padding: 10px 10px 10px 16px;
  transition: border-color var(--transition), box-shadow var(--transition);
}

.input-wrapper:focus-within {
  border-color: var(--accent);
  box-shadow: 0 0 0 3px rgba(107,79,187,0.08);
}

#user-input {
  flex: 1;
  border: none;
  background: transparent;
  font-size: 0.9375rem;
  color: var(--text-primary);
  outline: none;
  resize: none;
  font-family: inherit;
  line-height: 1.5;
  max-height: 200px;
  overflow-y: auto;
}

#user-input::placeholder { color: var(--text-muted); }

#send-btn {
  flex-shrink: 0;
  width: 34px; height: 34px;
  background: var(--accent);
  color: white;
  border: none;
  border-radius: 10px;
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: center;
  transition: background var(--transition), opacity var(--transition);
}

#send-btn:hover { background: var(--accent-hover); }
#send-btn:disabled { opacity: 0.4; cursor: not-allowed; }

.footer-note {
  text-align: center;
  font-size: 0.75rem;
  color: var(--text-muted);
  margin-top: 10px;
}

/* ─── Scrollbar ──────────────────────────────────────────── */
::-webkit-scrollbar { width: 5px; }
::-webkit-scrollbar-track { background: transparent; }
::-webkit-scrollbar-thumb { background: var(--border); border-radius: 99px; }
::-webkit-scrollbar-thumb:hover { background: var(--text-muted); }

/* ─── Responsive ─────────────────────────────────────────── */
@media (max-width: 640px) {
  .sidebar { position: absolute; z-index: 100; height: 100%; box-shadow: 4px 0 16px rgba(0,0,0,0.1); }
  .sidebar.collapsed { width: 0; min-width: 0; }
  .message-bubble { max-width: 90%; }
  .main-header { padding: 0 14px; }
  .input-area { padding: 12px 14px 10px; }
  .chat-messages { padding: 16px 0; }
  .message { padding: 5px 14px; }
}
