/* WP 22: team chat, presence, contact book (Office app + field app). Tokens only; phone layout below 1024 px:
   list and conversation on separate screens, the conversation fills the visible viewport (keyboard-safe via --chat-vh). */

/* ---------------------------------------------------------------- presence */
.presence-dot { position: absolute; right: -1px; bottom: -1px; width: 11px; height: 11px; border-radius: 50%; border: 2px solid var(--color-surface);
  background: var(--color-border-strong); }
.presence-dot.p-online { background: var(--color-success); }
.presence-dot.p-away { background: var(--color-warning); }
.presence-sum { display: inline-flex; align-items: center; gap: 4px; margin-right: var(--space-4); white-space: nowrap; }
.presence-sum .presence-dot { position: static; border: 0; width: 8px; height: 8px; }
.chat-ava { position: relative; display: inline-flex; flex: none; }
.chat-ava .avatar { width: 40px; height: 40px; }
.chat-ava.big .avatar { width: 64px; height: 64px; font-size: 1.3rem; }
.chat-ava.big .presence-dot { width: 15px; height: 15px; }
.chat-ava-ic { width: 40px; height: 40px; border-radius: 50%; place-items: center; display: inline-grid; background: var(--color-primary-subtle); color: var(--color-primary-text); }
.chat-ava-ic.kind-network { background: var(--color-info-subtle); color: var(--color-info-text); }
.chat-ava-ic.kind-franchise { background: var(--color-warning-subtle); color: var(--color-warning-text); }

/* ---------------------------------------------------------------- layout */
.chat-layout { display: grid; grid-template-columns: minmax(260px, 340px) 1fr; gap: 0; height: calc(100dvh - var(--d-top-h) - var(--banner-h) - 190px); min-height: 460px;
  border: 1px solid var(--color-border); border-radius: var(--radius-lg); background: var(--color-surface); overflow: hidden; }
.chat-side { display: flex; flex-direction: column; min-height: 0; border-right: 1px solid var(--color-border); background: var(--color-surface-secondary); }
.chat-side-body { display: flex; flex-direction: column; min-height: 0; flex: 1; }
.chat-seg { padding: var(--space-3) var(--space-3) 0; }
.chat-seg .seg-ctl { width: 100%; display: grid; grid-template-columns: 1fr 1fr; }
.chat-side-tools { display: flex; gap: var(--space-2); align-items: center; padding: var(--space-3); }
.chat-side-tools .search-input, .contacts-tools .search-input { position: relative; display: flex; align-items: center; min-width: 0; }
.chat-side-tools .search-input > .ic, .contacts-tools .search-input > .ic { position: absolute; left: 10px; color: var(--color-text-muted); pointer-events: none; }
.chat-side-tools .search-input .input, .contacts-tools .search-input .input { padding-left: 32px; width: 100%; }
.chat-list { flex: 1; overflow-y: auto; min-height: 0; padding: 0 var(--space-2) var(--space-2); }
.chat-msg-search { margin: 0 var(--space-3) var(--space-3); display: inline-flex; gap: 6px; align-items: center; align-self: flex-start; }
.chat-item { display: flex; gap: var(--space-3); align-items: center; width: 100%; text-align: left; padding: var(--space-2) var(--space-2); border: 0; background: none;
  border-radius: var(--radius-md); cursor: pointer; color: inherit; min-height: 56px; }
.chat-item:hover { background: var(--color-surface); }
.chat-item.active { background: var(--color-primary-subtle); }
.chat-item-text { display: flex; flex-direction: column; min-width: 0; flex: 1; gap: 2px; }
.chat-item-top, .chat-item-bottom { display: flex; gap: var(--space-2); align-items: center; justify-content: space-between; min-width: 0; }
.chat-item-title, .chat-item-preview { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; min-width: 0; }
.chat-item.unread .chat-item-title { font-weight: 800; }
.chat-item .count, .chat-list .count { background: var(--color-primary); color: var(--color-text-on-primary); border-radius: var(--radius-full); padding: 0 7px;
  font-size: var(--font-size-xs); font-weight: 700; min-width: 20px; text-align: center; }

