:root {
  --primary: #059669;
  --primary-soft: #D9F2E9;
  --amber: #D97706;
  --red: #DC2626;
  --bg: #F4FAF7;
  --surface: #FFFFFF;
  --text: #10231C;
  --muted: #5D7269;
  --border: #E2EDE8;
  --shadow: 0 1px 3px rgba(16, 35, 28, .07), 0 4px 16px rgba(16, 35, 28, .05);
  --radius: 18px;
}
@media (prefers-color-scheme: dark) {
  :root {
    --primary: #10B981;
    --primary-soft: #123B2F;
    --amber: #F59E0B;
    --red: #F87171;
    --bg: #0C1512;
    --surface: #14201B;
    --text: #E8F2ED;
    --muted: #93A89E;
    --border: #1F2E27;
    --shadow: 0 1px 3px rgba(0, 0, 0, .35);
  }
}

* { box-sizing: border-box; }
html { -webkit-text-size-adjust: 100%; }
body {
  margin: 0;
  font-family: 'Inter', system-ui, -apple-system, 'Segoe UI', Roboto, sans-serif;
  background: var(--bg);
  color: var(--text);
  min-height: 100dvh;
  line-height: 1.5;
}
.hidden { display: none !important; }
h1 { font-size: 1.45rem; font-weight: 700; margin: 0; letter-spacing: -.02em; }
h2 { font-size: 1.02rem; font-weight: 600; margin: 0; }
.muted { color: var(--muted); font-size: .875rem; margin: .1rem 0 0; }
.err { color: var(--red); font-size: .875rem; min-height: 1.2em; margin: .4rem 0 0; }

/* ---------- Auth ---------- */
.auth { display: flex; align-items: center; justify-content: center; min-height: 100dvh; padding: 24px; }
.auth-card {
  background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius);
  box-shadow: var(--shadow); padding: 36px 28px; width: 100%; max-width: 360px; text-align: center;
}
.auth-logo { border-radius: 20px; margin-bottom: 12px; }
#pin {
  width: 100%; margin: 18px 0 12px; padding: 14px; font-size: 1.5rem; text-align: center;
  letter-spacing: .5em; border: 1.5px solid var(--border); border-radius: 12px;
  background: var(--bg); color: var(--text);
}
#pin:focus { outline: 2px solid var(--primary); outline-offset: 1px; border-color: transparent; }

/* ---------- Buttons ---------- */
.btn {
  display: inline-flex; align-items: center; justify-content: center; gap: 8px;
  min-height: 48px; padding: 0 20px; font: inherit; font-weight: 600; font-size: .95rem;
  border-radius: 14px; border: 1px solid transparent; cursor: pointer;
  transition: background .15s ease, transform .1s ease; touch-action: manipulation;
}
.btn:active { transform: scale(.97); }
.btn.primary { background: var(--primary); color: #fff; width: 100%; }
.btn.ghost { background: var(--primary-soft); color: var(--primary); flex: 1; }
.btn.small { flex: 0 0 48px; padding: 0; }
.ico { width: 20px; height: 20px; }

/* ---------- Today layout ---------- */
#today { max-width: 480px; margin: 0 auto; padding: 20px 16px 96px; }
.top { display: flex; justify-content: space-between; align-items: center; margin-bottom: 16px; }
.chip {
  font: inherit; font-weight: 600; font-size: .875rem; color: var(--primary);
  background: var(--primary-soft); border: none; border-radius: 999px;
  padding: 10px 16px; min-height: 44px; cursor: pointer;
}
.card {
  background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius);
  box-shadow: var(--shadow); padding: 18px; margin-bottom: 14px;
}

