:root {
  --bg: #f5f7fb;
  --surface: #ffffff;
  --text: #1a1d29;
  --text-muted: #6b7280;
  --primary: #4f46e5;
  --primary-dark: #4338ca;
  --border: #e5e7eb;
  --user-bubble: #4f46e5;
  --user-bubble-text: #ffffff;
  --ai-bubble: #ffffff;
  --ai-bubble-text: #1a1d29;
  --correction-bg: #fff7e6;
  --correction-border: #f5c542;
  --error-bg: #fef2f2;
  --error-text: #b91c1c;
  --success: #16a34a;
  --radius: 14px;
}

@media (prefers-color-scheme: dark) {
  :root {
    --bg: #12141c;
    --surface: #1b1e2a;
    --text: #edeef2;
    --text-muted: #9aa0ac;
    --primary: #818cf8;
    --primary-dark: #6366f1;
    --border: #2b2f3d;
    --user-bubble: #6366f1;
    --user-bubble-text: #ffffff;
    --ai-bubble: #232636;
    --ai-bubble-text: #edeef2;
    --correction-bg: #2c2610;
    --correction-border: #b58a1a;
    --error-bg: #2a1414;
    --error-text: #f38a8a;
    --success: #4ade80;
  }
}

* { box-sizing: border-box; }

html, body { max-width: 100%; overflow-x: hidden; }

html { height: 100%; }

body {
  margin: 0;
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, "PingFang SC", "Microsoft YaHei", sans-serif;
  background: var(--bg);
  color: var(--text);
  height: 100vh;
  height: 100dvh; /* accounts for mobile browsers' shrinking/growing toolbar */
  /* Some mobile browsers miscalculate 100dvh on the very first paint
     (before their UI chrome settles) then correct it on the next
     layout pass. --app-height is set/corrected via JS as a more
     reliable override once available; falls back to 100dvh until then. */
  height: var(--app-height, 100dvh);
  overflow: hidden;
  /* Keep content clear of the home indicator / on-screen nav bar when
     installed as a standalone PWA (requires viewport-fit=cover above). */
  padding-bottom: env(safe-area-inset-bottom);
  display: flex;
  flex-direction: column;
}

.app-header {
  padding: 16px 20px;
  background: var(--surface);
  border-bottom: 1px solid var(--border);
}

.brand {
  display: flex;
  align-items: center;
  gap: 12px;
  max-width: 800px;
  margin: 0 auto;
}

.brand-icon { font-size: 32px; }
.brand h1 { margin: 0; font-size: 20px; }
.tagline { margin: 2px 0 0; font-size: 13px; color: var(--text-muted); }

.view {
  flex: 1;
  min-height: 0;
  max-width: 800px;
  width: 100%;
  margin: 0 auto;
  padding: 20px;
  display: flex;
  flex-direction: column;
}

#view-scenarios { overflow-y: auto; }

.hidden { display: none !important; }

.intro {
  color: var(--text-muted);
  font-size: 14px;
  margin: 0 0 16px;
}

.scenario-grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(220px, 1fr));
  gap: 14px;
}

.scenario-card-wrap { position: relative; }

.voice-pref-row {
  display: flex;
  align-items: center;
  flex-wrap: wrap;
  gap: 6px;
  margin-bottom: 16px;
}

.voice-pref-label { font-size: 13px; color: var(--text-muted); margin-right: 2px; }

.voice-pref-btn {
  padding: 5px 12px;
  border-radius: 14px;
  border: 1px solid var(--border);
  background: var(--surface);
  color: var(--text);
  font-size: 13px;
  cursor: pointer;
}
.voice-pref-btn:hover { border-color: var(--primary); }
.voice-pref-btn.active {
  background: var(--primary);
  border-color: var(--primary);
  color: #fff;
}

.custom-scenario-card {
  display: block;
  width: 100%;
  margin-bottom: 16px;
  border-style: dashed;
  border-color: var(--primary);
}
.custom-scenario-card .title-zh { color: var(--primary); }

.scenario-card {
  width: 100%;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 16px;
  text-align: left;
  cursor: pointer;
  transition: transform 0.15s ease, box-shadow 0.15s ease, border-color 0.15s ease;
  color: inherit;
  font: inherit;
}

.scenario-card:hover,
.scenario-card:focus-visible {
  transform: translateY(-2px);
  box-shadow: 0 6px 18px rgba(0, 0, 0, 0.08);
  border-color: var(--primary);
  outline: none;
}

.scenario-card .icon { font-size: 28px; display: block; margin-bottom: 8px; }
.scenario-card .title-zh { font-weight: 600; font-size: 16px; }
.scenario-card .title-en { font-size: 12px; color: var(--text-muted); margin: 2px 0 8px; }
.scenario-card .desc-zh { font-size: 13px; color: var(--text-muted); line-height: 1.4; }

