/* Токены палитры взяты из constants/colors.ts исходного приложения.
   Типографика увеличена: база 17px вместо 15 — дневник читают в 57 лет,
   часто без очков и при плохом свете. Тач-таргеты 56px вместо 44. */

:root {
  --bg: #f4f4f8;
  --card: #ffffff;
  --text: #1a1a2e;
  --muted: #5c6470;
  --border: #e0e3e9;
  --input: #eceef3;

  --primary: #ff6b35;
  --primary-text: #cd3b00;
  --on-primary: #1a1a2e;

  --ok: #22c55e;
  --ok-text: #15804f;
  --warn: #ffb800;
  --warn-text: #8a5200;
  --danger: #ef4444;
  --danger-text: #c41c23;
  --info: #3b82f6;
  --info-text: #1f71c6;

  /* Шкала сахара — смысловая ось приложения */
  --gl-low: #c41c23;
  --gl-ok: #15804f;
  --gl-high: #a36000;
  --gl-vhigh: #cd3b00;

  --r-sm: 8px;
  --r: 12px;
  --r-lg: 20px;
  --r-full: 999px;

  --pad: 18px;
  --gap: 14px;

  --shadow: 0 1px 2px rgba(26, 26, 46, 0.06), 0 4px 12px rgba(26, 26, 46, 0.04);
  --tap: 56px;
}

@media (prefers-color-scheme: dark) {
  :root {
    --bg: #0d1117;
    --card: #161b22;
    --text: #e6edf3;
    --muted: #9aa4b0;
    --border: #30363d;
    --input: #21262d;
    --primary-text: #ff8a5c;
    --ok-text: #3dd68c;
    --warn-text: #e0a53a;
    --danger-text: #ff7b72;
    --info-text: #58a6ff;
    --gl-low: #ff7b72;
    --gl-ok: #3dd68c;
    --gl-high: #e0a53a;
    --gl-vhigh: #ff8a5c;
    --shadow: none;
  }
}

* { box-sizing: border-box; }

html { -webkit-text-size-adjust: 100%; }

body {
  margin: 0;
  background: var(--bg);
  color: var(--text);
  font: 400 17px/1.45 -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto,
    "Helvetica Neue", Arial, sans-serif;
  font-variant-numeric: tabular-nums;
  padding-bottom: calc(72px + env(safe-area-inset-bottom));
  overscroll-behavior-y: none;
}

h1, h2, h3 { margin: 0; line-height: 1.2; }
h1 { font-size: 27px; font-weight: 800; letter-spacing: -0.4px; }
h2 { font-size: 20px; font-weight: 700; }
h3 { font-size: 17px; font-weight: 700; }
p { margin: 0 0 10px; }

button, input, select, textarea {
  font: inherit;
  color: inherit;
  font-variant-numeric: tabular-nums;
}

:focus-visible { outline: 3px solid var(--info); outline-offset: 2px; }

/* ── Каркас ────────────────────────────────────────────────────────────── */

.wrap { max-width: 720px; margin: 0 auto; padding: 0 var(--pad); }

.topbar {
  position: sticky; top: 0; z-index: 20;
  background: var(--bg);
  padding: calc(12px + env(safe-area-inset-top)) 0 14px;
  border-bottom: 1px solid transparent;
}
.topbar.stuck { border-bottom-color: var(--border); }
.topbar-inner { display: flex; align-items: center; gap: 10px; }
.topbar h1 { flex: 1; min-width: 0; }

.tabbar {
  position: fixed; left: 0; right: 0; bottom: 0; z-index: 30;
  display: grid; grid-template-columns: repeat(5, 1fr);
  background: var(--card);
  border-top: 1px solid var(--border);
  padding-bottom: env(safe-area-inset-bottom);
}
.tabbar button {
  background: none; border: 0; cursor: pointer;
  padding: 8px 2px 10px;
  display: flex; flex-direction: column; align-items: center; gap: 3px;
  color: var(--muted); font-size: 11px; font-weight: 600;
  min-height: 62px;
}
.tabbar button svg { width: 24px; height: 24px; }
.tabbar button[aria-current="page"] { color: var(--primary-text); }

/* ── Карточки ──────────────────────────────────────────────────────────── */

.card {
  background: var(--card);
  border-radius: var(--r);
  padding: var(--pad);
  box-shadow: var(--shadow);
  margin-bottom: var(--gap);
}
.card > h2, .card > h3 { margin-bottom: 14px; }

