/* ===========================================
   Knowledge (UI redesign Slice 5): knowledge bases, entries, search,
   imports and exports.

   Tokens only, mobile-first min-width queries, hairline rows instead of
   cards, neutral selection (the selection pattern: an inset 2px bar or
   outline in --color-text-emphasis), and no gradients, glows, hover
   transforms or entrance animation. Meters read their value from custom
   properties written by the style-vars controller, always with a 0%
   fallback. Shared blocks (.button, .badge, .form, .page-header,
   .empty-state) keep their own styles; this file styles only kb-, ke- and
   entry- blocks.
   =========================================== */

/* ===========================================
   Page scaffolding
   =========================================== */

.kb-back {
  margin-bottom: var(--spacing-xs);
}

.kb-form {
  max-width: var(--form-max-width);
}

.kb-form__section-title {
  margin: var(--spacing-2xl) 0 var(--spacing-md);
  padding-top: var(--spacing-lg);
  border-top: 1px solid var(--color-border-subtle);
}

/* The import form opens with its section title, right under the page header. */
.kb-import .kb-form__section-title {
  margin-top: 0;
  padding-top: 0;
  border-top: 0;
}

.kb-form__code {
  line-height: var(--leading-normal);
  tab-size: 2;
}

.kb-form__field-error {
  margin-top: var(--spacing-xs);
  color: var(--color-danger);
  font-size: var(--text-sm);
}

/* ===========================================
   Knowledge base list: one hairline row per base
   (.kb-grid stays on the list as a legacy hook)
   =========================================== */

.kb-list {
  margin: 0;
  padding: 0;
  border-top: 1px solid var(--color-border-subtle);
  list-style: none;
}

.kb-list__item {
  border-bottom: 1px solid var(--color-border-subtle);
}

.kb-card {
  display: grid;
  grid-template-columns: 2.5rem minmax(0, 1fr);
  gap: var(--spacing-md);
  align-items: start;
  padding: var(--spacing-lg) 0;
}

.kb-card__icon {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  width: 2.5rem;
  height: 2.5rem;
  border-radius: var(--radius-md);
  background: var(--color-surface-raised);
  color: var(--color-text-muted);
}

.kb-card__icon svg {
  width: 20px;
  height: 20px;
  fill: none;
  stroke: currentColor;
}

.kb-card__main {
  min-width: 0;
}

.kb-card__header {
  display: flex;
  flex-wrap: wrap;
  align-items: flex-start;
  justify-content: space-between;
  gap: var(--spacing-xs) var(--spacing-md);
}

.kb-card__title-group {
  min-width: 0;
}

.kb-card__name {
  margin: 0;
  overflow-wrap: anywhere;
  font-size: var(--text-base);
  font-weight: var(--font-semibold);
  line-height: var(--leading-snug);
}

/* :any-link outranks the shared "li a:not([class])" underline: titles read as
   text and underline on hover. */
.kb-card__name a:any-link {
  color: var(--color-text-emphasis);
  text-decoration: none;
}

.kb-card__name a:any-link:hover {
  text-decoration: underline;
  text-underline-offset: 2px;
}

.kb-card__owner {
  margin: 2px 0 0;
  overflow-wrap: anywhere;
  color: var(--color-text-muted);
  font-size: var(--text-sm);
}

.kb-card__body {
  margin-top: var(--spacing-sm);
}

.kb-card__description {
  max-width: var(--prose-max-width);
  margin: 0 0 var(--spacing-sm);
  color: var(--color-text-muted);
  font-size: var(--text-sm);
  line-height: var(--leading-normal);
}

.kb-card__meta {
  display: flex;
  flex-wrap: wrap;
  gap: var(--spacing-xs) 0;
  margin: 0;
  padding: 0;
  color: var(--color-text-muted);
  font-size: var(--text-sm);
  font-variant-numeric: tabular-nums;
  list-style: none;
}

.kb-card__meta li + li::before {
  content: "\00b7";
  margin: 0 var(--spacing-sm);
  color: var(--color-text-subtle);
}

.kb-progress {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  gap: var(--spacing-xs) var(--spacing-sm);
  margin-top: var(--spacing-sm);
}

