:root {
  --surface: #1e1e1e;
  --sidebar: #252526;
  --chat: #1e1e1e;
  --border: #3e3e3e;
  --accent: #0078d4;
  --accent-hover: #1a8ae8;
  --text-primary: #cccccc;
  --text-secondary: #969696;
  --text-muted: #6e6e6e;
  --user-bubble: #0078d4;
  --user-bubble-text: #ffffff;
  --assistant-bubble: #252526;
  --assistant-bubble-text: #cccccc;
}

* {
  margin: 0;
  padding: 0;
  box-sizing: border-box;
}

body {
  font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, sans-serif;
  background: var(--surface);
  color: var(--text-primary);
  height: 100vh;
  font-size: 13px;
}

.app {
  width: 100%;
  height: 100vh;
  display: flex;
  flex-direction: column;
  background: var(--chat);
}

/* ─── Header / Tab Bar ─────────────────────────────────── */

.tab-bar {
  display: flex;
  align-items: center;
  background: var(--sidebar);
  border-bottom: 1px solid var(--border);
  min-height: 33px;
}

.tabs {
  display: flex;
  align-items: center;
  overflow-x: auto;
  flex: 1;
  min-width: 0;
}

.tab {
  display: flex;
  align-items: center;
  gap: 4px;
  padding: 6px 12px;
  font-size: 11px;
  cursor: pointer;
  border-right: 1px solid var(--border);
  white-space: nowrap;
  color: var(--text-secondary);
  transition: background 0.15s;
}

.tab:hover {
  background: rgba(255,255,255,0.05);
}

.tab.active {
  background: var(--chat);
  color: var(--text-primary);
}

.tab .close-tab {
  margin-left: 4px;
  padding: 0 2px;
  border-radius: 3px;
  color: var(--text-muted);
  cursor: pointer;
}

.tab .close-tab:hover {
  color: var(--text-primary);
  background: var(--border);
}

.new-tab-btn {
  padding: 6px 8px;
  color: var(--text-muted);
  cursor: pointer;
  font-size: 14px;
  border: none;
  background: none;
}

.new-tab-btn:hover {
  color: var(--text-primary);
}

.header-controls {
  display: flex;
  align-items: center;
  gap: 6px;
  padding: 0 10px;
  border-left: 1px solid var(--border);
}

#model-select {
  background: var(--sidebar);
  color: var(--text-primary);
  border: 1px solid var(--border);
  border-radius: 4px;
  padding: 3px 8px;
  font-size: 11px;
}

#settings-btn {
  background: none;
  border: 1px solid var(--border);
  color: var(--text-muted);
  border-radius: 4px;
  padding: 3px 6px;
  font-size: 12px;
  cursor: pointer;
}

#settings-btn:hover {
  color: var(--accent);
  border-color: var(--accent);
}

/* ─── Chat Messages ────────────────────────────────────── */

.chat-messages {
  flex: 1;
  overflow-y: auto;
  padding: 16px;
  display: flex;
  flex-direction: column;
  gap: 12px;
}

.message {
  max-width: 85%;
  padding: 8px 12px;
  border-radius: 8px;
  line-height: 1.6;
  font-size: 13px;
}

.message.user {
  align-self: flex-end;
  background: var(--user-bubble);
  color: var(--user-bubble-text);
  border-bottom-right-radius: 3px;
  white-space: pre-wrap;
  word-wrap: break-word;
}

.message.assistant {
  align-self: flex-start;
  background: var(--assistant-bubble);
  color: var(--assistant-bubble-text);
  border-bottom-left-radius: 3px;
}

.message.assistant .msg-header {
  display: flex;
  align-items: center;
  gap: 8px;
  margin-bottom: 4px;
}

.message.assistant .msg-header .name {
  font-size: 10px;
  font-weight: 600;
  color: var(--accent);
}

.message.assistant .msg-header .elapsed {
  font-size: 9px;
  color: var(--text-muted);
}

