/* ======== 基础 ======== */
* { box-sizing: border-box; margin: 0; padding: 0; }

:root {
  --bg-1: #fef6ff;
  --bg-2: #e0f4ff;
  --bg-3: #fff4e0;
  --primary: #ff6fa3;
  --primary-dark: #e85789;
  --accent: #4a90e2;
  --text: #333;
  --text-light: #777;
  --card: #ffffff;
  --border: #ffd9e8;
  --shadow: 0 8px 30px rgba(255, 111, 163, 0.15);
  --radius: 18px;
}

html, body {
  height: 100%;
  font-family: 'PingFang SC', 'Microsoft YaHei', 'Segoe UI', sans-serif;
  color: var(--text);
  background: linear-gradient(135deg, var(--bg-1) 0%, var(--bg-2) 50%, var(--bg-3) 100%);
  background-attachment: fixed;
  overflow-x: hidden;
}

body {
  min-height: 100%;
}

body::before {
  content: '';
  position: fixed;
  inset: 0;
  background-image:
    radial-gradient(circle at 10% 20%, rgba(255, 200, 220, 0.4) 0, transparent 30%),
    radial-gradient(circle at 80% 70%, rgba(180, 220, 255, 0.4) 0, transparent 30%),
    radial-gradient(circle at 60% 10%, rgba(255, 240, 200, 0.4) 0, transparent 25%);
  pointer-events: none;
  z-index: 0;
}

.app {
  position: relative;
  z-index: 1;
  min-height: 100vh;
  display: flex;
  flex-direction: column;
  max-width: 1200px;
  margin: 0 auto;
  padding: 16px;
}

