:root {
  --bg: #faf6ef;
  --bg-2: #f3ecdf;
  --card: #ffffff;
  --ink: #1f1b3a;
  --ink-2: #4b4766;
  --muted: #8a8599;
  --line: #e7dfd0;
  --accent: #6b4fd8;
  --accent-ink: #ffffff;
  --accent-soft: #ece7ff;
  --tr: #7b52d6;
  --warm: #e0793a;
  --ok: #2f9e6e;
  --ok-soft: #e2f5ec;
  --bad: #d94f4f;
  --bad-soft: #fde8e8;
  --hl: #fff1c9;
  --shadow: 0 1px 2px rgba(31, 27, 58, .06), 0 8px 24px rgba(31, 27, 58, .08);
  --radius: 18px;
  --serif: "Fraunces", Georgia, "Times New Roman", serif;
  --sans: "Inter", system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
  color-scheme: light;
}
@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) {
    --bg: #14121f; --bg-2: #1b1829; --card: #221f33; --ink: #f2eff9; --ink-2: #c9c4dc;
    --muted: #8f89a6; --line: #332f48; --accent: #9b85ff; --accent-ink: #14121f;
    --accent-soft: #2c2650; --tr: #b49cff; --ok-soft: #173a2c; --bad-soft: #3d1d22;
    --hl: #3b3320; --shadow: 0 1px 2px rgba(0,0,0,.3), 0 8px 24px rgba(0,0,0,.35);
    color-scheme: dark;
  }
}
:root[data-theme="dark"] {
  --bg: #14121f; --bg-2: #1b1829; --card: #221f33; --ink: #f2eff9; --ink-2: #c9c4dc;
  --muted: #8f89a6; --line: #332f48; --accent: #9b85ff; --accent-ink: #14121f;
  --accent-soft: #2c2650; --tr: #b49cff; --ok-soft: #173a2c; --bad-soft: #3d1d22;
  --hl: #3b3320; --shadow: 0 1px 2px rgba(0,0,0,.3), 0 8px 24px rgba(0,0,0,.35);
  color-scheme: dark;
}

* { box-sizing: border-box; }
html, body { margin: 0; }
body {
  background: var(--bg);
  color: var(--ink);
  font-family: var(--sans);
  font-size: 16px;
  line-height: 1.5;
  -webkit-font-smoothing: antialiased;
  min-height: 100vh;
}
button { font: inherit; color: inherit; cursor: pointer; }
a { color: inherit; }
main { max-width: 760px; margin: 0 auto; padding: 16px 16px 120px; outline: none; }

/* ---------- top bar ---------- */
.topbar {
  position: sticky; top: 0; z-index: 20;
  display: flex; align-items: center; gap: 12px;
  padding: 10px 16px; padding-top: max(10px, env(safe-area-inset-top));
  background: color-mix(in srgb, var(--bg) 88%, transparent);
  backdrop-filter: blur(12px);
  border-bottom: 1px solid var(--line);
}
.brand { display: flex; align-items: center; gap: 8px; text-decoration: none; }
.brand-mark { font-size: 22px; }
.brand-name { font-family: var(--serif); font-size: 19px; letter-spacing: -.01em; }
.brand-name b { font-weight: 600; color: var(--accent); }
.top-stats { display: flex; gap: 8px; margin-left: auto; font-size: 14px; font-weight: 600; }
.stat-pill { display: inline-flex; align-items: center; gap: 4px; padding: 4px 10px; border-radius: 999px; background: var(--card); border: 1px solid var(--line); }
.top-actions { display: flex; gap: 6px; }
.chip, .icon-btn {
  border: 1px solid var(--line); background: var(--card); border-radius: 999px;
  height: 34px; min-width: 34px; padding: 0 12px; font-size: 13px; font-weight: 700;
}
.icon-btn { padding: 0; font-size: 16px; }
@media (max-width: 480px) {
  .brand-name { display: none; }
  .top-stats { margin-left: 0; flex: 1; justify-content: center; }
}

/* ---------- home ---------- */
.hero { padding: 20px 4px 8px; }
.hero h1 { font-family: var(--serif); font-weight: 600; font-size: clamp(28px, 6vw, 40px); line-height: 1.1; margin: 0 0 8px; letter-spacing: -.02em; }
.hero p { color: var(--ink-2); margin: 0; max-width: 52ch; }
.overall { margin: 18px 0 8px; background: var(--card); border: 1px solid var(--line); border-radius: var(--radius); padding: 14px 16px; display: flex; align-items: center; gap: 14px; box-shadow: var(--shadow); }
.overall .ring { --p: 0; width: 54px; height: 54px; border-radius: 50%; flex: none; display: grid; place-items: center; font-weight: 700; font-size: 13px;
  background: radial-gradient(closest-side, var(--card) 76%, transparent 78% 100%), conic-gradient(var(--accent) calc(var(--p) * 1%), var(--line) 0); }
