:root {
  --bg: var(--tg-theme-bg-color, #ffffff);
  --bg2: var(--tg-theme-secondary-bg-color, #f2f2f5);
  --section: var(--tg-theme-section-bg-color, var(--bg));
  --text: var(--tg-theme-text-color, #111111);
  --hint: var(--tg-theme-hint-color, #8a8a8e);
  --link: var(--tg-theme-link-color, #2a7fd4);
  --accent: var(--tg-theme-button-color, #2a7fd4);
  --accent-text: var(--tg-theme-button-text-color, #ffffff);
  --sep: var(--tg-theme-section-separator-color, rgba(128, 128, 128, 0.2));
  --destructive: var(--tg-theme-destructive-text-color, #d03b3b);
  /* Status colors: always paired with an icon and a value, never alone */
  --good: #0ca30c;
  --warning: #fab219;
  --critical: #d03b3b;
  --track: rgba(128, 128, 128, 0.18);
  --radius: 12px;
}

* { box-sizing: border-box; }
html, body {
  margin: 0;
  background: var(--bg2);
  color: var(--text);
  font: 15px/1.35 -apple-system, BlinkMacSystemFont, "SF Pro Text", "Segoe UI", Roboto, sans-serif;
  -webkit-text-size-adjust: 100%;
}
button, input, select, textarea { font: inherit; color: inherit; }
.boot { padding: 40px 16px; text-align: center; color: var(--hint); }

main { padding: 12px 12px 88px; max-width: 640px; margin: 0 auto; }
h2 { font-size: 17px; margin: 0; }
h3 { font-size: 13px; font-weight: 600; text-transform: uppercase; letter-spacing: .03em;
     color: var(--hint); margin: 18px 4px 6px; }
.muted { color: var(--hint); }
.small { font-size: 13px; }
.row { display: flex; align-items: center; gap: 8px; }
.row.between { justify-content: space-between; }
.grow { flex: 1; min-width: 0; }
.wrap { flex-wrap: wrap; }

.card { background: var(--section); border-radius: var(--radius); padding: 12px; margin-bottom: 10px; }
.card.flush { padding: 0; overflow: hidden; }

/* Day header */
.dayhead { display: flex; align-items: center; gap: 8px; margin: 2px 2px 10px; }
.dayhead .title { flex: 1; text-align: center; }
.dayhead .title b { display: block; font-size: 17px; }
.iconbtn { background: var(--section); border: 0; border-radius: 10px; width: 36px; height: 36px;
           font-size: 18px; cursor: pointer; }

/* Metric bars */
.metric { margin: 9px 0; }
.metric .top { display: flex; justify-content: space-between; gap: 8px; font-size: 14px; }
.metric .top .val { white-space: nowrap; }
.bar { position: relative; height: 8px; border-radius: 4px; background: var(--track); margin-top: 4px; overflow: hidden; }
.bar > span { position: absolute; left: 0; top: 0; bottom: 0; border-radius: 4px; background: var(--accent); }
.bar > i { position: absolute; top: -2px; bottom: -2px; width: 2px; background: var(--hint); opacity: .7; }
.s-ok > span { background: var(--good); }
.s-near > span { background: var(--warning); }
.s-over > span { background: var(--critical); }
.s-under > span { background: var(--accent); opacity: .55; }
.s-under.final > span { background: var(--critical); opacity: 1; }
.hints { margin: 8px 0 0; padding: 0; list-style: none; font-size: 13px; color: var(--hint); }
.hints li::before { content: "→ "; }

/* Lists */
.list { list-style: none; margin: 0; padding: 0; }
.list > li { display: flex; align-items: center; gap: 10px; padding: 10px 12px; border-top: 1px solid var(--sep); }
.list > li:first-child { border-top: 0; }
.list .title { font-weight: 500; overflow: hidden; text-overflow: ellipsis; }
.list .sub { font-size: 13px; color: var(--hint); }
.list .kcal { font-size: 13px; color: var(--hint); white-space: nowrap; }
.list li.done .title { color: var(--hint); text-decoration: line-through; }
.list li.tap { cursor: pointer; }
.mealhead { display: flex; justify-content: space-between; align-items: center; padding: 10px 12px 6px; }
.mealhead b { text-transform: capitalize; }

.check { width: 26px; height: 26px; border-radius: 50%; border: 2px solid var(--hint); background: none;
         flex: none; cursor: pointer; font-size: 14px; line-height: 1; padding: 0; }
.check.on { background: var(--good); border-color: var(--good); color: #fff; }

/* Buttons */
.btn { border: 0; border-radius: 10px; padding: 10px 14px; white-space: nowrap; flex: none; background: var(--accent); color: var(--accent-text);
       font-weight: 600; cursor: pointer; }
.btn.secondary { background: var(--bg2); color: var(--link); }
.btn.ghost { background: none; color: var(--link); padding: 6px 8px; font-weight: 500; }
.btn.danger { background: none; color: var(--destructive); }
.btn.small { padding: 6px 10px; font-size: 14px; }
.btn.block { width: 100%; }
.btn:disabled { opacity: .5; }
.chips { display: flex; gap: 6px; flex-wrap: wrap; }
.chip { border: 1px solid var(--sep); background: var(--section); border-radius: 16px; padding: 6px 12px;
        cursor: pointer; font-size: 14px; }
.chip.on { background: var(--accent); color: var(--accent-text); border-color: var(--accent); }
.seg { display: flex; background: var(--bg2); border-radius: 10px; padding: 2px; margin-bottom: 10px; }
.seg button { flex: 1; border: 0; background: none; padding: 7px; border-radius: 8px; cursor: pointer; }
.seg button.on { background: var(--section); font-weight: 600; box-shadow: 0 1px 2px rgba(0,0,0,.1); }

/* Inputs */
input[type=text], input[type=number], input[type=date], input[type=search], select, textarea {
  width: 100%; min-width: 0; background: var(--section); border: 1px solid var(--sep); border-radius: 10px; padding: 9px 10px;
}
.card input, .card select, .card textarea, .sheet input, .sheet select, .sheet textarea {
  background: var(--bg2); border-color: transparent;
}
input:focus, select:focus, textarea:focus { outline: none; border-color: var(--accent); }
label.field { display: block; margin: 8px 0; font-size: 13px; color: var(--hint); }
label.field > input, label.field > select { margin-top: 4px; color: var(--text); font-size: 15px; }
.grid2 { display: grid; grid-template-columns: 1fr 1fr; gap: 0 10px; }
.grid3 { display: grid; grid-template-columns: repeat(3, 1fr); gap: 0 8px; }

/* Water */
.water .big { font-size: 22px; font-weight: 700; }

/* Tabs */
nav.tabs { position: fixed; left: 0; right: 0; bottom: 0; display: flex; background: var(--section);
           border-top: 1px solid var(--sep); padding-bottom: env(safe-area-inset-bottom); z-index: 5; }
nav.tabs button { flex: 1; border: 0; background: none; padding: 8px 2px 9px; font-size: 11px; color: var(--hint);
                  cursor: pointer; }
nav.tabs button .ic { display: block; font-size: 20px; line-height: 1.2; }
nav.tabs button.on { color: var(--accent); font-weight: 600; }

/* Sheet */
.overlay { position: fixed; inset: 0; background: rgba(0,0,0,.35); z-index: 10; display: flex; align-items: flex-end; }
.sheet { background: var(--bg); width: 100%; max-width: 640px; margin: 0 auto; max-height: 92vh; overflow-y: auto;
         border-radius: 16px 16px 0 0; padding: 14px 14px calc(18px + env(safe-area-inset-bottom)); }
.sheet .head { display: flex; align-items: center; gap: 8px; margin-bottom: 10px; }
.sheet .head h2 { flex: 1; }
.preview { font-size: 13px; color: var(--hint); margin: 8px 0; }
.badge { display: inline-block; font-size: 11px; padding: 1px 6px; border-radius: 6px; background: var(--bg2);
         color: var(--hint); margin-left: 4px; vertical-align: middle; }
.badge.ok { color: var(--good); }
.imgs { display: flex; gap: 8px; overflow-x: auto; margin-bottom: 10px; }
.imgs img { height: 220px; border-radius: 10px; }
.steps { padding-left: 20px; margin: 6px 0; }
.steps li { margin: 4px 0; }

/* Toast */
.toast { position: fixed; left: 50%; transform: translateX(-50%); bottom: 84px; background: rgba(20,20,20,.88);
         color: #fff; padding: 9px 14px; border-radius: 10px; z-index: 20; font-size: 14px; max-width: 90%; }

/* Chart */
.chart svg { width: 100%; height: auto; display: block; }
.chart .grid { stroke: var(--track); stroke-width: 1; }
.chart .axis-label { fill: var(--hint); font-size: 10px; }
.chart .series { fill: none; stroke: var(--accent); stroke-width: 2; stroke-linejoin: round; stroke-linecap: round; }
.chart .trend { fill: none; stroke: var(--hint); stroke-width: 1.5; stroke-dasharray: 2 4; }
.chart .goal { stroke: var(--good); stroke-width: 1.5; stroke-dasharray: 6 4; }
.chart .goal-label { fill: var(--hint); font-size: 10px; }
.chart .dot { fill: var(--accent); stroke: var(--section); stroke-width: 2; }
.chart .dot.sel { r: 6; }
.chart .value-label { fill: var(--text); font-size: 11px; font-weight: 600; }
.chart .hit { fill: transparent; cursor: pointer; }
.tip { font-size: 13px; min-height: 18px; color: var(--text); margin-bottom: 4px; }
.stats { display: grid; grid-template-columns: repeat(2, 1fr); gap: 8px; margin-top: 10px; }
.stats div { background: var(--bg2); border-radius: 10px; padding: 8px 10px; }
.stats b { display: block; font-size: 18px; }
table.adh { width: 100%; border-collapse: collapse; font-size: 13px; }
table.adh th, table.adh td { padding: 6px 4px; text-align: center; border-top: 1px solid var(--sep); }
table.adh th:first-child, table.adh td:first-child { text-align: left; }
table.adh th { color: var(--hint); font-weight: 500; }
.empty { text-align: center; color: var(--hint); padding: 18px 8px; }
