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

:root {
  --bg-primary: #1a1a2e;
  --bg-secondary: #16213e;
  --bg-tertiary: #0f3460;
  --bg-message-user: #0f3460;
  --bg-message-ai: #1e2a3a;
  --bg-input: #1e2a3a;
  --bg-hover: rgba(255,255,255,0.06);
  --bg-modal: #1a1a2e;
  --border: rgba(255,255,255,0.1);
  --border-focus: #4a9eff;
  --text-primary: #e8eaf6;
  --text-secondary: #9e9eb3;
  --text-muted: #6b6b80;
  --accent: #4a9eff;
  --accent-hover: #5aadff;
  --accent-2: #7c4dff;
  --success: #4caf50;
  --danger: #f44336;
  --warning: #ff9800;
  --sidebar-width: 260px;
  --topbar-height: 56px;
  --radius-sm: 6px;
  --radius-md: 10px;
  --radius-lg: 16px;
  --shadow: 0 4px 24px rgba(0,0,0,0.4);
  --transition: 0.2s ease;
}

html, body {
  height: 100%;
  overflow: hidden;
}

body {
  font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, sans-serif;
  background: var(--bg-primary);
  color: var(--text-primary);
  font-size: 15px;
  line-height: 1.6;
}

/* ===== Layout ===== */
#app {
  display: flex;
  height: 100vh;
  width: 100vw;
}

/* ===== Sidebar ===== */
#sidebar {
  width: var(--sidebar-width);
  background: var(--bg-secondary);
  border-right: 1px solid var(--border);
  display: flex;
  flex-direction: column;
  flex-shrink: 0;
  overflow: hidden;
  transition: transform var(--transition);
}

.sidebar-header {
  padding: 16px 12px 12px;
  border-bottom: 1px solid var(--border);
}

.sidebar-logo {
  font-size: 18px;
  font-weight: 700;
  color: var(--accent);
  letter-spacing: -0.3px;
  display: flex;
  align-items: center;
  gap: 8px;
  margin-bottom: 12px;
}

.sidebar-logo svg {
  width: 24px;
  height: 24px;
}

#btn-new-chat {
  width: 100%;
  padding: 9px 14px;
  background: var(--accent);
  color: #fff;
  border: none;
  border-radius: var(--radius-sm);
  cursor: pointer;
  font-size: 14px;
  font-weight: 600;
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 6px;
  transition: background var(--transition);
}

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

/* Server status */
.server-status {
  padding: 8px 12px;
  display: flex;
  align-items: center;
  gap: 8px;
  font-size: 12px;
  color: var(--text-secondary);
  border-bottom: 1px solid var(--border);
}

.status-dot {
  width: 8px;
  height: 8px;
  border-radius: 50%;
  background: var(--text-muted);
  flex-shrink: 0;
  transition: background var(--transition);
}

.status-dot.online { background: var(--success); box-shadow: 0 0 6px var(--success); }
.status-dot.offline { background: var(--danger); }
.status-dot.checking { background: var(--warning); animation: pulse 1s infinite; }

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

/* GPU info */
.gpu-info {
  padding: 8px 12px;
  border-bottom: 1px solid var(--border);
  font-size: 11px;
  color: var(--text-muted);
  display: none;
}

.gpu-info.visible { display: block; }
.gpu-info-row { display: flex; justify-content: space-between; margin: 2px 0; }
.gpu-info-label { color: var(--text-secondary); }
.gpu-info-value { color: var(--text-primary); font-weight: 500; }

/* Chat list */
.sidebar-section-label {
  padding: 12px 12px 4px;
  font-size: 11px;
  font-weight: 600;
  color: var(--text-muted);
  text-transform: uppercase;
  letter-spacing: 0.6px;
}

#chat-list {
  flex: 1;
  overflow-y: auto;
  padding: 4px 6px;
  scrollbar-width: thin;
  scrollbar-color: var(--border) transparent;
}

#chat-list::-webkit-scrollbar { width: 4px; }
#chat-list::-webkit-scrollbar-track { background: transparent; }
#chat-list::-webkit-scrollbar-thumb { background: var(--border); border-radius: 2px; }

.chat-item {
  padding: 9px 10px;
  border-radius: var(--radius-sm);
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 6px;
  transition: background var(--transition);
  group: true;
}

.chat-item:hover { background: var(--bg-hover); }
.chat-item.active { background: rgba(74, 158, 255, 0.15); border-left: 2px solid var(--accent); padding-left: 8px; }

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

.chat-item-delete {
  display: none;
  background: none;
  border: none;
  color: var(--text-muted);
  cursor: pointer;
  padding: 2px 4px;
  border-radius: 3px;
  font-size: 14px;
  line-height: 1;
  flex-shrink: 0;
}

