/* ═══════════════════════════════════════════════════════════
   AutoAI Web SPA (P3) · design/design.css
   CSS dùng CHUNG cho tất cả file thiết kế static trong
   thư mục web/design/ (modal_roles.html, …).
   Nguyên tắc MULTI-LAYER:
     · Modal cha  (.design-modal)          z-index 50
     · Modal con  (.design-modal--child)   z-index 65, backdrop riêng,
       mở chồng TRÊN modal cha; modal cha được thêm class .is-dimmed.
     · Mọi khu vực dài (list, form, pre…) dùng .design-scroll
       → mỗi khu vực có scroll RIÊNG BIỆT, không kéo cả modal.
   Biến màu/font copy từ web/css/app.css (đồng bộ theme hiện hành).
   ═══════════════════════════════════════════════════════════ */

/* ─────────── VARIABLES (copy từ app.css :root) ─────────── */
:root {
  --bg: #f3f5f9;
  --bg-soft: #f8fafc;
  --surface: #ffffff;
  --border: #e3e8ef;
  --border-soft: #edf1f6;
  --text: #1c2333;
  --muted: #64748b;
  --primary: #4f6bf5;
  --primary-dark: #3b53d6;
  --primary-soft: #eef1fe;
  --danger: #dc2626;
  --danger-soft: #fee2e2;
  --ok: #16a34a;
  --ok-soft: #dcfce7;
  --warn: #d97706;
  --warn-soft: #fef3c7;
  --radius: 12px;
  --radius-sm: 8px;
  --shadow: 0 1px 2px rgba(16, 24, 40, .06), 0 1px 3px rgba(16, 24, 40, .10);
  --shadow-lg: 0 12px 40px rgba(16, 24, 40, .18);
  --font: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif;
  --mono: ui-monospace, "Cascadia Code", "JetBrains Mono", Consolas, monospace;
}

/* ─────────── BASE (chỉ cho trang thiết kế xem bằng file://) ─────────── */
* { box-sizing: border-box; }
html, body {
  margin: 0; padding: 0;
  font-family: var(--font);
  font-size: 14px;
  color: var(--text);
  background:
    radial-gradient(1200px 500px at 20% -10%, rgba(79, 107, 245, .10), transparent 60%),
    var(--bg);
  min-height: 100vh;
}
.design-page-note {
  position: fixed; left: 14px; bottom: 14px;
  z-index: 90;
  background: #0f172a; color: #f1f5f9;
  font-size: 12px; line-height: 1.5;
  padding: 8px 12px; border-radius: 10px;
  box-shadow: var(--shadow-lg);
  max-width: 420px;
}

/* FIX modal_agents (lỗi che footer): thuộc tính hidden LUÔN thắng mọi
   display inline — tránh tab-pane "hidden" bị style="display:flex" ghi đè
   → 2 tab-pane cùng hiện → chia đôi chiều cao body → form tràn đè footer. */
[hidden] { display: none !important; }

/* ─────────── MODAL SHELL — LAYER 1 (modal cha, z-index 50) ─────────── */
.design-modal {
  position: fixed; inset: 0;
  z-index: 50;
}
.design-modal-backdrop {
  position: absolute; inset: 0;
  background: rgba(15, 23, 42, .5);
  backdrop-filter: blur(2px);
  transition: background .18s ease;
}
.design-modal-panel {
  position: absolute;
  left: 50%; top: 50%;
  transform: translate(-50%, -50%);
  width: min(980px, calc(100vw - 32px));
  height: min(680px, calc(100vh - 48px));
  background: var(--surface);
  border-radius: 16px;
  box-shadow: var(--shadow-lg);
  display: flex; flex-direction: column;
  overflow: hidden;
  transition: filter .18s ease, opacity .18s ease;
}
/* Modal con mở → modal cha mờ đi nhẹ (chỉ backdrop + panel, không ẩn) */
.design-modal.is-dimmed .design-modal-backdrop { background: rgba(15, 23, 42, .62); }
.design-modal.is-dimmed .design-modal-panel { filter: brightness(.82) saturate(.8); }

/* ─────────── MODAL SHELL — LAYER 2 (modal con, z-index 65, backdrop riêng) ─────────── */
.design-modal--child {
  position: fixed; inset: 0;
  z-index: 65;
}
.design-modal--child .design-modal-backdrop {
  background: rgba(15, 23, 42, .45);
  backdrop-filter: blur(1px);
}
.design-modal--child .design-modal-panel {
  width: min(720px, calc(100vw - 48px));
  height: min(640px, calc(100vh - 64px));
}

/* ─────────── HEAD / BODY / FOOT (dùng chung 2 layer) ─────────── */
.design-head {
  display: flex; align-items: center; gap: 10px;
  padding: 14px 18px;
  border-bottom: 1px solid var(--border);
  flex: none;
}
.design-head h2 {
  margin: 0; font-size: 16px; font-weight: 700; flex: 1;
  min-width: 0; overflow: hidden; text-overflow: ellipsis; white-space: nowrap;
}
.design-head .sub {
  font-size: 12px; color: var(--muted); font-weight: 400;
  margin-left: 8px;
}
.design-body {
  flex: 1; min-height: 0;
  display: flex; flex-direction: column;
  padding: 16px 18px;
}
.design-foot {
  display: flex; justify-content: flex-end; align-items: center; gap: 8px;
  padding: 12px 18px;
  border-top: 1px solid var(--border);
  background: var(--bg-soft);
  flex: none;
}

/* ─────────── .design-scroll — khu vực scroll RIÊNG BIỆT ───────────
   Quy tắc: mọi danh sách / form / pre dài ĐỀU bọc trong .design-scroll
   để scroll nội bộ, không làm kéo giãn hoặc cắt modal. */
