/* Reset & Variables */
*, *::before, *::after {
  margin: 0;
  padding: 0;
  box-sizing: border-box;
}

:root {
  /* Colors - Light */
  --bg-page: #fafbfc;
  --bg-surface: #ffffff;
  --bg-surface-hover: #f4f6f8;
  --bg-muted: #f0f2f5;
  --bg-input: #ffffff;

  --text-primary: #1a1d21;
  --text-secondary: #5e6670;
  --text-muted: #8b919a;
  --text-inverse: #ffffff;

  --border-default: #e1e4e8;
  --border-strong: #d0d4d9;

  --accent: #0066ff;
  --accent-hover: #0052cc;
  --accent-muted: #e6f0ff;

  --success: #059669;
  --warning: #d97706;
  --danger: #dc2626;

  /* Spacing */
  --space-xs: 0.25rem;
  --space-sm: 0.5rem;
  --space-md: 1rem;
  --space-lg: 1.5rem;
  --space-xl: 2rem;
  --space-2xl: 3rem;

  /* Radii */
  --radius-sm: 6px;
  --radius-md: 10px;
  --radius-lg: 14px;
  --radius-full: 9999px;

  /* Shadows */
  --shadow-sm: 0 1px 2px rgba(0, 0, 0, 0.04);
  --shadow-md: 0 2px 8px rgba(0, 0, 0, 0.06);
  --shadow-lg: 0 4px 16px rgba(0, 0, 0, 0.08);
  --shadow-card: 0 1px 3px rgba(0, 0, 0, 0.04), 0 1px 2px rgba(0, 0, 0, 0.02);
  --shadow-card-hover: 0 8px 24px rgba(0, 0, 0, 0.1);

  /* Typography */
  --font-sans: -apple-system, BlinkMacSystemFont, 'Segoe UI', 'Inter', Roboto, sans-serif;
  --font-mono: 'SF Mono', 'Fira Code', Monaco, 'Cascadia Code', monospace;

  /* Transitions */
  --ease: cubic-bezier(0.4, 0, 0.2, 1);
  --duration-fast: 150ms;
  --duration: 200ms;
}

/* Dark Mode */
[data-theme="dark"] {
  --bg-page: #0d1117;
  --bg-surface: #161b22;
  --bg-surface-hover: #21262d;
  --bg-muted: #21262d;
  --bg-input: #0d1117;

  --text-primary: #e6edf3;
  --text-secondary: #8b949e;
  --text-muted: #6e7681;
  --text-inverse: #0d1117;

  --border-default: #30363d;
  --border-strong: #484f58;

  --accent: #58a6ff;
  --accent-hover: #79b8ff;
  --accent-muted: #1c3254;

  --shadow-sm: 0 1px 2px rgba(0, 0, 0, 0.2);
  --shadow-md: 0 2px 8px rgba(0, 0, 0, 0.3);
  --shadow-lg: 0 4px 16px rgba(0, 0, 0, 0.4);
  --shadow-card: 0 1px 3px rgba(0, 0, 0, 0.2);
  --shadow-card-hover: 0 8px 24px rgba(0, 0, 0, 0.4);
}

/* Base */
html {
  font-size: 16px;
  -webkit-font-smoothing: antialiased;
  -moz-osx-font-smoothing: grayscale;
}

body {
  font-family: var(--font-sans);
  background: var(--bg-page);
  color: var(--text-primary);
  line-height: 1.5;
  min-height: 100vh;
}

/* ======================================
   HEADER
====================================== */
.header {
  background: var(--bg-surface);
  border-bottom: 1px solid var(--border-default);
  position: sticky;
  top: 0;
  z-index: 100;
  transition: box-shadow var(--duration) var(--ease);
}

.header-content {
  max-width: 1400px;
  margin: 0 auto;
  padding: var(--space-md) var(--space-xl);
  display: flex;
  align-items: center;
  gap: var(--space-xl);
}

/* Logo */
.logo {
  display: flex;
  align-items: center;
  gap: var(--space-sm);
  text-decoration: none;
  color: var(--text-primary);
  font-weight: 700;
  font-size: 1.25rem;
  letter-spacing: -0.02em;
  flex-shrink: 0;
}

