/* Per-view layout. */

/* --- Shared page scaffolding -------------------------------------------- */

.page {
  flex: 1;
  min-height: 0;
  overflow-y: auto;
  padding: var(--container-margin);
  display: flex;
  flex-direction: column;
  gap: var(--stack-lg);
}

.page__header {
  display: flex;
  align-items: flex-end;
  justify-content: space-between;
  gap: var(--stack-md);
  flex-wrap: wrap;
}

.page__actions {
  display: flex;
  gap: var(--stack-sm);
  flex-wrap: wrap;
}

/* --- Dashboard ----------------------------------------------------------- */

.kpi-grid {
  display: grid;
  grid-template-columns: repeat(3, minmax(0, 1fr));
  gap: var(--gutter);
}

.kpi__head {
  display: flex;
  align-items: flex-start;
  justify-content: space-between;
}

.kpi__value {
  display: flex;
  align-items: baseline;
  gap: var(--stack-sm);
}

.kpi__spark {
  height: 48px;
  margin-top: var(--stack-sm);
}

/* Two rows share the dashboard, so neither may claim the leftover space: the
   chart row is given a height and the panels below it size to their content. */
.dashboard-bento {
  display: grid;
  grid-template-columns: 2fr 1fr;
  gap: var(--gutter);
  flex: 0 0 auto;
  align-items: start;
}

.dashboard-bento > * {
  min-height: 0;
}

.dashboard-bento--tall,
.dashboard-bento--tall > * {
  height: 460px;
}

/* The side column stacks two panels: the run list takes the slack and scrolls,
   so the activity panel below it is never pushed out of the row. */
.dashboard-bento--tall > .col {
  min-height: 0;
}

.dashboard-bento--tall > .col > .panel:first-child {
  flex: 1;
  min-height: 0;
}

.execution-list {
  display: flex;
  flex-direction: column;
  gap: var(--unit);
  padding: var(--stack-sm);
  overflow-y: auto;
  flex: 1;
  min-height: 0;
}

a.execution {
  text-decoration: none;
  color: inherit;
}

a.execution:hover {
  border-color: var(--primary);
}

.execution {
  background: var(--surface-container-high);
  border: 1px solid var(--outline-variant);
  border-radius: var(--radius);
  padding: 12px;
  display: flex;
  flex-direction: column;
  gap: var(--stack-sm);
}

.execution--failed {
  border-color: color-mix(in srgb, var(--error) 30%, transparent);
}

.execution__row {
  display: flex;
  align-items: flex-end;
  justify-content: space-between;
  gap: var(--stack-sm);
}

.execution__stat {
  display: flex;
  flex-direction: column;
  min-width: 0;
}

.execution__stat--right {
  text-align: right;
}

.execution__error {
  color: var(--error);
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.is-dimmed {
  opacity: 0.5;
}

/* --- Strategy editor ----------------------------------------------------- */

.editor-layout {
  flex: 1;
  min-height: 0;
  display: flex;
  overflow: hidden;
}

.explorer {
  width: 192px;
  flex-shrink: 0;
  display: flex;
  flex-direction: column;
  background: var(--surface-container-low);
  border-right: 1px solid var(--outline-variant);
}

.explorer__title {
  padding: var(--stack-sm);
  border-bottom: 1px solid var(--outline-variant);
  color: var(--on-surface-variant);
}

.explorer__list {
  flex: 1;
  overflow-y: auto;
  padding: var(--stack-sm) 0;
}

.explorer__item {
  display: flex;
  align-items: center;
  gap: var(--unit);
  padding: var(--unit) var(--stack-sm);
  color: var(--on-surface-variant);
  font-family: var(--font-code);
  font-size: 12px;
  cursor: pointer;
  width: 100%;
  background: none;
  border: none;
  text-align: left;
}

.explorer__item:hover {
  background: var(--surface-container-high);
}

.explorer__item[aria-current="true"] {
  background: var(--surface-variant);
  color: var(--on-surface);
}

.editor-pane {
  flex: 1;
  min-width: 0;
  display: flex;
  flex-direction: column;
  background: var(--surface-container-low);
  border-right: 1px solid var(--outline-variant);
}

.editor-pane__tabbar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: var(--stack-sm) var(--container-margin);
  background: var(--surface-container-highest);
  border-bottom: 1px solid var(--outline-variant);
}

