:root {
  color-scheme: light;
  --bg: #f3f4f6;
  --surface: #ffffff;
  --surface-2: #eceef2;
  --text: #121418;
  --muted: #646c7a;
  --border: #e1e4ea;
  --accent: #e8501a;
  --accent-soft: #fde7dd;
  --accent-ink: #ffffff;
  --good: #15803d;
  --good-soft: #dcfce7;
  --good-ink: #ffffff;
  --danger: #c62828;
  --radius: 14px;
  --tab-h: 64px;
  --safe-b: env(safe-area-inset-bottom, 0px);
}

@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) {
    color-scheme: dark;
    --bg: #0d0f12;
    --surface: #16191e;
    --surface-2: #1f232a;
    --text: #eceef2;
    --muted: #8a93a3;
    --border: #282d35;
    --accent: #ff6a33;
    --accent-soft: #3a1d12;
    --accent-ink: #ffffff;
    --good: #4ade80;
    --good-soft: #12301d;
    --good-ink: #0b1a10;
    --danger: #f87171;
  }
}

:root[data-theme="dark"] {
  color-scheme: dark;
  --bg: #0d0f12;
  --surface: #16191e;
  --surface-2: #1f232a;
  --text: #eceef2;
  --muted: #8a93a3;
  --border: #282d35;
  --accent: #ff6a33;
  --accent-soft: #3a1d12;
  --accent-ink: #ffffff;
  --good: #4ade80;
  --good-soft: #12301d;
  --good-ink: #0b1a10;
  --danger: #f87171;
}

* { 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;
}
body.sheet-open { overflow: hidden; }
button, input, select, textarea { font: inherit; color: inherit; }
button { touch-action: manipulation; }
h1, h2, h3 { letter-spacing: -0.01em; }

.sr-only {
  position: absolute; width: 1px; height: 1px; overflow: hidden;
  clip: rect(0 0 0 0); white-space: nowrap;
}

/* ---------- Layout ---------- */
.topbar {
  position: sticky; top: 0; z-index: 5;
  display: flex; align-items: center; justify-content: space-between;
  padding: calc(env(safe-area-inset-top, 0px) + 12px) max(16px, calc((100% - 568px) / 2)) 10px;
  background: color-mix(in srgb, var(--bg) 88%, transparent);
  backdrop-filter: blur(10px);
  -webkit-backdrop-filter: blur(10px);
}
.topbar h1 { font-size: 1.5rem; margin: 0; }

main {
  max-width: 600px;
  margin: 0 auto;
  padding: 4px 16px calc(var(--tab-h) + 32px + var(--safe-b));
}
body.resting main { padding-bottom: calc(var(--tab-h) + 130px + var(--safe-b)); }

.card {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 16px;
  margin-bottom: 12px;
}
.section-title {
  font-size: 0.78rem; text-transform: uppercase; letter-spacing: 0.06em;
  color: var(--muted); font-weight: 650;
  margin: 22px 4px 8px;
}
.muted { color: var(--muted); }
.small { font-size: 0.85rem; }
.row { display: flex; align-items: center; }
.between { justify-content: space-between; }
.gap { gap: 8px; }
.wrap { flex-wrap: wrap; }
.grow { flex: 1; }
.mt { margin-top: 16px; }
.empty {
  text-align: center; color: var(--muted);
  padding: 28px 16px; margin-bottom: 12px;
  border: 1.5px dashed var(--border); border-radius: var(--radius);
}

