/* Box sizing */
*,
*::before,
*::after {
    box-sizing: border-box;
}

/* Remove default margin */
html, body, h1, h2, h3, h4, h5, h6, p,
blockquote, pre,
dl, dd, ol, ul,
figure,
hr,
fieldset, legend {
    margin: 0;
    padding: 0;
}

/* Body defaults */
body {
    min-height: 100vh;
    line-height: 1.5;
    text-rendering: optimizeSpeed;
}

/* Lists */
ol, ul {
    list-style: none;
}

/* Links */
a {
    text-decoration: none;
    color: inherit;
}

/* Media */
img, picture, video, canvas, svg {
    display: block;
    max-width: 100%;
}

/* Forms */
input, button, textarea, select {
    font: inherit;/* Добавляем эти две строки */
    max-width: 100%; 
    box-sizing: border-box;
}

/* Buttons */
button {
    background: none;
    border: none;
    padding: 0;
    cursor: pointer;
}

/* Tables */
table {
    border-collapse: collapse;
    border-spacing: 0;
}


/* --- ПЕРЕМЕННЫЕ (Светлая тема по умолчанию) --- */
:root {
    --bg-color: #f0f7f7;
    --header-bg: #ffffff;
    --accent-color: #17a2b8;
    --accent-hover: #138496;
    --text-main: #2c3e50;
    --text-muted: #7f8c8d;
    --border-color: #e0e6e6;
    --white: #ffffff;
    --card-bg: #ffffff;
    --reader-bg: #ffffff; /* Фон читалки */
    --panel-bg: rgba(44, 62, 80, 0.95); /* Плавающая панель */
}

/* --- ТЕМНАЯ ТЕМА --- */
[data-theme="dark"] {
    --bg-color: #121212;        /* Глубокий темный фон сайта */
    --header-bg: #1e1e1e;      /* Темная шапка */
    --text-main: #e0e0e0;      /* Светло-серый текст */
    --text-muted: #a0a0a0;     /* Приглушенный текст */
    --border-color: #333333;   /* Темные границы */
    --white: #1e1e1e;
    --card-bg: #1e1e1e;        /* Фон блоков и карточек */
    --reader-bg: #1a1a1a;      /* Фон читалки (ночной режим) */
    --panel-bg: rgba(0, 0, 0, 0.9);
    --accent-color: #26c6da;   /* Чуть более яркий акцент для темного */
}

/* Применяем переменные ко всем существующим стилям */
body {
    margin: 0;
    padding: 0;
    background-color: var(--bg-color);
    color: var(--text-main);
    transition: background-color 0.3s, color 0.3s; /* Плавный переход */
    font-family: 'Segoe UI', system-ui, -apple-system, sans-serif;
}

.main-header, .home-section, .sidebar-block, .book-main, .auth-card, .notifications-list, .dropdown-menu {
    background: var(--card-bg) !important;
    border: 1px solid var(--border-color) !important;
    color: var(--text-main);
}

.card-title, .logo-text, .block-title, .book-page-title, .ch-title {
    color: var(--text-main) !important;
}

.genre-list-side li a, .dropdown-menu a, .chapter-item, .notif-item {
    color: var(--text-main);
    border-color: var(--border-color);
}

.chapter-item:hover, .dropdown-menu a:hover, .notif-item:hover {
    background: rgba(255, 255, 255, 0.05) !important;
}

/* Поиск */
.header-search input {
    background: var(--bg-color);
    color: var(--text-main);
    border: 1px solid var(--border-color);
}

/* Формы */
.form-group input, .comment-form textarea {
    background: var(--bg-color);
    color: var(--text-main);
    border: 1px solid var(--border-color);
}

.container {
    max-width: 1140px;
    margin: 0 auto;
    padding: 0 15px;
}

/* --- ШАПКА (HEADER) --- */
.main-header {
    background: var(--header-bg);
    border-bottom: 2px solid var(--border-color);
    height: 60px;
    display: flex;
    align-items: center;
    position: sticky;
    top: 0;
    z-index: 1000;
    box-shadow: 0 1px 5px rgba(0,0,0,0.05);
}

.header-flex {
    width: 100%;
    display: flex;
    justify-content: space-between;
    align-items: center;
    flex-wrap: nowrap; /* ЗАПРЕТ ПЕРЕНОСА */
}

