/* book.css — shared styles for book.html and book chapter pages */

/* ── Reading progress bar ── */
.progress-bar {
    position: fixed;
    top: 0;
    left: 0;
    height: 3px;
    width: 0;
    background: linear-gradient(90deg, var(--blue), #667eea);
    z-index: 1000;
    transition: width 0.15s linear;
}

/* ── Sticky TOC sidebar ── */
.ch-toc {
    display: none;
}
.toc-toggle {
    display: none;
}

/* Desktop: sidebar */
@media (min-width: 1080px) {
    .ch-toc {
        display: block;
        position: fixed;
        top: 80px;
        left: calc((100vw - 600px) / 2 - 240px);
        width: 210px;
        max-height: 75vh;
        overflow-y: auto;
        background: var(--white);
        border-radius: 12px;
        box-shadow: 0 2px 12px rgba(0,0,0,0.08);
        padding: 16px;
        z-index: 50;
        transition: opacity 0.25s;
    }
    .ch-toc::-webkit-scrollbar { width: 3px; }
    .ch-toc::-webkit-scrollbar-thumb { background: #ccc; border-radius: 3px; }
}

/* Mobile: floating button + slide-up panel */
@media (max-width: 1079px) {
    .toc-toggle {
        display: flex;
        align-items: center;
        justify-content: center;
        position: fixed;
        bottom: 24px;
        right: 20px;
        width: 48px;
        height: 48px;
        background: var(--blue);
        color: #fff;
        border: none;
        border-radius: 50%;
        font-size: 1.3rem;
        cursor: pointer;
        box-shadow: 0 4px 16px rgba(33,150,243,0.4);
        z-index: 100;
        transition: opacity 0.25s;
    }
    .ch-toc {
        display: none;
        position: fixed;
        bottom: 0;
        left: 0;
        right: 0;
        max-height: 70vh;
        overflow-y: auto;
        background: var(--white);
        border-radius: 16px 16px 0 0;
        box-shadow: 0 -4px 20px rgba(0,0,0,0.15);
        padding: 20px 20px 32px;
        z-index: 200;
        animation: tocSlideUp 0.25s ease-out;
    }
    .ch-toc.open {
        display: block;
    }
    .ch-toc.open ~ .toc-toggle {
        display: none;
    }
    @keyframes tocSlideUp {
        from { transform: translateY(100%); }
        to   { transform: translateY(0); }
    }
}

/* TOC content */
.toc-title {
    font-size: 0.75rem;
    font-weight: 700;
    text-transform: uppercase;
    letter-spacing: 0.05em;
    color: var(--text-secondary);
    margin-bottom: 10px;
}
.toc-list {
    list-style: none;
    padding: 0;
    margin: 0 0 4px;
}
.toc-list li {
    margin-bottom: 2px;
}
.toc-list .toc-h2 a {
    font-weight: 600;
}
.toc-list .toc-h3 a {
    padding-left: 12px;
}
.toc-list a {
    display: block;
    padding: 4px 8px;
    font-size: 0.8rem;
    line-height: 1.35;
    color: var(--text-secondary);
    text-decoration: none;
    border-radius: 4px;
    transition: all 0.15s;
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
}
.toc-list a:hover {
    color: var(--blue);
    background: #F0F7FF;
}
.toc-list a.active {
    color: var(--blue);
    background: #E8F4FD;
    font-weight: 600;
}


/* ── Header (blue, matching app.html) ── */
.book-header {
    background: var(--blue);
    color: #fff;
    padding: 52px 16px 32px;
    position: relative;
    overflow: hidden;
    text-align: center;
}

/* Language toggle — blue header style (matches app.html) */
.book-header .lang-toggle {
    position: absolute;
    background: rgba(255,255,255,0.2);
}
.book-header .lang-btn {
    color: rgba(255,255,255,0.7);
}
.book-header .lang-btn.active {
    background: rgba(255,255,255,0.3);
    color: #fff;
    box-shadow: none;
}
.book-header h1 {
    font-size: 2rem;
    font-weight: 800;
    letter-spacing: -0.02em;
    margin-bottom: 8px;
    position: relative;
    z-index: 1;
}
.book-header p {
    opacity: 0.85;
    font-size: 1.1rem;
    position: relative;
    z-index: 1;
}

/* Chapter header variant */
.book-header.ch-header {
    text-align: left;
}
.book-header.ch-header h1 {
    display: flex;
    align-items: center;
    gap: 14px;
    font-size: 1.6rem;
}
.book-header .header-inner {
    max-width: 600px;
    margin: 0 auto;
    position: relative;
    z-index: 1;
}
/* Back link to book TOC */
.ch-back-link {
    display: inline-flex;
    align-items: center;
    color: rgba(255,255,255,0.75);
    text-decoration: none;
    font-size: 0.85rem;
    font-weight: 600;
    margin-bottom: 12px;
    transition: color 0.2s;
}
.ch-back-link::before {
    content: '\2190';
    margin-right: 6px;
    font-size: 1.1rem;
}
.ch-back-link:hover {
    color: #fff;
}

.ch-badge {
    width: 40px;
    height: 40px;
    background: rgba(255,255,255,0.2);
    color: #fff;
    border-radius: 50%;
    display: inline-flex;
    align-items: center;
    justify-content: center;
    font-weight: 700;
    font-size: 1rem;
    flex-shrink: 0;
}
.ch-badge-img {
    width: 44px;
    height: 44px;
    object-fit: contain;
    flex-shrink: 0;
}

/* ── Floating decorations (matches app.html) ── */
.book-header .header-decor {
    display: none;
}
@media (min-width: 800px) {
    .book-header .header-decor { display: block; }

    .book-header .header-icon,
    .book-header .header-dot {
        position: absolute;
        pointer-events: none;
        z-index: 0;
    }
    .book-header .header-icon {
        opacity: 0.08;
    }
    .book-header .header-icon-1 { width: 75px; top: 8%;  left: 3%;  transform: rotate(-15deg); animation: bookFloat1 8s ease-in-out infinite; }
    .book-header .header-icon-2 { width: 55px; top: 45%; left: 10%; transform: rotate(10deg);  animation: bookFloat2 10s ease-in-out infinite; }
    .book-header .header-icon-3 { width: 65px; top: 75%; left: 4%;  transform: rotate(-20deg); animation: bookFloat3 12s ease-in-out infinite; opacity: 0.06; }
    .book-header .header-icon-4 { width: 70px; top: 30%; right: 4%;  transform: rotate(12deg);  animation: bookFloat3 9s ease-in-out infinite; }
    .book-header .header-icon-5 { width: 60px; top: 50%; right: 8%;  transform: rotate(-10deg); animation: bookFloat1 11s ease-in-out infinite; }
    .book-header .header-icon-6 { width: 55px; top: 78%; right: 2%;  transform: rotate(18deg);  animation: bookFloat2 13s ease-in-out infinite; opacity: 0.06; }

    .book-header .header-dot {
        border-radius: 50%;
        background: rgba(255,255,255,0.15);
    }
    .book-header .dot-1 { width: 6px; height: 6px; top: 20%; left: 15%;  animation: bookFloat2 12s ease-in-out infinite; }
    .book-header .dot-2 { width: 5px; height: 5px; top: 60%; left: 6%;   animation: bookFloat1 14s ease-in-out infinite; }
    .book-header .dot-3 { width: 5px; height: 5px; top: 88%; left: 14%;  animation: bookFloat3 16s ease-in-out infinite; }
    .book-header .dot-4 { width: 6px; height: 6px; top: 18%; right: 14%; animation: bookFloat3 13s ease-in-out infinite; }
    .book-header .dot-5 { width: 5px; height: 5px; top: 55%; right: 16%; animation: bookFloat2 15s ease-in-out infinite; }
    .book-header .dot-6 { width: 5px; height: 5px; top: 85%; right: 9%;  animation: bookFloat1 11s ease-in-out infinite; }
}

@keyframes bookFloat1 {
    0%, 100% { transform: rotate(-15deg) translateY(0); }
    50% { transform: rotate(-12deg) translateY(-8px); }
}
@keyframes bookFloat2 {
    0%, 100% { transform: rotate(10deg) translateY(0); }
    50% { transform: rotate(7deg) translateY(-6px); }
}
@keyframes bookFloat3 {
    0%, 100% { transform: rotate(12deg) translateY(0); }
    50% { transform: rotate(15deg) translateY(-10px); }
}

/* ── TOC grid ── */
.book-toc h2 {
    font-size: 1.4rem;
    font-weight: 700;
    margin-bottom: 24px;
}
.book-toc-grid {
    display: grid;
    grid-template-columns: repeat(auto-fill, minmax(140px, 1fr));
    gap: 14px;
}
@media (min-width: 768px) {
    .book-toc-grid {
        grid-template-columns: repeat(auto-fill, minmax(160px, 1fr));
        gap: 18px;
    }
}
.book-toc-item {
    display: flex;
    flex-direction: column;
    align-items: center;
    gap: 10px;
    padding: 20px 12px 16px;
    background: var(--white);
    border-radius: var(--radius);
    box-shadow: var(--shadow);
    text-decoration: none;
    color: var(--text);
    transition: transform 0.2s, box-shadow 0.2s;
}
.book-toc-item:hover {
    background: #E8F4FD;
    box-shadow: 0 4px 12px rgba(0,0,0,0.08);
}
.book-toc-item.book-toc-disabled {
    opacity: 0.4;
    pointer-events: none;
    filter: grayscale(100%);
}
.book-toc-img {
    width: 64px;
    height: 64px;
    object-fit: contain;
}
.book-toc-title {
    font-weight: 600;
    font-size: 0.85rem;
    text-align: center;
    line-height: 1.35;
}

/* ── Chapter content ── */
.ch-content {
    background: var(--white);
    border-radius: var(--radius);
    box-shadow: var(--shadow);
    padding: 28px 24px;
    margin-bottom: 16px;
}
.ch-placeholder {
    color: var(--text-secondary);
    font-style: italic;
    padding: 24px 0;
}

/* ── Chapter body typography ── */
.ch-content { overflow-x: auto; }

.ch-content h2 {
    font-size: 1.4rem;
    font-weight: 800;
    margin: 48px -24px 18px;
    padding: 14px 24px;
    background: var(--blue);
    color: #fff;
    letter-spacing: -0.01em;
}
.ch-content h3 {
    font-size: 1.15rem;
    font-weight: 700;
    margin: 36px 0 12px;
    padding: 10px 14px;
    background: #E8F4FD;
    color: var(--blue-dark, #1976D2);
    border-radius: 6px;
}
.ch-content h4 {
    font-size: 1.05rem;
    font-weight: 700;
    margin: 28px 0 10px;
    padding: 8px 14px;
    background: #F8F9FA;
    border-left: 3px solid var(--blue);
    border-radius: 0 6px 6px 0;
    color: var(--text);
}
.ch-content > h2:first-child,
.ch-content > h3:first-child {
    margin-top: 0;
}

.ch-content p {
    margin: 0 0 16px;
    line-height: 1.7;
}

.ch-content ul,
.ch-content ol {
    margin: 0 0 16px;
    padding-left: 24px;
}
.ch-content li {
    margin-bottom: 6px;
    line-height: 1.6;
}
.ch-content li::marker {
    color: var(--blue);
}

.ch-content .table-wrap {
    margin: 16px 0 24px;
    border: 1px solid #E8E8ED;
    border-radius: 10px;
    overflow: hidden;
}
.ch-content table {
    width: 100%;
    border-collapse: collapse;
    font-size: 0.92rem;
    line-height: 1.5;
}
.ch-content thead th {
    background: var(--blue);
    color: #fff;
    font-weight: 600;
    padding: 10px 14px;
    text-align: left;
}
.ch-content tbody td {
    padding: 10px 14px;
    border-bottom: 1px solid #E8E8ED;
}
.ch-content tbody tr:last-child td { border-bottom: none; }
.ch-content tbody tr:nth-child(even) { background: #F8F9FA; }
.ch-content tbody tr:hover { background: #E8F4FD; }

.ch-content blockquote {
    margin: 16px 0 24px;
    padding: 14px 18px;
    background: #FFF8E1;
    border-left: 4px solid #FF9500;
    border-radius: 0 8px 8px 0;
    font-size: 0.95rem;
}
.ch-content blockquote p { margin-bottom: 0; }
.ch-content blockquote p:not(:last-child) { margin-bottom: 8px; }

.ch-content hr {
    display: none;
}

.ch-content img {
    max-width: 100%;
    height: auto;
    border-radius: 10px;
    margin: 8px 0;
    display: block;
}
.ch-content > p > img:only-child {
    margin: 16px auto;
}
/* Vignette for single images (not in mosaic) – use same wrapper as mosaic */
.ch-content .img-solo {
    position: relative;
    overflow: hidden;
    border-radius: 10px;
    margin: 16px 0;
    width: fit-content;
    max-width: 100%;
}
.ch-content .img-solo img {
    display: block;
    margin: 0;
    border-radius: 0;
    max-width: 100%;
    height: auto;
}
.ch-content .img-solo::after {
    content: '';
    position: absolute;
    inset: 0;
    box-shadow: inset 0 0 30px rgba(0,0,0,0.35);
    pointer-events: none;
    border-radius: 10px;
}

.ch-content a {
    color: var(--blue);
    text-decoration: underline;
    text-decoration-thickness: 1px;
    text-underline-offset: 2px;
}
.ch-content a:hover {
    color: var(--blue-dark, #1976D2);
}

/* ── Definition cards (glossary terms) ── */
.ch-content .def {
    padding: 12px 16px;
    margin: 0 0 8px;
    background: #F8F9FA;
    border-left: 3px solid var(--blue);
    border-radius: 0 8px 8px 0;
    line-height: 1.6;
    font-size: 0.95rem;
}
.ch-content .def strong {
    color: var(--blue-dark, #1976D2);
}

/* ── Image groups (consecutive images) ── */
/* ── Image mosaic (Facebook-style, in-article) ── */
.ch-content .img-group {
    display: grid;
    gap: 2px;
    margin: 16px -24px 24px;
    overflow: hidden;
    grid-template-columns: repeat(2, 1fr);
    max-height: 280px;
}
.ch-content .img-group p {
    margin: 0;
    overflow: hidden;
    position: relative;
}
.ch-content .img-group p::after {
    content: '';
    position: absolute;
    inset: 0;
    box-shadow: inset 0 0 30px rgba(0,0,0,0.35);
    pointer-events: none;
}
.ch-content .img-group img {
    margin: 0;
    width: 100%;
    height: 100%;
    object-fit: cover;
    border-radius: 0;
    display: block;
    transition: opacity 0.2s;
    cursor: pointer;
}
.ch-content .img-group img:hover {
    opacity: 0.85;
}
/* 2 images: side by side */
.ch-content .img-group[data-count="2"] {
    grid-template-columns: 1fr 1fr;
}
.ch-content .img-group[data-count="2"] p {
    aspect-ratio: 4/3;
}
/* 3 images: 1 big left, 2 stacked right */
.ch-content .img-group[data-count="3"] {
    grid-template-columns: 1fr 1fr;
    grid-template-rows: 1fr 1fr;
}
.ch-content .img-group[data-count="3"] p:first-child {
    grid-row: 1 / 3;
}
/* 4 images: 2x2 */
.ch-content .img-group[data-count="4"] {
    grid-template-columns: 1fr 1fr;
}
.ch-content .img-group[data-count="4"] p {
    aspect-ratio: 4/3;
}
/* 5+ images: 2 top, 3 bottom */
.ch-content .img-group[data-count="5"] {
    grid-template-columns: 1fr 1fr 1fr;
    grid-template-rows: auto auto;
}
.ch-content .img-group[data-count="5"] p:nth-child(1),
.ch-content .img-group[data-count="5"] p:nth-child(2) {
    grid-column: span 1;
}
.ch-content .img-group[data-count="5"] p:nth-child(1) {
    grid-column: 1 / 3;
}
.ch-content .img-group[data-count="5"] p:nth-child(2) {
    grid-column: 3 / 4;
}
.ch-content .img-group[data-count="5"] p {
    aspect-ratio: 4/3;
}
@media (max-width: 500px) {
    .ch-content .img-group {
        grid-template-columns: 1fr 1fr;
        margin-left: -24px;
        margin-right: -24px;
    }
    .ch-content .img-group[data-count="3"] {
        grid-template-columns: 1fr 1fr;
    }
    .ch-content .img-group[data-count="5"] {
        grid-template-columns: 1fr 1fr;
    }
}

/* ── Lightbox ── */
.lightbox {
    display: none;
    position: fixed;
    inset: 0;
    background: rgba(0,0,0,0.9);
    z-index: 2000;
    align-items: center;
    justify-content: center;
    flex-direction: column;
    cursor: pointer;
}
.lightbox.open {
    display: flex;
}
.lightbox img {
    max-width: 90vw;
    max-height: 80vh;
    object-fit: contain;
    border-radius: 4px;
    box-shadow: 0 8px 40px rgba(0,0,0,0.5);
}
.lightbox-caption {
    color: rgba(255,255,255,0.8);
    font-size: 0.9rem;
    margin-top: 12px;
    text-align: center;
    max-width: 80vw;
}

/* ── Chapter navigation (prev/next) ── */
.ch-nav {
    display: flex;
    justify-content: space-between;
    gap: 14px;
    margin: 36px 0 20px;
}
.ch-nav-link {
    display: flex;
    align-items: center;
    gap: 12px;
    padding: 16px 20px;
    background: var(--white);
    border-radius: var(--radius);
    box-shadow: 0 2px 8px rgba(0,0,0,0.06);
    text-decoration: none;
    color: var(--text);
    font-weight: 600;
    font-size: 0.85rem;
    line-height: 1.35;
    transition: background 0.2s, box-shadow 0.2s;
}
.ch-nav-link:hover {
    background: #E8F4FD;
    box-shadow: 0 4px 12px rgba(0,0,0,0.08);
}
.ch-nav-dir { font-size: 1.2rem; color: var(--blue); opacity: 0.5; flex-shrink: 0; }
.ch-nav-img { width: 44px; height: 44px; object-fit: contain; flex-shrink: 0; }
.ch-nav-text { color: var(--text); }
.ch-nav-prev { margin-right: auto; }
.ch-nav-next { margin-left: auto; text-align: right; }
.ch-nav-only-next { margin-left: auto; margin-right: 0; }
@media (max-width: 500px) {
    .ch-nav { flex-direction: column; }
    .ch-nav-next { text-align: left; }
}

/* ── Nudge / CTA blocks ── */
.book-nudge {
    background: linear-gradient(135deg, #1976D2 0%, #1565C0 100%);
    border-radius: 0;
    padding: 32px 24px;
    text-align: center;
    color: #fff;
    margin-bottom: 16px;
}
.book-nudge strong {
    display: block;
    font-size: 1.2rem;
    margin-bottom: 6px;
}
.book-nudge p {
    opacity: 0.9;
    margin-bottom: 16px;
}
.book-nudge-btn {
    display: inline-flex;
    align-items: center;
    gap: 15px;
    padding: 18px 35px;
    background: linear-gradient(135deg, #667eea, #764ba2);
    color: #fff;
    border: none;
    border-radius: 60px;
    font-size: 1.1rem;
    font-weight: 600;
    cursor: pointer;
    text-decoration: none;
    transition: all 0.3s;
    box-shadow: 0 10px 30px rgba(102, 126, 234, 0.4);
    position: relative;
    overflow: hidden;
}
.book-nudge-btn::before {
    content: '';
    position: absolute;
    top: 0;
    left: -100%;
    width: 100%;
    height: 100%;
    background: linear-gradient(90deg, transparent, rgba(255,255,255,0.2), transparent);
    transition: left 0.5s;
}
.book-nudge-btn:hover::before {
    left: 100%;
}
.book-nudge-btn:hover {
    transform: translateY(-5px) scale(1.05);
    box-shadow: 0 20px 40px rgba(102, 126, 234, 0.6);
}

/* ── Footer ── */
.book-footer {
    text-align: center;
    padding: 20px;
    color: var(--text-secondary);
    font-size: 0.85rem;
}
