:root {
    color-scheme: light;
    --bg: #f5f2ea;
    --surface: rgba(255, 255, 255, 0.96);
    --surface-strong: rgba(255, 255, 255, 0.98);
    --surface-soft: rgba(255, 255, 255, 0.84);
    --text: #1f1b16;
    --muted: #7d7469;
    --line: rgba(31, 27, 22, 0.08);
    --shadow: 0 20px 40px rgba(26, 20, 12, 0.08);
    --accent: #1f1b16;
    --progress: #1f1b16;
    --page-padding-x: 30px;
    --page-padding-top: 84px;
    --page-padding-bottom: 112px;
    --page-gap: 28px;
    --font-scale: 1;
    --panel-blur: 18px;
    --page-width: 100vw;
    --page-height: 100vh;
    --image-max-height: 360px;
    --font-family-sans: -apple-system, BlinkMacSystemFont, "SF Pro Text", "SF Pro Display", "Helvetica Neue", Arial, sans-serif;
    --font-family-serif: "Iowan Old Style", "Palatino Linotype", "Book Antiqua", Palatino, Georgia, serif;
    --reader-font-family: var(--font-family-sans);
}

html[data-theme="dark"] {
    color-scheme: dark;
    --bg: #171411;
    --surface: rgba(25, 22, 18, 0.94);
    --surface-strong: rgba(32, 28, 23, 0.98);
    --surface-soft: rgba(32, 28, 23, 0.88);
    --text: #f4efe7;
    --muted: #b8aea2;
    --line: rgba(244, 239, 231, 0.08);
    --shadow: 0 20px 40px rgba(0, 0, 0, 0.28);
    --accent: #f4efe7;
    --progress: #f4efe7;
}

* {
    box-sizing: border-box;
    -webkit-tap-highlight-color: transparent;
}

html,
body {
    width: 100%;
    height: 100%;
    margin: 0;
    overflow: hidden;
    background: var(--bg);
    color: var(--text);
    font-family: var(--reader-font-family);
}

button {
    font: inherit;
}

.reader-app {
    position: relative;
    width: 100%;
    height: 100%;
    background: var(--bg);
}

.reader-stage {
    position: absolute;
    inset: 0;
    overflow: hidden;
    background: var(--bg);
}

.reader-track {
    position: absolute;
    inset: 0;
    overflow-x: hidden;
    overflow-y: hidden;
    touch-action: pan-y;
    scrollbar-width: none;
    scroll-behavior: smooth;
}

.reader-track::-webkit-scrollbar {
    display: none;
}

.reader-flow {
    display: flex;
    align-items: stretch;
    width: max-content;
    min-width: 100%;
    height: 100%;
}

.reader-page {
    flex: 0 0 var(--page-width);
    width: var(--page-width);
    height: var(--page-height);
    padding: var(--page-padding-top) var(--page-padding-x) var(--page-padding-bottom);
    overflow: hidden;
}

.reader-page-body {
    width: 100%;
    height: 100%;
    overflow: hidden;
}

.reader-page-measure {
    position: fixed;
    left: -10000px;
    top: 0;
    width: var(--page-width);
    height: var(--page-height);
    pointer-events: none;
    visibility: hidden;
    opacity: 0;
    z-index: -1;
}

.chapter,
.book-unit,
.book-block {
    width: 100%;
}

.chapter[data-accent="blue"] {
    --chapter-accent: #2f80ed;
}

.chapter[data-accent="green"] {
    --chapter-accent: #27ae60;
}

.chapter[data-accent="red"] {
    --chapter-accent: #eb5757;
}

.chapter[data-accent="purple"] {
    --chapter-accent: #9b51e0;
}

.chapter + .chapter {
    margin-left: 0;
}

.book-block {
    margin: 0 0 16px;
    max-width: 100%;
}