.design-scroll {
  max-height: 100%;
  overflow-y: auto;
  overscroll-behavior: contain;
  scrollbar-width: thin;
  scrollbar-color: var(--border) transparent;
}
.design-scroll::-webkit-scrollbar { width: 8px; height: 8px; }
.design-scroll::-webkit-scrollbar-track { background: transparent; }
.design-scroll::-webkit-scrollbar-thumb {
  background: var(--border);
  border-radius: 8px;
}
.design-scroll::-webkit-scrollbar-thumb:hover { background: var(--muted); }
/* FIX L2: khi .design-scroll là container CHÍNH (trực tiếp) của .design-body
   → flex-fill lấp đầy chiều cao body (nội dung ngắn: vùng scroll cao = body,
   không để khoảng trắng đáy; nội dung dài: scroll nội bộ). */
.design-body > .design-scroll { flex: 1; min-height: 0; }
/* FIX modal_tools: .design-field con TRỰC TIẾP của .design-scroll → flex-fill
   để pre-scroll bên trong có chiều cao đầy đủ, không để trống đáy. */
.design-body > .design-scroll > .design-field { flex: 1; min-height: 0; }

/* FIX 1.0.39 BUG C: guideline edit pane (#guidelineEdit.design-field — con trực
   tiếp của .design-body.design-scroll) → textarea fill hết chiều cao vùng scroll,
   không còn khoảng trắng đáy. View pane (#guidelineView) fill tương tự.
 */
/* FIX 1.0.39 BUG V3: #guidelineEdit tự nó phải flex-fill .design-body.design-scroll
   (trước chỉ có con .design-textarea bên trong được flex:1 → edit pane 298px,
   trống 263px đáy). */
.design-body.design-scroll > #guidelineEdit { flex: 1; min-height: 0; }
.design-body.design-scroll > #guidelineEdit > .design-textarea {
  flex: 1; min-height: 0; height: 100%;
}
.design-body.design-scroll > #guidelineView { flex: 1; min-height: 0; }
/* FIX modal_tools: .design-pre-scroll con TRỰC TIẾP của .design-field (nội dung chính)
   → fill hết field thay vì cố định 300px; pre dài scroll bên trong vùng fill. */
.design-body > .design-scroll > .design-field > .design-pre-scroll {
  flex: 1; min-height: 0; max-height: none;
}
/* FIX L2/L3: split 2 cột (agents) — grid phải fill body để 2 cột scroll
   (list | form) cao đúng bằng body, form không tràn qua footer. */
.design-body > .design-grid-2col { flex: 1; min-height: 0; }

/* ─────────── LIST (danh sách bên trái layer 1) ─────────── */
.design-list {
  display: flex; flex-direction: column; gap: 4px;
  padding: 8px;
  background: var(--bg-soft);
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  height: 100%;
  min-height: 100%; /* FIX: list NGẮN (vd dtree tasks) vẫn fill hết .design-scroll — không để trống đáy; list dài đã ≥100% nên không đổi */
}
.design-list-row {
  display: flex; align-items: center; gap: 8px;
  padding: 9px 12px;
  border: 1px solid var(--border); border-radius: var(--radius-sm);
  background: var(--surface);
  cursor: pointer; font-size: 13px;
  flex: none;
}
.design-list-row:hover { background: var(--bg-soft); }
.design-list-row.selected {
  border-color: var(--primary); background: var(--primary-soft);
}
.design-list-row .grow {
  flex: 1; min-width: 0;
  overflow: hidden; text-overflow: ellipsis; white-space: nowrap;
  font-weight: 600;
}
.design-list-row .meta {
  font-size: 11px; color: var(--muted);
  white-space: nowrap;
  flex: 0 1 auto;
  max-width: 45%;
  overflow: hidden;
  text-overflow: ellipsis;
}
/* [REQ-C] Row roles: wrapper flex-column — title tren, description chu nho duoi. */
.design-list-row .row-main {
  display: flex; flex-direction: column; gap: 2px;
  flex: 1; min-width: 0;
}
.design-list-row .row-title {
  font-weight: 600;
  overflow: hidden; text-overflow: ellipsis; white-space: nowrap;
}
.design-list-row .row-desc {
  font-size: 11px; color: var(--muted);
  white-space: nowrap; overflow: hidden; text-overflow: ellipsis;
}
.design-list-row .row-desc:empty { display: none; }

/* ─────────── NOTE LINES (note line list inside #paneNote) — FIX BUG-4 ───────────
   Note lines render DIRECTLY inside #paneNote (.design-card) without .design-list
   → long notes (50+ lines) stretch the card infinitely, rows stick together.
   Wrapper .design-note-lines (modal_noteplan_editor.tpl): max-height + internal scroll
   + flex column gap 4px (editor .design-scroll kept as fallback scroll). */
#paneNote .design-note-lines {
  display: flex; flex-direction: column; gap: 4px;
  max-height: 420px;
  overflow-y: auto;
  overscroll-behavior: contain;
  scrollbar-width: thin;
  scrollbar-color: var(--border) transparent;
}
#paneNote .design-note-lines::-webkit-scrollbar { width: 8px; }
#paneNote .design-note-lines::-webkit-scrollbar-track { background: transparent; }
#paneNote .design-note-lines::-webkit-scrollbar-thumb {
  background: var(--border); border-radius: 8px;
}
#paneNote .design-note-lines::-webkit-scrollbar-thumb:hover { background: var(--muted); }

/* ─────────── GRID 2 CỘT (split list | chi tiết) ─────────── */
.design-grid-2col {
  display: grid;
  grid-template-columns: minmax(240px, 360px) minmax(0, 1fr);
  grid-template-rows: minmax(0, 1fr); /* FIX L3: row fill container → cột .design-scroll bên trong clip đúng, form không tràn footer */
  gap: 14px;
  height: 100%;
  min-height: 0;
}
.design-grid-2col > * { min-width: 0; min-height: 0; }

