:root {
  --bg: #f0f2f5;
  --panel: #ffffff;
  --border: #e4e6eb;
  --text: #1c1e21;
  --muted: #65676b;
  --accent: #0a7cff;
  --accent-text: #ffffff;
  --bubble-in: #e9ebee;
  --hover: #f2f3f5;
  --active: #e7f0ff;
  --online: #31a24c;
  --error: #d93025;
}

@media (prefers-color-scheme: dark) {
  :root {
    --bg: #18191a;
    --panel: #242526;
    --border: #3a3b3c;
    --text: #e4e6eb;
    --muted: #b0b3b8;
    --bubble-in: #3a3b3c;
    --hover: #303132;
    --active: #263951;
  }
}

* { box-sizing: border-box; }
html, body { height: 100%; margin: 0; }
body {
  font-family: system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
  background: var(--bg);
  color: var(--text);
}
.hidden { display: none !important; }

button { font: inherit; cursor: pointer; border: 0; border-radius: 8px; }
.primary { background: var(--accent); color: var(--accent-text); padding: 10px 18px; font-weight: 600; }
.primary:hover { filter: brightness(1.08); }
.ghost { background: transparent; color: var(--muted); padding: 6px 10px; }
.ghost:hover { background: var(--hover); }
input, textarea {
  font: inherit; color: var(--text); background: var(--bg);
  border: 1px solid var(--border); border-radius: 8px; padding: 10px 12px; width: 100%;
}
input:focus, textarea:focus { outline: 2px solid var(--accent); border-color: transparent; }

/* ---------- Đăng nhập ---------- */
.auth { min-height: 100%; display: grid; place-items: center; padding: 16px; }
.auth-card {
  width: 100%; max-width: 360px; background: var(--panel); padding: 28px;
  border-radius: 14px; box-shadow: 0 4px 24px rgb(0 0 0 / .08);
  display: flex; flex-direction: column; gap: 14px;
}
.auth-card h1 { margin: 0; text-align: center; font-size: 26px; }
.brand { display: flex; align-items: center; justify-content: center; gap: 10px; }
.auth-card label { display: flex; flex-direction: column; gap: 6px; font-size: 14px; color: var(--muted); }
.tabs { display: grid; grid-template-columns: 1fr 1fr; background: var(--bg); border-radius: 8px; padding: 4px; }
.tab { background: transparent; color: var(--muted); padding: 8px; }
.tab.active { background: var(--panel); color: var(--text); font-weight: 600; }
.error { color: var(--error); margin: 0; min-height: 1.2em; font-size: 14px; }

/* ---------- Bố cục chat ---------- */
.app { height: 100%; display: grid; grid-template-columns: 320px 1fr; }
.sidebar { background: var(--panel); border-right: 1px solid var(--border); display: flex; flex-direction: column; min-height: 0; }
.sidebar-head { display: flex; align-items: center; justify-content: space-between; padding: 14px 16px; }
.me { display: flex; align-items: center; gap: 10px; min-width: 0; }
.me strong { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.search { margin: 0 16px 8px; width: auto; }
.user-list { list-style: none; margin: 0; padding: 0 8px 8px; overflow-y: auto; flex: 1; }
.user-item {
  display: flex; align-items: center; gap: 12px; padding: 10px; border-radius: 10px; cursor: pointer;
}
.user-item:hover { background: var(--hover); }
.user-item.active { background: var(--active); }
.user-info { flex: 1; min-width: 0; }
.user-info .name { font-weight: 600; }
.user-info .preview {
  font-size: 13px; color: var(--muted); overflow: hidden; text-overflow: ellipsis; white-space: nowrap;
}
.user-item.has-unread .preview { color: var(--text); font-weight: 600; }
.badge {
  background: var(--accent); color: var(--accent-text); font-size: 12px; font-weight: 700;
  border-radius: 999px; min-width: 20px; height: 20px; padding: 0 6px; display: grid; place-items: center;
}

.avatar {
  position: relative; flex: none; width: 40px; height: 40px; border-radius: 50%;
  display: grid; place-items: center; color: #fff; font-weight: 700; font-size: 16px;
}
.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);
}

.chat { display: flex; flex-direction: column; min-height: 0; min-width: 0; }
.empty { margin: auto; color: var(--muted); padding: 16px; text-align: center; }
.conversation { display: flex; flex-direction: column; height: 100%; min-height: 0; }
.chat-head {
  display: flex; align-items: center; gap: 12px; padding: 10px 16px;
  background: var(--panel); border-bottom: 1px solid var(--border);
}
.status { font-size: 13px; color: var(--muted); }
.back { display: none; font-size: 20px; }

.messages { flex: 1; overflow-y: auto; padding: 16px; display: flex; flex-direction: column; gap: 4px; }
.day-sep { align-self: center; font-size: 12px; color: var(--muted); margin: 12px 0 6px; }
.msg { max-width: min(75%, 560px); display: flex; flex-direction: column; }
.msg.out { align-self: flex-end; align-items: flex-end; }
.msg.in { align-self: flex-start; align-items: flex-start; }
.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(--accent); color: var(--accent-text); }
.meta { font-size: 11px; color: var(--muted); margin: 2px 6px 0; }
.typing { font-size: 13px; color: var(--muted); font-style: italic; padding: 0 16px 6px; min-height: 1.4em; }