.book-block.heading-1 {
    margin-top: 0;
    margin-bottom: 26px;
    font-size: calc(2.1rem * var(--font-scale));
    line-height: 1.12;
    font-weight: 700;
    letter-spacing: 0;
}

.book-block.heading-2 {
    margin-top: 34px;
    margin-bottom: 18px;
    font-size: calc(1.02rem * var(--font-scale));
    line-height: 1.34;
    font-weight: 700;
}

.book-block.heading-3 {
    margin-top: 26px;
    margin-bottom: 14px;
    font-size: calc(0.96rem * var(--font-scale));
    line-height: 1.34;
    font-weight: 600;
    color: var(--muted);
}

html[data-font-family="sans"] .book-block.heading-1,
html[data-font-family="sans"] .book-block.heading-2 {
    font-family: var(--font-family-sans);
}

html[data-font-family="serif"] .book-block.heading-1,
html[data-font-family="serif"] .book-block.heading-2,
html[data-font-family="serif"] .book-block.paragraph,
html[data-font-family="serif"] .book-block.list li {
    font-family: var(--font-family-serif);
}

.book-block.paragraph,
.book-block.list li {
    font-size: calc(1.1rem * var(--font-scale));
    line-height: 1.72;
}

.book-block.list {
    margin-top: 6px;
    margin-bottom: 22px;
    padding-left: 1.2rem;
}

.book-block.list.list-fragment {
    margin-top: 0;
    margin-bottom: 8px;
}

.book-block.paragraph.fragment-continued,
.book-block.list.fragment-continued {
    margin-bottom: 0;
}

.book-block.paragraph.continued-from-previous,
.book-block.list.continued-from-previous {
    margin-top: 0;
}

.book-block.list.continued-list-item {
    list-style: none;
    padding-left: 1.2rem;
}

.book-block.list li + li {
    margin-top: 8px;
}

.book-block.cover-title {
    font-size: calc(2.4rem * var(--font-scale));
    margin-top: 8vh;
}

.book-block.cover-subtitle {
    margin-top: 10px;
    color: var(--muted);
}

.book-block.chapter-title {
    position: relative;
    padding-top: 20px;
}

.book-block.chapter-title::before {
    content: "";
    position: absolute;
    top: 0;
    left: 0;
    width: 68px;
    height: 3px;
    border-radius: 999px;
    background: var(--chapter-accent, var(--accent));
}

.book-block img {
    display: block;
    width: 100%;
    height: auto;
    max-height: var(--image-max-height);
    object-fit: contain;
}

.image-block {
    margin: 22px 0 26px;
    padding: 0;
    border: 0;
    background: transparent;
}

.image-button {
    display: block;
    width: 100%;
    padding: 0;
    border: 0;
    background: transparent;
    cursor: pointer;
}

.image-card {
    display: flex;
    align-items: center;
    justify-content: center;
    overflow: visible;
    border-radius: 18px;
}

.image-inline-caption {
    margin-top: 8px;
    color: var(--muted);
    font-size: 0.78rem;
    line-height: 1.35;
    text-align: center;
}

.book-unit {
    width: 100%;
}

.book-unit.keep-together {
    break-inside: avoid;
}

.reader-panel {
    position: absolute;
    left: 0;
    right: 0;
    z-index: 5;
    display: flex;
    align-items: center;
    gap: 10px;
    padding: max(10px, env(safe-area-inset-top)) 14px 10px;
    background: linear-gradient(180deg, var(--surface-strong), transparent);
    opacity: 0;
    pointer-events: none;
    transition: opacity 180ms ease;
}

.reader-panel.is-visible {
    opacity: 1;
    pointer-events: auto;
}

.reader-panel-top {
    top: 0;
}

.reader-panel-bottom {
    bottom: 0;
    align-items: flex-end;
    min-height: 82px;
    padding: 8px 14px max(10px, env(safe-area-inset-bottom));
    background: linear-gradient(0deg, var(--surface-strong), transparent);
}

