/* ============================================================
   Workout Tracker — design system
   ============================================================ */
*, *::before, *::after { box-sizing: border-box; }
* { margin: 0; }

:root {
  --accent: #6366f1;
  --accent-strong: #4f46e5;
  --accent-soft: rgba(99, 102, 241, 0.14);
  --success: #22c55e;
  --warn: #f59e0b;
  --danger: #ef4444;
  --rpe: #f97316;

  --radius-sm: 8px;
  --radius: 14px;
  --radius-lg: 20px;
  --maxw: 640px;
  --nav-h: 64px;
  --shadow: 0 1px 3px rgba(0,0,0,.08), 0 6px 24px rgba(0,0,0,.06);
  --shadow-lg: 0 12px 40px rgba(0,0,0,.18);
  --font: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Helvetica, Arial, sans-serif;
  --safe-bottom: env(safe-area-inset-bottom, 0px);
  --safe-top: env(safe-area-inset-top, 0px);
}

[data-theme="light"] {
  --bg: #f5f6f8;
  --bg-elev: #ffffff;
  --surface: #ffffff;
  --surface-2: #f0f1f4;
  --border: #e4e6eb;
  --text: #1a1d23;
  --text-2: #5b6472;
  --text-3: #8a93a3;
  --scrim: rgba(20, 24, 30, .45);
}
[data-theme="dark"] {
  --bg: #0d1117;
  --bg-elev: #161b22;
  --surface: #161b22;
  --surface-2: #21262d;
  --border: #2a313c;
  --text: #e6edf3;
  --text-2: #9aa6b2;
  --text-3: #6b7682;
  --scrim: rgba(0, 0, 0, .6);
  --shadow: 0 1px 3px rgba(0,0,0,.4), 0 6px 24px rgba(0,0,0,.3);
}

html { -webkit-text-size-adjust: 100%; }
body {
  font-family: var(--font);
  background: var(--bg);
  color: var(--text);
  line-height: 1.5;
  overscroll-behavior-y: none;
  -webkit-tap-highlight-color: transparent;
}
h1, h2, h3, h4 { line-height: 1.25; font-weight: 700; }
a { color: var(--accent); text-decoration: none; }
button { font-family: inherit; cursor: pointer; }
input, select, textarea { font-family: inherit; font-size: 16px; }
::-webkit-scrollbar { width: 0; height: 0; }

/* ── App shell ─────────────────────────────────────────────── */
#app { min-height: 100vh; }
.screen {
  max-width: var(--maxw);
  margin: 0 auto;
  padding: calc(var(--safe-top) + 16px) 16px calc(var(--nav-h) + 90px);
}
.topbar {
  position: sticky;
  top: 0;
  z-index: 20;
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  padding: calc(var(--safe-top) + 12px) 16px 12px;
  max-width: var(--maxw);
  margin: 0 auto;
  background: linear-gradient(var(--bg) 70%, transparent);
}
.topbar h1 { font-size: 1.5rem; letter-spacing: -.02em; }
.topbar .sub { color: var(--text-2); font-size: .85rem; font-weight: 500; }
.topbar-actions { display: flex; gap: 8px; align-items: center; }

.back-link {
  display: inline-flex; align-items: center; gap: 4px;
  color: var(--text-2); font-weight: 600; font-size: .95rem; margin-bottom: 4px;
  background: none; border: none; padding: 4px 0;
}

/* ── Bottom navigation ─────────────────────────────────────── */
.bottom-nav {
  position: fixed; left: 0; right: 0; bottom: 0; z-index: 30;
  height: calc(var(--nav-h) + var(--safe-bottom));
  padding-bottom: var(--safe-bottom);
  display: flex; align-items: stretch;
  background: var(--bg-elev);
  border-top: 1px solid var(--border);
  box-shadow: 0 -4px 20px rgba(0,0,0,.06);
}
.nav-btn {
  flex: 1; display: flex; flex-direction: column; align-items: center; justify-content: center;
  gap: 3px; background: none; border: none; color: var(--text-3);
  font-size: .68rem; font-weight: 600; letter-spacing: .01em; transition: color .15s;
}
.nav-btn.active { color: var(--accent); }
.nav-btn .icon { transition: transform .15s; }
.nav-btn.active .icon { transform: translateY(-1px); }
.nav-start { position: relative; }
.nav-start .start-fab {
  width: 52px; height: 52px; margin-top: -22px; border-radius: 50%;
  background: var(--accent); color: #fff; border: 4px solid var(--bg-elev);
  display: flex; align-items: center; justify-content: center;
  box-shadow: 0 6px 18px var(--accent-soft);
}
.nav-start.active .start-fab { background: var(--accent-strong); }

