/* Samples module styles. Loaded after styles.css so the references-row /
   pill / lightrag-cell classes from the books table are reused unchanged
   for sample documents (Documentos subtab) and the flat Informes tab. */

/* Override the references-modal grid: the original was tuned for the
   book-upload modal's 7-row layout. Our structure is header + active
   panel, so collapse to 2 rows with the panel taking remaining height.
   Hidden panels are display:none and don't take grid tracks. */
#references-modal .book-modal-inner {
  grid-template-rows: auto minmax(0, 1fr) !important;
  gap: var(--space-2);
}

/* One-row header: title · toggle · orphans · spacer · + Add · Cerrar.
   All four chrome elements sit on the same line. Wraps to a new line
   on narrow screens via flex-wrap. */
.contents-modal-header {
  display: flex;
  align-items: center;
  justify-content: flex-start;
  gap: 14px;
  flex-wrap: wrap;
}

.contents-modal-header h2 {
  margin: 0;
  flex: 0 0 auto;
}

.contents-header-spacer {
  flex: 1 1 auto;
  min-width: 0;
}

/* Segmented control: single rounded container with three inset buttons.
   A position:absolute indicator slides between segments on tab change,
   driven by JS-computed left/width set via CSS custom properties. */
.segmented-toggle {
  position: relative;
  display: inline-flex;
  align-items: stretch;
  border: 1px solid var(--line);
  border-radius: 999px;
  padding: 3px;
  background: var(--surface);
  gap: 0;
  isolation: isolate;  /* keep the indicator stacked beneath buttons */
}

/* The sliding indicator pattern had a timing bug: slideTo() measured
   button dimensions in requestAnimationFrame while the modal was still
   `display: none`, so width came out 0 and the indicator stayed
   invisible until the operator clicked. Replaced by painting the
   teal background directly on .is-active buttons — see below. The
   indicator element is kept in the HTML so old DOM doesn't break,
   but it's invisible. */
.segmented-indicator {
  display: none;
}

/* Base state — !important on background + border because the modal's
   generic `.book-modal header button` (styles.css:118+) also matches
   these and would otherwise paint a surface fill + border that covers
   our sliding indicator. */
.segmented-toggle .seg-btn,
.segmented-toggle .seg-btn:hover,
.segmented-toggle .seg-btn:focus,
.segmented-toggle .seg-btn:active {
  position: relative;
  z-index: 1;  /* sit above the indicator */
  appearance: none;
  border: none !important;
  background: transparent !important;
  color: var(--ink);
  font-size: 0.85em;
  padding: 4px 18px;
  border-radius: 999px;
  cursor: pointer;
  min-height: 0 !important;
  height: auto !important;
  line-height: 1.4;
  white-space: nowrap;
  box-shadow: none !important;
}

/* Active button: paint the teal background directly on the button —
   no more separate indicator element, no JS measurement, no timing
   bug. The button's existing border-radius: 999px makes it a pill;
   the container's 3px padding insets it naturally. */
.segmented-toggle .seg-btn.is-active,
.segmented-toggle .seg-btn.is-active:hover,
.segmented-toggle .seg-btn.is-active:focus {
  background: var(--accent) !important;
  color: white !important;
}

/* Checkbox toggle: lock dimensions because the modal's default
   .book-modal input { height: 38px } would blow it up. */
.contents-modal-header .orphans-toggle {
  display: inline-flex;
  align-items: center;
  gap: 6px;
  font-size: 0.78em;
  color: var(--muted);
  cursor: pointer;
  white-space: nowrap;
}

.contents-modal-header .orphans-toggle input[type="checkbox"] {
  appearance: auto;
  width: 14px !important;
  height: 14px !important;
  min-height: 0 !important;
  min-width: 0 !important;
  padding: 0 !important;
  margin: 0 !important;
  flex: 0 0 auto;
}

#contents-add-button {
  padding: 5px 14px !important;
  min-height: 0 !important;
  height: auto !important;
  font-size: 0.85em;
  border-radius: 999px;
  flex: 0 0 auto;
}

