/* Reusable presentational components. */

/* --- Buttons ------------------------------------------------------------ */

.btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: var(--stack-sm);
  padding: var(--stack-sm) var(--stack-md);
  border-radius: var(--radius);
  border: 1px solid transparent;
  background: none;
  cursor: pointer;
  font-family: var(--font-code);
  font-size: 11px;
  font-weight: 600;
  letter-spacing: 0.05em;
  text-transform: uppercase;
  transition: background-color 0.15s ease, border-color 0.15s ease, opacity 0.15s ease;
}

.btn:disabled {
  opacity: 0.5;
  cursor: not-allowed;
}

.btn--primary {
  background: var(--primary);
  color: var(--on-primary);
}

.btn--primary:hover:not(:disabled) {
  background: var(--primary-fixed);
}

.btn--outline {
  border-color: var(--primary);
  color: var(--primary);
  background: var(--surface-container);
}

.btn--outline:hover:not(:disabled) {
  background: color-mix(in srgb, var(--primary) 10%, transparent);
}

.btn--ghost {
  border-color: var(--outline-variant);
  color: var(--on-surface);
}

.btn--ghost:hover:not(:disabled) {
  background: var(--surface-container-high);
}

.btn--danger {
  border-color: var(--error);
  color: var(--error);
}

.btn--danger:hover:not(:disabled) {
  background: color-mix(in srgb, var(--error) 10%, transparent);
}

.btn--block {
  width: 100%;
  padding: 12px;
  font-size: 14px;
  letter-spacing: 0.02em;
}

/* Groups of segmented controls in a panel header, spaced so the groups read
   as separate choices rather than one long strip of buttons. */
.panel__controls {
  display: flex;
  align-items: center;
  gap: var(--stack-lg);
  flex-wrap: wrap;
}

/* Single-choice button group (1M / 3M / YTD). */
.segmented {
  display: flex;
  gap: var(--stack-sm);
}

.segmented__option {
  padding: var(--unit) var(--stack-sm);
  border-radius: var(--radius);
  border: 1px solid transparent;
  background: var(--surface-container-high);
  color: var(--on-surface-variant);
  font-family: var(--font-code);
  font-size: 12px;
  cursor: pointer;
  transition: color 0.15s ease, background-color 0.15s ease;
}

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

.segmented__option:disabled {
  opacity: 0.35;
  cursor: not-allowed;
}

.segmented__option[aria-pressed="true"] {
  background: color-mix(in srgb, var(--primary) 20%, transparent);
  border-color: var(--primary);
  color: var(--primary);
}

/* --- Panels ------------------------------------------------------------- */

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

.panel__header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: var(--stack-sm);
  padding: var(--stack-md);
  border-bottom: 1px solid var(--outline-variant);
}

.panel__body {
  padding: var(--stack-md);
  flex: 1;
  min-height: 0;
}

.panel__body--flush {
  padding: 0;
}

.card {
  background: var(--surface-container);
  border: 1px solid var(--outline-variant);
  border-radius: var(--radius-panel);
  padding: var(--stack-md);
  display: flex;
  flex-direction: column;
  gap: var(--stack-sm);
  transition: border-color 0.15s ease;
}

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

/* --- Status indicators -------------------------------------------------- */

.dot {
  width: 8px;
  height: 8px;
  border-radius: var(--radius-full);
  flex-shrink: 0;
  display: inline-block;
}

.dot--sm {
  width: 6px;
  height: 6px;
}

.dot--secondary {
  background: var(--secondary);
  box-shadow: 0 0 8px var(--secondary);
}

.dot--primary {
  background: var(--primary);
  box-shadow: 0 0 8px var(--primary);
  animation: pulse 1.4s ease-in-out infinite;
}

.dot--error {
  background: var(--error);
}

@keyframes pulse {
  0%, 100% { opacity: 1; }
  50% { opacity: 0.35; }
}

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

/* --- Inputs ------------------------------------------------------------- */

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

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

