html {
    scroll-behavior: smooth;
}

:root {
    --bg-color: #0d1117;         
    --sidebar-bg: #161b22;     
    --header-bg: #161b22;      
    --card-bg: #21262d;        
    --primary-color: #cc2229;   
    --primary-hover: #f85149;   
    --text-color: #c9d1d9;      
    --text-strong: #f0f6fc;     
    --border-color: #30363d;    
    --sidebar-width: 280px;
    --header-height: 64px;
}

[data-theme="light"] {
    --bg-color: #f6f8fa;
    --sidebar-bg: #ffffff;
    --header-bg: #ffffff;
    --card-bg: #ffffff;
    --primary-color: #cc2229;
    --primary-hover: #a51d21;
    --text-color: #24292f;
    --text-strong: #1b1f24;
    --border-color: #d0d7de;
}

* {
    box-sizing: border-box;
}

body {
    font-family: 'Vazirmatn', sans-serif;
    background-color: var(--bg-color);
    color: var(--text-color);
    line-height: 1.8;
    margin: 0;
    padding: 0;
    -webkit-font-smoothing: antialiased;
    transition: background-color 0.3s ease, color 0.3s ease;
}

.doc-section img {
    max-width: 100%;
    height: auto;
    display: block;
    margin: 30px auto;
    border-radius: 6px;
}

/* ---------- Top header ---------- */
.site-header {
    position: sticky;
    top: 0;
    z-index: 1002;
    height: var(--header-height);
    background-color: var(--header-bg);
    border-bottom: 1px solid var(--border-color);
    transition: background-color 0.3s ease, border-color 0.3s ease, box-shadow 0.25s ease;
}

.site-header.scrolled {
    box-shadow: 0 6px 20px rgba(0, 0, 0, 0.25);
}

.header-inner {
    max-width: 1600px;
    height: 100%;
    margin: 0 auto;
    padding: 0 20px;
    display: flex;
    align-items: center;
    gap: 18px;
    width: 100%;
    position: relative;
}

.site-brand {
    display: flex;
    align-items: center;
    gap: 10px;
    text-decoration: none;
    flex-shrink: 0;
}

.brand-mark {
    width: 36px;
    height: 36px;
    border-radius: 9px;
    background: linear-gradient(135deg, var(--primary-hover), var(--primary-color));
    color: #fff;
    font-size: 0.8rem;
    font-weight: 900;
    letter-spacing: 0.5px;
    display: flex;
    align-items: center;
    justify-content: center;
    user-select: none;
}

.brand-text {
    display: flex;
    flex-direction: column;
    line-height: 1.25;
}

.brand-name {
    color: var(--text-strong);
    font-size: 1.05rem;
    font-weight: 700;
    white-space: nowrap;
}

.brand-tagline {
    color: var(--primary-color);
    font-size: 0.68rem;
    font-weight: 700;
    letter-spacing: 1.5px;
}

.header-actions {
    display: flex;
    align-items: center;
    gap: 12px;
    margin-inline-start: auto;
    flex-shrink: 0;
}

.header-socials {
    display: flex;
    align-items: center;
    gap: 6px;
}

.header-separator {
    width: 1px;
    height: 18px;
    background-color: var(--border-color);
    flex-shrink: 0;
    transition: background-color 0.3s ease;
}

.header-link {
    color: var(--text-color);
    text-decoration: none;
    font-size: 0.88rem;
    padding: 6px;
    border-radius: 8px;
    white-space: nowrap;
    transition: color 0.2s ease, background-color 0.2s ease, transform 0.15s ease;
    display: inline-flex;
    align-items: center;
    justify-content: center;
    line-height: 1;
}

.header-link:hover {
    color: var(--primary-hover);
    background-color: rgba(204, 34, 41, 0.1);
}

.header-link svg {
    width: 20px;
    height: 20px;
    display: block;
}

.social-icon {
    display: inline-block;
    vertical-align: middle;
}

/* ---------- Language toggle ---------- */
.lang-toggle {
    display: inline-flex;
    align-items: center;
    background-color: var(--card-bg);
    border: 1px solid var(--border-color);
    border-radius: 24px;
    height: 32px;
    padding: 2px;
    box-sizing: border-box;
    gap: 3px;
    flex-shrink: 0;
    transition: background-color 0.3s ease, border-color 0.3s ease;
    touch-action: manipulation;
}

