/* ============================================================
   Stock Screener — dark product dashboard
   Restrained palette: cool slate surfaces + one indigo accent.
   Gain/loss green/red are data-meaningful, not decoration.
   ============================================================ */

:root {
  /* Surfaces — cool slate, layered light-to-dark by elevation */
  --bg:         oklch(0.20 0.012 255);
  --surface:    oklch(0.24 0.014 255);
  --surface-2:  oklch(0.28 0.016 255);
  --border:     oklch(0.36 0.018 255);
  --border-strong: oklch(0.45 0.02 255);

  /* Ink — verified ≥4.5:1 on --bg/--surface */
  --ink:        oklch(0.97 0.004 255);
  --ink-muted:  oklch(0.78 0.012 255);
  --ink-faint:  oklch(0.66 0.014 255);

  /* Accent — primary action, focus, current selection only */
  --accent:     oklch(0.66 0.15 258);
  --accent-strong: oklch(0.72 0.15 258);
  --accent-ink: oklch(0.20 0.03 258);
  --accent-soft: oklch(0.66 0.15 258 / 0.14);

  /* Semantic performance colors */
  --gain:       oklch(0.78 0.16 152);
  --gain-soft:  oklch(0.78 0.16 152 / 0.13);
  --loss:       oklch(0.72 0.18 24);
  --loss-soft:  oklch(0.72 0.18 24 / 0.14);

  --radius:     10px;
  --radius-sm:  7px;
  --shadow:     0 8px 24px oklch(0.12 0.02 255 / 0.5);

  --font-ui: system-ui, -apple-system, "Segoe UI", Roboto, Helvetica, Arial, sans-serif;
  --font-mono: ui-monospace, "SF Mono", "Cascadia Code", "JetBrains Mono", Consolas, monospace;

  /* z-index scale */
  --z-sticky: 10;
  --z-overlay: 20;

  --ease-out: cubic-bezier(0.22, 1, 0.36, 1);
}

* { box-sizing: border-box; }

html, body { height: 100%; }

body {
  margin: 0;
  background: var(--bg);
  color: var(--ink);
  font-family: var(--font-ui);
  font-size: 14px;
  line-height: 1.5;
  -webkit-font-smoothing: antialiased;
  text-rendering: optimizeLegibility;
}

h1, h2 { margin: 0; font-weight: 600; letter-spacing: -0.01em; }

/* ── Top bar ───────────────────────────────────────────── */
.topbar {
  position: sticky;
  top: 0;
  z-index: var(--z-sticky);
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 16px;
  padding: 14px 24px;
  background: oklch(0.22 0.013 255 / 0.85);
  backdrop-filter: blur(8px);
  border-bottom: 1px solid var(--border);
}

.brand { display: flex; align-items: center; gap: 11px; }
.brand h1 { font-size: 1.0625rem; }
.brand__mark {
  display: grid;
  place-items: center;
  width: 30px; height: 30px;
  border-radius: var(--radius-sm);
  background: var(--accent-soft);
  color: var(--accent-strong);
}

.freshness {
  display: flex;
  align-items: center;
  gap: 8px;
  font-size: 0.8125rem;
  color: var(--ink-muted);
  font-variant-numeric: tabular-nums;
}
.freshness__dot {
  width: 8px; height: 8px;
  border-radius: 50%;
  background: var(--ink-faint);
  flex: none;
}
.freshness__dot[data-state="ok"]    { background: var(--gain); box-shadow: 0 0 0 3px var(--gain-soft); }
.freshness__dot[data-state="error"] { background: var(--loss); box-shadow: 0 0 0 3px var(--loss-soft); }
.freshness__dot[data-state="loading"] { background: var(--accent); animation: pulse 1.2s var(--ease-out) infinite; }

@keyframes pulse { 50% { opacity: 0.35; } }

/* ── Layout ────────────────────────────────────────────── */
.layout {
  max-width: 1320px;
  margin: 0 auto;
  padding: 22px 24px 64px;
  display: flex;
  flex-direction: column;
  gap: 20px;
}

/* ── Controls toolbar ──────────────────────────────────── */
.controls {
  display: flex;
  flex-wrap: wrap;
  align-items: flex-end;
  gap: 14px 16px;
  padding: 18px;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
}

