/* ---------- Design tokens ---------- */
:root {
  color-scheme: light dark;

  --bg: #f6f7f9;
  --surface: #ffffff;
  --text: #111827;
  --muted: #6b7280;
  --border: #e5e7eb;
  --accent: #ea580c;
  --accent-text: #ffffff;
  --danger: #dc2626;

  --radius: 12px;
  --space-1: 4px;
  --space-2: 8px;
  --space-3: 12px;
  --space-4: 16px;
  --space-6: 24px;

  --header-h: 56px;
  --nav-h: 64px;
  --tap: 44px; /* minimum touch target */

  font-family: system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
  font-size: 16px;
  line-height: 1.5;
}

@media (prefers-color-scheme: dark) {
  :root {
    --bg: #0b0f19;
    --surface: #111827;
    --text: #f3f4f6;
    --muted: #9ca3af;
    --border: #1f2937;
    --accent: #f97316;
    --accent-text: #111827;
    --danger: #f87171;
  }
}

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

/* Component display rules (e.g. display:flex) would otherwise beat [hidden]. */
[hidden] { display: none !important; }

body {
  margin: 0;
  min-height: 100dvh;
  background: var(--bg);
  color: var(--text);
  -webkit-tap-highlight-color: transparent;
}

h1, h2, h3 { line-height: 1.2; margin: 0 0 var(--space-3); }
p { margin: 0 0 var(--space-3); }
a { color: var(--accent); }

/* ---------- App shell ---------- */
.app-header {
  position: sticky;
  top: 0;
  z-index: 10;
  display: flex;
  align-items: center;
  min-height: var(--header-h);
  padding: env(safe-area-inset-top) var(--space-4) 0;
  background: var(--surface);
  border-bottom: 1px solid var(--border);
}
.app-header h1 { font-size: 1.25rem; margin: 0; }

main {
  max-width: 640px;
  margin: 0 auto;
  padding: var(--space-4);
  /* keep content clear of the fixed bottom nav */
  padding-bottom: calc(var(--nav-h) + env(safe-area-inset-bottom) + var(--space-4));
}
main:focus { outline: none; }

.app-nav {
  position: fixed;
  inset: auto 0 0 0;
  z-index: 10;
  display: flex;
  height: calc(var(--nav-h) + env(safe-area-inset-bottom));
  padding-bottom: env(safe-area-inset-bottom);
  background: var(--surface);
  border-top: 1px solid var(--border);
}
.app-nav a {
  flex: 1;
  display: grid;
  place-items: center;
  color: var(--muted);
  text-decoration: none;
  font-weight: 600;
}
.app-nav a[aria-current="page"] { color: var(--accent); }

/* ---------- Components ---------- */
.card {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: var(--space-4);
  margin-bottom: var(--space-4);
}

.muted { color: var(--muted); }

.btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  min-height: var(--tap);
  padding: 0 var(--space-4);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  background: var(--surface);
  color: var(--text);
  font: inherit;
  font-weight: 600;
  cursor: pointer;
}
.btn-primary {
  background: var(--accent);
  border-color: var(--accent);
  color: var(--accent-text);
}

.btn-row { display: flex; flex-wrap: wrap; gap: var(--space-2); }

.banner {
  display: flex;
  align-items: center;
  gap: var(--space-2);
  padding: var(--space-3) var(--space-4);
  background: var(--accent);
  color: var(--accent-text);
}
.banner p { margin: 0; flex: 1; }
.banner-close {
  min-width: var(--tap);
  min-height: var(--tap);
  border: 0;
  background: transparent;
  color: inherit;
  font-size: 1.5rem;
  cursor: pointer;
}

.status { min-height: 1.5em; margin-top: var(--space-3); }
.status:empty { display: none; }
.status.error { color: var(--danger); }

.btn:disabled { opacity: 0.5; cursor: default; }
.btn-small { min-height: 36px; padding: 0 var(--space-3); font-size: 0.875rem; text-decoration: none; }
.btn-block { display: flex; width: 100%; }
.btn-danger { color: var(--danger); }
a.btn { text-decoration: none; }

.visually-hidden {
  position: absolute !important;
  width: 1px;
  height: 1px;
  overflow: hidden;
  clip: rect(0 0 0 0);
  white-space: nowrap;
}

.meta { display: block; font-size: 0.8125rem; color: var(--muted); }

.badge {
  display: inline-block;
  margin-left: var(--space-2);
  padding: 0 var(--space-2);
  border-radius: 999px;
  background: var(--accent);
  color: var(--accent-text);
  font-size: 0.75rem;
  font-weight: 700;
  vertical-align: middle;
}

/* Category color: set --cat on an element to tint it. */
[style*="--cat"] { --cat-soft: color-mix(in srgb, var(--cat) 22%, transparent); }

/* ---------- Header ---------- */
.app-header { justify-content: space-between; gap: var(--space-3); }
.header-link {
  display: grid;
  place-items: center;
  min-height: var(--tap);
  font-weight: 600;
  text-decoration: none;
}

