:root {
  --bg: #0b0f14;
  --panel: #121821;
  --panel-2: #0e141c;
  --line: #1f2a37;
  --text: #e7eef7;
  --muted: #8ba0b6;
  --accent: #3b82f6;
  --accent-2: #2563eb;
  --mine: #1d4ed8;
  --theirs: #1b2532;
  --danger: #ef4444;
  --radius: 14px;
  --shadow: 0 8px 30px rgba(0,0,0,.35);
}
* { box-sizing: border-box; }
html, body { height: 100%; margin: 0; }
body {
  font: 15px/1.45 -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Helvetica, Arial, sans-serif;
  background: var(--bg);
  color: var(--text);
  -webkit-font-smoothing: antialiased;
  overflow: hidden;
}
.view { height: 100dvh; }
.hidden { display: none !important; }
button { font: inherit; cursor: pointer; }
a { color: #7fb2ff; }

/* ===================== LOGIN ===================== */
#login {
  display: grid;
  place-items: center;
  padding: 20px;
  background: radial-gradient(1200px 600px at 50% -10%, #12233b 0%, var(--bg) 60%);
}
.login-card {
  width: 100%;
  max-width: 360px;
  background: var(--panel);
  border: 1px solid var(--line);
  border-radius: 18px;
  padding: 28px 24px;
  box-shadow: var(--shadow);
  display: grid;
  gap: 14px;
}
.brand { display: flex; align-items: center; gap: 12px; margin-bottom: 6px; }
.brand h1 { margin: 0; font-size: 22px; letter-spacing: .5px; }
.brand .tag { margin: 0; color: var(--muted); font-size: 12px; }
.login-card label { display: grid; gap: 6px; font-size: 13px; color: var(--muted); }
.login-card input {
  padding: 11px 12px;
  background: var(--panel-2);
  border: 1px solid var(--line);
  border-radius: 10px;
  color: var(--text);
  font-size: 15px;
}
.login-card input:focus { outline: 2px solid var(--accent); border-color: transparent; }
.login-card button[type=submit] {
  margin-top: 4px;
  padding: 12px;
  border: 0;
  border-radius: 10px;
  background: var(--accent);
  color: white;
  font-weight: 600;
}
.login-card button[disabled] { opacity: .6; }
.status { min-height: 18px; margin: 0; font-size: 13px; color: var(--muted); }
.status.error { color: var(--danger); }
.fine { margin: 0; font-size: 11px; color: var(--muted); opacity: .8; text-align: center; }

/* ===================== APP LAYOUT ===================== */
#app { display: grid; grid-template-columns: 320px 1fr; height: 100dvh; }
#sidebar {
  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: 14px 16px; border-bottom: 1px solid var(--line);
}
.me { display: flex; align-items: center; gap: 10px; }
.me strong { display: block; }
.presence { color: var(--muted); font-size: 12px; }
.avatar {
  width: 38px; height: 38px; border-radius: 50%;
  display: grid; place-items: center;
  font-size: 13px; font-weight: 700; color: white; flex: 0 0 auto;
  background: #334; letter-spacing: .3px;
}
.icon-btn {
  background: transparent; border: 0; color: var(--muted);
  font-size: 18px; padding: 6px 8px; border-radius: 8px;
}
.icon-btn:hover { color: var(--text); background: var(--panel-2); }

.contacts { overflow-y: auto; flex: 1; padding: 8px; }
.contact {
  display: flex; align-items: center; gap: 12px; width: 100%;
  padding: 10px; margin: 2px 0; background: transparent;
  border: 0; border-radius: 12px; color: var(--text); text-align: left;
}
.contact:hover { background: var(--panel-2); }
.contact.active { background: var(--panel-2); outline: 1px solid var(--line); }
.c-meta { flex: 1; min-width: 0; }
.c-meta strong { display: block; }
.c-sub { font-size: 12px; color: var(--muted); }
.badge {
  background: var(--accent); color: white; font-size: 11px; font-weight: 700;
  min-width: 20px; height: 20px; border-radius: 10px; padding: 0 6px;
  display: grid; place-items: center;
}
.hint { color: var(--muted); font-size: 13px; padding: 12px; }
.notify-btn {
  margin: 8px; padding: 10px; border-radius: 10px; border: 1px solid var(--line);
  background: var(--panel-2); color: var(--text);
}

