/* ==================== 全局样式 ==================== */
:root {
  --primary: #4ecdc4;
  --primary-dark: #3ab5ad;
  --primary-light: #a8edea;
  --primary-bg: #e8faf8;
  --bubble-ai: #ffffff;
  --bubble-user: #95ec69;
  --bubble-user-dark: #89d961;
  --bg-chat: #ededf0;
  --bg-header: #2c2c2e;
  --text-primary: #1a1a1a;
  --text-secondary: #888;
  --text-light: #fff;
  --shadow: 0 2px 12px rgba(0, 0, 0, 0.08);
  --radius: 12px;
  --max-width: 768px;
}

* {
  margin: 0;
  padding: 0;
  box-sizing: border-box;
  -webkit-tap-highlight-color: transparent;
}

html, body {
  height: 100%;
  font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', 'PingFang SC', 'Hiragino Sans GB', 'Microsoft YaHei', sans-serif;
  font-size: 15px;
  color: var(--text-primary);
  background: var(--bg-chat);
  overflow: hidden;
}

/* ==================== 主容器 ==================== */
#app {
  max-width: var(--max-width);
  margin: 0 auto;
  height: 100%;
  display: flex;
  flex-direction: column;
  background: var(--bg-chat);
  position: relative;
  box-shadow: 0 0 40px rgba(0, 0, 0, 0.1);
}

@media (min-width: 481px) {
  body {
    background: #1a1a2e;
  }
  #app {
    border-left: 1px solid #ddd;
    border-right: 1px solid #ddd;
  }
}

/* ==================== 顶部导航 ==================== */
.chat-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 10px 14px;
  background: var(--bg-header);
  color: var(--text-light);
  z-index: 10;
  flex-shrink: 0;
  min-height: 56px;
}

.header-left {
  width: 40px;
}

.btn-icon {
  background: none;
  border: none;
  color: var(--text-light);
  font-size: 20px;
  cursor: pointer;
  padding: 4px 8px;
  border-radius: 6px;
  transition: background 0.2s;
}

.btn-icon:hover {
  background: rgba(255, 255, 255, 0.1);
}

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

.avatar-small {
  font-size: 28px;
  line-height: 1;
}

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

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

.header-status {
  font-size: 11px;
  color: var(--primary);
  transition: color 0.3s;
}

.header-status.typing {
  color: #ffd700;
}

.header-right {
  width: auto;
  display: flex;
  align-items: center;
}

.token-badge {
  font-size: 11px;
  background: rgba(78, 205, 196, 0.2);
  color: var(--primary);
  padding: 3px 8px;
  border-radius: 10px;
  white-space: nowrap;
}

.token-badge.low {
  background: rgba(255, 107, 107, 0.2);
  color: #ff6b6b;
}

/* ==================== 侧边菜单 ==================== */
.menu-overlay {
  position: fixed;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  background: rgba(0, 0, 0, 0.4);
  z-index: 99;
  opacity: 0;
  pointer-events: none;
  transition: opacity 0.3s;
}

.menu-overlay.show {
  opacity: 1;
  pointer-events: auto;
}

.side-menu {
  position: fixed;
  top: 0;
  left: 0;
  width: 260px;
  height: 100%;
  background: #fff;
  z-index: 100;
  transform: translateX(-100%);
  transition: transform 0.3s ease;
  display: flex;
  flex-direction: column;
}

.side-menu.show {
  transform: translateX(0);
}

