/* ============================================
   Customer Service Visitor Widget v2
   With Group Chat Support
   ============================================ */

/* CSS Variables */
:root {
  --cs-primary: #4A90D9;
  --cs-primary-dark: #357ABD;
  --cs-primary-light: #E8F2FF;
  --cs-success: #52C41A;
  --cs-danger: #FF4D4F;
  --cs-warning: #FAAD14;
  --cs-bg: #FFFFFF;
  --cs-bg-light: #F5F7FA;
  --cs-bg-dark: #E4E7ED;
  --cs-text: #333333;
  --cs-text-secondary: #666666;
  --cs-text-light: #999999;
  --cs-border: #E4E7ED;
  --cs-shadow: 0 2px 12px rgba(0, 0, 0, 0.1);
  --cs-shadow-lg: 0 4px 24px rgba(0, 0, 0, 0.15);
  --cs-radius: 8px;
  --cs-radius-lg: 12px;
  --cs-transition: all 0.3s ease;
}

/* Reset for widget container */
#cs-widget-container,
#cs-widget-container * {
  box-sizing: border-box;
  margin: 0;
  padding: 0;
}

/* Main Widget Container */
#cs-widget-container {
  position: fixed;
  bottom: 20px;
  right: 20px;
  z-index: 999999;
  font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, 'Helvetica Neue', Arial, sans-serif;
  font-size: 14px;
  color: var(--cs-text);
  line-height: 1.5;
}

/* Chat Button */
#cs-chat-btn {
  width: 60px;
  height: 60px;
  border-radius: 50%;
  background: linear-gradient(135deg, var(--cs-primary), var(--cs-primary-dark));
  border: none;
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: center;
  box-shadow: var(--cs-shadow-lg);
  transition: var(--cs-transition);
  position: relative;
}

#cs-chat-btn:hover {
  transform: scale(1.05);
  box-shadow: 0 6px 20px rgba(74, 144, 217, 0.4);
}

#cs-chat-btn .cs-icon {
  font-size: 28px;
  color: white;
  line-height: 1;
}

/* Notification Badge */
#cs-chat-btn .cs-badge {
  position: absolute;
  top: -5px;
  right: -5px;
  background: var(--cs-danger);
  color: white;
  font-size: 12px;
  font-weight: 600;
  min-width: 20px;
  height: 20px;
  border-radius: 10px;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 0 6px;
  border: 2px solid white;
  display: none;
}

/* Chat Window */
#cs-chat-window {
  position: absolute;
  bottom: 70px;
  right: 0;
  width: 380px;
  height: 520px;
  background: var(--cs-bg);
  border-radius: var(--cs-radius-lg);
  box-shadow: var(--cs-shadow-lg);
  display: none;
  flex-direction: column;
  overflow: hidden;
  animation: cs-slide-up 0.3s ease;
}

@keyframes cs-slide-up {
  from {
    opacity: 0;
    transform: translateY(20px);
  }
  to {
    opacity: 1;
    transform: translateY(0);
  }
}

/* Chat Header */
#cs-chat-header {
  background: linear-gradient(135deg, var(--cs-primary), var(--cs-primary-dark));
  color: white;
  padding: 16px;
  display: flex;
  flex-direction: column;
  gap: 12px;
}

#cs-header-top {
  display: flex;
  align-items: center;
  justify-content: space-between;
}

#cs-chat-title {
  font-size: 16px;
  font-weight: 600;
}

#cs-header-actions {
  display: flex;
  align-items: center;
  gap: 8px;
}

#cs-lang-btn,
#cs-minimize-btn {
  background: rgba(255, 255, 255, 0.2);
  border: none;
  color: white;
  width: 32px;
  height: 32px;
  border-radius: 6px;
  cursor: pointer;
  font-size: 14px;
  display: flex;
  align-items: center;
  justify-content: center;
  transition: var(--cs-transition);
}

#cs-lang-btn:hover,
#cs-minimize-btn:hover {
  background: rgba(255, 255, 255, 0.3);
}

/* Tab Buttons */
#cs-tab-container {
  display: flex;
  gap: 8px;
}

.cs-tab-btn {
  flex: 1;
  padding: 8px 16px;
  background: rgba(255, 255, 255, 0.2);
  border: none;
  color: white;
  border-radius: 6px;
  cursor: pointer;
  font-size: 13px;
  font-weight: 500;
  transition: var(--cs-transition);
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 6px;
}

