:root {
  --bg: #f6f7fb;
  --surface: #ffffff;
  --surface-2: #f2f3f8;
  --line: #e6e8ef;
  --text: #111827;
  --muted: #6b7280;
  --faint: #9ca3af;
  --primary: #4f46e5;
  --primary-soft: #eef2ff;
  --ok: #16a34a;
  --ok-soft: #dcfce7;
  --warn: #d97706;
  --warn-soft: #fef3c7;
  --bad: #dc2626;
  --bad-soft: #fee2e2;
  --radius: 12px;
  --shadow: 0 1px 2px rgba(16, 24, 40, .05), 0 1px 3px rgba(16, 24, 40, .06);
  --shadow-lg: 0 12px 32px rgba(16, 24, 40, .14);
  font-family: Inter, ui-sans-serif, system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
  font-size: 14px;
  color: var(--text);
  -webkit-font-smoothing: antialiased;
}
* { box-sizing: border-box; }
html, body { margin: 0; height: 100%; background: var(--bg); }
body { overscroll-behavior: none; }
[hidden] { display: none !important; }
button, input, textarea, select { font: inherit; color: inherit; }
a { color: var(--primary); text-decoration: none; }
.muted { color: var(--muted); }
.small { font-size: 12px; }
svg.i { width: 18px; height: 18px; stroke: currentColor; fill: none; stroke-width: 2; stroke-linecap: round; stroke-linejoin: round; flex: none; }

