/* ==========================================================================
   Zylink Corp Dashboard — Chat Styles
   iMessage-inspired chat UI with bubble tails, grouping, glass input bar.
   All values reference design tokens from tokens.css.
   ========================================================================== */

/* --------------------------------------------------------------------------
   1. CHAT CONTAINER — Full-height flex column
   -------------------------------------------------------------------------- */
.chat-container {
  display: flex;
  flex-direction: column;
  block-size: 100%;
  min-block-size: 0; /* Allow flex child to shrink */
}

/* --------------------------------------------------------------------------
   2. CHAT THREAD HEADER — Project name, online status
   -------------------------------------------------------------------------- */
.chat-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding-block: var(--space-2);
  padding-inline: var(--space-4);
  border-block-end: 1px solid var(--color-border);
  background-color: var(--color-surface);
  flex-shrink: 0;
}

.chat-header__info {
  display: flex;
  flex-direction: column;
  gap: 1px;
}

.chat-header__title {
  font-size: var(--font-size-headline);
  font-weight: var(--font-weight-semibold);
  color: var(--color-text-primary);
}

.chat-header__status {
  display: flex;
  align-items: center;
  gap: var(--space-1);
  font-size: var(--font-size-caption);
  color: var(--color-text-secondary);
}

/* Online indicator dot */
.chat-header__status-dot {
  inline-size: 6px;
  block-size: 6px;
  border-radius: var(--radius-full);
  background-color: var(--color-status-done);
}

.chat-header__status-dot--offline {
  background-color: var(--color-text-secondary);
}

.chat-header__actions {
  display: flex;
  align-items: center;
  gap: var(--space-2);
}

/* --------------------------------------------------------------------------
   3. CHAT MESSAGES AREA — Scrollable, flex-grow
   -------------------------------------------------------------------------- */
.chat-messages {
  flex: 1 1 0%;
  overflow-y: auto;
  overflow-x: hidden;
  padding-block: var(--space-4);
  padding-inline: var(--space-4);
  /* Extra padding at bottom so last message isn't hidden behind input bar */
  padding-block-end: var(--space-10);
  display: flex;
  flex-direction: column;
  gap: var(--space-1);
}

/* --------------------------------------------------------------------------
   4. CHAT BUBBLE — iMessage-style, sender/receiver variants
   -------------------------------------------------------------------------- */
.chat-bubble {
  position: relative;
  max-inline-size: 75%;
  padding-block: var(--space-2);
  padding-inline: var(--space-3);
  font-size: var(--font-size-body);
  line-height: var(--line-height-body);
  word-wrap: break-word;
  overflow-wrap: break-word;
}

/* Received (left-aligned) */
.chat-bubble--received {
  align-self: flex-start;
  background-color: var(--color-surface);
  color: var(--color-text-primary);
  border: 1px solid var(--color-border);
  border-radius: 18px 18px 4px 18px;
}

/* Sent (right-aligned) */
.chat-bubble--sent {
  align-self: flex-end;
  background-color: var(--color-accent);
  color: #ffffff;
  border-radius: 18px 18px 18px 4px;
}

/* --------------------------------------------------------------------------
   5. MESSAGE GROUPING — Consecutive messages from same sender
       - Reduced spacing between grouped messages
       - Only the last message in a group has the full tail radius
   -------------------------------------------------------------------------- */
.chat-bubble--grouped {
  margin-block-start: -2px;
}

/* Received grouped — middle messages lose bottom-left radius */
.chat-bubble--received.chat-bubble--grouped {
  border-radius: 18px 18px 4px 18px;
}

/* Sent grouped — middle messages lose bottom-right radius */
.chat-bubble--sent.chat-bubble--grouped {
  border-radius: 18px 18px 18px 4px;
}

/* Last in group — restore full tail */
.chat-bubble--grouped-end.chat-bubble--received {
  border-radius: 18px 18px 4px 18px;
}

.chat-bubble--grouped-end.chat-bubble--sent {
  border-radius: 18px 18px 18px 4px;
}

/* First in group — top corners tighten */
.chat-bubble--grouped-start.chat-bubble--received {
  border-radius: 18px 18px 4px 18px;
}

.chat-bubble--grouped-start.chat-bubble--sent {
  border-radius: 18px 18px 18px 4px;
}

/* Middle messages — no tail at all, tighter corners */
.chat-bubble--grouped-middle.chat-bubble--received {
  border-radius: 18px 18px 4px 18px;
}

