.mastery-dashboard { width: min(100% - 32px, 980px); margin: 12px auto 48px; }
.mastery-top-bar { position: relative; z-index: 10000010; display: grid; grid-template-columns: minmax(210px, .72fr) minmax(720px, 1.9fr) auto; align-items: center; gap: 14px; height: auto; min-height: 78px; padding-block: 10px; overflow: visible; }
.mastery-top-bar .top-bar-left { position: relative; z-index: 1000; min-width: 0; }
.mastery-top-bar .top-bar-right { position: relative; z-index: 1000; justify-self: end; }
.mastery-header-controls { position: relative; z-index: 1000; min-width: 0; display: grid; grid-template-columns: minmax(220px, max-content) minmax(390px, max-content) max-content; align-items: stretch; justify-content: center; gap: 10px; }
.mastery-header-controls:has(.mastery-subject-picker[open]) { z-index: 10000020 !important; overflow: visible !important; }
body.mastery-subject-menu-open .mastery-top-bar,
body.mastery-subject-menu-open .mastery-header-controls,
body.mastery-subject-menu-open .mastery-subject-cards {
  z-index: 10000030 !important;
  overflow: visible !important;
}

/* Acceptance: keep the three review-mode tabs centered while side controls change width. */
body.mastery-page .mastery-header-controls {
  display: grid !important;
  grid-template-columns: minmax(180px, 1fr) minmax(390px, max-content) minmax(180px, 1fr) !important;
  justify-content: stretch !important;
  align-items: stretch !important;
}

body.mastery-page .mastery-subject-cards { justify-self: end !important; }
body.mastery-page .recitation-review-tabs { justify-self: center !important; }
body.mastery-page .mastery-top-card-tools { justify-self: start !important; }

/* Keep the recitation controls centered in the viewport on desktop. */
@media (min-width: 1181px) {
  body.mastery-page .mastery-top-bar {
    grid-template-columns: minmax(220px, 1fr) auto minmax(220px, 1fr) !important;
  }

  body.mastery-page .mastery-header-controls {
    grid-column: 2 !important;
    justify-self: center !important;
    width: min(930px, calc(100vw - 520px)) !important;
  }

  body.mastery-page .mastery-top-bar .top-bar-left {
    grid-column: 1 !important;
    justify-self: start !important;
  }

  body.mastery-page .mastery-top-bar .top-bar-right {
    grid-column: 3 !important;
    justify-self: end !important;
  }

  body.mastery-page .mastery-review-stage {
    display: flex !important;
    justify-content: center !important;
    box-sizing: border-box !important;
  }

  body.mastery-page .mastery-card-scene,
  body.mastery-page .mastery-bottom-controls {
    margin-inline: auto !important;
  }
}

@media (max-width: 768px) {
  body.mastery-page .mastery-header-controls {
    display: flex !important;
    justify-content: flex-start !important;
  }
}
body.mastery-subject-menu-open .mastery-dashboard,
body.mastery-subject-menu-open .mastery-review,
body.mastery-subject-menu-open .mastery-review-stage,
body.mastery-subject-menu-open .mastery-card-scene {
  z-index: 1 !important;
}
.mastery-top-card-tools { display: inline-flex; align-items: center; justify-content: center; gap: 7px; min-height: 54px; padding: 6px; border-radius: 18px; background: color-mix(in srgb, var(--primary-light) 44%, rgba(255,255,255,.86) 56%); box-shadow: inset 0 1px 0 rgba(255,255,255,.7), 0 10px 24px rgba(31,54,38,.07); }
.mastery-top-card-tools .flashcard-icon-btn { display: inline-grid; place-items: center; width: 44px; min-width: 44px; height: 44px; min-height: 44px; padding: 0 !important; border-radius: 14px; }
.mastery-top-card-tools .flashcard-icon-btn svg,
.mastery-top-card-tools .flashcard-icon-btn i { width: 18px !important; min-width: 18px !important; height: 18px !important; flex: 0 0 18px !important; stroke-width: 2.1; }
.mastery-top-card-tools .flashcard-icon-btn:disabled { opacity: .42; cursor: not-allowed; transform: none; }
.mastery-hidden-top-actions { display: none !important; }
.mastery-immersive-exit-btn { position: fixed; top: max(14px, env(safe-area-inset-top)); right: 14px; z-index: 120; display: none; place-items: center; width: 44px; height: 44px; border: 1px solid color-mix(in srgb, var(--primary-color) 16%, var(--border-color)); border-radius: 999px; background: color-mix(in srgb, var(--panel-bg) 88%, var(--primary-light) 12%); color: var(--primary-color); box-shadow: 0 12px 30px rgba(31,54,38,.16); cursor: pointer; }
.mastery-immersive-exit-btn svg,
.mastery-immersive-exit-btn i { width: 20px !important; min-width: 20px !important; height: 20px !important; flex: 0 0 20px !important; stroke-width: 2.2; }
body.mastery-immersive .mastery-immersive-exit-btn { display: grid; }