/* ======== 顶栏 ======== */
.topbar {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 12px 20px;
  background: rgba(255, 255, 255, 0.7);
  backdrop-filter: blur(10px);
  border-radius: var(--radius);
  box-shadow: var(--shadow);
  margin-bottom: 16px;
}
.logo {
  display: flex;
  align-items: center;
  gap: 12px;
}
.logo-emoji {
  font-size: 32px;
  animation: wobble 3s ease-in-out infinite;
}
.logo h1 {
  font-size: 22px;
  background: linear-gradient(90deg, var(--primary), var(--accent));
  -webkit-background-clip: text;
  -webkit-text-fill-color: transparent;
  background-clip: text;
}
.badge {
  font-size: 11px;
  padding: 3px 10px;
  border-radius: 12px;
  background: #f0f0f0;
  color: #888;
  font-weight: 600;
  letter-spacing: 0.3px;
}
.badge.edge {
  background: linear-gradient(135deg, #b8e0ff, #d6c0ff);
  color: #1a3a5e;
}

/* ======== 按钮 ======== */
.icon-btn {
  background: rgba(255, 255, 255, 0.8);
  border: 1px solid var(--border);
  border-radius: 12px;
  width: 40px;
  height: 40px;
  font-size: 18px;
  cursor: pointer;
  transition: all 0.2s;
}
.icon-btn:hover:not(:disabled) {
  background: var(--primary);
  color: white;
  transform: translateY(-2px);
}
.icon-btn:disabled {
  opacity: 0.4;
  cursor: not-allowed;
}

.primary-btn {
  background: linear-gradient(135deg, var(--primary), var(--primary-dark));
  color: white;
  border: none;
  padding: 12px 22px;
  border-radius: 12px;
  font-size: 15px;
  font-weight: 600;
  cursor: pointer;
  box-shadow: 0 4px 12px rgba(255, 111, 163, 0.4);
  transition: all 0.2s;
}
.primary-btn:hover { transform: translateY(-2px); box-shadow: 0 6px 16px rgba(255, 111, 163, 0.5); }
.primary-btn:active { transform: translateY(0); }
.primary-btn.small { padding: 8px 16px; font-size: 13px; }
.primary-btn.full { width: 100%; padding: 14px; }

.link-btn {
  background: none;
  border: none;
  color: var(--text-light);
  font-size: 12px;
  cursor: pointer;
  padding: 4px 8px;
  border-radius: 6px;
}
.link-btn:hover { color: var(--primary); background: #fff5f9; }

/* ======== 主体布局 ======== */
.main {
  flex: 1;
  display: grid;
  grid-template-columns: 1fr 1.2fr;
  gap: 16px;
}

@media (max-width: 900px) {
  .main { grid-template-columns: 1fr; }
}

section {
  background: var(--card);
  border-radius: var(--radius);
  box-shadow: var(--shadow);
  padding: 20px;
  display: flex;
  flex-direction: column;
}

/* ======== 角色选择条 ======== */
.character-bar {
  width: 100%;
  display: flex;
  gap: 8px;
  padding: 4px;
  margin-bottom: 12px;
  overflow-x: auto;
  scrollbar-width: thin;
}
.character-bar::-webkit-scrollbar { height: 4px; }
.character-bar::-webkit-scrollbar-thumb { background: var(--border); border-radius: 2px; }

.char-tab {
  flex-shrink: 0;
  min-width: 64px;
  background: #f8f8f8;
  border: 2px solid transparent;
  border-radius: 12px;
  padding: 8px 10px;
  cursor: pointer;
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 4px;
  transition: all 0.2s;
}
.char-tab:hover {
  background: #fff5f9;
  border-color: var(--border);
  transform: translateY(-2px);
}
.char-tab.active {
  background: linear-gradient(135deg, #fff5f9, #ffe1ec);
  border-color: var(--primary);
  box-shadow: 0 4px 12px rgba(255, 111, 163, 0.25);
}
.char-emoji { font-size: 22px; }
.char-name  { font-size: 12px; font-weight: 600; color: var(--text); }

/* ======== 数字人 ======== */
.avatar-panel { align-items: center; }

.avatar-wrap {
  position: relative;
  width: 240px;
  height: 280px;
  display: flex;
  align-items: center;
  justify-content: center;
  margin: 0 0 8px;
}

#avatar {
  width: 100%;
  height: 100%;
  filter: drop-shadow(0 10px 20px rgba(255, 111, 163, 0.25));
  animation: bob 3s ease-in-out infinite;
  transform-origin: center bottom;
}

.halo {
  position: absolute;
  inset: -10px;
  border-radius: 50%;
  background: radial-gradient(circle, rgba(255, 200, 220, 0.5), transparent 60%);
  opacity: 0;
  transition: opacity 0.4s;
  z-index: -1;
}
.avatar-wrap.listening .halo {
  opacity: 1;
  background: radial-gradient(circle, rgba(255, 100, 150, 0.5), transparent 60%);
  animation: pulse 1.4s ease-in-out infinite;
}
.avatar-wrap.thinking .halo {
  opacity: 1;
  background: radial-gradient(circle, rgba(180, 140, 255, 0.5), transparent 60%);
  animation: pulse 1.8s ease-in-out infinite;
}
.avatar-wrap.speaking .halo {
  opacity: 1;
  background: radial-gradient(circle, rgba(100, 200, 255, 0.5), transparent 60%);
  animation: pulse 0.6s ease-in-out infinite;
}

/* 状态 */
.status {
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 6px 14px;
  background: #f8f8f8;
  border-radius: 20px;
  font-size: 13px;
  color: var(--text-light);
  margin-bottom: 14px;
  max-width: 100%;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}
.status-dot {
  width: 8px;
  height: 8px;
  border-radius: 50%;
  background: #aaa;
  flex-shrink: 0;
}
.status.listening .status-dot { background: #ff4d6d; box-shadow: 0 0 0 4px rgba(255, 77, 109, 0.2); animation: blink 1s infinite; }
.status.thinking .status-dot { background: #b48cff; animation: blink 1s infinite; }
.status.speaking .status-dot { background: #4a90e2; animation: blink 0.4s infinite; }

/* 角色背景设定 */
.persona-card {
  width: 100%;
  background: linear-gradient(135deg, #fff5f9, #fff);
  border: 2px dashed var(--border);
  border-radius: 14px;
  padding: 12px;
  margin-top: 4px;
}
.persona-head {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 8px;
}
.persona-card label {
  font-weight: 600;
  color: var(--primary-dark);
  font-size: 13px;
}
.persona-card textarea {
  width: 100%;
  border: 1px solid var(--border);
  border-radius: 10px;
  padding: 8px;
  font-family: inherit;
  font-size: 12px;
  resize: vertical;
  background: #fff;
  margin-bottom: 8px;
  color: var(--text);
}
.persona-card textarea:focus {
  outline: none;
  border-color: var(--primary);
  box-shadow: 0 0 0 3px rgba(255, 111, 163, 0.15);
}

/* ======== 聊天 ======== */
.chat-panel { padding: 0; overflow: hidden; }

.chat-history {
  flex: 1;
  overflow-y: auto;
  padding: 20px;
  min-height: 400px;
  max-height: 65vh;
  display: flex;
  flex-direction: column;
  gap: 12px;
}
.chat-history::-webkit-scrollbar { width: 6px; }
.chat-history::-webkit-scrollbar-thumb { background: var(--border); border-radius: 3px; }

.welcome {
  margin: auto;
  text-align: center;
  color: var(--text-light);
  padding: 20px;
}
.welcome-emoji { font-size: 56px; margin-bottom: 12px; animation: wobble 2s infinite; }
.welcome p { margin: 6px 0; }
.welcome .hint { font-size: 13px; opacity: 0.7; }

.msg {
  display: flex;
  gap: 10px;
  max-width: 85%;
  animation: slideIn 0.3s ease-out;
}
.msg.user { align-self: flex-end; flex-direction: row-reverse; }
.msg .avatar-mini {
  width: 36px;
  height: 36px;
  border-radius: 50%;
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 20px;
  flex-shrink: 0;
}
.msg.user .avatar-mini { background: linear-gradient(135deg, #4a90e2, #6db3ff); color: white; }
.msg.bot .avatar-mini  { background: linear-gradient(135deg, var(--primary), #ffb6c1); color: white; }
.msg .bubble {
  background: #f5f5f5;
  padding: 10px 14px;
  border-radius: 16px;
  font-size: 15px;
  line-height: 1.5;
  word-break: break-word;
  white-space: pre-wrap;
  min-height: 24px;
}
.msg.user .bubble { background: linear-gradient(135deg, #d6eaff, #b8d9ff); color: #1a3a5e; border-bottom-right-radius: 4px; }
.msg.bot .bubble  { background: linear-gradient(135deg, #fff0f5, #ffe1ec); color: #5e1a3a; border-bottom-left-radius: 4px; }

.msg.bot .bubble.thinking::after {
  content: '...';
  display: inline-block;
  animation: dots 1s infinite;
}

/* ======== 输入区 ======== */
.input-area {
  padding: 14px 20px 18px;
  border-top: 1px solid #f0f0f0;
  background: #fafafa;
  border-radius: 0 0 var(--radius) var(--radius);
}

/* 教育模式 */
.mode-bar {
  display: flex;
  gap: 6px;
  overflow-x: auto;
  padding: 4px 2px 8px;
  margin-bottom: 8px;
  scrollbar-width: thin;
}
.mode-bar::-webkit-scrollbar { height: 4px; }
.mode-bar::-webkit-scrollbar-thumb { background: var(--border); border-radius: 2px; }
.mode-chip {
  flex-shrink: 0;
  background: white;
  border: 1px solid #e5e5e5;
  border-radius: 20px;
  padding: 6px 12px;
  font-size: 13px;
  color: var(--text);
  cursor: pointer;
  transition: all 0.2s;
  white-space: nowrap;
}
.mode-chip:hover { border-color: var(--primary); transform: translateY(-1px); }
.mode-chip.active {
  background: linear-gradient(135deg, var(--primary), var(--primary-dark));
  color: white;
  border-color: var(--primary);
  box-shadow: 0 4px 10px rgba(255, 111, 163, 0.3);
}

.text-input-row {
  display: flex;
  gap: 8px;
  margin-bottom: 12px;
}
.text-input-row input {
  flex: 1;
  border: 1px solid #e5e5e5;
  border-radius: 12px;
  padding: 12px 16px;
  font-size: 15px;
  background: white;
  color: var(--text);
}
.text-input-row input:focus {
  outline: none;
  border-color: var(--primary);
  box-shadow: 0 0 0 3px rgba(255, 111, 163, 0.15);
}

.voice-controls {
  display: flex;
  gap: 12px;
  align-items: center;
  justify-content: center;
}

.mic-btn {
  flex: 1;
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 10px;
  background: linear-gradient(135deg, var(--primary), var(--primary-dark));
  color: white;
  border: none;
  padding: 14px 24px;
  border-radius: 50px;
  font-size: 16px;
  font-weight: 600;
  cursor: pointer;
  box-shadow: 0 6px 20px rgba(255, 111, 163, 0.4);
  transition: all 0.2s;
  min-height: 56px;
}
.mic-btn:hover { transform: translateY(-2px); }
.mic-btn:active { transform: translateY(0); }
.mic-btn.recording {
  background: linear-gradient(135deg, #ff4d6d, #d63653);
  animation: pulseRing 1s infinite;
}
.mic-btn.recording .mic-icon {
  animation: micShake 0.5s infinite;
}
.mic-icon { font-size: 22px; }

/* ======== 设置弹窗 ======== */
.modal {
  position: fixed;
  inset: 0;
  background: rgba(0, 0, 0, 0.5);
  display: flex;
  align-items: center;
  justify-content: center;
  z-index: 100;
  backdrop-filter: blur(5px);
  padding: 20px;
}
.modal[hidden] { display: none; }
.modal-content {
  background: white;
  border-radius: var(--radius);
  width: 100%;
  max-width: 500px;
  max-height: 90vh;
  display: flex;
  flex-direction: column;
  overflow: hidden;
}
.modal-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 16px 20px;
  border-bottom: 1px solid #f0f0f0;
}
.modal-header h2 { font-size: 18px; }
.modal-body {
  padding: 20px;
  overflow-y: auto;
}
.modal-body hr { border: none; border-top: 1px dashed #e5e5e5; margin: 16px 0; }
.group-title {
  font-size: 14px;
  color: var(--primary-dark);
  margin-bottom: 12px;
  font-weight: 700;
  letter-spacing: 0.5px;
}

.form-group { margin-bottom: 14px; }
.form-group label {
  display: block;
  font-size: 13px;
  font-weight: 600;
  margin-bottom: 6px;
  color: #555;
}
.form-group input, .form-group select {
  width: 100%;
  padding: 10px 12px;
  border: 1px solid #e0e0e0;
  border-radius: 10px;
  font-size: 14px;
  font-family: inherit;
  background: white;
  color: var(--text);
}
.form-group input[type="range"] { padding: 0; }
.form-group input:focus, .form-group select:focus {
  outline: none;
  border-color: var(--primary);
}
.form-group.switch-row {
  display: flex;
  align-items: center;
  gap: 10px;
}
.form-group.switch-row span { font-size: 13px; color: #555; }

/* Switch */
.switch {
  position: relative;
  display: inline-block;
  width: 44px;
  height: 24px;
  flex-shrink: 0;
}
.switch input { opacity: 0; width: 0; height: 0; }
.slider {
  position: absolute;
  inset: 0;
  background: #ccc;
  border-radius: 24px;
  cursor: pointer;
  transition: 0.3s;
}
.slider:before {
  content: '';
  position: absolute;
  height: 18px;
  width: 18px;
  left: 3px;
  top: 3px;
  background: white;
  border-radius: 50%;
  transition: 0.3s;
}
.switch input:checked + .slider {
  background: var(--primary);
}
.switch input:checked + .slider:before {
  transform: translateX(20px);
}

.tips {
  font-size: 12px;
  color: var(--text-light);
  margin-top: 12px;
  text-align: center;
}

/* ======== 动画 ======== */
@keyframes bob {
  0%, 100% { transform: translateY(0) rotate(0); }
  50% { transform: translateY(-6px) rotate(-1deg); }
}
@keyframes wobble {
  0%, 100% { transform: rotate(0); }
  25% { transform: rotate(-10deg); }
  75% { transform: rotate(10deg); }
}
@keyframes pulse {
  0%, 100% { transform: scale(1); opacity: 0.6; }
  50% { transform: scale(1.08); opacity: 1; }
}
@keyframes pulseRing {
  0%, 100% { box-shadow: 0 0 0 0 rgba(255, 77, 109, 0.5); }
  50% { box-shadow: 0 0 0 14px rgba(255, 77, 109, 0); }
}
@keyframes micShake {
  0%, 100% { transform: rotate(0); }
  25% { transform: rotate(-15deg); }
  75% { transform: rotate(15deg); }
}
@keyframes blink {
  0%, 100% { opacity: 1; }
  50% { opacity: 0.4; }
}
@keyframes slideIn {
  from { opacity: 0; transform: translateY(8px); }
  to { opacity: 1; transform: translateY(0); }
}
@keyframes dots {
  0% { content: '.'; }
  33% { content: '..'; }
  66% { content: '...'; }
}

#mouth { transition: d 0.1s ease-out; }

/* SVG transform-origin 默认是 0,0，需要 transform-box 才能基于元素中心 */
#eyeL, #eyeR {
  transform-box: fill-box;
  transform-origin: center;
}
.eye-blink { animation: blinkEye 0.22s ease-in-out; }
@keyframes blinkEye {
  0%, 100% { transform: scaleY(1); }
  50% { transform: scaleY(0.08); }
}

/* ======== 手机优先体验 ======== */
@media (max-width: 720px) {
  html, body {
    height: auto;
    min-height: 100%;
    background-attachment: scroll;
  }

  body::before {
    display: none;
  }

  .app {
    width: 100%;
    min-height: 100dvh;
    padding: 8px;
  }

  .topbar {
    position: sticky;
    top: 0;
    z-index: 20;
    margin-bottom: 8px;
    padding: 8px 10px;
    border-radius: 14px;
  }

  .logo {
    min-width: 0;
    gap: 8px;
  }

  .logo-emoji {
    font-size: 24px;
  }

  .logo h1 {
    font-size: 18px;
    white-space: nowrap;
  }

  .badge {
    display: none;
  }

  .icon-btn {
    width: 40px;
    height: 40px;
    border-radius: 12px;
  }

  .main {
    display: flex;
    flex-direction: column;
    gap: 8px;
  }

  section {
    border-radius: 14px;
    padding: 10px;
  }

  .avatar-panel {
    padding-bottom: 8px;
  }

  .character-bar {
    gap: 6px;
    padding: 2px 0 6px;
    margin-bottom: 4px;
    scroll-snap-type: x proximity;
  }

  .char-tab {
    min-width: 58px;
    min-height: 58px;
    padding: 6px 8px;
    border-radius: 12px;
    scroll-snap-align: start;
  }

  .char-emoji {
    font-size: 20px;
  }

  .char-name {
    max-width: 48px;
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
    font-size: 11px;
  }

  .avatar-wrap {
    width: min(44vw, 160px);
    height: min(52vw, 190px);
    margin: 0 auto;
  }

  .status {
    margin: 2px 0 0;
    padding: 5px 12px;
    font-size: 12px;
  }

  .persona-card {
    margin-top: 10px;
    padding: 10px;
  }

  .persona-card textarea {
    max-height: 110px;
    min-height: 76px;
    font-size: 12px;
  }

  .chat-panel {
    min-height: auto;
    flex: 1;
  }

  .chat-history {
    min-height: 260px;
    max-height: none;
    padding: 12px 10px;
    gap: 10px;
  }

  .welcome {
    padding: 14px 8px;
  }

  .welcome-emoji {
    font-size: 42px;
  }

  .welcome p {
    font-size: 14px;
  }

  .welcome .hint {
    font-size: 12px;
  }

  .msg {
    max-width: 94%;
    gap: 7px;
  }

  .msg .avatar-mini {
    width: 30px;
    height: 30px;
    font-size: 17px;
  }

  .msg .bubble {
    padding: 9px 12px;
    border-radius: 14px;
    font-size: 14px;
    line-height: 1.45;
  }

  .input-area {
    position: static;
    padding: 8px 8px calc(14px + env(safe-area-inset-bottom));
    margin: 0 -10px -10px;
    border-radius: 0 0 14px 14px;
    background: #fafafa;
    box-shadow: none;
  }

  .mode-bar {
    display: none;
  }

  .mode-chip {
    padding: 6px 11px;
    border-radius: 18px;
    font-size: 12px;
  }

  .text-input-row {
    gap: 6px;
    margin-bottom: 8px;
  }

  .text-input-row input {
    min-width: 0;
    height: 44px;
    padding: 10px 12px;
    border-radius: 12px;
    font-size: 16px;
  }

  .text-input-row .primary-btn {
    flex: 0 0 62px;
    padding: 0 12px;
    border-radius: 12px;
    font-size: 14px;
  }

  .voice-controls {
    gap: 8px;
  }

  .mic-btn {
    min-height: 52px;
    padding: 12px 14px;
    border-radius: 18px;
    font-size: 15px;
  }

  .mic-icon {
    font-size: 21px;
  }

  .modal {
    padding: 8px;
    align-items: flex-end;
  }

  .modal-content {
    max-height: 92dvh;
    border-radius: 16px 16px 0 0;
  }

  .modal-header {
    padding: 12px 14px;
  }

  .modal-body {
    padding: 14px;
  }

  .form-group input,
  .form-group select {
    min-height: 44px;
    font-size: 16px;
  }
}

@media (max-width: 380px) {
  .logo h1 {
    font-size: 16px;
  }

  .char-tab {
    min-width: 54px;
  }

  .avatar-wrap {
    width: 138px;
    height: 164px;
  }

  .text-input-row .primary-btn {
    flex-basis: 56px;
  }

  .chat-history {
    padding-bottom: 190px;
  }

  .voice-controls {
    gap: 6px;
  }

  .mic-btn {
    min-width: 0;
  }
}
