/* ============================================
   Design Tokens
   ============================================ */
:root {
  --color-primary:        #2563eb;
  --color-primary-dark:   #1d4ed8;
  --color-primary-light:  #eff6ff;

  --color-bg:             #f1f5f9;
  --color-surface:        #ffffff;
  --color-border:         #e2e8f0;

  --color-text:           #1e293b;
  --color-text-muted:     #64748b;
  --color-text-inverse:   #ffffff;

  --color-user-bubble:    #2563eb;
  --color-assistant-bubble: #ffffff;
  --color-error:          #ef4444;

  --radius-sm:    6px;
  --radius-md:    12px;
  --radius-lg:    18px;
  --radius-full:  9999px;

  --shadow-sm:  0 1px 2px rgba(0,0,0,0.05);
  --shadow-md:  0 4px 12px rgba(0,0,0,0.08);
  --shadow-lg:  0 8px 24px rgba(0,0,0,0.12);

  --font-sans: system-ui, -apple-system, BlinkMacSystemFont, 'Segoe UI', sans-serif;

  --header-height: 72px;
  --input-area-height: 96px;
}

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

html, body {
  height: 100%;
  font-family: var(--font-sans);
  font-size: 15px;
  color: var(--color-text);
  background: var(--color-bg);
  -webkit-font-smoothing: antialiased;
}

/* ============================================
   App Layout
   ============================================ */
.app {
  display: flex;
  flex-direction: column;
  height: 100vh;
  max-width: 800px;
  margin: 0 auto;
  background: var(--color-surface);
  box-shadow: var(--shadow-lg);
}

/* ============================================
   Header
   ============================================ */
.header {
  height: var(--header-height);
  background: var(--color-surface);
  border-bottom: 1px solid var(--color-border);
  padding: 0 24px;
  display: flex;
  align-items: center;
  flex-shrink: 0;
  position: sticky;
  top: 0;
  z-index: 10;
}

.header-content {
  display: flex;
  align-items: center;
  gap: 14px;
}

.header-icon {
  font-size: 28px;
  line-height: 1;
  background: var(--color-primary-light);
  width: 44px;
  height: 44px;
  border-radius: var(--radius-md);
  display: flex;
  align-items: center;
  justify-content: center;
}

.header-text h1 {
  font-size: 17px;
  font-weight: 600;
  color: var(--color-text);
  letter-spacing: -0.01em;
}

.header-text p {
  font-size: 13px;
  color: var(--color-text-muted);
  margin-top: 1px;
}

/* ============================================
   Chat Window
   ============================================ */
.chat-window {
  flex: 1;
  overflow-y: auto;
  padding: 24px 24px 8px;
  scroll-behavior: smooth;
}

.messages {
  display: flex;
  flex-direction: column;
  gap: 16px;
}

/* ============================================
   Message Bubbles
   ============================================ */
.message {
  display: flex;
  gap: 10px;
  animation: fadeSlideIn 0.2s ease;
}

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

/* Avatar */
.message-avatar {
  width: 32px;
  height: 32px;
  border-radius: var(--radius-full);
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 15px;
  flex-shrink: 0;
  margin-top: 2px;
}

.message.assistant .message-avatar {
  background: var(--color-primary-light);
}

.message.user .message-avatar {
  background: var(--color-primary);
  color: var(--color-text-inverse);
  font-size: 13px;
  font-weight: 600;
}

/* Bubble */
.message-bubble {
  max-width: 72%;
  padding: 12px 16px;
  border-radius: var(--radius-lg);
  line-height: 1.6;
  font-size: 15px;
}

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

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

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

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

/* Error state */
.message.error .message-bubble {
  background: #fef2f2;
  border-color: #fecaca;
  color: var(--color-error);
}

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

.typing-indicator span {
  width: 7px;
  height: 7px;
  background: var(--color-text-muted);
  border-radius: var(--radius-full);
  display: inline-block;
  animation: bounce 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 bounce {
  0%, 60%, 100% { transform: translateY(0); }
  30%           { transform: translateY(-6px); }
}

/* ============================================
   Input Area
   ============================================ */
.input-area {
  padding: 12px 24px 16px;
  border-top: 1px solid var(--color-border);
  background: var(--color-surface);
  flex-shrink: 0;
}

.input-form {
  display: flex;
  align-items: flex-end;
  gap: 10px;
  background: var(--color-bg);
  border: 1.5px solid var(--color-border);
  border-radius: var(--radius-lg);
  padding: 10px 10px 10px 16px;
  transition: border-color 0.15s;
}

.input-form:focus-within {
  border-color: var(--color-primary);
  box-shadow: 0 0 0 3px rgba(37, 99, 235, 0.1);
}

textarea#user-input {
  flex: 1;
  border: none;
  background: transparent;
  resize: none;
  font-family: var(--font-sans);
  font-size: 15px;
  color: var(--color-text);
  line-height: 1.5;
  max-height: 160px;
  outline: none;
}

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

#send-btn {
  width: 36px;
  height: 36px;
  border-radius: var(--radius-md);
  background: var(--color-primary);
  color: var(--color-text-inverse);
  border: none;
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: center;
  flex-shrink: 0;
  transition: background 0.15s, transform 0.1s;
}

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

#send-btn:active {
  transform: scale(0.94);
}

#send-btn:disabled {
  background: var(--color-border);
  cursor: not-allowed;
  transform: none;
}

.input-hint {
  font-size: 12px;
  color: var(--color-text-muted);
  margin-top: 8px;
  text-align: center;
}

