:root {
  --bg: #f5f4ef;
  --card: #ffffff;
  --ink: #1d2320;
  --muted: #6b726e;
  --line: #e4e2db;
  --chip: #ecebe4;
  --accent: #1f7a57;
  --accent-ink: #ffffff;
  --kcal: #c9731f;
  --protein: #1f7a57;
  --danger: #b83a34;
  --est: #8a6d1f;
  --est-bg: #f6edd3;
  --shadow: 0 1px 2px rgba(20, 30, 25, .06), 0 2px 10px rgba(20, 30, 25, .04);
  --radius: 16px;
  --tabbar: 64px;
  color-scheme: light;
}

@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) {
    --bg: #0f1412;
    --card: #171d1a;
    --ink: #e8ece9;
    --muted: #95a09a;
    --line: #26302b;
    --chip: #1f2723;
    --accent: #3fb984;
    --accent-ink: #08130e;
    --kcal: #f0a04b;
    --protein: #3fb984;
    --danger: #ef6b64;
    --est: #e2c06b;
    --est-bg: #2c2717;
    --shadow: none;
    color-scheme: dark;
  }
}

:root[data-theme="dark"] {
  --bg: #0f1412; --card: #171d1a; --ink: #e8ece9; --muted: #95a09a; --line: #26302b; --chip: #1f2723;
  --accent: #3fb984; --accent-ink: #08130e; --kcal: #f0a04b; --protein: #3fb984; --danger: #ef6b64;
  --est: #e2c06b; --est-bg: #2c2717; --shadow: none; color-scheme: dark;
}

* { box-sizing: border-box; }

html, body { margin: 0; }

body {
  background: var(--bg);
  color: var(--ink);
  font: 16px/1.4 system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
  -webkit-font-smoothing: antialiased;
  -webkit-tap-highlight-color: transparent;
}

button, input, select, textarea { font: inherit; color: inherit; }

b, .num { font-variant-numeric: tabular-nums; }

.muted { color: var(--muted); }
.small { font-size: 13px; }
.center { text-align: center; }

.boot {
  display: grid; place-items: center; height: 100vh; font-weight: 700; font-size: 22px; color: var(--muted);
}

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

.shell { max-width: 560px; margin: 0 auto; min-height: 100vh; }

.topbar {
  position: sticky; top: 0; z-index: 5;
  display: flex; align-items: center; justify-content: space-between; gap: 8px;
  padding: calc(env(safe-area-inset-top) + 8px) 16px 8px;
  background: color-mix(in srgb, var(--bg) 88%, transparent);
  backdrop-filter: blur(12px); -webkit-backdrop-filter: blur(12px);
}
.topbar h1 { font-size: 20px; margin: 0; }

.datenav { display: flex; align-items: center; gap: 4px; }
.datenav .day { font-weight: 700; font-size: 18px; min-width: 120px; text-align: center; background: none; border: 0; padding: 6px; }

.iconbtn {
  width: 40px; height: 40px; border-radius: 50%; border: 0; background: transparent; display: grid; place-items: center;
  cursor: pointer; color: var(--ink);
}
.iconbtn:disabled { opacity: .25; }
.iconbtn:active { background: var(--chip); }
.iconbtn svg { width: 22px; height: 22px; }

main.view { padding: 4px 16px calc(var(--tabbar) + env(safe-area-inset-bottom) + 24px); }

.tabbar {
  position: fixed; left: 0; right: 0; bottom: 0; z-index: 5;
  display: flex; justify-content: center;
  background: var(--card); border-top: 1px solid var(--line);
  padding-bottom: env(safe-area-inset-bottom);
}
.tabbar-inner { display: flex; width: 100%; max-width: 560px; }
.tab {
  flex: 1; height: var(--tabbar); border: 0; background: none; cursor: pointer;
  display: flex; flex-direction: column; align-items: center; justify-content: center; gap: 2px;
  font-size: 12px; color: var(--muted);
}
.tab svg { width: 24px; height: 24px; }
.tab.on { color: var(--accent); font-weight: 600; }

.card {
  background: var(--card); border-radius: var(--radius); box-shadow: var(--shadow);
  border: 1px solid var(--line); padding: 16px; margin: 12px 0;
}
.card-head { display: flex; justify-content: space-between; align-items: baseline; gap: 8px; margin-bottom: 8px; }
h2 { font-size: 17px; margin: 0; }
.section-title { font-size: 14px; text-transform: uppercase; letter-spacing: .06em; color: var(--muted); margin: 24px 4px 4px; }

