/* --- Design tokens: dark trading-terminal palette --- */
:root {
  --bg: #0b0e14;
  --bg-elevated: #111620;
  --bg-inset: #0d1117;
  --border: #232a36;
  --border-strong: #2f3947;

  --text-primary: #e6edf3;
  --text-secondary: #8b96a5;
  --text-tertiary: #5b6572;

  --accent: #3b82f6;
  --accent-hover: #5b93f5;
  --accent-soft: rgba(59, 130, 246, 0.14);

  --success: #34d399;
  --danger: #f87171;
  --warning: #f59e0b;

  --font-ui: 'Inter', system-ui, -apple-system, sans-serif;
  --font-mono: 'JetBrains Mono', ui-monospace, 'SFMono-Regular', Menlo, monospace;

  --radius-lg: 10px;
  --radius-md: 8px;
  --radius-sm: 6px;
}

html {
  scrollbar-width: thin;
  scrollbar-color: var(--border-strong) var(--bg-inset);
  scroll-behavior: smooth;
}

* {
  box-sizing: border-box;
}

body {
  font-family: var(--font-ui);
  margin: 0;
  padding: 24px 2vw;
  min-height: 100vh;
  color: var(--text-primary);
  background: var(--bg);
  display: flex;
  flex-direction: column;
  align-items: center;
}

.app-shell {
  width: 100%;
  max-width: 1600px;
  display: flex;
  flex-direction: column;
  gap: 16px;
}

canvas {
  max-width: 95vw !important;
  max-height: 100vh !important;
  width: 100% !important;
  height: 100% !important;
}

.main-content {
  max-width: 95vw !important;
  max-height: 100% !important;
  width: 100% !important;
  height: 100% !important;
}

/* --- Typography --- */
h1 {
  font-size: 1.4rem;
  margin: 0;
  line-height: 1.1;
  color: var(--text-primary);
}

h2 {
  font-size: 1.15rem;
  font-weight: 600;
  margin: 0 0 1rem 0;
  display: flex;
  align-items: center;
  gap: 10px;
  color: var(--text-primary);
}

h2 i {
  color: var(--text-tertiary);
  font-size: 0.95em;
}

h3 {
  color: var(--text-primary);
}

p {
  color: var(--text-secondary);
  font-size: 0.92rem;
  line-height: 1.5;
  margin: 0 0 1rem 0;
}

.section-heading-spaced {
  margin-top: 30px !important;
}

/* --- Top bar --- */
.topbar {
  width: 100%;
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  justify-content: space-between;
  gap: 14px 20px;
  padding: 14px 20px;
  background: var(--bg-elevated);
  border: 1px solid var(--border);
  border-radius: var(--radius-lg);
  box-sizing: border-box;
}

/* --- Validation trust badge --- */
.validation-badge {
  display: inline-flex;
  align-items: center;
  gap: 6px;
  font-size: 0.76rem;
  font-weight: 600;
  color: var(--success);
  background: rgba(52, 211, 153, 0.1);
  border: 1px solid rgba(52, 211, 153, 0.3);
  padding: 6px 12px;
  border-radius: 999px;
  text-decoration: none;
  white-space: nowrap;
  transition: background 0.15s ease, border-color 0.15s ease;
}

.validation-badge:hover {
  background: rgba(52, 211, 153, 0.18);
  border-color: var(--success);
}

.topbar-brand {
  display: flex;
  flex-direction: column;
  gap: 2px;
  min-width: 0;
}

.brand-mark {
  font-size: 1.1rem;
  font-weight: 700;
  color: var(--text-primary);
  display: flex;
  align-items: center;
  gap: 8px;
  letter-spacing: 0.2px;
}

.brand-mark i {
  color: var(--accent);
}

.brand-sub {
  font-size: 0.78rem;
  color: var(--text-tertiary);
}

.status-pill {
  display: flex;
  align-items: center;
  gap: 9px;
  font-weight: 600;
  font-size: 0.85rem;
  color: var(--text-secondary);
  background: var(--bg-inset);
  border: 1px solid var(--border);
  padding: 7px 14px;
  border-radius: 999px;
  white-space: nowrap;
}