.input-hint kbd {
  font-family: var(--font-sans);
  font-size: 11px;
  background: var(--color-bg);
  border: 1px solid var(--color-border);
  border-radius: 4px;
  padding: 1px 5px;
}

/* ============================================
   Suggested Prompts
   ============================================ */
.suggestions {
  display: flex;
  flex-wrap: wrap;
  gap: 8px;
  padding: 4px 0 8px 42px;
  animation: fadeSlideIn 0.25s ease;
}

.suggestion-btn {
  background: var(--color-surface);
  border: 1.5px solid var(--color-border);
  border-radius: var(--radius-full);
  padding: 8px 14px;
  font-family: var(--font-sans);
  font-size: 13px;
  color: var(--color-text);
  cursor: pointer;
  transition: border-color 0.15s, background 0.15s, transform 0.1s;
  white-space: nowrap;
}

.suggestion-btn:hover {
  border-color: var(--color-primary);
  background: var(--color-primary-light);
  color: var(--color-primary);
}

.suggestion-btn:active {
  transform: scale(0.96);
}

/* ============================================
   Markdown Rendering
   ============================================ */
.message-bubble.markdown { line-height: 1.65; }

.message-bubble.markdown p {
  margin-bottom: 10px;
}
.message-bubble.markdown p:last-child {
  margin-bottom: 0;
}

.message-bubble.markdown h1,
.message-bubble.markdown h2,
.message-bubble.markdown h3 {
  font-weight: 600;
  margin: 14px 0 6px;
  line-height: 1.3;
}
.message-bubble.markdown h1 { font-size: 18px; }
.message-bubble.markdown h2 { font-size: 16px; }
.message-bubble.markdown h3 { font-size: 15px; }
.message-bubble.markdown h1:first-child,
.message-bubble.markdown h2:first-child,
.message-bubble.markdown h3:first-child {
  margin-top: 0;
}

.message-bubble.markdown ul,
.message-bubble.markdown ol {
  padding-left: 20px;
  margin-bottom: 10px;
}
.message-bubble.markdown li {
  margin-bottom: 4px;
}

.message-bubble.markdown strong {
  font-weight: 600;
}

.message-bubble.markdown em {
  font-style: italic;
}

.message-bubble.markdown code {
  font-family: 'SF Mono', 'Fira Code', 'Consolas', monospace;
  font-size: 13px;
  background: #f1f5f9;
  border: 1px solid var(--color-border);
  border-radius: 4px;
  padding: 1px 5px;
}

.message-bubble.markdown pre {
  background: #1e293b;
  border-radius: var(--radius-md);
  padding: 14px 16px;
  overflow-x: auto;
  margin: 10px 0;
}
.message-bubble.markdown pre code {
  background: none;
  border: none;
  padding: 0;
  color: #e2e8f0;
  font-size: 13px;
}

.message-bubble.markdown blockquote {
  border-left: 3px solid var(--color-primary);
  padding-left: 12px;
  color: var(--color-text-muted);
  margin: 10px 0;
}

.message-bubble.markdown table {
  width: 100%;
  border-collapse: collapse;
  margin: 10px 0;
  font-size: 14px;
}
.message-bubble.markdown th,
.message-bubble.markdown td {
  text-align: left;
  padding: 8px 12px;
  border: 1px solid var(--color-border);
}
.message-bubble.markdown th {
  background: var(--color-bg);
  font-weight: 600;
}
.message-bubble.markdown tr:nth-child(even) td {
  background: #f8fafc;
}

.message-bubble.markdown hr {
  border: none;
  border-top: 1px solid var(--color-border);
  margin: 12px 0;
}

/* ============================================
   Collapsible Sources
   ============================================ */
.sources-collapsible {
  margin-top: 14px;
  border-top: 1px solid var(--color-border);
  padding-top: 10px;
}

.sources-summary {
  display: flex;
  align-items: center;
  justify-content: space-between;
  cursor: pointer;
  list-style: none;
  user-select: none;
  padding: 4px 0;
}

/* Remove default marker in all browsers */
.sources-summary::-webkit-details-marker { display: none; }
.sources-summary::marker { display: none; }

.sources-label {
  font-size: 13px;
  font-weight: 600;
  color: var(--color-text-muted);
  text-transform: uppercase;
  letter-spacing: 0.05em;
}

.sources-chevron {
  color: var(--color-text-muted);
  display: flex;
  align-items: center;
  transition: transform 0.2s ease;
}

/* Rotate chevron when open */
.sources-collapsible[open] .sources-chevron {
  transform: rotate(180deg);
}

/* Content inside the collapsible */
.sources-collapsible ul {
  margin-top: 10px;
  padding-left: 4px;
  list-style: none;
}

.sources-collapsible li {
  font-size: 13px;
  color: var(--color-text-muted);
  padding: 5px 0;
  border-bottom: 1px solid var(--color-border);
  line-height: 1.5;
}

.sources-collapsible li:last-child {
  border-bottom: none;
}

.sources-collapsible li strong {
  color: var(--color-text);
}

/* ============================================
   Scrollbar
   ============================================ */
.chat-window::-webkit-scrollbar {
  width: 5px;
}

.chat-window::-webkit-scrollbar-track {
  background: transparent;
}

.chat-window::-webkit-scrollbar-thumb {
  background: var(--color-border);
  border-radius: var(--radius-full);
}

/* ============================================
   Responsive
   ============================================ */
@media (max-width: 600px) {
  .app {
    max-width: 100%;
    box-shadow: none;
  }

  .chat-window {
    padding: 16px 16px 8px;
  }

  .input-area {
    padding: 10px 16px 14px;
  }

  .message-bubble {
    max-width: 88%;
  }
}
