/* ── Design Tokens ── */
:root {
  --color-bg: #ffffff;
  --color-surface: #f7f8fa;
  --color-border: #e2e5ea;
  --color-text: #1a1a2e;
  --color-text-muted: #6b7280;
  --color-accent: #2563eb;
  --color-accent-hover: #1d4ed8;
  --color-accent-light: #dbeafe;
  --color-user-msg: #2563eb;
  --color-user-text: #ffffff;
  --color-assistant-bg: #f7f8fa;
  --color-error: #dc2626;
  --color-code-bg: #f1f5f9;
  --font-sans: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif;
  --font-mono: "SF Mono", "Fira Code", Consolas, monospace;
  --radius: 8px;
  --radius-lg: 16px;
  --sidebar-width: 280px;
}

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

body {
  font-family: var(--font-sans);
  color: var(--color-text);
  background: var(--color-bg);
  height: 100dvh;
  overflow: hidden;
}

button {
  font-family: inherit;
  cursor: pointer;
  border: none;
  background: none;
  color: inherit;
}

input, textarea {
  font-family: inherit;
  font-size: inherit;
}

/* ── Setup Overlay ── */
.overlay {
  position: fixed;
  inset: 0;
  display: flex;
  align-items: center;
  justify-content: center;
  background: var(--color-surface);
  z-index: 100;
}

.overlay[hidden] {
  display: none;
}

.setup-card {
  background: var(--color-bg);
  border: 1px solid var(--color-border);
  border-radius: var(--radius-lg);
  padding: 2.5rem 2rem;
  width: 100%;
  max-width: 400px;
  box-shadow: 0 4px 24px rgba(37, 99, 235, 0.15);
  animation: card-glow 4s ease infinite;
}

@keyframes card-glow {
  0%   { box-shadow: 0 -4px 20px rgba(37, 99, 235, 0.2), 0 4px 20px rgba(37, 99, 235, 0.05); }
  25%  { box-shadow: 4px 0 20px rgba(124, 58, 237, 0.2), -4px 0 20px rgba(124, 58, 237, 0.05); }
  50%  { box-shadow: 0 4px 20px rgba(236, 72, 153, 0.2), 0 -4px 20px rgba(236, 72, 153, 0.05); }
  75%  { box-shadow: -4px 0 20px rgba(124, 58, 237, 0.2), 4px 0 20px rgba(124, 58, 237, 0.05); }
  100% { box-shadow: 0 -4px 20px rgba(37, 99, 235, 0.2), 0 4px 20px rgba(37, 99, 235, 0.05); }
}

.setup-card h1 {
  font-size: 1.75rem;
  font-weight: 700;
  color: var(--color-accent);
  margin-bottom: 0.25rem;
}

.setup-subtitle {
  color: var(--color-text-muted);
  font-size: 0.9rem;
  margin-bottom: 1.5rem;
}

.setup-card label {
  display: block;
  font-size: 0.8rem;
  font-weight: 600;
  color: var(--color-text-muted);
  text-transform: uppercase;
  letter-spacing: 0.03em;
  margin-bottom: 0.35rem;
}

.setup-card input {
  display: block;
  width: 100%;
  padding: 0.6rem 0.75rem;
  border: 1px solid var(--color-border);
  border-radius: var(--radius);
  font-size: 0.95rem;
  margin-bottom: 1rem;
  transition: border-color 0.15s;
}

.setup-card input:focus {
  outline: none;
  border-color: var(--color-accent);
  box-shadow: 0 0 0 3px var(--color-accent-light);
}

.error-msg {
  color: var(--color-error);
  font-size: 0.85rem;
  margin-bottom: 0.75rem;
}

.error-msg[hidden] {
  display: none;
}

.setup-card button[type="submit"] {
  width: 100%;
  padding: 0.7rem;
  background: var(--color-accent);
  color: white;
  font-size: 1rem;
  font-weight: 600;
  border-radius: var(--radius);
  transition: background 0.15s;
}

.setup-card button[type="submit"]:hover {
  background: var(--color-accent-hover);
}

.setup-card button[type="submit"]:disabled {
  opacity: 0.6;
  cursor: not-allowed;
}

/* ── App Layout ── */
#app {
  display: flex;
  height: 100dvh;
}

#app[hidden] {
  display: none;
}

/* ── Sidebar ── */
#sidebar {
  width: var(--sidebar-width);
  flex-shrink: 0;
  display: flex;
  flex-direction: column;
  background: var(--color-surface);
  border-right: 1px solid var(--color-border);
}

