#chatToggleBtn {
  position: fixed;
  bottom: 1.5rem;
  right: 1.5rem;
  z-index: 9998;
  width: 52px;
  height: 52px;
  border-radius: 50%;
  border: 2px solid var(--accent);
  background: var(--bg-sidebar);
  color: var(--accent);
  font-size: 1.4rem;
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: center;
  box-shadow: 0 4px 20px rgba(108,140,255,0.3);
  transition: all 0.25s ease;
}
#chatToggleBtn:hover { background: var(--accent); color: #fff; transform: scale(1.08); }
#chatToggleBtn.open { background: var(--accent); color: #fff; }

#chatPanel {
  position: fixed;
  top: 0; right: 0;
  width: 420px;
  min-width: 320px;
  max-width: 85vw;
  height: 100vh;
  z-index: 9999;
  background: var(--bg-sidebar);
  border-left: 1px solid var(--border);
  display: flex;
  flex-direction: column;
  transform: translateX(100%);
  transition: transform 0.3s cubic-bezier(0.4,0,0.2,1);
  box-shadow: -4px 0 30px rgba(0,0,0,0.4);
}
#chatPanel.open { transform: translateX(0); }

/* ===== RESIZE HANDLE ===== */
#chatResizeHandle {
  position: absolute;
  left: -4px; top: 0;
  width: 8px;
  height: 100%;
  cursor: col-resize;
  z-index: 10;
  background: transparent;
  transition: background 0.15s;
}
#chatResizeHandle:hover,
#chatResizeHandle.active { background: var(--accent); opacity: 0.5; }

#chatPanel .chat-header {
  padding: 0.85rem 1rem;
  border-bottom: 1px solid var(--border);
  display: flex;
  align-items: center;
  justify-content: space-between;
  flex-shrink: 0;
}
#chatPanel .chat-header h3 {
  margin: 0;
  font-size: 0.95rem;
  color: var(--text);
  display: flex;
  align-items: center;
  gap: 0.5rem;
}
#chatPanel .chat-header h3 { flex: 1; }
#chatPanel .chat-header h3 span { font-size: 1.1rem; }
#chatPanel .chat-close {
  background: none;
  border: none;
  color: var(--text-muted);
  font-size: 1.2rem;
  cursor: pointer;
  padding: 0.25rem;
  line-height: 1;
}
#chatPanel .chat-close:hover { color: var(--text); }

#chatPanel .chat-clear {
  background: none;
  border: none;
  color: var(--text-muted);
  font-size: 1rem;
  cursor: pointer;
  padding: 0.2rem;
  line-height: 1;
  margin-left: auto;
  margin-right: 0.25rem;
  opacity: 0.7;
  transition: opacity 0.15s;
}
#chatPanel .chat-clear:hover { color: var(--text); opacity: 1; }

#chatPanel .chat-config {
  padding: 0.5rem 1rem;
  border-bottom: 1px solid var(--border);
  display: flex;
  flex-direction: column;
  gap: 0.35rem;
  flex-shrink: 0;
  font-size: 0.78rem;
}
#chatPanel .chat-config label { color: var(--text-muted); font-size: 0.72rem; display: flex; align-items: baseline; gap: 0.3rem; }
#chatPanel .chat-config .label-hint { color: var(--text-dim); font-size: 0.65rem; font-weight: 400; }
#chatPanel .chat-config input, #chatPanel .chat-config select {
  padding: 0.3rem 0.5rem;
  background: var(--bg-card);
  border: 1px solid var(--border);
  border-radius: 6px;
  color: var(--text);
  font-size: 0.78rem;
  font-family: var(--font-mono);
  outline: none;
}
#chatPanel .chat-config input:focus { border-color: var(--accent); }

/* ===== MODEL SELECTOR ===== */
.chat-model-row {
  display: flex;
  gap: 0.35rem;
  align-items: center;
}
.chat-model-row select {
  flex: 1;
  min-width: 0;
}
.chat-model-row button {
  width: 28px;
  height: 28px;
  border-radius: 6px;
  border: 1px solid var(--border);
  background: var(--bg-card);
  color: var(--text-muted);
  font-size: 0.9rem;
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: center;
  flex-shrink: 0;
  transition: all 0.2s;
  padding: 0;
  line-height: 1;
}
.chat-model-row button:hover { border-color: var(--accent); color: var(--accent); }

#chatMessages {
  flex: 1;
  overflow-y: auto;
  padding: 0.75rem 1rem;
  display: flex;
  flex-direction: column;
  gap: 0.6rem;
}

.chat-msg {
  max-width: 94%;
  padding: 0.65rem 0.9rem;
  border-radius: 12px;
  font-size: 0.84rem;
  line-height: 1.6;
  word-wrap: break-word;
}
.chat-msg.user {
  align-self: flex-end;
  background: var(--accent);
  color: #fff;
  border-bottom-right-radius: 4px;
  white-space: pre-wrap;
}
.chat-msg.assistant {
  align-self: flex-start;
  background: var(--bg-card);
  color: var(--text);
  border: 1px solid var(--border);
  border-bottom-left-radius: 4px;
}
.chat-msg.system-info {
  align-self: center;
  background: transparent;
  color: var(--text-dim);
  font-size: 0.75rem;
  font-style: italic;
  padding: 0.25rem;
  text-align: center;
}
.chat-msg.error {
  align-self: center;
  background: var(--red-bg);
  color: var(--red);
  font-size: 0.78rem;
  border: 1px solid rgba(248,113,113,0.3);
}