.status-pill i {
  font-size: 0.9em;
}

.status-pill i.text-success {
  color: var(--success) !important;
}

.pulse {
  width: 8px;
  height: 8px;
  flex: none;
  background-color: var(--accent);
  border-radius: 50%;
  animation: pulse-anim 1.5s infinite;
}

@keyframes pulse-anim {
  0% {
    box-shadow: 0 0 0 0 rgba(59, 130, 246, 0.55);
  }

  70% {
    box-shadow: 0 0 0 8px rgba(59, 130, 246, 0);
  }

  100% {
    box-shadow: 0 0 0 0 rgba(59, 130, 246, 0);
  }
}

/* --- Layout Grid --- */
.dashboard-container {
  width: 100%;
  display: grid;
  grid-template-columns: 340px 1fr;
  gap: 16px;
  flex-grow: 1;
}

/* --- Panel (card) --- */
.panel {
  background: var(--bg-elevated);
  border: 1px solid var(--border);
  border-radius: var(--radius-lg);
  padding: 22px;
  position: relative;
}

/* --- Sidebar & Inputs --- */
.sidebar {
  display: flex;
  flex-direction: column;
}

.panel-intro {
  margin-bottom: 20px;
}

.ctrl-section {
  padding: 16px 0;
  border-top: 1px solid var(--border);
}

.ctrl-section:first-of-type {
  border-top: none;
  padding-top: 0;
}

.ctrl-section-dense .input-group {
  background: transparent;
  border: none;
  padding: 0;
}

.ctrl-section-title {
  font-size: 0.72rem;
  font-weight: 700;
  text-transform: uppercase;
  letter-spacing: 0.8px;
  color: var(--text-tertiary);
  display: flex;
  align-items: center;
  gap: 8px;
  margin: 0 0 12px 0;
}

.ctrl-section-title i {
  color: var(--accent);
  font-size: 0.9em;
}

.input-group {
  margin-top: 12px;
}

.input-group:first-child {
  margin-top: 0;
}

.input-group-label {
  display: block;
  font-weight: 600;
  font-size: 0.82rem;
  color: var(--text-secondary);
  margin-bottom: 8px;
}

label {
  font-weight: 600;
  color: var(--text-primary);
  display: flex;
  align-items: center;
  gap: 8px;
  margin-bottom: 10px;
}

select,
input[type="number"],
input[type="file"] {
  width: 100%;
  padding: 12px 14px;
  border-radius: var(--radius-md);
  border: 1px solid var(--border-strong);
  background: var(--bg-inset);
  font-family: var(--font-ui);
  font-size: 0.92rem;
  color: var(--text-primary);
  transition: border-color 0.15s ease, box-shadow 0.15s ease;
  box-sizing: border-box;
  cursor: pointer;
  min-height: 46px;
  /* touch target */
}

input[type="number"] {
  font-family: var(--font-mono);
}

select:focus,
input[type="number"]:focus,
input[type="file"]:focus {
  outline: none;
  border-color: var(--accent);
  box-shadow: 0 0 0 3px var(--accent-soft);
}

/* --- Tab switch (data source) --- */
.tab-switch {
  display: flex;
  gap: 4px;
  background: var(--bg-inset);
  border: 1px solid var(--border);
  border-radius: var(--radius-md);
  padding: 4px;
  margin-bottom: 12px;
}

.tab-btn {
  flex: 1;
  border: none;
  background: transparent;
  color: var(--text-secondary);
  font-family: var(--font-ui);
  font-weight: 600;
  font-size: 0.82rem;
  padding: 9px 8px;
  border-radius: var(--radius-sm);
  cursor: pointer;
  transition: background 0.15s ease, color 0.15s ease;
  min-height: 0;
  box-shadow: none;
}

.tab-btn:hover:not(.active) {
  color: var(--text-primary);
  transform: none;
  box-shadow: none;
}