/* ---------- Water ring ---------- */
.water-card { display: flex; align-items: center; gap: 18px; }
.ring-wrap { position: relative; width: 128px; height: 128px; flex: none; }
.ring { width: 100%; height: 100%; transform: rotate(-90deg); }
.ring-bg { fill: none; stroke: var(--primary-soft); stroke-width: 10; }
.ring-fg {
  fill: none; stroke: var(--primary); stroke-width: 10; stroke-linecap: round;
  stroke-dasharray: 339.3; stroke-dashoffset: 339.3; transition: stroke-dashoffset .4s ease;
}
.ring-center {
  position: absolute; inset: 0; display: flex; flex-direction: column;
  align-items: center; justify-content: center;
}
.ring-center strong { font-size: 1.5rem; font-weight: 700; font-variant-numeric: tabular-nums; }
.ring-note { font-size: .55rem; text-align: center; max-width: 100px; line-height: 1.3; margin-top: 2px; }
.water-actions { display: flex; flex-direction: column; gap: 8px; flex: 1; }
.water-actions .btn { width: 100%; }
.water-actions .btn.small { width: 48px; align-self: flex-end; }

/* ---------- Carbs ---------- */
.row-head { display: flex; justify-content: space-between; align-items: center; }
.light { width: 16px; height: 16px; border-radius: 50%; display: inline-block; }
.light.g { background: var(--primary); box-shadow: 0 0 8px var(--primary); }
.light.a { background: var(--amber); box-shadow: 0 0 8px var(--amber); }
.light.r { background: var(--red); box-shadow: 0 0 8px var(--red); }
#carbNow { font-size: 1.5rem; font-variant-numeric: tabular-nums; }
.meals { list-style: none; padding: 0; margin: 8px 0 0; }
.meals li {
  display: grid; grid-template-columns: 10px 1fr auto; gap: 10px; align-items: center;
  padding: 8px 0; border-top: 1px solid var(--border); font-size: .9rem;
}
.meals li.has-del { grid-template-columns: 10px 1fr auto auto; }
.hdot { width: 10px; height: 10px; border-radius: 50%; background: var(--border); flex: none; }
.hdot.green { background: var(--primary); }
.hdot.amber { background: var(--amber); }
.hdot.red { background: var(--red); }
.meal-g {
  border-left: 1px solid var(--border); padding-left: 12px; min-width: 56px; text-align: right;
  font-variant-numeric: tabular-nums; font-weight: 600;
}
.small-note { font-size: .78rem; margin-top: 10px; }

/* ---------- Check cards ---------- */
.check-row { display: flex; align-items: center; justify-content: space-between; gap: 10px; }
.check-info { flex: 1; }
.row-tools { display: flex; align-items: center; gap: 10px; }

