:root {
  --bg: #f3f4f6;
  --panel: #ffffff;
  --border: #e5e7eb;
  --text: #111827;
  --muted: #6b7280;
  --accent: #2563eb;
  --accent-text: #ffffff;
  --bubble-in: #e9ebef;
  --hover: #f3f4f6;
  --active: #e0e7ff;
  --online: #22c55e;
  --danger: #dc2626;
}
@media (prefers-color-scheme: dark) {
  :root {
    --bg: #0b0f17;
    --panel: #111827;
    --border: #1f2937;
    --text: #e5e7eb;
    --muted: #9ca3af;
    --accent: #3b82f6;
    --bubble-in: #1f2937;
    --hover: #1a2231;
    --active: #1e2a4a;
    --danger: #f87171;
  }
}

* { box-sizing: border-box; }
[hidden] { display: none !important; }
html, body { height: 100%; margin: 0; }
body {
  font-family: system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
  background: var(--bg);
  color: var(--text);
  -webkit-font-smoothing: antialiased;
}
button { font: inherit; cursor: pointer; }
input, textarea { font: inherit; color: inherit; }
svg { width: 22px; height: 22px; fill: none; stroke: currentColor; stroke-width: 2; stroke-linecap: round; stroke-linejoin: round; }

/* ---------- Đăng nhập ---------- */
.login { min-height: 100dvh; display: grid; place-items: center; padding: 16px; }
.login-card {
  width: 100%; max-width: 360px; background: var(--panel); border: 1px solid var(--border);
  border-radius: 16px; padding: 32px 24px; display: flex; flex-direction: column; gap: 14px;
  box-shadow: 0 10px 30px rgb(0 0 0 / .06);
}
.login-card .logo { font-size: 40px; text-align: center; }
.login-card h1 { margin: 0 0 6px; text-align: center; font-size: 22px; }
.login-card label { display: flex; flex-direction: column; gap: 6px; font-size: 14px; color: var(--muted); }
.login-card input {
  padding: 12px; border: 1px solid var(--border); border-radius: 10px; background: var(--bg); font-size: 16px;
}
.login-card input:focus { outline: 2px solid var(--accent); outline-offset: -1px; }
.login-card button {
  padding: 12px; border: 0; border-radius: 10px; background: var(--accent); color: var(--accent-text);
  font-weight: 600; font-size: 16px;
}
.login-card button:disabled { opacity: .6; }
.error { color: var(--danger); font-size: 14px; margin: 0; min-height: 1em; }

/* ---------- Bố cục chính ---------- */
.app { height: 100dvh; display: grid; grid-template-columns: 340px 1fr; overflow: hidden; }
.sidebar { background: var(--panel); border-right: 1px solid var(--border); display: flex; flex-direction: column; min-height: 0; }
.chat { display: flex; flex-direction: column; min-width: 0; min-height: 0; background: var(--bg); }

.icon-btn {
  border: 0; background: transparent; color: var(--muted); width: 40px; height: 40px; border-radius: 50%;
  display: grid; place-items: center; flex-shrink: 0;
}
.icon-btn:hover { background: var(--hover); color: var(--text); }

.avatar {
  --h: 220; width: 42px; height: 42px; border-radius: 50%; flex-shrink: 0; position: relative;
  display: grid; place-items: center; font-weight: 600; color: #fff; font-size: 16px;
  background: hsl(var(--h) 60% 50%);
}
.avatar.online::after {
  content: ""; position: absolute; right: 0; bottom: 0; width: 11px; height: 11px; border-radius: 50%;
  background: var(--online); border: 2px solid var(--panel);
}

