:root {
  --bg: #14110e;
  --bg-elev: #1d1914;
  --ink: #f3ece3;
  --muted: #b7aa9a;
  --line: rgba(243, 236, 227, 0.12);
  --accent: #d9783a;
  --accent-ink: #1a120c;
  --user: #2a241c;
  --bot: #221c17;
  --danger: #e06a4f;
  --ok: #8fbf7a;
  --shadow: 0 18px 40px rgba(0, 0, 0, 0.28);
  --safe-top: env(safe-area-inset-top, 0px);
  --safe-bottom: env(safe-area-inset-bottom, 0px);
  --keyboard: 0px;
  --font: "Segoe UI", "Helvetica Neue", system-ui, sans-serif;
}

* {
  box-sizing: border-box;
}

/* Thanh cuộn khớp theme tối (xem docs/sidebar-scrollbar.md): mỏng 8px,
   thumb bo tròn, màu nâu trầm; hover nhấn accent cam nhẹ. CHỈ đổi vẻ
   ngoài — không đổi hành vi cuộn. Áp dụng mọi vùng cuộn: khung chat,
   sidebar, drawer, panel, admin (admin.html cũng load styles.css này).
   Firefox dùng scrollbar-width/color; Chrome/Edge dùng ::-webkit-*. */
* {
  scrollbar-width: thin;
  scrollbar-color: #5c5044 transparent;
}
*::-webkit-scrollbar {
  width: 8px;
  height: 8px;
}
*::-webkit-scrollbar-track {
  background: transparent;
}
*::-webkit-scrollbar-thumb {
  background: #5c5044;
  border-radius: 8px;
}
*::-webkit-scrollbar-thumb:hover {
  background: var(--accent);
}
*::-webkit-scrollbar-corner {
  background: transparent;
}

html,
body {
  margin: 0;
  min-height: 100%;
  background: var(--bg);
  color: var(--ink);
  font-family: var(--font);
}

body {
  min-height: 100vh;
  min-height: 100dvh;
}

button,
input,
textarea {
  font: inherit;
  color: inherit;
}

button:focus-visible,
input:focus-visible,
textarea:focus-visible,
a:focus-visible {
  outline: 2px solid var(--accent);
  outline-offset: 2px;
}

.app {
  display: grid;
  /* 6 hàng explicit cho 5 phần tử cột 2 + topbar (xem grid-row ở dưới):
     trước đây chỉ có 4 hàng `auto auto 1fr auto` mà cột 2 có tới 5 phần
     tử (session-bar, search-bar, compact-note, thread, composer-dock) —
     khi search-bar/compact-note hiện, thread rơi vào hàng implicit `auto`
     thay vì hàng `1fr`, mất khung co giãn cố định. Hàng trống (phần tử
     [hidden]) co về 0 nên desktop/khi ẩn không đổi gì. */
  grid-template-rows: auto auto auto auto minmax(0, 1fr) auto;
  /* Cột đơn khóa ở minmax(0, 1fr): cột `auto` mặc định phình theo
     min-content lớn nhất của các hàng — vd label nowrap của thanh
     "Đang trả lời" trong composer-dock đẩy track lên 406px ở viewport
     390px, kéo cả topbar/session-bar/thread tràn ngang theo (grid item
     nào cũng stretch theo track). minmax(0, 1fr) chặn track ở đúng
     bề rộng container, nội dung dài tự ellipsis/cuộn nội bộ. */
  grid-template-columns: auto minmax(0, 1fr) auto;
  min-height: 100dvh;
  width: 100%;
  margin: 0 auto;
}

/* Chat (không phải admin): khung chat phải cuộn NỘI BỘ trong #thread.
   Với min-height thì chiều cao .app là "không xác định" nên hàng grid 1fr
   giãn theo nội dung — thread phình theo tin nhắn, trang cuộn thay vì
   thread cuộn, và mọi `thread.scrollTop = ...` (kể cả auto-scroll theo
   stream tư duy) đều vô tác dụng (scrollTop luôn 0). height: 100dvh cho
   chiều cao xác định để hàng 1fr bị chặn đúng phần còn lại của viewport.
   Admin (.admin-app) GIỮ min-height: trang admin dài, cuộn theo tài liệu
   là đúng (admin-main không có hộp cuộn riêng). */
.app:not(.admin-app) {
  /* Fallback 100vh cho trình duyệt chưa hiểu dvh: không có nó, .app cao
     theo nội dung, trang cuộn và composer trôi xuống dưới fold. */
  height: 100vh;
  height: 100dvh;
}

[hidden] {
  display: none !important;
}

.topbar {
  grid-column: 1 / -1;
  position: sticky;
  top: 0;
  z-index: 5;
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  min-width: 0;
  padding: calc(12px + var(--safe-top)) 16px 12px;
  background: color-mix(in srgb, var(--bg) 88%, transparent);
  backdrop-filter: blur(16px);
  border-bottom: 1px solid var(--line);
}

.brand {
  display: flex;
  align-items: center;
  gap: 10px;
  min-width: 0;
}

.brand > div {
  min-width: 0;
}

.mark {
  width: 28px;
  height: 28px;
  border-radius: 8px;
  display: block;
  flex: 0 0 auto;
}

.title {
  margin: 0;
  font-size: 15px;
  font-weight: 700;
  letter-spacing: 0.02em;
}

.subtitle {
  margin: 2px 0 0;
  color: var(--muted);
  font-size: 12px;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.top-actions {
  display: flex;
  align-items: center;
  gap: 8px;
  flex-shrink: 0;
}

.mode-switch {
  display: flex;
  padding: 3px;
  border-radius: 999px;
  background: var(--bg-elev);
  border: 1px solid var(--line);
}

.mode-btn,
.ghost-btn,
.send-btn,
.gate-card button {
  border: 0;
  cursor: pointer;
}

.mode-btn {
  min-height: 44px;
  padding: 0 14px;
  border-radius: 999px;
  background: transparent;
  color: var(--muted);
}

.mode-btn.is-active {
  background: var(--accent);
  color: var(--accent-ink);
  font-weight: 700;
}

.agent-picker {
  position: relative;
  display: flex;
  justify-content: center;
  flex: 1 1 auto;
  min-width: 0;
}

.agent-btn {
  display: flex;
  align-items: center;
  gap: 8px;
  min-height: 44px;
  min-width: 0;
  max-width: 240px;
  padding: 0 12px;
  border-radius: 999px;
  border: 1px solid var(--line);
  background: var(--bg-elev);
  color: var(--ink);
  font-size: 14px;
  font-weight: 600;
  cursor: pointer;
}

.agent-btn:hover {
  border-color: color-mix(in srgb, var(--accent) 45%, var(--line));
}

.agent-btn #agent-name {
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
  min-width: 0;
}

.agent-dot {
  width: 10px;
  height: 10px;
  border-radius: 50%;
  background: var(--accent);
  flex: 0 0 auto;
}

.agent-caret {
  color: var(--muted);
  font-size: 11px;
  flex: 0 0 auto;
}

.agent-list {
  position: absolute;
  top: calc(100% + 6px);
  right: 0;
  z-index: 20;
  min-width: 240px;
  max-width: min(320px, calc(100vw - 32px));
  max-height: 320px;
  overflow-y: auto;
  margin: 0;
  padding: 6px;
  list-style: none;
  background: var(--bg-elev);
  border: 1px solid var(--line);
  border-radius: 16px;
  box-shadow: var(--shadow);
}

.agent-option {
  display: flex;
  gap: 10px;
  align-items: flex-start;
  width: 100%;
  padding: 10px 12px;
  border: 0;
  border-radius: 12px;
  background: transparent;
  color: var(--ink);
  text-align: left;
  cursor: pointer;
}

.agent-option:hover,
.agent-option:focus-visible {
  background: rgba(243, 236, 227, 0.06);
}

.agent-option.is-selected {
  background: color-mix(in srgb, var(--accent) 16%, transparent);
}

.agent-opt-text {
  min-width: 0;
}

.agent-opt-name {
  display: block;
  font-size: 14px;
  font-weight: 700;
}

.agent-opt-desc {
  display: block;
  margin-top: 2px;
  color: var(--muted);
  font-size: 12px;
  line-height: 1.4;
}

/* Skill picker: cùng mẫu agent-picker (nút 44px, list thả xuống). */
.skill-picker {
  position: relative;
  display: flex;
  justify-content: center;
  flex: 1 1 auto;
  min-width: 0;
}

.skill-btn {
  display: flex;
  align-items: center;
  gap: 8px;
  min-height: 44px;
  min-width: 0;
  max-width: 240px;
  padding: 0 12px;
  border-radius: 999px;
  border: 1px solid var(--line);
  background: var(--bg-elev);
  color: var(--ink);
  font-size: 14px;
  font-weight: 600;
  cursor: pointer;
}

.skill-btn:hover {
  border-color: color-mix(in srgb, var(--accent) 45%, var(--line));
}

.skill-btn #skill-name {
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
  min-width: 0;
}

.skill-caret {
  color: var(--muted);
  font-size: 11px;
  flex: 0 0 auto;
}

.skill-list {
  position: absolute;
  top: calc(100% + 6px);
  right: 0;
  z-index: 20;
  min-width: 240px;
  max-width: min(320px, calc(100vw - 32px));
  max-height: 320px;
  overflow-y: auto;
  margin: 0;
  padding: 6px;
  list-style: none;
  background: var(--bg-elev);
  border: 1px solid var(--line);
  border-radius: 16px;
  box-shadow: var(--shadow);
}

.skill-option {
  display: flex;
  gap: 10px;
  align-items: flex-start;
  width: 100%;
  min-height: 44px;
  padding: 10px 12px;
  border: 0;
  border-radius: 12px;
  background: transparent;
  color: var(--ink);
  text-align: left;
  cursor: pointer;
}

.skill-option:hover,
.skill-option:focus-visible {
  background: rgba(243, 236, 227, 0.06);
}

.skill-option.is-selected {
  background: color-mix(in srgb, var(--accent) 16%, transparent);
}

.skill-opt-text {
  min-width: 0;
}

.skill-opt-name {
  display: block;
  font-size: 14px;
  font-weight: 700;
}

.skill-opt-desc {
  display: block;
  margin-top: 2px;
  color: var(--muted);
  font-size: 12px;
  line-height: 1.4;
}

/* Chip skill đang active, trên ô nhập: hiện tên + nguồn (tự chọn). */
.skill-chip-row {
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 0 4px 8px;
}

.skill-chip {
  display: inline-flex;
  align-items: center;
  min-height: 44px;
  padding: 0 12px;
  border-radius: 999px;
  border: 1px solid var(--line);
  background: color-mix(in srgb, var(--accent) 14%, transparent);
  color: var(--ink);
  font-size: 13px;
  font-weight: 600;
  overflow-wrap: anywhere;
}

.skill-chip-clear {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  min-width: 44px;
  min-height: 44px;
  padding: 0;
  border-radius: 999px;
  border: 1px solid var(--line);
  background: var(--bg-elev);
  color: var(--muted);
  font-size: 14px;
  cursor: pointer;
}

.skill-chip-clear:hover {
  color: var(--ink);
}

/* Thanh "Đang trả lời …" phía trên ô nhập (xem docs/message-reply.md):
   cùng hàng với nút X 44px như skill-chip-row; label ellipsis 1 dòng
   để mobile 390px không overflow. Ẩn bằng [hidden]. */
.reply-bar {
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 0 4px 8px;
}

.reply-bar[hidden] {
  display: none;
}

.reply-bar-label {
  flex: 1 1 auto;
  min-width: 0;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
  color: var(--muted);
  font-size: 13px;
}

.reply-bar-cancel {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  flex: 0 0 auto;
  min-width: 44px;
  min-height: 44px;
  padding: 0;
  border-radius: 999px;
  border: 1px solid var(--line);
  background: var(--bg-elev);
  color: var(--muted);
  font-size: 14px;
  cursor: pointer;
}

.reply-bar-cancel:hover {
  color: var(--ink);
}

