/* ====== Variabili CSS WhatsApp ====== */
:root {
  --wa-primary: #00a884;
  --wa-message-out: #dbffd4;
}

/* ====== SIDEBAR (solo lista/chat list) ====== */
.whatsapp-sidebar-overlay {
  display: none;
  position: fixed;
  inset: 0;
  background: rgba(0, 0, 0, .3);
  z-index: 1035;
}


.corpoMessaggi {
  margin-bottom: 0
}

#btn-mark-as-read {
  margin: auto
}

.btn-templates {
  width: 44px;
  height: 44px;
  border-color: #25D366;
  background-color: #fff;
}

.btn-file {
  width: 44px;
  height: 44px;
}

.reply-container {
  background-color: transparent;
  position: absolute;
  z-index: 1;
  bottom: -35px;
  left: 0;
  width: 100%
}

.whatsapp-sidebar {
  position: fixed;
  top: 0;
  right: 0;
  /* non più -400 / -100% */
  width: 300px;
  height: 100%;
  background: #fff;
  box-shadow: -2px 0 8px rgba(0, 0, 0, .2);
  z-index: 1040;
  display: flex;
  flex-direction: column;

  transform: translateX(100%);
  /* fuori schermo di default */
  transition: transform .3s ease;
  /* anima la traslazione */
}

.whatsapp-sidebar.open {
  transform: translateX(0);
  /* in vista */
}

.whatsapp-sidebar-header {
  display: none
}

.whatsapp-sidebar-body {
  flex: 1;
  overflow: auto;
  background: #F7F7F7;
  padding: 16px
}

.chat-preview-message {
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
  font-size: 13px;
  max-width: none;
}

.list-group-item.is-unread {
  border-left: 4px solid #dc3545 !important;
  padding-left: 12px !important
}

.list-group-item.not-unread {
  border-left: 4px solid transparent !important;
  padding-left: 12px !important
}

.whatsapp-sidebar-message {
  position: fixed;
  top: 0;
  bottom: 0;
  background-color: #fff;
  display: none;
}

.whatsapp-sidebar-message .message {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  justify-content: center;
  flex-direction: column;
  height: 100%;
}

.event-typing,
.users-presence {
  padding: .25rem 1rem;
  background-color: #fff;
  border-bottom: 1px solid #eee;
  position: absolute;
  width: 100%;
  z-index: 1;
}

.event-typing {
  z-index: 2;
}

#whatsappSidebar .whatsapp-sidebar-message {
  position: static;
  top: auto;
  bottom: auto;
  background: transparent;
}

#whatsappSidebar .whatsapp-sidebar-message .message {
  height: auto;
  justify-content: flex-start;
}

#menu-actions-container,
.wa-template-btns {
  bottom: 30px;
  left: 0;
  z-index: 1000;
}

.wa-template-btns .btn {
  width: 100%;
  white-space: nowrap;
  text-overflow: ellipsis;
  overflow: hidden;
}

@media (max-width: 768px) {

  .whatsapp-sidebar {
    width: 100%;
    max-width: 100%;
    left: 0;
    right: 0;
    /* fisso a 0, animiamo solo transform */
  }
}

/* ====== CHAT MODAL ====== */
/* .modal-backdrop.show {
  display: none
} */

#chatModal {
  position: fixed;
  display: none;
  inset: auto 310px 0 auto;
  width: 400px;
  height: 521px;
  z-index: 1070;
  box-shadow: 0 3px 6px rgba(0, 0, 0, .16), 0 3px 6px rgba(0, 0, 0, .23);
  transition: right .3s ease, height .3s ease;
  border-radius: 10px 10px 0 0;
  overflow: hidden
}

#chatModal.is-open {
  display: block;
}

/* Z-index dinamico per chat modal basato su stato sidebar */
#chatModal.sidebar-open {
  z-index: 1075;
}

#chatModal.sidebar-closed {
  z-index: 1040;
}

/* Abbassa z-index quando dropdown navbar Ã¨ aperto */
body:has(nav.main-header .navbar-nav .dropdown:hover) #chatModal {
  z-index: 1033 !important;
}

/* Sposta chatModal a sinistra della task sidebar quando aperta */
body.task-sidebar-open #chatModal:not(.collapsed) {
  right: 490px !important;
  transition: right .3s ease, height .3s ease;
}

