:root {
  color-scheme: light;
  --bg: #f3f3f1;
  --surface: #fcfcfb;
  --surface-2: #ecebe8;
  --border: #dcdbd6;
  --text: #0b0b0b;
  --text-2: #52514e;
  --accent: #2a78d6;
  --accent-ink: #ffffff;
  --accent-soft: #e3eefb;
  --done: #e4f4ea;
  --done-ink: #146c3a;
  --danger: #c4302f;
  --warn: #9a5b00;
  --radius: 14px;
  --tap: 44px;
  --safe-b: env(safe-area-inset-bottom, 0px);
  --safe-t: env(safe-area-inset-top, 0px);
  font-family: system-ui, -apple-system, 'Segoe UI', Roboto, sans-serif;
  -webkit-text-size-adjust: 100%;
}
@media (prefers-color-scheme: dark) {
  :root:not([data-theme='light']) {
    color-scheme: dark;
    --bg: #111110;
    --surface: #1a1a19;
    --surface-2: #262624;
    --border: #34332f;
    --text: #f0efec;
    --text-2: #c3c2b7;
    --accent: #3987e5;
    --accent-soft: #1d2b3d;
    --done: #15301f;
    --done-ink: #7fd8a1;
    --danger: #ef6b6a;
    --warn: #e0a640;
  }
}
:root[data-theme='dark'] {
  color-scheme: dark;
  --bg: #111110;
  --surface: #1a1a19;
  --surface-2: #262624;
  --border: #34332f;
  --text: #f0efec;
  --text-2: #c3c2b7;
  --accent: #3987e5;
  --accent-soft: #1d2b3d;
  --done: #15301f;
  --done-ink: #7fd8a1;
  --danger: #ef6b6a;
  --warn: #e0a640;
}

* { box-sizing: border-box; }
[hidden] { display: none !important; }
html, body { margin: 0; }
body {
  background: var(--bg);
  color: var(--text);
  font-size: 16px;
  line-height: 1.4;
  overscroll-behavior-y: none;
  padding-bottom: calc(72px + var(--safe-b));
}
body.in-workout { padding-bottom: calc(140px + var(--safe-b)); }
h1, h2, h3 { margin: 0; line-height: 1.2; }
h1 { font-size: 1.6rem; }
h2 { font-size: 1.2rem; }
h3 { font-size: 1.02rem; }
p { margin: 0; }
a { color: inherit; text-decoration: none; }
.muted { color: var(--text-2); }
.small { font-size: 0.85rem; }
.center { text-align: center; }
.warn { color: var(--warn); }
.grow { flex: 1; min-width: 0; }
.pad { padding: 12px 16px; }
.link { color: var(--accent); }

.content { max-width: 640px; margin: 0 auto; padding: 12px 16px 24px; }
.stack { display: flex; flex-direction: column; gap: 12px; }
.row { display: flex; align-items: center; gap: 8px; }
.row.end { justify-content: flex-end; }
.grid2 { display: grid; grid-template-columns: 1fr 1fr; gap: 12px; }

.topbar {
  position: sticky;
  top: 0;
  z-index: 5;
  display: flex;
  align-items: center;
  gap: 8px;
  padding: calc(10px + var(--safe-t)) 16px 10px;
  background: color-mix(in srgb, var(--bg) 92%, transparent);
  backdrop-filter: blur(10px);
  -webkit-backdrop-filter: blur(10px);
  border-bottom: 1px solid var(--border);
  max-width: 100%;
}
.small-title { font-size: 1.15rem; overflow-wrap: anywhere; }
.title-btn { all: unset; font-weight: 700; font-size: 1.1rem; cursor: pointer; display: block; overflow-wrap: anywhere; }
.title-input { font-size: 1.15rem; font-weight: 700; width: 100%; }

.card {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 14px;
  display: block;
}
.card.active { border-color: var(--accent); box-shadow: 0 0 0 1px var(--accent) inset; }
.section-title { margin-top: 8px; color: var(--text-2); font-size: 0.9rem; text-transform: uppercase; letter-spacing: 0.04em; }

.btn, .chip {
  font: inherit;
  color: var(--text);
  background: var(--surface-2);
  border: 1px solid var(--border);
  border-radius: 12px;
  min-height: var(--tap);
  padding: 0 16px;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 6px;
  cursor: pointer;
  text-align: center;
  touch-action: manipulation;
}
.btn.primary, .chip.primary { background: var(--accent); border-color: var(--accent); color: var(--accent-ink); font-weight: 600; }
.btn.big { min-height: 52px; font-size: 1.05rem; }
.btn.ghost { background: transparent; }
.btn.danger { color: var(--danger); }
.btn.danger:not(.ghost) { border-color: color-mix(in srgb, var(--danger) 40%, var(--border)); }
.btn:disabled { opacity: 0.45; cursor: not-allowed; }
.chip { min-height: 34px; padding: 0 12px; border-radius: 999px; font-size: 0.9rem; white-space: nowrap; }
.chip.on { background: var(--accent-soft); border-color: var(--accent); color: var(--text); font-weight: 600; }
.chips { display: flex; gap: 6px; overflow-x: auto; padding-bottom: 2px; scrollbar-width: none; }
.chips::-webkit-scrollbar { display: none; }
.icon-btn {
  font: inherit; font-size: 1.6rem; line-height: 1;
  width: var(--tap); height: var(--tap); flex: none;
  display: inline-flex; align-items: center; justify-content: center;
  background: transparent; border: 0; color: var(--text-2); border-radius: 10px; cursor: pointer;
}
.icon-btn:active { background: var(--surface-2); }
.badge { display: inline-block; font-size: 0.75rem; font-weight: 600; padding: 2px 8px; border-radius: 999px; background: var(--accent-soft); color: var(--text); margin-left: 6px; }
.badge.up { background: var(--done); color: var(--done-ink); }

