/*
 * Messaging Chat Panel Styles
 * Dark theme - uses design-system.css variables
 */

/* ========================================
   Floating Action Button (FAB)
   ======================================== */

.chat-fab {
  position: fixed;
  bottom: 24px;
  right: 24px;
  width: 56px;
  height: 56px;
  border-radius: var(--radius-full);
  background: var(--primary);
  color: #ffffff;
  border: none;
  cursor: pointer;
  z-index: 1000;
  display: flex;
  align-items: center;
  justify-content: center;
  box-shadow: 0 4px 14px rgba(34, 197, 94, 0.4);
  transition: background 0.2s ease, box-shadow 0.2s ease, transform 0.2s ease;
}

.chat-fab:hover {
  background: var(--primary-hover);
  box-shadow: 0 6px 20px rgba(34, 197, 94, 0.5);
  transform: scale(1.05);
}

.chat-fab:active {
  transform: scale(0.97);
}

.chat-fab i,
.chat-fab svg {
  width: 24px;
  height: 24px;
  pointer-events: none;
}

.chat-fab.hidden {
  display: none;
}

.chat-fab-badge {
  position: absolute;
  top: -2px;
  right: -2px;
  min-width: 20px;
  height: 20px;
  border-radius: var(--radius-full);
  background: #ef4444;
  color: #ffffff;
  font-size: 11px;
  font-weight: 700;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 0 5px;
  line-height: 1;
  border: 2px solid var(--bg);
  box-sizing: border-box;
}

/* ========================================
   Chat Overlay (mobile backdrop)
   ======================================== */

.chat-overlay {
  position: fixed;
  inset: 0;
  background: rgba(0, 0, 0, 0.5);
  z-index: 1000;
  display: none;
  opacity: 0;
  transition: opacity 0.3s ease;
}

.chat-overlay.visible {
  display: block;
  opacity: 1;
}

/* ========================================
   Chat Panel
   ======================================== */

.chat-panel {
  position: fixed;
  top: 0;
  right: 0;
  bottom: 0;
  width: 380px;
  background: var(--bg-elevated);
  z-index: 1001;
  border-left: 1px solid var(--border);
  display: flex;
  flex-direction: column;
  transform: translateX(100%);
  transition: transform 0.3s ease;
}

.chat-panel.open {
  transform: translateX(0);
}

/* ========================================
   Panel Header
   ======================================== */

.chat-panel-header {
  display: flex;
  flex-direction: row;
  align-items: center;
  padding: var(--space-4);
  border-bottom: 1px solid var(--border);
  min-height: 56px;
  gap: var(--space-3);
  flex-shrink: 0;
}

.chat-panel-header .title {
  flex: 1;
  font-weight: 600;
  font-size: var(--text-base);
  color: var(--text);
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.chat-panel-header .back-btn,
.chat-panel-header .close-btn,
.chat-panel-header .new-msg-btn {
  width: 32px;
  height: 32px;
  border-radius: var(--radius-full);
  background: transparent;
  border: none;
  color: var(--text-secondary);
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: center;
  flex-shrink: 0;
  transition: background 0.15s ease, color 0.15s ease;
}

.chat-panel-header .back-btn:hover,
.chat-panel-header .close-btn:hover,
.chat-panel-header .new-msg-btn:hover {
  background: var(--bg-surface);
  color: var(--text);
}

.chat-panel-header .back-btn i,
.chat-panel-header .back-btn svg,
.chat-panel-header .close-btn i,
.chat-panel-header .close-btn svg,
.chat-panel-header .new-msg-btn i,
.chat-panel-header .new-msg-btn svg {
  width: 18px;
  height: 18px;
}

/* Hide back button in list view — shown in chat/new views */
#chat-view-list .back-btn {
  display: none;
}

/* ========================================
   Views (show/hide)
   ======================================== */

.chat-view {
  display: none;
  flex-direction: column;
  flex: 1;
  overflow: hidden;
}

.chat-view.active {
  display: flex;
}

/* ========================================
   Conversation List
   ======================================== */

.conversation-list {
  flex: 1;
  overflow-y: auto;
}

