/* entity-form.css — generic metadata-driven entity form (P2-04 · UX-03 redesign)
 *
 * All classes are prefixed `ef-` to avoid collisions with the parent app.
 * Tokens come from styles.css :root (UX-01). The form is designed to mount
 * inside the CRM slide-over panel (crm.css .crm-modal-*) — the wrapper fills
 * the panel body, the form body scrolls internally, and the footer sticks to
 * the bottom so Save/Cancel/Delete are always reachable with NO viewport
 * overflow (1366×768 and tablet portrait).
 */

/* ---- Outer wrapper — fills the panel body, column layout ---- */
.ef-wrapper {
  display: flex;
  flex-direction: column;
  width: 100%;
  height: 100%;
  min-height: 0;
}

/* ---- Top-level error bar (400 / general errors) ---- */
.ef-top-error {
  display: none;          /* shown by JS when needed */
  color: var(--danger);
  background: var(--danger-bg);
  border: 1px solid var(--danger);
  border-radius: var(--radius-md);
  padding: var(--space-2) var(--space-3);
  margin: var(--space-4) var(--space-5) 0;
  font-size: var(--text-sm);
  line-height: var(--leading-normal);
  flex-shrink: 0;
}

/* ---- Loading placeholder ---- */
.ef-loading {
  color: var(--text-secondary);
  font-style: italic;
  padding: var(--space-6) var(--space-4);
  text-align: center;
}

/* ---- Form body — the internal scroll area ---- */
.ef-form-body {
  display: flex;
  flex-direction: column;
  gap: var(--space-5);
  flex: 1 1 auto;
  min-height: 0;
  overflow-y: auto;
  padding: var(--space-5);
}

/* ---- Section ---- */
.ef-section {
  display: flex;
  flex-direction: column;
  gap: var(--space-3);
}

.ef-section-title {
  font-size: var(--text-xs);
  font-weight: 700;
  text-transform: uppercase;
  letter-spacing: 0.06em;
  color: var(--text-secondary);
  padding-bottom: var(--space-1);
  border-bottom: 1px solid var(--border-color);
}

/* ---- 2-column field grid (collapses to 1-col on narrow panels) ---- */
.ef-grid {
  display: grid;
  grid-template-columns: repeat(2, minmax(0, 1fr));
  gap: var(--space-3) var(--space-4);
}

/* Wide fields (textarea, multiselect) span both columns */
.ef-grid > .ef-field-wide {
  grid-column: 1 / -1;
}

/* ---- Field group ---- */
.ef-field-group {
  margin-bottom: 0;
  min-width: 0;
}

.ef-field-group label {
  display: block;
  margin-bottom: var(--space-1);
  font-size: var(--text-sm);
  color: var(--text-secondary);
}

/* Generic input/select/textarea styling */
.ef-field-input {
  width: 100%;
  padding: var(--space-2) var(--space-3);
  border: 1px solid var(--border-color);
  border-radius: var(--radius-md);
  background: var(--bg-dark);
  color: var(--text-primary);
  font-size: var(--text-sm);
  font-family: inherit;
  box-sizing: border-box;
  transition: border-color 0.18s ease, box-shadow 0.18s ease;
}

.ef-field-input:focus {
  outline: none;
  border-color: var(--primary);
  box-shadow: 0 0 0 2px color-mix(in srgb, var(--primary) 28%, transparent);
}

/* textarea-specific */
textarea.ef-field-input {
  resize: vertical;
  min-height: 4.5rem;
  line-height: var(--leading-normal);
}

/* ---- Read-only field value ---- */
.ef-readonly-value {
  padding: var(--space-2) var(--space-3);
  border-radius: var(--radius-md);
  background: var(--bg-dark);
  color: var(--text-secondary);
  font-size: var(--text-sm);
  border: 1px solid var(--border-color);
  word-break: break-all;
}

/* ---- Inline field error ---- */
.ef-field-error {
  display: none;          /* toggled by JS */
  color: var(--danger);
  font-size: var(--text-xs);
  margin-top: var(--space-1);
  line-height: var(--leading-normal);
}

/* ---- Bool field label (wraps checkbox + text) ---- */
.ef-bool-label {
  display: inline-flex;
  align-items: center;
  gap: var(--space-2);
  cursor: pointer;
  font-size: var(--text-sm);
  color: var(--text-primary);
  min-height: 44px; /* touch target */
}

.ef-bool-input {
  width: 1.05rem;
  height: 1.05rem;
  cursor: pointer;
  accent-color: var(--primary);
}

/* ---- Multiselect group ---- */
.ef-multiselect-group {
  display: flex;
  flex-direction: column;
  gap: var(--space-1);
  padding: var(--space-1) 0;
}

