:root {
  --bg: #f6f8fb;
  --card: #ffffff;
  --text: #1f2430;
  --muted: #8a94a6;
  --primary: #ff6b6b;
  --primary-dark: #e94f4f;
  --line: #e8ecf2;
  --green: #2eb872;
  --red: #e03e3e;
  --shadow: 0 2px 10px rgba(31, 36, 48, 0.06);
  --radius: 14px;
}

* { box-sizing: border-box; margin: 0; padding: 0; }

.fatal-error {
  position: fixed;
  top: 0;
  left: 0;
  right: 0;
  z-index: 200;
  background: #7a1f1f;
  color: #fff;
  font-size: 12.5px;
  padding: 8px 14px;
  display: none;
  word-break: break-all;
}
.fatal-error.show { display: block; }

body {
  font-family: -apple-system, BlinkMacSystemFont, "PingFang SC", "Microsoft YaHei", "Segoe UI", Roboto, sans-serif;
  background: var(--bg);
  color: var(--text);
  line-height: 1.6;
  min-height: 100vh;
}

.topbar {
  position: sticky;
  top: 0;
  z-index: 10;
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 12px 16px;
  background: rgba(246, 248, 251, 0.92);
  backdrop-filter: blur(8px);
  border-bottom: 1px solid var(--line);
}

.topbar h1 {
  font-size: 20px;
  font-weight: 700;
  letter-spacing: 1px;
}

.actions { display: flex; gap: 8px; }

