/* ═══════════════════════════════════════
   邂逅 Encounter - 完整 UI 風格
   ═══════════════════════════════════════ */
:root {
  --bg-primary: #0a0a12;
  --bg-secondary: #12121e;
  --bg-card: #1a1a2e;
  --bg-input: #1e1e32;
  --bg-hover: rgba(255,255,255,0.06);
  --text-primary: #e8e8f0;
  --text-secondary: #8888a0;
  --text-muted: #5a5a70;
  --accent: #d4a5c0;
  --accent2: #7ec8b8;
  --accent-blue: #4a8cd4;
  --border: rgba(255,255,255,0.08);
  --border-hover: rgba(255,255,255,0.15);
  --radius: 12px;
  --radius-sm: 8px;
  --radius-lg: 16px;
  --stage-height: 32vh;
  --stage-min-height: 220px;
  --stage-max-height: 380px;
  --shadow: 0 4px 20px rgba(0,0,0,0.3);
}

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

body {
  font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', 'PingFang TC', 'Microsoft JhengHei', sans-serif;
  background: var(--bg-primary);
  color: var(--text-primary);
  height: 100vh;
  overflow: hidden;
  -webkit-font-smoothing: antialiased;
}

#app { height: 100vh; display: flex; flex-direction: column; }
.view { display: none; height: 100%; flex-direction: column; }
.view.active { display: flex; }

/* ═══════════════════════════════════════
   載入遮罩
   ═══════════════════════════════════════ */
.loading-overlay {
  position: fixed; inset: 0; z-index: 10000;
  background: rgba(10,10,18,0.9);
  backdrop-filter: blur(10px);
  display: flex; flex-direction: column; align-items: center;
  justify-content: center; gap: 16px;
}
.loading-overlay.hidden { display: none; }
.loading-spinner {
  width: 40px; height: 40px;
  border: 3px solid rgba(255,255,255,0.08);
  border-top-color: var(--accent);
  border-radius: 50%;
  animation: spin 0.8s linear infinite;
}
@keyframes spin { to { transform: rotate(360deg); } }
#loading-text { color: var(--text-secondary); font-size: 14px; }

/* ═══════════════════════════════════════
   角色大廳
   ═══════════════════════════════════════ */
.hall-header {
  padding: 48px 24px 20px;
  text-align: center;
}
.hall-title {
  font-size: 28px; font-weight: 300; letter-spacing: 4px;
  background: linear-gradient(135deg, var(--accent), var(--accent2));
  -webkit-background-clip: text;
  -webkit-text-fill-color: transparent;
  background-clip: text;
}
.hall-subtitle { color: var(--text-muted); font-size: 14px; margin-top: 4px; }

.character-grid {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 16px;
  padding: 16px 20px;
  overflow-y: auto;
  flex: 1;
}

.character-card {
  background: var(--bg-card);
  border: 1px solid var(--border);
  border-radius: var(--radius-lg);
  padding: 28px 16px;
  text-align: center;
  cursor: pointer;
  transition: all 0.25s ease;
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 8px;
}
.character-card:hover {
  transform: translateY(-3px);
  box-shadow: var(--shadow);
  border-color: var(--border-hover);
}
.character-card:active { transform: scale(0.97); }
.character-card .card-emoji { font-size: 48px; line-height: 1; }
.character-card .card-name { font-size: 18px; font-weight: 600; }
.character-card .card-title { font-size: 12px; color: var(--text-secondary); }
.character-card .card-personality { font-size: 12px; color: var(--text-muted); margin-top: 4px; }
.character-card[data-id="aina"] { border-color: rgba(212,165,192,0.2); }
.character-card[data-id="qiyan"] { border-color: rgba(126,200,184,0.2); }

/* ═══════════════════════════════════════
   聊天室 — 頂部欄
   ═══════════════════════════════════════ */
