.messages-layout {
  display: flex;
  flex-direction: column;

  height: 100vh;
  min-height: 0;
}

.messages-page {
  display: flex;
  flex: 1;
  flex-direction: column;

  min-height: 0;
  width: 100%;

  padding: 2rem;
  box-sizing: border-box;

  overflow: hidden;
}

/* --------------------------------------------------
   Page Header
-------------------------------------------------- */

.messages-page__header {
  display: block;
  margin-bottom: 1rem;
}

.messages-page__header h1 {
  margin: 0;
  color: var(--heading-color);
  font-size: 1.8rem;
  line-height: 1.2;
}

.messages-page__header p {
  margin: 0.35rem 0 0;
  color: var(--text-light-color);
  font-size: 0.9rem;
  line-height: 1.4;
}

/* --------------------------------------------------
   Messages Layout
-------------------------------------------------- */

.messages {
  display: grid;
  grid-template-columns: 320px minmax(0, 1fr);
  grid-template-rows: minmax(0, 1fr);

  width: 100%;
  flex: 1;

  min-height: 0;

  overflow: hidden;

  background: var(--card-background-color);

  border: 1px solid var(--border-color);
  border-radius: var(--radius-lg);
}

/* --------------------------------------------------
   Conversations
-------------------------------------------------- */

.messages__conversations {
  display: flex;
  flex-direction: column;
  min-width: 0;
  min-height: 0;
  overflow-y: auto;

  border-right: 1px solid var(--border-color);
  background: var(--secondary-background-color);
}

.messages__search {
  padding: 1rem;

  border-bottom: 1px solid var(--border-color);
}

.messages__search input {
  width: 100%;
  box-sizing: border-box;

  padding: 0.7rem 0.85rem;

  border: 1px solid var(--border-color);
  border-radius: var(--radius-sm);

  outline: none;

  background: var(--card-background-color);
  color: var(--text-color);

  font: inherit;
  font-size: 0.85rem;
}

.messages__search input:focus {
  border-color: var(--primary-color);
}

.conversation-list {
  flex: 1;

  overflow-y: auto;
}

.conversation {
  display: flex;
  align-items: center;

  gap: 0.75rem;

  width: 100%;

  padding: 0.9rem 1rem;

  border: 0;
  border-bottom: 1px solid var(--border-color);

  background: transparent;
  color: var(--text-color);

  text-align: left;

  cursor: pointer;

  transition: background-color 0.15s ease;
}

.conversation:hover {
  background: var(--card-background-color);
}

.conversation--active {
  background: var(--card-background-color);
}

.conversation--unread {
  font-weight: 600;
}

.conversation__avatar {
  display: flex;
  align-items: center;
  justify-content: center;

  flex-shrink: 0;

  width: 42px;
  height: 42px;

  border-radius: 50%;

  background: var(--tertiary-color);

  font-size: 1.25rem;
}

.conversation__content {
  min-width: 0;
  flex: 1;
}

.conversation__unread-badge {
  display: inline-flex;
  align-items: center;
  justify-content: center;

  min-width: 1.25rem;
  height: 1.25rem;

  margin-left: 0.4rem;
  padding: 0 0.35rem;

  border-radius: 999px;

  background: var(--primary-color);
  color: #ffffff;

  font-size: 0.7rem;
  font-weight: 700;
  line-height: 1;
}

.conversation__top {
  display: flex;
  align-items: center;
  justify-content: space-between;

  gap: 0.5rem;
}

.conversation__top strong {
  overflow: hidden;

  color: var(--text-color);

  font-size: 0.88rem;

  text-overflow: ellipsis;
  white-space: nowrap;
}

.conversation__top span {
  flex-shrink: 0;

  color: var(--text-light-color);

  font-size: 0.7rem;
}

.conversation__content p {
  margin: 0.3rem 0 0;

  overflow: hidden;

  color: var(--text-light-color);

  font-size: 0.78rem;

  text-overflow: ellipsis;
  white-space: nowrap;
}

/* --------------------------------------------------
   Chat
-------------------------------------------------- */

.chat {
  display: flex;
  flex-direction: column;

  min-width: 0;
  min-height: 0;

  background: var(--card-background-color);
}

