:root {
    --primary: #6366f1;
    --primary-dark: #4f46e5;
    --primary-light: #e0e7ff;
    --bg: #f8fafc;
    --card-bg: #ffffff;
    --text: #1e293b;
    --text-light: #64748b;
    --border: #e2e8f0;
    --shadow: 0 1px 3px rgba(0,0,0,0.06), 0 1px 2px rgba(0,0,0,0.04);
    --shadow-lg: 0 10px 25px -5px rgba(0,0,0,0.1), 0 8px 10px -6px rgba(0,0,0,0.04);
    --radius: 16px;
    --radius-sm: 10px;
}

/* ===== ТЁМНАЯ ТЕМА ===== */
[data-theme="dark"] {
    --primary: #818cf8;
    --primary-dark: #a5b4fc;
    --primary-light: #312e81;
    --bg: #0f172a;
    --card-bg: #1e293b;
    --text: #f1f5f9;
    --text-light: #94a3b8;
    --border: #334155;
    --shadow: 0 1px 3px rgba(0,0,0,0.4);
    --shadow-lg: 0 10px 25px -5px rgba(0,0,0,0.5), 0 8px 10px -6px rgba(0,0,0,0.4);
}

/* Плавный переход при смене темы */
body, header, .modal, .dropdown-menu, .action-card,
.dropdown-btn, .icon-btn, .custom-select, .grade-list,
.setting-item-btn, .modal-overlay {
    transition: background-color 0.25s ease, color 0.25s ease,
                border-color 0.25s ease, box-shadow 0.25s ease;
}

/* Иконка кнопки темы — отдельный поворот не нужен, убираем общий rotate только для неё */
#themeToggle:hover {
    transform: scale(1.08);
}

* {
    margin: 0;
    padding: 0;
    box-sizing: border-box;
}

/* ВАЖНО: страховка от горизонтального скролла при любых "вылезающих" элементах */
html, body {
    overflow-x: hidden;
    width: 100%;
}

body {
    font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, Helvetica, Arial, sans-serif;
    background: var(--bg);
    color: var(--text);
    min-height: 100vh;
    display: flex;
    flex-direction: column;
}

/* ==========================================================
   SVG-ИКОНКИ — базовые стили (работают и в светлой, и в тёмной теме)
   ========================================================== */

/* Базовый класс .icon для всех SVG */
.icon {
    width: 1em;
    height: 1em;
    display: block;
    fill: none;
    stroke: currentColor;
    stroke-width: 1.25;
    stroke-linecap: round;
    stroke-linejoin: round;
    overflow: visible;
}

/* Размеры для конкретных контейнеров */
.logo-icon .icon        { width: 22px; height: 22px; }
#subjectIcon .icon      { width: 18px; height: 18px; }
.dropdown-item .icon    { width: 18px; height: 18px; }
.icon-btn .icon         { width: 20px; height: 20px; }
.btn-main-icon .icon    { width: 22px; height: 22px; }
.action-icon .icon      { width: 32px; height: 32px; stroke-width: 1.1; }
.setting-item-btn .icon { width: 18px; height: 18px; }
.about-logo .icon       { width: 48px; height: 48px; stroke-width: 1; }
.chevron .icon          { width: 12px; height: 12px; stroke-width: 2; }
.modal-close .icon      { width: 16px; height: 16px; }
.back-btn .icon         { width: 16px; height: 16px; }
.setting-item-btn .arrow .icon { width: 14px; height: 14px; }

/* ===== HEADER ===== */
header {
    background: var(--card-bg);
    border-bottom: 1px solid var(--border);
    position: sticky;
    top: 0;
    z-index: 100;
}

.header-inner {
    max-width: 1200px;
    margin: 0 auto;
    padding: 14px 20px;
    display: flex;
    align-items: center;
    justify-content: space-between;
    gap: 16px;
}

.logo {
    display: flex;
    align-items: center;
    gap: 10px;
    font-weight: 700;
    font-size: 20px;
    flex-shrink: 0;
}

.logo-icon {
    width: 40px;
    height: 40px;
    border-radius: 10px;
    overflow: hidden;
    display: flex;
    align-items: center;
    justify-content: center;
    background: transparent; /* убираем прошлый градиентный фон под SVG */
    flex-shrink: 0;
}

.logo-img {
    width: 100%;
    height: 100%;
    object-fit: contain;
    display: block;
}

.header-controls {
    display: flex;
    align-items: center;
    gap: 10px;
}

