:root {
  --bg: #faf9f5;
  --bg-alt: #ffffff;
  --text: #24291f;
  --text-muted: #5b6355;
  --border: #e1e4d8;
  --accent: #3f7d4a;
  --accent-contrast: #ffffff;
  --accent-soft: #e7f2e6;
  --warn-bg: #fff6e5;
  --warn-border: #f0d789;
  --chip-bg: #eef1e6;
  --chip-active-bg: #3f7d4a;
  --chip-active-text: #ffffff;
  --shadow: 0 1px 3px rgba(30, 40, 20, 0.08);
  font-size: 16px;
}

@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) {
    --bg: #14170f;
    --bg-alt: #1b2015;
    --text: #eef1e6;
    --text-muted: #a8b39a;
    --border: #2c3324;
    --accent: #6fbf73;
    --accent-contrast: #0d1309;
    --accent-soft: #223223;
    --warn-bg: #2a2311;
    --warn-border: #6b5a1e;
    --chip-bg: #232a1c;
    --chip-active-bg: #6fbf73;
    --chip-active-text: #0d1309;
  }
}

* { box-sizing: border-box; }

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

.wrap {
  max-width: 980px;
  margin: 0 auto;
  padding: 0 1.25rem;
}

a { color: var(--accent); }

/* Header */
.site-header {
  background: var(--bg-alt);
  border-bottom: 1px solid var(--border);
  padding: 1.5rem 0 0;
}
.site-header h1 { margin: 0 0 0.25rem; font-size: 1.6rem; }
.tagline { margin: 0 0 1.25rem; color: var(--text-muted); max-width: 60ch; }

.tabs {
  display: flex;
  flex-wrap: wrap;
  gap: 0.25rem;
  border-top: 1px solid var(--border);
  padding-top: 0.5rem;
}
.tab-btn {
  background: none;
  border: none;
  font: inherit;
  font-size: 0.9rem;
  color: var(--text-muted);
  padding: 0.6rem 0.85rem;
  cursor: pointer;
  border-radius: 8px 8px 0 0;
  white-space: nowrap;
}
.tab-btn:hover { background: var(--accent-soft); color: var(--text); }
.tab-btn.active {
  color: var(--accent);
  font-weight: 600;
  background: var(--bg);
  border: 1px solid var(--border);
  border-bottom: 1px solid var(--bg);
  margin-bottom: -1px;
}

main.wrap { padding-top: 1.5rem; padding-bottom: 3rem; }

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

/* Disclaimer */
.disclaimer, .disclaimer-inline {
  background: var(--warn-bg);
  border: 1px solid var(--warn-border);
  border-radius: 10px;
  padding: 0.85rem 1rem;
  font-size: 0.92rem;
  margin-bottom: 1.5rem;
}
.disclaimer-inline { margin-top: 1rem; }

/* Controls */
.controls {
  background: var(--bg-alt);
  border: 1px solid var(--border);
  border-radius: 12px;
  padding: 1rem;
  margin-bottom: 1rem;
}
.search-row {
  display: flex;
  gap: 0.6rem;
  flex-wrap: wrap;
  margin-bottom: 0.75rem;
}
#search {
  flex: 1 1 260px;
  padding: 0.6rem 0.8rem;
  border-radius: 8px;
  border: 1px solid var(--border);
  background: var(--bg);
  color: var(--text);
  font-size: 1rem;
}
#sort, #fruLevel {
  padding: 0.6rem 0.8rem;
  border-radius: 8px;
  border: 1px solid var(--border);
  background: var(--bg);
  color: var(--text);
  font-size: 0.95rem;
}
.cat-row {
  display: flex;
  flex-wrap: wrap;
  gap: 0.4rem;
  margin-bottom: 0.75rem;
}
.chip {
  border: 1px solid var(--border);
  background: var(--chip-bg);
  color: var(--text);
  padding: 0.35rem 0.7rem;
  border-radius: 999px;
  font-size: 0.85rem;
  cursor: pointer;
}
.chip.active {
  background: var(--chip-active-bg);
  color: var(--chip-active-text);
  border-color: var(--chip-active-bg);
}
.opt-row {
  display: flex;
  align-items: center;
  justify-content: space-between;
  flex-wrap: wrap;
  gap: 0.5rem;
  font-size: 0.88rem;
  color: var(--text-muted);
}
.checkbox { display: flex; align-items: center; gap: 0.4rem; cursor: pointer; }
.link-btn {
  background: none;
  border: none;
  color: var(--accent);
  cursor: pointer;
  font-size: 0.88rem;
  text-decoration: underline;
  padding: 0;
}

.count { color: var(--text-muted); font-size: 0.88rem; margin: 0 0 0.6rem; }
.hint { color: var(--text-muted); font-size: 0.85rem; margin: 0 0 0.6rem; }

