/* Header: навигация, меню, локаль и переключатель темы. */

/* Каркас и бренд */
#navbar {
    --header-surface: rgba(10, 10, 10, 0.8);
    --header-cutout-bg: #0a0a0a;
    --header-motion-duration: 200ms;
    --header-motion-ease: cubic-bezier(0.22, 1, 0.36, 1);
    --header-shell-duration: 400ms;
    position: fixed;
    top: 0;
    left: 0;
    width: 100%;
    padding: 1rem 5%;
    background: var(--header-surface);
    backdrop-filter: blur(20px);
    border-bottom: 1px solid var(--border);
    z-index: 1000;
    display: grid;
    grid-template-columns: auto 1fr auto;
    align-items: center;
    transition: padding-top var(--header-shell-duration) var(--header-motion-ease),
                padding-bottom var(--header-shell-duration) var(--header-motion-ease),
                background-color var(--header-shell-duration) var(--header-motion-ease),
                border-color var(--header-shell-duration) var(--header-motion-ease),
                box-shadow var(--header-shell-duration) var(--header-motion-ease);
    box-sizing: border-box;
}

#navbar.scrolled {
    --header-surface: rgba(10, 10, 10, 0.95);
    padding-top: 0.8rem;
    padding-bottom: 0.8rem;
    background: var(--header-surface);
    box-shadow: none;
}

/* Ссылка быстрого перехода */
.skip-link {
    position: fixed;
    top: 0.75rem;
    left: 0.75rem;
    z-index: 2000;
    padding: 0.7rem 1rem;
    color: var(--bg-primary);
    background: var(--text-primary);
    border-radius: 8px;
    text-decoration: none;
    transform: translateY(-160%);
    transition: transform 0.2s ease;
}

.skip-link:focus {
    transform: translateY(0);
}

.brand-lockup {
    display: inline-flex;
    align-items: center;
    gap: 0.56rem;
    color: var(--text-primary);
    text-decoration: none;
    white-space: nowrap;
    transform-origin: left center;
    transition: transform 0.3s ease;
}

.brand-mark {
    display: block;
    width: 3.47rem;
    height: 2rem;
    flex: 0 0 auto;
    background-color: currentColor;
    -webkit-mask: url('../../assets/brand/codeak-mark.png') center / contain no-repeat;
    mask: url('../../assets/brand/codeak-mark.png') center / contain no-repeat;
}

.brand-lockup:focus-visible,
.theme-toggle:focus-visible,
.mobile-menu-toggle:focus-visible,
.locale-option:focus-visible {
    outline: 2px solid var(--accent);
    outline-offset: 4px;
}

/* Светлые состояния шапки */
[data-theme="light"] #navbar {
    --header-surface: rgba(255, 255, 255, 0.8);
    --header-cutout-bg: #ffffff;
    background: var(--header-surface);
}

[data-theme="light"] #navbar.scrolled {
    --header-surface: rgba(255, 255, 255, 0.95);
    background: var(--header-surface);
}

/* Действия шапки */
.nav-actions {
    display: flex;
    gap: 1.5rem;
    align-items: center;
    justify-content: flex-end;
    min-height: 2.95rem;
    min-width: fit-content;
    flex-wrap: nowrap;
}

/* Выпадающая навигация */
.nav-links {
    display: none;
    position: absolute;
    top: calc(100% + 15px);
    right: 5%;
    left: auto;
    width: 260px;
    box-sizing: border-box;
    max-height: calc(100svh - 100% - 15px);
    margin: 0;
    padding: 1rem 1.5rem;
    gap: 0;
    flex-direction: column;
    justify-content: center;
    list-style: none;
    background: rgba(10, 10, 10, 0.95);
    backdrop-filter: blur(15px);
    -webkit-backdrop-filter: blur(15px);
    border: 1px solid var(--border);
    border-radius: 16px;
    box-shadow: 0 15px 40px rgba(0, 0, 0, 0.3);
    opacity: 0;
    visibility: hidden;
    pointer-events: none;
    overflow-y: auto;
    transform: none;
    z-index: 1000;
}

[data-theme="light"] .nav-links {
    background: rgba(255, 255, 255, 0.85);
}

.nav-links.active {
    display: flex;
    opacity: 1;
    visibility: visible;
    pointer-events: auto;
}

