:root {
  font-family: system-ui, sans-serif;
  color: #213b37;
  background: #f6f5ee;
  line-height: 1.5;
  font-size: 16px;
  --green: #1c594f;
  --line: #d4ded7;
}
* {
  box-sizing: border-box;
}
body {
  margin: 0;
  height: 100dvh;
  display: grid;
  grid-template-rows: minmax(0, 1fr) auto auto;
  overflow: hidden;
}
/* The reading/form viewport and persistent controls have separate rows. */
#workspace {
  min-height: 0;
  overflow: auto;
  scroll-padding-bottom: 16px;
}
header {
  background: #fffef9;
  border-bottom: 1px solid var(--line);
  padding: 14px max(18px, calc((100vw - 1120px) / 2));
  display: flex;
  align-items: center;
  gap: 24px;
  position: sticky;
  top: 0;
  z-index: 5;
}
#brand {
  font-weight: 800;
  text-decoration: none;
  color: var(--green);
  white-space: nowrap;
  font-size: 20px;
}
#brand span {
  font-weight: 450;
}
nav {
  display: flex;
  gap: 3px;
  margin-left: auto;
}
button,
.button {
  font: inherit;
  border: 1px solid #b5c9bf;
  border-radius: 9px;
  padding: 9px 14px;
  background: #fffef9;
  color: var(--green);
  cursor: pointer;
  min-height: 44px;
  text-decoration: none;
  display: inline-block;
}
button:hover,
.button:hover {
  background: #edf3ec;
}
button:disabled {
  opacity: 0.5;
  cursor: wait;
}
.primary {
  background: var(--green);
  color: white;
  border-color: var(--green);
}
.primary:hover {
  background: #123c36;
}
main {
  max-width: 1120px;
  margin: 32px auto;
  padding: 0 24px 100px;
}
h1 {
  font-size: clamp(26px, 4vw, 38px);
  line-height: 1.15;
  margin: 12px 0 16px;
}
h2 {
  font-size: 23px;
  margin: 12px 0;
}
h3 {
  margin: 6px 0 12px;
}
p {
  margin: 10px 0 18px;
}
.muted,
small {
  color: #53665e;
}
small {
  display: block;
  margin-top: 8px;
}
.row {
  display: flex;
  align-items: center;
  gap: 10px;
  flex-wrap: wrap;
}
.lessonHeader {
  gap: 8px;
  margin-bottom: 10px;
}
.lessonTitle {
  flex: 1 1 80px;
  min-width: 0;
  margin: 0;
  font-size: clamp(22px, 3vw, 32px);
  overflow-wrap: anywhere;
}
#lessonOptions {
  margin: 0;
}
#lessonOptions > summary {
  font-size: 14px;
}
#lessonOptions[open] {
  flex-basis: 100%;
}
.readerActions {
  gap: 6px;
}
.readerActions button {
  font-size: 14px;
  padding: 6px 8px;
}
.between {
  justify-content: space-between;
}
.grid {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(280px, 1fr));
  gap: 20px;
}
.panel,
.card {
  background: #fffef9;
  border: 1px solid var(--line);
  border-radius: 15px;
  padding: 22px;
  margin-bottom: 18px;
}
.card p.preview {
  max-height: 7.5em;
  overflow: auto;
  white-space: pre-line;
  color: #52695e;
}
label {
  display: block;
  font-size: 14px;
  font-weight: 600;
  margin: 12px 0 5px;
}
input,
textarea,
select {
  font: inherit;
  padding: 10px;
  border: 1px solid #afc2b8;
  border-radius: 7px;
  background: white;
  color: #213b37;
  max-width: 100%;
  width: 100%;
}
input[type="file"] {
  font-size: 14px;
}
textarea {
  resize: vertical;
}
select,
input[type="number"] {
  width: auto;
}
details {
  margin: 16px 0;
}
summary {
  cursor: pointer;
  color: var(--green);
  padding: 8px 0;
}
/* Feedback uses page or active-card flow, so it never covers an action. */
#notice {
  width: min(1072px, calc(100% - 48px));
  margin: 8px auto;
  border-radius: 10px;
  background: #e6efe3;
  color: #234437;
  border: 1px solid #acbfa9;
  padding: 12px 16px;
  display: flex;
  align-items: flex-start;
  gap: 12px;
  overflow-wrap: anywhere;
  max-height: 25dvh;
  overflow: auto;
}
#notice > span { flex: 1; min-width: 0; }
#notice > button { flex: none; }
#wordDialog #notice { width: 100%; margin: 8px 0; padding: 8px; gap: 8px; font-size: 13px; }
#notice[hidden] {
  display: none;
}
#notice.error {
  background: #fff0dc;
  color: #723b17;
  border-color: #d1a474;
}
dialog {
  border: 1px solid #b8cdc1;
  border-radius: 16px;
  box-shadow: 0 20px 70px #14383044;
  width: min(540px, calc(100% - 24px));
  padding: 24px;
  background: #fffef9;
}
dialog::backdrop {
  background: #142a2255;
}
blockquote {
  border-left: 3px solid #afc7b3;
  margin: 10px 0;
  padding: 8px 14px;
  color: #516459;
}
#readerLayout {
  display: grid;
  grid-template-columns: minmax(0, 740px) 270px;
  gap: 32px;
}
#reading {
  touch-action: pan-y pinch-zoom;
  font-family: Georgia, serif;
  font-size: 22px;
  line-height: 1.9;
}
#reading p {
  padding: 10px 14px;
  margin: 0 0 15px;
  border-left: 3px solid transparent;
  border-radius: 4px;
  scroll-margin-top: 95px;
}
#reading p.current {
  border-left-color: #287968;
  background: #eef3e7;
}
.token {
  cursor: pointer;
  border-radius: 3px;
}
.token:hover {
  outline: 1px solid #4b78a0;
}
.token.new, .wordLegend .new {
  background: #d4e8fa;
  color: #173b58;
}
.token.saved, .wordLegend .saved {
  background: #fff1a8;
  color: #463b11;
}
/* A phrase is a saved range. Its words keep their own learning colours. */
.phraseMark {
  text-decoration-line: underline;
  text-decoration-color: #ae8500;
  text-decoration-thickness: 3px;
  text-underline-offset: 0.2em;
  text-decoration-skip-ink: none;
}
.token.level2 { background: #fff4bd; }
.token.level3 { background: #fff7d4; }
.token.level4 { background: #fffbe9; }
.token.known, .token.ignored { background: transparent; color: inherit; }
.wordStates { display: grid; grid-template-columns: repeat(3, minmax(0, 1fr)); gap: 4px; margin-top: 8px; }
.wordStates button { flex: 1; min-width: 0; padding: 6px 3px; }
.wordStates button[aria-pressed="true"] { background: #fff1a8; border: 2px solid #74651d; font-weight: 700; }
.wordStates button[data-status="known"][aria-pressed="true"] { background: #e4f1e7; border-color: #287047; }
.wordStates button[data-status="ignored"][aria-pressed="true"] { background: #ecefed; border-color: #58645c; }
#wordStateLabel { margin: 4px 0; }
.token.active {
  outline: 2px solid #376d9c;
  outline-offset: 2px;
}
.wordLegend {
  font-size: 13px;
}
.wordLegend span {
  display: inline-block;
  padding: 3px 6px;
  border-radius: 3px;
}
#wordDialog {
  position: fixed;
  inset: auto;
  margin: 0;
  width: min(420px, calc(100vw - 24px));
  max-height: min(620px, calc(100dvh - var(--daily-player-height, 0px) - 24px));
  overflow: auto;
  z-index: 7;
  padding: 16px;
  border-radius: 10px;
  background: white;
}
#wordDialog h2 {
  margin: 0;
  flex: 1;
}
#wordDialog blockquote {
  max-height: 4.5em;
  overflow: auto;
  font-size: 14px;
}
.sourceContext, #context, .savedContextMeaning { white-space: pre-wrap; }
.savedContextHistory { margin: 8px 0; }
.savedContextHistory > summary, .originalContext > summary { cursor: pointer; }
.savedContextRecord + .savedContextRecord { border-top: 1px solid var(--line); }
.savedContextRecord .sourceContext { max-height: 7em; overflow: auto; }
.savedContextMeaning { margin: 8px 0 4px; }
#wordDialog #meaning {
  font-size: 15px;
  max-height: 6em;
  overflow: auto;
  white-space: pre-wrap;
}
#wordDialog label {
  margin-top: 8px;
}
#wordDialog small {
  font-size: 12px;
}
#wordDialog #closeWord {
  border: none;
  padding: 4px 12px;
}

