:root {
  color-scheme: light dark;
  --bg: #f5f6f4;
  --surface: #ffffff;
  --surface-2: #eceee9;
  --text: #171a1c;
  --muted: #5d6468;
  --line: #dfe2dc;
  --accent: #127a5b;
  --accent-text: #ffffff;
  --protein: #2b6cb0;
  --carbs: #b7791f;
  --fat: #a23f7d;
  --warn-bg: #fdf1d8;
  --warn-text: #6f4a00;
  --danger: #b3261e;
}

@media (prefers-color-scheme: dark) {
  :root {
    --bg: #14171a;
    --surface: #1e2226;
    --surface-2: #2a2f34;
    --text: #edf0ee;
    --muted: #a3abae;
    --line: #333a3f;
    --accent: #3cc497;
    --accent-text: #08261c;
    --protein: #6aa9ec;
    --carbs: #e0ad52;
    --fat: #dd80ba;
    --warn-bg: #3a2f14;
    --warn-text: #f4d98e;
    --danger: #f2877f;
  }
}

* { box-sizing: border-box; }
[x-cloak] { display: none !important; }

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

body {
  margin: 0;
  background: var(--bg);
  color: var(--text);
  font: 16px/1.4 system-ui, -apple-system, 'Segoe UI', Roboto, sans-serif;
  -webkit-tap-highlight-color: transparent;
}

h1 { font-size: 1.6rem; line-height: 1.2; margin: 8px 0; }
h2 { font-size: 1.05rem; margin: 0; }
h3 { font-size: .9rem; margin: 22px 0 8px; }
p { margin: 8px 0; }
button, input, select { font: inherit; color: inherit; }

.visually-hidden { position: absolute; width: 1px; height: 1px; opacity: 0; pointer-events: none; }
.muted { color: var(--muted); font-size: .9rem; }
.hint { color: var(--muted); font-size: .82rem; margin: 6px 0 10px; }
.center { text-align: center; }
.error { color: var(--danger); font-size: .9rem; }
.notice {
  background: var(--warn-bg);
  color: var(--warn-text);
  padding: 10px 12px;
  border-radius: 10px;
  font-size: .88rem;
}
.load-error { padding: 24px 16px; color: var(--danger); }

.screen {
  max-width: 560px;
  margin: 0 auto;
  padding: calc(env(safe-area-inset-top) + 12px) 16px calc(env(safe-area-inset-bottom) + 24px);
}
.screen.with-nav { padding-bottom: calc(env(safe-area-inset-bottom) + 84px); }
.step-label { color: var(--accent); font-size: .82rem; font-weight: 600; margin: 8px 0 0; }

/* Cards */
.card {
  background: var(--surface);
  border: 1px solid var(--line);
  border-radius: 14px;
  padding: 14px;
  margin: 12px 0;
}
.card > h2 + .muted { margin-top: 6px; }
.card.list { padding: 0 14px; margin-top: 0; }

/* Date bar */
.date-bar { display: flex; align-items: center; justify-content: space-between; gap: 8px; }
.date-title { position: relative; display: flex; flex-direction: column; align-items: center; flex: 1; min-width: 0; }
.date-title strong { font-size: 1.15rem; }
.date-title span { color: var(--muted); font-size: .82rem; }
.date-input { position: absolute; inset: 0; width: 100%; height: 100%; opacity: 0; }
.today-link { display: block; margin: 2px auto 0; }

.icon-button {
  width: 44px;
  height: 44px;
  flex: none;
  border: 0;
  border-radius: 50%;
  background: transparent;
  font-size: 1.5rem;
  line-height: 1;
  cursor: pointer;
}
.icon-button:active { background: var(--surface-2); }

.banner {
  display: block;
  width: 100%;
  text-align: left;
  border: 0;
  border-radius: 10px;
  padding: 10px 12px;
  margin-top: 10px;
  font-size: .88rem;
  cursor: pointer;
}
.banner.warn { background: var(--warn-bg); color: var(--warn-text); }

