:root {
  --bg: #fff6f8;
  --panel: #ffffff;
  --panel-2: #fffafb;
  --ink: #3a1f2b;
  --muted: #8b6b78;
  --line: #f0dbe2;
  --rose: #d6336c;
  --rose-ink: #ffffff;
  --rose-soft: #fde4ec;
  --bubble-in: #f6eef1;
  --ok: #2f9e6e;
  --warn: #c77700;
  --danger: #c92a3a;
  --radius-lg: 18px;
  --radius: 12px;
  --radius-sm: 8px;
  --font: "Be Vietnam Pro", "Segoe UI", system-ui, -apple-system, Roboto, Arial, sans-serif;
  color-scheme: light;
}
@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) {
    --bg: #1b1317;
    --panel: #241a1f;
    --panel-2: #20171b;
    --ink: #f7e9ef;
    --muted: #b399a4;
    --line: #3a2a32;
    --rose: #ff6b9a;
    --rose-ink: #2a0f1a;
    --rose-soft: #3b1f2b;
    --bubble-in: #2f2229;
    --ok: #4cc38a;
    --warn: #f0a33a;
    --danger: #ff6b6b;
    color-scheme: dark;
  }
}

* { box-sizing: border-box; }
html, body { height: 100%; margin: 0; }
body {
  background: var(--bg);
  color: var(--ink);
  font: 400 15px/1.5 var(--font);
  -webkit-font-smoothing: antialiased;
  padding-top: env(safe-area-inset-top, 0px);
  padding-bottom: env(safe-area-inset-bottom, 0px);
}
#app { height: 100%; }
button, input, textarea { font: inherit; color: inherit; }
button { cursor: pointer; }
:focus-visible { outline: 2px solid var(--rose); outline-offset: 2px; }
img { max-width: 100%; }

/* ---------- Nút & ô nhập ---------- */
.btn {
  display: inline-flex; align-items: center; justify-content: center; gap: 6px;
  border: 1px solid var(--line); background: var(--panel); color: var(--ink);
  border-radius: 999px; padding: 7px 14px; font-weight: 500; font-size: 14px;
  white-space: nowrap;
}
.btn:hover { border-color: var(--rose); }
.btn.primary { background: var(--rose); border-color: var(--rose); color: var(--rose-ink); font-weight: 600; }
.btn.primary:hover { filter: brightness(1.06); }
.btn.ghost { border-color: transparent; background: transparent; }
.btn.ghost:hover { background: var(--rose-soft); }
.btn.danger { color: var(--danger); }
.btn:disabled { opacity: .5; cursor: not-allowed; }
.icon-btn { width: 38px; height: 38px; padding: 0; border-radius: 50%; font-size: 18px; }

.field { display: grid; gap: 6px; margin-bottom: 14px; }
.field label { font-size: 13px; font-weight: 500; color: var(--muted); }
.input {
  width: 100%; border: 1px solid var(--line); background: var(--panel-2);
  border-radius: var(--radius-sm); padding: 10px 12px; outline: none;
}
.input:focus { border-color: var(--rose); background: var(--panel); }

/* ---------- Màn hình đăng nhập / cài đặt ---------- */
.auth {
  min-height: 100%; display: grid; place-items: center; padding: 24px;
}
.auth-card {
  width: 100%; max-width: 380px; background: var(--panel);
  border: 1px solid var(--line); border-radius: var(--radius-lg); padding: 28px;
}
.brand { display: flex; align-items: center; gap: 10px; margin-bottom: 4px; }
.brand-mark {
  width: 36px; height: 36px; border-radius: 11px 11px 11px 3px;
  background: var(--rose); color: var(--rose-ink);
  display: grid; place-items: center; font-weight: 700; font-size: 15px;
}
.brand h1 { font-size: 20px; margin: 0; font-weight: 700; letter-spacing: -.01em; white-space: nowrap; }
.auth-card p.lead { color: var(--muted); margin: 6px 0 22px; font-size: 14px; }
.form-error { color: var(--danger); font-size: 14px; min-height: 20px; margin: -4px 0 10px; }
.auth-card .btn.primary { width: 100%; padding: 11px; }

