:root {
  --bg: #f4f6f2;
  --surface: #ffffff;
  --surface-2: #eef2ec;
  --ink: #18201c;
  --muted: #5d6b63;
  --line: #dce3dc;
  --accent: #2f7d5b;
  --accent-ink: #ffffff;
  --accent-soft: #e2f1e8;
  --warn: #b45309;
  --warn-soft: #fdf1e2;
  --danger: #b42318;
  --ok: #1f7a4d;
  --radius: 16px;
  --shadow: 0 1px 2px rgba(20, 30, 25, .06), 0 4px 16px rgba(20, 30, 25, .05);
  --font: system-ui, -apple-system, "Segoe UI", "Noto Sans Thai", "Noto Sans", sans-serif;
  color-scheme: light;
}
@media (prefers-color-scheme: dark) {
  :root {
    --bg: #111613;
    --surface: #1a211d;
    --surface-2: #222b26;
    --ink: #e8eee9;
    --muted: #9aaba1;
    --line: #2d3832;
    --accent: #5cc08e;
    --accent-ink: #0d1a13;
    --accent-soft: #1d3428;
    --warn: #f0a24b;
    --warn-soft: #3a2a17;
    --danger: #f27166;
    --ok: #5cc08e;
    --shadow: none;
    color-scheme: dark;
  }
}

* { box-sizing: border-box; }
[hidden] { display: none !important; }
html, body { margin: 0; }
body {
  background: var(--bg);
  color: var(--ink);
  font: 16px/1.6 var(--font);
  -webkit-text-size-adjust: 100%;
  -webkit-tap-highlight-color: transparent;
}
.shell { max-width: 640px; margin: 0 auto; min-height: 100dvh; padding-bottom: calc(72px + env(safe-area-inset-bottom)); }

.top {
  position: sticky; top: 0; z-index: 5;
  display: flex; align-items: center; gap: 12px;
  padding: calc(10px + env(safe-area-inset-top)) 16px 10px;
  background: color-mix(in srgb, var(--bg) 92%, transparent);
  backdrop-filter: blur(8px);
}
.top h1 { font-size: 20px; margin: 0; font-weight: 700; flex: 1; }
.top .back { min-height: 40px; min-width: 40px; padding: 4px; margin-left: -8px; }
.net { font-size: 13px; color: var(--warn); background: var(--warn-soft); padding: 2px 10px; border-radius: 99px; }

main { padding: 4px 16px 24px; }

.tabs {
  position: fixed; left: 0; right: 0; bottom: 0; z-index: 10;
  display: grid; grid-template-columns: repeat(4, 1fr);
  max-width: 640px; margin: 0 auto;
  background: var(--surface);
  border-top: 1px solid var(--line);
  padding-bottom: env(safe-area-inset-bottom);
}
.tabs a {
  display: flex; flex-direction: column; align-items: center; gap: 2px;
  padding: 8px 0 6px; min-height: 60px;
  color: var(--muted); text-decoration: none; font-size: 13px;
}
.tabs a svg { width: 24px; height: 24px; }
.tabs a.on { color: var(--accent); font-weight: 600; }

.card {
  background: var(--surface);
  border-radius: var(--radius);
  box-shadow: var(--shadow);
  border: 1px solid var(--line);
  padding: 16px;
  margin: 12px 0;
}
.card h2 { font-size: 17px; margin: 0 0 8px; }
.muted { color: var(--muted); }
.small { font-size: 14px; }
.row { display: flex; align-items: center; gap: 8px; }
.row.wrap { flex-wrap: wrap; }
.spread { justify-content: space-between; }
.grow { flex: 1; min-width: 0; }
.center { text-align: center; }

button, .btn {
  font: inherit; font-weight: 600;
  border: 1px solid var(--line);
  background: var(--surface);
  color: var(--ink);
  border-radius: 12px;
  padding: 10px 16px;
  min-height: 46px;
  cursor: pointer;
  display: inline-flex; align-items: center; justify-content: center; gap: 8px;
  text-decoration: none;
}
button:disabled { opacity: .55; cursor: default; }
button.primary { background: var(--accent); border-color: var(--accent); color: var(--accent-ink); }
button.danger { color: var(--danger); }
button.ghost { background: transparent; border-color: transparent; }
button.block { width: 100%; }
button.big { min-height: 64px; font-size: 18px; border-radius: 16px; }
button svg { width: 22px; height: 22px; flex: none; }