/* ---------- Lists & cards ---------- */
.list { list-style: none; margin: 0; padding: 0; }

.page-head {
  display: flex;
  align-items: center;
  justify-content: space-between;
  flex-wrap: wrap;
  gap: var(--space-2);
  margin-bottom: var(--space-4);
}
.page-head h2 { margin: 0; min-width: 0; overflow-wrap: anywhere; }

.back-link {
  display: inline-block;
  margin-bottom: var(--space-2);
  padding: var(--space-2) 0;
  text-decoration: none;
  font-weight: 600;
}

.card-button {
  display: grid;
  gap: var(--space-1);
  width: 100%;
  text-align: left;
  font: inherit;
  color: inherit;
  cursor: pointer;
}
.card-button:active, .category-row:active, .today-card:active { transform: scale(0.99); }
.card-title { font-weight: 700; font-size: 1.0625rem; }

.today-card {
  display: grid;
  gap: var(--space-1);
  color: inherit;
  text-decoration: none;
  border-color: var(--accent);
}
.today-card strong { font-size: 1.375rem; overflow-wrap: anywhere; }

.category-row {
  display: grid;
  gap: var(--space-1);
  color: inherit;
  text-decoration: none;
  border-left: 6px solid var(--cat);
}
.category-name { font-weight: 700; overflow-wrap: anywhere; }
.page-head .category-name { border-bottom: 4px solid var(--cat); }

/* ---------- Forms ---------- */
input, select {
  width: 100%;
  min-height: var(--tap);
  padding: 0 var(--space-3);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  background: var(--bg);
  color: var(--text);
  font: inherit;
}
input:focus-visible, select:focus-visible, .btn:focus-visible, a:focus-visible {
  outline: 2px solid var(--accent);
  outline-offset: 2px;
}

.field { display: grid; gap: var(--space-1); margin-bottom: var(--space-3); font-weight: 600; }
.field span { font-size: 0.875rem; }
.field-row { display: flex; gap: var(--space-2); }
.field-row .field { flex: 1; min-width: 0; }

.inline-form { display: flex; gap: var(--space-2); margin-top: var(--space-2); }
.inline-form input { flex: 1; min-width: 0; }

.segmented {
  display: flex;
  margin: 0 0 var(--space-3);
  padding: 0;
  border: 0;
}
.segmented legend { margin-bottom: var(--space-1); font-size: 0.875rem; font-weight: 600; }
.segmented label {
  flex: 1;
  display: grid;
  place-items: center;
  min-height: var(--tap);
  border: 1px solid var(--border);
  background: var(--bg);
  font-weight: 600;
  cursor: pointer;
}
.segmented label:first-of-type { border-radius: var(--radius) 0 0 var(--radius); }
.segmented label:last-of-type { border-radius: 0 var(--radius) var(--radius) 0; border-left: 0; }
.segmented input { position: absolute; opacity: 0; width: 1px; height: 1px; }
.segmented label:has(input:checked) { background: var(--accent); border-color: var(--accent); color: var(--accent-text); }
.segmented label:has(input:focus-visible) { outline: 2px solid var(--accent); outline-offset: 2px; }

/* ---------- Exercises ---------- */
.exercise-grid {
  list-style: none;
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(140px, 1fr));
  gap: var(--space-3);
  margin: 0 0 var(--space-4);
  padding: 0;
}
.exercise {
  display: grid;
  width: 100%;
  padding: 0;
  overflow: hidden;
  border: 1px solid var(--border);
  border-radius: var(--radius);
  background: var(--surface);
  color: inherit;
  font: inherit;
  text-align: left;
  cursor: pointer;
}
.thumb {
  display: grid;
  place-items: center;
  aspect-ratio: 1;
  background: var(--border);
  color: var(--muted);
  font-size: 3rem;
  font-weight: 700;
}
.thumb img { width: 100%; height: 100%; object-fit: cover; }
.exercise-name { padding: var(--space-2) var(--space-3); font-weight: 600; overflow-wrap: anywhere; }

.sheet {
  width: min(100% - 2 * var(--space-4), 480px);
  max-height: calc(100dvh - 2 * var(--space-4));
  padding: var(--space-4);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  background: var(--surface);
  color: var(--text);
}
.sheet::backdrop { background: rgb(0 0 0 / 0.55); }
.exercise-form .btn-row { justify-content: flex-end; margin-bottom: var(--space-3); }

.photo-picker {
  display: grid;
  place-items: center;
  aspect-ratio: 4 / 3;
  margin-bottom: var(--space-3);
  overflow: hidden;
  border: 2px dashed var(--border);
  border-radius: var(--radius);
  color: var(--muted);
  font-weight: 600;
  cursor: pointer;
  position: relative;
}
.photo-picker img { position: absolute; inset: 0; width: 100%; height: 100%; object-fit: cover; }
.photo-picker img:not([hidden]) + span {
  position: absolute;
  bottom: var(--space-2);
  padding: var(--space-1) var(--space-3);
  border-radius: 999px;
  background: rgb(0 0 0 / 0.6);
  color: #fff;
  font-size: 0.8125rem;
}

