* { box-sizing: border-box; }
body {
  font-family: -apple-system, system-ui, sans-serif;
  margin: 0;
  background: #0f1218;
  color: #e6e6e6;
}
header {
  padding: 1rem 2rem;
  background: #1a1f2b;
  border-bottom: 1px solid #2a3040;
}
header h1 { margin: 0; font-size: 1.2rem; display: inline-block; }
.app-nav { display: inline-block; margin-left: 1.5rem; font-size: 0.9rem; }
.app-nav a {
  color: #8892a6;
  text-decoration: none;
  margin-right: 1rem;
  padding-bottom: 2px;
  border-bottom: 1px solid transparent;
}
.app-nav a:hover { color: #e6e6e6; border-bottom-color: #2a3040; }
main { padding: 1rem 2rem; max-width: none; }
.panel {
  background: #1a1f2b;
  border: 1px solid #2a3040;
  border-radius: 6px;
  padding: 1rem 1.25rem;
  margin-bottom: 1rem;
}
.panel h2 { margin-top: 0; font-size: 1.05rem; }
.muted { color: #8892a6; font-size: 0.85rem; }
.err { color: #ff6b6b; font-size: 0.8rem; }

.info-tip {
  display: inline-flex;
  position: relative;
  width: 1.1em;
  height: 1.1em;
  align-items: center;
  justify-content: center;
  border: 1px solid #6b7585;
  border-radius: 50%;
  color: #8892a6;
  background: transparent;
  font-size: 0.7rem;
  font-weight: 600;
  font-style: italic;
  font-family: Georgia, serif;
  cursor: help;
  user-select: none;
  margin-left: 0.4em;
  vertical-align: middle;
}
.info-tip > .info-tip-body {
  display: none;
  position: absolute;
  z-index: 100;
  left: 1.6em;
  top: -0.3em;
  min-width: 32rem;
  max-width: 56rem;
  background: #2a3040;
  color: #e6e6e6;
  border: 1px solid #3a4258;
  border-radius: 4px;
  padding: 0.65rem 0.85rem;
  font-size: 0.82rem;
  font-style: normal;
  font-weight: normal;
  font-family: -apple-system, system-ui, sans-serif;
  line-height: 1.5;
  text-align: left;
  white-space: normal;
  cursor: auto;
  box-shadow: 0 4px 14px rgba(0,0,0,0.5);
}
.info-tip:hover > .info-tip-body,
.info-tip:focus > .info-tip-body,
.info-tip:focus-within > .info-tip-body { display: block; }

.info-tip.info-tip-wide > .info-tip-body {
  min-width: 50vw;
  max-width: 90vw;
}

.events-table {
  width: 100%;
  table-layout: fixed;
  border-collapse: collapse;
  font-size: 0.85rem;
}
/* Fixed-layout columns: the first five are sized to their content
   needs; the description column gets `auto` so it claims the rest of
   the row and ellipsizes overflow. */
.events-table col.ev-col-ticker { width: 4.5em; }
.events-table col.ev-col-date   { width: 9em; }
.events-table col.ev-col-cat    { width: 11em; }
.events-table col.ev-col-desc   { width: auto; }
.events-table td {
  border-bottom: 1px solid #2a3040;
  padding: 0.5rem 0.6rem;
  vertical-align: middle;
}
.events-table tr.events-row { cursor: pointer; }
.events-table tr.events-row:hover { background: #232938; }
.events-table tr.events-row:hover td { color: #fff; }
.events-table .ev-ticker { font-weight: 600; }
.events-table .ev-range  { color: #8892a6; font-variant-numeric: tabular-nums; }
.events-table .ev-count  { color: #8892a6; font-size: 0.75rem; }
.events-table .ev-cat    {
  display: inline-block;
  font-variant-numeric: tabular-nums;
}
.events-table .ev-col { white-space: nowrap; }
.events-table .ev-desc {
  color: #8892a6;
  position: relative;
  overflow: visible;  /* let the popover escape the cell */
}
.events-table .ev-desc .ev-desc-inner {
  display: block;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}
.events-table .ev-desc .ev-desc-tip {
  display: none;
  position: absolute;
  z-index: 50;
  /* Anchor all four edges to the cell so the popover takes the cell's
     exact width (height grows downward as long text wraps). */
  top: 0;
  left: 0;
  right: 0;
  background: #2a3040;
  color: #e6e6e6;
  border: 1px solid #3a4258;
  border-radius: 4px;
  padding: 0.6rem 0.8rem;
  font-size: 0.82rem;
  line-height: 1.45;
  white-space: normal;
  text-align: left;
  box-shadow: 0 4px 14px rgba(0,0,0,0.5);
}
.events-table .ev-desc:hover .ev-desc-tip,
.events-table .ev-desc:focus-within .ev-desc-tip { display: block; }
.events-table .ev-cat .ev-tag { color: #8892a6; margin-right: 0.3em; }
.events-table .ev-cat .ev-pct { color: #8892a6; margin-left: 0.4em; font-size: 0.75rem; }
.events-table .ev-up   { color: #4caf50; }
.events-table .ev-down { color: #ff6b6b; }
.events-table .ev-neutral { color: #e6e6e6; }
.events-empty { color: #8892a6; padding: 0.5rem 0; }
.badge {
  display: inline-block;
  font-size: 0.7rem;
  padding: 0.1em 0.5em;
  margin-left: 0.4em;
  background: #2a3040;
  border-radius: 3px;
  color: #8892a6;
}

table { border-collapse: collapse; width: 100%; font-size: 0.85rem; }
th, td {
  text-align: left;
  padding: 0.4rem 0.6rem;
  border-bottom: 1px solid #2a3040;
  vertical-align: top;
}
th { color: #8892a6; font-weight: 500; }
.ticker-table .drag { cursor: grab; user-select: none; color: #8892a6; }
.ticker-table .cell { min-width: 110px; }

.special-row {
  display: flex;
  align-items: center;
  gap: 0.75rem;
  padding: 0.35rem 0;
  border-bottom: 1px solid #2a3040;
}
.special-row .special-label {
  color: #8892a6;
  font-size: 0.8rem;
  min-width: 80px;
}
.special-row input[type=text] {
  font-weight: 600;
  width: 120px;
}
.ticker-list { margin-top: 0.75rem; width: auto; }
.ticker-list .drag { cursor: grab; user-select: none; color: #8892a6; width: 1.5em; }
.ticker-list td { border: none; padding: 0.25rem 0.5rem; }

.download-table .cell { min-width: 200px; }
.download-table .range { font-size: 0.8rem; margin-bottom: 0.15rem; white-space: nowrap; }
.download-row { margin: 0.5rem 0; }
.subsection {
  margin-top: 1rem;
  padding-top: 0.75rem;
  border-top: 1px solid #2a3040;
}

.collapsible > summary {
  cursor: pointer;
  list-style: none;
  display: flex;
  align-items: center;
  gap: 0.4rem;
  user-select: none;
}
.collapsible > summary::-webkit-details-marker { display: none; }
.collapsible > summary::before {
  content: "▸";
  color: #8892a6;
  font-size: 0.85rem;
  transition: transform 0.15s ease;
  display: inline-block;
}
.collapsible[open] > summary::before { transform: rotate(90deg); }
.collapsible > summary .section-title {
  font-size: 1.05rem;
  font-weight: 600;
}
/* When a form (or a .beta-controls row) sits directly under a section
   summary — no introductory paragraph in between — the form's small
   top margin reads as cramped. Match the breathing room paragraphs
   would otherwise provide. */
.collapsible > summary + form,
.collapsible > summary + .beta-controls {
  margin-top: 0.85rem;
}

.beta-controls {
  display: flex;
  align-items: center;
  gap: 1rem;
  flex-wrap: wrap;
  margin: 0.5rem 0 0.75rem;
}
.beta-controls input[type=number] { width: 5em; }
.beta-hint { font-style: italic; }

.job-grid td { min-width: 120px; vertical-align: top; }
.cell-status { font-weight: 600; }
.cell-last { font-family: ui-monospace, SFMono-Regular, Menlo, monospace; font-size: 0.75rem; }
.status-queued .cell-status { color: #8892a6; }
.status-running .cell-status { color: #f5a623; }
.status-done .cell-status { color: #4caf50; }
.status-failed .cell-status { color: #ff6b6b; }
.status-queued, .status-running, .status-done, .status-failed, .status-partial { }
.status-running { animation: pulse 1.5s ease-in-out infinite; }
@keyframes pulse { 0%,100% { opacity: 1 } 50% { opacity: 0.55 } }

.job-meta { margin: 0.75rem 0; }
.progress-bar {
  position: relative;
  height: 22px;
  background: #0f1218;
  border: 1px solid #2a3040;
  border-radius: 4px;
  overflow: hidden;
}
.progress-fill {
  height: 100%;
  background: linear-gradient(90deg, #2a3d6b, #3a5091);
  transition: width 0.4s ease;
}
.progress-text {
  position: absolute;
  inset: 0;
  display: flex;
  align-items: center;
  justify-content: center;
  color: #e6e6e6;
  font-size: 0.8rem;
  font-weight: 500;
}

form fieldset { border: 1px solid #2a3040; border-radius: 4px; padding: 0.5rem 1rem; }
form label { margin-right: 1rem; }
input[type=text], input[type=date] {
  background: #0f1218;
  border: 1px solid #2a3040;
  color: #e6e6e6;
  padding: 0.35rem 0.5rem;
  border-radius: 3px;
}
button {
  background: #2a3d6b;
  color: #fff;
  border: 1px solid #3a5091;
  padding: 0.4rem 0.9rem;
  border-radius: 3px;
  cursor: pointer;
}
button:hover { background: #355088; }

.log {
  background: #0f1218;
  padding: 0.5rem;
  max-height: 300px;
  overflow: auto;
  font-size: 0.75rem;
}

/* Factor analysis ticker section --------------------------------------- */

.factor-section { margin-bottom: 1.25rem; }
.factor-section:last-child { margin-bottom: 0; }
.factor-section-title {
  margin: 0 0 0.5rem 0;
  font-size: 0.85rem;
  text-transform: uppercase;
  letter-spacing: 0.05em;
  color: #8892a6;
}

.factor-reference input[type=text] {
  font-weight: 600;
  font-size: 1rem;
  width: 8em;
}

.factor-indexes { width: auto; }
.factor-indexes td {
  padding: 0.3rem 0.6rem;
  border-bottom: none;
  vertical-align: middle;
}
.factor-indexes .drag {
  cursor: grab;
  user-select: none;
  color: #8892a6;
  width: 1.5em;
}
.factor-indexes .ticker-name { min-width: 6em; }
.factor-indexes thead th {
  font-size: 0.7rem;
  text-transform: uppercase;
  letter-spacing: 0.05em;
  color: #8892a6;
  font-weight: 500;
  padding: 0.2rem 0.6rem;
  border-bottom: 1px solid #2a3040;
  text-align: left;
}
.factor-indexes thead th.select-col,
.factor-indexes thead th.residual-col { text-align: center; }
.factor-indexes .select-cell,
.factor-indexes .residual-cell { text-align: center; width: 5em; }
.factor-indexes .select-cell input,
.factor-indexes .residual-cell input { cursor: pointer; vertical-align: middle; }

.remove-cell { width: 1.5em; }
.remove-btn {
  background: transparent;
  border: none;
  color: #8892a6;
  font-size: 1rem;
  line-height: 1;
  padding: 0 0.3rem;
  cursor: pointer;
}
.remove-btn:hover { color: #ff6b6b; background: transparent; }

.factor-comps-grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(140px, 1fr));
  gap: 0.4rem 0.6rem;
  margin-bottom: 0.6rem;
}
.comp-tile {
  display: flex;
  align-items: center;
  justify-content: space-between;
  background: #0f1218;
  border: 1px solid #2a3040;
  border-radius: 3px;
  padding: 0.3rem 0.5rem;
}
.comp-tile .drag {
  cursor: grab;
  user-select: none;
  color: #8892a6;
  margin-right: 0.4em;
  font-size: 0.85rem;
}

.ticker-narrow-placeholder {
  display: flex;
  align-items: center;
  gap: 0.6em;
  padding: 0.6em 0.5em;
  color: #8892a6;
  font-size: 0.9rem;
  font-style: italic;
}
.ticker-narrow-placeholder[hidden] { display: none; }
.narrow-spinner {
  display: inline-block;
  width: 1em;
  height: 1em;
  border: 2px solid #2a3040;
  border-top-color: #e6e6e6;
  border-radius: 50%;
  animation: narrow-spin 0.8s linear infinite;
  flex: 0 0 auto;
}
@keyframes narrow-spin { to { transform: rotate(360deg); } }
.comp-label {
  display: flex;
  align-items: center;
  gap: 0.4em;
  font-size: 0.85rem;
  cursor: pointer;
  flex: 1;
  overflow: hidden;
  margin: 0;
}
.comp-ticker { font-weight: 500; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }

.add-form {
  display: flex;
  gap: 0.4rem;
  margin-top: 0.4rem;
}
.add-form input[type=text] { flex: 0 1 16em; }
.add-form-wide input[type=text] { flex: 1 1 auto; max-width: 32em; }
.warn { color: #f5a623; font-size: 0.75rem; margin-top: 0.15rem; }

.svd-subhead { font-size: 0.85rem; text-transform: uppercase; letter-spacing: 0.05em; color: #8892a6; margin: 1rem 0 0.4rem; }
.svd-eigenvalue-table { width: auto; font-size: 0.85rem; border-collapse: collapse; }
.svd-eigenvalue-table th { color: #8892a6; font-weight: 500; padding: 0.15em 0.55em; text-align: left; white-space: nowrap; }
.svd-eigenvalue-table td { font-family: ui-monospace, SFMono-Regular, Menlo, monospace; padding: 0.15em 0.55em; white-space: nowrap; }
.svd-signal { color: #4caf50; }
.svd-noise { color: #8892a6; }
.svd-eigenvalue-scroll { overflow-x: auto; padding-bottom: 0.4rem; }
.svd-eigenvalue-table th.loader-group { text-align: center; border-left: 1px solid #2a3040; }
.svd-eigenvalue-table th.loader-ticker, .svd-eigenvalue-table td.loader-ticker { color: #cfd6e4; font-size: 0.75rem; }
.svd-eigenvalue-table th.loader-weight, .svd-eigenvalue-table td.loader-weight { font-size: 0.75rem; text-align: right; padding-right: 0.4em; }
.svd-eigenvalue-table td.loader-pos { color: #4caf50; }
.svd-eigenvalue-table td.loader-neg { color: #d62728; }

/* Tabbed dashboard. Only applied when an ancestor carries data-tabbed; */
/* otherwise .tab-panel is a transparent wrapper that doesn't affect layout. */
.tab-nav {
  display: flex;
  gap: 0.25rem;
  margin-bottom: 1rem;
  border-bottom: 1px solid #2a3040;
}
.tab-nav button {
  background: transparent;
  color: #8892a6;
  border: 1px solid transparent;
  border-bottom: none;
  border-radius: 6px 6px 0 0;
  padding: 0.5rem 1rem;
  cursor: pointer;
  font-size: 0.9rem;
  font-family: inherit;
  margin-bottom: -1px;
}
.tab-nav button:hover { color: #e6e6e6; }
.tab-nav button.active {
  color: #e6e6e6;
  background: #1a1f2b;
  border-color: #2a3040;
}
[data-tabbed] .tab-panel { display: none; }
[data-tabbed] .tab-panel.active { display: block; }