.chat-item:hover .chat-item-delete { display: block; }
.chat-item-delete:hover { color: var(--danger); background: rgba(244, 67, 54, 0.1); }

.no-chats {
  padding: 20px 12px;
  text-align: center;
  font-size: 13px;
  color: var(--text-muted);
}

/* Sidebar footer */
.sidebar-footer {
  padding: 10px 12px;
  border-top: 1px solid var(--border);
}

#btn-settings {
  width: 100%;
  padding: 8px 12px;
  background: none;
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  color: var(--text-secondary);
  cursor: pointer;
  font-size: 13px;
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 6px;
  transition: all var(--transition);
}

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

/* ===== Main Area ===== */
#main {
  flex: 1;
  display: flex;
  flex-direction: column;
  overflow: hidden;
  min-width: 0;
}

/* ===== Top Bar ===== */
#topbar {
  height: var(--topbar-height);
  background: var(--bg-secondary);
  border-bottom: 1px solid var(--border);
  display: flex;
  align-items: center;
  padding: 0 16px;
  gap: 12px;
  flex-shrink: 0;
}

#btn-sidebar-toggle {
  display: none;
  background: none;
  border: none;
  color: var(--text-secondary);
  cursor: pointer;
  padding: 6px;
  border-radius: var(--radius-sm);
}

#btn-sidebar-toggle:hover { color: var(--text-primary); background: var(--bg-hover); }

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

/* Model selector */
.model-selector-wrapper {
  position: relative;
}

#model-selector {
  appearance: none;
  background: var(--bg-input);
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  color: var(--text-primary);
  padding: 7px 32px 7px 12px;
  font-size: 13px;
  cursor: pointer;
  transition: border-color var(--transition);
  min-width: 180px;
  background-image: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' width='12' height='12' fill='%239e9eb3' viewBox='0 0 16 16'%3E%3Cpath d='M7.247 11.14L2.451 5.658C1.885 5.013 2.345 4 3.204 4h9.592a1 1 0 0 1 .753 1.659l-4.796 5.48a1 1 0 0 1-1.506 0z'/%3E%3C/svg%3E");
  background-repeat: no-repeat;
  background-position: right 10px center;
}

#model-selector:hover { border-color: var(--accent); }
#model-selector:focus { outline: none; border-color: var(--border-focus); }
#model-selector option { background: var(--bg-secondary); }

.model-loading {
  display: none;
  position: absolute;
  right: 8px;
  top: 50%;
  transform: translateY(-50%);
  width: 16px;
  height: 16px;
  border: 2px solid var(--border);
  border-top-color: var(--accent);
  border-radius: 50%;
  animation: spin 0.7s linear infinite;
}

.model-loading.active { display: block; }

@keyframes spin {
  to { transform: translateY(-50%) rotate(360deg); }
}

/* ===== Chat Area ===== */
#chat-container {
  flex: 1;
  overflow-y: auto;
  padding: 24px 16px;
  scrollbar-width: thin;
  scrollbar-color: var(--border) transparent;
}

#chat-container::-webkit-scrollbar { width: 6px; }
#chat-container::-webkit-scrollbar-track { background: transparent; }
#chat-container::-webkit-scrollbar-thumb { background: var(--border); border-radius: 3px; }

.messages-wrap {
  max-width: 780px;
  margin: 0 auto;
  display: flex;
  flex-direction: column;
  gap: 20px;
}

/* Welcome screen */
#welcome {
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  min-height: 60vh;
  text-align: center;
  color: var(--text-secondary);
}

#welcome h2 {
  font-size: 26px;
  font-weight: 700;
  color: var(--text-primary);
  margin-bottom: 8px;
}

#welcome p {
  font-size: 14px;
  max-width: 400px;
  line-height: 1.7;
}

.welcome-starters {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 10px;
  margin-top: 24px;
  width: 100%;
  max-width: 480px;
}

.starter-btn {
  padding: 12px 14px;
  background: var(--bg-message-ai);
  border: 1px solid var(--border);
  border-radius: var(--radius-md);
  color: var(--text-secondary);
  cursor: pointer;
  font-size: 13px;
  text-align: left;
  transition: all var(--transition);
  line-height: 1.4;
}

.starter-btn:hover {
  border-color: var(--accent);
  color: var(--text-primary);
  background: rgba(74, 158, 255, 0.08);
}

/* Message bubbles */
.message {
  display: flex;
  gap: 12px;
  animation: fadeSlideIn 0.2s ease;
}

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

.message.user { flex-direction: row-reverse; }

.message-avatar {
  width: 34px;
  height: 34px;
  border-radius: 50%;
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 13px;
  font-weight: 700;
  flex-shrink: 0;
  margin-top: 2px;
}

.message.user .message-avatar {
  background: var(--accent);
  color: #fff;
}

