@media (max-width: 900px) {
  .topbar-main {
    grid-template-columns: minmax(180px, 1fr);
  }

  .topbar-status {
    grid-column: 1 / -1;
    width: 100%;
    grid-template-columns: minmax(0, 1fr) auto auto auto auto;
  }

  .status-value {
    max-width: none;
  }
}

@media (max-width: 980px) {
  .discover-grid {
    grid-template-columns: minmax(0, 1fr);
    grid-template-rows: minmax(0, 1fr);
    height: 100%;
    overflow: hidden;
  }

  .discover-search-panel,
  .discover-detail-panel {
    grid-column: 1;
    grid-row: 1;
    width: 100%;
    height: 100%;
    min-height: 0;
    overflow: hidden;
  }

  .discover-detail-panel {
    display: none;
  }

  .discover-grid.detail-active .discover-search-panel {
    display: none;
  }

  .discover-grid.detail-active .discover-detail-panel {
    display: flex;
  }

  .discover-search-panel #searchResults,
  .discover-detail-panel #bookDetailPanel {
    overflow-x: hidden;
    overflow-y: auto;
    -webkit-overflow-scrolling: touch;
    touch-action: pan-y;
    overscroll-behavior-y: contain;
    scrollbar-gutter: stable;
  }

  .mobile-back-button {
    display: inline-flex;
    flex: 0 0 auto;
  }

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

  /* The bottom navigation is a separate grid row on phones. Keep the
     detail scroller's final action row reachable instead of ending flush
     against the navigation hit area. */
  body.mobile-shell .discover-detail-panel #bookDetailPanel {
    padding-bottom: calc(16px + var(--safe-area-bottom));
    scroll-padding-bottom: calc(16px + var(--safe-area-bottom));
  }

  /* Android tablets are touch surfaces too; the phone-only rules below must
     not be the only place where controls receive a usable hit target. */
  body.mobile-shell button:not(.job-drawer-toggle),
  body.mobile-shell select,
  body.mobile-shell input:not([type="checkbox"]):not([type="radio"]) {
    min-height: 44px;
  }

  body.mobile-shell .tabbar {
    min-height: 80px;
  }

  /* Note: nav items keep min-height 44px from the button rule above; a
     dedicated .nav-item min-height here would be inert against it. */

  body.mobile-shell .language-select,
  body.mobile-shell .theme-toggle {
    min-height: 44px;
  }

  body.mobile-shell .theme-toggle {
    width: 44px;
    min-width: 44px;
    height: 44px;
  }

  body.mobile-shell .job-drawer-toggle {
    top: 4px;
    right: 4px;
    width: 44px;
    height: 44px;
  }

  /* The 44px toggle occupies the top-right corner (right: 4 -> 48px). Keep the
     content clear of it so the first-row status pill is never clipped. */
  body.mobile-shell .job-drawer {
    padding-right: 54px;
  }

  body.mobile-shell .job-drawer.collapsed {
    min-height: 50px;
    max-height: 50px;
    padding-top: 6px;
    padding-bottom: 6px;
  }
}

