:root {
    color-scheme: light;
}

:root[data-theme="dark"] {
    color-scheme: dark;
    --bg: #101715;
    --panel: #17201d;
    --ink: #eef5ef;
    --muted: #aab7ae;
    --line: #33423b;
    --accent: #c84b58;
    --accent-strong: #ee7480;
    --forest: #3f7a5a;
    --shadow: 0 18px 50px rgba(0, 0, 0, 0.34);
}

.is-hidden {
    display: none !important;
}

.library-view {
    min-height: 100vh;
    padding: 42px 20px;
    background: var(--bg);
}

.library-inner {
    max-width: 1040px;
    margin: 0 auto;
}

.library-header {
    display: grid;
    justify-items: center;
    gap: 11px;
    margin: 0 auto 34px;
    text-align: center;
}

.ministry-emblem {
    display: grid;
    place-items: center;
    width: 108px;
    height: 108px;
    margin-bottom: 4px;
    border-radius: 50%;
    background: radial-gradient(circle at 50% 38%, rgba(255, 255, 255, 0.95), rgba(255, 250, 240, 0.58));
    box-shadow: 0 16px 38px rgba(31, 42, 36, 0.16);
}

.ministry-emblem img {
    display: block;
    width: 82px;
    height: 82px;
    object-fit: contain;
    user-select: none;
    -webkit-user-drag: none;
}

.reader-emblem {
    display: grid;
    place-items: center;
    width: 46px;
    height: 46px;
    border-radius: 8px;
    background: rgba(255, 255, 255, 0.72);
    box-shadow: 0 8px 18px rgba(31, 42, 36, 0.12);
}

.reader-emblem img {
    display: block;
    width: 34px;
    height: 34px;
    object-fit: contain;
    user-select: none;
    -webkit-user-drag: none;
}

.library-header h1 {
    margin: 0;
    max-width: 900px;
    font-size: clamp(34px, 5vw, 58px);
    line-height: 1.05;
    color: var(--accent-strong);
}

.library-header p {
    margin: 0;
    max-width: 760px;
    color: var(--forest);
    font-size: clamp(17px, 2.2vw, 24px);
    font-weight: 700;
    line-height: 1.28;
}

.library-header small {
    color: var(--muted);
    font-size: 16px;
    font-weight: 700;
    letter-spacing: 0;
}

.book-grid {
    display: grid;
    grid-template-columns: repeat(2, minmax(220px, 1fr));
    gap: 24px;
}

.book-card {
    display: grid;
    grid-template-columns: 150px minmax(0, 1fr);
    align-items: center;
    gap: 20px;
    min-height: 236px;
    border: 1px solid var(--line);
    border-radius: 8px;
    padding: 20px;
    text-align: left;
    color: var(--ink);
    background: var(--panel);
    box-shadow: var(--shadow);
    transition: transform 160ms ease, border-color 160ms ease, box-shadow 160ms ease;
}

.book-card:hover,
.book-card:focus-visible {
    border-color: var(--accent);
    transform: translateY(-2px);
    box-shadow: 0 22px 54px rgba(31, 42, 36, 0.2);
}

.book-cover {
    display: block;
    height: 200px;
    overflow: hidden;
    border-radius: 6px;
    background: white;
    box-shadow: 0 10px 22px rgba(31, 42, 36, 0.18);
}

.book-cover img {
    display: block;
    width: 100%;
    height: 100%;
    object-fit: cover;
    object-position: top center;
    user-select: none;
    -webkit-user-drag: none;
}

.book-meta {
    display: grid;
    gap: 8px;
}

.book-meta strong {
    font-size: 26px;
    line-height: 1.05;
}

.book-meta small {
    color: var(--muted);
    font-size: 15px;
}

.toolbar-text {
    min-width: auto !important;
    padding: 0 14px !important;
    font-size: 15px !important;
    font-weight: 700;
}

.theme-toggle {
    width: 42px;
    min-width: 42px !important;
    padding: 0 !important;
    font-size: 20px !important;
}

.mobile-search-toggle,
.mobile-zoom-toggle,
.mobile-search-close,
.mobile-search-backdrop {
    display: none;
}

.page-stage img {
    width: 980px;
    max-width: none;
    min-width: 0;
    transition: opacity 140ms ease;
}

.page-stage img.is-loading {
    opacity: 0.5;
}

