:root {
  color-scheme: light;
  --page: #f9f9f7;
  --surface: #fcfcfb;
  --surface-2: #f0efec;
  --ink: #0b0b0b;
  --ink-2: #52514e;
  --muted: #6b6a65;
  --grid: #e1e0d9;
  --axis: #c3c2b7;
  --border: rgba(11, 11, 11, 0.10);
  --accent: #c2410c;
  --accent-ink: #ffffff;
  --accent-soft: rgba(235, 104, 52, 0.12);
  --series: #2a78d6;
  --danger: #c02f2f;
  --good: #006300;
  --good-soft: rgba(12, 163, 12, 0.10);
  --radius: 14px;
  --tabs-h: 64px;
  --shadow: 0 1px 2px rgba(0, 0, 0, 0.04);
}

@media (prefers-color-scheme: dark) {
  :root {
    color-scheme: dark;
    --page: #0d0d0d;
    --surface: #1a1a19;
    --surface-2: #262624;
    --ink: #ffffff;
    --ink-2: #c3c2b7;
    --muted: #9a9890;
    --grid: #2c2c2a;
    --axis: #383835;
    --border: rgba(255, 255, 255, 0.10);
    --accent: #d9531e;
    --accent-soft: rgba(235, 104, 52, 0.18);
    --series: #3987e5;
    --danger: #e66767;
    --good: #0ca30c;
    --good-soft: rgba(12, 163, 12, 0.16);
    --shadow: none;
  }
}

* { box-sizing: border-box; }
[hidden] { display: none !important; }

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

