/* Chat UI styles for user widget and responsable view */
:root {
  /* Color principal del widget de chat (pastel verde) */
  --chat-primary: #79d2ad; /* pastel green to match site style */
  --chat-bg: #f5f7fb;
  --chat-panel: #ffffff;
  --chat-text: #1f2937;
  --chat-muted: #6b7280;
  --chat-border: #e5e7eb;
}

/* Floating launcher button (similar to 3.png) */
.chat-launcher {
  position: fixed;
  right: 24px;
  bottom: 24px;
  width: 48px;
  height: 48px;
  border-radius: 999px;
  background: var(--chat-primary);
  color: #fff;
  display: flex;
  align-items: center;
  justify-content: center;
  box-shadow: 0 8px 24px rgba(0,0,0,0.18);
  border: 1px solid rgba(0,0,0,0.06);
  cursor: pointer;
  z-index: 9999;
}
.chat-launcher:hover { filter: brightness(1.05); }

/* User modal widget (in-page) */
.chat-modal {
  position: fixed;
  right: 24px;
  bottom: 88px;
  width: 360px;
  max-height: 70vh;
  background: var(--chat-panel);
  border-radius: 16px;
  box-shadow: 0 16px 40px rgba(0,0,0,0.2);
  display: none;
  flex-direction: column;
  overflow: hidden;
  z-index: 10001; /* por encima del launcher */
}
.chat-modal.open { display: flex; }
.chat-modal.fullscreen { right: 50%; transform: translateX(50%); width: 880px; max-height: 82vh; }