/* Quando sia WA sidebar che task sidebar sono aperte, chatModal va ancora piÃ¹ a sinistra */
body.task-sidebar-open #whatsappSidebar.open~#chatModal:not(.collapsed),
body.task-sidebar-open:has(#whatsappSidebar.open) #chatModal:not(.collapsed) {
  right: 790px !important;
  /* 480px task + 300px WA + 10px gap */
  transition: right .3s ease, height .3s ease;
}

#chatModal.collapsed {
  height: 45px;
  bottom: 0;
  width: 250px;
}

#chatModal.collapsed #telefono {
  display: none
}

/* struttura interna */
#chatModal .m-0 {
  height: 100%;
  display: flex;
  flex-direction: column;
  background-color: #fff
}

#chatModal .m-0>div {
  height: 100%;
}

#chatModal .bg-light {
  flex-shrink: 0
}

#chatModal .bg-light+div {
  flex-grow: 1;
  display: flex;
  flex-direction: column
}

#chatModal .wa-chat-body {
  overflow-y: auto;
  padding-bottom: 190px;
  /* oppure gestito via JS come giÃ  fai */
}

/* header/body/footer */
#chatModal .chat-header {
  cursor: pointer;
  border-radius: 10px 10px 0 0;
  border-bottom: 1px solid #eee;
  background: #fff;
  color: inherit;
  padding: .75rem 1rem
}

.chat-body {
  transition: max-height .2s ease;
  overflow: hidden;
  height: 100%;
  position: relative
}

.chat-body>div {
  height: 100%
}

#chatModal.collapsed .chat-body {
  opacity: 0;
  max-height: 0;
  pointer-events: none
}

#chatModal.collapsed #chatName {
  display: block;
  max-width: 22ch;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

#chatModal.collapsed .chat-dropdown-manual {
  display: none;
}

.chat-footer {
  padding: 12px 14px 0 14px;
}

/* area messaggi */
.chat-msg-container {
  height: 367px;
  display: flex;
  flex-direction: column;
  overflow-y: auto;
  padding: 16px;
  position: relative;
  transition: all .3s ease
}

.day-title {
  font-weight: 400;
  font-size: 12px;
  color: #666;
  text-align: center;
  margin: 8px 0;
}

.message-bubble {
  display: inline-flex;
  flex-direction: column;
}

.message-info {
  display: block;
  margin-top: 5px;
}

.message-bubble.my-message .message-info {
  align-self: flex-end;
  text-align: right;
}

.bubble-wrapper .retry-btn {
  font-size: 12px;
  opacity: .85;
}

.message-bubble .status-icon {
  margin-left: 4px;
}

/* Contenitore media + testo */
.message-media {
  display: grid;
  gap: .5rem;
}

.media-preview {
  display: block;
  width: auto;
  height: auto;
  max-width: 100%;
  max-height: 260px;
}

.message-media img.media-preview {
  object-fit: contain;
  width: auto;
  height: auto;
  max-width: 100%;
  max-height: 260px;
  min-width: 0;
  min-height: 0;
}

.message-media video.media-preview {
  max-width: 100%;
  height: auto;
  object-fit: contain;
}

.message-media audio.media-preview {
  width: 100%;
  max-width: 100%;
}

.message-text {
  white-space: pre-wrap;
  word-wrap: break-word;
}


/* Dropdown manuale nel header */
.chat-dropdown-manual {
  position: relative;
  display: inline-block;
  padding-right: 0px;
}

.chat-dropdown-toggle-manual {
  box-shadow: 0 0 0 .5px lightgrey;
  color: var(--primary-color);
  padding: 4px 8px;
  cursor: pointer;
  font-size: 12px;
  border-radius: 4px;
  margin-right: 5px;
}

.chat-dropdown-toggle-manual:hover {
  background-color: var(--form-elements-color);
  color: var(--primary-color);
}

.chat-dropdown-toggle-manual:focus {
  box-shadow: 0 0 0 .5px lightgrey;
}

.chat-dropdown-menu-manual {
  position: absolute;
  top: 100%;
  right: 0;
  background: #fff;
  border: 1px solid #dee2e6;
  border-radius: 8px;
  box-shadow: 0 .5rem 1rem rgba(0, 0, 0, .15);
  min-width: 150px;
  z-index: 1000;
  margin-top: 2px;
  display: none;
  padding: 5px 0px;
}

