:root {
  --bg: #0b1220;
  --panel: #131c2e;
  --panel-2: #1b2740;
  --line: #26324d;
  --text: #e7edf7;
  --muted: #93a0b8;
  --accent: #4f8cff;
  --good: #34d399;
  --warn: #fbbf24;
  --bad: #f87171;
  --swim: #38bdf8;
  --ride: #f59e0b;
  --run: #34d399;
  --radius: 14px;
}

* { box-sizing: border-box; }

html, body {
  margin: 0;
  padding: 0;
  background: var(--bg);
  color: var(--text);
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Helvetica, Arial, sans-serif;
  -webkit-font-smoothing: antialiased;
  line-height: 1.45;
}

.screen { min-height: 100vh; }
[hidden] { display: none !important; }
.muted { color: var(--muted); }
.error { color: var(--bad); }

/* ---------- Connect ---------- */
#connect,
#subscribe {
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 24px;
}
#subscribe .connect-card { text-align: center; }
#subscribe-recheck {
  background: var(--accent);
  color: white;
  padding: 12px;
  font-weight: 600;
  margin-top: 18px;
  width: 100%;
}
.connect-card {
  width: 100%;
  max-width: 420px;
  background: var(--panel);
  border: 1px solid var(--line);
  border-radius: var(--radius);
  padding: 28px;
}
.brand {
  font-size: 30px;
  margin: 0 0 4px;
  letter-spacing: -0.02em;
}
.connect-card form { display: flex; flex-direction: column; gap: 14px; margin-top: 18px; }
.connect-card label { display: flex; flex-direction: column; gap: 6px; font-size: 13px; color: var(--muted); }
.connect-card input {
  background: var(--bg);
  border: 1px solid var(--line);
  border-radius: 10px;
  padding: 11px 12px;
  color: var(--text);
  font-size: 15px;
}
.connect-card input:focus { outline: none; border-color: var(--accent); }
details summary { cursor: pointer; font-size: 13px; color: var(--muted); }
details[open] summary { margin-bottom: 8px; }
.hint { font-size: 12px; margin-top: 16px; }

button {
  font-family: inherit;
  cursor: pointer;
  border: none;
  border-radius: 10px;
  font-size: 15px;
}
#connect-btn {
  background: var(--accent);
  color: white;
  padding: 12px;
  font-weight: 600;
}
#connect-btn:disabled { opacity: 0.6; cursor: default; }
#auth-btn {
  background: var(--accent);
  color: white;
  padding: 12px;
  font-weight: 600;
}
#auth-btn:disabled { opacity: 0.6; cursor: default; }

/* Sign up / log in toggle */
.seg {
  display: flex;
  gap: 4px;
  margin-top: 18px;
  padding: 4px;
  background: var(--bg);
  border: 1px solid var(--line);
  border-radius: 10px;
}
.seg-btn {
  flex: 1;
  background: transparent;
  color: var(--muted);
  padding: 8px;
  font-weight: 600;
}
.seg-btn.active { background: var(--panel-2); color: var(--text); }

/* ---------- Topbar / tabs ---------- */
.topbar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 14px 18px;
  border-bottom: 1px solid var(--line);
  position: sticky;
  top: 0;
  background: rgba(11,18,32,0.92);
  backdrop-filter: blur(8px);
  z-index: 5;
  flex-wrap: wrap;
  gap: 10px;
}
.topbar-left { display: flex; align-items: baseline; gap: 10px; }
.brand-sm { font-weight: 700; font-size: 18px; letter-spacing: -0.02em; }
.topbar-right { display: flex; gap: 8px; }
.ghost {
  background: var(--panel-2);
  color: var(--text);
  border: 1px solid var(--line);
  padding: 8px 14px;
}
.ghost:hover { border-color: var(--accent); }

