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

:root {
  --font: "Inter", -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, sans-serif;
  --mono: "JetBrains Mono", "Fira Code", "Consolas", monospace;
  --bg: #ffffff;
  --bg-alt: #f7f8fa;
  --fg: #1a1a2e;
  --fg-muted: #5a5a72;
  --border: #e2e4e9;
  --accent: #00735c;
  --accent-light: #d6efe9;
  --highlight: #fff3cd;
  --card-shadow: 0 1px 3px rgba(0, 0, 0, 0.06);
  --radius: 6px;
  --max-width: 800px;
}

@media (prefers-color-scheme: dark) {
  :root {
    --bg: #16171d;
    --bg-alt: #1e1f27;
    --fg: #e0e0e8;
    --fg-muted: #9090a8;
    --border: #2e303a;
    --accent: #4db6ac;
    --accent-light: #163a36;
    --highlight: #3a3520;
    --card-shadow: 0 1px 3px rgba(0, 0, 0, 0.3);
  }
}

html {
  font-size: 15px;
  line-height: 1.6;
}

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

a {
  color: var(--accent);
  text-decoration: none;
}
a:hover {
  text-decoration: underline;
}

.container {
  width: 100%;
  max-width: var(--max-width);
  margin: 0 auto;
  padding: 0 1.25rem;
}

/* --- Top navigation (slim sticky utility bar) --- */

.top-nav {
  position: sticky;
  top: 0;
  z-index: 20;
  background: var(--bg);
  border-bottom: 1px solid var(--border);
}

.top-nav-inner {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 1rem;
  height: 3rem;
}

.top-nav-brand {
  font-weight: 700;
  font-size: 0.95rem;
  letter-spacing: -0.02em;
  color: var(--fg);
}

.top-nav-brand:hover {
  color: var(--accent);
  text-decoration: none;
}

.top-nav-links {
  display: flex;
  align-items: center;
  gap: 1.25rem;
  font-size: 0.85rem;
}

.top-nav-links a {
  color: var(--fg-muted);
  font-weight: 500;
}

.top-nav-links a:hover {
  color: var(--accent);
  text-decoration: none;
}

.top-nav-links a.ext-link {
  display: inline-flex;
  align-items: center;
  gap: 0.25rem;
}

.ext-icon {
  flex-shrink: 0;
  opacity: 0.7;
}

.top-nav-links a:hover .ext-icon {
  opacity: 1;
}

.top-nav-links a:focus-visible,
.top-nav-brand:focus-visible {
  outline: 2px solid var(--accent);
  outline-offset: 2px;
  border-radius: 2px;
}

.visually-hidden {
  position: absolute;
  width: 1px;
  height: 1px;
  padding: 0;
  margin: -1px;
  overflow: hidden;
  clip: rect(0, 0, 0, 0);
  white-space: nowrap;
  border: 0;
}

/* The HF panel clears the sticky bar when jumped to. */
#hf-panel {
  scroll-margin-top: 3.75rem;
}

/* --- Header --- */

header {
  padding: 2.5rem 0 1.5rem;
  text-align: center;
  border-bottom: 1px solid var(--border);
}

header h1 {
  font-size: 1.75rem;
  font-weight: 700;
  letter-spacing: -0.02em;
}

.header-title-link {
  color: inherit;
  text-decoration: none;
}

.subtitle {
  color: var(--fg-muted);
  font-size: 0.9rem;
  margin-top: 0.3rem;
}

.powered-by {
  font-size: 0.8rem;
  color: var(--fg-muted);
  margin-top: 0.15rem;
}

/* --- Search --- */

main {
  flex: 1;
  padding: 2rem 0;
}

.search-section {
  margin-bottom: 1.5rem;
}

.search-bar {
  display: flex;
  gap: 0.5rem;
  flex-wrap: wrap;
}

.search-bar select {
  flex: 0 0 auto;
  padding: 0.55rem 0.75rem;
  border: 1px solid var(--border);
  border-radius: var(--radius);
  background: var(--bg-alt);
  color: var(--fg);
  font-family: var(--font);
  font-size: 0.85rem;
}

.search-bar input {
  flex: 1 1 200px;
  padding: 0.55rem 0.85rem;
  border: 1px solid var(--border);
  border-radius: var(--radius);
  background: var(--bg);
  color: var(--fg);
  font-family: var(--font);
  font-size: 0.95rem;
  outline: none;
  transition: border-color 0.15s;
}
.search-bar input:focus {
  border-color: var(--accent);
}

