* {
    margin: 0;
    padding: 0;
    box-sizing: border-box;
}

html {
    scroll-behavior: smooth;
    scroll-padding-top: 4rem;
}

:root {
    --color-primary: #2563eb;
    --color-primary-light: #dbeafe;
    --color-primary-dark: #1d4ed8;
    --color-danger: #dc2626;
    --color-success: #16a34a;
    --color-warning: #ca8a04;
    --color-bg: #f1f5f9;
    --color-card: #ffffff;
    --color-border: #e2e8f0;
    --color-text: #1e293b;
    --color-text-muted: #64748b;
    --radius: 12px;
    --shadow-sm: 0 1px 2px rgba(0,0,0,0.04);
    --shadow-md: 0 4px 12px rgba(0,0,0,0.06);
    --shadow-lg: 0 8px 24px rgba(0,0,0,0.08);
    --transition: 0.2s ease;
}

body {
    font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', 'Inter', sans-serif;
    background: var(--color-bg);
    color: var(--color-text);
    line-height: 1.6;
    padding: 0;
    margin: 0;
}

/* ===== Sidebar Navigation (Outline Style) ===== */
.sidebar {
    position: fixed;
    top: 3rem;
    left: 0;
    width: 140px;
    padding: 0 0 0 1rem;
    display: flex;
    flex-direction: column;
    z-index: 1000;
}

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

.sidebar .nav-links {
    display: flex;
    flex-direction: column;
    gap: 0;
    border-left: 1px solid var(--color-border);
}

.sidebar .nav-links a {
    text-decoration: none;
    font-size: 0.72rem;
    font-weight: 400;
    color: var(--color-text-muted);
    padding: 0.35rem 0.75rem;
    margin-left: -1px;
    border-left: 1px solid transparent;
    transition: all var(--transition);
    line-height: 1.4;
}

.sidebar .nav-links a:hover {
    color: var(--color-text);
}

.sidebar .nav-links a.active {
    color: var(--color-primary);
    border-left-color: var(--color-primary);
}

/* ===== Main Content (offset for sidebar) ===== */
.main-content {
    margin-left: 140px;
    padding: 2rem 2.5rem;
    max-width: none;
    margin-right: 2rem;
}

