/* ============================================================
   Family Track — верхняя навигация (общий компонент для всех страниц)
   Десктоп: одна горизонтальная строка.
   Мобайл (<900px): логотип + бургер, меню выезжает панелью.
   ============================================================ */

:root {
    --tb-ink: #0f172a;
    --tb-muted: #475569;
    --tb-line: #e8edf3;
    --tb-bg: #ffffff;
    --tb-accent: #2563eb;
    --tb-accent-strong: #1d4ed8;
    --tb-hover: #f1f5f9;
    --tb-radius: 10px;
}

/* Спейсер в потоке документа — высота задаётся в topbar.js под реальную высоту панели */
.app-topbar-spacer {
    width: 100%;
    flex-shrink: 0;
    pointer-events: none;
}

.app-topbar {
    position: fixed;
    top: 0;
    left: 0;
    right: 0;
    width: 100%;
    z-index: 900;
    background: #ffffff;
    border-bottom: 1px solid var(--tb-line);
    box-shadow: 0 1px 2px rgba(15, 23, 42, 0.04);
    font-family: 'Golos Text', 'Inter', 'Nunito', sans-serif;
    font-synthesis-weight: none;
    -webkit-font-smoothing: antialiased;
    text-rendering: optimizeLegibility;
}

.app-topbar-inner {
    width: 100%;
    max-width: 1240px;
    margin: 0 auto;
    padding: 0 16px;
    display: flex;
    align-items: center;
    gap: 8px 16px;
    min-height: 60px;
    min-width: 0;
    box-sizing: border-box;
}

/* ---------- Логотип ---------- */
.app-top-logo {
    flex-shrink: 0;
    font-size: 1.35rem;
    font-weight: 900;
    font-family: 'Golos Text', 'Inter', 'Nunito', sans-serif;
    color: var(--tb-ink);
    text-decoration: none;
    letter-spacing: -0.03em;
    line-height: 1;
}
.app-top-logo span {
    color: var(--tb-accent);
}

/* ---------- Контейнер навигации (десктоп: разворачивается в строку) ---------- */
.app-topbar-collapse {
    flex: 1 1 auto;
    display: flex;
    align-items: center;
    gap: 8px 12px;
    min-width: 0;
}

.app-topbar-nav-block {
    display: flex;
    flex-wrap: wrap;
    align-items: center;
    gap: 4px 6px;
    min-width: 0;
}

/* Предметы — сгруппированы и отделены тонкой линией */
.app-top-subjects {
    display: flex;
    flex-wrap: wrap;
    align-items: center;
    gap: 4px 4px;
    padding-right: 10px;
    margin-right: 6px;
    border-right: 1px solid var(--tb-line);
}

.app-top-link {
    color: var(--tb-muted);
    text-decoration: none;
    font-weight: 600;
    font-size: 0.88rem;
    padding: 7px 9px;
    border-radius: var(--tb-radius);
    transition: background 0.15s ease, color 0.15s ease;
    line-height: 1.2;
    white-space: nowrap;
}
.app-top-link:hover {
    background: var(--tb-hover);
    color: var(--tb-ink);
}
.app-top-link.is-active {
    color: var(--tb-accent);
    background: rgba(37, 99, 235, 0.08);
}

/* Выпадающее меню «Предметы» (пункты строятся из /api/subjects/catalog) */
.app-top-dropdown { position: relative; flex-shrink: 0; }
.app-top-dropdown-toggle {
    border: 0;
    background: transparent;
    cursor: pointer;
    display: inline-flex;
    align-items: center;
    gap: 5px;
    font: inherit;
    font-weight: 600;
    font-size: 0.88rem;
}
.app-top-caret {
    font-size: 0.72em;
    line-height: 1;
    transition: transform 0.15s ease;
}
.app-top-dropdown.is-open .app-top-caret { transform: rotate(180deg); }
.app-top-dropdown.is-open .app-top-dropdown-toggle { background: var(--tb-hover); color: var(--tb-ink); }
.app-top-dropdown-menu {
    position: absolute; top: calc(100% + 6px); left: 0; min-width: 230px;
    max-height: min(70vh, 480px); overflow-y: auto;
    background: #fff; border: 1px solid var(--tb-line); border-radius: 10px;
    box-shadow: 0 8px 22px rgba(15, 23, 42, 0.12); padding: 6px; display: none; z-index: 200;
}
.app-top-dropdown.is-open .app-top-dropdown-menu { display: block; }
.app-top-dropdown-item {
    display: block; text-decoration: none; color: #334155; font-size: 0.86rem;
    font-weight: 700; border-radius: 8px; padding: 8px 10px;
    white-space: nowrap;
}
.app-top-dropdown-item:hover { background: var(--tb-hover); color: var(--tb-ink); }
.app-top-dropdown-item.is-active { color: var(--tb-accent); background: rgba(37, 99, 235, 0.08); }
.app-top-subj-emoji {
    display: inline-block;
    width: 1.5em;
    margin-right: 4px;
    text-align: center;
}