.chat__header {
  display: flex;
  align-items: center;
  justify-content: space-between;

  flex-shrink: 0;

  padding: 0.9rem 1.25rem;

  border-bottom: 1px solid var(--border-color);

  background: var(--card-background-color);
}

.chat__user {
  display: flex;
  align-items: center;

  gap: 0.75rem;
}

.chat__back-button {
  display: none;
}

.chat__avatar {
  display: flex;
  align-items: center;
  justify-content: center;

  width: 42px;
  height: 42px;

  border-radius: 50%;

  background: var(--tertiary-color);

  font-size: 1.25rem;
}

.chat__user strong {
  display: block;

  color: var(--text-color);

  font-size: 0.9rem;
}

.chat__status {
  display: flex;
  align-items: center;

  gap: 0.35rem;

  margin-top: 0.2rem;

  color: var(--text-light-color);

  font-size: 0.72rem;
}

.chat__status-dot {
  width: 7px;
  height: 7px;

  border-radius: 50%;

  background: var(--success-color);
}

.chat__menu-wrapper {
  position: relative;
}

.chat__menu {
  border: 0;
  background: transparent;
  color: var(--text-light-color);
  font-size: 1.4rem;
  line-height: 1;
  padding: 0.35rem 0.5rem;
  border-radius: 0.4rem;
  cursor: pointer;
  transition:
    color 0.2s ease,
    background-color 0.2s ease;
}

.chat__menu:hover {
  color: var(--text-color);
  background-color: var(--secondary-background-color);
}

.chat__menu-dropdown {
  position: absolute;
  top: calc(100% + 0.5rem);
  right: 0;
  min-width: 180px;
  padding: 0.35rem;
  background: var(--background-color);
  border: 1px solid var(--secondary-background-color);
  border-radius: 0.5rem;
  box-shadow: 0 4px 12px rgb(0 0 0 / 10%);
  opacity: 0;
  visibility: hidden;
  transform: translateY(-0.25rem);
  transition:
    opacity 0.2s ease,
    visibility 0.2s ease,
    transform 0.2s ease;
  z-index: 10;
}

.chat__menu-dropdown--open {
  opacity: 1;
  visibility: visible;
  transform: translateY(0);
}

.chat__menu-item {
  width: 100%;
  padding: 0.6rem 0.75rem;
  border: 0;
  border-radius: 0.35rem;
  background: transparent;
  color: var(--text-color);
  font: inherit;
  font-size: 0.85rem;
  text-align: left;
  cursor: pointer;
  transition:
    background-color 0.2s ease,
    color 0.2s ease;
}

.chat__menu-item:hover {
  background-color: var(--secondary-background-color);
}

.chat__menu-item--danger {
  color: var(--error-color);
}

.chat__menu-item--danger:hover {
  background-color: rgb(220 38 38 / 8%);
}
/* --------------------------------------------------
   Chat Messages
-------------------------------------------------- */

.chat__loading {
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 0.75rem;

  width: 100%;
  height: 100%;
  min-height: 200px;

  color: var(--text-color-of-background);
  font-size: 0.875rem;
}

.chat__loading-spinner {
  width: 32px;
  height: 32px;

  border: 3px solid var(--tertiary-color);
  border-top-color: var(--primary-color);
  border-radius: 50%;

  animation: chat-loading-spin 0.8s linear infinite;
}

@keyframes chat-loading-spin {
  to {
    transform: rotate(360deg);
  }
}

.chat__messages {
  display: flex;
  flex: 1;
  flex-direction: column;

  gap: 1rem;

  min-height: 0;

  padding: 1.5rem;

  overflow-y: auto;
}

.chat__date {
  display: flex;
  align-items: center;
  justify-content: center;

  margin: 0.25rem 0 0.5rem;
}

.chat__date span {
  padding: 0.25rem 0.65rem;

  border-radius: 999px;

  background: var(--secondary-background-color);

  color: var(--text-light-color);

  font-size: 0.7rem;
}

/* --------------------------------------------------
   Message
-------------------------------------------------- */

