/* ===== БАЗОВЫЕ СТИЛИ ===== */
* {
    margin: 0;
    padding: 0;
    box-sizing: border-box;
}

:root {
    --bg-body: #0f0e14;
    --bg-header: #1e1a26;
    --bg-sidebar: #1e1a26;
    --bg-content: #0f0e14;
    --bg-card: #1e1a26;
    --bg-code: #0f0e14;
    --bg-hover: rgba(255, 255, 255, 0.05);
    --bg-active: rgba(155, 89, 219, 0.15);
    --text-primary: #e0e0e0;
    --text-secondary: #b0b0b0;
    --text-muted: #888;
    --text-inverse: #0f0e14;
    --border-color: rgba(255, 255, 255, 0.08);
    --shadow: 0 4px 20px rgba(0,0,0,0.4);
    --highlight: #c97bff;
    --highlight-hover: #9b59db;
    --code-border: rgba(255, 255, 255, 0.08);
    --modal-bg: rgba(15, 14, 20, 0.92);
    --sidebar-width: 320px;
    --gallery-max-width: 400px;
    --accent-purple: #9b59db;
    --accent-neon: #c97bff;
}

[data-theme="light"] {
    --bg-body: #f5f5f5;
    --bg-header: #ffffff;
    --bg-sidebar: #ffffff;
    --bg-content: #f5f5f5;
    --bg-card: #ffffff;
    --bg-code: #f0f0f0;
    --bg-hover: rgba(0, 0, 0, 0.05);
    --bg-active: rgba(155, 89, 219, 0.1);
    --text-primary: #1a1a1a;
    --text-secondary: #333333;
    --text-muted: #888;
    --text-inverse: #ffffff;
    --border-color: rgba(0, 0, 0, 0.1);
    --shadow: 0 4px 20px rgba(0,0,0,0.08);
    --highlight: #9b59db;
    --highlight-hover: #7d3c98;
    --code-border: rgba(0, 0, 0, 0.1);
    --modal-bg: rgba(0, 0, 0, 0.85);
}

body {
    font-family: ui-sans-serif, system-ui, -system-ui, -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Oxygen, Ubuntu, Cantarell, "Open Sans", "Helvetica Neue", Helvetica, Arial, sans-serif;
    background: var(--bg-body);
    color: var(--text-primary);
    height: 100vh;
    display: flex;
    flex-direction: column;
    transition: background 0.3s, color 0.3s;
}

/* ===== ШАПКА ===== */
header {
    background: var(--bg-header);
    color: var(--text-primary);
    padding: 10px 0;
    box-shadow: 0 4px 12px rgba(0,0,0,0.3);
    flex-shrink: 0;
    transition: background 0.3s, color 0.3s;
    border-bottom: 1px solid var(--border-color);
}

.container {
    max-width: 100%;
    margin: 0 auto;
    padding: 0 24px;
}

.header-flex {
    display: flex;
    align-items: center;
    justify-content: space-between;
    flex-wrap: wrap;
    gap: 12px;
}

.logo {
    display: flex;
    align-items: center;
    gap: 10px;
    flex-shrink: 0;
}

.logo-icon {
    font-size: 1.8rem;
}

