:root {
  --bg: #f5f3ee;
  --surface: #fcfbf8;
  --sunken: #ece9e2;
  --line: #e2ded5;
  --ink: #1c1b19;
  --ink-2: #57534b;
  --ink-3: #8a857b;
  --accent: #1a8366;
  --accent-ink: #136650;
  --accent-soft: #dcefe7;
  --over: #c4572f;
  --over-soft: #f7e3da;
  --on-accent: #ffffff;
  --focus: #1a8366;
  --radius: 14px;
  --radius-sm: 9px;
  --shadow: 0 1px 2px rgb(28 27 25 / 0.04), 0 4px 16px rgb(28 27 25 / 0.04);
  --font: -apple-system, BlinkMacSystemFont, "SF Pro Text", "Segoe UI", "PingFang SC", "Hiragino Sans GB", "Microsoft YaHei", sans-serif;
  --font-display: -apple-system, BlinkMacSystemFont, "SF Pro Display", "Segoe UI", "PingFang SC", sans-serif;
  color-scheme: light;
}

@media (prefers-color-scheme: dark) {
  :root {
    --bg: #151514;
    --surface: #1d1d1b;
    --sunken: #262624;
    --line: #2f2e2b;
    --ink: #ecebe6;
    --ink-2: #b2aea5;
    --ink-3: #85817a;
    --accent: #35997a;
    --accent-ink: #6cc6a6;
    --accent-soft: #1d3a31;
    --over: #cf6743;
    --over-soft: #3d241a;
    --on-accent: #0f1f1a;
    --focus: #6cc6a6;
    --shadow: none;
    color-scheme: dark;
  }
}

* { box-sizing: border-box; }
html { -webkit-text-size-adjust: 100%; }
body {
  margin: 0;
  background: var(--bg);
  color: var(--ink);
  font: 16px/1.5 var(--font);
  -webkit-font-smoothing: antialiased;
  font-feature-settings: "tnum" 1;
}
[hidden] { display: none !important; }
button, input, select, textarea { font: inherit; color: inherit; }
:focus-visible { outline: 2px solid var(--focus); outline-offset: 2px; border-radius: 6px; }

h1, h2, p { margin: 0; }
.muted { color: var(--ink-2); }
.small { font-size: 0.875rem; }

.wordmark {
  font-family: var(--font-display);
  font-weight: 650;
  letter-spacing: -0.02em;
  font-size: 1.125rem;
}
.wordmark::after { content: "."; color: var(--accent); }

/* ---------- buttons & fields ---------- */

.btn {
  display: inline-flex; align-items: center; justify-content: center; gap: 8px;
  min-height: 42px; padding: 0 18px;
  border: 0; border-radius: 999px;
  font-weight: 600; font-size: 0.9375rem;
  cursor: pointer;
  transition: background-color 0.15s, opacity 0.15s, transform 0.1s;
}
.btn:active { transform: scale(0.98); }
.btn:disabled { opacity: 0.55; cursor: progress; }
.btn.primary { background: var(--ink); color: var(--bg); }
.btn.primary:hover { opacity: 0.88; }
.btn.quiet { background: var(--sunken); color: var(--ink); }
.btn.quiet:hover { background: var(--line); }
.btn.block { width: 100%; }

.icon-btn {
  width: 40px; height: 40px; border-radius: 50%;
  display: grid; place-items: center;
  border: 0; background: transparent; cursor: pointer; color: var(--ink-2);
}
.icon-btn:hover { background: var(--sunken); color: var(--ink); }
.icon-btn:disabled { opacity: 0.3; cursor: default; background: transparent; }
.icon-btn svg { width: 20px; height: 20px; fill: none; stroke: currentColor; stroke-width: 2; stroke-linecap: round; stroke-linejoin: round; }

.field { display: grid; gap: 6px; font-size: 0.875rem; color: var(--ink-2); }
.field input, .field select, textarea {
  width: 100%; min-height: 44px; padding: 10px 12px;
  background: var(--surface); color: var(--ink);
  border: 1px solid var(--line); border-radius: var(--radius-sm);
  font-size: 1rem;
}
.field input:focus, .field select:focus, textarea:focus, .weigh-input:focus-within {
  outline: none; border-color: var(--accent); box-shadow: 0 0 0 3px var(--accent-soft);
}
.form-error { color: var(--over); font-size: 0.875rem; min-height: 0; }
.form-error:empty { display: none; }

/* ---------- login ---------- */

