.backtest-layout {
  display: grid;
  grid-template-columns: 320px 1fr;
  gap: var(--space-3);
  height: calc(100vh - 100px);
  align-items: stretch;
  overflow: hidden;
}

.backtest-sidebar {
  background: var(--bg-elev-1);
  border: 1px solid var(--border-subtle);
  border-radius: var(--radius-lg);
  padding: var(--space-4);
  display: flex;
  flex-direction: column;
  gap: var(--space-4);
  overflow-y: auto;
  box-sizing: border-box;
  height: 100%;
}

.backtest-sidebar h2 {
  margin: 0;
  font-family: var(--font-display);
  font-size: var(--fs-xl);
  font-weight: 700;
  letter-spacing: 0.02em;
  color: var(--fg-primary);
  padding-bottom: var(--space-2);
  border-bottom: 1px solid var(--border-subtle);
  flex-shrink: 0;
}

.backtest-sidebar h3 {
  margin: 0 0 var(--space-2) 0;
  font-family: var(--font-display);
  font-size: var(--fs-xs);
  font-weight: 600;
  letter-spacing: 0.12em;
  text-transform: uppercase;
  color: var(--fg-tertiary);
}

.backtest-form {
  display: flex;
  flex-direction: column;
  gap: var(--space-3);
  flex-shrink: 0;
}

.backtest-form .field {
  display: flex;
  flex-direction: column;
  gap: var(--space-1);
}

.backtest-form label {
  font-family: var(--font-display);
  font-size: var(--fs-xs);
  text-transform: uppercase;
  letter-spacing: 0.06em;
  color: var(--fg-tertiary);
}

.backtest-form input,
.backtest-form select {
  background: var(--bg-input);
  border: 1px solid var(--border-default);
  color: var(--fg-primary);
  padding: var(--space-2) var(--space-3);
  border-radius: var(--radius-sm);
  font-family: var(--font-body);
  font-size: var(--fs-sm);
  min-height: 32px;
}

.backtest-form input:focus,
.backtest-form select:focus {
  outline: none;
  border-color: var(--border-focus);
  box-shadow: 0 0 0 1px rgba(74, 143, 231, 0.2);
}

.backtest-form button#bt-run {
  margin-top: var(--space-2);
  padding: var(--space-3);
  background: var(--status-success);
  color: #0a0e14;
  border: 1px solid var(--status-success);
  border-radius: var(--radius-sm);
  font-family: var(--font-display);
  font-size: var(--fs-sm);
  font-weight: 700;
  letter-spacing: 0.04em;
  text-transform: uppercase;
  cursor: pointer;
  transition: all 0.15s;
}

.backtest-form button#bt-run:hover:not(:disabled) {
  filter: brightness(1.1);
  box-shadow: 0 0 12px rgba(74, 222, 128, 0.3);
}

.backtest-form button#bt-run:disabled {
  background: var(--bg-elev-3);
  color: var(--fg-disabled);
  border-color: var(--border-default);
  cursor: not-allowed;
  opacity: 0.6;
}

.backtest-history {
  flex: 0 0 auto;
  display: flex;
  flex-direction: column;
  min-height: 0;
  border-top: 1px solid var(--border-subtle);
  padding-top: var(--space-3);
}

.backtest-history-list {
  display: flex;
  flex-direction: column;
  gap: var(--space-2);
  overflow-y: auto;
  max-height: 320px;
}

.backtest-history-empty {
  color: var(--fg-tertiary);
  font-style: italic;
  font-size: var(--fs-xs);
  padding: var(--space-2);
  text-align: center;
}

.backtest-history-item {
  position: relative;
  background: var(--bg-elev-2);
  border: 1px solid var(--border-default);
  border-radius: var(--radius-md);
  padding: var(--space-2) var(--space-3);
  cursor: pointer;
  transition: all 0.15s;
  overflow: hidden;
}

.backtest-history-item::before {
  content: '';
  position: absolute;
  left: 0;
  top: 0;
  bottom: 0;
  width: 3px;
  background: var(--border-default);
  transition: background 0.15s;
}

.backtest-history-item:hover {
  background: var(--bg-elev-3);
  border-color: var(--border-strong);
}

.backtest-history-item.active {
  background: var(--bg-elev-3);
  border-color: var(--accent-cyan);
}

.backtest-history-item.active::before {
  background: var(--accent-cyan);
}

.history-head {
  display: flex;
  justify-content: space-between;
  align-items: baseline;
  margin-bottom: var(--space-1);
}

.history-title {
  font-family: var(--font-display);
  font-size: var(--fs-sm);
  font-weight: 600;
  color: var(--fg-primary);
  letter-spacing: 0.02em;
}

.history-date {
  font-family: var(--font-mono);
  font-size: 10px;
  color: var(--fg-tertiary);
}