/* The shared inline meter's fill is a span here, so give it a box. */
.kb-page .progress-inline__fill {
  display: block;
}

.kb-progress__detail {
  color: var(--color-text-muted);
  font-size: var(--text-xs);
  font-variant-numeric: tabular-nums;
}

.kb-card__footer {
  display: flex;
  grid-column: 1 / -1;
  flex-wrap: wrap;
  gap: var(--spacing-xs);
}

/* ===========================================
   Search page
   =========================================== */

.kb-search__title {
  overflow-wrap: anywhere;
}

/* Quick stats: label over value, on one hairline-bounded line */
.kb-search__stats {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  gap: var(--spacing-md) var(--spacing-xl);
  margin: 0 0 var(--spacing-xl);
  padding: var(--spacing-md) 0;
  border-top: 1px solid var(--color-border-subtle);
  border-bottom: 1px solid var(--color-border-subtle);
}

.kb-search__stat {
  display: flex;
  flex-direction: column;
  gap: 2px;
}

.kb-search__stat dd {
  margin: 0;
}

.kb-search__stat-label {
  color: var(--color-text-muted);
  font-size: var(--text-xs);
  font-weight: var(--font-medium);
}

.kb-search__stat-value {
  color: var(--color-text-emphasis);
  font-size: var(--text-lg);
  font-weight: var(--font-semibold);
  font-variant-numeric: tabular-nums;
}

.kb-search__form-container {
  margin-bottom: var(--spacing-xl);
}

.kb-search__input-row {
  display: flex;
  gap: var(--spacing-sm);
  margin-bottom: var(--spacing-lg);
}

.kb-search__input-wrapper {
  position: relative;
  display: flex;
  flex: 1;
  align-items: center;
  min-width: 0;
}

.kb-search__input-icon {
  position: absolute;
  left: var(--spacing-md);
  width: 18px;
  height: 18px;
  fill: none;
  stroke: var(--color-text-muted);
  pointer-events: none;
}

.kb-search__input-wrapper .kb-search__input {
  width: 100%;
  padding-left: calc(var(--spacing-md) + 26px);
}

.kb-search__submit-btn {
  flex-shrink: 0;
}

/* Mode and result-count controls: segmented radio groups */
.kb-search__controls {
  display: flex;
  flex-wrap: wrap;
  gap: var(--spacing-lg) var(--spacing-2xl);
  margin-bottom: var(--spacing-lg);
}

.kb-search__mode-toggle,
.kb-search__limit-control {
  min-width: 0;
  margin: 0;
  padding: 0;
  border: 0;
}

.kb-search__control-label {
  display: block;
  margin: 0 0 var(--spacing-sm);
  padding: 0;
  color: var(--color-text-muted);
  font-size: var(--text-xs);
  font-weight: var(--font-medium);
}

.kb-search__toggle-group,
.kb-search__limit-options {
  display: inline-flex;
  flex-wrap: wrap;
  gap: 2px;
  padding: 2px;
  border: 1px solid var(--color-border-control);
  border-radius: var(--radius-md);
  background: var(--color-surface);
}

.kb-search__toggle-option,
.kb-search__limit-option {
  position: relative;
  cursor: pointer;
}

/* Visually hidden with the clip pattern, never display: none */
.kb-search__toggle-option input,
.kb-search__limit-option input,
.kb-search__filter-chip input {
  position: absolute;
  width: 1px;
  height: 1px;
  overflow: hidden;
  clip: rect(0, 0, 0, 0);
  white-space: nowrap;
}

.kb-search__toggle-btn,
.kb-search__limit-btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: var(--spacing-xs);
  min-width: calc(var(--control-height) - 4px);
  min-height: calc(var(--control-height) - 4px);
  padding: 0 var(--spacing-md);
  border-radius: var(--radius-sm);
  color: var(--color-text-muted);
  font-size: var(--text-sm);
  font-weight: var(--font-medium);
  font-variant-numeric: tabular-nums;
  transition:
    background-color var(--duration-fast) var(--ease-out),
    color var(--duration-fast) var(--ease-out);
}

.kb-search__toggle-btn svg {
  width: 16px;
  height: 16px;
  fill: none;
  stroke: currentColor;
}

