/* ============================================
   Buddy English - Kids Interaction App
   Mobile-first responsive design
   ============================================ */

/* --- Reset & Base --- */
*, *::before, *::after {
  margin: 0;
  padding: 0;
  box-sizing: border-box;
}

:root {
  --primary: #FF6B35;
  --primary-light: #FF8C5A;
  --secondary: #4ECDC4;
  --secondary-dark: #3DB8B0;
  --accent-yellow: #FFE66D;
  --accent-purple: #A06CD5;
  --bg-gradient-start: #FFF8E7;
  --bg-gradient-end: #E8F8F5;
  --text-dark: #2C3E50;
  --text-light: #7F8C8D;
  --white: #FFFFFF;
  --shadow: 0 4px 15px rgba(0, 0, 0, 0.1);
  --shadow-lg: 0 8px 30px rgba(0, 0, 0, 0.15);
  --radius: 20px;
  --radius-sm: 12px;
}

html, body {
  height: 100%;
  overflow: hidden;
  font-family: 'Segoe UI', 'PingFang SC', 'Microsoft YaHei', sans-serif;
  background: linear-gradient(135deg, var(--bg-gradient-start) 0%, var(--bg-gradient-end) 100%);
  color: var(--text-dark);
  -webkit-tap-highlight-color: transparent;
  user-select: none;
  -webkit-user-select: none;
}

/* --- App Container --- */
.app-container {
  display: flex;
  flex-direction: column;
  height: 100vh;
  height: 100dvh; /* mobile viewport fix */
  max-width: 480px;
  margin: 0 auto;
  position: relative;
  overflow: hidden;
}

/* --- Header --- */
.header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 12px 16px;
  background: var(--white);
  box-shadow: var(--shadow);
  z-index: 100;
  flex-shrink: 0;
}

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

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

.header-title h1 {
  font-size: 20px;
  font-weight: 700;
  color: var(--primary);
  letter-spacing: -0.5px;
}

.connection-status {
  display: flex;
  align-items: center;
  gap: 6px;
  font-size: 12px;
  color: var(--text-light);
}

.status-dot {
  width: 8px;
  height: 8px;
  border-radius: 50%;
  background: #2ECC71;
  animation: pulse 2s infinite;
}

.status-dot.disconnected {
  background: #E74C3C;
  animation: none;
}

@keyframes pulse {
  0%, 100% { opacity: 1; transform: scale(1); }
  50% { opacity: 0.5; transform: scale(0.8); }
}

/* --- Character Area --- */
.character-area {
  flex: 1;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  position: relative;
  padding: 20px 16px;
  min-height: 0; /* allow flex shrinking */
  overflow-y: auto;
}

.character-container {
  position: relative;
  width: 180px;
  height: 180px;
  border-radius: 50%;
  overflow: hidden;
  box-shadow: var(--shadow-lg);
  border: 5px solid var(--accent-yellow);
  transition: transform 0.3s ease, box-shadow 0.3s ease;
  flex-shrink: 0;
  display: flex;
  align-items: center;
  justify-content: center;
  background: #FFF8E7;
}

.character-container.speaking {
  animation: bounce 0.5s ease infinite alternate;
  box-shadow: 0 8px 40px rgba(255, 107, 53, 0.4);
}

@keyframes bounce {
  from { transform: scale(1); }
  to { transform: scale(1.05); }
}

.character-image {
  width: 100%;
  height: 100%;
  object-fit: cover;
  display: block;
  border-radius: 50%;
}

/* Fallback SVG avatar */
.fallback-avatar {
  width: 100%;
  height: 100%;
  display: flex;
  align-items: center;
  justify-content: center;
}

.fallback-avatar svg {
  width: 100%;
  height: 100%;
}

/* Thinking Indicator */
.thinking-indicator {
  position: absolute;
  top: -10px;
  right: -10px;
  z-index: 10;
}

.thinking-indicator .bubble {
  display: flex;
  gap: 4px;
  background: var(--white);
  padding: 8px 12px;
  border-radius: 20px;
  box-shadow: var(--shadow);
}

