:root {
  --bg: #f4f6f8;
  --panel: #ffffff;
  --line: #e3e7eb;
  --text: #15202b;
  --muted: #6b7785;
  --brand: #0f766e;
  --brand-2: #14b8a6;
  --mine: #0f766e;
  --mine-text: #ffffff;
  --theirs: #ffffff;
  --hover: #eef2f4;
  --danger: #dc2626;
  --ok: #16a34a;
  color-scheme: light;
}
@media (prefers-color-scheme: dark) {
  :root {
    --bg: #0b1116;
    --panel: #121a21;
    --line: #22303b;
    --text: #e6edf2;
    --muted: #8a99a6;
    --mine: #0d9488;
    --theirs: #1a252e;
    --hover: #1a252e;
    color-scheme: dark;
  }
}
* { box-sizing: border-box; }
html, body { height: 100%; margin: 0; }
body {
  font: 15px/1.4 system-ui, -apple-system, "Segoe UI", Roboto, "Noto Sans Bengali", sans-serif;
  background: var(--bg); color: var(--text);
  -webkit-tap-highlight-color: transparent;
  overflow: hidden;
}
.hidden { display: none !important; }
.muted { color: var(--muted); }
.err { color: var(--danger); min-height: 1.2em; margin: 4px 0 0; font-size: 14px; }
button { font: inherit; color: inherit; cursor: pointer; }
input, textarea {
  font: inherit; color: var(--text); background: var(--bg);
  border: 1px solid var(--line); border-radius: 10px; padding: 10px 12px; width: 100%;
  outline: none;
}
input:focus, textarea:focus { border-color: var(--brand-2); }
label { display: block; font-size: 13px; color: var(--muted); margin: 10px 0 0; }
label input { margin-top: 4px; }
.btn {
  border: 1px solid var(--line); background: var(--panel); border-radius: 10px;
  padding: 10px 16px; font-weight: 600;
}
.btn.primary { background: var(--brand); border-color: var(--brand); color: #fff; }
.btn.small { padding: 6px 12px; font-size: 13px; display: inline-block; color: var(--text); margin: 0; }
.btn:disabled { opacity: .6; }

/* auth */
.auth { height: 100%; display: grid; place-items: center; padding: 16px; }
.auth-card {
  width: 100%; max-width: 380px; background: var(--panel); border: 1px solid var(--line);
  border-radius: 18px; padding: 28px 24px;
}
.brand { display: flex; align-items: center; gap: 10px; }
.brand img { width: 42px; height: 42px; }
.brand h1 { margin: 0; font-size: 26px; }
.tabs { display: flex; gap: 6px; background: var(--bg); padding: 4px; border-radius: 12px; margin: 18px 0 6px; }
.tab { flex: 1; border: 0; background: transparent; padding: 8px; border-radius: 9px; font-weight: 600; color: var(--muted); }
.tab.active { background: var(--panel); color: var(--text); box-shadow: 0 1px 2px rgba(0,0,0,.08); }
#authForm .btn { width: 100%; margin-top: 12px; }

/* layout */
.main { display: flex; height: 100%; height: 100dvh; }
.sidebar {
  width: 340px; flex-shrink: 0; background: var(--panel); border-right: 1px solid var(--line);
  display: flex; flex-direction: column; min-height: 0;
}
.side-head {
  display: flex; align-items: center; justify-content: space-between;
  padding: 12px 14px; padding-top: max(12px, env(safe-area-inset-top));
  border-bottom: 1px solid var(--line);
}
.me { display: flex; align-items: center; gap: 10px; border: 0; background: none; font-weight: 700; font-size: 17px; padding: 0; }
.conn { width: 10px; height: 10px; border-radius: 50%; background: var(--muted); }
.conn.on { background: var(--ok); }
.search { padding: 10px 12px; }
.list { overflow-y: auto; flex: 1; min-height: 0; }
#results { flex: 0 1 auto; max-height: 50%; border-bottom: 1px solid var(--line); }
.item {
  display: flex; gap: 12px; align-items: center; padding: 10px 14px; cursor: pointer;
  border: 0; background: none; width: 100%; text-align: left;
}
.item:hover, .item.active { background: var(--hover); }
.item .meta { flex: 1; min-width: 0; }
.item .top { display: flex; justify-content: space-between; gap: 8px; }
.item .name { font-weight: 600; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.item .time { font-size: 12px; color: var(--muted); flex-shrink: 0; }
.item .sub { display: flex; justify-content: space-between; gap: 8px; color: var(--muted); font-size: 14px; }
.item .sub span { white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.badge {
  background: var(--brand); color: #fff; border-radius: 10px; font-size: 12px; font-weight: 700;
  min-width: 20px; height: 20px; padding: 0 6px; display: inline-grid; place-items: center; flex-shrink: 0;
}
.empty { padding: 30px 20px; text-align: center; color: var(--muted); }

.avatar {
  width: 42px; height: 42px; border-radius: 50%; flex-shrink: 0; position: relative;
  background: var(--brand-2) center/cover no-repeat; color: #fff; font-weight: 700;
  display: inline-grid; place-items: center; font-size: 16px; text-transform: uppercase;
}
.avatar.big { width: 96px; height: 96px; font-size: 36px; }
.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 */
.chat { flex: 1; display: flex; flex-direction: column; min-width: 0; min-height: 0; }
.no-chat { flex: 1; display: grid; place-content: center; text-align: center; color: var(--muted); }
.no-chat img { width: 80px; margin: 0 auto; opacity: .5; }
.chat-view { flex: 1; display: flex; flex-direction: column; min-height: 0; }
.chat-head {
  display: flex; align-items: center; gap: 10px; padding: 8px 12px;
  padding-top: max(8px, env(safe-area-inset-top));
  background: var(--panel); border-bottom: 1px solid var(--line);
}
.chat-head .avatar { width: 38px; height: 38px; font-size: 15px; }
.peer { flex: 1; min-width: 0; display: flex; flex-direction: column; }
.peer b { white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.peer small { color: var(--muted); }
.icon-btn {
  width: 40px; height: 40px; border-radius: 50%; border: 0; background: transparent;
  display: inline-grid; place-items: center; flex-shrink: 0; color: var(--brand); margin: 0;
  cursor: pointer;
}
.icon-btn:hover { background: var(--hover); }
.icon-btn svg, .round svg {
  width: 22px; height: 22px; fill: none; stroke: currentColor; stroke-width: 2;
  stroke-linecap: round; stroke-linejoin: round;
}
.back { display: none; }
.messages { flex: 1; overflow-y: auto; padding: 14px 12px; display: flex; flex-direction: column; gap: 2px; }
.day { align-self: center; font-size: 12px; color: var(--muted); background: var(--panel); border-radius: 8px; padding: 3px 10px; margin: 10px 0; }
.msg { max-width: min(75%, 520px); display: flex; flex-direction: column; margin-top: 2px; }
.msg.mine { align-self: flex-end; }
.bubble {
  padding: 7px 11px; border-radius: 16px; background: var(--theirs); word-wrap: break-word;
  white-space: pre-wrap; box-shadow: 0 1px 1px rgba(0,0,0,.05); overflow-wrap: anywhere;
}
.mine .bubble { background: var(--mine); color: var(--mine-text); border-bottom-right-radius: 5px; }
.theirs .bubble { border-bottom-left-radius: 5px; }
.bubble a { color: inherit; }
.bubble img { display: block; max-width: 100%; max-height: 320px; border-radius: 10px; cursor: zoom-in; }
.bubble.media { padding: 4px; }
.bubble audio { max-width: 240px; height: 40px; display: block; }
.file { display: flex; gap: 10px; align-items: center; text-decoration: none; }
.file svg { width: 28px; height: 28px; fill: none; stroke: currentColor; stroke-width: 1.6; flex-shrink: 0; }
.stamp { font-size: 11px; color: var(--muted); margin: 1px 6px 4px; align-self: flex-end; }
.theirs .stamp { align-self: flex-start; }
.pending .bubble { opacity: .6; }
.callmsg { align-self: center; display: flex; gap: 8px; align-items: center; font-size: 13px; color: var(--muted);
  background: var(--panel); border-radius: 10px; padding: 6px 12px; margin: 6px 0; }
.callmsg.missed { color: var(--danger); }
.typing { padding: 0 16px 4px; font-size: 13px; color: var(--muted); }
.composer {
  display: flex; align-items: flex-end; gap: 4px; padding: 8px;
  padding-bottom: max(8px, env(safe-area-inset-bottom));
  background: var(--panel); border-top: 1px solid var(--line);
}
.composer textarea { resize: none; max-height: 140px; border-radius: 20px; padding: 9px 14px; line-height: 1.35; }
.send { background: var(--brand); color: #fff; }
.send:hover { background: var(--brand); }
#rec.recording { background: var(--danger); color: #fff; animation: pulse 1s infinite; }
@keyframes pulse { 50% { opacity: .6; } }

/* dialog */
.dlg { border: 1px solid var(--line); border-radius: 16px; padding: 22px; background: var(--panel); color: var(--text); width: min(360px, calc(100% - 32px)); }
.dlg::backdrop { background: rgba(0,0,0,.4); }
.dlg h3 { margin: 0 0 10px; }
.prof-avatar { display: flex; align-items: center; gap: 16px; }
.row { display: flex; gap: 8px; justify-content: flex-end; margin-top: 16px; flex-wrap: wrap; }

/* call */
.call {
  position: fixed; inset: 0; z-index: 50; background: #0b1116; color: #fff;
  display: flex; flex-direction: column; align-items: center; justify-content: space-between;
  padding: max(40px, env(safe-area-inset-top)) 16px max(32px, env(safe-area-inset-bottom));
}
.call video { background: #000; }
#remoteVideo { position: absolute; inset: 0; width: 100%; height: 100%; object-fit: cover; }
#localVideo {
  position: absolute; right: 14px; top: max(14px, env(safe-area-inset-top)); width: 110px; height: 160px;
  object-fit: cover; border-radius: 12px; z-index: 2; transform: scaleX(-1); border: 2px solid rgba(255,255,255,.25);
}
#localVideo.back-cam { transform: none; }
.call-info { position: relative; z-index: 1; text-align: center; margin-top: 8vh; }
.call-info h2 { margin: 14px 0 4px; }
.call-info p { margin: 0; opacity: .8; }
.call.has-video .call-info { margin-top: 0; text-shadow: 0 1px 4px #000; }
.call.has-video .call-info .avatar { display: none; }
.call-controls { position: relative; z-index: 2; display: flex; gap: 18px; justify-content: center; }
.round {
  width: 60px; height: 60px; border-radius: 50%; border: 0; background: rgba(255,255,255,.18);
  color: #fff; display: grid; place-items: center; backdrop-filter: blur(6px);
}
.round svg { width: 26px; height: 26px; }
.round.off { background: #fff; color: #111; }
.round.accept { background: var(--ok); animation: pulse 1.2s infinite; }
.round.decline { background: var(--danger); }

.toast {
  position: fixed; left: 50%; bottom: 90px; transform: translateX(-50%); z-index: 60;
  background: #111; color: #fff; padding: 10px 16px; border-radius: 10px; font-size: 14px; max-width: 90%;
}
.viewer { position: fixed; inset: 0; z-index: 70; background: rgba(0,0,0,.92); display: grid; place-items: center; }
.viewer img { max-width: 100%; max-height: 100%; }

/* mobile */
@media (max-width: 760px) {
  .sidebar { width: 100%; border-right: 0; }
  .chat { display: none; }
  .main.in-chat .sidebar { display: none; }
  .main.in-chat .chat { display: flex; }
  .back { display: inline-grid; }
  .msg { max-width: 82%; }
}

#remoteVideo.screen { object-fit: contain; }
#localVideo.sharing { transform: none; }
