:root {
  --bg: #f7f7f8;
  --chat-bg: #ffffff;
  --user-bg: #f7f7f8;
  --border: #e5e5e5;
  --text: #1a1a1a;
  --text-secondary: #6b7280;
  --accent: #2563eb;
  --accent-light: #dbeafe;
  --code-bg: #f3f4f6;
  --max-w: 780px;
}
* { box-sizing: border-box; margin: 0; padding: 0; }
html, body { height: 100%; }
body {
  font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', sans-serif;
  color: var(--text); background: var(--bg);
  display: flex; flex-direction: column;
}

/* Header */
.header {
  background: var(--chat-bg); border-bottom: 1px solid var(--border);
  padding: 0.6rem 1rem; display: flex; align-items: center; gap: 1rem;
  flex-shrink: 0; z-index: 10;
}
.header h1 { font-size: 1rem; font-weight: 600; white-space: nowrap; }
.header-actions { display: flex; gap: 0.5rem; margin-left: auto; align-items: center; }
.header-actions button, .header-actions label {
  font-size: 0.75rem; padding: 0.3rem 0.7rem; border: 1px solid var(--border);
  border-radius: 6px; background: var(--chat-bg); cursor: pointer; color: var(--text-secondary);
  transition: all 0.15s;
}
.header-actions button:hover, .header-actions label:hover {
  border-color: var(--accent); color: var(--accent);
}
.header-actions input[type="file"] { display: none; }
.source-count { font-size: 0.7rem; color: var(--text-secondary); }

/* Model selector */
.model-select {
  font-size: 0.72rem; padding: 0.25rem 0.4rem; border: 1px solid var(--border);
  border-radius: 6px; background: var(--chat-bg); color: var(--text);
  cursor: pointer; max-width: 260px; transition: border-color 0.15s;
}
.model-select:hover, .model-select:focus { border-color: var(--accent); outline: none; }

/* Chat area */
.chat-wrap {
  flex: 1; overflow-y: auto; padding: 1rem 0;
  scroll-behavior: smooth;
}
.chat-messages { max-width: var(--max-w); margin: 0 auto; padding: 0 1rem; }