.history-meta {
  font-family: var(--font-mono);
  font-size: var(--fs-xs);
  color: var(--fg-tertiary);
  letter-spacing: 0.02em;
}

.history-meta .positive {
  color: var(--bull);
  font-weight: 600;
}

.history-meta .negative {
  color: var(--bear);
  font-weight: 600;
}

.backtest-main {
  display: flex;
  flex-direction: column;
  gap: var(--space-3);
  overflow: hidden;
  height: 100%;
  min-height: 0;
  box-sizing: border-box;
}

.backtest-results {
  background: var(--bg-elev-1);
  border: 1px solid var(--border-subtle);
  border-radius: var(--radius-lg);
  padding: var(--space-3) var(--space-4);
  flex-shrink: 0;
}

.backtest-results h3 {
  margin: 0 0 var(--space-2) 0;
  font-family: var(--font-display);
  font-size: var(--fs-xs);
  font-weight: 600;
  letter-spacing: 0.12em;
  text-transform: uppercase;
  color: var(--fg-tertiary);
}

.backtest-metrics {
  display: grid;
  grid-template-columns: repeat(6, 1fr);
  gap: var(--space-3);
}

.backtest-metrics .metric {
  position: relative;
  background: var(--bg-elev-2);
  border: 1px solid var(--border-subtle);
  border-radius: var(--radius-md);
  padding: var(--space-3) var(--space-3) var(--space-3) var(--space-4);
  display: flex;
  flex-direction: column;
  gap: var(--space-1);
  overflow: hidden;
  min-width: 0;
}

.backtest-metrics .metric::before {
  content: '';
  position: absolute;
  left: 0;
  top: 0;
  bottom: 0;
  width: 3px;
  background: var(--accent-cyan);
}

.backtest-metrics .metric:nth-child(1)::before { background: var(--accent-cyan); }
.backtest-metrics .metric:nth-child(2)::before { background: var(--accent-blue); }
.backtest-metrics .metric:nth-child(3)::before { background: var(--accent-magenta); }
.backtest-metrics .metric:nth-child(4)::before { background: var(--bear); }
.backtest-metrics .metric:nth-child(5)::before { background: var(--status-warning); }
.backtest-metrics .metric:nth-child(6)::before { background: var(--bull); }

