:root {
  --bg: #f4f6f5;
  --card: #ffffff;
  --text: #15201d;
  --muted: #66736f;
  --line: #e3e8e6;
  --primary: #0f766e;
  --primary-ink: #ffffff;
  --good: #15803d;
  --warn: #b45309;
  --bad: #b91c1c;
  --def: #0f766e;
  --sur: #dc6b2f;
}
@media (prefers-color-scheme: dark) {
  :root {
    --bg: #0e1412;
    --card: #17201d;
    --text: #e8efec;
    --muted: #93a19c;
    --line: #26322e;
    --primary: #2dd4bf;
    --primary-ink: #06201c;
    --good: #4ade80;
    --warn: #fbbf24;
    --bad: #f87171;
    --def: #2dd4bf;
    --sur: #fb923c;
  }
}
* { box-sizing: border-box; }
[hidden] { display: none !important; }
html, body { margin: 0; }
body {
  background: var(--bg);
  color: var(--text);
  font: 16px/1.4 system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
  padding-bottom: calc(72px + env(safe-area-inset-bottom));
}
h2 { font-size: 1rem; margin: 0 0 8px; }
.muted { color: var(--muted); }
.small { font-size: 0.85rem; }

.top {
  position: sticky; top: 0; z-index: 5;
  display: flex; align-items: center; justify-content: space-between;
  padding: 10px 16px; background: var(--bg);
}
.date-wrap { position: relative; text-align: center; }
.date-label { font-weight: 600; }
.date-wrap input { position: absolute; inset: 0; opacity: 0; width: 100%; }
.icon-btn {
  width: 40px; height: 40px; border-radius: 50%; border: 1px solid var(--line);
  background: var(--card); color: var(--text); font-size: 1.4rem; line-height: 1;
}

main { padding: 0 16px; max-width: 560px; margin: 0 auto; }
.view { display: none; }
.view.active { display: block; }

.card {
  background: var(--card); border: 1px solid var(--line); border-radius: 16px;
  padding: 16px; margin-bottom: 12px;
}
.row-between { display: flex; justify-content: space-between; align-items: center; }

.summary { text-align: center; }
.balance-value { font-size: 2.6rem; font-weight: 700; font-variant-numeric: tabular-nums; }
.balance-label { font-weight: 600; margin-bottom: 12px; }
.balance.good .balance-value, .balance.good .balance-label { color: var(--good); }
.balance.warn .balance-value, .balance.warn .balance-label { color: var(--warn); }
.balance.bad .balance-value, .balance.bad .balance-label { color: var(--bad); }
.stats { display: flex; justify-content: center; gap: 16px; align-items: center; margin-bottom: 12px; }
.stats .k { display: block; font-size: 0.8rem; color: var(--muted); }
.stats .v { font-size: 1.3rem; font-weight: 600; font-variant-numeric: tabular-nums; }
.stats .op { color: var(--muted); font-size: 1.3rem; }
.bar { position: relative; height: 10px; background: var(--line); border-radius: 5px; overflow: visible; margin: 4px 0 8px; }
#barFill { height: 100%; border-radius: 5px; background: var(--primary); width: 0; transition: width .3s; }
#barTarget { position: absolute; top: -4px; width: 2px; height: 18px; background: var(--text); opacity: .5; }
.macros { display: flex; justify-content: center; gap: 14px; margin-top: 10px; font-size: 0.85rem; color: var(--muted); }
.macros b { color: var(--text); }

.actions { display: grid; gap: 10px; margin-bottom: 12px; }
.btn {
  display: inline-flex; align-items: center; justify-content: center; gap: 8px;
  padding: 12px 16px; border-radius: 12px; border: 1px solid var(--line);
  background: var(--card); color: var(--text); font: inherit; font-weight: 600; cursor: pointer;
}
.btn.primary { background: var(--primary); color: var(--primary-ink); border-color: var(--primary); }
.btn.big { padding: 16px; font-size: 1.05rem; }
.btn.danger { color: var(--bad); }
.two { display: grid; grid-template-columns: 1fr 1fr; gap: 10px; }
.two .btn.big { font-size: .95rem; padding: 16px 8px; }
.chips { display: flex; flex-wrap: wrap; gap: 6px; margin: 6px 0 4px; }
.chip {
  padding: 6px 12px; border-radius: 99px; border: 1px solid var(--line);
  background: var(--bg); color: var(--text); font: inherit; font-size: .85rem;
}
.chip.on { background: var(--primary); color: var(--primary-ink); border-color: var(--primary); }
.matches { margin: 8px 0; border: 1px solid var(--line); border-radius: 12px; padding: 4px 10px; }
.matches h3 { font-size: .8rem; color: var(--muted); margin: 6px 0 0; font-weight: 600; }
.meal-list li.tap { cursor: pointer; }
.sheet-list { max-height: 50vh; overflow-y: auto; }
.sheet-head { display: flex; justify-content: space-between; align-items: center; margin-bottom: 8px; }
.sheet-head h2 { margin: 0; }
.link { background: none; border: none; color: var(--primary); font: inherit; font-weight: 600; padding: 4px; }

