:root {
  color-scheme: light;
  --page: #f4f4f1;
  --surface: #fcfcfb;
  --surface-2: #eeede8;
  --border: rgba(11, 11, 11, 0.1);
  --text: #0b0b0b;
  --text-2: #52514e;
  --muted: #7a7873;
  --grid: #e1e0d9;
  --axis: #c3c2b7;
  --accent: #2a78d6;
  --accent-wash: rgba(42, 120, 214, 0.12);
  --good: #0ca30c;
  --good-text: #006300;
  --good-wash: rgba(12, 163, 12, 0.12);
  --danger: #d03b3b;
  --radius: 14px;
  --radius-sm: 10px;
  --tap: 44px;
}

@media (prefers-color-scheme: dark) {
  :root {
    color-scheme: dark;
    --page: #0d0d0d;
    --surface: #1a1a19;
    --surface-2: #262624;
    --border: rgba(255, 255, 255, 0.1);
    --text: #ffffff;
    --text-2: #c3c2b7;
    --muted: #898781;
    --grid: #2c2c2a;
    --axis: #4a4a46;
    --accent: #3987e5;
    --accent-wash: rgba(57, 135, 229, 0.18);
    --good-text: #0ca30c;
    --good-wash: rgba(12, 163, 12, 0.18);
    --danger: #e66767;
  }
}

* { box-sizing: border-box; }
html { -webkit-text-size-adjust: 100%; }
body {
  margin: 0;
  background: var(--page);
  color: var(--text);
  font: 16px/1.45 system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
  -webkit-tap-highlight-color: transparent;
}
button, input, select { font: inherit; color: inherit; }
button { cursor: pointer; }
[hidden] { display: none !important; }
p { margin: 0; }
h1, h2, h3 { margin: 0; line-height: 1.25; }
code { font-size: 0.9em; background: var(--surface-2); padding: 1px 5px; border-radius: 5px; }

.muted { color: var(--muted); }
.small { font-size: 13px; }
.center { text-align: center; }

.icon {
  width: 20px; height: 20px; display: block;
  stroke: currentColor; fill: none; stroke-width: 2;
  stroke-linecap: round; stroke-linejoin: round;
}
.icon-wrap { display: inline-flex; }

/* Top bar */
.topbar {
  position: sticky; top: 0; z-index: 10;
  display: flex; align-items: center; justify-content: space-between; gap: 12px;
  padding: calc(10px + env(safe-area-inset-top)) 16px 10px;
  background: color-mix(in srgb, var(--page) 88%, transparent);
  -webkit-backdrop-filter: blur(12px);
  backdrop-filter: blur(12px);
  border-bottom: 1px solid var(--border);
}
.topbar h1 { font-size: 20px; }
.topbar-right { display: flex; gap: 10px; align-items: center; font-size: 13px; }
.timer { font-weight: 600; font-variant-numeric: tabular-nums; }
.sync { color: var(--muted); }

.main {
  max-width: 640px; margin: 0 auto;
  padding: 12px 16px calc(96px + env(safe-area-inset-bottom));
}
.view { display: grid; gap: 12px; }
.section-title { font-size: 16px; margin: 8px 2px 0; }

.card {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 14px;
  min-width: 0;
}
.card h2 { font-size: 16px; margin-bottom: 10px; }
.card-top { display: flex; justify-content: space-between; align-items: flex-start; gap: 8px; }
.card-top h2 { margin-bottom: 2px; }