/* ─────────── CARD ─────────── */
.design-card {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  box-shadow: var(--shadow);
  padding: 12px 14px;
  margin-bottom: 12px;
}
.design-card:last-child { margin-bottom: 0; }
.design-card-title {
  font-size: 13px; font-weight: 700;
  margin: 0 0 10px;
  display: flex; align-items: center; gap: 8px;
}
.design-card-title .note {
  font-size: 11px; font-weight: 400; color: var(--muted);
}

/* ─────────── FORM ─────────── */
.design-form { display: flex; flex-direction: column; gap: 12px; }
.design-field { display: flex; flex-direction: column; gap: 5px; }
.design-field > span {
  font-size: 12px; font-weight: 600; color: var(--muted);
}
.design-input, .design-textarea, .design-select {
  font: inherit; font-size: 13px;
  color: var(--text);
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  padding: 8px 10px;
  width: 100%;
}
.design-input:focus, .design-textarea:focus, .design-select:focus {
  outline: none;
  border-color: var(--primary);
  box-shadow: 0 0 0 3px var(--primary-soft);
}
.design-textarea { resize: vertical; line-height: 1.5; }
.design-row {
  display: flex; align-items: center; gap: 8px; flex-wrap: wrap;
}
.design-row .grow { flex: 1; }
.design-muted { font-size: 12px; color: var(--muted); line-height: 1.5; }
.design-badge {
  display: inline-block;
  font-size: 11px; font-weight: 600;
  padding: 2px 8px; border-radius: 999px;
  background: var(--primary-soft); color: var(--primary-dark);
}
.design-badge.ok { background: var(--ok-soft); color: var(--ok); }
.design-badge.warn { background: var(--warn-soft); color: var(--warn); }
.design-badge.danger { background: var(--danger-soft); color: var(--danger); }

/* ─────────── CHECKBOX GRID (Tools / Contexts / Managed roles) ─────────── */
.design-checkbox-grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(160px, 1fr));
  gap: 6px 12px;
  padding: 10px 12px;
  background: var(--bg-soft);
  border: 1px solid var(--border-soft);
  border-radius: var(--radius-sm);
}
.design-checkbox-grid label {
  display: inline-flex; align-items: center; gap: 6px;
  font-size: 13px; cursor: pointer;
  min-width: 0;
}
.design-checkbox-grid label span {
  overflow: hidden; text-overflow: ellipsis; white-space: nowrap;
}
.design-checkbox-grid input[type="checkbox"] { accent-color: var(--primary); }

/* ─────────── PRE (đoạn text mono: authorized, output…) ─────────── */
.design-pre {
  font-family: var(--mono);
  font-size: 12px; line-height: 1.55;
  background: #0f172a; color: #e2e8f0;
  border-radius: var(--radius-sm);
  padding: 10px 12px;
  margin: 0;
  white-space: pre-wrap; word-break: break-word;
}

