:root {
    color-scheme: light;
    --bg: #fff3e0;
    --bg-deep: #fff3e0;
    --panel: #fff3e0;
    --panel-soft: rgba(17, 13, 0, 0.025);
    --panel-raised: rgba(17, 13, 0, 0.045);
    --select-bg: #fff8ec;
    --ink: #110d00;
    --ink-strong: #110d00;
    --muted: #735f2a;
    --line: rgba(17, 13, 0, 0.24);
    --line-strong: #110d00;
    --accent: #110d00;
    --accent-strong: #110d00;
    --good: #26734d;
    --bad: #b63d32;
    --hold: #2869a6;
    --warn: #9d6500;
}

:root[data-theme="dark"] {
    color-scheme: dark;
    --bg: #110d00;
    --bg-deep: #110d00;
    --panel: #110d00;
    --panel-soft: rgba(255, 243, 224, 0.025);
    --panel-raised: rgba(255, 243, 224, 0.06);
    --select-bg: #211c10;
    --ink: #fff3e0;
    --ink-strong: #fff3e0;
    --muted: #d6c596;
    --line: rgba(255, 243, 224, 0.28);
    --line-strong: #fff3e0;
    --accent: #fff3e0;
    --accent-strong: #fff3e0;
    --good: #8ed6aa;
    --bad: #ffa398;
    --hold: #8abde8;
    --warn: #e6b85c;
}

* {
    box-sizing: border-box;
}

body {
    margin: 0;
    background: var(--bg);
    color: var(--ink);
    font-family: Inter, "Segoe UI", Tahoma, Verdana, sans-serif;
}

a {
    color: var(--accent-strong);
    text-decoration: none;
}

button,
select,
input {
    color: var(--ink);
    font: inherit;
}

.topbar {
    display: grid;
    grid-template-columns: minmax(220px, 1fr) minmax(280px, 1.5fr) auto;
    align-items: center;
    justify-content: space-between;
    gap: 18px;
    border-bottom: 2px solid var(--line-strong);
    background: var(--bg-deep);
    padding: 18px clamp(16px, 4vw, 34px);
}

.topbar h1,
.panel h2 {
    margin: 0;
    color: var(--ink-strong);
    letter-spacing: 0;
}

.topbar h1 {
    font-size: 32px;
    line-height: 1.1;
}

.backtest-intro {
    max-width: 680px;
    margin: 0;
    font-size: 14px;
    line-height: 1.6;
    color: var(--muted);
}

.eyebrow {
    margin: 0 0 6px;
    color: var(--accent);
    font-size: 12px;
    font-weight: 800;
    letter-spacing: 0;
    text-transform: uppercase;
}

.topnav,
.tab-list {
    display: flex;
    align-items: center;
    gap: 8px;
}

.tab-button {
    min-height: 38px;
    border: 1px solid var(--line);
    border-radius: 6px;
    background: var(--panel-raised);
    padding: 9px 12px;
    color: var(--ink);
    font-size: 14px;
    font-weight: 750;
}

.tab-button {
    cursor: pointer;
}

.tab-button.active {
    border-color: var(--ink);
    background: var(--ink);
    color: var(--bg);
}

.tab-button:hover,
select:hover {
    border-color: var(--line-strong);
    color: var(--ink-strong);
}

.tab-button.active:hover {
    color: var(--bg);
}

.theme-toggle,
.nav-icon {
    display: inline-grid;
    place-items: center;
    width: 48px;
    height: 48px;
    flex: 0 0 48px;
    border: 2px solid var(--line-strong);
    border-radius: 50%;
    background: var(--bg);
    cursor: pointer;
}

.theme-toggle:hover,
.nav-icon:hover {
    background: var(--ink);
    color: var(--bg);
}

.theme-icon {
    width: 24px;
    height: 24px;
    fill: none;
    stroke: currentColor;
    stroke-width: 2;
    stroke-linecap: round;
    stroke-linejoin: round;
}

.theme-icon-sun,
:root[data-theme="dark"] .theme-icon-moon {
    display: none;
}

:root[data-theme="dark"] .theme-icon-sun {
    display: block;
}

.shell {
    width: min(100% - 28px, 1380px);
    margin: 18px auto 36px;
}

.metric-grid {
    display: grid;
    gap: 12px;
}

.chart-controls {
    display: flex;
    align-items: end;
    gap: 16px;
    flex: 0 1 480px;
    min-width: 0;
}

.chart-controls > select {
    flex: 1 1 240px;
    min-width: 0;
}

.cost-control {
    display: grid;
    gap: 5px;
    flex: 0 0 168px;
    min-width: 0;
}

.cost-help {
    position: relative;
}

.cost-label {
    border: 0;
    padding: 2px 0;
    background: transparent;
    color: var(--muted);
    font-size: 12px;
    font-weight: 800;
    text-transform: uppercase;
    cursor: help;
    text-decoration: underline dotted;
    text-underline-offset: 4px;
    text-align: left;
}

.cost-help .legend-tooltip {
    left: auto;
    right: 0;
    width: min(340px, calc(100vw - 60px));
}

.results-summary {
    display: block;
    margin-top: 6px;
    color: var(--muted);
    font-size: 12px;
    line-height: 1.5;
}

select,
input {
    min-height: 42px;
    width: 100%;
    border: 1px solid var(--line);
    border-radius: 6px;
    background: var(--panel-raised);
    padding: 8px 10px;
}

select,
select option,
select optgroup {
    background-color: var(--select-bg);
    color: var(--ink);
}

.metric-grid {
    grid-template-columns: repeat(4, minmax(0, 1fr));
    margin-bottom: 12px;
}

