/* ===== Base & Variables ===== */
:root {
  --primary: #6366f1;
  --primary-light: #818cf8;
  --primary-bg: #eef2ff;
  --danger: #ef4444;
  --success: #10b981;
  --surface: #ffffff;
  --bg: #f8fafc;
  --text: #1e293b;
  --text-secondary: #64748b;
  --text-muted: #94a3b8;
  --border: #e2e8f0;
  --radius: 12px;
  --radius-lg: 20px;
  --shadow: 0 1px 3px rgba(0,0,0,0.08), 0 1px 2px rgba(0,0,0,0.06);
  --shadow-lg: 0 10px 40px rgba(0,0,0,0.08);

  /* Phone-call dark theme */
  --call-bg: #1a1a2e;
  --call-bg2: #16213e;
  --call-bubble: rgba(42,42,74,0.92);
  --call-user-bubble: rgba(99,102,241,0.85);
  --call-text: #f0f0ff;
  --call-text-muted: rgba(240,240,255,0.55);
}

* { margin: 0; padding: 0; box-sizing: border-box; }

body {
  font-family: 'Inter', -apple-system, BlinkMacSystemFont, 'Segoe UI', system-ui, sans-serif;
  background: var(--bg);
  color: var(--text);
  line-height: 1.5;
  height: 100dvh;
  overflow: hidden;
}

.hidden { display: none !important; }

/* ===== Screen Layout ===== */
.screen {
  height: 100dvh;
  display: flex;
  align-items: center;
  justify-content: center;
}

/* ===== 1. Welcome Screen ===== */
.welcome-container {
  max-width: 440px;
  width: 100%;
  padding: 40px 24px;
  text-align: center;
}

.tutor-avatar { margin-bottom: 32px; }

.avatar-img {
  width: 140px;
  height: 140px;
  border-radius: 50%;
  object-fit: cover;
  margin: 0 auto 12px;
  display: block;
  border: 3px solid var(--primary-bg);
  box-shadow: 0 4px 12px rgba(99,102,241,0.2);
}

.tutor-name { font-size: 1.25rem; font-weight: 700; color: var(--text); }
.tutor-role { font-size: 0.875rem; color: var(--text-secondary); margin-top: 2px; }

.welcome-message h1 { font-size: 1.5rem; font-weight: 700; margin-bottom: 8px; line-height: 1.3; }
.welcome-message p { font-size: 0.95rem; color: var(--text-secondary); margin-bottom: 32px; }

.session-options { display: flex; flex-direction: column; gap: 12px; margin-bottom: 32px; }

.session-btn {
  display: flex; align-items: center; gap: 14px; padding: 16px 20px;
  border: none; border-radius: var(--radius); cursor: pointer;
  font-family: inherit; transition: all 0.2s; text-align: left; width: 100%;
}

.session-btn.primary {
  background: var(--primary); color: white;
  box-shadow: 0 4px 14px rgba(99,102,241,0.35); justify-content: center;
}
.session-btn.primary:hover { background: #4f46e5; transform: translateY(-1px); box-shadow: 0 6px 20px rgba(99,102,241,0.4); }
.session-btn.primary:active { transform: translateY(0); }

.btn-icon { display: flex; align-items: center; justify-content: center; flex-shrink: 0; }
.btn-label { display: flex; flex-direction: column; gap: 2px; }
.btn-label strong { font-size: 1rem; }
.btn-label small { font-size: 0.8rem; opacity: 0.85; }

.welcome-tips { display: flex; flex-direction: column; gap: 8px; }
.tip { display: flex; align-items: center; gap: 8px; font-size: 0.8rem; color: var(--text-muted); }
.tip-icon { display: flex; align-items: center; flex-shrink: 0; opacity: 0.6; }

/* ===== 2. Connecting ===== */
.connecting-container { text-align: center; color: var(--text-secondary); }
.spinner { width: 48px; height: 48px; border: 3px solid var(--border); border-top-color: var(--primary); border-radius: 50%; margin: 0 auto 16px; animation: spin 0.8s linear infinite; }
@keyframes spin { to { transform: rotate(360deg); } }

/* ===== 3. Chat Screen — Phone-call style ===== */
#chat-screen {
  flex-direction: column;
  align-items: stretch;
  justify-content: flex-start;
  background: var(--call-bg);
  background-image: radial-gradient(ellipse at 50% 0%, #0f3460 0%, var(--call-bg) 70%);
}

/* Top bar */
.call-topbar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 16px 20px;
  padding-top: max(16px, env(safe-area-inset-top));
  flex-shrink: 0;
}