.message.assistant .message-avatar {
  background: var(--accent-2);
  color: #fff;
}

.message-body {
  max-width: 75%;
  display: flex;
  flex-direction: column;
  gap: 4px;
}

.message.user .message-body { align-items: flex-end; }

.message-bubble {
  padding: 12px 16px;
  border-radius: var(--radius-lg);
  line-height: 1.65;
  word-break: break-word;
  position: relative;
}

.message.user .message-bubble {
  background: var(--bg-message-user);
  border: 1px solid rgba(74, 158, 255, 0.3);
  border-bottom-right-radius: var(--radius-sm);
  color: var(--text-primary);
}

.message.assistant .message-bubble {
  background: var(--bg-message-ai);
  border: 1px solid var(--border);
  border-bottom-left-radius: var(--radius-sm);
  color: var(--text-primary);
}

.message-time {
  font-size: 11px;
  color: var(--text-muted);
  padding: 0 4px;
}

/* Markdown content */
.message-bubble h1, .message-bubble h2, .message-bubble h3 {
  margin: 12px 0 6px;
  font-weight: 600;
  line-height: 1.3;
}

.message-bubble h1 { font-size: 18px; }
.message-bubble h2 { font-size: 16px; }
.message-bubble h3 { font-size: 14px; color: var(--accent); }

.message-bubble p { margin: 6px 0; }
.message-bubble ul, .message-bubble ol { margin: 6px 0; padding-left: 20px; }
.message-bubble li { margin: 3px 0; }
.message-bubble blockquote {
  border-left: 3px solid var(--accent);
  margin: 8px 0;
  padding: 4px 12px;
  color: var(--text-secondary);
  background: rgba(74, 158, 255, 0.06);
  border-radius: 0 var(--radius-sm) var(--radius-sm) 0;
}

.message-bubble a { color: var(--accent); text-decoration: none; }
.message-bubble a:hover { text-decoration: underline; }

.message-bubble hr { border: none; border-top: 1px solid var(--border); margin: 12px 0; }

.message-bubble table { border-collapse: collapse; width: 100%; margin: 8px 0; font-size: 13px; }
.message-bubble th, .message-bubble td {
  border: 1px solid var(--border);
  padding: 6px 10px;
  text-align: left;
}
.message-bubble th { background: var(--bg-tertiary); font-weight: 600; }

/* Code blocks */
.message-bubble pre {
  background: #0d1117;
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  overflow-x: auto;
  margin: 8px 0;
  position: relative;
}

.message-bubble pre code {
  display: block;
  padding: 14px 16px;
  font-size: 13px;
  line-height: 1.6;
  font-family: 'JetBrains Mono', 'Cascadia Code', 'Fira Code', Consolas, monospace;
}

.message-bubble :not(pre) > code {
  background: rgba(74, 158, 255, 0.12);
  border: 1px solid rgba(74, 158, 255, 0.2);
  border-radius: 4px;
  padding: 1px 5px;
  font-size: 0.88em;
  font-family: 'JetBrains Mono', Consolas, monospace;
}

.code-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 6px 12px;
  background: #161b22;
  border-bottom: 1px solid var(--border);
  border-radius: var(--radius-sm) var(--radius-sm) 0 0;
  font-size: 12px;
  color: var(--text-muted);
}

.copy-btn {
  background: none;
  border: 1px solid var(--border);
  border-radius: 4px;
  color: var(--text-muted);
  cursor: pointer;
  padding: 2px 8px;
  font-size: 11px;
  transition: all var(--transition);
}

.copy-btn:hover { color: var(--text-primary); border-color: var(--accent); }
.copy-btn.copied { color: var(--success); border-color: var(--success); }

/* Loading indicator */
.message-loading .message-bubble {
  display: flex;
  align-items: center;
  gap: 4px;
  min-width: 60px;
}

.typing-dot {
  width: 7px;
  height: 7px;
  background: var(--text-muted);
  border-radius: 50%;
  animation: typing 1.2s infinite;
}

.typing-dot:nth-child(2) { animation-delay: 0.2s; }
.typing-dot:nth-child(3) { animation-delay: 0.4s; }

@keyframes typing {
  0%, 60%, 100% { transform: translateY(0); opacity: 0.5; }
  30% { transform: translateY(-6px); opacity: 1; }
}

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

.input-wrap {
  max-width: 780px;
  margin: 0 auto;
  display: flex;
  gap: 8px;
  align-items: flex-end;
  background: var(--bg-input);
  border: 1px solid var(--border);
  border-radius: var(--radius-lg);
  padding: 8px 10px 8px 16px;
  transition: border-color var(--transition);
}

.input-wrap:focus-within { border-color: var(--border-focus); }