.cs-tab-btn:hover {
  background: rgba(255, 255, 255, 0.3);
}

.cs-tab-btn.active {
  background: white;
  color: var(--cs-primary);
}

.cs-tab-btn .cs-tab-icon {
  font-size: 16px;
}

/* Chat Content Area */
#cs-chat-content {
  flex: 1;
  display: flex;
  flex-direction: column;
  overflow: hidden;
}

/* Customer Service Chat Panel */
#cs-service-panel {
  flex: 1;
  display: flex;
  flex-direction: column;
  overflow: hidden;
}

/* Messages Container */
#cs-messages {
  flex: 1;
  overflow-y: auto;
  padding: 16px;
  background: var(--cs-bg-light);
  display: flex;
  flex-direction: column;
  gap: 12px;
}

#cs-messages::-webkit-scrollbar {
  width: 6px;
}

#cs-messages::-webkit-scrollbar-track {
  background: transparent;
}

#cs-messages::-webkit-scrollbar-thumb {
  background: var(--cs-border);
  border-radius: 3px;
}

/* Message Bubbles */
.cs-message {
  display: flex;
  gap: 8px;
  max-width: 85%;
  animation: cs-fade-in 0.3s ease;
}

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

.cs-message.cs-mine {
  align-self: flex-end;
  flex-direction: row-reverse;
}

.cs-message.cs-other {
  align-self: flex-start;
}

.cs-message-avatar {
  width: 36px;
  height: 36px;
  border-radius: 50%;
  overflow: hidden;
  flex-shrink: 0;
  background: var(--cs-bg-dark);
}

.cs-message-avatar img {
  width: 100%;
  height: 100%;
  object-fit: cover;
}

.cs-message-content {
  display: flex;
  flex-direction: column;
  gap: 4px;
}

.cs-message.cs-mine .cs-message-content {
  align-items: flex-end;
}

.cs-message.cs-other .cs-message-content {
  align-items: flex-start;
}

.cs-message-name {
  font-size: 12px;
  color: var(--cs-text-light);
  padding: 0 4px;
}

.cs-message-bubble {
  padding: 10px 14px;
  border-radius: 12px;
  background: var(--cs-bg);
  box-shadow: 0 1px 2px rgba(0, 0, 0, 0.06);
  word-wrap: break-word;
  max-width: 100%;
}

.cs-message.cs-mine .cs-message-bubble {
  background: var(--cs-primary);
  color: white;
  border-bottom-right-radius: 4px;
}

.cs-message.cs-other .cs-message-bubble {
  background: var(--cs-bg);
  border-bottom-left-radius: 4px;
}

.cs-message-time {
  font-size: 11px;
  color: var(--cs-text-light);
  padding: 0 4px;
}

/* Message Recall */
.cs-message-meta {
  display: flex;
  align-items: center;
  gap: 6px;
}

.cs-message.cs-mine .cs-message-meta {
  flex-direction: row-reverse;
}

.cs-recall-btn {
  border: 1px solid var(--cs-border);
  background: var(--cs-bg);
  color: var(--cs-text-light);
  font-size: 11px;
  line-height: 1;
  padding: 4px 8px;
  border-radius: 10px;
  cursor: pointer;
  /* 移动端点击目标不能太小 */
  min-height: 24px;
}

.cs-recall-btn:hover {
  border-color: var(--cs-primary);
  color: var(--cs-primary);
}

.cs-recalled-message {
  font-style: normal;
}

/* Image Messages */
.cs-message-image {
  max-width: 200px;
  border-radius: 8px;
  cursor: pointer;
  transition: var(--cs-transition);
}

.cs-message-image:hover {
  transform: scale(1.02);
  box-shadow: var(--cs-shadow);
}

/* System Messages */
.cs-system-message {
  text-align: center;
  padding: 8px 16px;
  font-size: 12px;
  color: var(--cs-text-light);
  background: rgba(0, 0, 0, 0.04);
  border-radius: 16px;
  align-self: center;
  max-width: 80%;
}

/* Chat Input Area */
#cs-input-area {
  padding: 12px 16px;
  background: var(--cs-bg);
  border-top: 1px solid var(--cs-border);
  display: flex;
  align-items: center;
  gap: 8px;
}

#cs-image-btn {
  background: none;
  border: none;
  cursor: pointer;
  font-size: 20px;
  color: var(--cs-text-secondary);
  padding: 8px;
  border-radius: 6px;
  transition: var(--cs-transition);
  display: flex;
  align-items: center;
  justify-content: center;
}