.menu-header {
  padding: 40px 20px 20px;
  background: linear-gradient(135deg, var(--bg-header), #3a3a3c);
  color: white;
  display: flex;
  align-items: center;
  gap: 12px;
}

.menu-avatar {
  font-size: 36px;
}

.menu-title {
  font-size: 18px;
  font-weight: 600;
}

.menu-body {
  padding: 16px;
  flex: 1;
}

.menu-user {
  padding: 12px 16px;
  background: var(--primary-bg);
  border-radius: 10px;
  margin-bottom: 16px;
  font-size: 14px;
  color: var(--primary-dark);
  font-weight: 500;
}

.menu-item {
  padding: 14px 16px;
  cursor: pointer;
  border-radius: 10px;
  transition: background 0.2s;
  font-size: 14px;
  margin-bottom: 4px;
}

.menu-item:hover {
  background: #f5f5f5;
}

.menu-item:active {
  background: #eee;
}

/* ==================== 消息区域 ==================== */
.chat-messages {
  flex: 1;
  overflow-y: auto;
  padding: 12px 14px;
  display: flex;
  flex-direction: column;
  gap: 10px;
  -webkit-overflow-scrolling: touch;
  scroll-behavior: smooth;
}

.welcome-message {
  text-align: center;
  padding: 40px 20px;
  color: var(--text-secondary);
  margin: auto 0;
}

.welcome-avatar {
  font-size: 56px;
  margin-bottom: 12px;
  animation: float 3s ease-in-out infinite;
}

@keyframes float {
  0%, 100% { transform: translateY(0); }
  50% { transform: translateY(-10px); }
}

.welcome-message p {
  font-size: 16px;
  margin-bottom: 4px;
}

.welcome-sub {
  font-size: 13px !important;
  color: #aaa;
}

/* 消息气泡 */
.message-row {
  display: flex;
  align-items: flex-start;
  gap: 8px;
  max-width: 100%;
  animation: fadeInUp 0.25s ease;
}

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

.message-row.user {
  flex-direction: row-reverse;
}

.msg-avatar {
  font-size: 32px;
  line-height: 1;
  flex-shrink: 0;
  margin-top: 2px;
}

.msg-bubble {
  max-width: 75%;
  padding: 10px 14px;
  border-radius: var(--radius);
  font-size: 15px;
  line-height: 1.6;
  word-break: break-word;
  position: relative;
}

.message-row.assistant .msg-bubble {
  background: var(--bubble-ai);
  border-top-left-radius: 4px;
  box-shadow: 0 1px 2px rgba(0, 0, 0, 0.06);
}

.message-row.user .msg-bubble {
  background: var(--bubble-user);
  border-top-right-radius: 4px;
}

/* 消息内容中的 markdown */
.msg-bubble p {
  margin: 0 0 6px 0;
}

.msg-bubble p:last-child {
  margin-bottom: 0;
}

.msg-bubble strong {
  font-weight: 600;
}

.msg-bubble code {
  background: rgba(0, 0, 0, 0.06);
  padding: 1px 4px;
  border-radius: 3px;
  font-size: 13px;
  font-family: 'SF Mono', Monaco, Consolas, monospace;
}

.msg-bubble pre {
  background: rgba(0, 0, 0, 0.06);
  padding: 8px 12px;
  border-radius: 6px;
  overflow-x: auto;
  margin: 6px 0;
  font-size: 13px;
}

.msg-bubble pre code {
  background: none;
  padding: 0;
}

.msg-bubble ul, .msg-bubble ol {
  margin: 4px 0;
  padding-left: 20px;
}

.msg-bubble li {
  margin: 2px 0;
}

/* 搜索状态 */
.search-indicator {
  display: flex;
  align-items: center;
  gap: 6px;
  padding: 6px 12px;
  background: rgba(78, 205, 196, 0.1);
  border-radius: 16px;
  font-size: 12px;
  color: var(--primary-dark);
  margin: 4px 0;
  animation: fadeInUp 0.3s ease;
}

.search-indicator .spinner-sm {
  width: 12px;
  height: 12px;
  border: 2px solid var(--primary-light);
  border-top-color: var(--primary);
  border-radius: 50%;
  animation: spin 0.8s linear infinite;
}

/* 输入指示器 */
.typing-indicator {
  display: flex;
  align-items: flex-start;
  gap: 8px;
  animation: fadeInUp 0.25s ease;
}

.typing-dots {
  background: var(--bubble-ai);
  padding: 12px 18px;
  border-radius: var(--radius);
  border-top-left-radius: 4px;
  display: flex;
  gap: 5px;
  box-shadow: 0 1px 2px rgba(0, 0, 0, 0.06);
}

.typing-dots span {
  width: 8px;
  height: 8px;
  background: #ccc;
  border-radius: 50%;
  animation: bounce 1.4s ease-in-out infinite;
}

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

@keyframes bounce {
  0%, 60%, 100% { transform: translateY(0); }
  30% { transform: translateY(-8px); }
}

@keyframes spin {
  to { transform: rotate(360deg); }
}

/* ==================== 输入区域 ==================== */
.chat-input-area {
  padding: 8px 10px;
  padding-bottom: max(8px, env(safe-area-inset-bottom));
  background: #f7f7f8;
  border-top: 1px solid #e5e5e5;
  flex-shrink: 0;
}

.input-wrapper {
  display: flex;
  align-items: flex-end;
  gap: 8px;
  background: #fff;
  border-radius: 20px;
  padding: 6px 6px 6px 16px;
  box-shadow: 0 1px 4px rgba(0, 0, 0, 0.06);
}

#messageInput {
  flex: 1;
  border: none;
  outline: none;
  resize: none;
  font-size: 15px;
  line-height: 1.5;
  max-height: 120px;
  min-height: 24px;
  font-family: inherit;
  background: transparent;
  color: var(--text-primary);
}

