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

:root {
  --primary: #2563eb;
  --primary-hover: #1d4ed8;
  --primary-light: #dbeafe;
  --bg: #f8fafc;
  --bg-sidebar: #1e293b;
  --bg-sidebar-hover: #334155;
  --bg-sidebar-active: #3b82f6;
  --text: #1e293b;
  --text-light: #64748b;
  --text-sidebar: #e2e8f0;
  --border: #e2e8f0;
  --msg-user: #eff6ff;
  --msg-assistant: #ffffff;
  --danger: #ef4444;
  --danger-hover: #dc2626;
  --radius: 12px;
  --sidebar-width: 280px;
  --transition: 0.2s ease;
}

html, body { height: 100%; font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif; font-size: 15px; color: var(--text); background: var(--bg); }

/* ===== Layout ===== */
#app { display: flex; height: 100vh; overflow: hidden; }

/* ===== Sidebar ===== */
#sidebar {
  width: var(--sidebar-width);
  min-width: var(--sidebar-width);
  background: var(--bg-sidebar);
  color: var(--text-sidebar);
  display: flex;
  flex-direction: column;
  transition: margin-left var(--transition), min-width var(--transition), width var(--transition);
  overflow: hidden;
  z-index: 20;
}
#sidebar.collapsed { margin-left: calc(-1 * var(--sidebar-width)); min-width: 0; width: 0; }

.sidebar-header { display: flex; align-items: center; justify-content: space-between; padding: 16px 16px 8px; }
.app-title { font-size: 18px; font-weight: 700; letter-spacing: 0.5px; }
#btn-new-chat { font-size: 22px; background: none; border: none; color: var(--text-sidebar); cursor: pointer; border-radius: 8px; width: 36px; height: 36px; display: flex; align-items: center; justify-content: center; transition: background var(--transition); }
#btn-new-chat:hover { background: var(--bg-sidebar-hover); }