.panel-title {
    flex: 1;
    min-width: 0;
}

.panel-book {
    color: var(--muted);
    font-size: 0.75rem;
    line-height: 1.25;
}

.panel-chapter {
    margin-top: 2px;
    font-size: 0.98rem;
    line-height: 1.2;
    font-weight: 600;
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
}

.panel-actions {
    display: flex;
    gap: 8px;
}

.panel-button,
.bookmark-action,
.close-button,
.font-controls button,
.theme-buttons button,
.bookmark-card button,
.toc-item button {
    border: 1px solid var(--line);
    background: var(--surface-soft);
    color: var(--text);
    box-shadow: var(--shadow);
}

.panel-button,
.close-button {
    width: 40px;
    height: 40px;
    border-radius: 999px;
    font-size: 1rem;
    display: inline-flex;
    align-items: center;
    justify-content: center;
    cursor: pointer;
}

.bookmark-action {
    flex: none;
    height: 38px;
    padding: 0 14px;
    border-radius: 999px;
    cursor: pointer;
    white-space: nowrap;
}

.progress-wrap {
    flex: 1;
    min-width: 0;
}

.progress-meta {
    display: flex;
    justify-content: space-between;
    gap: 12px;
    margin-bottom: 6px;
    font-size: 0.78rem;
    color: var(--muted);
}

.progress-bar {
    height: 4px;
    border-radius: 999px;
    background: rgba(127, 116, 105, 0.18);
    overflow: hidden;
}

.progress-value {
    height: 100%;
    width: 0;
    border-radius: inherit;
    background: var(--progress);
}

.tap-zone {
    position: absolute;
    top: 0;
    bottom: 0;
    z-index: 3;
    pointer-events: none;
    border: 0;
    background: transparent;
    padding: 0;
    margin: 0;
}

.tap-zone-left {
    left: 0;
    width: 26%;
}

.tap-zone-center {
    left: 26%;
    width: 48%;
}

.tap-zone-right {
    right: 0;
    width: 26%;
}

.overlay {
    position: fixed;
    inset: 0;
    z-index: 20;
    display: flex;
    align-items: flex-end;
    justify-content: center;
    padding: 16px;
    background: rgba(15, 13, 11, 0.36);
    backdrop-filter: blur(6px);
}

.overlay[hidden] {
    display: none;
}

.sheet,
.image-sheet {
    width: min(720px, 100%);
    max-height: min(82vh, 900px);
    background: var(--surface-strong);
    color: var(--text);
    border-radius: 24px;
    box-shadow: var(--shadow);
    border: 1px solid var(--line);
    overflow: hidden;
}

.sheet-head {
    display: flex;
    align-items: flex-start;
    justify-content: space-between;
    gap: 12px;
    padding: 18px 18px 14px;
    border-bottom: 1px solid var(--line);
}

.sheet-eyebrow {
    color: var(--muted);
    font-size: 0.76rem;
    line-height: 1.2;
}

.sheet-head h2 {
    margin: 4px 0 0;
    font-size: 1.2rem;
    line-height: 1.2;
}

.sheet-body {
    overflow: auto;
    max-height: calc(82vh - 82px);
    padding: 8px 18px 18px;
}

.toc-list,
.bookmarks-list {
    display: grid;
    gap: 10px;
}

.toc-item .toc-main,
.toc-subitem button,
.bookmark-card button {
    width: 100%;
    padding: 14px 16px;
    border-radius: 18px;
    text-align: left;
    cursor: pointer;
}

.toc-item {
    display: grid;
    gap: 8px;
}

.toc-sublist {
    display: grid;
    gap: 8px;
    padding-left: 14px;
}

.toc-subitem button {
    padding: 10px 12px;
    border-radius: 14px;
    background: transparent;
    box-shadow: none;
}

.toc-label {
    display: block;
    font-size: 0.96rem;
    font-weight: 600;
    line-height: 1.3;
}

