:root {
    --sidebar-width: 240px;
    --topnav-height: 56px;
    --topbar-height: 0px;
    --workspace-tabs-height: 40px;
    --surface: #f4f6f9;
    --surface-2: #eef1f6;
    --border-light: #e2e8f0;
    --text: #1e293b;
    --text-muted: #64748b;
    --accent: #3b82f6;
    --accent-soft: #eff6ff;
    --nav-bg: #ffffff;
    --transition: 0.2s cubic-bezier(0.4, 0, 0.2, 1);
}

* { box-sizing: border-box; }

body {
    margin: 0;
    background: var(--surface);
    font-family: "Noto Sans SC", "PingFang SC", "Microsoft YaHei", "DM Sans", system-ui, sans-serif;
    color: var(--text);
    -webkit-font-smoothing: antialiased;
}

.app-shell {
    display: flex;
    flex-direction: column;
    min-height: 100vh;
}

/* ===== Top navigation (light) ===== */
.app-sidebar.app-topnav {
    width: 100%;
    height: var(--topnav-height);
    flex-shrink: 0;
    background: var(--nav-bg);
    color: var(--text);
    display: flex;
    flex-direction: row;
    align-items: center;
    gap: 8px;
    padding: 0 14px 0 12px;
    border-bottom: 1px solid var(--border-light);
    position: sticky;
    top: 0;
    z-index: 200;
    box-shadow: 0 1px 0 rgba(15, 23, 42, 0.02);
    overflow: visible;
}

.sidebar-brand {
    display: flex;
    align-items: center;
    gap: 10px;
    flex-shrink: 0;
    padding: 0 8px 0 0;
    border-bottom: none;
    border-right: 1px solid var(--border-light);
    margin-right: 6px;
    padding-right: 14px;
    min-width: 0;
}

.sidebar-brand-link {
    display: flex;
    align-items: center;
    gap: 10px;
    text-decoration: none;
    color: inherit;
    min-width: 0;
}

.sidebar-brand-icon {
    width: 34px;
    height: 34px;
    border-radius: 9px;
    display: flex;
    align-items: center;
    justify-content: center;
    flex-shrink: 0;
    overflow: hidden;
    background: var(--surface-2);
    border: 1px solid var(--border-light);
}

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

.sidebar-brand h1 {
    margin: 0;
    font-size: 0.92rem;
    font-weight: 700;
    color: var(--text);
    white-space: nowrap;
}

.sidebar-brand p {
    margin: 1px 0 0;
    font-size: 0.68rem;
    color: var(--text-muted);
    white-space: nowrap;
}

.sidebar-nav {
    display: flex;
    flex-direction: row;
    align-items: center;
    gap: 2px;
    flex: 1;
    min-width: 0;
    /* 切勿 overflow-x:auto：会连带把 overflow-y 变成 auto，裁切下拉并露出怪异滚动条 */
    overflow: visible;
    padding: 0;
}

.sidebar-nav-spacer {
    flex: 1 1 auto;
    min-width: 12px;
    height: 1px;
}

.sidebar-nav-end {
    display: flex;
    align-items: center;
    flex-shrink: 0;
    margin-left: 4px;
    padding-left: 10px;
    border-left: 1px solid var(--border-light);
}

.sidebar-section-label {
    display: none;
}

/* 顶层：纯文字 + 箭头（对齐参考图） */
.sidebar-nav > .sidebar-link > i,
.sidebar-nav > .sidebar-group > .sidebar-group-toggle > i:first-child,
.sidebar-nav-end > .sidebar-link > i,
.sidebar-nav-end > .sidebar-group > .sidebar-group-toggle > i:first-child {
    display: none;
}

.sidebar-link,
.sidebar-group-toggle {
    display: flex;
    align-items: center;
    gap: 7px;
    padding: 8px 12px;
    margin: 0;
    border-radius: 8px;
    color: var(--text-muted);
    text-decoration: none;
    font-size: 0.86rem;
    font-weight: 500;
    transition: background var(--transition), color var(--transition);
    border: none;
    border-left: none;
    white-space: nowrap;
    background: transparent;
    cursor: pointer;
}

.sidebar-link i,
.sidebar-group-toggle i:first-child {
    font-size: 0.95rem;
    width: 18px;
    text-align: center;
    flex-shrink: 0;
}

