:root {
  --bg: #0b1020;
  --bg-2: #11182e;
  --card: #161f3a;
  --card-2: #1b264a;
  --line: #28335c;
  --text: #eef2ff;
  --muted: #8b95b8;
  --accent: #34d399;     /* emerald */
  --accent-2: #38bdf8;   /* sky */
  --danger: #f87171;
  --radius: 16px;
  --safe-bottom: env(safe-area-inset-bottom, 0px);
  --safe-top: env(safe-area-inset-top, 0px);
}

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

html, body {
  margin: 0;
  padding: 0;
  background: var(--bg);
  color: var(--text);
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Helvetica, Arial, sans-serif;
  font-size: 17px;
  -webkit-font-smoothing: antialiased;
  overscroll-behavior-y: contain;
}

.hidden { display: none !important; }
.muted { color: var(--muted); }

/* ===== Login ===== */
.login {
  min-height: 100dvh;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 24px;
  background:
    radial-gradient(120% 80% at 50% -10%, #1a2a52 0%, var(--bg) 60%);
}
.login-card {
  width: 100%;
  max-width: 340px;
  text-align: center;
  background: var(--card);
  border: 1px solid var(--line);
  border-radius: 24px;
  padding: 36px 28px calc(28px + var(--safe-bottom));
  box-shadow: 0 20px 60px rgba(0,0,0,.45);
}
.logo { font-size: 44px; }
.login-card h1 { margin: 8px 0 4px; font-size: 26px; letter-spacing: .3px; }
.login-card p { margin: 0 0 22px; }
.pin-input {
  width: 100%;
  text-align: center;
  font-size: 34px;
  letter-spacing: 14px;
  padding: 14px 0;
  margin-bottom: 16px;
  background: var(--bg-2);
  border: 1px solid var(--line);
  border-radius: 14px;
  color: var(--text);
}
.pin-input:focus { outline: none; border-color: var(--accent-2); }
.error { color: var(--danger); min-height: 20px; margin: 14px 0 0; font-size: 14px; }

/* ===== Buttons ===== */
.btn {
  display: block;
  width: 100%;
  border: none;
  border-radius: 14px;
  padding: 15px;
  font-size: 17px;
  font-weight: 600;
  color: #06231a;
  cursor: pointer;
}
.btn-primary { background: linear-gradient(135deg, var(--accent), var(--accent-2)); }
.btn-primary:active { filter: brightness(.92); }
.btn-save { margin: 6px 0 8px; }
.btn-secondary {
  background: var(--bg-2);
  color: var(--text);
  border: 1px solid var(--line);
}
.btn-secondary:active { filter: brightness(1.15); }
.btn[disabled] { opacity: .6; }

/* ===== Meals ===== */
.meal-analyzing {
  display: flex; align-items: center; gap: 10px;
  color: var(--muted); font-size: 14px; padding: 14px 2px;
}
.spinner {
  width: 18px; height: 18px; border-radius: 50%;
  border: 2px solid var(--line); border-top-color: var(--accent);
  animation: spin .8s linear infinite;
}
@keyframes spin { to { transform: rotate(360deg); } }

.meal-confirm {
  margin-top: 12px;
  background: var(--bg-2);
  border: 1px solid var(--line);
  border-radius: 12px;
  padding: 12px;
}
.meal-confirm-top { display: flex; gap: 12px; }
.meal-confirm img, .meal-thumb {
  width: 64px; height: 64px; border-radius: 10px; object-fit: cover; flex: 0 0 auto;
  background: var(--card-2);
}
.meal-confirm .meal-name { font-weight: 600; }
.meal-sub { color: var(--muted); font-size: 13px; margin-top: 2px; }
.macro-chips { display: flex; flex-wrap: wrap; gap: 6px; margin-top: 8px; }
.chip {
  font-size: 12px; padding: 4px 8px; border-radius: 999px;
  background: var(--card-2); color: var(--text);
}
.chip b { color: var(--accent); }
.range { color: var(--muted); font-size: 11px; }
.meal-actions { display: flex; gap: 8px; margin-top: 12px; }
.meal-actions .btn { flex: 1; padding: 11px; font-size: 15px; }

.meal-row {
  display: flex; align-items: center; gap: 10px;
  padding: 10px 0; border-top: 1px solid var(--line);
}
.meal-row:first-child { border-top: none; }
.meal-row .meal-body { flex: 1; min-width: 0; }
.meal-row .meal-name { font-weight: 600; font-size: 15px; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.meal-row .meal-kcal { color: var(--accent); font-weight: 600; font-size: 14px; }
.meal-del {
  flex: 0 0 auto; background: none; border: none; color: var(--muted);
  font-size: 22px; line-height: 1; padding: 4px 6px; cursor: pointer;
}
.meal-empty { color: var(--muted); font-size: 14px; padding: 10px 2px 2px; }
.macro-total {
  margin-top: 12px; padding-top: 12px; border-top: 1px solid var(--line);
  display: flex; justify-content: space-between; flex-wrap: wrap; gap: 6px;
  font-size: 13px; color: var(--muted);
}
.macro-total b { color: var(--text); }
.macro-total.empty { display: none; }

.meal-btn-row { display: flex; gap: 8px; }
.meal-btn-row .btn { flex: 1; padding: 13px 8px; }
.tappable { cursor: pointer; }

/* macro steppers */
.macro-editor { margin: 12px 0; display: flex; flex-direction: column; gap: 10px; }
.stepper { display: flex; align-items: center; justify-content: space-between; gap: 10px; }
.stepper-label { font-size: 15px; color: var(--text); }
.stepper-ctrl { display: flex; align-items: center; gap: 8px; }
.stepper-btn {
  width: 40px; height: 40px; border-radius: 10px; border: 1px solid var(--line);
  background: var(--bg-2); color: var(--text); font-size: 22px; line-height: 1; cursor: pointer;
}
.stepper-btn:active { filter: brightness(1.25); }
.stepper-val {
  display: inline-block; box-sizing: border-box; min-width: 70px;
  text-align: center; background: var(--bg-2); border: 1px solid var(--line);
  border-radius: 10px; color: var(--text); padding: 10px 6px; font-size: 16px; font-weight: 600;
}

/* described-meal placeholder */
.meal-thumb-ph {
  display: flex; align-items: center; justify-content: center;
  font-size: 30px; background: var(--card-2);
}

/* Low / Recommended / High portion buttons */
.portion { margin: 12px 0 2px; }
.portion-quick { display: flex; gap: 8px; }
.portion-btn {
  flex: 1; padding: 10px 6px; border-radius: 12px; border: 1px solid var(--line);
  background: var(--bg-2); color: var(--text); font-size: 14px; font-weight: 600; cursor: pointer;
  display: flex; flex-direction: column; align-items: center; gap: 2px;
}
.portion-btn small { font-weight: 500; color: var(--muted); font-size: 11px; }
.portion-btn.sel { border-color: var(--accent); background: rgba(52, 211, 153, .14); }
.portion-btn.sel small { color: var(--accent); }
.portion-summary { text-align: center; color: var(--muted); font-size: 14px; margin: 12px 0 4px; }
.portion-adv-toggle { display: block; margin: 0 auto; }
.portion-adv { margin-top: 6px; }

/* bottom-sheet overlay (meal editor) */
.overlay {
  position: fixed; inset: 0; z-index: 60;
  background: rgba(0,0,0,.55);
  display: flex; align-items: flex-end; justify-content: center;
}
.overlay.hidden { display: none; }
.overlay-card {
  width: 100%; max-width: 560px; margin: 0;
  border-radius: 20px 20px 0 0;
  padding: 18px 16px calc(20px + var(--safe-bottom));
}
.overlay-card #ed-name { margin-bottom: 4px; }
.meal-text-row { display: flex; gap: 8px; margin-top: 10px; }
.meal-text-input {
  flex: 1; min-width: 0;
  background: var(--bg-2); border: 1px solid var(--line); border-radius: 12px;
  color: var(--text); padding: 13px; font-size: 15px;
}
.meal-text-input:focus { outline: none; border-color: var(--accent-2); }
.meal-text-btn { width: auto; flex: 0 0 auto; padding: 13px 16px; }

/* ===== To-dos ===== */
.todo-progress { color: var(--muted); font-size: 15px; font-weight: 600; }
.todo-bar {
  height: 8px; border-radius: 999px; background: var(--card);
  border: 1px solid var(--line); overflow: hidden; margin: 0 2px 16px;
}
.todo-bar-fill {
  height: 100%; width: 0%;
  background: linear-gradient(90deg, var(--accent), var(--accent-2));
  transition: width .3s ease;
}
.todo-item {
  display: flex; align-items: center; gap: 12px;
  padding: 13px 0; border-top: 1px solid var(--line);
}
.todo-item:first-child { border-top: none; }
.todo-check {
  flex: 0 0 auto; width: 26px; height: 26px; border-radius: 50%;
  border: 2px solid var(--line); display: flex; align-items: center; justify-content: center;
  color: transparent; font-size: 15px; cursor: pointer; background: none;
}
.todo-item.done .todo-check {
  background: linear-gradient(135deg, var(--accent), var(--accent-2));
  border-color: transparent; color: #06231a;
}
.todo-item.auto .todo-check { cursor: default; }
.todo-label { flex: 1; min-width: 0; font-size: 16px; }
.todo-item.done .todo-label { color: var(--muted); text-decoration: line-through; }
.todo-tag { font-size: 11px; color: var(--muted); flex: 0 0 auto; }
.todo-del {
  flex: 0 0 auto; background: none; border: none; color: var(--muted);
  font-size: 20px; line-height: 1; padding: 4px 6px; cursor: pointer;
}
.todo-section-label {
  font-size: 12px; text-transform: uppercase; letter-spacing: .5px;
  color: var(--muted); margin: 14px 2px 2px;
}
.todo-add { display: flex; gap: 8px; margin-top: 14px; }
.todo-add input {
  flex: 1; min-width: 0;
  background: var(--bg-2); border: 1px solid var(--line); border-radius: 12px;
  color: var(--text); padding: 12px; font-size: 15px;
}
.todo-add input:focus { outline: none; border-color: var(--accent-2); }
.todo-add .btn { width: auto; flex: 0 0 auto; padding: 12px 16px; }
.btn-complete-all { margin-top: 10px; }
.todo-schedule {
  display: flex; align-items: center; gap: 8px; flex-wrap: wrap;
  margin-top: 10px; font-size: 13px; color: var(--muted);
}
.todo-schedule input[type="date"] {
  background: var(--bg-2); border: 1px solid var(--line); color: var(--text);
  border-radius: 10px; padding: 7px 9px; font-size: 14px;
}
.todo-schedule-hint { font-size: 12px; opacity: .8; }
.todo-once {
  flex: 0 0 auto; font-size: 11px; color: var(--accent-2);
  border: 1px solid var(--line); border-radius: 999px; padding: 2px 8px;
}

/* meal history (lazy-loaded previous days) */
.meal-day-head {
  display: flex; justify-content: space-between; align-items: baseline;
  margin: 18px 4px 8px; font-size: 14px; font-weight: 600;
}
.meal-day-head .day-kcal { color: var(--accent); font-weight: 600; font-size: 13px; }
.meal-day-card {
  background: var(--card); border: 1px solid var(--line);
  border-radius: var(--radius); padding: 4px 16px;
}
.meal-history-loading { color: var(--muted); font-size: 13px; text-align: center; padding: 14px; }

/* dropdowns */
select {
  background: var(--bg-2); border: 1px solid var(--line); color: var(--text);
  border-radius: 10px; padding: 9px 10px; font-size: 14px; -webkit-appearance: none; appearance: none;
}
select:focus { outline: none; border-color: var(--accent-2); }
.todo-schedule select { flex: 0 0 auto; }
.todo-schedule input[type="date"] { flex: 1; }

/* link-style button */
.btn-link {
  display: block; width: 100%; margin-top: 12px;
  background: none; border: none; color: var(--accent-2);
  font-size: 14px; cursor: pointer; padding: 8px;
}
.topbar .btn-link { width: auto; margin: 0; font-size: 15px; }

/* meal day-complete button */
.meal-day-btn { margin-top: 12px; font-size: 14px; }
.meal-day-btn.done {
  background: linear-gradient(135deg, var(--accent), var(--accent-2)); color: #06231a; border-color: transparent;
}

/* manage repeating tasks */
.manage-row {
  display: flex; align-items: center; gap: 8px; flex-wrap: wrap;
  padding: 12px 0; border-top: 1px solid var(--line);
}
.manage-row:first-child { border-top: none; }
.manage-row .manage-title {
  flex: 1 1 100%; min-width: 0;
  background: var(--bg-2); border: 1px solid var(--line); color: var(--text);
  border-radius: 10px; padding: 10px; font-size: 15px;
}
.manage-row .manage-title:focus { outline: none; border-color: var(--accent-2); }
.manage-row select { flex: 1 1 auto; }
.manage-row .meal-del, .manage-row .todo-del { flex: 0 0 auto; }
.manage-freq-row { display: flex; gap: 8px; margin-top: 10px; flex-wrap: wrap; }
.manage-freq-row select { flex: 1 1 auto; }
.manage-freq-row .btn { width: auto; flex: 0 0 auto; padding: 9px 16px; }

/* ===== App shell ===== */
.app { min-height: 100dvh; }
.view {
  padding: calc(16px + var(--safe-top)) 16px calc(96px + var(--safe-bottom));
  max-width: 560px;
  margin: 0 auto;
}
.topbar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  margin: 6px 2px 16px;
}
.topbar h2 { margin: 0; font-size: 28px; letter-spacing: .3px; }
.date-input {
  background: var(--card);
  border: 1px solid var(--line);
  color: var(--text);
  border-radius: 10px;
  padding: 8px 10px;
  font-size: 15px;
}

/* ===== Cards ===== */
.card {
  background: var(--card);
  border: 1px solid var(--line);
  border-radius: var(--radius);
  padding: 16px;
  margin-bottom: 14px;
}
.card-head {
  display: flex;
  align-items: center;
  justify-content: space-between;
  font-weight: 600;
  font-size: 16px;
  margin-bottom: 12px;
}
.card-head .unit { color: var(--muted); font-weight: 500; font-size: 14px; }

/* ===== Finance rows ===== */
.acct-row {
  display: flex;
  align-items: center;
  gap: 10px;
  margin-bottom: 10px;
}
.acct-label {
  flex: 1 1 auto;
  min-width: 0;
  background: var(--bg-2);
  border: 1px solid var(--line);
  border-radius: 10px;
  color: var(--text);
  padding: 12px;
  font-size: 15px;
}
.acct-value {
  flex: 0 0 42%;
  background: var(--bg-2);
  border: 1px solid var(--line);
  border-radius: 10px;
  color: var(--text);
  padding: 12px;
  font-size: 17px;
  text-align: right;
}
.acct-label:focus, .acct-value:focus, .big-input:focus { outline: none; border-color: var(--accent-2); }

.total-row {
  display: flex;
  align-items: baseline;
  justify-content: space-between;
  margin-top: 14px;
  padding-top: 14px;
  border-top: 1px solid var(--line);
}
.total-row > span:first-child { color: var(--muted); }
.total-value {
  font-size: 26px;
  font-weight: 700;
  background: linear-gradient(135deg, var(--accent), var(--accent-2));
  -webkit-background-clip: text;
  background-clip: text;
  -webkit-text-fill-color: transparent;
}

/* ===== Big single inputs ===== */
.big-input {
  width: 100%;
  background: var(--bg-2);
  border: 1px solid var(--line);
  border-radius: 12px;
  color: var(--text);
  padding: 16px;
  font-size: 26px;
  font-weight: 600;
  text-align: center;
}

/* ===== Tab bar ===== */
.tabbar {
  position: fixed;
  left: 0; right: 0; bottom: 0;
  display: flex;
  background: rgba(13,19,38,.92);
  backdrop-filter: blur(12px);
  -webkit-backdrop-filter: blur(12px);
  border-top: 1px solid var(--line);
  padding-bottom: var(--safe-bottom);
}
.tab {
  flex: 1;
  background: none;
  border: none;
  color: var(--muted);
  padding: 10px 0 12px;
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 3px;
  font-size: 12px;
  cursor: pointer;
}
.tab.active { color: var(--accent); }
.tab-icon { font-size: 22px; line-height: 1; }

/* ===== History / charts ===== */
.toggle { font-size: 13px; color: var(--muted); font-weight: 500; display: flex; align-items: center; gap: 6px; }
.toggle input { width: 16px; height: 16px; accent-color: var(--accent); }
.chart { width: 100%; }
.chart svg { display: block; width: 100%; height: auto; }
.chart-empty {
  display: flex; align-items: center; justify-content: center;
  height: 150px; color: var(--muted); font-size: 14px;
}
.chart-caption {
  text-align: center;
  font-size: 13px;
  color: var(--muted);
  margin-top: 6px;
  min-height: 18px;
}
.chart-caption b { color: var(--text); }
.legend { display: flex; flex-wrap: wrap; gap: 12px; margin-top: 8px; }
.legend-item { display: flex; align-items: center; gap: 6px; font-size: 12px; color: var(--muted); }
.legend-dot { width: 10px; height: 10px; border-radius: 3px; }

/* ===== Toast ===== */
.toast {
  position: fixed;
  left: 50%;
  bottom: calc(86px + var(--safe-bottom));
  transform: translateX(-50%) translateY(20px);
  background: #0f1830;
  border: 1px solid var(--line);
  color: var(--text);
  padding: 12px 18px;
  border-radius: 12px;
  font-size: 15px;
  opacity: 0;
  pointer-events: none;
  transition: opacity .2s, transform .2s;
  box-shadow: 0 12px 30px rgba(0,0,0,.4);
  z-index: 50;
}
.toast.show { opacity: 1; transform: translateX(-50%) translateY(0); }
.toast.error { border-color: var(--danger); }