/* Summary */
.summary-top { display: flex; justify-content: space-between; align-items: flex-end; gap: 12px; }
.big-number { font-size: 2.6rem; font-weight: 700; line-height: 1; font-variant-numeric: tabular-nums; }
.big-number.over { color: var(--carbs); }
.summary-side { text-align: right; font-size: .85rem; color: var(--muted); font-variant-numeric: tabular-nums; }
.summary-side strong { color: var(--text); }

.bar { height: 10px; border-radius: 6px; background: var(--surface-2); overflow: hidden; margin-top: 12px; }
.bar.small { height: 6px; margin-top: 5px; }
.fill { display: block; height: 100%; border-radius: inherit; background: var(--accent); }
.fill.over { background: var(--carbs); }
.fill.protein { background: var(--protein); }
.fill.carbs { background: var(--carbs); }
.fill.fat { background: var(--fat); }

.macros { display: grid; grid-template-columns: repeat(3, minmax(0, 1fr)); gap: 12px; margin-top: 14px; }
.macro-label { color: var(--muted); font-size: .78rem; }
.macro-value { font-size: .92rem; font-variant-numeric: tabular-nums; white-space: nowrap; }
.macro-value span { color: var(--muted); font-size: .78rem; }

/* Meals and rows */
.meal { padding-bottom: 4px; }
.meal-header { display: flex; justify-content: space-between; align-items: baseline; padding-bottom: 6px; }

.row {
  display: flex;
  width: 100%;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  min-height: 56px;
  padding: 9px 0;
  background: none;
  border: 0;
  border-top: 1px solid var(--line);
  text-align: left;
  cursor: pointer;
}
.card.list .row:first-child { border-top: 0; }
.row-main { display: flex; flex-direction: column; min-width: 0; overflow-wrap: anywhere; }
.row-main strong { font-weight: 550; }
.row-main .muted { font-size: .82rem; }
.row-value { font-variant-numeric: tabular-nums; flex: none; }

.add-button {
  display: block;
  width: 100%;
  min-height: 46px;
  padding: 0;
  background: none;
  border: 0;
  border-top: 1px solid var(--line);
  color: var(--accent);
  font-weight: 600;
  text-align: left;
  cursor: pointer;
}

/* Buttons */
.button {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  min-height: 48px;
  padding: 10px 16px;
  border-radius: 12px;
  border: 1px solid var(--line);
  background: var(--surface);
  font-weight: 600;
  text-align: center;
  cursor: pointer;
}
.button:active { background: var(--surface-2); }
.button.primary { background: var(--accent); border-color: var(--accent); color: var(--accent-text); }
.button.primary:disabled { opacity: .5; }
.button.danger { color: var(--danger); }
.button.wide { display: flex; width: 100%; margin-top: 10px; }
.button-row { display: flex; gap: 10px; margin-top: 16px; }
.button-row .button { flex: 1; }
.link {
  background: none;
  border: 0;
  padding: 8px 0;
  color: var(--accent);
  font-weight: 600;
  font-size: .92rem;
  cursor: pointer;
}
a.link { display: inline-block; }
.center-link { display: block; margin: 14px auto 0; }

/* Forms */
.field { display: block; margin-top: 14px; font-size: .86rem; font-weight: 600; min-width: 0; }
.field input, .field select {
  display: block;
  width: 100%;
  min-height: 48px;
  margin-top: 5px;
  padding: 10px 12px;
  border: 1px solid var(--line);
  border-radius: 10px;
  background: var(--surface);
  font-weight: 400;
  font-size: 1rem;
}
.field-row { display: flex; gap: 10px; }
.field-row > .field { flex: 1; }
.field-row > .field.qty { flex: 0 0 34%; }
.field-row > .field.grow { flex: 1; }
.target-grid { display: grid; grid-template-columns: repeat(2, minmax(0, 1fr)); gap: 0 10px; }

