:root {
  --bg: var(--tg-theme-bg-color, #ffffff);
  --bg2: var(--tg-theme-secondary-bg-color, #f2f3f5);
  --text: var(--tg-theme-text-color, #111111);
  --hint: var(--tg-theme-hint-color, #8a8f98);
  --link: var(--tg-theme-link-color, #2481cc);
  --btn: var(--tg-theme-button-color, #2481cc);
  --btn-text: var(--tg-theme-button-text-color, #ffffff);
  --card: var(--tg-theme-section-bg-color, var(--bg2));
  --line: rgba(128, 128, 128, .22);
  --kcal: #ff7a45;
  --prot: #4c9aff;
  --fat: #f5b83d;
  --carb: #45c486;
  --danger: #e5484d;
  --r: 16px;
  --tabs-h: 58px;
}
* { box-sizing: border-box; -webkit-tap-highlight-color: transparent; }
html, body { margin: 0; background: var(--bg2); color: var(--text); font: 15px/1.4 -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, sans-serif; }
body { padding-bottom: calc(var(--tabs-h) + env(safe-area-inset-bottom) + 24px); }
button { font: inherit; color: inherit; border: 0; background: none; cursor: pointer; }
input, select, textarea { font: inherit; color: var(--text); background: var(--bg); border: 1px solid var(--line); border-radius: 12px; padding: 10px 12px; width: 100%; outline: none; }
input:focus, select:focus, textarea:focus { border-color: var(--btn); }
h2 { margin: 0 0 8px; font-size: 20px; }
h3 { margin: 16px 0 8px; font-size: 13px; color: var(--hint); text-transform: uppercase; letter-spacing: .04em; font-weight: 600; }

#app { max-width: 560px; margin: 0 auto; padding: 12px; }
#hdr { display: flex; align-items: center; justify-content: space-between; gap: 8px; margin-bottom: 12px; }
#hdr .hi { font-size: 18px; font-weight: 700; }
#hdr .chips { display: flex; gap: 6px; }
.chip { background: var(--bg); border-radius: 999px; padding: 5px 10px; font-weight: 600; font-size: 13px; white-space: nowrap; }
.chip.off { color: var(--hint); font-weight: 500; }
.chip.risk { background: #fff1e8; color: #c2410c; animation: pulse 1.6s infinite; }
@keyframes pulse { 50% { transform: scale(1.06); } }

.card { background: var(--bg); border-radius: var(--r); padding: 14px; margin-bottom: 12px; }
.empty { text-align: center; color: var(--hint); padding: 32px 12px; }
.empty .big { font-size: 42px; margin-bottom: 6px; }

.datenav { display: flex; align-items: center; justify-content: space-between; margin-bottom: 12px; }
.datenav button { width: 38px; height: 38px; border-radius: 12px; background: var(--bg); font-size: 18px; }
.datenav button:disabled { opacity: .3; }
.datenav .d { font-weight: 700; }

.sum { display: flex; gap: 14px; align-items: center; }
.ring { position: relative; width: 116px; height: 116px; flex: none; }
.ring svg { width: 100%; height: 100%; transform: rotate(-90deg); }
.ring circle { fill: none; stroke-width: 11; stroke-linecap: round; }
.ring .bg { stroke: var(--line); }
.ring .fg { stroke: var(--kcal); transition: stroke-dashoffset .5s; }
.ring .fg.over { stroke: var(--danger); }
.ring .mid { position: absolute; inset: 0; display: flex; flex-direction: column; align-items: center; justify-content: center; }
.ring .mid b { font-size: 22px; line-height: 1; }
.ring .mid small { color: var(--hint); font-size: 11px; margin-top: 2px; }
.macros { flex: 1; display: grid; gap: 9px; }
.macro .top { display: flex; justify-content: space-between; font-size: 12px; margin-bottom: 3px; }
.macro .top span { color: var(--hint); }
.bar { height: 6px; border-radius: 3px; background: var(--line); overflow: hidden; }
.bar i { display: block; height: 100%; border-radius: 3px; }

.meal { display: flex; gap: 10px; align-items: center; padding: 10px 0; border-top: 1px solid var(--line); cursor: pointer; }
.meal:first-of-type { border-top: 0; }
.meal .th { width: 52px; height: 52px; border-radius: 12px; background: var(--bg2); flex: none; overflow: hidden; display: flex; align-items: center; justify-content: center; font-size: 24px; }
.meal .th img { width: 100%; height: 100%; object-fit: cover; }
.meal .nm { font-weight: 600; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.meal .mt { font-size: 12px; color: var(--hint); }
.meal .kc { margin-left: auto; text-align: right; flex: none; }
.meal .kc b { display: block; }
.meal .kc small { color: var(--hint); font-size: 11px; }
.grp { display: flex; justify-content: space-between; font-weight: 700; margin-bottom: 4px; }
.grp span { color: var(--hint); font-weight: 500; }
.ai-badge { font-size: 10px; background: var(--bg2); border-radius: 6px; padding: 1px 5px; margin-left: 4px; color: var(--hint); }

#fab { position: fixed; right: 16px; bottom: calc(var(--tabs-h) + env(safe-area-inset-bottom) + 14px); width: 56px; height: 56px; border-radius: 50%; background: var(--btn); color: var(--btn-text); font-size: 28px; box-shadow: 0 6px 18px rgba(0,0,0,.25); z-index: 20; }
#tabs { position: fixed; left: 0; right: 0; bottom: 0; display: flex; background: var(--bg); border-top: 1px solid var(--line); padding-bottom: env(safe-area-inset-bottom); z-index: 20; }
#tabs button { flex: 1; height: var(--tabs-h); font-size: 11px; color: var(--hint); display: flex; flex-direction: column; align-items: center; justify-content: center; gap: 2px; }
#tabs button span { font-size: 20px; filter: grayscale(1); opacity: .7; }
#tabs button.on { color: var(--btn); font-weight: 600; }
#tabs button.on span { filter: none; opacity: 1; }

.btn { display: block; width: 100%; background: var(--btn); color: var(--btn-text); padding: 13px; border-radius: 14px; font-weight: 600; text-align: center; }
.btn.sec { background: var(--bg2); color: var(--text); }
.btn.dng { background: none; color: var(--danger); }
.btn:disabled { opacity: .5; }
.row { display: flex; gap: 8px; }
.row > * { flex: 1; min-width: 0; }
.field { margin-bottom: 10px; }
.field label { display: block; font-size: 12px; color: var(--hint); margin-bottom: 4px; }
.seg { display: flex; background: var(--bg2); border-radius: 12px; padding: 3px; gap: 3px; }
.seg button { flex: 1; padding: 8px 4px; border-radius: 10px; font-size: 13px; }
.seg button.on { background: var(--bg); font-weight: 600; box-shadow: 0 1px 3px rgba(0,0,0,.12); }

#sheet { position: fixed; inset: 0; z-index: 50; display: none; }
#sheet.open { display: block; }
#sheet .back { position: absolute; inset: 0; background: rgba(0,0,0,.45); }
#sheet .pane { position: absolute; left: 0; right: 0; bottom: 0; max-width: 560px; margin: 0 auto; max-height: 92vh; overflow-y: auto; background: var(--bg); border-radius: 22px 22px 0 0; padding: 8px 16px calc(16px + env(safe-area-inset-bottom)); animation: up .22s ease-out; }
#sheet .grab { width: 40px; height: 4px; border-radius: 2px; background: var(--line); margin: 4px auto 12px; }
@keyframes up { from { transform: translateY(40px); opacity: 0; } }

.opts { display: grid; grid-template-columns: 1fr 1fr; gap: 10px; }
.opt { background: var(--bg2); border-radius: 16px; padding: 16px 8px; text-align: center; font-weight: 600; }
.opt span { display: block; font-size: 30px; margin-bottom: 4px; }
.opt small { display: block; font-weight: 400; color: var(--hint); font-size: 11px; }

.photo-prev { width: 100%; max-height: 220px; object-fit: cover; border-radius: 14px; margin-bottom: 10px; }
.spin { text-align: center; padding: 30px 0; color: var(--hint); }
.spin i { display: block; width: 34px; height: 34px; margin: 0 auto 12px; border: 3px solid var(--line); border-top-color: var(--btn); border-radius: 50%; animation: rot .8s linear infinite; }
@keyframes rot { to { transform: rotate(360deg); } }

.item { display: flex; align-items: center; gap: 8px; padding: 8px 0; border-top: 1px solid var(--line); }
.item .n { flex: 1; min-width: 0; }
.item .n b { display: block; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; font-weight: 600; }
.item .n small { color: var(--hint); font-size: 11px; }
.item input { width: 72px; padding: 6px 8px; text-align: right; }
.item button { color: var(--danger); font-size: 18px; padding: 4px 8px; }
.tot { background: var(--bg2); border-radius: 14px; padding: 10px 12px; display: flex; justify-content: space-between; text-align: center; margin: 10px 0; }
.tot div b { display: block; font-size: 16px; }
.tot div small { color: var(--hint); font-size: 11px; }
.note { font-size: 12px; color: var(--hint); margin: 6px 0; }
.warn { background: #fff8e1; color: #7a5b00; border-radius: 12px; padding: 8px 10px; font-size: 12px; margin: 8px 0; }

.sug { border: 1px solid var(--line); border-radius: 12px; margin-top: 4px; max-height: 180px; overflow-y: auto; }
.sug button { display: flex; justify-content: space-between; width: 100%; padding: 9px 12px; text-align: left; border-top: 1px solid var(--line); }
.sug button:first-child { border-top: 0; }
.sug small { color: var(--hint); }

.chart { display: flex; align-items: flex-end; gap: 3px; height: 150px; position: relative; padding-top: 8px; }
.chart .col { flex: 1; display: flex; flex-direction: column; justify-content: flex-end; height: 100%; min-width: 0; }
.chart .col i { display: block; border-radius: 4px 4px 0 0; background: var(--kcal); min-height: 2px; }
.chart .col i.over { background: var(--danger); }
.chart .col i.zero { background: var(--line); height: 2px !important; }
.chart .goal { position: absolute; left: 0; right: 0; border-top: 1.5px dashed var(--hint); pointer-events: none; }
.chart .goal span { position: absolute; right: 0; top: -16px; font-size: 10px; color: var(--hint); }
.xl { display: flex; justify-content: space-between; font-size: 10px; color: var(--hint); margin-top: 4px; }
.stat3 { display: grid; grid-template-columns: repeat(3, 1fr); gap: 8px; text-align: center; }
.stat3 b { display: block; font-size: 20px; }
.stat3 small { color: var(--hint); font-size: 11px; }

.grid { display: grid; grid-template-columns: repeat(3, 1fr); gap: 4px; }
.grid .g { position: relative; aspect-ratio: 1; border-radius: 10px; overflow: hidden; background: var(--bg); cursor: pointer; }
.grid .g img { width: 100%; height: 100%; object-fit: cover; }
.grid .g span { position: absolute; left: 0; right: 0; bottom: 0; padding: 12px 5px 3px; font-size: 10px; color: #fff; background: linear-gradient(transparent, rgba(0,0,0,.65)); }

#viewer { position: fixed; inset: 0; z-index: 60; background: rgba(0,0,0,.94); display: none; flex-direction: column; color: #fff; }
#viewer.open { display: flex; }
#viewer .x { position: absolute; top: 10px; right: 12px; font-size: 28px; color: #fff; padding: 6px 12px; z-index: 2; }
#viewer .im { flex: 1; min-height: 0; display: flex; align-items: center; justify-content: center; padding: 50px 8px 8px; }
#viewer img { max-width: 100%; max-height: 100%; object-fit: contain; border-radius: 8px; }
#viewer .inf { padding: 12px 16px calc(16px + env(safe-area-inset-bottom)); background: rgba(255,255,255,.08); }
#viewer .inf b { display: block; font-size: 17px; }
#viewer .inf div { opacity: .8; font-size: 13px; margin-top: 2px; }

#celebrate { position: fixed; inset: 0; z-index: 80; display: none; align-items: center; justify-content: center; background: rgba(0,0,0,.55); padding: 24px; }
#celebrate.open { display: flex; }
#celebrate .box { background: var(--bg); border-radius: 24px; padding: 26px 22px 20px; text-align: center; max-width: 340px; width: 100%; animation: pop .35s cubic-bezier(.2, 1.4, .4, 1); }
@keyframes pop { from { transform: scale(.6); opacity: 0; } }
#celebrate .em { font-size: 72px; line-height: 1; animation: bounce 1s ease-in-out infinite; }
@keyframes bounce { 50% { transform: translateY(-8px) rotate(-4deg); } }
#celebrate h2 { margin: 12px 0 6px; font-size: 22px; }
#celebrate p { margin: 0 0 6px; color: var(--hint); }
#celebrate .rec { display: inline-block; background: #fff1c2; color: #7a5b00; border-radius: 999px; padding: 3px 10px; font-size: 12px; font-weight: 600; margin: 4px 0 8px; }
#celebrate .btn { margin-top: 12px; }
#confetti { position: fixed; inset: 0; z-index: 90; pointer-events: none; width: 100%; height: 100%; }

#toast { position: fixed; left: 50%; bottom: calc(var(--tabs-h) + 80px); transform: translateX(-50%); background: rgba(30,30,30,.92); color: #fff; padding: 9px 16px; border-radius: 12px; font-size: 13px; z-index: 100; opacity: 0; transition: opacity .2s; pointer-events: none; max-width: 86%; text-align: center; }
#toast.show { opacity: 1; }
.streakbox { display: flex; gap: 10px; }
.streakbox > div { flex: 1; background: var(--bg2); border-radius: 14px; padding: 12px; text-align: center; }
.streakbox b { display: block; font-size: 26px; }
.streakbox small { color: var(--hint); font-size: 11px; }