@media (max-width: 720px) {
  /* Issue #371/#391: on Android the window is not edge-to-edge, so
     env(safe-area-inset-bottom) reports 0px and the fixed bottom tab bar landed
     on top of the system keys. Android reserves a floor: the height reported by
     the native shell when available (--android-nav-bar-height, set from
     get_mobile_status), otherwise a 56px default — 3-button bars run 48px on
     stock Android and a little taller on some OEM skins. Other platforms keep
     the real safe-area inset only.
     Bottom-nav gap fix: the 56px default is only a placeholder until the
     native shell has measured the bar. A measured 0px (gesture navigation
     with the hint bar turned off, or no navigation bar at all) is a real
     answer and is applied as 0px; it used to be dropped and the 56px default
     stayed, leaving a nav-bar-sized empty band under the tab icons. */
  body {
    overflow: auto;
    --mobile-nav-inset-bottom: var(--safe-area-bottom);
  }

  body.android-shell {
    /* #436：额外底部留白加在导航栏之上，不再被 max() 吞掉。 */
    --mobile-nav-inset-bottom: calc(max(env(safe-area-inset-bottom, 0px), var(--native-inset-bottom, 0px), var(--android-nav-bar-height, 56px)) + var(--user-inset-bottom, 0px));
  }

  .app-shell {
    grid-template-areas:
      "topbar"
      "announcement"
      "content"
      "jobs"
      "navigation";
    grid-template-rows: auto auto auto auto 0px;
    /* With min-height: 100dvh and a short page, the default stretch handed the
       spare height out to every auto row, so the title card grew an empty
       band under the title and the hidden announcement row became a large
       gap above the first card. Rows keep their own height; the spare room
       stays below the content. */
    align-content: start;
    padding:
      calc(6px + var(--safe-area-top))
      calc(6px + var(--safe-area-right))
      calc(6px + var(--mobile-nav-inset-bottom) + 88px)
      calc(6px + var(--safe-area-left));
    gap: 6px;
    height: auto;
    max-height: none;
    min-height: 100vh;
    min-height: 100dvh;
    overflow: visible;
  }

  body.android-shell .app-shell {
    padding-top: calc(6px + var(--safe-area-top) + var(--mobile-system-bar-height));
  }

  .topbar {
    padding: 7px;
  }

  .topbar-main {
    grid-template-columns: minmax(0, 1fr);
    gap: 6px;
  }

  .brand-block,
  .topbar-status {
    grid-column: 1;
    width: 100%;
  }

  .brand-block {
    grid-template-columns: 30px minmax(0, 1fr);
  }

  .brand-logo {
    width: 30px;
    height: 30px;
  }

  .brand-title {
    font-size: 14px;
  }

  .topbar-status {
    display: flex;
    min-height: 0;
    padding: 4px;
    flex-wrap: wrap;
    justify-content: flex-start;
  }

  .topbar-status > :first-child {
    flex: 1 1 140px;
  }

  .tabbar {
    display: grid;
    /* Issue #426: 隐藏入口后按剩下的数量平分（app.js 写 --nav-columns）。 */
    grid-template-columns: repeat(var(--nav-columns, 6), minmax(0, 1fr));
    min-height: 80px;
    padding: 3px;
    padding-bottom: calc(3px + var(--mobile-nav-inset-bottom));
    position: fixed;
    bottom: 0;
    left: 0;
    right: 0;
    z-index: 10;
    background: var(--panel);
  }

  .nav-item {
    width: 100%;
    min-width: 0;
    padding: 0 6px;
    gap: 5px;
    font-size: 12px;
    min-height: 44px;
  }

  .nav-item > span:last-child {
    min-width: 0;
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
  }

  .view-stack {
    overflow: visible;
    min-height: auto;
  }

  .view.active {
    height: auto;
    overflow: visible;
  }

  .discover-grid {
    height: auto;
    overflow: visible;
  }

  .discover-search-panel,
  .discover-detail-panel {
    height: auto;
    overflow: visible;
  }

  .discover-search-panel #searchResults,
  .discover-detail-panel #bookDetailPanel {
    overflow: visible;
  }

  .bookshelf-grid {
    overflow: visible;
  }

  .table-wrap {
    overflow: visible;
  }

  .settings-grid {
    overflow: visible;
  }

  .panel-tight,
  .bookshelf-panel {
    padding: 10px;
  }

  .section-head {
    align-items: flex-start;
  }

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

  .history-panel {
    overflow: visible;
  }

  .history-panel .table-wrap {
    flex: 0 0 auto;
    min-height: 150px;
    max-height: none;
    overflow: visible;
    border: 0;
    background: transparent;
  }

  .history-table {
    display: block;
    min-width: 0;
  }

  .history-table thead {
    position: absolute;
    width: 1px;
    height: 1px;
    padding: 0;
    overflow: hidden;
    clip: rect(0, 0, 0, 0);
    white-space: nowrap;
    border: 0;
  }

  .history-table tbody {
    display: grid;
    gap: 8px;
  }

  .history-table tr {
    display: grid;
    min-width: 0;
    border: 1px solid var(--line);
    border-radius: var(--radius-md);
    background: var(--panel-2);
  }

  .history-table th,
  .history-table td {
    border: 0;
  }

  .history-table td {
    display: grid;
    grid-template-columns: minmax(64px, 26%) minmax(0, 1fr);
    align-items: start;
    gap: 8px;
    padding: 8px 10px;
    overflow-wrap: anywhere;
  }

  .history-table td::before {
    content: attr(data-label);
    color: var(--text-3);
    font-size: 11px;
    font-weight: 700;
  }

  .history-table td:first-child {
    display: block;
    padding-top: 11px;
    padding-bottom: 11px;
    border-bottom: 1px solid var(--line);
  }

  .history-table td:first-child::before {
    display: none;
  }

  .history-title,
  .history-meta {
    overflow-wrap: anywhere;
    word-break: break-word;
  }

  .history-table td[data-label="操作"] {
    grid-template-columns: minmax(0, 1fr);
  }

  .history-table .history-actions {
    display: grid;
    grid-template-columns: minmax(0, 1fr);
    width: 100%;
  }

  .history-table .history-actions > * {
    width: 100%;
    min-width: 0;
    min-height: 44px;
    white-space: normal;
  }

  .directory-picker-dialog .dialog-actions {
    display: grid;
    grid-template-columns: repeat(2, minmax(0, 1fr));
    width: 100%;
  }

  .directory-picker-dialog .dialog-actions > button {
    width: 100%;
    min-width: 0;
    white-space: normal;
  }

  #directoryPickerSelectButton {
    grid-column: 1 / -1;
  }

  .job-drawer {
    max-height: min(210px, 32vh);
    max-height: min(210px, 32dvh);
    padding: 9px 38px 9px 9px;
  }

  .job-drawer.collapsed {
    max-height: 36px;
  }

  .reader-header {
    grid-template-columns: minmax(0, 1fr) auto;
    align-items: stretch;
  }

  .reader-controls {
    width: max-content;
    max-width: none;
    justify-content: flex-start;
    overflow: visible;
    flex-wrap: nowrap;
  }

  .reader-controls > * {
    flex: 0 0 auto;
  }

  .reader-toc-toggle {
    display: inline-flex;
  }

  .reader-toc-close {
    display: inline-flex;
    min-height: 30px;
    padding: 0 10px;
    font-size: 12px;
  }

  .reader-toc-backdrop:not(.hidden) {
    display: block;
  }

  .reader-toc {
    position: absolute;
    top: 0;
    bottom: 0;
    left: 0;
    z-index: 5;
    display: none;
    width: min(280px, 82vw);
    border-right: 1px solid var(--reader-border);
    box-shadow: 8px 0 24px rgba(0, 0, 0, 0.22);
  }

  .reader-toc.open {
    display: block;
  }

  .reader-footer {
    padding:
      8px
      calc(8px + var(--safe-area-right))
      calc(8px + var(--reader-bottom-inset, var(--safe-area-bottom)))
      calc(8px + var(--safe-area-left));
  }

  .reader-auto-panel {
    right: calc(8px + var(--safe-area-right));
    bottom: calc(var(--reader-footer-height, 112px) + 8px);
    max-width: calc(100% - 16px);
  }
}

