/* === Reset & Base === */
*, *::before, *::after { box-sizing: border-box; margin: 0; padding: 0; }
:root {
  --green-900: #1B4332;
  --green-800: #2D6A4F;
  --green-700: #40916C;
  --green-600: #52B788;
  --green-500: #74C69D;
  --green-200: #D8F3DC;
  --green-100: #E9F5E9;
  --green-50: #F0FDF4;
  --gray-900: #1a1a2e;
  --gray-700: #333;
  --gray-500: #666;
  --gray-400: #999;
  --gray-200: #e0e0e0;
  --gray-100: #f5f5f5;
  --red-500: #e63946;
  --orange-500: #f4a261;
  --white: #fff;
  --shadow-sm: 0 1px 3px rgba(0,0,0,.08);
  --shadow-md: 0 2px 8px rgba(0,0,0,.1);
  --shadow-lg: 0 4px 16px rgba(0,0,0,.12);
  --radius: 10px;
  --radius-sm: 6px;
}
html { height: 100%; }
body {
  font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, sans-serif;
  background: var(--green-50);
  color: var(--gray-700);
  height: 100%;
  overflow: hidden;
  display: flex;
  flex-direction: column;
  -webkit-font-smoothing: antialiased;
}

/* === Header === */
#app-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 12px 16px;
  background: var(--green-800);
  color: var(--white);
  flex-shrink: 0;
  z-index: 10;
}
.app-title { font-size: 18px; font-weight: 700; letter-spacing: -.3px; }
.header-right { display: flex; gap: 4px; }

/* === Main Content === */
#main-content {
  flex: 1;
  overflow-y: auto;
  padding: 12px 16px 16px;
  -webkit-overflow-scrolling: touch;
}

/* === Tab System === */
.tab { display: none; }
.tab.active {
  display: block;
  animation: tabFadeIn .2s ease;
}
@keyframes tabFadeIn {
  from { opacity: 0; transform: translateY(6px); }
  to { opacity: 1; transform: translateY(0); }
}
.tab-header {
  display: flex; align-items: center; justify-content: space-between;
  margin-bottom: 12px;
}
.tab-header h2 { font-size: 16px; font-weight: 600; color: var(--green-900); }

/* === Bottom Nav === */
#bottom-nav {
  display: flex; background: var(--white);
  border-top: 1px solid var(--gray-200);
  flex-shrink: 0;
  padding-bottom: env(safe-area-inset-bottom, 0);
}
.nav-btn {
  flex: 1; display: flex; flex-direction: column; align-items: center;
  gap: 2px; padding: 8px 0 6px;
  border: none; background: transparent;
  color: var(--gray-400); font-size: 10px; cursor: pointer;
  transition: color .15s;
}
.nav-btn.active { color: var(--green-700); }
.nav-btn svg { display: block; }

/* === Loading Spinner === */
.spinner {
  display: inline-block;
  width: 16px;
  height: 16px;
  border: 2px solid rgba(255,255,255,.3);
  border-top-color: #fff;
  border-radius: 50%;
  animation: spin .6s linear infinite;
}
@keyframes spin { to { transform: rotate(360deg); } }

.btn-loading {
  position: relative;
  pointer-events: none;
  opacity: .7;
}
.btn-loading .btn-text { visibility: hidden; }
.btn-loading::after {
  content: '';
  position: absolute;
  inset: 0;
  display: flex;
  align-items: center;
  justify-content: center;
}
.btn-loading.btn-primary::after {
  content: '';
  width: 16px;
  height: 16px;
  border: 2px solid rgba(255,255,255,.3);
  border-top-color: #fff;
  border-radius: 50%;
  animation: spin .6s linear infinite;
}
.btn-loading.btn-outline::after {
  content: '';
  width: 16px;
  height: 16px;
  border: 2px solid rgba(45,106,79,.3);
  border-top-color: var(--green-700);
  border-radius: 50%;
  animation: spin .6s linear infinite;
}

