/* ═══════════════════════════════════════════
   Facebook-style chat: dropdown + bottom windows
   ═══════════════════════════════════════════ */

/* ── Chat dropdown (from topbar icon) ── */
.topbar__chat { position: relative; flex-shrink: 0; }

.chat-dropdown {
  position: absolute; right: 0; top: calc(100% + .5rem);
  width: 20rem; max-height: 26rem; overflow-y: auto;
  background: var(--bg-surface, #fff); border: 1px solid var(--border);
  border-radius: .75rem; box-shadow: 0 8px 24px rgba(0,0,0,.12);
  opacity: 0; visibility: hidden; transform: translateY(-4px);
  transition: opacity .15s, visibility .15s, transform .15s;
  z-index: 50;
}
.chat-dropdown--open { opacity: 1; visibility: visible; transform: translateY(0); }
.chat-dropdown__header {
  display: flex; align-items: center; justify-content: space-between;
  padding: .75rem 1rem; border-bottom: 1px solid var(--border);
  position: sticky; top: 0; background: var(--bg-surface, #fff); z-index: 1;
}
.chat-dropdown__title { font-size: .875rem; font-weight: 600; color: var(--text-primary); }
.chat-dropdown__list { padding: .25rem 0; }
.chat-dropdown__item {
  display: flex; align-items: center; gap: .625rem;
  padding: .5rem 1rem; cursor: pointer; transition: background .12s;
}
.chat-dropdown__item:hover { background: var(--bg-hover, #f4f4f5); }
.chat-dropdown__item--unread { font-weight: 600; }
.chat-dropdown__avatar {
  width: 2rem; height: 2rem; border-radius: 50%;
  background: linear-gradient(135deg, #6366f1, #8b5cf6);
  display: flex; align-items: center; justify-content: center;
  color: #fff; font-size: .6rem; font-weight: 700; flex-shrink: 0;
  text-transform: uppercase;
}
.chat-dropdown__avatar--group { background: linear-gradient(135deg, #06b6d4, #3b82f6); }
.chat-dropdown__info { flex: 1; min-width: 0; }
.chat-dropdown__name {
  font-size: .8125rem; color: var(--text-primary);
  overflow: hidden; text-overflow: ellipsis; white-space: nowrap;
}
.chat-dropdown__preview {
  font-size: .7rem; color: var(--text-muted); margin-top: .05rem;
  overflow: hidden; text-overflow: ellipsis; white-space: nowrap;
  font-weight: 400;
}
.chat-dropdown__time { font-size: .625rem; color: var(--text-muted); flex-shrink: 0; }
.chat-dropdown__badge {
  min-width: .875rem; height: .875rem; padding: 0 .25rem;
  border-radius: 999px; background: #6366f1; color: #fff;
  font-size: .575rem; font-weight: 700; line-height: .875rem;
  text-align: center; flex-shrink: 0;
}
.chat-dropdown__section {
  padding: .4rem .75rem .2rem; font-size: .625rem; font-weight: 600;
  text-transform: uppercase; letter-spacing: .06em; color: var(--text-muted);
  border-top: 1px solid var(--border); margin-top: .125rem;
}
.chat-dropdown__empty {
  padding: 1.5rem 1rem; text-align: center; color: var(--text-muted); font-size: .8125rem;
}

/* ── Bottom chat windows ── */
#chatWindows {
  position: fixed; bottom: 0; right: 0;
  display: flex; flex-direction: row-reverse;
  align-items: flex-end; gap: .5rem;
  padding: 0 .75rem; z-index: 200;
  pointer-events: none;
}

.chat-win {
  width: 328px; height: 420px;
  background: var(--bg-surface, #fff);
  border: 1px solid var(--border);
  border-bottom: none;
  border-radius: .75rem .75rem 0 0;
  box-shadow: 0 -4px 24px rgba(0,0,0,.1);
  display: flex; flex-direction: column;
  pointer-events: auto;
  animation: chatWinIn .2s ease;
}
@keyframes chatWinIn {
  from { opacity: 0; transform: translateY(20px); }
  to { opacity: 1; transform: translateY(0); }
}

/* Window header */
.chat-win__header {
  display: flex; align-items: center; gap: .5rem;
  padding: .5rem .625rem; border-bottom: 1px solid var(--border);
  flex-shrink: 0; border-radius: .75rem .75rem 0 0;
  background: var(--bg-surface, #fff); cursor: default;
}
.chat-win__avatar {
  width: 1.75rem; height: 1.75rem; border-radius: 50%;
  background: linear-gradient(135deg, #6366f1, #8b5cf6);
  display: flex; align-items: center; justify-content: center;
  color: #fff; font-size: .55rem; font-weight: 700; flex-shrink: 0;
}
.chat-win__name {
  flex: 1; min-width: 0; font-size: .8125rem; font-weight: 600;
  color: var(--text-primary); overflow: hidden; text-overflow: ellipsis;
  white-space: nowrap;
}
.chat-win__minimize, .chat-win__close {
  width: 1.625rem; height: 1.625rem; border-radius: .375rem;
  border: none; background: transparent; cursor: pointer;
  display: flex; align-items: center; justify-content: center;
  color: var(--text-muted); transition: background .12s;
  flex-shrink: 0;
}
.chat-win__minimize:hover, .chat-win__close:hover { background: var(--bg-hover, #f4f4f5); color: var(--text-primary); }
.chat-win__minimize svg, .chat-win__close svg { width: .875rem; height: .875rem; }

/* Minimized state — collapse to header only */
.chat-win--minimized { height: auto; }
.chat-win--minimized .chat-win__messages,
.chat-win--minimized .chat-win__compose,
.chat-win--minimized .chat-win__file-preview { display: none; }
.chat-win--minimized .chat-win__header { cursor: pointer; }
.chat-win--minimized .chat-win__minimize svg { transform: rotate(0); }

/* Messages area */
.chat-win__messages {
  flex: 1 1 0; overflow-y: auto; padding: .5rem .625rem;
  display: flex; flex-direction: column; gap: .125rem;
  min-height: 0; height: 0;
}

/* Message bubbles */
.chat-win__msg {
  display: flex; gap: .375rem; max-width: 88%; margin-bottom: .125rem;
}
.chat-win__msg--mine { margin-left: auto; flex-direction: row-reverse; }
.chat-win__msg-avatar {
  width: 1.375rem; height: 1.375rem; border-radius: 50%;
  background: linear-gradient(135deg, #6366f1, #8b5cf6);
  display: flex; align-items: center; justify-content: center;
  color: #fff; font-size: .45rem; font-weight: 700; flex-shrink: 0;
  margin-top: .125rem;
}
.chat-win__msg--mine .chat-win__msg-avatar { display: none; }
.chat-win__msg-body {}
.chat-win__msg-sender {
  font-size: .6rem; font-weight: 600; color: #6366f1; margin-bottom: .0625rem;
}
.chat-win__msg--mine .chat-win__msg-sender { display: none; }
.chat-win__msg-bubble {
  padding: .375rem .625rem; border-radius: .625rem;
  font-size: .8rem; line-height: 1.35; word-break: break-word;
}
.chat-win__msg--theirs .chat-win__msg-bubble {
  background: var(--bg-hover, #f4f4f5); color: var(--text-primary);
  border-top-left-radius: .1875rem;
}
.chat-win__msg--mine .chat-win__msg-bubble {
  background: #6366f1; color: #fff; border-top-right-radius: .1875rem;
}
.chat-win__msg-time {
  font-size: .55rem; color: var(--text-muted); margin-top: .125rem;
}
.chat-win__msg--mine .chat-win__msg-time { text-align: right; color: rgba(255,255,255,.5); }
.chat-win__msg-attachment { margin-top: .25rem; }
.chat-win__msg-attachment img {
  max-width: 180px; max-height: 120px; border-radius: .375rem; display: block;
}

/* Date separator */
.chat-win__date {
  text-align: center; padding: .5rem 0 .25rem;
  font-size: .575rem; font-weight: 600; color: var(--text-muted);
  text-transform: uppercase; letter-spacing: .04em;
}

/* Empty state */
.chat-win__empty {
  flex: 1; display: flex; align-items: center; justify-content: center;
  color: var(--text-muted); font-size: .8rem;
}

/* Compose bar */
.chat-win__compose {
  display: flex; align-items: flex-end; gap: .375rem;
  padding: .5rem .625rem; border-top: 1px solid var(--border);
  flex-shrink: 0;
}
.chat-win__input {
  flex: 1; resize: none; border: 1px solid var(--border);
  border-radius: .5rem; padding: .375rem .5rem;
  font-size: .8rem; font-family: inherit; color: var(--text-primary);
  background: var(--bg-surface, #fff); outline: none;
  max-height: 4.5rem; min-height: 1.875rem; line-height: 1.35;
}
.chat-win__input:focus { border-color: #6366f1; box-shadow: 0 0 0 2px rgba(99,102,241,.12); }
.chat-win__send {
  width: 1.875rem; height: 1.875rem; border-radius: .375rem;
  border: none; background: #6366f1; cursor: pointer;
  display: flex; align-items: center; justify-content: center;
  color: #fff; transition: background .12s; flex-shrink: 0;
}
.chat-win__send:hover { background: #4f46e5; }
.chat-win__send svg { width: .875rem; height: .875rem; }

/* File preview */
.chat-win__file-preview {
  display: flex; align-items: center; gap: .375rem;
  padding: .25rem .625rem; background: var(--bg-hover, #f4f4f5);
  border-radius: .375rem; font-size: .7rem; margin: 0 .625rem;
}
.chat-win__file-preview span { flex: 1; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.chat-win__file-remove {
  border: none; background: none; cursor: pointer; color: var(--text-muted);
  font-size: .875rem; padding: 0; line-height: 1;
}

/* ── Dark theme ── */
[data-theme="dark"] .chat-dropdown { background: #18181b; box-shadow: 0 8px 24px rgba(0,0,0,.4); }
[data-theme="dark"] .chat-dropdown__header { background: #18181b; }
[data-theme="dark"] .chat-dropdown__item:hover { background: #27272a; }
[data-theme="dark"] .chat-win { background: #09090b; border-color: #27272a; box-shadow: 0 -4px 24px rgba(0,0,0,.3); }
[data-theme="dark"] .chat-win__header { background: #09090b; }
[data-theme="dark"] .chat-win__msg--theirs .chat-win__msg-bubble { background: #27272a; }

/* ── Responsive ── */
@media (max-width: 920px) {
  .chat-dropdown { display: none; }
  .chat-dropdown--open { display: block; }
}

@media (max-width: 640px) {
  #chatWindows { padding: 0; gap: 0; }
  .chat-win { width: 100%; height: 60vh; border-radius: .75rem .75rem 0 0; }

  /* Chat full-page overlay on mobile */
  .chat-dropdown__back { display: none; }
  .topbar__chat--expanded {
    position: fixed; top: 0; left: 0; right: 0; bottom: 0;
    z-index: 200; background: var(--bg-surface, #fff);
    display: flex; flex-direction: column;
  }
  .topbar__chat--expanded .topbar__chat-btn { display: none; }
  .topbar__chat--expanded .chat-dropdown {
    display: flex; flex-direction: column; flex: 1;
    width: 100%; max-height: none; height: auto; border: none; border-radius: 0;
    box-shadow: none; opacity: 1; visibility: visible; transform: none;
    position: static; overflow: visible;
  }
  .topbar__chat--expanded .chat-dropdown__header {
    border-radius: 0; flex-shrink: 0;
  }
  .topbar__chat--expanded .chat-dropdown__back {
    display: flex; align-items: center; justify-content: center;
    width: 40px; height: 40px; padding: 0; border: none; background: none;
    color: var(--text-primary); cursor: pointer; flex-shrink: 0;
  }
  .topbar__chat--expanded .chat-dropdown__back svg { width: 20px; height: 20px; }
  .topbar__chat--expanded .chat-dropdown__list { flex: 1; overflow-y: auto; -webkit-overflow-scrolling: touch; }
  .topbar__chat--expanded .chat-dropdown__item { padding: 12px 16px; gap: 12px; min-height: 48px; }

  [data-theme="dark"] .topbar__chat--expanded { background: #09090b; }
  [data-theme="dark"] .topbar__chat--expanded .chat-dropdown { background: #09090b; }
  [data-theme="dark"] .topbar__chat--expanded .chat-dropdown__header { background: #09090b; }
}
