/* =========================================================================
   VisioMind AI Hub — Hệ thống thiết kế & giao diện Chat
   Thẩm mỹ "Ink & Signal": nền mực sẫm, một điểm nhấn tím điện (accent) dùng
   tiết chế cho các trạng thái sống động. Không phụ thuộc framework CSS.
   ========================================================================= */

/* ---------- Biến chủ đề (Dark mặc định) ---------- */
:root {
  --bg:            #0e1016;
  --bg-elev:       #15181f;
  --surface:       #1c2029;
  --surface-hover: #232833;
  --border:        rgba(255, 255, 255, 0.08);
  --border-strong: rgba(255, 255, 255, 0.14);
  --text:          #e8eaed;
  --text-muted:    #9aa0aa;
  --text-faint:    #6b7280;

  --accent:        #7b6cf6;
  --accent-hover:  #8f82f8;
  --accent-soft:   rgba(123, 108, 246, 0.14);
  --danger:        #ef4444;
  --success:       #22c55e;
  --warning:       #f59e0b;

  --user-bubble:   rgba(123, 108, 246, 0.13);

  --radius:        14px;
  --radius-sm:     9px;
  --sidebar-w:     272px;

  --font: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, "Helvetica Neue",
          Arial, "Noto Sans", sans-serif;
  --mono: "SF Mono", "JetBrains Mono", "Fira Code", ui-monospace, Menlo, Consolas, monospace;

  --shadow: 0 12px 32px rgba(0, 0, 0, 0.35);
  --ease: cubic-bezier(0.22, 1, 0.36, 1);
}

/* ---------- Light mode ---------- */
[data-theme="light"] {
  --bg:            #f6f6f8;
  --bg-elev:       #ffffff;
  --surface:       #ffffff;
  --surface-hover: #f0f0f3;
  --border:        rgba(0, 0, 0, 0.09);
  --border-strong: rgba(0, 0, 0, 0.16);
  --text:          #16181d;
  --text-muted:    #5c626d;
  --text-faint:    #8b909a;
  --accent:        #6d5eff;
  --accent-hover:  #5a49f5;
  --accent-soft:   rgba(109, 94, 255, 0.10);
  --user-bubble:   rgba(109, 94, 255, 0.09);
  --shadow: 0 12px 32px rgba(0, 0, 0, 0.12);
}

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

html, body { height: 100%; }

body {
  font-family: var(--font);
  background: var(--bg);
  color: var(--text);
  font-size: 15px;
  line-height: 1.6;
  -webkit-font-smoothing: antialiased;
  overflow: hidden;
}

button { font-family: inherit; cursor: pointer; border: none; background: none; color: inherit; }
input, textarea { font-family: inherit; }
a { color: var(--accent); }

/* Scrollbar tinh gọn */
::-webkit-scrollbar { width: 10px; height: 10px; }
::-webkit-scrollbar-thumb { background: var(--border-strong); border-radius: 8px; border: 3px solid transparent; background-clip: padding-box; }
::-webkit-scrollbar-thumb:hover { background: var(--text-faint); background-clip: padding-box; }

/* =========================================================================
   Bố cục chính
   ========================================================================= */
.layout {
  display: flex;
  height: 100vh;            /* dự phòng cho trình duyệt cũ */
  height: 100dvh;           /* CHIỀU CAO ĐỘNG: tự trừ đi bàn phím trên di động */
  height: calc(100dvh - var(--kb, 0px));  /* dự phòng thêm: JS đo bàn phím (Safari cũ) */
}

/* ---------- Sidebar ---------- */
.sidebar {
  width: var(--sidebar-w);
  flex-shrink: 0;
  background: var(--bg-elev);
  border-right: 1px solid var(--border);
  display: flex;
  flex-direction: column;
  transition: width 0.28s var(--ease), transform 0.28s var(--ease);
  overflow: hidden;
}
.sidebar.collapsed { width: 0; border-right: none; }

.sidebar-head {
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 16px 16px 12px;
}

/* Dấu hiệu thương hiệu: chấm "node" nhấp nháy báo AI cục bộ đang sống */
.brand { display: flex; align-items: center; gap: 9px; font-weight: 650; letter-spacing: -0.3px; font-size: 16px; }
.brand-node {
  width: 9px; height: 9px; border-radius: 50%;
  background: var(--accent);
  box-shadow: 0 0 0 0 var(--accent);
  animation: pulse 2.6s var(--ease) infinite;
}
@keyframes pulse {
  0%   { box-shadow: 0 0 0 0 rgba(123, 108, 246, 0.5); }
  70%  { box-shadow: 0 0 0 7px rgba(123, 108, 246, 0); }
  100% { box-shadow: 0 0 0 0 rgba(123, 108, 246, 0); }
}

.new-chat-btn {
  margin: 4px 12px 12px;
  padding: 11px 14px;
  display: flex; align-items: center; gap: 9px;
  background: var(--accent);
  color: #fff;
  border-radius: var(--radius-sm);
  font-weight: 550;
  font-size: 14px;
  transition: background 0.15s, transform 0.1s;
}
.new-chat-btn:hover { background: var(--accent-hover); }
.new-chat-btn:active { transform: scale(0.98); }
.new-chat-btn svg { width: 17px; height: 17px; }