.hide-btn {
  position: absolute;
  top: 8px;
  right: 8px;
  width: 22px;
  height: 22px;
  border-radius: 50%;
  border: none;
  background: rgba(0, 0, 0, 0.08);
  color: var(--text-muted);
  font-size: 13px;
  line-height: 1;
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: center;
}
.hide-btn:hover { background: rgba(0, 0, 0, 0.16); color: var(--text); }

.empty-pinned {
  text-align: center;
  color: var(--text-muted);
  font-size: 14px;
  padding: 24px 0;
}

.toggle-more-btn {
  margin-top: 16px;
  width: 100%;
  padding: 10px;
  border-radius: var(--radius);
  border: 1px dashed var(--border);
  background: none;
  color: var(--text-muted);
  font-size: 14px;
  cursor: pointer;
}
.toggle-more-btn:hover { border-color: var(--primary); color: var(--primary); }
.toggle-more-btn:disabled { opacity: 0.5; cursor: not-allowed; }

.more-scenarios-panel { margin-top: 12px; }

.more-scenario-list {
  display: flex;
  flex-direction: column;
  gap: 8px;
}

.more-scenario-row {
  display: flex;
  align-items: center;
  gap: 10px;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 10px;
  padding: 10px 12px;
}

.more-scenario-row .icon { font-size: 22px; flex-shrink: 0; }
.more-scenario-row .info { flex: 1; min-width: 0; }
.more-scenario-row .title-zh { font-weight: 600; font-size: 14px; }
.more-scenario-row .title-en {
  font-size: 12px;
  color: var(--text-muted);
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.add-btn {
  flex-shrink: 0;
  width: 30px;
  height: 30px;
  border-radius: 50%;
  border: none;
  background: var(--primary);
  color: #fff;
  font-size: 17px;
  cursor: pointer;
}
.add-btn:hover { background: var(--primary-dark); }

/* Chat view */

.chat-header {
  display: flex;
  align-items: center;
  gap: 10px;
  padding-bottom: 12px;
  border-bottom: 1px solid var(--border);
}

.icon-btn { flex-shrink: 0; }
.font-size-control { flex-shrink: 0; }

.icon-btn {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 10px;
  width: 36px;
  height: 36px;
  font-size: 16px;
  cursor: pointer;
  color: var(--text);
}

#view-chat {
  --chat-font-size: 15px;
  font-size: var(--chat-font-size);
}

.font-size-control { display: flex; gap: 6px; }
.font-size-control .icon-btn { width: auto; padding: 0 10px; font-size: 13px; font-weight: 600; }

.chat-title { display: flex; align-items: center; gap: 10px; flex: 1; min-width: 0; }
.chat-icon { font-size: 24px; flex-shrink: 0; }
.chat-title > div { min-width: 0; }
.chat-title-zh,
.chat-title-en {
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}
.chat-title-zh { font-weight: 600; font-size: 1em; }
.chat-title-en { font-size: 0.8em; color: var(--text-muted); }

.chat-messages {
  flex: 1;
  min-height: 0;
  overflow-y: auto;
  padding: 16px 0;
  display: flex;
  flex-direction: column;
  gap: 12px;
}

.chat-empty-hint {
  text-align: center;
  color: var(--text-muted);
  font-size: 14px;
  padding: 32px 16px;
}

.custom-setup-title { margin: 12px 0 4px; font-size: 18px; }

#custom-role-input {
  width: 100%;
  padding: 12px 14px;
  border-radius: 12px;
  border: 1px solid var(--border);
  background: var(--surface);
  color: var(--text);
  font-size: 15px;
  margin-bottom: 12px;
}
#custom-role-input:focus { outline: 2px solid var(--primary); }

.custom-start-btn { width: 100%; padding: 12px; font-size: 15px; border-radius: 12px; }

.msg-row { display: flex; flex-direction: column; max-width: 80%; }
.msg-row.user { align-self: flex-end; align-items: flex-end; }
.msg-row.assistant { align-self: flex-start; align-items: flex-start; }

.bubble {
  padding: 10px 14px;
  border-radius: var(--radius);
  font-size: 1em;
  line-height: 1.5;
  white-space: pre-wrap;
}

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

.msg-row.assistant .bubble {
  background: var(--ai-bubble);
  color: var(--ai-bubble-text);
  border: 1px solid var(--border);
  border-bottom-left-radius: 4px;
}

.replay-btn {
  background: none;
  border: none;
  cursor: pointer;
  font-size: 0.93em;
  margin-top: 4px;
  color: var(--text-muted);
  padding: 0;
}
.replay-btn:hover { color: var(--primary); }

.corrections {
  margin-top: 6px;
  display: flex;
  flex-direction: column;
  gap: 6px;
  max-width: 100%;
}

.correction-card {
  background: var(--correction-bg);
  border: 1px solid var(--correction-border);
  border-radius: 10px;
  padding: 8px 12px;
  font-size: 0.87em;
  line-height: 1.5;
}

.correction-card .line {
  display: flex;
  gap: 6px;
  flex-wrap: wrap;
}