.header-left, .header-right {
    display: flex;
    align-items: center;
    gap: 15px;
}

.logo-wrapper {
    display: flex;
    align-items: center;
    gap: 8px;
    text-decoration: none;
    flex-shrink: 0; 
}

.logo-text {
    font-size: 19px;
    font-weight: 800;
    color: var(--text-main);
}

.accent-text { color: var(--accent-color); }

.main-nav {
    display: flex;
    align-items: center;
    gap: 5px;
    white-space: nowrap; 
}

.main-nav a, .nav-item > a {
    text-decoration: none;
    color: var(--text-main);
    font-weight: 600;
    font-size: 13px;
    padding: 8px 10px;
    transition: color 0.2s;
}

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

.nav-item {
    position: relative;
    padding: 15px 0;
}

.nav-dropdown {
    position: absolute;
    top: 100%;
    left: 0;
    width: 200px;
    background: var(--card-bg);
    border: 1px solid var(--border-color);
    box-shadow: 0 10px 25px rgba(0,0,0,0.1);
    border-radius: 0 0 8px 8px;
    display: none;
    z-index: 1000;
    padding: 10px 0;
    margin-top: -10px;
}

.nav-item:hover .nav-dropdown {
    display: block;
}

.nav-dropdown a {
    display: block;
    padding: 10px 20px;
    color: var(--text-main);
    text-decoration: none;
    font-size: 14px;
    font-weight: 500;
    transition: background 0.2s, color 0.2s;
    white-space: nowrap; 
}

.nav-dropdown a:hover {
    background: var(--accent-color);
    color: #000 !important; 
}

.dropdown-divider {
    height: 1px;
    background: var(--border-color);
    margin: 5px 0;
}

.nav-dropdown::before {
    content: "";
    position: absolute;
    top: -10px;
    left: 0;
    width: 100%;
    height: 10px;
}

/* --- МАКЕТ (LAYOUT) --- */
.main-layout {
    display: grid;
    grid-template-columns: 1fr 280px; 
    gap: 30px;
    align-items: start;
    margin-top: 25px;
}

.page-content {
    padding-bottom: 50px;
}

/* --- СЕКЦИИ НА ГЛАВНОЙ --- */
.home-section {
    background: var(--card-bg);
    border: 1px solid var(--border-color);
    border-radius: 4px;
    padding: 15px;
    margin-bottom: 25px;
}

.section-header {
    display: flex;
    justify-content: space-between;
    align-items: center;
    margin-bottom: 15px;
    border-bottom: 1px solid var(--border-color);
    padding-bottom: 10px;
}

.section-title {
    font-size: 18px;
    margin: 0;
    font-weight: 700;
}

.all-link {
    font-size: 13px;
    color: var(--accent-color);
    text-decoration: none;
    font-weight: 600;
}

/* --- СЕТКА КНИГ (6 в ряд) --- */
.book-row {
    display: grid;
    grid-template-columns: repeat(6, 1fr);
    gap: 15px;
}

.book-card-compact {
    min-width: 0;
}

.cover-holder {
    position: relative;
    aspect-ratio: 3/4.2; 
    border-radius: 3px;
    overflow: hidden;
    box-shadow: 0 2px 4px rgba(0,0,0,0.1);
    background: var(--bg-color);
}

.cover-holder img {
    width: 100%;
    height: 100%;
    object-fit: cover;
    transition: transform 0.3s ease;
}

.book-card-compact:hover img {
    transform: scale(1.05);
}

/* Плашки на обложке */
.status-tag {
    position: absolute;
    bottom: 0;
    left: 0;
    right: 0;
    padding: 3px;
    font-size: 10px;
    text-align: center;
    color: #fff;
    font-weight: bold;
    background: rgba(0,0,0,0.6);
}

.tag-completed { background: rgba(40, 167, 69, 0.85); }
.tag-process { background: rgba(23, 162, 184, 0.85); }

.card-price-tag {
    position: absolute;
    top: 5px;
    right: 5px;
    background: var(--accent-color);
    color: #fff;
    font-size: 10px;
    padding: 2px 5px;
    font-weight: bold;
    border-radius: 2px;
}