#references-modal-close {
  flex: 0 0 auto;
}

.tab-panel.hidden {
  display: none;
}

/* Add/edit sample modal: same outer footprint as #book-modal (the
   `.book-modal` class supplies 1120×860). Inner grid rows from top:
   - Row 1: small-input grid (4 columns: Código/Nombre/Custodio/Lugar)
   - Row 2: textarea grid (2×2: Descripción/Condiciones/Certificaciones/Trabajo)
   - Row 3 (1fr): movements section — internally scrollable
   - Row 4: meta strip (edit mode only; hidden in create)
   - Row 5: audit block (edit mode only; hidden in create)
   - Row 6: result line
   - Row 7: footer
   The 1fr row is the movements section so the modal stays at 860px
   regardless of how many movements the operator has queued. */
#add-sample-modal .book-modal-inner {
  grid-template-rows: auto minmax(0, 1fr) !important;
}
.add-sample-form-flow {
  display: grid;
  grid-template-rows: auto auto minmax(0, 1fr) auto auto auto auto;
  gap: var(--space-3);
  min-height: 0;
  height: 100%;
}

/* Row 1: 4 small inputs side-by-side. Stacks 2x2 on narrow screens. */
.add-sample-small-grid {
  display: grid;
  grid-template-columns: repeat(4, minmax(120px, 1fr));
  gap: var(--space-3);
  align-items: end;
}

/* Row 2: textareas in a 2x2 grid. Collapses to one column on narrow. */
.add-sample-textarea-grid {
  display: grid;
  grid-template-columns: repeat(2, minmax(0, 1fr));
  gap: var(--space-3);
}
.field-textarea {
  display: grid;
  gap: 4px;
  font-size: 11px;
}
.field-textarea > span {
  font-weight: 600;
  color: var(--muted);
  text-transform: uppercase;
  letter-spacing: 0.04em;
  font-size: 10px;
}
.field-textarea textarea {
  width: 100%;
  min-height: 60px;
  resize: vertical;
  font: inherit;
  padding: 8px 10px;
  border: 1px solid var(--line);
  border-radius: var(--radius);
  background: var(--surface);
  color: var(--ink);
}
.field-textarea textarea:focus {
  outline: 2px solid var(--accent);
  outline-offset: -1px;
}

@media (max-width: 820px) {
  .add-sample-small-grid { grid-template-columns: 1fr 1fr; }
  .add-sample-textarea-grid { grid-template-columns: 1fr; }
}

/* Movement-rows block in the Add-sample modal: lets the operator queue
   one or more chain-of-custody movements at sample creation time. */
.movement-rows-block {
  border: 1px solid var(--line);
  border-radius: var(--radius);
  padding: var(--space-2);
  margin-top: var(--space-2);
  background: var(--surface);
}

.movement-rows-head {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 6px;
}

.movement-rows-head h3 {
  margin: 0;
  font-size: 0.92em;
}

.movement-rows-list {
  display: flex;
  flex-direction: column;
  gap: 8px;
}

.movement-row-card {
  border: 1px solid var(--line);
  border-radius: var(--radius);
  padding: 8px;
  background: var(--bg, white);
  display: grid;
  grid-template-columns: repeat(4, minmax(100px, 1fr)) auto;
  gap: 6px 8px;
  align-items: end;
}

.movement-row-card .field {
  gap: 2px !important;
  font-size: 11px;
}

.movement-row-card .field input {
  min-height: 30px !important;
  height: 30px !important;
  font-size: 12px !important;
  padding: 4px 8px !important;
}

.movement-row-card .field span {
  font-size: 10px;
  color: var(--muted);
}

.movement-row-card .movement-row-remove {
  align-self: end;
  padding: 4px 8px !important;
  min-height: 30px !important;
  height: 30px !important;
  background: #fce9e3;
  border: 1px solid #e9bdb0;
  color: #a3331d;
  border-radius: var(--radius);
  cursor: pointer;
  font-size: 11px;
}

