/* Базовые поверхности. Их переопределяет скрипт из темы Telegram,
   остальные цвета выводятся из них и потому контрастны в любой теме. */
:root {
  color-scheme: light dark;

  --bg: #eff1f4;         /* фон страницы */
  --card: #ffffff;       /* карточки и плашки */
  --text: #101418;       /* основной текст */
  --accent: #2f7fe0;
  --on-accent: #ffffff;

  --text-secondary: color-mix(in srgb, var(--text) 72%, var(--card));
  --text-muted: color-mix(in srgb, var(--text) 60%, var(--card));
  --border: color-mix(in srgb, var(--text) 14%, var(--card));
  --surface: color-mix(in srgb, var(--text) 6%, var(--card));

  --success: #2f9e50;
  --warning: #c9761a;
  --danger: #d64545;
  --radius: 14px;
  --radius-lg: 20px;
  --shadow: 0 1px 3px rgba(16, 24, 40, .07), 0 8px 24px rgba(16, 24, 40, .05);
  --safe-top: env(safe-area-inset-top, 0px);
  --safe-bottom: env(safe-area-inset-bottom, 0px);
}

/* Браузер без Telegram (локальная отладка) — та же логика на тёмной теме. */
@media (prefers-color-scheme: dark) {
  :root {
    --bg: #0f1419;
    --card: #1c242c;
    --text: #f1f4f6;
    --accent: #6aa9ff;
    --on-accent: #0b1118;
    --success: #4caf63;
    --warning: #e0a44a;
    --danger: #f0716f;
    --shadow: 0 1px 3px rgba(0, 0, 0, .35), 0 8px 24px rgba(0, 0, 0, .25);
  }
}

* { box-sizing: border-box; -webkit-tap-highlight-color: transparent; }

html, body {
  margin: 0;
  padding: 0;
  background: var(--bg);
  color: var(--text);
  font-family: -apple-system, BlinkMacSystemFont, "SF Pro Text", "Segoe UI", Roboto, sans-serif;
  font-size: 15px;
  line-height: 1.45;
  max-width: 100%;
}

html {
  overscroll-behavior-y: none;
  /* Горизонтальную прокрутку блокируем только на html. Если задать
     overflow-x: hidden ещё и на body, тот становится собственным
     контейнером прокрутки высотой ровно в содержимое, и колесо мыши
     перестаёт прокручивать страницу. Обрезки по горизонтали достаточно
     на html — она обрезает и содержимое body. */
  overflow-x: hidden;
}

body { padding-bottom: calc(76px + var(--safe-bottom)); }

button { font: inherit; color: inherit; cursor: pointer; border: none; background: none; }
input, select { font: inherit; color: inherit; }

/* ---------- Шапка ---------- */
.app-header {
  position: sticky;
  top: 0;
  z-index: 20;
  display: flex;
  align-items: center;
  gap: 12px;
  padding: calc(var(--safe-top) + 12px) 16px 12px;
  background: var(--bg);
  border-bottom: 1px solid var(--border);
}

.app-title { font-size: 17px; font-weight: 600; letter-spacing: -.01em; }
.app-subtitle { font-size: 12px; color: var(--text-secondary); margin-top: 1px; }
.header-spacer { flex: 1; }

.icon-button {
  display: grid;
  place-items: center;
  width: 38px;
  height: 38px;
  border-radius: 12px;
  background: var(--surface);
  color: var(--text-secondary);
  transition: transform .12s ease, background .12s ease;
}
.icon-button:active { transform: scale(.92); background: var(--border); }
.icon-button.is-spinning svg { animation: spin .8s linear infinite; }

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

/* ---------- Контент ---------- */
/* min-width: 0 обязателен для детей grid/flex: по умолчанию у них
   min-width: auto, и длинное слово растягивает колонку так, что страница
   начинает прокручиваться по горизонтали. */
main {
  padding: 16px;
  display: grid;
  gap: 14px;
  max-width: 720px;
  margin: 0 auto;
  overflow-wrap: break-word;
}
main > *,
.card > *,
.lesson > *,
.day-row > *,
.metrics > *,
.now > *,
.banner > * { min-width: 0; }

.view[hidden] { display: none; }

.card {
  background: var(--card);
  border-radius: var(--radius-lg);
  padding: 16px;
  box-shadow: var(--shadow);
  overflow: hidden;
}

.card-head {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  margin-bottom: 12px;
}
.card-title { font-size: 15px; font-weight: 600; }
.card-note { font-size: 12px; color: var(--text-secondary); }

/* ---------- Погода ---------- */
.now { display: flex; align-items: center; gap: 16px; }
.now-emoji { font-size: 56px; line-height: 1; }
.now-temp { font-size: 44px; font-weight: 300; letter-spacing: -.03em; line-height: 1; }
.now-meta { font-size: 13px; color: var(--text-secondary); margin-top: 6px; }
.now-feels { font-size: 13px; color: var(--text-secondary); margin-top: 2px; }

