:root {
  --bg: #000;
  --bg-elev: #0a0a0a;
  --bg-card: #111;
  --border: #1a1a1a;
  --border-strong: #262626;
  --text: #f5f5f5;
  --text-dim: #888;
  --text-faint: #555;
  --accent: #3ea6ff;
  --accent-dim: #2b7fd6;
  --accent-glow: rgba(62, 166, 255, 0.18);
  --danger: #ff3355;
  --topbar-grad: linear-gradient(to bottom, rgba(0,0,0,0.95), rgba(0,0,0,0.85));
  --navbar-bg: rgba(0, 0, 0, 0.92);
  --modal-backdrop: rgba(0,0,0,0.6);
  --heat-0: #3a3a3a; --heat-3: #eab308; --heat-5: #f97316; --heat-8: #dc2626;
  --nav-h: 68px;
  --safe-t: env(safe-area-inset-top, 0px);
  --safe-b: env(safe-area-inset-bottom, 0px);
  --safe-l: env(safe-area-inset-left, 0px);
  --safe-r: env(safe-area-inset-right, 0px);
  /* Content column: full width on phones, centred 720px on tablets / landscape. */
  --col: 720px;
  --pad-l: max(var(--safe-l), calc((100vw - var(--col)) / 2));
  --pad-r: max(var(--safe-r), calc((100vw - var(--col)) / 2));
}

* { box-sizing: border-box; margin: 0; padding: 0; }

html, body {
  height: 100%;
  background: var(--bg);
  color: var(--text);
  font-family: -apple-system, BlinkMacSystemFont, "SF Pro Text", system-ui, sans-serif;
  font-size: 15px;
  line-height: 1.4;
  -webkit-tap-highlight-color: transparent;
  -webkit-text-size-adjust: 100%; text-size-adjust: 100%;
  -webkit-font-smoothing: antialiased;
  overscroll-behavior-y: none;
  -webkit-user-select: none;
  user-select: none;
  cursor: default;
}
input, textarea, select { -webkit-user-select: text; user-select: text; cursor: text; }
button, .btn, .nav-btn { cursor: pointer; }
/* No 300ms tap delay / double-tap-zoom on controls (pinch zoom still works). */
button, .btn, .chip, .segment, a, input, select, textarea, label, summary { touch-action: manipulation; }

button, input, select, textarea {
  font: inherit; color: inherit; background: transparent; border: none; outline: none;
}
button { cursor: pointer; }

#app {
  overflow-x: clip;
  display: flex; flex-direction: column;
  min-height: 100vh; min-height: 100dvh;
}

/* Top bar */
.topbar {
  position: sticky; top: 0; z-index: 10;
  display: flex; align-items: center; justify-content: space-between;
  padding: calc(14px + var(--safe-t)) calc(20px + var(--pad-r)) 12px calc(20px + var(--pad-l));
  background: var(--topbar-grad);
  backdrop-filter: blur(12px);
  -webkit-backdrop-filter: blur(12px);
  border-bottom: 1px solid var(--border);
}
.topbar h1 {
  font-size: 22px; font-weight: 800; letter-spacing: -0.02em;
  color: var(--accent);
}

/* Screens */
/* Bottom padding clears the fixed navbar so the last row of a long list
   (Profile equipment, WHOOP charts) is never hidden behind it. */
.screen {
  display: none; flex: 1;
  padding: 16px calc(16px + var(--pad-r)) calc(24px + var(--nav-h) + var(--safe-b)) calc(16px + var(--pad-l));
}
.screen.active { display: block; }
.screen h2 {
  font-size: 13px; text-transform: uppercase; letter-spacing: 0.15em;
  color: var(--text-dim); font-weight: 600; margin: 20px 4px 10px;
}
.screen h2:first-of-type { margin-top: 4px; }

/* Cards */
.card {
  background: var(--bg-card);
  border: 1px solid var(--border);
  border-radius: 14px;
  padding: 14px 16px;
  margin-bottom: 10px;
}
.card + .card { margin-top: 0; }
.card.accent { border-color: var(--accent-dim); box-shadow: 0 0 0 3px var(--accent-glow); }

.row { display: flex; align-items: center; gap: 10px; }
.row.between { justify-content: space-between; }
.spacer { flex: 1; }

