/* UI refresh · history
   下载历史、导出记录、任务抽屉（下载进度）：.history-panel、.history-*、.summary-*、.table-wrap、.job-drawer、.job-*。
   只使用 refresh/foundation.css 中的 --ds-* 设计令牌；不得修改其他文件负责的选择器。

   本页改版要点：
   • 按钮 / 复选框命中区至少 36px，触屏（body.mobile-shell 与 ≤720px）44px；
   • 行内只留主操作，次要操作在“更多”菜单（app.js historyRowMoreMenuMarkup）；
   • ≤1023px 窗口改为卡片列表，不再出现 900px 宽表格的横向滚动；
   • 下载抽屉内容放在 .job-drawer-surface：桌面是可折叠的一行简报条，
     手机上浮在底部导航上方，#jobDrawer 只在页尾留出简报条高度的占位。 */

/* ---- Panel & header ---------------------------------------------------- */
.view-history .history-panel {
  gap: var(--ds-space-3);
}

.view-history .history-actions-row {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  justify-content: flex-end;
  gap: var(--ds-space-2);
}

.view-history .history-actions-row > button,
.view-history .history-actions-row > .menu-anchor > button {
  min-height: max(36px, var(--ds-control-sm));
  min-width: 36px;
  padding: 0 var(--ds-space-4);
  font-size: var(--ds-text-meta);
  white-space: nowrap;
}

.view-history .history-actions-row .menu-popup .menu-item,
.view-history .history-row-more-menu .menu-item {
  min-height: 40px;
  padding: var(--ds-space-2) var(--ds-space-3);
  font-size: var(--ds-text-meta);
}

.view-history .history-toolbar .text-input {
  min-height: max(36px, var(--ds-control-sm));
}

.view-history .summary-row {
  gap: var(--ds-space-2);
}

.view-history .summary-chip {
  padding: var(--ds-space-2) var(--ds-space-3);
  border-radius: var(--ds-radius-md);
  background: var(--ds-surface-card);
}

.view-history .summary-chip-label {
  color: var(--ds-text-muted);
  font-size: var(--ds-text-caption);
}

.view-history .summary-chip-value {
  color: var(--ds-text);
  font-size: var(--ds-text-title-sm);
  font-weight: var(--ds-weight-strong);
}

/* The two long hints are folded behind “使用说明”. */
.view-history .history-help {
  flex: 0 0 auto;
  color: var(--ds-text-subtle);
  font-size: var(--ds-text-caption);
}

.view-history .history-help-summary {
  display: inline-flex;
  align-items: center;
  min-height: 36px;
  padding: 0 var(--ds-space-1);
  border-radius: var(--ds-radius-sm);
  color: var(--ds-text-muted);
  cursor: pointer;
  user-select: none;
}

.view-history .history-help-summary:hover {
  background: var(--ds-state-hover);
}

.view-history .history-help[open] .settings-hint {
  margin-top: var(--ds-space-1);
}

/* ---- Table (wide windows) ---------------------------------------------- */
.view-history .table-wrap {
  border: 1px solid var(--ds-border);
  border-radius: var(--ds-radius-lg);
  background: var(--ds-surface-panel);
}

.view-history .history-table {
  min-width: 0;
  table-layout: auto;
}

.view-history .history-table th,
.view-history .history-table td {
  padding: var(--ds-space-2) var(--ds-space-3);
  border-bottom: 1px solid var(--ds-border);
  vertical-align: middle;
  font-size: var(--ds-text-meta);
}

.view-history .history-table thead th {
  position: sticky;
  top: 0;
  z-index: 1;
  background: var(--ds-surface-raised);
  color: var(--ds-text-muted);
  font-size: var(--ds-text-caption);
  font-weight: var(--ds-weight-strong);
}

.view-history .history-table tbody tr:hover {
  background: var(--ds-state-hover);
}

.view-history .history-table td[data-label="书籍"] {
  min-width: 220px;
}

