:root {
  --pink: #FF69B4;
  --pink-dark: #DB2777;
  --purple: #7C3AED;
  --bg: #0F0B1E;
  --bg-card: #1A1333;
  --text: #F0E6FF;
  --text-muted: #A09CB5;
}

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

html, body {
  height: 100%;
  overflow: hidden;
  font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', 'Hiragino Sans', sans-serif;
  background: var(--bg);
  color: var(--text);
  touch-action: manipulation;
}

.screen { display: none; width: 100%; height: 100%; position: absolute; top: 0; left: 0; }
.screen.active { display: flex; flex-direction: column; animation: fadeIn 0.4s ease; }
@keyframes fadeIn { from { opacity: 0; } to { opacity: 1; } }

/* ===== Language Screen ===== */
#language-screen {
  justify-content: center;
  align-items: center;
  background: linear-gradient(135deg, #0F0B1E 0%, #1A0A2E 50%, #0F0B1E 100%);
  padding: 30px;
}
.lang-header { text-align: center; margin-bottom: 30px; }
.lang-title {
  font-size: 32px;
  font-weight: 700;
  background: linear-gradient(135deg, var(--pink), var(--purple));
  -webkit-background-clip: text;
  -webkit-text-fill-color: transparent;
}
.lang-subtitle { font-size: 16px; color: var(--text-muted); margin-top: 8px; }
.lang-grid {
  display: grid;
  grid-template-columns: repeat(2, 1fr);
  gap: 16px;
  width: 100%;
  max-width: 500px;
}
.lang-btn {
  display: flex;
  align-items: center;
  gap: 14px;
  padding: 22px 26px;
  background: var(--bg-card);
  border: 2px solid rgba(255,105,180,0.2);
  border-radius: 16px;
  color: var(--text);
  font-size: 20px;
  font-weight: 600;
  cursor: pointer;
  transition: all 0.2s;
  user-select: none;
}
.lang-btn:hover, .lang-btn:active {
  background: rgba(255,105,180,0.15);
  border-color: var(--pink);
  transform: scale(1.03);
}
.lang-btn .flag { font-size: 36px; line-height: 1; }

#cafe-select {
  display: none;
  flex-wrap: wrap;
  gap: 10px;
  justify-content: center;
  margin-bottom: 20px;
  max-width: 500px;
}
.cafe-option {
  padding: 8px 16px;
  border-radius: 20px;
  border: 1px solid rgba(255,105,180,0.3);
  background: transparent;
  color: var(--text-muted);
  font-size: 13px;
  cursor: pointer;
}
.cafe-option.active { background: var(--pink); color: white; border-color: var(--pink); }

/* ===== Main Screen ===== */
#main-screen { height: 100%; }

.top-bar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 10px 16px;
  background: var(--bg-card);
  border-bottom: 1px solid rgba(255,105,180,0.15);
  flex-shrink: 0;
}
.top-bar-title {
  font-size: 16px;
  font-weight: 600;
  background: linear-gradient(135deg, var(--pink), var(--purple));
  -webkit-background-clip: text;
  -webkit-text-fill-color: transparent;
}
.top-bar-btns { display: flex; gap: 8px; }
.icon-btn {
  width: 40px;
  height: 40px;
  border-radius: 50%;
  border: 1px solid rgba(255,105,180,0.3);
  background: transparent;
  color: var(--text);
  font-size: 18px;
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: center;
}
.icon-btn:active { background: rgba(255,105,180,0.2); }

.main-content { display: flex; flex: 1; overflow: hidden; }

/* ===== Avatar Area (Left) ===== */
.character-area {
  width: 320px;
  flex-shrink: 0;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: flex-end;
  padding: 10px 15px 0;
  position: relative;
  overflow: hidden;
  background: radial-gradient(ellipse at center bottom, rgba(124,58,237,0.12) 0%, rgba(255,105,180,0.06) 40%, transparent 70%);
}

#character-container {
  width: 100%;
  max-width: 280px;
  flex-shrink: 0;
  flex-grow: 1;
  min-height: 350px;
}

/* Avatar styles */
.avatar-stage {
  position: relative;
  width: 100%;
  height: 100%;
  display: flex;
  flex-direction: column;
  align-items: center;
}
#character-canvas {
  position: relative;
  z-index: 1;
  width: 100%;
  flex-grow: 1;
  min-height: 300px;
}

