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

:root {
  --bg: #f7f8fa;
  --surface: #ffffff;
  --border: #e2e4e9;
  --text: #1a1a2e;
  --text-muted: #6b7280;
  --primary: #2563eb;
  --primary-hover: #1d4ed8;
  --user-bg: #2563eb;
  --user-text: #ffffff;
  --assistant-bg: #f1f3f5;
  --assistant-text: #1a1a2e;
  --radius: 12px;
  --shadow: 0 1px 3px rgba(0, 0, 0, 0.08);
}

body {
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif;
  background: var(--bg);
  color: var(--text);
  line-height: 1.6;
  min-height: 100dvh;
}

/* ── Layout ── */
.container {
  max-width: 780px;
  margin: 0 auto;
  display: flex;
  flex-direction: column;
  min-height: 100dvh;
  padding: 0 16px;
}

header {
  text-align: center;
  padding: 24px 0 12px;
}

header h1 {
  font-size: 1.5rem;
  font-weight: 700;
  color: var(--text);
}

.subtitle {
  color: var(--text-muted);
  font-size: 0.9rem;
  margin-top: 4px;
}

main {
  flex: 1;
  display: flex;
  flex-direction: column;
  gap: 0;
}

footer {
  text-align: center;
  padding: 12px 0;
  color: var(--text-muted);
  font-size: 0.8rem;
  border-top: 1px solid var(--border);
}

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

.message {
  display: flex;
  max-width: 85%;
}

.message.user {
  align-self: flex-end;
}

.message.assistant {
  align-self: flex-start;
}

.message-content {
  padding: 12px 16px;
  border-radius: var(--radius);
  font-size: 0.95rem;
  line-height: 1.6;
  box-shadow: var(--shadow);
  word-break: break-word;
}

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

.message.assistant .message-content {
  background: var(--assistant-bg);
  color: var(--assistant-text);
  border-bottom-left-radius: 4px;
}

.message-content ul {
  margin: 8px 0 0 20px;
}

.message-content li {
  margin-bottom: 4px;
}

.message-content p {
  margin-bottom: 8px;
}

.message-content p:last-child {
  margin-bottom: 0;
}

.message-content strong {
  font-weight: 600;
}

.message-content code {
  background: rgba(0, 0, 0, 0.06);
  padding: 2px 5px;
  border-radius: 4px;
  font-size: 0.88em;
}

/* ── Sources toggle ── */
.sources-toggle {
  margin-top: 8px;
  background: none;
  border: none;
  color: var(--primary);
  cursor: pointer;
  font-size: 0.82rem;
  padding: 2px 0;
}

.sources-toggle:hover {
  text-decoration: underline;
}

.sources-list {
  display: none;
  margin-top: 6px;
  padding: 8px 12px;
  background: rgba(0, 0, 0, 0.03);
  border-radius: 8px;
  font-size: 0.82rem;
  color: var(--text-muted);
  line-height: 1.5;
}

.sources-list.open {
  display: block;
}

.sources-list .source-item {
  margin-bottom: 2px;
}

/* ── Loading indicator ── */
.loading {
  display: flex;
  gap: 4px;
  padding: 4px 0;
}

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

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

.loading-text {
  margin-top: 8px;
  font-size: 0.85em;
  color: var(--text-muted);
  animation: pulse-text 2s infinite;
}

@keyframes pulse-text {
  0%, 100% { opacity: 0.6; }
  50% { opacity: 1; }
}

@keyframes bounce {
  0%, 80%, 100% { opacity: 0.3; transform: scale(0.8); }
  40% { opacity: 1; transform: scale(1); }
}

/* ── Input form ── */
#ask-form {
  padding: 12px 0;
  position: sticky;
  bottom: 0;
  background: var(--bg);
}

.input-row {
  display: flex;
  gap: 8px;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 6px 6px 6px 16px;
  box-shadow: var(--shadow);
  transition: border-color 0.15s;
}

.input-row:focus-within {
  border-color: var(--primary);
}

#question-input {
  flex: 1;
  border: none;
  outline: none;
  font-size: 0.95rem;
  font-family: inherit;
  color: var(--text);
  background: transparent;
}

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

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

#send-btn:hover {
  background: var(--primary-hover);
}

#send-btn:disabled {
  opacity: 0.5;
  cursor: not-allowed;
}

/* ── Error state ── */
.message.error .message-content {
  background: #fef2f2;
  color: #991b1b;
  border: 1px solid #fecaca;
}

/* ── Responsive ── */
@media (max-width: 600px) {
  .message {
    max-width: 92%;
  }

  header h1 {
    font-size: 1.25rem;
  }
}