.tabs {
  display: flex;
  gap: 4px;
  padding: 10px 14px;
  overflow-x: auto;
  border-bottom: 1px solid var(--line);
  position: sticky;
  top: 57px;
  background: rgba(11,18,32,0.92);
  backdrop-filter: blur(8px);
  z-index: 4;
}
.tab {
  background: transparent;
  color: var(--muted);
  padding: 8px 14px;
  white-space: nowrap;
  border-radius: 999px;
}
.tab.active { background: var(--panel-2); color: var(--text); }

.sync-status { padding: 8px 18px 0; font-size: 13px; }
.content { padding: 18px; max-width: 920px; margin: 0 auto; }

/* ---------- Cards & layout ---------- */
.card {
  background: var(--panel);
  border: 1px solid var(--line);
  border-radius: var(--radius);
  padding: 18px;
  margin-bottom: 16px;
}
.card h2 { margin: 0 0 4px; font-size: 16px; }
.card h3 { margin: 0 0 12px; font-size: 14px; color: var(--muted); font-weight: 600; }
.section-title { font-size: 13px; text-transform: uppercase; letter-spacing: 0.06em; color: var(--muted); margin: 22px 4px 10px; }

.stat-row { display: flex; flex-wrap: wrap; gap: 18px; }
.stat { display: flex; flex-direction: column; }
.stat .v { font-size: 26px; font-weight: 700; }
.stat .l { font-size: 12px; color: var(--muted); }

.grid { display: grid; grid-template-columns: repeat(auto-fill, minmax(160px, 1fr)); gap: 12px; }

.pill {
  display: inline-block;
  font-size: 11px;
  padding: 2px 9px;
  border-radius: 999px;
  background: var(--panel-2);
  color: var(--muted);
  border: 1px solid var(--line);
  text-transform: capitalize;
}
.pill.good { color: var(--good); border-color: var(--good); }
.pill.warn { color: var(--warn); border-color: var(--warn); }
.pill.bad { color: var(--bad); border-color: var(--bad); }
.pill.phase-build { color: var(--accent); border-color: var(--accent); }
.pill.phase-peak { color: var(--bad); border-color: var(--bad); }
.pill.phase-base { color: var(--good); border-color: var(--good); }
.pill.phase-recovery, .pill.phase-taper { color: var(--warn); border-color: var(--warn); }

/* ---------- Bars ---------- */
.bar-track {
  background: var(--bg);
  border-radius: 6px;
  height: 10px;
  overflow: hidden;
  border: 1px solid var(--line);
}
.bar-fill { height: 100%; background: var(--accent); }
.bar-fill.split { display: flex; }
.bar-fill .seg-swim { background: var(--swim); }
.bar-fill .seg-ride { background: var(--ride); }
.bar-fill .seg-run { background: var(--run); }

.week-row { display: grid; grid-template-columns: 70px 1fr auto; gap: 12px; align-items: center; padding: 9px 0; border-bottom: 1px solid var(--line); }
.week-row:last-child { border-bottom: none; }
.week-row .wk { font-size: 13px; }
.week-row .tss { font-size: 13px; color: var(--muted); min-width: 64px; text-align: right; }

/* ---------- Workouts ---------- */
.day-group { margin-bottom: 14px; }
.day-head { display: flex; align-items: center; gap: 8px; font-size: 13px; color: var(--muted); margin-bottom: 6px; }
.day-head.today { color: var(--accent); font-weight: 600; }
.workout {
  background: var(--panel-2);
  border: 1px solid var(--line);
  border-radius: 10px;
  padding: 12px 14px;
  margin-bottom: 8px;
}
.workout .w-top { display: flex; justify-content: space-between; gap: 10px; align-items: baseline; }
.workout .w-name { font-weight: 600; font-size: 14px; }
.workout .w-desc { font-size: 13px; color: var(--muted); margin-top: 6px; white-space: pre-wrap; }
.sport-dot { width: 9px; height: 9px; border-radius: 999px; display: inline-block; margin-right: 7px; vertical-align: middle; }
.sport-swim { background: var(--swim); }
.sport-ride, .sport-bike { background: var(--ride); }
.sport-run { background: var(--run); }
.sport-other { background: var(--muted); }
.status-completed { color: var(--good); }
.status-skipped { color: var(--bad); }