.kb-search__toggle-option:hover .kb-search__toggle-btn,
.kb-search__limit-option:hover .kb-search__limit-btn {
  background: var(--color-surface-hover);
  color: var(--color-text);
}

.kb-search__toggle-option input:checked + .kb-search__toggle-btn,
.kb-search__limit-option input:checked + .kb-search__limit-btn {
  background: var(--color-surface-active);
  box-shadow: inset 0 0 0 2px var(--color-text-emphasis);
  color: var(--color-text-emphasis);
  font-weight: var(--font-semibold);
}

/* The radios and checkboxes are hidden; their visible face carries the ring. */
.kb-search__toggle-option input:focus-visible + .kb-search__toggle-btn,
.kb-search__limit-option input:focus-visible + .kb-search__limit-btn,
.kb-search__filter-chip input:focus-visible + .kb-search__chip {
  outline: 2px solid var(--color-primary);
  outline-offset: 2px;
}

.kb-search__toggle-option input:disabled + .kb-search__toggle-btn,
.kb-search__limit-option input:disabled + .kb-search__limit-btn {
  color: var(--color-text-subtle);
  cursor: not-allowed;
}

/* Metadata filters */
.kb-search__filters {
  padding-top: var(--spacing-lg);
  border-top: 1px solid var(--color-border-subtle);
}

.kb-search__filters-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: var(--spacing-md);
}

.kb-search__filters-header .kb-search__control-label {
  margin: 0;
}

.kb-search__clear-filters,
.kb-search__filter-clear {
  display: inline-flex;
  align-items: center;
  min-height: var(--touch-target);
  color: var(--color-text-muted);
  font-size: var(--text-sm);
  text-decoration: underline;
  text-decoration-thickness: 1px;
  text-underline-offset: 2px;
}

.kb-search__clear-filters:hover,
.kb-search__filter-clear:hover {
  color: var(--color-text);
}

.kb-search__filter-chips {
  display: grid;
  gap: var(--spacing-md);
}

.kb-search__filter-group {
  min-width: 0;
}

.kb-search__filter-group-header {
  display: flex;
  align-items: center;
  gap: var(--spacing-md);
  min-height: var(--touch-target);
}

.kb-search__filter-label {
  color: var(--color-text);
  font-size: var(--text-sm);
  font-weight: var(--font-medium);
}

.kb-search__filter-options {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  gap: var(--spacing-xs);
}

.kb-search__filter-chip {
  position: relative;
  display: inline-flex;
  cursor: pointer;
}

.kb-search__chip {
  display: inline-flex;
  align-items: center;
  min-height: var(--control-height-sm);
  padding: 0 var(--spacing-md);
  border: 1px solid var(--color-border-control);
  border-radius: var(--radius-full);
  background: var(--color-surface);
  color: var(--color-text);
  font-size: var(--text-sm);
  transition:
    background-color var(--duration-fast) var(--ease-out),
    border-color var(--duration-fast) var(--ease-out);
}

.kb-search__filter-chip:hover .kb-search__chip {
  background: var(--color-surface-hover);
}

.kb-search__filter-chip input:checked + .kb-search__chip {
  border-color: var(--color-text-emphasis);
  background: var(--color-surface-active);
  box-shadow: inset 0 0 0 1px var(--color-text-emphasis);
  color: var(--color-text-emphasis);
  font-weight: var(--font-semibold);
}

.kb-search__filter-more {
  color: var(--color-text-muted);
  font-size: var(--text-xs);
}

.kb-search__warning {
  display: flex;
  align-items: flex-start;
  gap: var(--spacing-sm);
  padding: var(--spacing-md);
  border: 1px solid var(--color-warning-border);
  border-radius: var(--radius-md);
  background: var(--color-warning-surface);
  color: var(--color-text);
  font-size: var(--text-sm);
}

.kb-search__warning svg {
  flex-shrink: 0;
  width: 18px;
  height: 18px;
  margin-top: 1px;
  fill: none;
  stroke: var(--color-warning);
}

/* ===========================================
   Search results
   =========================================== */

.kb-search__results {
  margin-top: var(--spacing-md);
}

