:root {
  --bg: #F4F5F2;
  --card: #FFFFFF;
  --fg: #1C2321;
  --muted: #5E6763;
  --line: #E3E6E1;
  --grid: #ECEEEA;
  --band: #E4F2E9;
  --accent: #2F6F5E;
  --on-accent: #FFFFFF;
  --ok: #2E7D4F;      --ok-bg: #E4F2E9;
  --high: #9A5B00;    --high-bg: #FBEFD9;   --high-mark: #D08A1C;
  --low: #B3261E;     --low-bg: #FBE3E1;
  --neutral-mark: #8A938F;
  --chip: #ECEEEA;
  --shadow: 0 1px 2px rgba(0,0,0,.05), 0 4px 16px rgba(0,0,0,.04);
  color-scheme: light dark;
}
@media (prefers-color-scheme: dark) {
  :root {
    --bg: #111413;
    --card: #1B201E;
    --fg: #ECEFEC;
    --muted: #A3ACA8;
    --line: #2B322F;
    --grid: #242A27;
    --band: #183022;
    --accent: #6FBFA6;
    --on-accent: #0E1513;
    --ok: #7DD3A0;      --ok-bg: #18301F;
    --high: #F2BE6B;    --high-bg: #342813;   --high-mark: #E9A847;
    --low: #FF9A92;     --low-bg: #3A1A18;
    --neutral-mark: #7E8884;
    --chip: #252B29;
    --shadow: none;
  }
}
* { box-sizing: border-box; -webkit-tap-highlight-color: transparent; }
[hidden] { display: none !important; }
html, body { margin: 0; }
body {
  background: var(--bg);
  color: var(--fg);
  font: 16px/1.45 -apple-system, BlinkMacSystemFont, "SF Pro Text", "Segoe UI", Roboto, sans-serif;
  -webkit-font-smoothing: antialiased;
  -webkit-text-size-adjust: 100%;
}
.app {
  display: block;
  max-width: 430px;
  margin: 0 auto;
  min-height: 100vh;
  padding: calc(env(safe-area-inset-top) + 12px) 16px calc(env(safe-area-inset-bottom) + 96px);
}
body[data-screen="today"] .app { padding-bottom: calc(env(safe-area-inset-bottom) + 168px); }
button { font: inherit; color: inherit; cursor: pointer; }
a { color: var(--accent); }
button:focus-visible, input:focus-visible, textarea:focus-visible, [tabindex]:focus-visible, a:focus-visible {
  outline: 2px solid var(--accent); outline-offset: 2px;
}
input, textarea { font-size: 16px; } /* меньше 16px — iOS увеличивает страницу при вводе */

h1 { font-size: 30px; line-height: 1.15; margin: 2px 0 0; letter-spacing: -.01em; }
h1.page { margin: 18px 0 14px; }
.muted { color: var(--muted); margin: 0; }
.empty { color: var(--muted); text-align: center; padding: 40px 12px; }
.hint { font-size: 13px; color: var(--muted); margin: 10px 0 0; }
.err { color: var(--low); font-size: 14px; min-height: 0; margin: 8px 0 0; }
.err:empty { display: none; }
.center { text-align: center; }
.mt8 { margin-top: 8px; }

/* Навигация по дням */
.day-nav { display: grid; grid-template-columns: 44px 1fr 44px; align-items: center; gap: 4px; margin: 8px 0 14px; }
.day-title { text-align: center; }
.date { color: var(--muted); font-size: 15px; margin: 0; }
.icon-btn { width: 44px; height: 44px; border-radius: 12px; border: none; background: var(--chip); display: grid; place-items: center; }
.icon-btn svg { width: 20px; height: 20px; }
.icon-btn:disabled { opacity: .35; cursor: default; }
.back-today { display: block; width: 100%; min-height: 44px; border: none; border-radius: 12px; background: var(--chip); color: var(--accent); font-weight: 600; margin-bottom: 12px; }

/* Карточки */
.card { background: var(--card); border-radius: 18px; box-shadow: var(--shadow); padding: 16px; margin-bottom: 12px; }
.card h2 { font-size: 13px; font-weight: 600; text-transform: uppercase; letter-spacing: .04em; color: var(--muted); margin: 0 0 10px; }

