/* 统一的手机端适配层。
   作为最后一个样式表加载，覆盖各页面的桌面布局与页面专属样式。 */

html {
  min-width: 0;
  overflow-x: hidden;
  scroll-padding-top: 72px;
}

body {
  min-width: 0;
  overflow-x: hidden;
  padding-bottom: env(safe-area-inset-bottom);
}

img,
svg,
video,
iframe {
  max-width: 100%;
}

button,
input,
select,
textarea {
  max-width: 100%;
  font-family: inherit;
}

@media (max-width: 760px) {
  :root {
    --mobile-gutter: 16px;
  }

  body {
    font-size: 16px;
  }

  header.topbar {
    min-height: 56px;
    padding: env(safe-area-inset-top) max(14px, env(safe-area-inset-right)) 0 max(14px, env(safe-area-inset-left));
    gap: 14px;
    overflow-x: auto;
    overflow-y: hidden;
    overscroll-behavior-x: contain;
    -webkit-overflow-scrolling: touch;
    scrollbar-width: none;
  }

  header.topbar::-webkit-scrollbar {
    display: none;
  }

  header.topbar h1 {
    flex: 0 0 auto;
    min-height: 44px;
    margin-right: 0;
    padding-right: 10px;
    font-size: 16px;
  }

  header.topbar a {
    flex: 0 0 auto;
    min-height: 44px;
    white-space: nowrap;
    font-size: 13px;
  }

  .container {
    width: 100%;
    max-width: none;
    min-width: 0;
    padding: 20px var(--mobile-gutter) max(40px, calc(28px + env(safe-area-inset-bottom)));
  }

  html.embed .container {
    padding-top: 12px;
  }

  .container p,
  .container li {
    text-align: left;
    text-justify: auto;
  }

  button,
  select,
  input:not([type="checkbox"]):not([type="radio"]):not([type="range"]),
  textarea {
    min-height: 44px;
    font-size: 16px;
  }

  /* 阅读正文内的朗读图标保持行内紧凑，避免把英文句子撑成锯齿状。 */
  .speak-sentence,
  .speak-vocab-word,
  .speak-review-word,
  .lib-speak {
    min-height: 0;
  }

  .library-shell,
  .listening-page > .container {
    padding: 20px var(--mobile-gutter) max(44px, calc(32px + env(safe-area-inset-bottom)));
  }

  .library-hero {
    min-height: 0;
    padding: 26px 20px;
    gap: 24px;
    border-radius: 18px;
  }

  .library-hero h2 {
    font-size: clamp(29px, 9vw, 34px);
  }

  .library-hero p {
    font-size: 15px;
    line-height: 1.7;
  }

  .library-hero::after {
    right: -4px;
    top: -22px;
    font-size: 86px;
  }

  .library-stats {
    width: 100%;
    min-width: 0;
  }

  .library-stats div {
    padding: 14px 8px;
  }

  .library-stats strong {
    font-size: 22px;
  }

  .library-toolbar {
    align-items: stretch;
    flex-direction: column;
    gap: 14px;
    margin-top: 28px;
  }

  .library-toolbar > div:first-child {
    align-items: flex-start;
    flex-direction: column;
    gap: 4px;
  }

  .library-toolbar-label {
    font-size: 22px;
  }

  .library-search {
    width: 100%;
    height: 44px;
  }

  .reading-library .book-shelf,
  .lsn-book-shelf {
    gap: 12px;
  }

  .reading-library .book-group,
  .lsn-book-group {
    border-radius: 14px;
  }

  .reading-library .book-head,
  .lsn-book-head {
    padding: 18px;
  }

  .reading-library .book-group:not(.collapsed) .book-head,
  .lsn-book-group:not(.collapsed) .lsn-book-head {
    grid-template-columns: 48px minmax(0, 1fr) auto;
    padding: 14px;
    gap: 0 10px;
  }

  .reading-library .book-group:not(.collapsed) .book-number,
  .lsn-book-group:not(.collapsed) .book-number {
    font-size: 30px;
  }

  .reading-library .book-title-block h2 {
    font-size: 18px;
  }

  .reading-library .book-group .card-grid,
  .lsn-tests-grid {
    padding: 14px;
    gap: 10px;
  }

  .reading-library .card {
    min-height: 132px;
    padding: 15px;
  }

  .method-legend {
    width: 100%;
    max-width: 100%;
    flex-wrap: nowrap;
    overflow-x: auto;
    padding-bottom: 2px;
    scrollbar-width: none;
  }

  .method-legend::-webkit-scrollbar {
    display: none;
  }

  .method-legend span {
    flex: 0 0 auto;
    min-height: 32px;
    display: inline-flex;
    align-items: center;
  }

  /* 阅读器：工具栏和每个内容面板都按手机宽度收缩。 */
  .reader-toolbar {
    align-items: center;
    gap: 8px;
    margin-bottom: 10px;
  }

  .reader-toolbar .src {
    flex: 1 0 100%;
    min-width: 0;
    font-size: 13px;
    line-height: 1.45;
  }

  .phase-tabs {
    flex: 1 1 auto;
    min-width: 0;
  }

  .phase-tab {
    flex: 1 1 0;
    min-height: 44px;
    padding: 7px 10px;
  }

  .toolbar-btn {
    min-height: 44px;
    padding-inline: 8px;
  }

  .reader-grid,
  .reader-grid[data-phase="practice"],
  .reader-grid[data-phase="analysis"] {
    grid-template-columns: 1fr;
    gap: 12px;
  }

  .reader-grid .pane {
    min-width: 0;
    max-height: none;
    padding: 16px;
    border-radius: 12px;
  }

  .questions-title {
    align-items: flex-start;
    flex-wrap: wrap;
    gap: 8px;
    padding: 14px;
  }

  .questions-title span {
    font-size: 19px;
  }

  .question-actions {
    width: 100%;
    justify-content: flex-start;
  }

  .question-actions button,
  .check-answers {
    min-height: 44px;
  }

  .question-group {
    padding: 16px;
  }

  .question-item {
    padding: 12px;
  }

  .question-item label {
    align-items: flex-start;
    min-height: 44px;
  }

  .question-item input {
    width: 100%;
    min-height: 44px;
  }

  /* 生词复习：输入和评分按钮改为单列，避免 320px 宽度下互相挤压。 */
  .vocab-tabs {
    flex-wrap: nowrap;
    overflow-x: auto;
    padding-bottom: 4px;
    scrollbar-width: none;
  }

  .vocab-tabs::-webkit-scrollbar {
    display: none;
  }

  .vocab-tabs .tab {
    flex: 0 0 auto;
    min-height: 44px;
    white-space: nowrap;
  }

  .review-studio {
    grid-template-columns: 1fr;
    gap: 14px;
    padding: 16px;
    border-radius: 16px;
  }

  .review-intro h2 {
    font-size: 28px;
  }

  .review-card {
    min-width: 0;
    min-height: 0;
    padding: 18px;
  }

  .review-word-line,
  .review-word {
    min-width: 0;
  }

  .review-word {
    overflow-wrap: anywhere;
  }

  .review-actions {
    grid-template-columns: 1fr;
  }

  .review-actions button {
    min-height: 52px;
  }

  .cloze-input-row {
    flex-direction: column;
  }

  .cloze-input-row input,
  .cloze-input-row button {
    width: 100%;
    min-width: 0;
  }

  .vocab-tools {
    align-items: stretch;
  }

  .vocab-tools input[type="text"] {
    width: 100%;
    min-width: 0;
  }

  .vocab-tools > button,
  .vocab-tools > .filebtn {
    flex: 1 1 140px;
    min-height: 44px;
  }

  .vocab-tools > .filebtn {
    white-space: normal;
    text-align: center;
  }

  .vocab-count {
    width: 100%;
    margin-left: 0;
  }

  /* 生词表在桌面是表格，手机上改为带字段标签的卡片。 */
  table.vocab {
    display: block;
    overflow: visible;
    background: transparent;
    border-radius: 0;
  }

  table.vocab thead {
    display: none;
  }

  table.vocab tbody {
    display: block;
  }

  table.vocab tbody > tr:not(.vocab-aids-row) {
    display: block;
    margin-bottom: 12px;
    border: 1px solid #e4ece5;
    border-radius: 12px;
    background: var(--card);
    box-shadow: 0 1px 4px rgba(0, 0, 0, .05);
  }

  table.vocab tbody > tr:not(.vocab-aids-row) > td {
    display: grid;
    grid-template-columns: 4.5rem minmax(0, 1fr);
    gap: 10px;
    align-items: start;
    padding: 10px 14px;
    border-bottom: 1px solid #eef2ee;
    overflow-wrap: anywhere;
  }

  table.vocab tbody > tr:not(.vocab-aids-row) > td::before {
    color: #748178;
    font-size: 12px;
    font-weight: 700;
  }

  table.vocab tbody > tr:not(.vocab-aids-row) > td:nth-child(1)::before { content: "单词"; }
  table.vocab tbody > tr:not(.vocab-aids-row) > td:nth-child(2)::before { content: "释义"; }
  table.vocab tbody > tr:not(.vocab-aids-row) > td:nth-child(3)::before { content: "记忆法"; }
  table.vocab tbody > tr:not(.vocab-aids-row) > td:nth-child(4)::before { content: "出处"; }
  table.vocab tbody > tr:not(.vocab-aids-row) > td:nth-child(5)::before { content: "加入时间"; }

  table.vocab tbody > tr:not(.vocab-aids-row) > td:last-child {
    display: flex;
    justify-content: flex-end;
    border-bottom: 0;
  }

  table.vocab tbody > tr:not(.vocab-aids-row) > td:last-child::before {
    display: none;
  }

  table.vocab tbody > tr.vocab-aids-row {
    display: block;
    margin: -12px 0 12px;
    border: 1px solid #e4ece5;
    border-top: 0;
    border-radius: 0 0 12px 12px;
    background: #fbfdfb;
  }

  table.vocab tbody > tr.vocab-aids-row > td {
    display: block;
    padding: 12px 14px 15px;
  }

  /* 词库与词根页 */
  .lib-head {
    align-items: flex-start;
    flex-direction: column;
    gap: 5px;
  }

  .lib-head h2 {
    font-size: 24px;
  }

  .lib-progress {
    width: 100%;
  }

  .lib-search {
    width: 100%;
    min-height: 44px;
  }

  .lib-pager {
    gap: 7px;
  }

  .lib-pager select {
    max-width: 100%;
    min-height: 44px;
  }

  .lib-list-banner,
  .lib-root-title {
    align-items: flex-start;
    flex-wrap: wrap;
  }

  .family-shell {
    width: min(calc(100% - 24px), 620px);
    padding: 20px 0 max(44px, calc(32px + env(safe-area-inset-bottom)));
  }

  .family-hero {
    padding: 22px 18px;
    border-radius: 16px;
  }

  .family-root-line span {
    max-width: 100%;
    overflow-wrap: anywhere;
  }

  .family-explainer {
    padding: 15px 16px;
  }

  .family-toolbar {
    align-items: flex-start;
    flex-direction: column;
    gap: 5px;
  }

  .family-word-card {
    padding: 17px 16px;
  }

  /* 听力精听页 */
  .listen-toolbar {
    align-items: stretch;
    gap: 8px;
  }

  .listen-back {
    min-height: 44px;
    display: inline-flex;
    align-items: center;
  }

  .listen-toolbar .src {
    flex: 1 0 100%;
    min-width: 0;
    font-size: 16px;
    overflow-wrap: anywhere;
  }

  .lsn-annotate-link {
    margin-left: 0;
    min-height: 44px;
    display: inline-flex;
    align-items: center;
  }

  .listen-grid {
    grid-template-columns: 1fr;
    gap: 12px;
  }

  .listen-grid .pane {
    max-height: none;
    padding: 14px;
    border-radius: 14px;
  }

  .lsn-right,
  .lsn-side {
    gap: 12px;
  }

  .listening-page #player-view .tr-bar {
    margin: -14px -14px 12px;
    padding: 12px 14px;
    top: -14px;
  }

  .seg .seg-meta {
    flex-wrap: wrap;
  }

  .turn-speaker {
    max-width: 85px;
    overflow: hidden;
    text-overflow: ellipsis;
  }

  .pl-sticky {
    width: 100%;
  }

  .pl-sticky .pl-main button,
  .pl-sticky .pl-select {
    min-height: 44px;
  }

  /* 语法目录、写作和口语 */
  .grammar-mobilebar {
    position: sticky;
    top: 0;
    z-index: 10;
    padding: 5px 0 8px;
    background: var(--bg);
  }

  #toc-toggle {
    min-height: 44px;
  }

  .grammar-content {
    min-width: 0;
    padding: 16px;
    border-radius: 12px;
  }

  .grammar-toc {
    top: 0;
    width: min(88vw, 320px);
    max-height: 100dvh;
    padding-top: max(12px, env(safe-area-inset-top));
  }

  body.toc-open::before {
    content: "";
    position: fixed;
    inset: 0;
    z-index: 49;
    background: rgba(16, 32, 24, .38);
  }

  body.toc-open .grammar-mobilebar {
    position: relative;
    z-index: 51;
  }

  .grammar-toc summary,
  .grammar-toc li a {
    min-height: 44px;
    display: flex;
    align-items: center;
  }

  .writing-page,
  .speaking-page {
    width: 100%;
    max-width: none;
    padding: 20px var(--mobile-gutter) max(40px, calc(28px + env(safe-area-inset-bottom)));
  }

  .writing-tabs,
  .speaking-tabs {
    flex-wrap: nowrap;
    overflow-x: auto;
    padding-bottom: 4px;
    scrollbar-width: none;
  }

  .writing-tabs::-webkit-scrollbar,
  .speaking-tabs::-webkit-scrollbar {
    display: none;
  }

  .writing-tabs .tab,
  .speaking-tabs .tab {
    flex: 0 0 auto;
    min-height: 44px;
    white-space: nowrap;
  }

  .task-section,
  .sp-section {
    min-width: 0;
    padding: 14px;
  }

  .task-picker,
  .sp-picker {
    align-items: stretch;
    flex-direction: column;
  }

  .task-picker label[style],
  .sp-picker label[style] {
    margin-left: 0 !important;
  }

  .task-picker select,
  .sp-picker select {
    width: 100%;
    min-width: 0 !important;
  }

  .task-actions,
  .sp-answer-actions {
    align-items: stretch;
    flex-direction: column;
  }

  .task-actions button,
  .sp-submit {
    width: 100%;
  }

  .score-grid {
    grid-template-columns: repeat(2, minmax(0, 1fr));
  }

  .score-cell.overall {
    grid-column: 1 / -1;
  }

  .history-item {
    align-items: stretch;
    flex-direction: column;
  }

  .history-item button {
    width: 100%;
  }

  .task-result,
  .sp-result,
  .set-status,
  .set-warn,
  .g-notice,
  .family-explainer,
  .family-word-card {
    overflow-wrap: anywhere;
  }

  /* 设置页：表单标签独占一行，接口地址和 Key 不再撑出视口。 */
  .set-title {
    font-size: 24px;
  }

  .set-card {
    width: 100%;
    max-width: none;
    padding: 16px;
  }

  .set-row {
    align-items: stretch;
    flex-direction: column;
    gap: 6px;
  }

  .set-row label {
    flex: 0 0 auto;
  }

  .set-row input,
  .set-row select,
  .set-key-wrap,
  .set-row > div:not(.set-key-wrap) {
    width: 100%;
    min-width: 0;
    max-width: 100% !important;
  }

  .set-key-wrap {
    align-items: stretch;
  }

  .set-key-wrap input {
    min-width: 0;
  }

  .set-key-wrap .set-btn {
    flex: 0 0 auto;
    min-width: 72px;
  }

  .set-actions {
    align-items: stretch;
    flex-direction: column;
  }

  .set-actions .set-btn {
    width: 100%;
  }

  #srs-retention {
    width: 100% !important;
  }

  /* 今日任务和弹层 */
  .cal-card,
  .today-card,
  .done-card {
    padding: 18px 16px;
    border-radius: 14px;
  }

  .cal-top {
    align-items: flex-start;
    flex-direction: column;
  }

  .cal-legend {
    flex-wrap: wrap;
    row-gap: 6px;
  }

  .today-buttons {
    align-items: stretch;
    flex-direction: column;
  }

  .today-buttons button {
    width: 100%;
    min-height: 48px;
  }

  .study-wrap {
    width: 100%;
    max-width: none;
  }

  .study-controls {
    align-items: stretch;
    flex-direction: column;
  }

  .study-controls button,
  .study-remove {
    width: 100%;
    margin-left: 0;
  }

  .pace-modal {
    align-items: flex-end;
    padding: 12px;
  }

  .pace-panel {
    max-height: calc(100dvh - 24px);
    padding: 20px 16px max(20px, calc(16px + env(safe-area-inset-bottom)));
    border-radius: 16px 16px 10px 10px;
  }

  .pace-actions {
    justify-content: stretch;
  }

  .pace-actions button {
    flex: 1;
    min-height: 44px;
  }

  /* 单词测试：题干和结果按钮适应窄屏。 */
  .quiz-setup,
  .quiz-play,
  .quiz-result {
    min-width: 0;
    padding: 22px 16px;
  }

  .quiz-stem {
    font-size: 17px;
  }

  .q-prompt {
    font-size: clamp(22px, 7vw, 27px);
  }

  .quiz-missed li {
    align-items: flex-start;
    flex-direction: column;
    gap: 4px;
  }

  .quiz-missed li span {
    text-align: left;
  }

  .quiz-result-btns > button {
    width: 100%;
  }
}

