:root {
  --navy-900: #0e1740;
  --navy-800: #14245f;
  --navy-700: #1c3e95;
  --cream: #fef6e4;
  --cream-dim: #b9bfd9;
  --red: #e63946;
  --sage: #6b8f71;
  --font-ui: system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
  --font-text: ui-serif, Georgia, "Times New Roman", serif;
  --text-size: 1.35rem;
}
body[data-size="s"]  { --text-size: 1.1rem; }
body[data-size="m"]  { --text-size: 1.35rem; }
body[data-size="l"]  { --text-size: 1.65rem; }
body[data-size="xl"] { --text-size: 2rem; }

* { box-sizing: border-box; }
[hidden] { display: none !important; }

html, body { height: 100%; margin: 0; }
body {
  background: var(--navy-900);
  color: var(--cream);
  font-family: var(--font-ui);
  -webkit-tap-highlight-color: transparent;
  overscroll-behavior: none;
}

button { font: inherit; color: inherit; cursor: pointer; }
button:focus-visible, select:focus-visible, input:focus-visible { outline: 3px solid var(--cream); outline-offset: 2px; }

.screen { display: flex; flex-direction: column; height: 100dvh; max-width: 640px; margin: 0 auto; }

/* ---------- Library ---------- */
#library { overflow-y: auto; }
.hero { flex: none; background: #fef3df; color: var(--navy-800); }
.hero-text { padding: calc(14px + env(safe-area-inset-top)) 20px 0; }
.hero-text h1 { margin: 0; font-family: var(--font-text); font-size: clamp(1.7rem, 8vw, 2.4rem); line-height: 1.05; }
.hero-text p { margin: .3rem 0 0; font-size: .95rem; opacity: .85; }
.hero-img { display: block; width: 100%; height: auto; }

.story-list { padding: 16px 16px calc(24px + env(safe-area-inset-bottom)); display: grid; gap: 12px; }
.story-card {
  display: block; width: 100%; text-align: left; padding: 16px 18px;
  background: var(--navy-800); border: 1px solid #2b3f86; border-radius: 16px;
}
.story-card:active { background: var(--navy-700); }
.story-card .fr { font-family: var(--font-text); font-size: 1.35rem; }
.story-card .en { color: var(--cream-dim); margin-top: 2px; }
.story-card .meta { display: flex; gap: 10px; align-items: center; margin-top: 10px; font-size: .85rem; color: var(--cream-dim); }
.badge { padding: 2px 10px; border-radius: 999px; background: var(--navy-900); border: 1px solid #2b3f86; }
.badge.resume { background: var(--red); border-color: var(--red); color: #fff; }
.badge.offline { color: var(--cream-dim); }
#app-error { position: fixed; left: 8px; right: 8px; bottom: calc(8px + env(safe-area-inset-bottom)); z-index: 50;
  padding: 12px 14px; border-radius: 12px; background: #fde7e5; color: #5c1410; font-size: .9rem; line-height: 1.35;
  box-shadow: 0 4px 18px rgba(0, 0, 0, .35); }
.backup-nudge { width: 100%; min-height: 48px; padding: 10px 16px; border-radius: 14px; border: 1.5px dashed var(--cream-dim);
  background: transparent; color: var(--cream); font: inherit; text-align: center; }
dialog .info { margin: 10px 0 6px; font-size: .9rem; color: var(--cream-dim); }
dialog button.secondary { min-height: 44px; padding: 8px 14px; border-radius: 12px; border: 1.5px solid var(--cream-dim);
  background: transparent; color: var(--cream); font: inherit; }
dialog .row-buttons { display: flex; flex-wrap: wrap; gap: 8px; }
.empty { color: var(--cream-dim); text-align: center; padding: 24px; }
.shelf { margin: 0; font-size: .85rem; font-weight: 600; letter-spacing: .06em; text-transform: uppercase; color: var(--cream-dim); }
.shelf-toggle {
  display: flex; align-items: center; gap: 10px; width: 100%; min-height: 48px; padding: 6px 4px;
  background: none; border: 0; border-bottom: 1px solid #2b3f86; color: inherit; font: inherit; letter-spacing: inherit;
  text-transform: inherit; text-align: left;
}
.shelf-toggle:active { background: rgba(254, 246, 228, .08); }
.shelf-title { flex: 1; }
.shelf-count { padding: 1px 9px; border-radius: 999px; background: var(--navy-900); border: 1px solid #2b3f86; font-size: .75rem; }
.chev { width: 8px; height: 8px; margin: 0 6px 3px 2px; border-right: 2px solid currentColor; border-bottom: 2px solid currentColor; transform: rotate(45deg); transition: transform .15s; }
.shelf-toggle.folded .chev { transform: rotate(-45deg); margin-bottom: 0; }
.shelf-cards { display: grid; gap: 12px; padding-top: 12px; }
.shelf-cards[hidden] { display: none; }
/* Books: one card on the shelf, opening to its chapters (DESIGN.md §6.7) */
.book-card {
  display: flex; gap: 12px; align-items: flex-start; width: 100%; text-align: left; padding: 16px 18px;
  background: var(--navy-800); border: 1px solid #2b3f86; border-radius: 16px; color: inherit; font: inherit;
}
.book-card:active { background: var(--navy-700); }
.book-card .chev { margin-top: 10px; transform: rotate(-45deg); }
.book-card.open .chev { transform: rotate(45deg); }
.book-card .book-text { flex: 1; }
.book-card .fr { font-family: var(--font-text); font-size: 1.35rem; }
.book-card .en { color: var(--cream-dim); margin-top: 2px; }
.book-card .meta { display: flex; gap: 10px; align-items: center; margin-top: 10px; font-size: .85rem; color: var(--cream-dim); }
.book-chapters { display: grid; gap: 10px; margin: -4px 0 0 10px; padding-left: 12px; border-left: 2px solid #2b3f86; }
.book-chapters[hidden] { display: none; }
.book-chapters .story-card { padding: 12px 16px; }
.book-chapters .story-card .fr { font-size: 1.15rem; }

/* ---------- Reader ---------- */
.topbar {
  display: flex; align-items: center; gap: 8px; flex: none;
  padding: calc(8px + env(safe-area-inset-top)) 8px 4px;
}
.topbar h1 { flex: 1; margin: 0; text-align: center; font-family: var(--font-text); font-size: 1.1rem; font-weight: 600; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.icon-btn { width: 48px; height: 48px; background: none; border: 0; border-radius: 50%; display: grid; place-items: center; }
.icon-btn:active, .round:active, .speedrow button:active { background: rgba(254, 246, 228, .14); }
svg { width: 26px; height: 26px; fill: none; stroke: currentColor; stroke-width: 2; stroke-linecap: round; stroke-linejoin: round; }

.steps { display: flex; align-items: center; justify-content: center; gap: 10px; flex: none; font-size: .9rem; color: var(--cream-dim); }
#page-count { margin-right: 6px; }
.step { width: 28px; height: 28px; display: grid; place-items: center; border-radius: 50%; border: 1.5px solid #3a4f9a; font-weight: 600; font-size: .8rem; }
.step.active { background: var(--red); border-color: var(--red); color: #fff; }
.step.done { background: var(--sage); border-color: var(--sage); color: #fff; }
.hint { flex: none; margin: 6px 16px 0; text-align: center; font-size: .85rem; color: var(--cream-dim); min-height: 1.2em; }

.text { flex: 1; overflow-y: auto; padding: 16px 20px; scroll-behavior: smooth; }
/* a betweener's scene clip above the text (DESIGN.md §7.8) */
/* With clips (portrait, 9:16) the clip fills the reader and the text sits over its lower part on a dark fade;
   the bars float over it. Stories without clips keep the plain layout. */
#reader.video-mode { position: relative; background: var(--navy-900); }
#reader.video-mode .scene { position: absolute; inset: 0; width: 100%; height: 100%; max-height: none; margin: 0;
  border-radius: 0; aspect-ratio: auto; object-fit: cover; z-index: 0; }
#reader.video-mode > :not(.scene) { position: relative; z-index: 1; }
#reader.video-mode .overlay { position: absolute; z-index: 5; }
#reader.video-mode .topbar { background: linear-gradient(rgba(14, 23, 64, .7), rgba(14, 23, 64, 0)); }
#reader.video-mode .steps, #reader.video-mode .hint { text-shadow: 0 1px 3px rgba(0, 0, 0, .85); color: var(--cream); }
/* about four one-line segments (one line: text-size x 1.55 + 1.5rem of padding and gap), following the text size */
#reader.video-mode .text { flex: none; height: min(40vh, calc(4 * (var(--text-size) * 1.55 + 1.5rem) + 18px));
  margin-top: auto; padding-top: 18px; background: linear-gradient(rgba(14, 23, 64, 0), rgba(14, 23, 64, .9) 14%); }
#reader.video-mode #next { visibility: hidden; }   /* back restarts the scene; no forward button (play stays centred) */
#reader.video-mode .seg { text-shadow: 0 1px 2px rgba(0, 0, 0, .6); }
#reader.video-mode .controls { background: rgba(14, 23, 64, .92); }
.scene { flex: none; display: block; width: calc(100% - 32px); margin: 8px 16px 0; aspect-ratio: 16 / 9; max-height: 32vh; object-fit: contain; border-radius: 14px; background: var(--navy-800); }   /* a portrait clip fits, never cropped */
.seg {
  margin: 0 0 .5rem; padding: .5rem .7rem; border-radius: 12px;
  font-family: var(--font-text); font-size: var(--text-size); line-height: 1.55;
  transition: opacity .45s ease, background-color .2s ease;
}
.seg.pending { opacity: .2; }
.seg.current { background: rgba(254, 246, 228, .1); }
.tok { border-radius: 5px; padding: 0 1px; transition: background-color .08s linear; }
.tok.hl { background: var(--red); color: #fff; }
/* Core words still being learned (§7.5): a quiet underline that disappears once the word is known. New words: bold. */
.tok.core { text-decoration: underline; text-decoration-thickness: 1.5px; text-underline-offset: 4px;
  text-decoration-color: rgba(254, 246, 228, .45); }
.tok.newword { font-weight: 700; }

.overlay {
  position: absolute; inset: 0; z-index: 5; display: grid; place-content: center; gap: 18px; text-align: center;
  background: rgba(14, 23, 64, .94); padding: 24px; font-family: var(--font-text); font-size: 1.6rem;
}
.overlay p { margin: 0; }
#overlay-actions { display: flex; gap: 12px; justify-content: center; flex-wrap: wrap; font-family: var(--font-ui); font-size: 1rem; }
#overlay-actions button, .primary { padding: 12px 22px; border-radius: 999px; border: 0; background: var(--red); color: #fff; font-weight: 600; }
#overlay-actions button.secondary { background: transparent; border: 1.5px solid var(--cream-dim); }
#reader { position: relative; }

.controls { flex: none; padding: 8px 16px calc(14px + env(safe-area-inset-bottom)); background: linear-gradient(transparent, var(--navy-900) 30%); }
.transport { display: flex; align-items: center; justify-content: center; gap: 28px; }
.round { width: 64px; height: 64px; border-radius: 50%; border: 1.5px solid #3a4f9a; background: var(--navy-800); display: grid; place-items: center; }
.round.big { width: 84px; height: 84px; background: var(--red); border-color: var(--red); color: #fff; }
.round.big svg { width: 36px; height: 36px; fill: currentColor; }
.round.big svg path[d^="M8 5v14"] { fill: none; stroke-width: 3.5; }
.round:not(.big) svg { fill: currentColor; }
.round:not(.big) svg path:first-child { stroke-width: 2.5; }
.speedrow { display: flex; align-items: center; justify-content: center; gap: 6px; margin-top: 8px; color: var(--cream-dim); }
.speedrow button { min-width: 40px; height: 44px; border-radius: 22px; background: none; border: 0; font-size: 1.3rem; }
.speedrow .pagebtn { display: flex; align-items: center; gap: 2px; padding: 0 10px; font-size: .85rem; border: 1px solid #2b3f86; }
.speedrow .pagebtn svg { width: 20px; height: 20px; }
.speedrow button:disabled { opacity: .3; cursor: default; }
#speed-label { min-width: 4ch; text-align: center; }

/* ---------- Word Check ---------- */
#wordcheck { position: relative; }
.wc-body { flex: 1; overflow-y: auto; padding: 8px 16px 16px; }
.wc-footer { flex: none; display: flex; justify-content: space-between; align-items: center; gap: 12px; padding: 8px 16px calc(16px + env(safe-area-inset-bottom)); }
.ghost-btn { padding: 12px 22px; border-radius: 999px; background: none; border: 1.5px solid #3a4f9a; color: var(--cream-dim); }
.wc-prompt { margin: 6px 0 12px; text-align: center; font-family: var(--font-text); font-size: 1.25rem; }
.wc-prompt.small { font-size: 1rem; font-family: var(--font-ui); color: var(--cream-dim); margin-top: 18px; }

.pairs { display: grid; grid-template-columns: 1fr 1fr; gap: 10px 12px; align-items: stretch; }
.pairs .col { display: grid; gap: 10px; align-content: start; }
.tile {
  min-height: 60px; padding: 10px 12px; border-radius: 14px; text-align: center; line-height: 1.25;
  background: var(--navy-800); border: 1.5px solid #3a4f9a; font-family: var(--font-text); font-size: 1.1rem;
}
.tile.en { font-family: var(--font-ui); font-size: 1rem; }
.tile.sel { border-color: var(--cream); background: var(--navy-700); }
.tile.ok { background: var(--sage); border-color: var(--sage); color: #fff; pointer-events: none; }
.tile.bad { background: #7d2330; border-color: var(--red); animation: shake .35s; }
.tile.dim { opacity: .45; pointer-events: none; }
@keyframes shake { 20% { transform: translateX(-6px); } 50% { transform: translateX(6px); } 80% { transform: translateX(-3px); } }

.speaker { display: grid; place-items: center; width: 96px; height: 96px; margin: 8px auto 4px; border-radius: 50%; background: var(--red); border: 0; color: #fff; }
.speaker svg { width: 44px; height: 44px; fill: currentColor; stroke: currentColor; }
.options { display: grid; grid-template-columns: 1fr 1fr; gap: 10px; }
.options.locked .tile { pointer-events: none; }

.summary { text-align: center; }
.summary .score { font-family: var(--font-text); font-size: 1.6rem; margin: 10px 0 14px; }
.summary ul { list-style: none; margin: 0 auto; padding: 0; max-width: 380px; text-align: left; }
.summary li { display: flex; gap: 10px; padding: 8px 4px; border-bottom: 1px solid #25357a; }
.summary li .mark { width: 1.4em; }
.summary li.miss .mark { color: var(--red); }
.summary li.hit .mark { color: var(--sage); }
.summary li .fr { font-family: var(--font-text); flex: 1; }
.summary li .en { color: var(--cream-dim); }

.newfrench { margin: 22px auto 0; padding: 14px 16px; max-width: 380px; text-align: left; background: var(--navy-800); border: 1.5px solid var(--sage); border-radius: 14px; }
.newfrench .nf-title { margin: 0 0 8px; font-size: .95rem; color: var(--cream-dim); }
.overlay .newfrench { font-family: var(--font-ui); font-size: 1rem; }
.newfrench ul { margin: 0; padding: 0; list-style: none; }
.newfrench li { border: 0; padding: 3px 0; }
.newfrench b { font-family: var(--font-text); font-weight: 600; color: var(--cream); }

/* ---------- Settings ---------- */
dialog h3 { margin: 18px 0 0; font-size: 1rem; color: var(--cream-dim); font-weight: 600; }
dialog { width: min(92vw, 440px); border: 0; border-radius: 20px; background: var(--navy-800); color: var(--cream); padding: 20px 22px; }
dialog::backdrop { background: rgba(0, 0, 0, .6); }
dialog h2 { margin: 0 0 12px; font-family: var(--font-text); }
dialog label { display: block; margin: 14px 0; font-size: .95rem; }
dialog output { float: right; color: var(--cream-dim); }
dialog select, dialog input[type="range"] { display: block; width: 100%; margin-top: 6px; }
dialog select { padding: 10px; border-radius: 10px; border: 1px solid #3a4f9a; background: var(--navy-900); color: var(--cream); font: inherit; }
dialog label.check { display: flex; gap: 10px; align-items: center; }
dialog input[type="range"] { accent-color: var(--red); }
dialog input[type="checkbox"] { width: 22px; height: 22px; accent-color: var(--red); }
.dialog-actions { text-align: right; margin-top: 18px; }
.build { margin: 14px 0 0; font-size: .8rem; color: var(--cream-dim); }

/* ---------- Progress on the shelves (DESIGN.md §6.8) ---------- */
/* Tiles fill from the left as pages are read: the read part darker, the unread part lighter. */
:root { --tile-unread: #1d3176; --tile-read: #17404a; --gold: #e9c46a; }   /* read: a dark teal, apart from the page behind */
.story-card, .book-card { display: flex; gap: 14px; align-items: center; background: var(--tile-unread); }
.story-card .card-text, .book-card .book-text { flex: 1; min-width: 0; }
.story-card.read, .book-card.read {
  background: linear-gradient(90deg, var(--tile-read) 0, var(--tile-read) calc(var(--p) * 100% - 4%), var(--tile-unread) calc(var(--p) * 100% + 4%));
}
.story-card.done, .book-card.done { background: var(--tile-read); border-color: #2c5a5f; }
.story-card.perfect, .book-card.perfect { border: 1.5px solid var(--gold); box-shadow: 0 0 0 1px rgba(233, 196, 106, .25) inset; }
.badge.score { color: var(--cream); }
.badge.finished { background: var(--sage); border-color: var(--sage); color: #fff; }
.badge.perfect { background: var(--gold); border-color: var(--gold); color: var(--navy-900); font-weight: 600; }
.badge.perfect::before { content: '★ '; }
.badge.finished::before { content: '✓ '; }
/* a book on the tile: its thickness says how long it is */
.spine { flex: none; display: flex; align-items: center; height: 44px; }
.spine svg { width: auto; height: 44px; stroke: none; }
.spine .cover { fill: var(--book); }
.spine .band { fill: rgba(0, 0, 0, .28); }
.spine .label { fill: rgba(254, 246, 228, .6); }
.book-card .chev { align-self: center; margin-top: 0; }
/* the shelf heading: a thin bar shows how much of the level is read */
.shelf { position: relative; }
.shelf::after { content: ''; position: absolute; left: 0; bottom: 0; height: 3px; width: calc(var(--p, 0) * 100%); background: var(--sage); border-radius: 2px; }
.shelf.perfect::after { background: var(--gold); }
.shelf-meta { display: flex; align-items: center; gap: 8px; }
.shelf-score { font-size: .75rem; letter-spacing: 0; }
.shelf-mark { font-size: 1rem; letter-spacing: 0; }
.shelf-mark.finished { color: var(--sage); }
.shelf-mark.perfect { color: var(--gold); }
/* a level finished, with the next one finished too, steps back */
.shelf.past { opacity: .55; }
.shelf.past .shelf-toggle { min-height: 38px; font-size: .78rem; }
.shelf-cards.past { opacity: .7; filter: saturate(.5); }

/* the private shelf (Level 6), before the site's password is given */
.shelf.private .shelf-toggle { cursor: default; }
.shelf .lock { font-size: 1rem; letter-spacing: 0; }
.story-card.private-locked { display: block; border-style: dashed; background: transparent; }

/* ---------- Library menu and My words ---------- */
.hero { position: relative; }
.hero-text { padding-right: 60px; }
.hero-tools { position: absolute; top: calc(6px + env(safe-area-inset-top)); right: 6px; z-index: 5; }
.hero-tools .icon-btn { color: var(--navy-800); }
.hero-tools .icon-btn svg circle { fill: currentColor; }
.menu { position: absolute; right: 4px; top: 50px; min-width: 190px; padding: 6px; border-radius: 14px;
  background: var(--navy-800); border: 1px solid #2b3f86; box-shadow: 0 8px 24px rgba(0, 0, 0, .35); }
.menu button { display: block; width: 100%; min-height: 46px; padding: 8px 14px; text-align: left; background: none; border: 0; border-radius: 10px; color: var(--cream); }
.menu button:active, .menu button:hover { background: rgba(254, 246, 228, .1); }
.hero-words { margin: 10px 0 2px; padding: 5px 12px; border-radius: 999px; border: 0; background: var(--navy-800); color: var(--cream); font-size: .85rem; }
.mw-summary { margin: 0 0 8px; font-size: 1.05rem; }
.mw-bar { height: 8px; border-radius: 999px; background: var(--navy-900); overflow: hidden; }
#mw-fill { display: block; height: 100%; width: 0; background: var(--sage); }
.mw-list { list-style: none; margin: 6px 0 0; padding: 0; max-height: 38vh; overflow-y: auto; }
.mw-word { display: flex; align-items: baseline; gap: 12px; width: 100%; min-height: 44px; padding: 8px 4px; text-align: left;
  background: none; border: 0; border-bottom: 1px solid #2b3f86; color: var(--cream); }
.mw-word .fr { flex: 1; font-family: var(--font-text); font-size: 1.1rem; }
.mw-word .en { color: var(--cream-dim); font-size: .9rem; text-align: right; }
.mw-empty { padding: 8px 4px; color: var(--cream-dim); font-size: .9rem; }

@media (prefers-reduced-motion: reduce) {
  .seg, .tok { transition: none; }
  .text { scroll-behavior: auto; }
}
/* ---------- Daily review and the offers (M7, DESIGN.md §7.6) ---------- */
.offer-card { padding: 14px 16px; border-radius: 16px; background: var(--navy-800); border: 1.5px solid var(--gold); }
.offer-msg { margin: 0 0 12px; font-family: var(--font-text); font-size: 1.15rem; line-height: 1.35; }
.offer-actions { display: flex; gap: 10px; justify-content: flex-end; flex-wrap: wrap; }
.offer-actions button { min-height: 44px; padding: 10px 18px; }
.review-card { display: flex; align-items: center; justify-content: space-between; gap: 12px; width: 100%; min-height: 52px;
  padding: 10px 16px; border-radius: 14px; border: 1.5px solid #3a4f9a; background: var(--navy-800); color: var(--cream); text-align: left; }
.review-card .review-go { flex: none; padding: 6px 14px; border-radius: 999px; background: var(--red); color: #fff; font-weight: 600; }
.rv-head { display: flex; align-items: center; justify-content: center; gap: 14px; margin: 8px 0 6px; }
.speaker.small { width: 56px; height: 56px; margin: 0; flex: none; }
.speaker.small svg { width: 28px; height: 28px; }
.rv-big { margin: 0; font-family: var(--font-text); font-size: 1.9rem; overflow-wrap: anywhere; }
.rv-line { display: flex; align-items: flex-start; gap: 10px; max-width: 520px; margin: 10px auto 0; }
.rv-sentence { flex: 1; margin: 0; font-family: var(--font-text); font-size: 1.2rem; line-height: 1.45; }
.rv-word { color: var(--gold); }
.rv-blank { display: inline-block; min-width: 4.5em; border-bottom: 2px solid var(--cream-dim); }
.rv-play { flex: none; width: 44px; height: 44px; border-radius: 50%; border: 1.5px solid #3a4f9a; background: none; color: var(--cream); display: grid; place-items: center; }
.rv-play svg { width: 22px; height: 22px; fill: currentColor; stroke: currentColor; }
.rv-en { max-width: 520px; margin: 8px auto 12px; color: var(--cream-dim); font-size: .95rem; line-height: 1.4; }
.rv-answer { text-align: center; }
.rv-meaning { margin: 4px 0; font-size: 1.3rem; }
.rv-meaning.small { font-size: 1.05rem; text-align: center; margin-top: 14px; }
.rv-grade { display: flex; justify-content: center; gap: 12px; margin-top: 14px; }
