:root {
  --bg-primary: #1a1a2e;
  --bg-secondary: #16213e;
  --bg-sidebar: #0f3460;
  --bg-input: #1a1a3e;
  --text-primary: #e0e0e0;
  --text-secondary: #a0a0b0;
  --text-muted: #606070;
  --accent: #4fc3f7;
  --accent-hover: #29b6f6;
  --user-bubble: #1565c0;
  --ai-bubble: #2a2a4a;
  --bot-bubble: #e65100;
  --border: #2a2a4a;
  --scrollbar-track: #1a1a2e;
  --scrollbar-thumb: #4fc3f7;
  --danger: #ef5350;
  --success: #66bb6a;
  --shadow: rgba(0,0,0,0.3);
  --sidebar-width: 280px;
  --sidebar-min: 200px;
  --sidebar-max: 500px;
  --radius: 8px;
  --touch-target: 44px; /* minimum touch target size */
}

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

body {
  font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', 'PingFang SC', 'Microsoft YaHei', sans-serif;
  background: var(--bg-primary);
  color: var(--text-primary);
  height: 100vh;
  overflow: hidden;
  user-select: none;
}

body.dragging { user-select: none; }
body.dragging * { cursor: col-resize !important; }

::-webkit-scrollbar { width: 8px; }
::-webkit-scrollbar-track { background: var(--scrollbar-track); }
::-webkit-scrollbar-thumb { background: var(--scrollbar-thumb); border-radius: 4px; }
::-webkit-scrollbar-thumb:hover { background: var(--accent-hover); }

.app { display: flex; height: 100vh; }

.sidebar {
  width: var(--sidebar-width);
  min-width: var(--sidebar-min);
  max-width: var(--sidebar-max);
  background: var(--bg-sidebar);
  display: flex;
  flex-direction: column;
  border-right: 1px solid var(--border);
  position: relative;
  flex-shrink: 0;
  transition: margin-left 0.3s ease;
}

.sidebar.closed {
  margin-left: calc(-1 * var(--sidebar-width));
}

.resize-handle {
  position: absolute;
  right: 0; top: 0; bottom: 0;
  width: 4px;
  cursor: col-resize;
  z-index: 10;
  background: transparent;
}

.resize-handle:hover, .resize-handle.active { background: var(--accent); }

.sidebar-header {
  padding: 16px;
  font-size: 20px;
  font-weight: bold;
  border-bottom: 1px solid var(--border);
  display: flex;
  align-items: center;
  gap: 8px;
}

.sidebar-header .logo {
  font-size: 22px;
  margin-right: 4px;
}

.main-header .hamburger-toggle {
  display: none;
}

/* Hamburger — now visible on both desktop and mobile, placed in main-header right side */
.mobile-hamburger {
  display: flex !important;
  background: none;
  border: none;
  color: var(--text-primary);
  font-size: 24px;
  cursor: pointer;
  padding: 4px 10px;
  min-width: 48px;
  min-height: 48px;
  align-items: center;
  justify-content: center;
  border-radius: 8px;
  transition: background 0.2s;
}

.mobile-hamburger:hover {
  background: rgba(79, 195, 247, 0.15);
}

/* Hamburger menu */
.sidebar-tabs { display: flex; border-bottom: 1px solid var(--border); }

.sidebar-tab {
  flex: 1;
  padding: 10px 8px;
  text-align: center;
  cursor: pointer;
  font-size: 13px;
  color: var(--text-secondary);
  border-bottom: 2px solid transparent;
  transition: all 0.2s;
}

.sidebar-tab:hover { color: var(--text-primary); background: rgba(255,255,255,0.05); }
.sidebar-tab.active { color: var(--accent); border-bottom-color: var(--accent); }

.sidebar-content { flex: 1; overflow-y: auto; padding: 8px; }
.sidebar-footer { padding: 12px 12px; border-top: 1px solid var(--border); font-size: 13px; color: var(--text-secondary); text-align: center; flex-shrink: 0; }
.footer-contact { line-height: 1.8; font-weight: 500; }

