/* ============================================================================
   Wayne · AI 英語對話練習 — Speak App 風格聊天介面
   ============================================================================ */

:root {
  --wayne-bg: #f4f1ff;
  --wayne-primary: #6c5ce7;
  --wayne-primary-dark: #5b4bd1;
  --wayne-accent: #00d2a0;
  --wayne-red: #ff5c72;
  --wayne-red-bg: #ffe8ec;
  --wayne-green-bg: #e3fbf0;
  --wayne-amber-bg: #fff6e0;
  --wayne-amber: #ffb020;
  --wayne-purple-bg: #f0ecff;
  --wayne-text: #2c2c40;
  --wayne-subtext: #8a8aa3;
  --wayne-card-bg: #ffffff;
  --wayne-shadow: 0 8px 24px rgba(108, 92, 231, 0.10);
}

* { box-sizing: border-box; }

html, body {
  margin: 0;
  padding: 0;
  height: 100%;
  font-family: 'Nunito', 'PingFang TC', 'Microsoft JhengHei', sans-serif;
  background: linear-gradient(180deg, #efe9ff 0%, #f7f5ff 40%, #ffffff 100%);
  color: var(--wayne-text);
}

.hidden { display: none !important; }

/* ============ App Shell ============ */
#app-shell {
  display: flex;
  flex-direction: column;
  height: 100vh;
  max-width: 640px;
  margin: 0 auto;
  background: transparent;
  position: relative;
}

/* ============ 頂部導覽列 ============ */
#top-bar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 14px 16px;
  background: rgba(255, 255, 255, 0.85);
  backdrop-filter: blur(10px);
  border-bottom: 1px solid rgba(108, 92, 231, 0.08);
  position: sticky;
  top: 0;
  z-index: 20;
}

#top-bar-left, #top-bar-right {
  display: flex;
  align-items: center;
  gap: 10px;
}

.icon-btn {
  width: 36px;
  height: 36px;
  border-radius: 50%;
  border: none;
  background: #f0ecff;
  color: var(--wayne-primary);
  display: flex;
  align-items: center;
  justify-content: center;
  cursor: pointer;
  transition: all 0.15s ease;
  font-size: 14px;
}
.icon-btn:hover { background: #e2d9ff; transform: scale(1.05); }
.icon-btn.active { background: var(--wayne-primary); color: white; }

#topic-badge {
  display: flex;
  align-items: center;
  gap: 6px;
  background: linear-gradient(135deg, #6c5ce7, #a29bfe);
  color: white;
  padding: 6px 14px;
  border-radius: 999px;
  font-size: 13px;
  font-weight: 700;
  box-shadow: 0 3px 10px rgba(108, 92, 231, 0.3);
  max-width: 240px;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

#streak-badge {
  display: flex;
  align-items: center;
  gap: 4px;
  background: var(--wayne-amber-bg);
  color: #b8791a;
  padding: 6px 10px;
  border-radius: 999px;
  font-size: 13px;
  font-weight: 800;
}

/* ============ 對話串 ============ */
#chat-scroll {
  flex: 1;
  overflow-y: auto;
  padding: 20px 16px 12px;
  -webkit-overflow-scrolling: touch;
}

#chat-thread {
  display: flex;
  flex-direction: column;
  gap: 14px;
}

.chat-row {
  display: flex;
  gap: 8px;
  align-items: flex-end;
  animation: bubbleIn 0.28s ease;
}
.chat-row.user-row { flex-direction: row-reverse; }

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