.sidebar-header {
  padding: 0.75rem;
  border-bottom: 1px solid var(--color-border);
}

#new-chat-btn {
  width: 100%;
  padding: 0.55rem 0.75rem;
  background: var(--color-bg);
  border: 1px solid var(--color-border);
  border-radius: var(--radius);
  font-size: 0.875rem;
  font-weight: 500;
  text-align: left;
  transition: background 0.15s;
}

#new-chat-btn:hover {
  background: var(--color-accent-light);
  border-color: var(--color-accent);
  color: var(--color-accent);
}

#conversation-list {
  flex: 1;
  overflow-y: auto;
  padding: 0.5rem;
}

.conv-item {
  display: flex;
  align-items: center;
  gap: 0.5rem;
  padding: 0.55rem 0.75rem;
  border-radius: var(--radius);
  font-size: 0.85rem;
  color: var(--color-text);
  cursor: pointer;
  transition: background 0.1s;
}

.conv-item:hover {
  background: var(--color-border);
}

.conv-item.active {
  background: var(--color-accent-light);
  color: var(--color-accent);
  font-weight: 500;
}

.conv-item-title {
  flex: 1;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.conv-item-delete {
  opacity: 0;
  flex-shrink: 0;
  font-size: 0.75rem;
  color: var(--color-text-muted);
  padding: 0.15rem 0.35rem;
  border-radius: 4px;
  transition: opacity 0.1s, color 0.1s;
}

.conv-item:hover .conv-item-delete {
  opacity: 1;
}

.conv-item-delete:hover {
  color: var(--color-error);
  background: rgba(220, 38, 38, 0.08);
}

.sidebar-footer {
  padding: 0.75rem;
  border-top: 1px solid var(--color-border);
}

#settings-btn {
  display: flex;
  align-items: center;
  gap: 0.5rem;
  font-size: 0.85rem;
  color: var(--color-text-muted);
  padding: 0.4rem 0.5rem;
  border-radius: var(--radius);
  width: 100%;
  transition: background 0.1s;
}

#settings-btn:hover {
  background: var(--color-border);
  color: var(--color-text);
}

/* ── Chat Area ── */
#chat-area {
  flex: 1;
  display: flex;
  flex-direction: column;
  min-width: 0;
}

#chat-header {
  display: flex;
  align-items: center;
  gap: 0.75rem;
  padding: 0.75rem 1rem;
  border-bottom: 1px solid var(--color-border);
  font-weight: 600;
  font-size: 0.95rem;
}

#sidebar-toggle {
  display: none;
  padding: 0.3rem;
  border-radius: var(--radius);
  color: var(--color-text-muted);
}

#sidebar-toggle:hover {
  background: var(--color-surface);
}

/* ── Messages ── */
#messages {
  flex: 1;
  overflow-y: auto;
  padding: 1.5rem 1rem;
}

.welcome {
  text-align: center;
  padding: 4rem 1rem;
  color: var(--color-text-muted);
}

.welcome h2 {
  font-size: 1.35rem;
  color: var(--color-text);
  margin-bottom: 0.5rem;
}

.welcome p {
  font-size: 0.95rem;
}

.message-row {
  max-width: 720px;
  margin: 0 auto 1rem;
  display: flex;
  animation: msgIn 0.15s ease;
}

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

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

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

.message-bubble {
  max-width: 85%;
  padding: 0.7rem 1rem;
  border-radius: var(--radius-lg);
  line-height: 1.55;
  font-size: 0.925rem;
  word-wrap: break-word;
  overflow-wrap: break-word;
}

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

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

/* ── Streaming indicator ── */
.typing-indicator::after {
  content: "...";
  animation: dots 1s steps(4, end) infinite;
}

@keyframes dots {
  0%, 20% { content: ""; }
  40% { content: "."; }
  60% { content: ".."; }
  80%, 100% { content: "..."; }
}