.nav-links a {
    color: var(--text-secondary);
    text-decoration: none;
    transition: color 0.2s cubic-bezier(0.4, 0, 0.2, 1),
                opacity 0.2s cubic-bezier(0.4, 0, 0.2, 1);
    font-size: 0.85rem;
    font-weight: 500;
    text-transform: uppercase;
    letter-spacing: 0.05em;
    position: relative;
    white-space: nowrap;
    opacity: 0.7;
}

.nav-links a:hover {
    color: var(--text-primary);
    opacity: 1;
}

.nav-links a[aria-current="location"],
.nav-links a[aria-current="page"] {
    color: var(--text-primary);
    opacity: 1;
}

.nav-links a::after {
    content: '';
    position: absolute;
    bottom: -5px;
    left: 0;
    width: 0;
    height: 1.5px;
    background: var(--accent);
    transition: width 0.2s cubic-bezier(0.4, 0, 0.2, 1);
}

.nav-links a:hover::after {
    width: 100%;
}

.nav-links a[aria-current="location"]::after,
.nav-links a[aria-current="page"]::after {
    width: 100%;
}

.nav-active-indicator {
    display: none;
}

/* Переключатель темы */
.theme-toggle {
    --moon-cutout-bg: var(--header-cutout-bg, var(--bg-primary));
    height: 2.6rem;
    width: 2.6rem;
    background: var(--bg-card);
    border: 1.5px solid var(--border-light);
    border-radius: 8px;
    cursor: pointer;
    position: relative;
    transition: color 0.3s cubic-bezier(0.4, 0, 0.2, 1),
                background-color 0.3s cubic-bezier(0.4, 0, 0.2, 1),
                border-color 0.3s cubic-bezier(0.4, 0, 0.2, 1),
                box-shadow 0.3s cubic-bezier(0.4, 0, 0.2, 1);
    display: flex;
    align-items: center;
    justify-content: center;
    overflow: visible;
}

.theme-toggle:hover {
    border-color: var(--accent);
    box-shadow: 0 4px 15px var(--accent-glow);
}

.theme-toggle .moon {
    position: absolute;
    width: 18px;
    height: 18px;
    opacity: 1;
    transform: scale(1) rotate(-45deg);
    transition: opacity 0.4s cubic-bezier(0.4, 0, 0.2, 1),
                transform 0.4s cubic-bezier(0.4, 0, 0.2, 1);
    margin-left: 2px;
    margin-bottom: 2px;
}

.theme-toggle .moon::before {
    content: '';
    position: absolute;
    width: 14px;
    height: 14px;
    background: var(--text-primary);
    border-radius: 50%;
    top: 50%;
    left: 50%;
    transform: translate(-50%, -50%);
}

.theme-toggle .moon::after {
    content: '';
    position: absolute;
    width: 14px;
    height: 14px;
    background: var(--moon-cutout-bg);
    border-radius: 50%;
    top: 52%;
    left: 55%;
    transform: translate(-35%, -50%);
}

.theme-toggle .sun {
    position: absolute;
    width: 16px;
    height: 16px;
    opacity: 0;
    transform: scale(0.5) rotate(90deg);
    transition: opacity 0.4s cubic-bezier(0.4, 0, 0.2, 1),
                transform 0.4s cubic-bezier(0.4, 0, 0.2, 1);
}

.theme-toggle .sun::before {
    content: '';
    position: absolute;
    top: 50%;
    left: 50%;
    transform: translate(-50%, -50%);
    width: 8px;
    height: 8px;
    background: var(--text-primary);
    border-radius: 50%;
}

.theme-toggle .sun::after {
    content: '';
    position: absolute;
    top: 50%;
    left: 50%;
    transform: translate(-50%, -50%);
    width: 2px;
    height: 2px;
    border-radius: 50%;
    box-shadow:
        0 -7px 0 0 var(--text-primary),
        0 7px 0 0 var(--text-primary),
        7px 0 0 0 var(--text-primary),
        -7px 0 0 0 var(--text-primary),
        5px 5px 0 0 var(--text-primary),
        -5px -5px 0 0 var(--text-primary),
        5px -5px 0 0 var(--text-primary),
        -5px 5px 0 0 var(--text-primary);
}

[data-theme="light"] .theme-toggle .sun {
    opacity: 1;
    transform: scale(1) rotate(0deg);
}

[data-theme="light"] .theme-toggle .moon {
    opacity: 0;
    transform: scale(0.5) rotate(-45deg);
}

[data-theme="light"] .theme-toggle {
    background: var(--bg-card);
    border-color: var(--border);
}