/* ===================== CHAT ===================== */
#chat { display: flex; flex-direction: column; min-height: 0; background: var(--panel-2); }
.chat-head {
  display: flex; align-items: center; gap: 12px;
  padding: 12px 16px; border-bottom: 1px solid var(--line); background: var(--panel);
}
.chat-head .back { display: none; font-size: 26px; line-height: 1; }
.peer-meta strong { display: block; }
#chat.empty .chat-head, #chat.empty .composer { visibility: hidden; }

.messages {
  flex: 1; overflow-y: auto; padding: 18px;
  display: flex; flex-direction: column; gap: 4px;
}
.empty-state { margin: auto; color: var(--muted); text-align: center; }

.msg { display: flex; flex-direction: column; max-width: min(78%, 560px); }
.msg.mine { align-self: flex-end; align-items: flex-end; }
.msg.theirs { align-self: flex-start; align-items: flex-start; }
.msg.in { animation: pop .18s ease; }
@keyframes pop { from { transform: translateY(6px); opacity: 0; } }

.bubble {
  position: relative;
  padding: 8px 12px 6px;
  border-radius: 16px;
  background: var(--theirs);
  word-wrap: break-word; overflow-wrap: anywhere;
}
.msg.mine .bubble { background: var(--mine); }
.msg.mine .bubble { border-bottom-right-radius: 5px; }
.msg.theirs .bubble { border-bottom-left-radius: 5px; }
.bubble .text { white-space: pre-wrap; }
.bubble .time { font-size: 10px; color: rgba(255,255,255,.55); margin-left: 8px; float: right; margin-top: 3px; }
.caption { margin-top: 6px; white-space: pre-wrap; }

.media { max-width: 320px; border-radius: 10px; overflow: hidden; }
.media.loading { padding: 24px; color: var(--muted); font-size: 12px; }
.media img, .media video, .link-embed {
  max-width: 320px; max-height: 320px; border-radius: 10px; display: block; cursor: pointer;
}
.link-embed { margin-top: 8px; }

.react-btn {
  position: absolute; top: -10px;
  width: 24px; height: 24px; border-radius: 50%;
  border: 1px solid var(--line); background: var(--panel);
  color: var(--muted); font-size: 12px; line-height: 1;
  display: grid; place-items: center; opacity: 0; transition: opacity .12s;
}
.msg.mine .react-btn { left: -12px; }
.msg.theirs .react-btn { right: -12px; }
.bubble:hover .react-btn { opacity: 1; }
@media (hover: none) { .react-btn { opacity: .6; } }

.reactions { display: flex; flex-wrap: wrap; gap: 4px; margin-top: 4px; }
.chip {
  background: var(--panel); border: 1px solid var(--line); color: var(--text);
  border-radius: 12px; padding: 1px 8px; font-size: 12px;
}
.chip.own { border-color: var(--accent); background: #14233f; }

.day {
  align-self: center; color: var(--muted); font-size: 12px;
  padding: 4px 10px; margin: 8px 0;
}
.day.sys { background: var(--panel); border: 1px solid var(--line); border-radius: 20px; }

/* ===================== COMPOSER ===================== */
.composer {
  display: flex; align-items: center; gap: 6px;
  padding: 10px 12px; border-top: 1px solid var(--line); background: var(--panel);
  padding-bottom: max(10px, env(safe-area-inset-bottom));
}
.composer input#msgInput {
  flex: 1; padding: 11px 14px; border-radius: 20px;
  border: 1px solid var(--line); background: var(--panel-2); color: var(--text); font-size: 15px;
}
.composer input#msgInput:focus { outline: 2px solid var(--accent); border-color: transparent; }
.send-btn {
  width: 40px; height: 40px; border-radius: 50%; border: 0;
  background: var(--accent); color: white; font-size: 16px; flex: 0 0 auto;
}
.send-btn:hover { background: var(--accent-2); }

/* ===================== EMOJI PICKER ===================== */
.emoji-picker {
  position: fixed; z-index: 50;
  background: var(--panel); border: 1px solid var(--line); border-radius: 12px;
  box-shadow: var(--shadow); padding: 8px;
  display: grid; grid-template-columns: repeat(5, 1fr); gap: 2px; width: 220px;
}
.emoji-picker button {
  background: transparent; border: 0; font-size: 20px; padding: 6px; border-radius: 8px;
}
.emoji-picker button:hover { background: var(--panel-2); }

