/* TIEN GIOI TRUYEN 3D / CSKH V4.7.1 - READABLE UI & CHAT CONTAINMENT
   Requires helpdesk-v46.css + staff-account-v47.css.
   CSS-only visual patch: no Supabase tables, access rules, customer data, or chat routing changes.
   Scoped to #app and existing staff dashboard classes. */

/* Prevent overflow at every nested grid/flex boundary, without hiding modal/popup UI. */
#app .app-shell, #app .work, #app .main, #app .dashboard,
#app .dashboard .chat-panel, #app .dashboard .list-panel,
#app .chat-panel .messages, #app .chat-panel .composer {
  box-sizing:border-box!important;
  min-width:0!important;
  max-width:100%!important;
}
#app .dashboard {overflow:hidden!important;}
#app .chat-panel {
  position:relative!important;
  display:flex!important;
  flex-direction:column!important;
  overflow:hidden!important;
  isolation:isolate;
}
#app .chat-panel .conv-head {box-sizing:border-box!important;min-width:0!important;width:100%!important;flex-shrink:0!important;}
#app .conv-head .conv-person {min-width:0!important;max-width:100%;}
#app .conv-head .conv-title {overflow-wrap:anywhere;}
#app .conv-head .toolbar {max-width:100%;}

/* Only the message feed scrolls. Never show horizontal scrollbar or clip the last chat. */
#app .chat-panel .messages {
  box-sizing:border-box!important;
  width:100%!important;
  min-width:0!important;
  max-width:100%!important;
  min-height:0!important;
  flex:1 1 auto!important;
  overflow-y:auto!important;
  overflow-x:hidden!important;
  overscroll-behavior-x:none;
  scroll-behavior:auto;
  padding:20px clamp(14px,2.5vw,28px)!important;
  display:flex!important;
  flex-direction:column!important;
  align-items:stretch!important;
}
#app .chat-panel .messages .bubble {
  box-sizing:border-box!important;
  min-width:0!important;
  flex:0 0 auto!important;
  width:fit-content!important;
  max-width:min(84%,640px)!important;
  margin-inline-start:0!important;
  margin-inline-end:auto!important;
  overflow-wrap:anywhere!important;
  word-break:break-word;
  white-space:normal!important;
}
#app .chat-panel .messages .bubble:before {display:none!important;}
/* Former V4.5 absolute avatars were positioned 40px outside bubbles and
   caused the horizontal scrollbar / clipped right edge in the screenshot. */
#app .chat-panel .messages .bubble .bubble-avatar {
  position:absolute!important;
  top:8px!important;
  left:8px!important;
  right:auto!important;
  width:24px!important;
  height:24px!important;
  min-width:24px!important;
  z-index:1;
}
#app .chat-panel .messages .bubble.staff .bubble-avatar {
  left:auto!important;
  right:8px!important;
}
#app .chat-panel .messages .bubble .bubble-head {
  padding-inline-start:28px!important;
}
#app .chat-panel .messages .bubble.staff .bubble-head {
  padding-inline-start:0!important;
  padding-inline-end:28px!important;
}
#app .chat-panel .messages .bubble.staff {
  margin-inline-start:auto!important;
  margin-inline-end:0!important;
}
#app .chat-panel .messages .bubble-body {
  display:block;
  min-width:0!important;
  max-width:100%!important;
  white-space:pre-wrap!important;
  overflow-wrap:anywhere!important;
  word-break:break-word!important;
}
#app .chat-panel .messages .bubble-head {min-width:0!important;max-width:100%;flex-wrap:wrap;}
#app .chat-panel .messages .bubble .tg-media-anchor {
  box-sizing:border-box!important;
  display:block!important;
  width:fit-content!important;
  max-width:100%!important;
  min-width:0!important;
  overflow:hidden!important;
}
#app .chat-panel .messages .bubble .tg-chat-image {
  display:block!important;
  width:auto!important;
  height:auto!important;
  max-width:min(100%,340px)!important;
  max-height:min(52dvh,410px)!important;
  object-fit:contain!important;
}
#app .chat-panel .messages .tg-media-loading {
  max-width:100%;white-space:normal;overflow-wrap:anywhere;
}
#app .chat-panel .composer {flex:0 0 auto!important;overflow:visible!important;}
#app .chat-panel .composer .compose-input {box-sizing:border-box!important;min-width:0!important;max-width:100%!important;}
#app .chat-panel .composer #reply-text {box-sizing:border-box!important;min-width:0!important;width:100%!important;}
#app .list-panel .thread-list {overflow-x:hidden!important;}
#app .thread .thread-info {min-width:0;}