.login { min-height: 100dvh; display: grid; place-items: center; padding: 24px 16px; }
.login-card { width: min(360px, 100%); display: grid; gap: 20px; }
.login-card h1 { font-family: var(--font-display); font-size: 1.75rem; letter-spacing: -0.02em; font-weight: 650; }

/* ---------- layout ---------- */

.topbar {
  position: sticky; top: 0; z-index: 10;
  background: color-mix(in srgb, var(--bg) 88%, transparent);
  backdrop-filter: saturate(1.4) blur(12px);
  -webkit-backdrop-filter: saturate(1.4) blur(12px);
  border-bottom: 1px solid var(--line);
  padding-top: env(safe-area-inset-top);
}
.topbar-inner {
  max-width: 640px; margin: 0 auto; padding: 10px 16px;
  display: flex; align-items: center; justify-content: space-between; gap: 12px;
}
.tabs { display: flex; gap: 2px; background: var(--sunken); padding: 3px; border-radius: 999px; }
.tabs button {
  border: 0; background: transparent; cursor: pointer;
  padding: 6px 12px; border-radius: 999px;
  font-size: 0.875rem; font-weight: 550; color: var(--ink-2);
}
.tabs button[aria-selected="true"] { background: var(--surface); color: var(--ink); box-shadow: var(--shadow); }

.page { max-width: 640px; margin: 0 auto; padding: 8px 16px calc(48px + env(safe-area-inset-bottom)); }
.view { display: grid; gap: 28px; }

.section-title {
  font-size: 0.8125rem; font-weight: 600; letter-spacing: 0.04em; text-transform: uppercase;
  color: var(--ink-2); margin-bottom: 10px;
  display: flex; align-items: baseline; gap: 8px;
}
.section-title .count { text-transform: none; letter-spacing: 0; font-weight: 500; color: var(--ink-3); }

/* ---------- today ---------- */

.datebar { display: grid; grid-template-columns: 40px 1fr 40px; align-items: center; padding-top: 8px; }
.date-label { text-align: center; display: grid; line-height: 1.25; }
#date-title { font-weight: 600; }
#date-sub { font-size: 0.8125rem; }

.budget { display: grid; gap: 14px; }
.eyebrow { color: var(--ink-2); font-size: 0.9375rem; }
.hero-num {
  font-family: var(--font-display);
  font-size: clamp(3rem, 14vw, 4.25rem); line-height: 1; font-weight: 650; letter-spacing: -0.035em;
  display: flex; align-items: baseline; gap: 10px;
}
.hero-num .unit { font-size: 1rem; letter-spacing: 0; font-weight: 500; color: var(--ink-3); }
.budget.over .hero-num { color: var(--over); }

.meter { height: 10px; border-radius: 999px; background: var(--sunken); overflow: hidden; }
.meter-fill { height: 100%; width: 0; border-radius: 999px; background: var(--accent); transition: width 0.5s cubic-bezier(.2,.8,.2,1); }
.budget.over .meter-fill { background: var(--over); }

.budget-row { display: grid; grid-template-columns: repeat(4, 1fr); gap: 8px; margin: 0; }
.budget-row div { display: grid; gap: 2px; }
.budget-row dt { font-size: 0.75rem; color: var(--ink-3); }
.budget-row dd { margin: 0; font-weight: 600; font-size: 0.9375rem; }

.weigh {
  display: flex; align-items: center; gap: 12px;
  padding: 10px 10px 10px 16px; background: var(--surface); border: 1px solid var(--line); border-radius: var(--radius);
}
.weigh label { flex: 1; font-weight: 550; }
.weigh-input {
  display: flex; align-items: baseline; gap: 4px;
  border: 1px solid var(--line); border-radius: var(--radius-sm); padding: 0 10px; background: var(--bg);
}
.weigh-input input {
  width: 4.5ch; min-height: 40px; border: 0; background: transparent; text-align: right;
  font-size: 1.125rem; font-weight: 600; outline: none; -moz-appearance: textfield;
}
.weigh-input input::-webkit-outer-spin-button, .weigh-input input::-webkit-inner-spin-button { -webkit-appearance: none; margin: 0; }
.weigh-input span { color: var(--ink-3); font-size: 0.875rem; }

.composer form {
  display: grid; gap: 12px;
  padding: 14px; background: var(--surface); border: 1px solid var(--line); border-radius: var(--radius);
  box-shadow: var(--shadow);
}
.composer textarea { resize: vertical; min-height: 64px; border-color: transparent; background: var(--bg); line-height: 1.45; }
.composer-foot { display: flex; align-items: center; justify-content: space-between; gap: 12px; }
.hint { font-size: 0.8125rem; line-height: 1.35; }

