/* ===== 기본 설정 ===== */
*, *::before, *::after {
  box-sizing: border-box;
  margin: 0;
  padding: 0;
  -webkit-tap-highlight-color: transparent;
}

:root {
  --bg-primary: #0f0f1a;
  --bg-secondary: #1a1a2e;
  --bg-card: #16213e;
  --accent: #FF6B9D;
  --accent-light: #ff85ad;
  --text-primary: #e8e8f0;
  --text-secondary: #9090b0;
  --text-muted: #5a5a7a;
  --bubble-bot: #1e1e3a;
  --bubble-user: #6c63ff;
  --border: #2a2a4a;
  --danger: #ff4d6d;
  --success: #4dff91;
  --char-color: #FF6B9D;
}

html, body {
  height: 100%;
  width: 100%;
  overflow: hidden;
  background: var(--bg-primary);
  color: var(--text-primary);
  font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', 'Noto Sans KR', sans-serif;
  font-size: 16px;
  line-height: 1.5;
}

/* ===== 화면 전환 ===== */
.screen {
  position: fixed;
  inset: 0;
  display: none;
  flex-direction: column;
  overflow: hidden;
}
.screen.active { display: flex; }

/* ===== 1. 이름 입력 화면 ===== */
#setup-screen {
  align-items: center;
  justify-content: center;
  padding: 24px;
  background: radial-gradient(ellipse at top, #1a1a3e 0%, #0f0f1a 70%);
}

.setup-container {
  width: 100%;
  max-width: 400px;
  text-align: center;
}

.setup-logo {
  font-size: 64px;
  margin-bottom: 16px;
  animation: float 3s ease-in-out infinite;
}

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

.setup-title {
  font-size: 38px;
  font-weight: 800;
  background: linear-gradient(135deg, #FF6B9D, #6c63ff);
  -webkit-background-clip: text;
  -webkit-text-fill-color: transparent;
  background-clip: text;
  margin-bottom: 8px;
}

.setup-subtitle {
  color: var(--text-secondary);
  margin-bottom: 40px;
  font-size: 15px;
}

.setup-form {
  display: flex;
  flex-direction: column;
  gap: 16px;
}

.input-group label {
  display: block;
  font-size: 13px;
  color: var(--text-secondary);
  margin-bottom: 6px;
  font-weight: 500;
  text-align: left;
}

.input-group input {
  width: 100%;
  padding: 14px 16px;
  background: var(--bg-card);
  border: 1px solid var(--border);
  border-radius: 14px;
  color: var(--text-primary);
  font-size: 16px;
  outline: none;
  transition: border-color 0.2s;
}

.input-group input:focus { border-color: var(--accent); }
.input-group input::placeholder { color: var(--text-muted); }

/* ===== 2. 캐릭터 선택 화면 ===== */
#character-screen {
  background: var(--bg-primary);
  overflow-y: auto;
}

.char-container {
  padding: 40px 20px 30px;
  max-width: 500px;
  margin: 0 auto;
  width: 100%;
}

.char-header {
  text-align: center;
  margin-bottom: 30px;
}

.char-header h2 {
  font-size: 24px;
  font-weight: 800;
  margin-bottom: 6px;
}

.char-header p {
  color: var(--text-secondary);
  font-size: 14px;
}

.char-list {
  display: flex;
  flex-direction: column;
  gap: 14px;
}

.char-card {
  background: var(--bg-card);
  border: 2px solid var(--border);
  border-radius: 20px;
  padding: 20px;
  cursor: pointer;
  transition: all 0.2s;
  display: flex;
  align-items: center;
  gap: 16px;
}

.char-card:active {
  transform: scale(0.98);
}