.logo:hover {
  opacity: 0.8;
}

.logo-img {
  height: 32px;
  width: auto;
  object-fit: contain;
}

/* ======================================
   SEARCH
====================================== */
.search-container {
  flex: 1;
  max-width: 640px;
  display: flex;
  flex-direction: column;
  gap: var(--space-sm);
}

.search-field-wrapper {
  position: relative;
  display: flex;
  align-items: center;
}

.search-icon {
  position: absolute;
  left: var(--space-md);
  color: var(--text-muted);
  pointer-events: none;
  z-index: 1;
}

.search-input {
  width: 100%;
  height: 44px;
  padding: 0 var(--space-xl) 0 2.75rem;
  background: var(--bg-input);
  border: 1px solid var(--border-default);
  border-radius: var(--radius-lg);
  font-family: var(--font-mono);
  font-size: 0.875rem;
  color: var(--text-primary);
  transition: border-color var(--duration) var(--ease),
              box-shadow var(--duration) var(--ease);
}

.search-input::placeholder {
  color: var(--text-muted);
  font-family: var(--font-sans);
}

.search-input:hover {
  border-color: var(--border-strong);
}

.search-input:focus {
  outline: none;
  border-color: var(--accent);
  box-shadow: 0 0 0 3px var(--accent-muted);
}

.clear-search {
  position: absolute;
  right: var(--space-sm);
  width: 28px;
  height: 28px;
  display: none;
  align-items: center;
  justify-content: center;
  background: var(--bg-muted);
  border: none;
  border-radius: var(--radius-full);
  color: var(--text-muted);
  cursor: pointer;
  transition: all var(--duration-fast) var(--ease);
}

.clear-search.visible {
  display: flex;
}

.clear-search:hover {
  background: var(--border-default);
  color: var(--text-secondary);
}

/* Operator Buttons & Help */
.operator-buttons {
  display: flex;
  align-items: center;
  gap: var(--space-xs);
}

.op-btn {
  height: 28px;
  padding: 0 var(--space-sm);
  background: var(--bg-muted);
  border: 1px solid transparent;
  border-radius: var(--radius-sm);
  font-family: var(--font-mono);
  font-size: 0.8125rem;
  font-weight: 600;
  color: var(--text-secondary);
  cursor: pointer;
  transition: all var(--duration-fast) var(--ease);
}

.op-btn:hover {
  background: var(--accent-muted);
  color: var(--accent);
}

.help-btn {
  height: 28px;
  padding: 0 var(--space-sm);
  background: transparent;
  border: 1px solid var(--border-default);
  border-radius: var(--radius-sm);
  font-size: 0.75rem;
  font-weight: 500;
  color: var(--text-muted);
  cursor: pointer;
  display: flex;
  align-items: center;
  gap: 4px;
  margin-left: auto;
  transition: all var(--duration-fast) var(--ease);
}

.help-btn:hover {
  border-color: var(--accent);
  color: var(--accent);
}

.help-icon {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  width: 16px;
  height: 16px;
  background: var(--text-muted);
  color: var(--bg-surface);
  border-radius: var(--radius-full);
  font-size: 0.625rem;
  font-weight: 700;
}

.help-btn:hover .help-icon {
  background: var(--accent);
}

/* Parsed Query Tokens */
.parsed-query {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  gap: 6px;
  min-height: 24px;
  padding-left: 2px;
}

.parsed-query:empty {
  display: none;
}

.query-token {
  display: inline-flex;
  align-items: center;
  height: 22px;
  padding: 0 8px;
  border-radius: 4px;
  font-family: var(--font-mono);
  font-size: 0.75rem;
  font-weight: 500;
  line-height: 1;
}

.token-word {
  background: #dbeafe;
  color: #1d4ed8;
}

.token-phrase {
  background: #e9d5ff;
  color: #7c3aed;
}

.token-negated {
  background: #fecaca;
  color: #b91c1c;
}

.token-negated-phrase {
  background: #fbcfe8;
  color: #be185d;
}

.token-operator {
  background: transparent;
  padding: 0 4px;
  font-weight: 700;
  color: var(--text-muted);
}

.token-and { color: var(--success); }
.token-or { color: var(--warning); }