.sidebar-panel { display: none; }
.sidebar-panel.active { display: block; }

/* Edge strip — removed, sidebar toggle always visible in sidebar header */

.main {
  flex: 1;
  display: flex;
  flex-direction: column;
  min-width: 0;
  background: var(--bg-primary);
}

.main-header {
  padding: 12px 20px;
  border-bottom: 1px solid var(--border);
  display: flex;
  align-items: center;
  gap: 12px;
  min-height: 56px;
}

.hamburger {
  display: none;
  background: none;
  border: none;
  color: var(--text-primary);
  font-size: 24px;
  cursor: pointer;
}

.main-title { font-size: 16px; font-weight: 600; flex: 1; }
.main-subtitle { font-size: 12px; color: var(--text-muted); }

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

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

.empty-state .icon { font-size: 48px; }
.empty-state .text { font-size: 16px; }

.message {
  display: flex;
  gap: 8px;
  max-width: 75%;
  animation: fadeIn 0.2s ease;
}

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

.message.user { align-self: flex-end; flex-direction: row-reverse; }
.message.ai { align-self: flex-start; }
.message.bot { align-self: flex-end; flex-direction: row-reverse; }

.message-avatar {
  width: 32px; height: 32px;
  border-radius: 50%;
  background: var(--accent);
  display: flex; align-items: center; justify-content: center;
  font-size: 16px;
  flex-shrink: 0;
}

.message.user .message-avatar { background: var(--user-bubble); }
.message.bot .message-avatar { background: var(--bot-bubble); }

.message-body { display: flex; flex-direction: column; gap: 2px; }
.message-name { font-size: 12px; color: var(--text-muted); padding: 0 4px; }
.message.user .message-name, .message.bot .message-name { text-align: right; }

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

.message.ai .message-bubble { background: var(--ai-bubble); border-top-left-radius: 2px; }
.message.user .message-bubble { background: var(--user-bubble); border-top-right-radius: 2px; }
.message.bot .message-bubble { background: var(--bot-bubble); border-top-right-radius: 2px; }

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

.message.system-msg {
  align-self: center;
  background: rgba(79, 195, 247, 0.1);
  border: 1px solid var(--accent);
  border-radius: var(--radius);
  padding: 8px 16px;
  font-size: 12px;
  color: var(--accent);
  max-width: 90%;
  text-align: center;
}

.typing-indicator { display: flex; gap: 4px; padding: 8px 14px; align-items: center; }

.typing-indicator .dot {
  width: 8px; height: 8px;
  border-radius: 50%;
  background: var(--text-muted);
  animation: bounce 1.4s infinite ease-in-out;
}

.typing-indicator .dot:nth-child(1) { animation-delay: -0.32s; }
.typing-indicator .dot:nth-child(2) { animation-delay: -0.16s; }

@keyframes bounce {
  0%, 80%, 100% { transform: scale(0.6); opacity: 0.4; }
  40% { transform: scale(1); opacity: 1; }
}

.input-area {
  padding: 16px 20px;
  border-top: 1px solid var(--border);
  display: flex;
  gap: 8px;
  align-items: center;
  background: var(--bg-secondary);
}

.input-area textarea {
  flex: 1;
  background: var(--bg-input);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  color: var(--text-primary);
  padding: 10px 14px;
  font-size: 14px;
  resize: none;
  outline: none;
  font-family: inherit;
  min-height: 40px;
  max-height: 120px;
}

.input-area textarea:focus { border-color: var(--accent); }
.input-area textarea::placeholder { color: var(--text-muted); }

.btn {
  padding: 10px 16px;
  border: none;
  border-radius: var(--radius);
  cursor: pointer;
  font-size: 14px;
  font-weight: 500;
  transition: all 0.2s;
  display: flex;
  align-items: center;
  gap: 4px;
  white-space: nowrap;
}

