/* ============================================
   LiveChat Widget - Tidio-Style Light Theme
   ============================================ */

/* Inter, self-hosted alongside this stylesheet. A Google Fonts @import is
   refused by any host page with a strict style-src, which left the widget on
   fallback system fonts. Relative URLs resolve against /widget/ on every embed. */
@font-face {
  font-family: 'Inter';
  font-style: normal;
  font-weight: 400 700;
  font-display: swap;
  src: url('./fonts/inter-latin.woff2') format('woff2');
  unicode-range: U+0000-00FF, U+0131, U+0152-0153, U+02BB-02BC, U+02C6, U+02DA, U+02DC, U+0304, U+0308, U+0329, U+2000-206F, U+20AC, U+2122, U+2191, U+2193, U+2212, U+2215, U+FEFF, U+FFFD;
}

@font-face {
  font-family: 'Inter';
  font-style: normal;
  font-weight: 400 700;
  font-display: swap;
  src: url('./fonts/inter-latin-ext.woff2') format('woff2');
  unicode-range: U+0100-02BA, U+02BD-02C5, U+02C7-02CC, U+02CE-02D7, U+02DD-02FF, U+0304, U+0308, U+0329, U+1D00-1DBF, U+1E00-1E9F, U+1EF2-1EFF, U+2020, U+20A0-20AB, U+20AD-20C0, U+2113, U+2C60-2C7F, U+A720-A7FF;
}

/* ============================================
   CSS Custom Properties (Design Tokens)
   ============================================ */
:root {
  /* Primary Colors - Set dynamically by JS from backend settings */
  --livechat-primary: #2d3748;
  --livechat-primary-hover: #1a202c;
  --livechat-primary-light: #4a5568;
  
  /* Light Theme Colors */
  --livechat-bg-white: #ffffff;
  --livechat-bg-light: #f7f8fa;
  --livechat-bg-card: #ffffff;
  --livechat-bg-input: #ffffff;
  --livechat-bg-hover: #f3f4f6;
  --livechat-bg-message-agent: #f3f4f6;
  
  /* Text Colors - Dark for light theme */
  --livechat-text-primary: #1a202c;
  --livechat-text-secondary: #4a5568;
  --livechat-text-tertiary: #718096;
  --livechat-text-muted: #a0aec0;
  --livechat-text-placeholder: #cbd5e0;
  --livechat-text-white: #ffffff;
  
  /* Border Colors */
  --livechat-border-light: #e2e8f0;
  --livechat-border-default: #cbd5e0;
  --livechat-border-focus: #a0aec0;
  
  /* Semantic Colors */
  --livechat-success: #48bb78;
  --livechat-error: #f56565;
  --livechat-warning: #ed8936;
  
  /* Typography */
  --livechat-font-family: 'Inter', -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, sans-serif;
  --livechat-font-size-xs: 11px;
  --livechat-font-size-sm: 12px;
  --livechat-font-size-base: 14px;
  --livechat-font-size-md: 15px;
  --livechat-font-size-lg: 16px;
  --livechat-font-size-xl: 18px;
  
  /* Spacing Scale */
  --livechat-space-1: 4px;
  --livechat-space-2: 8px;
  --livechat-space-3: 12px;
  --livechat-space-4: 16px;
  --livechat-space-5: 20px;
  --livechat-space-6: 24px;
  --livechat-space-8: 32px;
  
  /* Border Radius */
  --livechat-radius-sm: 6px;
  --livechat-radius-md: 8px;
  --livechat-radius-lg: 12px;
  --livechat-radius-xl: 16px;
  --livechat-radius-2xl: 20px;
  --livechat-radius-full: 9999px;
  
  /* Shadows */
  --livechat-shadow-sm: 0 1px 2px rgba(0, 0, 0, 0.05);
  --livechat-shadow-md: 0 4px 6px rgba(0, 0, 0, 0.07), 0 2px 4px rgba(0, 0, 0, 0.05);
  --livechat-shadow-lg: 0 10px 25px rgba(0, 0, 0, 0.1), 0 6px 10px rgba(0, 0, 0, 0.08);
  --livechat-shadow-xl: 0 20px 40px rgba(0, 0, 0, 0.12), 0 8px 16px rgba(0, 0, 0, 0.08);
  --livechat-shadow-card: 0 2px 8px rgba(0, 0, 0, 0.08), 0 1px 2px rgba(0, 0, 0, 0.04);
  
  /* Transitions */
  --livechat-transition-fast: 150ms ease;
  --livechat-transition-base: 200ms ease;
  --livechat-transition-slow: 300ms ease;
  
  /* Z-index */
  --livechat-z-widget: 999998;
  --livechat-z-button: 999999;
}

/* ============================================
   Scoped Reset - Only affects widget elements
   ============================================ */
#livechat-widget,
#livechat-widget * {
  box-sizing: border-box;
}

#livechat-widget {
  font-family: var(--livechat-font-family);
  font-size: var(--livechat-font-size-base);
  font-weight: 400;
  line-height: 1.5;
  color: var(--livechat-text-primary);
  -webkit-font-smoothing: antialiased;
  -moz-osx-font-smoothing: grayscale;
  position: fixed;
  z-index: var(--livechat-z-widget);
}

/* Remove default browser focus outlines */
#livechat-widget input:focus,
#livechat-widget textarea:focus,
#livechat-widget button:focus {
  outline: none;
}

/* ============================================
   Chat Launcher Button
   ============================================ */
.livechat-button {
  position: fixed;
  bottom: 24px;
  width: 60px;
  height: 60px;
  border-radius: var(--livechat-radius-full);
  background: var(--livechat-primary);
  border: none;
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: center;
  box-shadow: var(--livechat-shadow-lg);
  transition: transform var(--livechat-transition-base), 
              box-shadow var(--livechat-transition-base),
              background var(--livechat-transition-base);
  z-index: var(--livechat-z-button);
  outline: none;
}

