/* =========================================================================
   admin.css — Dashboard quản trị
   ========================================================================= */
body { overflow: auto; }
.admin { min-height: 100vh; }

.admin-header {
  position: sticky; top: 0; z-index: 10;
  display: flex; align-items: center; justify-content: space-between;
  padding: 14px 28px; background: var(--bg-elev);
  border-bottom: 1px solid var(--border);
}
.admin-header-actions { display: flex; align-items: center; gap: 16px; }
.refresh-note { font-size: 12.5px; color: var(--text-faint); }

.admin-body { max-width: 1120px; margin: 0 auto; padding: 26px 28px 60px; display: flex; flex-direction: column; gap: 22px; }

/* Thẻ số liệu */
.stat-grid { display: grid; grid-template-columns: repeat(4, 1fr); gap: 14px; }
.stat-card {
  background: var(--bg-elev); border: 1px solid var(--border);
  border-radius: var(--radius); padding: 18px 20px;
}
.stat-card .label { font-size: 12.5px; color: var(--text-muted); text-transform: uppercase; letter-spacing: 0.5px; display: flex; align-items: center; gap: 7px; }
.stat-card .label svg { width: 15px; height: 15px; color: var(--accent); }
.stat-card .value { font-size: 30px; font-weight: 700; letter-spacing: -0.5px; margin-top: 8px; }
.stat-card .sub { font-size: 12.5px; color: var(--text-faint); margin-top: 2px; }

/* Panel */
.panel { background: var(--bg-elev); border: 1px solid var(--border); border-radius: var(--radius); padding: 20px 22px; }
.panel h2 { font-size: 15px; font-weight: 650; margin-bottom: 16px; }
.panel-head { display: flex; align-items: center; justify-content: space-between; margin-bottom: 16px; }
.panel-head h2 { margin: 0; }

.two-col { display: grid; grid-template-columns: 1fr 1fr; gap: 22px; }

/* Thanh tài nguyên */
.resource-grid { display: grid; grid-template-columns: repeat(2, 1fr); gap: 20px; }
.resource { }
.resource .top { display: flex; justify-content: space-between; margin-bottom: 8px; font-size: 13.5px; }
.resource .top .name { font-weight: 550; }
.resource .top .val { color: var(--text-muted); font-variant-numeric: tabular-nums; }
.bar { height: 8px; border-radius: 6px; background: var(--surface); overflow: hidden; }
.bar > span { display: block; height: 100%; border-radius: 6px; background: var(--accent); transition: width 0.6s var(--ease); }
.bar > span.warn { background: var(--warning); }
.bar > span.crit { background: var(--danger); }
.resource .detail { font-size: 12px; color: var(--text-faint); margin-top: 6px; }

/* Chips model */
.chips { display: flex; flex-wrap: wrap; gap: 8px; }
.chip {
  display: inline-flex; align-items: center; gap: 7px;
  padding: 7px 13px; border-radius: 999px;
  background: var(--accent-soft); color: var(--text); font-size: 13.5px; font-weight: 500;
}
.chip .dot { width: 7px; height: 7px; border-radius: 50%; background: var(--success); }
.chips .none { color: var(--text-faint); font-size: 13.5px; }

/* Bảng nhỏ (users) */
.mini-table { display: flex; flex-direction: column; gap: 2px; }
.mini-row { display: flex; align-items: center; gap: 10px; padding: 8px 6px; border-radius: 8px; font-size: 13.5px; }
.mini-row:hover { background: var(--surface-hover); }
.mini-row .avatar { width: 26px; height: 26px; font-size: 11px; }
.mini-row .u-name { flex: 1; font-weight: 500; }
.mini-row .badge { font-size: 11px; padding: 2px 8px; border-radius: 999px; background: var(--accent-soft); color: var(--accent); font-weight: 600; }
.mini-row .u-date { color: var(--text-faint); font-size: 12px; }

