/* Size reports from their available panel, including a resized sidebar or drawer.
   Keep text at its normal size; reduce gaps first, then reflow cards and rows. */
.app-main > div {
  container: crm-page / inline-size;
  min-width: 0;
}

.g {
  grid-template-columns: repeat(12, minmax(0, 1fr));
  min-width: 0;
}

.g > *, .field, .plot, .kv, .ch > *, .filters > * {
  min-width: 0;
  max-width: 100%;
}

.ch .tools, .pager, .filters .seg {
  flex-wrap: wrap;
}

.kv { grid-template-columns: minmax(0, auto) minmax(0, 1fr); }
.kv > *, .kpi .m, .ch .d { overflow-wrap: anywhere; }

/* The expanded row belongs to the scroll viewport, not to the potentially
   wider table. No clipping: its charts are measured again at this width. */
.tscroll {
  container: crm-data-grid / inline-size;
  min-width: 0;
  max-width: 100%;
}

.tscroll > .tb > tbody > .det > td > div {
  box-sizing: border-box;
  width: calc(100cqi - 28px);
  max-width: calc(100cqi - 28px);
  min-width: 0;
  position: sticky;
  left: 14px;
}

.tscroll > .tb > tfoot > tr > td > div {
  max-width: calc(100cqi - 18px);
  flex-wrap: wrap;
}

/* A label, bar and value must share the card's actual width. The secondary
   figure moves underneath its value instead of pushing into the next card. */
.tscroll:not([data-table='offices']):not([data-table='by-office']) .det .hb {
  grid-template-columns: minmax(0, 1.3fr) minmax(16px, .6fr) minmax(0, 1fr);
  gap: 8px;
  min-width: 0;
}
.tscroll:not([data-table='offices']):not([data-table='by-office']) .det .hb :is(.n, .v) {
  min-width: 0;
  white-space: normal;
  overflow-wrap: anywhere;
  line-height: 1.35;
}
.tscroll:not([data-table='offices']):not([data-table='by-office']) .det .hb .v small { display: block; margin-left: 0; }
.tscroll:not([data-table='offices']):not([data-table='by-office']) .det .hb .tr { min-width: 0; }

@container crm-page (max-width: 1040px) {
  .g > :is(.c4, .c6) { grid-column: span 6; }
  .g > :is(.c8, .c9) { grid-column: span 12; }
}
@container crm-page (max-width: 760px) {
  .g > .c3 { grid-column: span 6; }
  .g > :is(.c4, .c6, .c8, .c9) { grid-column: span 12; }
}
@container crm-page (max-width: 420px) {
  .g > .c3 { grid-column: span 12; }
}

@container crm-data-grid (max-width: 1040px) {
  .det .g > :is(.c4, .c6) { grid-column: span 6; }
  .det .g > :is(.c8, .c9) { grid-column: span 12; }
  .det .c { padding: 12px; }
}
@container crm-data-grid (max-width: 820px) {
  .det .g > .c3 { grid-column: span 6; }
}
@container crm-data-grid (max-width: 620px) {
  .det .g > :is(.c4, .c6, .c8, .c9) { grid-column: span 12; }
}
@container crm-data-grid (max-width: 420px) {
  .det .g > .c3 { grid-column: span 12; }
}

/* Receipts used to force a 1280px fixed table even when its panel was 900px.
   Give names the remaining space and let date / cash / card wrap naturally. */
[data-table='receipts'].tscroll > .tb {
  min-width: 0;
  width: 100%;
  table-layout: auto;
}

[data-table='receipts'].tscroll > .tb > thead > tr > th,
[data-table='receipts'].tscroll > .tb > tbody > tr > td:not([colspan]) {
  box-sizing: border-box;
  width: auto !important; /* Overrides the old inline column preferences. */
  min-width: 0;
  max-width: none;
  padding: 7px clamp(4px, .65cqi, 9px);
  white-space: normal;
  overflow-wrap: anywhere;
}

[data-table='receipts'].tscroll > .tb > thead > tr > th:first-child,
[data-table='receipts'].tscroll > .tb > tbody > tr > td:not([colspan]):first-child {
  width: 26px !important;
  padding-inline: 4px;
}

[data-table='receipts'].tscroll > .tb > thead > tr > th:nth-child(2) { width: 13% !important; }
[data-table='receipts'].tscroll > .tb > thead > tr > th:nth-child(5) { width: 18% !important; }
[data-table='receipts'].tscroll > .tb > thead > tr > th:nth-child(6) { width: 13% !important; }