@media (max-width: 860px) {
    .app-shell {
        display: flex;
        flex-direction: column;
        min-height: 100vh;
    }

    .reader {
        order: 1;
        min-height: auto;
    }

    .sidebar {
        position: fixed;
        left: 10px;
        right: 10px;
        bottom: 10px;
        z-index: 20;
        max-height: min(74vh, 620px);
        min-height: 0;
        padding: 16px;
        gap: 12px;
        border: 1px solid var(--line);
        border-radius: 8px;
        box-shadow: 0 22px 70px rgba(31, 42, 36, 0.26);
        transform: translateY(calc(100% + 26px));
        transition: transform 180ms ease;
        overflow: auto;
    }

    .sidebar .brand {
        display: flex;
        padding-right: 46px;
    }

    .sidebar .brand h1 {
        font-size: 15px;
    }

    .reader-emblem {
        width: 38px;
        height: 38px;
    }

    .reader-emblem img {
        width: 29px;
        height: 29px;
    }

    .is-search-open .sidebar {
        transform: translateY(0);
    }

    .mobile-search-backdrop {
        position: fixed;
        inset: 0;
        z-index: 19;
        display: block;
        border: 0;
        padding: 0;
        background: rgba(16, 23, 21, 0.42);
        opacity: 0;
        pointer-events: none;
        transition: opacity 180ms ease;
    }

    .is-search-open .mobile-search-backdrop {
        opacity: 1;
        pointer-events: auto;
    }

    .mobile-search-close {
        position: absolute;
        top: 12px;
        right: 12px;
        display: grid;
        place-items: center;
        width: 36px;
        height: 36px;
        border: 0;
        border-radius: 8px;
        color: white;
        background: var(--forest);
        font-size: 24px;
        line-height: 1;
    }

    .mobile-search-toggle {
        position: fixed;
        right: 14px;
        bottom: 16px;
        z-index: 18;
        display: grid;
        place-items: center;
        width: 52px;
        height: 52px;
        border: 0;
        border-radius: 50%;
        background: var(--accent);
        box-shadow: 0 14px 34px rgba(31, 42, 36, 0.26);
    }

    .mobile-search-toggle::before {
        content: "";
        width: 17px;
        height: 17px;
        border: 3px solid white;
        border-radius: 50%;
        transform: translate(-2px, -2px);
    }

    .mobile-search-toggle::after {
        content: "";
        width: 13px;
        height: 3px;
        border-radius: 999px;
        background: white;
        transform: translate(9px, 8px) rotate(45deg);
        position: absolute;
    }

    .mobile-zoom-toggle {
        position: fixed;
        left: 14px;
        bottom: 16px;
        z-index: 18;
        display: grid;
        place-items: center;
        width: 52px;
        height: 52px;
        border: 0;
        border-radius: 50%;
        color: white;
        background: var(--forest);
        box-shadow: 0 14px 34px rgba(31, 42, 36, 0.26);
    }

    .mobile-zoom-toggle::before {
        content: "±";
        color: white;
        font-size: 28px;
        font-weight: 700;
        line-height: 1;
    }

    .field,
    .search-box {
        gap: 6px;
    }

    .search-row {
        grid-template-columns: minmax(0, 1fr) 82px;
    }

    .results {
        max-height: 42vh;
    }

    .reader-toolbar {
        display: grid;
        grid-template-columns: auto 42px minmax(112px, 1fr) 42px 42px;
        gap: 7px;
        align-items: center;
        padding: 10px;
    }

    .reader-toolbar button {
        min-width: 42px;
        height: 40px;
        padding: 0 10px;
        border-radius: 8px;
        font-size: 19px;
    }

    .toolbar-text {
        padding: 0 10px !important;
        font-size: 13px !important;
    }

    .toolbar-spacer {
        display: none;
    }

    .page-control {
        min-width: 0;
        justify-content: center;
        gap: 6px;
    }

    .page-control input {
        width: 64px;
        height: 40px;
        padding: 8px 6px;
    }

    #zoomValue {
        min-width: 52px;
        font-size: 13px;
    }

    .theme-toggle {
        width: 42px;
        min-width: 42px !important;
    }

    #backToLibrary {
        grid-column: 1;
        grid-row: 1;
    }

    #prevPage {
        grid-column: 2;
        grid-row: 1;
    }

    .page-control {
        grid-column: 3;
        grid-row: 1;
    }

    #nextPage {
        grid-column: 4;
        grid-row: 1;
    }

    .theme-toggle {
        grid-column: 5;
        grid-row: 1;
    }

    #zoomOut {
        position: fixed;
        left: calc(50% - 74px);
        bottom: 18px;
        z-index: 17;
        width: 44px;
        height: 44px;
        border-radius: 50%;
        box-shadow: 0 12px 28px rgba(31, 42, 36, 0.2);
    }

    #zoomValue {
        position: fixed;
        left: calc(50% - 30px);
        bottom: 18px;
        z-index: 17;
        display: grid;
        place-items: center;
        width: 60px;
        height: 44px;
        border-radius: 999px;
        color: var(--ink);
        background: var(--panel);
        box-shadow: 0 12px 28px rgba(31, 42, 36, 0.14);
    }

    #zoomIn {
        position: fixed;
        left: calc(50% + 30px);
        bottom: 18px;
        z-index: 17;
        width: 44px;
        height: 44px;
        border-radius: 50%;
        box-shadow: 0 12px 28px rgba(31, 42, 36, 0.2);
    }

    .page-stage {
        min-height: calc(100vh - 61px);
        padding: 10px 10px 86px;
        background: color-mix(in srgb, var(--bg) 76%, white);
    }

    .page-stage img {
        border-radius: 3px;
    }
}

