:root {
  --bg: #f4f6f5;
  --card: #ffffff;
  --text: #17201d;
  --muted: #64706b;
  --line: #e2e7e4;
  --accent: #0f766e;
  --accent-soft: #d5efeb;
  --warn: #b45309;
  --warn-soft: #fdecd3;
  --danger: #b91c1c;
  --bar: #e8ecea;
  --protein: #7c3aed;
  --shadow: 0 1px 2px rgba(0, 0, 0, .05), 0 2px 8px rgba(0, 0, 0, .04);
}
@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) {
    --bg: #0e1412;
    --card: #17201d;
    --text: #e8eeeb;
    --muted: #93a09a;
    --line: #26322e;
    --accent: #2dd4bf;
    --accent-soft: #123b36;
    --warn: #f59e0b;
    --warn-soft: #3a2a10;
    --danger: #f87171;
    --bar: #24302c;
    --protein: #a78bfa;
    --shadow: none;
  }
}
:root[data-theme="dark"] {
  --bg: #0e1412; --card: #17201d; --text: #e8eeeb; --muted: #93a09a; --line: #26322e;
  --accent: #2dd4bf; --accent-soft: #123b36; --warn: #f59e0b; --warn-soft: #3a2a10;
  --danger: #f87171; --bar: #24302c; --protein: #a78bfa; --shadow: none;
}

* { box-sizing: border-box; }
html { -webkit-text-size-adjust: 100%; }
body {
  margin: 0;
  background: var(--bg);
  color: var(--text);
  font: 15px/1.6 "Vazirmatn", system-ui, -apple-system, sans-serif;
  padding: env(safe-area-inset-top) 0 calc(72px + env(safe-area-inset-bottom));
}
main { max-width: 640px; margin: 0 auto; padding: 12px 16px 24px; }
h1 { font-size: 20px; margin: 4px 0 12px; }
h2 { font-size: 16px; margin: 0 0 8px; }
h3 { font-size: 14px; margin: 0; }
p { margin: 0 0 8px; }
.muted { color: var(--muted); }
.small { font-size: 13px; }
.num { font-variant-numeric: tabular-nums; }

.card {
  background: var(--card);
  border-radius: 16px;
  padding: 14px 16px;
  margin-bottom: 12px;
  box-shadow: var(--shadow);
  border: 1px solid var(--line);
}
.row { display: flex; align-items: center; gap: 8px; }
.between { justify-content: space-between; }
.wrap { flex-wrap: wrap; }
.grow { flex: 1; min-width: 0; }