.view-history .history-table td[data-label="格式"],
.view-history .history-table td[data-label="下载时间"] {
  white-space: nowrap;
}

.view-history .history-table td[data-label="操作"] {
  width: 1%;
}

.view-history .history-title {
  color: var(--ds-text);
  font-size: var(--ds-text-body);
  font-weight: var(--ds-weight-medium);
  line-height: var(--ds-leading-tight);
}

.view-history .history-meta {
  color: var(--ds-text-subtle);
  font-size: var(--ds-text-caption);
}

/* Checkboxes: 20px box inside a 36px hit area (44px on touch). */
.view-history .history-book-heading {
  align-items: center;
  gap: var(--ds-space-1);
}

.view-history .history-select-hit,
.view-history .history-select-all {
  display: inline-flex;
  align-items: center;
  flex: 0 0 auto;
  min-width: 36px;
  min-height: 36px;
  border-radius: var(--ds-radius-sm);
  cursor: pointer;
}

.view-history .history-select-hit {
  justify-content: center;
  width: 36px;
  height: 36px;
  margin: -4px 0 -4px -8px;
}

.view-history .history-select-all {
  gap: var(--ds-space-2);
  padding-right: var(--ds-space-1);
}

.view-history .history-select-hit:hover,
.view-history .history-select-all:hover {
  background: var(--ds-state-hover);
}

.view-history .history-select-checkbox,
.view-history .history-select-all input {
  width: 20px;
  height: 20px;
  min-height: 0;
  margin: 0;
  accent-color: var(--ds-accent);
  cursor: pointer;
}

.view-history .history-select-all input {
  margin-left: var(--ds-space-2);
}

/* Row actions: primary buttons + “更多”. */
.view-history .history-table .history-actions {
  display: flex;
  flex-wrap: nowrap;
  align-items: center;
  gap: var(--ds-space-2);
}

.view-history .history-panel .history-actions > .history-action,
.view-history .history-panel .history-row-more > .history-action,
.view-history .history-progress-toolbar .history-action {
  min-width: 36px;
  min-height: 36px;
  padding: 0 var(--ds-space-3);
  border-radius: var(--ds-radius-full);
  font-size: var(--ds-text-meta);
  white-space: nowrap;
}

.view-history .history-row-more > .history-more-button {
  background: transparent;
  box-shadow: inset 0 0 0 1px var(--ds-border);
  color: var(--ds-text-muted);
}

.view-history .history-row-more > .history-more-button:hover,
.view-history .history-row-more > .history-more-button[aria-expanded="true"] {
  background: var(--ds-state-hover);
  color: var(--ds-text);
}

.view-history .history-table .progress-track {
  margin-top: var(--ds-space-1);
}

