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

:root {
  --primary: #5f8bcc;
  --primary-dark: #4a74b5;
  --bg: #f5f7fa;
  --white: #ffffff;
  --bot-bubble: #f0f2f5;
  --text: #1a1a2e;
  --text-light: #6b7280;
  --radius: 18px;
  --shadow-sm: 0 1px 3px rgba(0,0,0,.08);
  --shadow-md: 0 4px 12px rgba(0,0,0,.1);
}

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

/* ── Layout ───────────────────────────────────────────── */
.chat-container {
  display: flex;
  flex-direction: column;
  max-width: 760px;
  height: 100vh;
  margin: 0 auto;
  background: var(--white);
  box-shadow: var(--shadow-md);
}

/* ── Header ───────────────────────────────────────────── */
.chat-header {
  background: var(--primary);
  color: var(--white);
  padding: 18px 24px;
  flex-shrink: 0;
}

.header-content h1 {
  font-size: 1.25rem;
  font-weight: 700;
  letter-spacing: -.01em;
}

.header-content p {
  font-size: .8rem;
  opacity: .85;
  margin-top: 2px;
  font-weight: 400;
}

/* ── Messages Area ────────────────────────────────────── */
.chat-messages {
  flex: 1;
  overflow-y: auto;
  padding: 24px 20px 12px;
  scroll-behavior: smooth;
  background: var(--white);
}