.livechat-button.right {
  right: 24px;
}

.livechat-button.left {
  left: 24px;
}

.livechat-button:hover {
  transform: scale(1.05);
  box-shadow: var(--livechat-shadow-xl);
  background: var(--livechat-primary-hover);
}

.livechat-button:active {
  transform: scale(0.95);
}

.livechat-button.active {
  transform: scale(0.95);
}

.livechat-button svg {
  color: var(--livechat-text-white);
  width: 26px;
  height: 26px;
}

/* Unread Badge */
.livechat-unread {
  position: absolute;
  top: -4px;
  right: -4px;
  min-width: 22px;
  height: 22px;
  padding: 0 6px;
  border-radius: var(--livechat-radius-full);
  background: var(--livechat-error);
  color: var(--livechat-text-white);
  font-size: var(--livechat-font-size-xs);
  font-weight: 600;
  display: flex;
  align-items: center;
  justify-content: center;
  border: 2px solid var(--livechat-bg-white);
  animation: unreadPulse 2s ease-in-out infinite;
}

@keyframes unreadPulse {
  0%, 100% { transform: scale(1); }
  50% { transform: scale(1.1); }
}

/* ============================================
   Chat Window Container
   ============================================ */
.livechat-window {
  position: fixed;
  bottom: 24px;
  width: 380px;
  height: 750px;
  max-height: calc(100vh - 48px);
  min-height: min(680px, calc(100vh - 48px));
  background: var(--livechat-bg-white);
  border-radius: var(--livechat-radius-2xl);
  box-shadow: var(--livechat-shadow-xl);
  display: none;
  flex-direction: column;
  overflow: hidden;
  z-index: var(--livechat-z-widget);
  animation: windowSlideIn var(--livechat-transition-slow) forwards;
}

.livechat-window.right {
  right: 24px;
}

.livechat-window.left {
  left: 24px;
}

@keyframes windowSlideIn {
  from {
    opacity: 0;
    transform: translateY(20px) scale(0.95);
  }
  to {
    opacity: 1;
    transform: translateY(0) scale(1);
  }
}

/* ============================================
   Curved Header (Tidio Style)
   ============================================ */
.livechat-header {
  padding: 20px 20px 30px 20px;
  background: var(--livechat-primary);
  color: var(--livechat-text-white);
  display: flex;
  align-items: flex-start;
  justify-content: flex-start;
  /* Keeps a long title off the header buttons; it ellipsises instead */
  gap: var(--livechat-space-4);
  position: relative;
  flex-shrink: 0;
  /* The signature curved bottom */
  border-radius: 0 0 50% 50% / 0 0 24px 24px;
}

.livechat-header-content {
  display: flex;
  align-items: center;
  gap: 12px;
  flex: 0 1 auto;
  min-width: 0;
}

/* Return to Home. Living in the header instead of a persistent bottom tab bar
   leaves those rows to the transcript. Sits closer to the window edge than the
   header padding so it reads as a corner control without pushing the title. */
.livechat-back-btn {
  background: transparent;
  border: none;
  margin-left: -4px;
  padding: 0;
  width: 32px;
  height: 32px;
  border-radius: var(--livechat-radius-full);
  display: flex;
  align-items: center;
  justify-content: center;
  cursor: pointer;
  color: var(--livechat-text-white);
  transition: background var(--livechat-transition-fast);
  flex-shrink: 0;
}

.livechat-back-btn:hover {
  background: rgba(255, 255, 255, 0.18);
}

.livechat-back-btn svg {
  width: 20px;
  height: 20px;
}

.livechat-avatar-header {
  width: 44px;
  height: 44px;
  border-radius: var(--livechat-radius-full);
  background: var(--livechat-bg-white);
  display: flex;
  align-items: center;
  justify-content: center;
  flex-shrink: 0;
  overflow: hidden;
  border: 2px solid rgba(255, 255, 255, 0.3);
}

/* The back button and the title share a 380px header, so the chat view runs a
   tighter avatar than Home, where nothing competes with the greeting */
.livechat-has-back .livechat-avatar-header {
  width: 32px;
  height: 32px;
}

.livechat-avatar-header > img,
.livechat-avatar-image {
  width: 100%;
  height: 100%;
  object-fit: cover;
  border-radius: var(--livechat-radius-full);
}

.livechat-avatar-header.livechat-avatar-header-agents {
  width: auto;
  min-width: 44px;
  background: transparent;
  border: 0;
  overflow: visible;
}

.livechat-agent-avatar-stack {
  height: 44px;
  display: flex;
  align-items: center;
  padding-left: 1px;
}

.livechat-agent-avatar-image,
.livechat-agent-avatar-initials {
  width: 42px;
  height: 42px;
  flex: 0 0 42px;
  margin-left: -12px;
  border: 2px solid var(--livechat-bg-white);
  border-radius: var(--livechat-radius-full);
  box-shadow: 0 2px 7px rgba(0, 0, 0, 0.18);
}

.livechat-agent-avatar-image:first-child,
.livechat-agent-avatar-initials:first-child {
  margin-left: 0;
}

.livechat-agent-avatar-image {
  object-fit: cover;
  background: var(--livechat-bg-white);
}

.livechat-agent-avatar-initials {
  display: flex;
  align-items: center;
  justify-content: center;
  background: #edf2f7;
  color: var(--livechat-primary);
  font-size: 13px;
  font-weight: 700;
  letter-spacing: -0.02em;
}

.livechat-agent-avatar-initials:nth-child(2) {
  background: #e6fffa;
}

.livechat-agent-avatar-initials:nth-child(3) {
  background: #fff5f5;
}

.livechat-avatar-header svg {
  color: var(--livechat-primary);
  width: 22px;
  height: 22px;
}