.tab-btn.active {
  background: var(--bg-elevated);
  color: var(--text-primary);
  border: 1px solid var(--border-strong);
}

.tab-panel {
  animation: none;
}

/* --- Strategy Parameter Fields --- */
.param-row {
  display: flex;
  gap: 10px;
}

.param-field {
  flex: 1;
  display: flex;
  flex-direction: column;
  gap: 6px;
  min-width: 0;
}

.param-field span {
  font-size: 0.76rem;
  color: var(--text-tertiary);
  font-weight: 600;
}

.param-field input[type="number"] {
  cursor: text;
}

.param-field input,
.param-field select {
  margin-top: auto;
}

/* Drag & Drop Style File Input */
.file-upload-wrapper {
  position: relative;
  border: 1.5px dashed var(--border-strong);
  border-radius: var(--radius-md);
  padding: 22px 15px;
  text-align: center;
  background: var(--bg-inset);
  transition: border-color 0.15s ease, background 0.15s ease;
}

.file-upload-wrapper:hover {
  border-color: var(--accent);
  background: var(--accent-soft);
}

.upload-icon {
  font-size: 1.6rem;
  color: var(--text-tertiary);
  margin-bottom: 8px;
  display: block;
}

.upload-caption {
  font-size: 0.85rem;
  color: var(--text-secondary);
}

.file-upload-wrapper input[type="file"] {
  position: absolute;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
  opacity: 0;
  cursor: pointer;
}

/* --- Toggle switch (Allow short selling) --- */
.toggle-row {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  margin: 0;
  cursor: pointer;
  font-weight: 500;
  color: var(--text-primary);
}

.toggle-switch {
  position: relative;
  width: 40px;
  height: 22px;
  flex: none;
}

.toggle-switch input {
  position: absolute;
  inset: 0;
  opacity: 0;
  margin: 0;
  cursor: pointer;
  z-index: 2;
}

.toggle-track {
  position: absolute;
  inset: 0;
  background: var(--bg-inset);
  border: 1px solid var(--border-strong);
  border-radius: 999px;
  transition: background 0.15s ease, border-color 0.15s ease;
}

.toggle-thumb {
  position: absolute;
  top: 2px;
  left: 2px;
  width: 16px;
  height: 16px;
  border-radius: 50%;
  background: var(--text-tertiary);
  transition: transform 0.15s ease, background 0.15s ease;
}

.toggle-switch input:checked~.toggle-track {
  background: var(--accent-soft);
  border-color: var(--accent);
}

.toggle-switch input:checked~.toggle-track .toggle-thumb {
  transform: translateX(18px);
  background: var(--accent);
}

.toggle-switch input:focus-visible~.toggle-track {
  box-shadow: 0 0 0 3px var(--accent-soft);
}

/* --- Buttons --- */
button {
  font-family: var(--font-ui);
  border: none;
  border-radius: var(--radius-md);
  cursor: pointer;
  font-size: 0.98rem;
  font-weight: 600;
  transition: background 0.15s ease, border-color 0.15s ease, color 0.15s ease;
  width: 100%;
  display: flex;
  justify-content: center;
  align-items: center;
  gap: 10px;
  min-height: 48px;
  /* touch target */
}

button:disabled {
  background: var(--bg-inset) !important;
  color: var(--text-tertiary) !important;
  border-color: var(--border) !important;
  cursor: not-allowed;
}

.ctrl-actions {
  display: flex;
  flex-direction: column;
  gap: 10px;
  margin-top: 20px;
  padding-top: 20px;
  border-top: 1px solid var(--border);
}

.btn-primary {
  background: var(--accent);
  color: #fff;
  border: 1px solid var(--accent);
}

.btn-primary:hover:not(:disabled) {
  background: var(--accent-hover);
  border-color: var(--accent-hover);
}

.btn-secondary {
  background: transparent;
  color: var(--text-primary);
  border: 1px solid var(--border-strong);
}