.chat-header {
  display: flex; align-items: center; gap: 10px;
  padding: 10px 12px;
  background: var(--bg-secondary);
  border-bottom: 1px solid var(--border);
  flex-shrink: 0;
}
.btn-icon {
  width: 36px; height: 36px; border-radius: 50%;
  background: transparent; border: none;
  color: var(--text-secondary); font-size: 20px;
  cursor: pointer; display: flex; align-items: center; justify-content: center;
  transition: all 0.15s;
}
.btn-icon:hover { background: var(--bg-hover); color: var(--text-primary); }
.chat-character-info { display: flex; align-items: center; gap: 10px; flex: 1; }
.chat-emoji { font-size: 28px; }
.chat-name { font-size: 16px; font-weight: 600; display: block; }
.chat-title { font-size: 11px; color: var(--text-secondary); display: block; }

/* 更多下拉 */
.more-menu { position: relative; }
.more-dropdown {
  display: none; position: absolute; top: 100%; right: 0; z-index: 999;
  background: var(--bg-card); border: 1px solid var(--border);
  border-radius: var(--radius); padding: 6px; min-width: 140px;
  box-shadow: 0 10px 40px rgba(0,0,0,0.4);
}
.more-dropdown.show { display: block; }
.more-dropdown .tool-btn {
  display: block; width: 100%; text-align: left;
  background: none; border: none; color: var(--text-secondary);
  padding: 8px 12px; border-radius: var(--radius-sm);
  font-size: 13px; cursor: pointer; white-space: nowrap;
}
.more-dropdown .tool-btn:hover { background: var(--bg-hover); color: var(--text-primary); }

/* ═══════════════════════════════════════
   狀態條
   ═══════════════════════════════════════ */
.visual-state {
  display: flex; gap: 6px; padding: 6px 12px;
  background: var(--bg-secondary);
  border-bottom: 1px solid var(--border);
  overflow-x: auto; flex-shrink: 0;
  -webkit-overflow-scrolling: touch;
}
.state-badge {
  font-size: 11px; padding: 2px 10px;
  border-radius: 20px; background: var(--bg-input);
  color: var(--text-secondary); white-space: nowrap;
  border: 1px solid var(--border);
  transition: all 0.3s;
}
.state-badge.changed { border-color: var(--accent); color: var(--accent); background: rgba(212,165,192,0.1); }

/* ═══════════════════════════════════════
   角色舞台
   ═══════════════════════════════════════ */
.character-stage {
  height: var(--stage-height);
  min-height: var(--stage-min-height);
  max-height: var(--stage-max-height);
  flex-shrink: 0;
  overflow: hidden;
  position: relative;
  transition: height 0.3s ease;
  background: var(--bg-primary);
}
.character-stage.hidden { height: 0; min-height: 0; }
.scene-bg { width: 100%; height: 100%; position: relative; overflow: hidden; }
.scene-img {
  position: absolute; inset: 0;
  width: 100%; height: 100%;
  object-fit: cover;
  opacity: 0;
  transition: opacity 1s ease, transform 1.2s ease;
}
.scene-img.loaded { opacity: 0.4; }
.scene-img.fade-in { animation: sceneImgIn 1s ease forwards; }
@keyframes sceneImgIn {
  0% { opacity: 0; transform: scale(1.08); }
  100% { opacity: 0.4; transform: scale(1); }
}

.scene-overlay {
  position: absolute; inset: 0;
  background: linear-gradient(180deg, rgba(10,10,18,0.15) 0%, rgba(10,10,18,0.05) 35%, rgba(10,10,18,0.4) 70%, rgba(10,10,18,0.75) 100%);
  z-index: 1; pointer-events: none;
}