:root[data-theme="dark"] select,
:root[data-theme="dark"] input[type="search"],
:root[data-theme="dark"] input[type="number"],
:root[data-theme="dark"] .result-item {
    color: var(--ink);
    background: #111916;
}

:root[data-theme="dark"] .reader-toolbar {
    background: rgba(16, 23, 21, 0.94);
}

:root[data-theme="dark"] .library-header h1 {
    color: #ff9aa4;
}

:root[data-theme="dark"] .library-header p {
    color: #8fd6a7;
}

:root[data-theme="dark"] .ministry-emblem {
    background: radial-gradient(circle at 50% 38%, rgba(255, 255, 255, 0.9), rgba(238, 245, 239, 0.18));
    box-shadow: 0 18px 42px rgba(0, 0, 0, 0.34);
}

@media (max-width: 760px) {
    .book-grid {
        grid-template-columns: 1fr;
    }

    .book-card {
        grid-template-columns: 120px minmax(0, 1fr);
        min-height: 198px;
    }

    .book-cover {
        height: 160px;
    }

    .book-meta strong {
        font-size: 22px;
    }
}

@media (max-width: 540px) {
    .library-view {
        padding: 22px 14px;
    }

    .library-header {
        justify-items: center;
    }

    .ministry-emblem {
        width: 92px;
        height: 92px;
    }

    .ministry-emblem img {
        width: 70px;
        height: 70px;
    }

    .book-card {
        grid-template-columns: 104px minmax(0, 1fr);
        gap: 14px;
        padding: 14px;
    }

    .book-cover {
        height: 142px;
    }

    .reader-toolbar {
        grid-template-columns: auto 40px minmax(104px, 1fr) 40px 40px;
        gap: 6px;
        padding: 8px;
    }

    .reader-toolbar button {
        height: 38px;
        min-width: 38px;
        padding: 0 8px;
        font-size: 18px;
    }

    .toolbar-text {
        min-width: 72px !important;
        padding: 0 8px !important;
        font-size: 12px !important;
    }

    #backToLibrary {
        grid-column: 1;
        grid-row: 1;
    }

    #prevPage {
        grid-column: 2;
        grid-row: 1;
    }

    .page-control {
        grid-column: 3;
        grid-row: 1;
    }

    #nextPage {
        grid-column: 4;
        grid-row: 1;
    }

    .theme-toggle {
        grid-column: 5;
        grid-row: 1;
    }

    #zoomOut {
        position: fixed;
        left: calc(50% - 74px);
        bottom: 18px;
        z-index: 17;
        width: 44px;
        height: 44px;
        border-radius: 50%;
        box-shadow: 0 12px 28px rgba(31, 42, 36, 0.2);
    }

    #zoomValue {
        position: fixed;
        left: calc(50% - 30px);
        bottom: 18px;
        z-index: 17;
        display: grid;
        place-items: center;
        width: 60px;
        height: 44px;
        border-radius: 999px;
        color: var(--ink);
        background: var(--panel);
        box-shadow: 0 12px 28px rgba(31, 42, 36, 0.14);
    }

    #zoomIn {
        position: fixed;
        left: calc(50% + 30px);
        bottom: 18px;
        z-index: 17;
        width: 44px;
        height: 44px;
        border-radius: 50%;
        box-shadow: 0 12px 28px rgba(31, 42, 36, 0.2);
    }

    .page-control input {
        width: 58px;
        height: 38px;
    }

    .page-control span {
        font-size: 12px;
        white-space: nowrap;
    }

    #zoomValue {
        min-width: 44px;
        font-size: 12px;
    }

    .page-stage {
        min-height: calc(100vh - 55px);
        padding: 8px 8px 86px;
    }

    .sidebar {
        padding: 14px;
    }

    select,
    input[type="search"],
    input[type="number"] {
        padding: 10px;
    }
}