@media (max-width: 1400px) {
  body.mastery-page .mastery-top-bar {
    display: grid !important;
    grid-template-columns: minmax(210px, .72fr) minmax(720px, 1.9fr) auto !important;
    align-items: center !important;
    gap: 14px !important;
    height: auto !important;
    min-height: 78px !important;
    padding: 10px 24px !important;
    overflow: visible !important;
  }

  body.mastery-page .mastery-top-bar .top-bar-left {
    display: flex !important;
    grid-column: 1 !important;
    grid-row: 1 !important;
    align-items: center !important;
    gap: 16px !important;
    min-width: 0 !important;
  }

  body.mastery-page .mastery-top-bar .current-chapter-info {
    display: block !important;
    min-width: 0;
  }

  body.mastery-page .mastery-header-controls {
    grid-column: 2 !important;
    grid-row: 1 !important;
  }

  body.mastery-page .mastery-top-bar .top-bar-right {
    grid-column: 3 !important;
    grid-row: 1 !important;
  }
}
.mastery-heading { display: flex; align-items: end; justify-content: space-between; gap: 20px; margin-bottom: 18px; }
.mastery-heading h1 { margin: 0; font-size: 1.55rem; }
.mastery-eyebrow { margin: 0 0 4px; color: var(--primary-color); font-weight: 700; font-size: .82rem; }
.mastery-next-review { margin: 0; color: var(--text-muted); font-size: .9rem; }
.mastery-stat-grid { display: grid; grid-template-columns: repeat(3, minmax(0, 1fr)); gap: 12px; }
.mastery-stat { display: grid; grid-template-columns: auto 1fr; gap: 2px 10px; align-items: center; padding: 18px; border: 1px solid var(--border-color); border-radius: 8px; background: var(--panel-bg); }
.mastery-stat i { grid-row: span 2; color: var(--primary-color); width: 18px; height: 18px; }
.mastery-stat strong { font-size: 1.45rem; line-height: 1; }
.mastery-stat span { color: var(--text-muted); font-size: .82rem; }
.mastery-filter-bar { display: flex; gap: 8px; margin: 24px 0 14px; overflow-x: auto; }
.mastery-filter { flex: 0 0 auto; border: 1px solid var(--border-color); border-radius: 8px; padding: 8px 14px; background: var(--panel-bg); color: var(--text-muted); font-weight: 700; cursor: pointer; }
.mastery-filter.active { border-color: var(--primary-color); color: var(--primary-color); background: var(--primary-light); }
.mastery-groups { display: grid; gap: 12px; }
.mastery-subject-cards { display: flex; align-items: stretch; justify-content: center; min-height: 54px; margin: 0; }
.mastery-subject-picker { position: relative; width: fit-content; min-width: 230px; z-index: 1; }
.mastery-subject-picker[open] { z-index: 10000030; }
.mastery-subject-picker > summary { display: flex; align-items: center; justify-content: space-between; gap: 14px; min-height: 54px; padding: 0 16px; border: 1px solid var(--border-color); border-radius: 13px; background: var(--panel-bg); color: var(--primary-color); font-size: .96rem; font-weight: 800; cursor: pointer; list-style: none; box-shadow: inset 0 1px 0 rgba(255,255,255,.62), 0 8px 20px rgba(31,54,38,.06); }
.mastery-subject-picker > summary::-webkit-details-marker { display: none; }
.mastery-subject-picker > summary svg { width: 16px; height: 16px; transition: transform .16s ease; }
.mastery-subject-picker[open] > summary svg { transform: rotate(180deg); }
.mastery-subject-menu { position: absolute; z-index: 10000040; top: calc(100% + 6px); right: 0; display: grid; min-width: 252px; max-height: min(72vh, 520px); overflow-y: auto; padding: 8px; border: 1px solid var(--border-color); border-radius: 12px; background: var(--panel-bg); box-shadow: 0 10px 28px rgba(31, 54, 38, .14); }
body.mastery-subject-menu-open .mastery-subject-menu {
  position: fixed !important;
  left: var(--mastery-subject-menu-left, 16px) !important;
  right: auto !important;
  top: var(--mastery-subject-menu-top, 82px) !important;
  width: var(--mastery-subject-menu-width, 252px) !important;
  min-width: var(--mastery-subject-menu-width, 252px) !important;
  max-height: calc(100dvh - var(--mastery-subject-menu-top, 82px) - 12px) !important;
  z-index: 10000060 !important;
}
.mastery-subject-menu button { display: flex; align-items: center; justify-content: space-between; gap: 18px; width: 100%; padding: 9px 10px; border: 0; border-radius: 5px; background: transparent; color: var(--text-main); font: inherit; cursor: pointer; }
.mastery-subject-menu button:hover, .mastery-subject-menu button.is-active { background: var(--primary-light); color: var(--primary-color); }
.mastery-subject-menu strong { font-size: .82rem; }
.mastery-menu-section-title { margin: 6px 4px 4px; padding-top: 6px; border-top: 1px solid color-mix(in srgb, var(--border-color) 72%, transparent); color: var(--text-muted); font-size: .74rem; font-weight: 800; letter-spacing: .04em; }
.mastery-menu-section-title:first-child { margin-top: 0; padding-top: 0; border-top: 0; }
.mastery-menu-hint { padding: 9px 10px; color: var(--text-muted); font-size: .82rem; line-height: 1.35; }
.mastery-new-folder-btn { color: var(--primary-color) !important; font-weight: 800 !important; }
.mastery-subject-card { display: flex; align-items: center; justify-content: space-between; gap: 16px; padding: 18px; border: 1px solid var(--border-color); border-radius: 8px; background: var(--panel-bg); }
.mastery-subject-card.is-active { border-color: var(--primary-color); box-shadow: 0 0 0 2px var(--primary-light); }
.mastery-subject-card strong { display: block; margin-top: 7px; font-size: 1.35rem; }
.mastery-subject-card p { margin: 4px 0 0; color: var(--text-muted); font-size: .84rem; }
.mastery-review { margin-top: 4px; padding: 0; border: 0; border-radius: 0; background: transparent; }
.mastery-review-heading { display: flex; align-items: center; justify-content: flex-end; gap: 12px; width: min(100%, 760px); margin: 0 auto 4px; padding-inline: 4px; }
.mastery-review-heading h2 { margin: 6px 0 0; font-size: 1.08rem; }
.mastery-review-count { color: var(--primary-color); font-weight: 700; white-space: nowrap; }
.mastery-review-stage { width: min(100%, 900px); margin: 0 auto; padding-inline: 62px; }
.mastery-card-scene { flex: 0 1 720px; width: min(100%, 720px); min-height: 560px; height: min(72vh, 720px); max-height: 760px; }
.mastery-review-card { min-height: 0; }
.mastery-review-card .card-front .card-meta-row { justify-content: space-between; }
.mastery-review-card .card-source-badge { flex: 0 0 auto; }
.mastery-review-card .card-question-text { font-size: clamp(1.2rem, 2.2vw, 1.55rem); font-weight: 700; align-items: center; }
.mastery-review-card .card-answer-text { text-align: left; }
.mastery-review-card .tap-hint { margin-top: auto; }
/* v238: recitation cards keep actions visible while long content scrolls inside the card. */
.mastery-review-card .card-question-text,
.mastery-review-card .card-answer-text-container,
.mastery-review-card .card-answer-text {
  min-height: 0;
  max-width: 100%;
  overflow-y: auto;
  scrollbar-width: thin;
  scrollbar-color: rgba(95, 116, 94, 0.36) transparent;
  overscroll-behavior: contain;
  touch-action: pan-y;
}
.mastery-review-card .card-question-text::-webkit-scrollbar,
.mastery-review-card .card-answer-text-container::-webkit-scrollbar,
.mastery-review-card .card-answer-text::-webkit-scrollbar { width: 8px; }
.mastery-review-card .card-question-text::-webkit-scrollbar-thumb,
.mastery-review-card .card-answer-text-container::-webkit-scrollbar-thumb,
.mastery-review-card .card-answer-text::-webkit-scrollbar-thumb { border-radius: 999px; background: rgba(95, 116, 94, 0.36); }
/* v239: allow the recitation page itself to scroll when card + controls exceed the viewport. */
.mastery-dashboard,
.mastery-review,
.mastery-review-stage {
  min-height: 0;
}
.mastery-review,
.mastery-review-stage {
  overflow-y: auto;
  overscroll-behavior: contain;
  -webkit-overflow-scrolling: touch;
}
@media (min-width: 769px) and (max-width: 1180px) {
  .mastery-dashboard {
    max-height: calc(100dvh - 24px);
    overflow-y: auto;
    overscroll-behavior: contain;
    -webkit-overflow-scrolling: touch;
  }
}
body.mastery-inline-editing .mastery-review-card { cursor: text; }
body.mastery-inline-editing .mastery-review-card .tap-hint,
body.mastery-inline-editing .mastery-review-stage .floating-nav-btn { display: none !important; }
body.mastery-inline-editing .mastery-review-card .card-question-text[contenteditable="true"],
body.mastery-inline-editing .mastery-review-card .card-answer-text[contenteditable="true"] { min-height: 96px; }
.mastery-bottom-controls { margin-top: 12px; }
.mastery-bottom-controls .card-quick-controls { background: color-mix(in srgb, var(--primary-light) 48%, transparent); }
.mastery-inline-editor-actions { display: flex; align-items: center; justify-content: center; gap: 5px; width: min(100%, 214px); padding: 4px; border: 1px solid color-mix(in srgb, var(--primary-color) 14%, transparent); border-radius: 12px; background: color-mix(in srgb, var(--panel-bg) 66%, transparent); box-shadow: 0 6px 18px rgba(31,54,38,.10); backdrop-filter: blur(10px); }
.mastery-inline-editor-actions .btn { flex: 0 0 100px; width: 100px; min-width: 0; min-height: 36px; padding: 6px 10px; border-radius: 9px; font-size: .82rem; font-weight: 800; }
.mastery-unmaster-btn { color: #fff; background: #b42318; border-color: #b42318; }
.mastery-unmaster-btn:hover, .mastery-unmaster-btn:focus-visible { filter: brightness(.96); transform: translateY(-1px); }
.mastery-group { border: 1px solid var(--border-color); border-radius: 8px; background: var(--panel-bg); padding: 18px; }
.mastery-group header { display: flex; align-items: start; justify-content: space-between; gap: 16px; }
.mastery-group h2 { margin: 5px 0; font-size: 1.05rem; }
.mastery-group p { margin: 0; color: var(--text-muted); font-size: .86rem; }
.mastery-subject { display: inline-block; color: var(--primary-color); background: var(--primary-light); border-radius: 4px; padding: 3px 7px; font-size: .76rem; font-weight: 700; }
.mastery-group-actions { display: flex; gap: 8px; align-items: center; }
.icon-only-btn { display: inline-grid; place-items: center; width: 34px; height: 34px; border: 1px solid var(--border-color); border-radius: 7px; color: var(--text-muted); background: var(--panel-bg); cursor: pointer; }
.mastery-preview-list { margin: 16px 0 0; padding-left: 20px; color: var(--text-main); line-height: 1.6; }
.mastery-preview-list li { margin: 4px 0; }
.mastery-more { display: block; margin-top: 8px; color: var(--text-muted); font-size: .84rem; }
.mastery-empty-state { min-height: 250px; display: grid; place-items: center; align-content: center; gap: 10px; border: 1px dashed var(--border-color); border-radius: 8px; color: var(--text-muted); text-align: center; }
.mastery-empty-state i { width: 28px; height: 28px; color: var(--primary-color); }

.recitation-review-tabs { display: inline-flex; align-items: center; justify-content: center; gap: 8px; margin: 0; width: auto; min-width: 0; min-height: 54px; padding: 6px; border: 1px solid color-mix(in srgb, var(--primary-color) 12%, var(--border-color)); border-radius: 22px; background: color-mix(in srgb, var(--panel-bg) 84%, var(--primary-light) 16%); box-shadow: 0 10px 24px rgba(31,54,38,.06); }
.recitation-review-tab { display: inline-flex; align-items: center; justify-content: center; gap: 9px; min-width: 116px; min-height: 42px; padding: 0 16px; border: 1px solid transparent; border-radius: 17px; background: transparent; color: var(--text-main); text-align: center; cursor: pointer; box-shadow: none; }
.recitation-review-tab span { color: var(--text-muted); font-size: .9rem; font-weight: 900; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.recitation-review-tab strong { display: inline-grid; place-items: center; min-width: 27px; height: 27px; padding: 0 8px; border-radius: 999px; color: var(--primary-color); background: color-mix(in srgb, var(--primary-light) 68%, #fff 32%); font-size: 1rem; line-height: 1; }
.recitation-review-tab.is-active { border-color: color-mix(in srgb, var(--primary-color) 34%, var(--border-color)); background: #fff; box-shadow: 0 8px 18px rgba(47,107,47,.10); }
.recitation-review-tab.is-empty { opacity: .62; }
.mastery-review-heading p { margin: 4px 0 0; color: var(--text-muted); font-size: .82rem; }
.mastery-bottom-controls { display: flex; align-items: center; flex-direction: column; gap: 9px; }
.mastery-bottom-controls .card-review-controls { width: min(100%, 420px); }
.recitation-editor-modal[hidden] { display: none; }
.recitation-editor-modal { position: fixed; inset: 0; z-index: 1000; display: grid; place-items: center; padding: 18px; background: rgba(15,23,42,.28); backdrop-filter: blur(8px); }
.recitation-editor-card { width: min(100%, 560px); max-height: calc(100dvh - 36px); overflow: auto; display: grid; gap: 12px; padding: 18px; border: 1px solid var(--border-color); border-radius: 14px; background: var(--panel-bg); box-shadow: 0 24px 60px rgba(15,23,42,.22); }
.recitation-editor-card header { display: flex; align-items: center; justify-content: space-between; gap: 12px; }
.recitation-editor-card h3 { margin: 0; font-size: 1.05rem; }
.recitation-editor-card header button { display: grid; place-items: center; width: 34px; height: 34px; border: 0; border-radius: 999px; background: var(--primary-light); color: var(--primary-color); cursor: pointer; font-size: 1.2rem; }
.recitation-editor-card label { display: grid; gap: 6px; color: var(--text-muted); font-size: .82rem; font-weight: 800; }
.recitation-editor-card input, .recitation-editor-card select, .recitation-editor-card textarea { width: 100%; border: 1px solid var(--border-color); border-radius: 9px; padding: 10px 11px; background: var(--bg-color); color: var(--text-main); font: inherit; box-sizing: border-box; }
.recitation-editor-card textarea { resize: vertical; line-height: 1.55; }
.recitation-answer-editor { display: grid; gap: 10px; }
.recitation-answer-mode-toggle { display: inline-flex; align-items: center; gap: 10px; color: var(--text-muted); font-size: .82rem; font-weight: 800; }
.recitation-answer-mode-toggle label { display: inline-flex; align-items: center; gap: 5px; }
.recitation-editor-list-area { display: flex; flex-direction: column; gap: 10px; }
.recitation-editor-list-area[hidden],
.recitation-editor-text-area[hidden] { display: none !important; }
.recitation-editor-list-container { display: grid; gap: 9px; }
.recitation-editor-rich-text { min-height: 180px; }
.recitation-editor-card footer { display: flex; justify-content: flex-end; gap: 10px; }

body.mastery-page {
  overflow-x: hidden;
  overflow-y: auto;
  overscroll-behavior-y: contain;
}

body.mastery-page .app-container,
body.mastery-page .main-content {
  height: auto;
  min-height: calc(100dvh / var(--study-page-zoom, 1));
  overflow-x: hidden;
}

.mastery-dashboard {
  width: min(100% - 32px, 980px);
  margin: 12px auto 48px;
  padding-bottom: calc(28px + env(safe-area-inset-bottom));
}

.mastery-review,
.mastery-review-stage {
  overflow: visible;
}

.mastery-review-stage {
  width: min(100%, 900px);
  margin: 0 auto;
  padding-inline: 62px;
  padding-bottom: 0;
}

.mastery-card-scene {
  flex: 0 0 auto;
  width: min(100%, 760px);
  height: clamp(500px, calc(100dvh - 210px), 700px);
  min-height: clamp(500px, calc(100dvh - 210px), 700px);
  max-height: none;
}

.mastery-review-card,
.mastery-review-card .card-face {
  min-height: 0;
}

.mastery-review-card .card-question-text,
.mastery-review-card .card-answer-text-container {
  flex: 1 1 0%;
  min-height: 0;
  max-height: 100%;
  overflow-y: auto;
  overflow-x: hidden;
  overscroll-behavior: contain;
  -webkit-overflow-scrolling: touch;
  touch-action: pan-y;
}

.mastery-review-card .card-question-text {
  align-items: flex-start;
  justify-content: flex-start;
}

.mastery-review-card .card-answer-text {
  max-height: none;
  overflow: visible;
}

.mastery-bottom-controls {
  position: relative;
  z-index: 3;
  flex: 0 0 auto;
  margin-top: 14px;
  padding-bottom: max(0px, env(safe-area-inset-bottom));
  transform: none;
}

body.mastery-immersive .mastery-top-bar,
body.mastery-immersive .recitation-review-tabs { display: none !important; }
body.mastery-immersive .mastery-dashboard { width: min(100% - 24px, 980px); margin: 10px auto 0; }
body.mastery-immersive .mastery-card-scene { height: calc(100dvh - 150px); max-height: none; }

@media (min-width: 769px) and (max-width: 1180px) {
  .mastery-top-bar {
    grid-template-columns: max-content minmax(0, 1fr) max-content;
    align-items: center;
    column-gap: 10px;
    min-height: 66px;
    padding: 7px 16px;
  }
  body.mastery-page .mastery-top-bar {
    grid-template-columns: max-content minmax(0, 1fr) max-content !important;
    column-gap: 10px !important;
    min-height: 66px !important;
    padding: 7px 16px !important;
  }
  .mastery-top-bar .top-bar-left {
    min-width: 126px;
    gap: 10px;
  }
  body.mastery-page .mastery-top-bar .top-bar-left {
    grid-column: 1 !important;
    grid-row: 1 !important;
    gap: 10px !important;
  }
  .mastery-top-bar .current-chapter-info h2 { font-size: 1rem; margin: 0; }
  .mastery-top-bar .current-chapter-info span { display: none !important; }
  .mastery-header-controls {
    grid-column: 2;
    grid-row: 1;
    grid-template-columns: minmax(160px, 190px) minmax(305px, 1fr) max-content;
    justify-content: center;
    align-items: stretch;
    width: 100%;
    gap: 7px;
  }
  body.mastery-page .mastery-header-controls {
    grid-column: 2 !important;
    grid-row: 1 !important;
    grid-template-columns: minmax(160px, 190px) minmax(305px, 1fr) max-content !important;
    width: 100% !important;
  }
  .mastery-subject-cards { min-height: 48px; }
  .mastery-subject-picker { min-width: 0; width: 100%; }
  .mastery-subject-picker > summary { min-height: 48px; padding: 0 11px; border-radius: 12px; font-size: .88rem; }
  .recitation-review-tabs { width: 100%; min-height: 48px; gap: 5px; padding: 5px; border-radius: 18px; }
  .recitation-review-tab { flex: 1 1 0; min-width: 0; min-height: 38px; padding: 0 8px; gap: 6px; border-radius: 14px; }
  .recitation-review-tab span { font-size: .82rem; }
  .recitation-review-tab strong { min-width: 23px; height: 23px; padding: 0 6px; font-size: .9rem; }
  .mastery-top-card-tools { min-height: 48px; gap: 5px; padding: 5px; border-radius: 16px; }
  .mastery-top-card-tools .flashcard-icon-btn { width: 38px; min-width: 38px; height: 38px; min-height: 38px; border-radius: 12px; }
  .mastery-top-bar .top-bar-right {
    grid-column: 3;
    grid-row: 1;
  }
  body.mastery-page .mastery-top-bar .top-bar-right {
    grid-column: 3 !important;
    grid-row: 1 !important;
  }
  .mastery-dashboard { width: min(100% - 28px, 940px); margin-top: 12px; }
  .mastery-review-stage { width: 100%; padding-inline: 44px; }
  .mastery-card-scene {
    width: min(100%, 760px);
    height: clamp(420px, calc(100dvh - 206px), 590px);
    min-height: clamp(420px, calc(100dvh - 206px), 590px);
  }
}

@media (max-width: 768px) {
  body.mastery-page,
  body.mastery-page .app-container,
  body.mastery-page .main-content {
    height: auto;
    overflow-y: auto;
  }

  body.mastery-page .mastery-top-bar {
    display: grid !important;
    grid-template-columns: minmax(0, 1fr) max-content !important;
    grid-template-rows: 44px auto !important;
    align-items: center !important;
    gap: 8px 10px !important;
    min-height: 0 !important;
    padding: calc(8px + env(safe-area-inset-top)) 10px 8px !important;
    overflow: visible !important;
  }

  body.mastery-page .mastery-top-bar .top-bar-left {
    grid-column: 1 !important;
    grid-row: 1 !important;
    display: flex !important;
    align-items: center !important;
    min-width: 0 !important;
    gap: 10px !important;
  }

  body.mastery-page .mastery-top-bar .top-bar-right {
    grid-column: 2 !important;
    grid-row: 1 !important;
    align-self: center !important;
    display: inline-flex !important;
    align-items: center !important;
    justify-content: flex-end !important;
    gap: 8px !important;
  }

  body.mastery-page .mastery-top-bar .current-chapter-info {
    display: block !important;
    min-width: 0 !important;
  }

  body.mastery-page .mastery-top-bar .current-chapter-info h2 {
    margin: 0 !important;
    font-size: 1.08rem !important;
    line-height: 1.1 !important;
  }

  body.mastery-page .mastery-top-bar .current-chapter-info span {
    display: none !important;
  }

  body.mastery-page .mastery-header-controls {
    grid-column: 1 / -1 !important;
    grid-row: 2 !important;
    display: flex !important;
    flex-wrap: nowrap !important;
    align-items: center !important;
    justify-content: flex-start !important;
    width: 100% !important;
    min-width: 0 !important;
    gap: 8px !important;
    overflow-x: auto !important;
    overflow-y: visible !important;
    overscroll-behavior-inline: contain;
    scrollbar-width: none;
    -webkit-overflow-scrolling: touch;
  }

  body.mastery-page .mastery-header-controls::-webkit-scrollbar { display: none; }

  body.mastery-page .mastery-header-controls .mastery-subject-cards,
  body.mastery-page .mastery-header-controls .recitation-review-tabs,
  body.mastery-page .mastery-header-controls .mastery-top-card-tools {
    flex: 0 0 auto !important;
    width: auto !important;
    min-height: 46px !important;
  }

  body.mastery-page .mastery-subject-picker { width: clamp(176px, 52vw, 244px); min-width: 0; }
  body.mastery-page .mastery-subject-picker > summary { min-height: 46px; padding: 0 14px; border-radius: 16px; }
  body.mastery-page .mastery-subject-picker > summary span { white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
  body.mastery-page .mastery-subject-menu { left: 0; right: auto; transform: none; min-width: min(260px, calc(100vw - 24px)); }

  body.mastery-page .recitation-review-tabs {
    width: auto !important;
    min-width: max-content !important;
    min-height: 46px !important;
    padding: 4px !important;
    gap: 4px !important;
    overflow: visible !important;
    border-radius: 18px !important;
  }

  body.mastery-page .recitation-review-tab {
    flex: 0 0 auto !important;
    min-width: 86px !important;
    min-height: 38px !important;
    padding: 0 10px !important;
    gap: 5px !important;
    border-radius: 14px !important;
  }

  body.mastery-page .recitation-review-tab span { max-width: 74px; font-size: .78rem; }
  body.mastery-page .recitation-review-tab strong { min-width: 22px; height: 22px; padding: 0 6px; font-size: .82rem; }

  body.mastery-page .mastery-top-card-tools {
    min-height: 46px !important;
    padding: 4px !important;
    gap: 5px !important;
    border-radius: 16px !important;
  }

  body.mastery-page .mastery-top-card-tools .flashcard-icon-btn {
    width: 38px !important;
    min-width: 38px !important;
    height: 38px !important;
    min-height: 38px !important;
    border-radius: 13px !important;
  }

  .mastery-dashboard {
    width: min(100% - 16px, 760px);
    min-height: calc(100dvh - var(--study-topbar-height, 62px));
    margin: 8px auto 0;
    display: flex;
    flex-direction: column;
  }

  .mastery-review { flex: 1 1 auto; min-height: 0; }
  .mastery-review-stage { width: 100%; padding-inline: 34px; margin-top: 0; }
  .mastery-card-scene {
    width: min(100%, 430px);
    height: clamp(350px, calc(100dvh - 188px), 620px);
    min-height: clamp(350px, calc(100dvh - 188px), 620px);
  }
  .mastery-review-card .card-face { padding: 16px 14px; }
  .mastery-review-card .card-question-text { font-size: clamp(1.08rem, 5vw, 1.38rem); }
  .mastery-review-stage .floating-nav-btn { width: 34px; height: 34px; }
  .mastery-review-stage .floating-nav-prev { left: 0; }
  .mastery-review-stage .floating-nav-next { right: 0; }
  .mastery-bottom-controls { margin-top: 10px; }
  .mastery-bottom-controls .card-quick-controls { width: min(100%, 250px); }
  .mastery-bottom-controls .card-review-controls { width: min(100%, 330px); }
  .recitation-editor-modal { align-items: end; padding: 10px; }
  .recitation-editor-card { max-height: calc(100dvh - 20px); border-radius: 16px 16px 10px 10px; }
  body.mastery-immersive .mastery-dashboard { height: 100dvh; margin-top: 0; }
  body.mastery-immersive .mastery-card-scene { height: calc(100dvh - 120px); }
}

/* Mastery card stage: share the flashcard viewport layout, with card text scrolling inside. */
html.mastery-shell-page {
  --mastery-stage-gap: clamp(18px, 2.4dvh, 28px);
  height: calc(100dvh / var(--study-page-zoom, 1)) !important;
  min-height: 0 !important;
  overflow: hidden !important;
}

body.mastery-page {
  height: calc(100dvh / var(--study-page-zoom, 1)) !important;
  min-height: 0 !important;
  overflow: hidden !important;
}

body.mastery-page .app-container,
body.mastery-page .main-content {
  min-height: 0 !important;
  overflow: hidden !important;
}

body.mastery-page .main-content {
  height: calc(100dvh / var(--study-page-zoom, 1)) !important;
}

body.mastery-page .mastery-dashboard {
  flex: 1 1 auto !important;
  display: flex !important;
  flex-direction: column !important;
  width: min(100% - 28px, 1040px) !important;
  min-height: 0 !important;
  height: auto !important;
  margin: 8px auto 0 !important;
  padding-bottom: max(6px, env(safe-area-inset-bottom)) !important;
  overflow: hidden !important;
}

body.mastery-page .mastery-review {
  flex: 1 1 auto !important;
  display: grid !important;
  grid-template-columns: minmax(0, 1fr) !important;
  grid-template-rows: minmax(0, 1fr) auto !important;
  align-items: stretch;
  width: 100% !important;
  min-height: 0 !important;
  height: 100% !important;
  gap: var(--mastery-stage-gap) !important;
  margin: 0 !important;
  overflow: hidden !important;
}

body.mastery-page .mastery-review-stage {
  justify-self: center !important;
  min-height: 0 !important;
  height: auto !important;
  width: min(100%, 960px) !important;
  padding-inline: 62px !important;
  align-self: stretch !important;
  align-items: stretch !important;
  overflow: visible !important;
}

body.mastery-page .mastery-card-scene {
  align-self: stretch;
  width: min(100%, 800px) !important;
  height: var(--mastery-card-height, 100%) !important;
  min-height: 0 !important;
  max-height: var(--mastery-card-height, 100%) !important;
}

body.mastery-page .mastery-review-card,
body.mastery-page .mastery-review-card .card-face {
  min-height: 0 !important;
}

body.mastery-page .mastery-review-card .card-question-text,
body.mastery-page .mastery-review-card .card-answer-text-container {
  flex: 1 1 0 !important;
  min-height: 0 !important;
  max-height: 100% !important;
  overflow-y: auto !important;
  overflow-x: hidden !important;
}

body.mastery-page .mastery-review-card .card-answer-text {
  max-height: none !important;
  overflow: visible !important;
}

body.mastery-page .mastery-bottom-controls {
  justify-self: center !important;
  flex: 0 0 auto !important;
  width: min(100%, 420px) !important;
  min-height: 52px !important;
  margin-top: 0 !important;
  padding-bottom: max(2px, env(safe-area-inset-bottom)) !important;
  overflow: visible !important;
}

/* Mastery editing layout: match flashcards without overlapping the two faces. */
body.mastery-inline-editing .mastery-review-card {
  display: grid !important;
  grid-template-rows: minmax(0, 0.36fr) minmax(0, 0.64fr) !important;
  gap: 12px !important;
  height: 100% !important;
  min-height: 0 !important;
  transform: none !important;
  transform-style: flat !important;
  cursor: text !important;
}

body.mastery-inline-editing .mastery-review-card .card-face {
  position: relative !important;
  height: 100% !important;
  min-height: 0 !important;
  transform: none !important;
  backface-visibility: visible !important;
  opacity: 1 !important;
  visibility: visible !important;
  pointer-events: auto !important;
  overflow: hidden !important;
}

body.mastery-inline-editing .mastery-review-card .card-question-text[contenteditable="true"],
body.mastery-inline-editing .mastery-review-card .card-answer-text-container {
  min-height: 0 !important;
  max-height: 100% !important;
  overflow-y: auto !important;
  overflow-x: hidden !important;
}

body.mastery-inline-editing .mastery-review-card .card-answer-text[contenteditable="true"] {
  min-height: 100% !important;
  max-height: none !important;
  overflow: visible !important;
}

body.mastery-immersive .main-content,
body.mastery-immersive .mastery-dashboard,
body.mastery-immersive .mastery-review {
  height: calc(100dvh / var(--study-page-zoom, 1)) !important;
  min-height: 0 !important;
  overflow: hidden !important;
}

body.mastery-immersive .mastery-dashboard {
  width: min(100% - 18px, 1100px) !important;
  margin: 0 auto !important;
  padding: max(8px, env(safe-area-inset-top)) max(8px, env(safe-area-inset-right)) max(8px, env(safe-area-inset-bottom)) max(8px, env(safe-area-inset-left)) !important;
}

body.mastery-immersive .mastery-review-stage {
  width: min(100%, 1020px) !important;
}

@media (min-width: 769px) and (max-width: 1180px) {
  body.mastery-page .mastery-dashboard {
    width: min(100% - 22px, 960px) !important;
    margin-top: 7px !important;
  }

  body.mastery-page .mastery-review-stage {
    width: 100% !important;
    padding-inline: 44px !important;
  }

  body.mastery-page .mastery-card-scene {
    width: min(100%, 820px) !important;
    height: var(--mastery-card-height, 100%) !important;
    min-height: 0 !important;
    max-height: var(--mastery-card-height, 100%) !important;
  }

  body.mastery-page .mastery-review-card .card-face {
    padding: 22px 28px !important;
  }
}

@media (hover: hover) and (pointer: fine) and (min-width: 769px) {
  html.mastery-shell-page:not(.touch-study-device):not(:has(body.mastery-immersive)) {
    height: auto !important;
    min-height: 100% !important;
    overflow: auto !important;
  }

  html:not(.touch-study-device) body.mastery-page:not(.mastery-immersive) {
    height: auto !important;
    min-height: 100vh !important;
    overflow-x: hidden !important;
    overflow-y: auto !important;
  }

  html:not(.touch-study-device) body.mastery-page:not(.mastery-immersive) .app-container,
  html:not(.touch-study-device) body.mastery-page:not(.mastery-immersive) .main-content {
    height: auto !important;
    min-height: 100vh !important;
    overflow-x: hidden !important;
    overflow-y: auto !important;
  }

  html:not(.touch-study-device) body.mastery-page:not(.mastery-immersive) .mastery-dashboard {
    flex: 0 0 auto !important;
    height: auto !important;
    min-height: 0 !important;
    margin: 12px auto 48px !important;
    padding-bottom: calc(28px + env(safe-area-inset-bottom)) !important;
    overflow: visible !important;
  }

  html:not(.touch-study-device) body.mastery-page:not(.mastery-immersive) .mastery-review {
    display: grid !important;
    grid-template-columns: minmax(0, 1fr) !important;
    grid-template-rows: auto auto !important;
    justify-items: center !important;
    align-items: start !important;
    height: auto !important;
    min-height: 0 !important;
    gap: var(--mastery-stage-gap) !important;
    overflow: visible !important;
  }

  html:not(.touch-study-device) body.mastery-page:not(.mastery-immersive) .mastery-review-stage {
    justify-self: center !important;
    height: auto !important;
    align-items: center !important;
    overflow: visible !important;
  }

  html:not(.touch-study-device) body.mastery-page:not(.mastery-immersive) .mastery-card-scene {
    height: clamp(500px, calc(100dvh - 210px), 700px) !important;
    min-height: clamp(500px, calc(100dvh - 210px), 700px) !important;
    max-height: none !important;
  }

  html:not(.touch-study-device) body.mastery-page:not(.mastery-immersive) .mastery-bottom-controls {
    justify-self: center !important;
    margin-top: 0 !important;
    padding-bottom: max(0px, env(safe-area-inset-bottom)) !important;
  }
}

@media (max-width: 768px) {
  body.mastery-page .main-content {
    height: calc(100dvh / var(--study-page-zoom, 1)) !important;
  }

  body.mastery-page .mastery-dashboard {
    width: min(100% - 12px, 760px) !important;
    min-height: 0 !important;
    height: auto !important;
    margin-top: 5px !important;
    padding-bottom: max(5px, env(safe-area-inset-bottom)) !important;
  }

  body.mastery-page .mastery-review {
    --mastery-stage-gap: clamp(12px, 2dvh, 18px);
    gap: var(--mastery-stage-gap) !important;
  }

  body.mastery-page .mastery-review-stage {
    width: 100% !important;
    padding-inline: 28px !important;
  }

  body.mastery-page .mastery-card-scene {
    width: 100% !important;
    height: var(--mastery-card-height, 100%) !important;
    min-height: 0 !important;
    max-height: var(--mastery-card-height, 100%) !important;
  }

  body.mastery-page .mastery-review-card .card-face {
    padding: clamp(14px, 3.6vw, 20px) clamp(12px, 3.2vw, 18px) !important;
  }

  body.mastery-page .mastery-bottom-controls {
    padding-bottom: max(2px, env(safe-area-inset-bottom)) !important;
  }

  body.mastery-page .mastery-bottom-controls .card-review-controls {
    width: min(100%, 330px) !important;
  }
}

/* Final override: keep review-mode tabs centered after folder/action buttons were added. */
body.mastery-page .mastery-header-controls {
  display: grid !important;
  grid-template-columns: minmax(180px, 1fr) minmax(390px, max-content) minmax(180px, 1fr) !important;
  justify-content: stretch !important;
  align-items: stretch !important;
}

body.mastery-page .mastery-subject-cards { justify-self: end !important; }
body.mastery-page .recitation-review-tabs { justify-self: center !important; }
body.mastery-page .mastery-top-card-tools { justify-self: start !important; }

@media (max-width: 768px) {
  body.mastery-page .mastery-header-controls {
    display: flex !important;
    justify-content: flex-start !important;
  }
}