/* ===================== LIGHTBOX ===================== */
.lightbox {
  position: fixed; inset: 0; z-index: 100;
  background: rgba(0,0,0,.9); display: grid; place-items: center; padding: 20px;
}
.lightbox img, .lightbox video { max-width: 96vw; max-height: 92vh; border-radius: 8px; }

/* ===================== RESPONSIVE ===================== */
@media (max-width: 720px) {
  #app { grid-template-columns: 1fr; }
  #chat { display: none; }
  body.chat-open #sidebar { display: none; }
  body.chat-open #chat { display: flex; position: fixed; inset: 0; }
  .chat-head .back { display: block; }
  .msg { max-width: 86%; }
}

/* ===================== NEW: presence, menu, gallery, typing ===================== */
.avatar { position: relative; }
.online-dot {
  position: absolute; right: -1px; bottom: -1px;
  width: 11px; height: 11px; border-radius: 50%;
  background: #22c55e; border: 2px solid var(--panel);
}
.chat-head .peer-meta { flex: 1; min-width: 0; }
#galleryBtn { font-size: 17px; }

/* message action menu button */
.menu-btn {
  position: absolute; top: -10px;
  width: 24px; height: 24px; border-radius: 50%;
  border: 1px solid var(--line); background: var(--panel);
  color: var(--muted); font-size: 13px; line-height: 1;
  display: grid; place-items: center; opacity: 0; transition: opacity .12s;
}
.msg.mine .menu-btn { left: 14px; }
.bubble:hover .menu-btn { opacity: 1; }
@media (hover: none) { .menu-btn { opacity: .6; } }

.msg-menu {
  position: fixed; z-index: 60;
  background: var(--panel); border: 1px solid var(--line);
  border-radius: 10px; box-shadow: var(--shadow); overflow: hidden; min-width: 120px;
}
.msg-menu button {
  display: block; width: 100%; text-align: left;
  padding: 9px 14px; background: transparent; border: 0; color: var(--text); font-size: 14px;
}
.msg-menu button:hover { background: var(--panel-2); }
.msg-menu button[data-act="delete"] { color: var(--danger); }

/* editing state */
.composer.editing input#msgInput { outline: 2px solid #f59e0b; border-color: transparent; }

/* typing indicator */
.typing-row {
  display: flex; align-items: center; gap: 8px;
  padding: 2px 20px 8px; color: var(--muted); font-size: 12px;
}
.typing-dots { display: inline-flex; gap: 3px; }
.typing-dots i {
  width: 6px; height: 6px; border-radius: 50%; background: var(--muted);
  animation: blink 1.2s infinite both;
}
.typing-dots i:nth-child(2) { animation-delay: .2s; }
.typing-dots i:nth-child(3) { animation-delay: .4s; }
@keyframes blink { 0%, 80%, 100% { opacity: .3; } 40% { opacity: 1; } }

/* gallery overlay */
.gallery {
  position: fixed; inset: 0; z-index: 90;
  background: var(--bg); display: flex; flex-direction: column;
}
.gallery-head {
  display: flex; align-items: center; justify-content: space-between;
  padding: 14px 18px; border-bottom: 1px solid var(--line);
}
.gallery-grid {
  flex: 1; overflow-y: auto; padding: 12px;
  display: grid; grid-template-columns: repeat(auto-fill, minmax(120px, 1fr)); gap: 8px;
  align-content: start;
}
.g-cell {
  aspect-ratio: 1; border-radius: 10px; overflow: hidden; background: var(--panel-2);
}
.g-cell img, .g-cell video { width: 100%; height: 100%; object-fit: cover; cursor: pointer; }

/* youtube link embed */
.yt-embed { position: relative; display: inline-block; margin-top: 8px; cursor: pointer; }
.yt-embed .link-embed { display: block; margin: 0; }
.yt-play {
  position: absolute; inset: 0; margin: auto;
  width: 54px; height: 38px; border-radius: 9px;
  background: rgba(0,0,0,.65); color: #fff; font-size: 16px;
  display: grid; place-items: center; pointer-events: none;
}