/* Keep the primary search form usable in short windows and narrow phones. */
@media (max-width: 600px), (max-height: 500px) {
  /* Every search bar (小说 / 短剧 / 漫画): one row, the field takes the room
     and the button stays compact. The old two-column grid gave the 短剧 / 漫画
     fields half the row, so “输入短剧名称或编号” was cut off on a 360dp phone. */
  .search-form > .inline-form {
    grid-template-columns: minmax(0, 1fr) auto;
  }

  .search-form > .inline-form > input {
    grid-column: auto;
    min-width: 0;
    padding-inline: 14px;
    text-overflow: ellipsis;
  }

  .search-form > .inline-form > button {
    width: auto;
    min-width: 0;
    padding-inline: 12px;
    white-space: nowrap;
  }

  .job-drawer {
    padding-right: 54px;
  }

  .job-drawer-toggle {
    top: 4px;
    right: 4px;
    width: 44px;
    height: 44px;
  }

  /* The 44px toggle (top: 4 -> bottom 48) no longer fits the 36/38px collapsed
     bar. Grow the collapsed bar so the handle stays fully visible and tappable;
     on mobile-shell the matching 50px rule keeps the same result. */
  .job-drawer.collapsed {
    min-height: 50px;
    max-height: 50px;
  }
}

@media (max-width: 600px) {
  .topbar-main {
    grid-template-columns: minmax(0, 1fr) auto;
    align-items: center;
  }

  .brand-block {
    grid-column: 1;
  }

  .topbar-status {
    grid-column: 2;
    width: auto;
    flex-wrap: nowrap;
  }

  .topbar-status > :first-child {
    display: none;
  }

  .discover-grid {
    height: auto;
    min-height: 0;
    overflow: visible;
    touch-action: pan-y;
  }

  .result-card {
    grid-template-columns: 54px minmax(0, 1fr);
  }

  .result-cover {
    width: 54px;
    height: 72px;
  }

  .result-card > .status-pill {
    grid-column: 2;
    justify-self: start;
  }

  .detail-layout {
    grid-template-columns: 76px minmax(0, 1fr);
  }

  .detail-cover {
    width: 76px;
    height: 104px;
  }

  .path-picker {
    grid-template-columns: minmax(0, 1fr);
  }

  .path-picker > .ghost-button {
    width: 100%;
  }

  .chapter-range-row {
    display: grid;
    grid-template-columns: minmax(0, 1fr) auto minmax(0, 1fr);
  }

  .detail-form .button-row {
    display: grid;
    grid-template-columns: repeat(2, minmax(0, 1fr));
  }

  .detail-form .button-row > * {
    width: 100%;
    min-width: 0;
    white-space: normal;
  }

  .accent-button,
  .ghost-button,
  .danger-button,
  .history-action,
  .update-action-button,
  .language-select,
  .theme-toggle,
  .format-toggle-button,
  .text-input,
  select.text-input {
    min-height: 44px;
  }

  .history-toolbar .text-input {
    min-height: 44px;
  }

  .theme-toggle {
    min-width: 44px;
  }

  .bookshelf-grid {
    grid-template-columns: repeat(auto-fill, minmax(118px, 1fr));
  }

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

  .bookshelf-card-actions button {
    min-height: 44px;
  }

  .update-choice-actions {
    display: grid;
    grid-template-columns: repeat(2, minmax(0, 1fr));
  }

  .update-choice-actions > :last-child {
    grid-column: 1 / -1;
  }

  /* 手机上按钮占两行，版本说明再矮一点，保证不滚动整个弹窗也能看到「自动更新」。 */
  .release-notes {
    max-height: 30vh;
  }

  .directory-root-button,
  .directory-item {
    min-height: 44px;
  }

  .dialog-card,
  .batch-dialog,
  .directory-picker-dialog {
    padding: 12px;
  }

  .batch-toolbar {
    grid-template-columns: minmax(0, 1fr);
  }

  .batch-toolbar .path-picker {
    grid-template-columns: minmax(0, 1fr);
  }

  .toast-stack {
    right: calc(6px + var(--safe-area-right));
    bottom: var(--toast-stack-bottom, calc(6px + var(--safe-area-bottom)));
    width: calc(100vw - 12px - var(--safe-area-left) - var(--safe-area-right));
  }
}

