:root {
  --bg-primary: #ffffff;
  --bg-secondary: #f8f9fa;
  --bg-card: #ffffff;
  --text-primary: #1a1a2e;
  --text-secondary: #4a4a68;
  --text-muted: #6c757d;
  --border-color: #e9ecef;
  --accent-color: #0066cc;
  --accent-hover: #0052a3;
  --success-color: #28a745;
  --shadow-sm: 0 1px 3px rgba(0, 0, 0, 0.08);
  --shadow-md: 0 4px 12px rgba(0, 0, 0, 0.1);
  --shadow-lg: 0 8px 24px rgba(0, 0, 0, 0.12);
  --radius-sm: 6px;
  --radius-md: 10px;
  --radius-lg: 12px;
}

[data-theme="dark"] {
  --bg-primary: #0d1117;
  --bg-secondary: #161b22;
  --bg-card: #21262d;
  --text-primary: #f0f6fc;
  --text-secondary: #c9d1d9;
  --text-muted: #8b949e;
  --border-color: #30363d;
  --accent-color: #58a6ff;
  --accent-hover: #79b8ff;
  --shadow-sm: 0 1px 3px rgba(0, 0, 0, 0.3);
  --shadow-md: 0 4px 12px rgba(0, 0, 0, 0.4);
  --shadow-lg: 0 8px 24px rgba(0, 0, 0, 0.5);
}

@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) {
    --bg-primary: #0d1117;
    --bg-secondary: #161b22;
    --bg-card: #21262d;
    --text-primary: #f0f6fc;
    --text-secondary: #c9d1d9;
    --text-muted: #8b949e;
    --border-color: #30363d;
    --accent-color: #58a6ff;
    --accent-hover: #79b8ff;
    --shadow-sm: 0 1px 3px rgba(0, 0, 0, 0.3);
    --shadow-md: 0 4px 12px rgba(0, 0, 0, 0.4);
    --shadow-lg: 0 8px 24px rgba(0, 0, 0, 0.5);
  }
}

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

body {
  font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, Oxygen, Ubuntu, sans-serif;
  background-color: var(--bg-primary);
  color: var(--text-primary);
  line-height: 1.5;
  min-height: 100vh;
}

.container {
  width: 100%;
  max-width: 100%;
  margin: 0 auto;
  padding: 1.5rem;
}

header {
  text-align: center;
  margin-bottom: 1.5rem;
}

.header-top {
  display: flex;
  justify-content: center;
  align-items: center;
  gap: 1rem;
  margin-bottom: 0.25rem;
}

h1 {
  font-size: 2rem;
  font-weight: 700;
  background: linear-gradient(135deg, var(--accent-color), #00c853);
  -webkit-background-clip: text;
  -webkit-text-fill-color: transparent;
  background-clip: text;
}

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

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

.theme-icon {
  font-size: 1.1rem;
}

.data-notice {
  color: var(--text-muted);
  font-size: 0.8rem;
  margin-bottom: 0.5rem;
}

.data-notice span {
  color: var(--text-secondary);
}

.subtitle {
  color: var(--text-secondary);
  font-size: 1rem;
  margin-bottom: 0.5rem;
}

.doc-link {
  margin-bottom: 1rem;
}

.doc-link a {
  color: var(--accent-color);
  text-decoration: none;
  font-size: 0.9rem;
}

.doc-link a:hover {
  text-decoration: underline;
}

.doc-link .separator {
  color: var(--text-muted);
  margin: 0 0.5rem;
}

.stats {
  display: flex;
  justify-content: center;
  gap: 1.5rem;
  margin-bottom: 1rem;
  flex-wrap: wrap;
}

.stat-item {
  display: flex;
  flex-direction: column;
  align-items: center;
  padding: 0.75rem 1.25rem;
  background: var(--bg-secondary);
  border-radius: var(--radius-md);
  border: 1px solid var(--border-color);
}

.stat-value {
  font-size: 1.5rem;
  font-weight: 700;
  color: var(--accent-color);
}

.stat-label {
  font-size: 0.75rem;
  color: var(--text-muted);
  text-transform: uppercase;
  letter-spacing: 0.5px;
}

.controls {
  display: flex;
  gap: 0.75rem;
  justify-content: center;
  flex-wrap: wrap;
  margin-top: 0.75rem;
}

.controls input,
.controls select {
  padding: 0.5rem 0.75rem;
  font-size: 0.9rem;
  border: 1px solid var(--border-color);
  border-radius: var(--radius-sm);
  background: var(--bg-card);
  color: var(--text-primary);
  min-width: 160px;
  transition: border-color 0.2s, box-shadow 0.2s;
}

.controls input:focus,
.controls select:focus {
  outline: none;
  border-color: var(--accent-color);
  box-shadow: 0 0 0 3px rgba(0, 102, 204, 0.15);
}

.results-info {
  text-align: center;
  color: var(--text-muted);
  font-size: 0.85rem;
  margin-bottom: 1rem;
}

.collections-grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(280px, 1fr));
  gap: 0.75rem;
}

.collections-grid > * {
  min-width: 0;
}

