.message-button-wrap {
  position: relative;
  display: inline-block;
}

.message-button {
  position: relative;
}

.message-badge {
  position: absolute;
  top: -6px;
  right: -6px;
  min-width: 18px;
  height: 18px;
  padding: 0 4px;
  display: flex;
  align-items: center;
  justify-content: center;
  border: 2px solid #ffffff;
  border-radius: 999px;
  background: #e02424;
  color: #ffffff;
  font-size: 11px;
  font-weight: 800;
  box-shadow: 0 2px 4px rgba(0, 0, 0, 0.15);
}

.message-button.has-unread {
  color: #17617a;
  border-color: #8ec8dc;
  background: #eaf8fd;
  box-shadow: 0 0 0 4px rgba(23, 97, 122, 0.08);
}

.messages-dropdown {
  position: absolute;
  top: calc(100% + 12px);
  right: 0;
  z-index: 1000;
  width: min(380px, calc(100vw - 24px));
  overflow: hidden;
  border: 1px solid rgba(226, 232, 240, 0.8);
  border-radius: 16px;
  background: rgba(255, 255, 255, 0.95);
  backdrop-filter: blur(16px);
  -webkit-backdrop-filter: blur(16px);
  box-shadow: 0 20px 25px -5px rgba(0, 0, 0, 0.1), 0 10px 10px -5px rgba(0, 0, 0, 0.04), 0 0 0 1px rgba(0, 0, 0, 0.02);
  transform-origin: top right;
  animation: dropdownSlideIn 0.25s cubic-bezier(0.16, 1, 0.3, 1) forwards;
}

@keyframes dropdownSlideIn {
  from {
    opacity: 0;
    transform: translateY(12px) scale(0.97);
  }
  to {
    opacity: 1;
    transform: translateY(0) scale(1);
  }
}

.messages-dropdown-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 16px 20px;
  background: transparent;
  border-bottom: 1px solid rgba(226, 232, 240, 0.8);
}

.messages-dropdown-header h3 {
  margin: 0;
  font-size: 18px;
  font-weight: 800;
  color: #1e293b;
  letter-spacing: -0.02em;
}

.messages-dropdown-content {
  max-height: 360px;
  overflow-y: auto;
  padding: 8px;
}

.messages-dropdown-content::-webkit-scrollbar {
  width: 6px;
}
.messages-dropdown-content::-webkit-scrollbar-track {
  background: transparent;
}
.messages-dropdown-content::-webkit-scrollbar-thumb {
  background: rgba(148, 163, 184, 0.3);
  border-radius: 3px;
}
.messages-dropdown-content::-webkit-scrollbar-thumb:hover {
  background: rgba(148, 163, 184, 0.5);
}

.messages-dropdown-footer {
  padding: 14px 20px;
  background: rgba(248, 250, 252, 0.9);
  text-align: center;
  border-top: 1px solid rgba(226, 232, 240, 0.8);
}

.messages-dropdown-footer a {
  color: #17617a;
  font-size: 14px;
  font-weight: 700;
  text-decoration: none;
  transition: color 0.2s ease;
  display: inline-block;
}

.messages-dropdown-footer a:hover {
  color: #0d3d4e;
  text-decoration: underline;
}

.message-toast {
  position: fixed;
  top: 82px;
  right: 18px;
  z-index: 1200;
  width: min(360px, calc(100vw - 36px));
  padding: 12px;
  display: flex;
  align-items: center;
  gap: 12px;
  border: 1px solid #cfe0e9;
  border-radius: 14px;
  background: #ffffff;
  color: #102535;
  text-decoration: none;
  box-shadow: 0 16px 38px rgba(37, 83, 104, 0.22);
  animation: messageToastIn 0.22s ease-out;
}

.message-toast.is-hiding {
  opacity: 0;
  transform: translateY(-8px);
  transition: opacity 0.22s ease, transform 0.22s ease;
}