/* ===== Header ===== */
header {
    text-align: left;
    margin-bottom: 1.75rem;
    padding: 1.25rem 1.5rem;
    background: linear-gradient(135deg, #1e3a5f 0%, #2563eb 100%);
    border-radius: var(--radius);
    box-shadow: var(--shadow-md);
}

header h1 {
    font-size: 1.2rem;
    font-weight: 600;
    color: #ffffff;
    margin-bottom: 0.15rem;
    letter-spacing: -0.01em;
}

.subtitle {
    color: rgba(255,255,255,0.65);
    font-size: 0.78rem;
    font-weight: 400;
}

/* ===== Divergence Indicator Panel ===== */
.divergence-indicator {
    display: flex;
    gap: 1rem;
    margin: 1.25rem 0;
    flex-wrap: wrap;
}

.div-card {
    flex: 1;
    min-width: 180px;
    padding: 1rem;
    border-radius: 8px;
    text-align: center;
    border: 1.5px solid var(--color-border);
    background: #fafbfc;
}

.div-card.signal {
    border-color: #f59e0b;
    background: #fffbeb;
}

.div-card.signal-strong {
    border-color: #dc2626;
    background: #fef2f2;
    animation: pulse-border 1.5s ease-in-out infinite;
}

@keyframes pulse-border {
    0%, 100% { border-color: #dc2626; }
    50% { border-color: #fca5a5; }
}

.div-card .div-value {
    font-size: 1.6rem;
    font-weight: 800;
    letter-spacing: -0.02em;
}

.div-card .div-value.positive { color: #dc2626; }
.div-card .div-value.negative { color: #2563eb; }
.div-card .div-value.neutral { color: var(--color-text-muted); }

.div-card .div-label {
    font-size: 0.72rem;
    color: var(--color-text-muted);
    margin-top: 0.25rem;
    font-weight: 500;
}

.div-card .div-action {
    font-size: 0.75rem;
    font-weight: 600;
    margin-top: 0.4rem;
    padding: 0.2rem 0.5rem;
    border-radius: 4px;
    display: inline-block;
}

.div-action.buy { background: #dbeafe; color: #1d4ed8; }
.div-action.sell { background: #fee2e2; color: #dc2626; }
.div-action.hold { background: #f1f5f9; color: var(--color-text-muted); }

/* ===== Price Monitor Section ===== */
.monitor-input {
    margin-bottom: 1.5rem;
}

/* ===== Theo Premium Monitor (Bloomberg Excel-style live snapshot) ===== */
.monitor-subpanel {
    margin-bottom: 1.5rem;
}

.theo-mon-grid {
    display: grid;
    grid-template-columns: 1fr 1fr;
    gap: 1rem;
    margin-top: 0.75rem;
}
@media (max-width: 880px) {
    .theo-mon-grid { grid-template-columns: 1fr; }
}

.theo-mon-card {
    border: 1px solid var(--color-border);
    border-radius: var(--radius);
    padding: 0.85rem 1rem;
    background: #fafbfc;
}
.theo-mon-card h4 {
    margin: 0 0 0.6rem;
    font-size: 0.82rem;
    font-weight: 600;
    color: var(--color-text);
    border-bottom: 1px solid var(--color-border);
    padding-bottom: 0.35rem;
}
.theo-mon-hint {
    color: var(--color-text-muted);
    font-size: 0.7rem;
    font-weight: 400;
    margin-left: 0.4rem;
}

.theo-mon-table {
    width: 100%;
    border-collapse: collapse;
    font-size: 0.78rem;
}
.theo-mon-table th,
.theo-mon-table td {
    padding: 0.35rem 0.5rem;
    border-bottom: 1px solid var(--color-border);
    text-align: left;
    vertical-align: middle;
}
.theo-mon-table th {
    background: #f1f5f9;
    font-weight: 600;
    font-size: 0.72rem;
    color: var(--color-text-muted);
}
.theo-mon-table td code {
    background: #eef2f7;
    color: #475569;
    padding: 0.05rem 0.35rem;
    border-radius: 3px;
    font-size: 0.72rem;
}
.theo-mon-table input[type="number"] {
    width: 100%;
    border: 1px solid var(--color-border);
    border-radius: 5px;
    padding: 0.25rem 0.45rem;
    font-size: 0.78rem;
    text-align: right;
}
.theo-mon-table input[type="number"]:focus {
    outline: none;
    border-color: var(--color-primary);
    box-shadow: 0 0 0 2px rgba(37, 99, 235, 0.12);
}
.theo-mon-table .num-cell {
    font-family: ui-monospace, SFMono-Regular, Menlo, monospace;
    color: var(--color-text);
}
.theo-mon-table tr.highlight {
    background: #fef9c3;
}
.theo-mon-table tr.highlight td {
    border-bottom: none;
}

.theo-mon-actions {
    display: flex;
    gap: 0.5rem;
    margin-top: 0.6rem;
}

.theo-mon-formula {
    margin: 0.6rem 0 0;
    font-size: 0.72rem;
    color: var(--color-text-muted);
}
.theo-mon-formula code {
    background: #eef2f7;
    color: #1e293b;
    padding: 0.05rem 0.35rem;
    border-radius: 3px;
    font-size: 0.72rem;
}

.theo-mon-table-wrap {
    border: 1px solid var(--color-border);
    border-radius: var(--radius);
    overflow-x: auto;
    background: #fafbfc;
}
.theo-mon-result {
    font-size: 0.8rem;
}
.theo-mon-result th {
    background: linear-gradient(180deg, #1e3a5f 0%, #2563eb 100%);
    color: #fff;
    font-weight: 600;
    text-align: center;
    padding: 0.45rem 0.5rem;
}
.theo-mon-result td {
    text-align: center;
}
.theo-mon-result td:first-child {
    text-align: left;
    font-weight: 500;
}
.theo-mon-empty {
    color: var(--color-text-muted);
    text-align: center !important;
    padding: 1rem !important;
    font-style: italic;
}

/* Legacy "minute-level" subsection inside Price Monitor — collapsed by default. */
.monitor-legacy {
    margin-top: 1.25rem;
    border-top: 1px solid var(--color-border);
    padding-top: 1rem;
}
.monitor-legacy > summary {
    cursor: pointer;
    font-size: 0.85rem;
    font-weight: 600;
    color: var(--color-text-muted);
    padding: 0.4rem 0;
    user-select: none;
}
.monitor-legacy > summary:hover {
    color: var(--color-primary);
}
.monitor-legacy[open] > summary {
    color: var(--color-text);
    margin-bottom: 0.5rem;
}

.monitor-baseline {
    display: flex;
    gap: 0.75rem;
    align-items: center;
    flex-wrap: wrap;
    margin-bottom: 1rem;
    padding: 0.75rem 1rem;
    background: #f8fafc;
    border: 1px dashed var(--color-border);
    border-radius: 8px;
}

.monitor-baseline .baseline-label {
    font-size: 0.78rem;
    font-weight: 600;
    color: var(--color-text-muted);
}

.monitor-baseline label {
    font-size: 0.72rem;
    font-weight: 500;
    color: var(--color-text-muted);
    display: flex;
    flex-direction: column;
    gap: 0.2rem;
}

.monitor-baseline input {
    padding: 0.4rem 0.5rem;
    border: 1.5px solid var(--color-border);
    border-radius: 5px;
    font-size: 0.82rem;
    width: 100px;
}

.monitor-table-wrap {
    max-height: 300px;
    overflow-y: auto;
    border: 1px solid var(--color-border);
    border-radius: 8px;
}

#monitor-table {
    width: 100%;
    border-collapse: collapse;
    font-size: 0.82rem;
}

#monitor-table th {
    background: linear-gradient(to bottom, #f8fafc, #f1f5f9);
    padding: 0.5rem;
    font-size: 0.72rem;
    font-weight: 600;
    color: var(--color-text-muted);
    text-align: center;
    position: sticky;
    top: 0;
    border-bottom: 2px solid var(--color-border);
}

#monitor-table td {
    padding: 0;
    border-bottom: 1px solid #f1f5f9;
    text-align: center;
}

#monitor-table input {
    width: 100%;
    border: none;
    text-align: center;
    padding: 0.4rem;
    font-size: 0.82rem;
    background: transparent;
}

#monitor-table input:focus {
    outline: none;
    background: #dbeafe;
}

#monitor-table input.shadow-filled {
    background-color: #fff7ed !important;
    color: #ea580c;
}

#monitor-table input.shadow-cell {
    background-color: #fff7ed;
    color: #ea580c;
    cursor: default;
}

#monitor-table tr:nth-child(even) td {
    background: #fafbfc;
}

.monitor-charts {
    display: grid;
    grid-template-columns: 1fr 1fr;
    gap: 1.25rem;
}

/* ===== Blocks / Cards ===== */
.block {
    background: var(--color-card);
    border: none;
    border-radius: var(--radius);
    padding: 1.75rem;
    margin-bottom: 1.5rem;
    box-shadow: var(--shadow-md);
    transition: box-shadow var(--transition), transform var(--transition);
}

.block:hover {
    box-shadow: var(--shadow-lg);
}

.block h2 {
    font-size: 1.2rem;
    font-weight: 600;
    margin-bottom: 1.25rem;
    color: var(--color-text);
}

.hidden {
    display: none;
}

/* ===== Collapse Toggle (Accordion) ===== */
.collapse-toggle {
    display: flex;
    align-items: center;
    gap: 0.75rem;
    width: 100%;
    background: none;
    border: none;
    padding: 0.5rem 0;
    cursor: pointer;
    text-align: left;
    transition: opacity var(--transition);
}

.collapse-toggle:hover {
    opacity: 0.8;
}

.collapse-icon {
    display: flex;
    align-items: center;
    justify-content: center;
    width: 28px;
    height: 28px;
    border-radius: 6px;
    background: var(--color-primary-light);
    flex-shrink: 0;
    transition: transform 0.3s ease, background 0.3s ease;
}

.collapse-icon::after {
    content: '';
    width: 8px;
    height: 8px;
    border-right: 2px solid var(--color-primary);
    border-bottom: 2px solid var(--color-primary);
    transform: rotate(-45deg);
    transition: transform 0.3s ease;
}

.collapse-toggle[aria-expanded="true"] .collapse-icon {
    background: var(--color-primary);
}

.collapse-toggle[aria-expanded="true"] .collapse-icon::after {
    border-color: white;
    transform: rotate(45deg);
}

.collapse-title {
    font-size: 1.1rem;
    font-weight: 600;
    color: var(--color-text);
}

.collapse-hint {
    font-size: 0.78rem;
    color: var(--color-text-muted);
    margin-left: auto;
    transition: opacity 0.3s ease;
}

.collapse-toggle[aria-expanded="true"] .collapse-hint {
    opacity: 0;
}

/* Collapsible content with smooth animation */
.collapsible {
    display: grid;
    grid-template-rows: 0fr;
    transition: grid-template-rows 0.35s ease, opacity 0.25s ease;
    opacity: 0;
}

.collapsible > * {
    overflow: hidden;
}

.collapsible.open {
    grid-template-rows: 1fr;
    opacity: 1;
    margin-top: 1rem;
}

/* ===== Guide Grid ===== */
.guide-grid {
    display: grid;
    grid-template-columns: 1fr 1fr;
    gap: 1rem;
}

.guide-card {
    border: 1px solid var(--color-border);
    border-radius: var(--radius);
    padding: 1.25rem;
    background: #fafbfc;
    transition: border-color var(--transition);
}

.guide-card:hover {
    border-color: var(--color-primary);
}

.guide-card h3 {
    font-size: 0.9rem;
    font-weight: 600;
    margin-bottom: 0.5rem;
    color: var(--color-primary-dark);
}

.guide-card.full-width {
    grid-column: 1 / -1;
}

.formula-table {
    width: 100%;
    border-collapse: collapse;
    font-size: 0.82rem;
    margin-top: 0.5rem;
}

.formula-table th,
.formula-table td {
    border: 1px solid var(--color-border);
    padding: 0.5rem 0.7rem;
    text-align: left;
}

.formula-table th {
    background: #f1f5f9;
    font-weight: 600;
    color: var(--color-text-muted);
    text-transform: uppercase;
    font-size: 0.72rem;
    letter-spacing: 0.04em;
}

.formula-table code {
    background: #e8f0fe;
    color: var(--color-primary-dark);
    padding: 0.15rem 0.4rem;
    border-radius: 4px;
    font-size: 0.78rem;
    font-weight: 500;
}

.note {
    margin-top: 0.75rem;
    font-size: 0.82rem;
    color: var(--color-text-muted);
    font-style: italic;
    padding: 0.5rem 0.75rem;
    background: #fffbeb;
    border-left: 3px solid var(--color-warning);
    border-radius: 4px;
}

/* ===== Mode Switch (Pill Style) ===== */
.mode-switch {
    display: flex;
    width: fit-content;
    gap: 0;
    margin-bottom: 0;
    background: #f1f5f9;
    border-radius: 8px;
    padding: 4px;
    border: 1px solid var(--color-border);
}

.inline-hint {
    font-size: 0.75rem;
    color: var(--color-text-muted);
    margin: 0.3rem 0 0.75rem 0;
    padding-left: 0.25rem;
}

/* ===== Baseline Section ===== */
.baseline-section {
    margin-bottom: 1.25rem;
    padding: 1rem 1.25rem;
    background: #f8fafc;
    border: 1px dashed var(--color-border);
    border-radius: 8px;
}

/* ===== Ticker Config Section ===== */
.ticker-section {
    margin-bottom: 1.25rem;
    padding: 1rem 1.25rem;
    background: linear-gradient(135deg, #eff6ff, #f0f9ff);
    border: 1px solid #bfdbfe;
    border-radius: 8px;
}

.source-selector {
    margin-bottom: 0.75rem;
}

.source-label {
    display: block;
    font-size: 0.72rem;
    font-weight: 600;
    color: var(--color-text-muted);
    text-transform: uppercase;
    letter-spacing: 0.03em;
    margin-bottom: 0.3rem;
}

.source-selector select {
    padding: 0.5rem 0.75rem;
    border: 1.5px solid #bfdbfe;
    border-radius: 6px;
    font-size: 0.85rem;
    background: white;
    cursor: pointer;
    min-width: 280px;
    transition: border-color var(--transition);
}

.source-selector select:focus {
    outline: none;
    border-color: var(--color-primary);
}

.source-selector select option:disabled {
    color: #a0aec0;
    font-style: italic;
}

.source-row {
    display: flex;
    gap: 0.75rem;
    align-items: center;
}

.token-input {
    padding: 0.5rem 0.75rem;
    border: 1.5px solid #bfdbfe;
    border-radius: 6px;
    font-size: 0.85rem;
    font-family: 'SF Mono', 'Menlo', monospace;
    background: white;
    min-width: 200px;
    transition: border-color var(--transition), box-shadow var(--transition);
}

.token-input:focus {
    outline: none;
    border-color: var(--color-primary);
    box-shadow: 0 0 0 3px rgba(37, 99, 235, 0.12);
}

.token-input.hidden {
    display: none;
}

.ticker-grid {
    display: flex;
    gap: 0.75rem;
    flex-wrap: wrap;
    margin-bottom: 0.75rem;
}

.ticker-grid .ticker-item {
    display: flex;
    flex-direction: column;
    gap: 0.2rem;
    min-width: 150px;
    flex: 1;
}

.ticker-grid .ticker-item label {
    font-size: 0.72rem;
    font-weight: 600;
    color: var(--color-text-muted);
    text-transform: uppercase;
    letter-spacing: 0.03em;
}

.ticker-grid .ticker-item input {
    padding: 0.45rem 0.6rem;
    border: 1.5px solid #bfdbfe;
    border-radius: 6px;
    font-size: 0.85rem;
    font-family: 'SF Mono', 'Menlo', monospace;
    background: white;
    transition: border-color var(--transition), box-shadow var(--transition);
}

.ticker-grid .ticker-item input:focus {
    outline: none;
    border-color: var(--color-primary);
    box-shadow: 0 0 0 3px rgba(37, 99, 235, 0.12);
}

.ticker-actions {
    display: flex;
    align-items: center;
    gap: 0.75rem;
    flex-wrap: wrap;
}

.ticker-actions .btn svg {
    flex-shrink: 0;
}

.fetch-hint {
    font-size: 0.75rem;
    color: var(--color-text-muted);
}

.fetch-status {
    font-size: 0.8rem;
    color: var(--color-text-muted);
    transition: color 0.3s;
}

.fetch-status.success { color: var(--color-success); font-weight: 500; }
.fetch-status.error { color: var(--color-danger); font-weight: 500; }
.fetch-status.loading { color: var(--color-primary); }

.baseline-section h3 {
    font-size: 0.85rem;
    font-weight: 600;
    color: var(--color-text-muted);
    margin-bottom: 0.75rem;
}

.baseline-grid {
    display: flex;
    gap: 1rem;
    flex-wrap: wrap;
}

.baseline-grid .baseline-item {
    display: flex;
    flex-direction: column;
    gap: 0.25rem;
    min-width: 140px;
}

.baseline-grid .baseline-item label {
    font-size: 0.75rem;
    font-weight: 600;
    color: var(--color-text-muted);
    text-transform: uppercase;
    letter-spacing: 0.03em;
}

.baseline-grid .baseline-item input {
    padding: 0.5rem 0.6rem;
    border: 1.5px solid var(--color-border);
    border-radius: 6px;
    font-size: 0.88rem;
    background: white;
    transition: border-color var(--transition), box-shadow var(--transition);
}

.baseline-grid .baseline-item input:focus {
    outline: none;
    border-color: var(--color-primary);
    box-shadow: 0 0 0 3px rgba(37, 99, 235, 0.12);
}

.mode-switch label {
    cursor: pointer;
    font-size: 0.85rem;
    padding: 0.5rem 1rem;
    border-radius: 6px;
    transition: all var(--transition);
    font-weight: 500;
    color: var(--color-text-muted);
    user-select: none;
}

.mode-switch label:has(input:checked) {
    background: var(--color-primary);
    color: white;
    box-shadow: 0 2px 4px rgba(37, 99, 235, 0.3);
}

.mode-switch input[type="radio"] {
    display: none;
}

/* ===== Data Table ===== */
#data-table-container {
    max-height: 480px;
    overflow-y: auto;          /* this is the sticky header's scroll ancestor */
    overflow-x: auto;
    border-radius: 8px;
    border: 1px solid var(--color-border);
    background: #fff;
}

#data-table-container table {
    width: 100%;
    border-collapse: separate;
    border-spacing: 0;
    font-size: 0.85rem;
    /* No 'overflow: hidden' here — it would clip the sticky <th> and break it. */
}

#data-table-container thead th {
    background: linear-gradient(to bottom, #f8fafc, #f1f5f9);
    font-weight: 600;
    font-size: 0.78rem;
    text-transform: uppercase;
    letter-spacing: 0.03em;
    color: var(--color-text-muted);
    padding: 0.65rem 0.5rem;
    border-bottom: 2px solid var(--color-border);
    position: sticky;
    top: 0;
    z-index: 2;
    /* shadow makes the header visually float above scrolled rows */
    box-shadow: 0 1px 0 var(--color-border), 0 2px 4px rgba(15, 23, 42, 0.04);
}

#data-table-container td {
    padding: 0;
    border-bottom: 1px solid #f1f5f9;
    text-align: center;
}

