/* Шкала суток для timeline.js. Цвета — токены из app.css (тёмная и светлая темы):
   --ds-* для самой шкалы, --z-* для зон, общие --border, --text, --text-soft.
   По умолчанию (экран от 1100 px) — вертикальная колонка слева от кнопки;
   уже 1100 px — горизонтальная полоса (media query внизу). */

.day-scale {
  box-sizing: border-box;
  width: 300px;
  padding: 14px;
  background: var(--metal-card);
  border: 1px solid var(--metal-card-edge);
  border-radius: 16px;
  box-shadow: inset 0 1px 0 var(--metal-card-glint), var(--metal-card-shadow);
  color: var(--text);
  font-family: inherit;
}

.day-scale *,
.day-scale *::before,
.day-scale *::after {
  box-sizing: inherit;
}

.day-scale__title {
  margin: 0 0 8px;
  font-size: 14px;
  font-weight: 700;
  color: var(--text);
}

.day-scale__rows {
  display: block;
}

/* --- Строка (вертикальная шкала, широкий экран) --- */

.ds-row {
  position: relative;
  display: grid;
  grid-template-columns: 44px 1fr;
  align-items: stretch;
  width: 100%;
  height: 12px;
  margin: 0 0 1px;
  padding: 0;
  border: 0;
  background: none;
  font: inherit;
  color: inherit;
  cursor: pointer;
  overflow: visible;
  -webkit-tap-highlight-color: transparent;
}

.ds-row__label {
  position: relative;
  padding-right: 6px;
  font-size: 12px;
  font-weight: 700;
  line-height: 12px;
  color: var(--text-soft);
  text-align: right;
  white-space: nowrap;
  overflow: hidden;
}

/* Засечка у получасовых строк */
.ds-row--half .ds-row__label::after {
  content: "";
  position: absolute;
  right: 6px;
  top: 50%;
  width: 8px;
  border-top: 1px solid var(--border);
}

.ds-row__track {
  position: relative;
  display: flex;
  overflow: hidden;
  background: var(--ds-track);
  border-radius: 3px;
}

/* Будущее и время без данных: без заливки, тонкая пунктирная рамка.
   Цвета на шкале — только работа, плановый, внеплановый и авария (владелец, 01.10.2026) */
.ds-row--future .ds-row__track,
.ds-row--nodata .ds-row__track {
  background: transparent;
  border: 1px dashed var(--ds-future-border);
}

.ds-row--future .ds-row__label {
  color: var(--ds-faint);
}

/* Части строки: размер задан скриптом через flex-basis,
   поэтому в строке это ширина, а в горизонтальном режиме — высота. */
.ds-seg {
  flex: 0 0 auto;
  height: 100%;
}

.z-work { background: var(--z-work); }           /* работает — зелёный */
.z-plan { background: var(--z-plan); }           /* плановый простой: синий в обеих темах */
.z-unplanned { background: var(--z-unplanned); } /* внеплановый — жёлтый */
.z-failure { background: var(--z-failure); }     /* авария — красный */
.z-nodata { background: transparent; }           /* нет данных — без заливки */

/* Рамка перевалки на шкале (внутрь, размер части не меняется); в тёмной теме прозрачная */
.ds-seg.z-plan {
  box-shadow: inset 0 0 0 1px var(--z-plan-edge);
}

/* Текущая ячейка: рамка, черта и плашка времени */
.ds-row--now .ds-row__track {
  outline: 2px solid var(--ds-now);
  outline-offset: 0;
}

.ds-row--now .ds-row__label {
  visibility: hidden;
}

/* Черта «сейчас»: поперёк полосы, доля от начала ячейки — в --nowpos */
.ds-nowline {
  position: absolute;
  left: 0;
  right: 0;
  top: var(--nowpos);
  height: 2px;
  margin-top: -1px;
  background: var(--ds-now);
  box-shadow: var(--ds-now-shadow);
  z-index: 2;
  pointer-events: none;
}

