:root {
  --chat-primary: #2563eb;
  --chat-primary-dark: #1d4ed8;
  --chat-bg: #f8fafc;
  --chat-bubble-user: #2563eb;
  --chat-bubble-ai: #ffffff;
  --chat-shadow: 0 8px 32px rgba(0,0,0,.18);
  --chat-radius: 18px;
}

/* Trigger button */
#chat-trigger-label {
  position: fixed;
  right: 94px;
  bottom: 31px;
  z-index: 9997;
  border: none;
  border-radius: 999px;
  background: #fff;
  color: #1e3a8a;
  box-shadow: 0 6px 22px rgba(15, 23, 42, .16);
  padding: 10px 14px;
  font-size: .88rem;
  font-weight: 700;
  line-height: 1;
  cursor: pointer;
  transition: opacity .18s, transform .18s;
}
#chat-trigger-label:hover {
  transform: translateY(-1px);
}
#chat-trigger-label.hidden {
  opacity: 0;
  pointer-events: none;
  transform: translateY(4px);
}
body.chat-page-mode #chat-trigger,
body.chat-page-mode #chat-trigger-label {
  display: none;
}

#chat-trigger {
  position: fixed;
  bottom: 24px;
  right: 24px;
  z-index: 9998;
  width: 58px;
  height: 58px;
  border-radius: 50%;
  background: var(--chat-primary);
  color: #fff;
  border: none;
  cursor: pointer;
  box-shadow: 0 4px 18px rgba(37,99,235,.45);
  display: flex;
  align-items: center;
  justify-content: center;
  transition: background .2s, transform .15s;
}
#chat-trigger:hover { background: var(--chat-primary-dark); transform: scale(1.07); }
#chat-trigger svg { width: 26px; height: 26px; }
#chat-trigger.has-photo { background: #fff; padding: 0; overflow: hidden; }
#chat-trigger.has-photo:hover { background: #fff; }
#chat-trigger img {
  width: 100%;
  height: 100%;
  border-radius: 50%;
  object-fit: cover;
  display: block;
}

/* Notification badge */
#chat-badge {
  position: absolute;
  top: -3px;
  right: -3px;
  background: #ef4444;
  color: #fff;
  font-size: 11px;
  font-weight: 700;
  width: 18px;
  height: 18px;
  border-radius: 50%;
  display: none;
  align-items: center;
  justify-content: center;
}

/* Chat window */
#chat-window {
  position: fixed;
  bottom: 92px;
  right: 24px;
  z-index: 9999;
  width: 444px;
  max-width: calc(100vw - 32px);
  height: 624px;
  max-height: calc(100vh - 110px);
  background: var(--chat-bg);
  border-radius: var(--chat-radius);
  box-shadow: var(--chat-shadow);
  display: flex;
  flex-direction: column;
  overflow: hidden;
  transform: scale(.92) translateY(12px);
  opacity: 0;
  pointer-events: none;
  transition: transform .22s cubic-bezier(.4,0,.2,1), opacity .22s;
}
#chat-window.open {
  transform: scale(1) translateY(0);
  opacity: 1;
  pointer-events: all;
}

.chat-page-shell {
  flex: 1;
  display: flex;
  flex-direction: column;
  min-height: 0;
  /* center and constrain the chat panel with generous side margins */
  padding: 2rem 2rem 0;
  max-width: 860px;
  width: 100%;
  margin: 0 auto;
  box-sizing: border-box;
}
#chat-window[data-mode="page"] {
  position: relative;
  right: auto;
  bottom: auto;
  width: 100%;
  max-width: 100%;
  flex: 1;
  height: auto;
  max-height: none;
  margin: 0;
  transform: none;
  opacity: 1;
  pointer-events: all;
  border-radius: 12px 12px 0 0;
  box-shadow: 0 -2px 16px rgba(0,0,0,.07);
  overflow: hidden;
}