.editor-pane__tools {
  display: flex;
  align-items: center;
  gap: var(--unit);
}

.editor-pane__divider {
  width: 1px;
  height: 16px;
  background: var(--outline-variant);
  margin: 0 var(--unit);
}

.editor-host {
  flex: 1;
  min-height: 0;
  overflow: hidden;
  background: var(--surface-container-lowest);
}

.editor-host .cm-editor {
  height: 100%;
}

.problems {
  height: 128px;
  flex-shrink: 0;
  display: flex;
  flex-direction: column;
  background: var(--surface-container);
  border-top: 1px solid var(--outline-variant);
}

.problems__header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: var(--unit) var(--stack-md);
  background: var(--surface-container-highest);
  border-bottom: 1px solid var(--outline-variant);
}

.problems__list {
  flex: 1;
  overflow-y: auto;
  padding: var(--stack-sm);
  display: flex;
  flex-direction: column;
  gap: var(--unit);
}

/* A problem is a button: clicking it puts the cursor on the offending line. */
.problem {
  display: flex;
  align-items: center;
  gap: var(--stack-sm);
  width: 100%;
  border: 0;
  background: none;
  text-align: left;
  cursor: pointer;
}

.problem--warning { color: var(--tertiary); }
.problem--error { color: var(--error); }

.settings-panel {
  width: 384px;
  flex-shrink: 0;
  display: flex;
  flex-direction: column;
  background: var(--surface-container);
  border-left: 1px solid var(--outline-variant);
  overflow-y: auto;
}

.settings-panel__header {
  padding: var(--stack-md) var(--container-margin);
  border-bottom: 1px solid var(--outline-variant);
  position: sticky;
  top: 0;
  background: var(--surface-container);
  z-index: 10;
}

.settings-panel__body {
  padding: var(--container-margin);
  display: flex;
  flex-direction: column;
  gap: var(--stack-lg);
}

.settings-panel__footer {
  margin-top: auto;
  padding: var(--container-margin);
  border-top: 1px solid var(--outline-variant);
  background: var(--surface-container);
  position: sticky;
  bottom: 0;
  z-index: 10;
}

.param-group__title {
  display: flex;
  align-items: center;
  gap: var(--unit);
  margin-bottom: var(--stack-md);
}

.param-group__fields {
  display: flex;
  flex-direction: column;
  gap: var(--stack-md);
}

/* --- Optimization -------------------------------------------------------- */

.optimization-bento {
  display: grid;
  grid-template-columns: 5fr 7fr;
  gap: var(--stack-md);
  align-items: stretch;
}

.param-list {
  background: var(--surface-container-lowest);
  border: 1px solid var(--outline-variant);
  border-radius: var(--radius);
  padding: var(--stack-sm);
  font-family: var(--font-code);
  font-size: 12px;
  line-height: 18px;
}

.param-list__row {
  display: flex;
  justify-content: space-between;
  gap: var(--stack-md);
}

.param-list__key { color: var(--primary-fixed); }
.param-list__val { color: var(--secondary-fixed); }

.metric-grid {
  display: grid;
  grid-template-columns: repeat(2, minmax(0, 1fr));
  gap: var(--unit);
  margin-top: auto;
}

.metric {
  border: 1px solid var(--outline-variant);
  border-radius: var(--radius);
  padding: var(--stack-sm);
  display: flex;
  flex-direction: column;
  gap: var(--unit);
}

.metric__value {
  font-family: var(--font-code);
  font-size: 18px;
  font-weight: 450;
  font-variant-numeric: tabular-nums;
}

.heatmap-stage {
  flex: 1;
  display: flex;
  align-items: center;
  justify-content: center;
  position: relative;
  padding: var(--stack-lg);
  background: var(--surface-container-lowest);
  border-bottom-left-radius: var(--radius-panel);
  border-bottom-right-radius: var(--radius-panel);
}

.axis-label {
  position: absolute;
  color: var(--on-surface-variant);
  white-space: nowrap;
}

.axis-label--y {
  left: var(--stack-md);
  top: 50%;
  transform: translateY(-50%) rotate(-90deg);
  transform-origin: center;
}