.meal-list { list-style: none; margin: 0; padding: 0; }
.meal-list li { display: flex; gap: 12px; align-items: center; padding: 10px 0; border-top: 1px solid var(--line); }
.meal-list li:first-child { border-top: none; }
.meal-list img, .meal-list .ph {
  width: 52px; height: 52px; border-radius: 10px; object-fit: cover; flex-shrink: 0;
  background: var(--line); display: grid; place-items: center; font-size: 1.4rem;
}
.meal-list .info { flex: 1; min-width: 0; }
.meal-list .name { font-weight: 600; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.meal-list .kcal { font-weight: 700; font-variant-numeric: tabular-nums; }
.empty { color: var(--muted); padding: 8px 0; }

.chart { display: flex; align-items: center; gap: 6px; height: 180px; position: relative; margin: 8px 0; }
.chart::before { content: ""; position: absolute; left: 0; right: 0; top: 50%; border-top: 1px dashed var(--line); }
.chart .col { flex: 1; height: 100%; display: flex; flex-direction: column; align-items: center; position: relative; }
.chart .bar-v { position: absolute; left: 15%; right: 15%; border-radius: 4px; }
.chart .bar-v.def { background: var(--def); }
.chart .bar-v.sur { background: var(--sur); }
.chart .lbl { position: absolute; bottom: -20px; font-size: .75rem; color: var(--muted); }
.chart .val { position: absolute; font-size: .7rem; color: var(--muted); }
.legend { margin-top: 28px; font-size: .8rem; color: var(--muted); display: flex; gap: 6px; align-items: center; }
.sw { display: inline-block; width: 10px; height: 10px; border-radius: 2px; margin-left: 8px; }
.sw.def { background: var(--def); } .sw.sur { background: var(--sur); }

.form label { display: block; margin-bottom: 12px; font-size: .9rem; color: var(--muted); }
input, select, textarea {
  display: block; width: 100%; margin-top: 4px; padding: 10px 12px; font: inherit;
  border: 1px solid var(--line); border-radius: 10px; background: var(--bg); color: var(--text);
}

.tabs {
  position: fixed; bottom: 0; left: 0; right: 0; z-index: 10;
  display: flex; background: var(--card); border-top: 1px solid var(--line);
  padding-bottom: env(safe-area-inset-bottom);
}
.tabs button {
  flex: 1; background: none; border: none; color: var(--muted); padding: 8px 0 10px;
  font-size: 1.3rem; display: flex; flex-direction: column; align-items: center; gap: 2px;
}
.tabs button span { font-size: .75rem; }
.tabs button.active { color: var(--primary); }

dialog {
  border: none; border-radius: 20px 20px 0 0; padding: 0; width: 100%; max-width: 560px;
  margin: auto auto 0; max-height: 90vh; background: var(--card); color: var(--text);
}
dialog::backdrop { background: rgb(0 0 0 / .45); }
#sheetForm { padding: 18px 16px calc(18px + env(safe-area-inset-bottom)); }
.sheet-img { width: 100%; max-height: 200px; object-fit: cover; border-radius: 12px; margin-bottom: 10px; }
.item-row { display: grid; grid-template-columns: 1fr 84px 32px; gap: 8px; align-items: end; margin-bottom: 8px; }
.item-row input { margin-top: 0; }
.item-row .portion { grid-column: 1 / -1; font-size: .8rem; color: var(--muted); margin-top: -4px; }
.item-row .x { background: none; border: none; color: var(--muted); font-size: 1.2rem; padding-bottom: 8px; }
.sheet-total { display: flex; justify-content: space-between; font-weight: 700; font-size: 1.1rem; padding: 10px 0; border-top: 1px solid var(--line); margin-top: 6px; }
.sheet-actions { display: grid; grid-template-columns: 1fr 2fr; gap: 10px; margin-top: 10px; }
.badge { display: inline-block; font-size: .75rem; padding: 2px 8px; border-radius: 99px; background: var(--line); color: var(--muted); }

.loading {
  position: fixed; inset: 0; z-index: 50; background: rgb(0 0 0 / .55); color: #fff;
  display: flex; flex-direction: column; align-items: center; justify-content: center; gap: 14px;
}
.loading[hidden] { display: none; }
.spinner { width: 44px; height: 44px; border: 4px solid rgb(255 255 255 / .3); border-top-color: #fff; border-radius: 50%; animation: spin 0.9s linear infinite; }
@keyframes spin { to { transform: rotate(360deg); } }
.login {
  position: fixed; inset: 0; z-index: 100; background: var(--bg);
  display: flex; align-items: center; justify-content: center; padding: 16px;
}
.login .card { width: 100%; max-width: 360px; }
.toast {
  position: fixed; left: 16px; right: 16px; bottom: calc(84px + env(safe-area-inset-bottom)); z-index: 60;
  background: var(--text); color: var(--bg); padding: 12px 16px; border-radius: 12px; text-align: center;
}
.toast[hidden] { display: none; }