.lang-toggle:hover {
    border-color: var(--primary-hover);
}

.lang-btn {
    border: none;
    background: transparent;
    color: var(--text-color);
    font-family: inherit;
    font-size: 0.78rem;
    font-weight: 700;
    line-height: 1;
    height: 26px;
    min-width: 28px;
    padding: 0 8px;
    border-radius: 14px;
    cursor: pointer;
    transition: background-color 0.2s ease, color 0.2s ease, box-shadow 0.2s ease;
    display: inline-flex;
    align-items: center;
    justify-content: center;
    user-select: none;
    touch-action: manipulation;
}

.lang-btn:hover:not(.active) {
    color: var(--text-strong);
    background-color: rgba(255, 255, 255, 0.08);
}

.lang-btn:focus-visible {
    outline: 2px solid var(--primary-hover);
    outline-offset: 1px;
}

.lang-btn.active {
    background-color: var(--primary-color);
    color: #ffffff;
    box-shadow: 0 1px 3px rgba(204, 34, 41, 0.35);
}

[data-theme="light"] .lang-toggle {
    background-color: #eaecf0;
    border-color: #d0d7de;
}

[data-theme="light"] .lang-btn:hover:not(.active) {
    background-color: rgba(0, 0, 0, 0.06);
}

/* ---------- Language Visibility ---------- */
html[lang="fa"] .lang-en {
    display: none !important;
}

html[lang="en"] .lang-fa {
    display: none !important;
}

html[lang="en"] .doc-section p {
    text-align: left;
}

/* ---------- Theme toggle ---------- */
.theme-toggle {
    margin: 0;
    display: flex;
    align-items: center;
    flex-shrink: 0;
}

.theme-toggle .switch {
    position: relative;
    display: inline-flex;
    align-items: center;
    width: 52px;
    height: 30px;
    cursor: pointer;
    user-select: none;
    touch-action: manipulation;
}

.theme-toggle input {
    position: absolute;
    opacity: 0;
    width: 0;
    height: 0;
    margin: 0;
    pointer-events: none;
}

.theme-toggle .slider {
    position: absolute;
    inset: 0;
    cursor: pointer;
    background-color: var(--card-bg);
    border: 1px solid var(--border-color);
    transition: background-color 0.3s ease, border-color 0.3s ease, box-shadow 0.2s ease;
    border-radius: 24px;
    display: flex;
    align-items: center;
    justify-content: space-between;
    padding: 0 6px;
    direction: ltr; /* Keeps coordinates predictable regardless of page direction */
}

.theme-toggle .switch:hover .slider {
    border-color: var(--primary-hover);
}

.theme-toggle input:focus-visible + .slider {
    outline: 2px solid var(--primary-hover);
    outline-offset: 2px;
}

.theme-toggle .slider:before {
    position: absolute;
    content: "";
    height: 22px;
    width: 22px;
    left: 3px;
    bottom: 3px;
    background-color: #ffffff;
    transition: transform 0.25s cubic-bezier(0.4, 0, 0.2, 1), background-color 0.3s ease, box-shadow 0.3s ease;
    border-radius: 50%;
    box-shadow: 0 1px 3px rgba(0, 0, 0, 0.4);
    z-index: 2;
}

.theme-toggle input:checked + .slider {
    background-color: #eaecf0;
    border-color: #d0d7de;
}

.theme-toggle input:checked + .slider:before {
    transform: translateX(22px);
    background-color: var(--primary-color);
    box-shadow: 0 1px 3px rgba(204, 34, 41, 0.35);
}

.theme-toggle .icon {
    display: inline-flex;
    align-items: center;
    justify-content: center;
    width: 13px;
    height: 13px;
    transition: opacity 0.25s ease, color 0.25s ease;
    user-select: none;
    pointer-events: none;
    z-index: 1;
}