/* === Loading Overlay (initial page load) === */
#loading-overlay {
  position: fixed; inset: 0; z-index: 999;
  background: var(--green-50);
  display: flex; flex-direction: column;
  align-items: center; justify-content: center;
  gap: 12px;
  transition: opacity .3s ease;
}
#loading-overlay.hidden {
  opacity: 0;
  pointer-events: none;
}
#loading-overlay .spinner-lg {
  width: 32px; height: 32px;
  border: 3px solid rgba(45,106,79,.2);
  border-top-color: var(--green-700);
  border-radius: 50%;
  animation: spin .7s linear infinite;
}
#loading-overlay p {
  font-size: 14px; color: var(--green-700); font-weight: 500;
}

@media (prefers-color-scheme: dark) {
  #loading-overlay { background: #0a1a12; }
}

/* === Buttons === */
.icon-btn {
  width: 36px; height: 36px; border: none; border-radius: 50%;
  background: transparent; cursor: pointer; display: flex;
  align-items: center; justify-content: center;
  color: inherit; transition: background .15s;
}
.icon-btn:hover { background: rgba(255,255,255,.15); }
.btn {
  display: inline-flex; align-items: center; gap: 4px;
  padding: 8px 16px; border-radius: var(--radius-sm);
  font-size: 13px; font-weight: 500; cursor: pointer;
  border: none; transition: all .15s;
}
.btn-primary { background: var(--green-700); color: var(--white); }
.btn-primary:hover { background: var(--green-800); }
.btn:disabled, .btn[disabled] { opacity: .5; cursor: not-allowed; }
.btn-primary:disabled:hover { background: var(--green-700); }
.btn-outline:disabled:hover { background: transparent; }
.btn-outline {
  background: transparent; border: 1.5px solid var(--green-600);
  color: var(--green-700);
}
.btn-outline:hover { background: var(--green-100); }
.btn-sm { padding: 5px 10px; font-size: 12px; }
.btn-danger { background: var(--red-500); color: white; }

/* === Cards / Grid === */
.card {
  background: var(--white); border-radius: var(--radius);
  padding: 12px; box-shadow: var(--shadow-sm);
}

/* === Week Nav === */
.week-nav { display: flex; align-items: center; gap: 2px; }
#week-label { font-size: 13px; font-weight: 500; color: var(--gray-500); min-width: 100px; text-align: center; }
.week-nav .icon-btn { color: var(--green-700); width: 32px; height: 32px; }
.week-nav .icon-btn:hover { background: var(--green-100); }

