// =============================================================================
// Chat App Styles
// =============================================================================

// -----------------------------------------------------------------------------
// Container
// -----------------------------------------------------------------------------

.chat-container {
  display: flex;
  min-height: calc(100vh - 200px);
  background: var(--surface-color);
  border-radius: var(--bs-border-radius-lg);
  border: 1px solid var(--border-color);
  overflow: hidden;
}

// -----------------------------------------------------------------------------
// Sidebar
// -----------------------------------------------------------------------------

.chat-sidebar {
  width: 320px;
  border-right: 1px solid var(--border-color);
  display: flex;
  flex-direction: column;
  flex-shrink: 0;
}

.chat-sidebar-header {
  padding: 1rem 1.25rem;
  border-bottom: 1px solid var(--border-color);
}

.chat-sidebar-title {
  font-size: 1.25rem;
  font-weight: 600;
  color: var(--heading-color);
  margin-bottom: 1rem;
}

.chat-search {
  position: relative;

  .form-control {
    padding-left: 2.5rem;
    background: var(--background-color);
    border-color: var(--border-color);

    &:focus {
      background: var(--surface-color);
    }
  }
}

.chat-search-icon {
  position: absolute;
  left: 0.875rem;
  top: 50%;
  transform: translateY(-50%);
  color: var(--muted-color);
}

// -----------------------------------------------------------------------------
// Tabs
// -----------------------------------------------------------------------------

.chat-tabs {
  display: flex;
  padding: 0.5rem 1.25rem;
  gap: 0.5rem;
  border-bottom: 1px solid var(--border-color);
}

.chat-tab {
  flex: 1;
  padding: 0.5rem;
  border: none;
  background: none;
  color: var(--muted-color);
  font-size: 0.8125rem;
  font-weight: 500;
  border-radius: var(--bs-border-radius);
  cursor: pointer;
  transition: all 0.2s;

  &:hover {
    background: var(--background-color);
    color: var(--default-color);
  }

  &.active {
    background: color-mix(in srgb, var(--accent-color), transparent 90%);
    color: var(--accent-color);
  }

  .badge {
    margin-left: 0.25rem;
    font-size: 0.6875rem;
  }
}

// -----------------------------------------------------------------------------
// Chat List
// -----------------------------------------------------------------------------

.chat-list {
  flex: 1;
  overflow-y: auto;

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

  &::-webkit-scrollbar-thumb {
    background: var(--border-color);
    border-radius: 4px;
  }
}

.chat-item {
  display: flex;
  align-items: center;
  gap: 0.75rem;
  padding: 0.875rem 1.25rem;
  cursor: pointer;
  transition: background 0.2s;
  border-left: 3px solid transparent;

  &:hover {
    background: var(--background-color);
  }

  &.active {
    background: color-mix(in srgb, var(--accent-color), transparent 95%);
    border-left-color: var(--accent-color);
  }
}

.chat-item-avatar {
  position: relative;
  flex-shrink: 0;

  img {
    width: 48px;
    height: 48px;
    border-radius: 50%;
    object-fit: cover;
  }
}

.chat-item-status {
  position: absolute;
  bottom: 2px;
  right: 2px;
  width: 12px;
  height: 12px;
  border-radius: 50%;
  border: 2px solid var(--surface-color);

  &.online {
    background: var(--success-color);
  }

  &.away {
    background: var(--warning-color);
  }

  &.offline {
    background: var(--muted-color);
  }

  &.busy {
    background: var(--danger-color);
  }
}

.chat-item-content {
  flex: 1;
  min-width: 0;
}

.chat-item-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 0.25rem;
}

.chat-item-name {
  font-weight: 600;
  font-size: 0.9375rem;
  color: var(--heading-color);
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.chat-item-time {
  font-size: 0.75rem;
  color: var(--muted-color);
  flex-shrink: 0;
}

.chat-item-message {
  display: flex;
  align-items: center;
  gap: 0.5rem;
}

.chat-item-message-text {
  font-size: 0.8125rem;
  color: var(--muted-color);
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
  flex: 1;
}

.chat-item-unread {
  width: 20px;
  height: 20px;
  border-radius: 50%;
  background: var(--accent-color);
  color: var(--contrast-color);
  font-size: 0.6875rem;
  font-weight: 600;
  display: flex;
  align-items: center;
  justify-content: center;
  flex-shrink: 0;
}

// -----------------------------------------------------------------------------
// Main Area
// -----------------------------------------------------------------------------

.chat-main {
  flex: 1;
  display: flex;
  flex-direction: column;
  min-width: 0;
}

// -----------------------------------------------------------------------------
// Header
// -----------------------------------------------------------------------------

.chat-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 1rem 1.25rem;
  border-bottom: 1px solid var(--border-color);
}

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