.kb-search__results-header {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  justify-content: space-between;
  gap: var(--spacing-sm);
  margin-bottom: var(--spacing-md);
}

.kb-search__results-title {
  margin: 0;
  overflow-wrap: anywhere;
  color: var(--color-text-emphasis);
  font-size: var(--text-lg);
  font-weight: var(--font-semibold);
}

.kb-search__empty,
.kb-search__error {
  display: grid;
  justify-items: center;
  gap: var(--spacing-xs);
  padding: var(--spacing-2xl) var(--spacing-md);
  text-align: center;
}

.kb-search__empty svg,
.kb-search__error svg {
  width: 24px;
  height: 24px;
  margin-bottom: var(--spacing-xs);
  fill: none;
  stroke: var(--color-text-muted);
}

.kb-search__empty p,
.kb-search__error p {
  max-width: var(--prose-max-width);
  margin: 0;
  color: var(--color-text-muted);
  font-size: var(--text-sm);
}

.kb-search__error {
  justify-items: start;
  padding: var(--spacing-md);
  border: 1px solid var(--color-danger-border);
  border-radius: var(--radius-md);
  background: var(--color-danger-surface);
  text-align: left;
}

.kb-search__error svg {
  stroke: var(--color-danger);
}

.kb-search__error .kb-search__results-title {
  color: var(--color-danger);
  font-size: var(--text-base);
}

.kb-search__error p {
  color: var(--color-text);
}

/* Rank numbers are neutral: the solid accent means "needs you" only. */
.kb-quote__rank-badge,
.entry-item__rank {
  display: inline-flex;
  flex-shrink: 0;
  align-items: center;
  justify-content: center;
  min-width: 1.75rem;
  height: 1.75rem;
  padding: 0 var(--spacing-xs);
  border: 1px solid var(--color-border);
  border-radius: var(--radius-sm);
  color: var(--color-text-muted);
  font-family: var(--font-mono);
  font-size: var(--text-xs);
  font-variant-numeric: tabular-nums;
}

/* Match meters (role="meter"); the fill width comes from --match-percent */
.kb-quote__match,
.entry-item__match {
  display: inline-flex;
  align-items: center;
  gap: var(--spacing-sm);
}

.kb-quote__match-bar,
.entry-item__match-bar {
  display: block;
  width: 4rem;
  height: 6px;
  overflow: hidden;
  border-radius: var(--radius-full);
  background: var(--color-surface-elevated);
}

.kb-quote__match-bar::after,
.entry-item__match-bar::after {
  content: "";
  display: block;
  width: var(--match-percent, 0%);
  height: 100%;
  background: var(--color-text-muted);
}

.kb-quote__match--high .kb-quote__match-bar::after,
.entry-item__match--high .entry-item__match-bar::after {
  background: var(--color-success);
}

.kb-quote__match--medium .kb-quote__match-bar::after,
.entry-item__match--medium .entry-item__match-bar::after {
  background: var(--color-warning);
}

.kb-quote__match-text,
.entry-item__match-text {
  color: var(--color-text-muted);
  font-size: var(--text-xs);
  font-variant-numeric: tabular-nums;
}

/* Find Quotes mode */
.kb-quotes {
  margin: 0;
  padding: 0;
  border-top: 1px solid var(--color-border-subtle);
  list-style: none;
}

.kb-quote {
  padding: var(--spacing-md) 0;
  border-bottom: 1px solid var(--color-border-subtle);
}

.kb-quote__header {
  display: flex;
  align-items: center;
  gap: var(--spacing-md);
  margin-bottom: var(--spacing-sm);
}

.kb-quote__content {
  max-width: var(--prose-max-width);
  overflow-wrap: anywhere;
  color: var(--color-text);
  font-size: var(--text-sm);
  line-height: var(--leading-relaxed);
  white-space: pre-wrap;
}

/* ===========================================
   Entry rows: the entries index and Search Resources results
   =========================================== */

.entry-list {
  margin: 0;
  padding: 0;
  border-top: 1px solid var(--color-border-subtle);
  list-style: none;
}

.entry-list__item {
  border-bottom: 1px solid var(--color-border-subtle);
}

.entry-item {
  padding: var(--spacing-md) 0;
}

