* {
    box-sizing: border-box;
}

:root {
    color-scheme: dark;
    --ink: #c9e8cf;
    --line: #244d30;
}

body {
    margin: 0;
    background: radial-gradient(ellipse at 50% 40%,#102419,#050806 75%);
    color: var(--ink);
    font: 16px Georgia,serif;
    min-height: 100dvh;
    display: flex;
    flex-direction: column;
}

.reader {
    height: 100dvh;
    min-height: 0;
    overflow: hidden;
}

.reader header, .reader footer {
    flex: 0 0 auto;
}

.reader .heading {
    flex: 0 0 auto;
}

.reader .brand {
    color: #79ff8d;
}

button,input {
    font: inherit;
}

button {
    color: inherit;
    background: transparent;
    border: 1px solid transparent;
    cursor: pointer;
    padding: 8px 12px;
    border-radius: 4px;
}

button:hover,button:focus-visible {
    background: #142c1a;
    border-color: #4fa865;
}

button:disabled {
    opacity: .3;
    cursor: default;
}

header {
    height: 65px;
    display: flex;
    align-items: center;
    gap: 35px;
    padding: 0 3%;
    border-bottom: 1px solid var(--line);
    background: #090f0b;
}

.brand {
    color: inherit;
    text-decoration: none;
    letter-spacing: .22em;
    font-size: 23px;
    white-space: nowrap;
}

nav {
    display: flex;
    gap: 18px;
}

nav .active {
    border-bottom: 1px solid #79ff8d;
    border-radius: 0;
}

.tag {
    margin-left: auto;
    font: 10px Arial,sans-serif;
    letter-spacing: .16em;
    color: #82a68b;
}

main {
    flex: 1;
    display: flex;
    flex-direction: column;
    min-height: 0;
    padding: 12px 16px 18px;
}

.heading {
    display: flex;
    align-items: center;
    justify-content: center;
    gap: 24px;
    margin: 0 0 12px;
}

.heading span {
    width: 12%;
    height: 1px;
    background: var(--line);
}

h1 {
    font-size: clamp(19px,2.3vw,32px);
    font-weight: 400;
    margin: 8px 0;
    text-align: center;
}

.workspace {
    display: grid;
    grid-template-columns: clamp(105px,13vw,190px) minmax(0,1fr) clamp(105px,13vw,190px);
    gap: 14px;
    flex: 1;
    min-height: 0;
}

aside {
    border: 1px solid var(--line);
    border-radius: 5px;
    background: #090f0baa;
    padding: 10px;
    overflow: hidden;
    display: flex;
    flex-direction: column;
}

aside h2 {
    font-weight: 400;
    font-size: 15px;
    margin: 3px 0 10px;
    padding-bottom: 10px;
    border-bottom: 1px solid var(--line);
}

.thumbs {
    display: flex;
    flex-direction: column;
    gap: 10px;
    overflow: auto;
    scrollbar-width: thin;
    min-height: 0;
    flex: 1;
}

.thumb {
    display: block;
    width: 100%;
    padding: 4px;
    text-align: center;
    color: #a7c8ad;
    font-size: 13px;
}

.mini {
    display: flex;
    justify-content: center;
    aspect-ratio: 1.48;
    overflow: hidden;
    margin-bottom: 5px;
    filter: drop-shadow(0 3px 4px #000);
}

.mini img {
    width: 50%;
    height: 100%;
    object-fit: contain;
    background: #eee9dd;
}

.stage {
    display: flex;
    align-items: center;
    justify-content: center;
    overflow: auto;
    min-height: 0;
    height: 100%;
    perspective: 1800px;
    padding: 10px 8px;
}

.book {
    position: relative;
    display: flex;
    width: 100%;
    max-width: calc((100dvh - 190px) * 1.48);
    aspect-ratio: 1.48;
    filter: drop-shadow(0 15px 13px #0009);
    touch-action: pan-y;
    isolation: isolate;
    transform-style: preserve-3d;
}

.book:before {
    content: "";
    position: absolute;
    inset: 4px -5px -5px;
    background: repeating-linear-gradient(90deg,#d1c8b6 0 1px,#a69b88 2px 3px);
    border: 1px solid #766b5a;
    border-radius: 5px;
    z-index: -1;
}

.leaf {
    width: 50%;
    height: 100%;
    background: #eeeae1;
    position: relative;
    overflow: hidden;
}

.leaf img,.turn img {
    width: 100%;
    height: 100%;
    object-fit: contain;
    display: block;
    background: #eeeae1;
    pointer-events: none;
    user-select: none;
}

.left {
    border-radius: 2px 0 0 2px;
}

.right {
    border-radius: 0 2px 2px 0;
}

.left:after,.right:after {
    content: "";
    position: absolute;
    inset: 0;
    pointer-events: none;
}

.left:after {
    background: linear-gradient(90deg,transparent 92%,#0002 99%,#0004);
}

.right:after {
    background: linear-gradient(90deg,#0003,transparent 7%);
}

.spine {
    position: absolute;
    left: 50%;
    height: 100%;
    width: 1px;
    background: #746b5d;
    z-index: 2;
    pointer-events: none;
}

.turn {
    position: absolute;
    top: 0;
    width: 50%;
    height: 100%;
    z-index: 5;
    transform-style: preserve-3d;
    pointer-events: none;
}

.turn[hidden] {
    display: none;
}

.face {
    position: absolute;
    inset: 0;
    backface-visibility: hidden;
    overflow: hidden;
}

.face.back {
    transform: rotateY(180deg);
}

.face:after {
    content: "";
    position: absolute;
    inset: 0;
    background: linear-gradient(90deg,#0003,transparent 35%,#fff2 70%,#0002);
    pointer-events: none;
}

.turn.forward {
    left: 50%;
    transform-origin: left center;
}

.turn.backward {
    left: 0;
    transform-origin: right center;
}

footer {
    display: flex;
    align-items: center;
    justify-content: space-between;
    flex-wrap: wrap;
    gap: 12px;
    min-height: 62px;
    padding: 8px 22px;
    border-top: 1px solid var(--line);
    background: #090f0b;
}

.navigation,.tools {
    display: flex;
    align-items: center;
    gap: 6px;
}

.navigation button,.tools button {
    border-color: var(--line);
}

#counter {
    min-width: 102px;
    text-align: center;
    font-variant-numeric: tabular-nums;
}

#mobilePages {
    display: none;
}

dialog {
    width: min(760px,94vw);
    max-height: 88dvh;
    overflow: auto;
    color: var(--ink);
    background: #0d1710;
    border: 1px solid #356b42;
    border-radius: 8px;
    padding: 24px;
    box-shadow: 0 20px 80px #000;
}

dialog::backdrop {
    background: #000a;
}

.dialogHead {
    display: flex;
    justify-content: space-between;
    align-items: center;
}

.dialogHead h2 {
    font-weight: 400;
}

label {
    display: block;
    margin: 12px 0;
}

input {
    display: block;
    width: 100%;
    margin: 8px 0;
    padding: 10px;
    background: #060b07;
    border: 1px solid #555;
    color: inherit;
}

small {
    display: block;
    color: #aaa;
    font: 12px Arial,sans-serif;
}

.sources {
    display: grid;
    gap: 4px;
}

#draftPages {
    display: flex;
    flex-wrap: wrap;
    gap: 8px;
    margin: 10px 0;
}

.draft {
    width: 100px;
    border: 1px solid var(--line);
    padding: 5px;
}

.draft img {
    width: 100%;
    aspect-ratio: .74;
    object-fit: contain;
}

.draft button {
    padding: 3px 6px;
}

#publish,#newPub {
    background: #79ff8d;
    color: #0d1710;
}

.pubRow {
    display: flex;
    justify-content: space-between;
    align-items: center;
    border-bottom: 1px solid var(--line);
    padding: 10px 0;
}

.allPages {
    display: grid;
    grid-template-columns: repeat(auto-fill,minmax(120px,1fr));
    gap: 8px;
}

@media (max-width:760px) {
    header {
        height: auto;
        min-height: 60px;
        flex-wrap: wrap;
        gap: 6px;
        padding: 12px;
    }

    .brand {
        font-size: 19px;
    }

    nav {
        gap: 5px;
    }

    nav button {
        padding: 6px;
        font-size: 13px;
    }

    .tag,aside,.publication {
        display: none;
    }

    .workspace {
        grid-template-columns: 1fr;
    }

    .stage {
        padding: 8px 0;
    }

    main {
        padding: 8px;
        justify-content: center;
    }

    footer {
        flex-wrap: wrap;
        justify-content: center;
        padding: 8px;
        gap: 6px;
    }

    .tools button,.navigation button {
        padding: 6px 9px;
    }

    #mobilePages {
        display: block;
        font-size: 13px;
    }

    .heading span {
        display: none;
    }

    .heading {
        margin-bottom: 4px;
    }

}

@media (prefers-reduced-motion:reduce) {
    * {
        scroll-behavior: auto !important;
    }

}

/* Explicit pixel dimensions keep zoom independent of flex shrink and fit limits. */
.stage {
    display: flex;
    min-width: 0;
    overflow: auto;
    align-items: flex-start;
    justify-content: flex-start;
    scrollbar-width: none;
}
.bookSpace {
    flex: 0 0 auto;
    margin: auto;
    padding-bottom: 8px;
}
.book {
    width: 100%;
    max-width: none;
    flex-shrink: 0;
}

/* Flexible sheet is rendered above both static pages, without clipping the curl. */
.turn.flexible {
    inset: 0;
    width: 100%;
    height: 100%;
    transform: none;
    overflow: visible;
}
.paper-canvas {
    position: absolute;
    pointer-events: none;
}

/* Avoid re-filtering the entire animated canvas on every frame. */
.book {
    filter: none;
    box-shadow: 0 15px 13px #0009;
}

@media (max-height: 500px) {
    .reader header { height: auto; min-height: 38px; padding-block: 3px; }
    .reader main { padding: 3px 8px; }
    .reader .heading { margin-bottom: 2px; }
    .reader h1 { font-size: 16px; margin: 3px 0; }
    .reader footer { min-height: 36px; padding: 3px 8px; gap: 4px; }
    .reader footer button { padding: 4px 7px; }
    .reader .stage { padding: 4px; }
}

/* Keep the animation inside a fixed layout box, so it never changes scroll geometry. */
.stage { position: relative; overflow-anchor: none; }
.turn.flexible { overflow: hidden; transform-style: flat; contain: paint; }
.mini span { width: 50%; background: #eeeae1; }
.loading { position: absolute; z-index: 20; inset: 0; display: flex;
    flex-direction: column; align-items: center; justify-content: center;
    gap: 14px; background: #090f0bf2; color: var(--ink); }
.loading[hidden] { display: none; }
.loading progress { width: min(75%, 340px); height: 8px; accent-color: #79ff8d; }

/* The closed book occupies one leaf, centered within the stable spread geometry. */
.book { transform: translateX(var(--cover-offset, 0%)); }
.book.closed-cover, .book.cover-turn { box-shadow: none; }
.book.closed-cover::before, .book.cover-turn::before { left: calc(50% + 1px); }
.book.closed-cover .spine, .book.cover-turn .spine { visibility: hidden; }
.book.closed-cover .left:empty, .book.cover-turn .left:empty { visibility: hidden; }
.book.closed-cover:not(.cover-turn) .right { box-shadow: 0 15px 13px #0009; }
.book.closed-cover:not(.cover-turn) .right::after { background: none; }
.mini.cover-mini { justify-content: center; }
.mini.cover-mini img { width: 50%; }

/* Compact publication selector in the existing navigation. */
#library { max-width: min(300px, 36vw); min-width: 0; color: inherit; background: #090f0b; border: 1px solid var(--line); border-radius: 3px; padding: 7px; font: inherit; }
#library option { background: #090f0b; color: #c9e8cf; }
@media(max-width:760px) { #library { max-width: 44vw; font-size: 12px; } }