h1, h2, h3 { margin: 0; line-height: 1.2; }
h1 { font-size: 1.6rem; font-weight: 700; }
h3 { font-size: 1.05rem; font-weight: 600; }
p { margin: 0; }
a { color: inherit; }
.muted { color: var(--muted); }
.small { font-size: 0.875rem; }
.ellipsis { white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.stack > * + * { margin-top: 12px; }
.error { color: var(--danger); }

/* Profile bar */
.profile-bar {
  position: sticky;
  top: 0;
  z-index: 10;
  background: var(--page);
  padding: calc(env(safe-area-inset-top, 0px) + 10px) 16px 10px;
  border-bottom: 1px solid var(--border);
}
.chips {
  display: flex;
  gap: 8px;
  overflow-x: auto;
  scrollbar-width: none;
}
.chips::-webkit-scrollbar { display: none; }
.chip {
  display: inline-flex;
  align-items: center;
  gap: 8px;
  flex: none;
  min-height: 40px;
  padding: 4px 14px 4px 4px;
  border: 1px solid var(--border);
  border-radius: 999px;
  background: var(--surface);
  color: var(--ink);
  font: inherit;
  font-weight: 500;
  cursor: pointer;
}
.chip.active {
  background: var(--ink);
  color: var(--page);
  border-color: var(--ink);
}
.avatar {
  display: grid;
  place-items: center;
  width: 32px;
  height: 32px;
  border-radius: 50%;
  background: var(--accent-soft);
  color: var(--accent);
  font-weight: 700;
}
.chip.active .avatar { background: var(--accent); color: var(--accent-ink); }
.chip-add { padding: 0; width: 40px; justify-content: center; font-size: 1.4rem; font-weight: 400; }

/* Layout */
.main {
  max-width: 640px;
  margin: 0 auto;
  padding: 16px 16px calc(var(--tabs-h) + env(safe-area-inset-bottom, 0px) + 24px);
}
.view > * + * { margin-top: 16px; }
.view-head {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
}
.section-title {
  font-size: 0.8rem;
  font-weight: 600;
  text-transform: uppercase;
  letter-spacing: 0.06em;
  color: var(--muted);
  margin-top: 24px !important;
}
.back-link {
  display: inline-block;
  color: var(--muted);
  text-decoration: none;
  padding: 4px 0;
}

/* Tabs */
.tabs {
  position: fixed;
  left: 0;
  right: 0;
  bottom: 0;
  z-index: 10;
  display: flex;
  justify-content: center;
  background: var(--surface);
  border-top: 1px solid var(--border);
  padding-bottom: env(safe-area-inset-bottom, 0px);
}
.tab {
  flex: 1;
  max-width: 160px;
  height: var(--tabs-h);
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 2px;
  font-size: 0.75rem;
  color: var(--muted);
  text-decoration: none;
}
.tab-icon { font-size: 1.3rem; line-height: 1; filter: grayscale(1); opacity: 0.7; }
.tab.active { color: var(--ink); font-weight: 600; }
.tab.active .tab-icon { filter: none; opacity: 1; }

/* Buttons & inputs */
.btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 6px;
  min-height: 44px;
  padding: 0 16px;
  border: 1px solid var(--border);
  border-radius: 12px;
  background: var(--surface);
  color: var(--ink);
  font: inherit;
  font-weight: 600;
  text-decoration: none;
  cursor: pointer;
  white-space: nowrap;
}
.btn:disabled { opacity: 0.45; cursor: default; }
.btn-primary { background: var(--accent); border-color: var(--accent); color: var(--accent-ink); }
.btn-danger { background: var(--danger); border-color: var(--danger); color: #fff; }
.btn-ghost { background: transparent; border-color: transparent; color: var(--ink-2); }
.btn-danger-ghost { background: transparent; color: var(--danger); }
.btn-text-danger { background: transparent; border-color: transparent; color: var(--danger); font-weight: 500; }
.btn-icon { width: 44px; padding: 0; color: var(--ink-2); }
.danger-zone { margin-top: 32px !important; }
.btn-block { display: flex; width: 100%; }
.btn-row { display: flex; flex-wrap: wrap; gap: 8px; }
.btn-row > .btn { flex: 1; }
.icon-btn {
  display: inline-grid;
  place-items: center;
  width: 40px;
  height: 40px;
  border: 0;
  border-radius: 10px;
  background: transparent;
  color: var(--muted);
  font: inherit;
  font-size: 1.1rem;
  cursor: pointer;
}
.icon-btn:disabled { opacity: 0.3; }

.input {
  width: 100%;
  min-height: 44px;
  padding: 8px 12px;
  border: 1px solid var(--border);
  border-radius: 10px;
  background: var(--surface);
  color: var(--ink);
  font: inherit;
  font-size: 16px; /* prevents iOS zoom on focus */
}
.input:focus, .stepper-input:focus { outline: 2px solid var(--accent); outline-offset: -1px; }
.input-lg { font-size: 1.1rem; font-weight: 600; min-height: 52px; }
select.input { appearance: auto; }
.field { display: flex; flex-direction: column; gap: 4px; min-width: 0; }
.field-label { font-size: 0.75rem; color: var(--muted); font-weight: 500; }

/* Cards & lists */
.card {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 14px;
  box-shadow: var(--shadow);
}
.card-title { font-size: 1.05rem; }
/* The list sits inside a wrapper that gets the .view spacing, so it can reset all margins. */
.list { list-style: none; margin: 0; padding: 0; display: grid; gap: 10px; }
.routine-card { position: relative; transition: transform 0.18s ease, box-shadow 0.18s ease; }
.routine-card.has-handle { gap: 6px; padding-left: 2px; }
.routine-card.dragging { z-index: 2; transition: none; box-shadow: 0 10px 28px rgba(0, 0, 0, 0.28); }
.drag-handle {
  flex: none;
  display: grid;
  place-items: center;
  width: 32px;
  height: 48px;
  padding: 0;
  border: 0;
  border-radius: 8px;
  background: transparent;
  color: var(--muted);
  cursor: grab;
  touch-action: none;
  -webkit-user-select: none;
  user-select: none;
}
.drag-handle:active, .routine-card.dragging .drag-handle { cursor: grabbing; color: var(--ink); }
.btn-primary.btn-icon { color: var(--accent-ink); }
.shared-exercises { margin: 0; padding-left: 1.4em; color: var(--ink-2); }
.shared-exercises li + li { margin-top: 4px; }
.routine-card { display: flex; align-items: center; gap: 12px; }
/* Long press deletes, so suppress the OS link preview / share menu and text selection. */
.routine-info { flex: 1; min-width: 0; text-decoration: none; -webkit-touch-callout: none; -webkit-user-select: none; user-select: none; }
.head-actions { display: flex; gap: 8px; }
.check-row { display: flex; align-items: center; gap: 10px; cursor: pointer; }
.checkbox { flex: none; width: 22px; height: 22px; margin: 0; accent-color: var(--accent); }
.routine-actions { display: flex; flex: none; align-items: center; gap: 8px; }
.routine-info p { margin-top: 4px; }
.empty {
  text-align: center;
  padding: 32px 16px;
  border: 1px dashed var(--axis);
  border-radius: var(--radius);
}
.empty > * + * { margin-top: 8px; }
.empty .btn { margin-top: 16px; }
.banner {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  padding: 12px 14px;
  border-radius: var(--radius);
  background: var(--accent-soft);
  text-decoration: none;
}

/* Routine editor */
.ex-edit-list { list-style: none; margin-inline: 0; margin-bottom: 0; padding: 0; display: grid; gap: 10px; }
.ex-edit { display: flex; align-items: center; gap: 6px; padding: 8px 6px 8px 12px; }
.ex-edit-num { flex: none; width: 18px; color: var(--muted); font-weight: 600; font-variant-numeric: tabular-nums; }
.row-actions { display: flex; flex: none; }

/* Active workout */
.workout-head > * + * { margin-top: 6px; }
.workout-meta { display: flex; flex-wrap: wrap; align-items: end; gap: 12px; }
.workout-meta .field { width: 170px; }
.ex-list { display: grid; gap: 12px; }
.ex-card { padding: 14px 10px 10px; }
.ex-card-head { display: flex; align-items: flex-start; justify-content: space-between; gap: 8px; padding-left: 4px; }
.ex-card-head p { margin-top: 2px; }
.set-head, .set-row {
  display: grid;
  grid-template-columns: 24px minmax(0, 1fr) minmax(0, 1.3fr) 46px;
  gap: 6px;
  align-items: center;
}
.ex-card.finished .set-head, .ex-card.finished .set-row { grid-template-columns: 24px minmax(0, 1fr) minmax(0, 1.3fr); }
.set-head { margin: 12px 0 4px; font-size: 0.75rem; color: var(--muted); font-weight: 500; text-align: center; }
.set-rows { display: grid; gap: 6px; }
.set-num { text-align: center; font-weight: 600; color: var(--muted); font-variant-numeric: tabular-nums; }
.set-row.done .set-num { color: var(--good); }
.set-row.done .stepper-row { background: var(--good-soft); border-color: color-mix(in srgb, var(--good) 35%, transparent); }
.ex-title { min-width: 0; }
.ex-name-row { display: flex; flex-wrap: wrap; align-items: center; gap: 8px; }
.unit-toggle { padding: 2px; border-radius: 9px; }
.unit-toggle button { flex: none; min-height: 28px; padding: 0 10px; border-radius: 7px; font-size: 0.8rem; }
/* Weight stepper (2nd stepper in the row) is outlined when a logged set still has 0 weight. */
.set-row.invalid > .stepper-row:nth-of-type(2) {
  border-color: var(--danger);
  background: color-mix(in srgb, var(--danger) 12%, transparent);
}
.set-check {
  width: 46px;
  height: 48px;
  border: 1px solid var(--border);
  border-radius: 10px;
  background: transparent;
  color: var(--muted);
  font: inherit;
  font-size: 1.2rem;
  font-weight: 700;
  cursor: pointer;
  touch-action: manipulation;
}
.set-check[aria-pressed="true"] { background: var(--good); border-color: var(--good); color: #fff; }
.set-actions { display: flex; gap: 8px; margin-top: 10px; }
.set-actions .btn { flex: 1; min-height: 40px; font-size: 0.9rem; }
.stepper-row {
  display: flex;
  align-items: stretch;
  border: 1px solid var(--border);
  border-radius: 10px;
  background: var(--page);
  overflow: hidden;
}
.step-btn {
  flex: none;
  width: 36px;
  min-height: 48px;
  border: 0;
  background: transparent;
  color: var(--ink);
  font: inherit;
  font-size: 1.3rem;
  cursor: pointer;
  touch-action: manipulation;
}
.step-btn:active { background: var(--surface-2); }
.stepper-input {
  flex: 1;
  min-width: 0;
  width: 100%;
  border: 0;
  background: transparent;
  color: var(--ink);
  text-align: center;
  font: inherit;
  font-size: 1.15rem;
  font-weight: 600;
  font-variant-numeric: tabular-nums;
  -moz-appearance: textfield;
}
.stepper-input::-webkit-outer-spin-button,
.stepper-input::-webkit-inner-spin-button { -webkit-appearance: none; margin: 0; }
.footer-actions {
  position: sticky;
  bottom: calc(var(--tabs-h) + env(safe-area-inset-bottom, 0px) + 8px);
  display: flex;
  gap: 8px;
  margin-inline: -16px;
  padding: 10px 16px;
  background: var(--page);
  border-top: 1px solid var(--border);
}
.footer-actions .btn-primary { flex: 1; }

/* History */
.history > * + * { margin-top: 10px; }
.history-item { display: flex; align-items: center; gap: 14px; text-decoration: none; }
.history-date {
  flex: none;
  display: flex;
  flex-direction: column;
  align-items: center;
  width: 48px;
  padding: 6px 0;
  border-radius: 10px;
  background: var(--surface-2);
}
.history-date .dow { font-size: 0.7rem; text-transform: uppercase; color: var(--muted); font-weight: 600; }
.history-date .day { font-size: 1.25rem; font-weight: 700; font-variant-numeric: tabular-nums; }
.history-body { flex: 1; min-width: 0; }
.history-body p { margin-top: 2px; }
.chev { color: var(--muted); font-size: 1.5rem; }

/* Segmented control */
.segmented {
  display: flex;
  padding: 3px;
  border-radius: 12px;
  background: var(--surface-2);
}
.segmented button {
  flex: 1;
  min-height: 38px;
  padding: 0 10px;
  border: 0;
  border-radius: 9px;
  background: transparent;
  color: var(--ink-2);
  font: inherit;
  font-weight: 500;
  cursor: pointer;
}
.segmented button.active { background: var(--surface); color: var(--ink); font-weight: 600; box-shadow: 0 1px 3px rgba(0, 0, 0, 0.12); }

/* Progress */
.stats { display: grid; grid-template-columns: repeat(3, 1fr); gap: 8px; }
.stat { padding: 10px 12px; }
.stat-label { font-size: 0.75rem; color: var(--muted); font-weight: 500; }
.stat-value { font-size: 1.15rem; font-weight: 650; margin-top: 2px; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.chart-card > p { margin-top: 2px; }
.chart { position: relative; min-height: 220px; margin-top: 12px; }
.chart-svg { display: block; overflow: visible; touch-action: pan-y; outline: none; }
.chart-svg:focus-visible { outline: 2px solid var(--accent); outline-offset: 4px; border-radius: 4px; }
.chart-grid { stroke: var(--grid); stroke-width: 1; }
.chart-axis { stroke: var(--axis); stroke-width: 1; }
.chart-tick { fill: var(--muted); font-size: 11px; font-variant-numeric: tabular-nums; }
.chart-line { fill: none; stroke: var(--series); stroke-width: 2; stroke-linejoin: round; stroke-linecap: round; }
.chart-dot { fill: var(--series); stroke: var(--surface); stroke-width: 2; }
.chart-cross { stroke: var(--axis); stroke-width: 1; }
.chart-hit { fill: transparent; cursor: crosshair; }
.chart-endlabel { fill: var(--ink); font-size: 12px; font-weight: 600; }
.chart-tip {
  position: absolute;
  z-index: 2;
  display: flex;
  flex-direction: column;
  gap: 1px;
  max-width: 220px;
  padding: 8px 10px;
  border: 1px solid var(--border);
  border-radius: 10px;
  background: var(--surface);
  box-shadow: 0 4px 16px rgba(0, 0, 0, 0.14);
  font-size: 0.8rem;
  color: var(--ink-2);
  pointer-events: none;
}
.chart-tip strong { font-size: 1rem; color: var(--ink); }
.table-wrap { overflow-x: auto; padding: 0; }
.table { width: 100%; border-collapse: collapse; font-size: 0.9rem; }
.table th, .table td { padding: 10px 14px; text-align: left; border-bottom: 1px solid var(--grid); }
.table tr:last-child td { border-bottom: 0; }
.table th { font-size: 0.75rem; color: var(--muted); font-weight: 600; }
.table .num { text-align: right; font-variant-numeric: tabular-nums; white-space: nowrap; }
.table a { text-decoration: none; }

/* Settings */
.setting-row { display: flex; align-items: center; justify-content: space-between; gap: 12px; }
.setting-row .segmented { width: 140px; }
.setting-label { font-weight: 600; }

/* Onboarding */
.onboarding { padding-top: 12vh; text-align: center; }
.onboarding form { text-align: left; margin-top: 24px; }

/* Dialog */
.dialog {
  width: min(92vw, 420px);
  padding: 20px;
  border: 1px solid var(--border);
  border-radius: 18px;
  background: var(--surface);
  color: var(--ink);
}
.dialog::backdrop { background: rgba(0, 0, 0, 0.45); }
.dialog form > * + * { margin-top: 12px; }
.dialog h2 { font-size: 1.2rem; }
.dialog-actions { display: flex; flex-direction: row-reverse; flex-wrap: wrap; gap: 8px; margin-top: 20px !important; }
.dialog-actions .btn { flex: 1 1 auto; }

.toast {
  position: fixed;
  left: 50%;
  bottom: calc(var(--tabs-h) + env(safe-area-inset-bottom, 0px) + 80px);
  transform: translateX(-50%);
  z-index: 100;
  max-width: calc(100vw - 32px);
  padding: 10px 16px;
  border-radius: 999px;
  background: var(--ink);
  color: var(--page);
  font-weight: 500;
  animation: toast-in 0.2s ease-out;
}
@keyframes toast-in { from { opacity: 0; transform: translate(-50%, 8px); } }