.sprite-container {
  position: absolute; inset: 0; z-index: 2;
  display: flex; align-items: flex-end; justify-content: center;
}
.sprite-character-img {
  height: 100%; width: auto; max-width: 100%;
  object-fit: contain; object-position: center bottom;
  transition: opacity 0.6s ease, transform 0.6s ease;
  opacity: 0;
  filter: drop-shadow(0 2px 16px rgba(0,0,0,0.4));
}
.sprite-character-img.loaded { opacity: 1; }
.sprite-character-img.img-enter { animation: charImgIn 0.7s ease forwards; }
@keyframes charImgIn {
  0% { opacity: 0; transform: translateY(30px) scale(0.92); }
  100% { opacity: 1; transform: translateY(0) scale(1); }
}
.sprite-character-img.img-change { animation: charImgChange 0.6s ease; }
@keyframes charImgChange {
  0% { opacity: 0.4; transform: scale(0.95); filter: blur(2px); }
  100% { opacity: 1; transform: scale(1); filter: blur(0); }
}

/* 表情 overlay */
.sprite-expression {
  position: absolute; top: 8px; left: 8px;
  width: 52px; height: 52px; border-radius: 50%;
  border: 2px solid rgba(255,255,255,0.15);
  object-fit: cover;
  opacity: 0;
  box-shadow: 0 2px 12px rgba(0,0,0,0.6);
  z-index: 3; pointer-events: none;
  background: var(--bg-primary);
  transition: opacity 0.3s;
}
.sprite-expression.show { opacity: 1; }
.sprite-expression.hidden { display: none; }
.sprite-expression.express-enter { animation: expressIn 0.35s ease forwards; }
@keyframes expressIn {
  0% { opacity: 0; transform: scale(0.3) translateY(-8px); }
  60% { opacity: 1; transform: scale(1.1); }
  100% { opacity: 1; transform: scale(1) translateY(0); }
}

