:root {
  --bg: #f0f2f5; --panel: #fff; --border: #e4e6eb; --text: #1c1e21; --muted: #65676b;
  --primary: #0866ff; --primary-soft: #e7f0ff; --bubble-in: #f0f2f5; --bubble-out: #0866ff;
  --danger: #e41e3f; --unread: #e41e3f; --hover: #f2f3f5;
}
* { box-sizing: border-box; }
html, body { height: 100%; margin: 0; }
body { font: 14px/1.4 system-ui, -apple-system, "Segoe UI", Roboto, sans-serif; background: var(--bg); color: var(--text); }
button { font: inherit; cursor: pointer; border: 0; border-radius: 6px; padding: 7px 12px; background: var(--hover); color: var(--text); }
button.primary { background: var(--primary); color: #fff; }
button.danger { color: var(--danger); }
button:disabled { opacity: .5; cursor: default; }
input, textarea, select { font: inherit; border: 1px solid var(--border); border-radius: 6px; padding: 7px 10px; width: 100%; background: #fff; color: var(--text); }
textarea { resize: vertical; }
.muted { color: var(--muted); font-size: 12px; }
.ellipsis { white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
img.avatar { width: 40px; height: 40px; border-radius: 50%; object-fit: cover; background: #ccd0d5; flex: none; }
.avatar.ph { width: 40px; height: 40px; border-radius: 50%; background: #ccd0d5; color: #fff; display: grid; place-items: center; font-weight: 600; flex: none; }

/* Bố cục */
.app { display: grid; grid-template-columns: 220px 340px 1fr 280px; height: 100vh; }
.col { background: var(--panel); border-right: 1px solid var(--border); display: flex; flex-direction: column; min-width: 0; min-height: 0; }
.col-head { padding: 12px; border-bottom: 1px solid var(--border); display: flex; gap: 8px; align-items: center; }
.col-head h2 { font-size: 16px; margin: 0; flex: 1; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.scroll { overflow-y: auto; flex: 1; min-height: 0; }

/* Cột Page */
.page-item { display: flex; align-items: center; gap: 8px; padding: 8px 12px; cursor: pointer; }
.page-item:hover { background: var(--hover); }
.page-item.active { background: var(--primary-soft); color: var(--primary); font-weight: 600; }
.page-item img, .page-item .ph { width: 28px; height: 28px; border-radius: 50%; flex: none; }
.page-item .name { flex: 1; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.badge { background: var(--unread); color: #fff; border-radius: 10px; padding: 0 6px; font-size: 11px; font-weight: 600; min-width: 18px; text-align: center; }
.side-foot { padding: 12px; border-top: 1px solid var(--border); display: grid; gap: 6px; }

/* Danh sách hội thoại */
.filters { padding: 8px 12px; display: grid; gap: 8px; border-bottom: 1px solid var(--border); }
.chips { display: flex; gap: 6px; flex-wrap: wrap; }
.chip { padding: 4px 10px; border-radius: 14px; background: var(--hover); font-size: 13px; }
.chip.active { background: var(--primary-soft); color: var(--primary); font-weight: 600; }
.conv { display: flex; gap: 10px; padding: 10px 12px; cursor: pointer; align-items: center; }
.conv:hover { background: var(--hover); }
.conv.active { background: var(--primary-soft); }
.conv .body { flex: 1; min-width: 0; }
.conv .top { display: flex; gap: 6px; align-items: baseline; }
.conv .name { flex: 1; font-weight: 500; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.conv .snippet { color: var(--muted); white-space: nowrap; overflow: hidden; text-overflow: ellipsis; font-size: 13px; }
.conv.unread .name, .conv.unread .snippet { font-weight: 700; color: var(--text); }
.conv .page-tag { font-size: 11px; color: var(--primary); white-space: nowrap; overflow: hidden; text-overflow: ellipsis; max-width: 100%; }
.conv .labels { display: inline-flex; gap: 4px; }
.label { font-size: 11px; background: #fff3cd; color: #7a5b00; border-radius: 4px; padding: 0 5px; }
.load-more { text-align: center; padding: 10px; }

/* Khung chat */
.chat { background: var(--panel); display: flex; flex-direction: column; min-width: 0; min-height: 0; border-right: 1px solid var(--border); }
.messages { flex: 1; overflow-y: auto; padding: 16px; display: flex; flex-direction: column; gap: 2px; min-height: 0; }
.day { text-align: center; color: var(--muted); font-size: 12px; margin: 12px 0 6px; }
.msg { max-width: 70%; display: flex; flex-direction: column; }
.msg.in { align-self: flex-start; }
.msg.out { align-self: flex-end; align-items: flex-end; }
.bubble { padding: 8px 12px; border-radius: 18px; white-space: pre-wrap; word-wrap: break-word; overflow-wrap: anywhere; }
.msg.in .bubble { background: var(--bubble-in); }
.msg.out .bubble { background: var(--bubble-out); color: #fff; }
.msg img.att { max-width: 260px; max-height: 300px; border-radius: 12px; margin-top: 2px; cursor: zoom-in; }
.msg video.att { max-width: 300px; border-radius: 12px; }
.msg .time { font-size: 11px; color: var(--muted); margin: 1px 6px 6px; }
.msg.pending .bubble { opacity: .6; }
.msg.failed .bubble { background: #fde7ea; color: var(--danger); }
.composer { border-top: 1px solid var(--border); padding: 10px; display: grid; gap: 8px; }
.composer .row { display: flex; gap: 8px; align-items: flex-end; }
.composer textarea { min-height: 40px; max-height: 160px; resize: none; }
.warn { background: #fff3cd; color: #7a5b00; padding: 8px 12px; border-radius: 6px; font-size: 13px; }
.empty { margin: auto; color: var(--muted); text-align: center; padding: 24px; }
.tpl-list { display: flex; gap: 6px; flex-wrap: wrap; }

/* Thông tin khách */
.info { padding: 16px; display: grid; gap: 12px; align-content: start; }
.info .who { display: flex; gap: 10px; align-items: center; }
.info label { display: grid; gap: 4px; font-size: 12px; color: var(--muted); }

/* Hộp thoại */
.modal-bg { position: fixed; inset: 0; background: rgba(0,0,0,.4); display: grid; place-items: center; z-index: 10; padding: 16px; }
.modal { background: var(--panel); border-radius: 10px; width: min(640px, 100%); max-height: 90vh; overflow-y: auto; padding: 20px; display: grid; gap: 14px; }
.modal h3 { margin: 0; }
.modal section { display: grid; gap: 8px; border-top: 1px solid var(--border); padding-top: 14px; }
.modal code { background: var(--hover); padding: 2px 6px; border-radius: 4px; word-break: break-all; }
.lightbox { position: fixed; inset: 0; background: rgba(0,0,0,.85); display: grid; place-items: center; z-index: 20; cursor: zoom-out; }
.lightbox img { max-width: 95vw; max-height: 95vh; }
.toast { position: fixed; bottom: 20px; left: 50%; transform: translateX(-50%); background: #323232; color: #fff; padding: 10px 16px; border-radius: 6px; z-index: 30; max-width: 90vw; }
.back, .info-btn { display: none; }

/* Màn hình nhỏ: hiện từng cột một */
@media (max-width: 1100px) { .app { grid-template-columns: 200px 300px 1fr; } .info-col { display: none; } .info-btn { display: inline-block; } }
@media (max-width: 760px) {
  .app { grid-template-columns: 1fr; }
  .pages-col { display: none; }
  .app.show-pages .pages-col { display: flex; }
  .app.show-pages .list-col { display: none; }
  .app .chat { display: none; }
  .app.chatting .list-col { display: none; }
  .app.chatting .chat { display: flex; }
  .back { display: inline-block; }
  .msg { max-width: 85%; }
}

/* Đăng nhập */
.login { min-height: 100vh; display: grid; place-items: center; padding: 16px; }
.login form { background: var(--panel); padding: 28px; border-radius: 10px; width: min(360px, 100%); display: grid; gap: 12px; box-shadow: 0 2px 12px rgba(0,0,0,.08); }
.login h1 { font-size: 20px; margin: 0 0 4px; }
.error { color: var(--danger); font-size: 13px; min-height: 18px; }
