:root {
  --bg: #f6f8fb;
  --card: #ffffff;
  --ink: #1a202c;
  --muted: #5a6578;
  --border: #d1d5db;
  --primary: #2563eb;
  --primary-dark: #1d4ed8;
  --danger: #dc2626;
  --success: #16a34a;
  --warning: #d97706;
  --radius: 12px;
  --shadow: 0 4px 12px rgba(0,0,0,0.06);
  --max-width: 900px;
}

* { box-sizing: border-box; }

body {
  margin: 0;
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Helvetica, Arial, sans-serif;
  background: var(--bg);
  color: var(--ink);
  line-height: 1.45;
}

button, input, select, textarea {
  font: inherit;
}

.app-header {
  background: var(--card);
  border-bottom: 1px solid var(--border);
  padding: 16px;
  display: flex;
  flex-wrap: wrap;
  gap: 16px;
  align-items: center;
  justify-content: space-between;
  position: sticky;
  top: 0;
  z-index: 10;
}

.brand {
  display: flex;
  gap: 12px;
  align-items: center;
}

.logo { font-size: 1.8rem; }
.brand h1 { margin: 0; font-size: 1.4rem; }
.tagline { margin: 0; font-size: 0.85rem; color: var(--muted); }

.tabs {
  display: flex;
  gap: 8px;
  background: var(--bg);
  padding: 4px;
  border-radius: 999px;
}

.tabs button {
  border: none;
  background: transparent;
  padding: 8px 16px;
  border-radius: 999px;
  color: var(--muted);
  cursor: pointer;
  font-weight: 600;
}

.tabs button.active,
.tabs button[aria-selected="true"] {
  background: var(--card);
  color: var(--primary);
  box-shadow: var(--shadow);
}

.link-bar { display: flex; align-items: center; gap: 8px; }

#app {
  max-width: var(--max-width);
  margin: 0 auto;
  padding: 16px;
}

.tab-panel { display: none; }
.tab-panel.active { display: block; }

.card {
  background: var(--card);
  border-radius: var(--radius);
  padding: 20px;
  box-shadow: var(--shadow);
  margin-bottom: 16px;
}

.card h2 { margin-top: 0; font-size: 1.2rem; }
.card h3 { font-size: 1rem; margin: 20px 0 10px; }

@media screen { .print-only { display: none; } }

.section-head {
  display: flex;
  justify-content: space-between;
  align-items: center;
  gap: 12px;
  flex-wrap: wrap;
}

.hint { color: var(--muted); font-size: 0.9rem; margin-top: -8px; }

.field {
  display: flex;
  flex-direction: column;
  gap: 4px;
  margin-bottom: 12px;
}

.field label { font-size: 0.85rem; font-weight: 600; color: var(--muted); }

input[type="text"], input[type="number"], select, .search {
  padding: 10px 12px;
  border: 1px solid var(--border);
  border-radius: 8px;
  background: #fff;
  color: var(--ink);
  width: 100%;
}

input[type="number"] { text-align: right; }

.grid-3, .grid-4 {
  display: grid;
  gap: 12px;
}
.grid-3 { grid-template-columns: repeat(3, 1fr); }
.grid-4 { grid-template-columns: repeat(4, 1fr); }

.advanced {
  margin: 16px 0;
  padding: 12px;
  background: var(--bg);
  border-radius: 8px;
}

.advanced summary {
  cursor: pointer;
  font-weight: 600;
  color: var(--muted);
}

.effective {
  margin-top: 16px;
  padding: 12px;
  background: #eff6ff;
  border-radius: 8px;
  color: var(--primary-dark);
  font-weight: 700;
  text-align: center;
}

.btn-primary, .btn-secondary, .icon-btn {
  border: none;
  border-radius: 8px;
  padding: 10px 14px;
  cursor: pointer;
  font-weight: 600;
}

.btn-primary {
  background: var(--primary);
  color: #fff;
}
.btn-primary:hover { background: var(--primary-dark); }

.btn-secondary {
  background: var(--bg);
  color: var(--ink);
  border: 1px solid var(--border);
}
.btn-secondary:hover { background: #e5e7eb; }

.icon-btn {
  background: transparent;
  color: var(--muted);
  font-size: 1.4rem;
  line-height: 1;
  padding: 4px 8px;
}

.empty-state {
  text-align: center;
  padding: 40px 16px;
  color: var(--muted);
}
.empty-state p { margin-bottom: 16px; }

.menu-slots { display: flex; flex-direction: column; gap: 16px; }

.slot-group h4 {
  margin: 0 0 8px;
  text-transform: uppercase;
  font-size: 0.75rem;
  letter-spacing: 0.05em;
  color: var(--muted);
}

.entry-list { display: flex; flex-direction: column; gap: 8px; }

.entry-row {
  display: grid;
  grid-template-columns: 1fr auto auto auto;
  gap: 10px;
  align-items: center;
  padding: 10px 12px;
  background: var(--bg);
  border-radius: 8px;
}

.entry-row .meal-name { font-weight: 600; }
.entry-row .meal-tags { font-size: 0.75rem; color: var(--muted); }

.entry-row select { padding: 6px 8px; }

.stepper {
  display: flex;
  align-items: center;
  gap: 4px;
}
.stepper button {
  width: 28px;
  height: 28px;
  border: 1px solid var(--border);
  background: #fff;
  border-radius: 6px;
  cursor: pointer;
  font-weight: 700;
}
.stepper input {
  width: 48px;
  text-align: center;
  padding: 4px;
}

.remove-btn {
  background: transparent;
  border: none;
  color: var(--danger);
  cursor: pointer;
  font-size: 1.2rem;
}

.shopping-summary {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(140px, 1fr));
  gap: 12px;
  margin-bottom: 20px;
}