/* 红利现金牛：Bootstrap Icons 无奶牛，用 emoji 自定义 */
.bi-cow::before {
    content: "🐄";
    font-family: "Segoe UI Emoji", "Apple Color Emoji", "Noto Color Emoji", sans-serif !important;
    font-style: normal;
    font-weight: 400;
    line-height: 1;
}

.workspace-tab-main .bi-cow,
.sidebar-link .bi-cow {
    font-size: 0.95rem;
}

.sidebar-link:hover:not(.disabled),
.sidebar-group-toggle:hover {
    background: var(--surface-2);
    color: var(--text);
}

.sidebar-link.active,
.sidebar-group-toggle.active {
    background: var(--accent-soft);
    color: #2563eb;
}

.sidebar-link.disabled {
    opacity: 0.4;
    cursor: not-allowed;
    pointer-events: none;
}

.sidebar-group {
    position: relative;
    margin: 0;
    flex-shrink: 0;
}

.sidebar-group-chevron {
    margin-left: 2px;
    font-size: 0.7rem;
    opacity: 0.55;
    transition: transform var(--transition);
}

.sidebar-subnav .sidebar-group-chevron {
    margin-left: auto;
}

.sidebar-group.is-open > .sidebar-group-toggle .sidebar-group-chevron {
    transform: rotate(180deg);
}

@media (max-width: 1200px) {
    .sidebar-brand p {
        display: none;
    }

    .sidebar-nav > .sidebar-link,
    .sidebar-nav > .sidebar-group > .sidebar-group-toggle {
        padding: 7px 9px;
        font-size: 0.82rem;
    }
}

/* 顶层下拉：浮在页面内容上方，带淡出/滑入 */
.sidebar-nav > .sidebar-group > .sidebar-subnav {
    display: block;
    position: absolute;
    top: calc(100% + 6px);
    left: 0;
    min-width: 240px;
    max-width: min(360px, 92vw);
    max-height: min(70vh, 520px);
    overflow-x: hidden;
    overflow-y: auto;
    padding: 8px;
    background: #fff;
    border: 1px solid var(--border-light);
    border-radius: 12px;
    box-shadow: 0 12px 32px rgba(15, 23, 42, 0.14);
    z-index: 400;
    opacity: 0;
    visibility: hidden;
    pointer-events: none;
    transform: translateY(-6px);
    transition: opacity 0.16s ease, transform 0.16s ease, visibility 0.16s;
}

.sidebar-nav > .sidebar-group.is-open > .sidebar-subnav {
    opacity: 1;
    visibility: visible;
    pointer-events: auto;
    transform: translateY(0);
}

/* 桥接 toggle 与下拉之间的缝隙，避免悬停时闪关 */
.sidebar-nav > .sidebar-group > .sidebar-subnav::before {
    content: "";
    position: absolute;
    left: 0;
    right: 0;
    top: -10px;
    height: 10px;
}

.sidebar-nav > .sidebar-group:nth-last-child(-n+3) > .sidebar-subnav {
    left: auto;
    right: 0;
}

/* 下拉内：纵向手风琴 */
.sidebar-subnav .sidebar-group {
    width: 100%;
}

.sidebar-subnav .sidebar-link,
.sidebar-subnav .sidebar-group-toggle {
    width: 100%;
    padding: 10px 12px;
    font-size: 0.86rem;
    white-space: normal;
    border-radius: 8px;
}

.sidebar-subnav .sidebar-link > i,
.sidebar-subnav .sidebar-group-toggle > i:first-child {
    display: inline-block;
    color: var(--accent);
    opacity: 0.9;
}

.sidebar-subnav .sidebar-subnav {
    display: none;
    padding: 2px 0 4px 8px;
    position: static;
    box-shadow: none;
    border: none;
    background: transparent;
    max-height: none;
    overflow: visible;
}

.sidebar-subnav .sidebar-group.is-open > .sidebar-subnav {
    display: block;
}

.sidebar-sublink {
    padding-left: 14px;
}

.sidebar-group.depth-1 > .sidebar-group-toggle {
    font-size: 0.82rem;
}

.sidebar-group.depth-2 > .sidebar-group-toggle {
    font-size: 0.78rem;
    padding-left: 16px;
}

.sidebar-group.depth-2 .sidebar-sublink {
    padding-left: 20px;
    font-size: 0.76rem;
}

.sidebar-empty-hint {
    padding: 8px 12px;
    font-size: 0.75rem;
    color: var(--text-muted);
}