input, select, textarea {
  font: inherit;
  font-size: 16px; /* prevents iOS zoom on focus */
  color: var(--text);
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 10px;
  min-height: var(--tap);
  padding: 8px 10px;
  width: 100%;
}
textarea { resize: vertical; }
input:focus-visible, select:focus-visible, textarea:focus-visible, button:focus-visible, a:focus-visible, [tabindex]:focus-visible {
  outline: 2px solid var(--accent);
  outline-offset: 2px;
}
input.invalid { border-color: var(--danger); }
input.num { text-align: center; padding: 6px 4px; }
input[type='checkbox'] { width: 24px; height: 24px; min-height: 0; accent-color: var(--accent); }
label { display: flex; flex-direction: column; gap: 4px; }
label.row { flex-direction: row; }
.notes { min-height: 36px; font-size: 15px; background: transparent; border-style: dashed; }

.stats { display: grid; grid-template-columns: repeat(3, 1fr); gap: 8px; }
.stat { background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius); padding: 10px; display: flex; flex-direction: column; gap: 2px; min-width: 0; }
.stat strong { font-size: 1.25rem; font-variant-numeric: tabular-nums; overflow-wrap: anywhere; }

.list { list-style: none; margin: 0; padding: 0; }
.list.card { padding: 0; }
.list li + li { border-top: 1px solid var(--border); }
.list-item { all: unset; box-sizing: border-box; display: flex; flex-direction: column; gap: 4px; padding: 12px 16px; cursor: pointer; width: 100%; }
a.card.list-item, button.card.list-item { padding: 14px; border: 1px solid var(--border); border-radius: var(--radius); background: var(--surface); }
.empty { text-align: center; padding: 32px 8px; display: flex; flex-direction: column; gap: 10px; }

.plan { list-style: none; padding: 0; margin: 4px 0; display: flex; flex-direction: column; gap: 6px; }
.plan li { display: flex; justify-content: space-between; gap: 10px; }
.plan li span:last-child { text-align: right; font-variant-numeric: tabular-nums; }
details.card summary { cursor: pointer; min-height: 28px; }
details.card[open] summary { margin-bottom: 8px; }
details.card .btn { width: 100%; margin-top: 8px; }
.resume { display: flex; flex-direction: column; gap: 6px; border-color: var(--accent); }

