:root {
  --bg: #eef1f6;
  --bg-soft: #f7f9fc;
  --card: #ffffff;
  --border: #dfe4ec;
  --border-strong: #c9d1de;
  --text: #1a2230;
  --muted: #64748b;
  --accent: #2563eb;
  --accent-soft: #dbe7ff;
  --accent-text: #1d4ed8;
  --shadow: 0 1px 2px rgba(15, 23, 42, .06), 0 8px 24px rgba(15, 23, 42, .07);
  --radius: 14px;
  --radius-sm: 9px;
  --pair-w: 36px;
}

@media (prefers-color-scheme: dark) {
  :root {
    --bg: #0e131c;
    --bg-soft: #151b26;
    --card: #1a2130;
    --border: #2a3444;
    --border-strong: #3a465a;
    --text: #e7ecf3;
    --muted: #94a3b8;
    --accent: #4f8cff;
    --accent-soft: #22304a;
    --accent-text: #a9c6ff;
    --shadow: 0 1px 2px rgba(0, 0, 0, .4), 0 10px 30px rgba(0, 0, 0, .35);
  }
}

* { box-sizing: border-box; }
html, body { height: 100%; }

body {
  margin: 0;
  font-family: "Segoe UI", system-ui, -apple-system, "Helvetica Neue", Arial, sans-serif;
  background: var(--bg);
  color: var(--text);
  font-size: 15px;
  line-height: 1.45;
  -webkit-font-smoothing: antialiased;
  /* страница занимает ровно окно: шапки нет, фильтры сверху,
   * таблица растягивается на остаток и прокручивается внутри */
  display: flex;
  flex-direction: column;
  min-height: 100%;
}

/* ---------- Скрытый заголовок (для скринридеров) ---------- */
.sr-only {
  position: absolute;
  width: 1px; height: 1px;
  margin: -1px; padding: 0;
  overflow: hidden;
  clip: rect(0 0 0 0);
  white-space: nowrap;
  border: 0;
}

