:root {
  --bg: #eef1f4; --panel: #ffffff; --line: #dfe3e8; --text: #1c2430; --muted: #6b7684;
  --accent: #0b6bcb; --accent-ink: #ffffff; --mine: #d9ebff; --theirs: #ffffff; --danger: #c62828; --ok: #22a559;
  --c0: #0f766e; --c1: #b45309; --c2: #6d28d9; --c3: #be185d;
}
@media (prefers-color-scheme: dark) {
  :root {
    --bg: #14171b; --panel: #1d2126; --line: #2e343b; --text: #e7eaee; --muted: #98a2ae;
    --accent: #4c9cf0; --accent-ink: #0b1420; --mine: #1f3d5c; --theirs: #262b31; --danger: #ef6b6b; --ok: #3ccf7a;
    --c0: #4fd1c5; --c1: #f0a94b; --c2: #b79cf5; --c3: #f284b6;
  }
}
* { box-sizing: border-box; }
html, body { height: 100%; margin: 0; }
body { font: 16px/1.45 system-ui, -apple-system, "Segoe UI", Roboto, sans-serif; background: var(--bg); color: var(--text); }
[hidden] { display: none !important; }
#app { height: 100vh; height: 100dvh; max-width: 820px; margin: 0 auto; display: flex; flex-direction: column; }
button, input, textarea { font: inherit; color: inherit; }
a { color: var(--accent); }

/* Thẻ đăng nhập, thiết lập */
.center { flex: 1; display: flex; align-items: center; justify-content: center; padding: 20px; overflow-y: auto; }
.card { width: 100%; max-width: 420px; background: var(--panel); border: 1px solid var(--line); border-radius: 16px; padding: 24px; }
.logo { width: 52px; height: 52px; display: block; margin: 0 auto 10px; }
.card h1 { font-size: 22px; margin: 0 0 6px; text-align: center; }
.card h2 { font-size: 18px; margin: 0; }
.sub { color: var(--muted); font-size: 14px; margin: 0 0 14px; text-align: center; }
.form { display: flex; flex-direction: column; gap: 10px; }
.pair { display: grid; grid-template-columns: 1fr 1fr; gap: 8px; }
input[type=text], input[type=password] { width: 100%; min-width: 0; padding: 11px 12px; border: 1px solid var(--line); border-radius: 10px; background: var(--bg); outline: none; }
input:focus, textarea:focus { border-color: var(--accent); }
.err { color: var(--danger); font-size: 14px; min-height: 0; }
.err:empty { display: none; }
.btn { border: 1px solid var(--line); background: var(--panel); border-radius: 10px; padding: 10px 16px; cursor: pointer; text-decoration: none; color: var(--text); font-weight: 600; }
.btn.primary { background: var(--accent); border-color: var(--accent); color: var(--accent-ink); }
.btn.small { padding: 6px 10px; font-size: 13px; font-weight: 500; }
.btn:disabled { opacity: .6; cursor: default; }

/* Khung chat */
.chat { flex: 1; min-height: 0; display: flex; flex-direction: column; background: var(--bg); }
.head { display: flex; align-items: center; gap: 10px; padding: 10px 12px; background: var(--panel); border-bottom: 1px solid var(--line); }
.hlogo { width: 34px; height: 34px; flex: none; }
.htext { flex: 1; min-width: 0; }
.htitle { font-weight: 700; line-height: 1.2; }
.members { display: flex; flex-wrap: wrap; gap: 2px 12px; font-size: 13px; color: var(--muted); }
.member { display: inline-flex; align-items: center; gap: 5px; white-space: nowrap; }
.dot { width: 8px; height: 8px; border-radius: 50%; background: var(--line); display: inline-block; }
.member.on { color: var(--text); }
.member.on .dot { background: var(--ok); }
.hmenu { position: relative; flex: none; }
.icon { border: 0; background: transparent; width: 40px; height: 40px; border-radius: 10px; font-size: 20px; cursor: pointer; flex: none; }
.icon:hover { background: var(--bg); }
.menu { position: absolute; right: 0; top: 44px; background: var(--panel); border: 1px solid var(--line); border-radius: 12px; padding: 6px; min-width: 180px; z-index: 5; box-shadow: 0 8px 24px rgba(0, 0, 0, .14); display: flex; flex-direction: column; }
.mitem { border: 0; background: transparent; text-align: left; padding: 10px 12px; border-radius: 8px; cursor: pointer; }
.mitem:hover { background: var(--bg); }

