:root {
  --bg: #f5f7f6;
  --surface: #ffffff;
  --surface-2: #f0f4f2;
  --ink: #1f2a27;
  --ink-soft: #5d6b66;
  --line: #e3e9e6;
  --accent: #18a957;
  --accent-soft: #e6f6ec;
  --danger: #e2483d;
  --danger-soft: #fdecea;
  --shadow: 0 6px 20px rgba(31, 42, 39, 0.08);
  --radius: 16px;
}

* { box-sizing: border-box; }

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

/* ---------- Top bar ---------- */
.topbar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  padding: 12px 16px;
  background: var(--surface);
  border-bottom: 1px solid var(--line);
  position: sticky;
  top: 0;
  z-index: 20;
  flex-wrap: wrap;
}
.brand { display: flex; align-items: center; gap: 12px; }
.logo {
  font-size: 34px;
  line-height: 1;
  filter: drop-shadow(0 2px 4px rgba(0,0,0,.12));
}
.topbar h1 { margin: 0; font-size: 20px; letter-spacing: .5px; }
.subtitle { margin: 2px 0 0; font-size: 13px; color: var(--ink-soft); }

.summary { display: flex; gap: 12px; }
.summary-item {
  background: var(--accent-soft);
  border-radius: 14px;
  padding: 10px 18px;
  text-align: center;
  min-width: 96px;
}
.summary-num { display: block; font-size: 22px; font-weight: 700; color: var(--accent); line-height: 1.1; }
.summary-label { font-size: 12px; color: var(--ink-soft); }

/* ---------- Layout ---------- */
.layout {
  display: flex;
  flex-direction: column;
  gap: 14px;
  padding: 14px 16px 32px;
  max-width: 1100px;
  margin: 0 auto;
}

.panel {
  background: var(--surface);
  border: 1px solid var(--line);
  border-radius: var(--radius);
  box-shadow: var(--shadow);
  padding: 14px;
}
.panel-title { margin: 0 0 14px; font-size: 16px; }

/* ---------- 今日推荐条 ---------- */
.lucky-bar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 14px;
  background: linear-gradient(135deg, #e9f7ef 0%, #ffffff 70%);
  border: 1px solid var(--line);
  border-radius: var(--radius);
  padding: 16px 18px;
  box-shadow: var(--shadow);
  flex-wrap: wrap;
}
.lucky-bar-text { display: flex; flex-direction: column; gap: 2px; }
.lucky-bar-text strong { font-size: 16px; }
.lucky-bar-text span { font-size: 13px; color: var(--ink-soft); }

/* ---------- Editor: 左拍照区 + 右表单，重新编排 ---------- */
.editor-grid {
  display: grid;
  grid-template-columns: 170px 1fr;
  grid-template-areas:
    "photo fields"
    "name  name"
    "actions actions";
  gap: 16px;
  align-items: start;
}
.photo-block { grid-area: photo; width: 170px; display: flex; flex-direction: column; gap: 10px; }
.photo-zone {
  position: relative;
  width: 170px; height: 170px;
  background: var(--surface-2);
  border: 2px dashed var(--line);
  border-radius: 14px;
  overflow: hidden;
}
.preview { position: absolute; inset: 0; width: 100%; height: 100%; object-fit: cover; }
.photo-placeholder {
  position: absolute; inset: 0;
  display: flex; flex-direction: column; align-items: center; justify-content: center;
  text-align: center; color: var(--ink-soft); pointer-events: none;
}
.ph-icon { font-size: 30px; display: block; margin-bottom: 4px; }
.photo-placeholder p { margin: 0; font-size: 12px; }
.photo-remove {
  position: absolute; top: 6px; right: 6px;
  width: 26px; height: 26px; border-radius: 50%;
  border: none; background: rgba(31,42,39,.6); color: #fff;
  font-size: 14px; line-height: 1; cursor: pointer; z-index: 5;
}
.photo-buttons { display: flex; gap: 8px; }
.photo-buttons .btn { flex: 1; }
.fields-block { grid-area: fields; display: flex; flex-direction: column; gap: 12px; }
.hidden-input { display: none; }
.hidden { display: none !important; }