.message.thinking {
  align-self: flex-start;
  background: var(--assistant-bubble);
  color: var(--text-muted);
  border-bottom-left-radius: 3px;
  font-size: 12px;
}

.message.thinking .pulse {
  animation: pulse 1.5s infinite;
}

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

/* ─── Markdown in messages ─────────────────────────────── */

.message.assistant p {
  margin-bottom: 8px;
}

.message.assistant p:last-child {
  margin-bottom: 0;
}

.message.assistant ul,
.message.assistant ol {
  padding-left: 16px;
  margin-bottom: 8px;
}

.message.assistant li {
  margin-bottom: 4px;
}

.message.assistant h1,
.message.assistant h2,
.message.assistant h3 {
  margin: 12px 0 6px;
}

.message.assistant h1 { font-size: 16px; }
.message.assistant h2 { font-size: 14px; }
.message.assistant h3 { font-size: 13px; }

.message.assistant blockquote {
  border-left: 2px solid var(--accent);
  padding-left: 10px;
  margin: 8px 0;
  color: var(--text-muted);
  font-style: italic;
}

.message.assistant code {
  background: rgba(255,255,255,0.08);
  padding: 1px 5px;
  border-radius: 3px;
  font-family: 'SF Mono', Menlo, monospace;
  font-size: 12px;
}

.message.assistant pre {
  position: relative;
  background: #1e1e1e;
  border: 1px solid var(--border);
  border-radius: 6px;
  padding: 12px;
  margin: 8px 0;
  overflow-x: auto;
  font-size: 12px;
  line-height: 1.5;
}

.message.assistant pre code {
  background: none;
  padding: 0;
  font-size: 12px;
}

.copy-btn {
  position: absolute;
  top: 6px;
  right: 6px;
  padding: 3px 8px;
  font-size: 10px;
  background: var(--border);
  color: var(--text-primary);
  border: none;
  border-radius: 4px;
  cursor: pointer;
  opacity: 0;
  transition: opacity 0.2s;
}

pre:hover .copy-btn {
  opacity: 1;
}

.copy-btn:hover {
  background: var(--accent);
  color: #fff;
}

/* ─── Input Area ───────────────────────────────────────── */

.chat-input {
  display: flex;
  gap: 8px;
  padding: 10px 16px;
  border-top: 1px solid var(--border);
  background: var(--surface);
}

#message-input {
  flex: 1;
  resize: none;
  background: var(--sidebar);
  border: 1px solid var(--border);
  border-radius: 8px;
  padding: 8px 12px;
  color: var(--text-primary);
  font-size: 13px;
  font-family: inherit;
  max-height: 120px;
  line-height: 1.5;
}

#message-input:focus {
  outline: none;
  border-color: var(--accent);
}

#send-btn {
  background: var(--accent);
  color: #fff;
  border: none;
  border-radius: 8px;
  padding: 8px 16px;
  font-size: 12px;
  cursor: pointer;
  transition: background 0.2s;
  align-self: flex-end;
}

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

#send-btn:disabled {
  opacity: 0.4;
  cursor: not-allowed;
}

#send-btn.stop {
  background: #e53935;
}

#send-btn.stop:hover {
  background: #c62828;
}

/* ─── Empty State ──────────────────────────────────────── */

.empty-state {
  display: flex;
  align-items: center;
  justify-content: center;
  flex: 1;
  text-align: center;
  color: var(--text-muted);
}

.empty-state .icon {
  font-size: 32px;
  margin-bottom: 8px;
}

.empty-state .title {
  font-size: 14px;
  font-weight: 500;
  color: var(--text-secondary);
}

.empty-state .subtitle {
  font-size: 11px;
  margin-top: 4px;
}

/* ─── Settings Panel ───────────────────────────────────── */

.settings-overlay {
  display: none;
  position: fixed;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
  background: rgba(0, 0, 0, 0.6);
  z-index: 100;
  justify-content: center;
  align-items: center;
}

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

.settings-panel {
  background: var(--sidebar);
  border: 1px solid var(--border);
  border-radius: 8px;
  padding: 24px;
  width: 90%;
  max-width: 420px;
  max-height: 80vh;
  overflow-y: auto;
}