/* Message */
.msg { padding: 1rem 0; display: flex; gap: 0.8rem; }
.msg + .msg { border-top: 1px solid #f0f0f0; }
.msg-avatar {
  width: 28px; height: 28px; border-radius: 6px; flex-shrink: 0;
  display: flex; align-items: center; justify-content: center;
  font-size: 0.75rem; font-weight: 700; color: #fff;
}
.msg-avatar.user { background: #6366f1; }
.msg-avatar.bot { background: #059669; }
.msg-body { flex: 1; min-width: 0; line-height: 1.65; font-size: 0.9rem; }

/* User message bubble */
.msg-user {
  margin-left: 3rem; padding: 0;
}
.msg-user .msg-body {
  background: #eef2ff; border: 1px solid #e0e7ff; border-radius: 12px;
  padding: 0.7rem 1rem; font-size: 0.88rem; color: #1e1b4b;
}

/* Markdown content */
.msg-body h1, .msg-body h2, .msg-body h3 { margin: 0.8rem 0 0.3rem; font-size: 1rem; }
.msg-body h1 { font-size: 1.1rem; }
.msg-body p { margin: 0.3rem 0; }
.msg-body ul, .msg-body ol { margin: 0.3rem 0 0.3rem 1.5rem; }
.msg-body li { margin: 0.15rem 0; }
.msg-body code {
  background: var(--code-bg); padding: 0.15rem 0.35rem; border-radius: 4px;
  font-size: 0.82rem; font-family: 'SF Mono', Monaco, monospace;
}
.msg-body pre { background: var(--code-bg); padding: 0.8rem; border-radius: 8px;
  overflow-x: auto; margin: 0.5rem 0; }
.msg-body pre code { background: none; padding: 0; }
.msg-body blockquote {
  border-left: 3px solid var(--accent); padding-left: 0.8rem;
  color: var(--text-secondary); margin: 0.5rem 0;
}
.msg-body strong { font-weight: 600; }
.msg-body a { color: var(--accent); text-decoration: none; }
.msg-body a:hover { text-decoration: underline; }
.msg-body table { border-collapse: collapse; margin: 0.5rem 0; font-size: 0.85rem; }
.msg-body th, .msg-body td { border: 1px solid var(--border); padding: 0.3rem 0.6rem; }
.msg-body th { background: var(--code-bg); font-weight: 600; }

/* Inline images – flow side by side */
.msg-body p:has(img) {
  display: flex; flex-wrap: wrap; gap: 0.4rem; align-items: flex-start;
}
.msg-body img {
  max-width: 200px; max-height: 170px; border-radius: 6px;
  border: 1px solid var(--border); cursor: pointer;
  transition: transform 0.2s; object-fit: contain; flex-shrink: 0;
}
.msg-body img:hover { transform: scale(1.04); box-shadow: 0 2px 8px rgba(0,0,0,0.12); }
body.delete-mode .msg-body img { cursor: crosshair; outline: 2px dashed #ef4444; outline-offset: 2px; }
.msg-body p:has(img) img:only-child {
  max-width: 360px; max-height: 280px;
}

/* Images inside list items (step-by-step instructions) */
.msg-body li:has(img) {
  display: flex; flex-wrap: wrap; align-items: flex-start; gap: 0.5rem;
}
.msg-body li img {
  display: block; margin: 0.3rem 0;
  max-width: 280px; max-height: 220px;
}
.msg-body li > p { flex: 1 1 200px; min-width: 0; }

/* Source links */
.msg-sources {
  margin-top: 0.6rem; padding-top: 0.5rem; border-top: 1px solid #f0f0f0;
  display: flex; flex-wrap: wrap; gap: 0.35rem; align-items: center;
}
.msg-sources .label { font-size: 0.7rem; color: var(--text-secondary); margin-right: 0.2rem; }
.src-link {
  font-size: 0.72rem; padding: 0.2rem 0.5rem; background: var(--accent-light);
  border-radius: 4px; text-decoration: none; color: var(--accent);
  font-weight: 500; transition: all 0.15s; white-space: nowrap;
}
.src-link:hover { background: var(--accent); color: #fff; }

/* Thinking toggle */
.thinking-toggle {
  display: flex; align-items: center; gap: 0.3rem; cursor: pointer;
  font-size: 0.72rem; color: var(--text-secondary);
}
.thinking-toggle input { width: 14px; height: 14px; cursor: pointer; accent-color: var(--accent); }
.toggle-label { white-space: nowrap; }

/* Thinking stream */
.thinking-stream {
  font-style: italic; color: #9ca3af; font-size: 0.78rem;
  line-height: 1.5; white-space: pre-wrap; overflow: hidden;
  max-height: 4.5em;
  font-family: 'SF Mono', Monaco, Consolas, monospace;
  border-left: 2px solid #d1d5db; padding-left: 0.6rem;
  animation: thinkPulse 2s ease-in-out infinite;
}
@keyframes thinkPulse {
  0%, 100% { opacity: 0.6; }
  50% { opacity: 1; }
}

/* Message meta (time, tokens, cost) */
.msg-meta { font-size: 0.65rem; color: #b0b0b0; margin-top: 0.3rem; }

/* Typing indicator */
.typing { display: flex; gap: 4px; padding: 0.5rem 0; }
.typing span {
  width: 7px; height: 7px; background: #aaa; border-radius: 50%;
  animation: bounce 1.2s infinite;
}
.typing span:nth-child(2) { animation-delay: 0.2s; }
.typing span:nth-child(3) { animation-delay: 0.4s; }
@keyframes bounce { 0%, 80%, 100% { transform: translateY(0); } 40% { transform: translateY(-6px); } }

/* Input area */
.input-wrap {
  flex-shrink: 0; background: var(--chat-bg); border-top: 1px solid var(--border);
  padding: 0.8rem 1rem;
}
.input-inner {
  max-width: var(--max-w); margin: 0 auto;
  display: flex; gap: 0.5rem; align-items: flex-end;
}
.input-field {
  flex: 1; position: relative; display: flex; align-items: flex-end;
}
.input-field textarea {
  width: 100%; resize: none; padding: 0.6rem 2.2rem 0.6rem 0.9rem;
  border: 1px solid var(--border); border-radius: 10px;
  font-size: 0.9rem; font-family: inherit;
  line-height: 1.4; max-height: 150px; min-height: 42px;
  transition: border-color 0.15s;
}
.input-field textarea:focus { outline: none; border-color: var(--accent); }
.input-inner #sendBtn {
  width: 42px; height: 42px; border: none; border-radius: 10px;
  background: var(--accent); color: #fff; cursor: pointer;
  display: flex; align-items: center; justify-content: center;
  font-size: 1.1rem; transition: background 0.15s; flex-shrink: 0;
}
.input-inner #sendBtn:hover { background: #1d4ed8; }
.input-inner #sendBtn:disabled { background: #ccc; cursor: not-allowed; }
.input-inner #sendBtn.stop-mode { background: #dc2626; font-size: 1rem; }
.input-inner #sendBtn.stop-mode:hover { background: #b91c1c; }
.mic-btn {
  position: absolute; right: 0.4rem; bottom: 0.45rem;
  width: 28px; height: 28px; border: none; border-radius: 50%;
  background: transparent; color: #9ca3af; cursor: pointer;
  display: flex; align-items: center; justify-content: center;
  font-size: 0.95rem; transition: color 0.15s; padding: 0;
}
.mic-btn:hover { color: var(--accent); background: transparent; }
.mic-btn .mic-icon { display: inline; }
.mic-btn .mic-waves { display: none; }
.mic-btn.mic-active .mic-icon { display: none; }
.mic-btn.mic-active .mic-waves {
  display: flex; align-items: flex-end; gap: 2px; height: 18px;
}
.mic-btn.mic-active { background: transparent; }
.mic-wave-bar {
  width: 3px; border-radius: 2px; background: #ef4444;
  animation: waveBar 0.8s ease-in-out infinite alternate;
}
.mic-wave-bar:nth-child(1) { height: 6px; animation-delay: 0s; }
.mic-wave-bar:nth-child(2) { height: 12px; animation-delay: 0.15s; }
.mic-wave-bar:nth-child(3) { height: 18px; animation-delay: 0.3s; }
.mic-wave-bar:nth-child(4) { height: 12px; animation-delay: 0.45s; }
.mic-wave-bar:nth-child(5) { height: 6px; animation-delay: 0.6s; }
@keyframes waveBar {
  0% { transform: scaleY(0.3); }
  100% { transform: scaleY(1); }
}
.input-hint {
  max-width: var(--max-w); margin: 0.3rem auto 0; font-size: 0.65rem;
  color: #aaa; text-align: center;
}

/* Welcome */
.welcome {
  text-align: center; padding: 3rem 1rem; color: var(--text-secondary);
}
.welcome h2 { font-size: 1.2rem; color: var(--text); margin-bottom: 0.5rem; }
.welcome p { font-size: 0.85rem; max-width: 400px; margin: 0 auto; line-height: 1.5; }

/* Upload status toast */
.toast {
  position: fixed; bottom: 5rem; left: 50%; transform: translateX(-50%);
  background: #1f2937; color: #fff; padding: 0.5rem 1rem; border-radius: 8px;
  font-size: 0.8rem; z-index: 100; display: none; box-shadow: 0 4px 12px rgba(0,0,0,0.15);
}
.toast.show { display: flex; align-items: center; gap: 0.5rem; }
.toast .spinner { width: 14px; height: 14px; border: 2px solid #555; border-top-color: #fff;
  border-radius: 50%; animation: spin 0.7s linear infinite; }
@keyframes spin { to { transform: rotate(360deg); } }

/* Lightbox */
.lightbox {
  display: none; position: fixed; inset: 0; background: rgba(0,0,0,0.85);
  z-index: 200; align-items: center; justify-content: center; cursor: pointer;
}
.lightbox.open { display: flex; }
.lightbox img { max-width: 90vw; max-height: 90vh; border-radius: 8px; }

/* Confirm dialog */
.confirm-overlay {
  display: none; position: fixed; inset: 0; background: rgba(0,0,0,0.4);
  z-index: 300; align-items: center; justify-content: center;
}
.confirm-overlay.open { display: flex; }
.confirm-dialog {
  background: #fff; border-radius: 12px; padding: 1.4rem 1.6rem;
  box-shadow: 0 8px 30px rgba(0,0,0,0.18); max-width: 360px; width: 90%;
  text-align: center;
}
.confirm-dialog p { font-size: 0.88rem; line-height: 1.5; margin-bottom: 1rem; color: var(--text); }
.confirm-actions { display: flex; gap: 0.5rem; justify-content: center; }
.confirm-yes, .confirm-no {
  padding: 0.4rem 1.2rem; border: none; border-radius: 8px;
  font-size: 0.82rem; cursor: pointer; font-weight: 500; transition: all 0.15s;
}
.confirm-yes { background: #ef4444; color: #fff; }
.confirm-yes:hover { background: #dc2626; }
.confirm-no { background: #f3f4f6; color: var(--text-secondary); }
.confirm-no:hover { background: #e5e7eb; }

/* Login overlay */
.login-overlay {
  position: fixed; inset: 0; background: rgba(0,0,0,0.5); z-index: 500;
  display: flex; align-items: center; justify-content: center;
  backdrop-filter: blur(4px);
}
.login-dialog {
  background: #fff; border-radius: 12px; padding: 2rem 2.2rem;
  box-shadow: 0 12px 48px rgba(0,0,0,0.2); width: 320px;
}
.login-dialog h2 { font-size: 1.25rem; margin-bottom: 0.2rem; color: var(--text); }
.login-hint { font-size: 0.8rem; color: var(--text-secondary); margin-bottom: 1.4rem; }
.login-dialog input {
  display: block; width: 100%; padding: 0.6rem 0.8rem; margin-bottom: 0.6rem;
  border: 1px solid var(--border); border-radius: 8px; font-size: 0.9rem;
}
.login-dialog input:focus { outline: none; border-color: var(--accent); }
.login-dialog button {
  width: 100%; padding: 0.65rem; margin-top: 0.4rem;
  background: var(--accent); color: #fff; border: none; border-radius: 8px;
  font-size: 0.9rem; font-weight: 600; cursor: pointer; transition: background 0.15s;
}
.login-dialog button:hover { background: #1d4ed8; }
.login-error { font-size: 0.78rem; color: #dc2626; margin-top: 0.5rem; min-height: 1.2em; }
.login-overlay.hidden { display: none; }
.user-span { font-size: 0.72rem; color: var(--text-secondary); padding: 0 0.4rem; }

/* Meta timing breakdown */
.msg-meta { display: flex; align-items: center; gap: 0.5rem; flex-wrap: wrap; }
.meta-total { font-weight: 600; }
.meta-steps {
  font-size: 0.68rem; color: #9ca3af; font-weight: 400;
}

/* Pipeline status indicator */
.pipeline-status {
  font-size: 0.82rem; color: #6b7280; padding: 0.4rem 0;
  display: flex; align-items: center; gap: 0.5rem;
  animation: statusPulse 1.5s ease-in-out infinite;
}
.pipeline-status::before {
  content: ''; display: inline-block; width: 8px; height: 8px;
  border-radius: 50%; background: var(--accent);
  animation: statusDot 1.5s ease-in-out infinite;
}
@keyframes statusPulse {
  0%, 100% { opacity: 0.6; }
  50% { opacity: 1; }
}
@keyframes statusDot {
  0%, 100% { transform: scale(0.8); opacity: 0.5; }
  50% { transform: scale(1.2); opacity: 1; }
}

/* Feedback buttons */
.msg-feedback {
  display: inline-flex; gap: 0.2rem; margin-left: 0.4rem;
}
.msg-feedback.voted .feedback-btn:not(.selected) {
  opacity: 0.2; pointer-events: none;
}
.feedback-btn {
  background: none; border: 1px solid transparent; border-radius: 6px;
  cursor: pointer; font-size: 1rem; padding: 0.1rem 0.35rem;
  transition: all 0.15s; opacity: 0.7; line-height: 1;
}
.feedback-btn:hover { opacity: 1; border-color: var(--border); background: var(--bg-secondary); }
.feedback-btn.selected { opacity: 1; }
.feedback-btn.up.selected { border-color: #059669; background: #ecfdf5; }
.feedback-btn.down.selected { border-color: #dc2626; background: #fef2f2; }
