/* ==================== Chat UI — Blue & Pink Theme (Enhanced) ==================== */

/* ── Base Theme (CSS Variables) ────────────────────────────────────── */
:root {
  --brand-blue: #2a4ea3;
  --brand-blue-deep: #1f3b85;
  --brand-blue-soft: #eaf1ff;
  --brand-pink: #8e1022;
  --brand-pink-deep: #680b19;
  --brand-pink-soft: #f8e8ec;
  --lobster-orange: #dd5a30;
  --chat-bg: #f7f9fc;
  --chat-sidebar-bg: #ffffff;
  --chat-msg-user-bg: #2a4ea3;
  --chat-msg-user-text: #ffffff;
  --chat-msg-assistant-bg: #ffffff;
  --chat-msg-assistant-text: #1f2740;
  --chat-border: #e0e8f5;
  --chat-input-bg: #ffffff;
  --chat-input-border: #d0daea;
  --chat-text: #1f2740;
  --chat-text-sub: #5d6881;
  --chat-accent: #2a4ea3;
  --chat-radius: 14px;
}

[data-theme="dark"] {
  --chat-bg: #0f1420;
  --chat-sidebar-bg: #161b2e;
  --chat-msg-user-bg: #1f3b85;
  --chat-msg-user-text: #eaf1ff;
  --chat-msg-assistant-bg: #1a2035;
  --chat-msg-assistant-text: #e0e8f5;
  --chat-border: #2a3354;
  --chat-input-bg: #1a2035;
  --chat-input-border: #2a3354;
  --chat-text: #e0e8f5;
  --chat-text-sub: #8899b8;
  --chat-accent: #4a7aff;
}

/* ── Animations ────────────────────────────────────────────────────── */
@keyframes pulse-recording {
  0% { box-shadow: 0 0 0 0 rgba(220, 50, 50, 0.5); }
  70% { box-shadow: 0 0 0 10px rgba(220, 50, 50, 0); }
  100% { box-shadow: 0 0 0 0 rgba(220, 50, 50, 0); }
}

@keyframes slide-in-toast {
  from { opacity: 0; transform: translateY(20px); }
  to { opacity: 1; transform: translateY(0); }
}

@keyframes fade-in-msg {
  from { opacity: 0; transform: translateY(8px); }
  to { opacity: 1; transform: translateY(0); }
}

@keyframes typing-bounce {
  0%, 60%, 100% { transform: translateY(0); }
  30% { transform: translateY(-6px); }
}

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

/* ==================== 1. Main Container ============================ */
.chat-app {
  display: flex;
  height: 100vh;
  height: 100dvh;
  overflow: hidden;
  background: var(--chat-bg);
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, sans-serif;
  color: var(--chat-text);
}

/* ==================== 2. Sidebar =================================== */
.chat-sidebar {
  width: 280px;
  min-width: 280px;
  background: var(--chat-sidebar-bg);
  border-right: 1px solid var(--chat-border);
  display: flex;
  flex-direction: column;
  transition: transform 0.3s ease;
  overflow: hidden;
}

.chat-sidebar-header {
  padding: 16px;
  border-bottom: 1px solid var(--chat-border);
}

.chat-new-btn {
  width: 100%;
  padding: 10px 16px;
  background: var(--brand-blue);
  color: var(--chat-msg-user-text);
  border: none;
  border-radius: 10px;
  font-size: 14px;
  font-weight: 600;
  cursor: pointer;
  transition: background 0.2s;
}
.chat-new-btn:hover { background: var(--brand-blue-deep); }

.chat-sidebar-search {
  margin-top: 10px;
  width: 100%;
  padding: 8px 12px;
  border: 1px solid var(--chat-input-border);
  border-radius: 8px;
  font-size: 13px;
  background: var(--chat-bg);
  color: var(--chat-text);
  outline: none;
  transition: border-color 0.2s;
}
.chat-sidebar-search:focus { border-color: var(--chat-accent); }