@media (max-width: 420px) {
  .language-select {
    width: 104px;
    max-width: 104px;
  }

  .topbar {
    padding: 6px 7px;
  }

  .topbar-main {
    gap: 5px;
  }

  .brand-block {
    grid-template-columns: 28px minmax(0, 1fr);
    gap: 6px;
  }

  .brand-logo {
    width: 28px;
    height: 28px;
  }

  .topbar-status {
    gap: 4px;
    padding: 3px;
  }

  .nav-item {
    flex-direction: column;
    height: auto;
    min-height: 50px;
    gap: 2px;
    font-size: 10px;
  }

  .nav-icon,
  .nav-icon svg {
    width: 14px;
    height: 14px;
  }

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

  .section-head .button-row {
    display: flex;
    flex-wrap: wrap;
    width: 100%;
    overflow-x: visible;
  }

  .section-head .button-row > * {
    flex: 0 1 auto;
    width: auto;
    white-space: nowrap;
    font-size: 12px;
    padding: 4px 8px;
  }

  .history-panel .history-table td:first-child {
    padding-top: 7px;
    padding-bottom: 7px;
  }

  .history-panel .history-table td {
    padding: 6px 10px;
    gap: 5px;
  }

  .history-panel .history-title {
    font-size: 13px;
    line-height: 1.35;
  }

  .history-panel .history-meta {
    font-size: 11px;
    line-height: 1.35;
  }

  .history-panel .history-actions {
    gap: 4px;
  }

  .history-panel .history-action {
    font-size: 12px;
    padding: 3px 8px;
  }

  .detail-layout {
    grid-template-columns: minmax(0, 1fr);
  }

  .detail-cover {
    width: 84px;
    height: 116px;
  }

}