.field__label {
  font-family: var(--font-code);
  font-size: 12px;
  color: var(--on-surface-variant);
}

.field__value {
  font-family: var(--font-code);
  font-size: 12px;
  color: var(--primary);
}

/* Text inputs recess into the surface, per DESIGN.md. */
.input {
  width: 100%;
  background: var(--background);
  border: 1px solid var(--outline-variant);
  border-radius: var(--radius);
  padding: var(--unit) var(--stack-sm);
  font-family: var(--font-code);
  font-size: 12px;
  color: var(--on-surface);
  transition: border-color 0.15s ease;
}

.input:focus {
  outline: none;
  border-color: var(--primary);
  box-shadow: 0 0 0 1px var(--primary);
}

.input-search {
  position: relative;
  display: inline-flex;
  align-items: center;
}

.input-search .material-symbols-outlined {
  position: absolute;
  left: var(--stack-sm);
  color: var(--on-surface-variant);
  pointer-events: none;
}

.input-search .input {
  background: var(--surface-container-highest);
  padding-left: 30px;
  width: 200px;
}

/* Sliders: primary track, white thumb for maximum visibility. */
.slider {
  -webkit-appearance: none;
  appearance: none;
  width: 100%;
  height: 4px;
  border-radius: var(--radius-full);
  background: var(--surface-container-highest);
  cursor: pointer;
}

.slider::-webkit-slider-thumb {
  -webkit-appearance: none;
  appearance: none;
  width: 14px;
  height: 14px;
  border-radius: var(--radius-full);
  background: #fff;
  border: 2px solid var(--primary);
  cursor: pointer;
}

.slider::-moz-range-thumb {
  width: 14px;
  height: 14px;
  border-radius: var(--radius-full);
  background: #fff;
  border: 2px solid var(--primary);
  cursor: pointer;
}

.slider:disabled {
  opacity: 0.5;
  cursor: not-allowed;
}

.slider__bounds {
  display: flex;
  justify-content: space-between;
  font-family: var(--font-code);
  font-size: 10px;
  color: var(--outline);
}

/* --- Progress ----------------------------------------------------------- */

.progress {
  width: 100%;
  height: 4px;
  border-radius: var(--radius-full);
  background: var(--surface-dim);
  overflow: hidden;
}

.progress__bar {
  height: 100%;
  background: var(--primary);
}

/* --- Table -------------------------------------------------------------- */

.table-wrap {
  overflow-x: auto;
}

.data-table {
  width: 100%;
  border-collapse: collapse;
  text-align: left;
}

.data-table th {
  padding: 0;
  background: var(--surface-container-lowest);
  border-bottom: 1px solid var(--outline-variant);
  font-family: var(--font-code);
  font-size: 11px;
  font-weight: 600;
  letter-spacing: 0.05em;
  text-transform: uppercase;
  color: var(--on-surface-variant);
  white-space: nowrap;
  cursor: pointer;
  user-select: none;
  position: sticky;
  top: 0;
  z-index: 1;
}

.sort-button {
  width: 100%;
  padding: var(--stack-sm) var(--stack-md);
  border: none;
  background: none;
  color: inherit;
  font: inherit;
  letter-spacing: inherit;
  text-transform: inherit;
  text-align: inherit;
  cursor: pointer;
}

.data-table th:hover {
  color: var(--on-surface);
}

.data-table th .sort-arrow {
  opacity: 0.35;
  font-size: 14px;
  vertical-align: middle;
}

.data-table th[aria-sort] .sort-arrow {
  opacity: 1;
  color: var(--primary);
}

.data-table td {
  padding: var(--stack-sm) var(--stack-md);
  border-bottom: 1px solid var(--outline-variant);
  font-family: var(--font-code);
  font-size: 14px;
  font-weight: 450;
  color: var(--on-surface-variant);
  font-variant-numeric: tabular-nums;
  white-space: nowrap;
}