.avatar {
  width: 32px;
  height: 32px;
  border-radius: 50%;
  flex-shrink: 0;
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 15px;
  background: linear-gradient(135deg, #6c5ce7, #00d2a0);
  color: white;
  box-shadow: 0 2px 6px rgba(0,0,0,0.12);
}
.user-row .avatar { background: linear-gradient(135deg, #ff9f43, #ffb84d); }

.bubble-col {
  display: flex;
  flex-direction: column;
  gap: 6px;
  max-width: 78%;
}
.user-row .bubble-col { align-items: flex-end; }

.bubble {
  padding: 11px 16px;
  border-radius: 20px;
  font-size: 15px;
  line-height: 1.55;
  word-break: break-word;
  box-shadow: 0 2px 8px rgba(0,0,0,0.06);
}

.ai-bubble {
  background: var(--wayne-card-bg);
  color: var(--wayne-text);
  border-bottom-left-radius: 6px;
}

.user-bubble {
  background: linear-gradient(135deg, var(--wayne-primary), #8b7aef);
  color: white;
  border-bottom-right-radius: 6px;
}

/* 使用者原句 diff 顯示：紅字刪除線 + 綠字插入 */
.user-bubble .diff-strike {
  text-decoration: line-through;
  opacity: 0.65;
  color: #ffd7dd;
}
.user-bubble .diff-insert {
  color: #d8ffe9;
  font-weight: 800;
  margin-left: 3px;
}

.bubble-meta {
  font-size: 11px;
  color: var(--wayne-subtext);
  padding: 0 4px;
}

.tts-btn {
  border: none;
  background: #f0ecff;
  color: var(--wayne-primary);
  width: 24px;
  height: 24px;
  border-radius: 50%;
  font-size: 10px;
  cursor: pointer;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  margin-left: 6px;
  vertical-align: middle;
}
.tts-btn:hover { background: #e2d9ff; }
.tts-btn.speaking { background: var(--wayne-primary); color: white; animation: pulse 1s infinite; }

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

/* ============ 回饋卡片群 ============ */
.feedback-stack {
  display: flex;
  flex-direction: column;
  gap: 8px;
  max-width: 88%;
  margin-top: 2px;
}

.feedback-card {
  border-radius: 16px;
  padding: 12px 14px;
  font-size: 13px;
  line-height: 1.5;
  box-shadow: 0 2px 8px rgba(0,0,0,0.05);
  animation: bubbleIn 0.3s ease;
}

.feedback-card-title {
  font-weight: 800;
  font-size: 12px;
  display: flex;
  align-items: center;
  gap: 6px;
  margin-bottom: 6px;
}
.feedback-card-title i { font-size: 11px; }

.correction-card {
  background: var(--wayne-red-bg);
  color: #b03050;
}
.correction-card .feedback-card-title { color: var(--wayne-red); }

.diff-line {
  background: white;
  border-radius: 10px;
  padding: 8px 12px;
  font-size: 14px;
  margin-bottom: 6px;
  color: var(--wayne-text);
}
.diff-line .diff-del {
  text-decoration: line-through;
  color: #e35d75;
  background: #ffe0e6;
  padding: 1px 4px;
  border-radius: 4px;
}
.diff-line .diff-add {
  color: #17a673;
  background: #d9f9ec;
  font-weight: 800;
  padding: 1px 4px;
  border-radius: 4px;
  margin-left: 3px;
}

.feedback-explain {
  color: #7a5060;
  font-size: 12.5px;
}

.native-card {
  background: var(--wayne-amber-bg);
  color: #8a611a;
}
.native-card .feedback-card-title { color: var(--wayne-amber); }
.native-alt-list { display: flex; flex-direction: column; gap: 6px; }
.native-alt-item {
  background: white;
  border-radius: 10px;
  padding: 8px 12px;
}
.native-alt-phrase {
  font-weight: 800;
  color: #4a3a10;
  font-size: 14px;
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 6px;
}
.native-alt-explain {
  color: #9c7c3a;
  font-size: 12px;
  margin-top: 2px;
}

.hint-card {
  background: var(--wayne-purple-bg);
  color: #4a3aa0;
}
.hint-card .feedback-card-title { color: var(--wayne-primary); }
.hint-suggestions { display: flex; flex-direction: column; gap: 6px; margin-bottom: 6px; }
.hint-suggestion-item {
  background: white;
  border-radius: 10px;
  padding: 8px 12px;
  font-size: 14px;
  font-weight: 700;
  color: #3d2f8f;
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 6px;
}
.hint-explain { color: #6d5ec0; }

.badge-row {
  display: flex;
  flex-wrap: wrap;
  gap: 6px;
  max-width: 88%;
}
.status-badge {
  font-size: 11px;
  font-weight: 800;
  padding: 4px 10px;
  border-radius: 999px;
}
.badge-natural { background: var(--wayne-green-bg); color: #17a673; }
.badge-awkward { background: var(--wayne-amber-bg); color: #b8791a; }
.badge-perfect { background: var(--wayne-green-bg); color: #17a673; }
.badge-error { background: var(--wayne-red-bg); color: var(--wayne-red); }
.badge-empathy {
  background: var(--wayne-purple-bg);
  color: var(--wayne-primary-dark);
}

/* ============ Typing indicator ============ */
.typing-bubble {
  display: flex;
  align-items: center;
  gap: 4px;
  padding: 14px 18px;
}
.typing-bubble .dot {
  width: 7px;
  height: 7px;
  border-radius: 50%;
  background: #c7bff5;
  animation: typingBounce 1.2s infinite ease-in-out;
}
.typing-bubble .dot:nth-child(2) { animation-delay: 0.15s; }
.typing-bubble .dot:nth-child(3) { animation-delay: 0.3s; }
@keyframes typingBounce {
  0%, 60%, 100% { transform: translateY(0); opacity: 0.5; }
  30% { transform: translateY(-6px); opacity: 1; }
}
#typing-indicator {
  display: flex;
  padding-left: 40px;
  margin-top: 8px;
}

/* ============ 底部輸入區 ============ */
#composer {
  display: flex;
  align-items: flex-end;
  gap: 8px;
  padding: 12px 14px calc(env(safe-area-inset-bottom, 0px) + 12px);
  background: rgba(255,255,255,0.92);
  backdrop-filter: blur(10px);
  border-top: 1px solid rgba(108, 92, 231, 0.08);
  position: sticky;
  bottom: 0;
}

#composer-input-wrap {
  flex: 1;
  background: #f3f1fb;
  border-radius: 22px;
  padding: 4px 6px;
  display: flex;
}

#text-input {
  flex: 1;
  border: none;
  outline: none;
  background: transparent;
  resize: none;
  font-size: 15px;
  font-family: inherit;
  padding: 8px 10px;
  max-height: 100px;
  color: var(--wayne-text);
}

#composer button {
  border: none;
  border-radius: 50%;
  width: 42px;
  height: 42px;
  flex-shrink: 0;
  display: flex;
  align-items: center;
  justify-content: center;
  cursor: pointer;
  font-size: 16px;
  transition: all 0.15s ease;
  /* 手機防呆：避免長按按鈕時觸發系統的「文字選取/拷貝」選單，
     導致使用者以為按鈕沒反應（其實是被系統選字選單蓋住了點擊事件） */
  -webkit-user-select: none;
  user-select: none;
  -webkit-touch-callout: none;
  touch-action: manipulation;
}

#mic-btn {
  position: relative;
  background: #f0ecff;
  color: var(--wayne-primary);
}
#mic-btn.recording {
  background: var(--wayne-red);
  color: white;
  animation: pulse 1s infinite;
}
/* 進站時偵測到瀏覽器不支援 Web Speech API（例如 iOS Safari / 非 Chromium 瀏覽器），
   直接以灰階視覺 + 右上角警示角標提示使用者，避免點下去才發現不能用 */
#mic-btn.mic-unsupported {
  background: #ececec;
  color: #a0a0ab;
}
#mic-btn.mic-unsupported::after {
  content: '!';
  position: absolute;
  top: -2px;
  right: -2px;
  width: 16px;
  height: 16px;
  border-radius: 50%;
  background: var(--wayne-amber);
  color: white;
  font-size: 11px;
  font-weight: 800;
  display: flex;
  align-items: center;
  justify-content: center;
  line-height: 1;
  box-shadow: 0 0 0 2px #fff;
}
#mic-btn.disabled,
#mic-btn:disabled {
  opacity: 0.4;
  cursor: not-allowed;
  animation: none;
}