@media (max-height: 560px) and (min-width: 721px) {
  .app-shell {
    padding:
      calc(6px + var(--safe-area-top))
      calc(8px + var(--safe-area-right))
      calc(6px + var(--safe-area-bottom))
      calc(8px + var(--safe-area-left));
    gap: 6px;
  }

  .topbar,
  .panel-tight {
    padding: 7px 9px;
  }

  .brand-logo {
    width: 28px;
    height: 28px;
  }

  .brand-block {
    grid-template-columns: 28px minmax(0, 1fr);
  }

  .section-meta {
    display: none;
  }

  .tabbar {
    padding: 2px;
  }

  .nav-item {
    height: 28px;
    min-height: 28px;
  }

  .job-drawer {
    max-height: 132px;
  }
}

/* Landscape phones and other short mobile windows need a compact shell even
   when their CSS width is tablet-sized. Keep the content row on-screen and
   leave scrolling to the view's own list/pane. */
@media (max-width: 980px) and (max-height: 500px) {
  body {
    --mobile-nav-inset-bottom: var(--safe-area-bottom);
  }

  body.android-shell {
    /* #436：额外底部留白加在导航栏之上，不再被 max() 吞掉。 */
    --mobile-nav-inset-bottom: calc(max(env(safe-area-inset-bottom, 0px), var(--native-inset-bottom, 0px), var(--android-nav-bar-height, 56px)) + var(--user-inset-bottom, 0px));
  }

  body.mobile-shell {
    overflow: auto;
  }

  body.mobile-shell .app-shell {
    grid-template-areas:
      "topbar"
      "announcement"
      "content"
      "jobs"
      "navigation";
    grid-template-rows: auto auto auto auto 0px;
    /* Same as the portrait shell: never stretch the auto rows into gaps. */
    align-content: start;
    gap: 5px;
    padding:
      calc(5px + var(--safe-area-top) + var(--mobile-system-bar-height))
      calc(6px + var(--safe-area-right))
      calc(5px + var(--mobile-nav-inset-bottom, max(var(--safe-area-bottom), 48px)) + 72px)
      calc(6px + var(--safe-area-left));
    height: auto;
    max-height: none;
    min-height: 100vh;
    min-height: 100dvh;
    overflow: visible;
  }

  body.mobile-shell .topbar {
    padding: 5px 7px;
  }

  body.mobile-shell .topbar-main {
    grid-template-columns: minmax(0, 1fr) auto;
    gap: 5px;
  }

  body.mobile-shell .brand-block {
    grid-column: 1;
    grid-template-columns: 28px minmax(0, 1fr);
    gap: 6px;
  }

  body.mobile-shell .brand-logo {
    width: 28px;
    height: 28px;
  }

  body.mobile-shell .brand-title {
    font-size: 13px;
  }

  body.mobile-shell .topbar-status {
    grid-column: 2;
    width: auto;
    min-height: 36px;
    padding: 2px;
    flex-wrap: nowrap;
    gap: 4px;
  }

  body.mobile-shell .topbar-status > :first-child {
    display: none;
  }

  body.mobile-shell .language-select {
    width: 104px;
    max-width: 104px;
    min-height: 44px;
  }

  body.mobile-shell .theme-toggle {
    width: 44px;
    height: 44px;
  }

  body.mobile-shell .tabbar {
    display: grid;
    grid-area: navigation;
    /* Issue #426: 隐藏入口后按剩下的数量平分（app.js 写 --nav-columns）。 */
    grid-template-columns: repeat(var(--nav-columns, 6), minmax(0, 1fr));
    min-height: 64px;
    padding: 2px;
    padding-bottom: calc(2px + var(--mobile-nav-inset-bottom));
    position: fixed;
    bottom: 0;
    left: 0;
    right: 0;
    z-index: 10;
    background: var(--panel);
  }

  body.mobile-shell .nav-item {
    width: 100%;
    min-width: 0;
    height: 44px;
    min-height: 44px;
    padding: 0 4px;
    gap: 3px;
    font-size: 11px;
  }

  body.mobile-shell .nav-icon,
  body.mobile-shell .nav-icon svg {
    width: 14px;
    height: 14px;
  }

  body.mobile-shell .panel-tight,
  body.mobile-shell .bookshelf-panel {
    padding: 7px 9px;
  }

  body.mobile-shell .section-meta {
    display: none;
  }

  body.mobile-shell .announcement-rail {
    max-height: 64px;
  }

  body.mobile-shell .job-drawer {
    max-height: min(96px, 25vh);
    max-height: min(96px, 25dvh);
  }

  body.mobile-shell .history-panel {
    overflow: visible;
  }

  body.mobile-shell .history-panel .table-wrap {
    flex: 0 0 auto;
    min-height: 150px;
    max-height: none;
    overflow: visible;
  }

  body.mobile-shell .view-stack {
    overflow: visible;
    min-height: auto;
  }

  body.mobile-shell .view.active {
    height: auto;
    overflow: visible;
  }

  body.mobile-shell .discover-grid {
    height: auto;
    overflow: visible;
  }

  body.mobile-shell .discover-search-panel,
  body.mobile-shell .discover-detail-panel {
    height: auto;
    overflow: visible;
  }

  body.mobile-shell .discover-search-panel #searchResults,
  body.mobile-shell .discover-detail-panel #bookDetailPanel {
    overflow: visible;
  }

  body.mobile-shell .bookshelf-grid {
    overflow: visible;
  }

  body.mobile-shell .table-wrap {
    overflow: visible;
  }

  body.mobile-shell .settings-grid {
    overflow: visible;
  }

  body.mobile-shell .reader-header {
    grid-template-columns: minmax(0, 1fr) auto;
    gap: 6px;
    --chrome-top-gap: 5px;
    padding-bottom: 5px;
  }

  body.mobile-shell .reader-window-controls {
    display: none;
  }

  body.mobile-shell .reader-chapter-line {
    align-items: flex-start;
    flex-direction: column;
    gap: 1px;
  }

  body.mobile-shell .reader-chapter-time {
    max-width: 100%;
    overflow: hidden;
    text-overflow: ellipsis;
  }

  body.mobile-shell .reader-controls {
    max-width: none;
    overflow: visible;
    flex-wrap: nowrap;
  }

  body.mobile-shell .reader-content {
    padding: 16px calc(16px + var(--safe-area-right)) calc(16px + var(--reader-content-bottom-inset, var(--reader-bottom-inset, 0px))) calc(16px + var(--safe-area-left));
  }

  body.mobile-shell.reader-paged .reader-content {
    padding-bottom: calc(max(16px, var(--reader-info-reserve, 0px)) + var(--reader-content-bottom-inset, var(--reader-bottom-inset, 0px)));
  }

  /* Issue #460：页眉显示时上边距至少撑到页眉占的高度。 */
  body.mobile-shell.reader-paged .reader-content {
    padding-top: max(16px, var(--reader-header-reserve, 0px));
  }

  body.mobile-shell .reader-footer {
    padding-top: 5px;
    padding-bottom: calc(5px + var(--reader-bottom-inset, var(--safe-area-bottom)));
  }
}

