/* --- Telegram-style messenger --- */
body.tg-messenger-body {
  --site-nav-height: 56px;
  overflow: hidden;
  height: 100dvh;
}

main.tg-messenger-main {
  padding: 0 !important;
  margin: 0;
  max-width: none;
  width: 100%;
  height: calc(100dvh - var(--site-nav-height, 56px));
  overflow: hidden;
}

.tg-app {
  --tg-blue: #3390ec;
  --tg-blue-deep: #2a7fd4;
  --tg-sidebar-bg: var(--bs-tertiary-bg);
  --tg-chat-bg: var(--bs-body-bg);
  --tg-bubble-in: var(--bs-secondary-bg);
  --tg-bubble-out: #3390ec;
  --tg-bubble-out-text: #fff;
  --tg-border: var(--bs-border-color);
  --tg-muted: var(--bs-secondary-color);
  --tg-text: var(--bs-body-color);
  --tg-height: 100%;

  display: grid;
  grid-template-columns: minmax(320px, 420px) minmax(0, 1fr);
  height: var(--tg-height);
  max-height: var(--tg-height);
  overflow: hidden;
  background: var(--tg-chat-bg);
  color: var(--tg-text);
}

[data-bs-theme="dark"] .tg-app {
  --tg-sidebar-bg: #17212b;
  --tg-chat-bg: #0e1621;
  --tg-bubble-in: #182533;
  --tg-bubble-out: #2b5278;
  --tg-border: rgba(255, 255, 255, 0.08);
}

.tg-sidebar {
  display: flex;
  flex-direction: column;
  background: var(--tg-sidebar-bg);
  border-right: 1px solid var(--tg-border);
  min-height: 0;
}

.tg-new-group-btn {
  white-space: nowrap;
  font-size: 0.8rem;
  font-weight: 650;
  padding: 0.4rem 0.75rem;
}

.tg-sidebar__head {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 0.5rem;
  padding: 0.75rem 0.85rem 0.5rem;
  min-width: 0;
  border-bottom: 0;
}

.tg-sidebar__tools {
  display: flex;
  flex-wrap: wrap;
  justify-content: flex-end;
  align-items: center;
  gap: 0.25rem;
  min-width: 0;
}

.tg-sidebar__actions {
  padding: 0 0.85rem 0.65rem;
  border-bottom: 1px solid var(--tg-border);
}

.tg-sidebar__title {
  font-size: 1.15rem;
  font-weight: 700;
  margin: 0;
  letter-spacing: -0.02em;
}

.tg-sidebar__search {
  padding: 0.65rem 0.85rem;
  border-bottom: 1px solid var(--tg-border);
}

.tg-sidebar__search input {
  border-radius: 999px;
  border: 0;
  background: color-mix(in srgb, var(--bs-body-color) 6%, transparent);
  padding: 0.55rem 1rem;
}

.tg-sidebar__list {
  flex: 1;
  overflow-y: auto;
  min-height: 0;
}

.tg-sidebar__list form {
  width: 100%;
  margin: 0;
}

.tg-chat-panel {
  min-width: 0;
  min-height: 0;
  display: flex;
  flex-direction: column;
  overflow: hidden;
  background: var(--tg-chat-bg);
}

.tg-chat-panel > .tg-main {
  flex: 1 1 auto;
  min-height: 0;
  height: 100%;
}

.tg-chat-item {
  display: flex;
  align-items: center;
  gap: 0.75rem;
  padding: 0.7rem 1rem;
  text-decoration: none;
  color: inherit;
  border: 0;
  background: transparent;
  width: 100%;
  text-align: left;
  transition: background 0.12s ease;
}

.tg-chat-item:hover {
  background: color-mix(in srgb, var(--tg-blue) 8%, transparent);
  color: inherit;
}

.tg-chat-item.is-active {
  background: color-mix(in srgb, var(--tg-blue) 18%, transparent);
}

