:root {
  --bg: #f4f5f7;
  --card: #ffffff;
  --text: #14161a;
  --muted: #6b7280;
  --line: #e3e5ea;
  --accent: #f5a623;
  --accent-text: #3a2a05;
  --danger: #d1453b;
  --ok: #3f9142;
  --shadow: 0 1px 2px rgba(15, 17, 21, 0.06), 0 4px 14px rgba(15, 17, 21, 0.05);
  color-scheme: light dark;
}

@media (prefers-color-scheme: dark) {
  :root {
    --bg: #0f1115;
    --card: #181b21;
    --text: #e8eaee;
    --muted: #9aa1ad;
    --line: #262a32;
    --accent: #f5a623;
    --accent-text: #2a1d04;
    --danger: #ef6259;
    --ok: #5bbf5f;
    --shadow: none;
  }
}

* {
  box-sizing: border-box;
}

/* display у классов перебивает атрибут hidden, поэтому правило явное */
[hidden] {
  display: none !important;
}

body {
  margin: 0;
  padding: 0 16px calc(32px + env(safe-area-inset-bottom));
  background: var(--bg);
  color: var(--text);
  font: 16px/1.4 system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
  -webkit-tap-highlight-color: transparent;
}

.top {
  display: flex;
  align-items: baseline;
  gap: 12px;
  padding: calc(16px + env(safe-area-inset-top)) 0 8px;
  position: sticky;
  top: 0;
  background: var(--bg);
  z-index: 2;
  max-width: 720px;
  margin: 0 auto 8px;
}

h1 {
  font-size: 20px;
  margin: 0;
  letter-spacing: -0.01em;
  flex: none;
}

.status {
  font: inherit;
  font-size: 13px;
  color: var(--muted);
  margin-left: auto;
  display: flex;
  align-items: center;
  gap: 6px;
  background: none;
  border: 0;
  padding: 4px 2px;
  cursor: pointer;
  min-width: 0;
  text-align: right;
}

