/**
 * Recipe editor — form grid, section cards, CSS Grid data tables (#recipeEdit).
 * Loaded after zootech shell/components; replaces Bootstrap .table in editor sections.
 */

/* —— Form 2×2 grid —— */
#recipeEdit .recipe-form-grid {
  display: grid;
  grid-template-columns: repeat(2, minmax(0, 1fr));
  gap: 1rem 1.25rem;
  margin-bottom: 0;
  align-items: start;
}

#recipeEdit .recipe-form-grid .form-group {
  margin-bottom: 0;
  min-width: 0;
}

#recipeEdit .recipe-form-grid .form-group label,
#recipeEdit .recipe-form-grid .zt-form-label-with-help {
  display: block;
  margin-bottom: 0.5rem;
  font-weight: 600;
  font-size: 0.875rem;
  color: var(--text-primary);
  min-height: 1.375rem;
  line-height: 1.375rem;
}

#recipeEdit .zt-form-label-with-help {
  position: relative;
  padding-right: 2rem;
}

#recipeEdit .zt-form-label-with-help .recipe-help-icon-btn {
  position: absolute;
  right: 0;
  top: 50%;
  transform: translateY(-50%);
  margin: 0;
  width: 1.5rem;
  height: 1.5rem;
  padding: 0;
  display: inline-flex;
  align-items: center;
  justify-content: center;
}

#recipeEdit .recipe-form-grid .form-control {
  width: 100%;
  max-width: 100%;
  box-sizing: border-box;
}

#recipeEdit .recipe-form-grid + .zt-notice {
  margin-top: 0.5rem;
}

#recipeEdit .recipe-section-card__help.zt-notice {
  margin: 0.75rem 1.5rem 0;
}

#recipeEdit .recipe-section-card__help.zt-notice.is-visible {
  margin-bottom: 0.5rem;
}

#recipeEdit .card-body > .zt-notice.is-visible {
  margin-bottom: 0.75rem;
}

@media (max-width: 768px) {
  #recipeEdit .recipe-plain-help.zt-notice {
    display: none !important;
  }
}

@media (max-width: 767.98px) {
  #recipeEdit .recipe-form-grid {
    grid-template-columns: 1fr;
  }
}

/* —— Section cards (single border, no nested shell) —— */
#recipeEdit .recipe-section-card {
  margin-bottom: 0 !important;
  flex-shrink: 0;
  border-radius: 12px;
  border: 1px solid var(--border-subtle);
  box-shadow: 0 1px 2px rgba(15, 23, 42, 0.04);
  overflow: hidden;
}

#recipeEdit .recipe-section-card > .card-header {
  background: var(--surface-muted) !important;
  font-weight: 600;
  padding: 0.75rem 1.5rem;
  border-bottom: 1px solid var(--border-subtle);
  /* Глобальный .card .card-header = 14px; карточка секции = 12px — без сброса виден «ступень» в углу */
  border-radius: 0;
}

#recipeEdit .recipe-section-card > .card-body {
  padding: 1rem 1.5rem 1.25rem;
}

#recipeEdit .recipe-section-card > .card-body.recipe-section-card__body--grid {
  padding: 0;
}

#recipeEdit #recipeForm > .card-body > .recipe-section-card + .recipe-section-card {
  margin-top: 0.25rem;
}

#recipeEdit .recipe-section-card__help {
  padding: 0.75rem 1.5rem 0;
  margin: 0;
}

#recipeEdit .recipe-section-card__body--grid .recipe-section-card__help:last-child {
  padding-bottom: 0.75rem;
}

html[data-theme="dark"] #recipeEdit .recipe-section-card > .card-header {
  background: var(--dm-field) !important;
  border-bottom-color: var(--dm-border);
}

html[data-theme="dark"] #recipeEdit .recipe-section-card {
  border-color: var(--dm-border);
  box-shadow: none;
}

/* —— Data grid base —— */
#recipeEdit .zt-data-grid__scroll {
  overflow-x: auto;
  overflow-y: hidden;
  -webkit-overflow-scrolling: touch;
}

#recipeEdit .zt-data-grid {
  display: flex;
  flex-direction: column;
  gap: 0.625rem;
  min-width: 100%;
  width: 100%;
}

#recipeEdit .zt-data-grid__body {
  display: flex;
  flex-direction: column;
}