#messageInput::placeholder {
  color: #bbb;
}

.send-btn {
  width: 36px;
  height: 36px;
  border-radius: 50%;
  border: none;
  background: var(--primary);
  color: white;
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: center;
  flex-shrink: 0;
  transition: all 0.2s;
}

.send-btn:hover:not(:disabled) {
  background: var(--primary-dark);
  transform: scale(1.05);
}

.send-btn:disabled {
  background: #ddd;
  cursor: not-allowed;
}

.send-btn:active:not(:disabled) {
  transform: scale(0.95);
}

/* ==================== 通讯录页面 ==================== */
.contacts-list {
  flex: 1;
  overflow-y: auto;
  -webkit-overflow-scrolling: touch;
  background: var(--bg-chat);
}

.contacts-section-header {
  padding: 10px 16px 6px;
  font-size: 12px;
  color: var(--text-secondary);
  font-weight: 500;
  text-transform: uppercase;
  letter-spacing: 0.5px;
  background: var(--bg-chat);
  position: sticky;
  top: 0;
  z-index: 1;
}

.contact-item {
  display: flex;
  align-items: center;
  padding: 14px 16px;
  background: #fff;
  border-bottom: 1px solid #f0f0f0;
  cursor: pointer;
  transition: background 0.15s;
  gap: 14px;
}

.contact-item:first-of-type {
  border-top: 1px solid #f0f0f0;
}

.contact-item:hover {
  background: #f8f8f8;
}

.contact-item:active {
  background: #f0f0f0;
}

.contact-item.active {
  background: var(--primary-bg);
}

.contact-avatar {
  font-size: 40px;
  line-height: 1;
  flex-shrink: 0;
  width: 48px;
  height: 48px;
  display: flex;
  align-items: center;
  justify-content: center;
  background: #f5f5f5;
  border-radius: 10px;
}

.contact-item.active .contact-avatar {
  background: rgba(78, 205, 196, 0.15);
}

.contact-info {
  flex: 1;
  min-width: 0;
}

.contact-name {
  font-size: 16px;
  font-weight: 500;
  color: var(--text-primary);
  margin-bottom: 3px;
}