.table-wrap { overflow-x: auto; border: 1px solid var(--border); border-radius: 12px; }
table {
  width: 100%;
  border-collapse: collapse;
  background: var(--bg-alt);
  font-size: 0.92rem;
}
th, td { text-align: left; padding: 0.6rem 0.75rem; border-bottom: 1px solid var(--border); vertical-align: top; }
thead th {
  position: sticky; top: 0;
  background: var(--accent-soft);
  font-size: 0.78rem;
  text-transform: uppercase;
  letter-spacing: 0.03em;
  color: var(--text-muted);
}
tbody tr:last-child td { border-bottom: none; }
tbody tr:hover { background: var(--accent-soft); }
td.cat-cell { white-space: nowrap; color: var(--text-muted); font-size: 0.85rem; }
td.note-cell { color: var(--text-muted); font-size: 0.85rem; }
td.num-cell { white-space: nowrap; font-variant-numeric: tabular-nums; }
td.qty-cell { white-space: nowrap; }
.qty-input {
  width: 5.5rem;
  padding: 0.35rem 0.5rem;
  border-radius: 6px;
  border: 1px solid var(--border);
  background: var(--bg);
  color: var(--text);
  font-family: inherit;
  font-size: 1rem; /* >=16px prevents iOS Safari auto-zoom on focus */
  font-variant-numeric: tabular-nums;
}
.qty-input:focus { outline: 2px solid var(--accent); outline-offset: 1px; }
td.result-cell {
  white-space: nowrap;
  font-weight: 600;
  color: var(--accent);
  font-variant-numeric: tabular-nums;
}

.empty { color: var(--text-muted); padding: 2rem 0; text-align: center; }

/* Tips */
h2 { font-size: 1.3rem; margin-top: 0; }
.card-grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(230px, 1fr));
  gap: 0.9rem;
  margin-bottom: 2rem;
}
.tip-card {
  background: var(--bg-alt);
  border: 1px solid var(--border);
  border-radius: 12px;
  padding: 1rem;
}
.tip-card h3 { margin: 0 0 0.4rem; font-size: 1rem; }
.tip-card p { margin: 0; font-size: 0.9rem; color: var(--text-muted); }

.meal-ideas { padding-left: 1.2rem; }
.meal-ideas li { margin-bottom: 0.5rem; }

/* Analysis */
.analysis p, .analysis ul { max-width: 72ch; }
.analysis li { margin-bottom: 0.4rem; }

/* Watchlist */
table.watchlist td:first-child { font-weight: 600; white-space: nowrap; }

/* Footer */
.site-footer {
  border-top: 1px solid var(--border);
  padding: 1.25rem 0;
  color: var(--text-muted);
  font-size: 0.82rem;
}

@media (max-width: 600px) {
  .site-header h1 { font-size: 1.35rem; }
}

/* ---- Mobile (<=700px): swipeable tabs/chips, table becomes a card list ---- */
@media (max-width: 700px) {
  .wrap { padding: 0 0.85rem; }

  /* Swipeable, single-line tab bar instead of wrapping to multiple rows */
  .tabs {
    flex-wrap: nowrap;
    overflow-x: auto;
    -webkit-overflow-scrolling: touch;
    scrollbar-width: none;
  }
  .tabs::-webkit-scrollbar { display: none; }
  .tab-btn {
    flex: 0 0 auto;
    min-height: 2.75rem;
    display: flex;
    align-items: center;
  }

  /* Swipeable category chips instead of wrapping to many rows */
  .cat-row {
    flex-wrap: nowrap;
    overflow-x: auto;
    -webkit-overflow-scrolling: touch;
    padding-bottom: 0.3rem;
    margin-bottom: 0.5rem;
  }
  .chip {
    flex: 0 0 auto;
    min-height: 2.25rem;
    display: flex;
    align-items: center;
  }

  .search-row { flex-direction: column; }
  #search, #sort, #fruLevel { width: 100%; flex: 0 0 auto; }

  /* Table -> stacked cards: each row becomes a card, each cell a labeled line */
  .table-wrap { overflow-x: visible; border: none; }
  #foodTable, #foodTable thead, #foodTable tbody, #foodTable tr, #foodTable td {
    display: block;
    width: 100%;
  }
  #foodTable thead { position: absolute; left: -9999px; top: -9999px; }
  #foodTable tbody tr {
    background: var(--bg-alt);
    border: 1px solid var(--border);
    border-radius: 12px;
    padding: 0.85rem 0.9rem;
    margin-bottom: 0.75rem;
    box-shadow: var(--shadow);
  }
  #foodTable td {
    border-bottom: none;
    padding: 0.3rem 0;
  }
  #foodTable td.name-cell {
    font-weight: 700;
    font-size: 1.02rem;
    padding-bottom: 0.5rem;
    border-bottom: 1px solid var(--border);
    margin-bottom: 0.35rem;
  }
  #foodTable td[data-label] {
    display: flex;
    justify-content: space-between;
    align-items: center;
    gap: 0.75rem;
  }
  #foodTable td[data-label]::before {
    content: attr(data-label);
    font-size: 0.72rem;
    font-weight: 600;
    text-transform: uppercase;
    letter-spacing: 0.03em;
    color: var(--text-muted);
    flex: 0 0 auto;
  }
  #foodTable td.note-cell {
    flex-direction: column;
    align-items: flex-start;
    color: var(--text-muted);
    font-size: 0.85rem;
  }
  #foodTable td.note-cell::before { margin-bottom: 0.2rem; }
  #foodTable td.qty-cell .qty-input { margin-left: auto; }
}