.card-data {
    padding-top: 8px;
}

.card-title {
    display: block;
    font-size: 13px;
    font-weight: 700;
    color: var(--text-main);
    text-decoration: none;
    line-height: 1.25;
    height: 32px; 
    overflow: hidden;
    margin-bottom: 3px;
}

.card-author {
    display: block;
    font-size: 11px;
    color: var(--accent-color);
    text-decoration: none;
}

/* --- САЙДБАР (SIDEBAR) --- */
.sidebar {
    position: sticky;
    top: 85px;
}

.sidebar-block {
    background: var(--card-bg);
    border: 1px solid var(--border-color);
    border-radius: 4px;
    padding: 15px;
    margin-bottom: 20px;
}

.block-title {
    font-size: 15px;
    border-bottom: 2px solid var(--accent-color);
    padding-bottom: 5px;
    margin-bottom: 12px;
    font-weight: 700;
}

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

.genre-list-side li a {
    display: flex;
    justify-content: space-between;
    text-decoration: none;
    color: var(--text-main);
    font-size: 14px;
    padding: 6px 0;
    transition: 0.2s;
}

.genre-list-side li a:hover { color: var(--accent-color); }
.genre-list-side span { color: var(--text-muted); font-size: 12px; }

/* --- ПОЛЬЗОВАТЕЛЬСКИЙ ИНТЕРФЕЙС --- */
.header-search {
    position: relative;
    display: flex;
    align-items: center;
}

.header-search input {
    background: var(--bg-color);
    border: 1px solid var(--border-color);
    padding: 6px 30px 6px 15px;
    border-radius: 20px;
    font-size: 13px;
    width: 150px;
    color: var(--text-main);
    transition: width 0.3s ease;
}

.header-search input:focus {
    width: 200px;
    background: var(--card-bg);
    border-color: var(--accent-color);
    outline: none;
}

.header-search button {
    position: absolute;
    right: 10px;
    background: none;
    border: none;
    color: var(--text-muted);
    cursor: pointer;
}

/* Панель управления */
.user-control-panel {
    display: flex;
    align-items: center;
    gap: 8px;
    background: var(--bg-color);
    padding: 5px 12px;
    border-radius: 50px;
    border: 1px solid var(--border-color);
    flex-shrink: 0; 
}

.header-stat-item {
    display: flex;
    align-items: center;
    gap: 6px;
    text-decoration: none;
    color: var(--text-main);
    padding: 6px 10px;
    border-radius: 20px;
    transition: all 0.2s;
    position: relative;
}

.header-stat-item i {
    font-size: 16px;
    color: var(--text-muted);
}

.header-stat-item:hover {
    background: var(--card-bg);
    box-shadow: 0 2px 5px rgba(0,0,0,0.05);
}

.header-stat-item:hover i {
    color: var(--accent-color);
}

.balance-link .stat-value {
    font-weight: 700;
    font-size: 14px;
}

.balance-link small {
    font-size: 11px;
    color: var(--text-muted);
}

.notif-link.has-unread i {
    color: var(--accent-color);
    animation: pulse 2s infinite;
}

@keyframes pulse {
    0% { transform: scale(1); }
    50% { transform: scale(1.1); }
    100% { transform: scale(1); }
}

.header-divider {
    width: 1px;
    height: 20px;
    background: var(--border-color);
    margin: 0 4px;
}

.profile-link {
    display: flex;
    align-items: center;
    gap: 10px;
    text-decoration: none;
    color: var(--text-main);
    font-weight: 600;
    font-size: 14px;
}

.avatar-placeholder {
    width: 32px;
    height: 32px;
    background: var(--accent-color);
    color: #fff;
    border-radius: 50%;
    display: flex;
    align-items: center;
    justify-content: center;
    font-weight: 900;
    text-transform: uppercase;
    font-size: 14px;
    box-shadow: inset 0 0 5px rgba(0,0,0,0.2);
}

.profile-name {
    max-width: 100px;
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
}

.user-dropdown {
    position: relative;
    cursor: pointer;
    padding-bottom: 10px;
    margin-bottom: -10px;
}

.user-dropdown-trigger {
    display: flex;
    align-items: center;
    gap: 8px;
    padding: 4px 8px;
    border-radius: 20px;
}