.ef-multiselect-item {
  display: flex;
  align-items: center;
  gap: var(--space-2);
  font-size: var(--text-sm);
  color: var(--text-primary);
  cursor: pointer;
  margin-bottom: var(--space-1);
}

.ef-multiselect-item input[type="checkbox"] {
  width: 1rem;
  height: 1rem;
  accent-color: var(--primary);
  cursor: pointer;
}

/* ---- Status area ---- */
.ef-status-area {
  margin-bottom: 0;
}

.ef-status-row {
  display: flex;
  align-items: center;
  gap: var(--space-3);
  flex-wrap: wrap;
  margin-top: var(--space-1);
}

/* Current status badge */
.ef-status-badge {
  display: inline-flex;
  align-items: center;
  padding: var(--space-1) var(--space-3);
  border-radius: 1rem;
  font-size: var(--text-xs);
  font-weight: 600;
  text-transform: uppercase;
  letter-spacing: 0.03em;
  background: var(--bg-light);
  color: var(--text-primary);
  white-space: nowrap;
}

/* Workflow transition buttons */
.ef-workflow-btns {
  display: flex;
  gap: var(--space-2);
  flex-wrap: wrap;
}

.ef-transition-btn {
  background: var(--bg-light);
  color: var(--text-primary);
  border: 1px solid var(--border-color);
  font-size: var(--text-sm);
  padding: var(--space-2) var(--space-3);
  cursor: pointer;
  border-radius: var(--radius-md);
  transition: background 0.18s ease, border-color 0.18s ease;
}

.ef-transition-btn:hover {
  background: var(--secondary);
  border-color: var(--primary);
}

.ef-transition-btn.ef-transition-btn-active {
  background: var(--primary);
  border-color: var(--primary-dark);
  color: var(--text-primary);
}

/* Inline 409 error near status buttons */
.ef-status-error {
  display: none;          /* shown by JS on 409 */
  color: var(--danger);
  font-size: var(--text-xs);
  line-height: var(--leading-normal);
}

/* A free status select (workflow-less types) inside the status row */
.ef-status-row select.ef-field-input {
  width: auto;
  min-width: 160px;
}

/* ---- Audit strip ---- */
.ef-audit-strip {
  display: flex;
  flex-direction: column;
  gap: var(--space-1);
  font-size: var(--text-xs);
  color: var(--text-secondary);
}

/* ---- Lines section — read-only line-item table (CRM redesign W2) ----
   The centered 880px modal gives the table real room: Pos · Product · Qty ·
   Unit price · VAT · Total, numerics in tabular mono, a right-aligned totals
   summary (Net / VAT / Total, Total emphasised). */
.ef-lines-table {
  width: 100%;
  border-collapse: collapse;
  font-size: var(--text-sm);
}

.ef-lines-table thead th {
  font-size: var(--text-xs);
  font-weight: 800;
  text-transform: uppercase;
  letter-spacing: 0.06em;
  color: var(--text-secondary);
  white-space: nowrap;
  padding: 0 var(--space-2) var(--space-2);
  text-align: left;
}

.ef-lines-table tbody td {
  padding: var(--space-2);
  border-top: 1px solid var(--border-soft);
  vertical-align: top;
}

.ef-lines-table tbody tr:hover td {
  background: rgba(148, 163, 184, 0.06);
}

/* Description grows; the product line reads as the row's title */
.ef-lines-table td.ef-lines-desc {
  width: 100%;
  color: var(--text-primary);
  font-weight: 600;
}

/* Numeric columns right-align */
.ef-lines-table td.ef-lines-no,
.ef-lines-table td.ef-lines-qty,
.ef-lines-table td.ef-lines-unit,
.ef-lines-table td.ef-lines-vat,
.ef-lines-table td.ef-lines-total,
.ef-lines-table th:nth-child(1),
.ef-lines-table th:nth-child(3),
.ef-lines-table th:nth-child(4),
.ef-lines-table th:nth-child(5),
.ef-lines-table th:nth-child(6) {
  text-align: right;
  white-space: nowrap;
}

/* …and render in tabular mono */
.ef-lines-table td.ef-lines-no,
.ef-lines-table td.ef-lines-qty,
.ef-lines-table td.ef-lines-unit,
.ef-lines-table td.ef-lines-vat,
.ef-lines-table td.ef-lines-total {
  font-family: 'JetBrains Mono', ui-monospace, 'SFMono-Regular', Menlo, monospace;
  font-variant-numeric: tabular-nums;
  color: var(--text-primary);
}
.ef-lines-table td.ef-lines-no,
.ef-lines-table td.ef-lines-vat { color: var(--text-secondary); }
.ef-lines-table td.ef-lines-total { font-weight: 700; }

/* Empty / error state for the Lines tab (no rows, or fetch failed) */
.ef-lines-empty {
  color: var(--text-secondary);
  font-size: var(--text-sm);
  font-style: italic;
  padding: var(--space-3) 0;
}

