:root {
  --bg: #10141c;
  --panel: #1a2130;
  --panel2: #232c40;
  --text: #e8ecf4;
  --muted: #8b96ad;
  --accent: #4cc2ff;
  --good: #3ddc84;
  --bad: #ff5d73;
  --gold: #ffc94d;
  --radius: 14px;
}
* { box-sizing: border-box; }
html, body {
  margin: 0;
  font-family: -apple-system, "Segoe UI", Helvetica, Arial, sans-serif;
  background: var(--bg);
  color: var(--text);
  /* stop the whole page from rubber-banding/pull-to-refresh on mobile —
     scrollable areas like .chat-log opt back in below */
  overscroll-behavior-y: none;
}
#app { max-width: 560px; margin: 0 auto; padding: 16px 16px 80px; }

h1, h2 { margin: 8px 0 12px; }
.muted { color: var(--muted); font-size: 0.9em; }
.card {
  background: var(--panel);
  border-radius: var(--radius);
  padding: 16px;
  margin-bottom: 12px;
}
button {
  font: inherit;
  border: 0;
  border-radius: var(--radius);
  padding: 12px 18px;
  background: var(--accent);
  color: #06283c;
  font-weight: 700;
  cursor: pointer;
}
button.secondary { background: var(--panel2); color: var(--text); }
button:disabled { opacity: 0.4; cursor: default; }
input, textarea {
  font: inherit;
  width: 100%;
  padding: 12px;
  margin: 6px 0;
  border-radius: 10px;
  border: 1px solid #2e3950;
  background: var(--panel2);
  color: var(--text);
}
textarea {
  resize: none;
  overflow-y: hidden;
  max-height: 40vh;
  line-height: 1.4;
}
.row { display: flex; gap: 10px; align-items: center; flex-wrap: wrap; }
input[type=range] {
  -webkit-appearance: none; appearance: none;
  width: 100%; padding: 0; margin: 0; height: 28px;
  background: transparent; border: none; accent-color: var(--accent);
}
.slider-row { gap: 12px; margin: 6px 0; flex-wrap: nowrap; }
.slider-row input[type=range] { flex: 1; min-width: 0; }
.slider-row .setting-value { min-width: 4.5em; text-align: right; flex-shrink: 0; }

/* world map: render the SVG at native size and let the user pan/scroll
   rather than scaling it down to fit #app, which made boxes and text
   unreadably small on phones */
.world-map-wrap {
  overflow: auto; -webkit-overflow-scrolling: touch;
  max-height: 70vh; border: 1px solid #2e3950; border-radius: var(--radius);
}
.world-map-wrap svg { display: block; }
.spread { justify-content: space-between; }
.err { color: var(--bad); min-height: 1.2em; }

/* bigger, easier-to-read icon buttons */
.icon-btn { font-size: 1.8em; padding: 8px 14px; }
.icon-btn.recording { background: var(--bad); animation: pulse 1s ease-in-out infinite; }
@keyframes pulse { 0%, 100% { opacity: 1; } 50% { opacity: 0.6; } }

/* speed control segmented buttons */
.segmented { gap: 4px; flex-wrap: nowrap; }
.segmented button { padding: 10px 14px; }

/* chat input row: keep mic/send aligned to the bottom as the textarea grows */
.chat-input { align-items: flex-end; flex-wrap: nowrap; }
.chat-input textarea { flex: 1; margin: 0; }
.chat-input button { margin: 0; }
.chat-input textarea.transcribing { animation: pulse 1s ease-in-out infinite; }

/* full-viewport chat layout: header + input bar stay put, only the
   conversation log scrolls. Wider than the default #app so side-by-side
   translation tiles have more room. */
.chat-page {
  position: fixed; top: 0; left: 0; right: 0;
  bottom: calc(60px + env(safe-area-inset-bottom));
  display: flex; flex-direction: column;
  max-width: 760px; margin: 0 auto;
  padding: 16px 16px 0; box-sizing: border-box;
}
.chat-header { flex: 0 0 auto; }
.chat-header .topbar { margin-bottom: 8px; }
.chat-log {
  flex: 1 1 auto; overflow-y: auto; min-height: 0;
  display: flex; flex-direction: column; gap: 10px;
  padding-bottom: 20px;
  overscroll-behavior-y: contain;
}

/* the conversation goal, shown as the first bubble in the chat log so it
   scrolls away naturally as the conversation continues */
