:root {
  --bg: #0b0d10;
  --bg-elev: #14181d;
  --bg-elev2: #1c2229;
  --line: #262d36;
  --text: #e8edf3;
  --muted: #8b97a6;
  --accent: #4ea1ff;
  --accent-dim: #1d3b5c;
  --green: #47c97e;
  --amber: #f0b23a;
  --red: #ff6b6b;
  --radius: 14px;
  --tabbar: 56px;
  --topbar: 52px;
  --env-top: env(safe-area-inset-top, 0px); /* читается из app.js для детекта оболочки */
}

@media (prefers-color-scheme: light) {
  :root:not([data-theme="dark"]) {
    --bg: #f4f6f9;
    --bg-elev: #ffffff;
    --bg-elev2: #eef1f5;
    --line: #dde3ea;
    --text: #121721;
    --muted: #5f6b7a;
    --accent: #0a67d0;
    --accent-dim: #d6e6fb;
  }
}

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

html, body {
  margin: 0; padding: 0; height: 100%;
  background: var(--bg); color: var(--text);
  font: 16px/1.4 -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, sans-serif;
  overscroll-behavior-y: none;
}

/* fixed: iOS при открытии клавиатуры прокручивает документ и после закрытия
   оставляет окно сдвинутым — под таббаром появляется полоса фона. У fixed-каркаса
   документу скроллиться некуда, прокрутка живёт только внутри #view. */
#app {
  position: fixed; top: var(--app-top, 0px); left: 0; right: 0;
  height: var(--app-h, 100dvh); /* фактическая высота visualViewport из app.js */
  /* --app-top: под клавиатурой iOS видимое окно приподнято над layout viewport,
     и каркас переезжает за ним — иначе снизу остаётся полоса фона (app.js) */
  display: flex; flex-direction: column;
}

#topbar {
  display: flex; align-items: center; gap: 8px;
  height: calc(var(--topbar) + env(safe-area-inset-top));
  padding: env(safe-area-inset-top) 12px 0;
  background: color-mix(in srgb, var(--bg) 88%, transparent);
  backdrop-filter: blur(12px);
  border-bottom: 1px solid var(--line);
  position: sticky; top: 0; z-index: 20;
}
#topbar h1 { font-size: 17px; font-weight: 650; margin: 0; flex: 1; letter-spacing: -.01em; }
#topbar-actions { display: flex; gap: 6px; }

#view {
  flex: 1; overflow-y: auto; -webkit-overflow-scrolling: touch;
  padding: 12px 12px calc(12px + env(safe-area-inset-bottom));
  outline: none;
}

#tabs {
  display: grid; grid-auto-flow: column; grid-auto-columns: 1fr;
  border-top: 1px solid var(--line);
  background: color-mix(in srgb, var(--bg) 92%, transparent);
  backdrop-filter: blur(12px);
  /* --app-safe-bottom — замена env(), когда iOS отдаёт усечённый вьюпорт (app.js) */
  padding-bottom: max(env(safe-area-inset-bottom), var(--app-safe-bottom, 0px));
  z-index: 20;
}
/* Клавиатура открыта (класс из app.js): меню убираем совсем — переключать
   вкладки, не дописав фразу, не приходится, а 56 px внизу достаются ленте.
   Заодно не нужен отступ под домашнюю полосу: под клавиатурой её не видно. */
html.kb #tabs { display: none; }
#tabs a {
  display: flex; flex-direction: column; align-items: center; justify-content: center; gap: 2px;
  height: var(--tabbar); text-decoration: none; color: var(--muted);
  font-size: 10.5px; font-weight: 600;
}
#tabs a .ic { font-size: 19px; line-height: 1; filter: grayscale(1) opacity(.75); }
/* Векторные иконки вкладок (микрофон) — красятся цветом вкладки */
#tabs a .ic svg { display: block; }
#tabs a.active { color: var(--accent); }
#tabs a.active .ic { filter: none; }

