/* 移动端优先。主要使用场景是吃饭时单手拿手机拍照，
   因此所有可点区域都保证 44px 以上的触控高度。 */

/* 配色以四色绿为基调：
   #E8F5E9 最浅（页面底色 / 深色模式文字）
   #A5D6A7 浅（环形轨道 / 弱分隔）
   #66BB6A 中（图表填充 / 深色模式主色）
   #1B5E20 深（浅色模式主色 / 文字）
   --p-700 / --p-600 是深绿与中绿之间的过渡色，只用于渐变和图表分档。 */

:root {
  color-scheme: light;

  --p-50: #e8f5e9;
  --p-200: #a5d6a7;
  --p-400: #66bb6a;
  --p-600: #418d45;
  --p-700: #2e7533;
  --p-900: #1b5e20;

  --bg: var(--p-50);
  --surface: #ffffff;
  --field: #f0f7f1;
  --text: #14351a;
  --text-dim: #4f6b53;
  --line: #cfe6d0;

  --accent: var(--p-900);
  --accent-soft: var(--p-50);
  --on-accent: #ffffff;

  /* 图形填充：强 / 过渡 / 亮，深浅模式下互换方向 */
  --fill-strong: var(--p-900);
  --fill-mid: var(--p-700);
  --fill-bright: var(--p-400);

  --ring-track: var(--p-200);
  --track: #dceedd;

  --macro-1: var(--p-900);
  --macro-2: var(--p-600);
  --macro-3: var(--p-400);

  --green: var(--p-900);
  --red: #b3261e;
  --amber: #8d5a10;
  --warn-soft: #fbf3e4;

  --radius: 14px;
  --shadow: 0 1px 2px rgba(27, 94, 32, 0.07), 0 4px 14px rgba(27, 94, 32, 0.06);
  --scrim: rgba(14, 26, 16, 0.45);
  --scrim-strong: rgba(14, 26, 16, 0.62);
  --toast-bg: rgba(20, 53, 26, 0.95);
}

@media (prefers-color-scheme: dark) {
  :root {
    color-scheme: dark;

    --bg: #0e1a10;
    --surface: #16261a;
    --field: #101e13;
    --text: var(--p-50);
    --text-dim: #9ab69e;
    --line: #2a3f2e;

    --accent: var(--p-400);
    --accent-soft: #1b3a20;
    --on-accent: #08160b;

    --fill-strong: var(--p-400);
    --fill-mid: var(--p-600);
    --fill-bright: var(--p-200);

    --ring-track: #24382a;
    --track: #24382a;

    --macro-1: var(--p-200);
    --macro-2: var(--p-400);
    --macro-3: var(--p-600);

    --green: var(--p-400);
    --red: #ef6a5c;
    --amber: #d9a441;
    --warn-soft: #2a2113;

    --shadow: 0 1px 3px rgba(0, 0, 0, 0.45);
    --scrim: rgba(6, 14, 8, 0.55);
    --scrim-strong: rgba(6, 14, 8, 0.72);
    --toast-bg: rgba(46, 74, 52, 0.97);
  }
}

* { box-sizing: border-box; }

body {
  margin: 0;
  font-family: -apple-system, BlinkMacSystemFont, "PingFang SC", "Microsoft YaHei",
    "Segoe UI", Roboto, sans-serif;
  background: var(--bg);
  color: var(--text);
  -webkit-font-smoothing: antialiased;
}

.app {
  max-width: 560px;
  margin: 0 auto;
  padding: 0 16px calc(76px + env(safe-area-inset-bottom));
}

.hidden { display: none !important; }

/* ---------- 头部 ---------- */
.hd { padding: 24px 0 12px; }
.hd h1 { margin: 0; font-size: 28px; letter-spacing: -0.5px; }
.hd-sub { color: var(--text-dim); font-size: 14px; margin-top: 2px; }

/* ---------- 卡片 ---------- */
.card {
  background: var(--surface);
  border-radius: var(--radius);
  padding: 16px;
  margin-bottom: 14px;
  box-shadow: var(--shadow);
}
.card-title {
  font-weight: 600;
  font-size: 15px;
  margin-bottom: 12px;
  display: flex;
  justify-content: space-between;
  align-items: center;
}

