/* ==================== Reset & Base ==================== */
* {
  margin: 0;
  padding: 0;
  box-sizing: border-box;
  -webkit-tap-highlight-color: transparent;
}

html, body {
  height: 100%;
  font-family: -apple-system, BlinkMacSystemFont, 'PingFang SC', 'Hiragino Sans GB',
    'Microsoft YaHei', 'Helvetica Neue', Arial, sans-serif;
  font-size: 15px;
  color: #333;
  background: #f5f5f5;
  overflow: hidden;
}

#app {
  display: flex;
  flex-direction: column;
  height: 100%;
  max-width: 600px;
  margin: 0 auto;
  background: #fff;
  position: relative;
}

/* ==================== Header ==================== */
.chat-header {
  display: flex;
  align-items: center;
  padding: 12px 16px;
  background: linear-gradient(135deg, #4a90d9 0%, #357abd 100%);
  color: #fff;
  flex-shrink: 0;
  box-shadow: 0 2px 8px rgba(0,0,0,0.1);
  z-index: 10;
}

.header-left {
  display: flex;
  align-items: center;
  gap: 10px;
}

.agent-avatar {
  width: 40px;
  height: 40px;
  border-radius: 50%;
  background: rgba(255,255,255,0.2);
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 18px;
  font-weight: bold;
}

.header-info {
  display: flex;
  flex-direction: column;
}

.agent-name {
  font-size: 16px;
  font-weight: 600;
}

.agent-status {
  font-size: 12px;
  opacity: 0.85;
}

/* ==================== Messages ==================== */
.chat-messages {
  flex: 1;
  overflow-y: auto;
  padding: 16px;
  scroll-behavior: smooth;
  -webkit-overflow-scrolling: touch;
}

.messages-list {
  display: flex;
  flex-direction: column;
  gap: 12px;
}

.loading-more {
  text-align: center;
  color: #999;
  font-size: 12px;
  padding: 8px 0;
}

.message-item {
  display: flex;
  gap: 8px;
  max-width: 85%;
  animation: fadeIn 0.2s ease;
}

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

.message-item.customer {
  align-self: flex-end;
  flex-direction: row-reverse;
}

.message-item.agent {
  align-self: flex-start;
}

.message-item.system {
  align-self: center;
  max-width: 100%;
}

.msg-avatar {
  width: 36px;
  height: 36px;
  border-radius: 50%;
  flex-shrink: 0;
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 14px;
  color: #fff;
}

.msg-avatar.agent-avatar-msg {
  background: #4a90d9;
}

.msg-avatar.customer-avatar-msg {
  background: #67c23a;
}

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

.msg-sender {
  font-size: 11px;
  color: #999;
  padding: 0 4px;
}

.message-item.customer .msg-sender {
  text-align: right;
}

.msg-bubble {
  padding: 10px 14px;
  border-radius: 16px;
  line-height: 1.5;
  word-break: break-word;
  position: relative;
  font-size: 15px;
}

.message-item.agent .msg-bubble {
  background: #fff;
  border: 1px solid #e8e8e8;
  border-top-left-radius: 4px;
  color: #333;
}

.message-item.customer .msg-bubble {
  background: #4a90d9;
  color: #fff;
  border-top-right-radius: 4px;
}

/* Quote in message */
.msg-quote {
  background: rgba(0,0,0,0.05);
  border-left: 3px solid #4a90d9;
  padding: 6px 10px;
  margin-bottom: 6px;
  border-radius: 4px;
  font-size: 12px;
  color: #666;
}

.msg-quote-sender {
  font-weight: 600;
  color: #4a90d9;
  font-size: 11px;
}

.msg-quote-content {
  margin-top: 2px;
  overflow: hidden;
  text-overflow: ellipsis;
  display: -webkit-box;
  -webkit-line-clamp: 2;
  -webkit-box-orient: vertical;
}

.msg-time {
  font-size: 11px;
  color: #bbb;
  padding: 0 4px;
}

.message-item.customer .msg-time {
  text-align: right;
}

/* System message */
.system-message {
  background: rgba(0,0,0,0.04);
  color: #999;
  font-size: 12px;
  padding: 6px 14px;
  border-radius: 20px;
  text-align: center;
}

/* Message status */
.msg-status {
  font-size: 11px;
  color: #999;
  padding: 0 4px;
}

.msg-status.failed {
  color: #f56c6c;
}

.msg-status.sending {
  color: #909399;
}

.msg-status .retry-btn {
  background: none;
  border: none;
  color: #f56c6c;
  font-size: 12px;
  cursor: pointer;
  padding: 0 4px;
  text-decoration: underline;
}

/* Image message */
.msg-image {
  max-width: 200px;
  border-radius: 8px;
  cursor: pointer;
}

/* ==================== Typing Indicator ==================== */
.typing-indicator {
  display: flex;
  align-items: center;
  gap: 4px;
  padding: 8px 16px;
  font-size: 12px;
  color: #999;
}

.typing-dot {
  width: 6px;
  height: 6px;
  border-radius: 50%;
  background: #999;
  animation: typingBounce 1.4s infinite;
}

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

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

/* ==================== Quote Bar ==================== */
.quote-bar {
  display: flex;
  align-items: center;
  padding: 8px 16px;
  background: #f8f8f8;
  border-top: 1px solid #eee;
}

.quote-content {
  flex: 1;
  display: flex;
  flex-direction: column;
  gap: 2px;
  overflow: hidden;
}

.quote-label {
  font-size: 11px;
  color: #999;
}

.quote-text {
  font-size: 13px;
  color: #666;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.quote-close {
  background: none;
  border: none;
  font-size: 18px;
  color: #999;
  cursor: pointer;
  padding: 4px 8px;
}

/* ==================== Input Area ==================== */
.chat-input-area {
  flex-shrink: 0;
  background: #fff;
  border-top: 1px solid #eee;
  padding-bottom: env(safe-area-inset-bottom);
}

.input-toolbar {
  display: flex;
  padding: 8px 12px 4px;
  gap: 12px;
}

.toolbar-btn {
  background: none;
  border: none;
  font-size: 22px;
  cursor: pointer;
  padding: 4px;
  border-radius: 8px;
  transition: background 0.2s;
}

.toolbar-btn:active {
  background: #f0f0f0;
}

.input-row {
  display: flex;
  align-items: flex-end;
  padding: 4px 12px 12px;
  gap: 8px;
}

#messageInput {
  flex: 1;
  border: 1px solid #e0e0e0;
  border-radius: 20px;
  padding: 10px 16px;
  font-size: 15px;
  font-family: inherit;
  resize: none;
  max-height: 100px;
  outline: none;
  line-height: 1.4;
  transition: border-color 0.2s;
}