.composer {
  display: flex; gap: 8px; padding: 10px 16px; background: var(--panel); border-top: 1px solid var(--border);
}
.composer textarea { resize: none; max-height: 140px; }

/* Ô mật khẩu là ô chữ thường được che ký tự, để trình duyệt không hỏi lưu mật khẩu */
.secret { -webkit-text-security: disc; text-security: disc; font-family: system-ui, sans-serif; letter-spacing: 1px; }
.secret::placeholder { letter-spacing: normal; }

/* Không thao tác vài giây: làm mờ nội dung tin nhắn & bản xem trước (chạm là hiện lại) */
.app.shaded .messages,
.app.shaded .user-info,
.app.shaded #peer-name { filter: blur(9px); }

/* ---------- Bảo mật: khóa màn hình & tự hủy ---------- */
/* Khi khóa: ẩn hẳn khung chat và phủ nền tối đặc. Không dùng filter/transition
   vì chúng làm chậm khung hình đầu tiên — màn che phải hiện ngay lập tức. */
.app.locked { visibility: hidden; pointer-events: none; user-select: none; }
.lock {
  position: fixed; inset: 0; z-index: 10; display: grid; place-items: center; padding: 16px;
  background: #0b0c0f radial-gradient(circle at 50% 40%, #1b1e24 0%, #0b0c0f 70%);
}
.lock .auth-card { box-shadow: 0 10px 40px rgb(0 0 0 / .6); }
.lock .auth-card { text-align: center; }
.lock h2 { margin: 0; }
.lock-icon { font-size: 40px; }
.muted { color: var(--muted); margin: 0; font-size: 14px; }
.ttl-note {
  text-align: center; font-size: 12px; color: var(--muted); padding: 6px 16px;
  background: var(--panel); border-bottom: 1px solid var(--border);
}
.meta .ttl { font-variant-numeric: tabular-nums; }
.meta .ttl.soon { color: var(--error); font-weight: 600; }

/* ---------- Mã hóa đầu-cuối ---------- */
.safety-btn { margin-left: auto; white-space: nowrap; }
.safety-dialog { border: 0; padding: 0; background: transparent; }
.safety-dialog::backdrop { background: rgb(0 0 0 / .4); }
.safety-dialog h2 { margin: 0; text-align: center; }
.safety-code {
  font-family: ui-monospace, Consolas, monospace; font-size: 22px; letter-spacing: 1px; text-align: center;
  display: grid; grid-template-columns: repeat(3, auto); justify-content: center; gap: 6px 16px;
  word-spacing: 16px; padding: 8px 0;
}
.bubble.failed { font-style: italic; opacity: .8; }

/* ---------- Biểu tượng cảm xúc ---------- */
.composer { position: relative; align-items: flex-end; }
.emoji-btn { font-size: 22px; line-height: 1; padding: 8px; flex: none; }
.emoji-panel {
  position: absolute; left: 12px; bottom: calc(100% + 6px); z-index: 5;
  width: min(360px, calc(100vw - 24px)); background: var(--panel);
  border: 1px solid var(--border); border-radius: 12px; box-shadow: 0 8px 28px rgb(0 0 0 / .18);
  display: flex; flex-direction: column; overflow: hidden;
}
.emoji-tabs { display: flex; border-bottom: 1px solid var(--border); padding: 4px; gap: 2px; overflow-x: auto; }
.emoji-tab { background: transparent; font-size: 20px; padding: 6px; flex: 1; min-width: 34px; border-radius: 8px; }
.emoji-tab:hover { background: var(--hover); }
.emoji-tab.active { background: var(--active); }
.emoji-title { font-size: 12px; color: var(--muted); padding: 8px 12px 2px; }
.emoji-grid {
  display: grid; grid-template-columns: repeat(auto-fill, minmax(38px, 1fr));
  padding: 4px 8px 8px; height: 240px; overflow-y: auto; overflow-x: hidden; align-content: start;
}
.emoji-item { background: transparent; font-size: 24px; line-height: 1; padding: 6px 0; border-radius: 8px; }
.emoji-item:hover { background: var(--hover); transform: scale(1.15); }
.emoji-empty { grid-column: 1 / -1; color: var(--muted); font-size: 13px; padding: 16px; text-align: center; }
.bubble.big { background: transparent !important; font-size: 44px; line-height: 1.15; padding: 2px 4px; }

/* ---------- Điện thoại ---------- */
@media (max-width: 720px) {
  .app { grid-template-columns: 1fr; }
  .app.in-chat .sidebar { display: none; }
  .app:not(.in-chat) .chat { display: none; }
  .back { display: inline-block; }
  .msg { max-width: 85%; }
}