.summary-card {
  background: var(--bg);
  border-radius: 8px;
  padding: 12px;
  text-align: center;
}

.summary-card .value { font-size: 1.4rem; font-weight: 700; }
.summary-card .label { font-size: 0.75rem; color: var(--muted); text-transform: uppercase; }

.dietary-summary {
  background: #fffbeb;
  border: 1px solid #fde68a;
  border-radius: 8px;
  padding: 12px 16px;
  margin-bottom: 20px;
}
.dietary-summary h4 { margin: 0 0 8px; }
.dietary-summary ul { margin: 0; padding-left: 20px; }
.dietary-summary .chips { display: flex; flex-wrap: wrap; gap: 6px; margin-top: 4px; }
.chip {
  display: inline-block;
  font-size: 0.75rem;
  padding: 3px 8px;
  border-radius: 999px;
  background: #e5e7eb;
  color: var(--ink);
}
.chip.vegetarian { background: #dcfce7; }
.chip.nut_free { background: #ffedd5; }
.chip.gluten_free { background: #dbeafe; }
.chip.dairy_free { background: #fce7f3; }

.shopping-list { display: flex; flex-direction: column; gap: 20px; }

.aisle-group h4 {
  margin: 0 0 10px;
  padding-bottom: 6px;
  border-bottom: 2px solid var(--primary);
  text-transform: uppercase;
  font-size: 0.85rem;
}

.aisle-group ul { list-style: none; margin: 0; padding: 0; }
.aisle-group li {
  display: grid;
  grid-template-columns: 1fr auto auto auto;
  gap: 10px;
  align-items: center;
  padding: 8px 0;
  border-bottom: 1px solid var(--border);
}

.item-name { font-weight: 600; }
.item-detail { font-size: 0.8rem; color: var(--muted); }

.item-inputs {
  display: flex;
  gap: 8px;
  align-items: center;
}

.item-inputs input {
  width: 80px;
  padding: 6px 8px;
  text-align: right;
}

.item-cost {
  text-align: right;
  font-weight: 700;
  min-width: 70px;
}

.unpriced { color: var(--muted); font-style: italic; }

.modal {
  position: fixed;
  inset: 0;
  display: flex;
  align-items: flex-start;
  justify-content: center;
  z-index: 20;
  padding: 20px;
}
.modal[hidden] { display: none; }

.modal-backdrop {
  position: absolute;
  inset: 0;
  background: rgba(0,0,0,0.45);
}

.modal-card {
  position: relative;
  background: var(--card);
  border-radius: var(--radius);
  box-shadow: 0 20px 40px rgba(0,0,0,0.2);
  width: 100%;
  max-width: 640px;
  max-height: 90vh;
  display: flex;
  flex-direction: column;
  margin-top: 5vh;
}

.modal-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 16px 20px;
  border-bottom: 1px solid var(--border);
}

.modal-header h2 { margin: 0; font-size: 1.15rem; }

.modal-body { padding: 16px 20px; overflow-y: auto; }

.sub-tabs { margin-bottom: 12px; }
.sub-panel { display: none; }
.sub-panel.active { display: block; }

.library-list {
  display: flex;
  flex-direction: column;
  gap: 8px;
  max-height: 320px;
  overflow-y: auto;
}

.library-item {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 10px 12px;
  border: 1px solid var(--border);
  border-radius: 8px;
  cursor: pointer;
}
.library-item:hover { background: var(--bg); }

.diet-tags {
  border: none;
  padding: 0;
  margin: 12px 0;
  display: flex;
  flex-wrap: wrap;
  gap: 12px;
}
.diet-tags legend { font-weight: 600; width: 100%; margin-bottom: 6px; }

.ingredient-table {
  width: 100%;
  border-collapse: collapse;
  margin-bottom: 10px;
}
.ingredient-table th, .ingredient-table td {
  text-align: left;
  padding: 6px 4px;
  font-size: 0.9rem;
}
.ingredient-table input, .ingredient-table select {
  width: 100%;
  padding: 6px 8px;
}

.modal-footer {
  display: flex;
  justify-content: flex-end;
  gap: 10px;
  padding: 14px 20px;
  border-top: 1px solid var(--border);
}

.toast {
  position: fixed;
  bottom: 20px;
  left: 50%;
  transform: translateX(-50%) translateY(120%);
  background: var(--ink);
  color: #fff;
  padding: 10px 18px;
  border-radius: 999px;
  font-weight: 600;
  z-index: 30;
  opacity: 0;
  transition: transform 0.2s, opacity 0.2s;
  pointer-events: none;
}
.toast.show {
  transform: translateX(-50%) translateY(0);
  opacity: 1;
}

.app-footer {
  text-align: center;
  padding: 24px 16px;
  color: var(--muted);
  font-size: 0.85rem;
}

@media (max-width: 640px) {
  .app-header { flex-direction: column; align-items: stretch; }
  .tabs { justify-content: center; }
  .grid-3 { grid-template-columns: 1fr; }
  .grid-4 { grid-template-columns: repeat(2, 1fr); }
  .entry-row, .aisle-group li {
    grid-template-columns: 1fr;
    gap: 8px;
  }
  .stepper { justify-content: flex-start; }
  .shopping-summary { grid-template-columns: repeat(2, 1fr); }
  .modal-card { margin-top: 0; max-height: 100vh; border-radius: 0; }
}