[data-theme="dark"] .token-word {
  background: #1e3a5f;
  color: #93c5fd;
}

[data-theme="dark"] .token-phrase {
  background: #3b2d5c;
  color: #c4b5fd;
}

[data-theme="dark"] .token-negated {
  background: #4c1d1d;
  color: #fca5a5;
}

[data-theme="dark"] .token-negated-phrase {
  background: #4c1d3d;
  color: #f9a8d4;
}

/* ======================================
   STATS BAR
====================================== */
.stats-bar {
  display: flex;
  align-items: center;
  gap: var(--space-md);
  flex-shrink: 0;
}

.stat-item {
  display: flex;
  align-items: center;
  gap: 6px;
  font-size: 0.8125rem;
  color: var(--text-muted);
}

.stat-value {
  font-weight: 600;
  color: var(--text-primary);
}

/* Theme Toggle */
.theme-toggle {
  width: 36px;
  height: 36px;
  display: flex;
  align-items: center;
  justify-content: center;
  background: var(--bg-muted);
  border: 1px solid var(--border-default);
  border-radius: var(--radius-full);
  cursor: pointer;
  transition: all var(--duration) var(--ease);
}

.theme-toggle:hover {
  background: var(--bg-surface-hover);
  border-color: var(--border-strong);
}

.theme-toggle svg {
  width: 18px;
  height: 18px;
  color: var(--text-secondary);
}

.theme-toggle .sun-icon { display: none; }
.theme-toggle .moon-icon { display: block; }

[data-theme="dark"] .theme-toggle .sun-icon { display: block; }
[data-theme="dark"] .theme-toggle .moon-icon { display: none; }

/* ======================================
   HELP MODAL
====================================== */
.help-modal-overlay {
  position: fixed;
  inset: 0;
  background: rgba(0, 0, 0, 0.4);
  backdrop-filter: blur(4px);
  display: none;
  align-items: center;
  justify-content: center;
  z-index: 1000;
  padding: var(--space-md);
}

.help-modal-overlay.visible {
  display: flex;
}

.help-modal-window {
  background: var(--bg-surface);
  border-radius: var(--radius-lg);
  box-shadow: var(--shadow-lg);
  max-width: 480px;
  width: 100%;
  max-height: 85vh;
  overflow-y: auto;
  position: relative;
}

.help-modal-close {
  position: absolute;
  top: var(--space-md);
  right: var(--space-md);
  width: 32px;
  height: 32px;
  display: flex;
  align-items: center;
  justify-content: center;
  background: transparent;
  border: none;
  border-radius: var(--radius-sm);
  color: var(--text-muted);
  font-size: 1.5rem;
  cursor: pointer;
  transition: all var(--duration-fast) var(--ease);
}

.help-modal-close:hover {
  background: var(--bg-muted);
  color: var(--text-primary);
}

.help-modal-window h2 {
  padding: var(--space-lg) var(--space-lg) var(--space-md);
  font-size: 1.125rem;
  font-weight: 600;
  border-bottom: 1px solid var(--border-default);
}

.help-list {
  list-style: none;
  padding: var(--space-md) var(--space-lg) var(--space-lg);
}

.help-list li {
  padding: var(--space-md) 0;
  border-bottom: 1px solid var(--border-default);
}

.help-list li:last-child {
  border-bottom: none;
  padding-bottom: 0;
}

.help-list code {
  display: inline-block;
  background: var(--bg-muted);
  padding: 4px 8px;
  border-radius: var(--radius-sm);
  font-family: var(--font-mono);
  font-size: 0.8125rem;
  color: var(--accent);
  margin-bottom: 6px;
}

.help-list p {
  font-size: 0.8125rem;
  color: var(--text-secondary);
  line-height: 1.5;
}

.help-list p em {
  font-style: normal;
  font-weight: 500;
  color: var(--text-primary);
}

.help-list p strong {
  color: var(--accent);
}

/* ======================================
   MAIN CONTENT
====================================== */
.main-container {
  max-width: 1400px;
  margin: 0 auto;
  padding: var(--space-lg) var(--space-xl);
}

.results-area {
  width: 100%;
}

/* Results Header */
.results-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: var(--space-lg);
}