/* Totals summary — right-aligned, Total row emphasised */
.ef-lines-table tfoot td {
  border: none;
  padding: var(--space-1) var(--space-2);
}
.ef-lines-table tfoot .ef-lines-total-label {
  text-align: right;
  color: var(--text-secondary);
}
.ef-lines-table tfoot .ef-lines-total-val {
  text-align: right;
  font-family: 'JetBrains Mono', ui-monospace, 'SFMono-Regular', Menlo, monospace;
  font-variant-numeric: tabular-nums;
  color: var(--text-primary);
  min-width: 120px;
}
.ef-lines-table tfoot tr:last-child td {
  padding-top: var(--space-2);
  border-top: 1px solid var(--border-soft);
  font-size: var(--text-md);
  font-weight: 800;
}
.ef-lines-table tfoot tr:last-child .ef-lines-total-label,
.ef-lines-table tfoot tr:last-child .ef-lines-total-val {
  color: var(--text-primary);
}

/* ---- Sticky footer — action bar (CRM redesign W2) ---- */
.ef-footer {
  display: flex;
  justify-content: flex-end;
  align-items: center;
  flex-wrap: wrap;
  gap: var(--space-3);
  flex-shrink: 0;
  padding: var(--space-3) var(--space-5);
  border-top: 1px solid var(--border-soft);
  background: var(--bg-deep);
}

/* Button group */
.ef-btn-group {
  display: flex;
  gap: var(--space-2);
  flex-wrap: wrap;
  align-items: center;
}

/* Secondary actions — outline buttons (Print / Download PDF / Cancel) */
.ef-print-btn,
.ef-pdf-btn,
.ef-cancel-btn {
  background: transparent;
  color: var(--text-primary);
  border: 1px solid var(--border);
}
.ef-print-btn:hover,
.ef-pdf-btn:hover,
.ef-cancel-btn:hover {
  border-color: var(--secondary);
  background: rgba(148, 163, 184, 0.08);
}

/* Delete — danger outline, pushed to the far left */
.ef-delete-btn {
  background: transparent;
  color: var(--danger-soft);
  border: 1px solid var(--danger);
  margin-right: auto;
}
.ef-delete-btn:hover {
  background: var(--danger-bg);
}

/* Save / Create — uses btn-primary from styles.css, no override needed */

/* ---- Phone (≤768): full-screen sheet (shell in crm.css) ----
   1-col grid, sticky bottom action bar at 44px targets, and the line table
   re-stacks into labelled cards (qty/unit/vat/total read inline). */
@media (max-width: 768px) {
  .ef-grid {
    grid-template-columns: 1fr;
  }

  .ef-footer {
    justify-content: stretch;
    position: sticky;
    bottom: 0;
  }
  .ef-btn-group {
    width: 100%;
    justify-content: flex-end;
  }
  .ef-save-btn { flex: 1; }
  .ef-save-btn,
  .ef-cancel-btn,
  .ef-print-btn,
  .ef-pdf-btn,
  .ef-delete-btn { min-height: var(--touch); }
  .ef-delete-btn { margin-right: auto; }

  /* Line table → stacked cards */
  .ef-lines-table,
  .ef-lines-table tbody,
  .ef-lines-table tbody tr,
  .ef-lines-table tbody td {
    display: block;
    width: 100%;
  }
  .ef-lines-table thead { display: none; }
  .ef-lines-table tbody tr {
    border: 1px solid var(--border-soft);
    border-radius: var(--r-card);
    padding: var(--space-2) var(--space-3);
    margin-bottom: var(--space-2);
    background: var(--bg-card);
  }
  .ef-lines-table tbody td {
    border: none;
    padding: 2px 0;
    text-align: left;
  }
  .ef-lines-table tbody td.ef-lines-no { display: none; }
  .ef-lines-table tbody td.ef-lines-desc { font-weight: 700; margin-bottom: var(--space-1); }
  .ef-lines-table tbody td.ef-lines-qty::before   { content: 'Qty: ';   color: var(--text-secondary); }
  .ef-lines-table tbody td.ef-lines-unit::before  { content: 'Unit: ';  color: var(--text-secondary); }
  .ef-lines-table tbody td.ef-lines-vat::before   { content: 'VAT: ';   color: var(--text-secondary); }
  .ef-lines-table tbody td.ef-lines-total::before { content: 'Total: '; color: var(--text-secondary); }

  /* Totals → right-aligned lines */
  .ef-lines-table tfoot { display: block; margin-top: var(--space-2); }
  .ef-lines-table tfoot tr { display: flex; justify-content: flex-end; gap: var(--space-3); }
  .ef-lines-table tfoot td { padding: 2px 0; }
  .ef-lines-table tfoot tr:last-child td { border-top: none; }
}