/* ---------- Controls ---------- */
.btn {
  display: inline-flex; align-items: center; justify-content: center; gap: 6px;
  min-height: 44px; padding: 0 16px;
  border-radius: 12px; border: 1px solid var(--border);
  background: var(--surface); font-weight: 650; cursor: pointer;
  transition: transform 0.08s ease, background 0.15s ease;
}
.btn:active { transform: scale(0.98); }
a.btn { text-decoration: none; color: inherit; }
.btn.primary { background: var(--accent); border-color: var(--accent); color: var(--accent-ink); }
.btn.block { width: 100%; }
.btn.small { min-height: 36px; padding: 0 12px; font-size: 0.88rem; }
.btn.ghost { background: var(--surface-2); border-color: transparent; }
.btn.danger-outline {
  background: transparent; color: var(--danger);
  border-color: color-mix(in srgb, var(--danger) 40%, transparent);
}
.link {
  background: none; border: 0; padding: 0; cursor: pointer;
  color: var(--accent); font-weight: 650; text-decoration: underline;
}
.icon-btn {
  display: inline-grid; place-items: center;
  width: 36px; height: 36px; border-radius: 10px;
  border: 0; background: transparent; color: var(--muted); cursor: pointer;
}
.icon-btn:hover { background: var(--surface-2); color: var(--text); }
.icon-btn:disabled { opacity: 0.3; cursor: default; background: transparent; }

.seg {
  display: flex; gap: 3px; padding: 3px;
  background: var(--surface-2); border-radius: 12px;
}
.seg button {
  flex: 1; border: 0; background: none; cursor: pointer;
  padding: 8px 6px; border-radius: 9px;
  font-size: 0.85rem; font-weight: 650; color: var(--muted);
}
.seg button[aria-pressed="true"] {
  background: var(--surface); color: var(--text);
  box-shadow: 0 1px 2px rgba(0, 0, 0, 0.15);
}

.select, .text-input, .search {
  width: 100%; height: 44px; padding: 0 14px;
  border: 1px solid var(--border); border-radius: 12px;
  background: var(--surface-2);
}
.select { background: var(--surface); font-weight: 650; padding: 0 10px; }
.text-input:focus, .search:focus, .select:focus { outline: 2px solid var(--accent); outline-offset: -1px; }
.field { display: block; margin-bottom: 14px; }
.field > span { display: block; font-size: 0.8rem; font-weight: 650; color: var(--muted); margin-bottom: 4px; }

/* ---------- Start screen ---------- */
.stats { display: grid; grid-template-columns: repeat(3, 1fr); gap: 8px; margin-bottom: 16px; }
.stats div { display: flex; flex-direction: column; min-width: 0; }
.stats b { font-size: 1.4rem; font-variant-numeric: tabular-nums; }
.stats span { font-size: 0.8rem; color: var(--muted); }
.routine-pick {
  display: flex; align-items: center; justify-content: space-between; gap: 12px;
  width: 100%; text-align: left; cursor: pointer;
}
.routine-pick:hover { border-color: var(--accent); }
.chev { color: var(--muted); font-size: 1.5rem; line-height: 1; }

/* ---------- Active workout ---------- */
.active-head { margin: 4px 0 14px; }
.title-input {
  width: 100%; padding: 2px 0;
  font-size: 1.25rem; font-weight: 750;
  border: 0; border-bottom: 2px solid transparent; background: transparent;
}
.title-input:focus { outline: none; border-bottom-color: var(--accent); }
.ex-card { padding: 12px; }
.ex-head { display: flex; align-items: center; justify-content: space-between; margin: 0 2px 6px 4px; }
.ex-head .ex-name { font-size: 1.05rem; font-weight: 750; color: var(--accent); }

table.sets { width: 100%; border-collapse: separate; border-spacing: 0 4px; margin-bottom: 6px; }
.sets th {
  font-size: 0.72rem; text-transform: uppercase; letter-spacing: 0.04em;
  color: var(--muted); font-weight: 650; text-align: center; padding: 0 2px;
}
.sets td { text-align: center; padding: 2px; }
.sets td:first-child { border-radius: 10px 0 0 10px; }
.sets td:last-child { border-radius: 0 10px 10px 0; }
.sets tr.done td { background: var(--good-soft); }
.sets .setno { width: 34px; font-weight: 750; }
.sets .prev { font-size: 0.82rem; white-space: nowrap; }
.num {
  width: 100%; max-width: 76px; height: 40px;
  text-align: center; font-weight: 650; font-variant-numeric: tabular-nums;
  border: 1px solid var(--border); border-radius: 10px; background: var(--surface-2);
}
.num::placeholder { color: var(--muted); opacity: 0.75; font-weight: 400; }
.num:focus { outline: 2px solid var(--accent); outline-offset: -1px; background: var(--surface); }
.sets tr.done .num { background: transparent; border-color: transparent; }
.check {
  width: 40px; height: 40px; border-radius: 10px; cursor: pointer;
  border: 1px solid var(--border); background: var(--surface-2);
  color: var(--muted); font-weight: 800;
}
.check[aria-pressed="true"] { background: var(--good); border-color: var(--good); color: var(--good-ink); }