.sidebar-footer {
    display: none;
}

/* ===== Main area ===== */
.app-body {
    flex: 1;
    min-width: 0;
    min-height: 0;
    display: flex;
    flex-direction: column;
    height: auto;
}

.app-topbar {
    height: var(--topbar-height);
    flex-shrink: 0;
    background: #fff;
    border-bottom: 1px solid var(--border-light);
    display: flex;
    align-items: center;
    justify-content: space-between;
    gap: 10px;
    padding: 0 28px;
    position: relative;
    z-index: 100;
}

.topbar-left {
    display: flex;
    align-items: center;
    gap: 10px;
    min-width: 0;
    flex: 1;
}

.sidebar-toggle,
.sidebar-close {
    display: none;
    align-items: center;
    justify-content: center;
    width: 36px;
    height: 36px;
    border: 1px solid var(--border-light);
    border-radius: 10px;
    background: #fff;
    color: var(--text);
    cursor: pointer;
    flex-shrink: 0;
    padding: 0;
}

.sidebar-toggle:hover,
.sidebar-close:hover {
    background: var(--surface-2);
    color: var(--accent);
}

.sidebar-close {
    margin-left: auto;
}

.sidebar-backdrop {
    display: none;
}

.breadcrumb {
    margin: 0;
    font-size: 0.82rem;
    color: var(--text-muted);
    display: flex;
    align-items: center;
    gap: 6px;
}

.breadcrumb .sep { opacity: 0.4; }
.breadcrumb .current { color: var(--text); font-weight: 600; }

.topbar-meta {
    display: flex;
    align-items: center;
    gap: 10px;
}

.meta-chip {
    background: var(--surface-2);
    border: 1px solid var(--border-light);
    color: var(--text-muted);
    font-size: 0.78rem;
    padding: 5px 12px;
    border-radius: 20px;
    display: flex;
    align-items: center;
    gap: 6px;
}

a.meta-chip-link {
    text-decoration: none;
    transition: background 0.15s, border-color 0.15s, color 0.15s;
}

a.meta-chip-link:hover {
    background: #eff6ff;
    border-color: #93c5fd;
    color: #2563eb;
}

.page-content {
    flex: 1;
    min-height: 0;
    padding: 24px 28px 40px;
    overflow: auto;
}

/* ===== Workspace tabs ===== */
body.is-workspace-shell {
    overflow: hidden;
}

body.is-workspace-shell .app-shell {
    height: 100vh;
    max-height: 100vh;
    overflow: hidden;
}

body.is-workspace-shell .app-body {
    flex: 1;
    min-height: 0;
    height: auto;
}

.workspace-tabs {
    flex-shrink: 0;
    height: var(--workspace-tabs-height);
    background: #eef2f7;
    border-bottom: 1px solid var(--border-light);
    display: flex;
    align-items: stretch;
    gap: 6px;
    padding: 0 10px;
}

.workspace-tabs .sidebar-toggle {
    align-self: center;
    width: 32px;
    height: 32px;
    border-radius: 8px;
}

.workspace-tab-list {
    display: flex;
    align-items: flex-end;
    gap: 4px;
    overflow-x: auto;
    overflow-y: hidden;
    width: 100%;
    scrollbar-width: thin;
}

.workspace-tab {
    display: flex;
    align-items: center;
    max-width: 200px;
    min-width: 96px;
    height: 32px;
    background: #e2e8f0;
    border: 1px solid var(--border-light);
    border-bottom: none;
    border-radius: 8px 8px 0 0;
    color: var(--text-muted);
    flex-shrink: 0;
    user-select: none;
    -webkit-user-select: none;
}

.workspace-tab.is-active {
    background: #fff;
    color: var(--text);
    position: relative;
    z-index: 1;
}

.workspace-tab-main {
    flex: 1;
    min-width: 0;
    display: flex;
    align-items: center;
    gap: 6px;
    border: 0;
    background: transparent;
    color: inherit;
    padding: 0 4px 0 10px;
    height: 100%;
    cursor: pointer;
    font-size: 0.8rem;
    font-weight: 500;
}

.workspace-tab-main i {
    font-size: 0.85rem;
    opacity: 0.85;
    flex-shrink: 0;
}

.workspace-tab-label {
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
}

