/* Form & Fuel. Mobile-first, token-driven, light and dark. */

:root {
  --bg: #0f1623;
  --surface: #1a2333;
  --surface-2: #243044;
  --border: #334155;
  --text: #f8fafc;
  --muted: #a8b4c6;
  --primary: #f97316;
  --on-primary: #0f172a;
  --primary-text: #fb923c;
  --primary-soft: rgba(249, 115, 22, 0.14);
  --good: #4ade80;
  --good-soft: rgba(34, 197, 94, 0.14);
  --warn: #fbbf24;
  --warn-soft: rgba(251, 191, 36, 0.13);
  --info: #7dd3fc;
  --info-soft: rgba(56, 189, 248, 0.12);
  --danger: #f87171;
  --carbs: #38bdf8;
  --fat: #a78bfa;
  --track: #2b384d;
  --scrim: rgba(3, 7, 18, 0.62);
  --shadow: 0 8px 24px rgba(0, 0, 0, 0.35);

  --r-sm: 8px;
  --r-md: 14px;
  --r-lg: 20px;
  --nav-h: 64px;
  /* Room kept clear at the bottom of the screen: the phone's home bar, or the
     host's badge when the site is on a free hosting plan (set in app.js). */
  --bottom-inset: max(env(safe-area-inset-bottom), var(--host-badge, 0px));
  --font-body: 'Barlow', system-ui, -apple-system, 'Segoe UI', sans-serif;
  --font-head: 'Barlow Condensed', 'Barlow', system-ui, -apple-system, sans-serif;
  color-scheme: dark;
}

/* Light tokens: used when the user picks light, or follows a light system theme. */
:root[data-theme='light'] {
  --bg: #f3f5f8;
  --surface: #ffffff;
  --surface-2: #edf0f4;
  --border: #d3d9e2;
  --text: #0f172a;
  --muted: #4d5b73;
  --primary-text: #c2410c;
  --primary-soft: rgba(249, 115, 22, 0.12);
  --good: #15803d;
  --good-soft: rgba(22, 163, 74, 0.1);
  --warn: #a16207;
  --warn-soft: rgba(202, 138, 4, 0.12);
  --info: #0369a1;
  --info-soft: rgba(2, 132, 199, 0.09);
  --danger: #b91c1c;
  --carbs: #0284c7;
  --fat: #7c3aed;
  --track: #e2e7ee;
  --scrim: rgba(15, 23, 42, 0.45);
  --shadow: 0 8px 24px rgba(15, 23, 42, 0.12);
  color-scheme: light;
}

@media (prefers-color-scheme: light) {
  :root:not([data-theme]) {
    --bg: #f3f5f8;
    --surface: #ffffff;
    --surface-2: #edf0f4;
    --border: #d3d9e2;
    --text: #0f172a;
    --muted: #4d5b73;
    --primary-text: #c2410c;
    --primary-soft: rgba(249, 115, 22, 0.12);
    --good: #15803d;
    --good-soft: rgba(22, 163, 74, 0.1);
    --warn: #a16207;
    --warn-soft: rgba(202, 138, 4, 0.12);
    --info: #0369a1;
    --info-soft: rgba(2, 132, 199, 0.09);
    --danger: #b91c1c;
    --carbs: #0284c7;
    --fat: #7c3aed;
    --track: #e2e7ee;
    --scrim: rgba(15, 23, 42, 0.45);
    --shadow: 0 8px 24px rgba(15, 23, 42, 0.12);
    color-scheme: light;
  }
}

*, *::before, *::after { box-sizing: border-box; }

html { -webkit-text-size-adjust: 100%; }

body {
  margin: 0;
  background: var(--bg);
  color: var(--text);
  font: 400 1rem/1.5 var(--font-body);
  -webkit-font-smoothing: antialiased;
  overflow-wrap: break-word;
}

h1, h2, h3, p { margin: 0; }
h1, h2, h3 { font-family: var(--font-head); line-height: 1.1; letter-spacing: 0.01em; text-wrap: balance; }
h1 { font-size: 2.125rem; font-weight: 700; }
h2 { font-size: 1.5rem; font-weight: 600; }
h3 { font-size: 1.25rem; font-weight: 600; }