.overall-text b { display: block; }
.overall-text span { color: var(--muted); font-size: 14px; }
.continue-btn { margin-left: auto; }

.stage { margin-top: 28px; }
.stage-head { display: flex; align-items: center; gap: 12px; margin-bottom: 12px; }
.stage-badge { width: 44px; height: 44px; border-radius: 14px; display: grid; place-items: center; color: #fff; font-weight: 700; flex: none; box-shadow: var(--shadow); }
.stage-head h2 { margin: 0; font-size: 18px; }
.stage-head p { margin: 0; color: var(--muted); font-size: 14px; }
.stage-level { margin-left: auto; font-size: 12px; font-weight: 700; padding: 3px 8px; border-radius: 6px; background: var(--bg-2); color: var(--ink-2); }
.stage.locked .stories { opacity: .55; filter: grayscale(.6); }
.lock-note { font-size: 13px; color: var(--muted); margin: -4px 0 10px 56px; }

.stories { display: grid; grid-template-columns: repeat(auto-fill, minmax(210px, 1fr)); gap: 12px; }
.story-card {
  position: relative; text-align: left; border: 1px solid var(--line); background: var(--card);
  border-radius: var(--radius); padding: 0; overflow: hidden; box-shadow: var(--shadow);
  transition: transform .15s ease, box-shadow .15s ease;
}
.story-card:not(:disabled):hover { transform: translateY(-2px); }
.story-card:disabled { cursor: not-allowed; }
.story-art { height: 92px; display: flex; align-items: center; justify-content: center; gap: 6px; font-size: 34px; }
.story-body { padding: 12px 14px 14px; }
.story-body h3 { margin: 0 0 2px; font-family: var(--serif); font-weight: 600; font-size: 18px; }
.story-body .sub { color: var(--muted); font-size: 13px; }
.story-meta { display: flex; justify-content: space-between; align-items: center; margin-top: 10px; font-size: 12px; color: var(--muted); }
.done-badge { color: var(--ok); font-weight: 700; }
.lock-badge { position: absolute; top: 10px; right: 10px; font-size: 16px; }

/* ---------- reader ---------- */
.back { display: inline-flex; align-items: center; gap: 6px; text-decoration: none; color: var(--ink-2); font-weight: 600; font-size: 14px; margin: 4px 0 12px; }
.scene {
  border-radius: 24px; padding: 22px 18px 18px; text-align: center; color: #fff; position: relative; overflow: hidden;
  box-shadow: var(--shadow);
}
.scene::after { content: ""; position: absolute; inset: 0; background: radial-gradient(circle at 20% 0%, rgba(255,255,255,.25), transparent 50%); pointer-events: none; }
.scene-emojis { font-size: 46px; display: flex; justify-content: center; gap: 14px; filter: drop-shadow(0 6px 10px rgba(0,0,0,.2)); }
.scene-emojis span { animation: float 4s ease-in-out infinite; }
.scene-emojis span:nth-child(2) { animation-delay: -1.3s; font-size: 58px; }
.scene-emojis span:nth-child(3) { animation-delay: -2.6s; }
@keyframes float { 50% { transform: translateY(-6px); } }
.scene h1 { font-family: var(--serif); font-weight: 600; margin: 10px 0 0; font-size: 28px; letter-spacing: -.01em; }
.scene .scene-tr { opacity: .85; font-size: 14px; margin-top: 2px; }
.progress { height: 6px; border-radius: 99px; background: rgba(255,255,255,.3); margin-top: 16px; overflow: hidden; }
.progress > i { display: block; height: 100%; width: 0; background: #fff; border-radius: inherit; transition: width .3s ease; }

.toolbar {
  position: sticky; top: 56px; z-index: 10; display: flex; flex-wrap: wrap; gap: 8px; align-items: center;
  padding: 10px 0; margin: 8px 0 4px; background: var(--bg);
}
.play-btn {
  display: inline-flex; align-items: center; gap: 8px; border: 0; background: var(--accent); color: var(--accent-ink);
  border-radius: 999px; padding: 10px 18px; font-weight: 700; box-shadow: var(--shadow);
}
.seg { display: inline-flex; border: 1px solid var(--line); border-radius: 999px; background: var(--card); overflow: hidden; }
.seg button { border: 0; background: transparent; padding: 7px 11px; font-size: 13px; font-weight: 600; color: var(--ink-2); }
.seg button[aria-pressed="true"] { background: var(--accent-soft); color: var(--accent); }
.toolbar .spacer { flex: 1; }

.lines { list-style: none; margin: 0; padding: 0; display: flex; flex-direction: column; gap: 4px; }
.line {
  padding: 12px 14px; border-radius: 14px; cursor: pointer; transition: background .2s ease;
  border: 1px solid transparent; position: relative;
}
.line:hover { background: var(--card); border-color: var(--line); }
.line.active { background: var(--card); border-color: var(--accent); box-shadow: var(--shadow); }
.line .tr { color: var(--tr); font-weight: 600; font-size: 14px; line-height: 1.35; margin-bottom: 3px; transition: filter .2s ease, opacity .2s ease; }
.line .en { font-family: var(--serif); font-size: 21px; line-height: 1.4; }
.tr-blur .line .tr { filter: blur(5px); opacity: .7; user-select: none; }
.tr-blur .line .tr.revealed { filter: none; opacity: 1; }
.tr-hide .line .tr { display: none; }
.w { border-radius: 4px; padding: 0 1px; transition: background .15s ease; }
.w.known { text-decoration: underline dotted var(--muted); text-underline-offset: 4px; text-decoration-thickness: 1.5px; }
.w:hover { background: var(--accent-soft); }
.w.speaking { background: var(--hl); }
.line-num { position: absolute; left: -26px; top: 15px; font-size: 12px; color: var(--muted); width: 20px; text-align: right; }
@media (max-width: 860px) { .line-num { display: none; } }

.section-title { font-size: 13px; text-transform: uppercase; letter-spacing: .08em; color: var(--muted); font-weight: 700; margin: 28px 0 10px; }
.vocab-grid { display: grid; grid-template-columns: repeat(auto-fill, minmax(160px, 1fr)); gap: 8px; }
.vocab-item { display: flex; align-items: center; gap: 8px; background: var(--card); border: 1px solid var(--line); border-radius: 12px; padding: 8px 10px; text-align: left; }
.vocab-item b { font-family: var(--serif); font-weight: 600; font-size: 16px; display: block; }
.vocab-item span { color: var(--muted); font-size: 13px; }
.vocab-item .spk { margin-left: auto; font-size: 14px; opacity: .7; }

.cta-bar {
  position: fixed; left: 0; right: 0; bottom: 0; z-index: 15; padding: 12px 16px; padding-bottom: max(12px, env(safe-area-inset-bottom));
  background: color-mix(in srgb, var(--bg) 92%, transparent); backdrop-filter: blur(12px); border-top: 1px solid var(--line);
}
.cta-bar .inner { max-width: 760px; margin: 0 auto; display: flex; gap: 10px; align-items: center; }
.btn {
  border: 1px solid var(--line); background: var(--card); border-radius: 14px; padding: 12px 18px; font-weight: 700;
  display: inline-flex; align-items: center; justify-content: center; gap: 8px; text-decoration: none;
}
.btn.primary { background: var(--accent); color: var(--accent-ink); border-color: transparent; box-shadow: var(--shadow); }
.btn.block { flex: 1; }
.btn:disabled { opacity: .5; cursor: not-allowed; }

/* ---------- popover ---------- */
.popover {
  position: fixed; z-index: 40; min-width: 180px; max-width: 280px; background: var(--ink); color: var(--bg);
  border-radius: 14px; padding: 10px 12px; box-shadow: 0 12px 32px rgba(0,0,0,.25); font-size: 14px;
}
.popover b { font-family: var(--serif); font-size: 18px; font-weight: 600; }
.popover .pop-tr { margin-top: 2px; }
.popover .pop-actions { display: flex; gap: 6px; margin-top: 8px; }
.popover button { border: 0; border-radius: 999px; padding: 5px 10px; background: rgba(255,255,255,.15); color: inherit; font-size: 12px; font-weight: 700; }
:root[data-theme="dark"] .popover button, .popover button { background: color-mix(in srgb, var(--bg) 18%, transparent); }

/* ---------- practice ---------- */
.quiz-top { display: flex; align-items: center; gap: 12px; margin: 4px 0 18px; }
.quiz-top .bar { flex: 1; height: 10px; background: var(--line); border-radius: 99px; overflow: hidden; }
.quiz-top .bar i { display: block; height: 100%; background: var(--ok); width: 0; transition: width .3s ease; border-radius: inherit; }
.quiz-top .close { text-decoration: none; font-size: 20px; color: var(--muted); }
.q-kind { font-size: 13px; font-weight: 700; color: var(--accent); text-transform: uppercase; letter-spacing: .06em; }
.q-prompt { font-family: var(--serif); font-size: clamp(22px, 5vw, 28px); line-height: 1.3; margin: 8px 0 20px; }
.q-prompt small { display: block; font-family: var(--sans); font-size: 14px; color: var(--muted); margin-top: 6px; }
.options { display: grid; gap: 10px; }
.opt {
  text-align: left; border: 2px solid var(--line); background: var(--card); border-radius: 14px; padding: 14px 16px; font-size: 17px;
  transition: border-color .15s ease, background .15s ease;
}
.opt:hover:not(:disabled) { border-color: var(--accent); }
.opt.correct { border-color: var(--ok); background: var(--ok-soft); }
.opt.wrong { border-color: var(--bad); background: var(--bad-soft); }
.big-listen { display: inline-flex; align-items: center; justify-content: center; width: 84px; height: 84px; border-radius: 50%; border: 0; background: var(--accent); color: var(--accent-ink); font-size: 34px; box-shadow: var(--shadow); margin: 6px 0 20px; }
.builder-answer { min-height: 64px; border-bottom: 2px dashed var(--line); display: flex; flex-wrap: wrap; gap: 8px; padding: 8px 0 12px; margin-bottom: 18px; }
.builder-pool { display: flex; flex-wrap: wrap; gap: 8px; }
.tile { border: 1px solid var(--line); border-bottom-width: 3px; background: var(--card); border-radius: 12px; padding: 8px 12px; font-size: 17px; font-family: var(--serif); }
.tile.used { visibility: hidden; }
.feedback { margin-top: 18px; border-radius: 14px; padding: 12px 14px; font-weight: 600; }
.feedback.ok { background: var(--ok-soft); color: var(--ok); }
.feedback.bad { background: var(--bad-soft); color: var(--bad); }
.feedback .answer { display: block; font-weight: 500; color: var(--ink); margin-top: 4px; font-family: var(--serif); font-size: 17px; }

.result { text-align: center; padding: 30px 0; }
.result .big { font-size: 64px; }
.result h2 { font-family: var(--serif); font-size: 32px; margin: 8px 0; }
.result p { color: var(--ink-2); }
.result-stats { display: flex; justify-content: center; gap: 12px; margin: 20px 0; }
.result-stats div { background: var(--card); border: 1px solid var(--line); border-radius: 14px; padding: 12px 18px; min-width: 100px; }
.result-stats b { display: block; font-size: 22px; }
.result-stats span { font-size: 13px; color: var(--muted); }
.result-actions { display: flex; flex-wrap: wrap; gap: 10px; justify-content: center; }

/* ---------- settings ---------- */
.sheet { border: 0; padding: 0; border-radius: 22px; background: var(--card); color: var(--ink); width: min(440px, calc(100vw - 32px)); box-shadow: 0 24px 60px rgba(0,0,0,.3); }
.sheet::backdrop { background: rgba(20, 18, 31, .45); backdrop-filter: blur(2px); }
.sheet-body { padding: 20px; display: grid; gap: 16px; }
.sheet-body h2 { margin: 0; font-family: var(--serif); }
.field { display: grid; gap: 6px; font-size: 14px; font-weight: 600; }
.field select, .field input[type="range"] { font: inherit; }
.field select { padding: 10px; border-radius: 10px; border: 1px solid var(--line); background: var(--bg); color: var(--ink); }
.check { display: flex; gap: 10px; align-items: center; font-size: 14px; font-weight: 500; }
.hint { font-size: 12px; color: var(--muted); font-weight: 400; }
.sheet-actions { display: flex; gap: 10px; justify-content: space-between; }
.danger { color: var(--bad); }

/* ---------- toast ---------- */
.toast {
  position: fixed; left: 50%; bottom: 96px; transform: translate(-50%, 20px); opacity: 0; pointer-events: none; z-index: 50;
  background: var(--ink); color: var(--bg); padding: 10px 16px; border-radius: 999px; font-weight: 600; font-size: 14px;
  transition: all .25s ease; max-width: calc(100vw - 32px); text-align: center;
}
.toast.show { opacity: 1; transform: translate(-50%, 0); }

.notice { background: var(--accent-soft); color: var(--ink); border-radius: 14px; padding: 10px 14px; font-size: 14px; margin: 8px 0; }

@media (prefers-reduced-motion: reduce) {
  *, *::before, *::after { animation: none !important; transition: none !important; }
}

.btn, .chip { white-space: nowrap; }
@media (max-width: 520px) {
  .stories { grid-template-columns: repeat(2, minmax(0, 1fr)); gap: 10px; }
  .story-art { height: 70px; font-size: 24px; gap: 2px; }
  .story-body { padding: 10px 12px 12px; }
  .story-body h3 { font-size: 16px; }
  .story-meta { flex-direction: column; align-items: flex-start; gap: 2px; }
  .overall { flex-wrap: wrap; }
  .line .en { font-size: 19px; }
  .toolbar { top: 54px; }
  .seg button { padding: 7px 9px; }
}
.cta-bar:has(.inner:empty) { display: none; }