.card-head { display: flex; align-items: baseline; gap: 8px; margin-bottom: 14px; }
.card-head h2, .card-head h3 { flex: 1; }
.card-head .sub { color: var(--muted); font-size: 14px; }

.section-title {
  font-size: 14px; font-weight: 700; color: var(--muted);
  margin: 26px 0 10px;
}

/* ── Крупные значения ──────────────────────────────────────────────────── */

.stat-grid { display: grid; grid-template-columns: repeat(2, 1fr); gap: var(--gap); margin-bottom: var(--gap); }
.stat-grid.three { grid-template-columns: repeat(3, 1fr); }

.stat { background: var(--card); border-radius: var(--r); padding: 16px; box-shadow: var(--shadow); }
.stat .label { font-size: 13px; color: var(--muted); font-weight: 600; }
.stat .value { font-size: 30px; font-weight: 800; letter-spacing: -1px; line-height: 1.15; margin-top: 7px; }
.stat .value small { font-size: 15px; font-weight: 600; color: var(--muted); letter-spacing: 0; }
.stat .foot { font-size: 13px; color: var(--muted); margin-top: 6px; }

.value.low { color: var(--gl-low); }
.value.ok { color: var(--gl-ok); }
.value.high { color: var(--gl-high); }
.value.vhigh { color: var(--gl-vhigh); }

/* ── Кольца и полосы прогресса ─────────────────────────────────────────── */

.rings { display: flex; gap: 18px; align-items: center; }
.ring { flex: 0 0 auto; }
.ring-legend { flex: 1; display: grid; gap: 12px; }
.legend-row { display: flex; align-items: center; gap: 8px; font-size: 15px; }
.legend-row .dot { width: 11px; height: 11px; border-radius: 50%; flex: 0 0 auto; }
.legend-row .name { flex: 1; }
.legend-row .num { font-weight: 700; }

.bar { height: 10px; background: var(--input); border-radius: var(--r-full); overflow: hidden; }
.bar > span { display: block; height: 100%; border-radius: var(--r-full); background: var(--primary); }
.bar.over > span { background: var(--danger); }

.macro { display: grid; gap: 12px; margin-top: 14px; }
.macro-row { display: grid; grid-template-columns: 92px 1fr auto; gap: 10px; align-items: center; font-size: 15px; }
.macro-row .cap { color: var(--muted); font-weight: 600; }
.macro-row .num { font-weight: 700; font-size: 15px; }

/* ── Лента дня ─────────────────────────────────────────────────────────── */

.timeline { position: relative; padding-left: 36px; }
.timeline::before {
  content: ""; position: absolute; left: 11px; top: 6px; bottom: 6px;
  width: 2px; background: var(--border);
}
.tl-item { position: relative; padding: 15px 0; display: flex; gap: 12px; align-items: flex-start; }
.tl-item + .tl-item { border-top: 1px solid var(--border); }
.tl-dot {
  position: absolute; left: -30px; top: 20px;
  width: 16px; height: 16px; border-radius: 50%;
  border: 3px solid var(--card); box-sizing: content-box;
}
.tl-time { font-size: 14px; font-weight: 700; color: var(--muted); width: 46px; flex: 0 0 auto; padding-top: 3px; }
.tl-body { flex: 1; min-width: 0; }
.tl-title { font-weight: 600; }
.tl-sub { font-size: 14px; color: var(--muted); margin-top: 2px; }
.tl-val { font-weight: 800; font-size: 19px; white-space: nowrap; }
.tl-del {
  background: none; border: 0; color: var(--muted); cursor: pointer;
  padding: 6px; margin: -6px -6px -6px 0; border-radius: var(--r-sm);
  min-width: 34px; min-height: 34px;
}
.tl-del:hover { background: var(--input); color: var(--danger-text); }

/* ── Кнопки ────────────────────────────────────────────────────────────── */

.btn {
  display: inline-flex; align-items: center; justify-content: center; gap: 8px;
  min-height: var(--tap); padding: 0 18px;
  border: 0; border-radius: var(--r); cursor: pointer;
  background: var(--input); color: var(--text);
  font-size: 17px; font-weight: 700;
  text-decoration: none;
}
.btn:active { transform: scale(0.985); }
.btn.primary { background: var(--primary); color: var(--on-primary); }
.btn.ghost { background: transparent; border: 1.5px solid var(--border); }
.btn.danger { background: transparent; color: var(--danger-text); border: 1.5px solid var(--border); }
.btn.block { display: flex; width: 100%; }
.btn.sm { min-height: 42px; font-size: 15px; padding: 0 14px; border-radius: var(--r-sm); }
.btn[disabled] { opacity: 0.5; cursor: default; }

