/* Row Builder — dynamic multi-field row inputs for pipe-delimited fields */

.row-builder {
  --rb-accent: #3b82f6;
  background: #fff;
  border: 1px solid #e2e8f0;
  border-radius: 8px;
  padding: 16px;
  container-type: inline-size;
}

/* Header row (column labels) */
.rb-header-row {
  display: flex;
  gap: 8px;
  align-items: flex-start;
  margin-bottom: 4px;
}

.rb-header-label {
  font-size: .75rem;
  font-weight: 500;
  color: #64748b;
}

.rb-drag-spacer {
  width: 26px;
  flex: 0 0 26px;
}

.rb-remove-spacer {
  width: 40px;
  flex: 0 0 40px;
}

/* Data rows */
.rb-row {
  display: flex;
  gap: 8px;
  align-items: flex-start;
  padding: 8px 0;
  border-bottom: 1px solid #f1f5f9;
}

.rb-row:last-of-type {
  border-bottom: none;
}

.rb-row-dragging {
  opacity: .5;
  border: 1px dashed var(--rb-accent);
}

.rb-drop-target {
  border-top: 2px solid var(--rb-accent);
}

/* Inputs wrap (the row's fields, excluding handle/remove) */
.rb-inputs-wrap {
  display: flex;
  flex-direction: column;
  gap: 8px;
  flex: 1;
  min-width: 0;
}

.rb-inline-fields {
  display: flex;
  gap: 8px;
  align-items: flex-start;
  width: 100%;
}

.rb-field {
  display: flex;
  flex-direction: column;
  min-width: 0;
}

.rb-col-full {
  flex: 1 1 100%;
  width: 100%;
}

/* Mobile / repeated labels — hidden by default (header row covers row 1+) */
.rb-mobile-label {
  display: none;
  font-size: .75rem;
  font-weight: 500;
  color: #64748b;
  margin-bottom: 4px;
}

/* Inputs */
.rb-input {
  border: 1px solid #e2e8f0;
  border-radius: 6px;
  padding: 8px 10px;
  font-size: 14px;
  width: 100%;
  box-sizing: border-box;
  font-family: inherit;
  background: #fff;
  color: #1a1a1a;
  outline: none;
  transition: border-color .15s, background .15s;
}

.rb-input:focus {
  border-color: var(--rb-accent);
}

.rb-textarea {
  min-height: 64px;
  resize: vertical;
}

.rb-input-number {
  text-align: right;
}

.rb-input-error {
  border-color: #ef4444;
  background: #fef2f2;
}

.rb-field-error {
  font-size: .72rem;
  color: #ef4444;
  margin-top: 4px;
}

/* Currency input */
.rb-currency-wrap {
  display: flex;
  align-items: stretch;
  border: 1px solid #e2e8f0;
  border-radius: 6px;
  overflow: hidden;
  background: #fff;
}

.rb-currency-wrap:focus-within {
  border-color: var(--rb-accent);
}

.rb-currency-prefix {
  display: flex;
  align-items: center;
  padding: 8px 8px 8px 10px;
  font-size: 14px;
  color: #64748b;
  background: #f8fafc;
  border-right: 1px solid #e2e8f0;
}

.rb-currency-wrap .rb-input {
  border: none;
  border-radius: 0;
}

.rb-currency-wrap .rb-input:focus {
  border-color: transparent;
}

/* Drag handle */
.rb-drag-handle {
  color: #cbd5e1;
  cursor: grab;
  padding: 0 8px;
  font-size: 18px;
  align-self: center;
  flex: 0 0 26px;
  text-align: center;
  user-select: none;
}

.rb-row-dragging .rb-drag-handle {
  cursor: grabbing;
}

/* Remove button */
.rb-remove-btn {
  width: 32px;
  height: 32px;
  flex: 0 0 32px;
  border-radius: 6px;
  border: none;
  background: transparent;
  color: #94a3b8;
  cursor: pointer;
  font-size: 18px;
  line-height: 1;
  align-self: center;
  transition: background .15s, color .15s;
}

.rb-remove-btn:hover:not(:disabled) {
  background: #fee2e2;
  color: #ef4444;
}

.rb-remove-btn:disabled {
  color: #e2e8f0;
  cursor: not-allowed;
}

/* Add row button */
.rb-add-btn {
  display: inline-block;
  border: 1px dashed #cbd5e1;
  border-radius: 6px;
  padding: 8px 16px;
  background: #fff;
  color: #64748b;
  cursor: pointer;
  font-size: 14px;
  margin-top: 8px;
  transition: border-color .15s, color .15s;
}

.rb-add-btn:hover:not(:disabled) {
  border-color: var(--rb-accent);
  color: var(--rb-accent);
}

.rb-add-btn:disabled {
  color: #cbd5e1;
  border-color: #e2e8f0;
  cursor: not-allowed;
}

/* Empty message */
.rb-empty-message {
  font-size: .85rem;
  color: #94a3b8;
  padding: 8px 0;
}

/* Single column mode */
.row-builder-single .rb-row {
  border-bottom: none;
  padding: 0 0 8px 0;
}

.row-builder-single .rb-inputs-wrap {
  display: block;
}

.row-builder-single .rb-field {
  width: 100%;
  margin-bottom: 0;
}

.row-builder-single .rb-input {
  width: 100%;
}

/* Narrow container (e.g. multi-column row builder in a narrow form column,
   or small viewport): stack inputs vertically with labels above each */
@container (max-width: 500px) {
  .rb-header-row {
    display: none;
  }

  .rb-inline-fields {
    flex-direction: column;
  }

  .rb-inline-fields .rb-field {
    flex: 1 1 100% !important;
    width: 100% !important;
  }

  .rb-mobile-label {
    display: block;
  }
}

/* Fallback for browsers without container query support */
@media (max-width: 600px) {
  .rb-header-row {
    display: none;
  }

  .rb-inline-fields {
    flex-direction: column;
  }

  .rb-inline-fields .rb-field {
    flex: 1 1 100% !important;
    width: 100% !important;
  }

  .rb-mobile-label {
    display: block;
  }
}
