:root {
  --bg: #f2f2f7;
  --card: #ffffff;
  --text: #1c1c1e;
  --muted: #64748b; /* slate */
  --line: #e5e5ea;

  /* Modern data palette. Green is no longer the primary "good" colour and red
     is reserved for genuinely destructive actions and errors. */
  --accent: #2563eb;  /* blue — primary / calories */
  --violet: #8b5cf6;  /* carbs */
  --teal: #14b8a6;    /* exercise */
  --slate: #64748b;   /* net / neutral */
  --indigo: #6366f1;  /* weight & trend, positive movement */
  --sky: #38bdf8;     /* trend secondary */
  --amber: #f59e0b;   /* goal lines, warnings, gaps */
  --pink: #ec4899;    /* body fat */
  --cyan: #06b6d4;    /* lean mass */
  --danger: #dc2626;  /* muted red — delete / errors only */

  /* Legacy aliases kept so older rules resolve; both now map to the palette. */
  --green: var(--indigo);
  --red: var(--danger);
  --radius: 16px;
}

@media (prefers-color-scheme: dark) {
  :root {
    --bg: #000000;
    --card: #1c1c1e;
    --text: #f2f2f7;
    --muted: #94a3b8; /* slate-400 for contrast on dark */
    --line: #2c2c2e;
    --accent: #3b82f6;
    --indigo: #818cf8;
  }
}

* { box-sizing: border-box; }

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

body {
  margin: 0;
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Helvetica, Arial, sans-serif;
  background: var(--bg);
  color: var(--text);
  line-height: 1.4;
  padding-bottom: env(safe-area-inset-bottom);
}

.topbar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  padding: 12px 16px;
  padding-top: calc(12px + env(safe-area-inset-top));
  background: var(--card);
  border-bottom: 1px solid var(--line);
  position: sticky;
  top: 0;
  z-index: 10;
}

.brand { font-weight: 700; font-size: 18px; color: var(--text); text-decoration: none; }

.nav { display: flex; align-items: center; gap: 16px; flex-wrap: wrap; justify-content: flex-end; }
.nav a { color: var(--accent); text-decoration: none; font-size: 15px; }
.logout-form { margin: 0; }

/* The nav has a few items now; tighten it up on small screens so it wraps
   cleanly instead of overflowing. */
@media (max-width: 480px) {
  .nav { gap: 10px 14px; }
  .nav a, .nav .linkish { font-size: 14px; }
}

.container {
  max-width: 560px;
  margin: 0 auto;
  padding: 16px;
  display: flex;
  flex-direction: column;
  gap: 16px;
}

.card {
  background: var(--card);
  border-radius: var(--radius);
  padding: 18px;
  box-shadow: 0 1px 2px rgba(0, 0, 0, 0.04);
}

/* Today: the stacked cards live inside this panel, which isn't a flex column
   like .container, so they used to butt right up against each other. Give them
   the same breathing room as the rest of the page plus a few extra px. */
#today-panel { display: flex; flex-direction: column; gap: 21px; }

h2 { font-size: 15px; text-transform: uppercase; letter-spacing: 0.04em; color: var(--muted); margin: 0 0 12px; }

/* Add form */
.add-form { display: flex; flex-direction: column; gap: 12px; margin: 4px 0 0; }

/* Calories + Carbs sit side by side, and wrap to stacked when very narrow. */
.entry-fields { display: flex; flex-wrap: wrap; gap: 10px; }
.field { flex: 1 1 130px; min-width: 0; display: flex; flex-direction: column; gap: 6px; }
.field-label {
  font-size: 13px;
  font-weight: 600;
  text-transform: uppercase;
  letter-spacing: 0.04em;
  color: var(--muted);
}

/* Big, centered numeric fields with large tap targets. */
.add-form input[type="number"] {
  font-size: 40px;
  font-weight: 700;
  text-align: center;
  width: 100%;
  min-height: 72px;
  padding: 10px 8px;
  border: 1px solid var(--line);
  border-radius: 12px;
  background: var(--card);
  color: var(--text);
  -moz-appearance: textfield;
}
.add-form input[type="number"]::-webkit-outer-spin-button,
.add-form input[type="number"]::-webkit-inner-spin-button { -webkit-appearance: none; margin: 0; }

.add-form input.note-input {
  font-size: 17px;
  padding: 12px 14px;
  min-height: 48px;
  border: 1px solid var(--line);
  border-radius: 12px;
  background: var(--card);
  color: var(--text);
  width: 100%;
}

.add-form input:focus { outline: 2px solid var(--accent); outline-offset: -1px; }

