:root {
  color-scheme: dark;
  --bg: #0b141a;
  --panel: #111b21;
  --panel2: #202c33;
  --panel3: #0b141a;
  --line: rgba(134, 150, 160, 0.22);
  --text: #e9edef;
  --muted: #8696a0;
  --green: #00a884;
  --green2: #008069;
  --red: #e74c5f;
  --bubble: #005c4b;
  --bubble2: #202c33;
  --shadow: 0 18px 55px rgba(0, 0, 0, 0.42);
}

* { box-sizing: border-box; }
[hidden] { display: none !important; }

body {
  margin: 0;
  min-height: 100vh;
  overflow: hidden;
  font-family: "Segoe UI", Arial, sans-serif;
  background: #0b141a;
  color: var(--text);
}

button, input { font: inherit; }

button {
  min-height: 38px;
  border: 0;
  border-radius: 8px;
  padding: 0 14px;
  background: #2a3942;
  color: var(--text);
  font-weight: 700;
  cursor: pointer;
}

button:hover { background: #34464f; }
button.primary { background: var(--green2); color: white; }
button.danger { background: #c94152; color: white; }

button svg {
  width: 21px;
  height: 21px;
  fill: none;
  stroke: currentColor;
  stroke-width: 2;
  stroke-linecap: round;
  stroke-linejoin: round;
}

input {
  min-height: 40px;
  width: 100%;
  border: 0;
  border-radius: 8px;
  background: #2a3942;
  color: var(--text);
  padding: 0 12px;
  outline: none;
}

.app {
  height: 100vh;
  display: grid;
  grid-template-columns: 380px minmax(0, 1fr);
  background: var(--bg);
}

.sidebar, .conversation {
  min-height: 0;
}

.sidebar {
  display: flex;
  flex-direction: column;
  background: var(--panel);
  border-right: 1px solid var(--line);
}

.brand, .chat-top {
  min-height: 64px;
  display: flex;
  align-items: center;
  gap: 12px;
  padding: 10px 16px;
  background: var(--panel2);
}

.brand-mark, .avatar {
  width: 44px;
  height: 44px;
  flex: 0 0 44px;
  display: grid;
  place-items: center;
  border-radius: 50%;
  background: linear-gradient(135deg, #00a884, #53bdeb);
  color: white;
  font-weight: 900;
}

.brand strong, .brand small, .peer-summary strong, .peer-summary small {
  display: block;
}

.brand small, .peer-summary small, .section-title, .contact small, .info-panel span, .log-line {
  color: var(--muted);
}

.icon-btn {
  margin-left: auto;
  width: 42px;
  height: 42px;
  min-height: 42px;
  padding: 0;
  display: grid;
  place-items: center;
  border-radius: 50%;
  background: transparent;
  color: var(--muted);
}

.icon-btn:hover { background: #2a3942; color: var(--text); }

.profile-panel {
  display: grid;
  gap: 10px;
  padding: 12px 14px;
  border-bottom: 1px solid var(--line);
  background: #111b21;
}

.profile-panel label {
  display: grid;
  gap: 6px;
  color: var(--muted);
  font-size: 13px;
  font-weight: 700;
}

.room-actions {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 8px;
}

.search-box {
  padding: 10px 14px;
  background: var(--panel);
}

.save-contact {
  margin: 0 14px 10px;
  background: #1f2c33;
  color: #d9fdd3;
}

.contacts {
  min-height: 0;
  flex: 1;
  display: flex;
  flex-direction: column;
}

.section-title {
  display: flex;
  justify-content: space-between;
  padding: 12px 16px 7px;
  font-size: 12px;
  text-transform: uppercase;
  font-weight: 900;
}

.saved-title {
  margin-top: 8px;
  border-top: 1px solid var(--line);
}

.users {
  min-height: 0;
  overflow: auto;
}

.contact {
  display: grid;
  grid-template-columns: 46px minmax(0, 1fr) 12px;
  align-items: center;
  gap: 12px;
  padding: 12px 14px;
  border-top: 1px solid rgba(134, 150, 160, 0.12);
  cursor: pointer;
}

.contact.offline { opacity: 0.72; }
.contact:hover, .contact.active { background: #202c33; }
.contact strong, .contact small { display: block; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.contact .avatar { width: 42px; height: 42px; flex-basis: 42px; font-size: 13px; }
.contact button { display: none; }
.status-dot { width: 10px; height: 10px; border-radius: 50%; background: var(--red); }
.status-dot.online { background: var(--green); }

.conversation {
  min-width: 0;
  display: grid;
  grid-template-rows: 64px minmax(360px, 54vh) minmax(0, 1fr) 62px;
  background: #0b141a;
}

.chat-top {
  justify-content: space-between;
}

.peer-summary, .call-actions {
  display: flex;
  align-items: center;
  gap: 10px;
  min-width: 0;
}

.peer-summary > div {
  min-width: 0;
}

.peer-summary strong, .peer-summary small {
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.call-actions {
  flex: 0 0 auto;
}

.header-call-btn {
  width: 42px;
  height: 42px;
  min-height: 42px;
  padding: 0;
  display: grid;
  place-items: center;
  border-radius: 50%;
  background: transparent;
  color: var(--muted);
}

.header-call-btn:hover {
  background: #2a3942;
  color: var(--text);
}

.header-call-btn.is-muted {
  background: #7a2d38;
  color: white;
}

.header-call-btn.is-active {
  background: #0b5f4c;
  color: white;
}

.people-toggle {
  display: none;
}

.header-call-btn.danger {
  background: #c94152;
  color: white;
}

.call-area {
  position: relative;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 18px;
  border-bottom: 1px solid var(--line);
  background:
    linear-gradient(180deg, rgba(32, 44, 51, 0.72), rgba(5, 9, 11, 0.96)),
    #05090b;
  overflow: hidden;
}

.video-card {
  position: relative;
  overflow: hidden;
  border: 1px solid rgba(134, 150, 160, 0.24);
  border-radius: 10px;
  background: #030608;
  box-shadow: var(--shadow);
}

.video-card span {
  position: absolute;
  z-index: 2;
  left: 12px;
  top: 10px;
  padding: 5px 9px;
  border-radius: 999px;
  background: rgba(0, 0, 0, 0.55);
  color: white;
  font-size: 12px;
  font-weight: 800;
}

.video-card video {
  width: 100%;
  height: 100%;
  object-fit: contain;
  background: #030608;
}

.main-video {
  width: min(100%, calc((54vh - 36px) * 1.777));
  height: auto;
  min-height: 280px;
  max-height: 100%;
  aspect-ratio: 16 / 9;
}

.video-strip {
  position: absolute;
  right: 28px;
  top: 28px;
  width: min(210px, 24vw);
  max-height: calc(100% - 56px);
  overflow: auto;
  display: grid;
  gap: 10px;
  z-index: 5;
}

.video-strip .video-card {
  aspect-ratio: 16 / 9;
  height: auto;
  cursor: pointer;
  box-shadow: 0 14px 30px rgba(0, 0, 0, 0.42);
}

.video-strip .video-card video {
  object-fit: cover;
}

.video-strip .video-card.active {
  border-color: var(--green);
}

.messages {
  min-height: 0;
  overflow: auto;
  padding: 18px 7vw;
  display: flex;
  flex-direction: column;
  gap: 10px;
  background-color: #0b141a;
  background-image:
    linear-gradient(rgba(255,255,255,0.025) 1px, transparent 1px),
    linear-gradient(90deg, rgba(255,255,255,0.025) 1px, transparent 1px);
  background-size: 28px 28px;
}

.empty-state {
  margin: auto;
  width: min(460px, 90%);
  padding: 18px;
  border-radius: 8px;
  background: rgba(32, 44, 51, 0.72);
  text-align: center;
}

.empty-state b, .empty-state span { display: block; }
.empty-state span { margin-top: 8px; color: var(--muted); line-height: 1.45; }

.msg {
  max-width: min(620px, 78%);
  align-self: flex-start;
  padding: 8px 10px;
  border-radius: 8px 8px 8px 2px;
  background: var(--bubble2);
}

.msg.me {
  align-self: flex-end;
  border-radius: 8px 8px 2px 8px;
  background: var(--bubble);
}

.msg small {
  display: block;
  margin-bottom: 4px;
  color: rgba(233, 237, 239, 0.72);
  font-size: 11px;
}

.msg div { white-space: pre-wrap; overflow-wrap: anywhere; }

.file-msg {
  width: min(420px, 86%);
}

.file-bubble {
  display: grid;
  grid-template-columns: 48px minmax(0, 1fr);
  gap: 10px;
  align-items: center;
}

.file-icon {
  width: 48px;
  height: 48px;
  display: grid;
  place-items: center;
  border-radius: 8px;
  background: rgba(233, 237, 239, 0.12);
  color: #d9fdd3;
  font-size: 10px;
  font-weight: 900;
}

.file-body {
  min-width: 0;
}

.file-body strong, .file-body span, .file-body p {
  display: block;
  overflow-wrap: anywhere;
}

.file-body span, .file-body p {
  color: rgba(233, 237, 239, 0.72);
  font-size: 12px;
}

.file-body p {
  margin: 3px 0 0;
}

.attachment-preview {
  display: block;
  width: 100%;
  max-height: 260px;
  margin-top: 10px;
  border-radius: 8px;
  background: #030608;
}

.image-preview {
  object-fit: contain;
}

.audio-preview {
  width: 100%;
  margin-top: 10px;
}

.download-link {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  margin-top: 10px;
  min-height: 34px;
  padding: 0 12px;
  border-radius: 8px;
  background: rgba(0, 168, 132, 0.18);
  color: #d9fdd3;
  font-weight: 800;
}

.composer {
  position: relative;
  display: grid;
  grid-template-columns: 42px 42px 42px minmax(0, 1fr) 50px;
  gap: 8px;
  align-items: center;
  padding: 10px 14px;
  background: var(--panel2);
}

.round, .attach {
  width: 42px;
  height: 42px;
  min-height: 42px;
  padding: 0;
  display: grid;
  place-items: center;
  border-radius: 50%;
  background: transparent;
  color: var(--muted);
}

.round:hover, .attach:hover { background: #34464f; color: var(--text); }

.attach input { display: none; }
#sendMessage { border-radius: 50%; width: 44px; padding: 0; font-size: 0; }
#sendMessage::before { content: ">"; font-size: 20px; font-weight: 900; }
#screen { font-size: 0; }
#screen::before { content: "▣"; font-size: 18px; }
#screen.is-active { background: #0b5f4c; color: white; }
#screen.is-active::before { content: "■"; }
#emojiToggle { font-size: 0; }
#emojiToggle::before { content: ":)"; font-size: 16px; }

.emoji-panel {
  position: absolute;
  left: 14px;
  bottom: 70px;
  display: flex;
  gap: 8px;
  padding: 10px;
  border-radius: 8px;
  background: var(--panel2);
  box-shadow: var(--shadow);
}

.audio-panel {
  position: absolute;
  right: 18px;
  bottom: 76px;
  z-index: 12;
  width: min(340px, calc(100vw - 28px));
  display: grid;
  gap: 10px;
  padding: 14px;
  border: 1px solid rgba(134, 150, 160, 0.24);
  border-radius: 8px;
  background: rgba(32, 44, 51, 0.96);
  box-shadow: var(--shadow);
}

.audio-panel label {
  display: grid;
  grid-template-columns: 92px minmax(0, 1fr) 48px;
  align-items: center;
  gap: 10px;
  color: var(--text);
  font-size: 13px;
  font-weight: 800;
}

.audio-panel input[type="range"] {
  min-height: 24px;
  height: 24px;
  padding: 0;
  accent-color: var(--green);
}

.audio-panel b {
  text-align: right;
}

.audio-panel small {
  color: var(--muted);
  line-height: 1.35;
}

.info-panel {
  display: none;
}

.incoming {
  position: fixed;
  right: 24px;
  top: 24px;
  display: flex;
  align-items: center;
  gap: 12px;
  width: min(420px, calc(100vw - 28px));
  padding: 16px;
  border-radius: 8px;
  background: #202c33;
  box-shadow: var(--shadow);
  z-index: 20;
}

.incoming b, .incoming span { display: block; }
.incoming span { color: var(--muted); }

.ring-avatar {
  width: 54px;
  height: 54px;
  flex: 0 0 54px;
  display: grid;
  place-items: center;
  border-radius: 50%;
  background: linear-gradient(135deg, #00a884, #53bdeb);
  color: white;
  font-weight: 900;
}

@media (max-width: 980px) {
  body { overflow: hidden; }
  .app {
    height: 100dvh;
    grid-template-columns: 1fr;
    grid-template-rows: minmax(220px, 36dvh) minmax(0, 1fr);
  }
  .sidebar {
    min-height: 0;
    border-right: 0;
    border-bottom: 1px solid var(--line);
  }
  .conversation {
    min-height: 0;
  }
  .profile-panel {
    grid-template-columns: minmax(0, 1fr) minmax(0, 1fr);
    align-items: end;
  }
  .profile-panel .room-actions {
    grid-column: 1 / -1;
  }
  .contacts {
    min-height: 0;
  }
}

@media (max-width: 680px) {
  html, body {
    width: 100%;
    height: 100%;
    overscroll-behavior: none;
  }

  button {
    min-height: 36px;
  }

  input {
    min-height: 38px;
  }

  .app {
    position: fixed;
    inset: 0;
    height: 100dvh;
    width: 100vw;
    display: grid;
    grid-template-columns: 1fr;
    grid-template-rows: 100dvh;
    overflow: hidden;
  }

  .app:not(.is-online) .conversation {
    display: none;
  }

  .app:not(.is-online) .sidebar {
    height: 100dvh;
    border: 0;
  }

  .app:not(.is-online) .profile-panel {
    display: grid !important;
  }

  .app:not(.is-online) #enablePush,
  .app:not(.is-online) #installApp,
  .app:not(.is-online) #privateRoom,
  .app:not(.is-online) #addContact {
    display: none;
  }

  .app:not(.is-online) .room-actions {
    grid-template-columns: 1fr;
    order: 4;
  }

  .app:not(.is-online) #join {
    order: 3;
  }

  .app:not(.is-online) .contacts {
    flex: 1 1 auto;
    min-height: 0;
  }

  .app:not(.is-online) .users {
    min-height: 0;
  }

  .app.is-online .sidebar {
    position: fixed;
    inset: 0 auto 0 0;
    z-index: 35;
    width: min(335px, 88vw);
    height: 100dvh;
    border-right: 1px solid var(--line);
    border-bottom: 0;
    box-shadow: var(--shadow);
    transform: translateX(-105%);
    transition: transform 180ms ease;
  }

  .app.is-online.mobile-contacts-open .sidebar {
    transform: translateX(0);
  }

  .app.is-online.mobile-contacts-open::after {
    content: "";
    position: fixed;
    inset: 0;
    z-index: 30;
    background: rgba(0, 0, 0, 0.45);
  }

  .brand, .chat-top { padding: 10px 12px; }
  .brand {
    min-height: 54px;
  }

  .brand-mark, .avatar {
    width: 38px;
    height: 38px;
    flex-basis: 38px;
  }

  .brand small, .peer-summary small {
    font-size: 12px;
  }

  .icon-btn {
    width: 38px;
    height: 38px;
    min-height: 38px;
  }

  .profile-panel {
    grid-template-columns: 1fr;
    gap: 9px;
    max-height: none;
    overflow: visible;
    padding: 12px;
    background: #101a20;
  }
  .profile-panel label {
    gap: 4px;
    font-size: 12px;
  }
  .profile-panel input {
    min-height: 42px;
    border-radius: 8px;
    background: #26353d;
    font-size: 14px;
  }
  .room-actions {
    gap: 6px;
  }

  .profile-panel #enablePush,
  .profile-panel #installApp {
    min-height: 34px;
    font-size: 12px;
  }

  .profile-panel #join {
    min-height: 48px;
    font-size: 15px;
    border-radius: 8px;
    background: #00a884;
    box-shadow: 0 10px 24px rgba(0, 168, 132, 0.18);
  }

  .profile-panel #copyInvite {
    min-height: 38px;
    background: #1f2c33;
    color: #d9fdd3;
  }

  .search-box {
    padding: 10px 12px 7px;
  }
  .save-contact {
    margin: 0 12px 6px;
  }
  .app:not(.is-online) .search-box {
    border-top: 1px solid rgba(134, 150, 160, 0.14);
  }
  .app:not(.is-online) .contacts {
    border-top: 1px solid rgba(134, 150, 160, 0.12);
  }
  .section-title {
    padding: 8px 12px 4px;
    font-size: 11px;
  }
  .contact {
    min-height: 52px;
    grid-template-columns: 38px minmax(0, 1fr) 10px;
    gap: 10px;
    padding: 7px 12px;
  }
  .contact .avatar {
    width: 36px;
    height: 36px;
    flex-basis: 36px;
    font-size: 12px;
  }
  .contact small {
    font-size: 12px;
  }
  .saved-title {
    margin-top: 2px;
  }
  .chat-top {
    min-width: 0;
    min-height: 92px;
    display: grid;
    grid-template-columns: minmax(0, 1fr);
    grid-template-rows: 40px 36px;
    gap: 6px;
    align-content: center;
  }
  .peer-summary {
    width: 100%;
    flex: 1 1 auto;
    gap: 8px;
  }
  .call-actions {
    width: 100%;
    max-width: 100%;
    justify-content: space-between;
    gap: 2px;
    overflow-x: auto;
    scrollbar-width: none;
  }
  .call-actions::-webkit-scrollbar {
    display: none;
  }

  .people-toggle {
    display: grid;
  }

  .header-call-btn { width: 34px; height: 34px; min-height: 34px; flex: 0 0 34px; }
  .conversation {
    height: 100dvh;
    width: 100vw;
    max-width: 100vw;
    overflow: hidden;
    grid-template-rows: 92px minmax(135px, 27dvh) minmax(0, 1fr) 56px;
  }
  .conversation > * {
    min-width: 0;
    width: 100%;
    max-width: 100vw;
  }
  .call-area { padding: 8px; }
  .main-video {
    width: min(100%, calc(100vw - 16px));
    height: 100%;
    min-height: 0;
    max-height: 100%;
    aspect-ratio: 16 / 9;
  }
  .video-strip {
    right: 12px;
    top: 12px;
    width: 118px;
    max-height: calc(100% - 24px);
    gap: 6px;
  }
  .video-strip .video-card { height: 66px; }
  .messages {
    padding: 10px;
    gap: 8px;
    width: 100%;
    max-width: 100vw;
  }
  .empty-state {
    width: 96%;
    padding: 12px;
    font-size: 13px;
  }
  .composer {
    grid-template-columns: 34px 34px 34px minmax(0, 1fr) 40px;
    gap: 5px;
    padding: 7px 8px;
    width: 100%;
    max-width: 100vw;
  }
  .audio-panel {
    right: 8px;
    bottom: 64px;
    width: calc(100vw - 16px);
    padding: 12px;
  }
  .audio-panel label {
    grid-template-columns: 76px minmax(0, 1fr) 42px;
    gap: 8px;
    font-size: 12px;
  }
  .round, .attach {
    width: 34px;
    height: 34px;
    min-height: 34px;
  }
  #sendMessage {
    width: 40px;
    height: 40px;
    min-height: 40px;
  }
  .incoming {
    right: 10px;
    top: 10px;
    width: calc(100vw - 20px);
    padding: 12px;
  }
}