input, textarea, select {
  font: inherit; color: var(--ink);
  background: var(--surface);
  border: 1px solid var(--line);
  border-radius: 12px;
  padding: 10px 12px;
  min-height: 46px;
  width: 100%;
}
textarea { min-height: 76px; resize: vertical; }
input:focus, textarea:focus, select:focus, button:focus-visible { outline: 2px solid var(--accent); outline-offset: 1px; }
label { display: block; font-size: 14px; color: var(--muted); margin: 10px 0 4px; }
label > input, label > select, label > textarea { margin-top: 4px; color: var(--ink); font-size: 16px; }
.field-row { display: grid; grid-template-columns: 1fr 1fr; gap: 10px; }

/* Day header */
.datebar { display: flex; align-items: center; justify-content: space-between; margin: 4px 0 0; }
.datebar .when { font-weight: 700; font-size: 17px; }
.datebar button { min-width: 46px; padding: 8px; }

.total { display: flex; align-items: baseline; gap: 8px; flex-wrap: wrap; }
.total .num { font-size: 38px; font-weight: 800; line-height: 1.1; letter-spacing: -.5px; font-variant-numeric: tabular-nums; }
.total .of { color: var(--muted); }
.bar { height: 10px; border-radius: 99px; background: var(--surface-2); overflow: hidden; margin: 10px 0 8px; }
.bar > span { display: block; height: 100%; background: var(--accent); border-radius: 99px; width: 0; transition: width .4s; }
.bar.over > span { background: var(--warn); }
.macros { display: flex; gap: 14px; flex-wrap: wrap; font-size: 14px; color: var(--muted); }
.macros b { color: var(--ink); font-variant-numeric: tabular-nums; }
.notice { font-size: 14px; background: var(--warn-soft); color: var(--warn); border-radius: 10px; padding: 8px 12px; margin-top: 10px; }
.notice.info { background: var(--accent-soft); color: var(--accent); }

/* Capture */
.capture { display: grid; gap: 10px; }
.capture .two { display: grid; grid-template-columns: 1fr 1fr; gap: 10px; }
.compose img.preview { width: 100%; max-height: 52vh; object-fit: contain; border-radius: 12px; background: var(--surface-2); display: block; }
.chips { display: flex; gap: 8px; flex-wrap: wrap; margin: 10px 0 2px; }
.chip {
  border: 1px solid var(--line); background: var(--surface); color: var(--ink);
  border-radius: 99px; padding: 6px 16px; min-height: 40px; font-weight: 500;
}
.chip.on { background: var(--accent); color: var(--accent-ink); border-color: var(--accent); }
.progress { font-size: 14px; color: var(--muted); }
.error { color: var(--danger); font-size: 14px; margin: 8px 0 0; }

/* Meal list */
.meals { display: grid; gap: 10px; margin: 12px 0; }
.meal {
  display: grid; grid-template-columns: 76px 1fr auto; gap: 12px; align-items: center;
  background: var(--surface); border: 1px solid var(--line); border-radius: var(--radius);
  padding: 10px; color: inherit; text-decoration: none; box-shadow: var(--shadow);
}
.meal .thumb { width: 76px; height: 76px; border-radius: 12px; object-fit: cover; background: var(--surface-2); display: grid; place-items: center; color: var(--muted); font-size: 12px; }
.meal .meta { font-size: 14px; color: var(--muted); }
.meal .note { overflow: hidden; text-overflow: ellipsis; display: -webkit-box; -webkit-line-clamp: 2; -webkit-box-orient: vertical; line-height: 1.5; }
.meal .kcal { text-align: right; font-weight: 700; font-variant-numeric: tabular-nums; white-space: nowrap; }
.badge { display: inline-block; font-size: 12px; font-weight: 600; border-radius: 99px; padding: 1px 9px; line-height: 1.7; }
.badge.pending { background: var(--warn-soft); color: var(--warn); }
.badge.estimated { background: var(--accent-soft); color: var(--accent); }
.badge.confirmed { background: var(--surface-2); color: var(--ok); }
.empty { text-align: center; color: var(--muted); padding: 24px 8px; }