.livechat-header-text {
  flex: 0 1 auto;
  min-width: 0;
  display: flex;
  flex-direction: column;
  align-items: flex-start;
  justify-content: center;
  text-align: left;
}

.livechat-title {
  /* The column parent sizes to content, so without this the title grows past
     it and runs under the header buttons instead of ellipsising */
  max-width: 100%;
  font-size: var(--livechat-font-size-lg);
  font-weight: 600;
  color: var(--livechat-text-white);
  line-height: 1.4;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
  text-align: left;
}

.livechat-subtitle {
  display: flex;
  align-items: center;
  gap: 6px;
  font-size: var(--livechat-font-size-sm);
  color: rgba(255, 255, 255, 0.85);
  margin-top: 2px;
}

.livechat-status-indicator {
  width: 10px;
  height: 10px;
  border-radius: var(--livechat-radius-full);
  background: var(--livechat-success);
  display: inline-block;
  box-shadow: 0 0 0 2px rgba(72, 187, 120, 0.3);
  flex-shrink: 0;
}

.livechat-status-indicator.offline {
  background: var(--livechat-text-muted);
  box-shadow: none;
}

.livechat-header-actions {
  display: flex;
  gap: 4px;
  flex-shrink: 0;
  margin-left: auto;
}

.livechat-header-actions button {
  background: rgba(255, 255, 255, 0.15);
  border: none;
  width: 32px;
  height: 32px;
  border-radius: var(--livechat-radius-sm);
  display: flex;
  align-items: center;
  justify-content: center;
  cursor: pointer;
  color: var(--livechat-text-white);
  transition: background var(--livechat-transition-fast);
}

.livechat-header-actions button:hover {
  background: rgba(255, 255, 255, 0.25);
}

.livechat-header-actions button svg {
  width: 16px;
  height: 16px;
}

/* ============================================
   Home Tab
   ============================================ */
.livechat-home {
  flex: 1;
  display: flex;
  flex-direction: column;
  gap: var(--livechat-space-4);
  padding: var(--livechat-space-5);
  background: var(--livechat-bg-light);
  overflow-y: auto;
  min-height: 0;
}

/* Card list reads as one grouped panel, divided rather than spaced */
.livechat-home-cards {
  background: var(--livechat-bg-card);
  border: 1px solid var(--livechat-border-light);
  border-radius: var(--livechat-radius-lg);
  box-shadow: var(--livechat-shadow-card);
  overflow: hidden;
}

.livechat-home-cards:empty {
  display: none;
}

.livechat-home-card {
  width: 100%;
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: var(--livechat-space-3);
  padding: var(--livechat-space-4);
  background: transparent;
  border: none;
  border-top: 1px solid var(--livechat-border-light);
  font-family: var(--livechat-font-family);
  font-size: var(--livechat-font-size-md);
  font-weight: 500;
  color: var(--livechat-text-primary);
  text-align: left;
  cursor: pointer;
  transition: background var(--livechat-transition-fast);
}

.livechat-home-card:first-child {
  border-top: none;
}

.livechat-home-card:hover {
  background: var(--livechat-bg-hover);
}

.livechat-home-card svg {
  width: 18px;
  height: 18px;
  flex-shrink: 0;
  color: var(--livechat-text-muted);
}

.livechat-home-cta {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: var(--livechat-space-3);
  padding: var(--livechat-space-4);
  background: var(--livechat-bg-card);
  border: 1px solid var(--livechat-border-light);
  border-radius: var(--livechat-radius-lg);
  box-shadow: var(--livechat-shadow-card);
  font-family: var(--livechat-font-family);
  text-align: left;
  cursor: pointer;
  transition: background var(--livechat-transition-fast);
}

.livechat-home-cta:hover {
  background: var(--livechat-bg-hover);
}

.livechat-home-cta-title {
  font-size: var(--livechat-font-size-md);
  font-weight: 600;
  color: var(--livechat-text-primary);
}

.livechat-home-cta-subtitle {
  margin-top: 2px;
  font-size: var(--livechat-font-size-sm);
  color: var(--livechat-text-tertiary);
}

.livechat-home-cta svg {
  width: 20px;
  height: 20px;
  flex-shrink: 0;
  color: var(--livechat-primary);
}

/* ============================================
   Tab Bar (Home only)
   ============================================ */
.livechat-tabs {
  display: flex;
  flex-shrink: 0;
  border-top: 1px solid var(--livechat-border-light);
  background: var(--livechat-bg-white);
}

.livechat-tab {
  flex: 1;
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: var(--livechat-space-1);
  padding: var(--livechat-space-3) var(--livechat-space-2);
  background: transparent;
  border: none;
  font-family: var(--livechat-font-family);
  font-size: var(--livechat-font-size-sm);
  font-weight: 500;
  color: var(--livechat-text-tertiary);
  cursor: pointer;
  transition: color var(--livechat-transition-fast);
}

.livechat-tab svg {
  width: 20px;
  height: 20px;
}

.livechat-tab:hover {
  color: var(--livechat-text-secondary);
}

.livechat-tab.active {
  color: var(--livechat-primary);
}

/* Safe area only matters where the bar meets the bottom of the screen */
@media (max-width: 768px) {
  .livechat-tabs {
    padding-bottom: max(0px, env(safe-area-inset-bottom, 0px));
  }
}

/* ============================================
   Pre-Chat Form (Floating Card Style)
   ============================================ */
.livechat-prechat {
  flex: 1;
  display: flex;
  flex-direction: column;
  padding: 20px;
  background: var(--livechat-bg-light);
  overflow-y: auto;
  min-height: 0;
}

.livechat-prechat-card {
  background: var(--livechat-bg-card);
  border-radius: var(--livechat-radius-lg);
  padding: 24px;
  box-shadow: var(--livechat-shadow-card);
  border: 1px solid var(--livechat-border-light);
  width: 100%;
}

