:root {
  --bg: #f4f5f7;
  --surface: #ffffff;
  --surface-2: #eceef2;
  --line: #dde0e6;
  --ink: #15171c;
  --ink-2: #4a5060;
  --ink-3: #7b8190;
  --accent: #d9480f;
  --accent-ink: #ffffff;
  --accent-soft: #fde7dc;
  --good: #1f8a4c;
  --good-soft: #dcf2e4;
  --bad: #c92a2a;
  --bad-soft: #fbe0e0;
  --focus: #5f3dc4;
  --focus-soft: #e9e3fb;
  --fast: #0b7285;
  --fast-soft: #d8f0f3;
  --radius: 16px;
  --tabbar-h: 58px;
  color-scheme: light;
}

@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) {
    --bg: #0f1115;
    --surface: #181b21;
    --surface-2: #22262e;
    --line: #2c313b;
    --ink: #f1f2f4;
    --ink-2: #b8bdc8;
    --ink-3: #858b98;
    --accent: #ff6b2c;
    --accent-ink: #1a0d05;
    --accent-soft: #3a2014;
    --good: #51cf66;
    --good-soft: #173522;
    --bad: #ff6b6b;
    --bad-soft: #3d1b1b;
    --focus: #a78bfa;
    --focus-soft: #2a2140;
    --fast: #3bc9db;
    --fast-soft: #11323a;
    color-scheme: dark;
  }
}
:root[data-theme="dark"] {
  --bg: #0f1115; --surface: #181b21; --surface-2: #22262e; --line: #2c313b;
  --ink: #f1f2f4; --ink-2: #b8bdc8; --ink-3: #858b98;
  --accent: #ff6b2c; --accent-ink: #1a0d05; --accent-soft: #3a2014;
  --good: #51cf66; --good-soft: #173522; --bad: #ff6b6b; --bad-soft: #3d1b1b;
  --focus: #a78bfa; --focus-soft: #2a2140; --fast: #3bc9db; --fast-soft: #11323a;
  color-scheme: dark;
}

* { box-sizing: border-box; }
html { -webkit-text-size-adjust: 100%; }
body {
  margin: 0;
  background: var(--bg);
  color: var(--ink);
  font: 16px/1.4 -apple-system, BlinkMacSystemFont, "SF Pro Text", "Segoe UI", Roboto, sans-serif;
  -webkit-tap-highlight-color: transparent;
  overscroll-behavior-y: none;
}
button, input, select, textarea { font: inherit; color: inherit; }
button { cursor: pointer; touch-action: manipulation; }
svg { display: block; }

/* ---------- chrome ---------- */
.topbar {
  position: sticky; top: 0; z-index: 5;
  display: flex; align-items: center; justify-content: space-between;
  padding: calc(env(safe-area-inset-top) + 8px) 16px 8px;
  background: color-mix(in srgb, var(--bg) 88%, transparent);
  -webkit-backdrop-filter: blur(12px);
  backdrop-filter: blur(12px);
}
.topbar h1 { margin: 0; font-size: 28px; font-weight: 800; letter-spacing: -0.02em; }
.icon-btn {
  width: 40px; height: 40px; border-radius: 50%; border: 0;
  background: var(--surface-2); display: grid; place-items: center;
}
.icon-btn svg, .tabbar svg { width: 22px; height: 22px; fill: none; stroke: currentColor; stroke-width: 2; stroke-linecap: round; stroke-linejoin: round; }
.icon-btn[aria-current="page"] { background: var(--accent); color: var(--accent-ink); }

main {
  padding: 4px 16px calc(var(--tabbar-h) + env(safe-area-inset-bottom) + 96px);
  max-width: 640px; margin: 0 auto;
  outline: none;
}

.tabbar {
  position: fixed; left: 0; right: 0; bottom: 0; z-index: 6;
  display: grid; grid-template-columns: repeat(5, 1fr);
  padding-bottom: env(safe-area-inset-bottom);
  background: color-mix(in srgb, var(--surface) 92%, transparent);
  -webkit-backdrop-filter: blur(12px);
  backdrop-filter: blur(12px);
  border-top: 1px solid var(--line);
}
.tabbar button {
  height: var(--tabbar-h); border: 0; background: none; color: var(--ink-3);
  display: flex; flex-direction: column; align-items: center; justify-content: center; gap: 3px;
  font-size: 11px; font-weight: 600;
}
.tabbar button[aria-current="page"] { color: var(--accent); }

