:root {
  --bg: #f6f7f9;
  --surface: #ffffff;
  --surface-2: #eef0f4;
  --text: #14171c;
  --muted: #5d6573;
  --border: #dde1e8;
  --accent: #2f6fed;
  --accent-ink: #ffffff;
  --ok: #1f9d55;
  --warn: #d97706;
  --bad: #d64545;
  --radius: 14px;
  --tabbar-h: 64px;
  --safe-b: env(safe-area-inset-bottom, 0px);
  --safe-t: env(safe-area-inset-top, 0px);
  color-scheme: light;
}

@media (prefers-color-scheme: dark) {
  :root {
    --bg: #0f1115;
    --surface: #181b21;
    --surface-2: #22262e;
    --text: #eef1f5;
    --muted: #9aa3b2;
    --border: #2c313b;
    --accent: #5b8cff;
    --accent-ink: #0b0d12;
    --ok: #3ecf7a;
    --warn: #f0a33c;
    --bad: #ff6b6b;
    color-scheme: dark;
  }
}

* { box-sizing: border-box; }
html { -webkit-text-size-adjust: 100%; }
body {
  margin: 0;
  background: var(--bg);
  color: var(--text);
  font: 16px/1.45 system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
  -webkit-tap-highlight-color: transparent;
  overscroll-behavior-y: none;
}
h1, h2, h3 { margin: 0; line-height: 1.2; }
h1 { font-size: 1.35rem; }
h2 { font-size: 1.05rem; }
h3 { font-size: 1rem; }
button, input, select, textarea { font: inherit; color: inherit; }
[hidden] { display: none !important; }

/* Layout */
.topbar {
  position: sticky; top: 0; z-index: 5;
  display: flex; align-items: center; justify-content: space-between;
  padding: calc(10px + var(--safe-t)) 16px 10px;
  background: color-mix(in srgb, var(--bg) 88%, transparent);
  backdrop-filter: blur(12px);
  -webkit-backdrop-filter: blur(12px);
}
main {
  max-width: 640px; margin: 0 auto;
  padding: 4px 16px calc(var(--tabbar-h) + var(--safe-b) + 24px);
}
.view { display: flex; flex-direction: column; gap: 14px; }

.tabbar {
  position: fixed; left: 0; right: 0; bottom: 0; z-index: 10;
  display: grid; grid-template-columns: repeat(4, 1fr);
  height: calc(var(--tabbar-h) + var(--safe-b));
  padding-bottom: var(--safe-b);
  background: var(--surface);
  border-top: 1px solid var(--border);
}
.tabbar button {
  border: 0; background: none; color: var(--muted);
  display: flex; flex-direction: column; align-items: center; justify-content: center; gap: 2px;
  font-size: .72rem; cursor: pointer;
}
.tabbar svg { width: 24px; height: 24px; }
.tabbar button.active { color: var(--accent); font-weight: 600; }

/* Update banner */
.update-banner {
  position: sticky; top: 0; z-index: 20;
  display: flex; align-items: center; gap: 8px;
  padding: calc(10px + var(--safe-t)) 16px 10px;
  background: var(--accent); color: var(--accent-ink);
  font-size: .92rem; font-weight: 500;
}
.update-banner span { flex: 1; }
.update-banner button {
  border: 0; border-radius: 999px; padding: 8px 14px; min-height: 36px;
  font-weight: 700; cursor: pointer;
}
.update-banner .btn-update { background: var(--accent-ink); color: var(--accent); }
.update-banner .btn-later { background: transparent; color: inherit; opacity: .85; padding: 8px 6px; }
body.has-banner .topbar { position: static; padding-top: 10px; }

/* Cards */
.card {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 14px;
  display: flex; flex-direction: column; gap: 12px;
}
.row-between { display: flex; align-items: center; justify-content: space-between; gap: 8px; }
.muted { color: var(--muted); }
.small { font-size: .85rem; }
.hint { color: var(--muted); font-size: .85rem; margin: 0; }

/* Buttons */
.btn {
  display: inline-flex; align-items: center; justify-content: center; gap: 6px;
  min-height: 46px; padding: 0 16px;
  border-radius: 12px; border: 1px solid var(--border);
  background: var(--surface-2); cursor: pointer; font-weight: 600;
}
.btn.primary { background: var(--accent); border-color: var(--accent); color: var(--accent-ink); }
.btn.danger { color: var(--bad); }
.btn.block { width: 100%; }
.btn:active, .chip:active, .icon-btn:active { transform: scale(.97); }
.icon-btn {
  width: 40px; height: 40px; border-radius: 10px; border: 0;
  background: transparent; color: var(--text); cursor: pointer;
  display: inline-flex; align-items: center; justify-content: center; font-size: 1.4rem;
}
.chip {
  border: 1px solid var(--border); background: var(--surface-2);
  border-radius: 999px; padding: 4px 12px; font-size: .85rem; cursor: pointer;
}
.file-btn { cursor: pointer; }