.toc-chapter {
    display: block;
    margin-top: 4px;
    color: var(--muted);
    font-size: 0.82rem;
    line-height: 1.3;
}

.toc-subitem .toc-label {
    font-size: 0.86rem;
    font-weight: 500;
}

.settings-body {
    display: grid;
    gap: 18px;
}

.settings-section {
    display: grid;
    gap: 12px;
}

.settings-label {
    font-size: 0.92rem;
    font-weight: 600;
}

.font-controls {
    display: inline-flex;
    align-items: center;
    gap: 10px;
}

.font-controls button {
    width: 44px;
    height: 44px;
    border-radius: 14px;
    cursor: pointer;
    font-size: 1.4rem;
}

#font-size-label {
    min-width: 56px;
    text-align: center;
    font-size: 0.95rem;
}

.theme-buttons {
    display: grid;
    gap: 8px;
}

.theme-buttons button {
    width: 100%;
    min-height: 48px;
    padding: 0 14px;
    border-radius: 16px;
    text-align: left;
    cursor: pointer;
}

.theme-buttons button.is-active {
    outline: 2px solid rgba(47, 128, 237, 0.24);
}

.bookmark-card-title {
    display: block;
    font-size: 0.96rem;
    font-weight: 600;
    line-height: 1.3;
}

.bookmark-card-meta {
    display: block;
    margin-top: 4px;
    color: var(--muted);
    font-size: 0.82rem;
    line-height: 1.35;
}

.bookmarks-empty {
    padding: 16px;
    color: var(--muted);
    font-size: 0.95rem;
    line-height: 1.5;
}

.image-overlay {
    align-items: center;
}

.image-sheet {
    position: relative;
    width: min(960px, 100%);
    display: grid;
    grid-template-rows: minmax(0, 1fr) auto auto;
    gap: 0;
    padding: 56px 18px 18px;
    border-radius: 28px;
    overflow: visible;
}

.image-stage {
    position: relative;
    display: flex;
    align-items: center;
    justify-content: center;
    overflow: hidden;
    border-radius: 20px;
    touch-action: none;
    min-height: 0;
    z-index: 1;
    background: var(--surface);
}

.image-sheet img {
    display: block;
    width: 100%;
    max-height: min(72vh, calc(100vh - 240px));
    object-fit: contain;
    border-radius: 20px;
    transform-origin: center center;
    transition: transform 140ms ease;
    touch-action: none;
    user-select: none;
    -webkit-user-drag: none;
    cursor: grab;
}

.image-sheet img.is-zoomed {
    cursor: grab;
}

.image-sheet img.is-dragging {
    cursor: grabbing;
}

.image-toolbar {
    display: flex;
    justify-content: center;
    gap: 10px;
    margin-top: 14px;
    position: relative;
    z-index: 4;
}

.image-caption {
    margin-top: 10px;
    color: var(--muted);
    font-size: 0.9rem;
    line-height: 1.4;
    text-align: center;
    position: relative;
    z-index: 4;
}

.image-close {
    position: absolute;
    top: 14px;
    right: 14px;
    z-index: 5;
    background: var(--surface-strong);
    box-shadow: 0 10px 24px rgba(0, 0, 0, 0.18);
}

strong {
    font-weight: 700;
}

em {
    font-style: italic;
}

@media (max-width: 720px) {
    :root {
        --page-padding-x: 24px;
        --page-padding-top: 84px;
        --page-padding-bottom: 112px;
        --page-gap: 22px;
    }

    .book-block.heading-1 {
        font-size: calc(1.85rem * var(--font-scale));
    }

    .book-block.heading-2 {
        font-size: calc(1.08rem * var(--font-scale));
    }

    .book-block.paragraph,
    .book-block.list li {
        font-size: calc(1.03rem * var(--font-scale));
    }

    .book-block img {
        max-height: var(--image-max-height);
    }

}