/* ─────────── BUTTONS ─────────── */
.design-btn {
  font: inherit; font-size: 13px; font-weight: 600;
  color: var(--text);
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  padding: 7px 14px;
  cursor: pointer;
  transition: background .12s ease, border-color .12s ease;
}
.design-btn:hover { background: var(--bg-soft); }
.design-btn-sm { padding: 4px 10px; font-size: 12px; }
.design-btn-primary {
  background: var(--primary); border-color: var(--primary); color: #fff;
}
.design-btn-primary:hover { background: var(--primary-dark); }
.design-btn-danger {
  background: var(--danger-soft); border-color: transparent; color: var(--danger);
}
.design-btn-danger:hover { background: var(--danger); color: #fff; }
.design-close {
  border: none; background: none; font-size: 16px;
  cursor: pointer; color: var(--muted);
  padding: 4px 8px; border-radius: 8px; line-height: 1;
}
.design-close:hover { background: var(--bg-soft); color: var(--text); }

/* ─────────── KEY-VALUE (panel thông tin nhanh, layer 1 cột phải) ─────────── */
.design-kv { display: flex; flex-direction: column; gap: 8px; }
.design-kv-row {
  display: flex; align-items: baseline; gap: 10px;
  font-size: 13px;
}
.design-kv-row .k {
  width: 130px; flex: none;
  color: var(--muted); font-size: 12px; font-weight: 600;
}
.design-kv-row .v { flex: 1; min-width: 0; word-break: break-word; }

/* ─────────── TABS (modal cha: Notes | Plans) — thêm mới 2025 ─────────── */
.design-tabs {
  display: flex; gap: 4px;
  padding: 10px 18px 0;
  border-bottom: 1px solid var(--border);
  background: var(--bg-soft);
  flex: none;
}
.design-tab-btn {
  font: inherit; font-size: 13px; font-weight: 600;
  color: var(--muted);
  background: none;
  border: none; border-bottom: 2px solid transparent;
  padding: 8px 14px;
  cursor: pointer;
  border-radius: 8px 8px 0 0;
}
.design-tab-btn:hover { color: var(--text); background: var(--border-soft); }
.design-tab-btn.active {
  color: var(--primary-dark);
  border-bottom-color: var(--primary);
  background: var(--surface);
}
.design-tab-btn .design-badge { margin-left: 6px; }

/* ─────────── PRE-SCROLL (vùng scroll RIÊNG cho pre dài) — thêm modal_tools ───────────
   Bọc <pre class="design-pre"> để pre có max-height + scroll nội bộ,
   không kéo giãn .design-scroll của tab (modal_tools.html). */
.design-pre-scroll {
  max-height: 300px;
  overflow-y: auto;
  overscroll-behavior: contain;
  scrollbar-width: thin;
  scrollbar-color: var(--border) transparent;
}
.design-pre-scroll::-webkit-scrollbar { width: 8px; height: 8px; }
.design-pre-scroll::-webkit-scrollbar-track { background: transparent; }
.design-pre-scroll::-webkit-scrollbar-thumb {
  background: var(--border);
  border-radius: 8px;
}
.design-pre-scroll::-webkit-scrollbar-thumb:hover { background: var(--muted); }

/* ─────────── PRE-SCROLL --tall (vùng scroll cao hơn cho pre context dài) — thêm modal_contexts ───────────
   Modal con kết quả fetch context: pre rất dài (full text module),
   cần vùng scroll cao hơn .design-pre-scroll (300px) → 420px. */
.design-pre-scroll--tall {
  max-height: 420px;
}

/* ─────────── TABLE (danh sách dạng bảng: projects, …) — thêm modal_settings ───────────
   Table trong .design-scroll của tab pane (modal_settings.html). */
.design-table {
  width: 100%;
  border-collapse: collapse;
  font-size: 13px;
}
.design-table th {
  text-align: left;
  font-size: 11px; font-weight: 700; color: var(--muted);
  text-transform: uppercase; letter-spacing: .04em;
  padding: 6px 10px;
  border-bottom: 1px solid var(--border);
  background: var(--bg-soft);
}
.design-table td {
  padding: 8px 10px;
  border-bottom: 1px solid var(--border-soft);
  vertical-align: middle;
  word-break: break-word;
}
.design-table tbody tr:hover { background: var(--bg-soft); }
.design-table tbody tr:last-child td { border-bottom: none; }

/* ─────────── FIX L2/L6: pre-scroll là container CHÍNH của body → flex-fill ───────────
   modal_contexts (layer 2): .design-pre-scroll--tall nằm TRỰC TIẾP trong
   .design-body (padding:0) → flex-fill để vùng scroll cao = body; pre DÀI
   luôn CLIP trong scroller, không "vượt" panel, không để khoảng trắng đáy.
   (max-height 420px của --tall chỉ còn hiệu lực khi pre-scroll là card CON,
   KHÔNG phải container chính của body — nên đặt max-height: none ở đây.) */
.design-body > .design-pre-scroll,
.design-body > .design-pre-scroll--tall { flex: 1; min-height: 0; max-height: none; }

/* ─────────── FIX L1: RESPONSIVE — màn hẹp ≤820px: split 2 cột → 1 cột ───────────
   Panel đã tự co theo viewport (width: min(980px, calc(100vw - 32px)));
   ở viewport 768px split list|form 2 cột quá hẹp → xếp 1 cột dọc. */
@media (max-width: 820px) {
  .design-grid-2col { grid-template-columns: 1fr; }
}

/* ─────────── MARKDOWN RENDERED (.design-md) — thêm modal_roles (modal Guideline) ───────────
   View "rendered" của guideline: Markdown → HTML (h1 + ul/li + code inline).
   Class MỚI — không ảnh hưởng file khác. */
.design-md { font-size:13px; line-height:1.6; color:var(--text); }
.design-md h1 { font-size:16px; font-weight:700; margin:0 0 10px; border-bottom:1px solid var(--border); padding-bottom:8px; }
.design-md ul { margin:0; padding-left:20px; }
.design-md li { margin:4px 0; }
.design-md code { font-family:var(--mono); font-size:12px; background:var(--bg-soft); padding:1px 5px; border-radius:4px; }

/* ─────────── SUBPANEL NESTED (cấp 2 trong card Tools) — thêm modal_roles RoleEditor ───────────
   Panel phân quyền 2 cấp (CreateTask / GetAIRole): indent + border-left,
   nằm DƯỚI grid cấp 1; mỗi panel được checkbox cha (data-controls) điều khiển. */
.design-subpanel {
  margin: 12px 0 0 14px;
  padding-left: 12px;
  border-left: 3px solid var(--primary);
  transition: opacity .15s ease;
}
.design-subpanel + .design-subpanel { margin-top: 16px; }
.design-subpanel .design-card-title { margin-bottom: 6px; }
/* STATE DISABLED — checkbox cha bỏ tick (design.js [data-controls]) */
.design-subpanel.is-disabled { opacity: .45; }
.design-subpanel.is-disabled label span { color: var(--muted); }
.design-subpanel.is-disabled .design-card-title { text-decoration: line-through; }

/* RADIO LIST 1 CỘT — GetAIRole (single-select, mỗi dòng 1 radio + tên role) */
.design-radio-list {
  display: grid;
  grid-template-columns: 1fr;
  gap: 6px;
  padding: 10px 12px;
  background: var(--bg-soft);
  border: 1px solid var(--border-soft);
  border-radius: var(--radius-sm);
}
.design-radio-list label {
  display: inline-flex; align-items: center; gap: 6px;
  font-size: 13px; cursor: pointer;
  min-width: 0;
}
.design-radio-list label span {
  overflow: hidden; text-overflow: ellipsis; white-space: nowrap;
}
.design-radio-list input[type="radio"] { accent-color: var(--primary); }

/* ─────────── TOOL TREE (cây 2 cấp trong card Tools) — thêm modal_roles RoleEditor ───────────
   Cấp 1: 1 danh sách dọc duy nhất (.design-tool-row).
   Cấp 2: khối role con (.design-tool-children) indent NGAY DƯỚI tool cha —
   vẫn mang class .design-subpanel → inherit behavior disable (data-controls) + .is-disabled.
   Append-only — không sửa rule cũ. */
.design-tool-tree {
  display: flex; flex-direction: column;
  gap: 4px;
  padding: 10px 12px;
  background: var(--bg-soft);
  border: 1px solid var(--border-soft);
  border-radius: var(--radius-sm);
}
.design-tool-row {
  display: inline-flex; align-items: center; gap: 8px;
  font-size: 13px; font-weight: 600;
  cursor: pointer;
  padding: 4px 8px;
  border-radius: var(--radius-sm);
}
.design-tool-row:hover { background: var(--border-soft); }
.design-tool-row input[type="checkbox"] { accent-color: var(--primary); }
/* Khối role con: indent + border-left mảnh để phân cấp (không phải panel riêng) */
.design-tool-children {
  padding-left: 22px;
  border-left: 2px solid var(--muted);
  margin: 4px 0 8px;
  display: flex; flex-direction: column; gap: 2px;
}
.design-tool-child-row {
  display: inline-flex; align-items: center; gap: 6px;
  font-size: 12px;
  color: var(--text-soft, var(--muted));
  cursor: pointer;
  padding: 3px 8px;
  border-radius: var(--radius-sm);
}
.design-tool-child-row:hover { background: var(--border-soft); }
.design-tool-child-row input[type="checkbox"],
.design-tool-child-row input[type="radio"] { accent-color: var(--primary); }
/* Override: khi .design-subpanel đóng vai khối con trong TREE (card Tools RoleEditor)
   → giữ margin/padding/border-left của .design-tool-children, không áp indent panel
   (border-left 3px primary) của .design-subpanel gốc. Append-only, đặt sau rule gốc. */
.design-subpanel.design-tool-children {
  margin: 4px 0 8px;
  padding-left: 22px;
  border-left: 2px solid var(--muted);
}

/* ────── [CTX-PERM-G1/B2-FIX] RoleEditor: footer overlap card Contexts ──────
   Panel RoleEditor = flex column (head / body.design-scroll / foot). Card Contexts
   dài (1538px) → nội dung cuối (hàng checkbox quyền con) nẫm sát mép đáy scroller,
   footer (58px) trông như chống lên + nút "Lưu" bị cắt + hàng checkbox cuối khó bám.
   FIX: scroller RoleEditor thêm padding-bottom = chiều cao footer (~64px) để
   nội dung cuối có khoảng trống, KHÔNG bị che sát footer. Scoped theo #modal-roleeditor
   (KHÔNG ánh hưởng modal khác dùng .design-body.design-scroll). Append-only. */
#modal-roleeditor .design-body.design-scroll {
  padding-bottom: 64px;
}
/* ─────────── [CTX-PERM-V2] CONTEXTS TREE (cây indent — card Contexts RoleEditor) ───────────
   Thay layout .design-checkbox-grid + subpanel (grid-column:1/-1) cũ bằng CÂY INDENT:
   mỗi context = 1 dòng checkbox cấp 0 (.ctx-level0, căn trái); quyền con = các dòng
   checkbox indent (.ctx-level1) trong .ctx-children ngay bên dưới (indent thuần
   + border-left subtle). Context không có quyền → không có .ctx-children (1 dòng).
   Scoped theo #modal-roleeditor — append-only, KHÔNG sửa rule cũ. */