.message {
  display: flex;
  align-items: flex-end;

  gap: 0.6rem;

  max-width: 75%;
}

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

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

.message__avatar {
  display: flex;
  align-items: center;
  justify-content: center;

  flex-shrink: 0;

  width: 32px;
  height: 32px;

  border-radius: 50%;

  background: var(--tertiary-color);

  font-size: 0.95rem;
}

.message__body {
  display: flex;
  flex-direction: column;

  gap: 0.25rem;
}

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

.message__sender {
  display: block;

  margin-bottom: 0.2rem;

  color: var(--text-light-color);

  font-size: 0.72rem;
  font-weight: 600;
}

.message__bubble {
  padding: 0.7rem 0.9rem;

  border-radius: var(--radius-md);

  color: var(--text-color);

  font-size: 0.86rem;
  line-height: 1.5;

  overflow-wrap: anywhere;
}

.message--incoming .message__bubble {
  border-bottom-left-radius: 4px;

  background: var(--secondary-background-color);
}

.message--outgoing .message__bubble {
  border-bottom-right-radius: 4px;

  background: var(--primary-color);

  color: var(--text-color-of-primary);
}

.message__time {
  color: var(--text-light-color);

  font-size: 0.65rem;
}

/* --------------------------------------------------
   Message Form
-------------------------------------------------- */

.chat__form {
  display: flex;
  align-items: center;
  gap: 0.65rem;
  flex-shrink: 0;
  padding: 1rem 1.25rem;
  border-top: 1px solid var(--border-color);
  background: var(--card-background-color);
  position: relative;
}

.chat__attachment,
.chat__emoji {
  display: flex;
  align-items: center;
  justify-content: center;

  flex-shrink: 0;

  width: 38px;
  height: 38px;

  border: 0;
  border-radius: 50%;

  background: transparent;

  color: var(--text-light-color);

  font-size: 1rem;

  cursor: pointer;
}

.chat__attachment:hover,
.chat__emoji:hover {
  background: var(--secondary-background-color);
}

.chat__emoji-picker {
  position: absolute;
  right: 3.5rem;
  bottom: calc(100% + 0.5rem);
  display: grid;
  grid-template-columns: repeat(8, 1fr);
  gap: 0.25rem;
  width: 320px;
  max-height: 260px;
  padding: 0.75rem;
  overflow-y: auto;
  border: 1px solid var(--border-color);
  border-radius: var(--radius-md);
  background: var(--card-background-color);
  box-shadow: var(--shadow-md);
  z-index: 20;
}

.chat__emoji-picker[hidden] {
  display: none;
}

.chat__emoji-picker button {
  display: flex;
  align-items: center;
  justify-content: center;
  width: 32px;
  height: 32px;
  padding: 0;
  border: 0;
  border-radius: 6px;
  background: transparent;
  font-size: 1.2rem;
  cursor: pointer;
  transition: background-color 0.15s ease;
}

.chat__emoji-picker button:hover {
  background: var(--secondary-background-color);
}

.chat__input-wrapper {
  display: flex;
  align-items: center;

  min-width: 0;
  flex: 1;

  padding: 0 0.75rem;

  border: 1px solid var(--border-color);
  border-radius: var(--radius-md);

  background: var(--card-background-color);
}

.chat__input {
  width: 100%;
  min-width: 0;

  padding: 0.7rem 0;

  border: 0;
  outline: none;

  background: transparent;

  color: var(--text-color);

  font: inherit;
  font-size: 0.85rem;
}

.chat__input::placeholder {
  color: var(--text-light-color);
}

.chat__send {
  display: flex;
  align-items: center;
  justify-content: center;

  flex-shrink: 0;

  width: 40px;
  height: 40px;

  border: 0;
  border-radius: var(--radius-sm);

  background: var(--primary-color);

  color: var(--text-color-of-primary);

  font-size: 1rem;

  cursor: pointer;

  transition: background-color 0.15s ease;
}

.chat__send:hover {
  background: var(--hover-color-of-button-primary);
}

/* --------------------------------------------------
   Conversation Sections
-------------------------------------------------- */

.conversation-section {
  border-bottom: 1px solid var(--border-color);
}

