:root {
  --bg: #0F1115;
  --surface: #171A20;
  --surface-2: #1E222A;
  --ink: #ECEEF1;
  --muted: #8B93A1;
  --muted-2: #5E6673;
  --accent: #4CB8C4;
  --accent-soft: rgba(76,184,196,0.14);
  --accent-ink: #8FE3EC;
  --hairline: #262B33;
  --score-high: #4FC08A;
  --score-mid: #E3AC4E;
  --score-low: #E2695C;
  --radius-sm: 7px;
  --radius-md: 10px;
  --radius-lg: 14px;
  --shadow-card: 0 1px 2px rgba(0,0,0,0.24), 0 10px 28px -16px rgba(0,0,0,0.6);
  --header-h: 57px;
  --font-ui: 'IBM Plex Sans', system-ui, sans-serif;
  --font-mono: 'IBM Plex Mono', ui-monospace, monospace;
}

* { box-sizing: border-box; }

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;
}

/* ---------- Header ---------- */

.app-header {
  position: sticky;
  top: 0;
  z-index: 20;
  height: var(--header-h);
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 0 24px;
  background: var(--surface);
  border-bottom: 1px solid var(--hairline);
}

.brand { display: flex; align-items: baseline; gap: 9px; }
.brand-mark {
  font-family: var(--font-mono);
  font-weight: 600;
  background: var(--accent);
  color: #08151A;
  padding: 3px 8px;
  border-radius: 5px;
  font-size: 12px;
  letter-spacing: 0.04em;
}
.brand-name { font-weight: 600; font-size: 15px; letter-spacing: -0.01em; }

.status { display: flex; align-items: center; gap: 7px; font-family: var(--font-mono); font-size: 12px; color: var(--muted); }
.status-dot { width: 7px; height: 7px; border-radius: 50%; background: var(--score-high); box-shadow: 0 0 0 3px rgba(79,192,138,0.16); }
.status-dot.busy { background: var(--score-mid); box-shadow: 0 0 0 3px rgba(227,172,78,0.16); animation: pulse 1s infinite; }
.status-dot.error { background: var(--score-low); box-shadow: 0 0 0 3px rgba(226,105,92,0.16); }
@keyframes pulse { 50% { opacity: 0.35; } }

/* ---------- Layout ---------- */

.app-main { max-width: 1180px; margin: 0 auto; padding: 20px 24px 48px; display: flex; flex-direction: column; gap: 18px; }

.search-panel, .results-panel {
  background: var(--surface);
  border: 1px solid var(--hairline);
  border-radius: var(--radius-lg);
  box-shadow: var(--shadow-card);
}
.search-panel { padding: 18px 20px 20px; }
/* no overflow:hidden here -- it would clip position:sticky in the table header */
.results-panel { padding: 0; }
.results-toolbar { border-radius: var(--radius-lg) var(--radius-lg) 0 0; }
.table-wrap { border-radius: 0 0 var(--radius-lg) var(--radius-lg); }

/* ---------- Search form ---------- */