#modal-roleeditor .ctx-tree {
  display: flex; flex-direction: column;
  gap: 2px;
  padding: 10px 12px;
  background: var(--bg-soft);
  border: 1px solid var(--border-soft);
  border-radius: var(--radius-sm);
}
#modal-roleeditor .ctx-row {
  display: inline-flex; align-items: center; gap: 6px;
  font-size: 13px; cursor: pointer;
  padding: 3px 8px;
  border-radius: var(--radius-sm);
  min-width: 0;
}
#modal-roleeditor .ctx-row:hover { background: var(--border-soft); }
#modal-roleeditor .ctx-row input[type="checkbox"] { accent-color: var(--primary); }
#modal-roleeditor .ctx-row span {
  overflow: hidden; text-overflow: ellipsis; white-space: nowrap;
}
/* Cấp 0 — context: căn trái, nửa đậm. */
#modal-roleeditor .ctx-level0 {
  padding-left: 8px;
  font-weight: 600;
}
/* Khối quyền con: indent thuần (padding-left) + border-left subtle phân cấp. */
#modal-roleeditor .ctx-children {
  padding-left: 30px;
  border-left: 2px solid var(--border-soft);
  margin: 2px 0 6px;
  display: flex; flex-direction: column; gap: 2px;
}
/* Cấp 1 — permission: indent sâu hơn dòng cha. */
#modal-roleeditor .ctx-level1 {
  padding-left: 32px;
  font-weight: 400;
  font-size: 12px;
  color: var(--text-soft, var(--muted));
}
/* STATE DISABLED — context cha bỏ tick (data-controls → .ctx-children, _syncControls).
   Reuse pattern .is-disabled (opacity + muted) như .design-subpanel. */
#modal-roleeditor .ctx-children.is-disabled { opacity: .45; }
#modal-roleeditor .ctx-children.is-disabled .ctx-level1 span { color: var(--muted); }
}

/* ─────────── [G2-6] TASK TREE (cây task — modal_tasks, layer 1) ───────────
   Port từ web/design/modal_tasks.html (design-only <style>) — class .dtree-*
   chưa có trong design.css production. Append-only. */
