#messages-chat-panel {
  min-width: 0;
  height: 100%;
  display: none;
  flex-direction: column;
  background: #09090b;
  border-left: 1px solid #27272a;
}

body {
  --shell-safe-top: 0px;
  --shell-safe-bottom: 0px;
}

body.pwa-shell {
  --shell-safe-top: env(safe-area-inset-top, 0px);
  --shell-safe-bottom: env(safe-area-inset-bottom, 0px);
}

body.tg-shell {
  --shell-safe-top: var(--app-content-safe-area-inset-top, 0px);
  --shell-safe-bottom: var(--app-content-safe-area-inset-bottom, 0px);
}

#chat-empty-state {
  flex: 1;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  text-align: center;
  gap: 0.45rem;
  padding: 1rem;
  color: var(--text-secondary-color);
}

.chat-empty-title {
  color: var(--text-color);
  font-size: 1.05rem;
  font-weight: 600;
}

.chat-empty-text {
  max-width: 320px;
  font-size: 0.9rem;
  opacity: 0.85;
}

#chat-view {
  display: none;
  flex-direction: column;
  height: 100%;
  min-height: 0;
  position: relative;
}

#chat-view.is-visible {
  display: flex;
}

.chat-header {
  display: flex;
  align-items: center;
  gap: 0.7rem;
  min-height: 3.5rem;
  padding: 0.5rem 0.8rem;
  border-bottom: 1px solid #27272a;
  background: #09090b;
}

#chat-back-button {
  width: 2rem;
  height: 2rem;
  /* border-radius: 999px; */
  /* border: 1px solid #3f3f46; */
  /* background: #18181b; */
  color: var(--text-color);
  font-size: 1rem;
  line-height: 1;
  cursor: pointer;
}

#chat-back-button:hover {
  background: #27272a;
}

#chat-header-avatar {
  width: 2.15rem;
  height: 2.15rem;
  border-radius: 999px;
  object-fit: cover;
  background: #18181b;
}

.chat-header-meta {
  display: flex;
  flex-direction: column;
  min-width: 0;
}

#chat-header-name {
  color: var(--text-color);
  font-weight: 600;
  line-height: 1.2;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

#chat-header-status {
  position: relative;
  color: #a1a1aa;
  font-size: 0.78rem;
  line-height: 1.1;
}

.chat-status-typing {
  background: linear-gradient(90deg, #e4e4e7 0%, #a1a1aa 50%, #e4e4e7 100%);
  background-size: 200% 100%;
  -webkit-background-clip: text;
  background-clip: text;
  color: transparent;
  animation: chatTypingGlow 1.2s ease-in-out infinite;
}

@keyframes chatTypingGlow {
  0% { background-position: 0% 50%; }
  100% { background-position: 200% 50%; }
}

#chat-messages-scroll {
  flex: 1;
  min-height: 0;
  overflow-y: auto;
  overflow-x: hidden;
  padding: 0.75rem;
  display: flex;
  flex-direction: column;
  gap: 0.5rem;
  -webkit-overflow-scrolling: touch;
}

.chat-message {
  display: flex;
  transition: transform 160ms ease, filter 160ms ease, opacity 160ms ease;
}

.chat-message.own {
  justify-content: flex-end;
}

.chat-message.other {
  justify-content: flex-start;
}

.chat-message-bubble {
  max-width: min(72ch, 78%);
  min-width: 11rem;
  border-radius: 1.4rem;
  padding: 0.55rem 0.72rem;
  border: 1px solid rgba(255, 255, 255, 0.08);
  background: rgba(20, 20, 22, 0.92);
  color: #f4f4f5;
  position: relative;
  box-shadow: 0 6px 18px rgba(0, 0, 0, 0.22);
}

.chat-message.own .chat-message-bubble {
  background: #ffffff;
  border-color: rgba(255, 255, 255, 0.65);
  color: #0b0b0b;
}

.chat-message.own .chat-message-meta {
  color: rgba(11, 11, 11, 0.6);
}

.chat-message-media {
  display: grid;
  gap: 0.45rem;
  margin-bottom: 0.35rem;
}

