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

:root {
  --bg: #0d0d0d;
  --surface: #1a1a1a;
  --surface-hover: #222;
  --border: #2a2a2a;
  --border-light: #333;
  --text: #e8e8e8;
  --text-muted: #888;
  --accent: #6b7cff;
  --accent-hover: #8593ff;
  --danger: #ff4444;
  --user-bubble: #1e3a5f;
  --assistant-bubble: #1a1a1a;
  --radius: 12px;
  --radius-sm: 8px;
  --sidebar-width: 280px;
  --header-height: 56px;
  --input-height: 64px;
}

html, body {
  height: 100dvh;
  overflow: hidden;
  background: var(--bg);
  color: var(--text);
  font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, sans-serif;
  font-size: 15px;
  line-height: 1.5;
  -webkit-font-smoothing: antialiased;
  -webkit-tap-highlight-color: transparent;
  user-select: none;
}

/* ── Layout ─────────────────────────────────────────────── */
body {
  display: flex;
  position: relative;
}

/* ── Sidebar ─────────────────────────────────────────────── */
#sidebar {
  width: var(--sidebar-width);
  min-width: var(--sidebar-width);
  height: 100dvh;
  background: var(--surface);
  border-right: 1px solid var(--border);
  display: flex;
  flex-direction: column;
  z-index: 100;
  transform: translateX(0);
  transition: transform 0.25s ease;
}

#sidebar-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 12px 16px;
  border-bottom: 1px solid var(--border);
  min-height: var(--header-height);
}

#sidebar-title {
  font-weight: 700;
  font-size: 18px;
  letter-spacing: -0.3px;
  color: var(--accent);
}

#btn-new-chat {
  width: 32px;
  height: 32px;
  border-radius: 8px;
  background: var(--accent);
  color: #fff;
  border: none;
  font-size: 20px;
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: center;
  transition: background 0.15s;
}

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

#chat-list {
  flex: 1;
  overflow-y: auto;
  padding: 8px;
  -webkit-overflow-scrolling: touch;
}

.chat-item {
  padding: 10px 12px;
  border-radius: var(--radius-sm);
  cursor: pointer;
  margin-bottom: 2px;
  transition: background 0.1s;
  display: flex;
  justify-content: space-between;
  align-items: center;
  gap: 8px;
  min-height: 40px;
}

.chat-item:hover { background: var(--surface-hover); }
.chat-item.active { background: rgba(107, 124, 255, 0.12); }

.chat-item-title {
  flex: 1;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
  font-size: 14px;
  color: var(--text);
}

.chat-item-time {
  font-size: 11px;
  color: var(--text-muted);
  flex-shrink: 0;
}

.chat-item-delete {
  opacity: 0;
  background: none;
  border: none;
  color: var(--text-muted);
  cursor: pointer;
  font-size: 16px;
  padding: 0 4px;
  transition: opacity 0.15s, color 0.15s;
  flex-shrink: 0;
}

.chat-item:hover .chat-item-delete { opacity: 1; }
.chat-item-delete:hover { color: var(--danger); }

#sidebar-footer {
  padding: 12px 16px;
  border-top: 1px solid var(--border);
}

.sidebar-btn {
  display: flex;
  align-items: center;
  gap: 10px;
  width: 100%;
  padding: 10px 12px;
  background: none;
  border: none;
  color: var(--text-muted);
  font-size: 14px;
  cursor: pointer;
  border-radius: var(--radius-sm);
  transition: background 0.1s, color 0.1s;
}

.sidebar-btn:hover {
  background: var(--surface-hover);
  color: var(--text);
}

#sidebar-overlay {
  display: none;
  position: fixed;
  inset: 0;
  background: rgba(0,0,0,0.5);
  z-index: 99;
}

/* ── Main ────────────────────────────────────────────────── */
#main {
  flex: 1;
  display: flex;
  flex-direction: column;
  height: 100dvh;
  overflow: hidden;
  min-width: 0;
}