button, .btn {
  font: inherit;
  border: 0;
  border-radius: 12px;
  padding: 9px 14px;
  background: var(--accent);
  color: #fff;
  cursor: pointer;
  touch-action: manipulation;
}
@media (prefers-color-scheme: dark) { :root:not([data-theme="light"]) button, :root:not([data-theme="light"]) .btn { color: #06201c; } }
button.ghost { background: transparent; color: var(--accent); padding: 6px 10px; }
button.soft { background: var(--accent-soft); color: var(--accent); }
button.danger { background: transparent; color: var(--danger); }
button:disabled { opacity: .5; }
button.icon { background: var(--card); color: var(--text); border: 1px solid var(--line); width: 40px; height: 40px; padding: 0; font-size: 18px; }

input, select, textarea {
  font: inherit;
  width: 100%;
  padding: 9px 11px;
  border-radius: 10px;
  border: 1px solid var(--line);
  background: var(--bg);
  color: var(--text);
}
textarea { min-height: 70px; resize: vertical; }
label { display: block; font-size: 13px; color: var(--muted); margin-bottom: 8px; }
label > input, label > select, label > textarea { margin-top: 3px; }
.grid2 { display: grid; grid-template-columns: 1fr 1fr; gap: 0 10px; }
.grid3 { display: grid; grid-template-columns: repeat(3, 1fr); gap: 0 10px; }
.grid2 > label, .grid3 > label { display: flex; flex-direction: column; justify-content: space-between; }

/* date header */
.datebar { display: flex; align-items: center; gap: 8px; margin-bottom: 12px; }
.datebar .title { flex: 1; text-align: center; }
.datebar .title b { display: block; font-size: 17px; }

/* summary */
.big { font-size: 30px; font-weight: 700; line-height: 1.2; }
.progress { height: 10px; background: var(--bar); border-radius: 6px; overflow: hidden; margin: 8px 0 4px; }
.progress > i { display: block; height: 100%; background: var(--accent); border-radius: 6px; }
.progress.over > i { background: var(--warn); }
.progress.protein > i { background: var(--protein); }
.macros { display: grid; grid-template-columns: repeat(4, 1fr); gap: 6px; margin-top: 10px; text-align: center; }
.macros div { background: var(--bg); border-radius: 10px; padding: 6px 2px; }
.macros b { display: block; font-size: 16px; }
.activity { display: flex; gap: 14px; flex-wrap: wrap; margin-top: 10px; padding-top: 10px; border-top: 1px solid var(--line); font-size: 13px; color: var(--muted); }
.activity b { color: var(--text); }

/* groups */
.groups { display: grid; grid-template-columns: 1fr 1fr; gap: 8px 14px; }
.group .progress { height: 7px; margin: 3px 0 0; }

/* meals */
.meal-head { display: flex; align-items: center; justify-content: space-between; }
.entry { display: flex; align-items: center; gap: 10px; padding: 8px 0; border-top: 1px solid var(--line); cursor: pointer; }
.entry:first-of-type { border-top: 0; }
.entry .name { flex: 1; min-width: 0; }
.entry .name span { display: block; font-size: 12px; color: var(--muted); }
.entry .kcal { font-weight: 500; white-space: nowrap; }
.entries { margin-top: 4px; }
.tag { font-size: 11px; background: var(--accent-soft); color: var(--accent); padding: 1px 7px; border-radius: 8px; margin-inline-start: 4px; }

.tip { border-inline-start: 4px solid var(--accent); }
.tip .practice { background: var(--accent-soft); border-radius: 10px; padding: 8px 10px; margin-top: 8px; }

/* tabs */
.tabs {
  position: fixed; inset: auto 0 0 0;
  display: grid; grid-template-columns: repeat(4, 1fr);
  background: var(--card); border-top: 1px solid var(--line);
  padding: 4px 0 calc(4px + env(safe-area-inset-bottom));
  z-index: 5;
}
.tabs button { background: none; color: var(--muted); font-size: 12px; padding: 4px; border-radius: 0; display: flex; flex-direction: column; align-items: center; }
.tabs .ico { font-size: 20px; line-height: 1.3; filter: grayscale(1); opacity: .7; }
.tabs button.active { color: var(--accent); }
.tabs button.active .ico { filter: none; opacity: 1; }

/* sheet */
dialog {
  border: 0; padding: 0; width: 100%; max-width: 640px;
  margin: auto auto 0; border-radius: 20px 20px 0 0;
  background: var(--card); color: var(--text);
  max-height: 92vh;
}
dialog::backdrop { background: rgba(0, 0, 0, .4); }
#sheet-body { padding: 16px 16px calc(16px + env(safe-area-inset-bottom)); overflow-y: auto; max-height: 92vh; }
.seg { display: flex; background: var(--bg); border-radius: 12px; padding: 3px; margin-bottom: 12px; }
.seg button { flex: 1; background: none; color: var(--muted); padding: 7px; }
.seg button.on { background: var(--card); color: var(--text); box-shadow: var(--shadow); }
.photo-preview { width: 100%; max-height: 240px; object-fit: cover; border-radius: 12px; margin-bottom: 8px; }
.est-item { border: 1px solid var(--line); border-radius: 12px; padding: 10px; margin-bottom: 8px; }
.est-item.off { opacity: .45; }
.est-item .grid3 label, .est-item .grid2 label { margin-bottom: 4px; }
.conf { font-size: 12px; padding: 2px 8px; border-radius: 8px; background: var(--warn-soft); color: var(--warn); }
.conf.high { background: var(--accent-soft); color: var(--accent); }
.steppers { display: grid; grid-template-columns: 1fr 1fr; gap: 6px 12px; }
.stepper { display: flex; align-items: center; justify-content: space-between; font-size: 13px; }
.stepper div { display: flex; align-items: center; gap: 6px; }
.stepper button { width: 30px; height: 30px; padding: 0; border-radius: 8px; background: var(--bg); color: var(--text); border: 1px solid var(--line); }

/* charts */
.chart svg { width: 100%; height: auto; display: block; direction: ltr; }
.chart .axis { fill: var(--muted); font-size: 21px; font-family: inherit; }
.chart .grid { stroke: var(--line); stroke-width: 1; }
.chart .line { fill: none; stroke: var(--accent); stroke-width: 2.5; stroke-linejoin: round; stroke-linecap: round; }
.chart .dot { fill: var(--accent); }
.chart .bar { fill: var(--accent); }
.chart .bar.over { fill: var(--warn); }
.chart .target { stroke: var(--muted); stroke-dasharray: 4 4; stroke-width: 1.5; }
.ranges { display: flex; gap: 6px; }
.ranges button { padding: 4px 10px; font-size: 12px; background: var(--bg); color: var(--muted); }
.ranges button.on { background: var(--accent-soft); color: var(--accent); }

.stats { display: grid; grid-template-columns: repeat(2, 1fr); gap: 8px; }
.stats div { background: var(--bg); border-radius: 12px; padding: 8px 10px; }
.stats b { display: block; font-size: 18px; }
ul.clean { margin: 0; padding-inline-start: 18px; }
ul.clean li { margin-bottom: 4px; }
.change { background: var(--accent-soft); border-radius: 12px; padding: 8px 10px; margin-bottom: 6px; }
.change span { display: block; font-size: 13px; color: var(--muted); }

.list-row { display: flex; align-items: center; gap: 8px; padding: 6px 0; border-top: 1px solid var(--line); font-size: 14px; }
details summary { cursor: pointer; font-weight: 500; }
details ol { padding-inline-start: 20px; margin: 8px 0; }
details li { margin-bottom: 6px; }
code { background: var(--bg); padding: 1px 5px; border-radius: 5px; font-size: 13px; direction: ltr; unicode-bidi: embed; }
pre { background: var(--bg); padding: 10px; border-radius: 10px; overflow-x: auto; direction: ltr; text-align: left; font-size: 12px; }

#toast {
  position: fixed; left: 50%; bottom: calc(84px + env(safe-area-inset-bottom));
  transform: translateX(-50%) translateY(20px);
  background: var(--text); color: var(--bg);
  padding: 8px 16px; border-radius: 12px; font-size: 14px;
  opacity: 0; pointer-events: none; transition: .2s; z-index: 20; max-width: 90vw;
}
#toast.show { opacity: 1; transform: translateX(-50%); }
.loading { text-align: center; padding: 30px; color: var(--muted); }
.empty { color: var(--muted); font-size: 13px; padding: 4px 0; }