/* Only light mode receives dark text. Preserve dark theme's original palette. */
html:not([data-cskh-theme-resolved="dark"]) #app .app-shell,
html:not([data-cskh-theme-resolved="dark"]) #app .dashboard,
html:not([data-cskh-theme-resolved="dark"]) #app .work {
  color:#162333!important;
}
html:not([data-cskh-theme-resolved="dark"]) #app .header-workspace,
html:not([data-cskh-theme-resolved="dark"]) #app .panel-head h2,
html:not([data-cskh-theme-resolved="dark"]) #app .conv-title,
html:not([data-cskh-theme-resolved="dark"]) #app .hd-folder-brand strong,
html:not([data-cskh-theme-resolved="dark"]) #app .customer-panel .customer h3 {
  color:#172436!important;font-weight:850!important;
}
html:not([data-cskh-theme-resolved="dark"]) #app .hd-folder-item {
  color:#263549!important;font-weight:750!important;
}
html:not([data-cskh-theme-resolved="dark"]) #app .hd-folder-item b,
html:not([data-cskh-theme-resolved="dark"]) #app .kpi strong {
  color:#243347!important;font-weight:850!important;
}
html:not([data-cskh-theme-resolved="dark"]) #app .kpi small,
html:not([data-cskh-theme-resolved="dark"]) #app .hd-folder-heading,
html:not([data-cskh-theme-resolved="dark"]) #app .hd-folder-bottom small,
html:not([data-cskh-theme-resolved="dark"]) #app .hd-folder-brand small {
  color:#475569!important;font-weight:700!important;
}
html:not([data-cskh-theme-resolved="dark"]) #app .header .pill,
html:not([data-cskh-theme-resolved="dark"]) #app .header .btn,
html:not([data-cskh-theme-resolved="dark"]) #app .header .language,
html:not([data-cskh-theme-resolved="dark"]) #app .inbox-tab,
html:not([data-cskh-theme-resolved="dark"]) #app .panel-head .btn {
  color:#2b4055!important;font-weight:750!important;
}
html:not([data-cskh-theme-resolved="dark"]) #app .inbox-tab.active {
  color:#134c87!important;background:#e3efff!important;border-color:#bed6f2!important;
}
html:not([data-cskh-theme-resolved="dark"]) #app .thread .thread-info strong,
html:not([data-cskh-theme-resolved="dark"]) #app .thread .thread-info small,
html:not([data-cskh-theme-resolved="dark"]) #app .thread time {
  color:#203044!important;font-weight:700!important;
}
html:not([data-cskh-theme-resolved="dark"]) #app .thread .thread-info small,
html:not([data-cskh-theme-resolved="dark"]) #app .thread time {color:#4b6077!important;font-weight:650!important;}
html:not([data-cskh-theme-resolved="dark"]) #app .search-wrap .input,
html:not([data-cskh-theme-resolved="dark"]) #app .search-wrap select,
html:not([data-cskh-theme-resolved="dark"]) #app .composer #reply-text {
  color:#17293b!important;font-size:13px!important;font-weight:600!important;
}
html:not([data-cskh-theme-resolved="dark"]) #app .search-wrap .input::placeholder,
html:not([data-cskh-theme-resolved="dark"]) #app .composer #reply-text::placeholder {color:#526579!important;opacity:1!important;}
html:not([data-cskh-theme-resolved="dark"]) #app .conv-head small,
html:not([data-cskh-theme-resolved="dark"]) #app .customer-panel .info-row small,
html:not([data-cskh-theme-resolved="dark"]) #app .customer-panel .customer-note,
html:not([data-cskh-theme-resolved="dark"]) #app .empty {color:#506177!important;font-weight:650!important;}
html:not([data-cskh-theme-resolved="dark"]) #app .customer-panel .info-row div {
  color:#213246!important;font-weight:750!important;
}
html:not([data-cskh-theme-resolved="dark"]) #app .messages .bubble {
  background:#ffffff!important;color:#172b40!important;
  border-color:#d9e2ef!important;
}
html:not([data-cskh-theme-resolved="dark"]) #app .messages .bubble.staff {
  background:#dbeaff!important;color:#142d4c!important;
  border-color:#bfd7f7!important;
}
html:not([data-cskh-theme-resolved="dark"]) #app .messages .bubble-body {
  color:#172638!important;font-size:13px!important;font-weight:600!important;line-height:1.7!important;
}
html:not([data-cskh-theme-resolved="dark"]) #app .messages .bubble-head strong {
  color:#1a304a!important;font-size:12px!important;font-weight:800!important;
}
html:not([data-cskh-theme-resolved="dark"]) #app .messages .bubble-head time,
html:not([data-cskh-theme-resolved="dark"]) #app .messages .bubble>small {
  color:#425c78!important;font-size:11px!important;font-weight:650!important;
}
html:not([data-cskh-theme-resolved="dark"]) #app .messages .bubble .manage button {
  color:#164f91!important;font-size:12px!important;font-weight:750!important;
  text-decoration:none!important;
}
html:not([data-cskh-theme-resolved="dark"]) #app .messages .bubble .manage button:hover {
  color:#103c6c!important;text-decoration:underline!important;
}
html:not([data-cskh-theme-resolved="dark"]) #app .composer .shortcut {
  color:#344e6a!important;background:#f2f6fc!important;border-color:#d6e0ed!important;
  font-weight:700!important;font-size:11px!important;
}
html:not([data-cskh-theme-resolved="dark"]) #app .composer .tg-photo-button {
  color:#243b56!important;font-weight:800!important;
}
html:not([data-cskh-theme-resolved="dark"]) #app .composer .composer-actions .btn {
  background:#246db5!important;color:#fff!important;font-weight:850!important;
}
html:not([data-cskh-theme-resolved="dark"]) #app .thread .badge:not(.closed) {
  color:#155a38!important;background:#e6f6ec!important;
}
html:not([data-cskh-theme-resolved="dark"]) #app .thread .badge.closed {
  color:#46576c!important;background:#e9edf3!important;
}
html:not([data-cskh-theme-resolved="dark"]) #app .hd-account-popup .hd-account-option strong,
html:not([data-cskh-theme-resolved="dark"]) #app .hd-account-popup .hd-account-theme strong,
html:not([data-cskh-theme-resolved="dark"]) #app .hd-account-popup .hd-account-action strong {
  color:#1e2b3b!important;font-weight:800!important;
}
html:not([data-cskh-theme-resolved="dark"]) #app .hd-account-popup small,
html:not([data-cskh-theme-resolved="dark"]) #app .hd-account-popup .hd-account-muted {
  color:#4c596b!important;font-weight:600!important;
}

