/* === Chat Panel === */

.chat-backdrop {
  display: none; position: fixed; inset: 0;
  background: rgba(0,0,0,0.45); z-index: 1099;
  backdrop-filter: blur(2px); transition: opacity 0.2s;
}
.chat-backdrop.open { display: block; }

.chat-panel {
  position: fixed; top: 0; right: -520px;
  width: 500px; height: 100vh;
  background: var(--bg-secondary, #161b22);
  border-left: 1px solid var(--border, #30363d);
  z-index: 1100; display: flex; flex-direction: column;
  transition: right 0.25s cubic-bezier(.4,0,.2,1);
}
.chat-panel.open { right: 0; }

/* Header */
.chat-panel-header {
  display: flex; align-items: center; justify-content: space-between;
  padding: 12px 16px; border-bottom: 1px solid var(--border, #30363d);
  flex-shrink: 0; gap: 10px;
}
.chat-header-left {
  display: flex; align-items: center; gap: 10px; flex: 1; min-width: 0;
}
.chat-panel-title {
  font-weight: 700; font-size: 0.9rem; white-space: nowrap;
  color: var(--color-text-bright, #e6edf3);
}
.chat-model-select {
  flex: 1; max-width: 220px;
  background: var(--bg-tertiary, #21262d);
  border: 1px solid var(--border, #30363d);
  border-radius: 6px; padding: 4px 8px;
  color: var(--color-text, #e6edf3);
  font-size: 0.78rem; cursor: pointer;
}
.chat-model-select:focus { outline: none; border-color: var(--accent, #58a6ff); }
.chat-header-actions { display: flex; gap: 4px; }
.chat-hdr-btn {
  background: none; border: none;
  color: var(--color-text-muted, #8b949e);
  font-size: 0.9rem; cursor: pointer;
  padding: 4px 8px; border-radius: 6px;
  transition: color 0.15s, background 0.15s;
}
.chat-hdr-btn:hover { color: var(--color-text, #e6edf3); background: rgba(255,255,255,0.07); }

/* Messages */
.chat-messages {
  flex: 1; overflow-y: auto; padding: 16px;
  display: flex; flex-direction: column; gap: 14px;
  scrollbar-width: thin; scrollbar-color: rgba(255,255,255,0.1) transparent;
}

/* Welcome screen */
.chat-welcome {
  display: flex; flex-direction: column; align-items: center;
  justify-content: center; gap: 8px;
  margin: auto; text-align: center; padding: 32px 16px;
}
.chat-welcome-icon { font-size: 2.5rem; }
.chat-welcome-text {
  font-size: 0.9rem; color: var(--color-text, #e6edf3); font-weight: 500;
}
.chat-welcome-sub { font-size: 0.78rem; color: var(--color-text-muted, #8b949e); }

/* Message bubbles */
.chat-msg { display: flex; flex-direction: column; gap: 3px; max-width: 88%; }
.chat-msg-user { align-self: flex-end; align-items: flex-end; }
.chat-msg-assistant { align-self: flex-start; align-items: flex-start; }

.chat-msg-meta {
  font-size: 0.65rem; font-weight: 600; text-transform: uppercase;
  letter-spacing: 0.05em; color: var(--color-text-muted, #8b949e);
  padding: 0 4px;
}

.chat-bubble {
  padding: 10px 14px; border-radius: 12px;
  font-size: 0.85rem; line-height: 1.55;
  word-break: break-word;
}
.chat-msg-user .chat-bubble {
  background: var(--accent, #58a6ff);
  color: #fff; border-bottom-right-radius: 4px;
}
.chat-msg-assistant .chat-bubble {
  background: var(--bg-tertiary, #21262d);
  border: 1px solid var(--border, #30363d);
  color: var(--color-text, #e6edf3);
  border-bottom-left-radius: 4px;
}

/* Markdown in assistant bubble */
.chat-bubble p { margin: 0 0 8px; }
.chat-bubble p:last-child { margin-bottom: 0; }
.chat-bubble pre {
  background: rgba(0,0,0,0.35); border-radius: 8px;
  padding: 10px 12px; overflow-x: auto;
  font-size: 0.78rem; margin: 8px 0;
  border: 1px solid rgba(255,255,255,0.06);
}
.chat-bubble code:not(pre code) {
  background: rgba(255,255,255,0.08);
  padding: 1px 5px; border-radius: 4px;
  font-size: 0.82em;
}
.chat-bubble ul, .chat-bubble ol { padding-left: 18px; margin: 4px 0 8px; }
.chat-bubble li { margin-bottom: 3px; }
.chat-bubble h1,.chat-bubble h2,.chat-bubble h3 {
  font-size: 0.95rem; font-weight: 700; margin: 10px 0 4px;
  color: var(--color-text-bright, #e6edf3);
}
.chat-bubble a { color: var(--accent, #58a6ff); }
.chat-bubble blockquote {
  border-left: 3px solid var(--border, #30363d);
  padding-left: 10px; margin: 6px 0;
  color: var(--color-text-muted, #8b949e);
}

/* Streaming cursor */
.chat-cursor {
  display: inline-block; animation: blink 0.8s step-end infinite;
  color: var(--accent, #58a6ff); font-weight: 700;
}
@keyframes blink { 0%,100% { opacity: 1; } 50% { opacity: 0; } }

.chat-stopped { font-size: 0.72rem; color: var(--color-text-muted); font-style: italic; }
.chat-error { color: var(--color-error, #f85149); font-size: 0.82rem; }

/* Input area */
.chat-input-area {
  flex-shrink: 0; border-top: 1px solid var(--border, #30363d);
  padding: 10px 14px 12px; display: flex; flex-direction: column; gap: 6px;
}
.chat-system-toggle { display: flex; }
.chat-sys-btn {
  background: none; border: 1px solid var(--border, #30363d);
  border-radius: 6px; padding: 3px 10px;
  color: var(--color-text-muted, #8b949e);
  font-size: 0.72rem; cursor: pointer;
  transition: all 0.15s;
}
.chat-sys-btn:hover, .chat-sys-btn.active {
  border-color: var(--accent, #58a6ff); color: var(--accent, #58a6ff);
}
.chat-system-row { }
.chat-system-input {
  width: 100%; box-sizing: border-box;
  background: var(--bg-tertiary, #21262d);
  border: 1px solid var(--border, #30363d);
  border-radius: 6px; padding: 6px 10px;
  color: var(--color-text-muted, #8b949e);
  font-size: 0.78rem; font-family: inherit;
  resize: vertical; min-height: 44px;
}
.chat-input-row { display: flex; gap: 8px; align-items: flex-end; }
.chat-input {
  flex: 1; background: var(--bg-tertiary, #21262d);
  border: 1px solid var(--border, #30363d);
  border-radius: 10px; padding: 9px 12px;
  color: var(--color-text, #e6edf3);
  font-size: 0.85rem; font-family: inherit;
  resize: none; min-height: 40px; max-height: 140px;
  transition: border-color 0.15s;
}
.chat-input:focus {
  outline: none; border-color: var(--accent, #58a6ff);
  box-shadow: 0 0 0 3px rgba(88,166,255,0.1);
}
.chat-send-btn {
  flex-shrink: 0; width: 38px; height: 38px;
  background: var(--accent, #58a6ff);
  border: none; border-radius: 10px;
  color: #fff; cursor: pointer;
  display: flex; align-items: center; justify-content: center;
  transition: all 0.15s;
}
.chat-send-btn:hover:not([disabled]) {
  background: #79b8ff; transform: translateY(-1px);
  box-shadow: 0 3px 12px rgba(88,166,255,0.4);
}
.chat-send-btn:disabled { opacity: 0.4; cursor: not-allowed; }
.chat-input-hint {
  font-size: 0.65rem; color: var(--color-text-muted, #8b949e);
  text-align: right;
}

/* ── History panel ──────────────────────────────────────────────────── */
.chat-history-panel {
  position: absolute; top: 49px; left: 0; right: 0; bottom: 0;
  background: var(--bg-secondary, #161b22);
  z-index: 10; display: flex; flex-direction: column;
  border-top: 1px solid var(--border, #30363d);
}
.chat-history-header {
  display: flex; align-items: center; justify-content: space-between;
  padding: 10px 16px; border-bottom: 1px solid var(--border, #30363d);
  font-size: 0.82rem; font-weight: 600;
  color: var(--color-text-muted, #8b949e);
  flex-shrink: 0;
}
.chat-history-list {
  flex: 1; overflow-y: auto; padding: 8px 0;
  scrollbar-width: thin; scrollbar-color: rgba(255,255,255,0.1) transparent;
}
.chat-history-item {
  display: flex; align-items: center; justify-content: space-between;
  padding: 10px 16px; cursor: pointer; gap: 10px;
  border-bottom: 1px solid rgba(255,255,255,0.04);
  transition: background 0.15s;
}
.chat-history-item:hover { background: rgba(255,255,255,0.04); }
.chat-history-title {
  flex: 1; font-size: 0.82rem; color: var(--color-text, #e6edf3);
  overflow: hidden; white-space: nowrap; text-overflow: ellipsis;
}
.chat-history-date {
  font-size: 0.7rem; color: var(--color-text-muted, #8b949e);
  white-space: nowrap; flex-shrink: 0;
}
.chat-history-loading, .chat-history-empty {
  padding: 24px 16px; text-align: center;
  font-size: 0.8rem; color: var(--color-text-muted, #8b949e);
}

/* ── Attach button ───────────────────────────────────────────────────── */
.chat-attach-icon {
  background: none; border: none;
  color: var(--color-text-muted, #8b949e);
  font-size: 1.1rem; cursor: pointer;
  padding: 0 4px; align-self: flex-end;
  margin-bottom: 4px; transition: color 0.15s;
}
.chat-attach-icon:hover { color: var(--accent, #58a6ff); }

/* ── Attachment chips ────────────────────────────────────────────────── */
.chat-attach-chips {
  display: flex; flex-wrap: wrap; gap: 6px;
  padding: 0 14px; flex-shrink: 0;
}
.chat-chip {
  display: flex; align-items: center; gap: 5px;
  background: var(--bg-tertiary, #21262d);
  border: 1px solid var(--border, #30363d);
  border-radius: 20px; padding: 3px 8px 3px 5px;
  font-size: 0.75rem; color: var(--color-text-muted, #8b949e);
  max-width: 160px;
}
.chat-chip-thumb {
  width: 22px; height: 22px; object-fit: cover; border-radius: 4px;
}
.chat-chip-icon { font-size: 0.85rem; }
.chat-chip-name {
  flex: 1; overflow: hidden; white-space: nowrap; text-overflow: ellipsis;
  max-width: 90px;
}
.chat-chip-rm {
  background: none; border: none; cursor: pointer;
  color: var(--color-text-muted); font-size: 0.65rem; padding: 0 2px;
  flex-shrink: 0;
}
.chat-chip-rm:hover { color: var(--color-error, #f85149); }

/* ── Image previews in bubbles ───────────────────────────────────────── */
.chat-bubble-imgs {
  display: flex; flex-wrap: wrap; gap: 6px; margin-top: 8px;
}
.chat-bubble-img {
  max-width: 160px; max-height: 120px; border-radius: 8px;
  object-fit: cover; border: 1px solid rgba(255,255,255,0.1);
}
.chat-bubble-files {
  display: flex; flex-wrap: wrap; gap: 5px; margin-top: 6px;
}
.chat-chip-inline {
  background: rgba(255,255,255,0.07);
  border-radius: 6px; padding: 2px 8px;
  font-size: 0.72rem; color: rgba(255,255,255,0.6);
}

/* Mobile */
@media (max-width: 560px) {
  .chat-panel { width: 100%; right: -100%; }
}

/* ── Light theme overrides ──────────────────────────────────── */
body.light-theme .chat-panel,
html.light-theme-init body .chat-panel {
  background: #ffffff;
  border-left-color: rgba(0,0,0,0.10);
}
body.light-theme .chat-panel-header,
body.light-theme .chat-input-area,
body.light-theme .chat-history-header,
html.light-theme-init body .chat-panel-header,
html.light-theme-init body .chat-input-area,
html.light-theme-init body .chat-history-header {
  border-color: rgba(0,0,0,0.10);
}
body.light-theme .chat-hdr-btn:hover,
html.light-theme-init body .chat-hdr-btn:hover {
  background: rgba(0,0,0,0.06);
}
body.light-theme .chat-model-select,
body.light-theme .chat-system-input,
body.light-theme .chat-input,
html.light-theme-init body .chat-model-select,
html.light-theme-init body .chat-system-input,
html.light-theme-init body .chat-input {
  background: #dde4ed;
  border-color: rgba(0,0,0,0.10);
  color: #1a2636;
}
body.light-theme .chat-model-select:focus,
body.light-theme .chat-input:focus,
html.light-theme-init body .chat-model-select:focus,
html.light-theme-init body .chat-input:focus {
  border-color: #1a5ec7;
  box-shadow: 0 0 0 3px rgba(26,94,199,0.10);
}
body.light-theme .chat-messages,
html.light-theme-init body .chat-messages {
  scrollbar-color: rgba(0,0,0,0.12) transparent;
}
body.light-theme .chat-msg-user .chat-bubble,
html.light-theme-init body .chat-msg-user .chat-bubble {
  background: #1a5ec7;
}
body.light-theme .chat-msg-assistant .chat-bubble,
html.light-theme-init body .chat-msg-assistant .chat-bubble {
  background: #dde4ed;
  border-color: rgba(0,0,0,0.10);
}
body.light-theme .chat-bubble pre,
html.light-theme-init body .chat-bubble pre {
  background: rgba(0,0,0,0.05);
  border-color: rgba(0,0,0,0.08);
}
body.light-theme .chat-bubble code:not(pre code),
html.light-theme-init body .chat-bubble code:not(pre code) {
  background: rgba(0,0,0,0.07);
}
body.light-theme .chat-bubble blockquote,
html.light-theme-init body .chat-bubble blockquote {
  border-left-color: rgba(0,0,0,0.18);
}
body.light-theme .chat-bubble a,
body.light-theme .chat-cursor,
html.light-theme-init body .chat-bubble a,
html.light-theme-init body .chat-cursor {
  color: #1a5ec7;
}
body.light-theme .chat-sys-btn,
html.light-theme-init body .chat-sys-btn {
  border-color: rgba(0,0,0,0.10);
}
body.light-theme .chat-sys-btn:hover,
body.light-theme .chat-sys-btn.active,
html.light-theme-init body .chat-sys-btn:hover,
html.light-theme-init body .chat-sys-btn.active {
  border-color: #1a5ec7;
  color: #1a5ec7;
}
body.light-theme .chat-send-btn,
html.light-theme-init body .chat-send-btn {
  background: #1a5ec7;
}
body.light-theme .chat-send-btn:hover:not([disabled]),
html.light-theme-init body .chat-send-btn:hover:not([disabled]) {
  background: #1b5fc0;
  box-shadow: 0 3px 12px rgba(26,94,199,0.35);
}
body.light-theme .chat-history-panel,
html.light-theme-init body .chat-history-panel {
  background: #ffffff;
  border-top-color: rgba(0,0,0,0.10);
}
body.light-theme .chat-history-item,
html.light-theme-init body .chat-history-item {
  border-bottom-color: rgba(0,0,0,0.06);
}
body.light-theme .chat-history-item:hover,
html.light-theme-init body .chat-history-item:hover {
  background: rgba(0,0,0,0.04);
}
body.light-theme .chat-history-list,
html.light-theme-init body .chat-history-list {
  scrollbar-color: rgba(0,0,0,0.12) transparent;
}
body.light-theme .chat-chip,
html.light-theme-init body .chat-chip {
  background: #dde4ed;
  border-color: rgba(0,0,0,0.10);
}
body.light-theme .chat-attach-icon:hover,
html.light-theme-init body .chat-attach-icon:hover {
  color: #1a5ec7;
}
body.light-theme .chat-bubble-img,
html.light-theme-init body .chat-bubble-img {
  border-color: rgba(0,0,0,0.12);
}
body.light-theme .chat-chip-inline,
html.light-theme-init body .chat-chip-inline {
  background: rgba(0,0,0,0.06);
  color: #5a7082;
}
