:root {
  --bg: #f6f4fb;
  --surface: #ffffff;
  --surface-2: #f0ecfa;
  --text: #1d1530;
  --muted: #74698d;
  --line: #e6e0f3;
  --accent: #7c3aed;
  --accent-2: #a78bfa;
  --accent-soft: #ede7fd;
  --on-accent: #ffffff;
  --p: #7c3aed;
  --f: #c026d3;
  --c: #6366f1;
  --over: #e11d48;
  --track: #e9e3f7;
  --head-a: #6d28d9;
  --head-b: #8b5cf6;
  --shadow: 0 8px 30px rgba(76, 29, 149, .12);
  --radius: 18px;
}
@media (prefers-color-scheme: dark) {
  :root {
    --bg: #120e1c;
    --surface: #1c1629;
    --surface-2: #251d36;
    --text: #eee9fa;
    --muted: #a397bd;
    --line: #2f2645;
    --accent: #a78bfa;
    --accent-2: #c4b5fd;
    --accent-soft: #2c2142;
    --on-accent: #1a1029;
    --p: #a78bfa;
    --f: #e879f9;
    --c: #818cf8;
    --over: #fb7185;
    --track: #2f2645;
    --head-a: #2e1065;
    --head-b: #4c1d95;
    --shadow: 0 8px 30px rgba(0, 0, 0, .45);
  }
}

* { box-sizing: border-box; }
html { -webkit-text-size-adjust: 100%; }
body {
  margin: 0;
  background: var(--bg);
  color: var(--text);
  font: 15px/1.45 system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
  padding-bottom: calc(96px + env(safe-area-inset-bottom));
}
body.locked { overflow: hidden; }
body.locked .fab-row { display: none; }
button, input { font: inherit; color: inherit; }
button { cursor: pointer; border: 0; background: none; -webkit-tap-highlight-color: transparent; }
[hidden] { display: none !important; }

