/**
 * 全站统一顶栏 / 页内工具条
 * - Tier 1: .sa-site-bar — 52px sticky，品牌 + 页标题 + 元信息/操作
 * - Tier 2: .sa-page-toolbar — 页内筛选（可选第二行）
 * 兼容类名：.index-header / .global-search-container / .plate-toolbar（品牌行已迁出时）
 *
 * 契约（Page Shell）：
 * - 宽度 / 左右 gutter 只通过 --sa-topbar-max-width / --sa-topbar-px（移动端 --sa-topbar-px-mobile）
 * - 正文用 .sa-page-shell 或 .container，与顶栏 .sa-site-bar__inner 左右对齐
 * - 禁止页级 CSS 覆盖 .sa-site-bar__inner / .index-header-inner 的 max-width / padding / gap
 * - 密度页若需加宽：仅在 body 上覆盖 --sa-topbar-max-width，勿单独改 --sa-topbar-px
 *
 * 依赖 finance_nav/tokens.css — 请在模板中先 include _static_site_css.html（勿 @import，子表不带 ?v=）
 */

:root {
    --sa-topbar-height: 52px;
    --sa-topbar-bg: #f7f8fa;
    --sa-topbar-border: #e5e7eb;
    --sa-topbar-shadow: 0 1px 2px rgba(0, 0, 0, 0.05);
    --sa-topbar-max-width: 1400px;
    --sa-topbar-px: var(--spacing-md, 24px);
    --sa-topbar-px-mobile: 12px;
    --sa-topbar-brand-size: var(--font-size-lg, 18px);
    --sa-topbar-title-size: 17px;
    --sa-topbar-meta-size: var(--font-size-xs, 13px);
    --sa-page-toolbar-min-h: 48px;
    --sa-page-toolbar-bg: #f8f9fa;
    --sa-toolbar-control-h: 32px;
    --sa-toolbar-btn-h: 32px;
    --sa-toolbar-btn-radius: var(--border-radius-sm, 4px);
}

/* ── Tier 1：站点顶栏 ── */
.sa-site-bar,
.index-header {
    position: sticky;
    top: 0;
    z-index: 120;
    width: 100%;
    min-height: var(--sa-topbar-height);
    background: var(--sa-topbar-bg);
    border-bottom: 1px solid var(--sa-topbar-border);
    box-shadow: var(--sa-topbar-shadow);
    display: flex;
    align-items: center;
    flex-shrink: 0;
    box-sizing: border-box;
}

.sa-site-bar__inner,
.index-header-inner {
    width: 100%;
    max-width: var(--sa-topbar-max-width);
    margin: 0 auto;
    padding: 0 var(--sa-topbar-px);
    min-height: var(--sa-topbar-height);
    display: flex;
    align-items: center;
    justify-content: space-between;
    gap: var(--spacing-sm, 16px);
    box-sizing: border-box;
}

.sa-site-bar__start,
.sa-site-bar__end {
    display: flex;
    align-items: center;
    gap: 10px;
    min-width: 0;
}

.sa-site-bar__start {
    flex: 1;
    min-width: 0;
    flex-wrap: wrap;
    row-gap: 4px;
}

.sa-site-bar__title-block {
    display: flex;
    flex-direction: column;
    justify-content: center;
    min-width: 0;
    gap: 0;
}