/* ── Active session bar ────────────────────────────────────── */
.active-bar {
  position: fixed; left: 50%; transform: translateX(-50%);
  bottom: calc(var(--nav-h) + var(--safe-bottom) + 10px);
  width: min(var(--maxw), calc(100% - 24px)); z-index: 29;
  display: flex; align-items: center; gap: 12px;
  padding: 10px 14px; border-radius: var(--radius);
  background: var(--accent); color: #fff;
  box-shadow: var(--shadow-lg);
  animation: slideUp .25s ease;
}
.active-bar .pulse { width: 10px; height: 10px; border-radius: 50%; background: #fff; animation: pulse 1.4s infinite; }
.active-bar .ab-name { font-weight: 700; flex: 1; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.active-bar .ab-timer { font-variant-numeric: tabular-nums; font-weight: 700; }
@keyframes pulse { 0%,100% { opacity: 1; } 50% { opacity: .35; } }
@keyframes slideUp { from { transform: translate(-50%, 20px); opacity: 0; } to { transform: translate(-50%, 0); opacity: 1; } }

/* ── Cards ─────────────────────────────────────────────────── */
.card {
  background: var(--surface); border: 1px solid var(--border);
  border-radius: var(--radius); padding: 16px; box-shadow: var(--shadow);
}
.card + .card { margin-top: 14px; }
.card-tap { transition: transform .12s, border-color .12s; }
.card-tap:active { transform: scale(.99); }
.section-title { font-size: .8rem; text-transform: uppercase; letter-spacing: .06em; color: var(--text-3); margin: 22px 4px 10px; font-weight: 700; }

/* ── Buttons ───────────────────────────────────────────────── */
.btn {
  display: inline-flex; align-items: center; justify-content: center; gap: 8px;
  border: none; border-radius: var(--radius-sm); padding: 12px 18px;
  font-size: .98rem; font-weight: 700; transition: transform .1s, background .15s, opacity .15s;
}
.btn:active { transform: scale(.97); }
.btn:disabled { opacity: .45; pointer-events: none; }
.btn-primary { background: var(--accent); color: #fff; }
.btn-primary:hover { background: var(--accent-strong); }
.btn-ghost { background: var(--surface-2); color: var(--text); }
.btn-danger { background: var(--danger); color: #fff; }
.btn-success { background: var(--success); color: #fff; }
.btn-block { display: flex; width: 100%; }
.btn-lg { padding: 15px 22px; font-size: 1.05rem; }
.btn-row { display: flex; gap: 10px; }
.btn-row > .btn { flex: 1; }

.icon-btn {
  display: inline-flex; align-items: center; justify-content: center;
  width: 40px; height: 40px; border-radius: 50%; border: none;
  background: var(--surface-2); color: var(--text); transition: background .15s, transform .1s;
}
.icon-btn:active { transform: scale(.92); }
.icon-btn.ghost { background: transparent; color: var(--text-2); }
.icon-btn.danger { color: var(--danger); }

/* ── Forms ─────────────────────────────────────────────────── */
.field { display: block; margin-bottom: 14px; }
.field > label { display: block; font-size: .85rem; font-weight: 600; color: var(--text-2); margin-bottom: 6px; }
.input, .select, .textarea {
  width: 100%; padding: 12px 14px; border-radius: var(--radius-sm);
  border: 1px solid var(--border); background: var(--bg-elev); color: var(--text);
  transition: border-color .15s, box-shadow .15s;
}
.input:focus, .select:focus, .textarea:focus {
  outline: none; border-color: var(--accent); box-shadow: 0 0 0 3px var(--accent-soft);
}
.textarea { resize: vertical; min-height: 70px; }

/* ── Chips / badges ────────────────────────────────────────── */
.chip {
  display: inline-flex; align-items: center; gap: 5px;
  padding: 5px 11px; border-radius: 999px; font-size: .78rem; font-weight: 600;
  background: var(--surface-2); color: var(--text-2); border: 1px solid transparent;
}
.chip.on { background: var(--accent-soft); color: var(--accent); border-color: transparent; }
.chip-row { display: flex; flex-wrap: wrap; gap: 8px; }
.badge { display: inline-flex; align-items: center; gap: 4px; padding: 3px 9px; border-radius: 999px; font-size: .72rem; font-weight: 700; }
.badge-pr { background: rgba(245,158,11,.16); color: var(--warn); }
.badge-muscle { background: var(--accent-soft); color: var(--accent); }
.badge-muscle.secondary { background: var(--surface-2); color: var(--text-2); }

/* ── Loading / empty ───────────────────────────────────────── */
.loading { display: flex; flex-direction: column; align-items: center; gap: 12px; padding: 50px 0; color: var(--text-2); }
.spinner { width: 30px; height: 30px; border: 3px solid var(--surface-2); border-top-color: var(--accent); border-radius: 50%; animation: spin .7s linear infinite; }
@keyframes spin { to { transform: rotate(360deg); } }
.empty-state { text-align: center; padding: 40px 20px; color: var(--text-2); }
.empty-state .empty-icon { color: var(--text-3); margin-bottom: 8px; }
.empty-state h3 { color: var(--text); margin-bottom: 6px; }
.empty-state p { margin-bottom: 16px; }

/* ── Stats grid ────────────────────────────────────────────── */
.stat-grid { display: grid; grid-template-columns: repeat(2, 1fr); gap: 12px; }
.stat {
  background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius);
  padding: 14px; box-shadow: var(--shadow);
}
.stat .num { font-size: 1.7rem; font-weight: 800; letter-spacing: -.02em; display: flex; align-items: baseline; gap: 5px; }
.stat .num small { font-size: .8rem; font-weight: 600; color: var(--text-3); }
.stat .lbl { font-size: .78rem; color: var(--text-2); font-weight: 600; margin-top: 2px; }
.stat .accent { color: var(--accent); }
.stat .flame { color: var(--rpe); }

/* ── Muscle map ────────────────────────────────────────────── */
.muscle-map { display: flex; justify-content: center; gap: 6px; }
.muscle-map-side { flex: 1; max-width: 50%; }
.muscle-map svg { width: 100%; height: auto; }
.muscle-map svg path, .muscle-map svg polygon { fill: var(--surface-2); stroke: var(--border); stroke-width: .5; transition: fill .2s; }
.muscle-map .mm-secondary { fill: var(--accent); opacity: .45; }
.muscle-map .mm-primary { fill: var(--accent); opacity: 1; }
.muscle-map .mm-selected { fill: var(--accent) !important; opacity: 1; }
.muscle-map .mm-recovery-0 { fill: var(--danger); opacity: .85; }
.muscle-map .mm-recovery-1 { fill: var(--warn); opacity: .8; }
.muscle-map .mm-recovery-2 { fill: #eab308; opacity: .6; }
.muscle-map .mm-recovery-3 { fill: var(--success); opacity: .7; }
.muscle-map.mini { max-width: 120px; }
.recovery-legend { display: flex; gap: 12px; justify-content: center; flex-wrap: wrap; margin-top: 10px; font-size: .74rem; color: var(--text-2); }
.recovery-legend span { display: inline-flex; align-items: center; gap: 5px; }
.recovery-legend i { width: 12px; height: 12px; border-radius: 3px; display: inline-block; }

/* ── Exercise rows / library ───────────────────────────────── */
.ex-row { display: flex; align-items: center; gap: 12px; padding: 12px; border-radius: var(--radius); background: var(--surface); border: 1px solid var(--border); }
.ex-row + .ex-row { margin-top: 8px; }
.ex-thumb { width: 52px; height: 52px; border-radius: 10px; object-fit: cover; background: var(--surface-2); flex-shrink: 0; }
.ex-thumb.placeholder { display: flex; align-items: center; justify-content: center; color: var(--text-3); }
.ex-info { flex: 1; min-width: 0; }
.ex-name { font-weight: 700; font-size: .96rem; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.ex-meta { color: var(--text-2); font-size: .8rem; text-transform: capitalize; }
.ex-list { display: flex; flex-direction: column; }

/* ── Active session screen ─────────────────────────────────── */
.session-head {
  position: sticky; top: 0; z-index: 20;
  background: var(--bg); padding: calc(var(--safe-top) + 10px) 16px 10px;
  max-width: var(--maxw); margin: 0 auto;
  display: flex; align-items: center; gap: 12px; border-bottom: 1px solid var(--border);
}
.session-head .s-timer { font-size: 1.5rem; font-weight: 800; font-variant-numeric: tabular-nums; letter-spacing: -.02em; }
.session-head .s-name { color: var(--text-2); font-size: .8rem; font-weight: 600; }
.session-head .grow { flex: 1; }

.ex-card { background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius); padding: 14px; box-shadow: var(--shadow); margin-bottom: 14px; }
.ex-card-head { display: flex; align-items: center; gap: 10px; margin-bottom: 4px; }
.ex-card-head .ex-name { flex: 1; }
.ex-card .last { font-size: .8rem; color: var(--text-2); margin-bottom: 10px; }
.ex-card .last b { color: var(--text); font-weight: 700; }

.set-grid { display: flex; flex-direction: column; gap: 7px; }
.set-row {
  display: grid; grid-template-columns: 34px 1fr 1fr 52px 44px; gap: 7px; align-items: center;
}
.set-row.head { color: var(--text-3); font-size: .7rem; font-weight: 700; text-transform: uppercase; letter-spacing: .04em; padding: 0 2px; }
.set-row.head > div { text-align: center; }
.set-num { display: flex; align-items: center; justify-content: center; width: 30px; height: 34px; border-radius: 8px; background: var(--surface-2); font-weight: 700; font-size: .85rem; color: var(--text-2); }
.set-num.warmup { background: rgba(245,158,11,.16); color: var(--warn); }
.set-input { width: 100%; text-align: center; padding: 9px 4px; border-radius: 8px; border: 1px solid var(--border); background: var(--bg-elev); color: var(--text); font-weight: 700; font-variant-numeric: tabular-nums; }
.set-input:focus { outline: none; border-color: var(--accent); }
.set-rpe { background: var(--surface-2); border: none; border-radius: 8px; padding: 9px 2px; color: var(--rpe); font-weight: 700; text-align: center; }
.set-check {
  width: 40px; height: 36px; border-radius: 8px; border: 2px solid var(--border);
  background: var(--bg-elev); color: var(--text-3); display: flex; align-items: center; justify-content: center;
  transition: all .15s;
}
.set-row.done .set-check { background: var(--success); border-color: var(--success); color: #fff; }
.set-row.done .set-input { border-color: transparent; background: var(--surface-2); }
.set-add { margin-top: 9px; display: flex; gap: 8px; }
.set-add .btn { flex: 1; padding: 9px; font-size: .85rem; }
.set-swipe-del { color: var(--text-3); background: none; border: none; font-size: .75rem; }

/* ── Rest timer ────────────────────────────────────────────── */
.rest-timer {
  position: fixed; left: 50%; transform: translateX(-50%);
  bottom: calc(var(--nav-h) + var(--safe-bottom) + 10px); z-index: 35;
  width: min(var(--maxw), calc(100% - 24px));
  background: var(--bg-elev); border: 1px solid var(--border); border-radius: var(--radius);
  box-shadow: var(--shadow-lg); padding: 12px 14px; display: flex; align-items: center; gap: 12px;
  animation: slideUp .2s ease;
}
.rest-timer .rt-time { font-size: 1.6rem; font-weight: 800; font-variant-numeric: tabular-nums; color: var(--accent); min-width: 64px; }
.rest-timer .rt-label { font-size: .75rem; color: var(--text-2); font-weight: 600; }
.rest-timer .grow { flex: 1; }
.rest-timer .rt-bar { position: absolute; left: 0; bottom: 0; height: 3px; background: var(--accent); border-radius: 0 0 var(--radius) var(--radius); transition: width 1s linear; }

/* ── Progress bars ─────────────────────────────────────────── */
.progress { height: 8px; border-radius: 999px; background: var(--surface-2); overflow: hidden; }
.progress > i { display: block; height: 100%; background: var(--accent); border-radius: 999px; transition: width .3s; }

/* ── Chart ─────────────────────────────────────────────────── */
.chart { width: 100%; height: auto; }
.chart .c-line { fill: none; stroke: var(--accent); stroke-width: 2.5; }
.chart .c-area { fill: var(--accent-soft); }
.chart .c-dot { fill: var(--accent); }
.chart .c-bar { fill: var(--accent); }
.chart .c-axis { stroke: var(--border); stroke-width: 1; }
.chart .c-label { fill: var(--text-3); font-size: 10px; }

/* ── History timeline ──────────────────────────────────────── */
.history-item { display: flex; gap: 14px; }
.history-date { width: 54px; flex-shrink: 0; text-align: center; }
.history-date .d { font-size: 1.4rem; font-weight: 800; line-height: 1; }
.history-date .m { font-size: .72rem; color: var(--text-3); text-transform: uppercase; font-weight: 700; }

/* ── Tabs ──────────────────────────────────────────────────── */
.tabs { display: flex; gap: 6px; background: var(--surface-2); padding: 4px; border-radius: var(--radius-sm); margin-bottom: 16px; }
.tabs button { flex: 1; padding: 9px; border: none; border-radius: 7px; background: none; color: var(--text-2); font-weight: 700; font-size: .9rem; transition: all .15s; }
.tabs button.active { background: var(--bg-elev); color: var(--text); box-shadow: var(--shadow); }

/* ── Modal ─────────────────────────────────────────────────── */
.modal-overlay {
  position: fixed; inset: 0; z-index: 100; background: var(--scrim);
  display: flex; align-items: flex-end; justify-content: center;
  opacity: 0; transition: opacity .2s; backdrop-filter: blur(2px);
}
.modal-overlay.modal-open { opacity: 1; }
.modal {
  width: 100%; max-width: var(--maxw); max-height: 92vh; background: var(--bg-elev);
  border-radius: var(--radius-lg) var(--radius-lg) 0 0; display: flex; flex-direction: column;
  transform: translateY(100%); transition: transform .25s cubic-bezier(.2,.8,.2,1);
  padding-bottom: var(--safe-bottom);
}
.modal-open .modal { transform: translateY(0); }
.modal-fullscreen { max-height: 100vh; height: 100vh; border-radius: 0; }
.modal-head { display: flex; align-items: center; justify-content: space-between; padding: 16px; border-bottom: 1px solid var(--border); }
.modal-head h3 { font-size: 1.15rem; }
.modal-body { padding: 16px; overflow-y: auto; }
@media (min-width: 700px) {
  .modal-overlay { align-items: center; }
  .modal { border-radius: var(--radius-lg); max-height: 86vh; }
}
.confirm-actions { display: flex; gap: 10px; margin-top: 18px; }
.confirm-actions .btn { flex: 1; }

/* ── Toast ─────────────────────────────────────────────────── */
#toast-root { position: fixed; left: 0; right: 0; bottom: calc(var(--nav-h) + var(--safe-bottom) + 80px); z-index: 200; display: flex; flex-direction: column; align-items: center; gap: 8px; pointer-events: none; }
.toast {
  background: var(--text); color: var(--bg); padding: 11px 18px; border-radius: 999px;
  font-weight: 600; font-size: .9rem; box-shadow: var(--shadow-lg);
  opacity: 0; transform: translateY(10px); transition: all .25s; max-width: calc(100% - 32px);
}
.toast-show { opacity: 1; transform: translateY(0); }
.toast-success { background: var(--success); color: #fff; }
.toast-error { background: var(--danger); color: #fff; }
.toast-warn { background: var(--warn); color: #111; }

/* ── Auth ──────────────────────────────────────────────────── */
.auth { min-height: 100vh; display: flex; flex-direction: column; justify-content: center; max-width: 420px; margin: 0 auto; padding: 24px; }
.auth-logo { text-align: center; margin-bottom: 24px; }
.auth-logo .mark { width: 64px; height: 64px; border-radius: 18px; background: var(--accent); color: #fff; display: inline-flex; align-items: center; justify-content: center; box-shadow: var(--shadow-lg); }
.auth-logo h1 { margin-top: 14px; font-size: 1.6rem; letter-spacing: -.02em; }
.auth-logo p { color: var(--text-2); }
.auth-switch { text-align: center; margin-top: 18px; color: var(--text-2); }
.auth-switch button { background: none; border: none; color: var(--accent); font-weight: 700; }

/* ── Achievements ──────────────────────────────────────────── */
.badge-grid { display: grid; grid-template-columns: repeat(2, 1fr); gap: 12px; }
.ach {
  background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius);
  padding: 14px; text-align: center; box-shadow: var(--shadow); opacity: .6;
}
.ach-earned { opacity: 1; border-color: var(--accent); background: linear-gradient(180deg, var(--accent-soft), var(--surface)); }
.ach-icon { font-size: 2rem; filter: grayscale(1); }
.ach-earned .ach-icon { filter: none; }
.ach-title { font-weight: 700; font-size: .92rem; margin-top: 4px; }
.ach-desc { color: var(--text-2); font-size: .76rem; margin-top: 2px; }
.ach-progress { margin-top: 8px; }
.ach-check { margin-top: 8px; color: var(--success); font-weight: 700; font-size: .78rem; }
.ach-strip { display: flex; gap: 8px; font-size: 1.6rem; }
.ach-strip .locked { filter: grayscale(1); opacity: .4; }

/* misc */
.row-between { display: flex; align-items: center; justify-content: space-between; gap: 10px; }
.muted { color: var(--text-2); }
.center { text-align: center; }
.mt { margin-top: 14px; } .mt-lg { margin-top: 22px; }
.hstack { display: flex; align-items: center; gap: 10px; }
.wrap { flex-wrap: wrap; }
.grow { flex: 1; }
.dim { color: var(--text-3); font-size: .85rem; }
.stepper { display: flex; align-items: center; gap: 6px; }
.stepper button { width: 30px; height: 30px; border-radius: 8px; border: 1px solid var(--border); background: var(--surface-2); color: var(--text); font-weight: 800; font-size: 1rem; }