button {
  padding: 0.55rem 1.1rem;
  border: 1px solid var(--border);
  border-radius: var(--radius);
  background: var(--bg-alt);
  color: var(--fg);
  font-family: var(--font);
  font-size: 0.85rem;
  cursor: pointer;
  transition: background 0.15s, border-color 0.15s;
  white-space: nowrap;
}
button:hover {
  background: var(--border);
}

button.primary {
  background: var(--accent);
  color: #fff;
  border-color: var(--accent);
}
button.primary:hover {
  opacity: 0.9;
}

button:disabled {
  opacity: 0.5;
  cursor: not-allowed;
}

/* --- Status --- */

.status {
  margin-top: 0.75rem;
  padding: 0.6rem 0.85rem;
  border-radius: var(--radius);
  font-size: 0.85rem;
  background: var(--accent-light);
  color: var(--fg);
}

.status.error {
  background: #fce4e4;
  color: #8b1a1a;
}

@media (prefers-color-scheme: dark) {
  .status.error {
    background: #3a1c1c;
    color: #f0a0a0;
  }
}

/* --- Results --- */

.results {
  margin-bottom: 2rem;
}

.count-result {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  justify-content: space-between;
  gap: 0.5rem 1rem;
  padding: 0.75rem 1rem;
  background: var(--bg-alt);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  margin-bottom: 1rem;
  font-size: 0.9rem;
}

.count-result[hidden] {
  display: none;
}

.hf-toggle {
  flex-shrink: 0;
  padding: 0.3rem 0.7rem;
  font-size: 0.85rem;
  font-weight: 600;
  color: var(--accent);
  background: var(--bg);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  cursor: pointer;
}

.hf-toggle:hover {
  border-color: var(--accent);
}

.hf-caret {
  display: inline-block;
  transition: transform 0.15s;
}

.hf-toggle.open .hf-caret {
  transform: rotate(180deg);
}

.count-result strong {
  font-variant-numeric: tabular-nums;
}

/* --- Scope bar (year-range control under the search bar) --- */

.scope-bar {
  display: flex;
  align-items: center;
  gap: 0.6rem;
  flex-wrap: wrap;
  margin-top: 0.6rem;
  font-size: 0.85rem;
  color: var(--fg-muted);
}

.scope-bar[hidden] {
  display: none;
}

.scope-label {
  font-weight: 600;
  color: var(--fg);
}

/* Preset pills: one-click common scopes, ordered fast -> slow. */
.scope-presets {
  display: inline-flex;
  gap: 0.3rem;
}

.scope-preset {
  display: inline-flex;
  align-items: center;
  gap: 0.25rem;
  padding: 0.4rem 0.8rem;
  font-size: 0.8rem;
  border: 1px solid var(--border);
  border-radius: 999px;
  background: var(--bg-alt);
  color: var(--fg-muted);
  cursor: pointer;
  transition: border-color 0.15s, color 0.15s, background 0.15s;
}

.scope-preset:hover {
  border-color: var(--accent);
  color: var(--fg);
}

.scope-preset.active {
  background: var(--accent);
  border-color: var(--accent);
  color: #fff;
}

.scope-preset:focus-visible {
  outline: 2px solid var(--accent);
  outline-offset: 2px;
}

/* Custom-range disclosure + popover (precise from/to, rare case). */
.scope-custom {
  position: relative;
  display: inline-flex;
}

.scope-custom-toggle {
  padding: 0.4rem 0.7rem;
  font-size: 0.8rem;
  border: 1px solid transparent;
  border-radius: 999px;
  background: transparent;
  color: var(--fg-muted);
  cursor: pointer;
}

.scope-custom-toggle:hover {
  color: var(--fg);
}

.scope-custom-toggle.active {
  background: var(--accent);
  color: #fff;
}

.scope-caret {
  display: inline-block;
  transition: transform 0.15s;
}

.scope-custom-toggle.is-open .scope-caret {
  transform: rotate(180deg);
}

.scope-popover {
  position: absolute;
  top: calc(100% + 0.4rem);
  left: 0;
  z-index: 20;
  min-width: 11rem;
  padding: 0.75rem;
  background: var(--bg);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  box-shadow: var(--card-shadow);
}

.scope-popover[hidden] {
  display: none;
}

.scope-popover-row {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 0.6rem;
  margin-bottom: 0.4rem;
}

.scope-popover-row label {
  color: var(--fg-muted);
}

.scope-popover-row select {
  padding: 0.3rem 0.4rem;
  border: 1px solid var(--border);
  border-radius: var(--radius);
  background: var(--bg-alt);
  color: var(--fg);
  font-family: var(--font);
  font-size: 0.85rem;
  font-variant-numeric: tabular-nums;
  cursor: pointer;
}

.scope-popover-hint {
  margin-top: 0.5rem;
  font-size: 0.78rem;
  color: var(--fg-muted);
}