/* ===== DROPDOWN (Выбор предмета) ===== */
.dropdown {
    position: relative;
}

.dropdown-btn {
    display: flex;
    align-items: center;
    gap: 8px;
    padding: 10px 16px;
    background: var(--bg);
    border: 1px solid var(--border);
    border-radius: var(--radius-sm);
    font-size: 14px;
    font-weight: 500;
    color: var(--text);
    cursor: pointer;
    transition: all 0.2s ease;
    white-space: nowrap;
}

.dropdown-btn:hover {
    background: var(--primary-light);
    border-color: var(--primary);
}

.dropdown-btn.active {
    background: var(--primary-light);
    border-color: var(--primary);
    color: var(--primary-dark);
}

.chevron {
    transition: transform 0.2s ease;
    font-size: 10px;
}

.dropdown-btn.active .chevron {
    transform: rotate(180deg);
}

/*
  По умолчанию (десктоп/планшет) — обычное absolute-меню.
  На мобильных JS переключит position на fixed с left/right 12px,
  чтобы меню НИКОГДА не выходило за пределы экрана по ширине —
  это и есть фикс первого бага.
*/
.dropdown-menu {
    position: absolute;
    top: calc(100% + 8px);
    left: 0;
    min-width: 220px;
    max-width: calc(100vw - 24px); /* страховка на любых экранах */
    background: var(--card-bg);
    border-radius: var(--radius-sm);
    box-shadow: var(--shadow-lg);
    border: 1px solid var(--border);
    opacity: 0;
    visibility: hidden;
    transform: translateY(-8px);
    transition: opacity 0.2s ease, visibility 0.2s ease, transform 0.2s ease;
    z-index: 50;
    max-height: 300px;
    overflow-y: auto;
}

.dropdown-menu.show {
    opacity: 1;
    visibility: visible;
    transform: translateY(0);
}

.dropdown-item {
    padding: 10px 16px;
    cursor: pointer;
    font-size: 14px;
    display: flex;
    align-items: center;
    justify-content: space-between;
    transition: background 0.15s ease;
}

.dropdown-item:hover {
    background: var(--bg);
}

.dropdown-item.selected {
    color: var(--primary);
    font-weight: 600;
    background: var(--primary-light);
}

.dropdown-item.selected::after {
    content: "✓";
}

/* Кнопка настроек */
.icon-btn {
    width: 40px;
    height: 40px;
    display: flex;
    align-items: center;
    justify-content: center;
    background: var(--bg);
    border: 1px solid var(--border);
    border-radius: var(--radius-sm);
    cursor: pointer;
    font-size: 18px;
    transition: all 0.2s ease;
    flex-shrink: 0;
}

.icon-btn:hover {
    background: var(--primary-light);
    border-color: var(--primary);
    transform: rotate(45deg);
}

/* ===== MAIN CONTENT ===== */
main {
    flex: 1;
    max-width: 1200px;
    width: 100%;
    margin: 0 auto;
    padding: 32px 20px 60px;
}

.hero {
    text-align: center;
    margin-bottom: 40px;
}