.logo h1 {
    font-size: 2.4rem;
    font-weight: 900;
    background: linear-gradient(to right, #c97bff, #9b59db);
    -webkit-background-clip: text;
    -webkit-text-fill-color: transparent;
    margin: 0;
}

.search-box {
    display: flex;
    flex: 1 1 300px;
    max-width: 500px;
}

.search-box input {
    flex: 1;
    padding: 8px 16px;
    border: 1px solid var(--border-color);
    border-radius: 30px 0 0 30px;
    font-size: 0.95rem;
    outline: none;
    background: var(--bg-body);
    color: var(--text-primary);
    transition: 0.3s;
}

.search-box input:focus {
    border-color: var(--highlight);
    background: var(--bg-content);
}

.search-box input::placeholder {
    color: var(--text-muted);
}

.search-box button {
    padding: 8px 20px;
    border: 1px solid var(--border-color);
    border-left: none;
    background: var(--highlight);
    color: var(--text-inverse);
    font-weight: bold;
    border-radius: 0 30px 30px 0;
    cursor: pointer;
    font-size: 0.95rem;
    transition: 0.2s;
    white-space: nowrap;
}

.search-box button:hover {
    background: var(--highlight-hover);
}

.theme-toggle {
    background: var(--bg-body);
    border: 1px solid var(--border-color);
    color: var(--text-primary);
    padding: 8px 18px;
    border-radius: 30px;
    cursor: pointer;
    font-size: 0.9rem;
    transition: 0.2s;
    white-space: nowrap;
    flex-shrink: 0;
}

.theme-toggle:hover {
    background: var(--bg-hover);
    border-color: var(--highlight);
}

/* ===== ОСНОВНАЯ СЕТКА ===== */
main {
    display: flex;
    flex: 1;
    overflow: hidden;
}

/* ===== САЙДБАР С ДЕРЕВОМ ===== */
.sidebar {
    width: var(--sidebar-width);
    background: var(--bg-sidebar);
    border-right: 1px solid var(--border-color);
    overflow-y: auto;
    flex-shrink: 0;
    padding: 12px 0;
    transition: background 0.3s, border-color 0.3s;
}

.sidebar::-webkit-scrollbar {
    width: 6px;
}
.sidebar::-webkit-scrollbar-track {
    background: transparent;
}
.sidebar::-webkit-scrollbar-thumb {
    background: var(--border-color);
    border-radius: 10px;
}

#treeContainer {
    padding: 0 12px;
}

/* Уровень 1: Раздел */
.tree-section {
    margin-bottom: 4px;
}

.section-header {
    display: flex;
    align-items: center;
    gap: 8px;
    padding: 8px 12px;
    border-radius: 8px;
    cursor: pointer;
    font-weight: 700;
    font-size: 1rem;
    color: var(--text-primary);
    transition: 0.15s;
    user-select: none;
}

.section-header:hover {
    background: var(--bg-hover);
}

.section-header .arrow {
    transition: transform 0.2s;
    font-size: 0.65rem;
    color: var(--text-muted);
    display: inline-block;
}

.section-header .arrow.open {
    transform: rotate(90deg);
}

.section-header .icon {
    font-size: 1.1rem;
}

/* Уровень 2: Категория внутри раздела */
.tree-categories {
    overflow: hidden;
    transition: max-height 0.3s ease;
}

.tree-categories.hidden {
    display: none;
}

.tree-category {
    margin-left: 8px;
    border-left: 2px solid var(--border-color);
    padding-left: 8px;
    margin-bottom: 2px;
}

.category-header {
    display: flex;
    align-items: center;
    gap: 6px;
    padding: 6px 12px;
    border-radius: 6px;
    cursor: pointer;
    font-weight: 600;
    font-size: 0.9rem;
    color: var(--text-secondary);
    transition: 0.15s;
    user-select: none;
}

.category-header:hover {
    background: var(--bg-hover);
}

.category-header .arrow {
    transition: transform 0.2s;
    font-size: 0.6rem;
    color: var(--text-muted);
    display: inline-block;
}

.category-header .arrow.open {
    transform: rotate(90deg);
}

/* Уровень 3: Статьи */
.tree-articles {
    overflow: hidden;
    transition: max-height 0.25s ease;
}

.tree-articles.hidden {
    display: none;
}

.article-item {
    padding: 5px 12px;
    margin: 1px 0;
    border-radius: 4px;
    cursor: pointer;
    font-size: 0.85rem;
    color: var(--text-secondary);
    transition: 0.1s;
    display: flex;
    align-items: center;
    gap: 6px;
}

.article-item:hover {
    background: var(--bg-hover);
    color: var(--text-primary);
}

.article-item.active {
    background: var(--bg-active);
    color: var(--text-primary);
    font-weight: 600;
    border-left: 2px solid var(--highlight);
    padding-left: 10px;
}

.article-item .bullet {
    font-size: 0.5rem;
    color: var(--text-muted);
}

.article-item.active .bullet {
    color: var(--highlight);
}