#data-table-container tr:nth-child(even) td {
    background: #fafbfc;
}

#data-table-container tr:hover td {
    background: var(--color-primary-light);
}

#data-table-container input {
    width: 100%;
    border: none;
    text-align: center;
    padding: 0.55rem 0.4rem;
    font-size: 0.85rem;
    background: transparent;
    transition: background var(--transition);
}

#data-table-container input:focus {
    outline: none;
    background: #dbeafe;
}

/* Computed/read-only column (理论 iNAV). Class name kept for backward compat
 * with existing JS / templates; semantically this now flags the theoretical
 * iNAV column. */
#data-table-container input.shadow-cell {
    background: #fef3c7;
    color: #92400e;
    font-weight: 500;
}

/* "Main board closed" placeholder cell — KP after 14:30 */
#data-table-container input.closed-cell {
    background: #f1f5f9;
    color: #94a3b8;
    font-style: italic;
    font-size: 0.78rem;
    cursor: not-allowed;
}

.table-actions {
    margin-top: 1rem;
    display: flex;
    gap: 0.5rem;
}

/* ===== Params Grid ===== */
.params-grid {
    display: grid;
    grid-template-columns: repeat(auto-fit, minmax(200px, 1fr));
    gap: 1.25rem;
}

.param-item {
    position: relative;
}