/* Conditional slow-scope warning (only when all years are selected). */
.scope-speed-hint {
  flex-basis: 100%;
  font-size: 0.8rem;
  color: var(--fg-muted);
}

.scope-speed-hint[hidden] {
  display: none;
}

@media (prefers-reduced-motion: reduce) {
  .scope-caret {
    transition: none;
  }
}

@media (max-width: 600px) {
  .scope-popover {
    left: 0;
    right: 0;
  }
}

/* --- Document cards --- */

.doc-card {
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 1rem 1.15rem;
  margin-bottom: 0.75rem;
  background: var(--bg);
  box-shadow: var(--card-shadow);
}

.doc-card-header {
  margin-bottom: 0.5rem;
}

.doc-title {
  font-weight: 600;
  font-size: 0.95rem;
  line-height: 1.35;
}

.doc-meta {
  display: flex;
  flex-wrap: wrap;
  gap: 0.25rem 0.75rem;
  font-size: 0.78rem;
  color: var(--fg-muted);
  margin-top: 0.2rem;
}

.doc-text {
  font-family: var(--mono);
  font-size: 0.8rem;
  line-height: 1.55;
  background: var(--bg-alt);
  padding: 0.75rem;
  border-radius: var(--radius);
  white-space: pre-wrap;
  word-break: break-word;
  overflow-x: auto;
  max-height: 16rem;
  overflow-y: auto;
}

.doc-text mark {
  background: var(--highlight);
  color: inherit;
  padding: 0.05em 0.15em;
  border-radius: 2px;
}

.doc-links {
  margin-top: 0.5rem;
  font-size: 0.78rem;
}

.load-more {
  display: block;
  margin: 0.75rem auto 0;
}

pre {
  background: var(--bg-alt);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 0.75rem 1rem;
  overflow-x: auto;
  font-size: 0.78rem;
  line-height: 1.5;
  margin-bottom: 0.5rem;
}

code {
  font-family: var(--mono);
}

/* --- HF Panel --- */

.hf-panel {
  margin-top: 1.5rem;
  margin-bottom: 1.5rem;
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 0;
}

.hf-filter-row {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  gap: 0.75rem;
  margin: 0.75rem 0;
  font-size: 0.85rem;
}

.hf-filter-row[hidden] {
  display: none;
}

.filter-hint {
  color: var(--fg-muted);
  font-size: 0.78rem;
}

.hf-panel-content {
  padding: 1rem;
}

.hf-panel-content p {
  font-size: 0.85rem;
  color: var(--fg-muted);
  margin-bottom: 0.5rem;
}

.collect-row {
  display: flex;
  align-items: center;
  gap: 0.75rem;
  flex-wrap: wrap;
  margin-bottom: 0.75rem;
}

.collect-row label {
  font-size: 0.82rem;
  color: var(--fg-muted);
}

.collect-row input[type="number"] {
  width: 5rem;
  padding: 0.35rem 0.5rem;
  border: 1px solid var(--border);
  border-radius: var(--radius);
  background: var(--bg);
  color: var(--fg);
  font-family: var(--font);
  font-size: 0.85rem;
}

.progress-bar {
  width: 100%;
  height: 6px;
  background: var(--border);
  border-radius: 3px;
  overflow: hidden;
  margin-bottom: 0.75rem;
}

.progress-bar[hidden] {
  display: none;
}

.progress-bar-fill {
  height: 100%;
  width: 0%;
  background: var(--accent);
  border-radius: 3px;
  transition: width 0.15s ease;
}

.hf-table-wrap {
  overflow-x: auto;
  margin-bottom: 0.75rem;
}

.hf-table {
  width: 100%;
  border-collapse: collapse;
  font-size: 0.78rem;
}

.hf-table th,
.hf-table td {
  padding: 0.4rem 0.6rem;
  text-align: left;
  border-bottom: 1px solid var(--border);
}

.hf-table th {
  font-weight: 600;
  background: var(--bg-alt);
  position: sticky;
  top: 0;
}

.hf-table tbody tr:nth-child(even) {
  background: var(--bg-alt);
}