.ds-nowbadge {
  position: absolute;
  left: 0;
  top: var(--nowpos);
  transform: translateY(-50%);
  z-index: 3;
  pointer-events: none;
  padding: 2px 4px;
  font-size: 10px;
  font-weight: 700;
  line-height: 1;
  color: var(--ds-now-text);
  background: var(--ds-now);
  border-radius: 3px;
  white-space: nowrap;
}
/* Бейдж целиком занимает ячейку подписи и не перекрывает соседние часы. */
@media (min-width: 1100px) {
  .ds-row--now { min-height: 16px; }
  .ds-row--now .ds-row__label { visibility: hidden; }
  .ds-nowbadge { top: 0; bottom: 0; width: 44px; transform: none;
    display: grid; place-items: center; padding: 0 3px; }
}

/* Выбранная строка */
.ds-row--sel .ds-row__track {
  outline: 2px solid var(--ds-sel);
  outline-offset: 0;
}

/* Заголовок смены: полоса чуть светлее фона карточки */
.ds-shift {
  display: flex;
  align-items: baseline;
  gap: 6px;
  margin: 2px 0 4px;
  padding: 3px 8px;
  background: var(--ds-shift-bg);
  border-radius: 4px;
  font-size: 13px;
  font-weight: 700;
  color: var(--text-soft);
}

.ds-shift--active {
  background: var(--ds-shift-active-bg);
  color: var(--ds-shift-active-text);
}

.ds-shift__times {
  font-weight: 600;
  opacity: 0.85;
}

/* Подробности по нажатию */
.day-scale__details {
  margin-top: 8px;
  padding: 8px 10px;
  font-size: 12px;
  line-height: 1.35;
  color: var(--text);
  background: var(--ds-details-bg);
  border: 1px solid var(--border);
  border-radius: 6px;
}

/* Легенда: «За смену» и четыре строки с минутами справа */
.day-scale__legend {
  display: grid;
  grid-template-columns: 1fr;
  gap: 4px;
  margin-top: 8px;
}

.day-scale__legend-title {
  grid-column: 1 / -1;
  font-size: 12px;
  font-weight: 700;
  color: var(--text-soft);
}

.ds-legend__item {
  display: flex;
  align-items: center;
  gap: 6px;
  min-width: 0;
  font-size: 13px;
  line-height: 1.3;
  color: var(--ds-legend-text);
}

.ds-legend__item--zero {
  opacity: 1;
}

.ds-legend__chip {
  flex: 0 0 auto;
  width: 14px;
  height: 14px;
  border-radius: 3px;
  border: 1px solid var(--border);
}

/* Квадрат перевалки в легенде: в светлой теме светло-серый с заметной рамкой, как на шкале */
.ds-legend__chip.z-plan {
  border-color: var(--z-plan-chip-edge);
}

.ds-legend__val {
  margin-left: auto;
  font-weight: 700;
  color: var(--text);
}

/* --- Горизонтальная шкала на экране уже 1100 px: планшет стоя и телефон --- */