.entry-item__main {
  display: flex;
  align-items: flex-start;
  gap: var(--spacing-md);
}

.entry-item__content {
  flex: 1;
  min-width: 0;
}

.entry-item__header {
  display: flex;
  flex-wrap: wrap;
  align-items: baseline;
  gap: var(--spacing-xs) var(--spacing-sm);
}

.entry-item__title {
  min-width: 0;
  margin: 0;
  overflow-wrap: anywhere;
  font-size: var(--text-base);
  font-weight: var(--font-semibold);
  line-height: var(--leading-snug);
}

.entry-item__title a:any-link {
  color: var(--color-text-emphasis);
  text-decoration: none;
}

.entry-item__title a:any-link:hover {
  text-decoration: underline;
  text-underline-offset: 2px;
}

.entry-item__lang {
  flex-shrink: 0;
  color: var(--color-text-muted);
  font-size: var(--text-xs);
}

.entry-item__meta {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  gap: var(--spacing-xs) var(--spacing-md);
  margin-top: var(--spacing-xs);
  color: var(--color-text-muted);
  font-size: var(--text-sm);
}

.entry-item__updated {
  color: var(--color-text-muted);
}

.entry-item__chunk-badge,
.entry-item__tokens {
  padding: 0 var(--spacing-xs);
  border: 1px solid var(--color-neutral-border);
  border-radius: var(--radius-sm);
  background: var(--color-neutral-surface);
  color: var(--color-text-muted);
  font-size: var(--text-xs);
  font-variant-numeric: tabular-nums;
}

.entry-item__actions {
  display: flex;
  flex-shrink: 0;
  gap: var(--spacing-xs);
}

.entry-item__preview {
  margin-top: var(--spacing-sm);
}

.entry-item__text {
  max-width: var(--prose-max-width);
  margin: 0 0 var(--spacing-sm);
  overflow-wrap: anywhere;
  color: var(--color-text-muted);
  font-size: var(--text-sm);
  line-height: var(--leading-relaxed);
}

.entry-item__summary {
  max-width: var(--prose-max-width);
  margin-bottom: var(--spacing-sm);
  padding-left: var(--spacing-md);
  border-left: 2px solid var(--color-border);
  color: var(--color-text);
  font-size: var(--text-sm);
  line-height: var(--leading-normal);
}

.entry-item__summary-label {
  display: block;
  margin-bottom: 2px;
  color: var(--color-text-muted);
  font-size: var(--text-xs);
  font-weight: var(--font-medium);
}

.entry-item__tags {
  display: flex;
  flex-wrap: wrap;
  gap: var(--spacing-xs);
}

/* ===========================================
   Entry page: prose first, details beside it from 1024px
   =========================================== */

.ke-document__grid {
  display: grid;
  grid-template-columns: minmax(0, 1fr);
  gap: var(--spacing-2xl);
  align-items: start;
}

.ke-document__main {
  min-width: 0;
}

.ke-document__title {
  overflow-wrap: anywhere;
}

.ke-document__content-card {
  margin-bottom: var(--spacing-2xl);
}

.ke-document__content {
  color: var(--color-text);
  font-size: var(--text-base);
}

.ke-document__sidebar {
  display: flex;
  flex-direction: column;
  gap: var(--spacing-xl);
  min-width: 0;
}

/* Sidebar sections: a heading over a hairline, no card chrome */
.ke-status-card,
.ke-metadata-card {
  padding-top: var(--spacing-md);
  border-top: 1px solid var(--color-border-subtle);
}

.ke-status-card__header,
.ke-metadata-card__header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: var(--spacing-sm);
  margin-bottom: var(--spacing-md);
}

.ke-status-card__title,
.ke-metadata-card__title {
  margin: 0;
  color: var(--color-text-emphasis);
  font-size: var(--text-sm);
  font-weight: var(--font-semibold);
}

.ke-status-card__stats-row {
  display: grid;
  grid-template-columns: repeat(2, minmax(0, 1fr));
  gap: var(--spacing-md);
  margin: 0;
}

.ke-status-card__stat-item {
  display: flex;
  flex-direction: column;
  gap: 2px;
}

.ke-status-card__stat-item dd {
  margin: 0;
}