.chat-conversations {
  flex: 1;
  overflow-y: auto;
  padding: 8px;
}

.chat-sidebar-footer {
  padding: 12px 16px;
  border-top: 1px solid var(--chat-border);
  display: flex;
  justify-content: flex-end;
}

.chat-dark-toggle {
  border: 1px solid var(--chat-border);
  background: var(--chat-sidebar-bg);
  border-radius: 20px;
  padding: 6px 10px;
  font-size: 16px;
  cursor: pointer;
  color: var(--chat-text);
  line-height: 1;
  transition: border-color 0.2s;
}
.chat-dark-toggle:hover { border-color: var(--chat-accent); }

.chat-date-group { margin-bottom: 8px; }

.chat-date-label {
  font-size: 11px;
  font-weight: 700;
  color: var(--chat-text-sub);
  padding: 8px 12px 4px;
  text-transform: uppercase;
}

.chat-conv-item {
  padding: 10px 12px;
  border-radius: 8px;
  cursor: pointer;
  font-size: 13px;
  color: var(--chat-text);
  transition: background 0.15s;
  display: flex;
  justify-content: space-between;
  align-items: center;
}
.chat-conv-item:hover { background: var(--brand-blue-soft); }
[data-theme="dark"] .chat-conv-item:hover { background: rgba(74, 122, 255, 0.1); }

.chat-conv-item.active {
  background: var(--brand-blue);
  color: var(--chat-msg-user-text);
  border-left: 3px solid var(--brand-pink);
}