.data-table tbody tr:hover {
  background: var(--surface-container-high);
}

.data-table tbody tr.is-best {
  background: color-mix(in srgb, var(--primary) 10%, transparent);
}

.data-table tbody tr.is-best:hover {
  background: color-mix(in srgb, var(--primary) 20%, transparent);
}

.data-table .num {
  text-align: right;
}

.table-empty {
  padding: var(--stack-lg);
  text-align: center;
  color: var(--on-surface-variant);
}

/* --- Heatmap ------------------------------------------------------------ */

.heatmap {
  display: grid;
  gap: 1px;
  background: var(--outline-variant);
  padding: 1px;
  width: 100%;
  max-width: 420px;
}

.heatmap__cell {
  aspect-ratio: 1;
  position: relative;
  cursor: crosshair;
  transition: transform 0.1s ease;
}

.heatmap__cell:hover {
  transform: scale(1.15);
  z-index: 20;
  outline: 1px solid var(--on-surface);
}

.heatmap__cell.val-vlow { background: var(--heat-1); }
.heatmap__cell.val-low { background: var(--heat-2); }
.heatmap__cell.val-med { background: var(--heat-3); }
.heatmap__cell.val-high { background: var(--heat-4); }
.heatmap__cell.val-vhigh { background: var(--heat-5); }

.heatmap__cell.is-optimum {
  outline: 1px solid var(--on-surface);
  z-index: 10;
}

.heatmap__cell.is-optimum::after {
  content: "";
  position: absolute;
  top: -3px;
  right: -3px;
  width: 8px;
  height: 8px;
  border-radius: var(--radius-full);
  background: var(--on-surface);
  box-shadow: 0 0 4px #fff;
}

.heatmap__tooltip {
  position: absolute;
  bottom: calc(100% + 6px);
  left: 50%;
  transform: translateX(-50%);
  background: var(--inverse-surface);
  color: var(--inverse-on-surface);
  padding: var(--unit) var(--stack-sm);
  border-radius: var(--radius);
  font-family: var(--font-code);
  font-size: 12px;
  line-height: 16px;
  white-space: nowrap;
  pointer-events: none;
  opacity: 0;
  transition: opacity 0.1s ease;
  z-index: 30;
}

.heatmap__cell:hover .heatmap__tooltip {
  opacity: 1;
}

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

.heatmap__swatch {
  width: 8px;
  height: 8px;
  display: inline-block;
  margin-right: var(--unit);
}

/* --- Chart -------------------------------------------------------------- */

.chart {
  position: relative;
  width: 100%;
  height: 100%;
  min-height: 0;
}

.chart svg {
  display: block;
  width: 100%;
  height: 100%;
  overflow: visible;
}

/* The plot fills the chart box (or its grid cell when there are axes) so the
   SVG's `height: 100%` has something to resolve against. */
.chart__plot {
  position: relative;
  height: 100%;
  min-width: 0;
  min-height: 0;
}

/* Axis gutters wrap the plot; the corner keeps the two axes from overlapping. */
.chart--axes .chart__frame {
  display: grid;
  grid-template-columns: auto 1fr;
  grid-template-rows: 1fr auto;
  height: 100%;
  min-height: 0;
}

.chart__axis {
  position: relative;
  font-family: var(--font-code);
  font-size: 11px;
  color: var(--on-surface-variant);
  white-space: nowrap;
}

.chart__axis--y {
  width: 4.5em;
  padding-right: var(--stack-sm);
}

.chart__axis--x {
  height: 1.6em;
  padding-top: var(--unit);
}

.chart__axis--y .chart__tick {
  position: absolute;
  right: var(--stack-sm);
  transform: translateY(-50%);
}

.chart__axis--x .chart__tick {
  position: absolute;
  transform: translateX(-50%);
}

/* The first and last date labels would otherwise overflow the plot's edges. */
.chart__axis--x .chart__tick:first-child {
  transform: none;
}