.workspace-tab-close {
    flex-shrink: 0;
    width: 22px;
    height: 22px;
    margin-right: 4px;
    border: 0;
    border-radius: 6px;
    background: transparent;
    color: inherit;
    display: inline-flex;
    align-items: center;
    justify-content: center;
    opacity: 0.55;
    cursor: pointer;
}

.workspace-tab-close:hover {
    opacity: 1;
    background: rgba(15, 23, 42, 0.08);
}

.workspace-tab.is-active .workspace-tab-close:hover {
    background: rgba(239, 68, 68, 0.12);
    color: #ef4444;
}

.workspace-tab.is-pinned .workspace-tab-main {
    padding-right: 12px;
}

.workspace-tab:not(.is-pinned) {
    cursor: grab;
}

.workspace-tab-list.is-reordering .workspace-tab {
    will-change: transform;
}

.workspace-tab.is-dragging {
    opacity: 0.55;
    cursor: grabbing;
    z-index: 2;
    box-shadow: 0 6px 16px rgba(15, 23, 42, 0.18);
    background: #fff;
}

.workspace-tab-ctx {
    position: fixed;
    z-index: 4000;
    min-width: 148px;
    padding: 4px;
    background: #fff;
    border: 1px solid #e2e8f0;
    border-radius: 8px;
    box-shadow: 0 8px 24px rgba(15, 23, 42, 0.14);
    display: flex;
    flex-direction: column;
    gap: 2px;
}

.workspace-tab-ctx[hidden] {
    display: none !important;
}

.workspace-tab-ctx-item {
    border: 0;
    background: transparent;
    text-align: left;
    padding: 8px 12px;
    border-radius: 6px;
    font-size: 0.82rem;
    color: #0f172a;
    cursor: pointer;
}

.workspace-tab-ctx-item:hover:not(:disabled) {
    background: #f1f5f9;
}

.workspace-tab-ctx-item:disabled {
    color: #94a3b8;
    cursor: not-allowed;
}

.workspace-panes {
    flex: 1;
    min-height: 0;
    position: relative;
    background: var(--surface);
}

.workspace-pane {
    position: absolute;
    inset: 0;
}

.workspace-pane.is-active {
    z-index: 1;
}

.workspace-frame {
    width: 100%;
    height: 100%;
    border: 0;
    display: block;
    background: var(--surface);
}

body.is-workspace-embed {
    background: var(--surface);
    min-height: 100vh;
}

body.is-workspace-embed .page-content {
    min-height: 100vh;
}

body.is-workspace-embed .page-content.page-research,
body.is-workspace-embed .page-content.page-sa {
    height: 100vh;
    max-height: 100vh;
    overflow: hidden;
}

/* 机构调研 / SemiAnalysis：锁定视口高度，左右栏各自滚动 */
body:has(.page-research),
body:has(.page-sa) {
    overflow: hidden;
}

body:has(.page-research) .app-shell,
body:has(.page-sa) .app-shell {
    height: 100vh;
    min-height: 100vh;
    max-height: 100vh;
    overflow: hidden;
}

body:has(.page-research) .app-body,
body:has(.page-sa) .app-body {
    min-height: 0;
    overflow: hidden;
}

body:has(.page-research) .page-content.page-research,
body:has(.page-sa) .page-content.page-sa {
    overflow: hidden;
}

.page-header {
    margin-bottom: 22px;
}

.page-header h2 {
    margin: 0 0 6px;
    font-size: 1.35rem;
    font-weight: 700;
}

.page-header p {
    margin: 0;
    font-size: 0.88rem;
    color: var(--text-muted);
}

/* Placeholder page */
.placeholder-page {
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    min-height: 360px;
    color: var(--text-muted);
    text-align: center;
}

.placeholder-page i {
    font-size: 3rem;
    opacity: 0.25;
    margin-bottom: 16px;
}