/* Khối quote trên 1 tin reply: chữ nhỏ + mờ hơn nội dung, viền trái
   phân biệt; là <button> nên bấm được để cuộn tới tin gốc. Excerpt
   ellipsis, không bao giờ đẩy bubble tràn ngang. */
.reply-quote {
  display: flex;
  align-items: baseline;
  gap: 6px;
  width: 100%;
  min-width: 0;
  margin-bottom: 6px;
  padding: 6px 10px;
  border: 0;
  border-left: 3px solid var(--accent);
  border-radius: 0 8px 8px 0;
  background: color-mix(in srgb, var(--accent) 10%, transparent);
  color: var(--muted);
  font-size: 12px;
  line-height: 1.4;
  text-align: left;
  cursor: pointer;
}

.reply-quote:hover {
  color: var(--ink);
}

.reply-quote-author {
  flex: 0 0 auto;
  font-weight: 700;
}

.reply-quote-excerpt {
  flex: 1 1 auto;
  min-width: 0;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

/* Trích trong quote render markdown inline: phần tử inline (strong /
   em / code) giữ nguyên, <br> lạc vào thì ẩn để chip vẫn 1 dòng. */
.reply-quote-excerpt br {
  display: none;
}

.model-picker {
  position: relative;
  display: flex;
  justify-content: center;
  flex: 1 1 auto;
  min-width: 0;
}

.model-btn {
  display: flex;
  align-items: center;
  gap: 8px;
  min-height: 44px;
  min-width: 0;
  max-width: 280px;
  padding: 0 12px;
  border-radius: 999px;
  border: 1px solid var(--line);
  background: var(--bg-elev);
  color: var(--ink);
  font-size: 14px;
  font-weight: 600;
  cursor: pointer;
}

.model-btn:hover {
  border-color: color-mix(in srgb, var(--accent) 45%, var(--line));
}

.model-btn:disabled {
  opacity: 0.55;
  cursor: default;
}

.model-btn #model-name {
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
  min-width: 0;
}

.model-caret {
  color: var(--muted);
  font-size: 11px;
  flex: 0 0 auto;
}

.model-list {
  position: absolute;
  top: calc(100% + 6px);
  right: 0;
  z-index: 20;
  min-width: 240px;
  max-width: min(320px, calc(100vw - 32px));
  max-height: 320px;
  overflow-y: auto;
  margin: 0;
  padding: 6px;
  list-style: none;
  background: var(--bg-elev);
  border: 1px solid var(--line);
  border-radius: 16px;
  box-shadow: var(--shadow);
}

.model-option {
  display: flex;
  gap: 10px;
  align-items: flex-start;
  width: 100%;
  padding: 10px 12px;
  border: 0;
  border-radius: 12px;
  background: transparent;
  color: var(--ink);
  text-align: left;
  cursor: pointer;
}

.model-option:hover,
.model-option:focus-visible {
  background: rgba(243, 236, 227, 0.06);
}

.model-option.is-selected {
  background: color-mix(in srgb, var(--accent) 16%, transparent);
}

.model-option:disabled {
  color: var(--muted);
  cursor: default;
}

.model-opt-text {
  min-width: 0;
}

.model-opt-name {
  display: block;
  font-size: 14px;
  font-weight: 700;
  overflow-wrap: anywhere;
}

/* Thinking-speed picker: same shape as the model picker. */
.speed-picker {
  position: relative;
  display: flex;
  justify-content: center;
  flex: 1 1 auto;
  min-width: 0;
}

.speed-btn {
  display: flex;
  align-items: center;
  gap: 8px;
  min-height: 44px;
  min-width: 0;
  max-width: 160px;
  padding: 0 12px;
  border-radius: 999px;
  border: 1px solid var(--line);
  background: var(--bg-elev);
  color: var(--ink);
  font-size: 14px;
  font-weight: 600;
  cursor: pointer;
}

.speed-btn:hover {
  border-color: color-mix(in srgb, var(--accent) 45%, var(--line));
}

.speed-btn:disabled {
  opacity: 0.55;
  cursor: default;
}

.speed-btn #speed-name {
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
  min-width: 0;
}

.speed-caret {
  color: var(--muted);
  font-size: 11px;
  flex: 0 0 auto;
}

.speed-list {
  position: absolute;
  top: calc(100% + 6px);
  right: 0;
  z-index: 20;
  min-width: 180px;
  max-width: min(280px, calc(100vw - 32px));
  max-height: 320px;
  overflow-y: auto;
  margin: 0;
  padding: 6px;
  list-style: none;
  background: var(--bg-elev);
  border: 1px solid var(--line);
  border-radius: 16px;
  box-shadow: var(--shadow);
}

.speed-note {
  padding: 8px 12px 2px;
  color: var(--muted);
  font-size: 12px;
  font-weight: 500;
}

.speed-option {
  display: flex;
  gap: 10px;
  align-items: center;
  width: 100%;
  min-height: 44px;
  padding: 10px 12px;
  border: 0;
  border-radius: 12px;
  background: transparent;
  color: var(--ink);
  font-size: 14px;
  font-weight: 700;
  text-align: left;
  cursor: pointer;
}

.speed-option:hover,
.speed-option:focus-visible {
  background: rgba(243, 236, 227, 0.06);
}

.speed-option.is-selected {
  background: color-mix(in srgb, var(--accent) 16%, transparent);
}

/* Thinking toggle + body inside an assistant bubble. Collapsed by default;
   the body only renders when the user expands it. */
.thinking {
  margin-bottom: 8px;
}

.thinking-toggle {
  display: flex;
  align-items: center;
  gap: 8px;
  width: 100%;
  min-height: 44px;
  padding: 0 12px;
  border-radius: 12px;
  border: 1px solid var(--line);
  background: var(--bg);
  color: var(--muted);
  font-size: 13px;
  font-weight: 600;
  text-align: left;
  cursor: pointer;
}

.thinking-toggle:hover {
  color: var(--ink);
  border-color: color-mix(in srgb, var(--accent) 45%, var(--line));
}

.thinking-toggle.is-thinking {
  animation: typing-pulse 1.6s ease-in-out infinite;
}