/* ===== КОНТЕНТ ===== */
.content {
    flex: 1;
    padding: 30px 40px;
    overflow-y: auto;
    background: var(--bg-content);
    transition: background 0.3s;
}

.content::-webkit-scrollbar {
    width: 8px;
}
.content::-webkit-scrollbar-track {
    background: transparent;
}
.content::-webkit-scrollbar-thumb {
    background: var(--border-color);
    border-radius: 10px;
}

#articleRenderer {
    max-width: 100%;
    margin: 0 auto;
    background: var(--bg-card);
    padding: 40px 50px;
    border-radius: 16px;
    box-shadow: var(--shadow);
    transition: background 0.3s, box-shadow 0.3s;
    min-height: 60vh;
    border: 1px solid var(--border-color);
}

/* ===== СТИЛИ СТАТЕЙ ===== */
#articleRenderer h1 {
    font-size: 2.8rem;
    margin-bottom: 20px;
    color: var(--text-primary);
    border-bottom: 4px solid var(--highlight);
    padding-bottom: 12px;
}

#articleRenderer h2 {
    font-size: 2rem;
    margin: 28px 0 14px;
    color: var(--text-primary);
}

#articleRenderer h3 {
    font-size: 1.4rem;
    margin: 20px 0 10px;
    color: var(--text-secondary);
}

#articleRenderer p {
    font-size: 1.1rem;
    line-height: 1.9;
    margin-bottom: 14px;
    color: var(--text-secondary);
}

#articleRenderer ul, #articleRenderer ol {
    margin: 10px 0 20px 25px;
    font-size: 1.05rem;
    line-height: 1.8;
    color: var(--text-secondary);
}

#articleRenderer li {
    margin-bottom: 4px;
}

#articleRenderer strong {
    color: var(--text-primary);
}

#articleRenderer hr {
    border: none;
    border-top: 2px solid var(--border-color);
    margin: 28px 0;
}

/* ===== ССЫЛКИ ===== */
#articleRenderer a {
    color: var(--highlight);
    text-decoration: none;
    transition: 0.2s;
}

#articleRenderer a:hover {
    color: var(--highlight-hover);
    text-decoration: underline;
}

/* ===== КОД ===== */
#articleRenderer pre {
    background: var(--bg-code);
    border: 1px solid var(--code-border);
    border-radius: 8px;
    padding: 16px 20px;
    margin: 14px 0 20px;
    overflow-x: auto;
    font-family: ui-monospace, SFMono-Regular, "SF Mono", Menlo, Monaco, Consolas, "Liberation Mono", "Courier New", monospace;
    font-size: 0.9rem;
    color: var(--text-primary);
    white-space: pre-wrap;
    word-break: break-word;
}

#articleRenderer code {
    background: var(--bg-code);
    padding: 2px 8px;
    border-radius: 4px;
    font-family: ui-monospace, SFMono-Regular, "SF Mono", Menlo, Monaco, Consolas, "Liberation Mono", "Courier New", monospace;
    font-size: 0.9rem;
    color: var(--text-primary);
    border: 1px solid var(--code-border);
}

#articleRenderer pre code {
    background: transparent;
    border: none;
    padding: 0;
}

/* ===== КАРТИНКИ - ОДИНОЧНЫЕ ===== */
#articleRenderer > img {
    display: block;
    max-width: 100%;
    height: auto;
    border-radius: 10px;
    margin: 16px auto;
    cursor: pointer;
    box-shadow: 0 4px 12px rgba(0,0,0,0.2);
    transition: 0.2s;
    border: 2px solid transparent;
    width: auto;
}

#articleRenderer > img:hover {
    border-color: var(--highlight);
    transform: scale(1.01);
}

/* ===== СИСТЕМА ГРУППИРОВКИ КАРТИНОК (ГАЛЕРЕЯ) ===== */
#articleRenderer .image-group {
    display: flex;
    flex-wrap: wrap;
    gap: 16px;
    margin: 16px 0 24px;
    justify-content: flex-start;
    align-items: flex-start;
}

