/* ── Chat page layout ──────────────────────────────────────────────────────── */
.chat-layout {
  display: flex;
  height: calc(100vh - var(--header-height));
  height: calc(100dvh - var(--header-height));
  overflow: hidden;
  gap: 0;
}

/* ── Contacts sidebar ──────────────────────────────────────────────────────── */
.chat-contacts {
  width: 300px;
  min-width: 240px;
  flex-shrink: 0;
  background: #fff;
  border-right: 1px solid var(--border-color);
  display: flex;
  flex-direction: column;
  overflow: hidden;
}

.chat-contacts-header {
  padding: 1rem 1.1rem 0.75rem;
  border-bottom: 1px solid var(--border-color);
  flex-shrink: 0;
}

.chat-contacts-header h4 {
  margin: 0 0 0.6rem;
  font-size: 0.95rem;
  font-weight: 700;
  color: var(--dark-text);
}

.chat-search {
  width: 100%;
  padding: 0.42rem 0.75rem;
  border: 1.5px solid var(--border-color);
  border-radius: 0.55rem;
  font-size: 0.83rem;
  color: var(--dark-text);
  background: #f8fafc;
  outline: none;
  transition: border-color 0.15s;
}
.chat-search:focus { border-color: var(--primary-color); background: #fff; }

.chat-contact-list {
  flex: 1;
  overflow-y: auto;
  padding: 0.4rem 0;
}

.chat-contact-section {
  padding: 0.55rem 1.1rem 0.35rem;
  font-size: 0.68rem;
  font-weight: 800;
  color: #667085;
  text-transform: uppercase;
  letter-spacing: 0.08em;
  display: flex;
  align-items: center;
  justify-content: space-between;
}

.chat-contact-section span:last-child {
  color: #98a2b3;
  font-size: 0.62rem;
  font-weight: 700;
}

.chat-contact-item {
  display: flex;
  align-items: center;
  gap: 0.75rem;
  padding: 0.7rem 1.1rem;
  cursor: pointer;
  transition: background 0.12s;
  border-left: 3px solid transparent;
}
.chat-contact-item:hover { background: #f4f7ff; }
.chat-contact-item.active {
  background: #eef2ff;
  border-left-color: var(--primary-color);
}

.chat-contact-avatar {
  width: 2.4rem;
  height: 2.4rem;
  border-radius: 50%;
  background: linear-gradient(135deg, #667eea, #764ba2);
  color: #fff;
  display: grid;
  place-items: center;
  font-size: 1rem;
  font-weight: 700;
  flex-shrink: 0;
  position: relative;
  overflow: hidden;
}

.chat-contact-avatar.business-logo-avatar {
  background: #fff;
  color: transparent;
  border: 1px solid #e5e7eb;
}

.chat-contact-avatar.business-logo-avatar img {
  width: 100%;
  height: 100%;
  object-fit: contain;
  padding: 3px;
  border-radius: inherit;
}

.chat-contact-info { flex: 1; min-width: 0; }
.chat-contact-name {
  font-size: 0.88rem;
  font-weight: 600;
  color: var(--dark-text);
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}
.chat-contact-preview {
  font-size: 0.75rem;
  color: var(--secondary-color);
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
  margin-top: 0.08rem;
}
.chat-contact-meta {
  flex-shrink: 0;
  display: flex;
  flex-direction: column;
  align-items: flex-end;
  gap: 0.3rem;
}
.chat-contact-time { font-size: 0.68rem; color: var(--secondary-color); }
.chat-unread-badge {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  background: var(--primary-color);
  color: #fff;
  font-size: 0.65rem;
  font-weight: 700;
  min-width: 1.25rem;
  height: 1.25rem;
  border-radius: 999px;
  padding: 0 0.3rem;
}
.chat-role-tag {
  display: inline-block;
  font-size: 0.6rem;
  font-weight: 700;
  padding: 0.1rem 0.38rem;
  border-radius: 999px;
  text-transform: uppercase;
  letter-spacing: 0.04em;
  background: #e0e7ff;
  color: #3730a3;
  vertical-align: middle;
}

/* ── Chat window ───────────────────────────────────────────────────────────── */
.chat-window {
  flex: 1;
  display: flex;
  flex-direction: column;
  background: #f8fafc;
  overflow: hidden;
}

.chat-window-placeholder {
  flex: 1;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  color: var(--secondary-color);
  gap: 0.75rem;
}
.chat-window-placeholder i { font-size: 3rem; opacity: 0.3; }
.chat-window-placeholder p { font-size: 0.9rem; }

.chat-window-header {
  padding: 0.7rem 1.1rem;
  background: #fff;
  border-bottom: 1px solid var(--border-color);
  display: flex;
  align-items: center;
  gap: 0.7rem;
  flex-shrink: 0;
  min-width: 0;
}
.chat-header-avatar {
  width: 2.3rem;
  height: 2.3rem;
  font-size: 0.9rem;
  flex-shrink: 0;
}
.chat-window-header-info {
  flex: 1;
  min-width: 0;
  display: flex;
  flex-direction: column;
  gap: 0.08rem;
}
.chat-window-header-name-row {
  display: flex;
  align-items: center;
  gap: 0.4rem;
  min-width: 0;
}
.chat-window-header-name {
  font-size: 0.92rem;
  font-weight: 700;
  color: var(--dark-text);
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
  line-height: 1.3;
}
.chat-header-role-tag {
  font-size: 0.64rem;
  font-weight: 700;
  padding: 0.1rem 0.42rem;
  border-radius: 999px;
  background: #e0e7ff;
  color: #3730a3;
  text-transform: uppercase;
  letter-spacing: 0.04em;
  white-space: nowrap;
  flex-shrink: 0;
}
.chat-header-phone {
  display: inline-flex;
  align-items: center;
  gap: 0.25rem;
  font-size: 0.71rem;
  color: var(--secondary-color);
  text-decoration: none;
  font-weight: 500;
  transition: color 0.15s;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}
.chat-header-phone:hover { color: var(--primary-color); }
.chat-header-phone i { font-size: 0.65rem; }

/* ── View Profile button ────────────────────────────────────────────────────── */
.chat-profile-btn {
  display: inline-flex;
  align-items: center;
  gap: 0.35rem;
  padding: 0.4rem 0.8rem;
  background: rgba(79,70,229,0.07);
  border: 1px solid rgba(99,102,241,0.2);
  border-radius: 0.6rem;
  color: #4f46e5;
  text-decoration: none;
  font-size: 0.76rem;
  font-weight: 600;
  white-space: nowrap;
  flex-shrink: 0;
  transition: background 0.18s, border-color 0.18s;
}
.chat-profile-btn:hover {
  background: rgba(79,70,229,0.13);
  border-color: rgba(99,102,241,0.38);
}
.chat-profile-btn i { font-size: 0.82rem; }

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

/* date separator */
.chat-date-sep {
  text-align: center;
  font-size: 0.7rem;
  color: var(--secondary-color);
  margin: 0.5rem 0;
  position: relative;
}
.chat-date-sep::before, .chat-date-sep::after {
  content: '';
  position: absolute;
  top: 50%;
  width: 38%;
  height: 1px;
  background: var(--border-color);
}
.chat-date-sep::before { left: 0; }
.chat-date-sep::after  { right: 0; }

.chat-bubble-row {
  display: flex;
  align-items: flex-end;
  gap: 0.45rem;
}
.chat-bubble-row.mine  { flex-direction: row-reverse; }
.chat-bubble-row.theirs { flex-direction: row; }

.chat-bubble {
  max-width: 65%;
  padding: 0.55rem 0.85rem;
  border-radius: 1rem;
  font-size: 0.88rem;
  line-height: 1.45;
  word-break: break-word;
  position: relative;
}
.chat-bubble.mine {
  background: var(--primary-color);
  color: #fff;
  border-bottom-right-radius: 0.2rem;
}
.chat-bubble.theirs {
  background: #fff;
  color: var(--dark-text);
  border: 1px solid var(--border-color);
  border-bottom-left-radius: 0.2rem;
}
.chat-bubble-time {
  font-size: 0.65rem;
  opacity: 0.6;
  margin-top: 0.2rem;
  text-align: right;
  display: flex;
  align-items: center;
  justify-content: flex-end;
  gap: 0.3rem;
}
.chat-read-tick { font-size: 0.7rem; }
.chat-read-tick.read { color: #93c5fd; }

.chat-typing-indicator {
  font-size: 0.75rem;
  color: var(--secondary-color);
  padding: 0.2rem 1.4rem 0;
  min-height: 1.4rem;
  font-style: italic;
  flex-shrink: 0;
  display: flex;
  align-items: center;
  gap: 0.4rem;
}
.chat-typing-indicator:not(:empty)::before {
  content: '';
  display: inline-flex;
  width: 6px; height: 6px;
  border-radius: 50%;
  background: var(--secondary-color);
  animation: chat-pulse 1s ease infinite;
  flex-shrink: 0;
}
@keyframes chat-pulse {
  0%, 100% { opacity: 0.4; transform: scale(0.85); }
  50%       { opacity: 1;   transform: scale(1); }
}

/* ── Premium composer ─────────────────────────────────────────────────────── */
.chat-composer-wrap {
  padding: 0.75rem 1.1rem 0.6rem;
  background: linear-gradient(to top, #ffffff 0%, rgba(255,255,255,0.97) 100%);
  border-top: 1px solid rgba(226,232,240,0.9);
  box-shadow: 0 -6px 24px rgba(15,23,42,0.05);
  flex-shrink: 0;
}

.chat-composer {
  display: flex;
  align-items: flex-end;
  gap: 0.55rem;
  background: #f8fafc;
  border: 1.5px solid #e2e8f0;
  border-radius: 1.1rem;
  padding: 0.45rem 0.45rem 0.45rem 1rem;
  transition: border-color 0.2s, box-shadow 0.2s, background 0.2s;
}
.chat-composer:focus-within {
  border-color: var(--primary-light);
  box-shadow: 0 0 0 3px rgba(99,102,241,0.1);
  background: #fff;
}

.chat-input {
  flex: 1;
  border: none;
  background: transparent;
  outline: none;
  resize: none;
  font-size: 0.875rem;
  font-family: inherit;
  line-height: 1.55;
  color: var(--dark-text);
  padding: 0.25rem 0;
  max-height: 120px;
  overflow-y: auto;
}
.chat-input::placeholder { color: #94a3b8; }

.chat-send-btn {
  width: 2.2rem;
  height: 2.2rem;
  border-radius: 0.75rem;
  background: linear-gradient(135deg, #4f46e5 0%, #6366f1 100%);
  color: #fff;
  border: none;
  cursor: pointer;
  display: grid;
  place-items: center;
  font-size: 0.82rem;
  flex-shrink: 0;
  transition: all 0.2s cubic-bezier(0.4,0,0.2,1);
  box-shadow: 0 2px 8px rgba(99,102,241,0.4);
}
.chat-send-btn:hover {
  background: linear-gradient(135deg, #4338ca 0%, #4f46e5 100%);
  transform: translateY(-1px);
  box-shadow: 0 4px 14px rgba(99,102,241,0.5);
}
.chat-send-btn:active { transform: scale(0.9); box-shadow: none; }
.chat-send-btn:disabled { opacity: 0.35; cursor: default; transform: none; box-shadow: none; }

.chat-composer-hint {
  text-align: center;
  font-size: 0.67rem;
  color: #b0bad0;
  margin-top: 0.42rem;
  letter-spacing: 0.01em;
  user-select: none;
}
.chat-composer-hint kbd {
  font-size: 0.62rem;
  background: #f1f5f9;
  border: 1px solid #e2e8f0;
  border-bottom-width: 2px;
  border-radius: 4px;
  padding: 0.05rem 0.28rem;
  color: #64748b;
  font-family: inherit;
  font-weight: 600;
}

/* empty contacts */
.chat-empty-contacts {
  text-align: center;
  padding: 2rem 1rem;
  color: var(--secondary-color);
  font-size: 0.85rem;
}

/* ── Mobile: stack layout ──────────────────────────────────────────────────── */
.chat-back-btn {
  display: none;
}

@media (max-width: 640px) {
  .chat-layout {
    flex-direction: column;
    height: calc(100vh - var(--header-height));
    height: calc(100dvh - var(--header-height));
  }
  .chat-contacts {
    width: 100%;
    min-width: unset;
    border-right: none;
    border-bottom: none;
    flex: 1;
    max-height: none;
  }
  /* Hide chat window (placeholder) on mobile until a contact is selected */
  .chat-window {
    display: none;
  }
  /* When a chat is open, hide contacts and show full chat window */
  .chat-layout.chat-open .chat-contacts {
    display: none;
  }
  .chat-layout.chat-open .chat-window {
    display: flex;
    flex: 1;
    min-height: 0;
  }
  .chat-back-btn {
    display: inline-flex;
    align-items: center;
    gap: 0.4rem;
    background: none;
    border: none;
    color: var(--primary-color);
    font-size: 0.88rem;
    font-weight: 600;
    cursor: pointer;
    padding: 0.2rem 0.4rem;
    margin-right: 0.25rem;
    flex-shrink: 0;
  }
  .chat-bubble { max-width: 85%; }
  .chat-window-header { padding: 0.55rem 0.85rem; gap: 0.55rem; }
  .chat-profile-btn span { display: none; }
  .chat-profile-btn { padding: 0.4rem 0.5rem; border-radius: 0.55rem; }
  .chat-composer-wrap { padding: 0.6rem 0.75rem 0.5rem; }
  .chat-composer-hint { display: none; }
}