#recipeEdit .zt-data-grid__head,
#recipeEdit .zt-data-grid__row {
  display: grid;
  grid-template-columns: var(--zt-data-grid-cols);
  min-width: min(100%, 52rem);
}

#recipeEdit .zt-data-grid--ingredients {
  --zt-data-grid-actions-col: 10.75rem;
  --zt-data-grid-cols: minmax(0, 1.5fr) repeat(5, minmax(0, 0.72fr)) var(--zt-data-grid-actions-col);
}

#recipeEdit .zt-data-grid--unloading-groups {
  --zt-data-grid-actions-col: 10.75rem;
  --zt-data-grid-cols: minmax(0, 1.35fr) minmax(0, 1.15fr) minmax(0, 0.78fr) minmax(0, 0.78fr) var(--zt-data-grid-actions-col);
}

#recipeEdit .zt-data-grid__cell {
  min-width: 0;
  padding: 0.5rem 0.75rem;
  vertical-align: middle;
  border-bottom: 1px solid var(--border-subtle);
  font-variant-numeric: tabular-nums;
  font-family: var(--font-numeric, inherit);
  background: var(--surface, #fff);
}

#recipeEdit .zt-data-grid__cell--head {
  display: flex;
  align-items: center;
  justify-content: center;
  text-align: center;
  background: var(--surface-muted);
  color: var(--text-secondary);
  font-size: 0.6875rem;
  font-weight: 600;
  text-transform: uppercase;
  letter-spacing: 0.04em;
  padding: 0.625rem 0.75rem;
  border-bottom: 1px solid var(--border-subtle);
  font-family: inherit;
  font-variant-numeric: normal;
}

#recipeEdit .zt-data-grid__cell--num {
  text-align: center;
}

#recipeEdit .zt-data-grid__cell--actions {
  display: flex;
  align-items: center;
  justify-content: flex-end;
  padding: 0.5rem 0.75rem;
  text-align: right;
}

#recipeEdit .zt-data-grid__cell--head.zt-data-grid__cell--actions {
  justify-content: center;
  text-align: center;
}

#recipeEdit .zt-data-grid__body .zt-data-grid__row:last-child .zt-data-grid__cell {
  border-bottom: none;
}

#recipeEdit .zt-data-grid__body .zt-data-grid__row:hover .zt-data-grid__cell {
  background: rgba(var(--primary-rgb), 0.05);
}

/* Итого: gap у .zt-data-grid отделяет body от foot; внутри foot — только боковые отступы */
#recipeEdit .zt-data-grid__foot {
  padding: 0 0.75rem 0.75rem;
  margin-top: 0;
}

#recipeEdit .zt-data-grid__foot .zt-data-grid__row--total {
  background: var(--surface-muted);
  border-radius: 10px;
  overflow: hidden;
}

#recipeEdit .zt-data-grid__foot .zt-data-grid__cell {
  padding: 0.625rem 0.75rem;
  font-weight: 600;
  font-size: 0.875rem;
  background: transparent;
  border: none;
}

#recipeEdit .zt-data-grid__foot .zt-data-grid__cell--foot-label,
#recipeEdit .zt-data-grid__foot .zt-data-grid__cell--span-2 {
  display: flex;
  align-items: center;
  text-align: left;
  justify-content: flex-start;
}

#recipeEdit .zt-data-grid__foot .zt-data-grid__cell--num {
  display: flex;
  align-items: center;
  justify-content: center;
}

#recipeEdit .zt-total-line {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 0.2rem;
}

#recipeEdit .zt-total-label {
  font-size: 0.625rem;
  font-weight: 600;
  text-transform: uppercase;
  letter-spacing: 0.04em;
  color: var(--text-secondary);
  line-height: 1.2;
}

html[data-theme="dark"] #recipeEdit .zt-total-label {
  color: var(--dm-muted);
}

#recipeEdit .zt-total-value {
  font-variant-numeric: tabular-nums;
  font-weight: 600;
  font-size: 0.875rem;
}

#recipeEdit .zt-data-grid__foot .zt-data-grid__cell--actions {
  padding: 0.625rem 0.75rem;
}

#recipeEdit .zt-data-grid__cell--span-2 {
  grid-column: span 2;
}

#recipeEdit .zt-data-grid .form-control,
#recipeEdit .zt-data-grid .form-select {
  width: 100%;
  max-width: 100%;
  min-height: 2.25rem;
  padding: 0.375rem 0.5rem;
  font-size: 0.875rem;
  box-sizing: border-box;
}