.dtree { display: flex; flex-direction: column; gap: 2px; padding: 8px; min-height: 100%; }
.dtree-node { flex: none; }
.dtree-row {
  display: flex; align-items: center; gap: 8px;
  padding: 8px 10px;
  border: 1px solid var(--border); border-radius: var(--radius-sm);
  background: var(--surface);
  font-size: 13px; cursor: pointer;
}
.dtree-row:hover { background: var(--bg-soft); }
.dtree-row.selected { border-color: var(--primary); background: var(--primary-soft); }
.dtree-caret {
  border: none; background: none; cursor: pointer;
  font-size: 10px; color: var(--muted);
  width: 16px; flex: none; padding: 0;
}
.dtree-name { flex: 1; min-width: 0; font-weight: 600;
  overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.dtree-children { margin-left: 22px; padding-left: 10px;
  border-left: 2px solid var(--border-soft);
  display: flex; flex-direction: column; gap: 2px; }
.dtree-children.hidden { display: none; }
.dtree-status { font-size: 11px; font-weight: 600; flex: none; }
.st-pending { color: var(--muted); }
.st-running { color: var(--primary-dark); }
.st-done { color: var(--ok); }
.st-canceled { color: var(--danger); }
/* Badge role rỗng (chưa assign role) → ẩn (độ rộng 0) — chỉ dùng trong .dtree-row */
.dtree-row .design-badge:empty { display: none; }

/* ═══════════════════════════════════════════════════════════
   [G2-8] CỘT FILE TREE (phải, 300px) — port từ web/design/filetree_column.html
   (prefix .ft-*; giữ nguyên rule). Cột mount vào #filetreeMount (main-grid).
   ═══════════════════════════════════════════════════════════ */

/* Mount point: fill grid track; TPL wrapper (.tpl-mount > .tpl-modal) fill mount */
.ft-mount { min-width: 0; height: 0; overflow: visible; } /* [topbar v2 §5.4] mount chỉ là container cho .ft-col fixed — KHÔNG chiếm grid space (height:0, overflow:visible) */
.ft-mount .tpl-mount, .ft-mount .tpl-modal { display: block; width: 100%; height: 100%; }

/* [topbar v2 §5.4] File tree = flyout overlay trượt từ phải (cả desktop + mobile) —
   KHÔNG còn grid column 300px: .main-grid giữ layout 2 cột, chat full width. */

/* Nút toggle 🗂 trong topbar — state active (cột đang hiện) */
.btn-ghost.ft-active {
  background: rgba(79, 107, 245, .35);
  border-color: rgba(79, 107, 245, .7);
  color: #fff;
}

/* ── FILE TREE FLYOUT (overlay trượt từ phải, cả desktop + mobile — §5.4) ──
   Desktop: fixed overlay 300px (trước: grid column 300px, chat co lại instant).
   Mobile (≤768px): cùng pattern — width min(300px, 85vw) qua media override bên dưới. */
.ft-col {
  position: fixed;
  top: 0; right: 0; bottom: 0;
  width: 300px;
  z-index: 200; /* [backdrop] trên drawer-backdrop(185) — drawer luôn nổi trên backdrop */
  transform: translateX(100%);
  transition: transform .25s ease;
  background: var(--surface);
  border-left: 1px solid var(--border);
  box-shadow: -2px 0 8px rgba(0,0,0,.2);
  display: flex; flex-direction: column;
  min-height: 0; min-width: 0;
  height: 100%;
}
.main-grid.ft-shown .ft-col { transform: translateX(0); }
.ft-col-head {
  display: flex; align-items: center; gap: 8px;
  padding: 10px 12px;
  border-bottom: 1px solid var(--border);
  flex: 0 0 auto;
}
.ft-col-head .title {
  font-weight: 700; font-size: 13px;
  flex: 1; min-width: 0;
  overflow: hidden; text-overflow: ellipsis; white-space: nowrap;
}
.ft-close {
  border: none; background: none; font-size: 15px;
  cursor: pointer; color: var(--muted);
  padding: 4px 8px; border-radius: 8px; flex: 0 0 auto;
}
.ft-close:hover { background: var(--bg-soft); color: var(--text); }

/* Tree: scroll RIÊNG của cột (flex:1 + min-height:0 — không kéo giãn chat) */
.ft-tree {
  flex: 1; min-height: 0;
  overflow-y: auto;
  padding: 8px;
  font-size: 13px;
}

/* Row chung (folder + file) */
.ft-row {
  display: flex; align-items: center; gap: 6px;
  padding: 5px 8px;
  border-radius: var(--radius-sm);
  border-left: 3px solid transparent;
  cursor: pointer;
  user-select: none;
  white-space: nowrap;
}
.ft-row:hover { background: var(--bg-soft); }
.ft-row .caret {
  width: 14px; flex: 0 0 auto;
  font-size: 11px; color: var(--muted);
  text-align: center;
}
.ft-row .name {
  flex: 1; min-width: 0;
  overflow: hidden; text-overflow: ellipsis;
}
.ft-row .size {
  font-size: 11px; color: var(--muted);
  font-family: var(--mono); flex: 0 0 auto;
}
.ft-folder .name { font-weight: 600; }
.ft-file .name { font-family: var(--mono); font-size: 12px; }
.ft-file.selected {
  background: var(--primary-soft);
  border-left-color: var(--primary);
}
.ft-icon { width: 18px; flex: 0 0 auto; text-align: center; font-size: 13px; }

/* Khối con của folder — collapse bằng attribute hidden */
.ft-children {
  margin-left: 14px;
  padding-left: 8px;
  border-left: 1px solid var(--border-soft);
}

/* ═══════════════════════════════════════════════════════════
   [G2-8] MODAL FILEVIEWER — port từ web/design/modal_fileviewer.html
   (prefix .fv-*; giữ nguyên rule). z-65 (design-modal--child).
   ═══════════════════════════════════════════════════════════ */

/* Modal panel: rộng hơn child mặc định (viewer cần ngang) */
#modal-fileviewer .design-modal-panel {
  width: min(860px, calc(100vw - 48px));
  height: min(660px, calc(100vh - 48px));
}

/* Header: icon + tên + path + badge loại + selector file */
.fv-head-file {
  display: flex; align-items: center; gap: 8px;
  flex: 1; min-width: 0;
}
.fv-head-file .fv-ico { font-size: 16px; flex: none; }
.fv-head-file .fv-name {
  font-size: 14px; font-weight: 700;
  font-family: var(--mono);
}
.fv-head-file .fv-path {
  font-size: 11px; color: var(--muted);
  font-family: var(--mono);
  overflow: hidden; text-overflow: ellipsis; white-space: nowrap;
}
.fv-type-select {
  font: inherit; font-size: 12px; font-weight: 600;
  color: var(--text);
  background: var(--bg-soft);
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  padding: 5px 8px;
  cursor: pointer;
  flex: none;
}
.fv-type-select:focus {
  outline: none; border-color: var(--primary);
  box-shadow: 0 0 0 3px var(--primary-soft);
}