.ke-status-card__stat-label,
.ke-status-card__tokens-label {
  color: var(--color-text-muted);
  font-size: var(--text-xs);
}

.ke-status-card__stat-value,
.ke-status-card__tokens-value {
  color: var(--color-text);
  font-size: var(--text-sm);
  font-weight: var(--font-semibold);
  font-variant-numeric: tabular-nums;
}

/* Metadata tree (rendered by KnowledgeEntriesHelper#render_metadata_tree) */
.ke-metadata__section {
  margin-bottom: var(--spacing-md);
}

.ke-metadata__section:last-child {
  margin-bottom: 0;
}

.ke-metadata__section-title {
  margin: 0 0 var(--spacing-sm);
  padding-bottom: var(--spacing-xs);
  border-bottom: 1px solid var(--color-border-subtle);
  color: var(--color-text-muted);
  font-size: var(--text-xs);
  font-weight: var(--font-semibold);
}

.ke-metadata__section-content {
  padding-left: var(--spacing-md);
}

.ke-metadata__field {
  display: flex;
  flex-direction: column;
  gap: 2px;
  margin-bottom: var(--spacing-sm);
}

.ke-metadata__field:last-child {
  margin-bottom: 0;
}

.ke-metadata__label {
  color: var(--color-text-muted);
  font-size: var(--text-xs);
  font-weight: var(--font-medium);
}

.ke-metadata__value {
  overflow-wrap: anywhere;
  color: var(--color-text);
  font-size: var(--text-sm);
}

.ke-metadata__tags {
  display: flex;
  flex-wrap: wrap;
  gap: var(--spacing-xs);
}

.ke-metadata__tag {
  padding: 0 var(--spacing-xs);
  border: 1px solid var(--color-neutral-border);
  border-radius: var(--radius-sm);
  background: var(--color-neutral-surface);
  color: var(--color-text-muted);
  font-size: var(--text-xs);
}

/* ===========================================
   Chunks: a compact list; each row opens the chunk sheet
   =========================================== */

.ke-chunks {
  margin-top: var(--spacing-2xl);
}

.ke-chunks__header {
  display: flex;
  flex-wrap: wrap;
  align-items: baseline;
  justify-content: space-between;
  gap: var(--spacing-xs) var(--spacing-md);
  margin-bottom: var(--spacing-sm);
}

.ke-chunks__title {
  margin: 0;
  color: var(--color-text-emphasis);
  font-size: var(--text-lg);
  font-weight: var(--font-semibold);
}

.ke-chunks__count {
  color: var(--color-text-muted);
  font-size: var(--text-sm);
  font-variant-numeric: tabular-nums;
}

.ke-chunks__legend {
  display: flex;
  flex-wrap: wrap;
  gap: var(--spacing-xs) var(--spacing-lg);
  margin: 0 0 var(--spacing-md);
  padding: 0;
  color: var(--color-text-muted);
  font-size: var(--text-xs);
  list-style: none;
}

.ke-chunks__legend li {
  display: inline-flex;
  align-items: center;
  gap: var(--spacing-xs);
}

.ke-chunks__list {
  max-height: 24rem;
  overflow-y: auto;
  border: 1px solid var(--color-border);
  border-radius: var(--radius-md);
  background: var(--color-surface);
}

.ke-chunks__list > [role="listitem"] + [role="listitem"] {
  border-top: 1px solid var(--color-border-subtle);
}

.ke-chunks__item {
  display: flex;
  align-items: center;
  gap: var(--spacing-md);
  width: 100%;
  min-height: var(--touch-target);
  padding: var(--spacing-sm) var(--spacing-md);
  border: 0;
  background: transparent;
  color: var(--color-text);
  font: inherit;
  text-align: left;
  cursor: pointer;
  transition: background-color var(--duration-fast) var(--ease-out);
}

.ke-chunks__item:hover {
  background: var(--color-surface-hover);
}

/* The list clips its rows, so the global ring is drawn inset. */
.ke-chunks__item:focus-visible {
  outline-offset: -2px;
}

.ke-chunks__number {
  flex-shrink: 0;
  min-width: 2rem;
  color: var(--color-text-muted);
  font-family: var(--font-mono);
  font-size: var(--text-xs);
  font-variant-numeric: tabular-nums;
}