.thinking-indicator .dot {
  width: 8px;
  height: 8px;
  border-radius: 50%;
  background: var(--primary);
  animation: thinkDot 1.4s ease-in-out infinite;
}

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

@keyframes thinkDot {
  0%, 80%, 100% { transform: scale(0.6); opacity: 0.4; }
  40% { transform: scale(1); opacity: 1; }
}

/* Speech Bubble */
.speech-bubble {
  position: absolute;
  top: 10px;
  left: 50%;
  transform: translateX(-50%);
  background: var(--white);
  padding: 10px 18px;
  border-radius: 18px;
  box-shadow: var(--shadow);
  max-width: 280px;
  text-align: center;
  z-index: 10;
  animation: popIn 0.3s ease;
}

.speech-bubble::after {
  content: '';
  position: absolute;
  bottom: -8px;
  left: 50%;
  transform: translateX(-50%);
  width: 0;
  height: 0;
  border-left: 8px solid transparent;
  border-right: 8px solid transparent;
  border-top: 10px solid var(--white);
}

.speech-bubble p {
  font-size: 14px;
  color: var(--text-dark);
  line-height: 1.4;
}

@keyframes popIn {
  from { opacity: 0; transform: translateX(-50%) scale(0.8); }
  to { opacity: 1; transform: translateX(-50%) scale(1); }
}

/* --- Chat History --- */
.chat-history {
  flex: 0 0 auto;
  max-height: 140px;
  overflow-y: auto;
  padding: 0 16px 8px;
  scrollbar-width: thin;
  scrollbar-color: var(--primary-light) transparent;
}

.chat-history::-webkit-scrollbar {
  width: 4px;
}

.chat-history::-webkit-scrollbar-thumb {
  background: var(--primary-light);
  border-radius: 2px;
}

.welcome-message {
  text-align: center;
  padding: 12px;
  background: rgba(255, 255, 255, 0.7);
  border-radius: var(--radius-sm);
  margin-bottom: 8px;
}

.welcome-message p {
  font-size: 13px;
  color: var(--text-light);
  margin: 3px 0;
}

.chat-msg {
  display: flex;
  margin: 4px 0;
  animation: slideUp 0.3s ease;
}

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

.chat-msg.user {
  justify-content: flex-end;
}

.chat-msg.buddy {
  justify-content: flex-start;
}

.msg-bubble {
  max-width: 80%;
  padding: 8px 14px;
  border-radius: 16px;
  font-size: 13px;
  line-height: 1.4;
}

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

.chat-msg.buddy .msg-bubble {
  background: var(--white);
  color: var(--text-dark);
  border-bottom-left-radius: 4px;
  box-shadow: 0 2px 8px rgba(0,0,0,0.08);
}

.msg-label {
  font-size: 10px;
  opacity: 0.7;
  margin-bottom: 2px;
  font-weight: 600;
}

/* --- Controls Area --- */
.controls-area {
  background: var(--white);
  border-top: 1px solid rgba(0,0,0,0.05);
  padding: 10px 16px 16px;
  flex-shrink: 0;
}

/* Quick Actions */
.quick-actions {
  display: flex;
  gap: 8px;
  margin-bottom: 10px;
}

.action-btn {
  flex: 1;
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 4px;
  padding: 10px 8px;
  border: 2px solid transparent;
  border-radius: var(--radius-sm);
  background: #F8F9FA;
  color: var(--text-dark);
  font-size: 12px;
  font-weight: 600;
  cursor: pointer;
  transition: all 0.2s ease;
}

.action-btn:hover, .action-btn.active {
  border-color: var(--primary);
  background: rgba(255, 107, 53, 0.08);
  transform: translateY(-1px);
}

.action-btn.active {
  box-shadow: 0 2px 8px rgba(255, 107, 53, 0.2);
}

.action-icon {
  font-size: 22px;
}

/* Mode Indicator */
.mode-indicator {
  text-align: center;
  font-size: 12px;
  color: var(--text-light);
  margin-bottom: 10px;
}

