:root {
  --bg: #f3f4f8; --panel: #ffffff; --line: #e3e6ee; --text: #1c2333; --muted: #6b7388;
  --brand: #3b5bdb; --brand-dark: #2f4ac0; --mine: #dfe7ff; --theirs: #ffffff; --ok: #1fa971;
}
* { box-sizing: border-box; }
[hidden] { display: none !important; }
html, body { height: 100%; margin: 0; }
body { font: 15px/1.4 system-ui, -apple-system, "Segoe UI", Roboto, Arial, sans-serif; color: var(--text); background: var(--bg); }
button, input, textarea { font: inherit; color: inherit; }
button { cursor: pointer; border: 0; border-radius: 8px; padding: 8px 12px; background: transparent; }
button.primary { background: var(--brand); color: #fff; }
button.primary:hover { background: var(--brand-dark); }
button.primary:disabled { opacity: .6; cursor: default; }
button.ghost { color: var(--muted); }
button.ghost:hover { background: var(--bg); }
input[type=text], input[type=email], input[type=password], input[type=search], textarea {
  width: 100%; padding: 10px 12px; border: 1px solid var(--line); border-radius: 8px; background: #fff; outline: none;
}
input:focus, textarea:focus { border-color: var(--brand); }

/* ---- login / register ---- */
body.auth { display: flex; align-items: center; justify-content: center; padding: 16px; }
.auth-card { width: 100%; max-width: 360px; background: var(--panel); border: 1px solid var(--line); border-radius: 14px; padding: 28px; }
.auth-card h1 { margin: 0; font-size: 22px; color: var(--brand); }
.auth-card h2 { margin: 4px 0 18px; font-size: 16px; font-weight: 500; color: var(--muted); }
.auth-card label { display: block; margin-bottom: 12px; font-size: 13px; color: var(--muted); }
.auth-card label input { margin-top: 4px; color: var(--text); }
.auth-card button { width: 100%; background: var(--brand); color: #fff; padding: 11px; margin-top: 4px; }
.auth-card .foot { margin: 16px 0 0; font-size: 14px; text-align: center; color: var(--muted); }
.auth-card a { color: var(--brand); }
.err { background: #fdecec; color: #b3261e; padding: 10px 12px; border-radius: 8px; margin-bottom: 14px; font-size: 14px; }

/* ---- layout ---- */
.app { display: flex; height: 100vh; height: 100dvh; }
.side { width: 340px; flex: none; display: flex; flex-direction: column; background: var(--panel); border-right: 1px solid var(--line); min-width: 0; }
.side-head { display: flex; align-items: center; justify-content: space-between; gap: 8px; padding: 12px 14px; border-bottom: 1px solid var(--line); }
.side-head strong { display: block; color: var(--brand); }
.side-head small { color: var(--muted); }
.side-actions { display: flex; align-items: center; gap: 4px; }
.side-actions form { margin: 0; }
.conv-list { flex: 1; overflow-y: auto; }
.hint { padding: 20px; color: var(--muted); text-align: center; }

.conv { display: flex; align-items: center; gap: 10px; width: 100%; padding: 10px 14px; border-radius: 0; text-align: left; border-bottom: 1px solid var(--line); }
.conv:hover, .conv.on { background: var(--bg); }
.conv-mid { flex: 1; min-width: 0; }
.conv-title { font-weight: 600; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.conv-prev { color: var(--muted); font-size: 13px; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.conv-right { display: flex; flex-direction: column; align-items: flex-end; gap: 4px; flex: none; }
.conv-time { color: var(--muted); font-size: 12px; }
.badge { background: var(--brand); color: #fff; font-size: 12px; min-width: 20px; padding: 1px 6px; border-radius: 10px; text-align: center; }

.avatar { position: relative; width: 40px; height: 40px; flex: none; border-radius: 50%; background: #c9d3f5; color: var(--brand-dark); display: flex; align-items: center; justify-content: center; font-weight: 600; }
.avatar.grp { background: #d6efe3; color: #17694a; }
.avatar.online::after { content: ""; position: absolute; right: 0; bottom: 0; width: 11px; height: 11px; border-radius: 50%; background: var(--ok); border: 2px solid var(--panel); }

.chat { flex: 1; min-width: 0; display: flex; flex-direction: column; }
.empty { margin: auto; color: var(--muted); padding: 20px; text-align: center; }
.pane { flex: 1; min-height: 0; display: flex; flex-direction: column; }
.chat-head { display: flex; align-items: center; gap: 8px; padding: 10px 14px; background: var(--panel); border-bottom: 1px solid var(--line); }
.chat-head .back { display: none; font-size: 20px; padding: 4px 10px; }
.title { font-weight: 600; }
.status { font-size: 12px; color: var(--muted); min-height: 16px; }
.status.live { color: var(--ok); }

.msgs { flex: 1; overflow-y: auto; padding: 14px; display: flex; flex-direction: column; gap: 6px; }
.older { align-self: center; font-size: 13px; }
.day { align-self: center; font-size: 12px; color: var(--muted); background: var(--panel); border: 1px solid var(--line); border-radius: 10px; padding: 2px 10px; margin: 6px 0; }
.msg { max-width: min(75%, 560px); padding: 7px 10px 4px; border-radius: 12px; border: 1px solid var(--line); overflow-wrap: anywhere; }
.msg.mine { align-self: flex-end; background: var(--mine); border-color: #cfd9fb; border-bottom-right-radius: 4px; }
.msg.theirs { align-self: flex-start; background: var(--theirs); border-bottom-left-radius: 4px; }
.msg .sender { font-size: 12px; font-weight: 600; color: var(--brand); margin-bottom: 2px; }
.msg .body { white-space: pre-wrap; }
.msg .meta { font-size: 11px; color: var(--muted); text-align: right; margin-top: 2px; }
.tick { margin-left: 4px; letter-spacing: -3px; padding-right: 3px; }
.tick.read { color: var(--brand); font-weight: 700; }
.img-link img { display: block; max-width: 100%; max-height: 300px; border-radius: 8px; margin-bottom: 4px; }
.file-link { display: block; color: var(--brand-dark); text-decoration: none; padding: 6px 8px; background: rgba(255,255,255,.6); border-radius: 8px; margin-bottom: 4px; }

.file-chip { display: flex; align-items: center; justify-content: space-between; gap: 8px; padding: 6px 14px; background: var(--panel); border-top: 1px solid var(--line); font-size: 13px; color: var(--muted); }
.file-chip span { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.composer { display: flex; align-items: flex-end; gap: 8px; padding: 10px 12px; background: var(--panel); border-top: 1px solid var(--line); padding-bottom: max(10px, env(safe-area-inset-bottom)); }
.composer textarea { flex: 1; resize: none; max-height: 120px; }
.attach { cursor: pointer; font-size: 20px; padding: 6px 4px; }

/* ---- modal ---- */
.modal { position: fixed; inset: 0; background: rgba(20, 25, 40, .45); display: flex; align-items: center; justify-content: center; padding: 16px; z-index: 10; }
.box { width: 100%; max-width: 400px; max-height: 85vh; display: flex; flex-direction: column; gap: 10px; background: var(--panel); border-radius: 14px; padding: 14px; }
.tabs { display: flex; gap: 6px; align-items: center; }
.tab { background: var(--bg); color: var(--muted); }
.tab.on { background: var(--brand); color: #fff; }
.tabs #closeModal { margin-left: auto; font-size: 20px; padding: 2px 10px; }
.user-list { overflow-y: auto; min-height: 120px; }
.user { display: flex; align-items: center; gap: 10px; padding: 8px 6px; border-radius: 8px; cursor: pointer; }
.user:hover { background: var(--bg); }
.user input { width: 18px; height: 18px; }

.toast { position: fixed; left: 50%; bottom: 80px; transform: translateX(-50%); background: #1c2333; color: #fff; padding: 10px 16px; border-radius: 10px; z-index: 20; max-width: 90%; }

/* ---- phone ---- */
@media (max-width: 720px) {
  .side { width: 100%; border-right: 0; }
  .chat { display: none; }
  body.chat-open .side { display: none; }
  body.chat-open .chat { display: flex; }
  .chat-head .back { display: block; }
  .msg { max-width: 86%; }
}

/* ---- v2: OTP pages ---- */
.auth-card .note { font-size: 14px; color: var(--muted); margin: 0 0 14px; }
.auth-card .ok { background: #e6f6ee; color: #17694a; padding: 10px 12px; border-radius: 8px; margin-bottom: 14px; font-size: 14px; }
.auth-card button.link { background: transparent; color: var(--brand); padding: 6px; margin-top: 8px; font-size: 14px; }

/* ---- v2: edit / delete ---- */
.msg.deleted .body { font-style: italic; color: var(--muted); }
.msg .more { padding: 0 4px; margin-left: 4px; font-size: 14px; line-height: 1; color: var(--muted); border-radius: 4px; }
.msg .more:hover { background: rgba(0, 0, 0, .08); }
.msg-menu { position: fixed; z-index: 15; background: var(--panel); border: 1px solid var(--line); border-radius: 10px; box-shadow: 0 6px 20px rgba(20, 25, 40, .15); display: flex; flex-direction: column; min-width: 120px; padding: 4px; }
.msg-menu button { text-align: left; border-radius: 6px; }
.msg-menu button:hover { background: var(--bg); }
.msg-menu #menuDelete { color: #b3261e; }
button.danger { background: #d93a31; color: #fff; }
.box.small { max-width: 320px; }
.box .row { display: flex; justify-content: flex-end; gap: 8px; }
.box p { margin: 6px 4px; }

/* ---- v2: header buttons ---- */
.head-mid { flex: 1; min-width: 0; }
.call-btn { font-size: 18px; padding: 6px 10px; }
#bellBtn { font-size: 16px; padding: 6px 8px; opacity: .45; }
#bellBtn.on { opacity: 1; background: var(--mine); }

/* ---- v2: call screen ---- */
.call { position: fixed; inset: 0; z-index: 30; background: #141a2b; color: #fff; display: flex; flex-direction: column; align-items: center; justify-content: space-between; padding: 40px 16px max(28px, env(safe-area-inset-bottom)); }
.call video { display: none; }
.call.video #remoteVideo { display: block; position: absolute; inset: 0; width: 100%; height: 100%; object-fit: cover; background: #000; }
.call.video #localVideo { display: block; position: absolute; right: 14px; top: 14px; width: 110px; height: 150px; object-fit: cover; border-radius: 10px; border: 2px solid rgba(255, 255, 255, .6); z-index: 2; transform: scaleX(-1); }
.call-info, .call-btns { position: relative; z-index: 3; text-align: center; }
.call-info { text-shadow: 0 1px 4px rgba(0, 0, 0, .6); }
.call-name { font-size: 24px; font-weight: 600; margin-bottom: 6px; }
.call-btns { display: flex; gap: 14px; }
.call-btns button { padding: 14px 22px; border-radius: 30px; font-weight: 600; }
.call-btns .accept { background: var(--ok); color: #fff; }
.call-btns .mute { background: rgba(255, 255, 255, .2); color: #fff; }

.call.video.ringing #localVideo { display: none; }