.chat-msg .msg-context {
  display: block;
  margin-bottom: 0.4rem;
  padding: 0.35rem 0.5rem;
  background: rgba(255,255,255,0.08);
  border-radius: 6px;
  font-size: 0.76rem;
  color: rgba(255,255,255,0.7);
  border-left: 2px solid rgba(255,255,255,0.3);
  white-space: pre-wrap;
}
.chat-msg.assistant .msg-context {
  background: rgba(108,140,255,0.08);
  color: var(--text-muted);
  border-left-color: var(--accent);
}

/* ===== RENDERED MARKDOWN IN ASSISTANT MESSAGES ===== */
.chat-msg.assistant .msg-body { white-space: normal; }
.chat-msg.assistant .msg-body h1,
.chat-msg.assistant .msg-body h2,
.chat-msg.assistant .msg-body h3,
.chat-msg.assistant .msg-body h4 { margin: 0.7em 0 0.3em; color: var(--accent); font-size: 1em; }
.chat-msg.assistant .msg-body h3 { font-size: 0.95em; }
.chat-msg.assistant .msg-body h4 { font-size: 0.9em; color: var(--green); }
.chat-msg.assistant .msg-body p { margin: 0.35em 0; }
.chat-msg.assistant .msg-body ul, .chat-msg.assistant .msg-body ol {
  margin: 0.3em 0 0.3em 1.2em;
  padding: 0;
}
.chat-msg.assistant .msg-body li { margin: 0.2em 0; }
.chat-msg.assistant .msg-body strong { color: var(--accent); }
.chat-msg.assistant .msg-body em { color: var(--text-muted); }
.chat-msg.assistant .msg-body code {
  background: rgba(108,140,255,0.1);
  padding: 0.1em 0.35em;
  border-radius: 4px;
  font-family: var(--font-mono);
  font-size: 0.88em;
  color: var(--accent);
}
.chat-msg.assistant .msg-body pre {
  background: var(--bg);
  border: 1px solid var(--border);
  border-radius: 8px;
  padding: 0.6em 0.8em;
  overflow-x: auto;
  margin: 0.5em 0;
  font-size: 0.82em;
  line-height: 1.45;
}
.chat-msg.assistant .msg-body pre code {
  background: none;
  padding: 0;
  color: var(--text);
}
.chat-msg.assistant .msg-body blockquote {
  border-left: 3px solid var(--accent);
  margin: 0.4em 0;
  padding: 0.3em 0.7em;
  color: var(--text-muted);
  background: rgba(108,140,255,0.04);
  border-radius: 0 6px 6px 0;
}
.chat-msg.assistant .msg-body hr {
  border: none;
  border-top: 1px solid var(--border);
  margin: 0.6em 0;
}
.chat-msg.assistant .msg-body table {
  border-collapse: collapse;
  margin: 0.5em 0;
  font-size: 0.85em;
  width: 100%;
}
.chat-msg.assistant .msg-body th,
.chat-msg.assistant .msg-body td {
  border: 1px solid var(--border);
  padding: 0.3em 0.5em;
  text-align: left;
}
.chat-msg.assistant .msg-body th {
  background: var(--bg);
  font-weight: 600;
  color: var(--accent);
}

#chatInputArea {
  padding: 0.6rem 0.75rem;
  border-top: 1px solid var(--border);
  flex-shrink: 0;
  display: flex;
  flex-direction: column;
  gap: 0.4rem;
}
#chatSelectionBar {
  display: none;
  padding: 0.35rem 0.6rem;
  background: var(--accent-glow);
  border: 1px solid var(--accent);
  border-radius: 8px;
  font-size: 0.75rem;
  color: var(--accent);
  align-items: center;
  gap: 0.4rem;
}
#chatSelectionBar.has-selection { display: flex; }
#chatSelectionBar .sel-text {
  flex: 1;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
  font-family: var(--font-mono);
  font-size: 0.72rem;
}
#chatSelectionBar button {
  background: none;
  border: none;
  color: var(--accent);
  cursor: pointer;
  font-size: 0.9rem;
  padding: 0;
  line-height: 1;
}

