/* ============================================================================
   TAKUMI HUB — Chat module (Bitrix24-style messenger)
   Used by views/chat-module.js inside #view-chat, plus the right-edge dock.
   ============================================================================ */

/* ── Layout ── */
/* grid-template-rows:minmax(0,1fr) is load-bearing: without it the single
   implicit row sizes to CONTENT, so long conversations grew past the fixed
   container, overflow:hidden clipped the composer away, and the inner panes
   never scrolled (the page scrolled instead). */
.chat-wrap{display:grid;grid-template-columns:320px 1fr;grid-template-rows:minmax(0,1fr);gap:0;height:calc(100vh - 160px);min-height:480px;border:1.5px solid var(--border);border-radius:10px;overflow:hidden;background:#fff}
.chat-side{border-right:1.5px solid var(--border);display:flex;flex-direction:column;min-width:0;min-height:0;background:#fff}
.chat-main{display:flex;flex-direction:column;min-width:0;min-height:0;background:var(--paper)}

/* ── Sidebar ── */
.chat-side-hdr{padding:12px 12px 8px;display:flex;align-items:center;gap:8px}
.chat-tabs{display:flex;gap:4px;flex:1}
.chat-tab{border:none;background:transparent;padding:6px 10px;border-radius:6px;font-family:inherit;font-size:12.5px;font-weight:600;color:var(--muted);cursor:pointer}
.chat-tab.active{background:var(--green);color:#fff}
.chat-tab .nav-count{margin-left:4px}
.chat-new-btn{width:30px;height:30px;border-radius:8px;border:1.5px solid var(--border);background:#fff;cursor:pointer;font-size:17px;line-height:1;color:var(--green);font-weight:600}
.chat-new-btn:hover{background:var(--cream)}
.chat-search{margin:0 12px 8px;padding:8px 11px;border:1.5px solid var(--border);border-radius:7px;font-family:inherit;font-size:13px;outline:none;background:#fff}
.chat-list{flex:1;min-height:0;overflow-y:auto;overscroll-behavior:contain;padding:4px 6px 10px}
.chat-list-section{font-size:10.5px;letter-spacing:.12em;text-transform:uppercase;color:var(--muted);padding:10px 10px 4px;font-weight:600}

.chat-item{display:flex;align-items:center;gap:10px;width:100%;border:none;background:transparent;padding:8px 10px;border-radius:8px;cursor:pointer;text-align:left;font-family:inherit}
.chat-item:hover{background:var(--cream)}
.chat-item.active{background:var(--green);color:#fff}
.chat-item.active .chat-item-sub,.chat-item.active .chat-item-time{color:rgba(255,255,255,.7)}
.chat-item-body{flex:1;min-width:0}
.chat-item-top{display:flex;align-items:center;gap:6px}
.chat-item-name{font-size:13.5px;font-weight:600;white-space:nowrap;overflow:hidden;text-overflow:ellipsis;flex:1}
.chat-item-time{font-size:10.5px;color:var(--muted);white-space:nowrap}
.chat-item-sub{font-size:12px;color:var(--muted);white-space:nowrap;overflow:hidden;text-overflow:ellipsis;margin-top:2px}
.chat-item-pin{font-size:10px;opacity:.7}

.chat-avatar{width:38px;height:38px;border-radius:50%;flex:none;display:flex;align-items:center;justify-content:center;color:#fff;font-size:13px;font-weight:600;letter-spacing:.02em;overflow:hidden}
.chat-avatar img{width:100%;height:100%;object-fit:cover}
.chat-avatar.sq{border-radius:10px}
.chat-unread{background:var(--red);color:#fff;font-size:10.5px;font-weight:600;border-radius:9px;min-width:18px;height:18px;display:inline-flex;align-items:center;justify-content:center;padding:0 5px;flex:none}
.chat-priv-ico{font-size:11px;opacity:.65}

/* ── Conversation pane ── */
.chat-conv-hdr{display:flex;align-items:center;gap:10px;padding:10px 16px;background:#fff;border-bottom:1.5px solid var(--border)}
.chat-conv-title{font-size:15px;font-weight:600}
.chat-conv-sub{font-size:11.5px;color:var(--muted);margin-top:1px}
.chat-conv-actions{margin-left:auto;display:flex;gap:6px}
.chat-hdr-btn{border:1.5px solid var(--border);background:#fff;border-radius:7px;padding:6px 11px;font-family:inherit;font-size:12px;font-weight:600;cursor:pointer;color:var(--ink)}
.chat-hdr-btn:hover{background:var(--cream)}

.chat-msgs{flex:1;min-height:0;overflow-y:auto;overscroll-behavior:contain;padding:16px 20px;display:flex;flex-direction:column;gap:2px}
.chat-day-sep{align-self:center;background:var(--cream);color:var(--muted);font-size:11px;font-weight:600;border-radius:12px;padding:4px 12px;margin:12px 0 8px}
.chat-sys-msg{align-self:center;color:var(--muted);font-size:11.5px;padding:3px 10px;text-align:center}

.chat-msg{display:flex;gap:9px;max-width:72%;margin-top:8px;position:relative}
.chat-msg.mine{align-self:flex-end;flex-direction:row-reverse}
.chat-msg.cont{margin-top:2px}
.chat-msg .chat-avatar{width:30px;height:30px;font-size:11px;margin-top:2px}
.chat-msg.cont .chat-avatar{visibility:hidden;height:0;margin:0}
.chat-bubble{background:#fff;border:1px solid var(--border);border-radius:12px;border-top-left-radius:4px;padding:8px 12px;min-width:60px}
.chat-msg.mine .chat-bubble{background:#E4F0E4;border-top-left-radius:12px;border-top-right-radius:4px}
.chat-msg-sender{font-size:11.5px;font-weight:600;color:var(--copper);margin-bottom:2px}
.chat-msg-body{font-size:13.5px;line-height:1.45;white-space:pre-wrap;word-break:break-word}
.chat-msg-meta{font-size:10px;color:var(--muted);margin-top:3px;text-align:right}
.chat-msg-deleted{font-style:italic;color:var(--muted)}
.chat-reply-ref{border-left:3px solid var(--copper);background:rgba(160,82,45,.07);border-radius:5px;padding:4px 8px;margin-bottom:5px;font-size:11.5px;color:var(--muted);white-space:nowrap;overflow:hidden;text-overflow:ellipsis;max-width:340px}

.chat-msg-tools{position:absolute;top:-12px;right:8px;display:none;gap:2px;background:#fff;border:1px solid var(--border);border-radius:7px;padding:2px;box-shadow:0 2px 8px rgba(0,0,0,.08);z-index:2}
.chat-msg.mine .chat-msg-tools{right:auto;left:8px}
.chat-msg:hover .chat-msg-tools{display:flex}
.chat-msg-tool{border:none;background:transparent;cursor:pointer;font-size:12px;padding:3px 6px;border-radius:5px;font-family:inherit}
.chat-msg-tool:hover{background:var(--cream)}
.chat-reacts{display:flex;gap:4px;margin-top:4px;flex-wrap:wrap}
.chat-react{border:1px solid var(--border);background:#fff;border-radius:10px;font-size:11px;padding:1px 7px;cursor:pointer}

/* ── Composer ── */
.chat-composer{padding:12px 16px;background:#fff;border-top:1.5px solid var(--border)}
.chat-reply-bar{display:flex;align-items:center;gap:8px;font-size:12px;color:var(--muted);background:var(--cream);border-radius:7px;padding:6px 10px;margin-bottom:8px}
.chat-reply-bar button{margin-left:auto;border:none;background:transparent;cursor:pointer;font-size:14px;color:var(--muted)}
.chat-composer-row{display:flex;gap:9px;align-items:flex-end}
.chat-input{flex:1;border:1.5px solid var(--border);border-radius:9px;padding:10px 13px;font-family:inherit;font-size:13.5px;resize:none;outline:none;max-height:120px;background:#fff}
.chat-input:focus{border-color:var(--green)}
.chat-send-btn{background:var(--green);color:#fff;border:none;border-radius:9px;width:42px;height:42px;cursor:pointer;font-size:16px;flex:none}
.chat-send-btn:disabled{opacity:.5;cursor:default}

/* ── Empty state ── */
.chat-empty{flex:1;display:flex;flex-direction:column;align-items:center;justify-content:center;color:var(--muted);gap:10px;font-size:13.5px}
.chat-empty svg{width:46px;height:46px;opacity:.4}

/* ── Members side panel ── */
.chat-members-panel{position:absolute;top:0;right:0;bottom:0;width:320px;background:#fff;border-left:1.5px solid var(--border);display:flex;flex-direction:column;z-index:5;box-shadow:-6px 0 18px rgba(0,0,0,.06)}
.chat-members-hdr{display:flex;align-items:center;padding:13px 14px;border-bottom:1.5px solid var(--border);font-weight:600;font-size:14px}
.chat-members-hdr button{margin-left:auto;border:none;background:transparent;font-size:18px;cursor:pointer;color:var(--muted)}
.chat-members-list{flex:1;overflow-y:auto;padding:8px}
.chat-member-row{display:flex;align-items:center;gap:10px;padding:7px 8px;border-radius:8px}
.chat-member-row:hover{background:var(--cream)}
.chat-member-name{font-size:13px;font-weight:500;flex:1;min-width:0;white-space:nowrap;overflow:hidden;text-overflow:ellipsis}
.chat-role-chip{font-size:9.5px;letter-spacing:.08em;text-transform:uppercase;font-weight:700;border-radius:5px;padding:2px 6px}
.chat-role-chip.owner{background:#1B3A2D;color:#fff}
.chat-role-chip.supervisor{background:#A0522D;color:#fff}
.chat-member-menu{border:none;background:transparent;cursor:pointer;color:var(--muted);font-size:15px;padding:2px 6px;border-radius:5px}
.chat-member-menu:hover{background:rgba(0,0,0,.06)}

/* ── Staff picker (modals) ── */
.chat-pick-list{max-height:260px;overflow-y:auto;border:1.5px solid var(--border);border-radius:8px;padding:6px;margin-top:6px}
.chat-pick-row{display:flex;align-items:center;gap:9px;padding:6px 8px;border-radius:7px;cursor:pointer}
.chat-pick-row:hover{background:var(--cream)}
.chat-pick-row input{accent-color:var(--green)}
.chat-pick-name{font-size:13px;flex:1}
.chat-pick-dept{font-size:11px;color:var(--muted)}

/* ── Right-edge rail (Bitrix24-style dedicated column) ──
   Full-height slim rail pinned to the right edge. body.has-chat-rail
   reserves its width so page content never flows underneath; drawers and
   modals (z-index 200+/9999) deliberately cover it. */
#chat-dock{position:fixed;top:0;right:0;bottom:0;width:58px;z-index:150;display:none;flex-direction:column;align-items:center;gap:8px;padding:14px 0;background:#fff;border-left:1.5px solid var(--border);box-shadow:-2px 0 12px rgba(0,0,0,.04);overflow-y:auto;overscroll-behavior:contain}
#chat-dock.visible{display:flex}
body.has-chat-rail{padding-right:58px}
.dock-item{position:relative;width:40px;height:40px;border:none;background:transparent;padding:0;cursor:pointer;flex:none}
.dock-item .chat-avatar{width:40px;height:40px;font-size:13px;transition:transform .12s}
.dock-item:hover .chat-avatar{transform:scale(1.1)}
.dock-badge{position:absolute;top:-4px;right:-4px;background:var(--red);color:#fff;font-size:9.5px;font-weight:700;border-radius:8px;min-width:16px;height:16px;display:flex;align-items:center;justify-content:center;padding:0 4px;border:2px solid #fff}
.dock-sep{height:1px;background:var(--border);margin:2px 0;width:32px;flex:none}
.dock-open-btn{width:40px;height:40px;border-radius:50%;border:none;background:var(--green);color:#fff;cursor:pointer;display:flex;align-items:center;justify-content:center;flex:none}
.dock-open-btn svg{width:19px;height:19px}
.dock-item[data-tip]:hover::after{content:attr(data-tip);position:absolute;right:48px;top:50%;transform:translateY(-50%);background:var(--ink);color:#fff;font-size:11px;padding:4px 9px;border-radius:6px;white-space:nowrap;pointer-events:none;z-index:5}

/* ── Channels (WhatsApp) ── */
.chat-chan-bar{display:flex;gap:8px;align-items:center;padding:10px 12px;border-bottom:1.5px solid var(--border);background:#fff;flex-wrap:wrap}
.chat-chan-pill{border:1.5px solid var(--border);border-radius:18px;background:#fff;padding:6px 13px;font-family:inherit;font-size:12.5px;font-weight:600;cursor:pointer;display:flex;align-items:center;gap:6px}
.chat-chan-pill.active{background:var(--green);border-color:var(--green);color:#fff}
.chat-chan-dot{width:7px;height:7px;border-radius:50%;background:#bbb}
.chat-chan-dot.on{background:#2ecc71}

/* ════════════════════════════════════════════════════════════
   v2 — major upgrade
   ════════════════════════════════════════════════════════════ */

/* ── Presence dots ── */
.chat-av-wrap{position:relative;flex:none;display:inline-flex}
.presence-dot{position:absolute;bottom:-1px;right:-1px;width:11px;height:11px;border-radius:50%;background:#b9b3a6;border:2px solid #fff;display:none}
.presence-dot.on{display:block;background:#2ecc71}

/* ── Unread variants ── */
.chat-unread.mention{background:var(--copper);font-weight:800}
.chat-unread.muted{background:#b9b3a6}
.dock-badge.muted{background:#b9b3a6}

/* ── Mentions ── */
.chat-mention{color:var(--copper);font-weight:600}
.chat-mention.me{background:rgba(160,82,45,.14);border-radius:4px;padding:0 3px}
.chat-mention-pop{position:absolute;bottom:100%;left:60px;margin-bottom:6px;background:#fff;border:1.5px solid var(--border);border-radius:9px;box-shadow:0 6px 22px rgba(0,0,0,.12);z-index:30;min-width:220px;overflow:hidden}
.chat-mention-opt{display:flex;align-items:center;gap:9px;padding:7px 11px;cursor:pointer;font-size:13px}
.chat-mention-opt:hover{background:var(--cream)}
.chat-mention-opt .chat-avatar{width:26px;height:26px;font-size:10px}
.chat-composer{position:relative}

/* ── Typing indicator ── */
.chat-typing{min-height:18px;padding:0 20px 2px;font-size:11.5px;color:var(--muted);font-style:italic}

/* ── Read receipts ── */
.chat-ticks{font-size:10px;letter-spacing:-2px;color:var(--muted)}
.chat-ticks.all{color:#2e7d32}

/* ── Composer toolbar ── */
.chat-tool-btn{width:38px;height:42px;border:1.5px solid var(--border);border-radius:9px;background:#fff;cursor:pointer;font-size:16px;flex:none;padding:0}
.chat-tool-btn:hover{background:var(--cream)}
.chat-tool-btn.rec{background:var(--red);border-color:var(--red);animation:chatpulse 1.1s infinite}
@keyframes chatpulse{0%,100%{opacity:1}50%{opacity:.55}}

/* ── Attachments ── */
.chat-att-img{max-width:280px;max-height:220px;border-radius:9px;display:block;margin-bottom:4px}
.chat-att-audio{max-width:260px;height:36px;display:block;margin-bottom:4px}
.chat-att-video{max-width:300px;max-height:240px;border-radius:9px;display:block;margin-bottom:4px}
.chat-att-file{display:inline-flex;align-items:center;gap:7px;background:var(--cream);border-radius:8px;padding:7px 11px;font-size:12.5px;color:var(--ink);text-decoration:none;margin-bottom:4px}
.chat-att-file span{color:var(--muted);font-size:11px}
.chat-att-file:hover{background:#e3ddd1}

/* ── Pinned messages bar ── */
.chat-pin-bar{background:#FFF8E6;border-bottom:1.5px solid var(--border);padding:7px 16px;font-size:12.5px;cursor:pointer;white-space:nowrap;overflow:hidden;text-overflow:ellipsis}
.chat-pin-bar:hover{background:#FBF0D4}
.chat-pin-more{color:var(--copper);font-weight:600;margin-left:8px}
.chat-pin-row{display:flex;align-items:flex-start;gap:10px;padding:9px 6px;border-bottom:1px solid var(--border);font-size:13px}
.chat-pin-row > div{flex:1;min-width:0}
.chat-pin-row:last-child{border-bottom:none}

/* ── Emoji reaction picker (above the overlay too) ── */
.chat-react-pop{position:fixed;z-index:1200;background:#fff;border:1.5px solid var(--border);border-radius:22px;box-shadow:0 8px 26px rgba(0,0,0,.16);padding:5px 9px;display:flex;gap:2px}

/* Chat modals must sit ABOVE the overlay messenger (overlay is z-index 1000;
   the app-wide .modal-overlay base style is only z-index 200). */
#chat-modal{z-index:1200 !important}
.chat-react-pop button{border:none;background:transparent;font-size:19px;cursor:pointer;padding:3px 5px;border-radius:50%}
.chat-react-pop button:hover{background:var(--cream);transform:scale(1.18)}

/* ── WhatsApp "/" template picker ── */
.chat-wa-tpl-pop{position:fixed;z-index:1300;max-height:260px;overflow-y:auto;overscroll-behavior:contain;background:#fff;border:1.5px solid var(--border);border-radius:9px;box-shadow:0 8px 26px rgba(0,0,0,.16)}
.chat-wa-tpl-opt{padding:9px 13px;border-bottom:1px solid var(--border);cursor:pointer}
.chat-wa-tpl-opt:last-child{border-bottom:none}
.chat-wa-tpl-opt:hover{background:var(--cream)}
.chat-wa-tpl-name{font-size:13px;font-weight:600}
.chat-wa-tpl-desc{font-size:11px;color:var(--muted)}
.chat-wa-tpl-body{font-size:11.5px;color:var(--muted);white-space:nowrap;overflow:hidden;text-overflow:ellipsis}

/* ── Announcement lock ── */
.chat-locked-bar{text-align:center;font-size:12.5px;color:var(--muted);background:var(--cream);border-radius:8px;padding:10px}

/* ── v3: Bitrix24-style top bar (light theme) ── */
.chat-topbar{display:flex;align-items:center;gap:5px;flex-wrap:wrap;background:#fff;border:1.5px solid var(--border);border-radius:10px;padding:7px 10px;margin-bottom:10px}
.chat-top-btn{display:inline-flex;align-items:center;gap:6px;border:none;background:transparent;color:var(--muted);padding:7px 12px;border-radius:7px;font-family:inherit;font-size:12.5px;font-weight:600;cursor:pointer;white-space:nowrap}
.chat-top-btn:hover{background:var(--cream);color:var(--ink)}
.chat-top-btn.active{background:var(--green);color:#fff}
.chat-top-btn.icon{padding:7px 9px;font-size:14px}
.chat-top-btn .chat-unread{margin-left:2px}
.chat-top-btn .chat-chan-dot{flex:none}
.chat-top-sep{width:1px;height:20px;background:var(--border);margin:0 4px}
.chat-top-btn.chan{color:var(--ink)}
.chat-top-btn.chan.active{color:#fff}

/* ── v3: notification feed ── */
.chat-notif-feed{gap:10px}
.chat-notif-card{display:flex;gap:12px;background:#fff;border:1px solid var(--border);border-radius:12px;padding:13px 15px;align-items:flex-start}
.chat-notif-icon{width:38px;height:38px;border-radius:10px;background:var(--cream);display:flex;align-items:center;justify-content:center;font-size:17px;flex:none}
.chat-notif-body{flex:1;min-width:0}
.chat-notif-src{font-size:10px;letter-spacing:.1em;text-transform:uppercase;font-weight:700;color:var(--muted)}
.chat-notif-title{font-size:13.5px;font-weight:600;margin-top:2px;word-break:break-word}
.chat-notif-text{font-size:12.5px;color:var(--muted);margin-top:2px;word-break:break-word}
.chat-notif-time{font-size:11px;color:var(--muted);white-space:nowrap;margin-top:2px}

/* ── Overlay messenger (open chat from anywhere) ── */
#chat-overlay{position:fixed;inset:0;background:rgba(14,12,10,.48);z-index:1000;display:flex;align-items:center;justify-content:center;padding:22px;animation:chatfade .14s ease;overscroll-behavior:contain}
@keyframes chatfade{from{opacity:0}to{opacity:1}}
.chat-overlay-panel{position:relative;width:min(1240px,96vw);height:min(880px,93vh);background:var(--paper);border-radius:14px;box-shadow:0 28px 90px rgba(0,0,0,.4);overflow:hidden;display:flex;flex-direction:column;animation:chatrise .16s ease}
@keyframes chatrise{from{transform:translateY(14px);opacity:.6}to{transform:none;opacity:1}}
.chat-overlay-close{position:absolute;top:10px;right:14px;z-index:10;width:34px;height:34px;border-radius:50%;border:1.5px solid var(--border);background:#fff;font-size:20px;line-height:1;cursor:pointer;color:var(--muted)}
.chat-overlay-close:hover{background:var(--cream);color:var(--ink)}
.chat-overlay-body{flex:1;min-height:0;padding:14px 18px 18px;display:flex;flex-direction:column}
.chat-overlay-body #chat-app{flex:1;min-height:0;display:flex;flex-direction:column}
.chat-overlay-body .page-hdr{margin-bottom:10px;padding-right:44px}
.chat-overlay-body .chat-wrap{flex:1;height:auto !important;min-height:0}

@media (max-width:900px){
  .chat-wrap{grid-template-columns:1fr;height:calc(100vh - 120px)}
  .chat-side{display:none}
  .chat-wrap.show-side .chat-side{display:flex}
  .chat-wrap.show-side .chat-main{display:none}
  .chat-members-panel{width:100%}
  #chat-dock{display:none !important}
  body.has-chat-rail{padding-right:0}
  #chat-overlay{padding:0}
  .chat-overlay-panel{width:100vw;height:100vh;border-radius:0}
}