.conversation-item {
  display: flex;
  flex-direction: row;
  padding: var(--space-3) var(--space-4);
  gap: var(--space-3);
  align-items: center;
  cursor: pointer;
  border-bottom: 1px solid var(--border);
  transition: background 0.15s ease;
}

.conversation-item:hover {
  background: var(--bg-surface-hover);
}

.conversation-item.unread {
  background: var(--bg-surface);
}

.conversation-item .avatar {
  width: 40px;
  height: 40px;
  border-radius: var(--radius-full);
  background: var(--bg-surface);
  flex-shrink: 0;
  display: flex;
  align-items: center;
  justify-content: center;
  color: var(--text-muted);
  font-size: var(--text-sm);
  font-weight: 600;
}

.conversation-item .conv-info {
  flex: 1;
  min-width: 0;
}

.conversation-item .conv-name {
  font-weight: 500;
  font-size: var(--text-sm);
  color: var(--text);
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.conversation-item .conv-preview {
  font-size: var(--text-xs);
  color: var(--text-muted);
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
  margin-top: 2px;
}

.conversation-item .conv-meta {
  flex-shrink: 0;
  text-align: right;
  display: flex;
  flex-direction: column;
  align-items: flex-end;
}

.conversation-item .conv-time {
  font-size: 11px;
  color: var(--text-muted);
}

.conversation-item .unread-badge {
  min-width: 20px;
  height: 20px;
  border-radius: var(--radius-full);
  background: var(--primary);
  color: #ffffff;
  font-size: 11px;
  font-weight: 700;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 0 5px;
  margin-top: var(--space-1);
  line-height: 1;
}

.conversation-item .crew-dot {
  width: 8px;
  height: 8px;
  border-radius: var(--radius-full);
  display: inline-block;
  margin-right: 6px;
  flex-shrink: 0;
}

/* ========================================
   Empty State
   ======================================== */

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

.chat-empty p {
  margin: 0;
  font-size: var(--text-sm);
}

/* ========================================
   Chat Messages
   ======================================== */

.chat-messages {
  flex: 1;
  overflow-y: auto;
  padding: var(--space-4);
  display: flex;
  flex-direction: column;
  gap: var(--space-1);
}

.chat-bubble-wrap {
  display: flex;
  flex-direction: column;
  max-width: 80%;
}

.chat-bubble-wrap.own {
  align-self: flex-end;
}

.chat-bubble-wrap.other {
  align-self: flex-start;
}

.chat-bubble {
  padding: 10px 14px;
  border-radius: 16px;
  font-size: var(--text-sm);
  line-height: 1.4;
  word-break: break-word;
}

.chat-bubble-wrap.own .chat-bubble {
  background: var(--primary);
  color: #ffffff;
  border-bottom-right-radius: 4px;
}

.chat-bubble-wrap.other .chat-bubble {
  background: var(--bg-surface);
  color: var(--text);
  border-bottom-left-radius: 4px;
}

.sender-name {
  font-size: 11px;
  color: var(--text-muted);
  margin-bottom: 2px;
  margin-left: 14px;
}

.chat-time {
  font-size: 10px;
  color: var(--text-muted);
  margin-top: 2px;
  padding: 0 14px;
}

.chat-time.own {
  text-align: right;
}

.read-receipt {
  display: inline-flex;
  align-items: center;
  margin-left: 4px;
}

.read-receipt svg {
  width: 14px;
  height: 14px;
}

/* ========================================
   Deleted Message
   ======================================== */

.chat-bubble.deleted {
  font-style: italic;
  color: var(--text-muted);
  opacity: 0.7;
}

/* ========================================
   Input Bar
   ======================================== */

.chat-input-bar {
  display: flex;
  align-items: flex-end;
  padding: var(--space-3) var(--space-4);
  border-top: 1px solid var(--border);
  gap: var(--space-2);
  background: var(--bg-elevated);
  flex-shrink: 0;
}

.chat-input-bar textarea {
  flex: 1;
  resize: none;
  border: 1px solid var(--border);
  border-radius: 20px;
  padding: 10px 16px;
  background: var(--bg-surface);
  color: var(--text);
  font-size: var(--text-sm);
  font-family: var(--font-family);
  max-height: 120px;
  min-height: 40px;
  outline: none;
  line-height: 1.4;
  transition: border-color 0.15s ease;
}

.chat-input-bar textarea::placeholder {
  color: var(--text-muted);
}

.chat-input-bar textarea:focus {
  border-color: var(--border-focus);
}

.chat-input-bar .attach-btn,
.chat-input-bar .send-btn {
  width: 40px;
  height: 40px;
  border-radius: var(--radius-full);
  flex-shrink: 0;
  display: flex;
  align-items: center;
  justify-content: center;
  border: none;
  cursor: pointer;
  transition: background 0.15s ease, color 0.15s ease, opacity 0.15s ease;
}

.chat-input-bar .attach-btn i,
.chat-input-bar .attach-btn svg,
.chat-input-bar .send-btn i,
.chat-input-bar .send-btn svg {
  width: 20px;
  height: 20px;
}

.chat-input-bar .attach-btn {
  background: transparent;
  color: var(--text-muted);
}

.chat-input-bar .attach-btn:hover {
  color: var(--text);
  background: var(--bg-surface);
}

.chat-input-bar .send-btn {
  background: var(--primary);
  color: #ffffff;
}

.chat-input-bar .send-btn:hover {
  background: var(--primary-hover);
}

.chat-input-bar .send-btn:disabled {
  opacity: 0.5;
  cursor: not-allowed;
}

/* ========================================
   Attachment Preview (before send)
   ======================================== */

.chat-attachment-preview {
  display: flex;
  gap: var(--space-2);
  padding: var(--space-2) var(--space-4);
  overflow-x: auto;
  flex-wrap: nowrap;
  flex-shrink: 0;
  border-top: 1px solid var(--border);
}

.chat-attachment-item {
  position: relative;
  width: 64px;
  height: 64px;
  border-radius: var(--radius-sm);
  background: var(--bg-surface);
  overflow: hidden;
  flex-shrink: 0;
}

.chat-attachment-item img {
  width: 100%;
  height: 100%;
  object-fit: cover;
}

.chat-attachment-item .file-icon {
  display: flex;
  align-items: center;
  justify-content: center;
  width: 100%;
  height: 100%;
  color: var(--text-muted);
  font-size: var(--text-xs);
  text-align: center;
  padding: var(--space-1);
  word-break: break-all;
}

.chat-attachment-item .remove-btn {
  position: absolute;
  top: 2px;
  right: 2px;
  width: 20px;
  height: 20px;
  border-radius: var(--radius-full);
  background: rgba(0, 0, 0, 0.6);
  color: #ffffff;
  font-size: 12px;
  border: none;
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: center;
  line-height: 1;
  padding: 0;
}

.chat-attachment-item .remove-btn:hover {
  background: rgba(0, 0, 0, 0.8);
}

/* ========================================
   Attachment in Message
   ======================================== */

.message-attachment {
  display: flex;
  align-items: center;
  gap: var(--space-2);
  padding: 6px 10px;
  background: rgba(255, 255, 255, 0.1);
  border-radius: var(--radius-sm);
  margin-top: 6px;
  font-size: var(--text-xs);
  text-decoration: none;
  color: inherit;
  transition: background 0.15s ease;
}

.message-attachment:hover {
  background: rgba(255, 255, 255, 0.15);
}

.message-attachment img.thumb {
  width: 40px;
  height: 40px;
  border-radius: 4px;
  object-fit: cover;
  flex-shrink: 0;
}

.message-attachment .file-name {
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
  flex: 1;
  min-width: 0;
}

.message-attachment i,
.message-attachment svg {
  width: 16px;
  height: 16px;
  flex-shrink: 0;
}

/* ========================================
   New Message Picker
   ======================================== */

.new-message-search {
  padding: var(--space-3) var(--space-4);
  flex-shrink: 0;
}

.new-message-search input {
  width: 100%;
  border: 1px solid var(--border);
  border-radius: 20px;
  padding: 10px 16px;
  background: var(--bg-surface);
  color: var(--text);
  font-size: var(--text-sm);
  font-family: var(--font-family);
  outline: none;
  transition: border-color 0.15s ease;
  box-sizing: border-box;
}

.new-message-search input::placeholder {
  color: var(--text-muted);
}

.new-message-search input:focus {
  border-color: var(--border-focus);
}

.new-message-list {
  flex: 1;
  overflow-y: auto;
}

.new-message-section {
  padding: var(--space-2) var(--space-4);
  font-size: 11px;
  text-transform: uppercase;
  letter-spacing: 0.05em;
  color: var(--text-muted);
  font-weight: 600;
}

.new-message-item {
  display: flex;
  flex-direction: row;
  align-items: center;
  padding: 10px var(--space-4);
  gap: var(--space-3);
  cursor: pointer;
  transition: background 0.15s ease;
}

.new-message-item:hover {
  background: var(--bg-surface-hover);
}

.new-message-item .nm-avatar {
  width: 36px;
  height: 36px;
  border-radius: var(--radius-full);
  background: var(--bg-surface);
  flex-shrink: 0;
  display: flex;
  align-items: center;
  justify-content: center;
  color: var(--text-muted);
  font-size: var(--text-xs);
  font-weight: 600;
}

.new-message-item .nm-info {
  flex: 1;
  min-width: 0;
}

.new-message-item .nm-name {
  font-size: var(--text-sm);
  color: var(--text);
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.new-message-item .nm-role {
  font-size: var(--text-xs);
  color: var(--text-muted);
}

/* ========================================
   Loading State
   ======================================== */

.chat-loading {
  display: flex;
  justify-content: center;
  padding: var(--space-5);
  color: var(--text-muted);
  font-size: var(--text-sm);
}

.loading-dots {
  display: inline-flex;
  align-items: center;
}

@keyframes dot-pulse {
  0%, 80%, 100% {
    opacity: 0.3;
  }
  40% {
    opacity: 1;
  }
}

.loading-dots::after {
  content: '';
  display: inline-block;
  width: 4px;
  height: 4px;
  border-radius: var(--radius-full);
  background: var(--text-muted);
  margin-left: 6px;
  animation: dot-pulse 1.4s infinite;
}

/* ========================================
   Date Separator
   ======================================== */

.chat-date-sep {
  display: flex;
  align-items: center;
  justify-content: center;
  padding: var(--space-3) 0;
}

.chat-date-sep span {
  font-size: 11px;
  color: var(--text-muted);
  background: var(--bg-elevated);
  padding: 2px 10px;
  border-radius: var(--radius-full);
}

/* ========================================
   Custom Scrollbar
   ======================================== */

.conversation-list::-webkit-scrollbar,
.chat-messages::-webkit-scrollbar,
.new-message-list::-webkit-scrollbar,
.chat-attachment-preview::-webkit-scrollbar {
  width: 6px;
  height: 6px;
}

.conversation-list::-webkit-scrollbar-track,
.chat-messages::-webkit-scrollbar-track,
.new-message-list::-webkit-scrollbar-track,
.chat-attachment-preview::-webkit-scrollbar-track {
  background: transparent;
}

.conversation-list::-webkit-scrollbar-thumb,
.chat-messages::-webkit-scrollbar-thumb,
.new-message-list::-webkit-scrollbar-thumb,
.chat-attachment-preview::-webkit-scrollbar-thumb {
  background: var(--bg-surface-hover);
  border-radius: 3px;
}

.conversation-list::-webkit-scrollbar-thumb:hover,
.chat-messages::-webkit-scrollbar-thumb:hover,
.new-message-list::-webkit-scrollbar-thumb:hover,
.chat-attachment-preview::-webkit-scrollbar-thumb:hover {
  background: var(--border-hover);
}

/* Firefox scrollbar */
.conversation-list,
.chat-messages,
.new-message-list,
.chat-attachment-preview {
  scrollbar-width: thin;
  scrollbar-color: var(--bg-surface-hover) transparent;
}

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

@media (max-width: 767px) {
  .chat-panel {
    width: 100%;
    top: 0;
    border-left: none;
  }

  .chat-fab {
    bottom: 80px; /* Above bottom nav */
  }
}