/* ---------- buttons and inputs ---------- */

.primary, .ghost, .danger {
  border-radius: 12px; padding: 10px 18px; font-weight: 600; cursor: pointer; border: 1px solid transparent;
  min-height: 44px;
}
.primary { background: var(--accent); color: var(--accent-ink); }
.primary:disabled { opacity: .6; }
.ghost { background: transparent; border-color: var(--line); }
.danger { background: transparent; color: var(--danger); border-color: color-mix(in srgb, var(--danger) 35%, transparent); }
.small-btn { min-height: 36px; padding: 6px 12px; font-size: 14px; }

input[type=text], input[type=password], input[type=date], select, textarea {
  width: 100%; border: 1px solid var(--line); background: var(--bg); border-radius: 10px; padding: 10px 12px;
  min-height: 44px; outline: none;
}
input:focus, select:focus, textarea:focus { border-color: var(--accent); }
textarea { resize: none; min-height: 72px; line-height: 1.4; }
.field { display: block; font-size: 13px; color: var(--muted); }
.field > span { display: block; margin-bottom: 4px; }

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

.summary { display: grid; gap: 14px; }
.meter-top { display: flex; justify-content: space-between; align-items: baseline; }
.meter-label { font-weight: 600; }
.meter-num { color: var(--muted); }
.meter-num b { color: var(--ink); font-size: 20px; }
.bar { height: 10px; border-radius: 99px; background: var(--chip); overflow: hidden; margin: 6px 0 4px; }
.fill { height: 100%; border-radius: 99px; transition: width .3s; }
.meter.kcal .fill { background: var(--kcal); }
.meter.protein .fill { background: var(--protein); }
.fill.over { background: var(--danger) !important; }
.meter-sub { font-size: 13px; color: var(--muted); }
.meter-sub a { color: var(--accent); }
.macros { display: flex; justify-content: space-between; border-top: 1px solid var(--line); padding-top: 12px; }
.macro { text-align: center; flex: 1; }
.macro b { display: block; font-size: 17px; }
.macro span { font-size: 12px; color: var(--muted); }

.composer textarea { border: 0; background: transparent; padding: 0; min-height: 56px; font-size: 17px; }
.composer-row { display: flex; justify-content: space-between; align-items: center; gap: 12px; margin-top: 8px; }
.hint { font-size: 13px; color: var(--muted); }

.chips { display: flex; gap: 8px; overflow-x: auto; padding: 2px 2px 6px; margin: 0 -16px; padding-left: 16px; padding-right: 16px; scrollbar-width: none; }
.chips::-webkit-scrollbar { display: none; }
.chip {
  flex: none; border: 1px solid var(--line); background: var(--card); border-radius: 99px; padding: 8px 14px;
  font-size: 14px; cursor: pointer; white-space: nowrap; min-height: 38px;
}
.chip:active { background: var(--chip); }

