:root {
  --bg: #111418;
  --surface: #1b1f25;
  --surface-2: #252a32;
  --border: #2f3540;
  --text: #eef1f5;
  --muted: #98a2b3;
  --accent: #ff6b3d;
  --accent-text: #1a0d07;
  --done: #2fbf71;
  --danger: #ff5c5c;
  --radius: 14px;
  color-scheme: dark;
}

/* Light theme: follows the system unless Settings forces one (data-theme on <html>). */
@media (prefers-color-scheme: light) {
  :root:not([data-theme="dark"]) {
    --bg: #f4f5f7;
    --surface: #ffffff;
    --surface-2: #eceef2;
    --border: #d9dde4;
    --text: #151920;
    --muted: #5d6675;
    --accent: #e8531f;
    --accent-text: #ffffff;
    --done: #1f9d57;
    --danger: #d93636;
    color-scheme: light;
  }
}
:root[data-theme="light"] {
    --bg: #f4f5f7;
    --surface: #ffffff;
    --surface-2: #eceef2;
    --border: #d9dde4;
    --text: #151920;
    --muted: #5d6675;
    --accent: #e8531f;
    --accent-text: #ffffff;
    --done: #1f9d57;
    --danger: #d93636;
    color-scheme: light;
}

* { box-sizing: border-box; }

html, body {
  margin: 0;
  height: 100%;
  background: var(--bg);
  color: var(--text);
  font: 16px/1.45 system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
  -webkit-text-size-adjust: 100%;
  -webkit-tap-highlight-color: transparent;
  /* No double-tap zoom (it swallowed quick taps); pinch zoom still works. */
  touch-action: manipulation;
}

/* App shell: the page itself never scrolls. It is a column exactly as tall
   as the screen (update banner, content, rest timer, tab bar), and only the
   content scrolls. So the browser's own bars never collapse or expand, the
   tab bar never moves, and pull-to-refresh can't reload mid-workout. */
html { overflow: hidden; overscroll-behavior: none; }
body {
  display: flex;
  flex-direction: column;
  height: 100vh;
  height: 100dvh;
  overflow: hidden;
}

.app {
  flex: 1;
  min-height: 0;
  overflow-y: auto;
  overscroll-behavior: contain;
  -webkit-overflow-scrolling: touch;
  /* Content is at most 640px wide, centered in the full-width scroll area. */
  padding: max(12px, env(safe-area-inset-top))
    max(16px, env(safe-area-inset-right), calc((100% - 640px) / 2))
    max(32px, env(safe-area-inset-bottom))
    max(16px, env(safe-area-inset-left), calc((100% - 640px) / 2));
}
/* Once the tab bar or rest timer sits below the content, it covers the
   bottom safe area instead. */
.app:has(+ .restbar), .app:has(+ .tabbar) { padding-bottom: 24px; }
.update-banner + .app { padding-top: 12px; }

h1, h2 { margin: 0; line-height: 1.2; }
h1 { font-size: 1.5rem; }
h2 { font-size: 1.1rem; }
p { margin: 0.5rem 0; }
code { font-family: ui-monospace, "SF Mono", Consolas, monospace; }

.muted { color: var(--muted); font-size: 0.9rem; }
.notes { color: var(--muted); white-space: pre-line; }

/* Top bar */
.bar {
  display: flex;
  align-items: center;
  gap: 8px;
  min-height: 48px;
  margin-bottom: 12px;
}
.bar h1 { flex: 1; font-size: 1.25rem; }
.bar .actions { display: flex; gap: 8px; margin-left: auto; }
.title { margin: 4px 0 4px; }

/* Buttons */
button { font: inherit; color: inherit; cursor: pointer; }
.btn {
  min-height: 44px;
  padding: 0 16px;
  border: 1px solid var(--border);
  border-radius: 999px;
  background: var(--surface);
  font-weight: 600;
}
.btn:active { transform: scale(0.97); }
.btn.primary { background: var(--accent); border-color: var(--accent); color: var(--accent-text); }
.btn.ghost { background: transparent; border-color: transparent; padding: 0 8px; }
.btn.danger { color: var(--danger); background: transparent; }
.btn.small { min-height: 36px; font-size: 0.9rem; }
.btn.wide { display: block; width: 100%; margin: 16px 0; }
.icon {
  width: 40px;
  height: 40px;
  flex: none;
  border: none;
  border-radius: 10px;
  background: var(--surface-2);
  font-size: 1rem;
}
.icon.danger { color: var(--danger); }

