/* Vazirmatn – self-hosted (OFL-1.1, https://github.com/rastikerdar/vazirmatn) */
@font-face {
    font-family: "Vazirmatn";
    src: url("../fonts/Vazirmatn-wght.woff2") format("woff2");
    font-weight: 100 900;
    font-style: normal;
    font-display: swap;
}

/* ---------------------------------------------------------------------------
   Design tokens
   The accent is the violet of the Sierpiński logo, so the page and the mark
   belong to each other. Change --accent alone to re-tint the whole site.
   --------------------------------------------------------------------------- */
:root {
    --brand: #3e2a62;          /* the logo's purple */
    --accent: #4a3184;         /* links & interactive text (10:1 on white) */
    --accent-deep: #2f2050;    /* headings that carry the brand */
    --accent-tint: #f3f0fa;    /* filled surfaces */
    --accent-tint-strong: #e7e1f6;

    --ink: #14121c;
    --ink-soft: #55516a;
    --ink-faint: #6f6a86;

    --paper: #ffffff;
    --bg: #fcfbfe;
    --line: #e6e3ef;
    --line-strong: #d5cfe6;
}

body {
    font-family: "Vazirmatn", sans-serif;
    line-height: 1.9;
    font-size: 17px;
    direction: rtl;
    text-align: right;
    margin: 0;
    padding: 0;
    background: var(--bg);
    color: var(--ink);
    overflow-x: hidden;
}

/* Persian/Arabic script joins its letters — letter-spacing breaks the joins,
   so type hierarchy here comes from size and weight only, never tracking. */

:focus-visible {
    outline: 2px solid var(--accent);
    outline-offset: 2px;
    border-radius: 3px;
}

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

/* Image zoom / lightbox */
main img {
    cursor: zoom-in;
}

.image-lightbox-backdrop {
    position: fixed;
    top: 0;
    right: 0;
    bottom: 0;
    left: 0;
    width: 100vw;
    height: 100vh;
    background: rgba(15, 23, 42, 0.85);
    display: flex;
    align-items: center;
    justify-content: center;
    z-index: 1000;
    box-sizing: border-box;
    padding: 16px;
}

.image-lightbox-img {
    max-width: 100%;
    max-height: 100%;
    display: block;
    box-shadow: 0 20px 45px rgba(0, 0, 0, 0.5);
    border-radius: 10px;
}

/* Header / logo */
.site-header {
    background: #ffffff;
    border-bottom: 1px solid var(--line);
}

.site-header-inner {
    max-width: 1100px;
    margin: 0 auto;
    padding: 16px;
    display: flex;
    align-items: center;
    gap: 12px;
}
.site-brand {
    display: flex;
    align-items: center;
    gap: 12px;
}

.site-logo-link {
    display: inline-flex;
}

.site-logo {
    width: 40px;
    height: 40px;
    object-fit: contain;
}

.site-title-group {
    display: flex;
    flex-direction: column;
    gap: 2px;
}

.site-title {
    font-weight: 600;
    font-size: 18px;
    color: #111111;
}

.site-subtitle {
    font-size: 14px;
    color: #555555;
}

.site-footer {
    border-top: 1px solid var(--line);
    margin-top: 32px;
    background: var(--accent-tint);
}

.site-footer-inner {
    max-width: 1100px;
    margin: 0 auto;
    padding: 12px 16px 20px 16px;
    font-size: 14px;
    color: #555555;
    display: flex;
    align-items: center;
    gap: 8px;
}

.site-footer-label {
    font-weight: 500;
}

.social-link {
    color: var(--accent);
}

/* Side menu / index nav */
.site-body {
    display: flex;
    flex-direction: row;
    max-width: 1100px;
    margin: 0 auto;
    position: relative;
}

.side-nav {
    flex-shrink: 0;
    width: 260px;
    padding: 20px 14px 20px 14px;
    position: sticky;
    top: 0;
    max-height: 100vh;
    overflow-y: auto;
    border-left: 1px solid var(--line);
}

.side-nav-inner {
    display: flex;
    flex-direction: column;
    gap: 8px;
}

.side-nav-title {
    font-weight: 600;
    font-size: 14px;
    color: #333;
    margin-bottom: 8px;
}

.side-nav-list {
    list-style: none;
    padding: 0;
    margin: 0;
}

.side-nav-list li {
    margin: 0;
    padding: 0;
}

.side-nav-list a {
    display: block;
    padding: 6px 0;
    font-size: 14px;
    line-height: 1.55;
    color: #444;
}

.side-nav-list a:hover {
    color: var(--accent);
}

.side-nav-list a.active {
    color: var(--accent);
    font-weight: 500;
}

