:root {
  --fg: #222;
  --muted: #6b7280;
  --bg: #fff;
  --panel: #f8fafc;
  --border: #d1d5db;
  --red: #dc2626;
  --green: #16a34a;
  --blue: #2563eb;
  --primary: #1f2937;
}

* { box-sizing: border-box; }

body {
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, sans-serif;
  color: var(--fg);
  margin: 0;
  padding: 1.5rem;
  background: var(--bg);
}

header { display: flex; align-items: baseline; justify-content: space-between; margin-bottom: 1rem; }
h1 { margin: 0; font-size: 1.4rem; }
h2 { font-size: 1rem; margin: 0; font-weight: 600; }
.tag { font-size: 0.7rem; background: #fef3c7; color: #92400e; padding: 2px 6px; border-radius: 4px; margin-left: 8px; vertical-align: middle; }
.stats { color: var(--muted); font-size: 0.85rem; }

.builder-section, .results-section, .nl-section {
  border: 1px solid var(--border);
  border-radius: 6px;
  padding: 1rem;
  background: var(--panel);
  margin-bottom: 1rem;
}

/* Natural language search */
.nl-header { display: flex; align-items: center; gap: 0.5rem; }
.nl-help { color: var(--muted); font-size: 0.85rem; margin: 0.4rem 0 0.6rem; }
.nl-input-row { display: flex; gap: 0.5rem; align-items: stretch; }
.nl-input-row textarea {
  flex: 1;
  padding: 0.5rem;
  border: 2px solid var(--blue);
  border-radius: 4px;
  font-family: inherit;
  font-size: 0.9rem;
  resize: vertical;
}
.nl-input-row .btn { white-space: nowrap; }
.nl-manual { margin-top: 0.75rem; border-top: 1px dashed var(--border); padding-top: 0.5rem; }
.nl-manual summary { cursor: pointer; font-size: 0.85rem; color: var(--muted); user-select: none; }
.nl-manual textarea {
  width: 100%;
  padding: 0.5rem;
  border: 1px solid var(--border);
  border-radius: 4px;
  font-family: ui-monospace, SFMono-Regular, Menlo, monospace;
  font-size: 0.8rem;
  resize: vertical;
}
.nl-manual-actions { display: flex; align-items: center; gap: 1rem; margin-top: 0.5rem; }
.builder-header, .results-header, .builder-actions, .pager {
  display: flex; justify-content: space-between; align-items: center; gap: 1rem;
}
.builder-actions { margin-top: 0.75rem; }
.status { color: var(--muted); font-size: 0.85rem; }
.toggle { display: inline-flex; align-items: center; gap: 0.4rem; font-size: 0.85rem; color: var(--muted); cursor: pointer; }
.toggle em { font-style: normal; color: #9ca3af; }

/* Builder tree */
.group {
  border: 1px solid var(--border);
  border-radius: 6px;
  padding: 0.5rem 0.5rem 0.5rem 2.5rem;
  margin: 0.5rem 0;
  background: #fff;
  position: relative;
}
.group .group-label {
  position: absolute;
  left: 0; top: 0; bottom: 0;
  width: 1.75rem;
  display: flex; align-items: center; justify-content: center;
  background: #e5e7eb;
  border-right: 1px solid var(--border);
  font-weight: 600;
  font-size: 0.75rem;
  writing-mode: vertical-rl;
  transform: rotate(180deg);
  cursor: pointer;
  user-select: none;
}
.group .group-label:hover { background: #d1d5db; }
.group .group-remove {
  position: absolute;
  left: 0; bottom: -0.5rem;
  background: var(--bg); border: 1px solid var(--border); width: 1.5rem; height: 1.5rem;
  border-radius: 3px; cursor: pointer; font-size: 0.85rem; line-height: 1;
}

.rule {
  display: flex; gap: 0.5rem; align-items: center; margin: 0.25rem 0;
}
.rule select, .rule input {
  padding: 0.4rem 0.5rem;
  border: 2px solid;
  border-radius: 4px;
  background: #fff;
  font-size: 0.85rem;
}
.rule .field  { border-color: var(--red); min-width: 160px; }
.rule .op     { border-color: var(--green); min-width: 130px; }
.rule .value  { border-color: var(--blue); min-width: 200px; flex: 1; }
.rule .remove { background: #fff; border: 1px solid var(--border); width: 1.75rem; height: 1.75rem; border-radius: 3px; cursor: pointer; }
.rule .nest   { background: #fff; border: 1px solid var(--border); width: 1.75rem; height: 1.75rem; border-radius: 3px; cursor: pointer; }

.add-row { margin: 0.5rem 0; display: flex; gap: 0.5rem; }

.btn {
  padding: 0.4rem 0.8rem;
  border: 1px solid var(--border);
  border-radius: 4px;
  background: #fff;
  cursor: pointer;
  font-size: 0.85rem;
}
.btn:hover { background: #f3f4f6; }
.btn.primary { background: var(--primary); color: #fff; border-color: var(--primary); }
.btn.primary:hover { background: #111827; }
.btn.ghost { color: var(--muted); }

/* Results */
.table-wrap { overflow-x: auto; margin: 0.5rem 0; }
table { width: 100%; border-collapse: collapse; font-size: 0.85rem; background: #fff; }
th, td { padding: 0.5rem 0.6rem; text-align: left; border-bottom: 1px solid #eee; vertical-align: top; }
th { background: #f3f4f6; font-weight: 600; cursor: pointer; user-select: none; }
td.checkmark { text-align: center; }
.checkmark.yes { color: var(--green); font-weight: 700; }
.checkmark.no { color: #ccc; }

.result-count { color: var(--muted); font-size: 0.85rem; }
.pager { justify-content: center; margin-top: 0.5rem; }
#page-info { color: var(--muted); font-size: 0.85rem; padding: 0 0.5rem; }

/* Selection */
.select-col { width: 1.5rem; }
td.select-cell { text-align: center; padding: 0.3rem 0.2rem; }
.patient-check, .collection-check { cursor: pointer; margin: 0; }
.selection-info { color: var(--blue); font-weight: 600; }
.ds-name { display: inline-flex; align-items: center; gap: 0.1rem; }

/* Expandable per-patient dataset detail */
.expand-col { width: 1.5rem; }
td.expand-cell { text-align: center; padding: 0.3rem 0.2rem; }
.expander {
  background: none; border: none; cursor: pointer;
  color: var(--muted); font-size: 0.7rem; line-height: 1; padding: 2px 4px;
}
.expander:hover, .expander.open { color: var(--blue); }

.detail-row > td { background: #eef2f7; padding: 0.75rem 1rem; }
.detail-panel { font-size: 0.85rem; }
.detail-head { color: var(--muted); margin-bottom: 0.5rem; font-weight: 600; }

.ds-card {
  background: #fff; border: 1px solid var(--border); border-radius: 5px;
  padding: 0.55rem 0.75rem; margin-bottom: 0.5rem;
}
.ds-card:last-child { margin-bottom: 0; }
.ds-card.current { border-color: var(--blue); box-shadow: inset 0 0 0 1px var(--blue); }
/* Search match coding (only applied when a filter is active) */
.ds-card.match { border-left: 4px solid var(--green); background: #f0fdf4; }
.ds-card.nomatch { border-left: 4px solid #e5e7eb; background: #fafafa; opacity: 0.72; }
.ds-card.match.current { border-color: var(--blue); }

.ds-title {
  display: flex; justify-content: space-between; align-items: baseline;
  gap: 1rem; flex-wrap: wrap; font-weight: 600; margin-bottom: 0.4rem;
}
.ds-current {
  font-size: 0.68rem; font-weight: 600; background: #dbeafe; color: #1e40af;
  padding: 1px 6px; border-radius: 4px; vertical-align: middle;
}
.ds-link { font-weight: 400; font-size: 0.8rem; }
.ds-link a { color: var(--blue); text-decoration: none; }
.ds-link a:hover { text-decoration: underline; }

.ds-fields {
  display: grid; grid-template-columns: max-content 1fr;
  gap: 0.15rem 0.9rem; margin: 0;
}
.ds-fields dt { color: var(--muted); }
.ds-fields dd { margin: 0; word-break: break-word; }

.detail-loading { color: var(--muted); font-style: italic; }
