:root {
  --bg: #f6f7fb;
  --card: #ffffff;
  --text: #1f2937;
  --muted: #6b7280;
  --line: #e5e7eb;
  --primary: #3b82f6;
  --primary-dark: #2563eb;
  --danger: #ef4444;
  --success: #16a34a;
  --shadow: 0 10px 30px rgba(15, 23, 42, 0.08);
  --radius: 18px;
}

*,
*::before,
*::after {
  box-sizing: border-box;
}

html,
body {
  width: 100%;
  height: 100%;
  overflow-x: hidden;
  -webkit-text-size-adjust: 100%;
  text-rendering: optimizeLegibility;
}

body {
  margin: 0;
  min-height: 100dvh;
  font-family: Arial, Helvetica, sans-serif;
  background: var(--bg);
  color: var(--text);
}

button,
input,
textarea,
select {
  font: inherit;
}

img,
video,
audio,
canvas {
  max-width: 100%;
}

.hidden {
  display: none !important;
}

.muted,
.small {
  color: var(--muted);
}

.card {
  background: var(--card);
  box-shadow: var(--shadow);
  border-radius: var(--radius);
  border: 1px solid var(--line);
  padding: 18px;
}

.center {
  width: min(460px, calc(100% - 24px));
  margin: 6vh auto;
}

.stack {
  display: flex;
  flex-direction: column;
  gap: 10px;
}

.row {
  display: flex;
  gap: 10px;
}

.btn {
  border: 1px solid var(--line);
  background: #fff;
  color: var(--text);
  border-radius: 12px;
  padding: 10px 14px;
  cursor: pointer;
  transition: transform 0.15s ease, border-color 0.15s ease, background 0.15s ease;
}

.btn:hover {
  transform: translateY(-1px);
  border-color: #cbd5e1;
}

.btn:active {
  transform: scale(0.98);
}

.btn:disabled {
  opacity: 0.6;
  cursor: not-allowed;
  transform: none;
}

.btn.primary {
  background: var(--primary);
  border-color: var(--primary);
  color: #fff;
}

.btn.primary:hover {
  background: var(--primary-dark);
  border-color: var(--primary-dark);
}

.btn.small {
  padding: 7px 10px;
  border-radius: 10px;
  font-size: 0.9rem;
}

.btndangersmall {
  border: 1px solid #fecaca;
  background: #fff5f5;
  color: var(--danger);
  border-radius: 10px;
  padding: 7px 10px;
  cursor: pointer;
}

.topbar {
  min-height: 72px;
  background: #fff;
  border-bottom: 1px solid var(--line);
  display: flex;
  align-items: center;
  gap: 14px;
  padding: 12px 16px;
  position: sticky;
  top: 0;
  z-index: 50;
  flex-wrap: wrap;
}

.brand {
  display: flex;
  align-items: center;
  gap: 10px;
}

.logo {
  width: 40px;
  height: 40px;
  border-radius: 50%;
  object-fit: cover;
  border: 1px solid var(--line);
  background: #fff;
}

.header-actions {
  display: flex;
  align-items: center;
  gap: 8px;
  flex-wrap: wrap;
  overflow-x: auto;
  overflow-y: hidden;
  white-space: nowrap;
  -webkit-overflow-scrolling: touch;
  padding-bottom: 4px;
}

.header-actions .btn {
  flex: 0 0 auto;
}

#layout {
  display: flex;
  min-height: calc(100dvh - 72px);
  height: calc(100dvh - 72px);
  min-height: 0;
}

#sidebar {
  flex: 0 0 300px;
  width: 300px;
  min-width: 0;
  border-right: 1px solid var(--line);
  background: #fff;
  display: flex;
  flex-direction: column;
  min-height: 0;
  overflow-y: auto;
}

.search {
  padding: 12px;
  border-bottom: 1px solid var(--line);
}

.search input,
#username,
#password,
#avatar,
#groupName,
#groupAvatar,
#newUsername,
#newPassword,
#globalSearch,
#msgContent,
select,
input[type="text"],
input[type="password"],
input[type="url"] {
  width: 100%;
  border: 1px solid var(--line);
  border-radius: 12px;
  padding: 10px 12px;
  background: #fff;
  outline: none;
}