.ke-chunks__preview {
  flex: 1;
  min-width: 0;
  overflow: hidden;
  color: var(--color-text-muted);
  font-size: var(--text-sm);
  text-overflow: ellipsis;
  white-space: nowrap;
}

.ke-chunks__meta {
  display: flex;
  flex-shrink: 0;
  align-items: center;
  gap: var(--spacing-sm);
}

/* Markers differ by shape as well as colour: circle, square, diamond. */
.ke-chunks__indicator {
  display: inline-block;
  flex-shrink: 0;
  width: 8px;
  height: 8px;
  background: var(--color-text-muted);
}

.ke-chunks__indicator--embedded {
  border-radius: var(--radius-full);
  background: var(--color-success);
}

.ke-chunks__indicator--summarized {
  border-radius: 1px;
  background: var(--color-info);
}

.ke-chunks__indicator--contextual {
  width: 7px;
  height: 7px;
  background: var(--color-text-muted);
  transform: rotate(45deg);
}

.ke-chunks__tokens {
  display: none;
  color: var(--color-text-muted);
  font-size: var(--text-xs);
  font-variant-numeric: tabular-nums;
}

/* Chunk sheet (design §D.8): a bottom sheet below 640px, a dialog above */
.ke-chunk-modal {
  position: fixed;
  inset: 0;
  z-index: var(--z-modal);
  display: flex;
  align-items: flex-end;
  justify-content: center;
  background: var(--color-overlay);
  opacity: 0;
  visibility: hidden;
  transition:
    opacity var(--duration-fast) var(--ease-out),
    visibility var(--duration-fast) var(--ease-out);
}

.ke-chunk-modal.active {
  opacity: 1;
  visibility: visible;
}

.ke-chunk-modal__dialog {
  display: flex;
  flex-direction: column;
  width: 100%;
  max-height: 90dvh;
  padding-bottom: env(safe-area-inset-bottom);
  border: 1px solid var(--color-border);
  border-bottom: 0;
  border-radius: var(--radius-lg) var(--radius-lg) 0 0;
  background: var(--color-surface);
}

.ke-chunk-modal__header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: var(--spacing-sm);
  padding: var(--spacing-xs) var(--spacing-xs) var(--spacing-xs) var(--spacing-lg);
  border-bottom: 1px solid var(--color-border-subtle);
}

.ke-chunk-modal__title {
  margin: 0;
  color: var(--color-text-emphasis);
  font-size: var(--text-lg);
  font-weight: var(--font-semibold);
}

.ke-chunk-modal__body {
  flex: 1;
  padding: var(--spacing-lg);
  overflow-y: auto;
}

.ke-chunk-modal__content {
  overflow-wrap: anywhere;
  color: var(--color-text);
  font-size: var(--text-sm);
  line-height: var(--leading-relaxed);
  white-space: pre-wrap;
}

.ke-chunk-modal__section {
  margin-top: var(--spacing-lg);
  padding-top: var(--spacing-lg);
  border-top: 1px solid var(--color-border-subtle);
}

.ke-chunk-modal__section.hidden {
  display: none;
}

.ke-chunk-modal__section-title {
  margin: 0 0 var(--spacing-xs);
  color: var(--color-text-muted);
  font-size: var(--text-sm);
  font-weight: var(--font-semibold);
}

.ke-chunk-modal__section p {
  margin: 0;
  color: var(--color-text);
  font-size: var(--text-sm);
  line-height: var(--leading-relaxed);
}

.ke-chunk-modal__footer {
  padding: var(--spacing-sm) var(--spacing-lg);
  border-top: 1px solid var(--color-border-subtle);
}

.ke-chunk-modal__footer:has(.ke-chunk-modal__tokens:empty) {
  display: none;
}

.ke-chunk-modal__tokens {
  color: var(--color-text-muted);
  font-size: var(--text-sm);
  font-variant-numeric: tabular-nums;
}

/* ===========================================
   Export page
   =========================================== */

.kb-export__status {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  gap: var(--spacing-sm) var(--spacing-lg);
  margin-bottom: var(--spacing-xl);
}