/* ===== Mobile: drawer menu (light) ===== */
@media (max-width: 900px) {
    :root {
        --topnav-height: 0px;
        --topbar-height: 0px;
        --workspace-tabs-height: 40px;
    }

    .sidebar-toggle,
    .sidebar-close {
        display: inline-flex;
    }

    .app-sidebar.app-topnav {
        position: fixed;
        left: 0;
        top: 0;
        bottom: 0;
        width: min(86vw, 300px);
        height: 100vh;
        height: 100dvh;
        flex-direction: column;
        align-items: stretch;
        gap: 0;
        padding: 0;
        padding-top: env(safe-area-inset-top);
        padding-bottom: env(safe-area-inset-bottom);
        transform: translateX(-105%);
        transition: transform 0.25s cubic-bezier(0.4, 0, 0.2, 1);
        box-shadow: none;
        border-bottom: none;
        border-right: 1px solid var(--border-light);
        overflow: hidden;
    }

    body.sidebar-open .app-sidebar.app-topnav {
        transform: translateX(0);
        box-shadow: 12px 0 40px rgba(15, 23, 42, 0.18);
    }

    .sidebar-brand {
        padding: 14px 12px;
        border-right: none;
        border-bottom: 1px solid var(--border-light);
        margin-right: 0;
        width: 100%;
    }

    .sidebar-brand-text {
        min-width: 0;
    }

    .sidebar-nav {
        flex-direction: column;
        align-items: stretch;
        overflow-x: hidden;
        overflow-y: auto;
        padding: 12px 10px 20px;
        flex: 1;
    }

    .sidebar-nav-spacer {
        display: none;
    }

    .sidebar-nav-end {
        margin-left: 0;
        padding-left: 0;
        border-left: none;
        flex-direction: column;
        align-items: stretch;
        width: 100%;
        margin-top: 8px;
        padding-top: 8px;
        border-top: 1px solid var(--border-light);
    }

    .sidebar-nav > .sidebar-link > i,
    .sidebar-nav > .sidebar-group > .sidebar-group-toggle > i:first-child,
    .sidebar-nav-end > .sidebar-link > i,
    .sidebar-nav-end > .sidebar-group > .sidebar-group-toggle > i:first-child {
        display: inline-block;
    }

    .sidebar-section-label {
        display: block;
        font-size: 0.68rem;
        text-transform: uppercase;
        letter-spacing: 0.08em;
        color: var(--text-muted);
        padding: 8px 12px 10px;
        font-weight: 600;
    }

    .sidebar-nav > .sidebar-group > .sidebar-subnav {
        position: static;
        display: none;
        left: auto;
        right: auto;
        box-shadow: none;
        border: none;
        border-radius: 0;
        padding: 2px 0 6px 8px;
        max-height: none;
        max-width: none;
        background: transparent;
        opacity: 1;
        visibility: visible;
        pointer-events: auto;
        transform: none;
        transition: none;
    }

    .sidebar-nav > .sidebar-group.is-open > .sidebar-subnav {
        display: block;
    }

    .sidebar-link,
    .sidebar-group-toggle {
        width: 100%;
        padding: 12px 14px;
        font-size: 0.92rem;
        min-height: 44px;
        white-space: normal;
    }

    .sidebar-group-chevron {
        margin-left: auto;
    }

    .sidebar-footer {
        display: block;
        padding: 12px 16px;
        border-top: 1px solid var(--border-light);
        font-size: 0.72rem;
        color: var(--text-muted);
        text-align: center;
    }

    .sidebar-backdrop {
        display: block;
        position: fixed;
        inset: 0;
        background: rgba(15, 23, 42, 0.35);
        z-index: 190;
        opacity: 0;
        pointer-events: none;
        transition: opacity 0.2s ease;
        -webkit-backdrop-filter: blur(1px);
        backdrop-filter: blur(1px);
    }

    body.sidebar-open .sidebar-backdrop {
        opacity: 1;
        pointer-events: auto;
    }

    body.sidebar-open {
        overflow: hidden;
    }

    .workspace-tabs {
        padding-left: max(8px, env(safe-area-inset-left));
        padding-right: max(10px, env(safe-area-inset-right));
        padding: 0 6px;
    }

    .workspace-tab {
        max-width: 140px;
        min-width: 72px;
        height: 30px;
    }

    .workspace-tab-main {
        font-size: 0.74rem;
        padding-left: 8px;
    }

    .page-content {
        padding: 14px 12px 28px;
        padding-left: max(12px, env(safe-area-inset-left));
        padding-right: max(12px, env(safe-area-inset-right));
        padding-bottom: max(28px, env(safe-area-inset-bottom));
    }

    .page-header {
        margin-bottom: 14px;
    }

    .page-header h2 {
        font-size: 1.15rem;
    }

    .page-header p {
        font-size: 0.8rem;
    }

    body.is-workspace-shell .app-shell {
        height: 100dvh;
        max-height: 100dvh;
    }
}

@media (max-width: 576px) {
    .meta-chip-text {
        display: none;
    }

    .workspace-tab-label {
        max-width: 64px;
    }
}