/* ---------- khung ---------- */
.app { display: grid; grid-template-columns: 248px 1fr; height: 100dvh; }
.sidebar { background: var(--surface); border-right: 1px solid var(--line); display: flex; flex-direction: column; padding: 16px 12px; gap: 16px; }
.brand { display: flex; align-items: center; gap: 10px; padding: 4px 8px; }
.brand img { width: 36px; height: 36px; border-radius: 10px; }
.brand b { display: block; font-size: 15px; }
.brand small { color: var(--muted); font-size: 12px; }
.nav { display: flex; flex-direction: column; gap: 2px; flex: 1; }
.nav button { display: flex; align-items: center; gap: 10px; padding: 9px 10px; border: 0; background: none; border-radius: 8px; cursor: pointer; color: #374151; font-weight: 500; text-align: left; }
.nav button:hover { background: var(--surface-2); }
.nav button.active { background: var(--primary-soft); color: var(--primary); }
.nav .badge { margin-left: auto; }
.me { display: flex; align-items: center; gap: 10px; padding: 10px; border-top: 1px solid var(--line); }
.me .who { flex: 1; min-width: 0; }
.me .who b { display: block; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }

.main { display: flex; flex-direction: column; min-width: 0; min-height: 0; }
.topbar { display: flex; align-items: center; gap: 12px; padding: 14px 24px; padding-top: max(14px, env(safe-area-inset-top)); background: var(--bg); }
.topbar h1 { font-size: 20px; margin: 0; font-weight: 700; letter-spacing: -.01em; }
.topbar-actions { margin-left: auto; display: flex; gap: 8px; }
.conn { width: 9px; height: 9px; border-radius: 50%; background: var(--faint); flex: none; }
.conn.on { background: var(--ok); box-shadow: 0 0 0 3px var(--ok-soft); }
.page { flex: 1; min-height: 0; overflow: auto; padding: 4px 24px 24px; }
.tabbar { display: none; }

/* ---------- thành phần ---------- */
.btn { display: inline-flex; align-items: center; justify-content: center; gap: 6px; padding: 8px 14px; border-radius: 9px; border: 1px solid var(--line); background: var(--surface); cursor: pointer; font-weight: 600; white-space: nowrap; }
.btn:hover { background: var(--surface-2); }
.btn.primary { background: var(--primary); border-color: var(--primary); color: #fff; }
.btn.primary:hover { filter: brightness(1.08); }
.btn.ok { background: var(--ok); border-color: var(--ok); color: #fff; }
.btn.danger { background: var(--surface); border-color: #fecaca; color: var(--bad); }
.btn.danger.solid { background: var(--bad); border-color: var(--bad); color: #fff; }
.btn.sm { padding: 5px 10px; font-size: 13px; border-radius: 8px; }
.btn.block { width: 100%; }
.btn:disabled { opacity: .55; cursor: default; }
.icon-btn { display: inline-grid; place-items: center; width: 34px; height: 34px; border-radius: 9px; border: 1px solid var(--line); background: var(--surface); cursor: pointer; }
.icon-btn:hover { background: var(--surface-2); }
input, textarea, select { width: 100%; padding: 9px 11px; border: 1px solid var(--line); border-radius: 9px; background: var(--surface); font-size: 15px; outline: none; }
input:focus, textarea:focus, select:focus { border-color: var(--primary); box-shadow: 0 0 0 3px var(--primary-soft); }
textarea { resize: vertical; min-height: 80px; }
label { display: grid; gap: 6px; font-weight: 600; font-size: 13px; color: #374151; }
.form { display: grid; gap: 14px; }
.row { display: flex; gap: 8px; align-items: center; flex-wrap: wrap; }
.grow { flex: 1; min-width: 0; }

.card { background: var(--surface); border: 1px solid var(--line); border-radius: var(--radius); box-shadow: var(--shadow); }
.card.pad { padding: 16px; }
.section-title { display: flex; align-items: center; gap: 8px; margin: 22px 0 10px; font-size: 13px; font-weight: 700; color: var(--muted); text-transform: uppercase; letter-spacing: .05em; }
.empty { padding: 28px 16px; text-align: center; color: var(--muted); }
.badge { display: inline-grid; place-items: center; min-width: 20px; height: 20px; padding: 0 6px; border-radius: 10px; background: var(--bad); color: #fff; font-size: 11px; font-weight: 700; }
.chip { display: inline-flex; align-items: center; gap: 4px; padding: 2px 8px; border-radius: 999px; background: var(--surface-2); color: #374151; font-size: 12px; font-weight: 500; white-space: nowrap; }
.chip.primary { background: var(--primary-soft); color: var(--primary); }
.status { display: inline-flex; align-items: center; gap: 5px; padding: 2px 8px; border-radius: 999px; font-size: 12px; font-weight: 600; white-space: nowrap; }
.status::before { content: ""; width: 6px; height: 6px; border-radius: 50%; background: currentColor; }
.st-todo, .st-expired { background: var(--surface-2); color: var(--muted); }
.st-in_progress, .st-pending { background: var(--warn-soft); color: var(--warn); }
.st-done, .st-approved { background: var(--ok-soft); color: var(--ok); }
.st-failed, .st-cancelled, .st-rejected { background: var(--bad-soft); color: var(--bad); }

.avatar { position: relative; display: inline-grid; place-items: center; width: 36px; height: 36px; border-radius: 50%; color: #fff; font-weight: 700; font-size: 13px; flex: none; }
.avatar.sm { width: 26px; height: 26px; font-size: 10px; letter-spacing: -.02em; }
.avatar.lg { width: 52px; height: 52px; font-size: 18px; }
.avatar .dot { position: absolute; right: -1px; bottom: -1px; width: 11px; height: 11px; border-radius: 50%; background: var(--faint); border: 2px solid var(--surface); }
.avatar .dot.on { background: var(--ok); }
.avatar .dot.busy { background: var(--warn); }
.avatar.bot::after { content: ""; position: absolute; inset: -3px; border-radius: 50%; border: 2px solid transparent; }

.progress { height: 7px; border-radius: 99px; background: var(--surface-2); overflow: hidden; }
.progress > div { height: 100%; border-radius: 99px; background: linear-gradient(90deg, var(--primary), #8b5cf6); transition: width .4s ease; }
.progress.done > div { background: var(--ok); }
.progress.failed > div { background: var(--bad); }

/* ---------- tổng quan ---------- */
.stats { display: grid; grid-template-columns: repeat(4, 1fr); gap: 14px; }
.stat { padding: 16px; display: flex; gap: 12px; align-items: center; }
.stat .ico { width: 42px; height: 42px; border-radius: 12px; display: grid; place-items: center; }
.stat b { font-size: 24px; display: block; line-height: 1.1; }
.stat span { color: var(--muted); font-size: 13px; }
.two-col { display: grid; grid-template-columns: 1.4fr 1fr; gap: 18px; align-items: start; }
.list > * + * { border-top: 1px solid var(--line); }
.task-line { display: flex; gap: 12px; padding: 14px 16px; cursor: pointer; align-items: center; }
.task-line:hover { background: #fafbff; }
.task-line .t { font-weight: 600; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.feed-item { display: flex; gap: 10px; padding: 11px 16px; align-items: flex-start; }
.feed-item .txt { flex: 1; min-width: 0; line-height: 1.4; }
.feed-item .txt p { margin: 2px 0 0; color: var(--muted); overflow: hidden; text-overflow: ellipsis; display: -webkit-box; -webkit-line-clamp: 2; line-clamp: 2; -webkit-box-orient: vertical; }

/* ---------- nhân sự ---------- */
.agents { display: grid; grid-template-columns: repeat(auto-fill, minmax(290px, 1fr)); gap: 16px; }
.agent-card { padding: 18px; display: flex; flex-direction: column; gap: 12px; }
.agent-card .head { display: flex; gap: 12px; align-items: center; }
.agent-card .head b { font-size: 15px; display: block; }
.agent-card .desc { color: #374151; line-height: 1.45; margin: 0; min-height: 2.9em; }
.agent-card .meta { display: flex; flex-wrap: wrap; gap: 6px; }
.agent-card .now { background: var(--surface-2); border-radius: 10px; padding: 10px 12px; display: grid; gap: 6px; cursor: pointer; }
.agent-card .actions { display: flex; gap: 8px; margin-top: auto; }
.agent-card .actions .btn { flex: 1; }

/* ---------- trò chuyện ---------- */
.chat { display: grid; grid-template-columns: 260px 1fr; height: 100%; min-height: 0; overflow: hidden; padding: 0; }
.page.chat-page { padding: 0 24px 24px; overflow: hidden; }
.channels { border-right: 1px solid var(--line); overflow: auto; padding: 10px 8px; }
.channels h4 { margin: 10px 8px 6px; font-size: 11px; color: var(--muted); text-transform: uppercase; letter-spacing: .06em; }
.ch { display: flex; align-items: center; gap: 10px; width: 100%; padding: 8px; border: 0; background: none; border-radius: 9px; cursor: pointer; text-align: left; }
.ch:hover { background: var(--surface-2); }
.ch.active { background: var(--primary-soft); }
.ch .hash { width: 26px; height: 26px; border-radius: 8px; display: grid; place-items: center; background: var(--surface-2); color: var(--muted); font-weight: 700; }
.ch .name { flex: 1; min-width: 0; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; font-weight: 500; }
.conv { display: flex; flex-direction: column; min-width: 0; min-height: 0; }
.conv-head { padding: 12px 18px; border-bottom: 1px solid var(--line); display: flex; gap: 10px; align-items: center; }
.conv-head b { font-size: 15px; }
.msgs { flex: 1; overflow: auto; padding: 16px 18px; display: flex; flex-direction: column; gap: 14px; }
.msg { display: flex; gap: 10px; max-width: 760px; }
.msg .body { min-width: 0; }
.msg .meta { font-size: 12px; color: var(--muted); margin-bottom: 3px; }
.msg .meta b { color: var(--text); font-size: 13px; margin-right: 6px; }
.msg .bubble { background: var(--surface-2); border-radius: 4px 14px 14px 14px; padding: 9px 12px; white-space: pre-wrap; word-break: break-word; line-height: 1.45; }
.msg.mine { align-self: flex-end; flex-direction: row-reverse; }
.msg.mine .meta { text-align: right; }
.msg.mine .bubble { background: var(--primary); color: #fff; border-radius: 14px 4px 14px 14px; }
.file { display: inline-flex; align-items: center; gap: 8px; margin-top: 6px; padding: 8px 12px; border: 1px solid var(--line); border-radius: 10px; background: var(--surface); cursor: pointer; color: var(--text); font-weight: 500; }
.task-ref { display: inline-flex; gap: 4px; align-items: center; font-size: 12px; margin-left: 4px; cursor: pointer; }
.composer { display: flex; gap: 8px; padding: 12px 14px; border-top: 1px solid var(--line); align-items: flex-end; }
.composer textarea { min-height: 42px; max-height: 160px; resize: none; }
.attach { cursor: pointer; }

/* ---------- công việc ---------- */
.toolbar { display: flex; gap: 10px; align-items: center; margin-bottom: 14px; flex-wrap: wrap; }
.toolbar select { width: auto; min-width: 180px; }
.seg { display: none; }
.kanban { display: grid; grid-template-columns: repeat(4, minmax(240px, 1fr)); gap: 14px; align-items: start; }
.col { background: #eef0f6; border-radius: var(--radius); padding: 10px; display: flex; flex-direction: column; gap: 10px; min-height: 120px; }
.col-head { display: flex; align-items: center; gap: 8px; padding: 2px 4px; font-weight: 700; }
.col-head .n { color: var(--muted); font-weight: 600; }
.tcard { padding: 12px; display: grid; gap: 9px; cursor: pointer; transition: box-shadow .15s, transform .15s; }
.tcard:hover { box-shadow: var(--shadow-lg); transform: translateY(-1px); }
.tcard .title { font-weight: 600; line-height: 1.35; }
.tcard .note { font-size: 12px; color: var(--muted); overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.tcard .foot { display: flex; align-items: center; gap: 8px; font-size: 12px; color: var(--muted); }

/* ---------- drawer chi tiết task ---------- */
.drawer { position: fixed; inset: 0; z-index: 50; }
.drawer-bg, .modal-bg { position: absolute; inset: 0; background: rgba(17, 24, 39, .35); animation: fade .15s; }
.drawer-panel { position: absolute; right: 0; top: 0; bottom: 0; width: min(560px, 100%); background: var(--surface); box-shadow: var(--shadow-lg); display: flex; flex-direction: column; animation: slide .2s ease; }
.drawer-head { padding: 18px 20px 12px; padding-top: max(18px, env(safe-area-inset-top)); border-bottom: 1px solid var(--line); display: grid; gap: 10px; }
.drawer-head h2 { margin: 0; font-size: 18px; line-height: 1.3; }
.drawer-body { flex: 1; overflow: auto; padding: 16px 20px 24px; }
.drawer-body pre, .pre { white-space: pre-wrap; word-break: break-word; background: var(--surface-2); padding: 12px; border-radius: 10px; margin: 0; font: 13px/1.5 ui-monospace, SFMono-Regular, Menlo, Consolas, monospace; max-height: 340px; overflow: auto; }
.timeline { position: relative; display: grid; gap: 14px; padding-left: 26px; }
.timeline::before { content: ""; position: absolute; left: 9px; top: 6px; bottom: 6px; width: 2px; background: var(--line); }
.tl { position: relative; }
.tl .ic { position: absolute; left: -26px; top: 0; width: 20px; height: 20px; border-radius: 50%; display: grid; place-items: center; background: var(--surface); border: 2px solid var(--line); }
.tl .ic svg { width: 11px; height: 11px; }
.tl.k-progress .ic { border-color: var(--primary); color: var(--primary); }
.tl.k-status .ic { border-color: var(--ok); color: var(--ok); }
.tl.k-comment .ic { border-color: var(--warn); color: var(--warn); }
.tl.k-activity .ic { color: var(--faint); }
.tl .who { font-size: 12px; color: var(--muted); }
.tl .txt { white-space: pre-wrap; word-break: break-word; line-height: 1.4; }
.tl.k-activity .txt { color: var(--muted); font-size: 13px; font-family: ui-monospace, Menlo, Consolas, monospace; }
.tl.k-comment .txt { background: var(--warn-soft); padding: 8px 10px; border-radius: 8px; }
.drawer-foot { border-top: 1px solid var(--line); padding: 12px 16px; padding-bottom: max(12px, env(safe-area-inset-bottom)); display: flex; gap: 8px; }
.drawer-foot textarea { min-height: 42px; resize: none; }

/* ---------- duyệt ---------- */
.approvals { display: grid; gap: 14px; max-width: 760px; }
.approval { padding: 16px; display: grid; gap: 10px; }
.approval.pending { border-color: #fcd34d; box-shadow: 0 0 0 3px var(--warn-soft), var(--shadow); }
.approval .acts { display: grid; grid-template-columns: 1fr 1fr; gap: 10px; }
.approval .acts .btn { padding: 12px; font-size: 15px; }

/* ---------- quản trị ---------- */
.table { width: 100%; border-collapse: collapse; }
.table th { text-align: left; font-size: 12px; color: var(--muted); font-weight: 600; padding: 10px 14px; border-bottom: 1px solid var(--line); }
.table td { padding: 12px 14px; border-bottom: 1px solid var(--line); vertical-align: middle; }
.table tr:last-child td { border-bottom: 0; }

/* ---------- modal, toast, login ---------- */
.modal { position: fixed; inset: 0; z-index: 60; display: grid; place-items: center; padding: 16px; }
.modal-card { position: relative; width: min(520px, 100%); max-height: 90dvh; overflow: auto; background: var(--surface); border-radius: 16px; padding: 22px; box-shadow: var(--shadow-lg); animation: pop .18s ease; }
.modal-card h3 { margin: 0 0 16px; font-size: 18px; }
.modal-actions { display: flex; justify-content: flex-end; gap: 8px; margin-top: 18px; }
.checks { display: grid; gap: 6px; max-height: 50vh; overflow: auto; }
.checks label { display: flex; align-items: center; gap: 10px; font-weight: 500; padding: 6px; border-radius: 8px; }
.checks label:hover { background: var(--surface-2); }
.checks input { width: auto; }
.toasts { position: fixed; right: 16px; bottom: 16px; z-index: 80; display: grid; gap: 8px; }
.toast { background: #111827; color: #fff; padding: 11px 14px; border-radius: 10px; box-shadow: var(--shadow-lg); max-width: 360px; animation: pop .18s ease; cursor: pointer; }
.toast small { display: block; color: #cbd5e1; margin-top: 2px; }
.login { position: fixed; inset: 0; display: grid; place-items: center; padding: 20px; background: radial-gradient(1200px 600px at 10% -10%, #e0e7ff, transparent), radial-gradient(900px 500px at 110% 110%, #ede9fe, transparent), var(--bg); }
.login-card { width: min(380px, 100%); background: var(--surface); border-radius: 18px; padding: 28px; box-shadow: var(--shadow-lg); display: grid; gap: 14px; }
.login-card h2 { margin: 0; font-size: 22px; }
.login-card p { margin: -6px 0 6px; }
.login-logo { width: 54px; height: 54px; border-radius: 14px; }
.err { color: var(--bad); min-height: 1.2em; font-size: 13px; }

@keyframes fade { from { opacity: 0; } }
@keyframes slide { from { transform: translateX(30px); opacity: 0; } }
@keyframes pop { from { transform: scale(.97); opacity: 0; } }

/* ---------- điện thoại ---------- */
@media (max-width: 1100px) {
  .stats { grid-template-columns: repeat(2, 1fr); }
  .two-col { grid-template-columns: 1fr; }
}
@media (max-width: 860px) {
  .app { grid-template-columns: 1fr; grid-template-rows: 1fr auto; }
  .sidebar { display: none; }
  .topbar { padding: 12px 16px; padding-top: max(12px, env(safe-area-inset-top)); background: var(--surface); border-bottom: 1px solid var(--line); }
  .topbar h1 { font-size: 17px; }
  .page { padding: 14px 14px 20px; }
  .page.chat-page { padding: 0; }
  .tabbar { display: flex; background: var(--surface); border-top: 1px solid var(--line); padding-bottom: env(safe-area-inset-bottom); }
  .tabbar button { position: relative; flex: 1; display: grid; justify-items: center; gap: 3px; padding: 8px 2px 6px; border: 0; background: none; color: var(--faint); font-size: 11px; font-weight: 600; cursor: pointer; }
  .tabbar button.active { color: var(--primary); }
  .tabbar svg.i { width: 22px; height: 22px; }
  .tabbar .badge { position: absolute; top: 3px; left: calc(50% + 6px); height: 17px; min-width: 17px; font-size: 10px; }
  .stats { gap: 10px; }
  .stat { padding: 12px; }
  .stat b { font-size: 20px; }
  .chat { grid-template-columns: 1fr; border: 0; border-radius: 0; box-shadow: none; }
  .chat.in-conv .channels { display: none; }
  .chat:not(.in-conv) .conv { display: none; }
  .channels { border-right: 0; }
  .ch { padding: 12px 10px; }
  .msg { max-width: 92%; }
  .kanban { grid-template-columns: 1fr; }
  .kanban .col { display: none; background: none; padding: 0; }
  .kanban .col.show { display: flex; }
  .kanban .col-head { display: none; }
  .seg { display: flex; background: #e9ebf2; border-radius: 10px; padding: 3px; width: 100%; }
  .seg button { flex: 1; border: 0; background: none; padding: 7px 4px; border-radius: 8px; font-weight: 600; font-size: 12px; color: var(--muted); cursor: pointer; }
  .seg button.active { background: var(--surface); color: var(--text); box-shadow: var(--shadow); }
  .toolbar select { flex: 1; min-width: 0; }
  .table thead { display: none; }
  .table, .table tbody, .table tr, .table td { display: block; width: 100%; }
  .table tr { border-bottom: 1px solid var(--line); padding: 8px 0; }
  .table td { border: 0; padding: 4px 14px; }
  .toasts { left: 12px; right: 12px; bottom: calc(70px + env(safe-area-inset-bottom)); }
  .toast { max-width: none; }
  .modal { place-items: end center; padding: 0; }
  .modal-card { width: 100%; border-radius: 18px 18px 0 0; padding-bottom: max(22px, env(safe-area-inset-bottom)); }
}