.results-count {
  font-size: 0.9375rem;
  color: var(--text-secondary);
}

.results-count strong {
  font-weight: 600;
  color: var(--text-primary);
}

.sort-dropdown {
  display: flex;
  align-items: center;
  gap: var(--space-sm);
}

.sort-dropdown label {
  font-size: 0.8125rem;
  color: var(--text-muted);
}

.sort-dropdown select {
  height: 36px;
  padding: 0 var(--space-md);
  background: var(--bg-surface);
  border: 1px solid var(--border-default);
  border-radius: var(--radius-sm);
  font-size: 0.8125rem;
  color: var(--text-primary);
  cursor: pointer;
  transition: all var(--duration-fast) var(--ease);
}

.sort-dropdown select:hover {
  border-color: var(--border-strong);
}

.sort-dropdown select:focus {
  outline: none;
  border-color: var(--accent);
  box-shadow: 0 0 0 3px var(--accent-muted);
}

/* ======================================
   JOB CARDS
====================================== */
.jobs-grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(340px, 1fr));
  gap: var(--space-md);
  width: 100%;
}

.job-card {
  background: var(--bg-surface);
  border: 1px solid var(--border-default);
  border-radius: var(--radius-md);
  padding: var(--space-lg);
  display: flex;
  flex-direction: column;
  gap: var(--space-md);
  min-width: 0;
  transition: border-color var(--duration) var(--ease),
              box-shadow var(--duration) var(--ease),
              transform var(--duration) var(--ease);
}

.job-card:hover {
  border-color: var(--border-strong);
  box-shadow: var(--shadow-card-hover);
  transform: translateY(-2px);
}

.job-card-header {
  display: flex;
  align-items: flex-start;
  justify-content: space-between;
  gap: var(--space-sm);
}

.org-badge {
  display: inline-flex;
  align-items: center;
  height: 24px;
  padding: 0 10px;
  background: var(--accent-muted);
  border-radius: var(--radius-sm);
  font-size: 0.6875rem;
  font-weight: 600;
  color: var(--accent);
  text-transform: uppercase;
  letter-spacing: 0.04em;
  white-space: nowrap;
}

.job-title {
  font-size: 1rem;
  font-weight: 600;
  line-height: 1.4;
  color: var(--text-primary);
  display: -webkit-box;
  -webkit-line-clamp: 2;
  -webkit-box-orient: vertical;
  overflow: hidden;
  word-break: break-word;
}

.job-title a {
  color: inherit;
  text-decoration: none;
  transition: color var(--duration-fast) var(--ease);
}

.job-title a:hover {
  color: var(--accent);
}

.external-link-icon {
  display: inline-block;
  margin-left: 4px;
  vertical-align: middle;
  opacity: 0.4;
  transition: opacity var(--duration-fast) var(--ease);
}

.job-title a:hover .external-link-icon {
  opacity: 1;
}

/* Location Badges */
.job-meta {
  display: flex;
  flex-direction: column;
  gap: var(--space-sm);
}

.job-location-badges {
  display: flex;
  flex-wrap: wrap;
  gap: 6px;
}

.location-badge {
  display: inline-flex;
  align-items: center;
  height: 22px;
  padding: 0 8px;
  border-radius: 4px;
  font-size: 0.6875rem;
  font-weight: 500;
  white-space: nowrap;
}

.location-city {
  background: var(--text-secondary);
  color: var(--text-inverse);
}

.location-country {
  background: var(--text-muted);
  color: var(--text-inverse);
}

/* Card Footer */
.job-card-footer {
  margin-top: auto;
  padding-top: var(--space-md);
  border-top: 1px solid var(--border-default);
}

.job-org-name {
  font-size: 0.8125rem;
  color: var(--text-muted);
  text-decoration: none;
  transition: color var(--duration-fast) var(--ease);
}

.job-org-name:hover {
  color: var(--accent);
}

.job-org-name .external-link-icon {
  opacity: 0.3;
}

.job-org-name:hover .external-link-icon {
  opacity: 1;
}

/* ======================================
   STATES: Loading, No Results
====================================== */
.loading, .no-results {
  text-align: center;
  padding: var(--space-2xl);
  background: var(--bg-surface);
  border: 1px dashed var(--border-default);
  border-radius: var(--radius-lg);
}