/* 場景漸層備用 */
.scene-bg.scene-gradient-study_room { background: linear-gradient(180deg, #2a1f3d 0%, #1a1a2e 60%, #16213e 100%); }
.scene-bg.scene-gradient-bedroom { background: linear-gradient(180deg, #3d1f2a 0%, #2e1a1a 60%, #3e2116 100%); }
.scene-bg.scene-gradient-cafe { background: linear-gradient(180deg, #2d3d1f 0%, #1a2e1a 60%, #16213e 100%); }
.scene-bg.scene-gradient-balcony { background: linear-gradient(180deg, #1f2d3d 0%, #1a1a2e 60%, #0d1b2a 100%); }
.scene-bg.scene-gradient-beach { background: linear-gradient(180deg, #1f3d3d 0%, #1a2e2e 60%, #3e7e9e 100%); }
.scene-bg.scene-gradient-garden { background: linear-gradient(180deg, #2d3d1f 0%, #1a3d1a 60%, #1a2e1a 100%); }
.scene-bg.scene-gradient-default { background: linear-gradient(180deg, #1a1a2e 0%, #0a0a12 100%); }
.scene-bg.scene-gradient-gaming { background: linear-gradient(180deg, #1a0a2e 0%, #16213e 60%, #0a1628 100%); }
.scene-bg.scene-gradient-lab { background: linear-gradient(180deg, #0a1a2e 0%, #1a2a3e 60%, #0a1a2e 100%); }
.scene-bg.scene-gradient-rooftop { background: linear-gradient(180deg, #1a1a3e 0%, #2a1a2e 60%, #1a0a1e 100%); }
.scene-bg.scene-gradient-server { background: linear-gradient(180deg, #0a0a1e 0%, #1a1a2e 60%, #0a0a18 100%); }

/* ═══════════════════════════════════════
   工具列
   ═══════════════════════════════════════ */
.chat-toolbar {
  display: flex; gap: 4px; padding: 6px 12px;
  background: var(--bg-secondary);
  border-bottom: 1px solid var(--border);
  flex-shrink: 0;
  overflow-x: auto;
  -webkit-overflow-scrolling: touch;
}
.chat-toolbar .tool-btn {
  background: var(--bg-input);
  border: 1px solid var(--border);
  color: var(--text-secondary);
  padding: 5px 12px; border-radius: var(--radius-sm);
  font-size: 12px; cursor: pointer;
  transition: all 0.2s;
  white-space: nowrap;
  font-family: inherit;
}
.chat-toolbar .tool-btn:hover {
  background: rgba(74,140,212,0.12);
  border-color: rgba(74,140,212,0.25);
  color: #fff;
}
.chat-toolbar .tool-btn:active { transform: scale(0.95); }
.tool-divider {
  width: 1px; height: 20px;
  background: var(--border);
  margin: auto 4px;
}

/* ═══════════════════════════════════════
   訊息區域
   ═══════════════════════════════════════ */
.message-area {
  flex: 1; overflow-y: auto; padding: 16px;
  display: flex; flex-direction: column; gap: 12px;
  -webkit-overflow-scrolling: touch;
}

.msg-wrap {
  display: flex; align-items: flex-end; gap: 6px;
  max-width: 88%;
  animation: msgIn 0.25s ease;
}
.msg-wrap.user-wrap { align-self: flex-end; flex-direction: row-reverse; }
.msg-wrap.bot-wrap { align-self: flex-start; }

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

.msg-avatar {
  width: 28px; height: 28px; border-radius: 50%;
  flex-shrink: 0;
  background: linear-gradient(135deg, #2a5f8a, var(--accent-blue));
  display: flex; align-items: center; justify-content: center;
  font-size: 12px; font-weight: 700; color: #fff;
}
.msg-avatar.user-avatar { background: var(--bg-input); }

.msg-content {
  display: flex; flex-direction: column;
  gap: 2px;
}

.msg-bubble {
  padding: 10px 14px; border-radius: 16px;
  font-size: 14px; line-height: 1.6;
  white-space: pre-wrap; word-break: break-word;
  position: relative;
}
.msg-bubble.user {
  background: linear-gradient(135deg, #1e4d7a, #3a7bb5);
  border-bottom-right-radius: 4px;
  color: #fff;
}
.msg-bubble.bot {
  background: rgba(255,255,255,0.06);
  border: 1px solid var(--border);
  border-bottom-left-radius: 4px;
}

.msg-time {
  font-size: 10px; color: var(--text-muted);
  padding: 0 4px;
  margin-top: 1px;
}
.user-wrap .msg-time { text-align: right; }
.bot-wrap .msg-time { text-align: left; }

/* 語音播放按鈕 */
.btn-tts {
  display: inline-flex; align-items: center; gap: 4px;
  background: none; border: 1px solid var(--border);
  color: var(--text-muted); font-size: 11px;
  padding: 2px 8px; border-radius: 10px;
  cursor: pointer; margin-top: 4px;
  transition: all 0.2s; font-family: inherit;
}
.btn-tts:hover { border-color: var(--accent); color: var(--accent); background: rgba(212,165,192,0.08); }
.btn-tts.playing { border-color: var(--accent2); color: var(--accent2); }

/* 聊天中的圖片 */
.chat-img {
  max-width: 260px; border-radius: var(--radius);
  margin-top: 6px; cursor: pointer;
  transition: transform 0.15s;
  border: 1px solid var(--border);
}
.chat-img:hover { transform: scale(1.02); }

/* 系統訊息 */
.msg-system {
  align-self: center;
  font-size: 11px; color: var(--text-muted);
  padding: 4px 12px;
}

.greeting-msg {
  align-self: flex-start;
  background: rgba(255,255,255,0.04);
  border: 1px solid var(--border);
  border-radius: 16px;
  border-bottom-left-radius: 4px;
  padding: 10px 14px;
  font-size: 14px;
  color: var(--text-secondary);
}

/* ═══════════════════════════════════════
   輸入區域
   ═══════════════════════════════════════ */
.input-area {
  display: flex; align-items: flex-end; gap: 8px;
  padding: 10px 12px;
  background: var(--bg-secondary);
  border-top: 1px solid var(--border);
  flex-shrink: 0;
}
#chat-input {
  flex: 1; resize: none;
  background: var(--bg-input); border: 1px solid var(--border);
  border-radius: 20px; padding: 10px 16px;
  color: var(--text-primary); font-size: 14px;
  font-family: inherit; outline: none;
  max-height: 100px; line-height: 1.4;
}
#chat-input:focus { border-color: rgba(74,140,212,0.4); }
#chat-input::placeholder { color: var(--text-muted); }

.btn-send {
  width: 40px; height: 40px; border-radius: 50%;
  border: none; background: var(--accent);
  color: #1a1a24; font-size: 18px;
  cursor: pointer; display: flex; align-items: center; justify-content: center;
  transition: all 0.15s;
  flex-shrink: 0;
}
.btn-send:disabled { opacity: 0.3; }
.btn-send:not(:disabled):active { transform: scale(0.9); }

/* ═══════════════════════════════════════
   Modal 換裝/場景選單
   ═══════════════════════════════════════ */
.modal-overlay {
  position: fixed; inset: 0; z-index: 9999;
  background: rgba(10,10,18,0.7);
  backdrop-filter: blur(8px);
  display: flex; align-items: center; justify-content: center;
}
.modal-overlay.hidden { display: none; }

.modal-card {
  background: var(--bg-card);
  border: 1px solid var(--border);
  border-radius: var(--radius-lg);
  width: 320px; max-width: 88vw;
  max-height: 70vh;
  display: flex; flex-direction: column;
  box-shadow: 0 20px 60px rgba(0,0,0,0.5);
  animation: modalIn 0.25s ease;
}
@keyframes modalIn {
  from { opacity: 0; transform: scale(0.9) translateY(10px); }
  to { opacity: 1; transform: scale(1) translateY(0); }
}

.modal-header {
  display: flex; justify-content: space-between; align-items: center;
  padding: 16px 20px 12px;
  border-bottom: 1px solid var(--border);
}
.modal-title { font-size: 16px; font-weight: 600; }
.modal-close {
  background: none; border: none;
  color: var(--text-muted); font-size: 18px;
  cursor: pointer; width: 28px; height: 28px;
  border-radius: 50%; display: flex;
  align-items: center; justify-content: center;
}
.modal-close:hover { background: var(--bg-hover); color: var(--text-primary); }

.modal-body {
  padding: 16px 20px;
  overflow-y: auto;
  flex: 1;
}

.option-grid {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 8px;
}

.option-btn {
  background: var(--bg-input);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 12px 8px;
  text-align: center;
  cursor: pointer;
  transition: all 0.2s;
  color: var(--text-secondary);
  font-size: 13px;
  font-family: inherit;
}
.option-btn:hover {
  border-color: rgba(74,140,212,0.3);
  background: rgba(74,140,212,0.08);
  color: #fff;
}
.option-btn.active {
  border-color: var(--accent);
  background: rgba(212,165,192,0.1);
  color: var(--accent);
}
.option-btn .opt-icon { font-size: 24px; display: block; margin-bottom: 4px; }

/* ═══════════════════════════════════════
   燈箱
   ═══════════════════════════════════════ */
.lightbox-overlay {
  position: fixed; inset: 0; z-index: 100000;
  background: rgba(0,0,0,0.9);
  display: flex; align-items: center; justify-content: center;
  cursor: zoom-out;
}
.lightbox-overlay.hidden { display: none; }
.lightbox-img {
  max-width: 92vw; max-height: 88vh;
  border-radius: var(--radius);
  box-shadow: 0 20px 60px rgba(0,0,0,0.5);
}

/* ═══════════════════════════════════════
   響應式
   ═══════════════════════════════════════ */
@media (min-width: 600px) {
  .character-grid { grid-template-columns: 1fr 1fr 1fr; }
  .msg-wrap { max-width: 75%; }
  :root { --stage-height: 36vh; --stage-max-height: 450px; }
}
@media (max-width: 380px) {
  .character-grid { gap: 12px; padding: 12px; }
  .character-card { padding: 16px 12px; }
  .character-card .card-emoji { font-size: 36px; }
  :root { --stage-height: 28vh; --stage-min-height: 180px; --stage-max-height: 280px; }
  .chat-toolbar .tool-btn { font-size: 11px; padding: 4px 8px; }
}