/* Переключатель локали */
.locale-switcher {
    display: inline-flex;
    align-items: center;
    min-height: 2.5rem;
    padding: 0;
    color: var(--text-muted);
    background: transparent;
    border: 0;
    border-radius: 0;
}

.locale-option {
    min-height: 2.5rem;
    padding: 0 0.4rem;
    color: inherit;
    background: transparent;
    border: 0;
    border-radius: 0;
    font: inherit;
    font-size: 0.76rem;
    font-weight: 700;
    letter-spacing: 0.06em;
    cursor: pointer;
    transition: color 0.2s ease;
}

.locale-option:hover,
.locale-option.is-current {
    color: var(--text-primary);
}

.locale-option.is-current {
    color: var(--accent);
}

.locale-divider {
    color: var(--border-light);
    font-size: 0.72rem;
    user-select: none;
}

[data-theme="light"] #navbar .locale-divider {
    color: color-mix(in srgb, var(--text-muted) 55%, transparent);
}

[data-theme="light"] #navbar .theme-toggle {
    border-color: color-mix(in srgb, var(--text-muted) 32%, transparent);
}

.mobile-tools-wrapper .locale-switcher {
    min-height: 2.25rem;
    background: transparent;
    border: 0;
}

/* Кнопка мобильного меню */
.mobile-menu-toggle {
    display: flex;
    flex-direction: column;
    gap: 5px;
    background: none;
    border: none;
    cursor: pointer;
    padding: 5px 2px;
    z-index: 1001;
    position: relative;
}

.mobile-menu-toggle span {
    width: 22px;
    height: 2px;
    background: var(--text-primary);
    transition: transform 0.24s ease, opacity 0.18s ease, background-color 0.24s ease;
    border-radius: 2px;
    transform-origin: center;
}

.mobile-menu-toggle.active span:nth-child(1) {
    transform: translateY(7px) rotate(45deg);
}

.mobile-menu-toggle.active span:nth-child(2) {
    opacity: 0;
    transform: scaleX(0);
}

.mobile-menu-toggle.active span:nth-child(3) {
    transform: translateY(-7px) rotate(-45deg);
}

/* Инструменты внутри мобильного меню */
.mobile-only-450 {
    display: none;
}

.nav-links .mobile-only-450 {
    justify-content: center;
    align-items: center;
    padding: 0.5rem 0 0;
    margin-top: 0;
    border-top: none;
    width: 100%;
}

.mobile-tools-wrapper {
    display: flex;
    align-items: center;
    justify-content: space-between;
    gap: 0.75rem;
    width: 100%;
    min-height: 2.5rem;
    box-sizing: border-box;
    background: transparent;
    padding: 0;
    border: 0;
    border-radius: 0;
    box-shadow: none;
    margin: 0;
}

.mobile-tools-wrapper .theme-toggle {
    --moon-cutout-bg: var(--header-cutout-bg, var(--bg-primary));
    margin: 0;
    border: 1px solid transparent;
    border-radius: 8px;
    background: transparent;
    box-shadow: none;
    height: 2rem;
    width: 2rem;
    display: inline-flex;
    align-items: center;
    justify-content: center;
}

.mobile-tools-wrapper .theme-toggle:hover,
.mobile-tools-wrapper .theme-toggle:focus-visible {
    border-color: transparent;
    background: color-mix(in srgb, var(--accent) 9%, transparent);
    box-shadow: none;
}

/* Затемнение страницы под мобильным меню */
.mobile-menu-overlay {
    position: fixed;
    top: 0;
    left: 0;
    width: 100%;
    height: 100%;
    background: rgba(0, 0, 0, 0.4);
    backdrop-filter: blur(2px);
    opacity: 0;
    pointer-events: none;
    transition: opacity 0.3s ease;
    z-index: 998;
}

.mobile-menu-overlay.active {
    opacity: 1;
    pointer-events: auto;
}

/* Индикатор прокрутки */
.scroll-progress {
    position: fixed;
    top: 0;
    left: 0;
    height: 3px;
    background: var(--gradient-1);
    z-index: 1002;
    transition: width 0.1s;
}

/* Компактные элементы шапки */
.theme-toggle {
    background: transparent;
    border: none;
    width: auto;
    height: auto;
    padding: 0;
    color: var(--text-muted);
}

.theme-toggle:hover {
    color: var(--text-primary);
    box-shadow: none;
    border-color: transparent;
}

[data-theme="light"] .theme-toggle {
    background: transparent;
    border: none;
}