.conversation-section__title {
  padding: 0.75rem 1rem 0.45rem;

  color: var(--text-light-color);

  font-size: 0.7rem;
  font-weight: 600;

  text-transform: uppercase;
  letter-spacing: 0.05em;
}

/* --------------------------------------------------
   AI Loading
-------------------------------------------------- */

.loading-dots {
  display: inline-flex;

  align-items: center;

  gap: 4px;
}

.loading-dots span {
  width: 5px;
  height: 5px;

  border-radius: 50%;

  background: var(--text-light-color);

  animation: loading-dot 1.2s infinite ease-in-out;
}

.loading-dots span:nth-child(2) {
  animation-delay: 0.15s;
}

.loading-dots span:nth-child(3) {
  animation-delay: 0.3s;
}

@keyframes loading-dot {
  0%,
  60%,
  100% {
    opacity: 0.3;
    transform: translateY(0);
  }

  30% {
    opacity: 1;
    transform: translateY(-3px);
  }
}

.chat__send:disabled {
  opacity: 0.5;
  cursor: not-allowed;
}

/* ========================================
   Mobile
======================================== */

@media (max-width: 768px) {
  .messages-page {
    padding: 1rem;
  }

  .messages-page__header {
    margin-bottom: 0.75rem;
  }

  .messages-page__header h1 {
    font-size: 1.5rem;
  }

  .messages-page__header p {
    font-size: 0.8rem;
  }

  .messages {
    position: relative;
    display: block;

    border-radius: var(--radius-md);
  }

  .messages__conversations,
  .chat {
    width: 100%;
    height: 100%;
    min-height: 0;
  }

  .messages__conversations {
    border-right: 0;
    border-bottom: 0;
  }

  .chat {
    display: none;
  }

  .messages--chat-open .messages__conversations {
    display: none;
  }

  .messages--chat-open .chat {
    display: flex;
  }

  .messages__search {
    padding: 0.75rem;
  }

  .conversation {
    padding: 0.7rem 0.75rem;
  }

  .conversation__avatar {
    width: 38px;
    height: 38px;
    font-size: 1.1rem;
  }

  .chat__header {
    padding: 0.75rem 1rem;
  }

  .chat__back-button {
    display: flex;
    align-items: center;
    justify-content: center;
    flex-shrink: 0;

    width: 36px;
    height: 36px;
    padding: 0;

    border: 1px solid var(--border-color);
    border-radius: 50%;

    background-color: var(--secondary-background-color);
    color: var(--text-color-of-background);

    font-size: 1.25rem;
    line-height: 1;

    cursor: pointer;

    transition:
      background-color 0.2s ease,
      border-color 0.2s ease,
      transform 0.2s ease;
  }

  .chat__back-button:hover {
    background-color: var(--tertiary-color);
    border-color: var(--primary-color);
  }

  .chat__back-button:active {
    transform: scale(0.94);
  }

  .chat__back-button:focus-visible {
    outline: 2px solid var(--primary-color);
    outline-offset: 3px;
  }

  .chat__avatar {
    width: 38px;
    height: 38px;
    font-size: 1.1rem;
  }

  .chat__user strong {
    font-size: 0.85rem;
  }

  .chat__status {
    font-size: 0.68rem;
  }

  .chat__messages {
    gap: 0.75rem;
    padding: 1rem;
  }

  .message {
    max-width: 88%;
  }

  .message__avatar {
    width: 28px;
    height: 28px;
    font-size: 0.85rem;
  }

  .message__bubble {
    padding: 0.6rem 0.75rem;
    font-size: 0.82rem;
  }

  .chat__form {
    gap: 0.4rem;
    padding: 0.75rem;
  }

  .chat__emoji {
    width: 34px;
    height: 34px;
  }

  .chat__send {
    width: 38px;
    height: 38px;
  }

  .chat__emoji-picker {
    right: 0;
    bottom: calc(100% + 0.5rem);
    width: min(320px, calc(100vw - 2rem));
  }
}

@media (max-width: 600px) {
  .chat__emoji-picker {
    right: 0;
    width: min(320px, calc(100vw - 2rem));
  }
}