/* ---------- Lists ---------- */
.list-row {
  display: flex;
  justify-content: space-between;
  gap: 12px;
  padding: 10px 0;
  border-bottom: 1px solid var(--line);
}
.list-row:last-child { border-bottom: none; }
.list-row .meta { font-size: 12px; color: var(--muted); }

/* ---------- Coach / insights ---------- */
.insight { display: flex; gap: 10px; padding: 10px 0; border-bottom: 1px solid var(--line); }
.insight:last-child { border-bottom: none; }
.insight .ic { font-size: 18px; line-height: 1.2; }
.insight .hl { font-weight: 600; font-size: 14px; }
.insight .dt { font-size: 13px; color: var(--muted); }

/* ---------- Dietary preferences ---------- */
.diet-filters { display: flex; flex-wrap: wrap; gap: 8px; }
.diet-filter { display: inline-flex; align-items: center; gap: 6px; font-size: 13px;
  background: var(--panel-2); border: 1px solid var(--line); border-radius: 999px;
  padding: 6px 12px; cursor: pointer; }
#avoid-ingredients { width: 100%; box-sizing: border-box; margin-top: 4px; padding: 8px 10px;
  background: var(--panel-2); border: 1px solid var(--line); border-radius: 8px; color: inherit; }

/* ---------- Recipes ---------- */
.recipe { background: var(--panel-2); border: 1px solid var(--line); border-radius: 10px; padding: 12px; }
.recipe .r-name { font-weight: 600; font-size: 14px; }
.recipe .r-macros { font-size: 12px; color: var(--muted); margin-top: 4px; }
.recipe .r-tags { margin-top: 8px; display: flex; flex-wrap: wrap; gap: 5px; }

.loading, .empty { color: var(--muted); padding: 24px 0; text-align: center; }
.spinner {
  display: inline-block; width: 18px; height: 18px;
  border: 2px solid var(--line); border-top-color: var(--accent);
  border-radius: 999px; animation: spin 0.7s linear infinite; vertical-align: middle; margin-right: 8px;
}
@keyframes spin { to { transform: rotate(360deg); } }

@media (max-width: 520px) {
  .content { padding: 14px; }
  .stat .v { font-size: 22px; }
}

/* ----- Build tab: forms for the hackable surface ----- */
.form-row { display: flex; flex-wrap: wrap; gap: 8px; align-items: center; margin: 8px 0; }
.inp {
  flex: 1; min-width: 120px;
  background: var(--bg); color: var(--text);
  border: 1px solid var(--line); border-radius: 8px; padding: 9px 11px; font-size: 13px;
}
.inp:focus { outline: none; border-color: var(--accent); }
textarea.code, .code {
  width: 100%; box-sizing: border-box;
  background: var(--bg); color: var(--text);
  border: 1px solid var(--line); border-radius: 8px; padding: 11px;
  font-family: ui-monospace, SFMono-Regular, Menlo, monospace; font-size: 13px; line-height: 1.5;
}
.code { white-space: pre-wrap; }
.chk { display: inline-flex; align-items: center; gap: 5px; font-size: 12px; color: var(--muted); }
.card a { color: var(--accent); text-decoration: none; }
.card a:hover { text-decoration: underline; }

/* ----- Why tab: glass-box reasoning ----- */
.formula {
  font-family: ui-monospace, SFMono-Regular, Menlo, monospace; font-size: 13px;
  background: var(--panel-2); border: 1px solid var(--line); border-radius: 8px;
  padding: 9px 11px; margin: 10px 0; color: var(--text); overflow-x: auto;
}
.derivation {
  margin: 6px 0 4px; padding-left: 20px; font-size: 13px; color: var(--muted);
  font-family: ui-monospace, SFMono-Regular, Menlo, monospace; line-height: 1.7;
}
.why-link { display: inline-block; margin-top: 12px; font-size: 13px; color: var(--accent); cursor: pointer; }
.why-link:hover { text-decoration: underline; }