.thinking-label {
  flex: 1 1 auto;
  min-width: 0;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

/* Icon đầu nút Thinking: chỉ là dấu hiệu nhận biết, nhãn chữ giữ nguyên
   vì nó mang thông tin trạng thái ("Đã suy nghĩ · 5s"). */
.thinking-icon {
  display: flex;
  flex: 0 0 auto;
}

.thinking-caret {
  flex: 0 0 auto;
  font-size: 11px;
}

/* Thinking panel: KHÔNG chặn max-height, KHÔNG hộp cuộn riêng — khi mở,
   nội dung tư duy hiện hết theo chiều dài thật, khung chat (#thread) là
   chỗ cuộn duy nhất và tự cuộn theo stream (xem isNearThreadBottom trong
   app.js). */
.thinking-body {
  margin-top: 8px;
  padding: 10px 12px;
  border-radius: 12px;
  border: 1px solid var(--line);
  background: var(--bg);
  color: var(--muted);
  font-size: 13px;
  line-height: 1.55;
  white-space: pre-wrap;
  overflow-wrap: anywhere;
}

/* Browser activity view (Track C): cùng shape với khối thinking —
   toggle thu gọn/mở + thân hiện ảnh chụp mới nhất (to, full width)
   và danh sách bước bên dưới. Chỉ xem: không có control nào khác. */
.browser-activity {
  margin-bottom: 8px;
}

.browser-activity-toggle {
  display: flex;
  align-items: center;
  gap: 8px;
  width: 100%;
  min-height: 44px;
  padding: 0 12px;
  border-radius: 12px;
  border: 1px solid var(--line);
  background: var(--bg);
  color: var(--muted);
  font-size: 13px;
  font-weight: 600;
  text-align: left;
  cursor: pointer;
}

.browser-activity-toggle:hover {
  color: var(--ink);
  border-color: color-mix(in srgb, var(--accent) 45%, var(--line));
}

.browser-activity-label {
  flex: 1 1 auto;
  min-width: 0;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.browser-activity-icon {
  display: flex;
  flex: 0 0 auto;
}

.browser-activity-caret {
  flex: 0 0 auto;
  font-size: 11px;
}

.browser-activity-body {
  margin-top: 8px;
  padding: 10px 12px;
  border-radius: 12px;
  border: 1px solid var(--line);
  background: var(--bg);
  color: var(--muted);
  font-size: 13px;
  line-height: 1.55;
}

.browser-activity-shot {
  display: block;
  width: 100%;
  height: auto;
  margin-bottom: 10px;
  border-radius: 8px;
  border: 1px solid var(--line);
}

.browser-activity-noshot {
  margin-bottom: 10px;
  color: var(--muted);
}

.browser-activity-steps {
  margin: 0;
  padding-left: 20px;
  overflow-wrap: anywhere;
}

.browser-activity-step {
  margin: 2px 0;
}

.browser-activity-failed {
  color: var(--danger);
}

/* Nhãn trạng thái thật của đáp án (không thay đáp án bằng bubble lỗi):
   stream bị ngắt giữa chừng, hoặc đáp án bị cắt do hết token. */
.notice {
  margin-top: 8px;
  padding: 8px 10px;
  border-radius: 10px;
  border: 1px solid var(--line);
  background: var(--bg);
  color: var(--muted);
  font-size: 13px;
  line-height: 1.45;
}

.notice.is-interrupted {
  border-color: color-mix(in srgb, var(--accent) 55%, var(--line));
}

.notice.is-truncated {
  border-color: color-mix(in srgb, var(--danger) 55%, var(--line));
}

/* Tin bị chặn (gate fail / lỗi): dòng notice đầu bubble, đọc rõ là
   thông báo hệ thống chứ không phải đáp án của AI. */
.notice.is-blocked {
  border-color: color-mix(in srgb, var(--danger) 55%, var(--line));
  font-weight: 700;
  color: var(--ink);
}

.toast-root {
  position: fixed;
  top: 16px;
  right: 16px;
  z-index: 50;
  display: flex;
  flex-direction: column;
  gap: 8px;
  max-width: min(360px, calc(100vw - 32px));
}

.toast {
  background: var(--bg-elev);
  border: 1px solid var(--line);
  border-radius: 12px;
  padding: 10px 14px;
  box-shadow: var(--shadow);
  font-size: 14px;
  line-height: 1.4;
}

.toast.is-ok {
  border-color: color-mix(in srgb, var(--ok) 60%, var(--line));
}

.toast.is-error {
  border-color: color-mix(in srgb, var(--danger) 60%, var(--line));
}

.ghost-btn {
  min-height: 44px;
  padding: 0 12px;
  border-radius: 10px;
  background: transparent;
  color: var(--muted);
}

.thread {
  /* min-height: 0: grid item không được phình tối thiểu theo nội dung —
     hàng 1fr đã bị chặn bởi height: 100dvh của .app (xem trên), thread
     thừa nội dung thì cuộn nội bộ thay vì đẩy trang dài ra. */
  min-height: 0;
  overflow-y: auto;
  padding: 16px 16px 8px;
  display: flex;
  flex-direction: column;
  gap: 12px;
  -webkit-overflow-scrolling: touch;
}

.empty {
  margin: auto;
  max-width: 320px;
  text-align: center;
  color: var(--muted);
}

.empty h2 {
  margin: 0 0 8px;
  color: var(--ink);
  font-size: 20px;
}

.empty p {
  margin: 0 0 6px;
}

.empty-hint {
  font-size: 13px;
}

/* G41 — starter prompts ở empty state: cùng mẫu pill như .skill-chip
   (bo tròn, viền var(--line), nền accent nhạt), bấm là gửi. */
.empty-starters {
  display: flex;
  flex-wrap: wrap;
  justify-content: center;
  gap: 8px;
  margin-top: 12px;
}

.empty-starter {
  padding: 8px 12px;
  border-radius: 999px;
  border: 1px solid var(--line);
  background: color-mix(in srgb, var(--accent) 14%, transparent);
  color: var(--ink);
  font-size: 13px;
  line-height: 1.45;
  text-align: left;
  cursor: pointer;
}

.empty-starter:hover {
  border-color: color-mix(in srgb, var(--accent) 45%, var(--line));
}

.bubble {
  max-width: 100%;
  padding: 12px 14px;
  border-radius: 18px;
  border: 1px solid var(--line);
  overflow-wrap: anywhere;
  line-height: 1.55;
}

.bubble-body {
  font-size: 15px;
}

.bubble.user .bubble-body {
  white-space: pre-wrap;
}

.bubble-body p,
.bubble-body ul,
.bubble-body ol,
.bubble-body pre {
  margin: 0 0 0.7em;
}

.bubble-body p:last-child,
.bubble-body ul:last-child,
.bubble-body ol:last-child,
.bubble-body pre:last-child,
.bubble-body h3:last-child {
  margin-bottom: 0;
}

.bubble-body h3 {
  margin: 0 0 0.4em;
  font-size: 15px;
}

.bubble-body ul,
.bubble-body ol {
  padding-left: 1.2em;
}

.bubble-body li + li {
  margin-top: 0.25em;
}

.bubble-body code {
  font-family: ui-monospace, Consolas, monospace;
  font-size: 0.9em;
  background: var(--bg);
  padding: 0.1em 0.35em;
  border-radius: 6px;
}

.bubble-body pre {
  overflow-x: auto;
  padding: 10px 12px;
  border-radius: 12px;
  background: var(--bg);
  border: 1px solid var(--line);
}

.bubble-body pre code {
  padding: 0;
  background: none;
}

/* G1: link thu gọn thành tên nguồn + nút copy-link cạnh mỗi link.
   Nút nhỏ, mờ khi chưa hover để không át nội dung; chỉ là nút bấm
   (không chặn bấm mở link bên cạnh, không chặn long-press trên bubble). */
.bubble-body .md-link-copy {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  vertical-align: middle;
  margin-left: 2px;
  padding: 2px;
  border: none;
  border-radius: 4px;
  background: transparent;
  color: inherit;
  opacity: 0.55;
  cursor: pointer;
  line-height: 0;
}

.bubble-body .md-link-copy:hover,
.bubble-body .md-link-copy:focus-visible {
  opacity: 1;
  background: rgba(127, 127, 127, 0.18);
  outline: none;
}

/* G42: khối code có nhãn ngôn ngữ + nút Sao chép. Head nằm trên
   <pre>, nhãn mờ ở trái, nút ở phải (margin-left auto); khối giữ
   nền của <pre> cũ, head tách bằng border dưới. */
.bubble-body .md-code-block {
  margin: 8px 0;
  border: 1px solid var(--line);
  border-radius: 8px;
  overflow: hidden;
  background: var(--bg);
}

.bubble-body .md-code-head {
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 4px 8px;
  border-bottom: 1px solid var(--line);
  font-size: 12px;
  line-height: 1.4;
}

.bubble-body .md-code-lang {
  color: var(--muted);
  font-family: ui-monospace, SFMono-Regular, Menlo, monospace;
}

.bubble-body .md-code-copy {
  margin-left: auto;
  padding: 2px 8px;
  border: 1px solid var(--line);
  border-radius: 6px;
  background: transparent;
  color: inherit;
  font: inherit;
  cursor: pointer;
  opacity: 0.75;
}

.bubble-body .md-code-copy:hover,
.bubble-body .md-code-copy:focus-visible {
  opacity: 1;
  background: rgba(127, 127, 127, 0.18);
  outline: none;
}

.bubble-body .md-code-block pre {
  margin: 0;
  border: none;
  border-radius: 0;
  background: none;
}

.bubble.user {
  margin-left: auto;
  background: color-mix(in srgb, var(--accent) 18%, var(--user));
  border-color: color-mix(in srgb, var(--accent) 38%, var(--line));
  border-bottom-right-radius: 6px;
}

.bubble.assistant {
  margin-right: auto;
  background: var(--bot);
  border-bottom-left-radius: 6px;
}

/* Nhãn vai trò nhỏ giúp phân biệt tin user / AI ngay từ cái nhìn đầu tiên. */
.bubble.user::before,
.bubble.assistant:not(.typing)::before {
  display: block;
  margin-bottom: 4px;
  font-size: 11px;
  font-weight: 700;
  letter-spacing: 0.08em;
  text-transform: uppercase;
  color: var(--muted);
}

.bubble.user::before {
  content: "Bạn";
  text-align: right;
}

.bubble.assistant:not(.typing)::before {
  content: "AI";
}

/* Tin bị chặn (gate fail / lỗi) KHÔNG phải đáp án của AI: nhãn "Hệ thống"
   thay cho "AI", nền phẳng như thông báo (không nền bot). Specificity
   phải cao hơn `.bubble.assistant:not(.typing)::before` ở trên. */
.bubble.assistant.system-notice:not(.typing)::before {
  content: "Hệ thống";
}

.bubble.system-notice {
  background: var(--bg);
}

/* Tin hệ thống (role system): không phải tin user/AI — hiện như 1
   thông báo ở giữa thread, có nhãn "Hệ thống", nền phẳng, chiều rộng
   giới hạn, thay vì bubble trần full-width không nhãn như trước. */
.bubble.system {
  align-self: center;
  width: min(100%, 640px);
  background: var(--bg);
  text-align: center;
  color: var(--muted);
}

.bubble.system::before {
  display: block;
  margin-bottom: 4px;
  font-size: 11px;
  font-weight: 700;
  letter-spacing: 0.08em;
  text-transform: uppercase;
  color: var(--muted);
  content: "Hệ thống";
}

.bubble.warn {
  border-color: color-mix(in srgb, var(--danger) 50%, var(--line));
}

.meta {
  display: flex;
  gap: 8px;
  flex-wrap: wrap;
  margin-top: 8px;
  color: var(--muted);
  font-size: 11px;
}

.chip {
  border: 1px solid var(--line);
  border-radius: 999px;
  padding: 2px 8px;
}

.composer-dock {
  position: sticky;
  bottom: 0;
  padding: 8px 16px calc(12px + var(--safe-bottom) + var(--keyboard));
  background: color-mix(in srgb, var(--bg) 92%, transparent);
  backdrop-filter: blur(16px);
  border-top: 1px solid var(--line);
}

/* Copyright line at the bottom of the composer dock: single subtle line,
   always visible in the chat view (desktop + mobile), theme-aware muted. */
.copyright {
  margin: 8px 0 0;
  color: var(--muted);
  font-size: 11px;
  line-height: 1.4;
  text-align: center;
}

.file-field {
  display: grid;
  gap: 6px;
  margin-bottom: 8px;
  color: var(--muted);
  font-size: 12px;
}

.file-field.is-hidden {
  display: none;
}

.file-field input,
.composer textarea,
.gate-card input {
  width: 100%;
  min-height: 44px;
  border-radius: 14px;
  border: 1px solid var(--line);
  background: var(--bg-elev);
  padding: 10px 12px;
  font-size: 16px;
}

.composer {
  display: flex;
  gap: 8px;
  align-items: flex-end;
}

.composer textarea {
  resize: none;
  max-height: 144px;
  line-height: 1.4;
}

.send-btn,
.stop-btn,
.gate-card button {
  min-height: 44px;
  min-width: 72px;
  border-radius: 14px;
  background: var(--accent);
  color: var(--accent-ink);
  font-weight: 700;
}

/* Vision (P2): nút đính kèm/link + hàng preview + hàng nhập link. */
.attach-btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  min-height: 44px;
  min-width: 44px;
  border-radius: 14px;
  border: 1px solid var(--line);
  background: var(--bg-elev);
  color: inherit;
  font-size: 18px;
  flex-shrink: 0;
  cursor: pointer;
}

/* G47: SVG trong nút attach căn giữa, không rớt dòng. */
.attach-btn svg {
  display: block;
}

.attachment-preview {
  display: flex;
  flex-wrap: wrap;
  gap: 8px;
  margin-bottom: 8px;
}

.attachment-preview[hidden],
.vision-link-row[hidden] {
  display: none;
}

.attachment-item {
  position: relative;
  display: inline-flex;
  align-items: center;
  gap: 6px;
  max-width: 220px;
  border: 1px solid var(--line);
  border-radius: 12px;
  background: var(--bg-elev);
  padding: 4px 26px 4px 4px;
}

.attachment-thumb {
  width: 56px;
  height: 56px;
  object-fit: cover;
  border-radius: 8px;
  flex-shrink: 0;
}

.attachment-name {
  font-size: 12px;
  color: var(--muted);
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.attachment-link-chip .attachment-name {
  max-width: 180px;
}

.attachment-remove {
  position: absolute;
  top: 2px;
  right: 2px;
  width: 20px;
  height: 20px;
  border-radius: 999px;
  border: 1px solid var(--line);
  background: var(--bg-elev);
  font-size: 11px;
  line-height: 1;
  padding: 0;
}

.vision-link-row {
  display: flex;
  gap: 8px;
  margin-bottom: 8px;
}

.vision-link-row input {
  flex: 1;
  min-height: 44px;
  border-radius: 14px;
  border: 1px solid var(--line);
  background: var(--bg-elev);
  padding: 10px 12px;
  font-size: 15px;
}

.vision-link-add {
  min-height: 44px;
  border-radius: 14px;
  border: 1px solid var(--line);
  background: var(--bg-elev);
  padding: 0 14px;
  font-weight: 600;
  white-space: nowrap;
}

/* Ảnh trong bubble chat: thumbnail upload + ảnh trang web (P4). */
.bubble-attachments {
  display: flex;
  flex-wrap: wrap;
  gap: 8px;
  margin-top: 8px;
}

.bubble-attachment-img {
  width: auto;
  height: auto;
  max-width: 200px;
  max-height: 160px;
}

.attachment-link {
  font-size: 13px;
  overflow-wrap: anywhere;
}

.stop-btn {
  background: var(--danger);
  color: #fff;
}

.send-btn:disabled,
.stop-btn:disabled,
.gate-card button:disabled {
  opacity: 0.55;
}

.bubble-actions {
  display: flex;
  justify-content: flex-end;
  gap: 8px;
  margin-top: 8px;
}

.bubble-action,
.edit-actions button {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  border: 0;
  background: transparent;
  color: var(--muted);
  padding: 0 10px;
  min-width: 44px;
  min-height: 44px;
  cursor: pointer;
  font-size: 12px;
}

/* Nút hành động tin nhắn dạng icon-only: svg căn giữa, không lệch theo
   baseline chữ (svg inline mặc định ngồi trên baseline). */
.bubble-action svg,
.thinking-control-btn svg,
.thinking-icon svg {
  display: block;
}

/* G4 — nút hành động bị disable (vd Tạo lại lúc đang bận): mờ + không
   bấm được, khác hẳn nút ẩn — user vẫn thấy tính năng còn đó. */
.bubble-action:disabled {
  opacity: 0.35;
  cursor: not-allowed;
}

/* Nhãn "Đã chỉnh sửa" trên tin user đã sửa (kèm nút Lịch sử bên cạnh). */
.edited-label {
  margin-right: auto;
  align-self: center;
  color: var(--muted);
  font-size: 11px;
  font-style: italic;
}

/* Highlight tin được trỏ tới bằng deep link (?message=...). */
.bubble.is-highlight {
  animation: highlight-pulse 1s ease-in-out 4;
  border-color: var(--accent);
}

@keyframes highlight-pulse {
  0%, 100% {
    box-shadow: 0 0 0 0 rgba(217, 120, 58, 0.55);
  }
  50% {
    box-shadow: 0 0 0 6px rgba(217, 120, 58, 0.18);
  }
}

/* Nút tìm kiếm ở header chat (kính lúp, 44px) + ô tìm kiếm trượt dưới
   topbar (Esc / nút Đóng để đóng, Ctrl+F để mở). Ô tìm kiếm wrap 2 hàng
   trên mobile 390px nên không vỡ header. */
.top-icon-btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  flex: 0 0 auto;
  min-width: 44px;
  min-height: 44px;
  padding: 0 10px;
  border-radius: 999px;
  border: 1px solid var(--line);
  background: transparent;
  color: var(--ink);
  cursor: pointer;
}

.top-icon-btn:hover {
  border-color: color-mix(in srgb, var(--accent) 45%, var(--line));
}

.search-bar {
  display: flex;
  align-items: center;
  flex-wrap: wrap;
  gap: 8px;
  padding: 8px 16px;
  border-bottom: 1px solid var(--line);
  background: var(--bg);
}

.search-bar[hidden] {
  display: none;
}

.search-bar input {
  flex: 1 1 160px;
  min-width: 0;
  min-height: 44px;
  border-radius: 14px;
  border: 1px solid var(--line);
  background: var(--bg-elev);
  padding: 10px 12px;
  font-size: 16px;
  color: var(--ink);
}

.search-count {
  color: var(--muted);
  font-size: 13px;
  white-space: nowrap;
}

.search-nav-btn {
  min-height: 44px;
  padding: 0 12px;
  border-radius: 10px;
  border: 1px solid var(--line);
  background: var(--bg-elev);
  color: var(--ink);
  font-size: 13px;
  cursor: pointer;
}

.search-nav-btn:hover {
  border-color: color-mix(in srgb, var(--accent) 45%, var(--line));
}

/* Nút "Xem tin mới nhất" nổi phía trên composer: chỉ hiện khi user
   không ở cuối thread; bấm thì cuộn mượt xuống cuối + xóa số đếm. */
.jump-latest-btn {
  position: absolute;
  bottom: calc(100% + 8px);
  left: 50%;
  transform: translateX(-50%);
  z-index: 6;
  min-height: 44px;
  padding: 0 16px;
  border-radius: 999px;
  border: 1px solid var(--line);
  background: var(--accent);
  color: var(--accent-ink);
  font-size: 14px;
  font-weight: 700;
  white-space: nowrap;
  box-shadow: 0 4px 16px rgba(0, 0, 0, 0.25);
  cursor: pointer;
}

.jump-latest-btn[hidden] {
  display: none;
}

/* Highlight từ khóa tìm kiếm + tin đang chọn (Trước/Tiếp). */
mark.search-hit {
  background: rgba(217, 120, 58, 0.45);
  color: inherit;
  border-radius: 3px;
  padding: 0 1px;
}

.bubble.is-search-current {
  border-color: var(--accent);
  box-shadow: 0 0 0 3px rgba(217, 120, 58, 0.25);
}

/* Nút Tạm dừng / Tiếp tục / Bỏ qua trong khối tư duy — chỉ hiện khi đang
   ở phase tư duy. Tạm dừng là dừng HIỂN THỊ, generation vẫn chạy nền. */
.thinking-controls {
  display: flex;
  align-items: center;
  flex-wrap: wrap;
  gap: 8px;
  margin-top: 8px;
}

.thinking-control-btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  min-width: 44px;
  min-height: 44px;
  padding: 0 14px;
  border-radius: 12px;
  border: 1px solid var(--line);
  background: var(--bg);
  color: var(--ink);
  font-size: 13px;
  font-weight: 600;
  cursor: pointer;
}