.chat-media-item {
  width: min(320px, 100%);
  border-radius: 0.7rem;
  border: 1px solid #3f3f46;
  background: #131316;
  overflow: hidden;
  position: relative;
  min-height: 120px;
}

.chat-media-item .chat-message-image,
.chat-media-item .chat-message-video {
  width: 100%;
  height: 100%;
  display: block;
  object-fit: cover;
}

.chat-media-placeholder {
  position: absolute;
  inset: 0;
  background: linear-gradient(90deg, #1b1b20 0%, #2a2a31 45%, #1b1b20 100%);
  background-size: 180% 100%;
  animation: chatShimmer 1.4s infinite ease;
  opacity: 0.8;
  transition: opacity 180ms ease;
}

.chat-media-fallback {
  position: absolute;
  inset: 0;
  display: flex;
  align-items: center;
  justify-content: center;
  color: #d4d4d8;
  font-size: 0.85rem;
  opacity: 0;
  transition: opacity 180ms ease;
  background: rgba(12, 12, 14, 0.7);
}

.chat-media-item.is-loaded .chat-media-placeholder {
  opacity: 0;
  pointer-events: none;
}

.chat-media-item.is-error .chat-media-fallback {
  opacity: 1;
}

.chat-message-image,
.chat-message-video {
  width: min(320px, 100%);
  border-radius: 0.7rem;
  border: 0;
  background: transparent;
}

.chat-message-file {
  display: inline-block;
  color: #e4e4e7;
  font-size: 0.9rem;
  text-decoration: none;
}

.chat-message-file:hover {
  color: #fafafa;
  text-decoration: underline;
}

.chat-message-text {
  white-space: pre-wrap;
  word-break: break-word;
  line-height: 1.35;
}

.chat-message-meta {
  /* margin-top: 0.32rem; */
  font-size: 0.74rem;
  color: #a1a1aa;
  text-align: right;
  display: flex;
  align-items: center;
  justify-content: flex-end;
  gap: 0.4rem;
}

.chat-message-read-dot {
  width: 0.42rem;
  height: 0.42rem;
  border-radius: 999px;
  background: #1b1206;
  box-shadow: 0 0 0 1px rgba(255, 255, 255, 0.12);
  /* position: absolute; */
  /* left: 0.45rem; */
  /* bottom: 0.45rem; */
}

.chat-message.pending .chat-message-bubble {
  opacity: 1;
}

.chat-message.error .chat-message-bubble {
  border-color: #ef4444;
}

.chat-reply-inline {
  border-left: 2px solid rgba(255, 255, 255, 0.25);
  padding-left: 0.5rem;
  margin-bottom: 0.35rem;
  font-size: 0.78rem;
  color: #d4d4d8;
}

.chat-reply-inline-label {
  font-size: 0.7rem;
  text-transform: uppercase;
  letter-spacing: 0.08em;
  color: #a1a1aa;
}

.chat-reply-inline-text {
  margin-top: 0.15rem;
}

.chat-message-skeletons {
  display: flex;
  flex-direction: column;
  gap: 0.4rem;
  padding: 0.3rem 0;
  opacity: 0;
  max-height: 0;
  overflow: hidden;
  transition: opacity 220ms ease, max-height 260ms ease;
}

.chat-message-skeletons.is-visible {
  opacity: 1;
}

.chat-message-skeletons.is-hiding {
  opacity: 0;
}

.chat-message-skeleton {
  height: 2.35rem;
  border-radius: 1rem;
  background: linear-gradient(90deg, #1b1b20 0%, #24242a 45%, #1b1b20 100%);
  background-size: 180% 100%;
  animation: chatSkeleton 1.2s ease infinite;
  max-width: 78%;
  min-width: 30%;
}

.chat-message-skeleton.left {
  align-self: flex-start;
}

.chat-message-skeleton.right {
  align-self: flex-end;
}

.chat-message-skeleton.short {
  width: 38%;
}

.chat-message-skeleton.medium {
  width: 56%;
}

.chat-message-skeleton.long {
  width: 74%;
}

@keyframes chatSkeleton {
  0% { background-position: 0% 0%; }
  100% { background-position: 180% 0%; }
}

@keyframes chatShimmer {
  0% { background-position: 0% 50%; }
  100% { background-position: 180% 50%; }
}

#chat-reply-preview {
  display: flex;
  align-items: center;
  gap: 0.5rem;
  padding: 0.45rem 0.6rem;
  border-radius: 0.7rem;
  background: rgba(255, 255, 255, 0.06);
  border: 1px solid rgba(255, 255, 255, 0.12);
  color: #e4e4e7;
}

#chat-reply-preview[hidden] {
  display: none !important;
}