.btn-secondary:hover:not(:disabled) {
  border-color: var(--accent);
  color: var(--accent);
}

.ctrl-actions-caption {
  font-size: 0.76rem;
  color: var(--text-tertiary);
  margin: 0;
  line-height: 1.4;
}

.btn-clear {
  background: rgba(248, 113, 113, 0.08);
  color: var(--danger);
  border: 1px solid rgba(248, 113, 113, 0.3);
  padding: 9px;
  width: auto;
  border-radius: var(--radius-sm);
  font-size: 0.85rem;
  margin-top: 10px;
  min-height: 40px;
}

.btn-clear:hover {
  background: rgba(248, 113, 113, 0.16);
}

/* --- Metrics Grid --- */
.metrics-wrap {
  margin-bottom: 22px;
}

.metrics-primary {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(160px, 1fr));
  gap: 12px;
  margin-bottom: 12px;
}

.metrics-secondary {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(130px, 1fr));
  gap: 10px;
}

.metric-box {
  background: var(--bg-inset);
  padding: 14px;
  border-radius: var(--radius-md);
  border: 1px solid var(--border);
  display: flex;
  flex-direction: column;
  gap: 6px;
}

.metric-box-lg {
  padding: 16px;
}

.metric-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  color: var(--text-tertiary);
  font-size: 0.72rem;
  text-transform: uppercase;
  letter-spacing: 0.5px;
  font-weight: 600;
}

.metric-value {
  font-family: var(--font-mono);
  font-size: 1.3rem;
  font-weight: 600;
  color: var(--text-primary);
}

.metric-box-lg .metric-value {
  font-size: 1.7rem;
}

.metric-caption {
  font-family: var(--font-ui);
  font-size: 0.7rem;
  color: var(--text-tertiary);
  min-height: 0.9rem;
}

.run-notes {
  font-size: 0.76rem;
  color: var(--text-tertiary);
  margin: 10px 0;
  line-height: 1.4;
}

.run-notes:empty {
  display: none;
}

/* Dynamic Colors applied via JS */
.text-success {
  color: var(--success) !important;
}

.text-danger {
  color: var(--danger) !important;
}

/* --- Error alert --- */
#error-output {
  color: var(--danger);
  font-weight: 500;
  font-size: 0.88rem;
  background: rgba(248, 113, 113, 0.08);
  border: 1px solid rgba(248, 113, 113, 0.3);
  border-left: 3px solid var(--danger);
  border-radius: var(--radius-sm);
  padding: 12px 14px;
  margin-bottom: 18px;
  white-space: pre-wrap;
}

#error-output:empty {
  display: none;
  padding: 0;
  margin: 0;
  border: none;
}

/* --- Loading skeleton (shown while a backtest is running, pre-first-result) --- */
.skeleton-charts {
  margin-bottom: 18px;
}

.skeleton-block {
  position: relative;
  overflow: hidden;
  background: var(--bg-inset);
  border: 1px solid var(--border);
  border-radius: var(--radius-lg);
  margin-bottom: 20px;
}

.skeleton-block-chart {
  height: 300px;
}

.skeleton-block-table {
  height: 150px;
  padding: 18px;
  display: flex;
  flex-direction: column;
  gap: 14px;
  justify-content: center;
}

.skeleton-row {
  position: relative;
  overflow: hidden;
  height: 12px;
  border-radius: 4px;
  background: var(--border);
  width: 100%;
}

.skeleton-row:nth-child(2) {
  width: 85%;
}

.skeleton-row:nth-child(3) {
  width: 65%;
}

.skeleton-block::after,
.skeleton-row::after {
  content: '';
  position: absolute;
  inset: 0;
  transform: translateX(-100%);
  background: linear-gradient(90deg, transparent, rgba(255, 255, 255, 0.06), transparent);
  animation: shimmer 1.6s infinite;
}

@keyframes shimmer {
  100% {
    transform: translateX(100%);
  }
}