.quick { display: grid; grid-template-columns: repeat(2, 1fr); gap: var(--gap); margin-bottom: var(--gap); }
.quick .btn { flex-direction: column; gap: 7px; min-height: 88px; font-size: 15px; padding: 14px 10px; }
.quick .btn svg { width: 24px; height: 24px; }
.quick .btn span { font-weight: 700; }

.row-actions { display: flex; gap: 10px; flex-wrap: wrap; }
.row-actions .btn { flex: 1; min-width: 130px; }

/* ── Формы ─────────────────────────────────────────────────────────────── */

.field { margin-bottom: 14px; }
.field label { display: block; font-size: 14px; font-weight: 700; color: var(--muted); margin-bottom: 6px; }
.field .hint { font-size: 13px; color: var(--muted); font-weight: 400; margin-top: 5px; }

input[type="text"], input[type="password"], input[type="number"], input[type="date"],
input[type="time"], select, textarea {
  width: 100%; min-height: var(--tap);
  background: var(--input); border: 1.5px solid transparent;
  border-radius: var(--r); padding: 10px 14px;
  font-size: 17px;
}
textarea { min-height: 84px; resize: vertical; }
input:focus, select:focus, textarea:focus { border-color: var(--primary); outline: none; }

.field-row { display: grid; grid-template-columns: 1fr 1fr; gap: 10px; }
.field-row.three { grid-template-columns: repeat(3, 1fr); }

.chips { display: flex; flex-wrap: wrap; gap: 8px; }
.chip {
  border: 1.5px solid var(--border); background: var(--card);
  border-radius: var(--r-full); padding: 11px 15px;
  font-size: 15px; font-weight: 600; cursor: pointer; min-height: 46px;
}
.chip[aria-pressed="true"] { background: var(--primary); border-color: var(--primary); color: var(--on-primary); }

/* ── Списки ────────────────────────────────────────────────────────────── */

.list { display: grid; gap: 1px; background: var(--border); border-radius: var(--r); overflow: hidden; }
.list-item {
  background: var(--card); padding: 15px 16px;
  display: flex; align-items: center; gap: 12px; text-align: left;
  border: 0; width: 100%; cursor: pointer; min-height: var(--tap);
}
.list-item .grow { flex: 1; min-width: 0; }
.list-item .name { font-weight: 600; }
.list-item .meta { font-size: 13px; color: var(--muted); }

