/**
 * 全局主题样式 - 2026-07-27
 * 文件：common/css/theme.css
 * 说明：通过 <html data-theme="light|dark|auto"> 控制全局主题。
 *      - data-theme="light" 强制浅色
 *      - data-theme="dark"  强制暗黑
 *      - data-theme="auto"  跟随系统（CSS 层根据 prefers-color-scheme 自动选择）
 *      默认行为：暗黑（首次访问无 localStorage 时，直接给 html 加 data-theme="dark"）
 *      JS 脚本：common/js/theme.js 负责：
 *        1) 页面加载前从 localStorage 读取用户选择并应用（避免主题闪烁）
 *        2) 提供三选一切换器 UI（浅色 / 暗黑 / 跟随系统）
 * 适用：所有使用 PageHeader / CommonLayout 的页面，仅需 addCSS 即可。
 */

/* ========== 浅色主题变量（默认） ========== */
:root,
:root[data-theme="light"] {
    /* 背景 */
    --theme-bg:           #f4f6f9;
    --theme-bg-elevated:  #ffffff;
    --theme-bg-soft:      #f8fafc;
    --theme-bg-hover:     #f1f5f9;

    /* 文字 */
    --theme-text:         #1e293b;
    --theme-text-soft:    #475569;
    --theme-text-muted:   #94a3b8;

    /* 边框 */
    --theme-border:       #e2e8f0;
    --theme-border-soft:  #f1f5f9;

    /* 主色 */
    --theme-primary:      #2563eb;
    --theme-primary-hover:#1d4ed8;
    --theme-primary-soft: rgba(37, 99, 235, 0.10);
    --theme-primary-text: #ffffff;

    /* 状态色 */
    --theme-success:      #10b981;
    --theme-danger:       #ef4444;
    --theme-warning:      #f59e0b;
    --theme-info:         #3b82f6;

    /* 阴影 */
    --theme-shadow-sm:    0 1px 2px rgba(15, 23, 42, 0.06);
    --theme-shadow:       0 1px 3px rgba(15, 23, 42, 0.08), 0 4px 12px rgba(15, 23, 42, 0.04);
    --theme-shadow-lg:    0 4px 6px rgba(15, 23, 42, 0.08), 0 12px 24px rgba(15, 23, 42, 0.10);

    /* 表格 */
    --theme-table-stripe: #f8fafc;
    --theme-table-hover:  #f1f5f9;
    --theme-table-head:   #f1f5f9;

    /* 输入框 */
    --theme-input-bg:     #ffffff;
    --theme-input-border: #cbd5e1;
    --theme-input-text:   #1e293b;

    /* 卡片 */
    --theme-card-bg:      #ffffff;
    --theme-card-border:  #e2e8f0;

    /* 代码块 */
    --theme-code-bg:      #f1f5f9;
    --theme-code-text:    #1e293b;
}

/* ========== 暗黑主题变量 ========== */
:root[data-theme="dark"] {
    /* 背景 */
    --theme-bg:           #0b1220;
    --theme-bg-elevated:  #131c2e;
    --theme-bg-soft:      #18223a;
    --theme-bg-hover:     #1e2a44;

    /* 文字 */
    --theme-text:         #e2e8f0;
    --theme-text-soft:    #cbd5e1;
    --theme-text-muted:   #94a3b8;

    /* 边框 */
    --theme-border:       #2d3a55;
    --theme-border-soft:  #1e293b;

    /* 主色 */
    --theme-primary:      #60a5fa;
    --theme-primary-hover:#93c5fd;
    --theme-primary-soft: rgba(96, 165, 250, 0.15);
    --theme-primary-text: #0b1220;

    /* 状态色 */
    --theme-success:      #4ade80;
    --theme-danger:       #f87171;
    --theme-warning:      #fbbf24;
    --theme-info:         #38bdf8;

    /* 阴影 */
    --theme-shadow-sm:    0 1px 2px rgba(0, 0, 0, 0.30);
    --theme-shadow:       0 1px 3px rgba(0, 0, 0, 0.40), 0 4px 12px rgba(0, 0, 0, 0.30);
    --theme-shadow-lg:    0 4px 6px rgba(0, 0, 0, 0.50), 0 12px 24px rgba(0, 0, 0, 0.50);

    /* 表格 */
    --theme-table-stripe: #131c2e;
    --theme-table-hover:  #1e2a44;
    --theme-table-head:   #18223a;

    /* 输入框 */
    --theme-input-bg:     #131c2e;
    --theme-input-border: #2d3a55;
    --theme-input-text:   #e2e8f0;

    /* 卡片 */
    --theme-card-bg:      #131c2e;
    --theme-card-border:  #2d3a55;

    /* 代码块 */
    --theme-code-bg:      #18223a;
    --theme-code-text:    #e2e8f0;
}

