:root {
  --green: #2EA868; --green-dark: #1F7A4C; --green-deeper: #333D12;
  --green-light: #EBF0D5; --green-mid: #B8C87A;
  --blue: #378ADD; --blue-light: #E6F1FB;
  --amber: #EF9F27; --amber-light: #FAEEDA;
  --coral: #D85A30; --coral-light: #FAECE7;
  --red: #E24B4A; --red-light: #FCEBEB;
  --macro-cal: #FDDCAA; --macro-cal-text: #8B4500;
  --macro-prot: #D4E8FA; --macro-prot-text: #1A4F7A;
  --macro-fat: #FAF3DC; --macro-fat-text: #5C4817;
  --macro-carb: #E8D5F5; --macro-carb-text: #5C2880;
  --macro-fibre: #C0E8E5; --macro-fibre-text: #1A5C58;
  --purple: #7C3AED; --purple-dark: #5B21B6; --purple-light: #EDE9FE;
  --bg: #D6E48E; --surface: #FFFFFF; --surface2: #EEECEA; --entry-bg: #F6F8EE;
  --border: rgba(0,0,0,0.1); --text: #1A1A18; --text2: #5F5E5A; --text3: #888780;
  --radius: 14px; --radius-sm: 8px;
  --nav-h: 68px; --safe-bottom: env(safe-area-inset-bottom, 0px);
  --input-bg: #FFFFFF; --chip-bg: #FFFFFF; --btn-bg: #FFFFFF;
}
@media (prefers-color-scheme: dark) {
  :root {
    --bg: #111210; --surface: #1C1D1B; --surface2: #252622; --entry-bg: #1C1D1B;
    --border: rgba(255,255,255,0.1); --text: #F0EDE6; --text2: #A8A69E; --text3: #6B6965;
    --green-light: #1C2210; --blue-light: #0C1F35; --amber-light: #2A1E05; --coral-light: #2A100A; --red-light: #2A0A0A;
    --input-bg: var(--surface2); --chip-bg: var(--surface2); --btn-bg: var(--surface2);
    --entry-bg: #2A2510;
  }
  .meal-group-header { background: #163A1E; border-color: #0F2A15; }
  .log-action-btn { background: #2A2510; }
}
* { box-sizing: border-box; margin: 0; padding: 0; -webkit-tap-highlight-color: transparent; }
html, body { height: 100%; overflow: hidden; }
body { font-family: -apple-system, 'SF Pro Text', 'Helvetica Neue', sans-serif; background: var(--bg); color: var(--text); font-size: 16px; }
#app { display: flex; flex-direction: column; height: 100dvh; }

.header { background: var(--surface); border-bottom: 0.5px solid var(--border); padding: 12px 20px 10px; padding-top: max(12px, env(safe-area-inset-top,12px)); flex-shrink: 0; display: flex; align-items: center; justify-content: space-between; }
.header-title { font-size: 18px; font-weight: 600; letter-spacing: -0.3px; }
.header-date { font-size: 12px; color: var(--text2); margin-top: 1px; }
.header-dot { width: 8px; height: 8px; border-radius: 50%; background: var(--green); }

.content { flex: 1; overflow-y: auto; -webkit-overflow-scrolling: touch; padding-bottom: calc(var(--nav-h) + var(--safe-bottom) + 16px); }
.page { display: none; padding: 16px; }
.page.active { display: block; }

.nav { position: fixed; bottom: 0; left: 0; right: 0; height: calc(var(--nav-h) + var(--safe-bottom)); background: var(--surface); border-top: 0.5px solid var(--border); display: flex; padding-bottom: var(--safe-bottom); }
.nav-btn { flex: 1; display: flex; flex-direction: column; align-items: center; justify-content: center; gap: 3px; border: none; background: none; cursor: pointer; color: var(--text3); font-size: 10px; padding: 8px 0; transition: color 0.15s; }
.nav-btn.active { color: var(--green-dark); background: var(--green-light); border-radius: 10px; }
.nav-icon { font-size: 22px; line-height: 1; }

.card { background: var(--surface); border-radius: var(--radius); padding: 16px; margin-bottom: 12px; border: 0.5px solid var(--border); }
.card-title { font-size: 11px; font-weight: 600; letter-spacing: 0.06em; text-transform: uppercase; color: var(--text2); margin-bottom: 12px; }

.metrics-grid { display: grid; grid-template-columns: 1fr 1fr; gap: 10px; margin-bottom: 12px; }
.metric { background: var(--surface2); border-radius: var(--radius-sm); padding: 12px 14px; }
.metric-label { font-size: 11px; color: var(--text2); margin-bottom: 4px; }
.metric-value { font-size: 26px; font-weight: 600; letter-spacing: -0.5px; line-height: 1; }
.metric-unit { font-size: 11px; color: var(--text3); margin-top: 2px; }

.btn { display: inline-flex; align-items: center; justify-content: center; gap: 6px; padding: 0 18px; height: 42px; border-radius: var(--radius-sm); border: 1px solid var(--border); background: var(--btn-bg); cursor: pointer; font-size: 14px; font-weight: 500; color: var(--text); transition: opacity 0.1s; -webkit-appearance: none; }
.btn:active { opacity: 0.7; }
.btn-primary { background: var(--green); color: #fff; border-color: var(--green-dark); }
.btn-danger { background: var(--red-light); color: var(--red); border-color: var(--red); }
.btn-full { width: 100%; }
.btn-sm { height: 34px; padding: 0 12px; font-size: 13px; }

.form-group { margin-bottom: 12px; }
.result-dropdown { margin-bottom: 8px; }
.result-dropdown-header { display:flex;align-items:center;justify-content:space-between;padding:5px 12px;border-radius:16px;border:1px solid;font-size:12px;cursor:pointer;font-weight:500;font-family:inherit;width:100%;text-align:left; }
.result-dropdown-list { margin-top:4px;display:flex;flex-direction:column;gap:4px; }
.result-dropdown-item { padding:5px 12px;border-radius:16px;border:1px solid;font-size:12px;cursor:pointer;text-align:left;font-family:inherit;width:100%;font-weight:400; }
#f-name { scroll-margin-top: 8px; border-color: var(--green-mid); background: var(--input-bg); }
.form-label { font-size: 12px; color: var(--text2); margin-bottom: 5px; display: block; font-weight: 500; }
.form-row { display: grid; grid-template-columns: 1fr 1fr; gap: 10px; }
input[type=text], input[type=number], input[type=time], select, textarea { width: 100%; background: var(--input-bg); border: 1px solid var(--border); border-radius: var(--radius-sm); padding: 10px 12px; font-size: 15px; color: var(--text); font-family: inherit; -webkit-appearance: none; appearance: none; }
input:focus, select:focus { outline: 2px solid var(--green); outline-offset: -1px; }

.mic-wrap { display: flex; flex-direction: column; align-items: center; padding: 8px 0 16px; }
.mic-btn { width: 72px; height: 72px; border-radius: 50%; border: none; background: var(--green-light); font-size: 30px; cursor: pointer; display: flex; align-items: center; justify-content: center; transition: transform 0.1s; margin-bottom: 10px; }
.mic-btn:active { transform: scale(0.94); }
.mic-btn.listening { background: var(--coral-light); animation: pulse 1s infinite; }
@keyframes pulse { 0%,100%{transform:scale(1)} 50%{transform:scale(1.05)} }
.mic-hint { font-size: 13px; color: var(--text2); text-align: center; max-width: 260px; line-height: 1.4; }
.voice-debug {
  margin-top: 10px; width: 100%; max-width: 320px; max-height: 140px; overflow-y: auto;
  padding: 8px 10px; border-radius: var(--radius-sm); background: var(--bg2, #f3f4f0);
  border: 1px solid var(--border); font-family: ui-monospace, SFMono-Regular, Menlo, Monaco, Consolas, monospace;
  font-size: 11px; line-height: 1.45; color: var(--text2); text-align: left;
}
.voice-debug-line { margin-bottom: 2px; word-break: break-word; }

.chips { display: flex; flex-wrap: wrap; gap: 8px; }
.chip { padding: 7px 14px; border-radius: 20px; background: var(--chip-bg); border: 1px solid var(--green-mid); font-size: 13px; cursor: pointer; color: var(--green-deeper); transition: background 0.12s; }
.chip:active { background: var(--green-light); color: var(--green-dark); }

.log-list { display: flex; flex-direction: column; gap: 10px; }
.log-list .meal-group { margin-bottom: 0; }
.log-list .meal-group-items { display: flex; flex-direction: column; gap: 10px; padding-top: 6px; }
.meal-group { margin-bottom: 8px; }
.meal-group-header { display: flex; align-items: center; gap: 8px; padding: 7px 12px; background: #1E4D2A; border: 1px solid #163A20; border-radius: var(--radius-sm); cursor: pointer; user-select: none; }
.meal-group-title { font-size: 12px; font-weight: 600; text-transform: uppercase; letter-spacing: 0.05em; color: #C8E6C9; flex: 1; }
.meal-group-count { font-size: 11px; color: rgba(200,230,201,0.6); }
.meal-group-arrow { font-size: 12px; color: rgba(200,230,201,0.65); transition: transform 0.2s; }
.meal-group-items { padding-top: 6px; }
.log-item { background: var(--entry-bg); border-radius: var(--radius-sm); overflow: hidden; border: 2px solid rgba(90,120,55,0.45); margin-bottom: 0; position: relative; font-family: 'Manrope', -apple-system, 'SF Pro Text', 'Helvetica Neue', sans-serif; }
.recipe-nest { background: var(--entry-bg); border-radius: var(--radius-sm); overflow: hidden; border: 2px solid rgba(90,120,55,0.45); border-left: 3px solid var(--amber); font-family: 'Manrope', -apple-system, 'SF Pro Text', 'Helvetica Neue', sans-serif; }
.recipe-nest-header { display: flex; align-items: stretch; cursor: pointer; user-select: none; }
.recipe-nest-header:active { opacity: 0.92; }
.recipe-nest-main { flex: 1; padding: 8px 12px 8px 8px; min-width: 0; }
.recipe-nest-name { font-size: 14px; font-weight: 600; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.recipe-nest-meta { font-size: 11px; color: var(--text2); margin-top: 2px; }
.recipe-nest-arrow { font-size: 12px; color: var(--text2); transition: transform 0.2s; flex-shrink: 0; }
.recipe-nest-del { width: 36px; flex-shrink: 0; display: flex; align-items: center; justify-content: center; border: none; background: transparent; cursor: pointer; color: var(--red); font-size: 14px; padding: 0; }
.recipe-nest-del:active { opacity: 0.7; }
.recipe-nest-items { display: flex; flex-direction: column; gap: 10px; padding: 0 0 8px 10px; border-top: 1px solid rgba(90,120,55,0.18); }
.log-item-main { padding: 8px 12px 8px; box-sizing: border-box; min-width: 0; }
.log-name { font-size: 14px; font-weight: 500; }
.log-sub { font-size: 12px; color: var(--text2); margin-top: 2px; }
.log-actions { display: flex; gap: 0; border-top: 1px solid rgba(180,130,0,0.2); }
.log-action-btn { flex: 1; padding: 8px; border: none; background: #FFF3C4; font-size: 12px; cursor: pointer; color: var(--text2); display: flex; align-items: center; justify-content: center; gap: 4px; }
.log-action-btn:not(:last-child) { border-right: 0.5px solid rgba(180,130,0,0.2); }
.log-btn-nutrients { background: #C8E8D0; color: #1B5E2E; }
.log-btn-edit { background: #C5DCF5; color: #0A3A6E; }
.log-btn-del { background: #FAD0CC; color: #7A1A1A; }
.log-action-btn:active { opacity: 0.7; }
.log-time-pill { font-size:11px;font-weight:600;color:var(--text2);background:var(--surface2);padding:2px 7px;border-radius:8px; }
@keyframes flashEntry { 0%{background:var(--green-light);}100%{background:var(--entry-bg);} }
.flash { animation: flashEntry 1.5s ease-out; }
@keyframes spin { to { transform: rotate(360deg); } }
.spinning { display: inline-block; animation: spin 0.8s linear infinite; }
@keyframes hintPulse { 0%,100%{opacity:1} 50%{opacity:0.3} }
.hint-escalate { font-size:11px;background:none;border:none;cursor:pointer;padding:0;font-weight:500;text-decoration:underline;text-align:left;animation:hintPulse 1.2s ease-in-out 3; }
@keyframes macroFlash { 0%,100%{background:var(--input-bg);border-color:var(--border)} 45%{background:var(--mf-bg,var(--green-light));border-color:var(--mf-border,var(--green))} }
.macro-flash { animation: macroFlash 0.7s ease-out; }

.badge { display: inline-block; padding: 3px 8px; border-radius: 6px; font-size: 11px; font-weight: 600; }
.badge-food { background: var(--green-light); color: var(--green-deeper); }
.badge-sleep { background: var(--blue-light); color: #0C447C; }
.badge-weight { background: var(--amber-light); color: #633806; }
.badge-mood { background: var(--coral-light); color: #4A1B0C; }
.badge-energy { background: var(--amber-light); color: #633806; }
.badge-green { background: var(--green-light); color: var(--green-deeper); }
.badge-amber { background: var(--amber-light); color: #633806; }
.badge-blue { background: var(--blue-light); color: #0C447C; }

.bar-chart { display: flex; align-items: flex-end; gap: 6px; height: 80px; }
.bar-col { flex: 1; display: flex; flex-direction: column; align-items: center; gap: 4px; }
.bar-fill { width: 100%; border-radius: 3px 3px 0 0; min-height: 2px; }
.bar-lbl { font-size: 10px; color: var(--text3); }
.bar-val { font-size: 9px; color: var(--text2); }

.water-glasses { display: flex; flex-wrap: wrap; gap: 8px; justify-content: center; padding: 8px 0; }
.glass { width: 38px; height: 48px; border-radius: 0 0 6px 6px; border: 1.5px solid var(--border); position: relative; cursor: pointer; overflow: hidden; transition: border-color 0.15s; }
.glass.filled { border-color: var(--blue); }
.glass-fill { position: absolute; bottom: 0; left: 0; right: 0; background: var(--blue-light); transition: height 0.3s; }
.water-progress-track { height: 8px; background: var(--surface2); border-radius: 4px; overflow: hidden; margin: 8px 0; }
.water-progress-fill { height: 100%; background: var(--blue); border-radius: 4px; transition: width 0.3s; }

.goal-item { padding: 12px 14px; background: var(--surface2); border-radius: var(--radius-sm); margin-bottom: 8px; }
.goal-header { display: flex; justify-content: space-between; align-items: baseline; margin-bottom: 8px; }
.goal-name { font-size: 14px; font-weight: 500; }
.goal-track { height: 6px; background: var(--border); border-radius: 3px; overflow: hidden; }
.goal-fill { height: 100%; border-radius: 3px; transition: width 0.4s; }
.goal-nums { font-size: 11px; color: var(--text2); margin-top: 5px; }

.ai-input-row { display: flex; gap: 8px; margin-bottom: 12px; }
.ai-input-row input { flex: 1; }
.ai-bubble { background: var(--surface2); border-radius: var(--radius); padding: 14px 16px; font-size: 14px; line-height: 1.6; color: var(--text); white-space: pre-wrap; }
.ai-thinking { color: var(--text2); font-style: italic; }

.scatter { position: relative; height: 100px; border-left: 0.5px solid var(--border); border-bottom: 0.5px solid var(--border); margin: 8px 0 4px; }
.dot { position: absolute; width: 10px; height: 10px; border-radius: 50%; transform: translate(-50%, 50%); }
.trend-bars { display: flex; align-items: flex-end; gap: 3px; height: 50px; margin-top: 8px; }
.trend-bar { flex: 1; border-radius: 2px 2px 0 0; }

.empty { font-size: 14px; color: var(--text2); text-align: center; padding: 24px 0; line-height: 1.6; }
.empty-icon { font-size: 32px; margin-bottom: 8px; }

.toast { position: fixed; bottom: calc(var(--nav-h) + var(--safe-bottom) + 16px); left: 50%; transform: translateX(-50%); background: var(--text); color: var(--surface); padding: 10px 20px; border-radius: 20px; font-size: 13px; font-weight: 500; opacity: 0; transition: opacity 0.2s; pointer-events: none; white-space: nowrap; z-index: 999; }
.toast.show { opacity: 1; }

/* MODAL / EDIT SHEET */
.modal-overlay { position: fixed; inset: 0; background: rgba(0,0,0,0.5); z-index: 100; display: none; align-items: flex-end; }
.modal-overlay.open { display: flex; }
.modal { background: var(--surface); border-radius: var(--radius) var(--radius) 0 0; padding: 20px 16px; padding-bottom: calc(20px + var(--safe-bottom)); width: 100%; max-height: 85dvh; overflow-y: auto; }
.modal-title { font-size: 16px; font-weight: 600; margin-bottom: 16px; flex-shrink: 0; }
.modal-actions { display: flex; gap: 8px; margin-top: 16px; flex-shrink: 0; }
/* Entry detail: only modal-fields scrolls; title and actions stay pinned */
#modal > .modal { display: flex; flex-direction: column; overflow: hidden; padding-bottom: 0; }
#modal-fields { overflow-y: auto; flex: 1; min-height: 0; padding-bottom: 8px; }
#modal > .modal > .modal-actions { padding-bottom: calc(20px + var(--safe-bottom)); }

.upload-zone { border: 1.5px dashed var(--border); border-radius: var(--radius); padding: 28px 20px; text-align: center; color: var(--text2); font-size: 14px; cursor: pointer; line-height: 1.6; }
.upload-zone:active { background: var(--surface2); }

.data-table { width: 100%; border-collapse: collapse; font-size: 12px; }
.data-table th { text-align: left; padding: 6px 8px; color: var(--text2); font-weight: 600; border-bottom: 0.5px solid var(--border); }
.data-table td { padding: 6px 8px; border-bottom: 0.5px solid var(--border); color: var(--text); }

/* RECIPES */
.recipe-header-row { display: flex; align-items: center; justify-content: space-between; margin-bottom: 12px; }
.recipe-add-btn { width: 34px; padding: 0; font-size: 20px; font-weight: 600; line-height: 1; }
.recipe-search { width: 100%; margin-bottom: 10px; background: var(--input-bg); border: 1px solid var(--border); border-radius: var(--radius-sm); padding: 10px 12px; font-size: 15px; color: var(--text); font-family: inherit; }
.recipe-filters { margin-bottom: 12px; }
.recipe-filter-chip.active,
.recipe-log-meal-chip.active {
  background: var(--green-light);
  border-color: var(--green);
  color: var(--green-dark);
  font-weight: 600;
}
.recipe-row {
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 12px 10px;
  margin-bottom: 6px;
  background: var(--entry-bg);
  border: 1.5px solid rgba(90, 120, 55, 0.32);
  border-radius: var(--radius-sm);
  cursor: pointer;
}
.recipe-row:active { opacity: 0.85; }
.recipe-row-main { flex: 1; min-width: 0; }
.recipe-row-name { font-size: 14px; font-weight: 600; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.recipe-row-sub { font-size: 12px; color: var(--text2); margin-top: 2px; }
.recipe-fav {
  border: none;
  background: none;
  font-size: 20px;
  line-height: 1;
  cursor: pointer;
  color: var(--text3);
  padding: 4px 6px;
  flex-shrink: 0;
}
.recipe-fav.is-fav { color: var(--red); }
.recipe-del,
.recipe-del-btn {
  border: none;
  background: #FAD0CC;
  color: #7A1A1A;
  width: 32px;
  height: 32px;
  border-radius: var(--radius-sm);
  font-size: 20px;
  font-weight: 600;
  line-height: 1;
  cursor: pointer;
  flex-shrink: 0;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  padding: 0;
}
.recipe-del-btn { width: auto; min-width: 42px; height: 42px; border: 1px solid var(--red); }

#recipe-detail-modal > .modal {
  display: flex;
  flex-direction: column;
  overflow: hidden;
  padding-bottom: 0;
  max-height: 85dvh;
}
.recipe-detail-top { flex-shrink: 0; }
.recipe-detail-title-row { display: flex; align-items: center; gap: 8px; margin-bottom: 6px; }
.recipe-detail-title { flex: 1; margin-bottom: 0; min-width: 0; }
.recipe-detail-close { border: none; background: none; font-size: 20px; cursor: pointer; color: var(--text2); padding: 0 4px; }
.recipe-detail-meta { font-size: 13px; color: var(--text2); margin-bottom: 12px; }
.recipe-detail-body { overflow-y: auto; flex: 1; min-height: 0; padding-bottom: 8px; }
.recipe-detail-macros { display: grid; grid-template-columns: repeat(4, 1fr); gap: 8px; margin-bottom: 16px; }
.recipe-detail-macro { border-radius: var(--radius-sm); padding: 10px 6px; text-align: center; }
.recipe-detail-macro-val { font-size: 15px; font-weight: 700; line-height: 1.1; }
.recipe-detail-macro-lbl { font-size: 10px; margin-top: 3px; opacity: 0.85; text-transform: uppercase; letter-spacing: 0.03em; }
.recipe-detail-ingredients-label { font-size: 11px; font-weight: 600; color: var(--text2); text-transform: uppercase; letter-spacing: 0.05em; margin-bottom: 8px; }
.recipe-detail-ingredient {
  display: flex;
  justify-content: space-between;
  gap: 10px;
  font-size: 13px;
  padding: 8px 0;
  border-bottom: 0.5px solid var(--border);
}
.recipe-detail-ingredient-meta { color: var(--text2); font-size: 12px; white-space: nowrap; }
.recipe-detail-actions {
  padding-bottom: calc(20px + var(--safe-bottom));
  flex-wrap: wrap;
}
.recipe-detail-actions .btn-primary { flex: 1 1 100%; }
.recipe-detail-actions .btn:not(.btn-primary) { flex: 1; }

.servings-stepper {
  display: flex;
  align-items: center;
  gap: 8px;
}
.servings-stepper input {
  flex: 1;
  text-align: center;
  font-weight: 600;
}
.servings-stepper-btn {
  width: 42px;
  height: 42px;
  border-radius: var(--radius-sm);
  border: 1px solid var(--border);
  background: var(--btn-bg);
  font-size: 20px;
  font-weight: 600;
  cursor: pointer;
  color: var(--text);
  flex-shrink: 0;
}
.recipe-log-preview { text-align: center; margin-bottom: 16px; padding: 12px; background: var(--surface2); border-radius: var(--radius-sm); }
.recipe-log-kcal { font-size: 28px; font-weight: 700; letter-spacing: -0.5px; color: var(--green-dark); }
.recipe-log-kcal-sub { font-size: 12px; color: var(--text2); margin-top: 4px; }

input[type=search], input[type=date] {
  width: 100%;
  background: var(--input-bg);
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  padding: 10px 12px;
  font-size: 15px;
  color: var(--text);
  font-family: inherit;
  -webkit-appearance: none;
  appearance: none;
}