.axis-label--x {
  bottom: var(--stack-md);
  left: 50%;
  transform: translateX(-50%);
}

/* --- Tutorial ------------------------------------------------------------ */

.tutorial-layout {
  display: flex;
  gap: var(--stack-lg);
  flex: 1;
  min-height: 0;
}

.tutorial-layout__aside {
  width: 400px;
  flex-shrink: 0;
  display: flex;
  flex-direction: column;
  gap: var(--stack-md);
}

.tutorial-layout__stage {
  flex: 1;
  min-width: 0;
  display: flex;
  flex-direction: column;
  gap: var(--stack-md);
}

.tutorial-metrics {
  display: grid;
  grid-template-columns: repeat(4, minmax(0, 1fr));
  gap: var(--stack-sm);
}

.tutorial-chart {
  flex: 1;
  min-height: 400px;
  display: flex;
  flex-direction: column;
}

.code-preview {
  background: var(--surface-container-lowest);
  padding: var(--stack-md);
  overflow: auto;
  flex: 1;
  min-height: 220px;
}

.code-preview pre {
  margin: 0;
  font-family: var(--font-code);
  font-size: 12px;
  line-height: 20px;
  color: var(--on-surface-variant);
}

.tok-keyword { color: var(--primary); }
.tok-func { color: var(--tertiary); }
.tok-string { color: var(--secondary); }
.tok-number { color: var(--tertiary-fixed-dim); }
.tok-comment { color: var(--outline); }

.pulse-highlight {
  animation: pulse-border 2s ease-in-out infinite;
}

@keyframes pulse-border {
  0%, 100% { border-color: var(--outline-variant); }
  50% { border-color: var(--primary); }
}

@media (prefers-reduced-motion: reduce) {
  .pulse-highlight,
  .dot--primary {
    animation: none;
  }
}

/* --- Account -------------------------------------------------------------- */

/* DESIGN.md: settings and documentation use a fixed-centre 800px column. */
.page--narrow {
  max-width: 800px;
  width: 100%;
  margin: 0 auto;
  gap: var(--stack-md);
}

.avatar--lg {
  width: 56px;
  height: 56px;
  font-size: 18px;
}

.profile {
  display: flex;
  align-items: center;
  gap: var(--stack-md);
}

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

.detail-list {
  display: grid;
  grid-template-columns: 160px 1fr;
  gap: var(--stack-sm) var(--stack-md);
  align-items: center;
  margin: 0;
  padding-top: var(--stack-md);
  border-top: 1px solid var(--outline-variant);
}

.detail-list dd {
  margin: 0;
}

.plan {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: var(--stack-md);
  flex-wrap: wrap;
}

.meter {
  display: flex;
  flex-direction: column;
  gap: var(--stack-sm);
}

.meter__head {
  display: flex;
  align-items: baseline;
  justify-content: space-between;
  gap: var(--stack-sm);
}

.api-key {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: var(--stack-md);
  padding: var(--stack-md);
  background: var(--surface-container-high);
  border: 1px solid var(--outline-variant);
  border-radius: var(--radius);
}

.api-key .code-sm {
  overflow-wrap: anywhere;
}

@media (max-width: 640px) {
  .profile {
    flex-wrap: wrap;
  }

  .detail-list {
    grid-template-columns: minmax(0, 1fr);
    gap: var(--unit);
  }
}

/* --- Responsive ---------------------------------------------------------- */

@media (max-width: 1024px) {
  .kpi-grid {
    grid-template-columns: repeat(2, minmax(0, 1fr));
  }

  .dashboard-bento,
  .optimization-bento {
    grid-template-columns: minmax(0, 1fr);
    flex: none;
  }

  .dashboard-bento > * {
    min-height: 360px;
  }

  .tutorial-layout {
    flex-direction: column;
  }

  .tutorial-layout__aside {
    width: 100%;
  }

  .settings-panel {
    width: 320px;
  }
}

