/* ---------------------------------------------------------------------
   Grant Explorer — compact embeddable widget.
   Fixed cream background (#FCF7ED) to match the host page, neutral
   (grayscale) palette otherwise. Max 800px wide, height fits content —
   built to live inside an <iframe> sized to match (see /demo).
--------------------------------------------------------------------- */

:root {
  --bg: #FCF7ED;
  --text: #1d1d1f;
  --text-secondary: #6e6e73;
  --text-tertiary: #a1a1a6;
  --border: rgba(0, 0, 0, 0.15);
  --border-soft: rgba(0, 0, 0, 0.09);
  --fill: rgba(0, 0, 0, 0.045);
  --fill-strong: rgba(0, 0, 0, 0.075);
  --surface: rgba(255, 255, 255, 0.96);
  --row-hover: rgba(0, 0, 0, 0.035);
  --row-stripe: rgba(0, 0, 0, 0.015);
  --accent: #1d1d1f;
  --accent-hover: #000000;
  --accent-contrast: #ffffff;
  --focus-ring: rgba(0, 0, 0, 0.16);
  --danger: #c9271b;
  --shadow-popover: 0 12px 32px rgba(0, 0, 0, 0.16), 0 2px 8px rgba(0, 0, 0, 0.08);

  --radius-sm: 6px;
  --radius: 10px;
  --font: -apple-system, BlinkMacSystemFont, "SF Pro Text", "Segoe UI", system-ui, sans-serif;
  --mono: ui-monospace, "SF Mono", "Cascadia Mono", "Cascadia Code", Consolas, monospace;
  --ease-out: cubic-bezier(0.23, 1, 0.32, 1);
}

/* Fixed cream background is a deliberate brand match, not a system-theme
   surface — so this widget no longer flips with prefers-color-scheme:
   dark text on a light cream background would be illegible either way. */

* { box-sizing: border-box; }

html, body {
  margin: 0;
  /* Auto height — the page is exactly as tall as its content (toolbar +
     however many rows are on the page). No internal scrollbar anywhere.
     Set the embedding <iframe>'s height to match (see DEPLOY.md). */
  background: var(--bg);
}

body {
  color: var(--text);
  font: 400 13px/1.45 var(--font);
  -webkit-font-smoothing: antialiased;
}

code, .mono { font-family: var(--mono); }
.muted { color: var(--text-secondary); }
.small { font-size: 11.5px; }

/* ---------------------------------------------------------------------
   Loading overlay — shown while the parquet file downloads and DuckDB-WASM
   initializes (no backend, so this replaces a server-side "ready" state)
--------------------------------------------------------------------- */

.loading {
  max-width: 800px;
  margin: 0 auto;
  padding: 40px 20px;
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 12px;
  text-align: center;
}
/* Author styles always beat the browser's default [hidden]{display:none} —
   even at equal specificity, origin (author vs UA) is checked before
   specificity — so `.loading { display: flex }` above silently overrode
   the hidden attribute and the overlay stayed visible after load finished. */
.loading[hidden] {
  display: none;
}

.spinner {
  width: 22px;
  height: 22px;
  border-radius: 50%;
  border: 2.5px solid var(--border-soft);
  border-top-color: var(--text);
  animation: spin 700ms linear infinite;
}

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

.loading-status { font-size: 12.5px; color: var(--text-secondary); }
.loading-bar-track {
  width: 220px;
  height: 4px;
  border-radius: 2px;
  background: var(--fill-strong);
  overflow: hidden;
}
.loading-bar-fill {
  height: 100%;
  background: var(--text);
  width: 0%;
  transition: width 160ms var(--ease-out);
}
.loading.error .loading-status { color: var(--danger); }

/* ---------------------------------------------------------------------
   App shell — fixed height, capped width, transparent
--------------------------------------------------------------------- */

.app {
  max-width: 800px;
  margin: 0 auto;
  min-height: 120px;
  display: flex;
  flex-direction: column;
  gap: 8px;
  background: transparent;
}

.footnote {
  max-width: 800px;
  margin: 10px auto 0;
  padding: 0 1px;
  text-align: center;
}

/* ---------------------------------------------------------------------
   Toolbar
--------------------------------------------------------------------- */

.toolbar {
  display: flex;
  align-items: center;
  gap: 8px;
  flex: 0 0 auto;
}