/* Sticky last columns previously painted over neighbouring data. The fitted
   table keeps them in flow; they remain explicit fields in the narrow view. */
[data-table='receipts'].tscroll > .tb > tbody > tr > td:not([colspan]):nth-last-child(-n + 2) {
  position: static;
  box-shadow: none;
}
[data-table='receipts'].tscroll > .tb > thead > tr > th:nth-last-child(-n + 2) {
  right: auto;
  box-shadow: none;
}
[data-table='receipts'].tscroll > .tb > tbody > tr > td:not([colspan]):last-child > button {
  white-space: normal;
  padding: 4px 7px;
}

/* Below this width, eleven table columns cannot remain legible together.
   Keep every value and action, with a labelled two-column receipt layout.
   The original sortable headers remain visible and keyboard-operable. */
@container crm-data-grid (max-width: 740px) {
  [data-table='receipts'].tscroll > .tb,
  [data-table='receipts'].tscroll > .tb > :is(thead, tbody, tfoot) {
    display: block;
    width: 100%;
  }
  [data-table='receipts'].tscroll > .tb > thead {
    position: sticky;
    top: 0;
    z-index: 4;
    background: var(--surface);
    border-bottom: 1px solid var(--grid);
  }
  [data-table='receipts'].tscroll > .tb > thead > tr {
    display: flex;
    flex-wrap: wrap;
    gap: 2px;
    padding: 6px;
  }
  [data-table='receipts'].tscroll > .tb > thead > tr::before {
    content: 'Сортировка:';
    padding: 5px 4px;
    color: var(--muted);
    font-size: 11px;
  }
  [data-table='receipts'].tscroll > .tb > thead > tr > th:nth-child(n) {
    display: block;
    position: static;
    width: auto !important;
    padding: 5px;
    border: 0;
    border-radius: 4px;
  }
  [data-table='receipts'].tscroll > .tb > thead > tr > th:first-child {
    width: 0 !important;
    padding: 0;
  }
  [data-table='receipts'].tscroll > .tb > tbody > .row {
    display: grid;
    grid-template-columns: 26px repeat(2, minmax(0, 1fr));
    border-bottom: 1px solid var(--grid);
    padding: 8px;
    gap: 0 8px;
  }
  [data-table='receipts'].tscroll > .tb > tbody > .row > td:not([colspan]) {
    display: block;
    min-width: 0;
    padding: 5px 0;
    border: 0;
    text-align: left;
  }
  [data-table='receipts'].tscroll > .tb > tbody > .row > td:first-child {
    grid-column: 1;
    grid-row: 1 / span 5;
    align-self: stretch;
  }
  [data-table='receipts'].tscroll > .tb > tbody > .row > td:not(:first-child)::before {
    display: block;
    color: var(--muted);
    font-size: 11px;
    font-weight: 400;
    margin-bottom: 2px;
  }
  [data-table='receipts'].tscroll > .tb > tbody > .row > td:nth-child(2)::before { content: 'Время'; }
  [data-table='receipts'].tscroll > .tb > tbody > .row > td:nth-child(3)::before { content: '№ чека'; }
  [data-table='receipts'].tscroll > .tb > tbody > .row > td:nth-child(4)::before { content: 'Тип'; }
  [data-table='receipts'].tscroll > .tb > tbody > .row > td:nth-child(5)::before { content: 'Точка'; }
  [data-table='receipts'].tscroll > .tb > tbody > .row > td:nth-child(6)::before { content: 'Кассир'; }
  [data-table='receipts'].tscroll > .tb > tbody > .row > td:nth-child(7)::before { content: 'Позиций'; }
  [data-table='receipts'].tscroll > .tb > tbody > .row > td:nth-child(8)::before { content: 'Наличные'; }
  [data-table='receipts'].tscroll > .tb > tbody > .row > td:nth-child(9)::before { content: 'Карта'; }
  [data-table='receipts'].tscroll > .tb > tbody > .row > td:nth-child(10)::before { content: 'Сумма'; }
  [data-table='receipts'].tscroll > .tb > tbody > .row > td:nth-child(11)::before { content: 'Чек'; }
  [data-table='receipts'].tscroll > .tb > tbody > .det,
  [data-table='receipts'].tscroll > .tb > tbody > .det > td,
  [data-table='receipts'].tscroll > .tb > tfoot > tr,
  [data-table='receipts'].tscroll > .tb > tfoot > tr > td {
    display: block;
    width: 100%;
    box-sizing: border-box;
  }
}