/* ---- Card list (narrow windows, tablets portrait, phones) --------------- */
@media (max-width: 1023px) {
  /* Issue #431：手机上手指按在下载记录上滑不动，只有拖两边才能滚。
     overflow-x:hidden 让表格外壳成了纵向滚动容器（overflow-y 变成 auto），
     这里它的高度跟内容一样、自己滚不动，又带着 .table-wrap 的
     overscroll-behavior:contain，滑动就停在它这里，传不到外层整页。
     恢复滚动传递，滑到头或自己滚不动时交给外层。 */
  .view-history .history-panel .table-wrap {
    overflow-x: hidden;
    overscroll-behavior: auto;
    border: 0;
    background: transparent;
  }

  .view-history .history-panel .history-table {
    display: block;
    width: 100%;
    min-width: 0;
  }

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

  .view-history .history-panel .history-table tbody {
    display: grid;
    gap: var(--ds-space-2);
  }

  .view-history .history-panel .history-table tr {
    display: flex;
    flex-wrap: wrap;
    align-items: center;
    gap: var(--ds-space-1) var(--ds-space-3);
    min-width: 0;
    padding: var(--ds-pad-card);
    border: 1px solid var(--ds-border);
    border-radius: var(--ds-radius-lg);
    background: var(--ds-surface-card);
  }

  .view-history .history-panel .history-table tbody tr:hover {
    background: var(--ds-surface-card-hover);
  }

  .view-history .history-panel .history-table td {
    display: block;
    min-width: 0;
    padding: 0;
    border: 0;
    overflow-wrap: anywhere;
  }

  .view-history .history-panel .history-table td::before {
    content: none;
  }

  .view-history .history-panel .history-table td[data-label="书籍"] {
    flex: 1 1 100%;
    min-width: 0;
    padding: 0;
    border: 0;
  }

  .view-history .history-panel .history-table td[data-label="作者"],
  .view-history .history-panel .history-table td[data-label="格式"],
  .view-history .history-panel .history-table td[data-label="下载时间"] {
    flex: 0 1 auto;
    color: var(--ds-text-muted);
    font-size: var(--ds-text-caption);
    white-space: nowrap;
  }

  .view-history .history-panel .history-table td[data-label="作者"] {
    overflow: hidden;
    max-width: 50%;
    text-overflow: ellipsis;
  }

  .view-history .history-panel .history-table td[data-label="格式"] {
    padding: 0 var(--ds-space-2);
    border-radius: var(--ds-radius-full);
    background: var(--ds-accent-soft);
    color: var(--ds-on-accent-soft);
    font-weight: var(--ds-weight-strong);
  }

  .view-history .history-panel .history-table td[data-label="状态"] {
    flex: 1 1 100%;
    display: flex;
    flex-wrap: wrap;
    align-items: center;
    gap: var(--ds-space-1) var(--ds-space-2);
  }

  .view-history .history-panel .history-table td[data-label="状态"] .progress-track {
    flex: 1 1 100%;
    margin: 0;
  }

  .view-history .history-panel .history-table td[data-label="操作"] {
    flex: 1 1 100%;
    width: auto;
    display: block;
    padding-top: var(--ds-space-1);
  }

  .view-history .history-panel .history-table .history-actions {
    display: flex;
    flex-wrap: wrap;
    width: 100%;
    gap: var(--ds-space-2);
  }

  .view-history .history-panel .history-table .history-actions > * {
    width: auto;
    min-width: 0;
  }

  .view-history .history-panel .history-table .history-actions > .history-action {
    flex: 1 1 0;
    min-width: 88px;
    white-space: nowrap;
  }

  .view-history .history-panel .history-table .history-row-more {
    flex: 0 0 auto;
    margin-left: auto;
  }
}