.sidebar-search { padding: 4px 16px 8px; }
.sidebar-search input { width: 100%; padding: 8px 12px; border-radius: 8px; border: 1px solid #475569; background: #0f172a; color: var(--text-sidebar); font-size: 13px; outline: none; }
.sidebar-search input::placeholder { color: #94a3b8; }
.sidebar-search input:focus { border-color: var(--primary); }

#conversation-list { flex: 1; overflow-y: auto; list-style: none; padding: 0 8px; }
#conversation-list::-webkit-scrollbar { width: 4px; }
#conversation-list::-webkit-scrollbar-thumb { background: #475569; border-radius: 4px; }

.conv-item {
  display: flex; align-items: center; padding: 10px 12px; border-radius: 8px;
  cursor: pointer; transition: background var(--transition); margin-bottom: 2px;
  position: relative; font-size: 14px; overflow: hidden; white-space: nowrap; text-overflow: ellipsis;
}
.conv-item:hover { background: var(--bg-sidebar-hover); }
.conv-item.active { background: var(--bg-sidebar-active); font-weight: 600; }
.conv-item .conv-title { flex: 1; overflow: hidden; text-overflow: ellipsis; }
.conv-item .conv-delete {
  visibility: hidden; background: none; border: none; color: #94a3b8; cursor: pointer;
  font-size: 14px; padding: 2px 4px; border-radius: 4px; transition: all var(--transition);
}
.conv-item:hover .conv-delete { visibility: visible; }
.conv-item .conv-delete:hover { color: var(--danger); }

.sidebar-footer { padding: 12px 16px; border-top: 1px solid #334155; }
#btn-settings {
  width: 100%; padding: 10px; border-radius: 8px; border: 1px solid #475569;
  background: none; color: var(--text-sidebar); font-size: 14px; cursor: pointer;
  transition: all var(--transition);
}
#btn-settings:hover { background: var(--bg-sidebar-hover); border-color: #64748b; }

/* ===== Main ===== */
#main { flex: 1; display: flex; flex-direction: column; overflow: hidden; position: relative; }

/* Welcome Screen */
.welcome-screen { flex: 1; display: flex; align-items: center; justify-content: center; }
.welcome-content { text-align: center; color: var(--text-light); }
.welcome-icon { font-size: 64px; margin-bottom: 16px; }
.welcome-content h2 { font-size: 24px; color: var(--text); margin-bottom: 8px; }
.welcome-content p { font-size: 15px; line-height: 1.6; }

/* Chat Area */
.chat-area { flex: 1; display: flex; flex-direction: column; overflow: hidden; }

.chat-header {
  display: flex; align-items: center; gap: 8px; padding: 12px 20px;
  border-bottom: 1px solid var(--border); background: #fff; font-weight: 600; font-size: 16px;
}
#chat-title { flex: 1; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }

.icon-btn {
  background: none; border: none; font-size: 18px; cursor: pointer; color: var(--text-light);
  padding: 6px 8px; border-radius: 6px; transition: all var(--transition); line-height: 1;
}
.icon-btn:hover { background: var(--primary-light); color: var(--primary); }

/* Messages */
.messages { flex: 1; overflow-y: auto; padding: 20px; display: flex; flex-direction: column; gap: 16px; }
.messages::-webkit-scrollbar { width: 6px; }
.messages::-webkit-scrollbar-thumb { background: #cbd5e1; border-radius: 6px; }

.msg { display: flex; gap: 12px; max-width: 85%; animation: msgIn 0.25s ease; }
@keyframes msgIn { from { opacity: 0; transform: translateY(8px); } to { opacity: 1; transform: translateY(0); } }
.msg.user { align-self: flex-end; flex-direction: row-reverse; }
.msg.assistant { align-self: flex-start; }

.msg-avatar { width: 34px; height: 34px; border-radius: 50%; display: flex; align-items: center; justify-content: center; font-size: 16px; flex-shrink: 0; }
.msg.user .msg-avatar { background: var(--primary); color: #fff; }
.msg.assistant .msg-avatar { background: #e0e7ff; color: #4f46e5; }

.msg-bubble {
  padding: 12px 16px; border-radius: var(--radius); line-height: 1.65; word-break: break-word; position: relative;
}
.msg.user .msg-bubble { background: var(--primary); color: #fff; border-bottom-right-radius: 4px; }
.msg.assistant .msg-bubble { background: var(--msg-assistant); border: 1px solid var(--border); border-bottom-left-radius: 4px; }

.msg-bubble p { margin-bottom: 8px; }
.msg-bubble p:last-child { margin-bottom: 0; }
.msg-bubble code { background: #f1f5f9; padding: 2px 6px; border-radius: 4px; font-size: 13px; font-family: "Cascadia Code", "Fira Code", monospace; }
.msg.user .msg-bubble code { background: rgba(255,255,255,0.2); }
.msg-bubble pre { background: #1e293b; color: #e2e8f0; padding: 12px 16px; border-radius: 8px; overflow-x: auto; margin: 8px 0; }
.msg-bubble pre code { background: none; padding: 0; color: inherit; font-size: 13px; }

.msg-images { display: flex; flex-wrap: wrap; gap: 8px; margin-bottom: 8px; }
.msg-images img { max-width: 200px; max-height: 200px; border-radius: 8px; cursor: pointer; object-fit: cover; border: 1px solid var(--border); }

.msg-actions {
  display: none; gap: 4px; position: absolute; top: -12px; right: -4px;
  background: #fff; border-radius: 8px; box-shadow: 0 2px 8px rgba(0,0,0,0.12); padding: 2px; z-index: 5;
}
.msg.user .msg-actions { right: auto; left: -4px; }
.msg:hover .msg-actions { display: flex; }
.msg-action-btn { background: none; border: none; font-size: 14px; cursor: pointer; padding: 4px 6px; border-radius: 4px; transition: background var(--transition); }
.msg-action-btn:hover { background: var(--primary-light); }

.msg-timestamp { font-size: 11px; color: var(--text-light); margin-top: 4px; opacity: 0.7; }
.msg.user .msg-timestamp { text-align: right; color: rgba(255,255,255,0.6); }

.typing-indicator { display: inline-flex; gap: 4px; padding: 4px 0; }
.typing-indicator span { width: 6px; height: 6px; background: var(--text-light); border-radius: 50%; animation: typing 1.2s infinite; }
.typing-indicator span:nth-child(2) { animation-delay: 0.2s; }
.typing-indicator span:nth-child(3) { animation-delay: 0.4s; }
@keyframes typing { 0%, 60%, 100% { transform: translateY(0); opacity: 0.4; } 30% { transform: translateY(-6px); opacity: 1; } }

/* Input Area */
.input-area { padding: 12px 20px 20px; background: #fff; border-top: 1px solid var(--border); }
.image-preview-bar { display: flex; gap: 8px; margin-bottom: 8px; flex-wrap: wrap; }
.image-preview-bar:empty { display: none; margin: 0; }
.image-preview-item { position: relative; width: 64px; height: 64px; border-radius: 8px; overflow: hidden; border: 2px solid var(--primary-light); }
.image-preview-item img { width: 100%; height: 100%; object-fit: cover; }
.image-preview-item .remove-img {
  position: absolute; top: 2px; right: 2px; width: 18px; height: 18px;
  background: rgba(0,0,0,0.6); color: #fff; border: none; border-radius: 50%;
  font-size: 10px; cursor: pointer; display: flex; align-items: center; justify-content: center;
  line-height: 1;
}

.input-row { display: flex; align-items: flex-end; gap: 8px; }
#msg-input {
  flex: 1; resize: none; border: 1px solid var(--border); border-radius: 12px;
  padding: 10px 16px; font-size: 15px; font-family: inherit; line-height: 1.5;
  max-height: 150px; outline: none; transition: border-color var(--transition);
}
#msg-input:focus { border-color: var(--primary); }

.send-btn { font-size: 20px !important; }
.send-btn:hover { color: #fff !important; background: var(--primary) !important; }
.send-btn:disabled { opacity: 0.4; cursor: not-allowed; }
.send-btn:disabled:hover { background: var(--primary-light) !important; color: var(--text-light) !important; }

/* ===== Modals ===== */
.modal-overlay { position: fixed; inset: 0; background: rgba(0,0,0,0.4); display: flex; align-items: center; justify-content: center; z-index: 100; animation: fadeIn 0.15s ease; }
@keyframes fadeIn { from { opacity: 0; } to { opacity: 1; } }
.modal { background: #fff; border-radius: 16px; width: 440px; max-width: 92vw; box-shadow: 0 20px 60px rgba(0,0,0,0.2); animation: slideUp 0.2s ease; }
.modal-sm { width: 360px; }
@keyframes slideUp { from { transform: translateY(16px); opacity: 0; } to { transform: translateY(0); opacity: 1; } }
.modal-header { display: flex; align-items: center; justify-content: space-between; padding: 20px 24px 12px; }
.modal-header h2 { font-size: 18px; }
.modal-body { padding: 8px 24px 24px; display: flex; flex-direction: column; gap: 16px; }
.modal-body label { font-size: 14px; font-weight: 600; color: var(--text); display: flex; flex-direction: column; gap: 6px; }
.modal-body input, .modal-body textarea { padding: 10px 14px; border: 1px solid var(--border); border-radius: 8px; font-size: 14px; font-family: inherit; outline: none; transition: border-color var(--transition); }
.modal-body input:focus, .modal-body textarea:focus { border-color: var(--primary); }
.modal-body textarea { font-family: inherit; resize: vertical; }
.modal-body .hint { font-size: 13px; color: var(--text-light); }
.modal-actions { display: flex; justify-content: flex-end; gap: 8px; margin-top: 4px; }

.primary-btn { padding: 8px 20px; background: var(--primary); color: #fff; border: none; border-radius: 8px; font-size: 14px; cursor: pointer; transition: background var(--transition); }
.primary-btn:hover { background: var(--primary-hover); }
.secondary-btn { padding: 8px 20px; background: #f1f5f9; color: var(--text); border: none; border-radius: 8px; font-size: 14px; cursor: pointer; transition: background var(--transition); }
.secondary-btn:hover { background: #e2e8f0; }
.danger-btn { padding: 8px 20px; background: var(--danger); color: #fff; border: none; border-radius: 8px; font-size: 14px; cursor: pointer; transition: background var(--transition); }
.danger-btn:hover { background: var(--danger-hover); }

/* ===== Context Menu ===== */
.context-menu { position: fixed; background: #fff; border-radius: 8px; box-shadow: 0 4px 16px rgba(0,0,0,0.15); padding: 4px; z-index: 200; min-width: 140px; }
.context-menu button { display: flex; align-items: center; gap: 8px; width: 100%; padding: 8px 12px; border: none; background: none; font-size: 14px; cursor: pointer; border-radius: 6px; transition: background var(--transition); text-align: left; }
.context-menu button:hover { background: var(--primary-light); }

/* ===== Scrollbar ===== */
::-webkit-scrollbar { width: 6px; }
::-webkit-scrollbar-track { background: transparent; }
::-webkit-scrollbar-thumb { background: #cbd5e1; border-radius: 6px; }


/* Model input row */
.model-input-row { display: flex; gap: 6px; }
.model-input-row input { flex: 1; }
.fetch-btn {
  padding: 8px 12px; background: var(--primary-light); color: var(--primary);
  border: 1px solid var(--border); border-radius: 8px; font-size: 16px;
  cursor: pointer; transition: all var(--transition); flex-shrink: 0;
}
.fetch-btn:hover { background: var(--primary); color: #fff; }
.fetch-btn:disabled { opacity: 0.5; cursor: not-allowed; }
.fetch-btn.loading { animation: spin 0.8s linear infinite; }
@keyframes spin { from { transform: rotate(0deg); } to { transform: rotate(360deg); } }


/* Token usage & Vision badge */
.token-usage {
  font-size: 12px; font-weight: 400; color: var(--text-light);
  background: var(--primary-light); padding: 3px 10px; border-radius: 12px;
  white-space: nowrap; margin-left: auto;
}
.vision-badge {
  font-size: 16px; cursor: default; padding: 2px 4px; border-radius: 6px;
  background: #f0fdf4; border: 1px solid #bbf7d0;
}

/* Web Search Toggle */
.search-toggle {
  position: relative; display: flex; align-items: center; cursor: pointer;
  user-select: none; height: 38px; padding: 0 2px;
}
.search-toggle input { display: none; }
.search-toggle-slider {
  display: flex; align-items: center; justify-content: center;
  width: 34px; height: 34px; border-radius: 8px; font-size: 18px;
  background: #f1f5f9; border: 1px solid var(--border);
  transition: all var(--transition); opacity: 0.5;
}
.search-toggle input:checked + .search-toggle-slider {
  background: var(--primary-light); border-color: var(--primary);
  opacity: 1; box-shadow: 0 0 0 2px rgba(37,99,235,0.15);
}
.search-toggle:hover .search-toggle-slider { opacity: 0.8; }
.search-toggle input:checked + .search-toggle-slider { opacity: 1; }
/* ===== Responsive ===== */
@media (max-width: 768px) {
  #sidebar { position: fixed; left: 0; top: 0; bottom: 0; z-index: 30; }
  #sidebar.collapsed { margin-left: calc(-1 * var(--sidebar-width)); }
}