/* ===== FILE ATTACHMENT BAR ===== */
#chatFileBar {
  display: none;
  padding: 0.4rem 0.6rem;
  background: var(--bg-card);
  border: 1px solid var(--border);
  border-radius: 8px;
  font-size: 0.75rem;
  color: var(--text);
  align-items: center;
  gap: 0.5rem;
  overflow: hidden;
}
#chatFileBar.has-file { display: flex; }
#chatFileBar .file-preview-img {
  width: 40px; height: 40px;
  border-radius: 6px;
  object-fit: cover;
  border: 1px solid var(--border);
  flex-shrink: 0;
}
#chatFileBar .file-icon {
  width: 40px; height: 40px;
  border-radius: 6px;
  background: var(--accent-glow);
  border: 1px solid var(--accent);
  display: flex; align-items: center; justify-content: center;
  font-size: 1.1rem;
  flex-shrink: 0;
}
#chatFileBar .file-info {
  flex: 1;
  min-width: 0;
}
#chatFileBar .file-name {
  font-weight: 600;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
  display: block;
  font-size: 0.76rem;
}
#chatFileBar .file-size {
  color: var(--text-dim);
  font-size: 0.68rem;
}
#chatFileBar .file-clear {
  background: none;
  border: none;
  color: var(--text-muted);
  cursor: pointer;
  font-size: 1rem;
  padding: 0.15rem;
  line-height: 1;
  flex-shrink: 0;
}
#chatFileBar .file-clear:hover { color: var(--red); }

#chatFileInput { display: none; }

#chatAttachBtn {
  width: 36px;
  height: 36px;
  border-radius: 50%;
  border: 1px solid var(--border);
  background: transparent;
  color: var(--text-muted);
  font-size: 1.1rem;
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: center;
  flex-shrink: 0;
  transition: all 0.2s;
}
#chatAttachBtn:hover { border-color: var(--accent); color: var(--accent); }

/* User message with image thumbnail */
.chat-msg .msg-image {
  max-width: 200px;
  max-height: 150px;
  border-radius: 8px;
  margin-bottom: 0.4rem;
  border: 1px solid rgba(255,255,255,0.15);
  display: block;
}
.chat-msg .msg-file-badge {
  display: inline-flex;
  align-items: center;
  gap: 0.3rem;
  padding: 0.2rem 0.5rem;
  background: rgba(255,255,255,0.1);
  border-radius: 6px;
  font-size: 0.74rem;
  margin-bottom: 0.35rem;
}

#chatInputRow {
  display: flex;
  gap: 0.4rem;
  align-items: flex-end;
}
#chatInput {
  flex: 1;
  padding: 0.55rem 0.75rem;
  background: var(--bg-card);
  border: 1px solid var(--border);
  border-radius: 10px;
  color: var(--text);
  font-size: 0.84rem;
  font-family: var(--font);
  resize: none;
  outline: none;
  max-height: 100px;
  line-height: 1.45;
}
#chatInput:focus { border-color: var(--accent); }
#chatInput::placeholder { color: var(--text-dim); }

#chatSendBtn {
  width: 36px;
  height: 36px;
  border-radius: 50%;
  border: none;
  background: var(--accent);
  color: #fff;
  font-size: 1rem;
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: center;
  flex-shrink: 0;
  transition: background 0.2s;
}
#chatSendBtn:hover { background: #5a7ae6; }
#chatSendBtn:disabled { opacity: 0.4; cursor: not-allowed; }

/* ===== THINKING / REASONING BLOCK ===== */
.chat-msg.assistant .msg-thinking {
  display: none;
  margin-bottom: 0.5rem;
  border: 1px solid rgba(167,139,250,0.25);
  border-radius: 8px;
  background: rgba(167,139,250,0.06);
  font-size: 0.78rem;
  overflow: hidden;
}
.chat-msg.assistant .msg-thinking summary {
  padding: 0.4rem 0.7rem;
  cursor: pointer;
  color: var(--purple, #a78bfa);
  font-weight: 600;
  font-size: 0.76rem;
  user-select: none;
  list-style: none;
  display: flex;
  align-items: center;
  gap: 0.4rem;
}
.chat-msg.assistant .msg-thinking summary::before {
  content: "▶";
  font-size: 0.6rem;
  transition: transform 0.2s;
}
.chat-msg.assistant .msg-thinking[open] summary::before {
  transform: rotate(90deg);
}
.chat-msg.assistant .msg-thinking summary::-webkit-details-marker { display: none; }
.chat-msg.assistant .msg-thinking .thinking-body {
  padding: 0.4rem 0.7rem 0.6rem;
  color: var(--text-muted);
  line-height: 1.55;
  max-height: 300px;
  overflow-y: auto;
  white-space: pre-wrap;
  word-wrap: break-word;
  border-top: 1px solid rgba(167,139,250,0.15);
  font-size: 0.78rem;
}
.chat-msg.assistant .msg-thinking .thinking-body p { margin: 0.25em 0; }
.chat-msg.assistant .msg-thinking .thinking-body code {
  background: rgba(167,139,250,0.1);
  padding: 0.1em 0.3em;
  border-radius: 3px;
  font-family: var(--font-mono);
  font-size: 0.85em;
}

/* ===== STREAMING CURSOR ===== */
.stream-cursor {
  display: inline;
  animation: cursorBlink 0.7s infinite;
  color: var(--accent);
  font-weight: 400;
  margin-left: 1px;
}
@keyframes cursorBlink {
  0%, 50% { opacity: 1; }
  51%, 100% { opacity: 0; }
}