.chat-bubble--grouped-middle.chat-bubble--sent {
  border-radius: 18px 18px 18px 4px;
}

/* --------------------------------------------------------------------------
   6. CHAT BUBBLE CONTENT — Text, timestamps, sender, role badge
   -------------------------------------------------------------------------- */
.chat-bubble__text {
  white-space: pre-wrap;
}

/* Sender name — shown for received messages in group chats */
.chat-bubble__sender {
  display: block;
  font-size: var(--font-size-caption);
  font-weight: var(--font-weight-semibold);
  color: var(--color-accent);
  margin-block-end: var(--space-1);
}

/* Role badge for admin messages */
.chat-bubble__role {
  display: inline-flex;
  align-items: center;
  padding-block: 1px;
  padding-inline: var(--space-1);
  border-radius: var(--radius-full);
  background-color: color-mix(in srgb, var(--color-accent) 15%, transparent);
  font-size: 10px;
  font-weight: var(--font-weight-semibold);
  color: var(--color-accent);
  margin-inline-start: var(--space-1);
  letter-spacing: var(--letter-spacing-wide);
  text-transform: uppercase;
}

/* Timestamp — visible on hover / tap only */
.chat-bubble__timestamp {
  font-size: var(--font-size-caption);
  color: var(--color-text-secondary);
  opacity: 0;
  transition: opacity var(--duration-fast) var(--ease-standard);
  margin-block-start: var(--space-1);
  pointer-events: none;
}

.chat-bubble:hover .chat-bubble__timestamp,
.chat-bubble:focus-within .chat-bubble__timestamp,
.chat-bubble--show-timestamp .chat-bubble__timestamp {
  opacity: 1;
}

/* Sent bubbles have lighter timestamp */
.chat-bubble--sent .chat-bubble__timestamp {
  color: rgba(255, 255, 255, 0.7);
}

/* --------------------------------------------------------------------------
   7. CHAT INPUT BAR — Glass / blur, sticky bottom
   -------------------------------------------------------------------------- */
.chat-input-bar {
  position: sticky;
  inset-block-end: 0;
  z-index: 10;
  display: flex;
  align-items: flex-end;
  gap: var(--space-2);
  padding-block: var(--space-2);
  padding-inline: var(--space-4);
  background-color: var(--glass-background);
  backdrop-filter: var(--glass-blur);
  -webkit-backdrop-filter: var(--glass-blur);
  border-block-start: 1px solid var(--color-border);
  flex-shrink: 0;
}

.chat-input-bar__textarea {
  flex: 1 1 0%;
  min-block-size: 36px;
  max-block-size: 120px;
  padding-block: var(--space-2);
  padding-inline: var(--space-3);
  font-size: var(--font-size-body);
  line-height: var(--line-height-body);
  color: var(--color-text-primary);
  background-color: var(--color-surface);
  border: 1px solid var(--color-border);
  border-radius: 18px;
  resize: none;
  overflow-y: auto;
  transition: border-color var(--duration-fast) var(--ease-standard),
              box-shadow var(--duration-fast) var(--ease-standard);
}

.chat-input-bar__textarea::placeholder {
  color: var(--color-text-secondary);
  opacity: 0.6;
}

.chat-input-bar__textarea:focus {
  outline: none;
  border-color: var(--color-accent);
  box-shadow: 0 0 0 3px rgba(0, 113, 227, 0.15);
}

/* Send button */
.chat-input-bar__send {
  display: flex;
  align-items: center;
  justify-content: center;
  inline-size: 36px;
  block-size: 36px;
  border-radius: var(--radius-full);
  background-color: var(--color-accent);
  color: #ffffff;
  flex-shrink: 0;
  transition: background-color var(--duration-fast) var(--ease-standard),
              opacity var(--duration-fast) var(--ease-standard);
}

.chat-input-bar__send:hover:not(:disabled) {
  background-color: var(--color-accent-hover);
}

.chat-input-bar__send:disabled {
  opacity: 0.3;
  cursor: not-allowed;
}

/* --------------------------------------------------------------------------
   8. TYPING INDICATOR — Three bouncing dots
   -------------------------------------------------------------------------- */
.typing-indicator {
  display: inline-flex;
  align-items: center;
  gap: 3px;
  padding-block: var(--space-2);
  padding-inline: var(--space-3);
  background-color: var(--color-surface);
  border: 1px solid var(--color-border);
  border-radius: 18px 18px 4px 18px;
  align-self: flex-start;
}