/* ---- Touch: 44px targets ---------------------------------------------- */
@media (max-width: 720px) {
  .view-history .history-actions-row {
    justify-content: flex-start;
    width: 100%;
  }

  /* 五个按钮等分一行：以前“刷新”独自撑满剩余宽度，三个下拉按钮被挤成窄条。 */
  .view-history .section-head .history-actions-row > button,
  .view-history .section-head .history-actions-row > .menu-anchor {
    flex: 1 1 0;
    min-width: 0;
  }

  .view-history .section-head .history-actions-row > .menu-anchor {
    display: flex;
  }

  .view-history .section-head .history-actions-row > button,
  .view-history .section-head .history-actions-row > .menu-anchor > button {
    width: 100%;
    min-height: var(--ds-control-touch);
    padding: 0 var(--ds-space-3);
    font-size: var(--ds-text-meta);
  }

  .view-history .history-panel .history-actions > .history-action,
  .view-history .history-panel .history-row-more > .history-action,
  .view-history .history-progress-toolbar .history-action,
  .view-history .history-toolbar .text-input,
  .view-history .history-help-summary {
    min-height: var(--ds-control-touch);
  }

  .view-history .history-select-hit,
  .view-history .history-select-all {
    min-width: var(--ds-control-touch);
    min-height: var(--ds-control-touch);
  }

  .view-history .history-select-hit {
    width: var(--ds-control-touch);
    height: var(--ds-control-touch);
    margin: -6px 0 -6px -10px;
  }

  .view-history .history-select-checkbox,
  .view-history .history-select-all input {
    width: 22px;
    height: 22px;
  }

  .view-history .history-actions-row .menu-popup .menu-item,
  .view-history .history-row-more-menu .menu-item {
    min-height: var(--ds-control-touch);
  }

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

body.mobile-shell .view-history .history-panel .history-actions > .history-action,
body.mobile-shell .view-history .history-panel .history-row-more > .history-action,
body.mobile-shell .view-history .history-progress-toolbar .history-action,
body.mobile-shell .view-history .section-head .history-actions-row > button,
body.mobile-shell .view-history .section-head .history-actions-row > .menu-anchor > button,
body.mobile-shell .view-history .history-actions-row .menu-popup .menu-item,
body.mobile-shell .view-history .history-row-more-menu .menu-item,
body.mobile-shell .view-history .history-help-summary {
  min-height: var(--ds-control-touch);
}

body.mobile-shell .view-history .history-select-hit,
body.mobile-shell .view-history .history-select-all {
  min-width: var(--ds-control-touch);
  min-height: var(--ds-control-touch);
}

body.mobile-shell .view-history .history-select-hit {
  width: var(--ds-control-touch);
  height: var(--ds-control-touch);
}

/* ---- Download drawer ---------------------------------------------------
   #jobDrawer is only the layout slot; the visible card is the surface. */
#jobDrawer.job-drawer {
  position: relative;
  min-height: 0;
  max-height: none;
  height: auto;
  padding: 0;
  overflow: visible;
  border: 0;
  border-radius: 0;
  background: transparent;
  box-shadow: none;
  transition: none;
}

#jobDrawer.job-drawer.hidden {
  display: none;
}

#jobDrawer .job-drawer-surface {
  position: relative;
  max-height: min(220px, 40vh);
  padding: var(--ds-space-2) 52px var(--ds-space-2) var(--ds-space-3);
  overflow: auto;
  border: 1px solid var(--ds-border);
  border-radius: var(--ds-radius-lg);
  background: var(--ds-surface-panel);
  color: var(--ds-text);
  overscroll-behavior: contain;
}

#jobDrawer.collapsed .job-drawer-surface {
  max-height: none;
  padding: var(--ds-space-1) 52px var(--ds-space-1) var(--ds-space-1);
  overflow: hidden;
}

#jobDrawer .job-drawer-content[hidden],
#jobDrawer .job-mini[hidden],
#jobDrawer.collapsed .job-drawer-content {
  display: none;
}

#jobDrawer:not(.collapsed) .job-drawer-content {
  visibility: visible;
  opacity: 1;
  pointer-events: auto;
}

#jobDrawer .job-drawer-toggle {
  position: absolute;
  top: 6px;
  right: 8px;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  width: 36px;
  height: 36px;
  min-width: 36px;
  min-height: 36px;
  padding: 0;
  border: 0;
  border-radius: var(--ds-radius-full);
  background: var(--ds-surface-raised);
  color: var(--ds-text-muted);
  font-size: var(--ds-text-body);
  line-height: 1;
  cursor: pointer;
}

#jobDrawer .job-drawer-toggle:hover {
  background: var(--ds-state-hover);
  color: var(--ds-text);
}

/* Collapsed: one-line summary bar with a slim progress bar. */
#jobDrawer .job-mini {
  display: grid;
  grid-template-columns: minmax(0, 1fr) auto;
  grid-template-areas:
    "title meta"
    "track track";
  align-items: center;
  gap: 4px var(--ds-space-3);
  width: 100%;
  min-height: 40px;
  padding: var(--ds-space-1) var(--ds-space-2);
  border: 0;
  border-radius: var(--ds-radius-md);
  background: transparent;
  color: var(--ds-text);
  font: inherit;
  text-align: left;
  cursor: pointer;
  box-shadow: none;
}