/* ========== 跟随系统：auto 模式下，根据系统偏好应用暗色 ========== */
@media (prefers-color-scheme: dark) {
    :root[data-theme="auto"] {
        --theme-bg:           #0b1220;
        --theme-bg-elevated:  #131c2e;
        --theme-bg-soft:      #18223a;
        --theme-bg-hover:     #1e2a44;
        --theme-text:         #e2e8f0;
        --theme-text-soft:    #cbd5e1;
        --theme-text-muted:   #94a3b8;
        --theme-border:       #2d3a55;
        --theme-border-soft:  #1e293b;
        --theme-primary:      #60a5fa;
        --theme-primary-hover:#93c5fd;
        --theme-primary-soft: rgba(96, 165, 250, 0.15);
        --theme-primary-text: #0b1220;
        --theme-success:      #4ade80;
        --theme-danger:       #f87171;
        --theme-warning:      #fbbf24;
        --theme-info:         #38bdf8;
        --theme-shadow-sm:    0 1px 2px rgba(0, 0, 0, 0.30);
        --theme-shadow:       0 1px 3px rgba(0, 0, 0, 0.40), 0 4px 12px rgba(0, 0, 0, 0.30);
        --theme-shadow-lg:    0 4px 6px rgba(0, 0, 0, 0.50), 0 12px 24px rgba(0, 0, 0, 0.50);
        --theme-table-stripe: #131c2e;
        --theme-table-hover:  #1e2a44;
        --theme-table-head:   #18223a;
        --theme-input-bg:     #131c2e;
        --theme-input-border: #2d3a55;
        --theme-input-text:   #e2e8f0;
        --theme-card-bg:      #131c2e;
        --theme-card-border:  #2d3a55;
        --theme-code-bg:      #18223a;
        --theme-code-text:    #e2e8f0;
    }
}

/* ========== 基础应用：让 body/容器自动跟随主题 ========== */
body {
    background: var(--theme-bg);
    color: var(--theme-text);
    transition: background-color 0.2s ease, color 0.2s ease;
}

/* 滚动条暗色（auto/dark 时） */
:root[data-theme="dark"] ::-webkit-scrollbar,
:root[data-theme="auto"] ::-webkit-scrollbar {
    width: 10px;
    height: 10px;
}
:root[data-theme="dark"] ::-webkit-scrollbar-track,
:root[data-theme="auto"] ::-webkit-scrollbar-track {
    background: var(--theme-bg);
}
:root[data-theme="dark"] ::-webkit-scrollbar-thumb,
:root[data-theme="auto"] ::-webkit-scrollbar-thumb {
    background: var(--theme-border);
    border-radius: 5px;
}
:root[data-theme="dark"] ::-webkit-scrollbar-thumb:hover,
:root[data-theme="auto"] ::-webkit-scrollbar-thumb:hover {
    background: var(--theme-text-muted);
}

/* 通用链接颜色 */
:root[data-theme="dark"] a,
:root[data-theme="auto"] a {
    color: var(--theme-primary);
}
:root[data-theme="dark"] a:hover,
:root[data-theme="auto"] a:hover {
    color: var(--theme-primary-hover);
}

/* ========== 主题切换器按钮（三选一下拉） ========== */
.theme-switcher {
    position: relative;
    display: inline-block;
}
.theme-switcher__btn {
    display: inline-flex;
    align-items: center;
    gap: 6px;
    height: 32px;
    padding: 0 12px;
    border-radius: 10px;
    border: 1px solid var(--theme-border);
    background: var(--theme-bg-elevated);
    color: var(--theme-text);
    font-size: 14px;
    cursor: pointer;
    user-select: none;
    transition: all 0.2s;
}
.theme-switcher__btn:hover {
    background: var(--theme-bg-hover);
    border-color: var(--theme-primary);
}
.theme-switcher__icon {
    font-size: 16px;
    line-height: 1;
}
.theme-switcher__menu {
    position: absolute;
    right: 0;
    top: calc(100% + 6px);
    min-width: 160px;
    background: var(--theme-bg-elevated);
    border: 1px solid var(--theme-border);
    border-radius: 10px;
    box-shadow: var(--theme-shadow-lg);
    padding: 6px;
    z-index: 9999;
    display: none;
}
.theme-switcher.open .theme-switcher__menu {
    display: block;
}
.theme-switcher__item {
    display: flex;
    align-items: center;
    gap: 8px;
    padding: 8px 12px;
    border-radius: 6px;
    color: var(--theme-text);
    cursor: pointer;
    font-size: 14px;
    transition: background 0.15s;
}
.theme-switcher__item:hover {
    background: var(--theme-bg-hover);
}
.theme-switcher__item.active {
    background: var(--theme-primary-soft);
    color: var(--theme-primary);
    font-weight: 600;
}
.theme-switcher__item .check {
    margin-left: auto;
    opacity: 0;
}
.theme-switcher__item.active .check {
    opacity: 1;
}

