/* ===== 顏色與字體：從 App 圖示的綠色漸層取色 ===== */
:root {
  --bg: #F5F6F3;
  --surface: #FFFFFF;
  --surface-2: #EEF1EC;
  --ink: #141A18;
  --ink-2: #5E6A66;
  --ink-3: #97A29E;
  --line: rgba(20, 26, 24, 0.08);
  --brand-1: #8EDB78;
  --brand-2: #16A58C;
  --brand-3: #0B7E74;
  --brand-soft: rgba(22, 165, 140, 0.12);
  --protein: #16A58C;
  --carbs: #E6A23C;
  --fat: #E0735A;
  --fiber: #7A9E4B;
  --shadow: 0 1px 2px rgba(16, 24, 20, 0.04), 0 8px 24px rgba(16, 24, 20, 0.06);
  --radius: 22px;
  --font: -apple-system, BlinkMacSystemFont, "SF Pro Text", "PingFang TC", "Noto Sans TC", "Microsoft JhengHei", sans-serif;
  color-scheme: light;
}

@media (prefers-color-scheme: dark) {
  :root {
    --bg: #0E1211;
    --surface: #171D1B;
    --surface-2: #202826;
    --ink: #EEF3F1;
    --ink-2: #A3B0AC;
    --ink-3: #6B7773;
    --line: rgba(255, 255, 255, 0.08);
    --brand-soft: rgba(52, 199, 168, 0.16);
    --shadow: 0 1px 2px rgba(0, 0, 0, 0.3), 0 8px 24px rgba(0, 0, 0, 0.25);
    color-scheme: dark;
  }
}

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

html, body { margin: 0; background: var(--bg); color: var(--ink); }
body {
  font-family: var(--font);
  font-size: 16px;
  line-height: 1.5;
  -webkit-font-smoothing: antialiased;
  min-height: 100dvh;
}
button, input { font: inherit; color: inherit; }
button { cursor: pointer; }

.app {
  max-width: 480px;
  margin: 0 auto;
  padding: calc(env(safe-area-inset-top) + 20px) 20px calc(env(safe-area-inset-bottom) + 170px);
}

/* ===== 頂部 ===== */
.topbar { display: flex; justify-content: space-between; align-items: flex-end; margin-bottom: 20px; }
.eyebrow { margin: 0; font-size: 13px; color: var(--ink-2); letter-spacing: 0.02em; }
.title { margin: 2px 0 0; font-size: 28px; font-weight: 700; letter-spacing: -0.01em; }
.topbar-actions { display: flex; gap: 8px; }
.icon-btn {
  width: 44px; height: 44px; border-radius: 50%;
  border: 1px solid var(--line); background: var(--surface);
  display: grid; place-items: center; box-shadow: var(--shadow);
  transition: transform .15s ease;
}
.icon-btn:active { transform: scale(0.94); }
.icon-btn svg { width: 20px; height: 20px; fill: none; stroke: var(--ink); stroke-width: 1.7; stroke-linecap: round; stroke-linejoin: round; }

/* ===== 卡片 ===== */
.card { background: var(--surface); border-radius: var(--radius); box-shadow: var(--shadow); border: 1px solid var(--line); }

.summary { display: flex; align-items: center; gap: 22px; padding: 22px; }
.ring-wrap { position: relative; width: 132px; height: 132px; flex: none; }
.ring { width: 100%; height: 100%; transform: rotate(-90deg); }
.ring circle { fill: none; stroke-width: 10; stroke-linecap: round; }
.ring-track { stroke: var(--surface-2); }
.ring-fill { stroke: var(--brand-2); stroke-dasharray: 326.7; stroke-dashoffset: 326.7; transition: stroke-dashoffset .6s cubic-bezier(.2,.8,.2,1), stroke .3s; }
.ring-fill.over { stroke: var(--fat); }
.ring-center { position: absolute; inset: 0; display: grid; place-content: center; text-align: center; }
.ring-number { font-size: 30px; font-weight: 700; letter-spacing: -0.02em; font-variant-numeric: tabular-nums; line-height: 1.1; }
.ring-unit { font-size: 12px; color: var(--ink-2); }