.segmented { display: inline-flex; background: var(--sunken); padding: 3px; border-radius: 999px; justify-self: start; }
.segmented input { position: absolute; opacity: 0; pointer-events: none; }
.segmented span { display: block; padding: 5px 14px; border-radius: 999px; font-size: 0.875rem; font-weight: 550; color: var(--ink-2); cursor: pointer; }
.segmented input:checked + span { background: var(--surface); color: var(--ink); box-shadow: var(--shadow); }
.segmented input:focus-visible + span { outline: 2px solid var(--focus); outline-offset: 1px; }

.entries { list-style: none; margin: 0; padding: 0; border-top: 1px solid var(--line); }
.entries li { border-bottom: 1px solid var(--line); }
.entries .empty { padding: 14px 0; color: var(--ink-3); font-size: 0.9375rem; border-bottom: 0; }
.entry { display: grid; grid-template-columns: 1fr auto; gap: 4px 12px; padding: 14px 0; }
.entry-text { font-weight: 500; overflow-wrap: anywhere; }
.entry-kcal {
  font-weight: 650; white-space: nowrap; align-self: start;
  border: 0; background: transparent; padding: 2px 6px; margin: -2px -6px; border-radius: 6px; cursor: pointer;
}
.entry-kcal:hover { background: var(--sunken); }
.entry-kcal small { font-weight: 500; color: var(--ink-3); margin-left: 2px; }
.entry-meta { grid-column: 1 / -1; display: flex; flex-wrap: wrap; gap: 4px 12px; font-size: 0.8125rem; color: var(--ink-2); }
.entry-note { grid-column: 1 / -1; font-size: 0.8125rem; color: var(--ink-3); white-space: pre-line; line-height: 1.45; }
.entry-actions { grid-column: 1 / -1; display: flex; gap: 14px; }
.link-btn { border: 0; background: none; padding: 0; color: var(--ink-3); font-size: 0.8125rem; cursor: pointer; }
.link-btn:hover { color: var(--over); }
.kcal-edit { display: inline-flex; gap: 6px; align-items: center; }
.kcal-edit input { width: 6ch; min-height: 32px; padding: 2px 6px; border: 1px solid var(--accent); border-radius: 6px; background: var(--surface); text-align: right; }

.pending .entry-text { color: var(--ink-3); }
.shimmer {
  display: inline-block; width: 64px; height: 1em; border-radius: 6px;
  background: linear-gradient(90deg, var(--sunken), var(--line), var(--sunken));
  background-size: 200% 100%; animation: shimmer 1.2s linear infinite;
}
@keyframes shimmer { to { background-position: -200% 0; } }

.review-head { display: flex; justify-content: space-between; align-items: center; margin-bottom: 10px; }
.review-head .section-title { margin: 0; }
.review-card {
  display: grid; gap: 16px; padding: 18px;
  background: var(--surface); border: 1px solid var(--line); border-radius: var(--radius);
}
.review-top { display: flex; gap: 16px; align-items: center; }
.score {
  flex: none; width: 56px; height: 56px; border-radius: 50%;
  display: grid; place-items: center; line-height: 1;
  background: var(--accent-soft); color: var(--accent-ink);
  font-family: var(--font-display); font-size: 1.375rem; font-weight: 700;
}
.score small { font-size: 0.625rem; font-weight: 600; opacity: 0.8; display: block; text-align: center; margin-top: 2px; }
.score.low { background: var(--over-soft); color: var(--over); }
.review-headline { font-weight: 600; line-height: 1.4; }
.review-card h3 { margin: 0 0 6px; font-size: 0.8125rem; font-weight: 600; color: var(--ink-2); }
.review-card ul { margin: 0; padding-left: 1.1em; display: grid; gap: 4px; }
.tomorrow { padding: 12px 14px; background: var(--accent-soft); border-radius: var(--radius-sm); color: var(--ink); }
.tomorrow h3 { color: var(--accent-ink); }
.review-empty { color: var(--ink-3); font-size: 0.9375rem; }

/* ---------- progress ---------- */

.stats { display: grid; grid-template-columns: repeat(2, 1fr); gap: 10px; padding-top: 16px; }
@media (min-width: 560px) { .stats { grid-template-columns: repeat(4, 1fr); } }
.stat { margin: 0; padding: 14px; background: var(--surface); border: 1px solid var(--line); border-radius: var(--radius); display: grid; gap: 2px; }
.stat dt { font-size: 0.75rem; color: var(--ink-3); }
.stat dd { margin: 0; font-family: var(--font-display); font-size: 1.5rem; font-weight: 650; letter-spacing: -0.02em; }
.stat dd small { font-size: 0.8125rem; font-weight: 500; color: var(--ink-3); letter-spacing: 0; margin-left: 2px; }
.stat .sub { font-size: 0.75rem; color: var(--ink-2); }