/* --- Reveal-in animation (metric cards / charts / heatmaps on new results) --- */
@keyframes revealIn {
  from {
    opacity: 0;
    transform: translateY(8px);
  }

  to {
    opacity: 1;
    transform: translateY(0);
  }
}

.reveal-in {
  animation: revealIn 0.45s cubic-bezier(0.16, 1, 0.3, 1) both;
}

/* --- Charts & Tables --- */
.chart-wrapper {
  height: 600px;
  width: 100%;
  max-height: 50vh;
  position: relative;
  margin-bottom: 30px;
  background: var(--bg-inset);
  border-radius: var(--radius-lg);
  padding: 1rem;
  box-sizing: border-box;
  border: 1px solid var(--border);
  overflow: hidden;
  /* Prevents internal canvas from pushing boundaries */
}

.chart-wrapper-sub {
  height: 160px;
  max-height: 160px;
  margin-bottom: 1rem;
  padding-top: 2rem;
}

.chart-subtitle {
  position: absolute;
  top: 10px;
  left: 15px;
  font-size: 0.72rem;
  font-weight: 700;
  text-transform: uppercase;
  letter-spacing: 0.6px;
  color: var(--text-tertiary);
  z-index: 5;
}

/* Fullscreen overrides */
.chart-wrapper:fullscreen,
.chart-wrapper:-webkit-full-screen {
  background: var(--bg);
  padding: 5vw 5vh;
  height: 100vh;
  width: 100vw;
  border-radius: 0;
  border: none;
}

.chart-controls {
  position: absolute;
  top: 10px;
  right: 10px;
  display: flex;
  gap: 8px;
  z-index: 10;
}

.action-btn {
  background: var(--bg-elevated);
  color: var(--text-secondary);
  border: 1px solid var(--border-strong);
  border-radius: var(--radius-sm);
  padding: 8px 12px;
  font-size: 0.85rem;
  cursor: pointer;
  min-height: 36px;
  width: auto;
  display: flex;
  align-items: center;
  justify-content: center;
}

.action-btn:hover {
  color: var(--text-primary);
  border-color: var(--accent);
}

.action-btn.reset-zoom {
  display: none;
}

/* Hidden by default */

.table-container {
  overflow-x: auto;
  border-radius: var(--radius-lg);
  background: var(--bg-inset);
  border: 1px solid var(--border);
  max-height: 50vh;
  -webkit-overflow-scrolling: touch;
  /* smooth mobile scroll */
}

table {
  border-collapse: collapse;
  width: 100%;
  max-width: 95vw;
  font-size: 0.86rem;
  text-align: left;
  white-space: nowrap;
  font-family: var(--font-mono);
}

th {
  background: var(--bg-elevated);
  color: var(--text-tertiary);
  padding: 12px;
  font-weight: 600;
  text-transform: uppercase;
  font-size: 0.7rem;
  letter-spacing: 1px;
  font-family: var(--font-ui);
  position: sticky;
  top: 0;
  z-index: 10;
  border-bottom: 1px solid var(--border);
}

td {
  padding: 12px 16px;
  border-bottom: 1px solid var(--border);
  color: var(--text-primary);
}

tr:hover td {
  background: var(--bg-elevated);
}

.dir-long {
  color: var(--success);
  font-weight: 700;
}

.dir-short,
.dir-exit {
  color: var(--danger);
  font-weight: 700;
}

/* --- Overfitting Lab heatmaps --- */
.heatmap-grids {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 24px;
  margin: 15px 0 20px;
}

.heatmap-panel h3 {
  margin: 0 0 8px;
  font-size: 1rem;
  color: var(--text-primary);
}

.heatmap-panel h3 span {
  color: var(--text-tertiary);
  font-weight: 400;
  font-size: 0.82rem;
}

.heatmap {
  display: grid;
  gap: 5px;
}

.hm-cell {
  aspect-ratio: 1 / 1;
  border-radius: var(--radius-sm);
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  font-size: 0.9rem;
  font-family: var(--font-mono);
  font-weight: 600;
  color: #0b0e14;
  border: 1px solid rgba(0, 0, 0, 0.15);
}