/* Dose list */
.dose-list { display: flex; flex-direction: column; gap: 8px; }
.dose {
  display: grid; grid-template-columns: 6px 1fr auto; gap: 12px; align-items: center;
  padding: 10px 10px 10px 0; border-radius: 12px; background: var(--surface-2);
  overflow: hidden;
}
.dose .bar { align-self: stretch; background: var(--c, var(--accent)); }
.dose .name { font-weight: 600; }
.dose .meta { color: var(--muted); font-size: .85rem; }
.check {
  width: 44px; height: 44px; border-radius: 50%;
  border: 2px solid var(--border); background: var(--surface);
  display: inline-flex; align-items: center; justify-content: center; cursor: pointer;
  color: transparent; font-size: 1.2rem; font-weight: 700;
}
.check.on { background: var(--ok); border-color: var(--ok); color: #fff; }
.empty { color: var(--muted); text-align: center; padding: 10px 4px; margin: 0; }

/* Calendar */
.cal-head { display: flex; align-items: center; justify-content: space-between; }
.cal-grid { display: grid; grid-template-columns: repeat(7, 1fr); gap: 4px; }
.cal-dow span { text-align: center; font-size: .75rem; color: var(--muted); }
.cal-day {
  aspect-ratio: 1 / 1.05; min-width: 0;
  border: 1px solid transparent; border-radius: 10px; background: var(--surface-2);
  display: flex; flex-direction: column; align-items: center; justify-content: flex-start;
  padding: 4px 2px; gap: 3px; cursor: pointer; font-size: .9rem;
}
.cal-day.out { opacity: .35; }
.cal-day.today { border-color: var(--accent); font-weight: 700; }
.cal-day.sel { background: var(--accent); color: var(--accent-ink); }
.cal-day .dots { display: flex; flex-wrap: wrap; justify-content: center; gap: 3px; max-width: 100%; }
.dot { width: 7px; height: 7px; border-radius: 50%; display: inline-block; }
.cal-day .dot { background: transparent; border: 1.5px solid var(--c); }
.cal-day .dot.done { background: var(--c); }
.cal-day .dot.missed { border-color: var(--bad); background: transparent; }
.cal-day.sel .dot { border-color: var(--accent-ink); }
.cal-day.sel .dot.done { background: var(--accent-ink); }
.legend { display: flex; gap: 14px; justify-content: center; font-size: .78rem; color: var(--muted); flex-wrap: wrap; }
.legend span { display: inline-flex; align-items: center; gap: 5px; }
.legend .dot.planned { border: 1.5px solid var(--muted); }
.legend .dot.done { background: var(--muted); }
.legend .dot.missed { border: 1.5px solid var(--bad); }

/* Peptide list */
#peptide-list { display: flex; flex-direction: column; gap: 10px; }
.pep {
  background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius);
  padding: 14px; display: grid; grid-template-columns: 12px 1fr auto; gap: 12px; align-items: center;
  cursor: pointer; text-align: left; width: 100%;
}
.pep .swatch { width: 12px; height: 12px; border-radius: 50%; background: var(--c); }
.pep .title { font-weight: 600; }
.pep .sub { color: var(--muted); font-size: .85rem; }
.badge {
  font-size: .75rem; font-weight: 600; padding: 3px 9px; border-radius: 999px;
  background: var(--surface-2); color: var(--muted); white-space: nowrap;
}
.badge.on { background: color-mix(in srgb, var(--ok) 18%, transparent); color: var(--ok); }
.badge.off { background: color-mix(in srgb, var(--warn) 18%, transparent); color: var(--warn); }

/* Stats */
.summary-grid { display: grid; grid-template-columns: repeat(3, 1fr); gap: 8px; }
.tile {
  background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius);
  padding: 12px 10px; text-align: center;
}
.tile b { display: block; font-size: 1.4rem; font-variant-numeric: tabular-nums; }
.tile span { font-size: .75rem; color: var(--muted); }
#stats-list { display: flex; flex-direction: column; gap: 12px; }
.stat-head { display: flex; align-items: center; gap: 10px; }
.stat-head .swatch { width: 12px; height: 12px; border-radius: 50%; background: var(--c); }
.stat-grid { display: grid; grid-template-columns: 1fr 1fr; gap: 8px 14px; }
.stat-grid div { display: flex; flex-direction: column; }
.stat-grid b { font-variant-numeric: tabular-nums; font-size: 1.05rem; }
.stat-grid span { color: var(--muted); font-size: .78rem; }
.progress { height: 8px; background: var(--surface-2); border-radius: 99px; overflow: hidden; }
.progress i { display: block; height: 100%; background: var(--c, var(--accent)); border-radius: 99px; }
.strip { display: grid; grid-template-columns: repeat(28, 1fr); gap: 2px; }
.strip i { height: 14px; border-radius: 3px; background: var(--surface-2); }
.strip i.sched { background: color-mix(in srgb, var(--c) 25%, var(--surface-2)); }
.strip i.done { background: var(--c); }
.strip i.missed { background: color-mix(in srgb, var(--bad) 55%, var(--surface-2)); }