.chat-header-avatar {
  position: relative;

  img {
    width: 44px;
    height: 44px;
    border-radius: 50%;
    object-fit: cover;
  }
}

.chat-header-info {
  h5 {
    font-size: 1rem;
    font-weight: 600;
    color: var(--heading-color);
    margin: 0 0 0.125rem 0;
  }

  span {
    font-size: 0.8125rem;
    color: var(--success-color);
  }
}

.chat-header-actions {
  display: flex;
  gap: 0.5rem;
}

.chat-header-btn {
  width: 44px;
  height: 44px;
  border-radius: 50%;
  border: none;
  background: var(--background-color);
  color: var(--muted-color);
  display: flex;
  align-items: center;
  justify-content: center;
  cursor: pointer;
  transition: all 0.2s;

  &:hover {
    background: var(--border-color);
    color: var(--default-color);
  }
}

// -----------------------------------------------------------------------------
// Messages
// -----------------------------------------------------------------------------

.chat-messages {
  flex: 1;
  padding: 1.5rem;
  overflow-y: auto;
  display: flex;
  flex-direction: column;
  gap: 1.5rem;

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

  &::-webkit-scrollbar-thumb {
    background: var(--border-color);
    border-radius: 4px;
  }
}

// -----------------------------------------------------------------------------
// Message Group
// -----------------------------------------------------------------------------

.message-group {
  display: flex;
  gap: 0.75rem;

  &.sent {
    flex-direction: row-reverse;

    .message-content {
      align-items: flex-end;
    }

    .message-bubble {
      background: var(--accent-color);
      color: var(--contrast-color);
      border-bottom-right-radius: 0.25rem;
    }
  }

  &.received .message-bubble {
    border-bottom-left-radius: 0.25rem;
  }
}

.message-avatar img {
  width: 36px;
  height: 36px;
  border-radius: 50%;
  object-fit: cover;
}

.message-content {
  max-width: 70%;
  display: flex;
  flex-direction: column;
  gap: 0.25rem;
}

.message-bubble {
  padding: 0.75rem 1rem;
  border-radius: 1rem;
  background: var(--background-color);
  color: var(--default-color);
  font-size: 0.9375rem;
  line-height: 1.5;
  word-wrap: break-word;
}

.message-time {
  font-size: 0.6875rem;
  color: var(--muted-color);
  padding: 0 0.5rem;
}

.message-status {
  font-size: 0.75rem;
  color: var(--muted-color);
  display: flex;
  align-items: center;
  gap: 0.25rem;

  &.seen {
    color: var(--accent-color);
  }
}

// -----------------------------------------------------------------------------
// Message Date Divider
// -----------------------------------------------------------------------------

.message-date-divider {
  display: flex;
  align-items: center;
  gap: 1rem;
  margin: 0.5rem 0;

  &::before,
  &::after {
    content: '';
    flex: 1;
    height: 1px;
    background: var(--border-color);
  }

  span {
    font-size: 0.75rem;
    color: var(--muted-color);
    white-space: nowrap;
  }
}

// -----------------------------------------------------------------------------
// Message Image & File
// -----------------------------------------------------------------------------

.message-image {
  max-width: 280px;
  border-radius: 0.75rem;
  overflow: hidden;
  margin-top: 0.25rem;

  img {
    width: 100%;
    display: block;
  }
}

.message-file {
  display: flex;
  align-items: center;
  gap: 0.75rem;
  padding: 0.75rem 1rem;
  background: var(--background-color);
  border-radius: 0.75rem;
  margin-top: 0.25rem;
  border: 1px solid var(--border-color);
}

.message-file-icon {
  width: 40px;
  height: 40px;
  border-radius: var(--bs-border-radius);
  background: color-mix(in srgb, var(--accent-color), transparent 90%);
  color: var(--accent-color);
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 1.25rem;
}

.message-file-info {
  flex: 1;
}

.message-file-name {
  font-size: 0.875rem;
  font-weight: 500;
  color: var(--heading-color);
}

.message-file-size {
  font-size: 0.75rem;
  color: var(--muted-color);
}

.message-file-download {
  width: 32px;
  height: 32px;
  border-radius: 50%;
  background: var(--surface-color);
  border: 1px solid var(--border-color);
  color: var(--muted-color);
  display: flex;
  align-items: center;
  justify-content: center;
  cursor: pointer;
  transition: all 0.2s;

  &:hover {
    background: var(--accent-color);
    border-color: var(--accent-color);
    color: var(--contrast-color);
  }
}