/* === Plan Grid === */
.plan-grid { display: flex; flex-direction: column; gap: 6px; margin-bottom: 12px; }
.plan-day {
  background: var(--white); border-radius: var(--radius); padding: 10px 12px;
  box-shadow: var(--shadow-sm);
}
.plan-day-header {
  display: flex; align-items: center; justify-content: space-between; margin-bottom: 6px;
}
.plan-day-name { font-size: 13px; font-weight: 600; color: var(--green-900); }
.plan-day-date { font-size: 11px; color: var(--gray-400); }
.plan-day.today .plan-day-name::after {
  content: ' • Today'; font-weight: 400; color: var(--green-700); font-size: 11px;
}
.plan-day.today { border-left: 3px solid var(--green-700); }
.plan-meals { display: flex; flex-direction: column; gap: 3px; }
.plan-meal {
  display: flex; align-items: center; justify-content: space-between;
  padding: 4px 8px; border-radius: 4px; font-size: 12px;
  cursor: pointer; transition: background .15s;
}
.plan-meal:hover { background: var(--green-100); }
.plan-meal.empty { color: var(--gray-400); font-style: italic; cursor: default; }
.plan-meal.empty:hover { background: transparent; }
.plan-meal-time {
  font-size: 10px; font-weight: 600; text-transform: uppercase;
  color: var(--gray-400); width: 48px; flex-shrink: 0;
}
.plan-meal-name { flex: 1; margin: 0 8px; }
.plan-meal-remove {
  width: 20px; height: 20px; border: none; border-radius: 50%;
  background: transparent; color: var(--gray-400); cursor: pointer;
  display: flex; align-items: center; justify-content: center; font-size: 14px;
}
.plan-meal-remove:hover { background: var(--red-500); color: white; }
.plan-meal.breakfast { border-left: 3px solid #f4a261; }
.plan-meal.lunch { border-left: 3px solid #52B788; }
.plan-meal.dinner { border-left: 3px solid #2D6A4F; }
.plan-meal.snack { border-left: 3px solid #e9c46a; }
.plan-actions { display: flex; gap: 8px; justify-content: center; }

/* === Meal Grid === */
.meal-grid { display: grid; gap: 8px; }
.meal-card {
  background: var(--white); border-radius: var(--radius); padding: 10px 12px;
  box-shadow: var(--shadow-sm); cursor: pointer; transition: box-shadow .15s;
  position: relative;
}
.meal-card:hover { box-shadow: var(--shadow-md); }
.meal-card h3 { font-size: 14px; font-weight: 600; color: var(--green-900); margin-bottom: 2px; }
.meal-card .meal-meta { font-size: 11px; color: var(--gray-400); display: flex; gap: 8px; flex-wrap: wrap; }
.meal-card .meal-ingredients { font-size: 11px; color: var(--gray-500); margin-top: 4px; }
.meal-badge {
  display: inline-block; padding: 1px 6px; border-radius: 3px;
  font-size: 10px; font-weight: 500;
}
.meal-badge.breakfast { background: #fef3c7; color: #92400e; }
.meal-badge.lunch { background: #d1fae5; color: #065f46; }
.meal-badge.dinner { background: #dbeafe; color: #1e40af; }
.meal-badge.snack { background: #fce7f3; color: #9d174d; }

/* === Filter Bar === */
.filter-bar {
  display: flex; gap: 8px; margin-bottom: 10px;
}
.search-input {
  flex: 1; padding: 7px 10px; border: 1.5px solid var(--gray-200);
  border-radius: var(--radius-sm); font-size: 13px; outline: none;
}
.search-input:focus { border-color: var(--green-600); }
.filter-select {
  padding: 7px 10px; border: 1.5px solid var(--gray-200);
  border-radius: var(--radius-sm); font-size: 13px; outline: none;
  background: var(--white); color: var(--gray-700);
}
.filter-select:focus { border-color: var(--green-600); }

/* === Shopping List === */
.list-items { margin-bottom: 10px; }
.list-item {
  display: flex; align-items: center; gap: 8px;
  padding: 8px 12px; background: var(--white); border-radius: var(--radius-sm);
  margin-bottom: 4px; box-shadow: var(--shadow-sm); cursor: pointer;
  transition: background .15s;
}
.list-item:hover { background: var(--green-100); }
.list-item input[type="checkbox"] {
  width: 18px; height: 18px; accent-color: var(--green-700); cursor: pointer;
}
.list-item-name { flex: 1; font-size: 13px; }
.list-item.checked .list-item-name { text-decoration: line-through; color: var(--gray-400); }
.list-item-amount { font-size: 12px; color: var(--gray-400); margin-right: 4px; }
.list-item-delete {
  width: 24px; height: 24px; border: none; border-radius: 50%;
  background: transparent; color: var(--gray-400); cursor: pointer;
  font-size: 14px; line-height: 1;
}
.list-item-delete:hover { background: var(--red-500); color: white; }
.list-footer {
  display: flex; align-items: center; justify-content: space-between;
  padding-top: 6px;
}
.list-stats { font-size: 12px; color: var(--gray-400); }

/* === Empty State === */
.empty-state {
  text-align: center; padding: 32px 16px; color: var(--gray-400);
}
.empty-state svg { margin-bottom: 8px; }
.empty-state p { font-size: 13px; }

/* === Modal === */
.modal-overlay {
  position: fixed; inset: 0; background: rgba(0,0,0,.4);
  z-index: 100; display: flex; align-items: flex-end; justify-content: center;
  padding-top: env(safe-area-inset-top, 0);
  transition: background .2s ease;
}
.modal-overlay.hidden { display: none; }
.modal-overlay.closing .modal {
  animation: slideDown .15s ease forwards;
}
.modal {
  background: var(--white); border-radius: var(--radius) var(--radius) 0 0;
  width: 100%; max-width: 480px; max-height: 85vh; overflow-y: auto;
  padding: 16px; animation: slideUp .2s ease;
}
@keyframes slideUp { from { transform: translateY(100%); } to { transform: translateY(0); } }
@keyframes slideDown { from { transform: translateY(0); opacity: 1; } to { transform: translateY(100%); opacity: 0; } }
.modal-header {
  display: flex; align-items: center; justify-content: space-between; margin-bottom: 14px;
}
.modal-header h3 { font-size: 16px; font-weight: 600; color: var(--green-900); }
.modal-header .icon-btn { color: var(--gray-500); }

/* === Form Fields in Modal === */
.modal label { display: block; font-size: 12px; font-weight: 500; color: var(--gray-500); margin-bottom: 3px; margin-top: 10px; }
.modal label:first-child { margin-top: 0; }
.modal input, .modal textarea, .modal select {
  width: 100%; padding: 8px 10px; border: 1.5px solid var(--gray-200);
  border-radius: var(--radius-sm); font-size: 13px; outline: none; font-family: inherit;
}
.modal input:focus, .modal textarea:focus { border-color: var(--green-600); }
.modal textarea { min-height: 60px; resize: vertical; }
.modal .btn { margin-top: 14px; width: 100%; justify-content: center; }
.modal .form-row { display: flex; gap: 8px; }
.modal .form-row > * { flex: 1; }
.ingredient-row {
  display: flex; gap: 4px; margin-bottom: 4px; align-items: center;
}
.ingredient-row input { flex: 1; }
.ingredient-row .icon-btn { color: var(--red-500); width: 28px; height: 28px; flex-shrink: 0; }
.ingredient-row .icon-btn:hover { background: var(--green-100); }

/* === List Header Actions Loading === */
.list-header-actions { display: flex; gap: 6px; }

/* === Keyboard Focus === */
:focus-visible {
  outline: 2px solid var(--green-600);
  outline-offset: 2px;
}
button:focus-visible:not(:active) {
  outline: 2px solid var(--green-600);
  outline-offset: 2px;
}

/* === Toast === */
.toast {
  position: fixed; bottom: 80px; left: 50%; transform: translateX(-50%);
  background: var(--green-800); color: white; padding: 8px 16px;
  border-radius: 20px; font-size: 13px; box-shadow: var(--shadow-lg);
  z-index: 200; transition: opacity .2s;
}
.toast.hidden { opacity: 0; pointer-events: none; }

/* === Dark Mode === */
@media (prefers-color-scheme: dark) {
  :root {
    --green-50: #0a1a12;
    --green-100: #0f2418;
    --green-200: #1a3a28;
    --gray-100: #1a1a2e;
    --gray-200: #2a2a3e;
    --gray-500: #aaa;
    --gray-400: #888;
    --gray-700: #ccc;
    --gray-900: #eee;
    --white: #1e1e2e;
  }
  .plan-day { border-color: var(--gray-200); }
  .plan-day.today { border-left-color: var(--green-600); }
  .modal input, .modal textarea, .modal select, .search-input, .filter-select {
    background: #2a2a3e; color: #ccc; border-color: #333;
  }
  .toast { background: var(--green-700); }
}

/* === Responsive tablet+ === */
@media (min-width: 640px) {
  #main-content { max-width: 600px; margin: 0 auto; }
  .meal-grid { grid-template-columns: 1fr 1fr; }
}
