:root {
  --bg: #f4f5f7; --panel: #fff; --line: #e5e7eb; --text: #111827; --muted: #6b7280;
  --brand: #c8102e; --brand-soft: #fde8eb; --blue: #2563eb; --green: #16a34a; --amber: #d97706;
  --radius: 10px; font-size: 14px;
}
* { box-sizing: border-box; }
html, body { margin: 0; height: 100%; font-family: -apple-system, "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif; color: var(--text); background: var(--bg); }
button, input, select, textarea { font: inherit; }
.hidden { display: none !important; }
.muted { color: var(--muted); }
.err { color: var(--brand); min-height: 1.2em; }
.row { display: flex; align-items: center; }
.gap { gap: 8px; } .wrap { flex-wrap: wrap; }
input, select, textarea { border: 1px solid var(--line); border-radius: 8px; padding: 7px 10px; background: #fff; outline: none; }
input:focus, select:focus, textarea:focus { border-color: var(--brand); }
textarea { width: 100%; resize: vertical; }
textarea.code { font-family: ui-monospace, Menlo, Consolas, monospace; font-size: 12px; }
.btn { border: 1px solid var(--line); background: #fff; border-radius: 8px; padding: 7px 14px; cursor: pointer; }
.btn:hover { background: #f9fafb; }
.btn.primary { background: var(--brand); border-color: var(--brand); color: #fff; }
.btn.primary:hover { filter: brightness(.95); }
.btn.sm { padding: 4px 10px; font-size: 12px; }
.check { display: flex; align-items: center; gap: 6px; }
label { display: block; margin: 8px 0; }
label > select, label > textarea { margin-top: 4px; width: 100%; }

/* Login */
.login { height: 100%; display: grid; place-items: center; background: linear-gradient(135deg, #fff 0%, var(--brand-soft) 100%); }
.login-card { background: #fff; padding: 32px; border-radius: 16px; width: 340px; box-shadow: 0 10px 40px rgba(0,0,0,.08); display: grid; gap: 12px; }
.brand-big { font-size: 28px; font-weight: 800; color: var(--brand); }
.brand-big span { color: var(--text); font-weight: 400; margin-left: 4px; }

/* Layout */
.app { display: flex; height: 100vh; }
.rail { width: 76px; background: #1f2937; display: flex; flex-direction: column; align-items: center; padding: 12px 0; gap: 4px; }
.brand { color: #fff; background: var(--brand); font-weight: 800; border-radius: 10px; padding: 8px 6px; margin-bottom: 12px; font-size: 13px; }
.rail-btn { width: 64px; border: 0; background: transparent; color: #d1d5db; border-radius: 10px; padding: 8px 0; cursor: pointer; font-size: 18px; display: flex; flex-direction: column; align-items: center; gap: 2px; }
.rail-btn span { font-size: 10px; }
.rail-btn.active, .rail-btn:hover { background: #374151; color: #fff; }
.rail-spacer { flex: 1; }
.me { color: #9ca3af; font-size: 10px; text-align: center; padding: 0 4px; word-break: break-word; }
.view { flex: 1; min-width: 0; }

/* Inbox */
.inbox { display: flex; height: 100vh; }
.convs { width: 340px; background: var(--panel); border-right: 1px solid var(--line); display: flex; flex-direction: column; }
.convs-head { padding: 12px; border-bottom: 1px solid var(--line); display: grid; gap: 8px; }
.convs-head input, .convs-head select { width: 100%; }
.seg { display: flex; background: var(--bg); border-radius: 8px; padding: 3px; }
.seg button { flex: 1; border: 0; background: transparent; padding: 5px; border-radius: 6px; cursor: pointer; color: var(--muted); }
.seg button.active { background: #fff; color: var(--text); font-weight: 600; box-shadow: 0 1px 2px rgba(0,0,0,.08); }
.chips { display: flex; flex-wrap: wrap; gap: 4px; }
.chips button { border: 1px solid var(--line); background: #fff; border-radius: 999px; padding: 3px 10px; font-size: 12px; cursor: pointer; }
.chips button.active { background: var(--brand); border-color: var(--brand); color: #fff; }
.conv-list { overflow-y: auto; flex: 1; }
.conv { display: flex; gap: 10px; padding: 10px 12px; border-bottom: 1px solid #f3f4f6; cursor: pointer; position: relative; }
.conv:hover { background: #f9fafb; }
.conv.active { background: var(--brand-soft); }
.avatar { width: 40px; height: 40px; border-radius: 50%; background: #e5e7eb; flex: none; display: grid; place-items: center; font-weight: 700; color: #6b7280; overflow: hidden; }
.avatar img { width: 100%; height: 100%; object-fit: cover; }
.conv-body { flex: 1; min-width: 0; }
.conv-top { display: flex; justify-content: space-between; gap: 6px; }
.conv-name { font-weight: 600; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.conv.unread .conv-name, .conv.unread .conv-last { font-weight: 700; color: var(--text); }
.conv-time { font-size: 11px; color: var(--muted); flex: none; }
.conv-last { color: var(--muted); font-size: 13px; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.conv-meta { display: flex; gap: 4px; flex-wrap: wrap; margin-top: 4px; }
.badge { font-size: 10px; padding: 1px 6px; border-radius: 999px; background: #f3f4f6; color: #374151; white-space: nowrap; }
.badge.red { background: var(--brand); color: #fff; }
.badge.green { background: #dcfce7; color: #166534; }
.badge.blue { background: #dbeafe; color: #1e40af; }
.badge.amber { background: #fef3c7; color: #92400e; }
.dot { position: absolute; right: 12px; bottom: 12px; min-width: 18px; height: 18px; border-radius: 9px; background: var(--brand); color: #fff; font-size: 11px; display: grid; place-items: center; padding: 0 5px; }

.chat { flex: 1; display: flex; flex-direction: column; background: var(--bg); min-width: 0; }
.empty { margin: auto; color: var(--muted); }
.chat-head { background: var(--panel); border-bottom: 1px solid var(--line); padding: 10px 16px; display: flex; align-items: center; gap: 10px; }
.chat-head .title { font-weight: 700; }
.chat-head .sub { font-size: 12px; color: var(--muted); }
.chat-head .actions { margin-left: auto; display: flex; gap: 6px; }
.msgs { flex: 1; overflow-y: auto; padding: 16px; display: flex; flex-direction: column; gap: 6px; }
.msg { max-width: 66%; padding: 8px 12px; border-radius: 14px; white-space: pre-wrap; word-wrap: break-word; position: relative; }
.msg.in { background: #fff; align-self: flex-start; border-bottom-left-radius: 4px; }
.msg.out { background: var(--blue); color: #fff; align-self: flex-end; border-bottom-right-radius: 4px; }
.msg.out.bot { background: #4b5563; }
.msg.out.page { background: #7c3aed; }
.msg.hidden-cmt { opacity: .55; border: 1px dashed var(--muted); }
.msg-meta { font-size: 10px; opacity: .75; margin-top: 3px; display: flex; gap: 8px; align-items: center; }
.msg-meta button { border: 0; background: transparent; color: inherit; text-decoration: underline; cursor: pointer; font-size: 10px; padding: 0; }
.msg img { max-width: 220px; border-radius: 8px; display: block; }
.day-sep { align-self: center; font-size: 11px; color: var(--muted); margin: 8px 0; }
.composer { background: var(--panel); border-top: 1px solid var(--line); padding: 10px 12px; }
.composer .row { gap: 8px; align-items: flex-end; }
.composer textarea { flex: 1; min-height: 42px; max-height: 160px; }
.quick { display: flex; gap: 4px; flex-wrap: wrap; margin-bottom: 6px; }
.quick button { border: 1px solid var(--line); background: #fff; border-radius: 999px; font-size: 12px; padding: 2px 10px; cursor: pointer; }
.window-warn { font-size: 12px; color: var(--amber); margin-bottom: 6px; }

.side { width: 300px; background: var(--panel); border-left: 1px solid var(--line); overflow-y: auto; padding: 16px; }
.side h4 { margin: 16px 0 6px; font-size: 12px; text-transform: uppercase; letter-spacing: .04em; color: var(--muted); }
.side input, .side select, .side textarea { width: 100%; }
.side .field { margin-bottom: 8px; }
.side .field small { color: var(--muted); display: block; margin-bottom: 2px; }
.tags { display: flex; flex-wrap: wrap; gap: 4px; }
.tag { font-size: 12px; padding: 2px 8px; border-radius: 999px; color: #fff; display: inline-flex; align-items: center; gap: 4px; }
.tag button { border: 0; background: transparent; color: #fff; cursor: pointer; padding: 0; font-size: 12px; }

/* Pages */
.page { padding: 20px 24px; overflow-y: auto; height: 100vh; }
.page-head { display: flex; justify-content: space-between; align-items: center; gap: 12px; flex-wrap: wrap; margin-bottom: 16px; }
.page-head h2 { margin: 0; }
.card { background: var(--panel); border: 1px solid var(--line); border-radius: var(--radius); padding: 16px; margin-bottom: 16px; }
.card h3 { margin-top: 0; }
.grid2 { display: grid; grid-template-columns: repeat(auto-fit, minmax(420px, 1fr)); gap: 16px; align-items: start; }
.table-wrap { overflow-x: auto; background: var(--panel); border: 1px solid var(--line); border-radius: var(--radius); }
.table { width: 100%; border-collapse: collapse; font-size: 13px; }
.table th, .table td { padding: 8px 10px; border-bottom: 1px solid #f3f4f6; text-align: left; vertical-align: top; }
.table th { background: #f9fafb; font-weight: 600; color: #374151; position: sticky; top: 0; }
.table tr.link { cursor: pointer; } .table tr.link:hover { background: #f9fafb; }
.kpis { display: grid; grid-template-columns: repeat(auto-fit, minmax(160px, 1fr)); gap: 12px; margin-bottom: 16px; }
.kpi { background: var(--panel); border: 1px solid var(--line); border-radius: var(--radius); padding: 14px; }
.kpi .v { font-size: 26px; font-weight: 800; }
.kpi .l { color: var(--muted); font-size: 12px; }
.bars { display: flex; align-items: flex-end; gap: 3px; height: 160px; padding-top: 10px; }
.bar-col { flex: 1; display: flex; flex-direction: column; justify-content: flex-end; align-items: center; height: 100%; gap: 1px; min-width: 6px; }
.bar { width: 100%; border-radius: 3px 3px 0 0; }
.bar.a { background: #cbd5e1; } .bar.b { background: var(--brand); }
.bar-x { font-size: 9px; color: var(--muted); margin-top: 3px; }
.legend { display: flex; gap: 14px; font-size: 12px; color: var(--muted); }
.legend i { display: inline-block; width: 10px; height: 10px; border-radius: 2px; margin-right: 4px; vertical-align: middle; }
.hbar { display: grid; grid-template-columns: 140px 1fr 40px; gap: 8px; align-items: center; margin: 6px 0; font-size: 13px; }
.hbar .track { background: #f3f4f6; border-radius: 4px; height: 12px; }
.hbar .fill { background: var(--brand); height: 100%; border-radius: 4px; }

.toast { position: fixed; bottom: 20px; right: 20px; background: #111827; color: #fff; padding: 10px 16px; border-radius: 10px; z-index: 50; max-width: 380px; }

@media (max-width: 1100px) { .side { display: none; } }
@media (max-width: 760px) { .convs { width: 100%; } .chat { display: none; } .inbox.show-chat .convs { display: none; } .inbox.show-chat .chat { display: flex; } }

/* Trạng thái trực */
.duty-btn { width: 64px; border: 0; border-radius: 10px; padding: 8px 2px; cursor: pointer; font-size: 10px; font-weight: 700; line-height: 1.3; margin-bottom: 6px; }
.duty-btn.on { background: #16a34a; color: #fff; }
.duty-btn.off { background: #374151; color: #9ca3af; }
.duty-dot { display: inline-block; width: 8px; height: 8px; border-radius: 50%; margin-right: 4px; vertical-align: middle; }
.duty-dot.on { background: #16a34a; } .duty-dot.off { background: #d1d5db; }
.toast.alert { background: var(--brand); cursor: pointer; }

/* Tải kịch bản */
.upload-box { border: 2px dashed var(--line); border-radius: 10px; padding: 18px; text-align: center; display: grid; gap: 6px; background: #fafafa; }
.upload-box.drag { border-color: var(--brand); background: var(--brand-soft); }
.err-list { color: var(--brand); font-size: 13px; margin: 8px 0; }
.ok-text { color: var(--green); }
.test-box { margin-top: 16px; padding: 12px; background: #f9fafb; border-radius: 10px; }
.test-result { margin-top: 8px; padding: 10px; border-radius: 8px; background: #fff; border: 1px solid var(--line); white-space: pre-wrap; }
.faq-q { font-size: 12px; } .faq-q span { display: inline-block; background: #f3f4f6; border-radius: 6px; padding: 1px 6px; margin: 1px; }
.faq-edit textarea, .faq-edit input { width: 100%; margin: 3px 0; }

/* Kết nối */
.copy-row { display: grid; grid-template-columns: 1fr auto; gap: 4px 8px; align-items: center; margin: 8px 0; }
.copy-row small { grid-column: 1 / -1; color: var(--muted); }
.copy-row code { background: #f3f4f6; padding: 6px 8px; border-radius: 6px; font-size: 12px; overflow-wrap: anywhere; }
.badge.zalo { background: #0068ff; color: #fff; }
.badge.web { background: #7c3aed; color: #fff; }
.badge.ok { background: #dcfce7; color: #166534; } .badge.no { background: #fee2e2; color: #991b1b; }
details summary { cursor: pointer; margin-bottom: 6px; }
label > input:not([type=checkbox]):not([type=radio]) { display: block; width: 100%; margin-top: 4px; }

.rail-btn { position: relative; }
.rail-badge { position: absolute; top: 2px; right: 8px; background: var(--brand); color: #fff; border-radius: 9px; font-size: 10px; min-width: 16px; padding: 0 4px; line-height: 16px; }
.cb-overdue { color: var(--brand); font-weight: 700; }
.side .ad-src { font-size: 12px; background: #eff6ff; color: #1e40af; padding: 4px 8px; border-radius: 6px; margin-top: 6px; }

/* Báo cáo quảng cáo */
.kpi.hl { border-color: #fca5a5; background: #fff7f7; }
.card.warn { border-color: #fcd34d; background: #fffbeb; }
.bar.spend { background: #93c5fd; width: 70%; margin: 0 auto; }
.bar-n { font-size: 10px; font-weight: 700; color: var(--brand); text-align: center; min-height: 12px; }
.table.num td:not(:first-child), .table.num th:not(:first-child) { text-align: right; white-space: nowrap; }
#adsBlock .seg button { white-space: nowrap; }
#adsBlock .kpi .v { font-size: 21px; white-space: nowrap; }
.ads-block { border-top: 2px solid var(--line); padding-top: 12px; margin: 8px 0 18px; }