// -----------------------------------------------------------------------------
// Typing Indicator
// -----------------------------------------------------------------------------

.typing-indicator {
  display: flex;
  align-items: center;
  gap: 0.75rem;
  padding: 0.5rem 0;

  img {
    width: 32px;
    height: 32px;
    border-radius: 50%;
  }
}

.typing-dots {
  display: flex;
  gap: 4px;
  padding: 0.75rem 1rem;
  background: var(--background-color);
  border-radius: 1rem;
  border-bottom-left-radius: 0.25rem;

  span {
    width: 8px;
    height: 8px;
    border-radius: 50%;
    background: var(--muted-color);
    animation: typingBounce 1.4s infinite ease-in-out both;

    &:nth-child(1) { animation-delay: -0.32s; }
    &:nth-child(2) { animation-delay: -0.16s; }
  }
}

@keyframes typingBounce {
  0%, 80%, 100% {
    transform: scale(0.6);
    opacity: 0.5;
  }
  40% {
    transform: scale(1);
    opacity: 1;
  }
}

// -----------------------------------------------------------------------------
// Input
// -----------------------------------------------------------------------------

.chat-input {
  padding: 1rem 1.25rem;
  border-top: 1px solid var(--border-color);
  background: var(--surface-color);
}

.chat-input-wrapper {
  display: flex;
  align-items: flex-end;
  gap: 0.75rem;
  background: var(--background-color);
  border-radius: var(--bs-border-radius-lg);
  padding: 0.5rem;
}

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

.chat-input-btn {
  width: 44px;
  height: 44px;
  border-radius: 50%;
  border: none;
  background: none;
  color: var(--muted-color);
  display: flex;
  align-items: center;
  justify-content: center;
  cursor: pointer;
  transition: all 0.2s;

  &:hover {
    background: var(--border-color);
    color: var(--default-color);
  }
}