.tg-avatar {
  flex: 0 0 auto;
  width: 48px;
  height: 48px;
  border-radius: 50%;
  display: grid;
  place-items: center;
  font-weight: 700;
  font-size: 0.95rem;
  color: #fff;
  background: linear-gradient(135deg, #5ca8f5, #3390ec);
  text-transform: uppercase;
}

.tg-avatar--sm {
  width: 36px;
  height: 36px;
  font-size: 0.8rem;
}

.tg-avatar--group {
  background: linear-gradient(135deg, #3d8b6e, #2a6b54);
  font-size: 1.1rem;
}

.tg-chat-item__meta {
  min-width: 0;
  flex: 1;
}

.tg-chat-item__row {
  display: flex;
  justify-content: space-between;
  gap: 0.5rem;
  align-items: baseline;
}

.tg-chat-item__name {
  font-weight: 700;
  font-size: 1rem;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.tg-chat-item__time {
  flex: 0 0 auto;
  font-size: 0.72rem;
  color: var(--tg-muted);
}

.tg-chat-item__preview {
  font-size: 0.82rem;
  color: var(--tg-muted);
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
  margin-top: 0.1rem;
}

.tg-sidebar__section {
  padding: 0.65rem 1rem 0.25rem;
  font-size: 0.72rem;
  font-weight: 700;
  letter-spacing: 0.04em;
  text-transform: uppercase;
  color: var(--tg-muted);
}

.tg-main {
  display: flex;
  flex-direction: column;
  flex: 1 1 auto;
  height: 100%;
  min-width: 0;
  min-height: 0;
  overflow: hidden;
  background:
    radial-gradient(circle at 20% 20%, color-mix(in srgb, var(--tg-blue) 8%, transparent), transparent 40%),
    radial-gradient(circle at 80% 80%, color-mix(in srgb, var(--tg-blue) 6%, transparent), transparent 45%),
    var(--tg-chat-bg);
}

.tg-main__empty {
  margin: auto;
  text-align: center;
  color: var(--tg-muted);
  padding: 2rem;
}

.tg-main__empty-card {
  display: inline-block;
  padding: 1rem 1.25rem;
  border-radius: 999px;
  background: color-mix(in srgb, var(--bs-body-color) 6%, transparent);
  font-size: 0.92rem;
}

.tg-topbar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 1rem;
  padding: 0.65rem 1rem;
  background: var(--tg-sidebar-bg);
  border-bottom: 1px solid var(--tg-border);
}

.tg-topbar__peer {
  display: flex;
  align-items: center;
  gap: 0.75rem;
  min-width: 0;
}

.tg-topbar__name {
  font-weight: 700;
  font-size: 1rem;
  margin: 0;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.tg-topbar__status {
  font-size: 0.78rem;
  color: var(--tg-muted);
}

.tg-topbar__status.is-online {
  color: #4fae4e;
}

.tg-topbar__status.is-away {
  color: #eab308;
}

.tg-topbar__typing {
  font-size: 0.78rem;
  color: #4fae4e;
  font-style: italic;
}

.tg-topbar__actions {
  display: flex;
  gap: 0.35rem;
  align-items: center;
}

.tg-chat-settings {
  min-width: 14rem;
}

.tg-chat-flags {
  display: flex;
  flex-wrap: wrap;
  gap: 0.4rem;
  padding: 0.35rem 1rem;
  border-bottom: 1px solid var(--tg-border);
  background: color-mix(in srgb, var(--tg-sidebar-bg) 92%, #f59e0b);
}

.tg-chat-flag {
  display: inline-flex;
  align-items: center;
  font-size: 0.75rem;
  color: var(--tg-muted);
}

.tg-sidebar__filters {
  display: flex;
  gap: 0.35rem;
  padding: 0 0.75rem 0.5rem;
  flex-wrap: wrap;
}

.tg-filter {
  font-size: 0.72rem;
  padding: 0.2rem 0.55rem;
  border-radius: 999px;
  color: var(--tg-muted);
  text-decoration: none;
  background: transparent;
  border: 1px solid var(--tg-border);
}

.tg-filter.is-active,
.tg-filter:hover {
  color: var(--tg-text);
  background: color-mix(in srgb, var(--tg-accent, #2aabee) 12%, transparent);
  border-color: color-mix(in srgb, var(--tg-accent, #2aabee) 35%, var(--tg-border));
}

.tg-chat-item.is-unread .tg-chat-item__name {
  font-weight: 800;
}

.tg-chat-item.is-pinned {
  background: color-mix(in srgb, var(--tg-accent, #2aabee) 6%, transparent);
}

.tg-chat-item__sep {
  margin: 0 0.25rem;
  opacity: 0.5;
}

.tg-avatar.is-online,
.tg-avatar--sm.is-online {
  box-shadow: 0 0 0 2px #4fae4e;
}

.tg-avatar.is-away,
.tg-avatar--sm.is-away {
  box-shadow: 0 0 0 2px #eab308;
}

.tg-bubble__receipt {
  margin-left: 0.35rem;
  color: #4fae4e;
  font-size: 0.85em;
}

.tg-composer--disabled {
  opacity: 0.85;
}

.tg-composer__blocked {
  width: 100%;
  text-align: center;
  padding: 0.85rem 1rem;
  color: var(--tg-muted);
  font-size: 0.9rem;
}

.tg-icon-btn {
  width: 40px;
  height: 40px;
  border-radius: 50%;
  border: 0;
  background: transparent;
  color: var(--tg-muted);
  display: grid;
  place-items: center;
  text-decoration: none;
}

.tg-icon-btn:hover {
  background: color-mix(in srgb, var(--tg-blue) 12%, transparent);
  color: var(--tg-blue);
}

.tg-messages {
  flex: 1;
  overflow-y: auto;
  padding: 1rem 1.25rem 0.5rem;
  display: flex;
  flex-direction: column;
  gap: 0.35rem;
  min-height: 0;
}

.tg-message {
  display: flex;
  flex-direction: column;
  align-items: flex-start;
  max-width: min(72%, 520px);
}

.tg-message--mine {
  align-self: flex-end;
  align-items: flex-end;
}

.tg-bubble {
  padding: 0.45rem 0.7rem 0.35rem;
  border-radius: 14px;
  background: var(--tg-bubble-in);
  box-shadow: 0 1px 1px rgba(0, 0, 0, 0.04);
  position: relative;
}

.tg-message--mine .tg-bubble {
  background: var(--tg-bubble-out);
  color: var(--tg-bubble-out-text);
  border-bottom-right-radius: 4px;
}

.tg-message:not(.tg-message--mine) .tg-bubble {
  border-bottom-left-radius: 4px;
}

.tg-bubble__author {
  font-size: 0.72rem;
  font-weight: 700;
  margin-bottom: 0.15rem;
  opacity: 0.9;
}

.tg-message--mine .tg-bubble__author {
  color: rgba(255, 255, 255, 0.9);
}

.tg-message:not(.tg-message--mine) .tg-bubble__author {
  color: var(--tg-blue);
}

.tg-bubble__text {
  font-size: 0.95rem;
  line-height: 1.35;
  white-space: pre-wrap;
  word-break: break-word;
}

.tg-bubble__meta {
  display: flex;
  justify-content: flex-end;
  gap: 0.35rem;
  margin-top: 0.15rem;
  font-size: 0.68rem;
  opacity: 0.75;
}

.tg-composer {
  flex: 0 0 auto;
  position: sticky;
  bottom: 0;
  z-index: 5;
  padding: 0.65rem 0.85rem 0.85rem;
  padding-bottom: calc(0.85rem + env(safe-area-inset-bottom, 0px));
  background: var(--tg-sidebar-bg);
  border-top: 1px solid var(--tg-border);
}

.tg-composer__row {
  display: flex;
  align-items: flex-end;
  gap: 0.45rem;
}

.tg-composer__field {
  flex: 1;
  border-radius: 22px;
  border: 0;
  background: color-mix(in srgb, var(--bs-body-color) 6%, transparent);
  padding: 0.7rem 1rem;
  resize: none;
  max-height: 140px;
  min-height: 44px;
  color: var(--tg-text);
}

.tg-composer__field:focus {
  outline: none;
  box-shadow: 0 0 0 2px color-mix(in srgb, var(--tg-blue) 35%, transparent);
}

.tg-composer__send {
  width: 44px;
  height: 44px;
  border-radius: 50%;
  border: 0;
  background: var(--tg-blue);
  color: #fff;
  display: grid;
  place-items: center;
  flex: 0 0 auto;
}

.tg-composer__send:hover {
  background: var(--tg-blue-deep);
}

.tg-service {
  align-self: center;
  max-width: 85%;
  margin: 0.35rem 0;
  padding: 0.35rem 0.75rem;
  border-radius: 999px;
  background: color-mix(in srgb, var(--bs-body-color) 8%, transparent);
  color: var(--tg-muted);
  font-size: 0.78rem;
  text-align: center;
  line-height: 1.35;
}

/* --- Call overlay (Telegram-style, viewport-capped) --- */
.tg-call-overlay {
  display: flex;
  flex-direction: column;
  gap: 0.5rem;
  padding: 0.5rem 0.75rem 0.65rem;
  max-height: min(48vh, 420px);
  background: #0b141a;
  border-bottom: 1px solid rgba(255, 255, 255, 0.08);
  color: #e8eef2;
}

.tg-call-overlay__header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 0.75rem;
  flex-shrink: 0;
}

.tg-call-overlay__title {
  font-size: 0.88rem;
  font-weight: 600;
  opacity: 0.9;
}

.tg-call-stage {
  position: relative;
  flex: 1 1 auto;
  min-height: 120px;
  max-height: min(36vh, 320px);
  border-radius: 12px;
  overflow: hidden;
  background: #060b10;
}

.tg-call-remote {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(120px, 1fr));
  gap: 0.4rem;
  height: 100%;
  max-height: inherit;
  padding: 0.4rem;
}

.tg-call-tile {
  position: relative;
  border-radius: 10px;
  overflow: hidden;
  background: #121a22;
  min-height: 0;
  max-height: min(34vh, 300px);
}

.tg-call-tile__video {
  width: 100%;
  height: 100%;
  max-height: min(34vh, 300px);
  object-fit: contain;
  background: #000;
  display: block;
}

.tg-call-tile__label {
  position: absolute;
  left: 0.5rem;
  bottom: 0.5rem;
  padding: 0.15rem 0.45rem;
  border-radius: 999px;
  background: rgba(0, 0, 0, 0.55);
  font-size: 0.72rem;
}

.tg-call-local {
  position: absolute;
  right: 0.5rem;
  bottom: 0.5rem;
  width: 88px;
  border-radius: 10px;
  overflow: hidden;
  border: 2px solid rgba(255, 255, 255, 0.2);
  background: #000;
  box-shadow: 0 8px 24px rgba(0, 0, 0, 0.35);
  z-index: 2;
}

.tg-call-local__video {
  width: 100%;
  aspect-ratio: 3 / 4;
  max-height: 110px;
  object-fit: cover;
  display: block;
  background: #111;
}

.tg-call-local__video.is-hidden {
  display: none;
}

.tg-call-local__label {
  position: absolute;
  left: 0.35rem;
  bottom: 0.35rem;
  font-size: 0.65rem;
  background: rgba(0, 0, 0, 0.5);
  padding: 0.1rem 0.35rem;
  border-radius: 999px;
}

.tg-call-controls {
  display: flex;
  justify-content: center;
  gap: 0.75rem;
  padding-bottom: 0.25rem;
}

.tg-call-btn {
  width: 48px;
  height: 48px;
  border-radius: 50%;
  border: 0;
  display: grid;
  place-items: center;
  background: rgba(255, 255, 255, 0.12);
  color: #fff;
  text-decoration: none;
}

.tg-call-btn:hover {
  background: rgba(255, 255, 255, 0.2);
  color: #fff;
}

.tg-call-btn.is-off {
  background: rgba(255, 255, 255, 0.08);
  color: #f87171;
}

.tg-call-btn.is-sharing {
  background: rgba(74, 163, 255, 0.28);
  color: #e8eef5;
  box-shadow: 0 0 0 1px rgba(74, 163, 255, 0.4);
}

.tg-call-tile.is-screen-share .tg-call-tile__label::after {
  content: " · screen";
  opacity: 0.8;
  font-weight: 500;
}

.tg-call-btn--danger {
  background: #e53935;
}

.tg-call-btn--danger:hover {
  background: #c62828;
}

.tg-call-btn--ghost {
  background: transparent;
  color: rgba(255, 255, 255, 0.8);
}

.tg-call-btn--record.is-recording {
  background: #e53935;
  color: #fff;
  box-shadow: 0 0 0 3px rgba(229, 57, 53, 0.35);
  animation: tg-record-pulse 1.4s ease-in-out infinite;
}

.tg-call-btn--record.is-uploading {
  opacity: 0.7;
  cursor: wait;
}

@keyframes tg-record-pulse {
  0%, 100% { box-shadow: 0 0 0 3px rgba(229, 57, 53, 0.25); }
  50% { box-shadow: 0 0 0 6px rgba(229, 57, 53, 0.45); }
}

.tg-call-recording-banner {
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 0.35rem;
  padding: 0.4rem 0.75rem;
  background: rgba(229, 57, 53, 0.2);
  border-bottom: 1px solid rgba(229, 57, 53, 0.35);
  color: #fecaca;
  font-size: 0.85rem;
}

.tg-call-recording-banner[hidden] {
  display: none !important;
}

.tg-service__link {
  color: inherit;
  text-decoration: underline;
  text-underline-offset: 2px;
}

.tg-call-banner {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 1rem;
  padding: 0.55rem 1rem;
  background: color-mix(in srgb, #198754 16%, transparent);
  border-bottom: 1px solid color-mix(in srgb, #198754 30%, transparent);
  font-size: 0.88rem;
}

.tg-call-banner--ringing {
  background: color-mix(in srgb, #198754 22%, transparent);
  animation: tg-ring-pulse 1.4s ease-in-out infinite;
}

@keyframes tg-ring-pulse {
  0%, 100% { filter: brightness(1); }
  50% { filter: brightness(1.08); }
}

.tg-presence-select {
  max-width: 6.5rem;
  height: 1.75rem;
  padding: 0 0.35rem;
  border-radius: 0.4rem;
  border: 1px solid var(--tg-border, var(--bs-border-color));
  background: var(--bs-body-bg);
  color: var(--bs-body-color);
  font-size: 0.72rem;
}

.tg-audio-panel {
  display: inline-flex;
  align-items: center;
  gap: 0.25rem;
  padding-right: 0.15rem;
}

.tg-audio-panel__slider {
  width: 64px;
  height: 0.35rem;
  accent-color: var(--tg-blue, #3390ec);
  cursor: pointer;
}

.tg-audio-panel__label {
  font-size: 0.68rem;
  color: var(--tg-muted);
  min-width: 3.5rem;
}

@media (max-width: 768px) {
  .tg-app {
    grid-template-columns: 1fr;
  }

  .tg-app.has-chat .tg-sidebar {
    display: none;
  }

  .tg-app:not(.has-chat) .tg-chat-panel {
    display: none;
  }
}