/* In dark mode retain light-on-dark contrast even if earlier styles default to pale beige. */
html[data-cskh-theme-resolved="dark"] #app .messages .bubble-body,
html[data-cskh-theme-resolved="dark"] #app .messages .bubble-head strong {
  color:#f0f5fc!important;font-weight:650!important;
}
html[data-cskh-theme-resolved="dark"] #app .messages .bubble-head time,
html[data-cskh-theme-resolved="dark"] #app .messages .bubble>small {
  color:#bfcee3!important;
}
html[data-cskh-theme-resolved="dark"] #app .messages .bubble .manage button {
  color:#a9cffc!important;font-weight:750!important;
}
html[data-cskh-theme-resolved="dark"] #app .composer #reply-text::placeholder,
html[data-cskh-theme-resolved="dark"] #app .search-wrap .input::placeholder {
  color:#c4d0e0!important;opacity:1;
}

/* Responsive: the real dashboard uses mobile list/detail view; do not hide the list permanently. */
@media(max-width:880px){
  #app .dashboard.show-detail .chat-panel{min-height:0!important;max-height:none!important;}
  #app .dashboard.show-detail .chat-panel .messages{padding:13px 13px!important;}
  #app .chat-panel .messages .bubble{max-width:min(90%,540px)!important;}
  #app .chat-panel .messages .bubble .tg-chat-image{max-width:min(100%,270px)!important;max-height:43dvh!important;}
  #app .conv-head .toolbar{flex-wrap:wrap!important;}
}
@media(max-width:440px){
  #app .dashboard.show-detail .chat-panel .messages{padding:12px 11px!important;gap:12px!important;}
  #app .chat-panel .messages .bubble{max-width:93%!important;padding:10px 11px!important;}
  #app .chat-panel .messages .bubble .tg-chat-image{max-width:min(100%,245px)!important;}
  #app .composer .compose-input{grid-template-columns:minmax(0,1fr) 44px 80px!important;}
  #app .composer .composer-actions .btn{font-size:10px!important;}
  #app .composer #photo-pick{min-width:0!important;}
}
@media(forced-colors:active){#app .messages .bubble{border:1px solid CanvasText!important}}
/* Mobile chat header: return navigation, visitor name and actions must never overlap. */
@media (max-width:540px) {
  #app .chat-panel .conv-head {
    display:flex!important;
    flex-direction:column!important;
    justify-content:center!important;
    align-items:stretch!important;
    gap:6px!important;
    min-height:88px!important;
    padding:8px 10px!important;
  }
  #app .chat-panel .conv-identity {
    display:flex!important;
    flex:0 0 auto!important;
    width:100%!important;
    min-width:0!important;
    align-items:center!important;
    flex-wrap:nowrap!important;
    gap:7px!important;
  }
  #app .chat-panel .conv-identity .mobile-back {
    font-size:0!important;
    flex:0 0 32px!important;
    min-width:32px!important;
    width:32px!important;
    height:29px!important;
    margin-right:0!important;
    padding:0!important;
  }
  #app .chat-panel .conv-person {
    flex:1 1 auto!important;
    overflow:hidden!important;
    min-width:0!important;
  }
  #app .chat-panel .conv-person .conv-title,
  #app .chat-panel .conv-person small {
    overflow:hidden!important;
    text-overflow:ellipsis!important;
    white-space:nowrap!important;
  }
  #app .chat-panel .conv-head .toolbar {
    display:flex!important;
    width:100%!important;
    flex:0 0 auto!important;
    flex-wrap:nowrap!important;
    justify-content:flex-end!important;
    gap:5px!important;
  }
  #app .chat-panel .conv-head .toolbar .btn {
    font-size:10px!important;
    padding:4px 8px!important;
    min-height:27px!important;
  }
}