/* Отметка приёма препарата */
.med-item { display: flex; align-items: center; gap: 14px; background: var(--card); padding: 15px 16px; min-height: var(--tap); }
.med-check {
  width: 34px; height: 34px; flex: 0 0 auto; border-radius: 50%;
  border: 2.5px solid var(--border); background: var(--card);
  cursor: pointer; display: grid; place-items: center; padding: 0;
}
.med-check[data-status="taken"] { background: var(--ok); border-color: var(--ok); }
.med-check[data-status="skipped"] { background: var(--warn); border-color: var(--warn); }
.med-check svg { width: 20px; height: 20px; color: #1a1a2e; }
.med-item.taken .name { color: var(--muted); }

/* ── Бейджи и уведомления ──────────────────────────────────────────────── */

.badge {
  display: inline-block; font-size: 12px; font-weight: 700;
  padding: 3px 9px; border-radius: var(--r-full);
  background: var(--input); color: var(--muted);
}
.badge.low { background: rgba(196, 28, 35, 0.14); color: var(--gl-low); }
.badge.ok { background: rgba(21, 128, 79, 0.14); color: var(--gl-ok); }
.badge.high { background: rgba(163, 96, 0, 0.16); color: var(--gl-high); }
.badge.vhigh { background: rgba(205, 59, 0, 0.14); color: var(--gl-vhigh); }

.alert {
  border-radius: var(--r); padding: 15px 17px; margin-bottom: var(--gap);
  font-size: 15px; line-height: 1.4;
  border-left: 5px solid var(--muted); background: var(--card);
}
.alert.danger { border-left-color: var(--danger); }
.alert.warn { border-left-color: var(--warn); }
.alert.info { border-left-color: var(--info); }
.alert strong { display: block; margin-bottom: 3px; }

.toast {
  position: fixed; left: 50%; bottom: calc(84px + env(safe-area-inset-bottom));
  transform: translateX(-50%);
  background: var(--text); color: var(--bg);
  padding: 13px 20px; border-radius: var(--r-full);
  font-size: 15px; font-weight: 600; z-index: 60;
  max-width: calc(100% - 32px); text-align: center;
  box-shadow: 0 8px 24px rgba(0, 0, 0, 0.2);
}

.empty { text-align: center; color: var(--muted); padding: 28px 16px; font-size: 15px; }

/* ── Модальные окна ────────────────────────────────────────────────────── */

.sheet-backdrop {
  position: fixed; inset: 0; z-index: 50;
  background: rgba(10, 12, 18, 0.45);
  display: flex; align-items: flex-end; justify-content: center;
}
.sheet {
  background: var(--bg);
  width: 100%; max-width: 720px;
  max-height: 92vh; overflow-y: auto;
  border-radius: var(--r-lg) var(--r-lg) 0 0;
  padding: 8px var(--pad) calc(24px + env(safe-area-inset-bottom));
}
.sheet-grip { width: 40px; height: 4px; background: var(--border); border-radius: 2px; margin: 6px auto 12px; }
.sheet-head { display: flex; align-items: center; gap: 10px; margin-bottom: 14px; }
.sheet-head h2 { flex: 1; }
.sheet-close {
  background: var(--input); border: 0; border-radius: 50%;
  width: 40px; height: 40px; cursor: pointer; font-size: 22px; line-height: 1;
  color: var(--muted);
}

@media (prefers-reduced-motion: no-preference) {
  .sheet { animation: rise 0.18s ease-out; }
  @keyframes rise { from { transform: translateY(14px); opacity: 0.6; } }
}

/* ── Графики ───────────────────────────────────────────────────────────── */

.chart { width: 100%; display: block; overflow: visible; }
.chart text { font-size: 11px; fill: var(--muted); font-weight: 600; }
.chart .grid { stroke: var(--border); stroke-width: 1; }
.chart .band { fill: var(--ok); opacity: 0.1; }
.chart .line { fill: none; stroke: var(--primary); stroke-width: 2.5; stroke-linejoin: round; stroke-linecap: round; }
.chart .area { fill: var(--primary); opacity: 0.1; }

.tir { display: flex; height: 40px; border-radius: var(--r-sm); overflow: hidden; }
.tir > div { display: grid; place-items: center; font-size: 14px; font-weight: 800; color: #1a1a2e; }
.tir .b-low { background: var(--danger); }
.tir .b-ok { background: var(--ok); }
.tir .b-high { background: var(--warn); }

.kv { display: grid; gap: 12px; }
.kv-row { display: flex; align-items: baseline; gap: 10px; font-size: 15px; }
.kv-row .k { color: var(--muted); flex: 1; }
.kv-row .v { font-weight: 700; }

table.data { width: 100%; border-collapse: collapse; font-size: 15px; }
table.data th, table.data td { text-align: left; padding: 9px 6px; border-bottom: 1px solid var(--border); }
table.data th { color: var(--muted); font-size: 13px; font-weight: 700; }
table.data td.num, table.data th.num { text-align: right; }

/* ── Экран входа ───────────────────────────────────────────────────────── */

.auth { min-height: 100vh; display: flex; align-items: center; justify-content: center; padding: 24px 16px; }
.auth-box { width: 100%; max-width: 420px; }
.auth-logo { text-align: center; margin-bottom: 28px; }
.auth-logo .mark {
  width: 62px; height: 62px; border-radius: 18px; margin: 0 auto 14px;
  background: var(--primary); display: grid; place-items: center;
}
.auth-logo .mark svg { width: 34px; height: 34px; color: #1a1a2e; }
.auth-logo p { color: var(--muted); font-size: 15px; margin-top: 10px; line-height: 1.4; }
.auth-switch { text-align: center; margin-top: 16px; font-size: 15px; }
.auth-switch button { background: none; border: 0; color: var(--primary-text); font-weight: 700; cursor: pointer; padding: 8px; }

.err { color: var(--danger-text); font-size: 15px; font-weight: 600; margin-bottom: 12px; }

@media print {
  .tabbar, .quick, .topbar button, .btn { display: none !important; }
  body { padding: 0; background: #fff; }
  .card { box-shadow: none; border: 1px solid #ccc; break-inside: avoid; }
}