.paragraphTools {
  font: 13px system-ui;
  display: flex;
  gap: 8px;
  margin-top: 6px;
}
.paragraphTools button {
  font-size: 13px;
  padding: 3px 8px;
  min-height: 34px;
}
aside {
  align-self: start;
  position: sticky;
  top: 100px;
}
audio {
  width: 100%;
  margin: 10px 0;
}
.audioDock {
  background: #fffef9;
  border: 1px solid var(--line);
  border-radius: 12px;
  padding: 14px;
  margin: 18px 0;
}
.badge {
  font-size: 13px;
  padding: 3px 8px;
  background: #e4eddd;
  border-radius: 20px;
}
.danger {
  color: #a13c24;
}
.reviewTerm {
  font-family: Georgia, serif;
  font-size: 38px;
}
.wrap {
  overflow-wrap: anywhere;
}
.answer {
  font-size: 22px;
  color: var(--green);
}
a {
  color: var(--green);
}
:focus-visible {
  outline: 3px solid #c79f3b;
  outline-offset: 3px;
}
#positionSelect {
  max-width: 100%;
}
.count {
  font-size: 14px;
  color: #61766d;
}
.stack > * {
  margin-bottom: 12px;
}
#reading::selection,
.token::selection {
  background: #bcd8ee;
}
@media (max-width: 750px) {
  header {
    padding: 10px 12px;
    gap: 4px;
    align-items: flex-start;
    flex-wrap: wrap;
  }
  #brand {
    font-size: 18px;
  }
  nav {
    gap: 3px;
  }
  nav button {
    padding: 6px 10px;
    font-size: 14px;
    min-height: 40px;
  }
  main {
    margin-top: 22px;
    padding: 0 14px 80px;
  }
  #readerLayout {
    display: block;
  }
  aside {
    position: static;
  }
  .panel,
  .card {
    padding: 17px;
  }
  #reading {
    font-size: 20px;
  }
  #reading p {
    padding-left: 9px;
  }
  dialog {
    padding: 20px;
  }
  .grid {
    grid-template-columns: 1fr;
  }
  .audioDock {
    position: sticky;
    top: 65px;
    z-index: 3;
  }
  h1 {
    font-size: 29px;
  }
  .row > button {
    flex-shrink: 0;
  }
}