button, input, select, textarea { font: inherit; color: inherit; }
button { touch-action: manipulation; }
a { color: var(--primary-text); }

:focus-visible { outline: 3px solid var(--primary); outline-offset: 2px; border-radius: 4px; }
#view:focus { outline: none; }

.skip-link {
  position: absolute; left: 8px; top: -60px; z-index: 100;
  background: var(--primary); color: var(--on-primary); padding: 10px 14px; border-radius: var(--r-sm); font-weight: 600;
}
.skip-link:focus { top: 8px; }

.sr-only {
  position: absolute; width: 1px; height: 1px; padding: 0; margin: -1px; overflow: hidden;
  clip: rect(0, 0, 0, 0); white-space: nowrap; border: 0;
}

/* ---------- Layout ---------- */

#view {
  max-width: 720px;
  margin: 0 auto;
  padding: calc(20px + env(safe-area-inset-top)) max(16px, env(safe-area-inset-left)) calc(var(--nav-h) + 40px + var(--bottom-inset)) max(16px, env(safe-area-inset-right));
  display: flex;
  flex-direction: column;
  gap: 16px;
}
#app.no-nav #view { padding-bottom: calc(48px + var(--host-badge, 0px)); }
#app.has-rest #view { padding-bottom: calc(var(--nav-h) + 110px + var(--bottom-inset)); }

.page-head { display: flex; align-items: flex-end; justify-content: space-between; gap: 12px; margin-bottom: 4px; }
.eyebrow {
  font: 600 0.875rem/1.2 var(--font-head); letter-spacing: 0.1em; text-transform: uppercase; color: var(--primary-text);
}
.muted { color: var(--muted); }
.small { font-size: 0.875rem; }
.num { font-variant-numeric: tabular-nums; }
.stack { display: flex; flex-direction: column; gap: 12px; }
.stack-sm { display: flex; flex-direction: column; gap: 6px; }
.row { display: flex; align-items: center; gap: 12px; flex-wrap: wrap; }
.row.between { justify-content: space-between; }
.row.nowrap { flex-wrap: nowrap; }
.grow { flex: 1 1 0; min-width: 0; }
.grid-2 { display: grid; grid-template-columns: repeat(2, minmax(0, 1fr)); gap: 12px; }
.section-title { margin-top: 12px; }

/* ---------- Navigation ---------- */

#nav {
  position: fixed; left: 0; right: 0; bottom: 0; z-index: 20;
  display: flex; justify-content: space-around;
  background: var(--surface); border-top: 1px solid var(--border);
  padding: 0 max(4px, env(safe-area-inset-left)) var(--bottom-inset) max(4px, env(safe-area-inset-right));
}
#nav[hidden] { display: none; }
#nav a {
  flex: 1 1 0; max-width: 120px; min-height: var(--nav-h);
  display: flex; flex-direction: column; align-items: center; justify-content: center; gap: 3px;
  color: var(--muted); text-decoration: none; font-size: 0.75rem; font-weight: 500;
  border-radius: var(--r-sm); position: relative;
  transition: color 160ms ease-out;
}
#nav a .icon { width: 24px; height: 24px; }
#nav a[aria-current='page'] { color: var(--primary-text); font-weight: 600; }
#nav a[aria-current='page']::before {
  content: ''; position: absolute; top: 0; width: 28px; height: 3px; border-radius: 0 0 3px 3px; background: var(--primary);
}

@media (min-width: 900px) {
  #nav {
    top: 0; right: auto; bottom: 0; width: 92px; flex-direction: column; justify-content: flex-start; gap: 4px;
    border-top: 0; border-right: 1px solid var(--border); padding: 24px 6px;
  }
  #nav a { flex: 0 0 auto; max-width: none; min-height: 68px; }
  #nav a[aria-current='page']::before { top: 22px; left: 0; width: 3px; height: 24px; border-radius: 0 3px 3px 0; }
  #app:not(.no-nav) #view { padding-left: calc(92px + 24px); padding-bottom: calc(48px + var(--host-badge, 0px)); max-width: 860px; }
  #app.has-rest #view { padding-bottom: 130px; }
}

/* ---------- Icons ---------- */

.icon {
  width: 20px; height: 20px; flex: none;
  fill: none; stroke: currentColor; stroke-width: 2; stroke-linecap: round; stroke-linejoin: round;
}

