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

:root {
  --bg:          #0f1117;
  --surface:     #1a1d27;
  --surface2:    #222636;
  --border:      #2e3347;
  --accent:      #4f8ef7;
  --accent-dim:  #3a6fd4;
  --text:        #e8eaf0;
  --text-muted:  #7c839e;
  --user-bubble: #1e3a6e;
  --ai-bubble:   #1a1d27;
  --danger:      #e05252;
  --success:     #4caf88;
  --radius:      12px;
  --radius-sm:   8px;
  --sidebar-w:   260px;
  --font: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, sans-serif;
}

html, body { height: 100%; }
body {
  font-family: var(--font);
  background: var(--bg);
  color: var(--text);
  font-size: 15px;
  line-height: 1.6;
}

/* ── Modal ── */
.modal-backdrop {
  position: fixed; inset: 0; z-index: 1000;
  background: rgba(0,0,0,.7);
  backdrop-filter: blur(4px);
  display: flex; align-items: center; justify-content: center;
  padding: 16px;
}
.modal-backdrop.hidden { display: none; }

.modal {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 32px 28px;
  width: 100%; max-width: 440px;
  text-align: center;
  box-shadow: 0 24px 60px rgba(0,0,0,.5);
}
.modal-icon { font-size: 2.2rem; margin-bottom: 12px; }
.modal h2 { font-size: 1.2rem; margin-bottom: 8px; }
.modal p  { color: var(--text-muted); font-size: 0.88rem; margin-bottom: 20px; line-height: 1.5; }

.input-row { display: flex; gap: 8px; }
.input-row input {
  flex: 1; padding: 10px 14px;
  background: var(--surface2); border: 1px solid var(--border);
  border-radius: var(--radius-sm); color: var(--text);
  font-size: 0.9rem; font-family: monospace;
  outline: none; transition: border-color .15s;
}
.input-row input:focus { border-color: var(--accent); }

.error-msg { color: var(--danger); font-size: 0.83rem; margin-top: 8px; }
.error-msg.hidden { display: none; }

.get-key-link {
  display: inline-block; margin-top: 16px;
  color: var(--accent); font-size: 0.85rem; text-decoration: none;
}
.get-key-link:hover { text-decoration: underline; }

/* ── App layout ── */
#app {
  display: flex; height: 100vh; overflow: hidden;
}

/* ── Sidebar ── */
#sidebar {
  width: var(--sidebar-w); flex-shrink: 0;
  background: var(--surface);
  border-right: 1px solid var(--border);
  display: flex; flex-direction: column;
  transition: transform .25s ease;
}

.sidebar-header {
  padding: 18px 16px 12px;
  border-bottom: 1px solid var(--border);
}
.logo { display: flex; align-items: center; gap: 10px; font-weight: 600; font-size: 1rem; }
.logo-icon { font-size: 1.4rem; }

.btn-new-chat {
  margin: 12px 10px 4px;
  display: flex; align-items: center; gap: 8px;
  padding: 9px 14px; border-radius: var(--radius-sm);
  background: var(--accent); color: #fff;
  border: none; cursor: pointer; font-size: 0.9rem; font-weight: 500;
  transition: background .15s;
}
.btn-new-chat:hover { background: var(--accent-dim); }
.btn-new-chat span { font-size: 1.2rem; line-height: 1; }

.history-list {
  flex: 1; overflow-y: auto; padding: 6px 8px;
}
.history-item {
  padding: 9px 10px; border-radius: var(--radius-sm);
  cursor: pointer; font-size: 0.85rem; color: var(--text-muted);
  white-space: nowrap; overflow: hidden; text-overflow: ellipsis;
  transition: background .1s, color .1s;
}
.history-item:hover { background: var(--surface2); color: var(--text); }
.history-item.active { background: var(--surface2); color: var(--text); font-weight: 500; }

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

/* ── Main chat ── */
#main {
  flex: 1; display: flex; flex-direction: column; overflow: hidden;
}

#chat-header {
  display: flex; align-items: center; gap: 12px;
  padding: 12px 20px;
  border-bottom: 1px solid var(--border);
  background: var(--bg);
  min-height: 52px;
}
#chat-title {
  font-size: 0.9rem; font-weight: 500; color: var(--text-muted);
  overflow: hidden; text-overflow: ellipsis; white-space: nowrap;
}
.header-spacer { flex: 1; }

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

/* Welcome screen */
.welcome {
  display: flex; flex-direction: column; align-items: center;
  justify-content: center; padding: 48px 24px; text-align: center;
  max-width: 600px; margin: 0 auto;
}
.welcome-icon { font-size: 3rem; margin-bottom: 16px; }
.welcome h1 { font-size: 1.6rem; font-weight: 700; margin-bottom: 10px; }
.welcome p  { color: var(--text-muted); margin-bottom: 28px; }

.suggestions {
  display: flex; flex-wrap: wrap; gap: 8px; justify-content: center;
}
.suggestion-chip {
  padding: 8px 14px; border-radius: 20px;
  background: var(--surface2); border: 1px solid var(--border);
  color: var(--text-muted); font-size: 0.83rem; cursor: pointer;
  transition: background .15s, color .15s, border-color .15s;
}
.suggestion-chip:hover {
  background: var(--surface); color: var(--text);
  border-color: var(--accent);
}