/* ---------- Rest timer ---------- */
.rest {
  position: fixed; left: 0; right: 0; z-index: 6;
  bottom: calc(var(--tab-h) + var(--safe-b));
  padding: 0 16px 8px;
}
.rest[hidden] { display: none; }
.rest-inner {
  max-width: 568px; margin: 0 auto; padding: 10px 14px 12px;
  background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius);
  box-shadow: 0 8px 24px rgba(0, 0, 0, 0.18);
}
.rest-progress { height: 4px; border-radius: 4px; overflow: hidden; background: var(--surface-2); margin-bottom: 8px; }
.rest-progress span { display: block; height: 100%; width: 100%; background: var(--accent); transition: width 0.5s linear; }
.rest-time { font-size: 1.6rem; font-weight: 800; line-height: 1.1; font-variant-numeric: tabular-nums; }

/* ---------- History ---------- */
.hist-head {
  display: flex; justify-content: space-between; align-items: flex-start; gap: 12px;
  width: 100%; padding: 0; border: 0; background: none; text-align: left; cursor: pointer;
}
.hist-head strong { font-size: 1.05rem; }
.hist-meta { display: flex; flex-wrap: wrap; gap: 4px 14px; font-size: 0.85rem; color: var(--muted); margin: 4px 0 8px; }
.hist-lines { font-size: 0.9rem; }
.hist-detail { border-top: 1px solid var(--border); margin-top: 12px; padding-top: 12px; }
.hist-detail h4 { margin: 0 0 4px; font-size: 0.95rem; }
.badge {
  display: inline-block; white-space: nowrap;
  background: var(--accent-soft); color: var(--accent);
  font-size: 0.72rem; font-weight: 750; padding: 2px 8px; border-radius: 999px;
}
.chips { display: flex; flex-wrap: wrap; gap: 6px; margin: 4px 0 12px; }
.chip {
  background: var(--surface-2); border-radius: 8px; padding: 3px 8px;
  font-size: 0.85rem; font-variant-numeric: tabular-nums;
}
.heat {
  display: grid; grid-template-rows: repeat(7, 1fr); grid-auto-flow: column; grid-auto-columns: 1fr;
  gap: 4px; margin-top: 10px;
}
.heat i { aspect-ratio: 1; border-radius: 3px; background: var(--surface-2); }
.heat i.on { background: var(--accent); }
.heat i.today { outline: 1.5px solid var(--muted); outline-offset: -1.5px; }
.heat i.future { visibility: hidden; }

/* ---------- Progress ---------- */
.chart { display: block; width: 100%; height: auto; margin-top: 8px; }
.chart .grid { stroke: var(--border); stroke-width: 1; }
.chart text { fill: var(--muted); font-size: 10px; font-family: inherit; }
.chart .line { fill: none; stroke: var(--accent); stroke-width: 2.5; stroke-linejoin: round; stroke-linecap: round; }
.chart .area { fill: var(--accent); opacity: 0.1; }
.chart .dot { fill: var(--surface); stroke: var(--accent); stroke-width: 2; }
.chart .last { fill: var(--text); font-weight: 700; font-size: 11px; }
.pr-grid { display: grid; grid-template-columns: 1fr 1fr; gap: 8px; }
.pr { background: var(--surface-2); border-radius: 12px; padding: 10px 12px; min-width: 0; }
.pr span { display: block; font-size: 0.75rem; color: var(--muted); font-weight: 650; }
.pr b { display: block; font-size: 1.2rem; font-variant-numeric: tabular-nums; }
.pr small { color: var(--muted); }