/* ---------- Правая часть: действия и авторизация ---------- */
.app-topbar-right {
    flex: 0 0 auto;
    margin-left: auto;
    display: flex;
    align-items: center;
    justify-content: flex-end;
    gap: 8px;
    min-width: 0;
}

.app-topbar #admin-link-wrap { margin: 0; }
.app-topbar #admin-link-wrap a {
    color: var(--tb-accent);
    font-size: 0.88rem;
    font-weight: 700;
    text-decoration: none;
    padding: 7px 10px;
    border-radius: var(--tb-radius);
}
.app-topbar #admin-link-wrap a:hover { background: var(--tb-hover); }

/* Кнопка «Скачать» */
.app-topbar .btn-download {
    display: inline-flex;
    align-items: center;
    gap: 6px;
    margin: 0;
    padding: 8px 14px;
    font-size: 0.86rem;
    font-weight: 800;
    line-height: 1;
    color: #fff;
    background: var(--tb-accent);
    border: 1px solid var(--tb-accent-strong);
    border-radius: var(--tb-radius);
    cursor: pointer;
    text-decoration: none;
    white-space: nowrap;
    transition: background 0.15s ease, transform 0.15s ease, box-shadow 0.15s ease;
    box-shadow: 0 6px 16px rgba(37, 99, 235, 0.22);
}
.app-topbar .btn-download:hover {
    background: var(--tb-accent-strong);
    transform: translateY(-1px);
    box-shadow: 0 8px 20px rgba(37, 99, 235, 0.28);
}

/* Авторизация */
.app-topbar .auth-panel {
    margin: 0;
    display: flex;
    flex-direction: row;
    align-items: center;
    gap: 6px;
}
.app-topbar .auth-topbar-guest {
    display: flex;
    align-items: center;
    gap: 4px;
}
.app-topbar .auth-top-link {
    color: var(--tb-muted);
    text-decoration: none;
    font-weight: 700;
    font-size: 0.86rem;
    padding: 8px 12px;
    border-radius: var(--tb-radius);
    white-space: nowrap;
    transition: background 0.15s ease, color 0.15s ease, border-color 0.15s ease;
}
.app-topbar .auth-top-link:hover { background: var(--tb-hover); color: var(--tb-ink); }
.app-topbar .auth-top-link--accent {
    color: var(--tb-accent);
    border: 1px solid rgba(37, 99, 235, 0.35);
}
.app-topbar .auth-top-link--accent:hover {
    background: rgba(37, 99, 235, 0.08);
    color: var(--tb-accent-strong);
    border-color: var(--tb-accent);
}

.app-topbar .auth-user {
    display: flex;
    align-items: center;
    gap: 10px;
    font-size: 0.9rem;
    font-weight: 700;
    color: var(--tb-ink);
}
.app-topbar .auth-user #auth-user-label { white-space: nowrap; }
.app-topbar #auth-minutes-link {
    background: var(--tb-accent);
    border: 1px solid var(--tb-accent-strong);
    color: #fff;
    cursor: pointer;
    padding: 6px 12px;
    border-radius: var(--tb-radius);
    font: inherit;
    font-weight: 700;
    white-space: nowrap;
}
.app-topbar #auth-minutes-link:hover { background: var(--tb-accent-strong); }
.app-topbar #logout-btn {
    background: #fff;
    border: 1px solid var(--tb-line);
    color: var(--tb-muted);
    cursor: pointer;
    padding: 7px 12px;
    border-radius: var(--tb-radius);
    font: inherit;
    font-weight: 700;
    white-space: nowrap;
    transition: background 0.15s ease, color 0.15s ease;
}
.app-topbar #logout-btn:hover { background: var(--tb-hover); color: var(--tb-ink); }
.app-topbar .auth-error {
    color: #b91c1c;
    font-size: 0.82rem;
    font-weight: 700;
}

/* ---------- Бургер (только мобайл) ---------- */
.app-topbar-burger {
    display: none;
    flex-shrink: 0;
    margin-left: auto;
    width: 42px;
    height: 42px;
    padding: 0;
    border: 1px solid var(--tb-line);
    border-radius: var(--tb-radius);
    background: #fff;
    cursor: pointer;
    align-items: center;
    justify-content: center;
}
.app-topbar-burger:hover { background: var(--tb-hover); }
.app-burger-box {
    position: relative;
    display: block;
    width: 20px;
    height: 14px;
}
.app-burger-line,
.app-burger-line::before,
.app-burger-line::after {
    position: absolute;
    left: 0;
    width: 20px;
    height: 2px;
    border-radius: 2px;
    background: var(--tb-ink);
    transition: transform 0.22s ease, opacity 0.18s ease, top 0.22s ease;
}
.app-burger-line { top: 6px; }
.app-burger-line::before { content: ""; top: -6px; }
.app-burger-line::after { content: ""; top: 6px; }
/* Анимация в «крест» при открытии */
.is-menu-open .app-burger-line { background: transparent; }
.is-menu-open .app-burger-line::before { top: 0; transform: rotate(45deg); }
.is-menu-open .app-burger-line::after { top: 0; transform: rotate(-45deg); }