/* ---------- Cards ---------- */

.card {
  background: var(--surface); border: 1px solid var(--border); border-radius: var(--r-lg);
  padding: 16px; display: flex; flex-direction: column; gap: 12px;
}
.card.tight { padding: 14px; gap: 8px; }
.card.next { border-color: var(--primary); background: linear-gradient(180deg, var(--primary-soft), transparent 70%), var(--surface); }
.card h3 { display: flex; align-items: center; gap: 8px; }

.chip {
  display: inline-flex; align-items: center; gap: 6px; padding: 3px 10px; border-radius: 999px;
  background: var(--surface-2); color: var(--muted); font-size: 0.8125rem; font-weight: 500; white-space: nowrap;
}
.chip.primary { background: var(--primary-soft); color: var(--primary-text); }
.chip.good { background: var(--good-soft); color: var(--good); }
.chip .icon { width: 14px; height: 14px; }

.insight { display: flex; gap: 12px; padding: 14px; border-radius: var(--r-md); border: 1px solid var(--border); background: var(--surface); }
.insight .icon { margin-top: 2px; }
.insight.good { background: var(--good-soft); border-color: transparent; }
.insight.good .icon { color: var(--good); }
.insight.warn { background: var(--warn-soft); border-color: transparent; }
.insight.warn .icon { color: var(--warn); }
.insight.info { background: var(--info-soft); border-color: transparent; }
.insight.info .icon { color: var(--info); }
.insight strong { display: block; font-weight: 600; }
.insight p { color: var(--muted); font-size: 0.9375rem; }

.empty { text-align: center; padding: 28px 16px; color: var(--muted); display: flex; flex-direction: column; align-items: center; gap: 10px; }
.empty .icon { width: 32px; height: 32px; }

/* ---------- Buttons ---------- */

.btn {
  display: inline-flex; align-items: center; justify-content: center; gap: 8px;
  min-height: 48px; padding: 0 18px; border-radius: var(--r-md); border: 1px solid transparent;
  background: var(--surface-2); color: var(--text); font-weight: 600; cursor: pointer; text-decoration: none; white-space: nowrap;
  transition: transform 120ms ease-out, background-color 160ms ease-out, opacity 160ms ease-out;
}
.btn:active { transform: scale(0.97); }
.btn.primary { background: var(--primary); color: var(--on-primary); }
.btn.ghost { background: transparent; border-color: var(--border); }
.btn.danger { background: transparent; border-color: var(--danger); color: var(--danger); }
.btn.block { width: 100%; }
.btn.sm { min-height: 44px; padding: 0 14px; font-size: 0.9375rem; }
.btn[disabled] { opacity: 0.45; cursor: not-allowed; }
.btn:focus-within { outline: 3px solid var(--primary); outline-offset: 2px; }
.icon-btn[disabled] { opacity: 0.35; cursor: not-allowed; }
@media (hover: hover) {
  .btn:hover { opacity: 0.88; }
}

.icon-btn {
  display: inline-flex; align-items: center; justify-content: center; width: 44px; height: 44px; flex: none;
  border-radius: 50%; border: 0; background: transparent; color: var(--muted); cursor: pointer;
  transition: background-color 160ms ease-out;
}
.icon-btn:active { background: var(--surface-2); }
@media (hover: hover) {
  .icon-btn:hover { background: var(--surface-2); color: var(--text); }
}

.seg { display: flex; padding: 4px; gap: 4px; border-radius: var(--r-md); background: var(--surface-2); }
.seg button {
  flex: 1 1 0; min-height: 44px; border: 0; border-radius: 10px; background: transparent; color: var(--muted);
  font-weight: 600; cursor: pointer; transition: background-color 160ms ease-out, color 160ms ease-out;
}
.seg button[aria-selected='true'] { background: var(--surface); color: var(--text); box-shadow: 0 1px 3px rgba(0, 0, 0, 0.18); }

/* ---------- Forms ---------- */

.field { display: flex; flex-direction: column; gap: 6px; min-width: 0; }
.field > label, fieldset > legend { font-weight: 600; font-size: 0.9375rem; }
.field .hint { color: var(--muted); font-size: 0.875rem; }
.field .error { color: var(--danger); font-size: 0.875rem; font-weight: 500; }