.chart__axis--x .chart__tick:last-child {
  transform: translateX(-100%);
}

.chart__legend {
  position: absolute;
  top: 0;
  left: 0;
  display: flex;
  gap: var(--stack-md);
  color: var(--on-surface-variant);
  pointer-events: none;
}

.chart__legend-item {
  display: inline-flex;
  align-items: center;
  gap: var(--unit);
}

.chart__swatch {
  width: 14px;
  height: 2px;
  border-radius: 1px;
  background: var(--primary);
}

.chart__swatch--dashed {
  background: repeating-linear-gradient(
    to right,
    var(--on-surface-variant) 0 5px,
    transparent 5px 9px
  );
}

.chart__tooltip {
  position: absolute;
  transform: translate(-50%, -130%);
  background: var(--surface-container-highest);
  border: 1px solid var(--outline);
  border-radius: var(--radius);
  padding: var(--unit) var(--stack-sm);
  font-family: var(--font-code);
  font-size: 12px;
  color: var(--on-surface);
  white-space: nowrap;
  pointer-events: none;
  opacity: 0;
  transition: opacity 0.1s ease;
  z-index: 5;
  display: flex;
  flex-direction: column;
  gap: 1px;
}

.chart__tip-date {
  color: var(--on-surface-variant);
  font-size: 11px;
}

.chart__tip-benchmark {
  color: var(--on-surface-variant);
  font-size: 11px;
}

.chart.is-hovered .chart__tooltip {
  opacity: 1;
}

.chart__crosshair,
.chart__marker {
  opacity: 0;
}

.chart.is-hovered .chart__crosshair,
.chart.is-hovered .chart__marker {
  opacity: 1;
}

/* --- Toast -------------------------------------------------------------- */

.toast-stack {
  position: fixed;
  bottom: var(--container-margin);
  left: 50%;
  transform: translateX(-50%);
  display: flex;
  flex-direction: column;
  gap: var(--stack-sm);
  z-index: 100;
}

.toast {
  background: var(--surface-container-high);
  border: 1px solid var(--outline-variant);
  border-radius: var(--radius);
  padding: var(--stack-sm) var(--stack-md);
  font-family: var(--font-code);
  font-size: 12px;
  color: var(--on-surface);
  animation: toast-in 0.2s ease;
}

.toast--success { border-color: var(--secondary); }
.toast--error { border-color: var(--error); }

@keyframes toast-in {
  from { opacity: 0; transform: translateY(8px); }
  to { opacity: 1; transform: none; }
}

/* --- Popover menu -------------------------------------------------------- */

.menu {
  position: fixed;
  z-index: 90;
  min-width: 220px;
  padding: var(--unit);
  display: flex;
  flex-direction: column;
  gap: 2px;
  background: var(--surface-container-high);
  border: 1px solid var(--outline-variant);
  border-radius: var(--radius);
  animation: menu-in 0.12s ease;
}

@keyframes menu-in {
  from { opacity: 0; transform: translateY(4px); }
  to { opacity: 1; transform: none; }
}

.menu__item {
  display: flex;
  align-items: center;
  gap: var(--stack-sm);
  width: 100%;
  padding: var(--stack-sm);
  border: none;
  border-radius: var(--radius);
  background: none;
  color: var(--on-surface-variant);
  font-family: var(--font-body);
  font-size: 14px;
  text-align: left;
  cursor: pointer;
  transition: background-color 0.12s ease, color 0.12s ease;
}

.menu__item:hover,
.menu__item:focus-visible {
  background: var(--surface-container-highest);
  color: var(--on-surface);
  outline: none;
}

.menu__label {
  flex: 1;
}

.menu__detail {
  color: var(--outline);
}

.menu__separator {
  height: 1px;
  margin: var(--unit) 0;
  background: var(--outline-variant);
}

/* --- Toggle switch ------------------------------------------------------- */

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

.switch__control {
  position: relative;
  flex-shrink: 0;
  cursor: pointer;
}