.correction-card .original { text-decoration: line-through; color: var(--text-muted); }
.correction-card .arrow { color: var(--text-muted); }
.correction-card .corrected { font-weight: 600; color: var(--text); }
.correction-card .explanation { margin-top: 4px; color: var(--text-muted); }

.retry-btn {
  margin-top: 8px;
  background: none;
  border: 1px solid var(--correction-border);
  border-radius: 8px;
  padding: 3px 10px;
  font-size: 0.85em;
  cursor: pointer;
  color: var(--text);
}
.retry-btn:hover { background: rgba(0, 0, 0, 0.05); }

.retry-box {
  margin-top: 8px;
  display: flex;
  flex-wrap: wrap;
  gap: 6px;
  align-items: center;
}
.retry-box.hidden { display: none; }

.retry-mic-btn {
  width: 30px;
  height: 30px;
  border-radius: 50%;
  border: none;
  background: var(--primary);
  color: #fff;
  font-size: 14px;
  cursor: pointer;
  flex-shrink: 0;
  transition: background 0.15s ease;
}
.retry-mic-btn:hover { background: var(--primary-dark); }
.retry-mic-btn.listening { background: #ef4444; animation: pulse 1.2s infinite; }

.retry-text-fallback {
  display: flex;
  flex: 1;
  min-width: 120px;
  gap: 6px;
  align-items: center;
}
.retry-mic-btn:disabled { opacity: 0.4; cursor: not-allowed; animation: none; }

.retry-input {
  flex: 1;
  min-width: 0;
  padding: 6px 10px;
  border-radius: 16px;
  border: 1px solid var(--border);
  background: var(--surface);
  color: var(--text);
  font-size: 0.93em;
}
.retry-input:focus { outline: 2px solid var(--primary); }

.retry-submit {
  padding: 6px 14px;
  border-radius: 16px;
  border: none;
  background: var(--primary);
  color: #fff;
  font-size: 0.85em;
  cursor: pointer;
  flex-shrink: 0;
}
.retry-submit:hover { background: var(--primary-dark); }
.retry-submit:disabled { opacity: 0.5; cursor: not-allowed; }

.retry-feedback {
  flex-basis: 100%;
  font-size: 0.87em;
  color: var(--text-muted);
}
.retry-feedback.retry-correct { color: var(--success); font-weight: 600; }

.thinking-indicator {
  font-size: 0.87em;
  color: var(--text-muted);
  padding: 4px 0;
}

.error-banner {
  background: var(--error-bg);
  color: var(--error-text);
  border-radius: 10px;
  padding: 8px 12px;
  font-size: 0.87em;
  margin-bottom: 8px;
}

.chat-input-bar {
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 8px;
  padding-top: 12px;
  border-top: 1px solid var(--border);
}

.text-fallback {
  display: flex;
  flex: 1;
  min-width: 0;
  align-items: center;
  gap: 8px;
}

.mic-btn {
  width: 56px;
  height: 56px;
  border-radius: 50%;
  border: none;
  background: var(--primary);
  color: #fff;
  font-size: 24px;
  cursor: pointer;
  flex-shrink: 0;
  transition: background 0.15s ease, transform 0.15s ease;
}

.mic-btn:hover { background: var(--primary-dark); }

.mic-btn.listening {
  background: #ef4444;
  animation: pulse 1.2s infinite;
}

@keyframes pulse {
  0% { box-shadow: 0 0 0 0 rgba(239, 68, 68, 0.5); }
  70% { box-shadow: 0 0 0 12px rgba(239, 68, 68, 0); }
  100% { box-shadow: 0 0 0 0 rgba(239, 68, 68, 0); }
}

.mic-btn:disabled { opacity: 0.4; cursor: not-allowed; animation: none; }

#text-input {
  flex: 1;
  min-width: 0;
  padding: 10px 14px;
  border-radius: 22px;
  border: 1px solid var(--border);
  background: var(--surface);
  color: var(--text);
  font-size: 1em;
}

#text-input:focus { outline: 2px solid var(--primary); }

.send-btn {
  padding: 10px 18px;
  border-radius: 22px;
  border: none;
  background: var(--primary);
  color: #fff;
  font-size: 0.93em;
  cursor: pointer;
  flex-shrink: 0;
}

.send-btn:hover { background: var(--primary-dark); }
.send-btn:disabled, .mic-btn:disabled { opacity: 0.4; cursor: not-allowed; }

.mic-status {
  font-size: 0.87em;
  color: var(--text-muted);
  text-align: center;
  padding-top: 6px;
  min-height: 18px;
}

@media (max-width: 480px) {
  .view { padding: 14px; }
  .scenario-grid { grid-template-columns: repeat(auto-fill, minmax(160px, 1fr)); }

  .msg-row { max-width: 94%; }

  .chat-header { gap: 6px; }
  .chat-title { gap: 6px; }
  .icon-btn { width: 32px; height: 32px; font-size: 14px; }
  .chat-icon { font-size: 20px; }
  .font-size-control { gap: 4px; }
  .font-size-control .icon-btn { padding: 0 8px; font-size: 12px; }
}