/* Side menu: topic groups */
.side-nav-groups {
    list-style: none;
    padding: 0;
    margin: 0;
}

.side-nav-group + .side-nav-group {
    margin-top: 18px;
    padding-top: 14px;
    border-top: 1px solid var(--line);
}

.side-nav-group-title {
    display: block;
    font-size: 13px;
    font-weight: 600;
    color: #222;
    margin-bottom: 4px;
}

a.side-nav-group-title:hover {
    color: var(--accent);
}

a.side-nav-group-title.active {
    color: var(--accent);
}

.side-nav-group .side-nav-list a {
    padding: 5px 0;
    color: #555;
}

/* Ordered-series marker: the note's number in front of its title */
.lesson-num {
    display: inline-block;
    min-width: 1.35em;
    margin-inline-end: 6px;
    color: var(--ink-faint);
    font-size: 0.9em;
    font-weight: 500;
}

.side-nav-list a.active .lesson-num {
    color: var(--accent);
}

.side-nav-close,
.nav-toggle {
    display: none;
}

.side-nav-overlay {
    display: none;
}

/* Mobile: hide side nav by default, show toggle */
@media (max-width: 900px) {
    .site-body {
        max-width: 100%;
    }

    .nav-toggle {
        display: flex;
        align-items: center;
        justify-content: center;
        width: 44px;
        height: 44px;
        padding: 0;
        border: 1px solid var(--line);
        border-radius: 8px;
        background: #fff;
        cursor: pointer;
        margin-left: auto;
        flex-shrink: 0;
    }

    .nav-toggle:hover {
        background: #f5f5f5;
    }

    .nav-toggle-icon {
        width: 20px;
        height: 2px;
        background: #333;
        box-shadow: 0 -6px 0 0 #333, 0 6px 0 0 #333;
    }

    .side-nav {
        position: fixed;
        top: 0;
        bottom: 0;
        right: 0;
        width: 280px;
        max-width: 85vw;
        max-height: none;
        background: #fff;
        border-left: 1px solid var(--line);
        z-index: 1001;
        transform: translateX(100%);
        transition: transform 0.2s ease;
        padding: 16px;
        box-shadow: -4px 0 20px rgba(0, 0, 0, 0.08);
    }

    .side-nav-open {
        transform: translateX(0);
    }

    .side-nav-close {
        display: flex;
        align-items: center;
        justify-content: center;
        width: 40px;
        height: 40px;
        padding: 0;
        border: none;
        border-radius: 8px;
        background: #f0f0f0;
        font-size: 24px;
        line-height: 1;
        cursor: pointer;
        margin-right: auto;
        margin-bottom: 12px;
        color: #333;
    }

    .side-nav-close:hover {
        background: var(--line);
    }

    .side-nav-overlay {
        display: block;
        position: fixed;
        top: 0;
        right: 0;
        bottom: 0;
        left: 0;
        background: rgba(0, 0, 0, 0.3);
        z-index: 1000;
        opacity: 0;
        pointer-events: none;
        transition: opacity 0.2s ease;
    }

    body.side-nav-active .side-nav-overlay {
        opacity: 1;
        pointer-events: auto;
    }
}

@media (max-width: 600px) {
    .site-header-inner {
        flex-direction: row;
        align-items: center;
        gap: 8px;
        padding: 10px 12px;
    }

    /* RTL: menu on the right, logo/title immediately next to it (Wikipedia-style) */
    .nav-toggle {
        margin-left: 0;
        margin-inline-start: 0;
    }

    .site-brand {
        justify-content: flex-start;
        min-width: 0;
        flex: 1;
        min-width: 0;
    }

    /* Compact logo, title, subtitle on mobile (best practice) */
    .site-logo {
        width: 28px;
        height: 28px;
    }

    .site-title-group {
        gap: 0;
    }

    .site-title {
        font-size: 15px;
        font-weight: 600;
    }

    .site-subtitle {
        font-size: 11px;
        line-height: 1.35;
    }

    .site-footer-inner {
        flex-wrap: wrap;
    }
}

main.container {
    flex: 1;
    min-width: 0;
    max-width: 800px;
    margin: 24px auto 40px auto;
    padding: 0 16px 40px 16px;
    word-wrap: break-word;
    overflow-wrap: break-word;
    overflow-x: hidden;
}

h1, h2, h3, h4 {
    font-weight: 600;
    margin-top: 1.5em;
    margin-bottom: 0.5em;
}

p {
    margin: 0.4em 0;
}

ul, ol {
    padding-right: 1.5em;
}

a {
    color: var(--accent);
    text-decoration: none;
}