.avatar-glow {
  position: absolute;
  bottom: 0;
  left: 50%;
  transform: translateX(-50%);
  width: 200px;
  height: 200px;
  border-radius: 50%;
  background: radial-gradient(circle, rgba(255,105,180,0.15) 0%, transparent 70%);
  pointer-events: none;
  transition: all 0.5s;
}
.avatar-glow.glow-speaking {
  width: 240px;
  height: 240px;
  background: radial-gradient(circle, rgba(255,105,180,0.28) 0%, rgba(124,58,237,0.12) 50%, transparent 70%);
  animation: glowPulse 1.5s ease-in-out infinite;
}
.avatar-glow.glow-happy {
  background: radial-gradient(circle, rgba(255,182,210,0.25) 0%, transparent 70%);
}
.avatar-glow.glow-think {
  background: radial-gradient(circle, rgba(124,58,237,0.2) 0%, transparent 70%);
  animation: glowSlow 2s ease-in-out infinite;
}

@keyframes glowPulse {
  0%, 100% { opacity: 0.7; transform: translateX(-50%) scale(1); }
  50% { opacity: 1; transform: translateX(-50%) scale(1.1); }
}
@keyframes glowSlow {
  0%, 100% { opacity: 0.6; }
  50% { opacity: 1; }
}

.avatar-frame {
  position: relative;
  z-index: 1;
}

.avatar-img {
  width: 100%;
  height: auto;
  max-height: 420px;
  object-fit: contain;
  filter: drop-shadow(0 0 20px rgba(255,105,180,0.15));
  animation: avatarIdle 4s ease-in-out infinite;
  transition: filter 0.3s, transform 0.3s;
}
@keyframes avatarIdle {
  0%, 100% { transform: translateY(0); }
  50% { transform: translateY(-6px); }
}

.avatar-img.speaking {
  animation: avatarIdle 4s ease-in-out infinite, avatarSpeak 0.3s ease-in-out infinite alternate;
  filter: drop-shadow(0 0 25px rgba(255,105,180,0.3));
}
@keyframes avatarSpeak {
  0% { transform: translateY(0) scale(1); }
  100% { transform: translateY(-2px) scale(1.008); }
}

.avatar-img.expr-bounce {
  animation: avatarBounce 0.5s ease;
}
@keyframes avatarBounce {
  0% { transform: scale(1); }
  30% { transform: scale(1.06); }
  60% { transform: scale(0.97); }
  100% { transform: scale(1); }
}

.avatar-img.expr-think {
  filter: drop-shadow(0 0 20px rgba(124,58,237,0.25)) brightness(0.95);
}

.avatar-name-tag {
  position: relative;
  z-index: 2;
  margin-top: -8px;
  padding: 4px 20px;
  background: linear-gradient(135deg, var(--pink), var(--purple));
  border-radius: 12px;
  font-size: 13px;
  font-weight: 600;
  color: white;
  letter-spacing: 1px;
}

/* Speech Bubble */
.speech-bubble {
  background: white;
  color: #333;
  border-radius: 18px;
  padding: 14px 18px;
  font-size: 15px;
  line-height: 1.6;
  position: relative;
  margin-top: 10px;
  width: 100%;
  max-height: 120px;
  overflow-y: auto;
  opacity: 0;
  transform: scale(0.9) translateY(5px);
  transition: opacity 0.3s, transform 0.3s;
  box-shadow: 0 4px 24px rgba(255,105,180,0.12);
}
.speech-bubble.visible {
  opacity: 1;
  transform: scale(1) translateY(0);
}
.speech-bubble::before {
  content: '';
  position: absolute;
  top: -8px;
  left: 50%;
  transform: translateX(-50%);
  border-left: 10px solid transparent;
  border-right: 10px solid transparent;
  border-bottom: 10px solid white;
}

/* ===== Chat Area (Right) ===== */
.chat-area { flex: 1; display: flex; flex-direction: column; min-width: 0; }

#chat-messages {
  flex: 1;
  overflow-y: auto;
  padding: 16px;
  display: flex;
  flex-direction: column;
  gap: 10px;
  -webkit-overflow-scrolling: touch;
}