input[type='text'], input[type='number'], input[type='search'], input[type='date'], input[type='time'], select, textarea {
  width: 100%; min-height: 48px; padding: 10px 12px; border-radius: var(--r-sm);
  border: 1px solid var(--border); background: var(--bg); color: var(--text); font-size: 1rem;
}
input[aria-invalid='true'] { border-color: var(--danger); }
input::placeholder { color: var(--muted); opacity: 0.8; }

fieldset { border: 0; padding: 0; margin: 0; min-width: 0; display: flex; flex-direction: column; gap: 8px; }
fieldset > legend { padding: 0; margin-bottom: 8px; }

.choices { display: grid; grid-template-columns: repeat(auto-fit, minmax(150px, 1fr)); gap: 8px; }
.choice { position: relative; display: block; cursor: pointer; }
.choice input { position: absolute; opacity: 0; inset: 0; width: 100%; height: 100%; margin: 0; cursor: pointer; }
.choice span {
  display: flex; flex-direction: column; gap: 2px; height: 100%; min-height: 48px; justify-content: center;
  padding: 10px 12px; border-radius: var(--r-sm); border: 1px solid var(--border); background: var(--bg);
  transition: border-color 160ms ease-out, background-color 160ms ease-out;
}
.choice span b { font-weight: 600; }
.choice span small { color: var(--muted); font-size: 0.8125rem; line-height: 1.35; }
.choice input:checked + span { border-color: var(--primary); background: var(--primary-soft); }
.choice input:focus-visible + span { outline: 3px solid var(--primary); outline-offset: 2px; }

/* ---------- Progress ---------- */

.ring { position: relative; width: 132px; height: 132px; flex: none; }
.ring svg { width: 100%; height: 100%; transform: rotate(-90deg); }
.ring circle { fill: none; stroke-width: 11; }
.ring .track { stroke: var(--track); }
.ring .value { stroke: var(--primary); stroke-linecap: round; transition: stroke-dashoffset 400ms ease-out; }
.ring.over .value { stroke: var(--warn); }
.ring .label { position: absolute; inset: 0; display: flex; flex-direction: column; align-items: center; justify-content: center; text-align: center; }
.ring .label b { font: 700 1.875rem/1 var(--font-head); font-variant-numeric: tabular-nums; }
.ring .label span { font-size: 0.8125rem; color: var(--muted); }

.meter { display: flex; flex-direction: column; gap: 4px; }
.meter .top { display: flex; justify-content: space-between; gap: 8px; font-size: 0.875rem; }
.meter .top b { font-weight: 600; }
.meter .bar { height: 8px; border-radius: 999px; background: var(--track); overflow: hidden; }
.meter .bar i { display: block; height: 100%; border-radius: 999px; background: var(--primary); transition: width 400ms ease-out; }
.meter.carbs .bar i { background: var(--carbs); }
.meter.fat .bar i { background: var(--fat); }
.meter.good .bar i { background: var(--good); }
.meter.water .bar i { background: var(--carbs); }

.stat b { display: block; font: 700 1.75rem/1.1 var(--font-head); font-variant-numeric: tabular-nums; }
.stat span { color: var(--muted); font-size: 0.875rem; }

.dots { display: flex; gap: 6px; }
.dots i { width: 22px; height: 8px; border-radius: 999px; background: var(--track); }
.dots i.on { background: var(--good); }

.chart { width: 100%; height: auto; display: block; }
.chart .grid { stroke: var(--border); stroke-width: 1; opacity: 0.6; }
.chart .line { fill: none; stroke: var(--primary); stroke-width: 2.5; stroke-linecap: round; stroke-linejoin: round; }
.chart .area { fill: var(--primary-soft); }
.chart .dot { fill: var(--primary); }
.chart .bar { fill: var(--primary); }
.chart .bar.dim { fill: var(--track); }
.chart .bar.good { fill: var(--good); }
.chart .target { stroke: var(--muted); stroke-width: 1.5; stroke-dasharray: 5 4; }
.chart text { fill: var(--muted); font: 500 11px var(--font-body); font-variant-numeric: tabular-nums; }
.chart text.val { fill: var(--text); font-weight: 600; }