/* ---------- Кнопки ---------- */
.btn {
  font: inherit; cursor: pointer; border-radius: 10px; padding: 8px 12px;
  border: 1px solid var(--border-strong); background: var(--card); color: var(--text);
  transition: background .15s, border-color .15s, transform .05s; white-space: nowrap;
}
.btn:hover { border-color: var(--accent); color: var(--accent-text); }
.btn:active { transform: translateY(1px); }
.btn:disabled { opacity: .5; cursor: not-allowed; }
.btn:disabled:hover { border-color: var(--border-strong); color: var(--text); }
.btn-ghost { background: transparent; }
.btn.is-set {
  border-color: color-mix(in srgb, #d97706 55%, var(--border-strong));
  color: #b45309;
}
.btn.saved {
  border-color: #059669;
  color: #059669;
  background: color-mix(in srgb, #059669 10%, transparent);
}
@media (prefers-color-scheme: dark) {
  .btn.is-set { color: #fbbf24; }
}

/* ---------- Фильтры ---------- */
.filters {
  position: sticky; top: 0; z-index: 30;
  display: flex; align-items: flex-end; gap: 12px; flex-wrap: wrap;
  padding: 10px clamp(12px, 3vw, 32px);
  background: color-mix(in srgb, var(--bg) 88%, transparent);
  backdrop-filter: blur(8px);
  border-bottom: 1px solid var(--border);
}
.field { display: flex; flex-direction: column; gap: 5px; }
.field-label { font-size: .72rem; font-weight: 600; text-transform: uppercase; letter-spacing: .05em; color: var(--muted); }
select {
  font: inherit; min-width: 170px; padding: 8px 34px 8px 12px; border-radius: 10px;
  border: 1px solid var(--border-strong); background-color: var(--card); color: var(--text);
  cursor: pointer; appearance: none;
  background-image: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' width='12' height='12' viewBox='0 0 12 12'%3E%3Cpath d='M2 4l4 4 4-4' fill='none' stroke='%2364748b' stroke-width='1.8' stroke-linecap='round' stroke-linejoin='round'/%3E%3C/svg%3E");
  background-repeat: no-repeat; background-position: right 12px center;
  transition: border-color .15s, box-shadow .15s;
}
select:focus { outline: none; border-color: var(--accent); box-shadow: 0 0 0 3px var(--accent-soft); }

/* ---------- Комбобокс (преподаватель) ---------- */
.combo { position: relative; }
.combo input {
  font: inherit;
  width: 220px;
  max-width: 100%;
  padding: 8px 30px 8px 12px;
  border-radius: 10px;
  border: 1px solid var(--border-strong);
  background: var(--card);
  color: var(--text);
  transition: border-color .15s, box-shadow .15s;
}
.combo input::placeholder { color: var(--muted); }
.combo input:focus { outline: none; border-color: var(--accent); box-shadow: 0 0 0 3px var(--accent-soft); }
.combo.open input { border-color: var(--accent); }

.combo-clear {
  position: absolute;
  right: 8px;
  top: 50%;
  transform: translateY(-50%);
  width: 20px; height: 20px;
  display: flex; align-items: center; justify-content: center;
  border: 0; border-radius: 50%; cursor: pointer;
  background: color-mix(in srgb, var(--muted) 18%, transparent);
  color: var(--muted);
  font-size: 15px; line-height: 1;
  padding: 0;
}
.combo-clear:hover { background: var(--border-strong); color: var(--text); }

.combo-list {
  position: absolute;
  top: calc(100% + 4px);
  left: 0;
  min-width: 100%;
  max-width: 340px;
  max-height: 320px;
  overflow: auto;
  margin: 0;
  padding: 4px;
  list-style: none;
  background: var(--card);
  border: 1px solid var(--border-strong);
  border-radius: 10px;
  box-shadow: var(--shadow);
  z-index: 50;
}
.combo-list li {
  display: flex;
  align-items: baseline;
  justify-content: space-between;
  gap: 12px;
  padding: 7px 10px;
  border-radius: 7px;
  cursor: pointer;
  font-size: .88rem;
  white-space: nowrap;
}
.combo-list li:hover,
.combo-list li.active { background: var(--accent-soft); color: var(--accent-text); }
.combo-list li .cnt { color: var(--muted); font-size: .74rem; flex: none; }
.combo-list li.active .cnt { color: var(--accent-text); }
.combo-list li.combo-empty { cursor: default; color: var(--muted); justify-content: flex-start; }
.combo-list li.combo-empty:hover { background: transparent; color: var(--muted); }
/* ---------- Контент ---------- */
main { padding: 10px clamp(12px, 3vw, 32px); flex: 1 1 auto; min-height: 0; display: flex; flex-direction: column; }

.state {
  max-width: 520px; margin: 48px auto; text-align: center; color: var(--muted);
  background: var(--card); border: 1px solid var(--border); border-radius: var(--radius);
  padding: 26px 22px; box-shadow: var(--shadow);
}
.state.error { color: #dc2626; border-color: #fecaca; }
.state h2 { margin: 0 0 6px; font-size: 1.05rem; color: inherit; }
.state p { margin: 0; font-size: .88rem; }

/* ---------- Таблица ---------- */
.table-wrap {
  background: var(--card);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  box-shadow: var(--shadow);
  overflow: auto;
  /* не выходить за пределы окна: высота ограничена остатком под фильтрами */
  flex: 0 1 auto;
  max-height: 100%;
  /* при одной группе карточка не растягивается на всю ширину,
   * а при большом числе групп по-прежнему занимает всю доступную ширину */
  width: fit-content;
  max-width: 100%;
}

table.tt {
  border-collapse: separate;
  border-spacing: 0;
  table-layout: fixed;
  /* auto в fixed-таблице = сумма ширин колонок,
   * поэтому «День» и «Пара» не растягиваются на всю ширину */
  width: auto;
}

table.tt th,
table.tt td {
  border-bottom: 1px solid var(--border);
  border-right: 1px solid var(--border);
  vertical-align: top;
  text-align: left;
}
table.tt tr > *:last-child { border-right: 0; }
table.tt tbody tr:last-child > * { border-bottom: 0; }

/* верхний ряд — заголовки групп */
table.tt thead th {
  position: sticky;
  top: 0;
  z-index: 4;
  background: var(--bg-soft);
  font-size: .84rem;
}

.grp-col {
  padding: 8px 10px;
  width: 250px;
  min-width: 250px;
  max-width: 250px;
  border-bottom: 3px solid var(--c, var(--border)) !important;
}
.grp-btn {
  font: inherit; cursor: pointer; background: none; border: 0; padding: 0;
  display: flex; flex-direction: column; align-items: flex-start; gap: 1px;
  color: inherit; text-align: left; width: 100%;
}
.grp-btn:hover .grp-code { text-decoration: underline; }
.grp-year { font-size: .72rem; font-weight: 600; color: var(--c, var(--muted)); letter-spacing: .04em; }
.grp-code { font-size: .98rem; font-weight: 700; color: var(--text); }

/* липкая колонка «№» */
.pair-col {
  position: sticky;
  left: 0;
  background: var(--bg-soft);
  z-index: 2;
  width: var(--pair-w); min-width: var(--pair-w); max-width: var(--pair-w);
}

/* Заголовок «№» должен перекрывать уезжающие под него заголовки групп:
 * у `table.tt thead th` специфичность выше, поэтому селектор такой же подробный. */
table.tt thead th.pair-col { z-index: 7; }

thead th.pair-col { padding: 8px 0; text-align: center; }
thead .lbl { font-size: .72rem; text-transform: uppercase; letter-spacing: .04em; color: var(--muted); }

/* ---------- Строка дня ---------- */
.day-row .day-th {
  padding: 0;
  background: var(--bg-soft);
  border-top: 2px solid var(--border-strong);
  text-align: left;
}
tbody .day-row:first-child .day-th { border-top: 0; }
.day-tag {
  position: sticky;            /* не уезжает при горизонтальной прокрутке */
  left: 6px;
  display: inline-block;
  padding: 6px 10px;
  font-size: .84rem;
  font-weight: 700;
  color: var(--text);
}
.day-full { margin-left: 6px; font-weight: 500; color: var(--muted); }
.day-sep { margin: 0 6px; color: var(--muted); }
.day-date { color: var(--muted); font-weight: 600; }
.day-row.today .day-th { background: var(--accent-soft); }
.day-row.today .day-tag { color: var(--accent-text); }
.day-row.today .day-full,
.day-row.today .day-sep,
.day-row.today .day-date { color: var(--accent-text); opacity: .75; }

/* ячейка пары: номер и вертикальное время */
tbody th.pair-col {
  padding: 8px 0;
  text-align: center;
  border-right: 2px solid var(--border-strong);
}
tbody th.pair-col .num { display: block; font-size: 1rem; font-weight: 700; line-height: 1.1; }
tbody th.pair-col .time {
  display: inline-block;
  margin-top: 4px;
  font-size: .68rem;
  line-height: 1;
  white-space: nowrap;
  color: var(--muted);
  /* время повёрнуто на 90° против часовой — читается снизу вверх */
  writing-mode: vertical-rl;
  transform: rotate(180deg);
}
/* в колонке показываем только начало пары: полный интервал лежит в title */
tbody th.pair-col .t-sep,
tbody th.pair-col .t-end { display: none; }

table.tt td {
  padding: 4px;
  background: var(--card);
}
table.tt td.empty {
  background: repeating-linear-gradient(45deg, transparent, transparent 7px,
    color-mix(in srgb, var(--border) 30%, transparent) 7px, color-mix(in srgb, var(--border) 30%, transparent) 8px);
}

/* ---------- Карточка занятия ---------- */
/* Ячейка и её внутренние ряды тянутся на всю высоту строки, чтобы карточки
 * были одинаковыми по высоте и занимали клетку целиком. */
.cell { display: flex; flex-direction: column; gap: 5px; height: 100%; }

/* Подгруппы одной группы стоят в ряд: общая пара занимает обе дорожки. */
.subrow {
  display: grid;
  grid-template-columns: repeat(var(--slots, 1), minmax(0, 1fr));
  gap: 5px;
  flex: 1 1 auto;
}
.subrow > * { grid-row: 1; min-width: 0; }
/* Карточка тянется на всю высоту дорожки: пустого места под коротким
 * занятием не остаётся, все «таблетки» в строке одинаковой высоты. */
.slot-empty {
  display: flex;
  align-items: center;
  justify-content: center;
  min-height: 40px;
  border: 1px dashed var(--border-strong);
  border-radius: var(--radius-sm);
  background: color-mix(in srgb, var(--border) 12%, transparent);
  color: var(--muted);
  font-size: .72rem;
  font-weight: 600;
  user-select: none;
}

.lesson {
  --c: var(--accent);
  display: flex;
  flex-direction: column;
  justify-content: center;
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  background: var(--bg-soft);
  padding: 6px 8px;
  text-align: center;
  transition: box-shadow .15s;
}
.lesson:hover { box-shadow: 0 3px 10px rgba(15, 23, 42, .12); }
.lesson .subject { font-size: .82rem; font-weight: 600; line-height: 1.25; overflow-wrap: break-word; }
.lesson .meta { margin-top: 3px; font-size: .74rem; color: var(--muted); }

/* Сокращённое название: пунктир подсказывает, что карточка нажимаемая,
 * по клику/Enter название раскрывается полностью. */
.lesson.is-short { cursor: pointer; }
.lesson.is-short .subject {
  text-decoration: underline dotted;
  text-decoration-color: color-mix(in srgb, var(--muted) 55%, transparent);
  text-underline-offset: 2px;
}
.lesson.is-short:hover .subject { text-decoration-style: solid; }
.lesson.is-short:focus-visible { outline: 2px solid var(--accent); outline-offset: 2px; }
.lesson.is-open .subject { text-decoration: none; font-weight: 700; }

@media (max-width: 640px) {
  /* На телефоне всё то же самое, только компактнее: «Пара» уже,
   * шрифты мельче, у дня нет полного названия (его ширину считает app.js). */
  :root { --pair-w: 30px; }

  /* fixed-раскладка в Chrome работает только при заданной ширине таблицы;
   * без этого столбцы растягиваются по content и группа не влезает в экран */
  table.tt { table-layout: fixed; width: max-content; }

  /* ---------- фильтры: две компактные строки ---------- */
  .filters { padding: 6px 10px; gap: 6px 8px; }
  .field-label { display: none; }
  .field { flex: 1 1 40%; }
  select {
    min-width: 0; width: 100%;
    padding: 7px 26px 7px 9px;
    font-size: .88rem;
    background-position: right 8px center;
  }
  .combo { width: 100%; }
  .combo input { width: 100%; padding: 7px 26px 7px 9px; font-size: .88rem; }
  .filters .btn { padding: 7px 10px; font-size: .92rem; line-height: 1.2; }
  .filters .btn .btn-text { display: none; }

  /* ---------- «Пара»: совсем узкая ---------- */
  :root { --pair-w: 30px; }
  thead .lbl { font-size: .68rem; letter-spacing: 0; }
  tbody th.pair-col .num { font-size: .9rem; }
  tbody th.pair-col .time { margin-top: 3px; font-size: .62rem; }

  /* ---------- день тонкой строкой ---------- */
  .day-full { display: none; }          /* остаётся только «Пн · 16.11» */
  .day-tag { padding: 5px 8px; font-size: .76rem; }

  /* ---------- колонка группы и карточки ---------- */
  .grp-col { padding: 6px 8px; }
  .grp-code { font-size: .9rem; }
  .grp-year { font-size: .64rem; }
  table.tt thead th { font-size: .78rem; }
  table.tt td { padding: 3px; }
  .lesson { padding: 5px 6px; overflow-wrap: anywhere; }
  .lesson .subject { font-size: .76rem; overflow-wrap: anywhere; }
  .lesson .meta { font-size: .66rem; margin-top: 2px; overflow-wrap: anywhere; }
  .slot-empty { min-height: 32px; font-size: .62rem; }
  .subrow { gap: 3px; }
  .table-wrap { max-height: none; }
}

@media print {
  .filters { display: none; }
  body { background: #fff; }
  .table-wrap { width: auto; max-width: none; box-shadow: none; max-height: none; overflow: visible; border: 0; }
  table.tt { width: 100%; }
  table.tt thead th, .pair-col, .day-row .day-th { background: #f2f2f2 !important; position: static; }
  .day-tag { position: static; }
  .grp-col { max-width: none; }
}