@media (max-width: 640px) {
  .page {
    padding: var(--stack-md) 12px;
    gap: var(--stack-md);
  }

  .kpi-grid,
  .tutorial-metrics {
    grid-template-columns: minmax(0, 1fr);
  }

  .editor-layout {
    flex-direction: column;
    overflow: visible;
  }

  .explorer {
    width: 100%;
    border-right: none;
    border-bottom: 1px solid var(--outline-variant);
  }

  .explorer__list {
    display: flex;
    overflow-x: auto;
  }

  .editor-pane {
    border-right: none;
  }

  .editor-host {
    height: 320px;
    flex: none;
  }

  .settings-panel {
    width: 100%;
    border-left: none;
    border-top: 1px solid var(--outline-variant);
  }

  .input-search .input {
    width: 140px;
  }
}

/* A folder is structure, not a document: no click affordance. */
.explorer__item--folder {
  cursor: default;
  color: var(--outline);
}

.explorer__item--folder:hover {
  background: none;
}

/* --- Runs ---------------------------------------------------------------- */

.runs-table {
  overflow: auto;
  max-height: calc(100vh - 260px);
}

/* --- Run detail ----------------------------------------------------------- */

.run-back {
  display: inline-flex;
  align-items: center;
  gap: var(--unit);
  margin-bottom: var(--stack-sm);
  text-decoration: none;
}

.run-back:hover {
  color: var(--on-surface);
}

.run-id {
  align-self: center;
}

.run-subline {
  display: flex;
  align-items: center;
  gap: var(--stack-md);
  margin-top: var(--unit);
  flex-wrap: wrap;
}

.run-metrics {
  display: grid;
  grid-template-columns: repeat(5, 1fr);
  gap: var(--gutter);
}

.run-metric {
  display: flex;
  flex-direction: column;
  gap: var(--unit);
  padding: var(--stack-sm) var(--stack-md);
}

.run-metric__value {
  font-family: var(--font-code);
  font-size: 20px;
  font-weight: 450;
}

/**
 * The run page stacks several tall panels and scrolls, so its sections keep
 * their own height instead of competing for the leftover space the way a
 * single-screen layout does.
 */
.page--stacked > * {
  flex: 0 0 auto;
}

/* The equity curve gets the room; the configuration reads as a column beside it.
   The chart is given a height and the configuration takes whatever it needs —
   the reproducibility record is the last thing that should be cut off. */
.run-bento {
  display: grid;
  grid-template-columns: 2fr 1fr;
  gap: var(--gutter);
  align-items: start;
}

.run-bento > .panel:first-child {
  height: 440px;
}

/* Grid items default to min-height:auto, which lets a tall chart push the
   panel past its row instead of scrolling inside it. */
.run-bento > * {
  min-height: 0;
}

.run-config {
  overflow-y: auto;
}

.run-config__group {
  display: flex;
  flex-direction: column;
  gap: var(--stack-sm);
}

.run-error {
  margin: 0;
  padding: var(--stack-sm) var(--stack-md);
  border-radius: var(--radius);
  border: 1px solid var(--error);
  background: color-mix(in srgb, var(--error) 10%, transparent);
  color: var(--error);
  white-space: pre-wrap;
}

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

  .run-bento {
    grid-template-columns: 1fr;
  }
}

@media (max-width: 640px) {
  .run-metrics {
    grid-template-columns: repeat(2, 1fr);
  }
}

/* --- Price chart & blotter ------------------------------------------------ */

/* A definite height on the panel, so the body can flex and the plot inside it
   has something to resolve `height: 100%` against. */
.price-panel {
  height: 420px;
}

/* The chart is swapped when another instrument is picked, so it hangs off a
   host element — which has to carry the panel's height or the chart, sized in
   percent, resolves against nothing and overflows the panel. */
.price-host {
  height: 100%;
  min-height: 0;
}

.price__span {
  fill: color-mix(in srgb, var(--primary) 12%, transparent);
}

.price__marker {
  stroke: var(--surface);
  stroke-width: 1;
  vector-effect: non-scaling-stroke;
  /* Markers are drawn in a stretched viewBox; keep them from smearing. */
  transform-box: fill-box;
}

.price__marker--entry {
  fill: var(--secondary);
}

.price__marker--exit {
  fill: var(--error);
}

.price__marker.is-active {
  stroke: var(--on-surface);
  stroke-width: 2;
}

/* Legend keys mirror the triangles drawn on the plot. */
.price__key {
  width: 0;
  height: 0;
  border-left: 5px solid transparent;
  border-right: 5px solid transparent;
}