.livechat-prechat-title {
  font-size: var(--livechat-font-size-md);
  font-weight: 600;
  color: var(--livechat-text-primary);
  margin: 0 0 20px 0;
  text-align: center;
}

.livechat-prechat-form {
  display: flex;
  flex-direction: column;
  gap: 16px;
  width: 100%;
}

.livechat-form-field {
  display: flex;
  flex-direction: column;
  gap: 6px;
  width: 100%;
}

.livechat-form-field label {
  font-size: var(--livechat-font-size-sm);
  font-weight: 500;
  color: var(--livechat-text-secondary);
  margin: 0;
}

.livechat-input-field {
  display: block;
  width: 100%;
  height: 48px;
  padding: 12px 16px;
  margin: 0;
  border: 1px solid var(--livechat-border-light);
  border-radius: var(--livechat-radius-md);
  font-family: var(--livechat-font-family);
  font-size: var(--livechat-font-size-base);
  line-height: 1.5;
  color: var(--livechat-text-primary);
  background-color: var(--livechat-bg-input);
  transition: border-color var(--livechat-transition-fast),
              box-shadow var(--livechat-transition-fast);
  outline: none;
  -webkit-appearance: none;
  appearance: none;
}

.livechat-input-field::placeholder {
  color: var(--livechat-text-placeholder);
}

.livechat-input-field:hover {
  border-color: var(--livechat-border-default);
}

.livechat-input-field:focus {
  border-color: var(--livechat-primary);
  outline: none;
  box-shadow: none;
}

.livechat-btn-primary {
  display: block;
  width: 100%;
  height: 48px;
  padding: 12px 24px;
  margin: 8px 0 0 0;
  background-color: var(--livechat-primary);
  color: var(--livechat-text-white);
  border: none;
  border-radius: var(--livechat-radius-full);
  font-family: var(--livechat-font-family);
  font-size: var(--livechat-font-size-base);
  font-weight: 600;
  line-height: 1.5;
  text-align: center;
  cursor: pointer;
  transition: background-color var(--livechat-transition-fast),
              transform var(--livechat-transition-fast);
  -webkit-appearance: none;
  appearance: none;
}

.livechat-btn-primary:hover {
  background: var(--livechat-primary-hover);
  transform: translateY(-1px);
}

.livechat-btn-primary:active {
  transform: translateY(0);
}

/* ============================================
   Offline State
   ============================================ */
.livechat-offline {
  flex: 1;
  display: none;
  flex-direction: column;
  padding: 20px;
  background: var(--livechat-bg-light);
  overflow-y: auto;
}

.livechat-offline-card {
  background: var(--livechat-bg-card);
  border-radius: var(--livechat-radius-lg);
  padding: 24px;
  box-shadow: var(--livechat-shadow-card);
  border: 1px solid var(--livechat-border-light);
  text-align: center;
}

.livechat-offline-icon {
  width: 48px;
  height: 48px;
  margin: 0 auto 16px;
  color: var(--livechat-text-muted);
}

.livechat-offline-title {
  font-size: var(--livechat-font-size-lg);
  font-weight: 600;
  color: var(--livechat-text-primary);
  margin-bottom: 8px;
}

.livechat-offline-message {
  font-size: var(--livechat-font-size-base);
  color: var(--livechat-text-secondary);
  line-height: 1.5;
  margin-bottom: 20px;
}

/* ============================================
   Chat Body & Messages
   ============================================ */
.livechat-body {
  flex: 1;
  display: flex;
  flex-direction: column;
  min-height: 0;
  overflow: hidden;
  background: var(--livechat-bg-light);
}

.livechat-messages {
  flex: 1;
  overflow-y: auto;
  overflow-x: hidden;
  padding: 20px;
  display: flex;
  flex-direction: column;
  gap: 16px;
  background: var(--livechat-bg-light);
  min-height: 0;
}