#jobDrawer .job-mini:hover {
  background: var(--ds-state-hover);
}

#jobDrawer .job-mini-title {
  grid-area: title;
  min-width: 0;
  overflow: hidden;
  font-size: var(--ds-text-meta);
  font-weight: var(--ds-weight-medium);
  text-overflow: ellipsis;
  white-space: nowrap;
}

#jobDrawer .job-mini-meta {
  grid-area: meta;
  color: var(--ds-text-muted);
  font-size: var(--ds-text-caption);
  white-space: nowrap;
}

#jobDrawer .job-mini-track {
  grid-area: track;
  display: block;
  height: 4px;
  margin: 0;
}

#jobDrawer .job-mini-track .progress-bar {
  display: block;
  height: 100%;
}

/* Expanded content: compact rows. */
#jobDrawer .job-top {
  display: flex;
  align-items: flex-start;
  justify-content: space-between;
  gap: var(--ds-space-3);
}

#jobDrawer .job-title {
  font-size: var(--ds-text-title-sm);
  font-weight: var(--ds-weight-strong);
}

#jobDrawer .job-meta {
  color: var(--ds-text-muted);
  font-size: var(--ds-text-caption);
}

#jobDrawer .job-progress-detail {
  margin: var(--ds-space-1) 0;
}

#jobDrawer .job-foot {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  gap: var(--ds-space-2);
}

#jobDrawer .job-foot .history-action,
#jobDrawer .job-progress-switch .history-action {
  min-width: 36px;
  min-height: 36px;
  padding: 0 var(--ds-space-3);
  font-size: var(--ds-text-meta);
}

/* Phones: the surface floats above the bottom navigation, beside the
   back-to-top button; the slot keeps the summary bar's height at the end of
   the page so the last row of any page is never covered. */
@media (max-width: 720px), (max-width: 980px) and (max-height: 500px) {
  body.mobile-shell #jobDrawer.job-drawer {
    height: 64px;
    min-height: 64px;
    max-height: 64px;
    margin: 0;
  }

  body.mobile-shell #jobDrawer .job-drawer-surface {
    position: fixed;
    left: calc(8px + var(--safe-area-left, 0px));
    right: calc(68px + var(--safe-area-right, 0px));
    bottom: calc(4px + var(--mobile-nav-inset-bottom, max(var(--safe-area-bottom, 0px), var(--android-nav-bar-height, 56px))) + 72px);
    z-index: 35;
    max-height: min(55vh, 360px);
    max-height: min(55dvh, 360px);
    padding-right: 56px;
    background: var(--ds-surface-raised);
    box-shadow: var(--ds-shadow-3);
  }
}

body.mobile-shell #jobDrawer .job-drawer-toggle {
  top: 2px;
  right: 4px;
  width: var(--ds-control-touch);
  height: var(--ds-control-touch);
  min-width: var(--ds-control-touch);
  min-height: var(--ds-control-touch);
}

body.mobile-shell #jobDrawer .job-mini,
body.mobile-shell #jobDrawer .job-foot .history-action,
body.mobile-shell #jobDrawer .job-progress-switch .history-action {
  min-height: var(--ds-control-touch);
}

