/* Health Tracker: Apple-style system look. Colours follow the iOS system palette. */
:root {
  color-scheme: light dark;
  --bg: #f2f2f7; --card: #ffffff; --text: #000000; --muted: #6c6c70;
  --line: rgba(60, 60, 67, 0.14); --fill: rgba(118, 118, 128, 0.12);
  --accent: #0071e3; --accent-text: #ffffff; --accent-tint: rgba(0, 113, 227, 0.12);
  --good: #248a3d; --bad: #d70015; --manual: rgba(255, 204, 0, 0.22);
  --bar: rgba(255, 255, 255, 0.78); --chev: #c4c4c7; --sheet: #f2f2f7; --sheet-card: #ffffff;
  --indigo: #5856d6; --teal: #30b0c7; --orange: #ff9500; --purple: #af52de; --red: #ff3b30; --pink: #ff2d55;
  --font: -apple-system, BlinkMacSystemFont, "SF Pro Text", "SF Pro Display", "Helvetica Neue", Inter, "Segoe UI", system-ui, sans-serif;
}
@media (prefers-color-scheme: dark) {
  :root {
    --bg: #000000; --card: #1c1c1e; --text: #ffffff; --muted: #8e8e93;
    --line: rgba(84, 84, 88, 0.5); --fill: rgba(118, 118, 128, 0.24);
    --accent: #0a84ff; --accent-text: #ffffff; --accent-tint: rgba(10, 132, 255, 0.2);
    --good: #30d158; --bad: #ff453a; --manual: rgba(255, 214, 10, 0.2);
    --bar: rgba(28, 28, 30, 0.8); --chev: #5c5c61; --sheet: #1c1c1e; --sheet-card: #2c2c2e;
    --indigo: #7d7aff; --teal: #40c8e0; --orange: #ff9f0a; --purple: #bf5af2; --red: #ff453a; --pink: #ff375f;
  }
}

* { box-sizing: border-box; }
html { -webkit-text-size-adjust: 100%; }
body {
  margin: 0; background: var(--bg); color: var(--text);
  font: 17px/1.4 var(--font); letter-spacing: -0.01em;
  -webkit-font-smoothing: antialiased; padding-bottom: calc(84px + env(safe-area-inset-bottom));
}
h1, h2 { margin: 0; }
:focus-visible { outline: 3px solid var(--accent-tint); outline-offset: 1px; border-radius: 8px; }
button:focus-visible, input:focus-visible, select:focus-visible { outline: 3px solid var(--accent); outline-offset: 2px; }

/* ---------- page frame ---------- */
header { display: flex; align-items: flex-end; justify-content: space-between; gap: 12px; padding: 28px 20px 8px; max-width: 1100px; margin: 0 auto; }
header h1, .login h1 { font-size: 34px; line-height: 1.1; font-weight: 700; letter-spacing: -0.03em; }
main { padding: 8px 16px 24px; max-width: 1100px; margin: 0 auto; }
.muted { color: var(--muted); font-size: 14px; letter-spacing: 0; }
.row { display: flex; gap: 10px; flex-wrap: wrap; align-items: center; }
.grid { display: grid; gap: 12px; grid-template-columns: 1fr; }
@media (min-width: 720px) { .grid { grid-template-columns: 1fr 1fr; } }
@media (min-width: 1020px) { .grid.metrics { grid-template-columns: repeat(3, 1fr); } }

.card { background: var(--card); border-radius: 20px; padding: 16px 18px; margin-bottom: 12px; }
.card > h2, .card h2 { font-size: 20px; font-weight: 700; letter-spacing: -0.02em; margin-bottom: 10px; }

