/* ============================================================
   Chat — minimal AI chat interface
   A clean, dark, ChatGPT-inspired theme
   ============================================================ */

/* ---------- Design tokens (colors reused everywhere) ---------- */
:root {
  --bg: #0f0f10;            /* page background */
  --bg-elevated: #171719;   /* sidebar / cards */
  --bg-hover: #1f1f22;      /* hover states */
  --border: #2a2a2e;        /* subtle borders */
  --text: #ececf1;          /* main text */
  --text-muted: #9a9aa3;    /* secondary text */
  --accent: #4a9eff;        /* soft blue accent (links, focus, cursor) */
  --accent-hover: #6fb0ff;
  --user-bubble: #2a2a2e;   /* user message bubble background */
  --danger: #ef4444;
  --radius: 12px;
  --font: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Helvetica, Arial, sans-serif;
  --mono: "SF Mono", ui-monospace, Menlo, Consolas, monospace;
}

/* ---------- Reset & base ---------- */
* { box-sizing: border-box; margin: 0; padding: 0; }

html, body { height: 100%; }

body {
  font-family: var(--font);
  background: var(--bg);
  color: var(--text);
  display: flex;
  overflow: hidden;
  -webkit-font-smoothing: antialiased;
}

/* ---------- Sidebar ---------- */
.sidebar {
  width: 260px;
  min-width: 260px;
  background: var(--bg-elevated);
  border-right: 1px solid var(--border);
  display: flex;
  flex-direction: column;
  transition: margin-left 0.25s ease;
}

/* On mobile the sidebar becomes an off-canvas drawer (hidden until .open) */
.backdrop { display: none; }

.new-chat-btn {
  display: flex;
  align-items: center;
  gap: 8px;
  margin: 12px;
  padding: 10px 12px;
  background: transparent;
  border: 1px solid var(--border);
  border-radius: var(--radius);
  color: var(--text);
  font-size: 14px;
  cursor: pointer;
  transition: background 0.15s;
}
.new-chat-btn:hover { background: var(--bg-hover); }
.new-chat-btn svg { width: 16px; height: 16px; }

.conversation-list {
  flex: 1;
  overflow-y: auto;
  padding: 0 8px;
}

.conversation-item {
  display: flex;
  align-items: center;
  gap: 6px;
  padding: 9px 10px;
  border-radius: 8px;
  cursor: pointer;
  color: var(--text-muted);
  font-size: 14px;
  transition: background 0.15s;
}
.conversation-item:hover { background: var(--bg-hover); color: var(--text); }
.conversation-item.active { background: var(--bg-hover); color: var(--text); }

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

.conversation-item .delete-btn {
  background: none;
  border: none;
  color: var(--text-muted);
  cursor: pointer;
  padding: 2px;
  border-radius: 4px;
  opacity: 0;
  transition: opacity 0.15s;
}
.conversation-item:hover .delete-btn { opacity: 1; }
.conversation-item .delete-btn:hover { color: var(--danger); }
.conversation-item .delete-btn svg { width: 14px; height: 14px; }

.sidebar-footer {
  border-top: 1px solid var(--border);
  padding: 10px;
  display: flex;
  flex-direction: column;
  gap: 8px;
}

.settings-btn {
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 9px 10px;
  background: transparent;
  border: none;
  border-radius: 8px;
  color: var(--text);
  font-size: 14px;
  cursor: pointer;
  transition: background 0.15s;
}
.settings-btn:hover { background: var(--bg-hover); }
.settings-btn svg { width: 16px; height: 16px; }


/* ---------- Main area ---------- */
.main {
  flex: 1;
  display: flex;
  flex-direction: column;
  min-width: 0;
}

.topbar {
  display: flex;
  align-items: center;
  gap: 12px;
  padding: 12px 16px;
  border-bottom: 1px solid var(--border);
}

.model-label {
  display: flex;
  align-items: center;
  gap: 6px;
  min-width: 0;
  font-size: 13px;
  color: var(--text-muted);
  font-family: var(--mono);
}
.model-label #modelName {
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}
.model-dot {
  flex-shrink: 0;
  width: 8px;
  height: 8px;
  border-radius: 50%;
  background: var(--text-muted);
}
.model-dot.ok { background: var(--accent); }
.model-dot.missing { background: var(--danger); }

.icon-btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  background: transparent;
  border: none;
  color: var(--text-muted);
  cursor: pointer;
  padding: 6px;
  border-radius: 8px;
  transition: background 0.15s, color 0.15s;
}
.icon-btn:hover { background: var(--bg-hover); color: var(--text); }
.icon-btn svg { width: 18px; height: 18px; }