.chat-dropdown-item-manual {
  display: block;
  width: 100%;
  padding: .375rem 1rem;
  border: none;
  background: none;
  text-align: left;
  color: #212529;
  cursor: pointer;
  font-size: .875rem;
  border-radius: 8px;
}

.chat-dropdown-item-manual:hover {
  background: #f8f9fa;
}

.chat-dropdown-item-manual:focus {
  background: #e9ecef;
  outline: none;
}

#card-name {
  display: flex;
  align-items: center;
  gap: .25rem;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

#card-name span {
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
  min-width: 0;
  flex: 1;
}

.chat-card .when {
  font-size: 12px;
  flex-shrink: 0;
}

#chatHeader #chatName {
  font-size: 15px;
  max-width: 30ch;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
  display: inline-block;
}

#chatHeader #chatTypeIconImg {
  flex: 0 0 auto;
}

.gap-1 {
  gap: .25rem !important;
}

/* Auto-resize textarea (#msg) */

#chatModal .chat-footer {
  margin-top: auto;
  display: flex;
  flex-direction: column;
  justify-content: flex-end;
  padding: 12px 16px 3px 16px;
  position: absolute;
  bottom: 0;
  width: 100%;
  left: 0;
  right: 0;
  margin-top: 0;
  text-align: center
}

#chatModal .chat-footer textarea#msg {
  width: 100%;
  min-height: 50px;
  max-height: 250px;
  height: auto;
  line-height: 1.4;
  resize: none;
  overflow-y: hidden;
  margin-top: auto;
}

#chatModal .chat-footer .input-group {
  align-items: stretch;
}

#chatModal .chat-footer textarea#msg {
  display: block;
  width: 100%;
  min-height: 50px;
  max-height: 250px;
  height: auto;
  line-height: 1.4;
  resize: none;
  overflow-y: hidden;
}

#chatModal .chat-footer .input-group>button {
  align-self: flex-end;
}

#chatTypeIconImg {
  width: 20px;
  height: 20px;
  margin-right: 2px;
  margin-bottom: 4px;
  display: none;
  color: var(--primary-color);
}

.wa-template-selector {
  width: 100%;
  max-height: 150px;
  overflow-y: auto;
  z-index: 1000;
}

.wa-message-input {
  height: 50px;
  resize: none;
}

@media (max-width: 768px) {

  nav.main-header.navbar.navbar-expand.navbar-white.navbar-light {
    z-index: 1038;
  }

  #chatModal.collapsed #chatName {
    display: block;
    max-width: 50ch;
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
  }

  /* ChatModal full screen */
  #chatModal {
    inset: auto 0 0 0 !important;
    /* bottom:0; left:0; right:0 */
    width: 100% !important;
    height: 100% !important;
    max-height: 100% !important;
    border-radius: 0 !important;
    z-index: 1100 !important;
    /* sopra la sidebar (1080) */
  }

  /* In mobile la sidebar aperta NON deve spingere la chat a destra */
  /* (il JS non dovrÃ  piÃ¹ toccare right:310px su mobile) */
  #chatModal.is-open {
    right: 0 !important;
  }

  /* Stato collassato: resta in basso, full width */
  #chatModal.collapsed {
    height: 56px !important;
    bottom: 0 !important;
    top: auto !important;
    width: 100% !important;
    left: 0 !important;
    right: 0 !important;
    border-radius: 10px 10px 0 0 !important;
    z-index: 1100 !important;
    /* evita che â€œsalgaâ€ sopra la sidebar */
  }

  /* Body messaggi: occupa tutto lo spazio disponibile in full screen */
  #chatModal .wa-chat-body {
    height: auto !important;
    max-height: none !important;
    flex: 1 1 auto !important;
    overflow-y: auto !important;
  }

  .chat-msg-container {
    height: 100vh;
    padding-bottom: 190px;
  }

  .chat-footer {
    position: absolute;
    bottom: 0px;
    width: 100vw;
  }

  .chat-dropdown-toggle-manual {
    font-size: 10px;
  }

  #chatHeader #chatName {
    max-width: 20ch;

  }
}

.message-sender {
  font-size: 0.8rem;
  font-weight: 600;
  margin-bottom: .25rem;
}