#cs-image-btn:hover {
  background: var(--cs-bg-light);
  color: var(--cs-primary);
}

#cs-message-input {
  flex: 1;
  padding: 10px 14px;
  border: 1px solid var(--cs-border);
  border-radius: 20px;
  font-size: 14px;
  outline: none;
  transition: var(--cs-transition);
  resize: none;
  max-height: 100px;
  min-height: 40px;
  font-family: inherit;
}

#cs-message-input:focus {
  border-color: var(--cs-primary);
  box-shadow: 0 0 0 2px var(--cs-primary-light);
}

#cs-send-btn {
  background: var(--cs-primary);
  border: none;
  color: white;
  width: 40px;
  height: 40px;
  border-radius: 50%;
  cursor: pointer;
  font-size: 18px;
  display: flex;
  align-items: center;
  justify-content: center;
  transition: var(--cs-transition);
  flex-shrink: 0;
}

#cs-send-btn:hover {
  background: var(--cs-primary-dark);
}

#cs-send-btn:disabled {
  background: var(--cs-border);
  cursor: not-allowed;
}

/* Group Chat Panel */
#cs-group-panel {
  flex: 1;
  display: none;
  flex-direction: column;
  overflow: hidden;
}

/* Group Join Form */
#cs-group-join-form {
  flex: 1;
  padding: 12px;
  overflow-y: auto;
  display: flex;
  flex-direction: column;
  gap: 8px;
}

#cs-group-join-form h3 {
  margin: 0;
  font-size: 14px;
}

.cs-form-group {
  display: flex;
  flex-direction: column;
  gap: 3px;
}

.cs-form-label {
  font-size: 12px;
  font-weight: 500;
  color: var(--cs-text-secondary);
}

.cs-form-input {
  padding: 8px 12px;
  border: 1px solid var(--cs-border);
  border-radius: var(--cs-radius);
  font-size: 14px;
  outline: none;
  transition: var(--cs-transition);
  font-family: inherit;
}

.cs-form-input:focus {
  border-color: var(--cs-primary);
  box-shadow: 0 0 0 2px var(--cs-primary-light);
}

.cs-form-input::placeholder {
  color: var(--cs-text-light);
}

/* Avatar Selection */
#cs-avatar-selection {
  display: grid;
  grid-template-columns: repeat(5, 36px);
  justify-content: center;
  gap: 6px;
  padding: 4px 0;
}

.cs-avatar-option {
  width: 100%;
  aspect-ratio: 1;
  border-radius: 50%;
  overflow: hidden;
  cursor: pointer;
  border: 2px solid transparent;
  transition: var(--cs-transition);
  position: relative;
}

.cs-avatar-option:hover {
  transform: scale(1.05);
}

.cs-avatar-option.selected {
  border-color: var(--cs-primary);
  box-shadow: 0 0 0 2px var(--cs-primary-light);
}

.cs-avatar-option img {
  width: 100%;
  height: 100%;
  object-fit: cover;
}

/* Join Button */
#cs-group-join-btn {
  padding: 10px 20px;
  background: var(--cs-primary);
  color: white;
  border: none;
  border-radius: var(--cs-radius);
  font-size: 14px;
  font-weight: 500;
  cursor: pointer;
  transition: var(--cs-transition);
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 8px;
  flex-shrink: 0;
}

#cs-group-join-btn:hover {
  background: var(--cs-primary-dark);
}

#cs-group-join-btn:disabled {
  background: var(--cs-border);
  cursor: not-allowed;
}

/* Group Chat Room */
#cs-group-room {
  flex: 1;
  display: none;
  flex-direction: column;
  overflow: hidden;
}

/* Group Info Bar */
#cs-group-info {
  padding: 10px 16px;
  background: var(--cs-bg);
  border-bottom: 1px solid var(--cs-border);
  display: flex;
  align-items: center;
  justify-content: space-between;
}

#cs-group-info-left {
  display: flex;
  align-items: center;
  gap: 10px;
}

#cs-group-name {
  font-size: 14px;
  font-weight: 600;
  color: var(--cs-text);
}

#cs-group-count {
  font-size: 12px;
  color: var(--cs-text-light);
  background: var(--cs-bg-light);
  padding: 2px 8px;
  border-radius: 10px;
}