.metrics { display: grid; grid-template-columns: repeat(3, 1fr); gap: 8px; margin-top: 14px; }
.metric {
  background: var(--surface);
  border-radius: var(--radius);
  padding: 10px 8px;
  text-align: center;
}
.metric-value { font-size: 15px; font-weight: 600; }
.metric-label { font-size: 11px; color: var(--text-secondary); margin-top: 2px; }

.range {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  margin-top: 14px;
  padding-top: 14px;
  border-top: 1px solid var(--border);
}
.range-temp { font-size: 20px; font-weight: 500; }
.range-label { font-size: 12px; color: var(--text-secondary); }

.day-list { display: grid; gap: 2px; }
.day-row {
  display: grid;
  grid-template-columns: 52px 30px 1fr auto;
  align-items: center;
  gap: 10px;
  padding: 11px 4px;
}
.day-row + .day-row { border-top: 1px solid var(--border); }
.day-name { font-size: 14px; }
.day-date { font-size: 11px; color: var(--text-secondary); }
.day-emoji { font-size: 20px; text-align: center; }
.day-desc {
  font-size: 13px;
  color: var(--text-secondary);
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}
.day-temps { font-size: 14px; font-weight: 500; white-space: nowrap; }
.day-temps .max { font-weight: 600; }
.day-temps .min { color: var(--text-muted); }

/* ---------- Расписание ---------- */
/* Дни переносятся, а не прокручиваются: полоса скролла в расписании не нужна. */
.day-tabs {
  display: flex;
  flex-wrap: wrap;
  gap: 8px;
}

.day-tab {
  flex: 1 1 auto;
  min-width: 62px;
  padding: 9px 10px;
  border-radius: var(--radius);
  background: var(--surface);
  text-align: center;
  transition: background .15s ease, color .15s ease, transform .12s ease;
}
.day-tab:active { transform: scale(.95); }
.day-tab.is-active { background: var(--accent); color: var(--on-accent); }
.day-tab-weekday { font-size: 13px; font-weight: 600; }
.day-tab-count { font-size: 11px; opacity: .7; margin-top: 1px; }
.day-tab.is-empty .day-tab-count { opacity: .45; }

.lesson { display: grid; grid-template-columns: auto 1fr; gap: 14px; padding: 14px 0; }
.lesson + .lesson { border-top: 1px solid var(--border); }
.lesson-time {
  min-width: 62px;
  font-size: 13px;
  font-weight: 600;
  color: var(--accent);
  font-variant-numeric: tabular-nums;
}
.lesson-num { display: block; font-size: 11px; color: var(--text-muted); font-weight: 400; }
.lesson-title { font-size: 15px; font-weight: 500; }
.lesson-kind { font-size: 12px; color: var(--text-secondary); margin-top: 3px; }
.lesson-meta { display: grid; gap: 4px; margin-top: 8px; font-size: 12.5px; color: var(--text-secondary); }
.variant + .variant { margin-top: 2px; }
.variant-part { font-weight: 600; color: var(--text-muted); margin-right: 6px; }

/* ---------- Нижние вкладки ---------- */
.tabbar {
  position: fixed;
  left: 0;
  right: 0;
  bottom: 0;
  z-index: 30;
  display: grid;
  grid-template-columns: repeat(4, 1fr);
  gap: 4px;
  padding: 8px 12px calc(8px + var(--safe-bottom));
  background: var(--bg);
  border-top: 1px solid var(--border);
}
.tab {
  display: grid;
  justify-items: center;
  gap: 2px;
  padding: 6px 4px;
  border-radius: var(--radius);
  font-size: 11px;
  color: var(--text-secondary);
  transition: color .15s ease, background .15s ease;
}
.tab svg { width: 22px; height: 22px; }
.tab.is-active { color: var(--accent); background: color-mix(in srgb, var(--accent) 12%, transparent); }

/* ---------- Панель настроек ---------- */
.sheet-backdrop {
  position: fixed;
  inset: 0;
  z-index: 40;
  background: rgba(0, 0, 0, .45);
  opacity: 0;
  pointer-events: none;
  transition: opacity .2s ease;
}
.sheet-backdrop.is-open { opacity: 1; pointer-events: auto; }

.sheet {
  position: fixed;
  left: 0;
  right: 0;
  bottom: 0;
  z-index: 41;
  max-height: 88vh;
  display: flex;
  flex-direction: column;
  background: var(--bg);
  border-radius: 22px 22px 0 0;
  transform: translateY(100%);
  transition: transform .25s cubic-bezier(.32, .72, 0, 1);
  padding-bottom: var(--safe-bottom);
}
.sheet.is-open { transform: translateY(0); }

.sheet-grip {
  width: 38px;
  height: 4px;
  border-radius: 2px;
  background: var(--border);
  margin: 10px auto 4px;
  flex: 0 0 auto;
}
.sheet-head {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 8px 16px 12px;
  border-bottom: 1px solid var(--border);
  flex: 0 0 auto;
}
.sheet-title { font-size: 16px; font-weight: 600; }
.sheet-body { overflow-y: auto; padding: 16px; display: grid; gap: 18px; }