/* Причина может быть длинной — в шапке показываем одну строку */
.status span:last-child {
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.status .dot {
  width: 8px;
  height: 8px;
  border-radius: 50%;
  background: var(--ok);
  flex: none;
}

.status[data-status="degraded"] {
  color: var(--accent);
}

.status[data-status="degraded"] .dot {
  background: var(--accent);
}

.status[data-status="down"] {
  color: var(--danger);
}

.status[data-status="down"] .dot {
  background: var(--danger);
}

.icon-button {
  flex: none;
  background: none;
  border: 0;
  color: var(--muted);
  font-size: 18px;
  line-height: 1;
  padding: 4px 2px;
  cursor: pointer;
}

.icon-button[aria-expanded="true"] {
  color: var(--accent);
}

.admin {
  max-width: 720px;
  margin: 0 auto 16px;
  background: var(--card);
  border: 1px solid var(--line);
  border-radius: 14px;
  padding: 14px;
  box-shadow: var(--shadow);
  display: flex;
  flex-direction: column;
  gap: 12px;
}

.admin h3 {
  margin: 0;
  font-size: 12px;
  text-transform: uppercase;
  letter-spacing: 0.05em;
  color: var(--muted);
}

.admin-row {
  display: flex;
  align-items: center;
  gap: 8px;
  font-size: 14px;
  padding: 6px 0;
  border-bottom: 1px solid var(--line);
}

.admin-row:last-of-type {
  border-bottom: 0;
}

.admin-row .grow {
  flex: 1;
  min-width: 0;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.admin-row .muted {
  color: var(--muted);
  font-size: 12px;
}

.admin button,
.admin input,
.admin select {
  font: inherit;
  font-size: 14px;
  padding: 7px 10px;
  border-radius: 9px;
  border: 1px solid var(--line);
  background: var(--bg);
  color: var(--text);
}

.admin button {
  cursor: pointer;
}

.admin button.primary {
  background: var(--accent);
  color: var(--accent-text);
  border-color: var(--accent);
  font-weight: 600;
}

.admin button.danger {
  color: var(--danger);
}

.admin-form {
  display: flex;
  flex-direction: column;
  gap: 10px;
  border-top: 1px solid var(--line);
  padding-top: 12px;
}

.field {
  display: flex;
  flex-direction: column;
  gap: 4px;
}

.field label {
  font-size: 12px;
  color: var(--muted);
}

.field .hint {
  font-size: 11px;
  color: var(--muted);
}

.tile-value {
  font-size: 34px;
  line-height: 1.1;
  font-variant-numeric: tabular-nums;
  letter-spacing: -0.02em;
}

/* Раскладка «список значений»: подпись слева, значение справа */
.metrics.rows {
  flex-direction: column;
  gap: 4px;
}

.metrics.rows .metric {
  flex-direction: row-reverse;
  justify-content: space-between;
  align-items: baseline;
  gap: 12px;
}

.metrics.rows .metric-value {
  font-size: 16px;
}

/* Термостат на карточке: показания и пороги, которые крутят прямо здесь */
.control-box {
  display: flex;
  flex-direction: column;
  gap: 6px;
  margin-top: 4px;
}

.control-row {
  display: flex;
  align-items: center;
  gap: 8px;
}

/* Датчики термостата: кто что показывает и когда отчитывался */
.sensors {
  display: flex;
  flex-direction: column;
  gap: 2px;
  margin-bottom: 2px;
}

.sensor {
  display: flex;
  align-items: baseline;
  gap: 8px;
  font-size: 13px;
}

.sensor-name {
  flex: 1;
  color: var(--muted);
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.sensor.leading .sensor-name {
  color: var(--text);
}

.sensor-value {
  font-variant-numeric: tabular-nums;
}

.sensor-age {
  min-width: 92px;
  text-align: right;
  font-size: 12px;
  color: var(--muted);
}

.sensor-age.warn {
  color: var(--danger);
}

.dot {
  width: 7px;
  height: 7px;
  border-radius: 50%;
  flex: none;
  align-self: center;
}

.dot.ok {
  background: var(--ok);
}

.dot.bad {
  background: var(--danger);
}

.control-label {
  flex: 1;
  font-size: 13px;
  color: var(--muted);
}

input.control-value {
  width: 68px;
  padding: 6px 8px;
  text-align: center;
  font-size: 15px;
  font-variant-numeric: tabular-nums;
  -moz-appearance: textfield;
}

/* Штатные стрелки браузера тут лишние: рядом свои кнопки «−» и «+» */
input.control-value::-webkit-outer-spin-button,
input.control-value::-webkit-inner-spin-button {
  -webkit-appearance: none;
  margin: 0;
}

.control-unit {
  font-size: 13px;
  color: var(--muted);
}

.sensor-order {
  display: flex;
  gap: 2px;
}

/* Цвет лампы: две шкалы, образец и готовые цвета */
.color-sample {
  width: 28px;
  height: 20px;
  border-radius: 6px;
  border: 1px solid var(--line);
  flex: none;
}

input.color-scale {
  width: 100%;
  height: 22px;
  padding: 0;
  border-radius: 6px;
  border: 1px solid var(--line);
  appearance: none;
  cursor: pointer;
}

input.color-scale.hue {
  background: linear-gradient(
    to right,
    #ff0000,
    #ffff00,
    #00ff00,
    #00ffff,
    #0000ff,
    #ff00ff,
    #ff0000
  );
}

input.color-scale::-webkit-slider-thumb {
  appearance: none;
  width: 14px;
  height: 20px;
  border-radius: 4px;
  background: var(--card);
  border: 2px solid var(--text);
}

input.color-scale::-moz-range-thumb {
  width: 14px;
  height: 20px;
  border-radius: 4px;
  background: var(--card);
  border: 2px solid var(--text);
}

.swatches {
  display: flex;
  gap: 6px;
}

button.swatch {
  width: 26px;
  height: 26px;
  padding: 0;
  border-radius: 50%;
  border: 1px solid var(--line);
  cursor: pointer;
}

/* Подпись под целью: откуда она взялась и кнопка к расписанию */
.control-note {
  display: flex;
  align-items: baseline;
  justify-content: space-between;
  gap: 8px;
  font-size: 12px;
}

button.link {
  background: none;
  border: 0;
  padding: 0;
  font-size: 12px;
  color: var(--accent);
  cursor: pointer;
  white-space: nowrap;
}

/* Редактор расписания прямо на карточке */
.schedule {
  display: flex;
  flex-direction: column;
  gap: 8px;
  border-top: 1px solid var(--line);
  padding-top: 8px;
  margin-top: 4px;
}

.schedule-point {
  display: flex;
  align-items: center;
  gap: 6px;
}

.schedule-point input[type='time'] {
  width: 104px;
}

.week {
  display: flex;
  flex-wrap: wrap;
  gap: 6px;
}

.week-day {
  display: flex;
  flex-direction: column;
  gap: 2px;
  font-size: 12px;
}

.week-day select {
  min-width: 104px;
}

/* Выбор группировки в шапке: рядом со статусом, но не спорит с ним за внимание */
.grouping {
  width: auto;
  padding: 4px 8px;
  font-size: 12px;
  color: var(--muted);
  background: transparent;
  border-color: transparent;
}

.grouping:hover,
.grouping:focus {
  color: var(--text);
  border-color: var(--line);
}

button.step.small {
  width: 24px;
  height: 24px;
  font-size: 13px;
  border-radius: 7px;
}

button.step.small:disabled {
  opacity: 0.35;
}

button.step {
  width: 34px;
  height: 34px;
  padding: 0;
  font-size: 18px;
  line-height: 1;
  border-radius: 9px;
}

/* Вложенный блок в форме типа: канал */
.sub {
  border: 1px solid var(--line);
  border-radius: 10px;
  padding: 10px;
  display: flex;
  flex-direction: column;
  gap: 8px;
}

.sub .admin-row {
  border-bottom: 0;
  padding: 0;
}

.checks {
  display: flex;
  flex-wrap: wrap;
  gap: 6px 14px;
}

.check {
  display: flex;
  align-items: center;
  gap: 6px;
  font-size: 13px;
}

.check input {
  width: 16px;
  height: 16px;
  accent-color: var(--accent);
  padding: 0;
}

.form-actions {
  display: flex;
  gap: 8px;
}

.form-error {
  margin: 0;
  color: var(--danger);
  font-size: 13px;
}

.system {
  max-width: 720px;
  margin: 0 auto 16px;
  background: var(--card);
  border: 1px solid var(--line);
  border-radius: 14px;
  padding: 14px;
  box-shadow: var(--shadow);
  font-size: 13px;
  display: flex;
  flex-direction: column;
  gap: 10px;
}

.system h3 {
  margin: 0;
  font-size: 12px;
  text-transform: uppercase;
  letter-spacing: 0.05em;
  color: var(--muted);
}

.system dl {
  margin: 0;
  display: grid;
  grid-template-columns: minmax(120px, auto) 1fr;
  gap: 4px 12px;
}

.system dt {
  color: var(--muted);
}

.system dd {
  margin: 0;
  font-variant-numeric: tabular-nums;
}

.system .problems {
  margin: 0;
  padding-left: 18px;
  color: var(--danger);
}

.system .problems.warn {
  color: var(--accent);
}

.rooms {
  display: flex;
  flex-direction: column;
  gap: 24px;
  max-width: 720px;
  margin: 0 auto;
}

.room-title {
  font-size: 14px;
  font-weight: 600;
  color: var(--muted);
  margin: 4px 0 8px;
}

.cards {
  display: grid;
  gap: 12px;
  grid-template-columns: repeat(auto-fill, minmax(240px, 1fr));
}

.card {
  background: var(--card);
  border: 1px solid var(--line);
  border-radius: 14px;
  padding: 14px;
  box-shadow: var(--shadow);
  display: flex;
  flex-direction: column;
  gap: 10px;
}

.card.offline {
  opacity: 0.6;
}

.card-head {
  display: flex;
  align-items: flex-start;
  gap: 8px;
}

.card-title {
  font-weight: 600;
  line-height: 1.25;
}

.card-note {
  font-size: 12px;
  color: var(--muted);
  margin-top: 2px;
}

.card-note.warn {
  color: var(--danger);
}

.toggle {
  margin-left: auto;
  flex: none;
  width: 54px;
  height: 32px;
  border-radius: 999px;
  border: 1px solid var(--line);
  background: var(--bg);
  position: relative;
  cursor: pointer;
  transition: background 0.15s ease;
}

.toggle::after {
  content: "";
  position: absolute;
  top: 3px;
  left: 3px;
  width: 24px;
  height: 24px;
  border-radius: 50%;
  background: var(--muted);
  transition: transform 0.15s ease, background 0.15s ease;
}

.toggle[aria-pressed="true"] {
  background: var(--accent);
  border-color: var(--accent);
}

.toggle[aria-pressed="true"]::after {
  transform: translateX(22px);
  background: #fff;
}

.toggle[disabled] {
  cursor: default;
  opacity: 0.5;
}

.toggle.busy::after {
  background: var(--accent);
}

.metrics {
  display: flex;
  flex-wrap: wrap;
  gap: 14px;
}

.metric {
  display: flex;
  flex-direction: column;
}

.metric-value {
  font-size: 20px;
  font-variant-numeric: tabular-nums;
  letter-spacing: -0.01em;
}

.metric-label {
  font-size: 11px;
  color: var(--muted);
  text-transform: uppercase;
  letter-spacing: 0.04em;
}

.control {
  display: flex;
  align-items: center;
  gap: 10px;
}

.control label {
  font-size: 12px;
  color: var(--muted);
  min-width: 68px;
}

input[type="range"] {
  flex: 1;
  accent-color: var(--accent);
}

.buttons {
  display: flex;
  flex-wrap: wrap;
  gap: 8px;
}

/* Эффектов восемь — в строку они не встают, поэтому сеткой в несколько рядов */
.buttons.effects {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(84px, 1fr));
  gap: 6px;
}

.buttons.effects button {
  padding: 6px 4px;
  font-size: 12px;
  line-height: 1.2;
}

.buttons button {
  flex: 1 1 auto;
  padding: 8px;
  border-radius: 10px;
  border: 1px solid var(--line);
  background: var(--bg);
  color: var(--text);
  font-size: 14px;
  cursor: pointer;
}

.buttons button:active {
  background: var(--line);
}

.event {
  font-size: 12px;
  color: var(--muted);
}

/* Надзор за реле: что случилось в последний раз и журнал по требованию */
.supervision {
  margin-top: 6px;
  border-top: 1px solid var(--line);
  padding-top: 6px;
}

.journal summary {
  font-size: 12px;
  color: var(--muted);
  cursor: pointer;
  list-style: none;
  padding: 2px 0;
}

.journal summary::-webkit-details-marker {
  display: none;
}

.journal summary::before {
  content: '▸ ';
}

.journal[open] summary::before {
  content: '▾ ';
}

.journal-list {
  font-size: 12px;
  color: var(--muted);
  max-height: 180px;
  overflow-y: auto;
}

.journal-row {
  padding: 2px 0;
  border-bottom: 1px solid var(--line);
}

.journal-row:last-child {
  border-bottom: none;
}

.journal-row.warn {
  color: var(--danger);
}

.journal-actor {
  opacity: 0.7;
}

/* Шапка карточки: опрос и выключатель стоят рядом, прижатые вправо */
.card-actions {
  display: flex;
  align-items: center;
  gap: 8px;
  margin-left: auto;
  flex: none;
}

.card-actions .toggle {
  margin-left: 0;
}

.icon-button {
  width: 32px;
  height: 32px;
  padding: 0;
  border: 1px solid var(--line);
  border-radius: 50%;
  background: var(--card);
  color: var(--muted);
  font-size: 15px;
  line-height: 1;
  cursor: pointer;
}

.icon-button:hover {
  color: var(--text);
}

/* Подсказка рядом с целью: что случится с набранным значением */
.control-hint {
  font-size: 11px;
  color: var(--muted);
  margin-left: 8px;
}

/* Крестик рядом с заголовком: мельче круглой кнопки на карточке */
.icon-button.small {
  width: 22px;
  height: 22px;
  font-size: 13px;
  margin-left: 8px;
  vertical-align: middle;
}

.icon-button.busy {
  animation: spin 1s linear infinite;
}

@keyframes spin {
  to {
    transform: rotate(360deg);
  }
}

/* Мастер установки: те же поля, что и на входе, плюс блок брокера */
#setup-broker {
  display: contents;
}

#setup-form code {
  font-size: 12px;
  background: var(--bg);
  padding: 1px 4px;
  border-radius: 4px;
}

