@import url('https://fonts.googleapis.com/css2?family=DM+Sans:wght@400;500;600;700&family=Playfair+Display:ital,wght@0,500;0,600;1,500;1,600&display=swap');

:root {
    color-scheme: light;
    --ink: #17251f;
    --muted: #738078;
    --paper: #f5f3ee;
    --green: #355b45;
    --line: #dedfd8;
}

* { box-sizing: border-box; }

body {
    min-width: 320px;
    min-height: 100vh;
    margin: 0;
    color: var(--ink);
    background: var(--paper);
    font-family: 'DM Sans', sans-serif;
}

a { color: inherit; text-decoration: none; }
button { font: inherit; }

.library, .reader { width: min(1120px, calc(100% - 64px)); margin: 0 auto; }
.library-header, .reader-header {
    min-height: 82px;
    display: flex;
    align-items: center;
    justify-content: space-between;
    border-bottom: 1px solid var(--line);
}
.brand { display: inline-flex; align-items: center; gap: 11px; font-size: 15px; font-weight: 700; letter-spacing: -.03em; }
.brand-mark {
    display: grid;
    width: 31px;
    height: 36px;
    place-items: center;
    color: #f9f7f0;
    background: var(--green);
    font-family: 'Playfair Display', serif;
    font-size: 23px;
}
.library-label { font-size: 10px; font-weight: 700; letter-spacing: .16em; }
.library-label { color: var(--muted); }
.section-heading { display: flex; align-items: baseline; justify-content: space-between; padding-bottom: 17px; border-bottom: 1px solid var(--line); }
.section-heading h2 { margin: 0; font-family: 'Playfair Display', serif; font-size: 25px; font-weight: 500; }
.section-heading > span { color: var(--muted); font-size: 12px; }
.document-grid { display: grid; grid-template-columns: repeat(auto-fill, minmax(210px, 1fr)); gap: 30px 24px; padding: 28px 0 55px; }
.document-card { min-width: 0; }
.cover {
    position: relative;
    display: grid;
    min-height: 295px;
    place-items: center;
    overflow: hidden;
    padding: 8px;
    background: #e7e5df;
    box-shadow: 5px 7px 0 #e0ddd4, 0 14px 28px #17251f1c;
    transition: transform .2s ease, box-shadow .2s ease;
}
.document-card:hover .cover, .document-card:focus-visible .cover { transform: translateY(-4px) rotate(-.5deg); box-shadow: 7px 11px 0 #e0ddd4, 0 18px 30px #17251f26; }
.cover-preview { display: block; width: 100%; height: 100%; object-fit: contain; opacity: 0; transition: opacity .2s ease; }
.cover-preview.is-loaded { opacity: 1; }
.cover-loading { position: absolute; color: var(--muted); font-size: 11px; }
.cover.has-error { background: #ece9e1; }
.cover.has-error .cover-loading { color: #9c342a; }
.document-meta { display: flex; align-items: flex-start; justify-content: space-between; gap: 12px; padding-top: 15px; }
.document-name { overflow: hidden; color: #3a463f; font-size: 12px; text-overflow: ellipsis; white-space: nowrap; }
.open-label { flex: 0 0 auto; color: var(--green); font-size: 11px; font-weight: 700; }
.open-label span { padding-left: 4px; }
.empty-state { padding: 30px 0 60px; color: var(--muted); font-size: 14px; }
.empty-state code { color: var(--ink); }
.library-footer { padding: 22px 0; border-top: 1px solid var(--line); color: var(--muted); font-size: 11px; }

.reader { width: min(1240px, calc(100% - 48px)); }
.reader-header { min-height: 72px; gap: 20px; }
.reader-title { overflow: hidden; margin: 0 auto 0 16px; font-size: 13px; font-weight: 500; text-overflow: ellipsis; white-space: nowrap; }
.back-link { flex: 0 0 auto; color: var(--green); font-size: 12px; font-weight: 600; }
.reading-area { position: relative; display: flex; min-height: min(72vh, 790px); align-items: center; justify-content: center; padding: 38px 55px 20px; }
.book-viewport {
    display: flex;
    width: 100%;
    height: min(68vh, 790px);
    align-items: safe center;
    justify-content: safe center;
    overflow: auto;
    scrollbar-color: #9da69f #e8e7e1;
    scrollbar-width: thin;
    overscroll-behavior: contain;
    touch-action: pan-x pan-y pinch-zoom;
}
.book {
    position: relative;
    display: flex;
    width: min(100%, 980px);
    min-height: 300px;
    align-items: center;
    justify-content: center;
    perspective: 1800px;
    filter: drop-shadow(0 22px 26px #17251f24);
}
.page {
    position: relative;
    display: flex;
    width: 50%;
    min-height: 300px;
    align-items: center;
    justify-content: center;
    overflow: hidden;
    background: #fff;
    backface-visibility: hidden;
}
.page[hidden] { display: none; }
.page-left { border-right: 1px solid #e3e3df; box-shadow: inset -10px 0 18px -18px #17251f; }
.page-right { box-shadow: inset 10px 0 18px -18px #17251f; }
.page canvas { display: block; max-width: none; height: auto; }
.page-number { position: absolute; bottom: 10px; color: #888; font-size: 10px; }
.page-left .page-number { left: 18px; }
.page-right .page-number { right: 18px; }
.turn-sheet {
    position: absolute;
    z-index: 1;
    top: 0;
    right: 0;
    display: none;
    width: 50%;
    height: 100%;
    transform-style: preserve-3d;
    pointer-events: none;
    will-change: transform;
}
.turn-sheet.is-active { display: block; }
.turn-sheet.turn-backward { right: auto; left: 0; }
.turn-face {
    position: absolute;
    inset: 0;
    display: grid;
    place-items: center;
    overflow: hidden;
    background: #fff;
    backface-visibility: hidden;
    box-shadow: inset 0 0 1px #17251f20;
}
.turn-face canvas { display: block; max-width: none; max-height: none; }
.turn-back { transform: rotateY(180deg); }
.turn-sheet::before, .turn-sheet::after {
    position: absolute;
    z-index: 2;
    inset: 0;
    content: "";
    opacity: 0;
    pointer-events: none;
}
.turn-sheet::before {
    background: linear-gradient(90deg, #17251f00 18%, #17251f0a 43%, #17251f3b 76%, #17251f00);
}
.turn-sheet::after {
    background: linear-gradient(105deg, #fff0 25%, #ffffff12 42%, #ffffffc2 49%, #ffffff22 55%, #17251f18 69%, #fff0 83%);
    mix-blend-mode: multiply;
}
.turn-sheet.turn-forward { transform-origin: left center; }
.turn-sheet.turn-backward { transform-origin: right center; }
.book.turning-forward .turn-sheet { animation: page-curl-forward .96s cubic-bezier(.34, .02, .18, 1) both; }
.book.turning-backward .turn-sheet { animation: page-curl-backward .96s cubic-bezier(.34, .02, .18, 1) both; }
.book.turning-forward .turn-sheet::before, .book.turning-backward .turn-sheet::before {
    animation: curl-shadow .96s ease-in-out both;
}
.book.turning-forward .turn-sheet::after, .book.turning-backward .turn-sheet::after {
    animation: curl-highlight .96s ease-in-out both;
}
@keyframes page-curl-forward {
    0% { transform: perspective(1500px) rotateY(0) rotateZ(0) skewY(0) scaleX(1); box-shadow: 0 0 0 #17251f00; }
    14% { transform: perspective(1500px) rotateY(-12deg) rotateZ(1deg) skewY(1.4deg) scaleX(.985); box-shadow: -5px 4px 8px #17251f18; }
    34% { transform: perspective(1500px) rotateY(-48deg) rotateZ(2deg) skewY(-2.2deg) scaleX(.94); box-shadow: -12px 8px 18px #17251f30; }
    52% { transform: perspective(1500px) rotateY(-96deg) rotateZ(-1deg) skewY(1.4deg) scaleX(.9); box-shadow: -16px 5px 22px #17251f40; }
    72% { transform: perspective(1500px) rotateY(-145deg) rotateZ(-1.5deg) skewY(-1deg) scaleX(.95); box-shadow: -10px 6px 15px #17251f2b; }
    88% { transform: perspective(1500px) rotateY(-169deg) rotateZ(.7deg) skewY(.5deg) scaleX(.985); box-shadow: -4px 3px 7px #17251f14; }
    100% { transform: perspective(1500px) rotateY(-180deg) rotateZ(0) skewY(0) scaleX(1); box-shadow: 0 0 0 #17251f00; }
}
@keyframes page-curl-backward {
    0% { transform: perspective(1500px) rotateY(0) rotateZ(0) skewY(0) scaleX(1); box-shadow: 0 0 0 #17251f00; }
    14% { transform: perspective(1500px) rotateY(12deg) rotateZ(-1deg) skewY(-1.4deg) scaleX(.985); box-shadow: 5px 4px 8px #17251f18; }
    34% { transform: perspective(1500px) rotateY(48deg) rotateZ(-2deg) skewY(2.2deg) scaleX(.94); box-shadow: 12px 8px 18px #17251f30; }
    52% { transform: perspective(1500px) rotateY(96deg) rotateZ(1deg) skewY(-1.4deg) scaleX(.9); box-shadow: 16px 5px 22px #17251f40; }
    72% { transform: perspective(1500px) rotateY(145deg) rotateZ(1.5deg) skewY(1deg) scaleX(.95); box-shadow: 10px 6px 15px #17251f2b; }
    88% { transform: perspective(1500px) rotateY(169deg) rotateZ(-.7deg) skewY(-.5deg) scaleX(.985); box-shadow: 4px 3px 7px #17251f14; }
    100% { transform: perspective(1500px) rotateY(180deg) rotateZ(0) skewY(0) scaleX(1); box-shadow: 0 0 0 #17251f00; }
}
@keyframes curl-shadow {
    0%, 100% { opacity: 0; }
    24% { opacity: .28; }
    48%, 58% { opacity: .9; }
    78% { opacity: .34; }
}
@keyframes curl-highlight {
    0%, 100% { opacity: 0; }
    28% { opacity: .22; }
    46%, 56% { opacity: .86; }
    76% { opacity: .24; }
}
.loading { position: absolute; inset: 0; display: grid; place-items: center; color: var(--muted); background: #fff; font-size: 13px; }
.loading[hidden] { display: none; }
.nav-button {
    position: absolute;
    z-index: 2;
    top: 50%;
    display: grid;
    width: 42px;
    height: 42px;
    place-items: center;
    border: 1px solid #d5d9d3;
    border-radius: 50%;
    color: var(--ink);
    background: #fbfaf6;
    cursor: pointer;
    font-family: 'Playfair Display', serif;
    font-size: 30px;
    line-height: 1;
    transform: translateY(-50%);
}
.nav-button:hover:not(:disabled) { color: white; background: var(--green); }
.nav-button:disabled { cursor: default; opacity: .35; }
.previous { left: 0; }
.next { right: 0; }
.reader-controls { display: flex; align-items: center; justify-content: center; gap: 26px; padding-top: 2px; font-size: 12px; }
.reader-controls > span { min-width: 110px; color: var(--muted); text-align: center; }
.text-button { padding: 6px 0; border: 0; color: var(--green); background: transparent; cursor: pointer; font-size: 12px; font-weight: 600; }
.text-button:disabled { cursor: default; opacity: .4; }
.zoom-controls { display: inline-flex; align-items: center; gap: 8px; margin-left: 12px; }
.zoom-button {
    display: grid;
    width: 30px;
    height: 30px;
    place-items: center;
    border: 1px solid var(--line);
    border-radius: 50%;
    color: var(--green);
    background: #fbfaf6;
    cursor: pointer;
    font-size: 18px;
    line-height: 1;
}
.zoom-button:hover:not(:disabled) { color: #fff; background: var(--green); }
.zoom-button:disabled { cursor: default; opacity: .4; }
.zoom-slider { width: 110px; height: 4px; accent-color: var(--green); cursor: pointer; }
.zoom-level { min-width: 42px; padding: 5px 0; border: 0; color: var(--muted); background: transparent; cursor: pointer; font-size: 11px; }
.reader-hint { margin: 17px 0 0; color: #879088; font-size: 11px; text-align: center; }
.reader-error { margin: 20px 0; color: #9c342a; font-size: 13px; text-align: center; }

@media (max-width: 680px) {
    .library, .reader { width: calc(100% - 36px); }
    .library-header, .reader-header { min-height: 66px; }
    .document-grid { grid-template-columns: repeat(2, minmax(0, 1fr)); gap: 24px 16px; }
    .cover { min-height: 230px; padding: 6px; }
    .document-meta { flex-direction: column; gap: 7px; }
    .reader-header { flex-wrap: wrap; gap: 8px 12px; padding: 12px 0; }
    .reader-title { order: 3; width: 100%; margin: 0; }
    .back-link { margin-left: auto; font-size: 11px; }
    .reading-area { min-height: 67vh; padding: 32px 0 16px; }
    .book-viewport { height: 62vh; }
    .book { min-height: 260px; }
    .page { min-height: 260px; }
    .nav-button { width: 34px; height: 34px; background: #fbfaf6eF; font-size: 25px; }
    .previous { left: 5px; }
    .next { right: 5px; }
    .reader-controls { flex-wrap: wrap; gap: 10px 16px; }
    .zoom-controls { margin-left: 4px; }
    .zoom-slider { width: 80px; }
}

@media (prefers-reduced-motion: reduce) {
    *, *::before, *::after { scroll-behavior: auto !important; transition-duration: .01ms !important; animation-duration: .01ms !important; }
}