.metric,
.panel {
    border: 1px solid var(--line);
    border-radius: 8px;
    background: var(--panel);
}

.metric {
    min-height: 116px;
    padding: 16px;
}

.metric span,
.metric small {
    display: block;
    color: var(--muted);
}

.metric span {
    font-size: 12px;
    font-weight: 800;
    text-transform: uppercase;
}

.metric strong {
    display: block;
    margin: 10px 0 5px;
    color: var(--ink-strong);
    font-size: 30px;
    line-height: 1;
}

.metric small {
    font-size: 13px;
}

.panel {
    margin-bottom: 12px;
    border: 0;
    border-radius: 0;
    border-top: 1px solid var(--line-strong);
}

.panel-head {
    display: flex;
    align-items: center;
    justify-content: space-between;
    gap: 16px;
    border-bottom: 1px solid var(--line);
    padding: 14px 16px;
}

.panel h2 {
    font-size: 18px;
}

.chart-panel canvas {
    display: block;
    width: 100%;
    height: clamp(310px, 45vh, 460px);
    background: var(--bg);
}

.chart-legend {
    display: flex;
    flex-wrap: wrap;
    position: relative;
    gap: 4px 24px;
    padding: 10px 16px 4px;
}

.legend-key {
    display: inline-flex;
    align-items: center;
    gap: 8px;
    min-height: 36px;
    padding: 4px 0;
    border: 0;
    background: transparent;
    color: var(--ink);
    font-size: 13px;
    cursor: help;
}

.legend-key:hover {
    text-decoration: underline dotted;
    text-underline-offset: 4px;
}

button:focus-visible,
a:focus-visible,
select:focus-visible,
input:focus-visible {
    outline: 2px solid var(--ink);
    outline-offset: 3px;
}

.legend-swatch {
    width: 22px;
    flex: 0 0 22px;
    border-top: 3px solid var(--good);
}

.chart-legend[data-loss="true"] .strategy-swatch {
    border-color: var(--bad);
}

.hold-swatch {
    border-color: var(--hold);
}

.price-swatch {
    border-color: var(--warn);
    border-top-style: dashed;
}

.legend-tooltip {
    position: absolute;
    z-index: 5;
    top: 100%;
    left: 16px;
    width: min(380px, calc(100% - 32px));
    padding: 12px 14px;
    border: 1px solid var(--line-strong);
    border-radius: 6px;
    background: var(--ink);
    color: var(--bg);
    font-size: 13px;
    line-height: 1.5;
    visibility: hidden;
    pointer-events: none;
}

.legend-item:not(.tooltip-dismissed):hover .legend-tooltip,
.legend-item:not(.tooltip-dismissed):focus-within .legend-tooltip,
.cost-help:not(.tooltip-dismissed):hover .legend-tooltip,
.cost-help:not(.tooltip-dismissed):focus-within .legend-tooltip {
    visibility: visible;
    pointer-events: auto;
}

.view-panel {
    display: none;
}

.view-panel.active {
    display: block;
}

.symbol-chip-grid {
    display: grid;
    grid-template-columns: repeat(auto-fit, minmax(190px, 1fr));
    gap: 8px;
    padding: 12px;
}

.symbol-chip {
    min-height: 78px;
    border-bottom: 1px solid var(--line);
    padding: 10px;
}

.symbol-chip strong,
.symbol-chip span {
    display: block;
}

.symbol-chip strong {
    color: var(--ink-strong);
    font-size: 15px;
}

.symbol-chip span {
    margin-top: 5px;
    color: var(--muted);
    font-size: 12px;
    font-weight: 750;
}

.pill {
    display: inline-flex;
    align-items: center;
    min-height: 28px;
    border: 1px solid var(--line);
    border-radius: 999px;
    background: var(--panel-soft);
    padding: 5px 10px;
    color: var(--ink);
    font-size: 12px;
    font-weight: 800;
}

.table-wrap {
    overflow-x: auto;
}

table {
    width: 100%;
    min-width: 820px;
    border-collapse: collapse;
}

th,
td {
    border-bottom: 1px solid var(--line);
    padding: 11px 12px;
    text-align: left;
    white-space: nowrap;
}

th {
    color: var(--muted);
    font-size: 12px;
    font-weight: 850;
    text-transform: uppercase;
}

td {
    color: var(--ink);
    font-size: 14px;
}

tbody tr:hover {
    background: var(--panel-raised);
}

.num {
    text-align: right;
}

.positive {
    color: var(--good);
}

.negative {
    color: var(--bad);
}

.load-error {
    border-bottom: 1px solid var(--bad);
    background: var(--bg);
    color: var(--bad);
    padding: 12px 16px;
    font-weight: 800;
}

@media (max-width: 980px) {
    .topbar {
        display: grid;
        grid-template-columns: 1fr;
    }

    .metric-grid {
        grid-template-columns: repeat(2, minmax(0, 1fr));
    }

    .panel-head {
        align-items: flex-start;
        flex-direction: column;
    }

    .chart-controls {
        flex: none;
        width: 100%;
    }
}

@media (max-width: 620px) {
    .topnav,
    .tab-list {
        flex-wrap: wrap;
    }

    .tab-button {
        flex: 1 1 auto;
        text-align: center;
    }

    .metric-grid {
        grid-template-columns: 1fr;
    }

    .chart-controls {
        gap: 12px;
        flex-wrap: wrap;
    }

    .chart-controls > select {
        flex-basis: 100%;
    }

    .cost-control {
        flex: 1 1 100%;
    }

    .cost-help .legend-tooltip {
        left: 0;
        right: auto;
        width: 100%;
    }

    .results-panel .tab-list {
        width: 100%;
    }
}
