/*
 * Estilos propios de la zona «knowledge». Los componentes comunes están en app.css:
 * aquí sólo lo que no reutiliza ninguna otra pantalla.
 *
 * Conocimiento (kb-*): la lista de bases a la izquierda y, a la derecha, la elegida con sus
 * documentos y la prueba de búsqueda en dos pestañas.
 * Contexto de empresa (company-*): el editor grande con su coste al lado.
 */

/* Algo más ancha que las demás pantallas: la tabla de documentos va al lado de la lista. */
.page--kb {
  max-width: calc(1280px + 2 * var(--page-pad-x));
}

.kb-totals {
  font-size: 14px;
  font-weight: 600;
  color: var(--text-secondary);
}

/* Let the totals and «Nueva base» share lines on a phone instead of running off the side. */
.page--kb .page-header__actions {
  flex-shrink: 1;
  min-width: 0;
}

/* ============================================================
   Conocimiento: lista de bases
   ============================================================ */

.kb-layout {
  display: grid;
  grid-template-columns: minmax(250px, 310px) minmax(0, 1fr);
  gap: 24px;
  align-items: start;
}

/* Acompaña al bajar por una tabla larga y se desplaza por dentro: es su scroll el que va
   pidiendo más bases (knowledge_list_controller.js). Tan alta como cabe en la pantalla
   debajo de la cabecera, para que se vea entera sin mover la página. */
.kb-list {
  position: sticky;
  top: 24px;
  max-height: max(320px, calc(100vh - 176px));
  display: flex;
  flex-direction: column;
  gap: 12px;
  padding: 14px;
}

.kb-list__search .search {
  min-width: 0;
}

.kb-list__items {
  /* Positioned: the rows' offsetTop, to bring the open base into view, counts from here. */
  position: relative;
  flex: 1;
  min-height: 0;
  overflow-y: auto;
  overscroll-behavior: contain;
  list-style: none;
  margin: 0 -6px;
  padding: 0 6px;
  display: flex;
  flex-direction: column;
  gap: 2px;
  transition: opacity var(--duration) var(--ease);
}

.kb-list__items[aria-busy="true"] {
  opacity: 0.55;
}

.kb-list__item {
  display: flex;
  flex-direction: column;
  gap: 2px;
  padding: 9px 12px;
  border: 1px solid transparent;
  border-radius: var(--radius-md);
  color: var(--text-primary);
  transition: background var(--duration-fast) var(--ease), border-color var(--duration-fast) var(--ease);
}

.kb-list__item:hover {
  background: var(--surface-hover);
  color: var(--text-primary);
}

.kb-list__item.is-current {
  border-color: var(--brand-primary);
  background: var(--brand-primary-bg);
}

.kb-list__name {
  display: flex;
  align-items: center;
  gap: 8px;
  font-size: 14.5px;
  font-weight: 700;
  line-height: var(--leading-tight);
}

.kb-list__meta {
  font-size: 12.5px;
  font-weight: 600;
  color: var(--text-muted);
}

.kb-list__warn {
  color: var(--warning);
}

.kb-list__empty {
  padding: 12px;
  font-size: 13.5px;
  color: var(--text-secondary);
}

.kb-list__more a {
  display: block;
  padding: 10px 12px;
  font-size: 13px;
  font-weight: 700;
  text-align: center;
  color: var(--brand-primary);
}

.kb-list__more.is-loading a {
  color: var(--text-muted);
  pointer-events: none;
}

/* ============================================================
   Conocimiento: la base elegida
   ============================================================ */

.kb-detail {
  display: block;
  min-width: 0;
}

.kb-detail__stack {
  display: flex;
  flex-direction: column;
  gap: 16px;
}

/* Turbo marks the frame while the next base is on its way. */
.kb-detail[busy] .kb-base {
  opacity: 0.6;
  transition: opacity var(--duration) var(--ease);
}

.kb-base__top {
  padding: 24px 28px 0;
  display: flex;
  flex-direction: column;
  gap: 14px;
}