.typing-indicator__dot {
  inline-size: 7px;
  block-size: 7px;
  border-radius: var(--radius-full);
  background-color: var(--color-text-secondary);
  animation: typing-bounce 1.4s ease-in-out infinite;
}

.typing-indicator__dot:nth-child(2) {
  animation-delay: 0.2s;
}

.typing-indicator__dot:nth-child(3) {
  animation-delay: 0.4s;
}

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

/* --------------------------------------------------------------------------
   9. UNREAD BADGE — Red dot / counter on chat icon
   -------------------------------------------------------------------------- */
.unread-badge {
  position: absolute;
  inset-block-start: -4px;
  inset-inline-end: -4px;
  display: flex;
  align-items: center;
  justify-content: center;
  min-inline-size: 16px;
  block-size: 16px;
  padding-inline: 4px;
  border-radius: var(--radius-full);
  background-color: var(--color-status-blocked);
  color: #ffffff;
  font-size: 10px;
  font-weight: var(--font-weight-bold);
  line-height: 1;
  z-index: 1;
  pointer-events: none;
}

/* Dot-only variant (no count) */
.unread-badge--dot {
  min-inline-size: 8px;
  block-size: 8px;
  padding: 0;
  inset-block-start: -2px;
  inset-inline-end: -2px;
}

.unread-badge:empty {
  display: none;
}

/* --------------------------------------------------------------------------
   10. FAILED MESSAGE — Retry button
   -------------------------------------------------------------------------- */
.chat-bubble--failed {
  opacity: 0.7;
}

.chat-bubble--failed::after {
  content: "";
  position: absolute;
  inset: 0;
  border-radius: inherit;
  background-color: rgba(255, 59, 48, 0.05);
}

.chat-bubble__retry {
  display: inline-flex;
  align-items: center;
  gap: var(--space-1);
  margin-block-start: var(--space-1);
  padding-block: 1px;
  padding-inline: var(--space-2);
  border-radius: var(--radius-full);
  background-color: transparent;
  color: var(--color-status-blocked);
  font-size: var(--font-size-caption);
  font-weight: var(--font-weight-medium);
  cursor: pointer;
  transition: background-color var(--duration-fast) var(--ease-standard);
}

.chat-bubble__retry:hover {
  background-color: color-mix(in srgb, var(--color-status-blocked) 10%, transparent);
}

.chat-bubble__retry-icon {
  inline-size: 12px;
  block-size: 12px;
}

/* --------------------------------------------------------------------------
   11. DATE SEPARATOR — Day boundary between messages
   -------------------------------------------------------------------------- */
.chat-date-separator {
  display: flex;
  align-items: center;
  justify-content: center;
  padding-block: var(--space-4);
}

.chat-date-separator__label {
  font-size: var(--font-size-caption);
  font-weight: var(--font-weight-medium);
  color: var(--color-text-secondary);
  letter-spacing: var(--letter-spacing-wide);
  padding-block: var(--space-1);
  padding-inline: var(--space-3);
  background-color: var(--color-background);
  border-radius: var(--radius-full);
}

/* --------------------------------------------------------------------------
   12. NEW MESSAGE INDICATOR
   -------------------------------------------------------------------------- */
.chat-new-messages {
  display: flex;
  align-items: center;
  justify-content: center;
  padding-block: var(--space-2);
}

.chat-new-messages__label {
  font-size: var(--font-size-caption);
  font-weight: var(--font-weight-semibold);
  color: var(--color-accent);
  letter-spacing: var(--letter-spacing-wide);
  text-transform: uppercase;
}

/* --------------------------------------------------------------------------
   13. REDUCED MOTION — Disable chat animations
   -------------------------------------------------------------------------- */
@media (prefers-reduced-motion: reduce) {
  .typing-indicator__dot {
    animation: none;
    opacity: 0.6;
  }

  .chat-bubble__timestamp {
    opacity: 1; /* Always visible when motion reduced */
  }
}

/* --------------------------------------------------------------------------
   14. RESPONSIVE — Mobile chat layout
   -------------------------------------------------------------------------- */
@media (max-width: 768px) {
  .chat-bubble {
    max-inline-size: 85%;
  }

  .chat-messages {
    padding-inline: var(--space-3);
  }

  .chat-input-bar {
    padding-inline: var(--space-3);
  }

  /* On mobile, timestamps always visible (no hover) */
  .chat-bubble__timestamp {
    opacity: 1;
    font-size: 11px;
  }
}
