:root {
  --bg: #0b1620;
  --bg-elevated: #101f2c;
  --card: #142633;
  --border: #223441;
  --text: #e9f1f5;
  --text-dim: #90a4b0;
  --accent: #2ea6b8;
  --accent-strong: #47c2d4;
  --ok: #3bb273;
  --warn: #e2a83b;
  --danger: #e2523b;
  --danger-bg: #3a1a15;
  --radius: 14px;
  color-scheme: dark;
}

@media (prefers-color-scheme: light) {
  :root {
    --bg: #f4f8fa;
    --bg-elevated: #ffffff;
    --card: #ffffff;
    --border: #dbe6ea;
    --text: #142633;
    --text-dim: #5a7180;
    --accent: #1c8a9a;
    --accent-strong: #147080;
    --ok: #2a9260;
    --warn: #b8791f;
    --danger: #c8402a;
    --danger-bg: #fbe7e3;
    color-scheme: light;
  }
}

* { box-sizing: border-box; }

html, body {
  margin: 0;
  padding: 0;
  height: 100%;
  background: var(--bg);
  color: var(--text);
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Helvetica, Arial, sans-serif;
  -webkit-tap-highlight-color: transparent;
}

body {
  display: flex;
  flex-direction: column;
  min-height: 100vh;
  padding-top: env(safe-area-inset-top);
  padding-bottom: env(safe-area-inset-bottom);
}

button {
  font-family: inherit;
}

.hidden { display: none !important; }

/* --- шапка --- */
.app-header {
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 10px 14px;
  background: var(--bg-elevated);
  border-bottom: 1px solid var(--border);
  position: sticky;
  top: 0;
  z-index: 10;
}
.app-header-title {
  flex: 1;
  font-size: 17px;
  font-weight: 600;
}
.nav-back, .nav-about {
  background: none;
  border: none;
  color: var(--text);
  font-size: 22px;
  width: 44px;
  height: 44px;
  border-radius: 50%;
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: center;
}
.nav-about {
  font-style: italic;
  font-family: Georgia, serif;
  font-size: 16px;
  border: 1px solid var(--border);
}
.nav-back:active, .nav-about:active { background: var(--card); }

/* --- офлайн-плашка --- */
.offline-banner {
  background: var(--warn);
  color: #1a1200;
  text-align: center;
  font-size: 13px;
  padding: 6px 10px;
  padding-top: calc(6px + env(safe-area-inset-top));
}

/* --- контент --- */
#app {
  flex: 1;
  overflow-y: auto;
  padding: 16px;
  padding-bottom: 90px;
}

.screen { max-width: 560px; margin: 0 auto; }

h1 { font-size: 22px; margin: 4px 0 6px; }
h2 { font-size: 17px; margin: 0 0 10px; }
p { line-height: 1.5; color: var(--text-dim); }

.muted { color: var(--text-dim); font-size: 13px; }

/* --- карточки --- */
.card {
  background: var(--card);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 16px;
  margin-bottom: 14px;
}

.card-row {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 8px 0;
  border-bottom: 1px solid var(--border);
}
.card-row:last-child { border-bottom: none; }
.card-row .k { color: var(--text-dim); font-size: 14px; }
.card-row .v { font-weight: 600; text-align: right; }

/* --- формы --- */
.field { margin-bottom: 14px; }
.field label { display: block; font-size: 13px; color: var(--text-dim); margin-bottom: 6px; }
.field input {
  width: 100%;
  padding: 12px 14px;
  border-radius: 10px;
  border: 1px solid var(--border);
  background: var(--bg-elevated);
  color: var(--text);
  font-size: 16px;
}
.field input:focus { outline: 2px solid var(--accent); border-color: var(--accent); }
.field-hint { font-size: 12px; color: var(--text-dim); margin-top: 5px; }

button:focus-visible, a:focus-visible {
  outline: 2px solid var(--accent);
  outline-offset: 2px;
}