.switch__input {
  position: absolute;
  opacity: 0;
  width: 100%;
  height: 100%;
  margin: 0;
  cursor: pointer;
}

.switch__track {
  display: block;
  width: 40px;
  height: 22px;
  border-radius: var(--radius-full);
  background: var(--surface-container-highest);
  border: 1px solid var(--outline-variant);
  transition: background-color 0.15s ease, border-color 0.15s ease;
}

.switch__track::after {
  content: "";
  position: absolute;
  top: 4px;
  left: 4px;
  width: 14px;
  height: 14px;
  border-radius: var(--radius-full);
  background: var(--on-surface-variant);
  transition: transform 0.15s ease, background-color 0.15s ease;
}

.switch__input:checked + .switch__track {
  background: var(--primary);
  border-color: var(--primary);
}

.switch__input:checked + .switch__track::after {
  transform: translateX(18px);
  background: var(--on-primary);
}

.switch__input:focus-visible + .switch__track {
  outline: 2px solid var(--primary);
  outline-offset: 2px;
}

/* --- Badge --------------------------------------------------------------- */

.badge {
  padding: 2px var(--stack-sm);
  border-radius: var(--radius-chip);
  background: var(--secondary-container);
  color: var(--on-secondary-container);
  font-family: var(--font-code);
  font-size: 11px;
  font-weight: 600;
  letter-spacing: 0.05em;
  text-transform: uppercase;
}

.progress__bar--warn {
  background: var(--warning);
}

/* --- Sub-navigation (account / settings) -------------------------------- */

.tabs {
  display: flex;
  gap: var(--stack-sm);
  border-bottom: 1px solid var(--outline-variant);
}

.tabs__item {
  padding: var(--stack-sm) 12px;
  color: var(--on-surface-variant);
  border-bottom: 2px solid transparent;
  margin-bottom: -1px;
  transition: color 0.15s ease, border-color 0.15s ease;
}

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

.tabs__item[aria-current="page"] {
  color: var(--primary);
  border-bottom-color: var(--primary);
}

/* --- Loading placeholder ------------------------------------------------ */

.view-loading {
  flex: 1;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: var(--stack-lg);
}

.spinner {
  width: 24px;
  height: 24px;
  border-radius: var(--radius-full);
  border: 2px solid var(--outline-variant);
  border-top-color: var(--primary);
  animation: spin 0.7s linear infinite;
}

@keyframes spin {
  to { transform: rotate(360deg); }
}

@media (prefers-reduced-motion: reduce) {
  .spinner {
    animation-duration: 2.5s;
  }
}

/* Two fields sharing a row, for start/end dates and paired cost inputs. */
.field-row {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: var(--stack-sm);
}

/* Rows that navigate somewhere have to look and behave like it. */
.data-table tr.is-clickable {
  cursor: pointer;
}

.data-table tr.is-clickable:hover td {
  background: var(--surface-container-high);
}

.data-table tr.is-clickable:focus-visible {
  outline: 2px solid var(--primary);
  outline-offset: -2px;
}

/* The selected blotter row stays visible while you read the chart above it. */
.data-table tr.is-selected td {
  background: color-mix(in srgb, var(--primary) 14%, transparent);
}

/* --- Instrument picker ---------------------------------------------------- */

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

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

.picker__chips {
  display: flex;
  flex-wrap: wrap;
  gap: var(--unit);
}

.chip-token {
  display: inline-flex;
  align-items: center;
  gap: 4px;
  padding: 2px var(--unit);
  border-radius: var(--radius-sm);
  border: 1px solid var(--outline-variant);
  background: var(--surface-container-high);
  color: var(--on-surface);
  font-family: var(--font-code);
  font-size: 12px;
  cursor: pointer;
}

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

.picker__results {
  display: flex;
  flex-direction: column;
  max-height: 200px;
  overflow-y: auto;
  border: 1px solid var(--outline-variant);
  border-radius: var(--radius);
  background: var(--surface-container-lowest);
}

