:root {
  --theme-toggle-dark: "🌙";
  --theme-toggle-light: "☀️";
}

.theme-toggle {
  position: relative;
  z-index: 1;
  display: inline-grid;
  place-items: center;
  flex: 0 0 40px;
  width: 40px;
  height: 40px;
  padding: 0;
  border: 1px solid #2d5560;
  border-radius: 10px;
  background: rgba(34, 112, 133, 0.14);
  color: var(--cyan);
  font-size: 19px;
  line-height: 1;
  cursor: pointer;
  touch-action: manipulation;
}

.theme-toggle:hover {
  border-color: var(--cyan);
  background: rgba(85, 230, 212, 0.1);
}

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

html[data-theme='light'] {
  --color-scheme: light;
  --bg: #f4f7fb;
  --panel: #ffffff;
  --panel-2: #f8fafc;
  --line: #d8e1ec;
  --text: #1e293b;
  --muted: #64748b;
  --cyan: #0f766e;
  --cyan-2: #0b665f;
  --lime: #70a30f;
  --red: #dc2626;
  --amber: #b45309;
  --shadow: 0 14px 34px rgba(31, 55, 81, 0.08);
}

html {
  color-scheme: var(--color-scheme, dark);
}

html[data-theme='light'] body {
  background:
    radial-gradient(circle at 20% 0%, rgba(99, 139, 253, 0.08), transparent 42%),
    linear-gradient(180deg, #f5f7fb 0%, #f3f6fb 100%);
  color: var(--text);
}

html[data-theme='light'] .ambient {
  opacity: 0.06;
}

html[data-theme='light'] .ambient-one { background: #9cc6ff; }
html[data-theme='light'] .ambient-two { background: #7da9ff; }

html[data-theme='light'] .site-header,
html[data-theme='light'] .panel,
html[data-theme='light'] .submission-form,
html[data-theme='light'] .form-panel,
html[data-theme='light'] .submit-aside,
html[data-theme='light'] .self-test-form,
html[data-theme='light'] .self-test-history,
html[data-theme='light'] .self-test-result,
html[data-theme='light'] .self-test-api-panel,
html[data-theme='light'] .formula-editor,
html[data-theme='light'] .formula-operator,
html[data-theme='light'] .formula-operator>summary,
html[data-theme='light'] .formula-operator-body,
html[data-theme='light'] .reference-section,
html[data-theme='light'] .api-guide-panel,
html[data-theme='light'] .batch-panel,
html[data-theme='light'] .submission-api-panel,
html[data-theme='light'] .metric-card,
html[data-theme='light'] .yearly-grid article,
html[data-theme='light'] .group-chart,
html[data-theme='light'] .reference-card,
html[data-theme='light'] .operator-card>div,
html[data-theme='light'] .operator-library-list,
html[data-theme='light'] .manifest-example,
html[data-theme='light'] .api-example-card,
html[data-theme='light'] .profile-settings-panel,
html[data-theme='light'] .profile-hero,
html[data-theme='light'] .result-panel,
html[data-theme='light'] .login-card {
  background: var(--panel);
  border-color: var(--line);
  box-shadow: var(--shadow);
}

html[data-theme='light'] .theme-toggle {
  border: 1px solid #c4d2e4;
  border-radius: 9px;
  background: #f8fbff;
  color: #1f2937;
}

html[data-theme='light'] .theme-toggle:hover {
  border-color: #9ab6ca;
  background: #edf3fb;
}

html[data-theme='light'] .main-nav a,
html[data-theme='light'] .account,
html[data-theme='light'] .account-login-link,
html[data-theme='light'] .guest-badge {
  color: #435569;
}

html[data-theme='light'] .main-nav a:hover,
html[data-theme='light'] .main-nav a.active {
  color: #0f172a;
}

html[data-theme='light'] .field input,
html[data-theme='light'] .field select,
html[data-theme='light'] .field textarea,
html[data-theme='light'] .stack-form input,
html[data-theme='light'] .operator-source-guide textarea,
html[data-theme='light'] .formula-editor .formula-operator-body textarea,
html[data-theme='light'] .formula-operator-body textarea,
html[data-theme='light'] .operator-arg-input,
html[data-theme='light'] .table-submit-button,
html[data-theme='light'] .table-cancel-button,
html[data-theme='light'] .self-test-form .operator-source-input,
html[data-theme='light'] .code-copy .code-copy-button {
  background: #ffffff;
  border-color: #d7e1ed;
  color: var(--text);
}

html[data-theme='light'] .primary-button,
html[data-theme='light'] .secondary-button,
html[data-theme='light'] .sort-form button {
  color: #0f172a;
}

html[data-theme='light'] .panel-heading,
html[data-theme='light'] .detail-grid .panel-heading,
html[data-theme='light'] .api-guide-tabs a:hover,
html[data-theme='light'] .api-guide-tabs a:focus-visible,
html[data-theme='light'] .self-test-api-heading {
  border-bottom-color: var(--line);
}

html[data-theme='light'] .rank-table th,
html[data-theme='light'] .self-test-api-table th,
html[data-theme='light'] .rank-table td,
html[data-theme='light'] .submission-row:hover,
html[data-theme='light'] .submission-row,
html[data-theme='light'] .metric-value strong,
html[data-theme='light'] .research-metric-row:hover,
html[data-theme='light'] .reference-table td {
  color: #1f2a3b;
}

html[data-theme='light'] .self-test-form .operator-source-input,
html[data-theme='light'] .code-copy pre,
html[data-theme='light'] pre,
html[data-theme='light'] code,
html[data-theme='light'] .api-guide-section pre,
html[data-theme='light'] .submission-api-panel pre,
html[data-theme='light'] .manifest-example pre,
html[data-theme='light'] .code-copy .code-copy-button+pre,
html[data-theme='light'] .batch-guide pre,
html[data-theme='light'] .spec-card pre,
html[data-theme='light'] .operator-brief pre {
  background: #f8fafc;
  border-color: #dae4f1;
  color: #233045;
}

@media (max-width: 900px) {
  html[data-theme='light'] .main-nav a.active:after { left: 5px; right: 5px; }
}

/* Keep the light theme coherent across every page.  Several feature-specific
   components have dark-mode fills in app.css, so they need explicit light
   surfaces here instead of inheriting the dark palette. */
html[data-theme='light'] .site-header {
  background: rgba(255, 255, 255, 0.94);
  border-bottom-color: #dbe4ee;
  box-shadow: 0 3px 16px rgba(31, 55, 81, 0.05);
}

html[data-theme='light'] .brand strong,
html[data-theme='light'] .main-nav a.active,
html[data-theme='light'] .text-button:hover {
  color: #1e293b;
}

html[data-theme='light'] .brand-factor,
html[data-theme='light'] .brand-leaderboard {
  color: #334155;
}

html[data-theme='light'] .brand small,
html[data-theme='light'] .brand-title small {
  color: #64748b;
}

html[data-theme='light'] .brand-mark {
  background: #e7f4f2;
  border-color: #9bc8c1;
}

html[data-theme='light'] .account,
html[data-theme='light'] .main-nav a,
html[data-theme='light'] .account-name,
html[data-theme='light'] .text-button {
  color: #475569;
}

html[data-theme='light'] .avatar,
html[data-theme='light'] .researcher-avatar {
  background: #e7f4f2;
  border-color: #9bc8c1;
  color: #0f766e;
}

html[data-theme='light'] .panel,
html[data-theme='light'] .reference-section,
html[data-theme='light'] .login-card,
html[data-theme='light'] .profile-hero,
html[data-theme='light'] .result-panel,
html[data-theme='light'] .submission-api-panel,
html[data-theme='light'] .batch-panel,
html[data-theme='light'] .self-test-api,
html[data-theme='light'] .self-test-definition,
html[data-theme='light'] .activity-panel {
  background: #ffffff;
  border-color: var(--line);
  box-shadow: var(--shadow);
}

html[data-theme='light'] .reference-jump,
html[data-theme='light'] .reference-table-wrap,
html[data-theme='light'] .operator-group-title,
html[data-theme='light'] .rule-grid li,
html[data-theme='light'] .reference-card,
html[data-theme='light'] .metric-card,
html[data-theme='light'] .self-test-api-body section,
html[data-theme='light'] .self-test-definition-grid article,
html[data-theme='light'] .api-example-card,
html[data-theme='light'] .api-guide-http-details,
html[data-theme='light'] .api-guide-params,
html[data-theme='light'] .formula-operator,
html[data-theme='light'] .formula-neutralization,
html[data-theme='light'] .manifest-example {
  background: #f8fafc;
  border-color: var(--line);
}

html[data-theme='light'] .reference-jump>a,
html[data-theme='light'] .reference-jump .reference-subnav a {
  color: #475569;
}

html[data-theme='light'] .reference-jump>a:hover,
html[data-theme='light'] .reference-jump .reference-subnav a:hover {
  background: #e6f3f1;
  color: #0f766e;
}

html[data-theme='light'] .reference-downloads a,
html[data-theme='light'] .secondary-button,
html[data-theme='light'] .code-copy-button,
html[data-theme='light'] .manifest-example-head button {
  background: #ffffff;
  border-color: #8bb9b3;
  color: #0f766e;
}

html[data-theme='light'] .reference-downloads a:hover,
html[data-theme='light'] .secondary-button:hover,
html[data-theme='light'] .code-copy-button:hover,
html[data-theme='light'] .manifest-example-head button:hover {
  background: #e9f6f4;
  border-color: #0f766e;
  color: #075e58;
}

html[data-theme='light'] .reference-table th,
html[data-theme='light'] .rank-table th,
html[data-theme='light'] .self-test-api-table th {
  background: #f1f5f9;
  color: #475569;
  border-bottom-color: var(--line);
}

html[data-theme='light'] .reference-table td,
html[data-theme='light'] .rank-table td,
html[data-theme='light'] .self-test-api-table td {
  color: #475569;
  border-bottom-color: #e5eaf1;
}

html[data-theme='light'] .reference-table tbody tr:hover,
html[data-theme='light'] .rank-table tbody tr:hover,
html[data-theme='light'] .submission-row:hover {
  background: #f0f8f7;
}

html[data-theme='light'] .field input,
html[data-theme='light'] .field select,
html[data-theme='light'] .field textarea,
html[data-theme='light'] .stack-form input,
html[data-theme='light'] .operator-source-input,
html[data-theme='light'] .formula-operator-body textarea,
html[data-theme='light'] .neutralization-return-row input[type='number'] {
  background: #ffffff;
  border-color: #cbd5e1;
  color: #1e293b;
}

html[data-theme='light'] .field>span,
html[data-theme='light'] .stack-form label,
html[data-theme='light'] .operator-group-title p,
html[data-theme='light'] .api-guide-panel .self-test-api-body>p,
html[data-theme='light'] .api-guide-section>p,
html[data-theme='light'] .api-guide-intro,
html[data-theme='light'] .api-guide-list,
html[data-theme='light'] .api-guide-panel .self-test-api-body ul {
  color: #64748b;
}

html[data-theme='light'] pre,
html[data-theme='light'] code,
html[data-theme='light'] .code-copy pre,
html[data-theme='light'] .manifest-example pre,
html[data-theme='light'] .submission-api-panel pre,
html[data-theme='light'] .self-test-api-body pre,
html[data-theme='light'] .batch-guide pre,
html[data-theme='light'] .spec-card pre,
html[data-theme='light'] .operator-brief pre {
  background: #f1f5f9;
  border-color: #d6e0eb;
  color: #334155;
}

html[data-theme='light'] .formula-autocomplete,
html[data-theme='light'] .score-info-content {
  background: #ffffff;
  border-color: #cbd5e1;
  box-shadow: 0 14px 34px rgba(31, 55, 81, 0.16);
  color: #475569;
}

html[data-theme='light'] .formula-autocomplete button {
  color: #334155;
}

html[data-theme='light'] .formula-autocomplete button:hover,
html[data-theme='light'] .formula-autocomplete button.selected {
  background: #e8f5f3;
}

html[data-theme='light'] .neutralization-row,
html[data-theme='light'] .neutralization-barra-toggle {
  background: #ffffff;
  color: #334155;
  border-color: #d5dee8;
}

html[data-theme='light'] .neutralization-row:hover,
html[data-theme='light'] .neutralization-row:has(input:checked) {
  background: #eaf6f4;
  border-color: #8bb9b3;
}

html[data-theme='light'] .neutralization-row>input[type='checkbox'],
html[data-theme='light'] .neutralization-barra-toggle input[type='checkbox'],
html[data-theme='light'] .neutralization-barra-columns input[type='checkbox'] {
  background: #ffffff;
  border-color: #94a3b8;
}

html[data-theme='light'] footer {
  border-top-color: #dbe4ee;
  color: #64748b;
}

/* Remaining dark-mode accents used by the ranking and research pages. */
html[data-theme='light'] .board-tabs a {
  background: #ffffff;
  border-color: #d5dee8;
  color: #475569;
}

html[data-theme='light'] .board-tabs a span,
html[data-theme='light'] .board-tabs a small {
  color: #64748b;
}

html[data-theme='light'] .board-tabs a.active {
  background: linear-gradient(135deg, #e5f4f1, #f7fbfb);
  border-color: #79b7af;
  color: #134e4a;
  box-shadow: inset 0 0 0 1px rgba(15, 118, 110, 0.08);
}

html[data-theme='light'] .board-tabs a.active span,
html[data-theme='light'] .board-tabs a.active small {
  color: #0f766e;
}

html[data-theme='light'] .factor-cell,
html[data-theme='light'] .rank-table .factor-cell {
  color: #334155 !important;
}

html[data-theme='light'] .score {
  color: #334155;
}

html[data-theme='light'] .score-bar {
  background: #dbe5ed;
}

html[data-theme='light'] .empty-state h3,
html[data-theme='light'] .empty-state>span {
  color: #334155;
}

/* Developer center: override dark-theme copy colors with readable light-mode
   values. These selectors intentionally match app.css specificity. */
html[data-theme='light'] .api-guide-intro ul,
html[data-theme='light'] .api-guide-list,
html[data-theme='light'] .api-guide-panel .self-test-api-body ul {
  color: #475569;
}

html[data-theme='light'] .api-guide-subsection>p,
html[data-theme='light'] .api-guide-section>p,
html[data-theme='light'] .api-guide-panel .self-test-api-body>p,
html[data-theme='light'] .api-guide-http-note,
html[data-theme='light'] .api-example-card p,
html[data-theme='light'] .api-example-card small {
  color: #475569;
}

html[data-theme='light'] .api-guide-subsection>p strong,
html[data-theme='light'] .api-guide-section>p strong {
  color: #334155;
}

html[data-theme='light'] .theme-toggle {
  border: 1px solid #cbd5e1;
  border-radius: 9px;
  background: #f8fafc;
  color: #0f766e;
  font-size: 18px;
  line-height: 1;
}

html[data-theme='light'] .theme-toggle:hover {
  border-color: #9ab6ca;
  background: #edf3fb;
}

/* ==========================================================================
   Light-theme coverage fixes (2026-08-26).
   app.css hardcodes dark surfaces / near-white text in the components below;
   theme.css previously missed them, leaving white-on-white text and dark
   blocks embedded in light pages.  All rules are scoped to light mode only.
   ========================================================================== */

/* Metric cards: near-white figures (#f3f8fc) were invisible on white cards. */
html[data-theme='light'] .metric-card dd { color: #1e293b; }
html[data-theme='light'] .profile-stats strong { color: #1e293b; }
html[data-theme='light'] .profile-stats span { color: #64748b; }

/* Operator signatures: app.css pins #dce9f2 with !important, so match it. */
html[data-theme='light'] .operator-signature { color: #0f766e !important; }

/* Research tabs and my-research table chrome. */
html[data-theme='light'] .research-tabs { border-bottom-color: #dbe4ee; }
html[data-theme='light'] .research-tabs a { color: #64748b; }
html[data-theme='light'] .research-tabs a:hover,
html[data-theme='light'] .research-tabs a.active { color: #1e293b; }
html[data-theme='light'] .research-tabs a span { background: #eef2f7; color: #64748b; }
html[data-theme='light'] .research-tabs a.active span { background: #d7efec; color: #0f766e; }

html[data-theme='light'] .research-universe-head { background: #f1f5f9; }
html[data-theme='light'] .universe-group { color: #475569; }
html[data-theme='light'] .factor-summary { background: #f8fafc; border-right-color: #e5eaf1; }
html[data-theme='light'] .metric-sort-heading { color: #64748b; }
html[data-theme='light'] .metric-sort-heading.active { color: #0f766e; background: #eef7f5; }
html[data-theme='light'] .metric-sort-heading a { border-color: #cbd5e1; color: #64748b; }
html[data-theme='light'] .metric-value { border-left-color: #e5eaf1; }

/* Backtest chart cards: replace the night-blue gradient with light surfaces. */
html[data-theme='light'] .site-chart-grid article { background: #ffffff; }
html[data-theme='light'] .site-chart-label { color: #334155; }
html[data-theme='light'] .site-chart-legend span { color: #64748b; }
html[data-theme='light'] .chart-grid-line { stroke: #e2e8f0; }
html[data-theme='light'] .chart-axis,
html[data-theme='light'] .chart-tick-mark { stroke: #94a3b8; }
html[data-theme='light'] .chart-zero-line { stroke: #94a3b8; }
html[data-theme='light'] .chart-tick { fill: #64748b; }
html[data-theme='light'] .group-chart svg {
  background: linear-gradient(rgba(148, 163, 184, 0.14), rgba(148, 163, 184, 0.14));
}

/* Benchmark strip: dark gradient -> light teal band. */
html[data-theme='light'] .benchmark-strip {
  background: linear-gradient(135deg, #ecf7f5, #ffffff);
  border-color: #9bc8c1;
}
html[data-theme='light'] .benchmark-strip>div { border-color: #dbe9e6; }
html[data-theme='light'] .benchmark-strip span { color: #64748b; }
html[data-theme='light'] .benchmark-strip strong { color: #1e293b; }
html[data-theme='light'] .benchmark-strip small { color: #94a3b8; }
html[data-theme='light'] .benchmark-strip .benchmark-id span { color: #0f766e; }
html[data-theme='light'] .benchmark-strip .benchmark-total strong { color: #4d7c0f; }

/* Formula-fix panel: dark red box -> soft rose surface. */
html[data-theme='light'] .formula-fix {
  background: #fdf2f3;
  border-color: #e7b3ba;
  box-shadow: 0 8px 22px rgba(31, 55, 81, 0.06);
}
html[data-theme='light'] .formula-fix-heading strong { color: #b91c1c; }
html[data-theme='light'] .formula-fix-heading span { color: #64748b; }
html[data-theme='light'] .formula-fix kbd {
  background: #f1f5f9; border-color: #cbd5e1; color: #334155;
}
html[data-theme='light'] .formula-fix-summary { color: #64748b; }
html[data-theme='light'] .formula-fix-summary code {
  background: #fff1f2; border-color: #f1c9cd; color: #b91c1c;
}
html[data-theme='light'] .formula-fix-original {
  background: #ffffff; border-color: #e8d3d6; color: #475569;
}
html[data-theme='light'] .formula-fix-original mark {
  color: #dc2626; text-decoration-color: #dc2626;
}
html[data-theme='light'] .formula-fix-suggestion {
  background: #ecf7f5; border-color: #9bc8c1; color: #0f766e;
}
html[data-theme='light'] .formula-fix-suggestion:hover,
html[data-theme='light'] .formula-fix-suggestion:focus {
  background: #e0f2ef; border-color: #0f766e;
}
html[data-theme='light'] .formula-fix-suggestion code { color: #0f5f59; }

/* Floating benchmark locator pill. */
html[data-theme='light'] .benchmark-locator {
  background: rgba(255, 255, 255, 0.96);
  border-color: #9bc8c1;
  box-shadow: 0 8px 24px rgba(31, 55, 81, 0.14);
}

/* Status pills: lift text contrast on white backgrounds. */
html[data-theme='light'] .status-pill { border-color: #cbd5e1; color: #64748b; }
html[data-theme='light'] .status-completed { border-color: #a3c47a; color: #4d7c0f; background: #f3f9e8; }
html[data-theme='light'] .status-queued { border-color: #cbd5e1; color: #64748b; }
html[data-theme='light'] .status-running { border-color: #8bb9b3; color: #0f766e; background: #eaf6f4; }
html[data-theme='light'] .status-failed,
html[data-theme='light'] .status-rejected_high_correlation { border-color: #e4a3a9; color: #b91c1c; }

/* Logo refinements for light mode. */
html[data-theme='light'] .brand-mark-frame {
  fill: #f1f5f9;
  stroke: #94a3b8;
}
html[data-theme='light'] .brand-factor {
  color: #0f172a;
}
html[data-theme='light'] .brand-leaderboard {
  color: #64748b;
}

/* Activity calendar (profile heatmap): default cells are night-blue (#172432)
   with a teal ramp tuned for dark backgrounds; on white panels they read as a
   wall of dark blocks.  Remap to a light grey base + brand-teal ramp. */
html[data-theme='light'] .calendar-day,
html[data-theme='light'] .calendar-legend i {
  background: #e9eef4;
  border-color: rgba(100, 116, 139, 0.18);
}
html[data-theme='light'] .calendar-day.level-1,
html[data-theme='light'] .calendar-legend .level-1 { background: #c3e7e2; }
html[data-theme='light'] .calendar-day.level-2,
html[data-theme='light'] .calendar-legend .level-2 { background: #85d3c8; }
html[data-theme='light'] .calendar-day.level-3,
html[data-theme='light'] .calendar-legend .level-3 { background: #35ab9c; }
html[data-theme='light'] .calendar-day.level-4,
html[data-theme='light'] .calendar-legend .level-4 { background: #0f766e; }
html[data-theme='light'] .calendar-months span,
html[data-theme='light'] .calendar-weekdays span,
html[data-theme='light'] .calendar-legend {
  color: #64748b;
}

/* Public reference rules: app.css gives these cards dark-mode text colors
   with more specific selectors, so match that specificity in light mode. */
html[data-theme='light'] .rules-section .rule-grid strong {
  color: #334155;
}

html[data-theme='light'] .rules-section .rule-grid p {
  color: #475569;
}

html[data-theme='light'] .rules-section .rule-grid code {
  color: #0f766e;
}

/* Leaderboard ranks and benchmark values need darker accents on white. */
html[data-theme='light'] .rank {
  color: #475569;
}

html[data-theme='light'] .rank.rank-1 { color: #7c570c; }
html[data-theme='light'] .rank.rank-2 { color: #526173; }
html[data-theme='light'] .rank.rank-3 { color: #8b4a20; }

html[data-theme='light'] .benchmark-row .factor-cell,
html[data-theme='light'] .benchmark-row .score {
  color: #4d7c0f !important;
}

html[data-theme='light'] .week-card span,
html[data-theme='light'] .week-card small {
  color: #475569;
}

/* Login page secondary copy and status accents. */
html[data-theme='light'] .metric-strip span,
html[data-theme='light'] .guest-login-link {
  color: #475569;
}

html[data-theme='light'] .status-dot {
  color: #4d7c0f;
}