/* Header */
#chat-header {
  background: var(--chat-primary);
  color: #fff;
  padding: 14px 16px;
  display: flex;
  align-items: center;
  gap: 10px;
  flex-shrink: 0;
}
#chat-header .chat-avatar {
  width: 40px; height: 40px;
  background: rgba(255,255,255,.22);
  border-radius: 50%;
  display: flex; align-items: center; justify-content: center;
  font-size: 18px;
  font-weight: 700;
  overflow: hidden;
  flex-shrink: 0;
}
#chat-header .chat-avatar img {
  width: 100%; height: 100%;
  object-fit: cover;
  border-radius: 50%;
}
#chat-header .chat-title { font-weight: 700; font-size: .95rem; line-height: 1.2; }
#chat-header .chat-sub   { font-size: .75rem; opacity: .82; }
.chat-header-actions {
  margin-left: auto;
  display: flex;
  align-items: center;
  gap: 6px;
}
#chat-expand,
#chat-close {
  background: none;
  border: none;
  color: #fff;
  cursor: pointer;
  opacity: .8;
  padding: 4px;
  display: flex;
  align-items: center;
  justify-content: center;
  text-decoration: none;
}
#chat-expand:hover,
#chat-close:hover { opacity: 1; }
#chat-window[data-mode="page"] #chat-close {
  display: none;
}

/* Page-mode: hide the in-chat header — the site navbar already serves that purpose */
#chat-window[data-mode="page"] #chat-header {
  display: none;
}

/* Messages area */
#chat-messages {
  flex: 1;
  overflow-y: auto;
  padding: 14px 12px 8px;
  display: flex;
  flex-direction: column;
  gap: 10px;
  scroll-behavior: smooth;
}
#chat-messages::-webkit-scrollbar { width: 4px; }
#chat-messages::-webkit-scrollbar-thumb { background: #cbd5e1; border-radius: 4px; }

/* Bubbles */
.chat-msg {
  max-width: 82%;
  padding: 9px 13px;
  border-radius: 14px;
  font-size: .875rem;
  line-height: 1.45;
  word-break: break-word;
  white-space: normal;
}
.chat-msg.ai {
  align-self: flex-start;
  background: var(--chat-bubble-ai);
  color: #1e293b;
  border: 1px solid #e2e8f0;
  border-bottom-left-radius: 4px;
  box-shadow: 0 1px 3px rgba(0,0,0,.06);
}
.chat-msg.user {
  align-self: flex-end;
  background: var(--chat-bubble-user);
  color: #fff;
  border-bottom-right-radius: 4px;
}
.chat-msg ul { margin: 4px 0 2px 16px; padding: 0; }
.chat-msg li { margin-bottom: 2px; }