.sidebar-toggle { display: none; } /* shown on small screens */
.mobile-new-btn { margin-left: auto; display: none; } /* shown on small screens */

/* ---------- Messages ---------- */
.messages {
  flex: 1;
  overflow-y: auto;
  padding: 24px 0 8px;
  scroll-behavior: smooth;
}

/* Center the column of messages at a comfortable width */
.message-column {
  max-width: 760px;
  margin: 0 auto;
  padding: 0 20px;
}

/* Welcome / landing screen */
.welcome {
  height: 100%;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  text-align: center;
  padding: 0 24px;
  color: var(--text-muted);
}
.welcome .logo { width: 44px; height: 44px; margin-bottom: 16px; color: var(--accent); }
.welcome h1 { font-size: 26px; font-weight: 600; color: var(--text); margin-bottom: 8px; }
.welcome p { max-width: 420px; font-size: 15px; line-height: 1.5; }

/* A single message row */
.message {
  display: flex;
  padding: 14px 0;
}

/* ---- User messages: a bubble on the right, like ChatGPT ---- */
.message.user {
  justify-content: flex-end;
}
.message.user .bubble {
  max-width: 80%;
  background: var(--user-bubble);
  border: 1px solid var(--border);
  border-radius: 16px 16px 4px 16px;
  padding: 10px 14px;
  font-size: 15px;
  line-height: 1.55;
  white-space: pre-wrap;
  word-wrap: break-word;
}

/* ---- Assistant messages: full-width text, no bubble ---- */
.message.assistant {
  justify-content: flex-start;
}
.message-body { flex: 1; min-width: 0; }
.message-body .content {
  font-size: 15px;
  line-height: 1.65;
  white-space: pre-wrap;      /* preserve line breaks */
  word-wrap: break-word;
}

/* Thinking indicator — shimmering label while the AI prepares */
.thinking {
  display: inline-block;
  color: var(--text-muted);
  font-size: 15px;
}
.thinking-text {
  background: linear-gradient(90deg, var(--text-muted) 0%, var(--text) 50%, var(--text-muted) 100%);
  background-size: 200% 100%;
  -webkit-background-clip: text;
  background-clip: text;
  -webkit-text-fill-color: transparent;
  animation: shimmer 1.6s infinite linear;
}
@keyframes shimmer {
  0% { background-position: 200% 0; }
  100% { background-position: -200% 0; }
}

/* Markdown-ish styling inside AI messages */
.content p { margin: 0 0 10px; }
.content p:last-child { margin-bottom: 0; }
.content h1, .content h2, .content h3 { margin: 14px 0 8px; line-height: 1.3; }
.content ul, .content ol { margin: 0 0 10px 20px; }
.content li { margin-bottom: 4px; }
.content code {
  font-family: var(--mono);
  font-size: 0.9em;
  background: var(--bg-hover);
  padding: 2px 5px;
  border-radius: 5px;
}
.content pre {
  background: var(--bg-elevated);
  border: 1px solid var(--border);
  border-radius: 8px;
  padding: 12px;
  overflow-x: auto;
  margin: 10px 0;
}
.content pre code { background: none; padding: 0; }
.content blockquote {
  border-left: 3px solid var(--accent);
  padding-left: 12px;
  color: var(--text-muted);
  margin: 10px 0;
}
.content a { color: var(--accent); }

/* Action buttons (copy + speak) under assistant messages */
.action-btns {
  display: flex;
  gap: 4px;
  margin-top: 8px;
}
.action-btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  background: none;
  border: none;
  color: var(--text-muted);
  cursor: pointer;
  padding: 5px;
  border-radius: 6px;
  transition: background 0.15s, color 0.15s;
}
.action-btn:hover { background: var(--bg-hover); color: var(--text); }
.action-btn svg { width: 15px; height: 15px; }

/* ---------- Input area ---------- */
.input-area {
  padding: 12px 20px 16px;
  max-width: 760px;
  margin: 0 auto;
  width: 100%;
}

.input-row {
  display: flex;
  align-items: center;         /* vertically center the text/cursor */
  gap: 10px;
  background: rgba(255, 255, 255, 0.06);   /* liquid-glass tint */
  backdrop-filter: blur(12px);
  -webkit-backdrop-filter: blur(12px);
  border-radius: 14px;
  padding: 8px 12px;
}

.prompt-input {
  flex: 1;
  background: transparent;
  border: none;
  outline: none;
  resize: none;
  color: var(--text);
  font-family: var(--font);
  font-size: 16px;   /* >=16px prevents iOS auto-zoom on focus */
  line-height: 1.5;
  max-height: 160px;
}
.prompt-input::placeholder { color: var(--text-muted); }