/* ── Markdown in messages ── */
.message-bubble p { margin-bottom: 0.5em; }
.message-bubble p:last-child { margin-bottom: 0; }
.message-bubble strong { font-weight: 600; }
.message-bubble code {
  background: var(--color-code-bg);
  font-family: var(--font-mono);
  font-size: 0.85em;
  padding: 0.15em 0.35em;
  border-radius: 4px;
}
.message-bubble pre {
  background: var(--color-code-bg);
  border-radius: var(--radius);
  padding: 0.75rem 1rem;
  overflow-x: auto;
  margin: 0.5em 0;
}
.message-bubble pre code {
  background: none;
  padding: 0;
  font-size: 0.85em;
}
.message-bubble blockquote {
  border-left: 3px solid var(--color-accent);
  padding-left: 0.75rem;
  color: var(--color-text-muted);
  margin: 0.5em 0;
}
.message-bubble ul, .message-bubble ol {
  padding-left: 1.4em;
  margin: 0.5em 0;
}
.message-bubble li { margin-bottom: 0.25em; }
.message-bubble h3, .message-bubble h4, .message-bubble h5 {
  margin-top: 0.75em;
  margin-bottom: 0.25em;
}
.message-bubble hr {
  border: none;
  border-top: 1px solid var(--color-border);
  margin: 0.75em 0;
}
.message-bubble a {
  color: var(--color-accent);
  text-decoration: underline;
}

/* ── Source Citations ── */
.sources {
  margin-top: 0.5rem;
}

.sources summary {
  font-size: 0.8rem;
  color: var(--color-accent);
  cursor: pointer;
  font-weight: 500;
  padding: 0.25rem 0;
  list-style: none;
}

.sources summary::before {
  content: "[ ";
}

.sources summary::after {
  content: " ]";
}

.sources summary::-webkit-details-marker {
  display: none;
}

.sources[open] summary {
  margin-bottom: 0.4rem;
}

.source-card {
  background: var(--color-bg);
  border: 1px solid var(--color-border);
  border-radius: var(--radius);
  padding: 0.6rem 0.75rem;
  margin-bottom: 0.4rem;
  font-size: 0.8rem;
}

.source-title {
  font-weight: 600;
  color: var(--color-text);
  margin-bottom: 0.2rem;
}

.source-text {
  color: var(--color-text-muted);
  line-height: 1.45;
  display: -webkit-box;
  -webkit-line-clamp: 3;
  -webkit-box-orient: vertical;
  overflow: hidden;
}

.source-text.expanded {
  display: block;
  -webkit-line-clamp: unset;
}

.source-expand {
  font-size: 0.75rem;
  color: var(--color-accent);
  margin-top: 0.2rem;
  cursor: pointer;
  display: inline-block;
}

/* ── Input Area ── */
#input-area {
  flex-shrink: 0;
  border-top: 1px solid var(--color-border);
  padding: 0.75rem 1rem;
  background: var(--color-bg);
}

#chat-form {
  max-width: 720px;
  margin: 0 auto;
  display: flex;
  align-items: flex-end;
  gap: 0.5rem;
  background: var(--color-surface);
  border: 1px solid var(--color-border);
  border-radius: var(--radius-lg);
  padding: 0.5rem 0.5rem 0.5rem 1rem;
  transition: border-color 0.15s;
}

#chat-form:focus-within {
  border-color: var(--color-accent);
  box-shadow: 0 0 0 3px var(--color-accent-light);
}

#chat-input {
  flex: 1;
  border: none;
  background: none;
  resize: none;
  font-size: 0.925rem;
  line-height: 1.5;
  max-height: 150px;
  outline: none;
  padding: 0.25rem 0;
}

#send-btn {
  flex-shrink: 0;
  width: 36px;
  height: 36px;
  display: flex;
  align-items: center;
  justify-content: center;
  background: var(--color-accent);
  color: white;
  border-radius: 50%;
  transition: background 0.15s;
}

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

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

/* ── Inline error ── */
.chat-error {
  max-width: 720px;
  margin: 0.5rem auto;
  padding: 0.6rem 1rem;
  background: #fef2f2;
  border: 1px solid #fecaca;
  border-radius: var(--radius);
  color: var(--color-error);
  font-size: 0.85rem;
}

/* ── Responsive ── */
@media (max-width: 768px) {
  #sidebar {
    position: fixed;
    inset: 0;
    width: 100%;
    max-width: 300px;
    z-index: 50;
    transform: translateX(-100%);
    transition: transform 0.2s ease;
  }

  #app.sidebar-open #sidebar {
    transform: translateX(0);
  }

  #sidebar-toggle {
    display: block;
  }

  .sidebar-backdrop {
    display: none;
    position: fixed;
    inset: 0;
    background: rgba(0, 0, 0, 0.3);
    z-index: 40;
  }

  #app.sidebar-open .sidebar-backdrop {
    display: block;
  }

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