.summary-side { flex: 1; min-width: 0; display: grid; gap: 8px; }
.stat { display: grid; }
.stat-label { font-size: 12px; color: var(--ink-2); }
.stat-value { font-size: 14px; color: var(--ink-2); font-variant-numeric: tabular-nums; white-space: nowrap; }
.stat-value b { color: var(--ink); font-size: 17px; }
.macro-head { display: flex; justify-content: space-between; font-size: 12px; color: var(--ink-2); margin-bottom: 4px; font-variant-numeric: tabular-nums; }
.macro-head b { color: var(--ink); font-weight: 600; }
.bar { height: 5px; background: var(--surface-2); border-radius: 99px; overflow: hidden; }
.bar i { display: block; height: 100%; width: 0; border-radius: inherit; transition: width .6s cubic-bezier(.2,.8,.2,1); }
[data-key="protein"] .bar i { background: var(--protein); }
[data-key="carbs"] .bar i { background: var(--carbs); }
[data-key="fat"] .bar i { background: var(--fat); }
[data-key="fiber"] .bar i { background: var(--fiber); }

/* ===== 今日紀錄 ===== */
.meals { margin-top: 28px; }
.section-head { display: flex; justify-content: space-between; align-items: baseline; margin-bottom: 12px; }
.section-head h2 { margin: 0; font-size: 18px; font-weight: 650; }
.count { font-size: 13px; color: var(--ink-2); font-variant-numeric: tabular-nums; }
.meal-list { list-style: none; margin: 0; padding: 0; display: grid; grid-template-columns: minmax(0, 1fr); gap: 10px; }
.meal-row { position: relative; border-radius: 18px; animation: rise .35s cubic-bezier(.2,.8,.2,1); }
.meal {
  position: relative; z-index: 1; touch-action: pan-y;
  transition: transform .28s cubic-bezier(.2,.8,.2,1);
  display: flex; align-items: center; gap: 14px; padding: 10px 14px 10px 10px;
  background: var(--surface); border: 1px solid var(--line); border-radius: 18px; box-shadow: var(--shadow);
}
.meal img { width: 56px; height: 56px; object-fit: cover; border-radius: 12px; flex: none; background: var(--surface-2); }
.meal-body { flex: 1; min-width: 0; }
.meal-meta { font-size: 12px; color: var(--ink-2); }
.meal-food { font-weight: 600; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.meal-kcal { font-weight: 700; font-variant-numeric: tabular-nums; white-space: nowrap; }
.meal-kcal small { font-weight: 400; color: var(--ink-2); font-size: 12px; margin-left: 2px; }
.tag { display: inline-block; padding: 1px 8px; border-radius: 99px; background: var(--brand-soft); color: var(--brand-3); font-size: 11px; font-weight: 600; margin-right: 6px; }
@media (prefers-color-scheme: dark) { .tag { color: var(--brand-1); } }
.empty { margin: 4px 0 0; padding: 26px 16px; text-align: center; color: var(--ink-3); font-size: 14px; border: 1.5px dashed var(--line); border-radius: 18px; }

@keyframes rise { from { opacity: 0; transform: translateY(8px); } to { opacity: 1; transform: none; } }

/* ===== 圓形拍照按鈕 ===== */
.shutter-dock {
  position: fixed; left: 0; right: 0; bottom: 0;
  padding: 18px 0 calc(env(safe-area-inset-bottom) + 18px);
  display: grid; justify-items: center; gap: 6px;
  background: linear-gradient(to top, var(--bg) 55%, transparent);
  pointer-events: none;
}
.shutter {
  pointer-events: auto;
  width: 88px; height: 88px; border-radius: 50%;
  padding: 6px; background: var(--surface);
  box-shadow: 0 0 0 1px var(--line), 0 10px 30px rgba(11, 126, 116, 0.28);
  display: grid; cursor: pointer;
  transition: transform .18s cubic-bezier(.2,.8,.2,1);
}
.shutter:active { transform: scale(0.93); }
.shutter-inner {
  border-radius: 50%;
  background: linear-gradient(145deg, var(--brand-1), var(--brand-2) 55%, var(--brand-3));
  display: grid; place-items: center;
}
.shutter-inner svg { width: 32px; height: 32px; fill: none; stroke: #fff; stroke-width: 1.8; stroke-linejoin: round; }
.shutter-hint { margin: 0; font-size: 13px; color: var(--ink-2); pointer-events: auto; }

/* ===== 確認視窗 ===== */
.sheet-backdrop { position: fixed; inset: 0; background: rgba(8, 12, 10, 0.4); backdrop-filter: blur(2px); animation: fade .25s ease; z-index: 10; }
.sheet {
  position: fixed; left: 0; right: 0; bottom: 0; z-index: 11;
  max-width: 520px; margin: 0 auto; max-height: 92dvh;
  background: var(--surface); border-radius: 26px 26px 0 0;
  display: flex; flex-direction: column;
  box-shadow: 0 -10px 40px rgba(0,0,0,.18);
  animation: slide-up .35s cubic-bezier(.2,.8,.2,1);
}
.sheet[hidden], .sheet-backdrop[hidden] { display: none; }
.sheet-grip { width: 40px; height: 5px; border-radius: 99px; background: var(--line); margin: 10px auto 0; flex: none; }
.sheet-scroll { overflow-y: auto; padding: 8px 20px 12px; -webkit-overflow-scrolling: touch; }
.sheet-title { font-size: 20px; margin: 6px 0 14px; }
.photo-frame { border-radius: 18px; overflow: hidden; background: var(--surface-2); aspect-ratio: 4 / 3; }
.photo-frame img { width: 100%; height: 100%; object-fit: cover; display: block; }

.segmented { display: grid; grid-template-columns: repeat(4, 1fr); gap: 4px; margin-top: 14px; padding: 4px; background: var(--surface-2); border-radius: 14px; }
.segmented button { border: 0; background: transparent; padding: 0; min-height: 40px; border-radius: 10px; font-size: 14px; color: var(--ink-2); transition: background .2s, color .2s; }
.segmented button[aria-checked="true"] { background: var(--surface); color: var(--ink); font-weight: 600; box-shadow: 0 1px 3px rgba(0,0,0,.08); }

.notice { margin: 14px 0 0; padding: 10px 12px; border-radius: 12px; background: var(--brand-soft); color: var(--brand-3); font-size: 13px; }
@media (prefers-color-scheme: dark) { .notice { color: var(--brand-1); } }

.fields { display: grid; grid-template-columns: repeat(3, 1fr); gap: 10px; margin-top: 14px; }
.field { display: grid; gap: 4px; min-width: 0; }
.field > span { font-size: 12px; color: var(--ink-2); }
.field-wide, .field-hero { grid-column: 1 / -1; }
.field input {
  width: 100%; min-width: 0; border: 1px solid var(--line); background: var(--surface-2);
  border-radius: 12px; padding: 10px 12px; font-size: 16px; outline: none;
  font-variant-numeric: tabular-nums; transition: border-color .2s, background .2s;
}
.field input:focus { border-color: var(--brand-2); background: var(--surface); }
.input-unit { position: relative; }
.input-unit input { padding-right: 34px; }
.input-unit em { position: absolute; right: 12px; top: 50%; transform: translateY(-50%); font-style: normal; font-size: 12px; color: var(--ink-3); pointer-events: none; }
.field-hero input { font-size: 26px; font-weight: 700; padding: 10px 56px 10px 14px; }
.field-hero .input-unit em { font-size: 14px; }

.refine { display: flex; gap: 8px; margin-top: 14px; }
/* iPhone：輸入框字體小於 16px 會自動放大畫面，所以一律 16px */
.refine input { flex: 1; min-width: 0; border: 1px solid var(--line); background: var(--surface-2); border-radius: 12px; padding: 12px; font-size: 16px; outline: none; }
.refine input:disabled, .refine button:disabled { opacity: .5; }

.sheet-actions { display: grid; grid-template-columns: 1fr 2fr; gap: 10px; padding: 12px 20px calc(env(safe-area-inset-bottom) + 14px); border-top: 1px solid var(--line); flex: none; }
.btn-primary, .btn-secondary, .btn-ghost { border: 0; border-radius: 14px; padding: 14px; font-size: 16px; font-weight: 600; transition: transform .15s ease, opacity .2s; }
.btn-primary { background: linear-gradient(145deg, var(--brand-2), var(--brand-3)); color: #fff; }
.btn-secondary { background: var(--surface-2); }
.btn-ghost { background: var(--surface-2); padding: 12px 18px; font-size: 16px; }
.btn-primary:active, .btn-secondary:active, .btn-ghost:active { transform: scale(0.97); }

/* ===== 提示訊息 ===== */
.toast {
  position: fixed; left: 50%; top: calc(env(safe-area-inset-top) + 14px); z-index: 20;
  transform: translate(-50%, -140%); opacity: 0;
  max-width: calc(100% - 32px); padding: 10px 16px; border-radius: 14px;
  background: var(--ink); color: var(--bg); font-size: 14px; text-align: center;
  transition: transform .35s cubic-bezier(.2,.8,.2,1), opacity .35s;
}
.toast.show { transform: translate(-50%, 0); opacity: 1; }

@keyframes fade { from { opacity: 0; } }
@keyframes slide-up { from { transform: translateY(100%); } }

@media (prefers-reduced-motion: reduce) {
  *, *::before, *::after { animation: none !important; transition: none !important; }
}

/* 電腦大螢幕：稍微放大留白 */
@media (min-width: 720px) {
  .app { padding-top: 48px; }
}

/* ===== 第 2 階段：AI 估算狀態 ===== */
.notice[hidden], .items[hidden], .analyzing[hidden], .pass-error[hidden], .btn-link[hidden] { display: none; }
.notice { display: flex; align-items: flex-start; gap: 10px; justify-content: space-between; }
.notice p { margin: 0; }
.notice.warn { background: rgba(230, 162, 60, 0.14); color: #8A5A10; }
.notice.error { background: rgba(224, 115, 90, 0.14); color: #A5412B; }
@media (prefers-color-scheme: dark) {
  .notice.warn { color: #F2C27A; }
  .notice.error { color: #F3A08C; }
}
.btn-link { border: 0; background: none; padding: 0; font-size: 13px; font-weight: 600; color: inherit; text-decoration: underline; white-space: nowrap; }

.items { list-style: none; margin: 14px 0 0; padding: 0; display: grid; gap: 6px; }
.item { display: grid; gap: 8px; padding: 12px; background: var(--surface-2); border-radius: 14px; }
.item-head { display: flex; justify-content: space-between; align-items: baseline; gap: 12px; }
.item-name { font-weight: 600; font-size: 15px; }
.item-kcal { font-weight: 600; white-space: nowrap; font-variant-numeric: tabular-nums; font-size: 15px; }
.item-foot { display: flex; justify-content: space-between; align-items: center; gap: 10px; }
.item-portion { color: var(--ink-2); font-size: 12px; min-width: 0; }
.item-source { display: inline-block; margin-right: 6px; padding: 1px 7px; border-radius: 99px; font-size: 11px; font-weight: 600; background: var(--line); color: var(--ink-2); }
.item-source.is-label { background: var(--brand-soft); color: var(--brand-3); }
@media (prefers-color-scheme: dark) { .item-source.is-label { color: var(--brand-1); } }
.portion-chips { display: flex; gap: 2px; padding: 3px; background: var(--surface); border-radius: 12px; flex: none; }
.portion-chips button { border: 0; background: transparent; min-width: 44px; height: 36px; padding: 0 8px; border-radius: 9px; font-size: 14px; color: var(--ink-2); }
.portion-chips button[aria-checked="true"] { background: var(--brand-2); color: #fff; font-weight: 600; }

.fields { position: relative; }
.analyzing {
  position: absolute; inset: -4px; z-index: 2; border-radius: 14px;
  display: flex; align-items: center; justify-content: center; gap: 10px;
  background: color-mix(in srgb, var(--surface) 82%, transparent);
  backdrop-filter: blur(3px); font-size: 14px; color: var(--ink-2);
}
.spinner { width: 20px; height: 20px; border-radius: 50%; border: 2.5px solid var(--brand-soft); border-top-color: var(--brand-2); animation: spin .8s linear infinite; }
@keyframes spin { to { transform: rotate(360deg); } }
.btn-primary:disabled, .btn-ghost:disabled { opacity: .5; pointer-events: none; }

/* ===== 通行碼視窗 ===== */
.sheet-small { max-height: none; }
.sheet-small .sheet-scroll { padding-bottom: calc(env(safe-area-inset-bottom) + 20px); }
.sheet-desc { margin: -6px 0 14px; color: var(--ink-2); font-size: 14px; }
.pass-form { display: grid; gap: 12px; }
.pass-form input { border: 1px solid var(--line); background: var(--surface-2); border-radius: 14px; padding: 14px; font-size: 20px; letter-spacing: 0.12em; text-align: center; outline: none; }
.pass-form input:focus { border-color: var(--brand-2); }
.pass-error { margin: 0; color: var(--fat); font-size: 14px; text-align: center; }

/* ===== 第 3 階段：紀錄狀態、設定、7 天圖表 ===== */
.meal { cursor: pointer; }
.meal:active { transform: scale(0.99); }
.meal img:not([src]) { visibility: hidden; }
.meal-meta { display: flex; align-items: center; flex-wrap: wrap; gap: 0 6px; }
.status { font-size: 11px; color: var(--ink-3); }
.status::before { content: "·"; margin-right: 6px; }
.status.ok { color: var(--brand-2); }
.status.warn { color: #B7791F; font-weight: 600; }
@media (prefers-color-scheme: dark) { .status.warn { color: #F2C27A; } }

.sheet-head { display: flex; justify-content: space-between; align-items: center; }
.sheet-head .sheet-title { margin: 6px 0 14px; }
.btn-close { width: 40px; height: 40px; border: 0; border-radius: 50%; background: var(--surface-2); display: grid; place-items: center; }
.btn-close svg { width: 18px; height: 18px; fill: none; stroke: var(--ink-2); stroke-width: 2; stroke-linecap: round; }
.sheet-actions-single { grid-template-columns: 1fr; }

.settings { display: grid; gap: 18px; padding-bottom: 8px; }
.settings .field-hero input { font-size: 26px; }
.reminders { border: 0; margin: 0; padding: 0; display: grid; gap: 8px; }
.reminders legend { font-size: 12px; color: var(--ink-2); margin-bottom: 6px; padding: 0; }
.time-row { display: flex; justify-content: space-between; align-items: center; padding: 6px 6px 6px 14px; background: var(--surface-2); border-radius: 14px; }
.time-row span { font-weight: 600; }
.time-row input { border: 0; background: var(--surface); border-radius: 10px; padding: 8px 12px; font-size: 17px; min-height: 40px; font-variant-numeric: tabular-nums; }
.hint { margin: 2px 0 0; font-size: 12px; color: var(--ink-3); }

.chart-summary { margin: -4px 0 8px; color: var(--ink-2); font-size: 14px; }
.chart svg { width: 100%; height: auto; display: block; }
.chart .axis { stroke: var(--line); stroke-width: 1; }
.chart .target { stroke: var(--ink-2); stroke-width: 1.25; stroke-dasharray: 4 4; }
.chart .bar-ok { fill: var(--brand-2); }
.chart .bar-over { fill: var(--fat); }
.chart .bar-value { font-size: 10.5px; fill: var(--ink-2); text-anchor: middle; font-variant-numeric: tabular-nums; }
.chart .day { font-size: 11.5px; fill: var(--ink); text-anchor: middle; }
.chart .day.today { font-weight: 700; }
.chart .weekday { font-size: 10.5px; fill: var(--ink-3); text-anchor: middle; }
.legend { display: flex; gap: 14px; flex-wrap: wrap; font-size: 12px; color: var(--ink-2); margin: 6px 0 16px; }
.legend span { display: inline-flex; align-items: center; gap: 6px; }
.dot { width: 10px; height: 10px; border-radius: 3px; display: inline-block; }
.dot-ok { background: var(--brand-2); }
.dot-over { background: var(--fat); }
.legend .line { width: 16px; border-top: 1.5px dashed var(--ink-2); display: inline-block; }

.day-table { width: 100%; border-collapse: collapse; font-size: 13px; font-variant-numeric: tabular-nums; }
.day-table th { font-weight: 500; font-size: 11.5px; color: var(--ink-2); text-align: right; padding: 6px 4px; border-bottom: 1px solid var(--line); }
.day-table td { text-align: right; padding: 9px 4px; border-bottom: 1px solid var(--line); white-space: nowrap; }
.day-table th:first-child, .day-table td:first-child { text-align: left; }
.day-table td.over { color: var(--fat); font-weight: 600; }
.sheet-link { display: block; margin: 18px 0 8px; padding: 14px; border-radius: 14px; background: var(--brand-soft); color: var(--brand-3); font-weight: 600; text-decoration: none; text-align: center; }
@media (prefers-color-scheme: dark) { .sheet-link { color: var(--brand-1); } }
.sheet-link[hidden] { display: none; }

/* ===== 新流程：先確認吃了什麼，送出後才計算熱量 ===== */
.items-wrap { margin-top: 16px; }
.items-head { display: flex; justify-content: space-between; align-items: baseline; margin-bottom: 8px; }
.items-head > span:first-child { font-size: 12px; color: var(--ink-2); }
.items-hint { font-size: 12px; color: var(--ink-3); }
.items-wrap .items { margin-top: 0; }
.items-wrap .analyzing { position: static; inset: auto; backdrop-filter: none; background: var(--surface-2); padding: 22px; border-radius: 14px; margin-bottom: 8px; }
.item-remove { width: 32px; height: 32px; margin: -6px -6px -6px 0; border: 0; border-radius: 50%; background: transparent; display: grid; place-items: center; flex: none; }
.item-remove svg { width: 16px; height: 16px; fill: none; stroke: var(--ink-3); stroke-width: 2; stroke-linecap: round; }
.item-remove:active { background: var(--line); }
.meal-kcal.is-pending { font-size: 13px; font-weight: 500; color: var(--ink-3); }

/* ===== 往左滑：編輯、刪除 ===== */
.meal-actions {
  position: absolute; top: 0; right: 0; bottom: 0; width: 150px;
  display: flex; gap: 6px; padding-left: 6px;
  opacity: 0; transition: opacity .2s;
}
.meal-row.is-open .meal-actions { opacity: 1; }
.meal-action { flex: 1; border: 0; border-radius: 16px; font-size: 15px; font-weight: 600; }
.meal-action.is-edit { background: var(--surface-2); color: var(--ink); }
.meal-action.is-delete { background: var(--fat); color: #fff; }
.meal-action:active { opacity: .8; }
.swipe-hint { margin: 10px 0 0; text-align: center; font-size: 12px; color: var(--ink-3); }
.swipe-hint[hidden] { display: none; }

/* ===== 拍照＋相簿 ===== */
.shutter-row { display: flex; align-items: center; gap: 28px; }
.album-btn {
  pointer-events: auto; width: 60px; height: 60px; border-radius: 50%;
  background: var(--surface); box-shadow: 0 0 0 1px var(--line), var(--shadow);
  display: grid; place-content: center; justify-items: center; gap: 1px; cursor: pointer;
  font-size: 11px; color: var(--ink-2); transition: transform .18s cubic-bezier(.2,.8,.2,1);
}
.album-btn:active { transform: scale(0.93); }
.album-btn svg { width: 22px; height: 22px; fill: none; stroke: var(--brand-3); stroke-width: 1.7; stroke-linejoin: round; stroke-linecap: round; }
@media (prefers-color-scheme: dark) { .album-btn svg { stroke: var(--brand-1); } }
.album-spacer { visibility: hidden; }

.item-right { display: flex; align-items: center; gap: 4px; flex: none; }
.items-hint { font-weight: 600; color: var(--ink-2); }
.field input[type="datetime-local"] { -webkit-appearance: none; appearance: none; min-height: 44px; text-align: left; }
.sheet.is-edit .photo-frame { aspect-ratio: auto; height: 120px; }