/* Separates a previous conversation from a newly started one */
.livechat-divider {
  display: flex;
  align-items: center;
  gap: 12px;
  margin: 4px 0;
  color: var(--livechat-text-muted, #8a8f98);
  font-size: 12px;
  animation: messageSlideIn var(--livechat-transition-base) ease-out;
}

.livechat-divider::before,
.livechat-divider::after {
  content: '';
  flex: 1;
  height: 1px;
  background: var(--livechat-border-light);
}

.livechat-divider span {
  white-space: nowrap;
}

/* Message Bubbles */
.livechat-message {
  display: flex;
  flex-direction: column;
  max-width: 80%;
  animation: messageSlideIn var(--livechat-transition-base) ease-out;
}

@keyframes messageSlideIn {
  from {
    opacity: 0;
    transform: translateY(8px);
  }
  to {
    opacity: 1;
    transform: translateY(0);
  }
}

.livechat-message-sender {
  font-size: var(--livechat-font-size-xs);
  font-weight: 500;
  color: var(--livechat-text-tertiary);
  margin-bottom: 4px;
  padding: 0 4px;
}

.livechat-message-content {
  padding: 12px 16px;
  border-radius: var(--livechat-radius-xl);
  font-size: var(--livechat-font-size-base);
  line-height: 1.5;
  word-wrap: break-word;
  word-break: break-word;
  overflow-wrap: break-word;
  text-align: left;
}

.livechat-message-content p,
.livechat-system-content p {
  margin: 0;
}

.livechat-message-content p + p,
.livechat-system-content p + p {
  margin-top: 12px;
}

.livechat-message-content ul,
.livechat-message-content ol,
.livechat-system-content ul,
.livechat-system-content ol {
  margin: 8px 0;
  padding-left: 20px;
}

.livechat-message-content li + li,
.livechat-system-content li + li {
  margin-top: 4px;
}

.livechat-message-content h3,
.livechat-message-content h4,
.livechat-message-content h5,
.livechat-message-content h6,
.livechat-system-content h3,
.livechat-system-content h4,
.livechat-system-content h5,
.livechat-system-content h6 {
  margin: 10px 0 6px;
  font-size: 1em;
  line-height: 1.35;
}

.livechat-message-content h3:first-child,
.livechat-message-content h4:first-child,
.livechat-message-content h5:first-child,
.livechat-message-content h6:first-child,
.livechat-system-content h3:first-child,
.livechat-system-content h4:first-child,
.livechat-system-content h5:first-child,
.livechat-system-content h6:first-child {
  margin-top: 0;
}

.livechat-message-content blockquote,
.livechat-system-content blockquote {
  margin: 8px 0;
  padding-left: 10px;
  border-left: 3px solid var(--livechat-border-default);
  color: var(--livechat-text-secondary);
}

.livechat-message-content code,
.livechat-system-content code {
  padding: 1px 4px;
  border-radius: 4px;
  background: rgba(0, 0, 0, 0.06);
  font-family: ui-monospace, SFMono-Regular, Menlo, Monaco, Consolas, monospace;
  font-size: 0.9em;
}

.livechat-message-content pre,
.livechat-system-content pre {
  margin: 8px 0;
  padding: 10px;
  overflow-x: auto;
  border-radius: var(--livechat-radius-md);
  background: rgba(0, 0, 0, 0.06);
  white-space: pre-wrap;
}

.livechat-message-content pre code,
.livechat-system-content pre code {
  padding: 0;
  background: transparent;
}

/* Agent messages - Left aligned, light gray bubble */
.livechat-message.agent {
  align-self: flex-start;
}

.livechat-message.agent .livechat-message-content {
  background: var(--livechat-bg-message-agent);
  color: var(--livechat-text-primary);
  border-bottom-left-radius: var(--livechat-radius-sm);
}

/* Visitor messages - Right aligned, primary color bubble */
.livechat-message.visitor {
  align-self: flex-end;
}

.livechat-message.visitor .livechat-message-sender {
  text-align: right;
}

.livechat-message.visitor .livechat-message-content {
  background: var(--livechat-primary);
  color: var(--livechat-text-white);
  border-bottom-right-radius: var(--livechat-radius-sm);
}

/* System messages */
.livechat-message.system {
  align-self: center;
  max-width: 90%;
}

.livechat-message.system span,
.livechat-message.system .livechat-system-content {
  display: inline-block;
  padding: 8px 16px;
  background: var(--livechat-bg-white);
  border: 1px solid var(--livechat-border-light);
  border-radius: var(--livechat-radius-full);
  color: var(--livechat-text-tertiary);
  font-size: var(--livechat-font-size-sm);
}

/* Message timestamp */
.livechat-message-time {
  font-size: var(--livechat-font-size-xs);
  color: var(--livechat-text-muted);
  margin-top: 4px;
  padding: 0 4px;
}

.livechat-message.visitor .livechat-message-time {
  text-align: right;
}

.livechat-message-delivery {
  display: flex;
  align-items: center;
  justify-content: flex-end;
  gap: 8px;
  margin-top: 4px;
  padding: 0 4px;
  color: var(--livechat-text-muted);
  font-size: var(--livechat-font-size-xs);
}

.livechat-message.delivery-failed .livechat-message-content {
  opacity: 0.75;
}

.livechat-message.delivery-failed .livechat-message-delivery {
  color: var(--livechat-error, #dc2626);
}

.livechat-message-retry {
  padding: 0;
  border: 0;
  background: transparent;
  color: inherit;
  font: inherit;
  font-weight: 600;
  text-decoration: underline;
  cursor: pointer;
}

.livechat-form-error {
  margin: 0;
  color: var(--livechat-error, #dc2626);
  font-size: var(--livechat-font-size-sm);
}

.livechat-btn-primary:disabled {
  cursor: wait;
  opacity: 0.65;
}

/* Image messages */
.livechat-message-image {
  padding: 4px !important;
  background: transparent !important;
  max-width: 250px;
}

.livechat-message-image img {
  width: 100%;
  max-width: 100%;
  height: auto;
  border-radius: var(--livechat-radius-lg);
  display: block;
  cursor: pointer;
  transition: opacity var(--livechat-transition-fast);
}

.livechat-message-image img:hover {
  opacity: 0.9;
}

.livechat-message.agent .livechat-message-image {
  background: transparent !important;
}

.livechat-message.visitor .livechat-message-image {
  background: transparent !important;
}

/* ============================================
   Emoji Picker
   ============================================ */
.livechat-emoji-picker-wrapper {
  position: absolute;
  bottom: 68px;
  right: 12px;
  z-index: 1000;
  animation: slideUp var(--livechat-transition-fast);
}

@keyframes slideUp {
  from {
    opacity: 0;
    transform: translateY(10px);
  }
  to {
    opacity: 1;
    transform: translateY(0);
  }
}

/* Emoji Mart with the consistent Apple artwork set */
.livechat-emoji-picker-element {
  --rgb-accent: 45, 55, 72;
  --rgb-background: 255, 255, 255;
  --rgb-color: 26, 32, 44;
  --rgb-input: 247, 250, 252;
  --shadow: none;
  width: min(350px, calc(100vw - 32px));
  height: 390px;
  border: 1px solid var(--livechat-border-light);
  border-radius: 18px;
  box-shadow: var(--livechat-shadow-xl);
  overflow: hidden;
}

/* Simple fallback picker */
.livechat-emoji-picker-simple {
  position: absolute;
  bottom: 60px;
  right: 0;
  width: 280px;
  max-height: 300px;
  background: var(--livechat-bg-card);
  border: 1px solid var(--livechat-border-light);
  border-radius: var(--livechat-radius-lg);
  box-shadow: var(--livechat-shadow-lg);
  padding: 12px;
  display: grid;
  grid-template-columns: repeat(8, 1fr);
  gap: 4px;
  overflow-y: auto;
  z-index: 1000;
  animation: slideUp var(--livechat-transition-fast);
}

.livechat-emoji-item {
  width: 32px;
  height: 32px;
  border: none;
  background: transparent;
  font-size: 20px;
  cursor: pointer;
  border-radius: var(--livechat-radius-sm);
  display: flex;
  align-items: center;
  justify-content: center;
  transition: background var(--livechat-transition-fast);
  padding: 0;
}

.livechat-emoji-item:hover {
  background: var(--livechat-bg-hover);
}

.livechat-emoji-picker-simple::-webkit-scrollbar {
  width: 6px;
}

.livechat-emoji-picker-simple::-webkit-scrollbar-track {
  background: transparent;
}

.livechat-emoji-picker-simple::-webkit-scrollbar-thumb {
  background: var(--livechat-border-light);
  border-radius: var(--livechat-radius-full);
}

.livechat-emoji-picker-simple::-webkit-scrollbar-thumb:hover {
  background: var(--livechat-border-default);
}

/* ============================================
   Typing Indicator
   ============================================ */
.livechat-typing {
  display: none;
  align-items: center;
  gap: 8px;
  padding: 8px 20px;
  font-size: var(--livechat-font-size-sm);
  color: var(--livechat-text-tertiary);
}

.livechat-typing-dots {
  display: flex;
  gap: 4px;
}

.livechat-typing-dots span {
  width: 6px;
  height: 6px;
  border-radius: var(--livechat-radius-full);
  background: var(--livechat-text-muted);
  animation: typingBounce 1.4s ease-in-out infinite;
}

.livechat-typing-dots span:nth-child(1) { animation-delay: 0s; }
.livechat-typing-dots span:nth-child(2) { animation-delay: 0.15s; }
.livechat-typing-dots span:nth-child(3) { animation-delay: 0.3s; }

@keyframes typingBounce {
  0%, 60%, 100% { 
    opacity: 0.4; 
    transform: translateY(0);
  }
  30% { 
    opacity: 1; 
    transform: translateY(-4px);
  }
}

/* ============================================
   Input Form Area
   ============================================ */
.livechat-form {
  display: flex;
  flex-direction: column;
  gap: 8px;
  padding: 16px 20px;
  background: var(--livechat-bg-white);
  border-top: 1px solid var(--livechat-border-light);
  flex-shrink: 0;
}

.livechat-input-container {
  display: flex;
  flex-direction: column;
  gap: 8px;
}

.livechat-input-wrapper {
  flex: 1;
  display: flex;
  align-items: center;
  gap: 8px;
  min-height: 44px;
  background: var(--livechat-bg-light);
  border: 1px solid var(--livechat-border-light);
  border-radius: var(--livechat-radius-lg);
  padding: 0 12px;
  transition: border-color var(--livechat-transition-fast),
              box-shadow var(--livechat-transition-fast);
}

.livechat-input-wrapper:focus-within {
  border-color: var(--livechat-primary);
  box-shadow: 0 0 0 3px color-mix(in srgb, var(--livechat-primary) 14%, transparent);
}

.livechat-input {
  flex: 1;
  height: 44px;
  padding: 10px 0;
  margin: 0;
  background: transparent;
  border: none;
  font-family: var(--livechat-font-family);
  font-size: var(--livechat-font-size-base);
  color: var(--livechat-text-primary);
  resize: none;
  outline: none;
  line-height: 1.5;
  box-shadow: none;
}

.livechat-input:focus {
  outline: none;
  box-shadow: none;
}

.livechat-input::placeholder {
  color: var(--livechat-text-placeholder);
}

.livechat-action-btn {
  width: 32px;
  height: 32px;
  flex-shrink: 0;
  border-radius: var(--livechat-radius-md);
  border: none;
  background: transparent;
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: center;
  color: var(--livechat-text-tertiary);
  transition: background var(--livechat-transition-fast),
              color var(--livechat-transition-fast);
}

.livechat-action-btn:hover {
  background: var(--livechat-bg-hover);
  color: var(--livechat-text-secondary);
}

.livechat-action-btn svg {
  width: 18px;
  height: 18px;
}

/* Send Button - Inside input wrapper, appears on right when typing */
.livechat-send {
  width: 36px;
  height: 36px;
  border-radius: var(--livechat-radius-full);
  border: none;
  background: var(--livechat-primary);
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: center;
  transition: background var(--livechat-transition-fast),
              transform var(--livechat-transition-fast);
  flex-shrink: 0;
  padding: 0;
}

.livechat-send:hover {
  background: var(--livechat-primary-hover);
  transform: scale(1.05);
}

.livechat-send svg {
  width: 18px;
  height: 18px;
  color: white;
}

.livechat-send:active {
  transform: scale(0.95);
}

/* ============================================
   Custom Scrollbar
   ============================================ */
.livechat-messages::-webkit-scrollbar,
.livechat-prechat::-webkit-scrollbar {
  width: 6px;
}

.livechat-messages::-webkit-scrollbar-track,
.livechat-prechat::-webkit-scrollbar-track {
  background: transparent;
}

.livechat-messages::-webkit-scrollbar-thumb,
.livechat-prechat::-webkit-scrollbar-thumb {
  background: var(--livechat-border-light);
  border-radius: var(--livechat-radius-full);
}

.livechat-messages::-webkit-scrollbar-thumb:hover,
.livechat-prechat::-webkit-scrollbar-thumb:hover {
  background: var(--livechat-border-default);
}

/* Firefox */
.livechat-messages,
.livechat-prechat {
  scrollbar-width: thin;
  scrollbar-color: var(--livechat-border-light) transparent;
}

/* ============================================
   Mobile Responsive Styles
   Full-screen chat window when open
   ============================================ */
@media (max-width: 768px) {
  /* Anchored to the visual viewport, which is the part of the page actually
     visible on screen. On iOS the layout viewport keeps its full height when
     the keyboard opens, so a 100dvh window would push the composer behind the
     keyboard. JS keeps these vars in sync; the fallbacks cover no-JS paints.
     !important beats the inline width/height from widgetAppearance settings. */
  .livechat-window {
    top: var(--livechat-viewport-top, 0px) !important;
    left: 0 !important;
    right: 0 !important;
    bottom: auto !important;
    width: 100% !important;
    max-width: 100% !important;
    height: var(--livechat-viewport-height, 100dvh) !important;
    max-height: none !important;
    min-height: 0 !important;
    border-radius: 0 !important;
    box-shadow: none;
    /* A transform on a fixed element shifts it while iOS resizes the visual
       viewport, which reads as the whole widget sliding sideways. */
    animation: none;
  }

  /* iOS zooms the page whenever a focused field is under 16px, which drags the
     fixed widget off-centre and leaves the user stuck at that zoom level. */
  #livechat-widget input,
  #livechat-widget textarea,
  #livechat-widget select {
    font-size: 16px !important;
  }
  
  .livechat-header {
    border-radius: 0;
    padding-top: max(16px, env(safe-area-inset-top, 0px));
    padding-bottom: 24px;
  }

  /* Thumb-sized target for the one control a visitor reaches for most */
  .livechat-back-btn {
    margin-left: -8px;
    width: 40px;
    height: 40px;
  }

  .livechat-form {
    padding-bottom: max(16px, env(safe-area-inset-bottom, 0px));
  }

  /* The keyboard already covers the home-bar area, so the inset would only
     waste rows of the shrunken window. */
  #livechat-widget.livechat-keyboard-open .livechat-form {
    padding-bottom: 12px;
  }

  #livechat-widget.livechat-keyboard-open .livechat-header {
    padding-top: 12px;
    padding-bottom: 16px;
  }
  
  .livechat-button {
    bottom: max(20px, env(safe-area-inset-bottom, 0px));
    width: 56px;
    height: 56px;
  }
  
  .livechat-button.right {
    right: max(20px, env(safe-area-inset-right, 0px));
  }
  
  .livechat-button.left {
    left: max(20px, env(safe-area-inset-left, 0px));
  }
  
  .livechat-button svg {
    width: 24px;
    height: 24px;
  }
  
  .livechat-messages {
    padding: 16px;
  }
  
  .livechat-message {
    max-width: 85%;
  }
  
  .livechat-prechat,
  .livechat-offline {
    padding: 16px;
  }
  
  .livechat-prechat-card,
  .livechat-offline-card {
    padding: 20px;
  }
}