#hint-btn {
  background: var(--wayne-amber-bg);
  color: var(--wayne-amber);
}

#send-btn {
  background: var(--wayne-primary);
  color: white;
}
#send-btn:disabled {
  background: #d8d3f5;
  color: white;
  cursor: not-allowed;
}
#send-btn:not(:disabled):hover { background: var(--wayne-primary-dark); transform: scale(1.05); }

/* ============ 開場設定 Modal ============ */
.modal-overlay {
  position: fixed;
  inset: 0;
  background: rgba(44, 44, 64, 0.45);
  backdrop-filter: blur(4px);
  display: flex;
  align-items: center;
  justify-content: center;
  z-index: 100;
  padding: 20px;
}
.modal-overlay.hidden { display: none; }

.modal-card {
  background: white;
  border-radius: 24px;
  max-width: 440px;
  width: 100%;
  max-height: 88vh;
  overflow-y: auto;
  padding: 28px 24px 24px;
  box-shadow: 0 20px 60px rgba(108, 92, 231, 0.25);
  animation: modalPop 0.3s ease;
}
@keyframes modalPop {
  from { opacity: 0; transform: scale(0.94) translateY(10px); }
  to { opacity: 1; transform: scale(1) translateY(0); }
}

.modal-header { text-align: center; margin-bottom: 20px; }
.modal-header-icon {
  font-size: 32px;
  color: var(--wayne-primary);
  margin-bottom: 8px;
}
.modal-header h1 {
  font-size: 20px;
  font-weight: 800;
  margin: 0 0 6px;
  color: var(--wayne-text);
}
.modal-header p {
  font-size: 13px;
  color: var(--wayne-subtext);
  margin: 0;
}

