:root {
  --bg: #fff;
  --bg-soft: #f3f4f6;
  --line: #e5e7eb;
  --text: #111827;
  --muted: #6b7280;
  --brand: #0068ff;
  --brand-text: #fff;
  --out: #dbeafe;
  --ok: #047857;
  --ok-bg: #d1fae5;
  --warn: #92400e;
  --warn-bg: #fef3c7;
  --danger: #b91c1c;
  --radius: 12px;
  --tap: 44px;
  color-scheme: light;
}

* { box-sizing: border-box; }
html, body { margin: 0; height: 100%; }
body {
  font: 16px/1.5 -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, sans-serif;
  color: var(--text); background: var(--bg);
  overflow: hidden; -webkit-text-size-adjust: 100%;
}
[hidden] { display: none !important; }
button, input, textarea { font: inherit; color: inherit; }
button { min-height: var(--tap); padding: 0 14px; border: 0; border-radius: var(--radius); background: var(--bg-soft); cursor: pointer; }
.primary { background: var(--brand); color: var(--brand-text); font-weight: 600; }
.danger { background: var(--bg-soft); color: var(--danger); }
.ghost { background: transparent; }
.grow { flex: 1; min-width: 0; }
.tnum { font-variant-numeric: tabular-nums; }
.pretty { text-wrap: pretty; }
.muted { color: var(--muted); }
.err { color: var(--danger); min-height: 1.5em; margin: 0; }

/* ---------- đăng nhập ---------- */
.login { max-width: 360px; margin: 15vh auto 0; padding: 0 16px; display: grid; gap: 14px; }
.login label { display: grid; gap: 4px; }
input, textarea { min-height: var(--tap); padding: 10px 12px; border: 1px solid var(--line); border-radius: var(--radius); background: var(--bg); }

/* ---------- khung: điện thoại 1 cột, máy tính 2 cột ---------- */
.app { height: var(--app-h, 100dvh); display: grid; grid-template-columns: minmax(0, 1fr); }
.list, .thread { display: flex; flex-direction: column; min-height: 0; min-width: 0; height: var(--app-h, 100dvh); }
.thread { display: none; }
body.thread-open .list { display: none; }
body.thread-open .thread { display: flex; }
@media (min-width: 768px) {
  .app { grid-template-columns: 340px minmax(0, 1fr); }
  .list { display: flex !important; border-right: 1px solid var(--line); }
  .thread { display: flex; }
  .back { display: none; }
  /* Chưa chọn hội thoại: không hiện ô soạn tin để gõ vào khoảng không */
  body:not(.thread-open) .thread > * { display: none; }
  body:not(.thread-open) .thread::after { content: 'Chọn một hội thoại bên trái'; margin: auto; color: var(--muted); }
}

.bar { display: flex; align-items: center; gap: 8px; padding: 6px 8px; padding-top: max(6px, env(safe-area-inset-top)); border-bottom: 1px solid var(--line); }
.bar b { padding-left: 6px; }
.chip { min-height: 32px; padding: 0 10px; border-radius: 999px; font-size: 14px; margin-left: auto; max-width: 55vw; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.chip.ok { background: var(--ok-bg); color: var(--ok); }
.chip.warn { background: var(--warn-bg); color: var(--warn); }
.banner { background: var(--warn-bg); color: var(--warn); padding: 8px 12px; font-size: 14px; }
.empty { color: var(--muted); padding: 24px 16px; text-align: center; }

/* ---------- danh sách hội thoại ---------- */
.convs { list-style: none; margin: 0; padding: 0; overflow-y: auto; flex: 1; -webkit-overflow-scrolling: touch; padding-bottom: env(safe-area-inset-bottom); }
.conv { width: 100%; display: flex; gap: 10px; align-items: center; text-align: left; background: none; border-radius: 0; padding: 10px 12px; min-height: 64px; border-bottom: 1px solid var(--line); }
.conv.on { background: var(--bg-soft); }
.line { display: flex; gap: 8px; align-items: baseline; }
.name, .last { flex: 1; min-width: 0; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.last { color: var(--muted); font-size: 14px; }
.time { color: var(--muted); font-size: 13px; }
.badge { font-size: 12px; background: var(--warn-bg); color: var(--warn); border-radius: 999px; padding: 0 8px; white-space: nowrap; }
.avatar { width: 40px; height: 40px; border-radius: 50%; flex: none; object-fit: cover; background: var(--bg-soft); display: grid; place-items: center; font-weight: 600; color: var(--muted); }

/* ---------- hội thoại ---------- */
.msgs { flex: 1; overflow-y: auto; padding: 12px; -webkit-overflow-scrolling: touch; overscroll-behavior: contain; }
.older { display: block; margin: 0 auto 8px; color: var(--brand); }
.msg { display: flex; flex-direction: column; align-items: flex-start; margin: 6px 0; }
.msg.out { align-items: flex-end; }
.bubble { max-width: min(80%, 560px); background: var(--bg-soft); border-radius: 16px; padding: 8px 12px; overflow-wrap: anywhere; }
.msg.out .bubble { background: var(--out); }
.bubble p { margin: 0; white-space: pre-wrap; }
.bubble img, .bubble video { display: block; max-width: 100%; max-height: 320px; border-radius: 10px; }
.bubble.bare, .msg.out .bubble.bare { background: none; padding: 0; }
.bubble img.sticker { width: 120px; height: 120px; object-fit: contain; border-radius: 0; }
.bubble.sending { opacity: .6; }
.bubble.failed, .bubble.unknown { outline: 2px solid var(--warn-bg); }
.meta { font-size: 12px; color: var(--muted); margin-top: 2px; }
.st-failed, .st-unknown { color: var(--warn); font-weight: 600; }
.warn { font-size: 13px; color: var(--warn); background: var(--warn-bg); border-radius: 8px; padding: 6px 10px; margin-top: 4px; max-width: min(80%, 560px); }
.retry { min-height: 32px; color: var(--brand); padding: 0 6px; }

.composer { display: flex; gap: 8px; align-items: flex-end; padding: 8px; padding-bottom: max(8px, env(safe-area-inset-bottom)); border-top: 1px solid var(--line); }
.composer textarea { flex: 1; resize: none; max-height: 140px; }
.icon { min-width: var(--tap); min-height: var(--tap); display: grid; place-items: center; font-size: 22px; cursor: pointer; }

/* ---------- hộp thoại Zalo ---------- */
.sheet { border: 0; border-radius: 16px; padding: 20px; width: min(420px, calc(100vw - 32px)); }
.sheet::backdrop { background: rgb(0 0 0 / .4); }
.sheet h2 { margin: 0 0 8px; font-size: 18px; }
.qr { display: block; width: 240px; height: 240px; margin: 12px auto; image-rendering: pixelated; }
.row { display: flex; flex-wrap: wrap; gap: 8px; margin-top: 12px; }
.version { margin: 12px 0 0; text-align: right; font-size: 12px; color: var(--muted); }

.toast { position: fixed; left: 16px; right: 16px; bottom: calc(16px + env(safe-area-inset-bottom)); margin: auto; max-width: 480px; background: var(--text); color: var(--bg); padding: 12px 16px; border-radius: var(--radius); }