/* Пометка у отключённого объекта в списке настройки */
.tag.off {
  margin-left: 8px;
  padding: 1px 6px;
  border: 1px solid var(--line);
  border-radius: 10px;
  font-size: 11px;
  color: var(--muted);
  vertical-align: middle;
}

/* Сроки опроса: короткие числовые поля в одной строке с выключателем */
.admin-row input.narrow {
  width: 84px;
  flex: 0 0 auto;
}

.event.fresh {
  color: var(--accent);
}

.empty {
  text-align: center;
  color: var(--muted);
  margin-top: 40px;
}

/* Экран токена */
.gate {
  position: fixed;
  inset: 0;
  background: var(--bg);
  display: grid;
  place-items: center;
  padding: 24px;
  z-index: 10;
}

.gate form {
  width: min(340px, 100%);
  background: var(--card);
  border: 1px solid var(--line);
  border-radius: 14px;
  padding: 20px;
  display: flex;
  flex-direction: column;
  gap: 12px;
  box-shadow: var(--shadow);
}

.gate h2 {
  margin: 0;
  font-size: 18px;
}

.gate p {
  margin: 0;
  font-size: 13px;
  color: var(--muted);
}

.gate input,
.gate button {
  font-size: 16px;
  padding: 11px 12px;
  border-radius: 10px;
  border: 1px solid var(--line);
  background: var(--bg);
  color: var(--text);
}

.gate button {
  background: var(--accent);
  color: var(--accent-text);
  border-color: var(--accent);
  font-weight: 600;
  cursor: pointer;
}

.gate-error {
  color: var(--danger);
}

.toast {
  position: fixed;
  left: 50%;
  bottom: calc(20px + env(safe-area-inset-bottom));
  transform: translateX(-50%);
  background: var(--danger);
  color: #fff;
  padding: 10px 16px;
  border-radius: 10px;
  font-size: 14px;
  max-width: calc(100% - 32px);
  z-index: 20;
}