#articleRenderer .image-group .image-item {
    border-radius: 10px;
    overflow: hidden;
    background: var(--bg-code);
    border: 1px solid var(--border-color);
    transition: 0.2s;
    cursor: pointer;
    width: auto;
    max-width: 100%;
    flex: 0 0 auto;
    max-width: var(--gallery-max-width);
}

#articleRenderer .image-group .image-item:hover {
    transform: translateY(-3px);
    box-shadow: 0 8px 24px rgba(0,0,0,0.3);
    border-color: var(--highlight);
}

#articleRenderer .image-group .image-item img {
    display: block;
    height: auto;
    max-height: 250px;
    width: auto;
    max-width: 100%;
    margin: 0 auto;
    border-radius: 0;
    border: none;
    cursor: pointer;
    transition: 0.2s;
    object-fit: contain;
}

#articleRenderer .image-group .image-item img:hover {
    transform: scale(1.02);
}

#articleRenderer .image-group .image-item .image-label {
    padding: 6px 12px 10px;
    font-size: 0.8rem;
    color: var(--text-muted);
    background: var(--bg-card);
    border-top: 1px solid var(--border-color);
    text-align: center;
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
    max-width: 100%;
}

/* ===== СТАРЫЙ СТИЛЬ ГАЛЕРЕИ (для совместимости) ===== */
#articleRenderer .gallery {
    display: flex;
    flex-wrap: wrap;
    gap: 16px;
    margin: 16px 0 24px;
    justify-content: flex-start;
    align-items: flex-start;
}

#articleRenderer .gallery .gallery-item {
    border-radius: 10px;
    overflow: hidden;
    background: var(--bg-code);
    border: 1px solid var(--border-color);
    transition: 0.2s;
    cursor: pointer;
    width: auto;
    max-width: 100%;
    flex: 0 0 auto;
    max-width: var(--gallery-max-width);
}

#articleRenderer .gallery .gallery-item:hover {
    transform: translateY(-3px);
    box-shadow: 0 8px 24px rgba(0,0,0,0.3);
    border-color: var(--highlight);
}

#articleRenderer .gallery .gallery-item img {
    display: block;
    height: auto;
    max-height: 200px;
    width: auto;
    max-width: 100%;
    margin: 0 auto;
    border-radius: 0;
    border: none;
    object-fit: contain;
}

#articleRenderer .gallery .gallery-item .caption {
    padding: 6px 14px 10px;
    font-size: 0.85rem;
    color: var(--text-muted);
    background: var(--bg-card);
    border-top: 1px solid var(--border-color);
    text-align: center;
}

/* ===== ДАТА ПРАВКИ ===== */
.article-date {
    text-align: right;
    color: var(--text-muted);
    font-size: 0.8rem;
    margin-bottom: 16px;
    padding-bottom: 8px;
    border-bottom: 1px solid var(--border-color);
}

/* ===== ПРИВЕТСТВИЕ ===== */
.welcome-message {
    text-align: center;
    padding: 60px 0;
    color: var(--text-muted);
}

.welcome-message h2 {
    font-size: 2.2rem;
    color: var(--text-primary);
    margin-bottom: 12px;
}

/* ===== МОДАЛЬНОЕ ОКНО ===== */
.modal {
    display: none;
    position: fixed;
    z-index: 9999;
    top: 0;
    left: 0;
    width: 100%;
    height: 100%;
    background: var(--modal-bg);
    backdrop-filter: blur(6px);
    justify-content: center;
    align-items: center;
    cursor: pointer;
}

.modal-content {
    max-width: 90%;
    max-height: 90%;
    border-radius: 12px;
    box-shadow: 0 20px 60px rgba(0,0,0,0.6);
    animation: zoomIn 0.2s ease;
    cursor: default;
    border: 1px solid var(--border-color);
}

@keyframes zoomIn {
    from { transform: scale(0.85); opacity: 0; }
    to { transform: scale(1); opacity: 1; }
}

.close-modal {
    position: absolute;
    top: 30px;
    right: 40px;
    color: white;
    font-size: 3rem;
    font-weight: 300;
    cursor: pointer;
    transition: 0.2s;
    z-index: 10000;
}

.close-modal:hover {
    transform: rotate(90deg);
    color: var(--highlight);
}