.btn {
  border: none;
  cursor: pointer;
  border-radius: 10px;
  padding: 9px 16px;
  font-size: 14px;
  font-weight: 600;
  transition: transform 0.1s, opacity 0.15s;
}
.btn:active { transform: scale(0.96); }
.btn.primary { background: var(--primary); color: #fff; }
.btn.primary:hover { background: var(--primary-dark); }
.btn.ghost { background: #fff; color: var(--text); border: 1px solid var(--line); }
.btn.ghost:hover { border-color: #c9d2e0; }
.btn.sm { padding: 6px 12px; font-size: 13px; }
.btn:disabled { opacity: 0.55; cursor: not-allowed; }

.container {
  max-width: 960px;
  margin: 0 auto;
  padding: 16px;
  display: flex;
  flex-direction: column;
  gap: 16px;
}

.cards {
  display: grid;
  grid-template-columns: repeat(2, 1fr);
  gap: 12px;
}

.card {
  background: var(--card);
  border-radius: var(--radius);
  box-shadow: var(--shadow);
  padding: 16px;
  text-align: center;
}
.card .label { font-size: 12px; color: var(--muted); }
.card .value { font-size: 26px; font-weight: 700; margin: 4px 0 2px; }
.card .value.kg::after { content: " kg"; font-size: 13px; color: var(--muted); font-weight: 500; }
.card .value.cm::after { content: " cm"; font-size: 13px; color: var(--muted); font-weight: 500; }
.card .sub { font-size: 12px; color: var(--muted); }
.card .sub.up { color: var(--red); }
.card .sub.down { color: var(--green); }
.card .sub .val { font-weight: 700; }
.card.clickable { cursor: pointer; border: 1px solid transparent; transition: border-color 0.15s; }
.card.clickable:hover { border-color: var(--primary); }

/* ===== 身体成分 ===== */
.calorie-card {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  background: linear-gradient(135deg, #fff4f2, #fff);
  border: 1px solid #ffd9d4;
  border-radius: 12px;
  padding: 14px 16px;
  margin-bottom: 12px;
}
.cal-main { display: flex; flex-direction: column; }
.cal-label { font-size: 12px; color: var(--muted); }
.cal-remain { font-size: 34px; font-weight: 800; color: var(--green); line-height: 1.1; }
.cal-remain.over { color: var(--red); }
.cal-sub { font-size: 12px; color: var(--muted); }
.cal-budget { font-weight: 700; color: var(--primary); cursor: pointer; }
.cal-actions { display: flex; flex-direction: column; gap: 6px; flex: none; }

.cal-log { display: flex; flex-direction: column; gap: 6px; margin-bottom: 12px; }
.cal-empty { font-size: 12.5px; color: var(--muted); padding: 4px 2px; }
.cal-item {
  display: flex;
  align-items: center;
  gap: 10px;
  background: #f7f8fa;
  border-radius: 10px;
  padding: 8px 12px;
  font-size: 13px;
}
.cal-item-label { flex: 1; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.cal-item-time { color: var(--muted); font-size: 11px; flex: none; }
.cal-item-kcal { font-weight: 700; flex: none; }
.cal-del {
  flex: none;
  border: none;
  background: none;
  color: var(--muted);
  font-size: 18px;
  cursor: pointer;
  line-height: 1;
  padding: 0 2px;
}
.cal-del:hover { color: var(--red); }

@media (max-width: 520px) {
  .calorie-card { flex-direction: column; align-items: stretch; text-align: center; }
  .cal-actions { flex-direction: row; justify-content: center; }
  .cal-remain { font-size: 30px; }
}

.comp-grid {
  display: grid;
  grid-template-columns: repeat(2, 1fr);
  gap: 10px;
}
.comp-item {
  background: #f7f8fa;
  border-radius: 12px;
  padding: 12px 14px;
}
.comp-item.warn { background: rgba(255, 107, 107, 0.08); }
.comp-item.clickable { cursor: pointer; border: 1px solid transparent; transition: border-color 0.15s, background 0.15s; }
.comp-item.clickable:hover { border-color: var(--primary); background: #fff; }
.comp-label { font-size: 12px; color: var(--muted); }
.comp-value { font-size: 20px; font-weight: 700; margin: 2px 0; }
.comp-note { font-size: 11px; color: var(--muted); }
@media (min-width: 900px) {
  .comp-grid { grid-template-columns: repeat(4, 1fr); }
  .comp-value { font-size: 24px; }
}

.panel {
  background: var(--card);
  border-radius: var(--radius);
  box-shadow: var(--shadow);
  padding: 16px;
}
.panel-head {
  display: flex;
  align-items: center;
  justify-content: space-between;
  margin-bottom: 12px;
}
.panel-head h2 { font-size: 16px; font-weight: 700; }
.hint { font-size: 12px; color: var(--muted); }

.chart-wrap {
  position: relative;
  width: 100%;
  overflow-x: auto;
}
.chart-wrap svg { display: block; width: 100%; min-width: 420px; height: auto; }
.point .hit { fill: transparent; pointer-events: all; cursor: pointer; }

.chart-tip {
  position: fixed;
  z-index: 50;
  pointer-events: none;
  background: rgba(31, 36, 48, 0.92);
  color: #fff;
  font-size: 12.5px;
  line-height: 1.6;
  padding: 8px 12px;
  border-radius: 10px;
  opacity: 0;
  transform: translateY(4px);
  transition: opacity 0.12s, transform 0.12s;
  box-shadow: 0 4px 16px rgba(0, 0, 0, 0.18);
  white-space: nowrap;
}
.chart-tip b { color: #ffb4ac; }
.chart-tip .now {
  display: inline-block;
  background: var(--primary);
  color: #fff;
  font-size: 10px;
  border-radius: 4px;
  padding: 0 5px;
  margin-left: 2px;
}
.chart-tip.show { opacity: 1; transform: translateY(0); }
.axis-text { fill: var(--muted); font-size: 11px; }
.line-ghost { fill: none; stroke: var(--line); stroke-width: 1; stroke-dasharray: 4 4; }
.weight-line { fill: none; stroke: var(--primary); stroke-width: 2.5; stroke-linecap: round; }
.dot { fill: #fff; stroke: var(--primary); stroke-width: 2; }
.dot.last { fill: var(--primary); }
.grid-line { stroke: var(--line); stroke-width: 1; }

.rec-area { white-space: pre-wrap; font-size: 14.5px; }
.rec-area .placeholder { color: var(--muted); font-size: 14px; }
.rec-area .loading { color: var(--muted); }
.rec-area .loading::after {
  content: "";
  display: inline-block;
  width: 14px;
  height: 14px;
  margin-left: 8px;
  border: 2px solid var(--line);
  border-top-color: var(--primary);
  border-radius: 50%;
  vertical-align: -2px;
  animation: spin 0.8s linear infinite;
}
@keyframes spin { to { transform: rotate(360deg); } }

/* ===== 聊天面板 ===== */
.topbar .brand { display: flex; align-items: center; gap: 8px; }
.tag {
  font-size: 11px;
  color: var(--primary);
  background: rgba(255, 107, 107, 0.12);
  padding: 2px 8px;
  border-radius: 999px;
  font-weight: 600;
}

.chat-panel { display: flex; flex-direction: column; padding: 16px; }

.chat-box {
  height: min(52vh, 460px);
  overflow-y: auto;
  display: flex;
  flex-direction: column;
  gap: 12px;
  padding: 4px 4px 8px;
  scroll-behavior: smooth;
}

.msg { display: flex; gap: 10px; align-items: flex-start; }
.msg.user { flex-direction: row-reverse; }

.avatar {
  flex: none;
  width: 32px;
  height: 32px;
  border-radius: 50%;
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 12px;
  font-weight: 700;
  color: #fff;
}
.avatar.ai { background: linear-gradient(135deg, var(--primary), #ff9a8b); }
.avatar.user { background: #4a7dff; }

.bubble {
  max-width: 78%;
  background: #f2f5fa;
  border-radius: 12px;
  border-top-left-radius: 4px;
  padding: 10px 14px;
  font-size: 14px;
  line-height: 1.7;
  white-space: pre-wrap;
  word-break: break-word;
}
.msg.user .bubble {
  background: var(--primary);
  color: #fff;
  border-radius: 12px;
  border-top-right-radius: 4px;
}
.msg.user .bubble a { color: #fff; }
.bubble a { color: var(--primary); }
.bubble .w-inline { font-weight: 700; color: var(--primary); }
.bubble.thinking::after {
  content: "";
  display: inline-block;
  width: 12px;
  height: 12px;
  margin-left: 6px;
  border: 2px solid var(--line);
  border-top-color: var(--primary);
  border-radius: 50%;
  vertical-align: -2px;
  animation: spin 0.8s linear infinite;
}

.quick-actions {
  display: flex;
  gap: 8px;
  flex-wrap: wrap;
  margin: 10px 0;
}
.chip {
  border: 1px solid var(--line);
  background: #fff;
  color: var(--text);
  border-radius: 999px;
  padding: 7px 14px;
  font-size: 13px;
  cursor: pointer;
  transition: border-color 0.15s, background 0.15s;
}
.chip:hover { border-color: var(--primary); background: rgba(255, 107, 107, 0.05); }

.chat-input {
  display: flex;
  gap: 8px;
  align-items: flex-end;
}
.chat-input textarea {
  flex: 1;
  resize: none;
  border: 1px solid var(--line);
  border-radius: 12px;
  padding: 10px 14px;
  font-size: 14px;
  font-family: inherit;
  line-height: 1.5;
  max-height: 120px;
  outline: none;
  background: #fff;
  color: var(--text);
}
.chat-input textarea:focus { border-color: var(--primary); }
.send-btn { flex: none; }

.icon-btn {
  flex: none;
  width: 42px;
  height: 42px;
  padding: 0;
  font-size: 18px;
  line-height: 1;
  background: #fff;
  border: 1px solid var(--line);
  border-radius: 12px;
  cursor: pointer;
  transition: border-color 0.15s, background 0.15s;
}
.icon-btn:hover { border-color: var(--primary); background: rgba(255, 107, 107, 0.05); }

.photo-preview {
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 8px 10px;
  background: #f7f8fa;
  border: 1px solid var(--line);
  border-radius: 10px;
  margin-bottom: 8px;
}
.photo-thumbs { display: flex; flex-wrap: wrap; gap: 8px; }
.photo-thumb { position: relative; }
.photo-thumb img {
  width: 52px;
  height: 52px;
  object-fit: cover;
  border-radius: 8px;
  display: block;
}
.photo-remove {
  position: absolute;
  top: -6px;
  right: -6px;
  width: 20px;
  height: 20px;
  border-radius: 50%;
  border: none;
  background: rgba(31, 36, 48, 0.75);
  color: #fff;
  font-size: 13px;
  line-height: 1;
  cursor: pointer;
  padding: 0;
}
.photo-remove:hover { background: var(--red); }
.hidden { display: none !important; }

.bubble-img {
  display: block;
  max-width: 220px;
  max-height: 180px;
  border-radius: 10px;
  margin-bottom: 8px;
  object-fit: cover;
}

.foot {
  text-align: center;
  font-size: 12px;
  color: var(--muted);
  padding: 8px 0 24px;
}

/* 移动端聊天气泡更宽 */
@media (max-width: 520px) {
  .bubble { max-width: 85%; }
  .chat-box { height: 50vh; }
  .chat-panel { padding: 12px; }
}

.toast {
  position: fixed;
  left: 50%;
  bottom: 32px;
  transform: translateX(-50%) translateY(20px);
  background: rgba(31, 36, 48, 0.9);
  color: #fff;
  font-size: 13px;
  padding: 10px 18px;
  border-radius: 999px;
  opacity: 0;
  pointer-events: none;
  transition: opacity 0.25s, transform 0.25s;
  z-index: 99;
  max-width: 86vw;
}
.toast.show { opacity: 1; transform: translateX(-50%) translateY(0); }

/* ===== 目标体重弹窗 ===== */
.modal {
  position: fixed;
  inset: 0;
  background: rgba(31, 36, 48, 0.45);
  display: flex;
  align-items: center;
  justify-content: center;
  z-index: 100;
  opacity: 0;
  pointer-events: none;
  transition: opacity 0.2s;
}
.modal.show { opacity: 1; pointer-events: auto; }
.modal-box {
  background: #fff;
  border-radius: 14px;
  padding: 20px;
  width: min(320px, 90vw);
  box-shadow: 0 8px 30px rgba(0, 0, 0, 0.15);
}
.modal-box h3 { font-size: 16px; margin-bottom: 6px; }
.modal-tip { font-size: 12px; color: var(--muted); margin-bottom: 12px; }
.modal-field { display: flex; align-items: center; gap: 8px; margin-bottom: 16px; }
.modal-field input {
  flex: 1;
  border: 1px solid var(--line);
  border-radius: 10px;
  padding: 10px 12px;
  font-size: 16px;
  outline: none;
  min-width: 0;
}
.modal-field input:focus { border-color: var(--primary); }
.modal-field span { color: var(--muted); font-size: 14px; }
.modal-actions { display: flex; justify-content: flex-end; gap: 8px; }

/* ===== 历史记录弹窗 ===== */
.history-modal .modal-box { width: min(560px, 94vw); max-height: 86vh; display: flex; flex-direction: column; }
.history-head { display: flex; align-items: center; justify-content: space-between; margin-bottom: 10px; }
.history-head h3 { font-size: 16px; font-weight: 700; }
.history-head-actions { display: flex; align-items: center; gap: 8px; }
.hist-close {
  border: none; background: #f2f5fa; color: var(--muted); font-size: 20px;
  width: 30px; height: 30px; border-radius: 8px; cursor: pointer; line-height: 1;
}
.hist-close:hover { background: #e8ecf2; color: var(--text); }

.history-summary {
  display: grid; grid-template-columns: repeat(3, 1fr); gap: 8px; margin-bottom: 10px;
}
.hist-sum-item {
  background: #f7f8fa; border-radius: 10px; padding: 8px 10px; text-align: center;
}
.hist-sum-item span { display: block; font-size: 11px; color: var(--muted); }
.hist-sum-item b { display: block; font-size: 16px; font-weight: 700; margin: 2px 0; }
.hist-sum-item b.up { color: var(--red); }
.hist-sum-item em { display: block; font-style: normal; font-size: 11px; color: var(--muted); }

.history-chart {
  background: #fafbfd; border: 1px solid var(--line); border-radius: 12px;
  padding: 8px 10px; margin-bottom: 10px; overflow-x: auto;
}
.history-chart svg { display: block; width: 100%; min-width: 400px; height: auto; }
.history-chart .placeholder { padding: 24px 0; text-align: center; }
.bar-eat { fill: var(--green); opacity: 0.85; }
.bar-eat.over { fill: var(--red); opacity: 0.85; }
.bar-budget { stroke: var(--primary); stroke-width: 1.5; stroke-dasharray: 4 3; }

.history-list { overflow-y: auto; max-height: 38vh; display: flex; flex-direction: column; gap: 4px; }
.hist-row {
  display: flex; align-items: center; gap: 10px;
  background: #f7f8fa; border-radius: 8px; padding: 7px 12px; font-size: 13px;
}
.hist-row.latest { background: rgba(255, 107, 107, 0.08); }
.hist-date { color: var(--muted); font-size: 12px; width: 88px; flex: none; }
.hist-val { font-weight: 700; flex: 1; }
.hist-diff { font-size: 12px; font-weight: 700; flex: none; min-width: 56px; text-align: right; }
.hist-diff.up { color: var(--red); }
.hist-diff.down { color: var(--green); }
.hist-diff.flat { color: var(--muted); }
.hist-diff.none { color: var(--muted); font-weight: 400; }

.cal-hist-day {
  background: #f7f8fa; border-radius: 10px; padding: 10px 12px; margin-bottom: 6px;
}
.cal-hist-head { display: flex; align-items: center; justify-content: space-between; font-size: 13.5px; }
.cal-hist-head b { font-size: 14px; }
.cal-hist-head span { font-size: 12px; font-weight: 700; }
.cal-hist-head span.up { color: var(--red); }
.cal-hist-sub { font-size: 12px; color: var(--muted); margin: 2px 0 6px; }
.cal-hist-sub b { color: var(--text); }
.cal-hist-item {
  display: flex; align-items: center; gap: 8px; font-size: 12.5px;
  background: #fff; border-radius: 7px; padding: 5px 10px; margin-top: 4px;
}
.cal-hist-item span { flex: 1; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.cal-hist-item em { font-style: normal; color: var(--muted); font-size: 11px; flex: none; }
.cal-hist-item b { flex: none; font-weight: 700; }
.cal-hist-empty { font-size: 12px; color: var(--muted); padding: 4px 2px; }

@media (max-width: 520px) {
  .history-summary { grid-template-columns: 1fr; gap: 6px; }
  .hist-sum-item { display: flex; align-items: center; gap: 8px; text-align: left; padding: 6px 10px; }
  .hist-sum-item span { width: 44px; flex: none; }
  .hist-sum-item b { margin: 0; flex: 1; font-size: 14px; }
  .hist-sum-item em { flex: none; }
  .hist-date { width: 76px; }
}

/* ===== AI 设置中心 ===== */
.settings-box { width: min(400px, 92vw); }
.set-label {
  display: block;
  font-size: 12px;
  color: var(--muted);
  margin: 10px 0 4px;
}
.settings-box input[type="text"],
.settings-box input[type="password"],
.settings-box input[type="number"],
.settings-box textarea {
  width: 100%;
  border: 1px solid var(--line);
  border-radius: 10px;
  padding: 10px 12px;
  font-size: 14px;
  outline: none;
  font-family: inherit;
}
.settings-box input:focus,
.settings-box textarea:focus { border-color: var(--primary); }
.settings-box textarea { resize: vertical; min-height: 64px; line-height: 1.6; }
.set-divider {
  border-top: 1px solid var(--line);
  margin: 14px 0 4px;
}

/* ===== PC 端 ===== */
@media (min-width: 900px) {
  .container { padding: 24px; gap: 20px; }
  .topbar { padding: 14px 28px; }
  .card .value { font-size: 34px; }
  .cards { grid-template-columns: repeat(4, 1fr); }
}

/* ===== 手机端窄屏（紧凑布局） ===== */
@media (max-width: 520px) {
  .topbar h1 { font-size: 17px; }
  .btn { padding: 8px 12px; font-size: 13px; }
  .cards { grid-template-columns: repeat(4, 1fr); gap: 6px; }
  .card { padding: 8px 2px; }
  .card .label { font-size: 10px; }
  .card .value { font-size: 15px; margin: 2px 0; }
  .card .value.kg::after { font-size: 9px; }
  .card .value.cm::after { font-size: 9px; }
  .card .sub { display: none; }

  .comp-grid { grid-template-columns: repeat(3, 1fr); gap: 6px; }
  .comp-item { padding: 8px 4px; text-align: center; }
  .comp-label { font-size: 10px; }
  .comp-value { font-size: 16px; }
  .comp-note { display: none; }

  .panel { padding: 12px; }
}