.char-card:hover {
  border-color: var(--char-accent, #FF6B9D);
}

.char-card.selected {
  border-color: var(--char-accent, #FF6B9D);
  background: rgba(255, 107, 157, 0.1);
}

.char-emoji {
  font-size: 48px;
  flex-shrink: 0;
  width: 64px;
  height: 64px;
  display: flex;
  align-items: center;
  justify-content: center;
  background: rgba(255,255,255,0.05);
  border-radius: 16px;
}

.char-info { flex: 1; }

.char-name {
  font-size: 20px;
  font-weight: 700;
  margin-bottom: 4px;
}

.char-type {
  font-size: 12px;
  font-weight: 600;
  padding: 3px 10px;
  border-radius: 20px;
  display: inline-block;
  margin-bottom: 6px;
  opacity: 0.9;
}

.char-desc {
  font-size: 13px;
  color: var(--text-secondary);
  line-height: 1.4;
}

.char-arrow {
  font-size: 20px;
  color: var(--text-muted);
  flex-shrink: 0;
}

.char-nick-badge {
  display: inline;
  font-size: 14px;
  font-weight: 500;
  margin-left: 4px;
}

.char-adopted-mark {
  font-size: 11px;
  font-weight: 600;
  padding: 2px 8px;
  border-radius: 10px;
  display: inline-block;
  margin-top: 4px;
}

/* ===== 2.5. 분양 화면 ===== */
#adopt-screen {
  background: radial-gradient(ellipse at top, #1a1a3e 0%, #0f0f1a 70%);
  overflow-y: auto;
  align-items: center;
}

.adopt-container {
  width: 100%;
  max-width: 420px;
  padding: 20px 24px 40px;
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 0;
  position: relative;
}

.adopt-back-btn {
  align-self: flex-start;
  margin-bottom: 8px;
}

.adopt-viewer-wrap {
  width: 220px;
  height: 260px;
  display: flex;
  align-items: center;
  justify-content: center;
  background: rgba(255,255,255,0.03);
  border-radius: 24px;
  overflow: hidden;
  margin-bottom: 20px;
  border: 1px solid var(--border);
}

.viewer-emoji {
  font-size: 90px;
  line-height: 1;
  display: flex;
  align-items: center;
  justify-content: center;
  width: 100%;
  height: 100%;
}

.adopt-char-info {
  text-align: center;
  margin-bottom: 28px;
}

.adopt-char-name {
  font-size: 28px;
  font-weight: 800;
  margin-bottom: 6px;
}

.adopt-char-type {
  font-size: 13px;
  font-weight: 600;
  padding: 4px 14px;
  border-radius: 20px;
  display: inline-block;
  opacity: 0.9;
}

.adopt-form {
  width: 100%;
  margin-bottom: 28px;
  text-align: center;
}

.adopt-question {
  font-size: 16px;
  font-weight: 700;
  color: var(--text-primary);
  margin-bottom: 4px;
}

.adopt-hint {
  font-size: 12px;
  color: var(--text-muted);
  margin-bottom: 16px;
}

.adopt-nickname-input {
  width: 100%;
  padding: 14px 16px;
  background: var(--bg-card);
  border: 2px solid var(--border);
  border-radius: 14px;
  color: var(--text-primary);
  font-size: 18px;
  font-weight: 600;
  text-align: center;
  outline: none;
  transition: border-color 0.2s;
  letter-spacing: 1px;
}

.adopt-nickname-input:focus {
  border-color: var(--accent);
}

.adopt-nickname-input::placeholder {
  color: var(--text-muted);
  font-weight: 400;
  letter-spacing: 0;
}

.adopt-confirm-btn {
  width: 100%;
  font-size: 18px;
}

/* ===== 헤더 아바타 래퍼 (3D/이모지) ===== */
/* char-model-wrap = large character container */
.char-model-wrap canvas,
.char-model-wrap > div { width: 100% !important; height: 100% !important; }

.header-avatar-wrap {
  width: 44px;
  height: 44px;
  display: flex;
  align-items: center;
  justify-content: center;
  border-radius: 50%;
  overflow: hidden;
  background: rgba(255,255,255,0.06);
  flex-shrink: 0;
}

.header-avatar-wrap .viewer-emoji,
.header-avatar-wrap #header-avatar {
  font-size: 28px;
  line-height: 1;
}

.header-text {
  display: flex;
  flex-direction: column;
  justify-content: center;
}

.header-nickname {
  font-size: 11px;
  color: var(--text-muted);
  line-height: 1;
  margin-bottom: 1px;
}

/* ===== 버튼 ===== */
.btn-primary {
  width: 100%;
  padding: 16px;
  background: linear-gradient(135deg, #FF6B9D, #6c63ff);
  color: white;
  border: none;
  border-radius: 14px;
  font-size: 17px;
  font-weight: 700;
  cursor: pointer;
  transition: transform 0.1s, opacity 0.2s;
}
.btn-primary:active { transform: scale(0.97); }

.btn-secondary {
  width: 100%;
  padding: 14px;
  background: rgba(108, 99, 255, 0.15);
  color: #8b83ff;
  border: 1px solid rgba(108, 99, 255, 0.3);
  border-radius: 12px;
  font-size: 15px;
  font-weight: 600;
  cursor: pointer;
  transition: background 0.2s;
  margin-bottom: 10px;
}

.btn-icon {
  background: none;
  border: none;
  color: var(--text-primary);
  font-size: 20px;
  cursor: pointer;
  padding: 8px;
  border-radius: 50%;
  min-width: 40px;
  min-height: 40px;
  display: flex;
  align-items: center;
  justify-content: center;
  transition: background 0.15s;
}
.btn-icon:active { background: rgba(255,255,255,0.1); }

.btn-danger {
  width: 100%;
  padding: 14px;
  background: rgba(255, 77, 109, 0.1);
  color: var(--danger);
  border: 1px solid rgba(255, 77, 109, 0.3);
  border-radius: 12px;
  font-size: 15px;
  font-weight: 600;
  cursor: pointer;
}

/* ===== 채팅 상단 미니바 ===== */
.chat-topbar {
  height: 50px;
  background: rgba(15,15,26,0.85);
  backdrop-filter: blur(12px);
  border-bottom: 1px solid var(--border);
  display: flex;
  align-items: center;
  padding: 0 8px 0 4px;
  gap: 4px;
  flex-shrink: 0;
  padding-top: env(safe-area-inset-top, 0px);
  z-index: 10;
  position: relative;
}

.topbar-info {
  flex: 1;
  display: flex;
  align-items: baseline;
  gap: 6px;
  overflow: hidden;
}

.topbar-name {
  font-size: 15px;
  font-weight: 700;
  white-space: nowrap;
}

.topbar-nickname {
  font-size: 12px;
  color: var(--accent);
  white-space: nowrap;
}

.topbar-status {
  font-size: 11px;
  color: var(--success);
  white-space: nowrap;
}

.topbar-status::before {
  content: '';
  display: inline-block;
  width: 6px; height: 6px;
  background: var(--success);
  border-radius: 50%;
  margin-right: 3px;
  animation: pulse 2s infinite;
}

/* 구버전 호환 (JS에서 참조) */
.chat-header { display: none; }
.header-name, .header-nickname, .header-status { display: none; }

@keyframes pulse {
  0%, 100% { opacity: 1; }
  50% { opacity: 0.4; }
}

/* ===== 캐릭터 스테이지 ===== */
.char-stage {
  position: relative;
  flex-shrink: 0;
  height: 45vh;
  min-height: 220px;
  max-height: 380px;
  display: flex;
  align-items: center;
  justify-content: center;
  overflow: hidden;
  background: radial-gradient(ellipse at center, rgba(255,107,157,0.08) 0%, transparent 70%);
  transition: background 0.5s;
}

.char-stage.speaking {
  background: radial-gradient(ellipse at center, rgba(255,107,157,0.18) 0%, transparent 70%);
}

.char-stage.sleeping {
  filter: brightness(0.45) grayscale(0.3);
  transition: filter 1.2s ease;
}

.char-stage.sleeping #voice-wake-hint {
  opacity: 0.6;
}

.char-stage:not(.sleeping) {
  filter: brightness(1) grayscale(0);
  transition: filter 0.6s ease;
}

.char-stage.emotion-happy {
  background: radial-gradient(ellipse at center, rgba(255,200,50,0.12) 0%, transparent 70%);
}
.char-stage.emotion-sad {
  background: radial-gradient(ellipse at center, rgba(80,120,255,0.12) 0%, transparent 70%);
}
.char-stage.emotion-excited {
  background: radial-gradient(ellipse at center, rgba(255,80,160,0.18) 0%, transparent 70%);
}
.char-stage.emotion-concerned, .char-stage.emotion-empathetic {
  background: radial-gradient(ellipse at center, rgba(100,200,150,0.12) 0%, transparent 70%);
}

.char-model-wrap {
  width: 100%;
  height: 100%;
  display: flex;
  align-items: center;
  justify-content: center;
}

/* 감정 링 */
.emotion-ring {
  position: absolute;
  bottom: 0; left: 50%;
  transform: translateX(-50%);
  width: 120px; height: 30px;
  border-radius: 50%;
  background: radial-gradient(ellipse, var(--accent) 0%, transparent 70%);
  opacity: 0;
  transition: opacity 0.4s;
  pointer-events: none;
}

.char-stage.speaking .emotion-ring {
  opacity: 0.5;
  animation: ring-pulse 0.8s ease-in-out infinite;
}

@keyframes ring-pulse {
  0%, 100% { transform: translateX(-50%) scaleX(1); opacity: 0.4; }
  50% { transform: translateX(-50%) scaleX(1.3); opacity: 0.7; }
}

/* AI 말풍선 */
.char-speech-bubble {
  position: absolute;
  top: 12px;
  left: 50%;
  transform: translateX(-50%);
  max-width: 85%;
  background: rgba(30, 30, 50, 0.92);
  border: 1px solid rgba(255,107,157,0.3);
  border-radius: 18px 18px 18px 4px;
  padding: 10px 14px;
  font-size: 14px;
  line-height: 1.5;
  color: var(--text-primary);
  backdrop-filter: blur(8px);
  z-index: 5;
  animation: bubble-in 0.3s ease;
  max-height: 100px;
  overflow: hidden;
  display: -webkit-box;
  -webkit-line-clamp: 4;
  -webkit-box-orient: vertical;
}

@keyframes bubble-in {
  from { opacity: 0; transform: translateX(-50%) translateY(-8px); }
  to { opacity: 1; transform: translateX(-50%) translateY(0); }
}

/* ===== 음성 상태 바 ===== */
.voice-status-bar {
  flex-shrink: 0;
  height: 36px;
  display: flex;
  align-items: center;
  justify-content: center;
  background: var(--bg-secondary);
  border-bottom: 1px solid var(--border);
  font-size: 12px;
  color: var(--text-muted);
  gap: 8px;
  position: relative;
}

.voice-status-bar .typing-indicator {
  position: static;
  background: transparent;
  padding: 0;
  box-shadow: none;
}

.voice-wake-hint { transition: opacity 0.3s; }
.voice-wake-hint.active {
  color: var(--accent);
  font-weight: 600;
}

/* ===== 메시지 영역 ===== */
.chat-messages {
  flex: 1;
  overflow-y: auto;
  padding: 8px 12px;
  display: flex;
  flex-direction: column;
  gap: 4px;
  -webkit-overflow-scrolling: touch;
  min-height: 0;
  max-height: 35vh;
}

.chat-messages::-webkit-scrollbar { width: 3px; }
.chat-messages::-webkit-scrollbar-thumb { background: var(--border); border-radius: 3px; }

.date-divider {
  text-align: center;
  margin: 8px 0 16px;
}

.date-divider span {
  font-size: 12px;
  color: var(--text-muted);
  background: var(--bg-secondary);
  padding: 4px 12px;
  border-radius: 20px;
}

/* ===== 메시지 버블 ===== */
.message {
  display: flex;
  align-items: flex-end;
  gap: 8px;
  max-width: 100%;
  animation: msg-in 0.2s ease-out;
}

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

.bot-message { align-self: flex-start; }
.user-message { align-self: flex-end; flex-direction: row-reverse; }

.message-avatar { font-size: 28px; flex-shrink: 0; margin-bottom: 2px; }
.user-message .message-avatar { display: none; }

.message-content {
  display: flex;
  flex-direction: column;
  max-width: calc(100% - 50px);
}

.user-message .message-content {
  max-width: 80%;
  align-items: flex-end;
}

.message-bubble {
  padding: 10px 14px;
  border-radius: 18px;
  font-size: 15px;
  line-height: 1.55;
  word-break: break-word;
  white-space: pre-wrap;
}

.bot-message .message-bubble {
  background: var(--bubble-bot);
  border: 1px solid var(--border);
  border-bottom-left-radius: 6px;
}

.user-message .message-bubble {
  background: var(--bubble-user);
  color: white;
  border-bottom-right-radius: 6px;
}

.message-time {
  font-size: 10px;
  color: var(--text-muted);
  padding: 2px 4px;
  margin-top: 2px;
}

/* ===== 타이핑 인디케이터 ===== */
.typing-indicator {
  display: flex;
  align-items: flex-end;
  gap: 8px;
  padding: 4px 12px 8px;
  animation: msg-in 0.2s ease-out;
}

.typing-dots {
  background: var(--bubble-bot);
  border: 1px solid var(--border);
  padding: 12px 16px;
  border-radius: 18px;
  border-bottom-left-radius: 6px;
  display: flex;
  gap: 5px;
  align-items: center;
}

.typing-dots span {
  width: 7px;
  height: 7px;
  background: var(--text-secondary);
  border-radius: 50%;
  animation: typing-bounce 1.2s infinite;
}

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

@keyframes typing-bounce {
  0%, 80%, 100% { transform: translateY(0); opacity: 0.4; }
  40% { transform: translateY(-6px); opacity: 1; }
}

/* ===== 입력 영역 ===== */
.chat-input-area {
  background: var(--bg-secondary);
  border-top: 1px solid var(--border);
  padding: 10px 12px calc(10px + env(safe-area-inset-bottom, 0px));
  flex-shrink: 0;
}

.input-row {
  display: flex;
  align-items: flex-end;
  gap: 8px;
  background: var(--bg-card);
  border: 1px solid var(--border);
  border-radius: 24px;
  padding: 8px 8px 8px 16px;
  transition: border-color 0.2s;
}

.input-row:focus-within { border-color: var(--accent); }

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

textarea#message-input::placeholder { color: var(--text-muted); }

.btn-send {
  width: 40px;
  height: 40px;
  background: var(--accent);
  border: none;
  border-radius: 50%;
  display: flex;
  align-items: center;
  justify-content: center;
  cursor: pointer;
  flex-shrink: 0;
  transition: transform 0.1s, background 0.3s;
}

.btn-send:disabled { background: var(--border); cursor: not-allowed; }
.btn-send:not(:disabled):active { transform: scale(0.9); }
.btn-send svg { width: 18px; height: 18px; color: white; }

.btn-mic {
  width: 40px;
  height: 40px;
  border: none;
  border-radius: 50%;
  background: var(--surface2);
  font-size: 18px;
  cursor: pointer;
  flex-shrink: 0;
  transition: background 0.2s, transform 0.1s;
  display: flex;
  align-items: center;
  justify-content: center;
}
.btn-mic:active { transform: scale(0.9); }
.btn-mic.listening {
  background: #ff3b3b33;
  animation: mic-pulse 1s infinite;
}
@keyframes mic-pulse {
  0%, 100% { box-shadow: 0 0 0 0 #ff3b3b55; }
  50% { box-shadow: 0 0 0 8px #ff3b3b00; }
}

/* ===== 사이드 메뉴 ===== */
.side-menu {
  position: fixed;
  top: 0;
  right: -300px;
  width: 280px;
  height: 100%;
  background: var(--bg-secondary);
  border-left: 1px solid var(--border);
  z-index: 100;
  transition: right 0.3s cubic-bezier(0.4, 0, 0.2, 1);
  display: flex;
  flex-direction: column;
}

.side-menu.open { right: 0; }

.side-menu-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 20px 16px;
  border-bottom: 1px solid var(--border);
  font-weight: 700;
  font-size: 17px;
}

.side-menu-content {
  flex: 1;
  overflow-y: auto;
  padding: 16px;
  display: flex;
  flex-direction: column;
  gap: 20px;
}

.menu-section { display: flex; flex-direction: column; gap: 10px; }

.menu-section-title {
  font-size: 11px;
  text-transform: uppercase;
  letter-spacing: 1px;
  color: var(--text-muted);
  font-weight: 600;
}

.menu-item {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 10px 12px;
  background: var(--bg-card);
  border-radius: 10px;
  font-size: 14px;
}

.menu-value { color: var(--accent-light, #FF6B9D); font-weight: 600; text-align: right; }
.menu-value-sm { font-size: 11px; max-width: 130px; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }

.overlay {
  position: fixed;
  inset: 0;
  background: rgba(0,0,0,0.5);
  z-index: 99;
  opacity: 0;
  pointer-events: none;
  transition: opacity 0.3s;
}

.overlay.active { opacity: 1; pointer-events: all; }

/* ===== 토스트 ===== */
.toast {
  position: fixed;
  bottom: 100px;
  left: 50%;
  transform: translateX(-50%) translateY(20px);
  background: rgba(30,30,60,0.95);
  color: var(--text-primary);
  padding: 10px 20px;
  border-radius: 20px;
  font-size: 14px;
  border: 1px solid var(--border);
  opacity: 0;
  transition: all 0.3s;
  z-index: 200;
  white-space: nowrap;
}

.toast.show { opacity: 1; transform: translateX(-50%) translateY(0); }

/* ===== TTS ===== */
.tts-mute-btn {
  font-size: 18px;
  opacity: 0.85;
  transition: opacity 0.2s;
}
.tts-mute-btn.muted { opacity: 0.4; }

/* TTS 생성 중 음파 인디케이터 */
.tts-wave {
  display: inline-flex;
  align-items: flex-end;
  gap: 2px;
  height: 14px;
  margin-left: 6px;
  vertical-align: middle;
}
.tts-wave span {
  display: inline-block;
  width: 3px;
  background: var(--accent);
  border-radius: 2px;
  animation: tts-bounce 0.8s ease-in-out infinite;
}
.tts-wave span:nth-child(1) { height: 6px; animation-delay: 0s; }
.tts-wave span:nth-child(2) { height: 12px; animation-delay: 0.15s; }
.tts-wave span:nth-child(3) { height: 8px; animation-delay: 0.3s; }
.tts-wave span:nth-child(4) { height: 14px; animation-delay: 0.45s; }

@keyframes tts-bounce {
  0%, 100% { transform: scaleY(0.5); opacity: 0.6; }
  50% { transform: scaleY(1); opacity: 1; }
}

/* ===== 반응형 ===== */
@media (min-width: 600px) {
  #chat-screen, #character-screen, #setup-screen {
    max-width: 480px;
    margin: 0 auto;
    border-left: 1px solid var(--border);
    border-right: 1px solid var(--border);
  }
}

/* ===== 채팅 화면: 캐릭터 스테이지 내 뷰어 전체 크기 ===== */
#chat-screen .char-model-wrap,
#chat-screen #header-avatar-wrap {
  width: 100% !important;
  height: 100% !important;
  border-radius: 0 !important;
  background: transparent !important;
  overflow: visible !important;
}

#chat-screen #header-avatar-wrap canvas {
  width: 100% !important;
  height: 100% !important;
}

#chat-screen .viewer-emoji,
#chat-screen #header-avatar {
  font-size: 120px !important;
}

/* 채팅화면 전체 레이아웃 - display는 .screen.active 에서 처리 */
#chat-screen.active {
  flex-direction: column;
  height: 100%;
}