.hf-table td {
  max-width: 18rem;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.export-row {
  display: flex;
  gap: 0.5rem;
  flex-wrap: wrap;
  margin-bottom: 0.75rem;
}

.export-row[hidden] {
  display: none;
}

.copy-btn {
  position: relative;
}

.copy-btn.copied {
  pointer-events: none;
}

.copy-btn.copied::after {
  content: "Copied!";
  position: absolute;
  top: -1.6rem;
  left: 50%;
  transform: translateX(-50%);
  font-size: 0.72rem;
  background: var(--fg);
  color: var(--bg);
  padding: 0.15rem 0.45rem;
  border-radius: 3px;
  white-space: nowrap;
}

.hf-open {
  margin-top: 0.75rem;
}

.hf-estimate {
  margin-top: 0.5rem;
  font-size: 0.78rem;
  color: var(--fg-muted);
}

.code-tabs {
  display: flex;
  align-items: center;
  gap: 0.25rem;
  margin-bottom: 0.4rem;
}

.code-tab {
  padding: 0.3rem 0.8rem;
  font-size: 0.8rem;
  font-weight: 500;
  color: var(--fg-muted);
  background: var(--bg-alt);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  cursor: pointer;
}

.code-tab[aria-pressed="true"] {
  color: #fff;
  background: var(--accent);
  border-color: var(--accent);
}

.code-copy {
  margin-left: auto;
  font-size: 0.72rem;
  padding: 0.25rem 0.55rem;
}

.code-block {
  margin: 0;
  max-height: 22rem;
  overflow: auto;
}

.preview {
  margin-top: 1rem;
  border-top: 1px solid var(--border);
  padding-top: 0.75rem;
}

.preview > summary {
  cursor: pointer;
  font-size: 0.85rem;
  font-weight: 600;
  color: var(--accent);
  list-style: none;
}

.preview > summary::-webkit-details-marker {
  display: none;
}

.preview[open] > summary .hf-caret {
  transform: rotate(180deg);
}

.preview-body {
  margin-top: 0.75rem;
}

/* --- Footer --- */

footer {
  border-top: 1px solid var(--border);
  padding: 1.25rem 0;
  text-align: center;
  font-size: 0.8rem;
  color: var(--fg-muted);
}

footer p + p {
  margin-top: 0.2rem;
}

/* --- Legal pages --- */

.legal h2 {
  font-size: 1.3rem;
  margin-bottom: 1.25rem;
}

.legal h3 {
  font-size: 0.95rem;
  font-weight: 600;
  margin: 1.25rem 0 0.35rem;
}

.legal p {
  font-size: 0.88rem;
  line-height: 1.65;
  margin-bottom: 0.5rem;
}

.legal ul {
  font-size: 0.88rem;
  line-height: 1.65;
  margin: 0.25rem 0 0.75rem 1.25rem;
}

/* --- API reference page (rendered from API_REFERENCE.md) --- */

.api-doc {
  padding-bottom: 2rem;
}

.api-doc h1 {
  font-size: 1.6rem;
  font-weight: 700;
  letter-spacing: -0.02em;
  margin-bottom: 0.75rem;
}

.api-doc h2 {
  font-size: 1.2rem;
  font-weight: 700;
  margin: 2rem 0 0.75rem;
  padding-top: 0.75rem;
  border-top: 1px solid var(--border);
}

.api-doc h3 {
  font-size: 1rem;
  font-weight: 600;
  margin: 1.5rem 0 0.5rem;
}

.api-doc p {
  margin-bottom: 0.75rem;
}

.api-doc ul,
.api-doc ol {
  margin: 0.5rem 0 0.75rem 1.5rem;
}

.api-doc li {
  margin-bottom: 0.2rem;
}

.api-doc a {
  text-decoration: underline;
}

.api-doc hr {
  border: 0;
  border-top: 1px solid var(--border);
  margin: 1.5rem 0;
}

.api-doc :not(pre) > code {
  background: var(--bg-alt);
  padding: 0.1em 0.35em;
  border-radius: 3px;
  font-size: 0.85em;
}

.api-doc pre {
  margin: 0.5rem 0 1rem;
}

.api-doc table {
  display: block;
  width: 100%;
  overflow-x: auto;
  border-collapse: collapse;
  margin: 0.75rem 0 1.25rem;
  font-size: 0.85rem;
}

.api-doc th,
.api-doc td {
  padding: 0.45rem 0.65rem;
  text-align: left;
  border: 1px solid var(--border);
  vertical-align: top;
}

.api-doc th {
  background: var(--bg-alt);
  font-weight: 600;
}

/* Anchor jumps (the doc's own ToC links) clear the sticky nav. */
.api-doc :target {
  scroll-margin-top: 3.75rem;
}

/* --- Responsive --- */

@media (max-width: 600px) {
  .top-nav-links {
    gap: 0.85rem;
    font-size: 0.8rem;
  }

  header {
    padding: 1.5rem 0 1rem;
  }

  header h1 {
    font-size: 1.35rem;
  }

  .search-bar {
    flex-direction: column;
  }

  .search-bar select,
  .search-bar input,
  .search-bar button {
    width: 100%;
  }
}
