:root {
  --bg: #111214;
  --bg2: #1a1b1f;
  --bg3: #24262b;
  --line: #2e3036;
  --fg: #f1f1f3;
  --muted: #9a9ca5;
  --faint: #6b6e78;
  --accent: #ff7a45;
  --accent-ink: #1a0d06;
  --good: #3ecf8e;
  --bad: #ff5d5d;
  --gold: #ffc53d;
  --radius: 14px;
  --nav-h: 64px;
  --safe-b: env(safe-area-inset-bottom, 0px);
  --safe-t: env(safe-area-inset-top, 0px);
  color-scheme: dark;
}

* { box-sizing: border-box; -webkit-tap-highlight-color: transparent; }
html, body { margin: 0; background: var(--bg); color: var(--fg); }
body {
  font: 16px/1.45 system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
  min-height: 100vh;
  overscroll-behavior-y: none;
}
button, input, select, textarea { font: inherit; color: inherit; }
button { cursor: pointer; }
a { color: var(--accent); }
.hidden { display: none !important; }

/* ---------- layout ---------- */
.topbar {
  position: sticky; top: 0; z-index: 20;
  display: flex; align-items: center; gap: 8px;
  padding: calc(10px + var(--safe-t)) 14px 10px;
  background: rgba(17, 18, 20, 0.92);
  backdrop-filter: blur(10px);
  border-bottom: 1px solid var(--line);
  min-height: 56px;
}
.topbar h1 { font-size: 1.15rem; margin: 0; flex: 1; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.topbar .sub { color: var(--muted); font-size: .85rem; font-variant-numeric: tabular-nums; }
.topbar input.title-input {
  flex: 1; min-width: 0; background: transparent; border: 0; font-size: 1.15rem; font-weight: 700; padding: 4px 0;
}
.topbar input.title-input:focus { outline: none; border-bottom: 2px solid var(--accent); }

main { padding: 14px 14px calc(var(--nav-h) + 28px + var(--safe-b)); max-width: 720px; margin: 0 auto; }
main.no-nav { padding-bottom: calc(40px + var(--safe-b)); }

.nav {
  position: fixed; left: 0; right: 0; bottom: 0; z-index: 30;
  display: grid; grid-template-columns: repeat(4, 1fr);
  height: calc(var(--nav-h) + var(--safe-b)); padding-bottom: var(--safe-b);
  background: rgba(20, 21, 24, 0.96); backdrop-filter: blur(10px);
  border-top: 1px solid var(--line);
}
.nav a {
  display: flex; flex-direction: column; align-items: center; justify-content: center; gap: 3px;
  color: var(--faint); text-decoration: none; font-size: .72rem; font-weight: 600;
}
.nav a svg { width: 24px; height: 24px; }
.nav a.on { color: var(--accent); }
.nav a .dot { width: 6px; height: 6px; border-radius: 3px; background: var(--good); margin-top: -2px; }

/* ---------- building blocks ---------- */
.card { background: var(--bg2); border: 1px solid var(--line); border-radius: var(--radius); padding: 14px; margin-bottom: 12px; }
.card.tap { cursor: pointer; }
.card.tap:active { background: var(--bg3); }
.card h3 { margin: 0 0 4px; font-size: 1.02rem; }
.row { display: flex; align-items: center; gap: 10px; }
.row.between { justify-content: space-between; }
.grow { flex: 1; min-width: 0; }
.muted { color: var(--muted); }
.faint { color: var(--faint); }
.small { font-size: .85rem; }
.tiny { font-size: .75rem; }
.num { font-variant-numeric: tabular-nums; }
.ellipsis { white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.section-title { font-size: .78rem; font-weight: 700; text-transform: uppercase; letter-spacing: .06em; color: var(--faint); margin: 22px 2px 10px; }
.section-title:first-child { margin-top: 4px; }
.stack > * + * { margin-top: 10px; }
.empty { text-align: center; color: var(--muted); padding: 36px 12px; }
.empty .big { font-size: 2.2rem; margin-bottom: 6px; }

.btn {
  display: inline-flex; align-items: center; justify-content: center; gap: 8px;
  border: 1px solid var(--line); background: var(--bg3); color: var(--fg);
  border-radius: 12px; padding: 12px 16px; font-weight: 650; min-height: 46px;
  text-decoration: none; user-select: none;
}
.btn:active { transform: scale(.98); }
.btn.primary { background: var(--accent); border-color: var(--accent); color: var(--accent-ink); }
.btn.good { background: var(--good); border-color: var(--good); color: #062014; }
.btn.danger { color: var(--bad); }
.btn.ghost { background: transparent; }
.btn.block { display: flex; width: 100%; }
.btn.small { min-height: 36px; padding: 7px 12px; font-size: .9rem; border-radius: 10px; }
.btn.big { min-height: 58px; font-size: 1.08rem; }
.btn:disabled { opacity: .5; }
.icon-btn {
  width: 40px; height: 40px; border-radius: 10px; border: 0; background: transparent; color: var(--muted);
  display: inline-flex; align-items: center; justify-content: center; flex: none;
}
.icon-btn:active { background: var(--bg3); }
.icon-btn svg { width: 22px; height: 22px; }

.field { display: block; margin-bottom: 14px; }
.field > span { display: block; font-size: .82rem; color: var(--muted); margin-bottom: 6px; font-weight: 600; }
.input, select.input, textarea.input {
  width: 100%; background: var(--bg); border: 1px solid var(--line); border-radius: 10px; padding: 12px; min-height: 46px;
}
.input:focus { outline: none; border-color: var(--accent); }
textarea.input { min-height: 70px; resize: vertical; }

.chips { display: flex; gap: 8px; overflow-x: auto; padding-bottom: 4px; scrollbar-width: none; }
.chips::-webkit-scrollbar { display: none; }
.chip {
  flex: none; border: 1px solid var(--line); background: var(--bg2); color: var(--muted);
  border-radius: 999px; padding: 6px 12px; font-size: .85rem; font-weight: 600;
}
.chip.on { background: var(--fg); color: var(--bg); border-color: var(--fg); }

.tag { display: inline-block; font-size: .7rem; font-weight: 800; letter-spacing: .03em; padding: 2px 6px; border-radius: 6px; vertical-align: middle; }
.tag.pr { background: rgba(255, 197, 61, .15); color: var(--gold); }
.tag.live { background: rgba(62, 207, 142, .15); color: var(--good); }

/* ---------- stats ---------- */
.stats { display: grid; grid-template-columns: repeat(3, 1fr); gap: 8px; margin-bottom: 12px; }
.stat { background: var(--bg2); border: 1px solid var(--line); border-radius: 12px; padding: 10px 12px; }
.stat b { display: block; font-size: 1.25rem; font-variant-numeric: tabular-nums; }
.stat span { font-size: .75rem; color: var(--muted); }

/* ---------- workout editor ---------- */
.ex-card { background: var(--bg2); border: 1px solid var(--line); border-radius: var(--radius); margin-bottom: 14px; overflow: hidden; }
.ex-head { display: flex; align-items: center; gap: 6px; padding: 12px 6px 4px 14px; }
.ex-head .name { flex: 1; min-width: 0; font-weight: 700; color: var(--accent); background: none; border: 0; padding: 0; text-align: left; font-size: 1.02rem; }
.ex-last { padding: 0 14px 8px; font-size: .8rem; color: var(--faint); }
.ex-note { margin: 0 14px 10px; }
.ex-note textarea { min-height: 44px; font-size: .9rem; }

.sets { width: 100%; border-collapse: collapse; }
.sets th { font-size: .7rem; font-weight: 700; color: var(--faint); text-transform: uppercase; letter-spacing: .05em; padding: 4px 4px; text-align: center; }
.sets td { padding: 4px; text-align: center; }
.sets tr.done td { background: rgba(62, 207, 142, .09); }
.sets td.setno { width: 44px; }
.sets td.prev { color: var(--faint); font-size: .8rem; width: 26%; white-space: nowrap; font-variant-numeric: tabular-nums; }
.sets td.chk { width: 52px; }
.setno-btn { width: 34px; height: 34px; border-radius: 9px; border: 0; background: var(--bg3); font-weight: 700; font-size: .9rem; color: var(--muted); }
.cell {
  width: 100%; max-width: 92px; height: 40px; text-align: center; border-radius: 9px; border: 1px solid transparent;
  background: var(--bg3); font-weight: 650; font-variant-numeric: tabular-nums; padding: 0 4px;
}
.cell:focus { outline: none; border-color: var(--accent); background: var(--bg); }
.cell::placeholder { color: #565964; font-weight: 500; }
tr.done .cell { background: transparent; }
.check {
  width: 40px; height: 40px; border-radius: 10px; border: 0; background: var(--bg3); color: var(--faint);
  display: inline-flex; align-items: center; justify-content: center;
}
.check svg { width: 22px; height: 22px; }
tr.done .check { background: var(--good); color: #062014; }
.ex-foot { display: flex; gap: 8px; padding: 8px 14px 14px; }
.ex-foot .btn { flex: 1; }

/* ---------- sheets / modals ---------- */
.sheet-backdrop {
  position: fixed; inset: 0; z-index: 50; background: rgba(0, 0, 0, .55);
  display: flex; align-items: flex-end; justify-content: center;
  animation: fade .15s ease;
}
.sheet {
  width: 100%; max-width: 720px; max-height: 92vh; display: flex; flex-direction: column;
  background: var(--bg2); border-radius: 18px 18px 0 0; border: 1px solid var(--line); border-bottom: 0;
  padding-bottom: var(--safe-b);
  animation: up .2s ease;
}
.sheet.full { height: 92vh; }
.sheet-head { display: flex; align-items: center; gap: 8px; padding: 14px 14px 10px; }
.sheet-head h2 { margin: 0; font-size: 1.1rem; flex: 1; }
.sheet-body { overflow-y: auto; padding: 0 14px 16px; -webkit-overflow-scrolling: touch; }
.sheet-actions { display: grid; gap: 8px; padding: 6px 14px 16px; }
@keyframes up { from { transform: translateY(30px); opacity: .4; } to { transform: none; opacity: 1; } }
@keyframes fade { from { opacity: 0; } to { opacity: 1; } }

.pick-list { list-style: none; margin: 0; padding: 0; }
.pick-list li button {
  width: 100%; text-align: left; background: none; border: 0; border-bottom: 1px solid var(--line);
  padding: 13px 4px; display: flex; align-items: center; gap: 10px;
}
.pick-list li button:active { background: var(--bg3); }
.pick-list .g { color: var(--faint); font-size: .78rem; margin-left: auto; flex: none; }
.pick-list .group-label { font-size: .75rem; font-weight: 800; color: var(--faint); text-transform: uppercase; letter-spacing: .06em; padding: 16px 4px 4px; }

/* ---------- history ---------- */
.w-lines { margin: 8px 0 0; padding: 0; list-style: none; font-size: .88rem; color: var(--muted); }
.w-lines li { display: flex; justify-content: space-between; gap: 10px; padding: 2px 0; }
.w-lines li span:first-child { white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.w-lines li span:last-child { flex: none; font-variant-numeric: tabular-nums; color: var(--faint); }
.detail-sets { margin: 6px 0 0; padding: 0; list-style: none; font-variant-numeric: tabular-nums; }
.detail-sets li { display: flex; gap: 12px; padding: 4px 0; border-top: 1px solid var(--line); font-size: .95rem; }
.detail-sets li:first-child { border-top: 0; }
.detail-sets li .i { width: 22px; color: var(--faint); }

/* ---------- charts ---------- */
.chart { width: 100%; height: auto; display: block; }
.chart .grid { stroke: var(--line); stroke-width: 1; }
.chart .line { fill: none; stroke: var(--accent); stroke-width: 2.5; stroke-linejoin: round; stroke-linecap: round; }
.chart .area { fill: rgba(255, 122, 69, .12); }
.chart .pt { fill: var(--accent); }
.chart text { fill: var(--faint); font-size: 13px; }
.bars { display: flex; align-items: flex-end; gap: 4px; height: 70px; }
.bars div { flex: 1; background: var(--bg3); border-radius: 4px 4px 2px 2px; min-height: 3px; position: relative; }
.bars div.on { background: var(--accent); }
.bars-x { display: flex; justify-content: space-between; font-size: .7rem; color: var(--faint); margin-top: 6px; }

/* ---------- login ---------- */
.login { min-height: 100vh; display: grid; place-items: center; padding: 24px; }
.login form { width: min(360px, 100%); }
.login .logo { width: 64px; height: 64px; margin: 0 auto 14px; display: block; }
.login h1 { text-align: center; margin: 0 0 24px; font-size: 1.5rem; }
.err { color: var(--bad); font-size: .9rem; min-height: 1.3em; margin: 4px 0 10px; }

/* ---------- toast ---------- */
#toast {
  position: fixed; left: 50%; bottom: calc(var(--nav-h) + 18px + var(--safe-b)); transform: translateX(-50%) translateY(20px);
  background: var(--fg); color: var(--bg); padding: 10px 16px; border-radius: 12px; font-weight: 650; font-size: .92rem;
  opacity: 0; pointer-events: none; transition: all .2s ease; z-index: 80; max-width: 90vw; text-align: center;
}
#toast.show { opacity: 1; transform: translateX(-50%); }

.sync { font-size: .75rem; color: var(--faint); display: inline-flex; align-items: center; gap: 5px; }
.sync::before { content: ""; width: 7px; height: 7px; border-radius: 50%; background: var(--good); }
.sync.saving::before { background: var(--gold); }
.sync.offline::before { background: var(--bad); }

@media (min-width: 720px) {
  .sheet-backdrop { align-items: center; }
  .sheet { border-radius: 18px; border-bottom: 1px solid var(--line); }
}
.sheet .grow { min-height: 0; flex: 1; }

/* ---------- v2: dates, calendar, naming ---------- */
.chips.wrap { flex-wrap: wrap; overflow: visible; }
.flip svg { transform: scaleX(-1); }
.topbar input.title-input { border-bottom: 1px dashed var(--line); }
.date-row {
  width: 100%; display: flex; align-items: center; gap: 10px; text-align: left;
  background: var(--bg2); border: 1px solid var(--line); border-radius: 12px; padding: 11px 12px; margin-bottom: 10px; font-weight: 600;
}
.date-row svg { width: 20px; height: 20px; color: var(--accent); flex: none; }
.cal { display: grid; grid-template-columns: repeat(7, 1fr); gap: 4px; margin-top: 6px; }
.cal-h span { text-align: center; font-size: .7rem; font-weight: 700; color: var(--faint); padding: 6px 0 2px; }
.cal-d {
  position: relative; aspect-ratio: 1; border: 0; border-radius: 10px; background: transparent; color: var(--muted);
  font-weight: 600; font-size: .92rem; font-variant-numeric: tabular-nums;
}
.cal-d.has { background: rgba(255, 122, 69, .16); color: var(--fg); }
.cal-d.has i { position: absolute; left: 50%; bottom: 5px; width: 5px; height: 5px; margin-left: -2.5px; border-radius: 50%; background: var(--accent); }
.cal-d.today { box-shadow: inset 0 0 0 1px var(--faint); }
.cal-d.sel { background: var(--accent); color: var(--accent-ink); }
.cal-d.sel i { background: var(--accent-ink); }

/* ---------- v5: routines are exercise lists ---------- */
.ex-head.compact { padding: 10px 6px 10px 12px; }
.rt-num { width: 26px; height: 26px; border-radius: 8px; background: var(--bg3); color: var(--muted); font-size: .8rem; font-weight: 700; display: inline-flex; align-items: center; justify-content: center; flex: none; }
