:root {
  --bg: #f5f1e8;
  --surface: #ffffff;
  --surface-alt: #faf7f0;
  --border: #e2d9c3;
  --text: #2b2620;
  --text-muted: #6b6255;
  --accent: #7a4b2a;
  --accent-strong: #5c3820;
  --user-bubble: #7a4b2a;
  --user-bubble-text: #ffffff;
  --assistant-bubble: #ffffff;
  --assistant-bubble-text: #2b2620;
  --source-chip-bg: #f0e6d2;
  --source-chip-text: #5c3820;
  --error-bg: #fbe5e1;
  --error-text: #8a2c1f;
  --shadow: 0 2px 10px rgba(43, 38, 32, 0.08);
  --radius: 14px;
  --font: "Iowan Old Style", "Palatino Linotype", Georgia, "Times New Roman", serif;
}

[data-theme="dark"] {
  --bg: #1c1a17;
  --surface: #262320;
  --surface-alt: #2c2926;
  --border: #3d3833;
  --text: #ece5d8;
  --text-muted: #a89e8c;
  --accent: #c99a5b;
  --accent-strong: #dcb27a;
  --user-bubble: #c99a5b;
  --user-bubble-text: #1c1a17;
  --assistant-bubble: #2c2926;
  --assistant-bubble-text: #ece5d8;
  --source-chip-bg: #3a342b;
  --source-chip-text: #dcb27a;
  --error-bg: #3a201c;
  --error-text: #f2a191;
  --shadow: 0 2px 10px rgba(0, 0, 0, 0.35);
}

* { box-sizing: border-box; }

html, body {
  height: 100%;
  margin: 0;
}

body {
  font-family: var(--font);
  background: var(--bg);
  color: var(--text);
  transition: background 0.2s ease, color 0.2s ease;
}

.app {
  max-width: 820px;
  margin: 0 auto;
  min-height: 100vh;
  display: flex;
  flex-direction: column;
  padding: 0 16px;
}

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

.brand {
  display: flex;
  align-items: center;
  gap: 12px;
}

.brand-icon { font-size: 2rem; }

.brand h1 {
  margin: 0;
  font-size: 1.3rem;
  letter-spacing: 0.01em;
}

.subtitle {
  margin: 2px 0 0;
  font-size: 0.85rem;
  color: var(--text-muted);
}

.theme-toggle {
  border: 1px solid var(--border);
  background: var(--surface);
  color: var(--text);
  border-radius: 999px;
  width: 40px;
  height: 40px;
  cursor: pointer;
  font-size: 1.1rem;
  display: flex;
  align-items: center;
  justify-content: center;
  transition: background 0.15s ease;
}
.theme-toggle:hover { background: var(--surface-alt); }

.chat-container {
  flex: 1;
  display: flex;
  flex-direction: column;
  padding: 16px 0 24px;
  min-height: 0;
}

.chat-log {
  flex: 1;
  overflow-y: auto;
  display: flex;
  flex-direction: column;
  gap: 14px;
  padding: 8px 2px 12px;
}

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

.bubble {
  max-width: 82%;
  padding: 12px 16px;
  border-radius: var(--radius);
  box-shadow: var(--shadow);
  line-height: 1.5;
  font-size: 0.98rem;
}

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

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

.bubble p:first-child { margin-top: 0; }
.bubble p:last-child { margin-bottom: 0; }
.bubble code {
  background: var(--surface-alt);
  padding: 1px 5px;
  border-radius: 4px;
  font-size: 0.9em;
}

.sources {
  margin-top: 10px;
  display: flex;
  flex-wrap: wrap;
  gap: 6px;
}

.source-chip {
  background: var(--source-chip-bg);
  color: var(--source-chip-text);
  border-radius: 999px;
  padding: 4px 10px;
  font-size: 0.78rem;
  white-space: nowrap;
}

.typing-indicator {
  display: inline-flex;
  gap: 4px;
  align-items: center;
}
.typing-indicator span {
  width: 6px;
  height: 6px;
  border-radius: 50%;
  background: var(--text-muted);
  animation: blink 1.2s infinite ease-in-out;
}
.typing-indicator span:nth-child(2) { animation-delay: 0.2s; }
.typing-indicator span:nth-child(3) { animation-delay: 0.4s; }
@keyframes blink {
  0%, 80%, 100% { opacity: 0.2; }
  40% { opacity: 1; }
}

.suggested-questions {
  display: flex;
  flex-wrap: wrap;
  gap: 8px;
  padding: 4px 2px 12px;
}

.suggestion-chip {
  background: var(--surface);
  border: 1px solid var(--border);
  color: var(--text);
  border-radius: 999px;
  padding: 7px 14px;
  font-size: 0.85rem;
  cursor: pointer;
  transition: background 0.15s ease, border-color 0.15s ease;
}
.suggestion-chip:hover {
  background: var(--surface-alt);
  border-color: var(--accent);
}

.error-banner {
  background: var(--error-bg);
  color: var(--error-text);
  border-radius: 10px;
  padding: 10px 14px;
  font-size: 0.88rem;
  margin: 0 2px 10px;
}
.hidden { display: none !important; }

.chat-input-row {
  display: flex;
  align-items: flex-end;
  gap: 10px;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 18px;
  padding: 8px 8px 8px 16px;
  box-shadow: var(--shadow);
}

.chat-input {
  flex: 1;
  border: none;
  outline: none;
  background: transparent;
  color: var(--text);
  font-family: inherit;
  font-size: 0.98rem;
  resize: none;
  max-height: 140px;
  padding: 8px 0;
}
.chat-input::placeholder { color: var(--text-muted); }

.send-button {
  background: var(--accent);
  color: #fff;
  border: none;
  border-radius: 50%;
  width: 42px;
  height: 42px;
  flex-shrink: 0;
  cursor: pointer;
  font-size: 1.1rem;
  display: flex;
  align-items: center;
  justify-content: center;
  transition: background 0.15s ease, opacity 0.15s ease;
}
.send-button:hover { background: var(--accent-strong); }
.send-button:disabled { opacity: 0.5; cursor: not-allowed; }

.footnote {
  text-align: center;
  font-size: 0.75rem;
  color: var(--text-muted);
  margin: 10px 4px 0;
}

@media (max-width: 560px) {
  .app-header { padding: 14px 2px 12px; }
  .brand h1 { font-size: 1.1rem; }
  .subtitle { display: none; }
  .bubble { max-width: 90%; font-size: 0.95rem; }
}