.field { display: flex; flex-direction: column; gap: 6px; min-width: 150px; }
.field--narrow { min-width: 104px; flex: 0 0 auto; }

.field label, .field__label {
  font-size: 0.75rem;
  font-weight: 500;
  color: var(--ink-muted);
  letter-spacing: 0.01em;
}
.field__unit { color: var(--ink-faint); font-weight: 400; }

input, select {
  font-family: inherit;
  font-size: 0.875rem;
  color: var(--ink);
  background: var(--surface-2);
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  padding: 9px 11px;
  height: 38px;
  transition: border-color 0.15s var(--ease-out), box-shadow 0.15s var(--ease-out);
}
input::placeholder { color: var(--ink-faint); }
input:hover, select:hover { border-color: var(--border-strong); }
input:focus-visible, select:focus-visible {
  outline: none;
  border-color: var(--accent);
  box-shadow: 0 0 0 3px var(--accent-soft);
}
/* dark-friendly native date/number controls */
input[type="date"]::-webkit-calendar-picker-indicator { filter: invert(0.7); cursor: pointer; }
select { appearance: none; padding-right: 30px;
  background-image: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' width='12' height='12' viewBox='0 0 24 24' fill='none' stroke='%238b93a7' stroke-width='2.5' stroke-linecap='round' stroke-linejoin='round'%3E%3Cpath d='M6 9l6 6 6-6'/%3E%3C/svg%3E");
  background-repeat: no-repeat; background-position: right 10px center;
}

/* Segmented control (sort) */
.segmented {
  display: inline-flex;
  background: var(--surface-2);
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  padding: 3px;
  height: 38px;
}
.segmented input { position: absolute; opacity: 0; pointer-events: none; }
.segmented label {
  display: grid;
  place-items: center;
  padding: 0 14px;
  font-size: 0.8125rem;
  font-weight: 500;
  color: var(--ink-muted);
  border-radius: 5px;
  cursor: pointer;
  white-space: nowrap;
  transition: color 0.15s var(--ease-out), background 0.15s var(--ease-out);
}
.segmented label:hover { color: var(--ink); }
.segmented input:checked + label { background: var(--accent); color: var(--accent-ink); font-weight: 600; }
.segmented input:focus-visible + label { box-shadow: 0 0 0 3px var(--accent-soft); }

.controls__actions { display: flex; gap: 10px; margin-left: auto; align-self: flex-end; }

/* ── Buttons ───────────────────────────────────────────── */
.btn {
  font-family: inherit;
  font-size: 0.875rem;
  font-weight: 600;
  height: 38px;
  padding: 0 18px;
  border-radius: var(--radius-sm);
  border: 1px solid transparent;
  cursor: pointer;
  transition: background 0.15s var(--ease-out), border-color 0.15s var(--ease-out), transform 0.06s var(--ease-out);
}
.btn:active { transform: translateY(1px); }
.btn:focus-visible { outline: none; box-shadow: 0 0 0 3px var(--accent-soft); }
.btn--primary { background: var(--accent); color: var(--accent-ink); }
.btn--primary:hover { background: var(--accent-strong); }
.btn--primary:disabled { background: var(--surface-2); color: var(--ink-faint); cursor: progress; transform: none; }
.btn--ghost { background: transparent; color: var(--ink-muted); border-color: var(--border); }
.btn--ghost:hover { color: var(--ink); border-color: var(--border-strong); background: var(--surface-2); }

/* ── Results ───────────────────────────────────────────── */
.results__head {
  display: flex;
  align-items: baseline;
  justify-content: space-between;
  gap: 16px;
  flex-wrap: wrap;
  padding: 0 2px;
}
.results__summary { font-size: 0.9375rem; color: var(--ink); font-weight: 600; }
.results__summary .muted { color: var(--ink-faint); font-weight: 400; }
.results__count { font-size: 0.8125rem; color: var(--ink-muted); font-variant-numeric: tabular-nums; }

.table-wrap {
  position: relative;
  border: 1px solid var(--border);
  border-radius: var(--radius);
  overflow: auto;
  background: var(--surface);
  min-height: 200px;
}

