// ==========================================================================
// Messages Page Styles - Modern Design
// ==========================================================================

.messages-page {
  
  // Main messages container
  .messages-container {
    height: calc(100vh - var(--header-height) - 2rem);
    background: var(--messages-bg);
    border-radius: var(--border-radius-lg);
    overflow: hidden;
    box-shadow: var(--box-shadow);
    border: 1px solid var(--messages-border);

    @include media-breakpoint-up(lg) {
      height: calc(100vh - var(--header-height) - 1rem);
    }
  }

  // Messages layout wrapper
  .messages-layout {
    display: flex;
    height: 100%;
    position: relative;
  }

  // ==========================================================================
  // SIDEBAR STYLES
  // ==========================================================================
  
  .messages-sidebar {
    width: 380px;
    background: var(--messages-sidebar-bg);
    border-right: 1px solid var(--messages-border);
    display: flex;
    flex-direction: column;
    flex-shrink: 0;
    transition: all var(--transition-base);

    @include media-breakpoint-down(xl) {
      width: 320px;
    }

    @include media-breakpoint-down(lg) {
      position: absolute;
      left: -100%;
      top: 0;
      height: 100%;
      z-index: 1050;
      box-shadow: var(--box-shadow-lg);

      &.mobile-show {
        left: 0;
      }
    }

    @include media-breakpoint-down(md) {
      width: 300px;
    }

    @include media-breakpoint-down(sm) {
      width: 100%;
    }
  }

  // Sidebar header
  .messages-header {
    padding: 1.5rem;
    border-bottom: 1px solid var(--messages-border);
    background: var(--messages-sidebar-bg);
    flex-shrink: 0;

    .header-title {
      font-weight: 600;
      color: var(--messages-text);
      font-size: 1.25rem;
    }

    .search-container {
      position: relative;

      .form-control {
        padding: 0.75rem 1rem 0.75rem 2.75rem;
        border-radius: var(--border-radius);
        border: 1px solid var(--messages-border);
        background: var(--messages-input-bg);
        font-size: 0.9rem;
        transition: all var(--transition-fast);

        &:focus {
          border-color: var(--bs-primary);
          box-shadow: 0 0 0 0.25rem rgba(99, 102, 241, 0.15);
        }

        &::placeholder {
          color: var(--messages-text-muted);
        }
      }

      .search-icon {
        position: absolute;
        left: 1rem;
        top: 50%;
        transform: translateY(-50%);
        color: var(--messages-text-muted);
        font-size: 0.9rem;
      }
    }
  }

  // Conversations list
  .conversations-list {
    flex: 1;
    overflow-y: auto;
    padding: 0;

    // Custom scrollbar
    &::-webkit-scrollbar {
      width: 4px;
    }

    &::-webkit-scrollbar-track {
      background: transparent;
    }

    &::-webkit-scrollbar-thumb {
      background: var(--messages-border);
      border-radius: 2px;

      &:hover {
        background: var(--bs-secondary);
      }
    }
  }

  // Individual conversation item
  .conversation-item {
    display: flex;
    align-items: center;
    gap: 1rem;
    padding: 1rem 1.5rem;
    border-bottom: 1px solid rgba(var(--bs-border-color-rgb), 0.5);
    cursor: pointer;
    transition: all var(--transition-fast);
    text-decoration: none;
    color: var(--messages-text);
    position: relative;

    &:hover {
      background: rgba(var(--bs-primary-rgb), 0.08);
      transform: translateY(-1px);
    }

    &.active {
      background: var(--bs-primary);
      color: white;
      border-color: transparent;

      .conversation-name,
      .conversation-preview,
      .conversation-time,
      .conversation-type {
        color: white !important;
      }

      .conversation-preview {
        color: rgba(255, 255, 255, 0.9) !important;
      }

      .conversation-time {
        color: rgba(255, 255, 255, 0.8) !important;
      }

      .conversation-type {
        color: rgba(255, 255, 255, 0.7) !important;
      }

      .unread-badge {
        background: rgba(255, 255, 255, 0.25);
        color: white !important;
      }
    }

    &.unread {
      background: rgba(var(--bs-primary-rgb), 0.03);

      &::before {
        content: '';
        position: absolute;
        left: 0;
        top: 0;
        bottom: 0;
        width: 3px;
        background: var(--bs-primary);
      }
    }

    // Avatar container
    .conversation-avatar {
      position: relative;
      flex-shrink: 0;

      img {
        width: 48px;
        height: 48px;
        border-radius: 50%;
        object-fit: cover;
        border: 2px solid transparent;
        transition: all var(--transition-fast);
      }

      .online-indicator {
        position: absolute;
        bottom: 2px;
        right: 2px;
        width: 12px;
        height: 12px;
        background: var(--bs-success);
        border: 2px solid var(--messages-sidebar-bg);
        border-radius: 50%;
        animation: pulse-online 2s infinite;
      }
    }

    // Conversation info
    .conversation-info {
      flex: 1;
      min-width: 0;

      .conversation-header {
        display: flex;
        justify-content: between;
        align-items: baseline;
        margin-bottom: 0.25rem;
      }

      .conversation-name {
        font-weight: 500;
        font-size: 0.95rem;
        margin: 0;
        color: var(--messages-text);
        @include text-truncate();
      }

      .conversation-time {
        font-size: 0.75rem;
        color: var(--messages-text-muted);
        flex-shrink: 0;
        margin-left: auto;
      }

      .conversation-preview {
        font-size: 0.85rem;
        color: var(--messages-text-muted);
        margin: 0 0 0.5rem 0;
        line-height: 1.3;
        @include text-truncate();
      }

      .conversation-footer {
        display: flex;
        justify-content: space-between;
        align-items: center;
      }

      .conversation-type {
        font-size: 0.7rem;
        text-transform: uppercase;
        font-weight: 600;
        color: var(--messages-text-muted);
        letter-spacing: 0.5px;
      }

      .unread-badge {
        background: var(--bs-primary);
        color: white;
        font-size: 0.7rem;
        font-weight: 600;
        padding: 0.2rem 0.5rem;
        border-radius: 50px;
        min-width: 18px;
        text-align: center;
        line-height: 1;
      }
    }
  }

  // Empty conversations state
  .empty-conversations {
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    padding: 3rem 2rem;
    color: var(--messages-text-muted);
    text-align: center;

    i {
      font-size: 2.5rem;
      margin-bottom: 1rem;
      opacity: 0.6;
    }

    p {
      margin: 0;
      font-size: 0.9rem;
    }
  }

  // ==========================================================================
  // CHAT AREA STYLES
  // ==========================================================================

  .chat-area {
    flex: 1;
    display: flex;
    flex-direction: column;
    background: var(--messages-chat-bg);
    min-width: 0;
  }

  // Active chat container
  .active-chat {
    display: flex;
    flex-direction: column;
    height: 100%;
  }

  // Chat header
  .chat-header {
    padding: 1rem 1.5rem;
    border-bottom: 1px solid var(--messages-border);
    background: var(--messages-sidebar-bg);
    display: flex;
    align-items: center;
    justify-content: space-between;
    flex-shrink: 0;

    .chat-user-info {
      display: flex;
      align-items: center;
      gap: 0.75rem;

      .chat-avatar-container {
        position: relative;

        .chat-avatar {
          width: 40px;
          height: 40px;
          border-radius: 50%;
          object-fit: cover;
        }

        .online-indicator {
          position: absolute;
          bottom: 0;
          right: 0;
          width: 10px;
          height: 10px;
          background: var(--bs-success);
          border: 2px solid var(--messages-sidebar-bg);
          border-radius: 50%;
        }
      }

      .chat-details {
        .chat-name {
          font-weight: 500;
          margin: 0 0 0.125rem 0;
          color: var(--messages-text);
          font-size: 1rem;
        }

        .chat-status {
          font-size: 0.8rem;
          color: var(--bs-success);
          margin: 0;
          font-weight: 500;
        }
      }
    }

    .chat-actions {
      display: flex;
      gap: 0.25rem;

      .btn {
        border: none;
        background: none;
        color: var(--messages-text-muted);
        padding: 0.5rem;
        border-radius: var(--border-radius-sm);
        width: 36px;
        height: 36px;
        display: flex;
        align-items: center;
        justify-content: center;
        transition: all var(--transition-fast);

        &:hover {
          color: var(--messages-text);
          background: rgba(var(--bs-secondary-rgb), 0.1);
        }

        &:focus {
          box-shadow: 0 0 0 0.25rem rgba(var(--bs-primary-rgb), 0.15);
        }
      }
    }
  }

  // ==========================================================================
  // MESSAGES AREA
  // ==========================================================================

  .chat-messages {
    flex: 1;
    padding: 1.5rem;
    overflow-y: auto;
    background: var(--messages-chat-bg);
    display: flex;
    flex-direction: column;
    gap: 1.5rem;

    // Custom scrollbar
    &::-webkit-scrollbar {
      width: 6px;
    }

    &::-webkit-scrollbar-track {
      background: transparent;
    }

    &::-webkit-scrollbar-thumb {
      background: rgba(var(--bs-secondary-rgb), 0.3);
      border-radius: 3px;

      &:hover {
        background: rgba(var(--bs-secondary-rgb), 0.5);
      }
    }
  }

  // Date separator
  .date-separator {
    text-align: center;
    position: relative;
    margin: 1rem 0;

    &::before {
      content: '';
      position: absolute;
      top: 50%;
      left: 0;
      right: 0;
      height: 1px;
      background: linear-gradient(to right, transparent, var(--messages-border), transparent);
    }

    .date-label {
      background: var(--messages-chat-bg);
      color: var(--messages-text-muted);
      padding: 0.5rem 1rem;
      border-radius: var(--border-radius);
      font-size: 0.8rem;
      font-weight: 500;
      position: relative;
      z-index: 1;
    }
  }

  // Message group
  .message-group {
    display: flex;
    flex-direction: column;
    gap: 0.75rem;
  }

  // Individual message
  .message {
    display: flex;
    gap: 0.75rem;
    animation: message-appear 0.3s ease-out;

    &.own-message {
      flex-direction: row-reverse;

      .message-bubble {
        .message-content {
          background: var(--bs-primary);
          color: white !important;
          border-bottom-right-radius: 0.5rem;

          p {
            color: white !important;
          }
        }
      }

      .message-info {
        text-align: right;

        .message-time,
        .message-status {
          color: rgba(255, 255, 255, 0.8) !important;
        }
      }
    }

    .message-avatar {
      width: 32px;
      height: 32px;
      border-radius: 50%;
      object-fit: cover;
      flex-shrink: 0;
      margin-top: 0.25rem;
    }

    .message-bubble {
      flex: 1;
      max-width: 75%;

      .message-content {
        background: var(--messages-bubble-bg);
        border: 1px solid rgba(var(--bs-border-color-rgb), 0.1);
        border-radius: 1.25rem;
        border-bottom-left-radius: 0.5rem;
        padding: 0.875rem 1.125rem;
        margin-bottom: 0.25rem;
        box-shadow: 0 1px 3px rgba(0, 0, 0, 0.08);
        position: relative;
        word-wrap: break-word;

        p {
          margin: 0;
          line-height: 1.4;
          color: var(--messages-text);
        }

        // Message tail effect
        &::before {
          content: '';
          position: absolute;
          bottom: 0;
          left: -6px;
          width: 0;
          height: 0;
          border: 6px solid transparent;
          border-right-color: var(--messages-bubble-bg);
          border-bottom-color: var(--messages-bubble-bg);
        }
      }

      .message-info {
        display: flex;
        align-items: center;
        gap: 0.5rem;
        color: var(--messages-text-muted);
        font-size: 0.75rem;
        padding-left: 0.5rem;

        .message-time {
          margin: 0;
        }

        .message-status {
          color: var(--bs-success);
          
          i {
            font-size: 0.7rem;
          }
        }
      }
    }

    // Own message styling adjustments
    &.own-message .message-bubble .message-content {
      &::before {
        left: auto;
        right: -6px;
        border-left-color: var(--bs-primary);
        border-bottom-color: var(--bs-primary);
        border-right-color: transparent;
      }
    }
  }

  // Typing indicator
  .typing-indicator {
    display: flex;
    align-items: center;
    gap: 0.75rem;
    margin-top: 0.5rem;
    animation: typing-appear 0.3s ease-out;

    .typing-avatar {
      width: 32px;
      height: 32px;
      border-radius: 50%;
      object-fit: cover;
      margin-top: 0.25rem;
    }

    .typing-content {
      background: var(--messages-bubble-bg);
      border: 1px solid rgba(var(--bs-border-color-rgb), 0.1);
      border-radius: 1.25rem;
      border-bottom-left-radius: 0.5rem;
      padding: 0.875rem 1.125rem;
      display: flex;
      align-items: center;
      gap: 0.5rem;
      box-shadow: 0 1px 3px rgba(0, 0, 0, 0.08);

      .typing-dots {
        display: flex;
        gap: 0.25rem;

        .dot {
          width: 6px;
          height: 6px;
          background: var(--messages-text-muted);
          border-radius: 50%;
          animation: typing-dot 1.5s infinite;

          &:nth-child(2) {
            animation-delay: 0.2s;
          }

          &:nth-child(3) {
            animation-delay: 0.4s;
          }
        }
      }

      .typing-text {
        font-size: 0.8rem;
        color: var(--messages-text-muted);
        font-style: italic;
      }
    }
  }

  // ==========================================================================
  // CHAT INPUT
  // ==========================================================================

  .chat-input {
    padding: 1rem 1.5rem;
    border-top: 1px solid var(--messages-border);
    background: var(--messages-input-bg);
    flex-shrink: 0;

    .input-container {
      display: flex;
      gap: 0.75rem;
      align-items: flex-end;
      position: relative;
    }

    .message-input {
      flex: 1;

      .form-control {
        resize: none;
        max-height: 120px;
        min-height: 44px;
        border-radius: 1.375rem;
        padding: 0.75rem 1rem;
        border: 1px solid var(--messages-border);
        background: var(--messages-input-bg);
        transition: all var(--transition-fast);
        line-height: 1.5;

        &:focus {
          border-color: var(--bs-primary);
          box-shadow: 0 0 0 0.25rem rgba(var(--bs-primary-rgb), 0.15);
        }

        &::placeholder {
          color: var(--messages-text-muted);
        }
      }
    }

    .input-actions {
      display: flex;
      gap: 0.25rem;

      .btn {
        border: none;
        background: none;
        color: var(--messages-text-muted);
        padding: 0.5rem;
        border-radius: 50%;
        width: 40px;
        height: 40px;
        display: flex;
        align-items: center;
        justify-content: center;
        transition: all var(--transition-fast);

        &:hover {
          color: var(--messages-text);
          background: rgba(var(--bs-secondary-rgb), 0.1);
        }

        &.btn-primary {
          background: var(--bs-primary);
          color: white;

          &:hover {
            background: var(--bs-primary);
            transform: scale(1.05);
          }

          &:disabled {
            background: rgba(var(--bs-secondary-rgb), 0.3);
            color: var(--messages-text-muted);
            transform: none;
            cursor: not-allowed;
          }
        }

        &:focus {
          box-shadow: 0 0 0 0.25rem rgba(var(--bs-primary-rgb), 0.15);
        }
      }
    }

    // Emoji picker
    .emoji-picker {
      position: absolute;
      bottom: 100%;
      right: 4rem;
      background: var(--messages-input-bg);
      border: 1px solid var(--messages-border);
      border-radius: var(--border-radius);
      box-shadow: var(--box-shadow-lg);
      padding: 1rem;
      z-index: 1000;
      margin-bottom: 0.5rem;

      .emoji-grid {
        display: grid;
        grid-template-columns: repeat(8, 1fr);
        gap: 0.25rem;
        max-height: 200px;
        overflow-y: auto;

        .emoji-btn {
          border: none;
          background: none;
          padding: 0.5rem;
          border-radius: var(--border-radius-sm);
          cursor: pointer;
          transition: all var(--transition-fast);

          &:hover {
            background: rgba(var(--bs-secondary-rgb), 0.1);
            transform: scale(1.2);
          }
        }
      }
    }
  }

  // ==========================================================================
  // EMPTY CHAT STATE
  // ==========================================================================

  .empty-chat {
    flex: 1;
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    text-align: center;
    color: var(--messages-text-muted);
    padding: 3rem 2rem;

    .empty-icon {
      font-size: 4rem;
      margin-bottom: 1.5rem;
      opacity: 0.6;
      color: var(--bs-primary);
    }

    .empty-text {
      font-size: 1.25rem;
      font-weight: 500;
      margin-bottom: 0.5rem;
      color: var(--messages-text);
    }

    p {
      margin-bottom: 2rem;
      max-width: 400px;
    }

    .btn {
      padding: 0.75rem 1.5rem;
      font-weight: 500;
    }
  }
}