button {
  font-size: 17px;
  font-weight: 600;
  color: #fff;
  background: var(--accent);
  border: none;
  border-radius: 12px;
  padding: 14px 18px;
  min-height: 48px;
  cursor: pointer;
}
button:active { opacity: 0.85; }

.hint { text-align: center; color: var(--muted); font-size: 13px; margin: 2px 0 0; }

/* Summary */
.summary .numbers { display: flex; align-items: baseline; gap: 8px; }
.summary .total { font-size: 44px; font-weight: 700; letter-spacing: -0.02em; }
.summary .goal { color: var(--muted); font-size: 17px; }

.bar { height: 12px; background: var(--line); border-radius: 999px; overflow: hidden; margin: 14px 0 10px; }
.bar span { display: block; height: 100%; background: var(--accent); border-radius: 999px; transition: width 0.25s ease; }
.bar.over span { background: var(--amber); }

.status { margin: 0; font-size: 15px; color: var(--muted); }
.status.over { color: var(--amber); }
.status.over strong { color: var(--amber); }
.status.small { font-size: 13px; }
.status.small a { color: var(--accent); text-decoration: none; }
.carbs { margin: 8px 0 0; color: var(--muted); font-size: 14px; }

.sub { margin-top: 14px; }
.bar.small { height: 8px; margin: 0 0 6px; }

/* Keto-Aid */
.keto-head { display: flex; align-items: baseline; justify-content: space-between; }
.keto-head h2 { margin-bottom: 0; }
.keto-count { font-size: 17px; font-weight: 600; }
.electrolytes { margin: 0; }
.entry-list.compact li { padding: 8px 0; }

/* Entry list */
.entries-head { display: flex; align-items: baseline; justify-content: space-between; }
.entries-head h2 { margin-bottom: 0; }
.entry-list { list-style: none; margin: 12px 0 0; padding: 0; }
.entry-list li {
  display: flex;
  align-items: center;
  gap: 12px;
  padding: 10px 0;
  border-bottom: 1px solid var(--line);
}
.entry-list li:last-child { border-bottom: none; }
.entry-list .cal { font-size: 20px; font-weight: 600; min-width: 56px; }
.entry-list .meta { flex: 1; color: var(--muted); font-size: 14px; }
/* Keep edit/delete quiet until you reach for them. */
.entry-list .actions { display: flex; align-items: center; gap: 6px; }
.entry-list .actions a,
.entry-list .actions .linkish {
  color: var(--muted);
  text-decoration: none;
  font-size: 13px;
  padding: 8px 8px;
  min-height: 0;
}
.entry-list .actions .danger { color: var(--muted); }
.entry-list .actions .danger:active { color: var(--danger); }
.empty { color: var(--muted); margin: 12px 0 0; }

.linkish {
  background: none;
  color: var(--accent);
  border: none;
  padding: 6px 4px;
  font-size: 14px;
  font-weight: 500;
  cursor: pointer;
  width: auto;
  min-height: 0;
}
.linkish.danger { color: var(--danger); }
.delete-form { display: inline; margin: 0; }
.delete-form.standalone { margin-top: 18px; }
.undo-form { margin: 0; }

/* Secondary action (Keto-Aid): obvious, large, but clearly not the primary CTA. */
.secondary-button {
  width: 100%;
  margin: 12px 0 10px;
  padding: 15px;
  font-size: 17px;
  color: var(--accent);
  background: transparent;
  border: 1.5px solid var(--accent);
}

/* Stacked forms (settings / edit / login) */
.stacked-form p { margin: 0 0 14px; display: flex; flex-direction: column; gap: 6px; }
.stacked-form label { font-size: 14px; color: var(--muted); }
.stacked-form input,
.stacked-form select {
  font-size: 17px;
  padding: 11px 13px;
  border: 1px solid var(--line);
  border-radius: 12px;
  background: var(--card);
  color: var(--text);
  width: 100%;
}
.stacked-form input[type="checkbox"] { width: auto; }
.stacked-form .helptext { font-size: 12px; color: var(--muted); }
.form-actions { display: flex; align-items: center; gap: 16px; margin-top: 4px; }
.form-actions a { white-space: nowrap; }

/* Stats */
.avg-grid { display: grid; grid-template-columns: repeat(2, 1fr); gap: 12px; }
.avg-cell { background: var(--bg); border-radius: 12px; padding: 14px; text-align: center; }
.avg-value { font-size: 26px; font-weight: 700; }
.avg-label { font-size: 13px; color: var(--muted); margin-top: 2px; }