.side-head {
  display: flex; align-items: center; justify-content: space-between; padding: 12px 12px 12px 16px;
  padding-top: max(12px, env(safe-area-inset-top)); border-bottom: 1px solid var(--border);
}
.me { display: flex; align-items: center; gap: 10px; min-width: 0; }
.me-name { font-weight: 600; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.search { padding: 10px 12px; }
.search input {
  width: 100%; padding: 10px 14px; border-radius: 20px; border: 1px solid var(--border); background: var(--bg); font-size: 15px;
}
.search input:focus { outline: 2px solid var(--accent); outline-offset: -1px; }

.user-list { list-style: none; margin: 0; padding: 0 6px 12px; overflow-y: auto; flex: 1; }
.user-item {
  display: flex; align-items: center; gap: 12px; padding: 10px; border-radius: 12px; cursor: pointer;
}
.user-item:hover { background: var(--hover); }
.user-item.active { background: var(--active); }
.user-meta { flex: 1; min-width: 0; }
.user-top { display: flex; justify-content: space-between; gap: 8px; align-items: baseline; }
.user-name { font-weight: 600; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.user-time { font-size: 12px; color: var(--muted); flex-shrink: 0; }
.user-bottom { display: flex; justify-content: space-between; gap: 8px; align-items: center; margin-top: 2px; }
.user-last { font-size: 14px; color: var(--muted); white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.user-item.unread .user-last { color: var(--text); font-weight: 600; }
.badge {
  background: var(--accent); color: var(--accent-text); font-size: 12px; font-weight: 700; min-width: 20px; height: 20px;
  border-radius: 10px; padding: 0 6px; display: grid; place-items: center; flex-shrink: 0;
}
.list-empty { color: var(--muted); text-align: center; padding: 24px; font-size: 14px; }

/* ---------- Khung chat ---------- */
.chat-empty { flex: 1; display: grid; place-content: center; text-align: center; color: var(--muted); padding: 24px; }
.chat-empty div { font-size: 56px; }
.chat-panel { flex: 1; display: flex; flex-direction: column; min-height: 0; }
.chat-head {
  display: flex; align-items: center; gap: 10px; padding: 10px 14px; background: var(--panel);
  border-bottom: 1px solid var(--border); padding-top: max(10px, env(safe-area-inset-top));
}
.chat-head .back { display: none; margin-left: -6px; }
.peer-info { min-width: 0; }
.peer-name { font-weight: 600; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.peer-status { font-size: 13px; color: var(--muted); }

.messages { flex: 1; overflow-y: auto; padding: 16px; display: flex; flex-direction: column; gap: 3px; overscroll-behavior: contain; }
.day-sep { align-self: center; font-size: 12px; color: var(--muted); margin: 12px 0 6px; background: var(--panel); padding: 3px 10px; border-radius: 10px; }
.msg { display: flex; max-width: min(75%, 520px); }
.msg.mine { align-self: flex-end; }
.bubble {
  background: var(--bubble-in); padding: 8px 12px; border-radius: 18px; white-space: pre-wrap; word-wrap: break-word;
  overflow-wrap: anywhere; line-height: 1.4; font-size: 15px;
}
.msg.mine .bubble { background: var(--accent); color: var(--accent-text); }
.bubble.has-img { padding: 4px; }
.bubble.has-img .txt { padding: 4px 8px 4px; }
.bubble img {
  display: block; max-width: 100%; width: 280px; max-height: 360px; object-fit: cover; border-radius: 14px; cursor: zoom-in;
  background: rgb(0 0 0 / .08);
}
.bubble a { color: inherit; }
.msg-time { font-size: 11px; color: var(--muted); align-self: flex-end; margin: 0 6px 2px; white-space: nowrap; }
.msg.mine .msg-time { order: -1; }
.msg.pending { opacity: .6; }
.seen { align-self: flex-end; font-size: 12px; color: var(--muted); margin: 2px 4px 0; }
.typing { font-size: 13px; color: var(--muted); font-style: italic; }
.load-more { align-self: center; border: 1px solid var(--border); background: var(--panel); color: var(--muted); border-radius: 14px; padding: 4px 12px; font-size: 13px; margin-bottom: 8px; }

.composer {
  display: flex; align-items: flex-end; gap: 6px; padding: 10px 10px; background: var(--panel); border-top: 1px solid var(--border);
  padding-bottom: max(10px, env(safe-area-inset-bottom));
}
.composer textarea {
  flex: 1; resize: none; border: 1px solid var(--border); background: var(--bg); border-radius: 20px; padding: 9px 14px;
  font-size: 16px; line-height: 1.35; max-height: 140px; min-height: 40px;
}
.composer textarea:focus { outline: 2px solid var(--accent); outline-offset: -1px; }
.send-btn {
  width: 40px; height: 40px; border-radius: 50%; border: 0; background: var(--accent); color: var(--accent-text);
  display: grid; place-items: center; flex-shrink: 0;
}
.send-btn svg { width: 19px; height: 19px; }
.send-btn:disabled { opacity: .5; }

/* ---------- Xem ảnh ---------- */
.lightbox {
  position: fixed; inset: 0; background: rgb(0 0 0 / .92); display: grid; place-items: center; z-index: 50; padding: 16px;
}
.lightbox img { max-width: 100%; max-height: 100%; object-fit: contain; border-radius: 4px; }
.lb-close { position: absolute; top: max(12px, env(safe-area-inset-top)); right: 12px; color: #fff; font-size: 22px; background: rgb(255 255 255 / .12); }
.lb-close:hover { background: rgb(255 255 255 / .25); color: #fff; }

.toast {
  position: fixed; left: 50%; bottom: 90px; transform: translateX(-50%); background: #111827; color: #fff;
  padding: 10px 16px; border-radius: 10px; font-size: 14px; z-index: 60; max-width: calc(100% - 32px);
}

/* ---------- Mobile: 1 cột, chuyển giữa danh sách và khung chat ---------- */
@media (max-width: 767px) {
  .app { grid-template-columns: 1fr; }
  .chat { display: none; }
  .app.chat-open .sidebar { display: none; }
  .app.chat-open .chat { display: flex; }
  .chat-head .back { display: grid; }
  .msg { max-width: 85%; }
  .bubble img { width: 240px; }
}