/* Lock pill on existing-movement rows in edit mode. Sits in the same
   slot as the remove pill but glyph-only (🔒 / 🔓). Clicking it flips
   the disabled state on the row's inputs (handled in samples-ui.js).
   The .movement-row-existing class is what marks the card as
   prefilled-from-DB, and .movement-row-edited highlights rows the
   operator has unlocked so they pop visually. */
.movement-row-card .movement-row-lock-pill {
  align-self: end;
  padding: 4px 6px !important;
  min-height: 30px !important;
  height: 30px !important;
  width: 36px;
  text-align: center;
  background: var(--surface);
  border: 1px solid var(--line);
  color: var(--ink);
  border-radius: var(--radius);
  cursor: pointer;
  font-size: 13px;
  line-height: 1;
}
.movement-row-card .movement-row-lock-pill:hover { background: var(--surface-strong); }
.movement-row-existing { opacity: 0.85; }
.movement-row-edited {
  opacity: 1;
  border-color: var(--accent);
  background: linear-gradient(to right, rgba(0, 132, 117, 0.04), transparent);
}
.movement-row-card input:disabled {
  background: var(--surface);
  color: var(--muted);
  cursor: not-allowed;
}

.movement-rows-add-btn {
  padding: 4px 12px !important;
  min-height: 0 !important;
  height: auto !important;
  font-size: 0.82em;
  border-radius: 999px;
}

/* All three upload-related modals share the canonical book-modal width
   so they feel like a family. The base .book-modal class defines the
   1120px target; we just confirm/extend it to the report + add-sample
   dialogs that also use this class. */
#book-modal,
#upload-report-modal,
#add-sample-modal {
  width: min(1120px, calc(100vw - 32px));
}

/* Report-specific block: target sample + kind. Two-column grid that
   matches .book-metadata's cadence so the form reads as one unit. */
.report-fields-block {
  display: grid;
  grid-template-columns: minmax(220px, 1.35fr) minmax(190px, .95fr);
  gap: var(--space-3);
  align-items: end;
  padding-bottom: var(--space-2);
  border-bottom: 1px dashed var(--line);
}
.report-fields-block .field {
  gap: 4px;
  font-size: 11px;
}

/* Report modal grid: extra row above .book-metadata for the
   .report-fields-block. The seven other slots mirror book-modal. */
#upload-report-modal .book-modal-inner {
  grid-template-rows: auto auto auto auto 96px minmax(0, 1fr) auto auto;
}

/* Samples list rows use the same grid pattern as references-row but with
   sample-specific columns. */
.sample-row {
  display: grid;
  grid-template-columns: minmax(120px, 1.2fr) minmax(160px, 2fr) minmax(120px, 1fr) minmax(120px, 1.2fr) 70px 90px;
  gap: 8px;
  align-items: center;
  padding: 6px var(--space-2);
  border-bottom: 1px solid var(--line);
  cursor: pointer;
}

.sample-row.sample-head {
  font-weight: 600;
  color: var(--muted);
  background: var(--surface);
  cursor: default;
  font-size: 0.78em;
  text-transform: uppercase;
  letter-spacing: 0.04em;
}

.sample-row:hover:not(.sample-head):not(.sample-row-orphan) {
  background: var(--surface);
}

.sample-row-orphan {
  font-style: italic;
  color: var(--muted);
}

.sample-row-orphan .sample-code-cell::before {
  content: "huérfanos · ";
}

.sample-row .sample-attachment-count,
.sample-row .sample-movement-count {
  font-variant-numeric: tabular-nums;
}

/* Sample detail drill-down */
.sample-detail {
  padding: 6px 0;
}

.sample-detail-back {
  display: inline-flex;
  align-items: center;
  gap: 4px;
  background: transparent;
  border: none;
  color: var(--accent);
  cursor: pointer;
  font-size: 0.85em;
  padding: 2px 0;
  margin-bottom: 6px;
}

