:root {
  --bg: #0f1420;
  --panel: #171e2e;
  --border: #2a3346;
  --text: #e6e9f0;
  --muted: #93a0b8;
  --accent: #4f8cff;
  --green: #2ecc71;
  --red: #ff5c5c;
  --violet: #a78bfa;
  --amber: #f5b942;
}

* { box-sizing: border-box; }

:focus-visible {
  outline: 2px solid var(--accent);
  outline-offset: 2px;
}

body {
  margin: 0;
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, sans-serif;
  background: var(--bg);
  color: var(--text);
}

header {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  gap: 8px 12px;
  padding: 20px 24px;
  border-bottom: 1px solid var(--border);
}

header h1 { font-size: 20px; margin: 0; flex-shrink: 0; }

.main-nav { display: flex; gap: 4px; }
.main-nav a {
  color: var(--muted);
  text-decoration: none;
  font-size: 13px;
  font-weight: 600;
  padding: 6px 12px;
  border-radius: 6px;
}
.main-nav a:hover { color: var(--text); background: var(--panel); }
.main-nav a.active { color: var(--text); background: var(--panel); border: 1px solid var(--border); }

#logout-form { margin-left: auto; }
button#logout-button {
  background: transparent;
  color: var(--muted);
  border: 1px solid var(--border);
  padding: 6px 14px;
  border-radius: 6px;
  font-size: 13px;
  cursor: pointer;
}
button#logout-button:hover { color: var(--text); border-color: var(--muted); }

.badge {
  font-size: 12px;
  padding: 3px 10px;
  border-radius: 999px;
  border: 1px solid var(--border);
  color: var(--muted);
}
.badge.live { color: var(--green); border-color: var(--green); }
.badge.mock { color: #f5b942; border-color: #f5b942; }

.data-asof {
  font-size: 12px;
  color: var(--muted);
}

.kite-status {
  display: flex;
  align-items: center;
  gap: 10px;
  font-size: 13px;
  color: var(--muted);
  margin-bottom: 12px;
}
.kite-status.hidden { display: none; }
.kite-status a { color: inherit; text-decoration: underline; }

#filters, #filters-momentum, #filters-reversal {
  padding: 16px 24px 20px;
  background: var(--panel);
  border-bottom: 1px solid var(--border);
}

.filter-group-label {
  margin: 0 0 12px;
  font-size: 11px;
  font-weight: 700;
  letter-spacing: 0.06em;
  text-transform: uppercase;
  color: var(--muted);
}
.filter-group-note {
  text-transform: none;
  letter-spacing: normal;
  font-weight: 400;
  opacity: 0.8;
}

.filter-row {
  display: flex;
  flex-wrap: wrap;
  gap: 16px;
  align-items: end;
}

.field { display: flex; flex-direction: column; gap: 6px; min-width: 140px; }
.checkbox-field { flex-direction: row; align-items: center; min-width: auto; }
.checkbox-field label { display: flex; align-items: center; gap: 6px; font-size: 13px; }

label { font-size: 12px; color: var(--muted); }

input, select {
  background: var(--bg);
  border: 1px solid var(--border);
  color: var(--text);
  padding: 8px 10px;
  border-radius: 6px;
  font-size: 14px;
}

button#run-screen {
  background: var(--accent);
  color: white;
  border: none;
  padding: 10px 20px;
  border-radius: 6px;
  font-size: 14px;
  font-weight: 600;
  cursor: pointer;
  height: 38px;
}
button#run-screen:hover { opacity: 0.9; }
button#run-screen:disabled { opacity: 0.5; cursor: default; }

button#preset-institutional {
  background: transparent;
  color: var(--accent);
  border: 1px solid var(--accent);
  padding: 10px 20px;
  border-radius: 6px;
  font-size: 13px;
  font-weight: 600;
  cursor: pointer;
  height: 38px;
}
button#preset-institutional:hover { background: rgba(79, 140, 255, 0.1); }

#status {
  padding: 8px 24px;
  font-size: 13px;
  color: var(--muted);
  min-height: 20px;
  display: flex;
  align-items: center;
  gap: 8px;
}
#status.error { color: var(--red); }

#status.loading::before {
  content: "";
  width: 13px;
  height: 13px;
  border: 2px solid var(--border);
  border-top-color: var(--accent);
  border-radius: 50%;
  animation: spin 0.7s linear infinite;
  flex-shrink: 0;
}

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

@media (prefers-reduced-motion: reduce) {
  #status.loading::before { animation: none; }
}

#results { padding: 0 24px 24px; overflow-x: auto; max-height: 70vh; overflow-y: auto; }

#results-table.loading { opacity: 0.4; transition: opacity 0.15s ease; }

table { width: 100%; border-collapse: collapse; font-size: 13px; }

th, td {
  text-align: left;
  padding: 10px 12px;
  border-bottom: 1px solid var(--border);
  white-space: nowrap;
}
td { font-variant-numeric: tabular-nums; }