/* ---------- How-to (inline tutorials) ---------- */
.how-btn {
  flex: none; width: 44px; height: 44px; border-radius: 50%;
  border: none; background: none; color: var(--muted); cursor: pointer;
  display: flex; align-items: center; justify-content: center;
}
.how-btn[aria-expanded="true"] { color: var(--primary); background: var(--primary-soft); }
.howto {
  margin-top: 14px; padding-top: 14px; border-top: 1px solid var(--border);
}
.how-title { font-weight: 600; font-size: .9rem; margin: 0 0 4px; }
.how-list { margin: 4px 0 10px; padding-left: 18px; font-size: .875rem; }
.how-list li { margin-bottom: 6px; }
.exercise { display: flex; gap: 12px; align-items: flex-start; margin-bottom: 14px; }
.exercise .muted { font-size: .82rem; }
.diagram {
  flex: none; width: 130px; height: 58px;
  background: var(--primary-soft); border-radius: 12px; padding: 4px;
}
.d-ground { stroke: var(--muted); stroke-width: 2; stroke-linecap: round; opacity: .5; }
.d-head { fill: none; stroke: var(--primary); stroke-width: 3; }
.d-body { fill: none; stroke: var(--primary); stroke-width: 4; stroke-linecap: round; stroke-linejoin: round; }
.d-limb { fill: none; stroke: var(--primary); stroke-width: 3; stroke-linecap: round; stroke-linejoin: round; opacity: .75; }
.order-strip { display: flex; align-items: center; gap: 6px; flex-wrap: wrap; margin: 4px 0 6px; }
.order-pill {
  background: var(--primary-soft); color: var(--primary); font-size: .78rem; font-weight: 600;
  padding: 6px 10px; border-radius: 999px;
}
.order-arrow { color: var(--muted); }
.check-btn {
  flex: none; width: 52px; height: 52px; border-radius: 50%;
  border: 2px solid var(--border); background: var(--bg); cursor: pointer;
  display: flex; align-items: center; justify-content: center;
  transition: background .2s ease, border-color .2s ease;
}
.check-btn::after {
  content: ''; width: 22px; height: 12px;
  border-left: 3px solid transparent; border-bottom: 3px solid transparent;
  transform: rotate(-45deg) translateY(-2px); transition: border-color .2s ease;
}
.check-card.done .check-btn { background: var(--primary); border-color: var(--primary); }
.check-card.done .check-btn::after { border-color: #fff; }
.check-card.done .check-info h2 { text-decoration: line-through; text-decoration-thickness: 2px; color: var(--muted); }

/* ---------- Bottom nav ---------- */
.bottom-nav {
  position: fixed; bottom: 0; left: 0; right: 0;
  display: flex; justify-content: space-around;
  background: var(--surface); border-top: 1px solid var(--border);
  padding: 6px 8px calc(6px + env(safe-area-inset-bottom));
}
.nav-item {
  display: flex; flex-direction: column; align-items: center; gap: 2px;
  font: inherit; font-size: .7rem; font-weight: 500;
  background: none; border: none; color: var(--muted);
  padding: 6px 14px; min-width: 64px; min-height: 48px; cursor: pointer; border-radius: 12px;
}
.nav-item.active { color: var(--primary); }
.nav-item:disabled { opacity: .45; cursor: default; }

/* ---------- Meal logging ---------- */
.log-actions { display: grid; grid-template-columns: 1fr 1fr; gap: 8px; margin-top: 12px; }
.ask-answer {
  background: var(--primary-soft); border-radius: 12px; padding: 12px 14px;
  font-size: .9rem; margin-top: 10px; white-space: pre-wrap;
}
.ask-attach { font-size: .8rem; }
#micBtn { user-select: none; -webkit-user-select: none; }
#micBtn.recording {
  background: var(--red); color: #fff;
  animation: pulse 1.2s ease-in-out infinite;
}
@keyframes pulse { 50% { opacity: .75; } }
.meal-right { display: inline-flex; align-items: center; gap: 6px; }
.meal-del {
  border: none; background: none; color: var(--muted); font-size: 1.2rem;
  width: 32px; height: 32px; border-radius: 50%; cursor: pointer; line-height: 1;
}
.meal-del:active { background: var(--primary-soft); }

/* ---------- Recent-meal chips ---------- */
.chip-wrap { margin-bottom: 12px; }
.meal-chip {
  display: inline-flex; align-items: center; gap: 7px;
  font: inherit; font-size: .85rem; font-weight: 500;
  background: var(--bg); color: var(--text);
  border: 1px solid var(--border); border-radius: 999px;
  padding: 9px 14px; margin: 0 6px 6px 0; min-height: 40px; cursor: pointer;
}
.meal-chip:active { background: var(--primary-soft); }
.meal-chip:disabled { opacity: .5; }

/* ---------- Bottom sheet ---------- */
.sheet { position: fixed; inset: 0; z-index: 100; }
.sheet-scrim { position: absolute; inset: 0; background: rgba(0, 0, 0, .5); }
.sheet-card {
  position: absolute; left: 0; right: 0; bottom: 0;
  background: var(--surface); border-radius: 22px 22px 0 0;
  padding: 22px 18px calc(18px + env(safe-area-inset-bottom));
  max-width: 480px; margin: 0 auto; box-shadow: var(--shadow);
  max-height: 75dvh; overflow-y: auto;
}
.sheet-card h2 { margin-bottom: 12px; }
.sheet-input {
  width: 100%; padding: 12px 14px; margin-bottom: 10px; font: inherit; font-size: 1rem;
  border: 1.5px solid var(--border); border-radius: 12px;
  background: var(--bg); color: var(--text);
}
.sheet-input:focus { outline: 2px solid var(--primary); outline-offset: 1px; border-color: transparent; }
textarea.sheet-input { resize: none; line-height: 1.4; }
.sheet-input.carbs { width: 90px; margin-bottom: 0; font-variant-numeric: tabular-nums; }
.sheet-row { display: flex; align-items: center; gap: 10px; margin-bottom: 8px; flex-wrap: wrap; }
.conf { font-size: .75rem; font-weight: 600; padding: 4px 10px; border-radius: 999px; }
.conf.high { background: var(--primary-soft); color: var(--primary); }
.conf.medium { background: rgba(217, 119, 6, .15); color: var(--amber); }
.conf.low { background: rgba(220, 38, 38, .12); color: var(--red); }
.swap-hint {
  background: var(--primary-soft); color: var(--text); font-size: .85rem;
  padding: 10px 12px; border-radius: 12px; margin: 8px 0 0;
}
.intent-row {
  display: flex; align-items: center; gap: 12px; padding: 12px 4px;
  border-bottom: 1px solid var(--border); font-size: .95rem; cursor: pointer;
}
.intent-row input[type="checkbox"] { width: 22px; height: 22px; accent-color: var(--primary); }
.sheet-actions { display: flex; gap: 10px; margin-top: 16px; }
.sheet-actions .btn { flex: 1; }