/* ============================================
   Bot Messages & Action Buttons
   ============================================ */

/* Bot messages - Left aligned; match agent styling for a cohesive brand look */
.livechat-message.bot {
  align-self: flex-start;
  max-width: 85%;
}

.livechat-message.bot .livechat-message-content {
  background: var(--livechat-bg-message-agent);
  color: var(--livechat-text-primary);
  border: 1px solid var(--livechat-border-light);
  border-bottom-left-radius: var(--livechat-radius-sm);
}

.livechat-bot-badge {
  display: inline-flex;
  align-items: center;
  gap: 4px;
  padding: 2px 8px;
  background: var(--livechat-primary);
  color: var(--livechat-text-white);
  font-size: 10px;
  font-weight: 600;
  border-radius: var(--livechat-radius-full);
  text-transform: none;
  letter-spacing: 0.2px;
}

/* Cowhide Bot uses the same brand treatment as other bot replies */
.livechat-bot-badge.livechat-cowhide-bot {
  padding: 3px 10px;
  font-size: 11px;
}

.livechat-message.cowhide-bot .livechat-message-content {
  background: var(--livechat-bg-message-agent);
  border: 1px solid var(--livechat-border-light);
}

.livechat-message.cowhide-bot {
  animation: botFadeIn 0.3s ease-out;
}