.call-timer {
  font-size: 1rem;
  font-weight: 600;
  font-variant-numeric: tabular-nums;
  color: var(--call-text);
  letter-spacing: 0.02em;
}
.call-timer.warning { color: var(--danger); animation: pulse-warning 1s ease-in-out infinite; }
@keyframes pulse-warning { 0%,100% { opacity: 1; } 50% { opacity: 0.6; } }

.call-close-btn {
  width: 36px;
  height: 36px;
  border-radius: 50%;
  border: none;
  background: rgba(255,255,255,0.1);
  color: var(--call-text);
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: center;
  transition: background 0.2s;
}
.call-close-btn:hover { background: rgba(255,255,255,0.2); }

/* Teacher area */
.call-teacher-area {
  display: flex;
  flex-direction: column;
  align-items: center;
  padding: 8px 20px 12px;
  flex-shrink: 0;
}

.call-avatar-wrap {
  width: 100px;
  height: 100px;
  border-radius: 50%;
  border: 3px solid rgba(255,255,255,0.7);
  box-shadow: 0 0 0 6px rgba(255,255,255,0.08), 0 8px 32px rgba(0,0,0,0.4);
  overflow: hidden;
  margin-bottom: 12px;
  flex-shrink: 0;
}

.call-avatar {
  width: 100%;
  height: 100%;
  object-fit: cover;
  display: block;
}

.call-teacher-name {
  font-size: 1.15rem;
  font-weight: 700;
  color: var(--call-text);
  margin-bottom: 4px;
}