.hm-cell small {
  font-weight: 500;
  font-size: 0.6rem;
  color: rgba(11, 14, 20, 0.55);
}

.hm-axis {
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 0.7rem;
  color: var(--text-tertiary);
  font-weight: 600;
  font-family: var(--font-mono);
}

.hm-corner {
  font-size: 0.58rem;
  color: var(--text-tertiary);
  text-align: center;
  align-self: center;
}

.hm-invalid {
  background: repeating-linear-gradient(45deg, var(--bg-inset), var(--bg-inset) 6px, var(--bg-elevated) 6px, var(--bg-elevated) 12px);
  color: var(--text-tertiary);
  border-color: var(--border);
}

.hm-pick {
  outline: 3px solid var(--warning);
  outline-offset: -3px;
  box-shadow: 0 0 0 3px rgba(245, 158, 11, 0.3);
}

.hm-best {
  outline: 3px dashed var(--success);
  outline-offset: -3px;
}

.heatmap-legend {
  display: flex;
  align-items: center;
  gap: 18px;
  flex-wrap: wrap;
  font-size: 0.8rem;
  color: var(--text-secondary);
  margin-bottom: 18px;
}

.legend-item {
  display: flex;
  align-items: center;
  gap: 7px;
}

.legend-swatch {
  width: 16px;
  height: 16px;
  border-radius: 4px;
  display: inline-block;
}

.legend-gradient {
  width: 90px;
  height: 14px;
  border-radius: 4px;
  background: linear-gradient(to right, hsl(350, 65%, 55%), hsl(220, 12%, 30%), hsl(150, 65%, 45%));
  display: inline-block;
}

.of-verdict {
  background: var(--bg-inset);
  border-radius: var(--radius-md);
  padding: 16px 18px;
  font-size: 0.92rem;
  line-height: 1.55;
  color: var(--text-primary);
  border: 1px solid var(--border);
}

.of-verdict strong {
  color: var(--accent);
}

.dsr-good {
  color: var(--success);
  font-weight: 700;
}

.dsr-bad {
  color: var(--danger);
  font-weight: 700;
}

@media (max-width: 800px) {
  .heatmap-grids {
    grid-template-columns: 1fr;
  }
}

/* --- Footer --- */
.app-footer {
  width: 100%;
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 16px 20px;
  background: var(--bg-elevated);
  border: 1px solid var(--border);
  border-radius: var(--radius-lg);
  box-sizing: border-box;
}

.footer-legal {
  font-size: 0.82rem;
  color: var(--text-tertiary);
}

.footer-legal a {
  color: var(--text-tertiary);
  text-decoration: underline;
  transition: color 0.15s ease;
}

.footer-legal a:hover {
  color: var(--accent);
}

.social-icons {
  display: flex;
  gap: 1rem;
}

.social-icons a {
  color: var(--text-secondary);
  font-size: 1.5rem;
  transition: color 0.15s ease;
}

.social-icons a:hover {
  color: var(--accent);
}

/* --- Responsive Design --- */
@media (max-width: 1024px) {
  .dashboard-container {
    grid-template-columns: 1fr;
  }
}

@media (max-width: 600px) {
  body {
    padding: 14px 3vw;
  }

  .topbar {
    flex-wrap: wrap;
    gap: 12px;
  }

  .metrics-primary,
  .metrics-secondary {
    grid-template-columns: 1fr 1fr;
  }

  .chart-wrapper {
    height: 350px;
  }

  .chart-wrapper-sub {
    height: 110px;
    max-height: 110px;
  }

  .app-footer {
    flex-direction: column;
    gap: 12px;
    text-align: center;
  }
}

/* --- Reduced motion --- */
@media (prefers-reduced-motion: reduce) {

  *,
  *::before,
  *::after {
    animation-duration: 0.001ms !important;
    animation-iteration-count: 1 !important;
    transition-duration: 0.001ms !important;
    scroll-behavior: auto !important;
  }
}