/* ---------- Шапка ---------- */
.top {
  background: linear-gradient(135deg, var(--head-a), var(--head-b));
  color: #fff;
  padding: calc(10px + env(safe-area-inset-top)) 16px 18px;
  border-radius: 0 0 28px 28px;
}
.top { position: relative; overflow: hidden; }
.top-inner { max-width: 760px; margin: 0 auto; position: relative; z-index: 1; }
/* Звёздное небо в тёмной теме: два слоя звёзд, второй мерцает */
@media (prefers-color-scheme: dark) {
  .top::before, .top::after { content: ""; position: absolute; inset: 0; pointer-events: none; }
  .top::before {
    background:
      radial-gradient(circle 1px at 8% 22%, #fff 98%, transparent),
      radial-gradient(circle 1.2px at 23% 78%, #f5d0fe 98%, transparent),
      radial-gradient(circle 1px at 37% 12%, #fff 98%, transparent),
      radial-gradient(circle 1.5px at 52% 64%, #e9d5ff 98%, transparent),
      radial-gradient(circle 1px at 66% 30%, #fff 98%, transparent),
      radial-gradient(circle 1.2px at 81% 82%, #fff 98%, transparent),
      radial-gradient(circle 1px at 93% 18%, #f5d0fe 98%, transparent),
      radial-gradient(circle 1px at 4% 92%, #fff 98%, transparent),
      radial-gradient(circle 1px at 72% 52%, #fff 98%, transparent);
    opacity: .7;
  }
  .top::after {
    background:
      radial-gradient(circle 1.4px at 15% 48%, #fff 98%, transparent),
      radial-gradient(circle 1px at 45% 88%, #e9d5ff 98%, transparent),
      radial-gradient(circle 1.6px at 60% 8%, #fff 98%, transparent),
      radial-gradient(circle 1px at 88% 58%, #f5d0fe 98%, transparent),
      radial-gradient(circle 1.2px at 31% 40%, #fff 98%, transparent);
    animation: twinkle 4s ease-in-out infinite alternate;
  }
}
@keyframes twinkle { from { opacity: .15; } to { opacity: .9; } }
@media (prefers-reduced-motion: reduce) { .top::after { animation: none; } }
.daynav { display: flex; align-items: center; gap: 4px; margin-bottom: 10px; }
.icon-btn {
  width: 38px; height: 38px; border-radius: 12px;
  display: grid; place-items: center; font-size: 22px; color: inherit;
}
.icon-btn:hover { background: rgba(255,255,255,.14); }
.day-label { font-weight: 600; font-size: 16px; padding: 6px 10px; border-radius: 10px; color: #fff; }
.day-label:hover { background: rgba(255,255,255,.14); }
.settings { margin-left: auto; position: relative; }
.settings.warn::after { content: ""; position: absolute; top: 7px; right: 7px; width: 8px; height: 8px; border-radius: 50%; background: #fda4af; }
.btn:disabled { opacity: .6; cursor: default; }

.summary { display: grid; grid-template-columns: auto 1fr; gap: 20px; align-items: center; cursor: pointer; }
.ring { position: relative; width: 124px; height: 124px; }
/* Кольцо калорий — аккреционный диск вокруг чёрной дыры */
.ring svg { width: 100%; height: 100%; overflow: visible; }
.ring .bg { stroke: rgba(255,255,255,.16); }
.ring .flow { stroke: rgba(255,255,255,.55); animation: flow 2.4s linear infinite; }
.ring .photon { stroke: rgba(243,232,255,.85); }
.ring .hole { fill: #05010d; }
.ring.over .hole { fill: #12000a; }
@keyframes flow { to { stroke-dashoffset: -21; } }
@media (prefers-reduced-motion: reduce) { .ring .flow { animation: none; } }
.ring-text { position: absolute; inset: 0; display: grid; place-content: center; text-align: center; color: #fff; }
.ring-text b { font-size: 26px; line-height: 1.1; font-variant-numeric: tabular-nums; }
.ring-text span { font-size: 12px; opacity: .85; }
.macros { display: grid; gap: 10px; }
.left-line { font-size: 13px; opacity: .9; margin-bottom: 2px; }
.left-line b { font-weight: 700; }
.macro-row .lbl { display: flex; justify-content: space-between; font-size: 13px; margin-bottom: 4px; }
.macro-row .lbl b { font-variant-numeric: tabular-nums; }
.bar { height: 8px; border-radius: 99px; background: rgba(255,255,255,.2); overflow: hidden; }
.bar i { display: block; height: 100%; border-radius: inherit; background: #fff; transition: width .5s ease; }
.bar.over i { background: #fda4af; }

/* ---------- Вкладки ---------- */
.tabs {
  max-width: 760px; margin: 14px auto 6px; padding: 4px; display: flex; gap: 4px;
  background: var(--surface); border-radius: 14px; box-shadow: var(--shadow);
  position: sticky; top: 8px; z-index: 5;
  margin-left: max(16px, calc(50% - 380px)); margin-right: max(16px, calc(50% - 380px));
}
.tab { flex: 1; padding: 10px 6px; border-radius: 10px; font-weight: 600; color: var(--muted); font-size: 14px; }
.tab.active { background: var(--accent); color: var(--on-accent); }

main { max-width: 760px; margin: 0 auto; padding: 8px 16px; }
.panel h3 { font-size: 13px; text-transform: uppercase; letter-spacing: .06em; color: var(--muted); margin: 18px 4px 8px; font-weight: 600; }

/* ---------- Списки ---------- */
.list { background: var(--surface); border-radius: var(--radius); overflow: hidden; box-shadow: var(--shadow); }
.item {
  display: flex; align-items: center; gap: 12px; width: 100%; text-align: left;
  padding: 12px 14px; border-bottom: 1px solid var(--line);
}
.item:last-child { border-bottom: 0; }
.item:hover { background: var(--surface-2); }
.item .main { flex: 1; min-width: 0; }
.item .name { font-weight: 600; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.item .sub { font-size: 12.5px; color: var(--muted); overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.item .kcal { font-weight: 700; white-space: nowrap; font-variant-numeric: tabular-nums; }
.item .kcal small { font-weight: 500; color: var(--muted); }
.badge { font-size: 11px; padding: 2px 7px; border-radius: 99px; background: var(--accent-soft); color: var(--accent); font-weight: 600; margin-left: 6px; vertical-align: 1px; }
.x-btn { width: 32px; height: 32px; border-radius: 10px; color: var(--muted); font-size: 18px; flex: none; }
.x-btn:hover { background: var(--accent-soft); color: var(--accent); }

.empty { text-align: center; color: var(--muted); padding: 34px 20px; background: var(--surface); border-radius: var(--radius); box-shadow: var(--shadow); }
.empty b { display: block; color: var(--text); font-size: 16px; margin-bottom: 4px; }

.day-total { display: flex; justify-content: space-between; color: var(--muted); font-size: 13px; padding: 10px 6px 0; }

.recipe-card { background: var(--surface); border-radius: var(--radius); box-shadow: var(--shadow); padding: 14px; margin-bottom: 10px; }
.recipe-card .name { font-weight: 700; font-size: 16px; }
.recipe-card .sub { color: var(--muted); font-size: 13px; margin: 2px 0 10px; }
.row { display: flex; gap: 8px; flex-wrap: wrap; }

/* ---------- Кнопки, поля ---------- */
.btn {
  display: inline-flex; align-items: center; justify-content: center; gap: 8px;
  padding: 11px 16px; border-radius: 12px; font-weight: 600;
  background: var(--accent); color: var(--on-accent);
}
.btn:hover { filter: brightness(1.06); }
.btn.soft { background: var(--accent-soft); color: var(--accent); }
.btn.danger { background: transparent; color: var(--over); }
.btn.block { width: 100%; }
.btn.small { padding: 8px 12px; font-size: 14px; }

.field { display: block; margin-bottom: 12px; }
.field > span { display: block; font-size: 13px; color: var(--muted); margin-bottom: 5px; }
.input {
  width: 100%; padding: 12px 14px; border-radius: 12px; border: 1.5px solid var(--line);
  background: var(--surface); outline: none; font-size: 16px;
}
.input:focus { border-color: var(--accent); }
.grid2 { display: grid; grid-template-columns: 1fr 1fr; gap: 10px; }
.grid4 { display: grid; grid-template-columns: repeat(4, 1fr); gap: 8px; }
.chips { display: flex; gap: 8px; flex-wrap: wrap; margin: 4px 0 14px; }
.chip { padding: 7px 12px; border-radius: 99px; background: var(--accent-soft); color: var(--accent); font-weight: 600; font-size: 14px; }
.hint { font-size: 13px; color: var(--muted); margin: -4px 0 12px; }

/* ---------- Плавающие кнопки ---------- */
.fab-row {
  position: fixed; left: 0; right: 0; bottom: calc(18px + env(safe-area-inset-bottom));
  display: flex; justify-content: center; gap: 10px; pointer-events: none; z-index: 10;
}
.fab {
  pointer-events: auto; display: inline-flex; align-items: center; gap: 8px;
  height: 54px; padding: 0 24px; border-radius: 99px; font-weight: 700; font-size: 16px;
  background: var(--accent); color: var(--on-accent); box-shadow: 0 10px 28px rgba(109, 40, 217, .4);
}
.fab.ghost { width: 54px; padding: 0; justify-content: center; background: var(--surface); color: var(--accent); }

/* ---------- Нижние листы / диалоги ---------- */
.overlay {
  position: fixed; inset: 0; z-index: 20; background: rgba(20, 10, 40, .45);
  display: flex; align-items: flex-end; justify-content: center;
}
.sheet {
  width: 100%; max-width: 560px; max-height: 92vh; max-height: 92dvh;
  background: var(--bg); border-radius: 24px 24px 0 0;
  display: flex; flex-direction: column;
  animation: sheet-in .2s ease;
}
@keyframes sheet-in { from { transform: translateY(30px); opacity: 0; } }
.sheet:not(:last-child) { display: none; }
.sheet-head { display: flex; align-items: center; gap: 8px; padding: 16px 16px 8px; }
.sheet-head h2 { font-size: 18px; margin: 0; flex: 1; min-width: 0; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.sheet-head .icon-btn { color: var(--muted); }
.sheet-head .icon-btn:hover { background: var(--accent-soft); }
.sheet-body { padding: 4px 16px calc(20px + env(safe-area-inset-bottom)); overflow-y: auto; }
.sheet-foot { padding: 10px 16px calc(14px + env(safe-area-inset-bottom)); display: flex; gap: 8px; border-top: 1px solid var(--line); }
.sheet-foot .btn { flex: 1; }
.sheet-body { flex: 1; overscroll-behavior: contain; }
/* Поиск и рецепт — постоянной высоты, чтобы окно не прыгало, пока подгружаются результаты */
.sheet.full { height: 100%; max-height: none; border-radius: 0; padding-top: env(safe-area-inset-top); }
@media (min-width: 700px) {
  .overlay { align-items: center; }
  .sheet { border-radius: 24px; max-height: 86vh; }
  .sheet.full { height: 86vh; border-radius: 24px; padding-top: 0; }
}

.search { position: relative; display: flex; gap: 8px; margin-bottom: 6px; }
.sheet.full .search { position: sticky; top: -4px; z-index: 1; background: var(--bg); padding: 4px 0 8px; margin-top: -4px; }
.search .input { padding-left: 40px; }
.search svg.lens { position: absolute; left: 13px; top: 50%; transform: translateY(-50%); color: var(--muted); pointer-events: none; }
.search .btn { flex: none; width: 50px; padding: 0; }

.loading { color: var(--muted); font-size: 14px; padding: 14px; text-align: center; }
.spinner { display: inline-block; width: 14px; height: 14px; border: 2px solid var(--accent-2); border-top-color: transparent; border-radius: 50%; animation: spin .8s linear infinite; vertical-align: -2px; margin-right: 6px; }
@keyframes spin { to { transform: rotate(360deg); } }

.calc { background: var(--surface); border-radius: var(--radius); padding: 14px; box-shadow: var(--shadow); margin-bottom: 14px; }
.calc .big { font-size: 30px; font-weight: 800; font-variant-numeric: tabular-nums; }
.calc .big small { font-size: 15px; font-weight: 600; color: var(--muted); }
.mac3 { display: flex; gap: 14px; font-size: 14px; color: var(--muted); }
.mac3 b { color: var(--text); font-variant-numeric: tabular-nums; }
.dot { display: inline-block; width: 8px; height: 8px; border-radius: 50%; margin-right: 5px; }
.per100 { font-size: 13px; color: var(--muted); margin: -2px 0 12px; }

.ing { display: flex; align-items: center; gap: 8px; padding: 10px 12px; border-bottom: 1px solid var(--line); }
.ing:last-child { border-bottom: 0; }
.ing .main { flex: 1; min-width: 0; }
.ing .input { width: 84px; padding: 8px 10px; text-align: right; }

#reader { width: 100%; min-height: 220px; border-radius: 16px; overflow: hidden; background: #000; margin-bottom: 12px; }
#reader video { width: 100% !important; object-fit: cover; }

.toast {
  position: fixed; left: 50%; bottom: calc(90px + env(safe-area-inset-bottom)); transform: translateX(-50%);
  background: var(--text); color: var(--bg); padding: 10px 16px; border-radius: 12px; z-index: 40;
  font-weight: 600; font-size: 14px; max-width: calc(100% - 32px); box-shadow: var(--shadow);
}

@media (max-width: 420px) {
  .summary { gap: 14px; }
  .ring { width: 108px; height: 108px; }
  .ring-text b { font-size: 22px; }
  .ring-text span { font-size: 10.5px; }
  .grid4 { grid-template-columns: 1fr 1fr; }
}