a:hover {
    text-decoration: underline;
}

code, pre {
    direction: ltr;
    text-align: left;
    font-family: ui-monospace, SFMono-Regular, Menlo, Monaco, Consolas, "Liberation Mono", "Courier New", monospace;
}

pre {
    background: #f0f0f0;
    padding: 10px 12px;
    border-radius: 6px;
    overflow-x: auto;
}

/* Tables */
table {
    border-collapse: collapse;
    margin: 0;
    width: 100%;
}

table th,
table td {
    padding: 4px 6px;
}

/* KaTeX: always LTR and isolate from RTL so superscripts/indices (e.g. n^k) don't flip */
.katex,
.katex-display {
    direction: ltr;
    unicode-bidi: isolate;
}

/* Scrollable display math – KaTeX (faster than MathJax, less first-load jank) */
.katex-display {
    display: block;
    overflow-x: auto;
    overflow-y: hidden;
    max-width: 100%;
    padding: 0.75rem 1rem;
    margin: 1rem 0;
    border: none;
    border-radius: 0.5rem;
    background: var(--bg);
    -webkit-overflow-scrolling: touch;
    text-align: left;
}

.katex-display > .katex {
    min-width: max-content;
}

/* Overflow container for long content (horizontal scroll): scrollbox with overflow-x; only this box scrolls, not the page */
main .scroll-wrapper {
    width: 100%;
    max-width: 100%;
    min-width: 0;
    box-sizing: border-box;
    overflow-x: auto;
    overflow-y: hidden;
    -webkit-overflow-scrolling: touch;
    touch-action: pan-x pinch-zoom;
    padding: 10px 12px;
    margin: 1em 0;
    border-radius: 6px;
    background: #f7f7f7;
    border: 1px solid #e8e8e8;
    direction: ltr;
    text-align: left;
    /* Prevent main's word-wrap from wrapping formula/table; let content overflow and scroll */
    overflow-wrap: normal;
    word-wrap: normal;
}

/* No wrapping anywhere inside the box so content overflows and scrolls */
main .scroll-wrapper * {
    overflow-wrap: normal;
    word-wrap: normal;
}

/* Let content inside wrapper keep its natural width so the box can scroll horizontally */
main .scroll-wrapper > * {
    min-width: min-content;
}

/* KaTeX output in scroll-wrapper: block/inline-block so min-width works and box scrolls */
main .scroll-wrapper > .katex,
main .scroll-wrapper > .katex-display {
    display: inline-block;
}

/* Tables inside wrapper */
main .scroll-wrapper table {
    margin: 0;
    min-width: max-content;
}

/* ---------------------------------------------------------------------------
   Homepage
   Markup in index.md, generated from _data/site_index.yml.
   --------------------------------------------------------------------------- */

/* Hero. The Sierpiński gasket behind the type is the site's own logo mark
   enlarged to texture — the page opens on the thing the name refers to. */
.home-hero {
    position: relative;
    isolation: isolate;
    padding: 18px 0 30px 0;
    margin-bottom: 34px;
    border-bottom: 1px solid var(--line);
}

/* Sits inside the container's padding, not past it: main has overflow-x hidden,
   so a wider bleed would get cut mid-triangle instead of reading as a bleed. */
.home-hero-mark {
    position: absolute;
    z-index: -1;
    top: -10px;
    inset-inline-end: -10px;
    width: 240px;
    height: auto;
    fill: var(--brand);
    opacity: 0.08;
    pointer-events: none;
}

.home-title {
    margin: 0;
    font-size: clamp(2rem, 6vw, 2.9rem);
    font-weight: 800;
    line-height: 1.45;
    color: var(--accent-deep);
}

.home-tagline {
    margin: 2px 0 0 0;
    font-size: clamp(1.05rem, 3vw, 1.3rem);
    font-weight: 500;
    line-height: 1.7;
    color: var(--accent);
}

.home-lede {
    max-width: 34em;
    margin-top: 18px;
    color: var(--ink-soft);
}

.home-lede p {
    margin: 0;
    line-height: 2;
}

.home-lede p + p {
    margin-top: 10px;
}

.home-lede strong {
    font-weight: 700;
    color: var(--ink);
}

.home-stat {
    margin: 16px 0 0 0;
    font-size: 13.5px;
    color: var(--ink-faint);
}

.home-stat-n {
    font-weight: 700;
    color: var(--accent);
}

.home-stat-dot {
    color: var(--line-strong);
    padding: 0 2px;
}

/* A series sits on a small stack of sheets: the shape says "there are more of
   these, in order" before the kicker has to. Nothing else on the page stacks. */