.field { display: grid; gap: 8px; }
.field-label { font-size: 13px; font-weight: 500; color: var(--text-secondary); }
.field-hint { font-size: 12px; color: var(--text-muted); }

.input {
  width: 100%;
  padding: 12px 14px;
  border-radius: var(--radius);
  border: 1px solid var(--border);
  background: var(--surface);
  outline: none;
}
.input:focus { border-color: var(--accent); }

.chips { display: flex; flex-wrap: wrap; gap: 8px; }
.chip {
  padding: 8px 14px;
  border-radius: 999px;
  background: var(--surface);
  border: 1px solid var(--border);
  font-size: 13px;
  transition: transform .12s ease, background .15s ease, color .15s ease;
}
.chip:active { transform: scale(.94); }
.chip.is-active { background: var(--accent); color: var(--on-accent); border-color: var(--accent); }

.switch-row { display: flex; align-items: center; justify-content: space-between; gap: 12px; }
.switch {
  position: relative;
  width: 46px;
  height: 28px;
  border-radius: 999px;
  background: var(--border);
  transition: background .18s ease;
  flex: 0 0 auto;
}
.switch::after {
  content: "";
  position: absolute;
  top: 3px;
  left: 3px;
  width: 22px;
  height: 22px;
  border-radius: 50%;
  background: #fff;
  box-shadow: 0 1px 3px rgba(0, 0, 0, .25);
  transition: transform .18s ease;
}
.switch.is-on { background: var(--success); }
.switch.is-on::after { transform: translateX(18px); }

/* Недоступное действие: плата не отвечает, нажимать бесполезно. */
.switch.is-disabled { opacity: .45; pointer-events: none; }

/* ---------- Дом ---------- */
#home-sensor { margin-top: 12px; }

.list { display: grid; gap: 2px; max-height: 320px; overflow-y: auto; }
.list-row {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  width: 100%;
  padding: 12px 14px;
  border-radius: var(--radius);
  text-align: left;
}
.list-row:active { background: var(--surface); }
.list-row.is-active { background: color-mix(in srgb, var(--accent) 12%, transparent); }
.list-title { font-size: 14px; font-weight: 500; }
.list-sub { display: block; font-size: 12px; color: var(--text-secondary); margin-top: 1px; }
.list-check { color: var(--accent); opacity: 0; }
.list-row.is-active .list-check { opacity: 1; }

.group-course { margin-top: 8px; }
.group-course-title {
  font-size: 12px;
  font-weight: 600;
  color: var(--text-muted);
  text-transform: uppercase;
  letter-spacing: .04em;
}

.btn {
  width: 100%;
  padding: 13px;
  border-radius: var(--radius);
  background: var(--accent);
  color: var(--on-accent);
  font-size: 15px;
  font-weight: 500;
  transition: transform .12s ease, opacity .15s ease;
}
.btn:active { transform: scale(.98); opacity: .9; }
.btn.is-ghost { background: var(--surface); color: var(--text); }
.btn[disabled] { opacity: .6; pointer-events: none; }

/* ---------- Состояния ---------- */
.empty {
  display: grid;
  justify-items: center;
  gap: 10px;
  padding: 40px 20px;
  text-align: center;
}
.empty-emoji { font-size: 40px; }
.empty-title { font-size: 16px; font-weight: 600; }
.empty-text { font-size: 13px; color: var(--text-secondary); max-width: 280px; }

.skeleton {
  background: linear-gradient(90deg, var(--surface) 25%, var(--border) 37%, var(--surface) 63%);
  background-size: 400% 100%;
  animation: shimmer 1.3s ease infinite;
  border-radius: var(--radius);
}
@keyframes shimmer { 0% { background-position: 100% 0; } 100% { background-position: 0 0; } }

.banner {
  display: flex;
  align-items: flex-start;
  gap: 10px;
  padding: 12px 14px;
  border-radius: var(--radius);
  background: color-mix(in srgb, var(--warning) 16%, var(--card));
  font-size: 13px;
}
.banner.is-error { background: color-mix(in srgb, var(--danger) 14%, var(--card)); }

.toast {
  position: fixed;
  left: 50%;
  bottom: calc(88px + var(--safe-bottom));
  transform: translate(-50%, 12px);
  z-index: 60;
  max-width: calc(100vw - 40px);
  padding: 10px 16px;
  border-radius: 999px;
  background: var(--text);
  color: var(--bg);
  font-size: 13px;
  text-align: center;
  box-shadow: var(--shadow);
  opacity: 0;
  pointer-events: none;
  transition: opacity .2s ease, transform .2s ease;
}
.toast.is-visible { opacity: 1; transform: translate(-50%, 0); }

@media (min-width: 560px) {
  .sheet {
    left: 50%;
    width: min(560px, 100%);
    margin-left: -280px;
  }
}