.chat-conv-item .chat-conv-delete {
  opacity: 0;
  border: none;
  background: none;
  color: var(--brand-pink);
  cursor: pointer;
  font-size: 14px;
  padding: 2px 4px;
  border-radius: 4px;
}
.chat-conv-item:hover .chat-conv-delete { opacity: 1; }
.chat-conv-item.active .chat-conv-delete { color: #ffa0a0; opacity: 1; }

.chat-sidebar-overlay {
  display: none;
  position: fixed;
  inset: 0;
  background: rgba(0, 0, 0, 0.3);
  z-index: 999;
}
.chat-sidebar-overlay.open { display: block; }

/* ==================== 3. Main Chat Area ============================ */
.chat-main {
  flex: 1;
  display: flex;
  flex-direction: column;
  min-width: 0;
  background: var(--chat-bg);
}

/* ==================== 4. Chat Header =============================== */
.chat-header {
  padding: 12px 20px;
  padding-top: max(12px, env(safe-area-inset-top));
  border-bottom: 1px solid var(--chat-border);
  display: flex;
  align-items: center;
  gap: 12px;
  background: var(--chat-bg);
  flex-shrink: 0;
}

.chat-menu-btn {
  display: none;
  border: none;
  background: none;
  font-size: 22px;
  cursor: pointer;
  color: var(--chat-text);
  padding: 4px;
  line-height: 1;
}

.chat-model-select {
  padding: 6px 14px;
  border: 1px solid var(--chat-border);
  border-radius: 20px;
  font-size: 13px;
  font-weight: 600;
  color: var(--brand-blue);
  background: var(--chat-sidebar-bg);
  cursor: pointer;
  outline: none;
}

.chat-status {
  font-size: 12px;
  color: var(--chat-text-sub);
  display: flex;
  align-items: center;
  gap: 6px;
  margin-left: auto;
  white-space: nowrap;
}

.chat-status-dot {
  width: 8px;
  height: 8px;
  border-radius: 50%;
  background: #ccc;
  flex-shrink: 0;
}
.chat-status-dot.connected { background: #1f7a4c; }
.chat-status-dot.connecting { background: #c4a000; animation: pulse 1s infinite; }
.chat-status-dot.error { background: var(--brand-pink); }

.chat-usage {
  color: var(--chat-accent);
  font-weight: 600;
  font-size: 11px;
}

.chat-latency {
  color: var(--lobster-orange);
  font-weight: 500;
  font-size: 11px;
}

/* ==================== 5. Messages Area ============================== */
.chat-messages {
  flex: 1;
  overflow-y: auto;
  padding: 20px;
  scroll-behavior: smooth;
}

/* ==================== 6. Message Bubbles ============================ */
.chat-msg {
  display: flex;
  gap: 10px;
  margin-bottom: 16px;
  max-width: 780px;
  margin-left: auto;
  margin-right: auto;
  animation: fade-in-msg 0.25s ease-out;
}

.chat-msg-user { flex-direction: row-reverse; }

.chat-msg-avatar {
  width: 32px;
  height: 32px;
  border-radius: 50%;
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 16px;
  flex-shrink: 0;
}
.chat-msg-avatar.user-avatar { background: var(--brand-blue-soft); }
.chat-msg-avatar.assistant-avatar { background: var(--brand-pink-soft); }

.chat-msg-bubble {
  max-width: 75%;
  padding: 10px 14px;
  border-radius: var(--chat-radius);
  line-height: 1.6;
  font-size: 14px;
  word-break: break-word;
}

.chat-msg-user .chat-msg-bubble {
  background: var(--chat-msg-user-bg);
  color: var(--chat-msg-user-text);
  border-bottom-right-radius: 4px;
}

.chat-msg-assistant .chat-msg-bubble {
  background: var(--chat-msg-assistant-bg);
  color: var(--chat-msg-assistant-text);
  border: 1px solid var(--chat-border);
  border-bottom-left-radius: 4px;
}

.chat-msg-meta {
  display: flex;
  align-items: center;
  gap: 8px;
  margin-top: 4px;
}

.chat-msg-time {
  font-size: 11px;
  color: var(--chat-text-sub);
}

.chat-msg-actions {
  display: flex;
  gap: 4px;
  opacity: 0;
  transition: opacity 0.15s;
}
.chat-msg:hover .chat-msg-actions { opacity: 1; }

.chat-msg-act {
  border: none;
  background: var(--chat-sidebar-bg);
  border-radius: 6px;
  font-size: 13px;
  cursor: pointer;
  padding: 2px 6px;
  display: flex;
  align-items: center;
  justify-content: center;
  color: var(--chat-text-sub);
  transition: background 0.15s, color 0.15s;
  box-shadow: 0 1px 3px rgba(0, 0, 0, 0.08);
}
.chat-msg-act:hover {
  background: var(--brand-blue-soft);
  color: var(--brand-blue);
}
[data-theme="dark"] .chat-msg-act:hover {
  background: rgba(74, 122, 255, 0.15);
  color: var(--chat-accent);
}

/* User inline image */
.chat-inline-img {
  max-width: 260px;
  max-height: 200px;
  border-radius: 8px;
  margin-top: 6px;
  display: block;
}

/* Typing Indicator */
.chat-typing-indicator {
  display: inline-flex;
  gap: 4px;
  padding: 2px 0;
}
.chat-typing-indicator span {
  width: 6px;
  height: 6px;
  border-radius: 50%;
  background: var(--lobster-orange);
  animation: typing-bounce 1.4s infinite;
}
.chat-typing-indicator span:nth-child(2) { animation-delay: 0.2s; }
.chat-typing-indicator span:nth-child(3) { animation-delay: 0.4s; }

/* ==================== 7. Link Preview Card ========================= */
.chat-link-card {
  display: flex;
  align-items: flex-start;
  border: 1px solid var(--chat-border);
  border-radius: 10px;
  overflow: hidden;
  background: var(--chat-sidebar-bg);
  text-decoration: none;
  color: var(--chat-text);
  transition: transform 0.15s, box-shadow 0.15s;
  margin-top: 8px;
  max-width: 380px;
}
.chat-link-card:hover {
  transform: translateY(-1px);
  box-shadow: 0 2px 8px rgba(0, 0, 0, 0.06);
}
[data-theme="dark"] .chat-link-card:hover {
  box-shadow: 0 2px 8px rgba(0, 0, 0, 0.25);
}

.chat-link-card-img {
  width: 80px;
  min-height: 80px;
  flex-shrink: 0;
  overflow: hidden;
  background: var(--chat-border);
}
.chat-link-card-img img {
  width: 100%;
  height: 100%;
  object-fit: cover;
}

.chat-link-card-body {
  padding: 8px 10px;
  flex: 1;
  min-width: 0;
}

.chat-link-card-title {
  font-size: 13px;
  font-weight: 600;
  color: var(--chat-accent);
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.chat-link-card-desc {
  font-size: 12px;
  color: var(--chat-text-sub);
  display: -webkit-box;
  -webkit-line-clamp: 2;
  -webkit-box-orient: vertical;
  overflow: hidden;
  margin-top: 2px;
}

.chat-link-card-url {
  font-size: 11px;
  color: var(--chat-text-sub);
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
  margin-top: 4px;
}

/* ==================== 8. Slash Command Bar ========================= */
.chat-slash-bar {
  display: flex;
  gap: 6px;
  padding: 6px 20px;
  border-top: 1px solid var(--chat-border);
  background: var(--chat-bg);
  overflow-x: auto;
  scrollbar-width: thin;
  flex-shrink: 0;
}

.chat-slash-btn {
  padding: 4px 12px;
  border: 1px solid var(--chat-border);
  background: var(--chat-sidebar-bg);
  border-radius: 16px;
  font-size: 12px;
  font-weight: 500;
  color: var(--chat-text-sub);
  cursor: pointer;
  white-space: nowrap;
  transition: border-color 0.15s, color 0.15s, background 0.15s;
}
.chat-slash-btn:hover {
  border-color: var(--chat-accent);
  color: var(--chat-accent);
  background: var(--brand-blue-soft);
}
[data-theme="dark"] .chat-slash-btn:hover {
  background: rgba(74, 122, 255, 0.1);
}

.chat-slash-add {
  color: var(--lobster-orange);
  border-color: var(--lobster-orange);
}
.chat-slash-add:hover {
  background: var(--lobster-orange);
  color: #fff;
  border-color: var(--lobster-orange);
}

/* ==================== 9. Attachment Preview ======================== */
.chat-attachment-preview {
  display: flex;
  gap: 8px;
  padding: 8px 20px;
  background: var(--chat-bg);
  overflow-x: auto;
}

.chat-attachment-thumb {
  position: relative;
  width: 72px;
  height: 72px;
  flex-shrink: 0;
  border-radius: 8px;
  overflow: hidden;
  border: 1px solid var(--chat-border);
  background: var(--chat-sidebar-bg);
}
.chat-attachment-thumb img {
  width: 100%;
  height: 100%;
  object-fit: cover;
}
.chat-attachment-remove {
  position: absolute;
  top: 2px;
  right: 2px;
  width: 20px;
  height: 20px;
  border-radius: 50%;
  border: none;
  background: rgba(0, 0, 0, 0.55);
  color: #fff;
  font-size: 12px;
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: center;
  transition: background 0.15s;
}
.chat-attachment-remove:hover { background: rgba(0, 0, 0, 0.8); }

/* ==================== 10. Input Area =============================== */
.chat-input-area {
  padding: 12px 20px 16px;
  padding-bottom: max(16px, env(safe-area-inset-bottom));
  border-top: 1px solid var(--chat-border);
  background: var(--chat-bg);
  flex-shrink: 0;
}

.chat-input-row {
  display: flex;
  align-items: flex-end;
  gap: 8px;
  max-width: 780px;
  margin: 0 auto;
}

.chat-action-btn {
  width: 38px;
  height: 38px;
  border: 1px solid var(--chat-border);
  background: var(--chat-sidebar-bg);
  border-radius: 50%;
  font-size: 16px;
  cursor: pointer;
  flex-shrink: 0;
  display: flex;
  align-items: center;
  justify-content: center;
  transition: background 0.15s, color 0.15s, border-color 0.15s;
}
.chat-action-btn:hover {
  background: var(--brand-blue-soft);
  color: var(--brand-blue);
  border-color: var(--brand-blue);
}
[data-theme="dark"] .chat-action-btn:hover {
  background: rgba(74, 122, 255, 0.1);
  color: var(--chat-accent);
  border-color: var(--chat-accent);
}

.chat-action-btn.recording {
  background: #dc3232;
  color: #fff;
  border-color: #dc3232;
  animation: pulse-recording 1.5s infinite;
}

.chat-input {
  flex: 1;
  padding: 10px 14px;
  border: 1px solid var(--chat-input-border);
  border-radius: 12px;
  font-size: 14px;
  font-family: inherit;
  resize: none;
  min-height: 42px;
  max-height: 150px;
  line-height: 1.5;
  outline: none;
  background: var(--chat-input-bg);
  color: var(--chat-text);
  transition: border-color 0.2s;
}
.chat-input:focus { border-color: var(--chat-accent); }

.chat-send-btn {
  width: 42px;
  height: 42px;
  border: none;
  background: var(--brand-blue);
  color: var(--chat-msg-user-text);
  border-radius: 50%;
  font-size: 18px;
  cursor: pointer;
  flex-shrink: 0;
  display: flex;
  align-items: center;
  justify-content: center;
  transition: background 0.2s, transform 0.15s;
}
.chat-send-btn:hover { background: var(--brand-blue-deep); transform: scale(1.05); }
.chat-send-btn:disabled {
  background: var(--chat-border);
  color: var(--chat-text-sub);
  cursor: not-allowed;
  transform: none;
}

.chat-input-hint {
  font-size: 11px;
  color: var(--chat-text-sub);
  margin-top: 4px;
  max-width: 780px;
  margin-left: auto;
  margin-right: auto;
}

/* ==================== 11. Toast Notifications ====================== */
.chat-toast-container {
  position: fixed;
  bottom: 80px;
  left: 50%;
  transform: translateX(-50%);
  z-index: 10000;
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 8px;
  pointer-events: none;
}

.chat-toast {
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 10px 20px;
  border-radius: 50px;
  background: var(--chat-sidebar-bg);
  color: var(--chat-text);
  font-size: 13px;
  font-weight: 500;
  box-shadow: 0 4px 16px rgba(0, 0, 0, 0.12);
  animation: slide-in-toast 0.3s ease-out;
  pointer-events: auto;
  white-space: nowrap;
  max-width: 90vw;
  overflow: hidden;
  text-overflow: ellipsis;
}

.chat-toast-info { border-left: 4px solid var(--brand-blue); }
.chat-toast-success { border-left: 4px solid #1f7a4c; }
.chat-toast-warning { border-left: 4px solid var(--lobster-orange); }
.chat-toast-error { border-left: 4px solid #dc3232; }

.chat-toast.fade-out { opacity: 0; transition: opacity 0.4s; }

/* ==================== 12. Welcome Screen =========================== */
.chat-welcome {
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  height: 100%;
  gap: 16px;
  color: var(--chat-text-sub);
  text-align: center;
}

.chat-welcome-icon { font-size: 56px; }

.chat-welcome h2 {
  font-size: 22px;
  color: var(--chat-text);
  margin: 0;
}

.chat-welcome-prompts {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 10px;
  max-width: 500px;
  margin-top: 12px;
}

.chat-prompt-chip {
  padding: 12px 16px;
  background: var(--chat-sidebar-bg);
  border: 1px solid var(--chat-border);
  border-radius: 10px;
  font-size: 13px;
  color: var(--chat-text);
  cursor: pointer;
  text-align: left;
  transition: all 0.2s;
}
.chat-prompt-chip:hover {
  border-color: var(--chat-accent);
  background: var(--brand-blue-soft);
}
[data-theme="dark"] .chat-prompt-chip:hover {
  background: rgba(74, 122, 255, 0.1);
}

/* ==================== 13. Markdown Styles =========================== */
.chat-msg-bubble .md-code-block {
  border-radius: 8px;
  overflow: hidden;
  margin: 8px 0;
  border: 1px solid var(--chat-border);
}

.chat-msg-bubble .md-code-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 6px 12px;
  background: var(--brand-blue);
  color: #fff;
  font-size: 12px;
}

.chat-msg-bubble .md-code-copy {
  border: none;
  background: rgba(255, 255, 255, 0.2);
  color: #fff;
  padding: 2px 8px;
  border-radius: 4px;
  font-size: 11px;
  cursor: pointer;
}
.chat-msg-bubble .md-code-copy:hover { background: rgba(255, 255, 255, 0.35); }

.chat-msg-bubble pre {
  margin: 0;
  padding: 12px;
  background: #1e1e2e;
  color: #cdd6f4;
  font-size: 13px;
  overflow-x: auto;
  line-height: 1.5;
}

.chat-msg-bubble .md-inline-code {
  background: var(--brand-blue-soft);
  padding: 1px 6px;
  border-radius: 4px;
  font-size: 13px;
  font-family: ui-monospace, SFMono-Regular, Menlo, monospace;
}
[data-theme="dark"] .chat-msg-bubble .md-inline-code {
  background: rgba(74, 122, 255, 0.12);
}

.chat-msg-bubble .md-table {
  width: 100%;
  border-collapse: collapse;
  margin: 8px 0;
  font-size: 13px;
}
.chat-msg-bubble .md-table th,
.chat-msg-bubble .md-table td {
  border: 1px solid var(--chat-border);
  padding: 6px 10px;
  text-align: left;
}
.chat-msg-bubble .md-table th {
  background: var(--chat-sidebar-bg);
  font-weight: 600;
}

.chat-msg-bubble .md-blockquote {
  border-left: 3px solid var(--chat-accent);
  padding: 4px 12px;
  margin: 8px 0;
  color: var(--chat-text-sub);
  background: var(--chat-sidebar-bg);
  border-radius: 0 8px 8px 0;
}

.chat-msg-bubble .md-heading { margin: 12px 0 4px; }
.chat-msg-bubble .md-list { padding-left: 20px; margin: 4px 0; }
.chat-msg-bubble .md-hr { border: none; border-top: 1px solid var(--chat-border); margin: 12px 0; }
.chat-msg-bubble .md-paragraph { margin: 4px 0; }

/* ==================== 14. Mobile Responsive (<768px) =============== */
@media (max-width: 768px) {
  .chat-sidebar {
    position: fixed;
    left: 0;
    top: 0;
    bottom: 0;
    z-index: 1000;
    transform: translateX(-100%);
    box-shadow: 4px 0 20px rgba(0, 0, 0, 0.15);
  }
  .chat-sidebar.open { transform: translateX(0); }

  .chat-menu-btn { display: block; }

  .chat-msg { max-width: 100%; }
  .chat-msg-bubble { max-width: 88%; }

  .chat-slash-bar {
    scroll-snap-type: x mandatory;
    -webkit-overflow-scrolling: touch;
    padding: 6px 12px;
  }
  .chat-slash-btn { scroll-snap-align: start; }

  .chat-header {
    padding: 10px 12px;
    gap: 8px;
  }
  .chat-status { font-size: 10px; gap: 4px; }

  .chat-welcome-prompts { grid-template-columns: 1fr; }

  .chat-input-area { padding: 10px 12px; }

  .chat-link-card { max-width: 280px; }
  .chat-inline-img { max-width: 200px; }

  .chat-toast-container { bottom: 16px; }
}
