:root {
  --bg: #111418;
  --surface: #1a1f25;
  --surface-2: #232a32;
  --border: #2e3640;
  --text: #eef1f4;
  --muted: #9aa5b1;
  --accent: #4f9cf9;
  --accent-text: #0b1220;
  --good: #3ecf8e;
  --danger: #f2555a;
  --radius: 14px;
  --tap: 48px;
  --safe-bottom: env(safe-area-inset-bottom, 0px);
  --safe-top: env(safe-area-inset-top, 0px);
  --chart-line: #4592f5;
  color-scheme: dark;
}

@media (prefers-color-scheme: light) {
  :root {
    --bg: #f4f6f8;
    --surface: #ffffff;
    --surface-2: #eef1f4;
    --border: #dde2e7;
    --text: #12171c;
    --muted: #5d6874;
    --accent: #1f6fe0;
    --accent-text: #ffffff;
    --good: #138a55;
    --danger: #d23a3f;
    --chart-line: #1f6fe0;
    color-scheme: light;
  }
}

* { box-sizing: border-box; }

/* Component display rules (e.g. .tabbar { display: grid }) must not beat [hidden]. */
[hidden] { display: none !important; }

html, body {
  margin: 0;
  background: var(--bg);
  color: var(--text);
  font: 16px/1.4 -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, sans-serif;
  -webkit-text-size-adjust: 100%;
  -webkit-tap-highlight-color: transparent;
}

button, input, textarea, select { font: inherit; color: inherit; }

.topbar {
  position: sticky;
  top: 0;
  z-index: 10;
  display: flex;
  align-items: center;
  gap: 8px;
  padding: calc(10px + var(--safe-top)) 16px 10px;
  background: color-mix(in srgb, var(--bg) 90%, transparent);
  backdrop-filter: blur(10px);
  border-bottom: 1px solid var(--border);
}
.topbar h1 {
  flex: 1;
  margin: 0;
  font-size: 18px;
  font-weight: 650;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}
.save-state { font-size: 13px; color: var(--muted); }

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

.tabbar {
  position: fixed;
  left: 0; right: 0; bottom: 0;
  z-index: 10;
  display: grid;
  grid-template-columns: repeat(4, 1fr);
  padding-bottom: var(--safe-bottom);
  background: var(--surface);
  border-top: 1px solid var(--border);
}
.tabbar a {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 2px;
  min-height: 56px;
  justify-content: center;
  font-size: 12px;
  color: var(--muted);
  text-decoration: none;
}
.tabbar a.active { color: var(--accent); }
.tab-icon { width: 24px; height: 24px; fill: none; stroke: currentColor; stroke-width: 1.8;
            stroke-linecap: round; stroke-linejoin: round; }

h2 { font-size: 15px; font-weight: 600; color: var(--muted); margin: 24px 4px 8px;
     text-transform: uppercase; letter-spacing: .04em; }
h2:first-child { margin-top: 4px; }

.card {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 14px;
  margin-bottom: 12px;
}
.card-title { font-weight: 650; font-size: 17px; }
.muted { color: var(--muted); font-size: 14px; }
.row { display: flex; align-items: center; gap: 8px; }
.row > .grow { flex: 1; min-width: 0; }
.stack > * + * { margin-top: 8px; }

a.card { display: block; color: inherit; text-decoration: none; }
a.card:active, .card.tappable:active { background: var(--surface-2); }

.btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 6px;
  min-height: var(--tap);
  padding: 0 18px;
  border-radius: 12px;
  border: 1px solid var(--border);
  background: var(--surface-2);
  font-weight: 600;
  cursor: pointer;
  text-decoration: none;
  touch-action: manipulation;
}
.btn:active { transform: scale(.98); }
.btn[disabled] { opacity: .5; }
.btn.primary { background: var(--accent); border-color: var(--accent); color: var(--accent-text); }
.btn.good { background: var(--good); border-color: var(--good); color: #06140d; }
.btn.danger { color: var(--danger); }
.btn.ghost { background: transparent; }
.btn.block { width: 100%; }
.btn.small { min-height: 36px; padding: 0 12px; font-size: 14px; }

.icon-btn {
  min-width: 40px;
  min-height: 40px;
  border: 0;
  background: transparent;
  font-size: 22px;
  line-height: 1;
  border-radius: 10px;
  cursor: pointer;
  color: var(--muted);
  touch-action: manipulation;
}
.icon-btn:active { background: var(--surface-2); }
#back-btn { font-size: 32px; margin-left: -8px; color: var(--accent); }

input[type=text], input[type=number], input:not([type]), textarea, select {
  width: 100%;
  min-height: 44px;
  padding: 8px 12px;
  border-radius: 10px;
  border: 1px solid var(--border);
  background: var(--surface-2);
  font-size: 16px; /* >=16px stops iOS zooming on focus */
}
input:focus, textarea:focus, select:focus { outline: 2px solid var(--accent); outline-offset: -1px; }
textarea { min-height: 88px; resize: vertical; }
label.field { display: block; }
.field > span { display: block; font-size: 13px; color: var(--muted); margin: 0 0 4px 2px; }

/* Workout logging */
.exercise-name {
  font-weight: 650;
  font-size: 17px;
  border: 0;
  background: transparent;
  padding: 4px 0;
  min-height: 36px;
}
.last-time { font-size: 13px; color: var(--muted); min-width: 0; }
.set-grid {
  display: grid;
  grid-template-columns: 40px 1fr 1fr 52px 32px;
  gap: 8px;
  align-items: center;
}
.set-grid.head { font-size: 12px; color: var(--muted); text-transform: uppercase;
                 letter-spacing: .04em; margin-bottom: 4px; }
.set-grid + .set-grid { margin-top: 8px; }
.set-num { text-align: center; color: var(--muted); font-weight: 600; }
.set-grid input { text-align: center; padding: 8px 4px; font-weight: 600; }
.set-grid.done input { border-color: color-mix(in srgb, var(--good) 60%, transparent);
                       background: color-mix(in srgb, var(--good) 12%, var(--surface-2)); }
.check {
  width: 52px;
  height: 44px;
  border-radius: 10px;
  border: 1px solid var(--border);
  background: var(--surface-2);
  font-size: 20px;
  color: var(--muted);
  cursor: pointer;
  touch-action: manipulation;
}
.check.on { background: var(--good); border-color: var(--good); color: #06140d; }
.remove-set { font-size: 18px; min-width: 32px; min-height: 32px; }

.pill { display: inline-block; padding: 2px 8px; border-radius: 999px; font-size: 12px;
        font-weight: 600; background: var(--surface-2); color: var(--muted); }
.pill.live { background: color-mix(in srgb, var(--accent) 20%, transparent); color: var(--accent); }

.center { text-align: center; }
.login {
  min-height: 70vh;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 16px;
  text-align: center;
}
.login .logo { font-size: 56px; }
.login h1 { margin: 0; font-size: 28px; }

.toast {
  position: fixed;
  left: 16px; right: 16px;
  bottom: calc(72px + var(--safe-bottom));
  z-index: 20;
  max-width: 480px;
  margin: 0 auto;
  padding: 12px 16px;
  border-radius: 12px;
  background: var(--text);
  color: var(--bg);
  font-weight: 500;
  text-align: center;
}
.toast.error { background: var(--danger); color: #fff; }

.spinner { padding: 48px 0; text-align: center; color: var(--muted); }
.actions { display: flex; gap: 8px; margin-top: 16px; }
.actions > * { flex: 1; }
.summary-sets { font-size: 14px; color: var(--muted); }

.card-btn { display: block; width: 100%; text-align: left; cursor: pointer; font: inherit; color: inherit; }
.add-set { margin-top: 10px; }

/* Supersets */
.superset {
  border-left: 3px solid var(--accent);
  padding-left: 10px;
  margin-bottom: 12px;
}
.superset > .card { margin-bottom: 0; }
.superset-head { display: flex; flex-wrap: wrap; align-items: baseline; gap: 4px 10px; margin: 0 0 8px 2px; }
.superset-title { font-weight: 700; color: var(--accent); font-size: 14px;
                  text-transform: uppercase; letter-spacing: .04em; }
.superset-head .muted { font-size: 13px; }
.ss-badge {
  flex: none;
  min-width: 32px;
  padding: 3px 6px;
  border-radius: 8px;
  background: color-mix(in srgb, var(--accent) 20%, transparent);
  color: var(--accent);
  font-weight: 700;
  font-size: 13px;
  text-align: center;
}
.link-row { display: flex; justify-content: center; margin: -4px 0 8px; }
.superset .link-row { margin: 4px 0; }
.link-btn {
  min-height: 36px;
  padding: 0 14px;
  border: 1px dashed var(--border);
  border-radius: 999px;
  background: transparent;
  color: var(--muted);
  font-size: 13px;
  font-weight: 600;
  cursor: pointer;
  touch-action: manipulation;
}
.link-btn.linked { border-style: solid; color: var(--accent); border-color: color-mix(in srgb, var(--accent) 50%, transparent); }

/* Splits */
.split-card { border-color: color-mix(in srgb, var(--accent) 45%, var(--border)); }
.next-up { font-size: 20px; margin: 2px 0; }
.next-btn { margin: 12px 0 10px; }
.split-days { display: flex; gap: 8px; overflow-x: auto; padding-bottom: 2px; scrollbar-width: none; }
.split-days::-webkit-scrollbar { display: none; }
.day-chip {
  flex: none;
  display: inline-flex;
  align-items: center;
  gap: 6px;
  min-height: 40px;
  padding: 0 12px 0 6px;
  border-radius: 999px;
  border: 1px solid var(--border);
  background: var(--surface-2);
  font-size: 14px;
  font-weight: 600;
  cursor: pointer;
  touch-action: manipulation;
}
.day-chip.next { border-color: var(--accent); color: var(--accent); }
.day-chip[disabled] { opacity: .5; }
.day-num {
  flex: none;
  display: inline-grid;
  place-items: center;
  width: 26px;
  height: 26px;
  border-radius: 50%;
  background: var(--bg);
  color: var(--muted);
  font-size: 13px;
  font-weight: 700;
}
.day-row { margin-bottom: 8px; padding: 8px 8px 8px 12px; }
.toggle { margin-top: 16px; cursor: pointer; }
.toggle input { width: 22px; height: 22px; accent-color: var(--accent); }

/* Split templates */
.rotation { display: flex; flex-wrap: wrap; gap: 6px; }
.day-chip.static { cursor: default; min-height: 34px; font-size: 13px; }
.day-details { border-top: 1px solid var(--border); padding-top: 8px; }
.day-details summary { cursor: pointer; font-weight: 600; min-height: 32px; }
.day-details ul { margin: 6px 0 4px; padding-left: 18px; }
.day-details li { margin: 3px 0; }
.day-details li.ss-linked { list-style: none; margin-left: -12px; }
.ss-plus { color: var(--accent); font-weight: 700; }
.card > p { margin: 0; }

/* Exercise swap */
.ex-tools { display: flex; align-items: center; gap: 4px; margin: 4px 0 8px; }
.ex-tools .grow { flex: 1; }
.icon-btn.small { min-width: 36px; min-height: 36px; font-size: 18px; }
.last-time { margin: 0 0 8px; }
.swap-btn { flex: none; min-height: 32px; padding: 0 12px; }
.sheet-backdrop {
  position: fixed;
  inset: 0;
  z-index: 30;
  display: flex;
  align-items: flex-end;
  justify-content: center;
  background: rgb(0 0 0 / .5);
}
.sheet {
  width: 100%;
  max-width: 640px;
  max-height: 80vh;
  overflow-y: auto;
  padding: 16px 16px calc(16px + var(--safe-bottom));
  border-radius: 18px 18px 0 0;
  background: var(--surface);
  border: 1px solid var(--border);
  border-bottom: 0;
}
.sheet-title { font-size: 18px; font-weight: 700; margin-bottom: 4px; }
.sheet h2 { margin-top: 16px; }
.sheet-option {
  display: block;
  width: 100%;
  min-height: var(--tap);
  padding: 0 14px;
  margin-bottom: 6px;
  border-radius: 12px;
  border: 1px solid var(--border);
  background: var(--surface-2);
  text-align: left;
  font-weight: 600;
  cursor: pointer;
  touch-action: manipulation;
}
.sheet-option:active { background: var(--border); }
.sheet-cancel { margin-top: 12px; }

/* Double progression */
.suggestion {
  display: flex;
  gap: 8px;
  align-items: flex-start;
  margin: 0 0 10px;
  padding: 8px 10px;
  border-radius: 10px;
  font-size: 13px;
  line-height: 1.35;
  background: var(--surface-2);
  color: var(--muted);
}
.suggestion-icon { font-weight: 800; }
.suggestion.increase { background: color-mix(in srgb, var(--good) 14%, transparent); color: var(--text); }
.suggestion.increase .suggestion-icon { color: var(--good); }
.suggestion.reps .suggestion-icon, .suggestion.maxed .suggestion-icon { color: var(--accent); }
.template-fields { display: grid; grid-template-columns: 1fr 1.6fr 1.4fr; gap: 8px; }
.range-inputs { display: flex; align-items: center; gap: 4px; }
.range-inputs input { text-align: center; padding: 8px 4px; }
.set-grid.head > span { white-space: nowrap; }
.welcome { margin-bottom: 12px; }
.welcome .muted { margin-top: 4px; }

/* Rest timer */
.rest-bar {
  position: fixed;
  left: 16px; right: 16px;
  bottom: calc(16px + var(--safe-bottom));
  z-index: 25;
  max-width: 480px;
  margin: 0 auto;
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 8px 8px 8px 16px;
  border-radius: 16px;
  background: var(--surface);
  border: 1px solid var(--accent);
  box-shadow: 0 6px 24px rgb(0 0 0 / .35);
}
.rest-bar[hidden] { display: none; }
.rest-label { color: var(--muted); font-size: 13px; font-weight: 600; text-transform: uppercase; letter-spacing: .04em; }
.rest-time { flex: 1; font-size: 24px; font-weight: 750; font-variant-numeric: tabular-nums; }
body:has(.tabbar:not([hidden])) .rest-bar { bottom: calc(72px + var(--safe-bottom)); }
body:has(.rest-bar:not([hidden])) .toast { bottom: calc(140px + var(--safe-bottom)); }

/* History tabs */
.segmented { display: grid; grid-template-columns: 1fr 1fr; gap: 4px; padding: 4px; margin-bottom: 14px;
             background: var(--surface-2); border-radius: 12px; }
.seg { display: flex; align-items: center; justify-content: center; min-height: 40px; border-radius: 9px;
       color: var(--muted); text-decoration: none; font-weight: 600; font-size: 14px; }
.seg.active { background: var(--surface); color: var(--text); box-shadow: 0 1px 3px rgb(0 0 0 / .2); }

/* Progress */
.pill.pr { background: color-mix(in srgb, var(--good) 18%, transparent); color: var(--good); margin-left: 6px; }
.stat-row { display: grid; grid-template-columns: 1fr 1fr; gap: 12px; margin-bottom: 12px; }
.stat { background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius); padding: 12px 14px; }
.stat-label { font-size: 12px; color: var(--muted); text-transform: uppercase; letter-spacing: .04em; }
.stat-value { font-size: 26px; font-weight: 750; font-variant-numeric: tabular-nums; margin: 2px 0; }
.chart-title { font-weight: 600; margin-bottom: 6px; }
.chart-wrap { position: relative; }
.chart { display: block; touch-action: pan-y; }
.chart .grid { stroke: var(--border); stroke-width: 1; }
.chart .axis { fill: var(--muted); font-size: 11px; font-variant-numeric: tabular-nums; }
.chart .line { fill: none; stroke: var(--chart-line); stroke-width: 2; stroke-linejoin: round; stroke-linecap: round; }
.chart .dot { fill: var(--chart-line); stroke: var(--surface); stroke-width: 2; }
.chart .point-label { fill: var(--text); font-size: 11px; font-weight: 600; }
.chart .crosshair { stroke: var(--muted); stroke-width: 1; stroke-dasharray: 3 3; }
.chart-tip { position: absolute; top: 0; transform: translateX(-50%); pointer-events: none; padding: 6px 10px;
             border-radius: 10px; background: var(--surface-2); border: 1px solid var(--border);
             font-size: 13px; white-space: nowrap; box-shadow: 0 4px 14px rgb(0 0 0 / .25); }
table.sessions { width: 100%; border-collapse: collapse; font-variant-numeric: tabular-nums; }
.sessions th { text-align: left; font-size: 12px; color: var(--muted); font-weight: 600; padding: 6px 4px;
               text-transform: uppercase; letter-spacing: .04em; border-bottom: 1px solid var(--border); }
.sessions td { padding: 10px 4px; border-bottom: 1px solid var(--border); vertical-align: top; }
.muted.small { font-size: 12px; }

/* Smartwatch reminder */
.start-card {
  display: flex;
  align-items: center;
  gap: 12px;
  margin-bottom: 14px;
  padding: 12px 12px 12px 14px;
  border-radius: var(--radius);
  background: color-mix(in srgb, var(--accent) 16%, var(--surface));
  border: 1px solid color-mix(in srgb, var(--accent) 45%, transparent);
  font-size: 14px;
}
.start-card .watch-icon { font-size: 26px; line-height: 1; }
.start-card .grow { flex: 1; }

/* Percentage programs */
.set-num { display: flex; flex-direction: column; align-items: center; line-height: 1.1; }
.set-num small { font-size: 10px; font-weight: 600; color: var(--muted); }
.set-num .amrap-tag { color: var(--accent); }
.set-grid input.amrap { border-color: color-mix(in srgb, var(--accent) 60%, transparent); }
.ex-note { font-size: 13px; color: var(--muted); margin: 0 0 8px; font-style: italic; }
.program-inputs { display: grid; gap: 10px; padding-top: 8px; border-top: 1px solid var(--border); }
.program-summary { padding: 8px 10px; border-radius: 10px; background: var(--surface-2); font-size: 14px; }
.tm-field { margin-bottom: 10px; }

/* Exercise picker */
.sheet input[type=search] { margin: 8px 0 12px; }
.group-option { display: flex; justify-content: space-between; align-items: center; }
.create-option { color: var(--accent); border-style: dashed; }
.sheet-back { border: 0; background: none; color: var(--accent); font-size: 16px; font-weight: 600;
              min-height: 40px; padding: 0; cursor: pointer; }
.create-form { display: flex; gap: 8px; align-items: center; }
.create-form input { flex: 1; }
.create-form .btn { flex: none; min-height: 44px; }

.info-banner { margin-bottom: 14px; padding: 10px 12px; border-radius: 10px; font-size: 14px;
               background: color-mix(in srgb, var(--accent) 12%, var(--surface)); border: 1px solid var(--border); }

/* Quick workouts */
.choose-split { display: block; color: inherit; text-decoration: none; }
.quick-grid { display: grid; grid-template-columns: repeat(2, 1fr); gap: 8px; margin-bottom: 8px; }
.quick-btn {
  display: flex; flex-direction: column; align-items: flex-start; gap: 2px;
  min-height: 64px; padding: 10px 12px;
  border-radius: 12px; border: 1px solid var(--border); background: var(--surface);
  text-align: left; cursor: pointer; touch-action: manipulation;
}
.quick-btn:active { background: var(--surface-2); }
.quick-name { font-weight: 650; }
.quick-btn .muted { font-size: 13px; }

/* App feel */
html, body { overscroll-behavior-y: none; }
button, .tabbar, .btn, .seg, .day-chip, .quick-btn, .sheet-option, .check { -webkit-user-select: none; user-select: none; }
.tabbar a { -webkit-touch-callout: none; }
.tabbar a:active { opacity: .7; }
.install-card .muted { margin: 4px 0 10px; }

/* Screen transitions (View Transitions API) */
::view-transition-old(root) { animation: vt-out 120ms ease-in both; }
::view-transition-new(root) { animation: vt-in 180ms ease-out both; }
@keyframes vt-out { to { opacity: 0; } }
@keyframes vt-in { from { opacity: 0; transform: translateY(8px); } }
.topbar { view-transition-name: topbar; }
.tabbar { view-transition-name: tabbar; }
::view-transition-group(topbar), ::view-transition-group(tabbar),
::view-transition-old(topbar), ::view-transition-new(topbar),
::view-transition-old(tabbar), ::view-transition-new(tabbar) { animation: none; }

/* Loading skeletons */
.skeleton { display: grid; gap: 12px; }
.sk { border-radius: var(--radius); background: linear-gradient(90deg, var(--surface) 25%, var(--surface-2) 50%, var(--surface) 75%);
      background-size: 200% 100%; animation: shimmer 1.2s linear infinite; }
.sk-title { height: 22px; width: 40%; border-radius: 8px; }
.sk-card { height: 84px; }
.sk-card.short { width: 70%; }
@keyframes shimmer { to { background-position: -200% 0; } }
@media (prefers-reduced-motion: reduce) { .sk { animation: none; } }

.start-card { flex-direction: column; align-items: stretch; }
.start-row { display: flex; align-items: center; gap: 12px; }
.start-actions { display: flex; gap: 8px; justify-content: flex-end; }
.start-actions .btn { flex: 1; }
.workout-meta { align-items: center; margin: 4px 0 12px; }
.workout-meta p { margin: 0; }
.rest-toggle { min-height: 34px; padding: 0 12px; flex: none; }