/* ---------- Lists ---------- */

.list { display: flex; flex-direction: column; }
.list > * + * { border-top: 1px solid var(--border); }
.item { display: flex; align-items: center; gap: 12px; padding: 10px 0; min-height: 52px; }
.item .title { font-weight: 500; }
.item .meta { color: var(--muted); font-size: 0.875rem; }
.item .end { margin-left: auto; text-align: right; flex: none; font-variant-numeric: tabular-nums; }

button.item {
  width: 100%; border: 0; background: transparent; text-align: left; cursor: pointer; padding: 10px 8px; border-radius: var(--r-sm);
}
button.item:active { background: var(--surface-2); }
@media (hover: hover) {
  button.item:hover { background: var(--surface-2); }
}

details > summary { cursor: pointer; list-style: none; min-height: 44px; display: flex; align-items: center; gap: 8px; }
details > summary::-webkit-details-marker { display: none; }
details > summary::after { content: ''; margin-left: auto; width: 8px; height: 8px; border-right: 2px solid var(--muted); border-bottom: 2px solid var(--muted); transform: rotate(45deg); transition: transform 160ms ease-out; flex: none; }
details[open] > summary::after { transform: rotate(225deg); }
details.cue > summary { color: var(--primary-text); font-size: 0.9375rem; font-weight: 500; }
details.cue > summary::after { margin-left: 4px; }
details.cue p { color: var(--muted); font-size: 0.9375rem; padding-bottom: 4px; }

.ing { list-style: none; margin: 0; padding: 0; display: flex; flex-direction: column; gap: 4px; font-size: 0.9375rem; }
.ing li { display: flex; justify-content: space-between; gap: 12px; }
.ing li span:last-child { text-align: right; flex: none; }

/* ---------- Exercise photos, list and video ---------- */

.thumb {
  position: relative; width: 56px; height: 56px; flex: none; border-radius: var(--r-sm); overflow: hidden;
  background: var(--surface-2); color: var(--muted); display: inline-flex; align-items: center; justify-content: center;
}
.thumb img { position: absolute; inset: 0; width: 100%; height: 100%; object-fit: cover; }

.media { display: grid; grid-template-columns: repeat(2, minmax(0, 1fr)); gap: 8px; }
.media figure { margin: 0; display: flex; flex-direction: column; gap: 4px; }
.media img { width: 100%; aspect-ratio: 3 / 2; object-fit: cover; border-radius: var(--r-sm); background: var(--surface-2); display: block; }
.media figcaption { font-size: 0.8125rem; color: var(--muted); text-align: center; }

