/* Мини-эпп здоровья.
 *
 * Цвета берутся из темы телеграма, поэтому приложение выглядит частью
 * мессенджера, а не вставленным сайтом. Запасные значения нужны для случая,
 * когда страницу открыли в обычном браузере — при отладке это происходит
 * постоянно.
 *
 * Шрифт системный. Веб-шрифт здесь означал бы задержку на каждом открытии
 * ради того, чтобы отличаться от остального телефона; ни то, ни другое не
 * нужно.
 */

:root {
  --bg: var(--tg-theme-bg-color, #ffffff);
  --card: var(--tg-theme-secondary-bg-color, #f2f3f5);
  --ink: var(--tg-theme-text-color, #14181f);
  --muted: var(--tg-theme-hint-color, #6d7684);
  --link: var(--tg-theme-link-color, #2f7f76);
  --divider: color-mix(in srgb, var(--muted) 22%, transparent);

  /* Акцент — приглушённый бирюзовый: читается и на светлой, и на тёмной
   * подложке телеграма. Состояния окрашены отдельно и акцентом не являются:
   * смешав их, «хорошо» и «фирменный цвет» станут неотличимы. */
  --accent: #3f9a8f;
  --good: #4b9d6a;
  --watch: #c88a2e;
  --bad: #c25a52;

  --r: 14px;
  --gap: 12px;
}

@media (prefers-color-scheme: dark) {
  :root {
    --bg: var(--tg-theme-bg-color, #17191c);
    --card: var(--tg-theme-secondary-bg-color, #21242a);
    --ink: var(--tg-theme-text-color, #e8eaed);
    --muted: var(--tg-theme-hint-color, #8b939f);
    --accent: #58b3a7;
    --good: #5fb37e;
    --watch: #d69a3f;
    --bad: #d46f66;
  }
}

* { box-sizing: border-box; }

/* Атрибут `hidden` должен побеждать всегда.
 *
 * Он прячет элемент правилом `[hidden] { display: none }` из стилей браузера,
 * а любое авторское `display` бьёт браузерное независимо от весов. Поэтому
 * `.zoom { display: flex }` молча отменял `hidden` на разметке, и развёрнутый
 * график — `position: fixed; inset: 0` с непрозрачным фоном — висел поверх
 * приложения с самой загрузки. В тёмной теме это выглядело ровно как чёрный
 * экран: содержимое было на месте, но под заслонкой.
 *
 * Правило общее, а не точечное на `.zoom`: та же ловушка ждёт каждый
 * следующий элемент, которому зададут и `display`, и `hidden`. */
[hidden] { display: none !important; }

body {
  margin: 0;
  padding: 0 0 76px;
  background: var(--bg);
  color: var(--ink);
  font: 15px/1.45 -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto,
        "Helvetica Neue", sans-serif;
  -webkit-font-smoothing: antialiased;
}

/* --- шапка и выбор периода ------------------------------------------------ */

header {
  position: sticky;
  top: 0;
  z-index: 5;
  background: var(--bg);
  padding: 10px 16px 8px;
  border-bottom: 1px solid var(--divider);
}

.titlebar { display: flex; align-items: flex-start; justify-content: space-between; gap: 10px; }

h1 {
  margin: 0 0 2px;
  font-size: 20px;
  font-weight: 650;
  letter-spacing: -0.01em;
}

.icon-btn {
  border: 1px solid var(--divider);
  background: transparent;
  color: var(--ink);
  font-size: 15px;
  line-height: 1;
  width: 34px; height: 34px;
  border-radius: 10px;
  cursor: pointer;
  flex-shrink: 0;
}

/* Ручной выбор темы перекрывает и системную настройку, и телеграмную —
 * иначе кнопка работала бы только в половине случаев. */
:root[data-theme="light"] {
  --bg: #ffffff; --card: #f2f3f5; --ink: #14181f; --muted: #6d7684;
  --accent: #3f9a8f; --good: #4b9d6a; --watch: #c88a2e; --bad: #c25a52;
}
:root[data-theme="dark"] {
  --bg: #17191c; --card: #21242a; --ink: #e8eaed; --muted: #8b939f;
  --accent: #58b3a7; --good: #5fb37e; --watch: #d69a3f; --bad: #d46f66;
}

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

.periods {
  display: flex;
  gap: 4px;
  margin-top: 10px;
  background: var(--card);
  padding: 3px;
  border-radius: 10px;
}

.periods button {
  flex: 1;
  border: 0;
  background: transparent;
  color: var(--muted);
  font: inherit;
  font-size: 13px;
  padding: 6px 4px;
  border-radius: 8px;
  cursor: pointer;
}

.periods button[aria-selected="true"] {
  background: var(--bg);
  color: var(--ink);
  font-weight: 600;
  box-shadow: 0 1px 3px rgb(0 0 0 / 12%);
}

/* --- карточки ------------------------------------------------------------- */

main { padding: var(--gap) 16px 0; display: flex; flex-direction: column; gap: var(--gap); }

.card {
  background: var(--card);
  border-radius: var(--r);
  padding: 14px;
}

.card h2 {
  margin: 0 0 2px;
  font-size: 15px;
  font-weight: 620;
}

.card .hint { color: var(--muted); font-size: 12.5px; margin: 0 0 10px; }

/* Три подписи под каждым графиком: как читать, что видно, что делать.
 * График без пояснения пролистывают — то же правило, что в отчётах. */
.legend { margin-top: 10px; display: flex; flex-direction: column; gap: 5px; }
.legend div { font-size: 12.5px; color: var(--muted); line-height: 1.4; }
.legend b { color: var(--ink); font-weight: 600; }

.kpis { display: grid; grid-template-columns: repeat(3, 1fr); gap: 10px; }
.kpi { text-align: left; }
.kpi .v { font-size: 21px; font-weight: 650; letter-spacing: -0.02em;
          font-variant-numeric: tabular-nums; }
.kpi .k { font-size: 12px; color: var(--muted); margin-top: 1px; }
.kpi .d { font-size: 12px; margin-top: 2px; }

.good { color: var(--good); }
.watch { color: var(--watch); }
.bad { color: var(--bad); }

.rows { display: flex; flex-direction: column; gap: 7px; }
.row { display: flex; justify-content: space-between; gap: 10px; font-size: 13.5px; }
.row span:last-child { color: var(--muted); font-variant-numeric: tabular-nums;
                       text-align: right; flex-shrink: 0; }

.note {
  background: color-mix(in srgb, var(--watch) 12%, transparent);
  border-radius: 10px;
  padding: 10px 12px;
  font-size: 13px;
  line-height: 1.45;
}
.note.plain { background: color-mix(in srgb, var(--muted) 12%, transparent); }

/* Отметки: крупные цели для пальца, а не мелкие галочки. Отмечать должно
 * быть проще, чем не отмечать, — иначе это перестанут делать через неделю. */
.chips { display: flex; flex-wrap: wrap; gap: 7px; }

.chip {
  border: 1px solid var(--divider);
  background: transparent;
  color: var(--ink);
  font: inherit;
  font-size: 13px;
  padding: 8px 13px;
  border-radius: 999px;
  cursor: pointer;
  min-height: 38px;
}

.chip[aria-pressed="true"] {
  background: var(--accent);
  border-color: var(--accent);
  color: #fff;
  font-weight: 600;
}

.chip:disabled { opacity: 0.5; }

svg { display: block; width: 100%; height: auto; overflow: visible; }

/* --- развёрнутый график --------------------------------------------------- */

.zoom {
  position: fixed;
  inset: 0;
  z-index: 20;
  background: var(--bg);
  display: flex;
  flex-direction: column;
  padding: 12px 14px calc(14px + env(safe-area-inset-bottom, 0px));
  gap: 12px;
  overflow-y: auto;
}

.zoom-bar { display: flex; align-items: center; justify-content: space-between;
            gap: 10px; font-size: 15px; font-weight: 620; }

/* На боку график получает высоту экрана вместо его ширины — для месяца это
 * втрое больше места на точку. */
#zoom-body { flex: 1; display: flex; align-items: center; }
#zoom-body svg { width: 100%; }

.zoom.sideways #zoom-body {
  transform: rotate(90deg);
  transform-origin: center;
  width: 78vh;
  height: 88vw;
  margin: auto;
}

.expand {
  border: 0;
  background: transparent;
  color: var(--muted);
  font: inherit;
  font-size: 12px;
  padding: 4px 0 0;
  cursor: pointer;
  text-decoration: underline;
  text-underline-offset: 3px;
}

/* --- разбор --------------------------------------------------------------- */

.insight { line-height: 1.5; font-size: 14px; }
.insight h3 { margin: 14px 0 4px; font-size: 14px; font-weight: 650; }
.insight h3:first-child { margin-top: 0; }
.insight p { margin: 0 0 8px; }

.stale { color: var(--watch); }

/* Название метрики, по которому можно нажать за объяснением. */
.explain {
  border: 0; background: transparent; color: inherit; font: inherit;
  padding: 0; cursor: pointer;
  border-bottom: 1px dashed var(--muted);
}

.empty { color: var(--muted); font-size: 13px; padding: 6px 0; }

/* --- нижняя навигация ----------------------------------------------------- */

nav {
  position: fixed;
  left: 0; right: 0; bottom: 0;
  display: flex;
  background: var(--bg);
  border-top: 1px solid var(--divider);
  padding: 6px 4px calc(6px + env(safe-area-inset-bottom, 0px));
  z-index: 10;
}

nav button {
  flex: 1;
  border: 0;
  background: transparent;
  color: var(--muted);
  font: inherit;
  font-size: 11px;
  padding: 4px 2px;
  cursor: pointer;
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 3px;
}

nav button[aria-selected="true"] { color: var(--accent); font-weight: 600; }
nav .ico { font-size: 17px; line-height: 1; }

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

@media (prefers-reduced-motion: no-preference) {
  main > .card { animation: rise .18s ease-out both; }
  @keyframes rise { from { opacity: 0; transform: translateY(4px); } }
}