.list { flex: 1; min-height: 0; overflow-y: auto; padding: 12px 12px 6px; display: flex; flex-direction: column; gap: 4px; overscroll-behavior: contain; }
.list:empty::before { content: "Chưa có tin nhắn nào. Gửi lời chào đầu tiên nhé."; color: var(--muted); margin: auto; text-align: center; }
.day { align-self: center; font-size: 12px; color: var(--muted); background: var(--panel); border: 1px solid var(--line); border-radius: 999px; padding: 2px 12px; margin: 10px 0 6px; }
.row { display: flex; align-items: center; gap: 4px; max-width: 100%; }
.row.mine { justify-content: flex-end; }
.bubble { max-width: min(78%, 520px); padding: 7px 11px 5px; border-radius: 16px; background: var(--theirs); border: 1px solid var(--line); min-width: 0; }
.mine .bubble { background: var(--mine); border-color: transparent; border-bottom-right-radius: 6px; }
.theirs .bubble { border-bottom-left-radius: 6px; }
.who { font-size: 13px; font-weight: 700; margin-bottom: 1px; }
.c0 { color: var(--c0); } .c1 { color: var(--c1); } .c2 { color: var(--c2); } .c3 { color: var(--c3); }
.text { white-space: pre-wrap; overflow-wrap: anywhere; }
.time { font-size: 11px; color: var(--muted); text-align: right; margin-top: 1px; }
.pic { display: block; height: auto; max-width: 100%; border-radius: 10px; margin: 2px 0 4px; cursor: zoom-in; background: var(--line); }
.pic.nosize { width: auto; max-height: 340px; }
.del { border: 0; background: transparent; color: var(--muted); width: 28px; height: 28px; border-radius: 50%; cursor: pointer; font-size: 18px; line-height: 1; opacity: 0; flex: none; }
.row:hover .del, .del:focus { opacity: 1; }
.del:hover { background: var(--line); color: var(--danger); }
@media (pointer: coarse) { .del { opacity: .55; } }

.status { font-size: 13px; color: var(--muted); padding: 4px 14px; }
.strip { display: flex; gap: 8px; padding: 8px 12px 0; overflow-x: auto; }
.thumb { position: relative; flex: none; }
.thumb img { width: 64px; height: 64px; object-fit: cover; border-radius: 10px; display: block; border: 1px solid var(--line); }
.x { border: 0; background: var(--text); color: var(--panel); width: 22px; height: 22px; border-radius: 50%; cursor: pointer; line-height: 1; font-size: 15px; padding: 0; }
.thumb .x { position: absolute; top: -6px; right: -6px; }
.composer { display: flex; align-items: flex-end; gap: 6px; padding: 8px 10px calc(8px + env(safe-area-inset-bottom)); background: var(--panel); border-top: 1px solid var(--line); margin-top: 6px; }
.composer textarea { flex: 1; min-width: 0; resize: none; border: 1px solid var(--line); border-radius: 20px; padding: 9px 14px; background: var(--bg); outline: none; max-height: 150px; line-height: 1.4; }
.send { border-radius: 20px; flex: none; }

/* Lớp phủ */
.overlay, .viewer { position: fixed; inset: 0; z-index: 20; display: flex; align-items: center; justify-content: center; padding: 16px; }
.overlay { background: rgba(0, 0, 0, .45); }
.modal { max-height: 90vh; overflow-y: auto; }
.mhead { display: flex; align-items: center; justify-content: space-between; margin-bottom: 14px; }
.mhead .x { width: 28px; height: 28px; font-size: 18px; }
.mbody { display: flex; flex-direction: column; gap: 10px; }
.mbody .sub { text-align: left; margin: 4px 0 0; }
.urow { display: flex; align-items: center; justify-content: space-between; gap: 10px; padding: 6px 0; border-bottom: 1px solid var(--line); }
.viewer { background: rgba(0, 0, 0, .9); flex-direction: column; gap: 12px; }
.viewer img { max-width: 100%; max-height: calc(100vh - 100px); object-fit: contain; cursor: zoom-out; }
.vbar { display: flex; gap: 10px; }
.toast { position: fixed; left: 50%; bottom: 84px; transform: translateX(-50%); background: var(--text); color: var(--panel); padding: 10px 16px; border-radius: 10px; z-index: 30; max-width: 90vw; font-size: 14px; }

@media (max-width: 480px) {
  .pair { grid-template-columns: 1fr; gap: 6px; padding-bottom: 6px; }
  .bubble { max-width: 86%; }
}