#recipeEdit .zt-data-grid .text-cell {
  display: inline-block;
  min-height: 2.25rem;
  line-height: 2.25rem;
  font-size: 0.875rem;
}

#recipeEdit .zt-data-grid input[type="number"] {
  -moz-appearance: textfield;
}

#recipeEdit .zt-data-grid input[type="number"]::-webkit-outer-spin-button,
#recipeEdit .zt-data-grid input[type="number"]::-webkit-inner-spin-button {
  -webkit-appearance: none;
  margin: 0;
}

/* Row actions — кнопки по высоте полей строки, колонка без лишнего воздуха */
#recipeEdit .recipe-table-row-actions {
  gap: 0.375rem;
  justify-content: flex-end;
  width: 100%;
  max-width: 100%;
}

/* Список рейсов: не width:100% — иначе прозрачный слой перекрывает .recipe-drag-handle слева */
#recipesList .recipe-table-row-actions {
  gap: 0.375rem;
  justify-content: flex-end;
  flex: 1 1 0;
  min-width: 0;
  width: auto;
  max-width: 100%;
  pointer-events: none;
}

#recipesList .recipe-table-row-actions .btn,
#recipesList .recipe-table-row-actions .btn-group {
  pointer-events: auto;
}

#recipesList .list-item-leading {
  position: relative;
  z-index: 2;
  flex-shrink: 0;
  pointer-events: auto;
}

#recipeEdit .recipe-table-move-group,
#recipesList .recipe-table-move-group,
.feed-dispensers-page .dispenser-card .recipe-table-move-group,
.components-page .component-card .recipe-table-move-group {
  display: inline-flex;
  flex-shrink: 0;
  gap: 0.25rem;
}

#recipeEdit .recipe-table-move-group > .btn + .btn,
#recipesList .recipe-table-move-group > .btn + .btn,
.feed-dispensers-page .dispenser-card .recipe-table-move-group > .btn + .btn,
.components-page .component-card .recipe-table-move-group > .btn + .btn {
  margin-left: 0 !important;
}

#recipeEdit .recipe-table-move-group .btn,
#recipeEdit .recipe-table-delete-btn,
#recipesList .recipe-table-move-group .btn,
#recipesList .recipe-table-delete-btn,
.feed-dispensers-page .dispenser-card .recipe-table-move-group .btn,
.feed-dispensers-page .dispenser-card .recipe-table-delete-btn,
.components-page .component-card .recipe-table-move-group .btn,
.components-page .component-card .recipe-table-delete-btn,
#recipesList .recipe-drag-handle {
  width: 2.5rem;
  height: 2.5rem;
  min-width: 2.5rem;
  min-height: 2.5rem;
  padding: 0 !important;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  line-height: 1;
  border-radius: var(--zt-radius-sm, 8px) !important;
  flex-shrink: 0;
}

#recipeEdit .recipe-table-move-group .btn i,
#recipeEdit .recipe-table-delete-btn i,
#recipesList .recipe-table-move-group .btn i,
#recipesList .recipe-table-delete-btn i,
.feed-dispensers-page .dispenser-card .recipe-table-move-group .btn i,
.feed-dispensers-page .dispenser-card .recipe-table-delete-btn i,
.components-page .component-card .recipe-table-move-group .btn i,
.components-page .component-card .recipe-table-delete-btn i,
#recipesList .recipe-drag-handle i {
  font-size: 0.875rem;
}

#recipesList .recipe-drag-handle {
  cursor: grab;
  user-select: none;
  -webkit-user-select: none;
}

#recipesList .recipe-drag-handle:active {
  cursor: grabbing;
}

#recipesList .recipe-drag-handle:focus-visible {
  outline: 2px solid var(--primary-color, #0d6efd);
  outline-offset: 2px;
  z-index: 2;
}

#recipesList .recipe-drag-handle i {
  pointer-events: none;
}

#recipeEdit .recipe-table-move-group .btn:focus,
#recipesList .recipe-table-move-group .btn:focus {
  z-index: 2;
}

