/* Компоновка рабочих форм: ширина определяется содержимым и доступным местом. */
@media screen {
  main, .modal-section, .modal-body, .table-wrap, .filter-bar, .table-search { min-width: 0; }
  .page-head, .section-head, .form-actions { flex-wrap: wrap; }
  .page-head h1 { overflow-wrap: anywhere; min-width: 0; }
  .table-search .table-filter { min-width: 0; }
  .filter-bar:not(.form-grid) > label:has(select) { max-width: 100%; min-width: 0; }
  .filter-bar:not(.form-grid) > label > select { min-width: 0; max-width: min(480px, 100%); }

  /* У каждой пары подпись/поле есть место до перехода на следующую строку. */
  .filter-bar.form-grid {
    display: grid;
    grid-template-columns: repeat(auto-fit, minmax(min(100%, 440px), 1fr));
    gap: 14px 24px;
    align-items: start;
  }
  .filter-bar.form-grid > label {
    display: grid; grid-template-columns: 145px minmax(0, 1fr);
    align-items: center; min-width: 0; gap: 8px;
    white-space: nowrap;
  }
  .filter-bar.form-grid > label:has([name="note"]) { grid-column: 1 / -1; }
  .filter-bar.form-grid > label > :is(input, select, textarea) {
    width: 100% !important; min-width: 0; max-width: none;
    font: inherit; padding: 8px 10px;
    color: var(--text-primary); background: var(--surface-1);
    border: 1px solid var(--sect-border, var(--border)); border-radius: 8px;
  }
  .filter-bar.form-grid > label > :is([name="num"], [name="report_year"]) { max-width: 18ch; }
  .line-editor-grid { grid-template-columns: repeat(auto-fit, minmax(min(100%, 390px), 1fr)); }
  .line-editor-grid > .modal-section:first-child { grid-column: 1 / -1; }
  .line-editor-field { grid-template-columns: 135px minmax(0, 1fr); }
  .line-editor-field > span:first-child { white-space: normal; }

  .field-row { flex-wrap: wrap; }
  .field-row > .field { flex: 1 1 300px; min-width: 0; }
  .settings-form .field, .modal-overlay .modal-body .field { min-width: 0; }
  .settings-form .field > label { flex: 0 1 290px; min-width: 0; }
  .settings-form .field > :is(input:not([type="checkbox"]):not([type="radio"]), select, textarea),
  .modal-overlay .modal-body .field > :is(input:not([type="checkbox"]):not([type="radio"]), select, textarea) {
    width: 100% !important; min-width: 0; max-width: none; flex: 1 1 220px;
  }
  .modal-overlay .modal-body .field > label { min-width: 0; flex: 0 1 190px; }
  .modal-overlay .modal-body .field > .field-text { min-width: 0; overflow-wrap: anywhere; }
  .profile-grid { grid-template-columns: repeat(auto-fit, minmax(min(100%, 420px), 1fr)); }
  .card-grid { grid-template-columns: repeat(auto-fit, minmax(min(100%, 230px), 1fr)); }
  .card-kv { overflow-wrap: anywhere; }

  /* Длинные названия в списках переносятся, числовые колонки остаются цельными. */
  .data-table:not(.lines-edit) :is(th, td):not(.num-col):not(.nowrap):not(.perm-cell) { overflow-wrap: anywhere; }
  .lines-table .descr-col { min-width: 0; }
  .editor-wrap { container: line-editor / inline-size; max-height: none; overflow: visible; }
  .editor-wrap .lines-edit { table-layout: fixed; }
  .editor-wrap .lines-edit :is(th, td) { overflow-wrap: anywhere; white-space: normal; }
  .editor-wrap .lines-edit :is(input, select) {
    width: 100%; min-width: 0; max-width: none; min-height: 36px; font-size: 13px;
  }
  .editor-wrap .lines-edit :is(.ln-descr, .ln-work-cell, .ln-find, .ln-place) { min-width: 0; width: 100%; }
  .editor-wrap .lines-edit .ln-work-row { flex-wrap: wrap; }
  .editor-wrap .lines-edit .ln-work-row select { flex: 1 1 180px; min-width: 0; }
  .editor-wrap .lines-edit :is(th:first-child, td:first-child) { width: 42px; }
  .editor-wrap .lines-edit :is(th:last-child, td:last-child) { width: 48px; }
  .editor-wrap .lines-edit th { text-transform: none; font-size: 12px; }
  .editor-wrap .lines-edit thead th { position: static; }
  .editor-wrap .lines-edit td { padding: 8px 5px; }
  .modal-section:has(.editor-wrap) > .modal-section-head { display: block; }
  :is(input, select, textarea, button, a):focus-visible { outline: 2px solid var(--accent); outline-offset: 2px; }

  /* Порог по ширине самой зоны: работает и в узкой модалке на большом мониторе. */
  @container line-editor (max-width: 1700px) {
    .lines-edit, .lines-edit tbody { display: block; width: 100%; }
    .lines-edit thead, .lines-edit colgroup { display: none; }
    .editor-wrap .lines-edit tbody > tr:not([hidden]):not(.hidden) {
      display: grid; grid-template-columns: repeat(2, minmax(0, 1fr));
      gap: 12px 20px; padding: 16px 8px; border-bottom: 2px solid var(--gridline);
    }
    .editor-wrap .lines-edit tbody > tr > td:not([hidden]) {
      display: grid; grid-template-columns: 125px minmax(0, 1fr);
      align-items: start; gap: 6px 10px; width: auto; min-width: 0;
      padding: 0; border: none;
    }
    .editor-wrap .lines-edit td[data-label]::before { content: attr(data-label); color: var(--text-secondary); font-size: 12px; }
    .editor-wrap .lines-edit td:has([data-f="id_work_type"], [data-f="descr"], [data-f="note"], [data-f="id_material"]) { grid-column: 1 / -1; }
    .editor-wrap .lines-edit td.ln-work-cell > .ln-work-row { grid-column: 2; }
    .editor-wrap .lines-edit .ln-rest { grid-column: 2; white-space: normal; }
    .editor-wrap .lines-edit td:first-child { grid-column: 1; grid-row: 1; }
    .editor-wrap .lines-edit tbody > tr > td:last-child:not([hidden]) { grid-column: 2; grid-row: 1; display: flex; justify-content: flex-end; justify-self: end; }
    .editor-wrap .lines-edit td[colspan] { grid-column: 1 / -1; display: block; }
  }
  @container line-editor (max-width: 620px) {
    .editor-wrap .lines-edit tbody > tr:not([hidden]):not(.hidden) { grid-template-columns: minmax(0, 1fr); gap: 12px; }
    .editor-wrap .lines-edit tbody > tr > td:not([hidden]) { grid-column: 1; grid-template-columns: 110px minmax(0, 1fr); }
    .editor-wrap .lines-edit tbody > tr > td:last-child:not([hidden]) { grid-column: 1; grid-row: 1; display: flex; justify-self: end; }
    .editor-wrap .lines-edit td:first-child { padding-right: 45px; }
  }
  @media (max-width: 650px) {
    .filter-bar.form-grid > label { grid-template-columns: 130px minmax(0, 1fr); }
    .line-editor-field { grid-template-columns: 115px minmax(0, 1fr); }
    .settings-form .field, .modal-overlay .modal-body .field { gap: 8px; }
    .settings-form .field > label, .modal-overlay .modal-body .field > label { flex-basis: 44%; white-space: normal; }
    .settings-form .field > :is(input:not([type="checkbox"]), select, textarea),
    .modal-overlay .modal-body .field > :is(input:not([type="checkbox"]), select, textarea) { flex-basis: 0; }
    .table-search { flex-wrap: wrap; }
    .table-search .table-filter { flex: 1 1 140px; width: auto; }
    .form-actions { display: flex; gap: 8px; margin-bottom: 24px; }
    .form-actions .btn + .btn { margin-left: 0; }
  }
}