.draft { border-color: color-mix(in srgb, var(--accent) 45%, var(--line)); }
.draft-row { border-top: 1px solid var(--line); padding: 12px 0; }
.draft-row:first-of-type { border-top: 0; }
.draft-top { display: flex; gap: 10px; align-items: flex-start; }
.draft-name { flex: 1; min-width: 0; }
.draft-name input { font-weight: 600; padding: 6px 8px; min-height: 36px; }
.draft-name .name { font-weight: 600; padding-top: 2px; }
.row-nums { text-align: right; white-space: nowrap; }
.row-nums b { font-size: 17px; }
.x { border: 0; background: none; color: var(--muted); font-size: 22px; line-height: 1; width: 32px; height: 32px; cursor: pointer; border-radius: 50%; }
.x:active { background: var(--chip); }
.qty-row { display: flex; gap: 8px; align-items: center; margin-top: 8px; flex-wrap: wrap; }
.qty-row input { width: 84px; min-height: 38px; padding: 6px 10px; }
.qty-row select { width: auto; min-height: 38px; padding: 6px 10px; }
.grams { font-size: 13px; color: var(--muted); }
.badge {
  display: inline-block; font-size: 11px; font-weight: 600; letter-spacing: .02em; padding: 2px 8px;
  border-radius: 99px; background: var(--chip); color: var(--muted); margin-left: 6px; vertical-align: 2px;
}
.badge.est { background: var(--est-bg); color: var(--est); }
.badge.lib { color: var(--accent); }
.assumption { font-size: 13px; color: var(--muted); margin-top: 6px; }
.est-tools { display: flex; gap: 14px; align-items: center; margin-top: 8px; flex-wrap: wrap; font-size: 14px; }
.est-tools label { display: flex; gap: 6px; align-items: center; cursor: pointer; }
.est-tools input[type=checkbox] { width: 18px; height: 18px; accent-color: var(--accent); }
.linkbtn { border: 0; background: none; color: var(--accent); padding: 0; cursor: pointer; font-size: 14px; }
.per100 { display: grid; grid-template-columns: repeat(5, 1fr); gap: 6px; margin-top: 10px; }
.per100 label { font-size: 11px; color: var(--muted); }
.per100 input { min-height: 36px; padding: 6px; text-align: center; }
.draft-actions { display: flex; justify-content: flex-end; gap: 10px; margin-top: 8px; border-top: 1px solid var(--line); padding-top: 12px; }

.entry { padding: 10px 16px; }
.entry-head { display: flex; gap: 8px; font-size: 13px; color: var(--muted); padding: 2px 0 6px; }
.entry-head .time { flex: none; font-variant-numeric: tabular-nums; }
.entry-head .raw { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.item { display: flex; justify-content: space-between; gap: 10px; padding: 8px 0; border-top: 1px solid var(--line); cursor: pointer; }
.entry .item:first-of-type { border-top: 0; }
.item-name { font-weight: 500; }
.item-nums { text-align: right; white-space: nowrap; }
.item-edit { display: flex; gap: 8px; align-items: center; padding: 4px 0 10px; flex-wrap: wrap; }
.item-edit input { width: 84px; min-height: 38px; padding: 6px 10px; }
.item-edit select { width: auto; min-height: 38px; padding: 6px 10px; }
.empty { padding: 28px 0; }

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

.inline-form { display: flex; gap: 10px; align-items: center; }
.inline-form input { flex: 1; }
.stats { display: grid; grid-template-columns: repeat(3, 1fr); gap: 8px; margin-top: 14px; }
.stat { background: var(--bg); border-radius: 12px; padding: 10px; }
.stat b { display: block; font-size: 18px; }
.stat span { font-size: 12px; color: var(--muted); }
.down { color: var(--accent); }
.up { color: var(--kcal); }

.chart { width: 100%; height: auto; display: block; margin-top: 8px; }
.chart .grid { stroke: var(--line); stroke-width: 1; }
.chart .axis { fill: var(--muted); font-size: 10px; }
.chart .dot { fill: var(--muted); opacity: .55; }
.chart .trend { fill: none; stroke: var(--accent); stroke-width: 2.5; stroke-linejoin: round; stroke-linecap: round; }
.seg { display: inline-flex; background: var(--chip); border-radius: 10px; padding: 3px; }
.seg button { border: 0; background: none; padding: 4px 10px; border-radius: 8px; font-size: 13px; cursor: pointer; color: var(--muted); }
.seg button.on { background: var(--card); color: var(--ink); font-weight: 600; box-shadow: var(--shadow); }

table.week { width: 100%; border-collapse: collapse; font-size: 14px; font-variant-numeric: tabular-nums; }
.week th { text-align: right; font-weight: 500; color: var(--muted); font-size: 12px; padding: 4px; }
.week th:first-child, .week td:first-child { text-align: left; }
.week td { text-align: right; padding: 8px 4px; border-top: 1px solid var(--line); }
.week td.hit { color: var(--accent); font-weight: 600; }
.week td.over { color: var(--danger); }
.week-summary { font-size: 14px; color: var(--muted); margin-top: 10px; }

.list-row { display: flex; justify-content: space-between; padding: 8px 0; border-top: 1px solid var(--line); font-size: 15px; }
.list-row:first-child { border-top: 0; }

/* ---------- foods ---------- */

.food { display: flex; justify-content: space-between; gap: 10px; padding: 12px 0; border-top: 1px solid var(--line); cursor: pointer; }
.food:first-child { border-top: 0; }
.food-name { font-weight: 600; }
.food-meta { font-size: 13px; color: var(--muted); }
.toolbar { display: flex; gap: 10px; margin-top: 12px; }
.toolbar input { flex: 1; }

/* ---------- sheet ---------- */

.overlay {
  position: fixed; inset: 0; z-index: 20; background: rgba(0, 0, 0, .35);
  display: flex; align-items: flex-end; justify-content: center;
}
.sheet {
  background: var(--card); width: 100%; max-width: 560px; max-height: 92vh; overflow-y: auto;
  border-radius: 20px 20px 0 0; padding: 20px 16px calc(env(safe-area-inset-bottom) + 20px);
}
.sheet h2 { margin-bottom: 14px; }
.form-grid { display: grid; gap: 12px; }
.two { display: grid; grid-template-columns: 1fr 1fr; gap: 10px; }
.sheet-actions { display: flex; gap: 10px; justify-content: space-between; margin-top: 18px; }
.sheet-actions .right { display: flex; gap: 10px; margin-left: auto; }
.note { font-size: 13px; color: var(--muted); margin: 4px 0 0; }

/* ---------- login and toast ---------- */

.login { min-height: 100vh; display: grid; place-items: center; padding: 24px 16px; }
.login .card { width: 100%; max-width: 360px; padding: 24px; }
.login h1 { margin: 0 0 4px; font-size: 26px; }
.login p { margin: 0 0 18px; color: var(--muted); }
.login form { display: grid; gap: 12px; }

#toast {
  position: fixed; left: 50%; bottom: calc(var(--tabbar) + env(safe-area-inset-bottom) + 16px); z-index: 30;
  transform: translate(-50%, 20px); opacity: 0; pointer-events: none; transition: all .2s;
  background: var(--ink); color: var(--bg); padding: 10px 16px; border-radius: 12px; font-size: 14px;
  max-width: calc(100vw - 32px); box-shadow: 0 4px 20px rgba(0, 0, 0, .2);
}
#toast.show { opacity: 1; transform: translate(-50%, 0); }
#toast.error { background: var(--danger); color: #fff; }