// ==========================================================================
// ANIMATIONS
// ==========================================================================

@keyframes typing-dot {
  0%, 60%, 100% {
    transform: translateY(0);
    opacity: 0.4;
  }
  30% {
    transform: translateY(-8px);
    opacity: 1;
  }
}

@keyframes pulse-online {
  0% {
    transform: scale(1);
  }
  50% {
    transform: scale(1.1);
  }
  100% {
    transform: scale(1);
  }
}

@keyframes message-appear {
  from {
    opacity: 0;
    transform: translateY(10px);
  }
  to {
    opacity: 1;
    transform: translateY(0);
  }
}

@keyframes typing-appear {
  from {
    opacity: 0;
    transform: translateY(5px);
  }
  to {
    opacity: 1;
    transform: translateY(0);
  }
}

// ==========================================================================
// CSS CUSTOM PROPERTIES
// ==========================================================================

:root {
  --messages-bg: #{$body-bg};
  --messages-sidebar-bg: #{$white};
  --messages-border: #{$border-color};
  --messages-text: #{$body-color};
  --messages-text-muted: #{$text-muted};
  --messages-chat-bg: #{$gray-50};
  --messages-bubble-bg: #{$white};
  --messages-input-bg: #{$white};
}

[data-bs-theme="dark"] {
  --messages-bg: #{$dark-bg};
  --messages-sidebar-bg: #{$gray-800};
  --messages-border: #{$dark-border-color};
  --messages-text: #{$dark-color};
  --messages-text-muted: #{$dark-text-muted};
  --messages-chat-bg: #{$gray-900};
  --messages-bubble-bg: #{$gray-800};
  --messages-input-bg: #{$gray-800};

  .conversation-item.active {
    .conversation-name,
    .conversation-preview,
    .conversation-time,
    .conversation-type {
      color: white !important;
    }
  }
}