.last { display: flex; align-items: flex-end; justify-content: space-between; gap: 12px; }
.last .value { font-size: 52px; font-weight: 600; line-height: 1; letter-spacing: -.02em; }
.last .unit { font-size: 15px; color: var(--muted); font-weight: 400; margin-left: 4px; }
.last .meta { color: var(--muted); font-size: 15px; margin-top: 6px; }
.badge { display: inline-flex; align-items: center; gap: 3px; font-size: 11px; font-weight: 600; padding: 1px 7px; border-radius: 999px; white-space: nowrap; }
.badge.big { font-size: 13px; padding: 3px 9px; }
.badge.ok { color: var(--ok); background: var(--ok-bg); }
.badge.high { color: var(--high); background: var(--high-bg); }
.badge.low { color: var(--low); background: var(--low-bg); }
.daystats, .summary { display: grid; grid-template-columns: repeat(3, 1fr); gap: 8px; }
.daystats { margin-top: 14px; padding-top: 14px; border-top: 1px solid var(--line); }
.summary { margin: 14px 0 6px; }
.daystats div, .summary div { font-size: 13px; color: var(--muted); }
.daystats b, .summary b { display: block; font-size: 18px; color: var(--fg); font-variant-numeric: tabular-nums; }

/* Препараты дня */
.pill { display: flex; align-items: center; gap: 12px; min-height: 52px; }
.pill + .pill { border-top: 1px solid var(--line); }
.pill .name { flex: 1; }
.pill .name small { display: block; color: var(--muted); font-size: 13px; }
.check { min-width: 44px; height: 44px; border-radius: 12px; border: 1.5px solid var(--line); background: transparent; padding: 0 12px; font-size: 15px; }
.check.done { background: var(--ok-bg); border-color: transparent; color: var(--ok); font-weight: 600; }
.planned { font-size: 13px; color: var(--muted); padding-top: 10px; margin-top: 4px; border-top: 1px solid var(--line); }
.meds .muted + .planned { border-top: none; }

