*,
*::before,
*::after {
  margin: 0;
  padding: 0;
  box-sizing: border-box;
}

:root {
  --font: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Helvetica, Arial, sans-serif;
  --radius: 8px;
  --radius-sm: 4px;
  --transition: 0.2s ease;
}

[data-theme="dark"] {
  --bg: #0f0f1a;
  --bg-card: #1a1a2e;
  --bg-input: #16213e;
  --bg-hover: #222244;
  --bg-dropdown: #1a1a2e;
  --border: #2a2a4a;
  --text: #e0e0e0;
  --text-secondary: #9999bb;
  --text-muted: #666688;
  --accent: #4a9eff;
  --accent-hover: #3a8eef;
  --accent-bg: rgba(74, 158, 255, 0.1);
  --danger: #ff6b6b;
  --success: #51cf66;
  --table-stripe: rgba(255, 255, 255, 0.02);
  --table-hover: rgba(74, 158, 255, 0.08);
  --shadow: 0 2px 8px rgba(0, 0, 0, 0.3);
}

[data-theme="light"] {
  --bg: #f5f7fa;
  --bg-card: #ffffff;
  --bg-input: #f0f2f5;
  --bg-hover: #e8eaed;
  --bg-dropdown: #ffffff;
  --border: #d0d5dd;
  --text: #1a1a2e;
  --text-secondary: #555570;
  --text-muted: #888899;
  --accent: #0066cc;
  --accent-hover: #0055aa;
  --accent-bg: rgba(0, 102, 204, 0.08);
  --danger: #e03131;
  --success: #2f9e44;
  --table-stripe: rgba(0, 0, 0, 0.02);
  --table-hover: rgba(0, 102, 204, 0.06);
  --shadow: 0 2px 8px rgba(0, 0, 0, 0.08);
}

body {
  font-family: var(--font);
  background: var(--bg);
  color: var(--text);
  min-height: 100vh;
  display: flex;
  flex-direction: column;
  transition: background var(--transition), color var(--transition);
}

/* Header */
.header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 16px 32px;
  background: var(--bg-card);
  border-bottom: 1px solid var(--border);
}

.logo {
  font-size: 1.4rem;
  font-weight: 700;
  letter-spacing: -0.5px;
}

.logo-accent {
  color: var(--accent);
}

.subtitle {
  font-size: 0.8rem;
  color: var(--text-muted);
  margin-top: 2px;
}

.theme-toggle {
  background: var(--bg-input);
  border: 1px solid var(--border);
  border-radius: 50%;
  width: 40px;
  height: 40px;
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: center;
  transition: background var(--transition);
}

.theme-toggle:hover {
  background: var(--bg-hover);
}

.theme-icon {
  font-size: 1.2rem;
  color: var(--text);
}

/* Main */
.main {
  flex: 1;
  padding: 24px 32px;
  max-width: 1400px;
  width: 100%;
  margin: 0 auto;
  display: flex;
  flex-direction: column;
  gap: 24px;
}

/* Sections */
.section-title {
  font-size: 1rem;
  font-weight: 600;
  margin-bottom: 16px;
}

/* Search Panel */
.search-panel {
  background: var(--bg-card);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 24px;
  box-shadow: var(--shadow);
}

.search-form {
  display: flex;
  flex-direction: column;
  gap: 16px;
}

.form-row {
  display: flex;
  gap: 16px;
  flex-wrap: wrap;
}

.form-group {
  flex: 1;
  min-width: 200px;
  display: flex;
  flex-direction: column;
  gap: 6px;
}

.form-group label {
  font-size: 0.82rem;
  font-weight: 500;
  color: var(--text-secondary);
}

.optional {
  color: var(--text-muted);
  font-weight: 400;
}

.form-input,
.form-select {
  padding: 10px 12px;
  background: var(--bg-input);
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  color: var(--text);
  font-size: 0.9rem;
  font-family: var(--font);
  transition: border-color var(--transition);
}

.form-input:focus,
.form-select:focus {
  outline: none;
  border-color: var(--accent);
}

.form-select {
  cursor: pointer;
  appearance: none;
  background-image: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' width='12' height='12' viewBox='0 0 12 12'%3E%3Cpath fill='%239999bb' d='M6 8L1 3h10z'/%3E%3C/svg%3E");
  background-repeat: no-repeat;
  background-position: right 12px center;
  padding-right: 32px;
}

/* Multi-select dropdown */
.multi-select {
  position: relative;
}

.multi-select-btn {
  width: 100%;
  padding: 10px 12px;
  background: var(--bg-input);
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  color: var(--text);
  font-size: 0.9rem;
  font-family: var(--font);
  cursor: pointer;
  display: flex;
  align-items: center;
  gap: 8px;
  transition: border-color var(--transition);
  text-align: left;
}

.multi-select-btn:focus {
  outline: none;
  border-color: var(--accent);
}

.multi-select-label {
  flex: 1;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
  color: var(--text-muted);
}

.multi-select-label.has-selection {
  color: var(--text);
}

.multi-select-count {
  background: var(--accent);
  color: #fff;
  font-size: 0.72rem;
  font-weight: 600;
  padding: 2px 7px;
  border-radius: 10px;
  min-width: 20px;
  text-align: center;
}

.chevron {
  font-size: 0.7rem;
  color: var(--text-muted);
  transition: transform var(--transition);
}

.multi-select.open .chevron {
  transform: rotate(180deg);
}

