* {
  box-sizing: border-box;
}

html, body {
  margin: 0;
  padding: 0;
  height: 100%;
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, sans-serif;
  color: #1a1a1a;
  background: #fafafa;
  overscroll-behavior: none;
}

button {
  font: inherit;
}

ul {
  list-style: none;
  margin: 0;
  padding: 0;
}

/* App shell — height tracks the actual visible viewport (set via JS from
   window.visualViewport) so the layout shrinks correctly when the iOS
   on-screen keyboard is open, instead of relying on 100vh. */
.app {
  display: flex;
  flex-direction: column;
  height: 100dvh;
  height: var(--app-vh, 100dvh);
  overflow: hidden;
  /* In home-screen (standalone) mode on iOS the status bar's blur extends a
     little below the safe-area inset and fades over the search row, so add
     extra clearance — only when there is an inset (0 in regular Safari). */
  padding-top: calc(env(safe-area-inset-top) + min(env(safe-area-inset-top), 16px));
  padding-left: env(safe-area-inset-left);
  padding-right: env(safe-area-inset-right);
}

/* ---------- Ingredients panel ---------- */

.ingredients-panel {
  display: flex;
  flex-direction: column;
  min-height: 0;
  flex: 1;
  padding: 12px;
  gap: 10px;
}

.search-row {
  display: flex;
  align-items: center;
  gap: 8px;
  flex-shrink: 0;
}

.search-input {
  flex: 1;
  min-width: 0;
  font-size: 16px; /* prevents iOS Safari auto-zoom on focus */
  padding: 10px 12px;
  border: 1px solid #d0d0d0;
  border-radius: 8px;
  background: #fff;
}

.search-input:focus {
  outline: 2px solid #4a90d9;
  outline-offset: -1px;
}

.clear-btn {
  flex-shrink: 0;
  width: 40px;
  height: 40px;
  display: flex;
  align-items: center;
  justify-content: center;
  border: 1px solid #d0d0d0;
  border-radius: 8px;
  background: #fff;
  color: #555;
  cursor: pointer;
}

.clear-btn:active {
  background: #eee;
}

.ingredient-list {
  flex: 1;
  min-height: 0;
  overflow-y: auto;
  -webkit-overflow-scrolling: touch;
  border: 1px solid #eee;
  border-radius: 8px;
  background: #fff;
  /* Small scrollbar, invisible at rest, faded in via the .scrolling
     class (toggled from JS on scroll) — same look on mobile and desktop. */
  scrollbar-width: thin;
  scrollbar-color: transparent transparent;
}

.ingredient-list.scrolling {
  scrollbar-color: rgba(0, 0, 0, 0.35) transparent;
}

.ingredient-list::-webkit-scrollbar {
  width: 4px;
}

.ingredient-list::-webkit-scrollbar-track {
  background: transparent;
}

.ingredient-list::-webkit-scrollbar-thumb {
  background: transparent;
  border-radius: 4px;
  transition: background-color 0.3s ease;
}

.ingredient-list.scrolling::-webkit-scrollbar-thumb {
  background: rgba(0, 0, 0, 0.35);
}

.ingredient-item {
  padding: 10px 14px;
  border-bottom: 1px solid #f0f0f0;
  cursor: pointer;
  user-select: none;
}

.ingredient-item:last-child {
  border-bottom: none;
}

.ingredient-item.selected {
  background: #4a90d9;
  color: #fff;
}

.ingredient-item:active {
  background: #e8f0fb;
}

.ingredient-item.selected:active {
  background: #3f7fc0;
}

.ingredient-empty {
  padding: 14px;
  color: #999;
  font-size: 14px;
}

/* ---------- Results panel ---------- */

.results-panel {
  flex-shrink: 0;
}

.results-cards {
  display: none;
}

.results-chips {
  display: flex;
  flex-direction: column-reverse; /* grows upward from the bottom */
  gap: 6px;
  padding: 10px 12px;
  padding-bottom: calc(10px + env(safe-area-inset-bottom));
  max-height: calc(var(--app-vh, 100dvh) * 0.4);
  overflow-y: auto;
  flex-shrink: 1;
}

.result-chip {
  background: #fff;
  border: 1px solid #e0e0e0;
  border-radius: 999px;
  padding: 10px 16px;
  font-size: 16px;
  cursor: pointer;
  text-align: left;
}

.result-chip:active {
  background: #f0f0f0;
}

/* ---------- Desktop (wide) layout ---------- */

@media (min-width: 768px) {
  .app {
    flex-direction: row;
    overflow: visible;
    height: 100dvh;
  }

  .ingredients-panel {
    width: 38%;
    max-width: 420px;
    border-right: 1px solid #eee;
    height: 100%;
  }

  .results-panel {
    flex: 1;
    height: 100%;
    overflow-y: auto;
    padding: 16px;
  }

  .results-chips {
    display: none;
  }

  .results-cards {
    display: grid;
    grid-template-columns: repeat(auto-fill, minmax(180px, 1fr));
    gap: 16px;
  }
}

.recipe-card {
  background: #fff;
  border: 1px solid #eee;
  border-radius: 10px;
  overflow: hidden;
  cursor: pointer;
  text-align: left;
  padding: 0;
}

.recipe-card:hover {
  border-color: #4a90d9;
}

.recipe-card-photo {
  width: 100%;
  aspect-ratio: 1 / 1;
  object-fit: cover;
  display: block;
  background: #ddd;
}

.recipe-card-body {
  padding: 10px 12px;
}

.recipe-card-name {
  font-weight: 600;
  font-size: 15px;
  line-height: 1.3;
  margin: 0 0 4px;
  display: -webkit-box;
  -webkit-box-orient: vertical;
  -webkit-line-clamp: 2;
  line-clamp: 2;
  overflow: hidden;
  text-overflow: ellipsis;
  min-height: calc(1.3em * 2);
}

.recipe-card-count {
  font-size: 13px;
  color: #777;
  margin: 0;
}

.results-empty {
  color: #999;
  font-size: 16px;
  padding: 8px 4px;
}

/* ---------- Modal ---------- */

.modal-overlay {
  position: fixed;
  inset: 0;
  background: rgba(0, 0, 0, 0.5);
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 20px;
  z-index: 100;
}

.modal-overlay[hidden] {
  display: none;
}

.modal {
  background: #fff;
  border-radius: 14px;
  max-width: 480px;
  width: 100%;
  max-height: 85vh;
  overflow-y: auto;
  position: relative;
  padding: 0 0 20px;
}

.modal-close {
  position: absolute;
  top: 10px;
  right: 10px;
  width: 36px;
  height: 36px;
  border-radius: 50%;
  border: none;
  background: rgba(255, 255, 255, 0.9);
  display: flex;
  align-items: center;
  justify-content: center;
  cursor: pointer;
  z-index: 1;
}

.modal-photo {
  width: 100%;
  aspect-ratio: 1 / 1;
  object-fit: cover;
  display: block;
  background: #ddd;
  border-radius: 14px 14px 0 0;
}

.modal-title {
  margin: 16px 20px 8px;
  font-size: 20px;
}

.modal-ingredients {
  margin: 0 20px;
  padding-left: 18px;
  list-style: disc;
}

.modal-ingredients li {
  padding: 3px 0;
  font-size: 15px;
}