@media (max-width: 560px) {
  .reading-library .book-shelf,
  .lsn-book-shelf {
    grid-template-columns: 1fr;
  }

  .lsn-part-list {
    grid-template-columns: 1fr;
  }

  .library-hero {
    padding-inline: 18px;
  }

  .library-hero h2 {
    font-size: 30px;
  }

  .library-stats strong {
    font-size: 20px;
  }

  .library-stats span {
    font-size: 10px;
  }

  .pl-sticky {
    gap: 11px 8px;
    padding: 13px 12px;
  }

  .pl-sticky .pl-main {
    gap: 6px;
  }

  .pl-sticky .pl-main button {
    padding-inline: 9px;
  }

  .turn {
    grid-template-columns: max-content minmax(0, 1fr);
    gap: 0 7px;
  }
}

@media (max-width: 360px) {
  :root {
    --mobile-gutter: 12px;
  }

  header.topbar {
    gap: 11px;
    padding-left: max(12px, env(safe-area-inset-left));
    padding-right: max(12px, env(safe-area-inset-right));
  }

  .container,
  .library-shell,
  .listening-page > .container,
  .writing-page,
  .speaking-page {
    padding-inline: var(--mobile-gutter);
  }

  .reader-grid .pane,
  .listen-grid .pane {
    padding-inline: 13px;
  }

  .library-hero {
    padding-inline: 16px;
  }
}