/* ---------- building blocks ---------- */
.card {
  background: var(--surface); border-radius: var(--radius);
  padding: 16px; margin: 12px 0;
  border: 1px solid var(--line);
}
.card h2 { margin: 0 0 4px; font-size: 22px; letter-spacing: -0.01em; }
.card h3 { margin: 0 0 8px; font-size: 17px; }
.eyebrow { font-size: 12px; font-weight: 700; letter-spacing: 0.06em; text-transform: uppercase; color: var(--ink-3); }
.muted { color: var(--ink-3); }
.small { font-size: 14px; }
.row { display: flex; align-items: center; gap: 12px; }
.spread { display: flex; align-items: center; justify-content: space-between; gap: 12px; }
.stack > * + * { margin-top: 10px; }
.num { font-variant-numeric: tabular-nums; }
.center { text-align: center; }
hr { border: 0; border-top: 1px solid var(--line); margin: 14px 0; }

.btn {
  display: flex; align-items: center; justify-content: center; gap: 8px;
  width: 100%; min-height: 52px; padding: 0 18px;
  border: 0; border-radius: 14px;
  background: var(--surface-2); color: var(--ink);
  font-weight: 700; font-size: 17px;
}
.btn.primary { background: var(--accent); color: var(--accent-ink); }
.btn.focus { background: var(--focus); color: #fff; }
.btn.fast { background: var(--fast); color: #fff; }
.btn.danger { background: var(--bad-soft); color: var(--bad); }
.btn.small { min-height: 40px; font-size: 15px; width: auto; }
.btn:active { transform: scale(0.98); }
.btn-row { display: flex; gap: 10px; }
.btn-row > * { flex: 1; }
.link {
  background: none; border: 0; padding: 10px 4px; color: var(--accent);
  font-weight: 600; font-size: 15px;
}
.link.quiet { color: var(--ink-3); }
.link.danger { color: var(--bad); }

.chips { display: flex; gap: 8px; flex-wrap: wrap; }
.chip {
  border: 1px solid var(--line); background: var(--surface); border-radius: 999px;
  padding: 8px 14px; font-weight: 600; font-size: 14px;
}
.chip[aria-pressed="true"] { background: var(--ink); color: var(--bg); border-color: var(--ink); }

.field { display: flex; align-items: center; justify-content: space-between; gap: 12px; padding: 10px 0; border-bottom: 1px solid var(--line); }
.field:last-child { border-bottom: 0; }
.field label, .field .label { flex: 1; }
.field input[type="text"], .field input[type="number"], .field select, .field input[type="datetime-local"] {
  width: 110px; min-height: 40px; padding: 6px 10px; text-align: right;
  border: 1px solid var(--line); border-radius: 10px; background: var(--surface-2);
}
.field input.wide { width: 180px; }
.field .hint { display: block; font-size: 13px; color: var(--ink-3); }
textarea, .input {
  width: 100%; padding: 10px 12px; border-radius: 12px;
  border: 1px solid var(--line); background: var(--surface-2); min-height: 44px;
}
input:focus-visible, textarea:focus-visible, select:focus-visible, button:focus-visible {
  outline: 2px solid var(--accent); outline-offset: 2px;
}

.switch { position: relative; width: 51px; height: 31px; flex: none; }
.switch input { position: absolute; inset: 0; opacity: 0; margin: 0; z-index: 1; }
.switch span { position: absolute; inset: 0; border-radius: 999px; background: var(--line); transition: background .2s; }
.switch span::after {
  content: ""; position: absolute; top: 2px; left: 2px; width: 27px; height: 27px; border-radius: 50%;
  background: #fff; box-shadow: 0 1px 3px rgb(0 0 0 / .3); transition: transform .2s;
}
.switch input:checked + span { background: var(--good); }
.switch input:checked + span::after { transform: translateX(20px); }

.stats { display: grid; grid-template-columns: repeat(auto-fit, minmax(96px, 1fr)); gap: 10px; }
.stat { background: var(--surface-2); border-radius: 12px; padding: 10px 12px; }
.stat b { display: block; font-size: 22px; font-variant-numeric: tabular-nums; letter-spacing: -0.01em; }
.stat span { font-size: 12px; color: var(--ink-3); font-weight: 600; }

/* ---------- workout ---------- */
.lift-list { list-style: none; padding: 0; margin: 12px 0 16px; }
.lift-list li { display: flex; justify-content: space-between; padding: 10px 0; border-bottom: 1px solid var(--line); }
.lift-list li:last-child { border-bottom: 0; }
.lift-list .w { font-weight: 700; font-variant-numeric: tabular-nums; }

.ex-head { display: flex; align-items: baseline; justify-content: space-between; gap: 8px; }
.ex-head h3 { margin: 0; }
.weight-btn {
  border: 0; background: var(--accent-soft); color: var(--accent); border-radius: 10px;
  padding: 6px 12px; font-weight: 800; font-size: 18px; font-variant-numeric: tabular-nums;
}
.ex-meta { font-size: 13px; color: var(--ink-3); margin: 4px 0 12px; }
.sets { display: grid; grid-template-columns: repeat(5, 1fr); gap: 8px; }
.set {
  aspect-ratio: 1; border-radius: 50%; border: 2px solid var(--line); background: var(--surface-2);
  font-size: 22px; font-weight: 800; color: var(--ink-3); font-variant-numeric: tabular-nums;
  display: grid; place-items: center; max-width: 64px; width: 100%; justify-self: center;
}
.set.empty { color: color-mix(in srgb, var(--ink-3) 55%, transparent); }
.set.done { background: var(--good); border-color: var(--good); color: #fff; }
.set.miss { background: var(--bad-soft); border-color: var(--bad); color: var(--bad); }
.ex-card.done-all { border-color: var(--good); }
details.warm { margin-top: 12px; }
details.warm summary { font-size: 14px; color: var(--ink-2); font-weight: 600; cursor: pointer; }
.warm-list { margin: 8px 0 0; padding: 0; list-style: none; font-size: 14px; }
.warm-list li { display: flex; justify-content: space-between; padding: 4px 0; color: var(--ink-2); }

.restbar {
  position: fixed; left: 12px; right: 12px; z-index: 7;
  bottom: calc(var(--tabbar-h) + env(safe-area-inset-bottom) + 10px);
  max-width: 616px; margin: 0 auto;
  display: flex; align-items: center; gap: 10px;
  padding: 10px 10px 10px 16px; border-radius: 16px;
  background: var(--ink); color: var(--bg);
  box-shadow: 0 8px 24px rgb(0 0 0 / .25);
}
.restbar[hidden] { display: none; }
.restbar .t { font-size: 26px; font-weight: 800; font-variant-numeric: tabular-nums; min-width: 72px; }
.restbar .msg { flex: 1; font-size: 13px; line-height: 1.25; opacity: .8; }
.restbar button { border: 0; border-radius: 10px; min-height: 40px; padding: 0 12px; font-weight: 700; background: color-mix(in srgb, var(--bg) 18%, transparent); color: inherit; }
.restbar.go { background: var(--good); color: #fff; }

/* ---------- plates ---------- */
.plates { display: flex; align-items: center; gap: 3px; min-height: 64px; margin: 12px 0; }
.plates .bar { width: 36px; height: 8px; background: var(--ink-3); border-radius: 2px; }
.plate { width: 14px; border-radius: 3px; background: var(--ink-2); display: grid; place-items: center; }
.plate-labels { font-size: 14px; color: var(--ink-2); }

/* ---------- rings (focus, fasting) ---------- */
.ring-wrap { position: relative; width: min(78vw, 290px); aspect-ratio: 1; margin: 8px auto 16px; }
.ring-wrap svg { width: 100%; height: 100%; transform: rotate(-90deg); }
.ring-track { fill: none; stroke: var(--surface-2); stroke-width: 14; }
.ring-fill { fill: none; stroke-width: 14; stroke-linecap: round; transition: stroke-dashoffset .5s linear; }
.ring-center { position: absolute; inset: 0; display: flex; flex-direction: column; align-items: center; justify-content: center; text-align: center; }
.ring-center .big { font-size: 48px; font-weight: 800; letter-spacing: -0.03em; font-variant-numeric: tabular-nums; line-height: 1.05; }
.ring-center .sub { font-size: 14px; color: var(--ink-3); font-weight: 600; margin-top: 4px; }
.dots { display: flex; gap: 8px; justify-content: center; margin-bottom: 16px; }
.dots i { width: 10px; height: 10px; border-radius: 50%; background: var(--surface-2); border: 1px solid var(--line); }
.dots i.on { background: var(--focus); border-color: var(--focus); }
.phase-tag { display: inline-block; padding: 4px 10px; border-radius: 999px; font-size: 13px; font-weight: 700; }

/* ---------- lists ---------- */
.list { list-style: none; margin: 0; padding: 0; }
.list > li > button, .list > li.item {
  width: 100%; text-align: left; border: 0; background: none;
  display: flex; justify-content: space-between; align-items: center; gap: 10px;
  padding: 12px 0; border-bottom: 1px solid var(--line);
}
.list > li:last-child > button, .list > li:last-child.item { border-bottom: 0; }
.list .title { font-weight: 700; }
.list .detail { font-size: 13px; color: var(--ink-3); }
.badge { font-size: 12px; font-weight: 700; padding: 2px 8px; border-radius: 999px; background: var(--surface-2); color: var(--ink-2); white-space: nowrap; }
.badge.good { background: var(--good-soft); color: var(--good); }
.badge.bad { background: var(--bad-soft); color: var(--bad); }
.month { margin: 18px 0 0; }

/* ---------- charts ---------- */
.chart { width: 100%; height: auto; overflow: visible; }
.chart .grid { stroke: var(--line); stroke-width: 1; }
.chart .axis { fill: var(--ink-3); font-size: 11px; font-variant-numeric: tabular-nums; }
.chart .line { fill: none; stroke: var(--accent); stroke-width: 2; stroke-linejoin: round; stroke-linecap: round; }
.chart .pt { fill: var(--accent); stroke: var(--surface); stroke-width: 2; }
.chart .pt.hollow { fill: var(--surface); stroke: var(--accent); }
.chart .pt.sel { r: 6; }
.chart .hit { fill: transparent; }
.chart-tip { min-height: 22px; font-size: 14px; color: var(--ink-2); font-variant-numeric: tabular-nums; }

/* ---------- dialog & toast ---------- */
dialog {
  border: 0; padding: 0; border-radius: 20px; width: min(92vw, 440px);
  background: var(--surface); color: var(--ink);
  box-shadow: 0 20px 60px rgb(0 0 0 / .35);
}
dialog::backdrop { background: rgb(0 0 0 / .45); }
.dlg-body { padding: 20px; }
.dlg-body h2 { margin: 0 0 12px; font-size: 20px; }
.stepper { display: flex; align-items: center; gap: 10px; margin: 12px 0; }
.stepper button { width: 52px; height: 52px; border-radius: 50%; border: 0; background: var(--surface-2); font-size: 26px; font-weight: 700; }
.stepper input { flex: 1; min-width: 0; text-align: center; font-size: 30px; font-weight: 800; border: 0; background: none; font-variant-numeric: tabular-nums; }

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

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

/* ---------- weekly summary, history editing ---------- */
.stat .was { display: block; font-size: 11px; color: var(--ink-3); font-weight: 500; margin-top: 2px; }
.draft-ex { padding: 10px 0; border-bottom: 1px solid var(--line); }
.draft-ex input.mini {
  width: 76px; min-height: 36px; padding: 4px 8px; text-align: right;
  border: 1px solid var(--line); border-radius: 8px; background: var(--surface-2);
}
.mini-sets { margin-top: 8px; gap: 6px; }
.mini-sets .set { max-width: 44px; font-size: 16px; }