.chat-reply-pill {
  flex: 1;
  overflow: hidden;
}

.chat-reply-meta {
  font-size: 0.7rem;
  color: #a1a1aa;
  text-transform: uppercase;
  letter-spacing: 0.08em;
}

.chat-reply-text {
  font-size: 0.85rem;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.chat-reply-cancel {
  width: 28px;
  height: 28px;
  border-radius: 50%;
  border: 1px solid rgba(255, 255, 255, 0.18);
  background: transparent;
  color: #f4f4f5;
  cursor: pointer;
}

.chat-context-menu {
  position: fixed;
  z-index: 9999;
  min-width: 160px;
  padding: 0.35rem;
  border-radius: 0.7rem;
  background: rgba(24, 24, 27, 0.96);
  border: 1px solid rgba(255, 255, 255, 0.12);
  box-shadow: 0 12px 28px rgba(0, 0, 0, 0.4);
  opacity: 0;
  transform: translateY(6px) scale(0.96);
  transform-origin: top left;
  pointer-events: none;
  transition: opacity 260ms cubic-bezier(0.16, 1, 0.3, 1), transform 300ms cubic-bezier(0.16, 1, 0.3, 1);
  will-change: transform, opacity;
}

.chat-context-menu.visible {
  opacity: 1;
  transform: translateY(0) scale(1);
  pointer-events: auto;
}

.chat-context-item {
  width: 100%;
  padding: 0.5rem 0.65rem;
  border-radius: 0.5rem;
  background: transparent;
  border: none;
  color: #f4f4f5;
  text-align: left;
  cursor: pointer;
  font-size: 0.9rem;
}

.chat-context-item:hover {
  background: rgba(255, 255, 255, 0.08);
}

.chat-context-reactions {
  display: flex;
  gap: 0.35rem;
  padding: 0.35rem 0.45rem;
  border-radius: 999px;
  border: 1px solid rgba(255, 255, 255, 0.12);
  background: rgba(15, 15, 18, 0.8);
  overflow-x: auto;
  scrollbar-width: none;
  -ms-overflow-style: none;
  margin-bottom: 0.45rem;
}

.chat-context-reactions::-webkit-scrollbar {
  width: 0;
  height: 0;
}

.chat-context-reaction {
  width: 32px;
  height: 32px;
  border-radius: 50%;
  border: 1px solid rgba(255, 255, 255, 0.12);
  background: rgba(255, 255, 255, 0.06);
  color: #f4f4f5;
  font-size: 1rem;
  cursor: pointer;
  flex: 0 0 auto;
  display: grid;
  place-items: center;
  transition: transform 120ms ease, background 120ms ease;
}

.chat-context-reaction:hover {
  background: rgba(255, 255, 255, 0.14);
  transform: scale(1.05);
}

.chat-context-actions {
  display: grid;
  gap: 0.25rem;
}

.chat-message-reactions {
  display: flex;
  gap: 0.25rem;
  /* margin-top: 0.25rem; */
  flex-wrap: wrap;
}

.chat-reaction-pill {
  display: inline-flex;
  align-items: center;
  gap: 0.25rem;
  padding: 0.14rem 0.4rem;
  border-radius: 999px;
  border: 1px solid rgba(255, 255, 255, 0.14);
  background: rgba(255, 255, 255, 0.06);
  color: #f4f4f5;
  font-size: 0.74rem;
  cursor: default;
}

.chat-message.own .chat-reaction-pill {
  border-color: rgba(12, 12, 12, 0.22);
  background: rgba(12, 12, 12, 0.08);
  color: #0b0b0b;
}

.chat-reaction-pill.active {
  border-color: rgba(255, 255, 255, 0.35);
  background: rgba(255, 255, 255, 0.12);
}

.chat-message.own .chat-reaction-pill.active {
  border-color: rgba(12, 12, 12, 0.38);
  background: rgba(12, 12, 12, 0.16);
}

.chat-reaction-pill.pop {
  animation: reactionPop 140ms ease-out;
}

.chat-message-pending-icon {
  width: 0.55rem;
  height: 0.55rem;
  border-radius: 999px;
  border: 2px solid rgba(255, 255, 255, 0.55);
  border-top-color: transparent;
  display: inline-block;
  margin-right: 0.25rem;
  animation: chatPendingSpin 700ms linear infinite;
}

.chat-message.own .chat-message-pending-icon {
  border-color: rgba(12, 12, 12, 0.5);
  border-top-color: transparent;
}

@keyframes chatPendingSpin {
  0% { transform: rotate(0deg); }
  100% { transform: rotate(360deg); }
}

@keyframes reactionPop {
  0% { transform: scale(0.7); opacity: 0.6; }
  100% { transform: scale(1); opacity: 1; }
}

.chat-message[data-animate="1"] {
  animation: chatMessageIn 420ms cubic-bezier(0.16, 1, 0.3, 1);
  will-change: transform, opacity, filter;
}

@keyframes chatMessageIn {
  0% { opacity: 0; transform: translateY(12px) scale(0.96); filter: blur(3px); }
  70% { opacity: 1; transform: translateY(-1px) scale(1.002); filter: blur(0.6px); }
  100% { opacity: 1; transform: translateY(0) scale(1); filter: blur(0); }
}

#chat-attachments {
  display: none;
  flex-wrap: wrap;
  gap: 0.35rem;
  padding: 0.4rem 0.75rem 0;
}