.history { width: 100%; border-collapse: collapse; font-size: 15px; }
.history th { text-align: left; color: var(--muted); font-weight: 500; font-size: 13px; padding: 6px 4px; }
.history td { padding: 10px 4px; border-top: 1px solid var(--line); }
.history tr.over td { color: var(--amber); }
.badge { font-size: 11px; background: var(--amber); color: #fff; border-radius: 6px; padding: 1px 6px; vertical-align: middle; }

/* Status pills: connected (positive) vs not connected (neutral). Sized a touch
   larger than the inline table .badge so they read as a standalone status. */
.badge.pill { font-size: 12px; padding: 2px 10px; border-radius: 999px; font-weight: 600; }
.badge.ok { background: var(--indigo); }
.badge.off { background: var(--slate); }

/* Source marker on a metrics row (e.g. a Withings-imported measurement). */
.source-badge { font-size: 11px; background: var(--teal); color: #fff; border-radius: 6px; padding: 1px 6px; vertical-align: middle; }

/* Mark-today-complete: kept quiet so it never competes with entry. */
.entries-actions { display: flex; align-items: center; gap: 12px; }
.complete-form { margin: 0; }
.complete-toggle.done { color: var(--indigo); font-weight: 600; }

/* Coverage-aware window stats (Stats page) */
.window-list { display: flex; flex-direction: column; gap: 14px; }
.window-row { background: var(--bg); border-radius: 12px; padding: 14px; }
.window-head { display: flex; align-items: baseline; justify-content: space-between; gap: 8px; }
.window-label { font-size: 15px; font-weight: 600; }
.coverage { font-size: 12px; font-weight: 600; padding: 2px 8px; border-radius: 999px; background: var(--line); color: var(--muted); white-space: nowrap; }
.coverage-high { background: rgba(99, 102, 241, 0.16); color: var(--indigo); }
.coverage-medium { background: rgba(37, 99, 235, 0.14); color: var(--accent); }
.coverage-low { background: rgba(245, 158, 11, 0.18); color: var(--amber); }
.coverage-active { background: rgba(56, 189, 248, 0.18); color: var(--sky); }
.window-averages { display: flex; gap: 18px; margin: 10px 0 6px; flex-wrap: wrap; }
.window-avg { display: flex; flex-direction: column; }
.window-avg-value { font-size: 26px; font-weight: 700; }
.window-avg-label { font-size: 12px; color: var(--muted); }
.window-coverage { margin: 0; }

/* Trends summary cards */
.trend-cards { display: grid; grid-template-columns: repeat(2, 1fr); gap: 12px; }
.trend-card { padding: 16px; }
.trend-card-head { display: flex; align-items: baseline; justify-content: space-between; gap: 6px; }
.trend-card-value { font-size: 30px; font-weight: 700; margin-top: 6px; }
.trend-card-unit { font-size: 12px; color: var(--muted); }
.trend-card-sub { font-size: 13px; color: var(--muted); margin-top: 6px; }
.trend-card-coverage { font-size: 12px; color: var(--muted); margin-top: 8px; }
.chart-placeholder { min-height: 120px; display: flex; align-items: center; justify-content: center; border: 1px dashed var(--line); border-radius: 12px; }

/* Trend weight headline numbers */
.stat-grid { display: grid; grid-template-columns: repeat(2, 1fr); gap: 12px; }
.stat { background: var(--bg); border-radius: 12px; padding: 12px 14px; }
.stat-value { display: block; font-size: 24px; font-weight: 700; }
.stat-value.up { color: var(--amber); }
.stat-value.down { color: var(--indigo); }
.stat-label { font-size: 12px; color: var(--muted); }

/* Chart canvases need an explicit height since the canvas is absolutely sized
   by Chart.js to its container. */
.chart-box { position: relative; height: 240px; margin-top: 8px; }

/* Coach / Reality check */
.coach-head { display: flex; align-items: baseline; justify-content: space-between; gap: 8px; }
.coach-head h2 { margin-bottom: 0; }
.confidence { font-size: 12px; font-weight: 600; padding: 2px 10px; border-radius: 999px; background: var(--line); color: var(--muted); white-space: nowrap; text-transform: capitalize; }
.confidence-high { background: rgba(99, 102, 241, 0.16); color: var(--indigo); }
.confidence-medium { background: rgba(37, 99, 235, 0.14); color: var(--accent); }
.confidence-low { background: rgba(245, 158, 11, 0.18); color: var(--amber); }
.confidence-insufficient { background: var(--line); color: var(--muted); }
.coach-headline { font-size: 16px; margin: 12px 0 16px; }
.coach-cards { display: grid; grid-template-columns: repeat(2, 1fr); gap: 12px; }
.coach-card { background: var(--bg); border-radius: 12px; padding: 14px; }
.coach-card h3 { font-size: 12px; text-transform: uppercase; letter-spacing: 0.04em; color: var(--muted); margin: 0 0 10px; }
.coach-facts { list-style: none; margin: 0; padding: 0; display: flex; flex-direction: column; gap: 6px; }
.coach-facts li { display: flex; justify-content: space-between; gap: 8px; font-size: 14px; }
.coach-facts span { color: var(--muted); }
.coach-reasons { margin: 10px 0 0; padding-left: 18px; color: var(--muted); font-size: 13px; }
.coach-big { font-size: 30px; font-weight: 700; }
.coach-big-unit { font-size: 15px; font-weight: 400; color: var(--muted); }
.coach-rec { margin: 0 0 8px; font-size: 15px; }

@media (max-width: 420px) {
  .coach-cards { grid-template-columns: 1fr; }
}

.messages { list-style: none; margin: 0; padding: 0; display: flex; flex-direction: column; gap: 8px; }
.messages li { background: var(--card); border-radius: 12px; padding: 12px 14px; }

.login-card { max-width: 360px; margin: 8% auto 0; }

/* History day list */
.day-list { list-style: none; margin: 8px 0 0; padding: 0; }
.day-row { padding: 12px 0; border-bottom: 1px solid var(--line); }
.day-row:last-child { border-bottom: none; }
.day-main { display: flex; align-items: center; justify-content: space-between; gap: 10px; }
.day-date { font-weight: 600; color: var(--text); text-decoration: none; font-size: 15px; }
.day-meta { color: var(--muted); font-size: 13px; margin-top: 4px; }
.day-actions { display: flex; align-items: center; gap: 14px; margin-top: 6px; }
.day-actions a { color: var(--accent); text-decoration: none; font-size: 13px; }
.inline-form { display: inline; margin: 0; }

/* Today exercise card: net shown as a secondary breakdown (food stays primary) */
.net-grid { display: flex; gap: 12px; margin: 4px 0 8px; }
.net-cell { flex: 1; background: var(--bg); border-radius: 10px; padding: 10px; text-align: center; }
.net-cell.net-emph { outline: 1.5px solid var(--accent); }
.net-value { display: block; font-size: 22px; font-weight: 700; }
.net-label { font-size: 12px; color: var(--muted); }

/* Streaks (Trends) */
.streak-grid { display: grid; grid-template-columns: repeat(3, 1fr); gap: 12px; }
.streak { background: var(--bg); border-radius: 12px; padding: 12px; text-align: center; }
.streak-value { display: block; font-size: 24px; font-weight: 700; }
.streak-label { font-size: 12px; color: var(--muted); }

/* Compact Today motivation card */
.summary-mini .mini-stats { list-style: none; margin: 0; padding: 0; display: flex; flex-wrap: wrap; gap: 16px; }
.summary-mini .mini-stats li { display: flex; flex-direction: column; }
.summary-mini .mini-stats strong { font-size: 22px; font-weight: 700; }
.summary-mini .mini-stats span { font-size: 12px; color: var(--muted); }
.summary-mini .hint { text-align: left; margin-top: 10px; }
.summary-mini .hint a { color: var(--accent); text-decoration: none; }

@media (max-width: 420px) {
  .streak-grid { grid-template-columns: repeat(2, 1fr); }
}

/* Day status badges */
.state-badge { font-size: 11px; font-weight: 600; padding: 2px 9px; border-radius: 999px; background: var(--line); color: var(--muted); white-space: nowrap; }
.state-complete { background: rgba(99, 102, 241, 0.16); color: var(--indigo); }
.state-logged { background: rgba(37, 99, 235, 0.14); color: var(--accent); }
.state-partial { background: rgba(245, 158, 11, 0.18); color: var(--amber); }
.state-cheat-day { background: rgba(245, 158, 11, 0.22); color: var(--amber); }
.state-skipped { background: var(--line); color: var(--muted); }
.state-missing { background: rgba(245, 158, 11, 0.14); color: var(--amber); }
/* Active day (today) — neutral sky, never the warning amber of a missing day */
.state-today-so-far { background: rgba(56, 189, 248, 0.18); color: var(--sky); }
.state-no-entries-yet-today { background: rgba(100, 116, 139, 0.16); color: var(--slate); }

/* Day detail */
.day-head { display: flex; align-items: baseline; justify-content: space-between; gap: 8px; }
.day-head h2 { margin-bottom: 0; }
.subhead { font-size: 13px; text-transform: uppercase; letter-spacing: 0.04em; color: var(--muted); margin: 18px 0 10px; }