.btn {
  display: block;
  width: 100%;
  padding: 13px 16px;
  border-radius: 10px;
  border: none;
  background: var(--accent);
  color: #04191d;
  font-size: 16px;
  font-weight: 700;
  cursor: pointer;
  text-align: center;
}
.btn:active { background: var(--accent-strong); }
.btn[disabled] { opacity: .55; pointer-events: none; }
.btn-secondary {
  background: transparent;
  border: 1px solid var(--border);
  color: var(--text);
  font-weight: 600;
}
.btn-danger { background: var(--danger); color: #fff; }
.btn-link {
  background: none;
  border: none;
  color: var(--accent);
  font-size: 14px;
  cursor: pointer;
  padding: 8px 0;
  text-align: center;
  display: block;
  width: 100%;
}

.error-box {
  background: var(--danger-bg);
  color: var(--danger);
  border: 1px solid var(--danger);
  border-radius: 10px;
  padding: 10px 12px;
  font-size: 14px;
  margin-bottom: 14px;
}
.ok-box {
  background: rgba(59,178,115,.12);
  color: var(--ok);
  border: 1px solid var(--ok);
  border-radius: 10px;
  padding: 10px 12px;
  font-size: 14px;
  margin-bottom: 14px;
}

/* --- вкладки --- */
.tabbar {
  position: fixed;
  left: 0; right: 0; bottom: 0;
  display: flex;
  background: var(--bg-elevated);
  border-top: 1px solid var(--border);
  padding-bottom: env(safe-area-inset-bottom);
  z-index: 10;
}
.tab-btn {
  flex: 1;
  background: none;
  border: none;
  color: var(--text-dim);
  padding: 12px 4px;
  font-size: 13px;
  cursor: pointer;
}
.tab-btn.active { color: var(--accent); font-weight: 700; }

/* --- пустое состояние --- */
.empty-state {
  text-align: center;
  padding: 40px 12px 10px;
}
.empty-state .icon {
  font-size: 40px;
  margin-bottom: 10px;
}
.empty-state h1 { margin-bottom: 8px; }
.empty-state p { margin: 0 auto 18px; max-width: 380px; }

/* --- список приборов --- */
.device-item {
  display: flex;
  align-items: center;
  gap: 12px;
  padding: 14px;
  background: var(--card);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  margin-bottom: 10px;
  cursor: pointer;
  text-align: left;
  width: 100%;
}
.device-item .dot {
  width: 11px; height: 11px; border-radius: 50%;
  flex-shrink: 0;
  background: var(--ok);
}
.device-item.is-alarm .dot { background: var(--danger); }
.device-item.is-unknown .dot { background: var(--text-dim); }
.device-item .body { flex: 1; min-width: 0; }
.device-item .name { font-weight: 600; margin-bottom: 3px; }
.device-item .sub { font-size: 13px; color: var(--text-dim); }
.device-item .chev { color: var(--text-dim); font-size: 20px; }

/* --- тревога --- */
.alarm-banner {
  background: var(--danger-bg);
  border: 1px solid var(--danger);
  border-radius: var(--radius);
  padding: 16px;
  margin-bottom: 16px;
}
.alarm-banner .title {
  color: var(--danger);
  font-weight: 700;
  font-size: 17px;
  margin-bottom: 6px;
  display: flex;
  align-items: center;
  gap: 8px;
}
.alarm-banner p { color: var(--text); margin: 0 0 12px; }
.alarm-banner.acked {
  border-color: var(--warn);
  background: rgba(226,168,59,.1);
}
.alarm-banner.acked .title { color: var(--warn); }

/* --- график --- */
.chart-wrap {
  background: var(--bg-elevated);
  border: 1px solid var(--border);
  border-radius: 10px;
  padding: 10px;
}
canvas.chart { width: 100%; height: 160px; display: block; }
.period-select {
  min-height: 44px;
  margin-left: 6px;
  padding: 6px 8px;
  border-radius: 8px;
  border: 1px solid var(--border);
  background: var(--bg-elevated);
  color: var(--text);
  font-size: 15px;
}

/* --- лента событий --- */
.event-item {
  display: flex;
  gap: 10px;
  padding: 9px 0;
  border-bottom: 1px solid var(--border);
  font-size: 14px;
}
.event-item:last-child { border-bottom: none; }
.event-item .time { color: var(--text-dim); white-space: nowrap; font-size: 12px; min-width: 74px; }

/* --- спиннер --- */
.spinner {
  width: 22px; height: 22px;
  border: 3px solid var(--border);
  border-top-color: var(--accent);
  border-radius: 50%;
  animation: spin .8s linear infinite;
  margin: 30px auto;
}
@keyframes spin { to { transform: rotate(360deg); } }

/* --- pull to refresh --- */
.pull-indicator {
  text-align: center;
  font-size: 12px;
  color: var(--text-dim);
  padding: 6px 0;
  height: 0;
  overflow: hidden;
  transition: height .15s;
}
.pull-indicator.show { height: 26px; }

.center-msg { text-align: center; padding: 40px 20px; color: var(--text-dim); }

code.code-block {
  display: block;
  background: var(--bg-elevated);
  border: 1px solid var(--border);
  border-radius: 8px;
  padding: 10px;
  font-size: 15px;
  letter-spacing: .04em;
  word-break: break-all;
  margin: 6px 0 14px;
}

a.plain { color: var(--accent); }

/* --- шапка экрана прибора: крупная температура -------------------------
   Все, ради чего человек открывает приложение, должно читаться с
   вытянутой руки и одним глазом. Отсюда кегль 60 и цветная метка вместо
   строки текста. Таблица показаний осталась ниже, для тех случаев, когда
   человек уже приехал и разбирается. */
.hero {
  background: linear-gradient(160deg, var(--card), var(--bg-elevated));
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 16px 16px 14px;
  margin-bottom: 12px;
}
.hero.stale { opacity: .62; }
.hero-top { display: flex; justify-content: flex-end; }
.hero-temp {
  font-size: 60px;
  line-height: 1.02;
  font-weight: 700;
  letter-spacing: -1.5px;
  margin: 2px 0 2px;
  /* Цифры одной ширины: без этого при смене 8.9 на 9.0 вся строка дергается. */
  font-variant-numeric: tabular-nums;
}
.hero-temp .gradus { font-size: 24px; font-weight: 600; color: var(--text-dim); letter-spacing: 0; }
.hero-temp .net { font-size: 22px; font-weight: 600; color: var(--text-dim); letter-spacing: 0; }
.hero-sub { font-size: 13px; color: var(--text-dim); }

.pill {
  display: inline-block;
  padding: 4px 10px;
  border-radius: 999px;
  font-size: 12px;
  font-weight: 700;
  background: rgba(144,164,176,.16);
  color: var(--text-dim);
}
.pill.ok { background: rgba(59,178,115,.16); color: var(--ok); }
.pill.warn { background: rgba(226,168,59,.16); color: var(--warn); }
.pill.danger { background: rgba(226,82,59,.18); color: var(--danger); }

.plitki {
  display: grid;
  grid-template-columns: repeat(2, 1fr);
  gap: 8px;
  margin-top: 14px;
}
.plitka {
  background: var(--bg-elevated);
  border: 1px solid var(--border);
  border-radius: 10px;
  padding: 9px 11px;
  min-width: 0;
}
.plitka .pn { font-size: 11px; color: var(--text-dim); text-transform: uppercase; letter-spacing: .4px; }
.plitka .pv { font-size: 15px; font-weight: 600; margin-top: 2px; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.plitka.ok .pv { color: var(--ok); }
.plitka.bad { border-color: var(--danger); }
.plitka.bad .pv { color: var(--danger); }

/* Температура в строке списка приборов. */
.device-item .grad {
  font-size: 22px;
  font-weight: 700;
  font-variant-numeric: tabular-nums;
  color: var(--text);
  flex-shrink: 0;
}
.device-item .grad i { font-style: normal; font-size: 14px; color: var(--text-dim); }
.device-item.is-alarm .grad { color: var(--danger); }

/* --- подсказка про установку на экран ----------------------------------
   Показывается только в сафари на айфоне и только пока приложение не
   поставлено. Айфон, в отличие от андроида, сам такое не предлагает:
   человек так и живет с закладкой, без значка и без офлайна. */
.ustanovka {
  position: fixed;
  left: 10px; right: 10px;
  bottom: calc(58px + env(safe-area-inset-bottom));
  background: var(--bg-elevated);
  border: 1px solid var(--accent);
  border-radius: var(--radius);
  padding: 12px 14px;
  display: flex;
  gap: 10px;
  align-items: flex-start;
  box-shadow: 0 10px 30px rgba(0,0,0,.35);
  z-index: 20;
}
.ustanovka .txt { flex: 1; font-size: 13px; line-height: 1.45; }
.ustanovka .txt b { color: var(--accent-strong); }
.ustanovka .zakryt {
  background: none; border: none; color: var(--text-dim);
  font-size: 22px; line-height: 1; cursor: pointer;
  width: 44px; height: 44px; flex-shrink: 0;
  display: flex; align-items: center; justify-content: center;
}

/* --- экран тревог: код привязки и адрес темы --- */
.kod-box {
  border: 1px solid var(--border);
  border-radius: 10px;
  padding: 12px;
  margin: 10px 0 12px;
}
.kod {
  font-family: ui-monospace, SFMono-Regular, Menlo, monospace;
  font-size: 24px;
  letter-spacing: 2px;
  color: var(--accent-strong);
  margin: 6px 0 12px;
  user-select: all;
}
.adres {
  font-family: ui-monospace, SFMono-Regular, Menlo, monospace;
  font-size: 13px;
  word-break: break-all;
  background: var(--bg);
  border: 1px solid var(--border);
  border-radius: 10px;
  padding: 10px 12px;
  margin-bottom: 12px;
  user-select: all;
}
.card .btn { margin-bottom: 8px; }