.thinking-control-btn:hover {
  border-color: color-mix(in srgb, var(--accent) 45%, var(--line));
}

.thinking-pause-note {
  color: var(--muted);
  font-size: 12px;
  line-height: 1.4;
}

/* Drawer (trượt từ phải, mobile full-width) — hiện chỉ còn drawer Bộ
   nhớ dùng; drawer "Lịch sử" đã thay bằng sidebar (xem
   docs/sidebar-scrollbar.md).
   z-index 60 > toast (50): toast không được che tiêu đề drawer;
   toast ở ngoài drawer không đổi (vẫn trên mọi thứ còn lại). */
.drawer-overlay {
  position: fixed;
  inset: 0;
  z-index: 60;
  background: rgba(0, 0, 0, 0.5);
}

.drawer {
  position: absolute;
  top: 0;
  right: 0;
  display: flex;
  flex-direction: column;
  width: min(380px, 100vw);
  height: 100%;
  background: var(--bg-elev);
  border-left: 1px solid var(--line);
  padding: 16px;
  overflow-y: auto;
}

.drawer-head {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
}

.drawer-head h2 {
  margin: 0;
  font-size: 17px;
}

.drawer-close,
.panel-close {
  min-height: 44px;
  min-width: 44px;
  padding: 0 12px;
  border-radius: 12px;
  border: 1px solid var(--line);
  background: transparent;
  color: var(--ink);
  cursor: pointer;
}

.drawer-new {
  min-height: 44px;
  margin-top: 16px;
  border-radius: 12px;
  border: 0;
  background: var(--accent);
  color: var(--accent-ink);
  font-weight: 700;
  cursor: pointer;
}

/* Bảng GFM trong tin AI (xem docs/sidebar-scrollbar.md): theme tối,
   header đậm + nền accent nhạt, viền dòng, xen kẽ nhạt; rộng hơn
   bubble thì cuộn ngang trong wrap, không vỡ layout. */
.md-table-wrap {
  overflow-x: auto;
  margin: 8px 0;
  border: 1px solid var(--line);
  border-radius: 12px;
}

.md-table-wrap table {
  width: 100%;
  border-collapse: collapse;
  font-size: 14px;
  line-height: 1.5;
}

.md-table-wrap th,
.md-table-wrap td {
  padding: 8px 12px;
  border-bottom: 1px solid var(--line);
  vertical-align: top;
  overflow-wrap: anywhere;
}

.md-table-wrap thead th {
  font-weight: 700;
  background: color-mix(in srgb, var(--accent) 12%, var(--bg-elev));
}

.md-table-wrap tbody tr:nth-child(even) td {
  background: rgba(243, 236, 227, 0.04);
}

.md-table-wrap tbody tr:last-child td {
  border-bottom: 0;
}

/* Công thức toán KaTeX: cùng cỡ/màu chữ với theme (KaTeX mặc định
   1.21em — to lạc lõng); display math cuộn ngang khi dài, không vỡ
   mobile. Fallback Unicode (khi thiếu KaTeX) dùng chung cỡ chữ. */
.bubble-body .katex {
  font-size: 1em;
  color: inherit;
}

.math-display {
  overflow-x: auto;
  overflow-y: hidden;
  margin: 8px 0;
}

.math-display .katex-display {
  margin: 0;
}

.katex-fallback {
  font-size: 1em;
}

/* Menu chuột phải trên tin nhắn (xem docs/sidebar-scrollbar.md):
   1 element duy nhất, build lại mỗi lần mở, đặt fixed tại vị trí
   con trỏ (JS clamp vào viewport). z-index 70 > drawer (60). */
.context-menu {
  position: fixed;
  z-index: 70;
  min-width: 200px;
  max-width: min(280px, 90vw);
  padding: 6px;
  border-radius: 12px;
  border: 1px solid var(--line);
  background: var(--bg-elev);
  box-shadow: var(--shadow);
}

.context-menu-item {
  display: block;
  width: 100%;
  min-height: 44px;
  padding: 0 12px;
  border: 0;
  border-radius: 8px;
  background: transparent;
  color: var(--ink);
  font-size: 14px;
  text-align: left;
  cursor: pointer;
}

.context-menu-item:hover:not(:disabled) {
  background: color-mix(in srgb, var(--accent) 16%, transparent);
}

.context-menu-item:disabled {
  opacity: 0.45;
  cursor: default;
}

/* Sidebar danh sách phiên (thay drawer "Lịch sử", xem
   docs/sidebar-scrollbar.md): desktop là cột trái 280px trong grid
   .app; mobile (<=639px) là overlay trượt từ trái + backdrop.
   Các khối còn lại của chat ở cột 2; không có sidebar (admin) thì cột
   auto co về 0. */
.app > .session-bar,
.app > .search-bar,
.app > .compact-note,
.app > .thread,
.app > .composer-dock {
  grid-column: 2;
}

/* Hàng explicit cho từng khối cột 2 (xem grid-template-rows của .app):
   thread LUÔN là hàng `minmax(0, 1fr)` duy nhất, composer-dock LUÔN là
   hàng cuối — không còn phụ thuộc vào thứ tự auto-placement khi
   search-bar/compact-note ẩn/hiện. */
.app > .session-bar {
  grid-row: 2;
}
.app > .search-bar {
  grid-row: 3;
}
.app > .compact-note {
  grid-row: 4;
}
.app > .thread {
  grid-row: 5;
}
.app > .composer-dock {
  grid-row: 6;
}

.sidebar {
  grid-column: 1;
  grid-row: 2 / -1;
  /* position:relative: neo tay kéo giãn (.panel-resize-handle) ở
     mép phải; chế độ drawer ghi đè bằng position:fixed. */
  position: relative;
  width: 280px;
  min-height: 0;
  display: flex;
  flex-direction: column;
  background: var(--bg-elev);
  border-right: 1px solid var(--line);
  overflow: hidden;
}

.app.sidebar-collapsed .sidebar {
  display: none;
}

.sidebar-head {
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 12px;
  border-bottom: 1px solid var(--line);
}

.sidebar-new {
  flex: 1;
  min-height: 44px;
  border-radius: 12px;
  border: 0;
  background: var(--accent);
  color: var(--accent-ink);
  font-weight: 700;
  cursor: pointer;
}

.sidebar-new:hover {
  filter: brightness(1.08);
}

.sidebar-close {
  display: none;
  align-items: center;
  justify-content: center;
  min-width: 44px;
  min-height: 44px;
  border-radius: 12px;
  border: 1px solid var(--line);
  background: transparent;
  color: var(--ink);
  cursor: pointer;
}

.session-list {
  flex: 1;
  min-height: 0;
  overflow-y: auto;
  margin: 0;
  padding: 8px;
  list-style: none;
  display: flex;
  flex-direction: column;
  gap: 4px;
}

/* G5 — tiêu đề nhóm ngày trong danh sách phiên ("Hôm nay", "Hôm qua",
   "7 ngày trước", "Trước đó"): 1 dòng nhỏ, không bấm được, nhóm rỗng
   thì không render nên không bao giờ thấy tiêu đề trơ không hàng. */
.session-group-header {
  padding: 12px 12px 2px;
  color: var(--muted);
  font-size: 12px;
  font-weight: 700;
  letter-spacing: 0.02em;
  list-style: none;
}

/* G11 — ô tìm phiên theo tiêu đề (lọc client-side trên list đã tải):
   nằm ngay trên list, cùng chiều rộng sidebar. */
.session-search {
  padding: 0 8px 4px;
}

.session-search input {
  width: 100%;
  min-height: 36px;
  padding: 8px 12px;
  border-radius: 10px;
  border: 1px solid var(--line);
  background: var(--bg-elev);
  color: var(--ink);
  font: inherit;
  font-size: 14px;
  box-sizing: border-box;
}

.session-search input::placeholder {
  color: var(--muted);
}

.session-search input:focus {
  outline: 2px solid var(--accent);
  outline-offset: 0;
  border-color: var(--accent);
}

.session-item {
  display: flex;
  align-items: stretch;
  gap: 4px;
  border-radius: 12px;
}

.session-open {
  flex: 1;
  min-width: 0;
  display: flex;
  flex-direction: column;
  align-items: flex-start;
  gap: 2px;
  padding: 10px 12px;
  border-radius: 12px;
  border: 1px solid transparent;
  background: transparent;
  color: var(--ink);
  text-align: left;
  cursor: pointer;
  position: relative;
}

/* Chấm chưa đọc (live sync): dot trơn 8px ở góc phải hàng phiên,
   cùng màu accent với trạng thái đang chọn — không đếm số. */
.session-unread-dot {
  position: absolute;
  top: 12px;
  right: 12px;
  width: 8px;
  height: 8px;
  border-radius: 50%;
  background: var(--accent);
}

.session-open:hover {
  background: rgba(243, 236, 227, 0.06);
}

.session-open.is-active {
  background: color-mix(in srgb, var(--accent) 16%, var(--bg-elev));
  border-color: color-mix(in srgb, var(--accent) 45%, var(--line));
}