.message { display: flex; flex-direction: column; max-width: 85%; animation: msgIn 0.3s ease; }
@keyframes msgIn { from { opacity: 0; transform: translateY(8px); } to { opacity: 1; transform: translateY(0); } }
.message.assistant { align-self: flex-start; }
.message.user { align-self: flex-end; }

.msg-name { font-size: 11px; color: var(--pink); font-weight: 600; margin-bottom: 3px; padding-left: 4px; }
.msg-text { padding: 10px 14px; border-radius: 16px; font-size: 15px; line-height: 1.5; word-break: break-word; }
.message.assistant .msg-text { background: var(--bg-card); border: 1px solid rgba(255,105,180,0.15); border-top-left-radius: 4px; }
.message.user .msg-text { background: linear-gradient(135deg, var(--pink-dark), var(--purple)); color: white; border-bottom-right-radius: 4px; }
.message.error .msg-text { background: rgba(255,50,50,0.15); border: 1px solid rgba(255,50,50,0.3); color: #ff9999; font-size: 13px; }

.typing-indicator span { display: inline-block; animation: typingDot 1.2s infinite; font-size: 24px; line-height: 0.5; }
.typing-indicator span:nth-child(2) { animation-delay: 0.2s; }
.typing-indicator span:nth-child(3) { animation-delay: 0.4s; }
@keyframes typingDot { 0%,60%,100% { opacity:0.3; transform:translateY(0); } 30% { opacity:1; transform:translateY(-4px); } }

.quick-section { padding: 8px 16px; border-top: 1px solid rgba(255,105,180,0.1); flex-shrink: 0; }
#quick-buttons { display:flex; gap:8px; overflow-x:auto; padding-bottom:4px; -webkit-overflow-scrolling:touch; scrollbar-width:none; }
#quick-buttons::-webkit-scrollbar { display: none; }
.quick-btn { flex-shrink:0; padding:10px 16px; border-radius:20px; border:1px solid rgba(255,105,180,0.3); background:rgba(255,105,180,0.08); color:var(--text); font-size:14px; cursor:pointer; white-space:nowrap; }
.quick-btn:active { background: var(--pink); color: white; }

.chat-input-area { padding: 12px 16px; border-top: 1px solid rgba(255,105,180,0.1); flex-shrink: 0; }
#chat-form { display: flex; gap: 8px; align-items: center; }
#chat-input { flex:1; padding:12px 18px; border-radius:24px; border:1px solid rgba(255,105,180,0.3); background:var(--bg-card); color:var(--text); font-size:16px; outline:none; }
#chat-input:focus { border-color: var(--pink); }
#chat-input::placeholder { color: var(--text-muted); }

.send-btn { width:48px; height:48px; border-radius:50%; border:none; background:linear-gradient(135deg,var(--pink),var(--purple)); color:white; font-size:20px; cursor:pointer; display:flex; align-items:center; justify-content:center; flex-shrink:0; }
.send-btn:active { transform: scale(0.92); }

.mic-btn { width:48px; height:48px; font-size:20px; flex-shrink:0; }
.mic-btn.listening { background:rgba(255,50,50,0.3); border-color:#FF3232; animation:micPulse 1s infinite; }
@keyframes micPulse { 0%,100%{box-shadow:0 0 0 0 rgba(255,50,50,0.4);} 50%{box-shadow:0 0 0 8px rgba(255,50,50,0);} }

/* ===== Responsive ===== */
@media (max-width: 820px) {
  .main-content { flex-direction: column; }
  .character-area {
    width: 100%;
    flex-direction: row;
    padding: 8px 16px;
    gap: 12px;
    justify-content: center;
    align-items: flex-end;
  }
  #character-container { max-width: 160px; min-height: 200px; }
  #character-canvas { min-height: 180px; }
  .avatar-img { max-height: 200px; }
  .speech-bubble { max-height: 80px; margin-top: 0; font-size: 13px; padding: 10px 14px; }
  .speech-bubble::before {
    top: 50%; left: -8px; transform: translateY(-50%);
    border-top:10px solid transparent; border-bottom:10px solid transparent;
    border-right:10px solid white; border-left:none;
  }
}
@media (max-width: 480px) {
  #character-container { max-width: 120px; }
  .lang-btn { padding:14px 16px; font-size:16px; }
  .lang-btn .flag { font-size:28px; }
}