.segmented { display: flex; gap: 4px; padding: 4px; background: var(--surface-2); border-radius: 12px; margin-top: 8px; }
.segmented button {
  flex: 1;
  min-height: 42px;
  border: 0;
  border-radius: 9px;
  background: transparent;
  font-weight: 600;
  font-size: .92rem;
  cursor: pointer;
}
.segmented button.on { background: var(--accent); color: var(--accent-text); }

.choice-list { display: grid; gap: 8px; }
.choice {
  display: flex;
  flex-direction: column;
  gap: 2px;
  padding: 11px 13px;
  border: 1px solid var(--line);
  border-radius: 12px;
  background: var(--surface);
  text-align: left;
  cursor: pointer;
}
.choice span { color: var(--muted); font-size: .84rem; }
.choice.on { border-color: var(--accent); box-shadow: inset 0 0 0 1px var(--accent); }

.setup .button.primary.wide { margin-top: 22px; }
.restore-offer { margin-top: 30px; padding-top: 16px; border-top: 1px solid var(--line); }

/* Sheets */
.sheet-header { display: flex; align-items: center; gap: 6px; margin: 0 0 8px -10px; }
.search { display: flex; gap: 8px; }
.search input {
  flex: 1;
  min-width: 0;
  min-height: 48px;
  padding: 10px 12px;
  border: 1px solid var(--line);
  border-radius: 12px;
  background: var(--surface);
}
.food-title { overflow-wrap: anywhere; }
.totals { display: grid; grid-template-columns: repeat(4, minmax(0, 1fr)); gap: 6px; text-align: center; margin-top: 16px; }
.totals div { display: flex; flex-direction: column; }
.totals strong { font-variant-numeric: tabular-nums; }
.totals span { color: var(--muted); font-size: .76rem; }

/* Workout tab */
.eyebrow { color: var(--accent); font-size: .8rem; font-weight: 700; margin: 0 0 2px; }
.card.highlight { border-color: var(--accent); }
.card.list.spaced { margin-top: 12px; }
.week-strip { display: grid; grid-template-columns: repeat(7, minmax(0, 1fr)); gap: 4px; margin-top: 10px; }
.week-day {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 1px;
  padding: 6px 0;
  border-radius: 10px;
  background: var(--surface);
  border: 1px solid var(--line);
  color: var(--muted);
  font-size: .72rem;
}
.week-day strong { font-size: .95rem; color: var(--muted); font-weight: 600; }
.week-day.planned strong { color: var(--text); font-weight: 700; }
.week-day.done strong { color: var(--accent); }
.week-day.today { border-color: var(--accent); color: var(--text); box-shadow: inset 0 0 0 1px var(--accent); }
.plan-line { display: flex; justify-content: space-between; gap: 12px; padding: 6px 0; border-top: 1px solid var(--line); font-size: .94rem; }
.plan-line:first-of-type { margin-top: 8px; }
.other-workouts { margin-top: 14px; }
.other-workouts .hint { margin-bottom: 6px; }
.button-row.tight { margin-top: 0; }
.button:disabled { opacity: .45; }

/* Workout session */
.session-screen { padding-bottom: calc(env(safe-area-inset-bottom) + 96px); }
.exercise-header { display: flex; justify-content: space-between; align-items: center; gap: 12px; }
.link.small { font-size: .85rem; padding: 6px 0; min-height: 32px; }
.target { font-size: .88rem; font-weight: 600; margin: 2px 0 6px; }
.cues { margin: 0 0 6px; padding-left: 18px; color: var(--muted); font-size: .84rem; }
.small-text { font-size: .82rem; margin: 4px 0; }
.advice { border-radius: 10px; padding: 8px 10px; font-size: .84rem; margin: 8px 0; background: var(--surface-2); }
.advice.up { background: var(--accent); color: var(--accent-text); font-weight: 600; }