/* Bottom sheet: slides up from the bottom edge on phones, centered on wider screens. */
.sheet-bottom {
  width: 100%;
  max-width: min(100%, 560px);
  max-height: 85dvh;
  margin: auto auto 0;
  padding: 0 0 env(safe-area-inset-bottom);
  border-bottom: 0;
  border-radius: 16px 16px 0 0;
}
.sheet-bottom[open] { animation: sheet-up 0.22s ease-out; }
@keyframes sheet-up { from { transform: translateY(100%); } }
@media (prefers-reduced-motion: reduce) { .sheet-bottom[open] { animation: none; } }
@media (min-width: 600px) {
  .sheet-bottom { margin: auto; border-bottom: 1px solid var(--border); border-radius: var(--radius); }
}
.sheet-body { padding: var(--space-4); }
.sheet-body .page-head { margin-bottom: var(--space-2); }

/* ---------- Calendar ---------- */
.plan-prompt { border-color: var(--accent); }
.today-line { margin-bottom: var(--space-3); }

.legend { list-style: none; display: grid; gap: var(--space-2); margin: 0; padding: 0; }
.legend li { display: grid; grid-template-columns: auto 1fr auto; align-items: center; gap: var(--space-2); }
.legend .swatch { width: 14px; height: 14px; border-radius: 4px; background: var(--cat); }

.week-list { list-style: none; margin: 0; padding: 0; }
.week-row {
  display: grid;
  grid-template-columns: 7.5rem 1fr;
  align-items: center;
  gap: var(--space-2);
  padding: var(--space-1) var(--space-2);
  margin: 0 calc(-1 * var(--space-2));
  border-radius: var(--radius);
}
.week-row label { font-weight: 600; }
.week-row.is-today { background: color-mix(in srgb, var(--accent) 15%, transparent); }

.month-head { display: flex; align-items: center; justify-content: space-between; margin-bottom: var(--space-3); }
.month-head h3 { margin: 0; }

.month-grid {
  list-style: none;
  display: grid;
  grid-template-columns: repeat(7, minmax(0, 1fr));
  gap: var(--space-1);
  margin: 0;
  padding: 0;
}
.weekday { text-align: center; font-size: 0.75rem; font-weight: 700; color: var(--muted); }
/* Less side padding so the 7 columns get as much of a phone's width as possible. */
.month-card { padding: var(--space-3) var(--space-1); margin-inline: calc(-1 * var(--space-2)); }
.month-card .month-head { padding: 0 var(--space-2); }

.day {
  display: grid;
  align-content: start;
  gap: 2px;
  min-height: 4.75rem;
  padding: var(--space-1) 3px;
  border-radius: 8px;
  background: var(--cat-soft, transparent);
  border-top: 3px solid var(--cat, transparent);
  font-size: 0.875rem;
}
.day.blank { background: none; border: 0; }
.day.rest { color: var(--muted); border-top-color: var(--border); }
.day-num { font-weight: 700; }
.day-cat {
  overflow: hidden;
  font-size: 0.75rem;
  font-weight: 600;
  line-height: 1.2;
  display: -webkit-box;
  -webkit-line-clamp: 3;
  -webkit-box-orient: vertical;
  /* Wrap between words; only split (with a hyphen where possible) a word too long for the cell. */
  overflow-wrap: break-word;
  -webkit-hyphens: auto;
  hyphens: auto;
}
.day.is-today { outline: 2px solid var(--accent); outline-offset: -2px; }
.day.is-today .day-num { color: var(--accent); }

/* ---------- BMR ---------- */
.stat-row { display: grid; grid-template-columns: 1fr 1fr; gap: var(--space-3); margin-bottom: var(--space-4); }
.stat { display: grid; gap: 2px; }
.stat strong { font-size: 1.75rem; line-height: 1.1; }

.goal-table { width: 100%; border-collapse: collapse; margin-bottom: var(--space-4); }
.goal-table caption { text-align: left; font-weight: 700; font-size: 1.0625rem; margin-bottom: var(--space-1); }
.goal-table th, .goal-table td { padding: var(--space-2) 0; border-bottom: 1px solid var(--border); text-align: left; }
.goal-table thead th { font-size: 0.75rem; color: var(--muted); font-weight: 600; }
.goal-table thead th:not(:first-child) { text-align: right; }
.goal-table td { text-align: right; font-variant-numeric: tabular-nums; font-weight: 600; white-space: nowrap; }
.goal-table td.muted { width: 3.5rem; font-weight: 400; }
.goal-table tbody th { font-weight: 600; }
.goal-table tr.is-low td:first-of-type { color: var(--danger); }