.collection-card {
  background: var(--bg-card);
  border: 1px solid var(--border-color);
  border-radius: var(--radius-lg);
  padding: 0.875rem;
  transition: transform 0.15s, box-shadow 0.15s;
  cursor: pointer;
  display: flex;
  flex-direction: column;
  min-height: 140px;
  min-width: 0;
  overflow: hidden;
}

.collection-card:hover {
  transform: translateY(-2px);
  box-shadow: var(--shadow-md);
}

.collection-card h3 {
  font-size: 0.95rem;
  font-weight: 600;
  margin-bottom: 0.4rem;
  color: var(--text-primary);
  display: -webkit-box;
  -webkit-line-clamp: 2;
  -webkit-box-orient: vertical;
  overflow: hidden;
  line-height: 1.3;
  overflow-wrap: anywhere;
  word-break: break-word;
}

.collection-card .description {
  color: var(--text-secondary);
  font-size: 0.8rem;
  margin-bottom: 0.5rem;
  display: -webkit-box;
  -webkit-line-clamp: 2;
  -webkit-box-orient: vertical;
  overflow: hidden;
  flex-grow: 1;
  line-height: 1.4;
  overflow-wrap: anywhere;
  word-break: break-word;
}

.collection-meta {
  display: flex;
  flex-wrap: wrap;
  gap: 0.35rem;
  margin-top: auto;
}

.meta-tag {
  display: inline-flex;
  align-items: center;
  gap: 0.2rem;
  padding: 0.15rem 0.4rem;
  background: var(--bg-secondary);
  border-radius: 4px;
  font-size: 0.7rem;
  color: var(--text-muted);
  white-space: nowrap;
  max-width: 100%;
  overflow: hidden;
  text-overflow: ellipsis;
}

.meta-tag.agency {
  background: rgba(0, 102, 204, 0.1);
  color: var(--accent-color);
}

.meta-tag.frequency {
  background: rgba(40, 167, 69, 0.1);
  color: var(--success-color);
}

.meta-tag.updated {
  background: rgba(108, 117, 125, 0.1);
  color: var(--text-muted);
}

.collection-footer {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-top: 0.5rem;
  padding-top: 0.5rem;
  border-top: 1px solid var(--border-color);
}

.datasets-count {
  font-size: 0.75rem;
  color: var(--text-muted);
}

.view-link {
  display: inline-flex;
  align-items: center;
  gap: 0.2rem;
  color: var(--accent-color);
  text-decoration: none;
  font-size: 0.75rem;
  font-weight: 500;
  transition: color 0.2s;
  min-width: 0;
  overflow-wrap: anywhere;
  word-break: break-word;
}

.view-link:hover {
  color: var(--accent-hover);
  text-decoration: underline;
}

.loading,
.no-results {
  text-align: center;
  padding: 2rem;
  color: var(--text-muted);
  font-size: 1rem;
}

.no-results {
  display: none;
}

/* Modal */
.modal {
  display: none;
  position: fixed;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
  background: rgba(0, 0, 0, 0.6);
  z-index: 1000;
  align-items: center;
  justify-content: center;
  padding: 1.5rem;
  backdrop-filter: blur(4px);
}

.modal.active {
  display: flex;
}

.modal-content {
  background: var(--bg-card);
  border-radius: var(--radius-lg);
  max-width: 700px;
  width: 100%;
  max-height: 90vh;
  overflow-y: auto;
  position: relative;
  box-shadow: var(--shadow-lg);
}

.modal-close {
  position: absolute;
  top: 0.75rem;
  right: 0.75rem;
  background: var(--bg-secondary);
  border: none;
  width: 32px;
  height: 32px;
  border-radius: 50%;
  font-size: 1.25rem;
  cursor: pointer;
  color: var(--text-secondary);
  transition: background 0.2s;
  display: flex;
  align-items: center;
  justify-content: center;
  line-height: 1;
}

.modal-close:hover {
  background: var(--border-color);
}

#modalBody {
  padding: 1.5rem;
}

.modal-header {
  margin-bottom: 1.25rem;
}

.modal-header h2 {
  font-size: 1.25rem;
  margin-bottom: 0.4rem;
}

.modal-header .description {
  color: var(--text-secondary);
  font-size: 0.9rem;
}

.modal-section {
  margin-bottom: 1.25rem;
}

.modal-section h4 {
  font-size: 0.8rem;
  text-transform: uppercase;
  letter-spacing: 0.5px;
  color: var(--text-muted);
  margin-bottom: 0.6rem;
}

.info-grid {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(140px, 1fr));
  gap: 0.75rem;
}

.info-item {
  padding: 0.6rem 0.75rem;
  background: var(--bg-secondary);
  border-radius: var(--radius-sm);
}

.info-item label {
  display: block;
  font-size: 0.65rem;
  color: var(--text-muted);
  text-transform: uppercase;
  letter-spacing: 0.5px;
  margin-bottom: 0.15rem;
}

.info-item span {
  font-weight: 500;
  color: var(--text-primary);
  font-size: 0.85rem;
}

.datasets-list {
  list-style: none;
  max-height: 250px;
  overflow-y: auto;
}

