:root {
    --site-bg: #f8fafc;
    --site-panel: #ffffff;
    --site-text: #0f172a;
    --site-muted: #475569;
    --site-border: #e2e8f0;
    --site-primary: #2563eb;
    --site-primary-text: #ffffff;
    --site-action-hover: #1d4ed8;
    --site-action-secondary: #e2e8f0;
    --site-action-secondary-hover: #cbd5e1;
}

html[data-theme="dark"] {
    color-scheme: dark;
    --site-bg: #0f172a;
    --site-panel: #111827;
    --site-text: #f8fafc;
    --site-muted: #cbd5e1;
    --site-border: #334155;
    --site-primary: #38bdf8;
    --site-primary-text: #06263a;
    --site-action-hover: #7dd3fc;
    --site-action-secondary: #334155;
    --site-action-secondary-hover: #475569;

    /* 为原先只有亮色的独立表单补齐其现有变量。 */
    --bg: #0f172a;
    --panel: #111827;
    --text: #f8fafc;
    --muted: #cbd5e1;
    --border: #334155;
    --primary: #38bdf8;
    --primary-hover: #0ea5e9;
    --secondary: #334155;
    --secondary-hover: #475569;
    --success-bg: #143329;
    --success-text: #86efac;
    --error-bg: #3b2027;
    --error-text: #fca5a5;
    --warning-bg: #382c18;
    --warning-text: #fbbf24;
}

.site-theme-toolbar {
    display: flex;
    justify-content: flex-end;
    padding: 12px 20px;
}

.site-theme-toggle {
    display: inline-flex;
    align-items: center;
    justify-content: center;
    padding: 10px 16px;
    border: 1px solid var(--site-border);
    border-radius: 10px;
    background: var(--site-primary);
    color: var(--site-primary-text);
    font: inherit;
    font-weight: 600;
    cursor: pointer;
}

.site-theme-toggle:focus-visible {
    outline: 2px solid var(--site-primary);
    outline-offset: 3px;
}

/* 已有列表的标题区和右侧工具区保持在同一行。
   窄屏时按钮可在右侧区域内部换行，而不是整个工具区掉到标题下面。 */
.topbar,
.list-page-header {
    display: flex !important;
    flex-wrap: nowrap !important;
    align-items: flex-start !important;
    justify-content: space-between;
    gap: 16px;
}

.topbar > :first-child,
.list-page-header > :first-child {
    min-width: 0;
    flex: 1 1 auto;
}

.topbar > .actions,
.topbar > nav.actions,
.list-page-header > nav {
    flex: 0 1 auto;
    max-width: 65%;
    min-width: 0;
    justify-content: flex-end;
}

.list-page-header > nav {
    margin: 0;
}

.topbar h1,
.list-page-header h1 {
    margin-top: 0;
    overflow-wrap: anywhere;
}

/* 补齐使用硬编码亮色背景的页面，已有播放器和练习页仍使用各自主题变量。 */
html[data-theme="dark"] body {
    background: var(--site-bg) !important;
    color: var(--site-text) !important;
}

html[data-theme="dark"] .card,
html[data-theme="dark"] .empty-state,
html[data-theme="dark"] .feature-item,
html[data-theme="dark"] .profile-chip,
html[data-theme="dark"] .result-box,
html[data-theme="dark"] .selector-panel,
html[data-theme="dark"] .selected-panel,
html[data-theme="dark"] .selected-item,
html[data-theme="dark"] .selected-empty {
    background: var(--site-panel) !important;
    border-color: var(--site-border) !important;
    color: var(--site-text);
}

html[data-theme="dark"] .subtitle,
html[data-theme="dark"] .user-box,
html[data-theme="dark"] .help-text,
html[data-theme="dark"] .help,
html[data-theme="dark"] .feature-desc,
html[data-theme="dark"] .card p,
html[data-theme="dark"] .card .desc {
    color: var(--site-muted) !important;
}

html[data-theme="dark"] label {
    color: inherit;
}

html[data-theme="dark"] input[type="text"],
html[data-theme="dark"] input[type="password"],
html[data-theme="dark"] input[type="date"],
html[data-theme="dark"] input[type="file"],
html[data-theme="dark"] textarea,
html[data-theme="dark"] select,
html[data-theme="dark"] .ts-control,
html[data-theme="dark"] .ts-control input,
html[data-theme="dark"] .ts-dropdown {
    background: var(--site-panel) !important;
    color: var(--site-text) !important;
    border-color: var(--site-border) !important;
}

html[data-theme="dark"] .ts-dropdown .active {
    background: #334155;
    color: var(--site-text);
}

html[data-theme="dark"] .ts-option-title {
    color: var(--site-text);
}

html[data-theme="dark"] .card-emoji,
html[data-theme="dark"] .task-id-box,
html[data-theme="dark"] .chip.existing {
    background: #1e293b;
    color: #7dd3fc;
}

html[data-theme="dark"] .card .error-box,
html[data-theme="dark"] .message.error {
    background: #3b2027;
    color: #fca5a5 !important;
    border-color: #7f1d1d;
}

html[data-theme="dark"] .message.success {
    background: #143329;
    color: #86efac !important;
    border-color: #166534;
}

html[data-theme="dark"] .message.warning {
    background: #382c18;
    color: #fbbf24 !important;
}