/* ---------- primitives ---------- */
.card {
  background: var(--bg-elev); border: 1px solid var(--line);
  border-radius: var(--radius); padding: 12px; margin-bottom: 10px;
}
.card.tight { padding: 10px 12px; }
.row { display: flex; align-items: center; gap: 8px; }
.row.between { justify-content: space-between; }
.row.wrap { flex-wrap: wrap; }
.grow { flex: 1; min-width: 0; }
.muted { color: var(--muted); }
.small { font-size: 13px; }
.xsmall { font-size: 11.5px; }
.mono { font-variant-numeric: tabular-nums; }
.hidden { display: none !important; }
.center { text-align: center; }
.mt { margin-top: 10px; }
.mb { margin-bottom: 10px; }
h2 { font-size: 15px; margin: 18px 4px 8px; font-weight: 650; }
h2:first-child { margin-top: 4px; }

button, .btn {
  font: inherit; font-weight: 600; color: var(--text);
  background: var(--bg-elev2); border: 1px solid var(--line);
  border-radius: 11px; padding: 10px 14px; cursor: pointer;
  display: inline-flex; align-items: center; justify-content: center; gap: 6px;
  text-decoration: none; min-height: 42px;
}
button:active, .btn:active { transform: scale(.985); }
button.primary, .btn.primary { background: var(--accent); border-color: var(--accent); color: #fff; }
button.ghost { background: transparent; }
button.danger { color: var(--red); border-color: color-mix(in srgb, var(--red) 40%, var(--line)); background: transparent; }
button.wide, .btn.wide { width: 100%; }
button.sm { min-height: 34px; padding: 6px 10px; font-size: 13px; border-radius: 9px; }
button:disabled { opacity: .45; }
.icon-btn {
  min-height: 34px; width: 34px; padding: 0; font-size: 22px; line-height: 1;
  background: transparent; border: none; color: var(--accent);
}

input, select, textarea {
  font: inherit; color: var(--text); background: var(--bg-elev2);
  border: 1px solid var(--line); border-radius: 11px; padding: 10px 12px;
  width: 100%; min-height: 42px; appearance: none;
}
input:focus, select:focus, textarea:focus { outline: 2px solid var(--accent-dim); border-color: var(--accent); }
/* Чекбоксам возвращаем системный вид: appearance:none делает их невидимыми пилюлями */
input[type="checkbox"] {
  appearance: auto; width: 18px; height: 18px; min-height: auto;
  accent-color: var(--accent); flex: none;
}
label.field { display: block; margin-bottom: 10px; }
/* Нативное поле даты не сжимается: у него фиксированная раскладка «ГГГГ-ММ-ДД»
   плюс иконка календаря, и на 140px день уезжал за край — оставалось «2026-08-».
   Даём ему собственную ширину и запрещаем ужиматься. Правило точечное: в
   модалках дата идёт во всю ширину листа, и width:auto там смотрелся бы огрызком. */
label.field.date-auto { flex: none; }
label.field.date-auto input { width: auto; max-width: 100%; }
label.field > span { display: block; font-size: 12px; color: var(--muted); margin: 0 2px 4px; font-weight: 600; }
.fields { display: grid; grid-template-columns: repeat(auto-fit, minmax(72px, 1fr)); gap: 8px; }

.chip {
  display: inline-flex; align-items: center; gap: 4px;
  padding: 3px 8px; border-radius: 999px; font-size: 11.5px; font-weight: 650;
  background: var(--bg-elev2); border: 1px solid var(--line); color: var(--muted);
}
.chip.accent { color: var(--accent); border-color: var(--accent-dim); background: color-mix(in srgb, var(--accent) 12%, transparent); }
.chip.green { color: var(--green); border-color: color-mix(in srgb, var(--green) 35%, var(--line)); }
.chip.warm { color: var(--amber); border-color: color-mix(in srgb, var(--amber) 35%, var(--line)); }

.empty { text-align: center; color: var(--muted); padding: 40px 20px; font-size: 14px; }
.divider { height: 1px; background: var(--line); margin: 10px -12px; }

/* ---------- workout list ---------- */
.wo-item { display: block; text-decoration: none; color: inherit; }
.wo-date { font-weight: 700; font-size: 15px; }
.wo-ex { font-size: 13px; color: var(--muted); margin-top: 4px; line-height: 1.35; }

/* ---------- exercise / sets ---------- */
.ex { border: 1px solid var(--line); border-radius: var(--radius); margin-bottom: 10px; background: var(--bg-elev); overflow: hidden; }
.ex-head { display: flex; align-items: center; gap: 8px; padding: 11px 12px; }
.ex-name { font-weight: 650; font-size: 15px; }
.sets { list-style: none; margin: 0; padding: 0; }
.set-row {
  display: flex; align-items: center; gap: 8px;
  padding: 8px 12px; border-top: 1px solid var(--line); font-size: 14px;
}
.set-n {
  width: 22px; height: 22px; flex: none; border-radius: 7px; font-size: 11px; font-weight: 700;
  display: grid; place-items: center; background: var(--bg-elev2); color: var(--muted);
}
.set-n.warm { color: var(--amber); }
.set-main { flex: 1; font-variant-numeric: tabular-nums; }
.set-sub { font-size: 11.5px; color: var(--muted); }
.set-actions { display: flex; gap: 4px; }
.hist-line { font-size: 12px; color: var(--muted); font-variant-numeric: tabular-nums; padding: 2px 0; }
.hist-line b { color: var(--text); font-weight: 600; }

/* ---------- видео техники ---------- */
.video-box { aspect-ratio: 16 / 9; margin: 0 auto 6px; max-width: 520px; }
.video-box iframe { width: 100%; height: 100%; border: 0; border-radius: 10px; background: #000; }

/* ---------- музыкальный плеер «Бега» ---------- */
/* Контейнер плеера живёт в каркасе #app и переживает уход с вкладки. На экране
   «Бег» он fixed поверх коробки #run-video (из потока выпал, вёрстку не трогает),
   на других вкладках — строка над таббаром, ужимающая #view. */
.yt-host .yt-frame { width: 100%; height: 100%; }
.yt-host iframe {
  display: block; width: 100%; height: 100%; border: 0; border-radius: 10px; background: #000;
}
.yt-host .yt-bar { display: none; }

.yt-host.docked { position: fixed; z-index: 15; }

.yt-host.mini {
  display: flex; align-items: center; gap: 9px; padding: 6px 12px;
  background: var(--bg-elev); border-top: 1px solid var(--line);
}
.yt-host.mini .yt-frame { flex: none; width: 112px; height: auto; aspect-ratio: 16 / 9; }
.yt-host.mini iframe { border-radius: 8px; }
.yt-host.mini .yt-bar { display: flex; flex: 1; min-width: 0; align-items: center; gap: 4px; }
.yt-host .yt-open, .yt-host .yt-stop {
  min-height: 30px; padding: 3px 6px; font-size: 12.5px; border: 0; border-radius: 8px;
  background: transparent;
}
.yt-host .yt-open {
  display: block; flex: 1; min-width: 0; text-align: left;
  overflow: hidden; text-overflow: ellipsis; white-space: nowrap;
}
.yt-host .yt-stop { flex: none; color: var(--muted); font-size: 15px; }

/* ---------- закреплённая заметка (настройки тренажёра) ---------- */
.pin-note {
  font-size: 12.5px; line-height: 1.35; cursor: pointer;
  background: color-mix(in srgb, var(--accent) 8%, transparent);
  border: 1px dashed color-mix(in srgb, var(--accent) 35%, var(--line));
  border-radius: 9px; padding: 6px 9px; margin: 6px 0 2px;
}
.pin-note:active { background: color-mix(in srgb, var(--accent) 16%, transparent); }

/* ---------- ссылка на карточку упражнения ---------- */
.ex-link {
  color: inherit; text-decoration: none;
  border-bottom: 1px dotted color-mix(in srgb, var(--accent) 65%, transparent);
}
.msg.assistant .ex-link, .hist-line .ex-link { color: var(--accent); }
.msg.user .ex-link { border-bottom-color: rgba(255, 255, 255, .65); }

/* ---------- chat ---------- */
.chat-wrap { display: flex; flex-direction: column; height: 100%; }
#chat-log { flex: 1; overflow-y: auto; padding: 4px 2px 8px; display: flex; flex-direction: column; gap: 8px; }
.msg { max-width: 88%; padding: 9px 12px; border-radius: 16px; font-size: 14.5px; line-height: 1.42; white-space: pre-wrap; word-wrap: break-word; }
.msg.user { align-self: flex-end; background: var(--accent); color: #fff; border-bottom-right-radius: 5px; }
.msg.assistant { align-self: flex-start; background: var(--bg-elev); border: 1px solid var(--line); border-bottom-left-radius: 5px; }
.msg.system-note { align-self: center; background: transparent; color: var(--muted); font-size: 12px; text-align: center; padding: 2px; max-width: 100%; }
.msg.error { align-self: center; color: var(--red); border: 1px solid color-mix(in srgb, var(--red) 40%, var(--line)); font-size: 13px; max-width: 100%; }
.msg code { background: rgba(127,127,127,.18); padding: 1px 4px; border-radius: 5px; font-size: .92em; }
.msg b { font-weight: 700; }
.tool-note {
  align-self: flex-start; font-size: 11.5px; color: var(--green);
  border: 1px dashed color-mix(in srgb, var(--green) 40%, var(--line));
  border-radius: 9px; padding: 4px 9px; max-width: 92%;
}
.tool-note.editable { cursor: pointer; }
.tool-note.editable:active { background: var(--bg-elev2); }
#chat-form, #live-form { display: flex; gap: 8px; align-items: flex-end; padding: 8px 0 2px; border-top: 1px solid var(--line); }
#chat-input, #live-input { resize: none; max-height: 120px; min-height: 42px; padding: 10px 12px; line-height: 1.35; }
#chat-mic { flex: none; padding: 0 12px; font-size: 17px; }
#chat-mic.rec, #live-mic.rec {
  color: #fff; background: var(--red); border-color: var(--red);
  animation: rec-pulse 1.2s ease-in-out infinite;
}
@keyframes rec-pulse { 50% { opacity: .6; } }
.typing::after { content: '…'; animation: dots 1.2s steps(4, end) infinite; }
@keyframes dots { 0% { content: ' '; } 25% { content: '.'; } 50% { content: '..'; } 75% { content: '...'; } }
/* ---------- «пульт тренировки» (эксперимент) ---------- */
#live-log { flex: 1; overflow-y: auto; padding: 4px 2px 8px; display: flex; flex-direction: column; gap: 8px; }

/* Кнопка возврата к свежим записям — плавает над строкой ввода, появляется,
   когда ленту отлистали вверх. Пока она видна, новые записи не подматывают
   экран сами: читать прошлые подходы под рукой тренера иначе невозможно. */
.log-wrap { position: relative; flex: 1; min-height: 0; display: flex; flex-direction: column; }
.to-latest {
  position: absolute; left: 50%; bottom: 14px; transform: translateX(-50%);
  width: 44px; height: 44px; min-height: 44px; padding: 0;
  display: flex; align-items: center; justify-content: center;
  border-radius: 999px; border: 1px solid var(--line);
  /* На тёмной теме подложка карточек — --bg-elev, поэтому кнопке нужен слой
     выше, иначе она с ними сливается; на светлой белого достаточно. */
  background: var(--bg-elev2); color: var(--text);
  box-shadow: 0 6px 18px rgb(0 0 0 / 0.5);
  z-index: 3;
}
/* Прозрачный запас вокруг: палец в зале попадает мимо сорока точек, особенно
   когда кнопка висит над лентой и рядом строка ввода. Видимый кружок остаётся
   прежним, зона нажатия — 60 точек. */
.to-latest::after { content: ''; position: absolute; inset: -8px; border-radius: 999px; }
.to-latest.hidden { display: none; }
@media (prefers-color-scheme: light) {
  :root:not([data-theme="dark"]) .to-latest {
    background: var(--bg-elev);
    box-shadow: 0 4px 14px rgb(0 0 0 / 0.16);
  }
}
/* Закреплённый слот таймера: между лентой и подсказками, не скроллится */
#live-timer:not(:empty) { padding: 6px 2px 0; border-top: 1px solid var(--line); }
#live-timer .live-card { margin-bottom: 0; }
#live-mic { flex: none; padding: 0 16px; min-height: 42px; }
#live-mic svg { display: block; }
@keyframes spin { to { transform: rotate(360deg); } }
.spin { animation: spin .9s linear infinite; transform-origin: 50% 50%; }
.live-card {
  align-self: stretch; background: var(--bg-elev); border: 1px solid var(--line);
  border-radius: 12px; padding: 8px 12px; cursor: pointer;
}
.live-card:active { background: var(--bg-elev2); }

/* Пометка «можно редактировать» на карточках и заметках */
.pen { color: var(--accent); font-size: 16px; white-space: nowrap; }
.msg.user .pen { color: rgba(255, 255, 255, .85); }

.quick { display: flex; gap: 6px; overflow-x: auto; padding: 6px 0; scrollbar-width: none; }
.quick::-webkit-scrollbar { display: none; }
.quick button { white-space: nowrap; flex: none; font-size: 12.5px; min-height: 32px; padding: 5px 10px; border-radius: 999px; }

/* ---------- weight ---------- */
.big-num { font-size: 34px; font-weight: 700; letter-spacing: -.02em; font-variant-numeric: tabular-nums; }
.delta.up { color: var(--red); }
.delta.down { color: var(--green); }
svg.chart { width: 100%; height: 150px; display: block; overflow: visible; }
.wlist { list-style: none; margin: 0; padding: 0; }
.wlist li { display: flex; align-items: center; gap: 8px; padding: 9px 0; border-bottom: 1px solid var(--line); font-size: 14px; }
.wlist li:last-child { border-bottom: none; }

.toast {
  /* Строка мини-плеера живёт ровно под тостом: сквозные тапы, иначе пара секунд
     тоста съедала бы нажатия по названию трека и ✕. */
  pointer-events: none;
  position: fixed; left: 50%; transform: translateX(-50%);
  bottom: calc(var(--tabbar) + 18px + env(safe-area-inset-bottom));
  background: var(--bg-elev2); border: 1px solid var(--line); color: var(--text);
  padding: 9px 15px; border-radius: 999px; font-size: 13.5px; font-weight: 600;
  z-index: 100; box-shadow: 0 8px 26px rgba(0,0,0,.4); max-width: 90vw; text-align: center;
}
/* Меню спрятано под клавиатурой — тост опускается на его место, иначе висел бы
   в воздухе посреди экрана. */
html.kb .toast { bottom: 18px; }

progress { width: 100%; height: 6px; accent-color: var(--accent); }

/* Экран чата занимает всю высоту вьюпорта */
#view.chat-view { display: flex; flex-direction: column; padding-bottom: 0; overflow: hidden; }
#view.chat-view .chat-wrap { min-height: 0; }

/* ---------- модальные окна ---------- */
.overlay {
  position: fixed; inset: 0; z-index: 200;
  background: rgba(0,0,0,.5); backdrop-filter: blur(3px);
  display: flex; align-items: flex-end; justify-content: center;
  padding: 12px; padding-bottom: calc(12px + env(safe-area-inset-bottom));
  animation: fade .15s ease-out;
}
@media (min-width: 620px) { .overlay { align-items: center; } }
@keyframes fade { from { opacity: 0 } to { opacity: 1 } }
.sheet {
  width: 100%; max-width: 460px; max-height: 84dvh; overflow-y: auto;
  background: var(--bg-elev); border: 1px solid var(--line);
  border-radius: 18px; padding: 16px;
  box-shadow: 0 20px 60px rgba(0,0,0,.45);
  animation: rise .18s ease-out;
}
@keyframes rise { from { transform: translateY(14px) } to { transform: none } }
.sheet-title { font-size: 16px; font-weight: 700; margin-bottom: 10px; }

/* Контейнер экрана, пересоздаётся при каждом рендере */
.screen { min-height: 0; }
#view.chat-view .screen { height: 100%; display: flex; flex-direction: column; }