@keyframes botFadeIn {
  from {
    opacity: 0;
    transform: translateY(8px);
  }
  to {
    opacity: 1;
    transform: translateY(0);
  }
}

/* ============================================
   Self-Service Forms
   Structured cards inside the transcript, styled to match the pre-chat form
   so a form in a bubble does not read as a different product.
   ============================================ */
.livechat-form-card {
  display: flex;
  flex-direction: column;
  gap: 14px;
  margin-top: 12px;
  padding: 16px;
  background: var(--livechat-bg-card);
  border: 1px solid var(--livechat-border-light);
  border-radius: var(--livechat-radius-lg);
  box-shadow: var(--livechat-shadow-card);
}

.livechat-form-card.submitted {
  opacity: 0.6;
}

.livechat-form-card-title {
  font-size: var(--livechat-font-size-md);
  font-weight: 600;
  color: var(--livechat-text-primary);
}

.livechat-form-card .livechat-input-field {
  height: 42px;
}

.livechat-form-card .livechat-btn-primary {
  height: 42px;
  margin: 0;
}

.livechat-form-card .livechat-btn-primary:disabled {
  transform: none;
}

/*
 * Context the visitor confirms against rather than fills in — the order about
 * to be cancelled, the address about to be used. Reads as a receipt line, not
 * as a disabled input, so nobody tries to correct it here.
 */
.livechat-form-static {
  display: flex;
  justify-content: space-between;
  align-items: baseline;
  gap: 12px;
  padding: 10px 12px;
  background: var(--livechat-bg-message-agent);
  border-radius: var(--livechat-radius-md);
}

.livechat-form-static-label {
  font-size: 12px;
  color: var(--livechat-text-secondary);
  flex-shrink: 0;
}

.livechat-form-static-value {
  font-size: var(--livechat-font-size-base);
  color: var(--livechat-text-primary);
  text-align: right;
  white-space: pre-line;
}