.datasets-list li {
  margin-bottom: 0.5rem;
}

.dataset-card {
  display: block;
  padding: 0.75rem;
  background: var(--bg-secondary);
  border-radius: var(--radius-sm);
}

.dataset-header {
  display: flex;
  justify-content: space-between;
  align-items: flex-start;
  gap: 0.5rem;
  margin-bottom: 0.35rem;
}

.datasets-list .dataset-name {
  font-weight: 500;
  font-size: 0.85rem;
  color: var(--text-primary);
  line-height: 1.3;
}

.datasets-list .dataset-format {
  font-size: 0.65rem;
  padding: 0.15rem 0.4rem;
  background: var(--accent-color);
  color: white;
  border-radius: 4px;
  flex-shrink: 0;
  text-transform: uppercase;
  font-weight: 500;
}

.dataset-description {
  font-size: 0.8rem;
  color: var(--text-secondary);
  margin-bottom: 0.5rem;
  display: -webkit-box;
  -webkit-line-clamp: 2;
  -webkit-box-orient: vertical;
  overflow: hidden;
  line-height: 1.4;
}

.dataset-meta {
  display: flex;
  flex-wrap: wrap;
  gap: 0.5rem 1rem;
  font-size: 0.7rem;
  color: var(--text-muted);
}

.dataset-meta-item {
  white-space: normal;
}

.dataset-meta-item strong {
  color: var(--text-secondary);
  font-weight: 500;
}

.dataset-actions {
  display: flex;
  flex-wrap: wrap;
  gap: 0.4rem;
  margin-top: 0.6rem;
  padding-top: 0.6rem;
  border-top: 1px solid var(--border-color);
}

.dataset-btn {
  display: inline-flex;
  align-items: center;
  gap: 0.25rem;
  padding: 0.35rem 0.6rem;
  font-size: 0.7rem;
  font-weight: 500;
  border-radius: 4px;
  text-decoration: none;
  cursor: pointer;
  border: none;
  transition: background 0.15s, opacity 0.15s;
}

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

.dataset-btn-view {
  background: var(--accent-color);
  color: white;
}

.dataset-btn-view:hover {
  background: var(--accent-hover);
}

.dataset-btn-download {
  background: var(--success-color);
  color: white;
}

.dataset-btn-download:hover:not(:disabled) {
  background: #218838;
}

.dataset-btn-json {
  background: #6f42c1;
  color: white;
}

.dataset-btn-json:hover {
  background: #5a32a3;
}

.btn-primary {
  display: inline-flex;
  align-items: center;
  gap: 0.4rem;
  padding: 0.6rem 1.25rem;
  background: var(--accent-color);
  color: white;
  text-decoration: none;
  border-radius: var(--radius-sm);
  font-weight: 500;
  font-size: 0.9rem;
  transition: background 0.2s;
}

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

.metadata-loading {
  text-align: center;
  padding: 1.5rem;
  color: var(--text-muted);
  font-size: 0.85rem;
}

@media (max-width: 768px) {
  .container {
    padding: 1rem;
  }

  .header-top {
    flex-direction: column;
    gap: 0.5rem;
  }
  
  h1 {
    font-size: 1.5rem;
  }
  
  .collections-grid {
    grid-template-columns: 1fr;
  }
  
  .controls {
    flex-direction: column;
  }
  
  .controls input,
  .controls select {
    width: 100%;
    min-width: unset;
  }
  
  .stats {
    gap: 0.75rem;
  }
  
  .stat-item {
    padding: 0.5rem 0.75rem;
  }

  .modal {
    padding: 0.75rem;
  }

  .modal-content {
    max-height: 92vh;
  }

  #modalBody {
    padding: 1rem;
  }

  .info-grid {
    grid-template-columns: 1fr;
  }

  .dataset-header {
    flex-direction: column;
    align-items: flex-start;
  }

  .dataset-actions {
    flex-direction: column;
  }

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

  .collection-footer {
    flex-direction: column;
    align-items: flex-start;
    gap: 0.35rem;
  }

  .collection-meta {
    width: 100%;
  }

  .meta-tag {
    max-width: 100%;
  }
}

@media (max-width: 480px) {
  .container {
    padding: 0.75rem;
  }

  h1 {
    font-size: 1.3rem;
  }

  .subtitle {
    font-size: 0.9rem;
  }

  .doc-link {
    display: flex;
    flex-direction: column;
    gap: 0.25rem;
  }

  .doc-link .separator {
    display: none;
  }

  .stat-item {
    min-width: 100px;
    flex: 1 1 calc(50% - 0.5rem);
  }

  .collections-grid {
    grid-template-columns: 1fr;
    gap: 0.6rem;
  }

  .collection-card {
    padding: 0.75rem;
  }

  .datasets-list {
    max-height: 320px;
  }
}

@media (min-width: 1400px) {
  .collections-grid {
    grid-template-columns: repeat(auto-fill, minmax(260px, 1fr));
  }
}

@media (min-width: 1800px) {
  .collections-grid {
    grid-template-columns: repeat(auto-fill, minmax(240px, 1fr));
  }
}
