: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;
}

@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;
}

#app { display: flex; flex-direction: column; height: 100dvh; }

#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-template-columns: repeat(4, 1fr);
  border-top: 1px solid var(--line);
  background: color-mix(in srgb, var(--bg) 92%, transparent);
  backdrop-filter: blur(12px);
  padding-bottom: env(safe-area-inset-bottom);
  z-index: 20;
}
#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.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); }
label.field { display: block; margin-bottom: 10px; }
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-bottom: 6px; }
.video-box iframe { width: 100%; height: 100%; border: 0; border-radius: 10px; background: #000; }

/* ---------- 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%;
}
#chat-form { display: flex; gap: 8px; align-items: flex-end; padding: 8px 0 2px; border-top: 1px solid var(--line); }
#chat-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 {
  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: '...'; } }
.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 {
  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;
}
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; }