.param-item label {
    display: block;
    font-weight: 600;
    font-size: 0.82rem;
    margin-bottom: 0.35rem;
    color: var(--color-text);
}

.param-item input {
    width: 100%;
    padding: 0.6rem 0.75rem;
    border: 1.5px solid var(--color-border);
    border-radius: 8px;
    font-size: 0.9rem;
    transition: border-color var(--transition), box-shadow var(--transition);
    background: #fafbfc;
}

.param-item input:focus {
    outline: none;
    border-color: var(--color-primary);
    box-shadow: 0 0 0 3px rgba(37, 99, 235, 0.12);
    background: white;
}

.param-item .hint {
    display: block;
    font-size: 0.72rem;
    color: var(--color-text-muted);
    margin-top: 0.3rem;
}

/* Checkbox-style param item: keep label inline-with-input and skip the
 * 1.5px-bordered text-input look. */
.param-item-check .check-label {
    display: flex;
    align-items: center;
    gap: 0.45rem;
    margin-bottom: 0.35rem;
    cursor: pointer;
    user-select: none;
    font-weight: 600;
    font-size: 0.82rem;
    color: var(--color-text);
}
.param-item-check input[type="checkbox"] {
    width: 16px;
    height: 16px;
    margin: 0;
    padding: 0;
    accent-color: var(--color-primary);
    cursor: pointer;
}