html[data-theme="dark"] .link,
html[data-theme="dark"] .submit-btn {
    background: var(--site-primary);
    color: var(--site-primary-text);
}

html[data-theme="dark"] .link-secondary,
html[data-theme="dark"] .logout-btn,
html[data-theme="dark"] .toolbar a {
    background: #334155;
    color: var(--site-text);
}

html[data-theme="dark"] th,
html[data-theme="dark"] td {
    border-color: var(--site-border);
}

html[data-theme="dark"] th {
    background: #334155;
}

html[data-theme="dark"] tr:nth-child(even) {
    background: #1e293b;
}

@media (max-width: 640px) {
    .topbar,
    .list-page-header {
        gap: 10px;
    }

    .topbar > .actions,
    .topbar > nav.actions,
    .list-page-header > nav {
        max-width: 60%;
        gap: 8px;
    }
}

/* 公共导航按钮：覆盖独立页面的字体、边框及移动端缩小规则。
   主题入口兼容公共脚本使用的全部旧选择器，无需逐页改模板。
   不覆盖答题提交、播放器控制、软键盘及编辑/删除图标按钮。 */
html :is(
    [data-site-theme-toggle],
    #theme-toggle,
    #theme-toggle-btn,
    #themeToggleBtn,
    button.theme-toggle,
    .site-theme-toggle,
    a[href="/"],
    a[href="/profile/"],
    .logout-form button,
    a[href^="/flashcards/"][href$="/add/"],
    a[href^="/speech/"][href$="/add/"],
    .toolbar > a,
    .topbar > .actions > a,
    .list-page-header > nav > a,
    .page-actions > a,
    .page-toolbar > a,
    main > nav > a
) {
    box-sizing: border-box !important;
    display: inline-flex !important;
    align-items: center !important;
    justify-content: center !important;
    gap: 6px !important;
    min-height: 44px !important;
    width: auto !important;
    height: auto !important;
    margin: 0 !important;
    padding: 10px 16px !important;
    border: 1px solid transparent !important;
    border-radius: 10px !important;
    background: var(--site-primary) !important;
    color: var(--site-primary-text) !important;
    font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', sans-serif !important;
    font-size: 14px !important;
    font-weight: 600 !important;
    line-height: 22px !important;
    letter-spacing: normal !important;
    text-align: center !important;
    text-decoration: none !important;
    white-space: nowrap !important;
    box-shadow: none !important;
    cursor: pointer;
    transition: background-color .18s ease, color .18s ease;
}

/* 同功能使用同配色：新增和主题为主按钮，返回及账户操作为次按钮。 */
html :is(
    a[href="/"],
    a[href="/profile/"],
    .logout-form button,
    .toolbar > a:not([href$="/add/"]),
    .topbar > .actions > a.secondary,
    .list-page-header > nav > a[href="/"],
    .page-actions > a,
    .page-toolbar > a,
    main > nav > a
) {
    background: var(--site-action-secondary) !important;
    color: var(--site-text) !important;
    border-color: var(--site-border) !important;
}

html :is(
    [data-site-theme-toggle],
    #theme-toggle,
    #theme-toggle-btn,
    #themeToggleBtn,
    button.theme-toggle,
    .site-theme-toggle,
    a[href^="/flashcards/"][href$="/add/"],
    a[href^="/speech/"][href$="/add/"],
    .topbar > .actions > a:not(.secondary),
    .list-page-header > nav > a:not([href="/"])
):hover {
    background: var(--site-action-hover) !important;
}

html :is(
    a[href="/"],
    a[href="/profile/"],
    .logout-form button,
    .toolbar > a:not([href$="/add/"]),
    .topbar > .actions > a.secondary,
    .page-actions > a,
    .page-toolbar > a,
    main > nav > a
):hover {
    background: var(--site-action-secondary-hover) !important;
}

html :is(
    [data-site-theme-toggle],
    #theme-toggle,
    #theme-toggle-btn,
    #themeToggleBtn,
    button.theme-toggle,
    .site-theme-toggle,
    a[href="/"],
    a[href="/profile/"],
    .logout-form button,
    a[href^="/flashcards/"][href$="/add/"],
    a[href^="/speech/"][href$="/add/"],
    .toolbar > a,
    .topbar > .actions > a,
    .list-page-header > nav > a,
    .page-actions > a,
    .page-toolbar > a,
    main > nav > a
):focus-visible {
    outline: 2px solid var(--site-primary) !important;
    outline-offset: 3px !important;
}

@media (max-width: 640px) {
    .list:has(.wrong-label) {
        grid-template-columns: repeat(4, minmax(0, 1fr));
        gap: 6px;
    }
    .list:has(.wrong-label) .item {
        padding: 8px 4px;
        gap: 4px;
    }
    .list:has(.wrong-label) .item-answer {
        font-size: 20px;
        line-height: 1.2;
    }
    .list:has(.wrong-label) .item-text,
    .list:has(.wrong-label) .item-pinyin {
        font-size: 10px;
    }
    .list:has(.wrong-label) .wrong-label {
        font-size: 12px;
        gap: 2px;
    }
    .list:has(.wrong-label) .wrong-label input {
        width: 14px;
        height: 14px;
    }
}