.sample-detail-header {
  display: grid;
  grid-template-columns: minmax(120px, auto) 1fr;
  gap: 4px 12px;
  margin-bottom: 10px;
  padding-bottom: 10px;
  border-bottom: 1px solid var(--line);
  font-size: 0.88em;
}

.sample-detail-header dt {
  color: var(--muted);
  font-weight: 600;
}

.sample-detail-header dd {
  margin: 0;
}

.sample-detail-actions {
  display: flex;
  gap: 6px;
  margin-bottom: 10px;
}

.sample-subtabs {
  display: flex;
  gap: 4px;
  margin-bottom: 8px;
  border-bottom: 1px solid var(--line);
}

.sample-subtabs .subtab-button {
  background: transparent;
  border: none;
  border-bottom: 2px solid transparent;
  padding: 4px 12px;
  font-size: 0.85em;
  cursor: pointer;
  color: var(--muted);
}

.sample-subtabs .subtab-button.is-active {
  /* Was var(--text) which is undefined — the active subtab fell
     back to inherited color, making it visually identical to
     inactive. Use --ink (the canonical foreground token defined
     in styles.css :root) so the active subtab actually reads as
     active. The bottom-border accent stays as the primary cue. */
  color: var(--ink);
  border-bottom-color: var(--accent);
}

.sample-subtab-panel.hidden {
  display: none;
}

/* Reports table reuses the .reference-row grid look but adds a Sample
   and Kind column on the left. */
.report-row {
  display: grid;
  grid-template-columns: minmax(120px, 1fr) minmax(80px, 0.7fr) minmax(160px, 2fr) minmax(80px, 0.5fr) 100px 80px 80px 80px;
  gap: 8px;
  align-items: center;
  padding: 6px var(--space-2);
  border-bottom: 1px solid var(--line);
}

.report-row.report-head {
  font-weight: 600;
  color: var(--muted);
  background: var(--surface);
  font-size: 0.78em;
  text-transform: uppercase;
  letter-spacing: 0.04em;
}

.report-row-orphan {
  background: rgba(200, 130, 50, 0.05);
}

.report-row .report-kind-badge {
  display: inline-block;
  padding: 1px 6px;
  border-radius: 4px;
  background: var(--surface);
  border: 1px solid var(--line);
  font-size: 0.75em;
  text-transform: capitalize;
}

/* Per-row action pills mirror .reference-download / .reference-delete */
.report-reassign,
.report-delete-pill {
  display: inline-block;
  padding: 2px 6px;
  font-size: 8px;
  border-radius: 4px;
  cursor: pointer;
  text-decoration: none;
  text-align: center;
}

.report-reassign {
  border: 1px solid var(--line);
  background: transparent;
  color: var(--text);
}

.report-reassign:hover {
  background: var(--surface);
}

.report-delete-pill {
  border: 1px solid #e9bdb0;
  background: #fce9e3;
  color: #a3331d;
}

.report-delete-pill:hover {
  background: #f6d4ca;
}

/* Movement table */
.movement-row {
  display: grid;
  grid-template-columns: 110px minmax(120px, 1fr) minmax(120px, 1fr) minmax(160px, 1.5fr) 80px;
  gap: 8px;
  align-items: center;
  padding: 6px var(--space-2);
  border-bottom: 1px solid var(--line);
}

.movement-row.movement-head {
  font-weight: 600;
  color: var(--muted);
  background: var(--surface);
  font-size: 0.78em;
  text-transform: uppercase;
  letter-spacing: 0.04em;
}

.movement-row.movement-voided {
  text-decoration: line-through;
  color: var(--muted);
}

.movement-void-pill,
.movement-voided-badge {
  display: inline-block;
  padding: 1px 6px;
  font-size: 8px;
  border-radius: 4px;
}

.movement-void-pill {
  border: 1px solid #e9bdb0;
  background: #fce9e3;
  color: #a3331d;
  cursor: pointer;
}

.movement-voided-badge {
  border: 1px solid var(--line);
  background: var(--surface);
  color: var(--muted);
  font-style: italic;
}

