:root {
  --bg: #0f172a; --panel: #111c33; --panel2: #16233f; --line: #24324f;
  --text: #e6ecf7; --muted: #8fa0bf; --accent: #22c1a3; --accent2: #1a9e86;
  --mine: #1f6f63; --theirs: #1c2a48; --danger: #e5484d; --radius: 14px;
  --safe-b: env(safe-area-inset-bottom, 0px); --safe-t: env(safe-area-inset-top, 0px);
}
* { box-sizing: border-box; }
html, body { margin: 0; height: 100%; background: var(--bg); color: var(--text);
  font: 15px/1.4 -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Helvetica, Arial, sans-serif; }
body { overflow: hidden; height: 100dvh; }
button { font: inherit; color: inherit; cursor: pointer; }
input, select, textarea { font: inherit; color: var(--text); background: var(--panel2); border: 1px solid var(--line);
  border-radius: 10px; padding: 11px 12px; width: 100%; font-size: 16px; }
input:focus, select:focus, textarea:focus { outline: 2px solid var(--accent); border-color: transparent; }
svg { width: 22px; height: 22px; fill: currentColor; }
.hidden { display: none !important; }
.muted { color: var(--muted); } .small { font-size: 12.5px; } .strong { font-weight: 600; } .grow { flex: 1; min-width: 0; }
.pad { padding: 16px; } .error { color: #ff8a8f; min-height: 1.2em; }

.btn { border: 1px solid var(--line); background: var(--panel2); padding: 11px 14px; border-radius: 10px; width: 100%; }
.btn.primary { background: var(--accent); border-color: var(--accent); color: #04221c; font-weight: 600; }
.btn.ghost { background: transparent; border-color: transparent; color: var(--muted); }
.icon-btn { background: transparent; border: 0; padding: 8px; border-radius: 50%; display: grid; place-items: center; }
.icon-btn:hover { background: var(--panel2); }
.icon-btn.call { color: var(--accent); }
.icon-btn:disabled { opacity: .35; cursor: default; }
.link { background: none; border: 0; padding: 0; color: inherit; opacity: .8; text-decoration: underline; font-size: 12px; }

/* Auth */
#auth { min-height: 100dvh; display: grid; place-items: center; padding: 20px; overflow: auto; }
.auth-card { width: 100%; max-width: 380px; background: var(--panel); border: 1px solid var(--line); border-radius: 18px; padding: 24px; }
.brand { display: flex; gap: 10px; align-items: center; } .brand img { width: 40px; height: 40px; } .brand h1 { margin: 0; font-size: 24px; }
.tabs { display: flex; gap: 6px; margin: 18px 0 12px; background: var(--panel2); padding: 4px; border-radius: 10px; }
.tab { flex: 1; border: 0; background: transparent; padding: 9px; border-radius: 8px; color: var(--muted); }
.tab.active { background: var(--panel); color: var(--text); }
.form { display: flex; flex-direction: column; gap: 12px; }
.form label span { display: block; font-size: 13px; color: var(--muted); margin-bottom: 5px; }

/* Layout */
#main { display: flex; height: 100dvh; }
#sidebar { width: 320px; border-right: 1px solid var(--line); background: var(--panel); display: flex; flex-direction: column; padding-top: var(--safe-t); }
#chatPane { flex: 1; display: flex; flex-direction: column; min-width: 0; padding-top: var(--safe-t); }
.bar { display: flex; align-items: center; gap: 10px; padding: 12px 14px; border-bottom: 1px solid var(--line); }
.me { display: flex; gap: 10px; align-items: center; flex: 1; min-width: 0; }
.section-title { padding: 14px 16px 6px; font-size: 12px; text-transform: uppercase; letter-spacing: .08em; color: var(--muted); }
#contactList { list-style: none; margin: 0; padding: 0 8px; overflow-y: auto; flex: 1; }
.contact { display: flex; align-items: center; gap: 10px; padding: 10px; border-radius: 12px; cursor: pointer; }
.contact:hover, .contact.active { background: var(--panel2); }
.contact .status { white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.avatar { width: 42px; height: 42px; border-radius: 50%; display: grid; place-items: center; font-weight: 700; color: #fff; position: relative; flex: none; background: #334; }
.avatar.big { width: 84px; height: 84px; font-size: 28px; }
.presence { position: absolute; right: -1px; bottom: -1px; width: 12px; height: 12px; border-radius: 50%; background: #64748b; border: 2px solid var(--panel); }
.presence.on { background: #22c55e; }
.badge { background: var(--accent); color: #04221c; font-size: 12px; font-weight: 700; border-radius: 999px; padding: 2px 8px; }
.install-hint { margin: 10px; padding: 12px; border: 1px dashed var(--line); border-radius: 12px; font-size: 13px; color: var(--muted); }

.placeholder { margin: auto; padding: 30px; text-align: center; }
.chat-header .back { display: none; }
.messages { flex: 1; overflow-y: auto; padding: 16px; display: flex; flex-direction: column; gap: 8px; overscroll-behavior: contain; }
.msg { max-width: min(78%, 560px); padding: 9px 12px 6px; border-radius: var(--radius); word-wrap: break-word; white-space: pre-wrap; }
.msg.mine { align-self: flex-end; background: var(--mine); border-bottom-right-radius: 4px; }
.msg.theirs { align-self: flex-start; background: var(--theirs); border-bottom-left-radius: 4px; }
.msg.pending { opacity: .65; }
.msg .alt { font-size: 13px; opacity: .8; border-top: 1px solid rgba(255,255,255,.15); margin-top: 6px; padding-top: 5px; }
.msg .meta { display: flex; gap: 10px; justify-content: flex-end; align-items: center; font-size: 11.5px; opacity: .7; margin-top: 3px; }
.typing { min-height: 18px; padding: 0 18px; }
.composer { display: flex; gap: 8px; padding: 10px 12px calc(10px + var(--safe-b)); border-top: 1px solid var(--line); align-items: flex-end; }
.composer textarea { resize: none; max-height: 140px; border-radius: 20px; padding: 10px 14px; }
.composer input { border-radius: 20px; }
.send-btn { flex: none; width: 44px; height: 44px; border-radius: 50%; border: 0; background: var(--accent); color: #04221c; display: grid; place-items: center; }

/* Call */
#callView { position: fixed; inset: 0; background: #000; z-index: 20; }
#remoteVideo { position: absolute; inset: 0; width: 100%; height: 100%; object-fit: cover; background: #050913; }
#localVideo { position: absolute; top: calc(14px + var(--safe-t)); right: 14px; width: 28vw; max-width: 200px; aspect-ratio: 3/4; object-fit: cover;
  border-radius: 12px; border: 2px solid rgba(255,255,255,.25); transform: scaleX(-1); background: #111; z-index: 2; }
#localVideo.rear { transform: none; }
.call-status { position: absolute; top: 40%; left: 0; right: 0; text-align: center; font-size: 20px; text-shadow: 0 2px 8px #000; z-index: 2; }
.captions { position: absolute; left: 50%; transform: translateX(-50%); bottom: calc(110px + var(--safe-b)); width: min(92%, 720px);
  display: flex; flex-direction: column; gap: 6px; align-items: center; z-index: 3; pointer-events: none; }
.caption { background: rgba(0,0,0,.72); padding: 8px 14px; border-radius: 10px; font-size: 18px; text-align: center; animation: fadein .2s; }
@keyframes fadein { from { opacity: 0; transform: translateY(6px); } }
.call-controls { position: absolute; bottom: calc(20px + var(--safe-b)); left: 0; right: 0; display: flex; justify-content: center; gap: 14px; z-index: 4; }
.round { width: 56px; height: 56px; border-radius: 50%; border: 0; background: rgba(255,255,255,.16); color: #fff; display: grid; place-items: center;
  position: relative; backdrop-filter: blur(8px); -webkit-backdrop-filter: blur(8px); }
.round.off { background: #fff; color: #111; }
.round.danger { background: var(--danger); } .round.accept { background: #22c55e; }
.round .dot { position: absolute; top: 8px; right: 8px; width: 10px; height: 10px; border-radius: 50%; background: var(--accent); }
.call-chat { position: absolute; z-index: 5; right: 14px; top: calc(14px + var(--safe-t) + 0px); bottom: calc(96px + var(--safe-b)); width: 360px;
  background: rgba(15,23,42,.88); backdrop-filter: blur(10px); -webkit-backdrop-filter: blur(10px); border: 1px solid var(--line);
  border-radius: 16px; display: flex; flex-direction: column; overflow: hidden; }
.call-chat-head { display: flex; justify-content: space-between; align-items: center; padding: 8px 8px 8px 14px; border-bottom: 1px solid var(--line); font-weight: 600; }
.call-chat .composer { padding-bottom: 10px; }
#callView:has(.call-chat:not(.hidden)) #localVideo { right: 388px; }

/* Modals */
.modal { position: fixed; inset: 0; background: rgba(0,0,0,.6); display: grid; place-items: center; z-index: 30; padding: 20px; }
.modal-card { background: var(--panel); border: 1px solid var(--line); border-radius: 18px; padding: 24px; width: 100%; max-width: 360px;
  display: flex; flex-direction: column; align-items: center; gap: 10px; text-align: center; }
.modal-card.form { align-items: stretch; text-align: left; } .modal-card h2 { margin: 0 0 4px; }
.modal-card .row { display: flex; gap: 40px; margin-top: 14px; }
#incoming .avatar { animation: pulse 1.4s infinite; }
@keyframes pulse { 50% { box-shadow: 0 0 0 14px rgba(34,193,163,.15); } }
.toast { position: fixed; left: 50%; transform: translateX(-50%); bottom: calc(90px + var(--safe-b)); background: #000c; padding: 10px 16px;
  border-radius: 10px; z-index: 40; max-width: 90%; text-align: center; }

/* Phones */
@media (max-width: 760px) {
  #sidebar { width: 100%; border-right: 0; }
  #chatPane { display: none; }
  #main.chat-open #sidebar { display: none; }
  #main.chat-open #chatPane { display: flex; }
  .chat-header .back { display: grid; }
  .msg { max-width: 85%; }
  .call-chat { left: 0; right: 0; width: auto; top: auto; bottom: calc(92px + var(--safe-b)); height: 46%; border-radius: 16px 16px 0 0; }
  #callView:has(.call-chat:not(.hidden)) #localVideo { right: 14px; }
  #callView:has(.call-chat:not(.hidden)) .captions { display: none; }
  .round { width: 52px; height: 52px; }
}

/* Emoji picker */
.composer { position: relative; }
.emoji-btn { flex: none; width: 44px; height: 44px; border-radius: 50%; border: 1px solid var(--line); background: var(--panel2);
  font-size: 22px; line-height: 1; display: grid; place-items: center; cursor: pointer; padding: 0; }
.emoji-btn.on { border-color: var(--accent); }
.emoji-panel { position: absolute; left: 8px; right: 8px; bottom: calc(100% + 6px); max-width: 380px; z-index: 25;
  background: var(--panel); border: 1px solid var(--line); border-radius: var(--radius); box-shadow: 0 10px 30px rgba(0,0,0,.45);
  display: flex; flex-direction: column; overflow: hidden; }
.emoji-panel.hidden { display: none; }
.emoji-tabs { display: flex; gap: 2px; padding: 6px; border-bottom: 1px solid var(--line); overflow-x: auto; scrollbar-width: none; }
.emoji-tabs::-webkit-scrollbar { display: none; }
.emoji-tab { flex: none; width: 36px; height: 34px; border: 0; border-radius: 8px; background: transparent; font-size: 19px; cursor: pointer; padding: 0; }
.emoji-tab.on { background: var(--panel2); box-shadow: inset 0 -2px 0 var(--accent); }
.emoji-grid { display: grid; grid-template-columns: repeat(auto-fill, minmax(40px, 1fr)); gap: 2px; padding: 6px;
  max-height: 240px; overflow-y: auto; overscroll-behavior: contain; }
.emoji { border: 0; background: transparent; font-size: 26px; line-height: 1; height: 42px; border-radius: 8px; cursor: pointer; padding: 0; }
.emoji:hover, .emoji:focus-visible { background: var(--panel2); }
.emoji-empty { grid-column: 1 / -1; text-align: center; padding: 24px; opacity: .5; font-size: 28px; }
.call-chat .emoji-panel { max-height: 60vh; }
.emoji-grid { grid-auto-flow: dense; }
.emoji.combo { grid-column: span 2; height: 58px; font-size: 22px; display: flex; flex-direction: column; align-items: center;
  justify-content: center; gap: 3px; white-space: nowrap; }
.emoji-cap { font-size: 10px; line-height: 1.1; color: var(--muted); max-width: 100%; overflow: hidden; text-overflow: ellipsis; padding: 0 2px;
  font-family: system-ui, -apple-system, sans-serif; }
.emoji.combo { height: auto; min-height: 58px; padding: 6px 2px; white-space: normal; }
.emoji-cap { white-space: normal; text-align: center; display: -webkit-box; -webkit-line-clamp: 2; -webkit-box-orient: vertical; }
