:root {
  --ground: #EDF0F2;
  --panel: #ffffff;
  --ink: #1B2A33;
  --muted: #5D6E78;
  --rule: #C3CDD4;
  --hair: #E6EAED;
  --green: #2E7D5B;
  --red: #B4443A;
  --grey: #8A9BA5;
  --protein: #33456B;
  --carbs: #6E93B8;
  --fat: #C9A227;
  --fibre: #8A9BA5;
}

* { box-sizing: border-box; }

body {
  margin: 0;
  background: var(--ground);
  color: var(--ink);
  font-family: "Inter", "Helvetica Neue", Helvetica, Arial, sans-serif;
  font-size: 14px;
  line-height: 1.5;
  -webkit-font-smoothing: antialiased;
}

.serif { font-family: "Iowan Old Style", "Palatino Linotype", Palatino, Georgia, serif; }

h1 { font-size: 20px; font-weight: 600; margin: 0; letter-spacing: -0.01em; }
h2 { font-size: 15px; font-weight: 600; margin: 0 0 4px; }
.sub { color: var(--muted); margin: 2px 0 0; font-size: 13px; max-width: 68ch; }

.wrap { padding: 28px 20px 56px; }
.inner { max-width: 980px; margin: 0 auto; }

.head { display: flex; align-items: flex-start; justify-content: space-between; gap: 16px; flex-wrap: wrap; }
.head-actions { display: flex; gap: 8px; }

.panel { background: var(--panel); border: 1px solid var(--rule); padding: 20px; margin-top: 18px; }