/* ---------- controls ---------- */
button, input, select { font: inherit; color: inherit; letter-spacing: inherit; }
button {
  background: var(--accent); color: var(--accent-text); border: 0; border-radius: 12px;
  padding: 0 18px; min-height: 44px; font-weight: 600; cursor: pointer; transition: opacity 0.15s; white-space: nowrap;
}
button:active { opacity: 0.7; }
button.ghost { background: var(--accent-tint); color: var(--accent); }
button.danger { background: transparent; color: var(--bad); }
button.link { background: none; color: var(--accent); min-height: 36px; padding: 0 4px; font-weight: 500; }
button.small { min-height: 32px; padding: 0 12px; font-size: 15px; border-radius: 10px; }
input, select {
  background: var(--fill); border: 0; border-radius: 10px; padding: 0 12px; min-height: 44px; width: 100%;
  accent-color: var(--accent);
}
input::placeholder { color: var(--muted); }
label { display: block; font-size: 13px; color: var(--muted); margin: 0 0 4px 2px; letter-spacing: 0; }
.field { flex: 1 1 140px; }

.segmented { display: inline-flex; background: var(--fill); border-radius: 10px; padding: 2px; }
.segmented button {
  background: transparent; color: var(--text); min-height: 30px; padding: 0 16px; border-radius: 8px;
  font-size: 14px; font-weight: 500;
}
.segmented button.on { background: var(--card); font-weight: 600; box-shadow: 0 1px 3px rgba(0, 0, 0, 0.16); }
@media (prefers-color-scheme: dark) { .segmented button.on { background: #636366; box-shadow: none; } }

/* ---------- tab bar ---------- */
nav#tabs {
  position: fixed; bottom: 0; left: 0; right: 0; z-index: 10; display: flex;
  background: var(--bar); -webkit-backdrop-filter: saturate(180%) blur(20px); backdrop-filter: saturate(180%) blur(20px);
  border-top: 0.5px solid var(--line); padding: 6px 4px calc(6px + env(safe-area-inset-bottom));
}
nav#tabs a {
  flex: 1; display: flex; flex-direction: column; align-items: center; gap: 2px; padding: 4px 2px;
  font-size: 10px; font-weight: 500; letter-spacing: 0; color: var(--muted); text-decoration: none;
}
nav#tabs svg { width: 24px; height: 24px; fill: none; stroke: currentColor; stroke-width: 1.7; stroke-linecap: round; stroke-linejoin: round; }
nav#tabs a.on { color: var(--accent); }
@media (min-width: 720px) {
  body { padding-bottom: 24px; }
  nav#tabs {
    position: static; max-width: 1100px; margin: 0 auto; padding: 4px 16px 8px; gap: 4px; justify-content: flex-start;
    background: none; -webkit-backdrop-filter: none; backdrop-filter: none; border: 0;
  }
  nav#tabs a { flex: none; flex-direction: row; gap: 6px; font-size: 15px; padding: 7px 14px; border-radius: 10px; }
  nav#tabs svg { width: 18px; height: 18px; }
  nav#tabs a.on { background: var(--accent-tint); }
}

/* ---------- dashboard ---------- */
.page-tools { display: flex; align-items: center; justify-content: space-between; gap: 12px; margin: 4px 4px 14px; }
.metric { --c: var(--indigo); background: var(--card); border-radius: 20px; padding: 16px 18px 12px; display: flex; flex-direction: column; min-width: 0; }
.m-weight { --c: var(--indigo); } .m-waist { --c: var(--teal); } .m-steps { --c: var(--orange); }
.m-sleep { --c: var(--purple); } .m-hr { --c: var(--red); } .m-hrv { --c: var(--pink); }
.metric-top { display: flex; justify-content: space-between; align-items: baseline; gap: 8px; }
.metric-name { font-size: 15px; font-weight: 600; color: var(--c); letter-spacing: -0.01em; }
.metric-when { font-size: 13px; color: var(--muted); letter-spacing: 0; }
.metric-value { margin: 6px 0 2px; display: flex; align-items: baseline; gap: 5px; }
.metric-value b { font-size: 36px; line-height: 1.1; font-weight: 700; letter-spacing: -0.03em; font-variant-numeric: tabular-nums; }
.metric-value span { font-size: 16px; font-weight: 500; color: var(--muted); }
.metric-value b.none { font-size: 22px; font-weight: 600; color: var(--muted); letter-spacing: -0.01em; }
.metric-meta { display: flex; justify-content: space-between; gap: 8px; flex-wrap: wrap; font-size: 13px; color: var(--muted); letter-spacing: 0; font-variant-numeric: tabular-nums; }
.chartbox { position: relative; height: 120px; margin: 10px -4px 0; }