/* Toolbar tabs chế độ Xem | Sửa */
.fv-tabbar {
  display: flex; align-items: center; gap: 8px;
  padding: 8px 18px;
  border-bottom: 1px solid var(--border);
  background: var(--bg-soft);
  flex: none;
}
.fv-mode-tab {
  font: inherit; font-size: 12px; font-weight: 600;
  color: var(--muted);
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 999px;
  padding: 5px 14px;
  cursor: pointer;
  transition: background .12s ease, color .12s ease, border-color .12s ease;
}
.fv-mode-tab:hover:not(:disabled) { background: var(--border-soft); color: var(--text); }
.fv-mode-tab.active {
  background: var(--primary); border-color: var(--primary); color: #fff;
}
.fv-mode-tab:disabled {
  opacity: .45; cursor: not-allowed;
}
.fv-tabbar .grow { flex: 1; }
.fv-save-btn {
  font: inherit; font-size: 12px; font-weight: 600;
  color: #fff;
  background: var(--ok); border: 1px solid var(--ok);
  border-radius: var(--radius-sm);
  padding: 5px 12px;
  cursor: pointer;
}
.fv-save-btn:hover { background: #15803d; }

/* ── VIEW CODE: toolbar + line numbers + code ── */
.fv-code-toolbar {
  display: flex; align-items: center; gap: 10px;
  padding: 6px 12px;
  border-bottom: 1px solid var(--border);
  background: var(--bg-soft);
  font-size: 11px; color: var(--muted);
  font-family: var(--mono);
  flex: none;
}
.fv-code-toolbar .grow { flex: 1; }
.fv-code-wrap {
  flex: 1; min-height: 0;
  overflow: auto;
  background: #0f172a;
  font-family: var(--mono);
  font-size: 12px; line-height: 1.6;
}
.fv-code-line {
  display: flex;
  white-space: pre;
}
.fv-code-line .ln {
  width: 44px; flex: none;
  text-align: right; padding: 0 10px 0 0;
  color: #475569;
  user-select: none;
  border-right: 1px solid #1e293b;
}
.fv-code-line .lc {
  padding: 0 14px;
  color: #e2e8f0;
  flex: 1; min-width: 0;
}
/* Token màu (KHÔNG highlight thật) */
.fv-kw  { color: #93c5fd; font-weight: 600; }
.fv-str { color: #86efac; }
.fv-com { color: #64748b; font-style: italic; }
.fv-num { color: #fcd34d; }
/* Chế độ Sửa: border highlight + ghi chú */
.fv-code-wrap.is-editing {
  box-shadow: inset 0 0 0 2px var(--primary);
}
.fv-edit-hint {
  display: none;
  padding: 6px 12px;
  background: var(--primary-soft);
  border-top: 1px solid var(--primary-soft);
  font-size: 11px; color: var(--primary-dark);
  flex: none;
}
.fv-code-wrap.is-editing + .fv-edit-hint { display: block; }

/* ── VIEW MARKDOWN: sub-tab Render/Raw + render + raw ── */
.fv-md-subbar {
  display: flex; align-items: center; gap: 6px;
  padding: 6px 12px;
  border-bottom: 1px solid var(--border);
  background: var(--bg-soft);
  flex: none;
}
.fv-md-subbar .grow { flex: 1; }
.fv-md-sub {
  font: inherit; font-size: 11px; font-weight: 600;
  color: var(--muted);
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 999px;
  padding: 3px 12px;
  cursor: pointer;
}
.fv-md-sub.active {
  background: var(--primary-soft);
  border-color: var(--primary);
  color: var(--primary-dark);
}
.fv-md-render {
  flex: 1; min-height: 0;
  padding: 14px 16px;
  overflow-y: auto;
}
.fv-md-render table {
  border-collapse: collapse;
  margin: 8px 0;
  font-size: 12px;
}
.fv-md-render th, .fv-md-render td {
  border: 1px solid var(--border);
  padding: 5px 10px;
  text-align: left;
}
.fv-md-render th { background: var(--bg-soft); font-weight: 700; }

/* ── VIEW IMAGE: toolbar zoom + placeholder gradient ── */
.fv-img-toolbar {
  display: flex; align-items: center; gap: 8px;
  padding: 6px 12px;
  border-bottom: 1px solid var(--border);
  background: var(--bg-soft);
  flex: none;
}
.fv-img-toolbar .grow { flex: 1; }
.fv-img-toolbar .zoom {
  font-family: var(--mono);
  font-size: 12px; font-weight: 700;
  min-width: 48px; text-align: center;
}
.fv-img-stage {
  flex: 1; min-height: 0;
  display: flex; align-items: center; justify-content: center;
  background: var(--bg-soft);
  border: 1px solid var(--border-soft);
  border-radius: var(--radius-sm);
  margin: 12px;
  overflow: hidden;
}
.fv-img-ph {
  width: 60%; max-width: 420px;
  aspect-ratio: 16 / 9;
  display: flex; flex-direction: column;
  align-items: center; justify-content: center; gap: 8px;
  border-radius: var(--radius);
  background: linear-gradient(135deg, var(--primary-soft) 0%, var(--border-soft) 55%, var(--warn-soft) 100%);
  border: 1px dashed var(--border);
  transition: transform .15s ease;
}
.fv-img-ph .ico { font-size: 40px; }
.fv-img-ph .meta {
  font-family: var(--mono);
  font-size: 11px; color: var(--muted);
}
.fv-img-stage.is-fullscreen .fv-img-ph {
  width: 92%; max-width: none;
  border-style: solid;
  border-color: var(--primary);
}

/* ── VIEW BINARY: cảnh báo + hex preview ── */
.fv-bin-warn {
  display: flex; align-items: center; gap: 8px;
  padding: 8px 12px;
  background: var(--warn-soft);
  border-bottom: 1px solid var(--border);
  color: var(--warn);
  font-size: 12px; font-weight: 600;
  flex: none;
}
.fv-hex {
  flex: 1; min-height: 0;
  margin: 12px;
  overflow: auto;
  font-family: var(--mono);
  font-size: 12px; line-height: 1.7;
  background: #0f172a; color: #e2e8f0;
  border-radius: var(--radius-sm);
  padding: 12px 14px;
  white-space: pre;
}
.fv-hex .off { color: #475569; }
.fv-hex .asc { color: #94a3b8; }

/* ── Footer status bar ── */
.fv-status {
  display: flex; align-items: center; gap: 8px;
  font-size: 12px; color: var(--muted);
  flex: 1; min-width: 0;
  overflow: hidden; text-overflow: ellipsis; white-space: nowrap;
}
.fv-status .dot {
  width: 8px; height: 8px; flex: none;
  border-radius: 50%;
  background: var(--ok);
}

/* ── Toast (💾 Lưu / ⬇ Tải xuống) ── */
.fv-toast {
  position: fixed; left: 50%; bottom: 28px;
  transform: translateX(-50%) translateY(10px);
  background: #0f172a; color: #f1f5f9;
  font-size: 13px;
  padding: 9px 16px;
  border-radius: 10px;
  box-shadow: var(--shadow-lg);
  opacity: 0; pointer-events: none;
  transition: opacity .18s ease, transform .18s ease;
  z-index: 90;
}
.fv-toast.show {
  opacity: 1;
  transform: translateX(-50%) translateY(0);
}

/* ─────────── FIX (browser test 1.0.35): #modal-contexts-result height:0 ───────────
   Box wrapper (web/index.html: <div id="modal-contexts-result" hidden>) là div
   thường chứa duy nhất .design-modal--child (position:fixed) → box collapse
   height:0 khi mở (test đo box = 0). Cho box fill viewport khi mở → box luôn có
   kích thước đầy đủ, .design-modal--child (inset:0) fill đúng box, không collapse. */
#modal-contexts-result:not([hidden]) {
  position: fixed;
  z-index: 65;
  inset: 0;
  height: 100%;
  min-height: 0;
}

/* ═══════════════════════════════════════════════════════════
   RESPONSIVE MOBILE (768px) — Fix A: CỘT FILE TREE → DRAWER OVERLAY
   Base .ft-col / .main-grid.ft-shown ở trên (G2-8). Ở mobile cột rời grid,
   thành panel trượt từ phải (tương tự sidebar off-canvas @900px của app.css).
   ═══════════════════════════════════════════════════════════ */
@media (max-width: 768px) {
  /* [topbar v2 §5.4] bỏ rule .main-grid.ft-shown { grid-template-columns: 1fr } củ:
     .ft-col fixed ra khỗi flow — mount height:0, không chiếm grid track → không cần đổi grid. */
  /* [topbar v2 §5.4] flyout pattern đã nâng lên base .ft-col — mobile chỉ override width. */
  .ft-col { width: min(300px, 85vw); }

  /* Modal: gần full-screen trên mobile — ghi đè .design-modal-panel (width 980px)
     + #modal-fileviewer .design-modal-panel (specificity cao hơn) → cần !important. */
  .design-modal-panel,
  #modal-fileviewer .design-modal-panel {
    width: calc(100vw - 16px) !important;
    height: calc(100vh - 16px) !important;
  }
}

/* RESPONSIVE SMALL MOBILE (480px) — phone: file tree drawer rộng hơn + modal fit */
@media (max-width: 480px) {
  .ft-col { width: min(280px, 95vw); }
}

/* ═══════════════════════════════════════════════════════════
   ROLES LIST — row 2 dòng + quick-info card (design_roles_list.md)
   Class scoped (.design-list-row--role, .role-line*, .role-info-*) —
   append-only, KHÔNG sửa rule chung .design-list-row / .design-card.
   ═══════════════════════════════════════════════════════════ */

/* ── Roles list — row 2 dòng (design_roles_list.md §4.2) ── */
.design-list-row--role {
  display: flex; flex-direction: column; align-items: stretch; gap: 3px;
  padding: 8px 12px;
}
.role-line {
  display: flex; align-items: center; gap: 8px; min-width: 0;
}
.role-line--title .role-name {
  flex: 0 1 auto; min-width: 0; font-weight: 600; font-size: 13px;
  overflow: hidden; text-overflow: ellipsis; white-space: nowrap;
}
.role-line--title .role-star { flex: none; color: var(--warn); font-size: 12px; font-weight: 600; }
.role-line--desc .role-desc {
  flex: 1; min-width: 0; font-size: 11px; color: var(--muted);
  overflow: hidden; text-overflow: ellipsis; white-space: nowrap;
}

/* ── Roles quick-info — card scroll nội bộ + actions pinned (design_roles_list.md §4.3) ── */
.role-info-card {
  display: flex; flex-direction: column;
  max-height: 100%;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  box-shadow: var(--shadow);
  padding: 12px 14px;
}
.role-info-body {
  flex: 1; min-height: 0;
  overflow-y: auto;
  overscroll-behavior: contain;
  scrollbar-width: thin;
  scrollbar-color: var(--border) transparent;
}
.role-info-actions {
  flex: none;
  margin-top: 12px;
  padding-top: 10px;
  border-top: 1px solid var(--border-soft);
  display: flex; align-items: center; gap: 8px; flex-wrap: wrap;
}

/* ── Roles list — responsive ≤820px: 1 cột, list fill + card auto (design_roles_list.md §7) ──
   :has(.design-list-row--role) scoped theo class row mới → KHÔNG ảnh hưởng
   modal khác dùng .design-grid-2col (agents, contexts…). */
@media (max-width: 820px) {
  .design-grid-2col:has(.design-list-row--role) {
    grid-template-rows: minmax(0, 1fr) auto;
  }
  .design-grid-2col:has(.design-list-row--role) > .design-scroll {
    max-height: 55%;
  }
}

/* .role-info-actions KHÔNG phải .design-row → .grow không inherit flex:1
   (rule chung chỉ có trong .design-row .grow) — scoped riêng để nút Xóa
   bị đẩy sang phải (design_roles_list.md §4.3). */
.role-info-actions .grow { flex: 1; }