/* ---------- Khung ứng dụng ---------- */
.shell { height: 100%; display: grid; grid-template-columns: minmax(0, 1fr); grid-template-rows: auto 1fr; }
.topbar {
  display: flex; align-items: center; gap: 10px; padding: 10px 16px;
  border-bottom: 1px solid var(--line); background: var(--panel);
}
.topbar .brand { margin: 0; flex: 1; min-width: 0; }
.topbar .brand h1 { font-size: 17px; overflow: hidden; text-overflow: ellipsis; }
.topbar .btn { flex: none; }
.topbar .who { color: var(--muted); font-size: 13px; }

.main {
  display: grid; grid-template-columns: 236px 340px 1fr; min-height: 0;
}

/* Ảnh page dùng chung */
.pic {
  width: 34px; height: 34px; border-radius: 50%; object-fit: cover; flex: none;
  background: var(--line); display: grid; place-items: center; font-size: 13px; font-weight: 600; color: var(--muted);
}

/* Cột page */
.pages {
  border-right: 1px solid var(--line); overflow-y: auto; padding: 10px 8px;
  background: var(--panel-2);
}
.page-item {
  width: 100%; display: flex; align-items: center; gap: 10px; text-align: left;
  border: 0; background: transparent; border-radius: var(--radius); padding: 8px 10px;
}
.page-item:hover { background: var(--rose-soft); }
.page-item.active { background: var(--rose-soft); font-weight: 600; }
.page-item .pic {
  width: 30px; height: 30px; border-radius: 50%; object-fit: cover; flex: none;
  background: var(--line); display: grid; place-items: center; font-size: 13px; font-weight: 600; color: var(--muted);
}
.page-item .label { flex: 1; min-width: 0; }
.page-item .label b, .page-item .label small { display: block; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.page-item .label b { font-weight: inherit; font-size: 14px; }
.page-item .label small { color: var(--muted); font-size: 12px; font-weight: 400; }
.count {
  min-width: 20px; height: 20px; border-radius: 10px; padding: 0 6px;
  background: var(--rose); color: var(--rose-ink); font-size: 12px; font-weight: 600;
  display: inline-grid; place-items: center; flex: none;
}
.page-item .err { color: var(--danger); font-size: 14px; }
.pages-empty { padding: 14px 10px; color: var(--muted); font-size: 13px; }

/* Cột hội thoại */
.convs { border-right: 1px solid var(--line); display: grid; grid-template-rows: auto 1fr; min-height: 0; background: var(--panel); }
.convs-tools { padding: 10px; display: grid; gap: 8px; border-bottom: 1px solid var(--line); }
.convs-tools .row { display: flex; gap: 6px; }
.chip {
  border: 1px solid var(--line); background: transparent; border-radius: 999px; padding: 4px 12px; font-size: 13px;
}
.chip.on { background: var(--ink); color: var(--panel); border-color: var(--ink); }
.conv-list { overflow-y: auto; }
.conv {
  width: 100%; display: grid; grid-template-columns: 44px 1fr auto; gap: 10px; align-items: center;
  border: 0; border-bottom: 1px solid var(--line); background: transparent; text-align: left; padding: 11px 12px;
}
.conv:hover { background: var(--panel-2); }
.conv.active { background: var(--rose-soft); }
.avatar {
  width: 44px; height: 44px; border-radius: 50%; object-fit: cover; background: var(--line);
  display: grid; place-items: center; font-weight: 600; color: var(--muted); flex: none;
}
.conv .body { min-width: 0; }
.conv .name { font-weight: 500; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.conv.unread .name { font-weight: 700; }
.conv .snippet { color: var(--muted); font-size: 13px; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.conv.unread .snippet { color: var(--ink); }
.conv .group { color: var(--rose); font-size: 12px; font-weight: 500; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.conv .meta { display: grid; justify-items: end; gap: 4px; }
.conv .time { color: var(--muted); font-size: 12px; }
.list-empty { padding: 32px 20px; text-align: center; color: var(--muted); font-size: 14px; }
.more { display: block; margin: 12px auto; }

/* Cột chat */
.chat { display: grid; grid-template-columns: minmax(0, 1fr); grid-template-rows: auto auto 1fr auto; min-height: 0; min-width: 0; background: var(--bg); }
.chat-empty { grid-row: 1 / -1; display: grid; place-items: center; text-align: center; color: var(--muted); padding: 24px; }
.chat-empty .big { font-size: 40px; }
.chat-head {
  display: flex; align-items: center; gap: 12px; padding: 10px 16px;
  background: var(--panel); border-bottom: 1px solid var(--line);
}
.chat-head .back { display: none; }
.chat-head .info { flex: 1; min-width: 0; }
.chat-head .info b { display: block; font-size: 15px; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.chat-head .info small { display: block; color: var(--muted); font-size: 12px; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.chat-head .avatar { width: 38px; height: 38px; }

/* Thước đo khung 24 giờ */
.window {
  display: flex; align-items: center; gap: 12px; padding: 7px 16px;
  background: var(--panel); border-bottom: 1px solid var(--line); font-size: 13px;
}
.window .track { flex: 1; min-width: 40px; height: 6px; border-radius: 3px; background: var(--line); overflow: hidden; }
.window .fill { height: 100%; border-radius: 3px; background: var(--ok); transition: width .4s ease; }
.window.low .fill { background: var(--warn); }
.window.closed .fill { background: var(--danger); }
.window .left { color: var(--muted); white-space: nowrap; font-variant-numeric: tabular-nums; }
.window.low .left { color: var(--warn); font-weight: 500; }
.window.closed .left { color: var(--danger); font-weight: 500; }

.msgs { overflow-y: auto; padding: 16px; display: flex; flex-direction: column; gap: 4px; }
.day { align-self: center; color: var(--muted); font-size: 12px; margin: 12px 0 6px; }
.msg { max-width: min(72%, 560px); display: grid; gap: 2px; }
.msg.in { align-self: flex-start; }
.msg.out { align-self: flex-end; justify-items: end; }
.bubble {
  padding: 8px 12px; border-radius: 16px; white-space: pre-wrap; overflow-wrap: anywhere;
}
.msg.in .bubble { background: var(--bubble-in); border-bottom-left-radius: 5px; }
.msg.out .bubble { background: var(--rose); color: var(--rose-ink); border-bottom-right-radius: 5px; }
.msg.pending .bubble { opacity: .6; }
.msg .stamp { font-size: 11px; color: var(--muted); padding: 0 4px; }
.msg .att img, .msg .att video { display: block; max-width: 260px; max-height: 320px; border-radius: 12px; background: var(--line); }
.msg .att audio { max-width: 260px; }
.msg .att a { color: inherit; }
.msg .att.sticker img { max-width: 120px; background: transparent; }
.load-older { align-self: center; margin-bottom: 8px; }

.composer {
  display: flex; align-items: flex-end; gap: 8px; padding: 10px 12px;
  background: var(--panel); border-top: 1px solid var(--line);
}
.composer textarea {
  flex: 1; resize: none; border: 1px solid var(--line); background: var(--panel-2);
  border-radius: 20px; padding: 9px 14px; max-height: 160px; outline: none; line-height: 1.45;
}
.composer textarea:focus { border-color: var(--rose); background: var(--panel); }
.composer .btn.primary { height: 40px; }
.composer .note { flex: 1; color: var(--danger); font-size: 14px; padding: 8px 4px; }

/* ---------- Hộp thoại ---------- */
.modal-back {
  position: fixed; inset: 0; background: rgba(40, 15, 28, .45); display: grid; place-items: center;
  padding: 16px; z-index: 50;
}
.modal {
  width: 100%; max-width: 560px; max-height: calc(100% - 32px); overflow: auto;
  background: var(--panel); border-radius: var(--radius-lg); border: 1px solid var(--line);
}
.modal-head { display: flex; align-items: center; gap: 10px; padding: 16px 20px; border-bottom: 1px solid var(--line); position: sticky; top: 0; background: var(--panel); z-index: 1; }
.modal-head h2 { flex: 1; margin: 0; font-size: 17px; }
.modal-body { padding: 18px 20px; }
.modal-foot { display: flex; justify-content: flex-end; gap: 8px; padding: 14px 20px; border-top: 1px solid var(--line); position: sticky; bottom: 0; background: var(--panel); }
.modal p.hint { color: var(--muted); font-size: 14px; margin: 0 0 14px; }
.tabs { display: flex; gap: 6px; margin-bottom: 16px; }

.pick { display: grid; grid-template-columns: auto 34px 1fr; gap: 10px; align-items: center; padding: 10px 0; border-bottom: 1px solid var(--line); }
.pick:last-child { border-bottom: 0; }
.pick input[type="checkbox"] { width: 18px; height: 18px; accent-color: var(--rose); }
.pick .pic { width: 34px; height: 34px; border-radius: 50%; object-fit: cover; background: var(--line); }
.pick .detail { min-width: 0; }
.pick .detail b { display: block; font-weight: 600; font-size: 14px; }
.pick .detail small { display: block; color: var(--muted); font-size: 12px; }
.pick .detail small.warn { color: var(--warn); }
.pick .detail .input { margin-top: 6px; padding: 7px 10px; font-size: 14px; }

.row-item { display: flex; align-items: center; gap: 10px; padding: 10px 0; border-bottom: 1px solid var(--line); }
.row-item:last-child { border-bottom: 0; }
.row-item .grow { flex: 1; min-width: 0; }
.row-item .grow b { display: block; font-size: 14px; }
.row-item .grow small { display: block; color: var(--muted); font-size: 12px; }
.row-item .grow small.bad { color: var(--danger); }
.result-ok { color: var(--ok); }
.result-bad { color: var(--danger); }
details.manual { margin-top: 16px; font-size: 14px; }
details.manual summary { cursor: pointer; color: var(--muted); }
details.manual textarea { margin-top: 10px; min-height: 80px; resize: vertical; }

/* ---------- Thông báo nhỏ ---------- */
#toast {
  position: fixed; left: 50%; bottom: calc(20px + env(safe-area-inset-bottom, 0px)); transform: translateX(-50%) translateY(20px);
  background: var(--ink); color: var(--panel); padding: 10px 16px; border-radius: 999px; font-size: 14px;
  opacity: 0; pointer-events: none; transition: opacity .2s, transform .2s; z-index: 60; max-width: calc(100% - 32px);
}
#toast.show { opacity: 1; transform: translateX(-50%) translateY(0); }
#toast.bad { background: var(--danger); color: #fff; }

/* ---------- Điện thoại ---------- */
@media (max-width: 860px) {
  .topbar .who { display: none; }
  .topbar .btn .txt { display: none; }
  .topbar { padding: 8px 10px; gap: 4px; }
  .topbar .btn.ghost { padding: 7px 10px; }
  .main { grid-template-columns: minmax(0, 1fr); grid-template-rows: auto 1fr; }
  .pages {
    display: flex; gap: 6px; overflow-x: auto; overflow-y: hidden; padding: 8px 10px;
    border-right: 0; border-bottom: 1px solid var(--line);
  }
  .page-item { width: auto; flex: none; padding: 5px 10px 5px 5px; border-radius: 999px; border: 1px solid var(--line); background: var(--panel); }
  .page-item .label small { display: none; }
  .page-item .label b { max-width: 140px; font-size: 13px; }
  .page-item .pic { width: 24px; height: 24px; }
  .convs { border-right: 0; }
  .chat { position: fixed; inset: 0; z-index: 20; padding-top: env(safe-area-inset-top, 0px); padding-bottom: env(safe-area-inset-bottom, 0px); display: none; }
  .shell.view-chat .chat { display: grid; }
  .chat-head .back { display: inline-flex; }
  .msg { max-width: 84%; }
}
@media (prefers-reduced-motion: reduce) {
  * { transition: none !important; }
}