/* ===== ВИКИ-ССЫЛКИ ===== */
#articleRenderer a.wiki-link {
    color: var(--highlight);
    text-decoration: none;
    border-bottom: 1px dashed var(--highlight);
    transition: 0.2s;
    cursor: pointer;
}

#articleRenderer a.wiki-link:hover {
    color: var(--highlight-hover);
    border-bottom-style: solid;
    text-decoration: none;
}

#articleRenderer a.wiki-link-broken {
    color: #dc2626;
    text-decoration: none;
    border-bottom: 1px dashed #dc2626;
    cursor: help;
}

#articleRenderer a.wiki-link-broken:hover {
    border-bottom-style: solid;
}

/* ===== КНОПКА "ВВЕРХ" ===== */
.scroll-top-btn {
    position: fixed;
    bottom: 30px;
    right: 30px;
    width: 50px;
    height: 50px;
    border-radius: 50%;
    background: var(--highlight);
    color: var(--text-inverse);
    border: none;
    cursor: pointer;
    display: flex;
    align-items: center;
    justify-content: center;
    font-size: 1.5rem;
    box-shadow: 0 4px 20px rgba(201, 123, 255, 0.4);
    transition: all 0.3s;
    opacity: 0;
    visibility: hidden;
    transform: translateY(20px);
    z-index: 1000;
}

.scroll-top-btn.visible {
    opacity: 1;
    visibility: visible;
    transform: translateY(0);
}

.scroll-top-btn:hover {
    background: var(--highlight-hover);
    transform: translateY(-3px);
    box-shadow: 0 8px 24px rgba(201, 123, 255, 0.6);
}

.scroll-top-btn:active {
    transform: translateY(0);
}

@media (max-width: 768px) {
    .scroll-top-btn {
        bottom: 20px;
        right: 20px;
        width: 44px;
        height: 44px;
        font-size: 1.2rem;
    }
}

/* ===== АДАПТИВ ===== */
@media (max-width: 768px) {
    .header-flex {
        flex-direction: column;
        align-items: stretch;
        gap: 8px;
    }
    .logo h1 { font-size: 2rem; text-align: center; }
    .search-box { max-width: 100%; }
    .theme-toggle { align-self: center; }
    
    main { flex-direction: column; }
    .sidebar {
        width: 100%;
        max-height: 250px;
        border-right: none;
        border-bottom: 1px solid var(--border-color);
    }
    .content { padding: 12px; }
    #articleRenderer { 
        padding: 20px; 
        max-width: 100%;
    }
    #articleRenderer h1 { font-size: 1.8rem; }
    #articleRenderer h2 { font-size: 1.4rem; }
    #articleRenderer p { font-size: 0.95rem; }
    
    #articleRenderer .image-group {
        gap: 10px;
    }
    #articleRenderer .image-group .image-item {
        max-width: 200px;
    }
    #articleRenderer .image-group .image-item img {
        max-height: 150px;
    }
    #articleRenderer .gallery .gallery-item {
        max-width: 200px;
    }
    #articleRenderer .gallery .gallery-item img {
        max-height: 150px;
    }
    
    .modal-content { max-width: 95%; }
    .close-modal { top: 15px; right: 20px; font-size: 2.5rem; }
}

@media (min-width: 1800px) {
    #articleRenderer {
        max-width: 1700px;
        padding: 50px 70px;
    }
    #articleRenderer .image-group .image-item {
        max-width: 450px;
    }
    #articleRenderer .image-group .image-item img {
        max-height: 300px;
    }
}

@media (max-width: 500px) {
    #articleRenderer .image-group {
        gap: 8px;
        justify-content: center;
    }
    #articleRenderer .image-group .image-item {
        max-width: 160px;
    }
    #articleRenderer .image-group .image-item img {
        max-height: 120px;
    }
    #articleRenderer .image-group .image-item .image-label {
        font-size: 0.7rem;
        padding: 4px 8px 8px;
    }
    #articleRenderer .gallery .gallery-item {
        max-width: 160px;
    }
    #articleRenderer .gallery .gallery-item img {
        max-height: 120px;
    }
}