#globalSearch {
  padding: 11px 12px;
}

.users-list {
  overflow: auto;
  padding: 10px;
  display: flex;
  flex-direction: column;
  gap: 8px;
}

.user-item {
  border: 1px solid var(--line);
  background: #fff;
  border-radius: 14px;
  padding: 10px;
  cursor: pointer;
  transition: transform 0.15s ease, border-color 0.15s ease;
}

.user-item:hover {
  border-color: #cbd5e1;
  transform: translateY(-1px);
}

.user-item .badge,
.unread-badge {
  display: inline-flex;
  min-width: 20px;
  height: 20px;
  align-items: center;
  justify-content: center;
  border-radius: 999px;
  background: var(--danger);
  color: #fff;
  font-size: 0.75rem;
  padding: 0 6px;
}

.presence {
  width: 10px;
  height: 10px;
  border-radius: 50%;
  display: inline-block;
}

.presence.online {
  background: var(--success);
}

.presence.offline {
  background: #9ca3af;
}

.main-area {
  flex: 1;
  min-width: 0;
  display: flex;
  flex-direction: column;
  min-height: 0;
  overflow: hidden;
}

.messages {
  flex: 1;
  min-height: 0;
  overflow-y: auto;
  padding: 16px;
  display: flex;
  flex-direction: column;
  gap: 10px;
}

.message {
  max-width: min(78%, 760px);
  border: 1px solid var(--line);
  background: #fff;
  border-radius: 16px;
  padding: 10px 12px;
  box-shadow: 0 1px 0 rgba(0, 0, 0, 0.02);
  word-break: break-word;
}

.message.sent {
  margin-left: auto;
  background: #eff6ff;
  border-color: #dbeafe;
}

.message.received {
  margin-right: auto;
  background: #fff;
}

.message.selected {
  outline: 2px solid var(--primary);
}

.content {
  white-space: pre-wrap;
  line-height: 1.45;
}

.time {
  font-size: 0.76rem;
  color: var(--muted);
}

.seentick {
  font-size: 0.95rem;
  color: #9ca3af;
}

.seentick.read {
  color: var(--success);
  font-weight: 700;
}

.file-div {
  margin-top: 8px;
  padding: 8px;
  border: 1px solid #eef2f7;
  border-radius: 12px;
  background: #fafafa;
}

.chat-image-preview {
  display: block;
  width: 100%;
  max-width: 100%;
  height: auto;
  max-height: 240px;
  border-radius: 12px;
  margin-bottom: 8px;
  object-fit: cover;
}

.controls.sticky {
  position: sticky;
  bottom: 0;
  z-index: 20;
  background: #fff;
  border-top: 1px solid var(--line);
  padding: 12px;
  display: flex;
  gap: 8px;
  align-items: flex-end;
  flex-wrap: wrap;
}

#msgContent {
  min-height: 60px;
  max-height: 160px;
  resize: vertical;
  line-height: 1.4;
  flex: 1 1 100%;
}

.file-row {
  display: flex;
  gap: 8px;
  align-items: center;
  flex: 1 1 100%;
  min-width: 0;
}

.file-row input[type="file"] {
  flex: 1;
  min-width: 0;
}

.message audio {
  width: 100%;
  margin-top: 8px;
}

.message .download-line {
  margin-top: 8px;
  display: flex;
  gap: 8px;
  align-items: center;
  flex-wrap: wrap;
}

.message .download-line .file-name,
.file-name {
  font-size: 0.9rem;
  color: var(--muted);
  word-break: break-word;
}

.overlay {
  display: none;
  position: fixed;
  inset: 0;
  background: rgba(15, 23, 42, 0.45);
  z-index: 10000;
}

.popup-card {
  display: none;
  position: fixed;
  left: 50%;
  top: 50%;
  transform: translate(-50%, -50%);
  width: min(760px, calc(100% - 20px));
  max-height: 90dvh;
  overflow: auto;
  background: #fff;
  border: 1px solid var(--line);
  border-radius: 18px;
  box-shadow: var(--shadow);
  padding: 16px;
  z-index: 10001;
  overscroll-behavior: contain;
}