.mode-indicator strong {
  color: var(--primary);
}

/* Input Area */
.input-area {
  display: flex;
  gap: 8px;
  margin-bottom: 10px;
}

.text-input {
  flex: 1;
  padding: 10px 16px;
  border: 2px solid #E8E8E8;
  border-radius: 25px;
  font-size: 14px;
  outline: none;
  transition: border-color 0.2s;
  color: var(--text-dark);
}

.text-input:focus {
  border-color: var(--primary);
}

.text-input::placeholder {
  color: #CCC;
}

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

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

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

/* Microphone Button */
.mic-button {
  width: 100%;
  padding: 14px;
  border: none;
  border-radius: 16px;
  background: linear-gradient(135deg, var(--secondary) 0%, var(--secondary-dark) 100%);
  color: var(--white);
  font-size: 16px;
  font-weight: 700;
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 10px;
  transition: all 0.2s ease;
  box-shadow: 0 4px 15px rgba(78, 205, 196, 0.3);
}

.mic-button:hover {
  transform: translateY(-2px);
  box-shadow: 0 6px 20px rgba(78, 205, 196, 0.4);
}

.mic-button:active {
  transform: scale(0.98);
}

.mic-button.recording {
  background: linear-gradient(135deg, #E74C3C 0%, #C0392B 100%);
  box-shadow: 0 4px 20px rgba(231, 76, 60, 0.5);
  animation: recordPulse 1s ease infinite;
}

@keyframes recordPulse {
  0%, 100% { box-shadow: 0 4px 20px rgba(231, 76, 60, 0.5); }
  50% { box-shadow: 0 4px 30px rgba(231, 76, 60, 0.8); }
}

.mic-button svg {
  transition: transform 0.2s;
}

.mic-button.recording svg {
  transform: scale(1.2);
}

.mic-label {
  letter-spacing: 0.5px;
}

/* --- Recording Waveform --- */
.waveform {
  display: none;
  justify-content: center;
  align-items: center;
  gap: 3px;
  height: 30px;
  margin-top: 8px;
}

.waveform.active {
  display: flex;
}

.wave-bar {
  width: 4px;
  height: 8px;
  background: var(--white);
  border-radius: 2px;
  animation: wave 0.5s ease-in-out infinite alternate;
}

.wave-bar:nth-child(1) { animation-delay: 0s; }
.wave-bar:nth-child(2) { animation-delay: 0.1s; }
.wave-bar:nth-child(3) { animation-delay: 0.2s; }
.wave-bar:nth-child(4) { animation-delay: 0.3s; }
.wave-bar:nth-child(5) { animation-delay: 0.15s; }
.wave-bar:nth-child(6) { animation-delay: 0.25s; }
.wave-bar:nth-child(7) { animation-delay: 0.05s; }

@keyframes wave {
  from { height: 6px; }
  to { height: 24px; }
}

/* --- Loading Spinner --- */
.loading-spinner {
  display: none;
  width: 24px;
  height: 24px;
  border: 3px solid rgba(255, 107, 53, 0.2);
  border-top-color: var(--primary);
  border-radius: 50%;
  animation: spin 0.8s linear infinite;
  margin: 8px auto;
}

.loading-spinner.active {
  display: block;
}

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

/* --- Responsive adjustments --- */
@media (max-height: 600px) {
  .character-container {
    width: 140px;
    height: 140px;
  }
  
  .chat-history {
    max-height: 100px;
  }
  
  .speech-bubble {
    max-width: 220px;
    padding: 6px 12px;
  }
  
  .speech-bubble p {
    font-size: 12px;
  }
}

@media (min-width: 481px) {
  .app-container {
    border-left: 1px solid rgba(0,0,0,0.05);
    border-right: 1px solid rgba(0,0,0,0.05);
  }
}

/* --- Accessibility --- */
@media (prefers-reduced-motion: reduce) {
  *, *::before, *::after {
    animation-duration: 0.01ms !important;
    transition-duration: 0.01ms !important;
  }
}