/* ---------- Buttons ---------- */
.btn {
  border: none;
  border-radius: 10px;
  padding: 9px 14px;
  font-size: 14px;
  cursor: pointer;
  transition: transform .05s ease, background .15s ease, box-shadow .15s ease;
  font-family: inherit;
}
.btn:active { transform: translateY(1px); }
.btn.primary { background: var(--accent); color: #fff; font-weight: 600; box-shadow: 0 4px 12px rgba(24,169,87,.3); }
.btn.primary:hover { background: #149a4e; }
.btn.ghost { background: rgba(255,255,255,.92); color: var(--ink); border: 1px solid var(--line); }
.btn.ghost:hover { background: #fff; }
.btn.ghost.danger { color: var(--danger); border-color: var(--danger-soft); }
.btn.danger { color: var(--danger); }
.btn.mini { padding: 6px 10px; font-size: 13px; background: var(--surface-2); }
.btn.mini.danger { background: var(--danger-soft); }
.btn.big { padding: 12px 22px; font-size: 16px; }

/* ---------- Form ---------- */
.meal-form { flex: 1 1 320px; display: flex; flex-wrap: wrap; gap: 10px; align-items: flex-end; }
.field { display: flex; flex-direction: column; gap: 5px; }
.field > span { font-size: 12px; color: var(--ink-soft); font-weight: 600; }
.field input, .field select {
  border: 1px solid var(--line);
  border-radius: 10px;
  padding: 8px 10px;
  font-size: 13px;
  background: var(--surface);
  color: var(--ink);
  font-family: inherit;
}
.field input:focus, .field select:focus {
  outline: none;
  border-color: var(--accent);
  box-shadow: 0 0 0 3px var(--accent-soft);
}
.f-name { grid-area: name; }
.field-row { display: flex; flex-wrap: wrap; gap: 10px; margin: 0; align-items: flex-end; }
.f-cal { width: 140px; }
.f-tags { width: 100%; }
.form-actions { grid-area: actions; display: flex; gap: 8px; margin: 0; align-items: flex-end; }
.form-actions .btn.primary { flex: 1; }

/* ---------- 分类多选 ---------- */
.tag-picker { display: flex; flex-wrap: wrap; gap: 6px; }
.tag-chip {
  border: 1px solid var(--line);
  background: var(--surface);
  color: var(--ink-soft);
  border-radius: 999px;
  padding: 6px 12px;
  font-size: 13px;
  cursor: pointer;
  transition: all .15s ease;
  font-family: inherit;
}
.tag-chip:hover { border-color: var(--accent); }
.tag-chip.active { background: var(--accent); color: #fff; border-color: var(--accent); }

/* ---------- Board / 公共菜单 ---------- */
.board-head { display: flex; align-items: center; justify-content: space-between; gap: 12px; flex-wrap: wrap; margin-bottom: 14px; }
.board-head .panel-title { margin: 0; }
.hint-sub { font-size: 12px; color: var(--ink-soft); font-weight: 400; }
.filters { display: flex; gap: 6px; flex-wrap: wrap; }
.chip {
  border: 1px solid var(--line);
  background: var(--surface);
  color: var(--ink-soft);
  border-radius: 999px;
  padding: 6px 14px;
  font-size: 13px;
  cursor: pointer;
  transition: all .15s ease;
}
.chip:hover { border-color: var(--accent); }
.chip.active { background: var(--accent); color: #fff; border-color: var(--accent); }

/* 选中计数 + 一键发送 */
.pick-bar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 10px;
  margin: 0 0 12px;
  flex-wrap: wrap;
  background: var(--accent-soft);
  border: 1px solid var(--line);
  border-radius: 12px;
  padding: 10px 12px;
}
.pick-info { font-size: 13px; color: var(--ink-soft); font-weight: 600; }
.btn.primary:disabled { background: #b9c6c0; box-shadow: none; cursor: not-allowed; opacity: .8; }

/* 今日餐单 */
.picked-board { border-color: var(--accent); }
.picked-total { font-size: 14px; color: var(--ink-soft); font-weight: 600; }
.picked-total strong { color: var(--danger); font-size: 20px; margin: 0 2px; font-variant-numeric: tabular-nums; }
.picked-board .card-actions .btn { font-size: 12px; padding: 5px 6px; }

.meal-grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(150px, 1fr));
  gap: 10px;
}
.meal-card {
  background: var(--surface);
  border: 1px solid var(--line);
  border-radius: 14px;
  overflow: hidden;
  display: flex;
  flex-direction: column;
  transition: transform .12s ease, box-shadow .15s ease;
}
.meal-card:hover { transform: translateY(-2px); box-shadow: var(--shadow); }
.meal-card.selected { border-color: var(--accent); box-shadow: 0 0 0 2px var(--accent); }
.meal-card.selected .card-type { background: var(--accent); }
.pick-badge {
  position: absolute;
  top: 6px; right: 6px;
  width: 22px; height: 22px;
  border-radius: 50%;
  background: var(--accent);
  color: #fff;
  font-size: 13px; font-weight: 700;
  display: none;
  align-items: center; justify-content: center;
  z-index: 6;
  box-shadow: 0 2px 6px rgba(24,169,87,.4);
}
.meal-card.selected .pick-badge { display: flex; }
.card-ph {
  position: absolute;
  inset: 0;
  display: none;
  align-items: center;
  justify-content: center;
  font-size: 36px;
}
.card-img-wrap.no-img .card-img { display: none; }
.card-img-wrap.no-img .card-ph { display: flex; }
.card-img-wrap { position: relative; aspect-ratio: 1/1; background: var(--surface-2); cursor: pointer; -webkit-touch-callout: none; -webkit-user-select: none; user-select: none; }
.card-img { width: 100%; height: 100%; object-fit: cover; display: block; -webkit-touch-callout: none; -webkit-user-select: none; user-select: none; }
/* 卡片右上角「⋯」操作按钮：iOS / 桌面都可靠地打开编辑 / 删除 */
.card-menu-btn {
  position: absolute;
  top: 6px; right: 6px;
  width: 30px; height: 30px;
  border: none; border-radius: 50%;
  background: rgba(20, 30, 27, .55);
  color: #fff; font-size: 18px; line-height: 1;
  cursor: pointer; z-index: 7;
  display: flex; align-items: center; justify-content: center;
  backdrop-filter: blur(2px);
}
.card-menu-btn:active { background: rgba(20, 30, 27, .75); }
/* 选中标记移到右下角，避免与「⋯」按钮重叠 */
.meal-card.selected .pick-badge { display: flex; }
.pick-badge { top: auto; bottom: 6px; right: 6px; }
.card-type {
  position: absolute;
  top: 6px; left: 6px;
  background: rgba(31,42,39,.72);
  color: #fff;
  font-size: 11px;
  padding: 2px 8px;
  border-radius: 999px;
  max-width: 78%;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}
.card-body { padding: 9px; display: flex; flex-direction: column; gap: 4px; flex: 1; }
.card-name { margin: 0; font-size: 13px; font-weight: 600; word-break: break-word; line-height: 1.3; }
.card-cal { margin: 0; font-size: 12px; color: var(--ink-soft); }
.card-cal .cal-num { font-size: 15px; font-weight: 700; color: var(--accent); }
.card-meta { font-size: 10px; color: #9aa6a1; margin-top: auto; }
.card-actions { display: flex; gap: 6px; margin-top: 6px; }
  .card-actions .btn { flex: 1; padding: 5px 6px; font-size: 12px; }

  /* 骨架屏 / 加载与错误态 */
  .meal-card.skeleton { pointer-events: none; }
  .skeleton-img, .sk-line {
    background: linear-gradient(90deg, var(--surface-2) 25%, #e7eeeb 37%, var(--surface-2) 63%);
    background-size: 400% 100%;
    animation: shimmer 1.3s ease-in-out infinite;
  }
  .skeleton-img { width: 100%; aspect-ratio: 1/1; }
  .sk-line { height: 12px; border-radius: 6px; margin: 8px 0; }
  .sk-line.short { width: 50%; }
  @keyframes shimmer { 0% { background-position: 100% 0; } 100% { background-position: 0 0; } }
  .grid-error {
    grid-column: 1 / -1;
    text-align: center;
    padding: 36px 16px;
    color: var(--ink-soft);
    font-size: 14px;
    line-height: 1.8;
  }
  .grid-error .btn { margin-top: 12px; }

  /* 翻页 */
  .pager {
    display: flex;
    align-items: center;
    justify-content: center;
    gap: 6px;
    flex-wrap: wrap;
    margin-top: 14px;
  }
  .pager .btn.mini { min-width: 34px; }
  .pager .pg-num.cur { background: var(--accent); color: #fff; border-color: var(--accent); }
  .pager .btn.mini:disabled { opacity: .5; cursor: not-allowed; }

  /* ---------- Empty state ---------- */
.empty-state { text-align: center; padding: 50px 20px; color: var(--ink-soft); }
.es-icon { font-size: 48px; display: block; margin-bottom: 10px; }
.empty-state p { margin: 4px 0; }
.es-hint { font-size: 13px; opacity: .8; }

/* ---------- Toast ---------- */
.toast {
  position: fixed;
  bottom: 24px; left: 50%;
  transform: translateX(-50%) translateY(20px);
  background: var(--ink);
  color: #fff;
  padding: 10px 18px;
  border-radius: 10px;
  font-size: 14px;
  opacity: 0;
  pointer-events: none;
  transition: all .25s ease;
  z-index: 50;
}
.toast.show { opacity: 1; transform: translateX(-50%) translateY(0); }

/* ---------- 随机弹层 ---------- */
.lucky-modal {
  position: fixed;
  inset: 0;
  z-index: 200;
  display: flex;
  align-items: center;
  justify-content: center;
  background: rgba(20, 30, 27, .55);
  padding: 20px;
  backdrop-filter: blur(4px);
}
.lucky-card {
  position: relative;
  background: var(--surface);
  border-radius: 22px;
  box-shadow: 0 20px 60px rgba(0,0,0,.3);
  padding: 22px;
  width: 100%;
  max-width: 380px;
  text-align: center;
  animation: pop .25s ease;
}
@keyframes pop { from { transform: scale(.9); opacity: 0; } to { transform: scale(1); opacity: 1; } }
.lucky-close {
  position: absolute;
  top: 12px; right: 12px;
  border: none;
  background: var(--surface-2);
  color: var(--ink-soft);
  width: 32px; height: 32px;
  border-radius: 50%;
  cursor: pointer;
  font-size: 16px;
}
.lucky-eyebrow { margin: 0 0 10px; font-size: 13px; color: var(--accent); font-weight: 700; letter-spacing: 2px; }
.lucky-subtitle { margin: 0 0 14px; font-size: 12px; color: var(--ink-soft); }

/* 3 槽随机 */
.lucky-slots { display: grid; grid-template-columns: repeat(3, 1fr); gap: 8px; margin-bottom: 16px; }
.lucky-slot {
  background: var(--surface-2);
  border-radius: 14px;
  padding: 8px 6px;
  text-align: center;
  display: flex;
  flex-direction: column;
  align-items: center;
}
.lucky-slot-label { font-size: 12px; font-weight: 700; color: var(--ink); margin-bottom: 6px; }
.lucky-img-wrap {
  position: relative;
  width: 100%;
  aspect-ratio: 1 / 1;
  border-radius: 12px;
  overflow: hidden;
  background: var(--surface);
  margin-bottom: 6px;
}
.lucky-img { width: 100%; height: 100%; object-fit: cover; display: block; }
.lucky-ph { position: absolute; inset: 0; display: none; align-items: center; justify-content: center; font-size: 32px; }
.lucky-name { margin: 0 0 2px; font-size: 13px; font-weight: 600; word-break: break-word; line-height: 1.25; }
.lucky-cal { margin: 0; font-size: 11px; color: var(--ink-soft); }
.lucky-hint { margin: 14px 0 0; font-size: 13px; color: var(--ink-soft); min-height: 18px; }

/* 随机弹层按钮：上下排列 + 拉开间距，避免拥挤 */
.lucky-card #luckyAgain,
.lucky-card #luckySend {
  display: block;
  width: 100%;
}
#luckySend { margin-top: 14px; }

/* 滚动中动画 */
.lucky-card.rolling .lucky-img { animation: roll .12s linear infinite; filter: blur(1px); }
@keyframes roll {
  0% { transform: scale(1.04) translateX(-2px); }
  50% { transform: scale(1.08) translateX(2px); }
  100% { transform: scale(1.04) translateX(-2px); }
}
.lucky-card.rolling .lucky-eyebrow { animation: blink .4s linear infinite; }
@keyframes blink { 50% { opacity: .3; } }
.lucky-card:not(.rolling) .lucky-img-wrap img { transition: transform .3s ease; }

/* ---------- Utility ---------- */
.hidden { display: none !important; }

/* ---------- Auth ---------- */
.auth-screen {
  position: fixed;
  inset: 0;
  z-index: 100;
  display: flex;
  align-items: center;
  justify-content: center;
  background: linear-gradient(135deg, #e9f7ef 0%, #f5f7f6 60%);
  padding: 20px;
}
.auth-card {
  background: var(--surface);
  border: 1px solid var(--line);
  border-radius: 20px;
  box-shadow: 0 12px 40px rgba(31, 42, 39, 0.12);
  padding: 28px 26px;
  width: 100%;
  max-width: 360px;
}
.auth-brand { display: flex; align-items: center; gap: 10px; justify-content: center; }
.auth-brand .logo { font-size: 30px; }
.auth-brand h1 { margin: 0; font-size: 20px; }
.auth-sub { text-align: center; color: var(--ink-soft); font-size: 13px; margin: 8px 0 18px; }
.auth-tabs { display: flex; background: var(--surface-2); border-radius: 10px; padding: 4px; margin-bottom: 16px; }
.auth-tab {
  flex: 1; border: none; background: transparent; padding: 8px; border-radius: 8px;
  font-size: 14px; cursor: pointer; color: var(--ink-soft); font-family: inherit;
}
.auth-tab.active { background: var(--surface); color: var(--accent); font-weight: 600; box-shadow: var(--shadow); }
.auth-form { display: flex; flex-direction: column; gap: 12px; }
.btn.full { width: 100%; }
.auth-error { color: var(--danger); font-size: 13px; margin: 0; min-height: 18px; text-align: center; }

/* ---------- User box (header) ---------- */
.user-box { display: flex; align-items: center; gap: 8px; flex-wrap: wrap; }
.user-name { font-size: 14px; font-weight: 600; color: var(--ink); }
.sync-state { font-size: 12px; color: var(--ink-soft); }
.sync-state.syncing { color: var(--accent); }

/* 顶栏实时热量表 */
.kcal-meter {
  display: inline-flex;
  align-items: baseline;
  gap: 4px;
  background: var(--danger-soft);
  color: var(--danger);
  border-radius: 999px;
  padding: 5px 12px;
  font-weight: 700;
  white-space: nowrap;
}
.kcal-meter .kcal-flame { font-size: 14px; }
.kcal-meter .kcal-label { font-size: 11px; font-weight: 600; opacity: .85; }
.kcal-meter .kcal-num { font-size: 17px; font-variant-numeric: tabular-nums; }
.kcal-meter .kcal-unit { font-size: 11px; font-weight: 600; }

@media (max-width: 880px) {
  .user-box { width: 100%; justify-content: flex-start; }
}

/* ---------- Mobile: compact menu grid (3 columns) ---------- */
@media (max-width: 560px) {
  .topbar { padding: 10px 12px; gap: 10px; }
  .topbar h1 { font-size: 17px; }
  .logo { font-size: 28px; }
  .summary-item { padding: 7px 12px; min-width: 78px; }
  .summary-num { font-size: 18px; }
  .layout { padding: 10px 10px 24px; gap: 12px; }
  .panel { padding: 12px; border-radius: 14px; }

  /* 编辑区移动端：照片区撑满宽 + 按钮横排 + 紧凑间距 */
  .editor-grid { gap: 12px; grid-template-columns: 1fr; grid-template-areas: "photo" "fields" "name" "actions"; }
  .photo-block { width: 100%; max-width: 220px; margin: 0 auto; }
  .photo-zone { width: 100%; height: auto; aspect-ratio: 1/1; max-width: 220px; }
  .photo-buttons { flex-direction: row; gap: 8px; }
  .photo-buttons .btn { font-size: 13px; padding: 8px 12px; flex: 1; }
  .fields-block { gap: 10px; }
  .f-cal { width: 100%; }
  .tag-chip { padding: 5px 10px; font-size: 12px; }

  .meal-grid { grid-template-columns: repeat(3, 1fr); gap: 8px; }
  .card-name { font-size: 12px; }
  .card-cal .cal-num { font-size: 14px; }
  .card-actions .btn { font-size: 11px; padding: 4px 4px; }
  .board-head { margin-bottom: 10px; }
  .panel-title { font-size: 15px; }
  .lucky-bar { padding: 12px; }
  .lucky-bar .btn.big { width: 100%; }
  .kcal-meter { padding: 4px 9px; }
  .kcal-meter .kcal-num { font-size: 15px; }
  .pick-bar { padding: 8px 10px; }
  .pick-bar .btn.primary { flex: 1; }
}

/* ---------- 卡片长按操作菜单（底部弹出） ---------- */
.action-sheet-backdrop {
  position: fixed; inset: 0; z-index: 300;
  background: rgba(20, 30, 27, .45);
  display: flex; align-items: center; justify-content: center;
  padding: 20px;
  padding-bottom: calc(20px + env(safe-area-inset-bottom));
}
.action-sheet {
  width: 100%; max-width: 320px;
  background: var(--surface);
  border-radius: 16px;
  padding: 8px;
  box-shadow: 0 20px 60px rgba(0,0,0,.3);
  animation: sheetUp .2s ease;
}
@keyframes sheetUp { from { transform: scale(.92); opacity: 0; } to { transform: scale(1); opacity: 1; } }
.as-btn {
  display: block; width: 100%; border: none; background: transparent;
  padding: 15px; font-size: 16px; cursor: pointer; border-radius: 12px;
  color: var(--ink); font-family: inherit;
}
.as-btn:hover { background: var(--surface-2); }
.as-btn.danger { color: var(--danger); font-weight: 600; }
.as-btn.cancel { color: var(--ink-soft); border-top: 1px solid var(--line); margin-top: 4px; border-radius: 0 0 12px 12px; }