/* Meal detail */
.photo-full { width: 100%; border-radius: 12px; display: block; background: var(--surface-2); }
table { width: 100%; border-collapse: collapse; font-size: 15px; }
th, td { text-align: left; padding: 8px 4px; border-bottom: 1px solid var(--line); vertical-align: top; }
th { font-size: 13px; color: var(--muted); font-weight: 600; }
td.n, th.n { text-align: right; font-variant-numeric: tabular-nums; white-space: nowrap; }
.actions { display: grid; gap: 10px; margin-top: 12px; }

/* Markdown report */
.md h1, .md h2, .md h3 { font-size: 17px; margin: 14px 0 6px; }
.md p { margin: 6px 0; }
.md ul, .md ol { margin: 6px 0; padding-left: 22px; }
.md li { margin: 3px 0; }
.md code { background: var(--surface-2); padding: 0 5px; border-radius: 6px; font-size: 14px; }
.report-head { display: flex; justify-content: space-between; align-items: baseline; gap: 8px; }
.report-head .k { font-size: 14px; color: var(--muted); white-space: nowrap; }

/* Charts */
.chart { width: 100%; height: auto; display: block; }
.chart text { font: 11px var(--font); fill: var(--muted); }
.chart .grid { stroke: var(--line); stroke-width: 1; }
.chart .target { stroke: var(--warn); stroke-width: 1.5; stroke-dasharray: 5 4; }
.chart .goal { stroke: var(--accent); stroke-width: 1.5; stroke-dasharray: 5 4; opacity: .7; }
.chart .bar-k { fill: var(--accent); }
.chart .bar-k.over { fill: var(--warn); }
.chart .dot { fill: var(--surface); stroke: var(--muted); stroke-width: 1.5; }
.chart .avg { fill: none; stroke: var(--accent); stroke-width: 2.5; stroke-linejoin: round; stroke-linecap: round; }
.legend { display: flex; gap: 14px; flex-wrap: wrap; font-size: 13px; color: var(--muted); margin-top: 6px; }
.legend i { display: inline-block; width: 14px; height: 3px; vertical-align: middle; margin-right: 5px; border-radius: 2px; }
.legend i.a { background: var(--accent); }
.legend i.w { background: var(--warn); }
.legend i.d { width: 9px; height: 9px; border-radius: 50%; border: 1.5px solid var(--muted); }

.list-row { display: flex; align-items: center; gap: 10px; padding: 10px 0; border-bottom: 1px solid var(--line); }
.list-row:last-child { border-bottom: 0; }
.kv { display: grid; grid-template-columns: auto 1fr; gap: 4px 14px; font-size: 15px; }
.kv dt { color: var(--muted); }
.kv dd { margin: 0; font-weight: 600; font-variant-numeric: tabular-nums; }
.copy { display: flex; gap: 8px; }
.copy input { font-size: 14px; }

.login { max-width: 380px; margin: 12vh auto 0; }
.login .logo { width: 72px; height: 72px; border-radius: 18px; display: block; margin: 0 auto 8px; }
.toast {
  position: fixed; left: 50%; bottom: calc(84px + env(safe-area-inset-bottom)); transform: translateX(-50%);
  background: var(--ink); color: var(--bg); padding: 10px 18px; border-radius: 12px; font-size: 15px;
  z-index: 50; max-width: calc(100% - 32px); box-shadow: 0 6px 24px rgba(0,0,0,.2);
}
.spin { display: inline-block; width: 18px; height: 18px; border-radius: 50%; border: 2.5px solid currentColor; border-right-color: transparent; animation: spin .8s linear infinite; }
@keyframes spin { to { transform: rotate(360deg); } }
@media (prefers-reduced-motion: reduce) { .spin { animation-duration: 2s; } .bar > span { transition: none; } }