/* Legacy recommendation card */
.chat-recommendation {
  background: #fff;
  border: 1.5px solid #e2e8f0;
  border-radius: 12px;
  padding: 12px 14px;
  font-size: .83rem;
  color: #1e293b;
  margin: 2px 0;
  box-shadow: 0 2px 8px rgba(0,0,0,.06);
}
.chat-recommendation .rec-title { font-weight: 700; font-size: .9rem; margin-bottom: 6px; color: var(--chat-primary); }
.chat-recommendation .rec-row { display: flex; gap: 6px; margin-bottom: 3px; }
.chat-recommendation .rec-label { font-weight: 600; min-width: 90px; color: #475569; }

/* Proposal card */
.chat-proposal {
  background: #fff;
  border: 1px solid #dbe4f0;
  border-radius: 12px;
  padding: 14px 14px 12px;
  font-size: .8rem;
  color: #1e293b;
  margin: 2px 0;
  box-shadow: none;
}
.chat-proposal .prop-summary {
  display: grid;
  font-size: .82rem;
  gap: 6px;
}
.chat-proposal .prop-summary.plain {
  gap: 4px;
}
.chat-proposal .prop-plain-list {
  margin: 0;
  padding-left: 18px;
}
.chat-proposal .prop-plain-line {
  line-height: 1.4;
  color: #334155;
  margin: 0 0 3px;
}
.chat-proposal .prop-plain-price {
  margin-top: 6px;
  color: #0f4fbf;
  font-weight: 700;
  line-height: 1.4;
}
.chat-proposal .prop-email-cta {
  margin-top: 12px;
  padding-top: 10px;
  border-top: 1px solid #e2e8f0;
}
.chat-proposal .prop-email-cta p {
  margin: 0 0 8px;
  font-size: .83rem;
  color: #374151;
}
.chat-proposal .prop-privacy-label {
  display: flex;
  align-items: flex-start;
  gap: 6px;
  font-size: .75rem;
  color: #64748b;
  margin-bottom: 8px;
  cursor: pointer;
  line-height: 1.4;
}
.chat-proposal .prop-privacy-label input { margin-top: 2px; flex-shrink: 0; }
.chat-proposal .prop-privacy-label a { color: var(--chat-primary); }
.chat-proposal .prop-send-btn {
  width: 100%;
  background: var(--chat-primary);
  color: #fff;
  border: none;
  border-radius: 8px;
  padding: 9px 12px;
  font-size: .83rem;
  font-weight: 600;
  cursor: pointer;
  transition: background .15s;
}
.chat-proposal .prop-send-btn:hover:not(:disabled) { background: var(--chat-primary-dark); }
.chat-proposal .prop-send-btn:disabled { background: #94a3b8; cursor: not-allowed; }
.chat-proposal .prop-send-status { font-size: .76rem; margin-top: 6px; min-height: 16px; }
.chat-proposal .prop-send-status.ok { color: #16a34a; }
.chat-proposal .prop-send-status.error { color: #dc2626; }

@media (max-width: 520px) {
  .chat-proposal {
    padding: 13px 13px 11px;
    border-radius: 11px;
  }
  .chat-proposal .prop-plain-line,
  .chat-proposal .prop-plain-price {
    font-size: .79rem;
  }
}

/* Typing indicator */
.chat-typing {
  align-self: flex-start;
  background: #fff;
  border: 1px solid #e2e8f0;
  border-radius: 14px;
  border-bottom-left-radius: 4px;
  padding: 10px 14px;
  display: flex;
  gap: 5px;
  align-items: center;
}
.chat-typing span {
  width: 7px; height: 7px;
  background: #94a3b8;
  border-radius: 50%;
  animation: chatBounce .9s infinite;
}
.chat-typing span:nth-child(2) { animation-delay: .15s; }
.chat-typing span:nth-child(3) { animation-delay: .3s; }
@keyframes chatBounce {
  0%,80%,100% { transform: translateY(0); }
  40%          { transform: translateY(-6px); }
}

/* Quick-reply buttons */
#chat-buttons {
  padding: 6px 12px;
  display: flex;
  flex-wrap: wrap;
  gap: 6px;
  flex-shrink: 0;
}
.chat-btn {
  background: #eff6ff;
  color: var(--chat-primary);
  border: 1.5px solid #bfdbfe;
  border-radius: 20px;
  padding: 5px 13px;
  font-size: .8rem;
  font-weight: 600;
  cursor: pointer;
  transition: background .15s, color .15s;
  white-space: nowrap;
}
.chat-btn:hover { background: var(--chat-primary); color: #fff; }
.chat-btn.cta {
  background: var(--chat-primary);
  color: #fff;
  border-color: var(--chat-primary);
}
.chat-btn.cta:hover { background: var(--chat-primary-dark); }

/* Mode select buttons (Asistente de Precios / Asesor Virtual) */
.chat-mode-grid {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 8px;
  padding: 4px 2px 2px;
  width: 100%;
}
.chat-mode-btn {
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 5px;
  border: 2px solid #bfdbfe;
  border-radius: 12px;
  padding: 14px 8px;
  background: #eff6ff;
  cursor: pointer;
  transition: all .18s;
  text-align: center;
}
.chat-mode-btn:hover { transform: translateY(-2px); box-shadow: 0 4px 14px rgba(37,99,235,.18); }
.chat-mode-btn.prices { border-color: #2563eb; background: linear-gradient(135deg,#eff6ff,#dbeafe); }
.chat-mode-btn.prices:hover { background: linear-gradient(135deg,#dbeafe,#bfdbfe); }
.chat-mode-btn.advisor { border-color: #7c3aed; background: linear-gradient(135deg,#f5f3ff,#ede9fe); }
.chat-mode-btn.advisor:hover { background: linear-gradient(135deg,#ede9fe,#ddd6fe); }
.chat-mode-btn .mode-icon { font-size: 1.6rem; line-height: 1; }
.chat-mode-btn .mode-title { font-size: .82rem; font-weight: 700; color: #1e293b; line-height: 1.2; }
.chat-mode-btn.advisor .mode-title { color: #4c1d95; }
.chat-mode-btn.prices .mode-title  { color: #1e40af; }
.chat-mode-btn .mode-sub { font-size: .7rem; color: #64748b; line-height: 1.2; }

/* Input area */
#chat-input-area {
  padding: 10px 12px 12px;
  display: flex;
  gap: 8px;
  border-top: 1px solid #e2e8f0;
  flex-shrink: 0;
}
#chat-input {
  flex: 1;
  border: 1.5px solid #e2e8f0;
  border-radius: 22px;
  padding: 8px 14px;
  font-size: .875rem;
  outline: none;
  background: #fff;
  color: #1e293b;
  transition: border-color .15s;
  resize: none;
}
#chat-input:focus { border-color: var(--chat-primary); }
#chat-send {
  width: 38px; height: 38px;
  border-radius: 50%;
  background: var(--chat-primary);
  color: #fff;
  border: none;
  cursor: pointer;
  display: flex; align-items: center; justify-content: center;
  flex-shrink: 0;
  transition: background .15s;
}
#chat-send:hover  { background: var(--chat-primary-dark); }
#chat-send:disabled { background: #94a3b8; cursor: not-allowed; }

/* Login prompt */
#chat-login-prompt {
  flex: 1;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  padding: 24px;
  text-align: center;
  gap: 14px;
  color: #475569;
  font-size: .9rem;
}
#chat-login-prompt svg { color: var(--chat-primary); opacity: .7; }
#chat-login-btn {
  background: var(--chat-primary);
  color: #fff;
  border: none;
  border-radius: 22px;
  padding: 9px 22px;
  font-size: .88rem;
  font-weight: 600;
  cursor: pointer;
  transition: background .15s;
}
#chat-login-btn:hover { background: var(--chat-primary-dark); }

/* ── Tab system ──────────────────────────────────────────────────────────── */
.chat-tab {
  flex: 1;
  display: flex;
  flex-direction: column;
  overflow: hidden;
  min-height: 0;
}

/* ── Progress bar ────────────────────────────────────────────────────────── */
#chat-progress-bar {
  display: none;
  align-items: center;
  gap: 8px;
  padding: 5px 14px 5px;
  flex-shrink: 0;
  background: #fff;
  border-bottom: 1px solid #f1f5f9;
}
#chat-progress-bar .progress-track {
  flex: 1;
  height: 4px;
  border-radius: 4px;
  background: #e2e8f0;
  overflow: hidden;
}
#chat-progress-fill {
  height: 100%;
  border-radius: 4px;
  background: var(--chat-primary);
  transition: width .35s ease;
}
#chat-progress-label {
  font-size: .68rem;
  font-weight: 700;
  color: #94a3b8;
  white-space: nowrap;
  letter-spacing: .03em;
  min-width: 60px;
  text-align: right;
}

/* ── Bottom navigation bar ───────────────────────────────────────────────── */
#chat-bottom-nav {
  display: flex;
  border-top: 1px solid #e2e8f0;
  background: #fff;
  flex-shrink: 0;
  padding: 4px 0 6px;
}
.chat-nav-btn {
  flex: 1;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 3px;
  padding: 7px 6px;
  background: none;
  border: none;
  cursor: pointer;
  color: #94a3b8;
  font-size: .72rem;
  font-weight: 600;
  transition: color .18s;
  position: relative;
}
.chat-nav-btn::after {
  content: '';
  position: absolute;
  bottom: 0;
  left: 25%;
  width: 50%;
  height: 2px;
  border-radius: 2px;
  background: transparent;
  transition: background .18s;
}
.chat-nav-btn:hover { color: var(--chat-primary); }
.chat-nav-btn.active { color: var(--chat-primary); font-weight: 700; }
.chat-nav-btn.active::after { background: var(--chat-primary); }

/* ── Conversations list tab ──────────────────────────────────────────────── */
#tab-convs { overflow-y: auto; padding: 8px 0; }
#conv-list  { display: flex; flex-direction: column; }

.conv-list-item {
  display: flex;
  align-items: center;
  gap: 12px;
  padding: 12px 16px;
  background: #fff;
  border: none;
  border-bottom: 1px solid #f1f5f9;
  cursor: pointer;
  text-align: left;
  width: 100%;
  transition: background .15s;
}
.conv-list-item:hover  { background: #f8fafc; }
.conv-list-item.active { background: #eff6ff; }

.cli-avatar {
  width: 42px; height: 42px;
  border-radius: 50%;
  background: #dbeafe;
  display: flex; align-items: center; justify-content: center;
  font-size: 1.2rem;
  flex-shrink: 0;
}
.conv-list-item.active .cli-avatar { background: #bfdbfe; }

.cli-body  { flex: 1; min-width: 0; }
.cli-label { font-size: .88rem; font-weight: 700; color: #1e293b; margin-bottom: 2px; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.cli-snippet { font-size: .76rem; color: #64748b; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }

.cli-meta   { display: flex; flex-direction: column; align-items: flex-end; gap: 4px; flex-shrink: 0; }
.cli-badge  { font-size: .65rem; font-weight: 700; background: var(--chat-primary); color: #fff; border-radius: 10px; padding: 2px 7px; }
.cli-arrow  { font-size: 1.1rem; color: #cbd5e1; }

/* ── Conversation detail tab ─────────────────────────────────────────────── */
.conv-list-actions {
  padding: 16px;
  background: #fff;
}
.conv-list-new-btn {
  width: 100%;
  border: 1.5px solid #bfdbfe;
  background: #eff6ff;
  color: var(--chat-primary);
  border-radius: 14px;
  padding: 12px 14px;
  font-size: .88rem;
  font-weight: 700;
  cursor: pointer;
  transition: background .15s, border-color .15s, transform .15s;
}
.conv-list-new-btn:hover {
  background: #dbeafe;
  border-color: #93c5fd;
  transform: translateY(-1px);
}

#tab-conv-detail { display: flex; flex-direction: column; overflow: hidden; }

#conv-detail-back-bar {
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 8px 12px;
  background: #f8fafc;
  border-bottom: 1px solid #e2e8f0;
  flex-shrink: 0;
}
#conv-detail-back {
  background: none;
  border: none;
  color: var(--chat-primary);
  font-size: .85rem;
  font-weight: 700;
  cursor: pointer;
  padding: 4px 8px;
  border-radius: 6px;
  white-space: nowrap;
  transition: background .15s;
}
#conv-detail-back:hover { background: #dbeafe; }
#conv-detail-title {
  font-size: .78rem;
  font-weight: 600;
  color: #64748b;
  flex: 1;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}
#conv-detail-messages {
  flex: 1;
  overflow-y: auto;
  padding: 14px 12px;
  display: flex;
  flex-direction: column;
  gap: 10px;
}
#conv-detail-messages::-webkit-scrollbar { width: 4px; }
#conv-detail-messages::-webkit-scrollbar-thumb { background: #cbd5e1; border-radius: 4px; }

#conv-detail-actions {
  padding: 10px 12px 12px;
  display: flex;
  flex-direction: column;
  gap: 8px;
  border-top: 1px solid #e2e8f0;
  flex-shrink: 0;
}
.conv-detail-action-btn {
  display: block;
  width: 100%;
  text-align: center;
  padding: 10px 14px;
  border-radius: 8px;
  font-size: .875rem;
  font-weight: 600;
  text-decoration: none;
  transition: background .15s, opacity .15s;
}
.conv-detail-action-btn.primary {
  background: var(--chat-primary);
  color: #fff;
}
.conv-detail-action-btn.primary:hover { background: var(--chat-primary-dark); }
.conv-detail-action-btn.secondary {
  background: #f1f5f9;
  color: #475569;
  border: 1px solid #e2e8f0;
}
.conv-detail-action-btn.secondary:hover { background: #e2e8f0; }

/* Rating card */
.chat-rating {
  align-self: flex-start;
  background: #fff;
  border: 1.5px solid #e2e8f0;
  border-radius: 14px;
  border-bottom-left-radius: 4px;
  padding: 12px 14px;
  font-size: .85rem;
  color: #374151;
  box-shadow: 0 1px 3px rgba(0,0,0,.06);
  max-width: 82%;
}
.chat-rating p { margin: 0 0 8px; font-weight: 600; }
.chat-rating .chat-stars { display: flex; gap: 2px; }
.chat-rating .chat-star {
  font-size: 1.5rem;
  cursor: pointer;
  color: #d1d5db;
  transition: color .15s, transform .1s;
  line-height: 1;
  display: inline-block;
  width: 1.7rem;
  text-align: center;
  user-select: none;
}
.chat-rating .chat-star:hover,
.chat-rating .chat-star.active { color: #f59e0b; }
.chat-rating .chat-star:hover { transform: scale(1.15); }
.chat-rating .rating-thanks {
  margin-top: 8px;
  font-size: .8rem;
  color: #16a34a;
  font-weight: 600;
  display: none;
}

@media (max-width: 600px) {
  #chat-trigger-label {
    right: 74px;
    bottom: 27px;
    padding: 9px 12px;
    font-size: .82rem;
  }
  #chat-trigger {
    right: 14px;
    bottom: 16px;
    width: 52px;
    height: 52px;
  }
  /* Chat window: anchored to TOP only, height controlled by JS via --vvh.
     Anchoring to top (not bottom) is critical because iOS Safari treats
     position:fixed elements as anchored to the layout viewport, so when
     the keyboard appears, an element with bottom:0 ends up under the
     keyboard. By using top + height = visualViewport.height we guarantee
     the element fits exactly inside the visible area. */
  #chat-window {
    position: fixed;
    top: 0;
    left: 0;
    right: 0;
    bottom: auto;
    width: 100vw;
    max-width: 100vw;
    height: var(--vvh, 100dvh);
    max-height: var(--vvh, 100dvh);
    border-radius: 0;
    box-shadow: none;
    transform: none !important;
    transition: opacity .18s !important;
  }
  #chat-window.open { transform: none !important; }
  #chat-window[data-mode="page"] {
    position: relative;
    top: auto;
    left: auto;
    right: auto;
    bottom: auto;
    width: 100%;
    max-width: 100%;
    flex: 1;
    height: auto;
    max-height: none;
    border-radius: 0;
    box-shadow: none;
    overflow: hidden;
  }
  .chat-page-shell {
    padding: 0;
    max-width: 100%;
  }
  #chat-messages {
    flex: 1 1 0;
    min-height: 0;
    overflow-y: auto;
    -webkit-overflow-scrolling: touch;
  }
  #chat-input-area { flex-shrink: 0; }
  #chat-buttons    { flex-shrink: 0; }
  /* CRITICAL: iOS Safari auto-zooms when focusing inputs with font-size <16px,
     which is what was making the chat "get bigger" requiring pinch-to-fit.
     Force 16px on all chat inputs to disable iOS auto-zoom. */
  #chat-input,
  #chat-email-field,
  #chat-date-picker-wrap select {
    font-size: 16px !important;
  }
}