.conv-list { flex: 1; overflow-y: auto; padding: 0 8px; }
.conv-list-label { font-size: 11px; text-transform: uppercase; letter-spacing: 0.6px; color: var(--text-faint); padding: 8px 10px 6px; }

.conv-item {
  display: flex; align-items: center; gap: 8px;
  padding: 9px 10px;
  border-radius: var(--radius-sm);
  color: var(--text-muted);
  font-size: 14px;
  cursor: pointer;
  transition: background 0.13s, color 0.13s;
  position: relative;
}
.conv-item:hover { background: var(--surface-hover); color: var(--text); }
.conv-item.active { background: var(--accent-soft); color: var(--text); }
.conv-item .title { flex: 1; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.conv-item .actions { display: none; gap: 2px; }
.conv-item:hover .actions, .conv-item.active .actions { display: flex; }
.icon-btn {
  width: 26px; height: 26px; display: grid; place-items: center;
  border-radius: 6px; color: var(--text-faint);
  transition: background 0.12s, color 0.12s;
}
.icon-btn:hover { background: var(--border-strong); color: var(--text); }
.icon-btn.danger:hover { color: var(--danger); }
.icon-btn svg { width: 15px; height: 15px; }

.sidebar-foot { border-top: 1px solid var(--border); padding: 10px 12px; }
.user-chip {
  display: flex; align-items: center; gap: 10px;
  padding: 8px; border-radius: var(--radius-sm);
  transition: background 0.13s;
}
.user-chip:hover { background: var(--surface-hover); }
.avatar {
  width: 30px; height: 30px; border-radius: 50%;
  background: linear-gradient(135deg, var(--accent), #b5a6ff);
  display: grid; place-items: center; color: #fff; font-weight: 600; font-size: 13px;
  flex-shrink: 0;
}
.user-chip .meta { flex: 1; overflow: hidden; }
.user-chip .name { font-size: 13.5px; font-weight: 550; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.user-chip .role { font-size: 11.5px; color: var(--text-faint); }

/* =========================================================================
   Khu vực chat chính
   ========================================================================= */
.main { flex: 1; display: flex; flex-direction: column; min-width: 0; }

.topbar {
  height: 56px; flex-shrink: 0;
  display: flex; align-items: center; gap: 12px;
  padding: 0 16px;
  border-bottom: 1px solid var(--border);
}
.topbar .toggle-sidebar { display: grid; }

/* Bộ chọn model */
.model-select {
  position: relative;
}
.model-select-btn {
  display: flex; align-items: center; gap: 8px;
  padding: 7px 12px;
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  font-size: 14px; font-weight: 550;
  transition: background 0.13s, border-color 0.13s;
}
.model-select-btn:hover { background: var(--surface-hover); border-color: var(--border-strong); }
.model-select-btn .dot { width: 7px; height: 7px; border-radius: 50%; background: var(--success); }
.model-select-btn .dot.off { background: var(--text-faint); }
.model-select-btn svg { width: 15px; height: 15px; color: var(--text-muted); }

.model-menu {
  position: absolute; top: calc(100% + 6px); left: 0;
  min-width: 240px; max-height: 320px; overflow-y: auto;
  background: var(--surface);
  border: 1px solid var(--border-strong);
  border-radius: var(--radius);
  box-shadow: var(--shadow);
  padding: 6px;
  z-index: 40;
  opacity: 0; transform: translateY(-6px); pointer-events: none;
  transition: opacity 0.16s, transform 0.16s;
}
.model-menu.open { opacity: 1; transform: translateY(0); pointer-events: auto; }
.model-option {
  display: flex; align-items: center; gap: 9px;
  padding: 9px 10px; border-radius: var(--radius-sm);
  font-size: 14px; cursor: pointer;
}
.model-option:hover { background: var(--surface-hover); }
.model-option.selected { color: var(--accent); font-weight: 600; }
.model-option .check { margin-left: auto; opacity: 0; }
.model-option.selected .check { opacity: 1; }
.model-menu .empty-note { padding: 12px 10px; font-size: 13px; color: var(--text-faint); }

.topbar-spacer { flex: 1; }
.topbar-actions { display: flex; align-items: center; gap: 4px; }

/* Vùng cuộn tin nhắn */
.messages { flex: 1; overflow-y: auto; scroll-behavior: smooth; }
.messages-inner { max-width: 760px; margin: 0 auto; padding: 28px 20px 12px; }

/* Trạng thái rỗng */
.empty-state {
  height: 100%; display: flex; flex-direction: column;
  align-items: center; justify-content: center; text-align: center;
  padding: 40px 20px; gap: 14px;
}
.empty-logo {
  width: 54px; height: 54px; border-radius: 16px;
  background: var(--accent-soft);
  display: grid; place-items: center;
}
.empty-logo .brand-node { width: 16px; height: 16px; }
.empty-state h1 { font-size: 24px; font-weight: 650; letter-spacing: -0.5px; }
.empty-state p { color: var(--text-muted); max-width: 420px; }
.suggestions { display: grid; grid-template-columns: 1fr 1fr; gap: 10px; margin-top: 12px; width: 100%; max-width: 460px; }
.suggestion {
  text-align: left; padding: 13px 15px;
  border: 1px solid var(--border); border-radius: var(--radius);
  font-size: 13.5px; color: var(--text-muted);
  transition: border-color 0.14s, background 0.14s, color 0.14s;
}
.suggestion:hover { border-color: var(--accent); background: var(--accent-soft); color: var(--text); }

/* Tin nhắn */
.msg { display: flex; gap: 14px; padding: 14px 0; }
.msg .msg-avatar {
  width: 30px; height: 30px; border-radius: 8px; flex-shrink: 0;
  display: grid; place-items: center; font-size: 13px; font-weight: 600;
}
.msg.user .msg-avatar { background: var(--user-bubble); color: var(--accent); }
.msg.assistant .msg-avatar { background: var(--accent); color: #fff; }
.msg .msg-body { flex: 1; min-width: 0; padding-top: 3px; }
.msg-images { display: flex; flex-wrap: wrap; gap: 8px; margin-bottom: 8px; }
.msg-img { max-width: 220px; max-height: 220px; border-radius: 12px; border: 1px solid var(--border); object-fit: cover; cursor: pointer; }
.msg .msg-role { font-size: 13px; font-weight: 650; margin-bottom: 3px; }

/* Nội dung markdown đã render */
.md { font-size: 15px; line-height: 1.7; word-wrap: break-word; overflow-wrap: break-word; }
.md > *:first-child { margin-top: 0; }
.md > *:last-child { margin-bottom: 0; }
.md p { margin: 0 0 12px; }
.md h1, .md h2, .md h3, .md h4 { margin: 20px 0 10px; font-weight: 650; line-height: 1.3; }
.md h1 { font-size: 1.5em; } .md h2 { font-size: 1.3em; } .md h3 { font-size: 1.13em; }
.md ul, .md ol { margin: 0 0 12px; padding-left: 24px; }
.md li { margin: 4px 0; }
.md a { text-decoration: underline; text-underline-offset: 2px; }
.md blockquote { border-left: 3px solid var(--accent); padding: 2px 0 2px 14px; margin: 0 0 12px; color: var(--text-muted); }
.md hr { border: none; border-top: 1px solid var(--border); margin: 18px 0; }
.md :not(pre) > code {
  font-family: var(--mono); font-size: 0.88em;
  background: var(--surface); border: 1px solid var(--border);
  padding: 1px 6px; border-radius: 6px;
}
.md table { border-collapse: collapse; width: 100%; margin: 0 0 14px; font-size: 14px; display: block; overflow-x: auto; }
.md th, .md td { border: 1px solid var(--border); padding: 8px 12px; text-align: left; }
.md th { background: var(--surface); font-weight: 600; }

/* Code block + nút Copy */
.code-block {
  margin: 4px 0 16px; border-radius: 12px; overflow: hidden;
  border: 1px solid var(--border); box-shadow: 0 1px 3px rgba(0, 0, 0, 0.18);
}
.code-block-head {
  display: flex; align-items: center; justify-content: space-between;
  padding: 8px 14px; background: var(--bg-elev);
  border-bottom: 1px solid var(--border);
  font-size: 11.5px; color: var(--text-faint); font-family: var(--mono);
  letter-spacing: 0.05em; text-transform: uppercase;
}
.copy-btn {
  display: flex; align-items: center; gap: 5px;
  font-size: 11.5px; color: var(--text-muted);
  font-family: var(--font, system-ui, sans-serif);
  text-transform: none; letter-spacing: 0;
  padding: 4px 9px; border-radius: 7px; border: 1px solid transparent;
  transition: background 0.12s, color 0.12s, border-color 0.12s;
}
.copy-btn:hover { background: var(--surface-hover); color: var(--text); border-color: var(--border); }
.copy-btn svg { width: 13px; height: 13px; }
.copy-btn.copied { color: var(--success); }
.code-block pre { margin: 0; padding: 15px 16px; overflow-x: auto; background: var(--bg); }
.code-block code {
  font-family: var(--mono); font-size: 13px; line-height: 1.7;
  white-space: pre; tab-size: 2; -moz-tab-size: 2; color: var(--code-fg, #abb2bf);
}
/* Thanh cuộn ngang gọn cho code */
.code-block pre::-webkit-scrollbar { height: 9px; }
.code-block pre::-webkit-scrollbar-thumb { background: var(--border-strong); border-radius: 8px; }
.code-block pre::-webkit-scrollbar-thumb:hover { background: var(--text-faint); }
.code-block pre::-webkit-scrollbar-track { background: transparent; }

/* Tô màu cú pháp — bảng màu tối (mặc định) */
.tok-cmt { color: #7d8799; font-style: italic; }
.tok-str { color: #98c379; }
.tok-num { color: #d19a66; }
.tok-kw  { color: #c678dd; }
.tok-lit { color: #56b6c2; }
.tok-fn  { color: #61afef; }

/* Bảng màu sáng */
[data-theme="light"] .code-block code { color: #383a42; }
[data-theme="light"] .tok-cmt { color: #9098a3; }
[data-theme="light"] .tok-str { color: #50a14f; }
[data-theme="light"] .tok-num { color: #b76b01; }
[data-theme="light"] .tok-kw  { color: #a626a4; }
[data-theme="light"] .tok-lit { color: #0184bc; }
[data-theme="light"] .tok-fn  { color: #4078f2; }

/* Con trỏ gõ khi đang stream */
.typing-cursor { display: inline-block; width: 8px; height: 16px; background: var(--accent); border-radius: 2px; vertical-align: text-bottom; animation: blink 1s steps(2) infinite; }
@keyframes blink { 50% { opacity: 0; } }

/* Chấm "đang gõ" trước khi có token đầu tiên */
.typing-dots { display: inline-flex; gap: 4px; padding: 4px 0; }
.typing-dots span { width: 7px; height: 7px; border-radius: 50%; background: var(--text-faint); animation: bounce 1.3s var(--ease) infinite; }
.typing-dots span:nth-child(2) { animation-delay: 0.18s; }
.typing-dots span:nth-child(3) { animation-delay: 0.36s; }
@keyframes bounce { 0%, 60%, 100% { transform: translateY(0); opacity: 0.5; } 30% { transform: translateY(-5px); opacity: 1; } }

/* =========================================================================
   Vùng nhập
   ========================================================================= */
.composer-wrap {
  flex-shrink: 0;
  padding: 8px 20px 18px;
  /* Chừa chỗ cho thanh home / vùng khuyết của điện thoại */
  padding-bottom: calc(18px + env(safe-area-inset-bottom, 0px));
  background: var(--bg);     /* nền đặc: nội dung không "lòi" ra sau ô nhập */
}
.composer {
  max-width: 760px; margin: 0 auto;
  border: 1px solid var(--border-strong);
  border-radius: 20px;
  background: var(--bg-elev);
  padding: 8px 8px 8px 16px;
  transition: border-color 0.15s, box-shadow 0.15s;
}
.composer:focus-within { border-color: var(--accent); box-shadow: 0 0 0 3px var(--accent-soft); }

/* Chip tài liệu đính kèm */
.doc-chip {
  display: none; align-items: center; gap: 8px;
  margin-bottom: 8px; padding: 7px 10px;
  background: var(--accent-soft); border-radius: var(--radius-sm);
  font-size: 13px; color: var(--text);
}
.doc-chip.show { display: inline-flex; }
.doc-chip svg { width: 15px; height: 15px; color: var(--accent); }
.doc-chip .remove { margin-left: 4px; cursor: pointer; color: var(--text-muted); }
.doc-chip .remove:hover { color: var(--danger); }

/* Xem trước ảnh đính kèm trong ô nhập */
.image-previews { display: none; flex-wrap: wrap; gap: 8px; margin-bottom: 8px; }
.image-previews.show { display: flex; }
.img-thumb { position: relative; width: 62px; height: 62px; border-radius: 10px; overflow: hidden; border: 1px solid var(--border-strong); }
.img-thumb img { width: 100%; height: 100%; object-fit: cover; display: block; }
.img-thumb .remove {
  position: absolute; top: 2px; right: 2px; width: 18px; height: 18px;
  display: grid; place-items: center; border-radius: 50%;
  background: rgba(0,0,0,0.6); color: #fff; font-size: 12px; line-height: 1; cursor: pointer;
}
.img-thumb .remove:hover { background: var(--danger); }

.composer-row { display: flex; align-items: flex-end; gap: 8px; }
.composer textarea {
  flex: 1; resize: none; border: none; outline: none; background: none;
  color: var(--text); font-size: 15px; line-height: 1.5;
  max-height: 200px; padding: 8px 0;
}
.composer textarea::placeholder { color: var(--text-faint); }
.attach-btn { width: 36px; height: 36px; border-radius: 50%; display: grid; place-items: center; color: var(--text-muted); transition: background 0.13s, color 0.13s; flex-shrink: 0; }
.attach-btn:hover { background: var(--surface-hover); color: var(--text); }
.attach-btn svg { width: 19px; height: 19px; }
.send-btn {
  width: 36px; height: 36px; border-radius: 50%; flex-shrink: 0;
  background: var(--accent); color: #fff; display: grid; place-items: center;
  transition: background 0.13s, transform 0.1s, opacity 0.13s;
}
.send-btn:hover { background: var(--accent-hover); }
.send-btn:active { transform: scale(0.94); }
.send-btn:disabled { opacity: 0.4; cursor: not-allowed; }
.send-btn svg { width: 18px; height: 18px; }
.composer-hint { max-width: 760px; margin: 8px auto 0; text-align: center; font-size: 11.5px; color: var(--text-faint); }

/* =========================================================================
   Tiện ích chung: nút, toast, menu người dùng
   ========================================================================= */
.ghost-btn {
  display: inline-flex; align-items: center; gap: 7px;
  padding: 8px 12px; border-radius: var(--radius-sm);
  color: var(--text-muted); font-size: 14px;
  transition: background 0.13s, color 0.13s;
}
.ghost-btn:hover { background: var(--surface-hover); color: var(--text); }
.ghost-btn svg { width: 17px; height: 17px; }

/* Menu người dùng */
.user-menu {
  position: absolute; bottom: 62px; left: 12px; right: 12px;
  background: var(--surface); border: 1px solid var(--border-strong);
  border-radius: var(--radius); box-shadow: var(--shadow); padding: 6px;
  opacity: 0; transform: translateY(6px); pointer-events: none;
  transition: opacity 0.16s, transform 0.16s; z-index: 50;
}
.user-menu.open { opacity: 1; transform: translateY(0); pointer-events: auto; }
.user-menu a, .user-menu button {
  display: flex; align-items: center; gap: 9px; width: 100%;
  padding: 9px 10px; border-radius: var(--radius-sm);
  font-size: 14px; color: var(--text); text-decoration: none; text-align: left;
}
.user-menu a:hover, .user-menu button:hover { background: var(--surface-hover); }
.user-menu svg { width: 16px; height: 16px; color: var(--text-muted); }

/* Toast thông báo */
.toast-stack { position: fixed; bottom: 22px; left: 50%; transform: translateX(-50%); display: flex; flex-direction: column; gap: 8px; z-index: 200; }
.toast {
  padding: 11px 16px; border-radius: var(--radius); font-size: 14px;
  background: var(--surface); border: 1px solid var(--border-strong);
  box-shadow: var(--shadow); animation: toast-in 0.25s var(--ease);
  max-width: 340px;
}
.toast.error { border-left: 3px solid var(--danger); }
.toast.success { border-left: 3px solid var(--success); }
@keyframes toast-in { from { opacity: 0; transform: translateY(10px); } }

/* Lớp phủ mobile */
.overlay {
  position: fixed; inset: 0; background: rgba(0,0,0,0.5);
  opacity: 0; pointer-events: none; transition: opacity 0.25s; z-index: 30;
}
.overlay.show { opacity: 1; pointer-events: auto; }

/* =========================================================================
   Responsive
   ========================================================================= */
@media (max-width: 820px) {
  .sidebar {
    position: fixed; top: 0; bottom: 0; left: 0; z-index: 40;
    width: var(--sidebar-w); transform: translateX(-100%); box-shadow: var(--shadow);
  }
  .sidebar.open { transform: translateX(0); }
  .sidebar.collapsed { width: var(--sidebar-w); transform: translateX(-100%); }
  .messages-inner, .composer, .composer-hint { max-width: 100%; }
  .suggestions { grid-template-columns: 1fr; }
  .topbar .toggle-sidebar { display: grid; }
}

/* Tôn trọng người dùng tắt hiệu ứng chuyển động */
@media (prefers-reduced-motion: reduce) {
  *, *::before, *::after { animation-duration: 0.01ms !important; transition-duration: 0.01ms !important; }
}

/* Nhãn node trong menu chọn model */
.model-node { font-size: 10.5px; padding: 1px 6px; border-radius: 999px; background: var(--accent-soft); color: var(--accent); margin-left: 6px; vertical-align: middle; }

/* Focus rõ ràng cho bàn phím */
:focus-visible { outline: 2px solid var(--accent); outline-offset: 2px; }

/* =========================================================================
   Nâng cấp trải nghiệm (tính năng pro)
   ========================================================================= */

/* Tìm kiếm hội thoại */
.conv-search { display: flex; align-items: center; gap: 8px; margin: 0 12px 10px; padding: 8px 11px; background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius-sm); }
.conv-search svg { width: 15px; height: 15px; color: var(--text-faint); flex-shrink: 0; }
.conv-search input { flex: 1; border: none; outline: none; background: none; color: var(--text); font-size: 13.5px; }
.conv-search input::placeholder { color: var(--text-faint); }

/* Hành động trên tin nhắn (copy / regenerate) */
.msg-actions { display: flex; gap: 4px; margin-top: 8px; opacity: 0; transition: opacity 0.14s; }
.msg:hover .msg-actions, .msg-actions.pinned { opacity: 1; }
.msg-action {
  display: inline-flex; align-items: center; gap: 5px;
  padding: 4px 9px; border-radius: 7px; font-size: 12px; color: var(--text-muted);
  transition: background 0.12s, color 0.12s;
}
.msg-action:hover { background: var(--surface-hover); color: var(--text); }
.msg-action svg { width: 13px; height: 13px; }
.msg-action.done { color: var(--success); }

/* Thống kê dưới câu trả lời */
.msg-stats { margin-top: 6px; font-size: 11.5px; color: var(--text-faint); display: flex; gap: 12px; }

/* ============ Khối suy nghĩ của model (reasoning / thinking models) ============
   Triết lý: KHÔNG phải nội dung chính, nên phải LÙI VỀ SAU — chữ mờ, không viền,
   không nền. Đang nghĩ thì mở sẵn và nhấp nháy nhẹ; nghĩ xong thì tự thu gọn. */
.reasoning { margin: 0 0 14px; }

.reasoning > summary {
  display: inline-flex; align-items: center; gap: 7px;
  cursor: pointer; user-select: none; list-style: none;
  padding: 4px 8px; margin-left: -8px; border-radius: 8px;
  font-size: 13px; color: var(--text-faint);
  transition: color 0.15s, background 0.15s;
}
.reasoning > summary::-webkit-details-marker { display: none; }
.reasoning > summary:hover { color: var(--text-muted); background: var(--surface); }

.reasoning .th-icon { width: 14px; height: 14px; flex-shrink: 0; opacity: 0.8; }
.reasoning .th-chev {
  width: 13px; height: 13px; flex-shrink: 0; opacity: 0.6;
  transition: transform 0.2s ease;
}
.reasoning[open] > summary .th-chev { transform: rotate(90deg); }

/* Đang nghĩ: icon xoay nhẹ + chữ nhấp nháy chạy ngang (kiểu shimmer) */
.reasoning.thinking .th-icon { animation: th-pulse 1.6s ease-in-out infinite; }
@keyframes th-pulse {
  0%, 100% { opacity: 0.45; transform: scale(1); }
  50%      { opacity: 1;    transform: scale(1.08); }
}
.th-shimmer {
  background: linear-gradient(
    90deg,
    var(--text-faint) 0%, var(--text-faint) 40%,
    var(--text) 50%,
    var(--text-faint) 60%, var(--text-faint) 100%
  );
  background-size: 200% 100%;
  -webkit-background-clip: text; background-clip: text;
  -webkit-text-fill-color: transparent; color: transparent;
  animation: th-shine 2s linear infinite;
}
@keyframes th-shine {
  0%   { background-position: 100% 0; }
  100% { background-position: -100% 0; }
}

/* Nội dung suy nghĩ: chữ mờ, gạch dọc bên trái — nhìn là biết "phụ" */
.reasoning .reason-body {
  margin: 6px 0 0 6px; padding: 2px 0 2px 14px;
  border-left: 2px solid var(--border);
  font-size: 13px; line-height: 1.7; color: var(--text-faint);
  animation: th-fade 0.22s ease;
}
@keyframes th-fade {
  from { opacity: 0; transform: translateY(-3px); }
  to   { opacity: 1; transform: translateY(0); }
}
.reasoning .reason-body p { margin: 7px 0; }
.reasoning .reason-body p:first-child { margin-top: 0; }
.reasoning .reason-body p:last-child { margin-bottom: 0; }
.reasoning .reason-body code { font-size: 12px; opacity: 0.9; }
.reasoning .reason-body .code-block { margin: 8px 0; opacity: 0.85; }
.reasoning .reason-body h1, .reasoning .reason-body h2, .reasoning .reason-body h3 {
  font-size: 13px; font-weight: 600; color: var(--text-muted); margin: 8px 0 4px;
}

/* Tôn trọng người dùng tắt hiệu ứng chuyển động */
@media (prefers-reduced-motion: reduce) {
  .reasoning.thinking .th-icon,
  .th-shimmer { animation: none; }
  .th-shimmer { -webkit-text-fill-color: var(--text-muted); color: var(--text-muted); }
  .reasoning .reason-body { animation: none; }
}

/* Nút cuộn xuống cuối */
.scroll-bottom {
  position: absolute; left: 50%; transform: translateX(-50%) translateY(8px);
  bottom: 104px; width: 38px; height: 38px; border-radius: 50%;
  background: var(--surface); border: 1px solid var(--border-strong); box-shadow: var(--shadow);
  display: grid; place-items: center; color: var(--text); z-index: 10;
  opacity: 0; pointer-events: none; transition: opacity 0.18s, transform 0.18s;
}
.scroll-bottom.show { opacity: 1; pointer-events: auto; transform: translateX(-50%) translateY(0); }
.scroll-bottom svg { width: 18px; height: 18px; }
.scroll-bottom:hover { background: var(--surface-hover); }

/* Nút gửi khi đang stream -> chế độ Dừng */
.send-btn.stop { background: var(--danger); }
.send-btn.stop:hover { background: #dc2626; }

/* Kéo-thả ảnh vào ô nhập */
.composer.dragover { border-color: var(--accent); box-shadow: 0 0 0 3px var(--accent-soft); }
.composer.dragover::after { content: "Thả ảnh vào đây"; position: absolute; }

/* Lightbox xem ảnh */
.lightbox {
  position: fixed; inset: 0; z-index: 300; background: rgba(0,0,0,0.85);
  display: none; align-items: center; justify-content: center; padding: 32px; cursor: zoom-out;
}
.lightbox.show { display: flex; }
.lightbox img { max-width: 92vw; max-height: 92vh; border-radius: 10px; box-shadow: 0 20px 60px rgba(0,0,0,0.5); }

/* Con trỏ zoom cho ảnh trong tin nhắn */
.msg-img { cursor: zoom-in; }

@media (max-width: 820px) {
  .scroll-bottom { bottom: 96px; }
}

/* ---------- Nguồn trích dẫn từ Cơ sở tri thức (RAG) ---------- */
.msg-sources {
  display: flex; flex-wrap: wrap; align-items: center; gap: 6px;
  margin-top: 12px; padding: 9px 11px;
  background: var(--surface); border: 1px solid var(--border);
  border-left: 3px solid var(--accent); border-radius: 8px;
}
.msg-sources .src-label {
  width: 100%; font-size: 11.5px; font-weight: 600; color: var(--text-muted);
  text-transform: uppercase; letter-spacing: 0.04em; margin-bottom: 2px;
}
.msg-sources .src-item {
  display: inline-flex; align-items: center; gap: 5px;
  font-size: 12px; color: var(--text-muted);
  background: var(--bg-elev); border: 1px solid var(--border);
  padding: 3px 9px; border-radius: 999px; max-width: 100%;
  overflow: hidden; text-overflow: ellipsis; white-space: nowrap;
}
.msg-sources .src-item b { color: var(--accent); font-weight: 600; }

/* Nguồn là ẢNH (visual RAG): thumbnail bấm để phóng to */
.msg-sources .src-item.is-img { padding-left: 4px; }
.msg-sources .src-thumb {
  width: 26px; height: 26px; object-fit: cover; border-radius: 5px;
  border: 1px solid var(--border); cursor: zoom-in; opacity: 0;
  transition: opacity 0.2s, transform 0.12s;
}
.msg-sources .src-thumb.loaded { opacity: 1; }
.msg-sources .src-thumb:hover { transform: scale(1.08); border-color: var(--accent); }

/* ===================== Modal (Bộ nhớ / Tri thức của tôi) ===================== */
.modal {
  position: fixed; inset: 0; z-index: 200; display: none;
  background: rgba(0, 0, 0, 0.55); backdrop-filter: blur(3px);
  align-items: center; justify-content: center; padding: 20px;
}
.modal.open { display: flex; }
.modal-box {
  background: var(--bg-elev); border: 1px solid var(--border);
  border-radius: 16px; width: min(760px, 100%); max-height: 86vh;
  overflow-y: auto; padding: 20px 22px; box-shadow: 0 24px 60px rgba(0,0,0,0.45);
}
.modal-head { display: flex; align-items: center; justify-content: space-between; margin-bottom: 14px; }
.modal-head h3 { font-size: 17px; font-weight: 650; }
.modal-close {
  width: 30px; height: 30px; border-radius: 8px; color: var(--text-muted);
  font-size: 15px; transition: background 0.12s, color 0.12s;
}
.modal-close:hover { background: var(--surface-hover); color: var(--text); }

.modal-bar {
  display: flex; align-items: center; justify-content: space-between; gap: 12px;
  flex-wrap: wrap; padding: 10px 12px; margin-bottom: 12px;
  background: var(--surface); border: 1px solid var(--border); border-radius: 10px;
}
.modal-bar-right { display: flex; gap: 6px; flex-wrap: wrap; }
.switch { display: flex; align-items: center; gap: 8px; font-size: 13.5px; cursor: pointer; }
.switch input { width: 16px; height: 16px; accent-color: var(--accent); cursor: pointer; }
.chip-btn.danger { color: var(--danger); }

.mem-hint { font-size: 12.5px; color: var(--text-faint); margin-bottom: 12px; line-height: 1.5; }

.mem-add { display: flex; gap: 8px; margin-bottom: 14px; flex-wrap: wrap; }
.mem-add select, .mem-add input {
  padding: 9px 11px; border-radius: 9px; border: 1px solid var(--border);
  background: var(--surface); color: var(--text); font-size: 13px; outline: none;
}
.mem-add select { flex: 0 0 140px; }
.mem-add input { flex: 1; min-width: 120px; }
.mem-add select:focus, .mem-add input:focus { border-color: var(--accent); }

.mem-list { display: flex; flex-direction: column; gap: 6px; }
.mem-list .empty { color: var(--text-faint); font-size: 13.5px; padding: 18px 4px; line-height: 1.6; }
.mem-row {
  display: grid; grid-template-columns: 96px 1fr auto auto; gap: 10px; align-items: center;
  padding: 9px 11px; background: var(--surface);
  border: 1px solid var(--border); border-radius: 10px;
}
.mem-row.pinned { border-color: var(--accent); background: var(--accent-soft); }
.mem-cat {
  font-size: 11px; font-weight: 600; color: var(--accent);
  background: var(--accent-soft); padding: 3px 8px; border-radius: 999px; text-align: center;
}
.mem-body { min-width: 0; }
.mem-key { font-size: 11.5px; color: var(--text-faint); margin-bottom: 2px; }
.mem-sub { font-size: 11.5px; color: var(--text-faint); }
.mem-val {
  width: 100%; background: transparent; border: 1px solid transparent; color: var(--text);
  font-size: 13.5px; padding: 3px 5px; border-radius: 6px; outline: none;
}
.mem-val:hover { border-color: var(--border); }
.mem-val:focus { border-color: var(--accent); background: var(--bg); }
.mem-imp { font-size: 11px; color: var(--warning, #f0a500); letter-spacing: -1px; white-space: nowrap; }
.mem-acts { display: flex; gap: 5px; }

@media (max-width: 640px) {
  .mem-row { grid-template-columns: 1fr auto; }
  .mem-cat, .mem-imp { grid-column: 1; justify-self: start; }
}

/* ===================== Nút & form dùng trong modal =====================
   Các class này vốn chỉ có trong admin.css. Trang chat chỉ nạp style.css,
   nên phải định nghĩa lại ở đây, nếu không modal sẽ mất hoàn toàn bố cục
   (input/textarea về mặc định inline-block => nằm cạnh nhau). */
.chip-btn {
  display: inline-flex; align-items: center; gap: 6px;
  padding: 7px 12px; font-size: 12.5px; font-weight: 500;
  color: var(--text-muted); background: var(--surface);
  border: 1px solid var(--border); border-radius: 9px;
  cursor: pointer; transition: background 0.12s, color 0.12s, border-color 0.12s;
}
.chip-btn:hover { background: var(--surface-hover); color: var(--text); border-color: var(--border-strong); }
.chip-btn.danger:hover { border-color: var(--danger); background: rgba(239, 68, 68, 0.1); color: var(--danger); }

.primary-btn {
  display: inline-flex; align-items: center; gap: 6px;
  padding: 8px 14px; font-size: 13px; font-weight: 600;
  color: #fff; background: var(--accent); border: 1px solid var(--accent);
  border-radius: 9px; cursor: pointer; transition: filter 0.12s, transform 0.06s;
}
.primary-btn:hover { filter: brightness(1.08); }
.primary-btn:active { transform: translateY(1px); }
.primary-btn svg { width: 15px; height: 15px; }

.mini {
  padding: 4px 9px; font-size: 11.5px; font-weight: 500;
  color: var(--text-muted); background: var(--bg-elev);
  border: 1px solid var(--border); border-radius: 7px;
  cursor: pointer; transition: background 0.12s, color 0.12s, border-color 0.12s;
}
.mini:hover { background: var(--surface-hover); color: var(--text); }
.mini.danger:hover { border-color: var(--danger); color: var(--danger); background: rgba(239, 68, 68, 0.1); }

.badge { font-size: 11px; padding: 2px 9px; border-radius: 999px; font-weight: 600; white-space: nowrap; }
.badge.on  { background: rgba(34, 197, 94, 0.15); color: var(--success); }
.badge.idx { background: var(--accent-soft); color: var(--accent); }
.badge.err { background: rgba(239, 68, 68, 0.15); color: var(--danger); }

/* Khối thêm tài liệu trong modal "Tri thức của tôi" */
.modal .kb-add { display: flex; flex-direction: column; gap: 8px; margin-bottom: 14px; }
.modal .kb-add input[type="text"] {
  width: 100%; padding: 10px 13px; border-radius: var(--radius-sm);
  border: 1px solid var(--border); background: var(--surface); color: var(--text);
  font-size: 13.5px; font-family: inherit; outline: none;
}
.modal .kb-add textarea {
  width: 100%; min-height: 96px; resize: vertical; padding: 11px 13px;
  border-radius: var(--radius-sm); border: 1px solid var(--border);
  background: var(--surface); color: var(--text);
  font-size: 13.5px; font-family: inherit; line-height: 1.55; outline: none;
}
.modal .kb-add input:focus, .modal .kb-add textarea:focus {
  border-color: var(--accent); box-shadow: 0 0 0 3px var(--accent-soft);
}
.modal .kb-add-actions { display: flex; align-items: center; gap: 10px; flex-wrap: wrap; }
.modal .kb-status { font-size: 12.5px; color: var(--text-muted); }

/* =========================================================================
   Ô NHẬP TRÊN ĐIỆN THOẠI — to hơn, dễ bấm, không bị bàn phím che
   ========================================================================= */
@media (max-width: 820px) {
  .composer-wrap {
    padding: 8px 12px;
    padding-bottom: calc(10px + env(safe-area-inset-bottom, 0px));
    /* Dính đáy màn hình khả kiến, luôn nổi trên nội dung */
    position: sticky; bottom: 0; z-index: 20;
    border-top: 1px solid var(--border);
  }

  .composer {
    border-radius: 24px;
    padding: 6px 6px 6px 12px;
  }

  .composer textarea {
    /* 16px là NGƯỠNG BẮT BUỘC: dưới 16px, Safari/iOS tự phóng to trang khi gõ */
    font-size: 16px;
    line-height: 1.45;
    padding: 10px 0;
    min-height: 24px;
    max-height: 40vh;          /* gõ dài vẫn thấy, không nuốt hết màn hình */
  }

  /* Vùng chạm ≥ 44px theo khuyến nghị của Apple/Google */
  .attach-btn { width: 42px; height: 42px; }
  .attach-btn svg { width: 21px; height: 21px; }
  .send-btn { width: 42px; height: 42px; }
  .send-btn svg { width: 20px; height: 20px; }

  .composer-row { gap: 4px; }

  /* Dòng gợi ý chiếm chỗ quý giá trên màn hình nhỏ -> ẩn khi bàn phím bật */
  .composer-hint { font-size: 11px; margin-top: 6px; }
  body.kb-open .composer-hint { display: none; }

  /* Bàn phím bật: thu gọn khoảng đệm cho ô nhập nổi hẳn lên */
  body.kb-open .composer-wrap { padding-bottom: 6px; }
  body.kb-open .topbar { display: none; }   /* nhường chỗ cho nội dung + ô nhập */
}

@media (max-width: 820px) {
  /* Cuộn mượt, không "kéo" luôn cả trang phía sau khi đã cuộn hết tin nhắn */
  .messages {
    overscroll-behavior: contain;
    -webkit-overflow-scrolling: touch;
  }
  /* Vùng tin nhắn chừa chỗ để dòng cuối không nấp sau ô nhập */
  .messages-inner { padding-bottom: 8px; }
}

/* Nhãn trạng thái câu trả lời (dừng giữa chừng / bị gián đoạn) */
.msg-status {
  display: inline-block; margin-top: 8px;
  font-size: 11.5px; font-weight: 500;
  padding: 3px 9px; border-radius: 999px;
  background: var(--surface); border: 1px solid var(--border); color: var(--text-faint);
}
.msg-status.cancelled { color: var(--warning); border-color: rgba(245, 158, 11, 0.3); }
.msg-status.failed { color: var(--danger); border-color: rgba(239, 68, 68, 0.3); }

/* "Đang tạo câu trả lời…" khi mở lại hội thoại đang sinh dở */
.gen-pending {
  display: flex; align-items: center; gap: 8px;
  margin-top: 6px; font-size: 13.5px; color: var(--text-muted);
}
.gp-dot {
  width: 7px; height: 7px; border-radius: 50%; background: var(--accent);
  animation: gp-pulse 1.2s ease-in-out infinite;
}
@keyframes gp-pulse {
  0%, 100% { opacity: 0.35; transform: scale(0.85); }
  50%      { opacity: 1;    transform: scale(1.15); }
}