/* Workout logger */
.exercise { display: flex; flex-direction: column; gap: 8px; padding: 12px; }
.set-row {
  display: grid;
  grid-template-columns: 34px minmax(0, 1fr) minmax(0, 1.1fr) minmax(0, 0.9fr) minmax(0, 0.8fr) 44px;
  gap: 6px;
  align-items: center;
  border-radius: 10px;
  padding: 2px;
}
.set-row.head { text-align: center; }
.set-row.done { background: var(--done); }
.set-row.done input { background: transparent; border-color: transparent; color: var(--done-ink); font-weight: 600; }
.set-row .prev { text-align: center; font-size: 0.85rem; font-variant-numeric: tabular-nums; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.set-label { font: inherit; font-weight: 700; height: 36px; border-radius: 8px; border: 0; background: var(--surface-2); color: var(--text); cursor: pointer; }
.set-label.warmup { color: var(--warn); }
.set-label.drop, .set-label.failure { color: var(--danger); }
.check {
  width: 44px; height: 40px; border-radius: 10px; border: 1px solid var(--border);
  background: var(--surface-2); color: var(--text-2); display: inline-flex; align-items: center; justify-content: center; cursor: pointer;
}
.set-row.done .check { background: var(--done-ink); color: var(--surface); border-color: var(--done-ink); }
.elapsed { font-variant-numeric: tabular-nums; }

/* Program editor */
.prog-ex { border-top: 1px solid var(--border); padding-top: 10px; display: flex; flex-direction: column; gap: 8px; }
.fields { display: grid; grid-template-columns: repeat(3, 1fr); gap: 6px; }
.field span { font-size: 0.75rem; }
.day-name { font-weight: 700; flex: 1; }

/* History table */
table.sets { width: 100%; border-collapse: collapse; margin-top: 8px; font-variant-numeric: tabular-nums; }
table.sets th { text-align: left; font-weight: 500; color: var(--text-2); font-size: 0.8rem; padding: 4px; }
table.sets td { padding: 6px 4px; border-top: 1px solid var(--border); }

/* Plate calculator */
.plates { display: flex; align-items: center; gap: 4px; flex-wrap: wrap; min-height: 80px; }
.plate { min-width: 34px; padding: 0 4px; display: inline-flex; align-items: center; justify-content: center; background: var(--accent); color: var(--accent-ink); border-radius: 6px; font-weight: 700; font-size: 0.85rem; }

/* Charts */
.chart { position: relative; margin-top: 8px; }
.chart svg { width: 100%; height: auto; display: block; touch-action: pan-y; overflow: visible; }
.chart .grid { stroke: var(--border); stroke-width: 1; }
.chart .axis { fill: var(--text-2); font-size: 11px; }
.chart .line { fill: none; stroke: var(--accent); stroke-width: 2; stroke-linejoin: round; stroke-linecap: round; }
.chart .dot { fill: var(--accent); stroke: var(--surface); stroke-width: 2; }
.chart .dot-active { fill: var(--accent); stroke: var(--surface); stroke-width: 2; }
.chart .crosshair { stroke: var(--text-2); stroke-width: 1; stroke-dasharray: 3 3; }
.chart .bar { fill: var(--accent); }
.chart .hit { fill: transparent; }
.chart .bar-hit:focus { outline: none; }
.chart .bar-hit:focus .bar, .chart .bar-hit:hover .bar { opacity: 0.8; }
.chart-tip {
  position: absolute; top: 0; transform: translateX(-50%);
  background: var(--surface); border: 1px solid var(--border); border-radius: 8px;
  padding: 4px 8px; display: flex; flex-direction: column; font-size: 0.8rem; pointer-events: none;
  box-shadow: 0 2px 8px rgb(0 0 0 / 0.12); white-space: nowrap;
}

/* Bottom tabs, rest timer, banners */
.tabs {
  position: fixed; left: 0; right: 0; bottom: 0; z-index: 10;
  display: grid; grid-template-columns: repeat(5, 1fr);
  background: var(--surface); border-top: 1px solid var(--border);
  padding-bottom: var(--safe-b);
}
.tab { display: flex; flex-direction: column; align-items: center; gap: 2px; padding: 8px 0 6px; font-size: 0.7rem; color: var(--text-2); min-height: 56px; }
.tab.on { color: var(--accent); font-weight: 600; }
.resume-bar { grid-column: 1 / -1; background: var(--accent); color: var(--accent-ink); text-align: center; padding: 8px; font-weight: 600; font-size: 0.9rem; }
body.in-workout .tabs { display: none; }

.rest-timer {
  position: fixed; left: 8px; right: 8px; bottom: calc(8px + var(--safe-b)); z-index: 11;
  max-width: 624px; margin: 0 auto;
  background: var(--surface); border: 1px solid var(--accent); border-radius: 16px; overflow: hidden;
  box-shadow: 0 6px 24px rgb(0 0 0 / 0.2);
}
body:not(.in-workout) .rest-timer { bottom: calc(72px + var(--safe-b)); }
.rest-progress { height: 4px; background: var(--accent); transform-origin: left; transition: transform 0.25s linear; }
.rest-row { display: flex; align-items: center; gap: 8px; padding: 8px 10px; }
.rest-time { font-size: 1.4rem; font-variant-numeric: tabular-nums; flex: 1; }
.rest-label { color: var(--text-2); font-size: 0.85rem; }

.update-bar { position: sticky; top: 0; z-index: 20; display: flex; gap: 10px; align-items: center; justify-content: center; padding: calc(6px + var(--safe-t)) 10px 6px; background: var(--accent-soft); font-size: 0.9rem; }
.update-bar[hidden] { display: none; }

.toast {
  position: fixed; left: 50%; bottom: calc(90px + var(--safe-b)); z-index: 30;
  transform: translate(-50%, 20px); opacity: 0; pointer-events: none;
  background: var(--text); color: var(--surface); padding: 10px 16px; border-radius: 12px; max-width: calc(100% - 32px);
  transition: opacity 0.2s, transform 0.2s; font-size: 0.95rem;
}
.toast.show { opacity: 1; transform: translate(-50%, 0); }

dialog.sheet {
  border: 0; padding: 0; margin: auto auto 0; width: 100%; max-width: 640px; max-height: 85vh;
  background: var(--surface); color: var(--text); border-radius: 18px 18px 0 0;
  display: flex; flex-direction: column;
}
dialog.sheet:not([open]) { display: none; }
dialog.sheet::backdrop { background: rgb(0 0 0 / 0.45); }
.sheet-head { display: flex; align-items: center; justify-content: space-between; padding: 12px 8px 4px 16px; }
.sheet-body { padding: 8px 16px calc(20px + var(--safe-b)); overflow-y: auto; }

@media (min-width: 700px) {
  dialog.sheet { margin: auto; border-radius: 18px; }
}
@media (prefers-reduced-motion: reduce) {
  * { transition: none !important; }
}
.set-row.done input::placeholder { color: transparent; }
.install { border-color: var(--accent); background: var(--accent-soft); }