/* ---------- Charts & Labs ---------- */
.chart { width: 100%; height: auto; margin-top: 8px; }
.chart-lbl { font-size: 9px; fill: var(--muted); font-family: inherit; }
.chart-goal { stroke: var(--muted); stroke-width: 1.5; stroke-dasharray: 5 4; opacity: .7; }
.chart-band { fill: var(--primary-soft); }
.chart-line { fill: none; stroke: var(--primary); stroke-width: 2.5; stroke-linecap: round; stroke-linejoin: round; }
.chart-dot { fill: var(--primary); }
.labs-table { margin-top: 10px; }
.labs-row {
  display: grid; grid-template-columns: 16px 1fr auto auto; gap: 10px; align-items: center;
  padding: 9px 0; border-top: 1px solid var(--border); font-size: .875rem;
}
.labs-name { font-weight: 500; }
.labs-val { font-variant-numeric: tabular-nums; font-weight: 600; }
.labs-ref { font-size: .75rem; }

/* ---------- Calendar ---------- */
.cal-head { display: flex; justify-content: space-between; align-items: center; margin-bottom: 10px; }
.cal-nav {
  width: 44px; height: 44px; border-radius: 50%; border: 1px solid var(--border);
  background: var(--bg); color: var(--text); font-size: 1.3rem; cursor: pointer;
}
.cal-nav:disabled { opacity: .35; cursor: default; }
.cal-grid { display: grid; grid-template-columns: repeat(7, 1fr); gap: 4px; }
.cal-wd { text-align: center; font-size: .7rem; color: var(--muted); font-weight: 600; padding: 4px 0; }
.cal-cell {
  aspect-ratio: 1; border: none; border-radius: 10px; background: var(--bg);
  color: var(--text); font: inherit; font-size: .85rem; cursor: pointer;
  display: flex; flex-direction: column; align-items: center; justify-content: center; gap: 3px;
}
.cal-cell:disabled { opacity: .3; cursor: default; }
.cal-cell.today { outline: 2px solid var(--primary); font-weight: 700; }
.cal-cell:active:not(:disabled) { background: var(--primary-soft); }
.cal-dots { display: flex; gap: 3px; min-height: 6px; }
.cal-dot { width: 6px; height: 6px; border-radius: 50%; }
.cal-dot.g { background: var(--primary); }
.cal-dot.a { background: var(--amber); }
.cal-dot.r { background: var(--red); }
.card.tappable { cursor: pointer; }
.card.tappable:active { background: var(--primary-soft); }

/* ---------- Add-exercise button (end of Today page) ---------- */
.add-exercise { width: 100%; margin-top: 10px; }

/* ---------- Toast ---------- */
.toast {
  position: fixed; left: 50%; bottom: 88px; transform: translateX(-50%) translateY(20px);
  background: var(--text); color: var(--bg); padding: 10px 18px; border-radius: 999px;
  font-size: .875rem; font-weight: 500; opacity: 0; pointer-events: none;
  transition: opacity .25s ease, transform .25s ease; max-width: 90vw;
}
.toast.show { opacity: 1; transform: translateX(-50%) translateY(0); }

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