/* ============================================================
   Интерактивность таблиц (этап 9): сортировка по клику на заголовок, раскрытие продукта до курсов и тарифов,
   переключатель продукта у «Возражений». Отдельный файл, общие токены --ga-* не меняются.
   ============================================================ */

/* --- Заголовок-кнопка сортировки (table-sort.js; у серверной сортировки «Учеников» — такие же кнопки,
   у «Истории мероприятий» — ссылки a.th-sort с тем же видом) --- */
table th button.th-sort,
table th button.th-sort:hover,
table th button.th-sort:focus,
table th button.th-sort:active {
  all: unset;
  box-sizing: border-box;
  display: inline-flex;
  align-items: baseline;
  gap: 0.3em;
  max-width: 100%;
  cursor: pointer;
  font: inherit;
  color: inherit;
  text-align: inherit;
  white-space: inherit;
  transform: none;
}

table th a.th-sort {
  display: inline-flex;
  align-items: baseline;
  gap: 0.3em;
  max-width: 100%;
  color: inherit;
  text-decoration: none;
}

table th button.th-sort:hover,
table th a.th-sort:hover {
  color: var(--ga-green-dark);
  text-decoration: underline;
  text-decoration-color: var(--ga-sage);
  text-underline-offset: 3px;
}

table th button.th-sort:focus-visible,
table th a.th-sort:focus-visible {
  outline: 2px solid var(--ga-focus);
  outline-offset: 2px;
  border-radius: 3px;
}

/* У числовых столбцов значок слева, чтобы текст заголовка стоял ровно над числами. */
table th.num button.th-sort,
table th.num a.th-sort {
  flex-direction: row-reverse;
}

.sort-ind {
  display: inline-block;
  min-width: 0.8em;
  font-size: 0.8em;
  line-height: 1;
  text-align: center;
  color: var(--ga-sage-ink);
}

.sort-ind::after {
  content: "↕";
  opacity: 0.45;
}

th[aria-sort="descending"] .sort-ind::after {
  content: "▼";
  opacity: 1;
  color: var(--ga-green-dark);
}

th[aria-sort="ascending"] .sort-ind::after {
  content: "▲";
  opacity: 1;
  color: var(--ga-green-dark);
}

th[aria-sort] {
  box-shadow: inset 0 -2px 0 var(--ga-green);
}

/* --- Раскрытие продукта до курсов и тарифов (drilldown.js) --- */
button.row-expand,
button.row-expand:hover,
button.row-expand:focus,
button.row-expand:active {
  all: unset;
  box-sizing: border-box;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  width: 1.6em;
  height: 1.6em;
  margin-right: 0.35em;
  /* шаг B: квадрат с радиусом 3 px на беже (как «?» и кнопки сайта), без кружка */
  border-radius: var(--ga-radius-card);
  cursor: pointer;
  color: var(--ga-forest-deep);
  background: var(--ga-beige);
  font-size: 0.85em;
  line-height: 1;
  vertical-align: 0.05em;
  transform: none;
}

/* Место кнопки у строк без раскрытия: та же ширина, названия в столбце стоят ровно. */
.row-expand-spacer {
  display: inline-block;
  width: 1.6em;
  margin-right: 0.35em;
  font-size: 0.85em;
}

button.row-expand:hover {
  background: var(--ga-beige-hover);
  color: var(--ga-ink);
}

button.row-expand[aria-expanded="true"] {
  background: var(--ga-forest);
  color: var(--ga-on-dark-cream);
}

button.row-expand:focus-visible {
  outline: 2px solid var(--ga-focus);
  outline-offset: 2px;
}

button.row-expand::before {
  content: "▸";
  transition: transform 0.15s ease;
}

button.row-expand[aria-expanded="true"]::before {
  transform: rotate(90deg);
}

tr.expand-row > td {
  padding: 0 0 var(--ga-space-3) 2.2em;
  background: var(--ga-bg);
}

tbody tr.expand-row:hover > td {
  background: var(--ga-bg);
}

/* Раскрытие не раздвигает столбцы внешней таблицы: его ширина — ровно ширина ячейки, лишнее прокручивается. */
tr.expand-row > td > [data-expand-slot] {
  width: 0;
  min-width: 100%;
}