/* ===== Buttons ===== */
.btn {
    padding: 0.55rem 1.1rem;
    border: none;
    border-radius: 8px;
    cursor: pointer;
    font-size: 0.83rem;
    font-weight: 600;
    transition: all var(--transition);
    display: inline-flex;
    align-items: center;
    gap: 0.3rem;
}

.btn:active {
    transform: scale(0.97);
}

.btn-primary {
    background: linear-gradient(135deg, var(--color-primary), var(--color-primary-dark));
    color: white;
    box-shadow: 0 2px 8px rgba(37, 99, 235, 0.3);
}

.btn-primary:hover {
    box-shadow: 0 4px 14px rgba(37, 99, 235, 0.4);
    transform: translateY(-1px);
}

.btn-secondary {
    background: white;
    color: var(--color-text);
    border: 1.5px solid var(--color-border);
}

.btn-secondary:hover {
    background: #f8fafc;
    border-color: var(--color-primary);
    color: var(--color-primary);
}

.btn-danger {
    background: white;
    color: var(--color-danger);
    border: 1.5px solid #fecaca;
}

.btn-danger:hover {
    background: #fef2f2;
    border-color: var(--color-danger);
}

.btn-large {
    padding: 0.85rem 2.5rem;
    font-size: 1rem;
    border-radius: 10px;
}

.run-section {
    margin-top: 1.5rem;
    padding-top: 1.25rem;
    border-top: 1px solid var(--color-border);
    text-align: right;
}

/* ===== Stats Grid ===== */
.stats-grid {
    display: grid;
    grid-template-columns: repeat(auto-fit, minmax(120px, 1fr));
    gap: 0.6rem;
    margin-bottom: 0;
}

/* Variant: enforce exactly 2 / 1 column for "single-swap features" and
 * "direction distribution" groups so the cards don't collapse to single
 * columns on wide layouts (looks more deliberate than auto-fit). */
.stats-grid.stats-grid-2 {
    grid-template-columns: repeat(2, minmax(120px, 1fr));
}
.stats-grid.stats-grid-1 {
    grid-template-columns: 1fr;
}
/* Variant: 3-column row used to combine "单笔特征" (2 cards) and
 * "方向分布" (1 wide card) into a single horizontally compact section.
 * On narrow viewports falls back to auto-fit so cards wrap gracefully. */
.stats-grid.stats-grid-3 {
    grid-template-columns: repeat(3, minmax(160px, 1fr));
}
@media (max-width: 720px) {
    .stats-grid.stats-grid-3 {
        grid-template-columns: repeat(auto-fit, minmax(140px, 1fr));
    }
}

/* Section wrapper for the 3 logical groups inside #stats-panel.
 * Each section gets a thin uppercase header above its grid. */
.stats-panel {
    margin-bottom: 1.5rem;
}
.stats-section + .stats-section {
    margin-top: 0.85rem;
}
.stats-section-title {
    margin: 0 0 0.4rem;
    font-size: 0.65rem;
    font-weight: 600;
    color: var(--color-text-muted);
    text-transform: uppercase;
    letter-spacing: 0.08em;
    border-bottom: 1px solid var(--color-border);
    padding-bottom: 0.25rem;
}

/* Wide single-card variant used for "direction distribution". The two-leg
 * split inside is rendered with .dir-split. */
.stat-card-wide {
    text-align: center;
}
.dir-split {
    display: flex;
    align-items: center;
    justify-content: center;
    gap: 1rem;
    margin-bottom: 0.25rem;
}
.dir-leg {
    display: flex;
    flex-direction: column;
    align-items: center;
    gap: 0.05rem;
}
.dir-label {
    font-size: 0.62rem;
    color: var(--color-text-muted);
    font-weight: 500;
}
.dir-value {
    font-size: 1.1rem;
    font-weight: 700;
    letter-spacing: -0.01em;
    line-height: 1.2;
}
.dir-value.sell { color: var(--color-danger); }
.dir-value.buy  { color: var(--color-primary); }
.dir-divider {
    width: 1px;
    height: 1.6rem;
    background: var(--color-border);
}

/* Warning banner shown when threshold ≤ swapCost (degenerate config). */
.stats-warning {
    background: rgba(220, 38, 38, 0.08);
    border: 1px solid rgba(220, 38, 38, 0.3);
    border-left: 3px solid var(--color-danger);
    color: #991b1b;
    padding: 0.45rem 0.7rem;
    border-radius: 5px;
    font-size: 0.73rem;
    margin-bottom: 0.7rem;
    line-height: 1.45;
}

/* Info banner — neutral grey-blue, explains current trading-window scope. */
.stats-info {
    background: rgba(37, 99, 235, 0.05);
    border: 1px solid rgba(37, 99, 235, 0.2);
    border-left: 3px solid var(--color-primary);
    color: #1e3a5f;
    padding: 0.4rem 0.7rem;
    border-radius: 5px;
    font-size: 0.72rem;
    margin-bottom: 0.7rem;
    line-height: 1.4;
}

.stat-card {
    text-align: center;
    padding: 0.6rem 0.7rem 0.55rem;
    border-radius: 8px;
    background: #fafbfc;
    border-top: 2px solid var(--color-primary);
    box-shadow: var(--shadow-sm);
    transition: transform var(--transition), box-shadow var(--transition);
}

.stat-card:hover {
    transform: translateY(-1px);
    box-shadow: var(--shadow-md);
}

.stat-card .value {
    font-size: 1.05rem;
    font-weight: 700;
    color: var(--color-primary-dark);
    letter-spacing: -0.01em;
    line-height: 1.2;
}

.stat-card .label {
    font-size: 0.65rem;
    color: var(--color-text-muted);
    font-weight: 500;
    margin-top: 0.1rem;
    text-transform: uppercase;
    letter-spacing: 0.04em;
}

.stat-card .stat-hint {
    font-size: 0.62rem;
    color: var(--color-text-muted);
    margin-top: 0.25rem;
    line-height: 1.35;
    opacity: 0.75;
}