.spin { display: inline-block; width: 14px; height: 14px; border: 2px solid currentColor; border-right-color: transparent; border-radius: 50%; animation: spin .7s linear infinite; vertical-align: -2px; margin-right: 6px; }
@keyframes spin { to { transform: rotate(360deg); } }

/* ---------- profile ---------- */

.seg.wide { display: flex; width: 100%; }
.seg.wide button { flex: 1; padding: 8px 4px; font-size: 14px; min-height: 38px; }
.with-unit { display: flex; align-items: center; gap: 8px; }
.with-unit span { color: var(--muted); font-size: 14px; }
.stats.two-up { grid-template-columns: 1fr 1fr; margin-top: 10px; }
.note.warn { color: var(--est); }
.apply-row { display: flex; justify-content: space-between; align-items: center; gap: 12px; margin-top: 14px; padding-top: 12px; border-top: 1px solid var(--line); }
.save-row { display: flex; justify-content: flex-end; align-items: center; gap: 12px; margin: 12px 0; }
.assessment { background: var(--bg); border-radius: 12px; padding: 10px 12px; }
.assessment .note { margin-top: 4px; }
.tabbar .tab { font-size: 11px; }
.apply-row .primary { flex: none; white-space: nowrap; }
.list-row > b { flex: none; white-space: nowrap; padding-left: 12px; }
.measure-grid { display: grid; grid-template-columns: repeat(3, 1fr); gap: 10px; margin-top: 10px; }
.measure-grid input { min-height: 40px; padding: 8px; }
.since { font-style: normal; color: var(--muted); font-size: 11px; }
details.how { margin-top: 12px; font-size: 14px; }
details.how summary { cursor: pointer; color: var(--accent); }
details.how ul { margin: 8px 0 0; padding-left: 18px; color: var(--muted); }
details.how li { margin: 4px 0; }
.report .headline { font-size: 17px; font-weight: 600; margin: 4px 0 10px; line-height: 1.35; }
.report-section h3 { font-size: 14px; margin: 14px 0 4px; }
.report-section p { margin: 0; color: var(--ink); font-size: 15px; line-height: 1.5; }