.search-box {
  position: relative;
  display: flex;
  align-items: center;
  flex: 1 1 auto;
  min-width: 0;
}

.search-icon {
  position: absolute;
  left: 9px;
  width: 14px;
  height: 14px;
  color: var(--text-tertiary);
  pointer-events: none;
}

.search-box input[type="text"] {
  width: 100%;
  padding: 7px 10px 7px 30px;
}

input[type="text"], input[type="number"] {
  font: inherit;
  font-size: 13px;
  color: var(--text);
  background: var(--fill);
  border: 1px solid var(--border-soft);
  border-radius: var(--radius-sm);
  padding: 7px 9px;
  transition: border-color 140ms var(--ease-out), box-shadow 140ms var(--ease-out), background 140ms var(--ease-out);
  min-width: 0;
}

input[type="text"]:focus, input[type="number"]:focus, textarea:focus {
  outline: none;
  border-color: var(--text-tertiary);
  box-shadow: 0 0 0 3px var(--focus-ring);
  background: var(--surface);
}

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

.btn {
  display: inline-flex;
  align-items: center;
  gap: 5px;
  font: inherit;
  font-size: 12.5px;
  font-weight: 550;
  color: var(--text);
  background: var(--fill);
  border: 1px solid var(--border-soft);
  border-radius: var(--radius-sm);
  padding: 7px 12px;
  cursor: pointer;
  transition: transform 120ms var(--ease-out), background 140ms var(--ease-out), border-color 140ms var(--ease-out), opacity 140ms var(--ease-out);
  white-space: nowrap;
}

.btn:hover { background: var(--fill-strong); }
.btn:active { transform: scale(0.97); }
.btn:disabled { opacity: 0.4; cursor: default; pointer-events: none; }

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

.btn-sm { padding: 5px 10px; font-size: 12px; }

.link-btn {
  border: none;
  background: none;
  color: var(--text-secondary);
  font: inherit;
  font-size: 12px;
  font-weight: 550;
  cursor: pointer;
  padding: 2px 0;
  transition: color 140ms var(--ease-out), opacity 120ms var(--ease-out);
}
.link-btn:hover { color: var(--text); }
.link-btn:active { opacity: 0.6; }
.link-btn.small { font-size: 11.5px; }

.chip {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  min-width: 15px;
  height: 15px;
  padding: 0 4px;
  border-radius: 999px;
  background: var(--accent);
  color: var(--accent-contrast);
  font-size: 10px;
  font-weight: 700;
  line-height: 1;
}

.caret {
  width: 9px;
  height: 9px;
  color: var(--text-tertiary);
  transition: transform 160ms var(--ease-out);
}
.filters-anchor[data-open="true"] .caret { transform: rotate(180deg); }

/* ---------------------------------------------------------------------
   Filters popover — anchored to trigger, apple-style scale-in
--------------------------------------------------------------------- */

.filters-anchor {
  position: relative;
  flex: 0 0 auto;
}

.popover {
  position: absolute;
  top: calc(100% + 6px);
  /* Anchored to the button's RIGHT edge, extending left — the Filters
     button sits near the right side of the toolbar (search box, then
     Filters, then Export), so opening from left:0 pushed it past the
     right edge of the widget and got clipped. */
  right: 0;
  width: 280px;
  max-width: min(320px, 90vw);
  background: var(--surface);
  border: 1px solid var(--border-soft);
  border-radius: var(--radius);
  box-shadow: var(--shadow-popover);
  z-index: 30;
  transform-origin: top right;
  transform: scale(0.95);
  opacity: 0;
  transition: transform 160ms var(--ease-out), opacity 160ms var(--ease-out);
  pointer-events: none;
}

.popover.visible {
  transform: scale(1);
  opacity: 1;
  pointer-events: auto;
}

.popover-inner {
  padding: 12px;
  max-height: 360px;
  overflow-y: auto;
  display: flex;
  flex-direction: column;
  gap: 14px;
}

.popover-footer {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 8px 12px;
  border-top: 1px solid var(--border-soft);
}

.range-group label {
  display: block;
  font-size: 10.5px;
  font-weight: 650;
  text-transform: uppercase;
  letter-spacing: 0.03em;
  color: var(--text-secondary);
  margin-bottom: 6px;
}