/* Issue #343 / #353: these rules used to sit inside the short-viewport query
   above (max-width: 980px and max-height: 500px), so on a phone held upright —
   where the CSS viewport is around 360x800 — none of them ever applied and the
   report came back as "nothing changed". Panel geometry is a phone-portrait
   concern, so keep it keyed to the width alone. */
@media (max-width: 980px) {
  /* The four panel heads (search, book shelf, history, settings) grew to
     different heights because each view brought its own actions and title
     size. Pin one geometry for all of them: the title block keeps a fixed
     line height and the actions always start on their own full-width row, so
     the head no longer changes size with the number of buttons. */
  body.mobile-shell .section-head {
    flex-wrap: wrap;
    align-items: center;
    row-gap: 6px;
  }

  body.mobile-shell .section-head > div:first-child {
    flex: 1 1 100%;
    min-width: 0;
  }

  body.mobile-shell .section-head > .button-row,
  body.mobile-shell .section-head > .settings-save-actions,
  body.mobile-shell .section-head > .ghost-button,
  body.mobile-shell .section-head > .danger-button,
  body.mobile-shell .section-head > .accent-button,
  body.mobile-shell .section-head > .mobile-back-button {
    flex: 1 1 100%;
    width: 100%;
  }

  body.mobile-shell .section-title {
    font-size: 15px;
    line-height: 1.3;
  }

  /* Tapping an option on Android left the focus ring painted around it, which
     read as a stray blue frame on the tab bar and on every selected option.
     Pointer presses are already signalled by the active background, so drop
     the ring as soon as the press starts and keep it for keyboard users. */
  @media (hover: none) and (pointer: coarse) {
    body.mobile-shell .nav-item:active,
    body.mobile-shell .nav-item:focus,
    body.mobile-shell .format-toggle-button:active,
    body.mobile-shell .format-toggle-button:focus,
    body.mobile-shell .settings-tab:active,
    body.mobile-shell .settings-tab:focus,
    body.mobile-shell .ghost-button:active,
    body.mobile-shell .ghost-button:focus,
    body.mobile-shell .accent-button:active,
    body.mobile-shell .accent-button:focus,
    body.mobile-shell .danger-button:active,
    body.mobile-shell .danger-button:focus {
      outline: none;
    }
  }
}