.price__key--entry {
  border-bottom: 8px solid var(--secondary);
}

.price__key--exit {
  border-top: 8px solid var(--error);
}

.blotter {
  overflow: auto;
  max-height: 420px;
}

/* --- Run: thin samples, validation, drawdown ------------------------------- */

/* A metric that rests on too few trades is dimmed rather than hidden: the
   number is still the number, but it must not read as evidence. */
.run-metric--thin {
  border-style: dashed;
  opacity: 0.75;
}

.run-metric--thin .run-metric__value {
  color: var(--warning);
}

.validation-grid {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(320px, 1fr));
  gap: var(--gutter);
}

.validation-block {
  display: flex;
  flex-direction: column;
  gap: var(--stack-sm);
  padding: var(--stack-md);
  border: 1px solid var(--outline-variant);
  border-radius: var(--radius);
  background: var(--surface-container-lowest);
}

.stat-row {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(120px, 1fr));
  gap: var(--stack-sm);
}

.stat {
  display: flex;
  flex-direction: column;
  gap: 2px;
}

.stat__value {
  font-family: var(--font-code);
  font-size: 18px;
}

/* --- Compare ---------------------------------------------------------------- */

.compare-chips {
  display: flex;
  flex-wrap: wrap;
  gap: var(--stack-sm);
  margin-top: var(--stack-sm);
}

.compare-chip {
  display: inline-flex;
  align-items: center;
  gap: var(--unit);
  padding: 2px var(--stack-sm);
  border: 1px solid var(--outline-variant);
  border-radius: var(--radius-sm);
  text-decoration: none;
  color: var(--on-surface);
}

.compare-chip:hover { border-color: var(--primary); }

/* --- Optimisation setup ------------------------------------------------------ */

.sweep-setup {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(280px, 1fr));
  gap: var(--gutter);
  align-items: start;
}

.axis-row {
  display: flex;
  flex-direction: column;
  gap: 4px;
  padding: var(--stack-sm);
  border: 1px solid var(--outline-variant);
  border-radius: var(--radius);
}

.axis-row.is-off { opacity: 0.5; }

.axis-row__toggle {
  display: flex;
  align-items: center;
  gap: var(--unit);
  cursor: pointer;
}

.axis-row__bounds {
  display: flex;
  align-items: center;
  flex-wrap: wrap;
  gap: var(--unit);
}

.candidate {
  display: flex;
  flex-direction: column;
  gap: var(--stack-sm);
  padding: var(--stack-md);
  border: 1px solid var(--outline-variant);
  border-radius: var(--radius);
  background: var(--surface-container-lowest);
}

.candidate__head {
  display: flex;
  align-items: baseline;
  justify-content: space-between;
  gap: var(--stack-sm);
}

/* --- Dashboard contributors --------------------------------------------------- */

.contributor {
  display: grid;
  grid-template-columns: 1fr auto auto auto;
  align-items: center;
  gap: var(--stack-sm);
  padding: var(--unit) var(--stack-sm);
  border-bottom: 1px solid var(--outline-variant);
}

.contributor:last-child { border-bottom: 0; }
.contributor a { text-decoration: none; color: var(--on-surface); }
.contributor a:hover { color: var(--primary); }

/* --- Editor explorer --------------------------------------------------------- */

.explorer__footer {
  margin-top: auto;
  padding-top: var(--stack-sm);
  border-top: 1px solid var(--outline-variant);
}

.explorer__item--action { color: var(--primary); }

.problem--ok { color: var(--secondary); }

@media (max-width: 1100px) {
  .run-bento > .panel:first-child {
    height: 360px;
  }
}

/* --- Auth (Area A) -------------------------------------------------------- */

/* Rendered into #view with the sidebar/topbar/bottomnav hidden (main.js) —
   there is no signed-in workspace yet for that chrome to belong to. */
.auth-page {
  min-height: 100vh;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: var(--stack-lg);
}

.auth-card {
  width: 100%;
  max-width: 380px;
}

.auth-card__brand {
  display: flex;
  align-items: center;
  gap: var(--stack-sm);
  margin-bottom: var(--stack-lg);
}

.auth-card__logo {
  width: 32px;
  height: 32px;
}

.auth-card .field__label { display: block; margin-bottom: var(--unit); }
