/**
 * WhatsApp Chat Widget - Frontend Styles
 * Scoped, ultra-lightweight, zero framework dependencies.
 */

/* Scoped Container Reset */
.wcw-container,
.wcw-container * {
  box-sizing: border-box;
  margin: 0;
  padding: 0;
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif;
  -webkit-font-smoothing: antialiased;
}

.wcw-container {
  position: fixed;
  z-index: 9999999;
  line-height: 1.4;
}

/* Corner Positions */
.wcw-container.wcw-pos-bottom-right {
  bottom: max(var(--wcw-margin-bottom, 30px), env(safe-area-inset-bottom, 16px));
  right: max(var(--wcw-margin-right, 30px), env(safe-area-inset-right, 16px));
}

.wcw-container.wcw-pos-bottom-left {
  bottom: max(var(--wcw-margin-bottom, 30px), env(safe-area-inset-bottom, 16px));
  left: max(var(--wcw-margin-left, 30px), env(safe-area-inset-left, 16px));
}

/* Floating Trigger Button */
.wcw-chat-button {
  display: inline-flex;
  align-items: center;
  gap: 10px;
  background-color: var(--wcw-btn-bg, #4dc247);
  color: #ffffff;
  border: none;
  border-radius: var(--wcw-border-radius, 24px);
  padding: 12px 20px;
  cursor: pointer;
  box-shadow: 0 4px 14px rgba(0, 0, 0, 0.18);
  transition: transform 0.25s cubic-bezier(0.16, 1, 0.3, 1), box-shadow 0.25s ease, background-color 0.2s ease;
  user-select: none;
  outline: none;
  text-decoration: none;
  touch-action: manipulation;
  -webkit-tap-highlight-color: transparent;
}

@media (hover: hover) and (pointer: fine) {
  .wcw-chat-button:hover {
    transform: translateY(-2px) scale(1.02);
    box-shadow: 0 6px 20px rgba(0, 0, 0, 0.24);
  }
}

.wcw-chat-button:active {
  transform: translateY(0) scale(0.97);
}

.wcw-btn-icon {
  display: flex;
  align-items: center;
  justify-content: center;
  flex-shrink: 0;
  width: 24px;
  height: 24px;
}

.wcw-btn-icon svg {
  width: 24px;
  height: 24px;
  display: block;
}

.wcw-btn-text {
  font-size: 15px;
  font-weight: 600;
  color: #ffffff;
  white-space: nowrap;
  letter-spacing: 0.2px;
}

/* Single Round Icon-Only Button */
.wcw-chat-button.wcw-style-icon-only {
  width: 58px;
  height: 58px;
  padding: 0 !important;
  border-radius: 50% !important;
  justify-content: center;
  gap: 0;
}

.wcw-chat-button.wcw-style-icon-only .wcw-btn-icon {
  width: 32px;
  height: 32px;
}

.wcw-chat-button.wcw-style-icon-only .wcw-btn-icon svg {
  width: 32px;
  height: 32px;
}

.wcw-chat-button.wcw-style-icon-only .wcw-btn-text {
  display: none !important;
}

/* Without Background Mode (Transparent) */
.wcw-chat-button.wcw-bg-mode-no-bg {
  background-color: transparent !important;
  box-shadow: none !important;
  padding-left: 4px;
  padding-right: 4px;
}

.wcw-chat-button.wcw-bg-mode-no-bg .wcw-btn-icon svg {
  fill: var(--wcw-btn-bg, #25d366) !important;
  filter: drop-shadow(0 3px 8px rgba(0, 0, 0, 0.16));
}

.wcw-chat-button.wcw-bg-mode-no-bg .wcw-btn-text {
  color: var(--wcw-btn-bg, #25d366) !important;
  font-weight: 700;
  text-shadow: 0 1px 3px rgba(255, 255, 255, 0.8);
}

.wcw-chat-button.wcw-bg-mode-no-bg:hover {
  background-color: transparent !important;
  transform: scale(1.1);
  box-shadow: none !important;
}

/* Chat Popup Window */
.wcw-popup {
  position: absolute;
  width: 340px;
  max-width: calc(100vw - 32px);
  background: #ffffff;
  border-radius: 18px;
  box-shadow: 0 16px 40px -4px rgba(0, 0, 0, 0.16), 0 0 1px 1px rgba(0, 0, 0, 0.04);
  overflow: hidden;
  display: flex;
  flex-direction: column;
  z-index: 1000000;
  opacity: 0;
  visibility: hidden;
  pointer-events: none;
  transform: translateY(14px) scale(0.96);
  transition: opacity 0.28s cubic-bezier(0.16, 1, 0.3, 1), transform 0.28s cubic-bezier(0.16, 1, 0.3, 1), visibility 0.28s ease;
  will-change: transform, opacity;
}

.wcw-pos-bottom-right .wcw-popup {
  right: 0;
  bottom: calc(100% + 14px);
  transform-origin: bottom right;
}

.wcw-pos-bottom-left .wcw-popup {
  left: 0;
  bottom: calc(100% + 14px);
  transform-origin: bottom left;
}

.wcw-popup.wcw-open {
  opacity: 1;
  visibility: visible;
  pointer-events: auto;
  transform: translateY(0) scale(1);
}

/* Popup Header */
.wcw-popup-header {
  background-color: var(--wcw-brand-color, #0A5F54);
  padding: 16px 18px;
  display: flex;
  align-items: center;
  justify-content: space-between;
  color: #ffffff;
}

.wcw-header-brand {
  display: flex;
  align-items: center;
  gap: 12px;
  min-width: 0;
}

.wcw-header-avatar {
  width: 42px;
  height: 42px;
  border-radius: 50%;
  object-fit: cover;
  flex-shrink: 0;
  background: rgba(255, 255, 255, 0.2);
}

.wcw-header-info {
  display: flex;
  flex-direction: column;
  min-width: 0;
}

.wcw-header-name-wrap {
  display: flex;
  align-items: center;
  gap: 6px;
}

.wcw-header-name {
  font-size: 16px;
  font-weight: 700;
  color: #ffffff;
  line-height: 1.2;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.wcw-verified-badge {
  width: 16px;
  height: 16px;
  flex-shrink: 0;
  display: inline-flex;
  vertical-align: middle;
}

.wcw-header-subtitle {
  font-size: 13px;
  font-weight: 400;
  color: rgba(255, 255, 255, 0.85);
  line-height: 1.3;
}

.wcw-popup-close {
  background: none;
  border: none;
  color: rgba(255, 255, 255, 0.85);
  font-size: 24px;
  font-weight: 300;
  line-height: 1;
  cursor: pointer;
  width: 32px;
  height: 32px;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 0;
  border-radius: 50%;
  transition: color 0.2s ease, background-color 0.2s ease, transform 0.2s cubic-bezier(0.16, 1, 0.3, 1);
  -webkit-tap-highlight-color: transparent;
}

.wcw-popup-close:hover,
.wcw-popup-close:focus-visible {
  color: #ffffff;
  background-color: rgba(255, 255, 255, 0.18);
  transform: rotate(90deg) scale(1.1);
  outline: none;
}

/* Popup Body (Chat Canvas) */
.wcw-popup-body {
  background-color: #f0ebe4;
  background-image: radial-gradient(#d5cdc4 1px, transparent 1px);
  background-size: 16px 16px;
  padding: 20px 16px 22px 16px;
  display: flex;
  flex-direction: column;
  min-height: 100px;
  max-height: 360px;
  overflow-y: auto;
  -webkit-overflow-scrolling: touch;
  overscroll-behavior: contain;
}

.wcw-popup-body::-webkit-scrollbar {
  width: 4px;
}

.wcw-popup-body::-webkit-scrollbar-thumb {
  background: rgba(0, 0, 0, 0.15);
  border-radius: 4px;
}

.wcw-chat-bubble {
  align-self: flex-start;
  max-width: 90%;
  background: #ffffff;
  border-radius: 0 12px 12px 12px;
  padding: 10px 14px 12px 14px;
  box-shadow: 0 1px 3px rgba(0, 0, 0, 0.08);
  position: relative;
  transition: transform 0.3s cubic-bezier(0.16, 1, 0.3, 1), opacity 0.3s ease;
}

.wcw-popup.wcw-open .wcw-chat-bubble {
  animation: wcwBubbleSlide 0.35s cubic-bezier(0.16, 1, 0.3, 1) forwards;
}

@keyframes wcwBubbleSlide {
  from {
    opacity: 0;
    transform: translateY(6px) scale(0.98);
  }
  to {
    opacity: 1;
    transform: translateY(0) scale(1);
  }
}

/* Chat bubble triangular tail */
.wcw-chat-bubble::before {
  content: "";
  position: absolute;
  left: -8px;
  top: 0;
  width: 0;
  height: 0;
  border-top: 10px solid #ffffff;
  border-left: 8px solid transparent;
}

.wcw-bubble-sender-wrap {
  display: flex;
  align-items: center;
  gap: 5px;
  margin-bottom: 4px;
}

.wcw-bubble-sender {
  font-size: 12px;
  font-weight: 600;
  color: #667781;
  line-height: 1.2;
}

.wcw-bubble-badge {
  width: 13px;
  height: 13px;
  flex-shrink: 0;
}

.wcw-bubble-message {
  font-size: 14px;
  line-height: 1.4;
  color: #111b21;
  word-break: break-word;
  white-space: normal;
}

/* Popup Footer */
.wcw-popup-footer {
  background: #ffffff;
  padding: 14px 16px 12px 16px;
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 8px;
  border-top: 1px solid #f3f4f6;
}

.wcw-popup-cta-btn {
  width: 100%;
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 8px;
  background-color: var(--wcw-btn-bg, #4dc247);
  color: #ffffff;
  font-size: 15px;
  font-weight: 600;
  padding: 12px 20px;
  border-radius: 26px;
  border: none;
  cursor: pointer;
  box-shadow: 0 3px 12px rgba(77, 194, 71, 0.28);
  transition: transform 0.2s cubic-bezier(0.16, 1, 0.3, 1), box-shadow 0.2s ease, filter 0.2s ease;
  outline: none;
  text-align: center;
}

.wcw-cta-wa-icon {
  width: 18px;
  height: 18px;
  fill: currentColor;
  flex-shrink: 0;
}

.wcw-popup-cta-btn:hover {
  filter: brightness(1.05);
  transform: translateY(-2px);
  box-shadow: 0 6px 18px rgba(77, 194, 71, 0.4);
}

.wcw-popup-cta-btn:active {
  transform: translateY(0);
}

.wcw-popup-branding {
  font-size: 11px;
  color: #8696a0;
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 4px;
  letter-spacing: 0.15px;
  user-select: none;
}

.wcw-branding-link {
  color: #10b981;
  text-decoration: none;
  font-weight: 600;
  transition: color 0.15s ease, text-decoration 0.15s ease;
}

.wcw-branding-link:hover {
  color: #059669;
  text-decoration: underline;
}

/* Shortcode Trigger Button */
.wcw-shortcode-trigger {
  display: inline-flex;
  align-items: center;
  gap: 8px;
  background-color: #25d366;
  color: #ffffff;
  border: none;
  border-radius: 24px;
  padding: 10px 20px;
  font-size: 14px;
  font-weight: 600;
  cursor: pointer;
  transition: all 0.2s;
}

.wcw-shortcode-trigger:hover {
  background-color: #1eb855;
}

/* Responsive Breakpoints & Device Adaptations */
@media screen and (max-width: 768px) {
  .wcw-popup {
    max-width: min(340px, calc(100vw - 32px));
  }
}

@media screen and (max-width: 600px) {
  .wcw-container.wcw-pos-bottom-right {
    bottom: max(16px, env(safe-area-inset-bottom, 16px));
    right: max(16px, env(safe-area-inset-right, 16px));
    left: auto;
  }

  .wcw-container.wcw-pos-bottom-left {
    bottom: max(16px, env(safe-area-inset-bottom, 16px));
    left: max(16px, env(safe-area-inset-left, 16px));
    right: auto;
  }

  .wcw-container .wcw-popup,
  .wcw-pos-bottom-right .wcw-popup,
  .wcw-pos-bottom-left .wcw-popup,
  .wcw-popup {
    position: fixed !important;
    bottom: calc(76px + env(safe-area-inset-bottom, 16px)) !important;
    right: 14px !important;
    left: 14px !important;
    top: auto !important;
    width: auto !important;
    max-width: calc(100vw - 28px) !important;
    max-height: calc(100vh - 96px) !important;
    max-height: calc(100dvh - 96px) !important;
    border-radius: 16px;
    transform-origin: bottom center !important;
  }

  .wcw-container .wcw-popup.wcw-open,
  .wcw-pos-bottom-right .wcw-popup.wcw-open,
  .wcw-pos-bottom-left .wcw-popup.wcw-open,
  .wcw-popup.wcw-open {
    opacity: 1 !important;
    visibility: visible !important;
    pointer-events: auto !important;
    transform: translateY(0) scale(1) !important;
  }

  .wcw-popup-body {
    max-height: calc(100vh - 230px);
    max-height: calc(100dvh - 230px);
  }
}

/* Small screens (e.g. Galaxy Z Fold cover screen, narrow mobile devices) */
@media screen and (max-width: 360px) {
  .wcw-chat-button {
    padding: 10px 14px;
    gap: 6px;
  }

  .wcw-btn-text {
    font-size: 13px;
  }

  .wcw-container .wcw-popup,
  .wcw-pos-bottom-right .wcw-popup,
  .wcw-pos-bottom-left .wcw-popup,
  .wcw-popup {
    right: 8px !important;
    left: 8px !important;
    max-width: calc(100vw - 16px) !important;
  }
}

/* Landscape Mode on Mobile */
@media screen and (max-height: 500px) and (orientation: landscape) {
  .wcw-container .wcw-popup,
  .wcw-pos-bottom-right .wcw-popup,
  .wcw-pos-bottom-left .wcw-popup,
  .wcw-popup {
    position: fixed !important;
    bottom: max(8px, env(safe-area-inset-bottom, 8px)) !important;
    top: 8px !important;
    max-height: calc(100vh - 16px) !important;
    max-height: calc(100dvh - 16px) !important;
  }

  .wcw-popup-header {
    padding: 10px 14px;
  }

  .wcw-header-avatar {
    width: 32px;
    height: 32px;
  }

  .wcw-popup-body {
    max-height: 120px;
    padding: 10px 14px;
  }

  .wcw-popup-footer {
    padding: 8px 14px;
  }

  .wcw-popup-cta-btn {
    padding: 8px 16px;
    font-size: 13px;
  }
}

/* Attention Unread Badge on Floating Button */
.wcw-unread-badge {
  position: absolute;
  top: -5px;
  right: -5px;
  background: #ef4444;
  color: #ffffff;
  font-size: 11px;
  font-weight: 700;
  width: 20px;
  height: 20px;
  border-radius: 50%;
  display: flex;
  align-items: center;
  justify-content: center;
  border: 2px solid #ffffff;
  box-shadow: 0 2px 6px rgba(239, 68, 68, 0.45);
  animation: wcwBadgePulse 2.4s cubic-bezier(0.4, 0, 0.6, 1) infinite;
  pointer-events: none;
  z-index: 2;
}

@keyframes wcwBadgePulse {
  0%, 100% {
    transform: scale(1);
    box-shadow: 0 2px 6px rgba(239, 68, 68, 0.45);
  }
  50% {
    transform: scale(1.15);
    box-shadow: 0 4px 12px rgba(239, 68, 68, 0.7);
  }
}