/* Message rows */
.message-row {
  display: flex; padding: 6px 20px; max-width: 860px; margin: 0 auto; width: 100%;
}
.message-row.user { justify-content: flex-end; }
.message-row.assistant { justify-content: flex-start; }

.bubble {
  max-width: 78%; padding: 11px 16px;
  border-radius: var(--radius); font-size: 0.93rem; line-height: 1.65;
  word-wrap: break-word;
}
.user   .bubble { background: var(--user-bubble); border-bottom-right-radius: 4px; }
.assistant .bubble { background: var(--ai-bubble); border: 1px solid var(--border); border-bottom-left-radius: 4px; }

/* Markdown inside bubbles */
.bubble p { margin-bottom: 0.6em; }
.bubble p:last-child { margin-bottom: 0; }
.bubble strong { font-weight: 600; }
.bubble em { font-style: italic; }
.bubble code {
  background: rgba(255,255,255,.08); border-radius: 4px;
  padding: 1px 5px; font-family: "Consolas", "Menlo", monospace;
  font-size: 0.87em;
}
.bubble pre {
  background: #12141e; border: 1px solid var(--border);
  border-radius: var(--radius-sm); padding: 12px 14px;
  overflow-x: auto; margin: 8px 0;
}
.bubble pre code { background: none; padding: 0; font-size: 0.85em; }
.bubble ul, .bubble ol { padding-left: 1.4em; margin: 6px 0; }
.bubble li { margin-bottom: 4px; }
.bubble h1,.bubble h2,.bubble h3 { margin: 10px 0 4px; font-size: 1em; font-weight: 600; }
.bubble blockquote {
  border-left: 3px solid var(--accent);
  padding-left: 10px; color: var(--text-muted);
  margin: 6px 0;
}
.bubble hr { border: none; border-top: 1px solid var(--border); margin: 10px 0; }

/* Typing cursor */
.typing-cursor::after {
  content: "▋";
  animation: blink .7s infinite;
  color: var(--accent);
}
@keyframes blink { 0%,100% { opacity: 1; } 50% { opacity: 0; } }

/* Error bubble */
.bubble.error { color: var(--danger); border-color: var(--danger); }

/* ── Input area ── */
#input-area {
  padding: 12px 20px 16px;
  border-top: 1px solid var(--border);
  background: var(--bg);
}

.input-wrapper {
  display: flex; align-items: flex-end; gap: 8px;
  max-width: 820px; margin: 0 auto;
  background: var(--surface2); border: 1px solid var(--border);
  border-radius: var(--radius); padding: 10px 10px 10px 16px;
  transition: border-color .15s;
}
.input-wrapper:focus-within { border-color: var(--accent); }

#user-input {
  flex: 1; background: none; border: none; outline: none;
  color: var(--text); font-family: var(--font);
  font-size: 0.93rem; line-height: 1.5; resize: none;
  max-height: 160px; overflow-y: auto;
}
#user-input::placeholder { color: var(--text-muted); }

.btn-send {
  flex-shrink: 0; width: 36px; height: 36px;
  background: var(--accent); border: none; border-radius: 8px;
  color: #fff; cursor: pointer; display: flex; align-items: center; justify-content: center;
  transition: background .15s, opacity .15s;
}
.btn-send:disabled { opacity: .4; cursor: not-allowed; }
.btn-send:not(:disabled):hover { background: var(--accent-dim); }
.btn-send svg { width: 16px; height: 16px; }

.disclaimer {
  text-align: center; font-size: 0.75rem; color: var(--text-muted);
  margin-top: 8px; max-width: 820px; margin-inline: auto;
}

/* ── Buttons ── */
.btn-primary {
  padding: 10px 18px; background: var(--accent); color: #fff;
  border: none; border-radius: var(--radius-sm); cursor: pointer;
  font-size: 0.9rem; font-weight: 500; white-space: nowrap;
  transition: background .15s;
}
.btn-primary:hover { background: var(--accent-dim); }

.btn-ghost {
  flex: 1; padding: 7px 8px; background: none;
  border: 1px solid var(--border); border-radius: var(--radius-sm);
  color: var(--text-muted); cursor: pointer; font-size: 0.78rem;
  transition: background .1s, color .1s;
}
.btn-ghost:hover { background: var(--surface2); color: var(--text); }

.btn-icon {
  background: none; border: none; color: var(--text-muted);
  cursor: pointer; font-size: 1.1rem; padding: 4px 6px;
  border-radius: 6px; transition: color .1s;
}
.btn-icon:hover { color: var(--text); }

/* ── Sidebar hidden (mobile) ── */
#sidebar.hidden { transform: translateX(-100%); position: absolute; z-index: 100; height: 100%; }

@media (max-width: 640px) {
  #sidebar { position: absolute; z-index: 100; height: 100%; transform: translateX(-100%); }
  #sidebar.open { transform: translateX(0); }
  .message-row { padding: 6px 12px; }
  .bubble { max-width: 92%; }
}
