/* ==========================================================================
   Universal Real-Time Messaging API - Design System & Styles
   ========================================================================== */

:root {
  /* Color Palette - Refined Slate & Emerald (Blummerx) / Azure (Travwu) */
  --primary-color: #00b56a;
  --primary-hover: #009959;
  --primary-light: #e6f8f0;
  --primary-shadow: rgba(0, 181, 106, 0.25);

  --secondary-color: #2b3990;
  --secondary-light: #edf0ff;

  --bg-page: #f4f6f9;
  --bg-surface: #ffffff;
  --bg-surface-elevated: #ffffff;
  --bg-sidebar: #ffffff;
  --bg-hover: #f1f4f8;
  --bg-active: #eaf1fa;

  --border-light: #e5e9f0;
  --border-subtle: #edf1f7;
  --border-focus: #00b56a;

  --text-main: #1e293b;
  --text-muted: #64748b;
  --text-subtle: #94a3b8;
  --text-inverse: #ffffff;

  --bubble-incoming: #ffffff;
  --bubble-outgoing: #e1f7ec;
  --bubble-outgoing-border: #b8ebd4;
  --bubble-system: #fff8e6;
  --bubble-system-border: #fde68a;

  --status-online: #10b981;
  --status-offline: #94a3b8;
  --status-unread: #ff3b30;

  --danger-color: #ef4444;
  --warning-color: #f59e0b;
  --info-color: #3b82f6;

  --radius-sm: 6px;
  --radius-md: 10px;
  --radius-lg: 16px;
  --radius-full: 9999px;

  --shadow-sm: 0 1px 2px 0 rgba(0, 0, 0, 0.05);
  --shadow-md: 0 4px 6px -1px rgba(0, 0, 0, 0.08), 0 2px 4px -1px rgba(0, 0, 0, 0.04);
  --shadow-lg: 0 10px 15px -3px rgba(0, 0, 0, 0.1), 0 4px 6px -2px rgba(0, 0, 0, 0.05);

  --transition-fast: 150ms ease;
  --transition-normal: 250ms ease;
}

/* Base Reset & Typography */
* {
  box-sizing: border-box;
  margin: 0;
  padding: 0;
}

body {
  font-family: 'Inter', -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, sans-serif;
  background-color: var(--bg-page);
  color: var(--text-main);
  height: 100vh;
  overflow: hidden;
  -webkit-font-smoothing: antialiased;
}

button, input, select, textarea {
  font-family: inherit;
}

/* App Layout Grid */
.app-layout {
  display: flex;
  flex-direction: column;
  height: 100vh;
  max-width: 1600px;
  margin: 0 auto;
  background-color: var(--bg-surface);
  box-shadow: 0 0 30px rgba(0, 0, 0, 0.08);
}

/* Header Styles */
.app-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 12px 24px;
  background-color: var(--bg-surface);
  border-bottom: 1px solid var(--border-light);
  z-index: 20;
}

.brand-section {
  display: flex;
  align-items: center;
  gap: 14px;
}

.brand-logo {
  display: flex;
  align-items: center;
  gap: 10px;
  font-weight: 700;
  font-size: 1.15rem;
  color: var(--text-main);
}

.brand-icon {
  font-size: 1.4rem;
  color: var(--primary-color);
}

.platform-badge {
  display: inline-flex;
  align-items: center;
  gap: 6px;
  padding: 4px 10px;
  background-color: var(--primary-light);
  color: var(--primary-color);
  font-size: 0.78rem;
  font-weight: 600;
  border-radius: var(--radius-full);
}

.badge-dot {
  width: 7px;
  height: 7px;
  border-radius: 50%;
  background-color: currentColor;
}

.header-controls {
  display: flex;
  align-items: center;
  gap: 16px;
}

.tenant-selector-group {
  display: flex;
  align-items: center;
  gap: 8px;
}

.selector-label {
  font-size: 0.8rem;
  font-weight: 600;
  color: var(--text-muted);
}

.custom-select {
  padding: 6px 12px;
  border-radius: var(--radius-sm);
  border: 1px solid var(--border-light);
  background-color: var(--bg-surface);
  color: var(--text-main);
  font-size: 0.82rem;
  font-weight: 500;
  cursor: pointer;
  transition: border-color var(--transition-fast);
}

.custom-select:focus {
  outline: none;
  border-color: var(--primary-color);
  box-shadow: 0 0 0 3px var(--primary-shadow);
}