.metric-label {
  font-family: var(--font-display);
  font-size: var(--fs-xs);
  text-transform: uppercase;
  letter-spacing: 0.08em;
  color: var(--fg-tertiary);
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.metric-value {
  font-family: var(--font-display);
  font-size: var(--fs-xl);
  font-weight: 700;
  line-height: 1.1;
  color: var(--fg-primary);
}

.metric-value.positive {
  color: var(--bull);
}

.metric-value.negative {
  color: var(--bear);
}

.backtest-main .chart-layout {
  flex: 1;
  min-height: 0;
  background: var(--bg-elev-1);
  border: 1px solid var(--border-subtle);
  border-radius: var(--radius-lg);
  overflow: hidden;
  display: grid;
  grid-template-columns: 1fr 240px;
}

@media (max-width: 1400px) {
  .backtest-metrics {
    grid-template-columns: repeat(3, 1fr);
  }
}

@media (max-width: 1100px) {
  .backtest-layout {
    grid-template-columns: 280px 1fr;
  }
  .backtest-main .chart-layout {
    grid-template-columns: 1fr 200px;
  }
}

@media (max-width: 900px) {
  .backtest-layout {
    grid-template-columns: 1fr;
    grid-template-rows: auto 1fr;
    height: auto;
  }
  .backtest-sidebar {
    height: auto;
    max-height: 400px;
  }
  .backtest-metrics {
    grid-template-columns: repeat(2, 1fr);
  }
  .backtest-main .chart-layout {
    grid-template-columns: 1fr;
    grid-template-rows: 1fr auto;
  }
}

.backtest-backfill {
  margin-top: var(--space-4);
  padding-top: var(--space-3);
  border-top: 1px solid var(--border-subtle);
}

.backtest-backfill h3 {
  font-family: var(--font-display);
  font-size: var(--fs-xs);
  font-weight: 600;
  letter-spacing: 0.08em;
  text-transform: uppercase;
  color: var(--fg-tertiary);
  margin: 0 0 var(--space-2) 0;
}

.backfill-stats {
  background: var(--bg-elev-2);
  border: 1px solid var(--border-subtle);
  border-radius: var(--radius-sm);
  padding: var(--space-2) var(--space-3);
  font-family: var(--font-mono);
  font-size: var(--fs-sm);
  margin-bottom: var(--space-2);
}

.backfill-stats-label {
  color: var(--fg-tertiary);
  margin-right: 4px;
}

.backfill-stats-value {
  color: var(--accent-cyan);
  font-weight: 600;
}

.backfill-stats-range {
  color: var(--fg-tertiary);
  font-size: var(--fs-xs);
}

.backfill-actions {
  display: flex;
  gap: var(--space-2);
}

.backfill-actions button {
  flex: 1;
  font-size: var(--fs-xs);
  padding: 6px 10px;
}

.backfill-progress {
  margin-top: var(--space-2);
}

.backfill-progress-label {
  font-family: var(--font-mono);
  font-size: var(--fs-xs);
  color: var(--fg-secondary);
  margin-bottom: 4px;
}

.backfill-progress-bar {
  background: var(--bg-elev-3);
  border-radius: 3px;
  height: 6px;
  overflow: hidden;
}

.backfill-progress-fill {
  background: linear-gradient(90deg, var(--accent-cyan), var(--bull));
  height: 100%;
  width: 0%;
  transition: width 0.3s;
}

.backtest-history-item {
  position: relative;
}

.history-delete-btn {
  position: absolute;
  top: 6px;
  right: 6px;
  width: 22px;
  height: 22px;
  padding: 0;
  background: transparent;
  border: 1px solid transparent;
  border-radius: 4px;
  color: var(--fg-tertiary);
  cursor: pointer;
  font-size: 16px;
  line-height: 1;
  display: flex;
  align-items: center;
  justify-content: center;
  transition: all 0.15s;
}

.backtest-history-item:hover .history-delete-btn {
  border-color: var(--border-default);
}

.history-delete-btn:hover {
  background: var(--bear);
  color: white;
  border-color: var(--bear);
}

.backfill-history {
  margin-top: var(--space-3);
  padding-top: var(--space-3);
  border-top: 1px dashed var(--border-subtle);
}

.backfill-history h4 {
  font-family: var(--font-display);
  font-size: var(--fs-xs);
  font-weight: 600;
  letter-spacing: 0.08em;
  text-transform: uppercase;
  color: var(--fg-tertiary);
  margin: 0 0 var(--space-2) 0;
}

.backfill-history-list {
  display: flex;
  flex-direction: column;
  gap: 6px;
  overflow-y: auto;
  max-height: 240px;
}

.backfill-history-empty {
  color: var(--fg-tertiary);
  font-style: italic;
  font-size: var(--fs-xs);
  padding: var(--space-2);
  text-align: center;
}

.backfill-history-item {
  background: var(--bg-elev-2);
  border: 1px solid var(--border-default);
  border-radius: var(--radius-sm);
  padding: 6px 8px;
  font-size: var(--fs-xs);
}

.backfill-history-item.status-completed {
  border-left: 2px solid var(--bull);
}

.backfill-history-item.status-error {
  border-left: 2px solid var(--bear);
}

.backfill-history-item.status-running {
  border-left: 2px solid var(--accent-cyan);
}

.bf-item-head {
  display: flex;
  justify-content: space-between;
  align-items: center;
  gap: 6px;
}

.bf-item-title {
  font-family: var(--font-mono);
  font-weight: 600;
  color: var(--fg-primary);
}

.bf-item-status {
  font-family: var(--font-mono);
  font-size: 10px;
  padding: 1px 6px;
  border-radius: 3px;
  text-transform: uppercase;
  letter-spacing: 0.05em;
}

.bf-item-status.status-completed {
  background: rgba(40, 167, 69, 0.15);
  color: var(--bull);
}

.bf-item-status.status-error {
  background: rgba(220, 53, 69, 0.15);
  color: var(--bear);
}

.bf-item-status.status-running {
  background: rgba(0, 200, 255, 0.15);
  color: var(--accent-cyan);
}

.bf-item-meta {
  font-family: var(--font-mono);
  color: var(--fg-secondary);
  margin-top: 4px;
}

.bf-item-error {
  color: var(--bear);
  font-family: var(--font-mono);
  font-size: 10px;
  margin-top: 4px;
  word-break: break-all;
}

.bf-item-actions {
  display: flex;
  gap: 4px;
  margin-top: 6px;
}

.bf-delete-btn {
  flex: 1;
  padding: 3px 6px;
  background: transparent;
  border: 1px solid var(--border-default);
  border-radius: 3px;
  color: var(--fg-secondary);
  cursor: pointer;
  font-size: 10px;
  font-family: var(--font-mono);
  transition: all 0.15s;
}

.bf-delete-btn:hover {
  background: var(--bg-elev-3);
  color: var(--fg-primary);
}

.bf-delete-btn-danger:hover {
  background: var(--bear);
  color: white;
  border-color: var(--bear);
}

.range-info {
  background: rgba(255, 165, 0, 0.1);
  border: 1px solid rgba(255, 165, 0, 0.3);
  border-radius: var(--radius-sm);
  padding: 8px 10px;
  font-size: var(--fs-xs);
  color: #ffa500;
  font-family: var(--font-mono);
  margin-top: -4px;
}