.message-toast strong,
.message-toast span {
  display: block;
  min-width: 0;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.message-toast strong {
  margin-bottom: 3px;
  font-size: 14px;
  font-weight: 900;
}

.message-toast span {
  color: #60727e;
  font-size: 13px;
}

@keyframes messageToastIn {
  from {
    opacity: 0;
    transform: translateY(-8px);
  }

  to {
    opacity: 1;
    transform: translateY(0);
  }
}

.chat-sidebar-item {
  display: flex;
  align-items: center;
  gap: 12px;
  width: 100%;
  border: 0;
  border-bottom: 1px solid #eef3f6;
  background: transparent;
  color: inherit;
  text-align: left;
  text-decoration: none;
  cursor: pointer;
}

.chat-sidebar-item:hover {
  background: #f2f8fb;
}

.chat-sidebar-item.is-unread {
  background: #eef8fc;
}

.dropdown-chat-item {
  display: flex;
  align-items: center;
  gap: 14px;
  padding: 12px 14px;
  margin-bottom: 4px;
  border: 0;
  border-radius: 12px;
  background: transparent;
  color: #334155;
  text-decoration: none;
  transition: all 0.2s cubic-bezier(0.4, 0, 0.2, 1);
  cursor: pointer;
}

.dropdown-chat-item:last-child {
  margin-bottom: 0;
}

.dropdown-chat-item:hover {
  background: rgba(23, 97, 122, 0.05);
  transform: translateX(2px);
}

.dropdown-chat-item.is-unread {
  background: rgba(23, 97, 122, 0.08);
}

.dropdown-chat-item.is-unread:hover {
  background: rgba(23, 97, 122, 0.12);
}

.chat-item-info {
  flex: 1;
  min-width: 0;
  display: flex;
  flex-direction: column;
  gap: 3px;
}

.chat-item-row {
  display: flex;
  justify-content: space-between;
  align-items: baseline;
  gap: 8px;
}

.chat-item-name {
  font-weight: 700;
  font-size: 14px;
  color: #1e293b;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.dropdown-chat-item.is-unread .chat-item-name {
  color: #0f172a;
}

.chat-item-time {
  font-size: 11px;
  color: #94a3b8;
  white-space: nowrap;
}

.chat-item-snippet {
  font-size: 13px;
  color: #64748b;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
  max-width: 230px;
}

.dropdown-chat-item.is-unread .chat-item-snippet {
  font-weight: 600;
  color: #334155;
}

.unread-dot {
  width: 8px;
  height: 8px;
  background-color: #17617a;
  border-radius: 50%;
  flex-shrink: 0;
  box-shadow: 0 0 8px #17617a;
}

.dropdown-empty-state,
.chat-sidebar-loading {
  padding: 32px 20px;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 8px;
  color: #64748b;
  text-align: center;
  font-size: 14px;
}

.dropdown-empty-state strong {
  color: #1e293b;
  font-size: 15px;
  font-weight: 700;
}

.dropdown-empty-state span {
  font-size: 13px;
  color: #94a3b8;
}

.chat-item-avatar-wrap,
.chat-header-avatar {
  width: 48px;
  height: 48px;
  flex: 0 0 auto;
  display: flex;
  align-items: center;
  justify-content: center;
  overflow: hidden;
  border: 1px solid #cfe0e9;
  border-radius: 50%;
  background: #17617a;
  color: #ffffff;
  font-weight: 800;
}

.chat-header-avatar {
  width: 44px;
  height: 44px;
}

.chat-item-avatar-wrap img,
.chat-header-avatar img {
  width: 100%;
  height: 100%;
  object-fit: cover;
}

.chat-avatar-fallback {
  font-size: 16px;
}

.chat-item-info,
.chat-header-info {
  min-width: 0;
}

.chat-item-info {
  flex: 1 1 auto;
  display: flex;
  flex-direction: column;
  gap: 4px;
}

.chat-item-row {
  display: flex;
  align-items: baseline;
  justify-content: space-between;
  gap: 8px;
}

.chat-item-name,
.chat-item-snippet {
  min-width: 0;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.chat-item-name {
  color: #102535;
  font-size: 14px;
  font-weight: 800;
}

.chat-item-snippet {
  color: #60727e;
  font-size: 13px;
}

.chat-item-time {
  flex: 0 0 auto;
  color: #7a8d98;
  font-size: 12px;
}

.unread-dot {
  width: 8px;
  height: 8px;
  flex: 0 0 auto;
  border-radius: 50%;
  background: #17617a;
}

.messenger-page {
  min-height: 100vh;
}

.messenger-shell {
  width: min(1180px, calc(100% - 32px));
  min-height: calc(100vh - 96px);
  margin: 18px auto;
  display: flex;
}

.messenger-card {
  width: 100%;
  min-height: min(760px, calc(100vh - 132px));
  display: grid;
  grid-template-columns: minmax(280px, 340px) minmax(0, 1fr);
  overflow: hidden;
  border: 1px solid #cfe0e9;
  border-radius: 14px;
  background: #ffffff;
  box-shadow: 0 18px 42px rgba(37, 83, 104, 0.08);
}

.messenger-sidebar {
  min-width: 0;
  display: flex;
  flex-direction: column;
  border-right: 1px solid #dbe8ef;
  background: #ffffff;
}

.messenger-sidebar-head {
  min-height: 78px;
  padding: 16px 18px;
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 14px;
  border-bottom: 1px solid #e4eef3;
}

.messenger-sidebar-head small {
  color: #17617a;
  font-size: 12px;
  font-weight: 900;
}

.messenger-sidebar-head h1 {
  margin: 2px 0 0;
  color: #102535;
  font-size: 25px;
  line-height: 1.05;
}

.messenger-count {
  min-width: 30px;
  height: 30px;
  padding: 0 10px;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  border-radius: 999px;
  background: #eaf5f9;
  color: #17617a;
  font-size: 13px;
  font-weight: 900;
}

.chat-sidebar-list {
  min-height: 0;
  flex: 1 1 auto;
  overflow-y: auto;
}

.chat-sidebar-item {
  padding: 13px 16px;
}

.chat-sidebar-item.is-active {
  background: #eaf5f9;
  box-shadow: inset 4px 0 0 #17617a;
}

.chat-window {
  min-width: 0;
  min-height: 0;
  display: flex;
  flex-direction: column;
  background: #f5f9fb;
}

.chat-window-empty {
  flex: 1 1 auto;
  padding: 32px;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 10px;
  color: #60727e;
  text-align: center;
}

.chat-window-empty h2 {
  margin: 0;
  color: #102535;
  font-size: 22px;
}

.chat-window-empty span {
  max-width: 360px;
  line-height: 1.45;
}

.chat-window-active {
  min-height: 0;
  height: 100%;
  display: flex;
  flex-direction: column;
}

.chat-window-header {
  min-height: 72px;
  padding: 14px 20px;
  display: flex;
  align-items: center;
  gap: 12px;
  border-bottom: 1px solid #dbe8ef;
  background: #ffffff;
}

.chat-header-info h2 {
  margin: 0;
  color: #102535;
  font-size: 17px;
  line-height: 1.2;
}

.chat-header-info span {
  display: block;
  margin-top: 3px;
  color: #60727e;
  font-size: 12px;
  font-weight: 700;
}

.chat-messages {
  min-height: 0;
  flex: 1 1 auto;
  padding: 22px;
  display: flex;
  flex-direction: column;
  gap: 12px;
  overflow-y: auto;
}

.message-row {
  max-width: min(72%, 620px);
  display: flex;
  flex-direction: column;
}

.message-row.incoming {
  align-self: flex-start;
  align-items: flex-start;
}

.message-row.outgoing {
  align-self: flex-end;
  align-items: flex-end;
}

.message-bubble {
  padding: 10px 16px;
  border-radius: 18px;
  font-size: 14px;
  line-height: 1.45;
  word-break: break-word;
}

.message-row.incoming .message-bubble {
  border-bottom-left-radius: 5px;
  background: #e4e9ed;
  color: #102535;
}

.message-row.outgoing .message-bubble {
  border-bottom-right-radius: 5px;
  background: #17617a;
  color: #ffffff;
}

.message-time {
  margin-top: 4px;
  padding: 0 5px;
  color: #7a8d98;
  font-size: 10px;
}

.chat-input-form {
  padding: 14px 18px;
  display: flex;
  align-items: center;
  gap: 12px;
  border-top: 1px solid #dbe8ef;
  background: #ffffff;
}

#chatMessageInput {
  min-width: 0;
  height: 42px;
  flex: 1 1 auto;
  padding: 0 18px;
  border: 1px solid #cfe0e9;
  border-radius: 999px;
  background: #f5f9fb;
  font: inherit;
  outline: 0;
}

#chatMessageInput:focus {
  border-color: #17617a;
  background: #ffffff;
}