/* Cards and lists */
.card {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 16px;
}
.empty { text-align: left; }
.empty blockquote {
  margin: 12px 0;
  padding: 10px 14px;
  border-left: 3px solid var(--accent);
  background: var(--surface-2);
  border-radius: 0 10px 10px 0;
}
.workouts, .exercises { list-style: none; margin: 0; padding: 0; display: grid; gap: 10px; }
.workout-card {
  width: 100%;
  display: flex;
  flex-direction: column;
  align-items: flex-start;
  gap: 2px;
  text-align: left;
}
.workout-title { font-weight: 650; font-size: 1.05rem; }

/* Workout view */
.progress { color: var(--muted); font-weight: 600; margin: 8px 0 14px; }
.progress.complete { color: var(--done); }
.exercise h2 { margin-bottom: 4px; }
.sets { display: flex; flex-wrap: wrap; gap: 8px; margin-top: 10px; }
.set {
  min-width: 72px;
  min-height: 56px;
  padding: 6px 12px;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  border: 1px solid var(--border);
  border-radius: 12px;
  background: var(--surface-2);
}
.set-reps { font-weight: 700; font-size: 1.1rem; }
.set-load { font-size: 0.8rem; color: var(--muted); }
.set.done { background: var(--done); border-color: var(--done); color: #fff; }
.set.done .set-load { color: rgba(255, 255, 255, 0.85); }
.footer-actions { display: flex; justify-content: space-between; margin-top: 20px; }

/* Logging a workout */
.last-time { color: var(--muted); font-size: 0.85rem; margin: 2px 0 0; }
.log-grid {
  display: grid;
  grid-template-columns: 22px minmax(56px, auto) 1fr 1fr 44px;
  gap: 6px 6px;
  align-items: center;
  margin-top: 10px;
}
.log-grid > .muted { font-size: 0.75rem; }
.log-row { display: contents; }
.log-row .target { font-size: 0.8rem; color: var(--muted); line-height: 1.2; }
.log-row input { min-height: 44px; padding: 8px; text-align: center; }
.log-row.done input { background: color-mix(in srgb, var(--done) 14%, var(--surface)); border-color: var(--done); }
.check {
  width: 44px;
  height: 44px;
  border: 1px solid var(--border);
  border-radius: 10px;
  background: var(--surface-2);
  color: var(--muted);
  font-size: 1.1rem;
  font-weight: 700;
}
.log-row.done .check { background: var(--done); border-color: var(--done); color: #fff; }
.row-error { grid-column: 1 / -1; color: var(--danger); font-size: 0.85rem; margin-top: -2px; }
.btn.primary.ready { background: var(--done); border-color: var(--done); color: #fff; }
.session-notes { margin-top: 16px; }

/* Bottom tab bar (Workouts / History / Settings), the last row of the shell */
.tabbar {
  flex: none;
  display: flex;
  justify-content: center;
  gap: 8px;
  padding: 8px 16px calc(8px + env(safe-area-inset-bottom));
  background: color-mix(in srgb, var(--bg) 88%, transparent);
  backdrop-filter: blur(12px);
  border-top: 1px solid var(--border);
}
.tab {
  flex: 1;
  max-width: 200px;
  min-height: 44px;
  border: none;
  border-radius: 999px;
  background: transparent;
  color: var(--muted);
  font-weight: 650;
}
.tab.current { background: var(--surface-2); color: var(--text); }

.exercise-title { display: flex; align-items: baseline; justify-content: space-between; gap: 8px; }
.small { font-size: 0.85rem; }
.center { text-align: center; }

/* Rest timer bar: a row of the shell between the content and the tab bar */
.restbar {
  position: relative;
  flex: none;
  margin: 8px auto calc(12px + env(safe-area-inset-bottom));
  width: min(608px, calc(100% - 24px));
  overflow: hidden;
  border-radius: 18px;
  background: var(--surface-2);
  border: 1px solid var(--border);
  box-shadow: 0 8px 30px rgba(0, 0, 0, 0.35);
  z-index: 10;
}
.restbar:has(+ .tabbar) { margin-bottom: 8px; }
.rest-progress {
  position: absolute;
  inset: 0 auto 0 0;
  background: color-mix(in srgb, var(--accent) 22%, transparent);
  transition: width 0.25s linear;
}
.restbar.over { background: var(--done); border-color: var(--done); color: #fff; }
.restbar.over .rest-progress { display: none; }
.rest-content { position: relative; display: flex; align-items: center; gap: 8px; padding: 10px 12px; }
.rest-label { font-weight: 600; }
.rest-time { font-size: 1.6rem; font-weight: 750; font-variant-numeric: tabular-nums; margin-right: auto; }
.rest-btn {
  min-width: 48px;
  min-height: 44px;
  border: 1px solid var(--border);
  border-radius: 12px;
  background: var(--surface);
  font-weight: 650;
}
.rest-btn.strong { background: var(--accent); border-color: var(--accent); color: var(--accent-text); }
.restbar.over .rest-btn.strong { background: #fff; border-color: #fff; color: #145c35; }

/* Settings */
.settings { display: grid; gap: 10px; margin-bottom: 12px; }
.settings h2 { margin-bottom: -2px; }
.settings p { margin: 0; }
.chips { display: flex; flex-wrap: wrap; gap: 8px; }
.toggle { display: flex; align-items: center; justify-content: space-between; gap: 12px; min-height: 44px; }
.toggle input {
  appearance: none;
  flex: none;
  width: 50px;
  height: 30px;
  border-radius: 999px;
  background: var(--border);
  position: relative;
  cursor: pointer;
  transition: background 0.15s;
}
.toggle input::after {
  content: "";
  position: absolute;
  top: 3px;
  left: 3px;
  width: 24px;
  height: 24px;
  border-radius: 50%;
  background: #fff;
  transition: transform 0.15s;
}
.toggle input:checked { background: var(--done); }
.toggle input:checked::after { transform: translateX(20px); }
.toggle input:focus-visible { outline: 2px solid var(--accent); outline-offset: 2px; }
.button-row { display: flex; flex-wrap: wrap; gap: 8px; }
.file-btn { position: relative; display: inline-flex; align-items: center; cursor: pointer; }
.file-btn input { position: absolute; width: 1px; height: 1px; opacity: 0; pointer-events: none; }
.file-btn:focus-within { outline: 2px solid var(--accent); outline-offset: 2px; }
.success {
  color: var(--done);
  background: color-mix(in srgb, var(--done) 12%, transparent);
  border-radius: 10px;
  padding: 10px 14px;
}
.nudge {
  display: flex;
  align-items: center;
  gap: 12px;
  margin-bottom: 12px;
  border-color: color-mix(in srgb, var(--accent) 50%, var(--border));
}
.nudge p { margin: 0; flex: 1; font-size: 0.9rem; }
a { color: var(--accent); }

/* History tab: Sessions | Exercises switch */
.segmented {
  display: flex;
  gap: 4px;
  padding: 4px;
  margin: 0 0 12px;
  border-radius: 999px;
  background: var(--surface-2);
}
.segment {
  flex: 1;
  min-height: 38px;
  border: none;
  border-radius: 999px;
  background: transparent;
  color: var(--muted);
  font-weight: 650;
}
.segment.selected { background: var(--surface); color: var(--text); box-shadow: 0 1px 3px rgba(0, 0, 0, 0.2); }

/* Exercises list and detail */
.exercise-row { flex-direction: row; align-items: center; justify-content: space-between; gap: 12px; }
.row-main { display: flex; flex-direction: column; gap: 2px; min-width: 0; }
.row-stat { display: flex; flex-direction: column; align-items: flex-end; text-align: right; flex: none; }
.stat-value { font-weight: 700; font-size: 1.05rem; }
.stats { display: grid; grid-template-columns: repeat(3, 1fr); gap: 8px; margin: 12px 0; }
.stat {
  display: flex;
  flex-direction: column;
  gap: 2px;
  padding: 10px 12px;
  border-radius: 12px;
  background: var(--surface);
  border: 1px solid var(--border);
  min-width: 0;
}
.stat .stat-value { overflow-wrap: anywhere; }
.section-title { margin: 20px 0 10px; }
.pill {
  display: inline-block;
  padding: 1px 8px;
  border-radius: 999px;
  background: var(--accent);
  color: var(--accent-text);
  font-size: 0.72rem;
  font-weight: 750;
  letter-spacing: 0.02em;
  vertical-align: middle;
}
.pr-note { margin: 6px 0 0; font-size: 0.9rem; }
.workout-card .muted .pill { margin-left: 8px; }
.merge { flex-direction: column; align-items: stretch; }
.merge p { font-size: 0.95rem; }

/* Progress chart */
.chart { margin: 0 0 8px; padding: 12px 12px 6px; }
.chart figcaption { margin-bottom: 2px; }
.readout { margin: 0 0 6px; font-weight: 600; font-size: 0.95rem; min-height: 1.4em; }
.chart svg { display: block; width: 100%; height: auto; touch-action: pan-y; cursor: crosshair; }
.chart svg:focus-visible { outline: 2px solid var(--accent); outline-offset: 4px; border-radius: 6px; }
.chart .grid { stroke: var(--border); stroke-width: 1; }
.chart .axis { fill: var(--muted); font-size: 11px; }
.chart .crosshair { stroke: var(--muted); stroke-width: 1; }
.chart .series { fill: none; stroke: var(--accent); stroke-width: 2; stroke-linejoin: round; stroke-linecap: round; }
.chart .marker { fill: var(--accent); stroke: var(--surface); stroke-width: 2; }
.chart .direct-label { fill: var(--text); font-size: 11px; font-weight: 650; }

/* Supersets */
.exercise.superset { border-left: 3px solid var(--accent); }
.exercise.superset:not(.superset-start) { margin-top: -6px; }
.superset-note { margin: 0 0 6px; color: var(--muted); font-size: 0.8rem; font-weight: 600; }
.superset-pill { background: transparent; color: var(--text); border: 1.5px solid var(--accent); }
.small-toggle { min-height: 36px; font-size: 0.9rem; color: var(--muted); }

/* Update banner */
.update-banner {
  flex: none;
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  padding: max(8px, env(safe-area-inset-top)) 16px 8px;
  background: var(--accent);
  color: var(--accent-text);
  font-weight: 600;
  font-size: 0.9rem;
}
.update-banner .btn { background: var(--accent-text); color: var(--accent); border-color: var(--accent-text); }

/* Workout in progress: plates, card actions, add/swap forms */
.log-row .target {
  border: none;
  background: none;
  padding: 0;
  text-align: left;
  color: var(--muted);
  font-size: 0.8rem;
  line-height: 1.2;
  text-decoration: underline dotted;
  text-underline-offset: 3px;
  min-height: 44px;
}
.row-plates {
  grid-column: 1 / -1;
  font-size: 0.85rem;
  background: var(--surface-2);
  border-radius: 8px;
  padding: 6px 10px;
}
.card-actions { display: flex; flex-wrap: wrap; gap: 0 16px; margin-top: 6px; }
.link-btn {
  border: none;
  background: none;
  padding: 0;
  min-height: 40px;
  color: var(--accent);
  font-weight: 650;
  font-size: 0.9rem;
}
.link-btn.danger { color: var(--danger); }
.inline-form { display: flex; align-items: center; gap: 8px; flex-wrap: wrap; margin: 8px 0; }
.inline-form input[type="text"] { flex: 1; min-width: 140px; }
.add-exercise { margin: 12px 0; }
.icon:disabled { opacity: 0.35; cursor: default; }

/* Programs */
.program-card { display: grid; gap: 10px; }
.program-head {
  display: flex;
  flex-direction: column;
  align-items: flex-start;
  gap: 2px;
  border: none;
  background: none;
  padding: 0;
  text-align: left;
}
.next-btn { width: 100%; }
.day-chips { display: flex; flex-wrap: wrap; gap: 6px; }
.day-chips .chip { min-height: 34px; padding: 0 12px; font-size: 0.85rem; }
.pill.light { background: rgba(255, 255, 255, 0.25); color: inherit; margin-left: 6px; }
.program-days { padding-left: 20px; margin: 10px 0 0; }
.program-days li { margin: 6px 0; }

/* Bodyweight */
.entry-row { display: flex; align-items: center; justify-content: space-between; padding: 10px 14px; }

/* Calendar */
.calendar { margin-bottom: 12px; }
.stats.compact { margin: 0 0 12px; }
.stats.compact .stat { padding: 8px 10px; }
.cal-head { display: flex; align-items: center; justify-content: space-between; margin-bottom: 8px; }
.cal-grid { display: grid; grid-template-columns: repeat(7, 1fr); gap: 4px; }
.cal-dow { text-align: center; font-size: 0.75rem; color: var(--muted); font-weight: 600; }
.cal-cell {
  aspect-ratio: 1;
  display: flex;
  align-items: center;
  justify-content: center;
  border-radius: 8px;
  font-size: 0.85rem;
  color: var(--muted);
}
.cal-cell.trained { border: none; background: var(--accent); color: var(--accent-text); font-weight: 700; }
.cal-cell.today { box-shadow: inset 0 0 0 2px var(--text); color: var(--text); }
.cal-cell.trained.today { box-shadow: inset 0 0 0 2px var(--text); }

/* Forms */
.field { display: grid; gap: 6px; margin: 0 0 14px; border: none; padding: 0; }
.field > span, .field legend { font-size: 0.9rem; color: var(--muted); font-weight: 600; padding: 0; }
input[type="text"], textarea {
  width: 100%;
  min-height: 44px;
  padding: 10px 12px;
  border: 1px solid var(--border);
  border-radius: 10px;
  background: var(--surface);
  color: var(--text);
  font: inherit;
  font-size: 16px; /* prevents iOS zoom on focus */
}
input:focus, textarea:focus { outline: 2px solid var(--accent); outline-offset: -1px; border-color: transparent; }
textarea { resize: vertical; }
textarea.code { font-family: ui-monospace, "SF Mono", Consolas, monospace; word-break: break-all; }

.unit { display: flex; gap: 8px; align-items: center; }
.unit legend { margin-bottom: 6px; }
.chip {
  display: inline-flex;
  align-items: center;
  min-height: 40px;
  padding: 0 18px;
  border: 1px solid var(--border);
  border-radius: 999px;
  background: var(--surface);
  font-weight: 600;
}
/* Keep hidden inputs inside their label, not at the page's edge. */
.chip { position: relative; }
.chip input { position: absolute; opacity: 0; pointer-events: none; }
button.chip { cursor: pointer; font: inherit; color: inherit; }
.chip.selected { background: var(--accent); border-color: var(--accent); color: var(--accent-text); }

.exercise.edit { display: grid; gap: 10px; }
.exercise-head { display: flex; gap: 6px; align-items: center; }
.exercise-head .name { flex: 1; font-weight: 600; }
.set-grid {
  display: grid;
  grid-template-columns: 36px 1fr 1fr 40px;
  gap: 6px 8px;
  align-items: center;
}
.set-grid .muted { font-size: 0.8rem; }
.set-no { text-align: center; color: var(--muted); font-weight: 600; }
.hint { margin: 4px 0 0; }

/* Import / errors */
.error {
  color: var(--danger);
  white-space: pre-line;
  background: color-mix(in srgb, var(--danger) 10%, transparent);
  border-radius: 10px;
  padding: 10px 14px;
  margin: 12px 0;
}
ul.error { padding-left: 28px; }
.preview ul { margin: 10px 0 0; padding-left: 18px; }
.preview li { margin: 4px 0; }