.range-inputs {
  display: flex;
  align-items: center;
  gap: 6px;
}

.range-inputs input { width: 0; flex: 1 1 0; }
.range-sep { color: var(--text-tertiary); font-size: 12px; }

.filter-groups {
  display: flex;
  flex-direction: column;
  gap: 2px;
}

.filter-group summary {
  list-style: none;
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 6px 2px;
  font-size: 10.5px;
  font-weight: 650;
  text-transform: uppercase;
  letter-spacing: 0.03em;
  color: var(--text-secondary);
}
.filter-group summary::-webkit-details-marker { display: none; }
.filter-group summary::after {
  content: "";
  width: 6px;
  height: 6px;
  border-right: 1.5px solid var(--text-tertiary);
  border-bottom: 1.5px solid var(--text-tertiary);
  transform: rotate(-45deg);
  transition: transform 150ms var(--ease-out);
  margin-right: 2px;
}
.filter-group[open] summary::after { transform: rotate(45deg); }

.filter-group-count {
  font-weight: 400;
  text-transform: none;
  letter-spacing: 0;
  color: var(--text);
  opacity: 0;
  transition: opacity 140ms var(--ease-out);
}
.filter-group-count.visible { opacity: 1; }

.filter-options {
  display: flex;
  flex-direction: column;
  gap: 1px;
  padding-bottom: 6px;
}

.filter-option {
  display: flex;
  align-items: center;
  gap: 7px;
  padding: 4px 2px;
  border-radius: 5px;
  cursor: pointer;
  font-size: 12.5px;
}
.filter-option:hover { background: var(--fill); }
.filter-option input { margin: 0; accent-color: var(--accent); }
.filter-option .fo-label { flex: 1 1 auto; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.filter-option .fo-count { color: var(--text-tertiary); font-size: 11px; font-variant-numeric: tabular-nums; }

/* ---------------------------------------------------------------------
   Subline (row count + SQL toggle)
--------------------------------------------------------------------- */

.subline {
  display: flex;
  align-items: center;
  justify-content: space-between;
  flex: 0 0 auto;
  padding: 0 1px;
}

#rowCountBadge { font-variant-numeric: tabular-nums; }

/* ---------------------------------------------------------------------
   Panels (Browse / SQL) — one visible at a time, fills remaining height
--------------------------------------------------------------------- */

.panel {
  display: none;
  flex-direction: column;
  gap: 6px;
}
.panel.active { display: flex; }

.results-meta { flex: 0 0 auto; }
#resultsSummary, #sqlSummary { font-variant-numeric: tabular-nums; }

.table-wrap {
  /* No fixed height — grows to fit exactly the current page's rows so no
     vertical scrollbar ever appears. Still scrolls horizontally, since
     the table itself (20 columns) is wider than the 800px widget. */
  overflow-x: auto;
  overflow-y: visible;
  position: relative;
  border: 1px solid var(--border-soft);
  border-radius: var(--radius-sm);
  background: var(--fill);
}

table {
  border-collapse: collapse;
  width: 100%;
  font-size: 12px;
}

thead { position: sticky; top: 0; z-index: 2; }

th {
  text-align: left;
  padding: 7px 10px;
  background: var(--surface);
  border-bottom: 1px solid var(--border-soft);
  font-weight: 650;
  color: var(--text-secondary);
  white-space: nowrap;
  cursor: pointer;
  user-select: none;
}
th:hover { color: var(--text); }

th .sort-arrow {
  display: inline-block;
  margin-left: 3px;
  opacity: 0;
  transition: opacity 120ms var(--ease-out), transform 120ms var(--ease-out);
  font-size: 9px;
}
th.sorted .sort-arrow { opacity: 1; }
th.sorted-desc .sort-arrow { transform: rotate(180deg); }

td {
  padding: 6px 10px;
  border-bottom: 1px solid var(--border-soft);
  white-space: nowrap;
  max-width: 220px;
  overflow: hidden;
  text-overflow: ellipsis;
}

tbody tr:hover { background: var(--row-hover); }
tbody tr:nth-child(even) { background: var(--row-stripe); }
tbody tr:nth-child(even):hover { background: var(--row-hover); }

td.num { text-align: right; font-variant-numeric: tabular-nums; }
td.empty-cell { color: var(--text-tertiary); }

.empty-state {
  padding: 40px 16px;
  text-align: center;
  color: var(--text-secondary);
  font-size: 12.5px;
}

/* ---------------------------------------------------------------------
   Pagination
--------------------------------------------------------------------- */

.pagination {
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 10px;
  flex: 0 0 auto;
  font-size: 12px;
}

.pagination .page-indicator {
  color: var(--text-secondary);
  font-variant-numeric: tabular-nums;
  min-width: 84px;
  text-align: center;
}

/* ---------------------------------------------------------------------
   SQL panel
--------------------------------------------------------------------- */

.sql-head {
  display: flex;
  align-items: center;
  justify-content: space-between;
  flex: 0 0 auto;
}

.hint { font-size: 11px; color: var(--text-tertiary); }
.hint.error { color: var(--danger); }

.schema-chips {
  display: flex;
  gap: 4px;
  overflow-x: auto;
  flex: 0 0 auto;
  padding-bottom: 2px;
}

.schema-chip {
  flex: 0 0 auto;
  font-family: var(--mono);
  font-size: 10.5px;
  color: var(--text-secondary);
  background: var(--fill);
  border: 1px solid var(--border-soft);
  border-radius: 999px;
  padding: 3px 9px;
  cursor: pointer;
  white-space: nowrap;
  transition: background 120ms var(--ease-out), transform 120ms var(--ease-out);
}
.schema-chip:hover { background: var(--fill-strong); color: var(--text); }
.schema-chip:active { transform: scale(0.96); }

#sqlEditor {
  width: 100%;
  min-height: 76px;
  max-height: 130px;
  resize: vertical;
  font: 12.5px/1.5 var(--mono);
  color: var(--text);
  background: var(--fill);
  border: 1px solid var(--border-soft);
  border-radius: var(--radius-sm);
  padding: 8px 10px;
  flex: 0 0 auto;
  transition: border-color 140ms var(--ease-out), box-shadow 140ms var(--ease-out);
}