#wp-unread-badge {
  zoom: 0.9;
  top: 0;
  border-radius: 50%;
  padding: 3px;
  left: 35px;
  font-size: 12px;
  display: inline-block;
  min-width: 18px;
  height: 18px;
  text-align: center;
}

#wp-unread-badge .fa-whatsapp {
  color: #333
}

.badge-danger {
  color: #fff;
  background-color: #dc3545;
}

.wa-modal {
  background: #f0f2f5;
  color: #111b21 !important;
}

.wa-chat-body {
  padding: 0px;
  display: flex;
  flex-direction: column;
  height: 100%;
  background-color: #efeae2;
  transition: background-color .3s ease;
  transform-origin: left top;
}

.wa-contact {
  position: sticky;
  z-index: 2;
  top: 0;
  height: 4rem;
  background: #f0f2f5;
  color: #111b21 !important;
  padding: 10px 16px;
}

.wa-icon {
  font-size: 1.5rem;
  color: white;
}

.wa-number {
  font-size: 1rem;
  color: inherit;
}

.messages-container {
  min-height: 50vh;
  max-height: 500px;
  display: flex;
  flex-direction: column;
  overflow-y: auto;
  padding: 16px;
  position: relative;
  transition: all .3s ease;
}

.messages-container-old {
  background-color: #efeae2;
  transition: background-color .3s ease;
  transform-origin: left top;
  overflow-y: hidden;
}

.messages-container-image {
  position: absolute;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
  background-image: url(/assets/img/wa-bg.png);
  background-repeat: repeat;
  opacity: 0.4;
}

.message-bubble {
  position: relative;
  max-width: 75%;
  width: auto;
  min-width: 75px;
  background: white;
  border-radius: 0 8px 8px 8px;
  box-shadow: 0 1px 0.5px rgba(0, 0, 0, .13);
  padding: 8px 12px 16px;
  margin-right: auto;

  &:not(.my-message) .message-tail {
    position: absolute;
    top: 0;
    left: -8px;
    width: 8px;
    height: 13px;
    color: white;

    svg {
      display: block;
      pointer-events: none;
    }
  }

  &+&:not(.my-message) {
    margin-bottom: 2px;
  }

  &:not(& + &:not(.my-message)) {
    margin-bottom: 12px;
  }

  &:not(.my-message)+&:not(.my-message) {
    border-radius: 8px;

    .message-tail {
      display: none;
    }
  }

  &+&.my-message,
  &+.day-container {
    margin-bottom: 12px;
  }

  &:not(& + &.my-message) {
    margin-bottom: 2px;
  }

  &.my-message+&.my-message {
    .message-tail {
      display: none;
    }
  }

  &:last-child {
    margin-bottom: 12px !important;
  }
}

.my-message {
  margin-left: auto;
  margin-right: 0;
  background: var(--wa-message-out);
  border-radius: 8px 0 8px 8px;

}

.message-info {
  position: absolute;
  bottom: 4px;
  right: 8px;
  font-size: 0.7rem;
  color: #667781;
  display: flex;
  align-items: center;
  gap: 4px;
}

.message-text {
  white-space: pre-wrap;
  word-break: break-word;
  margin-bottom: 4px;
}

.wa-input-container {
  padding: .5rem 1rem;
  background: #f0f0f0;
}

.wa-message-input {
  background: #fff !important;
  border-radius: 8px !important;
  border: 1px solid #e9edef !important;
  padding: 8px !important;
  resize: none;
  font-size: 14px !important;
}

.status-icon {
  transition: all 0.3s ease;
}

.status-icon.sent {
  color: var(--wa-primary);
}

/* Media message styling */
.media-message {
  max-width: 300px;
}

.message-media {
  position: relative;
  margin-bottom: 4px;
  width: 100%;
}

.media-preview {
  max-width: 100%;
  min-width: 200px;
  border-radius: 4px;
  cursor: pointer;
  margin-bottom: 4px;
  max-height: 250px;
}

.media-icon {
  font-size: 30px;
  color: #075e54;
  background-color: rgba(0, 0, 0, 0.05);
  padding: 15px;
  border-radius: 8px;
  margin-bottom: 5px;
  display: block;
}

/* File message styling */
.message-file {
  display: flex;
  align-items: center;
  padding: 8px;
  cursor: pointer;
  border-radius: 4px;
  background-color: rgba(0, 0, 0, 0.05);
  margin-bottom: 4px;
  transition: background-color 0.2s ease;
}