.kb-base__head {
  display: flex;
  align-items: flex-start;
  justify-content: space-between;
  gap: 12px;
}

.kb-base__title {
  margin: 0;
  min-width: 0;
  font-size: 22px;
  line-height: var(--leading-tight);
}

.kb-base__desc {
  margin: 0;
  max-width: 78ch;
  font-size: 14.5px;
  line-height: var(--leading-normal);
  color: var(--text-secondary);
}

.kb-base__actions {
  display: flex;
  align-items: center;
  gap: 6px;
  flex-shrink: 0;
}

.kb-base__actions .menu {
  min-width: 220px;
}

.kb-base__actions .menu__item:disabled {
  opacity: 0.5;
  cursor: not-allowed;
}

/* Quién la usa y cuánto tiene. */
.kb-summary {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  gap: 10px 28px;
  padding: 12px 16px;
  border-radius: var(--radius-md);
  background: var(--surface-sunken);
}

.kb-summary__agents {
  flex: 1 1 280px;
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  gap: 8px;
}

.kb-summary__label,
.kb-summary__none {
  font-size: 13px;
  font-weight: 600;
  color: var(--text-secondary);
}

.kb-agent {
  display: inline-flex;
  align-items: center;
  gap: 6px;
  padding: 4px 12px;
  border: 1px solid var(--border-subtle);
  border-radius: var(--radius-full);
  background: var(--surface);
  font-size: 13.5px;
  font-weight: 700;
  color: var(--text-primary);
}

.kb-agent .icon {
  color: var(--brand-magenta);
}

.kb-agent:hover {
  border-color: var(--brand-primary);
  color: var(--brand-primary);
}

.kb-summary__figures {
  display: flex;
  gap: 20px;
  font-size: 13.5px;
  color: var(--text-secondary);
}

.kb-summary__figures strong {
  font-weight: 700;
  color: var(--text-primary);
}

.kb-summary__approval {
  flex-basis: 100%;
  margin: 0;
  font-size: 13px;
  color: var(--text-secondary);
}

.kb-tabs {
  margin: 4px -28px 0;
  padding: 0 28px;
}

.kb-panel {
  padding: 20px 28px 28px;
  display: flex;
  flex-direction: column;
  gap: 16px;
}

/* `display: flex` would show a panel even with `hidden`. */
.kb-panel[hidden] {
  display: none;
}

/* ---- Pestaña Documentos: subida ---- */

.kb-docs__bar {
  display: flex;
}

.kb-upload {
  flex: 1;
  margin: 0;
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  gap: 10px 14px;
}

.kb-upload .field__error {
  flex-basis: 100%;
}

.kb-upload__input {
  font-size: 13px;
}

.kb-upload__pick {
  cursor: pointer;
}

.kb-upload__hint {
  font-size: 13.5px;
  color: var(--text-secondary);
}

/* Over the upload limit: said where the eyes are after dropping. */
.kb-upload__hint.is-error {
  font-weight: 700;
  color: var(--danger);
}

.kb-upload__search {
  margin-left: auto;
  font-size: 13.5px;
  font-weight: 600;
}

/* Sin JavaScript, el selector de ficheros y «Subir». Con él (.is-enhanced), el botón abre el
   selector y elegir ya sube; el selector queda oculto pero sigue recibiendo el foco. */
.kb-docs:not(.is-enhanced) .kb-upload__pick,
.kb-docs:not(.is-enhanced) .kb-upload__hint {
  display: none;
}

.kb-docs.is-enhanced .kb-upload__input {
  position: absolute;
  width: 1px;
  height: 1px;
  overflow: hidden;
  clip: rect(0, 0, 0, 0);
  white-space: nowrap;
}

.kb-docs.is-enhanced .kb-upload__submit {
  display: none;
}

.kb-docs.is-enhanced .kb-upload__input:focus-visible + .kb-upload__pick {
  outline: 2px solid var(--focus-ring);
  outline-offset: 2px;
}