/* Forms */
.form { display: flex; flex-direction: column; gap: 12px; }
.form label { display: flex; flex-direction: column; gap: 5px; font-size: .85rem; color: var(--muted); font-weight: 500; }
.form input, .form select, .form textarea {
  width: 100%; min-height: 46px; padding: 10px 12px;
  border-radius: 10px; border: 1px solid var(--border);
  background: var(--surface-2); color: var(--text); font-size: 16px;
}
.form textarea { resize: vertical; }
.form input:focus, .form select:focus, .form textarea:focus { outline: 2px solid var(--accent); outline-offset: 0; border-color: transparent; }
.two { display: grid; grid-template-columns: 1fr 1fr; gap: 10px; }
.input-unit { position: relative; }
.input-unit input { padding-right: 44px; }
.input-unit span { position: absolute; right: 12px; top: 50%; transform: translateY(-50%); color: var(--muted); pointer-events: none; }
details { border: 1px solid var(--border); border-radius: 10px; padding: 10px 12px; }
details summary { cursor: pointer; font-size: .9rem; color: var(--muted); }
details[open] summary { margin-bottom: 10px; }
.swatches { display: flex; gap: 10px; flex-wrap: wrap; margin-top: -6px; }
.swatches button {
  width: 34px; height: 34px; border-radius: 50%; border: 3px solid transparent;
  background: var(--c); cursor: pointer;
}
.swatches button.on { border-color: var(--text); }
.actions { display: flex; gap: 10px; justify-content: flex-end; }
.actions .btn { flex: 1; }

/* Sheets */
dialog.sheet {
  width: 100%; max-width: 560px; max-height: 92dvh;
  margin: auto auto 0; padding: 16px 16px calc(16px + var(--safe-b));
  border: 0; border-radius: 20px 20px 0 0;
  background: var(--surface); color: var(--text);
  overflow-y: auto;
}
dialog.sheet::backdrop { background: rgb(0 0 0 / .5); }
dialog.sheet[open] { animation: up .2s ease-out; }
@keyframes up { from { transform: translateY(30px); opacity: .6; } to { transform: none; opacity: 1; } }
.sheet-head { display: flex; justify-content: space-between; align-items: center; }
@media (min-width: 600px) {
  dialog.sheet { margin: auto; border-radius: 20px; }
}

/* Calculator */
.result-card { gap: 10px; }
.big-result { display: flex; align-items: baseline; gap: 8px; flex-wrap: wrap; }
.big-result b { font-size: 2.4rem; font-variant-numeric: tabular-nums; color: var(--accent); line-height: 1; }
.big-result span { color: var(--muted); }
.kv { display: grid; grid-template-columns: 1fr auto; gap: 6px 12px; font-size: .92rem; }
.kv dt { color: var(--muted); }
.kv dd { margin: 0; font-weight: 600; text-align: right; font-variant-numeric: tabular-nums; }
.warn { color: var(--warn); font-size: .88rem; margin: 0; }
.math { margin: 0; padding-left: 20px; display: flex; flex-direction: column; gap: 10px; font-size: .92rem; }
.math li span { display: block; color: var(--muted); font-size: .82rem; }
.math code { font-family: ui-monospace, SFMono-Regular, Menlo, monospace; font-size: .86rem; background: var(--surface-2); padding: 2px 6px; border-radius: 6px; display: inline-block; margin-top: 3px; }
.syringe { width: 100%; height: auto; display: block; }
.disclaimer { color: var(--muted); font-size: .8rem; text-align: center; margin: 4px 8px 0; }

.toast {
  position: fixed; left: 50%; bottom: calc(var(--tabbar-h) + var(--safe-b) + 14px);
  transform: translate(-50%, 20px); opacity: 0; pointer-events: none;
  background: var(--text); color: var(--bg); padding: 10px 16px; border-radius: 999px;
  font-size: .9rem; transition: .2s; z-index: 50; max-width: calc(100% - 32px);
}
.toast.show { opacity: 1; transform: translate(-50%, 0); }

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