.home-series {
    position: relative;
    padding: 22px 24px 20px 24px;
    border: 1px solid var(--line-strong);
    border-radius: 14px;
    background: linear-gradient(170deg, var(--accent-tint), var(--paper) 55%);
}

.home-series::before,
.home-series::after {
    content: "";
    position: absolute;
    z-index: -1;
    height: 16px;
    border: 1px solid var(--line-strong);
    border-top: none;
    border-radius: 0 0 13px 13px;
    background: var(--paper);
}

.home-series::before {
    inset-inline: 9px;
    bottom: -7px;
}

.home-series::after {
    inset-inline: 21px;
    bottom: -14px;
    opacity: 0.65;
}

.home-series-kicker {
    margin: 0;
    font-size: 13px;
    font-weight: 700;
    color: var(--accent);
}

.home-series-title {
    margin: 2px 0 0 0;
    font-size: clamp(1.4rem, 4vw, 1.75rem);
    font-weight: 800;
    line-height: 1.6;
    color: var(--accent-deep);
}

.home-series-title a {
    color: inherit;
}

.home-series-summary {
    margin: 8px 0 0 0;
    max-width: 42em;
    font-size: 15px;
    line-height: 1.95;
    color: var(--ink-soft);
}

.home-series-list {
    list-style: none;
    padding: 0;
    margin: 18px 0 0 0;
    display: grid;
    gap: 2px;
}

.home-series-list li {
    margin: 0;
    padding: 0;
}

.home-series-list a {
    display: flex;
    align-items: baseline;
    gap: 10px;
    padding: 7px 10px;
    margin-inline-start: -10px;
    border-radius: 8px;
    color: var(--accent);
    transition: background-color 0.15s ease, color 0.15s ease;
}

.home-series-list a:hover,
.home-series-list a:focus-visible {
    text-decoration: none;
    background: var(--accent-tint-strong);
    color: var(--accent-deep);
}

.home-num {
    flex-shrink: 0;
    min-width: 1.5em;
    font-size: 13px;
    font-weight: 700;
    color: var(--accent);
}

.home-series-name {
    font-weight: 500;
    line-height: 1.7;
}

.home-series-actions {
    display: flex;
    flex-wrap: wrap;
    align-items: center;
    gap: 8px 18px;
    margin: 20px 0 0 0;
}

.home-cta {
    display: inline-block;
    padding: 9px 18px;
    border-radius: 999px;
    background: var(--accent);
    color: var(--paper);
    font-size: 15px;
    font-weight: 600;
    transition: background-color 0.15s ease;
}

.home-cta:hover {
    text-decoration: none;
    background: var(--accent-deep);
}

.home-quiet-link {
    font-size: 14px;
    color: var(--ink-faint);
}

.home-quiet-link:hover {
    color: var(--accent);
}

/* The standalone topics */
.home-topics {
    margin-top: 52px;
}

.home-section-title {
    margin: 0;
    font-size: 1.1rem;
    font-weight: 700;
    color: var(--ink);
}

.home-grid {
    display: grid;
    grid-template-columns: repeat(auto-fill, minmax(255px, 1fr));
    gap: 14px;
    margin-top: 18px;
}

.home-card {
    padding: 16px 18px 14px 18px;
    border: 1px solid var(--line);
    border-radius: 12px;
    background: var(--paper);
    transition: border-color 0.15s ease;
}

.home-card:hover {
    border-color: var(--line-strong);
}

.home-card-title {
    display: flex;
    align-items: baseline;
    justify-content: space-between;
    gap: 10px;
    margin: 0;
    font-size: 1.05rem;
    font-weight: 700;
    line-height: 1.6;
    color: var(--accent-deep);
}

.home-card-title a {
    color: inherit;
}

.home-card-count {
    flex-shrink: 0;
    font-size: 12.5px;
    font-weight: 500;
    color: var(--ink-faint);
}

.home-card-summary {
    margin: 6px 0 0 0;
    font-size: 14px;
    line-height: 1.85;
    color: var(--ink-soft);
}

.home-card-list {
    list-style: none;
    padding: 0;
    margin: 12px 0 0 0;
    border-top: 1px solid var(--line);
    padding-top: 6px;
}

.home-card-list li {
    margin: 0;
    padding: 0;
}

.home-card-list a {
    display: block;
    padding: 6px 10px;
    margin-inline: -10px;
    border-radius: 8px;
    font-size: 15px;
    font-weight: 500;
    line-height: 1.7;
    color: var(--accent);
    transition: background-color 0.15s ease, color 0.15s ease;
}

