/* Reset and base styles */
*,
*::before,
*::after {
    box-sizing: border-box;
}

:root {
    --bg: #ffffff;
    --text: #222222;
    --muted: #5f6368;
    --line: #e7e7e7;
    --link: #1f5ea8;
    --link-hover: #174a84;
    --surface: #f7f8fa;
    --quote: #eceff3;
}

html {
    font-size: 17px;
}

body {
    margin: 0;
    font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Helvetica, Arial, sans-serif;
    line-height: 1.65;
    color: var(--text);
    background: var(--bg);
}

a {
    color: var(--link);
}

a:hover {
    color: var(--link-hover);
}

/* Global page frame */
header,
main,
footer {
    width: min(1120px, 100% - 2rem);
    margin: 0 auto;
}

/* Header styles */
header {
    padding: 1.5rem 0;
    border-bottom: 1px solid var(--line);
}

nav {
    display: flex;
    align-items: center;
    justify-content: space-between;
    gap: 1rem;
}

nav > a {
    color: var(--text);
    text-decoration: none;
    font-weight: 600;
}

/* Language switcher moved from template inline styles */
.language-switcher {
    display: inline-flex;
    align-items: center;
    gap: 0.35rem;
    font-size: 0.95rem;
}

.language-switcher a {
    text-decoration: none;
    color: var(--muted);
    padding: 0.25rem 0.5rem;
    border-radius: 6px;
}

.language-switcher a:hover {
    background: var(--surface);
}

.language-switcher a.active {
    color: var(--text);
    font-weight: 700;
    background: var(--surface);
}

.language-switcher .separator {
    color: #b8bcc2;
}

/* Main content */
main {
    padding: 2rem 0;
    min-height: calc(100vh - 190px);
}

/* Chapter layout */
.chapter-content {
    width: min(860px, 100%);
    margin: 0 auto;
    font-family: Georgia, "Times New Roman", serif;
    line-height: 1.8;
}

.chapter-header {
    text-align: center;
    margin-bottom: 2.25rem;
}

.chapter-header h1 {
    margin: 0;
    line-height: 1.3;
    font-size: clamp(1.85rem, 3.2vw, 2.7rem);
    color: var(--text);
}

.chapter-body {
    font-size: clamp(1.05rem, 1.2vw, 1.2rem);
    margin-bottom: 3rem;
}

.chapter-body p,
.chapter-body ul,
.chapter-body ol,
.chapter-body blockquote {
    margin: 0 0 1.2rem;
}

.chapter-body h1:not(:first-of-type),
.chapter-body h2,
.chapter-body h3,
.chapter-body h4 {
    line-height: 1.35;
    margin: 2rem 0 0.9rem;
    color: #171717;
}

.chapter-body h2 {
    font-size: clamp(1.45rem, 2vw, 1.85rem);
}

.chapter-body h3 {
    font-size: clamp(1.2rem, 1.6vw, 1.45rem);
}

.chapter-body ul,
.chapter-body ol {
    padding-left: 1.4rem;
}

.chapter-body li + li {
    margin-top: 0.35rem;
}

.chapter-body blockquote {
    padding: 0.9rem 1.15rem;
    border-left: 4px solid #8f98a3;
    background: var(--quote);
}

.chapter-body img {
    max-width: 100%;
    height: auto;
}

.chapter-figure {
    margin: 1rem 0 1.35rem;
    padding: clamp(0.45rem, 1.2vw, 0.65rem);
    border: 1px solid var(--line);
    border-radius: 14px;
    background: var(--surface);
    max-width: min(520px, 100%);
    margin-left: auto;
    margin-right: auto;
}

.chapter-figure img {
    display: block;
    width: 100%;
    max-height: min(340px, 42vh);
    object-fit: contain;
    border-radius: 10px;
}

/* Make markdown tables readable on narrow screens */
.chapter-body table {
    width: 100%;
    border-collapse: collapse;
    border: 1px solid var(--line);
    font-size: 0.95em;
    margin: 1.1rem 0 1.4rem;
    display: block;
    overflow-x: auto;
    white-space: nowrap;
}

.chapter-body th,
.chapter-body td {
    border: 1px solid var(--line);
    padding: 0.55rem 0.75rem;
    text-align: left;
}

.chapter-body thead th {
    background: var(--surface);
}

.chapter-navigation {
    margin: 1.8rem 0;
    padding: 0.95rem 0;
    border-top: 1px solid var(--line);
    border-bottom: 1px solid var(--line);
}

.nav-links {
    display: flex;
    justify-content: space-between;
    align-items: center;
    gap: 0.5rem;
}

.prev-chapter,
.next-chapter {
    display: inline-flex;
    align-items: center;
    gap: 0.35rem;
    text-decoration: none;
    color: var(--muted);
    padding: 0.45rem 0.7rem;
    border-radius: 6px;
    transition: background-color 0.2s ease, color 0.2s ease;
}

.prev-chapter:hover,
.next-chapter:hover {
    color: var(--text);
    background: var(--surface);
}