.connection-status {
  display: flex;
  align-items: center;
  gap: 6px;
  font-size: 0.78rem;
  font-weight: 500;
  padding: 4px 8px;
  background-color: var(--bg-hover);
  border-radius: var(--radius-sm);
}

.status-indicator {
  width: 8px;
  height: 8px;
  border-radius: 50%;
}

.status-indicator.online {
  background-color: var(--status-online);
  box-shadow: 0 0 8px rgba(16, 185, 129, 0.5);
}

.status-indicator.offline {
  background-color: var(--status-offline);
}

/* Chat Main Layout */
.chat-container {
  display: flex;
  flex: 1;
  overflow: hidden;
  position: relative;
}

/* Sidebar Styles (Jiji.ng inspired) */
.conversation-sidebar {
  width: 380px;
  min-width: 320px;
  background-color: var(--bg-sidebar);
  border-right: 1px solid var(--border-light);
  display: flex;
  flex-direction: column;
  overflow: hidden;
}

.sidebar-header {
  padding: 16px;
  border-bottom: 1px solid var(--border-light);
  background-color: var(--bg-surface);
}

.sidebar-title-row {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 14px;
}

.sidebar-title {
  font-size: 1.25rem;
  font-weight: 700;
  color: var(--text-main);
}

.total-unread-badge {
  background-color: var(--status-unread);
  color: var(--text-inverse);
  font-size: 0.72rem;
  font-weight: 700;
  padding: 2px 8px;
  border-radius: var(--radius-full);
}

/* Search Box */
.search-box {
  position: relative;
  margin-bottom: 12px;
}

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

.search-input {
  width: 100%;
  padding: 8px 36px 8px 36px;
  border-radius: var(--radius-md);
  border: 1px solid var(--border-light);
  background-color: var(--bg-page);
  font-size: 0.85rem;
  transition: all var(--transition-fast);
}

.search-input:focus {
  outline: none;
  background-color: var(--bg-surface);
  border-color: var(--primary-color);
  box-shadow: 0 0 0 3px var(--primary-shadow);
}

.clear-search-btn {
  position: absolute;
  right: 10px;
  top: 50%;
  transform: translateY(-50%);
  background: none;
  border: none;
  color: var(--text-subtle);
  cursor: pointer;
  padding: 4px;
}

/* Filter Tabs */
.filter-tabs {
  display: flex;
  gap: 6px;
  background-color: var(--bg-page);
  padding: 4px;
  border-radius: var(--radius-md);
}

.filter-tab {
  flex: 1;
  background: none;
  border: none;
  padding: 6px 4px;
  font-size: 0.78rem;
  font-weight: 600;
  color: var(--text-muted);
  border-radius: var(--radius-sm);
  cursor: pointer;
  text-align: center;
  transition: all var(--transition-fast);
}

.filter-tab.active {
  background-color: var(--bg-surface);
  color: var(--primary-color);
  box-shadow: var(--shadow-sm);
}

.filter-tab:hover:not(.active) {
  color: var(--text-main);
}

/* Conversation List Items */
.conversation-list {
  flex: 1;
  overflow-y: auto;
  padding: 8px;
}

.conversation-item {
  display: flex;
  align-items: center;
  gap: 12px;
  padding: 12px;
  border-radius: var(--radius-md);
  margin-bottom: 4px;
  cursor: pointer;
  transition: background-color var(--transition-fast), transform 100ms;
  position: relative;
}

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

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

.conversation-avatar-wrap {
  position: relative;
  flex-shrink: 0;
}

.conv-avatar {
  width: 48px;
  height: 48px;
  border-radius: 50%;
  object-fit: cover;
  background-color: var(--border-light);
}

.conv-online-dot {
  position: absolute;
  bottom: 2px;
  right: 2px;
  width: 11px;
  height: 11px;
  border-radius: 50%;
  background-color: var(--status-online);
  border: 2px solid var(--bg-surface);
}

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

.conv-top-row {
  display: flex;
  justify-content: space-between;
  align-items: baseline;
  margin-bottom: 2px;
}