.courses-box {
  padding-top: var(--ga-space-2);
}

.courses-box table {
  margin-bottom: var(--ga-space-2);
  background: var(--ga-surface);
  border: 0;
  border-radius: var(--ga-radius-card);
}

/* Название курса — самый широкий столбец: на широком экране не переносится при пустых числовых столбцах. */
.courses-table :is(th, td):first-child {
  min-width: 13rem;
  width: 45%;
}

.courses-box .courses-note {
  display: block;
  margin: 0;
  line-height: 1.4;
}

.courses-box .course-variants {
  display: block;
  color: var(--ga-sage-ink);
  font-size: 0.85em;
}

/* --- Расшифровка выручки (этап 15): «Как посчитано» и CSV над курсами, платежи курса вторым уровнем --- */
.courses-head {
  display: flex;
  flex-wrap: wrap;
  align-items: flex-start;
  gap: var(--ga-space-2) var(--ga-space-3);
  margin: 0 0 var(--ga-space-2);
}

.courses-head > details.fold {
  flex: 1 1 16rem;
  margin: 0;
}

.courses-head > .courses-csv {
  margin: 0 0 0 auto;
}

/* «Как посчитано»: 3–5 строк цифрами — без абзацев, без маркеров списка */
.how-lines {
  margin: 0;
  padding: 0;
  list-style: none;
  font-size: var(--ga-text-sm);
  line-height: 1.45;
}

.how-lines > li {
  margin: 0;
  padding: 0;
  list-style: none;
}

.how-lines > li.how-total {
  margin-top: var(--ga-space-1);
  font-weight: 600;
}

/* Платежи курса внутри раскрытой строки курса: та же кремовая подложка, белая таблица */
.course-payments {
  padding-top: var(--ga-space-2);
}

.course-payments table {
  margin-bottom: var(--ga-space-2);
  background: var(--ga-surface);
  border: 0;
  border-radius: var(--ga-radius-card);
}

/* Дата и сумма — по содержимому, «Почему такая сумма» — всё остальное место: переносится, хвост мельче.
   Таблица платежей лежит внутри ячейки .courses-table, и правило столбца названия курса (min-width 13rem) задевало бы
   и её «Дату»: min-width: 0 снимает его (тот же вес, правило ниже по файлу). */
.course-payments-table :is(th, td):first-child {
  width: 1%;
  min-width: 0;
  white-space: nowrap;
}

.course-payments-table .cp-why {
  width: 60%;
  min-width: 14rem;
  white-space: normal;
}

.course-payments-table .cp-why small {
  font-size: 0.85em;
}

.course-payments-table .cp-student {
  min-width: 9rem;
}

/* Телефон: раскрытие лежит в широкой таблице «Линеек», которая листается вбок, — «Как посчитано» и платежи не шире
   экрана (видны без прокрутки), а каждый платёж — карточкой: дата и сумма в строку, ниже ученик и «почему». */
@media (max-width: 575.98px) {
  .courses-head,
  .course-payments {
    max-width: calc(100vw - 4.5rem);
  }

  .courses-head > .courses-csv {
    margin-left: 0;
  }

  .course-payments-table,
  .course-payments-table :is(tbody, tfoot) {
    display: block;
  }

  .course-payments-table thead {
    position: absolute;
    width: 1px;
    height: 1px;
    overflow: hidden;
    clip-path: inset(50%);
    white-space: nowrap;
  }

  .course-payments-table tr {
    display: grid;
    grid-template-columns: 1fr auto;
    gap: 2px var(--ga-space-2);
    padding: var(--ga-space-2);
    border-bottom: 1px solid var(--ga-line);
  }

  .course-payments-table :is(th, td) {
    display: block;
    width: auto;
    min-width: 0;
    padding: 0;
    border: 0;
    background: none;
  }

  .course-payments-table tr > :first-child {
    grid-column: 1;
    grid-row: 1;
  }

  .course-payments-table tr > .num {
    grid-column: 2;
    grid-row: 1;
  }

  .course-payments-table :is(.cp-student, .cp-why, tfoot td:last-child) {
    grid-column: 1 / -1;
    min-width: 0;
  }

  /* В карточке ширины столбцов таблицы не нужны: «Дата» не держит 13rem/1 %, «почему» — не 60 %, а во всю карточку,
     сумма справа помещается целиком. Вес выше, чем у обычных правил .cp-why и :first-child, и правило стоит позже. */
  .course-payments-table :is(th, td):is(:first-child, .cp-why, .cp-student) {
    width: auto;
    min-width: 0;
  }

  .course-payments-table tfoot td:last-child:empty {
    display: none;
  }

  .course-payments-table tfoot tr {
    background: var(--ga-beige);
    font-weight: 700;
  }
}