.chat-input-field {
  flex: 1;

  textarea {
    width: 100%;
    border: none;
    background: none;
    resize: none;
    padding: 0.5rem;
    font-size: 0.9375rem;
    color: var(--default-color);
    max-height: 120px;
    line-height: 1.5;

    &:focus {
      outline: none;
    }

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

.chat-send-btn {
  width: 44px;
  height: 44px;
  border-radius: 50%;
  border: none;
  background: var(--accent-color);
  color: var(--contrast-color);
  display: flex;
  align-items: center;
  justify-content: center;
  cursor: pointer;
  transition: all 0.2s;

  &:hover {
    background: color-mix(in srgb, var(--accent-color), black 15%);
  }

  &:disabled {
    background: var(--border-color);
    color: var(--muted-color);
    cursor: not-allowed;
  }
}

// -----------------------------------------------------------------------------
// Info Panel
// -----------------------------------------------------------------------------

.chat-info {
  width: 300px;
  border-left: 1px solid var(--border-color);
  display: none;
  flex-direction: column;

  &.show {
    display: flex;
  }
}

.chat-info-header {
  padding: 1rem 1.25rem;
  border-bottom: 1px solid var(--border-color);
  display: flex;
  align-items: center;
  justify-content: space-between;

  h6 {
    font-size: 0.9375rem;
    font-weight: 600;
    color: var(--heading-color);
    margin: 0;
  }
}

.chat-info-close {
  width: 32px;
  height: 32px;
  border-radius: 50%;
  border: none;
  background: none;
  color: var(--muted-color);
  cursor: pointer;
  transition: all 0.2s;

  &:hover {
    background: var(--background-color);
    color: var(--default-color);
  }
}

.chat-info-body {
  flex: 1;
  overflow-y: auto;
  padding: 1.25rem;
}

.chat-info-profile {
  text-align: center;
  margin-bottom: 1.5rem;

  img {
    width: 80px;
    height: 80px;
    border-radius: 50%;
    object-fit: cover;
    margin-bottom: 0.75rem;
  }

  h5 {
    font-size: 1.125rem;
    font-weight: 600;
    color: var(--heading-color);
    margin: 0 0 0.25rem 0;
  }

  span {
    font-size: 0.875rem;
    color: var(--muted-color);
  }
}

.chat-info-section {
  margin-bottom: 1.5rem;
}

.chat-info-section-title {
  font-size: 0.75rem;
  font-weight: 600;
  color: var(--muted-color);
  text-transform: uppercase;
  letter-spacing: 0.05em;
  margin-bottom: 0.75rem;
}

.chat-info-item {
  display: flex;
  align-items: center;
  gap: 0.75rem;
  padding: 0.5rem 0;

  i {
    width: 20px;
    color: var(--muted-color);
    text-align: center;
  }

  span {
    font-size: 0.875rem;
    color: var(--default-color);
  }
}

.chat-info-media {
  display: grid;
  grid-template-columns: repeat(3, 1fr);
  gap: 0.5rem;

  img {
    width: 100%;
    aspect-ratio: 1;
    object-fit: cover;
    border-radius: var(--bs-border-radius);
    cursor: pointer;
    transition: opacity 0.2s;

    &:hover {
      opacity: 0.8;
    }
  }
}

// -----------------------------------------------------------------------------
// Empty State
// -----------------------------------------------------------------------------

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

.chat-empty-icon {
  width: 80px;
  height: 80px;
  border-radius: 50%;
  background: var(--background-color);
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 2rem;
  margin-bottom: 1rem;
}

.chat-empty h5 {
  font-size: 1.125rem;
  font-weight: 600;
  color: var(--heading-color);
  margin: 0 0 0.5rem 0;
}

.chat-empty p {
  font-size: 0.875rem;
  margin: 0;
}

// -----------------------------------------------------------------------------
// Sidebar Toggle Button (hidden by default, shown under 1280px)
// -----------------------------------------------------------------------------

.chat-sidebar-toggle {
  display: none;
  align-items: center;
  justify-content: center;
  width: 44px;
  height: 44px;
  padding: 0;
  background: var(--background-color);
  border: none;
  border-radius: 50%;
  color: var(--muted-color);
  font-size: 1.125rem;
  cursor: pointer;
  transition: all 0.2s;
  flex-shrink: 0;

  &:hover {
    background: var(--border-color);
    color: var(--default-color);
  }
}

// -----------------------------------------------------------------------------
// Sidebar Close Button (hidden by default)
// -----------------------------------------------------------------------------

.chat-sidebar-close {
  display: none;
  position: absolute;
  top: 0.75rem;
  right: 0.75rem;
  width: 44px;
  height: 44px;
  padding: 0;
  background: color-mix(in srgb, var(--default-color), transparent 95%);
  border: none;
  border-radius: var(--bs-border-radius);
  color: var(--muted-color);
  cursor: pointer;
  transition: all 0.2s;
  z-index: 10;

  &:hover {
    background: color-mix(in srgb, var(--danger-color), transparent 85%);
    color: var(--danger-color);
  }
}

// -----------------------------------------------------------------------------
// Sidebar Overlay (hidden by default)
// -----------------------------------------------------------------------------

.chat-sidebar-overlay {
  display: none;
  position: fixed;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  background: color-mix(in srgb, var(--default-color), transparent 50%);
  z-index: 1040;
  opacity: 0;
  transition: opacity 0.3s ease;

  &.show {
    opacity: 1;
  }

  [data-theme="dark"] & {
    background: color-mix(in srgb, var(--default-color), transparent 30%);
  }
}

// -----------------------------------------------------------------------------
// Responsive - Under 1280px: Show toggle button, hide sidebar
// -----------------------------------------------------------------------------

@media (max-width: 1279.98px) {
  // Show the toggle button in header
  .chat-sidebar-toggle {
    display: flex;
  }

  // Show the overlay when sidebar is open
  .chat-sidebar-overlay {
    display: block;
    pointer-events: none;

    &.show {
      pointer-events: auto;
    }
  }

  // Show the close button inside sidebar
  .chat-sidebar-close {
    display: flex;
    align-items: center;
    justify-content: center;
  }

  // Sidebar becomes slide-in panel
  .chat-sidebar {
    position: fixed;
    top: 0;
    left: 0;
    bottom: 0;
    width: 340px;
    max-width: 90vw;
    height: 100vh;
    height: 100dvh; // Dynamic viewport height for mobile browsers
    background: var(--surface-color);
    z-index: 1050;
    display: flex;
    flex-direction: column;
    overflow: hidden; // Container doesn't scroll, children do
    padding-top: 3.5rem; // Space for close button
    box-shadow: 4px 0 16px rgba(0, 0, 0, 0.15);
    transform: translateX(-100%);
    transition: transform 0.3s ease;

    &.show {
      transform: translateX(0);
    }
  }

  // Sidebar header - fixed at top
  .chat-sidebar-header {
    flex-shrink: 0;
  }

  // Chat tabs - fixed
  .chat-tabs {
    flex-shrink: 0;
  }

  // Chat list should take remaining space and scroll
  .chat-list {
    flex: 1;
    min-height: 0; // Important for flex child to allow shrinking
    overflow-y: auto;
    max-height: none; // Remove any max-height restrictions
    scrollbar-width: thin;

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

    &::-webkit-scrollbar-track {
      background: var(--background-color);
    }

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

      &:hover {
        background: var(--muted-color);
      }
    }
  }

  // Prevent body scroll when sidebar is open
  body.chat-sidebar-open {
    overflow: hidden;
  }

  .chat-info {
    display: none !important;
  }
}

// -----------------------------------------------------------------------------
// Responsive - Tablet
// -----------------------------------------------------------------------------

@media (max-width: 991.98px) {
  .chat-info {
    display: none !important;
  }
}

// -----------------------------------------------------------------------------
// Responsive - Mobile
// -----------------------------------------------------------------------------

@media (max-width: 767.98px) {
  .chat-container {
    min-height: calc(100vh - 180px);
  }

  // Wider message bubbles on mobile
  .message-content {
    max-width: 80%;
  }

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

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

  .chat-input-wrapper {
    padding: 0.375rem;
  }
}

// Small phone breakpoint
@media (max-width: 575.98px) {
  .chat-container {
    border-radius: 0;
    border-left: none;
    border-right: none;
    min-height: calc(100vh - 160px);
  }

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

  .chat-sidebar-title {
    font-size: 1.125rem;
    margin-bottom: 0.75rem;
  }

  // Tabs stay touch-friendly
  .chat-tabs {
    padding: 0.375rem 0.75rem;
  }

  .chat-tab {
    padding: 0.625rem 0.5rem;
    min-height: 44px;
    font-size: 0.75rem;
  }

  // Compact chat items but touch-friendly
  .chat-list {
    max-height: 200px;
  }

  .chat-item {
    padding: 0.75rem 1rem;
    min-height: 64px;
  }

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

  .chat-item-name {
    font-size: 0.875rem;
  }

  .chat-item-message-text {
    font-size: 0.75rem;
  }

  .chat-item-time {
    font-size: 0.6875rem;
  }

  // Header - wrap on mobile
  .chat-header {
    padding: 0.75rem 1rem;
    flex-wrap: wrap;
    gap: 0.5rem;
  }

  .chat-header-user {
    flex: 1;
    min-width: 0; // Allow shrinking
  }

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

  .chat-header-info {
    min-width: 0; // Allow text truncation

    h5 {
      font-size: 0.9375rem;
      white-space: nowrap;
      overflow: hidden;
      text-overflow: ellipsis;
    }

    span {
      font-size: 0.75rem;
    }
  }

  .chat-header-actions {
    gap: 0.25rem;
  }

  // Hide some action buttons on very small screens
  .chat-header-btn[title="Voice Call"],
  .chat-header-btn[title="Video Call"] {
    display: none;
  }

  // Messages - wider on very small screens
  .message-content {
    max-width: 85%;
  }

  .message-avatar img {
    width: 32px;
    height: 32px;
  }

  .message-bubble {
    padding: 0.625rem 0.875rem;
    font-size: 0.875rem;
  }

  .message-time {
    font-size: 0.625rem;
  }

  .message-image {
    max-width: 220px;
  }

  .message-file {
    padding: 0.5rem 0.75rem;
  }

  .message-file-icon {
    width: 36px;
    height: 36px;
    font-size: 1rem;
  }

  .message-file-name {
    font-size: 0.8125rem;
  }

  .message-file-download {
    width: 44px;
    height: 44px;
  }

  // Chat input
  .chat-input {
    padding: 0.5rem 0.75rem;
  }

  .chat-input-wrapper {
    gap: 0.5rem;
    padding: 0.25rem;
  }

  .chat-input-actions {
    gap: 0;
  }

  .chat-input-btn {
    width: 44px;
    height: 44px;
  }

  .chat-input-field textarea {
    font-size: 0.875rem;
    padding: 0.375rem 0.5rem;
  }

  .chat-send-btn {
    width: 44px;
    height: 44px;
  }

  // Typing indicator
  .typing-indicator img {
    width: 28px;
    height: 28px;
  }

  .typing-dots {
    padding: 0.625rem 0.875rem;

    span {
      width: 6px;
      height: 6px;
    }
  }

  // Empty state
  .chat-empty {
    padding: 1.5rem;
  }

  .chat-empty-icon {
    width: 60px;
    height: 60px;
    font-size: 1.5rem;
  }

  .chat-empty h5 {
    font-size: 1rem;
  }

  .chat-empty p {
    font-size: 0.8125rem;
  }
}