/* Nhật ký */
.log-filters { display: flex; gap: 6px; }
.chip-btn { padding: 5px 12px; border-radius: 999px; font-size: 12.5px; color: var(--text-muted); border: 1px solid var(--border); transition: all 0.13s; }
.chip-btn:hover { background: var(--surface-hover); }
.chip-btn.active { background: var(--accent); color: #fff; border-color: var(--accent); }

.logs { display: flex; flex-direction: column; gap: 2px; max-height: 420px; overflow-y: auto; font-family: var(--mono); font-size: 12.5px; }
.log-row { display: flex; gap: 12px; padding: 7px 10px; border-radius: 7px; align-items: baseline; }
.log-row:hover { background: var(--surface-hover); }
.log-row .time { color: var(--text-faint); white-space: nowrap; }
.log-row .lvl { font-weight: 700; width: 62px; flex-shrink: 0; }
.log-row .lvl.INFO { color: var(--text-muted); }
.log-row .lvl.WARNING { color: var(--warning); }
.log-row .lvl.ERROR { color: var(--danger); }
.log-row .msg { flex: 1; color: var(--text); word-break: break-word; }
.log-row .rt { color: var(--accent); white-space: nowrap; }
.logs .empty { color: var(--text-faint); padding: 16px 10px; font-family: var(--font); }

@media (max-width: 880px) {
  .stat-grid { grid-template-columns: repeat(2, 1fr); }
  .two-col, .resource-grid { grid-template-columns: 1fr; }
  .admin-body { padding: 20px 16px 50px; }
  .admin-header { padding: 12px 16px; }
  .refresh-note { display: none; }
}

/* ===================== Quản lý Model ===================== */
.model-meta { display: flex; flex-wrap: wrap; gap: 8px 18px; margin-bottom: 14px; font-size: 13px; color: var(--text-muted); align-items: center; }
.model-meta code { background: var(--surface); border: 1px solid var(--border); padding: 2px 7px; border-radius: 6px; font-size: 12px; color: var(--text); }
.model-meta .none { color: var(--text-faint); }

.pull-row { display: flex; gap: 10px; margin-bottom: 10px; }
.pull-row input {
  flex: 1; padding: 10px 13px; border-radius: var(--radius-sm); border: 1px solid var(--border);
  background: var(--surface); color: var(--text); font-size: 13.5px; outline: none;
}
.pull-row input:focus { border-color: var(--accent); box-shadow: 0 0 0 3px var(--accent-soft); }
.primary-btn {
  display: inline-flex; align-items: center; gap: 7px; padding: 10px 16px; border-radius: var(--radius-sm);
  background: var(--accent); color: #fff; font-size: 13.5px; font-weight: 600; white-space: nowrap; transition: background 0.13s;
}
.primary-btn:hover { background: var(--accent-hover, var(--accent)); filter: brightness(1.05); }
.primary-btn:disabled { opacity: 0.55; cursor: not-allowed; }
.primary-btn svg { width: 16px; height: 16px; }

.pull-progress { display: none; margin-bottom: 14px; }
.pull-progress.show { display: block; }
.pp-status { font-size: 12.5px; color: var(--text-muted); margin-bottom: 6px; }
.pp-bar { height: 8px; background: var(--surface); border: 1px solid var(--border); border-radius: 999px; overflow: hidden; }
.pp-bar span { display: block; height: 100%; width: 0; background: var(--accent); border-radius: 999px; transition: width 0.25s ease; }
.pp-bar span.err { background: var(--danger); }

.model-table { display: flex; flex-direction: column; gap: 2px; }
.mt-head, .mt-row {
  display: grid; grid-template-columns: 2fr 1fr 1fr 1.1fr auto; gap: 10px; align-items: center;
  padding: 9px 12px;
}
.mt-head { font-size: 11.5px; text-transform: uppercase; letter-spacing: 0.04em; color: var(--text-faint); }
.mt-row { background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius-sm); font-size: 13px; }
.mt-row:hover { border-color: var(--border-strong); }
.mt-name { font-weight: 600; color: var(--text); word-break: break-all; }
.mt-row .badge { font-size: 11px; padding: 2px 9px; border-radius: 999px; font-weight: 600; }
.mt-row .badge.on { background: rgba(34,197,94,0.15); color: var(--success, #22c55e); }
.mt-row .badge.off { background: var(--surface-hover); color: var(--text-faint); }
.mt-actions { display: flex; gap: 6px; justify-content: flex-end; }
.mini { padding: 5px 11px; border-radius: 7px; font-size: 12px; font-weight: 500; border: 1px solid var(--border); color: var(--text-muted); transition: all 0.12s; background: var(--surface); }
.mini:hover { background: var(--surface-hover); color: var(--text); border-color: var(--border-strong); }
.mini.outline { color: var(--accent); border-color: var(--accent-soft); }
.mini.danger { color: var(--danger); border-color: transparent; }
.mini.danger:hover { background: rgba(239,68,68,0.12); border-color: var(--danger); }
.model-table .empty { color: var(--text-faint); padding: 16px 10px; font-size: 13.5px; }

@media (max-width: 680px) {
  .mt-head { display: none; }
  .mt-row { grid-template-columns: 1fr auto; grid-template-areas: "name status" "size actions" "date actions"; gap: 4px 10px; }
  .mt-name { grid-area: name; }
  .mt-row > span:nth-child(2) { grid-area: size; color: var(--text-muted); }
  .mt-row > span:nth-child(3) { grid-area: date; color: var(--text-faint); font-size: 12px; }
  .mt-row > span:nth-child(4) { grid-area: status; justify-self: end; }
  .mt-actions { grid-area: actions; }
  .pull-row { flex-direction: column; }
}

/* ===================== Cụm node ===================== */
.node-grid { display: grid; grid-template-columns: repeat(auto-fill, minmax(240px, 1fr)); gap: 12px; }
.node-card { background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius-sm); padding: 13px 15px; }
.node-card.off { opacity: 0.72; }
.node-card .dot { width: 9px; height: 9px; border-radius: 50%; display: inline-block; flex-shrink: 0; }
.node-card .dot.on, .ng-name .dot.on { background: var(--success, #22c55e); box-shadow: 0 0 0 3px rgba(34,197,94,0.15); }
.node-card .dot.off, .ng-name .dot.off { background: var(--text-faint); }
.nc-head { display: flex; align-items: center; gap: 8px; margin-bottom: 6px; }
.nc-name { font-weight: 600; color: var(--text); }
.nc-loc { margin-left: auto; font-size: 11px; padding: 2px 8px; border-radius: 999px; background: var(--surface-hover); color: var(--text-faint); }
.nc-url { font-size: 12px; color: var(--text-muted); font-family: var(--font-mono, monospace); word-break: break-all; margin-bottom: 8px; }
.nc-stats { display: flex; gap: 12px; font-size: 12.5px; color: var(--text-muted); }
.nc-stats .on { color: var(--success, #22c55e); font-weight: 600; }
.nc-stats .off { color: var(--text-faint); }
.nc-tags { display: flex; flex-wrap: wrap; gap: 5px; margin-top: 9px; }
.ntag { font-size: 11px; padding: 2px 9px; border-radius: 999px; background: var(--accent-soft); color: var(--accent); font-weight: 600; }
.nc-err { margin-top: 8px; font-size: 11.5px; color: var(--danger); }

/* Chọn node đích khi tải model */
.node-select { padding: 10px 12px; border-radius: var(--radius-sm); border: 1px solid var(--border); background: var(--surface); color: var(--text); font-size: 13px; max-width: 180px; }
.node-select:focus { border-color: var(--accent); outline: none; }

/* Nhóm model theo node */
.node-group { margin-bottom: 16px; }
.node-group:last-child { margin-bottom: 0; }
.node-group-head { display: flex; align-items: center; justify-content: space-between; gap: 10px; flex-wrap: wrap; padding: 6px 2px 10px; }
.ng-name { display: flex; align-items: center; gap: 8px; font-weight: 600; color: var(--text); }
.ng-meta { font-size: 12px; color: var(--text-faint); }
.ng-meta code { background: var(--surface); border: 1px solid var(--border); padding: 1px 6px; border-radius: 5px; color: var(--text-muted); }

/* ===================== Cơ sở tri thức (KB / RAG) ===================== */
.kb-meta { display: flex; flex-wrap: wrap; gap: 8px 18px; margin-bottom: 14px; font-size: 13px; color: var(--text-muted); align-items: center; }
.kb-meta code { background: var(--surface); border: 1px solid var(--border); padding: 2px 7px; border-radius: 6px; font-size: 12px; color: var(--text); }

.kb-add { display: flex; flex-direction: column; gap: 8px; margin-bottom: 16px; }
.kb-add input[type="text"] {
  padding: 10px 13px; border-radius: var(--radius-sm); border: 1px solid var(--border);
  background: var(--surface); color: var(--text); font-size: 13.5px; outline: none;
}
.kb-add textarea {
  min-height: 90px; resize: vertical; padding: 11px 13px; border-radius: var(--radius-sm);
  border: 1px solid var(--border); background: var(--surface); color: var(--text);
  font-size: 13.5px; font-family: inherit; line-height: 1.55; outline: none;
}
.kb-add input:focus, .kb-add textarea:focus { border-color: var(--accent); box-shadow: 0 0 0 3px var(--accent-soft); }
.kb-add-actions { display: flex; align-items: center; gap: 10px; flex-wrap: wrap; }
.kb-status { font-size: 12.5px; color: var(--text-muted); }

.kb-table { display: flex; flex-direction: column; gap: 2px; }
.kbt-head, .kbt-row {
  display: grid; grid-template-columns: 2fr 1.4fr 0.6fr 0.7fr 1fr auto; gap: 10px;
  align-items: center; padding: 9px 12px;
}
.kbt-head { font-size: 11.5px; text-transform: uppercase; letter-spacing: 0.04em; color: var(--text-faint); }
.kbt-row { background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius-sm); font-size: 13px; }
.kbt-row:hover { border-color: var(--border-strong); }
.kbt-title { font-weight: 600; color: var(--text); word-break: break-word; }
.kbt-src { color: var(--text-muted); font-size: 12px; word-break: break-all; }
.kbt-row .badge { font-size: 11px; padding: 2px 9px; border-radius: 999px; font-weight: 600; }
.kbt-row .badge.on { background: rgba(34,197,94,0.15); color: var(--success, #22c55e); }
.kbt-row .badge.idx { background: rgba(123,108,246,0.15); color: var(--accent); }
.kbt-row .badge.err { background: rgba(239,68,68,0.15); color: var(--danger); }
.kbt-actions { display: flex; gap: 6px; justify-content: flex-end; }
.kb-table .empty { color: var(--text-faint); padding: 16px 10px; font-size: 13.5px; }

@media (max-width: 720px) {
  .kbt-head { display: none; }
  .kbt-row { grid-template-columns: 1fr auto; grid-template-areas: "title status" "src actions" "meta actions"; gap: 4px 10px; }
  .kbt-title { grid-area: title; }
  .kbt-src { grid-area: src; }
  .kbt-row > span:nth-child(3), .kbt-row > span:nth-child(4) { grid-area: meta; display: inline; color: var(--text-faint); font-size: 12px; }
  .kbt-row > span:nth-child(5) { grid-area: status; justify-self: end; }
  .kbt-actions { grid-area: actions; }
}

/* Tri thức dạng ảnh: thumbnail trong bảng KB */
.kbt-title { display: flex; align-items: center; gap: 9px; }
.kbt-thumb {
  width: 34px; height: 34px; object-fit: cover; border-radius: 6px;
  border: 1px solid var(--border); flex-shrink: 0; opacity: 0; transition: opacity 0.2s;
}
.kbt-thumb.loaded { opacity: 1; }
.kbt-kind { font-size: 11px; color: var(--text-faint); font-weight: 500; }

/* Memory Statistics */
.sub-h { font-size: 12px; text-transform: uppercase; letter-spacing: 0.05em; color: var(--text-faint); margin-bottom: 10px; font-weight: 600; }
.mem-mini { display: flex; flex-direction: column; gap: 5px; }
.mem-mini-row {
  display: grid; grid-template-columns: 88px 1fr auto; gap: 9px; align-items: center;
  padding: 7px 10px; background: var(--surface); border: 1px solid var(--border);
  border-radius: 8px; font-size: 12.5px;
}
.mem-mini-val { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; color: var(--text-muted); }
.mem-mini-val b { color: var(--text); }
.mem-mini-x { color: var(--text-faint); font-size: 11.5px; white-space: nowrap; }
.mem-mini .empty { color: var(--text-faint); font-size: 12.5px; padding: 10px 2px; }

/* ===================== Cấu hình hệ thống ===================== */
.panel-head-actions { display: flex; align-items: center; gap: 10px; }
.cfg-status { font-size: 12.5px; color: var(--text-muted); }
.cfg-status.ok { color: var(--success, #22c55e); }
.cfg-status.err { color: var(--danger); }
.cfg-hint { font-size: 12.5px; color: var(--text-faint); margin-bottom: 16px; line-height: 1.6; }
.cfg-hint code { background: var(--surface); border: 1px solid var(--border); padding: 1px 6px; border-radius: 5px; }

.dot-ovr {
  display: inline-block; width: 7px; height: 7px; border-radius: 50%;
  background: var(--accent); vertical-align: middle; margin-left: 4px;
}

.cfg-groups { display: flex; flex-direction: column; gap: 20px; }
.cfg-group { border-top: 1px solid var(--border); padding-top: 14px; }
.cfg-group:first-child { border-top: none; padding-top: 0; }

.cfg-row {
  display: grid; grid-template-columns: 1fr 220px; gap: 14px; align-items: start;
  padding: 10px 0; border-bottom: 1px dashed var(--border);
}
.cfg-row:last-child { border-bottom: none; }
.cfg-row.wide { grid-template-columns: 1fr; }
.cfg-label { display: flex; flex-direction: column; gap: 3px; min-width: 0; }
.cfg-label > span { font-size: 13.5px; font-weight: 600; color: var(--text); }
.cfg-label code { font-size: 11px; color: var(--text-faint); font-family: var(--mono); }
.cfg-help { font-size: 12px; color: var(--text-muted); line-height: 1.5; margin-top: 2px; }
.cfg-warn { font-size: 12px; color: var(--warning, #f59e0b); line-height: 1.5; margin-top: 2px; }

.cfg-input { display: flex; align-items: center; gap: 6px; justify-content: flex-end; }
.cfg-row.wide .cfg-input { justify-content: flex-start; }
.cfg-num, .cfg-str, .cfg-text {
  width: 100%; padding: 8px 11px; border-radius: 8px;
  border: 1px solid var(--border); background: var(--surface); color: var(--text);
  font-size: 13px; font-family: inherit; outline: none;
}
.cfg-text { resize: vertical; line-height: 1.55; font-size: 12.5px; }
.cfg-num:focus, .cfg-str:focus, .cfg-text:focus {
  border-color: var(--accent); box-shadow: 0 0 0 3px var(--accent-soft);
}
.cfg-input .switch { justify-content: flex-end; }
.cfg-input .switch input { width: 17px; height: 17px; accent-color: var(--accent); cursor: pointer; }

@media (max-width: 720px) {
  .cfg-row { grid-template-columns: 1fr; }
  .cfg-input { justify-content: flex-start; }
}

.switch { display: flex; align-items: center; gap: 8px; font-size: 13.5px; cursor: pointer; }
.switch input { width: 16px; height: 16px; accent-color: var(--accent); cursor: pointer; }

/* Provider (Ollama / LM Studio) */
.nc-prov {
  font-size: 10px; font-weight: 700; letter-spacing: 0.03em;
  padding: 2px 7px; border-radius: 999px; text-transform: uppercase;
}
.nc-prov.oll { background: rgba(123, 108, 246, 0.16); color: var(--accent); }
.nc-prov.lms { background: rgba(34, 197, 94, 0.16); color: var(--success, #22c55e); }
.node-note {
  font-size: 12.5px; color: var(--text-muted); line-height: 1.6;
  background: var(--surface); border: 1px solid var(--border);
  border-left: 3px solid var(--success, #22c55e);
  border-radius: 8px; padding: 9px 12px; margin-bottom: 10px;
}
.node-note code { background: var(--bg-elev); padding: 1px 6px; border-radius: 5px; font-size: 11.5px; }

/* ===================== Modal (quyền model theo tài khoản) =====================
   admin.css KHÔNG có sẵn các class modal (chúng nằm trong style.css của trang chat),
   nên phải định nghĩa lại ở đây — nếu không modal sẽ mất hoàn toàn bố cục. */
.modal {
  position: fixed; inset: 0; z-index: 200; display: none;
  background: rgba(0, 0, 0, 0.55); backdrop-filter: blur(3px);
  align-items: center; justify-content: center; padding: 20px;
}
.modal.open { display: flex; }
.modal-box {
  background: var(--bg-elev); border: 1px solid var(--border);
  border-radius: 16px; width: min(620px, 100%); max-height: 86vh;
  overflow-y: auto; padding: 20px 22px; box-shadow: 0 24px 60px rgba(0, 0, 0, 0.45);
}
.modal-head { display: flex; align-items: center; justify-content: space-between; margin-bottom: 14px; }
.modal-head h3 { font-size: 16.5px; font-weight: 650; }
.modal-close {
  width: 30px; height: 30px; border-radius: 8px; color: var(--text-muted);
  font-size: 15px; cursor: pointer; background: none; border: none;
  transition: background 0.12s, color 0.12s;
}
.modal-close:hover { background: var(--surface-hover); color: var(--text); }
.modal-bar { display: flex; align-items: center; gap: 8px; flex-wrap: wrap; }

/* Danh sách model để tick chọn */
.perm-list { display: flex; flex-direction: column; gap: 5px; max-height: 45vh; overflow-y: auto; }
.perm-row {
  display: grid; grid-template-columns: auto 1fr auto; gap: 10px; align-items: center;
  padding: 9px 12px; background: var(--surface); border: 1px solid var(--border);
  border-radius: 9px; cursor: pointer; transition: border-color 0.12s, background 0.12s;
}
.perm-row:hover { border-color: var(--border-strong); }
.perm-row:has(input:checked) { border-color: var(--accent); background: var(--accent-soft); }
.perm-row:has(input:disabled) { opacity: 0.5; cursor: not-allowed; }
.perm-row input { width: 16px; height: 16px; accent-color: var(--accent); cursor: pointer; }
.perm-name { font-size: 13.5px; font-weight: 500; color: var(--text); font-family: var(--mono); }
.perm-node { font-size: 11.5px; color: var(--text-faint); }
.perm-list .empty { color: var(--text-faint); font-size: 13px; padding: 16px 4px; }

/* Nút "Model" trong dòng người dùng */
.mini-row .mini { margin-left: auto; }

/* Tài khoản chờ phê duyệt */
.pending-badge {
  display: inline-block; min-width: 20px; padding: 1px 7px; margin-left: 6px;
  font-size: 11.5px; font-weight: 700; text-align: center;
  background: var(--danger); color: #fff; border-radius: 999px;
}
#pending-list .mem-row { grid-template-columns: auto 1fr auto; }
.pending-name { font-size: 14px; font-weight: 600; color: var(--text); }
#pending-list .avatar {
  width: 34px; height: 34px; border-radius: 50%;
  display: flex; align-items: center; justify-content: center;
  background: var(--accent-soft); color: var(--accent); font-weight: 700; font-size: 14px;
}
.mini-primary { padding: 5px 12px; font-size: 12px; }