/* ===== Charts ===== */
.charts-container {
    display: grid;
    grid-template-columns: 1fr 1fr;
    gap: 0.85rem;
    margin-bottom: 1.5rem;
}

.chart-wrapper {
    position: relative;            /* required for maintainAspectRatio: false */
    border: 1px solid var(--color-border);
    border-radius: 8px;
    padding: 0.75rem 0.85rem 0.6rem;
    background: #fafbfc;
}

/* The first chart (累计收益曲线) spans the full width. With Chart.js's
 * default 2:1 aspect ratio it would be roughly twice as tall as its
 * half-width siblings, which crushes the rest of the backtest-results page.
 *
 * We disable maintainAspectRatio in JS and constrain the wrapper height
 * here, so the cumulative-return chart becomes wide-but-short while the
 * other two charts stay at a comfortable size. */
#backtest-results .charts-container > .chart-wrapper:first-child {
    grid-column: 1 / -1;
    height: 200px;
}
#backtest-results .charts-container > .chart-wrapper:not(:first-child) {
    height: 260px;
}
/* The heatmap canvas isn't driven by Chart.js, so it needs explicit CSS
 * sizing to fill its wrapper. Other Chart.js canvases work because the
 * library sizes them itself when maintainAspectRatio: false. */
#pnl-chart {
    width: 100% !important;
    height: 100% !important;
    display: block;
}

/* Heatmap floating tooltip (created lazily by renderTriggerHeatmap). */
.heatmap-tooltip {
    position: fixed;
    z-index: 1000;
    background: rgba(15, 23, 42, 0.92);
    color: #f8fafc;
    padding: 0.4rem 0.6rem;
    border-radius: 6px;
    font-size: 0.72rem;
    line-height: 1.4;
    pointer-events: none;
    display: none;
    max-width: 240px;
    box-shadow: 0 4px 12px rgba(0, 0, 0, 0.18);
}
.heatmap-tooltip-head {
    font-weight: 600;
    margin-bottom: 0.15rem;
    color: #fef3c7;
}
.heatmap-tooltip-swap {
    margin-top: 0.2rem;
    font-weight: 600;
    color: #fbbf24;
}

/* Same treatment for the dashboard's two full-width charts (偏离走势 +
 * 官方 iNAV vs 理论 iNAV). Without an explicit height they'd render at
 * their natural 2:1 aspect ratio (~600px tall on a 1200px viewport),
 * which dwarfs the indicator cards and forces a lot of scrolling. */
#dashboard > .chart-wrapper {
    height: 240px;
}

/* In-chart toggle button (e.g. zoom mode for the Theo vs Published chart). */
.zoom-toggle {
    position: absolute;
    top: 0.6rem;
    right: 0.8rem;
    z-index: 5;
    font-size: 0.72rem;
    font-weight: 500;
    padding: 0.25rem 0.6rem;
    background: var(--color-card);
    color: var(--color-text-muted);
    border: 1px solid var(--color-border);
    border-radius: 6px;
    cursor: pointer;
    transition: all var(--transition);
}
.zoom-toggle:hover {
    color: var(--color-primary);
    border-color: var(--color-primary);
}
.zoom-toggle[aria-pressed="true"] {
    color: #fff;
    background: var(--color-primary);
    border-color: var(--color-primary);
}

/* ===== Trade Log ===== */
/* Inline help popover ("规则说明") shown next to a section title.
 * Native <details>/<summary> for accessibility; positioned absolute so
 * the panel doesn't reflow the table below it when expanded. */
.rules-popover {
    position: relative;
    font-size: 0.72rem;
}
.rules-popover > summary {
    list-style: none;
    cursor: pointer;
    color: var(--color-primary);
    padding: 0.15rem 0.5rem;
    border: 1px solid #cbd5e1;
    border-radius: 999px;
    background: #f8fafc;
    user-select: none;
    transition: background var(--transition), border-color var(--transition);
}
.rules-popover > summary::-webkit-details-marker { display: none; }
.rules-popover > summary::before {
    content: 'ⓘ ';
    margin-right: 2px;
}
.rules-popover[open] > summary,
.rules-popover > summary:hover {
    background: var(--color-primary-light);
    border-color: var(--color-primary);
}
.rules-popover-body {
    position: absolute;
    top: calc(100% + 6px);
    left: 0;
    z-index: 100;
    width: min(560px, 90vw);
    background: #ffffff;
    border: 1px solid var(--color-border);
    border-radius: 10px;
    box-shadow: 0 8px 24px rgba(15, 23, 42, 0.12);
    padding: 0.85rem 1.1rem 1rem;
    font-size: 0.78rem;
    line-height: 1.55;
    color: var(--color-text);
}
.rules-popover-body h4 {
    margin: 0.7rem 0 0.25rem;
    font-size: 0.78rem;
    font-weight: 700;
    color: var(--color-primary-dark);
}
.rules-popover-body h4:first-child { margin-top: 0; }
.rules-popover-body p { margin: 0.2rem 0; }
.rules-popover-body ul {
    margin: 0.2rem 0 0.2rem 1.1rem;
    padding: 0;
}
.rules-popover-body li { margin: 0.1rem 0; }
.rules-popover-body code {
    background: #f1f5f9;
    padding: 0.05rem 0.3rem;
    border-radius: 3px;
    font-size: 0.72rem;
    color: #be185d;
}

/* Shared compact-table style applied to the 3 result tables in the
 * backtest section: trade log, by-date summary, and data-quality
 * diagnostic. Single rule keeps them visually consistent. */
#trade-log-container,
#by-date-container,
#data-quality-container {
    margin-top: 0.5rem;
}

#trade-log-container table,
#by-date-container table,
#data-quality-container table {
    width: 100%;
    border-collapse: separate;
    border-spacing: 0;
    font-size: 0.8rem;
    border-radius: 8px;
    overflow: hidden;
    border: 1px solid var(--color-border);
}