/* --- Переключатель продукта у таблиц (data-table-filter) --- */
.table-filter {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  gap: var(--ga-space-2) var(--ga-space-3);
  margin: 0 0 var(--ga-space-3);
}

.table-filter label {
  display: inline-flex;
  align-items: center;
  gap: var(--ga-space-2);
  margin: 0;
  font-size: var(--ga-text-sm);
}

.table-filter select {
  width: auto;
  min-width: 14rem;
  margin: 0;
}

/* Подзаголовок группы (продукт у «Возражений») — как tr.row-group в app.css: forest 600, черта снизу */
tr.group-row > th {
  background: var(--ga-surface);
  color: var(--ga-forest);
  font-weight: 600;
  width: auto;
  border-bottom: 1px solid var(--ga-line-strong);
}

tr.group-row > th small {
  font-weight: 400;
  color: var(--ga-sage-ink);
}

.table-sort-note {
  margin: calc(-1 * var(--ga-space-2)) 0 var(--ga-space-3);
}

/* Название рядом с квадратом ▸ — висячим отступом: кнопка в начале первой строки, перенос названия идёт ровно под
   его первой буквой (раньше на телефоне квадрат вставал отдельной строкой, а название — под ним). У строк без
   раскрытия — тот же отступ ячейки вместо пустой распорки: inline-block распорка уводила название на вторую строку,
   и строка выглядела сбитой. Отступ = кнопка с полем справа: (1.6em + 0.35em) × 0.85 ≈ 1.66em. Только экран: в печати
   кнопок и распорок нет. */
@media screen {
  table.compact td:has(> :is(button.row-expand, .row-expand-spacer)) {
    padding-left: calc(12px + 1.66em);
  }

  table.compact td:has(> button.row-expand) {
    text-indent: -1.66em;
  }

  td:has(> button.row-expand) > * {
    text-indent: 0;
  }

  table.compact td > .row-expand-spacer {
    display: none;
  }
}

/* Палец: квадрат ▸, кнопки сортировки, «исправить» у платежа и галочки выбора — зона нажатия 44 px (невидимая, как у
   «?»), вид и высота строк те же */
@media (pointer: coarse) {
  button.row-expand,
  button.row-expand:is(:hover, :focus, :active) {
    position: relative;
  }

  button.row-expand::after {
    content: "";
    position: absolute;
    inset: -13px;
  }

  table th button.th-sort,
  table th button.th-sort:is(:hover, :focus, :active),
  table th a.th-sort {
    position: relative;
  }

  table th button.th-sort::after,
  table th a.th-sort::after {
    content: "";
    position: absolute;
    inset: -12px -6px;
  }

  details.row-actions > summary {
    display: inline-flex;
    align-items: center;
    min-height: 44px;
  }

  td.check-col input[type="checkbox"] {
    width: 24px;
    height: 24px;
  }
}

@media (max-width: 575.98px) {
  /* Узкий экран: колонка названий со ▸ не уже 11rem — название идёт в строку с квадратом */
  td:has(> button.row-expand),
  td:has(> .row-expand-spacer) {
    min-width: 11rem;
  }

  .table-filter select {
    min-width: 0;
    width: 100%;
  }

  .table-filter label {
    flex: 1 1 100%;
    flex-wrap: wrap;
  }

  tr.expand-row > td {
    padding-left: var(--ga-space-2);
  }
}

@media print {
  .sort-ind,
  button.row-expand,
  .row-expand-spacer,
  tr.expand-row,
  .table-filter {
    display: none !important;
  }

  th[aria-sort] {
    box-shadow: none;
  }
}