.kb-export__section {
  margin-top: var(--spacing-2xl);
}

.kb-export__section-title {
  margin: 0 0 var(--spacing-md);
  color: var(--color-text-emphasis);
  font-size: var(--text-lg);
  font-weight: var(--font-semibold);
}

.kb-details {
  margin: 0;
  border-top: 1px solid var(--color-border-subtle);
}

.kb-details__row {
  display: grid;
  gap: 2px;
  padding: var(--spacing-sm) 0;
  border-bottom: 1px solid var(--color-border-subtle);
}

.kb-details__row dt,
.kb-details__row dd {
  margin: 0;
}

.kb-details__row dd {
  min-width: 0;
  overflow-wrap: anywhere;
}

.kb-details__value--danger {
  color: var(--color-danger);
}

.kb-export__url {
  display: flex;
  flex-direction: column;
  gap: var(--spacing-sm);
}

.kb-export__url-text {
  display: block;
  padding: var(--spacing-sm) var(--spacing-md);
  border: 1px solid var(--color-border);
  border-radius: var(--radius-md);
  background: var(--color-background-subtle);
  color: var(--color-text);
  word-break: break-all;
}

.kb-export__copy-status {
  color: var(--color-text-muted);
}

.kb-export__hint {
  margin-bottom: 0;
  color: var(--color-text-muted);
  font-size: var(--text-sm);
}

.kb-export__log {
  max-height: 24rem;
  margin: 0;
  overflow: auto;
}

/* ===========================================
   Wider screens
   =========================================== */

@media (min-width: 640px) {
  .kb-search__filter-chips {
    grid-template-columns: repeat(auto-fill, minmax(16rem, 1fr));
    gap: var(--spacing-md) var(--spacing-xl);
  }

  .kb-search__stat--status {
    margin-left: auto;
  }

  .entry-item:has(.entry-item__rank) .entry-item__preview {
    padding-left: calc(1.75rem + var(--spacing-md));
  }

  .ke-chunks__tokens {
    display: inline;
  }

  .ke-chunk-modal {
    align-items: center;
    padding: var(--spacing-lg);
  }

  .ke-chunk-modal__dialog {
    width: min(40rem, 100% - 2rem);
    max-height: 80vh;
    padding-bottom: 0;
    border-bottom: 1px solid var(--color-border);
    border-radius: var(--radius-lg);
    box-shadow: var(--shadow-lg);
  }

  .kb-details__row {
    grid-template-columns: 12rem minmax(0, 1fr);
    gap: var(--spacing-md);
  }

  .kb-export__url {
    flex-direction: row;
    align-items: flex-start;
  }

  .kb-export__url-text {
    flex: 1;
    min-width: 0;
  }
}

@media (min-width: 768px) {
  .kb-card__footer {
    grid-column: 2;
  }
}

@media (min-width: 1024px) {
  .ke-document__grid {
    grid-template-columns: minmax(0, 1fr) 18rem;
  }
}

/* From 1200px the actions sit beside the row on one line. */
@media (min-width: 1200px) {
  .kb-card {
    grid-template-columns: 2.5rem minmax(0, 1fr) auto;
  }

  .kb-card__footer {
    grid-column: 3;
    flex-wrap: nowrap;
    justify-content: flex-end;
  }

  .ke-document__grid {
    grid-template-columns: minmax(0, 1fr) 20rem;
  }
}

@media (prefers-reduced-motion: reduce) {
  .kb-search__toggle-btn,
  .kb-search__limit-btn,
  .kb-search__chip,
  .ke-chunks__item,
  .ke-chunk-modal {
    transition: none;
  }
}

/* Forced colours drop box-shadow, so the selection outline becomes real. */
@media (forced-colors: active) {
  .kb-search__toggle-option input:checked + .kb-search__toggle-btn,
  .kb-search__limit-option input:checked + .kb-search__limit-btn,
  .kb-search__filter-chip input:checked + .kb-search__chip {
    outline: 2px solid Highlight;
    outline-offset: -2px;
  }

  .ke-chunks__indicator,
  .kb-quote__match-bar,
  .entry-item__match-bar {
    border: 1px solid CanvasText;
  }
}