.call-status {
  font-size: 0.85rem;
  font-weight: 500;
  color: var(--call-text-muted);
  transition: color 0.3s;
}
.call-status.listening  { color: #34d399; }
.call-status.recording  { color: var(--danger); }
.call-status.recognizing { color: #a78bfa; }
.call-status.thinking   { color: #60a5fa; }
.call-status.speaking   { color: #38bdf8; }

/* Chat messages area (scrollable) */
.call-chat-messages {
  flex: 1;
  overflow-y: auto;
  padding: 12px 16px;
  display: flex;
  flex-direction: column;
  gap: 8px;
  scroll-behavior: smooth;
}

.call-msg {
  max-width: 75%;
  padding: 10px 14px;
  border-radius: 18px;
  font-size: 0.93rem;
  line-height: 1.45;
  word-wrap: break-word;
  animation: bubble-in 0.2s ease-out;
  position: relative;
}

.call-msg.ai {
  align-self: flex-start;
  background: rgba(255, 255, 255, 0.12);
  color: var(--call-text);
  border-radius: 18px 18px 18px 4px;
  margin-left: 8px;
}

.call-msg.ai::before {
  content: '';
  position: absolute;
  left: -6px;
  bottom: 0;
  width: 12px;
  height: 12px;
  background: rgba(255, 255, 255, 0.12);
  border-bottom-right-radius: 16px;
  clip-path: polygon(100% 0, 100% 100%, 0 100%);
}

/* Korean subtitle under AI message */
.kr-subtitle {
  margin-top: 6px;
  padding-top: 6px;
  border-top: 1px solid rgba(255, 255, 255, 0.08);
  font-size: 0.82rem;
  color: rgba(255, 255, 255, 0.5);
  line-height: 1.4;
}

.call-msg.user {
  align-self: flex-end;
  background: var(--call-user-bubble);
  color: white;
  border-radius: 18px 18px 4px 18px;
  margin-right: 8px;
}

.call-msg.user::before {
  content: '';
  position: absolute;
  right: -6px;
  bottom: 0;
  width: 12px;
  height: 12px;
  background: var(--call-user-bubble);
  border-bottom-left-radius: 16px;
  clip-path: polygon(0 0, 100% 100%, 0 100%);
}

.call-msg.typing {
  padding: 14px 20px;
}

@keyframes bubble-in {
  from { opacity: 0; transform: translateY(6px); }
  to   { opacity: 1; transform: translateY(0); }
}

/* Typing dots inside bubble */
.typing-dots {
  display: flex;
  gap: 5px;
  align-items: center;
}
.typing-dots span {
  width: 7px; height: 7px;
  background: var(--call-text-muted);
  border-radius: 50%;
  animation: typing-bounce 1.2s ease-in-out infinite;
}
.typing-dots span:nth-child(2) { animation-delay: 0.15s; }
.typing-dots span:nth-child(3) { animation-delay: 0.3s; }
@keyframes typing-bounce { 0%,60%,100% { transform: translateY(0); } 30% { transform: translateY(-5px); } }

.call-msg.user.typing .typing-dots span {
  background: rgba(255,255,255,0.6);
}

/* VU meter */
.vu-meter {
  width: calc(100% - 48px);
  height: 4px;
  background: rgba(255,255,255,0.08);
  border-radius: 2px;
  margin: 0 24px 8px;
  overflow: hidden;
  flex-shrink: 0;
}
.vu-bar {
  height: 100%; width: 0%;
  border-radius: 2px;
  background: #34d399;
  transition: width 0.08s, background 0.2s;
}
.vu-bar.recording { background: var(--danger); }
.vu-bar.speaking  { background: #38bdf8; }

/* Mic button */
.call-mic-area {
  display: flex;
  justify-content: center;
  align-items: center;
  padding: 12px 0 16px;
  flex-shrink: 0;
  position: relative;
}

/* 파형 링 (녹음 중 표시) */
.mic-wave-ring {
  position: absolute;
  width: 64px; height: 64px;
  border-radius: 50%;
  border: 3px solid transparent;
  transition: all 0.2s;
  pointer-events: none;
}
.mic-wave-ring.active {
  animation: wave-ring 1.2s ease-out infinite;
  border-color: rgba(16,185,129,0.5);
}

@keyframes wave-ring {
  0%   { transform: scale(1);   opacity: 0.7; border-color: rgba(16,185,129,0.6); }
  100% { transform: scale(1.8); opacity: 0;   border-color: rgba(16,185,129,0); }
}

.call-mic-btn {
  width: 64px;
  height: 64px;
  border-radius: 50%;
  border: none;
  color: white;
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: center;
  transition: background 0.3s, box-shadow 0.3s;
  position: relative;
  z-index: 1;
}

/* 미연결 = 빨간색 */
.call-mic-btn.disconnected {
  background: var(--danger);
  box-shadow: 0 4px 16px rgba(239,68,68,0.4);
}

/* 연결됨 (listening) = 녹색 */
.call-mic-btn.connected {
  background: var(--success);
  box-shadow: 0 4px 16px rgba(16,185,129,0.4);
}

/* 녹음 중 = 녹색 + 파형 펄스 */
.call-mic-btn.recording {
  background: var(--success);
  box-shadow: 0 4px 16px rgba(16,185,129,0.5);
}

/* 처리 중 */
.call-mic-btn.processing {
  background: rgba(255,255,255,0.15);
  box-shadow: none;
  pointer-events: none;
}

/* 비활성 (TTS 재생 중) */
.call-mic-btn.speaking {
  background: rgba(255,255,255,0.08);
  color: rgba(255,255,255,0.3);
  box-shadow: none;
  pointer-events: none;
}

/* Text input */
.call-text-area {
  padding: 0 16px;
  padding-bottom: max(16px, env(safe-area-inset-bottom));
  flex-shrink: 0;
}

.text-input-wrap {
  display: flex;
  align-items: center;
  gap: 8px;
  background: rgba(255,255,255,0.07);
  border-radius: 28px;
  padding: 6px 6px 6px 16px;
  border: 1px solid rgba(255,255,255,0.1);
}

#text-input {
  flex: 1;
  background: transparent;
  border: none;
  outline: none;
  font-family: inherit;
  font-size: 0.9rem;
  color: var(--call-text);
  padding: 4px 0;
}
#text-input::placeholder { color: var(--call-text-muted); }

.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;
  transition: background 0.2s; flex-shrink: 0;
}
.send-btn:hover { background: #4f46e5; }

/* Chat scrollbar (dark) */
.call-chat-messages::-webkit-scrollbar { width: 4px; }
.call-chat-messages::-webkit-scrollbar-track { background: transparent; }
.call-chat-messages::-webkit-scrollbar-thumb { background: rgba(255,255,255,0.15); border-radius: 2px; }

/* ===== 4. End Screen ===== */
.end-container { max-width: 400px; width: 100%; padding: 40px 24px; text-align: center; }
.end-icon { margin-bottom: 16px; }
.end-container h2 { font-size: 1.5rem; margin-bottom: 24px; }
.session-summary { background: var(--bg); border-radius: var(--radius); padding: 20px; margin-bottom: 8px; }
.summary-row { display: flex; justify-content: space-between; padding: 8px 0; }
.summary-row + .summary-row { border-top: 1px solid var(--border); }
.summary-label { color: var(--text-secondary); font-size: 0.9rem; }
.summary-value { font-weight: 600; font-size: 0.95rem; }

/* tutor bubble — single Emma style */
.call-msg.ai.tutor-emma {
  /* default ai style */
}

/* ===== Responsive ===== */
@media (max-width: 480px) {
  .welcome-message h1 { font-size: 1.25rem; }
  .call-avatar-wrap { width: 80px; height: 80px; }
  .call-mic-btn { width: 56px; height: 56px; }
  .call-bubble { font-size: 0.93rem; }
}

@media (max-height: 640px) {
  .call-teacher-area { padding: 12px 20px 8px; }
  .call-avatar-wrap { width: 72px; height: 72px; }
  .call-teacher-name { font-size: 1rem; }
}

/* ===== 마이크 권한 안내 오버레이 ===== */
.mic-guide-overlay {
  position: fixed;
  inset: 0;
  background: rgba(0,0,0,0.7);
  display: flex;
  align-items: center;
  justify-content: center;
  z-index: 9999;
  padding: 20px;
}
.mic-guide-card {
  background: var(--surface, #fff);
  border-radius: 16px;
  padding: 32px 28px;
  max-width: 380px;
  width: 100%;
  text-align: center;
  box-shadow: 0 20px 60px rgba(0,0,0,0.3);
}
.mic-guide-icon {
  font-size: 48px;
  margin-bottom: 12px;
}
.mic-guide-card h3 {
  color: var(--text, #1e293b);
  font-size: 1.15rem;
  margin-bottom: 8px;
}
.mic-guide-card p {
  color: var(--text-secondary, #64748b);
  font-size: 0.9rem;
  line-height: 1.5;
  margin-bottom: 20px;
}
.mic-guide-steps {
  text-align: left;
  margin-bottom: 24px;
}
.mic-guide-step {
  display: flex;
  align-items: flex-start;
  gap: 12px;
  padding: 8px 0;
  color: var(--text, #1e293b);
  font-size: 0.88rem;
  line-height: 1.4;
}
.step-num {
  flex-shrink: 0;
  width: 24px;
  height: 24px;
  border-radius: 50%;
  background: var(--primary, #6366f1);
  color: #fff;
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 0.75rem;
  font-weight: 600;
}
.mic-guide-buttons {
  display: flex;
  gap: 10px;
  justify-content: center;
}
.mic-guide-btn {
  padding: 10px 24px;
  border-radius: 10px;
  font-size: 0.9rem;
  font-weight: 600;
  border: none;
  cursor: pointer;
  transition: opacity 0.2s;
}
.mic-guide-btn:hover { opacity: 0.85; }
.mic-guide-btn.primary {
  background: var(--primary, #6366f1);
  color: #fff;
}
.mic-guide-btn.secondary {
  background: var(--border, #e2e8f0);
  color: var(--text-secondary, #64748b);
}