@media (max-width: 640px) {
  .book-comments-dialog {
    width: 100%;
    height: 100%;
    min-height: 0;
    border-radius: var(--radius-lg);
  }

  .book-comments-header {
    align-items: flex-start;
  }

  .book-comment-item {
    grid-template-columns: 36px minmax(0, 1fr);
  }

  .book-comment-avatar,
  .book-comment-avatar-image,
  .book-comment-avatar-fallback {
    width: 36px;
    height: 36px;
  }

  .book-comment-head {
    display: grid;
  }

  .book-comments-footer {
    align-items: center;
  }

  body.mobile-shell .bookshelf-controls-summary {
    display: flex;
    align-items: center;
    justify-content: space-between;
    min-height: 44px;
    padding: 8px 12px;
    border: 1px solid var(--line);
    border-radius: var(--radius-md);
    background: var(--bg-1);
    color: var(--text);
    font-weight: 700;
    cursor: pointer;
    list-style: none;
  }

  body.mobile-shell .bookshelf-controls-summary::-webkit-details-marker {
    display: none;
  }

  body.mobile-shell .bookshelf-controls-summary:focus-visible {
    outline: 2px solid var(--accent);
    outline-offset: 2px;
  }

  body.mobile-shell .bookshelf-controls-chevron {
    transition: transform 160ms ease;
  }

  body.mobile-shell .bookshelf-controls-disclosure[open] .bookshelf-controls-chevron {
    transform: rotate(180deg);
  }

  body.mobile-shell .bookshelf-controls-disclosure:not([open]) > .bookshelf-toolbar {
    display: none;
  }

  /* Two equal columns that span the panel. The grid used to inherit
     justify-content: flex-start from the desktop flex toolbar, which packed its
     single implicit column down to the widest label (~135px) and left every
     segmented control and button stranded in a narrow strip on the left.
     Segmented controls and the refresh button take a whole row; the two
     toggle buttons (batch / compact) share one. */
  body.mobile-shell .bookshelf-toolbar {
    display: grid;
    grid-template-columns: repeat(2, minmax(0, 1fr));
    justify-content: stretch;
    align-items: stretch;
    gap: 8px;
    margin-top: 8px;
  }

  body.mobile-shell .bookshelf-toolbar > .format-toggle,
  body.mobile-shell .bookshelf-toolbar > .bookshelf-refresh-button {
    grid-column: 1 / -1;
    width: 100%;
    margin-left: 0;
  }

  body.mobile-shell .bookshelf-toolbar > .ghost-button {
    width: 100%;
  }

  body.mobile-shell .bookshelf-toolbar .official-bookshelf-filters {
    grid-template-columns: repeat(3, minmax(0, 1fr));
  }

  body.mobile-shell .bookshelf-toolbar .local-bookshelf-filters {
    grid-template-columns: repeat(2, minmax(0, 1fr));
  }

  /* Segmented controls fill the field on phones, matching the full-width
     inputs and directory pickers next to them (download panel, batch dialog,
     settings). On desktop they keep their compact width. */
  .format-toggle {
    width: 100%;
  }
}