#trade-log-container th,
#by-date-container th,
#data-quality-container th {
    background: linear-gradient(to bottom, #f8fafc, #f1f5f9);
    padding: 0.6rem 0.6rem;
    text-align: center;
    font-weight: 600;
    font-size: 0.72rem;
    text-transform: uppercase;
    letter-spacing: 0.03em;
    color: var(--color-text-muted);
    border-bottom: 2px solid var(--color-border);
}

#trade-log-container td,
#by-date-container td,
#data-quality-container td {
    padding: 0.5rem 0.6rem;
    text-align: center;
    border-bottom: 1px solid #f1f5f9;
}

#trade-log-container tr:nth-child(even) td,
#by-date-container tr:nth-child(even) td,
#data-quality-container tr:nth-child(even) td {
    background: #fafbfc;
}

#trade-log-container tr:hover td,
#by-date-container tr:hover td,
#data-quality-container tr:hover td {
    background: var(--color-primary-light);
}

/* ===== Traffic Light ===== */
#traffic-light {
    text-align: center;
    margin-bottom: 1.5rem;
    padding: 1rem;
}

.light {
    display: inline-block;
    width: 56px;
    height: 56px;
    border-radius: 50%;
    margin: 0 0.75rem;
    opacity: 0.15;
    border: 3px solid rgba(0,0,0,0.05);
    transition: all 0.4s ease;
}

.light.active {
    opacity: 1;
    transform: scale(1.15);
    box-shadow: 0 0 20px currentColor, 0 0 40px currentColor;
    animation: pulse 2s ease-in-out infinite;
}

@keyframes pulse {
    0%, 100% { box-shadow: 0 0 20px currentColor, 0 0 40px currentColor; }
    50% { box-shadow: 0 0 10px currentColor, 0 0 20px currentColor; }
}

.light.red { background: var(--color-danger); color: var(--color-danger); }
.light.yellow { background: var(--color-warning); color: var(--color-warning); }
.light.green { background: var(--color-success); color: var(--color-success); }

/* ===== Conclusion Text ===== */
#conclusion-text {
    padding: 1.5rem;
    border: none;
    border-radius: var(--radius);
    background: #f8fafc;
    line-height: 1.8;
    box-shadow: inset 0 2px 4px rgba(0,0,0,0.03);
}

#conclusion-text h3 {
    font-size: 0.9rem;
    font-weight: 700;
    color: var(--color-text);
    margin-top: 1rem;
    margin-bottom: 0.4rem;
    padding-bottom: 0.3rem;
    border-bottom: 1px solid var(--color-border);
}

#conclusion-text h3:first-child {
    margin-top: 0;
}

#conclusion-text ul {
    padding-left: 1.25rem;
    margin: 0.5rem 0;
}

#conclusion-text li {
    font-size: 0.85rem;
    margin-bottom: 0.3rem;
    color: var(--color-text-muted);
}

#conclusion-text p {
    font-size: 0.88rem;
}

.conclusion-metrics-note {
    background: var(--color-bg);
    border-radius: 8px;
    padding: 0.75rem 1rem;
    margin-top: 0.5rem;
}

.conclusion-metrics-note p {
    margin-bottom: 0.3rem;
    font-size: 0.82rem;
}

/* ===== Dashboard ===== */
.dashboard-stats {
    margin-top: 1rem;
}

.dashboard-split {
    display: grid;
    grid-template-columns: 1fr 1fr;
    gap: 1rem;
}

.dashboard-period {
    padding: 1rem;
    border: 1px solid var(--color-border);
    border-radius: 8px;
    background: #fafbfc;
}

.dashboard-period h4 {
    font-size: 0.8rem;
    font-weight: 600;
    color: var(--color-text-muted);
    margin-bottom: 0.6rem;
}

.mini-stats {
    display: grid;
    grid-template-columns: 1fr 1fr;
    gap: 0.5rem;
}

.mini-stat {
    text-align: center;
}

.mini-stat .val {
    font-size: 1.1rem;
    font-weight: 700;
    color: var(--color-text);
}

.mini-stat .lbl {
    font-size: 0.68rem;
    color: var(--color-text-muted);
}

/* ===== Appendix (Bloomberg Guide) ===== */
.appendix {
    margin-top: 3rem;
    padding: 1.5rem;
    border-top: 1px dashed var(--color-border);
    background: none;
}

.appendix h2 {
    font-size: 0.9rem;
    font-weight: 600;
    color: var(--color-text-muted);
    margin-bottom: 1.25rem;
    text-transform: uppercase;
    letter-spacing: 0.03em;
}

.bbg-section {
    margin-bottom: 1.5rem;
    padding-bottom: 1.25rem;
    border-bottom: 1px solid var(--color-border);
}

.bbg-section:last-child {
    margin-bottom: 0;
    padding-bottom: 0;
    border-bottom: none;
}

.bbg-section h3 {
    font-size: 0.9rem;
    font-weight: 600;
    margin-bottom: 0.6rem;
    color: var(--color-text);
}

.bbg-cmd {
    margin-top: 0.5rem;
    font-size: 0.82rem;
    color: var(--color-text-muted);
}

.bbg-cmd code {
    background: #e8f0fe;
    color: var(--color-primary-dark);
    padding: 0.2rem 0.5rem;
    border-radius: 4px;
    font-size: 0.8rem;
}

.bbg-note {
    margin-top: 0.5rem;
    font-size: 0.8rem;
    color: var(--color-text-muted);
    line-height: 1.5;
}

.bbg-code {
    margin-top: 0.5rem;
    padding: 0.6rem 0.8rem;
    background: #1e293b;
    color: #93c5fd;
    border-radius: 6px;
    font-size: 0.78rem;
    font-family: 'SF Mono', 'Menlo', monospace;
    overflow-x: auto;
    white-space: pre-wrap;
}

.bbg-steps {
    margin-top: 0.5rem;
    padding-left: 1.25rem;
    font-size: 0.82rem;
    color: var(--color-text);
    line-height: 1.8;
}

.bbg-steps li {
    margin-bottom: 0.3rem;
}

.bbg-steps code {
    background: #e8f0fe;
    color: var(--color-primary-dark);
    padding: 0.15rem 0.4rem;
    border-radius: 4px;
    font-size: 0.76rem;
    word-break: break-all;
}