/* Лента дня */
.feed { list-style: none; margin: 0; padding: 0; }
.feed li { display: grid; grid-template-columns: 48px 28px 1fr auto; align-items: center; gap: 8px; min-height: 52px; padding: 6px 0; cursor: pointer; }
.feed li + li { border-top: 1px solid var(--line); }
.feed time { color: var(--muted); font-size: 15px; font-variant-numeric: tabular-nums; }
.feed .ico { width: 28px; height: 28px; border-radius: 8px; background: var(--chip); display: grid; place-items: center; color: var(--muted); }
.feed .ico svg { width: 16px; height: 16px; }
.feed .what { min-width: 0; }
.feed .what small { display: block; color: var(--muted); font-size: 13px; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.feed .num { font-size: 20px; font-weight: 600; font-variant-numeric: tabular-nums; text-align: right; }
.feed .num .badge { display: flex; margin-top: 2px; justify-content: center; }

.more { display: flex; flex-wrap: wrap; gap: 8px; margin: 4px 0 12px; }
.more button { min-height: 44px; padding: 0 14px; border-radius: 12px; border: none; background: var(--chip); font-size: 15px; display: inline-flex; align-items: center; gap: 6px; }
.more svg { width: 16px; height: 16px; color: var(--muted); }

.fab-wrap { position: fixed; left: 0; right: 0; bottom: calc(env(safe-area-inset-bottom) + 64px); padding: 0 16px 12px; pointer-events: none; z-index: 2; }
.fab {
  pointer-events: auto; display: flex; align-items: center; justify-content: center; gap: 8px;
  width: 100%; max-width: 398px; margin: 0 auto; height: 56px; border-radius: 16px; border: none;
  background: var(--accent); color: var(--on-accent); font-size: 17px; font-weight: 600; box-shadow: 0 6px 20px rgba(47,111,94,.3);
}
.fab svg { width: 20px; height: 20px; }

/* Нижняя навигация */
nav {
  position: fixed; left: 0; right: 0; bottom: 0; background: var(--card); border-top: 1px solid var(--line);
  padding-bottom: env(safe-area-inset-bottom); z-index: 3;
}
nav .tabs { max-width: 430px; margin: 0 auto; display: grid; grid-template-columns: repeat(4, 1fr); }
nav a { text-decoration: none; height: 64px; display: flex; flex-direction: column; align-items: center; justify-content: center; gap: 3px; font-size: 11px; color: var(--muted); }
nav a svg { width: 24px; height: 24px; }
nav a[aria-current="page"] { color: var(--accent); font-weight: 600; }

/* Переключатель периода */
.seg { display: grid; grid-auto-flow: column; grid-auto-columns: 1fr; background: var(--chip); border-radius: 12px; padding: 3px; gap: 2px; margin-bottom: 12px; }
.sheet .seg { margin-bottom: 0; }
.seg button { border: none; background: transparent; min-height: 38px; border-radius: 9px; font-size: 14px; color: var(--muted); padding: 0 4px; }
.seg button[aria-pressed="true"] { background: var(--card); color: var(--fg); font-weight: 600; box-shadow: var(--shadow); }
.range { display: none; grid-template-columns: 1fr auto 1fr; align-items: center; gap: 8px; margin: -4px 0 8px; }
.range.show { display: grid; }
.range span { color: var(--muted); }

/* Таблица замеров */
table.readings { width: 100%; border-collapse: separate; border-spacing: 0; }
table.readings thead th { position: absolute; width: 1px; height: 1px; overflow: hidden; clip: rect(0 0 0 0); }
table.readings tbody th {
  text-align: left; font-size: 13px; font-weight: 600; color: var(--muted); padding: 18px 4px 8px;
  position: sticky; top: 0; background: var(--bg); z-index: 1;
}
table.readings tbody th span { float: right; font-weight: 400; }
tr.row { cursor: pointer; }
tr.row td { background: var(--card); padding: 10px 12px; vertical-align: top; border-top: 1px solid var(--line); }
tr.row.first td { border-top: none; }
tr.row.first td:first-child { border-top-left-radius: 14px; }
tr.row.first td:last-child { border-top-right-radius: 14px; }
tr.row.last td:first-child { border-bottom-left-radius: 14px; }
tr.row.last td:last-child { border-bottom-right-radius: 14px; }
tr.row:active td { background: var(--chip); }
td.t { width: 56px; color: var(--muted); font-variant-numeric: tabular-nums; padding-right: 0 !important; }
td.v { width: 92px; font-size: 20px; font-weight: 600; font-variant-numeric: tabular-nums; line-height: 1.2; }
td.v .badge { margin-top: 3px; }
td.c { font-size: 15px; }
td.c .ctx { color: var(--muted); font-size: 13px; display: block; }
td.c .cmt { display: block; overflow-wrap: anywhere; }
td.c .none { color: var(--muted); }
td.c .src { font-size: 11px; color: var(--muted); display: block; margin-top: 3px; }

/* Статистика */
.hero { display: flex; align-items: baseline; gap: 10px; }
.hero b { font-size: 48px; font-weight: 600; line-height: 1; letter-spacing: -.02em; }
.hero span { color: var(--muted); font-size: 15px; }
.stack { display: flex; gap: 2px; height: 10px; margin: 14px 0 10px; }
.stack i { display: block; height: 100%; min-width: 4px; }
.stack i:first-child { border-radius: 4px 0 0 4px; }
.stack i:last-child { border-radius: 0 4px 4px 0; }
.stack i:only-child { border-radius: 4px; }
.legend { list-style: none; margin: 0; padding: 0; display: grid; gap: 4px; font-size: 15px; }
.legend li { display: flex; align-items: center; gap: 8px; }
.legend .sw { width: 10px; height: 10px; border-radius: 3px; flex: none; }
.legend .n { margin-left: auto; font-variant-numeric: tabular-nums; font-weight: 600; }
.avg { width: 100%; border-collapse: collapse; font-size: 15px; }
.avg th { text-align: left; font-size: 12px; font-weight: 600; color: var(--muted); padding: 0 0 6px; }
.avg th.r, .avg td.r { text-align: right; }
.avg td { padding: 9px 0; border-top: 1px solid var(--line); vertical-align: middle; }
.avg td.v { font-weight: 600; font-variant-numeric: tabular-nums; font-size: 17px; white-space: nowrap; }
.avg td.goal, .avg td.n { color: var(--muted); font-variant-numeric: tabular-nums; padding-left: 8px; white-space: nowrap; }
.avg tr.total td { font-weight: 600; }
.avg .empty-cell { color: var(--muted); font-size: 14px; }
.chart { position: relative; }
.chart svg { display: block; width: 100%; overflow: visible; touch-action: pan-y; }
.chart text { font-size: 11px; fill: var(--muted); font-variant-numeric: tabular-nums; }
.chart .ref { stroke-width: 1; stroke-dasharray: 4 3; }
.tip {
  position: absolute; pointer-events: none; background: var(--fg); color: var(--bg); font-size: 13px; line-height: 1.35;
  padding: 6px 9px; border-radius: 8px; white-space: nowrap; transform: translate(-50%, calc(-100% - 10px)); display: none; z-index: 1;
}
.tip b { font-size: 15px; }
.chart-legend { display: flex; flex-wrap: wrap; gap: 6px 14px; font-size: 13px; color: var(--muted); margin-top: 8px; }
.chart-legend span { display: inline-flex; align-items: center; gap: 6px; }
.chart-legend .dot { width: 8px; height: 8px; border-radius: 50%; }
.chart-legend .bandsw { width: 14px; height: 8px; border-radius: 2px; background: var(--band); }
.chart-legend .dash { width: 14px; border-top: 1.5px dashed; }
.lows, .metrics { list-style: none; margin: 0; padding: 0; }
.lows li { display: grid; grid-template-columns: 1fr auto; gap: 2px 8px; padding: 8px 0; }
.lows li + li, .metrics li + li { border-top: 1px solid var(--line); }
.lows .when { color: var(--muted); font-size: 14px; }
.lows .val { font-weight: 600; color: var(--low); grid-row: span 2; align-self: center; font-size: 18px; }
.lows .cmt { font-size: 14px; }
.metrics li { display: grid; grid-template-columns: 1fr auto; padding: 8px 0; }
.metrics b { text-align: right; }
.metrics small { grid-column: 1 / -1; color: var(--muted); font-size: 13px; }
.report {
  width: 100%; height: 52px; border-radius: 14px; border: 1.5px solid var(--accent); background: transparent;
  color: var(--accent); font-size: 17px; font-weight: 600; display: flex; align-items: center; justify-content: center; gap: 8px;
}
.report svg { width: 20px; height: 20px; }

/* Настройки: списки в стиле iOS */
.group-h { font-size: 13px; font-weight: 600; text-transform: uppercase; letter-spacing: .04em; color: var(--muted); margin: 20px 4px 8px; }
.foot { font-size: 13px; color: var(--muted); margin: 8px 4px 0; }
.list { background: var(--card); border-radius: 14px; box-shadow: var(--shadow); overflow: hidden; }
.item { display: flex; align-items: center; gap: 10px; width: 100%; min-height: 52px; padding: 10px 14px; border: none; background: transparent; text-align: left; }
.item + .item { border-top: 1px solid var(--line); }
.item .main { flex: 1; min-width: 0; }
.item .main small { display: block; color: var(--muted); font-size: 13px; }
.item .val { color: var(--muted); font-variant-numeric: tabular-nums; white-space: nowrap; }
.chev { width: 8px; height: 13px; color: var(--muted); flex: none; opacity: .6; }
.item.add { color: var(--accent); font-weight: 600; }
.item.add svg { width: 20px; height: 20px; }
button.item:active { background: var(--chip); }
.status { font-size: 12px; font-weight: 600; padding: 2px 8px; border-radius: 999px; white-space: nowrap; }
.status.on { color: var(--ok); background: var(--ok-bg); }
.status.planned { color: var(--muted); background: var(--chip); }
.status.paused { color: var(--high); background: var(--high-bg); }
.status.off { color: var(--muted); border: 1px solid var(--line); }
.warn { display: flex; gap: 10px; background: var(--high-bg); color: var(--fg); border-radius: 14px; padding: 12px 14px; font-size: 14px; margin-bottom: 8px; }
.warn svg { width: 20px; height: 20px; color: var(--high); flex: none; margin-top: 1px; }

/* Нижнее окно */
.scrim { position: fixed; inset: 0; background: rgba(0,0,0,.35); opacity: 0; pointer-events: none; transition: opacity .2s; z-index: 4; }
.scrim.open { opacity: 1; pointer-events: auto; }
.sheet {
  position: fixed; left: 0; right: 0; bottom: 0; max-width: 430px; margin: 0 auto; z-index: 5;
  background: var(--bg); border-radius: 22px 22px 0 0; padding: 10px 16px calc(env(safe-area-inset-bottom) + 16px);
  transform: translateY(105%); transition: transform .28s cubic-bezier(.2,.8,.2,1); max-height: 92vh; overflow-y: auto;
  overscroll-behavior: contain;
}
.sheet.open { transform: none; }
.grabber { width: 36px; height: 5px; border-radius: 3px; background: var(--line); margin: 0 auto 10px; }
.sheet-head { display: flex; justify-content: space-between; align-items: center; gap: 8px; }
.sheet-head h3 { margin: 0; font-size: 18px; text-align: center; flex: 1; min-width: 0; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.head-spacer { width: 64px; }
.link { background: none; border: none; color: var(--accent); font-size: 16px; min-height: 44px; padding: 0 4px; min-width: 64px; text-align: left; }
.link.r { text-align: right; font-weight: 600; }
.link.danger { color: var(--low); min-width: 0; font-size: 15px; }
.big-input { display: flex; align-items: baseline; justify-content: center; gap: 8px; margin: 12px 0 4px; }
.big-input b { font-size: 32px; color: var(--muted); font-weight: 400; }
.big-input input {
  width: 150px; text-align: center; font-size: 56px; font-weight: 600; border: none; background: transparent; color: var(--fg);
  border-bottom: 2px solid var(--line); padding: 0 0 4px; font-variant-numeric: tabular-nums; border-radius: 0;
}
.big-input span { color: var(--muted); }
.verdict { text-align: center; min-height: 28px; margin-bottom: 8px; }
label.lbl { display: block; font-size: 13px; color: var(--muted); margin: 14px 0 6px; font-weight: 600; }
label.lbl .opt { font-weight: 400; }
.chips { display: flex; flex-wrap: wrap; gap: 8px; }
.chips button { min-height: 40px; padding: 0 12px; border-radius: 10px; border: 1.5px solid var(--line); background: var(--card); font-size: 15px; }
.chips button[aria-pressed="true"] { border-color: var(--accent); background: var(--accent); color: var(--on-accent); }
.when { display: grid; grid-template-columns: 1fr 1fr; gap: 8px; }
.when input, .range input, textarea {
  width: 100%; min-height: 44px; border-radius: 10px; border: 1.5px solid var(--line); background: var(--card);
  color: var(--fg); font: inherit; padding: 8px 10px; -webkit-appearance: none; appearance: none;
}
textarea { resize: none; height: 72px; display: block; }
textarea.tall { height: 140px; }
.fields { background: var(--card); border-radius: 14px; overflow: hidden; margin-top: 12px; }
.fields.mt0 { margin-top: 0; }
/* Обхваты — сетка по два поля в ряд, последнее нечётное на всю ширину */
.girths { display: grid; grid-template-columns: 1fr 1fr; }
.girths .field { border-top: 1px solid var(--line); }
.girths .field:nth-child(-n+2) { border-top: none; }
.girths .field:nth-child(even) { border-left: 1px solid var(--line); }
.girths .field:last-child:nth-child(odd) { grid-column: 1 / -1; }
.field { display: block; padding: 8px 14px; }
.field + .field, .row3 + .field, .field + .row3 { border-top: 1px solid var(--line); }
.field span { display: block; font-size: 12px; color: var(--muted); font-weight: 600; }
.field input { width: 100%; border: none; background: transparent; color: var(--fg); font: inherit; padding: 2px 0; min-height: 30px; }
.field .ro { min-height: 30px; padding: 2px 0; }
.row2, .row3 { display: grid; }
.row2 { grid-template-columns: 1fr 1fr; }
.row3 { grid-template-columns: 1fr 1fr 1fr; }
.row2 .field + .field, .row3 .field + .field { border-top: none; border-left: 1px solid var(--line); }
.period { background: var(--card); border-radius: 14px; margin-top: 8px; overflow: hidden; display: none; }
.period.show { display: block; }
.period .pad { padding: 10px 14px; }
.period .field { border-top: 1px solid var(--line); }
.save { width: 100%; height: 52px; border-radius: 14px; border: none; background: var(--accent); color: var(--on-accent); font-size: 17px; font-weight: 600; margin-top: 16px; }
.save.small { height: 44px; margin: 0; font-size: 16px; }
.delete { width: 100%; height: 48px; border-radius: 14px; border: none; background: transparent; color: var(--low); font-size: 16px; margin-top: 6px; }

/* Окно подтверждения */
.confirm { position: fixed; inset: 0; z-index: 8; display: none; align-items: center; justify-content: center; padding: 24px; background: rgba(0,0,0,.4); }
.confirm.open { display: flex; }
.confirm-box { background: var(--card); border-radius: 16px; width: 100%; max-width: 320px; overflow: hidden; box-shadow: 0 10px 40px rgba(0,0,0,.25); }
.confirm-box p { margin: 0; padding: 20px 18px; text-align: center; font-size: 16px; }
.confirm-btns { display: grid; grid-template-columns: 1fr 1fr; border-top: 1px solid var(--line); }
.confirm-btns button { min-height: 48px; border: none; background: transparent; font-size: 17px; color: var(--accent); }
.confirm-btns button + button { border-left: 1px solid var(--line); }
.confirm-btns .danger { color: var(--low); font-weight: 600; }

.toast {
  position: fixed; left: 50%; top: calc(env(safe-area-inset-top) + 12px); transform: translate(-50%, -150%);
  background: var(--fg); color: var(--bg); padding: 10px 16px; border-radius: 12px; font-size: 15px; z-index: 10;
  transition: transform .25s; max-width: calc(100% - 32px); text-align: center;
}
.toast.show { transform: translate(-50%, 0); }

@media (prefers-reduced-motion: reduce) { .sheet, .scrim, .toast { transition: none; } }