/* Audit subtab */
.audit-row {
  display: grid;
  grid-template-columns: 140px 130px 90px 1fr;
  gap: 8px;
  align-items: center;
  padding: 4px var(--space-2);
  border-bottom: 1px solid var(--line);
  font-family: var(--mono, ui-monospace, monospace);
  font-size: 0.78em;
}

.audit-row.audit-head {
  font-weight: 600;
  color: var(--muted);
  background: var(--surface);
}

/* Two-mode delete dialog layout */
.delete-sample-options {
  display: grid;
  grid-template-columns: 1fr;
  gap: 12px;
  margin: 10px 0;
}

.delete-sample-option {
  border: 1px solid var(--line);
  border-radius: 6px;
  padding: 10px 12px;
}

.delete-sample-option legend {
  font-weight: 600;
  padding: 0 4px;
}

.delete-sample-option button {
  margin-top: 6px;
}

/* Sample chooser inside move-attachment modal */
#move-attachment-target {
  width: 100%;
}

/* Editar pill on samples-list rows. Same metrics as .reference-delete
   / .reference-download pills for visual family. Stop-propagation in
   samples-ui.js prevents the row's drill-down click from firing. */
.sample-row-actions {
  text-align: right;
}
.sample-row-edit {
  display: inline-block;
  padding: 2px 8px;
  font-size: 10px;
  border: 1px solid var(--line);
  background: var(--surface);
  color: var(--ink);
  border-radius: 4px;
  cursor: pointer;
}
.sample-row-edit:hover {
  background: var(--surface-strong);
  border-color: var(--accent);
  color: var(--accent);
}

/* Edit-mode metadata strip at the bottom of the sample modal. Small,
   italic, muted — operators glance, don't read. */
.add-sample-meta-strip {
  display: flex;
  flex-wrap: wrap;
  gap: var(--space-2);
  font-size: 0.78em;
  color: var(--muted);
  font-style: italic;
  padding: 4px 2px;
  border-top: 1px dashed var(--line);
}

/* Collapsible activity log. summary shows "(N eventos)". The inner
   list scrolls if the audit is long so the modal stays at 860px. */
.add-sample-audit-block {
  border: 1px solid var(--line);
  border-radius: var(--radius);
  padding: 6px var(--space-2);
  font-size: 0.85em;
  background: var(--surface);
}
.add-sample-audit-block summary {
  cursor: pointer;
  font-weight: 600;
  color: var(--ink);
  padding: 2px 0;
}
.add-sample-audit-block summary:hover {
  color: var(--accent);
}
.add-sample-audit-list {
  margin-top: 6px;
  max-height: 200px;
  overflow-y: auto;
  border: 1px solid var(--line);
  border-radius: var(--radius);
  padding: 4px;
}

/* Movement-section structure inside the add-sample form. The section
   takes the 1fr row of .add-sample-form-flow; its own inner grid
   stacks the head bar + the hint + the scrollable list. */
.add-sample-movements-section {
  display: grid;
  grid-template-rows: auto auto minmax(0, 1fr);
  gap: 6px;
  min-height: 0;
  border: 1px solid var(--line);
  border-radius: var(--radius);
  padding: var(--space-2);
  background: var(--surface);
}
.add-sample-movements-head {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: var(--space-2);
}
.add-sample-movements-head strong {
  font-size: 0.92em;
}
.add-sample-movements-hint {
  margin: 0;
  font-size: 0.78em;
  color: var(--muted);
}
.add-sample-movement-list-scroll {
  overflow-y: auto;
  min-height: 0;
  display: flex;
  flex-direction: column;
  gap: 8px;
  padding: 2px;
}
.add-sample-movement-list-scroll:empty::before {
  content: "Sin movimientos registrados. Pulsa + Añadir movimiento para encadenar una fila.";
  display: block;
  text-align: center;
  color: var(--muted);
  font-size: 0.8em;
  padding: 14px 6px;
  font-style: italic;
}