.picker__row {
  display: grid;
  grid-template-columns: 4.5rem minmax(0, 1fr) auto 1rem;
  align-items: center;
  gap: var(--stack-sm);
  padding: 6px var(--stack-sm);
  border: 0;
  border-bottom: 1px solid var(--outline-variant);
  background: none;
  color: var(--on-surface);
  text-align: left;
  cursor: pointer;
}

.picker__row:last-child { border-bottom: 0; }
.picker__row:hover { background: var(--surface-container-high); }
.picker__row.is-selected { background: color-mix(in srgb, var(--primary) 14%, transparent); }

.picker__name,
.picker__meta {
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.picker__empty { padding: var(--stack-sm); }

/* --- Jobs ----------------------------------------------------------------- */

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

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

.job--running { border-left-color: var(--primary); }
.job--done { border-left-color: var(--secondary); }
.job--failed { border-left-color: var(--error); }

.job__head,
.job__meta,
.job-progress__row {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: var(--stack-sm);
}

.job__label {
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.job-progress {
  display: flex;
  flex-direction: column;
  gap: 4px;
  margin-bottom: var(--stack-sm);
}

/* --- Notices --------------------------------------------------------------- */

.notice {
  display: flex;
  align-items: flex-start;
  gap: var(--stack-sm);
  padding: var(--stack-sm) var(--stack-md);
  border-radius: var(--radius);
  border: 1px solid var(--outline-variant);
  background: var(--surface-container-lowest);
}

.notice--warning {
  border-color: var(--warning);
  background: color-mix(in srgb, var(--warning) 10%, transparent);
  color: var(--warning);
}

.notice--info {
  border-color: var(--primary);
  background: color-mix(in srgb, var(--primary) 10%, transparent);
}

/* --- Selection bar --------------------------------------------------------- */

/* `hidden` has to win over the layout display, or the bar shows with nothing
   selected. */
.selection-bar[hidden] {
  display: none;
}

.selection-bar {
  display: flex;
  align-items: center;
  gap: var(--stack-sm);
  padding: var(--unit) var(--stack-md);
  border-radius: var(--radius);
  border: 1px solid var(--primary);
  background: color-mix(in srgb, var(--primary) 12%, transparent);
}

.checkbox {
  width: 15px;
  height: 15px;
  accent-color: var(--primary);
  cursor: pointer;
}

/* --- Small control variants ------------------------------------------------ */

.btn--sm {
  padding: 4px var(--stack-sm);
  font-size: 12px;
}

.icon-button--sm {
  width: 28px;
  height: 28px;
}

.input--inline {
  width: auto;
  min-width: 5.5rem;
  padding: 2px var(--unit);
  font-size: 12px;
}

/* --- Multi-series chart tooltip -------------------------------------------- */

.chart__tooltip--multi {
  display: flex;
  flex-direction: column;
  gap: 2px;
  top: var(--stack-sm);
}

.chart__tip-row {
  display: flex;
  align-items: center;
  gap: var(--unit);
  white-space: nowrap;
}

.chart__tip-label {
  flex: 1;
  color: var(--on-surface-variant);
}

/* --- Comparison tables ----------------------------------------------------- */

.compare-table th[scope="row"] {
  text-align: left;
  font-weight: 400;
  color: var(--on-surface-variant);
  white-space: nowrap;
}

.compare-table td.is-best {
  color: var(--secondary);
  background: color-mix(in srgb, var(--secondary) 12%, transparent);
}

/* --- Heatmap additions ----------------------------------------------------- */

.heatmap__cell--empty {
  background: repeating-linear-gradient(
    45deg,
    var(--surface-container-low) 0 4px,
    var(--surface-container-lowest) 4px 8px
  );
}

.heatmap__cell.is-clickable { cursor: pointer; }

.heatmap__cell.is-plateau {
  outline: 2px dashed var(--secondary);
  outline-offset: -2px;
}