/* Mic button (dictation) */
.mic-btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  width: 34px;
  height: 34px;
  border: none;
  border-radius: 8px;
  background: transparent;
  color: var(--text-muted);
  cursor: pointer;
  transition: background 0.15s, color 0.15s;
}
.mic-btn:hover { background: var(--bg-hover); color: var(--text); }
.mic-btn.recording { background: var(--danger); color: #fff; }
.mic-btn svg { width: 16px; height: 16px; }

/* Send button — white, no green */
.send-btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  width: 34px;
  height: 34px;
  border: none;
  border-radius: 8px;
  background: #fff;
  color: #0f0f10;
  cursor: pointer;
  transition: background 0.15s, opacity 0.15s;
}
.send-btn:hover { background: #e0e0e0; }
.send-btn:disabled { opacity: 0.4; cursor: not-allowed; }
.send-btn svg { width: 16px; height: 16px; }

/* ---------- Modal ---------- */
.modal-overlay {
  position: fixed;
  inset: 0;
  background: rgba(0, 0, 0, 0.6);
  display: flex;
  align-items: center;
  justify-content: center;
  z-index: 100;
  padding: 20px;
}
.modal-overlay.hidden { display: none; }

.modal {
  background: var(--bg-elevated);
  border: 1px solid var(--border);
  border-radius: 16px;
  padding: 24px;
  width: 100%;
  max-width: 460px;
  max-height: 90vh;
  overflow-y: auto;
}
.modal h2 { font-size: 18px; margin-bottom: 18px; }

.field {
  display: block;
  margin-bottom: 16px;
}
.field > span {
  display: block;
  font-size: 13px;
  font-weight: 500;
  margin-bottom: 6px;
}
.field > span em { font-style: normal; color: var(--text-muted); font-weight: 400; }
.field input[type="text"],
.field input[type="password"],
.field input[type="number"],
.field select,
.field textarea {
  width: 100%;
  background: var(--bg);
  border: 1px solid var(--border);
  border-radius: 8px;
  color: var(--text);
  padding: 9px 11px;
  font-size: 16px;   /* >=16px prevents iOS auto-zoom on focus */
  font-family: var(--font);
  outline: none;
  transition: border-color 0.15s;
}
.field input:focus, .field select:focus, .field textarea:focus { border-color: var(--accent); }
.field textarea { resize: vertical; }
/* Keep the select the same height as the single-line text fields */
.field select {
  height: 40px;
}
.field small { display: block; margin-top: 5px; color: var(--text-muted); font-size: 12px; }
.field small a { color: var(--accent); }

.key-row { display: flex; gap: 8px; }
.key-row input { flex: 1; }

.hidden { display: none !important; }

.modal-actions {
  display: flex;
  gap: 10px;
  margin-top: 20px;
  justify-content: flex-end;
}

.btn {
  padding: 9px 16px;
  border-radius: 8px;
  font-size: 14px;
  font-weight: 500;
  cursor: pointer;
  border: 1px solid transparent;
  transition: background 0.15s, opacity 0.15s;
}
.btn:disabled { opacity: 0.5; cursor: not-allowed; }
.btn.primary { background: var(--accent); color: #fff; }
.btn.primary:hover { background: var(--accent-hover); }
.btn.secondary { background: var(--bg-hover); color: var(--text); border-color: var(--border); }
.btn.secondary:hover { background: var(--border); }
.btn.ghost { background: transparent; color: var(--text-muted); }
.btn.ghost:hover { color: var(--text); }

.test-result { margin-top: 12px; font-size: 13px; min-height: 18px; }
.test-result.ok { color: var(--accent); }
.test-result.err { color: var(--danger); }

/* ---------- Scrollbars ---------- */
::-webkit-scrollbar { width: 8px; height: 8px; }
::-webkit-scrollbar-thumb { background: var(--border); border-radius: 4px; }
::-webkit-scrollbar-thumb:hover { background: #3a3a40; }

/* ---------- Responsive (small screens) ---------- */
@media (max-width: 768px) {
  .sidebar { position: fixed; z-index: 50; height: 100%; margin-left: -260px; box-shadow: 0 0 30px rgba(0,0,0,0.5); }
  .sidebar.open { margin-left: 0; }
  .backdrop.show { display: block; position: fixed; inset: 0; background: rgba(0,0,0,0.45); z-index: 40; }
  .sidebar-toggle { display: inline-flex; }
  .mobile-new-btn { display: inline-flex; }
}