/* ---------- tables ---------- */
.tablewrap { overflow-x: auto; background: var(--card); border-radius: 20px; }
table { border-collapse: collapse; width: 100%; font-size: 15px; font-variant-numeric: tabular-nums; }
th, td { padding: 6px 8px; border-bottom: 0.5px solid var(--line); text-align: left; white-space: nowrap; }
tr:last-child td { border-bottom: 0; }
th { color: var(--muted); font-weight: 600; font-size: 13px; letter-spacing: 0; position: sticky; top: 0; background: var(--card); padding-top: 12px; }
td.sticky, th.sticky { position: sticky; left: 0; background: var(--card); z-index: 1; padding-left: 16px; font-weight: 500; }
td input { min-height: 34px; padding: 0 8px; width: 88px; border-radius: 8px; }
td input[type=time] { width: 100px; }
td input.manual { background: var(--manual); }
td input.saved { outline: 2px solid var(--good); }
td input.err { outline: 2px solid var(--bad); }

/* ---------- lists, food ---------- */
.list > div { display: flex; justify-content: space-between; gap: 8px; align-items: center; padding: 10px 0; border-bottom: 0.5px solid var(--line); }
.list > div:last-child { border-bottom: 0; }
.pick { width: 100%; text-align: left; background: var(--fill); color: var(--text); font-weight: 500; margin-top: 6px; padding: 10px 14px; }
.pick.on { outline: 2px solid var(--accent); }
.totals { display: grid; grid-template-columns: repeat(auto-fit, minmax(84px, 1fr)); gap: 10px; text-align: center; }
.totals b { display: block; font-size: 24px; font-weight: 700; letter-spacing: -0.02em; font-variant-numeric: tabular-nums; }
.err-text { color: var(--bad); min-height: 1.2em; font-size: 14px; letter-spacing: 0; }
.ok-text { color: var(--good); min-height: 1.2em; font-size: 14px; letter-spacing: 0; }
details summary { cursor: pointer; padding: 4px 0; }

/* ---------- login ---------- */
.login { max-width: 380px; margin: 16vh auto 0; padding: 0 16px; }
.login h1 { margin-bottom: 18px; }
.login .card { padding: 22px; }