#chatSendBtn {
  width: 42px;
  height: 42px;
  flex: 0 0 auto;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  border: 0;
  border-radius: 50%;
  background: #17617a;
  color: #ffffff;
  cursor: pointer;
}

#chatSendBtn:hover {
  background: #124d62;
}

#chatSendBtn svg {
  width: 20px;
  height: 20px;
  margin-left: 2px;
  fill: none;
  stroke: currentColor;
  stroke-linecap: round;
  stroke-linejoin: round;
  stroke-width: 1.8;
}

.chat-back-button {
  background: transparent;
  border: none;
  color: #64748b;
  cursor: pointer;
  padding: 4px;
  display: none;
  align-items: center;
  justify-content: center;
  margin-right: 8px;
}
.chat-back-button svg {
  width: 24px;
  height: 24px;
  stroke: currentColor;
  stroke-width: 2.5;
}
.chat-back-button:hover {
  color: #17617a;
}

@media (max-width: 820px) {
  body.messenger-page {
    overflow: hidden !important;
    height: 100vh !important;
    margin: 0 !important;
    display: flex !important;
    flex-direction: column !important;
  }

  .messenger-shell {
    width: 100% !important;
    height: calc(100vh - 60px - 64px) !important;
    min-height: 0 !important;
    margin: 0 !important;
  }

  .messenger-card {
    height: 100% !important;
    min-height: 0 !important;
    grid-template-columns: 1fr !important;
    grid-template-rows: 1fr !important;
    border-width: 1px 0 0 !important;
    border-radius: 0 !important;
    overflow: hidden !important;
  }

  .messenger-sidebar {
    height: 100% !important;
    max-height: none !important;
    border-right: 0 !important;
    background: #ffffff !important;
    padding: 16px !important;
    display: flex !important;
    flex-direction: column !important;
    overflow-y: auto !important;
  }

  .messenger-sidebar-head {
    display: flex !important;
    align-items: center !important;
    justify-content: space-between !important;
    margin-bottom: 16px !important;
  }

  .chat-sidebar-list {
    display: flex !important;
    flex-direction: column !important;
    overflow-y: auto !important;
    overflow-x: hidden !important;
    padding: 0 !important;
    gap: 8px !important;
    white-space: normal !important;
  }

  .chat-sidebar-item {
    display: flex !important;
    flex-direction: row !important;
    align-items: center !important;
    justify-content: flex-start !important;
    padding: 12px !important;
    width: 100% !important;
    height: auto !important;
    border-radius: 12px !important;
    border-bottom: 1px solid #eef3f6 !important;
    box-shadow: none !important;
    margin: 0 !important;
    background: transparent !important;
  }

  .chat-sidebar-item:hover {
    background: #f2f8fb !important;
  }

  .chat-sidebar-item.is-active {
    background: #eaf8fd !important;
  }

  .chat-sidebar-item.is-unread {
    background: #eef8fc !important;
  }

  .chat-item-avatar-wrap {
    width: 44px !important;
    height: 44px !important;
    margin: 0 !important;
    border-radius: 50% !important;
    flex-shrink: 0 !important;
  }

  .chat-item-info {
    display: flex !important;
    flex-direction: column !important;
    align-items: flex-start !important;
    margin-left: 12px !important;
    flex: 1 !important;
    min-width: 0 !important;
  }

  .chat-window {
    height: 100% !important;
    width: 100% !important;
    display: flex !important;
    flex-direction: column !important;
    min-height: 0 !important;
  }

  .chat-window-active {
    height: 100% !important;
    min-height: 0 !important;
  }

  .chat-window-header {
    min-height: 54px !important;
    padding: 8px 14px !important;
    flex-shrink: 0 !important;
  }

  .chat-back-button {
    display: inline-flex !important;
  }

  .chat-messages {
    padding: 14px 12px 80px !important;
    flex: 1 1 auto !important;
    overflow-y: auto !important;
    min-height: 0 !important;
  }

  .message-row {
    max-width: 86% !important;
  }

  .chat-input-form {
    position: fixed !important;
    left: 0 !important;
    right: 0 !important;
    bottom: 64px !important;
    z-index: 500 !important;
    padding: 10px 14px !important;
    background: #ffffff !important;
    border-top: 1px solid #dbe8ef !important;
    box-shadow: 0 -2px 8px rgba(0,0,0,0.07) !important;
    flex-shrink: 0 !important;
  }

  /* List & Detail View Toggle classes */
  body.has-active-chat .messenger-sidebar {
    display: none !important;
  }

  body:not(.has-active-chat) .chat-window {
    display: none !important;
  }

  body.has-active-chat .chat-window {
    display: flex !important;
  }
}

@media (max-width: 520px) {
  .messages-dropdown {
    position: fixed;
    top: 68px;
    right: 10px;
    left: 10px;
    width: auto;
  }
}