.video { width: 100%; aspect-ratio: 16 / 9; border: 0; border-radius: var(--r-sm); background: #000; display: block; }

.item > button.item { width: auto; min-width: 0; }
button.item h3 { font-size: 1.25rem; }
.tick { display: inline-flex; align-items: center; justify-content: center; width: 48px; height: 48px; flex: none; cursor: pointer; }
.tick input { width: 26px; height: 26px; accent-color: var(--primary); cursor: pointer; }
a.chip { text-decoration: none; min-height: 36px; padding: 6px 12px; }
.seg button[aria-pressed='true'] { background: var(--surface); color: var(--text); box-shadow: 0 1px 3px rgba(0, 0, 0, 0.18); }

/* ---------- Active workout ---------- */

.suggest { display: flex; gap: 8px; padding: 10px 12px; border-radius: var(--r-sm); background: var(--primary-soft); font-size: 0.9375rem; }
.suggest .icon { color: var(--primary-text); margin-top: 2px; }

.sets { display: flex; flex-direction: column; gap: 8px; }
.set-row { display: grid; grid-template-columns: 28px minmax(0, 1fr) minmax(0, 1fr) 48px; gap: 8px; align-items: center; }
.set-row.single { grid-template-columns: 28px minmax(0, 1fr) 48px; }
.set-row.head { font-size: 0.8125rem; color: var(--muted); font-weight: 500; }
.set-row .n { font: 600 1.125rem var(--font-head); color: var(--muted); text-align: center; }
.set-row input { text-align: center; font-variant-numeric: tabular-nums; font-weight: 600; }
.set-row.done input { border-color: transparent; background: var(--good-soft); }
.check {
  width: 48px; height: 48px; border-radius: var(--r-sm); border: 1px solid var(--border); background: var(--bg); color: var(--muted);
  display: inline-flex; align-items: center; justify-content: center; cursor: pointer;
  transition: background-color 160ms ease-out, color 160ms ease-out, transform 120ms ease-out;
}
.check:active { transform: scale(0.94); }
.check[aria-pressed='true'] { background: var(--good); border-color: var(--good); color: #052e16; }

#rest {
  position: fixed; z-index: 30; left: 12px; right: 12px; bottom: calc(var(--nav-h) + 12px + var(--bottom-inset));
  max-width: 696px; margin: 0 auto; display: flex; align-items: center; gap: 12px;
  padding: 10px 10px 10px 16px; border-radius: var(--r-md); background: var(--surface-2); border: 1px solid var(--border); box-shadow: var(--shadow);
}
#rest[hidden] { display: none; }
#rest b { font: 700 1.5rem/1 var(--font-head); font-variant-numeric: tabular-nums; min-width: 3.2ch; }
#rest.done { border-color: var(--good); }
@media (min-width: 900px) {
  #rest { left: 116px; bottom: calc(16px + var(--host-badge, 0px)); }
}

/* ---------- Sheet (dialog) ---------- */

#sheet {
  border: 0; padding: 0; margin: auto auto 0; width: 100%; max-width: 640px; max-height: 90dvh;
  background: var(--surface); color: var(--text); border-radius: var(--r-lg) var(--r-lg) 0 0; box-shadow: var(--shadow);
  overflow: hidden;
}
#sheet[open] { display: flex; flex-direction: column; animation: sheet-in 220ms ease-out; }
#sheet::backdrop { background: var(--scrim); }
.sheet-head { display: flex; align-items: center; justify-content: space-between; gap: 12px; padding: 14px 8px 8px 20px; }
.sheet-body { padding: 4px 20px calc(20px + env(safe-area-inset-bottom)); overflow-y: auto; display: flex; flex-direction: column; gap: 14px; }
@keyframes sheet-in { from { transform: translateY(24px); opacity: 0; } to { transform: none; opacity: 1; } }
@media (min-width: 700px) {
  #sheet { margin: auto; border-radius: var(--r-lg); }
}

.stepper { display: flex; align-items: center; gap: 12px; justify-content: center; }
.stepper output { font: 700 2rem/1 var(--font-head); min-width: 3ch; text-align: center; font-variant-numeric: tabular-nums; }
.stepper .btn { width: 52px; padding: 0; }

.macro-row { display: grid; grid-template-columns: repeat(4, 1fr); gap: 8px; text-align: center; }
.macro-row div { background: var(--surface-2); border-radius: var(--r-sm); padding: 8px 4px; }
.macro-row b { display: block; font: 700 1.25rem/1.1 var(--font-head); font-variant-numeric: tabular-nums; }
.macro-row span { font-size: 0.75rem; color: var(--muted); }

/* ---------- Toasts ---------- */

#toasts {
  position: fixed; z-index: 60; left: 12px; right: 12px; bottom: calc(var(--nav-h) + 16px + var(--bottom-inset));
  display: flex; flex-direction: column; align-items: center; gap: 8px; pointer-events: none;
}
.toast {
  pointer-events: auto; display: flex; align-items: center; gap: 12px; max-width: 480px;
  padding: 10px 16px; border-radius: var(--r-md); background: var(--text); color: var(--bg); font-weight: 500; box-shadow: var(--shadow);
  animation: toast-in 200ms ease-out;
}
.toast button { background: transparent; border: 0; color: inherit; font-weight: 700; text-decoration: underline; cursor: pointer; min-height: 44px; padding: 0 4px; }
@keyframes toast-in { from { transform: translateY(8px); opacity: 0; } to { transform: none; opacity: 1; } }

@media (prefers-reduced-motion: reduce) {
  *, *::before, *::after { animation-duration: 1ms !important; transition-duration: 1ms !important; }
}

@media (max-width: 380px) {
  h1 { font-size: 1.875rem; }
  .hero { flex-direction: column; align-items: stretch; }
  .hero .ring { align-self: center; }
}