.chat-messages::-webkit-scrollbar { width: 5px; }
.chat-messages::-webkit-scrollbar-thumb { background: #d1d5db; border-radius: 4px; }

/* ── Message Row ──────────────────────────────────────── */
.message-row {
  display: flex;
  gap: 10px;
  margin-bottom: 16px;
  animation: fadeUp .35s ease both;
}

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

@keyframes fadeUp {
  from { opacity: 0; transform: translateY(10px); }
  to   { opacity: 1; transform: translateY(0); }
}

/* ── Avatar ───────────────────────────────────────────── */
.avatar {
  width: 34px;
  height: 34px;
  border-radius: 50%;
  background: var(--primary);
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 16px;
  flex-shrink: 0;
  align-self: flex-end;
  box-shadow: var(--shadow-sm);
}

.message-row.user .avatar { display: none; }

/* ── Bubble ───────────────────────────────────────────── */
.bubble {
  max-width: 78%;
  padding: 12px 16px;
  line-height: 1.55;
  font-size: .925rem;
  word-wrap: break-word;
}

.message-row.bot .bubble {
  background: var(--bot-bubble);
  color: var(--text);
  border-radius: var(--radius) var(--radius) var(--radius) 4px;
}

.message-row.user .bubble {
  background: var(--primary);
  color: var(--white);
  border-radius: var(--radius) var(--radius) 4px var(--radius);
}

/* ── Markdown inside bubbles ──────────────────────────── */
.bubble p              { margin-bottom: .45em; }
.bubble p:last-child   { margin-bottom: 0; }
.bubble h1 { font-size: 1.3em; font-weight: 700; margin: 0.6em 0 0.3em; color: #2c3e50; }
.bubble h2 { font-size: 1.15em; font-weight: 700; margin: 0.5em 0 0.25em; color: #2c3e50; }
.bubble h3 { font-size: 1.05em; font-weight: 600; margin: 0.4em 0 0.2em; color: #34495e; }
.bubble h4 { font-size: 1em; font-weight: 600; margin: 0.3em 0 0.15em; color: #34495e; }
.bubble hr { border: none; border-top: 1px solid #ddd; margin: 0.8em 0; }
.bubble blockquote {
    border-left: 3px solid #5f8bcc;
    margin: 0.5em 0;
    padding: 0.4em 0.8em;
    background: rgba(95, 139, 204, 0.06);
    color: #555;
    font-style: italic;
    border-radius: 0 6px 6px 0;
}
.bubble ul, .bubble ol { margin: 0.3em 0 0.3em 1.2em; }
.bubble li { margin-bottom: 0.2em; }
.bubble code { background: rgba(0,0,0,0.06); padding: 0.15em 0.35em; border-radius: 3px; font-size: 0.9em; }
.bubble strong { font-weight: 600; }
.bubble a { color: #5f8bcc; text-decoration: underline; }
.bubble table { width: 100%; border-collapse: collapse; margin: 0.5em 0; font-size: 0.92em; }
.bubble th { background: rgba(95,139,204,0.1); text-align: left; padding: 6px 10px; border-bottom: 2px solid #5f8bcc; font-weight: 600; }
.bubble td { padding: 5px 10px; border-bottom: 1px solid #e8e8e8; }
.bubble tr:last-child td { border-bottom: none; }
.bubble tbody tr:hover { background: rgba(95,139,204,0.04); }
.bubble strong         { font-weight: 600; }
.bubble ul, .bubble ol { padding-left: 1.3em; margin: .4em 0; }
.bubble li             { margin-bottom: .25em; }
.bubble a              { color: var(--primary-dark); text-decoration: underline; }
.message-row.user .bubble a { color: #dbeafe; }
.bubble code {
  background: rgba(0,0,0,.06);
  padding: 1px 5px;
  border-radius: 4px;
  font-size: .85em;
}

/* ── Citations ────────────────────────────────────────── */
.citations {
  display: flex;
  flex-wrap: wrap;
  gap: 6px;
  margin-top: 8px;
}

.citation-tag {
  display: inline-block;
  font-size: .72rem;
  padding: 3px 10px;
  background: #e8edf4;
  color: var(--primary-dark);
  border-radius: 12px;
  cursor: pointer;
  text-decoration: none;
  transition: background .2s, color .2s;
  border: 1px solid transparent;
}

.citation-tag:hover {
  background: var(--primary);
  color: var(--white);
}

/* ── Typing Indicator ─────────────────────────────────── */
.typing-indicator {
  display: flex;
  gap: 5px;
  padding: 14px 18px;
  align-items: center;
}

.typing-indicator span {
  width: 8px;
  height: 8px;
  background: #b0b8c4;
  border-radius: 50%;
  animation: bounce 1.4s infinite ease-in-out both;
}

.typing-indicator span:nth-child(2) { animation-delay: .16s; }
.typing-indicator span:nth-child(3) { animation-delay: .32s; }
.typing-status { font-size: 0.75em; color: #8a9bb5; margin-top: 4px; margin-left: 2px; }

@keyframes bounce {
  0%, 80%, 100% { transform: scale(0.7); opacity: .4; }
  40%           { transform: scale(1);   opacity: 1; }
}

/* ── Input Bar ────────────────────────────────────────── */
.chat-input-bar {
  flex-shrink: 0;
  padding: 12px 16px 14px;
  background: var(--white);
  border-top: 1px solid #e5e7eb;
}

.input-wrapper {
  display: flex;
  gap: 10px;
  align-items: center;
}

#messageInput {
  flex: 1;
  padding: 12px 18px;
  border: 1.5px solid #d1d5db;
  border-radius: 28px;
  font-size: .925rem;
  outline: none;
  transition: border-color .2s, box-shadow .2s;
  font-family: inherit;
  background: var(--bg);
}

#messageInput:focus {
  border-color: var(--primary);
  box-shadow: 0 0 0 3px rgba(95,139,204,.18);
}

#sendBtn {
  width: 44px;
  height: 44px;
  border: none;
  border-radius: 50%;
  background: var(--primary);
  color: var(--white);
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: center;
  flex-shrink: 0;
  transition: background .2s, transform .15s;
}

#sendBtn:hover { background: var(--primary-dark); }
#sendBtn:active { transform: scale(.93); }
#sendBtn:disabled { opacity: .5; cursor: not-allowed; }

/* ── Responsive ───────────────────────────────────────── */
@media (max-width: 600px) {
  .chat-container { max-width: 100%; }
  .chat-header { padding: 14px 16px; }
  .header-content h1 { font-size: 1.1rem; }
  .bubble { max-width: 88%; font-size: .875rem; }
  .chat-messages { padding: 16px 12px 8px; }
}
