/* ==========================================================================
   PROVLÄSARE – Vilse & Iver
   En egen bläddringsbar bok. Sidorna ställs in i index.html (window.PROVLAS).
   ========================================================================== */

html.reader-open { overflow: hidden; }

.reader {
    --cream: #efefdc;
    --paper: #fbf7ea;
    --ink: #4F4A40;
    --gold: #c59027;
    --butter: #ffe79b;
    --rose: #eeafaf;
    position: fixed;
    inset: 0;
    z-index: 1000;
    display: grid;
    grid-template-rows: auto 1fr auto;
    color: var(--cream);
    font-family: 'Times New Roman', serif;
    opacity: 0;
    transition: opacity .35s ease;
    -webkit-tap-highlight-color: transparent;
}
.reader[hidden] { display: none; }
.reader.is-open { opacity: 1; }

.reader__backdrop {
    position: absolute;
    inset: 0;
    z-index: -1;
    background:
        radial-gradient(ellipse at 50% 45%, #5c4e3e 0%, #2e2720 55%, #1a1612 100%);
}
/* Lite filtkänsla i bakgrunden */
.reader__backdrop::after {
    content: "";
    position: absolute;
    inset: 0;
    opacity: .18;
    mix-blend-mode: overlay;
    background-image: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' width='220' height='220'%3E%3Cfilter id='n'%3E%3CfeTurbulence type='fractalNoise' baseFrequency='.9' numOctaves='3' stitchTiles='stitch'/%3E%3C/filter%3E%3Crect width='100%25' height='100%25' filter='url(%23n)'/%3E%3C/svg%3E");
}

/* --- Toppfält --- */
.reader__top {
    display: flex;
    align-items: center;
    justify-content: space-between;
    gap: 12px;
    padding: 14px 16px 6px;
}
.reader__title {
    margin: 0;
    font-family: 'IM Fell English', serif;
    font-size: 1.15rem;
    line-height: 1.2;
    color: var(--cream);
    opacity: .9;
}
.reader__title span { display: block; font-size: .8rem; letter-spacing: .14em; text-transform: uppercase; opacity: .65; font-family: 'Open Sans', sans-serif; }
.reader__tools { display: flex; gap: 14px; }

.reader-btn {
    flex: none;
    width: 44px;
    height: 44px;
    display: grid;
    place-items: center;
    padding: 0;
    border-radius: 50%;
    background: var(--cream);
    color: var(--ink);
    border: 3px solid var(--butter);
    box-shadow: 0 0 0 5px var(--rose), 0 6px 14px rgba(0,0,0,.35);
    cursor: pointer;
    transition: background-color .25s, color .25s, transform .12s, opacity .25s;
}
.reader-btn svg { width: 20px; height: 20px; stroke: currentColor; fill: none; stroke-width: 2; stroke-linecap: round; stroke-linejoin: round; }
.reader-btn:hover { background: var(--gold); color: #FBF7ED; }
.reader-btn:active { transform: translateY(2px); }
.reader-btn:focus-visible { outline: 2px solid #fff; outline-offset: 7px; }
.reader-btn[disabled] { opacity: .35; cursor: default; pointer-events: none; }
.reader-btn[aria-pressed="false"] .when-on,
.reader-btn[aria-pressed="true"] .when-off { display: none; }

/* --- Scen och bok --- */
.reader__stage {
    position: relative;
    display: flex;
    overflow: hidden;
    min-height: 0;
    padding: 10px 18px 34px;
}
.reader.is-zoomed .reader__stage { overflow: auto; }

.book {
    position: relative;
    margin: auto;           /* centrerar även när innehållet är större än scenen */
    flex: none;
    width: var(--bw, 600px);
    height: var(--ph, 450px);
    touch-action: none;
    user-select: none;
    -webkit-user-select: none;
    cursor: grab;
    transform: translateY(18px) scale(.97);
    transition: transform .55s cubic-bezier(.2,.8,.2,1);
}
.reader.is-open .book { transform: none; }
.book.is-dragging { cursor: grabbing; }
.reader.is-zoomed .book { touch-action: auto; cursor: default; }

.book__shift {
    position: absolute;
    inset: 0;
    will-change: transform;
}

/* Mjuk skugga under boken – följer bokens öppna yta */
.book__ground {
    position: absolute;
    top: 1.5%;
    bottom: .5%;
    left: var(--gl, 0px);
    width: var(--gw, 100%);
    border-radius: 8px;
    box-shadow: 0 18px 40px rgba(0,0,0,.42), 0 4px 10px rgba(0,0,0,.28);
    pointer-events: none;
}

/* Sidkanter – bokens tjocklek som växer och krymper när man bläddrar */
.book__edge {
    position: absolute;
    top: 3px;
    bottom: 3px;
    width: var(--ew, 0px);
    pointer-events: none;
    background:
        repeating-linear-gradient(to right, #e9e1cb 0 1px, #d8ceb3 1px 2px);
    box-shadow: 0 2px 4px rgba(0,0,0,.25);
}
.book__edge--left  { right: calc(100% - var(--el, 0px)); border-radius: 4px 0 0 4px; }
.book__edge--right { left: var(--er, 100%); border-radius: 0 4px 4px 0; }

/* Ett blad = framsida (höger sida) + baksida (vänster sida på nästa uppslag) */
.leaf {
    position: absolute;
    top: 0;
    height: 100%;
    width: var(--pw);
    transform-origin: 0 50%;
    transform-style: preserve-3d;
    will-change: transform;
}
.face {
    position: absolute;
    inset: 0;
    overflow: hidden;
    backface-visibility: hidden;
    -webkit-backface-visibility: hidden;
    background-color: var(--paper);
}
.face--front { border-radius: 0 5px 5px 0; }
.face--back  { border-radius: 5px 0 0 5px; transform: rotateY(180deg); }

/* Papperskorn ovanpå allt, även inskannade sidor */
.face::before {
    content: "";
    position: absolute;
    inset: 0;
    z-index: 3;
    pointer-events: none;
    opacity: .22;
    mix-blend-mode: multiply;
    background-image: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' width='300' height='300'%3E%3Cfilter id='p'%3E%3CfeTurbulence type='fractalNoise' baseFrequency='.75' numOctaves='4' stitchTiles='stitch'/%3E%3CfeColorMatrix values='0 0 0 0 .55  0 0 0 0 .47  0 0 0 0 .36  0 0 0 .55 0'/%3E%3C/filter%3E%3Crect width='100%25' height='100%25' filter='url(%23p)'/%3E%3C/svg%3E");
}
/* Ryggskugga – där sidan böjer sig in mot bindningen */
.face::after {
    content: "";
    position: absolute;
    inset: 0;
    z-index: 4;
    pointer-events: none;
}
.face--front::after {
    background:
        linear-gradient(to right, rgba(70,48,25,.32) 0%, rgba(70,48,25,.12) 3.5%, rgba(255,255,255,.10) 7%, rgba(255,255,255,0) 14%),
        linear-gradient(to left, rgba(70,48,25,.10) 0%, rgba(70,48,25,0) 2.5%);
}
.face--back::after {
    background:
        linear-gradient(to left, rgba(70,48,25,.32) 0%, rgba(70,48,25,.12) 3.5%, rgba(255,255,255,.10) 7%, rgba(255,255,255,0) 14%),
        linear-gradient(to right, rgba(70,48,25,.10) 0%, rgba(70,48,25,0) 2.5%);
}
.face.is-cover::after {
    background: linear-gradient(to right, rgba(0,0,0,.35) 0, rgba(255,255,255,.18) 1.2%, rgba(0,0,0,.12) 2.6%, rgba(0,0,0,0) 5%);
}
.face.is-cover::before { opacity: .12; }

/* Ljus/skugga när sidan vänds */
.face__shade {
    position: absolute;
    inset: 0;
    z-index: 5;
    pointer-events: none;
    opacity: 0;
}
.face--front .face__shade { background: linear-gradient(to right, rgba(40,26,12,.0) 0%, rgba(255,250,235,.25) 55%, rgba(40,26,12,.45) 100%); }
.face--back  .face__shade { background: linear-gradient(to left,  rgba(40,26,12,.0) 0%, rgba(255,250,235,.25) 55%, rgba(40,26,12,.45) 100%); }

/* Böjda blad: remsor med kopior av sidans innehåll */
.leaf.is-bending > .face { visibility: hidden; }
.strip {
    position: absolute;
    top: 0;
    height: 100%;
    transform-origin: 0 50%;
    transform-style: preserve-3d;
}
.strip__piece {
    position: absolute;
    inset: 0;
    overflow: hidden;
    backface-visibility: hidden;
    -webkit-backface-visibility: hidden;
}
.strip__piece--back { transform: rotateY(180deg); }
.face.face--clone {
    inset: 0 auto 0 auto;
    width: var(--pw);
    transform: none;
    visibility: visible;
    border-radius: 0;
}
.face--clone .face__shade { display: none; }
.strip__shade { position: absolute; inset: 0; z-index: 6; pointer-events: none; }

/* Sidinnehåll */
.page {
    position: absolute;
    inset: 0;
    container-type: size;
}
.page img {
    display: block;
    width: 100%;
    height: 100%;
    object-fit: cover;
    -webkit-user-drag: none;
    user-select: none;
    pointer-events: none;
}

/* Platshållarsida */
.page-ph {
    position: absolute;
    inset: 0;
    display: flex;
    flex-direction: column;
    padding: 11cqh 12cqw 8cqh;
    color: #b3a88d;
}
.page-ph__chapter {
    font-family: 'IM Fell English', serif;
    font-size: 7cqw;
    color: #a0ac95;
    text-align: center;
    margin: 0 0 6cqh;
}
.page-ph__lines {
    flex: 1;
    background: repeating-linear-gradient(to bottom, transparent 0 2.6cqh, rgba(160,150,120,.28) 2.6cqh 3.3cqh, transparent 3.3cqh 5.2cqh);
    -webkit-mask-image: linear-gradient(to right, #000 0 82%, rgba(0,0,0,.6) 92%, transparent);
            mask-image: linear-gradient(to right, #000 0 82%, rgba(0,0,0,.6) 92%, transparent);
}
.page-ph__tag {
    align-self: center;
    margin-top: 4cqh;
    font-family: 'Open Sans', sans-serif;
    font-size: 2.6cqw;
    letter-spacing: .12em;
    text-transform: uppercase;
    border: 1px dashed currentColor;
    padding: .5em 1em;
    border-radius: 3px;
}
.page-num {
    position: absolute;
    bottom: 4.2cqh;
    left: 0; right: 0;
    text-align: center;
    font-family: 'IM Fell English', serif;
    font-size: 3.6cqw;
    color: #9d9277;
}
.page-blank { position: absolute; inset: 0; background: linear-gradient(135deg, rgba(0,0,0,0), rgba(120,100,70,.06)); }

/* Sista sidan – slut på provläsningen */
.page-end {
    position: absolute;
    inset: 0;
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    text-align: center;
    padding: 10cqh 11cqw;
    color: var(--ink);
}
.page-end__frog {
    width: 22cqw !important;
    max-width: 140px;
    height: auto !important;
    object-fit: contain !important;
    margin: 0 auto 3cqh;
    pointer-events: none;
}
.page-end__kicker {
    margin: 0;
    font-family: 'Open Sans', sans-serif;
    font-size: 2.7cqw;
    letter-spacing: .16em;
    text-transform: uppercase;
    color: #a0ac95;
}
.page-end__title {
    margin: 2.5cqh 0 2cqh;
    font-family: 'IM Fell English', serif;
    font-size: 8cqw;
    line-height: 1.12;
    color: var(--ink);
}
.page-end__text { margin: 0 0 6cqh; font-size: 4.4cqw; line-height: 1.45; }
.page-end__btn {
    display: inline-block;
    background: var(--cream);
    border: 1.2cqw solid var(--butter);
    box-shadow: 0 0 0 2.2cqw var(--rose);
    padding: 1.2cqh 7cqw;
    font-size: 5.6cqw;
    color: var(--ink);
    text-decoration: none;
    border-radius: 2px;
    transition: background-color .3s, color .3s;
}
.page-end__btn:hover { background: var(--gold); color: #FBF7ED; }

/* Kastad skugga från sidan som vänds */
.book__cast {
    position: absolute;
    inset: 0;
    pointer-events: none;
    opacity: 0;
}

/* --- Bottenfält --- */
.reader__bar {
    display: grid;
    grid-template-columns: auto minmax(0, 420px) auto;
    justify-content: center;
    align-items: center;
    gap: 22px;
    padding: 8px 16px max(16px, env(safe-area-inset-bottom));
}
.reader__meta { text-align: center; min-width: 0; }
.reader__label {
    display: block;
    font-family: 'IM Fell English', serif;
    font-size: 1.05rem;
    margin-bottom: 6px;
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
}
.reader__range {
    -webkit-appearance: none;
    appearance: none;
    width: 100%;
    height: 22px;
    margin: 0;
    background: transparent;
    cursor: pointer;
}
.reader__range::-webkit-slider-runnable-track {
    height: 4px;
    border-radius: 4px;
    background: linear-gradient(to right, var(--butter) 0 var(--fill, 0%), rgba(239,239,220,.28) var(--fill, 0%) 100%);
}
.reader__range::-moz-range-track { height: 4px; border-radius: 4px; background: rgba(239,239,220,.28); }
.reader__range::-moz-range-progress { height: 4px; border-radius: 4px; background: var(--butter); }
.reader__range::-webkit-slider-thumb {
    -webkit-appearance: none;
    width: 18px; height: 18px;
    margin-top: -7px;
    border-radius: 50%;
    background: var(--cream);
    border: 3px solid var(--butter);
    box-shadow: 0 0 0 3px var(--rose);
}
.reader__range::-moz-range-thumb {
    width: 12px; height: 12px;
    border-radius: 50%;
    background: var(--cream);
    border: 3px solid var(--butter);
    box-shadow: 0 0 0 3px var(--rose);
}
.reader__range:focus-visible { outline: 2px solid #fff; outline-offset: 4px; border-radius: 4px; }

.reader__hint {
    position: absolute;
    left: 50%;
    bottom: 8px;
    transform: translateX(-50%);
    margin: 0;
    font-size: .85rem;
    font-family: 'Open Sans', sans-serif;
    color: rgba(239,239,220,.7);
    white-space: nowrap;
    pointer-events: none;
    transition: opacity .6s;
}
.reader__hint.is-hidden { opacity: 0; }

@media (max-width: 600px) {
    .reader__top { padding: 10px 12px 4px; }
    .reader__title { font-size: 1rem; }
    .reader__tools { gap: 12px; }
    .reader-btn { width: 40px; height: 40px; box-shadow: 0 0 0 4px var(--rose), 0 6px 14px rgba(0,0,0,.35); }
    .reader__stage { padding: 6px 10px 30px; }
    .reader__bar { gap: 14px; padding-left: 12px; padding-right: 12px; }
    .reader__hint { font-size: .75rem; }
}

@media (prefers-reduced-motion: reduce) {
    .reader, .book { transition: none; }
}
