/* AskoWriter — component styles (dùng design tokens AskoChat v2). */

* { box-sizing: border-box; }
html, body { margin: 0; height: 100%; }
body {
  font-family: var(--font-sans); color: var(--fg-default);
  background: var(--bg-app); font-size: var(--text-base);
  -webkit-font-smoothing: antialiased;
}
button { font-family: inherit; cursor: pointer; }
::-webkit-scrollbar { width: 10px; height: 10px; }
::-webkit-scrollbar-thumb { background: var(--slate-300); border-radius: 999px; }

/* ── Shell ────────────────────────────────────────────────── */
.app-shell { display: flex; height: 100vh; overflow: hidden; }

/* ── Sidebar (dark inverse) ───────────────────────────────── */
.sidebar {
  width: var(--sidebar-width); flex-shrink: 0; background: var(--bg-inverse);
  color: var(--fg-inverse); display: flex; flex-direction: column;
  border-right: 1px solid var(--border-on-dark);
}

/* App nav list — khớp Asko Chat (hàng icon + tên + tag) */
.app-nav {
  display: flex;
  flex-direction: column;
  gap: 2px;
  padding: 14px 12px 8px;
  flex-shrink: 0;
}
.app-nav-item {
  display: flex;
  align-items: center;
  gap: 12px;
  padding: 8px 10px;
  border-radius: 10px;
  text-decoration: none;
  color: inherit;
  border: 1px solid transparent;
  transition: background .15s, border-color .15s, box-shadow .15s;
  min-width: 0;
}
.app-nav-item:hover {
  background: var(--bg-inverse-soft);
  border-color: var(--border-on-dark);
}
.app-nav-item.active {
  background: rgba(99, 102, 241, 0.22);
  border-color: rgba(129, 140, 248, 0.4);
  box-shadow: 0 1px 2px rgba(99, 102, 241, 0.12);
}
.app-nav-icon {
  width: 36px;
  height: 36px;
  border-radius: 10px;
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 15px;
  color: #fff;
  flex-shrink: 0;
}
.app-nav-chat {
  background: linear-gradient(135deg, #6366f1, #8b5cf6);
  box-shadow: 0 1px 2px rgba(99, 102, 241, 0.3), 0 6px 16px -8px rgba(99, 102, 241, 0.5);
}
.app-nav-writer {
  background: linear-gradient(135deg, #0ea5e9, #2563eb);
  box-shadow: 0 1px 2px rgba(37, 99, 235, 0.3), 0 6px 16px -8px rgba(37, 99, 235, 0.45);
}
.app-nav-text {
  display: flex;
  flex-direction: column;
  min-width: 0;
  gap: 1px;
}
.app-nav-name {
  font-size: 14px;
  font-weight: 700;
  color: #f8fafc;
  letter-spacing: -0.01em;
  line-height: 1.2;
}
.app-nav-tag {
  font-size: 11px;
  color: var(--fg-inverse-muted);
  font-weight: 500;
  line-height: 1.2;
}
.app-nav-external {
  margin-left: auto;
  flex-shrink: 0;
  font-size: 12px;
  color: var(--fg-inverse-faint);
  opacity: 0.7;
}
.app-nav-item:hover .app-nav-external {
  opacity: 1;
  color: #fff;
}
.sidebar-brand { display: none; }

/* Workspace footer — trong sidebar, giống Asko Chat (không fixed che content) */
.sidebar-footer {
  flex-shrink: 0;
  padding: 10px 12px 12px;
  border-top: 1px solid var(--border-on-dark);
  margin-top: auto;
}
.ws-chip {
  display: flex; align-items: center; gap: 10px; width: 100%;
  padding: 8px 10px; border-radius: 12px; border: 1px solid transparent;
  background: transparent; color: #f8fafc; cursor: pointer; text-align: left;
  transition: background .15s, border-color .15s;
}
.ws-chip:hover {
  background: var(--bg-inverse-soft);
  border-color: var(--border-on-dark);
}
.ws-avatar {
  width: 32px; height: 32px; border-radius: 10px; flex-shrink: 0;
  display: flex; align-items: center; justify-content: center; font-weight: 700; font-size: 13px;
  background: linear-gradient(135deg, #f97316, #ea580c); color: #fff;
}
.ws-avatar.lg { width: 40px; height: 40px; font-size: 15px; }
.ws-meta { flex: 1; min-width: 0; display: flex; flex-direction: column; gap: 1px; }
.ws-name { font-size: 13px; font-weight: 700; }
.ws-id { font-size: 10px; color: var(--fg-inverse-muted); overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.ws-gear { opacity: 0.6; font-size: 14px; }
.ws-modal { position: fixed; inset: 0; z-index: 80; display: flex; align-items: center; justify-content: center; }
.ws-modal[hidden] { display: none !important; }
.ws-backdrop { position: absolute; inset: 0; background: rgba(15, 23, 42, 0.55); }
.ws-dialog {
  position: relative; width: min(420px, 92vw); border-radius: 16px;
  background: #fff; color: #0f172a; box-shadow: 0 25px 50px -12px rgba(0,0,0,.35);
  overflow: hidden;
}
.ws-head { display: flex; align-items: center; gap: 12px; padding: 16px 16px 12px; border-bottom: 1px solid #e2e8f0; }
.ws-title { font-weight: 700; font-size: 15px; }
.ws-sub { font-size: 11px; color: #64748b; word-break: break-all; }
.ws-x { margin-left: auto; border: none; background: transparent; font-size: 16px; cursor: pointer; color: #64748b; }
.ws-body { padding: 14px 16px; }
.ws-body h4 { margin: 0 0 6px; font-size: 12px; text-transform: uppercase; letter-spacing: .04em; color: #64748b; }
.ws-hint { font-size: 12px; color: #64748b; margin: 0 0 12px; line-height: 1.45; }
.ws-row { display: flex; flex-direction: column; gap: 4px; margin-bottom: 10px; font-size: 13px; font-weight: 600; }
.ws-row .dark-input {
  background: #f8fafc; border: 1px solid #e2e8f0; color: #0f172a; border-radius: 8px;
  padding: 8px 10px; font-weight: 500; font-size: 12px; font-family: ui-monospace, monospace;
}
.ws-thread-row { display: flex; gap: 8px; align-items: center; }
.ws-thread-row .dark-input { flex: 1; }
.ws-foot { padding: 12px 16px 16px; border-top: 1px solid #e2e8f0; display: flex; justify-content: flex-end; }
.ws-foot .btn-accent {
  border: none; border-radius: 10px; padding: 8px 16px; font-weight: 600; cursor: pointer;
  background: linear-gradient(135deg, #6366f1, #8b5cf6); color: #fff;
}

.sidebar-cta { padding: 4px 14px 10px; }
.new-chat-link {
  width: 100%; display: flex; align-items: center; gap: 8px; justify-content: center;
  padding: 10px 12px; border-radius: var(--radius-pill); border: none; color: #fff;
  font-weight: 600; font-size: var(--text-sm);
  background: linear-gradient(135deg, var(--accent-500), var(--accent-700));
  box-shadow: 0 4px 10px -4px rgba(99,102,241,.5);
}
.new-chat-link:hover { filter: brightness(1.06); }

.sidebar-scroll { flex: 1; overflow-y: auto; padding: 6px 10px 10px; }
.sidebar-section-title {
  font-size: 11px; text-transform: uppercase; letter-spacing: .06em;
  color: var(--fg-inverse-faint); padding: 12px 8px 6px; font-weight: 600;
}

/* thread list */
.thread-item {
  display: flex; align-items: center; gap: 8px; padding: 9px 10px; border-radius: var(--radius-md);
  color: var(--fg-inverse-muted); font-size: var(--text-sm); cursor: pointer;
  border: 1px solid transparent; margin-bottom: 2px;
}
.thread-item:hover { background: var(--bg-inverse-soft); color: #fff; }
.thread-item.active { background: var(--bg-inverse-soft); color: #fff; border-color: var(--border-on-dark); }
.thread-item .ti-title { flex: 1; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.thread-item .ti-del { opacity: 0; border: none; background: none; color: var(--fg-inverse-faint); padding: 2px 4px; border-radius: 4px; }
.thread-item:hover .ti-del { opacity: 1; }
.thread-item .ti-del:hover { color: #fff; background: rgba(255,255,255,.1); }

/* nút "Xem cuộc trò chuyện cũ hơn" + danh sách bung (scroll được) */
.older-toggle {
  width: 100%; text-align: left; padding: 8px 10px; margin-top: 2px; background: transparent;
  border: none; color: var(--fg-inverse-faint); font-size: 12px; font-weight: 600;
  border-radius: var(--radius-md); cursor: pointer;
}
.older-toggle:hover { background: var(--bg-inverse-soft); color: #fff; }
.older-list { max-height: 240px; overflow-y: auto; margin-bottom: 4px; }

/* sidebar panels (rules / sample / tài liệu) — NÚT CHỨC NĂNG dạng card nổi bật */
.side-panel { margin-top: 6px; }
.side-panel > summary {
  list-style: none; cursor: pointer; padding: 11px 12px; font-size: var(--text-sm);
  font-weight: 600; color: var(--fg-inverse); border-radius: var(--radius-md);
  background: var(--bg-inverse-soft); border: 1px solid var(--border-on-dark);
  display: flex; align-items: center; gap: 8px;
  transition: border-color .15s, background .15s, color .15s;
}
.side-panel > summary::-webkit-details-marker { display: none; }
.side-panel > summary:hover { border-color: var(--accent-400); background: #1c2742; color: #fff; }
.side-panel[open] > summary { border-color: var(--accent-500); color: #fff; }
.side-panel .panel-body { padding: 8px 8px 10px; display: flex; flex-direction: column; gap: 8px; }

.dark-input, .dark-textarea {
  width: 100%; background: var(--bg-inverse-soft); color: #f1f5f9;
  border: 1px solid var(--border-on-dark); border-radius: var(--radius-md);
  padding: 8px 10px; font-size: var(--text-sm); font-family: inherit;
}
.dark-textarea { resize: vertical; min-height: 64px; }
.dark-input::placeholder, .dark-textarea::placeholder { color: var(--fg-inverse-faint); }

.btn-accent {
  border: none; color: #fff; border-radius: var(--radius-pill); padding: 8px 12px;
  font-weight: 600; font-size: var(--text-sm); background: var(--accent-500);
}
.btn-accent:hover { background: var(--accent-600); }
.btn-ghost-dark {
  border: 1px solid var(--border-on-dark); background: transparent; color: var(--fg-inverse-muted);
  border-radius: var(--radius-pill); padding: 6px 10px; font-size: var(--text-xs);
}
.btn-ghost-dark:hover { color: #fff; background: var(--bg-inverse-soft); }

/* ── Memory phong cách (học bị động) ──────────────────────── */
#memList { max-height: 240px; overflow-y: auto; margin-top: 6px; }
.mem-sub { font-size: 10.5px; text-transform: uppercase; letter-spacing: .04em;
  color: var(--fg-inverse-faint); margin: 10px 0 4px; }
.mem-row { display: flex; align-items: flex-start; gap: 6px; padding: 6px 6px 6px 8px;
  border-radius: var(--radius-md); background: var(--bg-inverse-soft); margin-bottom: 4px;
  opacity: .55; }
.mem-row.on { opacity: 1; border-left: 2px solid var(--accent-500); }
.mem-tag { flex: none; font-size: 10px; font-weight: 700; color: var(--accent-300);
  background: var(--accent-soft); border-radius: var(--radius-pill); padding: 1px 6px; margin-top: 1px; }
.mem-text { flex: 1; font-size: 12px; line-height: 1.4; color: var(--fg-inverse-muted); word-break: break-word; }
.mem-count { flex: none; font-size: 10px; color: var(--fg-inverse-faint); margin-top: 2px; }
.mem-row .rc-del { flex: none; border: none; background: none; color: var(--fg-inverse-faint);
  padding: 1px 3px; border-radius: 4px; line-height: 1; }
.mem-row .rc-del:hover { color: #fff; background: rgba(255,255,255,.1); }

.mem-empty {
  font-size: 11.5px; line-height: 1.6; color: var(--fg-inverse-faint);
  padding: 10px 11px; border: 1px dashed var(--border-on-dark);
  border-radius: var(--radius-md); margin-top: 4px;
}
.mem-empty b { color: var(--accent-300); }

.mem-toast { position: fixed; right: 20px; bottom: 20px; z-index: 60; max-width: 380px;
  background: var(--bg-inverse); color: var(--fg-inverse-muted); border: 1px solid var(--border-on-dark);
  border-radius: var(--radius-lg); padding: 12px 14px; font-size: 12.5px; line-height: 1.6;
  box-shadow: 0 12px 28px -12px rgba(0,0,0,.55); }
.mem-toast b { color: #fff; display: block; margin-bottom: 4px; }
.mem-undo { margin-top: 8px; border: 1px solid var(--border-on-dark); background: none;
  color: var(--fg-inverse-muted); border-radius: var(--radius-pill); padding: 4px 12px; font-size: 11.5px; }
.mem-undo:hover { color: #fff; background: var(--bg-inverse-soft); }


/* ── Template v2: danh sách chọn + ô yêu cầu + đề xuất hệ thống ── */
.tpl-note { font-size: 11.5px; color: var(--fg-inverse-faint); padding: 8px 10px;
  border: 1px dashed var(--border-on-dark); border-radius: var(--radius-md); margin-bottom: 10px; }
.tpl-warn { border-color: var(--danger-400, #f87171); color: var(--danger-400, #f87171); }
.tpl-warn b { color: #fff; }
.tpl-pick { margin: 8px 0 12px; }
.tpl-select {
  width: 100%; background: var(--bg-inverse-soft); color: #f1f5f9;
  border: 1px solid var(--border-on-dark); border-radius: var(--radius-md);
  padding: 9px 10px; font-size: 13px; font-family: inherit; cursor: pointer;
}
.tpl-select:focus { outline: none; border-color: var(--accent-500);
  box-shadow: 0 0 0 3px var(--accent-ring); }
.tpl-select option { background: var(--bg-inverse); color: #f1f5f9; }
.tpl-select:disabled { opacity: .5; cursor: not-allowed; }
.tpl-lbl { font-size: 11.5px; font-weight: 600; color: var(--fg-inverse-muted); margin-bottom: 5px; }
.tpl-lbl em { font-style: normal; font-weight: 400; color: var(--accent-300); font-size: 10.5px; }
.tpl-optlist { display: flex; flex-direction: column; gap: 3px;
  border: 1px solid var(--border-on-dark); border-radius: var(--radius-md); padding: 5px; }
.tpl-opt { display: flex; align-items: center; gap: 8px; padding: 6px 8px;
  border-radius: var(--radius-sm); font-size: 12.5px; color: var(--fg-inverse-muted); cursor: pointer; }
.tpl-opt:hover { background: var(--bg-inverse-soft); color: #fff; }
.tpl-opt.on { background: var(--accent-soft); color: #fff; }
.tpl-opt input { accent-color: var(--accent-500); flex: none; }
.tpl-opt span { flex: 1; }
.tpl-opt .tpl-tag { flex: none; font-style: normal; font-size: 9.5px; font-weight: 700;
  color: var(--accent-300); background: var(--bg-inverse-soft); border-radius: var(--radius-pill);
  padding: 1px 6px; }

.tpl-default { border-top: 1px solid var(--border-on-dark); padding-top: 6px; margin-top: 4px; }
.tpl-def-head { display: flex; align-items: center; gap: 8px; cursor: pointer;
  font-size: 12px; font-weight: 600; color: var(--fg-inverse-muted); padding: 4px 0; }
.tpl-def-head span { flex: 1; }
.tpl-def-body { padding: 6px 0 4px 4px; font-size: 12px; line-height: 1.6; }
.td-row { color: var(--fg-inverse-muted); margin: 4px 0 2px; }
.td-row b { color: #fff; }
.td-dim { color: var(--fg-inverse-faint); }
.td-if { display: flex; align-items: baseline; gap: 6px; padding: 2px 0 2px 12px;
  color: var(--fg-inverse-muted); font-size: 11.5px; }
.td-key { flex: none; font-weight: 700; font-size: 10px; color: var(--accent-300);
  background: var(--accent-soft); border-radius: 3px; padding: 0 4px; }
.td-arrow { color: var(--fg-inverse-faint); }
.td-if b { color: #fff; }
.td-ban { color: var(--danger-400, #f87171); margin-top: 6px; }
.td-ban b { color: var(--danger-400, #f87171); }
.td-sub { padding-left: 12px; font-size: 11.5px; color: var(--fg-inverse-faint); }
.td-why { margin-top: 8px; padding: 7px 9px; border-left: 2px solid var(--accent-soft);
  font-size: 11.5px; color: var(--fg-inverse-faint); line-height: 1.55; }
.td-note { margin-top: 8px; font-size: 11px; color: var(--fg-inverse-faint); font-style: italic; }
.td-note-on { color: var(--accent-300); font-style: normal; }
.td-note-on b { color: #fff; }

/* ── Template chuyên mục ──────────────────────────────────── */
/* Danh sách = các HÀNG gọn. Chi tiết nằm trong popup, không xổ tại chỗ. */
#tplList { max-height: 340px; overflow-y: auto; resize: vertical; padding-right: 2px; margin-top: 6px; }

.tpl-row {
  display: flex; align-items: center; gap: 9px;
  border: 1px solid var(--border-on-dark); border-radius: var(--radius-md);
  background: var(--bg-inverse-soft); margin-bottom: 6px; padding: 10px 10px 10px 11px;
}
.tpl-row.selected { border-color: var(--accent-500); box-shadow: 0 0 0 1px var(--accent-500) inset; }
.tpl-row input[type="radio"] { accent-color: var(--accent-500); flex: none; cursor: pointer; }
.tpl-name { flex: 1; font-size: 13px; font-weight: 600; color: #fff; cursor: pointer; }
.tpl-see {
  flex: none; border: none; background: none; color: var(--accent-300);
  font-size: 11.5px; padding: 3px 6px; border-radius: 4px; cursor: pointer;
}
.tpl-see:hover { color: #fff; background: rgba(255,255,255,.08); }
.tpl-row .rc-del {
  border: none; background: none; color: var(--fg-inverse-faint);
  padding: 2px 4px; border-radius: 4px; line-height: 1; flex: none;
}
.tpl-row .rc-del:hover { color: #fff; background: rgba(255,255,255,.1); }

/* Popup chi tiết template */
.tpl-detail-dlg { width: min(720px, 92vw); }
.tpl-dlg-head { display: flex; align-items: center; gap: 12px; margin-bottom: 6px; }
.tpl-dlg-head h3 { flex: 1; margin: 0; }
.tpl-x {
  flex: none; border: none; background: none; color: var(--fg-inverse-faint);
  font-size: 18px; line-height: 1; padding: 4px 8px; border-radius: 6px; cursor: pointer;
}
.tpl-x:hover { color: #fff; background: rgba(255,255,255,.1); }

.tpl-sec-title {
  display: flex; align-items: center; gap: 8px;
  font-size: 11px; font-weight: 700; letter-spacing: .04em; text-transform: uppercase;
  color: var(--fg-inverse-faint); margin: 9px 0 4px;
}
.tpl-sec-title::after { content: ''; flex: 1; height: 1px; background: var(--border-on-dark); }

/* Hàng gọn — mặc định CHỈ hiện lựa chọn; phần "tại sao" nằm sau nút ⌄. */
.tpl-item { border-bottom: 1px solid rgba(255,255,255,.04); }
.tpl-item:last-child { border-bottom: none; }
.tpl-item-top { display: flex; align-items: flex-start; gap: 6px; padding: 4px 0; }
.tpl-choice {
  flex: 1; font-size: 12px; line-height: 1.45; color: var(--fg-inverse-muted); word-break: break-word;
}
.tpl-choice::before { content: '• '; color: var(--fg-inverse-faint); }
.tpl-tag {
  display: inline-block; font-style: normal; font-size: 10px; font-weight: 700;
  color: var(--accent-300); background: var(--accent-soft); border-radius: var(--radius-pill);
  padding: 1px 6px; margin-left: 4px; vertical-align: 1px;
}
.tpl-caret {
  flex: none; border: none; background: none; color: var(--fg-inverse-faint);
  font-size: 13px; line-height: 1; padding: 3px 4px; border-radius: 4px;
  transition: transform var(--duration-fast) ease, color var(--duration-fast) ease;
}
.tpl-caret:hover { color: #fff; background: rgba(255,255,255,.08); }
.tpl-caret.open { transform: rotate(180deg); color: var(--accent-300); }
.tpl-detail {
  font-size: 11.5px; line-height: 1.55; color: var(--fg-inverse-faint);
  padding: 2px 4px 8px 12px; border-left: 2px solid var(--accent-soft);
  margin: 0 0 4px 4px; white-space: pre-wrap;
}

.flow-gold { border: 1px solid var(--danger-400, #f87171); border-radius: var(--radius-md);
  padding: 9px 11px; margin-bottom: 12px; font-size: 11.5px; line-height: 1.6;
  color: var(--fg-inverse-muted); }
.flow-gold b { display: block; color: var(--danger-400, #f87171); margin-bottom: 4px; }
.flow-step { border-top: 1px solid var(--border-on-dark); }
.flow-step-head { display: flex; align-items: center; gap: 8px; cursor: pointer; padding: 8px 0;
  font-size: 13px; font-weight: 700; color: #fff; }
.flow-step-head span { flex: 1; }
.flow-step-body { padding: 0 0 10px 4px; }
.flow-do { color: var(--fg-inverse-muted); }
.if-no { background: rgba(248,113,113,.07); border-radius: 4px; }
.if-no .td-key { background: rgba(248,113,113,.18); color: var(--danger-400, #f87171); }
.flow-must { font-size: 11.5px; color: var(--success-400, #4ade80); padding: 3px 0 3px 12px; }
.flow-never { font-size: 11.5px; color: var(--danger-400, #f87171); padding: 3px 0 3px 12px; }

.tpl-flow {
  font-size: 12px; line-height: 1.6; color: var(--fg-inverse-muted);
  margin: 0 0 10px; white-space: pre-wrap;
}
.tpl-flow strong { color: #fff; }

.btn-add-tpl { width: 100%; margin-top: 8px; }
.btn-sm { font-size: 11px; padding: 3px 8px; }

/* Dialog tạo template */
.tpl-dialog {
  width: min(680px, 92vw); max-height: 84vh; overflow-y: auto; border: none;
  border-radius: var(--radius-lg); padding: 20px 22px;
  background: var(--bg-inverse); color: var(--fg-inverse-muted);
}
.tpl-dialog::backdrop { background: rgba(0,0,0,.55); }
.tpl-dialog h3 { color: #fff; font-size: 16px; margin: 0 0 14px; }
.tpl-field { display: block; margin-bottom: 14px; }
.tpl-field > span { display: block; font-size: 11px; color: var(--fg-inverse-faint); margin-bottom: 4px; }
.tpl-build-sec { margin-bottom: 12px; }
.tpl-build-row { display: flex; align-items: flex-start; gap: 6px; margin-bottom: 6px; }
.tpl-build-row .bi-choice { flex: 0 0 34%; }
.tpl-build-row .bi-detail { flex: 1; min-height: 42px; }
.tpl-build-row .rc-del {
  flex: none; border: none; background: none; color: var(--fg-inverse-faint);
  padding: 4px 6px; border-radius: 4px; line-height: 1;
}
.tpl-actions { display: flex; justify-content: flex-end; gap: 8px; margin-top: 16px; }

.hint-dark { font-size: 11px; color: var(--fg-inverse-faint); }
.pill-ok { font-size: 11px; color: var(--success-500); }

/* ── Thanh kéo resize sidebar ↔ chat ──────────────────────── */
.resizer { width: 6px; flex-shrink: 0; cursor: col-resize; position: relative; z-index: 5; background: transparent; }
.resizer::after {
  content: ''; position: absolute; inset: 0 2px; border-radius: 3px;
  background: var(--border-subtle); opacity: 0; transition: opacity .15s;
}
.resizer:hover::after, .resizer.dragging::after { opacity: 1; background: var(--accent-400, #818cf8); }

/* ── Right rail (Nguồn + Bài mẫu) — dark inverse giống sidebar trái ── */
.right-rail {
  width: var(--rail-width, 300px);
  flex-shrink: 0;
  background: var(--bg-inverse);
  color: var(--fg-inverse);
  border-left: 1px solid var(--border-on-dark);
  display: flex;
  flex-direction: column;
  min-height: 0;
}
.rail-head {
  flex-shrink: 0;
  padding: 12px 12px 0;
  border-bottom: 1px solid var(--border-on-dark);
}
.rail-tabs {
  display: flex;
  gap: 4px;
  margin-bottom: -1px;
}
.rail-tab {
  flex: 1;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 6px;
  padding: 10px 8px;
  border: none;
  background: transparent;
  color: var(--fg-inverse-muted);
  font-size: var(--text-sm);
  font-weight: 600;
  border-bottom: 2px solid transparent;
  border-radius: var(--radius-md) var(--radius-md) 0 0;
}
.rail-tab:hover { color: #fff; background: var(--bg-inverse-soft); }
.rail-tab.is-active {
  color: #fff;
  border-bottom-color: var(--accent-400);
  background: transparent;
}
.rail-badge {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  min-width: 18px;
  height: 18px;
  padding: 0 5px;
  border-radius: var(--radius-pill);
  font-size: 11px;
  font-weight: 700;
  background: rgba(99, 102, 241, 0.35);
  color: #e0e7ff;
}
.rail-badge.is-muted {
  background: rgba(255, 255, 255, 0.08);
  color: var(--fg-inverse-faint);
}
.rail-body {
  flex: 1;
  overflow-y: auto;
  padding: 14px 12px 16px;
  display: flex;
  flex-direction: column;
  gap: 12px;
}
.rail-body.is-hidden { display: none; }
.rail-hint {
  margin: 0;
  font-size: 12px;
  line-height: 1.5;
  color: var(--fg-inverse-muted);
}
.rail-hint b { color: #fff; font-weight: 600; }
.rail-add-btn {
  width: 100%;
  padding: 10px 12px;
  border-radius: var(--radius-md);
  border: 1px dashed var(--border-on-dark);
  background: var(--bg-inverse-soft);
  color: var(--accent-300);
  font-weight: 600;
  font-size: var(--text-sm);
}
.rail-add-btn:hover {
  border-color: var(--accent-400);
  background: #1c2742;
  color: #fff;
}
.rail-plus-btn {
  width: 100%;
  margin-top: 2px;
  padding: 8px 10px;
  border-radius: var(--radius-md);
  border: 1px dashed var(--border-on-dark);
  background: transparent;
  color: var(--fg-inverse-muted);
  font-weight: 600;
  font-size: 12.5px;
}
.rail-plus-btn:hover {
  border-color: var(--accent-400);
  color: #fff;
  background: var(--bg-inverse-soft);
}
.sample-block {
  display: flex;
  flex-direction: column;
  gap: 8px;
  padding: 10px;
  border-radius: var(--radius-md);
  background: var(--bg-inverse-soft);
  border: 1px solid var(--border-on-dark);
}
.sample-block-head {
  display: flex;
  align-items: center;
  justify-content: space-between;
}
.sample-block-title {
  font-size: 11px;
  font-weight: 700;
  text-transform: uppercase;
  letter-spacing: .04em;
  color: var(--fg-inverse-faint);
}
.sample-field-list {
  display: flex;
  flex-direction: column;
  gap: 8px;
}
.sample-field-row {
  display: flex;
  flex-direction: column;
  gap: 6px;
  position: relative;
}
.sample-field-row .sample-row-tools {
  display: flex;
  justify-content: flex-end;
  gap: 4px;
}
.sample-field-row .sample-rm {
  border: none;
  background: rgba(255, 255, 255, 0.06);
  color: var(--fg-inverse-faint);
  font-size: 11px;
  font-weight: 600;
  padding: 3px 8px;
  border-radius: var(--radius-pill);
  line-height: 1.3;
}
.sample-field-row .sample-rm:hover {
  color: #fff;
  background: rgba(239, 68, 68, 0.35);
}
.src-list { display: flex; flex-direction: column; gap: 6px; }
.src-item-row {
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 10px 10px;
  border-radius: var(--radius-md);
  border: 1px solid var(--border-on-dark);
  background: var(--bg-inverse-soft);
  cursor: pointer;
  transition: border-color .15s, background .15s;
}
.src-item-row:hover { border-color: var(--accent-400); background: #1c2742; }
.src-item-row.is-off { opacity: .5; }
.src-item-row input[type="checkbox"] {
  width: 16px; height: 16px; accent-color: var(--accent-400); flex-shrink: 0; cursor: pointer;
}
.src-item-row .src-icon {
  flex-shrink: 0;
  width: 28px; height: 28px;
  border-radius: 6px;
  background: rgba(255, 255, 255, 0.08);
  display: flex; align-items: center; justify-content: center;
  font-size: 14px;
}
.src-item-row .src-meta {
  flex: 1;
  min-width: 0;
  overflow: hidden;
  display: flex;
  flex-direction: column;
  gap: 2px;
}
/* stem ellipsis + .ext luôn cùng dòng (tránh ".txt" rơi xuống dòng phụ) */
.src-item-row .src-name {
  display: flex;
  align-items: baseline;
  min-width: 0;
  max-width: 100%;
  font-size: var(--text-sm);
  font-weight: 600;
  color: #f8fafc;
  line-height: 1.35;
}
.src-item-row .src-stem {
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
  min-width: 0;
}
.src-item-row .src-ext {
  flex-shrink: 0;
  font-weight: 500;
  opacity: 0.72;
  white-space: nowrap;
}
.src-item-row .src-sub {
  font-size: 11px;
  color: var(--fg-inverse-faint);
  line-height: 1.3;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}
.src-item-row .src-sub.is-uploading {
  color: var(--accent-300, #a5b4fc);
  font-style: italic;
}
.src-item-row .src-sub.is-err {
  color: var(--warning-500, #fbbf24);
}
.src-item-row.is-uploading {
  opacity: 0.9;
  border-style: dashed;
  border-color: var(--accent-400, #818cf8);
}
.src-item-row.is-uploading .src-name { opacity: 0.9; }
.src-item-row.is-uploading .src-icon {
  animation: src-spin 0.9s linear infinite;
}
@keyframes src-spin {
  to { transform: rotate(360deg); }
}
.src-item-row .src-rm {
  flex-shrink: 0;
  border: none;
  background: none;
  color: var(--fg-inverse-faint);
  padding: 4px 6px;
  border-radius: 4px;
  line-height: 1;
  font-size: 14px;
}
.src-item-row .src-rm:hover { color: #fff; background: rgba(255, 255, 255, 0.1); }
.src-empty {
  font-size: 12.5px;
  color: var(--fg-inverse-faint);
  text-align: center;
  padding: 24px 12px;
  border: 1px dashed var(--border-on-dark);
  border-radius: var(--radius-md);
}
.rail-input, .rail-textarea {
  width: 100%;
  border: 1px solid var(--border-on-dark);
  border-radius: var(--radius-md);
  padding: 8px 10px;
  font-size: var(--text-sm);
  font-family: inherit;
  color: #f1f5f9;
  background: rgba(0, 0, 0, 0.25);
}
.rail-textarea { min-height: 88px; resize: vertical; max-height: 200px; }
.rail-input::placeholder, .rail-textarea::placeholder { color: var(--fg-inverse-faint); }
.rail-input:focus, .rail-textarea:focus {
  outline: none;
  border-color: var(--accent-400);
  box-shadow: 0 0 0 3px var(--accent-ring);
}
.rail-file-label {
  display: flex;
  flex-direction: column;
  gap: 6px;
  font-size: 12px;
  color: var(--fg-inverse-muted);
  font-weight: 500;
}
.rail-file-label input[type="file"] {
  font-size: 12px;
  color: var(--fg-inverse-muted);
  width: 100%;
}
.rail-status {
  font-size: 11.5px;
  color: var(--success-500);
  line-height: 1.45;
  min-height: 1.2em;
}
.rail-status.is-warn { color: var(--warning-500); }

@media (max-width: 960px) {
  .right-rail { width: 260px; }
}
@media (max-width: 780px) {
  .right-rail {
    position: fixed;
    right: 0; top: 0; bottom: 0;
    z-index: 40;
    box-shadow: var(--shadow-xl);
    transform: translateX(100%);
    transition: transform .2s ease;
  }
  .right-rail.is-open { transform: none; }
}

/* ── Main column ──────────────────────────────────────────── */
.main-column { flex: 1; display: flex; flex-direction: column; min-width: 0; background: var(--bg-canvas); }
.topbar {
  height: var(--topbar-height); flex-shrink: 0; display: flex; align-items: center;
  padding: 0 24px; border-bottom: 1px solid var(--border-subtle); gap: 10px;
}
.thread-title { font-size: var(--text-lg); font-weight: 600; color: var(--fg-strong); margin: 0;
  overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.status-pill { margin-left: auto; font-size: 12px; color: var(--fg-muted); display: flex; align-items: center; gap: 6px; }
.status-dot { width: 8px; height: 8px; border-radius: 50%; background: var(--success-500); }
.status-pill.busy .status-dot { background: var(--warning-500); animation: pulse 1s infinite; }
@keyframes pulse { 50% { opacity: .3; } }

/* ── Chat area ────────────────────────────────────────────── */
.chat-messages { flex: 1; overflow-y: auto; padding: 28px clamp(20px, 5vw, 48px); scroll-behavior: smooth; }
.chat-inner { max-width: 820px; margin: 0 auto; }

.message { display: flex; margin-bottom: 20px; animation: fadeUp .2s var(--ease-emphasized); flex-direction: column; }
@keyframes fadeUp { from { opacity: 0; transform: translateY(6px); } to { opacity: 1; transform: none; } }
.message.user { align-items: flex-end; }
.message.bot { align-items: flex-start; }
.msg-docs-row { display: flex; flex-wrap: wrap; gap: 6px; margin-bottom: 6px; max-width: min(88%, 760px); }
.file-chip-sent { background: var(--accent-soft); color: var(--accent-700); font-size: 11.5px; }
.bubble { max-width: min(88%, 760px); padding: 12px 16px; border-radius: var(--radius-xl);
  line-height: var(--leading-relaxed); font-size: var(--text-md); box-shadow: var(--shadow-xs); }
.message.user .bubble { background: var(--accent-500); color: #fff; border-bottom-right-radius: var(--radius-sm); }
.message.bot .bubble { background: var(--bg-canvas); color: var(--fg-strong);
  border: 1px solid var(--border-subtle); border-bottom-left-radius: var(--radius-sm); }
.message.bot .bubble.error { border-color: var(--danger-500); color: var(--danger-500); background: var(--danger-bg); }

/* status bubble (đang xử lý) */
.status-bubble { color: var(--fg-muted); font-style: italic; display: flex; align-items: center; gap: 8px; }
.status-spin { width: 13px; height: 13px; flex-shrink: 0; border: 2px solid var(--accent-200);
  border-top-color: var(--accent-500); border-radius: 50%; display: inline-block; animation: spin .7s linear infinite; }
@keyframes spin { to { transform: rotate(360deg); } }

/* article render */
.bubble.article { max-width: min(96%, 780px); }
/* Chỉ tít/tiểu mục in hoa kiểu báo; thân bài GIỮ nguyên hoa-thường (không uppercase) */
.article h1, .article .a-title { font-size: 1.5rem; font-weight: 800; text-transform: uppercase; margin: .2rem 0 .7rem; line-height: 1.25; }
.article .a-sapo { font-weight: 700; margin: 0 0 1rem; text-transform: none; }
.article h2, .article .a-sub { font-size: 1.05rem; font-weight: 800; text-transform: uppercase; margin: 1.2rem 0 .5rem; }
.article p { margin: 0 0 .75rem; text-transform: none; }
.plan-md { font-size: 13.5px; }
.plan-md :where(h2,h3) { font-size: 1rem; }

/* ── Gates (tick sources / approve plan) ──────────────────── */
.gate-card { max-width: 820px; margin: 4px auto 20px; background: var(--bg-subtle);
  border: 1px solid var(--border-default); border-radius: var(--radius-xl); padding: 16px 18px; }
.gate-title { font-weight: 700; color: var(--fg-strong); margin: 0 0 4px; font-size: var(--text-md); }
.gate-sub { font-size: 12px; color: var(--fg-muted); margin: 0 0 12px; }
.src-item { display: flex; gap: 10px; padding: 10px; border: 1px solid var(--border-subtle);
  border-radius: var(--radius-md); background: var(--bg-canvas); margin-bottom: 8px; }
.src-item input { margin-top: 3px; }
.src-item .s-title { font-weight: 600; color: var(--fg-strong); font-size: 13.5px; }
.src-item .s-url { font-size: 11.5px; color: var(--accent-600); word-break: break-all; }
.src-item .s-snip { font-size: 12px; color: var(--fg-muted); margin-top: 3px; }
/* ── Bảng kế hoạch: thẻ accordion, SỬA TRỰC TIẾP trên từng ô ── */
.plan-card {
  background: var(--bg-canvas); border: 1px solid var(--border-subtle);
  border-radius: var(--radius-lg); padding: 12px 14px; margin-bottom: 10px;
}
.pc-head { display: flex; align-items: center; gap: 10px; cursor: pointer; }
.pc-icon { font-size: 15px; flex: none; }
.pc-title { flex: 1; font-weight: 700; color: var(--fg-strong); font-size: 14.5px; }
.pc-caret {
  flex: none; border: none; background: none; color: var(--fg-muted); font-size: 14px;
  line-height: 1; padding: 4px 6px; border-radius: 4px;
  transition: transform var(--duration-fast) ease;
}
.pc-caret:hover { background: var(--bg-hover); color: var(--fg-strong); }
.pc-caret.open { transform: rotate(180deg); }
.pc-body { margin-top: 8px; }

/* nhóm đánh số bên trong "Chi tiết bảng kế hoạch" */
.plan-group { border-top: 1px solid var(--border-subtle); padding: 4px 0; }
.plan-group:first-child { border-top: none; }
.pg-head { display: flex; align-items: center; gap: 10px; cursor: pointer; padding: 7px 0; }
.pg-num { flex: none; width: 18px; text-align: center; font-size: 15px; color: var(--fg-muted); }
.pg-title { font-weight: 600; color: var(--fg-strong); font-size: 14px; white-space: nowrap; }
/* đường kẻ mảnh chạy hết bề ngang, ĐẨY mũi tên sang tận bên phải */
.pg-line { flex: 1; height: 1px; background: var(--border-default); }
.pg-body { padding: 2px 0 10px 28px; }

/* một trường: nhãn nhỏ + ô sửa tại chỗ */
.plan-field { display: flex; align-items: flex-start; gap: 10px; margin-bottom: 7px; }
.pf-key {
  flex: 0 0 132px; font-size: 12px; font-weight: 600; color: var(--fg-muted);
  padding-top: 7px; word-break: break-word;
}
.pf-val {
  flex: 1; min-height: 32px; font-size: 13px; line-height: 1.55; color: var(--fg-strong);
  padding: 6px 9px; border: 1px solid transparent; border-radius: var(--radius-md);
  white-space: pre-wrap; word-break: break-word; outline: none;
  transition: background var(--duration-fast) ease, border-color var(--duration-fast) ease;
}
.pf-val:hover { background: var(--bg-hover); }
.pf-val:focus { background: var(--bg-canvas); border-color: var(--accent-400); box-shadow: 0 0 0 3px var(--accent-ring); }
.pf-val:empty::before { content: 'Bấm để nhập…'; color: var(--fg-faint); }
.pf-outline { font-family: var(--font-mono); font-size: 12.5px; min-height: 120px; }
.pf-del {
  flex: none; border: none; background: none; color: var(--fg-faint);
  padding: 6px; border-radius: 4px; line-height: 1;
}
.pf-del:hover { color: var(--danger-600, #dc2626); background: var(--bg-hover); }

.btn-add-src {
  border: 1px dashed var(--border-default); background: none; color: var(--fg-muted);
  border-radius: var(--radius-pill); padding: 6px 14px; font-size: 12.5px; margin-top: 4px;
}
.btn-add-src:hover { color: var(--fg-strong); border-color: var(--accent-400); }

/* Nút Cập nhật theo khối (prompt / nguồn) — replan chỉ khối Chi tiết */
.pc-actions {
  display: flex; justify-content: flex-end; gap: 8px;
  padding: 8px 12px 12px; border-top: 1px solid var(--border-subtle);
}
.pc-src-toolbar {
  display: flex; flex-wrap: wrap; align-items: center; gap: 8px;
  margin-top: 8px; padding-top: 8px; border-top: 1px dashed var(--border-subtle);
}
.btn-section-update {
  padding: 7px 14px !important; font-size: 12.5px !important;
}
.btn-section-update:disabled { opacity: 0.65; cursor: wait; }

.btn-link { border: none; background: none; color: var(--fg-muted); font-size: 12.5px; text-decoration: underline; }
.btn-link:hover { color: var(--fg-strong); }
.plan-raw {
  margin-top: 12px; padding: 12px; border-radius: var(--radius-md); background: var(--bg-muted);
  font-family: var(--font-mono); font-size: 11.5px; line-height: 1.5; color: var(--fg-default);
  white-space: pre-wrap; max-height: 320px; overflow-y: auto;
}

/* Máy hiểu gì từ dàn ý — hiện trước khi duyệt */
.plan-echo {
  display: flex; align-items: flex-start; gap: 8px; margin-top: 10px; padding: 9px 12px;
  border-radius: var(--radius-md); background: var(--accent-soft);
  font-size: 12.5px; line-height: 1.6; color: var(--fg-default);
}
.pe-icon { flex: none; }
.pe-tag {
  display: inline-block; background: var(--bg-canvas); border: 1px solid var(--border-subtle);
  border-radius: var(--radius-pill); padding: 1px 8px; margin: 2px 2px 0 0; font-size: 11.5px;
}

.gate-actions { display: flex; gap: 8px; align-items: center; margin-top: 12px; }
.btn-primary { border: none; color: #fff; border-radius: var(--radius-pill); padding: 10px 18px;
  font-weight: 600; background: linear-gradient(135deg, var(--accent-500), var(--accent-700));
  box-shadow: 0 4px 10px -4px rgba(99,102,241,.5); }
.btn-primary:hover { filter: brightness(1.05); }
.btn-secondary { border: 1px solid var(--border-default); background: var(--bg-canvas); color: var(--fg-default);
  border-radius: var(--radius-pill); padding: 10px 16px; font-weight: 600; }

/* ── Composer ─────────────────────────────────────────────── */
.composer-wrap { flex-shrink: 0; padding: 10px clamp(20px, 5vw, 48px) 16px; position: relative; }
.composer-card { max-width: 820px; margin: 0 auto; background: var(--bg-canvas);
  border: 1px solid var(--border-default); border-radius: var(--radius-2xl); padding: 10px 12px 8px;
  box-shadow: var(--shadow-sm); display: flex; flex-direction: column; gap: 8px; }
.composer-card:focus-within { border-color: var(--accent-400); box-shadow: var(--shadow-md), var(--focus-ring); }
#composerInput { width: 100%; border: none; outline: none; resize: none; font-family: inherit;
  font-size: var(--text-md); color: var(--fg-strong); background: transparent; max-height: 200px;
  min-height: 24px; line-height: var(--leading-normal); padding: 4px; }
.composer-bottom { display: flex; align-items: center; gap: 8px; }
.chip-row { display: flex; flex-wrap: wrap; gap: 6px; }
.file-chip {
  display: inline-flex;
  align-items: center;
  gap: 6px;
  font-size: 12px;
  padding: 4px 10px;
  background: var(--bg-muted);
  border-radius: var(--radius-pill);
  color: var(--fg-default);
  max-width: min(280px, 70vw);
  min-width: 0;
}
.file-chip .fc-name {
  display: inline-flex;
  align-items: baseline;
  min-width: 0;
  overflow: hidden;
}
.file-chip .fc-stem {
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
  min-width: 0;
}
.file-chip .fc-ext {
  flex-shrink: 0;
  opacity: 0.75;
}
.file-chip .fc-x { border: none; background: none; color: var(--fg-muted); }
.tool-btn { display: inline-flex; align-items: center; gap: 6px; padding: 7px 12px; border: 1px solid transparent;
  background: transparent; color: var(--fg-muted); border-radius: var(--radius-pill); font-size: var(--text-sm); font-weight: 500; }
.tool-btn:hover { background: var(--bg-hover); color: var(--fg-strong); }
.btn-send { width: 36px; height: 36px; margin-left: auto; flex-shrink: 0; display: inline-flex;
  align-items: center; justify-content: center; border-radius: 50%; border: none; color: #fff; font-size: 15px;
  background: linear-gradient(135deg, var(--accent-500), var(--accent-700));
  box-shadow: inset 0 1px 0 rgba(255,255,255,.18), 0 4px 10px -4px rgba(99,102,241,.5); }
.btn-send:hover:not(:disabled) { filter: brightness(1.05); transform: translateY(-1px); }
.btn-send:disabled { background: var(--slate-300); box-shadow: none; cursor: not-allowed; }
#composerInput:disabled { opacity: .65; cursor: not-allowed; }
.tool-btn:disabled, .new-chat-link:disabled, #addDocBtn:disabled {
  opacity: .5; cursor: not-allowed; pointer-events: none;
}
/* Đang tạo phiên / stream — composer không nhận focus ring */
body.ui-busy .composer-card { opacity: .85; }
body.ui-busy .composer-card:focus-within {
  border-color: var(--border-default); box-shadow: var(--shadow-sm);
}
.composer-hint { text-align: center; font-size: 11px; color: var(--fg-faint); margin: 8px 0 0; }

/* welcome */
.welcome { max-width: 620px; margin: 8vh auto 0; text-align: center; }
.welcome .w-emblem { width: 56px; height: 56px; border-radius: var(--radius-xl); margin: 0 auto 16px;
  display: flex; align-items: center; justify-content: center; font-size: 26px; color: #fff;
  background: linear-gradient(135deg, var(--accent-500), var(--accent-700)); }
.welcome h2 { font-size: var(--text-2xl); color: var(--fg-strong); margin: 0 0 8px; }
.welcome p { color: var(--fg-muted); margin: 0; }
.is-hidden { display: none !important; }