.popup-card.small {
  width: min(560px, calc(100% - 20px));
}

.popup-actions {
  display: flex;
  justify-content: flex-end;
  gap: 8px;
  margin-top: 14px;
  flex-wrap: wrap;
}

.toast-root {
  position: fixed;
  right: 16px;
  bottom: 16px;
  z-index: 20000;
  display: flex;
  flex-direction: column;
  gap: 8px;
}

.toast-item {
  background: #111827;
  color: #fff;
  padding: 10px 14px;
  border-radius: 12px;
  box-shadow: var(--shadow);
  max-width: 320px;
}

.alluser-item,
.notif-item {
  border: 1px solid var(--line);
  border-radius: 12px;
  padding: 10px;
  background: #fff;
  margin-bottom: 8px;
}

.avatar-small {
  width: 28px;
  height: 28px;
  border-radius: 50%;
  object-fit: cover;
  vertical-align: middle;
  margin-right: 8px;
}

.call-room-shell {
  display: grid;
  grid-template-columns: minmax(0, 2fr) 380px;
  gap: 12px;
  min-height: 72vh;
}

.call-panel-card {
  border: 1px solid #e5e7eb;
  background: #fff;
  border-radius: 18px;
  padding: 12px;
  box-shadow: 0 8px 24px rgba(15, 23, 42, 0.06);
}

.call-remote-grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(180px, 1fr));
  gap: 10px;
}

.call-remote-tile {
  position: relative;
  min-height: 160px;
  background: #0f172a;
  border-radius: 16px;
  overflow: hidden;
  border: 1px solid rgba(255, 255, 255, 0.06);
}

.call-remote-tile video {
  width: 100%;
  height: 100%;
  object-fit: cover;
  background: #000;
}

.call-remote-badge {
  position: absolute;
  left: 8px;
  bottom: 8px;
  background: rgba(15, 23, 42, 0.72);
  color: #fff;
  padding: 6px 10px;
  border-radius: 999px;
  font-size: 0.82rem;
}

#callStage,
#callVideoContainer {
  min-height: 620px;
}

#callRightPanel {
  min-width: 0;
}

@media (max-width: 1100px) {
  .call-room-shell {
    grid-template-columns: 1fr;
  }

  #callRightPanel {
    order: 2;
  }
}

@media (max-width: 900px) {
  #layout {
    flex-direction: column;
    height: auto;
    min-height: calc(100dvh - 72px);
  }

  #sidebar {
    width: 100%;
    flex: none;
    max-height: 240px;
    border-right: 0;
    border-bottom: 1px solid var(--line);
  }

  .main-area {
    width: 100%;
  }

  .messages {
    padding: 10px;
  }

  .message {
    max-width: 94%;
  }

  .topbar {
    min-height: auto;
    padding: 10px 12px;
  }

  .brand strong {
    font-size: 1rem;
  }

  .header-actions {
    width: 100%;
  }

  .popup-card {
    width: calc(100% - 16px);
    max-height: 88dvh;
    padding: 14px;
  }

  .popup-card.small {
    width: calc(100% - 16px);
  }

  #callRoomPopup {
    width: calc(100% - 16px);
  }

  .call-room-shell {
    grid-template-columns: 1fr;
  }

  #callStage,
  #callVideoContainer {
    min-height: 420px !important;
  }

  .controls.sticky {
    padding-bottom: calc(12px + env(safe-area-inset-bottom));
  }
}

@media (max-width: 640px) {
  .center {
    margin: 3vh auto;
  }

  .card,
  .popup-card.small,
  .popup-card {
    padding: 14px;
  }

  .btn {
    padding: 9px 12px;
  }

  .header-actions {
    gap: 6px;
  }

  .topbar {
    align-items: stretch;
  }

  .brand {
    width: 100%;
  }

  .toast-root {
    right: 10px;
    left: 10px;
    bottom: 10px;
  }

  .toast-item {
    max-width: none;
    width: 100%;
  }

  .chat-image-preview {
    max-height: 200px;
  }
}