@media (max-width: 1099px) {
  .day-scale {
    width: 100%;
  }

  /* Высота столбиков около 200 px + место сверху под плашку времени
     и заголовки смен, снизу — под подписи часов */
  .day-scale__rows {
    display: flex;
    align-items: stretch;
    gap: 1px;
    height: 252px;
    padding-top: 36px;
  }

  .ds-row {
    display: flex;
    flex-direction: column;
    flex: 1 1 0;
    width: auto;
    min-width: 0;
    height: auto;
    margin: 0;
  }

  .ds-row__track {
    flex: 1 1 auto;
    flex-direction: column;
  }

  .ds-seg {
    width: 100%;
    height: auto;
  }

  /* Подписи шире узкого столбика — выпускаем их наружу */
  .ds-row__label {
    order: 2;
    flex: 0 0 auto;
    min-height: 16px;
    padding: 2px 0 0;
    font-size: 12px;
    line-height: 14px;
    text-align: left;
    overflow: visible;
    visibility: hidden;
  }

  .ds-row--half .ds-row__label::after {
    content: none;
  }

  /* В горизонтали подписи каждые 4 часа */
  .ds-row--tick4 .ds-row__label {
    visibility: visible;
  }

  /* Черта «сейчас» — вертикальная, плашка — над столбиком */
  .ds-nowline {
    top: 0;
    bottom: 0;
    left: var(--nowpos);
    right: auto;
    width: 2px;
    height: auto;
    margin-top: 0;
  }

  .ds-nowbadge {
    top: -18px;
    left: 50%;
    transform: translateX(-50%);
  }

  /* Заголовок смены — над столбиками, коротко; черта — вертикальная */
  .ds-shift {
    position: relative;
    flex: 0 0 auto;
    width: 0;
    margin: 0;
    padding: 0;
    background: none;
    overflow: visible;
  }

  .ds-shift::before {
    content: "";
    position: absolute;
    left: -1px;
    top: 0;
    bottom: 0;
    border-left: 2px dashed var(--ds-faint);
  }

  .ds-shift__name {
    position: absolute;
    top: -33px;
    left: 3px;
    font-size: 11px;
    font-weight: 700;
    color: var(--text-soft);
    white-space: nowrap;
  }

  .ds-shift--active .ds-shift__name {
    color: var(--ds-shift-active-text);
  }

  .ds-shift__times {
    display: none;
  }
}

/* Легенда на телефоне — в два столбца */
@media (max-width: 480px) {
  .day-scale__legend {
    grid-template-columns: repeat(2, minmax(0, 1fr));
    gap: 4px 12px;
  }
}

/* --- Главный экран со шкалой (подключает app.js) --- */
/* Широкий экран: шкала колонкой слева от главной кнопки */
.with-scale {
  display: grid;
  grid-template-columns: 300px minmax(0, 1fr);
  gap: 24px;
  align-items: start;
}
.with-scale__main { min-width: 0; }
:root { --screen-gutter: 24px; }
/* Главный экран — во всю ширину окна на мониторе, планшете и телефоне (владелец, 01.10.2026); отступы — --screen-gutter */
.topbar, main:has(.with-scale), main:has(.with-scale .stop-grid) { max-width: none; padding-inline: var(--screen-gutter); }


/* Узкий экран: шкала полосой под главными кнопками, чтобы не отодвигать их вниз */
@media (max-width: 1099px) {
  .with-scale { grid-template-columns: minmax(0, 1fr); }
  .with-scale > .day-scale { order: 2; }
}

.day-scale__title { display: flex; align-items: center; gap: 7px; margin-bottom: 12px; }
.day-scale__title .ico { width: 18px; height: 18px; }
.ds-shift__name { display: inline-flex; align-items: center; gap: 5px; }
.ds-shift__name .ico { width: 16px; height: 16px; }
.ds-shift { align-items: center; padding-block: 6px; }
.day-scale__legend { border-top: 1px solid var(--metal-card-edge); padding-top: 10px; margin-top: 12px; gap: 6px; }
@media (max-width: 1099px) {
  .ds-row { height: auto; margin: 0; }
  .ds-shift { padding: 0; }
  :root { --screen-gutter: 16px; }
}
@media (max-width: 480px) {
  :root { --screen-gutter: 12px; }
  .ds-legend__item { font-size: 11px; }
}
@media (min-width: 1100px) and (max-height: 950px) {
  .ds-row { height: 10px; }
  .ds-row__label { font-size: 11px; line-height: 10px; }
}

/* --- Монитор: главный экран целиком в высоту окна, без прокрутки (владелец, 01.10.2026) ---
   Шкала сжимает 48 строк под высоту, пульт забирает остаток справа, клавиши считаются
   от высоты пульта (cqh), три кнопки смены — в один ряд. Планшет и телефон — как раньше. */