.settings-panel h2 {
  color: var(--text-primary);
  margin-bottom: 16px;
  font-size: 14px;
  font-weight: 600;
}

.color-group {
  margin-bottom: 16px;
}

.color-group h3 {
  font-size: 10px;
  color: var(--text-muted);
  margin-bottom: 6px;
  text-transform: uppercase;
  letter-spacing: 0.5px;
}

.color-option {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 4px 0;
}

.color-option label {
  font-size: 12px;
  color: var(--text-primary);
}

.color-option input[type="color"] {
  width: 32px;
  height: 24px;
  border: none;
  border-radius: 3px;
  cursor: pointer;
  background: none;
}

.settings-actions {
  display: flex;
  gap: 6px;
  margin-top: 16px;
}

.settings-actions button {
  flex: 1;
  padding: 6px 10px;
  border-radius: 4px;
  border: none;
  cursor: pointer;
  font-size: 11px;
  transition: opacity 0.2s;
}

.settings-actions button:hover {
  opacity: 0.85;
}

#save-settings {
  background: var(--accent);
  color: #fff;
}

#reset-settings {
  background: var(--border);
  color: var(--text-primary);
}

#close-settings {
  background: none;
  border: 1px solid var(--border);
  color: var(--text-primary);
}

/* ─── Scrollbar ────────────────────────────────────────── */

::-webkit-scrollbar {
  width: 6px;
  height: 6px;
}

::-webkit-scrollbar-track {
  background: transparent;
}

::-webkit-scrollbar-thumb {
  background: #424242;
  border-radius: 3px;
}

::-webkit-scrollbar-thumb:hover {
  background: #555;
}

/* ─── Responsive ───────────────────────────────────────── */

@media (max-width: 600px) {
  .message {
    max-width: 92%;
  }
  .header-controls {
    gap: 4px;
    padding: 0 6px;
  }
}


/* ─── Connection Status Dot ────────────────────────────── */

.status-dot {
  width: 8px;
  height: 8px;
  border-radius: 50%;
  display: inline-block;
}

.status-dot.connected {
  background: #4caf50;
}

.status-dot.disconnected {
  background: #e53935;
}

/* ─── Tab Rename Input ─────────────────────────────────── */

.tab-rename-input {
  background: var(--surface);
  border: 1px solid var(--accent);
  border-radius: 3px;
  color: var(--text-primary);
  font-size: 11px;
  padding: 1px 4px;
  width: 100px;
  outline: none;
}

/* ─── Mobile Improvements ──────────────────────────────── */

@media (max-width: 600px) {
  .app {
    height: 100dvh; /* dynamic viewport height for mobile browsers */
  }

  .tab-bar {
    flex-wrap: nowrap;
  }

  .tabs {
    max-width: calc(100% - 120px);
  }

  .chat-messages {
    padding: 12px;
    gap: 10px;
  }

  .message {
    max-width: 92%;
    font-size: 14px;
  }

  .chat-input {
    padding: 8px 12px;
    gap: 6px;
  }

  #message-input {
    font-size: 16px; /* prevents iOS zoom */
    padding: 10px 12px;
    border-radius: 20px;
  }

  #send-btn {
    padding: 10px 14px;
    border-radius: 50%;
    min-width: 40px;
    min-height: 40px;
    font-size: 14px;
  }

  .header-controls {
    gap: 4px;
    padding: 0 6px;
  }

  #model-select {
    max-width: 80px;
    font-size: 10px;
  }

  .settings-panel {
    width: 95%;
    max-height: 90vh;
    padding: 16px;
  }

  /* Bigger touch targets for tabs */
  .tab {
    padding: 8px 12px;
    font-size: 12px;
  }

  .tab .close-tab {
    padding: 2px 6px;
    font-size: 14px;
  }
}

/* Prevent text selection on long press for tabs */
.tab {
  -webkit-touch-callout: none;
  -webkit-user-select: none;
  user-select: none;
}