#cs-group-leave-btn {
  background: none;
  border: 1px solid var(--cs-danger);
  color: var(--cs-danger);
  padding: 6px 12px;
  border-radius: 6px;
  font-size: 12px;
  cursor: pointer;
  transition: var(--cs-transition);
}

#cs-group-leave-btn:hover {
  background: var(--cs-danger);
  color: white;
}

/* Group Messages */
#cs-group-messages {
  flex: 1;
  overflow-y: auto;
  padding: 16px;
  background: var(--cs-bg-light);
  display: flex;
  flex-direction: column;
  gap: 16px;
}

#cs-group-messages::-webkit-scrollbar {
  width: 6px;
}

#cs-group-messages::-webkit-scrollbar-track {
  background: transparent;
}

#cs-group-messages::-webkit-scrollbar-thumb {
  background: var(--cs-border);
  border-radius: 3px;
}

/* Group Message Layout */
.cs-group-msg {
  display: flex;
  gap: 10px;
  max-width: 75%;
  animation: cs-fade-in 0.3s ease;
}

.cs-group-msg.cs-msg-mine {
  align-self: flex-end;
  flex-direction: row-reverse;
}

.cs-group-msg.cs-msg-other {
  align-self: flex-start;
}

.cs-group-msg-avatar {
  width: 30px;
  height: 30px;
  border-radius: 50%;
  overflow: hidden;
  flex-shrink: 0;
  background: var(--cs-bg-dark);
}

.cs-group-msg-avatar img {
  width: 100%;
  height: 100%;
  object-fit: cover;
}

.cs-group-msg-body {
  display: flex;
  flex-direction: column;
  gap: 4px;
}

.cs-group-msg.cs-msg-mine .cs-group-msg-body {
  align-items: flex-end;
}

.cs-group-msg.cs-msg-other .cs-group-msg-body {
  align-items: flex-start;
}

.cs-group-msg-name {
  font-size: 12px;
  color: var(--cs-text-light);
  padding: 0 4px;
}

.cs-group-msg-bubble {
  padding: 10px 14px;
  border-radius: 12px;
  background: var(--cs-bg);
  box-shadow: 0 1px 2px rgba(0, 0, 0, 0.06);
  word-wrap: break-word;
  max-width: 100%;
  font-size: 14px;
  line-height: 1.5;
}

.cs-group-msg.cs-msg-mine .cs-group-msg-bubble {
  background: var(--cs-primary);
  color: white;
  border-bottom-right-radius: 4px;
}

.cs-group-msg.cs-msg-other .cs-group-msg-bubble {
  background: var(--cs-bg);
  border-bottom-left-radius: 4px;
}

.cs-group-msg-time {
  font-size: 11px;
  color: var(--cs-text-light);
  padding: 0 4px;
}

/* Group Image Messages */
.cs-group-msg-image {
  max-width: 200px;
  border-radius: 8px;
  cursor: pointer;
  transition: var(--cs-transition);
}

.cs-group-msg-image:hover {
  transform: scale(1.02);
  box-shadow: var(--cs-shadow);
}

/* Group System Messages */
.cs-group-system-msg {
  text-align: center;
  padding: 6px 14px;
  font-size: 12px;
  color: var(--cs-text-light);
  background: rgba(0, 0, 0, 0.04);
  border-radius: 12px;
  align-self: center;
  max-width: 80%;
}

/* Group Input Area */
#cs-group-input-area {
  padding: 12px 16px;
  background: var(--cs-bg);
  border-top: 1px solid var(--cs-border);
  display: flex;
  align-items: center;
  gap: 8px;
}

#cs-group-image-btn {
  background: none;
  border: none;
  cursor: pointer;
  font-size: 20px;
  color: var(--cs-text-secondary);
  padding: 8px;
  border-radius: 6px;
  transition: var(--cs-transition);
  display: flex;
  align-items: center;
  justify-content: center;
}

#cs-group-image-btn:hover {
  background: var(--cs-bg-light);
  color: var(--cs-primary);
}

#cs-group-msg-input {
  flex: 1;
  padding: 10px 14px;
  border: 1px solid var(--cs-border);
  border-radius: 20px;
  font-size: 14px;
  outline: none;
  transition: var(--cs-transition);
  resize: none;
  max-height: 100px;
  min-height: 40px;
  font-family: inherit;
}

#cs-group-msg-input:focus {
  border-color: var(--cs-primary);
  box-shadow: 0 0 0 2px var(--cs-primary-light);
}

