* {
  box-sizing: border-box;
  margin: 0;
  padding: 0;
}

html {
  background: #f1f5f9;
}

body {
  font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Arial, sans-serif;
  background: #f1f5f9;
  /* vh first as a fallback for older mobile browsers that don't know
     dvh (older Android WebViews, older Samsung Internet) — dvh then
     overrides it where supported, since it tracks the real visible
     area as the mobile URL bar/keyboard show and hide. */
  height: 100vh;
  height: 100dvh;
  min-height: 100vh;
  min-height: 100dvh;
  display: flex;
  flex-direction: column;
  overflow: hidden;
}

/* On desktop/tablet the share link opens full-viewport by default —
   frame it as a premium floating card instead of stretching edge to edge. */
@media (min-width: 640px) {
  html {
    background: linear-gradient(135deg, #eef2ff, #fff7ed);
  }
  body {
    max-width: 460px;
    height: min(88vh, 720px);
    height: min(88dvh, 720px);
    min-height: 0;
    margin: max(3vh, 1rem) auto;
    border-radius: 22px;
    box-shadow: 0 25px 60px rgba(79, 70, 229, 0.22);
  }
}

/* HEADER */
.chat-header {
  background: linear-gradient(135deg, #2563eb, #3b82f6);
  color: white;
  padding: 0.9rem 1rem;
  display: flex;
  align-items: center;
  gap: 0.75rem;
  flex-shrink: 0;
  box-shadow: 0 2px 10px rgba(37, 99, 235,0.3);
}
.chat-avatar {
  width: 40px;
  height: 40px;
  background: rgba(255,255,255,0.2);
  border-radius: 50%;
  display: flex;
  align-items: center;
  justify-content: center;
  font-weight: 700;
  font-size: 0.85rem;
  flex-shrink: 0;
  border: 2px solid rgba(255,255,255,0.3);
}
.chat-info { flex: 1; min-width: 0; }
.chat-name {
  font-weight: 700;
  font-size: 0.95rem;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}
.chat-business {
  font-size: 0.75rem;
  opacity: 0.8;
  text-transform: capitalize;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}
.chat-status {
  display: flex;
  align-items: center;
  gap: 0.3rem;
  font-size: 0.75rem;
  opacity: 0.9;
  flex-shrink: 0;
}
.status-dot {
  width: 8px;
  height: 8px;
  background: #4ade80;
  border-radius: 50%;
  animation: pulse 2s infinite;
}
@keyframes pulse {
  0%, 100% { opacity: 1; }
  50% { opacity: 0.5; }
}

/* TALK TO HUMAN */
.talk-to-human-btn {
  display: block;
  width: fit-content;
  margin: 0.5rem auto 0;
  background: none;
  border: 1px solid #e2e8f0;
  color: #2563eb;
  font-size: 0.75rem;
  font-weight: 600;
  padding: 0.3rem 0.8rem;
  border-radius: 20px;
  cursor: pointer;
  flex-shrink: 0;
}
.talk-to-human-btn:hover { background: #eef2ff; }
.talk-to-human-btn:disabled { opacity: 0.5; cursor: default; }
.talk-to-human-btn:disabled:hover { background: none; }

.handoff-banner {
  text-align: center;
  font-size: 0.75rem;
  color: #92400e;
  background: #fef3c7;
  padding: 0.4rem;
  flex-shrink: 0;
}

.handoff-offer-row {
  display: flex;
  justify-content: flex-start;
  margin-left: 2.25rem;
}
.handoff-offer-btn {
  background: #eef2ff;
  border: 1px solid #93c5fd;
  color: #1d4ed8;
  font-size: 0.75rem;
  font-weight: 600;
  padding: 0.4rem 0.8rem;
  border-radius: 14px;
  cursor: pointer;
}
.handoff-offer-btn:hover { background: #dbeafe; }

.msg-row.staff .msg-bubble {
  background: #fef3c7;
  color: #92400e;
  border-bottom-left-radius: 4px;
}
.msg-row.staff .msg-avatar {
  background: linear-gradient(135deg, #f59e0b, #ea580c);
}

/* DISCLAIMER */
.chat-disclaimer {
  background: #fefce8;
  border-bottom: 1px solid #fde68a;
  padding: 0.5rem 1rem;
  font-size: 0.75rem;
  color: #92400e;
  text-align: center;
  flex-shrink: 0;
  line-height: 1.4;
}

/* MESSAGES */
.chat-messages {
  flex: 1;
  min-height: 0;
  overflow-y: auto;
  padding: 1rem;
  display: flex;
  flex-direction: column;
  gap: 0.75rem;
  scroll-behavior: smooth;
}
.chat-messages::-webkit-scrollbar { width: 4px; }
.chat-messages::-webkit-scrollbar-thumb {
  background: #cbd5e1;
  border-radius: 2px;
}

/* MESSAGE BUBBLES */
.msg-row {
  display: flex;
  align-items: flex-end;
  gap: 0.5rem;
  animation: msgIn 0.25s ease;
}
@keyframes msgIn {
  from { opacity: 0; transform: translateY(6px); }
  to { opacity: 1; transform: translateY(0); }
}
.msg-row.user { justify-content: flex-end; }
.msg-row.bot { justify-content: flex-start; }

.msg-avatar {
  width: 28px;
  height: 28px;
  background: linear-gradient(135deg, #2563eb, #3b82f6);
  border-radius: 50%;
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 0.65rem;
  font-weight: 700;
  color: white;
  flex-shrink: 0;
}

.msg-bubble {
  max-width: 75%;
  padding: 0.65rem 1rem;
  border-radius: 18px;
  font-size: 0.9rem;
  line-height: 1.5;
  word-wrap: break-word;
}
.msg-row.user .msg-bubble {
  background: linear-gradient(135deg, #2563eb, #3b82f6);
  color: white;
  border-bottom-right-radius: 4px;
}
.msg-row.bot .msg-bubble {
  background: white;
  color: #1e293b;
  border-bottom-left-radius: 4px;
  box-shadow: 0 1px 4px rgba(0,0,0,0.08);
}

/* TYPING INDICATOR */
.typing-indicator {
  display: flex;
  gap: 4px;
  padding: 0.65rem 1rem;
  background: white;
  border-radius: 18px;
  border-bottom-left-radius: 4px;
  box-shadow: 0 1px 4px rgba(0,0,0,0.08);
  width: fit-content;
}
.typing-dot {
  width: 7px;
  height: 7px;
  background: #94a3b8;
  border-radius: 50%;
  animation: typing 1.2s infinite;
}
.typing-dot:nth-child(2) { animation-delay: 0.2s; }
.typing-dot:nth-child(3) { animation-delay: 0.4s; }
@keyframes typing {
  0%, 100% { transform: translateY(0); opacity: 0.4; }
  50% { transform: translateY(-4px); opacity: 1; }
}

/* INPUT BAR */
.chat-input-bar {
  background: white;
  border-top: 1px solid #e2e8f0;
  padding: 0.75rem 1rem;
  display: flex;
  gap: 0.6rem;
  align-items: flex-end;
  flex-shrink: 0;
}
.chat-input {
  flex: 1;
  border: 1.5px solid #e2e8f0;
  border-radius: 22px;
  padding: 0.6rem 1rem;
  font-size: 0.9rem;
  outline: none;
  transition: border-color 0.2s;
  font-family: inherit;
  background: #f8faff;
  resize: none;
  line-height: 1.4;
  max-height: 6.5em;
  overflow-y: auto;
}
.chat-input:focus {
  border-color: #2563eb;
  background: white;
}
.chat-send-btn {
  width: 40px;
  height: 40px;
  background: linear-gradient(135deg, #2563eb, #3b82f6);
  color: white;
  border: none;
  border-radius: 50%;
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 1rem;
  flex-shrink: 0;
  transition: all 0.2s;
  box-shadow: 0 2px 8px rgba(37, 99, 235,0.3);
}
.chat-mic-btn {
  width: 40px;
  height: 40px;
  background: #f1f5f9;
  color: #2563eb;
  border: 1.5px solid #e2e8f0;
  border-radius: 50%;
  cursor: pointer;
  align-items: center;
  justify-content: center;
  font-size: 1rem;
  flex-shrink: 0;
  transition: all 0.2s;
}
.chat-mic-btn.listening {
  background: #fee2e2;
  color: #dc2626;
  border-color: #fecaca;
}

.chat-attach-btn {
  width: 40px;
  height: 40px;
  background: #f1f5f9;
  color: #2563eb;
  border: 1.5px solid #e2e8f0;
  border-radius: 50%;
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 1rem;
  flex-shrink: 0;
  transition: all 0.2s;
}
.chat-attach-btn:disabled {
  opacity: 0.5;
  cursor: not-allowed;
}

.chat-send-btn:hover {
  transform: scale(1.05);
  box-shadow: 0 4px 12px rgba(37, 99, 235,0.4);
}
.chat-send-btn:disabled {
  background: #cbd5e1;
  box-shadow: none;
  cursor: not-allowed;
  transform: none;
}

/* POWERED BY */
.chat-powered {
  text-align: center;
  font-size: 0.68rem;
  color: #94a3b8;
  padding: 0.3rem;
  background: white;
  flex-shrink: 0;
}
.chat-powered a {
  color: #2563eb;
  text-decoration: none;
  font-weight: 600;
}

/* Mobile keyboard fix */
@media (max-height: 600px) {
  .chat-disclaimer { display: none; }
}
@media (max-width: 600px) {
  .chat-input-bar {
    padding-bottom: calc(0.75rem + env(safe-area-inset-bottom));
  }
}

/* Reply rating (thumbs under AI replies) */
.rate-row {
  display: flex;
  gap: 6px;
  margin: -2px 0 2px 38px;
}
.rate-btn {
  background: none;
  border: 1px solid #e2e8f0;
  border-radius: 12px;
  font-size: 12px;
  line-height: 1;
  padding: 4px 9px;
  cursor: pointer;
  opacity: 0.65;
  transition: opacity 0.15s, background 0.15s;
}
.rate-btn:hover { opacity: 1; background: #f8faff; }
.rate-btn.chosen {
  opacity: 1;
  background: #eef2ff;
  border-color: #2563eb;
}
.rate-btn:disabled { cursor: default; }
.rate-btn:disabled:not(.chosen) { opacity: 0.3; }

/* End-of-chat resolution feedback card */
.fb-card {
  background: #f8faff;
  border: 1px solid #e2e8f0;
  border-radius: 12px;
  padding: 12px 14px;
  text-align: center;
  margin: 4px 0;
}
.fb-question {
  font-size: 0.85rem;
  color: #334155;
  font-weight: 600;
  margin-bottom: 10px;
}
.fb-btn-row {
  display: flex;
  gap: 10px;
  justify-content: center;
}
.fb-btn {
  font-size: 0.8rem;
  font-weight: 600;
  padding: 6px 18px;
  border-radius: 14px;
  cursor: pointer;
}
.fb-yes { background: #dcfce7; border: 1px solid #bbf7d0; color: #15803d; }
.fb-no { background: #fee2e2; border: 1px solid #fecaca; color: #991b1b; }