#message-input {
  flex: 1;
  background: none;
  border: none;
  color: var(--text-primary);
  font-size: 15px;
  line-height: 1.5;
  resize: none;
  max-height: 200px;
  min-height: 24px;
  overflow-y: auto;
  font-family: inherit;
  scrollbar-width: thin;
}

#message-input::placeholder { color: var(--text-muted); }
#message-input:focus { outline: none; }

#btn-send {
  width: 36px;
  height: 36px;
  background: var(--accent);
  border: none;
  border-radius: var(--radius-sm);
  color: #fff;
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: center;
  flex-shrink: 0;
  transition: background var(--transition);
}

#btn-send:hover:not(:disabled) { background: var(--accent-hover); }
#btn-send:disabled { background: var(--text-muted); cursor: not-allowed; }

#btn-stop {
  width: 36px;
  height: 36px;
  background: var(--danger);
  border: none;
  border-radius: var(--radius-sm);
  color: #fff;
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: center;
  flex-shrink: 0;
  transition: background var(--transition);
}

#btn-stop:hover { background: #d32f2f; }

.input-hint {
  text-align: center;
  font-size: 11px;
  color: var(--text-muted);
  margin-top: 6px;
  max-width: 780px;
  margin-left: auto;
  margin-right: auto;
}

/* ===== Settings Modal ===== */
.modal-overlay {
  display: none;
  position: fixed;
  inset: 0;
  background: rgba(0, 0, 0, 0.6);
  backdrop-filter: blur(4px);
  z-index: 100;
  align-items: center;
  justify-content: center;
}

.modal-overlay.open { display: flex; }

.modal {
  background: var(--bg-modal);
  border: 1px solid var(--border);
  border-radius: var(--radius-lg);
  width: 480px;
  max-width: 95vw;
  max-height: 90vh;
  overflow-y: auto;
  box-shadow: var(--shadow);
  animation: modalIn 0.2s ease;
}

@keyframes modalIn {
  from { opacity: 0; transform: scale(0.95); }
  to { opacity: 1; transform: scale(1); }
}

.modal-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 18px 20px 14px;
  border-bottom: 1px solid var(--border);
}

.modal-header h3 { font-size: 16px; font-weight: 600; }

.modal-close {
  background: none;
  border: none;
  color: var(--text-muted);
  cursor: pointer;
  font-size: 20px;
  line-height: 1;
  padding: 2px 6px;
  border-radius: 4px;
}

.modal-close:hover { color: var(--text-primary); background: var(--bg-hover); }

.modal-body { padding: 20px; display: flex; flex-direction: column; gap: 16px; }

.form-group { display: flex; flex-direction: column; gap: 6px; }
.form-label { font-size: 13px; font-weight: 500; color: var(--text-secondary); }

.form-input {
  background: var(--bg-input);
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  color: var(--text-primary);
  padding: 9px 12px;
  font-size: 14px;
  font-family: inherit;
  transition: border-color var(--transition);
}

.form-input:focus { outline: none; border-color: var(--border-focus); }

.form-hint { font-size: 12px; color: var(--text-muted); }

.modal-footer {
  display: flex;
  gap: 8px;
  justify-content: flex-end;
  padding: 14px 20px 18px;
  border-top: 1px solid var(--border);
}

.btn-primary {
  padding: 8px 18px;
  background: var(--accent);
  color: #fff;
  border: none;
  border-radius: var(--radius-sm);
  cursor: pointer;
  font-size: 14px;
  font-weight: 500;
  transition: background var(--transition);
}

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

.btn-secondary {
  padding: 8px 18px;
  background: none;
  color: var(--text-secondary);
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  cursor: pointer;
  font-size: 14px;
  transition: all var(--transition);
}

.btn-secondary:hover { border-color: var(--accent); color: var(--text-primary); }

/* Toast notification */
.toast {
  position: fixed;
  bottom: 20px;
  right: 20px;
  padding: 10px 16px;
  background: var(--bg-secondary);
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  color: var(--text-primary);
  font-size: 13px;
  box-shadow: var(--shadow);
  z-index: 200;
  animation: toastIn 0.2s ease;
  max-width: 300px;
}

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

.toast.success { border-color: var(--success); }
.toast.error { border-color: var(--danger); }
.toast.warning { border-color: var(--warning); }

/* ===== Responsive ===== */
@media (max-width: 680px) {
  #sidebar {
    position: fixed;
    top: 0;
    left: 0;
    height: 100%;
    z-index: 50;
    transform: translateX(-100%);
  }

  #sidebar.open { transform: translateX(0); box-shadow: var(--shadow); }

  #btn-sidebar-toggle { display: flex; }

  .welcome-starters { grid-template-columns: 1fr; }

  .message-body { max-width: 90%; }

  #topbar { padding: 0 12px; }

  #chat-container { padding: 16px 12px; }
}

/* hljs overrides */
.hljs { background: transparent !important; }