.loading-spinner {
  width: 32px;
  height: 32px;
  margin: 0 auto var(--space-md);
  border: 2px solid var(--border-default);
  border-top-color: var(--accent);
  border-radius: var(--radius-full);
  animation: spin 0.8s linear infinite;
}

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

.loading p, .no-results p {
  color: var(--text-muted);
  font-size: 0.875rem;
}

.no-results-icon {
  width: 48px;
  height: 48px;
  margin: 0 auto var(--space-md);
  color: var(--text-muted);
  opacity: 0.5;
}

.no-results h3 {
  font-size: 1rem;
  font-weight: 600;
  color: var(--text-primary);
  margin-bottom: 4px;
}

/* Highlight */
.highlight {
  background: #fef08a;
  padding: 1px 2px;
  border-radius: 2px;
}

[data-theme="dark"] .highlight {
  background: #854d0e;
}

/* ======================================
   SCROLLBAR
====================================== */
::-webkit-scrollbar {
  width: 8px;
  height: 8px;
}

::-webkit-scrollbar-track {
  background: transparent;
}

::-webkit-scrollbar-thumb {
  background: var(--border-default);
  border-radius: 4px;
}

::-webkit-scrollbar-thumb:hover {
  background: var(--border-strong);
}

/* ======================================
   FOCUS STATES
====================================== */
:focus-visible {
  outline: 2px solid var(--accent);
  outline-offset: 2px;
}

button:focus:not(:focus-visible),
a:focus:not(:focus-visible) {
  outline: none;
}

/* ======================================
   RESPONSIVE
====================================== */
@media (max-width: 900px) {
  .header-content {
    flex-wrap: wrap;
    gap: var(--space-sm) var(--space-md);
    padding: var(--space-md);
  }

  .logo {
    order: 1;
  }

  .nav {
    order: 2;
    flex-wrap: wrap;
    gap: var(--space-sm);
  }

  .stats-bar {
    order: 3;
    margin-left: auto;
  }

  .search-container {
    order: 4;
    max-width: none;
    width: 100%;
  }

  .search-container--narrow {
    max-width: none;
  }

  .header.is-collapsed {
    box-shadow: var(--shadow-md);
  }

  .header.is-collapsed .header-content {
    flex-direction: row;
    flex-wrap: nowrap;
    align-items: center;
    padding: var(--space-sm) var(--space-md);
    gap: var(--space-lg);
  }

  .header.is-collapsed .logo {
    order: 1;
    flex-shrink: 0;
  }

  .header.is-collapsed .nav {
    order: 2;
    width: auto;
    flex-shrink: 0;
  }

  .header.is-collapsed .search-container {
    display: none;
  }

  .header.is-collapsed .stats-bar {
    order: 3;
    width: auto;
    margin-left: auto;
    flex-shrink: 0;
  }

  .header.is-collapsed .stat-item {
    display: none;
  }

  .header.is-collapsed .theme-toggle {
    display: flex;
  }

  .main-container {
    padding: var(--space-md);
  }

  .jobs-grid {
    grid-template-columns: minmax(0, 1fr);
  }
}

@media (max-width: 600px) {
  .stat-item:not(:first-child):not(:last-child) {
    display: none;
  }

  .results-header {
    flex-direction: column;
    align-items: flex-start;
    gap: var(--space-sm);
  }

  .sort-dropdown {
    width: 100%;
    justify-content: space-between;
    flex-wrap: wrap;
  }

  .sort-dropdown select {
    width: 100%;
    max-width: 260px;
  }

  .operator-buttons {
    flex-wrap: wrap;
  }

  .nav-link {
    padding: 6px 10px;
    font-size: 0.8125rem;
  }

  .job-card {
    padding: var(--space-md);
  }
}

/* ======================================
   NAVIGATION
====================================== */
.nav {
  display: flex;
  align-items: center;
  gap: 4px;
}

.nav-link {
  padding: 8px 14px;
  font-size: 0.875rem;
  font-weight: 500;
  color: var(--text-secondary);
  text-decoration: none;
  border-radius: var(--radius-sm);
  transition: all var(--duration-fast) var(--ease);
}