th {
  color: var(--muted);
  font-weight: 600;
  cursor: pointer;
  user-select: none;
  position: sticky;
  top: 0;
  background: var(--bg);
  z-index: 1;
}
th:hover { color: var(--text); }
th[data-sort]::after { content: ""; display: inline-block; width: 10px; }
th.sorted-asc, th.sorted-desc { color: var(--text); }
th.sorted-asc::after { content: "▲"; font-size: 9px; margin-left: 4px; color: var(--accent); }
th.sorted-desc::after { content: "▼"; font-size: 9px; margin-left: 4px; color: var(--accent); }

tbody tr:hover td { background: rgba(147, 160, 184, 0.06); }

.expandable-row { cursor: pointer; }
.detail-row td {
  background: var(--panel);
  border-top: 1px solid var(--border);
  padding: 16px;
}
.detail-row.hidden { display: none; }

td.positive { color: var(--green); }
td.negative { color: var(--red); }

.signal-badge {
  display: inline-block;
  padding: 2px 8px;
  border-radius: 4px;
  font-size: 11px;
  font-weight: 600;
  white-space: nowrap;
}
.signal-strong-buy { background: rgba(46, 204, 113, 0.2); color: var(--green); }
.signal-buy { background: rgba(46, 204, 113, 0.1); color: var(--green); }
.signal-neutral { background: rgba(147, 160, 184, 0.15); color: var(--muted); }
.signal-sell { background: rgba(255, 92, 92, 0.1); color: var(--red); }
.signal-strong-sell { background: rgba(255, 92, 92, 0.2); color: var(--red); }
.signal-unchecked { background: rgba(147, 160, 184, 0.1); color: var(--muted); font-weight: 500; }

/* Deliberately distinct hues from Signal (green/red) -- Reversal is an
   independent, opposite-strategy read and must not look like agreement. */
.reversal-buy { background: rgba(167, 139, 250, 0.2); color: var(--violet); }
.reversal-oversold { background: rgba(245, 185, 66, 0.15); color: var(--amber); }

.fundamentals-col { display: none; }
.fundamentals-col.visible { display: table-cell; }

footer {
  padding: 16px 24px;
  font-size: 12px;
  color: var(--muted);
  border-top: 1px solid var(--border);
}

#portfolio-section { padding: 24px; }
.table-scroll { overflow-x: auto; }
.portfolio-note { font-size: 12px; color: var(--muted); margin: 0 0 16px; max-width: 640px; }

.portfolio-form {
  display: flex;
  flex-wrap: wrap;
  gap: 16px;
  align-items: end;
  margin-bottom: 28px;
}
.portfolio-form button {
  background: var(--accent);
  color: white;
  border: none;
  padding: 10px 20px;
  border-radius: 6px;
  font-size: 14px;
  font-weight: 600;
  cursor: pointer;
  height: 38px;
}
.portfolio-form button:hover { opacity: 0.9; }
.portfolio-form button:disabled { opacity: 0.5; cursor: default; }

.field-symbol { position: relative; }
.symbol-status {
  position: absolute;
  top: 100%;
  left: 0;
  margin-top: 4px;
  font-size: 12px;
  white-space: nowrap;
}
.symbol-status.checking { color: var(--muted); }
.symbol-status.valid { color: var(--green); }
.symbol-status.invalid { color: var(--red); }

#portfolio-status {
  padding: 4px 0 12px;
  font-size: 13px;
  color: var(--muted);
}
#portfolio-status.error { color: var(--red); }

button.delete-holding, button.delete-watchlist-item {
  background: transparent;
  color: var(--red);
  border: 1px solid var(--red);
  padding: 4px 10px;
  border-radius: 4px;
  font-size: 12px;
  cursor: pointer;
}
button.delete-holding:hover, button.delete-watchlist-item:hover { background: rgba(255, 92, 92, 0.1); }

button.watch-button {
  background: transparent;
  color: var(--accent);
  border: 1px solid var(--accent);
  padding: 4px 10px;
  border-radius: 4px;
  font-size: 12px;
  cursor: pointer;
  white-space: nowrap;
}
button.watch-button:hover { background: rgba(79, 140, 255, 0.1); }
button.watch-button:disabled { cursor: default; }
button.watch-button.watching {
  color: var(--green);
  border-color: var(--green);
  background: rgba(46, 204, 113, 0.1);
  opacity: 1;
}

/* Help page -- a reading page, so it gets a constrained line length and
   its own table treatment instead of the dense sortable-dashboard one. */
.help-content { max-width: 760px; margin: 0 auto; padding: 32px 24px 64px; }

.help-lead { font-size: 15px; color: var(--muted); margin: 0 0 20px; line-height: 1.6; }

.help-toc {
  display: flex;
  flex-wrap: wrap;
  gap: 8px 20px;
  padding: 16px 20px;
  margin: 0 0 40px;
  background: var(--panel);
  border: 1px solid var(--border);
  border-radius: 8px;
}
.help-toc a { color: var(--accent); text-decoration: none; font-size: 13px; }
.help-toc a:hover { text-decoration: underline; }