.sql-actions {
  display: flex;
  align-items: center;
  justify-content: space-between;
  flex: 0 0 auto;
}

.kbd-hint { font-size: 10.5px; color: var(--text-tertiary); }
.sql-actions-buttons { display: flex; gap: 6px; }

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

.toast {
  position: fixed;
  bottom: 14px;
  left: 50%;
  translate: -50% 0;
  background: var(--surface);
  border: 1px solid var(--border-soft);
  box-shadow: var(--shadow-popover);
  color: var(--text);
  font-size: 12.5px;
  padding: 8px 14px;
  border-radius: var(--radius);
  opacity: 0;
  transform: translateY(6px) scale(0.97);
  pointer-events: none;
  transition: opacity 160ms var(--ease-out), transform 160ms var(--ease-out);
  z-index: 100;
  max-width: 90%;
}
.toast.visible { opacity: 1; transform: translateY(0) scale(1); }
.toast.error { border-color: var(--danger); color: var(--danger); }

/* ---------------------------------------------------------------------
   Reduced motion / transparency / contrast
--------------------------------------------------------------------- */

@media (prefers-reduced-motion: reduce) {
  * { transition-duration: 60ms !important; animation-duration: 60ms !important; }
  .popover { transform: none !important; }
  .spinner { animation-duration: 1400ms; }
}

@media (prefers-contrast: more) {
  :root { --border-soft: var(--border); --fill: transparent; }
  input[type="text"], input[type="number"], .btn, #sqlEditor, th, .table-wrap { border-width: 1.5px; }
}

/* ---------------------------------------------------------------------
   Narrow widths (iframe embedded in a small slot)
--------------------------------------------------------------------- */

@media (max-width: 480px) {
  .toolbar { flex-wrap: wrap; }
  .search-box { flex: 1 1 100%; order: 1; }
  .filters-anchor { order: 2; }
  #exportBrowseBtn { order: 3; margin-left: auto; }
  /* At this width the toolbar wraps and Filters sits near the LEFT edge
     of its row (Export gets pushed right via margin-left:auto) — the
     opposite of the desktop layout, so the popover must open rightward
     from the button's left edge instead of leftward from its right. */
  .popover { left: 0; right: auto; transform-origin: top left; }
}