.multi-select-dropdown {
  display: none;
  position: absolute;
  top: calc(100% + 4px);
  left: 0;
  right: 0;
  background: var(--bg-dropdown);
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  box-shadow: var(--shadow);
  max-height: 240px;
  overflow-y: auto;
  z-index: 100;
}

.multi-select.open .multi-select-dropdown {
  display: block;
}

.multi-select-option {
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 8px 12px;
  cursor: pointer;
  font-size: 0.85rem;
  transition: background var(--transition);
}

.multi-select-option:hover {
  background: var(--bg-hover);
}

.multi-select-option input[type="checkbox"] {
  accent-color: var(--accent);
  cursor: pointer;
}

/* Form actions */
.form-actions {
  display: flex;
  gap: 12px;
  padding-top: 4px;
}

/* Buttons */
.btn {
  padding: 10px 20px;
  border: none;
  border-radius: var(--radius-sm);
  font-size: 0.9rem;
  font-weight: 500;
  font-family: var(--font);
  cursor: pointer;
  transition: background var(--transition), opacity var(--transition);
  display: inline-flex;
  align-items: center;
  gap: 8px;
}

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

.btn-primary {
  background: var(--accent);
  color: #fff;
}

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

.btn-secondary {
  background: var(--bg-input);
  color: var(--text);
  border: 1px solid var(--border);
}

.btn-secondary:hover:not(:disabled) {
  background: var(--bg-hover);
}

.btn-export {
  background: var(--success);
  color: #fff;
  font-size: 0.82rem;
  padding: 8px 16px;
}

.btn-export:hover {
  opacity: 0.9;
}

.btn-page {
  background: var(--bg-input);
  color: var(--text);
  border: 1px solid var(--border);
  font-size: 0.82rem;
  padding: 6px 14px;
}

.btn-page:hover:not(:disabled) {
  background: var(--bg-hover);
}

.btn-spinner {
  width: 16px;
  height: 16px;
  border: 2px solid rgba(255, 255, 255, 0.3);
  border-top-color: #fff;
  border-radius: 50%;
  animation: spin 0.6s linear infinite;
}

/* Results Panel */
.results-panel {
  background: var(--bg-card);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 24px;
  box-shadow: var(--shadow);
}

.results-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  margin-bottom: 16px;
}

.results-info {
  display: flex;
  align-items: center;
  gap: 12px;
}

.results-info .section-title {
  margin-bottom: 0;
}

.results-count {
  font-size: 0.82rem;
  color: var(--text-muted);
  background: var(--accent-bg);
  padding: 4px 10px;
  border-radius: 12px;
}

/* Empty / Loading / Error states */
.empty-state,
.loading-state,
.error-state {
  text-align: center;
  padding: 48px 24px;
  color: var(--text-muted);
}

.empty-icon,
.error-icon {
  font-size: 2.5rem;
  margin-bottom: 12px;
}

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

.error-message {
  margin-bottom: 16px;
}

/* Spinner */
.spinner {
  width: 36px;
  height: 36px;
  border: 3px solid var(--border);
  border-top-color: var(--accent);
  border-radius: 50%;
  animation: spin 0.8s linear infinite;
  margin: 0 auto 16px;
}

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

/* Table */
.table-container {
  overflow-x: auto;
}

.leads-table {
  width: 100%;
  border-collapse: collapse;
  font-size: 0.85rem;
}

.leads-table th {
  text-align: left;
  padding: 10px 12px;
  font-weight: 600;
  font-size: 0.78rem;
  text-transform: uppercase;
  letter-spacing: 0.5px;
  color: var(--text-secondary);
  border-bottom: 2px solid var(--border);
  cursor: pointer;
  white-space: nowrap;
  user-select: none;
}

.leads-table th:hover {
  color: var(--accent);
}

.leads-table th .sort-icon::after {
  content: "";
  margin-left: 4px;
}

.leads-table th.sort-asc .sort-icon::after {
  content: " \25B2";
}

.leads-table th.sort-desc .sort-icon::after {
  content: " \25BC";
}

.leads-table td {
  padding: 10px 12px;
  border-bottom: 1px solid var(--border);
  max-width: 200px;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.leads-table tbody tr:nth-child(even) {
  background: var(--table-stripe);
}

.leads-table tbody tr:hover {
  background: var(--table-hover);
}

.leads-table a {
  color: var(--accent);
  text-decoration: none;
}

.leads-table a:hover {
  text-decoration: underline;
}

/* Pagination */
.pagination {
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 16px;
  margin-top: 16px;
}

.page-info {
  font-size: 0.82rem;
  color: var(--text-muted);
}

/* Footer */
.footer {
  text-align: center;
  padding: 16px 32px;
  font-size: 0.75rem;
  color: var(--text-muted);
  border-top: 1px solid var(--border);
}

.footer a {
  color: var(--accent);
  text-decoration: none;
}

.footer a:hover {
  text-decoration: underline;
}

/* Responsive */
@media (max-width: 768px) {
  .header {
    padding: 12px 16px;
  }

  .main {
    padding: 16px;
  }

  .form-row {
    flex-direction: column;
  }

  .form-group {
    min-width: 100%;
  }

  .results-header {
    flex-direction: column;
    align-items: flex-start;
    gap: 12px;
  }

  .leads-table {
    font-size: 0.78rem;
  }

  .leads-table td {
    max-width: 120px;
  }
}
