/* ============================================
   Heart Talk Book - Premium Typography Styles
   ============================================ */

:root {
    --book-bg: #fefefe;
    --page-bg: #ffffff;
    --text-primary: #2c2c2c;
    --text-secondary: #666;
    --accent-color: #c54a4a;
    --border-color: #e0e0e0;
    --shadow-light: rgba(0, 0, 0, 0.05);
    --shadow-medium: rgba(0, 0, 0, 0.1);
    --shadow-strong: rgba(0, 0, 0, 0.15);
    --serif: Georgia, "Times New Roman", serif;
    --sans: -apple-system, BlinkMacSystemFont, "Segoe UI", sans-serif;
}

* {
    margin: 0;
    padding: 0;
    box-sizing: border-box;
}

html {
    scroll-behavior: smooth;
}

body {
    font-family: var(--serif);
    font-size: 18px;
    line-height: 1.7;
    color: var(--text-primary);
    background: var(--book-bg);
    background-image:
        linear-gradient(90deg, transparent 79px, var(--border-color) 79px, var(--border-color) 81px, transparent 81px),
        linear-gradient(#f8f8f8 0.5px, transparent 0.5px);
    background-size: 100% 100%, 100% 24px;
    min-height: 100vh;
}

/* ============================================
   Book Container - Page Layout
   ============================================ */

.book-container {
    max-width: 900px;
    margin: 0 auto;
    padding: 40px 20px;
    background: var(--page-bg);
    box-shadow: 0 0 40px var(--shadow-light);
    min-height: 100vh;
}

@media (min-width: 768px) {
    .book-container {
        margin: 20px auto;
        padding: 60px 80px;
        box-shadow:
            0 2px 8px var(--shadow-light),
            0 8px 24px var(--shadow-medium);
        border-radius: 3px;
    }
}

/* ============================================
   Language Toggle
   ============================================ */

.language-toggle {
    position: absolute;
    top: 20px;
    right: 20px;
    display: flex;
    gap: 10px;
    align-items: center;
    font-family: var(--sans);
    z-index: 100;
}

.lang-link {
    padding: 8px 16px;
    text-decoration: none;
    color: var(--text-primary);
    border-radius: 6px;
    transition: all 0.2s;
    font-size: 0.9em;
    border: 1px solid var(--border-color);
}

.lang-link.active {
    background: var(--accent-color);
    color: white;
    border-color: var(--accent-color);
}

.lang-link:hover:not(.active) {
    background: #f5f5f5;
    border-color: var(--accent-color);
}

.language-separator {
    color: var(--text-secondary);
}

@media (max-width: 767px) {
    .language-toggle {
        position: static;
        justify-content: center;
        margin-bottom: 20px;
    }
}

/* ============================================
   Book Header - Index Page
   ============================================ */

.book-header {
    text-align: center;
    padding: 32px 0 48px;
    border-bottom: 2px solid var(--border-color);
    margin-bottom: 48px;
    position: relative;
}

@media (max-width: 767px) {
    .book-header {
        padding: 24px 0 32px;
        margin-bottom: 24px;
    }
}

.book-title {
    font-size: 2.6em;
    font-weight: 400;
    letter-spacing: -0.5px;
    margin-bottom: 16px;
    color: var(--accent-color);
    line-height: 1.2;
}

@media (max-width: 767px) {
    .book-title {
        font-size: 2em;
    }
}

.book-author {
    font-size: 1.3em;
    color: var(--text-secondary);
    font-style: italic;
    margin-bottom: 16px;
}

.book-description {
    font-size: 1em;
    color: var(--text-secondary);
    max-width: 560px;
    margin: 0 auto;
    line-height: 1.6;
}

/* ============================================
   Table of Contents
   ============================================ */

.table-of-contents {
    margin-bottom: 60px;
}

.table-of-contents h2 {
    font-size: 2em;
    font-weight: 400;
    text-align: center;
    margin-bottom: 40px;
    color: var(--text-primary);
}

.toc-grid {
    display: grid;
    grid-template-columns: 1fr;
    gap: 0;
    border-top: 1px solid var(--border-color);
}

@media (min-width: 768px) {
    .toc-grid {
        grid-template-columns: repeat(2, 1fr);
    }
}

.toc-item {
    display: flex;
    flex-direction: column;
    padding: 20px 24px;
    text-decoration: none;
    color: var(--text-primary);
    border-bottom: 1px solid var(--border-color);
    transition: all 0.3s ease;
    background: var(--page-bg);
    position: relative;
}

@media (min-width: 768px) {
    .toc-item:nth-child(odd) {
        border-right: 1px solid var(--border-color);
    }
}

.toc-item:hover {
    background: #f9f9f9;
    transform: translateX(4px);
    box-shadow: -3px 0 0 var(--accent-color);
}

.toc-number {
    font-size: 0.85em;
    color: var(--accent-color);
    font-weight: 600;
    margin-bottom: 6px;
    font-family: var(--sans);
    letter-spacing: 0.5px;
}

.toc-title {
    font-size: 1.05em;
    line-height: 1.4;
}

/* ============================================
   Chapter Header
   ============================================ */

.chapter-header {
    display: flex;
    justify-content: space-between;
    align-items: center;
    gap: 12px;
    padding-bottom: 20px;
    margin-bottom: 40px;
    border-bottom: 1px solid var(--border-color);
    flex-wrap: wrap;
}

.home-link {
    font-family: var(--sans);
    font-size: 0.88em;
    color: var(--accent-color);
    text-decoration: none;
    transition: all 0.2s ease;
    padding: 7px 12px;
    border-radius: 6px;
    border: 1px solid var(--border-color);
    white-space: nowrap;
}

.home-link:hover {
    background: #f5f5f5;
    border-color: var(--accent-color);
}

.chapter-number {
    font-family: var(--sans);
    font-size: 0.82em;
    color: var(--text-secondary);
    font-weight: 600;
    letter-spacing: 1px;
    text-transform: uppercase;
    white-space: nowrap;
}

@media (max-width: 767px) {
    .chapter-header {
        gap: 8px;
    }

    .chapter-number {
        font-size: 0.78em;
    }
}

/* ============================================
   Chapter Content - Beautiful Typography
   ============================================ */

.chapter-content {
    margin-bottom: 60px;
}

.chapter-title {
    font-size: 2.5em;
    font-weight: 400;
    line-height: 1.3;
    margin-bottom: 40px;
    color: var(--text-primary);
    text-align: center;
}

/* Drop cap for first paragraph (optional) */
.chapter-content p:first-of-type::first-letter {
    font-size: 3.5em;
    line-height: 0.9;
    float: left;
    margin: 0.1em 0.1em 0 0;
    color: var(--accent-color);
    font-weight: 400;
}

.chapter-content p {
    margin-bottom: 1.5em;
    text-align: justify;
    hyphens: auto;
}

.chapter-content h2 {
    font-size: 1.8em;
    font-weight: 400;
    margin: 50px 0 20px;
    color: var(--text-primary);
}

.chapter-content h3 {
    font-size: 1.4em;
    font-weight: 400;
    margin: 40px 0 16px;
    color: var(--text-secondary);
}

.chapter-content strong {
    font-weight: 600;
    color: var(--text-primary);
}

.chapter-content em {
    font-style: italic;
}

/* ============================================
   Author Signature Styling
   ============================================ */

/* Horizontal rule before signature */
.chapter-content hr {
    margin: 60px auto 40px;
    max-width: 200px;
    border: none;
    border-top: 1px solid var(--border-color);
}

/* Signature paragraph styling */
.chapter-content hr + p {
    text-align: center;
    font-style: italic;
    color: var(--text-secondary);
    line-height: 1.8;
    margin-top: 0;
}

/* Doctor name in signature */
.chapter-content hr + p strong {
    display: block;
    font-size: 1.1em;
    color: var(--text-primary);
    font-style: normal;
    margin-bottom: 4px;
    font-weight: 600;
}

/* ============================================
   Chapter Navigation - Mobile First
   ============================================ */

/* Mobile: two rows — [prev|next] then [jump dropdown] */
.chapter-navigation {
    margin-top: 60px;
    padding-top: 40px;
    border-top: 2px solid var(--border-color);
    display: flex;
    flex-direction: column;
    gap: 12px;
}

.chapter-nav-arrows {
    display: flex;
    gap: 12px;
}

.chapter-nav-jump {
    display: flex;
    align-items: center;
}

/* Desktop: single row — prev | jump | next */
@media (min-width: 768px) {
    .chapter-navigation {
        flex-direction: row;
        align-items: center;
        gap: 16px;
    }

    .chapter-nav-jump {
        flex: 1;
        justify-content: center;
    }
}

/* Navigation buttons */
.nav-button {
    display: flex;
    flex-direction: column;
    padding: 12px 16px;
    background: #f9f9f9;
    border: 1px solid var(--border-color);
    border-radius: 8px;
    text-decoration: none;
    color: var(--text-primary);
    transition: all 0.25s ease;
    box-shadow: 0 1px 3px var(--shadow-light);
    flex: 1;
    min-width: 0;
}

@media (min-width: 768px) {
    .nav-button {
        flex: none;
        padding: 14px 20px;
        min-width: 130px;
        max-width: 200px;
    }

    .next-button {
        align-items: flex-end;
        text-align: right;
    }
}

.nav-button:hover {
    background: var(--page-bg);
    box-shadow: 0 4px 12px var(--shadow-medium);
    border-color: var(--accent-color);
    transform: translateY(-2px);
}

.nav-label {
    font-family: var(--sans);
    font-size: 0.72em;
    text-transform: uppercase;
    letter-spacing: 1px;
    color: var(--accent-color);
    margin-bottom: 4px;
    font-weight: 700;
}

.nav-title {
    font-size: 0.88em;
    line-height: 1.3;
    overflow: hidden;
    text-overflow: ellipsis;
    display: -webkit-box;
    -webkit-line-clamp: 2;
    -webkit-box-orient: vertical;
}

/* Chapter dropdown selector */
.chapter-select {
    font-family: var(--sans);
    font-size: 0.85em;
    padding: 10px 12px;
    border: 1px solid var(--border-color);
    border-radius: 8px;
    background: var(--page-bg);
    color: var(--text-primary);
    cursor: pointer;
    transition: border-color 0.2s ease, box-shadow 0.2s ease;
    width: 100%;
}

@media (min-width: 768px) {
    .chapter-select {
        font-size: 0.88em;
        padding: 12px 14px;
        max-width: 280px;
        width: auto;
    }
}

.chapter-select:hover {
    border-color: var(--accent-color);
    box-shadow: 0 2px 8px var(--shadow-light);
}

.chapter-select:focus {
    outline: none;
    border-color: var(--accent-color);
    box-shadow: 0 0 0 3px rgba(197, 74, 74, 0.1);
}

/* ============================================
   Footer
   ============================================ */

.book-footer {
    text-align: center;
    padding: 40px 0 20px;
    border-top: 1px solid var(--border-color);
    color: var(--text-secondary);
    font-family: var(--sans);
    font-size: 0.9em;
}

/* ============================================
   PDF Download & Print Actions
   ============================================ */

.chapter-actions {
    display: flex;
    gap: 12px;
    margin: 30px 0;
    padding: 20px;
    background: #f9f9f9;
    border-radius: 8px;
    flex-wrap: wrap;
}

@media (max-width: 767px) {
    .chapter-actions {
        flex-direction: column;
    }
}

.print-button,
.pdf-download {
    flex: 1;
    min-width: 150px;
    padding: 12px 20px;
    border: none;
    border-radius: 6px;
    font-weight: 600;
    cursor: pointer;
    text-decoration: none;
    display: flex;
    align-items: center;
    justify-content: center;
    gap: 8px;
    transition: all 0.2s;
    font-family: var(--sans);
    font-size: 0.95em;
}

.print-button {
    background: white;
    border: 2px solid var(--border-color);
    color: var(--text-primary);
}

.print-button:hover {
    background: #f9f9f9;
    border-color: var(--accent-color);
    transform: translateY(-2px);
    box-shadow: 0 4px 12px var(--shadow-medium);
}

.pdf-download {
    background: var(--accent-color);
    color: white;
}

.pdf-download:hover {
    background: #b03f3f;
    transform: translateY(-2px);
    box-shadow: 0 4px 12px var(--shadow-medium);
}

/* Download Section on Index Page */
.download-section {
    margin: 60px 0;
    padding: 40px;
    background: linear-gradient(135deg, #f5f5f5 0%, #fafafa 100%);
    border-radius: 12px;
}

.download-section h2 {
    font-size: 2em;
    font-weight: 400;
    text-align: center;
    margin-bottom: 40px;
    color: var(--text-primary);
}

.download-grid {
    display: grid;
    grid-template-columns: repeat(auto-fit, minmax(300px, 1fr));
    gap: 30px;
    margin-top: 30px;
}

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

.download-card {
    background: white;
    padding: 40px;
    border-radius: 12px;
    box-shadow: 0 4px 12px var(--shadow-light);
    text-align: center;
    transition: all 0.3s;
}

.download-card:hover {
    box-shadow: 0 8px 24px var(--shadow-medium);
    transform: translateY(-4px);
}

.download-card h3 {
    font-size: 1.5em;
    margin-bottom: 16px;
    color: var(--text-primary);
}

.download-card p {
    color: var(--text-secondary);
    margin-bottom: 12px;
}

.file-info {
    font-size: 0.85em;
    color: var(--accent-color);
    font-weight: 600;
    font-family: var(--sans);
}

.download-btn,
.browse-btn {
    display: inline-block;
    padding: 14px 28px;
    background: var(--accent-color);
    color: white;
    text-decoration: none;
    border-radius: 8px;
    font-weight: 600;
    transition: all 0.3s;
    margin-top: 20px;
    font-family: var(--sans);
}

.download-btn:hover,
.browse-btn:hover {
    background: #b03f3f;
    transform: translateY(-2px);
    box-shadow: 0 6px 20px var(--shadow-medium);
}

.browse-btn {
    background: #666;
}

.browse-btn:hover {
    background: #555;
}

/* ============================================
   Print Styles - Enhanced for PDF Export
   ============================================ */

@media print {
    /* Page setup for A4 */
    @page {
        size: A4;
        margin: 1in 1in 1in 1in;
    }

    body {
        background: white;
        font-size: 12pt;
        line-height: 1.6;
    }

    .book-container {
        box-shadow: none;
        max-width: 100%;
        padding: 0;
        margin: 0;
    }

    /* Chapter page breaks */
    .chapter-content {
        page-break-before: always;
    }

    .chapter-title {
        page-break-after: avoid;
        font-size: 20pt;
        margin-bottom: 0.5in;
    }

    /* Prevent breaks in paragraphs */
    p, blockquote {
        page-break-inside: avoid;
        orphans: 3;
        widows: 3;
        text-align: left;
    }

    /* Headers stay with content */
    h1, h2, h3 {
        page-break-after: avoid;
    }

    /* Lists keep together */
    ul, ol {
        page-break-inside: avoid;
    }

    /* Signature stays together */
    hr + p {
        page-break-before: avoid;
    }

    /* Remove interactive elements */
    .search-button,
    .chapter-navigation,
    .chapter-select,
    .nav-button,
    .book-footer,
    .chapter-header,
    .chapter-actions,
    .print-button,
    .pdf-download,
    .download-section {
        display: none !important;
    }

    /* Show link URLs in footnotes */
    a[href]:after {
        content: " (" attr(href) ")";
        font-size: 10pt;
        color: #666;
    }

    /* Don't show internal navigation URLs */
    a[href^="#"]:after,
    a[href^="/"]:after,
    .home-link:after,
    .glossary-link:after {
        content: "";
    }

    /* Optimize drop cap for print */
    .chapter-content p:first-of-type::first-letter {
        font-size: 3em;
    }
}

/* ============================================
   Animations & Effects
   ============================================ */

@keyframes fadeIn {
    from {
        opacity: 0;
        transform: translateY(20px);
    }
    to {
        opacity: 1;
        transform: translateY(0);
    }
}

.chapter-content {
    animation: fadeIn 0.6s ease-out;
}

/* Smooth page transitions */
.page-transition {
    transition: opacity 0.3s ease;
}

/* Selection styling */
::selection {
    background: rgba(197, 74, 74, 0.2);
    color: var(--text-primary);
}

::-moz-selection {
    background: rgba(197, 74, 74, 0.2);
    color: var(--text-primary);
}

/* ============================================
   Search Functionality
   ============================================ */

/* Search Button */
.search-button {
    padding: 10px 20px;
    background: var(--page-bg);
    border: 1px solid var(--border-color);
    border-radius: 6px;
    cursor: pointer;
    font-size: 0.9em;
    font-family: var(--sans);
    color: var(--text-primary);
    transition: all 0.2s ease;
    font-weight: 500;
    display: inline-flex;
    align-items: center;
    gap: 6px;
}

.search-button:hover {
    background: #f9f9f9;
    border-color: var(--accent-color);
    box-shadow: 0 2px 8px var(--shadow-light);
    transform: translateY(-1px);
}

.search-button:active {
    transform: translateY(0);
}

/* Position in header */
.chapter-header .search-button,
.book-header .search-button {
    position: absolute;
    top: 20px;
    right: 20px;
}

/* Hide header search button on mobile — mobile copy lives in .index-actions */
@media (max-width: 767px) {
    .book-header .search-button:not(.search-button-mobile) {
        display: none;
    }

    .chapter-header .search-button {
        position: static;
        margin-top: 15px;
        width: 100%;
        max-width: 100%;
        justify-content: center;
    }
}

/* Hide the mobile copy on desktop */
.search-button-mobile {
    display: none;
}

@media (max-width: 767px) {
    .search-button-mobile {
        display: inline-flex;
    }
}

/* ============================================
   Glossary Link
   ============================================ */

.glossary-link {
    display: inline-flex;
    align-items: center;
    gap: 6px;
    padding: 10px 20px;
    background: var(--page-bg);
    border: 1px solid var(--border-color);
    border-radius: 6px;
    cursor: pointer;
    font-size: 0.9em;
    font-family: var(--sans);
    color: var(--text-primary);
    text-decoration: none;
    transition: all 0.2s ease;
    font-weight: 500;
    text-align: center;
}

.glossary-link:hover {
    background: #f9f9f9;
    border-color: var(--accent-color);
    box-shadow: 0 2px 8px var(--shadow-light);
    transform: translateY(-1px);
}

.glossary-link:active {
    transform: translateY(0);
}

/* book-header: glossary link flows in document order above the title */
.book-header .glossary-link {
    display: inline-block;
    margin-bottom: 20px;
}

/* chapter-header: flex layout handles positioning naturally */
/* glossary-link sits as leftmost flex item alongside home-link */

@media (max-width: 767px) {
    /* Hide the glossary link in book-header on mobile — it appears in .index-actions instead */
    .book-header .glossary-link {
        display: none;
    }

    .chapter-header .glossary-link {
        display: flex;
        justify-content: center;
        margin-top: 10px;
        margin-bottom: 0;
        width: 100%;
        max-width: 100%;
    }
}

/* ============================================
   Index Actions Bar (above TOC on homepage)
   ============================================ */

.index-actions {
    display: none; /* hidden on desktop — search+glossary live in the header there */
}

@media (max-width: 767px) {
    .index-actions {
        display: flex;
        flex-direction: column;
        gap: 10px;
        margin: 0 0 32px;
    }

    .index-actions .glossary-link,
    .index-actions .search-button {
        display: flex;
        justify-content: center;
        margin: 0;
        width: 100%;
        max-width: none;
        position: static;
    }
}

/* Search Modal */
.search-modal {
    position: fixed;
    top: 0;
    left: 0;
    right: 0;
    bottom: 0;
    z-index: 9999;
    display: flex;
    align-items: flex-start;
    justify-content: center;
    padding-top: 10vh;
}

.search-modal[hidden] {
    display: none;
}

.search-backdrop {
    position: absolute;
    top: 0;
    left: 0;
    right: 0;
    bottom: 0;
    background: rgba(0, 0, 0, 0.6);
    backdrop-filter: blur(4px);
}

.search-container {
    position: relative;
    background: var(--page-bg);
    max-width: 650px;
    width: 90%;
    border-radius: 12px;
    box-shadow: 0 20px 60px rgba(0, 0, 0, 0.3);
    max-height: 80vh;
    display: flex;
    flex-direction: column;
    animation: searchSlideIn 0.3s ease-out;
}

@keyframes searchSlideIn {
    from {
        opacity: 0;
        transform: translateY(-20px);
    }
    to {
        opacity: 1;
        transform: translateY(0);
    }
}

/* Search Header */
.search-header {
    display: flex;
    justify-content: space-between;
    align-items: center;
    padding: 20px 24px;
    border-bottom: 1px solid var(--border-color);
}

.search-title {
    font-size: 1.3em;
    font-weight: 600;
    color: var(--text-primary);
    font-family: var(--sans);
}

.search-close {
    background: none;
    border: none;
    font-size: 2em;
    color: var(--text-secondary);
    cursor: pointer;
    padding: 0;
    width: 32px;
    height: 32px;
    display: flex;
    align-items: center;
    justify-content: center;
    border-radius: 4px;
    transition: all 0.2s ease;
}

.search-close:hover {
    background: #f5f5f5;
    color: var(--text-primary);
}

/* Search Input */
.search-input {
    width: 100%;
    padding: 20px 24px;
    border: none;
    border-bottom: 1px solid var(--border-color);
    font-size: 1.1em;
    font-family: var(--serif);
    color: var(--text-primary);
    background: transparent;
}

.search-input:focus {
    outline: none;
    background: #fafafa;
}

.search-input::placeholder {
    color: var(--text-secondary);
    opacity: 0.7;
}

/* Search Results Container */
.search-results {
    flex: 1;
    overflow-y: auto;
    max-height: 50vh;
}

.search-results:empty {
    display: none;
}

/* Individual Search Result */
.search-result {
    padding: 16px 24px;
    border-bottom: 1px solid var(--border-color);
    cursor: pointer;
    transition: all 0.2s ease;
}

.search-result:hover,
.search-result-selected {
    background: #f9f9f9;
    border-left: 3px solid var(--accent-color);
    padding-left: 21px;
}

.search-result:last-child {
    border-bottom: none;
}

.search-result-number {
    font-size: 0.8em;
    color: var(--accent-color);
    font-weight: 600;
    font-family: var(--sans);
    margin-bottom: 4px;
}

.search-result-title {
    font-weight: 600;
    font-size: 1.05em;
    margin-bottom: 8px;
    color: var(--text-primary);
    font-family: var(--serif);
}

.search-result-context {
    font-size: 0.9em;
    color: var(--text-secondary);
    line-height: 1.5;
    font-family: var(--sans);
}

.search-result-context mark {
    background: #fff8dc;
    padding: 2px 4px;
    border-radius: 2px;
    font-weight: 600;
    color: var(--text-primary);
}

/* Empty States */
.search-no-results,
.search-loading {
    padding: 60px 24px;
    text-align: center;
    color: var(--text-secondary);
}

.search-no-results p:first-child {
    font-size: 1.1em;
    color: var(--text-primary);
    margin-bottom: 8px;
}

.search-hint {
    font-size: 0.9em;
}

/* Search Footer */
.search-footer {
    padding: 12px 24px;
    border-top: 1px solid var(--border-color);
    background: #f9f9f9;
    font-size: 0.85em;
    color: var(--text-secondary);
    font-family: var(--sans);
    text-align: center;
    border-radius: 0 0 12px 12px;
}

.search-footer kbd {
    display: inline-block;
    padding: 3px 8px;
    background: var(--page-bg);
    border: 1px solid var(--border-color);
    border-radius: 4px;
    font-family: monospace;
    font-size: 0.9em;
    margin: 0 2px;
    box-shadow: 0 1px 2px var(--shadow-light);
}

/* Mobile Responsive */
@media (max-width: 767px) {
    .search-modal {
        padding-top: 5vh;
    }

    .search-container {
        width: 95%;
        max-height: 85vh;
    }

    .search-header {
        padding: 16px 20px;
    }

    .search-title {
        font-size: 1.1em;
    }

    .search-input {
        padding: 16px 20px;
        font-size: 1em;
    }

    .search-result {
        padding: 14px 20px;
    }

    .search-footer {
        font-size: 0.8em;
        padding: 10px 20px;
    }
}

/* Print: Hide Search */
@media print {
    .search-button,
    .search-modal {
        display: none !important;
    }
}

/* Reduced Motion */
@media (prefers-reduced-motion: reduce) {
    .search-container {
        animation: none;
    }

    .search-result {
        transition: none;
    }
}