.nav-arrow {
    font-size: 1.1rem;
    line-height: 1;
}

.nav-text {
    font-size: 0.9rem;
    max-width: 260px;
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
}

/* Footer styles */
footer {
    padding: 1.6rem 0;
    border-top: 1px solid var(--line);
    text-align: center;
    color: var(--muted);
    font-size: 0.9rem;
}

/* Language index & landing (book home) */
.book-container {
    max-width: 800px;
    margin: 0 auto;
    padding: 2rem;
}

.book-header {
    text-align: center;
    margin-bottom: 2.25rem;
}

.book-header .home-lang-links {
    margin-top: 1.25rem;
    font-size: 1rem;
    color: var(--muted);
}

.book-header .home-lang-links a {
    white-space: nowrap;
}

.book-intro {
    margin: 0 auto 2rem;
    max-width: 42rem;
    text-align: left;
    color: var(--text);
    line-height: 1.65;
}

.book-description {
    font-size: 1.15rem;
    color: var(--muted);
    margin-top: 1rem;
}

.table-of-contents {
    margin: 2rem 0;
}

.table-of-contents > h2 {
    margin-bottom: 1.25rem;
    font-size: 1.35rem;
}

.toc-phases {
    display: flex;
    flex-direction: column;
    gap: 1.75rem;
}

.toc-phase-heading {
    margin: 0 0 0.65rem;
    font-size: 1.05rem;
    font-weight: 700;
    letter-spacing: 0.02em;
    text-transform: uppercase;
    color: var(--muted);
}

.toc-phase-chapters.chapters {
    gap: 0.65rem;
}

.chapters {
    display: flex;
    flex-direction: column;
    gap: 1rem;
}

.chapter-item {
    border: 1px solid var(--line);
    border-radius: 8px;
    transition: transform 0.2s ease, box-shadow 0.2s ease;
}

.chapter-item:hover {
    transform: translateX(6px);
    box-shadow: 0 2px 10px rgba(0, 0, 0, 0.06);
}

.chapter-link {
    display: block;
    padding: 1rem;
    text-decoration: none;
    color: inherit;
}

.chapter-title {
    font-weight: 500;
}

/* Chapter duplicate markdown title (layout already shows primary h1) */
.chapter-body > h1:first-of-type {
    font-family: inherit;
    font-size: clamp(1.1rem, 1.75vw, 1.3rem);
    font-weight: 600;
    color: var(--muted);
    margin-top: 0;
    margin-bottom: 1rem;
}

/* Top-of-chapter breadcrumb */
.chapter-breadcrumb {
    margin: 0 0 1.25rem;
    font-size: 0.92rem;
    color: var(--muted);
}

.chapter-breadcrumb a {
    color: var(--muted);
    text-decoration: none;
}

.chapter-breadcrumb a:hover {
    color: var(--link);
}

.chapter-breadcrumb .breadcrumb-sep {
    margin: 0 0.4rem;
    color: #b8bcc2;
}

.chapter-footer .chapter-navigation {
    margin-top: 2.5rem;
    margin-bottom: 0;
}

.chapter-footer .chapter-navigation:last-child {
    border-bottom: none;
}

/* Handbook callouts (optional in Markdown/HTML) */
.callout-key-idea,
.callout-risk,
.callout-checklist,
.callout-example {
    margin: 1.25rem 0;
    padding: 1rem 1.15rem;
    border-radius: 10px;
    border-left: 4px solid var(--line);
    background: var(--surface);
    font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Helvetica, Arial, sans-serif;
    font-size: 0.98em;
    line-height: 1.55;
}

.callout-key-idea {
    border-left-color: #2e6bdc;
    background: #f3f6fc;
}

.callout-risk {
    border-left-color: #c43d3d;
    background: #fcf4f4;
}

.callout-checklist {
    border-left-color: #2d8a54;
    background: #f4faf6;
}

.callout-example {
    border-left-color: #8a5cb8;
    background: #f8f5fc;
}

.callout-key-idea > :first-child,
.callout-risk > :first-child,
.callout-checklist > :first-child,
.callout-example > :first-child {
    margin-top: 0;
}

.callout-key-idea > :last-child,
.callout-risk > :last-child,
.callout-checklist > :last-child,
.callout-example > :last-child {
    margin-bottom: 0;
}

/* Responsive design */
@media (max-width: 768px) {
    html {
        font-size: 16px;
    }

    header,
    main,
    footer {
        width: min(1120px, 100% - 1rem);
    }

    nav {
        flex-direction: row;
        flex-wrap: wrap;
        gap: 0.65rem 1rem;
    }

    .chapter-content {
        width: 100%;
    }

    .chapter-body {
        font-size: 1.08rem;
    }

    .nav-text {
        max-width: 145px;
    }

    .chapter-figure {
        margin: 0.75rem 0 1.1rem;
        padding: 0.4rem;
        max-width: 100%;
    }

    .chapter-figure img {
        max-height: min(260px, 38vh);
    }
}