.dropdown-menu {
    position: absolute;
    top: 100%; 
    right: 0;
    width: 220px;
    background: var(--card-bg);
    border: 1px solid var(--border-color);
    border-radius: 8px;
    box-shadow: 0 10px 25px rgba(0,0,0,0.1);
    display: none;
    z-index: 1100;
    padding: 8px 0;
    animation: fadeInDown 0.2s ease-out;
}

.user-dropdown:hover .dropdown-menu {
    display: block;
}

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

.dropdown-menu a {
    display: flex;
    align-items: center;
    gap: 12px;
    padding: 10px 15px;
    text-decoration: none;
    color: var(--text-main);
    font-size: 14px;
}

.dropdown-menu a:hover {
    background: var(--bg-color);
    color: var(--accent-color);
}

.guest-actions {
    display: flex;
    align-items: center;
    gap: 12px;
    flex-shrink: 0;
}

.btn-login-header {
    text-decoration: none;
    color: var(--text-main);
    font-size: 14px;
    font-weight: 600;
}

.btn-signup-header {
    text-decoration: none;
    background: var(--accent-color);
    color: #fff !important;
    font-size: 14px;
    font-weight: 700;
    padding: 7px 18px;
    border-radius: 4px;
    white-space: nowrap;
}

/* --- СТРАНИЦЫ АВТОРИЗАЦИИ --- */
.auth-wrapper {
    display: flex;
    justify-content: center;
    align-items: center;
    padding: 40px 0;
}

.auth-card {
    background: var(--card-bg);
    width: 100%;
    max-width: 420px;
    padding: 40px;
    border-radius: 8px;
    border: 1px solid var(--border-color);
    box-shadow: 0 15px 35px rgba(23, 162, 184, 0.05);
    text-align: center;
}

.form-group {
    margin-bottom: 20px;
    text-align: left;
}

.form-group label {
    display: block;
    font-size: 13px;
    font-weight: 700;
    margin-bottom: 8px;
}

.form-group input {
    width: 100%;
    padding: 12px 15px;
    border: 1px solid var(--border-color);
    border-radius: 6px;
    background: var(--bg-color);
    color: var(--text-main);
}

.btn-full {
    width: 100%;
    padding: 14px;
    background: var(--accent-color);
    color: #fff;
    border: none;
    border-radius: 6px;
    font-weight: 700;
    cursor: pointer;
}

/* --- СТРАНИЦА КНИГИ --- */
.book-page-layout {
    display: grid;
    grid-template-columns: 280px 1fr;
    gap: 40px;
    margin-top: 30px;
    align-items: start;
}

.book-aside {
    width: 280px;          
    position: sticky;
    top: 85px;
    align-self: start;
    flex-shrink: 0;        
}

.main-cover {
    width: 280px;          
    height: 400px;         
    position: relative;
    border-radius: 12px;
    overflow: hidden;      
    box-shadow: 0 10px 30px rgba(0,0,0,0.2);
    background: var(--bg-color); 
}

.main-cover img {
    width: 100%;
    height: 100%;
    object-fit: cover; 
    display: block;
}

.book-actions {
    margin-top: 20px;
    display: flex;
    flex-direction: column;
    gap: 10px;
}

.book-action-btn {
    display: block;
    width: 100%;
    padding: 12px;
    border-radius: 6px;
    font-weight: 700;
    text-align: center;
    text-decoration: none;
    border: none;
    cursor: pointer;
    transition: 0.2s;
}