/* ---------- 热量环 ---------- */
.ring-card {
  background: var(--surface);
  border-radius: var(--radius);
  padding: 20px 16px 16px;
  margin-bottom: 14px;
  box-shadow: var(--shadow);
}
.ring-wrap { position: relative; width: 160px; height: 160px; margin: 0 auto; }
.ring { width: 100%; height: 100%; transform: rotate(-90deg); }
.ring circle { fill: none; stroke-width: 10; stroke-linecap: round; }
.ring-bg { stroke: var(--ring-track); }
/* 渐变本体写在 HTML 的 stroke 属性上，避免外链样式表里 url(#id) 的解析差异 */
.ring-stop-a { stop-color: var(--fill-bright); }
.ring-stop-b { stop-color: var(--fill-strong); }
.ring-fg {
  stroke-dasharray: 327;
  stroke-dashoffset: 327;
  transition: stroke-dashoffset 0.5s ease, stroke 0.3s;
}
.ring-fg.over { stroke: var(--red); }
.ring-center {
  position: absolute;
  inset: 0;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
}
.ring-num { font-size: 34px; font-weight: 700; letter-spacing: -1px; }
.ring-unit { font-size: 12px; color: var(--text-dim); margin-top: 2px; }
.ring-meta {
  display: flex;
  justify-content: center;
  gap: 40px;
  margin-top: 14px;
}
.ring-meta div { text-align: center; }
.ring-meta span { display: block; font-size: 17px; font-weight: 600; }
.ring-meta small { color: var(--text-dim); font-size: 12px; }

.macro-bars { margin-top: 16px; display: grid; gap: 9px; }
.macro {
  display: grid;
  grid-template-columns: 34px 1fr auto;
  align-items: center;
  gap: 10px;
  font-size: 12px;
}
.macro-track {
  height: 6px;
  background: var(--track);
  border-radius: 3px;
  overflow: hidden;
}
.macro-fill { height: 100%; border-radius: 3px; transition: width 0.4s ease; }
.macro-val { color: var(--text-dim); font-variant-numeric: tabular-nums; }

/* ---------- 记录一餐 ----------
   描述框和拍照按钮同等醒目：一餐的信息有一半在文字里（吃了几分之几、
   已知重量、照片外的食物），把它做成小小的"备注"会让用户不填。 */
.compose {
  background: var(--surface);
  border-radius: var(--radius);
  padding: 14px;
  margin-bottom: 18px;
  box-shadow: var(--shadow);
  display: grid;
  gap: 10px;
}
.thumbs { display: flex; gap: 8px; flex-wrap: wrap; }
.thumbs:empty { display: none; }
.thumb { position: relative; width: 76px; height: 76px; }
.thumb img {
  width: 100%;
  height: 100%;
  object-fit: cover;
  border-radius: 9px;
  display: block;
}
.thumb-del {
  position: absolute;
  top: -6px;
  right: -6px;
  width: 22px;
  height: 22px;
  border: 0;
  border-radius: 50%;
  background: var(--red);
  color: #fff;
  font-size: 15px;
  line-height: 1;
  cursor: pointer;
  padding: 0;
}
.add-photo {
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 8px;
  border: 1px dashed var(--line);
  border-radius: 10px;
  padding: 13px;
  cursor: pointer;
  user-select: none;
  font-size: 15px;
  color: var(--text-dim);
}
.add-photo:active { background: var(--field); }
.add-photo-icon { font-size: 20px; }
.compose-note {
  width: 100%;
  padding: 11px 12px;
  font-size: 16px; /* 低于 16px 时 iOS Safari 聚焦会放大页面 */
  line-height: 1.5;
  border: 1px solid var(--line);
  border-radius: 10px;
  background: var(--field);
  color: var(--text);
  font-family: inherit;
  resize: vertical;
}
.compose-note:focus { outline: 2px solid var(--accent); outline-offset: -1px; }
.compose-hint { font-size: 12px; color: var(--text-dim); text-align: center; }
.compose-when {
  display: flex;
  align-items: center;
  gap: 10px;
  font-size: 14px;
  color: var(--text-dim);
}
.compose-when input[type="date"] { flex: 1; }