/* ── Header ──────────────────────────────────────────────── */
#chat-header {
  display: flex;
  align-items: center;
  gap: 12px;
  padding: 0 16px;
  min-height: var(--header-height);
  border-bottom: 1px solid var(--border);
  background: var(--bg);
  flex-shrink: 0;
}

#btn-hamburger {
  display: none;
  background: none;
  border: none;
  color: var(--text-muted);
  cursor: pointer;
  padding: 4px;
  border-radius: 6px;
}

#btn-hamburger:hover { color: var(--text); }

.chat-title {
  font-size: 15px;
  font-weight: 600;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
  color: var(--text);
}

/* ── Messages ────────────────────────────────────────────── */
#chat-messages {
  flex: 1;
  overflow-y: auto;
  padding: 16px;
  -webkit-overflow-scrolling: touch;
  scroll-behavior: smooth;
}

#empty-state {
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  height: 100%;
  min-height: 300px;
  color: var(--text-muted);
}

.empty-icon { margin-bottom: 16px; opacity: 0.3; }
.empty-title { font-size: 24px; font-weight: 700; margin-bottom: 4px; color: var(--text); }
.empty-subtitle { font-size: 14px; }

.message {
  max-width: 85%;
  margin-bottom: 16px;
  animation: fadeIn 0.2s ease;
}

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

.message.user { margin-left: auto; }

.message-role {
  font-size: 11px;
  font-weight: 600;
  margin-bottom: 4px;
  color: var(--text-muted);
  text-transform: uppercase;
  letter-spacing: 0.5px;
  padding: 0 4px;
}

.message.user .message-role { text-align: right; }

.message-bubble {
  padding: 10px 14px;
  border-radius: var(--radius);
  line-height: 1.6;
  word-wrap: break-word;
  white-space: pre-wrap;
}

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

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

.message-bubble p { margin-bottom: 8px; }
.message-bubble p:last-child { margin-bottom: 0; }
.message-bubble code {
  background: rgba(255,255,255,0.1);
  padding: 2px 6px;
  border-radius: 4px;
  font-size: 13px;
}
.message-bubble pre {
  background: rgba(0,0,0,0.3);
  padding: 12px;
  border-radius: var(--radius-sm);
  overflow-x: auto;
  margin: 8px 0;
}
.message-bubble pre code { background: none; padding: 0; }

.typing-indicator {
  display: inline-flex;
  gap: 4px;
  padding: 6px 14px;
}

.typing-indicator span {
  width: 6px;
  height: 6px;
  border-radius: 50%;
  background: var(--text-muted);
  animation: bounce 1.4s 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 bar ───────────────────────────────────────────── */
#input-bar {
  padding: 8px 16px;
  border-top: 1px solid var(--border);
  background: var(--bg);
  flex-shrink: 0;
  min-height: var(--input-height);
  display: flex;
  align-items: flex-end;
}

.input-row {
  display: flex;
  align-items: flex-end;
  gap: 8px;
  width: 100%;
  background: var(--surface);
  border: 1px solid var(--border-light);
  border-radius: var(--radius);
  padding: 4px 8px;
}

#chat-input {
  flex: 1;
  background: none;
  border: none;
  color: var(--text);
  font-size: 15px;
  font-family: inherit;
  line-height: 1.5;
  resize: none;
  padding: 6px 0;
  min-height: 24px;
  max-height: 120px;
  outline: none;
}

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

.input-btn {
  background: none;
  border: none;
  color: var(--text-muted);
  cursor: pointer;
  padding: 6px;
  border-radius: 6px;
  display: flex;
  align-items: center;
  justify-content: center;
  transition: color 0.1s;
  flex-shrink: 0;
}

.input-btn:hover { color: var(--text); }
.input-btn:disabled { opacity: 0.3; cursor: default; }
#btn-send:not(:disabled) { color: var(--accent); }

#btn-mic.recording {
  color: var(--danger);
  animation: pulse 1.5s infinite;
}

@keyframes pulse {
  0%, 100% { opacity: 1; }
  50% { opacity: 0.5; }
}