.contact-desc {
  font-size: 13px;
  color: var(--text-secondary);
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.contact-arrow {
  font-size: 20px;
  color: #ccc;
  flex-shrink: 0;
}

.contacts-loading,
.contacts-empty {
  text-align: center;
  padding: 60px 20px;
  color: var(--text-secondary);
  font-size: 14px;
}

/* ==================== 认证页面 ==================== */
.auth-body {
  overflow: auto;
  background: linear-gradient(135deg, #1a1a2e 0%, #16213e 50%, #0f3460 100%);
  display: flex;
  align-items: center;
  justify-content: center;
}

.auth-container {
  width: 100%;
  max-width: 400px;
  padding: 20px;
  position: relative;
}

.auth-card {
  background: rgba(255, 255, 255, 0.95);
  backdrop-filter: blur(20px);
  border-radius: 20px;
  padding: 36px 28px 28px;
  box-shadow: var(--shadow);
  position: relative;
  z-index: 2;
}

.auth-header {
  text-align: center;
  margin-bottom: 28px;
}

.auth-avatar {
  font-size: 52px;
  margin-bottom: 8px;
  animation: float 3s ease-in-out infinite;
}

.auth-header h1 {
  font-size: 22px;
  font-weight: 700;
  color: var(--text-primary);
  margin-bottom: 4px;
}

.auth-subtitle {
  color: var(--text-secondary);
  font-size: 14px;
}

.auth-form {
  display: flex;
  flex-direction: column;
  gap: 14px;
}

.form-group input {
  width: 100%;
  padding: 14px 16px;
  border: 1.5px solid #e8e8e8;
  border-radius: 12px;
  font-size: 15px;
  outline: none;
  transition: border-color 0.3s, box-shadow 0.3s;
  font-family: inherit;
  background: #fafafa;
}

.form-group input:focus {
  border-color: var(--primary);
  box-shadow: 0 0 0 3px rgba(78, 205, 196, 0.15);
  background: #fff;
}

.form-error {
  color: #ff6b6b;
  font-size: 13px;
  min-height: 18px;
  text-align: center;
}

.auth-btn {
  width: 100%;
  padding: 14px;
  background: linear-gradient(135deg, var(--primary), var(--primary-dark));
  color: white;
  border: none;
  border-radius: 12px;
  font-size: 16px;
  font-weight: 600;
  cursor: pointer;
  transition: all 0.3s;
  font-family: inherit;
}

.auth-btn:hover:not(:disabled) {
  transform: translateY(-1px);
  box-shadow: 0 4px 12px rgba(78, 205, 196, 0.4);
}

.auth-btn:disabled {
  opacity: 0.6;
  cursor: not-allowed;
}

.auth-footer {
  text-align: center;
  margin-top: 20px;
  font-size: 14px;
  color: var(--text-secondary);
}

.auth-footer a {
  color: var(--primary-dark);
  text-decoration: none;
  font-weight: 500;
}

.auth-footer a:hover {
  text-decoration: underline;
}

/* 萤火虫装饰 */
.auth-decoration {
  position: fixed;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  pointer-events: none;
  z-index: 1;
  overflow: hidden;
}

.firefly {
  position: absolute;
  width: 6px;
  height: 6px;
  background: var(--primary);
  border-radius: 50%;
  box-shadow: 0 0 10px var(--primary), 0 0 20px var(--primary-light);
  animation: fly linear infinite;
  opacity: 0;
}

.f1 { left: 10%; top: 20%; animation-duration: 8s; animation-delay: 0s; }
.f2 { left: 80%; top: 40%; animation-duration: 11s; animation-delay: 2s; }
.f3 { left: 40%; top: 70%; animation-duration: 9s; animation-delay: 4s; }
.f4 { left: 60%; top: 15%; animation-duration: 12s; animation-delay: 1s; }
.f5 { left: 25%; top: 55%; animation-duration: 10s; animation-delay: 3s; }

@keyframes fly {
  0% {
    opacity: 0;
    transform: translate(0, 0) scale(0.5);
  }
  10% {
    opacity: 0.8;
  }
  50% {
    opacity: 0.4;
    transform: translate(60px, -40px) scale(1);
  }
  80% {
    opacity: 0.6;
  }
  100% {
    opacity: 0;
    transform: translate(-30px, -80px) scale(0.3);
  }
}

/* ==================== 滚动条 ==================== */
.chat-messages::-webkit-scrollbar,
.contacts-list::-webkit-scrollbar {
  width: 4px;
}

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

.chat-messages::-webkit-scrollbar-thumb,
.contacts-list::-webkit-scrollbar-thumb {
  background: #ccc;
  border-radius: 4px;
}

/* ==================== 额度不足提示 ==================== */
.quota-warning {
  text-align: center;
  padding: 12px 16px;
  background: #fff3f3;
  border-radius: 10px;
  color: #e74c3c;
  font-size: 13px;
  margin: 8px 0;
}

/* ==================== 消息时间 ==================== */
.msg-time {
  font-size: 11px;
  color: #aaa;
  text-align: center;
  padding: 8px 0 4px;
}