/* ---------- inset grouped lists ---------- */
.inset-list { background: var(--card); border-radius: 16px; overflow: hidden; margin-bottom: 12px; }
.inset-list .item {
  display: flex; align-items: center; gap: 12px; width: 100%; min-height: 56px; padding: 10px 16px; position: relative;
  background: transparent; color: var(--text); border-radius: 0; font-weight: 400; text-align: left; letter-spacing: -0.01em;
}
.inset-list .item + .item::before { content: ""; position: absolute; top: 0; left: 16px; right: 0; border-top: 0.5px solid var(--line); }
.inset-list .item::after {
  content: ""; flex: none; width: 7px; height: 7px; margin-left: 2px; transform: rotate(45deg);
  border-right: 2px solid var(--chev); border-top: 2px solid var(--chev);
}
.inset-list .item:active { background: var(--fill); opacity: 1; }
@media (hover: hover) { .inset-list .item:hover { background: var(--fill); } }
.item-main { flex: 1; min-width: 0; display: flex; flex-direction: column; gap: 2px; }
.item-title { font-weight: 500; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.item-sub { display: flex; flex-wrap: wrap; gap: 0 12px; font-size: 14px; color: var(--muted); letter-spacing: 0; font-variant-numeric: tabular-nums; }
.item-end { flex: none; color: var(--muted); font-size: 15px; font-variant-numeric: tabular-nums; }
.empty { text-align: center; padding: 28px 16px; }
.empty p { margin: 0 0 4px; }
.row.center { justify-content: center; margin-bottom: 12px; }
.row.nowrap { flex-wrap: nowrap; }
.row.nowrap .field { flex: 1 1 auto; min-width: 0; }

/* ---------- grouped forms ---------- */
.form-section { margin-bottom: 20px; }
.group-title { display: flex; justify-content: space-between; margin: 0 16px 6px; font-size: 13px; font-weight: 500; color: var(--muted); letter-spacing: 0; }
.group-footer { margin: 6px 16px 0; font-size: 13px; color: var(--muted); letter-spacing: 0; }
.group-footer.err-text { color: var(--bad); }
.group-footer.ok-text { color: var(--good); }
.form-group { background: var(--card); border-radius: 16px; overflow: hidden; }
.form-group > * { position: relative; }
.form-group > * + *::before { content: ""; position: absolute; top: 0; left: 16px; right: 0; border-top: 0.5px solid var(--line); }
.form-row { display: flex; align-items: center; justify-content: space-between; gap: 12px; min-height: 48px; padding: 0 16px; margin: 0; font-size: 17px; color: var(--text); letter-spacing: -0.01em; cursor: text; }
.form-label { flex: none; display: flex; align-items: center; }
.form-row.typed .form-label::after { content: ""; width: 7px; height: 7px; margin-left: 8px; border-radius: 50%; background: #ffcc00; }
.form-value { flex: 1; min-width: 0; display: flex; align-items: center; justify-content: flex-end; gap: 6px; }
.form-row input { background: transparent; border-radius: 8px; min-height: 44px; padding: 0; width: 100%; text-align: right; font: inherit; letter-spacing: inherit; }
.form-row input[type=date], .form-row input[type=time] { width: auto; flex: none; }
.form-row input:focus-visible { outline-offset: 4px; }
.form-row input::placeholder { color: var(--muted); opacity: 0.7; }
.form-row.stack { flex-direction: column; align-items: stretch; justify-content: center; gap: 0; padding: 8px 16px; cursor: text; }
.form-row.stack .form-value { justify-content: stretch; }
.form-row.stack input { text-align: left; min-height: 32px; }
.unit { flex: none; color: var(--muted); font-size: 15px; letter-spacing: 0; }
.row-action { width: 100%; min-height: 48px; background: transparent; border-radius: 0; color: var(--accent); font-weight: 400; font-size: 17px; }
.row-action.danger { color: var(--bad); }
.row-action:active { background: var(--fill); opacity: 1; }
.segmented.full { display: flex; width: 100%; }
.segmented.full button { flex: 1; padding: 0 4px; }

/* ---------- bottom sheet ---------- */
button.link.strong { font-weight: 600; }
body.no-scroll { overflow: hidden; }
.sheet-backdrop { position: fixed; inset: 0; z-index: 50; display: flex; align-items: flex-end; justify-content: center; background: rgba(0, 0, 0, 0); transition: background 0.25s; }
.sheet-backdrop.open { background: rgba(0, 0, 0, 0.4); }
.sheet {
  width: 100%; max-width: 560px; max-height: 92vh; max-height: 92dvh; display: flex; flex-direction: column;
  background: var(--sheet); border-radius: 20px 20px 0 0; transform: translateY(100%); transition: transform 0.35s cubic-bezier(0.32, 0.72, 0, 1);
}
.sheet-backdrop.open .sheet { transform: none; }
.sheet .form-group, .sheet .inset-list { background: var(--sheet-card); }
.sheet-bar { display: grid; grid-template-columns: minmax(0, 1fr) minmax(0, auto) minmax(0, 1fr); align-items: center; gap: 8px; padding: 8px 12px; border-bottom: 0.5px solid var(--line); }
.sheet-bar h2 { margin: 0; font-size: 17px; font-weight: 600; text-align: center; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; max-width: 52vw; }
.sheet-bar > :first-child { justify-self: start; }
.sheet-bar > :last-child { justify-self: end; }
.sheet-status { font-size: 15px; color: var(--muted); letter-spacing: 0; }
.sheet-status.bad { color: var(--bad); }
.sheet-body { overflow: auto; overscroll-behavior: contain; padding: 20px 16px calc(16px + env(safe-area-inset-bottom)); }
@media (min-width: 720px) {
  .sheet-backdrop { align-items: center; }
  .sheet { border-radius: 20px; max-height: 85vh; opacity: 0; transform: translateY(16px); transition: transform 0.3s cubic-bezier(0.32, 0.72, 0, 1), opacity 0.2s; }
  .sheet-backdrop.open .sheet { opacity: 1; }
}

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