.nav-actions .nav-login-btn {
    padding: 0.7rem 1.6rem;
    font-size: 0.95rem;
    font-weight: 500;
    border-radius: 12px;
}

.nav-login-btn[hidden] {
    display: none !important;
}

/* Режим desktop-fit */
#navbar[data-nav-mode="adaptive"].is-desktop-fit .nav-actions {
    display: grid;
    grid-template-columns: auto auto auto;
    gap: 0 1.5rem;
}

#navbar[data-nav-mode="adaptive"].is-desktop-fit .nav-actions:has(.nav-login-btn[hidden]) {
    grid-template-columns: auto auto;
}

#navbar[data-nav-mode="adaptive"].is-desktop-fit .nav-actions > .theme-toggle {
    width: 2.25rem;
    height: 2.25rem;
    justify-self: end;
}

#navbar[data-nav-mode="adaptive"].is-desktop-fit .nav-actions > .locale-switcher {
    justify-self: center;
}

#navbar[data-nav-mode="adaptive"].is-desktop-fit .nav-actions > .nav-login-btn,
#navbar[data-nav-mode="adaptive"].is-desktop-fit .nav-actions > .mobile-menu-toggle {
    justify-self: start;
}

#navbar[data-nav-mode="adaptive"].is-desktop-fit .nav-actions > .mobile-menu-toggle {
    padding-inline: 0;
}

#navbar[data-nav-mode="adaptive"].is-desktop-fit .nav-links {
    display: flex;
    position: relative;
    top: auto;
    right: auto;
    left: auto;
    width: auto;
    margin: 0 auto;
    padding: 0 1rem;
    gap: clamp(0.75rem, 1.3vw, 1.45rem);
    flex-direction: row;
    background: transparent;
    backdrop-filter: none;
    -webkit-backdrop-filter: none;
    border: 0;
    border-radius: 0;
    box-shadow: none;
    opacity: 1;
    visibility: visible;
    pointer-events: auto;
    overflow: visible;
    transform: none;
    z-index: auto;
}

#navbar[data-nav-mode="adaptive"].is-desktop-fit .mobile-menu-toggle {
    display: none;
}

#navbar[data-nav-mode="adaptive"].is-desktop-fit .nav-links > li > a {
    display: inline-block;
    padding: 0;
}

#navbar[data-nav-mode="adaptive"].is-desktop-fit .nav-links a::after {
    display: block;
    width: 0;
}

#navbar[data-nav-mode="adaptive"].is-desktop-fit .nav-links a:hover:not([aria-current="location"]):not([aria-current="page"])::after {
    width: 100%;
}

#navbar[data-nav-mode="adaptive"].is-desktop-fit .nav-links a[aria-current="location"]::after,
#navbar[data-nav-mode="adaptive"].is-desktop-fit .nav-links a[aria-current="page"]::after {
    width: 0;
}

#navbar[data-nav-mode="adaptive"].is-desktop-fit .nav-active-indicator {
    position: absolute;
    bottom: -5px;
    left: 0;
    display: block;
    width: var(--nav-indicator-width, 0);
    height: 1.5px;
    padding: 0;
    background: var(--accent);
    opacity: 0;
    pointer-events: none;
    transform: translateX(var(--nav-indicator-x, 0));
}

#navbar[data-nav-mode="adaptive"].is-desktop-fit .nav-active-indicator.is-ready {
    transition: width var(--header-motion-duration) var(--header-motion-ease),
                transform var(--header-motion-duration) var(--header-motion-ease);
}

#navbar[data-nav-mode="adaptive"].is-desktop-fit .nav-active-indicator.is-visible {
    opacity: 1;
}

#navbar[data-nav-mode="adaptive"].is-desktop-fit .nav-actions .btn-primary {
    display: inline-flex;
}

/* Скрытые измерительные копии */
.header-layout-probe {
    position: fixed !important;
    inset: 0 auto auto -10000px !important;
    display: flex !important;
    width: max-content !important;
    max-width: none !important;
    visibility: hidden !important;
    pointer-events: none !important;
}

.header-nav-probe {
    padding: 0 !important;
    gap: clamp(0.75rem, 1.3vw, 1.45rem) !important;
    flex-direction: row !important;
}

.header-actions-probe > .mobile-menu-toggle {
    display: none !important;
}

.header-actions-probe > .desktop-switcher,
.header-actions-probe > .locale-switcher,
.header-actions-probe > .theme-toggle,
.header-actions-probe > .nav-login-btn {
    display: inline-flex !important;
}