.btn-primary { background: var(--accent); color: #fff; }
.btn-primary:hover { background: var(--accent-hover); }
.btn-danger { background: var(--danger); color: #fff; }
.btn-danger:hover { opacity: 0.85; }
.btn-ghost { background: transparent; color: var(--text-secondary); border: 1px solid var(--border); }
.btn-ghost:hover { background: rgba(255,255,255,0.05); color: var(--text-primary); }
.btn-sm { padding: 6px 10px; font-size: 12px; }
.btn-icon { background: none; border: none; color: var(--text-secondary); cursor: pointer; font-size: 18px; padding: 4px; }
.btn-icon:hover { color: var(--text-primary); }

.chat-item {
  padding: 10px 12px;
  border-radius: var(--radius);
  cursor: pointer;
  margin-bottom: 4px;
  transition: background 0.15s;
  display: flex;
  flex-direction: column;
  gap: 4px;
  min-height: 56px;
}

.chat-item:hover { background: rgba(255,255,255,0.08); }
.chat-item.active { background: rgba(79, 195, 247, 0.15); border-left: 3px solid var(--accent); }

.chat-item-row {
  display: flex;
  align-items: center;
  gap: 6px;
  font-size: 14px;
  font-weight: 500;
  min-width: 0;
  flex: 1;
}

.chat-item-row .chat-title-text {
  flex: 1;
  min-width: 0;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.chat-item-row .edit-title-input {
  flex: 1; min-width: 0; background: var(--bg-input); border: 1px solid var(--accent);
  border-radius: 4px; color: var(--text-primary); font-size: 14px; padding: 2px 6px;
  outline: none; font-family: inherit;
}

.chat-item-row .chat-item-actions {
  display: flex; gap: 4px; margin-left: auto; flex-shrink: 0;
}

.chat-item-preview {
  font-size: 12px;
  color: var(--text-muted);
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
  flex-shrink: 0;
}

.chat-type-badge { font-size: 10px; padding: 1px 6px; border-radius: 10px; background: rgba(255,255,255,0.1); flex-shrink: 0; }

.new-chat-btn {
  width: 100%;
  padding: 10px;
  margin-bottom: 8px;
  background: rgba(79, 195, 247, 0.1);
  border: 1px dashed var(--accent);
  border-radius: var(--radius);
  color: var(--accent);
  cursor: pointer;
  font-size: 14px;
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 6px;
  transition: all 0.2s;
}

.new-chat-btn:hover { background: rgba(79, 195, 247, 0.2); }

.agent-item {
  padding: 8px 10px;
  border-radius: var(--radius);
  margin-bottom: 4px;
  display: flex;
  align-items: center;
  gap: 8px;
  background: rgba(255,255,255,0.03);
  cursor: pointer;
  transition: background 0.15s;
}

.agent-item:hover { background: rgba(255,255,255,0.08); }
.agent-item.selected { background: rgba(79, 195, 247, 0.15); border: 1px solid var(--accent); }
.agent-item.bot-type { border-left: 3px solid var(--bot-bubble); background: rgba(230,81,0,0.05); }

.agent-avatar {
  width: 28px; height: 28px; border-radius: 50%;
  display: flex; align-items: center; justify-content: center;
  font-size: 14px; flex-shrink: 0;
}

.agent-info { flex: 1; min-width: 0; }
.agent-name { font-size: 13px; font-weight: 500; }
.agent-prompt { font-size: 11px; color: var(--text-muted); overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.agent-type-tag { font-size: 10px; padding: 1px 6px; border-radius: 10px; background: rgba(255,255,255,0.1); flex-shrink: 0; }

.api-config-card {
  padding: 12px;
  border-radius: var(--radius);
  background: rgba(255,255,255,0.03);
  border: 1px solid var(--border);
  margin-bottom: 8px;
}

.api-config-card .field { margin-bottom: 8px; }
.api-config-card label { display: block; font-size: 12px; color: var(--text-secondary); margin-bottom: 3px; }

.api-config-card input {
  width: 100%; padding: 6px 10px;
  background: var(--bg-input);
  border: 1px solid var(--border);
  border-radius: 4px;
  color: var(--text-primary);
  font-size: 13px;
  outline: none;
  font-family: inherit;
}

.api-config-card input:focus { border-color: var(--accent); }

.api-config-card select {
  width: 100%; padding: 6px 10px;
  background: var(--bg-input);
  border: 1px solid var(--border);
  border-radius: 4px;
  color: var(--text-primary);
  font-size: 13px;
  outline: none;
  font-family: inherit;
}

.api-config-card select option { background: var(--bg-input); }

.api-status { font-size: 12px; margin-top: 6px; display: flex; align-items: center; gap: 6px; }
.api-status.connected { color: var(--success); }
.api-status.error { color: var(--danger); }
.api-status.checking { color: var(--text-muted); }

.modal-overlay {
  display: none;
  position: fixed; inset: 0;
  background: rgba(0,0,0,0.6);
  z-index: 100;
  align-items: center;
  justify-content: center;
}

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

.modal {
  background: var(--bg-secondary);
  border: 1px solid var(--border);
  border-radius: 12px;
  padding: 24px;
  width: 90%;
  max-width: 480px;
  max-height: 80vh;
  overflow-y: auto;
  box-shadow: 0 8px 32px var(--shadow);
}

.modal h3 { margin-bottom: 16px; font-size: 18px; display: flex; align-items: center; gap: 8px; }
.modal-close { margin-left: auto; cursor: pointer; color: var(--text-muted); font-size: 20px; }
.modal-close:hover { color: var(--text-primary); }

.form-group { margin-bottom: 14px; }
.form-group label { display: block; font-size: 13px; color: var(--text-secondary); margin-bottom: 4px; }
.form-group input, .form-group textarea, .form-group select {
  width: 100%; padding: 8px 12px;
  background: var(--bg-input);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  color: var(--text-primary);
  font-size: 14px;
  outline: none;
  font-family: inherit;
}

.form-group input:focus, .form-group textarea:focus, .form-group select:focus { border-color: var(--accent); }
.form-group textarea { min-height: 80px; resize: vertical; }
.form-group select option { background: var(--bg-input); }

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

.control-btn {
  padding: 6px 12px;
  border: 1px solid var(--border);
  border-radius: var(--radius);
  background: transparent;
  color: var(--text-muted);
  cursor: pointer;
  font-size: 13px;
  font-weight: 500;
  transition: all 0.2s;
  white-space: nowrap;
  display: flex;
  align-items: center;
  gap: 4px;
}

.control-btn:hover { color: var(--text-primary); background: rgba(255,255,255,0.05); }

.control-btn.on {
  border-color: var(--accent);
  color: var(--accent);
  background: rgba(79, 195, 247, 0.1);
}

.message-bubble p { margin-bottom: 6px; }
.message-bubble p:last-child { margin-bottom: 0; }
.message-bubble strong { font-weight: 600; color: var(--accent); }
.message-bubble em { font-style: italic; }

.message-bubble code {
  background: rgba(0,0,0,0.3);
  padding: 2px 6px;
  border-radius: 4px;
  font-size: 13px;
  font-family: 'Consolas', 'Monaco', monospace;
}

.message-bubble pre {
  background: rgba(0,0,0,0.3);
  padding: 12px;
  border-radius: var(--radius);
  overflow-x: auto;
  margin: 8px 0;
}

.message-bubble pre code { background: none; padding: 0; }

.message-bubble ul, .message-bubble ol { padding-left: 20px; margin: 6px 0; }

.message-bubble blockquote {
  border-left: 3px solid var(--accent);
  padding-left: 12px;
  color: var(--text-secondary);
  margin: 6px 0;
}

.welcome-msg {
  text-align: center;
  padding: 20px;
  color: var(--text-secondary);
  font-size: 13px;
  line-height: 1.6;
  max-width: 500px;
  margin: 0 auto;
}

.welcome-msg .title { font-size: 18px; font-weight: 600; color: var(--accent); margin-bottom: 8px; }

@media (max-width: 768px) {
  .sidebar {
    position: fixed;
    left: -280px; top: 0; bottom: 0;
    z-index: 50;
    transition: left 0.3s ease;
    width: 280px !important;
    min-width: 280px !important;
    max-width: 280px !important;
  }

  .sidebar.open { left: 0; }

  .mobile-hamburger {
    display: flex !important;
  }

  .message { max-width: 85%; }
  .resize-handle { display: none; }

  /* Header: larger touch target */
  .main-header { padding: 10px 12px; min-height: 48px; gap: 8px; }
  .main-title { font-size: 15px; }
  .main-subtitle { font-size: 11px; }
  .btn-icon { font-size: 16px; padding: 8px; min-width: var(--touch-target); min-height: var(--touch-target); display: flex; align-items: center; justify-content: center; }
  #stopBtn { min-height: var(--touch-target); padding: 8px 12px; font-size: 13px; }

  /* Chat area: less padding on mobile */
  .chat-area { padding: 12px; gap: 10px; }

  /* Messages: larger avatars */
  .message-avatar { width: 28px; height: 28px; font-size: 14px; }
  .message-bubble { padding: 8px 12px; font-size: 15px; line-height: 1.5; }
  .message-name { font-size: 11px; }
  .message-time { font-size: 10px; }
  .message.system-msg { max-width: 95%; padding: 6px 12px; font-size: 11px; }

  /* Input area: optimized for mobile keyboard */
  .input-area { padding: 10px 12px; gap: 6px; background: var(--bg-secondary); position: relative; z-index: 5; }
  .input-area textarea { font-size: 16px; padding: 10px 12px; min-height: 44px; line-height: 1.4; } /* 16px prevents iOS zoom */
  .input-area textarea::placeholder { font-size: 14px; }
  #sendBtn { min-height: var(--touch-target); min-width: var(--touch-target); padding: 0 16px; font-size: 18px; justify-content: center; }
  .control-btn { padding: 8px 10px; font-size: 13px; min-height: var(--touch-target); display: flex; align-items: center; }

  /* Sidebar: full height, no footer overflow */
  .sidebar-footer { padding: 10px; font-size: 12px; }
  .footer-contact { font-size: 12px; }

  /* Modals: full screen on mobile */
  .modal { width: 95%; max-width: none; max-height: 90vh; padding: 20px 16px; border-radius: 12px 12px 0 0; margin-top: auto; position: fixed; bottom: 0; left: 0; right: 0; }
  .modal h3 { font-size: 16px; }
  .form-group input, .form-group textarea, .form-group select { font-size: 16px; padding: 10px 12px; }
  .btn { min-height: var(--touch-target); padding: 10px 14px; font-size: 15px; }
  .btn-sm { min-height: var(--touch-target); padding: 8px 12px; font-size: 14px; }

  /* Chat list items: taller for touch */
  .chat-item { padding: 12px; min-height: 64px; }
  .chat-item-row { font-size: 15px; }

  /* Agent items: taller for touch */
  .agent-item { padding: 10px 12px; min-height: 48px; }
  .new-chat-btn { padding: 12px; font-size: 15px; min-height: var(--touch-target); }

  /* Empty state */
  .empty-state .icon { font-size: 40px; }
  .empty-state .text { font-size: 14px; }

  /* Welcome message */
  .welcome-msg { padding: 16px 12px; font-size: 12px; }
  .welcome-msg .title { font-size: 16px; }

  /* Typing indicator: smaller dots */
  .typing-indicator .dot { width: 6px; height: 6px; }

  /* Prevent horizontal scroll */
  body { overflow-x: hidden; overflow-y: auto; }

  /* Ensure main takes full width */
  .main { width: 100vw; min-width: 0; }
  .app { overflow-x: hidden; }
}