/* Buttons */
.btn {
  display: inline-flex; align-items: center; justify-content: center; gap: 8px;
  padding: 12px 18px; border-radius: 12px;
  background: var(--bg-elev); border: 1px solid var(--border-strong);
  color: var(--text); font-weight: 600; font-size: 15px;
  transition: transform 0.05s, background 0.15s;
}
.btn:active { transform: scale(0.97); }
.btn.primary {
  background: var(--accent); color: #00111f; border-color: var(--accent);
  box-shadow: 0 0 20px var(--accent-glow);
}
.btn.ghost { background: transparent; border-color: var(--border-strong); color: var(--text-dim); }
.btn.danger { color: var(--danger); border-color: rgba(255,51,85,0.3); }
.btn.block { width: 100%; }
.btn.sm { padding: 8px 12px; font-size: 13px; border-radius: 9px; }
.btn.icon { padding: 10px; width: 40px; height: 40px; }

/* Inputs */
.field { display: flex; flex-direction: column; gap: 6px; margin-bottom: 12px; }
.field label { font-size: 11px; text-transform: uppercase; letter-spacing: 0.1em; color: var(--text-dim); font-weight: 600; }
.input, .select, .textarea {
  padding: 12px 14px; background: var(--bg-elev);
  border: 1px solid var(--border-strong); border-radius: 10px;
  color: var(--text); font-size: 16px; width: 100%;
}
.input:focus, .select:focus, .textarea:focus { border-color: var(--accent-dim); }
.textarea { min-height: 70px; resize: vertical; }
.inline-fields { display: grid; grid-template-columns: repeat(3, 1fr); gap: 8px; }
/* Grid items default to min-width:auto, and iOS date inputs have a wide intrinsic
   width, so they pushed past the card edge. Let them shrink and match text inputs. */
.inline-fields > * { min-width: 0; }
.input[type="date"] {
  min-width: 0; max-width: 100%;
  -webkit-appearance: none; appearance: none;
  min-height: 46px; text-align: left;
}
.input[type="date"]::-webkit-date-and-time-value { text-align: left; }