.kb-upload__off {
  margin: 0;
  display: flex;
  align-items: center;
  gap: 8px;
  font-size: 13.5px;
  font-weight: 600;
  color: var(--danger);
}

/* ---- Pestaña Documentos: acciones sobre los marcados ---- */

.kb-bulk {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  gap: 8px;
  margin: 0 0 12px;
  padding: 8px 8px 8px 16px;
  border: 1px solid var(--brand-primary-bg-strong);
  border-radius: var(--radius-md);
  background: var(--brand-primary-bg);
}

.kb-bulk__count {
  flex: 1 1 220px;
  font-size: 13.5px;
  font-weight: 700;
  color: var(--brand-magenta);
}

/* With JavaScript the bar only shows while something is marked. */
.kb-docs__body.is-enhanced .kb-bulk.is-idle {
  display: none;
}

/* Without it, the bar is always there and says what it is for. */
.kb-docs__body:not(.is-enhanced) .kb-bulk {
  border-color: var(--border-subtle);
  background: var(--surface-sunken);
}

.kb-docs__body:not(.is-enhanced) .kb-bulk__count {
  font-weight: 600;
  color: var(--text-secondary);
}

/* «Marcar todos» and «Cancelar» need JavaScript: without it they would do nothing. */
.kb-docs__body:not(.is-enhanced) .kb-docs__all,
.kb-docs__body:not(.is-enhanced) .kb-bulk__clear {
  visibility: hidden;
}

/* ---- Pestaña Documentos: la tabla y la zona de soltar ---- */

.kb-docs__zone {
  position: relative;
}

.kb-docs__drop {
  position: absolute;
  inset: 0;
  display: none;
  align-items: center;
  justify-content: center;
  gap: 10px;
  min-height: 120px;
  border: 2px dashed var(--brand-primary);
  border-radius: var(--radius-lg);
  background: rgb(249 236 239 / 0.94);
  font-size: 15px;
  font-weight: 700;
  color: var(--brand-primary);
  pointer-events: none;
}

.kb-docs__zone.is-over .kb-docs__drop {
  display: flex;
}

.kb-docs__zone.is-busy {
  opacity: 0.6;
  cursor: progress;
}

.kb-docs__empty {
  border: 1.5px dashed var(--border-strong);
  border-radius: var(--radius-lg);
}

.kb-docs__table {
  box-shadow: none;
  container-type: inline-size;
}

/* On a phone it scrolls sideways rather than squeezing the names. */
.kb-table {
  min-width: 600px;
}

.kb-table th,
.kb-table td {
  padding: 12px;
}

.kb-table .kb-table__pick {
  width: 20px;
  padding-left: 16px;
}

.kb-table__pick input {
  width: 16px;
  height: 16px;
  accent-color: var(--brand-primary);
  cursor: pointer;
}

.kb-table .kb-table__doc {
  width: 38%;
}

.kb-table .kb-table__when {
  white-space: nowrap;
  font-size: 13px;
  font-weight: 600;
  color: var(--text-muted);
}

.kb-table .kb-table__actions {
  width: 40px;
  padding-right: 10px;
  text-align: right;
}

.kb-doc {
  display: flex;
  align-items: center;
  gap: 12px;
  min-width: 0;
}

.kb-doc__icon {
  flex: none;
  width: 34px;
  height: 34px;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  border-radius: 9px;
  background: var(--surface-sunken);
  color: var(--brand-magenta);
}

.kb-doc__main {
  min-width: 0;
  display: flex;
  flex-direction: column;
  gap: 1px;
}

/* Long file names break anywhere instead of being cut: «informe-2024-v1» and «informe-2024-v2»
   have to be told apart. */
.kb-doc__name {
  display: block;
  font-weight: 700;
  color: var(--text-primary);
  overflow-wrap: anywhere;
}

.kb-doc__name:hover {
  color: var(--brand-primary);
}

.kb-doc__meta {
  font-size: 12.5px;
  font-weight: 600;
  color: var(--text-muted);
}