/* Buttons */
.btn {
  display: inline-flex; align-items: center; justify-content: center; gap: 8px;
  min-height: var(--tap); padding: 0 16px;
  border-radius: var(--radius-sm); border: 1px solid transparent;
  background: var(--surface-2); font-weight: 600;
}
.btn.primary { background: var(--accent); color: #fff; }
.btn.big { width: 100%; min-height: 56px; font-size: 18px; }
.btn.ghost { background: transparent; border-color: var(--border); }
.btn.small { min-height: 36px; padding: 0 12px; font-size: 14px; }
.btn.danger { color: var(--danger); }
.btn.armed, .icon-btn.armed { background: var(--danger); color: #fff; border-color: transparent; }
.icon-btn {
  min-width: 40px; height: 40px; flex: none;
  display: inline-grid; place-items: center;
  border: 0; border-radius: var(--radius-sm);
  background: transparent; color: var(--muted);
}
.icon-btn.armed { padding: 0 10px; font-size: 13px; font-weight: 600; }
.linkish { background: none; border: 0; color: var(--accent); padding: 8px 0; }

.chips { display: flex; flex-wrap: wrap; align-items: center; gap: 8px; margin-top: 12px; }
.chip {
  display: inline-flex; align-items: center; gap: 6px;
  min-height: 38px; padding: 0 12px;
  border-radius: 999px; border: 1px solid var(--border);
  background: var(--surface-2); font-size: 14px;
}
.chip .icon { width: 16px; height: 16px; }

.advice {
  padding: 12px 14px; border-radius: var(--radius);
  background: var(--accent-wash); font-size: 15px;
}

/* Week strip */
.week { display: grid; grid-template-columns: repeat(7, 1fr); gap: 4px; margin-bottom: 10px; }
.week-day { display: grid; justify-items: center; gap: 6px; font-size: 12px; color: var(--muted); }
.week-dot {
  width: 30px; height: 30px; border-radius: 50%;
  border: 2px solid var(--grid);
  display: grid; place-items: center;
}
.week-dot .icon { width: 16px; height: 16px; }
.week-day.trained .week-dot { background: var(--accent); border-color: var(--accent); color: #fff; }
.week-day.today { color: var(--text); font-weight: 600; }

/* Health tiles */
.tiles { display: grid; grid-template-columns: 1fr 1fr; gap: 10px; }
.tile {
  background: var(--surface); border: 1px solid var(--border);
  border-radius: var(--radius); padding: 12px; min-width: 0;
}
.tile-label { font-size: 13px; color: var(--text-2); }
.tile-value { font-size: 22px; font-weight: 600; margin-top: 2px; }
.tile.empty .tile-value { font-size: 15px; font-weight: 500; color: var(--muted); }
.tile-sub { font-size: 12px; color: var(--muted); margin-top: 2px; }
.spark { display: block; width: 100%; height: 32px; margin-top: 8px; }
.spark path {
  fill: none; stroke-width: 2; vector-effect: non-scaling-stroke;
  stroke-linecap: round; stroke-linejoin: round;
}
.spark-line { stroke: var(--axis); }
.spark-now { stroke: var(--accent); }

/* Workout */
.wk-name {
  width: 100%; padding: 6px 0;
  border: 0; border-bottom: 1px solid var(--border); border-radius: 0;
  background: transparent; font-size: 18px; font-weight: 600;
}
.wk-name:focus { outline: none; border-bottom-color: var(--accent); }
.card.ex { scroll-margin-top: 72px; }
.ex-head { display: flex; justify-content: space-between; align-items: flex-start; gap: 8px; }
.ex-head h3 { font-size: 17px; }
.hint { font-size: 14px; margin-top: 8px; color: var(--text-2); font-variant-numeric: tabular-nums; }
.hint.target { color: var(--text); font-weight: 500; }
.hint.target.up::before { content: "\2191  "; color: var(--good-text); font-weight: 700; }

.sets { display: grid; gap: 6px; margin: 12px 0 10px; }
.set-row {
  display: grid; align-items: center; gap: 6px;
  grid-template-columns: 22px minmax(0, 1fr) minmax(0, 1fr) 44px;
}
.set-head { font-size: 12px; color: var(--muted); }
.set-head span { text-align: center; }
.set-no { font-size: 14px; color: var(--muted); text-align: center; }
.stepper {
  display: flex; align-items: center; min-width: 0;
  background: var(--surface-2); border-radius: var(--radius-sm);
}
.stepper input {
  flex: 1; width: 100%; min-width: 0; padding: 10px 0;
  border: 0; background: transparent; text-align: center;
  font-size: 17px; font-weight: 600; font-variant-numeric: tabular-nums;
}
.stepper input:focus { outline: 2px solid var(--accent); outline-offset: -2px; border-radius: 8px; }
.step {
  width: 34px; height: 44px; flex: none;
  display: grid; place-items: center;
  border: 0; background: transparent; color: var(--text-2);
}
.step .icon { width: 16px; height: 16px; }
.done-btn {
  width: 44px; height: 44px;
  display: grid; place-items: center;
  border: 2px solid var(--grid); border-radius: var(--radius-sm);
  background: transparent; color: var(--muted);
}
.set-row.done .done-btn { background: var(--good); border-color: var(--good); color: #fff; }
.set-row.done .stepper { background: var(--good-wash); }
.wk-actions { display: grid; grid-template-columns: 1fr auto; gap: 8px; }
.add-ex { width: 100%; }

/* History */
.hist-week { font-size: 13px; font-weight: 600; color: var(--muted); margin: 6px 2px -4px; }
.hist-meta { font-size: 13px; color: var(--muted); margin-top: 2px; }
.hist-list { list-style: none; margin: 10px 0 12px; padding: 0; display: grid; gap: 6px; font-size: 14px; }
.hist-list li { display: grid; }
.hist-sets { color: var(--text-2); font-variant-numeric: tabular-nums; }
.badge {
  display: inline-block; margin-left: 6px; padding: 2px 8px; vertical-align: middle;
  border-radius: 999px; background: var(--surface-2); color: var(--text-2);
  font-size: 11px; font-weight: 600;
}
.row-actions { display: flex; flex-wrap: wrap; gap: 8px; }

/* Progress */
select {
  width: 100%; min-height: var(--tap); padding: 0 12px;
  border: 1px solid var(--border); border-radius: var(--radius-sm);
  background: var(--surface-2); font-size: 16px;
}
.stats { display: grid; grid-template-columns: repeat(3, minmax(0, 1fr)); gap: 8px; margin: 12px 0 4px; }
.stat .v { font-size: 19px; font-weight: 600; }
.stat .l { font-size: 12px; color: var(--muted); }
.note { font-size: 13px; color: var(--muted); margin-top: 8px; }

.vol { display: grid; gap: 8px; }
.vol-row {
  display: grid; align-items: center; gap: 8px; font-size: 14px;
  grid-template-columns: 6.5em minmax(0, 1fr) 2.2em;
}
.vol-label { color: var(--text-2); white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.vol-track { position: relative; height: 16px; }
.vol-zone { position: absolute; top: 0; bottom: 0; background: var(--accent-wash); border-radius: 3px; }
.vol-fill { position: absolute; left: 0; top: 3px; bottom: 3px; background: var(--accent); border-radius: 0 4px 4px 0; }
.vol-value { text-align: right; color: var(--text-2); font-variant-numeric: tabular-nums; }

.chart { position: relative; margin-top: 10px; touch-action: pan-y; user-select: none; -webkit-user-select: none; }
.chart svg { display: block; width: 100%; height: auto; overflow: visible; }
.chart .grid { stroke: var(--grid); stroke-width: 1; }
.chart .baseline { stroke: var(--axis); stroke-width: 1; }
.chart .ref { stroke: var(--text-2); stroke-width: 1.5; stroke-opacity: 0.8; }
.chart .tick { fill: var(--muted); font-size: 11px; font-variant-numeric: tabular-nums; }
.chart .line { fill: none; stroke: var(--accent); stroke-width: 2; stroke-linecap: round; stroke-linejoin: round; }
.chart .dot { fill: var(--accent); stroke: var(--surface); stroke-width: 2; }
.chart .end-label { fill: var(--text); font-size: 12px; font-weight: 600; }
.chart .cross { stroke: var(--axis); stroke-width: 1; }
.chart .bar { fill: var(--accent); }
.chart .bar.hot { fill-opacity: 0.7; }
.tip {
  position: absolute; top: -6px; pointer-events: none;
  display: grid; white-space: nowrap;
  padding: 6px 8px; border-radius: 8px;
  background: var(--surface); border: 1px solid var(--border);
  box-shadow: 0 4px 14px rgba(0, 0, 0, 0.14); font-size: 12px;
}
.tip strong { font-size: 14px; }
.tip span { color: var(--text-2); }

details.table { margin-top: 8px; font-size: 14px; }
details.table summary { color: var(--accent); padding: 6px 0; cursor: pointer; }
table { width: 100%; border-collapse: collapse; font-variant-numeric: tabular-nums; }
th, td { padding: 6px 4px; border-bottom: 1px solid var(--grid); text-align: left; }
th { font-size: 12px; font-weight: 600; color: var(--muted); }
.num { text-align: right; }

/* Tab bar */
.tabbar {
  position: fixed; left: 0; right: 0; bottom: 0; z-index: 20;
  display: grid; grid-template-columns: repeat(4, 1fr);
  padding-bottom: env(safe-area-inset-bottom);
  background: color-mix(in srgb, var(--surface) 92%, transparent);
  -webkit-backdrop-filter: blur(12px);
  backdrop-filter: blur(12px);
  border-top: 1px solid var(--border);
}
.tab {
  display: grid; justify-items: center; gap: 2px; padding: 8px 0 6px;
  border: 0; background: none; color: var(--muted); font-size: 11px;
}
.tab .icon { width: 24px; height: 24px; }
.tab.active { color: var(--accent); }

/* Exercise picker */
dialog#picker {
  width: 100%; max-width: 560px; height: min(90dvh, 780px);
  margin: auto auto 0; padding: 0; border: 0;
  border-radius: 18px 18px 0 0;
  background: var(--surface); color: var(--text);
}
@media (min-width: 600px) {
  dialog#picker { margin: auto; border-radius: 18px; }
}
dialog::backdrop { background: rgba(0, 0, 0, 0.45); }
.sheet { display: flex; flex-direction: column; height: 100%; }
.sheet-head { display: flex; justify-content: space-between; align-items: center; padding: 12px 10px 4px 16px; }
.sheet-head h2 { font-size: 18px; }
.search { padding: 4px 16px 8px; }
.search input, .login input, .custom-ex input {
  width: 100%; min-height: var(--tap); padding: 0 12px;
  border: 1px solid var(--border); border-radius: var(--radius-sm);
  background: var(--surface-2); font-size: 16px;
}
.picker-list { flex: 1; overflow-y: auto; padding: 0 8px 12px; overscroll-behavior: contain; }
.pick-group {
  padding: 12px 8px 4px; font-size: 12px; font-weight: 600; color: var(--muted);
  text-transform: uppercase; letter-spacing: 0.04em;
}
.pick {
  display: flex; justify-content: space-between; align-items: center; gap: 8px;
  width: 100%; min-height: 48px; padding: 8px;
  border: 0; border-radius: var(--radius-sm); background: none; text-align: left;
}
.pick:active { background: var(--surface-2); }
.pick small { color: var(--muted); flex: none; }
.custom-ex { border-top: 1px solid var(--border); padding: 4px 16px calc(12px + env(safe-area-inset-bottom)); }
.custom-ex summary { padding: 10px 0; color: var(--accent); font-weight: 600; cursor: pointer; }
.custom-ex form { display: grid; grid-template-columns: 1fr 1fr; gap: 10px; }
.custom-ex .full { grid-column: 1 / -1; }
label { display: grid; gap: 4px; font-size: 13px; color: var(--text-2); }

/* Login */
.login { min-height: 100dvh; display: grid; place-items: center; padding: 24px 16px; }
.login-card { width: 100%; max-width: 360px; display: grid; gap: 14px; text-align: center; }
.login-card form { display: grid; gap: 12px; text-align: left; }
.login-logo { margin: 0 auto; border-radius: 16px; }
.error { min-height: 1.3em; color: var(--danger); font-size: 14px; }

.toast {
  position: fixed; left: 50%; z-index: 50; transform: translateX(-50%);
  bottom: calc(84px + env(safe-area-inset-bottom));
  max-width: calc(100% - 32px); padding: 10px 16px;
  border-radius: 999px; background: var(--text); color: var(--page); font-size: 14px;
}
.records { display: grid; gap: 4px; margin-top: 8px; font-size: 14px; }

/* Health Auto Export card */
.btn:disabled { opacity: 0.5; cursor: default; }
.copy-row {
  display: flex; align-items: center; justify-content: space-between; gap: 10px;
  margin-top: 10px; padding-top: 10px; border-top: 1px solid var(--grid);
}
.copy-row > div { min-width: 0; }
.copy-row code { display: block; margin-top: 2px; font-size: 13px; overflow-wrap: anywhere; }
.copy-row .btn { flex: none; }
.copy-fallback {
  width: 100%; min-height: 40px; margin-top: 8px; padding: 0 10px;
  border: 1px solid var(--border); border-radius: var(--radius-sm);
  background: var(--surface-2); font-size: 14px;
}
.card > .small.muted + .copy-row { margin-top: 12px; }
.card > .copy-row + .small.muted { margin-top: 10px; }