/* ===== Responsive ===== */
@media (max-width: 768px) {
    .sidebar { display: none; }
    .main-content { margin-left: 0; padding: 1.5rem 1rem; }
    header h1 { font-size: 1.4rem; }
    .guide-grid { grid-template-columns: 1fr; }
    .charts-container { grid-template-columns: 1fr; }
    .params-grid { grid-template-columns: 1fr; }
}

/* ===== Section role tags & subtitles ===== */
.section-tag {
    display: inline-block;
    font-size: 0.7rem;
    font-weight: 600;
    padding: 0.15rem 0.55rem;
    border-radius: 999px;
    margin-left: 0.5rem;
    vertical-align: middle;
    letter-spacing: 0.04em;
}

.section-tag.tag-live {
    background: rgba(22, 163, 74, 0.12);
    color: var(--color-success);
    border: 1px solid rgba(22, 163, 74, 0.3);
}

.section-tag.tag-history {
    background: rgba(202, 138, 4, 0.12);
    color: var(--color-warning);
    border: 1px solid rgba(202, 138, 4, 0.3);
}

.section-subtitle {
    font-size: 0.85rem;
    color: var(--color-text-muted);
    margin: 0.35rem 0 1rem 0;
    line-height: 1.55;
}

.section-subtitle strong {
    color: var(--color-primary-dark);
    font-weight: 600;
}

/* ===== Baseline / data-convention notice ===== */
.baseline-notice {
    font-size: 0.85rem;
    line-height: 1.7;
    color: #334155;
    background: #f8fafc;
    border: 1px solid var(--color-border);
    border-left: 3px solid var(--color-primary);
    border-radius: 6px;
    padding: 0.7rem 0.95rem;
}

.baseline-notice-row {
    margin-bottom: 0.3rem;
}

.baseline-notice strong {
    color: var(--color-primary-dark);
}

.baseline-notice code {
    background: rgba(37, 99, 235, 0.08);
    color: var(--color-primary-dark);
    padding: 0.05rem 0.3rem;
    border-radius: 3px;
    font-size: 0.78rem;
}

.baseline-notice-list {
    margin: 0.4rem 0 0 0;
    padding-left: 1.1rem;
}

.baseline-notice-list li {
    margin-bottom: 0.2rem;
}

.tag-truth, .tag-shadow, .tag-skip {
    display: inline-block;
    font-size: 0.7rem;
    font-weight: 600;
    padding: 0.05rem 0.4rem;
    border-radius: 3px;
    margin-right: 0.35rem;
    vertical-align: 1px;
    letter-spacing: 0.02em;
}
.tag-truth  { background: rgba(22, 163, 74, 0.12);  color: var(--color-success); border: 1px solid rgba(22, 163, 74, 0.3); }
.tag-shadow { background: rgba(202, 138, 4, 0.12);  color: var(--color-warning); border: 1px solid rgba(202, 138, 4, 0.3); }
.tag-skip   { background: rgba(100, 116, 139, 0.12); color: var(--color-text-muted); border: 1px solid rgba(100, 116, 139, 0.3); }

/* Bias label (premium / discount / flat) — used in the dashboard indicator
 * card and the indicator-guide section. */
.bias-tag {
    display: inline-block;
    font-size: 0.65rem;
    font-weight: 700;
    letter-spacing: 0.04em;
    padding: 0.05rem 0.35rem;
    border-radius: 3px;
    margin-left: 0.35rem;
    vertical-align: middle;
}
.bias-premium  { background: rgba(220, 38, 38, 0.12); color: var(--color-danger);  border: 1px solid rgba(220, 38, 38, 0.3); }
.bias-discount { background: rgba(37, 99, 235, 0.12); color: var(--color-primary); border: 1px solid rgba(37, 99, 235, 0.3); }
.bias-flat     { background: rgba(100, 116, 139, 0.12); color: var(--color-text-muted); border: 1px solid rgba(100, 116, 139, 0.3); }

/* Reference-side tag for trade log (Last/Bid/Ask). */
.ref-tag {
    display: inline-block;
    font-size: 0.62rem;
    font-weight: 600;
    padding: 0.02rem 0.3rem;
    border-radius: 3px;
    margin-left: 0.35rem;
    vertical-align: middle;
}
.ref-bid  { background: rgba(220, 38, 38, 0.10); color: var(--color-danger);  border: 1px solid rgba(220, 38, 38, 0.25); }
.ref-ask  { background: rgba(37, 99, 235, 0.10); color: var(--color-primary); border: 1px solid rgba(37, 99, 235, 0.25); }
.ref-last { background: rgba(100, 116, 139, 0.10); color: var(--color-text-muted); border: 1px solid rgba(100, 116, 139, 0.25); }



/* ===== Changelog ===== */
.changelog-list {
    padding: 0.5rem 0 0.25rem;
}

.changelog-day {
    display: grid;
    grid-template-columns: 90px 1fr;
    gap: 0.75rem;
    padding: 0.6rem 0;
    border-bottom: 1px dashed var(--color-border);
}
.changelog-day:last-child { border-bottom: none; }

.changelog-date {
    font-variant-numeric: tabular-nums;
    color: var(--color-text-muted);
    font-size: 0.78rem;
    font-weight: 600;
    padding-top: 0.2rem;
    letter-spacing: 0.02em;
}

.changelog-items {
    list-style: none;
    margin: 0;
    padding: 0;
}
.changelog-items li {
    padding: 0.2rem 0;
    font-size: 0.86rem;
    color: var(--color-text);
    line-height: 1.5;
    display: flex;
    justify-content: space-between;
    gap: 0.75rem;
    align-items: baseline;
}
.changelog-items li + li {
    border-top: 1px solid #f1f5f9;
}

.changelog-text { flex: 1; }

.changelog-hash {
    font-family: ui-monospace, SFMono-Regular, "SF Mono", Menlo, Consolas, monospace;
    font-size: 0.72rem;
    color: var(--color-text-muted);
    background: #f1f5f9;
    padding: 0.05rem 0.4rem;
    border-radius: 4px;
    flex-shrink: 0;
}