.chat-main { display: flex; flex-direction: column; min-height: 0; min-width: 0; background: var(--color-surface); }
.chat-empty { margin: auto; padding: var(--space-6); }
.chat-head { display: flex; align-items: center; gap: var(--space-3); padding: var(--space-3) var(--space-4); border-bottom: 1px solid var(--color-border); }
.chat-head-text { flex: 1; min-width: 0; }
.chat-head-text h2 { margin: 0; font-size: var(--font-size-lg); overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.chat-back { display: none; }
.chat-scroll-wrap { position: relative; flex: 1; min-height: 0; display: flex; }
.chat-scroll { flex: 1; overflow-y: auto; padding: var(--space-3) var(--space-4); overscroll-behavior: contain; }
.chat-more { text-align: center; min-height: 8px; }
.chat-msgs { display: flex; flex-direction: column; gap: 4px; }
.chat-first { text-align: center; padding: var(--space-4); }
.chat-day { text-align: center; margin: var(--space-3) 0 var(--space-2); }
.chat-day span { background: var(--color-surface-secondary); color: var(--color-text-secondary); border-radius: var(--radius-full); padding: 2px 12px; font-size: var(--font-size-xs); }
.chat-sys { text-align: center; color: var(--color-text-muted); font-size: var(--font-size-xs); margin: var(--space-2) 0; }
.chat-row { display: flex; align-items: flex-end; gap: 4px; max-width: 100%; }
.chat-row.own { justify-content: flex-end; }
.chat-row.flash .chat-bubble { box-shadow: 0 0 0 2px var(--color-focus-ring); }
.chat-bubble { max-width: min(560px, 78%); background: var(--color-surface-secondary); border-radius: 16px 16px 16px 4px; padding: 8px 12px; overflow-wrap: anywhere; }
.chat-bubble.own { background: var(--color-primary-subtle); border-radius: 16px 16px 4px 16px; }
.chat-bubble.deleted { background: transparent; border: 1px dashed var(--color-border); }
.chat-author { font-size: var(--font-size-xs); font-weight: 700; color: var(--color-primary-text); margin-bottom: 2px; }
.chat-text { white-space: pre-wrap; line-height: 1.45; }
.chat-text a { color: var(--color-primary-text); text-decoration: underline; }
.chat-mention { font-weight: 700; color: var(--color-primary-text); }
.chat-meta { display: flex; gap: 6px; justify-content: flex-end; align-items: center; font-size: var(--font-size-xs); color: var(--color-text-muted); margin-top: 2px; }
.chat-tick { display: inline-flex; color: var(--color-text-muted); }
.chat-tick.read { color: var(--color-info); }
.chat-quote { display: flex; flex-direction: column; text-align: left; width: 100%; border: 0; border-radius: var(--radius-sm); background: var(--color-surface); padding: 4px 8px;
  margin-bottom: 4px; font-size: var(--font-size-sm); color: inherit; cursor: pointer; box-shadow: inset 2px 0 0 var(--color-border-strong); }
.chat-quote span { color: var(--color-text-secondary); overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.chat-act { opacity: 0; transition: opacity .15s; }
.chat-row:hover .chat-act, .chat-act:focus-visible, .chat-act[aria-expanded="true"] { opacity: 1; }
.chat-img img { display: block; max-width: 260px; max-height: 260px; border-radius: var(--radius-md); margin-bottom: 4px; }
.chat-file { display: flex; gap: var(--space-2); align-items: center; padding: 6px 8px; border-radius: var(--radius-md); background: var(--color-surface); color: inherit; text-decoration: none; margin-bottom: 4px; }
.chat-file > span { display: flex; flex-direction: column; min-width: 0; }
.chat-newpill { position: absolute; bottom: var(--space-3); left: 50%; transform: translateX(-50%); border: 0; border-radius: var(--radius-full); padding: 6px 14px;
  background: var(--color-primary); color: var(--color-text-on-primary); display: inline-flex; gap: 6px; align-items: center; box-shadow: var(--shadow-2); cursor: pointer; }

/* ---------------------------------------------------------------- composer */
.chat-composer { border-top: 1px solid var(--color-border); padding: var(--space-2) var(--space-3) var(--space-3); position: relative; background: var(--color-surface); }
.chat-privacy-hint { display: flex; gap: 6px; align-items: center; padding: 0 var(--space-1) var(--space-2); }
.chat-compose-row { display: flex; gap: var(--space-2); align-items: flex-end; }
.chat-input { flex: 1; resize: none; min-height: 44px; height: 44px; max-height: 160px; line-height: 1.4; padding-top: 10px; padding-bottom: 10px; }
.chat-send, .chat-attach { width: 44px; height: 44px; flex: none; }
.chat-replybar { display: flex; gap: var(--space-2); align-items: center; padding: 6px 8px; margin-bottom: var(--space-2); border-radius: var(--radius-md); background: var(--color-surface-secondary); }
.chat-replybar[hidden] { display: none; }
.chat-suggest { position: absolute; bottom: calc(100% - 4px); left: var(--space-3); min-width: 240px; max-height: 260px; overflow-y: auto; z-index: 5; }
.chat-suggest[hidden] { display: none; }
.chat-suggest button { display: flex; gap: var(--space-2); align-items: center; width: 100%; }
.chat-suggest .avatar { width: 24px; height: 24px; }
.chat-count { display: block; text-align: right; }

/* ---------------------------------------------------------------- drawers: members, pickers, search hits */
.chat-members li, .chat-pick li, .contacts-people li { display: flex; gap: var(--space-3); align-items: center; padding: var(--space-2) 0; border-bottom: 1px solid var(--color-divider); }
.chat-pick-item, .contact-main { display: flex; gap: var(--space-3); align-items: center; flex: 1; min-width: 0; border: 0; background: none; text-align: left; padding: 4px 0;
  color: inherit; cursor: pointer; min-height: 44px; }
.chat-pick-item > .grow, .contact-text { display: flex; flex-direction: column; min-width: 0; }
.contact-text > *, .chat-pick-item .grow > * { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.chat-hit { display: flex; flex-direction: column; gap: 2px; width: 100%; text-align: left; border: 0; background: none; padding: var(--space-2); border-radius: var(--radius-md); color: inherit; cursor: pointer; }
.chat-hit:hover { background: var(--color-surface-secondary); }

/* ---------------------------------------------------------------- contact book */
.contacts-tools { display: flex; gap: var(--space-2); align-items: center; flex-wrap: wrap; margin-bottom: var(--space-3); }
.contacts-tools .search-input { flex: 1 1 240px; }
.contacts-tools select.input { width: auto; max-width: 220px; }
.contacts-tools.compact { padding: var(--space-3) var(--space-3) 0; }
.chip-toggle { display: inline-flex; gap: 6px; align-items: center; border: 1px solid var(--color-border); background: var(--color-surface); color: inherit; border-radius: var(--radius-full);
  padding: 6px 12px; cursor: pointer; min-height: 36px; }
.chip-toggle[aria-pressed="true"] { border-color: var(--color-primary); background: var(--color-primary-subtle); color: var(--color-primary-text); }
.contacts-group { margin-bottom: var(--space-4); }
.contacts-group-head { display: flex; gap: var(--space-2); align-items: baseline; flex-wrap: wrap; padding: var(--space-2) 0; border-bottom: 1px solid var(--color-border); }
.contacts-group-head > :last-child { margin-left: auto; }
.contacts-support { display: flex; gap: 6px; align-items: center; margin-bottom: var(--space-2); }
.star-btn.starred, .btn.starred { color: var(--color-warning); }
.star-btn.starred svg, .btn.starred svg { fill: currentColor; }
.chat-contacts { flex: 1; overflow-y: auto; min-height: 0; }
.chat-contacts .contacts-list { padding: 0 var(--space-3) var(--space-3); }
.contact-head { display: flex; gap: var(--space-3); align-items: center; }
.contact-head h2 { margin: 0; }
.contact-rows li { display: flex; gap: var(--space-3); align-items: center; padding: var(--space-2) 0; }
.policy-row { display: flex; gap: var(--space-3); align-items: center; flex-wrap: wrap; padding: var(--space-2) 0; border-bottom: 1px solid var(--color-divider); }

/* ---------------------------------------------------------------- phones / tablets */
@media (max-width: 1023px) {
  .chat-page .page-head { margin-bottom: var(--space-2); }
  .chat-layout { display: block; height: auto; min-height: 0; border: 0; border-radius: 0; background: none; overflow: visible; }
  .chat-side { border: 0; background: none; }
  .chat-side-tools { padding: 0 0 var(--space-2); }
  .chat-seg { padding: 0 0 var(--space-3); }
  .chat-list { padding: 0; overflow: visible; }
  .chat-item { min-height: 64px; padding: var(--space-2) 0; border-radius: 0; border-bottom: 1px solid var(--color-divider); }
  .chat-msg-search { margin: var(--space-3) 0; }
  .contacts-tools.compact { padding: 0 0 var(--space-3); }
  .chat-contacts .contacts-list { padding: 0; }
  .chat-main { display: none; }
  /* open conversation: full screen between the top bar and the bottom of the visible viewport (keyboard included) */
  .chat-layout.show-main .chat-main { display: flex; position: fixed; left: 0; right: 0; z-index: 46; top: calc(var(--d-top-h) + var(--banner-h));
    height: calc(var(--chat-vh, 100dvh) - var(--d-top-h) - var(--banner-h)); border-top: 1px solid var(--color-border); }
  body.chat-focus .btabs { display: none !important; }
  .chat-back { display: inline-flex; }
  .chat-head { padding: var(--space-2) var(--space-3); }
  .chat-scroll { padding: var(--space-2) var(--space-3); }
  .chat-bubble { max-width: 86%; }
  .chat-act { opacity: 1; }
  .chat-composer { padding-bottom: calc(var(--space-2) + env(safe-area-inset-bottom)); }
  .chat-input { font-size: 16px; }                         /* no zoom on focus (iOS) */
  .chat-privacy-hint { font-size: var(--font-size-xs); }
  .chat-img img { max-width: 220px; }
  .contacts-tools select.input { flex: 1 1 140px; max-width: none; }
}

/* ---------------------------------------------------------------- profile → chat & visibility */
[data-testid="chat-privacy"] .field { margin-bottom: var(--space-4); }
[data-testid="chat-privacy"] .field .hint { margin-top: 4px; }