#cs-group-send-btn {
  background: var(--cs-primary);
  border: none;
  color: white;
  width: 40px;
  height: 40px;
  border-radius: 50%;
  cursor: pointer;
  font-size: 18px;
  display: flex;
  align-items: center;
  justify-content: center;
  transition: var(--cs-transition);
  flex-shrink: 0;
}

#cs-group-send-btn:hover {
  background: var(--cs-primary-dark);
}

#cs-group-send-btn:disabled {
  background: var(--cs-border);
  cursor: not-allowed;
}

/* Image Lightbox */
#cs-lightbox {
  position: fixed;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
  background: rgba(0, 0, 0, 0.9);
  display: none;
  align-items: center;
  justify-content: center;
  z-index: 9999999;
  cursor: pointer;
}

#cs-lightbox img {
  max-width: 90%;
  max-height: 90%;
  object-fit: contain;
  border-radius: 8px;
}

#cs-lightbox-close {
  position: absolute;
  top: 20px;
  right: 20px;
  background: none;
  border: none;
  color: white;
  font-size: 32px;
  cursor: pointer;
  width: 48px;
  height: 48px;
  display: flex;
  align-items: center;
  justify-content: center;
  border-radius: 50%;
  transition: var(--cs-transition);
}

#cs-lightbox-close:hover {
  background: rgba(255, 255, 255, 0.2);
}

/* Typing Indicator */
.cs-typing-indicator {
  display: flex;
  align-items: center;
  gap: 4px;
  padding: 8px 12px;
  font-size: 12px;
  color: var(--cs-text-light);
}

.cs-typing-dot {
  width: 6px;
  height: 6px;
  background: var(--cs-text-light);
  border-radius: 50%;
  animation: cs-typing 1.4s infinite;
}

.cs-typing-dot:nth-child(2) {
  animation-delay: 0.2s;
}

.cs-typing-dot:nth-child(3) {
  animation-delay: 0.4s;
}

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

/* Hidden file inputs */
.cs-hidden-input {
  display: none;
}

/* Toast Messages */
#cs-toast {
  position: fixed;
  top: 20px;
  left: 50%;
  transform: translateX(-50%);
  background: var(--cs-text);
  color: white;
  padding: 12px 24px;
  border-radius: var(--cs-radius);
  font-size: 14px;
  z-index: 99999999;
  display: none;
  animation: cs-toast-in 0.3s ease;
}

@keyframes cs-toast-in {
  from {
    opacity: 0;
    transform: translateX(-50%) translateY(-20px);
  }
  to {
    opacity: 1;
    transform: translateX(-50%) translateY(0);
  }
}

/* Mobile Responsive */
@media (max-width: 480px) {
  #cs-widget-container {
    bottom: 10px;
    right: 10px;
  }

  #cs-chat-window {
    width: calc(100vw - 20px);
    height: calc(100vh - 100px);
    right: -10px;
    bottom: 70px;
    border-radius: 12px 12px 0 0;
  }

  #cs-chat-btn {
    width: 52px;
    height: 52px;
  }

  #cs-chat-btn .cs-icon {
    font-size: 24px;
  }

  .cs-form-input {
    font-size: 16px; /* Prevents iOS zoom on focus */
  }

  #cs-message-input,
  #cs-group-msg-input {
    font-size: 16px; /* Prevents iOS zoom on focus */
  }
}

/* Keyboard-aware adjustments for mobile */
@media (max-width: 480px) and (max-height: 500px) {
  #cs-chat-window {
    height: 100vh;
    bottom: 0;
    right: 0;
    border-radius: 0;
  }

  #cs-messages,
  #cs-group-messages {
    min-height: 100px;
  }
}

/* Dark mode support */
@media (prefers-color-scheme: dark) {
  :root {
    --cs-bg: #1E1E1E;
    --cs-bg-light: #2D2D2D;
    --cs-bg-dark: #3D3D3D;
    --cs-text: #E0E0E0;
    --cs-text-secondary: #B0B0B0;
    --cs-text-light: #808080;
    --cs-border: #404040;
  }

  .cs-message-bubble,
  .cs-group-msg-bubble {
    box-shadow: 0 1px 2px rgba(0, 0, 0, 0.2);
  }

  #cs-chat-header {
    background: linear-gradient(135deg, #2D2D2D, #1E1E1E);
    border-bottom: 1px solid var(--cs-border);
  }
}
