/* ================= 变量与重置 ================= */
:root {
  --green: #4CAF50;
  --green-dark: #388E3C;
  --green-light: #E8F5E9;
  --red: #e53935;
  --orange: #FF9800;
  --blue: #2196F3;
  --purple: #9C27B0;
  --text: #212121;
  --text-2: #757575;
  --text-3: #9E9E9E;
  --bg: #F5F6F8;
  --card: #FFFFFF;
  --line: #ECECEC;
  --radius: 14px;
  --shadow: 0 1px 4px rgba(0,0,0,.06);
  --tabbar-h: 58px;
  --safe-b: env(safe-area-inset-bottom, 0px);
  --safe-t: env(safe-area-inset-top, 0px);
}

* { box-sizing: border-box; margin: 0; padding: 0; -webkit-tap-highlight-color: transparent; }

html { font-size: 16px; }

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

.hidden { display: none !important; }

a { color: inherit; text-decoration: none; }
button { font-family: inherit; font-size: 1rem; cursor: pointer; border: none; background: none; }
input, select, textarea {
  font-family: inherit; font-size: 1rem;
  width: 100%; padding: 11px 13px;
  border: 1px solid var(--line); border-radius: 10px;
  background: #FAFAFA; color: var(--text); outline: none;
}
input:focus, select:focus, textarea:focus { border-color: var(--green); background: #fff; }
input[type="number"] { -moz-appearance: textfield; }

/* ================= 通用组件 ================= */
.btn {
  display: inline-flex; align-items: center; justify-content: center; gap: 6px;
  padding: 11px 20px; border-radius: 10px;
  font-weight: 600; transition: opacity .15s, transform .05s;
}
.btn:active { transform: scale(.97); }
.btn:disabled { opacity: .5; pointer-events: none; }
.btn-primary { background: var(--green); color: #fff; }
.btn-ghost { background: var(--green-light); color: var(--green-dark); }
.btn-danger { background: #FDECEA; color: var(--red); }
.btn-block { width: 100%; }
.btn-sm { padding: 6px 12px; font-size: .875rem; border-radius: 8px; }

.card {
  background: var(--card); border-radius: var(--radius);
  box-shadow: var(--shadow); padding: 16px; margin: 0 14px 12px;
}

.section-title {
  font-size: .85rem; font-weight: 600; color: var(--text-2);
  margin: 18px 16px 8px; letter-spacing: .5px;
}

/* 页面标题 */
.page-header {
  padding: calc(14px + var(--safe-t)) 16px 6px;
  display: flex; align-items: center; justify-content: space-between;
}
.page-header h2 { font-size: 1.3rem; font-weight: 700; }

/* chips 单选 */
.chips { display: flex; gap: 8px; flex-wrap: wrap; }
.chip {
  padding: 7px 16px; border-radius: 999px;
  background: #fff; border: 1px solid var(--line);
  font-size: .9rem; color: var(--text-2); transition: all .15s;
}
.chip.active { background: var(--green); border-color: var(--green); color: #fff; font-weight: 600; }

/* ================= 登录层 ================= */
.auth-screen {
  min-height: 100dvh; display: flex; align-items: center; justify-content: center;
  padding: 24px; padding-top: calc(24px + var(--safe-t));
  background: linear-gradient(160deg, var(--green) 0%, var(--green-dark) 100%);
}
.auth-card {
  width: 100%; max-width: 360px; background: #fff;
  border-radius: 20px; padding: 34px 26px 26px; text-align: center;
  box-shadow: 0 10px 40px rgba(0,0,0,.18);
}
.auth-logo { font-size: 3rem; }
.auth-card h1 { font-size: 1.35rem; margin: 8px 0 4px; }
.auth-sub { color: var(--text-2); font-size: .875rem; margin-bottom: 22px; }
.auth-card form { display: flex; flex-direction: column; gap: 12px; }
.auth-toggle { color: var(--blue); font-size: .875rem; margin-top: 16px; }
.auth-error { color: var(--red); font-size: .85rem; margin-top: 10px; min-height: 1.2em; }

/* ================= 主布局 ================= */
.app { min-height: 100dvh; display: flex; flex-direction: column; }

#views { flex: 1; padding-bottom: calc(var(--tabbar-h) + var(--safe-b) + 12px); }
.view { display: none; animation: fadeIn .18s ease; }
.view.active { display: block; }
@keyframes fadeIn { from { opacity: 0; transform: translateY(4px); } to { opacity: 1; transform: none; } }

/* 底部 Tab */
.tabbar {
  position: fixed; left: 0; right: 0; bottom: 0;
  height: calc(var(--tabbar-h) + var(--safe-b));
  padding-bottom: var(--safe-b);
  display: flex; background: #fff;
  border-top: 1px solid var(--line);
  z-index: 50;
}
.tab {
  flex: 1; display: flex; flex-direction: column; align-items: center; justify-content: center;
  gap: 1px; color: var(--text-3); font-size: .7rem;
}
.tab-icon { font-size: 1.4rem; line-height: 1; filter: grayscale(1) opacity(.55); transition: filter .15s; }
.tab.active { color: var(--green-dark); font-weight: 600; }
.tab.active .tab-icon { filter: none; }

/* ================= 今日总览 ================= */
.hero {
  margin: 10px 14px 12px; background: var(--card);
  border-radius: var(--radius); box-shadow: var(--shadow);
  padding: 18px 16px; display: flex; align-items: center; gap: 18px;
}
.ring-wrap { position: relative; width: 128px; height: 128px; flex-shrink: 0; }
.ring-center {
  position: absolute; inset: 0; display: flex; flex-direction: column;
  align-items: center; justify-content: center;
}
.ring-center .num { font-size: 1.55rem; font-weight: 700; line-height: 1.1; }
.ring-center .den { font-size: .72rem; color: var(--text-3); }
.hero-stats { flex: 1; display: flex; flex-direction: column; gap: 10px; }
.hstat { display: flex; align-items: center; justify-content: space-between; }
.hstat .hstat-label { font-size: .82rem; color: var(--text-2); }
.hstat .hstat-val { font-weight: 700; font-size: .95rem; }
.dot { display: inline-block; width: 8px; height: 8px; border-radius: 50%; margin-right: 6px; vertical-align: 1px; }

/* 营养素条 */
.macro-bar { display: flex; height: 10px; border-radius: 5px; overflow: hidden; background: var(--line); }
.macro-bar > div { transition: width .4s ease; }
.macro-legend { display: flex; justify-content: space-between; margin-top: 8px; font-size: .78rem; color: var(--text-2); }

/* 统计三卡 */
.stat-grid { display: grid; grid-template-columns: repeat(3, 1fr); gap: 10px; margin: 0 14px 12px; }
.stat-cell {
  background: var(--card); border-radius: 12px; box-shadow: var(--shadow);
  padding: 12px 8px; text-align: center;
}
.stat-cell .v { font-size: 1.15rem; font-weight: 700; }
.stat-cell .l { font-size: .72rem; color: var(--text-2); margin-top: 2px; }

/* 记录列表 */
.meal-group { margin-bottom: 4px; }
.meal-group-title {
  display: flex; align-items: center; justify-content: space-between;
  font-size: .85rem; font-weight: 600; color: var(--text-2);
  padding: 4px 2px;
}
.meal-group-title .sum { color: var(--text-3); font-weight: 500; }

.food-item {
  display: flex; align-items: center; gap: 10px;
  background: var(--card); border-radius: 12px; box-shadow: var(--shadow);
  padding: 11px 14px; margin-bottom: 8px;
}
.food-item .fi-body { flex: 1; min-width: 0; }
.food-item .fi-name { font-weight: 600; font-size: .95rem; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.food-item .fi-sub { font-size: .76rem; color: var(--text-3); margin-top: 1px; }
.food-item .fi-kcal { font-weight: 700; color: var(--green-dark); white-space: nowrap; }
.food-item .fi-kcal small { font-weight: 400; color: var(--text-3); }
.food-item .fi-del { color: var(--text-3); font-size: 1.1rem; padding: 6px; }
.food-item .fi-del:active { color: var(--red); }

/* 空状态 */
.empty { text-align: center; color: var(--text-3); font-size: .85rem; padding: 26px 0; }
.empty .emoji { font-size: 2rem; display: block; margin-bottom: 6px; }

/* 快捷按钮 */
.quick-actions { display: flex; gap: 10px; margin: 14px 14px 4px; }
.quick-actions .btn { flex: 1; }

/* ================= 喝水卡片 ================= */
.water-bar { height: 12px; border-radius: 6px; background: #E3F2FD; overflow: hidden; margin: 10px 0 12px; }
.water-bar > div { height: 100%; background: linear-gradient(90deg, #64B5F6, #2196F3); border-radius: 6px; transition: width .4s ease; }
.water-btns { display: flex; gap: 8px; }
.water-add {
  flex: 1; padding: 10px 0 8px; border-radius: 10px;
  background: #E3F2FD; color: #1565C0;
  font-size: 1rem; font-weight: 700; line-height: 1.1;
  display: flex; flex-direction: column; align-items: center; gap: 1px;
  transition: transform .05s;
}
.water-add small { font-size: .68rem; font-weight: 400; color: #64B5F6; }
.water-add:active { transform: scale(.96); }
.water-logs { display: flex; flex-wrap: wrap; gap: 6px; margin-top: 10px; }
.water-log {
  font-size: .75rem; color: var(--text-2);
  background: #FAFAFA; border: 1px solid var(--line);
  border-radius: 999px; padding: 3px 6px 3px 10px;
  display: inline-flex; align-items: center; gap: 5px;
}
.water-log button { color: var(--text-3); font-size: .72rem; padding: 1px 4px; }
.water-log button:active { color: var(--red); }

/* ================= 拍照页 ================= */
.camera-zone {
  margin: 10px 14px 14px; border: 2px dashed #CDE8CE; border-radius: var(--radius);
  background: var(--green-light); padding: 34px 16px; text-align: center;
}
.camera-zone .big-emoji { font-size: 3rem; display: block; margin-bottom: 8px; }
.camera-zone p { color: var(--text-2); font-size: .85rem; margin-bottom: 16px; }
.camera-btns { display: flex; gap: 10px; justify-content: center; }
.photo-preview { margin: 0 14px 12px; position: relative; }
.photo-preview img { width: 100%; border-radius: var(--radius); display: block; box-shadow: var(--shadow); }

/* 识别中动画 */
.analyzing { text-align: center; padding: 22px 0; color: var(--text-2); }
.analyzing .spinner {
  width: 38px; height: 38px; margin: 0 auto 10px;
  border: 3px solid var(--green-light); border-top-color: var(--green);
  border-radius: 50%; animation: spin .8s linear infinite;
}
@keyframes spin { to { transform: rotate(360deg); } }

/* 识别结果编辑卡片 */
.food-edit-card {
  background: var(--card); border-radius: 12px; box-shadow: var(--shadow);
  padding: 13px 14px; margin-bottom: 10px;
}
.fec-head { display: flex; align-items: center; gap: 8px; margin-bottom: 9px; }
.fec-head .fec-name { flex: 1; font-weight: 600; }
.fec-badge {
  font-size: .68rem; padding: 2px 8px; border-radius: 999px;
  background: var(--green-light); color: var(--green-dark);
}
.fec-grid { display: grid; grid-template-columns: repeat(4, 1fr); gap: 7px; }
.fec-grid label { font-size: .68rem; color: var(--text-3); display: block; margin-bottom: 2px; }
.fec-grid input { padding: 7px 8px; font-size: .88rem; border-radius: 8px; }
.fec-serving { margin-top: 8px; display: flex; gap: 7px; }
.fec-serving input { padding: 7px 8px; font-size: .85rem; border-radius: 8px; }

/* ================= 历史页 ================= */
.date-stepper {
  display: flex; align-items: center; justify-content: space-between;
  background: var(--card); border-radius: 12px; box-shadow: var(--shadow);
  margin: 10px 14px 12px; padding: 8px 10px;
}
.date-stepper button { font-size: 1.3rem; color: var(--text-2); padding: 4px 12px; }
.date-stepper .d { font-weight: 600; font-size: .98rem; }
.date-stepper .back-today { font-size: .78rem; color: var(--blue); padding: 6px 8px; }

.day-summary-row {
  display: flex; align-items: center; gap: 10px;
  background: var(--card); border-radius: 12px; box-shadow: var(--shadow);
  padding: 11px 14px; margin-bottom: 8px;
}
.day-summary-row .dsr-date { flex: 1; }
.day-summary-row .dsr-date b { font-size: .92rem; }
.day-summary-row .dsr-date span { display: block; font-size: .72rem; color: var(--text-3); }
.day-summary-row .dsr-num { font-size: .82rem; text-align: right; }
.day-summary-row .dsr-num b { display: block; font-size: .95rem; }

/* ================= 设置页 ================= */
.form-row { margin-bottom: 13px; }
.form-row label { display: block; font-size: .8rem; color: var(--text-2); margin-bottom: 5px; }
.form-2col { display: grid; grid-template-columns: 1fr 1fr; gap: 10px; }
.settings-item {
  display: flex; align-items: center; justify-content: space-between;
  padding: 13px 2px; border-bottom: 1px solid var(--line); font-size: .95rem;
}
.settings-item:last-child { border-bottom: none; }
.settings-item .si-desc { font-size: .75rem; color: var(--text-3); margin-top: 2px; }
.settings-item .si-left { flex: 1; }

/* ================= 弹窗 ================= */
.modal-mask {
  position: fixed; inset: 0; background: rgba(0,0,0,.45); z-index: 100;
  display: flex; align-items: flex-end; justify-content: center;
  animation: fadeIn .15s ease;
}
.modal-sheet {
  background: #fff; width: 100%; max-width: 480px;
  border-radius: 20px 20px 0 0; padding: 20px 18px calc(20px + var(--safe-b));
  max-height: 86dvh; overflow-y: auto;
  animation: slideUp .22s ease;
}
@keyframes slideUp { from { transform: translateY(40px); opacity: .4; } to { transform: none; opacity: 1; } }
.modal-sheet h3 { font-size: 1.05rem; margin-bottom: 14px; }
.modal-actions { display: flex; gap: 10px; margin-top: 16px; }
.modal-actions .btn { flex: 1; }

/* ================= Toast ================= */
.toast {
  position: fixed; left: 50%; bottom: calc(var(--tabbar-h) + var(--safe-b) + 20px);
  transform: translateX(-50%) translateY(16px);
  background: rgba(33,33,33,.92); color: #fff;
  padding: 10px 20px; border-radius: 999px; font-size: .88rem;
  opacity: 0; pointer-events: none; transition: all .25s ease;
  z-index: 200; max-width: 86vw; text-align: center;
}
.toast.show { opacity: 1; transform: translateX(-50%) translateY(0); }

/* ================= 桌面端适配 ================= */
@media (min-width: 560px) {
  .card, .hero, .stat-grid, .quick-actions, .photo-preview, .date-stepper, .camera-zone { max-width: 520px; margin-left: auto; margin-right: auto; }
  .food-item, .food-edit-card, .day-summary-row { max-width: 520px; margin-left: auto; margin-right: auto; }
  .section-title { max-width: 520px; margin-left: auto; margin-right: auto; }
  .page-header { max-width: 520px; margin: 0 auto; }
  .tabbar { max-width: 560px; left: 50%; transform: translateX(-50%); border-radius: 16px 16px 0 0; border: 1px solid var(--line); border-bottom: none; }
}