/* ---------- 分析中 / 失败 ---------- */
.meal.analyzing { border: 1px dashed var(--accent); cursor: default; }
.meal.analyzing:active { opacity: 1; }
.meal-analyzing {
  display: inline-flex;
  align-items: center;
  gap: 7px;
  font-size: 14px;
  font-weight: 600;
  color: var(--accent);
}
.meal-analyzing .dot {
  width: 8px;
  height: 8px;
  border-radius: 50%;
  background: var(--accent);
  animation: pulse 1.2s ease-in-out infinite;
}
@keyframes pulse { 0%,100% { opacity: 1; } 50% { opacity: 0.25; } }
@media (prefers-reduced-motion: reduce) {
  .meal-analyzing .dot { animation: none; }
}
.meal.failed { border-left: 3px solid var(--red); cursor: default; }
.meal-failed { font-size: 14px; font-weight: 600; color: var(--red); }
.badge.warn-badge { background: var(--warn-soft); color: var(--amber); }

.badge.tiny { font-size: 10px; padding: 1px 6px; margin-left: 5px; }
.btn.danger { background: var(--red); color: #fff; margin-top: 8px; }

/* ---------- 记录详情 ---------- */
.meal { cursor: pointer; }
.meal:active { opacity: 0.9; }
.meal-more { font-size: 12px; color: var(--accent); margin-top: 8px; }
.detail-photos {
  display: flex;
  gap: 8px;
  overflow-x: auto;
  margin-bottom: 14px;
  -webkit-overflow-scrolling: touch;
}
.detail-photo {
  height: 168px;
  border-radius: 10px;
  flex: 0 0 auto;
  object-fit: cover;
}
.detail-note {
  background: var(--field);
  border-radius: 10px;
  padding: 10px 12px;
  margin-bottom: 12px;
  font-size: 13px;
  line-height: 1.6;
}
.detail-note strong { font-size: 12px; color: var(--text-dim); }
.detail-note p { margin: 4px 0 0; white-space: pre-wrap; }

/* ---------- 餐食列表 ---------- */
.meal {
  background: var(--surface);
  border-radius: var(--radius);
  padding: 14px 16px;
  margin-bottom: 10px;
  box-shadow: var(--shadow);
}
.meal-top {
  display: flex;
  justify-content: space-between;
  align-items: baseline;
  margin-bottom: 6px;
}
.meal-time { font-size: 13px; color: var(--text-dim); }
.meal-kcal { font-weight: 700; font-size: 17px; }
.meal-items { font-size: 14px; color: var(--text-dim); line-height: 1.5; }
.meal-badges { margin-top: 8px; display: flex; gap: 6px; flex-wrap: wrap; }
.badge {
  font-size: 11px;
  padding: 2px 8px;
  border-radius: 20px;
  background: var(--accent-soft);
  color: var(--accent);
}
.badge.plain { background: var(--line); color: var(--text-dim); }
.meal-del {
  border: 0;
  background: none;
  color: var(--text-dim);
  font-size: 13px;
  padding: 4px 0 0;
  cursor: pointer;
}

/* ---------- 图表 ----------
   绘图区与坐标轴分层：柱子的百分比高度只相对 .chart-plot，目标线用同一参照系。
   把标签放进同一个 flex 列会占掉高度，参照系变含混，满格的目标线会溢出画布。 */
.chart { display: flex; flex-direction: column; gap: 4px; }
.chart-plot {
  position: relative;
  display: flex;
  align-items: flex-end;
  gap: 3px;
  /* 顶部留白给目标线的文字标签，避免贴边被裁掉 */
  height: 116px;
  margin-top: 14px;
}
.chart-axis {
  display: flex;
  gap: 3px;
}
.chart-axis .bar-label {
  flex: 1;
  max-width: 44px;
  text-align: center;
}
.bar-col {
  flex: 1;
  /* 不封顶的话，只有一两天数据时单根柱子会撑满整块画布，看起来像渲染坏了 */
  max-width: 44px;
  display: flex;
  align-items: flex-end;
  height: 100%;
}
.bar-col .bar { width: 100%; }
.target-line {
  position: absolute;
  left: 0;
  right: 0;
  border-top: 1px dashed var(--text-dim);
  opacity: 0.6;
  pointer-events: none;
}
.target-line span {
  position: absolute;
  right: 0;
  top: -13px;
  font-size: 10px;
  color: var(--text-dim);
  background: var(--surface);
  padding: 0 3px;
}
.bar {
  width: 100%;
  background: linear-gradient(180deg, var(--fill-bright), var(--fill-strong));
  border-radius: 3px 3px 0 0;
  min-height: 2px;
  transition: height 0.4s ease;
}
.bar.over { background: var(--red); }
.bar-label { font-size: 9px; color: var(--text-dim); }
.line-chart { width: 100%; height: 120px; }
.line-chart path { fill: none; stroke: var(--fill-strong); stroke-width: 2; }
.line-chart circle { fill: var(--fill-bright); }

/* ---------- 进度 ---------- */
.prog-grid {
  display: grid;
  grid-template-columns: repeat(2, 1fr);
  gap: 14px;
}
.prog-item span { display: block; font-size: 20px; font-weight: 700; }
.prog-item small { color: var(--text-dim); font-size: 12px; }
.prog-note {
  margin-top: 14px;
  padding-top: 12px;
  border-top: 1px solid var(--line);
  font-size: 13px;
  color: var(--text-dim);
  line-height: 1.6;
}

/* 用量明细。测试期用，上线前大概率要收起来或只留给管理员。 */
.usage-detail {
  margin-top: 14px;
  padding-top: 12px;
  border-top: 1px solid var(--line);
  display: grid;
  gap: 7px;
}
.usage-row {
  display: flex;
  justify-content: space-between;
  align-items: baseline;
  gap: 12px;
  font-size: 13.5px;
}
.usage-row span:first-child { color: var(--text-dim); }
.usage-row b { font-variant-numeric: tabular-nums; font-weight: 600; }
.usage-row.sub {
  margin-top: -4px;
  font-size: 12px;
  color: var(--text-dim);
}
.usage-row.sub span { color: var(--text-dim); }

/* ---------- 表单 ---------- */
.form { display: grid; gap: 12px; }
.row { display: grid; grid-template-columns: 110px 1fr; align-items: center; gap: 10px; }
.row label { font-size: 14px; color: var(--text-dim); }
input, select {
  width: 100%;
  padding: 10px 12px;
  font-size: 16px; /* 低于 16px 时 iOS Safari 会在聚焦时自动放大页面 */
  border: 1px solid var(--line);
  border-radius: 9px;
  background: var(--field);
  color: var(--text);
  font-family: inherit;
}
input:focus, select:focus { outline: 2px solid var(--accent); outline-offset: -1px; }
.check {
  display: flex;
  align-items: center;
  gap: 8px;
  font-size: 13px;
  color: var(--text-dim);
}
.check input { width: auto; accent-color: var(--accent); }

.btn {
  padding: 12px;
  border: 0;
  border-radius: 10px;
  font-size: 15px;
  font-weight: 600;
  cursor: pointer;
  font-family: inherit;
}
.btn.primary {
  background: linear-gradient(135deg, var(--fill-strong), var(--fill-mid));
  color: var(--on-accent);
}
.btn:active { opacity: 0.85; }
.link {
  border: 0;
  background: none;
  color: var(--accent);
  font-size: 14px;
  cursor: pointer;
  padding: 4px;
  font-family: inherit;
}
.link.strong { font-weight: 700; }

.note { font-size: 13px; color: var(--text-dim); line-height: 1.6; margin: 0 0 12px; }
.note strong { color: var(--text); }
.note.tip {
  background: var(--accent-soft);
  border-left: 3px solid var(--p-200);
  padding: 10px 12px;
  border-radius: 0 9px 9px 0;
  margin: 12px 0;
}
.empty { color: var(--text-dim); font-size: 14px; text-align: center; padding: 20px 0; }

/* ---------- 底部导航 ---------- */
.tabbar {
  position: fixed;
  bottom: 0;
  left: 0;
  right: 0;
  display: flex;
  background: var(--surface);
  border-top: 1px solid var(--line);
  padding-bottom: env(safe-area-inset-bottom);
  z-index: 10;
}
.tab {
  flex: 1;
  border: 0;
  background: none;
  padding: 9px 0 11px;
  font-size: 11px;
  color: var(--text-dim);
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 3px;
  cursor: pointer;
  font-family: inherit;
}
.tab span { font-size: 19px; }
.tab.active { color: var(--accent); font-weight: 600; }

/* ---------- 结果确认层 ---------- */
.sheet-backdrop {
  position: fixed;
  inset: 0;
  background: var(--scrim);
  z-index: 20;
  display: flex;
  align-items: flex-end;
}
.sheet {
  background: var(--surface);
  width: 100%;
  max-width: 560px;
  margin: 0 auto;
  border-radius: 18px 18px 0 0;
  max-height: 92vh;
  display: flex;
  flex-direction: column;
  animation: slide-up 0.25s ease;
}
@keyframes slide-up { from { transform: translateY(100%); } }
@media (prefers-reduced-motion: reduce) {
  .sheet { animation: none; }
  .ring-fg, .macro-fill, .bar { transition: none; }
}
.sheet-head {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 12px 14px;
  border-bottom: 1px solid var(--line);
}
.sheet-body {
  overflow-y: auto;
  padding: 16px;
  -webkit-overflow-scrolling: touch;
}

.sheet-summary { text-align: center; margin-bottom: 4px; }
.sheet-kcal { font-size: 36px; font-weight: 700; letter-spacing: -1px; }
.conf { font-size: 12px; margin-top: 8px; }
.conf.high { color: var(--green); }
.conf.mid { color: var(--amber); }
.conf.low { color: var(--red); }

.warn {
  background: var(--warn-soft);
  border-left: 3px solid var(--amber);
  padding: 9px 12px;
  border-radius: 0 8px 8px 0;
  font-size: 13px;
  margin: 10px 0;
  line-height: 1.5;
}

.item {
  border: 1px solid var(--line);
  border-radius: 11px;
  padding: 13px;
  margin-bottom: 10px;
}
.item-head {
  display: flex;
  justify-content: space-between;
  align-items: baseline;
  gap: 8px;
}
.item-name { font-weight: 600; font-size: 15px; }
.item-kcal { font-weight: 700; font-variant-numeric: tabular-nums; }
.item-sub { font-size: 12px; color: var(--text-dim); margin-top: 3px; }
.item-slider { margin-top: 10px; display: flex; align-items: center; gap: 10px; }
.item-slider input[type="range"] {
  flex: 1;
  padding: 0;
  accent-color: var(--accent);
  background: none;
  border: 0;
}
.item-grams {
  min-width: 58px;
  text-align: right;
  font-size: 13px;
  font-variant-numeric: tabular-nums;
  color: var(--text-dim);
}
.item-macros {
  display: flex;
  gap: 12px;
  margin-top: 8px;
  font-size: 11px;
  color: var(--text-dim);
}
.item-note {
  font-size: 12px;
  color: var(--amber);
  margin-top: 6px;
  line-height: 1.5;
}
/* ---------- 识别与修正的对照 ----------
   刻意做得比条目卡片轻：它是给用户回顾用的参考，不是可编辑的内容，
   长得一样重会诱导用户去点它。 */
.rev-block {
  margin-top: 18px;
  padding-top: 14px;
  border-top: 1px solid var(--line);
}
.rev-title { font-size: 12px; color: var(--text-dim); margin-bottom: 10px; }
.rev { margin-bottom: 10px; }
.rev-head {
  display: flex;
  justify-content: space-between;
  align-items: baseline;
  gap: 8px;
  font-size: 13px;
}
.rev-when { color: var(--text-dim); }
.rev-kcal { font-variant-numeric: tabular-nums; white-space: nowrap; }
.rev-kcal em {
  font-style: normal;
  font-size: 12px;
  margin-left: 5px;
}
.rev-kcal em.up { color: var(--amber); }
.rev-kcal em.down { color: var(--green); }
.rev-diff {
  margin: 5px 0 0;
  padding-left: 16px;
  font-size: 12px;
  color: var(--text-dim);
  line-height: 1.7;
}
.rev-bias {
  font-size: 12px;
  color: var(--text-dim);
  background: var(--field);
  border-radius: 8px;
  padding: 8px 10px;
  margin-top: 10px;
}

.meal-type-row { margin-top: 14px; }

/* ---------- 全局遮罩与提示 ---------- */
.overlay {
  position: fixed;
  inset: 0;
  background: var(--scrim-strong);
  z-index: 30;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 16px;
  color: var(--p-50);
}
.spinner {
  width: 38px;
  height: 38px;
  border: 3px solid rgba(232, 245, 233, 0.22);
  border-top-color: var(--p-400);
  border-radius: 50%;
  animation: spin 0.8s linear infinite;
}
@keyframes spin { to { transform: rotate(360deg); } }
.loading-text { font-size: 14px; }

.toast {
  position: fixed;
  bottom: calc(88px + env(safe-area-inset-bottom));
  left: 50%;
  transform: translateX(-50%);
  background: var(--toast-bg);
  color: var(--p-50);
  padding: 11px 18px;
  border-radius: 22px;
  font-size: 14px;
  z-index: 40;
  max-width: 88vw;
  text-align: center;
  line-height: 1.45;
}
.toast.err { background: var(--red); color: #ffffff; }