@media (min-width: 1100px) and (min-height: 600px) and (orientation: landscape) {
  body:has(#main .with-scale) { display: flex; flex-direction: column; height: 100dvh; overflow: hidden; }
  body:has(#main .with-scale) > .topbar { flex: none; width: 100%; box-sizing: border-box; padding-block: 12px; }
  body:has(#main .with-scale) > #main { flex: 1 1 auto; min-height: 0; width: 100%; box-sizing: border-box;
    padding-top: 4px; padding-bottom: 14px; overflow: auto; }
  /* Отказы находятся в панели поверх экрана и не уменьшают пульт. */
  .with-scale { height: 100%; align-items: stretch; gap: 18px; }

  /* Шкала: строки делят высоту поровну */
  .with-scale > .day-scale { display: flex; flex-direction: column; height: 100%; min-height: 0; overflow: hidden; padding: 12px; }
  .with-scale > .day-scale > :not(.day-scale__rows) { flex: none; }
  .day-scale__title { margin-bottom: 6px; }
  .day-scale__rows { flex: 1 1 auto; min-height: 0; display: flex; flex-direction: column; }
  .day-scale__rows > .ds-shift { flex: none; margin: 1px 0 2px; padding-block: 3px; }
  .day-scale__rows > .ds-row { flex: 1 1 0; height: auto; min-height: 5px; max-height: 16px; margin-bottom: 1px; }
  .day-scale__legend { margin-top: 6px; padding-top: 6px; gap: 2px; }

  /* Правая колонка: состояние, пульт (растягивается), смена */
  .with-scale__main { display: flex; flex-direction: column; gap: 14px; height: 100%; min-height: 0; }
  .with-scale__main > .mill-console, .with-scale__main > div:has(> .mill-console) { flex: 1 1 auto; min-height: 0; display: flex; flex-direction: column; }
  .with-scale__main > .stop-grid { flex: 1 1 auto; min-height: 0; display: grid; grid-template-columns: minmax(0, 1fr) 260px;
    gap: 14px; align-items: stretch; }
  .stop-grid > :first-child { display: flex; flex-direction: column; min-height: 0; }
  .stop-grid > :first-child > .mill-console { flex: 1 1 auto; min-height: 0; display: flex; flex-direction: column; }
  .stop-grid > :last-child { min-height: 0; overflow: auto; }
  .mill-status { flex: none; min-height: 0; padding-block: 12px; margin-bottom: 12px; }
  .mill-panel { flex: 1 1 auto; min-height: 0; container-type: size; grid-template-rows: minmax(0, 1fr) auto;
    padding: 18px 18px 12px; gap: 10px 0; }
  /* Табличка сбоку от клавиши: так клавиша получает почти всю высоту пульта */
  .mill-btn { min-height: 0; height: 100%; flex-direction: row; justify-content: center; align-items: center; gap: 14px; padding: 0 10px; }
  /* Клавиша 4:3 по высоте пульта (минус подсказка и отступы ~70 px), но не шире 58% своей половины */
  .mill-lamp { width: min(58%, 560px, max(96px, calc((100cqh - 70px) * 4 / 3))); max-width: none; flex: none; }
  .mill-label { width: auto; max-width: 42%; flex: 0 1 auto; padding: 8px 12px; text-align: left; font-size: clamp(12px, 1.9cqi, 28px); letter-spacing: .02em; }

  /* Смена: карточка мастера во всю ширину, кнопки — в ряд */
  /* Кнопки смены делят ширину поровну: три, а с «Продолжить заполнение» — четыре */
  .shift-block { flex: none; margin-top: 0; gap: 12px; grid-template-columns: repeat(3, minmax(0, 1fr)); }
  .shift-block:has(> .shift-action:nth-child(5)) { grid-template-columns: repeat(4, minmax(0, 1fr)); }
  .shift-block > .shift-card { grid-column: 1 / -1; padding: 12px 18px 10px; }
  .shift-person { grid-template-columns: 56px minmax(0, 1fr); gap: 14px; }
  .shift-avatar { width: 56px; height: 60px; }
  .shift-avatar .ico { width: 36px; height: 36px; }
  .shift-person h2 { font-size: 20px; }
  .shift-person p { margin: 1px 0; }
  .shift-block .shift-time { margin-top: 8px; padding-block: 7px; }
  .btn.shift-action { min-height: 0; padding: 10px 14px; gap: 12px; grid-template-columns: 40px minmax(0, 1fr) 18px; font-size: 17px; }
  .action-ico { width: 40px; height: 40px; }
  .action-ico .ico { width: 22px; height: 22px; }
  .shift-action .action-help { font-size: 12.5px; margin-top: 2px; }
}
/* Низкий монитор (ноутбук 1366×768 и т.п.): убираем второстепенные строки, чтобы клавиши не мельчали */
@media (min-width: 1100px) and (min-height: 600px) and (orientation: landscape) and (max-height: 760px) {
  .mill-status { padding-block: 8px; margin-bottom: 8px; }
  .mill-status .mill-subtitle, .mill-state > .ico, .mill-clock > .ico { display: none; }
  .shift-avatar, .shift-person p.muted, .shift-action .action-help { display: none; }
  .shift-person { grid-template-columns: minmax(0, 1fr); }
  .shift-person h2 { font-size: 18px; margin: 0; }
  .shift-block .shift-time { margin-top: 6px; padding-block: 5px; }
  .btn.shift-action { padding-block: 8px; }
  .action-ico { width: 32px; height: 32px; }
  .btn.shift-action { grid-template-columns: 32px minmax(0, 1fr) 16px; font-size: 16px; }
  .mill-panel { padding: 12px 14px 8px; }
}
/* Монитор: клавиша заполняет всю половину пульта по ширине и высоте, табличка — под ней */
@media (min-width: 1100px) and (min-height: 600px) and (orientation: landscape) {
  .mill-panel { padding: 16px 16px 10px; gap: 10px 0; }
  .mill-btn { flex-direction: column; align-items: stretch; justify-content: stretch; gap: 10px; padding: 0 12px; }
  .mill-lamp { flex: 1 1 auto; width: 100%; height: auto; min-height: 0; max-width: none; aspect-ratio: auto; }
  .mill-label { flex: none; width: 100%; max-width: none; text-align: center; padding: 6px 10px; font-size: clamp(16px, 2.4cqi, 32px); }
}
/* Вертикальный монитор: клавиша не вытягивается в колонну — высота не больше 0,8 ширины половины пульта */
@media (min-width: 1100px) and (min-height: 600px) and (orientation: landscape) {
  .mill-btn { justify-content: center; }
  .mill-lamp { flex: 0 1 auto; height: 100%; max-height: calc((50cqi - 40px) * 0.8); }
}

/* Шапка сжимается вместе с окном, табличка использует ширину своей клавиши. */
.topbar { width: 100%; box-sizing: border-box; min-width: 0; }
.brand { flex: 0 1 auto; min-width: 0; }
.brand strong, .brand span { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.mill-btn { min-width: 0; container-type: inline-size; }
.mill-label { box-sizing: border-box; max-width: 100%; font-size: clamp(10px, 7cqi, 32px); overflow-wrap: anywhere; }
.current-shift { outline: 2px solid var(--focus); outline-offset: -2px; }
@media (min-width: 1100px) and (min-height: 600px) and (orientation: landscape) {
  .mill-lamp { max-height: 80cqi; }
}
@media (max-width: 820px) { .brand { display: none; } }
@media (max-width: 380px) {
  .topbar .pill .pill-text { position: absolute; width: 1px; height: 1px; overflow: hidden; clip-path: inset(50%); }
  .topbar .save-status { overflow-wrap: anywhere; }
}

/* Отказы раскрываются из статуса связи поверх страницы. Высота пульта неизменна. */
#rejects { position: fixed; z-index: 30; inset: 76px var(--screen-gutter) 16px auto;
  width: min(620px, calc(100vw - 2 * var(--screen-gutter))); box-sizing: border-box;
  max-height: none; margin: 0; padding: 16px; overflow: auto; overscroll-behavior: contain;
  background: var(--bg); border: 2px solid var(--text-attn); border-radius: 14px; box-shadow: var(--shadow-toast); }
#rejects:not(.is-open) { display: none; }
.rejects-heading { display: flex; align-items: center; justify-content: space-between; gap: 12px; }
.rejects-heading .btn { width: auto; font-size: 15px; margin: 0; }
.saved-answers p { margin: 4px 0; }
.saved-answers, .transfer, .card-note, .card-line, .handover-note, h2, .action-help, .seg, .earlier-text { overflow-wrap: anywhere; }
.note-preview { display: -webkit-box; -webkit-box-orient: vertical; -webkit-line-clamp: 3; overflow: hidden; }
.handover-preview[open] .note-preview { -webkit-line-clamp: 1; }
.handover-preview summary { cursor: pointer; }
.topbar { background: var(--metal-page); background-attachment: fixed; }
.topbar::before { display: none; }
.topbar .save-status, .topbar .save-status:not(.attention) { flex: 0 1 250px; justify-content: flex-start;
  min-height: 44px; padding: 6px; border: 0; background: transparent; color: var(--text-soft); cursor: pointer; text-align: left; }
.save-status .save-status-text, .save-status:not(.attention) .save-status-text {
  position: static; width: auto; height: auto; margin: 0; overflow: visible; clip: auto; clip-path: none; white-space: normal; }
.save-status-count { display: none; font-weight: 800; }
.save-status.online::before { background: #22863a; box-shadow: none; }
.save-status.waiting::before { background: #bd7600; box-shadow: none; }
.save-status.offline::before { background: #cf222e; box-shadow: none; }
.topbar .pill, .theme-toggle { min-width: 44px; min-height: 44px; box-sizing: border-box; }
.tile-off { opacity: 1; color: var(--text-muted); }
.ds-legend__item--zero { opacity: 1; }
.toast { pointer-events: none; }
.toast.warning { color: var(--text); background: var(--bg); border: 2px solid var(--text-attn); }
.m-track { display: flex; }
.chart .nodata, .chart-legend .nodata::before { fill: none; background: transparent; stroke-dasharray: 4 3; }
@media (max-width: 600px) {
  .topbar .pill .pill-text { position: absolute; width: 1px; height: 1px; overflow: hidden; clip-path: inset(50%); }
  .topbar .save-status, .topbar .save-status:not(.attention) { flex: 0 0 auto; min-width: 44px; min-height: 44px; justify-content: center; margin-left: auto; }
  .save-status .save-status-text, .save-status:not(.attention) .save-status-text { position: absolute; width: 1px; height: 1px; overflow: hidden; clip-path: inset(50%); }
  .save-status-count { display: inline; }
  .toast { position: sticky; top: 68px; bottom: auto; left: auto; right: auto; transform: none; width: 100%; max-width: none;
    margin: 0; padding: 10px 12px; box-sizing: border-box; border-radius: 0; font-size: 14px; }
  #rejects { inset: 72px 8px 8px; width: auto; padding: 12px; }
}
/* Часы могут быть выше своей получасовой полоски. Не срезаем буквы строкой. */
@media (min-width: 1100px) {
  .ds-row { overflow: visible; }
  .ds-row__label { height: 14px; align-self: center; line-height: 14px; position: relative; z-index: 1; overflow: visible; }
  .ds-row--tick .ds-row__label { transform: translateY(-1px); }
}

.stop-grid > :last-child .card > .btn { min-height: 44px; padding: 8px; font-size: 16px; }
.stop-grid > :last-child .card-title { font-size: 20px; }
.stop-grid > :last-child .card-note { font-size: 16px; }