.help-content section { margin-bottom: 40px; }
.help-content h2 {
  font-size: 16px;
  margin: 0 0 12px;
  padding-bottom: 8px;
  border-bottom: 1px solid var(--border);
}
.help-content p { line-height: 1.6; margin: 0 0 12px; }
.help-content p:last-child { margin-bottom: 0; }

.help-note { font-size: 13px; color: var(--muted); }

.help-list { line-height: 1.8; margin: 0 0 12px; padding-left: 20px; }

table.reference-table {
  width: 100%;
  border-collapse: collapse;
  font-size: 13px;
  margin: 0 0 12px;
}
table.reference-table th, table.reference-table td {
  text-align: left;
  padding: 10px 12px;
  border-bottom: 1px solid var(--border);
  white-space: normal;
  vertical-align: top;
}
table.reference-table th {
  color: var(--muted);
  font-weight: 600;
  cursor: default;
  user-select: text;
  position: static;
  background: transparent;
}
table.reference-table td:first-child { color: var(--text); font-weight: 600; white-space: nowrap; }
table.reference-table code {
  background: var(--bg);
  border: 1px solid var(--border);
  border-radius: 4px;
  padding: 1px 5px;
  font-size: 12px;
}

.filter-guide { margin-bottom: 28px; padding-bottom: 24px; border-bottom: 1px solid var(--border); }
.filter-guide:last-child { border-bottom: none; margin-bottom: 0; padding-bottom: 0; }
.filter-guide h3, .help-content h3 { font-size: 14px; margin: 20px 0 8px; color: var(--text); }
.filter-guide h3 { margin-top: 0; }

.help-suggested {
  font-size: 13px;
  color: var(--muted);
  background: var(--panel);
  border: 1px solid var(--border);
  border-radius: 6px;
  padding: 10px 14px;
  margin: 10px 0 0;
  line-height: 1.6;
}
.help-suggested strong { color: var(--accent); font-weight: 600; }

/* Analyzer page */
.hidden { display: none; }

#analyzer-section { padding: 24px; }

.analyzer-results { margin-top: 24px; }

.analyzer-headline {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  padding-bottom: 16px;
  margin-bottom: 16px;
  border-bottom: 1px solid var(--border);
}
.analyzer-headline-main { display: flex; align-items: baseline; gap: 12px; }
.analyzer-symbol { font-size: 22px; font-weight: 700; }
.analyzer-price { font-size: 18px; font-variant-numeric: tabular-nums; }
.analyzer-change { font-size: 14px; font-weight: 600; }
.analyzer-change.positive { color: var(--green); }
.analyzer-change.negative { color: var(--red); }
.analyzer-headline-badges { display: flex; gap: 8px; }
.analyzer-headline-badges .signal-badge { font-size: 13px; padding: 4px 12px; }

.news-agreement {
  font-size: 13px;
  line-height: 1.6;
  padding: 12px 16px;
  border-radius: 8px;
  margin-bottom: 20px;
  background: var(--panel);
  border: 1px solid var(--border);
  color: var(--text);
}
.news-agreement.aligned { border-color: var(--green); }
.news-agreement.conflicting { border-color: var(--amber); }
.news-agreement.neutral, .news-agreement.no-news { color: var(--muted); }

.analyzer-stats {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(130px, 1fr));
  gap: 12px;
  margin-bottom: 28px;
}
.stat-tile {
  display: flex;
  flex-direction: column;
  gap: 4px;
  padding: 10px 14px;
  background: var(--panel);
  border: 1px solid var(--border);
  border-radius: 8px;
}
.stat-label { font-size: 11px; color: var(--muted); text-transform: uppercase; letter-spacing: 0.04em; }
.stat-value { font-size: 15px; font-weight: 600; font-variant-numeric: tabular-nums; color: var(--text); }
.stat-value.positive { color: var(--green); }
.stat-value.negative { color: var(--red); }
.stat-value.warning { color: var(--amber); }

.kpi-tally {
  font-size: 13px;
  line-height: 1.6;
  padding: 10px 16px;
  border-radius: 8px;
  margin-bottom: 12px;
  background: var(--panel);
  border: 1px solid var(--border);
  color: var(--muted);
}
.kpi-tally:empty { display: none; }
.kpi-tally.positive { border-color: var(--green); color: var(--text); }
.kpi-tally.negative { border-color: var(--red); color: var(--text); }

.analyzer-news-heading { font-size: 15px; margin: 0 0 12px; }

.news-list { list-style: none; margin: 0; padding: 0; }
.news-item {
  padding: 12px 0;
  border-bottom: 1px solid var(--border);
}
.news-item:last-child { border-bottom: none; }
.news-item-title {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  gap: 8px;
  margin-bottom: 4px;
}
.news-item-title a {
  color: var(--text);
  text-decoration: none;
  font-size: 14px;
  font-weight: 600;
}
.news-item-title a:hover { color: var(--accent); text-decoration: underline; }
.news-item-title .signal-badge { font-size: 10px; padding: 1px 7px; text-transform: capitalize; }
.news-item-meta { font-size: 12px; color: var(--muted); }