#recipeEdit .recipe-table-delete-btn,
#recipesList .recipe-table-delete-btn,
.feed-dispensers-page .dispenser-card .recipe-table-delete-btn,
.components-page .component-card .recipe-table-delete-btn {
  background: transparent !important;
  border: 1px solid var(--border-light, rgba(148, 163, 184, 0.35)) !important;
  color: var(--text-muted, #94a3b8) !important;
  transition: var(--transition);
}

#recipeEdit .recipe-table-delete-btn:hover,
#recipeEdit .recipe-table-delete-btn:focus,
#recipesList .recipe-table-delete-btn:hover,
#recipesList .recipe-table-delete-btn:focus,
.feed-dispensers-page .dispenser-card .recipe-table-delete-btn:hover,
.feed-dispensers-page .dispenser-card .recipe-table-delete-btn:focus,
.components-page .component-card .recipe-table-delete-btn:hover,
.components-page .component-card .recipe-table-delete-btn:focus {
  color: var(--danger-color, #ef4444) !important;
  background: rgba(239, 68, 68, 0.08) !important;
  border-color: rgba(239, 68, 68, 0.35) !important;
}

/* Dark theme */
html[data-theme="dark"] #recipeEdit .zt-data-grid__cell {
  background: var(--dm-surface);
  border-bottom-color: var(--dm-border);
  color: var(--dm-text);
}

html[data-theme="dark"] #recipeEdit .zt-data-grid__cell--head {
  background: var(--dm-field);
  color: var(--dm-muted);
  border-bottom-color: var(--dm-border);
}

html[data-theme="dark"] #recipeEdit .zt-data-grid__body .zt-data-grid__row:hover .zt-data-grid__cell {
  background: rgba(var(--primary-rgb), 0.15);
}

html[data-theme="dark"] #recipeEdit .zt-data-grid__foot .zt-data-grid__row--total {
  background: var(--dm-field);
}

html[data-theme="dark"] #recipeEdit .zt-data-grid__foot .zt-data-grid__cell {
  background: transparent;
  color: var(--dm-text);
}

html[data-theme="dark"] #recipeEdit .recipe-table-delete-btn,
html[data-theme="dark"] #recipesList .recipe-table-delete-btn,
html[data-theme="dark"] .feed-dispensers-page .dispenser-card .recipe-table-delete-btn,
html[data-theme="dark"] .components-page .component-card .recipe-table-delete-btn {
  color: var(--dm-muted) !important;
}

html[data-theme="dark"] #recipeEdit .recipe-table-delete-btn:hover,
html[data-theme="dark"] #recipeEdit .recipe-table-delete-btn:focus,
html[data-theme="dark"] #recipesList .recipe-table-delete-btn:hover,
html[data-theme="dark"] #recipesList .recipe-table-delete-btn:focus,
html[data-theme="dark"] .feed-dispensers-page .dispenser-card .recipe-table-delete-btn:hover,
html[data-theme="dark"] .feed-dispensers-page .dispenser-card .recipe-table-delete-btn:focus,
html[data-theme="dark"] .components-page .component-card .recipe-table-delete-btn:hover,
html[data-theme="dark"] .components-page .component-card .recipe-table-delete-btn:focus {
  color: #f87171 !important;
  background: rgba(239, 68, 68, 0.12) !important;
  border-color: rgba(239, 68, 68, 0.35) !important;
}

html[data-theme="dark"] #recipeEdit .zt-data-grid .form-control {
  min-height: 2.5rem;
  line-height: 1.5;
  padding-top: 0.5rem;
  padding-bottom: 0.5rem;
  box-shadow: none !important;
  opacity: 1 !important;
  border-width: 1px;
  border-style: solid;
  background-color: var(--dm-field) !important;
  color: var(--dm-text) !important;
  border-color: var(--dm-border) !important;
}

html[data-theme="dark"] #recipeEdit .zt-data-grid .form-control:focus:not(:disabled):not(.mode-locked-field) {
  box-shadow: 0 0 0 0.2rem rgba(var(--primary-rgb), 0.25) !important;
}

html[data-theme="dark"] #recipeEdit .zt-data-grid .form-control.mode-locked-field,
html[data-theme="dark"] #recipeEdit .zt-data-grid .form-control:disabled,
html[data-theme="dark"] #recipeEdit .zt-data-grid .form-control[readonly] {
  background-color: #23292f !important;
  background-image: none !important;
  border: 1px solid rgba(255, 255, 255, 0.38) !important;
  color: var(--dm-text) !important;
  box-shadow: inset 0 0 0 1px rgba(0, 0, 0, 0.35) !important;
  min-height: 2.5rem;
  -webkit-text-fill-color: var(--dm-text) !important;
}