.kb-doc__uploaded {
  display: none;
}

/* With the list beside it the table rarely has room for six columns: the date moves under
   the name, where it takes no width of its own. */
@container (max-width: 820px) {
  .kb-table .kb-table__when {
    display: none;
  }

  .kb-doc__uploaded {
    display: inline;
  }
}

.kb-doc__state {
  display: flex;
  flex-direction: column;
  align-items: flex-start;
  gap: 2px;
}

.kb-doc__state .status {
  white-space: normal;
}

.kb-doc__fix {
  padding: 0;
  border: 0;
  background: transparent;
  font: 700 12.5px var(--font-sans);
  color: var(--brand-primary);
  cursor: pointer;
  text-align: left;
}

.kb-doc__fix:hover {
  color: var(--brand-primary-dark);
  text-decoration: underline;
}

/* Each document's switch is a button that sends the form, drawn like .switch. */
.kb-switch {
  display: inline-flex;
  align-items: center;
  gap: 8px;
  padding: 4px 0;
  border: 0;
  background: transparent;
  font: 600 13.5px var(--font-sans);
  color: var(--text-muted);
  cursor: pointer;
}

.kb-switch[aria-checked="true"] {
  color: var(--text-primary);
}

.kb-switch[aria-checked="true"] .switch__track {
  background: var(--brand-primary);
}

.kb-switch[aria-checked="true"] .switch__track::after {
  transform: translateX(16px);
}

.kb-switch:focus-visible {
  outline: none;
}

.kb-switch:focus-visible .switch__track {
  outline: 2px solid var(--focus-ring);
  outline-offset: 2px;
}

.kb-approval {
  display: inline-flex;
  align-items: center;
  gap: 5px;
  white-space: nowrap;
  font-size: 13.5px;
  font-weight: 600;
  color: var(--text-muted);
}

.kb-approval--on {
  color: var(--text-primary);
}

/* La papelera sólo se intuye al pasar por la fila: eliminar es lo menos frecuente. */
.kb-doc__delete {
  border-color: transparent;
  background: transparent;
  color: var(--text-muted);
  opacity: 0.55;
}

.kb-table tbody tr:hover .kb-doc__delete,
.kb-doc__delete:focus-visible {
  opacity: 1;
}

.kb-doc__delete:hover:not(:disabled) {
  border-color: transparent;
  background: var(--danger-bg);
  color: var(--danger);
}

.kb-docs__foot {
  margin: 0;
  font-size: 12.5px;
  font-weight: 600;
  color: var(--text-muted);
}

/* ---- Pestaña Probar búsqueda ---- */

.kb-probe__title {
  margin: 0;
  font-size: 17px;
}

.kb-probe__lead {
  margin: 4px 0 0;
  max-width: 78ch;
  font-size: 14px;
  color: var(--text-secondary);
}

.kb-probe__form {
  display: flex;
  flex-wrap: wrap;
  gap: 10px;
}

.kb-probe__input {
  flex: 1 1 320px;
  min-width: 0;
  padding: 11px 16px;
  font-size: 15px;
}

.kb-probe__placeholder {
  padding: 36px 24px;
  border: 1.5px dashed var(--border-strong);
  border-radius: var(--radius-md);
  text-align: center;
  font-size: 14px;
  color: var(--text-secondary);
}

.kb-hits__head {
  font-size: 13px;
  font-weight: 600;
  color: var(--text-secondary);
}

.kb-verdict {
  padding: 10px 12px;
  border-radius: 10px;
  font-size: 13px;
  font-weight: 600;
}

.kb-verdict--clear {
  background: var(--success-bg);
  color: var(--success);
  font-weight: 700;
}

.kb-verdict--flat {
  background: var(--warning-bg);
  color: var(--warning);
}

.kb-hits {
  list-style: none;
  margin: 0;
  padding: 0;
}

.kb-hit {
  display: flex;
  flex-direction: column;
  gap: 6px;
  padding: 12px 0;
  border-top: 1px solid var(--divider);
}