/* ---------- Exercise details ---------- */
.ex-title {
  display: flex; align-items: center; gap: 8px; min-width: 0;
  padding: 4px 0; border: 0; background: none; text-align: left; cursor: pointer;
}
.info-dot {
  flex: none; display: inline-grid; place-items: center;
  width: 20px; height: 20px; border-radius: 50%;
  background: var(--surface-2); color: var(--muted); font-size: 0.75rem; font-weight: 800;
}
.tag {
  flex: none; display: inline-block;
  padding: 1px 7px; border-radius: 6px;
  background: var(--accent-soft); color: var(--accent);
  font-size: 0.75rem; font-weight: 800;
}
.ex-meta { font-size: 0.85rem; color: var(--muted); margin: -2px 4px 6px; }
.hint {
  font-size: 0.82rem; color: var(--muted);
  background: var(--surface-2); border-radius: 8px; padding: 6px 10px; margin: 0 0 6px;
}
.hint.up { background: var(--good-soft); color: var(--good); font-weight: 700; }
.notes {
  background: var(--accent-soft); color: var(--text);
  border-radius: var(--radius); padding: 10px 14px; margin-bottom: 12px; font-size: 0.9rem;
}
.notes div + div { margin-top: 4px; }
.nowrap { white-space: nowrap; }

.demo {
  position: relative; aspect-ratio: 3 / 2; overflow: hidden;
  border-radius: 12px; background: #fff;
}
.demo img { position: absolute; inset: 0; width: 100%; height: 100%; object-fit: contain; }
.demo img:nth-child(2) { animation: demo-swap 2.4s ease-in-out infinite; }
@keyframes demo-swap {
  0%, 40% { opacity: 0; }
  50%, 90% { opacity: 1; }
  100% { opacity: 0; }
}
@media (prefers-reduced-motion: reduce) {
  .demo { aspect-ratio: 3 / 1; display: grid; grid-template-columns: 1fr 1fr; }
  .demo img { position: static; }
  .demo img:nth-child(2) { animation: none; }
}
.steps { margin: 0; padding-left: 20px; font-size: 0.92rem; }
.steps li + li { margin-top: 6px; }

table.log { width: 100%; border-collapse: collapse; font-size: 0.9rem; font-variant-numeric: tabular-nums; }
.log th { text-align: left; font-size: 0.72rem; text-transform: uppercase; letter-spacing: 0.04em; color: var(--muted); font-weight: 650; padding: 0 6px 6px 0; }
.log td { padding: 7px 6px 7px 0; border-top: 1px solid var(--border); }
.log td.w { font-weight: 750; white-space: nowrap; }
.log td.w.up { color: var(--good); }
.log td.w.down { color: var(--danger); }

/* ---------- Routines ---------- */
.ex-list { list-style: none; margin: 8px 0 12px; padding: 0; }
.ex-list li { border-top: 1px solid var(--border); }
.ex-list button {
  display: flex; align-items: center; gap: 8px; width: 100%;
  padding: 8px 2px; border: 0; background: none; text-align: left; cursor: pointer; font-size: 0.92rem;
}
.ex-list button:hover { color: var(--accent); }
.r-item { padding: 8px 0; border-bottom: 1px solid var(--border); }
.r-item:last-child { border-bottom: 0; }
.r-name { flex: 1; font-weight: 650; min-width: 0; }
.r-controls { margin-top: 4px; }
.stepper { display: flex; align-items: center; gap: 2px; font-size: 0.85rem; white-space: nowrap; }
.stepper .icon-btn { width: 30px; height: 30px; background: var(--surface-2); }
.mini { display: inline-flex; align-items: center; gap: 4px; font-size: 0.85rem; color: var(--muted); }
.mini .num { width: 56px; height: 32px; }
.textarea { height: auto; padding: 10px 14px; resize: vertical; line-height: 1.4; }