.set-grid { display: grid; grid-template-columns: 24px minmax(0, 1fr) minmax(0, 1fr) 58px 46px; gap: 6px; align-items: center; margin-top: 6px; }
.set-head { color: var(--muted); font-size: .74rem; font-weight: 600; text-align: center; margin-top: 10px; }
.set-number { text-align: center; color: var(--muted); font-size: .9rem; font-variant-numeric: tabular-nums; }
.set-grid input, .set-grid select {
  width: 100%;
  min-height: 46px;
  padding: 6px;
  border: 1px solid var(--line);
  border-radius: 10px;
  background: var(--bg);
  text-align: center;
  font-size: 1rem;
  font-variant-numeric: tabular-nums;
}
.set-grid select { padding: 6px 2px; }
.check {
  width: 46px;
  height: 46px;
  border: 1px solid var(--line);
  border-radius: 12px;
  background: var(--surface-2);
  color: var(--muted);
  font-size: 1.2rem;
  font-weight: 700;
  cursor: pointer;
}
.set-grid.done .check { background: var(--accent); border-color: var(--accent); color: var(--accent-text); }
.set-grid.done input, .set-grid.done select { border-color: var(--accent); }
.exercise-actions { display: flex; flex-wrap: wrap; gap: 4px 18px; margin-top: 6px; }

.rest-bar {
  position: fixed;
  left: 0;
  right: 0;
  bottom: 0;
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 10px 16px calc(env(safe-area-inset-bottom) + 10px);
  background: var(--surface);
  border-top: 2px solid var(--accent);
  z-index: 10;
}
.rest-bar.over { background: var(--accent); color: var(--accent-text); }
.rest-bar .button { color: var(--text); }
.rest-time { flex: 1; display: flex; align-items: baseline; gap: 8px; }
.rest-time strong { font-size: 1.6rem; font-variant-numeric: tabular-nums; }

/* Program editor */
.day-picker { display: grid; grid-template-columns: repeat(7, minmax(0, 1fr)); gap: 4px; margin-top: 10px; }
.day-picker button {
  min-height: 44px;
  padding: 0;
  border: 1px solid var(--line);
  border-radius: 10px;
  background: transparent;
  font-size: .78rem;
  font-weight: 600;
  cursor: pointer;
}
.day-picker button.on { background: var(--accent); border-color: var(--accent); color: var(--accent-text); }
.program-card { padding-bottom: 4px; }
.program-card h2 { padding-bottom: 8px; }

/* Bottom navigation */
.tabbar {
  position: fixed;
  left: 0;
  right: 0;
  bottom: 0;
  display: flex;
  background: var(--surface);
  border-top: 1px solid var(--line);
  padding-bottom: env(safe-area-inset-bottom);
  z-index: 5;
}
.tabbar button {
  flex: 1;
  min-height: 58px;
  border: 0;
  background: none;
  color: var(--muted);
  font-weight: 600;
  font-size: .9rem;
  cursor: pointer;
}
.tabbar button.on { color: var(--accent); box-shadow: inset 0 2px 0 var(--accent); }

/* Overlays */
.modal-backdrop {
  position: fixed;
  inset: 0;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 16px;
  background: rgba(0, 0, 0, .5);
  z-index: 20;
}
.modal { width: 100%; max-width: 420px; padding: 18px; border-radius: 16px; background: var(--surface); }

.toast {
  position: fixed;
  left: 16px;
  right: 16px;
  bottom: calc(env(safe-area-inset-bottom) + 72px);
  max-width: 528px;
  margin: 0 auto;
  display: flex;
  justify-content: space-between;
  align-items: center;
  gap: 12px;
  min-height: 46px;
  padding: 8px 14px;
  border-radius: 12px;
  background: var(--text);
  color: var(--bg);
  font-size: .9rem;
  z-index: 15;
}
.toast .link { color: var(--bg); text-decoration: underline; }

.update-pill {
  position: fixed;
  left: 50%;
  top: calc(env(safe-area-inset-top) + 8px);
  transform: translateX(-50%);
  padding: 9px 16px;
  border: 0;
  border-radius: 20px;
  background: var(--accent);
  color: var(--accent-text);
  font-size: .85rem;
  font-weight: 600;
  white-space: nowrap;
  cursor: pointer;
  z-index: 25;
}