.app-topbar-backdrop { display: none; }

.page-with-topbar { padding-top: 10px; }

/* ============================================================
   ДЕСКТОП ≥900px
   ============================================================ */
@media (min-width: 900px) {
    .app-topbar-inner { padding: 0 28px; }
    /* Полупрозрачность с блюром — только на десктопе: на узких вьюпортах
       backdrop-filter у fixed-панели даёт артефакты отрисовки в Chromium. */
    .app-topbar {
        background: rgba(255, 255, 255, 0.85);
        -webkit-backdrop-filter: saturate(180%) blur(12px);
        backdrop-filter: saturate(180%) blur(12px);
    }
}

/* ============================================================
   МОБАЙЛ / ПЛАНШЕТ-ПОРТРЕТ <900px
   ============================================================ */
@media (max-width: 899px) {
    .app-topbar-inner {
        min-height: 58px;
        padding: 0 14px;
        gap: 10px;
        flex-wrap: nowrap;
    }

    .app-topbar-burger { display: inline-flex; }

    /* Меню скрыто за бургером и выезжает панелью под шапкой */
    .app-topbar-collapse {
        position: absolute;
        top: 100%;
        left: 0;
        right: 0;
        flex-direction: column;
        align-items: stretch;
        gap: 0;
        padding: 8px 12px 16px;
        background: #fff;
        border-bottom: 1px solid var(--tb-line);
        box-shadow: 0 18px 40px rgba(15, 23, 42, 0.16);
        transform: translateY(-12px);
        opacity: 0;
        visibility: hidden;
        pointer-events: none;
        transition: transform 0.22s ease, opacity 0.2s ease, visibility 0.2s;
        max-height: calc(100vh - 58px);
        overflow-y: auto;
    }
    .is-menu-open .app-topbar-collapse {
        transform: translateY(0);
        opacity: 1;
        visibility: visible;
        pointer-events: auto;
    }

    .app-topbar-nav-block {
        flex-direction: column;
        align-items: stretch;
        gap: 2px;
        padding: 6px 0;
        border-bottom: 1px solid var(--tb-line);
    }
    .app-top-subjects {
        flex-direction: column;
        align-items: stretch;
        gap: 2px;
        padding: 0 0 6px;
        margin: 0 0 6px;
        border-right: 0;
        border-bottom: 1px solid var(--tb-line);
    }
    .app-top-link {
        display: block;
        font-size: 1rem;
        padding: 12px 12px;
        border-radius: var(--tb-radius);
        white-space: normal;
    }

    /* Предметы в мобильном меню — просто развёрнутый список (без дропдауна) */
    .app-top-dropdown { position: static; width: 100%; }
    .app-top-dropdown-toggle { display: none; }
    .app-top-dropdown-menu {
        display: block;
        position: static;
        min-width: 0;
        max-height: none;
        overflow: visible;
        border: 0;
        box-shadow: none;
        border-radius: 0;
        padding: 0;
        background: transparent;
    }
    .app-top-dropdown-item {
        font-size: 1rem;
        padding: 12px;
        border-radius: var(--tb-radius);
        white-space: normal;
    }

    /* Правая часть в мобильном меню — вертикально, на всю ширину */
    .app-topbar-right {
        flex-direction: column;
        align-items: stretch;
        margin: 0;
        gap: 8px;
        padding-top: 12px;
    }
    .app-topbar #admin-link-wrap a { padding: 12px; }
    .app-topbar .btn-download {
        justify-content: center;
        padding: 12px 14px;
        font-size: 0.95rem;
    }
    .app-topbar .auth-panel {
        flex-direction: column;
        align-items: stretch;
        gap: 8px;
    }
    .app-topbar .auth-topbar-guest {
        display: grid;
        grid-template-columns: 1fr 1fr;
        gap: 8px;
    }
    .app-topbar .auth-top-link {
        text-align: center;
        padding: 12px;
        border: 1px solid var(--tb-line);
        font-size: 0.95rem;
    }
    .app-topbar .auth-top-link--accent {
        border-color: rgba(37, 99, 235, 0.35);
    }
    .app-topbar .auth-user {
        flex-direction: column;
        align-items: stretch;
        gap: 8px;
    }
    .app-topbar .auth-user #auth-user-label {
        white-space: normal;
        padding: 4px 2px;
    }
    .app-topbar #auth-minutes-link,
    .app-topbar #logout-btn {
        padding: 12px;
        text-align: center;
    }

    /* Затемнение за меню */
    .app-topbar-backdrop {
        display: block;
        position: fixed;
        inset: 0;
        top: 58px;
        background: rgba(15, 23, 42, 0.32);
        opacity: 0;
        visibility: hidden;
        transition: opacity 0.22s ease, visibility 0.22s;
        z-index: -1;
    }
    .is-menu-open .app-topbar-backdrop {
        opacity: 1;
        visibility: visible;
    }

    /* Блокируем прокрутку фона при открытом меню */
    body.app-topbar-menu-open { overflow: hidden; }
}