.chat-attachment-chip {
  display: inline-flex;
  align-items: center;
  gap: 0.4rem;
  max-width: 100%;
  border: 1px solid #3f3f46;
  background: #18181b;
  border-radius: 999px;
  padding: 0.18rem 0.55rem;
}

.chat-attachment-name {
  color: #e4e4e7;
  font-size: 0.75rem;
  max-width: 190px;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.chat-attachment-remove {
  border: 0;
  background: transparent;
  color: #a1a1aa;
  font-size: 1rem;
  cursor: pointer;
  line-height: 1;
}

#chat-composer {
  display: flex;
  flex-direction: column;
  align-items: stretch;
  gap: 0.4rem;
  border-top: 1px solid #27272a;
  padding: 0.5rem 0.7rem;
  background: #09090b;
  flex-shrink: 0;
}

.chat-composer-row {
  display: flex;
  align-items: flex-end;
  gap: 0.4rem;
}

#chat-attach-button {
  width: 2.2rem;
  height: 2.2rem;
  border-radius: 0.65rem;
  border: 1px solid #3f3f46;
  background: #18181b;
  color: #e4e4e7;
  font-size: 1.1rem;
  line-height: 1;
  cursor: pointer;
}

#chat-attach-button:hover {
  background: #27272a;
}

#chat-input {
  flex: 1;
  min-height: 2.2rem;
  max-height: 8.2rem;
  border-radius: 0.9rem;
  border: 1px solid #3f3f46;
  background: #18181b;
  color: var(--text-color);
  resize: none;
  outline: none;
  padding: 0.55rem 0.7rem;
  font-size: 0.95rem;
  line-height: 1.35;
}

#chat-input:focus {
  border-color: #71717a;
}

#chat-send-button {
  height: 2.2rem;
  border-radius: 0.65rem;
  border: 1px solid #52525b;
  background: #27272a;
  color: #fafafa;
  font-size: 0.83rem;
  font-weight: 600;
  padding: 0 0.8rem;
  cursor: pointer;
}

#chat-send-button:hover {
  background: #3f3f46;
}

#chat-send-button:disabled {
  opacity: 0.65;
  cursor: default;
}

#chat-scroll-button {
  position: absolute;
  right: 0.9rem;
  bottom: 4.2rem;
  display: flex;
  align-items: center;
  gap: 0.45rem;
  padding: 0.35rem 0.6rem;
  border-radius: 999px;
  border: 1px solid rgba(255, 255, 255, 0.18);
  background: rgba(24, 24, 27, 0.95);
  color: #f9fafb;
  font-size: 0.85rem;
  font-weight: 600;
  box-shadow: 0 10px 24px rgba(0, 0, 0, 0.4);
  opacity: 0;
  pointer-events: none;
  transform: translateY(8px);
  transition: all 160ms ease;
  z-index: 2;
}