.btn-read-gold { background: #ffc107; color: #000; }
.btn-buy-green { background: #28a745; color: #fff; }

.book-main {
    background: var(--card-bg);
    padding: 30px;
    border-radius: 8px;
    border: 1px solid var(--border-color);
}

.book-page-title {
    margin: 0 0 10px 0;
    font-size: 28px;
    font-weight: 800;
}

.book-stats-inline {
    display: flex;
    gap: 20px;
    color: var(--text-muted);
    font-size: 14px;
    margin: 15px 0;
    padding-bottom: 15px;
    border-bottom: 1px solid var(--border-color);
}

.description-text { line-height: 1.7; font-size: 15px; }

.chapter-list {
    border: 1px solid var(--border-color);
    border-radius: 8px;
    overflow: hidden;
}

.chapter-item {
    display: flex;
    justify-content: space-between;
    align-items: center;
    padding: 12px 20px;
    border-bottom: 1px solid var(--border-color);
    text-decoration: none;
    color: var(--text-main);
}

.ch-title { font-weight: 600; color: #17a2b8; }

.download-links-group {
    display: grid;
    grid-template-columns: 1fr 1fr; 
    gap: 8px;
    margin-top: 15px;
}

.btn-download {
    display: flex;
    align-items: center;
    justify-content: center;
    gap: 6px;
    padding: 10px 4px; 
    background: var(--bg-color);
    border: 1px solid var(--border-color);
    border-radius: 6px;
    color: var(--text-main);
    font-size: 12px; 
    font-weight: 700;
    text-decoration: none;
}

/* Комментарии и рейтинг */
.comment-form {
    display: flex;
    flex-direction: column;
    gap: 12px;
    margin: 20px 0;
    background: var(--bg-color);
    padding: 20px;
    border-radius: 8px;
}

.comment-form textarea {
    width: 100%;
    min-height: 100px;
    padding: 12px;
    border-radius: 6px;
    background: var(--card-bg);
    color: var(--text-main);
    resize: vertical;
}

.book-rating-bar {
    display: flex;
    align-items: center;
    gap: 20px;
    margin: 20px 0;
    padding: 15px;
    background: var(--bg-color);
    border-radius: 10px;
}

/* Уведомления */
.notif-item {
    display: flex;
    gap: 15px;
    padding: 15px 20px;
    border-bottom: 1px solid var(--border-color);
}

.notif-item.unread {
    background: rgba(255, 193, 7, 0.05); 
    border-left: 4px solid var(--accent-color);
}

/* --- ФУТЕР --- */
.main-footer {
    background: var(--white);
    padding: 40px 0;
    margin-top: 50px;
    border-top: 1px solid var(--border-color);
}

.footer-grid {
    display: flex;
    justify-content: space-between;
    gap: 30px;
}

.footer-nav-col a {
    display: block;
    text-decoration: none;
    color: var(--text-muted);
    font-size: 14px;
    margin-bottom: 8px;
}

/* ===========================================================
   АДАПТИВНОСТЬ (САЙДБАР ТЕПЕРЬ ГАРАНТИРОВАННО ПАДАЕТ ВНИЗ)
   ===========================================================
*/

@media screen and (max-width: 1024px) {
    /* Главная страница */
    .main-layout {
        display: flex !important;
        flex-direction: column !important;
    }
    
    .sidebar {
        position: relative !important;
        top: 0 !important;
        width: 100% !important;
        order: 2 !important; /* Сайдбар вниз */
        margin-top: 30px;
    }

    /* Страница книги */
    .book-page-layout {
        display: flex !important;
        flex-direction: column !important;
    }

    .book-aside {
        position: relative !important;
        top: 0 !important;
        width: 100% !important;
        max-width: 320px !important;
        margin: 0 auto 30px !important;
        order: 1 !important; /* Обложка сверху */
    }
    
    .book-main {
        order: 2 !important; /* Описание снизу обложки */
    }

    .book-row {
        grid-template-columns: repeat(4, 1fr) !important;
    }

    .main-nav, .header-search {
        display: none !important;
    }
}

@media screen and (max-width: 768px) {
    .book-row {
        grid-template-columns: repeat(3, 1fr) !important;
        gap: 10px !important;
    }
    
    .footer-grid {
        flex-direction: column !important;
        text-align: center !important;
        align-items: center !important;
    }
    
    .footer-flex {
        display: flex;
        flex-direction: column;
        gap: 30px;
    }
}

@media screen and (max-width: 580px) {
    .book-row {
        grid-template-columns: repeat(2, 1fr) !important;
    }

    .profile-name {
        display: none !important;
    }

    .user-control-panel {
        padding: 4px 8px !important;
    }

    .download-links-group {
        grid-template-columns: 1fr !important;
    }

    .logo-text {
        font-size: 16px !important;
    }
}

@media screen and (max-width: 380px) {
    .book-row {
        grid-template-columns: 1fr !important;
    }
}