.goal-bar {
  background: var(--panel2);
  border: 1px solid #2e3950;
  border-radius: 10px;
  padding: 8px 12px;
  margin: 0 0 0;
  color: var(--muted);
  font-size: 0.9em;
  line-height: 1.45;
}
.goal-bar:empty { display: none; }

/* transient mic/recording status messages, shown in the chat header */
.status-line {
  font-size: 0.85em;
  margin: 2px 0;
}
.status-line:empty { display: none; }

.chat-inputbar { flex: 0 0 auto; padding: 10px 0; background: var(--bg); }
.chat-inputbar .icon-btn { font-size: 1.4em; padding: 10px 12px; }

/* you vs Hoshi bubbles. scroll-margin-bottom gives breathing room when
   scrollIntoView({block:"end"}) runs during streaming/reveal — without it
   the latest line lands flush against the say-bar/continue button, looking
   cut off. */
.bubble, .bubble-content, .card { scroll-margin-bottom: 16px; }
.bubble { max-width: 92%; margin-bottom: 0; }
.bubble.user { align-self: flex-end; background: #1d3a52; border: 1px solid #2c5577; }
.bubble.assistant { align-self: flex-start; }

/* voice picker */
select.voice-picker {
  background: var(--panel2); color: var(--text); border: 1px solid #2e3950;
  border-radius: var(--radius); padding: 10px 12px; font-size: 0.95em;
}

/* language picker on the Learn tab */
select.lang-picker {
  background: var(--panel2); color: var(--text); border: 1px solid #2e3950;
  border-radius: var(--radius); padding: 12px 14px; font-size: 1.1em;
  font-weight: 700; width: 100%;
}

/* side-by-side translation tiles in chat */
.translate-row { display: flex; gap: 8px; margin: 6px 0; align-items: stretch; }
.translate-row .tile { flex: 1 1 0; padding: 8px 10px; border-radius: 8px; line-height: 1.35; }
.translate-row .tile.orig { flex-grow: 6; background: var(--panel2); }
/* the closing question of a bot turn is the one thing you must respond to —
   highlight it so the rest reads as optional context */
.translate-row .tile.orig.question { border: 2px solid var(--gold); font-weight: 700; }
.translate-row .tile.en { flex-grow: 4; background: transparent; color: var(--muted); font-size: 0.9em; cursor: pointer; }
.translate-row .tile.en.blurred { filter: blur(5px); user-select: none; }

/* target-language text starts blurred so the learner focuses on the audio
   first, then clears in stages as the clip plays; tap reveals immediately */
.translate-row .tile.orig.reveal-3,
.translate-row .tile.orig.reveal-2,
.translate-row .tile.orig.reveal-1 {
  cursor: pointer; user-select: none; transition: filter 0.5s ease;
}
.translate-row .tile.orig.reveal-3 { filter: blur(6px); }
.translate-row .tile.orig.reveal-2 { filter: blur(3.5px); }
.translate-row .tile.orig.reveal-1 { filter: blur(1.5px); }

/* persistent say-again controls, pinned above the input bar */
.say-bar { flex: 0 0 auto; gap: 8px; padding-bottom: 8px; }
.say-btn { padding: 8px 14px; font-size: 0.9em; }

/* "Continue" button between revealed lines at beginner levels — a fill bar
   slides across it over a few seconds, tap to skip ahead immediately */
.continue-row { margin: 4px 0; }
.continue-btn { position: relative; overflow: hidden; padding: 6px 12px; font-size: 0.85em; }
.continue-btn .continue-fill {
  position: absolute; inset: 0; left: 0; z-index: 0;
  background: var(--accent); opacity: 0.25;
  transform: scaleX(0); transform-origin: left;
  animation-name: continue-fill; animation-timing-function: linear; animation-fill-mode: forwards;
}
.continue-btn .continue-label { position: relative; z-index: 1; }
@keyframes continue-fill { to { transform: scaleX(1); } }

/* feedback (🐞) report button + modal overlay */
.report-btn { padding: 4px 10px; font-size: 1.1em; line-height: 1; }
.modal-overlay {
  position: fixed; inset: 0; z-index: 100;
  background: rgba(0,0,0,0.55);
  display: flex; align-items: center; justify-content: center;
  padding: 16px;
}
.modal-overlay .modal { width: 100%; max-width: 440px; margin: 0; }

/* top bar */
.topbar { display: flex; justify-content: space-between; align-items: center; margin-bottom: 14px; }
.stat { font-weight: 700; }
.stat .flame { color: var(--gold); }

/* course map */
.lesson-btn {
  display: flex; width: 100%; text-align: left;
  background: var(--panel2); color: var(--text);
  margin: 6px 0; justify-content: space-between;
}
.lesson-btn.done { background: #1c3a2b; color: var(--good); }

/* lesson player */
.progressbar { height: 10px; background: var(--panel2); border-radius: 5px; overflow: hidden; margin-bottom: 16px; }
.progressbar > div { height: 100%; background: var(--good); transition: width 0.2s; }
.progressbar > div.overdue { animation: progress-pulse 1.5s ease-in-out infinite; }
@keyframes progress-pulse { 0%, 100% { opacity: 1; } 50% { opacity: 0.4; } }
.choices button, .tiles button { margin: 5px 5px 0 0; }
.choices button { display: block; width: 100%; text-align: left; background: var(--panel2); color: var(--text); }
.choices button.picked { outline: 2px solid var(--accent); }
.tiles button { background: var(--panel2); color: var(--text); }
.answer-line { min-height: 48px; border-bottom: 2px solid #2e3950; margin-bottom: 10px; padding: 6px 2px; font-size: 1.1em; }
.feedback { padding: 12px; border-radius: 10px; margin-top: 12px; font-weight: 700; }
.feedback.good { background: #173526; color: var(--good); }
.feedback.bad { background: #3a1b22; color: var(--bad); }
.pairs { display: grid; grid-template-columns: 1fr 1fr; gap: 8px; }
.pairs button { background: var(--panel2); color: var(--text); }
.pairs button.sel { outline: 2px solid var(--accent); }
.pairs button.matched { opacity: 0.35; }
.pairs button.wrongflash { outline: 2px solid var(--bad); }

/* bottom nav */
.nav {
  position: fixed; bottom: 0; left: 0; right: 0;
  display: flex; justify-content: space-around;
  background: var(--panel); padding: 10px 0 calc(10px + env(safe-area-inset-bottom));
}
.nav button { background: none; color: var(--muted); font-size: 0.95em; padding: 6px 14px; }
.nav button.active { color: var(--accent); }

/* leaderboard */
.lb-row { display: flex; justify-content: space-between; padding: 10px 4px; border-bottom: 1px solid #242e44; }
.lb-row .rank { color: var(--muted); width: 2em; }

/* tappable words + dictionary card */
.w { cursor: pointer; }
.w:hover, .w:active { text-decoration: underline dotted var(--accent); }
.wordcard {
  position: fixed; left: 12px; right: 12px; bottom: 70px;
  max-width: 536px; margin: 0 auto;
  background: var(--panel2); border: 1px solid #2e3950;
  border-radius: var(--radius); padding: 14px;
  z-index: 50; box-shadow: 0 8px 30px rgba(0,0,0,0.5);
}

/* Scene Mode: a small CSS "stage" above the chat log */
.scene-stage-wrap { flex: 0 0 auto; }
.scene-stage {
  position: relative; height: 150px; margin-bottom: 10px;
  border-radius: var(--radius); overflow: hidden;
  display: flex; align-items: flex-end; justify-content: space-between;
  padding: 0 18px 14px;
}
.scene-bg-cafe { background: linear-gradient(180deg, #6b4226 0%, #3b2a1c 100%); }
.scene-bg-padaria { background: linear-gradient(180deg, #c97f3a 0%, #5c3a1e 100%); }
.scene-bg-parque { background: linear-gradient(180deg, #4f8a52 0%, #1f3d24 100%); }
.scene-bg-livraria { background: linear-gradient(180deg, #5b4a7a 0%, #2a2240 100%); }

.scene-props { position: absolute; inset: 0; }
.scene-prop {
  position: absolute; font-size: 1.8em; opacity: 0.35;
  filter: drop-shadow(0 2px 4px rgba(0,0,0,0.3));
}
.scene-prop:nth-child(1) { top: 12px; left: 10%; }
.scene-prop:nth-child(2) { top: 20px; left: 40%; font-size: 1.4em; }
.scene-prop:nth-child(3) { top: 14px; right: 14%; }
.scene-prop:nth-child(4) { top: 30px; right: 38%; font-size: 1.3em; }

.scene-avatar { position: relative; display: flex; flex-direction: column; align-items: center; gap: 4px; }
.scene-avatar-circle {
  width: 56px; height: 56px; border-radius: 50%;
  background: var(--panel); border: 2px solid var(--panel2);
  display: flex; align-items: center; justify-content: center;
  font-size: 1.8em;
  transition: border-color 0.15s, box-shadow 0.15s, transform 0.15s;
}
.scene-avatar-label { font-size: 0.8em; color: #fff; text-shadow: 0 1px 3px rgba(0,0,0,0.6); }

/* highlight whichever avatar is currently "speaking" (mirrors the visible bubble) */
.scene-avatar.active .scene-avatar-circle {
  border-color: var(--accent);
  box-shadow: 0 0 0 4px rgba(76, 194, 255, 0.25);
  transform: scale(1.08);
}

/* speech bubble pinned to the top of the stage, mirroring whoever just
   spoke — kept inside the stage's bounds (it's tall enough for ~3 lines) */
.scene-bubble {
  position: absolute; top: 10px;
  width: max-content; max-width: 60%; min-width: 80px; max-height: 100px;
  background: var(--panel); border: 1px solid var(--panel2);
  border-radius: 10px; padding: 8px 10px; font-size: 0.85em;
  text-align: left; line-height: 1.3; white-space: pre-line;
  overflow: hidden;
  opacity: 0; transform: scale(0.9); pointer-events: none; transition: opacity 0.15s, transform 0.15s;
}
.scene-bubble.show { opacity: 1; transform: scale(1); }
.scene-bubble.you { left: 10px; transform-origin: top left; }
.scene-bubble.npc { right: 10px; transform-origin: top right; }

/* inline corrections on the learner's own chat bubble */
.corr-del { color: var(--bad); text-decoration: line-through; margin-right: 2px; }
.corr-ins { color: var(--good); }
.correction-line {
  margin-top: 6px; padding-top: 6px; border-top: 1px dashed #2e3950;
  color: var(--good);
}
.correction-explain { font-style: italic; color: var(--muted); font-size: 0.85em; margin-top: 2px; }

/* ?debug=1 panel: lesson-steering inputs (goal, level, vocab, scene) */
.debug-panel { font-size: 0.85em; font-family: ui-monospace, monospace; }
.debug-panel h2 { margin-top: 0; }
.debug-prompt-wrap summary { cursor: pointer; }

/* Collapsible admin list cards (locations, characters, etc.) — collapsed by
   default so long lists stay scannable; click the summary row to expand. */
details.card { padding: 0; }
details.card > summary {
  cursor: pointer; padding: 16px; font-weight: 600; list-style-position: inside;
}
details.card[open] > summary { padding-bottom: 8px; }
details.card > summary .muted { font-weight: normal; }
details.card > .details-body { padding: 0 16px 16px; }
.debug-prompt {
  white-space: pre-wrap; word-break: break-word; font-size: 0.95em;
  max-height: 300px; overflow-y: auto; margin: 6px 0 0;
  background: var(--panel2); padding: 8px; border-radius: 8px;
}

/* On wide screens, pin the debug panel as a small left sidebar instead of
   eating vertical space above the chat — smaller, bright accent-colored
   monospace text. */
@media (min-width: 1000px) {
  .debug-panel {
    position: fixed; left: 16px; top: 16px; bottom: 16px;
    width: 220px; overflow-y: auto;
    font-size: 0.6em; line-height: 1.4;
    color: var(--accent); padding: 12px;
  }
  .debug-panel > div { margin-bottom: 5px; word-break: break-word; }
  .debug-panel .debug-prompt { font-size: 1em; max-height: 240px; }
}

/* On tablets/laptops, give the single-column layout more room — most
   noticeably the admin world map, which can then scale down to fit the
   available width instead of needing to scroll. Phones keep the native-size
   pannable map from the readability fix above. */
@media (min-width: 900px) {
  #app { max-width: 900px; }
  .world-map-wrap { max-height: 85vh; }
  .world-map-wrap svg {
    max-width: 100%; max-height: 85vh; width: auto; height: auto;
  }
}

/* scene picker grid */
.scene-grid { display: grid; grid-template-columns: 1fr 1fr; gap: 10px; }
.scene-card { text-align: center; cursor: pointer; }
.scene-card.locked { cursor: default; opacity: 0.45; }
.scene-card-emoji { font-size: 2.4em; margin-bottom: 4px; }

.bigxp { font-size: 2.4em; font-weight: 800; color: var(--gold); text-align: center; margin: 18px 0; }
.center { text-align: center; }
.pronounce-emoji { font-size: 4em; text-align: center; margin: 10px 0; }
.pronounce-word { font-size: 2em; font-weight: 800; text-align: center; margin-bottom: 4px; }