.kb-hit__head {
  display: flex;
  align-items: center;
  gap: 10px;
}

.kb-hit__source {
  flex: 1;
  min-width: 0;
  font-size: 13px;
  font-weight: 700;
  color: var(--text-primary);
}

.kb-hit__source:hover {
  color: var(--brand-primary);
}

.kb-hit__score {
  font-size: 12.5px;
  font-weight: 700;
  color: var(--brand-primary-dark);
  font-variant-numeric: tabular-nums;
}

.kb-hit__bar {
  background: var(--neutral-100);
}

.kb-hit__bar .meter__fill {
  background: var(--brand-primary);
}

.kb-hit__text {
  margin: 0;
  font-size: 13.5px;
  line-height: 1.5;
  color: var(--neutral-600);
  white-space: pre-line;
  display: -webkit-box;
  -webkit-line-clamp: 6;
  -webkit-box-orient: vertical;
  overflow: hidden;
}

.kb-probe__note {
  margin: 0;
  font-size: 12.5px;
  font-weight: 600;
  color: var(--text-muted);
}

/* Documento: el texto extraído */
.kb-text {
  margin: 0;
  max-height: 65vh;
  overflow-y: auto;
  white-space: pre-wrap;
  word-break: break-word;
  background: var(--neutral-25);
  color: var(--text-primary);
  border: 1px solid var(--border-subtle);
  font-size: 12.5px;
}


@media (max-width: 980px) {
  .kb-layout {
    grid-template-columns: minmax(0, 1fr);
  }

  /* En una columna la lista va arriba, con su propio scroll y sin pegarse. */
  .kb-list {
    position: static;
    max-height: none;
  }

  .kb-list__items {
    max-height: 45vh;
  }
}

@media (max-width: 700px) {
  .kb-base__top {
    padding: 18px 18px 0;
  }

  .kb-base__head {
    flex-wrap: wrap;
  }

  .kb-tabs {
    margin: 4px -18px 0;
    padding: 0 18px;
  }

  .kb-panel {
    padding: 16px 18px 20px;
  }

  .kb-upload__search {
    margin-left: 0;
  }

  .kb-doc__delete {
    opacity: 1;
  }
}


/* ============================================================
   Contexto de empresa
   ============================================================ */

.company-layout {
  display: flex;
  flex-wrap: wrap;
  gap: 20px;
  align-items: flex-start;
}

.company-editor {
  flex: 2 1 520px;
  min-width: 0;
  overflow: hidden;
}

.company-editor__bar {
  display: flex;
  gap: 6px;
  padding: 10px 14px;
  border-bottom: 1px solid var(--divider);
  font-size: 13px;
  font-weight: 700;
  color: var(--text-muted);
}

.company-editor__count {
  margin-left: auto;
  font-variant-numeric: tabular-nums;
}

/* El editor sin caja propia: la tarjeta ya es la caja. */
.company-editor .textarea {
  display: block;
  border: 0;
  border-radius: 0;
  padding: 20px 22px;
  font: 14px/1.7 var(--font-mono);
  min-height: 520px;
}

.company-editor .textarea:focus {
  outline: 0;
}

/* El foco se marca en la tarjeta entera: un anillo dentro del editor parecería un error. */
.company-editor:focus-within {
  border-color: var(--border-strong);
  box-shadow: 0 0 0 3px var(--brand-primary-bg);
}

.company-editor__foot {
  display: flex;
  align-items: center;
  gap: 16px;
  flex-wrap: wrap;
  padding: 12px 18px;
  border-top: 1px solid var(--divider);
}

.company-editor__foot .field {
  flex: 1 1 280px;
}

.company-editor__updated {
  font-size: 12.5px;
  font-weight: 600;
  color: var(--text-muted);
}

.company-editor .field__error {
  padding: 0 22px 12px;
}

.company-aside {
  flex: 1 1 300px;
  min-width: 0;
  display: flex;
  flex-direction: column;
  gap: 16px;
}