.screener-table { width: 100%; border-collapse: collapse; font-size: 0.875rem; }
.screener-table th, .screener-table td {
  text-align: left;
  padding: 11px 14px;
  white-space: nowrap;
}
.screener-table thead th {
  position: sticky;
  top: 0;
  z-index: 1;
  background: var(--surface-2);
  color: var(--ink-muted);
  font-weight: 600;
  font-size: 0.75rem;
  letter-spacing: 0.02em;
  text-transform: uppercase;
  border-bottom: 1px solid var(--border);
  user-select: none;
}
.screener-table thead th[data-sort] { cursor: pointer; }
.screener-table thead th[data-sort]:hover { color: var(--ink); }
.screener-table thead th[aria-sort] { color: var(--ink); }
.screener-table thead th[aria-sort]::after {
  content: "";
  display: inline-block;
  width: 0; height: 0;
  margin-left: 6px;
  vertical-align: middle;
  border-left: 4px solid transparent;
  border-right: 4px solid transparent;
}
.screener-table thead th[aria-sort="descending"]::after { border-top: 5px solid currentColor; }
.screener-table thead th[aria-sort="ascending"]::after  { border-bottom: 5px solid currentColor; }

.col-rank { width: 44px; color: var(--ink-faint); text-align: right; }
.col-num, .col-change { text-align: right; }
td.col-num, td.col-change, td.col-rank { font-family: var(--font-mono); font-variant-numeric: tabular-nums; }

.screener-table tbody tr { border-top: 1px solid var(--border); transition: background 0.12s var(--ease-out); }
.screener-table tbody tr:first-child { border-top: none; }
.screener-table tbody tr:hover { background: var(--surface-2); }

.t-ticker { font-weight: 600; font-family: var(--font-mono); letter-spacing: -0.01em; }
.t-name { color: var(--ink-muted); max-width: 280px; overflow: hidden; text-overflow: ellipsis; }
.t-exch, .t-sector { color: var(--ink-faint); font-size: 0.8125rem; }

/* Change cell: number + magnitude bar */
.change { display: inline-flex; align-items: center; justify-content: flex-end; gap: 9px; }
.change__bar { width: 46px; height: 6px; border-radius: 3px; background: var(--surface-2); overflow: hidden; flex: none; }
.change__fill { display: block; height: 100%; border-radius: 3px; }
.change__val { font-weight: 600; min-width: 62px; }
.gain .change__val { color: var(--gain); }
.gain .change__fill { background: var(--gain); }
.loss .change__val { color: var(--loss); }
.loss .change__fill { background: var(--loss); }

/* ── States: skeleton / empty / error ──────────────────── */
.skeleton-row td { padding: 12px 14px; }
.sk {
  display: block;
  height: 11px;
  border-radius: 4px;
  background: linear-gradient(90deg, var(--surface-2) 0%, var(--border) 50%, var(--surface-2) 100%);
  background-size: 200% 100%;
  animation: shimmer 1.3s linear infinite;
}
@keyframes shimmer { to { background-position: -200% 0; } }

.state {
  display: grid;
  place-content: center;
  text-align: center;
  gap: 8px;
  padding: 56px 24px;
  color: var(--ink-muted);
}
.state__icon { color: var(--ink-faint); display: grid; place-items: center; margin-bottom: 2px; }
.state__title { font-size: 0.9375rem; font-weight: 600; color: var(--ink); }
.state__msg { font-size: 0.8125rem; max-width: 42ch; }
.state--error .state__icon { color: var(--loss); }
.state .btn { margin-top: 8px; justify-self: center; }

/* ── Responsive ────────────────────────────────────────── */
@media (max-width: 720px) {
  .topbar { padding: 12px 16px; }
  .layout { padding: 16px 14px 48px; }
  .controls { padding: 14px; gap: 12px; }
  .controls__actions { margin-left: 0; width: 100%; }
  .controls__actions .btn { flex: 1; }
  .field, .field--narrow { min-width: 0; flex: 1 1 120px; }
  .col-sector, .col-avgvol { display: none; }
  .change__bar { display: none; }
}

/* ── Reduced motion ────────────────────────────────────── */
@media (prefers-reduced-motion: reduce) {
  *, *::before, *::after { animation-duration: 0.001ms !important; animation-iteration-count: 1 !important; transition-duration: 0.001ms !important; }
  .sk { background: var(--surface-2); }
}