/* Readout -------------------------------------------------------------- */
.readout { display: flex; align-items: baseline; gap: 26px; flex-wrap: wrap; }
.big { font-size: 46px; line-height: 1; font-weight: 400; letter-spacing: -0.02em; }
.big small { font-size: 18px; margin-left: 4px; color: var(--muted); }
.stat { display: flex; flex-direction: column; }
.stat b { font-size: 17px; font-weight: 500; }
.pct-delta { font-size: 12.5px; font-weight: 500; margin-left: 7px; }
.pct-delta.up { color: #2E7D5B; }
.pct-delta.down { color: #B4443A; }
.stat i { font-style: normal; font-size: 12px; color: var(--muted); }

.chart { margin-top: 14px; }
.chart svg { display: block; width: 100%; height: auto; }
.donut { max-width: 260px; }

.chart-wrap { position: relative; }
.marker { cursor: pointer; }
.marker circle:nth-child(2) { transition: r 120ms ease; }
.marker:hover circle:nth-child(2), .marker:focus-visible circle:nth-child(2) { r: 10; }
.marker:focus { outline: none; }
.marker:focus-visible circle:nth-child(2) { stroke: var(--protein); }

.marker-popup {
  position: absolute;
  transform: translate(-50%, calc(-100% - 18px));
  background: var(--panel);
  border: 1px solid var(--rule);
  box-shadow: 0 4px 14px rgba(27, 42, 51, 0.14);
  padding: 9px 12px;
  font-size: 13px;
  display: flex;
  flex-direction: column;
  gap: 2px;
  white-space: nowrap;
  pointer-events: none;
  z-index: 2;
}
.marker-popup[hidden] { display: none; }
.marker-popup strong { font-weight: 600; }
.marker-popup .popup-training { color: var(--muted); }
.marker-popup .popup-note { color: var(--muted); font-style: italic; white-space: normal; max-width: 30ch; }
.marker-popup::after {
  content: ""; position: absolute; left: 50%; bottom: -6px; margin-left: -6px;
  width: 11px; height: 11px; background: var(--panel);
  border-right: 1px solid var(--rule); border-bottom: 1px solid var(--rule);
  transform: rotate(45deg);
}

.legend { display: flex; gap: 18px; font-size: 12px; color: var(--muted); margin-top: 10px; flex-wrap: wrap; }
.swatch { width: 9px; height: 9px; border-radius: 50%; display: inline-block; margin-right: 6px; vertical-align: 1px; }

.proj { display: flex; gap: 30px; flex-wrap: wrap; margin-top: 16px; }
.note { font-size: 12.5px; color: var(--muted); margin: 12px 0 0; max-width: 72ch; }
.foot { display: flex; justify-content: space-between; gap: 12px; max-width: none; }
.status { color: var(--muted); font-size: 12px; }

/* Tables --------------------------------------------------------------- */
.scroll { overflow-x: auto; margin-top: 16px; }
table { width: 100%; border-collapse: collapse; min-width: 560px; }
#food-table { min-width: 940px; }
th {
  text-align: left; font-weight: 500; font-size: 12px; color: var(--muted);
  padding: 0 8px 7px 0; border-bottom: 1px solid var(--rule); white-space: nowrap;
}
th.n, td.n { text-align: right; }
td { padding: 5px 8px 5px 0; border-bottom: 1px solid var(--hair); vertical-align: middle; }
tfoot td { font-weight: 600; border-bottom: none; border-top: 1px solid var(--rule); padding-top: 9px; }
.col-calc { color: var(--ink); background: #F7F9FA; }
td.col-calc { font-variant-numeric: tabular-nums; }

/* Controls ------------------------------------------------------------- */
input, select {
  font: inherit; color: inherit; background: #F7F9FA;
  border: 1px solid var(--rule); padding: 5px 7px; width: 100%;
}
input.n { text-align: right; font-variant-numeric: tabular-nums; }
input.bare, select.bare { background: transparent; border: 1px solid transparent; padding: 4px 6px; }
select.bare { min-width: 52px; color: var(--ink); appearance: auto; }
input.bare:hover, select.bare:hover { border-color: var(--rule); background: #F7F9FA; }
input:focus, select:focus, button:focus-visible { outline: 2px solid var(--protein); outline-offset: 1px; }

.btn { font: inherit; background: var(--ink); color: #fff; border: none; padding: 8px 15px; cursor: pointer; }
.btn:hover { background: #2B3E49; }
.btn.ghost { background: transparent; color: var(--muted); border: 1px solid var(--rule); }
.btn.ghost:hover { background: #F0F3F5; color: var(--ink); }
.x { background: none; border: none; color: #9AA8B0; cursor: pointer; font-size: 16px; line-height: 1; padding: 2px 5px; }
.x:hover { color: var(--red); }

.btn.small { padding: 4px 11px; font-size: 13px; }
.btn:disabled { background: #DBE1E5; color: #94A3AB; cursor: default; }

tr.new-food td { background: #F7F9FA; }
tr.editing td { background: #EAF1F7; }

.entry-buttons { display: flex; gap: 8px; }
.row-actions { display: flex; gap: 2px; justify-content: flex-end; }

.form-row { display: grid; grid-template-columns: 1.2fr 1fr 1fr 1.5fr auto; gap: 9px; align-items: end; margin-top: 14px; }
.field label, .field .label { display: block; font-size: 12px; color: var(--muted); margin-bottom: 3px; }
.field.wide { margin-top: 10px; }

.toggle { display: flex; }
.toggle button {
  flex: 1; font: inherit; padding: 5px 0; cursor: pointer;
  border: 1px solid var(--rule); background: #F7F9FA; color: var(--muted);
}
.toggle button + button { border-left: none; }
.toggle button.on { color: #fff; border-color: transparent; }
.toggle button[data-value="true"].on { background: var(--green); }
.toggle button[data-value="false"].on { background: var(--red); }

.split { display: grid; grid-template-columns: 1fr 1fr; gap: 24px; align-items: start; }
.pie-key { display: flex; flex-direction: column; gap: 9px; }
.pie-key div { display: flex; justify-content: space-between; border-bottom: 1px solid var(--hair); padding-bottom: 7px; }
.pie-key .pct { color: var(--muted); }

/* Sign in -------------------------------------------------------------- */
.gate { min-height: 100vh; display: grid; place-items: center; padding: 24px; }
.gate-card { background: var(--panel); border: 1px solid var(--rule); padding: 28px; width: 100%; max-width: 360px; }
.gate-card label { display: block; font-size: 12px; color: var(--muted); margin: 14px 0 3px; }
.gate-actions { display: flex; gap: 8px; margin-top: 18px; }
.gate-actions .btn { flex: 1; }
.gate-msg { font-size: 13px; margin: 14px 0 0; min-height: 1.2em; }
.gate-msg[data-tone="error"] { color: var(--red); }
.gate-msg[data-tone="ok"] { color: var(--green); }

.empty { color: var(--muted); padding: 14px 0; }

@media (max-width: 760px) {
  .split { grid-template-columns: 1fr; }
}

/* iPhone and other narrow screens ---------------------------------------- */
@media (max-width: 640px) {
  body { font-size: 15px; }

  .wrap {
    padding: 16px 14px calc(40px + env(safe-area-inset-bottom));
    padding-left: calc(14px + env(safe-area-inset-left));
    padding-right: calc(14px + env(safe-area-inset-right));
  }
  .panel { padding: 16px 14px; margin-top: 14px; }

  /* Readout: a tidy two-column grid instead of ragged wrapping. */
  .readout { display: grid; grid-template-columns: 1fr 1fr; gap: 16px 14px; align-items: start; }
  .readout .big { grid-column: 1 / -1; font-size: 44px; }
  .stat b { font-size: 16px; display: flex; align-items: baseline; flex-wrap: wrap; gap: 2px; }
  .pct-delta { margin-left: 4px; }

  .head { gap: 10px; }
  .head-actions { width: 100%; }
  .head-actions .btn { flex: 1; }

  /* 16px is the threshold below which iOS zooms the page on focus. */
  input, select, .btn, .toggle button { font-size: 16px; }
  input, select { padding: 9px 10px; }

  .form-row { grid-template-columns: 1fr 1fr; gap: 10px; }
  .toggle button { padding: 9px 0; }
  .entry-buttons { margin-top: 14px; }
  .entry-buttons .btn { flex: 1; padding: 12px 15px; }

  /* Comfortable tap targets in the day list. */
  .btn.small { padding: 8px 14px; font-size: 15px; }
  .x { font-size: 20px; padding: 8px 10px; }
  td { padding: 9px 8px 9px 0; }

  /* Keep the food name visible while scrolling its many columns. */
  #food-table td:first-child, #food-table th:first-child {
    position: sticky; left: 0; background: var(--panel); z-index: 1;
    box-shadow: 1px 0 0 var(--hair);
  }
  #food-table tr.new-food td:first-child { background: #F7F9FA; }
  .scroll { -webkit-overflow-scrolling: touch; }

  .legend { gap: 14px; }
  .donut { max-width: 100%; margin: 0 auto; }
  .marker-popup { font-size: 14px; }
  .compare { gap: 18px; }
}

@media (max-width: 380px) {
  .readout { grid-template-columns: 1fr; }
  .form-row { grid-template-columns: 1fr; }
}

@media (prefers-reduced-motion: reduce) {
  * { transition: none !important; animation: none !important; }
}