/* 审查：一条下载记录都没有时，收起用不上的工具栏，只留空状态和「去找书」。 */
.history-panel.history-is-empty .history-actions-row > :not(#historyRefreshButton),
.history-panel.history-is-empty .history-toolbar,
.history-panel.history-is-empty #historySummary,
.history-panel.history-is-empty .history-help {
  display: none;
}

/* ---- Issue #430：下载历史 / 阅读历史 ------------------------------------
   “历史”页分两页：默认下载历史（和以前一样），阅读历史列出本机读过的书。
   .history-downloads-only 只属于下载历史，切到阅读历史时整组隐藏。 */
.view-history .history-mode-switch {
  flex: 0 0 auto;
}

.view-history .history-mode-switch .format-toggle-button {
  min-height: max(36px, var(--ds-control-sm));
  padding: 0 var(--ds-space-4);
}

.view-history .history-panel.history-mode-reading .history-actions-row,
.view-history .history-panel.history-mode-reading .history-downloads-only {
  display: none !important;
}

.view-history .history-panel:not(.history-mode-reading) .reading-history-pane {
  display: none;
}

.view-history .reading-history-pane {
  display: flex;
  flex-direction: column;
  gap: var(--ds-space-3);
}

.view-history .history-panel.history-is-empty .reading-history-toolbar {
  display: flex;
}

.view-history .reading-history-toolbar {
  display: flex;
  align-items: center;
  gap: var(--ds-space-2);
}

.view-history .reading-history-toolbar .text-input {
  flex: 1 1 auto;
  min-width: 0;
}

.view-history .reading-history-toolbar > button {
  flex: 0 0 auto;
  min-height: max(36px, var(--ds-control-sm));
  white-space: nowrap;
}

.view-history .reading-history-hint {
  color: var(--ds-text-subtle);
  font-size: var(--ds-text-caption);
}

.view-history .reading-history-list {
  display: flex;
  flex-direction: column;
  gap: var(--ds-space-2);
}

.view-history .reading-history-item {
  display: flex;
  align-items: center;
  gap: var(--ds-space-2);
  padding: var(--ds-space-2) var(--ds-space-3);
  border-radius: var(--ds-radius-md);
  background: var(--ds-surface-card);
}

.view-history .reading-history-item:hover {
  background: var(--ds-surface-card-hover);
}

.view-history .reading-history-open {
  flex: 1 1 auto;
  min-width: 0;
  min-height: 44px;
  display: flex;
  align-items: center;
  gap: var(--ds-space-3);
  padding: 0;
  border: 0;
  background: transparent;
  color: inherit;
  font: inherit;
  text-align: left;
  cursor: pointer;
}

.view-history .reading-history-open:focus-visible {
  outline: none;
  box-shadow: var(--ds-focus-ring);
  border-radius: var(--ds-radius-sm);
}

.view-history .reading-history-cover {
  flex: 0 0 auto;
  width: 48px;
  height: 64px;
  display: flex;
  align-items: center;
  justify-content: center;
  overflow: hidden;
  border-radius: var(--ds-radius-sm);
  background: var(--ds-surface-field);
}

.view-history .reading-history-cover img {
  display: block;
  width: 100%;
  height: 100%;
  object-fit: cover;
}

.view-history .reading-history-cover .cover-fallback {
  color: var(--ds-text-muted);
  font-size: var(--ds-text-title-sm);
}

.view-history .reading-history-text {
  min-width: 0;
  display: flex;
  flex-direction: column;
  gap: var(--ds-space-1);
}

.view-history .reading-history-title,
.view-history .reading-history-position,
.view-history .reading-history-meta {
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.view-history .reading-history-title {
  color: var(--ds-text);
  font-weight: var(--ds-weight-strong);
}

.view-history .reading-history-position {
  color: var(--ds-text-muted);
  font-size: var(--ds-text-meta);
}

.view-history .reading-history-meta {
  color: var(--ds-text-subtle);
  font-size: var(--ds-text-caption);
}

.view-history .reading-history-remove {
  flex: 0 0 auto;
  min-height: max(36px, var(--ds-control-sm));
  padding: 0 var(--ds-space-3);
  font-size: var(--ds-text-meta);
}

body.mobile-shell .view-history .reading-history-remove,
body.mobile-shell .view-history .reading-history-toolbar > button,
body.mobile-shell .view-history .history-mode-switch .format-toggle-button {
  min-height: 44px;
}

@media (max-width: 720px) {
  .view-history .reading-history-remove,
  .view-history .reading-history-toolbar > button,
  .view-history .history-mode-switch .format-toggle-button {
    min-height: 44px;
  }
}