/* Reader chrome owns two independent rows on phones and short landscape
   windows. Navigation never competes with the tool buttons for the same line. */
@media (max-width: 720px), (max-width: 980px) and (max-height: 500px) {
  .reader-image-preview-toolbar {
    grid-template-columns: 1fr auto 1fr;
    gap: 6px;
    min-height: 60px;
    --chrome-top-gap: 5px;
    padding-bottom: 5px;
  }

  .reader-image-preview-controls {
    gap: 3px;
  }

  .reader-image-tool-button {
    width: 44px;
    min-width: 44px;
    min-height: 44px;
  }


  .reader-footer {
    display: block;
    min-width: 0;
    overflow-x: hidden;
    overflow-y: visible;
  }

  .reader-navigation {
    display: grid;
    grid-template-columns: auto minmax(0, 1fr) auto;
    grid-template-rows: auto auto;
    align-items: center;
    width: 100%;
    min-width: 0;
    row-gap: 6px;
  }

  .reader-navigation > #readerPrevButton {
    grid-column: 1;
    grid-row: 1;
    min-width: 0;
  }

  .reader-navigation > #readerProgressLabel {
    grid-column: 2;
    grid-row: 1;
    min-width: 0;
  }

  .reader-navigation > #readerNextButton {
    grid-column: 3;
    grid-row: 1;
    min-width: 0;
  }

  .reader-navigation > #readerPrevButton,
  .reader-navigation > #readerNextButton {
    min-height: 44px;
  }

  .reader-controls {
    grid-column: 1 / -1;
    grid-row: 2;
    display: flex;
    align-items: center;
    justify-content: center;
    width: 100%;
    min-width: 0;
    max-width: 100%;
    overflow: visible;
    flex-wrap: wrap;
    gap: 4px;
  }

  .reader-controls > * {
    flex: 0 1 auto;
    min-width: 44px;
    min-height: 44px;
    max-width: 100%;
  }

  .reader-controls > .reader-toc-toggle {
    display: inline-flex;
  }

  .reader-auto-panel {
    bottom: calc(var(--reader-footer-height, 112px) + 8px);
  }

  .reader-auto-field select,
  .reader-auto-panel > #readerAutoStartButton {
    min-height: 44px;
  }
}

/* Issue #322: the tomato mark's primary-container chip reads as a theme ring
   and collides with the status strip. Drop it on phone/Android and keep the
   title a little below the status bar. The boot overlay reuses .brand-logo
   and is left alone. */
body.android-shell .topbar .brand-logo,
body.mobile-shell .topbar .brand-logo {
  display: none;
}

body.android-shell .topbar .brand-block,
body.mobile-shell .topbar .brand-block {
  grid-template-columns: minmax(0, 1fr);
  gap: 0;
}

body.android-shell .app-shell {
  padding-top: calc(16px + var(--safe-area-top) + var(--mobile-system-bar-height));
}

/* Issue #446：正文宽度只在电脑上有意义，手机和窄窗口里正文本来就铺满，不显示「宽度」按钮。 */
@media (max-width: 720px), (max-width: 980px) and (max-height: 500px) {
  .reader-width-toggle { display: none !important; }
}
body.mobile-shell .reader-width-toggle { display: none !important; }

/* #481：键盘开着时原生层把 WebView 底边抬到键盘上沿，导航栏在键盘后面，底部不再
   给它留位置；底部标签栏也先收起，把高度留给正在输入的面板。 */
body.android-shell.android-keyboard-open {
  --mobile-nav-inset-bottom: var(--user-inset-bottom, 0px);
}

body.android-shell.android-keyboard-open .reader-overlay {
  --reader-bottom-inset: var(--user-inset-bottom, 0px);
}

body.android-shell.android-keyboard-open .tabbar {
  display: none;
}