.goal-track { grid-column: 1 / -1; display: grid; gap: 8px; padding: 14px; background: var(--surface); border: 1px solid var(--line); border-radius: var(--radius); }
.goal-track .row { display: flex; justify-content: space-between; font-size: 0.8125rem; color: var(--ink-2); }

.chart-card { margin: 0; padding: 16px 14px 10px; background: var(--surface); border: 1px solid var(--line); border-radius: var(--radius); }
.chart-card figcaption { margin-bottom: 8px; padding: 0 2px; }
.chart-card .section-title { margin-bottom: 4px; }
.chart { position: relative; width: 100%; }
.chart svg { display: block; width: 100%; height: auto; overflow: visible; }
.chart .grid { stroke: var(--line); stroke-width: 1; }
.chart .axis-text { fill: var(--ink-3); font-size: 11px; }
.chart .label-text { fill: var(--ink-2); font-size: 11px; font-weight: 600; }
.chart .empty-text { fill: var(--ink-3); font-size: 13px; }
.chart .series { stroke: var(--accent); stroke-width: 2; fill: none; stroke-linejoin: round; stroke-linecap: round; }
.chart .dot { fill: var(--accent); stroke: var(--surface); stroke-width: 2; }
.chart .raw { fill: var(--ink-3); opacity: 0.45; }
.chart .goal { stroke: var(--ink-2); stroke-width: 1.5; stroke-dasharray: 4 4; }
.chart .bar { fill: var(--accent); }
.chart .bar.over { fill: var(--over); }
.chart .target { stroke: var(--ink); stroke-width: 1.5; stroke-dasharray: 4 3; opacity: 0.7; }
.chart .cross { stroke: var(--ink-3); stroke-width: 1; }
.chart .hit { fill: transparent; }
.legend { display: flex; flex-wrap: wrap; gap: 4px 14px; font-size: 0.75rem; color: var(--ink-2); }
.legend span { display: inline-flex; align-items: center; gap: 6px; }
.legend i { display: inline-block; width: 14px; height: 2px; background: var(--accent); border-radius: 2px; }
.legend i.dot { width: 7px; height: 7px; border-radius: 50%; background: var(--ink-3); opacity: 0.6; }
.legend i.dash { background: repeating-linear-gradient(90deg, var(--ink-2) 0 4px, transparent 4px 7px); }

.tooltip {
  position: fixed; z-index: 50; pointer-events: none;
  padding: 8px 10px; border-radius: 8px; font-size: 0.8125rem; line-height: 1.4;
  background: var(--ink); color: var(--bg); box-shadow: 0 6px 20px rgb(0 0 0 / 0.18);
  transform: translate(-50%, calc(-100% - 12px)); white-space: nowrap;
}
.tooltip b { font-weight: 650; }

.table-view summary { cursor: pointer; color: var(--ink-2); font-size: 0.875rem; padding: 4px 0; }
.table-view table { width: 100%; border-collapse: collapse; margin-top: 8px; font-size: 0.9375rem; }
.table-view th, .table-view td { text-align: left; padding: 8px 4px; border-bottom: 1px solid var(--line); }
.table-view th { font-size: 0.75rem; color: var(--ink-3); font-weight: 600; }
.table-view td:last-child, .table-view th:last-child { text-align: right; }

/* ---------- settings ---------- */

.settings { display: grid; gap: 16px; padding-top: 16px; }
.grid2 { display: grid; grid-template-columns: 1fr 1fr; gap: 14px; }
@media (max-width: 420px) { .grid2 { grid-template-columns: 1fr; } }
.settings-foot { display: flex; justify-content: flex-end; align-items: center; gap: 14px; }
.plan { padding: 16px; border-radius: var(--radius); background: var(--sunken); display: grid; gap: 10px; font-size: 0.9375rem; }
.plan dl { margin: 0; display: grid; grid-template-columns: 1fr auto; gap: 6px 12px; }
.plan dt { color: var(--ink-2); }
.plan dd { margin: 0; font-weight: 600; text-align: right; }
#logout { justify-self: start; }

@media (max-width: 400px) {
  .tabs button { padding: 6px 9px; }
  .composer-foot { flex-direction: column; align-items: stretch; }
}

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