.livechat-field-help {
  font-size: 12px;
  line-height: 1.4;
  color: var(--livechat-text-secondary);
}

.livechat-field-error {
  font-size: 12px;
  line-height: 1.4;
  color: var(--livechat-error, #dc2626);
}

/* Action Button Group */
.livechat-button-group {
  display: flex;
  flex-direction: column;
  gap: 8px;
  margin-top: 12px;
  padding: 0 4px;
}

/* Action Buttons (class name predates the retired Flow Builder) */
.livechat-flow-button {
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 12px 16px;
  background: white;
  border: 2px solid var(--livechat-primary);
  border-radius: var(--livechat-radius-lg);
  color: var(--livechat-primary);
  font-family: var(--livechat-font-family);
  font-size: var(--livechat-font-size-base);
  font-weight: 400;
  cursor: pointer;
  transition: all var(--livechat-transition-fast);
  text-align: center;
  line-height: 1.5;
}

.livechat-flow-button:hover:not(:disabled) {
  background: var(--livechat-primary);
  color: white;
  transform: translateY(-1px);
  box-shadow: 0 4px 12px rgba(99, 102, 241, 0.25);
}

.livechat-flow-button:active:not(:disabled) {
  transform: translateY(0);
}

.livechat-flow-button:disabled {
  opacity: 0.5;
  cursor: not-allowed;
  border-color: var(--livechat-border-light);
  color: var(--livechat-text-tertiary);
}

.livechat-flow-button.clicked {
  opacity: 0.6;
  background: var(--livechat-bg-light);
}

.livechat-flow-button.selected {
  background: var(--livechat-primary);
  color: white;
  opacity: 1;
  border-color: var(--livechat-primary);
}

/* Button Response Message (visitor's button selection) */
.livechat-button-response {
  background: var(--livechat-primary) !important;
  color: white !important;
  border-radius: var(--livechat-radius-xl) !important;
  border-bottom-right-radius: var(--livechat-radius-sm) !important;
}

/* ============================================
   Reduced Motion Preference
   ============================================ */
@media (prefers-reduced-motion: reduce) {
  *,
  *::before,
  *::after {
    animation-duration: 0.01ms !important;
    animation-iteration-count: 1 !important;
    transition-duration: 0.01ms !important;
  }
  
  .livechat-window {
    animation: none;
  }
  
  .livechat-message {
    animation: none;
  }
}

/* ============================================
   Print Styles
   ============================================ */
@media print {
  #livechat-widget {
    display: none !important;
  }
}

/* ============================================
   Link Styling
   ============================================ */
.livechat-link {
  color: inherit;
  text-decoration: underline;
  font-weight: 500;
  word-break: break-all;
}

.livechat-link:hover {
  opacity: 0.8;
}

/* Specific color adjustments based on background */
.livechat-message.visitor .livechat-link {
  color: var(--livechat-text-white);
}

.livechat-message.agent .livechat-link,
.livechat-message.bot .livechat-link {
  color: var(--livechat-primary);
}

/* CSAT Survey */
.livechat-survey {
  text-align: center;
  padding: 16px 12px;
}

.livechat-survey-prompt {
  font-size: var(--livechat-font-size-base);
  font-weight: 500;
  color: var(--livechat-text-primary);
  margin-bottom: 12px;
}

.livechat-survey-stars {
  display: flex;
  justify-content: center;
  gap: 8px;
  margin-bottom: 8px;
}

.livechat-star {
  background: none;
  border: none;
  font-size: 28px;
  cursor: pointer;
  color: #d1d5db;
  transition: color 0.15s, transform 0.15s;
  padding: 4px;
  line-height: 1;
}

.livechat-star:hover:not(:disabled),
.livechat-star.hover {
  color: #fbbf24;
  transform: scale(1.2);
}

.livechat-star.active {
  color: #f59e0b;
}

.livechat-star:disabled {
  cursor: default;
}

.livechat-survey-thanks {
  font-size: var(--livechat-font-size-sm);
  color: var(--livechat-text-secondary);
  margin-top: 8px;
  animation: livechat-fade-in 0.3s ease;
}

@keyframes livechat-fade-in {
  from { opacity: 0; transform: translateY(4px); }
  to { opacity: 1; transform: translateY(0); }
}

/* Product Card */
.livechat-product-card {
  border: 1px solid var(--livechat-border-light);
  border-radius: var(--livechat-radius-lg);
  overflow: hidden;
  background: white;
  margin-top: 4px;
}

.livechat-product-image {
  width: 100%;
  height: 140px;
  object-fit: cover;
}

.livechat-product-info {
  padding: 10px 12px;
}

.livechat-product-title {
  font-size: var(--livechat-font-size-base);
  font-weight: 600;
  color: var(--livechat-text-primary);
  margin-bottom: 4px;
}

.livechat-product-price {
  font-size: var(--livechat-font-size-base);
  font-weight: 500;
  color: var(--livechat-primary);
}

.livechat-product-compare {
  text-decoration: line-through;
  color: var(--livechat-text-tertiary);
  margin-right: 6px;
  font-weight: 400;
}

.livechat-product-button {
  display: block;
  text-align: center;
  padding: 10px;
  background: var(--livechat-primary);
  color: white;
  font-size: var(--livechat-font-size-sm);
  font-weight: 500;
  text-decoration: none;
  transition: opacity 0.15s;
}

.livechat-product-button:hover {
  opacity: 0.9;
}

/* Preserve clear keyboard focus without adding rings for pointer clicks. */
#livechat-widget button:focus-visible,
#livechat-widget input:not(.livechat-input):focus-visible,
#livechat-widget textarea:focus-visible,
#livechat-widget a:focus-visible {
  outline: 3px solid var(--livechat-primary);
  outline-offset: 2px;
}