/* ── Settings overlay ────────────────────────────────────── */
#settings-overlay {
  position: fixed;
  inset: 0;
  background: rgba(0,0,0,0.7);
  z-index: 200;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 20px;
}

#settings-overlay.hidden { display: none; }

#settings-panel {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  width: 100%;
  max-width: 440px;
  max-height: 90dvh;
  overflow-y: auto;
}

#settings-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 16px 20px;
  border-bottom: 1px solid var(--border);
}

#settings-header h2 {
  font-size: 18px;
  font-weight: 600;
}

.close-btn {
  background: none;
  border: none;
  color: var(--text-muted);
  font-size: 24px;
  cursor: pointer;
  padding: 0 4px;
}

.close-btn:hover { color: var(--text); }

#settings-body {
  padding: 20px;
  display: flex;
  flex-direction: column;
  gap: 16px;
}

.settings-group {
  display: flex;
  flex-direction: column;
  gap: 6px;
}

.settings-group label {
  font-size: 13px;
  font-weight: 600;
  color: var(--text-muted);
  text-transform: uppercase;
  letter-spacing: 0.5px;
}

.settings-group select,
.settings-group input {
  background: var(--bg);
  border: 1px solid var(--border-light);
  color: var(--text);
  padding: 10px 12px;
  border-radius: var(--radius-sm);
  font-size: 15px;
  font-family: inherit;
  outline: none;
  transition: border-color 0.15s;
}

.settings-group select:focus,
.settings-group input:focus,
.settings-group textarea:focus {
  border-color: var(--accent);
}

.settings-group textarea {
  background: var(--bg);
  border: 1px solid var(--border-light);
  color: var(--text);
  padding: 10px 12px;
  border-radius: var(--radius-sm);
  font-size: 14px;
  font-family: monospace;
  resize: vertical;
  outline: none;
  transition: border-color 0.15s;
}

.key-wrapper {
  display: flex;
  gap: 8px;
}

.key-wrapper input { flex: 1; }

.toggle-btn {
  background: var(--bg);
  border: 1px solid var(--border-light);
  color: var(--text-muted);
  padding: 8px;
  border-radius: var(--radius-sm);
  cursor: pointer;
  display: flex;
  align-items: center;
}

.toggle-btn:hover { color: var(--text); }

.primary-btn {
  background: var(--accent);
  color: #fff;
  border: none;
  padding: 12px;
  border-radius: var(--radius-sm);
  font-size: 15px;
  font-weight: 600;
  cursor: pointer;
  transition: background 0.15s;
}

.primary-btn:hover { background: var(--accent-hover); }

.settings-status {
  font-size: 13px;
  text-align: center;
}

.settings-status.success { color: #4ade80; }
.settings-status.error { color: var(--danger); }

/* ── Update toast ────────────────────────────────────────── */
#update-toast {
  position: fixed;
  bottom: 20px;
  left: 50%;
  transform: translateX(-50%);
  background: var(--accent);
  color: #fff;
  padding: 10px 20px;
  border-radius: 20px;
  font-size: 14px;
  z-index: 300;
  display: flex;
  align-items: center;
  gap: 10px;
  box-shadow: 0 4px 12px rgba(0,0,0,0.3);
}

#update-toast.hidden { display: none; }

#btn-update {
  background: rgba(255,255,255,0.2);
  border: none;
  color: #fff;
  padding: 4px 12px;
  border-radius: 12px;
  font-size: 13px;
  font-weight: 600;
  cursor: pointer;
}

#btn-update:hover { background: rgba(255,255,255,0.3); }

/* ── Mobile ──────────────────────────────────────────────── */
@media (max-width: 640px) {
  #sidebar {
    position: fixed;
    left: 0;
    top: 0;
    transform: translateX(-100%);
  }

  #sidebar.open {
    transform: translateX(0);
    box-shadow: 4px 0 24px rgba(0,0,0,0.4);
  }

  #sidebar.open ~ #sidebar-overlay,
  #sidebar-overlay.visible {
    display: block;
  }

  #btn-hamburger { display: flex; }

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