.session-title,
.session-preview,
.session-meta {
  max-width: 100%;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.session-title {
  font-size: 14px;
  font-weight: 700;
}

.session-preview {
  color: var(--muted);
  font-size: 13px;
}

.session-meta {
  color: var(--muted);
  font-size: 12px;
}

/* Nút ⋯ mở menu phiên: 44x44, chỉ hiện khi hover/focus vào item
   (desktop) hoặc luôn hiện trên hàng đang chọn; màn cảm ứng
   (hover: none) luôn hiện vì không có hover. 1 nút duy nhất thay
   2 nút bút/chổi thường trực để hàng thread lấy lại ~48px. */
.session-menu {
  flex: 0 0 auto;
  align-self: center;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  width: 44px;
  min-height: 44px;
  border-radius: 12px;
  border: 1px solid transparent;
  background: transparent;
  color: var(--muted);
  cursor: pointer;
  opacity: 0;
}

.session-item:hover .session-menu,
.session-item:focus-within .session-menu,
.session-item.is-active .session-menu {
  opacity: 1;
}

.session-menu:hover {
  color: var(--ink);
  border-color: var(--line);
}

/* Đang đổi tên inline: chỉ input hiện, nút mở + nút ⋯ ẩn đi để
   input chiếm trọn chiều rộng item, không vỡ layout sidebar/mobile. */
.session-item.is-renaming .session-open,
.session-item.is-renaming .session-menu {
  display: none;
}

.session-rename-input {
  flex: 1;
  min-width: 0;
  min-height: 44px;
  margin: 0;
  padding: 10px 12px;
  border-radius: 12px;
  border: 1px solid var(--accent);
  background: var(--bg-elev);
  color: var(--ink);
  font: inherit;
  font-size: 14px;
  font-weight: 700;
  box-sizing: border-box;
}

.session-rename-input:disabled {
  opacity: 0.65;
}

/* G33: trạng thái "Đang lưu…" lúc PATCH đổi tên đang chạy — nằm cạnh
   input đã khóa trong cùng hàng phiên, không vỡ layout. */
.session-rename-saving {
  flex: 0 0 auto;
  align-self: center;
  padding: 0 4px;
  color: var(--muted);
  font-size: 14px;
  font-style: italic;
  white-space: nowrap;
}

@media (hover: none) {
  .session-menu {
    opacity: 1;
  }
}

.sidebar-backdrop {
  position: fixed;
  inset: 0;
  z-index: 55;
  background: rgba(0, 0, 0, 0.5);
}

/* Panel phải kiểu Muse (xem docs/right-panel.md): desktop >=1400px là
   cột phải 300px trong grid .app (cột `auto` thứ 3 — đóng thì
   display:none co về 0, chat giãn ra); dưới 1400px là drawer trượt
   từ phải + backdrop, giống sidebar mobile nhưng ở phía phải.
   Các khối chat vẫn ở cột 2 nên topbar/session-bar/thread/composer
   thẳng hàng như cũ, không bị panel chen. */
.right-panel {
  grid-column: 3;
  grid-row: 2 / -1;
  /* position:relative: neo tay kéo giãn (.panel-resize-handle) ở
     mép trái; chế độ drawer ghi đè bằng position:fixed. */
  position: relative;
  width: 300px;
  min-height: 0;
  display: flex;
  flex-direction: column;
  background: var(--bg-elev);
  border-left: 1px solid var(--line);
  overflow: hidden;
}

/* Tay kéo giãn panel (desktop, chế độ grid): vùng bắt 8px ở mép
   giáp cột chat, vạch accent chỉ hiện khi hover/kéo/focus. Đánh
   đổi đã biết: vùng bắt phủ lên mép phải của thanh cuộn sidebar
   (8px) nên kéo thumb chuột ở đúng mép đó bị chặn — cuộn lăn/phím
   vẫn chạy, list phiên ngắn nên chấp nhận được; panel phải không
   bị vì tay kéo ở mép trái, thanh cuộn ở mép phải. Ẩn ở chế độ
   drawer (xem media query 639px/1399px dưới). */
.panel-resize-handle {
  position: absolute;
  top: 0;
  bottom: 0;
  width: 8px;
  z-index: 6;
  cursor: ew-resize;
  touch-action: none;
  outline: none;
}

.sidebar .panel-resize-handle {
  right: 0;
}

.right-panel .panel-resize-handle {
  left: 0;
}

.panel-resize-handle::after {
  content: '';
  position: absolute;
  top: 0;
  bottom: 0;
  left: 50%;
  width: 2px;
  transform: translateX(-50%);
  background: transparent;
  transition: background 0.15s ease;
}

.panel-resize-handle:hover::after,
.panel-resize-handle.is-dragging::after,
.panel-resize-handle:focus-visible::after {
  background: var(--accent);
}

/* Đang kéo: con trỏ ew-resize khắp trang kể cả khi chuột chạy qua
   cột chat (pointer capture giữ drag, con trỏ theo class body). */
body.is-resizing-panel,
body.is-resizing-panel * {
  cursor: ew-resize !important;
}

.right-panel-head {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 8px;
  padding: 12px 12px 12px 16px;
  border-bottom: 1px solid var(--line);
}

.right-panel-head h2 {
  margin: 0;
  font-size: 15px;
}

.right-panel-close {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  min-width: 44px;
  min-height: 44px;
  border-radius: 12px;
  border: 1px solid var(--line);
  background: transparent;
  color: var(--ink);
  cursor: pointer;
}

.right-panel-body {
  flex: 1;
  min-height: 0;
  overflow-y: auto;
  padding: 4px 16px 16px;
}

.rp-section {
  padding: 18px 0;
  border-bottom: 1px solid var(--line);
}

.rp-section:last-child {
  border-bottom: 0;
}

.rp-section h3 {
  margin: 0 0 10px;
  font-size: 13px;
  text-transform: uppercase;
  letter-spacing: 0.06em;
  color: var(--muted);
}

/* Khối con trong một nhóm (vd Vault + Ánh xạ title, Phiên +
   Thông tin phiên — xem docs/right-panel.md §1): tách khỏi khối
   trên bằng vạch nhạt + khoảng trống, tiêu đề con dùng .rp-subhead
   (13px đậm, không caps) để phân tầng rõ với tiêu đề nhóm (h3
   caps, muted). Nhóm vẫn là MỘT .rp-section duy nhất — vạch này
   nhẹ hơn vạch giữa các nhóm: 55% độ đậm của --line (color-mix
   với transparent), trong khi vạch giữa các nhóm dùng --line
   nguyên (100%). */
.rp-subsection {
  margin-top: 16px;
  padding-top: 16px;
  border-top: 1px solid color-mix(in srgb, var(--line) 55%, transparent);
}

.rp-subsection > .rp-subhead {
  margin-top: 0;
}

.rp-field {
  display: flex;
  flex-direction: column;
  gap: 6px;
  margin-bottom: 10px;
  font-size: 13px;
  font-weight: 600;
}

.rp-field select {
  min-height: 44px;
  width: 100%;
  box-sizing: border-box;
  padding: 10px 12px;
  border: 1px solid var(--line);
  border-radius: 8px;
  background: var(--bg);
  color: var(--text);
  font: inherit;
  font-size: 14px;
  font-weight: 400;
}

.rp-note {
  margin: 4px 0 0;
  color: var(--muted);
  font-size: 12px;
  line-height: 1.5;
}

/* Section "Phiên" trong panel phải — bản mobile của session-bar
   (docs/right-panel.md § mobile): 2 cột, nút min-height 44px, quota là
   dòng muted. Chỉ hiện <640px: desktop đã có session-bar nên ẩn hẳn
   (xem @media min-width:640px). */
.rp-quota {
  margin: 0 0 10px;
  color: var(--muted);
  font-size: 13px;
}

.rp-action-list {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 8px;
}

.rp-action-list button,
.rp-action-list a {
  min-height: 44px;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  padding: 0 12px;
  border: 1px solid var(--line);
  border-radius: 8px;
  background: var(--bg);
  color: var(--ink);
  font: inherit;
  font-size: 14px;
  text-decoration: none;
  cursor: pointer;
}

.rp-action-list button:hover,
.rp-action-list a:hover {
  border-color: color-mix(in srgb, var(--accent) 45%, var(--line));
}

.rp-memory-list,
.rp-skill-list,
.rp-tool-list {
  list-style: none;
  margin: 0;
  padding: 0;
  display: flex;
  flex-direction: column;
  gap: 8px;
}

.rp-memory-item,
.rp-skill-item,
.rp-tool-item {
  border: 1px solid var(--line);
  border-radius: 8px;
  padding: 8px 10px;
  display: flex;
  flex-direction: column;
  gap: 6px;
  word-break: break-word;
}

/* Mục HOẠT ĐỘNG (live sync): phiên đang làm việc ở tab/trình duyệt
   khác (hoặc tab này) — bấm vào mở đúng phiên đó. */
.rp-activity-list {
  list-style: none;
  margin: 0;
  padding: 0;
  display: flex;
  flex-direction: column;
  gap: 8px;
}

.rp-activity-item {
  display: flex;
  align-items: flex-start;
  gap: 8px;
  padding: 8px 10px;
  border: 1px solid var(--line);
  border-radius: 10px;
  cursor: pointer;
}

.rp-activity-item:hover {
  background: rgba(243, 236, 227, 0.06);
}

.rp-activity-dot {
  flex: none;
  width: 8px;
  height: 8px;
  margin-top: 6px;
  border-radius: 50%;
  background: var(--accent);
}

.rp-activity-body {
  min-width: 0;
}

.rp-activity-title {
  font-size: 13px;
  font-weight: 700;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.rp-activity-label {
  color: var(--muted);
  font-size: 12px;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.rp-skill-item.is-current,
.rp-tool-item.is-current {
  border-color: color-mix(in srgb, var(--accent) 45%, var(--line));
  background: color-mix(in srgb, var(--accent) 8%, transparent);
}

.rp-item-btn {
  min-height: 44px;
  padding: 0;
  border: 0;
  background: transparent;
  color: var(--ink);
  font: inherit;
  text-align: left;
  cursor: pointer;
}

.rp-item-name {
  display: block;
  font-size: 14px;
  font-weight: 700;
}

.rp-item-desc {
  display: block;
  margin-top: 2px;
  color: var(--muted);
  font-size: 13px;
  line-height: 1.45;
}

.rp-item-badge {
  align-self: flex-start;
  background: rgba(217, 120, 58, 0.16);
  color: #d9783a;
  border-radius: 999px;
  padding: 2px 10px;
  font-size: 12px;
  font-weight: 600;
  white-space: nowrap;
}

.rp-info {
  margin: 0;
  display: flex;
  flex-direction: column;
  gap: 8px;
  font-size: 14px;
}

.rp-info > div {
  display: flex;
  justify-content: space-between;
  gap: 12px;
}

.rp-info dt {
  flex: 0 0 auto;
  color: var(--muted);
}

.rp-info dd {
  margin: 0;
  min-width: 0;
  text-align: right;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.rp-info dd.rp-wrap {
  white-space: normal;
  word-break: break-word;
}

.rp-status-dot {
  display: inline-block;
  width: 8px;
  height: 8px;
  border-radius: 50%;
  margin-right: 6px;
  background: #8fbf7a;
  vertical-align: middle;
}

.rp-status-dot.is-off {
  background: var(--muted);
}

/* Kết nối per-user (xem docs/connectors.md §0): form nhập bot token
   của user trong panel phải, theo mẫu memory-form (cột, gap 8px). */
.rp-connector {
  display: flex;
  flex-direction: column;
  gap: 8px;
}

.rp-connector-row {
  display: flex;
  align-items: center;
  gap: 4px;
  margin: 0;
}

.rp-field-label {
  font-size: 13px;
  color: var(--muted);
}

.rp-connector-input {
  min-height: 44px;
  width: 100%;
  box-sizing: border-box;
  padding: 10px 12px;
  border: 1px solid var(--line);
  border-radius: 8px;
  background: var(--bg);
  color: var(--ink);
  font: inherit;
}

.rp-checkbox {
  display: flex;
  align-items: center;
  gap: 8px;
  font-size: 14px;
  cursor: pointer;
}

.rp-connector-actions {
  display: flex;
  gap: 8px;
  align-items: center;
}

.rp-connector-actions .drawer-new {
  margin-top: 0;
  flex: 1;
}

.rp-connector-actions button:not(.drawer-new) {
  min-height: 44px;
  padding: 0 12px;
  border: 1px solid var(--line);
  border-radius: 12px;
  background: var(--bg);
  color: var(--ink);
  font: inherit;
  font-size: 14px;
  cursor: pointer;
}

.rp-connector-actions button:disabled {
  opacity: 0.5;
  cursor: default;
}

/* Multi-account (§0.1): danh sách tài khoản đã lưu trong mỗi connector.
   Hàng nào cũng có dot trạng thái + tên + badge; bấm vào tên thì mở
   form sửa, bấm "Dùng" thì chọn tài khoản đó mà không cần mở form. */
.rp-connector-list {
  display: flex;
  flex-direction: column;
  gap: 6px;
}

.rp-connector-list:empty {
  display: none;
}

/* Hàng PHẢI wrap: tài khoản vừa mặc định vừa đang dùng có tới 3 pill
   (Mặc định + Đang dùng + nút Đang dùng) — tổng chiều rộng tối thiểu
   của chúng đã vượt lòng panel 300px/320px nên hàng nowrap đẩy nút
   lòi qua mép phải và bị .right-panel (overflow:hidden) cắt (dính
   thật 17/11: lòi 42.8px ở 1920, 22.8px ở drawer 320px). flex-wrap +
   tên co giãn với basis 120px: pill thừa tự xuống dòng 2, không pill
   nào bị cắt ở mọi breakpoint — xem verify-right-panel-polish.mjs. */
.rp-connector-item {
  display: flex;
  align-items: center;
  flex-wrap: wrap;
  gap: 8px;
  padding: 8px 10px;
  border: 1px solid var(--line);
  border-radius: 8px;
  background: var(--bg);
  font-size: 14px;
}

.rp-connector-item.is-selected {
  border-color: #d9783a;
}

.rp-connector-item .rp-item-name {
  flex: 1 1 120px;
  min-width: 0;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
  cursor: pointer;
}

.rp-connector-item .rp-item-badge {
  flex: 0 0 auto;
  align-self: center;
}

.rp-connector-item .rp-status-dot {
  margin-right: 0;
  flex: 0 0 auto;
}

.rp-connector-item button {
  flex: 0 0 auto;
  min-height: 32px;
  padding: 0 10px;
  border: 1px solid var(--line);
  border-radius: 8px;
  background: var(--bg);
  color: var(--ink);
  font: inherit;
  font-size: 13px;
  cursor: pointer;
}

.rp-connector-item button:disabled {
  opacity: 0.5;
  cursor: default;
}

.rp-n8n {
  margin: 10px 0 0;
  font-size: 13px;
  line-height: 1.5;
}

.right-panel-backdrop {
  position: fixed;
  inset: 0;
  z-index: 55;
  background: rgba(0, 0, 0, 0.5);
}

/* Picker chat trong panel (từ 07/11: 4 picker rời topbar vào đây):
   list thả xuống thành inline (position:static) vì .right-panel-body
   có overflow-y:auto sẽ clip list absolute; nút picker full-width. */
.rp-picker-field {
  margin-bottom: 10px;
}

.rp-picker-field > span {
  display: block;
  margin-bottom: 6px;
  font-size: 13px;
  color: var(--muted);
}

.right-panel .agent-picker,
.right-panel .skill-picker,
.right-panel .model-picker,
.right-panel .speed-picker {
  display: block;
  flex: none;
}

.right-panel .agent-btn,
.right-panel .skill-btn,
.right-panel .model-btn,
.right-panel .speed-btn {
  width: 100%;
  max-width: none;
}

.right-panel .agent-list,
.right-panel .skill-list,
.right-panel .model-list,
.right-panel .speed-list {
  position: static;
  width: 100%;
  min-width: 0;
  max-width: none;
  margin-top: 6px;
}

/* Mục Kỹ năng trong panel chỉ hiển thị (không bấm): bỏ cursor pointer
   của .rp-item-btn để không gợi ý tương tác. */
.rp-skill-item .rp-item-btn {
  cursor: default;
}

/* Mobile: topbar một hàng sau khi picker rời header — padding dọc 6px
   (ước tính, đo số thật ở 390px khi verify). */
@media (max-width: 639px) {
  .topbar {
    padding-top: calc(6px + var(--safe-top));
    padding-bottom: 6px;
  }
}

@media (min-width: 1400px) {
  /* Desktop: đóng panel = ẩn hẳn khỏi grid (cột auto co về 0). Quy
     tắc này CHỈ trong min-width:1400 — dưới breakpoint panel là
     drawer (display:flex + translateX) nên class collapsed không
     được ẩn nó. */
  .app.right-panel-collapsed .right-panel {
    display: none;
  }
}

@media (max-width: 1399px) {
  /* Drawer: panel rời khỏi grid (position:fixed) nên cột `auto` thứ
     3 co về 0, chat không bị bóp; mở/đóng bằng translateX giống
     sidebar mobile. Đóng thì visibility:hidden (kèm delay = thời
     gian trượt) để các control bên trong không còn tab-focus được —
     chỉ transform thì chúng vẫn nằm trong tab order (NIT-1). */
  .right-panel {
    position: fixed;
    top: 0;
    right: 0;
    bottom: 0;
    z-index: 60;
    width: min(320px, 88vw);
    transform: translateX(105%);
    transition: transform 0.2s ease, visibility 0s linear 0.2s;
    visibility: hidden;
    box-shadow: var(--shadow);
  }

  .app.right-panel-mobile-open .right-panel {
    transform: translateX(0);
    transition: transform 0.2s ease, visibility 0s;
    visibility: visible;
  }

  /* Drawer: không kéo giãn (chỉ desktop grid mới có tay kéo). */
  .right-panel .panel-resize-handle {
    display: none;
  }
}

@media (max-width: 639px) {
  .app {
    grid-template-columns: minmax(0, 1fr);
  }

  /* Mobile chỉ có 1 cột: các khối chat PHẢI về cột 1 — giữ
     grid-column: 2 (của desktop) sẽ đẩy chúng sang cột implicit
     auto, bóp cột 1fr về ~0 và topbar co lại còn ~24px (dính
     06/11: #speed-name width 0, topbar lệch xuống y≈391). */
  .app > .session-bar,
  .app > .search-bar,
  .app > .compact-note,
  .app > .thread,
  .app > .composer-dock {
    grid-column: 1;
  }

  /* Mobile: sidebar là overlay cố định trượt từ trái (kiểu sidebar
     mobile của Muse); class sidebar-collapsed của desktop không được
     ẩn nó — trạng thái mobile do sidebar-mobile-open quyết. */
  .sidebar {
    position: fixed;
    top: 0;
    left: 0;
    bottom: 0;
    z-index: 60;
    width: min(300px, 85vw);
    transform: translateX(-105%);
    transition: transform 0.2s ease;
    box-shadow: var(--shadow);
  }

  .app.sidebar-mobile-open .sidebar {
    transform: translateX(0);
  }

  .app.sidebar-collapsed .sidebar {
    display: flex;
  }

  /* Drawer: không kéo giãn (chỉ desktop grid mới có tay kéo). */
  .sidebar .panel-resize-handle {
    display: none;
  }

  .sidebar-close {
    display: inline-flex;
  }
}

/* Overlay dialog chung: panel lịch sử version + dialog chia sẻ phiên.
   z-index 65 > drawer panel phải (60) để nút dialog không bị panel
   che ở ≤1399px (G27: dialog render bên dưới, nút không bấm được),
   < context-menu (70) < modal (100). Đánh đổi có chủ ý: toast (50)
   nằm DƯỚI overlay nên toast bắn ra trong lúc dialog đang mở (vd
   "Đã tạo link chia sẻ phiên.") hiện mờ qua nền backdrop — vẫn đọc
   được (nền toast đặc), tự tắt sau 4s; không nâng toast lên trên vì
   toast phải ở dưới drawer (60) để không che tiêu đề drawer. */
.overlay {
  position: fixed;
  inset: 0;
  z-index: 65;
  display: grid;
  place-items: center;
  padding: 16px;
  background: rgba(0, 0, 0, 0.5);
}

.overlay[hidden],
.drawer-overlay[hidden] {
  display: none !important;
}

.panel-card {
  width: min(480px, 100%);
  max-height: min(80dvh, 640px);
  overflow-y: auto;
  background: var(--bg-elev);
  border: 1px solid var(--line);
  border-radius: 18px;
  padding: 16px;
  box-shadow: var(--shadow);
}

.panel-head {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
}

.panel-head h2 {
  margin: 0;
  font-size: 17px;
}

.panel-btn {
  min-height: 44px;
  padding: 0 14px;
  border-radius: 12px;
  border: 1px solid var(--line);
  background: var(--bg);
  color: var(--ink);
  font-weight: 600;
  cursor: pointer;
}

.panel-btn:hover {
  border-color: color-mix(in srgb, var(--accent) 45%, var(--line));
}

.panel-btn.primary {
  border: 0;
  background: var(--accent);
  color: var(--accent-ink);
}

.panel-btn.danger {
  border-color: color-mix(in srgb, var(--danger) 60%, var(--line));
  color: var(--danger);
}

.panel-actions {
  display: flex;
  flex-wrap: wrap;
  gap: 8px;
  margin-top: 16px;
}

.share-link-row {
  display: flex;
  gap: 8px;
  margin-top: 12px;
}

.share-link-row input {
  flex: 1 1 auto;
  min-width: 0;
  min-height: 44px;
  border-radius: 12px;
  border: 1px solid var(--line);
  background: var(--bg);
  padding: 10px 12px;
  font-size: 14px;
}

.version-list {
  margin: 12px 0 0;
  padding: 0;
  list-style: none;
  display: grid;
  gap: 12px;
}

.version-item {
  display: grid;
  gap: 6px;
  padding: 12px;
  border-radius: 12px;
  border: 1px solid var(--line);
  background: var(--bg);
}

.version-item.is-current {
  border-color: color-mix(in srgb, var(--accent) 55%, var(--line));
}

.version-text {
  margin: 0;
  white-space: pre-wrap;
  overflow-wrap: anywhere;
  font-size: 14px;
  line-height: 1.5;
}

.version-meta {
  margin: 0;
  color: var(--muted);
  font-size: 12px;
}

/* Chế độ share public: không composer, không session-bar. */
.share-mode .composer-dock,
.share-mode .session-bar {
  display: none !important;
}

@media (max-width: 639px) {
  .drawer {
    width: 100vw;
    border-left: 0;
  }
  .share-link-row {
    flex-wrap: wrap;
  }
  .share-link-row input {
    flex-basis: 100%;
  }
}

.bubble-action:hover,
.edit-actions button:hover {
  color: var(--ink);
}

.edit-box {
  display: grid;
  gap: 8px;
  width: 100%;
}

.edit-box textarea {
  width: 100%;
  min-height: 72px;
  border-radius: 12px;
  border: 1px solid var(--line);
  background: var(--bg);
  padding: 8px 10px;
  resize: vertical;
  font-size: 15px;
}

.edit-actions {
  display: flex;
  justify-content: flex-end;
  gap: 12px;
}

.gate {
  position: fixed;
  inset: 0;
  display: grid;
  place-items: center;
  padding: calc(24px + var(--safe-top)) 16px calc(24px + var(--safe-bottom));
  background:
    radial-gradient(circle at top, rgba(217, 120, 58, 0.18), transparent 38%),
    var(--bg);
}

.gate[hidden] {
  display: none !important;
}

/* Overlay kiểm tra auth (chuẩn gate 12/11): hiện từ lần vẽ đầu tiên,
   che toàn trang + chặn thao tác cho tới khi trạng thái auth đã biết.
   Form đăng nhập (#gate) mặc định hidden trong HTML và chỉ hiện khi đã
   biết là chưa đăng nhập — không bao giờ vẽ form trong lúc chưa biết.
   z-index 100 > mọi drawer/toast/menu (tối đa 70) để không gì lọt lên trên. */
.auth-check {
  position: fixed;
  inset: 0;
  z-index: 100;
  display: grid;
  place-items: center;
  padding: 24px 16px;
  background: var(--bg);
  background: color-mix(in srgb, var(--bg) 88%, transparent);
}

.auth-check[hidden] {
  display: none !important;
}

.auth-check-card {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 14px;
  width: min(100%, 320px);
  background: var(--bg-elev);
  border: 1px solid var(--line);
  border-radius: 20px;
  padding: 28px 24px;
  box-shadow: var(--shadow);
  text-align: center;
}

.auth-check-card p {
  margin: 0;
  color: var(--muted);
  line-height: 1.5;
}

.auth-check-spinner {
  width: 28px;
  height: 28px;
  border-radius: 50%;
  border: 3px solid var(--line);
  border-top-color: var(--accent);
  animation: auth-check-spin 0.8s linear infinite;
}

@keyframes auth-check-spin {
  to {
    transform: rotate(360deg);
  }
}

.gate-card {
  display: flex;
  flex-direction: column;
  gap: 12px;
  width: min(100%, 420px);
  background: var(--bg-elev);
  border: 1px solid var(--line);
  border-radius: 24px;
  padding: 24px;
  box-shadow: var(--shadow);
}

.gate-kicker {
  margin: 0 0 8px;
  letter-spacing: 0.14em;
  text-transform: uppercase;
  font-size: 11px;
  color: var(--accent);
}

.gate-card h1 {
  margin: 0 0 8px;
  font-size: 28px;
}

.gate-copy {
  color: var(--muted);
  line-height: 1.5;
}

.gate-card label {
  display: block;
  margin: 4px 0 0;
  font-size: 13px;
}

.error {
  margin: 0;
  color: var(--danger);
  line-height: 1.5;
}

.sso-divider {
  display: flex;
  align-items: center;
  gap: 12px;
  color: var(--muted);
  font-size: 13px;
}

.sso-divider::before,
.sso-divider::after {
  content: '';
  flex: 1;
  height: 1px;
  background: var(--line);
}

.sso-divider[hidden] {
  display: none;
}

.gate-card .sso-btn {
  background: transparent;
  border: 1px solid var(--line);
  color: inherit;
}

.typing {
  color: var(--muted);
  font-style: italic;
  animation: typing-pulse 1.6s ease-in-out infinite;
}

@keyframes typing-pulse {
  0%, 100% {
    opacity: 0.55;
  }
  50% {
    opacity: 1;
  }
}

.quota-chip {
  max-width: 42vw;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

a.ghost-btn {
  display: inline-grid;
  place-items: center;
  text-decoration: none;
}

.session-bar {
  display: flex;
  align-items: center;
  justify-content: flex-end;
  flex-wrap: wrap;
  gap: 4px 14px;
  padding: 0 16px 10px;
  color: var(--muted);
  font-size: 13px;
  border-bottom: 1px solid var(--line);
}

/* Session-bar: 5 mục thẳng hàng trên cùng 1 baseline.
   Gốc lệch: Quản trị là <a> (chữ inline, line box ngồi ở đầu content box
   sau padding-top) trong khi các nút <button> được UA căn chữ giữa hộp —
   cùng min-height/padding nhưng chữ vẫn lệch nhau. Fix: cho cả <a> lẫn
   <button> (và cả quota-chip) cùng display:inline-flex + align-items:center
   để chữ được căn giữa hộp theo cùng 1 cách, cùng line-height, cùng
   min-height 44px. Không đổi <a> thành <button> vì <a href="/admin"> vẫn
   là link điều hướng đúng nghĩa (mở tab mới được, SEO/a11y). */
.session-bar a,
.session-bar button {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  line-height: 1.4;
  border: 0;
  background: none;
  color: var(--muted);
  padding: 6px 8px;
  min-height: 44px;
  cursor: pointer;
  text-decoration: none;
}

.session-bar .quota-chip {
  display: inline-flex;
  align-items: center;
  min-height: 44px;
}

.session-bar a:hover,
.session-bar button:hover {
  color: var(--ink);
}

.admin-app {
  grid-template-rows: auto auto 1fr;
  /* Trang admin chỉ có 1 cột (topbar / tabs / main xếp dọc): phải ghi đè
     cả grid-template-columns 3 cột của .app (dành cho khung chat), nếu
     không .tabs và .admin-main auto-place chung hàng 2 — nav bị kéo giãn
     bằng chiều cao nội dung, nút tab đang chọn (nền cam) thành vệt dọc
     cao cả trang, còn main bị ép vào cột hẹp bên phải. */
  grid-template-columns: minmax(0, 1fr);
}

.tabs {
  display: flex;
  gap: 6px;
  overflow-x: auto;
  padding: 10px 16px 0;
}

.admin-main {
  padding: 16px;
}

.panel.is-hidden,
.is-hidden {
  display: none !important;
}

.stack {
  display: grid;
  gap: 12px;
  margin-top: 12px;
}

.card {
  background: var(--bg-elev);
  border: 1px solid var(--line);
  border-radius: 18px;
  padding: 16px;
}

.card h2 {
  margin: 0 0 8px;
  font-size: 16px;
}

.card label {
  display: block;
  margin-top: 10px;
  color: var(--muted);
  font-size: 13px;
}

.muted {
  margin: 0;
  color: var(--muted);
  white-space: pre-wrap;
  overflow-wrap: anywhere;
}

.row {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 8px;
  margin-top: 10px;
}

.card input,
.card select,
.card button,
.gate-card select {
  width: 100%;
  min-height: 44px;
  border-radius: 14px;
  border: 1px solid var(--line);
  background: var(--bg);
  padding: 10px 12px;
  font-size: 16px;
}

.card button,
.gate-card button {
  margin-top: 16px;
  border: 0;
  background: var(--accent);
  color: var(--accent-ink);
  font-weight: 700;
  cursor: pointer;
}

.card .row button {
  margin-top: 0;
}

/* Breakpoints: mobile <640px, tablet 640–1024px, desktop >1024px. */
@media (max-width: 639px) {
  /* Mobile gọn 1 hàng (docs/right-panel.md § mobile): subtitle ẩn nên
     brand chỉ còn logo + title; mode-switch quay lại cùng hàng, không
     xuống hàng riêng nữa. Giữ padding-top safe-area ở block 639px phía
     trên (6px + var(--safe-top)). */
  .topbar {
    flex-wrap: nowrap;
    gap: 8px;
    padding-left: 12px;
    padding-right: 12px;
  }
  .subtitle {
    display: none;
  }
  .title {
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
  }
  /* Mobile: 4 picker xếp 2 cái/hàng thay vì 1 cái/hàng (xem
     docs/mobile-composer-fix.md): topbar từ 6 hàng (337px, 40% màn
     390×844) xuống 4 hàng (~230px), thread lấy lại ~100px. KHÔNG dùng
     strip cuộn ngang cho picker: strip cần `overflow-x: auto` mà
     dropdown của picker (`position: absolute`) sẽ bị cắt dọc theo mép
     strip. Nút bên trong vẫn `width: 100%`, chữ ellipsis như cũ.
     Basis `calc(50% - 6px)` trừ đúng nửa gap 12px của topbar: 2 cái +
     gap = đúng 100% bề rộng content — trừ 4px thì 2 cái + gap vượt
     366px ở viewport 390px nên vẫn rớt xuống 1 cái/hàng. */
  /* Mobile hẹp (360px): nút tìm kiếm rớt xuống hàng riêng vì brand
     (basis auto = subtitle nowrap dài) không co đủ — topbar thêm 1 hàng
     (285px thay vì 233px). Basis 0 + grow: brand lấy đúng phần còn lại
     sau hamburger + tìm kiếm, subtitle ellipsis như cũ. */
  .brand {
    flex: 1 1 0;
  }
  /* Mobile 1 hàng: top-actions là flex item thường (bỏ order 2 +
     flex 1 1 100% bắt mode-switch xuống hàng riêng). */
  .top-actions {
    order: 0;
    flex: 0 0 auto;
  }
  .mode-btn {
    padding: 0 10px;
  }
  .agent-picker {
    order: 3;
    flex: 1 1 calc(50% - 6px);
    justify-content: stretch;
  }
  .agent-btn {
    width: 100%;
    max-width: none;
  }
  .agent-list {
    left: 0;
    right: 0;
    min-width: 0;
  }
  .skill-picker {
    order: 4;
    flex: 1 1 calc(50% - 6px);
    justify-content: stretch;
  }
  .skill-btn {
    width: 100%;
    max-width: none;
  }
  .skill-list {
    left: 0;
    right: 0;
    min-width: 0;
  }
  .model-picker {
    order: 5;
    flex: 1 1 calc(50% - 6px);
    justify-content: stretch;
  }
  .model-btn {
    width: 100%;
    max-width: none;
  }
  .model-list {
    left: 0;
    right: 0;
    min-width: 0;
  }
  .speed-picker {
    order: 6;
    flex: 1 1 calc(50% - 6px);
    justify-content: stretch;
  }
  .speed-btn {
    width: 100%;
    max-width: none;
  }
  .speed-list {
    left: 0;
    right: 0;
    min-width: 0;
  }
  .thread {
    padding: 12px 12px 8px;
  }
  .search-bar {
    padding-left: 12px;
    padding-right: 12px;
  }
  .composer-dock {
    padding-left: 12px;
    padding-right: 12px;
  }
  /* Mobile: session-bar ẩn hẳn — các nút đã có bản trong section
     "Phiên" của panel phải; grid-row 2 (auto) co về 0, thread giãn ra.
     Các rule .session-bar bên dưới thành dead code trên mobile, để yên. */
  #session-bar {
    display: none;
  }
  .session-bar {
    padding-left: 12px;
    padding-right: 12px;
    /* Mobile: session-bar 1 hàng cuộn ngang thay vì wrap 2 hàng (103px
       → ~54px, xem docs/mobile-composer-fix.md). Chỉ chứa nút bấm, không
       có dropdown nên `overflow-x` an toàn; ẩn thanh cuộn cho gọn. */
    flex-wrap: nowrap;
    justify-content: flex-start;
    overflow-x: auto;
    scrollbar-width: none;
  }
  .session-bar::-webkit-scrollbar {
    display: none;
  }
  .session-bar a,
  .session-bar button,
  .session-bar .quota-chip {
    flex: 0 0 auto;
  }
  .tabs {
    padding-left: 12px;
    padding-right: 12px;
  }
  .admin-main {
    padding: 12px;
  }
  .row {
    grid-template-columns: 1fr;
  }
}

@media (min-width: 640px) {
  /* Desktop đã có session-bar: khối nút "Phiên" trong nhóm Phiên
     của panel phải chỉ phục vụ mobile nên ẩn hẳn (không đổi
     desktop) — chỉ ẩn khối nút (.rp-session-actions), nhóm Phiên
     vẫn hiện với khối con "Thông tin phiên" bên dưới. */
  .rp-session-actions {
    display: none;
  }
  /* Full-width (docs/container-width.md): app chiếm toàn bộ viewport,
     không trần, không viền khung hai bên — trần 1600px cũ để thừa
     160px mỗi bên trên màn 1920px. `.app` giữ `width: 100%` từ quy
     tắc gốc, không cần ghi đè ở đây. Sidebar giữ 280px, cột chat lấy
     hết phần còn lại; sidebar đóng thì cột chat giãn thêm đúng 280px. */
  .thread {
    padding: 20px 20px 8px;
  }
}

@media (min-width: 1024px) {
  .thread {
    padding: 24px 24px 12px;
    gap: 14px;
  }
  .bubble-body {
    font-size: 16px;
  }
  .title {
    font-size: 16px;
  }
  .subtitle {
    font-size: 13px;
  }
}

/* --- Bộ nhớ + Compact (docs/agent-memory-compact.md) --- */
/* Nút Bộ nhớ / Tóm tắt nằm trong session-bar: min-height 44px đã có từ
   quy tắc chung `.session-bar button`. Ghi chú compact nằm ngay dưới
   session-bar, bấm để xem/thu gọn bản tóm tắt. */
.compact-note {
  margin: 0 12px;
  padding: 8px 12px;
  border: 1px solid var(--line);
  border-left: 3px solid #d9783a;
  border-radius: 8px;
  background: rgba(217, 120, 58, 0.08);
  color: var(--text);
  font-size: 13px;
  line-height: 1.5;
  cursor: pointer;
  word-break: break-word;
}
.compact-note[hidden] {
  display: none;
}
.compact-note-full {
  display: block;
  margin-top: 6px;
  color: var(--muted);
  white-space: pre-wrap;
}
.compact-note-full[hidden] {
  display: none;
}

/* Drawer Bộ nhớ: tái dùng khung .drawer; form + item đều >= 44px. */
.memory-form {
  display: flex;
  flex-direction: column;
  gap: 8px;
  margin-bottom: 12px;
}
.memory-form textarea,
.memory-form select {
  min-height: 44px;
  padding: 10px 12px;
  border: 1px solid var(--line);
  border-radius: 8px;
  background: var(--bg);
  color: var(--text);
  font: inherit;
  font-size: 14px;
  width: 100%;
  box-sizing: border-box;
}
.memory-list {
  list-style: none;
  margin: 0;
  padding: 0;
  display: flex;
  flex-direction: column;
  gap: 10px;
}
.memory-item {
  border: 1px solid var(--line);
  border-radius: 8px;
  padding: 10px 12px;
  display: flex;
  flex-direction: column;
  gap: 8px;
  word-break: break-word;
}
.memory-head {
  display: flex;
  align-items: center;
  gap: 8px;
}
.memory-type {
  background: rgba(217, 120, 58, 0.16);
  color: #d9783a;
  border-radius: 999px;
  padding: 2px 10px;
  font-size: 12px;
  font-weight: 600;
  white-space: nowrap;
}
.memory-meta {
  color: var(--muted);
  font-size: 12px;
}
.memory-text {
  margin: 0;
  font-size: 14px;
  line-height: 1.5;
  white-space: pre-wrap;
}
.memory-actions {
  display: flex;
  gap: 8px;
  flex-wrap: wrap;
}
.memory-btn {
  min-height: 44px;
  min-width: 64px;
  padding: 8px 14px;
  border: 1px solid var(--line);
  border-radius: 8px;
  background: var(--bg);
  color: var(--text);
  font: inherit;
  font-size: 14px;
  cursor: pointer;
}
.memory-btn.danger {
  border-color: #b3261e;
  color: #b3261e;
}
.memory-edit-input,
.memory-edit-type {
  width: 100%;
  box-sizing: border-box;
  min-height: 44px;
  padding: 10px 12px;
  border: 1px solid var(--line);
  border-radius: 8px;
  background: var(--bg);
  color: var(--text);
  font: inherit;
  font-size: 14px;
}

/* Custom picker thay chrome cho <select> native (xem enhanceSelect trong
   app.js / admin.js và scripts/verify-right-panel-polish.mjs): cùng mẫu
   speed-picker — nút pill 44px + caret vẽ tay, menu listbox vẽ tay,
   option 44px, selected nền accent 16%. <select> gốc VẪN nằm trong DOM
   (ẩn 1px, opacity 0, không bắt click) để mọi code đọc/ghi .value,
   .disabled, .options, form submit/reset và selectOption của Playwright
   chạy y như cũ; nút + menu chỉ là chrome đồng bộ 2 chiều với nó.
   List luôn inline (position:static) như 4 picker trong panel phải:
   panel/drawer/card đều có tổ tiên cuộn sẽ clip list absolute.
   Selector 2 class để thắng .card button/.card select (admin) mà
   không cần !important; select ẩn dùng 2 class + 2 element để thắng
   cả .card select:disabled. */
.custom-picker {
  position: relative;
  display: block;
  min-width: 0;
}

div.custom-picker > select.custom-picker-select {
  position: absolute;
  top: 0;
  left: 0;
  width: 1px;
  height: 1px;
  min-height: 0;
  margin: 0;
  padding: 0;
  border: 0;
  opacity: 0;
  pointer-events: none;
}

.custom-picker .custom-picker-btn {
  display: flex;
  align-items: center;
  gap: 8px;
  width: 100%;
  min-height: 44px;
  min-width: 0;
  margin: 0;
  padding: 0 12px;
  border-radius: 999px;
  border: 1px solid var(--line);
  background: var(--bg-elev);
  color: var(--ink);
  font-size: 14px;
  font-weight: 600;
  text-align: left;
  cursor: pointer;
}

.custom-picker .custom-picker-btn:hover {
  border-color: color-mix(in srgb, var(--accent) 45%, var(--line));
}

.custom-picker .custom-picker-btn:disabled {
  opacity: 0.55;
  cursor: default;
}

.custom-picker .custom-picker-label {
  flex: 1 1 auto;
  min-width: 0;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.custom-picker .custom-picker-caret {
  color: var(--muted);
  font-size: 11px;
  flex: 0 0 auto;
}

.custom-picker .custom-picker-list {
  margin: 6px 0 0;
  padding: 6px;
  list-style: none;
  background: var(--bg-elev);
  border: 1px solid var(--line);
  border-radius: 16px;
  box-shadow: var(--shadow);
  max-height: 320px;
  overflow-y: auto;
}

.custom-picker .custom-picker-list[hidden] {
  display: none;
}

.custom-picker .custom-picker-option {
  display: flex;
  align-items: center;
  width: 100%;
  min-height: 44px;
  margin: 0;
  padding: 10px 12px;
  border: 0;
  border-radius: 12px;
  background: transparent;
  color: var(--ink);
  font-size: 14px;
  font-weight: 700;
  text-align: left;
  cursor: pointer;
}

.custom-picker .custom-picker-option:hover,
.custom-picker .custom-picker-option:focus-visible {
  background: rgba(243, 236, 227, 0.06);
}

.custom-picker .custom-picker-option.is-selected {
  background: color-mix(in srgb, var(--accent) 16%, transparent);
}

/* --- Thẻ hỏi quyền trong khung chat (xem docs/permissions.md) --- */
.approval-card {
  border: 1px solid color-mix(in srgb, var(--accent) 55%, var(--line));
  border-radius: 18px;
  background: var(--bg-elev);
  padding: 12px 14px;
  display: flex;
  flex-direction: column;
  gap: 8px;
  line-height: 1.5;
  overflow-wrap: anywhere;
}

.approval-card.is-resolved {
  border-color: var(--line);
  opacity: 0.85;
}

.approval-title {
  margin: 0;
  font-size: 14px;
  font-weight: 700;
  color: var(--ink);
}

.approval-action {
  margin: 0;
  font-size: 15px;
  color: var(--ink);
}

.approval-origin {
  margin: 0;
  font-size: 13px;
  color: var(--muted);
}

.approval-note {
  margin: 0;
  font-size: 13px;
  color: var(--muted);
}

.approval-expiry-label {
  font-size: 13px;
  color: var(--muted);
  display: flex;
  align-items: center;
  gap: 8px;
  flex-wrap: wrap;
}

.approval-expiry {
  min-height: 36px;
  border: 1px solid var(--line);
  border-radius: 8px;
  background: var(--bg);
  color: var(--ink);
  font: inherit;
  font-size: 14px;
  padding: 0 8px;
}

.approval-actions {
  display: flex;
  gap: 8px;
  flex-wrap: wrap;
}

.approval-btn {
  min-height: 44px;
  padding: 0 14px;
  border-radius: 10px;
  border: 1px solid var(--line);
  background: transparent;
  color: var(--ink);
  font: inherit;
  font-size: 14px;
  font-weight: 600;
  cursor: pointer;
}

.approval-btn:hover:not(:disabled) {
  border-color: var(--accent);
}

.approval-btn:disabled {
  opacity: 0.5;
  cursor: default;
}

.approval-btn-always {
  background: var(--accent);
  border-color: var(--accent);
  color: var(--accent-ink);
}

.approval-btn-deny {
  color: var(--danger);
  border-color: color-mix(in srgb, var(--danger) 45%, var(--line));
}

.approval-outcome {
  margin: 0;
  font-size: 13px;
  color: var(--muted);
}

/* --- Thẻ nhập credential an toàn (xem docs/credential-input-card.md) --- */
.credential-card {
  border: 1px solid color-mix(in srgb, var(--accent) 55%, var(--line));
  border-radius: 18px;
  background: var(--bg-elev);
  padding: 12px 14px;
  display: flex;
  flex-direction: column;
  gap: 8px;
  line-height: 1.5;
  overflow-wrap: anywhere;
}

.credential-card.is-resolved {
  border-color: var(--line);
  opacity: 0.85;
}

.credential-title {
  margin: 0;
  font-size: 14px;
  font-weight: 700;
  color: var(--ink);
}

.credential-question {
  margin: 0;
  font-size: 15px;
  color: var(--ink);
  white-space: pre-line;
}

.credential-origin {
  margin: 0;
  font-size: 13px;
  color: var(--muted);
}

.credential-note {
  margin: 0;
  font-size: 13px;
  color: var(--muted);
}

.credential-field-label {
  font-size: 13px;
  color: var(--muted);
  display: flex;
  flex-direction: column;
  gap: 4px;
}

.credential-username,
.credential-secret {
  min-height: 44px;
  border: 1px solid var(--line);
  border-radius: 10px;
  background: var(--bg);
  color: var(--ink);
  font: inherit;
  font-size: 15px;
  padding: 0 12px;
  width: 100%;
  box-sizing: border-box;
}

.credential-username:disabled,
.credential-secret:disabled {
  opacity: 0.5;
}

.credential-save-label {
  font-size: 14px;
  color: var(--ink);
  display: flex;
  align-items: center;
  gap: 8px;
  cursor: pointer;
}

.credential-save {
  width: 18px;
  height: 18px;
  accent-color: var(--accent);
  flex-shrink: 0;
}

.credential-actions {
  display: flex;
  gap: 8px;
  flex-wrap: wrap;
}

.credential-btn {
  min-height: 44px;
  padding: 0 14px;
  border-radius: 10px;
  border: 1px solid var(--line);
  background: transparent;
  color: var(--ink);
  font: inherit;
  font-size: 14px;
  font-weight: 600;
  cursor: pointer;
}

.credential-btn:hover:not(:disabled) {
  border-color: var(--accent);
}

.credential-btn:disabled {
  opacity: 0.5;
  cursor: default;
}

.credential-btn-submit {
  background: var(--accent);
  border-color: var(--accent);
  color: var(--accent-ink);
}

.credential-btn-cancel {
  color: var(--danger);
  border-color: color-mix(in srgb, var(--danger) 45%, var(--line));
}

.credential-outcome {
  margin: 0;
  font-size: 13px;
  color: var(--muted);
}

/* --- Mục Quyền truy cập trong panel phải --- */
.rp-subhead {
  margin: 14px 0 8px;
  font-size: 13px;
  font-weight: 700;
  color: var(--ink);
}

.rp-permission-list,
.rp-audit-list {
  list-style: none;
  margin: 0;
  padding: 0;
  display: flex;
  flex-direction: column;
  gap: 8px;
}

.rp-permission-item,
.rp-audit-item {
  border: 1px solid var(--line);
  border-radius: 8px;
  padding: 8px 10px;
  display: flex;
  flex-direction: column;
  gap: 6px;
  word-break: break-word;
}

.rp-permission-revoke {
  align-self: flex-start;
  min-height: 36px;
  padding: 0 12px;
  border-radius: 8px;
  border: 1px solid color-mix(in srgb, var(--danger) 45%, var(--line));
  background: transparent;
  color: var(--danger);
  font: inherit;
  font-size: 13px;
  font-weight: 600;
  cursor: pointer;
}

.rp-permission-revoke:disabled {
  opacity: 0.5;
  cursor: default;
}