.field-grid { display: grid; grid-template-columns: repeat(4, 1fr); gap: 14px 16px; }
.field { display: flex; flex-direction: column; gap: 6px; font-size: 12px; color: var(--muted); font-weight: 500; }
.field-wide { grid-column: span 2; }
.field input, .field select {
  font-family: var(--font-ui);
  font-size: 14px;
  padding: 9px 11px;
  border: 1px solid var(--hairline);
  border-radius: var(--radius-sm);
  background: var(--surface-2);
  color: var(--ink);
  transition: border-color 0.12s ease, background 0.12s ease;
}
.field select { appearance: none; -webkit-appearance: none; background-image: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' width='10' height='6'%3E%3Cpath d='M1 1l4 4 4-4' fill='none' stroke='%238B93A1' stroke-width='1.5' stroke-linecap='round'/%3E%3C/svg%3E"); background-repeat: no-repeat; background-position: right 11px center; padding-right: 30px; cursor: pointer; }
.field input::placeholder { color: var(--muted-2); }
.field input:hover, .field select:hover { border-color: #3A4150; }
.field input:focus, .field select:focus {
  outline: none;
  border-color: var(--accent);
  box-shadow: 0 0 0 3px var(--accent-soft);
}

.field-row { display: flex; align-items: center; gap: 22px; margin-top: 18px; flex-wrap: wrap; }
.checkbox { display: flex; align-items: center; gap: 7px; font-size: 13px; color: var(--muted); cursor: pointer; }
.checkbox input { accent-color: var(--accent); width: 15px; height: 15px; }
.checkbox em { color: var(--muted-2); font-style: normal; }

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

.btn {
  display: inline-flex;
  align-items: center;
  gap: 7px;
  font-family: var(--font-ui);
  font-weight: 600;
  font-size: 13px;
  padding: 9px 15px;
  border-radius: var(--radius-sm);
  border: 1px solid transparent;
  cursor: pointer;
  transition: transform 0.08s ease, background 0.12s ease, border-color 0.12s ease, color 0.12s ease;
}
.btn:active:not(:disabled) { transform: translateY(1px); }
.btn:disabled { opacity: 0.38; cursor: not-allowed; }
.btn:focus-visible { outline: 2px solid var(--accent); outline-offset: 2px; }
.btn-primary { background: var(--accent); color: #08151A; margin-left: auto; }
.btn-primary:hover:not(:disabled) { background: var(--accent-ink); }
.btn-ghost { background: transparent; border-color: var(--hairline); color: var(--ink); }
.btn-ghost:hover:not(:disabled) { border-color: var(--accent); color: var(--accent-ink); background: var(--accent-soft); }
.btn-small { padding: 6px 11px; font-size: 12px; }

.link-btn {
  background: none; border: none; padding: 0; margin: 0;
  color: var(--accent-ink); font-size: 12px; font-weight: 600;
  cursor: pointer; text-decoration: underline; text-underline-offset: 2px;
}
.link-btn:hover { color: var(--ink); }

.icon { width: 14px; height: 14px; flex-shrink: 0; }

/* ---------- Results toolbar ---------- */

.results-toolbar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 16px;
  padding: 14px 20px;
  border-bottom: 1px solid var(--hairline);
  background: var(--surface);
}
.results-meta { display: flex; align-items: center; gap: 12px; flex-wrap: wrap; }
.results-count { font-family: var(--font-mono); font-size: 12px; color: var(--muted); }
.selection-chip {
  display: flex; align-items: center; gap: 8px;
  font-size: 12px; font-family: var(--font-mono); color: var(--accent-ink);
  background: var(--accent-soft); border: 1px solid rgba(76,184,196,0.3);
  padding: 3px 10px 3px 10px; border-radius: 999px;
}
.selection-chip[hidden] { display: none; } /* wins over .selection-chip's own display:flex */
.results-actions { display: flex; gap: 8px; flex-shrink: 0; }

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

/* overflow-x:auto here implicitly forces overflow-y non-visible too (a CSS
   quirk: you can't scroll one axis and leave the other truly visible), which
   makes this box -- not the page -- the sticky positioning context for the
   thead below. Giving it a real bounded height + its own scrollbar makes
   that correct instead of accidental: the results list scrolls in place,
   search filters stay put, and the sticky header genuinely pins within it. */
.table-wrap { overflow: auto; max-height: 62vh; }
.results-table { width: 100%; border-collapse: collapse; }
.results-table thead th {
  position: sticky;
  top: 0;
  z-index: 5;
  text-align: left;
  font-size: 11px;
  text-transform: uppercase;
  letter-spacing: 0.05em;
  color: var(--muted);
  font-weight: 600;
  padding: 10px 14px;
  background: var(--surface);
  border-bottom: 1px solid var(--hairline);
  white-space: nowrap;
}
.col-check { width: 36px; } .col-score { width: 68px; } .col-expand { width: 32px; }

.results-table tbody tr.lead-row { border-bottom: 1px solid var(--hairline); cursor: pointer; transition: background 0.1s ease; }
.results-table tbody tr.lead-row:nth-child(4n+1) { background: rgba(255,255,255,0.012); }
.results-table tbody tr.lead-row:hover { background: var(--surface-2); }
.results-table tbody tr.lead-row.is-selected { background: var(--accent-soft); box-shadow: inset 3px 0 0 var(--accent); }
.results-table tbody tr.lead-row.is-selected:hover { background: var(--accent-soft); }
.results-table td { padding: 11px 14px; vertical-align: middle; }
.empty-row td { text-align: center; color: var(--muted); padding: 40px 10px; font-style: italic; }

.loading-row { display: flex; align-items: center; justify-content: center; gap: 10px; padding: 40px 10px; color: var(--muted); }
.spinner {
  width: 15px; height: 15px; border-radius: 50%;
  border: 2px solid var(--hairline); border-top-color: var(--accent);
  animation: spin 0.7s linear infinite;
}
@keyframes spin { to { transform: rotate(360deg); } }

.company-name { font-weight: 600; }
.company-taxonomy { font-size: 12px; color: var(--muted); margin-top: 1px; }
.mono { font-family: var(--font-mono); font-size: 13px; }
.chevron { display: inline-block; transition: transform 0.15s ease; color: var(--muted); }
.chevron.open { transform: rotate(90deg); color: var(--accent-ink); }

.score-ring { display: inline-flex; align-items: center; justify-content: center; position: relative; width: 42px; height: 42px; }
.score-ring svg { transform: rotate(-90deg); }
.score-ring circle { fill: none; stroke-width: 4.5; }
.score-ring .track { stroke: var(--hairline); }
.score-ring .fill { stroke-linecap: round; transition: stroke-dashoffset 0.5s ease; }
.score-ring .label { position: absolute; font-family: var(--font-mono); font-size: 12px; font-weight: 700; }

/* ---------- Expanded row ---------- */

.detail-row td { background: var(--bg); padding: 18px 22px 22px; border-bottom: 1px solid var(--hairline); }
.detail-grid { display: grid; grid-template-columns: 1fr 1fr; gap: 24px; }
.detail-block h4 { margin: 0 0 10px; font-size: 11px; text-transform: uppercase; letter-spacing: 0.05em; color: var(--muted); font-weight: 600; }
.contact-item { padding: 7px 0; border-bottom: 1px dashed var(--hairline); font-size: 13px; }
.contact-item:last-child { border-bottom: none; }
.contact-role { font-family: var(--font-mono); font-size: 10px; text-transform: uppercase; color: var(--accent-ink); background: rgba(76,184,196,0.16); padding: 2px 7px; border-radius: 4px; margin-left: 7px; }
.contact-phone { font-size: 12px; color: var(--muted); margin-top: 3px; }

.brief-box { margin-top: 16px; }
.brief-output {
  white-space: pre-wrap; background: var(--surface); border: 1px solid var(--hairline);
  border-radius: var(--radius-sm); padding: 13px 14px; font-size: 13px;
  line-height: 1.6; margin-top: 10px; display: none;
}
.brief-output.visible { display: block; }

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

.toast {
  position: fixed; bottom: 22px; right: 22px;
  background: var(--surface-2); color: var(--ink);
  border: 1px solid var(--hairline);
  padding: 12px 16px; border-radius: var(--radius-md);
  box-shadow: var(--shadow-card);
  font-size: 13px; opacity: 0; transform: translateY(8px);
  transition: opacity 0.2s ease, transform 0.2s ease;
  pointer-events: none; max-width: 340px; z-index: 30;
}
.toast.visible { opacity: 1; transform: translateY(0); }
.toast.error { background: var(--score-low); color: #1A0605; border-color: var(--score-low); }
.toast a { color: var(--accent-ink); font-weight: 600; }
.toast.error a { color: #1A0605; text-decoration-color: currentColor; }

@media (max-width: 720px) {
  .field-grid { grid-template-columns: 1fr 1fr; }
  .field-wide { grid-column: span 2; }
  .detail-grid { grid-template-columns: 1fr; }
  .results-toolbar { flex-direction: column; align-items: stretch; }
  .results-actions { justify-content: stretch; }
  .results-actions .btn { flex: 1; justify-content: center; }
}