#messageInput:focus {
  border-color: #4a90d9;
}

.send-btn {
  background: #4a90d9;
  color: #fff;
  border: none;
  border-radius: 20px;
  padding: 10px 20px;
  font-size: 15px;
  font-weight: 600;
  cursor: pointer;
  flex-shrink: 0;
  transition: background 0.2s, opacity 0.2s;
}

.send-btn:active {
  background: #357abd;
}

.send-btn:disabled {
  opacity: 0.5;
  cursor: not-allowed;
}

/* ==================== Emoji Panel ==================== */
.emoji-panel {
  background: #f8f8f8;
  border-top: 1px solid #eee;
  padding: 10px;
  max-height: 180px;
  overflow-y: auto;
  flex-shrink: 0;
}

.emoji-grid {
  display: grid;
  grid-template-columns: repeat(8, 1fr);
  gap: 8px;
}

.emoji-item {
  font-size: 24px;
  text-align: center;
  cursor: pointer;
  padding: 4px;
  border-radius: 8px;
  transition: background 0.2s;
}

.emoji-item:active {
  background: #e0e0e0;
}

/* ==================== Leave Message ==================== */
.leave-message-overlay {
  position: fixed;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  background: rgba(0,0,0,0.5);
  display: flex;
  align-items: center;
  justify-content: center;
  z-index: 100;
}

.leave-message-form {
  background: #fff;
  border-radius: 16px;
  padding: 32px 24px;
  width: 90%;
  max-width: 400px;
  display: flex;
  flex-direction: column;
  gap: 16px;
}

.leave-message-form h3 {
  font-size: 18px;
  text-align: center;
  color: #333;
}

.leave-message-form p {
  font-size: 13px;
  color: #999;
  text-align: center;
}

.leave-message-form textarea,
.leave-message-form input {
  border: 1px solid #e0e0e0;
  border-radius: 8px;
  padding: 12px;
  font-size: 15px;
  font-family: inherit;
  outline: none;
}

.leave-message-form textarea:focus,
.leave-message-form input:focus {
  border-color: #4a90d9;
}

.submit-btn {
  background: #4a90d9;
  color: #fff;
  border: none;
  border-radius: 8px;
  padding: 14px;
  font-size: 16px;
  font-weight: 600;
  cursor: pointer;
  transition: background 0.2s;
}

.submit-btn:active {
  background: #357abd;
}

/* ==================== Image Preview ==================== */
.image-preview-overlay {
  position: fixed;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  background: rgba(0,0,0,0.9);
  display: flex;
  align-items: center;
  justify-content: center;
  z-index: 200;
}

.image-preview-overlay img {
  max-width: 90%;
  max-height: 80vh;
  border-radius: 8px;
}

.preview-close {
  position: absolute;
  top: 16px;
  right: 16px;
  background: rgba(255,255,255,0.2);
  border: none;
  color: #fff;
  font-size: 24px;
  width: 40px;
  height: 40px;
  border-radius: 50%;
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: center;
}

/* ==================== Scrollbar ==================== */
.chat-messages::-webkit-scrollbar {
  width: 4px;
}

.chat-messages::-webkit-scrollbar-track {
  background: transparent;
}

.chat-messages::-webkit-scrollbar-thumb {
  background: #ccc;
  border-radius: 2px;
}

/* ==================== Responsive (PC) ==================== */
@media (min-width: 601px) {
  body {
    background: #e8ecf1;
  }

  #app {
    border-radius: 16px;
    margin-top: 20px;
    margin-bottom: 20px;
    height: calc(100% - 40px);
    box-shadow: 0 4px 24px rgba(0,0,0,0.12);
  }

  .chat-header {
    border-radius: 16px 16px 0 0;
  }
}
