/* Telegram injects --tg-theme-* vars when running in-app; fallbacks cover browser dev. */
:root {
  --bg: var(--tg-theme-bg-color, #ffffff);
  --text: var(--tg-theme-text-color, #1a1a1a);
  --hint: var(--tg-theme-hint-color, #8a8a8e);
  --link: var(--tg-theme-link-color, #2a6df5);
  --button: var(--tg-theme-button-color, #2a6df5);
  --button-text: var(--tg-theme-button-text-color, #ffffff);
  --secondary-bg: var(--tg-theme-secondary-bg-color, #f1f2f6);
  --radius: 14px;
  --gap: 12px;
}

* { box-sizing: border-box; -webkit-tap-highlight-color: transparent; }
html, body { margin: 0; height: 100%; }
body {
  background: var(--bg);
  color: var(--text);
  font: 16px/1.5 -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, sans-serif;
  overflow: hidden;
}

/* ── screens ─────────────────────────────────────────────── */
.screen { display: none; flex-direction: column; height: 100vh; }
.screen.active { display: flex; }
#screen-home, #screen-start, #screen-complete, #screen-vocab, #screen-settings { overflow-y: auto; padding: 0 16px 32px; }

.app-header {
  position: sticky; top: 0; z-index: 5;
  display: flex; align-items: center; gap: 10px;
  padding: 14px 0 10px; background: var(--bg);
}
.app-header h1 { font-size: 20px; margin: 0; flex: 1; }
.beta { font-size: 12px; color: var(--hint); text-transform: uppercase; letter-spacing: .05em; }
.section-title { font-size: 14px; color: var(--hint); text-transform: uppercase; letter-spacing: .04em; margin: 18px 0 8px; }
.muted { color: var(--hint); }
.small { font-size: 13px; }

.back {
  border: none; background: var(--secondary-bg); color: var(--text);
  width: 34px; height: 34px; border-radius: 50%; font-size: 18px; cursor: pointer;
}
/* Compact round action buttons on the right of a header (📖 vocab, ⚙ settings, 🌐 passage,
   🎴 review, ⇄ direction). No `display` set here so the bare [hidden] attribute still hides them. */
.hdr-btn {
  border: none; background: var(--secondary-bg); color: var(--text);
  width: 34px; height: 34px; border-radius: 50%; font-size: 16px; cursor: pointer;
  flex-shrink: 0; padding: 0;
}
.hdr-btn:active { opacity: .7; }
.hdr-btn.active { background: var(--button); color: var(--button-text); }

/* ── cards (campaigns, archetypes) ───────────────────────── */
.card-list { display: flex; flex-direction: column; gap: var(--gap); }
.card {
  background: var(--secondary-bg); border: 2px solid transparent;
  border-radius: var(--radius); padding: 14px 16px; cursor: pointer; text-align: left;
  color: var(--text); width: 100%; transition: border-color .12s;
}
.card:active { opacity: .85; }
.card.selected { border-color: var(--button); }
.card .card-title { font-weight: 600; font-size: 16px; }
.card .card-sub { color: var(--hint); font-size: 14px; margin-top: 3px; }
.card .card-stats { color: var(--hint); font-size: 12px; margin-top: 6px; font-variant-numeric: tabular-nums; }

/* ── forms ───────────────────────────────────────────────── */
.field { display: flex; flex-direction: column; gap: 6px; font-size: 14px; color: var(--hint); margin: 16px 0 0; }
.field input, .field select {
  font-size: 16px; padding: 12px 14px; border-radius: var(--radius);
  border: 1px solid var(--secondary-bg); background: var(--secondary-bg); color: var(--text);
}
.primary {
  margin-top: 20px; width: 100%; padding: 15px; border: none; border-radius: var(--radius);
  background: var(--button); color: var(--button-text); font-size: 16px; font-weight: 600; cursor: pointer;
}
.primary:disabled { opacity: .45; cursor: default; }

/* ── story screen ────────────────────────────────────────── */
.story-header h1 { font-size: 17px; }
.chips { display: flex; gap: 6px; }
.chip { background: var(--secondary-bg); color: var(--hint); font-size: 12px; padding: 3px 8px; border-radius: 10px; }

/* per-turn context bar: turn marker + HP/stat line, mirroring the Telegram status message.
   Sits between the header and the (scrolling) narrative, so it stays put while the story scrolls. */
.turn-meta {
  flex-shrink: 0;
  display: flex; flex-wrap: wrap; align-items: center; gap: 6px 10px;
  padding: 7px 16px;
  border-bottom: 1px solid var(--secondary-bg);
}
.turn-marker {
  font-size: 12px; font-weight: 700; letter-spacing: .04em;
  color: var(--link); text-transform: uppercase; white-space: nowrap;
}
.statline { display: flex; flex-wrap: wrap; gap: 5px; }
.stat-pill {
  background: var(--secondary-bg); color: var(--text);
  font-size: 12px; padding: 3px 8px; border-radius: 10px;
  font-variant-numeric: tabular-nums; white-space: nowrap;
}

.narrative {
  flex: 1; overflow-y: auto; padding: 8px 16px 16px;
  font-size: 17px; line-height: 1.62;
}
.narrative p { margin: 0 0 14px; }
.narrative blockquote {
  margin: 0 0 14px; padding: 4px 0 4px 12px;
  border-left: 3px solid var(--button); color: var(--hint);
}
/* tap-to-translate: every word in a non-English narrative is a tappable span.
   pan-y lets vertical scrolling stay native while horizontal drag selects a phrase. */
.narrative.taps { -webkit-user-select: none; user-select: none; }
.narrative.taps .w { cursor: pointer; border-radius: 3px; touch-action: pan-y; }
.narrative.taps .w.sel { background: var(--button); color: var(--button-text); }
/* key-phrases (phrase-tap): the turn's teachable multi-word phrases, underlined as the
   primary tap targets. The wrapper underlines continuously (incl. interior spaces); inner .w spans
   still drive free-select, so any other text stays drag-selectable. */
.narrative.taps .ph {
  cursor: pointer;
  text-decoration: underline; text-decoration-thickness: 2px; text-underline-offset: 3px;
  text-decoration-color: var(--link); -webkit-text-decoration-color: var(--link);
}

.quiz-q { font-weight: 600; margin-bottom: 10px; }
.quiz-feedback { margin-top: 14px; padding: 12px 14px; border-radius: var(--radius); background: var(--secondary-bg); }
.quiz-feedback.correct { border-left: 3px solid #2ecc71; }
.quiz-feedback.wrong { border-left: 3px solid #e74c3c; }

/* ── sticky bars ─────────────────────────────────────────── */
.sticky-bar { flex-shrink: 0; background: var(--bg); border-top: 1px solid var(--secondary-bg); }
.audio-bar { display: flex; align-items: center; gap: 10px; padding: 8px 16px; }
.audio-bar[hidden] { display: none; }  /* the class sets display:flex, which would override bare [hidden] */
.audio-btn { width: 36px; height: 36px; border-radius: 50%; border: none; background: var(--secondary-bg); color: var(--text); font-size: 15px; cursor: pointer; flex-shrink: 0; }
.audio-btn:active { opacity: .7; }
.audio-scrub { flex: 1; min-width: 40px; height: 4px; accent-color: var(--button); cursor: pointer; }
.audio-time { font-variant-numeric: tabular-nums; min-width: 34px; text-align: right; }
.audio-chip { border: none; background: var(--secondary-bg); color: var(--text); border-radius: 12px; padding: 6px 9px; font-size: 13px; cursor: pointer; white-space: nowrap; flex-shrink: 0; }
.audio-chip.active { background: var(--button); color: var(--button-text); }
/* listening-first: blur the narrative until the learner taps to reveal it */
.narrative.blur { filter: blur(7px); cursor: pointer; transition: filter .15s ease; }

.choices { display: flex; flex-direction: column; gap: 8px; padding: 10px 16px calc(14px + env(safe-area-inset-bottom)); }
.choice {
  width: 100%; text-align: left; padding: 13px 15px; border-radius: var(--radius);
  border: 1px solid var(--secondary-bg); background: var(--secondary-bg); color: var(--text);
  font-size: 15px; cursor: pointer; display: flex; align-items: center; gap: 8px;
}
.choice:active { opacity: .8; }
.choice.answer.correct { border-color: #2ecc71; }
.choice.answer.wrong { border-color: #e74c3c; }
.choice .skill { margin-left: auto; font-size: 12px; color: var(--hint); white-space: nowrap; }
.choice .num { color: var(--hint); font-weight: 600; min-width: 14px; }

/* ── misc ────────────────────────────────────────────────── */
.center { display: flex; flex-direction: column; align-items: center; justify-content: center; height: 100%; gap: 12px; }
.spinner {
  width: 28px; height: 28px; border: 3px solid var(--secondary-bg);
  border-top-color: var(--button); border-radius: 50%; animation: spin .8s linear infinite;
}
@keyframes spin { to { transform: rotate(360deg); } }
.busy { opacity: .5; pointer-events: none; }

/* turn-generation veil: a clean centred spinner + rotating flavour line shown while Sonnet
   writes the next turn (30–60s). Mirrors the bot's "⏳ The fates are weaving…" placeholder. */
#screen-story { position: relative; }
.turn-loading {
  position: absolute; inset: 0; z-index: 8;
  display: flex; flex-direction: column; align-items: center; justify-content: center;
  gap: 18px; padding: 24px; background: var(--bg);
  animation: lt-fade .4s ease;
}
.turn-loading[hidden] { display: none; }  /* class sets display:flex, overriding bare [hidden] */
.turn-loading .spinner { width: 34px; height: 34px; }
.turn-loading-text {
  margin: 0; color: var(--hint); font-size: 15px; font-style: italic;
  text-align: center; max-width: 280px; min-height: 1.5em;
}
@keyframes lt-fade { from { opacity: 0; } to { opacity: 1; } }

/* ── tap-to-translate bottom sheet ───────────────────────── */
/* Non-modal: the container never blocks taps, only the card does — so the learner can keep
   tapping words and the popup persists until they dismiss it. */
.sheet { position: fixed; left: 0; right: 0; bottom: 0; z-index: 20; pointer-events: none; }
.sheet-card {
  pointer-events: auto;
  background: var(--bg); border-top: 1px solid var(--secondary-bg);
  border-radius: 16px 16px 0 0;
  padding: 12px 16px calc(16px + env(safe-area-inset-bottom));
  box-shadow: 0 -8px 30px rgba(0, 0, 0, .22);
  max-height: 55vh; overflow-y: auto;
  transform: translateY(110%); transition: transform .2s ease;
}
.sheet.open .sheet-card { transform: translateY(0); }
.sheet-head { display: flex; align-items: center; gap: 10px; margin-bottom: 8px; }
.sheet-phrase { font-weight: 700; font-size: 17px; flex: 1; word-break: break-word; }
.sheet-star {
  border: none; background: none; font-size: 22px; line-height: 1; cursor: pointer;
  color: var(--hint); padding: 2px 4px;
}
.sheet-star.saved { color: #f5b301; }
.sheet-close {
  border: none; background: var(--secondary-bg); color: var(--text);
  width: 30px; height: 30px; border-radius: 50%; font-size: 14px; cursor: pointer; flex-shrink: 0;
}
.sheet-body { font-size: 15px; line-height: 1.55; }
.sheet-body i { color: var(--text); }
.sheet-spinner { display: flex; justify-content: center; padding: 14px; }

/* ── learning surfaces (Wave 4): vocab panel, flashcards, settings ─────────── */
.vocab-row {
  background: var(--secondary-bg); border-radius: var(--radius);
  padding: 12px 14px; display: flex; align-items: flex-start; gap: 10px;
}
.vocab-main { flex: 1; min-width: 0; }
.vocab-phrase { font-weight: 600; word-break: break-word; }
.vocab-gloss { font-size: 14px; margin-top: 2px; }
.vocab-context { color: var(--hint); font-size: 13px; margin-top: 6px; font-style: italic; }
.vocab-del {
  border: none; background: none; color: var(--hint);
  font-size: 18px; line-height: 1; cursor: pointer; flex-shrink: 0; padding: 2px 4px;
}
.vocab-del:active { opacity: .6; }
.empty-state { color: var(--hint); text-align: center; padding: 48px 24px; font-size: 15px; }

.settings-note { margin-top: 18px; }

/* Flashcards: a centered tap-to-flip card with Again / Got it under it. */
.flash-progress { font-variant-numeric: tabular-nums; }
.flash-stage { flex: 1; display: flex; align-items: center; justify-content: center; padding: 20px 16px; }
.flash-card {
  width: 100%; max-width: 440px; min-height: 220px;
  background: var(--secondary-bg); border: 2px solid transparent; border-radius: 18px;
  display: flex; flex-direction: column; align-items: center; justify-content: center;
  gap: 14px; padding: 24px; cursor: pointer; color: var(--text); text-align: center;
  transition: border-color .12s;
}
.flash-card:active { opacity: .9; }
.flash-card.flipped { border-color: var(--button); }
.flash-face { font-size: 24px; font-weight: 600; word-break: break-word; }
.flash-actions { display: flex; gap: 10px; padding: 10px 16px calc(16px + env(safe-area-inset-bottom)); }
.flash-act { flex: 1; justify-content: center; text-align: center; }

.toast {
  position: fixed; left: 50%; bottom: 24px; transform: translateX(-50%) translateY(20px);
  background: #e74c3c; color: #fff; padding: 10px 16px; border-radius: 10px;
  font-size: 14px; opacity: 0; transition: all .2s; pointer-events: none; max-width: 90%;
}
.toast.show { opacity: 1; transform: translateX(-50%) translateY(0); }