.conv-name {
  font-size: 0.92rem;
  font-weight: 600;
  color: var(--text-main);
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.conv-time {
  font-size: 0.72rem;
  color: var(--text-subtle);
  white-space: nowrap;
  margin-left: 6px;
}

.conv-context-title {
  font-size: 0.76rem;
  font-weight: 600;
  color: var(--primary-color);
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
  margin-bottom: 2px;
}

.conv-message-row {
  display: flex;
  justify-content: space-between;
  align-items: center;
}

.conv-preview {
  font-size: 0.8rem;
  color: var(--text-muted);
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.conv-unread-badge {
  background-color: var(--status-unread);
  color: var(--text-inverse);
  font-size: 0.7rem;
  font-weight: 700;
  min-width: 18px;
  height: 18px;
  line-height: 18px;
  text-align: center;
  border-radius: var(--radius-full);
  padding: 0 4px;
  margin-left: 6px;
}

.conv-pin-icon {
  font-size: 0.7rem;
  color: var(--text-subtle);
  margin-right: 4px;
}

/* Active Chat Panel */
.active-chat-panel {
  flex: 1;
  display: flex;
  flex-direction: column;
  background-color: #f8fafc;
  overflow: hidden;
}

/* Chat Header */
.chat-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 12px 20px;
  background-color: var(--bg-surface);
  border-bottom: 1px solid var(--border-light);
  gap: 16px;
  position: relative;
}

.mobile-back-btn {
  display: none;
  background: none;
  border: none;
  font-size: 1.1rem;
  color: var(--text-muted);
  cursor: pointer;
  margin-right: 6px;
}

.participant-profile {
  display: flex;
  align-items: center;
  gap: 12px;
}

.avatar-wrapper {
  position: relative;
}

.avatar-img {
  width: 44px;
  height: 44px;
  border-radius: 50%;
  object-fit: cover;
  background-color: var(--border-light);
}

.presence-badge {
  position: absolute;
  bottom: 0;
  right: 0;
  width: 10px;
  height: 10px;
  border-radius: 50%;
  background-color: var(--status-offline);
  border: 2px solid var(--bg-surface);
}

.presence-badge.online {
  background-color: var(--status-online);
}

.participant-name {
  font-size: 1rem;
  font-weight: 600;
  color: var(--text-main);
}

.participant-status {
  font-size: 0.75rem;
  color: var(--text-muted);
}

/* Contextual Card in Header */
.context-card-header {
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 6px 12px;
  background-color: var(--bg-page);
  border: 1px solid var(--border-light);
  border-radius: var(--radius-md);
  max-width: 380px;
}

.context-thumb {
  width: 36px;
  height: 36px;
  border-radius: var(--radius-sm);
  object-fit: cover;
}

.context-details {
  flex: 1;
  min-width: 0;
}

.context-tag {
  font-size: 0.65rem;
  font-weight: 700;
  text-transform: uppercase;
  color: var(--primary-color);
  letter-spacing: 0.5px;
}

.context-title {
  font-size: 0.8rem;
  font-weight: 600;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.context-price {
  font-size: 0.78rem;
  font-weight: 700;
  color: var(--text-main);
}

.context-action-btn {
  background-color: var(--primary-color);
  color: var(--text-inverse);
  border: none;
  padding: 6px 12px;
  font-size: 0.78rem;
  font-weight: 600;
  border-radius: var(--radius-sm);
  cursor: pointer;
  display: inline-flex;
  align-items: center;
  gap: 4px;
  transition: background-color var(--transition-fast);
}

.context-action-btn:hover {
  background-color: var(--primary-hover);
}

/* Chat Action Menu */
.chat-actions-menu {
  position: relative;
}

.action-menu-btn {
  background: none;
  border: none;
  width: 36px;
  height: 36px;
  border-radius: 50%;
  color: var(--text-muted);
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: center;
  transition: background-color var(--transition-fast);
}

.action-menu-btn:hover {
  background-color: var(--bg-hover);
  color: var(--text-main);
}

.action-dropdown-menu {
  position: absolute;
  right: 0;
  top: 42px;
  background-color: var(--bg-surface);
  border: 1px solid var(--border-light);
  border-radius: var(--radius-md);
  box-shadow: var(--shadow-lg);
  min-width: 200px;
  z-index: 50;
  padding: 6px 0;
}

.dropdown-item {
  width: 100%;
  padding: 8px 16px;
  border: none;
  background: none;
  font-size: 0.84rem;
  color: var(--text-main);
  display: flex;
  align-items: center;
  gap: 10px;
  cursor: pointer;
  text-align: left;
  transition: background-color var(--transition-fast);
}

.dropdown-item:hover {
  background-color: var(--bg-hover);
}

.dropdown-item.text-danger {
  color: var(--danger-color);
}

.dropdown-divider {
  height: 1px;
  background-color: var(--border-light);
  margin: 4px 0;
}

/* Safety Alert Banner */
.safety-banner {
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 10px 16px;
  background-color: #fffbeb;
  border-bottom: 1px solid #fef3c7;
  color: #92400e;
  font-size: 0.78rem;
  line-height: 1.4;
}

.safety-icon {
  font-size: 1rem;
  color: var(--warning-color);
  flex-shrink: 0;
}

.safety-text {
  flex: 1;
  font-weight: 500;
}

.safety-close-btn {
  background: none;
  border: none;
  color: #b45309;
  cursor: pointer;
  padding: 2px 6px;
}

/* Messages History Stream */
.messages-history {
  flex: 1;
  overflow-y: auto;
  padding: 20px;
  display: flex;
  flex-direction: column;
  gap: 12px;
}

.chat-placeholder-state {
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  height: 100%;
  color: var(--text-muted);
  text-align: center;
  max-width: 420px;
  margin: auto;
}

.placeholder-icon {
  font-size: 3.5rem;
  color: var(--text-subtle);
  margin-bottom: 16px;
}

.chat-placeholder-state h3 {
  font-size: 1.15rem;
  font-weight: 700;
  color: var(--text-main);
  margin-bottom: 8px;
}

.chat-placeholder-state p {
  font-size: 0.85rem;
  line-height: 1.5;
}

/* Message Bubbles */
.date-separator {
  text-align: center;
  margin: 12px 0;
  position: relative;
}

.date-separator span {
  background-color: #e2e8f0;
  color: var(--text-muted);
  font-size: 0.72rem;
  font-weight: 600;
  padding: 3px 12px;
  border-radius: var(--radius-full);
}

.message-row {
  display: flex;
  gap: 8px;
  max-width: 75%;
}

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

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

.message-bubble {
  padding: 10px 14px;
  border-radius: var(--radius-lg);
  font-size: 0.88rem;
  line-height: 1.45;
  box-shadow: var(--shadow-sm);
  position: relative;
  word-break: break-word;
}

.message-row.outgoing .message-bubble {
  background-color: var(--bubble-outgoing);
  border: 1px solid var(--bubble-outgoing-border);
  border-bottom-right-radius: 4px;
}

.message-row.incoming .message-bubble {
  background-color: var(--bubble-incoming);
  border: 1px solid var(--border-light);
  border-bottom-left-radius: 4px;
}

.message-row.system-row {
  align-self: center;
  max-width: 85%;
}

.message-bubble.system-alert {
  background-color: var(--bubble-system);
  border: 1px solid var(--bubble-system-border);
  color: #78350f;
  font-size: 0.82rem;
  border-radius: var(--radius-md);
  padding: 10px 14px;
  display: flex;
  align-items: flex-start;
  gap: 8px;
}

.message-meta {
  display: flex;
  align-items: center;
  justify-content: flex-end;
  gap: 4px;
  font-size: 0.7rem;
  color: var(--text-subtle);
  margin-top: 4px;
}

/* Receipts checkmarks */
.receipt-tick {
  font-size: 0.75rem;
}

.receipt-tick.sent {
  color: var(--text-subtle);
}

.receipt-tick.delivered {
  color: var(--text-subtle);
}

.receipt-tick.read {
  color: #2563eb;
}

/* Attachments inside bubble */
.message-attachment-card {
  margin-top: 6px;
  border-radius: var(--radius-sm);
  overflow: hidden;
  border: 1px solid rgba(0, 0, 0, 0.08);
}

.message-attachment-img {
  max-width: 100%;
  max-height: 250px;
  display: block;
  object-fit: cover;
  border-radius: var(--radius-sm);
}

.message-attachment-doc {
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 8px 12px;
  background-color: rgba(0, 0, 0, 0.04);
  text-decoration: none;
  color: var(--text-main);
  font-size: 0.82rem;
  font-weight: 500;
}

/* Typing Indicator */
.typing-indicator-bar {
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 6px 20px;
  font-size: 0.78rem;
  color: var(--text-muted);
}

.typing-dots {
  display: flex;
  gap: 3px;
}

.typing-dots span {
  width: 5px;
  height: 5px;
  background-color: var(--text-muted);
  border-radius: 50%;
  animation: typingBounce 1.4s infinite ease-in-out both;
}

.typing-dots span:nth-child(1) { animation-delay: -0.32s; }
.typing-dots span:nth-child(2) { animation-delay: -0.16s; }

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

/* Message Composer */
.message-composer {
  padding: 12px 20px;
  background-color: var(--bg-surface);
  border-top: 1px solid var(--border-light);
  display: flex;
  flex-direction: column;
  gap: 8px;
  position: relative;
}

.composer-input-row {
  display: flex;
  align-items: flex-end;
  gap: 10px;
  position: relative;
}

.composer-tool-btn {
  background: none;
  border: none;
  color: var(--text-muted);
  width: 38px;
  height: 38px;
  border-radius: 50%;
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 1.1rem;
  transition: background-color var(--transition-fast), color var(--transition-fast);
}

.composer-tool-btn:hover {
  background-color: var(--bg-hover);
  color: var(--text-main);
}

.message-textarea {
  flex: 1;
  min-height: 40px;
  max-height: 120px;
  padding: 10px 14px;
  border-radius: var(--radius-lg);
  border: 1px solid var(--border-light);
  background-color: var(--bg-page);
  color: var(--text-main);
  font-size: 0.88rem;
  line-height: 1.4;
  resize: none;
  transition: all var(--transition-fast);
}

.message-textarea:focus {
  outline: none;
  background-color: var(--bg-surface);
  border-color: var(--primary-color);
  box-shadow: 0 0 0 3px var(--primary-shadow);
}

.send-message-btn {
  background-color: var(--primary-color);
  color: var(--text-inverse);
  border: none;
  width: 40px;
  height: 40px;
  border-radius: 50%;
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 1rem;
  box-shadow: var(--shadow-sm);
  transition: all var(--transition-fast);
}

.send-message-btn:hover {
  background-color: var(--primary-hover);
  transform: scale(1.05);
}

.send-message-btn:active {
  transform: scale(0.96);
}

.composer-footer-hint {
  display: flex;
  justify-content: space-between;
  align-items: center;
  font-size: 0.72rem;
  color: var(--text-subtle);
  padding: 0 4px;
}

.enter-to-send-toggle {
  display: flex;
  align-items: center;
  gap: 5px;
  cursor: pointer;
  user-select: none;
}

/* Emoji Popover */
.emoji-popover {
  position: absolute;
  bottom: 50px;
  left: 50px;
  background-color: var(--bg-surface);
  border: 1px solid var(--border-light);
  border-radius: var(--radius-md);
  box-shadow: var(--shadow-lg);
  padding: 8px;
  display: grid;
  grid-template-columns: repeat(4, 1fr);
  gap: 6px;
  z-index: 100;
}

.emoji-btn {
  background: none;
  border: none;
  font-size: 1.3rem;
  padding: 6px;
  cursor: pointer;
  border-radius: var(--radius-sm);
  transition: background-color var(--transition-fast);
}

.emoji-btn:hover {
  background-color: var(--bg-hover);
}

/* Attachment Preview */
.attachment-preview-strip {
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 6px 10px;
  background-color: var(--bg-hover);
  border-radius: var(--radius-sm);
}

.preview-card {
  display: flex;
  align-items: center;
  gap: 8px;
  font-size: 0.8rem;
  font-weight: 500;
}

.remove-attachment-btn {
  background: none;
  border: none;
  color: var(--danger-color);
  cursor: pointer;
}

.upload-progress-bar {
  flex: 1;
  height: 4px;
  background-color: var(--border-light);
  border-radius: var(--radius-full);
  overflow: hidden;
}

.progress-fill {
  width: 0%;
  height: 100%;
  background-color: var(--primary-color);
  transition: width 200ms ease;
}

/* Spinner */
.spinner {
  width: 32px;
  height: 32px;
  border: 3px solid var(--border-light);
  border-top-color: var(--primary-color);
  border-radius: 50%;
  animation: spin 0.8s linear infinite;
  margin: 20px auto 10px;
}

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

.loading-state {
  text-align: center;
  color: var(--text-muted);
  font-size: 0.84rem;
  padding: 20px;
}

/* Responsive Rules */
@media (max-width: 900px) {
  .app-header {
    flex-wrap: wrap;
    gap: 10px;
  }
  .conversation-sidebar {
    width: 300px;
  }
}

@media (max-width: 768px) {
  .conversation-sidebar {
    width: 100%;
  }
  .active-chat-panel {
    display: none;
  }
  .app-layout.chat-open .conversation-sidebar {
    display: none;
  }
  .app-layout.chat-open .active-chat-panel {
    display: flex;
  }
  .mobile-back-btn {
    display: inline-flex;
  }
  .context-card-header {
    display: none;
  }
}