.chat-header {
  background: linear-gradient(180deg, #111827, #1f2937);
  color: #fff;
  padding: 16px;
}
.chat-header .title { font-weight: 600; font-size: 16px; }
.chat-header .subtitle { color: #cbd5e1; font-size: 12px; margin-top: 4px; }
.chat-header .topbar { display: flex; align-items: center; justify-content: space-between; }
.chat-header .avatars { display: flex; gap: 8px; }
.chat-header .avatar { width: 24px; height: 24px; border-radius: 999px; background: #fff; opacity: 0.85; }

.chat-body {
  background: 
    /* Custom SVG pattern */
    url('chat-pattern.svg'),
    /* Base gradient */
    linear-gradient(180deg, #ffffff, #f3f4f6);
  background-size: 60px 60px, 100% 100%;
  padding: 12px;
  overflow-y: auto;
}

.message-row { display: flex; margin: 6px 0; }
.message { max-width: 72%; padding: 8px 12px; border-radius: 16px; font-size: 14px; line-height: 1.35; white-space: pre-wrap; word-break: break-word; overflow-wrap: anywhere; position: relative; }
.message.user { background: #d9fdd3; color: #000000; margin-left: auto; border-bottom-right-radius: 4px; }
.message.responsable { background: #edf2fe; color: #1f2937; margin-right: auto; border-bottom-left-radius: 4px; }
.message-meta { font-size: 11px; color: var(--chat-muted); margin-top: 4px; }

/* Message content and attachments */
.message-content { display: block; }
.message-content img, .message-content video { max-width: 100%; border-radius: 12px; display: block; }
.message-content audio { width: 350px; max-width: 100%; min-width: 220px; height: 40px; display: block; border-radius: 3px; background: #fff; flex-shrink: 0; }
.message.user .message-content audio { filter: brightness(0.98); background: #d9fdd3; }
.message.responsable .message-content audio { background: #edf2fe; }

/* Compact reply preview inside bubble */
.reply-preview { background: rgba(255,255,255,0.18); border-left: 3px solid rgba(0,0,0,0.2); padding: 4px 8px; margin-bottom: 6px; border-radius: 8px; }
.message.responsable .reply-preview { background: rgba(22,119,255,0.08); border-left-color: #1677ff; }
.reply-preview .reply-author { font-size: 12px; font-weight: 600; color: #d1fae5; }
.message.responsable .reply-preview .reply-author { color: #0f5132; }
.reply-preview .reply-text { font-size: 12px; color: #f1f5f9; display: -webkit-box; -webkit-line-clamp: 1; -webkit-box-orient: vertical; overflow: hidden; }
.message.responsable .reply-preview .reply-text { color: #1f2937; }

.chat-footer { 
  border-top: 1px solid var(--chat-border); 
  padding: 8px; 
  background: #fff; 
  display: flex;
  flex-direction: column;
  gap: 6px;
}

.chat-input-container {
  display: flex;
  align-items: center;
  gap: 6px;
  padding: 6px 10px;
  background: #fff;
  border-radius: 20px;
  border: 1px solid var(--chat-border);
  margin: 0 12px;
  box-shadow: 0 1px 2px rgba(17,24,39,0.06);
  position: relative;
  user-select: none;
  -webkit-user-select: none;
  -moz-user-select: none;
  -ms-user-select: none;
  touch-action: none;
  pointer-events: auto;
  max-width: calc(100% - 24px);
}

.chat-input-row {
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 8px 12px;
}
.chat-input { 
  flex: 1; 
  height: 36px;
  border: none; 
  border-radius: 18px; 
  padding: 6px 12px; 
  font-size: 14px; 
  background: transparent;
  outline: none;
  user-select: text;
  -webkit-user-select: text;
  -moz-user-select: text;
  -ms-user-select: text;
  touch-action: manipulation;
}
.chat-input::placeholder{ color: var(--chat-muted); }
.chat-input:focus{ outline: none; border-color: transparent; box-shadow: none; }
.chat-btn {
  background: transparent;
  border: none;
  color: #8696a0;
  font-size: 18px;
  cursor: pointer;
  padding: 6px;
  border-radius: 50%;
  transition: all 0.2s ease;
  display: flex;
  align-items: center;
  justify-content: center;
  min-width: 32px;
  min-height: 32px;
  user-select: none;
  -webkit-user-select: none;
  -moz-user-select: none;
  -ms-user-select: none;
  touch-action: manipulation;
}

.chat-btn:hover {
  background: rgba(134, 150, 160, 0.1);
  color: #54656f;
}
.chat-send { 
  background: #25d366; 
  color: #fff; 
  border: none; 
  border-radius: 50%; 
  width: 36px; 
  height: 36px; 
  cursor: pointer; 
  display:flex; 
  align-items:center; 
  justify-content:center; 
  box-shadow: 0 2px 8px rgba(37, 211, 102, 0.3);
}
.chat-send i { font-size: 16px; }
.chat-send:hover { filter: brightness(1.05); }
.chat-actions { 
  display:flex; 
  align-items:center; 
  justify-content:center; 
  gap: 8px; 
  padding: 6px 8px; 
  margin-top: 8px; 
  border: 1px solid var(--chat-border); 
  border-radius: 16px; 
  background: #fff; 
  width: fit-content; 
  box-shadow: 0 2px 8px rgba(17,24,39,0.06); 
}

.chat-options {
  position: relative;
  display: flex;
  align-items: center;
}

.chat-options button {
  background: none;
  border: none;
  color: #6b7280;
  font-size: 18px;
  cursor: pointer;
  padding: 8px;
  border-radius: 50%;
  transition: background-color 0.2s;
}

.chat-options button:hover {
  background-color: rgba(0,0,0,0.05);
}

.chat-options-menu {
  position: absolute;
  top: 100%;
  right: 0;
  background: white;
  border: 1px solid #e5e7eb;
  border-radius: 8px;
  box-shadow: 0 4px 12px rgba(0,0,0,0.15);
  min-width: 180px;
  z-index: 1000;
  display: none;
  overflow: hidden;
}

.chat-options-menu.open {
  display: block;
}

.chat-options-menu .item {
  padding: 12px 16px;
  cursor: pointer;
  transition: background-color 0.2s;
  font-size: 14px;
  color: #374151;
}

.chat-options-menu .item:hover {
  background-color: #f3f4f6;
}

/* Hide topbar on desktop by default */
.topbar {
  display: none;
}
.resp-wrap { 
  display: grid; 
  grid-template-columns: 320px 1fr; 
  height: calc(100vh - 40px); 
  gap: 16px; 
  padding: 20px; 
  background: 
    /* Custom SVG pattern for responsable view */
    url('resp-pattern.svg'),
    /* Base background */
    var(--chat-bg);
  background-size: 80px 80px, 100% 100%;
}
.resp-panel { background: var(--chat-panel); border-radius: 16px; box-shadow: 0 6px 24px rgba(0,0,0,0.08); overflow: hidden; display: flex; flex-direction: column; }
.resp-panel .panel-header { padding: 14px; border-bottom: 1px solid var(--chat-border); font-weight: 600; display: flex; justify-content: space-between; align-items: center; }
.resp-search { width: 100%; border: 1px solid var(--chat-border); border-radius: 10px; padding: 8px 10px; font-size: 14px; }
.thread-list { overflow-y: auto; }
.thread-item { padding: 12px 14px; border-bottom: 1px solid var(--chat-border); cursor: pointer; }
.thread-item:hover { background: #f9fafb; }
.thread-item .name { font-weight: 600; }
.thread-item .snippet { font-size: 12px; color: var(--chat-muted); }

.resp-chat { display: flex; flex-direction: column; }
.resp-chat .chat-body { flex: 1; }

/* Mobile WhatsApp-style layout */
@media (max-width: 768px) {
  /* Show topbar only inside first resp-panel on mobile */
  .resp-panel .topbar {
    display: flex;
    align-items: center;
    justify-content: space-between;
    background: #ffffff54;
    color: #000000;
    padding: 12px 16px;
    font-weight: 600;
    position: sticky;
    top: 0;
    z-index: 1001;
    border-bottom: 1px solid #6ec7f0;
  }
  
  .resp-panel .topbar .brand {
    display: flex;
    align-items: center;
    gap: 8px;
    font-size: 18px;
  }
  
  .resp-panel .topbar a {
    color: #1677ff !important;
    text-decoration: none;
    font-size: 14px;
  }
  
  /* Hide topbar in chat panel */
  .resp-chat .topbar {
    display: none !important;
  }
  
  .resp-wrap { 
    display: block;
    height: 100vh;
    padding: 0; 
    gap: 0;
    background: var(--chat-bg);
    overflow: hidden;
    position: fixed;
    top: 0;
    left: 0;
    right: 0;
    bottom: 0;
  }
  
  .resp-panel {
    border-radius: 0;
    box-shadow: none;
    height: 100%;
    overflow: hidden;
  }
  
  /* Chat list panel - visible by default on mobile */
  .resp-panel:first-child {
    display: block;
  }
  
  /* Chat conversation panel - hidden by default on mobile */
  .resp-chat {
    display: none;
    position: absolute;
    top: 0;
    left: 0;
    right: 0;
    bottom: 0;
    z-index: 1000;
    background: var(--chat-panel);
    overflow: hidden;
  }
  
  /* When chat is active, show conversation and hide list */
  .resp-wrap.chat-active {
    position: fixed;
    top: 0;
    height: 100vh;
  }
  
  .resp-wrap.chat-active .resp-panel:first-child {
    display: none;
  }
  
  .resp-wrap.chat-active .resp-chat {
    display: flex;
  }
  
  /* Fix chat footer positioning on mobile */
  .resp-chat .chat-footer {
    position: sticky;
    bottom: 0;
    background: #fff;
    border-top: 1px solid var(--chat-border);
    z-index: 100;
  }
  
  /* Prevent dragging of chat input container on mobile */
  .chat-input-container {
    position: relative;
    user-select: none;
    -webkit-user-select: none;
    -moz-user-select: none;
    -ms-user-select: none;
    touch-action: none;
    pointer-events: auto;
    -webkit-touch-callout: none;
    -webkit-user-drag: none;
    -khtml-user-drag: none;
    -moz-user-drag: none;
    -o-user-drag: none;
    user-drag: none;
  }
  
  /* Allow text selection only in input */
  .chat-input-container .chat-input {
    user-select: text;
    -webkit-user-select: text;
    -moz-user-select: text;
    -ms-user-select: text;
    touch-action: manipulation;
  }
  
  /* Back button for mobile */
  .mobile-back-btn {
    display: inline-flex;
    align-items: center;
    background: none;
    border: none;
    color: var(--chat-primary);
    font-size: 18px;
    padding: 8px;
    margin-right: 8px;
    cursor: pointer;
    border-radius: 50%;
    transition: background-color 0.2s;
  }
  
  .mobile-back-btn:hover {
    background: rgba(22, 119, 255, 0.1);
  }
  
  /* Adjust panel header for mobile */
  .resp-chat .panel-header {
    display: flex;
    align-items: center;
    padding: 12px 16px;
    border-bottom: 1px solid var(--chat-border);
    background: var(--chat-panel);
  }
  
  /* Thread items more touch-friendly */
  .thread-item {
    padding: 16px;
    border-bottom: 1px solid var(--chat-border);
    cursor: pointer;
    transition: background-color 0.2s;
  }
  
  .thread-item:active {
    background: #f0f0f0;
  }
  
  /* Chat options menu positioning fix for mobile */
  .chat-options-menu {
    position: fixed;
    right: 16px;
    top: 60px;
    z-index: 10001;
  }
  
  /* Ensure proper touch targets */
  .chat-options button {
    padding: 12px;
    min-width: 44px;
    min-height: 44px;
    display: flex;
    align-items: center;
    justify-content: center;
  }
}

@media (max-width: 480px) {
  .chat-launcher { right: 16px; bottom: 16px; width: 44px; height: 44px; }
  
  /* Flotante en móvil: margen desde bordes y sombra */
  .chat-modal { 
    right: 12px; 
    bottom: calc(20px + env(safe-area-inset-bottom, 0px)); 
    width: 92vw; 
    max-height: 78vh; 
    border-radius: 16px; 
    z-index: 10001; 
  }
  
  /* Fullscreen ocupa todo cuando se selecciona ampliar */
  .chat-modal.fullscreen { right: 0; transform: none; width: 100vw; max-height: 100vh; border-radius: 0; }
  .chat-header .title { font-size: 15px; }
  .chat-input { font-size: 15px; }
  .message { max-width: 86%; font-size: 14px; }
  
  /* Mobile-specific adjustments */
  .thread-item .name {
    font-size: 16px;
    font-weight: 600;
  }
  
  .thread-item .snippet {
    font-size: 14px;
    margin-top: 4px;
  }
}

@media (min-width: 1024px) {
  .chat-modal { width: 360px; }
  .resp-wrap { grid-template-columns: 320px 1fr; }
}

/* Fullscreen for responsable chat panel */
.resp-chat.fullscreen {
  position: fixed;
  inset: 12px;
  background: var(--chat-panel);
  border-radius: 16px;
  box-shadow: 0 16px 40px rgba(0,0,0,0.24);
  z-index: 10000;
  display: flex;
}

/* Overlay para cerrar al tocar fuera */
.chat-overlay { position: fixed; inset: 0; background: rgba(17,24,39,0.25); backdrop-filter: blur(2px); display: none; z-index: 10000; }
.chat-overlay.open { display: block; }
.emoji-panel { 
  position: absolute;
  bottom: 70px;
  left: 12px;
  right: 12px;
  max-height: 200px;
  overflow-y: auto;
  padding: 12px;
  background: white;
  border: 1px solid #e0e0e0;
  border-radius: 12px;
  box-shadow: 0 4px 12px rgba(0,0,0,0.15);
  z-index: 1000;
}
.emoji-grid { 
  display: grid; 
  grid-template-columns: repeat(8, 1fr); 
  gap: 8px; 
}
.emoji-btn { 
  border: none; 
  background: transparent; 
  border-radius: 6px; 
  padding: 8px; 
  cursor: pointer; 
  font-size: 20px; 
  line-height: 1;
  transition: background 0.2s ease;
  display: flex;
  align-items: center;
  justify-content: center;
  min-height: 36px;
}
.emoji-btn:hover { 
  background: #f0f2f5; 
}

/* Reply functionality styles */
.reply-btn {
  position: absolute;
  top: 8px;
  right: 8px;
  background: rgba(0,0,0,0.1);
  border: none;
  border-radius: 50%;
  width: 24px;
  height: 24px;
  display: none;
  align-items: center;
  justify-content: center;
  color: #666;
  font-size: 12px;
  cursor: pointer;
  transition: all 0.2s ease;
}

.message:hover .reply-btn {
  display: flex;
}

.reply-btn:hover {
  background: rgba(0,0,0,0.2);
  color: #333;
}

.reply-preview {
  background: transparent;
  border-radius: 4px;
  padding: 2px 4px;
  margin-bottom: 4px;
  border-left: 2px solid #25d366;
  font-size: 11px;
  line-height: 1.1;
  display: flex;
  align-items: center;
  gap: 6px;
}
.reply-preview .reply-author {
  font-weight: 600;
  color: #25d366;
  margin: 0;
  font-size: 11px;
}
.reply-preview .reply-text {
  color: #666;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
  max-width: 100%;
}
.reply-container {
  display: none;
  background: #f7f8f9;
  border-radius: 6px;
  margin-bottom: 8px;
  padding: 0;
}

.replying-to {
  background: transparent;
  border-radius: 6px;
  padding: 4px 6px;
  gap: 4px;
  border-left: 2px solid #25d366;
  display: flex;
  align-items: center;
  justify-content: space-between;
}
.reply-info .reply-author { font-size: 11px; margin-bottom: 0; }
.reply-info .reply-text { font-size: 12px; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.cancel-reply {
  background: none;
  border: none;
  color: #666;
  cursor: pointer;
  padding: 4px;
  border-radius: 50%;
  display: flex;
  align-items: center;
  justify-content: center;
  width: 24px;
  height: 24px;
}

.cancel-reply:hover {
  background: rgba(0,0,0,0.1);
}

.message {
  position: relative;
}

.message-content {
  position: relative;
}
.chat-footer .btn.btn-link {
    width: 28px;
    height: 28px;
    border-radius: 50%;
    border: none;
    background: transparent;
    color: #8696a0;
    display: flex;
    align-items: center;
    justify-content: center;
    font-size: 18px;
    transition: all 0.2s ease;
    padding: 0;
}
.chat-footer .btn.btn-link i{ font-size: 18px; }
.chat-footer .btn.btn-link:hover {
    background: rgba(134, 150, 160, 0.1);
    color: #54656f;
}
.message.pending { opacity: 0.85; position: relative; }
.message.pending::after { content:' '; position:absolute; right:12px; bottom:8px; width:12px; height:12px; border:2px solid var(--chat-border); border-top-color:var(--chat-primary); border-radius:50%; animation: spin 0.8s linear infinite; }
@keyframes spin { to { transform: rotate(360deg); } }
.message a { color: var(--chat-primary); text-decoration: none; }
.message a:hover { text-decoration: underline; }
.message img, .message video { display:block; max-width:100%; border-radius:12px; }
.message audio {
  width: 400px;
  max-width: 100%;
  min-width: 220px;
  height: 40px;
  border-radius: 3px;
  margin: 1px 0;
  background: #fff;
  display: block;
  flex-shrink: 0;
}
.message.user audio { 
  filter: brightness(0.98); 
  background: rgba(255,255,255,0.95);
}
.message.responsable audio { 
  background: rgba(0,0,0,0.05); 
}