/* SAMA CRM — chat widget. Mirrors the notification panel's look (layout.css)
   so the header stays visually consistent. Uses the shared theme variables. */

/* ── Header button + badge (twin of .notif-btn) ── */
.chat-btn {
  position: relative; width: 42px; height: 42px;
  border: none; background: none; border-radius: 50%;
  display: flex; align-items: center; justify-content: center;
  cursor: pointer; transition: background var(--transition); flex-shrink: 0;
}
.chat-btn:hover { background: rgba(224,123,32,.1); }
.chat-btn-icon { width: 38px; height: 38px; display: flex; align-items: center; justify-content: center; }
.chat-btn-icon svg { width: 22px; height: 22px; transition: transform .2s cubic-bezier(.34,1.56,.64,1); }
.chat-btn:hover .chat-btn-icon svg { transform: scale(1.08); }
.chat-badge {
  position: absolute; top: 1px; right: 1px;
  min-width: 17px; height: 17px; padding: 0 4px;
  background: linear-gradient(135deg, #e03535, #c0392b);
  color: #fff; font-size: .58rem; font-weight: 700;
  border-radius: 9px; display: flex; align-items: center; justify-content: center;
  border: 2px solid var(--clr-surface); line-height: 1;
  pointer-events: none; letter-spacing: .02em;
  box-shadow: 0 2px 6px rgba(192,57,43,.4);
}
.chat-badge.hidden { display: none; }

/* ── Panel shell ── */
.chat-panel-backdrop { position: fixed; inset: 0; z-index: 9990; display: none; }
.chat-panel-backdrop.open { display: block; }
.chat-panel {
  position: fixed; bottom: 20px; right: 20px;
  width: 420px; max-width: calc(100vw - 24px);
  height: calc(100vh - var(--header-h) - 30px); max-height: 640px;
  background: var(--clr-surface); border: 1px solid var(--clr-border);
  border-radius: 14px; box-shadow: 0 20px 60px rgba(0,0,0,.18), 0 4px 16px rgba(0,0,0,.08);
  z-index: 9991; display: flex; flex-direction: column; overflow: hidden;
  opacity: 0; transform: translateY(20px) scale(.96); transform-origin: bottom right;
  transition: opacity .22s cubic-bezier(.4,0,.2,1), transform .22s cubic-bezier(.4,0,.2,1);
  pointer-events: none;
}
.chat-panel.open { opacity: 1; transform: translateY(0) scale(1); pointer-events: all; }

/* View switching: one view visible at a time */
.chat-list-view, .chat-users-view, .chat-thread-view {
  flex: 1; min-height: 0; flex-direction: column; display: none;
}
.chat-panel[data-view="list"]   .chat-list-view,
.chat-panel[data-view="users"]  .chat-users-view,
.chat-panel[data-view="thread"] .chat-thread-view { display: flex; }

/* ── Heads ── */
.chat-panel-head {
  padding: 16px 18px 14px; border-bottom: 1px solid var(--clr-border);
  background: linear-gradient(135deg, #faf9f7, #f5f3f0);
  display: flex; align-items: center; justify-content: space-between;
  flex-shrink: 0; gap: 12px;
}
.chat-head-left { display: flex; align-items: center; gap: 12px; min-width: 0; }
.chat-head-icon {
  width: 40px; height: 40px; border-radius: 11px; flex-shrink: 0;
  background: linear-gradient(135deg, rgba(224,123,32,.15), rgba(224,123,32,.08));
  border: 1.5px solid rgba(224,123,32,.22);
  display: flex; align-items: center; justify-content: center;
}
.chat-head-icon svg { width: 20px; height: 20px; }
.chat-panel-title {
  font-family: 'Times New Roman', Times, serif;
  font-size: 1.05rem; font-weight: 700; color: var(--clr-text); line-height: 1.1;
  white-space: nowrap; overflow: hidden; text-overflow: ellipsis;
}
.chat-panel-sub { font-size: .72rem; color: var(--clr-text-sub); margin-top: 3px; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.chat-panel-actions { display: flex; align-items: center; gap: 4px; flex-shrink: 0; }
.chat-new-btn, .chat-back-btn, .chat-panel-close {
  border: none; background: none; cursor: pointer; border-radius: 50%;
  display: flex; align-items: center; justify-content: center;
  transition: background var(--transition); color: var(--clr-gray); flex-shrink: 0;
}
.chat-new-btn { width: 32px; height: 32px; color: var(--clr-orange); border: 1px solid rgba(224,123,32,.25); }
.chat-new-btn:hover { background: rgba(224,123,32,.08); border-color: rgba(224,123,32,.5); }
.chat-new-btn svg { width: 16px; height: 16px; }
.chat-back-btn { width: 32px; height: 32px; }
.chat-back-btn:hover { background: rgba(0,0,0,.06); color: var(--clr-text); }
.chat-back-btn svg { width: 18px; height: 18px; fill: none; stroke: currentColor; stroke-width: 2; stroke-linecap: round; stroke-linejoin: round; }
.chat-panel-close { width: 30px; height: 30px; }
.chat-panel-close:hover { background: rgba(0,0,0,.07); color: var(--clr-text); }
.chat-panel-close svg { width: 13px; height: 13px; stroke: currentColor; fill: none; stroke-width: 2.2; stroke-linecap: round; }
.chat-thread-head { gap: 10px; }
.chat-thread-peer { display: flex; align-items: center; gap: 10px; min-width: 0; flex: 1; }
.chat-thread-title { min-width: 0; }

/* ── Search ── */
.chat-search-bar { position: relative; padding: 10px 14px; border-bottom: 1px solid var(--clr-border); flex-shrink: 0; }
.chat-search-bar input {
  width: 100%; border: 1px solid var(--clr-border); border-radius: 20px;
  padding: 8px 12px 8px 34px; font-family: 'Times New Roman', Times, serif;
  font-size: .82rem; color: var(--clr-text); background: var(--clr-bg);
  outline: none; transition: border-color var(--transition), background var(--transition);
}
.chat-search-bar input:focus { border-color: var(--clr-orange); background: #fff; }
.chat-search-icon { position: absolute; left: 24px; top: 50%; transform: translateY(-50%); width: 15px; height: 15px; fill: none; stroke-width: 2; opacity: .5; pointer-events: none; }

/* ── Avatars ── */
.chat-avatar {
  width: 40px; height: 40px; border-radius: 50%; flex-shrink: 0;
  background: linear-gradient(145deg, var(--clr-orange-dk), var(--clr-orange));
  display: flex; align-items: center; justify-content: center;
  font-size: .82rem; font-weight: 600; letter-spacing: .04em; color: #fff;
  box-shadow: 0 2px 6px rgba(224,123,32,.3);
}
.chat-thread-head .chat-avatar { width: 38px; height: 38px; font-size: .78rem; }

/* ── List / users body ── */
.chat-body { overflow-y: auto; flex: 1; padding: 6px; scrollbar-width: thin; scrollbar-color: var(--clr-border) transparent; }
.chat-row {
  display: flex; align-items: center; gap: 12px; padding: 10px 12px;
  border-radius: 10px; cursor: pointer; transition: background .13s; position: relative;
}
.chat-row:hover { background: #f7f3ee; }
.chat-row-main { flex: 1; min-width: 0; }
.chat-row-top { display: flex; align-items: baseline; justify-content: space-between; gap: 8px; }
.chat-row-name { font-weight: 600; color: var(--clr-text); font-size: .9rem; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.chat-row-time { font-size: .66rem; color: var(--clr-text-sub); flex-shrink: 0; }
.chat-row-preview { font-size: .78rem; color: var(--clr-text-sub); white-space: nowrap; overflow: hidden; text-overflow: ellipsis; margin-top: 2px; }
.chat-row.unread .chat-row-preview { color: var(--clr-text); font-weight: 600; }
.chat-row-badge {
  min-width: 19px; height: 19px; padding: 0 5px; flex-shrink: 0;
  background: linear-gradient(135deg, var(--clr-orange-lt), var(--clr-orange));
  color: #fff; font-size: .64rem; font-weight: 700; border-radius: 10px;
  display: flex; align-items: center; justify-content: center;
}

/* ── Thread / messages ── */
.chat-messages {
  flex: 1; overflow-y: auto; padding: 16px 14px; display: flex; flex-direction: column; gap: 4px;
  background: var(--clr-bg); scrollbar-width: thin; scrollbar-color: var(--clr-border) transparent;
}
.chat-msg { max-width: 78%; display: flex; flex-direction: column; }
.chat-msg-bubble {
  padding: 8px 12px; border-radius: 14px; font-size: .85rem; line-height: 1.4;
  color: var(--clr-text); word-wrap: break-word; white-space: pre-wrap;
}
.chat-msg-meta { font-size: .6rem; color: var(--clr-text-sub); margin-top: 3px; display: flex; align-items: center; gap: 4px; }
.chat-msg.them { align-self: flex-start; }
.chat-msg.them .chat-msg-bubble { background: #fff; border: 1px solid var(--clr-border); border-bottom-left-radius: 4px; }
.chat-msg.me { align-self: flex-end; align-items: flex-end; }
.chat-msg.me .chat-msg-bubble { background: linear-gradient(135deg, var(--clr-orange), var(--clr-orange-dk)); color: #fff; border-bottom-right-radius: 4px; }
.chat-msg.me .chat-msg-meta { justify-content: flex-end; }
.chat-msg-tick { font-weight: 700; letter-spacing: -1px; }
.chat-msg-tick.read { color: var(--clr-orange); }
.chat-day-sep { align-self: center; font-size: .62rem; color: var(--clr-text-sub); text-transform: uppercase; letter-spacing: .12em; margin: 8px 0 4px; }

/* ── Attachments in bubbles ── */
.chat-att-img { display: block; margin-bottom: 4px; border-radius: 12px; overflow: hidden; max-width: 240px; line-height: 0; }
.chat-att-img img { width: 100%; max-height: 280px; object-fit: cover; display: block; cursor: zoom-in; transition: opacity .15s; }
.chat-att-img:hover img { opacity: .92; }
.chat-att-file {
  display: flex; align-items: center; gap: 10px; margin-bottom: 4px;
  padding: 9px 12px; border-radius: 12px; text-decoration: none;
  background: rgba(0,0,0,.04); border: 1px solid var(--clr-border); max-width: 260px;
}
.chat-msg.me .chat-att-file { background: rgba(0, 0, 0, 0.5); border-color: rgba(255,255,255,.3); }
.chat-att-icon svg { width: 22px; height: 22px; color: var(--clr-orange); flex-shrink: 0; }
.chat-msg.me .chat-att-icon svg { color: #fff; }
.chat-att-meta { display: flex; flex-direction: column; min-width: 0; }
.chat-att-name { font-size: .82rem; font-weight: 600; color: var(--clr-text); white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.chat-msg.me .chat-att-name { color: #fff; }
.chat-att-size { font-size: .66rem; color: var(--clr-text-sub); }
.chat-msg.me .chat-att-size { color: rgba(255,255,255,.8); }

/* ── Staging tray ── */
.chat-attach-tray { display: flex; gap: 8px; flex-wrap: wrap; padding: 10px 12px 0; flex-shrink: 0; }
.chat-attach-tray.hidden { display: none; }
.chat-tray-item { position: relative; width: 60px; }
.chat-tray-item img, .chat-tray-doc {
  width: 60px; height: 60px; border-radius: 10px; object-fit: cover; display: flex;
  align-items: center; justify-content: center; background: var(--clr-bg); border: 1px solid var(--clr-border);
}
.chat-tray-doc svg { width: 26px; height: 26px; color: var(--clr-orange); }
.chat-tray-rm {
  position: absolute; top: -6px; right: -6px; width: 18px; height: 18px; border-radius: 50%;
  border: none; background: #c0392b; color: #fff; font-size: 13px; line-height: 1; cursor: pointer;
  display: flex; align-items: center; justify-content: center;
}
.chat-tray-name { display: block; font-size: .6rem; color: var(--clr-text-sub); white-space: nowrap; overflow: hidden; text-overflow: ellipsis; margin-top: 2px; }

/* ── Composer ── */
.chat-composer { display: flex; align-items: flex-end; gap: 8px; padding: 10px 12px; border-top: 1px solid var(--clr-border); background: var(--clr-surface); flex-shrink: 0; }
.chat-attach-btn {
  width: 40px; height: 40px; border-radius: 50%; border: none; cursor: pointer; flex-shrink: 0;
  background: none; color: var(--clr-gray-lt); display: flex; align-items: center; justify-content: center;
  transition: background var(--transition), color var(--transition);
}
.chat-attach-btn:hover { background: rgba(224,123,32,.1); color: var(--clr-orange); }
.chat-attach-btn svg { width: 20px; height: 20px; }
.chat-composer textarea {
  flex: 1; resize: none; border: 1px solid var(--clr-border); border-radius: 18px;
  padding: 9px 14px; font-family: 'Times New Roman', Times, serif; font-size: .85rem;
  color: var(--clr-text); background: var(--clr-bg); outline: none; max-height: 120px;
  line-height: 1.4; transition: border-color var(--transition), background var(--transition);
}
.chat-composer textarea:focus { border-color: var(--clr-orange); background: #fff; }
.chat-send-btn {
  width: 40px; height: 40px; border-radius: 50%; border: none; cursor: pointer; flex-shrink: 0;
  background: linear-gradient(145deg, var(--clr-orange-dk), var(--clr-orange));
  color: #fff; display: flex; align-items: center; justify-content: center;
  box-shadow: 0 2px 8px rgba(224,123,32,.35); transition: transform .15s, opacity .15s;
}
.chat-send-btn:hover { transform: scale(1.06); }
.chat-send-btn:disabled { opacity: .45; cursor: default; transform: none; }
.chat-send-btn svg { width: 18px; height: 18px; }

/* ── Empty / loading ── */
.chat-empty { display: flex; flex-direction: column; align-items: center; justify-content: center; text-align: center; padding: 48px 24px; color: var(--clr-text-sub); height: 100%; }
.chat-empty svg { width: 46px; height: 46px; stroke: var(--clr-border); fill: none; stroke-width: 1.4; margin-bottom: 12px; }
.chat-empty h4 { font-family: 'Times New Roman', Times, serif; color: var(--clr-text); font-size: 1rem; margin-bottom: 4px; }
.chat-empty p { font-size: .8rem; }

/* ── Responsive: full-screen sheet on phones ── */
@media (max-width: 560px) {
  .chat-panel {
    top: 0; right: 0; left: 0; bottom: 0; width: 100%; max-width: 100%;
    height: 100%; max-height: 100%; border-radius: 0; border: none;
  }
}

/* ── Chat Lightbox ── */
.chat-lightbox {
  position: fixed;
  inset: 0;
  background: rgba(0, 0, 0, 0.85);
  z-index: 99999;
  display: none;
  align-items: center;
  justify-content: center;
  opacity: 0;
  transition: opacity .25s ease;
}
.chat-lightbox.open {
  display: flex;
  opacity: 1;
}
.chat-lightbox-content {
  max-width: 90vw;
  max-height: 90vh;
  object-fit: contain;
  border-radius: 8px;
  box-shadow: 0 10px 40px rgba(0,0,0,.5);
}
.chat-lightbox-close {
  position: absolute;
  top: 20px;
  right: 25px;
  color: #f1f1f1;
  font-size: 40px;
  font-weight: bold;
  cursor: pointer;
  transition: color 0.15s;
  user-select: none;
}
.chat-lightbox-close:hover {
  color: #bbb;
}
