/* 移动端 / 平板阅读页专项优化 */

@media (max-width: 1024px) {
  body.page-reader {
    padding-left: 0 !important;
    padding-right: 0 !important;
    overflow-x: hidden;
  }

  .page-reader {
    --content-width: 100%;
  }

  .page-reader .site-header {
    display: none;
  }

  .site-main--reader {
    max-width: none;
    width: 100%;
    margin: 0;
    padding: 0;
    min-height: 100dvh;
  }

  .reader-layout {
    display: block;
    width: 100%;
    margin: 0 !important;
    min-height: 100dvh;
  }

  /* 目录脱离文档流，不占左侧宽度 */
  .reader-toc {
    position: fixed;
    top: 0;
    left: 0;
    width: min(300px, 88vw);
    height: 100dvh;
    z-index: 202;
    transform: translateX(-100%);
    transition: transform var(--transition);
    box-shadow: 8px 0 32px var(--color-shadow);
    padding-top: max(var(--space-md), env(safe-area-inset-top));
    padding-bottom: env(safe-area-inset-bottom);
    -webkit-overflow-scrolling: touch;
  }

  .reader-toc.open {
    transform: translateX(0);
  }

  .toc-close {
    display: inline-flex;
  }

  .reader-main {
    width: 100%;
    margin: 0;
    background: var(--color-bg);
  }

  /* 阅读顶栏 */
  .reader-topbar {
    position: sticky;
    top: 0;
    z-index: 110;
    display: grid;
    grid-template-columns: 44px 1fr auto;
    align-items: center;
    gap: var(--space-sm);
    width: 100%;
    padding: 8px 12px;
    padding-top: max(8px, env(safe-area-inset-top));
    min-height: 48px;
    background: var(--header-bg);
    backdrop-filter: blur(12px);
    border-bottom: 1px solid var(--color-border);
  }

  .reader-back {
    display: inline-flex;
    align-items: center;
    justify-content: center;
    width: 44px;
    height: 44px;
    border-radius: var(--radius-sm);
    color: var(--color-text-muted);
    flex-shrink: 0;
  }

  .reader-back:active {
    background: var(--color-primary-soft);
  }

  .reader-topbar-center {
    min-width: 0;
    text-align: center;
    line-height: 1.3;
  }

  .reader-novel-name {
    display: block;
    font-size: 0.72rem;
    color: var(--color-text-muted);
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
  }

  .reader-chapter-name {
    display: block;
    font-size: 0.9rem;
    font-weight: 600;
    color: var(--color-text);
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
  }

  .reader-topbar-actions {
    display: flex;
    align-items: center;
    gap: 2px;
    flex-shrink: 0;
  }

  .reader-topbar-actions .icon-btn {
    width: 44px;
    height: 44px;
  }

  /* 正文：贴左全宽，禁止 auto 居中偏移 */
  .reader-content {
    box-sizing: border-box;
    width: 100% !important;
    max-width: none !important;
    margin: 0 !important;
    font-size: max(var(--font-size), 17px);
    line-height: max(var(--line-height), 1.75);
    padding: 18px 16px;
    padding-bottom: calc(84px + env(safe-area-inset-bottom));
    text-align: left;
  }

  .chapter-heading {
    font-size: 1.25rem;
    margin: 0 0 1.25rem;
    padding-bottom: 0.75rem;
    border-bottom: 1px solid var(--color-border);
    text-align: left;
    line-height: 1.45;
  }

  .reader-content.prose > h1:not(.chapter-heading) {
    display: none;
  }

  .reader-content.prose p {
    margin: 0 0 1em;
    text-indent: 2em;
    text-align: left;
    word-break: break-word;
    overflow-wrap: break-word;
    letter-spacing: 0.01em;
  }

  .reader-content.prose h2,
  .reader-content.prose h3,
  .reader-content.prose h4 {
    text-align: left;
    text-indent: 0;
  }

  .reader-content.prose h2 {
    font-size: 1.12em;
    margin: 1.4em 0 0.6em;
  }

  .reader-content.prose h3 {
    font-size: 1.05em;
    margin: 1.2em 0 0.5em;
  }

  .reader-content.prose blockquote {
    margin: 1em 0;
    padding: 0.75em 1em;
    font-size: 0.95em;
  }

  .reader-content.prose ul,
  .reader-content.prose ol {
    padding-left: 1.5em;
  }

  .reader-content.prose pre {
    margin: 1em 0;
    padding: 0.75em;
    font-size: 0.8em;
    -webkit-overflow-scrolling: touch;
  }

  .reader-content.prose table {
    display: block;
    width: 100%;
    overflow-x: auto;
    -webkit-overflow-scrolling: touch;
    font-size: 0.84em;
  }

  .reader-content.prose th,
  .reader-content.prose td {
    padding: 6px 10px;
    white-space: nowrap;
  }

  .chapter-nav {
    display: none;
  }

  /* 底部章节栏 */
  .mobile-chapter-bar {
    display: flex;
    position: fixed;
    left: 0;
    right: 0;
    bottom: 0;
    z-index: 120;
    height: calc(60px + env(safe-area-inset-bottom));
    padding: 0;
    padding-bottom: env(safe-area-inset-bottom);
    background: var(--color-surface);
    border-top: 1px solid var(--color-border);
    box-shadow: 0 -2px 16px var(--color-shadow);
  }

  .mobile-nav-btn {
    flex: 1;
    min-width: 0;
    height: 60px;
    padding: 6px 8px;
    font-size: 0.82rem;
    background: var(--color-surface);
    border-radius: 0;
    gap: 4px;
  }

  .mobile-nav-btn.prev {
    justify-content: flex-start;
    text-align: left;
  }

  .mobile-nav-btn.next {
    justify-content: flex-end;
    text-align: right;
  }

  .mobile-nav-text {
    display: flex;
    flex-direction: column;
    gap: 1px;
    min-width: 0;
    flex: 1;
    line-height: 1.25;
  }

  .mobile-nav-text small {
    font-size: 0.68rem;
    color: var(--color-text-muted);
    flex-shrink: 0;
  }

  .mobile-nav-title {
    font-size: 0.78rem;
    font-weight: 500;
    color: var(--color-text);
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
  }

  .mobile-nav-toc {
    flex: 0 0 56px;
    flex-direction: column;
    gap: 2px;
    color: var(--color-primary);
    font-weight: 600;
    font-size: 0.75rem;
    border-left: 1px solid var(--color-border);
    border-right: 1px solid var(--color-border);
  }

  .mobile-nav-btn.disabled {
    opacity: 0.35;
    pointer-events: none;
  }

  .mobile-nav-btn.disabled .mobile-nav-title {
    color: var(--color-text-muted);
  }

  .toc-list a {
    padding: 14px var(--space-md);
    font-size: 0.95rem;
    min-height: 48px;
    align-items: center;
  }

  .toc-list .ch-index {
    width: 32px;
    flex-shrink: 0;
  }
}

/* 桌面保留原顶栏，隐藏阅读页内嵌工具栏 */
@media (min-width: 1025px) {
  .reader-topbar-actions {
    display: none;
  }

  .reader-topbar {
    display: flex;
    align-items: center;
    gap: var(--space-md);
  }

  .reader-topbar-center {
    flex: 1;
    text-align: left;
  }

  .reader-novel-name {
    display: inline;
    font-size: 0.85rem;
  }

  .reader-novel-name::after {
    content: " / ";
    opacity: 0.5;
  }

  .reader-chapter-name {
    display: inline;
    font-size: 0.85rem;
    font-weight: 500;
  }

  .reader-back {
    width: auto;
    height: auto;
    padding: 6px;
  }

  .mobile-chapter-bar {
    display: none !important;
  }

  #toggle-toc {
    display: none;
  }
}

@media (max-width: 1024px) and (orientation: landscape) {
  .reader-content {
    padding-bottom: calc(72px + env(safe-area-inset-bottom));
  }

  .mobile-chapter-bar {
    height: calc(52px + env(safe-area-inset-bottom));
  }

  .mobile-nav-btn {
    height: 52px;
    padding: 4px 6px;
  }

  .mobile-nav-title {
    font-size: 0.72rem;
  }
}
