:root {
  color-scheme: light dark;

  --bg: light-dark(#f2f4f7, #0e1014);
  --card: light-dark(#ffffff, #171a21);
  --card-2: light-dark(#f6f7f9, #1e222b);
  --text: light-dark(#10131a, #f2f4f8);
  --muted: light-dark(#69707d, #99a1b0);
  --line: light-dark(#e4e7ec, #262b35);
  --brand: light-dark(#1f9d63, #35c98a);
  --brand-soft: light-dark(#e3f6ec, #163024);
  --warn: light-dark(#d98324, #f0a44b);
  --danger: light-dark(#d3453b, #f07a72);
  --info: light-dark(#2f7de1, #6fb0ff);
  --radius: 16px;
  --shadow: light-dark(0 1px 2px rgba(16,19,26,.06), 0 1px 0 rgba(255,255,255,.04));
}

/* ручное переключение схемы отражается в CSS */
:root:has(> head > meta[name="color-scheme"][content="dark"]) { color-scheme: dark; }
:root:has(> head > meta[name="color-scheme"][content="light"]) { color-scheme: light; }

* { box-sizing: border-box; }

html, body {
  margin: 0;
  padding: 0;
  background: var(--bg);
  color: var(--text);
  font: 15px/1.45 -apple-system, BlinkMacSystemFont, "SF Pro Text", "Segoe UI", Roboto, sans-serif;
  -webkit-font-smoothing: antialiased;
  scrollbar-color: light-dark(#c3c8d1, #39404d) var(--bg);
}

/* страница не скроллится целиком: скроллится только .wrap,
   нижняя панель табов всегда остаётся на экране (в Telegram WebView
   position:fixed прячется при скролле) */
html, body { height: 100%; }
body {
  height: 100dvh;
  display: flex;
  flex-direction: column;
  overflow: hidden;
  overscroll-behavior: none;
}

.wrap {
  flex: 1 1 auto;
  min-height: 0;
  overflow-y: auto;
  overflow-x: hidden;
  -webkit-overflow-scrolling: touch;
  overscroll-behavior: contain;
  max-width: 560px;
  width: 100%;
  margin: 0 auto;
  padding: 16px 14px 24px;
}

.top {
  display: flex;
  align-items: flex-start;
  justify-content: space-between;
  gap: 12px;
  padding: 4px 2px 14px;
}
.top h1 { font-size: 24px; margin: 0; letter-spacing: -.02em; }
.muted { color: var(--muted); }
.small { font-size: 13px; }

.icon-btn {
  border: 1px solid var(--line);
  background: var(--card);
  color: var(--text);
  width: 38px; height: 38px;
  border-radius: 12px;
  font-size: 17px;
  cursor: pointer;
}
.icon-btn:active { transform: scale(.96); }

.card {
  background: var(--card);
  border: 1px solid var(--line);
  border-radius: var(--radius);
  padding: 16px;
  margin-bottom: 12px;
  box-shadow: var(--shadow);
}
.card h2 { font-size: 15px; margin: 0 0 12px; font-weight: 600; }

.ring-card { display: flex; align-items: center; gap: 16px; }
.ring { width: 132px; height: 132px; flex: 0 0 auto; transform: rotate(-90deg); }
.ring circle { fill: none; stroke-width: 14; stroke-linecap: round; }
.ring-bg { stroke: var(--card-2); }
.ring-fg {
  stroke: var(--brand);
  stroke-dasharray: 414.7;
  stroke-dashoffset: 414.7;
  transition: stroke-dashoffset .5s ease;
}
.ring-wrap { position: relative; width: 132px; height: 132px; flex: 0 0 auto; }
.ring-center {
  position: absolute; inset: 0;
  display: flex; flex-direction: column; align-items: center; justify-content: center;
  gap: 2px;
}
.ring-center strong { font-size: 26px; letter-spacing: -.02em; }
.ring-center span { font-size: 12px; }
.ring-side { display: flex; flex-direction: column; gap: 8px; }
.pill {
  display: inline-block;
  padding: 6px 12px;
  border-radius: 999px;
  background: var(--brand-soft);
  color: var(--brand);
  font-weight: 600;
  font-size: 14px;
  width: fit-content;
}

.macro { margin-bottom: 12px; }
.macro:last-child { margin-bottom: 0; }
.macro-head { display: flex; justify-content: space-between; font-size: 13px; margin-bottom: 6px; }
.macro-head .muted { font-size: 12px; }
.bar { height: 8px; border-radius: 999px; background: var(--card-2); overflow: hidden; }
.bar > i { display: block; height: 100%; border-radius: 999px; background: var(--brand); transition: width .4s ease; }
.bar.over > i { background: var(--danger); }
.bar.under > i { background: var(--info); }

.quick { display: grid; grid-template-columns: 1fr 1fr; gap: 8px; }
.quick .btn { margin: 0; }

.btn {
  border: 1px solid var(--line);
  background: var(--card-2);
  color: var(--text);
  border-radius: 12px;
  padding: 11px 14px;
  font-size: 14px;
  font-weight: 500;
  cursor: pointer;
}
.btn:active { transform: scale(.98); }
.btn.primary { background: var(--brand); border-color: var(--brand); color: #fff; }
.btn.ghost { background: transparent; }
.btn.danger { background: #d64545; border-color: #d64545; color: #fff; }
.btn:focus-visible, .icon-btn:focus-visible, .seg:focus-visible, .tab:focus-visible {
  outline: 2px solid var(--info); outline-offset: 2px;
}

.form { display: flex; flex-direction: column; gap: 10px; }
.form label { display: flex; flex-direction: column; gap: 6px; font-size: 13px; color: var(--muted); }
.row { display: flex; gap: 8px; }
.row > * { flex: 1; min-width: 0; }
input {
  border: 1px solid var(--line);
  background: var(--card-2);
  color: var(--text);
  border-radius: 12px;
  padding: 11px 12px;
  font-size: 15px;
  width: 100%;
  accent-color: var(--brand);
}
input::placeholder { color: var(--muted); opacity: .8; }
select {
  border: 1px solid var(--line);
  background: var(--card-2);
  color: var(--text);
  border-radius: 12px;
  padding: 11px 12px;
  font-size: 15px;
  width: 100%;
  appearance: none;
  accent-color: var(--brand);
}

.meals { display: flex; flex-direction: column; gap: 8px; }
.meal {
  display: flex; align-items: center; gap: 10px;
  padding: 10px 12px;
  border: 1px solid var(--line);
  border-radius: 12px;
  background: var(--card-2);
}
.meal .name { flex: 1; min-width: 0; }
.meal .name b { display: block; font-weight: 600; font-size: 14px; }
.meal .name span { font-size: 12px; color: var(--muted); }
.meal .kcal { font-weight: 600; font-size: 14px; }
.del {
  border: none; background: transparent; color: var(--muted);
  font-size: 18px; cursor: pointer; padding: 4px 6px; border-radius: 8px;
}
.del:active { color: var(--danger); }
.del.danger { color: var(--danger); }

/* ---------- правка блюда ---------- */
.meal-edit {
  display: flex; flex-direction: column; gap: 8px;
  padding: 10px 12px;
  border: 1px solid var(--brand);
  border-radius: 12px;
  background: var(--card-2);
}
.meal-edit input { padding: 9px 10px; font-size: 14px; }
.meal-edit .row > * { flex: 1; min-width: 0; }
.me-actions { display: flex; gap: 6px; flex-wrap: wrap; }
.me-actions .btn { flex: 0 0 auto; }

/* ---------- список записей веса ---------- */
.w-list { margin-top: 10px; }
.w-row {
  display: flex; align-items: center; gap: 10px;
  padding: 6px 0; border-bottom: 1px dashed var(--line); font-size: 13.5px;
}
.w-row b { margin-left: auto; }

.btn.armed { background: #a32727; border-color: #a32727; color: #fff; }
.w-name { width: 100%; margin-bottom: 8px; padding: 9px 10px; font-size: 14px; font-weight: 600; }

.chart { width: 100%; height: 150px; }
.chart svg { width: 100%; height: 100%; overflow: visible; }
.stats { display: flex; flex-wrap: wrap; gap: 10px; margin-top: 10px; }
.stat {
  background: var(--card-2); border: 1px solid var(--line);
  border-radius: 12px; padding: 8px 12px; font-size: 13px;
}
.stat b { display: block; font-size: 16px; }

.segmented { display: flex; gap: 6px; background: var(--card-2); padding: 4px; border-radius: 12px; }
.seg {
  flex: 1; border: none; background: transparent; color: var(--muted);
  padding: 9px 6px; border-radius: 9px; font-size: 13px; cursor: pointer;
}
.seg.active { background: var(--card); color: var(--text); box-shadow: var(--shadow); }

.tabs {
  flex: 0 0 auto;
  display: flex;
  gap: 2px;
  padding: 6px 8px calc(6px + env(safe-area-inset-bottom));
  background: light-dark(rgba(255,255,255,.92), rgba(20,23,29,.92));
  backdrop-filter: blur(12px);
  border-top: 1px solid var(--line);
  z-index: 20;
}
.tab {
  flex: 1; border: none; background: transparent; color: var(--muted);
  font-size: 11px; padding: 8px 2px; border-radius: 10px; cursor: pointer;
}
.tab span { display: block; font-size: 16px; margin-bottom: 2px; }
.tab.active { color: var(--brand); background: var(--brand-soft); }

.hidden { display: none !important; }

@media (prefers-reduced-motion: reduce) {
  * { transition: none !important; animation: none !important; }
}

/* ---------- калории + макросы рядом ---------- */
.kcal-card { display: flex; align-items: center; gap: 14px; }
.kcal-card .ring-wrap { width: 124px; height: 124px; }
.kcal-card .ring { width: 124px; height: 124px; }
.kcal-card .ring-center strong { font-size: 24px; }
.macro-side { flex: 1; min-width: 0; display: flex; flex-direction: column; gap: 10px; }
.macro-side-head { font-size: 12px; font-weight: 600; }
.macro { margin-bottom: 0; }
.macro-head { display: flex; justify-content: space-between; gap: 6px; font-size: 12.5px; margin-bottom: 4px; }
.macro-head span:last-child { white-space: nowrap; }
.macro-foot { font-size: 11px; margin-top: 3px; }
.bar { height: 6px; }
.bar.over > i { background: var(--danger) !important; }
.bar.under > i { background: var(--info) !important; }

/* ---------- календарь ---------- */
.seg-row { display: flex; align-items: center; justify-content: space-between; gap: 10px; margin-bottom: 10px; }
.month-nav { display: flex; align-items: center; gap: 6px; }
.month-label { font-size: 13px; font-weight: 600; min-width: 108px; text-align: center; }
.calendar { user-select: none; }
.cal-dow { display: grid; grid-template-columns: repeat(7, 1fr); gap: 4px; margin-bottom: 4px; }
.cal-dow span { text-align: center; font-size: 11px; color: var(--muted); }
.cal-grid { display: grid; grid-template-columns: repeat(7, 1fr); gap: 4px; }
.cal-day {
  position: relative;
  border: 1px solid var(--line);
  background: var(--card-2);
  color: var(--text);
  border-radius: 10px;
  height: 46px;
  padding: 4px 2px 3px;
  cursor: pointer;
  display: flex; flex-direction: column; align-items: center; justify-content: flex-start;
  gap: 2px;
}
.cal-day .cal-num { font-size: 12px; line-height: 1.1; }
.cal-bar { width: 70%; height: 16px; background: var(--line); border-radius: 4px; display: flex; align-items: flex-end; overflow: hidden; }
.cal-bar > i { width: 100%; background: var(--brand); border-radius: 4px; display: block; }
.cal-day.sel { border-color: var(--brand); background: var(--brand-soft); }
.cal-day.today .cal-num { color: var(--brand); font-weight: 700; }
.dot { width: 6px; height: 6px; border-radius: 50%; display: inline-block; }
.dot.gym { background: var(--warn); }
.dot.kcal { background: var(--brand); }
.legend { display: flex; gap: 14px; margin-top: 10px; }
.legend span { display: inline-flex; align-items: center; gap: 5px; }
.year-list { display: flex; flex-direction: column; gap: 6px; }
.year-row {
  display: grid; grid-template-columns: 84px 1fr 96px; align-items: center; gap: 8px;
  background: var(--card-2); border: 1px solid var(--line); border-radius: 12px;
  padding: 8px 10px; cursor: pointer; color: var(--text); text-align: left;
}
.yr-label { font-size: 12px; font-weight: 600; }
.yr-bars { display: flex; align-items: flex-end; gap: 1px; height: 26px; }
.yr-bars > i { flex: 1; background: var(--brand); opacity: .6; border-radius: 1px; min-height: 1px; }
.yr-sum { font-size: 11px; text-align: right; }

/* ---------- день ---------- */
.day-summary { display: flex; flex-wrap: wrap; gap: 8px; margin-bottom: 12px; }
.sub { font-size: 13px; margin: 14px 0 8px; color: var(--muted); font-weight: 600; }

/* ---------- тренировки ---------- */
.gym-head { display: flex; align-items: center; justify-content: space-between; gap: 10px; margin-bottom: 10px; }
.gym-head h2 { margin: 0; }
.w-card {
  border: 1px solid var(--line); border-radius: 12px; background: var(--card-2);
  margin-bottom: 8px; overflow: hidden;
}
.w-card > summary {
  list-style: none; cursor: pointer; padding: 10px 12px;
  display: flex; align-items: center; gap: 10px; justify-content: space-between;
}
.w-card > summary::-webkit-details-marker { display: none; }
.w-sum { flex: 1 1 auto; min-width: 0; }
.w-sum b { display: block; font-size: 14px; }
.w-sum .muted { font-size: 12px; }
.chev { color: var(--muted); transition: transform .2s ease; }
/* маленький крестик удаления в заголовке тренировки */
.w-del {
  flex: 0 0 auto;
  font-size: 15px;
  line-height: 1;
  padding: 5px 7px;
  color: var(--muted);
  background: transparent;
  border: none;
  border-radius: 8px;
  cursor: pointer;
}
.w-del:hover, .w-del:active { color: var(--danger); background: var(--card-2); }

/* подтверждение удаления */
.confirm-back { align-items: center; justify-content: center; }
.confirm-sheet { max-width: 380px; text-align: center; }
.confirm-sheet h3 { margin: 0 0 8px; font-size: 18px; }
.confirm-sheet .pr-actions { margin-top: 14px; }
.w-card[open] .chev { transform: rotate(180deg); }
.w-body { padding: 0 12px 12px; border-top: 1px solid var(--line); }
.ex-group { padding: 10px 0; border-bottom: 1px dashed var(--line); }
.ex-head { display: flex; align-items: center; gap: 8px; margin-bottom: 6px; }
.ex-head b { font-size: 13.5px; }
.ex-head .link { margin-left: auto; }
.set-row { display: flex; align-items: center; gap: 6px; margin-bottom: 5px; }
.set-row input { padding: 7px 9px; font-size: 14px; }
.set-row .set-w { width: 88px; flex: 0 0 auto; }
.set-row .set-r { width: 74px; flex: 0 0 auto; }
.set-row .x { color: var(--muted); font-size: 13px; }
.link {
  border: none; background: transparent; color: var(--brand);
  font-size: 12px; cursor: pointer; padding: 2px 4px; border-radius: 6px; font-weight: 600;
}
.w-actions { display: flex; align-items: center; gap: 6px; margin-top: 10px; flex-wrap: wrap; }
.w-actions input { width: 86px; padding: 8px 9px; font-size: 13px; }
.btn.small { padding: 8px 10px; font-size: 12.5px; }
.pr-list { display: flex; flex-direction: column; gap: 6px; }
.pr { display: flex; justify-content: space-between; gap: 10px; font-size: 13px; padding: 6px 0; border-bottom: 1px dashed var(--line); }

/* ---------- шторка выбора упражнения ---------- */
.sheet-back {
  position: fixed; inset: 0; background: rgba(0,0,0,.45);
  display: flex; align-items: flex-end; justify-content: center; z-index: 40;
}
.sheet {
  width: 100%; max-width: 560px;
  background: var(--card);
  border-radius: 18px 18px 0 0;
  border: 1px solid var(--line);
  padding: 14px 14px calc(18px + env(safe-area-inset-bottom));
  max-height: 80vh; overflow-y: auto;
}
.sheet-head { display: flex; align-items: center; justify-content: space-between; margin-bottom: 10px; }
.sheet-head h3 { margin: 0; font-size: 16px; }
.chips { display: flex; flex-wrap: wrap; gap: 6px; margin: 10px 0; }
.chip {
  border: 1px solid var(--line); background: var(--card-2); color: var(--muted);
  border-radius: 999px; padding: 6px 12px; font-size: 12.5px; cursor: pointer;
}
.chip.active { background: var(--brand-soft); color: var(--brand); border-color: var(--brand); }
.ex-list { display: flex; flex-direction: column; gap: 4px; }
.ex {
  display: flex; justify-content: space-between; align-items: center; gap: 8px;
  border: none; background: transparent; color: var(--text);
  padding: 10px 8px; border-radius: 10px; cursor: pointer; font-size: 14px; text-align: left;
}
.ex:active { background: var(--card-2); }

/* ---------- отдых ---------- */
.rest-chip {
  position: fixed; right: 12px; bottom: calc(78px + env(safe-area-inset-bottom));
  background: var(--warn); color: #16181d; font-weight: 700; font-size: 13px;
  padding: 8px 14px; border-radius: 999px; z-index: 30; box-shadow: var(--shadow);
}

/* ---------- повтор прошлой тренировки ---------- */
.repeat-row { display: flex; align-items: center; gap: 10px; justify-content: space-between;
  padding: 8px 0 2px; }
.repeat-row .btn.small { flex: 0 0 auto; }

/* ---------- всплывашка ---------- */
.toast {
  position: fixed; left: 12px; right: 12px; bottom: calc(84px + env(safe-area-inset-bottom));
  background: #16181d; color: #fff; font-size: 13px; line-height: 1.35;
  padding: 12px 14px; border-radius: 14px; z-index: 60; box-shadow: var(--shadow);
  max-width: 520px; margin: 0 auto;
}

/* ---------- состав на 100 г ---------- */
.p100-block { margin: 2px 0 6px; }
.p100-block summary {
  cursor: pointer; font-size: 12.5px; color: var(--muted);
  padding: 4px 0; list-style: none;
}
.p100-block summary::-webkit-details-marker { display: none; }
.p100-block summary:before { content: "＋ "; }
.p100-block[open] summary:before { content: "－ "; }
.p100-block .row { margin-top: 6px; }

/* ---------- проверка фото перед записью ---------- */
.pr-row {
  border: 1px solid var(--line); border-radius: 12px;
  padding: 10px; margin-bottom: 10px; background: var(--card-2);
}
.pr-name { display: flex; gap: 8px; align-items: center; margin-bottom: 6px; }
.pr-name input { flex: 1; font-weight: 600; }
.pr-label { margin: 6px 0 2px; }
.pr-row .row { margin-top: 4px; }
.pr-row [data-out] { flex: 1; text-align: right; }
.pr-actions { display: flex; gap: 8px; margin-top: 10px; }
.pr-actions .btn { flex: 1; }

.ex-manual {
  display: flex; gap: 8px; margin-top: 10px; padding-top: 10px;
  border-top: 1px solid var(--line);
}
.ex-manual input { flex: 1; }