.message-file:hover {
  background-color: rgba(0, 0, 0, 0.1);
}

.file-icon {
  font-size: 24px;
  margin-right: 10px;
}

/* File type colors */
.file-pdf .file-icon {
  color: #e74c3c;
}

.file-word .file-icon {
  color: #2b579a;
}

.file-excel .file-icon {
  color: #217346;
}

.file-powerpoint .file-icon {
  color: #d24726;
}

.file-archive .file-icon {
  color: #f39c12;
}

.file-text .file-icon {
  color: #7f8c8d;
}

.file-code .file-icon {
  color: #3498db;
}

.file-generic .file-icon {
  color: #95a5a6;
}

.file-info {
  overflow: hidden;
}

.file-name {
  font-weight: bold;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
  margin-bottom: 2px;
}

.wa-footer {
  padding: 5px 1rem;
  background: #f0f2f5;
}

.day-container {
  display: flex;
  justify-content: center;
  z-index: 1;
  margin-bottom: 12px;

  .day-title {
    box-sizing: border-box;
    display: inline-block;
    flex: none;
    max-width: 100% !important;
    padding: 5px 12px 6px 12px;
    font-size: 12.5px;
    line-height: 21px;
    color: #54656f;
    text-align: center;
    text-shadow: 0 1px 0 rgba(255, 255, 255, .4);
    background-color: rgba(255, 255, 255, .95);
    border-radius: 7.5px;
    box-shadow: 0 1px .5px rgba(11, 20, 26, .13);
  }

  &.last {
    margin-bottom: 0;
  }

  &.first.load-previous-container {
    padding-bottom: 8px;
  }
}

/* Load Previous Messages Button */
.load-previous-container {
  width: 100%;
  display: flex;
  justify-content: center;
  padding: 6px 0;
}

.btn-load-previous-messages {
  background-color: #fff;
  color: #54656f;
  border: 1px solid #e9edef;
  border-radius: 24px;
  padding: 6px 12px;
  font-size: 13px;
  cursor: pointer;
  transition: all 0.2s ease;
  box-shadow: 0 1px 0.5px rgba(11, 20, 26, .13);
  display: flex;
  align-items: center;
  gap: 5px;
}

.btn-load-previous-messages:hover {
  background-color: #f5f5f5;
}

.btn-load-previous-messages.loading {
  opacity: 0.7;
  cursor: default;
}

@media (max-width: 768px) {
  #whatsappModal .wa-modal {
    height: 100vh;
  }

  #whatsappModal .messages-container {
    height: 100%;
    max-height: 100vh;
  }
}

span.sfondowa {
  background-image: url('/assets/img/sfondowa.png');
  opacity: 0.1;
  position: absolute;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
  z-index: 0
}

span.sfondo-email {
  background-image: url('/assets/img/sfondo-email.png');
  opacity: 0.6;
  position: absolute;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
  z-index: 0
}

/* Supporto per i nuovi elementi del whatsapp-reply-container */
.gap-2 {
  gap: 0.5rem !important;
}

.reply-container {
  padding: 0px 12px 7px;
}

.reply-container .rounded-circle {
  border-radius: 50% !important;
}

.reply-container .reply-input {
  border: 1px solid #ced4da;
  transition: border-color 0.15s ease-in-out, box-shadow 0.15s ease-in-out;
}

.reply-container .reply-input:focus {
  border-color: #25D366;
  outline: 0;
  box-shadow: 0 0 0 0.2rem rgba(37, 211, 102, 0.25);
}

#wa-template-preview-mode {
  padding: 12px;
}

#wa-template-preview {
  background-color: #f8f9fa !important;
  border: 1px solid #dee2e6;
}

.templates-menu {
  width: calc(100% - 1.5rem);
  height: 300px;
  overflow-y: auto;
  position: absolute;
  bottom: 100%;
  z-index: 1000;
  display: none;
  list-style: none;
  background-color: white;
  border-radius: 5px;
  padding: 10px;
}

.templates-menu .dropdown-item {
  white-space: normal;
  word-wrap: break-word;
  padding: 0.5rem 1rem;
}

.templates-menu .dropdown-item:hover {
  background-color: #f0f2f5;
  cursor: pointer;
}

.chat-footer small.text-muted {
  display: block;
  margin: auto;
}