/* Set row */
.set-row {
  display: grid; grid-template-columns: 30px 1fr 60px 60px 32px; gap: 8px; align-items: center;
  padding: 10px 4px; border-bottom: 1px solid var(--border);
}
.set-row:last-child { border-bottom: none; }
.set-row .idx { color: var(--accent); font-weight: 700; font-size: 13px; text-align: center; }
.set-row .name { font-size: 14px; color: var(--text); overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.set-row .stat { text-align: right; color: var(--text-dim); font-variant-numeric: tabular-nums; font-size: 14px; }
.set-row .stat b { color: var(--text); font-weight: 600; }
.set-row .del { color: var(--text-faint); background: transparent; padding: 4px; }
.set-row .del:active { color: var(--danger); }

/* Empty */
.empty { color: var(--text-faint); text-align: center; padding: 30px 20px; font-size: 14px; }

/* Bottom Nav */
.navbar {
  position: fixed; bottom: 0; left: 0; right: 0; z-index: 30;
  display: grid; grid-auto-flow: column; grid-auto-columns: 1fr;
  height: calc(var(--nav-h) + var(--safe-b));
  padding: 0 var(--pad-r) var(--safe-b) var(--pad-l);
  /* Solid, not translucent: scrolled content must never read through the bar.
     Shadow above gives extra lift. */
  background: var(--bg-card);
  border-top: 1px solid var(--border-strong);
  box-shadow: 0 -4px 12px rgba(0, 0, 0, 0.25);
  /* Own compositing layer, so the bar stays painted while the page scrolls. */
  transform: translateZ(0);
  will-change: transform;
}
.nav-btn {
  display: flex; flex-direction: column; align-items: center; justify-content: center; gap: 3px;
  color: var(--text-faint); font-size: 10px; font-weight: 600;
  text-transform: uppercase; letter-spacing: 0.06em;
  transition: color 0.15s;
}
.nav-btn { min-width: 0; padding: 0 2px; }
.del-workout, .set-row .del { background: transparent; border: none; color: var(--text-faint); font-size: 18px; line-height: 1; padding: 4px 8px; cursor: pointer; }
.nav-btn svg { width: 22px; height: 22px; stroke-width: 2; }
.nav-btn.active { color: var(--accent); }
.nav-btn.active svg { filter: drop-shadow(0 0 6px var(--accent-glow)); }

/* Segmented control (Track tab: Strength / Cardio / Food) */
.segmented {
  display: grid; grid-auto-flow: column; grid-auto-columns: 1fr;
  gap: 2px; padding: 3px; margin-bottom: 16px;
  background: var(--bg-card); border: 1px solid var(--border); border-radius: 10px;
}
.segment {
  padding: 8px 4px; border: none; border-radius: 7px;
  background: transparent; color: var(--text-dim);
  font-size: 13px; font-weight: 600; cursor: pointer;
  transition: background 0.15s, color 0.15s;
}
.segment.active { background: var(--accent); color: #000; }

/* Selectable chips (cardio session type, RPE scale) */
.chip-row { display: flex; flex-wrap: wrap; gap: 6px; }
.chip-row.rpe .chip { flex: 1; min-width: 28px; padding: 8px 0; text-align: center; }
.chip {
  padding: 7px 12px; border-radius: 999px; cursor: pointer;
  background: var(--bg-elev); border: 1px solid var(--border-strong);
  color: var(--text-dim); font-size: 12px; font-weight: 600;
  transition: background 0.15s, color 0.15s, border-color 0.15s;
}
.chip.active { background: var(--accent); border-color: var(--accent); color: #000; }
.chip.static { cursor: default; }

.section-label {
  font-size: 11px; text-transform: uppercase; letter-spacing: 0.1em;
  color: var(--text-dim);
}

/* Modal */
.modal-backdrop {
  position: fixed; inset: 0; z-index: 50;
  background: var(--modal-backdrop);
  display: flex; align-items: flex-end; justify-content: center;
  animation: fade 0.15s;
}

.modal {
  background: var(--bg-card); border-top: 1px solid var(--border-strong);
  border-radius: 20px 20px 0 0; width: 100%; max-width: 560px;
  padding: 20px calc(16px + var(--safe-r)) calc(24px + var(--safe-b)) calc(16px + var(--safe-l));
  max-height: 90vh; max-height: calc(var(--vvh, 100dvh) * 0.9);
  overflow-y: auto; overscroll-behavior: contain; -webkit-overflow-scrolling: touch;
  animation: slideUp 0.2s;
}
.modal h3 { font-size: 18px; margin-bottom: 14px; font-weight: 700; }
@keyframes fade { from { opacity: 0; } }
@keyframes slideUp { from { transform: translateY(30px); } }

/* Toast */
.toast {
  position: fixed; bottom: calc(var(--nav-h) + var(--safe-b) + 16px); left: 50%;
  transform: translateX(-50%);
  background: var(--accent); color: #00111f;
  padding: 10px 18px; border-radius: 24px;
  font-weight: 600; font-size: 13px; z-index: 100;
  max-width: calc(100vw - 32px); text-align: center;
  box-shadow: 0 8px 24px rgba(62,166,255,0.35);
  animation: toast 2.4s;
}
@keyframes toast {
  0% { opacity: 0; transform: translate(-50%, 20px); }
  10%, 85% { opacity: 1; transform: translate(-50%, 0); }
  100% { opacity: 0; transform: translate(-50%, -10px); }
}

/* Combobox / typeahead */
.typeahead { position: relative; }
.typeahead-list {
  position: absolute; top: 100%; left: 0; right: 0; z-index: 10;
  background: var(--bg-elev); border: 1px solid var(--border-strong);
  border-radius: 10px; margin-top: 4px; max-height: 200px; overflow-y: auto;
}
.typeahead-list button {
  display: block; width: 100%; text-align: left; padding: 10px 14px;
  color: var(--text); font-size: 14px;
  border-bottom: 1px solid var(--border);
}
.typeahead-list button:last-child { border-bottom: none; }
.typeahead-list button:active { background: var(--accent-glow); }
.typeahead-list .create { color: var(--accent); font-weight: 600; }

/* List rows (injuries, equipment) */
.list-row {
  display: flex; align-items: center; gap: 10px;
  padding: 12px 4px; border-bottom: 1px solid var(--border);
}
.list-row:last-child { border-bottom: none; }
.list-row .title { font-weight: 600; font-size: 14px; }
.list-row .sub { font-size: 12px; color: var(--text-dim); margin-top: 2px; }
.list-row { flex-wrap: wrap; }
.list-row .grow { flex: 1 1 140px; min-width: 0; overflow-wrap: anywhere; }
.list-row .actions { display: flex; flex-wrap: wrap; gap: 6px; margin-left: auto; }

.badge {
  display: inline-block; padding: 2px 8px; border-radius: 10px;
  font-size: 10px; font-weight: 700; letter-spacing: 0.06em; text-transform: uppercase;
}
.badge.active  { background: rgba(255,51,85,0.15);  color: #ff6688; }
.badge.healed  { background: rgba(62,166,255,0.14);  color: var(--accent-dim); }

.stat-grid {
  display: grid; grid-template-columns: repeat(3, 1fr); gap: 10px; margin-bottom: 4px;
}
.stat-tile {
  background: var(--bg-elev); border: 1px solid var(--border); border-radius: 12px;
  padding: 12px 8px; text-align: center;
}
.stat-grid.four { grid-template-columns: repeat(4, 1fr); gap: 6px; }
.stat-grid.four .stat-tile { padding: 12px 2px; min-width: 0; }
.stat-grid.four .lbl { font-size: clamp(8px, 2.6vw, 10px); letter-spacing: 0.02em; }
.stat-grid.four .val { font-size: clamp(15px, 5.4vw, 22px); }
.stat-tile .val { font-size: 22px; font-weight: 700; color: var(--accent); font-variant-numeric: tabular-nums; }
.stat-tile .lbl { font-size: 10px; text-transform: uppercase; letter-spacing: 0.08em; color: var(--text-dim); margin-top: 4px; }

#body-map { margin: 0 auto; }
#body-map svg { width: 100%; height: 100%; display: block; }


/* ---------- Responsive / device hardening ---------- */

/* Very narrow phones (iPhone 5/SE1 320, Galaxy Fold cover 280): tighter chrome. */
@media (max-width: 359px) {
  .nav-btn { font-size: 8.5px; letter-spacing: 0.02em; }
  .topbar { padding-top: calc(10px + var(--safe-t)); }
  .topbar h1 { font-size: 20px; }
  .screen { padding-top: 12px; }
  .card { padding: 12px; }
  .modal { padding-top: 16px; }
}

/* Short viewports (phones in landscape): reclaim vertical space. */
@media (max-height: 480px) {
  :root { --nav-h: 52px; }
  .topbar { position: static; padding-top: calc(8px + var(--safe-t)); padding-bottom: 8px; }
  .nav-btn { flex-direction: row; gap: 6px; font-size: 11px; }
  .nav-btn svg { width: 20px; height: 20px; }
  .modal { max-height: 96vh; max-height: calc(var(--vvh, 100dvh) * 0.96); }
}

/* Touch devices: 44pt minimum hit areas (Apple HIG; Material asks 48dp). */
@media (pointer: coarse) {
  .btn.sm { min-height: 44px; min-width: 44px; padding: 10px 14px; }
  input[type="range"] { min-height: 44px; }
  .btn.icon { min-width: 44px; min-height: 44px; }
  .chip { min-height: 44px; min-width: 44px; padding: 10px 14px; display: inline-flex; align-items: center; justify-content: center; }
  .chip-row.rpe .chip { min-width: 0; padding: 10px 0; }
  /* 1-10 RPE: two rows of five on phones so every chip stays tappable. */
  .chip-row.rpe { display: grid; grid-template-columns: repeat(5, 1fr); }
  @media (min-width: 560px) { .chip-row.rpe { grid-template-columns: repeat(10, 1fr); } }
  .segment { min-height: 44px; }
  .set-row { grid-template-columns: 26px minmax(0, 1fr) auto auto 44px; }
  .set-row .del, .del-workout { min-width: 44px; min-height: 44px; display: inline-flex; align-items: center; justify-content: center; }
  .typeahead-list button { min-height: 44px; }
}

/* Keyboard open (set from app.js via visualViewport): hide the tab bar so it
   can't eat the little space left, and drop the bottom padding it reserved. */
html.kb-open .navbar { display: none; }
html.kb-open .screen { padding-bottom: 24px; }

/* Switch row (Profile → Integrations). The whole row is the button. */
.toggle-row {
  display: flex; align-items: center; gap: 12px; width: 100%; min-height: 56px;
  text-align: left; color: var(--text);
}
.toggle-row .grow { flex: 1; min-width: 0; display: flex; flex-direction: column; gap: 2px; }
.toggle-row .title { font-weight: 600; font-size: 15px; }
.toggle-row .sub { font-size: 12px; color: var(--text-dim); }
.switch {
  position: relative; flex: none; width: 52px; height: 32px; border-radius: 16px;
  background: var(--border-strong); transition: background 0.2s;
}
.switch::after {
  content: ''; position: absolute; top: 3px; left: 3px; width: 26px; height: 26px;
  border-radius: 50%; background: #fff; box-shadow: 0 1px 3px rgba(0,0,0,0.4);
  transition: transform 0.2s;
}
.toggle-row[aria-checked="true"] .switch { background: var(--accent); }
.toggle-row[aria-checked="true"] .switch::after { transform: translateX(20px); }
@media (prefers-reduced-motion: reduce) { .switch, .switch::after { transition: none; } }

/* Profile avatar: initials in a circle, top right. 44px = full tap target. */
.avatar {
  flex: none; width: 44px; height: 44px; border-radius: 50%;
  display: inline-flex; align-items: center; justify-content: center;
  background: var(--bg-elev); border: 1px solid var(--border-strong);
  color: var(--text-dim); font-size: 15px; font-weight: 700; letter-spacing: 0.02em;
  line-height: 1; transition: color 0.15s, border-color 0.15s, background 0.15s, transform 0.1s;
}
.avatar svg { width: 22px; height: 22px; }
.avatar:active { transform: scale(0.94); }
.avatar.active { color: var(--accent); border-color: var(--accent); background: var(--accent-glow); }


/* Sign-in / create-account screen: replaces the app until there is a session. */
.signed-out #app { display: none; }
#auth-root:empty { display: none; }
#auth-root {
  min-height: 100vh; min-height: 100dvh;
  display: flex; align-items: flex-start; justify-content: center;
  padding: max(var(--safe-t), 16px) max(var(--safe-r), 16px) max(var(--safe-b), 24px) max(var(--safe-l), 16px);
  overflow-y: auto;
}
.auth-card { width: 100%; max-width: 420px; padding-top: 6vh; }
.auth-title { font-size: 26px; font-weight: 800; letter-spacing: -0.01em; margin-bottom: 18px; text-align: center; }
.auth-card .segment { min-height: 44px; }
.auth-card .btn { min-height: 48px; }
.auth-error { margin-top: 12px; min-height: 1.4em; color: var(--danger); font-size: 14px; text-align: center; }
@media (max-height: 480px) and (orientation: landscape) { .auth-card { padding-top: 0; } }

/* Sign-up journey */
.auth-hero { text-align: center; padding: 4vh 0 22px; }
.auth-lead { color: var(--text-dim); font-size: 17px; margin: 0 auto 18px; max-width: 28ch; }
.auth-points { list-style: none; text-align: left; display: inline-flex; flex-direction: column; gap: 10px; color: var(--text-dim); font-size: 15px; margin-bottom: 8px; }
.auth-points li { padding-left: 22px; position: relative; }
.auth-points li::before { content: ''; position: absolute; left: 2px; top: 0.5em; width: 8px; height: 8px; border-radius: 50%; background: var(--accent); box-shadow: 0 0 8px var(--accent-glow); }
.auth-title.sm { font-size: 24px; text-align: left; margin: 14px 0 6px; }
.auth-card { padding-top: 2vh; }
.auth-hero .auth-title { margin-bottom: 10px; }
.auth-nav { display: flex; align-items: center; justify-content: space-between; min-height: 44px; }
.auth-back { min-height: 44px; min-width: 44px; padding: 0 6px 0 0; color: var(--accent); font-size: 16px; font-weight: 600; text-align: left; }
.auth-step-label { font-size: 12px; text-transform: uppercase; letter-spacing: 0.1em; color: var(--text-dim); font-weight: 600; }
.auth-dots { display: flex; gap: 6px; margin: 4px 0 6px; }
.auth-dot { flex: 1; height: 4px; border-radius: 2px; background: var(--border-strong); transition: background 0.2s; }
.auth-dot.on { background: var(--accent); }
.auth-intro { color: var(--text-dim); font-size: 14px; margin-bottom: 16px; }
.auth-hint { font-size: 12px; color: var(--text-faint); }
.auth-pw { position: relative; }
.auth-pw .input { padding-right: 64px; }
.auth-reveal { position: absolute; right: 4px; top: 0; bottom: 0; min-width: 56px; color: var(--accent); font-size: 14px; font-weight: 600; }
.auth-chips .chip { min-height: 44px; flex: 1 1 auto; }
.auth-switch { margin-top: 16px; text-align: center; color: var(--text-dim); font-size: 14px; }
.auth-link { color: var(--accent); font-weight: 600; font-size: 14px; min-height: 44px; padding: 0 4px; }
.auth-switch .auth-link { min-height: 0; padding: 0; }
.auth-review { padding: 4px 14px; margin-bottom: 14px; }
.auth-review-row { display: flex; align-items: center; justify-content: space-between; gap: 12px; padding: 10px 0; border-bottom: 1px solid var(--border); }
.auth-review-row:last-child { border-bottom: none; }
.auth-review-label { font-size: 11px; text-transform: uppercase; letter-spacing: 0.1em; color: var(--text-dim); font-weight: 600; }
.auth-review-value { font-size: 16px; overflow-wrap: anywhere; }