.hero h1 {
    font-size: 32px;
    font-weight: 800;
    margin-bottom: 10px;
    background: linear-gradient(135deg, var(--primary), #8b5cf6);
    -webkit-background-clip: text;
    background-clip: text;
    -webkit-text-fill-color: transparent;
}

.hero p {
    color: var(--text-light);
    font-size: 16px;
}

.main-action {
    display: flex;
    justify-content: center;
    margin-bottom: 40px;
}

.btn-main {
    display: flex;
    align-items: center;
    gap: 14px;
    padding: 20px 48px;
    background: linear-gradient(135deg, var(--primary), #8b5cf6);
    color: white;
    border: none;
    border-radius: var(--radius);
    font-size: 18px;
    font-weight: 700;
    cursor: pointer;
    box-shadow: 0 8px 20px -6px rgba(99, 102, 241, 0.5);
    transition: all 0.25s ease;
}

.btn-main:hover {
    transform: translateY(-3px);
    box-shadow: 0 12px 24px -6px rgba(99, 102, 241, 0.6);
}

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

.btn-main-icon {
    font-size: 24px;
}

.actions-grid {
    display: grid;
    grid-template-columns: repeat(auto-fit, minmax(220px, 1fr));
    gap: 20px;
}

.action-card {
    background: var(--card-bg);
    border: 1px solid var(--border);
    border-radius: var(--radius);
    padding: 28px 24px;
    cursor: pointer;
    transition: all 0.25s ease;
    display: flex;
    flex-direction: column;
    align-items: flex-start;
    gap: 14px;
    position: relative;
    overflow: hidden;
}

.action-card::before {
    content: "";
    position: absolute;
    top: 0; left: 0; right: 0;
    height: 3px;
    background: linear-gradient(90deg, var(--primary), #8b5cf6);
    transform: scaleX(0);
    transform-origin: left;
    transition: transform 0.3s ease;
}

.action-card:hover {
    box-shadow: var(--shadow-lg);
    transform: translateY(-4px);
    border-color: transparent;
}

.action-card:hover::before {
    transform: scaleX(1);
}

.action-icon {
    width: 48px;
    height: 48px;
    border-radius: 12px;
    background: var(--primary-light);
    display: flex;
    align-items: center;
    justify-content: center;
    font-size: 22px;
}

.action-card h3 {
    font-size: 17px;
    font-weight: 700;
}

.action-card p {
    font-size: 13.5px;
    color: var(--text-light);
    line-height: 1.5;
}

/* ===== MODAL (Настройки) ===== */
.modal-overlay {
    position: fixed;
    inset: 0;
    background: rgba(15, 23, 42, 0.5);
    backdrop-filter: blur(4px);
    display: flex;
    align-items: center;
    justify-content: center;
    z-index: 1000;
    opacity: 0;
    visibility: hidden;
    transition: all 0.25s ease;
    padding: 20px;
}

.modal-overlay.show {
    opacity: 1;
    visibility: visible;
}

.modal {
    background: var(--card-bg);
    border-radius: var(--radius);
    width: 100%;
    max-width: 420px;
    max-height: 85vh;
    box-shadow: var(--shadow-lg);
    transform: scale(0.95) translateY(10px);
    transition: all 0.25s ease;
    display: flex;
    flex-direction: column;
    min-height: 0; /* критично для корректного flex-скролла внутри */
    overflow: hidden;
}

.modal-overlay.show .modal {
    transform: scale(1) translateY(0);
}

.modal-header {
    padding: 20px 24px;
    display: flex;
    align-items: center;
    justify-content: space-between;
    border-bottom: 1px solid var(--border);
    flex-shrink: 0;
}

.modal-header h2 {
    font-size: 18px;
    font-weight: 700;
}

.modal-close {
    width: 32px;
    height: 32px;
    border-radius: 8px;
    border: none;
    background: var(--bg);
    cursor: pointer;
    font-size: 16px;
    display: flex;
    align-items: center;
    justify-content: center;
    transition: background 0.2s ease;
    flex-shrink: 0;
}

.modal-close:hover {
    background: #fee2e2;
    color: #dc2626;
}

/*
  ФИКС второго бага: min-height:0 + flex позволяют overflow-y:auto
  реально работать внутри flex-колонки, а не раздувать .modal
  за пределы max-height (что и вызывало "вылезание" на мобильных).
*/
.modal-body {
    padding: 20px 24px 24px;
    display: flex;
    flex-direction: column;
    gap: 16px;
    overflow-y: auto;
    flex: 1 1 auto;
    min-height: 0;
}

.setting-row {
    display: flex;
    flex-direction: column;
    gap: 8px;
}

.setting-label {
    font-size: 13px;
    font-weight: 600;
    color: var(--text-light);
    text-transform: uppercase;
    letter-spacing: 0.03em;
}

.custom-select {
    width: 100%;
    padding: 12px 16px;
    border: 1px solid var(--border);
    border-radius: var(--radius-sm);
    background: var(--bg);
    font-size: 14px;
    font-family: inherit;
    cursor: pointer;
    display: flex;
    justify-content: space-between;
    align-items: center;
    transition: all 0.2s ease;
    color: var(--text);
}

.custom-select:hover {
    border-color: var(--primary);
}

.custom-select.active {
    border-color: var(--primary);
    background: var(--primary-light);
}

.custom-select.active .chevron {
    transform: rotate(180deg);
}

/* Встроенный (не absolute!) список классов — не выходит за пределы модалки */
.grade-list {
    max-height: 0;
    overflow-y: auto;
    border: 1px solid transparent;
    border-radius: var(--radius-sm);
    transition: max-height 0.25s ease, margin-top 0.25s ease, border-color 0.25s ease;
    margin-top: 0;
}

.grade-list.open {
    max-height: 210px;
    margin-top: 8px;
    border-color: var(--border);
    background: var(--card-bg);
    box-shadow: var(--shadow);
}

.grade-item {
    padding: 11px 16px;
    cursor: pointer;
    font-size: 14px;
    transition: background 0.15s ease;
}

.grade-item:hover {
    background: var(--bg);
}

.grade-item.selected {
    color: var(--primary);
    font-weight: 600;
    background: var(--primary-light);
}

.setting-item-btn {
    display: flex;
    align-items: center;
    gap: 12px;
    padding: 14px 16px;
    border: 1px solid var(--border);
    border-radius: var(--radius-sm);
    background: var(--bg);
    cursor: pointer;
    transition: all 0.2s ease;
    font-size: 14px;
    font-weight: 500;
    color: var(--text);
    width: 100%;
    font-family: inherit;
}

.setting-item-btn:hover {
    background: var(--primary-light);
    border-color: var(--primary);
}

.setting-item-btn .arrow {
    margin-left: auto;
    color: var(--text-light);
}

.about-content {
    text-align: center;
    padding: 10px 0;
}

.about-logo {
    width: 64px;
    height: 64px;
    border-radius: 16px;
    margin: 0 auto 16px;
    display: flex;
    align-items: center;
    justify-content: center;
    overflow: hidden;
    background: transparent; /* убираем градиентную подложку под картинку */
}

.about-logo-img {
    width: 100%;
    height: 100%;
    object-fit: contain;
    display: block;
}

.about-content h3 {
    font-size: 18px;
    margin-bottom: 4px;
}

.about-content .version {
    color: var(--text-light);
    font-size: 13px;
    margin-bottom: 16px;
}

.about-content p {
    font-size: 14px;
    color: var(--text-light);
    line-height: 1.6;
}

.back-btn {
    display: flex;
    align-items: center;
    gap: 6px;
    background: none;
    border: none;
    color: var(--primary);
    font-size: 14px;
    font-weight: 600;
    cursor: pointer;
    padding: 4px 0;
    margin-bottom: 8px;
    font-family: inherit;
}

.toast {
    position: fixed;
    bottom: 24px;
    left: 50%;
    transform: translateX(-50%) translateY(100px);
    background: var(--text);
    color: white;
    padding: 14px 24px;
    border-radius: var(--radius-sm);
    font-size: 14px;
    font-weight: 500;
    box-shadow: var(--shadow-lg);
    opacity: 0;
    transition: all 0.3s ease;
    z-index: 2000;
    max-width: 90%;
    text-align: center;
}

.toast.show {
    opacity: 1;
    transform: translateX(-50%) translateY(0);
}

footer {
    text-align: center;
    padding: 20px;
    color: var(--text-light);
    font-size: 13px;
    border-top: 1px solid var(--border);
}

/* ==========================================================
   ТЁМНАЯ ТЕМА — явная перекраска иконок
   Используем !important и селекторы с *, чтобы пробить shadow DOM от <use>
   ========================================================== */

[data-theme="dark"] .icon-btn,
[data-theme="dark"] .logo-icon,
[data-theme="dark"] .dropdown-btn,
[data-theme="dark"] .modal-close,
[data-theme="dark"] .back-btn,
[data-theme="dark"] .setting-item-btn,
[data-theme="dark"] .about-logo {
    color: #ffffff !important;
}

[data-theme="dark"] .icon-btn svg,
[data-theme="dark"] .icon-btn svg *,
[data-theme="dark"] .logo-icon svg,
[data-theme="dark"] .logo-icon svg *,
[data-theme="dark"] .dropdown-btn svg,
[data-theme="dark"] .dropdown-btn svg *,
[data-theme="dark"] .modal-close svg,
[data-theme="dark"] .modal-close svg *,
[data-theme="dark"] .back-btn svg,
[data-theme="dark"] .back-btn svg *,
[data-theme="dark"] .setting-item-btn svg,
[data-theme="dark"] .setting-item-btn svg *,
[data-theme="dark"] .about-logo svg,
[data-theme="dark"] .about-logo svg * {
    stroke: #ffffff !important;
    color: #ffffff !important;
}

/* Иконки в выпадающем списке предметов */
[data-theme="dark"] .dropdown-item {
    color: #cbd5e1;
}
[data-theme="dark"] .dropdown-item svg,
[data-theme="dark"] .dropdown-item svg * {
    stroke: #cbd5e1 !important;
}
[data-theme="dark"] .dropdown-item.selected,
[data-theme="dark"] .dropdown-item.selected svg,
[data-theme="dark"] .dropdown-item.selected svg * {
    color: #ffffff !important;
    stroke: #ffffff !important;
}

/* Иконки внутри action-card в тёмной теме */
[data-theme="dark"] .action-icon {
    background: rgba(129, 140, 248, 0.15);
}
[data-theme="dark"] .action-icon svg,
[data-theme="dark"] .action-icon svg * {
    stroke: var(--primary) !important;
}

/* Hover для кнопки закрытия модалки в тёмной теме */
[data-theme="dark"] .modal-close:hover {
    background: rgba(248, 113, 113, 0.15);
    color: #fca5a5 !important;
}
[data-theme="dark"] .modal-close:hover svg,
[data-theme="dark"] .modal-close:hover svg * {
    stroke: #fca5a5 !important;
}

/* ===== АДАПТИВНОСТЬ ===== */

@media (max-width: 768px) {
    .hero h1 { font-size: 26px; }
    .hero p { font-size: 14px; }
    .btn-main { padding: 16px 32px; font-size: 16px; }
    .actions-grid {
        grid-template-columns: repeat(auto-fit, minmax(160px, 1fr));
        gap: 14px;
    }
    .action-card { padding: 20px 18px; }
}

@media (max-width: 560px) {
    .header-inner { padding: 12px 16px; gap: 8px; }
    .logo span.logo-text { display: none; }
    .dropdown-btn span.btn-label { display: none; }
    .dropdown-btn { padding: 10px 12px; }

    main { padding: 24px 14px 40px; }
    .hero { margin-bottom: 28px; }
    .hero h1 { font-size: 22px; }

    .btn-main { width: 100%; justify-content: center; padding: 16px 20px; }

    .actions-grid { grid-template-columns: 1fr 1fr; }
    .action-card { padding: 18px 14px; gap: 10px; }
    .action-icon { width: 40px; height: 40px; font-size: 18px; }
    .action-card h3 { font-size: 15px; }
    .action-card p { font-size: 12.5px; }

    /* Модалка — "шторка" снизу на мобильных */
    .modal-overlay {
        padding: 12px;
        align-items: flex-end;
    }
    .modal {
        max-width: 100%;
        max-height: 80vh;
        border-radius: var(--radius) var(--radius) 0 0;
        width: 100%;
    }
    .modal-header { padding: 16px 18px; }
    .modal-body { padding: 16px 18px 20px; }

    /*
      Важно: здесь НЕ задаём position:fixed напрямую в CSS для .dropdown-menu,
      т.к. top/left нужно вычислять относительно кнопки — это делает JS
      (см. script.js, функция positionDropdownMenu).
    */
}

@media (max-width: 380px) {
    .actions-grid { grid-template-columns: 1fr; }
}
/* ===== СТРАНИЦА "СЛУЧАЙНАЯ СТАТЬЯ" (отдельная страница, стиль учебника) ===== */
.ra-main {
    max-width: 720px;
}

.ra-card {
    background: var(--card-bg);
    border: 1px solid var(--border);
    border-radius: var(--radius);
    overflow: hidden;
}

[data-theme="dark"] .ra-card {
    background: var(--bg);
}

/* Шапка раздела — без hover/курсора (статична) */
.ra-unit-header {
    cursor: default;
    user-select: none;
    border-bottom: 1px solid var(--border);
}

.ra-unit-header:hover {
    background: transparent;
}

/* Заголовок темы — без hover/курсора (статичен) */
.ra-topic-header {
    cursor: default;
    user-select: none;
    padding-top: 16px;
}

.ra-topic-header:hover {
    background: transparent;
}

.ra-definition {
    padding-top: 0;
}

/* Блок "Далее" внизу карточки */
.ra-footer {
    display: flex;
    align-items: center;
    justify-content: space-between;
    gap: 14px;
    padding: 16px 20px;
    border-top: 1px solid var(--border);
}

.ra-counter {
    font-size: 12px;
    color: var(--text-light);
}

.article-next-btn {
    padding: 10px 22px !important;
    font-size: 14px !important;
    gap: 8px !important;
    flex-shrink: 0;
}

.article-next-btn .btn-main-icon {
    font-size: 16px;
}

.article-next-btn .btn-main-icon .icon {
    width: 16px;
    height: 16px;
    stroke-width: 2;
}

@media (max-width: 560px) {
    .ra-footer {
        padding: 14px 16px;
    }
    .article-next-btn {
        padding: 10px 18px !important;
    }
    .ra-topic-header {
        padding: 12px 14px 12px 16px;
    }
    .ra-definition {
        padding-left: 40px;
        padding-right: 16px;
    }
}