.sa-site-bar__subtitle {
    margin: 2px 0 0;
    padding: 0;
    font-size: 12px;
    font-weight: 500;
    line-height: 1.35;
    color: var(--text-secondary, #64748b);
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
    max-width: min(560px, 52vw);
}

.sa-site-bar--has-subtitle .sa-site-bar__inner {
    min-height: var(--sa-topbar-height);
    padding-top: 6px;
    padding-bottom: 6px;
}

.sa-site-bar__center {
    display: flex;
    align-items: center;
    justify-content: center;
    min-width: 0;
    max-width: 100%;
    flex: 0 1 auto;
}

.sa-site-bar__inner--triptych {
    display: grid;
    grid-template-columns: minmax(0, 1fr) auto minmax(0, 1fr);
    align-items: center;
    column-gap: 12px;
}

.sa-site-bar__inner--triptych .sa-site-bar__start {
    flex: unset;
    justify-self: start;
}

.sa-site-bar__inner--triptych .sa-site-bar__end {
    margin-left: 0;
    justify-self: end;
}

.sa-site-bar__actions {
    display: flex;
    align-items: center;
    gap: 8px;
    flex-shrink: 0;
}

.sa-site-bar__end {
    margin-left: auto;
    flex-shrink: 0;
}

/* 顶栏内联导航（交易灵感等） */
.sa-topbar-nav {
    display: inline-flex;
    align-items: center;
    gap: 4px;
    margin-left: 4px;
    flex-shrink: 0;
}

.sa-topbar-nav-link,
.tmr-nav-link,
.kb-nav-link,
.tt-nav-link {
    display: inline-flex;
    align-items: center;
    height: var(--sa-toolbar-btn-h);
    padding: 0 12px;
    border-radius: var(--sa-toolbar-btn-radius);
    font-size: var(--sa-topbar-meta-size);
    font-weight: 600;
    color: var(--primary-color, #1a56a7);
    text-decoration: none;
    white-space: nowrap;
    transition: var(--transition-fast, 0.15s ease);
}

.sa-topbar-nav-link:hover,
.tmr-nav-link:hover,
.kb-nav-link:hover,
.tt-nav-link:hover {
    background: var(--primary-light, #e8f0f8);
    text-decoration: none;
    color: var(--primary-active, #144785);
}

/* 多行 sticky：自选股顶栏 + 提醒条 */
.sa-site-bar-stack {
    position: sticky;
    top: 0;
    z-index: 120;
    width: 100%;
    background: var(--sa-topbar-bg);
    border-bottom: 1px solid var(--sa-topbar-border);
    box-shadow: var(--sa-topbar-shadow);
    box-sizing: border-box;
}

.sa-site-bar-stack > .sa-site-bar,
.sa-site-bar-stack > .global-search-container {
    position: static;
    box-shadow: none;
    border-bottom: none;
}

/* 页内工具条紧贴 Tier-1 下方时可 sticky */
.sa-page-toolbar--sticky {
    position: sticky;
    top: var(--sa-topbar-height);
    z-index: 110;
    margin-top: 0;
    margin-bottom: 16px;
    border-radius: 0;
    border-left: none;
    border-right: none;
    border-top: none;
}

.sa-page-toolbar--sticky.sa-site-bar--has-subtitle-offset {
    top: calc(var(--sa-topbar-height) + 8px);
}

/* 全宽页内工具条（与 container 同宽对齐） */
.sa-page-toolbar--fullbleed {
    margin-left: calc(-1 * var(--sa-topbar-px));
    margin-right: calc(-1 * var(--sa-topbar-px));
    width: calc(100% + 2 * var(--sa-topbar-px));
    border-radius: 0;
    border-left: none;
    border-right: none;
}

.sa-site-bar__brand,
.index-header-brand,
.plate-toolbar-brand,
.calendar-toolbar-brand,
.back-link,
.sa-site-bar__brand:visited {
    display: inline-flex;
    align-items: center;
    gap: 8px;
    text-decoration: none;
    color: var(--text-primary, #262626);
    font-size: var(--sa-topbar-brand-size);
    font-weight: 600;
    font-family: var(--font-family-base, inherit);
    white-space: nowrap;
    flex-shrink: 0;
}

.sa-site-bar__brand:hover,
.index-header-brand:hover,
.plate-toolbar-brand:hover,
.back-link:hover {
    color: var(--primary-color, #1a56a7);
}

.sa-site-bar__logo,
.index-header-logo,
.plate-toolbar-logo,
.calendar-toolbar-logo,
.back-link-logo {
    display: block;
    width: 28px;
    height: 28px;
    object-fit: contain;
    flex-shrink: 0;
}

.index-header-logo {
    margin-left: 2px;
}

.sa-site-bar__title,
.plate-toolbar-title,
.calendar-toolbar-title,
.global-search-title {
    margin: 0;
    font-size: var(--sa-topbar-title-size);
    font-weight: 700;
    line-height: 1.25;
    color: var(--text-primary, #111827);
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
    min-width: 0;
}

.sa-site-bar__meta,
.index-header-status {
    display: flex;
    align-items: center;
    gap: 10px;
    font-size: var(--sa-topbar-meta-size);
    color: var(--text-secondary, #595959);
    font-family: var(--font-family-base, inherit);
    white-space: nowrap;
    flex-shrink: 0;
}

.index-header-date,
.sa-site-bar__meta-date {
    font-variant-numeric: tabular-nums;
}

.index-header-market,
.sa-site-bar__meta-market {
    font-weight: 500;
}

/* 自选股 / 周复盘等：顶栏容器与站点栏同高 */
.user-shares-sticky-top {
    position: sticky;
    top: 0;
    z-index: 120;
    background: var(--sa-topbar-bg, #f7f8fa);
    border-bottom: 1px solid var(--sa-topbar-border);
    box-shadow: var(--sa-topbar-shadow);
    width: 100%;
    box-sizing: border-box;
}

.user-shares-sticky-top .global-search-container {
    position: static;
    min-height: var(--sa-topbar-height);
    padding: 0 var(--sa-topbar-px);
    margin-bottom: 0;
    border-bottom: none;
    box-shadow: none;
    display: flex;
    align-items: center;
    background: transparent;
}

.user-shares-sticky-top .global-search-wrapper,
.user-shares-sticky-top .global-search-wrapper.user-shares-topbar-row {
    min-height: var(--sa-topbar-height);
    align-items: center;
}

.global-search-container:not(.user-shares-sticky-top .global-search-container) {
    position: sticky;
    top: 0;
    z-index: 100;
    min-height: var(--sa-topbar-height);
    padding: 0 var(--sa-topbar-px);
    margin-bottom: 20px;
    border-bottom: 1px solid var(--sa-topbar-border);
    box-shadow: var(--sa-topbar-shadow);
    background: var(--sa-topbar-bg);
    display: flex;
    align-items: center;
    box-sizing: border-box;
}

.global-search-wrapper {
    min-height: var(--sa-topbar-height);
    align-items: center;
}

/* ── Tier 2：页内工具条（筛选 / 搜索） ── */
.sa-page-toolbar,
.plate-toolbar:not(.plate-toolbar--legacy),
.calendar-toolbar {
    display: flex;
    align-items: center;
    justify-content: flex-start;
    flex-wrap: wrap;
    gap: 10px;
    background: var(--sa-page-toolbar-bg);
    padding: 8px var(--sa-topbar-px);
    margin: 0 0 16px;
    border-radius: var(--border-radius-sm, 4px);
    border: 1px solid var(--sa-topbar-border);
    min-height: var(--sa-page-toolbar-min-h);
    box-sizing: border-box;
}

.sa-page-toolbar__inner,
.plate-toolbar-right,
.calendar-toolbar-right {
    display: flex;
    align-items: center;
    gap: 10px;
    flex-wrap: wrap;
    flex: 1;
    min-width: 0;
}

.sa-page-toolbar .toolbar-item,
.plate-toolbar .toolbar-item,
.calendar-toolbar .toolbar-item {
    display: inline-flex;
    align-items: center;
    gap: 6px;
}

.sa-page-toolbar .toolbar-item label,
.plate-toolbar .toolbar-item label,
.calendar-toolbar .toolbar-item label {
    margin: 0;
    font-size: 11px;
    color: var(--text-tertiary, #8c8c8c);
    white-space: nowrap;
}

.sa-page-toolbar .form-control,
.plate-toolbar .toolbar-item .form-control,
.plate-toolbar .toolbar-item select.form-control,
.calendar-toolbar .form-control {
    height: var(--sa-toolbar-control-h);
    padding: 4px 10px;
    font-size: 12px;
    border: 1px solid var(--border-color, #d1d5db);
    border-radius: var(--sa-toolbar-btn-radius);
    min-width: 0;
}

.sa-toolbar-btn,
.plate-toolbar .toolbar-btn,
.calendar-toolbar .toolbar-btn {
    display: inline-flex;
    align-items: center;
    justify-content: center;
    height: var(--sa-toolbar-btn-h);
    min-width: var(--sa-toolbar-btn-h);
    padding: 0 12px;
    border: 1px solid var(--border-color, #d1d5db);
    border-radius: var(--sa-toolbar-btn-radius);
    background: var(--card-bg, #fff);
    color: var(--text-primary, #374151);
    font-size: 12px;
    cursor: pointer;
    transition: var(--transition-fast, 0.15s ease);
}

.sa-toolbar-btn.primary,
.plate-toolbar .toolbar-btn.primary,
.calendar-toolbar .toolbar-btn.primary {
    background: var(--primary-color, #1a56a7);
    border-color: var(--primary-color, #1a56a7);
    color: #fff;
}

.sa-toolbar-btn:hover,
.plate-toolbar .toolbar-btn:hover {
    border-color: var(--primary-color, #1a56a7);
    color: var(--primary-color, #1a56a7);
}

.sa-toolbar-btn.primary:hover,
.plate-toolbar .toolbar-btn.primary:hover {
    background: var(--primary-active, #144785);
    border-color: var(--primary-active, #144785);
    color: #fff;
}

.top-bar-actions {
    display: flex;
    align-items: center;
    gap: 8px;
    flex-shrink: 0;
}

.top-bar-actions .sa-toolbar-btn,
.top-bar-actions button {
    height: var(--sa-toolbar-btn-h);
}

.calendar-toolbar.sa-page-toolbar {
    justify-content: space-between;
}

.calendar-toolbar-summary {
    font-size: var(--sa-topbar-meta-size);
    color: var(--text-secondary, #6b7280);
    font-variant-numeric: tabular-nums;
    white-space: nowrap;
    flex-shrink: 0;
}

.calendar-toolbar-summary .heavy { color: #b91c1c; }
.calendar-toolbar-summary .new-events-summary { color: #dc2626; font-weight: 600; }

/* ── 移动端 ── */
@media (max-width: 768px) {
    :root {
        --sa-topbar-px: var(--sa-topbar-px-mobile);
        --sa-topbar-title-size: 15px;
        --sa-page-toolbar-min-h: 44px;
    }

    .sa-site-bar__inner--triptych {
        grid-template-columns: minmax(0, 1fr) auto;
        row-gap: 6px;
    }

    .sa-site-bar__inner--triptych .sa-site-bar__center {
        grid-column: 1 / -1;
        order: 3;
        justify-self: stretch;
        justify-content: center;
    }

    .sa-site-bar__subtitle {
        max-width: 72vw;
        white-space: normal;
        display: -webkit-box;
        -webkit-line-clamp: 2;
        -webkit-box-orient: vertical;
        overflow: hidden;
    }

    .sa-page-toolbar--fullbleed {
        margin-left: calc(-1 * var(--sa-topbar-px-mobile));
        margin-right: calc(-1 * var(--sa-topbar-px-mobile));
        width: calc(100% + 2 * var(--sa-topbar-px-mobile));
    }

    .sa-topbar-nav {
        display: none;
    }

    .sa-topbar-nav--mobile-visible {
        display: inline-flex;
    }

    .sa-site-bar__inner,
    .index-header-inner {
        padding: 0 var(--sa-topbar-px-mobile);
        gap: 8px;
    }

    .sa-site-bar__title,
    .plate-toolbar-title,
    .global-search-title {
        max-width: 42vw;
    }

    .index-header-status,
    .sa-site-bar__meta {
        font-size: 12px;
        gap: 6px;
    }

    .user-shares-sticky-top .global-search-container,
    .global-search-container {
        padding: 0 var(--sa-topbar-px-mobile);
    }

    .sa-page-toolbar,
    .plate-toolbar,
    .calendar-toolbar {
        padding: 8px var(--sa-topbar-px-mobile);
        gap: 8px;
    }

    /* 投资日历 Tier2：摘要一行 + 控件网格，避免顶栏统一后移动端错位 */
    .calendar-toolbar.sa-page-toolbar {
        flex-direction: column;
        align-items: stretch;
        gap: 8px;
    }

    .calendar-toolbar-summary {
        white-space: normal;
        line-height: 1.4;
        width: 100%;
        flex-shrink: 1;
    }

    .calendar-toolbar-right {
        width: 100%;
        flex: none;
        display: grid;
        grid-template-columns: auto 1fr auto;
        gap: 8px;
        align-items: center;
    }

    .calendar-toolbar-right .toolbar-search {
        grid-column: 1 / -1;
        width: 100%;
        max-width: none;
        box-sizing: border-box;
    }

    .calendar-toolbar-right .toolbar-month-jump {
        grid-column: 1 / -1;
        width: 100%;
        display: flex;
        flex-direction: row;
        flex-wrap: nowrap;
        align-items: center;
        gap: 6px;
        box-sizing: border-box;
    }

    .calendar-toolbar-right .toolbar-month-jump select {
        flex: 1 1 0;
        min-width: 0;
        width: auto;
        height: 36px;
        font-size: 14px;
    }

    .calendar-toolbar-right .toolbar-month-jump button {
        flex-shrink: 0;
        height: 36px;
        font-size: 13px;
        padding: 0 10px;
        white-space: nowrap;
    }

    .calendar-toolbar-right .toolbar-switch-wrap {
        grid-column: 1;
        flex-shrink: 0;
    }

    .calendar-toolbar-right .toolbar-view-group {
        grid-column: 2;
        justify-self: center;
        flex-shrink: 0;
    }

    .calendar-toolbar-right .toolbar-filter-btn {
        grid-column: 3;
        justify-self: end;
        flex-shrink: 0;
    }

    .plate-toolbar .date-range-wrap .date-range-inputs .form-control {
        flex: 1 1 0;
        width: auto;
        min-width: 0;
    }

    .user-shares-sticky-top .global-search-wrapper.user-shares-topbar-row {
        grid-template-columns: minmax(0, 1fr) auto;
        row-gap: 6px;
    }

    /* 自选股三列顶栏在 topbar-layout.css 中改为 flex，避免 grid 挤没标题 */
    .user-shares-topbar-stack .sa-site-bar__title,
    .user-shares-topbar-stack .sa-site-bar__title.sa-site-bar__title--has-short::after {
        max-width: 100%;
    }

    .user-shares-topbar-center {
        grid-column: 1 / -1;
        order: 3;
    }

    .sa-site-bar__actions .privacy-mode-label,
    .sa-site-bar__actions .share-export-btn-label,
    .sa-site-bar__actions .share-btn-label,
    .sa-site-bar__actions .switch-view-mode-label {
        display: none;
    }
}

@media (max-width: 480px) {
    .index-header-status,
    .sa-site-bar__meta {
        font-size: 11px;
        gap: 4px;
    }

    .sa-site-bar__brand-text {
        display: none;
    }
}

/* 容器与顶栏水平对齐（同一 max-width + gutter） */
.sa-page-shell,
body > .container,
.container {
    width: 100%;
    max-width: var(--sa-topbar-max-width);
    margin-inline: auto;
    padding-inline: var(--sa-topbar-px);
    box-sizing: border-box;
}

@supports (padding: max(0px)) {
    .sa-site-bar,
    .index-header,
    .user-shares-sticky-top,
    .sa-site-bar-stack {
        padding-left: max(0px, env(safe-area-inset-left));
        padding-right: max(0px, env(safe-area-inset-right));
    }
}