.header-actions-probe > .theme-toggle {
    width: 2.25rem !important;
}

.header-actions-probe .locale-option {
    padding-inline: 0.4rem !important;
}

/* Режим с burger-меню */
#navbar:not(.is-desktop-fit) .nav-actions > .desktop-switcher,
#navbar:not(.is-desktop-fit) .nav-actions > .locale-switcher,
#navbar:not(.is-desktop-fit) .nav-actions > .theme-toggle {
    display: none !important;
}

#navbar:not(.is-desktop-fit) .nav-actions {
    gap: clamp(0.65rem, 1.8vw, 1.15rem);
}

#navbar:not(.is-desktop-fit) .nav-login-btn {
    order: 1;
}

#navbar:not(.is-desktop-fit) .mobile-menu-toggle {
    order: 2;
}

#navbar:not(.is-desktop-fit) .nav-actions > .theme-toggle {
    width: 2rem;
    height: 2rem;
}

#navbar:not(.is-desktop-fit) .mobile-nav-tools {
    display: flex;
}

#navbar:not(.is-desktop-fit) #navLinks.active li {
    width: 100%;
    border-bottom: 1px solid rgba(255, 255, 255, 0.08);
}

#navbar:not(.is-desktop-fit) #navLinks.active li:last-child {
    border-bottom: none;
}

#navbar:not(.is-desktop-fit) #navLinks.active .mobile-nav-tools,
#navbar:not(.is-desktop-fit) #navLinks.active .nav-active-indicator {
    border-bottom: none;
}

#navbar:not(.is-desktop-fit) #navLinks.active > li:nth-last-of-type(2) {
    border-bottom: none;
}

#navbar:not(.is-desktop-fit) #navLinks.active .mobile-nav-tools {
    border-bottom: none;
}

[data-theme="light"] #navbar:not(.is-desktop-fit) #navLinks.active .mobile-nav-tools {
    border-top-color: color-mix(in srgb, var(--text-muted) 38%, transparent);
}

#navbar:not(.is-desktop-fit) #navLinks.active a {
    position: relative;
    display: flex;
    align-items: center;
    width: 100%;
    padding: 0.8rem 0 0.6rem;
    line-height: 1.25;
}

#navbar:not(.is-desktop-fit) #navLinks.active a::after {
    display: block;
    bottom: 0rem;
    width: 0;
    height: 1px;
}

#navbar:not(.is-desktop-fit) #navLinks.active a:is(:hover, :focus-visible) {
    color: var(--text-primary);
    opacity: 1;
}

#navbar:not(.is-desktop-fit) #navLinks.active a:is(:hover, :focus-visible)::after,
#navbar:not(.is-desktop-fit) #navLinks.active a:is([aria-current="location"], [aria-current="page"])::after {
    width: 100%;
}

/* Мобильная версия */
@media (max-width: 450px) {
    .mobile-tools-wrapper .theme-toggle .moon {
        margin-left: 0;
        margin-bottom: 0;
    }

    #navbar {
        padding-left: clamp(10px, 3.5vw, 16px);
        padding-right: clamp(10px, 3.5vw, 16px);
    }

    .nav-actions .btn-outline {
        padding: 0.52rem clamp(0.72rem, 2.6vw, 1.05rem);
        font-size: 0.85rem;
    }

    .mobile-menu-toggle {
        padding: 4px 2px;
    }

    .brand-lockup {
        gap: 0.5rem;
    }

    .brand-mark {
        width: 3.04rem;
        height: 1.75rem;
    }

}

/* Уменьшенное движение */
@media (prefers-reduced-motion: reduce) {
    .skip-link,
    #navbar,
    .nav-links a,
    .theme-toggle,
    .mobile-menu-toggle span,
    .nav-active-indicator {
        transition-duration: 0.01ms;
    }

    .brand-lockup,
    .brand-lockup:hover {
        transform: none;
        transition-duration: 0s;
    }

    .nav-active-indicator {
        transition: none;
    }

    .nav-links {
        transition: none;
    }
}

@media (max-width: 360px) {
    .nav-links {
        width: min(92vw, 250px);
    }

    .nav-login-btn {
        display: none !important;
    }
}

@media (max-width: 281px) {
    .nav-login-btn {
        display: none !important;
    }
}

[data-theme="light"] #navbar:not(.is-desktop-fit) #navLinks.active li {
    border-bottom-color: rgba(0, 0, 0, 0.15);
}