.home-card-list a:hover,
.home-card-list a:focus-visible {
    text-decoration: none;
    background: var(--accent-tint);
    color: var(--accent-deep);
}

@media (max-width: 600px) {
    .home-hero {
        padding-bottom: 24px;
        margin-bottom: 26px;
    }

    .home-hero-mark {
        width: 165px;
        inset-inline-end: -34px;
        opacity: 0.07;
    }

    .home-series {
        padding: 18px 16px 16px 16px;
    }

    .home-cta {
        text-align: center;
    }

    .home-topics {
        margin-top: 40px;
    }
}

@media (prefers-reduced-motion: reduce) {
    * {
        transition-duration: 0.01ms !important;
        animation-duration: 0.01ms !important;
    }
}

/* ---------------------------------------------------------------------------
   Series (mini-courses): landing page + note-page navigation
   Markup from _includes/series-nav.html and <topic>/index.md.
   --------------------------------------------------------------------------- */

/* Course landing page: the lesson list */
.course-toc {
    list-style: none;
    padding: 0;
    margin: 18px 0 0 0;
    display: flex;
    flex-direction: column;
    gap: 10px;
}

.course-toc-item {
    margin: 0;
    padding: 0;
}

.course-toc-link {
    display: flex;
    align-items: flex-start;
    gap: 12px;
    padding: 12px 14px;
    border: 1px solid var(--line);
    border-radius: 10px;
    background: #ffffff;
    color: inherit;
    transition: border-color 0.15s ease, background-color 0.15s ease;
}

.course-toc-link:hover {
    text-decoration: none;
    border-color: var(--accent);
    background: var(--accent-tint);
}

.course-toc-num {
    flex-shrink: 0;
    width: 28px;
    height: 28px;
    display: flex;
    align-items: center;
    justify-content: center;
    border-radius: 50%;
    background: var(--accent-tint-strong);
    color: var(--accent);
    font-size: 14px;
    font-weight: 600;
    line-height: 1;
}

.course-toc-body {
    display: flex;
    flex-direction: column;
    gap: 2px;
    min-width: 0;
}

.course-toc-title {
    font-weight: 600;
    color: var(--accent);
    line-height: 1.7;
}

.course-toc-summary {
    font-size: 14.5px;
    line-height: 1.8;
    color: #444;
}

.course-start {
    margin-top: 14px;
    font-weight: 500;
}

/* Note page: breadcrumb chip above the title */
.series-crumb {
    display: flex;
    align-items: center;
    flex-wrap: wrap;
    gap: 6px;
    font-size: 13.5px;
    color: #777;
}

.series-crumb-sep {
    color: #bbb;
}

.series-crumb + h1 {
    margin-top: 0.35em;
}

/* Note page: previous / next lesson */
.series-nav {
    margin-top: 40px;
    padding-top: 20px;
    border-top: 1px solid var(--line);
}

.series-nav-head {
    margin: 0 0 12px 0;
    font-size: 14px;
    color: #555;
}

.series-nav-links {
    display: grid;
    grid-template-columns: 1fr 1fr;
    gap: 12px;
}

.series-nav-link {
    display: flex;
    flex-direction: column;
    gap: 3px;
    padding: 12px 14px;
    border: 1px solid var(--line);
    border-radius: 10px;
    background: var(--paper);
    color: inherit;
    transition: border-color 0.15s ease, background-color 0.15s ease;
}

.series-nav-link:hover {
    text-decoration: none;
    border-color: var(--accent);
    background: var(--accent-tint);
}

/* RTL: previous sits at the start (right), next at the end (left) */
.series-nav-prev {
    text-align: start;
}

.series-nav-next {
    text-align: end;
}

.series-nav-dir {
    font-size: 13px;
    color: #777;
}

.series-nav-arrow {
    color: var(--accent);
}

.series-nav-name {
    font-size: 15px;
    font-weight: 500;
    color: var(--accent);
    line-height: 1.7;
}

/* Placeholder that keeps the first lesson's «next» card in the left column */
.series-nav-empty {
    border: none;
    background: none;
    padding: 0;
}

@media (max-width: 600px) {
    .series-nav-links {
        grid-template-columns: 1fr;
    }

    .series-nav-empty {
        display: none;
    }

    .series-nav-next {
        text-align: start;
    }
}

/* Fallback: Kramdown .math block (raw LaTeX before KaTeX runs) */
main .math {
    max-width: 100%;
    overflow-x: auto;
    -webkit-overflow-scrolling: touch;
    padding: 10px 12px;
    margin: 1em 0;
    border-radius: 6px;
    background: #f7f7f7;
    border: 1px solid #e8e8e8;
    direction: ltr;
}