@media (max-width: 420px) {
    .reader-toolbar {
        grid-template-columns: auto 38px minmax(86px, 1fr) 38px 38px;
        gap: 5px;
    }

    .reader-toolbar button {
        height: 36px;
        min-width: 36px;
        padding: 0 6px;
    }

    .toolbar-text {
        min-width: 64px !important;
        font-size: 11px !important;
    }

    .page-control input {
        width: 52px;
        height: 36px;
    }

    #zoomValue {
        width: 56px;
        left: calc(50% - 28px);
        min-width: 0;
    }

    #zoomOut {
        left: calc(50% - 70px);
    }

    #zoomIn {
        left: calc(50% + 28px);
    }
}

@media (max-width: 860px) {
    #zoomOut,
    #zoomIn {
        position: fixed !important;
        top: auto !important;
        bottom: 18px !important;
        z-index: 17;
        width: 44px;
        height: 44px;
        border-radius: 50%;
        opacity: 0;
        pointer-events: none;
        transform: translateY(12px) scale(0.94);
        transition: opacity 160ms ease, transform 160ms ease;
    }

    #zoomOut {
        left: calc(50% - 74px) !important;
    }

    #zoomValue {
        position: fixed !important;
        top: auto !important;
        left: calc(50% - 30px) !important;
        bottom: 18px !important;
        z-index: 17;
        display: grid;
        place-items: center;
        width: 60px;
        height: 44px;
        min-width: 0;
        border-radius: 999px;
        color: var(--ink);
        background: var(--panel);
        box-shadow: 0 12px 28px rgba(31, 42, 36, 0.14);
        opacity: 0;
        pointer-events: none;
        transform: translateY(12px) scale(0.94);
        transition: opacity 160ms ease, transform 160ms ease;
    }

    #zoomIn {
        left: calc(50% + 30px) !important;
    }

    .is-search-open .mobile-search-toggle {
        opacity: 0;
        pointer-events: none;
    }

    .is-zoom-open #zoomOut,
    .is-zoom-open #zoomValue,
    .is-zoom-open #zoomIn {
        opacity: 1;
        pointer-events: auto;
        transform: translateY(0) scale(1);
    }

    .is-search-open .mobile-zoom-toggle {
        opacity: 0;
        pointer-events: none;
    }
}

@media (max-width: 420px) {
    #zoomOut {
        left: calc(50% - 70px) !important;
    }

    #zoomValue {
        left: calc(50% - 28px) !important;
        width: 56px;
    }

    #zoomIn {
        left: calc(50% + 28px) !important;
    }
}

.zoom-popover {
    position: fixed;
    top: 14px;
    right: 76px;
    z-index: 4;
    display: flex;
    align-items: center;
    gap: 8px;
}

.zoom-popover #zoomOut,
.zoom-popover #zoomIn {
    position: static !important;
    width: 42px;
    height: 42px;
    min-width: 42px;
    border: 0;
    border-radius: 8px;
    color: white;
    background: var(--forest);
    font-size: 22px;
    line-height: 1;
    box-shadow: none;
    opacity: 1;
    pointer-events: auto;
    transform: none;
}

.zoom-popover #zoomValue {
    position: static !important;
    display: grid;
    place-items: center;
    width: 58px;
    height: 42px;
    min-width: 58px;
    border: 1px solid var(--line);
    border-radius: 8px;
    color: var(--ink);
    background: var(--panel);
    box-shadow: none;
    opacity: 1;
    pointer-events: auto;
    transform: none;
}

@media (max-width: 860px) {
    .zoom-popover {
        top: auto;
        right: auto;
        left: 50%;
        bottom: 18px;
        z-index: 18;
        padding: 0;
        opacity: 0;
        pointer-events: none;
        transform: translate(-50%, 12px) scale(0.94);
        transition: opacity 160ms ease, transform 160ms ease;
    }

    .is-zoom-open .zoom-popover {
        opacity: 1;
        pointer-events: auto;
        transform: translate(-50%, 0) scale(1);
    }

    .zoom-popover #zoomOut,
    .zoom-popover #zoomIn {
        width: 44px;
        height: 44px;
        min-width: 44px;
        border-radius: 50%;
        box-shadow: 0 12px 28px rgba(31, 42, 36, 0.2);
    }

    .zoom-popover #zoomValue {
        width: 60px;
        height: 44px;
        min-width: 60px;
        border-radius: 999px;
        box-shadow: 0 12px 28px rgba(31, 42, 36, 0.14);
    }
}