.company-size {
  padding: 22px;
}

.company-size__label {
  font-size: 13px;
  font-weight: 700;
  color: var(--text-secondary);
}

.company-size__value {
  margin-top: 4px;
  font-family: var(--font-display);
  font-size: 30px;
  line-height: 1.1;
  color: var(--brand-magenta);
  font-variant-numeric: tabular-nums;
}

.company-size__unit {
  font-family: var(--font-sans);
  font-size: 14px;
  font-weight: 700;
  color: var(--text-muted);
}

.company-size__calc {
  margin: 10px 0 0;
  font-size: 13.5px;
  line-height: 1.55;
  color: var(--text-secondary);
}

.company-size__calc strong {
  color: var(--text-primary);
}

.company-size__sep {
  height: 1px;
  margin: 16px 0;
  background: var(--divider);
}

.company-size__note {
  display: flex;
  gap: 10px;
  align-items: flex-start;
  margin: 0;
  font-size: 13.5px;
  line-height: 1.55;
  color: var(--neutral-600);
}

.company-size__note .dot {
  width: 8px;
  height: 8px;
  margin-top: 6px;
}

.company-guide {
  padding: 22px;
}

.company-guide__title {
  margin: 0;
  font-family: var(--font-sans);
  font-size: 15px;
  font-weight: 700;
}

.company-guide__list {
  list-style: none;
  margin: 12px 0 0;
  padding: 0;
  display: flex;
  flex-direction: column;
  gap: 12px;
  font-size: 13.5px;
  line-height: 1.5;
}

.company-guide__list a {
  font-weight: 700;
}

@media (max-width: 700px) {
  .company-editor .textarea {
    min-height: 360px;
    padding: 16px;
    font-size: 13px;
  }
}

.company-size__est {
  margin: 12px 0 0;
  font-size: 12px;
  font-weight: 600;
  color: var(--text-muted);
}

/* La ficha del documento va en la columna lateral: dato debajo de su etiqueta. */
.kb-facts {
  grid-template-columns: minmax(0, 1fr);
  gap: 4px;
}

.kb-facts dt {
  font-size: 12px;
  letter-spacing: 0.04em;
  text-transform: uppercase;
  color: var(--text-muted);
}

.kb-facts dd + dt {
  margin-top: 12px;
}

.kb-facts__action {
  margin: 6px 0 0;
}

/* Chunks of a document: what agents receive, or how the text would be chunked. */
.kb-chunks {
  list-style: none;
  margin: 0;
  padding: 0;
}

.kb-chunk {
  display: flex;
  flex-direction: column;
  gap: 6px;
  padding: 12px 0;
  border-top: 1px solid var(--divider);
}

.kb-chunk:first-child {
  border-top: 0;
  padding-top: 0;
}

.kb-chunk__head {
  display: flex;
  align-items: baseline;
  gap: 10px;
  font-size: 12.5px;
  font-weight: 700;
}

.kb-chunk__n {
  color: var(--brand-primary-dark);
  font-variant-numeric: tabular-nums;
}

.kb-chunk__heading {
  flex: 1;
  min-width: 0;
  color: var(--text-secondary);
}

.kb-chunk__size {
  color: var(--text-muted);
  font-variant-numeric: tabular-nums;
  white-space: nowrap;
}

.kb-chunk__text {
  margin: 0;
  font-size: 13px;
  line-height: 1.5;
  color: var(--neutral-600);
  white-space: pre-line;
  display: -webkit-box;
  -webkit-line-clamp: 5;
  -webkit-box-orient: vertical;
  overflow: hidden;
}

.kb-chunks__more {
  margin: 12px 0 0;
  font-size: 12.5px;
  font-weight: 600;
  color: var(--text-muted);
}

/* Editing the converted text: the box is the page. */
.kb-editor .textarea {
  display: block;
  width: 100%;
  min-height: 60vh;
  font: 13px/1.6 var(--font-mono);
}