/* ---------- Sheet ---------- */
.sheet { position: fixed; inset: 0; z-index: 10; display: flex; align-items: flex-end; justify-content: center; }
.sheet[hidden] { display: none; }
.sheet-backdrop { position: absolute; inset: 0; background: rgba(0, 0, 0, 0.45); }
.sheet-panel {
  position: relative; display: flex; flex-direction: column;
  width: 100%; max-width: 600px; max-height: 88vh; max-height: 88dvh;
  padding: 16px 16px calc(16px + var(--safe-b));
  background: var(--surface); border-radius: 20px 20px 0 0;
  animation: sheet-up 0.2s ease;
}
.sheet-panel:has(.picker-list) { height: 88vh; height: 88dvh; }
@keyframes sheet-up { from { transform: translateY(24px); opacity: 0; } }
@media (min-width: 700px) {
  .sheet { align-items: center; }
  .sheet-panel { border-radius: 20px; }
}
.sheet-head { display: flex; align-items: center; justify-content: space-between; margin-bottom: 12px; }
.sheet-head h2 { margin: 0; font-size: 1.2rem; }
.sheet-body { overflow-y: auto; }
.picker-list { overflow-y: auto; flex: 1; margin: 8px -16px 0; padding: 0 16px; }
.picker-cat {
  font-size: 0.75rem; text-transform: uppercase; letter-spacing: 0.06em;
  color: var(--muted); font-weight: 650; margin: 14px 4px 4px;
}
.picker-item {
  display: flex; justify-content: space-between; align-items: center;
  width: 100%; padding: 11px 12px; border: 0; border-radius: 10px;
  background: none; text-align: left; cursor: pointer;
}
.picker-item:hover, .picker-item:focus-visible { background: var(--surface-2); outline: none; }
.picker-item.create { color: var(--accent); font-weight: 650; }
.set-group { margin-bottom: 22px; }
.set-group h3 {
  font-size: 0.78rem; text-transform: uppercase; letter-spacing: 0.06em;
  color: var(--muted); margin: 0 0 8px;
}

/* ---------- Toast & tab bar ---------- */
.toast {
  position: fixed; left: 50%; z-index: 20;
  bottom: calc(var(--tab-h) + 16px + var(--safe-b));
  transform: translate(-50%, 16px); opacity: 0; pointer-events: none;
  max-width: calc(100% - 32px); padding: 10px 18px; border-radius: 999px;
  background: var(--text); color: var(--bg); font-weight: 650; text-align: center;
  transition: opacity 0.2s ease, transform 0.2s ease;
}
.toast.show { opacity: 1; transform: translate(-50%, 0); }
body.resting .toast { bottom: calc(var(--tab-h) + 120px + var(--safe-b)); }

.tabbar {
  position: fixed; left: 0; right: 0; bottom: 0; z-index: 7;
  display: flex; justify-content: center;
  padding-bottom: var(--safe-b);
  background: var(--surface); border-top: 1px solid var(--border);
}
.tabbar button {
  position: relative; flex: 1; max-width: 150px; height: var(--tab-h);
  display: flex; flex-direction: column; align-items: center; justify-content: center; gap: 2px;
  border: 0; background: none; cursor: pointer;
  color: var(--muted); font-size: 0.72rem; font-weight: 650;
}
.tabbar svg { width: 24px; height: 24px; }
.tabbar button[aria-current="page"] { color: var(--accent); }
body.has-active .tabbar [data-tab="workout"]::after {
  content: ""; position: absolute; top: 10px; left: calc(50% + 10px);
  width: 8px; height: 8px; border-radius: 50%; background: var(--good);
}

/* ---------- Account and sync ---------- */
.sync-badge {
  margin-left: auto; margin-right: 8px; padding: 3px 9px; border-radius: 999px;
  font-size: 0.75rem; font-weight: 650; background: var(--surface-2); color: var(--muted);
}
.sync-badge[hidden] { display: none; }
body.signed-out .tabbar,
body.signed-out .topbar .icon-btn,
body.signed-out .rest { display: none; }
.login { max-width: 420px; margin: 24px auto 0; }
.login form { margin-bottom: 4px; }
.welcome { border-color: var(--accent); }
.welcome p { margin: 6px 0 12px; }
[data-error]:empty { display: none; }