.modal-label {
  display: block;
  font-size: 12px;
  font-weight: 800;
  color: var(--wayne-subtext);
  text-transform: uppercase;
  letter-spacing: 0.03em;
  margin: 16px 0 8px;
}
.modal-label:first-child { margin-top: 0; }

.chip-row {
  display: flex;
  flex-wrap: wrap;
  gap: 8px;
  margin-bottom: 8px;
}
.chip {
  border: 1.5px solid #e3ddff;
  background: white;
  color: var(--wayne-text);
  padding: 7px 14px;
  border-radius: 999px;
  font-size: 13px;
  font-weight: 700;
  cursor: pointer;
  transition: all 0.15s ease;
}
.chip:hover { border-color: var(--wayne-primary); }
.chip-active {
  background: var(--wayne-primary);
  border-color: var(--wayne-primary);
  color: white;
}

.modal-input {
  width: 100%;
  border: 1.5px solid #e3ddff;
  border-radius: 12px;
  padding: 10px 14px;
  font-size: 14px;
  font-family: inherit;
  outline: none;
  color: var(--wayne-text);
}
.modal-input:focus { border-color: var(--wayne-primary); }

.modal-cta {
  width: 100%;
  margin-top: 22px;
  background: linear-gradient(135deg, var(--wayne-primary), #8b7aef);
  color: white;
  border: none;
  border-radius: 14px;
  padding: 14px;
  font-size: 16px;
  font-weight: 800;
  cursor: pointer;
  box-shadow: 0 6px 18px rgba(108, 92, 231, 0.35);
  transition: transform 0.15s ease;
}
.modal-cta:hover { transform: translateY(-1px); }
.modal-cta:active { transform: translateY(0); }

/* ============ Scrollbar ============ */
#chat-scroll::-webkit-scrollbar { width: 5px; }
#chat-scroll::-webkit-scrollbar-thumb { background: #d8d3f5; border-radius: 3px; }

/* ============ Responsive ============ */
@media (min-width: 641px) {
  #app-shell {
    height: 92vh;
    margin-top: 4vh;
    border-radius: 28px;
    overflow: hidden;
    box-shadow: 0 20px 60px rgba(108, 92, 231, 0.18);
    border: 1px solid rgba(108,92,231,0.08);
  }
}

/* ============ 頁面內提示 Toast（取代 alert，避免在 iframe/WebView 中被靜默阻擋） ============ */
#wayne-toast {
  position: fixed;
  left: 50%;
  bottom: 90px;
  transform: translateX(-50%) translateY(20px);
  max-width: 88vw;
  background: rgba(44, 44, 64, 0.94);
  color: #fff;
  padding: 12px 18px;
  border-radius: 14px;
  font-size: 14px;
  line-height: 1.5;
  white-space: pre-line;
  text-align: center;
  box-shadow: 0 8px 24px rgba(0,0,0,0.25);
  z-index: 999;
  opacity: 0;
  pointer-events: none;
  transition: opacity 0.2s ease, transform 0.2s ease;
}
#wayne-toast.show {
  opacity: 1;
  transform: translateX(-50%) translateY(0);
}