.nav-link:hover {
  color: var(--text-primary);
  background: var(--bg-muted);
}

.nav-link.active {
  color: var(--accent);
  background: var(--accent-muted);
}

/* Narrower search for institutions page */
.search-container--narrow {
  max-width: 400px;
}

/* ======================================
   INSTITUTIONS TABLE
====================================== */
.institutions-table-wrapper {
  background: var(--bg-surface);
  border: 1px solid var(--border-default);
  border-radius: var(--radius-lg);
  overflow-x: auto;
  -webkit-overflow-scrolling: touch;
}

.institutions-table {
  width: 100%;
  border-collapse: collapse;
}

.institutions-table thead {
  background: var(--bg-muted);
  border-bottom: 1px solid var(--border-default);
}

.institutions-table th {
  padding: var(--space-md) var(--space-lg);
  font-size: 0.75rem;
  font-weight: 600;
  text-transform: uppercase;
  letter-spacing: 0.04em;
  color: var(--text-muted);
  text-align: left;
}

.institutions-table td {
  padding: var(--space-md) var(--space-lg);
  vertical-align: middle;
  border-bottom: 1px solid var(--border-default);
}

.institution-row:last-child td {
  border-bottom: none;
}

.institution-row:hover {
  background: var(--bg-surface-hover);
}

.col-abbr {
  width: 100px;
}

.col-name {
  font-weight: 500;
  color: var(--text-primary);
}

.col-links {
  width: 200px;
}

.abbr-badge {
  display: inline-flex;
  align-items: center;
  height: 26px;
  padding: 0 10px;
  background: var(--accent-muted);
  border-radius: var(--radius-sm);
  font-size: 0.75rem;
  font-weight: 600;
  color: var(--accent);
  text-transform: uppercase;
  letter-spacing: 0.02em;
}

.links-group {
  display: flex;
  align-items: center;
  gap: var(--space-sm);
  flex-wrap: wrap;
}

.link-btn {
  display: inline-flex;
  align-items: center;
  gap: 6px;
  padding: 6px 12px;
  background: var(--bg-muted);
  border: 1px solid var(--border-default);
  border-radius: var(--radius-sm);
  font-size: 0.75rem;
  font-weight: 500;
  color: var(--text-secondary);
  text-decoration: none;
  transition: all var(--duration-fast) var(--ease);
}

.link-btn:hover {
  background: var(--bg-surface-hover);
  border-color: var(--border-strong);
  color: var(--text-primary);
}

.link-btn--primary {
  background: var(--accent-muted);
  border-color: transparent;
  color: var(--accent);
}

.link-btn--primary:hover {
  background: var(--accent);
  color: var(--text-inverse);
}

.link-btn svg {
  flex-shrink: 0;
}

/* Responsive table */
@media (max-width: 700px) {
  .institutions-table th,
  .institutions-table td {
    padding: var(--space-sm) var(--space-md);
  }

  .col-abbr {
    width: 80px;
  }

  .col-links {
    width: auto;
  }

  .link-btn {
    padding: 8px 10px;
  }

  .links-group {
    gap: var(--space-xs);
  }
}

@media (max-width: 540px) {
  .institutions-table {
    border-collapse: separate;
  }

  .institutions-table thead {
    display: none;
  }

  .institutions-table,
  .institutions-table tbody,
  .institutions-table tr,
  .institutions-table td {
    display: block;
    width: 100%;
  }

  .institution-row {
    margin: 0 0 var(--space-sm);
    padding: var(--space-md);
    background: var(--bg-surface);
    border: 1px solid var(--border-default);
    border-radius: var(--radius-md);
  }

  .institution-row:last-child {
    margin-bottom: 0;
  }

  .institution-row td {
    border-bottom: 0;
    padding: var(--space-xs) 0;
  }

  .institution-row td + td {
    margin-top: var(--space-sm);
  }

  .institution-row td::before {
    content: attr(data-label);
    display: block;
    font-size: 0.75rem;
    color: var(--text-muted);
    margin-bottom: 2px;
  }

  .link-btn {
    width: 100%;
    justify-content: center;
  }
}