// ==========================================================================
// RESPONSIVE DESIGN
// ==========================================================================

@include media-breakpoint-down(lg) {
  .messages-page {
    .messages-container {
      height: calc(100vh - var(--header-height) - 1rem);
    }

    .chat-header .chat-user-info {
      gap: 0.5rem;
    }

    .chat-messages {
      padding: 1rem;
    }

    .chat-input {
      padding: 0.75rem 1rem;
    }
  }
}

@include media-breakpoint-down(md) {
  .messages-page {
    .message .message-bubble {
      max-width: 85%;
    }

    .chat-input .input-container {
      gap: 0.5rem;
    }

    .empty-chat {
      padding: 2rem 1rem;

      .empty-icon {
        font-size: 3rem;
      }
    }
  }
}

@include media-breakpoint-down(sm) {
  .messages-page {
    .messages-container {
      height: calc(100vh - var(--header-height) - 0.5rem);
      border-radius: var(--border-radius);
    }

    .messages-header {
      padding: 1rem;
    }

    .conversation-item {
      padding: 0.75rem 1rem;
    }

    .chat-header {
      padding: 0.75rem 1rem;
    }

    .chat-messages {
      padding: 0.75rem;
      gap: 1rem;
    }

    .chat-input {
      padding: 0.75rem;
    }

    .message .message-bubble {
      max-width: 90%;
    }
  }
}