[data-theme="dark"] .icon-sun { opacity: 0; color: #8b949e; }
[data-theme="dark"] .icon-moon { opacity: 1; color: #f0f6fc; }
[data-theme="light"] .icon-sun { opacity: 1; color: var(--primary-color); }
[data-theme="light"] .icon-moon { opacity: 0; color: #57606a; }

/* ---------- Search ---------- */
.header-search {
    position: relative;
    flex: 1 1 auto;
    max-width: 520px;
    min-width: 0;
    margin: 0 4px;
}

.search-mobile-toggle {
    display: none;
    background: transparent;
    border: none;
    color: var(--text-color);
    width: 36px;
    height: 36px;
    border-radius: 50%;
    cursor: pointer;
    align-items: center;
    justify-content: center;
    transition: background-color 0.2s ease, color 0.2s ease;
    touch-action: manipulation;
}

.search-mobile-toggle:hover,
.search-mobile-toggle:focus-visible {
    background-color: rgba(255, 255, 255, 0.08);
    color: var(--primary-hover);
}

[data-theme="light"] .search-mobile-toggle:hover,
[data-theme="light"] .search-mobile-toggle:focus-visible {
    background-color: rgba(0, 0, 0, 0.06);
}

.search-bar-inner {
    position: relative;
    width: 100%;
    display: flex;
    align-items: center;
}

.search-icon {
    position: absolute;
    inset-inline-start: 14px;
    top: 50%;
    transform: translateY(-50%);
    color: var(--text-color);
    opacity: 0.6;
    pointer-events: none;
    display: flex;
    transition: opacity 0.2s ease;
}

.search-mobile-close {
    display: none;
}

.search-input {
    width: 100%;
    height: 40px;
    font-family: inherit;
    font-size: 0.9rem;
    color: var(--text-color);
    background-color: var(--bg-color);
    border: 1px solid var(--border-color);
    border-radius: 999px;
    padding-inline-start: 40px;
    padding-inline-end: 16px;
    outline: none;
    transition: border-color 0.2s ease, box-shadow 0.2s ease, background-color 0.3s ease;
}

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

.search-input:focus {
    border-color: var(--primary-color);
    box-shadow: 0 0 0 3px rgba(204, 34, 41, 0.15);
}

.header-search:focus-within .search-icon {
    opacity: 1;
}

.search-input::-webkit-search-cancel-button {
    filter: invert(50%);
    cursor: pointer;
}

.search-results {
    display: none;
    position: absolute;
    top: calc(100% + 8px);
    inset-inline: 0;
    z-index: 1003;
    padding: 6px;
    max-height: min(60vh, 460px);
    overflow-y: auto;
    background-color: var(--card-bg);
    border: 1px solid var(--border-color);
    border-radius: 10px;
    box-shadow: 0 14px 34px rgba(0, 0, 0, 0.4);
    transition: background-color 0.3s ease, border-color 0.3s ease;
}

.search-results.open {
    display: block;
    animation: fadeIn 0.15s ease-in-out;
}

.search-result {
    display: block;
    padding: 10px 12px;
    border-radius: 8px;
    text-decoration: none;
    color: var(--text-color);
    transition: background-color 0.15s ease;
}

.search-result:hover,
.search-result.active {
    background-color: rgba(204, 34, 41, 0.1);
}

.search-result-title {
    color: var(--text-strong);
    font-size: 0.95rem;
    font-weight: 500;
    line-height: 1.6;
}

.search-result-snippet {
    margin-top: 4px;
    font-size: 0.8rem;
    line-height: 1.7;
    opacity: 0.75;
    display: -webkit-box;
    -webkit-line-clamp: 2;
    -webkit-box-orient: vertical;
    overflow: hidden;
}

.search-result mark {
    background-color: rgba(204, 34, 41, 0.25);
    color: var(--text-strong);
    border-radius: 3px;
    padding: 0 2px;
}

.search-empty {
    padding: 16px 12px;
    text-align: center;
    font-size: 0.85rem;
    opacity: 0.7;
}

/* ---------- Layout ---------- */
.layout-wrapper {
    display: flex;
    min-height: calc(100vh - var(--header-height));
    max-width: 1600px;
    margin: 0 auto;
}

.sidebar {
    width: var(--sidebar-width);
    background-color: var(--sidebar-bg);
    border-inline-end: 1px solid var(--border-color);
    position: sticky;
    top: var(--header-height);
    height: calc(100vh - var(--header-height));
    padding: 30px 20px;
    overflow-y: auto;
    display: flex;
    flex-direction: column;
    transition: background-color 0.3s ease, border-color 0.3s ease;
}

.sidebar-menu {
    list-style: none;
    padding: 0;
    margin: 0;
}

.sidebar-menu li {
    margin-bottom: 8px;
}

.sidebar-menu a {
    display: block;
    color: var(--text-color);
    text-decoration: none;
    padding: 10px 15px;
    border-radius: 6px;
    font-size: 0.95rem;
    transition: all 0.2s ease;
}

.sidebar-menu a:hover, .sidebar-menu a.active {
    background-color: rgba(204, 34, 41, 0.1);
    color: var(--primary-hover);
    padding-inline-start: 20px;
}

.sidebar-menu a.active {
    border-inline-start: 3px solid var(--primary-color);
}

/* Only shown in the mobile drawer, where the header socials are hidden */
.sidebar-footer {
    display: none;
    margin-top: auto;
    padding-top: 20px;
    border-top: 1px solid var(--border-color);
    font-size: 0.85rem;
    text-align: center;
    transition: border-color 0.3s ease;
}

.sidebar-footer a {
    color: var(--text-strong);
    text-decoration: none;
    margin: 0 5px;
    display: inline-flex;
    align-items: center;
    gap: 4px;
}

.sidebar-footer a:hover {
    color: var(--primary-color);
}

.main-content {
    flex: 1;
    min-width: 0;
    padding: 40px 50px;
}

.doc-section {
    min-width: 0;
    max-width: 100%;
    background-color: var(--card-bg);
    border-radius: 8px;
    padding: 40px;
    margin-bottom: 40px;
    border: 1px solid var(--border-color);
    animation: fadeIn 0.4s ease-in-out;
    transition: background-color 0.3s ease, border-color 0.3s ease, color 0.3s ease;
}

.doc-section pre {
    overflow-x: auto;
    max-width: 100%;
    border-radius: 8px;
    padding: 16px;
    background-color: rgba(0, 0, 0, 0.25);
    border: 1px solid var(--border-color);
    direction: ltr;
    text-align: left;
}

[data-theme="light"] .doc-section pre {
    background-color: #f6f8fa;
}

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

/* Style Markdown-generated child items neatly inside the template wrapper */
.doc-section h1 {
    color: var(--text-strong);
    margin-top: 0;
    font-size: 2rem;
    font-weight: 700;
    padding-bottom: 15px;
    border-bottom: 1px solid var(--border-color);
    transition: color 0.3s ease, border-color 0.3s ease;
}

.doc-section h2 {
    color: var(--text-strong);
    font-size: 1.4rem;
    margin-top: 35px;
    border-inline-start: 3px solid var(--primary-color);
    padding-inline-start: 10px;
    transition: color 0.3s ease, border-color 0.3s ease;
}

.doc-section h3 {
    color: var(--text-strong);
    font-size: 1.15rem;
    margin-top: 25px;
    transition: color 0.3s ease;
}

.doc-section p {
    text-align: justify;
    font-size: 1.05rem;
    color: var(--text-color);
    transition: color 0.3s ease;
}

.doc-section ul {
    padding-inline-start: 25px;
    padding-inline-end: 0;
}

.doc-section li {
    margin-bottom: 10px;
    font-size: 1.05rem;
}

.doc-section strong {
    color: var(--text-strong);
}

.doc-section a {
    color: var(--primary-hover);
    text-decoration: none;
}

/* Callouts / Blockquotes */
.doc-section blockquote {
    background: rgba(204, 34, 41, 0.05); 
    border: 1px dashed rgba(255, 69, 69, 0.4); 
    border-radius: 8px; 
    padding: 2px 25px; 
    margin: 25px 0;
}

.hamburger-btn {
    display: none;
    background: transparent;
    border: none;
    width: 40px;
    height: 40px;
    cursor: pointer;
    border-radius: 8px;
    padding: 0;
    align-items: center;
    justify-content: center;
    color: var(--text-strong);
    transition: background-color 0.2s ease;
    flex-shrink: 0;
    touch-action: manipulation;
}

.hamburger-btn:hover,
.hamburger-btn:focus-visible {
    background-color: rgba(255, 255, 255, 0.08);
}

[data-theme="light"] .hamburger-btn:hover,
[data-theme="light"] .hamburger-btn:focus-visible {
    background-color: rgba(0, 0, 0, 0.06);
}

.hamburger-icon {
    width: 20px;
    height: 14px;
    position: relative;
    display: flex;
    flex-direction: column;
    justify-content: space-between;
}

.hamburger-icon .bar {
    display: block;
    width: 100%;
    height: 2px;
    background-color: currentColor;
    border-radius: 2px;
    transition: transform 0.25s ease, opacity 0.2s ease;
}

.hamburger-btn[aria-expanded="true"] .bar:nth-child(1) {
    transform: translateY(6px) rotate(45deg);
}

.hamburger-btn[aria-expanded="true"] .bar:nth-child(2) {
    opacity: 0;
}

.hamburger-btn[aria-expanded="true"] .bar:nth-child(3) {
    transform: translateY(-6px) rotate(-45deg);
}

.sidebar-backdrop {
    display: none;
}

@media (max-width: 900px) {
    .layout-wrapper {
        flex-direction: column;
    }

    .hamburger-btn {
        display: inline-flex;
    }

    .sidebar {
        position: fixed;
        top: var(--header-height);
        width: 290px;
        height: calc(100vh - var(--header-height));
        z-index: 1000;
        border-inline-start: 1px solid var(--border-color);
        box-shadow: 0 0 30px rgba(0,0,0,0.6);
        transition: transform 0.25s cubic-bezier(0.4, 0, 0.2, 1);
    }

    html[dir="rtl"] .sidebar {
        right: 0;
        left: auto;
        transform: translateX(100%);
    }

    html[dir="rtl"] .sidebar.open {
        transform: translateX(0);
    }

    html[dir="ltr"] .sidebar {
        left: 0;
        right: auto;
        transform: translateX(-100%);
    }

    html[dir="ltr"] .sidebar.open {
        transform: translateX(0);
    }

    .sidebar-menu li {
        margin-bottom: 6px;
    }

    .sidebar-menu a {
        min-height: 46px;
        display: flex;
        align-items: center;
        padding: 12px 16px;
        font-size: 0.98rem;
        border-radius: 8px;
        touch-action: manipulation;
    }

    .sidebar-footer {
        display: flex;
        gap: 10px;
        margin-top: auto;
        padding-top: 20px;
        border-top: 1px solid var(--border-color);
    }

    .sidebar-social-btn {
        flex: 1;
        display: inline-flex;
        align-items: center;
        justify-content: center;
        gap: 8px;
        min-height: 44px;
        padding: 8px 12px;
        background-color: var(--card-bg);
        border: 1px solid var(--border-color);
        border-radius: 8px;
        color: var(--text-color);
        font-size: 0.88rem;
        font-weight: 500;
        text-decoration: none;
        transition: all 0.2s ease;
        touch-action: manipulation;
    }

    .sidebar-social-btn:hover,
    .sidebar-social-btn:active {
        background-color: rgba(204, 34, 41, 0.1);
        border-color: var(--primary-hover);
        color: var(--primary-hover);
    }

    .main-content {
        padding: 24px 20px 48px 20px;
    }
    
    .doc-section {
        padding: 30px 22px;
    }
}

@media (max-width: 768px) {
    .header-inner {
        gap: 10px;
        padding: 0 12px;
    }

    /* Socials and separator live in the drawer footer on mobile */
    .header-socials,
    .header-separator {
        display: none;
    }

    .brand-tagline {
        display: none;
    }

    /* Mobile Search Activation */
    .header-actions {
        margin-inline-start: 0;
    }

    .header-search {
        flex: 0 0 auto;
        max-width: none;
        margin: 0;
        margin-inline-start: auto;
    }

    .search-mobile-toggle {
        display: inline-flex;
    }

    .search-bar-inner {
        display: none;
    }

    .header-search.mobile-open {
        position: static;
    }

    .header-search.mobile-open .search-bar-inner {
        display: flex;
        position: absolute;
        inset: 0;
        z-index: 1005;
        background-color: var(--header-bg);
        backdrop-filter: blur(16px);
        padding: 0 10px;
        gap: 8px;
        align-items: center;
        width: 100%;
        box-sizing: border-box;
        animation: fadeIn 0.15s ease-out;
    }

    .header-search.mobile-open .search-input {
        height: 42px;
        font-size: 0.95rem;
        flex: 1;
        padding-inline-start: 40px;
        padding-inline-end: 12px;
    }

    .header-search.mobile-open .search-mobile-close {
        display: inline-flex;
        align-items: center;
        justify-content: center;
        width: 38px;
        height: 38px;
        border: none;
        background: transparent;
        color: var(--text-color);
        border-radius: 8px;
        cursor: pointer;
        flex-shrink: 0;
        touch-action: manipulation;
    }

    .header-search.mobile-open .search-mobile-close:hover,
    .header-search.mobile-open .search-mobile-close:focus-visible {
        background-color: rgba(255, 255, 255, 0.08);
        color: var(--text-strong);
    }

    [data-theme="light"] .header-search.mobile-open .search-mobile-close:hover {
        background-color: rgba(0, 0, 0, 0.06);
    }

    .header-search.mobile-open .search-results {
        position: fixed;
        top: var(--header-height);
        inset-inline: 0;
        bottom: 0;
        max-height: none;
        width: 100%;
        border-radius: 0;
        border: none;
        border-top: 1px solid var(--border-color);
        background-color: var(--bg-color);
        z-index: 1004;
        padding: 14px 14px 40px 14px;
        box-shadow: none;
        overflow-y: auto;
        -webkit-overflow-scrolling: touch;
    }

    .search-result {
        padding: 14px 14px;
        margin-bottom: 8px;
        background-color: var(--card-bg);
        border: 1px solid var(--border-color);
        border-radius: 10px;
    }

    .search-result-title {
        font-size: 1rem;
    }
}

@media (max-width: 560px) {
    .site-header {
        height: 58px;
    }

    :root {
        --header-height: 58px;
    }

    .header-inner {
        padding: 0 10px;
        gap: 6px;
    }

    .hamburger-btn {
        width: 38px;
        height: 38px;
    }

    .brand-mark {
        width: 30px;
        height: 30px;
        font-size: 0.75rem;
    }

    .brand-name {
        font-size: 0.88rem;
    }

    .header-actions {
        gap: 6px;
    }

    .lang-toggle {
        height: 30px;
        padding: 2px;
    }

    .lang-btn {
        height: 24px;
        min-width: 26px;
        padding: 0 6px;
        font-size: 0.72rem;
    }

    .theme-toggle .switch {
        width: 48px;
        height: 28px;
    }

    .theme-toggle .slider:before {
        height: 20px;
        width: 20px;
    }

    .theme-toggle input:checked + .slider:before {
        transform: translateX(20px);
    }

    .search-mobile-toggle {
        width: 34px;
        height: 34px;
    }

    .main-content {
        padding: 16px 12px 36px 12px;
    }

    .doc-section {
        padding: 22px 14px;
        border-radius: 10px;
    }

    .doc-section h1 {
        font-size: 1.55rem;
        line-height: 1.35;
        margin-bottom: 20px;
    }

    .doc-section h2 {
        font-size: 1.25rem;
        margin-top: 28px;
    }

    .doc-section h3 {
        font-size: 1.1rem;
    }

    .doc-section p,
    .doc-section li {
        font-size: 0.96rem;
        line-height: 1.75;
    }

    .doc-section pre {
        padding: 12px 10px;
        font-size: 0.82rem;
        border-radius: 6px;
        -webkit-overflow-scrolling: touch;
    }

    .doc-section table {
        display: block;
        width: 100%;
        overflow-x: auto;
        -webkit-overflow-scrolling: touch;
    }

    .doc-section blockquote {
        padding: 2px 16px;
        margin: 18px 0;
    }

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

@media (max-width: 375px) {
    .header-inner {
        padding: 0 6px;
        gap: 4px;
    }

    .header-actions {
        gap: 4px;
    }

    .brand-name {
        font-size: 0.82rem;
    }

    .brand-mark {
        width: 28px;
        height: 28px;
    }
}