[hidden] {
  display: none !important;
}
@media (max-width: 750px) {
  .audioDock {
    position: static;
  }
  .paragraphTools {
    gap: 5px;
  }
}

.wordList {
  margin: 10px 0 0;
  padding-left: 20px;
  overflow-wrap: anywhere;
}
.wordList li {
  margin: 0 0 12px;
}
.wordList li div {
  white-space: pre-wrap;
}

#wordDialog details { margin: 8px 0 0; }
#wordDialog summary { font-size: 13px; }
#wordDialog #lookupStatus { margin: 4px 0 8px; }
#wordDialog #saveWord { margin-top: 8px; }
#wordDialog #onlineTranslate { margin-top: 10px; }

.readingHint { font-size: 14px; line-height: 1.4; margin: 8px 0; color: #516459; }
.optionalAudio { margin: 12px 0; }

/* Vocabulary keeps search and state selection stable while results change. */
.vocabularyFilters { display: flex; flex-wrap: wrap; gap: 10px; align-items: end; }
.vocabularyFilters label:first-child { flex: 1 1 240px; }
.vocabularyFilters label { margin: 0; }
.vocabularyFilters select { display: block; min-height: 44px; }
.vocabularyWord h2 { margin: 0; overflow-wrap: anywhere; }
.vocabularyStatus { margin-left: auto; }
.vocabularyVersion p, .vocabularyVersion blockquote { white-space: pre-wrap; overflow-wrap: anywhere; }
.vocabularyVersion blockquote { margin: 8px 0; padding-left: 12px; border-left: 3px solid #b9c9c2; }
.vocabularyWord small { overflow-wrap: anywhere; }
.vocabularyWord .row small { flex: 1 1 180px; }
#vocabularyWords { scroll-margin-top: 110px; }
.vocabularyPages[hidden] { display: none; }
@media(max-width:750px) {
  header nav { flex-wrap: wrap; }
  #vocabularyNav { min-height: 44px; }
  .vocabularyFilters { gap: 6px 10px; }
  .vocabularyFilters label:first-child { flex-basis: 100%; }
}

/* One bottom player survives view changes; options take space only on demand. */
#lessonPlayer {
  position: relative;
  margin: 0 auto;
  width: min(100%, 1100px);
  box-sizing: border-box;
  padding: 4px 12px max(4px, env(safe-area-inset-bottom));
  background: #fffef9;
  border: 1px solid var(--line);
  border-radius: 12px 12px 0 0;
  box-shadow: 0 -2px 10px #0001;
  z-index: 5;
}
#lessonPlayer audio { display: block; height: 36px; margin: 2px 0; }
.playerHeading { gap: 8px; flex-wrap: nowrap; }
#audioLesson { flex: 1; text-align: left; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; min-width: 0; }
#audioOptions { flex: none; margin: 0; }
#audioOptions > summary { cursor: pointer; padding: 8px 0; }
.audioOptionsBody { position: absolute; bottom: 100%; left: 0; right: 0; padding: 14px; background: #fffef9; border: 1px solid var(--line); border-radius: 12px 12px 0 0; max-height: 50vh; overflow: auto; }
#stopListening { flex: none; }

/* The library uses the original's started-lesson shelf; full previews stay readable. */
.lessonShelf {
  display: flex;
  gap: 20px;
  overflow-x: auto;
  scroll-snap-type: x proximity;
  padding-bottom: 8px;
}
.lessonShelf > .card {
  flex: 0 0 min(340px, 86%);
  min-width: 0;
  scroll-snap-align: start;
}
#lessonSearch { width: 100%; }

.libraryHeading { margin: 16px 0; }
.libraryHeading h1 { margin: 0; }
.courseFilterRow { margin: 8px 0; }
.courseFilterRow label { margin: 0; }
.courseFilterRow select { flex: 1; min-width: 100px; }
.libraryCourses .panel { margin-top: 12px; }
.libraryCourses form .row { margin-top: 12px; }
.libraryCourses h2, .lessonCourseLabel { overflow-wrap: anywhere; }
.libraryCourses select { max-width: 100%; }
.librarySearchLabel { position: absolute; width: 1px; height: 1px; padding: 0; margin: -1px; overflow: hidden; clip-path: inset(50%); white-space: nowrap; }

/* One local listening list, with explicit keyboard and touch reorder controls. */
.playlistLessons { padding-left: 1.8rem; }
.playlistLesson { border-bottom: 1px solid var(--line); padding: 14px 0; }
.playlistLesson .count { margin: 3px 0 8px; }
.playlistLesson .row { gap: 6px; }
.playlistLesson .up, .playlistLesson .down { padding-inline: 12px; }
@media (max-width: 600px) {
  .libraryHeading > .row { gap: 6px; }
  #playerPlaylist, #playlistNext { padding: 6px 10px; }
  .playlistLesson .row { flex-wrap: wrap; }
}


/* Progress stays one optional row; collection counts never replace saved contexts. */
#learningOverview { margin: 0 0 18px; }
#learningOverview > summary { cursor: pointer; padding: 8px 0; min-height: 44px; }
.learningCollections { display: grid; grid-template-columns: repeat(auto-fit, minmax(min(100%, 280px), 1fr)); gap: 16px; }
.learningCollection { border: 1px solid var(--line); border-radius: 9px; padding: 12px 16px; margin: 8px 0; }
.learningCollection h3 { margin: 0 0 8px; overflow-wrap: anywhere; }
.learningCounts { margin: 0; }
.learningCounts > div { display: flex; justify-content: space-between; align-items: baseline; gap: 12px; }
.learningCounts dt { overflow-wrap: anywhere; }
.learningCounts dd { margin: 0; font-weight: 700; }
.learningCollection .count { margin: 8px 0 0; }
#localLearning { margin: 16px 0; }
#localLearning > summary { cursor: pointer; padding: 8px 0; }

/* Review keeps the term, sentence and frequent controls together on a phone. */
.reviewHeading { margin: 0 0 10px; }
.reviewHeading h1 { margin: 0; font-size: clamp(1.25rem, 4vw, 2rem); }
.reviewPanel { margin: 0; padding: 14px 16px; }
.reviewPanel .reviewTerm { margin: 4px 0; font-size: 1.8rem; line-height: 1.1; }
.reviewPanel .count { font-size: 13px; line-height: 1.3; }
.reviewPanel blockquote { margin: 8px 0; padding: 4px 12px; }
.reviewPanel details { margin: 8px 0; }
.reviewPanel .answer { margin: 6px 0; font-size: 20px; line-height: 1.3; }
.reviewTiming { margin-top: 12px; }

.vocabularyChoice { display: flex; align-items: center; gap: 10px; margin: 0; min-height: 44px; cursor: pointer; }
.vocabularyChoice input { width: 22px; height: 22px; margin: 0; flex: 0 0 22px; }
.vocabularyReviewActions button[hidden] { display: none; }
.vocabularyReviewActions { margin-top: 8px; }

/* Study uses the same controls and nodes at each height. Short screens give
   the passage/card priority; navigation remains above in normal page flow. */
#reading p, #wordDialog input, #wordDialog textarea, #wordDialog select, .reviewPanel button {
  scroll-margin-bottom: calc(var(--daily-player-height, 0px) + 16px);
}
.reviewScope { margin: 0 0 8px; }
@media (max-height: 600px) {
  header { position: static; }
  #wordDialog {
    max-height: calc(100dvh - var(--daily-player-height, 0px) - 24px);
    padding: 10px 12px;
  }
  #wordDialog h2 { font-size: 21px; }
  #wordDialog #wordStateLabel { margin: 0; line-height: 1.3; }
  #wordDialog blockquote { max-height: 3em; line-height: 1.5; padding: 0 8px; margin: 4px 0; }
  #wordDialog label { margin: 4px 0 0; }
  #wordDialog #definition { padding: 6px 8px; line-height: 1.3; }
  #wordDialog #saveWord { margin-top: 4px; }
  #wordDialog #lookupStatus { margin: 4px 0; }
  .reviewHeading { scroll-margin-top: 8px; margin-bottom: 6px; }
  .reviewHeading h1 { font-size: 20px; }
  .reviewPanel { padding: 10px 12px; }
  .reviewPanel blockquote { max-height: 3em; overflow: auto; line-height: 1.5; }
}