/* ========== 通用卡片组件（dark 模式自动适配） ========== */
.theme-card {
    background: var(--theme-card-bg);
    border: 1px solid var(--theme-card-border);
    border-radius: 12px;
    box-shadow: var(--theme-shadow);
    padding: 20px;
    transition: box-shadow 0.2s, transform 0.2s;
}
.theme-card:hover {
    box-shadow: var(--theme-shadow-lg);
}

/* ========== 通用表格组件（dark 模式自动适配） ========== */
.theme-table {
    width: 100%;
    border-collapse: separate;
    border-spacing: 0;
    background: var(--theme-card-bg);
    border: 1px solid var(--theme-border);
    border-radius: 10px;
    overflow: hidden;
}
.theme-table thead th {
    background: var(--theme-table-head);
    color: var(--theme-text-soft);
    font-weight: 600;
    font-size: 13px;
    text-align: left;
    padding: 12px 16px;
    border-bottom: 1px solid var(--theme-border);
    text-transform: uppercase;
    letter-spacing: 0.4px;
}
.theme-table tbody td {
    padding: 12px 16px;
    border-bottom: 1px solid var(--theme-border-soft);
    color: var(--theme-text);
    font-size: 14px;
}
.theme-table tbody tr:nth-child(even) td {
    background: var(--theme-table-stripe);
}
.theme-table tbody tr:hover td {
    background: var(--theme-table-hover);
}
.theme-table tbody tr:last-child td {
    border-bottom: none;
}

/* ========== 通用按钮（dark 模式自动适配） ========== */
.theme-btn {
    display: inline-flex;
    align-items: center;
    gap: 6px;
    height: 36px;
    padding: 0 16px;
    border-radius: 8px;
    border: 1px solid var(--theme-border);
    background: var(--theme-bg-elevated);
    color: var(--theme-text);
    font-size: 14px;
    font-weight: 500;
    cursor: pointer;
    transition: all 0.2s;
    text-decoration: none;
}
.theme-btn:hover {
    background: var(--theme-bg-hover);
    border-color: var(--theme-primary);
    color: var(--theme-primary);
}
.theme-btn--primary {
    background: var(--theme-primary);
    color: var(--theme-primary-text);
    border-color: var(--theme-primary);
}
.theme-btn--primary:hover {
    background: var(--theme-primary-hover);
    color: var(--theme-primary-text);
    border-color: var(--theme-primary-hover);
}
.theme-btn--danger {
    background: var(--theme-danger);
    color: #ffffff;
    border-color: var(--theme-danger);
}
.theme-btn--sm {
    height: 28px;
    padding: 0 10px;
    font-size: 12px;
    border-radius: 6px;
}

/* ========== 通用输入框（dark 模式自动适配） ========== */
.theme-input,
.theme-select {
    height: 36px;
    padding: 0 12px;
    border-radius: 8px;
    border: 1px solid var(--theme-input-border);
    background: var(--theme-input-bg);
    color: var(--theme-input-text);
    font-size: 14px;
    outline: none;
    transition: border-color 0.2s, box-shadow 0.2s;
}
.theme-input:focus,
.theme-select:focus {
    border-color: var(--theme-primary);
    box-shadow: 0 0 0 3px var(--theme-primary-soft);
}

/* ========== 通用徽章（dark 模式自动适配） ========== */
.theme-badge {
    display: inline-block;
    padding: 2px 8px;
    border-radius: 999px;
    font-size: 12px;
    font-weight: 500;
    background: var(--theme-primary-soft);
    color: var(--theme-primary);
}
.theme-badge--success {
    background: rgba(16, 185, 129, 0.15);
    color: var(--theme-success);
}
.theme-badge--danger {
    background: rgba(239, 68, 68, 0.15);
    color: var(--theme-danger);
}
.theme-badge--warning {
    background: rgba(245, 158, 11, 0.15);
    color: var(--theme-warning);
}