#chat-scroll-button.visible {
  opacity: 1;
  pointer-events: auto;
  transform: translateY(0);
}

#chat-scroll-button .chat-scroll-arrow {
  font-size: 0.95rem;
}

#chat-scroll-button .chat-scroll-count {
  min-width: 1.2rem;
  height: 1.2rem;
  border-radius: 999px;
  background: #f97316;
  color: #09090b;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  font-size: 0.72rem;
  font-weight: 700;
  padding: 0 0.35rem;
}

@media (max-width: 1024px) {
  body.messages-screen-active.chat-open-mobile {
    position: relative !important;
    width: 100% !important;
    height: 100dvh !important;
    max-height: 100dvh !important;
    padding: 0 !important;
    overflow: hidden !important;
    overscroll-behavior: none !important;
    touch-action: none;
    scroll-behavior: auto !important;
  }

  body.messages-screen-active.chat-open-mobile #screens-div {
    height: 100dvh !important;
    max-height: 100dvh !important;
  }

  body.messages-screen-active.chat-open-mobile #screens-div,
  body.messages-screen-active.chat-open-mobile .screen-div,
  body.messages-screen-active.chat-open-mobile #messages-app,
  body.messages-screen-active.chat-open-mobile #messages-chat-panel,
  body.messages-screen-active.chat-open-mobile #chat-view {
    padding-top: 0;
    height: 100% !important;
    max-height: 100% !important;
    min-height: 0 !important;
    overflow: hidden !important;
  }

  body.messages-screen-active.chat-open-mobile #message-screen {
    position: fixed !important;
    left: 0 !important;
    right: 0 !important;
    top: var(--shell-safe-top) !important;
    bottom: auto !important;
    height: calc(100dvh - var(--shell-safe-top)) !important;
    max-height: calc(100dvh - var(--shell-safe-top)) !important;
    overflow: hidden !important;
    z-index: 20;
    background: #09090b;
  }

  body.messages-screen-active.chat-open-mobile #chat-messages-scroll {
    min-height: 0;
    overflow-y: auto;
    overscroll-behavior: none;
    -webkit-overflow-scrolling: auto;
    touch-action: pan-y;
    scroll-behavior: auto !important;
  }

  body.messages-screen-active.chat-open-mobile #message-screen-chats {
    touch-action: pan-y;
  }

  #messages-app.chat-open #messages-list-panel {
    display: none; 
  }

  #messages-chat-panel {
    display: none;
  }

  #messages-app.chat-open #messages-chat-panel {
    display: flex;
  }

  #chat-empty-state {
    display: none !important;
  } 

  #chat-view {
    padding-bottom: 0;
  }

  body.chat-open-mobile footer.main-blocks {
    display: none;
  }

  body.messages-screen-active.chat-open-mobile #screens-div {
    padding-bottom: 0 !important;
  }

  body.messages-screen-active.chat-open-mobile .chat-header {
    padding: 0.35rem 0.7rem;
    min-height: 3rem;
  }

  body.messages-screen-active.chat-open-mobile #chat-composer {
    padding: 0.35rem 0.6rem !important;
  }

  body.messages-screen-active.chat-open-mobile #chat-scroll-button {
    bottom: 4.6rem;
    right: 0.7rem;
  }

  .chat-message-bubble {
    border: none !important;
  }

}

@media (min-width: 1025px) {
  #messages-chat-panel {
    display: flex;
    height: 100%;
    overflow: hidden;
  }

  #chat-back-button {
    display: none;
  }

  #chat-composer {
    padding-bottom: 0.65rem;
  }

  #chat-view {
    height: 100%;
    overflow: hidden;
  }

  #chat-messages-scroll {
    min-height: 0;
    overflow-y: auto;
  }
}
.chat-messages-dim .chat-message {
  filter: blur(2px);
  opacity: 0.45;
}

.chat-messages-dim .chat-message.is-focused {
  filter: none;
  opacity: 1;
  transform: translateY(-4px) scale(1.02);
}
